time.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/timer.h>
  15. #include <linux/clockchips.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/sched_clock.h>
  19. /*
  20. * MBus bridge block registers.
  21. */
  22. #define BRIDGE_CAUSE_OFF 0x0110
  23. #define BRIDGE_MASK_OFF 0x0114
  24. #define BRIDGE_INT_TIMER0 0x0002
  25. #define BRIDGE_INT_TIMER1 0x0004
  26. /*
  27. * Timer block registers.
  28. */
  29. #define TIMER_CTRL_OFF 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_OFF 0x0010
  35. #define TIMER0_VAL_OFF 0x0014
  36. #define TIMER1_RELOAD_OFF 0x0018
  37. #define TIMER1_VAL_OFF 0x001c
  38. /*
  39. * SoC-specific data.
  40. */
  41. static void __iomem *bridge_base;
  42. static u32 bridge_timer1_clr_mask;
  43. static void __iomem *timer_base;
  44. /*
  45. * Number of timer ticks per jiffy.
  46. */
  47. static u32 ticks_per_jiffy;
  48. /*
  49. * Orion's sched_clock implementation. It has a resolution of
  50. * at least 7.5ns (133MHz TCLK).
  51. */
  52. static u32 notrace orion_read_sched_clock(void)
  53. {
  54. return ~readl(timer_base + TIMER0_VAL_OFF);
  55. }
  56. /*
  57. * Clockevent handling.
  58. */
  59. static int
  60. orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev)
  61. {
  62. unsigned long flags;
  63. u32 u;
  64. if (delta == 0)
  65. return -ETIME;
  66. local_irq_save(flags);
  67. /*
  68. * Clear and enable clockevent timer interrupt.
  69. */
  70. writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
  71. u = readl(bridge_base + BRIDGE_MASK_OFF);
  72. u |= BRIDGE_INT_TIMER1;
  73. writel(u, bridge_base + BRIDGE_MASK_OFF);
  74. /*
  75. * Setup new clockevent timer value.
  76. */
  77. writel(delta, timer_base + TIMER1_VAL_OFF);
  78. /*
  79. * Enable the timer.
  80. */
  81. u = readl(timer_base + TIMER_CTRL_OFF);
  82. u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN;
  83. writel(u, timer_base + TIMER_CTRL_OFF);
  84. local_irq_restore(flags);
  85. return 0;
  86. }
  87. static void
  88. orion_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  89. {
  90. unsigned long flags;
  91. u32 u;
  92. local_irq_save(flags);
  93. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  94. /*
  95. * Setup timer to fire at 1/HZ intervals.
  96. */
  97. writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD_OFF);
  98. writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL_OFF);
  99. /*
  100. * Enable timer interrupt.
  101. */
  102. u = readl(bridge_base + BRIDGE_MASK_OFF);
  103. writel(u | BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
  104. /*
  105. * Enable timer.
  106. */
  107. u = readl(timer_base + TIMER_CTRL_OFF);
  108. writel(u | TIMER1_EN | TIMER1_RELOAD_EN,
  109. timer_base + TIMER_CTRL_OFF);
  110. } else {
  111. /*
  112. * Disable timer.
  113. */
  114. u = readl(timer_base + TIMER_CTRL_OFF);
  115. writel(u & ~TIMER1_EN, timer_base + TIMER_CTRL_OFF);
  116. /*
  117. * Disable timer interrupt.
  118. */
  119. u = readl(bridge_base + BRIDGE_MASK_OFF);
  120. writel(u & ~BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
  121. /*
  122. * ACK pending timer interrupt.
  123. */
  124. writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
  125. }
  126. local_irq_restore(flags);
  127. }
  128. static struct clock_event_device orion_clkevt = {
  129. .name = "orion_tick",
  130. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  131. .shift = 32,
  132. .rating = 300,
  133. .set_next_event = orion_clkevt_next_event,
  134. .set_mode = orion_clkevt_mode,
  135. };
  136. static irqreturn_t orion_timer_interrupt(int irq, void *dev_id)
  137. {
  138. /*
  139. * ACK timer interrupt and call event handler.
  140. */
  141. writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
  142. orion_clkevt.event_handler(&orion_clkevt);
  143. return IRQ_HANDLED;
  144. }
  145. static struct irqaction orion_timer_irq = {
  146. .name = "orion_tick",
  147. .flags = IRQF_DISABLED | IRQF_TIMER,
  148. .handler = orion_timer_interrupt
  149. };
  150. void __init
  151. orion_time_set_base(u32 _timer_base)
  152. {
  153. timer_base = (void __iomem *)_timer_base;
  154. }
  155. void __init
  156. orion_time_init(u32 _bridge_base, u32 _bridge_timer1_clr_mask,
  157. unsigned int irq, unsigned int tclk)
  158. {
  159. u32 u;
  160. /*
  161. * Set SoC-specific data.
  162. */
  163. bridge_base = (void __iomem *)_bridge_base;
  164. bridge_timer1_clr_mask = _bridge_timer1_clr_mask;
  165. ticks_per_jiffy = (tclk + HZ/2) / HZ;
  166. /*
  167. * Set scale and timer for sched_clock.
  168. */
  169. setup_sched_clock(orion_read_sched_clock, 32, tclk);
  170. /*
  171. * Setup free-running clocksource timer (interrupts
  172. * disabled).
  173. */
  174. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  175. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  176. u = readl(bridge_base + BRIDGE_MASK_OFF);
  177. writel(u & ~BRIDGE_INT_TIMER0, bridge_base + BRIDGE_MASK_OFF);
  178. u = readl(timer_base + TIMER_CTRL_OFF);
  179. writel(u | TIMER0_EN | TIMER0_RELOAD_EN, timer_base + TIMER_CTRL_OFF);
  180. clocksource_mmio_init(timer_base + TIMER0_VAL_OFF, "orion_clocksource",
  181. tclk, 300, 32, clocksource_mmio_readl_down);
  182. /*
  183. * Setup clockevent timer (interrupt-driven).
  184. */
  185. setup_irq(irq, &orion_timer_irq);
  186. orion_clkevt.mult = div_sc(tclk, NSEC_PER_SEC, orion_clkevt.shift);
  187. orion_clkevt.max_delta_ns = clockevent_delta2ns(0xfffffffe, &orion_clkevt);
  188. orion_clkevt.min_delta_ns = clockevent_delta2ns(1, &orion_clkevt);
  189. orion_clkevt.cpumask = cpumask_of(0);
  190. clockevents_register_device(&orion_clkevt);
  191. }