11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b53cdd03SAlexandre Belloni /*
3b53cdd03SAlexandre Belloni  * linux/arch/arm/mach-at91/at91rm9200_time.c
4b53cdd03SAlexandre Belloni  *
5b53cdd03SAlexandre Belloni  *  Copyright (C) 2003 SAN People
6b53cdd03SAlexandre Belloni  *  Copyright (C) 2003 ATMEL
7b53cdd03SAlexandre Belloni  */
8b53cdd03SAlexandre Belloni 
9b53cdd03SAlexandre Belloni #include <linux/kernel.h>
10b53cdd03SAlexandre Belloni #include <linux/interrupt.h>
11b53cdd03SAlexandre Belloni #include <linux/irq.h>
12216ab8f1SAlexandre Belloni #include <linux/clk.h>
13b53cdd03SAlexandre Belloni #include <linux/clockchips.h>
14b53cdd03SAlexandre Belloni #include <linux/export.h>
15adf2edfdSAlexandre Belloni #include <linux/mfd/syscon.h>
16adf2edfdSAlexandre Belloni #include <linux/mfd/syscon/atmel-st.h>
17b53cdd03SAlexandre Belloni #include <linux/of_irq.h>
18adf2edfdSAlexandre Belloni #include <linux/regmap.h>
19b53cdd03SAlexandre Belloni 
20b53cdd03SAlexandre Belloni static unsigned long last_crtr;
21b53cdd03SAlexandre Belloni static u32 irqmask;
22b53cdd03SAlexandre Belloni static struct clock_event_device clkevt;
23adf2edfdSAlexandre Belloni static struct regmap *regmap_st;
24216ab8f1SAlexandre Belloni static int timer_latch;
25b53cdd03SAlexandre Belloni 
26b53cdd03SAlexandre Belloni /*
27b53cdd03SAlexandre Belloni  * The ST_CRTR is updated asynchronously to the master clock ... but
28b53cdd03SAlexandre Belloni  * the updates as seen by the CPU don't seem to be strictly monotonic.
29b53cdd03SAlexandre Belloni  * Waiting until we read the same value twice avoids glitching.
30b53cdd03SAlexandre Belloni  */
read_CRTR(void)31b53cdd03SAlexandre Belloni static inline unsigned long read_CRTR(void)
32b53cdd03SAlexandre Belloni {
33adf2edfdSAlexandre Belloni 	unsigned int x1, x2;
34b53cdd03SAlexandre Belloni 
35adf2edfdSAlexandre Belloni 	regmap_read(regmap_st, AT91_ST_CRTR, &x1);
36b53cdd03SAlexandre Belloni 	do {
37adf2edfdSAlexandre Belloni 		regmap_read(regmap_st, AT91_ST_CRTR, &x2);
38b53cdd03SAlexandre Belloni 		if (x1 == x2)
39b53cdd03SAlexandre Belloni 			break;
40b53cdd03SAlexandre Belloni 		x1 = x2;
41b53cdd03SAlexandre Belloni 	} while (1);
42b53cdd03SAlexandre Belloni 	return x1;
43b53cdd03SAlexandre Belloni }
44b53cdd03SAlexandre Belloni 
45b53cdd03SAlexandre Belloni /*
46b53cdd03SAlexandre Belloni  * IRQ handler for the timer.
47b53cdd03SAlexandre Belloni  */
at91rm9200_timer_interrupt(int irq,void * dev_id)48b53cdd03SAlexandre Belloni static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
49b53cdd03SAlexandre Belloni {
50adf2edfdSAlexandre Belloni 	u32 sr;
51adf2edfdSAlexandre Belloni 
52adf2edfdSAlexandre Belloni 	regmap_read(regmap_st, AT91_ST_SR, &sr);
53adf2edfdSAlexandre Belloni 	sr &= irqmask;
54b53cdd03SAlexandre Belloni 
55b53cdd03SAlexandre Belloni 	/*
56b53cdd03SAlexandre Belloni 	 * irqs should be disabled here, but as the irq is shared they are only
57b53cdd03SAlexandre Belloni 	 * guaranteed to be off if the timer irq is registered first.
58b53cdd03SAlexandre Belloni 	 */
59b53cdd03SAlexandre Belloni 	WARN_ON_ONCE(!irqs_disabled());
60b53cdd03SAlexandre Belloni 
61b53cdd03SAlexandre Belloni 	/* simulate "oneshot" timer with alarm */
62b53cdd03SAlexandre Belloni 	if (sr & AT91_ST_ALMS) {
63b53cdd03SAlexandre Belloni 		clkevt.event_handler(&clkevt);
64b53cdd03SAlexandre Belloni 		return IRQ_HANDLED;
65b53cdd03SAlexandre Belloni 	}
66b53cdd03SAlexandre Belloni 
67b53cdd03SAlexandre Belloni 	/* periodic mode should handle delayed ticks */
68b53cdd03SAlexandre Belloni 	if (sr & AT91_ST_PITS) {
69b53cdd03SAlexandre Belloni 		u32	crtr = read_CRTR();
70b53cdd03SAlexandre Belloni 
71216ab8f1SAlexandre Belloni 		while (((crtr - last_crtr) & AT91_ST_CRTV) >= timer_latch) {
72216ab8f1SAlexandre Belloni 			last_crtr += timer_latch;
73b53cdd03SAlexandre Belloni 			clkevt.event_handler(&clkevt);
74b53cdd03SAlexandre Belloni 		}
75b53cdd03SAlexandre Belloni 		return IRQ_HANDLED;
76b53cdd03SAlexandre Belloni 	}
77b53cdd03SAlexandre Belloni 
78b53cdd03SAlexandre Belloni 	/* this irq is shared ... */
79b53cdd03SAlexandre Belloni 	return IRQ_NONE;
80b53cdd03SAlexandre Belloni }
81b53cdd03SAlexandre Belloni 
read_clk32k(struct clocksource * cs)82a5a1d1c2SThomas Gleixner static u64 read_clk32k(struct clocksource *cs)
83b53cdd03SAlexandre Belloni {
84b53cdd03SAlexandre Belloni 	return read_CRTR();
85b53cdd03SAlexandre Belloni }
86b53cdd03SAlexandre Belloni 
87b53cdd03SAlexandre Belloni static struct clocksource clk32k = {
88b53cdd03SAlexandre Belloni 	.name		= "32k_counter",
89b53cdd03SAlexandre Belloni 	.rating		= 150,
90b53cdd03SAlexandre Belloni 	.read		= read_clk32k,
91b53cdd03SAlexandre Belloni 	.mask		= CLOCKSOURCE_MASK(20),
92b53cdd03SAlexandre Belloni 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
93b53cdd03SAlexandre Belloni };
94b53cdd03SAlexandre Belloni 
clkdev32k_disable_and_flush_irq(void)958ab28230SViresh Kumar static void clkdev32k_disable_and_flush_irq(void)
96b53cdd03SAlexandre Belloni {
97adf2edfdSAlexandre Belloni 	unsigned int val;
98adf2edfdSAlexandre Belloni 
99b53cdd03SAlexandre Belloni 	/* Disable and flush pending timer interrupts */
100adf2edfdSAlexandre Belloni 	regmap_write(regmap_st, AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
101adf2edfdSAlexandre Belloni 	regmap_read(regmap_st, AT91_ST_SR, &val);
102b53cdd03SAlexandre Belloni 	last_crtr = read_CRTR();
1038ab28230SViresh Kumar }
1048ab28230SViresh Kumar 
clkevt32k_shutdown(struct clock_event_device * evt)1058ab28230SViresh Kumar static int clkevt32k_shutdown(struct clock_event_device *evt)
1068ab28230SViresh Kumar {
1078ab28230SViresh Kumar 	clkdev32k_disable_and_flush_irq();
1088ab28230SViresh Kumar 	irqmask = 0;
1098ab28230SViresh Kumar 	regmap_write(regmap_st, AT91_ST_IER, irqmask);
1108ab28230SViresh Kumar 	return 0;
1118ab28230SViresh Kumar }
1128ab28230SViresh Kumar 
clkevt32k_set_oneshot(struct clock_event_device * dev)1138ab28230SViresh Kumar static int clkevt32k_set_oneshot(struct clock_event_device *dev)
1148ab28230SViresh Kumar {
1158ab28230SViresh Kumar 	clkdev32k_disable_and_flush_irq();
1168ab28230SViresh Kumar 
1178ab28230SViresh Kumar 	/*
1188ab28230SViresh Kumar 	 * ALM for oneshot irqs, set by next_event()
1198ab28230SViresh Kumar 	 * before 32 seconds have passed.
120b53cdd03SAlexandre Belloni 	 */
121b53cdd03SAlexandre Belloni 	irqmask = AT91_ST_ALMS;
122adf2edfdSAlexandre Belloni 	regmap_write(regmap_st, AT91_ST_RTAR, last_crtr);
123adf2edfdSAlexandre Belloni 	regmap_write(regmap_st, AT91_ST_IER, irqmask);
1248ab28230SViresh Kumar 	return 0;
1258ab28230SViresh Kumar }
1268ab28230SViresh Kumar 
clkevt32k_set_periodic(struct clock_event_device * dev)1278ab28230SViresh Kumar static int clkevt32k_set_periodic(struct clock_event_device *dev)
1288ab28230SViresh Kumar {
1298ab28230SViresh Kumar 	clkdev32k_disable_and_flush_irq();
1308ab28230SViresh Kumar 
1318ab28230SViresh Kumar 	/* PIT for periodic irqs; fixed rate of 1/HZ */
1328ab28230SViresh Kumar 	irqmask = AT91_ST_PITS;
133216ab8f1SAlexandre Belloni 	regmap_write(regmap_st, AT91_ST_PIMR, timer_latch);
1348ab28230SViresh Kumar 	regmap_write(regmap_st, AT91_ST_IER, irqmask);
1358ab28230SViresh Kumar 	return 0;
136b53cdd03SAlexandre Belloni }
137b53cdd03SAlexandre Belloni 
138b53cdd03SAlexandre Belloni static int
clkevt32k_next_event(unsigned long delta,struct clock_event_device * dev)139b53cdd03SAlexandre Belloni clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
140b53cdd03SAlexandre Belloni {
141b53cdd03SAlexandre Belloni 	u32		alm;
142adf2edfdSAlexandre Belloni 	unsigned int	val;
143b53cdd03SAlexandre Belloni 
144b53cdd03SAlexandre Belloni 	BUG_ON(delta < 2);
145b53cdd03SAlexandre Belloni 
146b53cdd03SAlexandre Belloni 	/* The alarm IRQ uses absolute time (now+delta), not the relative
147b53cdd03SAlexandre Belloni 	 * time (delta) in our calling convention.  Like all clockevents
148b53cdd03SAlexandre Belloni 	 * using such "match" hardware, we have a race to defend against.
149b53cdd03SAlexandre Belloni 	 *
150b53cdd03SAlexandre Belloni 	 * Our defense here is to have set up the clockevent device so the
151b53cdd03SAlexandre Belloni 	 * delta is at least two.  That way we never end up writing RTAR
152b53cdd03SAlexandre Belloni 	 * with the value then held in CRTR ... which would mean the match
153b53cdd03SAlexandre Belloni 	 * wouldn't trigger until 32 seconds later, after CRTR wraps.
154b53cdd03SAlexandre Belloni 	 */
155b53cdd03SAlexandre Belloni 	alm = read_CRTR();
156b53cdd03SAlexandre Belloni 
157b53cdd03SAlexandre Belloni 	/* Cancel any pending alarm; flush any pending IRQ */
158adf2edfdSAlexandre Belloni 	regmap_write(regmap_st, AT91_ST_RTAR, alm);
159adf2edfdSAlexandre Belloni 	regmap_read(regmap_st, AT91_ST_SR, &val);
160b53cdd03SAlexandre Belloni 
161b53cdd03SAlexandre Belloni 	/* Schedule alarm by writing RTAR. */
162b53cdd03SAlexandre Belloni 	alm += delta;
163adf2edfdSAlexandre Belloni 	regmap_write(regmap_st, AT91_ST_RTAR, alm);
164b53cdd03SAlexandre Belloni 
1658c42c0f7SJason Yan 	return 0;
166b53cdd03SAlexandre Belloni }
167b53cdd03SAlexandre Belloni 
168b53cdd03SAlexandre Belloni static struct clock_event_device clkevt = {
169b53cdd03SAlexandre Belloni 	.name			= "at91_tick",
1708ab28230SViresh Kumar 	.features		= CLOCK_EVT_FEAT_PERIODIC |
1718ab28230SViresh Kumar 				  CLOCK_EVT_FEAT_ONESHOT,
172b53cdd03SAlexandre Belloni 	.rating			= 150,
173b53cdd03SAlexandre Belloni 	.set_next_event		= clkevt32k_next_event,
1748ab28230SViresh Kumar 	.set_state_shutdown	= clkevt32k_shutdown,
1758ab28230SViresh Kumar 	.set_state_periodic	= clkevt32k_set_periodic,
1768ab28230SViresh Kumar 	.set_state_oneshot	= clkevt32k_set_oneshot,
1778ab28230SViresh Kumar 	.tick_resume		= clkevt32k_shutdown,
178b53cdd03SAlexandre Belloni };
179b53cdd03SAlexandre Belloni 
180b53cdd03SAlexandre Belloni /*
181b53cdd03SAlexandre Belloni  * ST (system timer) module supports both clockevents and clocksource.
182b53cdd03SAlexandre Belloni  */
atmel_st_timer_init(struct device_node * node)183adbaf525SDaniel Lezcano static int __init atmel_st_timer_init(struct device_node *node)
184b53cdd03SAlexandre Belloni {
185216ab8f1SAlexandre Belloni 	struct clk *sclk;
186216ab8f1SAlexandre Belloni 	unsigned int sclk_rate, val;
1870afb46b2SAlexandre Belloni 	int irq, ret;
188adf2edfdSAlexandre Belloni 
189adf2edfdSAlexandre Belloni 	regmap_st = syscon_node_to_regmap(node);
190adbaf525SDaniel Lezcano 	if (IS_ERR(regmap_st)) {
191adbaf525SDaniel Lezcano 		pr_err("Unable to get regmap\n");
192adbaf525SDaniel Lezcano 		return PTR_ERR(regmap_st);
193adbaf525SDaniel Lezcano 	}
194b53cdd03SAlexandre Belloni 
195b53cdd03SAlexandre Belloni 	/* Disable all timer interrupts, and clear any pending ones */
196adf2edfdSAlexandre Belloni 	regmap_write(regmap_st, AT91_ST_IDR,
197b53cdd03SAlexandre Belloni 		AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
198adf2edfdSAlexandre Belloni 	regmap_read(regmap_st, AT91_ST_SR, &val);
199adf2edfdSAlexandre Belloni 
200adf2edfdSAlexandre Belloni 	/* Get the interrupts property */
2010afb46b2SAlexandre Belloni 	irq  = irq_of_parse_and_map(node, 0);
202adbaf525SDaniel Lezcano 	if (!irq) {
203adbaf525SDaniel Lezcano 		pr_err("Unable to get IRQ from DT\n");
204adbaf525SDaniel Lezcano 		return -EINVAL;
205adbaf525SDaniel Lezcano 	}
206b53cdd03SAlexandre Belloni 
207b53cdd03SAlexandre Belloni 	/* Make IRQs happen for the system timer */
2080afb46b2SAlexandre Belloni 	ret = request_irq(irq, at91rm9200_timer_interrupt,
2090afb46b2SAlexandre Belloni 			  IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
2100afb46b2SAlexandre Belloni 			  "at91_tick", regmap_st);
211adbaf525SDaniel Lezcano 	if (ret) {
212adbaf525SDaniel Lezcano 		pr_err("Unable to setup IRQ\n");
213adbaf525SDaniel Lezcano 		return ret;
214adbaf525SDaniel Lezcano 	}
215b53cdd03SAlexandre Belloni 
216216ab8f1SAlexandre Belloni 	sclk = of_clk_get(node, 0);
217adbaf525SDaniel Lezcano 	if (IS_ERR(sclk)) {
218adbaf525SDaniel Lezcano 		pr_err("Unable to get slow clock\n");
219adbaf525SDaniel Lezcano 		return PTR_ERR(sclk);
220adbaf525SDaniel Lezcano 	}
221216ab8f1SAlexandre Belloni 
222adbaf525SDaniel Lezcano 	ret = clk_prepare_enable(sclk);
223adbaf525SDaniel Lezcano 	if (ret) {
224adbaf525SDaniel Lezcano 		pr_err("Could not enable slow clock\n");
225adbaf525SDaniel Lezcano 		return ret;
226adbaf525SDaniel Lezcano 	}
227216ab8f1SAlexandre Belloni 
228216ab8f1SAlexandre Belloni 	sclk_rate = clk_get_rate(sclk);
229adbaf525SDaniel Lezcano 	if (!sclk_rate) {
230adbaf525SDaniel Lezcano 		pr_err("Invalid slow clock rate\n");
231adbaf525SDaniel Lezcano 		return -EINVAL;
232adbaf525SDaniel Lezcano 	}
233216ab8f1SAlexandre Belloni 	timer_latch = (sclk_rate + HZ / 2) / HZ;
234216ab8f1SAlexandre Belloni 
235b53cdd03SAlexandre Belloni 	/* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
236b53cdd03SAlexandre Belloni 	 * directly for the clocksource and all clockevents, after adjusting
237b53cdd03SAlexandre Belloni 	 * its prescaler from the 1 Hz default.
238b53cdd03SAlexandre Belloni 	 */
239adf2edfdSAlexandre Belloni 	regmap_write(regmap_st, AT91_ST_RTMR, 1);
240b53cdd03SAlexandre Belloni 
241b53cdd03SAlexandre Belloni 	/* Setup timer clockevent, with minimum of two ticks (important!!) */
242b53cdd03SAlexandre Belloni 	clkevt.cpumask = cpumask_of(0);
243216ab8f1SAlexandre Belloni 	clockevents_config_and_register(&clkevt, sclk_rate,
244b53cdd03SAlexandre Belloni 					2, AT91_ST_ALMV);
245b53cdd03SAlexandre Belloni 
246b53cdd03SAlexandre Belloni 	/* register clocksource */
247adbaf525SDaniel Lezcano 	return clocksource_register_hz(&clk32k, sclk_rate);
248b53cdd03SAlexandre Belloni }
24917273395SDaniel Lezcano TIMER_OF_DECLARE(atmel_st_timer, "atmel,at91rm9200-st",
250b53cdd03SAlexandre Belloni 		       atmel_st_timer_init);
251