1 /* 2 * linux/arch/i386/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 #include <linux/interrupt.h>
21
22 #include <asm/segment.h>
23 #include <asm/io.h>
24 #include <asm/irq.h>
25
26 #include <linux/mc146818rtc.h>
27 #include <linux/timex.h>
28 #include <linux/config.h>
29
30 externintsetup_x86_irq(int, structirqaction *);
31
32 #ifndefCONFIG_APM/* cycle counter may be unreliable */ 33 /* Cycle counter value at the previous timer interrupt.. */ 34 staticunsignedlonglonglast_timer_cc = 0;
35 staticunsignedlonglonginit_timer_cc = 0;
36
37 staticunsignedlongdo_fast_gettimeoffset(void)
/* */ 38 { 39 unsignedlongtime_low, time_high;
40 unsignedlongquotient, remainder;
41
42 /* Get last timer tick in absolute kernel time */ 43 __asm__("subl %2,%0\n\t"
44 "sbbl %3,%1"
45 :"=r" (time_low), "=r" (time_high)
46 :"m" (*(0+(long *)&init_timer_cc)),
47 "m" (*(1+(long *)&init_timer_cc)),
48 "0" (*(0+(long *)&last_timer_cc)),
49 "1" (*(1+(long *)&last_timer_cc)));
50 /* 51 * Divide the 64-bit time with the 32-bit jiffy counter, 52 * getting the quotient in clocks. 53 * 54 * Giving quotient = "average internal clocks per jiffy" 55 */ 56 __asm__("divl %2"
57 :"=a" (quotient), "=d" (remainder)
58 :"r" (jiffies),
59 "0" (time_low), "1" (time_high));
60
61 /* Read the time counter */ 62 __asm__(".byte 0x0f,0x31"
63 :"=a" (time_low), "=d" (time_high));
64
65 /* .. relative to previous jiffy (32 bits is enough) */ 66 time_low -= (unsignedlong) last_timer_cc;
67
68 /* 69 * Time offset = (1000000/HZ * remainder) / quotient. 70 */ 71 __asm__("mull %1\n\t"
72 "divl %2"
73 :"=a" (quotient), "=d" (remainder)
74 :"r" (quotient),
75 "0" (time_low), "1" (1000000/HZ));
76
77 /* 78 * Due to rounding errors (and jiffies inconsistencies), 79 * we need to check the result so that we'll get a timer 80 * that is monotonous. 81 */ 82 if (quotient >= 1000000/HZ)
83 quotient = 1000000/HZ-1;
84 returnquotient;
85 } 86 #endif 87
88 /* This function must be called with interrupts disabled 89 * It was inspired by Steve McCanne's microtime-i386 for BSD. -- jrs 90 * 91 * However, the pc-audio speaker driver changes the divisor so that 92 * it gets interrupted rather more often - it loads 64 into the 93 * counter rather than 11932! This has an adverse impact on 94 * do_gettimeoffset() -- it stops working! What is also not 95 * good is that the interval that our timer function gets called 96 * is no longer 10.0002 ms, but 9.9767 ms. To get around this 97 * would require using a different timing source. Maybe someone 98 * could use the RTC - I know that this can interrupt at frequencies 99 * ranging from 8192Hz to 2Hz. If I had the energy, I'd somehow fix 100 * it so that at startup, the timer code in sched.c would select 101 * using either the RTC or the 8253 timer. The decision would be 102 * based on whether there was any other device around that needed 103 * to trample on the 8253. I'd set up the RTC to interrupt at 1024 Hz, 104 * and then do some jiggery to have a version of do_timer that 105 * advanced the clock by 1/1024 s. Every time that reached over 1/100 106 * of a second, then do all the old code. If the time was kept correct 107 * then do_gettimeoffset could just return 0 - there is no low order 108 * divider that can be accessed. 109 * 110 * Ideally, you would be able to use the RTC for the speaker driver, 111 * but it appears that the speaker driver really needs interrupt more 112 * often than every 120 us or so. 113 * 114 * Anyway, this needs more thought.... pjsg (1993-08-28) 115 * 116 * If you are really that interested, you should be reading 117 * comp.protocols.time.ntp! 118 */ 119
120 #defineTICK_SIZEtick 121
122 staticunsignedlongdo_slow_gettimeoffset(void)
/* */ 123 { 124 intcount;
125 unsignedlongoffset = 0;
126
127 /* timer count may underflow right here */ 128 outb_p(0x00, 0x43); /* latch the count ASAP */ 129 count = inb_p(0x40); /* read the latched count */ 130 count |= inb(0x40) << 8;
131 /* we know probability of underflow is always MUCH less than 1% */ 132 if (count > (LATCH - LATCH/100)) { 133 /* check for pending timer interrupt */ 134 outb_p(0x0a, 0x20);
135 if (inb(0x20) & 1)
136 offset = TICK_SIZE;
137 } 138 count = ((LATCH-1) - count) * TICK_SIZE;
139 count = (count + LATCH/2) / LATCH;
140 returnoffset + count;
141 } 142
143 staticunsignedlong (*do_gettimeoffset)(void) = do_slow_gettimeoffset;
144
145 /* 146 * This version of gettimeofday has near microsecond resolution. 147 */ 148 voiddo_gettimeofday(structtimeval *tv)
/* */ 149 { 150 unsignedlongflags;
151
152 save_flags(flags);
153 cli();
154 *tv = xtime;
155 tv->tv_usec += do_gettimeoffset();
156 if (tv->tv_usec >= 1000000) { 157 tv->tv_usec -= 1000000;
158 tv->tv_sec++;
159 } 160 restore_flags(flags);
161 } 162
163 voiddo_settimeofday(structtimeval *tv)
/* */ 164 { 165 cli();
166 /* This is revolting. We need to set the xtime.tv_usec 167 * correctly. However, the value in this location is 168 * is value at the last tick. 169 * Discover what correction gettimeofday 170 * would have done, and then undo it! 171 */ 172 tv->tv_usec -= do_gettimeoffset();
173
174 if (tv->tv_usec < 0) { 175 tv->tv_usec += 1000000;
176 tv->tv_sec--;
177 } 178
179 xtime = *tv;
180 time_state = TIME_BAD;
181 time_maxerror = MAXPHASE;
182 time_esterror = MAXPHASE;
183 sti();
184 } 185
186
187 /* 188 * In order to set the CMOS clock precisely, set_rtc_mmss has to be 189 * called 500 ms after the second nowtime has started, because when 190 * nowtime is written into the registers of the CMOS clock, it will 191 * jump to the next second precisely 500 ms later. Check the Motorola 192 * MC146818A or Dallas DS12887 data sheet for details. 193 */ 194 staticintset_rtc_mmss(unsignedlongnowtime)
/* */ 195 { 196 intretval = 0;
197 intreal_seconds, real_minutes, cmos_minutes;
198 unsignedcharsave_control, save_freq_select;
199
200 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */ 201 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
202
203 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */ 204 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
205
206 cmos_minutes = CMOS_READ(RTC_MINUTES);
207 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
208 BCD_TO_BIN(cmos_minutes);
209
210 /* 211 * since we're only adjusting minutes and seconds, 212 * don't interfere with hour overflow. This avoids 213 * messing with unknown time zones but requires your 214 * RTC not to be off by more than 15 minutes 215 */ 216 real_seconds = nowtime % 60;
217 real_minutes = nowtime / 60;
218 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
219 real_minutes += 30; /* correct for half hour time zone */ 220 real_minutes %= 60;
221
222 if (abs(real_minutes - cmos_minutes) < 30) { 223 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { 224 BIN_TO_BCD(real_seconds);
225 BIN_TO_BCD(real_minutes);
226 } 227 CMOS_WRITE(real_seconds,RTC_SECONDS);
228 CMOS_WRITE(real_minutes,RTC_MINUTES);
229 }else 230 retval = -1;
231
232 /* The following flags have to be released exactly in this order, 233 * otherwise the DS12887 (popular MC146818A clone with integrated 234 * battery and quartz) will not reset the oscillator and will not 235 * update precisely 500 ms later. You won't find this mentioned in 236 * the Dallas Semiconductor data sheets, but who believes data 237 * sheets anyway ... -- Markus Kuhn 238 */ 239 CMOS_WRITE(save_control, RTC_CONTROL);
240 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
241
242 returnretval;
243 } 244
245 /* last time the cmos clock got updated */ 246 staticlonglast_rtc_update = 0;
247
248 /* 249 * timer_interrupt() needs to keep up the real-time clock, 250 * as well as call the "do_timer()" routine every clocktick 251 */ 252 staticinlinevoidtimer_interrupt(intirq, void *dev_id, structpt_regs *regs)
/* */ 253 { 254 do_timer(regs);
255
256 /* 257 * If we have an externally synchronized Linux clock, then update 258 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be 259 * called as close as possible to 500 ms before the new second starts. 260 */ 261 if (time_state != TIME_BAD && xtime.tv_sec > last_rtc_update + 660 &&
262 xtime.tv_usec > 500000 - (tick >> 1) &&
263 xtime.tv_usec < 500000 + (tick >> 1))
264 if (set_rtc_mmss(xtime.tv_sec) == 0)
265 last_rtc_update = xtime.tv_sec;
266 else 267 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */ 268 /* As we return to user mode fire off the other CPU schedulers.. this is 269 basically because we don't yet share IRQ's around. This message is 270 rigged to be safe on the 386 - basically it's a hack, so don't look 271 closely for now.. */ 272 /*smp_message_pass(MSG_ALL_BUT_SELF, MSG_RESCHEDULE, 0L, 0); */ 273
274 } 275
276 #ifndefCONFIG_APM/* cycle counter may be unreliable */ 277 /* 278 * This is the same as the above, except we _also_ save the current 279 * cycle counter value at the time of the timer interrupt, so that 280 * we later on can estimate the time of day more exactly. 281 */ 282 staticvoidpentium_timer_interrupt(intirq, void *dev_id, structpt_regs *regs)
/* */ 283 { 284 /* read Pentium cycle counter */ 285 __asm__(".byte 0x0f,0x31"
286 :"=a" (((unsignedlong *) &last_timer_cc)[0]),
287 "=d" (((unsignedlong *) &last_timer_cc)[1]));
288 timer_interrupt(irq, NULL, regs);
289 } 290 #endif 291
292 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00. 293 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 294 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. 295 * 296 * [For the Julian calendar (which was used in Russia before 1917, 297 * Britain & colonies before 1752, anywhere else before 1582, 298 * and is still in use by some communities) leave out the 299 * -year/100+year/400 terms, and add 10.] 300 * 301 * This algorithm was first published by Gauss (I think). 302 * 303 * WARNING: this function will overflow on 2106-02-07 06:28:16 on 304 * machines were long is 32-bit! (However, as time_t is signed, we 305 * will already get problems at other places on 2038-01-19 03:14:08) 306 */ 307 staticinlineunsignedlongmktime(unsignedintyear, unsignedintmon,
/* */ 308 unsignedintday, unsignedinthour,
309 unsignedintmin, unsignedintsec)
310 { 311 if (0 >= (int) (mon -= 2)) {/* 1..12 -> 11,12,1..10 */ 312 mon += 12; /* Puts Feb last since it has leap day */ 313 year -= 1;
314 } 315 return (((
316 (unsignedlong)(year/4 - year/100 + year/400 + 367*mon/12 + day) +
317 year*365 - 719499
318 )*24 + hour/* now have hours */ 319 )*60 + min/* now have minutes */ 320 )*60 + sec; /* finally seconds */ 321 } 322
323 unsignedlongget_cmos_time(void)
/* */ 324 { 325 unsignedintyear, mon, day, hour, min, sec;
326 inti;
327
328 /* The Linux interpretation of the CMOS clock register contents: 329 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the 330 * RTC registers show the second which has precisely just started. 331 * Let's hope other operating systems interpret the RTC the same way. 332 */ 333 /* read RTC exactly on falling edge of update flag */ 334 for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */ 335 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
336 break;
337 for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */ 338 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
339 break;
340 do{/* Isn't this overkill ? UIP above should guarantee consistency */ 341 sec = CMOS_READ(RTC_SECONDS);
342 min = CMOS_READ(RTC_MINUTES);
343 hour = CMOS_READ(RTC_HOURS);
344 day = CMOS_READ(RTC_DAY_OF_MONTH);
345 mon = CMOS_READ(RTC_MONTH);
346 year = CMOS_READ(RTC_YEAR);
347 }while (sec != CMOS_READ(RTC_SECONDS));
348 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
349 { 350 BCD_TO_BIN(sec);
351 BCD_TO_BIN(min);
352 BCD_TO_BIN(hour);
353 BCD_TO_BIN(day);
354 BCD_TO_BIN(mon);
355 BCD_TO_BIN(year);
356 } 357 if ((year += 1900) < 1970)
358 year += 100;
359 returnmktime(year, mon, day, hour, min, sec);
360 } 361
362 staticstructirqactionirq0 = {timer_interrupt, 0, 0, "timer", NULL, NULL};
363
364 voidtime_init(void)
/* */ 365 { 366 xtime.tv_sec = get_cmos_time();
367 xtime.tv_usec = 0;
368
369 /* If we have the CPU hardware time counters, use them */ 370 #ifndefCONFIG_APM 371 /* Don't use them if a suspend/resume could 372 corrupt the timer value. This problem 373 needs more debugging. */ 374 if (x86_capability & 16) { 375 do_gettimeoffset = do_fast_gettimeoffset;
376 /* read Pentium cycle counter */ 377 __asm__(".byte 0x0f,0x31"
378 :"=a" (((unsignedlong *) &init_timer_cc)[0]),
379 "=d" (((unsignedlong *) &init_timer_cc)[1]));
380 irq0.handler = pentium_timer_interrupt;
381 } 382 #endif 383 setup_x86_irq(0, &irq0);
384 }