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