1d2912cb1SThomas 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>
12755350bcSMartin Blumenstingl #include <linux/bitfield.h>
13c1b40e44SStuart Menefy #include <linux/clocksource.h>
14c1b40e44SStuart Menefy #include <linux/clockchips.h>
15c1b40e44SStuart Menefy #include <linux/cpu.h>
16c1b40e44SStuart Menefy #include <linux/clk.h>
17bbaa0670SRabin Vincent #include <linux/delay.h>
18c1b40e44SStuart Menefy #include <linux/err.h>
19c1b40e44SStuart Menefy #include <linux/io.h>
20c1b40e44SStuart Menefy #include <linux/of.h>
21c1b40e44SStuart Menefy #include <linux/of_irq.h>
22c1b40e44SStuart Menefy #include <linux/of_address.h>
23c1b40e44SStuart Menefy #include <linux/sched_clock.h>
24c1b40e44SStuart Menefy 
25c1b40e44SStuart Menefy #include <asm/cputype.h>
26c1b40e44SStuart Menefy 
27c1b40e44SStuart Menefy #define GT_COUNTER0	0x00
28c1b40e44SStuart Menefy #define GT_COUNTER1	0x04
29c1b40e44SStuart Menefy 
30c1b40e44SStuart Menefy #define GT_CONTROL	0x08
31c1b40e44SStuart Menefy #define GT_CONTROL_TIMER_ENABLE		BIT(0)  /* this bit is NOT banked */
32c1b40e44SStuart Menefy #define GT_CONTROL_COMP_ENABLE		BIT(1)	/* banked */
33c1b40e44SStuart Menefy #define GT_CONTROL_IRQ_ENABLE		BIT(2)	/* banked */
34c1b40e44SStuart Menefy #define GT_CONTROL_AUTO_INC		BIT(3)	/* banked */
35755350bcSMartin Blumenstingl #define GT_CONTROL_PRESCALER_MASK	GENMASK(15, 8)
36c1b40e44SStuart Menefy 
37c1b40e44SStuart Menefy #define GT_INT_STATUS	0x0c
38c1b40e44SStuart Menefy #define GT_INT_STATUS_EVENT_FLAG	BIT(0)
39c1b40e44SStuart Menefy 
40c1b40e44SStuart Menefy #define GT_COMP0	0x10
41c1b40e44SStuart Menefy #define GT_COMP1	0x14
42c1b40e44SStuart Menefy #define GT_AUTO_INC	0x18
43c1b40e44SStuart Menefy 
44171b45a4SAndrea Merello #define MAX_F_ERR 50
45c1b40e44SStuart Menefy /*
46c1b40e44SStuart Menefy  * We are expecting to be clocked by the ARM peripheral clock.
47c1b40e44SStuart Menefy  *
48c1b40e44SStuart Menefy  * Note: it is assumed we are using a prescaler value of zero, so this is
49c1b40e44SStuart Menefy  * the units for all operations.
50c1b40e44SStuart Menefy  */
51c1b40e44SStuart Menefy static void __iomem *gt_base;
52be534f8eSZou Wei static struct notifier_block gt_clk_rate_change_nb;
53f31c2048SMartin Blumenstingl static u32 gt_psv_new, gt_psv_bck;
54f31c2048SMartin Blumenstingl static unsigned long gt_target_rate;
55c1b40e44SStuart Menefy static int gt_ppi;
56c1b40e44SStuart Menefy static struct clock_event_device __percpu *gt_evt;
57c1b40e44SStuart Menefy 
58c1b40e44SStuart Menefy /*
59c1b40e44SStuart Menefy  * To get the value from the Global Timer Counter register proceed as follows:
60c1b40e44SStuart Menefy  * 1. Read the upper 32-bit timer counter register
61c1b40e44SStuart Menefy  * 2. Read the lower 32-bit timer counter register
62c1b40e44SStuart Menefy  * 3. Read the upper 32-bit timer counter register again. If the value is
63c1b40e44SStuart Menefy  *  different to the 32-bit upper value read previously, go back to step 2.
64c1b40e44SStuart Menefy  *  Otherwise the 64-bit timer counter value is correct.
65c1b40e44SStuart Menefy  */
_gt_counter_read(void)66d6df3576SJisheng Zhang static u64 notrace _gt_counter_read(void)
67c1b40e44SStuart Menefy {
68c1b40e44SStuart Menefy 	u64 counter;
69c1b40e44SStuart Menefy 	u32 lower;
70c1b40e44SStuart Menefy 	u32 upper, old_upper;
71c1b40e44SStuart Menefy 
72c1b40e44SStuart Menefy 	upper = readl_relaxed(gt_base + GT_COUNTER1);
73c1b40e44SStuart Menefy 	do {
74c1b40e44SStuart Menefy 		old_upper = upper;
75c1b40e44SStuart Menefy 		lower = readl_relaxed(gt_base + GT_COUNTER0);
76c1b40e44SStuart Menefy 		upper = readl_relaxed(gt_base + GT_COUNTER1);
77c1b40e44SStuart Menefy 	} while (upper != old_upper);
78c1b40e44SStuart Menefy 
79c1b40e44SStuart Menefy 	counter = upper;
80c1b40e44SStuart Menefy 	counter <<= 32;
81c1b40e44SStuart Menefy 	counter |= lower;
82c1b40e44SStuart Menefy 	return counter;
83c1b40e44SStuart Menefy }
84c1b40e44SStuart Menefy 
gt_counter_read(void)85d6df3576SJisheng Zhang static u64 gt_counter_read(void)
86d6df3576SJisheng Zhang {
87d6df3576SJisheng Zhang 	return _gt_counter_read();
88d6df3576SJisheng Zhang }
89d6df3576SJisheng Zhang 
9097454a65SRandy Dunlap /*
91c1b40e44SStuart Menefy  * To ensure that updates to comparator value register do not set the
92c1b40e44SStuart Menefy  * Interrupt Status Register proceed as follows:
93c1b40e44SStuart Menefy  * 1. Clear the Comp Enable bit in the Timer Control Register.
94c1b40e44SStuart Menefy  * 2. Write the lower 32-bit Comparator Value Register.
95c1b40e44SStuart Menefy  * 3. Write the upper 32-bit Comparator Value Register.
96c1b40e44SStuart Menefy  * 4. Set the Comp Enable bit and, if necessary, the IRQ enable bit.
97c1b40e44SStuart Menefy  */
gt_compare_set(unsigned long delta,int periodic)98c1b40e44SStuart Menefy static void gt_compare_set(unsigned long delta, int periodic)
99c1b40e44SStuart Menefy {
100c1b40e44SStuart Menefy 	u64 counter = gt_counter_read();
101c1b40e44SStuart Menefy 	unsigned long ctrl;
102c1b40e44SStuart Menefy 
103c1b40e44SStuart Menefy 	counter += delta;
104171b45a4SAndrea Merello 	ctrl = readl(gt_base + GT_CONTROL);
105171b45a4SAndrea Merello 	ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE |
106f94bc266SWan Jiabing 		  GT_CONTROL_AUTO_INC);
107171b45a4SAndrea Merello 	ctrl |= GT_CONTROL_TIMER_ENABLE;
10808e4b448SJisheng Zhang 	writel_relaxed(ctrl, gt_base + GT_CONTROL);
10908e4b448SJisheng Zhang 	writel_relaxed(lower_32_bits(counter), gt_base + GT_COMP0);
11008e4b448SJisheng Zhang 	writel_relaxed(upper_32_bits(counter), gt_base + GT_COMP1);
111c1b40e44SStuart Menefy 
112c1b40e44SStuart Menefy 	if (periodic) {
11308e4b448SJisheng Zhang 		writel_relaxed(delta, gt_base + GT_AUTO_INC);
114c1b40e44SStuart Menefy 		ctrl |= GT_CONTROL_AUTO_INC;
115c1b40e44SStuart Menefy 	}
116c1b40e44SStuart Menefy 
117c1b40e44SStuart Menefy 	ctrl |= GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE;
11808e4b448SJisheng Zhang 	writel_relaxed(ctrl, gt_base + GT_CONTROL);
119c1b40e44SStuart Menefy }
120c1b40e44SStuart Menefy 
gt_clockevent_shutdown(struct clock_event_device * evt)121e511e6c3SViresh Kumar static int gt_clockevent_shutdown(struct clock_event_device *evt)
122c1b40e44SStuart Menefy {
123c1b40e44SStuart Menefy 	unsigned long ctrl;
124c1b40e44SStuart Menefy 
125c1b40e44SStuart Menefy 	ctrl = readl(gt_base + GT_CONTROL);
126e511e6c3SViresh Kumar 	ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE |
127e511e6c3SViresh Kumar 		  GT_CONTROL_AUTO_INC);
128c1b40e44SStuart Menefy 	writel(ctrl, gt_base + GT_CONTROL);
129e511e6c3SViresh Kumar 	return 0;
130c1b40e44SStuart Menefy }
131e511e6c3SViresh Kumar 
gt_clockevent_set_periodic(struct clock_event_device * evt)132e511e6c3SViresh Kumar static int gt_clockevent_set_periodic(struct clock_event_device *evt)
133e511e6c3SViresh Kumar {
134171b45a4SAndrea Merello 	gt_compare_set(DIV_ROUND_CLOSEST(gt_target_rate, HZ), 1);
135e511e6c3SViresh Kumar 	return 0;
136c1b40e44SStuart Menefy }
137c1b40e44SStuart Menefy 
gt_clockevent_set_next_event(unsigned long evt,struct clock_event_device * unused)138c1b40e44SStuart Menefy static int gt_clockevent_set_next_event(unsigned long evt,
139c1b40e44SStuart Menefy 					struct clock_event_device *unused)
140c1b40e44SStuart Menefy {
141c1b40e44SStuart Menefy 	gt_compare_set(evt, 0);
142c1b40e44SStuart Menefy 	return 0;
143c1b40e44SStuart Menefy }
144c1b40e44SStuart Menefy 
gt_clockevent_interrupt(int irq,void * dev_id)145c1b40e44SStuart Menefy static irqreturn_t gt_clockevent_interrupt(int irq, void *dev_id)
146c1b40e44SStuart Menefy {
147c1b40e44SStuart Menefy 	struct clock_event_device *evt = dev_id;
148c1b40e44SStuart Menefy 
149c1b40e44SStuart Menefy 	if (!(readl_relaxed(gt_base + GT_INT_STATUS) &
150c1b40e44SStuart Menefy 				GT_INT_STATUS_EVENT_FLAG))
151c1b40e44SStuart Menefy 		return IRQ_NONE;
152c1b40e44SStuart Menefy 
153c1b40e44SStuart Menefy 	/**
154c1b40e44SStuart Menefy 	 * ERRATA 740657( Global Timer can send 2 interrupts for
155c1b40e44SStuart Menefy 	 * the same event in single-shot mode)
156c1b40e44SStuart Menefy 	 * Workaround:
157c1b40e44SStuart Menefy 	 *	Either disable single-shot mode.
158c1b40e44SStuart Menefy 	 *	Or
159c1b40e44SStuart Menefy 	 *	Modify the Interrupt Handler to avoid the
160c1b40e44SStuart Menefy 	 *	offending sequence. This is achieved by clearing
161c1b40e44SStuart Menefy 	 *	the Global Timer flag _after_ having incremented
162c1b40e44SStuart Menefy 	 *	the Comparator register	value to a higher value.
163c1b40e44SStuart Menefy 	 */
164e511e6c3SViresh Kumar 	if (clockevent_state_oneshot(evt))
165c1b40e44SStuart Menefy 		gt_compare_set(ULONG_MAX, 0);
166c1b40e44SStuart Menefy 
167c1b40e44SStuart Menefy 	writel_relaxed(GT_INT_STATUS_EVENT_FLAG, gt_base + GT_INT_STATUS);
168c1b40e44SStuart Menefy 	evt->event_handler(evt);
169c1b40e44SStuart Menefy 
170c1b40e44SStuart Menefy 	return IRQ_HANDLED;
171c1b40e44SStuart Menefy }
172c1b40e44SStuart Menefy 
gt_starting_cpu(unsigned int cpu)173b8a12296SRichard Cochran static int gt_starting_cpu(unsigned int cpu)
174c1b40e44SStuart Menefy {
175b8a12296SRichard Cochran 	struct clock_event_device *clk = this_cpu_ptr(gt_evt);
176c1b40e44SStuart Menefy 
177c1b40e44SStuart Menefy 	clk->name = "arm_global_timer";
1786661039dSSoren Brinkmann 	clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
1796661039dSSoren Brinkmann 		CLOCK_EVT_FEAT_PERCPU;
180e511e6c3SViresh Kumar 	clk->set_state_shutdown = gt_clockevent_shutdown;
181e511e6c3SViresh Kumar 	clk->set_state_periodic = gt_clockevent_set_periodic;
182e511e6c3SViresh Kumar 	clk->set_state_oneshot = gt_clockevent_shutdown;
1833effa3ceSViresh Kumar 	clk->set_state_oneshot_stopped = gt_clockevent_shutdown;
184c1b40e44SStuart Menefy 	clk->set_next_event = gt_clockevent_set_next_event;
185c1b40e44SStuart Menefy 	clk->cpumask = cpumask_of(cpu);
186c1b40e44SStuart Menefy 	clk->rating = 300;
187c1b40e44SStuart Menefy 	clk->irq = gt_ppi;
188171b45a4SAndrea Merello 	clockevents_config_and_register(clk, gt_target_rate,
189c1b40e44SStuart Menefy 					1, 0xffffffff);
190c1b40e44SStuart Menefy 	enable_percpu_irq(clk->irq, IRQ_TYPE_NONE);
191c1b40e44SStuart Menefy 	return 0;
192c1b40e44SStuart Menefy }
193c1b40e44SStuart Menefy 
gt_dying_cpu(unsigned int cpu)194b8a12296SRichard Cochran static int gt_dying_cpu(unsigned int cpu)
195c1b40e44SStuart Menefy {
196b8a12296SRichard Cochran 	struct clock_event_device *clk = this_cpu_ptr(gt_evt);
197b8a12296SRichard Cochran 
198c1b40e44SStuart Menefy 	disable_percpu_irq(clk->irq);
199b8a12296SRichard Cochran 	return 0;
200c1b40e44SStuart Menefy }
201c1b40e44SStuart Menefy 
gt_clocksource_read(struct clocksource * cs)202a5a1d1c2SThomas Gleixner static u64 gt_clocksource_read(struct clocksource *cs)
203c1b40e44SStuart Menefy {
204c1b40e44SStuart Menefy 	return gt_counter_read();
205c1b40e44SStuart Menefy }
206c1b40e44SStuart Menefy 
gt_resume(struct clocksource * cs)2079c9ae5ffSGrygorii Strashko static void gt_resume(struct clocksource *cs)
2089c9ae5ffSGrygorii Strashko {
2099c9ae5ffSGrygorii Strashko 	unsigned long ctrl;
2109c9ae5ffSGrygorii Strashko 
2119c9ae5ffSGrygorii Strashko 	ctrl = readl(gt_base + GT_CONTROL);
2129c9ae5ffSGrygorii Strashko 	if (!(ctrl & GT_CONTROL_TIMER_ENABLE))
2139c9ae5ffSGrygorii Strashko 		/* re-enable timer on resume */
2149c9ae5ffSGrygorii Strashko 		writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
2159c9ae5ffSGrygorii Strashko }
2169c9ae5ffSGrygorii Strashko 
217c1b40e44SStuart Menefy static struct clocksource gt_clocksource = {
218c1b40e44SStuart Menefy 	.name	= "arm_global_timer",
219c1b40e44SStuart Menefy 	.rating	= 300,
220c1b40e44SStuart Menefy 	.read	= gt_clocksource_read,
221c1b40e44SStuart Menefy 	.mask	= CLOCKSOURCE_MASK(64),
222c1b40e44SStuart Menefy 	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
2239c9ae5ffSGrygorii Strashko 	.resume = gt_resume,
224c1b40e44SStuart Menefy };
225c1b40e44SStuart Menefy 
226c1b40e44SStuart Menefy #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
gt_sched_clock_read(void)227af066fceSStephen Boyd static u64 notrace gt_sched_clock_read(void)
228c1b40e44SStuart Menefy {
229d6df3576SJisheng Zhang 	return _gt_counter_read();
230c1b40e44SStuart Menefy }
231c1b40e44SStuart Menefy #endif
232c1b40e44SStuart Menefy 
gt_read_long(void)233bbaa0670SRabin Vincent static unsigned long gt_read_long(void)
234bbaa0670SRabin Vincent {
235bbaa0670SRabin Vincent 	return readl_relaxed(gt_base + GT_COUNTER0);
236bbaa0670SRabin Vincent }
237bbaa0670SRabin Vincent 
238bbaa0670SRabin Vincent static struct delay_timer gt_delay_timer = {
239bbaa0670SRabin Vincent 	.read_current_timer = gt_read_long,
240bbaa0670SRabin Vincent };
241bbaa0670SRabin Vincent 
gt_write_presc(u32 psv)242171b45a4SAndrea Merello static void gt_write_presc(u32 psv)
243171b45a4SAndrea Merello {
244171b45a4SAndrea Merello 	u32 reg;
245171b45a4SAndrea Merello 
246171b45a4SAndrea Merello 	reg = readl(gt_base + GT_CONTROL);
247171b45a4SAndrea Merello 	reg &= ~GT_CONTROL_PRESCALER_MASK;
248755350bcSMartin Blumenstingl 	reg |= FIELD_PREP(GT_CONTROL_PRESCALER_MASK, psv);
249171b45a4SAndrea Merello 	writel(reg, gt_base + GT_CONTROL);
250171b45a4SAndrea Merello }
251171b45a4SAndrea Merello 
gt_read_presc(void)252171b45a4SAndrea Merello static u32 gt_read_presc(void)
253171b45a4SAndrea Merello {
254171b45a4SAndrea Merello 	u32 reg;
255171b45a4SAndrea Merello 
256171b45a4SAndrea Merello 	reg = readl(gt_base + GT_CONTROL);
257755350bcSMartin Blumenstingl 	return FIELD_GET(GT_CONTROL_PRESCALER_MASK, reg);
258171b45a4SAndrea Merello }
259171b45a4SAndrea Merello 
gt_delay_timer_init(void)260bbaa0670SRabin Vincent static void __init gt_delay_timer_init(void)
261bbaa0670SRabin Vincent {
262171b45a4SAndrea Merello 	gt_delay_timer.freq = gt_target_rate;
263bbaa0670SRabin Vincent 	register_current_timer_delay(&gt_delay_timer);
264bbaa0670SRabin Vincent }
265bbaa0670SRabin Vincent 
gt_clocksource_init(void)2665a54c187SDaniel Lezcano static int __init gt_clocksource_init(void)
267c1b40e44SStuart Menefy {
268c1b40e44SStuart Menefy 	writel(0, gt_base + GT_CONTROL);
269c1b40e44SStuart Menefy 	writel(0, gt_base + GT_COUNTER0);
270c1b40e44SStuart Menefy 	writel(0, gt_base + GT_COUNTER1);
271171b45a4SAndrea Merello 	/* set prescaler and enable timer on all the cores */
272755350bcSMartin Blumenstingl 	writel(FIELD_PREP(GT_CONTROL_PRESCALER_MASK,
273755350bcSMartin Blumenstingl 			  CONFIG_ARM_GT_INITIAL_PRESCALER_VAL - 1) |
274755350bcSMartin Blumenstingl 	       GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
275c1b40e44SStuart Menefy 
276c1b40e44SStuart Menefy #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
277171b45a4SAndrea Merello 	sched_clock_register(gt_sched_clock_read, 64, gt_target_rate);
278c1b40e44SStuart Menefy #endif
279171b45a4SAndrea Merello 	return clocksource_register_hz(&gt_clocksource, gt_target_rate);
280171b45a4SAndrea Merello }
281171b45a4SAndrea Merello 
gt_clk_rate_change_cb(struct notifier_block * nb,unsigned long event,void * data)282171b45a4SAndrea Merello static int gt_clk_rate_change_cb(struct notifier_block *nb,
283171b45a4SAndrea Merello 				 unsigned long event, void *data)
284171b45a4SAndrea Merello {
285171b45a4SAndrea Merello 	struct clk_notifier_data *ndata = data;
286171b45a4SAndrea Merello 
287171b45a4SAndrea Merello 	switch (event) {
288171b45a4SAndrea Merello 	case PRE_RATE_CHANGE:
289171b45a4SAndrea Merello 	{
290e651f2faSMartin Blumenstingl 		unsigned long psv;
291171b45a4SAndrea Merello 
292e651f2faSMartin Blumenstingl 		psv = DIV_ROUND_CLOSEST(ndata->new_rate, gt_target_rate);
293e651f2faSMartin Blumenstingl 		if (!psv ||
294e651f2faSMartin Blumenstingl 		    abs(gt_target_rate - (ndata->new_rate / psv)) > MAX_F_ERR)
295171b45a4SAndrea Merello 			return NOTIFY_BAD;
296171b45a4SAndrea Merello 
297171b45a4SAndrea Merello 		psv--;
298171b45a4SAndrea Merello 
299171b45a4SAndrea Merello 		/* prescaler within legal range? */
300755350bcSMartin Blumenstingl 		if (!FIELD_FIT(GT_CONTROL_PRESCALER_MASK, psv))
301171b45a4SAndrea Merello 			return NOTIFY_BAD;
302171b45a4SAndrea Merello 
303171b45a4SAndrea Merello 		/*
304171b45a4SAndrea Merello 		 * store timer clock ctrl register so we can restore it in case
305171b45a4SAndrea Merello 		 * of an abort.
306171b45a4SAndrea Merello 		 */
307171b45a4SAndrea Merello 		gt_psv_bck = gt_read_presc();
308171b45a4SAndrea Merello 		gt_psv_new = psv;
309171b45a4SAndrea Merello 		/* scale down: adjust divider in post-change notification */
310171b45a4SAndrea Merello 		if (ndata->new_rate < ndata->old_rate)
311171b45a4SAndrea Merello 			return NOTIFY_DONE;
312171b45a4SAndrea Merello 
313171b45a4SAndrea Merello 		/* scale up: adjust divider now - before frequency change */
314171b45a4SAndrea Merello 		gt_write_presc(psv);
315171b45a4SAndrea Merello 		break;
316171b45a4SAndrea Merello 	}
317171b45a4SAndrea Merello 	case POST_RATE_CHANGE:
318171b45a4SAndrea Merello 		/* scale up: pre-change notification did the adjustment */
319171b45a4SAndrea Merello 		if (ndata->new_rate > ndata->old_rate)
320171b45a4SAndrea Merello 			return NOTIFY_OK;
321171b45a4SAndrea Merello 
322171b45a4SAndrea Merello 		/* scale down: adjust divider now - after frequency change */
323171b45a4SAndrea Merello 		gt_write_presc(gt_psv_new);
324171b45a4SAndrea Merello 		break;
325171b45a4SAndrea Merello 
326171b45a4SAndrea Merello 	case ABORT_RATE_CHANGE:
327171b45a4SAndrea Merello 		/* we have to undo the adjustment in case we scale up */
328171b45a4SAndrea Merello 		if (ndata->new_rate < ndata->old_rate)
329171b45a4SAndrea Merello 			return NOTIFY_OK;
330171b45a4SAndrea Merello 
331171b45a4SAndrea Merello 		/* restore original register value */
332171b45a4SAndrea Merello 		gt_write_presc(gt_psv_bck);
333171b45a4SAndrea Merello 		break;
334171b45a4SAndrea Merello 	default:
335171b45a4SAndrea Merello 		return NOTIFY_DONE;
336171b45a4SAndrea Merello 	}
337171b45a4SAndrea Merello 
338171b45a4SAndrea Merello 	return NOTIFY_DONE;
339c1b40e44SStuart Menefy }
340c1b40e44SStuart Menefy 
global_timer_of_register(struct device_node * np)3415a54c187SDaniel Lezcano static int __init global_timer_of_register(struct device_node *np)
342c1b40e44SStuart Menefy {
343c1b40e44SStuart Menefy 	struct clk *gt_clk;
344171b45a4SAndrea Merello 	static unsigned long gt_clk_rate;
345*f3539a6aSLi kunyu 	int err;
346c1b40e44SStuart Menefy 
347c1b40e44SStuart Menefy 	/*
3482cf2ff9fSMatthew Leach 	 * In A9 r2p0 the comparators for each processor with the global timer
349c1b40e44SStuart Menefy 	 * fire when the timer value is greater than or equal to. In previous
350c1b40e44SStuart Menefy 	 * revisions the comparators fired when the timer value was equal to.
351c1b40e44SStuart Menefy 	 */
352af040ffcSRussell King 	if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9
3532cf2ff9fSMatthew Leach 	    && (read_cpuid_id() & 0xf0000f) < 0x200000) {
354c1b40e44SStuart Menefy 		pr_warn("global-timer: non support for this cpu version.\n");
3555a54c187SDaniel Lezcano 		return -ENOSYS;
356c1b40e44SStuart Menefy 	}
357c1b40e44SStuart Menefy 
358c1b40e44SStuart Menefy 	gt_ppi = irq_of_parse_and_map(np, 0);
359c1b40e44SStuart Menefy 	if (!gt_ppi) {
360c1b40e44SStuart Menefy 		pr_warn("global-timer: unable to parse irq\n");
3615a54c187SDaniel Lezcano 		return -EINVAL;
362c1b40e44SStuart Menefy 	}
363c1b40e44SStuart Menefy 
364c1b40e44SStuart Menefy 	gt_base = of_iomap(np, 0);
365c1b40e44SStuart Menefy 	if (!gt_base) {
366c1b40e44SStuart Menefy 		pr_warn("global-timer: invalid base address\n");
3675a54c187SDaniel Lezcano 		return -ENXIO;
368c1b40e44SStuart Menefy 	}
369c1b40e44SStuart Menefy 
370c1b40e44SStuart Menefy 	gt_clk = of_clk_get(np, 0);
371c1b40e44SStuart Menefy 	if (!IS_ERR(gt_clk)) {
372c1b40e44SStuart Menefy 		err = clk_prepare_enable(gt_clk);
373c1b40e44SStuart Menefy 		if (err)
374c1b40e44SStuart Menefy 			goto out_unmap;
375c1b40e44SStuart Menefy 	} else {
376c1b40e44SStuart Menefy 		pr_warn("global-timer: clk not found\n");
377c1b40e44SStuart Menefy 		err = -EINVAL;
378c1b40e44SStuart Menefy 		goto out_unmap;
379c1b40e44SStuart Menefy 	}
380c1b40e44SStuart Menefy 
381c1b40e44SStuart Menefy 	gt_clk_rate = clk_get_rate(gt_clk);
382171b45a4SAndrea Merello 	gt_target_rate = gt_clk_rate / CONFIG_ARM_GT_INITIAL_PRESCALER_VAL;
383171b45a4SAndrea Merello 	gt_clk_rate_change_nb.notifier_call =
384171b45a4SAndrea Merello 		gt_clk_rate_change_cb;
385171b45a4SAndrea Merello 	err = clk_notifier_register(gt_clk, &gt_clk_rate_change_nb);
386171b45a4SAndrea Merello 	if (err) {
387171b45a4SAndrea Merello 		pr_warn("Unable to register clock notifier\n");
388171b45a4SAndrea Merello 		goto out_clk;
389171b45a4SAndrea Merello 	}
390171b45a4SAndrea Merello 
391c1b40e44SStuart Menefy 	gt_evt = alloc_percpu(struct clock_event_device);
392c1b40e44SStuart Menefy 	if (!gt_evt) {
393c1b40e44SStuart Menefy 		pr_warn("global-timer: can't allocate memory\n");
394c1b40e44SStuart Menefy 		err = -ENOMEM;
395171b45a4SAndrea Merello 		goto out_clk_nb;
396c1b40e44SStuart Menefy 	}
397c1b40e44SStuart Menefy 
398c1b40e44SStuart Menefy 	err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt,
399c1b40e44SStuart Menefy 				 "gt", gt_evt);
400c1b40e44SStuart Menefy 	if (err) {
401c1b40e44SStuart Menefy 		pr_warn("global-timer: can't register interrupt %d (%d)\n",
402c1b40e44SStuart Menefy 			gt_ppi, err);
403c1b40e44SStuart Menefy 		goto out_free;
404c1b40e44SStuart Menefy 	}
405c1b40e44SStuart Menefy 
406b8a12296SRichard Cochran 	/* Register and immediately configure the timer on the boot CPU */
4075a54c187SDaniel Lezcano 	err = gt_clocksource_init();
4085a54c187SDaniel Lezcano 	if (err)
4095a54c187SDaniel Lezcano 		goto out_irq;
4105a54c187SDaniel Lezcano 
411b8a12296SRichard Cochran 	err = cpuhp_setup_state(CPUHP_AP_ARM_GLOBAL_TIMER_STARTING,
41273c1b41eSThomas Gleixner 				"clockevents/arm/global_timer:starting",
413b8a12296SRichard Cochran 				gt_starting_cpu, gt_dying_cpu);
4145a54c187SDaniel Lezcano 	if (err)
4155a54c187SDaniel Lezcano 		goto out_irq;
4165a54c187SDaniel Lezcano 
417bbaa0670SRabin Vincent 	gt_delay_timer_init();
418c1b40e44SStuart Menefy 
4195a54c187SDaniel Lezcano 	return 0;
420c1b40e44SStuart Menefy 
421c1b40e44SStuart Menefy out_irq:
422c1b40e44SStuart Menefy 	free_percpu_irq(gt_ppi, gt_evt);
423c1b40e44SStuart Menefy out_free:
424c1b40e44SStuart Menefy 	free_percpu(gt_evt);
425171b45a4SAndrea Merello out_clk_nb:
426171b45a4SAndrea Merello 	clk_notifier_unregister(gt_clk, &gt_clk_rate_change_nb);
427c1b40e44SStuart Menefy out_clk:
428c1b40e44SStuart Menefy 	clk_disable_unprepare(gt_clk);
429c1b40e44SStuart Menefy out_unmap:
430c1b40e44SStuart Menefy 	iounmap(gt_base);
431c1b40e44SStuart Menefy 	WARN(err, "ARM Global timer register failed (%d)\n", err);
4325a54c187SDaniel Lezcano 
4335a54c187SDaniel Lezcano 	return err;
434c1b40e44SStuart Menefy }
435c1b40e44SStuart Menefy 
436c1b40e44SStuart Menefy /* Only tested on r2p2 and r3p0  */
43717273395SDaniel Lezcano TIMER_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer",
438c1b40e44SStuart Menefy 			global_timer_of_register);
439