xref: /original-bsd/sys/news3400/news3400/clock.c (revision c02ddb16)
1b2535c44Smckusick /*
2b2535c44Smckusick  * Copyright (c) 1988 University of Utah.
3*c02ddb16Sbostic  * Copyright (c) 1992, 1993
4*c02ddb16Sbostic  *	The Regents of the University of California.  All rights reserved.
5b2535c44Smckusick  *
6b2535c44Smckusick  * This code is derived from software contributed to Berkeley by
7b2535c44Smckusick  * the Systems Programming Group of the University of Utah Computer
8b2535c44Smckusick  * Science Department, Ralph Campbell, and Kazumasa Utashiro of
9b2535c44Smckusick  * Software Research Associates, Inc.
10b2535c44Smckusick  *
11b2535c44Smckusick  * %sccs.include.redist.c%
12b2535c44Smckusick  *
13b2535c44Smckusick  * from: Utah $Hdr: clock.c 1.18 91/01/21$
14b2535c44Smckusick  *
15*c02ddb16Sbostic  *	@(#)clock.c	8.1 (Berkeley) 06/11/93
16b2535c44Smckusick  */
17b2535c44Smckusick 
18a4eab072Sutashiro #include <machine/adrsmap.h>
19b2535c44Smckusick 
20a4eab072Sutashiro #include <sys/param.h>
21a4eab072Sutashiro #include <sys/kernel.h>
22b2535c44Smckusick 
23a4eab072Sutashiro #include <news3400/news3400/clockreg.h>
24b2535c44Smckusick 
25b2535c44Smckusick /*
26b2535c44Smckusick  * Machine-dependent clock routines.
27b2535c44Smckusick  *
28b2535c44Smckusick  * Startrtclock restarts the real-time clock, which provides
29b2535c44Smckusick  * hardclock interrupts to kern_clock.c.
30b2535c44Smckusick  *
31b2535c44Smckusick  * Inittodr initializes the time of day hardware which provides
32b2535c44Smckusick  * date functions.  Its primary function is to use some file
33b2535c44Smckusick  * system information in case the hardare clock lost state.
34b2535c44Smckusick  *
35b2535c44Smckusick  * Resettodr restores the time of day hardware after a time change.
36b2535c44Smckusick  */
37b2535c44Smckusick 
38b2535c44Smckusick /*
3918e6ce8eSbostic  * We assume newhz is either stathz or profhz, and that neither will
4018e6ce8eSbostic  * change after being set up above.  Could recalculate intervals here
4118e6ce8eSbostic  * but that would be a drag.
4218e6ce8eSbostic  */
4318e6ce8eSbostic void
setstatclockrate(newhz)4418e6ce8eSbostic setstatclockrate(newhz)
4518e6ce8eSbostic 	int newhz;
4618e6ce8eSbostic {
4718e6ce8eSbostic 
4818e6ce8eSbostic 	/* KU:XXX do something! */
4918e6ce8eSbostic }
5018e6ce8eSbostic 
5118e6ce8eSbostic /*
527e6db3ccSutashiro  * Set up the real-time and statistics clocks.  Leave stathz 0 only if
537e6db3ccSutashiro  * no alternative timer is available.
547e6db3ccSutashiro  */
cpu_initclocks()557e6db3ccSutashiro cpu_initclocks()
567e6db3ccSutashiro {
577e6db3ccSutashiro 
5867bda491Sutashiro 	/*
5967bda491Sutashiro 	 * Start the real-time clock.
6067bda491Sutashiro 	 */
6167bda491Sutashiro 	*(char *)ITIMER = IOCLOCK / 6144 / 100 - 1;
6267bda491Sutashiro 
6367bda491Sutashiro 	/*
6467bda491Sutashiro 	 * Enable the real-time clock.
6567bda491Sutashiro 	 */
6667bda491Sutashiro 	*(char *)INTEN0 |= (char)INTEN0_TIMINT;
677e6db3ccSutashiro }
687e6db3ccSutashiro 
697e6db3ccSutashiro /*
70b2535c44Smckusick  * This code is defunct after 2099.
71b2535c44Smckusick  * Will Unix still be here then??
72b2535c44Smckusick  */
73b2535c44Smckusick static short dayyr[12] = {
74b2535c44Smckusick 	0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
75b2535c44Smckusick };
76b2535c44Smckusick 
7714093794Sutashiro #define	bcd_to_int(BCD)	(i = BCD, (((i) >> 4) & 0xf) * 10 + ((i) & 0xf))
78b2535c44Smckusick #define	int_to_bcd(INT)	(i = INT, ((((i) / 10) % 10) << 4) + (i) % 10)
79b2535c44Smckusick 
80b2535c44Smckusick /*
81b2535c44Smckusick  * Initialze the time of day register, based on the time base which is, e.g.
82b2535c44Smckusick  * from a filesystem.  Base provides the time to within six months,
83b2535c44Smckusick  * and the time of year clock (if any) provides the rest.
84b2535c44Smckusick  */
inittodr(base)85b2535c44Smckusick inittodr(base)
86b2535c44Smckusick 	time_t base;
87b2535c44Smckusick {
88b2535c44Smckusick 	register volatile u_char *rtc_port = (u_char *)RTC_PORT;
89b2535c44Smckusick 	register volatile u_char *rtc_data = (u_char *)DATA_PORT;
90b2535c44Smckusick 	register int days, yr;
91b2535c44Smckusick 	int sec, min, hour, week, day, mon, year;
92b2535c44Smckusick 	long deltat, badbase = 0;
9314093794Sutashiro 	register u_int i;
94b2535c44Smckusick 
95b2535c44Smckusick 	if (base < 5*SECYR) {
96b2535c44Smckusick 		printf("WARNING: preposterous time in file system\n");
97b2535c44Smckusick 		/* read the system clock anyway */
98b2535c44Smckusick 		base = 6*SECYR + 186*SECDAY + SECDAY/2;
99b2535c44Smckusick 		badbase = 1;
100b2535c44Smckusick 	}
101b2535c44Smckusick 
102b2535c44Smckusick 	*rtc_port = READ_CLOCK;
103b2535c44Smckusick 	sec  = bcd_to_int(*rtc_data++);
104b2535c44Smckusick 	min  = bcd_to_int(*rtc_data++);
105b2535c44Smckusick 	hour = bcd_to_int(*rtc_data++);
106b2535c44Smckusick 	week = bcd_to_int(*rtc_data++);
107b2535c44Smckusick 	day  = bcd_to_int(*rtc_data++);
108b2535c44Smckusick 	mon  = bcd_to_int(*rtc_data++);
109b2535c44Smckusick 	year = bcd_to_int(*rtc_data++);
110b2535c44Smckusick 	*rtc_port = 0;
111b2535c44Smckusick 
112b2535c44Smckusick 	/* simple sanity checks */
113b2535c44Smckusick 	if (year < 70 || mon < 1 || mon > 12 || day < 1 || day > 31 ||
114b2535c44Smckusick 	    hour > 23 || min > 59 || sec > 59) {
11514093794Sutashiro 		printf("WARNING: preposterous clock chip time\n");
116b2535c44Smckusick 		/*
117b2535c44Smckusick 		 * Believe the time in the file system for lack of
118b2535c44Smckusick 		 * anything better, resetting the TODR.
119b2535c44Smckusick 		 */
120b2535c44Smckusick 		time.tv_sec = base;
121b2535c44Smckusick 		if (!badbase)
122b2535c44Smckusick 			resettodr();
123b2535c44Smckusick 		return (0);
124b2535c44Smckusick 	}
125b2535c44Smckusick 	days = 0;
126b2535c44Smckusick 	for (yr = 70; yr < year; yr++)
127b2535c44Smckusick 		days += LEAPYEAR(yr) ? 366 : 365;
128b2535c44Smckusick 	days += dayyr[mon - 1] + day - 1;
129b2535c44Smckusick 	if (LEAPYEAR(yr) && mon > 2)
130b2535c44Smckusick 		days++;
131b2535c44Smckusick 	/* now have days since Jan 1, 1970; the rest is easy... */
132b2535c44Smckusick 	time.tv_sec = days * SECDAY + hour * 3600 + min * 60 + sec;
133b2535c44Smckusick 
134b2535c44Smckusick 	if (!badbase) {
135b2535c44Smckusick 		/*
136b2535c44Smckusick 		 * See if we gained/lost two or more days;
137b2535c44Smckusick 		 * if so, assume something is amiss.
138b2535c44Smckusick 		 */
139b2535c44Smckusick 		deltat = time.tv_sec - base;
140b2535c44Smckusick 		if (deltat < 0)
141b2535c44Smckusick 			deltat = -deltat;
142b2535c44Smckusick 		if (deltat < 2 * SECDAY)
143b2535c44Smckusick 			return;
144b2535c44Smckusick 		printf("WARNING: clock %s %d days",
145b2535c44Smckusick 		    time.tv_sec < base ? "lost" : "gained", deltat / SECDAY);
146b2535c44Smckusick 	}
147b2535c44Smckusick 	printf(" -- CHECK AND RESET THE DATE!\n");
148b2535c44Smckusick }
149b2535c44Smckusick 
150b2535c44Smckusick /*
151b2535c44Smckusick  * Reset the TODR based on the time value; used when the TODR
152b2535c44Smckusick  * has a preposterous value and also when the time is reset
153b2535c44Smckusick  * by the stime system call.  Also called when the TODR goes past
154b2535c44Smckusick  * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
155b2535c44Smckusick  * to wrap the TODR around.
156b2535c44Smckusick  */
resettodr()157b2535c44Smckusick resettodr()
158b2535c44Smckusick {
159b2535c44Smckusick 	register volatile u_char *rtc_port = (u_char *)RTC_PORT;
160b2535c44Smckusick 	register volatile u_char *rtc_data = (u_char *)DATA_PORT;
161b2535c44Smckusick 	int sec, min, hour, week, day, mon, year;
162b2535c44Smckusick 	register int t, t2, t3;
163b2535c44Smckusick 	register int i;
164b2535c44Smckusick 
165b2535c44Smckusick 	/* compute the year */
166b2535c44Smckusick 	t2 = time.tv_sec / SECDAY;
167b2535c44Smckusick 	t = 69;
168b2535c44Smckusick 	while (t2 >= 0) {	/* whittle off years */
169b2535c44Smckusick 		t3 = t2;
170b2535c44Smckusick 		t++;
171b2535c44Smckusick 		t2 -= LEAPYEAR(t) ? 366 : 365;
172b2535c44Smckusick 	}
173b2535c44Smckusick 
174b2535c44Smckusick 	year = t;
175b2535c44Smckusick 
176b2535c44Smckusick 	/* t3 = month + day; separate */
177b2535c44Smckusick 	t = LEAPYEAR(t);
178b2535c44Smckusick 	for (t2 = 1; t2 < 12; t2++)
179b2535c44Smckusick 		if (t3 < dayyr[t2] + (t && t2 > 1))
180b2535c44Smckusick 			break;
181b2535c44Smckusick 
182b2535c44Smckusick 	/* t2 is month */
183b2535c44Smckusick 	mon = t2;
184b2535c44Smckusick 	t3 = t3 - dayyr[t2 - 1] + 1;
185b2535c44Smckusick 	if (t && t2 > 2)
186b2535c44Smckusick 		t3--;
187b2535c44Smckusick 	day = t3;
188b2535c44Smckusick 
189b2535c44Smckusick 	week = 0;
190b2535c44Smckusick 
191b2535c44Smckusick 	/* the rest is easy */
192b2535c44Smckusick 	t = time.tv_sec % SECDAY;
193b2535c44Smckusick 	hour = t / 3600;
194b2535c44Smckusick 	t %= 3600;
195b2535c44Smckusick 	min = t / 60;
196b2535c44Smckusick 	sec = t % 60;
197b2535c44Smckusick 
198b2535c44Smckusick 	*rtc_port = SET_CLOCK;
199b2535c44Smckusick 	*rtc_data++ = int_to_bcd(sec);
200b2535c44Smckusick 	*rtc_data++ = int_to_bcd(min);
201b2535c44Smckusick 	*rtc_data++ = int_to_bcd(hour);
202b2535c44Smckusick 	*rtc_data++ = int_to_bcd(week);
203b2535c44Smckusick 	*rtc_data++ = int_to_bcd(day);
204b2535c44Smckusick 	*rtc_data++ = int_to_bcd(mon);
205b2535c44Smckusick 	*rtc_data   = int_to_bcd(year);
206b2535c44Smckusick 	*rtc_port = 0;
207b2535c44Smckusick }
208