time.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <linux/cnt32_to_63.h>
  20. #include <asm/div64.h>
  21. #include <asm/mach/irq.h>
  22. #include <asm/mach/time.h>
  23. #include <mach/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. #define MIN_OSCR_DELTA 16
  54. static irqreturn_t
  55. pxa_ost0_interrupt(int irq, void *dev_id)
  56. {
  57. struct clock_event_device *c = dev_id;
  58. /* Disarm the compare/match, signal the event. */
  59. OIER &= ~OIER_E0;
  60. OSSR = OSSR_M0;
  61. c->event_handler(c);
  62. return IRQ_HANDLED;
  63. }
  64. static int
  65. pxa_osmr0_set_next_event(unsigned long delta, struct clock_event_device *dev)
  66. {
  67. unsigned long flags, next, oscr;
  68. raw_local_irq_save(flags);
  69. OIER |= OIER_E0;
  70. next = OSCR + delta;
  71. OSMR0 = next;
  72. oscr = OSCR;
  73. raw_local_irq_restore(flags);
  74. return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0;
  75. }
  76. static void
  77. pxa_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *dev)
  78. {
  79. unsigned long irqflags;
  80. switch (mode) {
  81. case CLOCK_EVT_MODE_ONESHOT:
  82. raw_local_irq_save(irqflags);
  83. OIER &= ~OIER_E0;
  84. OSSR = OSSR_M0;
  85. raw_local_irq_restore(irqflags);
  86. break;
  87. case CLOCK_EVT_MODE_UNUSED:
  88. case CLOCK_EVT_MODE_SHUTDOWN:
  89. /* initializing, released, or preparing for suspend */
  90. raw_local_irq_save(irqflags);
  91. OIER &= ~OIER_E0;
  92. OSSR = OSSR_M0;
  93. raw_local_irq_restore(irqflags);
  94. break;
  95. case CLOCK_EVT_MODE_RESUME:
  96. case CLOCK_EVT_MODE_PERIODIC:
  97. break;
  98. }
  99. }
  100. static struct clock_event_device ckevt_pxa_osmr0 = {
  101. .name = "osmr0",
  102. .features = CLOCK_EVT_FEAT_ONESHOT,
  103. .shift = 32,
  104. .rating = 200,
  105. .cpumask = CPU_MASK_CPU0,
  106. .set_next_event = pxa_osmr0_set_next_event,
  107. .set_mode = pxa_osmr0_set_mode,
  108. };
  109. static cycle_t pxa_read_oscr(void)
  110. {
  111. return OSCR;
  112. }
  113. static struct clocksource cksrc_pxa_oscr0 = {
  114. .name = "oscr0",
  115. .rating = 200,
  116. .read = pxa_read_oscr,
  117. .mask = CLOCKSOURCE_MASK(32),
  118. .shift = 20,
  119. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  120. };
  121. static struct irqaction pxa_ost0_irq = {
  122. .name = "ost0",
  123. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  124. .handler = pxa_ost0_interrupt,
  125. .dev_id = &ckevt_pxa_osmr0,
  126. };
  127. static void __init pxa_timer_init(void)
  128. {
  129. unsigned long clock_tick_rate;
  130. OIER = 0;
  131. OSSR = OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3;
  132. if (cpu_is_pxa25x())
  133. clock_tick_rate = 3686400;
  134. else if (machine_is_mainstone())
  135. clock_tick_rate = 3249600;
  136. else
  137. clock_tick_rate = 3250000;
  138. set_oscr2ns_scale(clock_tick_rate);
  139. ckevt_pxa_osmr0.mult =
  140. div_sc(clock_tick_rate, NSEC_PER_SEC, ckevt_pxa_osmr0.shift);
  141. ckevt_pxa_osmr0.max_delta_ns =
  142. clockevent_delta2ns(0x7fffffff, &ckevt_pxa_osmr0);
  143. ckevt_pxa_osmr0.min_delta_ns =
  144. clockevent_delta2ns(MIN_OSCR_DELTA * 2, &ckevt_pxa_osmr0) + 1;
  145. cksrc_pxa_oscr0.mult =
  146. clocksource_hz2mult(clock_tick_rate, cksrc_pxa_oscr0.shift);
  147. setup_irq(IRQ_OST0, &pxa_ost0_irq);
  148. clocksource_register(&cksrc_pxa_oscr0);
  149. clockevents_register_device(&ckevt_pxa_osmr0);
  150. }
  151. #ifdef CONFIG_PM
  152. static unsigned long osmr[4], oier, oscr;
  153. static void pxa_timer_suspend(void)
  154. {
  155. osmr[0] = OSMR0;
  156. osmr[1] = OSMR1;
  157. osmr[2] = OSMR2;
  158. osmr[3] = OSMR3;
  159. oier = OIER;
  160. oscr = OSCR;
  161. }
  162. static void pxa_timer_resume(void)
  163. {
  164. /*
  165. * Ensure that we have at least MIN_OSCR_DELTA between match
  166. * register 0 and the OSCR, to guarantee that we will receive
  167. * the one-shot timer interrupt. We adjust OSMR0 in preference
  168. * to OSCR to guarantee that OSCR is monotonically incrementing.
  169. */
  170. if (osmr[0] - oscr < MIN_OSCR_DELTA)
  171. osmr[0] += MIN_OSCR_DELTA;
  172. OSMR0 = osmr[0];
  173. OSMR1 = osmr[1];
  174. OSMR2 = osmr[2];
  175. OSMR3 = osmr[3];
  176. OIER = oier;
  177. OSCR = oscr;
  178. }
  179. #else
  180. #define pxa_timer_suspend NULL
  181. #define pxa_timer_resume NULL
  182. #endif
  183. struct sys_timer pxa_timer = {
  184. .init = pxa_timer_init,
  185. .suspend = pxa_timer_suspend,
  186. .resume = pxa_timer_resume,
  187. };