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