time.c 5.4 KB

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