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