time.c 9.7 KB

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