time.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * Since IRQs are disabled at this point, coherence between
  78. * lost_ticks(updated in do_timer()) and the match reg value is
  79. * ensured, hence we can use do_gettimeofday() from interrupt
  80. * handlers.
  81. *
  82. * HACK ALERT: it seems that the PXA timer regs aren't updated right
  83. * away in all cases when a write occurs. We therefore compare with
  84. * 8 instead of 0 in the while() condition below to avoid missing a
  85. * match if OSCR has already reached the next OSMR value.
  86. * Experience has shown that up to 6 ticks are needed to work around
  87. * this problem, but let's use 8 to be conservative. Note that this
  88. * affect things only when the timer IRQ has been delayed by nearly
  89. * exactly one tick period which should be a pretty rare event.
  90. */
  91. do {
  92. timer_tick(regs);
  93. OSSR = OSSR_M0; /* Clear match on timer 0 */
  94. next_match = (OSMR0 += LATCH);
  95. } while( (signed long)(next_match - OSCR) <= 8 );
  96. write_sequnlock(&xtime_lock);
  97. return IRQ_HANDLED;
  98. }
  99. static struct irqaction pxa_timer_irq = {
  100. .name = "PXA Timer Tick",
  101. .flags = SA_INTERRUPT | SA_TIMER,
  102. .handler = pxa_timer_interrupt,
  103. };
  104. static void __init pxa_timer_init(void)
  105. {
  106. struct timespec tv;
  107. set_rtc = pxa_set_rtc;
  108. tv.tv_nsec = 0;
  109. tv.tv_sec = pxa_get_rtc_time();
  110. do_settimeofday(&tv);
  111. OSMR0 = 0; /* set initial match at 0 */
  112. OSSR = 0xf; /* clear status on all timers */
  113. setup_irq(IRQ_OST0, &pxa_timer_irq);
  114. OIER |= OIER_E0; /* enable match on timer 0 to cause interrupts */
  115. OSCR = 0; /* initialize free-running timer, force first match */
  116. }
  117. #ifdef CONFIG_NO_IDLE_HZ
  118. static int pxa_dyn_tick_enable_disable(void)
  119. {
  120. /* nothing to do */
  121. return 0;
  122. }
  123. static void pxa_dyn_tick_reprogram(unsigned long ticks)
  124. {
  125. if (ticks > 1) {
  126. initial_match = OSMR0;
  127. OSMR0 = initial_match + ticks * LATCH;
  128. match_posponed = 1;
  129. }
  130. }
  131. static irqreturn_t
  132. pxa_dyn_tick_handler(int irq, void *dev_id, struct pt_regs *regs)
  133. {
  134. if (match_posponed) {
  135. match_posponed = 0;
  136. OSMR0 = initial_match;
  137. if ( (signed long)(initial_match - OSCR) <= 8 )
  138. return pxa_timer_interrupt(irq, dev_id, regs);
  139. }
  140. return IRQ_NONE;
  141. }
  142. static struct dyn_tick_timer pxa_dyn_tick = {
  143. .enable = pxa_dyn_tick_enable_disable,
  144. .disable = pxa_dyn_tick_enable_disable,
  145. .reprogram = pxa_dyn_tick_reprogram,
  146. .handler = pxa_dyn_tick_handler,
  147. };
  148. #endif
  149. #ifdef CONFIG_PM
  150. static unsigned long osmr[4], oier;
  151. static void pxa_timer_suspend(void)
  152. {
  153. osmr[0] = OSMR0;
  154. osmr[1] = OSMR1;
  155. osmr[2] = OSMR2;
  156. osmr[3] = OSMR3;
  157. oier = OIER;
  158. }
  159. static void pxa_timer_resume(void)
  160. {
  161. OSMR0 = osmr[0];
  162. OSMR1 = osmr[1];
  163. OSMR2 = osmr[2];
  164. OSMR3 = osmr[3];
  165. OIER = oier;
  166. /*
  167. * OSMR0 is the system timer: make sure OSCR is sufficiently behind
  168. */
  169. OSCR = OSMR0 - LATCH;
  170. }
  171. #else
  172. #define pxa_timer_suspend NULL
  173. #define pxa_timer_resume NULL
  174. #endif
  175. struct sys_timer pxa_timer = {
  176. .init = pxa_timer_init,
  177. .suspend = pxa_timer_suspend,
  178. .resume = pxa_timer_resume,
  179. .offset = pxa_gettimeoffset,
  180. #ifdef CONFIG_NO_IDLE_HZ
  181. .dyn_tick = &pxa_dyn_tick,
  182. #endif
  183. };