timer.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * linux/arch/arm/plat-nomadik/timer.c
  3. *
  4. * Copyright (C) 2008 STMicroelectronics
  5. * Copyright (C) 2010 Alessandro Rubini
  6. * Copyright (C) 2010 Linus Walleij for ST-Ericsson
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2, as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/io.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/clk.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/err.h>
  20. #include <linux/cnt32_to_63.h>
  21. #include <linux/timer.h>
  22. #include <asm/mach/time.h>
  23. #include <plat/mtu.h>
  24. void __iomem *mtu_base; /* Assigned by machine code */
  25. /*
  26. * Kernel assumes that sched_clock can be called early
  27. * but the MTU may not yet be initialized.
  28. */
  29. static cycle_t nmdk_read_timer_dummy(struct clocksource *cs)
  30. {
  31. return 0;
  32. }
  33. /* clocksource: MTU decrements, so we negate the value being read. */
  34. static cycle_t nmdk_read_timer(struct clocksource *cs)
  35. {
  36. return -readl(mtu_base + MTU_VAL(0));
  37. }
  38. static struct clocksource nmdk_clksrc = {
  39. .name = "mtu_0",
  40. .rating = 200,
  41. .read = nmdk_read_timer_dummy,
  42. .mask = CLOCKSOURCE_MASK(32),
  43. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  44. };
  45. /*
  46. * Override the global weak sched_clock symbol with this
  47. * local implementation which uses the clocksource to get some
  48. * better resolution when scheduling the kernel.
  49. *
  50. * Because the hardware timer period may be quite short
  51. * (32.3 secs on the 133 MHz MTU timer selection on ux500)
  52. * and because cnt32_to_63() needs to be called at least once per
  53. * half period to work properly, a kernel keepwarm() timer is set up
  54. * to ensure this requirement is always met.
  55. *
  56. * Also the sched_clock timer will wrap around at some point,
  57. * here we set it to run continously for a year.
  58. */
  59. #define SCHED_CLOCK_MIN_WRAP 3600*24*365
  60. static struct timer_list cnt32_to_63_keepwarm_timer;
  61. static u32 sched_mult;
  62. static u32 sched_shift;
  63. unsigned long long notrace sched_clock(void)
  64. {
  65. u64 cycles;
  66. if (unlikely(!mtu_base))
  67. return 0;
  68. cycles = cnt32_to_63(-readl(mtu_base + MTU_VAL(0)));
  69. /*
  70. * sched_mult is guaranteed to be even so will
  71. * shift out bit 63
  72. */
  73. return (cycles * sched_mult) >> sched_shift;
  74. }
  75. /* Just kick sched_clock every so often */
  76. static void cnt32_to_63_keepwarm(unsigned long data)
  77. {
  78. mod_timer(&cnt32_to_63_keepwarm_timer, round_jiffies(jiffies + data));
  79. (void) sched_clock();
  80. }
  81. /*
  82. * Set up a timer to keep sched_clock():s 32_to_63 algorithm warm
  83. * once in half a 32bit timer wrap interval.
  84. */
  85. static void __init nmdk_sched_clock_init(unsigned long rate)
  86. {
  87. u32 v;
  88. unsigned long delta;
  89. u64 days;
  90. /* Find the apropriate mult and shift factors */
  91. clocks_calc_mult_shift(&sched_mult, &sched_shift,
  92. rate, NSEC_PER_SEC, SCHED_CLOCK_MIN_WRAP);
  93. /* We need to multiply by an even number to get rid of bit 63 */
  94. if (sched_mult & 1)
  95. sched_mult++;
  96. /* Let's see what we get, take max counter and scale it */
  97. days = (0xFFFFFFFFFFFFFFFFLLU * sched_mult) >> sched_shift;
  98. do_div(days, NSEC_PER_SEC);
  99. do_div(days, (3600*24));
  100. pr_info("sched_clock: using %d bits @ %lu Hz wrap in %lu days\n",
  101. (64 - sched_shift), rate, (unsigned long) days);
  102. /*
  103. * Program a timer to kick us at half 32bit wraparound
  104. * Formula: seconds per wrap = (2^32) / f
  105. */
  106. v = 0xFFFFFFFFUL / rate;
  107. /* We want half of the wrap time to keep cnt32_to_63 warm */
  108. v /= 2;
  109. pr_debug("sched_clock: prescaled timer rate: %lu Hz, "
  110. "initialize keepwarm timer every %d seconds\n", rate, v);
  111. /* Convert seconds to jiffies */
  112. delta = msecs_to_jiffies(v*1000);
  113. setup_timer(&cnt32_to_63_keepwarm_timer, cnt32_to_63_keepwarm, delta);
  114. mod_timer(&cnt32_to_63_keepwarm_timer, round_jiffies(jiffies + delta));
  115. }
  116. /* Clockevent device: use one-shot mode */
  117. static void nmdk_clkevt_mode(enum clock_event_mode mode,
  118. struct clock_event_device *dev)
  119. {
  120. u32 cr;
  121. switch (mode) {
  122. case CLOCK_EVT_MODE_PERIODIC:
  123. pr_err("%s: periodic mode not supported\n", __func__);
  124. break;
  125. case CLOCK_EVT_MODE_ONESHOT:
  126. /* Load highest value, enable device, enable interrupts */
  127. cr = readl(mtu_base + MTU_CR(1));
  128. writel(0, mtu_base + MTU_LR(1));
  129. writel(cr | MTU_CRn_ENA, mtu_base + MTU_CR(1));
  130. writel(1 << 1, mtu_base + MTU_IMSC);
  131. break;
  132. case CLOCK_EVT_MODE_SHUTDOWN:
  133. case CLOCK_EVT_MODE_UNUSED:
  134. /* disable irq */
  135. writel(0, mtu_base + MTU_IMSC);
  136. /* disable timer */
  137. cr = readl(mtu_base + MTU_CR(1));
  138. cr &= ~MTU_CRn_ENA;
  139. writel(cr, mtu_base + MTU_CR(1));
  140. /* load some high default value */
  141. writel(0xffffffff, mtu_base + MTU_LR(1));
  142. break;
  143. case CLOCK_EVT_MODE_RESUME:
  144. break;
  145. }
  146. }
  147. static int nmdk_clkevt_next(unsigned long evt, struct clock_event_device *ev)
  148. {
  149. /* writing the value has immediate effect */
  150. writel(evt, mtu_base + MTU_LR(1));
  151. return 0;
  152. }
  153. static struct clock_event_device nmdk_clkevt = {
  154. .name = "mtu_1",
  155. .features = CLOCK_EVT_FEAT_ONESHOT,
  156. .rating = 200,
  157. .set_mode = nmdk_clkevt_mode,
  158. .set_next_event = nmdk_clkevt_next,
  159. };
  160. /*
  161. * IRQ Handler for timer 1 of the MTU block.
  162. */
  163. static irqreturn_t nmdk_timer_interrupt(int irq, void *dev_id)
  164. {
  165. struct clock_event_device *evdev = dev_id;
  166. writel(1 << 1, mtu_base + MTU_ICR); /* Interrupt clear reg */
  167. evdev->event_handler(evdev);
  168. return IRQ_HANDLED;
  169. }
  170. static struct irqaction nmdk_timer_irq = {
  171. .name = "Nomadik Timer Tick",
  172. .flags = IRQF_DISABLED | IRQF_TIMER,
  173. .handler = nmdk_timer_interrupt,
  174. .dev_id = &nmdk_clkevt,
  175. };
  176. void __init nmdk_timer_init(void)
  177. {
  178. unsigned long rate;
  179. struct clk *clk0;
  180. u32 cr = MTU_CRn_32BITS;
  181. clk0 = clk_get_sys("mtu0", NULL);
  182. BUG_ON(IS_ERR(clk0));
  183. clk_enable(clk0);
  184. /*
  185. * Tick rate is 2.4MHz for Nomadik and 2.4Mhz, 100MHz or 133 MHz
  186. * for ux500.
  187. * Use a divide-by-16 counter if the tick rate is more than 32MHz.
  188. * At 32 MHz, the timer (with 32 bit counter) can be programmed
  189. * to wake-up at a max 127s a head in time. Dividing a 2.4 MHz timer
  190. * with 16 gives too low timer resolution.
  191. */
  192. rate = clk_get_rate(clk0);
  193. if (rate > 32000000) {
  194. rate /= 16;
  195. cr |= MTU_CRn_PRESCALE_16;
  196. } else {
  197. cr |= MTU_CRn_PRESCALE_1;
  198. }
  199. clocksource_calc_mult_shift(&nmdk_clksrc, rate, MTU_MIN_RANGE);
  200. /* Timer 0 is the free running clocksource */
  201. writel(cr, mtu_base + MTU_CR(0));
  202. writel(0, mtu_base + MTU_LR(0));
  203. writel(0, mtu_base + MTU_BGLR(0));
  204. writel(cr | MTU_CRn_ENA, mtu_base + MTU_CR(0));
  205. /* Now the clock source is ready */
  206. nmdk_clksrc.read = nmdk_read_timer;
  207. if (clocksource_register(&nmdk_clksrc))
  208. pr_err("timer: failed to initialize clock source %s\n",
  209. nmdk_clksrc.name);
  210. nmdk_sched_clock_init(rate);
  211. /* Timer 1 is used for events */
  212. clockevents_calc_mult_shift(&nmdk_clkevt, rate, MTU_MIN_RANGE);
  213. writel(cr | MTU_CRn_ONESHOT, mtu_base + MTU_CR(1)); /* off, currently */
  214. nmdk_clkevt.max_delta_ns =
  215. clockevent_delta2ns(0xffffffff, &nmdk_clkevt);
  216. nmdk_clkevt.min_delta_ns =
  217. clockevent_delta2ns(0x00000002, &nmdk_clkevt);
  218. nmdk_clkevt.cpumask = cpumask_of(0);
  219. /* Register irq and clockevents */
  220. setup_irq(IRQ_MTU0, &nmdk_timer_irq);
  221. clockevents_register_device(&nmdk_clkevt);
  222. }