kern_todr.c revision 1.6 1 /* $NetBSD: kern_todr.c,v 1.6 2006/09/07 00:10:49 gdamore 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.6 2006/09/07 00:10:49 gdamore Exp $");
80
81 #include <sys/param.h>
82
83 #ifdef __HAVE_GENERIC_TODR
84
85 #include <sys/kernel.h>
86 #include <sys/systm.h>
87 #include <sys/device.h>
88 #include <sys/timetc.h>
89 #include <dev/clock_subr.h> /* hmm.. this should probably move to sys */
90
91 static todr_chip_handle_t todr_handle = NULL;
92
93 /*
94 * Attach the clock device to todr_handle.
95 */
96 void
97 todr_attach(todr_chip_handle_t todr)
98 {
99
100 if (todr_handle) {
101 printf("todr_attach: TOD already configured\n");
102 return;
103 }
104 todr_handle = todr;
105 }
106
107 static int timeset = 0;
108
109 /*
110 * Set up the system's time, given a `reasonable' time value.
111 */
112 void
113 inittodr(time_t base)
114 {
115 int badbase = 0, waszero = (base == 0), goodtime = 0, badrtc = 0;
116 #ifdef __HAVE_TIMECOUNTER
117 struct timespec ts;
118 #endif
119 struct timeval tv;
120
121 if (base < 5 * SECYR) {
122 struct clock_ymdhms basedate;
123
124 /*
125 * If base is 0, assume filesystem time is just unknown
126 * instead of preposterous. Don't bark.
127 */
128 if (base != 0)
129 printf("WARNING: preposterous time in file system\n");
130 /* not going to use it anyway, if the chip is readable */
131 basedate.dt_year = 2006;
132 basedate.dt_mon = 1;
133 basedate.dt_day = 1;
134 basedate.dt_hour = 12;
135 basedate.dt_min = 0;
136 basedate.dt_sec = 0;
137 base = clock_ymdhms_to_secs(&basedate);
138 badbase = 1;
139 }
140
141 /*
142 * Some ports need to be supplied base in order to fabricate a time_t.
143 */
144 tv.tv_sec = base;
145 tv.tv_usec = 0;
146
147 if ((todr_handle == NULL) ||
148 (todr_gettime(todr_handle, &tv) != 0) ||
149 (tv.tv_sec < (5 * SECYR))) {
150
151 if (todr_handle != NULL)
152 printf("WARNING: preposterous TOD clock time\n");
153 else
154 printf("WARNING: no TOD clock present\n");
155 badrtc = 1;
156 } else {
157 int deltat = tv.tv_sec - base;
158
159 if (deltat < 0)
160 deltat = -deltat;
161
162 if ((badbase == 0) && deltat >= 2 * SECDAY) {
163
164 if (tv.tv_sec < base) {
165 /*
166 * The clock should never go backwards
167 * relative to filesystem time. If it
168 * does by more than the threshold,
169 * believe the filesystem.
170 */
171 printf("WARNING: clock lost %d days\n",
172 deltat / SECDAY);
173 badrtc = 1;
174 }
175 else {
176 printf("WARNING: clock gained %d days\n",
177 deltat / SECDAY);
178 }
179 } else {
180 goodtime = 1;
181 }
182 }
183
184 /* if the rtc time is bad, use the filesystem time */
185 if (badrtc) {
186 if (badbase) {
187 printf("WARNING: using default initial time\n");
188 } else {
189 printf("WARNING: using filesystem time\n");
190 }
191 tv.tv_sec = base;
192 tv.tv_usec = 0;
193 }
194
195 timeset = 1;
196
197 #ifdef __HAVE_TIMECOUNTER
198 ts.tv_sec = tv.tv_sec;
199 ts.tv_nsec = tv.tv_usec * 1000;
200 tc_setclock(&ts);
201 #else
202 time = tv;
203 #endif
204
205 if (waszero || goodtime)
206 return;
207
208 printf("WARNING: CHECK AND RESET THE DATE!\n");
209 }
210
211 /*
212 * Reset the TODR based on the time value; used when the TODR
213 * has a preposterous value and also when the time is reset
214 * by the stime system call. Also called when the TODR goes past
215 * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
216 * to wrap the TODR around.
217 */
218 void
219 resettodr(void)
220 {
221 #ifdef __HAVE_TIMECOUNTER
222 struct timeval time;
223 #endif
224
225 /*
226 * We might have been called by boot() due to a crash early
227 * on. Don't reset the clock chip if we don't know what time
228 * it is.
229 */
230 if (!timeset)
231 return;
232
233 #ifdef __HAVE_TIMECOUNTER
234 getmicrotime(&time);
235 #endif
236
237 if (time.tv_sec == 0)
238 return;
239
240 if (todr_handle)
241 if (todr_settime(todr_handle, &time) != 0)
242 printf("Cannot set TOD clock time\n");
243 }
244
245 #endif /* __HAVE_GENERIC_TODR */
246
247
248 int
249 todr_gettime(todr_chip_handle_t tch, struct timeval *tvp)
250 {
251 struct clock_ymdhms dt;
252 int rv;
253
254 if (tch->todr_gettime)
255 return tch->todr_gettime(tch, tvp);
256
257 if (tch->todr_gettime_ymdhms) {
258 if ((rv = tch->todr_gettime_ymdhms(tch, &dt)) != 0)
259 return rv;
260 /*
261 * If tv_sec is 32 bits, then the "End of Time" is Mon
262 * Jan 18 22:14:07 2038 (US/Eastern) This code copes
263 * with RTC's past the end of time if tv_sec is an
264 * int32 or less. Needed because sometimes RTCs screw
265 * up or are badly set, and that would cause the time
266 * to go negative in the calculation below, which
267 * causes Very Bad Mojo. This at least lets the user
268 * boot and fix the problem. Note the code is self
269 * eliminating once tv_sec goes to 64 bits.
270 */
271 if ((sizeof (tvp->tv_sec) <= sizeof (int32_t)) &&
272 (dt.dt_year >= 2038)) {
273 return -1;
274 }
275
276 tvp->tv_sec = clock_ymdhms_to_secs(&dt) + rtc_offset * 60;
277 tvp->tv_usec = 0;
278 return 0;
279 }
280
281 return -1;
282 }
283
284 int
285 todr_settime(todr_chip_handle_t tch, struct timeval *tvp)
286 {
287 struct clock_ymdhms dt;
288
289 if (tch->todr_settime)
290 return tch->todr_settime(tch, tvp);
291
292 if (tch->todr_settime_ymdhms) {
293 clock_secs_to_ymdhms(tvp->tv_sec - rtc_offset * 60, &dt);
294 return tch->todr_settime_ymdhms(tch, &dt);
295 }
296
297 return -1;
298 }
299