time.c 9.7 KB

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