18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2018 Socionext Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/clk.h>
78c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
88c2ecf20Sopenharmony_ci#include <linux/irq.h>
98c2ecf20Sopenharmony_ci#include <linux/irqreturn.h>
108c2ecf20Sopenharmony_ci#include <linux/sched_clock.h>
118c2ecf20Sopenharmony_ci#include "timer-of.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define MLB_TMR_TMCSR_OFS	0x0
148c2ecf20Sopenharmony_ci#define MLB_TMR_TMR_OFS		0x4
158c2ecf20Sopenharmony_ci#define MLB_TMR_TMRLR1_OFS	0x8
168c2ecf20Sopenharmony_ci#define MLB_TMR_TMRLR2_OFS	0xc
178c2ecf20Sopenharmony_ci#define MLB_TMR_REGSZPCH	0x10
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define MLB_TMR_TMCSR_OUTL	BIT(5)
208c2ecf20Sopenharmony_ci#define MLB_TMR_TMCSR_RELD	BIT(4)
218c2ecf20Sopenharmony_ci#define MLB_TMR_TMCSR_INTE	BIT(3)
228c2ecf20Sopenharmony_ci#define MLB_TMR_TMCSR_UF	BIT(2)
238c2ecf20Sopenharmony_ci#define MLB_TMR_TMCSR_CNTE	BIT(1)
248c2ecf20Sopenharmony_ci#define MLB_TMR_TMCSR_TRG	BIT(0)
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define MLB_TMR_TMCSR_CSL_DIV2	0
278c2ecf20Sopenharmony_ci#define MLB_TMR_DIV_CNT		2
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define MLB_TMR_SRC_CH		1
308c2ecf20Sopenharmony_ci#define MLB_TMR_EVT_CH		0
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define MLB_TMR_SRC_CH_OFS	(MLB_TMR_REGSZPCH * MLB_TMR_SRC_CH)
338c2ecf20Sopenharmony_ci#define MLB_TMR_EVT_CH_OFS	(MLB_TMR_REGSZPCH * MLB_TMR_EVT_CH)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define MLB_TMR_SRC_TMCSR_OFS	(MLB_TMR_SRC_CH_OFS + MLB_TMR_TMCSR_OFS)
368c2ecf20Sopenharmony_ci#define MLB_TMR_SRC_TMR_OFS	(MLB_TMR_SRC_CH_OFS + MLB_TMR_TMR_OFS)
378c2ecf20Sopenharmony_ci#define MLB_TMR_SRC_TMRLR1_OFS	(MLB_TMR_SRC_CH_OFS + MLB_TMR_TMRLR1_OFS)
388c2ecf20Sopenharmony_ci#define MLB_TMR_SRC_TMRLR2_OFS	(MLB_TMR_SRC_CH_OFS + MLB_TMR_TMRLR2_OFS)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define MLB_TMR_EVT_TMCSR_OFS	(MLB_TMR_EVT_CH_OFS + MLB_TMR_TMCSR_OFS)
418c2ecf20Sopenharmony_ci#define MLB_TMR_EVT_TMR_OFS	(MLB_TMR_EVT_CH_OFS + MLB_TMR_TMR_OFS)
428c2ecf20Sopenharmony_ci#define MLB_TMR_EVT_TMRLR1_OFS	(MLB_TMR_EVT_CH_OFS + MLB_TMR_TMRLR1_OFS)
438c2ecf20Sopenharmony_ci#define MLB_TMR_EVT_TMRLR2_OFS	(MLB_TMR_EVT_CH_OFS + MLB_TMR_TMRLR2_OFS)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define MLB_TIMER_RATING	500
468c2ecf20Sopenharmony_ci#define MLB_TIMER_ONESHOT	0
478c2ecf20Sopenharmony_ci#define MLB_TIMER_PERIODIC	1
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic irqreturn_t mlb_timer_interrupt(int irq, void *dev_id)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct clock_event_device *clk = dev_id;
528c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(clk);
538c2ecf20Sopenharmony_ci	u32 val;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	val = readl_relaxed(timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
568c2ecf20Sopenharmony_ci	val &= ~MLB_TMR_TMCSR_UF;
578c2ecf20Sopenharmony_ci	writel_relaxed(val, timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	clk->event_handler(clk);
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic void mlb_evt_timer_start(struct timer_of *to, bool periodic)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	u32 val = MLB_TMR_TMCSR_CSL_DIV2;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	val |= MLB_TMR_TMCSR_CNTE | MLB_TMR_TMCSR_TRG | MLB_TMR_TMCSR_INTE;
698c2ecf20Sopenharmony_ci	if (periodic)
708c2ecf20Sopenharmony_ci		val |= MLB_TMR_TMCSR_RELD;
718c2ecf20Sopenharmony_ci	writel_relaxed(val, timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void mlb_evt_timer_stop(struct timer_of *to)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	u32 val = readl_relaxed(timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	val &= ~MLB_TMR_TMCSR_CNTE;
798c2ecf20Sopenharmony_ci	writel_relaxed(val, timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic void mlb_evt_timer_register_count(struct timer_of *to, unsigned long cnt)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	writel_relaxed(cnt, timer_of_base(to) + MLB_TMR_EVT_TMRLR1_OFS);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int mlb_set_state_periodic(struct clock_event_device *clk)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(clk);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	mlb_evt_timer_stop(to);
928c2ecf20Sopenharmony_ci	mlb_evt_timer_register_count(to, to->of_clk.period);
938c2ecf20Sopenharmony_ci	mlb_evt_timer_start(to, MLB_TIMER_PERIODIC);
948c2ecf20Sopenharmony_ci	return 0;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic int mlb_set_state_oneshot(struct clock_event_device *clk)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(clk);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	mlb_evt_timer_stop(to);
1028c2ecf20Sopenharmony_ci	mlb_evt_timer_start(to, MLB_TIMER_ONESHOT);
1038c2ecf20Sopenharmony_ci	return 0;
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic int mlb_set_state_shutdown(struct clock_event_device *clk)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(clk);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	mlb_evt_timer_stop(to);
1118c2ecf20Sopenharmony_ci	return 0;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic int mlb_clkevt_next_event(unsigned long event,
1158c2ecf20Sopenharmony_ci				   struct clock_event_device *clk)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	struct timer_of *to = to_timer_of(clk);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	mlb_evt_timer_stop(to);
1208c2ecf20Sopenharmony_ci	mlb_evt_timer_register_count(to, event);
1218c2ecf20Sopenharmony_ci	mlb_evt_timer_start(to, MLB_TIMER_ONESHOT);
1228c2ecf20Sopenharmony_ci	return 0;
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic int mlb_config_clock_source(struct timer_of *to)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	u32 val = MLB_TMR_TMCSR_CSL_DIV2;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	writel_relaxed(val, timer_of_base(to) + MLB_TMR_SRC_TMCSR_OFS);
1308c2ecf20Sopenharmony_ci	writel_relaxed(~0, timer_of_base(to) + MLB_TMR_SRC_TMRLR1_OFS);
1318c2ecf20Sopenharmony_ci	writel_relaxed(~0, timer_of_base(to) + MLB_TMR_SRC_TMRLR2_OFS);
1328c2ecf20Sopenharmony_ci	val |= MLB_TMR_TMCSR_RELD | MLB_TMR_TMCSR_CNTE | MLB_TMR_TMCSR_TRG;
1338c2ecf20Sopenharmony_ci	writel_relaxed(val, timer_of_base(to) + MLB_TMR_SRC_TMCSR_OFS);
1348c2ecf20Sopenharmony_ci	return 0;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic int mlb_config_clock_event(struct timer_of *to)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	writel_relaxed(0, timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
1408c2ecf20Sopenharmony_ci	return 0;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic struct timer_of to = {
1448c2ecf20Sopenharmony_ci	.flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	.clkevt = {
1478c2ecf20Sopenharmony_ci		.name = "mlb-clkevt",
1488c2ecf20Sopenharmony_ci		.rating = MLB_TIMER_RATING,
1498c2ecf20Sopenharmony_ci		.cpumask = cpu_possible_mask,
1508c2ecf20Sopenharmony_ci		.features = CLOCK_EVT_FEAT_DYNIRQ | CLOCK_EVT_FEAT_ONESHOT,
1518c2ecf20Sopenharmony_ci		.set_state_oneshot = mlb_set_state_oneshot,
1528c2ecf20Sopenharmony_ci		.set_state_periodic = mlb_set_state_periodic,
1538c2ecf20Sopenharmony_ci		.set_state_shutdown = mlb_set_state_shutdown,
1548c2ecf20Sopenharmony_ci		.set_next_event = mlb_clkevt_next_event,
1558c2ecf20Sopenharmony_ci	},
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	.of_irq = {
1588c2ecf20Sopenharmony_ci		.flags = IRQF_TIMER | IRQF_IRQPOLL,
1598c2ecf20Sopenharmony_ci		.handler = mlb_timer_interrupt,
1608c2ecf20Sopenharmony_ci	},
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic u64 notrace mlb_timer_sched_read(void)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	return ~readl_relaxed(timer_of_base(&to) + MLB_TMR_SRC_TMR_OFS);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic int __init mlb_timer_init(struct device_node *node)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	int ret;
1718c2ecf20Sopenharmony_ci	unsigned long rate;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	ret = timer_of_init(node, &to);
1748c2ecf20Sopenharmony_ci	if (ret)
1758c2ecf20Sopenharmony_ci		return ret;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	rate = timer_of_rate(&to) / MLB_TMR_DIV_CNT;
1788c2ecf20Sopenharmony_ci	mlb_config_clock_source(&to);
1798c2ecf20Sopenharmony_ci	clocksource_mmio_init(timer_of_base(&to) + MLB_TMR_SRC_TMR_OFS,
1808c2ecf20Sopenharmony_ci		node->name, rate, MLB_TIMER_RATING, 32,
1818c2ecf20Sopenharmony_ci		clocksource_mmio_readl_down);
1828c2ecf20Sopenharmony_ci	sched_clock_register(mlb_timer_sched_read, 32, rate);
1838c2ecf20Sopenharmony_ci	mlb_config_clock_event(&to);
1848c2ecf20Sopenharmony_ci	clockevents_config_and_register(&to.clkevt, timer_of_rate(&to), 15,
1858c2ecf20Sopenharmony_ci		0xffffffff);
1868c2ecf20Sopenharmony_ci	return 0;
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(mlb_peritimer, "socionext,milbeaut-timer",
1898c2ecf20Sopenharmony_ci		mlb_timer_init);
190