xref: /netbsd-src/sys/kern/kern_todr.c (revision 4bfc10355ca5ccd94d950ad6f7092be3470193fa)
1 /*	$NetBSD: kern_todr.c,v 1.29 2009/03/21 15:01:57 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * the Systems Programming Group of the University of Utah Computer
9  * Science Department and Ralph Campbell.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * from: Utah Hdr: clock.c 1.18 91/01/21
36  *
37  *	@(#)clock.c	8.1 (Berkeley) 6/10/93
38  */
39 /*
40  * Copyright (c) 1988 University of Utah.
41  *
42  * This code is derived from software contributed to Berkeley by
43  * the Systems Programming Group of the University of Utah Computer
44  * Science Department and Ralph Campbell.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. All advertising materials mentioning features or use of this software
55  *    must display the following acknowledgement:
56  *	This product includes software developed by the University of
57  *	California, Berkeley and its contributors.
58  * 4. Neither the name of the University nor the names of its contributors
59  *    may be used to endorse or promote products derived from this software
60  *    without specific prior written permission.
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  *
74  * from: Utah Hdr: clock.c 1.18 91/01/21
75  *
76  *	@(#)clock.c	8.1 (Berkeley) 6/10/93
77  */
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.29 2009/03/21 15:01:57 ad Exp $");
80 
81 #include <sys/param.h>
82 #include <sys/kernel.h>
83 #include <sys/systm.h>
84 #include <sys/device.h>
85 #include <sys/timetc.h>
86 #include <sys/intr.h>
87 
88 #include <dev/clock_subr.h>	/* hmm.. this should probably move to sys */
89 
90 static todr_chip_handle_t todr_handle = NULL;
91 
92 /*
93  * Attach the clock device to todr_handle.
94  */
95 void
96 todr_attach(todr_chip_handle_t todr)
97 {
98 
99 	if (todr_handle) {
100 		printf("todr_attach: TOD already configured\n");
101 		return;
102 	}
103 	todr_handle = todr;
104 }
105 
106 static int timeset = 0;
107 
108 /*
109  * Set up the system's time, given a `reasonable' time value.
110  */
111 void
112 inittodr(time_t base)
113 {
114 	int badbase = 0, waszero = (base == 0), goodtime = 0, badrtc = 0;
115 	int s;
116 	struct timespec ts;
117 	struct timeval tv;
118 
119 	if (base < 5 * SECYR) {
120 		struct clock_ymdhms basedate;
121 
122 		/*
123 		 * If base is 0, assume filesystem time is just unknown
124 		 * instead of preposterous. Don't bark.
125 		 */
126 		if (base != 0)
127 			printf("WARNING: preposterous time in file system\n");
128 		/* not going to use it anyway, if the chip is readable */
129 		basedate.dt_year = 2006;
130 		basedate.dt_mon = 1;
131 		basedate.dt_day = 1;
132 		basedate.dt_hour = 12;
133 		basedate.dt_min = 0;
134 		basedate.dt_sec = 0;
135 		base = clock_ymdhms_to_secs(&basedate);
136 		badbase = 1;
137 	}
138 
139 	/*
140 	 * Some ports need to be supplied base in order to fabricate a time_t.
141 	 */
142 	if (todr_handle)
143 		todr_handle->base_time = base;
144 
145 	if ((todr_handle == NULL) ||
146 	    (todr_gettime(todr_handle, &tv) != 0) ||
147 	    (tv.tv_sec < (25 * SECYR))) {
148 
149 		if (todr_handle != NULL)
150 			printf("WARNING: preposterous TOD clock time\n");
151 		else
152 			printf("WARNING: no TOD clock present\n");
153 		badrtc = 1;
154 	} else {
155 		int deltat = tv.tv_sec - base;
156 
157 		if (deltat < 0)
158 			deltat = -deltat;
159 
160 		if ((badbase == 0) && deltat >= 2 * SECDAY) {
161 
162 			if (tv.tv_sec < base) {
163 				/*
164 				 * The clock should never go backwards
165 				 * relative to filesystem time.  If it
166 				 * does by more than the threshold,
167 				 * believe the filesystem.
168 				 */
169 				printf("WARNING: clock lost %d days\n",
170 				    deltat / SECDAY);
171 				badrtc = 1;
172 			} else {
173 				aprint_verbose("WARNING: clock gained %d "
174 				    "days\n", deltat / SECDAY);
175 				goodtime = 1;
176 			}
177 		} else {
178 			goodtime = 1;
179 		}
180 	}
181 
182 	/* if the rtc time is bad, use the filesystem time */
183 	if (badrtc) {
184 		if (badbase) {
185 			printf("WARNING: using default initial time\n");
186 		} else {
187 			printf("WARNING: using filesystem time\n");
188 		}
189 		tv.tv_sec = base;
190 		tv.tv_usec = 0;
191 	}
192 
193 	timeset = 1;
194 
195 	ts.tv_sec = tv.tv_sec;
196 	ts.tv_nsec = tv.tv_usec * 1000;
197 	s = splclock();
198 	tc_setclock(&ts);
199 	splx(s);
200 
201 	if (waszero || goodtime)
202 		return;
203 
204 	printf("WARNING: CHECK AND RESET THE DATE!\n");
205 }
206 
207 /*
208  * Reset the TODR based on the time value; used when the TODR
209  * has a preposterous value and also when the time is reset
210  * by the stime system call.  Also called when the TODR goes past
211  * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
212  * to wrap the TODR around.
213  */
214 void
215 resettodr(void)
216 {
217 	struct timeval tv;
218 
219 	/*
220 	 * We might have been called by boot() due to a crash early
221 	 * on.  Don't reset the clock chip if we don't know what time
222 	 * it is.
223 	 */
224 	if (!timeset)
225 		return;
226 
227 	getmicrotime(&tv);
228 
229 	if (tv.tv_sec == 0)
230 		return;
231 
232 	if (todr_handle)
233 		if (todr_settime(todr_handle, &tv) != 0)
234 			printf("Cannot set TOD clock time\n");
235 }
236 
237 #ifdef	TODR_DEBUG
238 static void
239 todr_debug(const char *prefix, int rv, struct clock_ymdhms *dt,
240     volatile struct timeval *tvp)
241 {
242 	struct timeval tv_val;
243 	struct clock_ymdhms dt_val;
244 
245 	if (dt == NULL) {
246 		clock_secs_to_ymdhms(tvp->tv_sec, &dt_val);
247 		dt = &dt_val;
248 	}
249 	if (tvp == NULL) {
250 		tvp = &tv_val;
251 		tvp->tv_sec = clock_ymdhms_to_secs(dt);
252 		tvp->tv_usec = 0;
253 	}
254 	printf("%s: rv = %d\n", prefix, rv);
255 	printf("%s: rtc_offset = %d\n", prefix, rtc_offset);
256 	printf("%s: %4u/%02u/%02u %02u:%02u:%02u, (wday %d) (epoch %u.%06u)\n",
257 	    prefix,
258 	    dt->dt_year, dt->dt_mon, dt->dt_day,
259 	    dt->dt_hour, dt->dt_min, dt->dt_sec,
260 	    dt->dt_wday, (unsigned)tvp->tv_sec, (unsigned)tvp->tv_usec);
261 }
262 #else	/* !TODR_DEBUG */
263 #define	todr_debug(prefix, rv, dt, tvp)
264 #endif	/* TODR_DEBUG */
265 
266 
267 int
268 todr_gettime(todr_chip_handle_t tch, volatile struct timeval *tvp)
269 {
270 	struct clock_ymdhms	dt;
271 	int			rv;
272 
273 	if (tch->todr_gettime) {
274 		rv = tch->todr_gettime(tch, tvp);
275 		/*
276 		 * Some unconverted ports have their own references to
277 		 * rtc_offset.   A converted port must not do that.
278 		 */
279 		if (rv == 0)
280 			tvp->tv_sec += rtc_offset * 60;
281 		todr_debug("TODR-GET-SECS", rv, NULL, tvp);
282 		return rv;
283 	} else if (tch->todr_gettime_ymdhms) {
284 		rv = tch->todr_gettime_ymdhms(tch, &dt);
285 		todr_debug("TODR-GET-YMDHMS", rv, &dt, NULL);
286 		if (rv)
287 			return rv;
288 
289 		/*
290 		 * Simple sanity checks.  Note that this includes a
291 		 * value for clocks that can return a leap second.
292 		 * Note that we don't support double leap seconds,
293 		 * since this was apparently an error/misunderstanding
294 		 * on the part of the ISO C committee, and can never
295 		 * actually occur.  If your clock issues us a double
296 		 * leap second, it must be broken.  Ultimately, you'd
297 		 * have to be trying to read time at precisely that
298 		 * instant to even notice, so even broken clocks will
299 		 * work the vast majority of the time.  In such a case
300 		 * it is recommended correction be applied in the
301 		 * clock driver.
302 		 */
303 		if (dt.dt_mon < 1 || dt.dt_mon > 12 ||
304 		    dt.dt_day < 1 || dt.dt_day > 31 ||
305 		    dt.dt_hour > 23 || dt.dt_min > 59 || dt.dt_sec > 60) {
306 			return EINVAL;
307 		}
308 		tvp->tv_sec = clock_ymdhms_to_secs(&dt) + rtc_offset * 60;
309 		tvp->tv_usec = 0;
310 		return tvp->tv_sec < 0 ? EINVAL : 0;
311 	}
312 
313 	return ENXIO;
314 }
315 
316 int
317 todr_settime(todr_chip_handle_t tch, volatile struct timeval *tvp)
318 {
319 	struct clock_ymdhms	dt;
320 	int			rv;
321 
322 	if (tch->todr_settime) {
323 		/* See comments above in gettime why this is ifdef'd */
324 		struct timeval	copy = *tvp;
325 		copy.tv_sec -= rtc_offset * 60;
326 		rv = tch->todr_settime(tch, &copy);
327 		todr_debug("TODR-SET-SECS", rv, NULL, tvp);
328 		return rv;
329 	} else if (tch->todr_settime_ymdhms) {
330 		time_t	sec = tvp->tv_sec - rtc_offset * 60;
331 		if (tvp->tv_usec >= 500000)
332 			sec++;
333 		clock_secs_to_ymdhms(sec, &dt);
334 		rv = tch->todr_settime_ymdhms(tch, &dt);
335 		todr_debug("TODR-SET-YMDHMS", rv, &dt, NULL);
336 		return rv;
337 	} else {
338 		return ENXIO;
339 	}
340 }
341