time.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * arch/arm/plat-orion/time.c
  3. *
  4. * Marvell Orion SoC timer handling.
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. *
  10. * Timer 0 is used as free-running clocksource, while timer 1 is
  11. * used as clock_event_device.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/cnt32_to_63.h>
  16. #include <linux/timer.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irq.h>
  20. #include <asm/mach/time.h>
  21. #include <mach/bridge-regs.h>
  22. #include <mach/hardware.h>
  23. /*
  24. * Number of timer ticks per jiffy.
  25. */
  26. static u32 ticks_per_jiffy;
  27. /*
  28. * Timer block registers.
  29. */
  30. #define TIMER_CTRL (TIMER_VIRT_BASE + 0x0000)
  31. #define TIMER0_EN 0x0001
  32. #define TIMER0_RELOAD_EN 0x0002
  33. #define TIMER1_EN 0x0004
  34. #define TIMER1_RELOAD_EN 0x0008
  35. #define TIMER0_RELOAD (TIMER_VIRT_BASE + 0x0010)
  36. #define TIMER0_VAL (TIMER_VIRT_BASE + 0x0014)
  37. #define TIMER1_RELOAD (TIMER_VIRT_BASE + 0x0018)
  38. #define TIMER1_VAL (TIMER_VIRT_BASE + 0x001c)
  39. /*
  40. * Orion's sched_clock implementation. It has a resolution of
  41. * at least 7.5ns (133MHz TCLK) and a maximum value of 834 days.
  42. *
  43. * Because the hardware timer period is quite short (21 secs if
  44. * 200MHz TCLK) and because cnt32_to_63() needs to be called at
  45. * least once per half period to work properly, a kernel timer is
  46. * set up to ensure this requirement is always met.
  47. */
  48. #define TCLK2NS_SCALE_FACTOR 8
  49. static unsigned long tclk2ns_scale;
  50. unsigned long long notrace sched_clock(void)
  51. {
  52. unsigned long long v = cnt32_to_63(0xffffffff - readl(TIMER0_VAL));
  53. return (v * tclk2ns_scale) >> TCLK2NS_SCALE_FACTOR;
  54. }
  55. static struct timer_list cnt32_to_63_keepwarm_timer;
  56. static void cnt32_to_63_keepwarm(unsigned long data)
  57. {
  58. mod_timer(&cnt32_to_63_keepwarm_timer, round_jiffies(jiffies + data));
  59. (void) sched_clock();
  60. }
  61. static void __init setup_sched_clock(unsigned long tclk)
  62. {
  63. unsigned long long v;
  64. unsigned long data;
  65. v = NSEC_PER_SEC;
  66. v <<= TCLK2NS_SCALE_FACTOR;
  67. v += tclk/2;
  68. do_div(v, tclk);
  69. /*
  70. * We want an even value to automatically clear the top bit
  71. * returned by cnt32_to_63() without an additional run time
  72. * instruction. So if the LSB is 1 then round it up.
  73. */
  74. if (v & 1)
  75. v++;
  76. tclk2ns_scale = v;
  77. data = (0xffffffffUL / tclk / 2 - 2) * HZ;
  78. setup_timer(&cnt32_to_63_keepwarm_timer, cnt32_to_63_keepwarm, data);
  79. mod_timer(&cnt32_to_63_keepwarm_timer, round_jiffies(jiffies + data));
  80. }
  81. /*
  82. * Clocksource handling.
  83. */
  84. static cycle_t orion_clksrc_read(struct clocksource *cs)
  85. {
  86. return 0xffffffff - readl(TIMER0_VAL);
  87. }
  88. static struct clocksource orion_clksrc = {
  89. .name = "orion_clocksource",
  90. .rating = 300,
  91. .read = orion_clksrc_read,
  92. .mask = CLOCKSOURCE_MASK(32),
  93. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  94. };
  95. /*
  96. * Clockevent handling.
  97. */
  98. static int
  99. orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev)
  100. {
  101. unsigned long flags;
  102. u32 u;
  103. if (delta == 0)
  104. return -ETIME;
  105. local_irq_save(flags);
  106. /*
  107. * Clear and enable clockevent timer interrupt.
  108. */
  109. writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
  110. u = readl(BRIDGE_MASK);
  111. u |= BRIDGE_INT_TIMER1;
  112. writel(u, BRIDGE_MASK);
  113. /*
  114. * Setup new clockevent timer value.
  115. */
  116. writel(delta, TIMER1_VAL);
  117. /*
  118. * Enable the timer.
  119. */
  120. u = readl(TIMER_CTRL);
  121. u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN;
  122. writel(u, TIMER_CTRL);
  123. local_irq_restore(flags);
  124. return 0;
  125. }
  126. static void
  127. orion_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  128. {
  129. unsigned long flags;
  130. u32 u;
  131. local_irq_save(flags);
  132. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  133. /*
  134. * Setup timer to fire at 1/HZ intervals.
  135. */
  136. writel(ticks_per_jiffy - 1, TIMER1_RELOAD);
  137. writel(ticks_per_jiffy - 1, TIMER1_VAL);
  138. /*
  139. * Enable timer interrupt.
  140. */
  141. u = readl(BRIDGE_MASK);
  142. writel(u | BRIDGE_INT_TIMER1, BRIDGE_MASK);
  143. /*
  144. * Enable timer.
  145. */
  146. u = readl(TIMER_CTRL);
  147. writel(u | TIMER1_EN | TIMER1_RELOAD_EN, TIMER_CTRL);
  148. } else {
  149. /*
  150. * Disable timer.
  151. */
  152. u = readl(TIMER_CTRL);
  153. writel(u & ~TIMER1_EN, TIMER_CTRL);
  154. /*
  155. * Disable timer interrupt.
  156. */
  157. u = readl(BRIDGE_MASK);
  158. writel(u & ~BRIDGE_INT_TIMER1, BRIDGE_MASK);
  159. /*
  160. * ACK pending timer interrupt.
  161. */
  162. writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
  163. }
  164. local_irq_restore(flags);
  165. }
  166. static struct clock_event_device orion_clkevt = {
  167. .name = "orion_tick",
  168. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  169. .shift = 32,
  170. .rating = 300,
  171. .set_next_event = orion_clkevt_next_event,
  172. .set_mode = orion_clkevt_mode,
  173. };
  174. static irqreturn_t orion_timer_interrupt(int irq, void *dev_id)
  175. {
  176. /*
  177. * ACK timer interrupt and call event handler.
  178. */
  179. writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
  180. orion_clkevt.event_handler(&orion_clkevt);
  181. return IRQ_HANDLED;
  182. }
  183. static struct irqaction orion_timer_irq = {
  184. .name = "orion_tick",
  185. .flags = IRQF_DISABLED | IRQF_TIMER,
  186. .handler = orion_timer_interrupt
  187. };
  188. void __init orion_time_init(unsigned int irq, unsigned int tclk)
  189. {
  190. u32 u;
  191. ticks_per_jiffy = (tclk + HZ/2) / HZ;
  192. /*
  193. * Set scale and timer for sched_clock
  194. */
  195. setup_sched_clock(tclk);
  196. /*
  197. * Setup free-running clocksource timer (interrupts
  198. * disabled.)
  199. */
  200. writel(0xffffffff, TIMER0_VAL);
  201. writel(0xffffffff, TIMER0_RELOAD);
  202. u = readl(BRIDGE_MASK);
  203. writel(u & ~BRIDGE_INT_TIMER0, BRIDGE_MASK);
  204. u = readl(TIMER_CTRL);
  205. writel(u | TIMER0_EN | TIMER0_RELOAD_EN, TIMER_CTRL);
  206. clocksource_register_hz(&orion_clksrc, tclk);
  207. /*
  208. * Setup clockevent timer (interrupt-driven.)
  209. */
  210. setup_irq(irq, &orion_timer_irq);
  211. orion_clkevt.mult = div_sc(tclk, NSEC_PER_SEC, orion_clkevt.shift);
  212. orion_clkevt.max_delta_ns = clockevent_delta2ns(0xfffffffe, &orion_clkevt);
  213. orion_clkevt.min_delta_ns = clockevent_delta2ns(1, &orion_clkevt);
  214. orion_clkevt.cpumask = cpumask_of(0);
  215. clockevents_register_device(&orion_clkevt);
  216. }