kern_todr.c revision 1.1 1 1.1 gdamore /* $NetBSD: kern_todr.c,v 1.1 2006/09/02 20:18:00 gdamore Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*
4 1.1 gdamore * Copyright (c) 1992, 1993
5 1.1 gdamore * The Regents of the University of California. All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * This code is derived from software contributed to Berkeley by
8 1.1 gdamore * the Systems Programming Group of the University of Utah Computer
9 1.1 gdamore * Science Department and Ralph Campbell.
10 1.1 gdamore *
11 1.1 gdamore * Redistribution and use in source and binary forms, with or without
12 1.1 gdamore * modification, are permitted provided that the following conditions
13 1.1 gdamore * are met:
14 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
15 1.1 gdamore * notice, this list of conditions and the following disclaimer.
16 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
18 1.1 gdamore * documentation and/or other materials provided with the distribution.
19 1.1 gdamore * 3. Neither the name of the University nor the names of its contributors
20 1.1 gdamore * may be used to endorse or promote products derived from this software
21 1.1 gdamore * without specific prior written permission.
22 1.1 gdamore *
23 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 gdamore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 gdamore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 gdamore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 gdamore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 gdamore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 gdamore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 gdamore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 gdamore * SUCH DAMAGE.
34 1.1 gdamore *
35 1.1 gdamore * from: Utah Hdr: clock.c 1.18 91/01/21
36 1.1 gdamore *
37 1.1 gdamore * @(#)clock.c 8.1 (Berkeley) 6/10/93
38 1.1 gdamore */
39 1.1 gdamore /*
40 1.1 gdamore * Copyright (c) 1988 University of Utah.
41 1.1 gdamore *
42 1.1 gdamore * This code is derived from software contributed to Berkeley by
43 1.1 gdamore * the Systems Programming Group of the University of Utah Computer
44 1.1 gdamore * Science Department and Ralph Campbell.
45 1.1 gdamore *
46 1.1 gdamore * Redistribution and use in source and binary forms, with or without
47 1.1 gdamore * modification, are permitted provided that the following conditions
48 1.1 gdamore * are met:
49 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
50 1.1 gdamore * notice, this list of conditions and the following disclaimer.
51 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
52 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
53 1.1 gdamore * documentation and/or other materials provided with the distribution.
54 1.1 gdamore * 3. All advertising materials mentioning features or use of this software
55 1.1 gdamore * must display the following acknowledgement:
56 1.1 gdamore * This product includes software developed by the University of
57 1.1 gdamore * California, Berkeley and its contributors.
58 1.1 gdamore * 4. Neither the name of the University nor the names of its contributors
59 1.1 gdamore * may be used to endorse or promote products derived from this software
60 1.1 gdamore * without specific prior written permission.
61 1.1 gdamore *
62 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 1.1 gdamore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 1.1 gdamore * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 1.1 gdamore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 1.1 gdamore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 1.1 gdamore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 1.1 gdamore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 1.1 gdamore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 1.1 gdamore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 1.1 gdamore * SUCH DAMAGE.
73 1.1 gdamore *
74 1.1 gdamore * from: Utah Hdr: clock.c 1.18 91/01/21
75 1.1 gdamore *
76 1.1 gdamore * @(#)clock.c 8.1 (Berkeley) 6/10/93
77 1.1 gdamore */
78 1.1 gdamore #include <sys/cdefs.h>
79 1.1 gdamore __KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.1 2006/09/02 20:18:00 gdamore Exp $");
80 1.1 gdamore
81 1.1 gdamore #include <sys/param.h>
82 1.1 gdamore
83 1.1 gdamore #ifdef __HAVE_GENERIC_TODR
84 1.1 gdamore
85 1.1 gdamore #include <sys/kernel.h>
86 1.1 gdamore #include <sys/systm.h>
87 1.1 gdamore #include <sys/device.h>
88 1.1 gdamore #include <sys/timetc.h>
89 1.1 gdamore #include <dev/clock_subr.h> /* hmm.. this should probably move to sys */
90 1.1 gdamore
91 1.1 gdamore static todr_chip_handle_t todr_handle = NULL;
92 1.1 gdamore
93 1.1 gdamore /*
94 1.1 gdamore * Attach the clock device to todr_handle.
95 1.1 gdamore */
96 1.1 gdamore void
97 1.1 gdamore todr_attach(todr_chip_handle_t todr)
98 1.1 gdamore {
99 1.1 gdamore
100 1.1 gdamore if (todr_handle) {
101 1.1 gdamore printf("todr_attach: TOD already configured\n");
102 1.1 gdamore return;
103 1.1 gdamore }
104 1.1 gdamore todr_handle = todr;
105 1.1 gdamore }
106 1.1 gdamore
107 1.1 gdamore /*
108 1.1 gdamore * Set up the system's time, given a `reasonable' time value.
109 1.1 gdamore */
110 1.1 gdamore void
111 1.1 gdamore inittodr(time_t base)
112 1.1 gdamore {
113 1.1 gdamore int badbase = 0, waszero = base == 0;
114 1.1 gdamore #ifdef __HAVE_TIMECOUNTER
115 1.1 gdamore struct timeval time;
116 1.1 gdamore struct timespec ts;
117 1.1 gdamore #endif
118 1.1 gdamore
119 1.1 gdamore if (base < 5 * SECYR) {
120 1.1 gdamore /*
121 1.1 gdamore * If base is 0, assume filesystem time is just unknown
122 1.1 gdamore * in stead of preposterous. Don't bark.
123 1.1 gdamore */
124 1.1 gdamore if (base != 0)
125 1.1 gdamore printf("WARNING: preposterous time in file system\n");
126 1.1 gdamore /* not going to use it anyway, if the chip is readable */
127 1.1 gdamore base = 21*SECYR + 186*SECDAY + SECDAY/2;
128 1.1 gdamore badbase = 1;
129 1.1 gdamore }
130 1.1 gdamore
131 1.1 gdamore if ((todr_handle == NULL) ||
132 1.1 gdamore (todr_gettime(todr_handle, &time) != 0) ||
133 1.1 gdamore (time.tv_sec == 0)) {
134 1.1 gdamore
135 1.1 gdamore if (todr_handle != NULL)
136 1.1 gdamore printf("WARNING: preposterous TOD clock time");
137 1.1 gdamore else
138 1.1 gdamore printf("WARNING: no TOD clock present");
139 1.1 gdamore
140 1.1 gdamore /*
141 1.1 gdamore * Believe the time in the file system for lack of
142 1.1 gdamore * anything better, resetting the clock.
143 1.1 gdamore */
144 1.1 gdamore time.tv_sec = base;
145 1.1 gdamore #ifdef __HAVE_TIMECOUNTER
146 1.1 gdamore ts.tv_sec = base;
147 1.1 gdamore ts.tv_nsec = 0;
148 1.1 gdamore tc_setclock(&ts);
149 1.1 gdamore #endif
150 1.1 gdamore if (!badbase)
151 1.1 gdamore resettodr();
152 1.1 gdamore } else {
153 1.1 gdamore int deltat = time.tv_sec - base;
154 1.1 gdamore
155 1.1 gdamore #ifdef __HAVE_TIMECOUNTER
156 1.1 gdamore ts.tv_sec = time.tv_sec;
157 1.1 gdamore ts.tv_nsec = time.tv_usec * 1000;
158 1.1 gdamore tc_setclock(&ts);
159 1.1 gdamore #endif
160 1.1 gdamore
161 1.1 gdamore if (deltat < 0)
162 1.1 gdamore deltat = -deltat;
163 1.1 gdamore if (waszero || deltat < 2 * SECDAY)
164 1.1 gdamore return;
165 1.1 gdamore printf("WARNING: clock %s %d days",
166 1.1 gdamore time.tv_sec < base ? "lost" : "gained", deltat / SECDAY);
167 1.1 gdamore }
168 1.1 gdamore printf(" -- CHECK AND RESET THE DATE!\n");
169 1.1 gdamore }
170 1.1 gdamore
171 1.1 gdamore /*
172 1.1 gdamore * Reset the TODR based on the time value; used when the TODR
173 1.1 gdamore * has a preposterous value and also when the time is reset
174 1.1 gdamore * by the stime system call. Also called when the TODR goes past
175 1.1 gdamore * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
176 1.1 gdamore * to wrap the TODR around.
177 1.1 gdamore */
178 1.1 gdamore void
179 1.1 gdamore resettodr(void)
180 1.1 gdamore {
181 1.1 gdamore #ifdef __HAVE_TIMECOUNTER
182 1.1 gdamore struct timeval time;
183 1.1 gdamore
184 1.1 gdamore getmicrotime(&time);
185 1.1 gdamore #endif
186 1.1 gdamore
187 1.1 gdamore if (time.tv_sec == 0)
188 1.1 gdamore return;
189 1.1 gdamore
190 1.1 gdamore if (todr_handle)
191 1.1 gdamore if (todr_settime(todr_handle, &time) != 0)
192 1.1 gdamore printf("Cannot set TOD clock time\n");
193 1.1 gdamore }
194 1.1 gdamore
195 1.1 gdamore #endif /* __HAVE_GENERIC_TODR */
196