time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/io.h>
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/platform_device.h>
  21. #include <mach/hardware.h>
  22. #include <asm/mach/irq.h>
  23. #include <asm/mach/time.h>
  24. #include <mach/cputype.h>
  25. #include <mach/time.h>
  26. #include "clock.h"
  27. static struct clock_event_device clockevent_davinci;
  28. static unsigned int davinci_clock_tick_rate;
  29. /*
  30. * This driver configures the 2 64-bit count-up timers as 4 independent
  31. * 32-bit count-up timers used as follows:
  32. */
  33. enum {
  34. TID_CLOCKEVENT,
  35. TID_CLOCKSOURCE,
  36. };
  37. /* Timer register offsets */
  38. #define PID12 0x0
  39. #define TIM12 0x10
  40. #define TIM34 0x14
  41. #define PRD12 0x18
  42. #define PRD34 0x1c
  43. #define TCR 0x20
  44. #define TGCR 0x24
  45. #define WDTCR 0x28
  46. /* Offsets of the 8 compare registers */
  47. #define CMP12_0 0x60
  48. #define CMP12_1 0x64
  49. #define CMP12_2 0x68
  50. #define CMP12_3 0x6c
  51. #define CMP12_4 0x70
  52. #define CMP12_5 0x74
  53. #define CMP12_6 0x78
  54. #define CMP12_7 0x7c
  55. /* Timer register bitfields */
  56. #define TCR_ENAMODE_DISABLE 0x0
  57. #define TCR_ENAMODE_ONESHOT 0x1
  58. #define TCR_ENAMODE_PERIODIC 0x2
  59. #define TCR_ENAMODE_MASK 0x3
  60. #define TGCR_TIMMODE_SHIFT 2
  61. #define TGCR_TIMMODE_64BIT_GP 0x0
  62. #define TGCR_TIMMODE_32BIT_UNCHAINED 0x1
  63. #define TGCR_TIMMODE_64BIT_WDOG 0x2
  64. #define TGCR_TIMMODE_32BIT_CHAINED 0x3
  65. #define TGCR_TIM12RS_SHIFT 0
  66. #define TGCR_TIM34RS_SHIFT 1
  67. #define TGCR_RESET 0x0
  68. #define TGCR_UNRESET 0x1
  69. #define TGCR_RESET_MASK 0x3
  70. #define WDTCR_WDEN_SHIFT 14
  71. #define WDTCR_WDEN_DISABLE 0x0
  72. #define WDTCR_WDEN_ENABLE 0x1
  73. #define WDTCR_WDKEY_SHIFT 16
  74. #define WDTCR_WDKEY_SEQ0 0xa5c6
  75. #define WDTCR_WDKEY_SEQ1 0xda7e
  76. struct timer_s {
  77. char *name;
  78. unsigned int id;
  79. unsigned long period;
  80. unsigned long opts;
  81. unsigned long flags;
  82. void __iomem *base;
  83. unsigned long tim_off;
  84. unsigned long prd_off;
  85. unsigned long enamode_shift;
  86. struct irqaction irqaction;
  87. };
  88. static struct timer_s timers[];
  89. /* values for 'opts' field of struct timer_s */
  90. #define TIMER_OPTS_DISABLED 0x01
  91. #define TIMER_OPTS_ONESHOT 0x02
  92. #define TIMER_OPTS_PERIODIC 0x04
  93. #define TIMER_OPTS_STATE_MASK 0x07
  94. #define TIMER_OPTS_USE_COMPARE 0x80000000
  95. #define USING_COMPARE(t) ((t)->opts & TIMER_OPTS_USE_COMPARE)
  96. static char *id_to_name[] = {
  97. [T0_BOT] = "timer0_0",
  98. [T0_TOP] = "timer0_1",
  99. [T1_BOT] = "timer1_0",
  100. [T1_TOP] = "timer1_1",
  101. };
  102. static int timer32_config(struct timer_s *t)
  103. {
  104. u32 tcr;
  105. struct davinci_soc_info *soc_info = &davinci_soc_info;
  106. if (USING_COMPARE(t)) {
  107. struct davinci_timer_instance *dtip =
  108. soc_info->timer_info->timers;
  109. int event_timer = ID_TO_TIMER(timers[TID_CLOCKEVENT].id);
  110. /*
  111. * Next interrupt should be the current time reg value plus
  112. * the new period (using 32-bit unsigned addition/wrapping
  113. * to 0 on overflow). This assumes that the clocksource
  114. * is setup to count to 2^32-1 before wrapping around to 0.
  115. */
  116. __raw_writel(__raw_readl(t->base + t->tim_off) + t->period,
  117. t->base + dtip[event_timer].cmp_off);
  118. } else {
  119. tcr = __raw_readl(t->base + TCR);
  120. /* disable timer */
  121. tcr &= ~(TCR_ENAMODE_MASK << t->enamode_shift);
  122. __raw_writel(tcr, t->base + TCR);
  123. /* reset counter to zero, set new period */
  124. __raw_writel(0, t->base + t->tim_off);
  125. __raw_writel(t->period, t->base + t->prd_off);
  126. /* Set enable mode */
  127. if (t->opts & TIMER_OPTS_ONESHOT)
  128. tcr |= TCR_ENAMODE_ONESHOT << t->enamode_shift;
  129. else if (t->opts & TIMER_OPTS_PERIODIC)
  130. tcr |= TCR_ENAMODE_PERIODIC << t->enamode_shift;
  131. __raw_writel(tcr, t->base + TCR);
  132. }
  133. return 0;
  134. }
  135. static inline u32 timer32_read(struct timer_s *t)
  136. {
  137. return __raw_readl(t->base + t->tim_off);
  138. }
  139. static irqreturn_t timer_interrupt(int irq, void *dev_id)
  140. {
  141. struct clock_event_device *evt = &clockevent_davinci;
  142. evt->event_handler(evt);
  143. return IRQ_HANDLED;
  144. }
  145. /* called when 32-bit counter wraps */
  146. static irqreturn_t freerun_interrupt(int irq, void *dev_id)
  147. {
  148. return IRQ_HANDLED;
  149. }
  150. static 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. static void __init timer_init(void)
  170. {
  171. struct davinci_soc_info *soc_info = &davinci_soc_info;
  172. struct davinci_timer_instance *dtip = soc_info->timer_info->timers;
  173. void __iomem *base[2];
  174. int i;
  175. /* Global init of each 64-bit timer as a whole */
  176. for(i=0; i<2; i++) {
  177. u32 tgcr;
  178. base[i] = ioremap(dtip[i].base, SZ_4K);
  179. if (WARN_ON(!base[i]))
  180. continue;
  181. /* Disabled, Internal clock source */
  182. __raw_writel(0, base[i] + TCR);
  183. /* reset both timers, no pre-scaler for timer34 */
  184. tgcr = 0;
  185. __raw_writel(tgcr, base[i] + TGCR);
  186. /* Set both timers to unchained 32-bit */
  187. tgcr = TGCR_TIMMODE_32BIT_UNCHAINED << TGCR_TIMMODE_SHIFT;
  188. __raw_writel(tgcr, base[i] + TGCR);
  189. /* Unreset timers */
  190. tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
  191. (TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
  192. __raw_writel(tgcr, base[i] + TGCR);
  193. /* Init both counters to zero */
  194. __raw_writel(0, base[i] + TIM12);
  195. __raw_writel(0, base[i] + TIM34);
  196. }
  197. /* Init of each timer as a 32-bit timer */
  198. for (i=0; i< ARRAY_SIZE(timers); i++) {
  199. struct timer_s *t = &timers[i];
  200. int timer = ID_TO_TIMER(t->id);
  201. u32 irq;
  202. t->base = base[timer];
  203. if (!t->base)
  204. continue;
  205. if (IS_TIMER_BOT(t->id)) {
  206. t->enamode_shift = 6;
  207. t->tim_off = TIM12;
  208. t->prd_off = PRD12;
  209. irq = dtip[timer].bottom_irq;
  210. } else {
  211. t->enamode_shift = 22;
  212. t->tim_off = TIM34;
  213. t->prd_off = PRD34;
  214. irq = dtip[timer].top_irq;
  215. }
  216. /* Register interrupt */
  217. t->irqaction.name = t->name;
  218. t->irqaction.dev_id = (void *)t;
  219. if (t->irqaction.handler != NULL) {
  220. irq = USING_COMPARE(t) ? dtip[i].cmp_irq : irq;
  221. setup_irq(irq, &t->irqaction);
  222. }
  223. }
  224. }
  225. /*
  226. * clocksource
  227. */
  228. static cycle_t read_cycles(struct clocksource *cs)
  229. {
  230. struct timer_s *t = &timers[TID_CLOCKSOURCE];
  231. return (cycles_t)timer32_read(t);
  232. }
  233. static struct clocksource clocksource_davinci = {
  234. .rating = 300,
  235. .read = read_cycles,
  236. .mask = CLOCKSOURCE_MASK(32),
  237. .shift = 24,
  238. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  239. };
  240. /*
  241. * clockevent
  242. */
  243. static int davinci_set_next_event(unsigned long cycles,
  244. struct clock_event_device *evt)
  245. {
  246. struct timer_s *t = &timers[TID_CLOCKEVENT];
  247. t->period = cycles;
  248. timer32_config(t);
  249. return 0;
  250. }
  251. static void davinci_set_mode(enum clock_event_mode mode,
  252. struct clock_event_device *evt)
  253. {
  254. struct timer_s *t = &timers[TID_CLOCKEVENT];
  255. switch (mode) {
  256. case CLOCK_EVT_MODE_PERIODIC:
  257. t->period = davinci_clock_tick_rate / (HZ);
  258. t->opts &= ~TIMER_OPTS_STATE_MASK;
  259. t->opts |= TIMER_OPTS_PERIODIC;
  260. timer32_config(t);
  261. break;
  262. case CLOCK_EVT_MODE_ONESHOT:
  263. t->opts &= ~TIMER_OPTS_STATE_MASK;
  264. t->opts |= TIMER_OPTS_ONESHOT;
  265. break;
  266. case CLOCK_EVT_MODE_UNUSED:
  267. case CLOCK_EVT_MODE_SHUTDOWN:
  268. t->opts &= ~TIMER_OPTS_STATE_MASK;
  269. t->opts |= TIMER_OPTS_DISABLED;
  270. break;
  271. case CLOCK_EVT_MODE_RESUME:
  272. break;
  273. }
  274. }
  275. static struct clock_event_device clockevent_davinci = {
  276. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  277. .shift = 32,
  278. .set_next_event = davinci_set_next_event,
  279. .set_mode = davinci_set_mode,
  280. };
  281. static void __init davinci_timer_init(void)
  282. {
  283. struct clk *timer_clk;
  284. struct davinci_soc_info *soc_info = &davinci_soc_info;
  285. unsigned int clockevent_id;
  286. unsigned int clocksource_id;
  287. static char err[] __initdata = KERN_ERR
  288. "%s: can't register clocksource!\n";
  289. int i;
  290. clockevent_id = soc_info->timer_info->clockevent_id;
  291. clocksource_id = soc_info->timer_info->clocksource_id;
  292. timers[TID_CLOCKEVENT].id = clockevent_id;
  293. timers[TID_CLOCKSOURCE].id = clocksource_id;
  294. /*
  295. * If using same timer for both clock events & clocksource,
  296. * a compare register must be used to generate an event interrupt.
  297. * This is equivalent to a oneshot timer only (not periodic).
  298. */
  299. if (clockevent_id == clocksource_id) {
  300. struct davinci_timer_instance *dtip =
  301. soc_info->timer_info->timers;
  302. int event_timer = ID_TO_TIMER(clockevent_id);
  303. /* Only bottom timers can use compare regs */
  304. if (IS_TIMER_TOP(clockevent_id))
  305. pr_warning("davinci_timer_init: Invalid use"
  306. " of system timers. Results unpredictable.\n");
  307. else if ((dtip[event_timer].cmp_off == 0)
  308. || (dtip[event_timer].cmp_irq == 0))
  309. pr_warning("davinci_timer_init: Invalid timer instance"
  310. " setup. Results unpredictable.\n");
  311. else {
  312. timers[TID_CLOCKEVENT].opts |= TIMER_OPTS_USE_COMPARE;
  313. clockevent_davinci.features = CLOCK_EVT_FEAT_ONESHOT;
  314. }
  315. }
  316. timer_clk = clk_get(NULL, "timer0");
  317. BUG_ON(IS_ERR(timer_clk));
  318. clk_enable(timer_clk);
  319. /* init timer hw */
  320. timer_init();
  321. davinci_clock_tick_rate = clk_get_rate(timer_clk);
  322. /* setup clocksource */
  323. clocksource_davinci.name = id_to_name[clocksource_id];
  324. clocksource_davinci.mult =
  325. clocksource_khz2mult(davinci_clock_tick_rate/1000,
  326. clocksource_davinci.shift);
  327. if (clocksource_register(&clocksource_davinci))
  328. printk(err, clocksource_davinci.name);
  329. /* setup clockevent */
  330. clockevent_davinci.name = id_to_name[timers[TID_CLOCKEVENT].id];
  331. clockevent_davinci.mult = div_sc(davinci_clock_tick_rate, NSEC_PER_SEC,
  332. clockevent_davinci.shift);
  333. clockevent_davinci.max_delta_ns =
  334. clockevent_delta2ns(0xfffffffe, &clockevent_davinci);
  335. clockevent_davinci.min_delta_ns = 50000; /* 50 usec */
  336. clockevent_davinci.cpumask = cpumask_of(0);
  337. clockevents_register_device(&clockevent_davinci);
  338. for (i=0; i< ARRAY_SIZE(timers); i++)
  339. timer32_config(&timers[i]);
  340. }
  341. struct sys_timer davinci_timer = {
  342. .init = davinci_timer_init,
  343. };
  344. /* reset board using watchdog timer */
  345. void davinci_watchdog_reset(struct platform_device *pdev)
  346. {
  347. u32 tgcr, wdtcr;
  348. void __iomem *base;
  349. struct clk *wd_clk;
  350. base = ioremap(pdev->resource[0].start, SZ_4K);
  351. if (WARN_ON(!base))
  352. return;
  353. wd_clk = clk_get(&pdev->dev, NULL);
  354. if (WARN_ON(IS_ERR(wd_clk)))
  355. return;
  356. clk_enable(wd_clk);
  357. /* disable, internal clock source */
  358. __raw_writel(0, base + TCR);
  359. /* reset timer, set mode to 64-bit watchdog, and unreset */
  360. tgcr = 0;
  361. __raw_writel(tgcr, base + TGCR);
  362. tgcr = TGCR_TIMMODE_64BIT_WDOG << TGCR_TIMMODE_SHIFT;
  363. tgcr |= (TGCR_UNRESET << TGCR_TIM12RS_SHIFT) |
  364. (TGCR_UNRESET << TGCR_TIM34RS_SHIFT);
  365. __raw_writel(tgcr, base + TGCR);
  366. /* clear counter and period regs */
  367. __raw_writel(0, base + TIM12);
  368. __raw_writel(0, base + TIM34);
  369. __raw_writel(0, base + PRD12);
  370. __raw_writel(0, base + PRD34);
  371. /* put watchdog in pre-active state */
  372. wdtcr = __raw_readl(base + WDTCR);
  373. wdtcr = (WDTCR_WDKEY_SEQ0 << WDTCR_WDKEY_SHIFT) |
  374. (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
  375. __raw_writel(wdtcr, base + WDTCR);
  376. /* put watchdog in active state */
  377. wdtcr = (WDTCR_WDKEY_SEQ1 << WDTCR_WDKEY_SHIFT) |
  378. (WDTCR_WDEN_ENABLE << WDTCR_WDEN_SHIFT);
  379. __raw_writel(wdtcr, base + WDTCR);
  380. /* write an invalid value to the WDKEY field to trigger
  381. * a watchdog reset */
  382. wdtcr = 0x00004000;
  383. __raw_writel(wdtcr, base + WDTCR);
  384. }