at91rm9200_time.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <asm/mach/time.h>
  26. #include <mach/at91_st.h>
  27. static unsigned long last_crtr;
  28. static u32 irqmask;
  29. static struct clock_event_device clkevt;
  30. /*
  31. * The ST_CRTR is updated asynchronously to the master clock ... but
  32. * the updates as seen by the CPU don't seem to be strictly monotonic.
  33. * Waiting until we read the same value twice avoids glitching.
  34. */
  35. static inline unsigned long read_CRTR(void)
  36. {
  37. unsigned long x1, x2;
  38. x1 = at91_sys_read(AT91_ST_CRTR);
  39. do {
  40. x2 = at91_sys_read(AT91_ST_CRTR);
  41. if (x1 == x2)
  42. break;
  43. x1 = x2;
  44. } while (1);
  45. return x1;
  46. }
  47. /*
  48. * IRQ handler for the timer.
  49. */
  50. static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
  51. {
  52. u32 sr = at91_sys_read(AT91_ST_SR) & irqmask;
  53. /*
  54. * irqs should be disabled here, but as the irq is shared they are only
  55. * guaranteed to be off if the timer irq is registered first.
  56. */
  57. WARN_ON_ONCE(!irqs_disabled());
  58. /* simulate "oneshot" timer with alarm */
  59. if (sr & AT91_ST_ALMS) {
  60. clkevt.event_handler(&clkevt);
  61. return IRQ_HANDLED;
  62. }
  63. /* periodic mode should handle delayed ticks */
  64. if (sr & AT91_ST_PITS) {
  65. u32 crtr = read_CRTR();
  66. while (((crtr - last_crtr) & AT91_ST_CRTV) >= LATCH) {
  67. last_crtr += LATCH;
  68. clkevt.event_handler(&clkevt);
  69. }
  70. return IRQ_HANDLED;
  71. }
  72. /* this irq is shared ... */
  73. return IRQ_NONE;
  74. }
  75. static struct irqaction at91rm9200_timer_irq = {
  76. .name = "at91_tick",
  77. .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  78. .handler = at91rm9200_timer_interrupt
  79. };
  80. static cycle_t read_clk32k(struct clocksource *cs)
  81. {
  82. return read_CRTR();
  83. }
  84. static struct clocksource clk32k = {
  85. .name = "32k_counter",
  86. .rating = 150,
  87. .read = read_clk32k,
  88. .mask = CLOCKSOURCE_MASK(20),
  89. .shift = 10,
  90. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  91. };
  92. static void
  93. clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  94. {
  95. /* Disable and flush pending timer interrupts */
  96. at91_sys_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
  97. (void) at91_sys_read(AT91_ST_SR);
  98. last_crtr = read_CRTR();
  99. switch (mode) {
  100. case CLOCK_EVT_MODE_PERIODIC:
  101. /* PIT for periodic irqs; fixed rate of 1/HZ */
  102. irqmask = AT91_ST_PITS;
  103. at91_sys_write(AT91_ST_PIMR, LATCH);
  104. break;
  105. case CLOCK_EVT_MODE_ONESHOT:
  106. /* ALM for oneshot irqs, set by next_event()
  107. * before 32 seconds have passed
  108. */
  109. irqmask = AT91_ST_ALMS;
  110. at91_sys_write(AT91_ST_RTAR, last_crtr);
  111. break;
  112. case CLOCK_EVT_MODE_SHUTDOWN:
  113. case CLOCK_EVT_MODE_UNUSED:
  114. case CLOCK_EVT_MODE_RESUME:
  115. irqmask = 0;
  116. break;
  117. }
  118. at91_sys_write(AT91_ST_IER, irqmask);
  119. }
  120. static int
  121. clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
  122. {
  123. u32 alm;
  124. int status = 0;
  125. BUG_ON(delta < 2);
  126. /* The alarm IRQ uses absolute time (now+delta), not the relative
  127. * time (delta) in our calling convention. Like all clockevents
  128. * using such "match" hardware, we have a race to defend against.
  129. *
  130. * Our defense here is to have set up the clockevent device so the
  131. * delta is at least two. That way we never end up writing RTAR
  132. * with the value then held in CRTR ... which would mean the match
  133. * wouldn't trigger until 32 seconds later, after CRTR wraps.
  134. */
  135. alm = read_CRTR();
  136. /* Cancel any pending alarm; flush any pending IRQ */
  137. at91_sys_write(AT91_ST_RTAR, alm);
  138. (void) at91_sys_read(AT91_ST_SR);
  139. /* Schedule alarm by writing RTAR. */
  140. alm += delta;
  141. at91_sys_write(AT91_ST_RTAR, alm);
  142. return status;
  143. }
  144. static struct clock_event_device clkevt = {
  145. .name = "at91_tick",
  146. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  147. .shift = 32,
  148. .rating = 150,
  149. .set_next_event = clkevt32k_next_event,
  150. .set_mode = clkevt32k_mode,
  151. };
  152. /*
  153. * ST (system timer) module supports both clockevents and clocksource.
  154. */
  155. void __init at91rm9200_timer_init(void)
  156. {
  157. /* Disable all timer interrupts, and clear any pending ones */
  158. at91_sys_write(AT91_ST_IDR,
  159. AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
  160. (void) at91_sys_read(AT91_ST_SR);
  161. /* Make IRQs happen for the system timer */
  162. setup_irq(AT91_ID_SYS, &at91rm9200_timer_irq);
  163. /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
  164. * directly for the clocksource and all clockevents, after adjusting
  165. * its prescaler from the 1 Hz default.
  166. */
  167. at91_sys_write(AT91_ST_RTMR, 1);
  168. /* Setup timer clockevent, with minimum of two ticks (important!!) */
  169. clkevt.mult = div_sc(AT91_SLOW_CLOCK, NSEC_PER_SEC, clkevt.shift);
  170. clkevt.max_delta_ns = clockevent_delta2ns(AT91_ST_ALMV, &clkevt);
  171. clkevt.min_delta_ns = clockevent_delta2ns(2, &clkevt) + 1;
  172. clkevt.cpumask = cpumask_of(0);
  173. clockevents_register_device(&clkevt);
  174. /* register clocksource */
  175. clk32k.mult = clocksource_hz2mult(AT91_SLOW_CLOCK, clk32k.shift);
  176. clocksource_register(&clk32k);
  177. }
  178. struct sys_timer at91rm9200_timer = {
  179. .init = at91rm9200_timer_init,
  180. };