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