time.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * linux/arch/arm26/kernel/time.c
  3. *
  4. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  5. * Modifications for ARM (C) 1994-2001 Russell King
  6. * Mods for ARM26 (C) 2003 Ian Molton
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This file contains the ARM-specific time handling details:
  13. * reading the RTC at bootup, etc...
  14. *
  15. * 1994-07-02 Alan Modra
  16. * fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
  17. * 1998-12-20 Updated NTP code according to technical memorandum Jan '96
  18. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/time.h>
  24. #include <linux/init.h>
  25. #include <linux/smp.h>
  26. #include <linux/timex.h>
  27. #include <linux/errno.h>
  28. #include <linux/profile.h>
  29. #include <asm/hardware.h>
  30. #include <asm/io.h>
  31. #include <asm/irq.h>
  32. #include <asm/ioc.h>
  33. extern unsigned long wall_jiffies;
  34. /* this needs a better home */
  35. DEFINE_SPINLOCK(rtc_lock);
  36. /* change this if you have some constant time drift */
  37. #define USECS_PER_JIFFY (1000000/HZ)
  38. static int dummy_set_rtc(void)
  39. {
  40. return 0;
  41. }
  42. /*
  43. * hook for setting the RTC's idea of the current time.
  44. */
  45. int (*set_rtc)(void) = dummy_set_rtc;
  46. /*
  47. * Get time offset based on IOCs timer.
  48. * FIXME - if this is called with interrutps off, why the shennanigans
  49. * below ?
  50. */
  51. static unsigned long gettimeoffset(void)
  52. {
  53. unsigned int count1, count2, status;
  54. long offset;
  55. ioc_writeb (0, IOC_T0LATCH);
  56. barrier ();
  57. count1 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
  58. barrier ();
  59. status = ioc_readb(IOC_IRQREQA);
  60. barrier ();
  61. ioc_writeb (0, IOC_T0LATCH);
  62. barrier ();
  63. count2 = ioc_readb(IOC_T0CNTL) | (ioc_readb(IOC_T0CNTH) << 8);
  64. offset = count2;
  65. if (count2 < count1) {
  66. /*
  67. * We have not had an interrupt between reading count1
  68. * and count2.
  69. */
  70. if (status & (1 << 5))
  71. offset -= LATCH;
  72. } else if (count2 > count1) {
  73. /*
  74. * We have just had another interrupt between reading
  75. * count1 and count2.
  76. */
  77. offset -= LATCH;
  78. }
  79. offset = (LATCH - offset) * (tick_nsec / 1000);
  80. return (offset + LATCH/2) / LATCH;
  81. }
  82. /*
  83. * Scheduler clock - returns current time in nanosec units.
  84. */
  85. unsigned long long sched_clock(void)
  86. {
  87. return (unsigned long long)jiffies * (1000000000 / HZ);
  88. }
  89. static unsigned long next_rtc_update;
  90. /*
  91. * If we have an externally synchronized linux clock, then update
  92. * CMOS clock accordingly every ~11 minutes. set_rtc() has to be
  93. * called as close as possible to 500 ms before the new second
  94. * starts.
  95. */
  96. static inline void do_set_rtc(void)
  97. {
  98. if (!ntp_synced() || set_rtc == NULL)
  99. return;
  100. //FIXME - timespec.tv_sec is a time_t not unsigned long
  101. if (next_rtc_update &&
  102. time_before((unsigned long)xtime.tv_sec, next_rtc_update))
  103. return;
  104. if (xtime.tv_nsec < 500000000 - ((unsigned) tick_nsec >> 1) &&
  105. xtime.tv_nsec >= 500000000 + ((unsigned) tick_nsec >> 1))
  106. return;
  107. if (set_rtc())
  108. /*
  109. * rtc update failed. Try again in 60s
  110. */
  111. next_rtc_update = xtime.tv_sec + 60;
  112. else
  113. next_rtc_update = xtime.tv_sec + 660;
  114. }
  115. #define do_leds()
  116. void do_gettimeofday(struct timeval *tv)
  117. {
  118. unsigned long flags;
  119. unsigned long seq;
  120. unsigned long usec, sec, lost;
  121. do {
  122. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  123. usec = gettimeoffset();
  124. lost = jiffies - wall_jiffies;
  125. if (lost)
  126. usec += lost * USECS_PER_JIFFY;
  127. sec = xtime.tv_sec;
  128. usec += xtime.tv_nsec / 1000;
  129. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  130. /* usec may have gone up a lot: be safe */
  131. while (usec >= 1000000) {
  132. usec -= 1000000;
  133. sec++;
  134. }
  135. tv->tv_sec = sec;
  136. tv->tv_usec = usec;
  137. }
  138. EXPORT_SYMBOL(do_gettimeofday);
  139. int do_settimeofday(struct timespec *tv)
  140. {
  141. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  142. return -EINVAL;
  143. write_seqlock_irq(&xtime_lock);
  144. /*
  145. * This is revolting. We need to set "xtime" correctly. However, the
  146. * value in this location is the value at the most recent update of
  147. * wall time. Discover what correction gettimeofday() would have
  148. * done, and then undo it!
  149. */
  150. tv->tv_nsec -= 1000 * (gettimeoffset() +
  151. (jiffies - wall_jiffies) * USECS_PER_JIFFY);
  152. while (tv->tv_nsec < 0) {
  153. tv->tv_nsec += NSEC_PER_SEC;
  154. tv->tv_sec--;
  155. }
  156. xtime.tv_sec = tv->tv_sec;
  157. xtime.tv_nsec = tv->tv_nsec;
  158. ntp_clear();
  159. write_sequnlock_irq(&xtime_lock);
  160. clock_was_set();
  161. return 0;
  162. }
  163. EXPORT_SYMBOL(do_settimeofday);
  164. static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  165. {
  166. do_timer(regs);
  167. #ifndef CONFIG_SMP
  168. update_process_times(user_mode(regs));
  169. #endif
  170. do_set_rtc(); //FIME - EVERY timer IRQ?
  171. profile_tick(CPU_PROFILING, regs);
  172. return IRQ_HANDLED; //FIXME - is this right?
  173. }
  174. static struct irqaction timer_irq = {
  175. .name = "timer",
  176. .flags = IRQF_DISABLED,
  177. .handler = timer_interrupt,
  178. };
  179. extern void ioctime_init(void);
  180. /*
  181. * Set up timer interrupt.
  182. */
  183. void __init time_init(void)
  184. {
  185. ioc_writeb(LATCH & 255, IOC_T0LTCHL);
  186. ioc_writeb(LATCH >> 8, IOC_T0LTCHH);
  187. ioc_writeb(0, IOC_T0GO);
  188. setup_irq(IRQ_TIMER, &timer_irq);
  189. }