time.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 int sa1100_set_rtc(void)
  22. {
  23. unsigned long current_time = xtime.tv_sec;
  24. if (RTSR & RTSR_ALE) {
  25. /* make sure not to forward the clock over an alarm */
  26. unsigned long alarm = RTAR;
  27. if (current_time >= alarm && alarm >= RCNR)
  28. return -ERESTARTSYS;
  29. }
  30. RCNR = current_time;
  31. return 0;
  32. }
  33. /* IRQs are disabled before entering here from do_gettimeofday() */
  34. static unsigned long sa1100_gettimeoffset (void)
  35. {
  36. unsigned long ticks_to_match, elapsed, usec;
  37. /* Get ticks before next timer match */
  38. ticks_to_match = OSMR0 - OSCR;
  39. /* We need elapsed ticks since last match */
  40. elapsed = LATCH - ticks_to_match;
  41. /* Now convert them to usec */
  42. usec = (unsigned long)(elapsed * (tick_nsec / 1000))/LATCH;
  43. return usec;
  44. }
  45. #ifdef CONFIG_NO_IDLE_HZ
  46. static unsigned long initial_match;
  47. static int match_posponed;
  48. #endif
  49. static irqreturn_t
  50. sa1100_timer_interrupt(int irq, void *dev_id)
  51. {
  52. unsigned int next_match;
  53. write_seqlock(&xtime_lock);
  54. #ifdef CONFIG_NO_IDLE_HZ
  55. if (match_posponed) {
  56. match_posponed = 0;
  57. OSMR0 = initial_match;
  58. }
  59. #endif
  60. /*
  61. * Loop until we get ahead of the free running timer.
  62. * This ensures an exact clock tick count and time accuracy.
  63. * Since IRQs are disabled at this point, coherence between
  64. * lost_ticks(updated in do_timer()) and the match reg value is
  65. * ensured, hence we can use do_gettimeofday() from interrupt
  66. * handlers.
  67. */
  68. do {
  69. timer_tick();
  70. OSSR = OSSR_M0; /* Clear match on timer 0 */
  71. next_match = (OSMR0 += LATCH);
  72. } while ((signed long)(next_match - OSCR) <= 0);
  73. write_sequnlock(&xtime_lock);
  74. return IRQ_HANDLED;
  75. }
  76. static struct irqaction sa1100_timer_irq = {
  77. .name = "SA11xx Timer Tick",
  78. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  79. .handler = sa1100_timer_interrupt,
  80. };
  81. static void __init sa1100_timer_init(void)
  82. {
  83. unsigned long flags;
  84. set_rtc = sa1100_set_rtc;
  85. OIER = 0; /* disable any timer interrupts */
  86. OSSR = 0xf; /* clear status on all timers */
  87. setup_irq(IRQ_OST0, &sa1100_timer_irq);
  88. local_irq_save(flags);
  89. OIER = OIER_E0; /* enable match on timer 0 to cause interrupts */
  90. OSMR0 = OSCR + LATCH; /* set initial match */
  91. local_irq_restore(flags);
  92. }
  93. #ifdef CONFIG_NO_IDLE_HZ
  94. static int sa1100_dyn_tick_enable_disable(void)
  95. {
  96. /* nothing to do */
  97. return 0;
  98. }
  99. static void sa1100_dyn_tick_reprogram(unsigned long ticks)
  100. {
  101. if (ticks > 1) {
  102. initial_match = OSMR0;
  103. OSMR0 = initial_match + ticks * LATCH;
  104. match_posponed = 1;
  105. }
  106. }
  107. static irqreturn_t
  108. sa1100_dyn_tick_handler(int irq, void *dev_id)
  109. {
  110. if (match_posponed) {
  111. match_posponed = 0;
  112. OSMR0 = initial_match;
  113. if ((signed long)(initial_match - OSCR) <= 0)
  114. return sa1100_timer_interrupt(irq, dev_id);
  115. }
  116. return IRQ_NONE;
  117. }
  118. static struct dyn_tick_timer sa1100_dyn_tick = {
  119. .enable = sa1100_dyn_tick_enable_disable,
  120. .disable = sa1100_dyn_tick_enable_disable,
  121. .reprogram = sa1100_dyn_tick_reprogram,
  122. .handler = sa1100_dyn_tick_handler,
  123. };
  124. #endif
  125. #ifdef CONFIG_PM
  126. unsigned long osmr[4], oier;
  127. static void sa1100_timer_suspend(void)
  128. {
  129. osmr[0] = OSMR0;
  130. osmr[1] = OSMR1;
  131. osmr[2] = OSMR2;
  132. osmr[3] = OSMR3;
  133. oier = OIER;
  134. }
  135. static void sa1100_timer_resume(void)
  136. {
  137. OSSR = 0x0f;
  138. OSMR0 = osmr[0];
  139. OSMR1 = osmr[1];
  140. OSMR2 = osmr[2];
  141. OSMR3 = osmr[3];
  142. OIER = oier;
  143. /*
  144. * OSMR0 is the system timer: make sure OSCR is sufficiently behind
  145. */
  146. OSCR = OSMR0 - LATCH;
  147. }
  148. #else
  149. #define sa1100_timer_suspend NULL
  150. #define sa1100_timer_resume NULL
  151. #endif
  152. struct sys_timer sa1100_timer = {
  153. .init = sa1100_timer_init,
  154. .suspend = sa1100_timer_suspend,
  155. .resume = sa1100_timer_resume,
  156. .offset = sa1100_gettimeoffset,
  157. #ifdef CONFIG_NO_IDLE_HZ
  158. .dyn_tick = &sa1100_dyn_tick,
  159. #endif
  160. };