time.c 4.8 KB

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