/*-
* Copyright (c) 2003 Izumi Tsutsui. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* mc146818 and compatible time of day chip subroutines
*/
/*
* todr_gettime function:
* Get time of day and convert it to a struct timeval.
* Return 0 on success, an error number othersize.
*/
int
mc146818_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
{
struct mc146818_softc *sc;
int s, timeout, cent, year;
sc = handle->cookie;
s = splclock(); /* XXX really needed? */
timeout = 1000000; /* XXX how long should we wait? */
for (;;) {
if (((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP) == 0)
break;
if (--timeout < 0) {
printf("%s: timeout\n", __func__);
return EBUSY;
}
}
year += sc->sc_year0;
if (year < POSIX_BASE_YEAR &&
(sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
year += 100;
dt->dt_year = year;
splx(s);
return 0;
}
/*
* todr_settime function:
* Set the time of day clock based on the value of the struct timeval arg.
* Return 0 on success, an error number othersize.
*/
int
mc146818_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
{
struct mc146818_softc *sc;
int s, cent, year;