time.c 5.5 KB

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