Home | History | Annotate | Line # | Download | only in kern
kern_todr.c revision 1.39.18.2
      1  1.39.18.2    martin /*	$NetBSD: kern_todr.c,v 1.39.18.2 2020/04/13 08:05:04 martin Exp $	*/
      2  1.39.18.1    martin 
      3  1.39.18.1    martin /*-
      4  1.39.18.1    martin  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.39.18.1    martin  * All rights reserved.
      6  1.39.18.1    martin  *
      7  1.39.18.1    martin  * This code is derived from software contributed to The NetBSD Foundation
      8  1.39.18.1    martin  * by Jason R. Thorpe.
      9  1.39.18.1    martin  *
     10  1.39.18.1    martin  * Redistribution and use in source and binary forms, with or without
     11  1.39.18.1    martin  * modification, are permitted provided that the following conditions
     12  1.39.18.1    martin  * are met:
     13  1.39.18.1    martin  * 1. Redistributions of source code must retain the above copyright
     14  1.39.18.1    martin  *    notice, this list of conditions and the following disclaimer.
     15  1.39.18.1    martin  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.39.18.1    martin  *    notice, this list of conditions and the following disclaimer in the
     17  1.39.18.1    martin  *    documentation and/or other materials provided with the distribution.
     18  1.39.18.1    martin  *
     19  1.39.18.1    martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.39.18.1    martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.39.18.1    martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.39.18.1    martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.39.18.1    martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.39.18.1    martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.39.18.1    martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.39.18.1    martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.39.18.1    martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.39.18.1    martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.39.18.1    martin  * POSSIBILITY OF SUCH DAMAGE.
     30  1.39.18.1    martin  */
     31        1.1   gdamore 
     32        1.1   gdamore /*
     33       1.34     rmind  * Copyright (c) 1988 University of Utah.
     34        1.1   gdamore  * Copyright (c) 1992, 1993
     35        1.1   gdamore  *	The Regents of the University of California.  All rights reserved.
     36        1.1   gdamore  *
     37        1.1   gdamore  * This code is derived from software contributed to Berkeley by
     38        1.1   gdamore  * the Systems Programming Group of the University of Utah Computer
     39        1.1   gdamore  * Science Department and Ralph Campbell.
     40        1.1   gdamore  *
     41        1.1   gdamore  * Redistribution and use in source and binary forms, with or without
     42        1.1   gdamore  * modification, are permitted provided that the following conditions
     43        1.1   gdamore  * are met:
     44        1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     45        1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     46        1.1   gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     47        1.1   gdamore  *    notice, this list of conditions and the following disclaimer in the
     48        1.1   gdamore  *    documentation and/or other materials provided with the distribution.
     49        1.1   gdamore  * 3. Neither the name of the University nor the names of its contributors
     50        1.1   gdamore  *    may be used to endorse or promote products derived from this software
     51        1.1   gdamore  *    without specific prior written permission.
     52        1.1   gdamore  *
     53        1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54        1.1   gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55        1.1   gdamore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56        1.1   gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57        1.1   gdamore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58        1.1   gdamore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59        1.1   gdamore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60        1.1   gdamore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61        1.1   gdamore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62        1.1   gdamore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63        1.1   gdamore  * SUCH DAMAGE.
     64        1.1   gdamore  *
     65        1.1   gdamore  * from: Utah Hdr: clock.c 1.18 91/01/21
     66        1.1   gdamore  *
     67        1.1   gdamore  *	@(#)clock.c	8.1 (Berkeley) 6/10/93
     68        1.1   gdamore  */
     69       1.34     rmind 
     70       1.38  jmcneill #include "opt_todr.h"
     71       1.38  jmcneill 
     72        1.1   gdamore #include <sys/cdefs.h>
     73  1.39.18.2    martin __KERNEL_RCSID(0, "$NetBSD: kern_todr.c,v 1.39.18.2 2020/04/13 08:05:04 martin Exp $");
     74        1.1   gdamore 
     75        1.1   gdamore #include <sys/param.h>
     76        1.1   gdamore #include <sys/kernel.h>
     77        1.1   gdamore #include <sys/systm.h>
     78        1.1   gdamore #include <sys/device.h>
     79        1.1   gdamore #include <sys/timetc.h>
     80       1.25        ad #include <sys/intr.h>
     81       1.39  riastrad #include <sys/rndsource.h>
     82  1.39.18.1    martin #include <sys/mutex.h>
     83       1.25        ad 
     84        1.1   gdamore #include <dev/clock_subr.h>	/* hmm.. this should probably move to sys */
     85        1.1   gdamore 
     86  1.39.18.1    martin static int todr_gettime(todr_chip_handle_t, struct timeval *);
     87  1.39.18.1    martin static int todr_settime(todr_chip_handle_t, struct timeval *);
     88  1.39.18.1    martin 
     89  1.39.18.1    martin static kmutex_t todr_mutex;
     90  1.39.18.1    martin static todr_chip_handle_t todr_handle;
     91  1.39.18.1    martin static bool todr_initialized;
     92  1.39.18.1    martin 
     93  1.39.18.1    martin /*
     94  1.39.18.1    martin  * todr_init:
     95  1.39.18.1    martin  *	Initialize TOD clock data.
     96  1.39.18.1    martin  */
     97  1.39.18.1    martin void
     98  1.39.18.1    martin todr_init(void)
     99  1.39.18.1    martin {
    100  1.39.18.1    martin 
    101  1.39.18.1    martin 	mutex_init(&todr_mutex, MUTEX_DEFAULT, IPL_NONE);
    102  1.39.18.1    martin 	todr_initialized = true;
    103  1.39.18.1    martin }
    104  1.39.18.1    martin 
    105  1.39.18.1    martin /*
    106  1.39.18.1    martin  * todr_lock:
    107  1.39.18.1    martin  *	Acquire the TODR lock.
    108  1.39.18.1    martin  */
    109  1.39.18.1    martin void
    110  1.39.18.1    martin todr_lock(void)
    111  1.39.18.1    martin {
    112  1.39.18.1    martin 
    113  1.39.18.1    martin 	mutex_enter(&todr_mutex);
    114  1.39.18.1    martin }
    115        1.1   gdamore 
    116        1.1   gdamore /*
    117  1.39.18.1    martin  * todr_unlock:
    118  1.39.18.1    martin  *	Release the TODR lock.
    119  1.39.18.1    martin  */
    120  1.39.18.1    martin void
    121  1.39.18.1    martin todr_unlock(void)
    122  1.39.18.1    martin {
    123  1.39.18.1    martin 
    124  1.39.18.1    martin 	mutex_exit(&todr_mutex);
    125  1.39.18.1    martin }
    126  1.39.18.1    martin 
    127  1.39.18.1    martin /*
    128  1.39.18.1    martin  * todr_lock_owned:
    129  1.39.18.1    martin  *	Return true if the current thread owns the TODR lock.
    130  1.39.18.1    martin  *	This is to be used by diagnostic assertions only.
    131  1.39.18.1    martin  */
    132  1.39.18.1    martin bool
    133  1.39.18.1    martin todr_lock_owned(void)
    134  1.39.18.1    martin {
    135  1.39.18.1    martin 
    136  1.39.18.1    martin 	return mutex_owned(&todr_mutex) ? true : false;
    137  1.39.18.1    martin }
    138  1.39.18.1    martin 
    139  1.39.18.1    martin /*
    140  1.39.18.1    martin  * todr_attach:
    141  1.39.18.1    martin  *	Attach the clock device to todr_handle.
    142        1.1   gdamore  */
    143        1.1   gdamore void
    144        1.1   gdamore todr_attach(todr_chip_handle_t todr)
    145        1.1   gdamore {
    146        1.1   gdamore 
    147  1.39.18.1    martin 	/*
    148  1.39.18.1    martin 	 * todr_init() is called very early in main(), but this is
    149  1.39.18.1    martin 	 * here to catch a case where todr_attach() is called before
    150  1.39.18.1    martin 	 * main().
    151  1.39.18.1    martin 	 */
    152  1.39.18.1    martin 	KASSERT(todr_initialized);
    153  1.39.18.1    martin 
    154  1.39.18.1    martin 	todr_lock();
    155       1.24     peter 	if (todr_handle) {
    156  1.39.18.1    martin 		todr_unlock();
    157       1.24     peter 		printf("todr_attach: TOD already configured\n");
    158        1.1   gdamore 		return;
    159        1.1   gdamore 	}
    160       1.24     peter 	todr_handle = todr;
    161  1.39.18.1    martin 	todr_unlock();
    162        1.1   gdamore }
    163        1.1   gdamore 
    164       1.31   tsutsui static bool timeset = false;
    165        1.2   gdamore 
    166        1.1   gdamore /*
    167  1.39.18.1    martin  * todr_set_systime:
    168  1.39.18.1    martin  *	Set up the system's time.  The "base" argument is a best-guess
    169  1.39.18.1    martin  *	close-enough value to use if the TOD clock is unavailable or
    170  1.39.18.1    martin  *	contains garbage.  Must be called with the TODR lock held.
    171        1.1   gdamore  */
    172        1.1   gdamore void
    173  1.39.18.1    martin todr_set_systime(time_t base)
    174        1.1   gdamore {
    175       1.31   tsutsui 	bool badbase = false;
    176       1.31   tsutsui 	bool waszero = (base == 0);
    177       1.31   tsutsui 	bool goodtime = false;
    178       1.31   tsutsui 	bool badrtc = false;
    179        1.1   gdamore 	struct timespec ts;
    180        1.2   gdamore 	struct timeval tv;
    181        1.1   gdamore 
    182  1.39.18.1    martin 	KASSERT(todr_lock_owned());
    183  1.39.18.1    martin 
    184       1.35       tls 	rnd_add_data(NULL, &base, sizeof(base), 0);
    185       1.35       tls 
    186       1.37  christos 	if (base < 5 * SECS_PER_COMMON_YEAR) {
    187        1.4   gdamore 		struct clock_ymdhms basedate;
    188        1.4   gdamore 
    189        1.1   gdamore 		/*
    190        1.1   gdamore 		 * If base is 0, assume filesystem time is just unknown
    191        1.2   gdamore 		 * instead of preposterous. Don't bark.
    192        1.1   gdamore 		 */
    193        1.1   gdamore 		if (base != 0)
    194        1.1   gdamore 			printf("WARNING: preposterous time in file system\n");
    195        1.1   gdamore 		/* not going to use it anyway, if the chip is readable */
    196       1.33   tsutsui 		basedate.dt_year = 2010;
    197        1.4   gdamore 		basedate.dt_mon = 1;
    198        1.4   gdamore 		basedate.dt_day = 1;
    199        1.4   gdamore 		basedate.dt_hour = 12;
    200        1.4   gdamore 		basedate.dt_min = 0;
    201        1.4   gdamore 		basedate.dt_sec = 0;
    202        1.4   gdamore 		base = clock_ymdhms_to_secs(&basedate);
    203       1.31   tsutsui 		badbase = true;
    204        1.1   gdamore 	}
    205        1.1   gdamore 
    206        1.5      matt 	/*
    207        1.5      matt 	 * Some ports need to be supplied base in order to fabricate a time_t.
    208        1.5      matt 	 */
    209       1.22   gdamore 	if (todr_handle)
    210       1.22   gdamore 		todr_handle->base_time = base;
    211        1.5      matt 
    212  1.39.18.2    martin 	memset(&tv, 0, sizeof(tv));
    213  1.39.18.2    martin 
    214        1.1   gdamore 	if ((todr_handle == NULL) ||
    215        1.2   gdamore 	    (todr_gettime(todr_handle, &tv) != 0) ||
    216       1.37  christos 	    (tv.tv_sec < (25 * SECS_PER_COMMON_YEAR))) {
    217        1.1   gdamore 
    218       1.24     peter 		if (todr_handle != NULL)
    219        1.2   gdamore 			printf("WARNING: preposterous TOD clock time\n");
    220        1.1   gdamore 		else
    221        1.2   gdamore 			printf("WARNING: no TOD clock present\n");
    222       1.31   tsutsui 		badrtc = true;
    223        1.1   gdamore 	} else {
    224       1.30   tsutsui 		time_t deltat = tv.tv_sec - base;
    225        1.1   gdamore 
    226        1.1   gdamore 		if (deltat < 0)
    227        1.1   gdamore 			deltat = -deltat;
    228        1.2   gdamore 
    229       1.37  christos 		if (!badbase && deltat >= 2 * SECS_PER_DAY) {
    230        1.2   gdamore 
    231        1.2   gdamore 			if (tv.tv_sec < base) {
    232        1.2   gdamore 				/*
    233        1.2   gdamore 				 * The clock should never go backwards
    234        1.2   gdamore 				 * relative to filesystem time.  If it
    235        1.2   gdamore 				 * does by more than the threshold,
    236        1.2   gdamore 				 * believe the filesystem.
    237        1.2   gdamore 				 */
    238       1.30   tsutsui 				printf("WARNING: clock lost %" PRId64 " days\n",
    239       1.37  christos 				    deltat / SECS_PER_DAY);
    240       1.31   tsutsui 				badrtc = true;
    241       1.24     peter 			} else {
    242       1.30   tsutsui 				aprint_verbose("WARNING: clock gained %" PRId64
    243       1.37  christos 				    " days\n", deltat / SECS_PER_DAY);
    244       1.31   tsutsui 				goodtime = true;
    245        1.2   gdamore 			}
    246        1.2   gdamore 		} else {
    247       1.31   tsutsui 			goodtime = true;
    248        1.2   gdamore 		}
    249       1.35       tls 
    250       1.35       tls 		rnd_add_data(NULL, &tv, sizeof(tv), 0);
    251        1.2   gdamore 	}
    252        1.2   gdamore 
    253        1.2   gdamore 	/* if the rtc time is bad, use the filesystem time */
    254        1.2   gdamore 	if (badrtc) {
    255        1.2   gdamore 		if (badbase) {
    256        1.2   gdamore 			printf("WARNING: using default initial time\n");
    257        1.2   gdamore 		} else {
    258        1.2   gdamore 			printf("WARNING: using filesystem time\n");
    259        1.2   gdamore 		}
    260        1.2   gdamore 		tv.tv_sec = base;
    261        1.2   gdamore 		tv.tv_usec = 0;
    262        1.1   gdamore 	}
    263        1.2   gdamore 
    264       1.31   tsutsui 	timeset = true;
    265        1.2   gdamore 
    266        1.2   gdamore 	ts.tv_sec = tv.tv_sec;
    267        1.2   gdamore 	ts.tv_nsec = tv.tv_usec * 1000;
    268        1.2   gdamore 	tc_setclock(&ts);
    269        1.2   gdamore 
    270        1.2   gdamore 	if (waszero || goodtime)
    271        1.2   gdamore 		return;
    272        1.2   gdamore 
    273        1.2   gdamore 	printf("WARNING: CHECK AND RESET THE DATE!\n");
    274        1.1   gdamore }
    275        1.1   gdamore 
    276        1.1   gdamore /*
    277  1.39.18.1    martin  * todr_save_systime:
    278  1.39.18.1    martin  *	Save the current system time back to the TOD clock.
    279  1.39.18.1    martin  *	Must be called with the TODR lock held.
    280        1.1   gdamore  */
    281        1.1   gdamore void
    282  1.39.18.1    martin todr_save_systime(void)
    283        1.1   gdamore {
    284       1.26      yamt 	struct timeval tv;
    285        1.1   gdamore 
    286  1.39.18.1    martin 	KASSERT(todr_lock_owned());
    287  1.39.18.1    martin 
    288        1.2   gdamore 	/*
    289        1.2   gdamore 	 * We might have been called by boot() due to a crash early
    290        1.2   gdamore 	 * on.  Don't reset the clock chip if we don't know what time
    291        1.2   gdamore 	 * it is.
    292        1.2   gdamore 	 */
    293        1.2   gdamore 	if (!timeset)
    294        1.2   gdamore 		return;
    295        1.2   gdamore 
    296       1.26      yamt 	getmicrotime(&tv);
    297        1.1   gdamore 
    298       1.26      yamt 	if (tv.tv_sec == 0)
    299        1.1   gdamore 		return;
    300        1.1   gdamore 
    301        1.1   gdamore 	if (todr_handle)
    302       1.26      yamt 		if (todr_settime(todr_handle, &tv) != 0)
    303        1.1   gdamore 			printf("Cannot set TOD clock time\n");
    304        1.1   gdamore }
    305        1.1   gdamore 
    306  1.39.18.1    martin /*
    307  1.39.18.1    martin  * inittodr:
    308  1.39.18.1    martin  *	Legacy wrapper around todr_set_systime().
    309  1.39.18.1    martin  */
    310  1.39.18.1    martin void
    311  1.39.18.1    martin inittodr(time_t base)
    312  1.39.18.1    martin {
    313  1.39.18.1    martin 
    314  1.39.18.1    martin 	todr_lock();
    315  1.39.18.1    martin 	todr_set_systime(base);
    316  1.39.18.1    martin 	todr_unlock();
    317  1.39.18.1    martin }
    318  1.39.18.1    martin 
    319  1.39.18.1    martin /*
    320  1.39.18.1    martin  * resettodr:
    321  1.39.18.1    martin  *	Legacy wrapper around todr_save_systime().
    322  1.39.18.1    martin  */
    323  1.39.18.1    martin void
    324  1.39.18.1    martin resettodr(void)
    325  1.39.18.1    martin {
    326  1.39.18.1    martin 
    327  1.39.18.1    martin 	/*
    328  1.39.18.1    martin 	 * If we're shutting down, we don't want to get stuck in case
    329  1.39.18.1    martin 	 * someone was already fiddling with the TOD clock.
    330  1.39.18.1    martin 	 */
    331  1.39.18.1    martin 	if (shutting_down) {
    332  1.39.18.1    martin 		if (mutex_tryenter(&todr_mutex) == 0) {
    333  1.39.18.1    martin 			printf("WARNING: Cannot set TOD clock time (busy)\n");
    334  1.39.18.1    martin 			return;
    335  1.39.18.1    martin 		}
    336  1.39.18.1    martin 	} else {
    337  1.39.18.1    martin 		todr_lock();
    338  1.39.18.1    martin 	}
    339  1.39.18.1    martin 	todr_save_systime();
    340  1.39.18.1    martin 	todr_unlock();
    341  1.39.18.1    martin }
    342  1.39.18.1    martin 
    343       1.18   gdamore #ifdef	TODR_DEBUG
    344       1.18   gdamore static void
    345       1.18   gdamore todr_debug(const char *prefix, int rv, struct clock_ymdhms *dt,
    346       1.32   tsutsui     struct timeval *tvp)
    347       1.18   gdamore {
    348       1.18   gdamore 	struct timeval tv_val;
    349       1.18   gdamore 	struct clock_ymdhms dt_val;
    350       1.18   gdamore 
    351       1.18   gdamore 	if (dt == NULL) {
    352       1.18   gdamore 		clock_secs_to_ymdhms(tvp->tv_sec, &dt_val);
    353       1.18   gdamore 		dt = &dt_val;
    354       1.18   gdamore 	}
    355       1.18   gdamore 	if (tvp == NULL) {
    356       1.18   gdamore 		tvp = &tv_val;
    357       1.18   gdamore 		tvp->tv_sec = clock_ymdhms_to_secs(dt);
    358       1.18   gdamore 		tvp->tv_usec = 0;
    359       1.18   gdamore 	}
    360       1.18   gdamore 	printf("%s: rv = %d\n", prefix, rv);
    361       1.18   gdamore 	printf("%s: rtc_offset = %d\n", prefix, rtc_offset);
    362       1.23   tsutsui 	printf("%s: %4u/%02u/%02u %02u:%02u:%02u, (wday %d) (epoch %u.%06u)\n",
    363       1.18   gdamore 	    prefix,
    364       1.36  jmcneill 	    (unsigned)dt->dt_year, dt->dt_mon, dt->dt_day,
    365       1.18   gdamore 	    dt->dt_hour, dt->dt_min, dt->dt_sec,
    366       1.18   gdamore 	    dt->dt_wday, (unsigned)tvp->tv_sec, (unsigned)tvp->tv_usec);
    367       1.18   gdamore }
    368       1.18   gdamore #else	/* !TODR_DEBUG */
    369       1.18   gdamore #define	todr_debug(prefix, rv, dt, tvp)
    370       1.18   gdamore #endif	/* TODR_DEBUG */
    371       1.18   gdamore 
    372  1.39.18.1    martin static int
    373  1.39.18.1    martin todr_wenable(todr_chip_handle_t todr, int onoff)
    374  1.39.18.1    martin {
    375        1.6   gdamore 
    376  1.39.18.1    martin 	if (todr->todr_setwen != NULL)
    377  1.39.18.1    martin 		return todr->todr_setwen(todr, onoff);
    378  1.39.18.1    martin 
    379  1.39.18.1    martin 	return 0;
    380  1.39.18.1    martin }
    381  1.39.18.1    martin 
    382  1.39.18.1    martin #define	ENABLE_TODR_WRITES()						\
    383  1.39.18.1    martin do {									\
    384  1.39.18.1    martin 	if ((rv = todr_wenable(tch, 1)) != 0) {				\
    385  1.39.18.1    martin 		printf("%s: cannot enable TODR writes\n", __func__);	\
    386  1.39.18.1    martin 		return rv;						\
    387  1.39.18.1    martin 	}								\
    388  1.39.18.1    martin } while (/*CONSTCOND*/0)
    389  1.39.18.1    martin 
    390  1.39.18.1    martin #define	DISABLE_TODR_WRITES()						\
    391  1.39.18.1    martin do {									\
    392  1.39.18.1    martin 	if (todr_wenable(tch, 0) != 0)					\
    393  1.39.18.1    martin 		printf("%s: WARNING: cannot disable TODR writes\n",	\
    394  1.39.18.1    martin 		    __func__);						\
    395  1.39.18.1    martin } while (/*CONSTCOND*/0)
    396  1.39.18.1    martin 
    397  1.39.18.1    martin static int
    398       1.32   tsutsui todr_gettime(todr_chip_handle_t tch, struct timeval *tvp)
    399        1.6   gdamore {
    400  1.39.18.1    martin 	int rv;
    401  1.39.18.1    martin 
    402  1.39.18.1    martin 	/*
    403  1.39.18.1    martin 	 * Write-enable is used even when reading the TODR because
    404  1.39.18.1    martin 	 * writing to registers may be required in order to do so.
    405  1.39.18.1    martin 	 */
    406        1.6   gdamore 
    407       1.17   gdamore 	if (tch->todr_gettime) {
    408  1.39.18.1    martin 		ENABLE_TODR_WRITES();
    409       1.17   gdamore 		rv = tch->todr_gettime(tch, tvp);
    410  1.39.18.1    martin 		DISABLE_TODR_WRITES();
    411       1.20   gdamore 		/*
    412       1.20   gdamore 		 * Some unconverted ports have their own references to
    413       1.20   gdamore 		 * rtc_offset.   A converted port must not do that.
    414       1.20   gdamore 		 */
    415       1.20   gdamore 		if (rv == 0)
    416       1.20   gdamore 			tvp->tv_sec += rtc_offset * 60;
    417       1.23   tsutsui 		todr_debug("TODR-GET-SECS", rv, NULL, tvp);
    418       1.17   gdamore 		return rv;
    419       1.17   gdamore 	} else if (tch->todr_gettime_ymdhms) {
    420  1.39.18.1    martin 		struct clock_ymdhms dt = { 0 };
    421  1.39.18.1    martin 		ENABLE_TODR_WRITES();
    422       1.17   gdamore 		rv = tch->todr_gettime_ymdhms(tch, &dt);
    423  1.39.18.1    martin 		DISABLE_TODR_WRITES();
    424       1.23   tsutsui 		todr_debug("TODR-GET-YMDHMS", rv, &dt, NULL);
    425       1.17   gdamore 		if (rv)
    426        1.6   gdamore 			return rv;
    427       1.10   gdamore 
    428        1.6   gdamore 		/*
    429       1.19   gdamore 		 * Simple sanity checks.  Note that this includes a
    430       1.19   gdamore 		 * value for clocks that can return a leap second.
    431       1.19   gdamore 		 * Note that we don't support double leap seconds,
    432       1.19   gdamore 		 * since this was apparently an error/misunderstanding
    433       1.19   gdamore 		 * on the part of the ISO C committee, and can never
    434       1.19   gdamore 		 * actually occur.  If your clock issues us a double
    435       1.19   gdamore 		 * leap second, it must be broken.  Ultimately, you'd
    436       1.19   gdamore 		 * have to be trying to read time at precisely that
    437       1.19   gdamore 		 * instant to even notice, so even broken clocks will
    438       1.19   gdamore 		 * work the vast majority of the time.  In such a case
    439       1.21   gdamore 		 * it is recommended correction be applied in the
    440       1.21   gdamore 		 * clock driver.
    441       1.19   gdamore 		 */
    442       1.13   tsutsui 		if (dt.dt_mon < 1 || dt.dt_mon > 12 ||
    443       1.13   tsutsui 		    dt.dt_day < 1 || dt.dt_day > 31 ||
    444       1.19   gdamore 		    dt.dt_hour > 23 || dt.dt_min > 59 || dt.dt_sec > 60) {
    445       1.17   gdamore 			return EINVAL;
    446       1.17   gdamore 		}
    447        1.6   gdamore 		tvp->tv_sec = clock_ymdhms_to_secs(&dt) + rtc_offset * 60;
    448        1.6   gdamore 		tvp->tv_usec = 0;
    449       1.17   gdamore 		return tvp->tv_sec < 0 ? EINVAL : 0;
    450        1.6   gdamore 	}
    451        1.6   gdamore 
    452       1.17   gdamore 	return ENXIO;
    453        1.6   gdamore }
    454        1.6   gdamore 
    455  1.39.18.1    martin static int
    456       1.32   tsutsui todr_settime(todr_chip_handle_t tch, struct timeval *tvp)
    457        1.6   gdamore {
    458  1.39.18.1    martin 	int rv;
    459        1.6   gdamore 
    460       1.17   gdamore 	if (tch->todr_settime) {
    461  1.39.18.1    martin 		struct timeval copy = *tvp;
    462       1.20   gdamore 		copy.tv_sec -= rtc_offset * 60;
    463  1.39.18.1    martin 		ENABLE_TODR_WRITES();
    464       1.20   gdamore 		rv = tch->todr_settime(tch, &copy);
    465  1.39.18.1    martin 		DISABLE_TODR_WRITES();
    466       1.17   gdamore 		todr_debug("TODR-SET-SECS", rv, NULL, tvp);
    467       1.17   gdamore 		return rv;
    468       1.17   gdamore 	} else if (tch->todr_settime_ymdhms) {
    469  1.39.18.1    martin 		struct clock_ymdhms dt;
    470  1.39.18.1    martin 		time_t sec = tvp->tv_sec - rtc_offset * 60;
    471       1.10   gdamore 		if (tvp->tv_usec >= 500000)
    472       1.10   gdamore 			sec++;
    473       1.10   gdamore 		clock_secs_to_ymdhms(sec, &dt);
    474  1.39.18.1    martin 		ENABLE_TODR_WRITES();
    475       1.17   gdamore 		rv = tch->todr_settime_ymdhms(tch, &dt);
    476  1.39.18.1    martin 		DISABLE_TODR_WRITES();
    477       1.17   gdamore 		todr_debug("TODR-SET-YMDHMS", rv, &dt, NULL);
    478       1.17   gdamore 		return rv;
    479        1.6   gdamore 	}
    480  1.39.18.1    martin 
    481  1.39.18.1    martin 	return ENXIO;
    482        1.6   gdamore }
    483