2 * linux/arch/arm/kernel/arch_timer.c
4 * Copyright (C) 2011 ARM Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/smp.h>
16 #include <linux/cpu.h>
17 #include <linux/jiffies.h>
18 #include <linux/clockchips.h>
19 #include <linux/interrupt.h>
20 #include <linux/of_irq.h>
23 #include <asm/delay.h>
24 #include <asm/arch_timer.h>
25 #include <asm/sched_clock.h>
27 static u32 arch_timer_rate;
37 static int arch_timer_ppi[MAX_TIMER_PPI];
39 static struct clock_event_device __percpu *arch_timer_evt;
40 static struct delay_timer arch_delay_timer;
42 static bool arch_timer_use_virtual = true;
45 * Architected system timer support.
48 static irqreturn_t inline timer_handler(const int access,
49 struct clock_event_device *evt)
52 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
53 if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
54 ctrl |= ARCH_TIMER_CTRL_IT_MASK;
55 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
56 evt->event_handler(evt);
63 static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
65 struct clock_event_device *evt = dev_id;
67 return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
70 static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
72 struct clock_event_device *evt = dev_id;
74 return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
77 static inline void timer_set_mode(const int access, int mode)
81 case CLOCK_EVT_MODE_UNUSED:
82 case CLOCK_EVT_MODE_SHUTDOWN:
83 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
84 ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
85 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
92 static void arch_timer_set_mode_virt(enum clock_event_mode mode,
93 struct clock_event_device *clk)
95 timer_set_mode(ARCH_TIMER_VIRT_ACCESS, mode);
98 static void arch_timer_set_mode_phys(enum clock_event_mode mode,
99 struct clock_event_device *clk)
101 timer_set_mode(ARCH_TIMER_PHYS_ACCESS, mode);
104 static inline void set_next_event(const int access, unsigned long evt)
107 ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
108 ctrl |= ARCH_TIMER_CTRL_ENABLE;
109 ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
110 arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt);
111 arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
114 static int arch_timer_set_next_event_virt(unsigned long evt,
115 struct clock_event_device *unused)
117 set_next_event(ARCH_TIMER_VIRT_ACCESS, evt);
121 static int arch_timer_set_next_event_phys(unsigned long evt,
122 struct clock_event_device *unused)
124 set_next_event(ARCH_TIMER_PHYS_ACCESS, evt);
128 static int __cpuinit arch_timer_setup(struct clock_event_device *clk)
130 clk->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP;
131 clk->name = "arch_sys_timer";
133 if (arch_timer_use_virtual) {
134 clk->irq = arch_timer_ppi[VIRT_PPI];
135 clk->set_mode = arch_timer_set_mode_virt;
136 clk->set_next_event = arch_timer_set_next_event_virt;
138 clk->irq = arch_timer_ppi[PHYS_SECURE_PPI];
139 clk->set_mode = arch_timer_set_mode_phys;
140 clk->set_next_event = arch_timer_set_next_event_phys;
143 clk->cpumask = cpumask_of(smp_processor_id());
145 clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, NULL);
147 clockevents_config_and_register(clk, arch_timer_rate,
150 if (arch_timer_use_virtual)
151 enable_percpu_irq(arch_timer_ppi[VIRT_PPI], 0);
153 enable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], 0);
154 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
155 enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], 0);
158 arch_counter_set_user_access();
163 static int arch_timer_available(void)
167 if (arch_timer_rate == 0) {
168 freq = arch_timer_get_cntfrq();
170 /* Check the timer frequency. */
172 pr_warn("Architected timer frequency not available\n");
176 arch_timer_rate = freq;
179 pr_info_once("Architected local timer running at %lu.%02luMHz (%s).\n",
180 (unsigned long)arch_timer_rate / 1000000,
181 (unsigned long)(arch_timer_rate / 10000) % 100,
182 arch_timer_use_virtual ? "virt" : "phys");
187 * Some external users of arch_timer_read_counter (e.g. sched_clock) may try to
188 * call it before it has been initialised. Rather than incur a performance
189 * penalty checking for initialisation, provide a default implementation that
190 * won't lead to time appearing to jump backwards.
192 static u64 arch_timer_read_zero(void)
197 u64 (*arch_timer_read_counter)(void) = arch_timer_read_zero;
199 static u32 arch_timer_read_counter32(void)
201 return arch_timer_read_counter();
204 static cycle_t arch_counter_read(struct clocksource *cs)
206 return arch_timer_read_counter();
209 static unsigned long arch_timer_read_current_timer(void)
211 return arch_timer_read_counter();
214 static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
216 return arch_timer_read_counter();
219 static struct clocksource clocksource_counter = {
220 .name = "arch_sys_counter",
222 .read = arch_counter_read,
223 .mask = CLOCKSOURCE_MASK(56),
224 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
227 static struct cyclecounter cyclecounter = {
228 .read = arch_counter_read_cc,
229 .mask = CLOCKSOURCE_MASK(56),
232 static struct timecounter timecounter;
234 struct timecounter *arch_timer_get_timecounter(void)
239 static void __cpuinit arch_timer_stop(struct clock_event_device *clk)
241 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
242 clk->irq, smp_processor_id());
244 if (arch_timer_use_virtual)
245 disable_percpu_irq(arch_timer_ppi[VIRT_PPI]);
247 disable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI]);
248 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
249 disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
252 clk->set_mode(CLOCK_EVT_MODE_UNUSED, clk);
255 static int __cpuinit arch_timer_cpu_notify(struct notifier_block *self,
256 unsigned long action, void *hcpu)
258 struct clock_event_device *evt = this_cpu_ptr(arch_timer_evt);
260 switch (action & ~CPU_TASKS_FROZEN) {
262 arch_timer_setup(evt);
265 arch_timer_stop(evt);
272 static struct notifier_block arch_timer_cpu_nb __cpuinitdata = {
273 .notifier_call = arch_timer_cpu_notify,
276 static int __init arch_timer_register(void)
281 err = arch_timer_available();
285 arch_timer_evt = alloc_percpu(struct clock_event_device);
286 if (!arch_timer_evt) {
291 clocksource_register_hz(&clocksource_counter, arch_timer_rate);
292 cyclecounter.mult = clocksource_counter.mult;
293 cyclecounter.shift = clocksource_counter.shift;
294 timecounter_init(&timecounter, &cyclecounter,
295 arch_counter_get_cntpct());
297 if (arch_timer_use_virtual) {
298 ppi = arch_timer_ppi[VIRT_PPI];
299 err = request_percpu_irq(ppi, arch_timer_handler_virt,
300 "arch_timer", arch_timer_evt);
302 ppi = arch_timer_ppi[PHYS_SECURE_PPI];
303 err = request_percpu_irq(ppi, arch_timer_handler_phys,
304 "arch_timer", arch_timer_evt);
305 if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
306 ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
307 err = request_percpu_irq(ppi, arch_timer_handler_phys,
308 "arch_timer", arch_timer_evt);
310 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
316 pr_err("arch_timer: can't register interrupt %d (%d)\n",
321 err = register_cpu_notifier(&arch_timer_cpu_nb);
325 /* Immediately configure the timer on the boot CPU */
326 arch_timer_setup(this_cpu_ptr(arch_timer_evt));
328 /* Use the architected timer for the delay loop. */
329 arch_delay_timer.read_current_timer = &arch_timer_read_current_timer;
330 arch_delay_timer.freq = arch_timer_rate;
331 register_current_timer_delay(&arch_delay_timer);
335 if (arch_timer_use_virtual)
336 free_percpu_irq(arch_timer_ppi[VIRT_PPI], arch_timer_evt);
338 free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
340 if (arch_timer_ppi[PHYS_NONSECURE_PPI])
341 free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
346 free_percpu(arch_timer_evt);
351 static const struct of_device_id arch_timer_of_match[] __initconst = {
352 { .compatible = "arm,armv7-timer", },
356 int __init arch_timer_of_register(void)
358 struct device_node *np;
362 np = of_find_matching_node(NULL, arch_timer_of_match);
364 pr_err("arch_timer: can't find DT node\n");
368 /* Try to determine the frequency from the device tree or CNTFRQ */
369 if (!of_property_read_u32(np, "clock-frequency", &freq))
370 arch_timer_rate = freq;
372 for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
373 arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
378 * If no interrupt provided for virtual timer, we'll have to
379 * stick to the physical timer. It'd better be accessible...
381 if (!arch_timer_ppi[VIRT_PPI]) {
382 arch_timer_use_virtual = false;
384 if (!arch_timer_ppi[PHYS_SECURE_PPI] ||
385 !arch_timer_ppi[PHYS_NONSECURE_PPI]) {
386 pr_warn("arch_timer: No interrupt available, giving up\n");
391 if (arch_timer_use_virtual)
392 arch_timer_read_counter = arch_counter_get_cntvct;
394 arch_timer_read_counter = arch_counter_get_cntpct;
396 return arch_timer_register();
399 int __init arch_timer_sched_clock_init(void)
403 err = arch_timer_available();
407 setup_sched_clock(arch_timer_read_counter32,
408 32, arch_timer_rate);