timer-atmel-pit.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * at91sam926x_time.c - Periodic Interval Timer (PIT) for at91sam926x
  3. *
  4. * Copyright (C) 2005-2006 M. Amine SAYA, ATMEL Rousset, France
  5. * Revision 2005 M. Nicolas Diremdjian, ATMEL Rousset, France
  6. * Converted to ClockSource/ClockEvents by David Brownell.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) "AT91: PIT: " fmt
  13. #include <linux/clk.h>
  14. #include <linux/clockchips.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/kernel.h>
  18. #include <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/slab.h>
  22. #define AT91_PIT_MR 0x00 /* Mode Register */
  23. #define AT91_PIT_PITIEN BIT(25) /* Timer Interrupt Enable */
  24. #define AT91_PIT_PITEN BIT(24) /* Timer Enabled */
  25. #define AT91_PIT_PIV GENMASK(19, 0) /* Periodic Interval Value */
  26. #define AT91_PIT_SR 0x04 /* Status Register */
  27. #define AT91_PIT_PITS BIT(0) /* Timer Status */
  28. #define AT91_PIT_PIVR 0x08 /* Periodic Interval Value Register */
  29. #define AT91_PIT_PIIR 0x0c /* Periodic Interval Image Register */
  30. #define AT91_PIT_PICNT GENMASK(31, 20) /* Interval Counter */
  31. #define AT91_PIT_CPIV GENMASK(19, 0) /* Inverval Value */
  32. #define PIT_CPIV(x) ((x) & AT91_PIT_CPIV)
  33. #define PIT_PICNT(x) (((x) & AT91_PIT_PICNT) >> 20)
  34. struct pit_data {
  35. struct clock_event_device clkevt;
  36. struct clocksource clksrc;
  37. void __iomem *base;
  38. u32 cycle;
  39. u32 cnt;
  40. unsigned int irq;
  41. struct clk *mck;
  42. };
  43. static inline struct pit_data *clksrc_to_pit_data(struct clocksource *clksrc)
  44. {
  45. return container_of(clksrc, struct pit_data, clksrc);
  46. }
  47. static inline struct pit_data *clkevt_to_pit_data(struct clock_event_device *clkevt)
  48. {
  49. return container_of(clkevt, struct pit_data, clkevt);
  50. }
  51. static inline unsigned int pit_read(void __iomem *base, unsigned int reg_offset)
  52. {
  53. return __raw_readl(base + reg_offset);
  54. }
  55. static inline void pit_write(void __iomem *base, unsigned int reg_offset, unsigned long value)
  56. {
  57. __raw_writel(value, base + reg_offset);
  58. }
  59. /*
  60. * Clocksource: just a monotonic counter of MCK/16 cycles.
  61. * We don't care whether or not PIT irqs are enabled.
  62. */
  63. static cycle_t read_pit_clk(struct clocksource *cs)
  64. {
  65. struct pit_data *data = clksrc_to_pit_data(cs);
  66. unsigned long flags;
  67. u32 elapsed;
  68. u32 t;
  69. raw_local_irq_save(flags);
  70. elapsed = data->cnt;
  71. t = pit_read(data->base, AT91_PIT_PIIR);
  72. raw_local_irq_restore(flags);
  73. elapsed += PIT_PICNT(t) * data->cycle;
  74. elapsed += PIT_CPIV(t);
  75. return elapsed;
  76. }
  77. /*
  78. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  79. */
  80. static void
  81. pit_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  82. {
  83. struct pit_data *data = clkevt_to_pit_data(dev);
  84. switch (mode) {
  85. case CLOCK_EVT_MODE_PERIODIC:
  86. /* update clocksource counter */
  87. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  88. pit_write(data->base, AT91_PIT_MR,
  89. (data->cycle - 1) | AT91_PIT_PITEN | AT91_PIT_PITIEN);
  90. break;
  91. case CLOCK_EVT_MODE_ONESHOT:
  92. BUG();
  93. /* FALLTHROUGH */
  94. case CLOCK_EVT_MODE_SHUTDOWN:
  95. case CLOCK_EVT_MODE_UNUSED:
  96. /* disable irq, leaving the clocksource active */
  97. pit_write(data->base, AT91_PIT_MR,
  98. (data->cycle - 1) | AT91_PIT_PITEN);
  99. break;
  100. case CLOCK_EVT_MODE_RESUME:
  101. break;
  102. }
  103. }
  104. static void at91sam926x_pit_suspend(struct clock_event_device *cedev)
  105. {
  106. struct pit_data *data = clkevt_to_pit_data(cedev);
  107. /* Disable timer */
  108. pit_write(data->base, AT91_PIT_MR, 0);
  109. }
  110. static void at91sam926x_pit_reset(struct pit_data *data)
  111. {
  112. /* Disable timer and irqs */
  113. pit_write(data->base, AT91_PIT_MR, 0);
  114. /* Clear any pending interrupts, wait for PIT to stop counting */
  115. while (PIT_CPIV(pit_read(data->base, AT91_PIT_PIVR)) != 0)
  116. cpu_relax();
  117. /* Start PIT but don't enable IRQ */
  118. pit_write(data->base, AT91_PIT_MR,
  119. (data->cycle - 1) | AT91_PIT_PITEN);
  120. }
  121. static void at91sam926x_pit_resume(struct clock_event_device *cedev)
  122. {
  123. struct pit_data *data = clkevt_to_pit_data(cedev);
  124. at91sam926x_pit_reset(data);
  125. }
  126. /*
  127. * IRQ handler for the timer.
  128. */
  129. static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
  130. {
  131. struct pit_data *data = dev_id;
  132. /*
  133. * irqs should be disabled here, but as the irq is shared they are only
  134. * guaranteed to be off if the timer irq is registered first.
  135. */
  136. WARN_ON_ONCE(!irqs_disabled());
  137. /* The PIT interrupt may be disabled, and is shared */
  138. if ((data->clkevt.mode == CLOCK_EVT_MODE_PERIODIC) &&
  139. (pit_read(data->base, AT91_PIT_SR) & AT91_PIT_PITS)) {
  140. unsigned nr_ticks;
  141. /* Get number of ticks performed before irq, and ack it */
  142. nr_ticks = PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  143. do {
  144. data->cnt += data->cycle;
  145. data->clkevt.event_handler(&data->clkevt);
  146. nr_ticks--;
  147. } while (nr_ticks);
  148. return IRQ_HANDLED;
  149. }
  150. return IRQ_NONE;
  151. }
  152. /*
  153. * Set up both clocksource and clockevent support.
  154. */
  155. static void __init at91sam926x_pit_common_init(struct pit_data *data)
  156. {
  157. unsigned long pit_rate;
  158. unsigned bits;
  159. int ret;
  160. /*
  161. * Use our actual MCK to figure out how many MCK/16 ticks per
  162. * 1/HZ period (instead of a compile-time constant LATCH).
  163. */
  164. pit_rate = clk_get_rate(data->mck) / 16;
  165. data->cycle = DIV_ROUND_CLOSEST(pit_rate, HZ);
  166. WARN_ON(((data->cycle - 1) & ~AT91_PIT_PIV) != 0);
  167. /* Initialize and enable the timer */
  168. at91sam926x_pit_reset(data);
  169. /*
  170. * Register clocksource. The high order bits of PIV are unused,
  171. * so this isn't a 32-bit counter unless we get clockevent irqs.
  172. */
  173. bits = 12 /* PICNT */ + ilog2(data->cycle) /* PIV */;
  174. data->clksrc.mask = CLOCKSOURCE_MASK(bits);
  175. data->clksrc.name = "pit";
  176. data->clksrc.rating = 175;
  177. data->clksrc.read = read_pit_clk,
  178. data->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS,
  179. clocksource_register_hz(&data->clksrc, pit_rate);
  180. /* Set up irq handler */
  181. ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  182. IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  183. "at91_tick", data);
  184. if (ret)
  185. panic(pr_fmt("Unable to setup IRQ\n"));
  186. /* Set up and register clockevents */
  187. data->clkevt.name = "pit";
  188. data->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
  189. data->clkevt.shift = 32;
  190. data->clkevt.mult = div_sc(pit_rate, NSEC_PER_SEC, data->clkevt.shift);
  191. data->clkevt.rating = 100;
  192. data->clkevt.cpumask = cpumask_of(0);
  193. data->clkevt.set_mode = pit_clkevt_mode;
  194. data->clkevt.resume = at91sam926x_pit_resume;
  195. data->clkevt.suspend = at91sam926x_pit_suspend;
  196. clockevents_register_device(&data->clkevt);
  197. }
  198. static void __init at91sam926x_pit_dt_init(struct device_node *node)
  199. {
  200. struct pit_data *data;
  201. data = kzalloc(sizeof(*data), GFP_KERNEL);
  202. if (!data)
  203. panic(pr_fmt("Unable to allocate memory\n"));
  204. data->base = of_iomap(node, 0);
  205. if (!data->base)
  206. panic(pr_fmt("Could not map PIT address\n"));
  207. data->mck = of_clk_get(node, 0);
  208. if (IS_ERR(data->mck))
  209. /* Fallback on clkdev for !CCF-based boards */
  210. data->mck = clk_get(NULL, "mck");
  211. if (IS_ERR(data->mck))
  212. panic(pr_fmt("Unable to get mck clk\n"));
  213. /* Get the interrupts property */
  214. data->irq = irq_of_parse_and_map(node, 0);
  215. if (!data->irq)
  216. panic(pr_fmt("Unable to get IRQ from DT\n"));
  217. at91sam926x_pit_common_init(data);
  218. }
  219. CLOCKSOURCE_OF_DECLARE(at91sam926x_pit, "atmel,at91sam9260-pit",
  220. at91sam926x_pit_dt_init);
  221. static void __iomem *pit_base_addr;
  222. void __init at91sam926x_pit_init(int irq)
  223. {
  224. struct pit_data *data;
  225. data = kzalloc(sizeof(*data), GFP_KERNEL);
  226. if (!data)
  227. panic(pr_fmt("Unable to allocate memory\n"));
  228. data->base = pit_base_addr;
  229. data->mck = clk_get(NULL, "mck");
  230. if (IS_ERR(data->mck))
  231. panic(pr_fmt("Unable to get mck clk\n"));
  232. data->irq = irq;
  233. at91sam926x_pit_common_init(data);
  234. }
  235. void __init at91sam926x_ioremap_pit(u32 addr)
  236. {
  237. if (of_have_populated_dt())
  238. return;
  239. pit_base_addr = ioremap(addr, 16);
  240. if (!pit_base_addr)
  241. panic(pr_fmt("Impossible to ioremap PIT\n"));
  242. }