time.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * linux/arch/arm/mach-sa1100/time.c
  3. *
  4. * Copyright (C) 1998 Deborah Wallach.
  5. * Twiddles (C) 1999 Hugo Fiennes <hugo@empeg.com>
  6. *
  7. * 2000/03/29 (C) Nicolas Pitre <nico@cam.org>
  8. * Rewritten: big cleanup, much simpler, better HZ accuracy.
  9. *
  10. */
  11. #include <linux/init.h>
  12. #include <linux/errno.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/timex.h>
  15. #include <linux/signal.h>
  16. #include <asm/mach/time.h>
  17. #include <asm/hardware.h>
  18. #define RTC_DEF_DIVIDER (32768 - 1)
  19. #define RTC_DEF_TRIM 0
  20. static unsigned long __init sa1100_get_rtc_time(void)
  21. {
  22. /*
  23. * According to the manual we should be able to let RTTR be zero
  24. * and then a default diviser for a 32.768KHz clock is used.
  25. * Apparently this doesn't work, at least for my SA1110 rev 5.
  26. * If the clock divider is uninitialized then reset it to the
  27. * default value to get the 1Hz clock.
  28. */
  29. if (RTTR == 0) {
  30. RTTR = RTC_DEF_DIVIDER + (RTC_DEF_TRIM << 16);
  31. printk(KERN_WARNING "Warning: uninitialized Real Time Clock\n");
  32. /* The current RTC value probably doesn't make sense either */
  33. RCNR = 0;
  34. return 0;
  35. }
  36. return RCNR;
  37. }
  38. static int sa1100_set_rtc(void)
  39. {
  40. unsigned long current_time = xtime.tv_sec;
  41. if (RTSR & RTSR_ALE) {
  42. /* make sure not to forward the clock over an alarm */
  43. unsigned long alarm = RTAR;
  44. if (current_time >= alarm && alarm >= RCNR)
  45. return -ERESTARTSYS;
  46. }
  47. RCNR = current_time;
  48. return 0;
  49. }
  50. /* IRQs are disabled before entering here from do_gettimeofday() */
  51. static unsigned long sa1100_gettimeoffset (void)
  52. {
  53. unsigned long ticks_to_match, elapsed, usec;
  54. /* Get ticks before next timer match */
  55. ticks_to_match = OSMR0 - OSCR;
  56. /* We need elapsed ticks since last match */
  57. elapsed = LATCH - ticks_to_match;
  58. /* Now convert them to usec */
  59. usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
  60. return usec;
  61. }
  62. #ifdef CONFIG_NO_IDLE_HZ
  63. static unsigned long initial_match;
  64. static int match_posponed;
  65. #endif
  66. static irqreturn_t
  67. sa1100_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  68. {
  69. unsigned int next_match;
  70. write_seqlock(&xtime_lock);
  71. #ifdef CONFIG_NO_IDLE_HZ
  72. if (match_posponed) {
  73. match_posponed = 0;
  74. OSMR0 = initial_match;
  75. }
  76. #endif
  77. /*
  78. * Loop until we get ahead of the free running timer.
  79. * This ensures an exact clock tick count and time accuracy.
  80. * Since IRQs are disabled at this point, coherence between
  81. * lost_ticks(updated in do_timer()) and the match reg value is
  82. * ensured, hence we can use do_gettimeofday() from interrupt
  83. * handlers.
  84. */
  85. do {
  86. timer_tick(regs);
  87. OSSR = OSSR_M0; /* Clear match on timer 0 */
  88. next_match = (OSMR0 += LATCH);
  89. } while ((signed long)(next_match - OSCR) <= 0);
  90. write_sequnlock(&xtime_lock);
  91. return IRQ_HANDLED;
  92. }
  93. static struct irqaction sa1100_timer_irq = {
  94. .name = "SA11xx Timer Tick",
  95. .flags = SA_INTERRUPT | SA_TIMER,
  96. .handler = sa1100_timer_interrupt,
  97. };
  98. static void __init sa1100_timer_init(void)
  99. {
  100. struct timespec tv;
  101. set_rtc = sa1100_set_rtc;
  102. tv.tv_nsec = 0;
  103. tv.tv_sec = sa1100_get_rtc_time();
  104. do_settimeofday(&tv);
  105. OIER = 0; /* disable any timer interrupts */
  106. OSCR = LATCH*2; /* push OSCR out of the way */
  107. OSMR0 = LATCH; /* set initial match */
  108. OSSR = 0xf; /* clear status on all timers */
  109. setup_irq(IRQ_OST0, &sa1100_timer_irq);
  110. OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
  111. OSCR = 0; /* initialize free-running timer */
  112. }
  113. #ifdef CONFIG_NO_IDLE_HZ
  114. static int sa1100_dyn_tick_enable_disable(void)
  115. {
  116. /* nothing to do */
  117. return 0;
  118. }
  119. static void sa1100_dyn_tick_reprogram(unsigned long ticks)
  120. {
  121. if (ticks > 1) {
  122. initial_match = OSMR0;
  123. OSMR0 = initial_match + ticks * LATCH;
  124. match_posponed = 1;
  125. }
  126. }
  127. static irqreturn_t
  128. sa1100_dyn_tick_handler(int irq, void *dev_id, struct pt_regs *regs)
  129. {
  130. if (match_posponed) {
  131. match_posponed = 0;
  132. OSMR0 = initial_match;
  133. if ((signed long)(initial_match - OSCR) <= 0)
  134. return sa1100_timer_interrupt(irq, dev_id, regs);
  135. }
  136. return IRQ_NONE;
  137. }
  138. static struct dyn_tick_timer sa1100_dyn_tick = {
  139. .enable = sa1100_dyn_tick_enable_disable,
  140. .disable = sa1100_dyn_tick_enable_disable,
  141. .reprogram = sa1100_dyn_tick_reprogram,
  142. .handler = sa1100_dyn_tick_handler,
  143. };
  144. #endif
  145. #ifdef CONFIG_PM
  146. unsigned long osmr[4], oier;
  147. static void sa1100_timer_suspend(void)
  148. {
  149. osmr[0] = OSMR0;
  150. osmr[1] = OSMR1;
  151. osmr[2] = OSMR2;
  152. osmr[3] = OSMR3;
  153. oier = OIER;
  154. }
  155. static void sa1100_timer_resume(void)
  156. {
  157. OSSR = 0x0f;
  158. OSMR0 = osmr[0];
  159. OSMR1 = osmr[1];
  160. OSMR2 = osmr[2];
  161. OSMR3 = osmr[3];
  162. OIER = oier;
  163. /*
  164. * OSMR0 is the system timer: make sure OSCR is sufficiently behind
  165. */
  166. OSCR = OSMR0 - LATCH;
  167. }
  168. #else
  169. #define sa1100_timer_suspend NULL
  170. #define sa1100_timer_resume NULL
  171. #endif
  172. struct sys_timer sa1100_timer = {
  173. .init = sa1100_timer_init,
  174. .suspend = sa1100_timer_suspend,
  175. .resume = sa1100_timer_resume,
  176. .offset = sa1100_gettimeoffset,
  177. #ifdef CONFIG_NO_IDLE_HZ
  178. .dyn_tick = &sa1100_dyn_tick,
  179. #endif
  180. };