time.c 10 KB

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