time.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 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. .shift = 20,
  91. .rating = 300,
  92. .read = orion_clksrc_read,
  93. .mask = CLOCKSOURCE_MASK(32),
  94. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  95. };
  96. /*
  97. * Clockevent handling.
  98. */
  99. static int
  100. orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev)
  101. {
  102. unsigned long flags;
  103. u32 u;
  104. if (delta == 0)
  105. return -ETIME;
  106. local_irq_save(flags);
  107. /*
  108. * Clear and enable clockevent timer interrupt.
  109. */
  110. writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
  111. u = readl(BRIDGE_MASK);
  112. u |= BRIDGE_INT_TIMER1;
  113. writel(u, BRIDGE_MASK);
  114. /*
  115. * Setup new clockevent timer value.
  116. */
  117. writel(delta, TIMER1_VAL);
  118. /*
  119. * Enable the timer.
  120. */
  121. u = readl(TIMER_CTRL);
  122. u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN;
  123. writel(u, TIMER_CTRL);
  124. local_irq_restore(flags);
  125. return 0;
  126. }
  127. static void
  128. orion_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  129. {
  130. unsigned long flags;
  131. u32 u;
  132. local_irq_save(flags);
  133. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  134. /*
  135. * Setup timer to fire at 1/HZ intervals.
  136. */
  137. writel(ticks_per_jiffy - 1, TIMER1_RELOAD);
  138. writel(ticks_per_jiffy - 1, TIMER1_VAL);
  139. /*
  140. * Enable timer interrupt.
  141. */
  142. u = readl(BRIDGE_MASK);
  143. writel(u | BRIDGE_INT_TIMER1, BRIDGE_MASK);
  144. /*
  145. * Enable timer.
  146. */
  147. u = readl(TIMER_CTRL);
  148. writel(u | TIMER1_EN | TIMER1_RELOAD_EN, TIMER_CTRL);
  149. } else {
  150. /*
  151. * Disable timer.
  152. */
  153. u = readl(TIMER_CTRL);
  154. writel(u & ~TIMER1_EN, TIMER_CTRL);
  155. /*
  156. * Disable timer interrupt.
  157. */
  158. u = readl(BRIDGE_MASK);
  159. writel(u & ~BRIDGE_INT_TIMER1, BRIDGE_MASK);
  160. /*
  161. * ACK pending timer interrupt.
  162. */
  163. writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
  164. }
  165. local_irq_restore(flags);
  166. }
  167. static struct clock_event_device orion_clkevt = {
  168. .name = "orion_tick",
  169. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  170. .shift = 32,
  171. .rating = 300,
  172. .set_next_event = orion_clkevt_next_event,
  173. .set_mode = orion_clkevt_mode,
  174. };
  175. static irqreturn_t orion_timer_interrupt(int irq, void *dev_id)
  176. {
  177. /*
  178. * ACK timer interrupt and call event handler.
  179. */
  180. writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
  181. orion_clkevt.event_handler(&orion_clkevt);
  182. return IRQ_HANDLED;
  183. }
  184. static struct irqaction orion_timer_irq = {
  185. .name = "orion_tick",
  186. .flags = IRQF_DISABLED | IRQF_TIMER,
  187. .handler = orion_timer_interrupt
  188. };
  189. void __init orion_time_init(unsigned int irq, unsigned int tclk)
  190. {
  191. u32 u;
  192. ticks_per_jiffy = (tclk + HZ/2) / HZ;
  193. /*
  194. * Set scale and timer for sched_clock
  195. */
  196. setup_sched_clock(tclk);
  197. /*
  198. * Setup free-running clocksource timer (interrupts
  199. * disabled.)
  200. */
  201. writel(0xffffffff, TIMER0_VAL);
  202. writel(0xffffffff, TIMER0_RELOAD);
  203. u = readl(BRIDGE_MASK);
  204. writel(u & ~BRIDGE_INT_TIMER0, BRIDGE_MASK);
  205. u = readl(TIMER_CTRL);
  206. writel(u | TIMER0_EN | TIMER0_RELOAD_EN, TIMER_CTRL);
  207. orion_clksrc.mult = clocksource_hz2mult(tclk, orion_clksrc.shift);
  208. clocksource_register(&orion_clksrc);
  209. /*
  210. * Setup clockevent timer (interrupt-driven.)
  211. */
  212. setup_irq(irq, &orion_timer_irq);
  213. orion_clkevt.mult = div_sc(tclk, NSEC_PER_SEC, orion_clkevt.shift);
  214. orion_clkevt.max_delta_ns = clockevent_delta2ns(0xfffffffe, &orion_clkevt);
  215. orion_clkevt.min_delta_ns = clockevent_delta2ns(1, &orion_clkevt);
  216. orion_clkevt.cpumask = cpumask_of(0);
  217. clockevents_register_device(&orion_clkevt);
  218. }