time.c 5.2 KB

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