time.c 4.8 KB

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