timer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 <asm/mach/time.h>
  21. #include <asm/sched_clock.h>
  22. /*
  23. * Guaranteed runtime conversion range in seconds for
  24. * the clocksource and clockevent.
  25. */
  26. #define MTU_MIN_RANGE 4
  27. /*
  28. * The MTU device hosts four different counters, with 4 set of
  29. * registers. These are register names.
  30. */
  31. #define MTU_IMSC 0x00 /* Interrupt mask set/clear */
  32. #define MTU_RIS 0x04 /* Raw interrupt status */
  33. #define MTU_MIS 0x08 /* Masked interrupt status */
  34. #define MTU_ICR 0x0C /* Interrupt clear register */
  35. /* per-timer registers take 0..3 as argument */
  36. #define MTU_LR(x) (0x10 + 0x10 * (x) + 0x00) /* Load value */
  37. #define MTU_VAL(x) (0x10 + 0x10 * (x) + 0x04) /* Current value */
  38. #define MTU_CR(x) (0x10 + 0x10 * (x) + 0x08) /* Control reg */
  39. #define MTU_BGLR(x) (0x10 + 0x10 * (x) + 0x0c) /* At next overflow */
  40. /* bits for the control register */
  41. #define MTU_CRn_ENA 0x80
  42. #define MTU_CRn_PERIODIC 0x40 /* if 0 = free-running */
  43. #define MTU_CRn_PRESCALE_MASK 0x0c
  44. #define MTU_CRn_PRESCALE_1 0x00
  45. #define MTU_CRn_PRESCALE_16 0x04
  46. #define MTU_CRn_PRESCALE_256 0x08
  47. #define MTU_CRn_32BITS 0x02
  48. #define MTU_CRn_ONESHOT 0x01 /* if 0 = wraps reloading from BGLR*/
  49. /* Other registers are usual amba/primecell registers, currently not used */
  50. #define MTU_ITCR 0xff0
  51. #define MTU_ITOP 0xff4
  52. #define MTU_PERIPH_ID0 0xfe0
  53. #define MTU_PERIPH_ID1 0xfe4
  54. #define MTU_PERIPH_ID2 0xfe8
  55. #define MTU_PERIPH_ID3 0xfeC
  56. #define MTU_PCELL0 0xff0
  57. #define MTU_PCELL1 0xff4
  58. #define MTU_PCELL2 0xff8
  59. #define MTU_PCELL3 0xffC
  60. static bool clkevt_periodic;
  61. static u32 clk_prescale;
  62. static u32 nmdk_cycle; /* write-once */
  63. void __iomem *mtu_base; /* Assigned by machine code */
  64. #ifdef CONFIG_NOMADIK_MTU_SCHED_CLOCK
  65. /*
  66. * Override the global weak sched_clock symbol with this
  67. * local implementation which uses the clocksource to get some
  68. * better resolution when scheduling the kernel.
  69. */
  70. static u32 notrace nomadik_read_sched_clock(void)
  71. {
  72. if (unlikely(!mtu_base))
  73. return 0;
  74. return -readl(mtu_base + MTU_VAL(0));
  75. }
  76. #endif
  77. /* Clockevent device: use one-shot mode */
  78. static int nmdk_clkevt_next(unsigned long evt, struct clock_event_device *ev)
  79. {
  80. writel(1 << 1, mtu_base + MTU_IMSC);
  81. writel(evt, mtu_base + MTU_LR(1));
  82. /* Load highest value, enable device, enable interrupts */
  83. writel(MTU_CRn_ONESHOT | clk_prescale |
  84. MTU_CRn_32BITS | MTU_CRn_ENA,
  85. mtu_base + MTU_CR(1));
  86. return 0;
  87. }
  88. void nmdk_clkevt_reset(void)
  89. {
  90. if (clkevt_periodic) {
  91. /* Timer: configure load and background-load, and fire it up */
  92. writel(nmdk_cycle, mtu_base + MTU_LR(1));
  93. writel(nmdk_cycle, mtu_base + MTU_BGLR(1));
  94. writel(MTU_CRn_PERIODIC | clk_prescale |
  95. MTU_CRn_32BITS | MTU_CRn_ENA,
  96. mtu_base + MTU_CR(1));
  97. writel(1 << 1, mtu_base + MTU_IMSC);
  98. } else {
  99. /* Generate an interrupt to start the clockevent again */
  100. (void) nmdk_clkevt_next(nmdk_cycle, NULL);
  101. }
  102. }
  103. static void nmdk_clkevt_mode(enum clock_event_mode mode,
  104. struct clock_event_device *dev)
  105. {
  106. switch (mode) {
  107. case CLOCK_EVT_MODE_PERIODIC:
  108. clkevt_periodic = true;
  109. nmdk_clkevt_reset();
  110. break;
  111. case CLOCK_EVT_MODE_ONESHOT:
  112. clkevt_periodic = false;
  113. break;
  114. case CLOCK_EVT_MODE_SHUTDOWN:
  115. case CLOCK_EVT_MODE_UNUSED:
  116. writel(0, mtu_base + MTU_IMSC);
  117. /* disable timer */
  118. writel(0, mtu_base + MTU_CR(1));
  119. /* load some high default value */
  120. writel(0xffffffff, mtu_base + MTU_LR(1));
  121. break;
  122. case CLOCK_EVT_MODE_RESUME:
  123. break;
  124. }
  125. }
  126. static struct clock_event_device nmdk_clkevt = {
  127. .name = "mtu_1",
  128. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  129. .rating = 200,
  130. .set_mode = nmdk_clkevt_mode,
  131. .set_next_event = nmdk_clkevt_next,
  132. };
  133. /*
  134. * IRQ Handler for timer 1 of the MTU block.
  135. */
  136. static irqreturn_t nmdk_timer_interrupt(int irq, void *dev_id)
  137. {
  138. struct clock_event_device *evdev = dev_id;
  139. writel(1 << 1, mtu_base + MTU_ICR); /* Interrupt clear reg */
  140. evdev->event_handler(evdev);
  141. return IRQ_HANDLED;
  142. }
  143. static struct irqaction nmdk_timer_irq = {
  144. .name = "Nomadik Timer Tick",
  145. .flags = IRQF_DISABLED | IRQF_TIMER,
  146. .handler = nmdk_timer_interrupt,
  147. .dev_id = &nmdk_clkevt,
  148. };
  149. void nmdk_clksrc_reset(void)
  150. {
  151. /* Disable */
  152. writel(0, mtu_base + MTU_CR(0));
  153. /* ClockSource: configure load and background-load, and fire it up */
  154. writel(nmdk_cycle, mtu_base + MTU_LR(0));
  155. writel(nmdk_cycle, mtu_base + MTU_BGLR(0));
  156. writel(clk_prescale | MTU_CRn_32BITS | MTU_CRn_ENA,
  157. mtu_base + MTU_CR(0));
  158. }
  159. void __init nmdk_timer_init(void)
  160. {
  161. unsigned long rate;
  162. struct clk *clk0;
  163. clk0 = clk_get_sys("mtu0", NULL);
  164. BUG_ON(IS_ERR(clk0));
  165. clk_enable(clk0);
  166. /*
  167. * Tick rate is 2.4MHz for Nomadik and 2.4Mhz, 100MHz or 133 MHz
  168. * for ux500.
  169. * Use a divide-by-16 counter if the tick rate is more than 32MHz.
  170. * At 32 MHz, the timer (with 32 bit counter) can be programmed
  171. * to wake-up at a max 127s a head in time. Dividing a 2.4 MHz timer
  172. * with 16 gives too low timer resolution.
  173. */
  174. rate = clk_get_rate(clk0);
  175. if (rate > 32000000) {
  176. rate /= 16;
  177. clk_prescale = MTU_CRn_PRESCALE_16;
  178. } else {
  179. clk_prescale = MTU_CRn_PRESCALE_1;
  180. }
  181. nmdk_cycle = (rate + HZ/2) / HZ;
  182. /* Timer 0 is the free running clocksource */
  183. nmdk_clksrc_reset();
  184. if (clocksource_mmio_init(mtu_base + MTU_VAL(0), "mtu_0",
  185. rate, 200, 32, clocksource_mmio_readl_down))
  186. pr_err("timer: failed to initialize clock source %s\n",
  187. "mtu_0");
  188. #ifdef CONFIG_NOMADIK_MTU_SCHED_CLOCK
  189. setup_sched_clock(nomadik_read_sched_clock, 32, rate);
  190. #endif
  191. /* Timer 1 is used for events */
  192. clockevents_calc_mult_shift(&nmdk_clkevt, rate, MTU_MIN_RANGE);
  193. nmdk_clkevt.max_delta_ns =
  194. clockevent_delta2ns(0xffffffff, &nmdk_clkevt);
  195. nmdk_clkevt.min_delta_ns =
  196. clockevent_delta2ns(0x00000002, &nmdk_clkevt);
  197. nmdk_clkevt.cpumask = cpumask_of(0);
  198. /* Register irq and clockevents */
  199. setup_irq(IRQ_MTU0, &nmdk_timer_irq);
  200. clockevents_register_device(&nmdk_clkevt);
  201. }