time.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. static irqreturn_t
  61. pxa_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  62. {
  63. int next_match;
  64. write_seqlock(&xtime_lock);
  65. /* Loop until we get ahead of the free running timer.
  66. * This ensures an exact clock tick count and time accuracy.
  67. * IRQs are disabled inside the loop to ensure coherence between
  68. * lost_ticks (updated in do_timer()) and the match reg value, so we
  69. * can use do_gettimeofday() from interrupt handlers.
  70. *
  71. * HACK ALERT: it seems that the PXA timer regs aren't updated right
  72. * away in all cases when a write occurs. We therefore compare with
  73. * 8 instead of 0 in the while() condition below to avoid missing a
  74. * match if OSCR has already reached the next OSMR value.
  75. * Experience has shown that up to 6 ticks are needed to work around
  76. * this problem, but let's use 8 to be conservative. Note that this
  77. * affect things only when the timer IRQ has been delayed by nearly
  78. * exactly one tick period which should be a pretty rare event.
  79. */
  80. do {
  81. timer_tick(regs);
  82. OSSR = OSSR_M0; /* Clear match on timer 0 */
  83. next_match = (OSMR0 += LATCH);
  84. } while( (signed long)(next_match - OSCR) <= 8 );
  85. write_sequnlock(&xtime_lock);
  86. return IRQ_HANDLED;
  87. }
  88. static struct irqaction pxa_timer_irq = {
  89. .name = "PXA Timer Tick",
  90. .flags = SA_INTERRUPT | SA_TIMER,
  91. .handler = pxa_timer_interrupt,
  92. };
  93. static void __init pxa_timer_init(void)
  94. {
  95. struct timespec tv;
  96. set_rtc = pxa_set_rtc;
  97. tv.tv_nsec = 0;
  98. tv.tv_sec = pxa_get_rtc_time();
  99. do_settimeofday(&tv);
  100. OSMR0 = 0; /* set initial match at 0 */
  101. OSSR = 0xf; /* clear status on all timers */
  102. setup_irq(IRQ_OST0, &pxa_timer_irq);
  103. OIER |= OIER_E0; /* enable match on timer 0 to cause interrupts */
  104. OSCR = 0; /* initialize free-running timer, force first match */
  105. }
  106. #ifdef CONFIG_PM
  107. static unsigned long osmr[4], oier;
  108. static void pxa_timer_suspend(void)
  109. {
  110. osmr[0] = OSMR0;
  111. osmr[1] = OSMR1;
  112. osmr[2] = OSMR2;
  113. osmr[3] = OSMR3;
  114. oier = OIER;
  115. }
  116. static void pxa_timer_resume(void)
  117. {
  118. OSMR0 = osmr[0];
  119. OSMR1 = osmr[1];
  120. OSMR2 = osmr[2];
  121. OSMR3 = osmr[3];
  122. OIER = oier;
  123. /*
  124. * OSMR0 is the system timer: make sure OSCR is sufficiently behind
  125. */
  126. OSCR = OSMR0 - LATCH;
  127. }
  128. #else
  129. #define pxa_timer_suspend NULL
  130. #define pxa_timer_resume NULL
  131. #endif
  132. struct sys_timer pxa_timer = {
  133. .init = pxa_timer_init,
  134. .suspend = pxa_timer_suspend,
  135. .resume = pxa_timer_resume,
  136. .offset = pxa_gettimeoffset,
  137. };