152118Smckusick /* 252118Smckusick * Copyright (c) 1988 University of Utah. 352118Smckusick * Copyright (c) 1992 The Regents of the University of California. 452118Smckusick * All rights reserved. 552118Smckusick * 652118Smckusick * This code is derived from software contributed to Berkeley by 752118Smckusick * the Systems Programming Group of the University of Utah Computer 852118Smckusick * Science Department and Ralph Campbell. 952118Smckusick * 1052118Smckusick * %sccs.include.redist.c% 1152118Smckusick * 1252118Smckusick * from: Utah $Hdr: clock.c 1.18 91/01/21$ 1352118Smckusick * 14*52699Sralph * @(#)clock.c 7.2 (Berkeley) 02/29/92 1552118Smckusick */ 1652118Smckusick 1752118Smckusick #include "param.h" 1852118Smckusick #include "kernel.h" 1952118Smckusick 2052118Smckusick #include "../include/machConst.h" 2152118Smckusick #include "clockreg.h" 2252118Smckusick 2352118Smckusick /* 2452118Smckusick * Machine-dependent clock routines. 2552118Smckusick * 2652118Smckusick * Startrtclock restarts the real-time clock, which provides 2752118Smckusick * hardclock interrupts to kern_clock.c. 2852118Smckusick * 2952118Smckusick * Inittodr initializes the time of day hardware which provides 3052118Smckusick * date functions. Its primary function is to use some file 3152118Smckusick * system information in case the hardare clock lost state. 3252118Smckusick * 3352118Smckusick * Resettodr restores the time of day hardware after a time change. 3452118Smckusick */ 3552118Smckusick 3652118Smckusick /* 3752118Smckusick * Start the real-time clock. 3852118Smckusick */ 3952118Smckusick startrtclock() 4052118Smckusick { 4152118Smckusick register volatile struct chiptime *c; 4252118Smckusick extern int tickadj; 4352118Smckusick 4452118Smckusick tick = 15625; /* number of micro-seconds between interrupts */ 4552118Smckusick hz = 1000000 / 15625; /* about 64 */ 4652118Smckusick tickadj = 240000 / (60000000 / 15625); 4752118Smckusick c = (volatile struct chiptime *)MACH_CLOCK_ADDR; 4852118Smckusick c->rega = REGA_TIME_BASE | SELECTED_RATE; 4952118Smckusick c->regb = REGB_PER_INT_ENA | REGB_DATA_MODE | REGB_HOURS_FORMAT; 5052118Smckusick } 5152118Smckusick 5252118Smckusick /* 5352118Smckusick * This code is defunct after 2099. 5452118Smckusick * Will Unix still be here then?? 5552118Smckusick */ 5652118Smckusick static short dayyr[12] = { 5752118Smckusick 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 5852118Smckusick }; 5952118Smckusick 6052118Smckusick /* 6152118Smckusick * Initialze the time of day register, based on the time base which is, e.g. 6252118Smckusick * from a filesystem. Base provides the time to within six months, 6352118Smckusick * and the time of year clock (if any) provides the rest. 6452118Smckusick */ 6552118Smckusick inittodr(base) 6652118Smckusick time_t base; 6752118Smckusick { 6852118Smckusick register volatile struct chiptime *c; 6952118Smckusick register int days, yr; 7052118Smckusick int sec, min, hour, day, mon, year; 7152118Smckusick long deltat, badbase = 0; 7252118Smckusick int s; 7352118Smckusick 7452118Smckusick if (base < 5*SECYR) { 7552118Smckusick printf("WARNING: preposterous time in file system\n"); 7652118Smckusick /* read the system clock anyway */ 7752118Smckusick base = 6*SECYR + 186*SECDAY + SECDAY/2; 7852118Smckusick badbase = 1; 7952118Smckusick } 8052118Smckusick 8152118Smckusick c = (volatile struct chiptime *)MACH_CLOCK_ADDR; 8252118Smckusick /* don't read clock registers while they are being updated */ 83*52699Sralph s = splclock(); 8452118Smckusick while ((c->rega & REGA_UIP) == 1) 8552118Smckusick ; 8652118Smckusick sec = c->sec; 8752118Smckusick min = c->min; 8852118Smckusick hour = c->hour; 8952118Smckusick day = c->day; 9052118Smckusick mon = c->mon; 9152118Smckusick year = c->year + 19; 9252118Smckusick splx(s); 93*52699Sralph 9452118Smckusick /* simple sanity checks */ 9552118Smckusick if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31 || 9652118Smckusick hour > 23 || min > 59 || sec > 59) { 9752118Smckusick printf("WARNING: preposterous clock chip time"); 9852118Smckusick /* 9952118Smckusick * Believe the time in the file system for lack of 10052118Smckusick * anything better, resetting the TODR. 10152118Smckusick */ 10252118Smckusick time.tv_sec = base; 10352118Smckusick if (!badbase) 10452118Smckusick resettodr(); 10552118Smckusick return (0); 10652118Smckusick } 10752118Smckusick days = 0; 10852118Smckusick for (yr = 70; yr < year; yr++) 10952118Smckusick days += LEAPYEAR(yr) ? 366 : 365; 11052118Smckusick days += dayyr[mon - 1] + day - 1; 11152118Smckusick if (LEAPYEAR(yr) && mon > 2) 11252118Smckusick days++; 11352118Smckusick /* now have days since Jan 1, 1970; the rest is easy... */ 11452118Smckusick time.tv_sec = days * SECDAY + hour * 3600 + min * 60 + sec; 11552118Smckusick 11652118Smckusick if (!badbase) { 11752118Smckusick /* 11852118Smckusick * See if we gained/lost two or more days; 11952118Smckusick * if so, assume something is amiss. 12052118Smckusick */ 12152118Smckusick deltat = time.tv_sec - base; 12252118Smckusick if (deltat < 0) 12352118Smckusick deltat = -deltat; 12452118Smckusick if (deltat < 2 * SECDAY) 12552118Smckusick return; 12652118Smckusick printf("WARNING: clock %s %d days", 12752118Smckusick time.tv_sec < base ? "lost" : "gained", deltat / SECDAY); 12852118Smckusick } 12952118Smckusick printf(" -- CHECK AND RESET THE DATE!\n"); 13052118Smckusick } 13152118Smckusick 13252118Smckusick /* 13352118Smckusick * Reset the TODR based on the time value; used when the TODR 13452118Smckusick * has a preposterous value and also when the time is reset 13552118Smckusick * by the stime system call. Also called when the TODR goes past 13652118Smckusick * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight) 13752118Smckusick * to wrap the TODR around. 13852118Smckusick */ 13952118Smckusick resettodr() 14052118Smckusick { 14152118Smckusick register volatile struct chiptime *c; 14252118Smckusick register int t, t2, t3; 143*52699Sralph int s; 14452118Smckusick 14552118Smckusick /* compute the year */ 14652118Smckusick t2 = time.tv_sec / SECDAY; 14752118Smckusick t = 69; 14852118Smckusick while (t2 >= 0) { /* whittle off years */ 14952118Smckusick t3 = t2; 15052118Smckusick t++; 15152118Smckusick t2 -= LEAPYEAR(t) ? 366 : 365; 15252118Smckusick } 153*52699Sralph 15452118Smckusick c = (volatile struct chiptime *)MACH_CLOCK_ADDR; 155*52699Sralph s = splclock(); 15652118Smckusick c->regb |= REGB_SET_TIME; 15752118Smckusick c->year = t - 19; 15852118Smckusick 15952118Smckusick /* t3 = month + day; separate */ 16052118Smckusick t = LEAPYEAR(t); 16152118Smckusick for (t2 = 1; t2 < 12; t2++) 16252118Smckusick if (t3 < dayyr[t2] + (t && t2 > 1)) 16352118Smckusick break; 16452118Smckusick 16552118Smckusick /* t2 is month */ 16652118Smckusick c->mon = t2; 16752118Smckusick t3 = t3 - dayyr[t2 - 1] + 1; 16852118Smckusick if (t && t2 > 2) 16952118Smckusick t3--; 17052118Smckusick c->day = t3; 17152118Smckusick 17252118Smckusick /* the rest is easy */ 17352118Smckusick t = time.tv_sec % SECDAY; 17452118Smckusick c->hour = t / 3600; 17552118Smckusick t %= 3600; 17652118Smckusick c->min = t / 60; 17752118Smckusick c->sec = t % 60; 17852118Smckusick c->regb &= ~REGB_SET_TIME; 179*52699Sralph MachEmptyWriteBuffer(); 180*52699Sralph splx(s); 18152118Smckusick } 182