kern_todr.c revision 1.25 1 /* $NetBSD: kern_todr.c,v 1.25 2007/11/29 18:04:46 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.25 2007/11/29 18:04:46 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 #ifdef __HAVE_GENERIC_TODR
91
92 static todr_chip_handle_t todr_handle = NULL;
93
94 /*
95 * Attach the clock device to todr_handle.
96 */
97 void
98 todr_attach(todr_chip_handle_t todr)
99 {
100
101 if (todr_handle) {
102 printf("todr_attach: TOD already configured\n");
103 return;
104 }
105 todr_handle = todr;
106 }
107
108 static int timeset = 0;
109
110 /*
111 * Set up the system's time, given a `reasonable' time value.
112 */
113 void
114 inittodr(time_t base)
115 {
116 int badbase = 0, waszero = (base == 0), goodtime = 0, badrtc = 0;
117 int s;
118 #ifdef __HAVE_TIMECOUNTER
119 struct timespec ts;
120 #endif
121 struct timeval tv;
122
123 if (base < 5 * SECYR) {
124 struct clock_ymdhms basedate;
125
126 /*
127 * If base is 0, assume filesystem time is just unknown
128 * instead of preposterous. Don't bark.
129 */
130 if (base != 0)
131 printf("WARNING: preposterous time in file system\n");
132 /* not going to use it anyway, if the chip is readable */
133 basedate.dt_year = 2006;
134 basedate.dt_mon = 1;
135 basedate.dt_day = 1;
136 basedate.dt_hour = 12;
137 basedate.dt_min = 0;
138 basedate.dt_sec = 0;
139 base = clock_ymdhms_to_secs(&basedate);
140 badbase = 1;
141 }
142
143 /*
144 * Some ports need to be supplied base in order to fabricate a time_t.
145 */
146 if (todr_handle)
147 todr_handle->base_time = base;
148
149 if ((todr_handle == NULL) ||
150 (todr_gettime(todr_handle, &tv) != 0) ||
151 (tv.tv_sec < (25 * SECYR))) {
152
153 if (todr_handle != NULL)
154 printf("WARNING: preposterous TOD clock time\n");
155 else
156 printf("WARNING: no TOD clock present\n");
157 badrtc = 1;
158 } else {
159 int deltat = tv.tv_sec - base;
160
161 if (deltat < 0)
162 deltat = -deltat;
163
164 if ((badbase == 0) && deltat >= 2 * SECDAY) {
165
166 if (tv.tv_sec < base) {
167 /*
168 * The clock should never go backwards
169 * relative to filesystem time. If it
170 * does by more than the threshold,
171 * believe the filesystem.
172 */
173 printf("WARNING: clock lost %d days\n",
174 deltat / SECDAY);
175 badrtc = 1;
176 } else {
177 printf("WARNING: clock gained %d days\n",
178 deltat / SECDAY);
179 }
180 } else {
181 goodtime = 1;
182 }
183 }
184
185 /* if the rtc time is bad, use the filesystem time */
186 if (badrtc) {
187 if (badbase) {
188 printf("WARNING: using default initial time\n");
189 } else {
190 printf("WARNING: using filesystem time\n");
191 }
192 tv.tv_sec = base;
193 tv.tv_usec = 0;
194 }
195
196 timeset = 1;
197
198 s = splclock();
199 #ifdef __HAVE_TIMECOUNTER
200 ts.tv_sec = tv.tv_sec;
201 ts.tv_nsec = tv.tv_usec * 1000;
202 tc_setclock(&ts);
203 #else
204 time = tv;
205 #endif
206 splx(s);
207
208 if (waszero || goodtime)
209 return;
210
211 printf("WARNING: CHECK AND RESET THE DATE!\n");
212 }
213
214 /*
215 * Reset the TODR based on the time value; used when the TODR
216 * has a preposterous value and also when the time is reset
217 * by the stime system call. Also called when the TODR goes past
218 * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
219 * to wrap the TODR around.
220 */
221 void
222 resettodr(void)
223 {
224 #ifdef __HAVE_TIMECOUNTER
225 struct timeval time;
226 #endif
227
228 /*
229 * We might have been called by boot() due to a crash early
230 * on. Don't reset the clock chip if we don't know what time
231 * it is.
232 */
233 if (!timeset)
234 return;
235
236 #ifdef __HAVE_TIMECOUNTER
237 getmicrotime(&time);
238 #endif
239
240 if (time.tv_sec == 0)
241 return;
242
243 if (todr_handle)
244 if (todr_settime(todr_handle, &time) != 0)
245 printf("Cannot set TOD clock time\n");
246 }
247
248 #endif /* __HAVE_GENERIC_TODR */
249
250 #ifdef TODR_DEBUG
251 static void
252 todr_debug(const char *prefix, int rv, struct clock_ymdhms *dt,
253 volatile struct timeval *tvp)
254 {
255 struct timeval tv_val;
256 struct clock_ymdhms dt_val;
257
258 if (dt == NULL) {
259 clock_secs_to_ymdhms(tvp->tv_sec, &dt_val);
260 dt = &dt_val;
261 }
262 if (tvp == NULL) {
263 tvp = &tv_val;
264 tvp->tv_sec = clock_ymdhms_to_secs(dt);
265 tvp->tv_usec = 0;
266 }
267 printf("%s: rv = %d\n", prefix, rv);
268 printf("%s: rtc_offset = %d\n", prefix, rtc_offset);
269 printf("%s: %4u/%02u/%02u %02u:%02u:%02u, (wday %d) (epoch %u.%06u)\n",
270 prefix,
271 dt->dt_year, dt->dt_mon, dt->dt_day,
272 dt->dt_hour, dt->dt_min, dt->dt_sec,
273 dt->dt_wday, (unsigned)tvp->tv_sec, (unsigned)tvp->tv_usec);
274 }
275 #else /* !TODR_DEBUG */
276 #define todr_debug(prefix, rv, dt, tvp)
277 #endif /* TODR_DEBUG */
278
279
280 int
281 todr_gettime(todr_chip_handle_t tch, volatile struct timeval *tvp)
282 {
283 struct clock_ymdhms dt;
284 int rv;
285
286 if (tch->todr_gettime) {
287 rv = tch->todr_gettime(tch, tvp);
288 /*
289 * Some unconverted ports have their own references to
290 * rtc_offset. A converted port must not do that.
291 */
292 #ifdef __HAVE_GENERIC_TODR
293 if (rv == 0)
294 tvp->tv_sec += rtc_offset * 60;
295 #endif
296 todr_debug("TODR-GET-SECS", rv, NULL, tvp);
297 return rv;
298 } else if (tch->todr_gettime_ymdhms) {
299 rv = tch->todr_gettime_ymdhms(tch, &dt);
300 todr_debug("TODR-GET-YMDHMS", rv, &dt, NULL);
301 if (rv)
302 return rv;
303
304 /*
305 * Formerly we had code here that explicitly checked
306 * for 2038 year rollover.
307 *
308 * However, clock_ymdhms_to_secs performs the same
309 * check for us, so we need not worry about it. Note
310 * that this assumes that the tvp->tv_sec member is
311 * a time_t.
312 */
313
314 /*
315 * Simple sanity checks. Note that this includes a
316 * value for clocks that can return a leap second.
317 * Note that we don't support double leap seconds,
318 * since this was apparently an error/misunderstanding
319 * on the part of the ISO C committee, and can never
320 * actually occur. If your clock issues us a double
321 * leap second, it must be broken. Ultimately, you'd
322 * have to be trying to read time at precisely that
323 * instant to even notice, so even broken clocks will
324 * work the vast majority of the time. In such a case
325 * it is recommended correction be applied in the
326 * clock driver.
327 */
328 if (dt.dt_mon < 1 || dt.dt_mon > 12 ||
329 dt.dt_day < 1 || dt.dt_day > 31 ||
330 dt.dt_hour > 23 || dt.dt_min > 59 || dt.dt_sec > 60) {
331 return EINVAL;
332 }
333 tvp->tv_sec = clock_ymdhms_to_secs(&dt) + rtc_offset * 60;
334 tvp->tv_usec = 0;
335 return tvp->tv_sec < 0 ? EINVAL : 0;
336 }
337
338 return ENXIO;
339 }
340
341 int
342 todr_settime(todr_chip_handle_t tch, volatile struct timeval *tvp)
343 {
344 struct clock_ymdhms dt;
345 int rv;
346
347 if (tch->todr_settime) {
348 /* See comments above in gettime why this is ifdef'd */
349 #ifdef __HAVE_GENERIC_TODR
350 struct timeval copy = *tvp;
351 copy.tv_sec -= rtc_offset * 60;
352 rv = tch->todr_settime(tch, ©);
353 #else
354 rv = tch->todr_settime(tch, tvp);
355 #endif
356 todr_debug("TODR-SET-SECS", rv, NULL, tvp);
357 return rv;
358 } else if (tch->todr_settime_ymdhms) {
359 time_t sec = tvp->tv_sec - rtc_offset * 60;
360 if (tvp->tv_usec >= 500000)
361 sec++;
362 clock_secs_to_ymdhms(sec, &dt);
363 rv = tch->todr_settime_ymdhms(tch, &dt);
364 todr_debug("TODR-SET-YMDHMS", rv, &dt, NULL);
365 return rv;
366 } else {
367 return ENXIO;
368 }
369 }
370