time.c 6.8 KB

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