1 /* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1992 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department and Ralph Campbell. 9 * 10 * %sccs.include.redist.c% 11 * 12 * from: Utah $Hdr: clock.c 1.18 91/01/21$ 13 * 14 * @(#)clock.c 7.5 (Berkeley) 10/11/92 15 */ 16 17 #include <sys/param.h> 18 #include <sys/kernel.h> 19 20 #include <machine/machConst.h> 21 #include <pmax/pmax/clockreg.h> 22 23 /* 24 * Machine-dependent clock routines. 25 * 26 * Startrtclock restarts the real-time clock, which provides 27 * hardclock interrupts to kern_clock.c. 28 * 29 * Inittodr initializes the time of day hardware which provides 30 * date functions. Its primary function is to use some file 31 * system information in case the hardare clock lost state. 32 * 33 * Resettodr restores the time of day hardware after a time change. 34 */ 35 36 /* 37 * Start the real-time and statistics clocks. Leave stathz 0 since there 38 * are no other timers available. 39 */ 40 cpu_initclocks() 41 { 42 register volatile struct chiptime *c; 43 extern int tickadj; 44 45 tick = 15625; /* number of micro-seconds between interrupts */ 46 hz = 1000000 / 15625; /* 64 Hz */ 47 tickadj = 240000 / (60000000 / 15625); 48 c = (volatile struct chiptime *)MACH_CLOCK_ADDR; 49 c->rega = REGA_TIME_BASE | SELECTED_RATE; 50 c->regb = REGB_PER_INT_ENA | REGB_DATA_MODE | REGB_HOURS_FORMAT; 51 } 52 53 /* 54 * We assume newhz is either stathz or profhz, and that neither will 55 * change after being set up above. Could recalculate intervals here 56 * but that would be a drag. 57 */ 58 void 59 setstatclockrate(newhz) 60 int newhz; 61 { 62 } 63 64 /* 65 * This code is defunct after 2099. 66 * Will Unix still be here then?? 67 */ 68 static short dayyr[12] = { 69 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 70 }; 71 72 /* 73 * Initialze the time of day register, based on the time base which is, e.g. 74 * from a filesystem. Base provides the time to within six months, 75 * and the time of year clock (if any) provides the rest. 76 */ 77 void 78 inittodr(base) 79 time_t base; 80 { 81 register volatile struct chiptime *c; 82 register int days, yr; 83 int sec, min, hour, day, mon, year; 84 long deltat; 85 int badbase, s; 86 87 if (base < 5*SECYR) { 88 printf("WARNING: preposterous time in file system"); 89 /* read the system clock anyway */ 90 base = 6*SECYR + 186*SECDAY + SECDAY/2; 91 badbase = 1; 92 } else 93 badbase = 0; 94 95 c = (volatile struct chiptime *)MACH_CLOCK_ADDR; 96 /* don't read clock registers while they are being updated */ 97 s = splclock(); 98 while ((c->rega & REGA_UIP) == 1) 99 ; 100 sec = c->sec; 101 min = c->min; 102 hour = c->hour; 103 day = c->day; 104 mon = c->mon; 105 year = c->year + 20; /* must be multiple of 4 because chip knows leap */ 106 splx(s); 107 108 /* simple sanity checks */ 109 if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31 || 110 hour > 23 || min > 59 || sec > 59) { 111 /* 112 * Believe the time in the file system for lack of 113 * anything better, resetting the TODR. 114 */ 115 time.tv_sec = base; 116 if (!badbase) { 117 printf("WARNING: preposterous clock chip time\n"); 118 resettodr(); 119 } 120 goto bad; 121 } 122 days = 0; 123 for (yr = 70; yr < year; yr++) 124 days += LEAPYEAR(yr) ? 366 : 365; 125 days += dayyr[mon - 1] + day - 1; 126 if (LEAPYEAR(yr) && mon > 2) 127 days++; 128 /* now have days since Jan 1, 1970; the rest is easy... */ 129 time.tv_sec = days * SECDAY + hour * 3600 + min * 60 + sec; 130 131 if (!badbase) { 132 /* 133 * See if we gained/lost two or more days; 134 * if so, assume something is amiss. 135 */ 136 deltat = time.tv_sec - base; 137 if (deltat < 0) 138 deltat = -deltat; 139 if (deltat < 2 * SECDAY) 140 return; 141 printf("WARNING: clock %s %d days", 142 time.tv_sec < base ? "lost" : "gained", deltat / SECDAY); 143 } 144 bad: 145 printf(" -- CHECK AND RESET THE DATE!\n"); 146 } 147 148 /* 149 * Reset the TODR based on the time value; used when the TODR 150 * has a preposterous value and also when the time is reset 151 * by the stime system call. Also called when the TODR goes past 152 * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight) 153 * to wrap the TODR around. 154 */ 155 resettodr() 156 { 157 register volatile struct chiptime *c; 158 register int t, t2; 159 int sec, min, hour, day, mon, year; 160 int s; 161 162 /* compute the year */ 163 t2 = time.tv_sec / SECDAY; 164 year = 69; 165 while (t2 >= 0) { /* whittle off years */ 166 t = t2; 167 year++; 168 t2 -= LEAPYEAR(year) ? 366 : 365; 169 } 170 171 /* t = month + day; separate */ 172 t2 = LEAPYEAR(year); 173 for (mon = 1; mon < 12; mon++) 174 if (t < dayyr[mon] + (t2 && mon > 1)) 175 break; 176 177 day = t - dayyr[mon - 1] + 1; 178 if (t2 && mon > 2) 179 day--; 180 181 /* the rest is easy */ 182 t = time.tv_sec % SECDAY; 183 hour = t / 3600; 184 t %= 3600; 185 min = t / 60; 186 sec = t % 60; 187 188 c = (volatile struct chiptime *)MACH_CLOCK_ADDR; 189 s = splclock(); 190 t = c->regb; 191 c->regb = t | REGB_SET_TIME; 192 MachEmptyWriteBuffer(); 193 c->sec = sec; 194 c->min = min; 195 c->hour = hour; 196 c->day = day; 197 c->mon = mon; 198 c->year = year - 20; /* must be multiple of 4 because chip knows leap */ 199 c->regb = t; 200 MachEmptyWriteBuffer(); 201 splx(s); 202 } 203