at91sam926x_time.c 7.1 KB

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