xref: /csrg-svn/sys/pmax/pmax/clock.c (revision 53204)
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*53204Sralph  *	@(#)clock.c	7.3 (Berkeley) 04/19/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 */
45*53204Sralph 	hz = 1000000 / 15625;	/* 64 Hz */
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  */
65*53204Sralph void
6652118Smckusick inittodr(base)
6752118Smckusick 	time_t base;
6852118Smckusick {
6952118Smckusick 	register volatile struct chiptime *c;
7052118Smckusick 	register int days, yr;
7152118Smckusick 	int sec, min, hour, day, mon, year;
72*53204Sralph 	long deltat;
73*53204Sralph 	int badbase, s;
7452118Smckusick 
7552118Smckusick 	if (base < 5*SECYR) {
76*53204Sralph 		printf("WARNING: preposterous time in file system");
7752118Smckusick 		/* read the system clock anyway */
7852118Smckusick 		base = 6*SECYR + 186*SECDAY + SECDAY/2;
7952118Smckusick 		badbase = 1;
80*53204Sralph 	} else
81*53204Sralph 		badbase = 0;
8252118Smckusick 
8352118Smckusick 	c = (volatile struct chiptime *)MACH_CLOCK_ADDR;
8452118Smckusick 	/* don't read clock registers while they are being updated */
8552699Sralph 	s = splclock();
8652118Smckusick 	while ((c->rega & REGA_UIP) == 1)
8752118Smckusick 		;
8852118Smckusick 	sec = c->sec;
8952118Smckusick 	min = c->min;
9052118Smckusick 	hour = c->hour;
9152118Smckusick 	day = c->day;
9252118Smckusick 	mon = c->mon;
93*53204Sralph 	year = c->year + 20; /* must be multiple of 4 because chip knows leap */
9452118Smckusick 	splx(s);
9552699Sralph 
9652118Smckusick 	/* simple sanity checks */
9752118Smckusick 	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31 ||
9852118Smckusick 	    hour > 23 || min > 59 || sec > 59) {
9952118Smckusick 		/*
10052118Smckusick 		 * Believe the time in the file system for lack of
10152118Smckusick 		 * anything better, resetting the TODR.
10252118Smckusick 		 */
10352118Smckusick 		time.tv_sec = base;
104*53204Sralph 		if (!badbase) {
105*53204Sralph 			printf("WARNING: preposterous clock chip time\n");
10652118Smckusick 			resettodr();
107*53204Sralph 		}
108*53204Sralph 		goto bad;
10952118Smckusick 	}
11052118Smckusick 	days = 0;
11152118Smckusick 	for (yr = 70; yr < year; yr++)
11252118Smckusick 		days += LEAPYEAR(yr) ? 366 : 365;
11352118Smckusick 	days += dayyr[mon - 1] + day - 1;
11452118Smckusick 	if (LEAPYEAR(yr) && mon > 2)
11552118Smckusick 		days++;
11652118Smckusick 	/* now have days since Jan 1, 1970; the rest is easy... */
11752118Smckusick 	time.tv_sec = days * SECDAY + hour * 3600 + min * 60 + sec;
11852118Smckusick 
11952118Smckusick 	if (!badbase) {
12052118Smckusick 		/*
12152118Smckusick 		 * See if we gained/lost two or more days;
12252118Smckusick 		 * if so, assume something is amiss.
12352118Smckusick 		 */
12452118Smckusick 		deltat = time.tv_sec - base;
12552118Smckusick 		if (deltat < 0)
12652118Smckusick 			deltat = -deltat;
12752118Smckusick 		if (deltat < 2 * SECDAY)
12852118Smckusick 			return;
12952118Smckusick 		printf("WARNING: clock %s %d days",
13052118Smckusick 		    time.tv_sec < base ? "lost" : "gained", deltat / SECDAY);
13152118Smckusick 	}
132*53204Sralph bad:
13352118Smckusick 	printf(" -- CHECK AND RESET THE DATE!\n");
13452118Smckusick }
13552118Smckusick 
13652118Smckusick /*
13752118Smckusick  * Reset the TODR based on the time value; used when the TODR
13852118Smckusick  * has a preposterous value and also when the time is reset
13952118Smckusick  * by the stime system call.  Also called when the TODR goes past
14052118Smckusick  * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
14152118Smckusick  * to wrap the TODR around.
14252118Smckusick  */
14352118Smckusick resettodr()
14452118Smckusick {
14552118Smckusick 	register volatile struct chiptime *c;
146*53204Sralph 	register int t, t2;
147*53204Sralph 	int sec, min, hour, day, mon, year;
14852699Sralph 	int s;
14952118Smckusick 
15052118Smckusick 	/* compute the year */
15152118Smckusick 	t2 = time.tv_sec / SECDAY;
152*53204Sralph 	year = 69;
15352118Smckusick 	while (t2 >= 0) {	/* whittle off years */
154*53204Sralph 		t = t2;
155*53204Sralph 		year++;
156*53204Sralph 		t2 -= LEAPYEAR(year) ? 366 : 365;
15752118Smckusick 	}
15852699Sralph 
159*53204Sralph 	/* t = month + day; separate */
160*53204Sralph 	t2 = LEAPYEAR(year);
161*53204Sralph 	for (mon = 1; mon < 12; mon++)
162*53204Sralph 		if (t < dayyr[mon] + (t2 && mon > 1))
16352118Smckusick 			break;
16452118Smckusick 
165*53204Sralph 	day = t - dayyr[mon - 1] + 1;
166*53204Sralph 	if (t2 && mon > 2)
167*53204Sralph 		day--;
16852118Smckusick 
16952118Smckusick 	/* the rest is easy */
17052118Smckusick 	t = time.tv_sec % SECDAY;
171*53204Sralph 	hour = t / 3600;
17252118Smckusick 	t %= 3600;
173*53204Sralph 	min = t / 60;
174*53204Sralph 	sec = t % 60;
175*53204Sralph 
176*53204Sralph 	c = (volatile struct chiptime *)MACH_CLOCK_ADDR;
177*53204Sralph 	s = splclock();
178*53204Sralph 	t = c->regb;
179*53204Sralph 	c->regb = t | REGB_SET_TIME;
18052699Sralph 	MachEmptyWriteBuffer();
181*53204Sralph 	c->sec = sec;
182*53204Sralph 	c->min = min;
183*53204Sralph 	c->hour = hour;
184*53204Sralph 	c->day = day;
185*53204Sralph 	c->mon = mon;
186*53204Sralph 	c->year = year - 20; /* must be multiple of 4 because chip knows leap */
187*53204Sralph 	c->regb = t;
188*53204Sralph 	MachEmptyWriteBuffer();
18952699Sralph 	splx(s);
19052118Smckusick }
191