time.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include <plat/time.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 u64 notrace orion_read_sched_clock(void)
  54. {
  55. return ~readl(timer_base + TIMER0_VAL_OFF);
  56. }
  57. /*
  58. * Clockevent handling.
  59. */
  60. static int
  61. orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev)
  62. {
  63. unsigned long flags;
  64. u32 u;
  65. if (delta == 0)
  66. return -ETIME;
  67. local_irq_save(flags);
  68. /*
  69. * Clear and enable clockevent timer interrupt.
  70. */
  71. writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
  72. u = readl(bridge_base + BRIDGE_MASK_OFF);
  73. u |= BRIDGE_INT_TIMER1;
  74. writel(u, bridge_base + BRIDGE_MASK_OFF);
  75. /*
  76. * Setup new clockevent timer value.
  77. */
  78. writel(delta, timer_base + TIMER1_VAL_OFF);
  79. /*
  80. * Enable the timer.
  81. */
  82. u = readl(timer_base + TIMER_CTRL_OFF);
  83. u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN;
  84. writel(u, timer_base + TIMER_CTRL_OFF);
  85. local_irq_restore(flags);
  86. return 0;
  87. }
  88. static int orion_clkevt_shutdown(struct clock_event_device *evt)
  89. {
  90. unsigned long flags;
  91. u32 u;
  92. local_irq_save(flags);
  93. /* Disable timer */
  94. u = readl(timer_base + TIMER_CTRL_OFF);
  95. writel(u & ~TIMER1_EN, timer_base + TIMER_CTRL_OFF);
  96. /* Disable timer interrupt */
  97. u = readl(bridge_base + BRIDGE_MASK_OFF);
  98. writel(u & ~BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
  99. /* ACK pending timer interrupt */
  100. writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
  101. local_irq_restore(flags);
  102. return 0;
  103. }
  104. static int orion_clkevt_set_periodic(struct clock_event_device *evt)
  105. {
  106. unsigned long flags;
  107. u32 u;
  108. local_irq_save(flags);
  109. /* Setup timer to fire at 1/HZ intervals */
  110. writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD_OFF);
  111. writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL_OFF);
  112. /* Enable timer interrupt */
  113. u = readl(bridge_base + BRIDGE_MASK_OFF);
  114. writel(u | BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
  115. /* Enable timer */
  116. u = readl(timer_base + TIMER_CTRL_OFF);
  117. writel(u | TIMER1_EN | TIMER1_RELOAD_EN, timer_base + TIMER_CTRL_OFF);
  118. local_irq_restore(flags);
  119. return 0;
  120. }
  121. static struct clock_event_device orion_clkevt = {
  122. .name = "orion_tick",
  123. .features = CLOCK_EVT_FEAT_ONESHOT |
  124. CLOCK_EVT_FEAT_PERIODIC,
  125. .rating = 300,
  126. .set_next_event = orion_clkevt_next_event,
  127. .set_state_shutdown = orion_clkevt_shutdown,
  128. .set_state_periodic = orion_clkevt_set_periodic,
  129. .set_state_oneshot = orion_clkevt_shutdown,
  130. .tick_resume = orion_clkevt_shutdown,
  131. };
  132. static irqreturn_t orion_timer_interrupt(int irq, void *dev_id)
  133. {
  134. /*
  135. * ACK timer interrupt and call event handler.
  136. */
  137. writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
  138. orion_clkevt.event_handler(&orion_clkevt);
  139. return IRQ_HANDLED;
  140. }
  141. static struct irqaction orion_timer_irq = {
  142. .name = "orion_tick",
  143. .flags = IRQF_TIMER,
  144. .handler = orion_timer_interrupt
  145. };
  146. void __init
  147. orion_time_set_base(void __iomem *_timer_base)
  148. {
  149. timer_base = _timer_base;
  150. }
  151. void __init
  152. orion_time_init(void __iomem *_bridge_base, u32 _bridge_timer1_clr_mask,
  153. unsigned int irq, unsigned int tclk)
  154. {
  155. u32 u;
  156. /*
  157. * Set SoC-specific data.
  158. */
  159. bridge_base = _bridge_base;
  160. bridge_timer1_clr_mask = _bridge_timer1_clr_mask;
  161. ticks_per_jiffy = (tclk + HZ/2) / HZ;
  162. /*
  163. * Set scale and timer for sched_clock.
  164. */
  165. sched_clock_register(orion_read_sched_clock, 32, tclk);
  166. /*
  167. * Setup free-running clocksource timer (interrupts
  168. * disabled).
  169. */
  170. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  171. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  172. u = readl(bridge_base + BRIDGE_MASK_OFF);
  173. writel(u & ~BRIDGE_INT_TIMER0, bridge_base + BRIDGE_MASK_OFF);
  174. u = readl(timer_base + TIMER_CTRL_OFF);
  175. writel(u | TIMER0_EN | TIMER0_RELOAD_EN, timer_base + TIMER_CTRL_OFF);
  176. clocksource_mmio_init(timer_base + TIMER0_VAL_OFF, "orion_clocksource",
  177. tclk, 300, 32, clocksource_mmio_readl_down);
  178. /*
  179. * Setup clockevent timer (interrupt-driven).
  180. */
  181. setup_irq(irq, &orion_timer_irq);
  182. orion_clkevt.cpumask = cpumask_of(0);
  183. clockevents_config_and_register(&orion_clkevt, tclk, 1, 0xfffffffe);
  184. }