• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * linux/arch/arm/mach-omap2/timer.c
3  *
4  * OMAP2 GP timer support.
5  *
6  * Copyright (C) 2009 Nokia Corporation
7  *
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:
13  * Copyright (C) 2005 Nokia Corporation
14  * Author: Paul Mundt <paul.mundt@nokia.com>
15  *         Juha Yrjölä <juha.yrjola@nokia.com>
16  * OMAP Dual-mode timer framework support by Timo Teras
17  *
18  * Some parts based off of TI's 24xx code:
19  *
20  * Copyright (C) 2004-2009 Texas Instruments, Inc.
21  *
22  * Roughly modelled after the OMAP1 MPU timer code.
23  * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
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>
33 #include <linux/clk.h>
34 #include <linux/delay.h>
35 #include <linux/irq.h>
36 #include <linux/clocksource.h>
37 #include <linux/clockchips.h>
38 #include <linux/slab.h>
39 #include <linux/of.h>
40 #include <linux/of_address.h>
41 #include <linux/of_irq.h>
42 #include <linux/platform_device.h>
43 #include <linux/platform_data/dmtimer-omap.h>
44 #include <linux/sched_clock.h>
45 
46 #include <asm/mach/time.h>
47 
48 #include "omap_hwmod.h"
49 #include "omap_device.h"
50 #include <plat/counter-32k.h>
51 #include <clocksource/timer-ti-dm.h>
52 
53 #include "soc.h"
54 #include "common.h"
55 #include "control.h"
56 #include "powerdomain.h"
57 #include "omap-secure.h"
58 
59 #define REALTIME_COUNTER_BASE				0x48243200
60 #define INCREMENTER_NUMERATOR_OFFSET			0x10
61 #define INCREMENTER_DENUMERATOR_RELOAD_OFFSET		0x14
62 #define NUMERATOR_DENUMERATOR_MASK			0xfffff000
63 
64 /* Clockevent code */
65 
66 /* Clockevent hwmod for am335x and am437x suspend */
67 static struct omap_hwmod *clockevent_gpt_hwmod;
68 
69 /* Clockesource hwmod for am437x suspend */
70 static struct omap_hwmod *clocksource_gpt_hwmod;
71 
72 struct dmtimer_clockevent {
73 	struct clock_event_device dev;
74 	struct omap_dm_timer timer;
75 };
76 
77 static struct dmtimer_clockevent clockevent;
78 
to_dmtimer(struct clock_event_device * clockevent)79 static struct omap_dm_timer *to_dmtimer(struct clock_event_device *clockevent)
80 {
81 	struct dmtimer_clockevent *clkevt =
82 		container_of(clockevent, struct dmtimer_clockevent, dev);
83 	struct omap_dm_timer *timer = &clkevt->timer;
84 
85 	return timer;
86 }
87 
88 #ifdef CONFIG_SOC_HAS_REALTIME_COUNTER
89 static unsigned long arch_timer_freq;
90 
set_cntfreq(void)91 void set_cntfreq(void)
92 {
93 	omap_smc1(OMAP5_DRA7_MON_SET_CNTFRQ_INDEX, arch_timer_freq);
94 }
95 #endif
96 
omap2_gp_timer_interrupt(int irq,void * dev_id)97 static irqreturn_t omap2_gp_timer_interrupt(int irq, void *dev_id)
98 {
99 	struct dmtimer_clockevent *clkevt = dev_id;
100 	struct clock_event_device *evt = &clkevt->dev;
101 	struct omap_dm_timer *timer = &clkevt->timer;
102 
103 	__omap_dm_timer_write_status(timer, OMAP_TIMER_INT_OVERFLOW);
104 	evt->event_handler(evt);
105 	return IRQ_HANDLED;
106 }
107 
omap2_gp_timer_set_next_event(unsigned long cycles,struct clock_event_device * evt)108 static int omap2_gp_timer_set_next_event(unsigned long cycles,
109 					 struct clock_event_device *evt)
110 {
111 	struct omap_dm_timer *timer = to_dmtimer(evt);
112 
113 	__omap_dm_timer_load_start(timer, OMAP_TIMER_CTRL_ST,
114 				   0xffffffff - cycles, OMAP_TIMER_POSTED);
115 
116 	return 0;
117 }
118 
omap2_gp_timer_shutdown(struct clock_event_device * evt)119 static int omap2_gp_timer_shutdown(struct clock_event_device *evt)
120 {
121 	struct omap_dm_timer *timer = to_dmtimer(evt);
122 
123 	__omap_dm_timer_stop(timer, OMAP_TIMER_POSTED, timer->rate);
124 
125 	return 0;
126 }
127 
omap2_gp_timer_set_periodic(struct clock_event_device * evt)128 static int omap2_gp_timer_set_periodic(struct clock_event_device *evt)
129 {
130 	struct omap_dm_timer *timer = to_dmtimer(evt);
131 	u32 period;
132 
133 	__omap_dm_timer_stop(timer, OMAP_TIMER_POSTED, timer->rate);
134 
135 	period = timer->rate / HZ;
136 	period -= 1;
137 	/* Looks like we need to first set the load value separately */
138 	__omap_dm_timer_write(timer, OMAP_TIMER_LOAD_REG, 0xffffffff - period,
139 			      OMAP_TIMER_POSTED);
140 	__omap_dm_timer_load_start(timer,
141 				   OMAP_TIMER_CTRL_AR | OMAP_TIMER_CTRL_ST,
142 				   0xffffffff - period, OMAP_TIMER_POSTED);
143 	return 0;
144 }
145 
omap_clkevt_idle(struct clock_event_device * unused)146 static void omap_clkevt_idle(struct clock_event_device *unused)
147 {
148 	if (!clockevent_gpt_hwmod)
149 		return;
150 
151 	omap_hwmod_idle(clockevent_gpt_hwmod);
152 }
153 
omap_clkevt_unidle(struct clock_event_device * evt)154 static void omap_clkevt_unidle(struct clock_event_device *evt)
155 {
156 	struct omap_dm_timer *timer = to_dmtimer(evt);
157 
158 	if (!clockevent_gpt_hwmod)
159 		return;
160 
161 	omap_hwmod_enable(clockevent_gpt_hwmod);
162 	__omap_dm_timer_int_enable(timer, OMAP_TIMER_INT_OVERFLOW);
163 }
164 
165 static const struct of_device_id omap_timer_match[] __initconst = {
166 	{ .compatible = "ti,omap2420-timer", },
167 	{ .compatible = "ti,omap3430-timer", },
168 	{ .compatible = "ti,omap4430-timer", },
169 	{ .compatible = "ti,omap5430-timer", },
170 	{ .compatible = "ti,dm814-timer", },
171 	{ .compatible = "ti,dm816-timer", },
172 	{ .compatible = "ti,am335x-timer", },
173 	{ .compatible = "ti,am335x-timer-1ms", },
174 	{ }
175 };
176 
omap_timer_add_disabled_property(struct device_node * np)177 static int omap_timer_add_disabled_property(struct device_node *np)
178 {
179 	struct property *prop;
180 
181 	prop = kzalloc(sizeof(*prop), GFP_KERNEL);
182 	if (!prop)
183 		return -ENOMEM;
184 
185 	prop->name = "status";
186 	prop->value = "disabled";
187 	prop->length = strlen(prop->value);
188 
189 	return of_add_property(np, prop);
190 }
191 
omap_timer_update_dt(struct device_node * np)192 static int omap_timer_update_dt(struct device_node *np)
193 {
194 	int error = 0;
195 
196 	if (!of_device_is_compatible(np, "ti,omap-counter32k")) {
197 		error = omap_timer_add_disabled_property(np);
198 		if (error)
199 			return error;
200 	}
201 
202 	/* No parent interconnect target module configured? */
203 	if (of_get_property(np, "ti,hwmods", NULL))
204 		return error;
205 
206 	/* Tag parent interconnect target module disabled */
207 	error = omap_timer_add_disabled_property(np->parent);
208 	if (error)
209 		return error;
210 
211 	return 0;
212 }
213 
214 /**
215  * omap_get_timer_dt - get a timer using device-tree
216  * @match	- device-tree match structure for matching a device type
217  * @property	- optional timer property to match
218  *
219  * Helper function to get a timer during early boot using device-tree for use
220  * as kernel system timer. Optionally, the property argument can be used to
221  * select a timer with a specific property. Once a timer is found then mark
222  * the timer node in device-tree as disabled, to prevent the kernel from
223  * registering this timer as a platform device and so no one else can use it.
224  */
omap_get_timer_dt(const struct of_device_id * match,const char * property)225 static struct device_node * __init omap_get_timer_dt(const struct of_device_id *match,
226 						     const char *property)
227 {
228 	struct device_node *np;
229 	int error;
230 
231 	for_each_matching_node(np, match) {
232 		if (!of_device_is_available(np))
233 			continue;
234 
235 		if (property && !of_get_property(np, property, NULL))
236 			continue;
237 
238 		if (!property && (of_get_property(np, "ti,timer-alwon", NULL) ||
239 				  of_get_property(np, "ti,timer-dsp", NULL) ||
240 				  of_get_property(np, "ti,timer-pwm", NULL) ||
241 				  of_get_property(np, "ti,timer-secure", NULL)))
242 			continue;
243 
244 		error = omap_timer_update_dt(np);
245 		WARN(error, "%s: Could not update dt: %i\n", __func__, error);
246 
247 		return np;
248 	}
249 
250 	return NULL;
251 }
252 
253 /**
254  * omap_dmtimer_init - initialisation function when device tree is used
255  *
256  * For secure OMAP3/DRA7xx devices, timers with device type "timer-secure"
257  * cannot be used by the kernel as they are reserved. Therefore, to prevent the
258  * kernel registering these devices remove them dynamically from the device
259  * tree on boot.
260  */
omap_dmtimer_init(void)261 static void __init omap_dmtimer_init(void)
262 {
263 	struct device_node *np;
264 
265 	if (!cpu_is_omap34xx() && !soc_is_dra7xx())
266 		return;
267 
268 	/* If we are a secure device, remove any secure timer nodes */
269 	if ((omap_type() != OMAP2_DEVICE_TYPE_GP)) {
270 		np = omap_get_timer_dt(omap_timer_match, "ti,timer-secure");
271 		of_node_put(np);
272 	}
273 }
274 
275 /**
276  * omap_dm_timer_get_errata - get errata flags for a timer
277  *
278  * Get the timer errata flags that are specific to the OMAP device being used.
279  */
omap_dm_timer_get_errata(void)280 static u32 __init omap_dm_timer_get_errata(void)
281 {
282 	if (cpu_is_omap24xx())
283 		return 0;
284 
285 	return OMAP_TIMER_ERRATA_I103_I767;
286 }
287 
omap_dm_timer_init_one(struct omap_dm_timer * timer,const char * fck_source,const char * property,const char ** timer_name,int posted)288 static int __init omap_dm_timer_init_one(struct omap_dm_timer *timer,
289 					 const char *fck_source,
290 					 const char *property,
291 					 const char **timer_name,
292 					 int posted)
293 {
294 	const char *oh_name = NULL;
295 	struct device_node *np;
296 	struct omap_hwmod *oh;
297 	struct clk *src;
298 	int r = 0;
299 
300 	np = omap_get_timer_dt(omap_timer_match, property);
301 	if (!np)
302 		return -ENODEV;
303 
304 	of_property_read_string_index(np, "ti,hwmods", 0, &oh_name);
305 	if (!oh_name) {
306 		of_property_read_string_index(np->parent, "ti,hwmods", 0,
307 					      &oh_name);
308 		if (!oh_name)
309 			return -ENODEV;
310 	}
311 
312 	timer->irq = irq_of_parse_and_map(np, 0);
313 	if (!timer->irq)
314 		return -ENXIO;
315 
316 	timer->io_base = of_iomap(np, 0);
317 
318 	timer->fclk = of_clk_get_by_name(np, "fck");
319 
320 	of_node_put(np);
321 
322 	oh = omap_hwmod_lookup(oh_name);
323 	if (!oh)
324 		return -ENODEV;
325 
326 	*timer_name = oh->name;
327 
328 	if (!timer->io_base)
329 		return -ENXIO;
330 
331 	omap_hwmod_setup_one(oh_name);
332 
333 	/* After the dmtimer is using hwmod these clocks won't be needed */
334 	if (IS_ERR_OR_NULL(timer->fclk))
335 		timer->fclk = clk_get(NULL, omap_hwmod_get_main_clk(oh));
336 	if (IS_ERR(timer->fclk))
337 		return PTR_ERR(timer->fclk);
338 
339 	src = clk_get(NULL, fck_source);
340 	if (IS_ERR(src))
341 		return PTR_ERR(src);
342 
343 	WARN(clk_set_parent(timer->fclk, src) < 0,
344 	     "Cannot set timer parent clock, no PLL clock driver?");
345 
346 	clk_put(src);
347 
348 	omap_hwmod_enable(oh);
349 	__omap_dm_timer_init_regs(timer);
350 
351 	if (posted)
352 		__omap_dm_timer_enable_posted(timer);
353 
354 	/* Check that the intended posted configuration matches the actual */
355 	if (posted != timer->posted)
356 		return -EINVAL;
357 
358 	timer->rate = clk_get_rate(timer->fclk);
359 	timer->reserved = 1;
360 
361 	return r;
362 }
363 
364 #if !defined(CONFIG_SMP) && defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST)
tick_broadcast(const struct cpumask * mask)365 void tick_broadcast(const struct cpumask *mask)
366 {
367 }
368 #endif
369 
dmtimer_clkevt_init_common(struct dmtimer_clockevent * clkevt,int gptimer_id,const char * fck_source,unsigned int features,const struct cpumask * cpumask,const char * property,int rating,const char * name)370 static void __init dmtimer_clkevt_init_common(struct dmtimer_clockevent *clkevt,
371 					      int gptimer_id,
372 					      const char *fck_source,
373 					      unsigned int features,
374 					      const struct cpumask *cpumask,
375 					      const char *property,
376 					      int rating, const char *name)
377 {
378 	struct omap_dm_timer *timer = &clkevt->timer;
379 	int res;
380 
381 	timer->id = gptimer_id;
382 	timer->errata = omap_dm_timer_get_errata();
383 	clkevt->dev.features = features;
384 	clkevt->dev.rating = rating;
385 	clkevt->dev.set_next_event = omap2_gp_timer_set_next_event;
386 	clkevt->dev.set_state_shutdown = omap2_gp_timer_shutdown;
387 	clkevt->dev.set_state_periodic = omap2_gp_timer_set_periodic;
388 	clkevt->dev.set_state_oneshot = omap2_gp_timer_shutdown;
389 	clkevt->dev.tick_resume = omap2_gp_timer_shutdown;
390 
391 	/*
392 	 * For clock-event timers we never read the timer counter and
393 	 * so we are not impacted by errata i103 and i767. Therefore,
394 	 * we can safely ignore this errata for clock-event timers.
395 	 */
396 	__omap_dm_timer_override_errata(timer, OMAP_TIMER_ERRATA_I103_I767);
397 
398 	res = omap_dm_timer_init_one(timer, fck_source, property,
399 				     &clkevt->dev.name, OMAP_TIMER_POSTED);
400 	BUG_ON(res);
401 
402 	clkevt->dev.cpumask = cpumask;
403 	clkevt->dev.irq = omap_dm_timer_get_irq(timer);
404 
405 	if (request_irq(clkevt->dev.irq, omap2_gp_timer_interrupt,
406 			IRQF_TIMER | IRQF_IRQPOLL, name, clkevt))
407 		pr_err("Failed to request irq %d (gp_timer)\n", clkevt->dev.irq);
408 
409 	__omap_dm_timer_int_enable(timer, OMAP_TIMER_INT_OVERFLOW);
410 
411 	if (soc_is_am33xx() || soc_is_am43xx()) {
412 		clkevt->dev.suspend = omap_clkevt_idle;
413 		clkevt->dev.resume = omap_clkevt_unidle;
414 
415 		clockevent_gpt_hwmod =
416 			omap_hwmod_lookup(clkevt->dev.name);
417 	}
418 
419 	pr_info("OMAP clockevent source: %s at %lu Hz\n", clkevt->dev.name,
420 		timer->rate);
421 }
422 
423 /* Clocksource code */
424 static struct omap_dm_timer clksrc;
425 static bool use_gptimer_clksrc __initdata;
426 
427 /*
428  * clocksource
429  */
clocksource_read_cycles(struct clocksource * cs)430 static u64 clocksource_read_cycles(struct clocksource *cs)
431 {
432 	return (u64)__omap_dm_timer_read_counter(&clksrc,
433 						     OMAP_TIMER_NONPOSTED);
434 }
435 
436 static struct clocksource clocksource_gpt = {
437 	.rating		= 300,
438 	.read		= clocksource_read_cycles,
439 	.mask		= CLOCKSOURCE_MASK(32),
440 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
441 };
442 
dmtimer_read_sched_clock(void)443 static u64 notrace dmtimer_read_sched_clock(void)
444 {
445 	if (clksrc.reserved)
446 		return __omap_dm_timer_read_counter(&clksrc,
447 						    OMAP_TIMER_NONPOSTED);
448 
449 	return 0;
450 }
451 
452 static const struct of_device_id omap_counter_match[] __initconst = {
453 	{ .compatible = "ti,omap-counter32k", },
454 	{ }
455 };
456 
457 /* Setup free-running counter for clocksource */
omap2_sync32k_clocksource_init(void)458 static int __init __maybe_unused omap2_sync32k_clocksource_init(void)
459 {
460 	int ret;
461 	struct device_node *np = NULL;
462 	struct omap_hwmod *oh;
463 	const char *oh_name = "counter_32k";
464 
465 	/*
466 	 * See if the 32kHz counter is supported.
467 	 */
468 	np = omap_get_timer_dt(omap_counter_match, NULL);
469 	if (!np)
470 		return -ENODEV;
471 
472 	of_property_read_string_index(np->parent, "ti,hwmods", 0, &oh_name);
473 	if (!oh_name) {
474 		of_property_read_string_index(np, "ti,hwmods", 0, &oh_name);
475 		if (!oh_name)
476 			return -ENODEV;
477 	}
478 
479 	/*
480 	 * First check hwmod data is available for sync32k counter
481 	 */
482 	oh = omap_hwmod_lookup(oh_name);
483 	if (!oh || oh->slaves_cnt == 0)
484 		return -ENODEV;
485 
486 	omap_hwmod_setup_one(oh_name);
487 
488 	ret = omap_hwmod_enable(oh);
489 	if (ret) {
490 		pr_warn("%s: failed to enable counter_32k module (%d)\n",
491 							__func__, ret);
492 		return ret;
493 	}
494 
495 	return ret;
496 }
497 
498 static unsigned int omap2_gptimer_clksrc_load;
499 
omap2_gptimer_clksrc_suspend(struct clocksource * unused)500 static void omap2_gptimer_clksrc_suspend(struct clocksource *unused)
501 {
502 	omap2_gptimer_clksrc_load =
503 		__omap_dm_timer_read_counter(&clksrc, OMAP_TIMER_NONPOSTED);
504 
505 	omap_hwmod_idle(clocksource_gpt_hwmod);
506 }
507 
omap2_gptimer_clksrc_resume(struct clocksource * unused)508 static void omap2_gptimer_clksrc_resume(struct clocksource *unused)
509 {
510 	omap_hwmod_enable(clocksource_gpt_hwmod);
511 
512 	__omap_dm_timer_load_start(&clksrc,
513 				   OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR,
514 				   omap2_gptimer_clksrc_load,
515 				   OMAP_TIMER_NONPOSTED);
516 }
517 
omap2_gptimer_clocksource_init(int gptimer_id,const char * fck_source,const char * property)518 static void __init omap2_gptimer_clocksource_init(int gptimer_id,
519 						  const char *fck_source,
520 						  const char *property)
521 {
522 	int res;
523 
524 	clksrc.id = gptimer_id;
525 	clksrc.errata = omap_dm_timer_get_errata();
526 
527 	res = omap_dm_timer_init_one(&clksrc, fck_source, property,
528 				     &clocksource_gpt.name,
529 				     OMAP_TIMER_NONPOSTED);
530 
531 	if (soc_is_am43xx()) {
532 		clocksource_gpt.suspend = omap2_gptimer_clksrc_suspend;
533 		clocksource_gpt.resume = omap2_gptimer_clksrc_resume;
534 
535 		clocksource_gpt_hwmod =
536 			omap_hwmod_lookup(clocksource_gpt.name);
537 	}
538 
539 	BUG_ON(res);
540 
541 	__omap_dm_timer_load_start(&clksrc,
542 				   OMAP_TIMER_CTRL_ST | OMAP_TIMER_CTRL_AR, 0,
543 				   OMAP_TIMER_NONPOSTED);
544 	sched_clock_register(dmtimer_read_sched_clock, 32, clksrc.rate);
545 
546 	if (clocksource_register_hz(&clocksource_gpt, clksrc.rate))
547 		pr_err("Could not register clocksource %s\n",
548 			clocksource_gpt.name);
549 	else
550 		pr_info("OMAP clocksource: %s at %lu Hz\n",
551 			clocksource_gpt.name, clksrc.rate);
552 }
553 
__omap_sync32k_timer_init(int clkev_nr,const char * clkev_src,const char * clkev_prop,int clksrc_nr,const char * clksrc_src,const char * clksrc_prop,bool gptimer)554 static void __init __omap_sync32k_timer_init(int clkev_nr, const char *clkev_src,
555 		const char *clkev_prop, int clksrc_nr, const char *clksrc_src,
556 		const char *clksrc_prop, bool gptimer)
557 {
558 	omap_clk_init();
559 	omap_dmtimer_init();
560 	dmtimer_clkevt_init_common(&clockevent, clkev_nr, clkev_src,
561 				   CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
562 				   cpu_possible_mask, clkev_prop, 300, "clockevent");
563 	clockevents_config_and_register(&clockevent.dev, clockevent.timer.rate,
564 					3, /* Timer internal resynch latency */
565 					0xffffffff);
566 
567 	/* Enable the use of clocksource="gp_timer" kernel parameter */
568 	if (use_gptimer_clksrc || gptimer)
569 		omap2_gptimer_clocksource_init(clksrc_nr, clksrc_src,
570 						clksrc_prop);
571 	else
572 		omap2_sync32k_clocksource_init();
573 }
574 
omap_init_time(void)575 void __init omap_init_time(void)
576 {
577 	__omap_sync32k_timer_init(1, "timer_32k_ck", "ti,timer-alwon",
578 			2, "timer_sys_ck", NULL, false);
579 
580 	timer_probe();
581 }
582 
583 #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_SOC_AM43XX)
omap3_secure_sync32k_timer_init(void)584 void __init omap3_secure_sync32k_timer_init(void)
585 {
586 	__omap_sync32k_timer_init(12, "secure_32k_fck", "ti,timer-secure",
587 			2, "timer_sys_ck", NULL, false);
588 
589 	timer_probe();
590 }
591 #endif /* CONFIG_ARCH_OMAP3 */
592 
593 #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_SOC_AM33XX) || \
594 	defined(CONFIG_SOC_AM43XX)
omap3_gptimer_timer_init(void)595 void __init omap3_gptimer_timer_init(void)
596 {
597 	__omap_sync32k_timer_init(2, "timer_sys_ck", NULL,
598 			1, "timer_sys_ck", "ti,timer-alwon", true);
599 	if (of_have_populated_dt())
600 		timer_probe();
601 }
602 #endif
603 
604 #if defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_SOC_OMAP5) ||		\
605 	defined(CONFIG_SOC_DRA7XX)
omap4_sync32k_timer_init(void)606 static void __init omap4_sync32k_timer_init(void)
607 {
608 	__omap_sync32k_timer_init(1, "timer_32k_ck", "ti,timer-alwon",
609 			2, "sys_clkin_ck", NULL, false);
610 }
611 
omap4_local_timer_init(void)612 void __init omap4_local_timer_init(void)
613 {
614 	omap4_sync32k_timer_init();
615 	timer_probe();
616 }
617 #endif
618 
619 #if defined(CONFIG_SOC_OMAP5) || defined(CONFIG_SOC_DRA7XX)
620 
621 /*
622  * The realtime counter also called master counter, is a free-running
623  * counter, which is related to real time. It produces the count used
624  * by the CPU local timer peripherals in the MPU cluster. The timer counts
625  * at a rate of 6.144 MHz. Because the device operates on different clocks
626  * in different power modes, the master counter shifts operation between
627  * clocks, adjusting the increment per clock in hardware accordingly to
628  * maintain a constant count rate.
629  */
realtime_counter_init(void)630 static void __init realtime_counter_init(void)
631 {
632 #ifdef CONFIG_SOC_HAS_REALTIME_COUNTER
633 	void __iomem *base;
634 	static struct clk *sys_clk;
635 	unsigned long rate;
636 	unsigned int reg;
637 	unsigned long long num, den;
638 
639 	base = ioremap(REALTIME_COUNTER_BASE, SZ_32);
640 	if (!base) {
641 		pr_err("%s: ioremap failed\n", __func__);
642 		return;
643 	}
644 	sys_clk = clk_get(NULL, "sys_clkin");
645 	if (IS_ERR(sys_clk)) {
646 		pr_err("%s: failed to get system clock handle\n", __func__);
647 		iounmap(base);
648 		return;
649 	}
650 
651 	rate = clk_get_rate(sys_clk);
652 	clk_put(sys_clk);
653 
654 	if (soc_is_dra7xx()) {
655 		/*
656 		 * Errata i856 says the 32.768KHz crystal does not start at
657 		 * power on, so the CPU falls back to an emulated 32KHz clock
658 		 * based on sysclk / 610 instead. This causes the master counter
659 		 * frequency to not be 6.144MHz but at sysclk / 610 * 375 / 2
660 		 * (OR sysclk * 75 / 244)
661 		 *
662 		 * This affects at least the DRA7/AM572x 1.0, 1.1 revisions.
663 		 * Of course any board built without a populated 32.768KHz
664 		 * crystal would also need this fix even if the CPU is fixed
665 		 * later.
666 		 *
667 		 * Either case can be detected by using the two speedselect bits
668 		 * If they are not 0, then the 32.768KHz clock driving the
669 		 * coarse counter that corrects the fine counter every time it
670 		 * ticks is actually rate/610 rather than 32.768KHz and we
671 		 * should compensate to avoid the 570ppm (at 20MHz, much worse
672 		 * at other rates) too fast system time.
673 		 */
674 		reg = omap_ctrl_readl(DRA7_CTRL_CORE_BOOTSTRAP);
675 		if (reg & DRA7_SPEEDSELECT_MASK) {
676 			num = 75;
677 			den = 244;
678 			goto sysclk1_based;
679 		}
680 	}
681 
682 	/* Numerator/denumerator values refer TRM Realtime Counter section */
683 	switch (rate) {
684 	case 12000000:
685 		num = 64;
686 		den = 125;
687 		break;
688 	case 13000000:
689 		num = 768;
690 		den = 1625;
691 		break;
692 	case 19200000:
693 		num = 8;
694 		den = 25;
695 		break;
696 	case 20000000:
697 		num = 192;
698 		den = 625;
699 		break;
700 	case 26000000:
701 		num = 384;
702 		den = 1625;
703 		break;
704 	case 27000000:
705 		num = 256;
706 		den = 1125;
707 		break;
708 	case 38400000:
709 	default:
710 		/* Program it for 38.4 MHz */
711 		num = 4;
712 		den = 25;
713 		break;
714 	}
715 
716 sysclk1_based:
717 	/* Program numerator and denumerator registers */
718 	reg = readl_relaxed(base + INCREMENTER_NUMERATOR_OFFSET) &
719 			NUMERATOR_DENUMERATOR_MASK;
720 	reg |= num;
721 	writel_relaxed(reg, base + INCREMENTER_NUMERATOR_OFFSET);
722 
723 	reg = readl_relaxed(base + INCREMENTER_DENUMERATOR_RELOAD_OFFSET) &
724 			NUMERATOR_DENUMERATOR_MASK;
725 	reg |= den;
726 	writel_relaxed(reg, base + INCREMENTER_DENUMERATOR_RELOAD_OFFSET);
727 
728 	arch_timer_freq = DIV_ROUND_UP_ULL(rate * num, den);
729 	set_cntfreq();
730 
731 	iounmap(base);
732 #endif
733 }
734 
omap5_realtime_timer_init(void)735 void __init omap5_realtime_timer_init(void)
736 {
737 	omap4_sync32k_timer_init();
738 	realtime_counter_init();
739 
740 	timer_probe();
741 }
742 #endif /* CONFIG_SOC_OMAP5 || CONFIG_SOC_DRA7XX */
743 
744 /**
745  * omap2_override_clocksource - clocksource override with user configuration
746  *
747  * Allows user to override default clocksource, using kernel parameter
748  *   clocksource="gp_timer"	(For all OMAP2PLUS architectures)
749  *
750  * Note that, here we are using same standard kernel parameter "clocksource=",
751  * and not introducing any OMAP specific interface.
752  */
omap2_override_clocksource(char * str)753 static int __init omap2_override_clocksource(char *str)
754 {
755 	if (!str)
756 		return 0;
757 	/*
758 	 * For OMAP architecture, we only have two options
759 	 *    - sync_32k (default)
760 	 *    - gp_timer (sys_clk based)
761 	 */
762 	if (!strcmp(str, "gp_timer"))
763 		use_gptimer_clksrc = true;
764 
765 	return 0;
766 }
767 early_param("clocksource", omap2_override_clocksource);
768