time.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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>
  17. #include <linux/spinlock.h>
  18. #include <asm/io.h>
  19. #include <asm/hardware.h>
  20. #include <asm/system.h>
  21. #include <asm/irq.h>
  22. #include <asm/mach/irq.h>
  23. #include <asm/mach/time.h>
  24. #include <asm/errno.h>
  25. #include <asm/arch/io.h>
  26. static struct clock_event_device clockevent_davinci;
  27. #define DAVINCI_TIMER0_BASE (IO_PHYS + 0x21400)
  28. #define DAVINCI_TIMER1_BASE (IO_PHYS + 0x21800)
  29. #define DAVINCI_WDOG_BASE (IO_PHYS + 0x21C00)
  30. enum {
  31. T0_BOT = 0, T0_TOP, T1_BOT, T1_TOP, NUM_TIMERS,
  32. };
  33. #define IS_TIMER1(id) (id & 0x2)
  34. #define IS_TIMER0(id) (!IS_TIMER1(id))
  35. #define IS_TIMER_TOP(id) ((id & 0x1))
  36. #define IS_TIMER_BOT(id) (!IS_TIMER_TOP(id))
  37. static int timer_irqs[NUM_TIMERS] = {
  38. IRQ_TINT0_TINT12,
  39. IRQ_TINT0_TINT34,
  40. IRQ_TINT1_TINT12,
  41. IRQ_TINT1_TINT34,
  42. };
  43. /*
  44. * This driver configures the 2 64-bit count-up timers as 4 independent
  45. * 32-bit count-up timers used as follows:
  46. *
  47. * T0_BOT: Timer 0, bottom: clockevent source for hrtimers
  48. * T0_TOP: Timer 0, top : clocksource for generic timekeeping
  49. * T1_BOT: Timer 1, bottom: (used by DSP in TI DSPLink code)
  50. * T1_TOP: Timer 1, top : <unused>
  51. */
  52. #define TID_CLOCKEVENT T0_BOT
  53. #define TID_CLOCKSOURCE T0_TOP
  54. /* Timer register offsets */
  55. #define PID12 0x0
  56. #define TIM12 0x10
  57. #define TIM34 0x14
  58. #define PRD12 0x18
  59. #define PRD34 0x1c
  60. #define TCR 0x20
  61. #define TGCR 0x24
  62. #define WDTCR 0x28
  63. /* Timer register bitfields */
  64. #define TCR_ENAMODE_DISABLE 0x0
  65. #define TCR_ENAMODE_ONESHOT 0x1
  66. #define TCR_ENAMODE_PERIODIC 0x2
  67. #define TCR_ENAMODE_MASK 0x3
  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. #define TGCR_TIM12RS_SHIFT 0
  74. #define TGCR_TIM34RS_SHIFT 1
  75. #define TGCR_RESET 0x0
  76. #define TGCR_UNRESET 0x1
  77. #define TGCR_RESET_MASK 0x3
  78. #define WDTCR_WDEN_SHIFT 14
  79. #define WDTCR_WDEN_DISABLE 0x0
  80. #define WDTCR_WDEN_ENABLE 0x1
  81. #define WDTCR_WDKEY_SHIFT 16
  82. #define WDTCR_WDKEY_SEQ0 0xa5c6
  83. #define WDTCR_WDKEY_SEQ1 0xda7e
  84. struct timer_s {
  85. char *name;
  86. unsigned int id;
  87. unsigned long period;
  88. unsigned long opts;
  89. unsigned long reg_base;
  90. unsigned long tim_reg;
  91. unsigned long prd_reg;
  92. unsigned long enamode_shift;
  93. struct irqaction irqaction;
  94. };
  95. static struct timer_s timers[];
  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. static int timer32_config(struct timer_s *t)
  101. {
  102. u32 tcr = davinci_readl(t->reg_base + TCR);
  103. /* disable timer */
  104. tcr &= ~(TCR_ENAMODE_MASK << t->enamode_shift);
  105. davinci_writel(tcr, t->reg_base + TCR);
  106. /* reset counter to zero, set new period */
  107. davinci_writel(0, t->tim_reg);
  108. davinci_writel(t->period, t->prd_reg);
  109. /* Set enable mode */
  110. if (t->opts & TIMER_OPTS_ONESHOT) {
  111. tcr |= TCR_ENAMODE_ONESHOT << t->enamode_shift;
  112. } else if (t->opts & TIMER_OPTS_PERIODIC) {
  113. tcr |= TCR_ENAMODE_PERIODIC << t->enamode_shift;
  114. }
  115. davinci_writel(tcr, t->reg_base + TCR);
  116. return 0;
  117. }
  118. static inline u32 timer32_read(struct timer_s *t)
  119. {
  120. return davinci_readl(t->tim_reg);
  121. }
  122. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  123. {
  124. struct clock_event_device *evt = &clockevent_davinci;
  125. evt->event_handler(evt);
  126. return IRQ_HANDLED;
  127. }
  128. /* called when 32-bit counter wraps */
  129. static irqreturn_t freerun_interrupt(int irq, void *dev_id)
  130. {
  131. return IRQ_HANDLED;
  132. }
  133. static struct timer_s timers[] = {
  134. [TID_CLOCKEVENT] = {
  135. .name = "clockevent",
  136. .opts = TIMER_OPTS_DISABLED,
  137. .irqaction = {
  138. .flags = IRQF_DISABLED | IRQF_TIMER,
  139. .handler = timer_interrupt,
  140. }
  141. },
  142. [TID_CLOCKSOURCE] = {
  143. .name = "free-run counter",
  144. .period = ~0,
  145. .opts = TIMER_OPTS_PERIODIC,
  146. .irqaction = {
  147. .flags = IRQF_DISABLED | IRQF_TIMER,
  148. .handler = freerun_interrupt,
  149. }
  150. },
  151. };
  152. static void __init timer_init(void)
  153. {
  154. u32 bases[] = {DAVINCI_TIMER0_BASE, DAVINCI_TIMER1_BASE};
  155. int i;
  156. /* Global init of each 64-bit timer as a whole */
  157. for(i=0; i<2; i++) {
  158. u32 tgcr, base = bases[i];
  159. /* Disabled, Internal clock source */
  160. davinci_writel(0, base + TCR);
  161. /* reset both timers, no pre-scaler for timer34 */
  162. tgcr = 0;
  163. davinci_writel(tgcr, base + TGCR);
  164. /* Set both timers to unchained 32-bit */
  165. tgcr = TGCR_TIMMODE_32BIT_UNCHAINED << TGCR_TIMMODE_SHIFT;
  166. davinci_writel(tgcr, base + TGCR);
  167. /* Unreset timers */
  168. tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
  169. (TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
  170. davinci_writel(tgcr, base + TGCR);
  171. /* Init both counters to zero */
  172. davinci_writel(0, base + TIM12);
  173. davinci_writel(0, base + TIM34);
  174. }
  175. /* Init of each timer as a 32-bit timer */
  176. for (i=0; i< ARRAY_SIZE(timers); i++) {
  177. struct timer_s *t = &timers[i];
  178. if (t->name) {
  179. t->id = i;
  180. t->reg_base = (IS_TIMER1(t->id) ?
  181. DAVINCI_TIMER1_BASE : DAVINCI_TIMER0_BASE);
  182. if (IS_TIMER_BOT(t->id)) {
  183. t->enamode_shift = 6;
  184. t->tim_reg = t->reg_base + TIM12;
  185. t->prd_reg = t->reg_base + PRD12;
  186. } else {
  187. t->enamode_shift = 22;
  188. t->tim_reg = t->reg_base + TIM34;
  189. t->prd_reg = t->reg_base + PRD34;
  190. }
  191. /* Register interrupt */
  192. t->irqaction.name = t->name;
  193. t->irqaction.dev_id = (void *)t;
  194. if (t->irqaction.handler != NULL) {
  195. setup_irq(timer_irqs[t->id], &t->irqaction);
  196. }
  197. timer32_config(&timers[i]);
  198. }
  199. }
  200. }
  201. /*
  202. * clocksource
  203. */
  204. static cycle_t read_cycles(void)
  205. {
  206. struct timer_s *t = &timers[TID_CLOCKSOURCE];
  207. return (cycles_t)timer32_read(t);
  208. }
  209. static struct clocksource clocksource_davinci = {
  210. .name = "timer0_1",
  211. .rating = 300,
  212. .read = read_cycles,
  213. .mask = CLOCKSOURCE_MASK(32),
  214. .shift = 24,
  215. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  216. };
  217. /*
  218. * clockevent
  219. */
  220. static int davinci_set_next_event(unsigned long cycles,
  221. struct clock_event_device *evt)
  222. {
  223. struct timer_s *t = &timers[TID_CLOCKEVENT];
  224. t->period = cycles;
  225. timer32_config(t);
  226. return 0;
  227. }
  228. static void davinci_set_mode(enum clock_event_mode mode,
  229. struct clock_event_device *evt)
  230. {
  231. struct timer_s *t = &timers[TID_CLOCKEVENT];
  232. switch (mode) {
  233. case CLOCK_EVT_MODE_PERIODIC:
  234. t->period = CLOCK_TICK_RATE / (HZ);
  235. t->opts = TIMER_OPTS_PERIODIC;
  236. timer32_config(t);
  237. break;
  238. case CLOCK_EVT_MODE_ONESHOT:
  239. t->opts = TIMER_OPTS_ONESHOT;
  240. break;
  241. case CLOCK_EVT_MODE_UNUSED:
  242. case CLOCK_EVT_MODE_SHUTDOWN:
  243. t->opts = TIMER_OPTS_DISABLED;
  244. break;
  245. case CLOCK_EVT_MODE_RESUME:
  246. break;
  247. }
  248. }
  249. static struct clock_event_device clockevent_davinci = {
  250. .name = "timer0_0",
  251. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  252. .shift = 32,
  253. .set_next_event = davinci_set_next_event,
  254. .set_mode = davinci_set_mode,
  255. };
  256. static void __init davinci_timer_init(void)
  257. {
  258. static char err[] __initdata = KERN_ERR
  259. "%s: can't register clocksource!\n";
  260. /* init timer hw */
  261. timer_init();
  262. /* setup clocksource */
  263. clocksource_davinci.mult =
  264. clocksource_khz2mult(CLOCK_TICK_RATE/1000,
  265. clocksource_davinci.shift);
  266. if (clocksource_register(&clocksource_davinci))
  267. printk(err, clocksource_davinci.name);
  268. /* setup clockevent */
  269. clockevent_davinci.mult = div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC,
  270. clockevent_davinci.shift);
  271. clockevent_davinci.max_delta_ns =
  272. clockevent_delta2ns(0xfffffffe, &clockevent_davinci);
  273. clockevent_davinci.min_delta_ns =
  274. clockevent_delta2ns(1, &clockevent_davinci);
  275. clockevent_davinci.cpumask = cpumask_of_cpu(0);
  276. clockevents_register_device(&clockevent_davinci);
  277. }
  278. struct sys_timer davinci_timer = {
  279. .init = davinci_timer_init,
  280. };
  281. /* reset board using watchdog timer */
  282. void davinci_watchdog_reset(void) {
  283. u32 tgcr, wdtcr, base = DAVINCI_WDOG_BASE;
  284. /* disable, internal clock source */
  285. davinci_writel(0, base + TCR);
  286. /* reset timer, set mode to 64-bit watchdog, and unreset */
  287. tgcr = 0;
  288. davinci_writel(tgcr, base + TCR);
  289. tgcr = TGCR_TIMMODE_64BIT_WDOG << TGCR_TIMMODE_SHIFT;
  290. tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
  291. (TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
  292. davinci_writel(tgcr, base + TCR);
  293. /* clear counter and period regs */
  294. davinci_writel(0, base + TIM12);
  295. davinci_writel(0, base + TIM34);
  296. davinci_writel(0, base + PRD12);
  297. davinci_writel(0, base + PRD34);
  298. /* enable */
  299. wdtcr = davinci_readl(base + WDTCR);
  300. wdtcr |= WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT;
  301. davinci_writel(wdtcr, base + WDTCR);
  302. /* put watchdog in pre-active state */
  303. wdtcr = (WDTCR_WDKEY_SEQ0 << WDTCR_WDKEY_SHIFT) |
  304. (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
  305. davinci_writel(wdtcr, base + WDTCR);
  306. /* put watchdog in active state */
  307. wdtcr = (WDTCR_WDKEY_SEQ1 << WDTCR_WDKEY_SHIFT) |
  308. (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
  309. davinci_writel(wdtcr, base + WDTCR);
  310. /* write an invalid value to the WDKEY field to trigger
  311. * a watchdog reset */
  312. wdtcr = 0x00004000;
  313. davinci_writel(wdtcr, base + WDTCR);
  314. }