Home | History | Annotate | Line # | Download | only in ic
mc146818.c revision 1.13.8.1
      1  1.13.8.1   bouyer /*	$NetBSD: mc146818.c,v 1.13.8.1 2008/01/10 23:44:15 bouyer Exp $	*/
      2       1.1  tsutsui 
      3       1.1  tsutsui /*
      4       1.1  tsutsui  * Copyright (c) 2003 Izumi Tsutsui.  All rights reserved.
      5       1.1  tsutsui  *
      6       1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
      7       1.1  tsutsui  * modification, are permitted provided that the following conditions
      8       1.1  tsutsui  * are met:
      9       1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     10       1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     11       1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     13       1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     14       1.1  tsutsui  * 3. The name of the author may not be used to endorse or promote products
     15       1.1  tsutsui  *    derived from this software without specific prior written permission.
     16       1.1  tsutsui  *
     17       1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18       1.1  tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19       1.1  tsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20       1.1  tsutsui  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21       1.1  tsutsui  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22       1.1  tsutsui  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23       1.1  tsutsui  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24       1.1  tsutsui  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25       1.1  tsutsui  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26       1.1  tsutsui  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27       1.1  tsutsui  */
     28       1.1  tsutsui 
     29       1.1  tsutsui /*
     30       1.1  tsutsui  * mc146818 and compatible time of day chip subroutines
     31       1.1  tsutsui  */
     32       1.1  tsutsui 
     33       1.1  tsutsui #include <sys/cdefs.h>
     34  1.13.8.1   bouyer __KERNEL_RCSID(0, "$NetBSD: mc146818.c,v 1.13.8.1 2008/01/10 23:44:15 bouyer Exp $");
     35       1.1  tsutsui 
     36       1.1  tsutsui #include <sys/param.h>
     37       1.1  tsutsui #include <sys/systm.h>
     38       1.1  tsutsui #include <sys/device.h>
     39       1.1  tsutsui #include <sys/errno.h>
     40       1.1  tsutsui 
     41      1.13       ad #include <sys/bus.h>
     42       1.1  tsutsui 
     43       1.1  tsutsui #include <dev/clock_subr.h>
     44       1.1  tsutsui 
     45       1.1  tsutsui #include <dev/ic/mc146818reg.h>
     46       1.1  tsutsui #include <dev/ic/mc146818var.h>
     47       1.1  tsutsui 
     48      1.10  gdamore int mc146818_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     49      1.10  gdamore int mc146818_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *);
     50       1.1  tsutsui 
     51       1.1  tsutsui void
     52       1.5  tsutsui mc146818_attach(struct mc146818_softc *sc)
     53       1.1  tsutsui {
     54       1.1  tsutsui 	todr_chip_handle_t handle;
     55       1.1  tsutsui 
     56       1.1  tsutsui #ifdef DIAGNOSTIC
     57       1.1  tsutsui 	if (sc->sc_mcread == NULL ||
     58       1.1  tsutsui 	    sc->sc_mcwrite == NULL)
     59  1.13.8.1   bouyer 		panic("%s: invalid read/write functions", __func__);
     60       1.1  tsutsui #endif
     61       1.1  tsutsui 
     62  1.13.8.1   bouyer 	aprint_normal(": mc146818 compatible time-of-day clock");
     63       1.1  tsutsui 
     64       1.1  tsutsui 	handle = &sc->sc_handle;
     65       1.1  tsutsui 	handle->cookie = sc;
     66      1.10  gdamore 	handle->todr_gettime = NULL;
     67      1.10  gdamore 	handle->todr_settime = NULL;
     68      1.12  tsutsui 	handle->todr_gettime_ymdhms = mc146818_gettime_ymdhms;
     69      1.10  gdamore 	handle->todr_settime_ymdhms = mc146818_settime_ymdhms;
     70       1.1  tsutsui 	handle->todr_setwen  = NULL;
     71  1.13.8.1   bouyer 
     72  1.13.8.1   bouyer 	todr_attach(handle);
     73       1.1  tsutsui }
     74       1.1  tsutsui 
     75       1.1  tsutsui /*
     76       1.1  tsutsui  * todr_gettime function:
     77       1.1  tsutsui  *  Get time of day and convert it to a struct timeval.
     78       1.1  tsutsui  *  Return 0 on success, an error number othersize.
     79       1.1  tsutsui  */
     80       1.1  tsutsui int
     81      1.10  gdamore mc146818_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
     82       1.1  tsutsui {
     83       1.1  tsutsui 	struct mc146818_softc *sc;
     84       1.1  tsutsui 	int s, timeout, cent, year;
     85       1.1  tsutsui 
     86       1.1  tsutsui 	sc = handle->cookie;
     87       1.1  tsutsui 
     88       1.1  tsutsui 	s = splclock();		/* XXX really needed? */
     89       1.1  tsutsui 
     90       1.1  tsutsui 	todr_wenable(handle, 1);
     91       1.1  tsutsui 
     92       1.1  tsutsui 	timeout = 1000000;	/* XXX how long should we wait? */
     93       1.1  tsutsui 	for (;;) {
     94       1.1  tsutsui 		if ((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP)
     95       1.1  tsutsui 			break;
     96       1.1  tsutsui 		if (--timeout < 0) {
     97  1.13.8.1   bouyer 			printf("%s: timeout\n", __func__);
     98       1.1  tsutsui 			return EBUSY;
     99       1.1  tsutsui 		}
    100       1.1  tsutsui 	}
    101       1.1  tsutsui 
    102       1.4  tsutsui #define	FROMREG(x)	((sc->sc_flag & MC146818_BCD) ? FROMBCD(x) : (x))
    103       1.4  tsutsui 
    104      1.10  gdamore 	dt->dt_sec  = FROMREG((*sc->sc_mcread)(sc, MC_SEC));
    105      1.10  gdamore 	dt->dt_min  = FROMREG((*sc->sc_mcread)(sc, MC_MIN));
    106      1.10  gdamore 	dt->dt_hour = FROMREG((*sc->sc_mcread)(sc, MC_HOUR));
    107      1.10  gdamore 	dt->dt_wday = FROMREG((*sc->sc_mcread)(sc, MC_DOW));
    108      1.10  gdamore 	dt->dt_day  = FROMREG((*sc->sc_mcread)(sc, MC_DOM));
    109      1.10  gdamore 	dt->dt_mon  = FROMREG((*sc->sc_mcread)(sc, MC_MONTH));
    110       1.4  tsutsui 	year       = FROMREG((*sc->sc_mcread)(sc, MC_YEAR));
    111       1.1  tsutsui 	if (sc->sc_getcent) {
    112       1.1  tsutsui 		cent = (*sc->sc_getcent)(sc);
    113       1.1  tsutsui 		year += cent * 100;
    114       1.1  tsutsui 	}
    115       1.1  tsutsui 
    116       1.4  tsutsui #undef FROMREG
    117       1.4  tsutsui 
    118       1.1  tsutsui 	year += sc->sc_year0;
    119       1.1  tsutsui 	if (year < POSIX_BASE_YEAR &&
    120       1.3  tsutsui 	    (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
    121       1.1  tsutsui 		year += 100;
    122      1.10  gdamore 	dt->dt_year = year;
    123       1.1  tsutsui 
    124       1.1  tsutsui 	todr_wenable(handle, 0);
    125       1.1  tsutsui 
    126       1.1  tsutsui 	splx(s);
    127       1.1  tsutsui 
    128       1.1  tsutsui 	return 0;
    129       1.1  tsutsui }
    130       1.1  tsutsui 
    131       1.1  tsutsui /*
    132       1.1  tsutsui  * todr_settime function:
    133       1.1  tsutsui  *  Set the time of day clock based on the value of the struct timeval arg.
    134       1.1  tsutsui  *  Return 0 on success, an error number othersize.
    135       1.1  tsutsui  */
    136       1.1  tsutsui int
    137      1.10  gdamore mc146818_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
    138       1.1  tsutsui {
    139       1.1  tsutsui 	struct mc146818_softc *sc;
    140       1.1  tsutsui 	int s, timeout, cent, year;
    141       1.1  tsutsui 
    142       1.1  tsutsui 	sc = handle->cookie;
    143       1.1  tsutsui 
    144       1.1  tsutsui 	s = splclock();		/* XXX really needed? */
    145       1.1  tsutsui 
    146       1.1  tsutsui 	todr_wenable(handle, 1);
    147       1.1  tsutsui 
    148       1.1  tsutsui 	timeout = 1000000;	/* XXX how long should we wait? */
    149       1.1  tsutsui 	for (;;) {
    150       1.1  tsutsui 		if ((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP)
    151       1.1  tsutsui 			break;
    152       1.1  tsutsui 		if (--timeout < 0) {
    153  1.13.8.1   bouyer 			printf("%s: timeout\n", __func__);
    154       1.1  tsutsui 			return EBUSY;
    155       1.1  tsutsui 		}
    156       1.1  tsutsui 	}
    157       1.1  tsutsui 
    158       1.4  tsutsui #define	TOREG(x)	((sc->sc_flag & MC146818_BCD) ? TOBCD(x) : (x))
    159       1.4  tsutsui 
    160      1.10  gdamore 	(*sc->sc_mcwrite)(sc, MC_SEC, TOREG(dt->dt_sec));
    161      1.10  gdamore 	(*sc->sc_mcwrite)(sc, MC_MIN, TOREG(dt->dt_min));
    162      1.10  gdamore 	(*sc->sc_mcwrite)(sc, MC_HOUR, TOREG(dt->dt_hour));
    163      1.10  gdamore 	(*sc->sc_mcwrite)(sc, MC_DOW, TOREG(dt->dt_wday));
    164      1.10  gdamore 	(*sc->sc_mcwrite)(sc, MC_DOM, TOREG(dt->dt_day));
    165      1.10  gdamore 	(*sc->sc_mcwrite)(sc, MC_MONTH, TOREG(dt->dt_mon));
    166       1.1  tsutsui 
    167      1.10  gdamore 	year = dt->dt_year - sc->sc_year0;
    168       1.1  tsutsui 	if (sc->sc_setcent) {
    169       1.1  tsutsui 		cent = year / 100;
    170       1.1  tsutsui 		(*sc->sc_setcent)(sc, cent);
    171       1.1  tsutsui 		year -= cent * 100;
    172       1.1  tsutsui 	}
    173       1.3  tsutsui 	if (year > 99 &&
    174       1.3  tsutsui 	    (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
    175       1.1  tsutsui 		year -= 100;
    176       1.4  tsutsui 	(*sc->sc_mcwrite)(sc, MC_YEAR, TOREG(year));
    177       1.4  tsutsui 
    178       1.4  tsutsui #undef TOREG
    179       1.1  tsutsui 
    180       1.1  tsutsui 	todr_wenable(handle, 0);
    181       1.1  tsutsui 
    182       1.1  tsutsui 	splx(s);
    183       1.1  tsutsui 
    184       1.1  tsutsui 	return 0;
    185       1.1  tsutsui }
    186