time.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/clockchips.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <asm/mach/time.h>
  18. #include <mach/hardware.h>
  19. /*
  20. * Number of timer ticks per jiffy.
  21. */
  22. static u32 ticks_per_jiffy;
  23. /*
  24. * Timer block registers.
  25. */
  26. #define TIMER_CTRL (TIMER_VIRT_BASE + 0x0000)
  27. #define TIMER0_EN 0x0001
  28. #define TIMER0_RELOAD_EN 0x0002
  29. #define TIMER1_EN 0x0004
  30. #define TIMER1_RELOAD_EN 0x0008
  31. #define TIMER0_RELOAD (TIMER_VIRT_BASE + 0x0010)
  32. #define TIMER0_VAL (TIMER_VIRT_BASE + 0x0014)
  33. #define TIMER1_RELOAD (TIMER_VIRT_BASE + 0x0018)
  34. #define TIMER1_VAL (TIMER_VIRT_BASE + 0x001c)
  35. /*
  36. * Clocksource handling.
  37. */
  38. static cycle_t orion_clksrc_read(void)
  39. {
  40. return 0xffffffff - readl(TIMER0_VAL);
  41. }
  42. static struct clocksource orion_clksrc = {
  43. .name = "orion_clocksource",
  44. .shift = 20,
  45. .rating = 300,
  46. .read = orion_clksrc_read,
  47. .mask = CLOCKSOURCE_MASK(32),
  48. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  49. };
  50. /*
  51. * Clockevent handling.
  52. */
  53. static int
  54. orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev)
  55. {
  56. unsigned long flags;
  57. u32 u;
  58. if (delta == 0)
  59. return -ETIME;
  60. local_irq_save(flags);
  61. /*
  62. * Clear and enable clockevent timer interrupt.
  63. */
  64. writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
  65. u = readl(BRIDGE_MASK);
  66. u |= BRIDGE_INT_TIMER1;
  67. writel(u, BRIDGE_MASK);
  68. /*
  69. * Setup new clockevent timer value.
  70. */
  71. writel(delta, TIMER1_VAL);
  72. /*
  73. * Enable the timer.
  74. */
  75. u = readl(TIMER_CTRL);
  76. u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN;
  77. writel(u, TIMER_CTRL);
  78. local_irq_restore(flags);
  79. return 0;
  80. }
  81. static void
  82. orion_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  83. {
  84. unsigned long flags;
  85. u32 u;
  86. local_irq_save(flags);
  87. if (mode == CLOCK_EVT_MODE_PERIODIC) {
  88. /*
  89. * Setup timer to fire at 1/HZ intervals.
  90. */
  91. writel(ticks_per_jiffy - 1, TIMER1_RELOAD);
  92. writel(ticks_per_jiffy - 1, TIMER1_VAL);
  93. /*
  94. * Enable timer interrupt.
  95. */
  96. u = readl(BRIDGE_MASK);
  97. writel(u | BRIDGE_INT_TIMER1, BRIDGE_MASK);
  98. /*
  99. * Enable timer.
  100. */
  101. u = readl(TIMER_CTRL);
  102. writel(u | TIMER1_EN | TIMER1_RELOAD_EN, TIMER_CTRL);
  103. } else {
  104. /*
  105. * Disable timer.
  106. */
  107. u = readl(TIMER_CTRL);
  108. writel(u & ~TIMER1_EN, TIMER_CTRL);
  109. /*
  110. * Disable timer interrupt.
  111. */
  112. u = readl(BRIDGE_MASK);
  113. writel(u & ~BRIDGE_INT_TIMER1, BRIDGE_MASK);
  114. /*
  115. * ACK pending timer interrupt.
  116. */
  117. writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
  118. }
  119. local_irq_restore(flags);
  120. }
  121. static struct clock_event_device orion_clkevt = {
  122. .name = "orion_tick",
  123. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
  124. .shift = 32,
  125. .rating = 300,
  126. .cpumask = CPU_MASK_CPU0,
  127. .set_next_event = orion_clkevt_next_event,
  128. .set_mode = orion_clkevt_mode,
  129. };
  130. static irqreturn_t orion_timer_interrupt(int irq, void *dev_id)
  131. {
  132. /*
  133. * ACK timer interrupt and call event handler.
  134. */
  135. writel(BRIDGE_INT_TIMER1_CLR, BRIDGE_CAUSE);
  136. orion_clkevt.event_handler(&orion_clkevt);
  137. return IRQ_HANDLED;
  138. }
  139. static struct irqaction orion_timer_irq = {
  140. .name = "orion_tick",
  141. .flags = IRQF_DISABLED | IRQF_TIMER,
  142. .handler = orion_timer_interrupt
  143. };
  144. void __init orion_time_init(unsigned int irq, unsigned int tclk)
  145. {
  146. u32 u;
  147. ticks_per_jiffy = (tclk + HZ/2) / HZ;
  148. /*
  149. * Setup free-running clocksource timer (interrupts
  150. * disabled.)
  151. */
  152. writel(0xffffffff, TIMER0_VAL);
  153. writel(0xffffffff, TIMER0_RELOAD);
  154. u = readl(BRIDGE_MASK);
  155. writel(u & ~BRIDGE_INT_TIMER0, BRIDGE_MASK);
  156. u = readl(TIMER_CTRL);
  157. writel(u | TIMER0_EN | TIMER0_RELOAD_EN, TIMER_CTRL);
  158. orion_clksrc.mult = clocksource_hz2mult(tclk, orion_clksrc.shift);
  159. clocksource_register(&orion_clksrc);
  160. /*
  161. * Setup clockevent timer (interrupt-driven.)
  162. */
  163. setup_irq(irq, &orion_timer_irq);
  164. orion_clkevt.mult = div_sc(tclk, NSEC_PER_SEC, orion_clkevt.shift);
  165. orion_clkevt.max_delta_ns = clockevent_delta2ns(0xfffffffe, &orion_clkevt);
  166. orion_clkevt.min_delta_ns = clockevent_delta2ns(1, &orion_clkevt);
  167. clockevents_register_device(&orion_clkevt);
  168. }