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