ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / cris / kernel / time.c
1 /* $Id: time.c,v 1.9 2003/07/04 08:27:52 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
25 #include <asm/rtc.h>
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>
32
33 u64 jiffies_64 = INITIAL_JIFFIES;
34
35 EXPORT_SYMBOL(jiffies_64);
36
37 int have_rtc;  /* used to remember if we have an RTC or not */;
38
39 #define TICK_SIZE tick
40
41 extern unsigned long wall_jiffies;
42
43 extern unsigned long do_slow_gettimeoffset(void);
44 static unsigned long (*do_gettimeoffset)(void) = do_slow_gettimeoffset;
45
46 /*
47  * This version of gettimeofday has near microsecond resolution.
48  *
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).
52  */
53 void do_gettimeofday(struct timeval *tv)
54 {
55         unsigned long flags;
56         signed long usec, sec;
57         local_irq_save(flags);
58         local_irq_disable();
59         usec = do_gettimeoffset();
60         {
61                 unsigned long lost = jiffies - wall_jiffies;
62                 if (lost)
63                         usec += lost * (1000000 / HZ);
64         }
65         sec = xtime.tv_sec;
66         usec += xtime.tv_nsec / 1000;
67         local_irq_restore(flags);
68
69         while (usec >= 1000000) {
70                 usec -= 1000000;
71                 sec++;
72         }
73
74         tv->tv_sec = sec;
75         tv->tv_usec = usec;
76 }
77
78 EXPORT_SYMBOL(do_gettimeofday);
79
80 int do_settimeofday(struct timespec *tv)
81 {
82         unsigned long flags;
83
84         if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
85                 return -EINVAL;
86
87         local_irq_save(flags);
88         local_irq_disable();
89
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!
95          */
96         tv->tv_nsec -= do_gettimeoffset() * 1000;
97         tv->tv_nsec -= (jiffies - wall_jiffies) * TICK_NSEC;
98
99         while (tv->tv_nsec < 0) {
100                 tv->tv_nsec += NSEC_PER_SEC;
101                 tv->tv_sec--;
102         }
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);
111         clock_was_set();
112         return 0;
113 }
114
115 EXPORT_SYMBOL(do_settimeofday);
116
117
118 /*
119  * BUG: This routine does not handle hour overflow properly; it just
120  *      sets the minutes. Usually you'll only notice that after reboot!
121  */
122
123 int set_rtc_mmss(unsigned long nowtime)
124 {
125         int retval = 0;
126         int real_seconds, real_minutes, cmos_minutes;
127
128         printk("set_rtc_mmss(%lu)\n", nowtime);
129
130         if(!have_rtc)
131                 return 0;
132
133         cmos_minutes = CMOS_READ(RTC_MINUTES);
134         BCD_TO_BIN(cmos_minutes);
135
136         /*
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
141          */
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 */
146         real_minutes %= 60;
147
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);
153         } else {
154                 printk(KERN_WARNING
155                        "set_rtc_mmss: can't update from %d to %d\n",
156                        cmos_minutes, real_minutes);
157                 retval = -1;
158         }
159
160         return retval;
161 }
162
163 /* grab the time from the RTC chip */
164
165 unsigned long
166 get_cmos_time(void)
167 {
168         unsigned int year, mon, day, hour, min, sec;
169
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);
176
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);
179
180         BCD_TO_BIN(sec);
181         BCD_TO_BIN(min);
182         BCD_TO_BIN(hour);
183         BCD_TO_BIN(day);
184         BCD_TO_BIN(mon);
185         BCD_TO_BIN(year);
186
187         if ((year += 1900) < 1970)
188                 year += 100;
189
190         return mktime(year, mon, day, hour, min, sec);
191 }
192
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.
195  */
196
197 void
198 update_xtime_from_cmos(void)
199 {
200         if(have_rtc) {
201                 xtime.tv_sec = get_cmos_time();
202                 xtime.tv_nsec = 0;
203         }
204 }