1 |
199 |
simons |
/*
|
2 |
|
|
* linux/arch/alpha/kernel/time.c
|
3 |
|
|
*
|
4 |
|
|
* Copyright (C) 1991, 1992, 1995 Linus Torvalds
|
5 |
|
|
*
|
6 |
|
|
* This file contains the PC-specific time handling details:
|
7 |
|
|
* reading the RTC at bootup, etc..
|
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 |
|
|
*/
|
14 |
|
|
#include <linux/errno.h>
|
15 |
|
|
#include <linux/sched.h>
|
16 |
|
|
#include <linux/kernel.h>
|
17 |
|
|
#include <linux/param.h>
|
18 |
|
|
#include <linux/string.h>
|
19 |
|
|
#include <linux/mm.h>
|
20 |
|
|
|
21 |
|
|
#include <asm/segment.h>
|
22 |
|
|
#include <asm/io.h>
|
23 |
|
|
#include <asm/hwrpb.h>
|
24 |
|
|
|
25 |
|
|
#include <linux/mc146818rtc.h>
|
26 |
|
|
#include <linux/timex.h>
|
27 |
|
|
|
28 |
|
|
#define TIMER_IRQ 0
|
29 |
|
|
|
30 |
|
|
extern struct hwrpb_struct *hwrpb;
|
31 |
|
|
|
32 |
|
|
static int set_rtc_mmss(unsigned long);
|
33 |
|
|
|
34 |
|
|
|
35 |
|
|
/*
|
36 |
|
|
* Shift amount by which scaled_ticks_per_cycle is scaled. Shifting
|
37 |
|
|
* by 48 gives us 16 bits for HZ while keeping the accuracy good even
|
38 |
|
|
* for large CPU clock rates.
|
39 |
|
|
*/
|
40 |
|
|
#define FIX_SHIFT 48
|
41 |
|
|
|
42 |
|
|
/* lump static variables together for more efficient access: */
|
43 |
|
|
static struct {
|
44 |
|
|
__u32 last_time; /* cycle counter last time it got invoked */
|
45 |
|
|
__u32 max_cycles_per_tick; /* more makes us think we lost an interrupt */
|
46 |
|
|
unsigned long scaled_ticks_per_cycle; /* ticks/cycle * 2^48 */
|
47 |
|
|
long last_rtc_update; /* last time the cmos clock got updated */
|
48 |
|
|
} state;
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
static inline __u32 rpcc(void)
|
52 |
|
|
{
|
53 |
|
|
__u32 result;
|
54 |
|
|
|
55 |
|
|
asm volatile ("rpcc %0" : "r="(result));
|
56 |
|
|
return result;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
/*
|
61 |
|
|
* timer_interrupt() needs to keep up the real-time clock,
|
62 |
|
|
* as well as call the "do_timer()" routine every clocktick
|
63 |
|
|
*/
|
64 |
|
|
void timer_interrupt(struct pt_regs * regs)
|
65 |
|
|
{
|
66 |
|
|
__u32 delta, now;
|
67 |
|
|
|
68 |
|
|
now = rpcc();
|
69 |
|
|
delta = now - state.last_time;
|
70 |
|
|
state.last_time = now;
|
71 |
|
|
if (delta > state.max_cycles_per_tick) {
|
72 |
|
|
int i, missed_ticks;
|
73 |
|
|
|
74 |
|
|
missed_ticks = ((delta * state.scaled_ticks_per_cycle) >> FIX_SHIFT) - 1;
|
75 |
|
|
for (i = 0; i < missed_ticks; ++i) {
|
76 |
|
|
do_timer(regs);
|
77 |
|
|
}
|
78 |
|
|
}
|
79 |
|
|
do_timer(regs);
|
80 |
|
|
|
81 |
|
|
/*
|
82 |
|
|
* If we have an externally synchronized Linux clock, then update
|
83 |
|
|
* CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
|
84 |
|
|
* called as close as possible to 500 ms before the new second starts.
|
85 |
|
|
*/
|
86 |
|
|
if (time_state != TIME_BAD && xtime.tv_sec > state.last_rtc_update + 660 &&
|
87 |
|
|
xtime.tv_usec > 500000 - (tick >> 1) &&
|
88 |
|
|
xtime.tv_usec < 500000 + (tick >> 1))
|
89 |
|
|
if (set_rtc_mmss(xtime.tv_sec) == 0)
|
90 |
|
|
state.last_rtc_update = xtime.tv_sec;
|
91 |
|
|
else
|
92 |
|
|
state.last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
|
96 |
|
|
* Assumes input in normal date format, i.e. 1980-12-31 23:59:59
|
97 |
|
|
* => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
|
98 |
|
|
*
|
99 |
|
|
* [For the Julian calendar (which was used in Russia before 1917,
|
100 |
|
|
* Britain & colonies before 1752, anywhere else before 1582,
|
101 |
|
|
* and is still in use by some communities) leave out the
|
102 |
|
|
* -year/100+year/400 terms, and add 10.]
|
103 |
|
|
*
|
104 |
|
|
* This algorithm was first published by Gauss (I think).
|
105 |
|
|
*
|
106 |
|
|
* WARNING: this function will overflow on 2106-02-07 06:28:16 on
|
107 |
|
|
* machines were long is 32-bit! (However, as time_t is signed, we
|
108 |
|
|
* will already get problems at other places on 2038-01-19 03:14:08)
|
109 |
|
|
*/
|
110 |
|
|
static inline unsigned long mktime(unsigned int year, unsigned int mon,
|
111 |
|
|
unsigned int day, unsigned int hour,
|
112 |
|
|
unsigned int min, unsigned int sec)
|
113 |
|
|
{
|
114 |
|
|
if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */
|
115 |
|
|
mon += 12; /* Puts Feb last since it has leap day */
|
116 |
|
|
year -= 1;
|
117 |
|
|
}
|
118 |
|
|
return (((
|
119 |
|
|
(unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day) +
|
120 |
|
|
year*365 - 719499
|
121 |
|
|
)*24 + hour /* now have hours */
|
122 |
|
|
)*60 + min /* now have minutes */
|
123 |
|
|
)*60 + sec; /* finally seconds */
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
void time_init(void)
|
127 |
|
|
{
|
128 |
|
|
unsigned int year, mon, day, hour, min, sec;
|
129 |
|
|
int i;
|
130 |
|
|
|
131 |
|
|
/* The Linux interpretation of the CMOS clock register contents:
|
132 |
|
|
* When the Update-In-Progress (UIP) flag goes from 1 to 0, the
|
133 |
|
|
* RTC registers show the second which has precisely just started.
|
134 |
|
|
* Let's hope other operating systems interpret the RTC the same way.
|
135 |
|
|
*/
|
136 |
|
|
/* read RTC exactly on falling edge of update flag */
|
137 |
|
|
for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
|
138 |
|
|
if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
|
139 |
|
|
break;
|
140 |
|
|
for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
|
141 |
|
|
if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
|
142 |
|
|
break;
|
143 |
|
|
do { /* Isn't this overkill ? UIP above should guarantee consistency */
|
144 |
|
|
sec = CMOS_READ(RTC_SECONDS);
|
145 |
|
|
min = CMOS_READ(RTC_MINUTES);
|
146 |
|
|
hour = CMOS_READ(RTC_HOURS);
|
147 |
|
|
day = CMOS_READ(RTC_DAY_OF_MONTH);
|
148 |
|
|
mon = CMOS_READ(RTC_MONTH);
|
149 |
|
|
year = CMOS_READ(RTC_YEAR);
|
150 |
|
|
} while (sec != CMOS_READ(RTC_SECONDS));
|
151 |
|
|
if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
|
152 |
|
|
{
|
153 |
|
|
BCD_TO_BIN(sec);
|
154 |
|
|
BCD_TO_BIN(min);
|
155 |
|
|
BCD_TO_BIN(hour);
|
156 |
|
|
BCD_TO_BIN(day);
|
157 |
|
|
BCD_TO_BIN(mon);
|
158 |
|
|
BCD_TO_BIN(year);
|
159 |
|
|
}
|
160 |
|
|
#ifdef ALPHA_PRE_V1_2_SRM_CONSOLE
|
161 |
|
|
/*
|
162 |
|
|
* The meaning of life, the universe, and everything. Plus
|
163 |
|
|
* this makes the year come out right on SRM consoles earlier
|
164 |
|
|
* than v1.2.
|
165 |
|
|
*/
|
166 |
|
|
year -= 42;
|
167 |
|
|
#endif
|
168 |
|
|
if ((year += 1900) < 1970)
|
169 |
|
|
year += 100;
|
170 |
|
|
xtime.tv_sec = mktime(year, mon, day, hour, min, sec);
|
171 |
|
|
xtime.tv_usec = 0;
|
172 |
|
|
|
173 |
|
|
if (HZ > (1<<16)) {
|
174 |
|
|
extern void __you_loose (void);
|
175 |
|
|
__you_loose();
|
176 |
|
|
}
|
177 |
|
|
state.last_time = rpcc();
|
178 |
|
|
state.scaled_ticks_per_cycle = ((unsigned long) HZ << FIX_SHIFT) / hwrpb->cycle_freq;
|
179 |
|
|
state.max_cycles_per_tick = (2 * hwrpb->cycle_freq) / HZ;
|
180 |
|
|
state.last_rtc_update = 0;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
/*
|
184 |
|
|
* We could get better timer accuracy by using the alpha
|
185 |
|
|
* time counters or something. Now this is limited to
|
186 |
|
|
* the HZ clock frequency.
|
187 |
|
|
*/
|
188 |
|
|
void do_gettimeofday(struct timeval *tv)
|
189 |
|
|
{
|
190 |
|
|
unsigned long flags;
|
191 |
|
|
|
192 |
|
|
save_flags(flags);
|
193 |
|
|
cli();
|
194 |
|
|
*tv = xtime;
|
195 |
|
|
restore_flags(flags);
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
void do_settimeofday(struct timeval *tv)
|
199 |
|
|
{
|
200 |
|
|
cli();
|
201 |
|
|
xtime = *tv;
|
202 |
|
|
time_state = TIME_BAD;
|
203 |
|
|
time_maxerror = 0x70000000;
|
204 |
|
|
time_esterror = 0x70000000;
|
205 |
|
|
sti();
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
/*
|
210 |
|
|
* In order to set the CMOS clock precisely, set_rtc_mmss has to be
|
211 |
|
|
* called 500 ms after the second nowtime has started, because when
|
212 |
|
|
* nowtime is written into the registers of the CMOS clock, it will
|
213 |
|
|
* jump to the next second precisely 500 ms later. Check the Motorola
|
214 |
|
|
* MC146818A or Dallas DS12887 data sheet for details.
|
215 |
|
|
*/
|
216 |
|
|
static int set_rtc_mmss(unsigned long nowtime)
|
217 |
|
|
{
|
218 |
|
|
int retval = 0;
|
219 |
|
|
int real_seconds, real_minutes, cmos_minutes;
|
220 |
|
|
unsigned char save_control, save_freq_select;
|
221 |
|
|
|
222 |
|
|
save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
|
223 |
|
|
CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
|
224 |
|
|
|
225 |
|
|
save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
|
226 |
|
|
CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
|
227 |
|
|
|
228 |
|
|
cmos_minutes = CMOS_READ(RTC_MINUTES);
|
229 |
|
|
if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
|
230 |
|
|
BCD_TO_BIN(cmos_minutes);
|
231 |
|
|
|
232 |
|
|
/*
|
233 |
|
|
* since we're only adjusting minutes and seconds,
|
234 |
|
|
* don't interfere with hour overflow. This avoids
|
235 |
|
|
* messing with unknown time zones but requires your
|
236 |
|
|
* RTC not to be off by more than 15 minutes
|
237 |
|
|
*/
|
238 |
|
|
real_seconds = nowtime % 60;
|
239 |
|
|
real_minutes = nowtime / 60;
|
240 |
|
|
if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
|
241 |
|
|
real_minutes += 30; /* correct for half hour time zone */
|
242 |
|
|
real_minutes %= 60;
|
243 |
|
|
|
244 |
|
|
if (abs(real_minutes - cmos_minutes) < 30) {
|
245 |
|
|
if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
|
246 |
|
|
BIN_TO_BCD(real_seconds);
|
247 |
|
|
BIN_TO_BCD(real_minutes);
|
248 |
|
|
}
|
249 |
|
|
CMOS_WRITE(real_seconds,RTC_SECONDS);
|
250 |
|
|
CMOS_WRITE(real_minutes,RTC_MINUTES);
|
251 |
|
|
} else
|
252 |
|
|
retval = -1;
|
253 |
|
|
|
254 |
|
|
/* The following flags have to be released exactly in this order,
|
255 |
|
|
* otherwise the DS12887 (popular MC146818A clone with integrated
|
256 |
|
|
* battery and quartz) will not reset the oscillator and will not
|
257 |
|
|
* update precisely 500 ms later. You won't find this mentioned in
|
258 |
|
|
* the Dallas Semiconductor data sheets, but who believes data
|
259 |
|
|
* sheets anyway ... -- Markus Kuhn
|
260 |
|
|
*/
|
261 |
|
|
CMOS_WRITE(save_control, RTC_CONTROL);
|
262 |
|
|
CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
|
263 |
|
|
|
264 |
|
|
return retval;
|
265 |
|
|
}
|