2 * DaVinci timer subsystem
4 * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com>
6 * 2007 (c) MontaVista Software, Inc. This file is licensed under
7 * the terms of the GNU General Public License version 2. This program
8 * is licensed "as is" without any warranty of any kind, whether express
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/interrupt.h>
15 #include <linux/clocksource.h>
16 #include <linux/clockchips.h>
18 #include <linux/clk.h>
19 #include <linux/err.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched_clock.h>
23 #include <asm/mach/irq.h>
24 #include <asm/mach/time.h>
26 #include <mach/cputype.h>
27 #include <mach/hardware.h>
28 #include <mach/time.h>
32 static struct clock_event_device clockevent_davinci;
33 static unsigned int davinci_clock_tick_rate;
36 * This driver configures the 2 64-bit count-up timers as 4 independent
37 * 32-bit count-up timers used as follows:
45 /* Timer register offsets */
55 /* Offsets of the 8 compare registers */
65 /* Timer register bitfields */
66 #define TCR_ENAMODE_DISABLE 0x0
67 #define TCR_ENAMODE_ONESHOT 0x1
68 #define TCR_ENAMODE_PERIODIC 0x2
69 #define TCR_ENAMODE_MASK 0x3
71 #define TGCR_TIMMODE_SHIFT 2
72 #define TGCR_TIMMODE_64BIT_GP 0x0
73 #define TGCR_TIMMODE_32BIT_UNCHAINED 0x1
74 #define TGCR_TIMMODE_64BIT_WDOG 0x2
75 #define TGCR_TIMMODE_32BIT_CHAINED 0x3
77 #define TGCR_TIM12RS_SHIFT 0
78 #define TGCR_TIM34RS_SHIFT 1
79 #define TGCR_RESET 0x0
80 #define TGCR_UNRESET 0x1
81 #define TGCR_RESET_MASK 0x3
90 unsigned long tim_off;
91 unsigned long prd_off;
92 unsigned long enamode_shift;
93 struct irqaction irqaction;
95 static struct timer_s timers[];
97 /* values for 'opts' field of struct timer_s */
98 #define TIMER_OPTS_DISABLED 0x01
99 #define TIMER_OPTS_ONESHOT 0x02
100 #define TIMER_OPTS_PERIODIC 0x04
101 #define TIMER_OPTS_STATE_MASK 0x07
103 #define TIMER_OPTS_USE_COMPARE 0x80000000
104 #define USING_COMPARE(t) ((t)->opts & TIMER_OPTS_USE_COMPARE)
106 static char *id_to_name[] = {
107 [T0_BOT] = "timer0_0",
108 [T0_TOP] = "timer0_1",
109 [T1_BOT] = "timer1_0",
110 [T1_TOP] = "timer1_1",
113 static int timer32_config(struct timer_s *t)
116 struct davinci_soc_info *soc_info = &davinci_soc_info;
118 if (USING_COMPARE(t)) {
119 struct davinci_timer_instance *dtip =
120 soc_info->timer_info->timers;
121 int event_timer = ID_TO_TIMER(timers[TID_CLOCKEVENT].id);
124 * Next interrupt should be the current time reg value plus
125 * the new period (using 32-bit unsigned addition/wrapping
126 * to 0 on overflow). This assumes that the clocksource
127 * is setup to count to 2^32-1 before wrapping around to 0.
129 __raw_writel(__raw_readl(t->base + t->tim_off) + t->period,
130 t->base + dtip[event_timer].cmp_off);
132 tcr = __raw_readl(t->base + TCR);
135 tcr &= ~(TCR_ENAMODE_MASK << t->enamode_shift);
136 __raw_writel(tcr, t->base + TCR);
138 /* reset counter to zero, set new period */
139 __raw_writel(0, t->base + t->tim_off);
140 __raw_writel(t->period, t->base + t->prd_off);
142 /* Set enable mode */
143 if (t->opts & TIMER_OPTS_ONESHOT)
144 tcr |= TCR_ENAMODE_ONESHOT << t->enamode_shift;
145 else if (t->opts & TIMER_OPTS_PERIODIC)
146 tcr |= TCR_ENAMODE_PERIODIC << t->enamode_shift;
148 __raw_writel(tcr, t->base + TCR);
153 static inline u32 timer32_read(struct timer_s *t)
155 return __raw_readl(t->base + t->tim_off);
158 static irqreturn_t timer_interrupt(int irq, void *dev_id)
160 struct clock_event_device *evt = &clockevent_davinci;
162 evt->event_handler(evt);
166 /* called when 32-bit counter wraps */
167 static irqreturn_t freerun_interrupt(int irq, void *dev_id)
172 static struct timer_s timers[] = {
174 .name = "clockevent",
175 .opts = TIMER_OPTS_DISABLED,
178 .handler = timer_interrupt,
181 [TID_CLOCKSOURCE] = {
182 .name = "free-run counter",
184 .opts = TIMER_OPTS_PERIODIC,
187 .handler = freerun_interrupt,
192 static void __init timer_init(void)
194 struct davinci_soc_info *soc_info = &davinci_soc_info;
195 struct davinci_timer_instance *dtip = soc_info->timer_info->timers;
196 void __iomem *base[2];
199 /* Global init of each 64-bit timer as a whole */
203 base[i] = ioremap(dtip[i].base, SZ_4K);
204 if (WARN_ON(!base[i]))
207 /* Disabled, Internal clock source */
208 __raw_writel(0, base[i] + TCR);
210 /* reset both timers, no pre-scaler for timer34 */
212 __raw_writel(tgcr, base[i] + TGCR);
214 /* Set both timers to unchained 32-bit */
215 tgcr = TGCR_TIMMODE_32BIT_UNCHAINED << TGCR_TIMMODE_SHIFT;
216 __raw_writel(tgcr, base[i] + TGCR);
219 tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
220 (TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
221 __raw_writel(tgcr, base[i] + TGCR);
223 /* Init both counters to zero */
224 __raw_writel(0, base[i] + TIM12);
225 __raw_writel(0, base[i] + TIM34);
228 /* Init of each timer as a 32-bit timer */
229 for (i=0; i< ARRAY_SIZE(timers); i++) {
230 struct timer_s *t = &timers[i];
231 int timer = ID_TO_TIMER(t->id);
234 t->base = base[timer];
238 if (IS_TIMER_BOT(t->id)) {
239 t->enamode_shift = 6;
242 irq = dtip[timer].bottom_irq;
244 t->enamode_shift = 22;
247 irq = dtip[timer].top_irq;
250 /* Register interrupt */
251 t->irqaction.name = t->name;
252 t->irqaction.dev_id = (void *)t;
254 if (t->irqaction.handler != NULL) {
255 irq = USING_COMPARE(t) ? dtip[i].cmp_irq : irq;
256 setup_irq(irq, &t->irqaction);
264 static u64 read_cycles(struct clocksource *cs)
266 struct timer_s *t = &timers[TID_CLOCKSOURCE];
268 return (cycles_t)timer32_read(t);
271 static struct clocksource clocksource_davinci = {
274 .mask = CLOCKSOURCE_MASK(32),
275 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
279 * Overwrite weak default sched_clock with something more precise
281 static u64 notrace davinci_read_sched_clock(void)
283 return timer32_read(&timers[TID_CLOCKSOURCE]);
289 static int davinci_set_next_event(unsigned long cycles,
290 struct clock_event_device *evt)
292 struct timer_s *t = &timers[TID_CLOCKEVENT];
299 static int davinci_shutdown(struct clock_event_device *evt)
301 struct timer_s *t = &timers[TID_CLOCKEVENT];
303 t->opts &= ~TIMER_OPTS_STATE_MASK;
304 t->opts |= TIMER_OPTS_DISABLED;
308 static int davinci_set_oneshot(struct clock_event_device *evt)
310 struct timer_s *t = &timers[TID_CLOCKEVENT];
312 t->opts &= ~TIMER_OPTS_STATE_MASK;
313 t->opts |= TIMER_OPTS_ONESHOT;
317 static int davinci_set_periodic(struct clock_event_device *evt)
319 struct timer_s *t = &timers[TID_CLOCKEVENT];
321 t->period = davinci_clock_tick_rate / (HZ);
322 t->opts &= ~TIMER_OPTS_STATE_MASK;
323 t->opts |= TIMER_OPTS_PERIODIC;
328 static struct clock_event_device clockevent_davinci = {
329 .features = CLOCK_EVT_FEAT_PERIODIC |
330 CLOCK_EVT_FEAT_ONESHOT,
331 .set_next_event = davinci_set_next_event,
332 .set_state_shutdown = davinci_shutdown,
333 .set_state_periodic = davinci_set_periodic,
334 .set_state_oneshot = davinci_set_oneshot,
338 void __init davinci_timer_init(void)
340 struct clk *timer_clk;
341 struct davinci_soc_info *soc_info = &davinci_soc_info;
342 unsigned int clockevent_id;
343 unsigned int clocksource_id;
346 clockevent_id = soc_info->timer_info->clockevent_id;
347 clocksource_id = soc_info->timer_info->clocksource_id;
349 timers[TID_CLOCKEVENT].id = clockevent_id;
350 timers[TID_CLOCKSOURCE].id = clocksource_id;
353 * If using same timer for both clock events & clocksource,
354 * a compare register must be used to generate an event interrupt.
355 * This is equivalent to a oneshot timer only (not periodic).
357 if (clockevent_id == clocksource_id) {
358 struct davinci_timer_instance *dtip =
359 soc_info->timer_info->timers;
360 int event_timer = ID_TO_TIMER(clockevent_id);
362 /* Only bottom timers can use compare regs */
363 if (IS_TIMER_TOP(clockevent_id))
364 pr_warn("%s: Invalid use of system timers. Results unpredictable.\n",
366 else if ((dtip[event_timer].cmp_off == 0)
367 || (dtip[event_timer].cmp_irq == 0))
368 pr_warn("%s: Invalid timer instance setup. Results unpredictable.\n",
371 timers[TID_CLOCKEVENT].opts |= TIMER_OPTS_USE_COMPARE;
372 clockevent_davinci.features = CLOCK_EVT_FEAT_ONESHOT;
376 timer_clk = clk_get(NULL, "timer0");
377 BUG_ON(IS_ERR(timer_clk));
378 clk_prepare_enable(timer_clk);
383 davinci_clock_tick_rate = clk_get_rate(timer_clk);
385 /* setup clocksource */
386 clocksource_davinci.name = id_to_name[clocksource_id];
387 if (clocksource_register_hz(&clocksource_davinci,
388 davinci_clock_tick_rate))
389 pr_err("%s: can't register clocksource!\n",
390 clocksource_davinci.name);
392 sched_clock_register(davinci_read_sched_clock, 32,
393 davinci_clock_tick_rate);
395 /* setup clockevent */
396 clockevent_davinci.name = id_to_name[timers[TID_CLOCKEVENT].id];
398 clockevent_davinci.cpumask = cpumask_of(0);
399 clockevents_config_and_register(&clockevent_davinci,
400 davinci_clock_tick_rate, 1, 0xfffffffe);
402 for (i=0; i< ARRAY_SIZE(timers); i++)
403 timer32_config(&timers[i]);