time.c 10 KB

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