time.c 5.5 KB

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