at91sam926x_time.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 struct clock_event_device pit_clkevt = {
  93. .name = "pit",
  94. .features = CLOCK_EVT_FEAT_PERIODIC,
  95. .shift = 32,
  96. .rating = 100,
  97. .set_mode = pit_clkevt_mode,
  98. };
  99. /*
  100. * IRQ handler for the timer.
  101. */
  102. static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
  103. {
  104. /*
  105. * irqs should be disabled here, but as the irq is shared they are only
  106. * guaranteed to be off if the timer irq is registered first.
  107. */
  108. WARN_ON_ONCE(!irqs_disabled());
  109. /* The PIT interrupt may be disabled, and is shared */
  110. if ((pit_clkevt.mode == CLOCK_EVT_MODE_PERIODIC)
  111. && (pit_read(AT91_PIT_SR) & AT91_PIT_PITS)) {
  112. unsigned nr_ticks;
  113. /* Get number of ticks performed before irq, and ack it */
  114. nr_ticks = PIT_PICNT(pit_read(AT91_PIT_PIVR));
  115. do {
  116. pit_cnt += pit_cycle;
  117. pit_clkevt.event_handler(&pit_clkevt);
  118. nr_ticks--;
  119. } while (nr_ticks);
  120. return IRQ_HANDLED;
  121. }
  122. return IRQ_NONE;
  123. }
  124. static struct irqaction at91sam926x_pit_irq = {
  125. .name = "at91_tick",
  126. .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  127. .handler = at91sam926x_pit_interrupt,
  128. .irq = NR_IRQS_LEGACY + AT91_ID_SYS,
  129. };
  130. static void at91sam926x_pit_reset(void)
  131. {
  132. /* Disable timer and irqs */
  133. pit_write(AT91_PIT_MR, 0);
  134. /* Clear any pending interrupts, wait for PIT to stop counting */
  135. while (PIT_CPIV(pit_read(AT91_PIT_PIVR)) != 0)
  136. cpu_relax();
  137. /* Start PIT but don't enable IRQ */
  138. pit_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN);
  139. }
  140. #ifdef CONFIG_OF
  141. static struct of_device_id pit_timer_ids[] = {
  142. { .compatible = "atmel,at91sam9260-pit" },
  143. { /* sentinel */ }
  144. };
  145. static int __init of_at91sam926x_pit_init(void)
  146. {
  147. struct device_node *np;
  148. int ret;
  149. np = of_find_matching_node(NULL, pit_timer_ids);
  150. if (!np)
  151. goto err;
  152. pit_base_addr = of_iomap(np, 0);
  153. if (!pit_base_addr)
  154. goto node_err;
  155. /* Get the interrupts property */
  156. ret = irq_of_parse_and_map(np, 0);
  157. if (!ret) {
  158. pr_crit("AT91: PIT: Unable to get IRQ from DT\n");
  159. goto ioremap_err;
  160. }
  161. at91sam926x_pit_irq.irq = ret;
  162. of_node_put(np);
  163. return 0;
  164. ioremap_err:
  165. iounmap(pit_base_addr);
  166. node_err:
  167. of_node_put(np);
  168. err:
  169. return -EINVAL;
  170. }
  171. #else
  172. static int __init of_at91sam926x_pit_init(void)
  173. {
  174. return -EINVAL;
  175. }
  176. #endif
  177. /*
  178. * Set up both clocksource and clockevent support.
  179. */
  180. static void __init at91sam926x_pit_init(void)
  181. {
  182. unsigned long pit_rate;
  183. unsigned bits;
  184. int ret;
  185. /* For device tree enabled device: initialize here */
  186. of_at91sam926x_pit_init();
  187. /*
  188. * Use our actual MCK to figure out how many MCK/16 ticks per
  189. * 1/HZ period (instead of a compile-time constant LATCH).
  190. */
  191. pit_rate = clk_get_rate(clk_get(NULL, "mck")) / 16;
  192. pit_cycle = (pit_rate + HZ/2) / HZ;
  193. WARN_ON(((pit_cycle - 1) & ~AT91_PIT_PIV) != 0);
  194. /* Initialize and enable the timer */
  195. at91sam926x_pit_reset();
  196. /*
  197. * Register clocksource. The high order bits of PIV are unused,
  198. * so this isn't a 32-bit counter unless we get clockevent irqs.
  199. */
  200. bits = 12 /* PICNT */ + ilog2(pit_cycle) /* PIV */;
  201. pit_clk.mask = CLOCKSOURCE_MASK(bits);
  202. clocksource_register_hz(&pit_clk, pit_rate);
  203. /* Set up irq handler */
  204. ret = setup_irq(at91sam926x_pit_irq.irq, &at91sam926x_pit_irq);
  205. if (ret)
  206. pr_crit("AT91: PIT: Unable to setup IRQ\n");
  207. /* Set up and register clockevents */
  208. pit_clkevt.mult = div_sc(pit_rate, NSEC_PER_SEC, pit_clkevt.shift);
  209. pit_clkevt.cpumask = cpumask_of(0);
  210. clockevents_register_device(&pit_clkevt);
  211. }
  212. static void at91sam926x_pit_suspend(void)
  213. {
  214. /* Disable timer */
  215. pit_write(AT91_PIT_MR, 0);
  216. }
  217. void __init at91sam926x_ioremap_pit(u32 addr)
  218. {
  219. #if defined(CONFIG_OF)
  220. struct device_node *np =
  221. of_find_matching_node(NULL, pit_timer_ids);
  222. if (np) {
  223. of_node_put(np);
  224. return;
  225. }
  226. #endif
  227. pit_base_addr = ioremap(addr, 16);
  228. if (!pit_base_addr)
  229. panic("Impossible to ioremap PIT\n");
  230. }
  231. struct sys_timer at91sam926x_timer = {
  232. .init = at91sam926x_pit_init,
  233. .suspend = at91sam926x_pit_suspend,
  234. .resume = at91sam926x_pit_reset,
  235. };