time.c 5.1 KB

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