1 /* $NetBSD: kern_todr.c,v 1.32 2009/12/12 15:17:23 tsutsui Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 * from: Utah Hdr: clock.c 1.18 91/01/21
37 * @(#)clock.c 8.1 (Berkeley) 6/10/93
40 * Copyright (c) 1988 University of Utah.
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.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
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.
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
74 * from: Utah Hdr: clock.c 1.18 91/01/21
76 * @(#)clock.c 8.1 (Berkeley) 6/10/93
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.32 2009/12/12 15:17:23 tsutsui Exp $");
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>
88 #include <dev/clock_subr.h> /* hmm.. this should probably move to sys */
90 static todr_chip_handle_t todr_handle
= NULL
;
93 * Attach the clock device to todr_handle.
96 todr_attach(todr_chip_handle_t todr
)
100 printf("todr_attach: TOD already configured\n");
106 static bool timeset
= false;
109 * Set up the system's time, given a `reasonable' time value.
112 inittodr(time_t base
)
114 bool badbase
= false;
115 bool waszero
= (base
== 0);
116 bool goodtime
= false;
122 if (base
< 5 * SECYR
) {
123 struct clock_ymdhms basedate
;
126 * If base is 0, assume filesystem time is just unknown
127 * instead of preposterous. Don't bark.
130 printf("WARNING: preposterous time in file system\n");
131 /* not going to use it anyway, if the chip is readable */
132 basedate
.dt_year
= 2010;
135 basedate
.dt_hour
= 12;
138 base
= clock_ymdhms_to_secs(&basedate
);
143 * Some ports need to be supplied base in order to fabricate a time_t.
146 todr_handle
->base_time
= base
;
148 if ((todr_handle
== NULL
) ||
149 (todr_gettime(todr_handle
, &tv
) != 0) ||
150 (tv
.tv_sec
< (25 * SECYR
))) {
152 if (todr_handle
!= NULL
)
153 printf("WARNING: preposterous TOD clock time\n");
155 printf("WARNING: no TOD clock present\n");
158 time_t deltat
= tv
.tv_sec
- base
;
163 if (!badbase
&& deltat
>= 2 * SECDAY
) {
165 if (tv
.tv_sec
< base
) {
167 * The clock should never go backwards
168 * relative to filesystem time. If it
169 * does by more than the threshold,
170 * believe the filesystem.
172 printf("WARNING: clock lost %" PRId64
" days\n",
176 aprint_verbose("WARNING: clock gained %" PRId64
177 " days\n", deltat
/ SECDAY
);
185 /* if the rtc time is bad, use the filesystem time */
188 printf("WARNING: using default initial time\n");
190 printf("WARNING: using filesystem time\n");
198 ts
.tv_sec
= tv
.tv_sec
;
199 ts
.tv_nsec
= tv
.tv_usec
* 1000;
204 if (waszero
|| goodtime
)
207 printf("WARNING: CHECK AND RESET THE DATE!\n");
211 * Reset the TODR based on the time value; used when the TODR
212 * has a preposterous value and also when the time is reset
213 * by the stime system call. Also called when the TODR goes past
214 * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
215 * to wrap the TODR around.
223 * We might have been called by boot() due to a crash early
224 * on. Don't reset the clock chip if we don't know what time
236 if (todr_settime(todr_handle
, &tv
) != 0)
237 printf("Cannot set TOD clock time\n");
242 todr_debug(const char *prefix
, int rv
, struct clock_ymdhms
*dt
,
245 struct timeval tv_val
;
246 struct clock_ymdhms dt_val
;
249 clock_secs_to_ymdhms(tvp
->tv_sec
, &dt_val
);
254 tvp
->tv_sec
= clock_ymdhms_to_secs(dt
);
257 printf("%s: rv = %d\n", prefix
, rv
);
258 printf("%s: rtc_offset = %d\n", prefix
, rtc_offset
);
259 printf("%s: %4u/%02u/%02u %02u:%02u:%02u, (wday %d) (epoch %u.%06u)\n",
261 dt
->dt_year
, dt
->dt_mon
, dt
->dt_day
,
262 dt
->dt_hour
, dt
->dt_min
, dt
->dt_sec
,
263 dt
->dt_wday
, (unsigned)tvp
->tv_sec
, (unsigned)tvp
->tv_usec
);
265 #else /* !TODR_DEBUG */
266 #define todr_debug(prefix, rv, dt, tvp)
267 #endif /* TODR_DEBUG */
271 todr_gettime(todr_chip_handle_t tch
, struct timeval
*tvp
)
273 struct clock_ymdhms dt
;
276 if (tch
->todr_gettime
) {
277 rv
= tch
->todr_gettime(tch
, tvp
);
279 * Some unconverted ports have their own references to
280 * rtc_offset. A converted port must not do that.
283 tvp
->tv_sec
+= rtc_offset
* 60;
284 todr_debug("TODR-GET-SECS", rv
, NULL
, tvp
);
286 } else if (tch
->todr_gettime_ymdhms
) {
287 rv
= tch
->todr_gettime_ymdhms(tch
, &dt
);
288 todr_debug("TODR-GET-YMDHMS", rv
, &dt
, NULL
);
293 * Simple sanity checks. Note that this includes a
294 * value for clocks that can return a leap second.
295 * Note that we don't support double leap seconds,
296 * since this was apparently an error/misunderstanding
297 * on the part of the ISO C committee, and can never
298 * actually occur. If your clock issues us a double
299 * leap second, it must be broken. Ultimately, you'd
300 * have to be trying to read time at precisely that
301 * instant to even notice, so even broken clocks will
302 * work the vast majority of the time. In such a case
303 * it is recommended correction be applied in the
306 if (dt
.dt_mon
< 1 || dt
.dt_mon
> 12 ||
307 dt
.dt_day
< 1 || dt
.dt_day
> 31 ||
308 dt
.dt_hour
> 23 || dt
.dt_min
> 59 || dt
.dt_sec
> 60) {
311 tvp
->tv_sec
= clock_ymdhms_to_secs(&dt
) + rtc_offset
* 60;
313 return tvp
->tv_sec
< 0 ? EINVAL
: 0;
320 todr_settime(todr_chip_handle_t tch
, struct timeval
*tvp
)
322 struct clock_ymdhms dt
;
325 if (tch
->todr_settime
) {
326 /* See comments above in gettime why this is ifdef'd */
327 struct timeval copy
= *tvp
;
328 copy
.tv_sec
-= rtc_offset
* 60;
329 rv
= tch
->todr_settime(tch
, ©
);
330 todr_debug("TODR-SET-SECS", rv
, NULL
, tvp
);
332 } else if (tch
->todr_settime_ymdhms
) {
333 time_t sec
= tvp
->tv_sec
- rtc_offset
* 60;
334 if (tvp
->tv_usec
>= 500000)
336 clock_secs_to_ymdhms(sec
, &dt
);
337 rv
= tch
->todr_settime_ymdhms(tch
, &dt
);
338 todr_debug("TODR-SET-YMDHMS", rv
, &dt
, NULL
);