time.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 <linux/sched.h>
  19. #include <asm/div64.h>
  20. #include <asm/cnt32_to_63.h>
  21. #include <asm/mach/irq.h>
  22. #include <asm/mach/time.h>
  23. #include <asm/arch/pxa-regs.h>
  24. /*
  25. * This is PXA's sched_clock implementation. This has a resolution
  26. * of at least 308 ns and a maximum value of 208 days.
  27. *
  28. * The return value is guaranteed to be monotonic in that range as
  29. * long as there is always less than 582 seconds between successive
  30. * calls to sched_clock() which should always be the case in practice.
  31. */
  32. #define OSCR2NS_SCALE_FACTOR 10
  33. static unsigned long oscr2ns_scale;
  34. static void __init set_oscr2ns_scale(unsigned long oscr_rate)
  35. {
  36. unsigned long long v = 1000000000ULL << OSCR2NS_SCALE_FACTOR;
  37. do_div(v, oscr_rate);
  38. oscr2ns_scale = v;
  39. /*
  40. * We want an even value to automatically clear the top bit
  41. * returned by cnt32_to_63() without an additional run time
  42. * instruction. So if the LSB is 1 then round it up.
  43. */
  44. if (oscr2ns_scale & 1)
  45. oscr2ns_scale++;
  46. }
  47. unsigned long long sched_clock(void)
  48. {
  49. unsigned long long v = cnt32_to_63(OSCR);
  50. return (v * oscr2ns_scale) >> OSCR2NS_SCALE_FACTOR;
  51. }
  52. static irqreturn_t
  53. pxa_ost0_interrupt(int irq, void *dev_id)
  54. {
  55. int next_match;
  56. struct clock_event_device *c = dev_id;
  57. if (c->mode == CLOCK_EVT_MODE_ONESHOT) {
  58. /* Disarm the compare/match, signal the event. */
  59. OIER &= ~OIER_E0;
  60. c->event_handler(c);
  61. } else if (c->mode == CLOCK_EVT_MODE_PERIODIC) {
  62. /* Call the event handler as many times as necessary
  63. * to recover missed events, if any (if we update
  64. * OSMR0 and OSCR0 is still ahead of us, we've missed
  65. * the event). As we're dealing with that, re-arm the
  66. * compare/match for the next event.
  67. *
  68. * HACK ALERT:
  69. *
  70. * There's a latency between the instruction that
  71. * writes to OSMR0 and the actual commit to the
  72. * physical hardware, because the CPU doesn't (have
  73. * to) run at bus speed, there's a write buffer
  74. * between the CPU and the bus, etc. etc. So if the
  75. * target OSCR0 is "very close", to the OSMR0 load
  76. * value, the update to OSMR0 might not get to the
  77. * hardware in time and we'll miss that interrupt.
  78. *
  79. * To be safe, if the new OSMR0 is "very close" to the
  80. * target OSCR0 value, we call the event_handler as
  81. * though the event actually happened. According to
  82. * Nico's comment in the previous version of this
  83. * code, experience has shown that 6 OSCR ticks is
  84. * "very close" but he went with 8. We will use 16,
  85. * based on the results of testing on PXA270.
  86. *
  87. * To be doubly sure, we also tell clkevt via
  88. * clockevents_register_device() not to ask for
  89. * anything that might put us "very close".
  90. */
  91. #define MIN_OSCR_DELTA 16
  92. do {
  93. OSSR = OSSR_M0;
  94. next_match = (OSMR0 += LATCH);
  95. c->event_handler(c);
  96. } while (((signed long)(next_match - OSCR) <= MIN_OSCR_DELTA)
  97. && (c->mode == CLOCK_EVT_MODE_PERIODIC));
  98. }
  99. return IRQ_HANDLED;
  100. }
  101. static int
  102. pxa_osmr0_set_next_event(unsigned long delta, struct clock_event_device *dev)
  103. {
  104. unsigned long irqflags;
  105. raw_local_irq_save(irqflags);
  106. OSMR0 = OSCR + delta;
  107. OSSR = OSSR_M0;
  108. OIER |= OIER_E0;
  109. raw_local_irq_restore(irqflags);
  110. return 0;
  111. }
  112. static void
  113. pxa_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  114. {
  115. unsigned long irqflags;
  116. switch (mode) {
  117. case CLOCK_EVT_MODE_PERIODIC:
  118. raw_local_irq_save(irqflags);
  119. OSMR0 = OSCR + LATCH;
  120. OSSR = OSSR_M0;
  121. OIER |= OIER_E0;
  122. raw_local_irq_restore(irqflags);
  123. break;
  124. case CLOCK_EVT_MODE_ONESHOT:
  125. raw_local_irq_save(irqflags);
  126. OIER &= ~OIER_E0;
  127. raw_local_irq_restore(irqflags);
  128. break;
  129. case CLOCK_EVT_MODE_UNUSED:
  130. case CLOCK_EVT_MODE_SHUTDOWN:
  131. /* initializing, released, or preparing for suspend */
  132. raw_local_irq_save(irqflags);
  133. OIER &= ~OIER_E0;
  134. raw_local_irq_restore(irqflags);
  135. break;
  136. }
  137. }
  138. static struct clock_event_device ckevt_pxa_osmr0 = {
  139. .name = "osmr0",
  140. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  141. .shift = 32,
  142. .rating = 200,
  143. .cpumask = CPU_MASK_CPU0,
  144. .set_next_event = pxa_osmr0_set_next_event,
  145. .set_mode = pxa_osmr0_set_mode,
  146. };
  147. static cycle_t pxa_read_oscr(void)
  148. {
  149. return OSCR;
  150. }
  151. static struct clocksource cksrc_pxa_oscr0 = {
  152. .name = "oscr0",
  153. .rating = 200,
  154. .read = pxa_read_oscr,
  155. .mask = CLOCKSOURCE_MASK(32),
  156. .shift = 20,
  157. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  158. };
  159. static struct irqaction pxa_ost0_irq = {
  160. .name = "ost0",
  161. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  162. .handler = pxa_ost0_interrupt,
  163. .dev_id = &ckevt_pxa_osmr0,
  164. };
  165. static void __init pxa_timer_init(void)
  166. {
  167. OIER = 0;
  168. OSSR = OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3;
  169. set_oscr2ns_scale(CLOCK_TICK_RATE);
  170. ckevt_pxa_osmr0.mult =
  171. div_sc(CLOCK_TICK_RATE, NSEC_PER_SEC, ckevt_pxa_osmr0.shift);
  172. ckevt_pxa_osmr0.max_delta_ns =
  173. clockevent_delta2ns(0x7fffffff, &ckevt_pxa_osmr0);
  174. ckevt_pxa_osmr0.min_delta_ns =
  175. clockevent_delta2ns(MIN_OSCR_DELTA, &ckevt_pxa_osmr0) + 1;
  176. cksrc_pxa_oscr0.mult =
  177. clocksource_hz2mult(CLOCK_TICK_RATE, cksrc_pxa_oscr0.shift);
  178. setup_irq(IRQ_OST0, &pxa_ost0_irq);
  179. clocksource_register(&cksrc_pxa_oscr0);
  180. clockevents_register_device(&ckevt_pxa_osmr0);
  181. }
  182. #ifdef CONFIG_PM
  183. static unsigned long osmr[4], oier;
  184. static void pxa_timer_suspend(void)
  185. {
  186. osmr[0] = OSMR0;
  187. osmr[1] = OSMR1;
  188. osmr[2] = OSMR2;
  189. osmr[3] = OSMR3;
  190. oier = OIER;
  191. }
  192. static void pxa_timer_resume(void)
  193. {
  194. OSMR0 = osmr[0];
  195. OSMR1 = osmr[1];
  196. OSMR2 = osmr[2];
  197. OSMR3 = osmr[3];
  198. OIER = oier;
  199. /*
  200. * OSCR0 is the system timer, which has to increase
  201. * monotonically until it rolls over in hardware. The value
  202. * (OSMR0 - LATCH) is OSCR0 at the most recent system tick,
  203. * which is a handy value to restore to OSCR0.
  204. */
  205. OSCR = OSMR0 - LATCH;
  206. }
  207. #else
  208. #define pxa_timer_suspend NULL
  209. #define pxa_timer_resume NULL
  210. #endif
  211. struct sys_timer pxa_timer = {
  212. .init = pxa_timer_init,
  213. .suspend = pxa_timer_suspend,
  214. .resume = pxa_timer_resume,
  215. };