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/cputype.h>
24 #include <asm/localtimer.h>
25 #include <asm/arch_timer.h>
26 #include <asm/system_info.h>
27 #include <asm/sched_clock.h>
29 static unsigned long arch_timer_rate
;
30 static int arch_timer_ppi
;
31 static int arch_timer_ppi2
;
33 static struct clock_event_device __percpu
**arch_timer_evt
;
35 extern void init_current_timer_delay(unsigned long freq
);
38 * Architected system timer support.
41 #define ARCH_TIMER_CTRL_ENABLE (1 << 0)
42 #define ARCH_TIMER_CTRL_IT_MASK (1 << 1)
43 #define ARCH_TIMER_CTRL_IT_STAT (1 << 2)
45 #define ARCH_TIMER_REG_CTRL 0
46 #define ARCH_TIMER_REG_FREQ 1
47 #define ARCH_TIMER_REG_TVAL 2
49 static void arch_timer_reg_write(int reg
, u32 val
)
52 case ARCH_TIMER_REG_CTRL
:
53 asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (val
));
55 case ARCH_TIMER_REG_TVAL
:
56 asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (val
));
63 static u32
arch_timer_reg_read(int reg
)
68 case ARCH_TIMER_REG_CTRL
:
69 asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val
));
71 case ARCH_TIMER_REG_FREQ
:
72 asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val
));
74 case ARCH_TIMER_REG_TVAL
:
75 asm volatile("mrc p15, 0, %0, c14, c2, 0" : "=r" (val
));
84 static irqreturn_t
arch_timer_handler(int irq
, void *dev_id
)
86 struct clock_event_device
*evt
= *(struct clock_event_device
**)dev_id
;
89 ctrl
= arch_timer_reg_read(ARCH_TIMER_REG_CTRL
);
90 if (ctrl
& ARCH_TIMER_CTRL_IT_STAT
) {
91 ctrl
|= ARCH_TIMER_CTRL_IT_MASK
;
92 arch_timer_reg_write(ARCH_TIMER_REG_CTRL
, ctrl
);
93 evt
->event_handler(evt
);
100 static void arch_timer_disable(void)
104 ctrl
= arch_timer_reg_read(ARCH_TIMER_REG_CTRL
);
105 ctrl
&= ~ARCH_TIMER_CTRL_ENABLE
;
106 arch_timer_reg_write(ARCH_TIMER_REG_CTRL
, ctrl
);
109 static void arch_timer_set_mode(enum clock_event_mode mode
,
110 struct clock_event_device
*clk
)
113 case CLOCK_EVT_MODE_UNUSED
:
114 case CLOCK_EVT_MODE_SHUTDOWN
:
115 arch_timer_disable();
122 static int arch_timer_set_next_event(unsigned long evt
,
123 struct clock_event_device
*unused
)
127 ctrl
= arch_timer_reg_read(ARCH_TIMER_REG_CTRL
);
128 ctrl
|= ARCH_TIMER_CTRL_ENABLE
;
129 ctrl
&= ~ARCH_TIMER_CTRL_IT_MASK
;
131 arch_timer_reg_write(ARCH_TIMER_REG_TVAL
, evt
);
132 arch_timer_reg_write(ARCH_TIMER_REG_CTRL
, ctrl
);
137 static int __cpuinit
arch_timer_setup(struct clock_event_device
*clk
)
140 arch_timer_disable();
142 clk
->features
= CLOCK_EVT_FEAT_ONESHOT
| CLOCK_EVT_FEAT_C3STOP
;
143 clk
->name
= "arch_sys_timer";
145 clk
->set_mode
= arch_timer_set_mode
;
146 clk
->set_next_event
= arch_timer_set_next_event
;
147 clk
->irq
= arch_timer_ppi
;
149 clockevents_config_and_register(clk
, arch_timer_rate
,
152 *__this_cpu_ptr(arch_timer_evt
) = clk
;
154 enable_percpu_irq(clk
->irq
, 0);
156 enable_percpu_irq(arch_timer_ppi2
, 0);
161 /* Is the optional system timer available? */
162 static int local_timer_is_architected(void)
164 return (cpu_architecture() >= CPU_ARCH_ARMv7
) &&
165 ((read_cpuid_ext(CPUID_EXT_PFR1
) >> 16) & 0xf) == 1;
168 static int arch_timer_available(void)
172 if (!local_timer_is_architected())
175 if (arch_timer_rate
== 0) {
176 arch_timer_reg_write(ARCH_TIMER_REG_CTRL
, 0);
177 freq
= arch_timer_reg_read(ARCH_TIMER_REG_FREQ
);
179 /* Check the timer frequency. */
181 pr_warn("Architected timer frequency not available\n");
185 arch_timer_rate
= freq
;
188 pr_info_once("Architected local timer running at %lu.%02luMHz.\n",
189 arch_timer_rate
/ 1000000, (arch_timer_rate
/ 10000) % 100);
193 static inline cycle_t
arch_counter_get_cntpct(void)
197 asm volatile("mrrc p15, 0, %0, %1, c14" : "=r" (cvall
), "=r" (cvalh
));
199 return ((cycle_t
) cvalh
<< 32) | cvall
;
202 static inline cycle_t
arch_counter_get_cntvct(void)
206 asm volatile("mrrc p15, 1, %0, %1, c14" : "=r" (cvall
), "=r" (cvalh
));
208 return ((cycle_t
) cvalh
<< 32) | cvall
;
211 static u32 notrace
arch_counter_get_cntvct32(void)
213 cycle_t cntvct
= arch_counter_get_cntvct();
216 * The sched_clock infrastructure only knows about counters
217 * with at most 32bits. Forget about the upper 24 bits for the
220 return (u32
)(cntvct
& (u32
)~0);
223 static cycle_t
arch_counter_read(struct clocksource
*cs
)
225 return arch_counter_get_cntpct();
228 int read_current_timer(unsigned long *timer_val
)
230 if (!arch_timer_rate
)
232 *timer_val
= arch_counter_get_cntpct();
236 static struct clocksource clocksource_counter
= {
237 .name
= "arch_sys_counter",
239 .read
= arch_counter_read
,
240 .mask
= CLOCKSOURCE_MASK(56),
241 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
244 static void __cpuinit
arch_timer_stop(struct clock_event_device
*clk
)
246 pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
247 clk
->irq
, smp_processor_id());
248 disable_percpu_irq(clk
->irq
);
250 disable_percpu_irq(arch_timer_ppi2
);
251 arch_timer_set_mode(CLOCK_EVT_MODE_UNUSED
, clk
);
254 static struct local_timer_ops arch_timer_ops __cpuinitdata
= {
255 .setup
= arch_timer_setup
,
256 .stop
= arch_timer_stop
,
259 static struct clock_event_device arch_timer_global_evt
;
261 static int __init
arch_timer_register(void)
265 err
= arch_timer_available();
269 arch_timer_evt
= alloc_percpu(struct clock_event_device
*);
273 clocksource_register_hz(&clocksource_counter
, arch_timer_rate
);
275 err
= request_percpu_irq(arch_timer_ppi
, arch_timer_handler
,
276 "arch_timer", arch_timer_evt
);
278 pr_err("arch_timer: can't register interrupt %d (%d)\n",
279 arch_timer_ppi
, err
);
283 if (arch_timer_ppi2
) {
284 err
= request_percpu_irq(arch_timer_ppi2
, arch_timer_handler
,
285 "arch_timer", arch_timer_evt
);
287 pr_err("arch_timer: can't register interrupt %d (%d)\n",
288 arch_timer_ppi2
, err
);
294 err
= local_timer_register(&arch_timer_ops
);
297 * We couldn't register as a local timer (could be
298 * because we're on a UP platform, or because some
299 * other local timer is already present...). Try as a
300 * global timer instead.
302 arch_timer_global_evt
.cpumask
= cpumask_of(0);
303 err
= arch_timer_setup(&arch_timer_global_evt
);
309 init_current_timer_delay(arch_timer_rate
);
313 free_percpu_irq(arch_timer_ppi
, arch_timer_evt
);
315 free_percpu_irq(arch_timer_ppi2
, arch_timer_evt
);
318 free_percpu(arch_timer_evt
);
323 static const struct of_device_id arch_timer_of_match
[] __initconst
= {
324 { .compatible
= "arm,armv7-timer", },
328 int __init
arch_timer_of_register(void)
330 struct device_node
*np
;
333 np
= of_find_matching_node(NULL
, arch_timer_of_match
);
335 pr_err("arch_timer: can't find DT node\n");
339 /* Try to determine the frequency from the device tree or CNTFRQ */
340 if (!of_property_read_u32(np
, "clock-frequency", &freq
))
341 arch_timer_rate
= freq
;
343 arch_timer_ppi
= irq_of_parse_and_map(np
, 0);
344 arch_timer_ppi2
= irq_of_parse_and_map(np
, 1);
345 pr_info("arch_timer: found %s irqs %d %d\n",
346 np
->name
, arch_timer_ppi
, arch_timer_ppi2
);
348 return arch_timer_register();
351 int __init
arch_timer_sched_clock_init(void)
355 err
= arch_timer_available();
359 setup_sched_clock(arch_counter_get_cntvct32
, 32, arch_timer_rate
);