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*56524Sbostic * @(#)clock.c 7.5 (Berkeley) 10/11/92 1552118Smckusick */ 1652118Smckusick 17*56524Sbostic #include <sys/param.h> 18*56524Sbostic #include <sys/kernel.h> 1952118Smckusick 20*56524Sbostic #include <machine/machConst.h> 21*56524Sbostic #include <pmax/pmax/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 /* 3756229Sralph * Start the real-time and statistics clocks. Leave stathz 0 since there 3856229Sralph * are no other timers available. 3952118Smckusick */ 4056229Sralph cpu_initclocks() 4152118Smckusick { 4252118Smckusick register volatile struct chiptime *c; 4352118Smckusick extern int tickadj; 4452118Smckusick 4552118Smckusick tick = 15625; /* number of micro-seconds between interrupts */ 4653204Sralph hz = 1000000 / 15625; /* 64 Hz */ 4752118Smckusick tickadj = 240000 / (60000000 / 15625); 4852118Smckusick c = (volatile struct chiptime *)MACH_CLOCK_ADDR; 4952118Smckusick c->rega = REGA_TIME_BASE | SELECTED_RATE; 5052118Smckusick c->regb = REGB_PER_INT_ENA | REGB_DATA_MODE | REGB_HOURS_FORMAT; 5152118Smckusick } 5252118Smckusick 5352118Smckusick /* 5456229Sralph * We assume newhz is either stathz or profhz, and that neither will 5556229Sralph * change after being set up above. Could recalculate intervals here 5656229Sralph * but that would be a drag. 5756229Sralph */ 5856229Sralph void 5956229Sralph setstatclockrate(newhz) 6056229Sralph int newhz; 6156229Sralph { 6256229Sralph } 6356229Sralph 6456229Sralph /* 6552118Smckusick * This code is defunct after 2099. 6652118Smckusick * Will Unix still be here then?? 6752118Smckusick */ 6852118Smckusick static short dayyr[12] = { 6952118Smckusick 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 7052118Smckusick }; 7152118Smckusick 7252118Smckusick /* 7352118Smckusick * Initialze the time of day register, based on the time base which is, e.g. 7452118Smckusick * from a filesystem. Base provides the time to within six months, 7552118Smckusick * and the time of year clock (if any) provides the rest. 7652118Smckusick */ 7753204Sralph void 7852118Smckusick inittodr(base) 7952118Smckusick time_t base; 8052118Smckusick { 8152118Smckusick register volatile struct chiptime *c; 8252118Smckusick register int days, yr; 8352118Smckusick int sec, min, hour, day, mon, year; 8453204Sralph long deltat; 8553204Sralph int badbase, s; 8652118Smckusick 8752118Smckusick if (base < 5*SECYR) { 8853204Sralph printf("WARNING: preposterous time in file system"); 8952118Smckusick /* read the system clock anyway */ 9052118Smckusick base = 6*SECYR + 186*SECDAY + SECDAY/2; 9152118Smckusick badbase = 1; 9253204Sralph } else 9353204Sralph badbase = 0; 9452118Smckusick 9552118Smckusick c = (volatile struct chiptime *)MACH_CLOCK_ADDR; 9652118Smckusick /* don't read clock registers while they are being updated */ 9752699Sralph s = splclock(); 9852118Smckusick while ((c->rega & REGA_UIP) == 1) 9952118Smckusick ; 10052118Smckusick sec = c->sec; 10152118Smckusick min = c->min; 10252118Smckusick hour = c->hour; 10352118Smckusick day = c->day; 10452118Smckusick mon = c->mon; 10553204Sralph year = c->year + 20; /* must be multiple of 4 because chip knows leap */ 10652118Smckusick splx(s); 10752699Sralph 10852118Smckusick /* simple sanity checks */ 10952118Smckusick if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31 || 11052118Smckusick hour > 23 || min > 59 || sec > 59) { 11152118Smckusick /* 11252118Smckusick * Believe the time in the file system for lack of 11352118Smckusick * anything better, resetting the TODR. 11452118Smckusick */ 11552118Smckusick time.tv_sec = base; 11653204Sralph if (!badbase) { 11753204Sralph printf("WARNING: preposterous clock chip time\n"); 11852118Smckusick resettodr(); 11953204Sralph } 12053204Sralph goto bad; 12152118Smckusick } 12252118Smckusick days = 0; 12352118Smckusick for (yr = 70; yr < year; yr++) 12452118Smckusick days += LEAPYEAR(yr) ? 366 : 365; 12552118Smckusick days += dayyr[mon - 1] + day - 1; 12652118Smckusick if (LEAPYEAR(yr) && mon > 2) 12752118Smckusick days++; 12852118Smckusick /* now have days since Jan 1, 1970; the rest is easy... */ 12952118Smckusick time.tv_sec = days * SECDAY + hour * 3600 + min * 60 + sec; 13052118Smckusick 13152118Smckusick if (!badbase) { 13252118Smckusick /* 13352118Smckusick * See if we gained/lost two or more days; 13452118Smckusick * if so, assume something is amiss. 13552118Smckusick */ 13652118Smckusick deltat = time.tv_sec - base; 13752118Smckusick if (deltat < 0) 13852118Smckusick deltat = -deltat; 13952118Smckusick if (deltat < 2 * SECDAY) 14052118Smckusick return; 14152118Smckusick printf("WARNING: clock %s %d days", 14252118Smckusick time.tv_sec < base ? "lost" : "gained", deltat / SECDAY); 14352118Smckusick } 14453204Sralph bad: 14552118Smckusick printf(" -- CHECK AND RESET THE DATE!\n"); 14652118Smckusick } 14752118Smckusick 14852118Smckusick /* 14952118Smckusick * Reset the TODR based on the time value; used when the TODR 15052118Smckusick * has a preposterous value and also when the time is reset 15152118Smckusick * by the stime system call. Also called when the TODR goes past 15252118Smckusick * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight) 15352118Smckusick * to wrap the TODR around. 15452118Smckusick */ 15552118Smckusick resettodr() 15652118Smckusick { 15752118Smckusick register volatile struct chiptime *c; 15853204Sralph register int t, t2; 15953204Sralph int sec, min, hour, day, mon, year; 16052699Sralph int s; 16152118Smckusick 16252118Smckusick /* compute the year */ 16352118Smckusick t2 = time.tv_sec / SECDAY; 16453204Sralph year = 69; 16552118Smckusick while (t2 >= 0) { /* whittle off years */ 16653204Sralph t = t2; 16753204Sralph year++; 16853204Sralph t2 -= LEAPYEAR(year) ? 366 : 365; 16952118Smckusick } 17052699Sralph 17153204Sralph /* t = month + day; separate */ 17253204Sralph t2 = LEAPYEAR(year); 17353204Sralph for (mon = 1; mon < 12; mon++) 17453204Sralph if (t < dayyr[mon] + (t2 && mon > 1)) 17552118Smckusick break; 17652118Smckusick 17753204Sralph day = t - dayyr[mon - 1] + 1; 17853204Sralph if (t2 && mon > 2) 17953204Sralph day--; 18052118Smckusick 18152118Smckusick /* the rest is easy */ 18252118Smckusick t = time.tv_sec % SECDAY; 18353204Sralph hour = t / 3600; 18452118Smckusick t %= 3600; 18553204Sralph min = t / 60; 18653204Sralph sec = t % 60; 18753204Sralph 18853204Sralph c = (volatile struct chiptime *)MACH_CLOCK_ADDR; 18953204Sralph s = splclock(); 19053204Sralph t = c->regb; 19153204Sralph c->regb = t | REGB_SET_TIME; 19252699Sralph MachEmptyWriteBuffer(); 19353204Sralph c->sec = sec; 19453204Sralph c->min = min; 19553204Sralph c->hour = hour; 19653204Sralph c->day = day; 19753204Sralph c->mon = mon; 19853204Sralph c->year = year - 20; /* must be multiple of 4 because chip knows leap */ 19953204Sralph c->regb = t; 20053204Sralph MachEmptyWriteBuffer(); 20152699Sralph splx(s); 20252118Smckusick } 203