1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c1b40e44SStuart Menefy /*
3c1b40e44SStuart Menefy  * drivers/clocksource/arm_global_timer.c
4c1b40e44SStuart Menefy  *
5c1b40e44SStuart Menefy  * Copyright (C) 2013 STMicroelectronics (R&D) Limited.
6c1b40e44SStuart Menefy  * Author: Stuart Menefy <[email protected]>
7c1b40e44SStuart Menefy  * Author: Srinivas Kandagatla <[email protected]>
8c1b40e44SStuart Menefy  */
9c1b40e44SStuart Menefy 
10c1b40e44SStuart Menefy #include <linux/init.h>
11c1b40e44SStuart Menefy #include <linux/interrupt.h>
12c1b40e44SStuart Menefy #include <linux/clocksource.h>
13c1b40e44SStuart Menefy #include <linux/clockchips.h>
14c1b40e44SStuart Menefy #include <linux/cpu.h>
15c1b40e44SStuart Menefy #include <linux/clk.h>
16bbaa0670SRabin Vincent #include <linux/delay.h>
17c1b40e44SStuart Menefy #include <linux/err.h>
18c1b40e44SStuart Menefy #include <linux/io.h>
19c1b40e44SStuart Menefy #include <linux/of.h>
20c1b40e44SStuart Menefy #include <linux/of_irq.h>
21c1b40e44SStuart Menefy #include <linux/of_address.h>
22c1b40e44SStuart Menefy #include <linux/sched_clock.h>
23c1b40e44SStuart Menefy 
24c1b40e44SStuart Menefy #include <asm/cputype.h>
25c1b40e44SStuart Menefy 
26c1b40e44SStuart Menefy #define GT_COUNTER0	0x00
27c1b40e44SStuart Menefy #define GT_COUNTER1	0x04
28c1b40e44SStuart Menefy 
29c1b40e44SStuart Menefy #define GT_CONTROL	0x08
30c1b40e44SStuart Menefy #define GT_CONTROL_TIMER_ENABLE		BIT(0)  /* this bit is NOT banked */
31c1b40e44SStuart Menefy #define GT_CONTROL_COMP_ENABLE		BIT(1)	/* banked */
32c1b40e44SStuart Menefy #define GT_CONTROL_IRQ_ENABLE		BIT(2)	/* banked */
33c1b40e44SStuart Menefy #define GT_CONTROL_AUTO_INC		BIT(3)	/* banked */
34c1b40e44SStuart Menefy 
35c1b40e44SStuart Menefy #define GT_INT_STATUS	0x0c
36c1b40e44SStuart Menefy #define GT_INT_STATUS_EVENT_FLAG	BIT(0)
37c1b40e44SStuart Menefy 
38c1b40e44SStuart Menefy #define GT_COMP0	0x10
39c1b40e44SStuart Menefy #define GT_COMP1	0x14
40c1b40e44SStuart Menefy #define GT_AUTO_INC	0x18
41c1b40e44SStuart Menefy 
42c1b40e44SStuart Menefy /*
43c1b40e44SStuart Menefy  * We are expecting to be clocked by the ARM peripheral clock.
44c1b40e44SStuart Menefy  *
45c1b40e44SStuart Menefy  * Note: it is assumed we are using a prescaler value of zero, so this is
46c1b40e44SStuart Menefy  * the units for all operations.
47c1b40e44SStuart Menefy  */
48c1b40e44SStuart Menefy static void __iomem *gt_base;
49c1b40e44SStuart Menefy static unsigned long gt_clk_rate;
50c1b40e44SStuart Menefy static int gt_ppi;
51c1b40e44SStuart Menefy static struct clock_event_device __percpu *gt_evt;
52c1b40e44SStuart Menefy 
53c1b40e44SStuart Menefy /*
54c1b40e44SStuart Menefy  * To get the value from the Global Timer Counter register proceed as follows:
55c1b40e44SStuart Menefy  * 1. Read the upper 32-bit timer counter register
56c1b40e44SStuart Menefy  * 2. Read the lower 32-bit timer counter register
57c1b40e44SStuart Menefy  * 3. Read the upper 32-bit timer counter register again. If the value is
58c1b40e44SStuart Menefy  *  different to the 32-bit upper value read previously, go back to step 2.
59c1b40e44SStuart Menefy  *  Otherwise the 64-bit timer counter value is correct.
60c1b40e44SStuart Menefy  */
61d6df3576SJisheng Zhang static u64 notrace _gt_counter_read(void)
62c1b40e44SStuart Menefy {
63c1b40e44SStuart Menefy 	u64 counter;
64c1b40e44SStuart Menefy 	u32 lower;
65c1b40e44SStuart Menefy 	u32 upper, old_upper;
66c1b40e44SStuart Menefy 
67c1b40e44SStuart Menefy 	upper = readl_relaxed(gt_base + GT_COUNTER1);
68c1b40e44SStuart Menefy 	do {
69c1b40e44SStuart Menefy 		old_upper = upper;
70c1b40e44SStuart Menefy 		lower = readl_relaxed(gt_base + GT_COUNTER0);
71c1b40e44SStuart Menefy 		upper = readl_relaxed(gt_base + GT_COUNTER1);
72c1b40e44SStuart Menefy 	} while (upper != old_upper);
73c1b40e44SStuart Menefy 
74c1b40e44SStuart Menefy 	counter = upper;
75c1b40e44SStuart Menefy 	counter <<= 32;
76c1b40e44SStuart Menefy 	counter |= lower;
77c1b40e44SStuart Menefy 	return counter;
78c1b40e44SStuart Menefy }
79c1b40e44SStuart Menefy 
80d6df3576SJisheng Zhang static u64 gt_counter_read(void)
81d6df3576SJisheng Zhang {
82d6df3576SJisheng Zhang 	return _gt_counter_read();
83d6df3576SJisheng Zhang }
84d6df3576SJisheng Zhang 
85c1b40e44SStuart Menefy /**
86c1b40e44SStuart Menefy  * To ensure that updates to comparator value register do not set the
87c1b40e44SStuart Menefy  * Interrupt Status Register proceed as follows:
88c1b40e44SStuart Menefy  * 1. Clear the Comp Enable bit in the Timer Control Register.
89c1b40e44SStuart Menefy  * 2. Write the lower 32-bit Comparator Value Register.
90c1b40e44SStuart Menefy  * 3. Write the upper 32-bit Comparator Value Register.
91c1b40e44SStuart Menefy  * 4. Set the Comp Enable bit and, if necessary, the IRQ enable bit.
92c1b40e44SStuart Menefy  */
93c1b40e44SStuart Menefy static void gt_compare_set(unsigned long delta, int periodic)
94c1b40e44SStuart Menefy {
95c1b40e44SStuart Menefy 	u64 counter = gt_counter_read();
96c1b40e44SStuart Menefy 	unsigned long ctrl;
97c1b40e44SStuart Menefy 
98c1b40e44SStuart Menefy 	counter += delta;
99c1b40e44SStuart Menefy 	ctrl = GT_CONTROL_TIMER_ENABLE;
10008e4b448SJisheng Zhang 	writel_relaxed(ctrl, gt_base + GT_CONTROL);
10108e4b448SJisheng Zhang 	writel_relaxed(lower_32_bits(counter), gt_base + GT_COMP0);
10208e4b448SJisheng Zhang 	writel_relaxed(upper_32_bits(counter), gt_base + GT_COMP1);
103c1b40e44SStuart Menefy 
104c1b40e44SStuart Menefy 	if (periodic) {
10508e4b448SJisheng Zhang 		writel_relaxed(delta, gt_base + GT_AUTO_INC);
106c1b40e44SStuart Menefy 		ctrl |= GT_CONTROL_AUTO_INC;
107c1b40e44SStuart Menefy 	}
108c1b40e44SStuart Menefy 
109c1b40e44SStuart Menefy 	ctrl |= GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE;
11008e4b448SJisheng Zhang 	writel_relaxed(ctrl, gt_base + GT_CONTROL);
111c1b40e44SStuart Menefy }
112c1b40e44SStuart Menefy 
113e511e6c3SViresh Kumar static int gt_clockevent_shutdown(struct clock_event_device *evt)
114c1b40e44SStuart Menefy {
115c1b40e44SStuart Menefy 	unsigned long ctrl;
116c1b40e44SStuart Menefy 
117c1b40e44SStuart Menefy 	ctrl = readl(gt_base + GT_CONTROL);
118e511e6c3SViresh Kumar 	ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE |
119e511e6c3SViresh Kumar 		  GT_CONTROL_AUTO_INC);
120c1b40e44SStuart Menefy 	writel(ctrl, gt_base + GT_CONTROL);
121e511e6c3SViresh Kumar 	return 0;
122c1b40e44SStuart Menefy }
123e511e6c3SViresh Kumar 
124e511e6c3SViresh Kumar static int gt_clockevent_set_periodic(struct clock_event_device *evt)
125e511e6c3SViresh Kumar {
126e511e6c3SViresh Kumar 	gt_compare_set(DIV_ROUND_CLOSEST(gt_clk_rate, HZ), 1);
127e511e6c3SViresh Kumar 	return 0;
128c1b40e44SStuart Menefy }
129c1b40e44SStuart Menefy 
130c1b40e44SStuart Menefy static int gt_clockevent_set_next_event(unsigned long evt,
131c1b40e44SStuart Menefy 					struct clock_event_device *unused)
132c1b40e44SStuart Menefy {
133c1b40e44SStuart Menefy 	gt_compare_set(evt, 0);
134c1b40e44SStuart Menefy 	return 0;
135c1b40e44SStuart Menefy }
136c1b40e44SStuart Menefy 
137c1b40e44SStuart Menefy static irqreturn_t gt_clockevent_interrupt(int irq, void *dev_id)
138c1b40e44SStuart Menefy {
139c1b40e44SStuart Menefy 	struct clock_event_device *evt = dev_id;
140c1b40e44SStuart Menefy 
141c1b40e44SStuart Menefy 	if (!(readl_relaxed(gt_base + GT_INT_STATUS) &
142c1b40e44SStuart Menefy 				GT_INT_STATUS_EVENT_FLAG))
143c1b40e44SStuart Menefy 		return IRQ_NONE;
144c1b40e44SStuart Menefy 
145c1b40e44SStuart Menefy 	/**
146c1b40e44SStuart Menefy 	 * ERRATA 740657( Global Timer can send 2 interrupts for
147c1b40e44SStuart Menefy 	 * the same event in single-shot mode)
148c1b40e44SStuart Menefy 	 * Workaround:
149c1b40e44SStuart Menefy 	 *	Either disable single-shot mode.
150c1b40e44SStuart Menefy 	 *	Or
151c1b40e44SStuart Menefy 	 *	Modify the Interrupt Handler to avoid the
152c1b40e44SStuart Menefy 	 *	offending sequence. This is achieved by clearing
153c1b40e44SStuart Menefy 	 *	the Global Timer flag _after_ having incremented
154c1b40e44SStuart Menefy 	 *	the Comparator register	value to a higher value.
155c1b40e44SStuart Menefy 	 */
156e511e6c3SViresh Kumar 	if (clockevent_state_oneshot(evt))
157c1b40e44SStuart Menefy 		gt_compare_set(ULONG_MAX, 0);
158c1b40e44SStuart Menefy 
159c1b40e44SStuart Menefy 	writel_relaxed(GT_INT_STATUS_EVENT_FLAG, gt_base + GT_INT_STATUS);
160c1b40e44SStuart Menefy 	evt->event_handler(evt);
161c1b40e44SStuart Menefy 
162c1b40e44SStuart Menefy 	return IRQ_HANDLED;
163c1b40e44SStuart Menefy }
164c1b40e44SStuart Menefy 
165b8a12296SRichard Cochran static int gt_starting_cpu(unsigned int cpu)
166c1b40e44SStuart Menefy {
167b8a12296SRichard Cochran 	struct clock_event_device *clk = this_cpu_ptr(gt_evt);
168c1b40e44SStuart Menefy 
169c1b40e44SStuart Menefy 	clk->name = "arm_global_timer";
1706661039dSSoren Brinkmann 	clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
1716661039dSSoren Brinkmann 		CLOCK_EVT_FEAT_PERCPU;
172e511e6c3SViresh Kumar 	clk->set_state_shutdown = gt_clockevent_shutdown;
173e511e6c3SViresh Kumar 	clk->set_state_periodic = gt_clockevent_set_periodic;
174e511e6c3SViresh Kumar 	clk->set_state_oneshot = gt_clockevent_shutdown;
1753effa3ceSViresh Kumar 	clk->set_state_oneshot_stopped = gt_clockevent_shutdown;
176c1b40e44SStuart Menefy 	clk->set_next_event = gt_clockevent_set_next_event;
177c1b40e44SStuart Menefy 	clk->cpumask = cpumask_of(cpu);
178c1b40e44SStuart Menefy 	clk->rating = 300;
179c1b40e44SStuart Menefy 	clk->irq = gt_ppi;
180c1b40e44SStuart Menefy 	clockevents_config_and_register(clk, gt_clk_rate,
181c1b40e44SStuart Menefy 					1, 0xffffffff);
182c1b40e44SStuart Menefy 	enable_percpu_irq(clk->irq, IRQ_TYPE_NONE);
183c1b40e44SStuart Menefy 	return 0;
184c1b40e44SStuart Menefy }
185c1b40e44SStuart Menefy 
186b8a12296SRichard Cochran static int gt_dying_cpu(unsigned int cpu)
187c1b40e44SStuart Menefy {
188b8a12296SRichard Cochran 	struct clock_event_device *clk = this_cpu_ptr(gt_evt);
189b8a12296SRichard Cochran 
190e511e6c3SViresh Kumar 	gt_clockevent_shutdown(clk);
191c1b40e44SStuart Menefy 	disable_percpu_irq(clk->irq);
192b8a12296SRichard Cochran 	return 0;
193c1b40e44SStuart Menefy }
194c1b40e44SStuart Menefy 
195a5a1d1c2SThomas Gleixner static u64 gt_clocksource_read(struct clocksource *cs)
196c1b40e44SStuart Menefy {
197c1b40e44SStuart Menefy 	return gt_counter_read();
198c1b40e44SStuart Menefy }
199c1b40e44SStuart Menefy 
2009c9ae5ffSGrygorii Strashko static void gt_resume(struct clocksource *cs)
2019c9ae5ffSGrygorii Strashko {
2029c9ae5ffSGrygorii Strashko 	unsigned long ctrl;
2039c9ae5ffSGrygorii Strashko 
2049c9ae5ffSGrygorii Strashko 	ctrl = readl(gt_base + GT_CONTROL);
2059c9ae5ffSGrygorii Strashko 	if (!(ctrl & GT_CONTROL_TIMER_ENABLE))
2069c9ae5ffSGrygorii Strashko 		/* re-enable timer on resume */
2079c9ae5ffSGrygorii Strashko 		writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
2089c9ae5ffSGrygorii Strashko }
2099c9ae5ffSGrygorii Strashko 
210c1b40e44SStuart Menefy static struct clocksource gt_clocksource = {
211c1b40e44SStuart Menefy 	.name	= "arm_global_timer",
212c1b40e44SStuart Menefy 	.rating	= 300,
213c1b40e44SStuart Menefy 	.read	= gt_clocksource_read,
214c1b40e44SStuart Menefy 	.mask	= CLOCKSOURCE_MASK(64),
215c1b40e44SStuart Menefy 	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
2169c9ae5ffSGrygorii Strashko 	.resume = gt_resume,
217c1b40e44SStuart Menefy };
218c1b40e44SStuart Menefy 
219c1b40e44SStuart Menefy #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
220af066fceSStephen Boyd static u64 notrace gt_sched_clock_read(void)
221c1b40e44SStuart Menefy {
222d6df3576SJisheng Zhang 	return _gt_counter_read();
223c1b40e44SStuart Menefy }
224c1b40e44SStuart Menefy #endif
225c1b40e44SStuart Menefy 
226bbaa0670SRabin Vincent static unsigned long gt_read_long(void)
227bbaa0670SRabin Vincent {
228bbaa0670SRabin Vincent 	return readl_relaxed(gt_base + GT_COUNTER0);
229bbaa0670SRabin Vincent }
230bbaa0670SRabin Vincent 
231bbaa0670SRabin Vincent static struct delay_timer gt_delay_timer = {
232bbaa0670SRabin Vincent 	.read_current_timer = gt_read_long,
233bbaa0670SRabin Vincent };
234bbaa0670SRabin Vincent 
235bbaa0670SRabin Vincent static void __init gt_delay_timer_init(void)
236bbaa0670SRabin Vincent {
237bbaa0670SRabin Vincent 	gt_delay_timer.freq = gt_clk_rate;
238bbaa0670SRabin Vincent 	register_current_timer_delay(&gt_delay_timer);
239bbaa0670SRabin Vincent }
240bbaa0670SRabin Vincent 
2415a54c187SDaniel Lezcano static int __init gt_clocksource_init(void)
242c1b40e44SStuart Menefy {
243c1b40e44SStuart Menefy 	writel(0, gt_base + GT_CONTROL);
244c1b40e44SStuart Menefy 	writel(0, gt_base + GT_COUNTER0);
245c1b40e44SStuart Menefy 	writel(0, gt_base + GT_COUNTER1);
246c1b40e44SStuart Menefy 	/* enables timer on all the cores */
247c1b40e44SStuart Menefy 	writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
248c1b40e44SStuart Menefy 
249c1b40e44SStuart Menefy #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
250af066fceSStephen Boyd 	sched_clock_register(gt_sched_clock_read, 64, gt_clk_rate);
251c1b40e44SStuart Menefy #endif
2525a54c187SDaniel Lezcano 	return clocksource_register_hz(&gt_clocksource, gt_clk_rate);
253c1b40e44SStuart Menefy }
254c1b40e44SStuart Menefy 
2555a54c187SDaniel Lezcano static int __init global_timer_of_register(struct device_node *np)
256c1b40e44SStuart Menefy {
257c1b40e44SStuart Menefy 	struct clk *gt_clk;
258c1b40e44SStuart Menefy 	int err = 0;
259c1b40e44SStuart Menefy 
260c1b40e44SStuart Menefy 	/*
2612cf2ff9fSMatthew Leach 	 * In A9 r2p0 the comparators for each processor with the global timer
262c1b40e44SStuart Menefy 	 * fire when the timer value is greater than or equal to. In previous
263c1b40e44SStuart Menefy 	 * revisions the comparators fired when the timer value was equal to.
264c1b40e44SStuart Menefy 	 */
265af040ffcSRussell King 	if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9
2662cf2ff9fSMatthew Leach 	    && (read_cpuid_id() & 0xf0000f) < 0x200000) {
267c1b40e44SStuart Menefy 		pr_warn("global-timer: non support for this cpu version.\n");
2685a54c187SDaniel Lezcano 		return -ENOSYS;
269c1b40e44SStuart Menefy 	}
270c1b40e44SStuart Menefy 
271c1b40e44SStuart Menefy 	gt_ppi = irq_of_parse_and_map(np, 0);
272c1b40e44SStuart Menefy 	if (!gt_ppi) {
273c1b40e44SStuart Menefy 		pr_warn("global-timer: unable to parse irq\n");
2745a54c187SDaniel Lezcano 		return -EINVAL;
275c1b40e44SStuart Menefy 	}
276c1b40e44SStuart Menefy 
277c1b40e44SStuart Menefy 	gt_base = of_iomap(np, 0);
278c1b40e44SStuart Menefy 	if (!gt_base) {
279c1b40e44SStuart Menefy 		pr_warn("global-timer: invalid base address\n");
2805a54c187SDaniel Lezcano 		return -ENXIO;
281c1b40e44SStuart Menefy 	}
282c1b40e44SStuart Menefy 
283c1b40e44SStuart Menefy 	gt_clk = of_clk_get(np, 0);
284c1b40e44SStuart Menefy 	if (!IS_ERR(gt_clk)) {
285c1b40e44SStuart Menefy 		err = clk_prepare_enable(gt_clk);
286c1b40e44SStuart Menefy 		if (err)
287c1b40e44SStuart Menefy 			goto out_unmap;
288c1b40e44SStuart Menefy 	} else {
289c1b40e44SStuart Menefy 		pr_warn("global-timer: clk not found\n");
290c1b40e44SStuart Menefy 		err = -EINVAL;
291c1b40e44SStuart Menefy 		goto out_unmap;
292c1b40e44SStuart Menefy 	}
293c1b40e44SStuart Menefy 
294c1b40e44SStuart Menefy 	gt_clk_rate = clk_get_rate(gt_clk);
295c1b40e44SStuart Menefy 	gt_evt = alloc_percpu(struct clock_event_device);
296c1b40e44SStuart Menefy 	if (!gt_evt) {
297c1b40e44SStuart Menefy 		pr_warn("global-timer: can't allocate memory\n");
298c1b40e44SStuart Menefy 		err = -ENOMEM;
299c1b40e44SStuart Menefy 		goto out_clk;
300c1b40e44SStuart Menefy 	}
301c1b40e44SStuart Menefy 
302c1b40e44SStuart Menefy 	err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt,
303c1b40e44SStuart Menefy 				 "gt", gt_evt);
304c1b40e44SStuart Menefy 	if (err) {
305c1b40e44SStuart Menefy 		pr_warn("global-timer: can't register interrupt %d (%d)\n",
306c1b40e44SStuart Menefy 			gt_ppi, err);
307c1b40e44SStuart Menefy 		goto out_free;
308c1b40e44SStuart Menefy 	}
309c1b40e44SStuart Menefy 
310b8a12296SRichard Cochran 	/* Register and immediately configure the timer on the boot CPU */
3115a54c187SDaniel Lezcano 	err = gt_clocksource_init();
3125a54c187SDaniel Lezcano 	if (err)
3135a54c187SDaniel Lezcano 		goto out_irq;
3145a54c187SDaniel Lezcano 
315b8a12296SRichard Cochran 	err = cpuhp_setup_state(CPUHP_AP_ARM_GLOBAL_TIMER_STARTING,
31673c1b41eSThomas Gleixner 				"clockevents/arm/global_timer:starting",
317b8a12296SRichard Cochran 				gt_starting_cpu, gt_dying_cpu);
3185a54c187SDaniel Lezcano 	if (err)
3195a54c187SDaniel Lezcano 		goto out_irq;
3205a54c187SDaniel Lezcano 
321bbaa0670SRabin Vincent 	gt_delay_timer_init();
322c1b40e44SStuart Menefy 
3235a54c187SDaniel Lezcano 	return 0;
324c1b40e44SStuart Menefy 
325c1b40e44SStuart Menefy out_irq:
326c1b40e44SStuart Menefy 	free_percpu_irq(gt_ppi, gt_evt);
327c1b40e44SStuart Menefy out_free:
328c1b40e44SStuart Menefy 	free_percpu(gt_evt);
329c1b40e44SStuart Menefy out_clk:
330c1b40e44SStuart Menefy 	clk_disable_unprepare(gt_clk);
331c1b40e44SStuart Menefy out_unmap:
332c1b40e44SStuart Menefy 	iounmap(gt_base);
333c1b40e44SStuart Menefy 	WARN(err, "ARM Global timer register failed (%d)\n", err);
3345a54c187SDaniel Lezcano 
3355a54c187SDaniel Lezcano 	return err;
336c1b40e44SStuart Menefy }
337c1b40e44SStuart Menefy 
338c1b40e44SStuart Menefy /* Only tested on r2p2 and r3p0  */
33917273395SDaniel Lezcano TIMER_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer",
340c1b40e44SStuart Menefy 			global_timer_of_register);
341