timer-atmel-pit.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 readl_relaxed(base + reg_offset);
  54. }
  55. static inline void pit_write(void __iomem *base, unsigned int reg_offset, unsigned long value)
  56. {
  57. writel_relaxed(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. static int pit_clkevt_shutdown(struct clock_event_device *dev)
  78. {
  79. struct pit_data *data = clkevt_to_pit_data(dev);
  80. /* disable irq, leaving the clocksource active */
  81. pit_write(data->base, AT91_PIT_MR, (data->cycle - 1) | AT91_PIT_PITEN);
  82. return 0;
  83. }
  84. /*
  85. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  86. */
  87. static int pit_clkevt_set_periodic(struct clock_event_device *dev)
  88. {
  89. struct pit_data *data = clkevt_to_pit_data(dev);
  90. /* update clocksource counter */
  91. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
  92. pit_write(data->base, AT91_PIT_MR,
  93. (data->cycle - 1) | AT91_PIT_PITEN | AT91_PIT_PITIEN);
  94. return 0;
  95. }
  96. static void at91sam926x_pit_suspend(struct clock_event_device *cedev)
  97. {
  98. struct pit_data *data = clkevt_to_pit_data(cedev);
  99. /* Disable timer */
  100. pit_write(data->base, AT91_PIT_MR, 0);
  101. }
  102. static void at91sam926x_pit_reset(struct pit_data *data)
  103. {
  104. /* Disable timer and irqs */
  105. pit_write(data->base, AT91_PIT_MR, 0);
  106. /* Clear any pending interrupts, wait for PIT to stop counting */
  107. while (PIT_CPIV(pit_read(data->base, AT91_PIT_PIVR)) != 0)
  108. cpu_relax();
  109. /* Start PIT but don't enable IRQ */
  110. pit_write(data->base, AT91_PIT_MR,
  111. (data->cycle - 1) | AT91_PIT_PITEN);
  112. }
  113. static void at91sam926x_pit_resume(struct clock_event_device *cedev)
  114. {
  115. struct pit_data *data = clkevt_to_pit_data(cedev);
  116. at91sam926x_pit_reset(data);
  117. }
  118. /*
  119. * IRQ handler for the timer.
  120. */
  121. static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
  122. {
  123. struct pit_data *data = dev_id;
  124. /* The PIT interrupt may be disabled, and is shared */
  125. if (clockevent_state_periodic(&data->clkevt) &&
  126. (pit_read(data->base, AT91_PIT_SR) & AT91_PIT_PITS)) {
  127. /* Get number of ticks performed before irq, and ack it */
  128. data->cnt += data->cycle * PIT_PICNT(pit_read(data->base,
  129. AT91_PIT_PIVR));
  130. data->clkevt.event_handler(&data->clkevt);
  131. return IRQ_HANDLED;
  132. }
  133. return IRQ_NONE;
  134. }
  135. /*
  136. * Set up both clocksource and clockevent support.
  137. */
  138. static int __init at91sam926x_pit_dt_init(struct device_node *node)
  139. {
  140. unsigned long pit_rate;
  141. unsigned bits;
  142. int ret;
  143. struct pit_data *data;
  144. data = kzalloc(sizeof(*data), GFP_KERNEL);
  145. if (!data)
  146. return -ENOMEM;
  147. data->base = of_iomap(node, 0);
  148. if (!data->base) {
  149. pr_err("Could not map PIT address\n");
  150. return -ENXIO;
  151. }
  152. data->mck = of_clk_get(node, 0);
  153. if (IS_ERR(data->mck)) {
  154. pr_err("Unable to get mck clk\n");
  155. return PTR_ERR(data->mck);
  156. }
  157. ret = clk_prepare_enable(data->mck);
  158. if (ret) {
  159. pr_err("Unable to enable mck\n");
  160. return ret;
  161. }
  162. /* Get the interrupts property */
  163. data->irq = irq_of_parse_and_map(node, 0);
  164. if (!data->irq) {
  165. pr_err("Unable to get IRQ from DT\n");
  166. return -EINVAL;
  167. }
  168. /*
  169. * Use our actual MCK to figure out how many MCK/16 ticks per
  170. * 1/HZ period (instead of a compile-time constant LATCH).
  171. */
  172. pit_rate = clk_get_rate(data->mck) / 16;
  173. data->cycle = DIV_ROUND_CLOSEST(pit_rate, HZ);
  174. WARN_ON(((data->cycle - 1) & ~AT91_PIT_PIV) != 0);
  175. /* Initialize and enable the timer */
  176. at91sam926x_pit_reset(data);
  177. /*
  178. * Register clocksource. The high order bits of PIV are unused,
  179. * so this isn't a 32-bit counter unless we get clockevent irqs.
  180. */
  181. bits = 12 /* PICNT */ + ilog2(data->cycle) /* PIV */;
  182. data->clksrc.mask = CLOCKSOURCE_MASK(bits);
  183. data->clksrc.name = "pit";
  184. data->clksrc.rating = 175;
  185. data->clksrc.read = read_pit_clk;
  186. data->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  187. ret = clocksource_register_hz(&data->clksrc, pit_rate);
  188. if (ret) {
  189. pr_err("Failed to register clocksource");
  190. return ret;
  191. }
  192. /* Set up irq handler */
  193. ret = request_irq(data->irq, at91sam926x_pit_interrupt,
  194. IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
  195. "at91_tick", data);
  196. if (ret) {
  197. pr_err("Unable to setup IRQ\n");
  198. return ret;
  199. }
  200. /* Set up and register clockevents */
  201. data->clkevt.name = "pit";
  202. data->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
  203. data->clkevt.shift = 32;
  204. data->clkevt.mult = div_sc(pit_rate, NSEC_PER_SEC, data->clkevt.shift);
  205. data->clkevt.rating = 100;
  206. data->clkevt.cpumask = cpumask_of(0);
  207. data->clkevt.set_state_shutdown = pit_clkevt_shutdown;
  208. data->clkevt.set_state_periodic = pit_clkevt_set_periodic;
  209. data->clkevt.resume = at91sam926x_pit_resume;
  210. data->clkevt.suspend = at91sam926x_pit_suspend;
  211. clockevents_register_device(&data->clkevt);
  212. return 0;
  213. }
  214. CLOCKSOURCE_OF_DECLARE(at91sam926x_pit, "atmel,at91sam9260-pit",
  215. at91sam926x_pit_dt_init);