at91sam926x_time.c 4.7 KB

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