time.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* $Id: time.c,v 1.18 2005/03/04 08:16:17 starvik Exp $
  2. *
  3. * linux/arch/cris/kernel/time.c
  4. *
  5. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  6. * Copyright (C) 1999, 2000, 2001 Axis Communications AB
  7. *
  8. * 1994-07-02 Alan Modra
  9. * fixed set_rtc_mmss, fixed time.year for >= 2000, new mktime
  10. * 1995-03-26 Markus Kuhn
  11. * fixed 500 ms bug at call to set_rtc_mmss, fixed DS12887
  12. * precision CMOS clock update
  13. * 1996-05-03 Ingo Molnar
  14. * fixed time warps in do_[slow|fast]_gettimeoffset()
  15. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  16. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  17. *
  18. * Linux/CRIS specific code:
  19. *
  20. * Authors: Bjorn Wesen
  21. * Johan Adolfsson
  22. *
  23. */
  24. #include <asm/rtc.h>
  25. #include <linux/errno.h>
  26. #include <linux/module.h>
  27. #include <linux/param.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/bcd.h>
  30. #include <linux/timex.h>
  31. #include <linux/init.h>
  32. #include <linux/profile.h>
  33. u64 jiffies_64 = INITIAL_JIFFIES;
  34. EXPORT_SYMBOL(jiffies_64);
  35. int have_rtc; /* used to remember if we have an RTC or not */;
  36. #define TICK_SIZE tick
  37. extern unsigned long wall_jiffies;
  38. extern unsigned long loops_per_jiffy; /* init/main.c */
  39. unsigned long loops_per_usec;
  40. extern unsigned long do_slow_gettimeoffset(void);
  41. static unsigned long (*do_gettimeoffset)(void) = do_slow_gettimeoffset;
  42. /*
  43. * This version of gettimeofday has near microsecond resolution.
  44. *
  45. * Note: Division is quite slow on CRIS and do_gettimeofday is called
  46. * rather often. Maybe we should do some kind of approximation here
  47. * (a naive approximation would be to divide by 1024).
  48. */
  49. void do_gettimeofday(struct timeval *tv)
  50. {
  51. unsigned long flags;
  52. signed long usec, sec;
  53. local_irq_save(flags);
  54. local_irq_disable();
  55. usec = do_gettimeoffset();
  56. {
  57. unsigned long lost = jiffies - wall_jiffies;
  58. if (lost)
  59. usec += lost * (1000000 / HZ);
  60. }
  61. /*
  62. * If time_adjust is negative then NTP is slowing the clock
  63. * so make sure not to go into next possible interval.
  64. * Better to lose some accuracy than have time go backwards..
  65. */
  66. if (unlikely(time_adjust < 0) && usec > tickadj)
  67. usec = tickadj;
  68. sec = xtime.tv_sec;
  69. usec += xtime.tv_nsec / 1000;
  70. local_irq_restore(flags);
  71. while (usec >= 1000000) {
  72. usec -= 1000000;
  73. sec++;
  74. }
  75. tv->tv_sec = sec;
  76. tv->tv_usec = usec;
  77. }
  78. EXPORT_SYMBOL(do_gettimeofday);
  79. int do_settimeofday(struct timespec *tv)
  80. {
  81. time_t wtm_sec, sec = tv->tv_sec;
  82. long wtm_nsec, nsec = tv->tv_nsec;
  83. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  84. return -EINVAL;
  85. write_seqlock_irq(&xtime_lock);
  86. /*
  87. * This is revolting. We need to set "xtime" correctly. However, the
  88. * value in this location is the value at the most recent update of
  89. * wall time. Discover what correction gettimeofday() would have
  90. * made, and then undo it!
  91. */
  92. nsec -= do_gettimeoffset() * NSEC_PER_USEC;
  93. nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
  94. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  95. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  96. set_normalized_timespec(&xtime, sec, nsec);
  97. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  98. ntp_clear();
  99. write_sequnlock_irq(&xtime_lock);
  100. clock_was_set();
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(do_settimeofday);
  104. /*
  105. * BUG: This routine does not handle hour overflow properly; it just
  106. * sets the minutes. Usually you'll only notice that after reboot!
  107. */
  108. int set_rtc_mmss(unsigned long nowtime)
  109. {
  110. int retval = 0;
  111. int real_seconds, real_minutes, cmos_minutes;
  112. printk(KERN_DEBUG "set_rtc_mmss(%lu)\n", nowtime);
  113. if(!have_rtc)
  114. return 0;
  115. cmos_minutes = CMOS_READ(RTC_MINUTES);
  116. BCD_TO_BIN(cmos_minutes);
  117. /*
  118. * since we're only adjusting minutes and seconds,
  119. * don't interfere with hour overflow. This avoids
  120. * messing with unknown time zones but requires your
  121. * RTC not to be off by more than 15 minutes
  122. */
  123. real_seconds = nowtime % 60;
  124. real_minutes = nowtime / 60;
  125. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  126. real_minutes += 30; /* correct for half hour time zone */
  127. real_minutes %= 60;
  128. if (abs(real_minutes - cmos_minutes) < 30) {
  129. BIN_TO_BCD(real_seconds);
  130. BIN_TO_BCD(real_minutes);
  131. CMOS_WRITE(real_seconds,RTC_SECONDS);
  132. CMOS_WRITE(real_minutes,RTC_MINUTES);
  133. } else {
  134. printk(KERN_WARNING
  135. "set_rtc_mmss: can't update from %d to %d\n",
  136. cmos_minutes, real_minutes);
  137. retval = -1;
  138. }
  139. return retval;
  140. }
  141. /* grab the time from the RTC chip */
  142. unsigned long
  143. get_cmos_time(void)
  144. {
  145. unsigned int year, mon, day, hour, min, sec;
  146. sec = CMOS_READ(RTC_SECONDS);
  147. min = CMOS_READ(RTC_MINUTES);
  148. hour = CMOS_READ(RTC_HOURS);
  149. day = CMOS_READ(RTC_DAY_OF_MONTH);
  150. mon = CMOS_READ(RTC_MONTH);
  151. year = CMOS_READ(RTC_YEAR);
  152. printk(KERN_DEBUG
  153. "rtc: sec 0x%x min 0x%x hour 0x%x day 0x%x mon 0x%x year 0x%x\n",
  154. sec, min, hour, day, mon, year);
  155. BCD_TO_BIN(sec);
  156. BCD_TO_BIN(min);
  157. BCD_TO_BIN(hour);
  158. BCD_TO_BIN(day);
  159. BCD_TO_BIN(mon);
  160. BCD_TO_BIN(year);
  161. if ((year += 1900) < 1970)
  162. year += 100;
  163. return mktime(year, mon, day, hour, min, sec);
  164. }
  165. /* update xtime from the CMOS settings. used when /dev/rtc gets a SET_TIME.
  166. * TODO: this doesn't reset the fancy NTP phase stuff as do_settimeofday does.
  167. */
  168. void
  169. update_xtime_from_cmos(void)
  170. {
  171. if(have_rtc) {
  172. xtime.tv_sec = get_cmos_time();
  173. xtime.tv_nsec = 0;
  174. }
  175. }
  176. extern void cris_profile_sample(struct pt_regs* regs);
  177. void
  178. cris_do_profile(struct pt_regs* regs)
  179. {
  180. #if CONFIG_SYSTEM_PROFILER
  181. cris_profile_sample(regs);
  182. #endif
  183. #if CONFIG_PROFILING
  184. profile_tick(CPU_PROFILING, regs);
  185. #endif
  186. }
  187. /*
  188. * Scheduler clock - returns current time in nanosec units.
  189. */
  190. unsigned long long sched_clock(void)
  191. {
  192. return (unsigned long long)jiffies * (1000000000 / HZ);
  193. }
  194. static int
  195. __init init_udelay(void)
  196. {
  197. loops_per_usec = (loops_per_jiffy * HZ) / 1000000;
  198. return 0;
  199. }
  200. __initcall(init_udelay);