Home | History | Annotate | Line # | Download | only in kern
kern_todr.c revision 1.25.6.1
      1 /*	$NetBSD: kern_todr.c,v 1.25.6.1 2008/01/02 21:56:04 bouyer 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.25.6.1 2008/01/02 21:56:04 bouyer Exp $");
     80 
     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>
     86 #include <sys/intr.h>
     87 
     88 #include <dev/clock_subr.h>	/* hmm.. this should probably move to sys */
     89 
     90 #ifdef	__HAVE_GENERIC_TODR
     91 
     92 static todr_chip_handle_t todr_handle = NULL;
     93 
     94 /*
     95  * Attach the clock device to todr_handle.
     96  */
     97 void
     98 todr_attach(todr_chip_handle_t todr)
     99 {
    100 
    101 	if (todr_handle) {
    102 		printf("todr_attach: TOD already configured\n");
    103 		return;
    104 	}
    105 	todr_handle = todr;
    106 }
    107 
    108 static int timeset = 0;
    109 
    110 /*
    111  * Set up the system's time, given a `reasonable' time value.
    112  */
    113 void
    114 inittodr(time_t base)
    115 {
    116 	int badbase = 0, waszero = (base == 0), goodtime = 0, badrtc = 0;
    117 	int s;
    118 	struct timespec ts;
    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 	if (todr_handle)
    145 		todr_handle->base_time = base;
    146 
    147 	if ((todr_handle == NULL) ||
    148 	    (todr_gettime(todr_handle, &tv) != 0) ||
    149 	    (tv.tv_sec < (25 * 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 			} else {
    175 				printf("WARNING: clock gained %d days\n",
    176 				    deltat / SECDAY);
    177 			}
    178 		} else {
    179 			goodtime = 1;
    180 		}
    181 	}
    182 
    183 	/* if the rtc time is bad, use the filesystem time */
    184 	if (badrtc) {
    185 		if (badbase) {
    186 			printf("WARNING: using default initial time\n");
    187 		} else {
    188 			printf("WARNING: using filesystem time\n");
    189 		}
    190 		tv.tv_sec = base;
    191 		tv.tv_usec = 0;
    192 	}
    193 
    194 	timeset = 1;
    195 
    196 	ts.tv_sec = tv.tv_sec;
    197 	ts.tv_nsec = tv.tv_usec * 1000;
    198 	s = splclock();
    199 	tc_setclock(&ts);
    200 	splx(s);
    201 
    202 	if (waszero || goodtime)
    203 		return;
    204 
    205 	printf("WARNING: CHECK AND RESET THE DATE!\n");
    206 }
    207 
    208 /*
    209  * Reset the TODR based on the time value; used when the TODR
    210  * has a preposterous value and also when the time is reset
    211  * by the stime system call.  Also called when the TODR goes past
    212  * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
    213  * to wrap the TODR around.
    214  */
    215 void
    216 resettodr(void)
    217 {
    218 	struct timeval tv;
    219 
    220 	/*
    221 	 * We might have been called by boot() due to a crash early
    222 	 * on.  Don't reset the clock chip if we don't know what time
    223 	 * it is.
    224 	 */
    225 	if (!timeset)
    226 		return;
    227 
    228 	getmicrotime(&tv);
    229 
    230 	if (tv.tv_sec == 0)
    231 		return;
    232 
    233 	if (todr_handle)
    234 		if (todr_settime(todr_handle, &tv) != 0)
    235 			printf("Cannot set TOD clock time\n");
    236 }
    237 
    238 #endif	/* __HAVE_GENERIC_TODR */
    239 
    240 #ifdef	TODR_DEBUG
    241 static void
    242 todr_debug(const char *prefix, int rv, struct clock_ymdhms *dt,
    243     volatile struct timeval *tvp)
    244 {
    245 	struct timeval tv_val;
    246 	struct clock_ymdhms dt_val;
    247 
    248 	if (dt == NULL) {
    249 		clock_secs_to_ymdhms(tvp->tv_sec, &dt_val);
    250 		dt = &dt_val;
    251 	}
    252 	if (tvp == NULL) {
    253 		tvp = &tv_val;
    254 		tvp->tv_sec = clock_ymdhms_to_secs(dt);
    255 		tvp->tv_usec = 0;
    256 	}
    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",
    260 	    prefix,
    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);
    264 }
    265 #else	/* !TODR_DEBUG */
    266 #define	todr_debug(prefix, rv, dt, tvp)
    267 #endif	/* TODR_DEBUG */
    268 
    269 
    270 int
    271 todr_gettime(todr_chip_handle_t tch, volatile struct timeval *tvp)
    272 {
    273 	struct clock_ymdhms	dt;
    274 	int			rv;
    275 
    276 	if (tch->todr_gettime) {
    277 		rv = tch->todr_gettime(tch, tvp);
    278 		/*
    279 		 * Some unconverted ports have their own references to
    280 		 * rtc_offset.   A converted port must not do that.
    281 		 */
    282 #ifdef	__HAVE_GENERIC_TODR
    283 		if (rv == 0)
    284 			tvp->tv_sec += rtc_offset * 60;
    285 #endif
    286 		todr_debug("TODR-GET-SECS", rv, NULL, tvp);
    287 		return rv;
    288 	} else if (tch->todr_gettime_ymdhms) {
    289 		rv = tch->todr_gettime_ymdhms(tch, &dt);
    290 		todr_debug("TODR-GET-YMDHMS", rv, &dt, NULL);
    291 		if (rv)
    292 			return rv;
    293 
    294 		/*
    295 		 * Formerly we had code here that explicitly checked
    296 		 * for 2038 year rollover.
    297 		 *
    298 		 * However, clock_ymdhms_to_secs performs the same
    299 		 * check for us, so we need not worry about it.  Note
    300 		 * that this assumes that the tvp->tv_sec member is
    301 		 * a time_t.
    302 		 */
    303 
    304 		/*
    305 		 * Simple sanity checks.  Note that this includes a
    306 		 * value for clocks that can return a leap second.
    307 		 * Note that we don't support double leap seconds,
    308 		 * since this was apparently an error/misunderstanding
    309 		 * on the part of the ISO C committee, and can never
    310 		 * actually occur.  If your clock issues us a double
    311 		 * leap second, it must be broken.  Ultimately, you'd
    312 		 * have to be trying to read time at precisely that
    313 		 * instant to even notice, so even broken clocks will
    314 		 * work the vast majority of the time.  In such a case
    315 		 * it is recommended correction be applied in the
    316 		 * clock driver.
    317 		 */
    318 		if (dt.dt_mon < 1 || dt.dt_mon > 12 ||
    319 		    dt.dt_day < 1 || dt.dt_day > 31 ||
    320 		    dt.dt_hour > 23 || dt.dt_min > 59 || dt.dt_sec > 60) {
    321 			return EINVAL;
    322 		}
    323 		tvp->tv_sec = clock_ymdhms_to_secs(&dt) + rtc_offset * 60;
    324 		tvp->tv_usec = 0;
    325 		return tvp->tv_sec < 0 ? EINVAL : 0;
    326 	}
    327 
    328 	return ENXIO;
    329 }
    330 
    331 int
    332 todr_settime(todr_chip_handle_t tch, volatile struct timeval *tvp)
    333 {
    334 	struct clock_ymdhms	dt;
    335 	int			rv;
    336 
    337 	if (tch->todr_settime) {
    338 		/* See comments above in gettime why this is ifdef'd */
    339 #ifdef	__HAVE_GENERIC_TODR
    340 		struct timeval	copy = *tvp;
    341 		copy.tv_sec -= rtc_offset * 60;
    342 		rv = tch->todr_settime(tch, &copy);
    343 #else
    344 		rv = tch->todr_settime(tch, tvp);
    345 #endif
    346 		todr_debug("TODR-SET-SECS", rv, NULL, tvp);
    347 		return rv;
    348 	} else if (tch->todr_settime_ymdhms) {
    349 		time_t	sec = tvp->tv_sec - rtc_offset * 60;
    350 		if (tvp->tv_usec >= 500000)
    351 			sec++;
    352 		clock_secs_to_ymdhms(sec, &dt);
    353 		rv = tch->todr_settime_ymdhms(tch, &dt);
    354 		todr_debug("TODR-SET-YMDHMS", rv, &dt, NULL);
    355 		return rv;
    356 	} else {
    357 		return ENXIO;
    358 	}
    359 }
    360