time.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. * Clocksource handling.
  70. */
  71. static cycle_t orion_clksrc_read(struct clocksource *cs)
  72. {
  73. return 0xffffffff - readl(timer_base + TIMER0_VAL_OFF);
  74. }
  75. static struct clocksource orion_clksrc = {
  76. .name = "orion_clocksource",
  77. .rating = 300,
  78. .read = orion_clksrc_read,
  79. .mask = CLOCKSOURCE_MASK(32),
  80. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  81. };
  82. /*
  83. * Clockevent handling.
  84. */
  85. static int
  86. orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev)
  87. {
  88. unsigned long flags;
  89. u32 u;
  90. if (delta == 0)
  91. return -ETIME;
  92. local_irq_save(flags);
  93. /*
  94. * Clear and enable clockevent timer interrupt.
  95. */
  96. writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
  97. u = readl(bridge_base + BRIDGE_MASK_OFF);
  98. u |= BRIDGE_INT_TIMER1;
  99. writel(u, bridge_base + BRIDGE_MASK_OFF);
  100. /*
  101. * Setup new clockevent timer value.
  102. */
  103. writel(delta, timer_base + TIMER1_VAL_OFF);
  104. /*
  105. * Enable the timer.
  106. */
  107. u = readl(timer_base + TIMER_CTRL_OFF);
  108. u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN;
  109. writel(u, timer_base + TIMER_CTRL_OFF);
  110. local_irq_restore(flags);
  111. return 0;
  112. }
  113. static void
  114. orion_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  115. {
  116. unsigned long flags;
  117. u32 u;
  118. local_irq_save(flags);
  119. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  120. /*
  121. * Setup timer to fire at 1/HZ intervals.
  122. */
  123. writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD_OFF);
  124. writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL_OFF);
  125. /*
  126. * Enable timer interrupt.
  127. */
  128. u = readl(bridge_base + BRIDGE_MASK_OFF);
  129. writel(u | BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
  130. /*
  131. * Enable timer.
  132. */
  133. u = readl(timer_base + TIMER_CTRL_OFF);
  134. writel(u | TIMER1_EN | TIMER1_RELOAD_EN,
  135. timer_base + TIMER_CTRL_OFF);
  136. } else {
  137. /*
  138. * Disable timer.
  139. */
  140. u = readl(timer_base + TIMER_CTRL_OFF);
  141. writel(u & ~TIMER1_EN, timer_base + TIMER_CTRL_OFF);
  142. /*
  143. * Disable timer interrupt.
  144. */
  145. u = readl(bridge_base + BRIDGE_MASK_OFF);
  146. writel(u & ~BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF);
  147. /*
  148. * ACK pending timer interrupt.
  149. */
  150. writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
  151. }
  152. local_irq_restore(flags);
  153. }
  154. static struct clock_event_device orion_clkevt = {
  155. .name = "orion_tick",
  156. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  157. .shift = 32,
  158. .rating = 300,
  159. .set_next_event = orion_clkevt_next_event,
  160. .set_mode = orion_clkevt_mode,
  161. };
  162. static irqreturn_t orion_timer_interrupt(int irq, void *dev_id)
  163. {
  164. /*
  165. * ACK timer interrupt and call event handler.
  166. */
  167. writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF);
  168. orion_clkevt.event_handler(&orion_clkevt);
  169. return IRQ_HANDLED;
  170. }
  171. static struct irqaction orion_timer_irq = {
  172. .name = "orion_tick",
  173. .flags = IRQF_DISABLED | IRQF_TIMER,
  174. .handler = orion_timer_interrupt
  175. };
  176. void __init
  177. orion_time_set_base(u32 _timer_base)
  178. {
  179. timer_base = (void __iomem *)_timer_base;
  180. }
  181. void __init
  182. orion_time_init(u32 _bridge_base, u32 _bridge_timer1_clr_mask,
  183. unsigned int irq, unsigned int tclk)
  184. {
  185. u32 u;
  186. /*
  187. * Set SoC-specific data.
  188. */
  189. bridge_base = (void __iomem *)_bridge_base;
  190. bridge_timer1_clr_mask = _bridge_timer1_clr_mask;
  191. ticks_per_jiffy = (tclk + HZ/2) / HZ;
  192. /*
  193. * Set scale and timer for sched_clock.
  194. */
  195. setup_sched_clock(tclk);
  196. /*
  197. * Setup free-running clocksource timer (interrupts
  198. * disabled).
  199. */
  200. writel(0xffffffff, timer_base + TIMER0_VAL_OFF);
  201. writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);
  202. u = readl(bridge_base + BRIDGE_MASK_OFF);
  203. writel(u & ~BRIDGE_INT_TIMER0, bridge_base + BRIDGE_MASK_OFF);
  204. u = readl(timer_base + TIMER_CTRL_OFF);
  205. writel(u | TIMER0_EN | TIMER0_RELOAD_EN, timer_base + TIMER_CTRL_OFF);
  206. clocksource_register_hz(&orion_clksrc, tclk);
  207. /*
  208. * Setup clockevent timer (interrupt-driven).
  209. */
  210. setup_irq(irq, &orion_timer_irq);
  211. orion_clkevt.mult = div_sc(tclk, NSEC_PER_SEC, orion_clkevt.shift);
  212. orion_clkevt.max_delta_ns = clockevent_delta2ns(0xfffffffe, &orion_clkevt);
  213. orion_clkevt.min_delta_ns = clockevent_delta2ns(1, &orion_clkevt);
  214. orion_clkevt.cpumask = cpumask_of(0);
  215. clockevents_register_device(&orion_clkevt);
  216. }