Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 1 | /* |
| 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> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 17 | #include <linux/io.h> |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 18 | #include <linux/clk.h> |
| 19 | #include <linux/err.h> |
Kevin Hilman | fb63138 | 2009-04-29 16:23:59 -0700 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 21 | |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 22 | #include <asm/mach/irq.h> |
| 23 | #include <asm/mach/time.h> |
Sekhar Nori | 5d0ef6a | 2011-12-23 17:57:19 +0000 | [diff] [blame^] | 24 | |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 25 | #include <mach/cputype.h> |
Sekhar Nori | 5d0ef6a | 2011-12-23 17:57:19 +0000 | [diff] [blame^] | 26 | #include <mach/hardware.h> |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 27 | #include <mach/time.h> |
Sekhar Nori | 5d0ef6a | 2011-12-23 17:57:19 +0000 | [diff] [blame^] | 28 | |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 29 | #include "clock.h" |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 30 | |
| 31 | static struct clock_event_device clockevent_davinci; |
Kevin Hilman | e609900 | 2009-04-14 07:06:37 -0500 | [diff] [blame] | 32 | static unsigned int davinci_clock_tick_rate; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 33 | |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 34 | /* |
| 35 | * This driver configures the 2 64-bit count-up timers as 4 independent |
| 36 | * 32-bit count-up timers used as follows: |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 37 | */ |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 38 | |
| 39 | enum { |
| 40 | TID_CLOCKEVENT, |
| 41 | TID_CLOCKSOURCE, |
| 42 | }; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 43 | |
| 44 | /* Timer register offsets */ |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 45 | #define PID12 0x0 |
| 46 | #define TIM12 0x10 |
| 47 | #define TIM34 0x14 |
| 48 | #define PRD12 0x18 |
| 49 | #define PRD34 0x1c |
| 50 | #define TCR 0x20 |
| 51 | #define TGCR 0x24 |
| 52 | #define WDTCR 0x28 |
| 53 | |
| 54 | /* Offsets of the 8 compare registers */ |
| 55 | #define CMP12_0 0x60 |
| 56 | #define CMP12_1 0x64 |
| 57 | #define CMP12_2 0x68 |
| 58 | #define CMP12_3 0x6c |
| 59 | #define CMP12_4 0x70 |
| 60 | #define CMP12_5 0x74 |
| 61 | #define CMP12_6 0x78 |
| 62 | #define CMP12_7 0x7c |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 63 | |
| 64 | /* Timer register bitfields */ |
| 65 | #define TCR_ENAMODE_DISABLE 0x0 |
| 66 | #define TCR_ENAMODE_ONESHOT 0x1 |
| 67 | #define TCR_ENAMODE_PERIODIC 0x2 |
| 68 | #define TCR_ENAMODE_MASK 0x3 |
| 69 | |
| 70 | #define TGCR_TIMMODE_SHIFT 2 |
| 71 | #define TGCR_TIMMODE_64BIT_GP 0x0 |
| 72 | #define TGCR_TIMMODE_32BIT_UNCHAINED 0x1 |
| 73 | #define TGCR_TIMMODE_64BIT_WDOG 0x2 |
| 74 | #define TGCR_TIMMODE_32BIT_CHAINED 0x3 |
| 75 | |
| 76 | #define TGCR_TIM12RS_SHIFT 0 |
| 77 | #define TGCR_TIM34RS_SHIFT 1 |
| 78 | #define TGCR_RESET 0x0 |
| 79 | #define TGCR_UNRESET 0x1 |
| 80 | #define TGCR_RESET_MASK 0x3 |
| 81 | |
| 82 | #define WDTCR_WDEN_SHIFT 14 |
| 83 | #define WDTCR_WDEN_DISABLE 0x0 |
| 84 | #define WDTCR_WDEN_ENABLE 0x1 |
| 85 | #define WDTCR_WDKEY_SHIFT 16 |
| 86 | #define WDTCR_WDKEY_SEQ0 0xa5c6 |
| 87 | #define WDTCR_WDKEY_SEQ1 0xda7e |
| 88 | |
| 89 | struct timer_s { |
| 90 | char *name; |
| 91 | unsigned int id; |
| 92 | unsigned long period; |
| 93 | unsigned long opts; |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 94 | unsigned long flags; |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 95 | void __iomem *base; |
| 96 | unsigned long tim_off; |
| 97 | unsigned long prd_off; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 98 | unsigned long enamode_shift; |
| 99 | struct irqaction irqaction; |
| 100 | }; |
| 101 | static struct timer_s timers[]; |
| 102 | |
| 103 | /* values for 'opts' field of struct timer_s */ |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 104 | #define TIMER_OPTS_DISABLED 0x01 |
| 105 | #define TIMER_OPTS_ONESHOT 0x02 |
| 106 | #define TIMER_OPTS_PERIODIC 0x04 |
| 107 | #define TIMER_OPTS_STATE_MASK 0x07 |
| 108 | |
| 109 | #define TIMER_OPTS_USE_COMPARE 0x80000000 |
| 110 | #define USING_COMPARE(t) ((t)->opts & TIMER_OPTS_USE_COMPARE) |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 111 | |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 112 | static char *id_to_name[] = { |
| 113 | [T0_BOT] = "timer0_0", |
| 114 | [T0_TOP] = "timer0_1", |
| 115 | [T1_BOT] = "timer1_0", |
| 116 | [T1_TOP] = "timer1_1", |
| 117 | }; |
| 118 | |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 119 | static int timer32_config(struct timer_s *t) |
| 120 | { |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 121 | u32 tcr; |
Mark A. Greer | 5570078 | 2009-04-15 12:42:06 -0700 | [diff] [blame] | 122 | struct davinci_soc_info *soc_info = &davinci_soc_info; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 123 | |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 124 | if (USING_COMPARE(t)) { |
| 125 | struct davinci_timer_instance *dtip = |
| 126 | soc_info->timer_info->timers; |
| 127 | int event_timer = ID_TO_TIMER(timers[TID_CLOCKEVENT].id); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 128 | |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 129 | /* |
| 130 | * Next interrupt should be the current time reg value plus |
| 131 | * the new period (using 32-bit unsigned addition/wrapping |
| 132 | * to 0 on overflow). This assumes that the clocksource |
| 133 | * is setup to count to 2^32-1 before wrapping around to 0. |
| 134 | */ |
| 135 | __raw_writel(__raw_readl(t->base + t->tim_off) + t->period, |
| 136 | t->base + dtip[event_timer].cmp_off); |
| 137 | } else { |
| 138 | tcr = __raw_readl(t->base + TCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 139 | |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 140 | /* disable timer */ |
| 141 | tcr &= ~(TCR_ENAMODE_MASK << t->enamode_shift); |
| 142 | __raw_writel(tcr, t->base + TCR); |
| 143 | |
| 144 | /* reset counter to zero, set new period */ |
| 145 | __raw_writel(0, t->base + t->tim_off); |
| 146 | __raw_writel(t->period, t->base + t->prd_off); |
| 147 | |
| 148 | /* Set enable mode */ |
| 149 | if (t->opts & TIMER_OPTS_ONESHOT) |
| 150 | tcr |= TCR_ENAMODE_ONESHOT << t->enamode_shift; |
| 151 | else if (t->opts & TIMER_OPTS_PERIODIC) |
| 152 | tcr |= TCR_ENAMODE_PERIODIC << t->enamode_shift; |
| 153 | |
| 154 | __raw_writel(tcr, t->base + TCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 155 | } |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static inline u32 timer32_read(struct timer_s *t) |
| 160 | { |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 161 | return __raw_readl(t->base + t->tim_off); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static irqreturn_t timer_interrupt(int irq, void *dev_id) |
| 165 | { |
| 166 | struct clock_event_device *evt = &clockevent_davinci; |
| 167 | |
| 168 | evt->event_handler(evt); |
| 169 | return IRQ_HANDLED; |
| 170 | } |
| 171 | |
| 172 | /* called when 32-bit counter wraps */ |
| 173 | static irqreturn_t freerun_interrupt(int irq, void *dev_id) |
| 174 | { |
| 175 | return IRQ_HANDLED; |
| 176 | } |
| 177 | |
| 178 | static struct timer_s timers[] = { |
| 179 | [TID_CLOCKEVENT] = { |
| 180 | .name = "clockevent", |
| 181 | .opts = TIMER_OPTS_DISABLED, |
| 182 | .irqaction = { |
| 183 | .flags = IRQF_DISABLED | IRQF_TIMER, |
| 184 | .handler = timer_interrupt, |
| 185 | } |
| 186 | }, |
| 187 | [TID_CLOCKSOURCE] = { |
| 188 | .name = "free-run counter", |
| 189 | .period = ~0, |
| 190 | .opts = TIMER_OPTS_PERIODIC, |
| 191 | .irqaction = { |
| 192 | .flags = IRQF_DISABLED | IRQF_TIMER, |
| 193 | .handler = freerun_interrupt, |
| 194 | } |
| 195 | }, |
| 196 | }; |
| 197 | |
| 198 | static void __init timer_init(void) |
| 199 | { |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 200 | struct davinci_soc_info *soc_info = &davinci_soc_info; |
| 201 | struct davinci_timer_instance *dtip = soc_info->timer_info->timers; |
Cyril Chemparathy | 1bcd38a | 2010-05-07 17:06:35 -0400 | [diff] [blame] | 202 | void __iomem *base[2]; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 203 | int i; |
| 204 | |
| 205 | /* Global init of each 64-bit timer as a whole */ |
| 206 | for(i=0; i<2; i++) { |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 207 | u32 tgcr; |
Cyril Chemparathy | 1bcd38a | 2010-05-07 17:06:35 -0400 | [diff] [blame] | 208 | |
| 209 | base[i] = ioremap(dtip[i].base, SZ_4K); |
| 210 | if (WARN_ON(!base[i])) |
| 211 | continue; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 212 | |
| 213 | /* Disabled, Internal clock source */ |
Cyril Chemparathy | 1bcd38a | 2010-05-07 17:06:35 -0400 | [diff] [blame] | 214 | __raw_writel(0, base[i] + TCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 215 | |
| 216 | /* reset both timers, no pre-scaler for timer34 */ |
| 217 | tgcr = 0; |
Cyril Chemparathy | 1bcd38a | 2010-05-07 17:06:35 -0400 | [diff] [blame] | 218 | __raw_writel(tgcr, base[i] + TGCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 219 | |
| 220 | /* Set both timers to unchained 32-bit */ |
| 221 | tgcr = TGCR_TIMMODE_32BIT_UNCHAINED << TGCR_TIMMODE_SHIFT; |
Cyril Chemparathy | 1bcd38a | 2010-05-07 17:06:35 -0400 | [diff] [blame] | 222 | __raw_writel(tgcr, base[i] + TGCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 223 | |
| 224 | /* Unreset timers */ |
| 225 | tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) | |
| 226 | (TGCR_UNRESET << TGCR_TIM34RS_SHIFT); |
Cyril Chemparathy | 1bcd38a | 2010-05-07 17:06:35 -0400 | [diff] [blame] | 227 | __raw_writel(tgcr, base[i] + TGCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 228 | |
| 229 | /* Init both counters to zero */ |
Cyril Chemparathy | 1bcd38a | 2010-05-07 17:06:35 -0400 | [diff] [blame] | 230 | __raw_writel(0, base[i] + TIM12); |
| 231 | __raw_writel(0, base[i] + TIM34); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* Init of each timer as a 32-bit timer */ |
| 235 | for (i=0; i< ARRAY_SIZE(timers); i++) { |
| 236 | struct timer_s *t = &timers[i]; |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 237 | int timer = ID_TO_TIMER(t->id); |
| 238 | u32 irq; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 239 | |
Cyril Chemparathy | 1bcd38a | 2010-05-07 17:06:35 -0400 | [diff] [blame] | 240 | t->base = base[timer]; |
| 241 | if (!t->base) |
| 242 | continue; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 243 | |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 244 | if (IS_TIMER_BOT(t->id)) { |
| 245 | t->enamode_shift = 6; |
| 246 | t->tim_off = TIM12; |
| 247 | t->prd_off = PRD12; |
| 248 | irq = dtip[timer].bottom_irq; |
| 249 | } else { |
| 250 | t->enamode_shift = 22; |
| 251 | t->tim_off = TIM34; |
| 252 | t->prd_off = PRD34; |
| 253 | irq = dtip[timer].top_irq; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 254 | } |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 255 | |
| 256 | /* Register interrupt */ |
| 257 | t->irqaction.name = t->name; |
| 258 | t->irqaction.dev_id = (void *)t; |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 259 | |
| 260 | if (t->irqaction.handler != NULL) { |
| 261 | irq = USING_COMPARE(t) ? dtip[i].cmp_irq : irq; |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 262 | setup_irq(irq, &t->irqaction); |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 263 | } |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * clocksource |
| 269 | */ |
Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 270 | static cycle_t read_cycles(struct clocksource *cs) |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 271 | { |
| 272 | struct timer_s *t = &timers[TID_CLOCKSOURCE]; |
| 273 | |
| 274 | return (cycles_t)timer32_read(t); |
| 275 | } |
| 276 | |
Andreas Gaeer | 6d1c57c | 2010-10-06 10:38:55 +0200 | [diff] [blame] | 277 | /* |
| 278 | * Kernel assumes that sched_clock can be called early but may not have |
| 279 | * things ready yet. |
| 280 | */ |
| 281 | static cycle_t read_dummy(struct clocksource *cs) |
| 282 | { |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 287 | static struct clocksource clocksource_davinci = { |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 288 | .rating = 300, |
Andreas Gaeer | 6d1c57c | 2010-10-06 10:38:55 +0200 | [diff] [blame] | 289 | .read = read_dummy, |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 290 | .mask = CLOCKSOURCE_MASK(32), |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 291 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 292 | }; |
| 293 | |
| 294 | /* |
Andreas Gaeer | 6d1c57c | 2010-10-06 10:38:55 +0200 | [diff] [blame] | 295 | * Overwrite weak default sched_clock with something more precise |
| 296 | */ |
| 297 | unsigned long long notrace sched_clock(void) |
| 298 | { |
| 299 | const cycle_t cyc = clocksource_davinci.read(&clocksource_davinci); |
| 300 | |
| 301 | return clocksource_cyc2ns(cyc, clocksource_davinci.mult, |
| 302 | clocksource_davinci.shift); |
| 303 | } |
| 304 | |
| 305 | /* |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 306 | * clockevent |
| 307 | */ |
| 308 | static int davinci_set_next_event(unsigned long cycles, |
| 309 | struct clock_event_device *evt) |
| 310 | { |
| 311 | struct timer_s *t = &timers[TID_CLOCKEVENT]; |
| 312 | |
| 313 | t->period = cycles; |
| 314 | timer32_config(t); |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | static void davinci_set_mode(enum clock_event_mode mode, |
| 319 | struct clock_event_device *evt) |
| 320 | { |
| 321 | struct timer_s *t = &timers[TID_CLOCKEVENT]; |
| 322 | |
| 323 | switch (mode) { |
| 324 | case CLOCK_EVT_MODE_PERIODIC: |
Kevin Hilman | e609900 | 2009-04-14 07:06:37 -0500 | [diff] [blame] | 325 | t->period = davinci_clock_tick_rate / (HZ); |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 326 | t->opts &= ~TIMER_OPTS_STATE_MASK; |
| 327 | t->opts |= TIMER_OPTS_PERIODIC; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 328 | timer32_config(t); |
| 329 | break; |
| 330 | case CLOCK_EVT_MODE_ONESHOT: |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 331 | t->opts &= ~TIMER_OPTS_STATE_MASK; |
| 332 | t->opts |= TIMER_OPTS_ONESHOT; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 333 | break; |
| 334 | case CLOCK_EVT_MODE_UNUSED: |
| 335 | case CLOCK_EVT_MODE_SHUTDOWN: |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 336 | t->opts &= ~TIMER_OPTS_STATE_MASK; |
| 337 | t->opts |= TIMER_OPTS_DISABLED; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 338 | break; |
Thomas Gleixner | 18de5bc | 2007-07-21 04:37:34 -0700 | [diff] [blame] | 339 | case CLOCK_EVT_MODE_RESUME: |
| 340 | break; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | static struct clock_event_device clockevent_davinci = { |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 345 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, |
| 346 | .shift = 32, |
| 347 | .set_next_event = davinci_set_next_event, |
| 348 | .set_mode = davinci_set_mode, |
| 349 | }; |
| 350 | |
| 351 | |
| 352 | static void __init davinci_timer_init(void) |
| 353 | { |
Kevin Hilman | e609900 | 2009-04-14 07:06:37 -0500 | [diff] [blame] | 354 | struct clk *timer_clk; |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 355 | struct davinci_soc_info *soc_info = &davinci_soc_info; |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 356 | unsigned int clockevent_id; |
| 357 | unsigned int clocksource_id; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 358 | static char err[] __initdata = KERN_ERR |
| 359 | "%s: can't register clocksource!\n"; |
Kevin Hilman | d99c387 | 2010-03-11 14:57:35 -0800 | [diff] [blame] | 360 | int i; |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 361 | |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 362 | clockevent_id = soc_info->timer_info->clockevent_id; |
| 363 | clocksource_id = soc_info->timer_info->clocksource_id; |
| 364 | |
| 365 | timers[TID_CLOCKEVENT].id = clockevent_id; |
| 366 | timers[TID_CLOCKSOURCE].id = clocksource_id; |
| 367 | |
| 368 | /* |
| 369 | * If using same timer for both clock events & clocksource, |
| 370 | * a compare register must be used to generate an event interrupt. |
| 371 | * This is equivalent to a oneshot timer only (not periodic). |
| 372 | */ |
| 373 | if (clockevent_id == clocksource_id) { |
| 374 | struct davinci_timer_instance *dtip = |
| 375 | soc_info->timer_info->timers; |
| 376 | int event_timer = ID_TO_TIMER(clockevent_id); |
| 377 | |
| 378 | /* Only bottom timers can use compare regs */ |
| 379 | if (IS_TIMER_TOP(clockevent_id)) |
| 380 | pr_warning("davinci_timer_init: Invalid use" |
| 381 | " of system timers. Results unpredictable.\n"); |
| 382 | else if ((dtip[event_timer].cmp_off == 0) |
| 383 | || (dtip[event_timer].cmp_irq == 0)) |
| 384 | pr_warning("davinci_timer_init: Invalid timer instance" |
| 385 | " setup. Results unpredictable.\n"); |
| 386 | else { |
| 387 | timers[TID_CLOCKEVENT].opts |= TIMER_OPTS_USE_COMPARE; |
| 388 | clockevent_davinci.features = CLOCK_EVT_FEAT_ONESHOT; |
| 389 | } |
| 390 | } |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 391 | |
Kevin Hilman | e609900 | 2009-04-14 07:06:37 -0500 | [diff] [blame] | 392 | timer_clk = clk_get(NULL, "timer0"); |
| 393 | BUG_ON(IS_ERR(timer_clk)); |
| 394 | clk_enable(timer_clk); |
| 395 | |
Cyril Chemparathy | 8ca2e59 | 2010-03-25 17:43:45 -0400 | [diff] [blame] | 396 | /* init timer hw */ |
| 397 | timer_init(); |
| 398 | |
Kevin Hilman | e609900 | 2009-04-14 07:06:37 -0500 | [diff] [blame] | 399 | davinci_clock_tick_rate = clk_get_rate(timer_clk); |
| 400 | |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 401 | /* setup clocksource */ |
Andreas Gaeer | 6d1c57c | 2010-10-06 10:38:55 +0200 | [diff] [blame] | 402 | clocksource_davinci.read = read_cycles; |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 403 | clocksource_davinci.name = id_to_name[clocksource_id]; |
Russell King | 7c044be | 2010-12-13 13:17:12 +0000 | [diff] [blame] | 404 | if (clocksource_register_hz(&clocksource_davinci, |
| 405 | davinci_clock_tick_rate)) |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 406 | printk(err, clocksource_davinci.name); |
| 407 | |
| 408 | /* setup clockevent */ |
Mark A. Greer | f64691b | 2009-04-15 12:40:11 -0700 | [diff] [blame] | 409 | clockevent_davinci.name = id_to_name[timers[TID_CLOCKEVENT].id]; |
Kevin Hilman | e609900 | 2009-04-14 07:06:37 -0500 | [diff] [blame] | 410 | clockevent_davinci.mult = div_sc(davinci_clock_tick_rate, NSEC_PER_SEC, |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 411 | clockevent_davinci.shift); |
| 412 | clockevent_davinci.max_delta_ns = |
| 413 | clockevent_delta2ns(0xfffffffe, &clockevent_davinci); |
Mark A. Greer | 3abd5ac | 2009-04-15 12:41:54 -0700 | [diff] [blame] | 414 | clockevent_davinci.min_delta_ns = 50000; /* 50 usec */ |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 415 | |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 416 | clockevent_davinci.cpumask = cpumask_of(0); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 417 | clockevents_register_device(&clockevent_davinci); |
Kevin Hilman | d99c387 | 2010-03-11 14:57:35 -0800 | [diff] [blame] | 418 | |
| 419 | for (i=0; i< ARRAY_SIZE(timers); i++) |
| 420 | timer32_config(&timers[i]); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | struct sys_timer davinci_timer = { |
| 424 | .init = davinci_timer_init, |
| 425 | }; |
| 426 | |
| 427 | |
| 428 | /* reset board using watchdog timer */ |
Cyril Chemparathy | c78a5bc | 2010-05-01 18:38:28 -0400 | [diff] [blame] | 429 | void davinci_watchdog_reset(struct platform_device *pdev) |
Kevin Hilman | fb63138 | 2009-04-29 16:23:59 -0700 | [diff] [blame] | 430 | { |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 431 | u32 tgcr, wdtcr; |
Cyril Chemparathy | c78a5bc | 2010-05-01 18:38:28 -0400 | [diff] [blame] | 432 | void __iomem *base; |
Kevin Hilman | e609900 | 2009-04-14 07:06:37 -0500 | [diff] [blame] | 433 | struct clk *wd_clk; |
Kevin Hilman | e609900 | 2009-04-14 07:06:37 -0500 | [diff] [blame] | 434 | |
Cyril Chemparathy | c78a5bc | 2010-05-01 18:38:28 -0400 | [diff] [blame] | 435 | base = ioremap(pdev->resource[0].start, SZ_4K); |
| 436 | if (WARN_ON(!base)) |
| 437 | return; |
| 438 | |
Kevin Hilman | 5fcd294 | 2009-06-03 12:24:50 -0700 | [diff] [blame] | 439 | wd_clk = clk_get(&pdev->dev, NULL); |
Kevin Hilman | e609900 | 2009-04-14 07:06:37 -0500 | [diff] [blame] | 440 | if (WARN_ON(IS_ERR(wd_clk))) |
| 441 | return; |
| 442 | clk_enable(wd_clk); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 443 | |
| 444 | /* disable, internal clock source */ |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 445 | __raw_writel(0, base + TCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 446 | |
| 447 | /* reset timer, set mode to 64-bit watchdog, and unreset */ |
| 448 | tgcr = 0; |
David Griego | a23f7dc | 2009-06-01 11:41:54 -0700 | [diff] [blame] | 449 | __raw_writel(tgcr, base + TGCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 450 | tgcr = TGCR_TIMMODE_64BIT_WDOG << TGCR_TIMMODE_SHIFT; |
| 451 | tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) | |
| 452 | (TGCR_UNRESET << TGCR_TIM34RS_SHIFT); |
David Griego | a23f7dc | 2009-06-01 11:41:54 -0700 | [diff] [blame] | 453 | __raw_writel(tgcr, base + TGCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 454 | |
| 455 | /* clear counter and period regs */ |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 456 | __raw_writel(0, base + TIM12); |
| 457 | __raw_writel(0, base + TIM34); |
| 458 | __raw_writel(0, base + PRD12); |
| 459 | __raw_writel(0, base + PRD34); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 460 | |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 461 | /* put watchdog in pre-active state */ |
David Griego | a23f7dc | 2009-06-01 11:41:54 -0700 | [diff] [blame] | 462 | wdtcr = __raw_readl(base + WDTCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 463 | wdtcr = (WDTCR_WDKEY_SEQ0 << WDTCR_WDKEY_SHIFT) | |
| 464 | (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT); |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 465 | __raw_writel(wdtcr, base + WDTCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 466 | |
| 467 | /* put watchdog in active state */ |
| 468 | wdtcr = (WDTCR_WDKEY_SEQ1 << WDTCR_WDKEY_SHIFT) | |
| 469 | (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT); |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 470 | __raw_writel(wdtcr, base + WDTCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 471 | |
| 472 | /* write an invalid value to the WDKEY field to trigger |
| 473 | * a watchdog reset */ |
| 474 | wdtcr = 0x00004000; |
Kevin Hilman | f5c122d | 2009-04-14 07:04:16 -0500 | [diff] [blame] | 475 | __raw_writel(wdtcr, base + WDTCR); |
Kevin Hilman | 7c6337e | 2007-04-30 19:37:19 +0100 | [diff] [blame] | 476 | } |