at91sam926x_time.c 4.7 KB

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