at91rm9200_time.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * linux/arch/arm/mach-at91/at91rm9200_time.c
  3. *
  4. * Copyright (C) 2003 SAN People
  5. * Copyright (C) 2003 ATMEL
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/clockchips.h>
  25. #include <linux/export.h>
  26. #include <asm/mach/time.h>
  27. #include <mach/at91_st.h>
  28. static unsigned long last_crtr;
  29. static u32 irqmask;
  30. static struct clock_event_device clkevt;
  31. #define RM9200_TIMER_LATCH ((AT91_SLOW_CLOCK + HZ/2) / HZ)
  32. /*
  33. * The ST_CRTR is updated asynchronously to the master clock ... but
  34. * the updates as seen by the CPU don't seem to be strictly monotonic.
  35. * Waiting until we read the same value twice avoids glitching.
  36. */
  37. static inline unsigned long read_CRTR(void)
  38. {
  39. unsigned long x1, x2;
  40. x1 = at91_st_read(AT91_ST_CRTR);
  41. do {
  42. x2 = at91_st_read(AT91_ST_CRTR);
  43. if (x1 == x2)
  44. break;
  45. x1 = x2;
  46. } while (1);
  47. return x1;
  48. }
  49. /*
  50. * IRQ handler for the timer.
  51. */
  52. static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
  53. {
  54. u32 sr = at91_st_read(AT91_ST_SR) & irqmask;
  55. /*
  56. * irqs should be disabled here, but as the irq is shared they are only
  57. * guaranteed to be off if the timer irq is registered first.
  58. */
  59. WARN_ON_ONCE(!irqs_disabled());
  60. /* simulate "oneshot" timer with alarm */
  61. if (sr & AT91_ST_ALMS) {
  62. clkevt.event_handler(&clkevt);
  63. return IRQ_HANDLED;
  64. }
  65. /* periodic mode should handle delayed ticks */
  66. if (sr & AT91_ST_PITS) {
  67. u32 crtr = read_CRTR();
  68. while (((crtr - last_crtr) & AT91_ST_CRTV) >= RM9200_TIMER_LATCH) {
  69. last_crtr += RM9200_TIMER_LATCH;
  70. clkevt.event_handler(&clkevt);
  71. }
  72. return IRQ_HANDLED;
  73. }
  74. /* this irq is shared ... */
  75. return IRQ_NONE;
  76. }
  77. static struct irqaction at91rm9200_timer_irq = {
  78. .name = "at91_tick",
  79. .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  80. .handler = at91rm9200_timer_interrupt
  81. };
  82. static cycle_t read_clk32k(struct clocksource *cs)
  83. {
  84. return read_CRTR();
  85. }
  86. static struct clocksource clk32k = {
  87. .name = "32k_counter",
  88. .rating = 150,
  89. .read = read_clk32k,
  90. .mask = CLOCKSOURCE_MASK(20),
  91. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  92. };
  93. static void
  94. clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  95. {
  96. /* Disable and flush pending timer interrupts */
  97. at91_st_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
  98. at91_st_read(AT91_ST_SR);
  99. last_crtr = read_CRTR();
  100. switch (mode) {
  101. case CLOCK_EVT_MODE_PERIODIC:
  102. /* PIT for periodic irqs; fixed rate of 1/HZ */
  103. irqmask = AT91_ST_PITS;
  104. at91_st_write(AT91_ST_PIMR, RM9200_TIMER_LATCH);
  105. break;
  106. case CLOCK_EVT_MODE_ONESHOT:
  107. /* ALM for oneshot irqs, set by next_event()
  108. * before 32 seconds have passed
  109. */
  110. irqmask = AT91_ST_ALMS;
  111. at91_st_write(AT91_ST_RTAR, last_crtr);
  112. break;
  113. case CLOCK_EVT_MODE_SHUTDOWN:
  114. case CLOCK_EVT_MODE_UNUSED:
  115. case CLOCK_EVT_MODE_RESUME:
  116. irqmask = 0;
  117. break;
  118. }
  119. at91_st_write(AT91_ST_IER, irqmask);
  120. }
  121. static int
  122. clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
  123. {
  124. u32 alm;
  125. int status = 0;
  126. BUG_ON(delta < 2);
  127. /* The alarm IRQ uses absolute time (now+delta), not the relative
  128. * time (delta) in our calling convention. Like all clockevents
  129. * using such "match" hardware, we have a race to defend against.
  130. *
  131. * Our defense here is to have set up the clockevent device so the
  132. * delta is at least two. That way we never end up writing RTAR
  133. * with the value then held in CRTR ... which would mean the match
  134. * wouldn't trigger until 32 seconds later, after CRTR wraps.
  135. */
  136. alm = read_CRTR();
  137. /* Cancel any pending alarm; flush any pending IRQ */
  138. at91_st_write(AT91_ST_RTAR, alm);
  139. at91_st_read(AT91_ST_SR);
  140. /* Schedule alarm by writing RTAR. */
  141. alm += delta;
  142. at91_st_write(AT91_ST_RTAR, alm);
  143. return status;
  144. }
  145. static struct clock_event_device clkevt = {
  146. .name = "at91_tick",
  147. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  148. .shift = 32,
  149. .rating = 150,
  150. .set_next_event = clkevt32k_next_event,
  151. .set_mode = clkevt32k_mode,
  152. };
  153. void __iomem *at91_st_base;
  154. EXPORT_SYMBOL_GPL(at91_st_base);
  155. void __init at91rm9200_ioremap_st(u32 addr)
  156. {
  157. at91_st_base = ioremap(addr, 256);
  158. if (!at91_st_base)
  159. panic("Impossible to ioremap ST\n");
  160. }
  161. /*
  162. * ST (system timer) module supports both clockevents and clocksource.
  163. */
  164. void __init at91rm9200_timer_init(void)
  165. {
  166. /* Disable all timer interrupts, and clear any pending ones */
  167. at91_st_write(AT91_ST_IDR,
  168. AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
  169. at91_st_read(AT91_ST_SR);
  170. /* Make IRQs happen for the system timer */
  171. setup_irq(AT91_ID_SYS, &at91rm9200_timer_irq);
  172. /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
  173. * directly for the clocksource and all clockevents, after adjusting
  174. * its prescaler from the 1 Hz default.
  175. */
  176. at91_st_write(AT91_ST_RTMR, 1);
  177. /* Setup timer clockevent, with minimum of two ticks (important!!) */
  178. clkevt.mult = div_sc(AT91_SLOW_CLOCK, NSEC_PER_SEC, clkevt.shift);
  179. clkevt.max_delta_ns = clockevent_delta2ns(AT91_ST_ALMV, &clkevt);
  180. clkevt.min_delta_ns = clockevent_delta2ns(2, &clkevt) + 1;
  181. clkevt.cpumask = cpumask_of(0);
  182. clockevents_register_device(&clkevt);
  183. /* register clocksource */
  184. clocksource_register_hz(&clk32k, AT91_SLOW_CLOCK);
  185. }
  186. struct sys_timer at91rm9200_timer = {
  187. .init = at91rm9200_timer_init,
  188. };