at91sam926x_time.c 5.1 KB

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