blob: f80ae25a52e4c519efb9f48ec66574c068f37088 [file] [log] [blame]
Kevin Hilman7c6337e2007-04-30 19:37:19 +01001/*
2 * DaVinci timer subsystem
3 *
4 * Author: Kevin Hilman, MontaVista Software, Inc. <source@mvista.com>
5 *
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
9 * or implied.
10 */
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>
Russell Kingfced80c2008-09-06 12:10:45 +010018#include <linux/io.h>
Kevin Hilmanf5c122d2009-04-14 07:04:16 -050019#include <linux/clk.h>
20#include <linux/err.h>
21#include <linux/device.h>
Kevin Hilmanfb631382009-04-29 16:23:59 -070022#include <linux/platform_device.h>
Kevin Hilman7c6337e2007-04-30 19:37:19 +010023
Russell Kinga09e64f2008-08-05 16:14:15 +010024#include <mach/hardware.h>
Kevin Hilman7c6337e2007-04-30 19:37:19 +010025#include <asm/system.h>
26#include <asm/irq.h>
27#include <asm/mach/irq.h>
28#include <asm/mach/time.h>
29#include <asm/errno.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010030#include <mach/io.h>
Kevin Hilmanf5c122d2009-04-14 07:04:16 -050031#include <mach/cputype.h>
Mark A. Greerf64691b2009-04-15 12:40:11 -070032#include <mach/time.h>
Kevin Hilmanf5c122d2009-04-14 07:04:16 -050033#include "clock.h"
Kevin Hilman7c6337e2007-04-30 19:37:19 +010034
35static struct clock_event_device clockevent_davinci;
Kevin Hilmane6099002009-04-14 07:06:37 -050036static unsigned int davinci_clock_tick_rate;
Kevin Hilman7c6337e2007-04-30 19:37:19 +010037
Kevin Hilman7c6337e2007-04-30 19:37:19 +010038/*
39 * This driver configures the 2 64-bit count-up timers as 4 independent
40 * 32-bit count-up timers used as follows:
Kevin Hilman7c6337e2007-04-30 19:37:19 +010041 */
Mark A. Greerf64691b2009-04-15 12:40:11 -070042
43enum {
44 TID_CLOCKEVENT,
45 TID_CLOCKSOURCE,
46};
Kevin Hilman7c6337e2007-04-30 19:37:19 +010047
48/* Timer register offsets */
49#define PID12 0x0
50#define TIM12 0x10
51#define TIM34 0x14
52#define PRD12 0x18
53#define PRD34 0x1c
54#define TCR 0x20
55#define TGCR 0x24
56#define WDTCR 0x28
57
58/* Timer register bitfields */
59#define TCR_ENAMODE_DISABLE 0x0
60#define TCR_ENAMODE_ONESHOT 0x1
61#define TCR_ENAMODE_PERIODIC 0x2
62#define TCR_ENAMODE_MASK 0x3
63
64#define TGCR_TIMMODE_SHIFT 2
65#define TGCR_TIMMODE_64BIT_GP 0x0
66#define TGCR_TIMMODE_32BIT_UNCHAINED 0x1
67#define TGCR_TIMMODE_64BIT_WDOG 0x2
68#define TGCR_TIMMODE_32BIT_CHAINED 0x3
69
70#define TGCR_TIM12RS_SHIFT 0
71#define TGCR_TIM34RS_SHIFT 1
72#define TGCR_RESET 0x0
73#define TGCR_UNRESET 0x1
74#define TGCR_RESET_MASK 0x3
75
76#define WDTCR_WDEN_SHIFT 14
77#define WDTCR_WDEN_DISABLE 0x0
78#define WDTCR_WDEN_ENABLE 0x1
79#define WDTCR_WDKEY_SHIFT 16
80#define WDTCR_WDKEY_SEQ0 0xa5c6
81#define WDTCR_WDKEY_SEQ1 0xda7e
82
83struct timer_s {
84 char *name;
85 unsigned int id;
86 unsigned long period;
87 unsigned long opts;
Kevin Hilmanf5c122d2009-04-14 07:04:16 -050088 void __iomem *base;
89 unsigned long tim_off;
90 unsigned long prd_off;
Kevin Hilman7c6337e2007-04-30 19:37:19 +010091 unsigned long enamode_shift;
92 struct irqaction irqaction;
93};
94static struct timer_s timers[];
95
96/* values for 'opts' field of struct timer_s */
97#define TIMER_OPTS_DISABLED 0x00
98#define TIMER_OPTS_ONESHOT 0x01
99#define TIMER_OPTS_PERIODIC 0x02
100
Mark A. Greerf64691b2009-04-15 12:40:11 -0700101static char *id_to_name[] = {
102 [T0_BOT] = "timer0_0",
103 [T0_TOP] = "timer0_1",
104 [T1_BOT] = "timer1_0",
105 [T1_TOP] = "timer1_1",
106};
107
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100108static int timer32_config(struct timer_s *t)
109{
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500110 u32 tcr = __raw_readl(t->base + TCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100111
112 /* disable timer */
113 tcr &= ~(TCR_ENAMODE_MASK << t->enamode_shift);
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500114 __raw_writel(tcr, t->base + TCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100115
116 /* reset counter to zero, set new period */
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500117 __raw_writel(0, t->base + t->tim_off);
118 __raw_writel(t->period, t->base + t->prd_off);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100119
120 /* Set enable mode */
121 if (t->opts & TIMER_OPTS_ONESHOT) {
122 tcr |= TCR_ENAMODE_ONESHOT << t->enamode_shift;
123 } else if (t->opts & TIMER_OPTS_PERIODIC) {
124 tcr |= TCR_ENAMODE_PERIODIC << t->enamode_shift;
125 }
126
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500127 __raw_writel(tcr, t->base + TCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100128 return 0;
129}
130
131static inline u32 timer32_read(struct timer_s *t)
132{
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500133 return __raw_readl(t->base + t->tim_off);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100134}
135
136static irqreturn_t timer_interrupt(int irq, void *dev_id)
137{
138 struct clock_event_device *evt = &clockevent_davinci;
139
140 evt->event_handler(evt);
141 return IRQ_HANDLED;
142}
143
144/* called when 32-bit counter wraps */
145static irqreturn_t freerun_interrupt(int irq, void *dev_id)
146{
147 return IRQ_HANDLED;
148}
149
150static struct timer_s timers[] = {
151 [TID_CLOCKEVENT] = {
152 .name = "clockevent",
153 .opts = TIMER_OPTS_DISABLED,
154 .irqaction = {
155 .flags = IRQF_DISABLED | IRQF_TIMER,
156 .handler = timer_interrupt,
157 }
158 },
159 [TID_CLOCKSOURCE] = {
160 .name = "free-run counter",
161 .period = ~0,
162 .opts = TIMER_OPTS_PERIODIC,
163 .irqaction = {
164 .flags = IRQF_DISABLED | IRQF_TIMER,
165 .handler = freerun_interrupt,
166 }
167 },
168};
169
170static void __init timer_init(void)
171{
Mark A. Greerf64691b2009-04-15 12:40:11 -0700172 struct davinci_soc_info *soc_info = &davinci_soc_info;
173 struct davinci_timer_instance *dtip = soc_info->timer_info->timers;
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100174 int i;
175
176 /* Global init of each 64-bit timer as a whole */
177 for(i=0; i<2; i++) {
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500178 u32 tgcr;
Mark A. Greerf64691b2009-04-15 12:40:11 -0700179 void __iomem *base = dtip[i].base;
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100180
181 /* Disabled, Internal clock source */
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500182 __raw_writel(0, base + TCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100183
184 /* reset both timers, no pre-scaler for timer34 */
185 tgcr = 0;
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500186 __raw_writel(tgcr, base + TGCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100187
188 /* Set both timers to unchained 32-bit */
189 tgcr = TGCR_TIMMODE_32BIT_UNCHAINED << TGCR_TIMMODE_SHIFT;
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500190 __raw_writel(tgcr, base + TGCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100191
192 /* Unreset timers */
193 tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
194 (TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500195 __raw_writel(tgcr, base + TGCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100196
197 /* Init both counters to zero */
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500198 __raw_writel(0, base + TIM12);
199 __raw_writel(0, base + TIM34);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100200 }
201
202 /* Init of each timer as a 32-bit timer */
203 for (i=0; i< ARRAY_SIZE(timers); i++) {
204 struct timer_s *t = &timers[i];
Mark A. Greerf64691b2009-04-15 12:40:11 -0700205 int timer = ID_TO_TIMER(t->id);
206 u32 irq;
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100207
Mark A. Greerf64691b2009-04-15 12:40:11 -0700208 t->base = dtip[timer].base;
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100209
Mark A. Greerf64691b2009-04-15 12:40:11 -0700210 if (IS_TIMER_BOT(t->id)) {
211 t->enamode_shift = 6;
212 t->tim_off = TIM12;
213 t->prd_off = PRD12;
214 irq = dtip[timer].bottom_irq;
215 } else {
216 t->enamode_shift = 22;
217 t->tim_off = TIM34;
218 t->prd_off = PRD34;
219 irq = dtip[timer].top_irq;
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100220 }
Mark A. Greerf64691b2009-04-15 12:40:11 -0700221
222 /* Register interrupt */
223 t->irqaction.name = t->name;
224 t->irqaction.dev_id = (void *)t;
225 if (t->irqaction.handler != NULL)
226 setup_irq(irq, &t->irqaction);
227
228 timer32_config(&timers[i]);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100229 }
230}
231
232/*
233 * clocksource
234 */
Magnus Damm8e196082009-04-21 12:24:00 -0700235static cycle_t read_cycles(struct clocksource *cs)
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100236{
237 struct timer_s *t = &timers[TID_CLOCKSOURCE];
238
239 return (cycles_t)timer32_read(t);
240}
241
242static struct clocksource clocksource_davinci = {
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100243 .rating = 300,
244 .read = read_cycles,
245 .mask = CLOCKSOURCE_MASK(32),
246 .shift = 24,
247 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
248};
249
250/*
251 * clockevent
252 */
253static int davinci_set_next_event(unsigned long cycles,
254 struct clock_event_device *evt)
255{
256 struct timer_s *t = &timers[TID_CLOCKEVENT];
257
258 t->period = cycles;
259 timer32_config(t);
260 return 0;
261}
262
263static void davinci_set_mode(enum clock_event_mode mode,
264 struct clock_event_device *evt)
265{
266 struct timer_s *t = &timers[TID_CLOCKEVENT];
267
268 switch (mode) {
269 case CLOCK_EVT_MODE_PERIODIC:
Kevin Hilmane6099002009-04-14 07:06:37 -0500270 t->period = davinci_clock_tick_rate / (HZ);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100271 t->opts = TIMER_OPTS_PERIODIC;
272 timer32_config(t);
273 break;
274 case CLOCK_EVT_MODE_ONESHOT:
275 t->opts = TIMER_OPTS_ONESHOT;
276 break;
277 case CLOCK_EVT_MODE_UNUSED:
278 case CLOCK_EVT_MODE_SHUTDOWN:
279 t->opts = TIMER_OPTS_DISABLED;
280 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700281 case CLOCK_EVT_MODE_RESUME:
282 break;
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100283 }
284}
285
286static struct clock_event_device clockevent_davinci = {
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100287 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
288 .shift = 32,
289 .set_next_event = davinci_set_next_event,
290 .set_mode = davinci_set_mode,
291};
292
293
294static void __init davinci_timer_init(void)
295{
Kevin Hilmane6099002009-04-14 07:06:37 -0500296 struct clk *timer_clk;
Mark A. Greerf64691b2009-04-15 12:40:11 -0700297 struct davinci_soc_info *soc_info = &davinci_soc_info;
Kevin Hilmane6099002009-04-14 07:06:37 -0500298
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100299 static char err[] __initdata = KERN_ERR
300 "%s: can't register clocksource!\n";
301
Mark A. Greerf64691b2009-04-15 12:40:11 -0700302 timers[TID_CLOCKEVENT].id = soc_info->timer_info->clockevent_id;
303 timers[TID_CLOCKSOURCE].id = soc_info->timer_info->clocksource_id;
304
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100305 /* init timer hw */
306 timer_init();
307
Kevin Hilmane6099002009-04-14 07:06:37 -0500308 timer_clk = clk_get(NULL, "timer0");
309 BUG_ON(IS_ERR(timer_clk));
310 clk_enable(timer_clk);
311
312 davinci_clock_tick_rate = clk_get_rate(timer_clk);
313
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100314 /* setup clocksource */
Mark A. Greerf64691b2009-04-15 12:40:11 -0700315 clocksource_davinci.name = id_to_name[timers[TID_CLOCKSOURCE].id];
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100316 clocksource_davinci.mult =
Kevin Hilmane6099002009-04-14 07:06:37 -0500317 clocksource_khz2mult(davinci_clock_tick_rate/1000,
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100318 clocksource_davinci.shift);
319 if (clocksource_register(&clocksource_davinci))
320 printk(err, clocksource_davinci.name);
321
322 /* setup clockevent */
Mark A. Greerf64691b2009-04-15 12:40:11 -0700323 clockevent_davinci.name = id_to_name[timers[TID_CLOCKEVENT].id];
Kevin Hilmane6099002009-04-14 07:06:37 -0500324 clockevent_davinci.mult = div_sc(davinci_clock_tick_rate, NSEC_PER_SEC,
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100325 clockevent_davinci.shift);
326 clockevent_davinci.max_delta_ns =
327 clockevent_delta2ns(0xfffffffe, &clockevent_davinci);
328 clockevent_davinci.min_delta_ns =
329 clockevent_delta2ns(1, &clockevent_davinci);
330
Rusty Russell320ab2b2008-12-13 21:20:26 +1030331 clockevent_davinci.cpumask = cpumask_of(0);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100332 clockevents_register_device(&clockevent_davinci);
333}
334
335struct sys_timer davinci_timer = {
336 .init = davinci_timer_init,
337};
338
339
340/* reset board using watchdog timer */
Kevin Hilmanfb631382009-04-29 16:23:59 -0700341void davinci_watchdog_reset(void)
342{
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500343 u32 tgcr, wdtcr;
Mark A. Greer951d6f62009-04-15 12:40:21 -0700344 struct davinci_soc_info *soc_info = &davinci_soc_info;
345 void __iomem *base = soc_info->wdt_base;
Kevin Hilmane6099002009-04-14 07:06:37 -0500346 struct clk *wd_clk;
Kevin Hilmane6099002009-04-14 07:06:37 -0500347
Kevin Hilmanfb631382009-04-29 16:23:59 -0700348 wd_clk = clk_get(&davinci_wdt_device.dev, NULL);
Kevin Hilmane6099002009-04-14 07:06:37 -0500349 if (WARN_ON(IS_ERR(wd_clk)))
350 return;
351 clk_enable(wd_clk);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100352
353 /* disable, internal clock source */
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500354 __raw_writel(0, base + TCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100355
356 /* reset timer, set mode to 64-bit watchdog, and unreset */
357 tgcr = 0;
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500358 __raw_writel(tgcr, base + TCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100359 tgcr = TGCR_TIMMODE_64BIT_WDOG << TGCR_TIMMODE_SHIFT;
360 tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
361 (TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500362 __raw_writel(tgcr, base + TCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100363
364 /* clear counter and period regs */
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500365 __raw_writel(0, base + TIM12);
366 __raw_writel(0, base + TIM34);
367 __raw_writel(0, base + PRD12);
368 __raw_writel(0, base + PRD34);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100369
370 /* enable */
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500371 wdtcr = __raw_readl(base + WDTCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100372 wdtcr |= WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT;
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500373 __raw_writel(wdtcr, base + WDTCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100374
375 /* put watchdog in pre-active state */
376 wdtcr = (WDTCR_WDKEY_SEQ0 << WDTCR_WDKEY_SHIFT) |
377 (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500378 __raw_writel(wdtcr, base + WDTCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100379
380 /* put watchdog in active state */
381 wdtcr = (WDTCR_WDKEY_SEQ1 << WDTCR_WDKEY_SHIFT) |
382 (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500383 __raw_writel(wdtcr, base + WDTCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100384
385 /* write an invalid value to the WDKEY field to trigger
386 * a watchdog reset */
387 wdtcr = 0x00004000;
Kevin Hilmanf5c122d2009-04-14 07:04:16 -0500388 __raw_writel(wdtcr, base + WDTCR);
Kevin Hilman7c6337e2007-04-30 19:37:19 +0100389}