time.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/config.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/time.h>
  18. #include <linux/signal.h>
  19. #include <linux/errno.h>
  20. #include <linux/sched.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. /* IRQs are disabled before entering here from do_gettimeofday() */
  46. static unsigned long pxa_gettimeoffset (void)
  47. {
  48. long ticks_to_match, elapsed, usec;
  49. /* Get ticks before next timer match */
  50. ticks_to_match = OSMR0 - OSCR;
  51. /* We need elapsed ticks since last match */
  52. elapsed = LATCH - ticks_to_match;
  53. /* don't get fooled by the workaround in pxa_timer_interrupt() */
  54. if (elapsed <= 0)
  55. return 0;
  56. /* Now convert them to usec */
  57. usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
  58. return usec;
  59. }
  60. #ifdef CONFIG_NO_IDLE_HZ
  61. static unsigned long initial_match;
  62. static int match_posponed;
  63. #endif
  64. static irqreturn_t
  65. pxa_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  66. {
  67. int next_match;
  68. write_seqlock(&xtime_lock);
  69. #ifdef CONFIG_NO_IDLE_HZ
  70. if (match_posponed) {
  71. match_posponed = 0;
  72. OSMR0 = initial_match;
  73. }
  74. #endif
  75. /* Loop until we get ahead of the free running timer.
  76. * This ensures an exact clock tick count and time accuracy.
  77. * IRQs are disabled inside the loop to ensure coherence between
  78. * lost_ticks (updated in do_timer()) and the match reg value, so we
  79. * can use do_gettimeofday() from interrupt handlers.
  80. *
  81. * HACK ALERT: it seems that the PXA timer regs aren't updated right
  82. * away in all cases when a write occurs. We therefore compare with
  83. * 8 instead of 0 in the while() condition below to avoid missing a
  84. * match if OSCR has already reached the next OSMR value.
  85. * Experience has shown that up to 6 ticks are needed to work around
  86. * this problem, but let's use 8 to be conservative. Note that this
  87. * affect things only when the timer IRQ has been delayed by nearly
  88. * exactly one tick period which should be a pretty rare event.
  89. */
  90. do {
  91. timer_tick(regs);
  92. OSSR = OSSR_M0; /* Clear match on timer 0 */
  93. next_match = (OSMR0 += LATCH);
  94. } while( (signed long)(next_match - OSCR) <= 8 );
  95. write_sequnlock(&xtime_lock);
  96. return IRQ_HANDLED;
  97. }
  98. static struct irqaction pxa_timer_irq = {
  99. .name = "PXA Timer Tick",
  100. .flags = SA_INTERRUPT | SA_TIMER,
  101. .handler = pxa_timer_interrupt,
  102. };
  103. static void __init pxa_timer_init(void)
  104. {
  105. struct timespec tv;
  106. set_rtc = pxa_set_rtc;
  107. tv.tv_nsec = 0;
  108. tv.tv_sec = pxa_get_rtc_time();
  109. do_settimeofday(&tv);
  110. OSMR0 = 0; /* set initial match at 0 */
  111. OSSR = 0xf; /* clear status on all timers */
  112. setup_irq(IRQ_OST0, &pxa_timer_irq);
  113. OIER |= OIER_E0; /* enable match on timer 0 to cause interrupts */
  114. OSCR = 0; /* initialize free-running timer, force first match */
  115. }
  116. #ifdef CONFIG_NO_IDLE_HZ
  117. static int pxa_dyn_tick_enable_disable(void)
  118. {
  119. /* nothing to do */
  120. return 0;
  121. }
  122. static void pxa_dyn_tick_reprogram(unsigned long ticks)
  123. {
  124. if (ticks > 1) {
  125. initial_match = OSMR0;
  126. OSMR0 = initial_match + ticks * LATCH;
  127. match_posponed = 1;
  128. }
  129. }
  130. static irqreturn_t
  131. pxa_dyn_tick_handler(int irq, void *dev_id, struct pt_regs *regs)
  132. {
  133. if (match_posponed) {
  134. match_posponed = 0;
  135. OSMR0 = initial_match;
  136. if ( (signed long)(initial_match - OSCR) <= 8 )
  137. return pxa_timer_interrupt(irq, dev_id, regs);
  138. }
  139. return IRQ_NONE;
  140. }
  141. static struct dyn_tick_timer pxa_dyn_tick = {
  142. .enable = pxa_dyn_tick_enable_disable,
  143. .disable = pxa_dyn_tick_enable_disable,
  144. .reprogram = pxa_dyn_tick_reprogram,
  145. .handler = pxa_dyn_tick_handler,
  146. };
  147. #endif
  148. #ifdef CONFIG_PM
  149. static unsigned long osmr[4], oier;
  150. static void pxa_timer_suspend(void)
  151. {
  152. osmr[0] = OSMR0;
  153. osmr[1] = OSMR1;
  154. osmr[2] = OSMR2;
  155. osmr[3] = OSMR3;
  156. oier = OIER;
  157. }
  158. static void pxa_timer_resume(void)
  159. {
  160. OSMR0 = osmr[0];
  161. OSMR1 = osmr[1];
  162. OSMR2 = osmr[2];
  163. OSMR3 = osmr[3];
  164. OIER = oier;
  165. /*
  166. * OSMR0 is the system timer: make sure OSCR is sufficiently behind
  167. */
  168. OSCR = OSMR0 - LATCH;
  169. }
  170. #else
  171. #define pxa_timer_suspend NULL
  172. #define pxa_timer_resume NULL
  173. #endif
  174. struct sys_timer pxa_timer = {
  175. .init = pxa_timer_init,
  176. .suspend = pxa_timer_suspend,
  177. .resume = pxa_timer_resume,
  178. .offset = pxa_gettimeoffset,
  179. #ifdef CONFIG_NO_IDLE_HZ
  180. .dyn_tick = &pxa_dyn_tick,
  181. #endif
  182. };