at91sam926x_time.c 4.8 KB

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