kern_todr.c revision 1.1 1 /* $NetBSD: kern_todr.c,v 1.1 2006/09/02 20:18:00 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.1 2006/09/02 20:18:00 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 /*
108 * Set up the system's time, given a `reasonable' time value.
109 */
110 void
111 inittodr(time_t base)
112 {
113 int badbase = 0, waszero = base == 0;
114 #ifdef __HAVE_TIMECOUNTER
115 struct timeval time;
116 struct timespec ts;
117 #endif
118
119 if (base < 5 * SECYR) {
120 /*
121 * If base is 0, assume filesystem time is just unknown
122 * in stead of preposterous. Don't bark.
123 */
124 if (base != 0)
125 printf("WARNING: preposterous time in file system\n");
126 /* not going to use it anyway, if the chip is readable */
127 base = 21*SECYR + 186*SECDAY + SECDAY/2;
128 badbase = 1;
129 }
130
131 if ((todr_handle == NULL) ||
132 (todr_gettime(todr_handle, &time) != 0) ||
133 (time.tv_sec == 0)) {
134
135 if (todr_handle != NULL)
136 printf("WARNING: preposterous TOD clock time");
137 else
138 printf("WARNING: no TOD clock present");
139
140 /*
141 * Believe the time in the file system for lack of
142 * anything better, resetting the clock.
143 */
144 time.tv_sec = base;
145 #ifdef __HAVE_TIMECOUNTER
146 ts.tv_sec = base;
147 ts.tv_nsec = 0;
148 tc_setclock(&ts);
149 #endif
150 if (!badbase)
151 resettodr();
152 } else {
153 int deltat = time.tv_sec - base;
154
155 #ifdef __HAVE_TIMECOUNTER
156 ts.tv_sec = time.tv_sec;
157 ts.tv_nsec = time.tv_usec * 1000;
158 tc_setclock(&ts);
159 #endif
160
161 if (deltat < 0)
162 deltat = -deltat;
163 if (waszero || deltat < 2 * SECDAY)
164 return;
165 printf("WARNING: clock %s %d days",
166 time.tv_sec < base ? "lost" : "gained", deltat / SECDAY);
167 }
168 printf(" -- CHECK AND RESET THE DATE!\n");
169 }
170
171 /*
172 * Reset the TODR based on the time value; used when the TODR
173 * has a preposterous value and also when the time is reset
174 * by the stime system call. Also called when the TODR goes past
175 * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
176 * to wrap the TODR around.
177 */
178 void
179 resettodr(void)
180 {
181 #ifdef __HAVE_TIMECOUNTER
182 struct timeval time;
183
184 getmicrotime(&time);
185 #endif
186
187 if (time.tv_sec == 0)
188 return;
189
190 if (todr_handle)
191 if (todr_settime(todr_handle, &time) != 0)
192 printf("Cannot set TOD clock time\n");
193 }
194
195 #endif /* __HAVE_GENERIC_TODR */
196