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 27 #define TIMER_IRQ 0 28 29 static int set_rtc_mmss(unsigned long); 30 31 /* 32 * timer_interrupt() needs to keep up the real-time clock, 33 * as well as call the "do_timer()" routine every clocktick 34 */ 35 static void timer_interrupt(int irq, struct pt_regs * regs) /* */ 36 { 37 /* last time the cmos clock got updated */ 38 static long last_rtc_update=0; 39 40 do_timer(regs); 41 42 /* 43 * If we have an externally synchronized Linux clock, then update 44 * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be 45 * called as close as possible to 500 ms before the new second starts. 46 */ 47 if (time_state != TIME_BAD && xtime.tv_sec > last_rtc_update + 660 && 48 xtime.tv_usec > 500000 - (tick >> 1) && 49 xtime.tv_usec < 500000 + (tick >> 1)) 50 if (set_rtc_mmss(xtime.tv_sec) == 0) 51 last_rtc_update = xtime.tv_sec; 52 else 53 last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */ 54 } 55 56 /* Converts Gregorian date to seconds since 1970-01-01 00:00:00. 57 * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 58 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. 59 * 60 * [For the Julian calendar (which was used in Russia before 1917, 61 * Britain & colonies before 1752, anywhere else before 1582, 62 * and is still in use by some communities) leave out the 63 * -year/100+year/400 terms, and add 10.] 64 * 65 * This algorithm was first published by Gauss (I think). 66 * 67 * WARNING: this function will overflow on 2106-02-07 06:28:16 on 68 * machines were long is 32-bit! (However, as time_t is signed, we 69 * will already get problems at other places on 2038-01-19 03:14:08) 70 */ 71 static inline unsigned long mktime(unsigned int year, unsigned int mon, /* */ 72 unsigned int day, unsigned int hour, 73 unsigned int min, unsigned int sec) 74 { 75 if (0 >= (int) (mon -= 2)) { /* 1..12 -> 11,12,1..10 */ 76 mon += 12; /* Puts Feb last since it has leap day */ 77 year -= 1; 78 } 79 return ((( 80 (unsigned long)(year/4 - year/100 + year/400 + 367*mon/12 + day) + 81 year*365 - 719499 82 )*24 + hour /* now have hours */ 83 )*60 + min /* now have minutes */ 84 )*60 + sec; /* finally seconds */ 85 } 86 87 void time_init(void) /* */ 88 { 89 unsigned int year, mon, day, hour, min, sec; 90 int i; 91 92 /* The Linux interpretation of the CMOS clock register contents: 93 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the 94 * RTC registers show the second which has precisely just started. 95 * Let's hope other operating systems interpret the RTC the same way. 96 */ 97 /* read RTC exactly on falling edge of update flag */ 98 for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */ 99 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) 100 break; 101 for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */ 102 if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)) 103 break; 104 do { /* Isn't this overkill ? UIP above should guarantee consistency */ 105 sec = CMOS_READ(RTC_SECONDS); 106 min = CMOS_READ(RTC_MINUTES); 107 hour = CMOS_READ(RTC_HOURS); 108 day = CMOS_READ(RTC_DAY_OF_MONTH); 109 mon = CMOS_READ(RTC_MONTH); 110 year = CMOS_READ(RTC_YEAR); 111 } while (sec != CMOS_READ(RTC_SECONDS)); 112 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) 113 { 114 BCD_TO_BIN(sec); 115 BCD_TO_BIN(min); 116 BCD_TO_BIN(hour); 117 BCD_TO_BIN(day); 118 BCD_TO_BIN(mon); 119 BCD_TO_BIN(year); 120 } 121 if ((year += 1900) < 1970) 122 year += 100; 123 xtime.tv_sec = mktime(year, mon, day, hour, min, sec); 124 xtime.tv_usec = 0; 125 if (request_irq(TIMER_IRQ, timer_interrupt, 0, "timer") != 0) 126 panic("Could not allocate timer IRQ!"); 127 } 128 129 /* This function must be called with interrupts disabled 130 * It was inspired by Steve McCanne's microtime-i386 for BSD. -- jrs 131 * 132 * However, the pc-audio speaker driver changes the divisor so that 133 * it gets interrupted rather more often - it loads 64 into the 134 * counter rather than 11932! This has an adverse impact on 135 * do_gettimeoffset() -- it stops working! What is also not 136 * good is that the interval that our timer function gets called 137 * is no longer 10.0002 ms, but 9.9767 ms. To get around this 138 * would require using a different timing source. Maybe someone 139 * could use the RTC - I know that this can interrupt at frequencies 140 * ranging from 8192Hz to 2Hz. If I had the energy, I'd somehow fix 141 * it so that at startup, the timer code in sched.c would select 142 * using either the RTC or the 8253 timer. The decision would be 143 * based on whether there was any other device around that needed 144 * to trample on the 8253. I'd set up the RTC to interrupt at 1024 Hz, 145 * and then do some jiggery to have a version of do_timer that 146 * advanced the clock by 1/1024 s. Every time that reached over 1/100 147 * of a second, then do all the old code. If the time was kept correct 148 * then do_gettimeoffset could just return 0 - there is no low order 149 * divider that can be accessed. 150 * 151 * Ideally, you would be able to use the RTC for the speaker driver, 152 * but it appears that the speaker driver really needs interrupt more 153 * often than every 120 us or so. 154 * 155 * Anyway, this needs more thought.... pjsg (1993-08-28) 156 * 157 * If you are really that interested, you should be reading 158 * comp.protocols.time.ntp! 159 */ 160 161 #define TICK_SIZE tick 162 163 static inline unsigned long do_gettimeoffset(void) /* */ 164 { 165 int count; 166 unsigned long offset = 0; 167 168 /* timer count may underflow right here */ 169 outb_p(0x00, 0x43); /* latch the count ASAP */ 170 count = inb_p(0x40); /* read the latched count */ 171 count |= inb(0x40) << 8; 172 /* we know probability of underflow is always MUCH less than 1% */ 173 if (count > (LATCH - LATCH/100)) { 174 /* check for pending timer interrupt */ 175 outb_p(0x0a, 0x20); 176 if (inb(0x20) & 1) 177 offset = TICK_SIZE; 178 } 179 count = ((LATCH-1) - count) * TICK_SIZE; 180 count = (count + LATCH/2) / LATCH; 181 return offset + count; 182 } 183 184 /* 185 * This version of gettimeofday has near microsecond resolution. 186 */ 187 void do_gettimeofday(struct timeval *tv) /* */ 188 { 189 unsigned long flags; 190 191 save_flags(flags); 192 cli(); 193 *tv = xtime; 194 tv->tv_usec += do_gettimeoffset(); 195 if (tv->tv_usec >= 1000000) { 196 tv->tv_usec -= 1000000; 197 tv->tv_sec++; 198 } 199 restore_flags(flags); 200 } 201 202 void do_settimeofday(struct timeval *tv) /* */ 203 { 204 cli(); 205 /* This is revolting. We need to set the xtime.tv_usec 206 * correctly. However, the value in this location is 207 * is value at the last tick. 208 * Discover what correction gettimeofday 209 * would have done, and then undo it! 210 */ 211 tv->tv_usec -= do_gettimeoffset(); 212 213 if (tv->tv_usec < 0) { 214 tv->tv_usec += 1000000; 215 tv->tv_sec--; 216 } 217 218 xtime = *tv; 219 time_state = TIME_BAD; 220 time_maxerror = 0x70000000; 221 time_esterror = 0x70000000; 222 sti(); 223 } 224 225 226 /* 227 * In order to set the CMOS clock precisely, set_rtc_mmss has to be 228 * called 500 ms after the second nowtime has started, because when 229 * nowtime is written into the registers of the CMOS clock, it will 230 * jump to the next second precisely 500 ms later. Check the Motorola 231 * MC146818A or Dallas DS12887 data sheet for details. 232 */ 233 static int set_rtc_mmss(unsigned long nowtime) /* */ 234 { 235 int retval = 0; 236 int real_seconds, real_minutes, cmos_minutes; 237 unsigned char save_control, save_freq_select; 238 239 save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */ 240 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL); 241 242 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */ 243 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); 244 245 cmos_minutes = CMOS_READ(RTC_MINUTES); 246 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) 247 BCD_TO_BIN(cmos_minutes); 248 249 /* 250 * since we're only adjusting minutes and seconds, 251 * don't interfere with hour overflow. This avoids 252 * messing with unknown time zones but requires your 253 * RTC not to be off by more than 15 minutes 254 */ 255 real_seconds = nowtime % 60; 256 real_minutes = nowtime / 60; 257 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1) 258 real_minutes += 30; /* correct for half hour time zone */ 259 real_minutes %= 60; 260 261 if (abs(real_minutes - cmos_minutes) < 30) { 262 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { 263 BIN_TO_BCD(real_seconds); 264 BIN_TO_BCD(real_minutes); 265 } 266 CMOS_WRITE(real_seconds,RTC_SECONDS); 267 CMOS_WRITE(real_minutes,RTC_MINUTES); 268 } else 269 retval = -1; 270 271 /* The following flags have to be released exactly in this order, 272 * otherwise the DS12887 (popular MC146818A clone with integrated 273 * battery and quartz) will not reset the oscillator and will not 274 * update precisely 500 ms later. You won't find this mentioned in 275 * the Dallas Semiconductor data sheets, but who believes data 276 * sheets anyway ... -- Markus Kuhn 277 */ 278 CMOS_WRITE(save_control, RTC_CONTROL); 279 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); 280 281 return retval; 282 }