Home | History | Annotate | Line # | Download | only in kern
kern_todr.c revision 1.22.2.1
      1  1.22.2.1     yamt /*	$NetBSD: kern_todr.c,v 1.22.2.1 2006/10/22 06:07:11 yamt 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.22.2.1     yamt __KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.22.2.1 2006/10/22 06:07:11 yamt Exp $");
     80       1.1  gdamore 
     81       1.1  gdamore #include <sys/param.h>
     82       1.1  gdamore #include <sys/kernel.h>
     83       1.1  gdamore #include <sys/systm.h>
     84       1.1  gdamore #include <sys/device.h>
     85       1.1  gdamore #include <sys/timetc.h>
     86       1.1  gdamore #include <dev/clock_subr.h>	/* hmm.. this should probably move to sys */
     87       1.1  gdamore 
     88      1.11   dogcow #ifdef	__HAVE_GENERIC_TODR
     89      1.11   dogcow 
     90       1.1  gdamore static todr_chip_handle_t todr_handle = NULL;
     91       1.1  gdamore 
     92       1.1  gdamore /*
     93       1.1  gdamore  * Attach the clock device to todr_handle.
     94       1.1  gdamore  */
     95       1.1  gdamore void
     96       1.1  gdamore todr_attach(todr_chip_handle_t todr)
     97       1.1  gdamore {
     98       1.1  gdamore 
     99  1.22.2.1     yamt 	if (todr_handle) {
    100  1.22.2.1     yamt 		printf("todr_attach: TOD already configured\n");
    101       1.1  gdamore 		return;
    102       1.1  gdamore 	}
    103  1.22.2.1     yamt 	todr_handle = todr;
    104       1.1  gdamore }
    105       1.1  gdamore 
    106       1.2  gdamore static int timeset = 0;
    107       1.2  gdamore 
    108       1.1  gdamore /*
    109       1.1  gdamore  * Set up the system's time, given a `reasonable' time value.
    110       1.1  gdamore  */
    111       1.1  gdamore void
    112       1.1  gdamore inittodr(time_t base)
    113       1.1  gdamore {
    114       1.5     matt 	int badbase = 0, waszero = (base == 0), goodtime = 0, badrtc = 0;
    115      1.12  gdamore 	int s;
    116       1.1  gdamore #ifdef	__HAVE_TIMECOUNTER
    117       1.1  gdamore 	struct timespec ts;
    118       1.1  gdamore #endif
    119       1.2  gdamore 	struct timeval tv;
    120       1.1  gdamore 
    121       1.1  gdamore 	if (base < 5 * SECYR) {
    122       1.4  gdamore 		struct clock_ymdhms basedate;
    123       1.4  gdamore 
    124       1.1  gdamore 		/*
    125       1.1  gdamore 		 * If base is 0, assume filesystem time is just unknown
    126       1.2  gdamore 		 * instead of preposterous. Don't bark.
    127       1.1  gdamore 		 */
    128       1.1  gdamore 		if (base != 0)
    129       1.1  gdamore 			printf("WARNING: preposterous time in file system\n");
    130       1.1  gdamore 		/* not going to use it anyway, if the chip is readable */
    131       1.4  gdamore 		basedate.dt_year = 2006;
    132       1.4  gdamore 		basedate.dt_mon = 1;
    133       1.4  gdamore 		basedate.dt_day = 1;
    134       1.4  gdamore 		basedate.dt_hour = 12;
    135       1.4  gdamore 		basedate.dt_min = 0;
    136       1.4  gdamore 		basedate.dt_sec = 0;
    137       1.4  gdamore 		base = clock_ymdhms_to_secs(&basedate);
    138       1.1  gdamore 		badbase = 1;
    139       1.1  gdamore 	}
    140       1.1  gdamore 
    141       1.5     matt 	/*
    142       1.5     matt 	 * Some ports need to be supplied base in order to fabricate a time_t.
    143       1.5     matt 	 */
    144      1.22  gdamore 	if (todr_handle)
    145      1.22  gdamore 		todr_handle->base_time = base;
    146       1.5     matt 
    147       1.1  gdamore 	if ((todr_handle == NULL) ||
    148       1.2  gdamore 	    (todr_gettime(todr_handle, &tv) != 0) ||
    149      1.17  gdamore 	    (tv.tv_sec < (25 * SECYR))) {
    150       1.1  gdamore 
    151  1.22.2.1     yamt 		if (todr_handle != NULL)
    152       1.2  gdamore 			printf("WARNING: preposterous TOD clock time\n");
    153       1.1  gdamore 		else
    154       1.2  gdamore 			printf("WARNING: no TOD clock present\n");
    155       1.2  gdamore 		badrtc = 1;
    156       1.1  gdamore 	} else {
    157       1.2  gdamore 		int deltat = tv.tv_sec - base;
    158       1.1  gdamore 
    159       1.1  gdamore 		if (deltat < 0)
    160       1.1  gdamore 			deltat = -deltat;
    161       1.2  gdamore 
    162       1.3  gdamore 		if ((badbase == 0) && deltat >= 2 * SECDAY) {
    163       1.2  gdamore 
    164       1.2  gdamore 			if (tv.tv_sec < base) {
    165       1.2  gdamore 				/*
    166       1.2  gdamore 				 * The clock should never go backwards
    167       1.2  gdamore 				 * relative to filesystem time.  If it
    168       1.2  gdamore 				 * does by more than the threshold,
    169       1.2  gdamore 				 * believe the filesystem.
    170       1.2  gdamore 				 */
    171       1.2  gdamore 				printf("WARNING: clock lost %d days\n",
    172       1.2  gdamore 				    deltat / SECDAY);
    173       1.2  gdamore 				badrtc = 1;
    174  1.22.2.1     yamt 			} else {
    175       1.2  gdamore 				printf("WARNING: clock gained %d days\n",
    176       1.2  gdamore 				    deltat / SECDAY);
    177       1.2  gdamore 			}
    178       1.2  gdamore 		} else {
    179       1.2  gdamore 			goodtime = 1;
    180       1.2  gdamore 		}
    181       1.2  gdamore 	}
    182       1.2  gdamore 
    183       1.2  gdamore 	/* if the rtc time is bad, use the filesystem time */
    184       1.2  gdamore 	if (badrtc) {
    185       1.2  gdamore 		if (badbase) {
    186       1.2  gdamore 			printf("WARNING: using default initial time\n");
    187       1.2  gdamore 		} else {
    188       1.2  gdamore 			printf("WARNING: using filesystem time\n");
    189       1.2  gdamore 		}
    190       1.2  gdamore 		tv.tv_sec = base;
    191       1.2  gdamore 		tv.tv_usec = 0;
    192       1.1  gdamore 	}
    193       1.2  gdamore 
    194       1.2  gdamore 	timeset = 1;
    195       1.2  gdamore 
    196      1.12  gdamore 	s = splclock();
    197       1.2  gdamore #ifdef	__HAVE_TIMECOUNTER
    198       1.2  gdamore 	ts.tv_sec = tv.tv_sec;
    199       1.2  gdamore 	ts.tv_nsec = tv.tv_usec * 1000;
    200       1.2  gdamore 	tc_setclock(&ts);
    201       1.2  gdamore #else
    202       1.2  gdamore 	time = tv;
    203       1.2  gdamore #endif
    204      1.12  gdamore 	splx(s);
    205       1.2  gdamore 
    206       1.2  gdamore 	if (waszero || goodtime)
    207       1.2  gdamore 		return;
    208       1.2  gdamore 
    209       1.2  gdamore 	printf("WARNING: CHECK AND RESET THE DATE!\n");
    210       1.1  gdamore }
    211       1.1  gdamore 
    212       1.1  gdamore /*
    213       1.1  gdamore  * Reset the TODR based on the time value; used when the TODR
    214       1.1  gdamore  * has a preposterous value and also when the time is reset
    215       1.1  gdamore  * by the stime system call.  Also called when the TODR goes past
    216       1.1  gdamore  * TODRZERO + 100*(SECYEAR+2*SECDAY) (e.g. on Jan 2 just after midnight)
    217       1.1  gdamore  * to wrap the TODR around.
    218       1.1  gdamore  */
    219       1.1  gdamore void
    220       1.1  gdamore resettodr(void)
    221       1.1  gdamore {
    222       1.1  gdamore #ifdef	__HAVE_TIMECOUNTER
    223       1.1  gdamore 	struct timeval	time;
    224       1.2  gdamore #endif
    225       1.1  gdamore 
    226       1.2  gdamore 	/*
    227       1.2  gdamore 	 * We might have been called by boot() due to a crash early
    228       1.2  gdamore 	 * on.  Don't reset the clock chip if we don't know what time
    229       1.2  gdamore 	 * it is.
    230       1.2  gdamore 	 */
    231       1.2  gdamore 	if (!timeset)
    232       1.2  gdamore 		return;
    233       1.2  gdamore 
    234       1.2  gdamore #ifdef	__HAVE_TIMECOUNTER
    235       1.1  gdamore 	getmicrotime(&time);
    236       1.1  gdamore #endif
    237       1.1  gdamore 
    238       1.1  gdamore 	if (time.tv_sec == 0)
    239       1.1  gdamore 		return;
    240       1.1  gdamore 
    241       1.1  gdamore 	if (todr_handle)
    242       1.1  gdamore 		if (todr_settime(todr_handle, &time) != 0)
    243       1.1  gdamore 			printf("Cannot set TOD clock time\n");
    244       1.1  gdamore }
    245       1.1  gdamore 
    246       1.1  gdamore #endif	/* __HAVE_GENERIC_TODR */
    247       1.6  gdamore 
    248      1.18  gdamore #ifdef	TODR_DEBUG
    249      1.18  gdamore static void
    250      1.18  gdamore todr_debug(const char *prefix, int rv, struct clock_ymdhms *dt,
    251      1.18  gdamore     volatile struct timeval *tvp)
    252      1.18  gdamore {
    253      1.18  gdamore 	struct timeval tv_val;
    254      1.18  gdamore 	struct clock_ymdhms dt_val;
    255      1.18  gdamore 
    256      1.18  gdamore 	if (dt == NULL) {
    257      1.18  gdamore 		clock_secs_to_ymdhms(tvp->tv_sec, &dt_val);
    258      1.18  gdamore 		dt = &dt_val;
    259      1.18  gdamore 	}
    260      1.18  gdamore 	if (tvp == NULL) {
    261      1.18  gdamore 		tvp = &tv_val;
    262      1.18  gdamore 		tvp->tv_sec = clock_ymdhms_to_secs(dt);
    263      1.18  gdamore 		tvp->tv_usec = 0;
    264      1.18  gdamore 	}
    265      1.18  gdamore 	printf("%s: rv = %d\n", prefix, rv);
    266      1.18  gdamore 	printf("%s: rtc_offset = %d\n", prefix, rtc_offset);
    267  1.22.2.1     yamt 	printf("%s: %4u/%02u/%02u %02u:%02u:%02u, (wday %d) (epoch %u.%06u)\n",
    268      1.18  gdamore 	    prefix,
    269      1.18  gdamore 	    dt->dt_year, dt->dt_mon, dt->dt_day,
    270      1.18  gdamore 	    dt->dt_hour, dt->dt_min, dt->dt_sec,
    271      1.18  gdamore 	    dt->dt_wday, (unsigned)tvp->tv_sec, (unsigned)tvp->tv_usec);
    272      1.18  gdamore }
    273      1.18  gdamore #else	/* !TODR_DEBUG */
    274      1.18  gdamore #define	todr_debug(prefix, rv, dt, tvp)
    275      1.18  gdamore #endif	/* TODR_DEBUG */
    276      1.18  gdamore 
    277       1.6  gdamore 
    278       1.6  gdamore int
    279      1.14    gavan todr_gettime(todr_chip_handle_t tch, volatile struct timeval *tvp)
    280       1.6  gdamore {
    281       1.6  gdamore 	struct clock_ymdhms	dt;
    282       1.6  gdamore 	int			rv;
    283       1.6  gdamore 
    284      1.17  gdamore 	if (tch->todr_gettime) {
    285      1.17  gdamore 		rv = tch->todr_gettime(tch, tvp);
    286      1.20  gdamore 		/*
    287      1.20  gdamore 		 * Some unconverted ports have their own references to
    288      1.20  gdamore 		 * rtc_offset.   A converted port must not do that.
    289      1.20  gdamore 		 */
    290      1.20  gdamore #ifdef	__HAVE_GENERIC_TODR
    291      1.20  gdamore 		if (rv == 0)
    292      1.20  gdamore 			tvp->tv_sec += rtc_offset * 60;
    293      1.20  gdamore #endif
    294  1.22.2.1     yamt 		todr_debug("TODR-GET-SECS", rv, NULL, tvp);
    295      1.17  gdamore 		return rv;
    296      1.17  gdamore 	} else if (tch->todr_gettime_ymdhms) {
    297      1.17  gdamore 		rv = tch->todr_gettime_ymdhms(tch, &dt);
    298  1.22.2.1     yamt 		todr_debug("TODR-GET-YMDHMS", rv, &dt, NULL);
    299      1.17  gdamore 		if (rv)
    300       1.6  gdamore 			return rv;
    301      1.10  gdamore 
    302       1.6  gdamore 		/*
    303      1.10  gdamore 		 * Formerly we had code here that explicitly checked
    304      1.10  gdamore 		 * for 2038 year rollover.
    305      1.10  gdamore 		 *
    306      1.10  gdamore 		 * However, clock_ymdhms_to_secs performs the same
    307      1.10  gdamore 		 * check for us, so we need not worry about it.  Note
    308      1.13  tsutsui 		 * that this assumes that the tvp->tv_sec member is
    309      1.10  gdamore 		 * a time_t.
    310       1.6  gdamore 		 */
    311       1.6  gdamore 
    312      1.19  gdamore 		/*
    313      1.19  gdamore 		 * Simple sanity checks.  Note that this includes a
    314      1.19  gdamore 		 * value for clocks that can return a leap second.
    315      1.19  gdamore 		 * Note that we don't support double leap seconds,
    316      1.19  gdamore 		 * since this was apparently an error/misunderstanding
    317      1.19  gdamore 		 * on the part of the ISO C committee, and can never
    318      1.19  gdamore 		 * actually occur.  If your clock issues us a double
    319      1.19  gdamore 		 * leap second, it must be broken.  Ultimately, you'd
    320      1.19  gdamore 		 * have to be trying to read time at precisely that
    321      1.19  gdamore 		 * instant to even notice, so even broken clocks will
    322      1.19  gdamore 		 * work the vast majority of the time.  In such a case
    323      1.21  gdamore 		 * it is recommended correction be applied in the
    324      1.21  gdamore 		 * clock driver.
    325      1.19  gdamore 		 */
    326      1.13  tsutsui 		if (dt.dt_mon < 1 || dt.dt_mon > 12 ||
    327      1.13  tsutsui 		    dt.dt_day < 1 || dt.dt_day > 31 ||
    328      1.19  gdamore 		    dt.dt_hour > 23 || dt.dt_min > 59 || dt.dt_sec > 60) {
    329      1.17  gdamore 			return EINVAL;
    330      1.17  gdamore 		}
    331       1.6  gdamore 		tvp->tv_sec = clock_ymdhms_to_secs(&dt) + rtc_offset * 60;
    332       1.6  gdamore 		tvp->tv_usec = 0;
    333      1.17  gdamore 		return tvp->tv_sec < 0 ? EINVAL : 0;
    334       1.6  gdamore 	}
    335       1.6  gdamore 
    336      1.17  gdamore 	return ENXIO;
    337       1.6  gdamore }
    338       1.6  gdamore 
    339       1.6  gdamore int
    340       1.8      uwe todr_settime(todr_chip_handle_t tch, volatile struct timeval *tvp)
    341       1.6  gdamore {
    342       1.6  gdamore 	struct clock_ymdhms	dt;
    343      1.17  gdamore 	int			rv;
    344       1.6  gdamore 
    345      1.17  gdamore 	if (tch->todr_settime) {
    346      1.20  gdamore 		/* See comments above in gettime why this is ifdef'd */
    347      1.20  gdamore #ifdef	__HAVE_GENERIC_TODR
    348      1.20  gdamore 		struct timeval	copy = *tvp;
    349      1.20  gdamore 		copy.tv_sec -= rtc_offset * 60;
    350      1.20  gdamore 		rv = tch->todr_settime(tch, &copy);
    351      1.20  gdamore #else
    352      1.17  gdamore 		rv = tch->todr_settime(tch, tvp);
    353      1.20  gdamore #endif
    354      1.17  gdamore 		todr_debug("TODR-SET-SECS", rv, NULL, tvp);
    355      1.17  gdamore 		return rv;
    356      1.17  gdamore 	} else if (tch->todr_settime_ymdhms) {
    357      1.10  gdamore 		time_t	sec = tvp->tv_sec - rtc_offset * 60;
    358      1.10  gdamore 		if (tvp->tv_usec >= 500000)
    359      1.10  gdamore 			sec++;
    360      1.10  gdamore 		clock_secs_to_ymdhms(sec, &dt);
    361      1.17  gdamore 		rv = tch->todr_settime_ymdhms(tch, &dt);
    362      1.17  gdamore 		todr_debug("TODR-SET-YMDHMS", rv, &dt, NULL);
    363      1.17  gdamore 		return rv;
    364      1.17  gdamore 	} else {
    365      1.17  gdamore 		return ENXIO;
    366       1.6  gdamore 	}
    367       1.6  gdamore }
    368