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