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>
22 #include <asm/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
83 #define WDTCR_WDEN_SHIFT 14
84 #define WDTCR_WDEN_DISABLE 0x0
85 #define WDTCR_WDEN_ENABLE 0x1
86 #define WDTCR_WDKEY_SHIFT 16
87 #define WDTCR_WDKEY_SEQ0 0xa5c6
88 #define WDTCR_WDKEY_SEQ1 0xda7e
97 unsigned long tim_off
;
98 unsigned long prd_off
;
99 unsigned long enamode_shift
;
100 struct irqaction irqaction
;
102 static struct timer_s timers
[];
104 /* values for 'opts' field of struct timer_s */
105 #define TIMER_OPTS_DISABLED 0x01
106 #define TIMER_OPTS_ONESHOT 0x02
107 #define TIMER_OPTS_PERIODIC 0x04
108 #define TIMER_OPTS_STATE_MASK 0x07
110 #define TIMER_OPTS_USE_COMPARE 0x80000000
111 #define USING_COMPARE(t) ((t)->opts & TIMER_OPTS_USE_COMPARE)
113 static char *id_to_name
[] = {
114 [T0_BOT
] = "timer0_0",
115 [T0_TOP
] = "timer0_1",
116 [T1_BOT
] = "timer1_0",
117 [T1_TOP
] = "timer1_1",
120 static int timer32_config(struct timer_s
*t
)
123 struct davinci_soc_info
*soc_info
= &davinci_soc_info
;
125 if (USING_COMPARE(t
)) {
126 struct davinci_timer_instance
*dtip
=
127 soc_info
->timer_info
->timers
;
128 int event_timer
= ID_TO_TIMER(timers
[TID_CLOCKEVENT
].id
);
131 * Next interrupt should be the current time reg value plus
132 * the new period (using 32-bit unsigned addition/wrapping
133 * to 0 on overflow). This assumes that the clocksource
134 * is setup to count to 2^32-1 before wrapping around to 0.
136 __raw_writel(__raw_readl(t
->base
+ t
->tim_off
) + t
->period
,
137 t
->base
+ dtip
[event_timer
].cmp_off
);
139 tcr
= __raw_readl(t
->base
+ TCR
);
142 tcr
&= ~(TCR_ENAMODE_MASK
<< t
->enamode_shift
);
143 __raw_writel(tcr
, t
->base
+ TCR
);
145 /* reset counter to zero, set new period */
146 __raw_writel(0, t
->base
+ t
->tim_off
);
147 __raw_writel(t
->period
, t
->base
+ t
->prd_off
);
149 /* Set enable mode */
150 if (t
->opts
& TIMER_OPTS_ONESHOT
)
151 tcr
|= TCR_ENAMODE_ONESHOT
<< t
->enamode_shift
;
152 else if (t
->opts
& TIMER_OPTS_PERIODIC
)
153 tcr
|= TCR_ENAMODE_PERIODIC
<< t
->enamode_shift
;
155 __raw_writel(tcr
, t
->base
+ TCR
);
160 static inline u32
timer32_read(struct timer_s
*t
)
162 return __raw_readl(t
->base
+ t
->tim_off
);
165 static irqreturn_t
timer_interrupt(int irq
, void *dev_id
)
167 struct clock_event_device
*evt
= &clockevent_davinci
;
169 evt
->event_handler(evt
);
173 /* called when 32-bit counter wraps */
174 static irqreturn_t
freerun_interrupt(int irq
, void *dev_id
)
179 static struct timer_s timers
[] = {
181 .name
= "clockevent",
182 .opts
= TIMER_OPTS_DISABLED
,
184 .flags
= IRQF_DISABLED
| IRQF_TIMER
,
185 .handler
= timer_interrupt
,
188 [TID_CLOCKSOURCE
] = {
189 .name
= "free-run counter",
191 .opts
= TIMER_OPTS_PERIODIC
,
193 .flags
= IRQF_DISABLED
| IRQF_TIMER
,
194 .handler
= freerun_interrupt
,
199 static void __init
timer_init(void)
201 struct davinci_soc_info
*soc_info
= &davinci_soc_info
;
202 struct davinci_timer_instance
*dtip
= soc_info
->timer_info
->timers
;
203 void __iomem
*base
[2];
206 /* Global init of each 64-bit timer as a whole */
210 base
[i
] = ioremap(dtip
[i
].base
, SZ_4K
);
211 if (WARN_ON(!base
[i
]))
214 /* Disabled, Internal clock source */
215 __raw_writel(0, base
[i
] + TCR
);
217 /* reset both timers, no pre-scaler for timer34 */
219 __raw_writel(tgcr
, base
[i
] + TGCR
);
221 /* Set both timers to unchained 32-bit */
222 tgcr
= TGCR_TIMMODE_32BIT_UNCHAINED
<< TGCR_TIMMODE_SHIFT
;
223 __raw_writel(tgcr
, base
[i
] + TGCR
);
226 tgcr
|= (TGCR_UNRESET
<< TGCR_TIM12RS_SHIFT
) |
227 (TGCR_UNRESET
<< TGCR_TIM34RS_SHIFT
);
228 __raw_writel(tgcr
, base
[i
] + TGCR
);
230 /* Init both counters to zero */
231 __raw_writel(0, base
[i
] + TIM12
);
232 __raw_writel(0, base
[i
] + TIM34
);
235 /* Init of each timer as a 32-bit timer */
236 for (i
=0; i
< ARRAY_SIZE(timers
); i
++) {
237 struct timer_s
*t
= &timers
[i
];
238 int timer
= ID_TO_TIMER(t
->id
);
241 t
->base
= base
[timer
];
245 if (IS_TIMER_BOT(t
->id
)) {
246 t
->enamode_shift
= 6;
249 irq
= dtip
[timer
].bottom_irq
;
251 t
->enamode_shift
= 22;
254 irq
= dtip
[timer
].top_irq
;
257 /* Register interrupt */
258 t
->irqaction
.name
= t
->name
;
259 t
->irqaction
.dev_id
= (void *)t
;
261 if (t
->irqaction
.handler
!= NULL
) {
262 irq
= USING_COMPARE(t
) ? dtip
[i
].cmp_irq
: irq
;
263 setup_irq(irq
, &t
->irqaction
);
271 static cycle_t
read_cycles(struct clocksource
*cs
)
273 struct timer_s
*t
= &timers
[TID_CLOCKSOURCE
];
275 return (cycles_t
)timer32_read(t
);
278 static struct clocksource clocksource_davinci
= {
281 .mask
= CLOCKSOURCE_MASK(32),
282 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
286 * Overwrite weak default sched_clock with something more precise
288 static u32 notrace
davinci_read_sched_clock(void)
290 return timer32_read(&timers
[TID_CLOCKSOURCE
]);
296 static int davinci_set_next_event(unsigned long cycles
,
297 struct clock_event_device
*evt
)
299 struct timer_s
*t
= &timers
[TID_CLOCKEVENT
];
306 static void davinci_set_mode(enum clock_event_mode mode
,
307 struct clock_event_device
*evt
)
309 struct timer_s
*t
= &timers
[TID_CLOCKEVENT
];
312 case CLOCK_EVT_MODE_PERIODIC
:
313 t
->period
= davinci_clock_tick_rate
/ (HZ
);
314 t
->opts
&= ~TIMER_OPTS_STATE_MASK
;
315 t
->opts
|= TIMER_OPTS_PERIODIC
;
318 case CLOCK_EVT_MODE_ONESHOT
:
319 t
->opts
&= ~TIMER_OPTS_STATE_MASK
;
320 t
->opts
|= TIMER_OPTS_ONESHOT
;
322 case CLOCK_EVT_MODE_UNUSED
:
323 case CLOCK_EVT_MODE_SHUTDOWN
:
324 t
->opts
&= ~TIMER_OPTS_STATE_MASK
;
325 t
->opts
|= TIMER_OPTS_DISABLED
;
327 case CLOCK_EVT_MODE_RESUME
:
332 static struct clock_event_device clockevent_davinci
= {
333 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
,
335 .set_next_event
= davinci_set_next_event
,
336 .set_mode
= davinci_set_mode
,
340 void __init
davinci_timer_init(void)
342 struct clk
*timer_clk
;
343 struct davinci_soc_info
*soc_info
= &davinci_soc_info
;
344 unsigned int clockevent_id
;
345 unsigned int clocksource_id
;
346 static char err
[] __initdata
= KERN_ERR
347 "%s: can't register clocksource!\n";
350 clockevent_id
= soc_info
->timer_info
->clockevent_id
;
351 clocksource_id
= soc_info
->timer_info
->clocksource_id
;
353 timers
[TID_CLOCKEVENT
].id
= clockevent_id
;
354 timers
[TID_CLOCKSOURCE
].id
= clocksource_id
;
357 * If using same timer for both clock events & clocksource,
358 * a compare register must be used to generate an event interrupt.
359 * This is equivalent to a oneshot timer only (not periodic).
361 if (clockevent_id
== clocksource_id
) {
362 struct davinci_timer_instance
*dtip
=
363 soc_info
->timer_info
->timers
;
364 int event_timer
= ID_TO_TIMER(clockevent_id
);
366 /* Only bottom timers can use compare regs */
367 if (IS_TIMER_TOP(clockevent_id
))
368 pr_warning("davinci_timer_init: Invalid use"
369 " of system timers. Results unpredictable.\n");
370 else if ((dtip
[event_timer
].cmp_off
== 0)
371 || (dtip
[event_timer
].cmp_irq
== 0))
372 pr_warning("davinci_timer_init: Invalid timer instance"
373 " setup. Results unpredictable.\n");
375 timers
[TID_CLOCKEVENT
].opts
|= TIMER_OPTS_USE_COMPARE
;
376 clockevent_davinci
.features
= CLOCK_EVT_FEAT_ONESHOT
;
380 timer_clk
= clk_get(NULL
, "timer0");
381 BUG_ON(IS_ERR(timer_clk
));
382 clk_prepare_enable(timer_clk
);
387 davinci_clock_tick_rate
= clk_get_rate(timer_clk
);
389 /* setup clocksource */
390 clocksource_davinci
.name
= id_to_name
[clocksource_id
];
391 if (clocksource_register_hz(&clocksource_davinci
,
392 davinci_clock_tick_rate
))
393 printk(err
, clocksource_davinci
.name
);
395 setup_sched_clock(davinci_read_sched_clock
, 32,
396 davinci_clock_tick_rate
);
398 /* setup clockevent */
399 clockevent_davinci
.name
= id_to_name
[timers
[TID_CLOCKEVENT
].id
];
400 clockevent_davinci
.mult
= div_sc(davinci_clock_tick_rate
, NSEC_PER_SEC
,
401 clockevent_davinci
.shift
);
402 clockevent_davinci
.max_delta_ns
=
403 clockevent_delta2ns(0xfffffffe, &clockevent_davinci
);
404 clockevent_davinci
.min_delta_ns
= 50000; /* 50 usec */
406 clockevent_davinci
.cpumask
= cpumask_of(0);
407 clockevents_register_device(&clockevent_davinci
);
409 for (i
=0; i
< ARRAY_SIZE(timers
); i
++)
410 timer32_config(&timers
[i
]);
413 /* reset board using watchdog timer */
414 void davinci_watchdog_reset(struct platform_device
*pdev
)
420 base
= ioremap(pdev
->resource
[0].start
, SZ_4K
);
424 wd_clk
= clk_get(&pdev
->dev
, NULL
);
425 if (WARN_ON(IS_ERR(wd_clk
)))
427 clk_prepare_enable(wd_clk
);
429 /* disable, internal clock source */
430 __raw_writel(0, base
+ TCR
);
432 /* reset timer, set mode to 64-bit watchdog, and unreset */
434 __raw_writel(tgcr
, base
+ TGCR
);
435 tgcr
= TGCR_TIMMODE_64BIT_WDOG
<< TGCR_TIMMODE_SHIFT
;
436 tgcr
|= (TGCR_UNRESET
<< TGCR_TIM12RS_SHIFT
) |
437 (TGCR_UNRESET
<< TGCR_TIM34RS_SHIFT
);
438 __raw_writel(tgcr
, base
+ TGCR
);
440 /* clear counter and period regs */
441 __raw_writel(0, base
+ TIM12
);
442 __raw_writel(0, base
+ TIM34
);
443 __raw_writel(0, base
+ PRD12
);
444 __raw_writel(0, base
+ PRD34
);
446 /* put watchdog in pre-active state */
447 wdtcr
= __raw_readl(base
+ WDTCR
);
448 wdtcr
= (WDTCR_WDKEY_SEQ0
<< WDTCR_WDKEY_SHIFT
) |
449 (WDTCR_WDEN_ENABLE
<< WDTCR_WDEN_SHIFT
);
450 __raw_writel(wdtcr
, base
+ WDTCR
);
452 /* put watchdog in active state */
453 wdtcr
= (WDTCR_WDKEY_SEQ1
<< WDTCR_WDKEY_SHIFT
) |
454 (WDTCR_WDEN_ENABLE
<< WDTCR_WDEN_SHIFT
);
455 __raw_writel(wdtcr
, base
+ WDTCR
);
457 /* write an invalid value to the WDKEY field to trigger
458 * a watchdog reset */
460 __raw_writel(wdtcr
, base
+ WDTCR
);