time.c 10 KB

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