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