time.c 6.6 KB

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