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