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>
17 #include <linux/spinlock.h>
20 #include <mach/hardware.h>
21 #include <asm/system.h>
23 #include <asm/mach/irq.h>
24 #include <asm/mach/time.h>
25 #include <asm/errno.h>
28 static struct clock_event_device clockevent_davinci;
30 #define DAVINCI_TIMER0_BASE (IO_PHYS + 0x21400)
31 #define DAVINCI_TIMER1_BASE (IO_PHYS + 0x21800)
32 #define DAVINCI_WDOG_BASE (IO_PHYS + 0x21C00)
35 T0_BOT = 0, T0_TOP, T1_BOT, T1_TOP, NUM_TIMERS,
38 #define IS_TIMER1(id) (id & 0x2)
39 #define IS_TIMER0(id) (!IS_TIMER1(id))
40 #define IS_TIMER_TOP(id) ((id & 0x1))
41 #define IS_TIMER_BOT(id) (!IS_TIMER_TOP(id))
43 static int timer_irqs[NUM_TIMERS] = {
51 * This driver configures the 2 64-bit count-up timers as 4 independent
52 * 32-bit count-up timers used as follows:
54 * T0_BOT: Timer 0, bottom: clockevent source for hrtimers
55 * T0_TOP: Timer 0, top : clocksource for generic timekeeping
56 * T1_BOT: Timer 1, bottom: (used by DSP in TI DSPLink code)
57 * T1_TOP: Timer 1, top : <unused>
59 #define TID_CLOCKEVENT T0_BOT
60 #define TID_CLOCKSOURCE T0_TOP
62 /* Timer register offsets */
72 /* Timer register bitfields */
73 #define TCR_ENAMODE_DISABLE 0x0
74 #define TCR_ENAMODE_ONESHOT 0x1
75 #define TCR_ENAMODE_PERIODIC 0x2
76 #define TCR_ENAMODE_MASK 0x3
78 #define TGCR_TIMMODE_SHIFT 2
79 #define TGCR_TIMMODE_64BIT_GP 0x0
80 #define TGCR_TIMMODE_32BIT_UNCHAINED 0x1
81 #define TGCR_TIMMODE_64BIT_WDOG 0x2
82 #define TGCR_TIMMODE_32BIT_CHAINED 0x3
84 #define TGCR_TIM12RS_SHIFT 0
85 #define TGCR_TIM34RS_SHIFT 1
86 #define TGCR_RESET 0x0
87 #define TGCR_UNRESET 0x1
88 #define TGCR_RESET_MASK 0x3
90 #define WDTCR_WDEN_SHIFT 14
91 #define WDTCR_WDEN_DISABLE 0x0
92 #define WDTCR_WDEN_ENABLE 0x1
93 #define WDTCR_WDKEY_SHIFT 16
94 #define WDTCR_WDKEY_SEQ0 0xa5c6
95 #define WDTCR_WDKEY_SEQ1 0xda7e
100 unsigned long period;
102 unsigned long reg_base;
103 unsigned long tim_reg;
104 unsigned long prd_reg;
105 unsigned long enamode_shift;
106 struct irqaction irqaction;
108 static struct timer_s timers[];
110 /* values for 'opts' field of struct timer_s */
111 #define TIMER_OPTS_DISABLED 0x00
112 #define TIMER_OPTS_ONESHOT 0x01
113 #define TIMER_OPTS_PERIODIC 0x02
115 static int timer32_config(struct timer_s *t)
117 u32 tcr = davinci_readl(t->reg_base + TCR);
120 tcr &= ~(TCR_ENAMODE_MASK << t->enamode_shift);
121 davinci_writel(tcr, t->reg_base + TCR);
123 /* reset counter to zero, set new period */
124 davinci_writel(0, t->tim_reg);
125 davinci_writel(t->period, t->prd_reg);
127 /* Set enable mode */
128 if (t->opts & TIMER_OPTS_ONESHOT) {
129 tcr |= TCR_ENAMODE_ONESHOT << t->enamode_shift;
130 } else if (t->opts & TIMER_OPTS_PERIODIC) {
131 tcr |= TCR_ENAMODE_PERIODIC << t->enamode_shift;
134 davinci_writel(tcr, t->reg_base + TCR);
138 static inline u32 timer32_read(struct timer_s *t)
140 return davinci_readl(t->tim_reg);
143 static irqreturn_t timer_interrupt(int irq, void *dev_id)
145 struct clock_event_device *evt = &clockevent_davinci;
147 evt->event_handler(evt);
151 /* called when 32-bit counter wraps */
152 static irqreturn_t freerun_interrupt(int irq, void *dev_id)
157 static struct timer_s timers[] = {
159 .name = "clockevent",
160 .opts = TIMER_OPTS_DISABLED,
162 .flags = IRQF_DISABLED | IRQF_TIMER,
163 .handler = timer_interrupt,
166 [TID_CLOCKSOURCE] = {
167 .name = "free-run counter",
169 .opts = TIMER_OPTS_PERIODIC,
171 .flags = IRQF_DISABLED | IRQF_TIMER,
172 .handler = freerun_interrupt,
177 static void __init timer_init(void)
179 u32 bases[] = {DAVINCI_TIMER0_BASE, DAVINCI_TIMER1_BASE};
182 /* Global init of each 64-bit timer as a whole */
184 u32 tgcr, base = bases[i];
186 /* Disabled, Internal clock source */
187 davinci_writel(0, base + TCR);
189 /* reset both timers, no pre-scaler for timer34 */
191 davinci_writel(tgcr, base + TGCR);
193 /* Set both timers to unchained 32-bit */
194 tgcr = TGCR_TIMMODE_32BIT_UNCHAINED << TGCR_TIMMODE_SHIFT;
195 davinci_writel(tgcr, base + TGCR);
198 tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
199 (TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
200 davinci_writel(tgcr, base + TGCR);
202 /* Init both counters to zero */
203 davinci_writel(0, base + TIM12);
204 davinci_writel(0, base + TIM34);
207 /* Init of each timer as a 32-bit timer */
208 for (i=0; i< ARRAY_SIZE(timers); i++) {
209 struct timer_s *t = &timers[i];
213 t->reg_base = (IS_TIMER1(t->id) ?
214 DAVINCI_TIMER1_BASE : DAVINCI_TIMER0_BASE);
216 if (IS_TIMER_BOT(t->id)) {
217 t->enamode_shift = 6;
218 t->tim_reg = t->reg_base + TIM12;
219 t->prd_reg = t->reg_base + PRD12;
221 t->enamode_shift = 22;
222 t->tim_reg = t->reg_base + TIM34;
223 t->prd_reg = t->reg_base + PRD34;
226 /* Register interrupt */
227 t->irqaction.name = t->name;
228 t->irqaction.dev_id = (void *)t;
229 if (t->irqaction.handler != NULL) {
230 setup_irq(timer_irqs[t->id], &t->irqaction);
233 timer32_config(&timers[i]);
241 static cycle_t read_cycles(void)
243 struct timer_s *t = &timers[TID_CLOCKSOURCE];
245 return (cycles_t)timer32_read(t);
248 static struct clocksource clocksource_davinci = {
252 .mask = CLOCKSOURCE_MASK(32),
254 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
260 static int davinci_set_next_event(unsigned long cycles,
261 struct clock_event_device *evt)
263 struct timer_s *t = &timers[TID_CLOCKEVENT];
270 static void davinci_set_mode(enum clock_event_mode mode,
271 struct clock_event_device *evt)
273 struct timer_s *t = &timers[TID_CLOCKEVENT];
276 case CLOCK_EVT_MODE_PERIODIC:
277 t->period = CLOCK_TICK_RATE / (HZ);
278 t->opts = TIMER_OPTS_PERIODIC;
281 case CLOCK_EVT_MODE_ONESHOT:
282 t->opts = TIMER_OPTS_ONESHOT;
284 case CLOCK_EVT_MODE_UNUSED:
285 case CLOCK_EVT_MODE_SHUTDOWN:
286 t->opts = TIMER_OPTS_DISABLED;
288 case CLOCK_EVT_MODE_RESUME:
293 static struct clock_event_device clockevent_davinci = {
295 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
297 .set_next_event = davinci_set_next_event,
298 .set_mode = davinci_set_mode,
302 static void __init davinci_timer_init(void)
304 static char err[] __initdata = KERN_ERR
305 "%s: can't register clocksource!\n";
310 /* setup clocksource */
311 clocksource_davinci.mult =
312 clocksource_khz2mult(CLOCK_TICK_RATE/1000,
313 clocksource_davinci.shift);
314 if (clocksource_register(&clocksource_davinci))
315 printk(err, clocksource_davinci.name);
317 /* setup clockevent */
318 clockevent_davinci.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC,
319 clockevent_davinci.shift);
320 clockevent_davinci.max_delta_ns =
321 clockevent_delta2ns(0xfffffffe, &clockevent_davinci);
322 clockevent_davinci.min_delta_ns =
323 clockevent_delta2ns(1, &clockevent_davinci);
325 clockevent_davinci.cpumask = cpumask_of_cpu(0);
326 clockevents_register_device(&clockevent_davinci);
329 struct sys_timer davinci_timer = {
330 .init = davinci_timer_init,
334 /* reset board using watchdog timer */
335 void davinci_watchdog_reset(void) {
336 u32 tgcr, wdtcr, base = DAVINCI_WDOG_BASE;
338 /* disable, internal clock source */
339 davinci_writel(0, base + TCR);
341 /* reset timer, set mode to 64-bit watchdog, and unreset */
343 davinci_writel(tgcr, base + TCR);
344 tgcr = TGCR_TIMMODE_64BIT_WDOG << TGCR_TIMMODE_SHIFT;
345 tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
346 (TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
347 davinci_writel(tgcr, base + TCR);
349 /* clear counter and period regs */
350 davinci_writel(0, base + TIM12);
351 davinci_writel(0, base + TIM34);
352 davinci_writel(0, base + PRD12);
353 davinci_writel(0, base + PRD34);
356 wdtcr = davinci_readl(base + WDTCR);
357 wdtcr |= WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT;
358 davinci_writel(wdtcr, base + WDTCR);
360 /* put watchdog in pre-active state */
361 wdtcr = (WDTCR_WDKEY_SEQ0 << WDTCR_WDKEY_SHIFT) |
362 (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
363 davinci_writel(wdtcr, base + WDTCR);
365 /* put watchdog in active state */
366 wdtcr = (WDTCR_WDKEY_SEQ1 << WDTCR_WDKEY_SHIFT) |
367 (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
368 davinci_writel(wdtcr, base + WDTCR);
370 /* write an invalid value to the WDKEY field to trigger
371 * a watchdog reset */
373 davinci_writel(wdtcr, base + WDTCR);