time.c 4.0 KB

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