timer-atmel-st.c 6.2 KB

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