time.c 5.0 KB

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