Commit | Line | Data |
---|---|---|
1dbae815 | 1 | /* |
0f622e8c | 2 | * linux/arch/arm/mach-omap2/timer.c |
1dbae815 TL |
3 | * |
4 | * OMAP2 GP timer support. | |
5 | * | |
f248076c PW |
6 | * Copyright (C) 2009 Nokia Corporation |
7 | * | |
5a3a388f KH |
8 | * Update to use new clocksource/clockevent layers |
9 | * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com> | |
10 | * Copyright (C) 2007 MontaVista Software, Inc. | |
11 | * | |
12 | * Original driver: | |
1dbae815 TL |
13 | * Copyright (C) 2005 Nokia Corporation |
14 | * Author: Paul Mundt <paul.mundt@nokia.com> | |
96de0e25 | 15 | * Juha Yrjölä <juha.yrjola@nokia.com> |
77900a2f | 16 | * OMAP Dual-mode timer framework support by Timo Teras |
1dbae815 TL |
17 | * |
18 | * Some parts based off of TI's 24xx code: | |
19 | * | |
44169075 | 20 | * Copyright (C) 2004-2009 Texas Instruments, Inc. |
1dbae815 TL |
21 | * |
22 | * Roughly modelled after the OMAP1 MPU timer code. | |
44169075 | 23 | * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com> |
1dbae815 TL |
24 | * |
25 | * This file is subject to the terms and conditions of the GNU General Public | |
26 | * License. See the file "COPYING" in the main directory of this archive | |
27 | * for more details. | |
28 | */ | |
29 | #include <linux/init.h> | |
30 | #include <linux/time.h> | |
31 | #include <linux/interrupt.h> | |
32 | #include <linux/err.h> | |
f8ce2547 | 33 | #include <linux/clk.h> |
77900a2f | 34 | #include <linux/delay.h> |
e6687290 | 35 | #include <linux/irq.h> |
5a3a388f KH |
36 | #include <linux/clocksource.h> |
37 | #include <linux/clockchips.h> | |
c345c8b0 | 38 | #include <linux/slab.h> |
eed0de27 | 39 | #include <linux/of.h> |
9725f445 JH |
40 | #include <linux/of_address.h> |
41 | #include <linux/of_irq.h> | |
f8ce2547 | 42 | |
1dbae815 | 43 | #include <asm/mach/time.h> |
a45c983f | 44 | #include <asm/smp_twd.h> |
cbc94380 | 45 | #include <asm/sched_clock.h> |
7d7e1eba | 46 | |
3c7c5dab | 47 | #include <asm/arch_timer.h> |
38698bef | 48 | #include <plat/omap_hwmod.h> |
c345c8b0 | 49 | #include <plat/omap_device.h> |
7d7e1eba | 50 | #include <plat/dmtimer.h> |
b481113a TKD |
51 | #include <plat/omap-pm.h> |
52 | ||
dbc04161 | 53 | #include "soc.h" |
7d7e1eba | 54 | #include "common.h" |
b481113a | 55 | #include "powerdomain.h" |
1dbae815 | 56 | |
aa561889 TL |
57 | /* Parent clocks, eventually these will come from the clock framework */ |
58 | ||
59 | #define OMAP2_MPU_SOURCE "sys_ck" | |
60 | #define OMAP3_MPU_SOURCE OMAP2_MPU_SOURCE | |
61 | #define OMAP4_MPU_SOURCE "sys_clkin_ck" | |
62 | #define OMAP2_32K_SOURCE "func_32k_ck" | |
63 | #define OMAP3_32K_SOURCE "omap_32k_fck" | |
64 | #define OMAP4_32K_SOURCE "sys_32k_ck" | |
65 | ||
66 | #ifdef CONFIG_OMAP_32K_TIMER | |
67 | #define OMAP2_CLKEV_SOURCE OMAP2_32K_SOURCE | |
68 | #define OMAP3_CLKEV_SOURCE OMAP3_32K_SOURCE | |
69 | #define OMAP4_CLKEV_SOURCE OMAP4_32K_SOURCE | |
70 | #define OMAP3_SECURE_TIMER 12 | |
9725f445 | 71 | #define TIMER_PROP_SECURE "ti,timer-secure" |
aa561889 TL |
72 | #else |
73 | #define OMAP2_CLKEV_SOURCE OMAP2_MPU_SOURCE | |
74 | #define OMAP3_CLKEV_SOURCE OMAP3_MPU_SOURCE | |
75 | #define OMAP4_CLKEV_SOURCE OMAP4_MPU_SOURCE | |
76 | #define OMAP3_SECURE_TIMER 1 | |
9725f445 | 77 | #define TIMER_PROP_SECURE "ti,timer-alwon" |
aa561889 | 78 | #endif |
d8328f3b | 79 | |
fa6d79d2 SS |
80 | #define REALTIME_COUNTER_BASE 0x48243200 |
81 | #define INCREMENTER_NUMERATOR_OFFSET 0x10 | |
82 | #define INCREMENTER_DENUMERATOR_RELOAD_OFFSET 0x14 | |
83 | #define NUMERATOR_DENUMERATOR_MASK 0xfffff000 | |
84 | ||
aa561889 TL |
85 | /* Clockevent code */ |
86 | ||
87 | static struct omap_dm_timer clkev; | |
5a3a388f | 88 | static struct clock_event_device clockevent_gpt; |
1dbae815 | 89 | |
0cd61b68 | 90 | static irqreturn_t omap2_gp_timer_interrupt(int irq, void *dev_id) |
1dbae815 | 91 | { |
5a3a388f KH |
92 | struct clock_event_device *evt = &clockevent_gpt; |
93 | ||
ee17f114 | 94 | __omap_dm_timer_write_status(&clkev, OMAP_TIMER_INT_OVERFLOW); |
1dbae815 | 95 | |
5a3a388f | 96 | evt->event_handler(evt); |
1dbae815 TL |
97 | return IRQ_HANDLED; |
98 | } | |
99 | ||
100 | static struct irqaction omap2_gp_timer_irq = { | |
f36921be | 101 | .name = "gp_timer", |
b30fabad | 102 | .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL, |
1dbae815 TL |
103 | .handler = omap2_gp_timer_interrupt, |
104 | }; | |
105 | ||
5a3a388f KH |
106 | static int omap2_gp_timer_set_next_event(unsigned long cycles, |
107 | struct clock_event_device *evt) | |
1dbae815 | 108 | { |
ee17f114 | 109 | __omap_dm_timer_load_start(&clkev, OMAP_TIMER_CTRL_ST, |
aa561889 | 110 | 0xffffffff - cycles, 1); |
5a3a388f KH |
111 | |
112 | return 0; | |
113 | } | |
114 | ||
115 | static void omap2_gp_timer_set_mode(enum clock_event_mode mode, | |
116 | struct clock_event_device *evt) | |
117 | { | |
118 | u32 period; | |
119 | ||
ee17f114 | 120 | __omap_dm_timer_stop(&clkev, 1, clkev.rate); |
5a3a388f KH |
121 | |
122 | switch (mode) { | |
123 | case CLOCK_EVT_MODE_PERIODIC: | |
aa561889 | 124 | period = clkev.rate / HZ; |
5a3a388f | 125 | period -= 1; |
aa561889 | 126 | /* Looks like we need to first set the load value separately */ |
ee17f114 | 127 | __omap_dm_timer_write(&clkev, OMAP_TIMER_LOAD_REG, |
aa561889 | 128 | 0xffffffff - period, 1); |
ee17f114 | 129 | __omap_dm_timer_load_start(&clkev, |
aa561889 TL |
130 | OMAP_TIMER_CTRL_AR | OMAP_TIMER_CTRL_ST, |
131 | 0xffffffff - period, 1); | |
5a3a388f KH |
132 | break; |
133 | case CLOCK_EVT_MODE_ONESHOT: | |
134 | break; | |
135 | case CLOCK_EVT_MODE_UNUSED: | |
136 | case CLOCK_EVT_MODE_SHUTDOWN: | |
137 | case CLOCK_EVT_MODE_RESUME: | |
138 | break; | |
139 | } | |
140 | } | |
141 | ||
142 | static struct clock_event_device clockevent_gpt = { | |
f36921be | 143 | .name = "gp_timer", |
5a3a388f KH |
144 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, |
145 | .shift = 32, | |
11d6ec2e | 146 | .rating = 300, |
5a3a388f KH |
147 | .set_next_event = omap2_gp_timer_set_next_event, |
148 | .set_mode = omap2_gp_timer_set_mode, | |
149 | }; | |
150 | ||
ad24bde8 JH |
151 | static struct property device_disabled = { |
152 | .name = "status", | |
153 | .length = sizeof("disabled"), | |
154 | .value = "disabled", | |
155 | }; | |
156 | ||
157 | static struct of_device_id omap_timer_match[] __initdata = { | |
158 | { .compatible = "ti,omap2-timer", }, | |
159 | { } | |
160 | }; | |
161 | ||
9725f445 JH |
162 | /** |
163 | * omap_get_timer_dt - get a timer using device-tree | |
164 | * @match - device-tree match structure for matching a device type | |
165 | * @property - optional timer property to match | |
166 | * | |
167 | * Helper function to get a timer during early boot using device-tree for use | |
168 | * as kernel system timer. Optionally, the property argument can be used to | |
169 | * select a timer with a specific property. Once a timer is found then mark | |
170 | * the timer node in device-tree as disabled, to prevent the kernel from | |
171 | * registering this timer as a platform device and so no one else can use it. | |
172 | */ | |
173 | static struct device_node * __init omap_get_timer_dt(struct of_device_id *match, | |
174 | const char *property) | |
175 | { | |
176 | struct device_node *np; | |
177 | ||
178 | for_each_matching_node(np, match) { | |
179 | if (!of_device_is_available(np)) { | |
180 | of_node_put(np); | |
181 | continue; | |
182 | } | |
183 | ||
184 | if (property && !of_get_property(np, property, NULL)) { | |
185 | of_node_put(np); | |
186 | continue; | |
187 | } | |
188 | ||
189 | prom_add_property(np, &device_disabled); | |
190 | return np; | |
191 | } | |
192 | ||
193 | return NULL; | |
194 | } | |
195 | ||
ad24bde8 JH |
196 | /** |
197 | * omap_dmtimer_init - initialisation function when device tree is used | |
198 | * | |
199 | * For secure OMAP3 devices, timers with device type "timer-secure" cannot | |
200 | * be used by the kernel as they are reserved. Therefore, to prevent the | |
201 | * kernel registering these devices remove them dynamically from the device | |
202 | * tree on boot. | |
203 | */ | |
204 | void __init omap_dmtimer_init(void) | |
205 | { | |
206 | struct device_node *np; | |
207 | ||
208 | if (!cpu_is_omap34xx()) | |
209 | return; | |
210 | ||
211 | /* If we are a secure device, remove any secure timer nodes */ | |
212 | if ((omap_type() != OMAP2_DEVICE_TYPE_GP)) { | |
9725f445 JH |
213 | np = omap_get_timer_dt(omap_timer_match, "ti,timer-secure"); |
214 | if (np) | |
215 | of_node_put(np); | |
ad24bde8 JH |
216 | } |
217 | } | |
218 | ||
aa561889 TL |
219 | static int __init omap_dm_timer_init_one(struct omap_dm_timer *timer, |
220 | int gptimer_id, | |
9725f445 JH |
221 | const char *fck_source, |
222 | const char *property) | |
5a3a388f | 223 | { |
aa561889 | 224 | char name[10]; /* 10 = sizeof("gptXX_Xck0") */ |
9725f445 JH |
225 | const char *oh_name; |
226 | struct device_node *np; | |
aa561889 | 227 | struct omap_hwmod *oh; |
6c0c27fd | 228 | struct resource irq_rsrc, mem_rsrc; |
aa561889 TL |
229 | size_t size; |
230 | int res = 0; | |
6c0c27fd | 231 | int r; |
aa561889 | 232 | |
9725f445 JH |
233 | if (of_have_populated_dt()) { |
234 | np = omap_get_timer_dt(omap_timer_match, NULL); | |
235 | if (!np) | |
236 | return -ENODEV; | |
237 | ||
238 | of_property_read_string_index(np, "ti,hwmods", 0, &oh_name); | |
239 | if (!oh_name) | |
240 | return -ENODEV; | |
241 | ||
242 | timer->irq = irq_of_parse_and_map(np, 0); | |
243 | if (!timer->irq) | |
244 | return -ENXIO; | |
245 | ||
246 | timer->io_base = of_iomap(np, 0); | |
247 | ||
248 | of_node_put(np); | |
249 | } else { | |
250 | if (omap_dm_timer_reserve_systimer(gptimer_id)) | |
251 | return -ENODEV; | |
252 | ||
253 | sprintf(name, "timer%d", gptimer_id); | |
254 | oh_name = name; | |
255 | } | |
256 | ||
257 | omap_hwmod_setup_one(oh_name); | |
258 | oh = omap_hwmod_lookup(oh_name); | |
259 | ||
aa561889 TL |
260 | if (!oh) |
261 | return -ENODEV; | |
262 | ||
9725f445 JH |
263 | if (!of_have_populated_dt()) { |
264 | r = omap_hwmod_get_resource_byname(oh, IORESOURCE_IRQ, NULL, | |
265 | &irq_rsrc); | |
266 | if (r) | |
267 | return -ENXIO; | |
268 | timer->irq = irq_rsrc.start; | |
269 | ||
270 | r = omap_hwmod_get_resource_byname(oh, IORESOURCE_MEM, NULL, | |
271 | &mem_rsrc); | |
272 | if (r) | |
273 | return -ENXIO; | |
274 | timer->phys_base = mem_rsrc.start; | |
275 | size = mem_rsrc.end - mem_rsrc.start; | |
276 | ||
277 | /* Static mapping, never released */ | |
278 | timer->io_base = ioremap(timer->phys_base, size); | |
279 | } | |
aa561889 | 280 | |
aa561889 TL |
281 | if (!timer->io_base) |
282 | return -ENXIO; | |
283 | ||
284 | /* After the dmtimer is using hwmod these clocks won't be needed */ | |
ae6df418 | 285 | timer->fclk = clk_get(NULL, omap_hwmod_get_main_clk(oh)); |
aa561889 TL |
286 | if (IS_ERR(timer->fclk)) |
287 | return -ENODEV; | |
288 | ||
aa561889 TL |
289 | omap_hwmod_enable(oh); |
290 | ||
9725f445 | 291 | /* FIXME: Need to remove hard-coded test on timer ID */ |
aa561889 TL |
292 | if (gptimer_id != 12) { |
293 | struct clk *src; | |
294 | ||
295 | src = clk_get(NULL, fck_source); | |
296 | if (IS_ERR(src)) { | |
297 | res = -EINVAL; | |
298 | } else { | |
299 | res = __omap_dm_timer_set_source(timer->fclk, src); | |
300 | if (IS_ERR_VALUE(res)) | |
9725f445 JH |
301 | pr_warn("%s: %s cannot set source\n", |
302 | __func__, oh->name); | |
aa561889 TL |
303 | clk_put(src); |
304 | } | |
305 | } | |
ee17f114 TL |
306 | __omap_dm_timer_init_regs(timer); |
307 | __omap_dm_timer_reset(timer, 1, 1); | |
aa561889 TL |
308 | timer->posted = 1; |
309 | ||
310 | timer->rate = clk_get_rate(timer->fclk); | |
1dbae815 | 311 | |
aa561889 | 312 | timer->reserved = 1; |
38698bef | 313 | |
aa561889 TL |
314 | return res; |
315 | } | |
f248076c | 316 | |
aa561889 | 317 | static void __init omap2_gp_clockevent_init(int gptimer_id, |
9725f445 JH |
318 | const char *fck_source, |
319 | const char *property) | |
aa561889 TL |
320 | { |
321 | int res; | |
f248076c | 322 | |
9725f445 | 323 | res = omap_dm_timer_init_one(&clkev, gptimer_id, fck_source, property); |
aa561889 | 324 | BUG_ON(res); |
f248076c | 325 | |
a032d33b | 326 | omap2_gp_timer_irq.dev_id = &clkev; |
aa561889 | 327 | setup_irq(clkev.irq, &omap2_gp_timer_irq); |
5a3a388f | 328 | |
ee17f114 | 329 | __omap_dm_timer_int_enable(&clkev, OMAP_TIMER_INT_OVERFLOW); |
aa561889 TL |
330 | |
331 | clockevent_gpt.mult = div_sc(clkev.rate, NSEC_PER_SEC, | |
5a3a388f KH |
332 | clockevent_gpt.shift); |
333 | clockevent_gpt.max_delta_ns = | |
334 | clockevent_delta2ns(0xffffffff, &clockevent_gpt); | |
335 | clockevent_gpt.min_delta_ns = | |
df88acbb AK |
336 | clockevent_delta2ns(3, &clockevent_gpt); |
337 | /* Timer internal resynch latency. */ | |
5a3a388f | 338 | |
11d6ec2e SS |
339 | clockevent_gpt.cpumask = cpu_possible_mask; |
340 | clockevent_gpt.irq = omap_dm_timer_get_irq(&clkev); | |
5a3a388f | 341 | clockevents_register_device(&clockevent_gpt); |
aa561889 TL |
342 | |
343 | pr_info("OMAP clockevent source: GPTIMER%d at %lu Hz\n", | |
344 | gptimer_id, clkev.rate); | |
5a3a388f KH |
345 | } |
346 | ||
f248076c | 347 | /* Clocksource code */ |
3d05a3e8 | 348 | static struct omap_dm_timer clksrc; |
1fe97c8f | 349 | static bool use_gptimer_clksrc; |
3d05a3e8 | 350 | |
5a3a388f KH |
351 | /* |
352 | * clocksource | |
353 | */ | |
8e19608e | 354 | static cycle_t clocksource_read_cycles(struct clocksource *cs) |
5a3a388f | 355 | { |
ee17f114 | 356 | return (cycle_t)__omap_dm_timer_read_counter(&clksrc, 1); |
5a3a388f KH |
357 | } |
358 | ||
359 | static struct clocksource clocksource_gpt = { | |
f36921be | 360 | .name = "gp_timer", |
5a3a388f KH |
361 | .rating = 300, |
362 | .read = clocksource_read_cycles, | |
363 | .mask = CLOCKSOURCE_MASK(32), | |
5a3a388f KH |
364 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
365 | }; | |
366 | ||
2f0778af | 367 | static u32 notrace dmtimer_read_sched_clock(void) |
cbc94380 | 368 | { |
3d05a3e8 | 369 | if (clksrc.reserved) |
dbc3982a | 370 | return __omap_dm_timer_read_counter(&clksrc, 1); |
5a3a388f | 371 | |
2f0778af | 372 | return 0; |
3d05a3e8 TL |
373 | } |
374 | ||
45caae74 | 375 | #ifdef CONFIG_OMAP_32K_TIMER |
3d05a3e8 | 376 | /* Setup free-running counter for clocksource */ |
1fe97c8f VH |
377 | static int __init omap2_sync32k_clocksource_init(void) |
378 | { | |
379 | int ret; | |
380 | struct omap_hwmod *oh; | |
381 | void __iomem *vbase; | |
382 | const char *oh_name = "counter_32k"; | |
383 | ||
384 | /* | |
385 | * First check hwmod data is available for sync32k counter | |
386 | */ | |
387 | oh = omap_hwmod_lookup(oh_name); | |
388 | if (!oh || oh->slaves_cnt == 0) | |
389 | return -ENODEV; | |
390 | ||
391 | omap_hwmod_setup_one(oh_name); | |
392 | ||
393 | vbase = omap_hwmod_get_mpu_rt_va(oh); | |
394 | if (!vbase) { | |
395 | pr_warn("%s: failed to get counter_32k resource\n", __func__); | |
396 | return -ENXIO; | |
397 | } | |
398 | ||
399 | ret = omap_hwmod_enable(oh); | |
400 | if (ret) { | |
401 | pr_warn("%s: failed to enable counter_32k module (%d)\n", | |
402 | __func__, ret); | |
403 | return ret; | |
404 | } | |
405 | ||
406 | ret = omap_init_clocksource_32k(vbase); | |
407 | if (ret) { | |
408 | pr_warn("%s: failed to initialize counter_32k as a clocksource (%d)\n", | |
409 | __func__, ret); | |
410 | omap_hwmod_idle(oh); | |
411 | } | |
412 | ||
413 | return ret; | |
414 | } | |
45caae74 IG |
415 | #else |
416 | static inline int omap2_sync32k_clocksource_init(void) | |
417 | { | |
418 | return -ENODEV; | |
419 | } | |
420 | #endif | |
1fe97c8f VH |
421 | |
422 | static void __init omap2_gptimer_clocksource_init(int gptimer_id, | |
3d05a3e8 TL |
423 | const char *fck_source) |
424 | { | |
425 | int res; | |
426 | ||
9725f445 | 427 | res = omap_dm_timer_init_one(&clksrc, gptimer_id, fck_source, NULL); |
3d05a3e8 | 428 | BUG_ON(res); |
5a3a388f | 429 | |
ee17f114 | 430 | __omap_dm_timer_load_start(&clksrc, |
e9d0b97e | 431 | OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR, 0, 1); |
2f0778af | 432 | setup_sched_clock(dmtimer_read_sched_clock, 32, clksrc.rate); |
cbc94380 | 433 | |
3d05a3e8 TL |
434 | if (clocksource_register_hz(&clocksource_gpt, clksrc.rate)) |
435 | pr_err("Could not register clocksource %s\n", | |
436 | clocksource_gpt.name); | |
1fe97c8f VH |
437 | else |
438 | pr_info("OMAP clocksource: GPTIMER%d at %lu Hz\n", | |
439 | gptimer_id, clksrc.rate); | |
440 | } | |
441 | ||
442 | static void __init omap2_clocksource_init(int gptimer_id, | |
443 | const char *fck_source) | |
444 | { | |
445 | /* | |
446 | * First give preference to kernel parameter configuration | |
447 | * by user (clocksource="gp_timer"). | |
448 | * | |
449 | * In case of missing kernel parameter for clocksource, | |
450 | * first check for availability for 32k-sync timer, in case | |
451 | * of failure in finding 32k_counter module or registering | |
452 | * it as clocksource, execution will fallback to gp-timer. | |
453 | */ | |
454 | if (use_gptimer_clksrc == true) | |
455 | omap2_gptimer_clocksource_init(gptimer_id, fck_source); | |
456 | else if (omap2_sync32k_clocksource_init()) | |
457 | /* Fall back to gp-timer code */ | |
458 | omap2_gptimer_clocksource_init(gptimer_id, fck_source); | |
5a3a388f | 459 | } |
5a3a388f | 460 | |
fa6d79d2 SS |
461 | #ifdef CONFIG_SOC_HAS_REALTIME_COUNTER |
462 | /* | |
463 | * The realtime counter also called master counter, is a free-running | |
464 | * counter, which is related to real time. It produces the count used | |
465 | * by the CPU local timer peripherals in the MPU cluster. The timer counts | |
466 | * at a rate of 6.144 MHz. Because the device operates on different clocks | |
467 | * in different power modes, the master counter shifts operation between | |
468 | * clocks, adjusting the increment per clock in hardware accordingly to | |
469 | * maintain a constant count rate. | |
470 | */ | |
471 | static void __init realtime_counter_init(void) | |
472 | { | |
473 | void __iomem *base; | |
474 | static struct clk *sys_clk; | |
475 | unsigned long rate; | |
476 | unsigned int reg, num, den; | |
477 | ||
478 | base = ioremap(REALTIME_COUNTER_BASE, SZ_32); | |
479 | if (!base) { | |
480 | pr_err("%s: ioremap failed\n", __func__); | |
481 | return; | |
482 | } | |
483 | sys_clk = clk_get(NULL, "sys_clkin_ck"); | |
533b2981 | 484 | if (IS_ERR(sys_clk)) { |
fa6d79d2 SS |
485 | pr_err("%s: failed to get system clock handle\n", __func__); |
486 | iounmap(base); | |
487 | return; | |
488 | } | |
489 | ||
490 | rate = clk_get_rate(sys_clk); | |
491 | /* Numerator/denumerator values refer TRM Realtime Counter section */ | |
492 | switch (rate) { | |
493 | case 1200000: | |
494 | num = 64; | |
495 | den = 125; | |
496 | break; | |
497 | case 1300000: | |
498 | num = 768; | |
499 | den = 1625; | |
500 | break; | |
501 | case 19200000: | |
502 | num = 8; | |
503 | den = 25; | |
504 | break; | |
505 | case 2600000: | |
506 | num = 384; | |
507 | den = 1625; | |
508 | break; | |
509 | case 2700000: | |
510 | num = 256; | |
511 | den = 1125; | |
512 | break; | |
513 | case 38400000: | |
514 | default: | |
515 | /* Program it for 38.4 MHz */ | |
516 | num = 4; | |
517 | den = 25; | |
518 | break; | |
519 | } | |
520 | ||
521 | /* Program numerator and denumerator registers */ | |
522 | reg = __raw_readl(base + INCREMENTER_NUMERATOR_OFFSET) & | |
523 | NUMERATOR_DENUMERATOR_MASK; | |
524 | reg |= num; | |
525 | __raw_writel(reg, base + INCREMENTER_NUMERATOR_OFFSET); | |
526 | ||
527 | reg = __raw_readl(base + INCREMENTER_NUMERATOR_OFFSET) & | |
528 | NUMERATOR_DENUMERATOR_MASK; | |
529 | reg |= den; | |
530 | __raw_writel(reg, base + INCREMENTER_DENUMERATOR_RELOAD_OFFSET); | |
531 | ||
532 | iounmap(base); | |
533 | } | |
534 | #else | |
535 | static inline void __init realtime_counter_init(void) | |
536 | {} | |
537 | #endif | |
538 | ||
9725f445 | 539 | #define OMAP_SYS_TIMER_INIT(name, clkev_nr, clkev_src, clkev_prop, \ |
3d05a3e8 | 540 | clksrc_nr, clksrc_src) \ |
e74984e4 TL |
541 | static void __init omap##name##_timer_init(void) \ |
542 | { \ | |
ad24bde8 | 543 | omap_dmtimer_init(); \ |
9725f445 | 544 | omap2_gp_clockevent_init((clkev_nr), clkev_src, clkev_prop); \ |
1fe97c8f | 545 | omap2_clocksource_init((clksrc_nr), clksrc_src); \ |
e74984e4 TL |
546 | } |
547 | ||
548 | #define OMAP_SYS_TIMER(name) \ | |
549 | struct sys_timer omap##name##_timer = { \ | |
550 | .init = omap##name##_timer_init, \ | |
551 | }; | |
552 | ||
553 | #ifdef CONFIG_ARCH_OMAP2 | |
9725f445 JH |
554 | OMAP_SYS_TIMER_INIT(2, 1, OMAP2_CLKEV_SOURCE, "ti,timer-alwon", |
555 | 2, OMAP2_MPU_SOURCE) | |
e74984e4 TL |
556 | OMAP_SYS_TIMER(2) |
557 | #endif | |
558 | ||
559 | #ifdef CONFIG_ARCH_OMAP3 | |
9725f445 JH |
560 | OMAP_SYS_TIMER_INIT(3, 1, OMAP3_CLKEV_SOURCE, "ti,timer-alwon", |
561 | 2, OMAP3_MPU_SOURCE) | |
e74984e4 | 562 | OMAP_SYS_TIMER(3) |
3d05a3e8 | 563 | OMAP_SYS_TIMER_INIT(3_secure, OMAP3_SECURE_TIMER, OMAP3_CLKEV_SOURCE, |
9725f445 | 564 | TIMER_PROP_SECURE, 2, OMAP3_MPU_SOURCE) |
e74984e4 TL |
565 | OMAP_SYS_TIMER(3_secure) |
566 | #endif | |
567 | ||
08f30989 | 568 | #ifdef CONFIG_SOC_AM33XX |
9725f445 JH |
569 | OMAP_SYS_TIMER_INIT(3_am33xx, 1, OMAP4_MPU_SOURCE, "ti,timer-alwon", |
570 | 2, OMAP4_MPU_SOURCE) | |
08f30989 AM |
571 | OMAP_SYS_TIMER(3_am33xx) |
572 | #endif | |
573 | ||
e74984e4 | 574 | #ifdef CONFIG_ARCH_OMAP4 |
39e1d4c1 | 575 | #ifdef CONFIG_LOCAL_TIMERS |
a45c983f | 576 | static DEFINE_TWD_LOCAL_TIMER(twd_local_timer, |
3f216ef3 | 577 | OMAP44XX_LOCAL_TWD_BASE, 29); |
39e1d4c1 | 578 | #endif |
a45c983f MZ |
579 | |
580 | static void __init omap4_timer_init(void) | |
581 | { | |
9725f445 | 582 | omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE, "ti,timer-alwon"); |
1fe97c8f | 583 | omap2_clocksource_init(2, OMAP4_MPU_SOURCE); |
a45c983f MZ |
584 | #ifdef CONFIG_LOCAL_TIMERS |
585 | /* Local timers are not supprted on OMAP4430 ES1.0 */ | |
586 | if (omap_rev() != OMAP4430_REV_ES1_0) { | |
587 | int err; | |
588 | ||
eed0de27 SS |
589 | if (of_have_populated_dt()) { |
590 | twd_local_timer_of_register(); | |
591 | return; | |
592 | } | |
593 | ||
a45c983f MZ |
594 | err = twd_local_timer_register(&twd_local_timer); |
595 | if (err) | |
596 | pr_err("twd_local_timer_register failed %d\n", err); | |
597 | } | |
598 | #endif | |
1dbae815 | 599 | } |
e74984e4 TL |
600 | OMAP_SYS_TIMER(4) |
601 | #endif | |
c345c8b0 | 602 | |
37b3280d | 603 | #ifdef CONFIG_SOC_OMAP5 |
fa6d79d2 SS |
604 | static void __init omap5_timer_init(void) |
605 | { | |
3c7c5dab SS |
606 | int err; |
607 | ||
9725f445 | 608 | omap2_gp_clockevent_init(1, OMAP4_CLKEV_SOURCE, "ti,timer-alwon"); |
fa6d79d2 SS |
609 | omap2_clocksource_init(2, OMAP4_MPU_SOURCE); |
610 | realtime_counter_init(); | |
3c7c5dab SS |
611 | |
612 | err = arch_timer_of_register(); | |
613 | if (err) | |
614 | pr_err("%s: arch_timer_register failed %d\n", __func__, err); | |
fa6d79d2 | 615 | } |
37b3280d S |
616 | OMAP_SYS_TIMER(5) |
617 | #endif | |
618 | ||
c345c8b0 TKD |
619 | /** |
620 | * omap_timer_init - build and register timer device with an | |
621 | * associated timer hwmod | |
622 | * @oh: timer hwmod pointer to be used to build timer device | |
623 | * @user: parameter that can be passed from calling hwmod API | |
624 | * | |
625 | * Called by omap_hwmod_for_each_by_class to register each of the timer | |
626 | * devices present in the system. The number of timer devices is known | |
627 | * by parsing through the hwmod database for a given class name. At the | |
628 | * end of function call memory is allocated for timer device and it is | |
629 | * registered to the framework ready to be proved by the driver. | |
630 | */ | |
631 | static int __init omap_timer_init(struct omap_hwmod *oh, void *unused) | |
632 | { | |
633 | int id; | |
634 | int ret = 0; | |
635 | char *name = "omap_timer"; | |
636 | struct dmtimer_platform_data *pdata; | |
c541c15f | 637 | struct platform_device *pdev; |
c345c8b0 TKD |
638 | struct omap_timer_capability_dev_attr *timer_dev_attr; |
639 | ||
640 | pr_debug("%s: %s\n", __func__, oh->name); | |
641 | ||
642 | /* on secure device, do not register secure timer */ | |
643 | timer_dev_attr = oh->dev_attr; | |
644 | if (omap_type() != OMAP2_DEVICE_TYPE_GP && timer_dev_attr) | |
645 | if (timer_dev_attr->timer_capability == OMAP_TIMER_SECURE) | |
646 | return ret; | |
647 | ||
648 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); | |
649 | if (!pdata) { | |
650 | pr_err("%s: No memory for [%s]\n", __func__, oh->name); | |
651 | return -ENOMEM; | |
652 | } | |
653 | ||
654 | /* | |
655 | * Extract the IDs from name field in hwmod database | |
656 | * and use the same for constructing ids' for the | |
657 | * timer devices. In a way, we are avoiding usage of | |
658 | * static variable witin the function to do the same. | |
659 | * CAUTION: We have to be careful and make sure the | |
660 | * name in hwmod database does not change in which case | |
661 | * we might either make corresponding change here or | |
662 | * switch back static variable mechanism. | |
663 | */ | |
664 | sscanf(oh->name, "timer%2d", &id); | |
665 | ||
d1c1691b JH |
666 | if (timer_dev_attr) |
667 | pdata->timer_capability = timer_dev_attr->timer_capability; | |
0dad9fae | 668 | |
c541c15f | 669 | pdev = omap_device_build(name, id, oh, pdata, sizeof(*pdata), |
c16ae1e6 | 670 | NULL, 0, 0); |
c345c8b0 | 671 | |
c541c15f | 672 | if (IS_ERR(pdev)) { |
c345c8b0 TKD |
673 | pr_err("%s: Can't build omap_device for %s: %s.\n", |
674 | __func__, name, oh->name); | |
675 | ret = -EINVAL; | |
676 | } | |
677 | ||
678 | kfree(pdata); | |
679 | ||
680 | return ret; | |
681 | } | |
3392cdd3 TKD |
682 | |
683 | /** | |
684 | * omap2_dm_timer_init - top level regular device initialization | |
685 | * | |
686 | * Uses dedicated hwmod api to parse through hwmod database for | |
687 | * given class name and then build and register the timer device. | |
688 | */ | |
689 | static int __init omap2_dm_timer_init(void) | |
690 | { | |
691 | int ret; | |
692 | ||
9725f445 JH |
693 | /* If dtb is there, the devices will be created dynamically */ |
694 | if (of_have_populated_dt()) | |
695 | return -ENODEV; | |
696 | ||
3392cdd3 TKD |
697 | ret = omap_hwmod_for_each_by_class("timer", omap_timer_init, NULL); |
698 | if (unlikely(ret)) { | |
699 | pr_err("%s: device registration failed.\n", __func__); | |
700 | return -EINVAL; | |
701 | } | |
702 | ||
703 | return 0; | |
704 | } | |
705 | arch_initcall(omap2_dm_timer_init); | |
1fe97c8f VH |
706 | |
707 | /** | |
708 | * omap2_override_clocksource - clocksource override with user configuration | |
709 | * | |
710 | * Allows user to override default clocksource, using kernel parameter | |
711 | * clocksource="gp_timer" (For all OMAP2PLUS architectures) | |
712 | * | |
713 | * Note that, here we are using same standard kernel parameter "clocksource=", | |
714 | * and not introducing any OMAP specific interface. | |
715 | */ | |
716 | static int __init omap2_override_clocksource(char *str) | |
717 | { | |
718 | if (!str) | |
719 | return 0; | |
720 | /* | |
721 | * For OMAP architecture, we only have two options | |
722 | * - sync_32k (default) | |
723 | * - gp_timer (sys_clk based) | |
724 | */ | |
725 | if (!strcmp(str, "gp_timer")) | |
726 | use_gptimer_clksrc = true; | |
727 | ||
728 | return 0; | |
729 | } | |
730 | early_param("clocksource", omap2_override_clocksource); |