at91sam926x_time.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/kernel.h>
  15. #include <linux/clk.h>
  16. #include <linux/clockchips.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <asm/mach/time.h>
  21. #define AT91_PIT_MR 0x00 /* Mode Register */
  22. #define AT91_PIT_PITIEN (1 << 25) /* Timer Interrupt Enable */
  23. #define AT91_PIT_PITEN (1 << 24) /* Timer Enabled */
  24. #define AT91_PIT_PIV (0xfffff) /* Periodic Interval Value */
  25. #define AT91_PIT_SR 0x04 /* Status Register */
  26. #define AT91_PIT_PITS (1 << 0) /* Timer Status */
  27. #define AT91_PIT_PIVR 0x08 /* Periodic Interval Value Register */
  28. #define AT91_PIT_PIIR 0x0c /* Periodic Interval Image Register */
  29. #define AT91_PIT_PICNT (0xfff << 20) /* Interval Counter */
  30. #define AT91_PIT_CPIV (0xfffff) /* Inverval Value */
  31. #define PIT_CPIV(x) ((x) & AT91_PIT_CPIV)
  32. #define PIT_PICNT(x) (((x) & AT91_PIT_PICNT) >> 20)
  33. static u32 pit_cycle; /* write-once */
  34. static u32 pit_cnt; /* access only w/system irq blocked */
  35. static void __iomem *pit_base_addr __read_mostly;
  36. static inline unsigned int pit_read(unsigned int reg_offset)
  37. {
  38. return __raw_readl(pit_base_addr + reg_offset);
  39. }
  40. static inline void pit_write(unsigned int reg_offset, unsigned long value)
  41. {
  42. __raw_writel(value, pit_base_addr + reg_offset);
  43. }
  44. /*
  45. * Clocksource: just a monotonic counter of MCK/16 cycles.
  46. * We don't care whether or not PIT irqs are enabled.
  47. */
  48. static cycle_t read_pit_clk(struct clocksource *cs)
  49. {
  50. unsigned long flags;
  51. u32 elapsed;
  52. u32 t;
  53. raw_local_irq_save(flags);
  54. elapsed = pit_cnt;
  55. t = pit_read(AT91_PIT_PIIR);
  56. raw_local_irq_restore(flags);
  57. elapsed += PIT_PICNT(t) * pit_cycle;
  58. elapsed += PIT_CPIV(t);
  59. return elapsed;
  60. }
  61. static struct clocksource pit_clk = {
  62. .name = "pit",
  63. .rating = 175,
  64. .read = read_pit_clk,
  65. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  66. };
  67. /*
  68. * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
  69. */
  70. static void
  71. pit_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  72. {
  73. switch (mode) {
  74. case CLOCK_EVT_MODE_PERIODIC:
  75. /* update clocksource counter */
  76. pit_cnt += pit_cycle * PIT_PICNT(pit_read(AT91_PIT_PIVR));
  77. pit_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN
  78. | AT91_PIT_PITIEN);
  79. break;
  80. case CLOCK_EVT_MODE_ONESHOT:
  81. BUG();
  82. /* FALLTHROUGH */
  83. case CLOCK_EVT_MODE_SHUTDOWN:
  84. case CLOCK_EVT_MODE_UNUSED:
  85. /* disable irq, leaving the clocksource active */
  86. pit_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN);
  87. break;
  88. case CLOCK_EVT_MODE_RESUME:
  89. break;
  90. }
  91. }
  92. static void at91sam926x_pit_suspend(struct clock_event_device *cedev)
  93. {
  94. /* Disable timer */
  95. pit_write(AT91_PIT_MR, 0);
  96. }
  97. static void at91sam926x_pit_reset(void)
  98. {
  99. /* Disable timer and irqs */
  100. pit_write(AT91_PIT_MR, 0);
  101. /* Clear any pending interrupts, wait for PIT to stop counting */
  102. while (PIT_CPIV(pit_read(AT91_PIT_PIVR)) != 0)
  103. cpu_relax();
  104. /* Start PIT but don't enable IRQ */
  105. pit_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN);
  106. }
  107. static void at91sam926x_pit_resume(struct clock_event_device *cedev)
  108. {
  109. at91sam926x_pit_reset();
  110. }
  111. static struct clock_event_device pit_clkevt = {
  112. .name = "pit",
  113. .features = CLOCK_EVT_FEAT_PERIODIC,
  114. .shift = 32,
  115. .rating = 100,
  116. .set_mode = pit_clkevt_mode,
  117. .suspend = at91sam926x_pit_suspend,
  118. .resume = at91sam926x_pit_resume,
  119. };
  120. /*
  121. * IRQ handler for the timer.
  122. */
  123. static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
  124. {
  125. /*
  126. * irqs should be disabled here, but as the irq is shared they are only
  127. * guaranteed to be off if the timer irq is registered first.
  128. */
  129. WARN_ON_ONCE(!irqs_disabled());
  130. /* The PIT interrupt may be disabled, and is shared */
  131. if ((pit_clkevt.mode == CLOCK_EVT_MODE_PERIODIC)
  132. && (pit_read(AT91_PIT_SR) & AT91_PIT_PITS)) {
  133. unsigned nr_ticks;
  134. /* Get number of ticks performed before irq, and ack it */
  135. nr_ticks = PIT_PICNT(pit_read(AT91_PIT_PIVR));
  136. do {
  137. pit_cnt += pit_cycle;
  138. pit_clkevt.event_handler(&pit_clkevt);
  139. nr_ticks--;
  140. } while (nr_ticks);
  141. return IRQ_HANDLED;
  142. }
  143. return IRQ_NONE;
  144. }
  145. static struct irqaction at91sam926x_pit_irq = {
  146. .name = "at91_tick",
  147. .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  148. .handler = at91sam926x_pit_interrupt,
  149. .irq = NR_IRQS_LEGACY + AT91_ID_SYS,
  150. };
  151. #ifdef CONFIG_OF
  152. static struct of_device_id pit_timer_ids[] = {
  153. { .compatible = "atmel,at91sam9260-pit" },
  154. { /* sentinel */ }
  155. };
  156. static int __init of_at91sam926x_pit_init(void)
  157. {
  158. struct device_node *np;
  159. int ret;
  160. np = of_find_matching_node(NULL, pit_timer_ids);
  161. if (!np)
  162. goto err;
  163. pit_base_addr = of_iomap(np, 0);
  164. if (!pit_base_addr)
  165. goto node_err;
  166. /* Get the interrupts property */
  167. ret = irq_of_parse_and_map(np, 0);
  168. if (!ret) {
  169. pr_crit("AT91: PIT: Unable to get IRQ from DT\n");
  170. goto ioremap_err;
  171. }
  172. at91sam926x_pit_irq.irq = ret;
  173. of_node_put(np);
  174. return 0;
  175. ioremap_err:
  176. iounmap(pit_base_addr);
  177. node_err:
  178. of_node_put(np);
  179. err:
  180. return -EINVAL;
  181. }
  182. #else
  183. static int __init of_at91sam926x_pit_init(void)
  184. {
  185. return -EINVAL;
  186. }
  187. #endif
  188. /*
  189. * Set up both clocksource and clockevent support.
  190. */
  191. void __init at91sam926x_pit_init(void)
  192. {
  193. unsigned long pit_rate;
  194. unsigned bits;
  195. int ret;
  196. /* For device tree enabled device: initialize here */
  197. of_at91sam926x_pit_init();
  198. /*
  199. * Use our actual MCK to figure out how many MCK/16 ticks per
  200. * 1/HZ period (instead of a compile-time constant LATCH).
  201. */
  202. pit_rate = clk_get_rate(clk_get(NULL, "mck")) / 16;
  203. pit_cycle = (pit_rate + HZ/2) / HZ;
  204. WARN_ON(((pit_cycle - 1) & ~AT91_PIT_PIV) != 0);
  205. /* Initialize and enable the timer */
  206. at91sam926x_pit_reset();
  207. /*
  208. * Register clocksource. The high order bits of PIV are unused,
  209. * so this isn't a 32-bit counter unless we get clockevent irqs.
  210. */
  211. bits = 12 /* PICNT */ + ilog2(pit_cycle) /* PIV */;
  212. pit_clk.mask = CLOCKSOURCE_MASK(bits);
  213. clocksource_register_hz(&pit_clk, pit_rate);
  214. /* Set up irq handler */
  215. ret = setup_irq(at91sam926x_pit_irq.irq, &at91sam926x_pit_irq);
  216. if (ret)
  217. pr_crit("AT91: PIT: Unable to setup IRQ\n");
  218. /* Set up and register clockevents */
  219. pit_clkevt.mult = div_sc(pit_rate, NSEC_PER_SEC, pit_clkevt.shift);
  220. pit_clkevt.cpumask = cpumask_of(0);
  221. clockevents_register_device(&pit_clkevt);
  222. }
  223. void __init at91sam926x_ioremap_pit(u32 addr)
  224. {
  225. #if defined(CONFIG_OF)
  226. struct device_node *np =
  227. of_find_matching_node(NULL, pit_timer_ids);
  228. if (np) {
  229. of_node_put(np);
  230. return;
  231. }
  232. #endif
  233. pit_base_addr = ioremap(addr, 16);
  234. if (!pit_base_addr)
  235. panic("Impossible to ioremap PIT\n");
  236. }