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