time.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* time.c: FRV arch-specific time handling
  2. *
  3. * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from arch/m68k/kernel/time.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/config.h> /* CONFIG_HEARTBEAT */
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel.h>
  17. #include <linux/param.h>
  18. #include <linux/string.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/profile.h>
  21. #include <linux/irq.h>
  22. #include <linux/mm.h>
  23. #include <asm/io.h>
  24. #include <asm/timer-regs.h>
  25. #include <asm/mb-regs.h>
  26. #include <asm/mb86943a.h>
  27. #include <asm/irq-routing.h>
  28. #include <linux/timex.h>
  29. #define TICK_SIZE (tick_nsec / 1000)
  30. extern unsigned long wall_jiffies;
  31. unsigned long __nongprelbss __clkin_clock_speed_HZ;
  32. unsigned long __nongprelbss __ext_bus_clock_speed_HZ;
  33. unsigned long __nongprelbss __res_bus_clock_speed_HZ;
  34. unsigned long __nongprelbss __sdram_clock_speed_HZ;
  35. unsigned long __nongprelbss __core_bus_clock_speed_HZ;
  36. unsigned long __nongprelbss __core_clock_speed_HZ;
  37. unsigned long __nongprelbss __dsu_clock_speed_HZ;
  38. unsigned long __nongprelbss __serial_clock_speed_HZ;
  39. unsigned long __delay_loops_MHz;
  40. static irqreturn_t timer_interrupt(int irq, void *dummy, struct pt_regs *regs);
  41. static struct irqaction timer_irq = {
  42. timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer", NULL, NULL
  43. };
  44. static inline int set_rtc_mmss(unsigned long nowtime)
  45. {
  46. return -1;
  47. }
  48. /*
  49. * timer_interrupt() needs to keep up the real-time clock,
  50. * as well as call the "do_timer()" routine every clocktick
  51. */
  52. static irqreturn_t timer_interrupt(int irq, void *dummy, struct pt_regs * regs)
  53. {
  54. /* last time the cmos clock got updated */
  55. static long last_rtc_update = 0;
  56. /*
  57. * Here we are in the timer irq handler. We just have irqs locally
  58. * disabled but we don't know if the timer_bh is running on the other
  59. * CPU. We need to avoid to SMP race with it. NOTE: we don' t need
  60. * the irq version of write_lock because as just said we have irq
  61. * locally disabled. -arca
  62. */
  63. write_seqlock(&xtime_lock);
  64. do_timer(regs);
  65. update_process_times(user_mode(regs));
  66. profile_tick(CPU_PROFILING, regs);
  67. /*
  68. * If we have an externally synchronized Linux clock, then update
  69. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  70. * called as close as possible to 500 ms before the new second starts.
  71. */
  72. if (ntp_synced() &&
  73. xtime.tv_sec > last_rtc_update + 660 &&
  74. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  75. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2
  76. ) {
  77. if (set_rtc_mmss(xtime.tv_sec) == 0)
  78. last_rtc_update = xtime.tv_sec;
  79. else
  80. last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
  81. }
  82. #ifdef CONFIG_HEARTBEAT
  83. static unsigned short n;
  84. n++;
  85. __set_LEDS(n);
  86. #endif /* CONFIG_HEARTBEAT */
  87. write_sequnlock(&xtime_lock);
  88. return IRQ_HANDLED;
  89. }
  90. void time_divisor_init(void)
  91. {
  92. unsigned short base, pre, prediv;
  93. /* set the scheduling timer going */
  94. pre = 1;
  95. prediv = 4;
  96. base = __res_bus_clock_speed_HZ / pre / HZ / (1 << prediv);
  97. __set_TPRV(pre);
  98. __set_TxCKSL_DATA(0, prediv);
  99. __set_TCTR(TCTR_SC_CTR0 | TCTR_RL_RW_LH8 | TCTR_MODE_2);
  100. __set_TCSR_DATA(0, base & 0xff);
  101. __set_TCSR_DATA(0, base >> 8);
  102. }
  103. void time_init(void)
  104. {
  105. unsigned int year, mon, day, hour, min, sec;
  106. extern void arch_gettod(int *year, int *mon, int *day, int *hour, int *min, int *sec);
  107. /* FIX by dqg : Set to zero for platforms that don't have tod */
  108. /* without this time is undefined and can overflow time_t, causing */
  109. /* very stange errors */
  110. year = 1980;
  111. mon = day = 1;
  112. hour = min = sec = 0;
  113. arch_gettod (&year, &mon, &day, &hour, &min, &sec);
  114. if ((year += 1900) < 1970)
  115. year += 100;
  116. xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
  117. xtime.tv_nsec = 0;
  118. /* install scheduling interrupt handler */
  119. setup_irq(IRQ_CPU_TIMER0, &timer_irq);
  120. time_divisor_init();
  121. }
  122. /*
  123. * This version of gettimeofday has near microsecond resolution.
  124. */
  125. void do_gettimeofday(struct timeval *tv)
  126. {
  127. unsigned long seq;
  128. unsigned long usec, sec;
  129. unsigned long max_ntp_tick;
  130. do {
  131. unsigned long lost;
  132. seq = read_seqbegin(&xtime_lock);
  133. usec = 0;
  134. lost = jiffies - wall_jiffies;
  135. /*
  136. * If time_adjust is negative then NTP is slowing the clock
  137. * so make sure not to go into next possible interval.
  138. * Better to lose some accuracy than have time go backwards..
  139. */
  140. if (unlikely(time_adjust < 0)) {
  141. max_ntp_tick = (USEC_PER_SEC / HZ) - tickadj;
  142. usec = min(usec, max_ntp_tick);
  143. if (lost)
  144. usec += lost * max_ntp_tick;
  145. }
  146. else if (unlikely(lost))
  147. usec += lost * (USEC_PER_SEC / HZ);
  148. sec = xtime.tv_sec;
  149. usec += (xtime.tv_nsec / 1000);
  150. } while (read_seqretry(&xtime_lock, seq));
  151. while (usec >= 1000000) {
  152. usec -= 1000000;
  153. sec++;
  154. }
  155. tv->tv_sec = sec;
  156. tv->tv_usec = usec;
  157. }
  158. EXPORT_SYMBOL(do_gettimeofday);
  159. int do_settimeofday(struct timespec *tv)
  160. {
  161. time_t wtm_sec, sec = tv->tv_sec;
  162. long wtm_nsec, nsec = tv->tv_nsec;
  163. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  164. return -EINVAL;
  165. write_seqlock_irq(&xtime_lock);
  166. /*
  167. * This is revolting. We need to set "xtime" correctly. However, the
  168. * value in this location is the value at the most recent update of
  169. * wall time. Discover what correction gettimeofday() would have
  170. * made, and then undo it!
  171. */
  172. nsec -= 0 * NSEC_PER_USEC;
  173. nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
  174. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  175. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  176. set_normalized_timespec(&xtime, sec, nsec);
  177. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  178. ntp_clear();
  179. write_sequnlock_irq(&xtime_lock);
  180. clock_was_set();
  181. return 0;
  182. }
  183. EXPORT_SYMBOL(do_settimeofday);
  184. /*
  185. * Scheduler clock - returns current time in nanosec units.
  186. */
  187. unsigned long long sched_clock(void)
  188. {
  189. return jiffies_64 * (1000000000 / HZ);
  190. }