time.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 void
  89. orion_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  90. {
  91. unsigned long flags;
  92. u32 u;
  93. local_irq_save(flags);
  94. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  95. /*
  96. * Setup timer to fire at 1/HZ intervals.
  97. */
  98. writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD_OFF);
  99. writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL_OFF);
  100. /*
  101. * Enable timer interrupt.
  102. */
  103. u = readl(bridge_base + BRIDGE_MASK_OFF);
  104. writel(u | BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
  105. /*
  106. * Enable timer.
  107. */
  108. u = readl(timer_base + TIMER_CTRL_OFF);
  109. writel(u | TIMER1_EN | TIMER1_RELOAD_EN,
  110. timer_base + TIMER_CTRL_OFF);
  111. } else {
  112. /*
  113. * Disable timer.
  114. */
  115. u = readl(timer_base + TIMER_CTRL_OFF);
  116. writel(u & ~TIMER1_EN, timer_base + TIMER_CTRL_OFF);
  117. /*
  118. * Disable timer interrupt.
  119. */
  120. u = readl(bridge_base + BRIDGE_MASK_OFF);
  121. writel(u & ~BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
  122. /*
  123. * ACK pending timer interrupt.
  124. */
  125. writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
  126. }
  127. local_irq_restore(flags);
  128. }
  129. static struct clock_event_device orion_clkevt = {
  130. .name = "orion_tick",
  131. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  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_TIMER,
  148. .handler = orion_timer_interrupt
  149. };
  150. void __init
  151. orion_time_set_base(void __iomem *_timer_base)
  152. {
  153. timer_base = _timer_base;
  154. }
  155. void __init
  156. orion_time_init(void __iomem *_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 = _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. sched_clock_register(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.cpumask = cpumask_of(0);
  187. clockevents_config_and_register(&orion_clkevt, tclk, 1, 0xfffffffe);
  188. }