timer-atmel-st.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/clk.h>
  25. #include <linux/clockchips.h>
  26. #include <linux/export.h>
  27. #include <linux/mfd/syscon.h>
  28. #include <linux/mfd/syscon/atmel-st.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/regmap.h>
  31. static unsigned long last_crtr;
  32. static u32 irqmask;
  33. static struct clock_event_device clkevt;
  34. static struct regmap *regmap_st;
  35. static int timer_latch;
  36. /*
  37. * The ST_CRTR is updated asynchronously to the master clock ... but
  38. * the updates as seen by the CPU don't seem to be strictly monotonic.
  39. * Waiting until we read the same value twice avoids glitching.
  40. */
  41. static inline unsigned long read_CRTR(void)
  42. {
  43. unsigned int x1, x2;
  44. regmap_read(regmap_st, AT91_ST_CRTR, &x1);
  45. do {
  46. regmap_read(regmap_st, AT91_ST_CRTR, &x2);
  47. if (x1 == x2)
  48. break;
  49. x1 = x2;
  50. } while (1);
  51. return x1;
  52. }
  53. /*
  54. * IRQ handler for the timer.
  55. */
  56. static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
  57. {
  58. u32 sr;
  59. regmap_read(regmap_st, AT91_ST_SR, &sr);
  60. sr &= irqmask;
  61. /*
  62. * irqs should be disabled here, but as the irq is shared they are only
  63. * guaranteed to be off if the timer irq is registered first.
  64. */
  65. WARN_ON_ONCE(!irqs_disabled());
  66. /* simulate "oneshot" timer with alarm */
  67. if (sr & AT91_ST_ALMS) {
  68. clkevt.event_handler(&clkevt);
  69. return IRQ_HANDLED;
  70. }
  71. /* periodic mode should handle delayed ticks */
  72. if (sr & AT91_ST_PITS) {
  73. u32 crtr = read_CRTR();
  74. while (((crtr - last_crtr) & AT91_ST_CRTV) >= timer_latch) {
  75. last_crtr += timer_latch;
  76. clkevt.event_handler(&clkevt);
  77. }
  78. return IRQ_HANDLED;
  79. }
  80. /* this irq is shared ... */
  81. return IRQ_NONE;
  82. }
  83. static cycle_t read_clk32k(struct clocksource *cs)
  84. {
  85. return read_CRTR();
  86. }
  87. static struct clocksource clk32k = {
  88. .name = "32k_counter",
  89. .rating = 150,
  90. .read = read_clk32k,
  91. .mask = CLOCKSOURCE_MASK(20),
  92. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  93. };
  94. static void clkdev32k_disable_and_flush_irq(void)
  95. {
  96. unsigned int val;
  97. /* Disable and flush pending timer interrupts */
  98. regmap_write(regmap_st, AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
  99. regmap_read(regmap_st, AT91_ST_SR, &val);
  100. last_crtr = read_CRTR();
  101. }
  102. static int clkevt32k_shutdown(struct clock_event_device *evt)
  103. {
  104. clkdev32k_disable_and_flush_irq();
  105. irqmask = 0;
  106. regmap_write(regmap_st, AT91_ST_IER, irqmask);
  107. return 0;
  108. }
  109. static int clkevt32k_set_oneshot(struct clock_event_device *dev)
  110. {
  111. clkdev32k_disable_and_flush_irq();
  112. /*
  113. * ALM for oneshot irqs, set by next_event()
  114. * before 32 seconds have passed.
  115. */
  116. irqmask = AT91_ST_ALMS;
  117. regmap_write(regmap_st, AT91_ST_RTAR, last_crtr);
  118. regmap_write(regmap_st, AT91_ST_IER, irqmask);
  119. return 0;
  120. }
  121. static int clkevt32k_set_periodic(struct clock_event_device *dev)
  122. {
  123. clkdev32k_disable_and_flush_irq();
  124. /* PIT for periodic irqs; fixed rate of 1/HZ */
  125. irqmask = AT91_ST_PITS;
  126. regmap_write(regmap_st, AT91_ST_PIMR, timer_latch);
  127. regmap_write(regmap_st, AT91_ST_IER, irqmask);
  128. return 0;
  129. }
  130. static int
  131. clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
  132. {
  133. u32 alm;
  134. int status = 0;
  135. unsigned int val;
  136. BUG_ON(delta < 2);
  137. /* The alarm IRQ uses absolute time (now+delta), not the relative
  138. * time (delta) in our calling convention. Like all clockevents
  139. * using such "match" hardware, we have a race to defend against.
  140. *
  141. * Our defense here is to have set up the clockevent device so the
  142. * delta is at least two. That way we never end up writing RTAR
  143. * with the value then held in CRTR ... which would mean the match
  144. * wouldn't trigger until 32 seconds later, after CRTR wraps.
  145. */
  146. alm = read_CRTR();
  147. /* Cancel any pending alarm; flush any pending IRQ */
  148. regmap_write(regmap_st, AT91_ST_RTAR, alm);
  149. regmap_read(regmap_st, AT91_ST_SR, &val);
  150. /* Schedule alarm by writing RTAR. */
  151. alm += delta;
  152. regmap_write(regmap_st, AT91_ST_RTAR, alm);
  153. return status;
  154. }
  155. static struct clock_event_device clkevt = {
  156. .name = "at91_tick",
  157. .features = CLOCK_EVT_FEAT_PERIODIC |
  158. CLOCK_EVT_FEAT_ONESHOT,
  159. .rating = 150,
  160. .set_next_event = clkevt32k_next_event,
  161. .set_state_shutdown = clkevt32k_shutdown,
  162. .set_state_periodic = clkevt32k_set_periodic,
  163. .set_state_oneshot = clkevt32k_set_oneshot,
  164. .tick_resume = clkevt32k_shutdown,
  165. };
  166. /*
  167. * ST (system timer) module supports both clockevents and clocksource.
  168. */
  169. static void __init atmel_st_timer_init(struct device_node *node)
  170. {
  171. struct clk *sclk;
  172. unsigned int sclk_rate, val;
  173. int irq, ret;
  174. regmap_st = syscon_node_to_regmap(node);
  175. if (IS_ERR(regmap_st))
  176. panic(pr_fmt("Unable to get regmap\n"));
  177. /* Disable all timer interrupts, and clear any pending ones */
  178. regmap_write(regmap_st, AT91_ST_IDR,
  179. AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
  180. regmap_read(regmap_st, AT91_ST_SR, &val);
  181. /* Get the interrupts property */
  182. irq = irq_of_parse_and_map(node, 0);
  183. if (!irq)
  184. panic(pr_fmt("Unable to get IRQ from DT\n"));
  185. /* Make IRQs happen for the system timer */
  186. ret = request_irq(irq, at91rm9200_timer_interrupt,
  187. IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  188. "at91_tick", regmap_st);
  189. if (ret)
  190. panic(pr_fmt("Unable to setup IRQ\n"));
  191. sclk = of_clk_get(node, 0);
  192. if (IS_ERR(sclk))
  193. panic(pr_fmt("Unable to get slow clock\n"));
  194. clk_prepare_enable(sclk);
  195. if (ret)
  196. panic(pr_fmt("Could not enable slow clock\n"));
  197. sclk_rate = clk_get_rate(sclk);
  198. if (!sclk_rate)
  199. panic(pr_fmt("Invalid slow clock rate\n"));
  200. timer_latch = (sclk_rate + HZ / 2) / HZ;
  201. /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
  202. * directly for the clocksource and all clockevents, after adjusting
  203. * its prescaler from the 1 Hz default.
  204. */
  205. regmap_write(regmap_st, AT91_ST_RTMR, 1);
  206. /* Setup timer clockevent, with minimum of two ticks (important!!) */
  207. clkevt.cpumask = cpumask_of(0);
  208. clockevents_config_and_register(&clkevt, sclk_rate,
  209. 2, AT91_ST_ALMV);
  210. /* register clocksource */
  211. clocksource_register_hz(&clk32k, sclk_rate);
  212. }
  213. CLOCKSOURCE_OF_DECLARE(atmel_st_timer, "atmel,at91rm9200-st",
  214. atmel_st_timer_init);