time.c 11 KB

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