1c1b40e44SStuart Menefy /*
2c1b40e44SStuart Menefy  * drivers/clocksource/arm_global_timer.c
3c1b40e44SStuart Menefy  *
4c1b40e44SStuart Menefy  * Copyright (C) 2013 STMicroelectronics (R&D) Limited.
5c1b40e44SStuart Menefy  * Author: Stuart Menefy <[email protected]>
6c1b40e44SStuart Menefy  * Author: Srinivas Kandagatla <[email protected]>
7c1b40e44SStuart Menefy  *
8c1b40e44SStuart Menefy  * This program is free software; you can redistribute it and/or modify
9c1b40e44SStuart Menefy  * it under the terms of the GNU General Public License version 2 as
10c1b40e44SStuart Menefy  * published by the Free Software Foundation.
11c1b40e44SStuart Menefy  */
12c1b40e44SStuart Menefy 
13c1b40e44SStuart Menefy #include <linux/init.h>
14c1b40e44SStuart Menefy #include <linux/interrupt.h>
15c1b40e44SStuart Menefy #include <linux/clocksource.h>
16c1b40e44SStuart Menefy #include <linux/clockchips.h>
17c1b40e44SStuart Menefy #include <linux/cpu.h>
18c1b40e44SStuart Menefy #include <linux/clk.h>
19bbaa0670SRabin Vincent #include <linux/delay.h>
20c1b40e44SStuart Menefy #include <linux/err.h>
21c1b40e44SStuart Menefy #include <linux/io.h>
22c1b40e44SStuart Menefy #include <linux/of.h>
23c1b40e44SStuart Menefy #include <linux/of_irq.h>
24c1b40e44SStuart Menefy #include <linux/of_address.h>
25c1b40e44SStuart Menefy #include <linux/sched_clock.h>
26c1b40e44SStuart Menefy 
27c1b40e44SStuart Menefy #include <asm/cputype.h>
28c1b40e44SStuart Menefy 
29c1b40e44SStuart Menefy #define GT_COUNTER0	0x00
30c1b40e44SStuart Menefy #define GT_COUNTER1	0x04
31c1b40e44SStuart Menefy 
32c1b40e44SStuart Menefy #define GT_CONTROL	0x08
33c1b40e44SStuart Menefy #define GT_CONTROL_TIMER_ENABLE		BIT(0)  /* this bit is NOT banked */
34c1b40e44SStuart Menefy #define GT_CONTROL_COMP_ENABLE		BIT(1)	/* banked */
35c1b40e44SStuart Menefy #define GT_CONTROL_IRQ_ENABLE		BIT(2)	/* banked */
36c1b40e44SStuart Menefy #define GT_CONTROL_AUTO_INC		BIT(3)	/* banked */
37c1b40e44SStuart Menefy 
38c1b40e44SStuart Menefy #define GT_INT_STATUS	0x0c
39c1b40e44SStuart Menefy #define GT_INT_STATUS_EVENT_FLAG	BIT(0)
40c1b40e44SStuart Menefy 
41c1b40e44SStuart Menefy #define GT_COMP0	0x10
42c1b40e44SStuart Menefy #define GT_COMP1	0x14
43c1b40e44SStuart Menefy #define GT_AUTO_INC	0x18
44c1b40e44SStuart Menefy 
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;
52c1b40e44SStuart Menefy static unsigned long gt_clk_rate;
53c1b40e44SStuart Menefy static int gt_ppi;
54c1b40e44SStuart Menefy static struct clock_event_device __percpu *gt_evt;
55c1b40e44SStuart Menefy 
56c1b40e44SStuart Menefy /*
57c1b40e44SStuart Menefy  * To get the value from the Global Timer Counter register proceed as follows:
58c1b40e44SStuart Menefy  * 1. Read the upper 32-bit timer counter register
59c1b40e44SStuart Menefy  * 2. Read the lower 32-bit timer counter register
60c1b40e44SStuart Menefy  * 3. Read the upper 32-bit timer counter register again. If the value is
61c1b40e44SStuart Menefy  *  different to the 32-bit upper value read previously, go back to step 2.
62c1b40e44SStuart Menefy  *  Otherwise the 64-bit timer counter value is correct.
63c1b40e44SStuart Menefy  */
64d6df3576SJisheng Zhang static u64 notrace _gt_counter_read(void)
65c1b40e44SStuart Menefy {
66c1b40e44SStuart Menefy 	u64 counter;
67c1b40e44SStuart Menefy 	u32 lower;
68c1b40e44SStuart Menefy 	u32 upper, old_upper;
69c1b40e44SStuart Menefy 
70c1b40e44SStuart Menefy 	upper = readl_relaxed(gt_base + GT_COUNTER1);
71c1b40e44SStuart Menefy 	do {
72c1b40e44SStuart Menefy 		old_upper = upper;
73c1b40e44SStuart Menefy 		lower = readl_relaxed(gt_base + GT_COUNTER0);
74c1b40e44SStuart Menefy 		upper = readl_relaxed(gt_base + GT_COUNTER1);
75c1b40e44SStuart Menefy 	} while (upper != old_upper);
76c1b40e44SStuart Menefy 
77c1b40e44SStuart Menefy 	counter = upper;
78c1b40e44SStuart Menefy 	counter <<= 32;
79c1b40e44SStuart Menefy 	counter |= lower;
80c1b40e44SStuart Menefy 	return counter;
81c1b40e44SStuart Menefy }
82c1b40e44SStuart Menefy 
83d6df3576SJisheng Zhang static u64 gt_counter_read(void)
84d6df3576SJisheng Zhang {
85d6df3576SJisheng Zhang 	return _gt_counter_read();
86d6df3576SJisheng Zhang }
87d6df3576SJisheng Zhang 
88c1b40e44SStuart Menefy /**
89c1b40e44SStuart Menefy  * To ensure that updates to comparator value register do not set the
90c1b40e44SStuart Menefy  * Interrupt Status Register proceed as follows:
91c1b40e44SStuart Menefy  * 1. Clear the Comp Enable bit in the Timer Control Register.
92c1b40e44SStuart Menefy  * 2. Write the lower 32-bit Comparator Value Register.
93c1b40e44SStuart Menefy  * 3. Write the upper 32-bit Comparator Value Register.
94c1b40e44SStuart Menefy  * 4. Set the Comp Enable bit and, if necessary, the IRQ enable bit.
95c1b40e44SStuart Menefy  */
96c1b40e44SStuart Menefy static void gt_compare_set(unsigned long delta, int periodic)
97c1b40e44SStuart Menefy {
98c1b40e44SStuart Menefy 	u64 counter = gt_counter_read();
99c1b40e44SStuart Menefy 	unsigned long ctrl;
100c1b40e44SStuart Menefy 
101c1b40e44SStuart Menefy 	counter += delta;
102c1b40e44SStuart Menefy 	ctrl = GT_CONTROL_TIMER_ENABLE;
10308e4b448SJisheng Zhang 	writel_relaxed(ctrl, gt_base + GT_CONTROL);
10408e4b448SJisheng Zhang 	writel_relaxed(lower_32_bits(counter), gt_base + GT_COMP0);
10508e4b448SJisheng Zhang 	writel_relaxed(upper_32_bits(counter), gt_base + GT_COMP1);
106c1b40e44SStuart Menefy 
107c1b40e44SStuart Menefy 	if (periodic) {
10808e4b448SJisheng Zhang 		writel_relaxed(delta, gt_base + GT_AUTO_INC);
109c1b40e44SStuart Menefy 		ctrl |= GT_CONTROL_AUTO_INC;
110c1b40e44SStuart Menefy 	}
111c1b40e44SStuart Menefy 
112c1b40e44SStuart Menefy 	ctrl |= GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE;
11308e4b448SJisheng Zhang 	writel_relaxed(ctrl, gt_base + GT_CONTROL);
114c1b40e44SStuart Menefy }
115c1b40e44SStuart Menefy 
116e511e6c3SViresh Kumar static int gt_clockevent_shutdown(struct clock_event_device *evt)
117c1b40e44SStuart Menefy {
118c1b40e44SStuart Menefy 	unsigned long ctrl;
119c1b40e44SStuart Menefy 
120c1b40e44SStuart Menefy 	ctrl = readl(gt_base + GT_CONTROL);
121e511e6c3SViresh Kumar 	ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE |
122e511e6c3SViresh Kumar 		  GT_CONTROL_AUTO_INC);
123c1b40e44SStuart Menefy 	writel(ctrl, gt_base + GT_CONTROL);
124e511e6c3SViresh Kumar 	return 0;
125c1b40e44SStuart Menefy }
126e511e6c3SViresh Kumar 
127e511e6c3SViresh Kumar static int gt_clockevent_set_periodic(struct clock_event_device *evt)
128e511e6c3SViresh Kumar {
129e511e6c3SViresh Kumar 	gt_compare_set(DIV_ROUND_CLOSEST(gt_clk_rate, HZ), 1);
130e511e6c3SViresh Kumar 	return 0;
131c1b40e44SStuart Menefy }
132c1b40e44SStuart Menefy 
133c1b40e44SStuart Menefy static int gt_clockevent_set_next_event(unsigned long evt,
134c1b40e44SStuart Menefy 					struct clock_event_device *unused)
135c1b40e44SStuart Menefy {
136c1b40e44SStuart Menefy 	gt_compare_set(evt, 0);
137c1b40e44SStuart Menefy 	return 0;
138c1b40e44SStuart Menefy }
139c1b40e44SStuart Menefy 
140c1b40e44SStuart Menefy static irqreturn_t gt_clockevent_interrupt(int irq, void *dev_id)
141c1b40e44SStuart Menefy {
142c1b40e44SStuart Menefy 	struct clock_event_device *evt = dev_id;
143c1b40e44SStuart Menefy 
144c1b40e44SStuart Menefy 	if (!(readl_relaxed(gt_base + GT_INT_STATUS) &
145c1b40e44SStuart Menefy 				GT_INT_STATUS_EVENT_FLAG))
146c1b40e44SStuart Menefy 		return IRQ_NONE;
147c1b40e44SStuart Menefy 
148c1b40e44SStuart Menefy 	/**
149c1b40e44SStuart Menefy 	 * ERRATA 740657( Global Timer can send 2 interrupts for
150c1b40e44SStuart Menefy 	 * the same event in single-shot mode)
151c1b40e44SStuart Menefy 	 * Workaround:
152c1b40e44SStuart Menefy 	 *	Either disable single-shot mode.
153c1b40e44SStuart Menefy 	 *	Or
154c1b40e44SStuart Menefy 	 *	Modify the Interrupt Handler to avoid the
155c1b40e44SStuart Menefy 	 *	offending sequence. This is achieved by clearing
156c1b40e44SStuart Menefy 	 *	the Global Timer flag _after_ having incremented
157c1b40e44SStuart Menefy 	 *	the Comparator register	value to a higher value.
158c1b40e44SStuart Menefy 	 */
159e511e6c3SViresh Kumar 	if (clockevent_state_oneshot(evt))
160c1b40e44SStuart Menefy 		gt_compare_set(ULONG_MAX, 0);
161c1b40e44SStuart Menefy 
162c1b40e44SStuart Menefy 	writel_relaxed(GT_INT_STATUS_EVENT_FLAG, gt_base + GT_INT_STATUS);
163c1b40e44SStuart Menefy 	evt->event_handler(evt);
164c1b40e44SStuart Menefy 
165c1b40e44SStuart Menefy 	return IRQ_HANDLED;
166c1b40e44SStuart Menefy }
167c1b40e44SStuart Menefy 
168*b8a12296SRichard Cochran static int gt_starting_cpu(unsigned int cpu)
169c1b40e44SStuart Menefy {
170*b8a12296SRichard Cochran 	struct clock_event_device *clk = this_cpu_ptr(gt_evt);
171c1b40e44SStuart Menefy 
172c1b40e44SStuart Menefy 	clk->name = "arm_global_timer";
1736661039dSSoren Brinkmann 	clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
1746661039dSSoren Brinkmann 		CLOCK_EVT_FEAT_PERCPU;
175e511e6c3SViresh Kumar 	clk->set_state_shutdown = gt_clockevent_shutdown;
176e511e6c3SViresh Kumar 	clk->set_state_periodic = gt_clockevent_set_periodic;
177e511e6c3SViresh Kumar 	clk->set_state_oneshot = gt_clockevent_shutdown;
1783effa3ceSViresh Kumar 	clk->set_state_oneshot_stopped = gt_clockevent_shutdown;
179c1b40e44SStuart Menefy 	clk->set_next_event = gt_clockevent_set_next_event;
180c1b40e44SStuart Menefy 	clk->cpumask = cpumask_of(cpu);
181c1b40e44SStuart Menefy 	clk->rating = 300;
182c1b40e44SStuart Menefy 	clk->irq = gt_ppi;
183c1b40e44SStuart Menefy 	clockevents_config_and_register(clk, gt_clk_rate,
184c1b40e44SStuart Menefy 					1, 0xffffffff);
185c1b40e44SStuart Menefy 	enable_percpu_irq(clk->irq, IRQ_TYPE_NONE);
186c1b40e44SStuart Menefy 	return 0;
187c1b40e44SStuart Menefy }
188c1b40e44SStuart Menefy 
189*b8a12296SRichard Cochran static int gt_dying_cpu(unsigned int cpu)
190c1b40e44SStuart Menefy {
191*b8a12296SRichard Cochran 	struct clock_event_device *clk = this_cpu_ptr(gt_evt);
192*b8a12296SRichard Cochran 
193e511e6c3SViresh Kumar 	gt_clockevent_shutdown(clk);
194c1b40e44SStuart Menefy 	disable_percpu_irq(clk->irq);
195*b8a12296SRichard Cochran 	return 0;
196c1b40e44SStuart Menefy }
197c1b40e44SStuart Menefy 
198c1b40e44SStuart Menefy static cycle_t gt_clocksource_read(struct clocksource *cs)
199c1b40e44SStuart Menefy {
200c1b40e44SStuart Menefy 	return gt_counter_read();
201c1b40e44SStuart Menefy }
202c1b40e44SStuart Menefy 
2039c9ae5ffSGrygorii Strashko static void gt_resume(struct clocksource *cs)
2049c9ae5ffSGrygorii Strashko {
2059c9ae5ffSGrygorii Strashko 	unsigned long ctrl;
2069c9ae5ffSGrygorii Strashko 
2079c9ae5ffSGrygorii Strashko 	ctrl = readl(gt_base + GT_CONTROL);
2089c9ae5ffSGrygorii Strashko 	if (!(ctrl & GT_CONTROL_TIMER_ENABLE))
2099c9ae5ffSGrygorii Strashko 		/* re-enable timer on resume */
2109c9ae5ffSGrygorii Strashko 		writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
2119c9ae5ffSGrygorii Strashko }
2129c9ae5ffSGrygorii Strashko 
213c1b40e44SStuart Menefy static struct clocksource gt_clocksource = {
214c1b40e44SStuart Menefy 	.name	= "arm_global_timer",
215c1b40e44SStuart Menefy 	.rating	= 300,
216c1b40e44SStuart Menefy 	.read	= gt_clocksource_read,
217c1b40e44SStuart Menefy 	.mask	= CLOCKSOURCE_MASK(64),
218c1b40e44SStuart Menefy 	.flags	= CLOCK_SOURCE_IS_CONTINUOUS,
2199c9ae5ffSGrygorii Strashko 	.resume = gt_resume,
220c1b40e44SStuart Menefy };
221c1b40e44SStuart Menefy 
222c1b40e44SStuart Menefy #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
223af066fceSStephen Boyd static u64 notrace gt_sched_clock_read(void)
224c1b40e44SStuart Menefy {
225d6df3576SJisheng Zhang 	return _gt_counter_read();
226c1b40e44SStuart Menefy }
227c1b40e44SStuart Menefy #endif
228c1b40e44SStuart Menefy 
229bbaa0670SRabin Vincent static unsigned long gt_read_long(void)
230bbaa0670SRabin Vincent {
231bbaa0670SRabin Vincent 	return readl_relaxed(gt_base + GT_COUNTER0);
232bbaa0670SRabin Vincent }
233bbaa0670SRabin Vincent 
234bbaa0670SRabin Vincent static struct delay_timer gt_delay_timer = {
235bbaa0670SRabin Vincent 	.read_current_timer = gt_read_long,
236bbaa0670SRabin Vincent };
237bbaa0670SRabin Vincent 
238bbaa0670SRabin Vincent static void __init gt_delay_timer_init(void)
239bbaa0670SRabin Vincent {
240bbaa0670SRabin Vincent 	gt_delay_timer.freq = gt_clk_rate;
241bbaa0670SRabin Vincent 	register_current_timer_delay(&gt_delay_timer);
242bbaa0670SRabin Vincent }
243bbaa0670SRabin Vincent 
2445a54c187SDaniel Lezcano static int __init gt_clocksource_init(void)
245c1b40e44SStuart Menefy {
246c1b40e44SStuart Menefy 	writel(0, gt_base + GT_CONTROL);
247c1b40e44SStuart Menefy 	writel(0, gt_base + GT_COUNTER0);
248c1b40e44SStuart Menefy 	writel(0, gt_base + GT_COUNTER1);
249c1b40e44SStuart Menefy 	/* enables timer on all the cores */
250c1b40e44SStuart Menefy 	writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);
251c1b40e44SStuart Menefy 
252c1b40e44SStuart Menefy #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK
253af066fceSStephen Boyd 	sched_clock_register(gt_sched_clock_read, 64, gt_clk_rate);
254c1b40e44SStuart Menefy #endif
2555a54c187SDaniel Lezcano 	return clocksource_register_hz(&gt_clocksource, gt_clk_rate);
256c1b40e44SStuart Menefy }
257c1b40e44SStuart Menefy 
2585a54c187SDaniel Lezcano static int __init global_timer_of_register(struct device_node *np)
259c1b40e44SStuart Menefy {
260c1b40e44SStuart Menefy 	struct clk *gt_clk;
261c1b40e44SStuart Menefy 	int err = 0;
262c1b40e44SStuart Menefy 
263c1b40e44SStuart Menefy 	/*
2642cf2ff9fSMatthew Leach 	 * In A9 r2p0 the comparators for each processor with the global timer
265c1b40e44SStuart Menefy 	 * fire when the timer value is greater than or equal to. In previous
266c1b40e44SStuart Menefy 	 * revisions the comparators fired when the timer value was equal to.
267c1b40e44SStuart Menefy 	 */
268af040ffcSRussell King 	if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9
2692cf2ff9fSMatthew Leach 	    && (read_cpuid_id() & 0xf0000f) < 0x200000) {
270c1b40e44SStuart Menefy 		pr_warn("global-timer: non support for this cpu version.\n");
2715a54c187SDaniel Lezcano 		return -ENOSYS;
272c1b40e44SStuart Menefy 	}
273c1b40e44SStuart Menefy 
274c1b40e44SStuart Menefy 	gt_ppi = irq_of_parse_and_map(np, 0);
275c1b40e44SStuart Menefy 	if (!gt_ppi) {
276c1b40e44SStuart Menefy 		pr_warn("global-timer: unable to parse irq\n");
2775a54c187SDaniel Lezcano 		return -EINVAL;
278c1b40e44SStuart Menefy 	}
279c1b40e44SStuart Menefy 
280c1b40e44SStuart Menefy 	gt_base = of_iomap(np, 0);
281c1b40e44SStuart Menefy 	if (!gt_base) {
282c1b40e44SStuart Menefy 		pr_warn("global-timer: invalid base address\n");
2835a54c187SDaniel Lezcano 		return -ENXIO;
284c1b40e44SStuart Menefy 	}
285c1b40e44SStuart Menefy 
286c1b40e44SStuart Menefy 	gt_clk = of_clk_get(np, 0);
287c1b40e44SStuart Menefy 	if (!IS_ERR(gt_clk)) {
288c1b40e44SStuart Menefy 		err = clk_prepare_enable(gt_clk);
289c1b40e44SStuart Menefy 		if (err)
290c1b40e44SStuart Menefy 			goto out_unmap;
291c1b40e44SStuart Menefy 	} else {
292c1b40e44SStuart Menefy 		pr_warn("global-timer: clk not found\n");
293c1b40e44SStuart Menefy 		err = -EINVAL;
294c1b40e44SStuart Menefy 		goto out_unmap;
295c1b40e44SStuart Menefy 	}
296c1b40e44SStuart Menefy 
297c1b40e44SStuart Menefy 	gt_clk_rate = clk_get_rate(gt_clk);
298c1b40e44SStuart Menefy 	gt_evt = alloc_percpu(struct clock_event_device);
299c1b40e44SStuart Menefy 	if (!gt_evt) {
300c1b40e44SStuart Menefy 		pr_warn("global-timer: can't allocate memory\n");
301c1b40e44SStuart Menefy 		err = -ENOMEM;
302c1b40e44SStuart Menefy 		goto out_clk;
303c1b40e44SStuart Menefy 	}
304c1b40e44SStuart Menefy 
305c1b40e44SStuart Menefy 	err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt,
306c1b40e44SStuart Menefy 				 "gt", gt_evt);
307c1b40e44SStuart Menefy 	if (err) {
308c1b40e44SStuart Menefy 		pr_warn("global-timer: can't register interrupt %d (%d)\n",
309c1b40e44SStuart Menefy 			gt_ppi, err);
310c1b40e44SStuart Menefy 		goto out_free;
311c1b40e44SStuart Menefy 	}
312c1b40e44SStuart Menefy 
313*b8a12296SRichard Cochran 	/* Register and immediately configure the timer on the boot CPU */
3145a54c187SDaniel Lezcano 	err = gt_clocksource_init();
3155a54c187SDaniel Lezcano 	if (err)
3165a54c187SDaniel Lezcano 		goto out_irq;
3175a54c187SDaniel Lezcano 
318*b8a12296SRichard Cochran 	err = cpuhp_setup_state(CPUHP_AP_ARM_GLOBAL_TIMER_STARTING,
319*b8a12296SRichard Cochran 				"AP_ARM_GLOBAL_TIMER_STARTING",
320*b8a12296SRichard Cochran 				gt_starting_cpu, gt_dying_cpu);
3215a54c187SDaniel Lezcano 	if (err)
3225a54c187SDaniel Lezcano 		goto out_irq;
3235a54c187SDaniel Lezcano 
324bbaa0670SRabin Vincent 	gt_delay_timer_init();
325c1b40e44SStuart Menefy 
3265a54c187SDaniel Lezcano 	return 0;
327c1b40e44SStuart Menefy 
328c1b40e44SStuart Menefy out_irq:
329c1b40e44SStuart Menefy 	free_percpu_irq(gt_ppi, gt_evt);
330c1b40e44SStuart Menefy out_free:
331c1b40e44SStuart Menefy 	free_percpu(gt_evt);
332c1b40e44SStuart Menefy out_clk:
333c1b40e44SStuart Menefy 	clk_disable_unprepare(gt_clk);
334c1b40e44SStuart Menefy out_unmap:
335c1b40e44SStuart Menefy 	iounmap(gt_base);
336c1b40e44SStuart Menefy 	WARN(err, "ARM Global timer register failed (%d)\n", err);
3375a54c187SDaniel Lezcano 
3385a54c187SDaniel Lezcano 	return err;
339c1b40e44SStuart Menefy }
340c1b40e44SStuart Menefy 
341c1b40e44SStuart Menefy /* Only tested on r2p2 and r3p0  */
342177cf6e5SDaniel Lezcano CLOCKSOURCE_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer",
343c1b40e44SStuart Menefy 			global_timer_of_register);
344