at91rm9200_time.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 <linux/of.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_irq.h>
  29. #include <asm/mach/time.h>
  30. #include <mach/at91_st.h>
  31. static unsigned long last_crtr;
  32. static u32 irqmask;
  33. static struct clock_event_device clkevt;
  34. #define RM9200_TIMER_LATCH ((AT91_SLOW_CLOCK + HZ/2) / HZ)
  35. /*
  36. * The ST_CRTR is updated asynchronously to the master clock ... but
  37. * the updates as seen by the CPU don't seem to be strictly monotonic.
  38. * Waiting until we read the same value twice avoids glitching.
  39. */
  40. static inline unsigned long read_CRTR(void)
  41. {
  42. unsigned long x1, x2;
  43. x1 = at91_st_read(AT91_ST_CRTR);
  44. do {
  45. x2 = at91_st_read(AT91_ST_CRTR);
  46. if (x1 == x2)
  47. break;
  48. x1 = x2;
  49. } while (1);
  50. return x1;
  51. }
  52. /*
  53. * IRQ handler for the timer.
  54. */
  55. static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
  56. {
  57. u32 sr = at91_st_read(AT91_ST_SR) & irqmask;
  58. /*
  59. * irqs should be disabled here, but as the irq is shared they are only
  60. * guaranteed to be off if the timer irq is registered first.
  61. */
  62. WARN_ON_ONCE(!irqs_disabled());
  63. /* simulate "oneshot" timer with alarm */
  64. if (sr & AT91_ST_ALMS) {
  65. clkevt.event_handler(&clkevt);
  66. return IRQ_HANDLED;
  67. }
  68. /* periodic mode should handle delayed ticks */
  69. if (sr & AT91_ST_PITS) {
  70. u32 crtr = read_CRTR();
  71. while (((crtr - last_crtr) & AT91_ST_CRTV) >= RM9200_TIMER_LATCH) {
  72. last_crtr += RM9200_TIMER_LATCH;
  73. clkevt.event_handler(&clkevt);
  74. }
  75. return IRQ_HANDLED;
  76. }
  77. /* this irq is shared ... */
  78. return IRQ_NONE;
  79. }
  80. static struct irqaction at91rm9200_timer_irq = {
  81. .name = "at91_tick",
  82. .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  83. .handler = at91rm9200_timer_interrupt,
  84. .irq = NR_IRQS_LEGACY + AT91_ID_SYS,
  85. };
  86. static cycle_t read_clk32k(struct clocksource *cs)
  87. {
  88. return read_CRTR();
  89. }
  90. static struct clocksource clk32k = {
  91. .name = "32k_counter",
  92. .rating = 150,
  93. .read = read_clk32k,
  94. .mask = CLOCKSOURCE_MASK(20),
  95. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  96. };
  97. static void
  98. clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  99. {
  100. /* Disable and flush pending timer interrupts */
  101. at91_st_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
  102. at91_st_read(AT91_ST_SR);
  103. last_crtr = read_CRTR();
  104. switch (mode) {
  105. case CLOCK_EVT_MODE_PERIODIC:
  106. /* PIT for periodic irqs; fixed rate of 1/HZ */
  107. irqmask = AT91_ST_PITS;
  108. at91_st_write(AT91_ST_PIMR, RM9200_TIMER_LATCH);
  109. break;
  110. case CLOCK_EVT_MODE_ONESHOT:
  111. /* ALM for oneshot irqs, set by next_event()
  112. * before 32 seconds have passed
  113. */
  114. irqmask = AT91_ST_ALMS;
  115. at91_st_write(AT91_ST_RTAR, last_crtr);
  116. break;
  117. case CLOCK_EVT_MODE_SHUTDOWN:
  118. case CLOCK_EVT_MODE_UNUSED:
  119. case CLOCK_EVT_MODE_RESUME:
  120. irqmask = 0;
  121. break;
  122. }
  123. at91_st_write(AT91_ST_IER, irqmask);
  124. }
  125. static int
  126. clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
  127. {
  128. u32 alm;
  129. int status = 0;
  130. BUG_ON(delta < 2);
  131. /* The alarm IRQ uses absolute time (now+delta), not the relative
  132. * time (delta) in our calling convention. Like all clockevents
  133. * using such "match" hardware, we have a race to defend against.
  134. *
  135. * Our defense here is to have set up the clockevent device so the
  136. * delta is at least two. That way we never end up writing RTAR
  137. * with the value then held in CRTR ... which would mean the match
  138. * wouldn't trigger until 32 seconds later, after CRTR wraps.
  139. */
  140. alm = read_CRTR();
  141. /* Cancel any pending alarm; flush any pending IRQ */
  142. at91_st_write(AT91_ST_RTAR, alm);
  143. at91_st_read(AT91_ST_SR);
  144. /* Schedule alarm by writing RTAR. */
  145. alm += delta;
  146. at91_st_write(AT91_ST_RTAR, alm);
  147. return status;
  148. }
  149. static struct clock_event_device clkevt = {
  150. .name = "at91_tick",
  151. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  152. .shift = 32,
  153. .rating = 150,
  154. .set_next_event = clkevt32k_next_event,
  155. .set_mode = clkevt32k_mode,
  156. };
  157. void __iomem *at91_st_base;
  158. EXPORT_SYMBOL_GPL(at91_st_base);
  159. #ifdef CONFIG_OF
  160. static struct of_device_id at91rm9200_st_timer_ids[] = {
  161. { .compatible = "atmel,at91rm9200-st" },
  162. { /* sentinel */ }
  163. };
  164. static int __init of_at91rm9200_st_init(void)
  165. {
  166. struct device_node *np;
  167. int ret;
  168. np = of_find_matching_node(NULL, at91rm9200_st_timer_ids);
  169. if (!np)
  170. goto err;
  171. at91_st_base = of_iomap(np, 0);
  172. if (!at91_st_base)
  173. goto node_err;
  174. /* Get the interrupts property */
  175. ret = irq_of_parse_and_map(np, 0);
  176. if (!ret)
  177. goto ioremap_err;
  178. at91rm9200_timer_irq.irq = ret;
  179. of_node_put(np);
  180. return 0;
  181. ioremap_err:
  182. iounmap(at91_st_base);
  183. node_err:
  184. of_node_put(np);
  185. err:
  186. return -EINVAL;
  187. }
  188. #else
  189. static int __init of_at91rm9200_st_init(void)
  190. {
  191. return -EINVAL;
  192. }
  193. #endif
  194. void __init at91rm9200_ioremap_st(u32 addr)
  195. {
  196. #ifdef CONFIG_OF
  197. struct device_node *np;
  198. np = of_find_matching_node(NULL, at91rm9200_st_timer_ids);
  199. if (np) {
  200. of_node_put(np);
  201. return;
  202. }
  203. #endif
  204. at91_st_base = ioremap(addr, 256);
  205. if (!at91_st_base)
  206. panic("Impossible to ioremap ST\n");
  207. }
  208. /*
  209. * ST (system timer) module supports both clockevents and clocksource.
  210. */
  211. void __init at91rm9200_timer_init(void)
  212. {
  213. /* For device tree enabled device: initialize here */
  214. of_at91rm9200_st_init();
  215. /* Disable all timer interrupts, and clear any pending ones */
  216. at91_st_write(AT91_ST_IDR,
  217. AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
  218. at91_st_read(AT91_ST_SR);
  219. /* Make IRQs happen for the system timer */
  220. setup_irq(at91rm9200_timer_irq.irq, &at91rm9200_timer_irq);
  221. /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
  222. * directly for the clocksource and all clockevents, after adjusting
  223. * its prescaler from the 1 Hz default.
  224. */
  225. at91_st_write(AT91_ST_RTMR, 1);
  226. /* Setup timer clockevent, with minimum of two ticks (important!!) */
  227. clkevt.mult = div_sc(AT91_SLOW_CLOCK, NSEC_PER_SEC, clkevt.shift);
  228. clkevt.max_delta_ns = clockevent_delta2ns(AT91_ST_ALMV, &clkevt);
  229. clkevt.min_delta_ns = clockevent_delta2ns(2, &clkevt) + 1;
  230. clkevt.cpumask = cpumask_of(0);
  231. clockevents_register_device(&clkevt);
  232. /* register clocksource */
  233. clocksource_register_hz(&clk32k, AT91_SLOW_CLOCK);
  234. }
  235. struct sys_timer at91rm9200_timer = {
  236. .init = at91rm9200_timer_init,
  237. };