Home | History | Annotate | Line # | Download | only in kern
kern_todr.c revision 1.11
      1 /*	$NetBSD: kern_todr.c,v 1.11 2006/09/07 07:26:07 dogcow 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.11 2006/09/07 07:26:07 dogcow 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 <dev/clock_subr.h>	/* hmm.. this should probably move to sys */
     87 
     88 #ifdef	__HAVE_GENERIC_TODR
     89 
     90 static todr_chip_handle_t todr_handle = NULL;
     91 
     92 /*
     93  * Attach the clock device to todr_handle.
     94  */
     95 void
     96 todr_attach(todr_chip_handle_t todr)
     97 {
     98 
     99         if (todr_handle) {
    100                 printf("todr_attach: TOD already configured\n");
    101 		return;
    102 	}
    103         todr_handle = todr;
    104 }
    105 
    106 static int timeset = 0;
    107 
    108 /*
    109  * Set up the system's time, given a `reasonable' time value.
    110  */
    111 void
    112 inittodr(time_t base)
    113 {
    114 	int badbase = 0, waszero = (base == 0), goodtime = 0, badrtc = 0;
    115 #ifdef	__HAVE_TIMECOUNTER
    116 	struct timespec ts;
    117 #endif
    118 	struct timeval tv;
    119 
    120 	if (base < 5 * SECYR) {
    121 		struct clock_ymdhms basedate;
    122 
    123 		/*
    124 		 * If base is 0, assume filesystem time is just unknown
    125 		 * instead of preposterous. Don't bark.
    126 		 */
    127 		if (base != 0)
    128 			printf("WARNING: preposterous time in file system\n");
    129 		/* not going to use it anyway, if the chip is readable */
    130 		basedate.dt_year = 2006;
    131 		basedate.dt_mon = 1;
    132 		basedate.dt_day = 1;
    133 		basedate.dt_hour = 12;
    134 		basedate.dt_min = 0;
    135 		basedate.dt_sec = 0;
    136 		base = clock_ymdhms_to_secs(&basedate);
    137 		badbase = 1;
    138 	}
    139 
    140 	/*
    141 	 * Some ports need to be supplied base in order to fabricate a time_t.
    142 	 */
    143 	tv.tv_sec = base;
    144 	tv.tv_usec = 0;
    145 
    146 	if ((todr_handle == NULL) ||
    147 	    (todr_gettime(todr_handle, &tv) != 0) ||
    148 	    (tv.tv_sec < (5 * SECYR))) {
    149 
    150 		if (todr_handle != NULL)
    151 			printf("WARNING: preposterous TOD clock time\n");
    152 		else
    153 			printf("WARNING: no TOD clock present\n");
    154 		badrtc = 1;
    155 	} else {
    156 		int deltat = tv.tv_sec - base;
    157 
    158 		if (deltat < 0)
    159 			deltat = -deltat;
    160 
    161 		if ((badbase == 0) && deltat >= 2 * SECDAY) {
    162 
    163 			if (tv.tv_sec < base) {
    164 				/*
    165 				 * The clock should never go backwards
    166 				 * relative to filesystem time.  If it
    167 				 * does by more than the threshold,
    168 				 * believe the filesystem.
    169 				 */
    170 				printf("WARNING: clock lost %d days\n",
    171 				    deltat / SECDAY);
    172 				badrtc = 1;
    173 			}
    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 #ifdef	__HAVE_TIMECOUNTER
    197 	ts.tv_sec = tv.tv_sec;
    198 	ts.tv_nsec = tv.tv_usec * 1000;
    199 	tc_setclock(&ts);
    200 #else
    201 	time = tv;
    202 #endif
    203 
    204 	if (waszero || goodtime)
    205 		return;
    206 
    207 	printf("WARNING: CHECK AND RESET THE DATE!\n");
    208 }
    209 
    210 /*
    211  * Reset the TODR based on the time value; used when the TODR
    212  * has a preposterous value and also when the time is reset
    213  * by the stime system call.  Also called when the TODR goes past
    214  * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
    215  * to wrap the TODR around.
    216  */
    217 void
    218 resettodr(void)
    219 {
    220 #ifdef	__HAVE_TIMECOUNTER
    221 	struct timeval	time;
    222 #endif
    223 
    224 	/*
    225 	 * We might have been called by boot() due to a crash early
    226 	 * on.  Don't reset the clock chip if we don't know what time
    227 	 * it is.
    228 	 */
    229 	if (!timeset)
    230 		return;
    231 
    232 #ifdef	__HAVE_TIMECOUNTER
    233 	getmicrotime(&time);
    234 #endif
    235 
    236 	if (time.tv_sec == 0)
    237 		return;
    238 
    239 	if (todr_handle)
    240 		if (todr_settime(todr_handle, &time) != 0)
    241 			printf("Cannot set TOD clock time\n");
    242 }
    243 
    244 #endif	/* __HAVE_GENERIC_TODR */
    245 
    246 
    247 int
    248 todr_gettime(todr_chip_handle_t tch, struct timeval *tvp)
    249 {
    250 	struct clock_ymdhms	dt;
    251 	int			rv;
    252 
    253 	if (tch->todr_gettime)
    254 		return tch->todr_gettime(tch, tvp);
    255 
    256 	if (tch->todr_gettime_ymdhms) {
    257 		if ((rv = tch->todr_gettime_ymdhms(tch, &dt)) != 0)
    258 			return rv;
    259 
    260 		/*
    261 		 * Formerly we had code here that explicitly checked
    262 		 * for 2038 year rollover.
    263 		 *
    264 		 * However, clock_ymdhms_to_secs performs the same
    265 		 * check for us, so we need not worry about it.  Note
    266 		 * that this assumes that the tvp->tv_sec member is is
    267 		 * a time_t.
    268 		 */
    269 
    270 		/* simple sanity checks */
    271 		if (dt.dt_mon > 12 || dt.dt_day > 31 ||
    272 		    dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60)
    273 			return -1;
    274 
    275 		tvp->tv_sec = clock_ymdhms_to_secs(&dt) + rtc_offset * 60;
    276 		tvp->tv_usec = 0;
    277 		return tvp->tv_sec < 0 ? -1 : 0;
    278 	}
    279 
    280 	return -1;
    281 }
    282 
    283 int
    284 todr_settime(todr_chip_handle_t tch, volatile struct timeval *tvp)
    285 {
    286 	struct clock_ymdhms	dt;
    287 
    288 	if (tch->todr_settime)
    289 		return tch->todr_settime(tch, tvp);
    290 
    291 	if (tch->todr_settime_ymdhms) {
    292 		time_t	sec = tvp->tv_sec - rtc_offset * 60;
    293 		if (tvp->tv_usec >= 500000)
    294 			sec++;
    295 		clock_secs_to_ymdhms(sec, &dt);
    296 		return tch->todr_settime_ymdhms(tch, &dt);
    297 	}
    298 
    299 	return -1;
    300 }
    301