at91sam926x_time.c 6.2 KB

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