time.c 4.0 KB

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