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