time.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * arch/arm/mach-pxa/time.c
  3. *
  4. * Author: Nicolas Pitre
  5. * Created: Jun 15, 2001
  6. * Copyright: MontaVista Software Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/time.h>
  17. #include <linux/signal.h>
  18. #include <linux/errno.h>
  19. #include <linux/sched.h>
  20. #include <linux/clocksource.h>
  21. #include <asm/system.h>
  22. #include <asm/hardware.h>
  23. #include <asm/io.h>
  24. #include <asm/leds.h>
  25. #include <asm/irq.h>
  26. #include <asm/mach/irq.h>
  27. #include <asm/mach/time.h>
  28. #include <asm/arch/pxa-regs.h>
  29. static inline unsigned long pxa_get_rtc_time(void)
  30. {
  31. return RCNR;
  32. }
  33. static int pxa_set_rtc(void)
  34. {
  35. unsigned long current_time = xtime.tv_sec;
  36. if (RTSR & RTSR_ALE) {
  37. /* make sure not to forward the clock over an alarm */
  38. unsigned long alarm = RTAR;
  39. if (current_time >= alarm && alarm >= RCNR)
  40. return -ERESTARTSYS;
  41. }
  42. RCNR = current_time;
  43. return 0;
  44. }
  45. #ifdef CONFIG_NO_IDLE_HZ
  46. static unsigned long initial_match;
  47. static int match_posponed;
  48. #endif
  49. static irqreturn_t
  50. pxa_timer_interrupt(int irq, void *dev_id)
  51. {
  52. int next_match;
  53. write_seqlock(&xtime_lock);
  54. #ifdef CONFIG_NO_IDLE_HZ
  55. if (match_posponed) {
  56. match_posponed = 0;
  57. OSMR0 = initial_match;
  58. }
  59. #endif
  60. /* Loop until we get ahead of the free running timer.
  61. * This ensures an exact clock tick count and time accuracy.
  62. * Since IRQs are disabled at this point, coherence between
  63. * lost_ticks(updated in do_timer()) and the match reg value is
  64. * ensured, hence we can use do_gettimeofday() from interrupt
  65. * handlers.
  66. *
  67. * HACK ALERT: it seems that the PXA timer regs aren't updated right
  68. * away in all cases when a write occurs. We therefore compare with
  69. * 8 instead of 0 in the while() condition below to avoid missing a
  70. * match if OSCR has already reached the next OSMR value.
  71. * Experience has shown that up to 6 ticks are needed to work around
  72. * this problem, but let's use 8 to be conservative. Note that this
  73. * affect things only when the timer IRQ has been delayed by nearly
  74. * exactly one tick period which should be a pretty rare event.
  75. */
  76. do {
  77. timer_tick();
  78. OSSR = OSSR_M0; /* Clear match on timer 0 */
  79. next_match = (OSMR0 += LATCH);
  80. } while( (signed long)(next_match - OSCR) <= 8 );
  81. write_sequnlock(&xtime_lock);
  82. return IRQ_HANDLED;
  83. }
  84. static struct irqaction pxa_timer_irq = {
  85. .name = "PXA Timer Tick",
  86. .flags = IRQF_DISABLED | IRQF_TIMER,
  87. .handler = pxa_timer_interrupt,
  88. };
  89. static cycle_t pxa_get_cycles(void)
  90. {
  91. return OSCR;
  92. }
  93. static struct clocksource clocksource_pxa = {
  94. .name = "pxa_timer",
  95. .rating = 200,
  96. .read = pxa_get_cycles,
  97. .mask = CLOCKSOURCE_MASK(32),
  98. .shift = 20,
  99. .is_continuous = 1,
  100. };
  101. static void __init pxa_timer_init(void)
  102. {
  103. struct timespec tv;
  104. unsigned long flags;
  105. set_rtc = pxa_set_rtc;
  106. tv.tv_nsec = 0;
  107. tv.tv_sec = pxa_get_rtc_time();
  108. do_settimeofday(&tv);
  109. OIER = 0; /* disable any timer interrupts */
  110. OSSR = 0xf; /* clear status on all timers */
  111. setup_irq(IRQ_OST0, &pxa_timer_irq);
  112. local_irq_save(flags);
  113. OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
  114. OSMR0 = OSCR + LATCH; /* set initial match */
  115. local_irq_restore(flags);
  116. /*
  117. * OSCR runs continuously on PXA and is not written to,
  118. * so we can use it as clock source directly.
  119. */
  120. clocksource_pxa.mult =
  121. clocksource_hz2mult(CLOCK_TICK_RATE, clocksource_pxa.shift);
  122. clocksource_register(&clocksource_pxa);
  123. }
  124. #ifdef CONFIG_NO_IDLE_HZ
  125. static int pxa_dyn_tick_enable_disable(void)
  126. {
  127. /* nothing to do */
  128. return 0;
  129. }
  130. static void pxa_dyn_tick_reprogram(unsigned long ticks)
  131. {
  132. if (ticks > 1) {
  133. initial_match = OSMR0;
  134. OSMR0 = initial_match + ticks * LATCH;
  135. match_posponed = 1;
  136. }
  137. }
  138. static irqreturn_t
  139. pxa_dyn_tick_handler(int irq, void *dev_id)
  140. {
  141. if (match_posponed) {
  142. match_posponed = 0;
  143. OSMR0 = initial_match;
  144. if ( (signed long)(initial_match - OSCR) <= 8 )
  145. return pxa_timer_interrupt(irq, dev_id);
  146. }
  147. return IRQ_NONE;
  148. }
  149. static struct dyn_tick_timer pxa_dyn_tick = {
  150. .enable = pxa_dyn_tick_enable_disable,
  151. .disable = pxa_dyn_tick_enable_disable,
  152. .reprogram = pxa_dyn_tick_reprogram,
  153. .handler = pxa_dyn_tick_handler,
  154. };
  155. #endif
  156. #ifdef CONFIG_PM
  157. static unsigned long osmr[4], oier;
  158. static void pxa_timer_suspend(void)
  159. {
  160. osmr[0] = OSMR0;
  161. osmr[1] = OSMR1;
  162. osmr[2] = OSMR2;
  163. osmr[3] = OSMR3;
  164. oier = OIER;
  165. }
  166. static void pxa_timer_resume(void)
  167. {
  168. OSMR0 = osmr[0];
  169. OSMR1 = osmr[1];
  170. OSMR2 = osmr[2];
  171. OSMR3 = osmr[3];
  172. OIER = oier;
  173. /*
  174. * OSMR0 is the system timer: make sure OSCR is sufficiently behind
  175. */
  176. OSCR = OSMR0 - LATCH;
  177. }
  178. #else
  179. #define pxa_timer_suspend NULL
  180. #define pxa_timer_resume NULL
  181. #endif
  182. struct sys_timer pxa_timer = {
  183. .init = pxa_timer_init,
  184. .suspend = pxa_timer_suspend,
  185. .resume = pxa_timer_resume,
  186. #ifdef CONFIG_NO_IDLE_HZ
  187. .dyn_tick = &pxa_dyn_tick,
  188. #endif
  189. };