1 /* $Id: time.c,v 1.9 2003/07/04 08:27:52 starvik Exp $
3 * linux/arch/cris/kernel/time.c
5 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
6 * Copyright (C) 1999, 2000, 2001 Axis Communications AB
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
18 * Linux/CRIS specific code:
20 * Authors: Bjorn Wesen
26 #include <linux/errno.h>
27 #include <linux/module.h>
28 #include <linux/param.h>
29 #include <linux/jiffies.h>
30 #include <linux/bcd.h>
31 #include <linux/timex.h>
33 u64 jiffies_64 = INITIAL_JIFFIES;
35 EXPORT_SYMBOL(jiffies_64);
37 int have_rtc; /* used to remember if we have an RTC or not */;
39 #define TICK_SIZE tick
41 extern unsigned long wall_jiffies;
43 extern unsigned long do_slow_gettimeoffset(void);
44 static unsigned long (*do_gettimeoffset)(void) = do_slow_gettimeoffset;
47 * This version of gettimeofday has near microsecond resolution.
49 * Note: Division is quite slow on CRIS and do_gettimeofday is called
50 * rather often. Maybe we should do some kind of approximation here
51 * (a naive approximation would be to divide by 1024).
53 void do_gettimeofday(struct timeval *tv)
56 signed long usec, sec;
57 local_irq_save(flags);
59 usec = do_gettimeoffset();
61 unsigned long lost = jiffies - wall_jiffies;
63 usec += lost * (1000000 / HZ);
66 usec += xtime.tv_nsec / 1000;
67 local_irq_restore(flags);
69 while (usec >= 1000000) {
78 EXPORT_SYMBOL(do_gettimeofday);
80 int do_settimeofday(struct timespec *tv)
84 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
87 local_irq_save(flags);
90 /* This is revolting. We need to set the xtime.tv_usec
91 * correctly. However, the value in this location is
92 * is value at the last tick.
93 * Discover what correction gettimeofday
94 * would have done, and then undo it!
96 tv->tv_nsec -= do_gettimeoffset() * 1000;
97 tv->tv_nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
99 while (tv->tv_nsec < 0) {
100 tv->tv_nsec += NSEC_PER_SEC;
103 xtime.tv_sec = tv->tv_sec;
104 xtime.tv_nsec = tv->tv_nsec;
105 time_adjust = 0; /* stop active adjtime() */
106 time_status |= STA_UNSYNC;
107 time_state = TIME_ERROR; /* p. 24, (a) */
108 time_maxerror = NTP_PHASE_LIMIT;
109 time_esterror = NTP_PHASE_LIMIT;
110 local_irq_restore(flags);
115 EXPORT_SYMBOL(do_settimeofday);
119 * BUG: This routine does not handle hour overflow properly; it just
120 * sets the minutes. Usually you'll only notice that after reboot!
123 int set_rtc_mmss(unsigned long nowtime)
126 int real_seconds, real_minutes, cmos_minutes;
128 printk("set_rtc_mmss(%lu)\n", nowtime);
133 cmos_minutes = CMOS_READ(RTC_MINUTES);
134 BCD_TO_BIN(cmos_minutes);
137 * since we're only adjusting minutes and seconds,
138 * don't interfere with hour overflow. This avoids
139 * messing with unknown time zones but requires your
140 * RTC not to be off by more than 15 minutes
142 real_seconds = nowtime % 60;
143 real_minutes = nowtime / 60;
144 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
145 real_minutes += 30; /* correct for half hour time zone */
148 if (abs(real_minutes - cmos_minutes) < 30) {
149 BIN_TO_BCD(real_seconds);
150 BIN_TO_BCD(real_minutes);
151 CMOS_WRITE(real_seconds,RTC_SECONDS);
152 CMOS_WRITE(real_minutes,RTC_MINUTES);
155 "set_rtc_mmss: can't update from %d to %d\n",
156 cmos_minutes, real_minutes);
163 /* grab the time from the RTC chip */
168 unsigned int year, mon, day, hour, min, sec;
170 sec = CMOS_READ(RTC_SECONDS);
171 min = CMOS_READ(RTC_MINUTES);
172 hour = CMOS_READ(RTC_HOURS);
173 day = CMOS_READ(RTC_DAY_OF_MONTH);
174 mon = CMOS_READ(RTC_MONTH);
175 year = CMOS_READ(RTC_YEAR);
177 printk("rtc: sec 0x%x min 0x%x hour 0x%x day 0x%x mon 0x%x year 0x%x\n",
178 sec, min, hour, day, mon, year);
187 if ((year += 1900) < 1970)
190 return mktime(year, mon, day, hour, min, sec);
193 /* update xtime from the CMOS settings. used when /dev/rtc gets a SET_TIME.
194 * TODO: this doesn't reset the fancy NTP phase stuff as do_settimeofday does.
198 update_xtime_from_cmos(void)
201 xtime.tv_sec = get_cmos_time();