time.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * arch/arm/mach-pxa/time.c
  3. *
  4. * PXA clocksource, clockevents, and OST interrupt handlers.
  5. * Copyright (c) 2007 by Bill Gatliff <bgat@billgatliff.com>.
  6. *
  7. * Derived from Nicolas Pitre's PXA timer handler Copyright (c) 2001
  8. * by MontaVista Software, Inc. (Nico, your code rocks!)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/clockchips.h>
  18. #include <asm/mach/irq.h>
  19. #include <asm/mach/time.h>
  20. #include <asm/arch/pxa-regs.h>
  21. static irqreturn_t
  22. pxa_ost0_interrupt(int irq, void *dev_id)
  23. {
  24. int next_match;
  25. struct clock_event_device *c = dev_id;
  26. if (c->mode == CLOCK_EVT_MODE_ONESHOT) {
  27. /* Disarm the compare/match, signal the event. */
  28. OIER &= ~OIER_E0;
  29. c->event_handler(c);
  30. } else if (c->mode == CLOCK_EVT_MODE_PERIODIC) {
  31. /* Call the event handler as many times as necessary
  32. * to recover missed events, if any (if we update
  33. * OSMR0 and OSCR0 is still ahead of us, we've missed
  34. * the event). As we're dealing with that, re-arm the
  35. * compare/match for the next event.
  36. *
  37. * HACK ALERT:
  38. *
  39. * There's a latency between the instruction that
  40. * writes to OSMR0 and the actual commit to the
  41. * physical hardware, because the CPU doesn't (have
  42. * to) run at bus speed, there's a write buffer
  43. * between the CPU and the bus, etc. etc. So if the
  44. * target OSCR0 is "very close", to the OSMR0 load
  45. * value, the update to OSMR0 might not get to the
  46. * hardware in time and we'll miss that interrupt.
  47. *
  48. * To be safe, if the new OSMR0 is "very close" to the
  49. * target OSCR0 value, we call the event_handler as
  50. * though the event actually happened. According to
  51. * Nico's comment in the previous version of this
  52. * code, experience has shown that 6 OSCR ticks is
  53. * "very close" but he went with 8. We will use 16,
  54. * based on the results of testing on PXA270.
  55. *
  56. * To be doubly sure, we also tell clkevt via
  57. * clockevents_register_device() not to ask for
  58. * anything that might put us "very close".
  59. */
  60. #define MIN_OSCR_DELTA 16
  61. do {
  62. OSSR = OSSR_M0;
  63. next_match = (OSMR0 += LATCH);
  64. c->event_handler(c);
  65. } while (((signed long)(next_match - OSCR) <= MIN_OSCR_DELTA)
  66. && (c->mode == CLOCK_EVT_MODE_PERIODIC));
  67. }
  68. return IRQ_HANDLED;
  69. }
  70. static int
  71. pxa_osmr0_set_next_event(unsigned long delta, struct clock_event_device *dev)
  72. {
  73. unsigned long irqflags;
  74. raw_local_irq_save(irqflags);
  75. OSMR0 = OSCR + delta;
  76. OSSR = OSSR_M0;
  77. OIER |= OIER_E0;
  78. raw_local_irq_restore(irqflags);
  79. return 0;
  80. }
  81. static void
  82. pxa_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  83. {
  84. unsigned long irqflags;
  85. switch (mode) {
  86. case CLOCK_EVT_MODE_PERIODIC:
  87. raw_local_irq_save(irqflags);
  88. OSMR0 = OSCR + LATCH;
  89. OSSR = OSSR_M0;
  90. OIER |= OIER_E0;
  91. raw_local_irq_restore(irqflags);
  92. break;
  93. case CLOCK_EVT_MODE_ONESHOT:
  94. raw_local_irq_save(irqflags);
  95. OIER &= ~OIER_E0;
  96. raw_local_irq_restore(irqflags);
  97. break;
  98. case CLOCK_EVT_MODE_UNUSED:
  99. case CLOCK_EVT_MODE_SHUTDOWN:
  100. /* initializing, released, or preparing for suspend */
  101. raw_local_irq_save(irqflags);
  102. OIER &= ~OIER_E0;
  103. raw_local_irq_restore(irqflags);
  104. break;
  105. }
  106. }
  107. static struct clock_event_device ckevt_pxa_osmr0 = {
  108. .name = "osmr0",
  109. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  110. .shift = 32,
  111. .rating = 200,
  112. .cpumask = CPU_MASK_CPU0,
  113. .set_next_event = pxa_osmr0_set_next_event,
  114. .set_mode = pxa_osmr0_set_mode,
  115. };
  116. static cycle_t pxa_read_oscr(void)
  117. {
  118. return OSCR;
  119. }
  120. static struct clocksource cksrc_pxa_oscr0 = {
  121. .name = "oscr0",
  122. .rating = 200,
  123. .read = pxa_read_oscr,
  124. .mask = CLOCKSOURCE_MASK(32),
  125. .shift = 20,
  126. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  127. };
  128. static struct irqaction pxa_ost0_irq = {
  129. .name = "ost0",
  130. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  131. .handler = pxa_ost0_interrupt,
  132. .dev_id = &ckevt_pxa_osmr0,
  133. };
  134. static void __init pxa_timer_init(void)
  135. {
  136. OIER = 0;
  137. OSSR = OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3;
  138. ckevt_pxa_osmr0.mult =
  139. div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC, ckevt_pxa_osmr0.shift);
  140. ckevt_pxa_osmr0.max_delta_ns =
  141. clockevent_delta2ns(0x7fffffff, &ckevt_pxa_osmr0);
  142. ckevt_pxa_osmr0.min_delta_ns =
  143. clockevent_delta2ns(MIN_OSCR_DELTA, &ckevt_pxa_osmr0) + 1;
  144. cksrc_pxa_oscr0.mult =
  145. clocksource_hz2mult(CLOCK_TICK_RATE, cksrc_pxa_oscr0.shift);
  146. setup_irq(IRQ_OST0, &pxa_ost0_irq);
  147. clocksource_register(&cksrc_pxa_oscr0);
  148. clockevents_register_device(&ckevt_pxa_osmr0);
  149. }
  150. #ifdef CONFIG_PM
  151. static unsigned long osmr[4], oier;
  152. static void pxa_timer_suspend(void)
  153. {
  154. osmr[0] = OSMR0;
  155. osmr[1] = OSMR1;
  156. osmr[2] = OSMR2;
  157. osmr[3] = OSMR3;
  158. oier = OIER;
  159. }
  160. static void pxa_timer_resume(void)
  161. {
  162. OSMR0 = osmr[0];
  163. OSMR1 = osmr[1];
  164. OSMR2 = osmr[2];
  165. OSMR3 = osmr[3];
  166. OIER = oier;
  167. /*
  168. * OSCR0 is the system timer, which has to increase
  169. * monotonically until it rolls over in hardware. The value
  170. * (OSMR0 - LATCH) is OSCR0 at the most recent system tick,
  171. * which is a handy value to restore to OSCR0.
  172. */
  173. OSCR = OSMR0 - LATCH;
  174. }
  175. #else
  176. #define pxa_timer_suspend NULL
  177. #define pxa_timer_resume NULL
  178. #endif
  179. struct sys_timer pxa_timer = {
  180. .init = pxa_timer_init,
  181. .suspend = pxa_timer_suspend,
  182. .resume = pxa_timer_resume,
  183. };