Home | History | Annotate | Line # | Download | only in ic
      1 /*	$NetBSD: mc146818.c,v 1.22 2025/09/07 21:45:16 thorpej 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  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 /*
     28  * mc146818 and compatible time of day chip subroutines
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: mc146818.c,v 1.22 2025/09/07 21:45:16 thorpej Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/device.h>
     37 #include <sys/errno.h>
     38 
     39 #include <sys/bus.h>
     40 
     41 #include <dev/clock_subr.h>
     42 
     43 #include <dev/ic/mc146818reg.h>
     44 #include <dev/ic/mc146818var.h>
     45 
     46 void
     47 mc146818_attach(struct mc146818_softc *sc)
     48 {
     49 	todr_chip_handle_t handle;
     50 
     51 #ifdef DIAGNOSTIC
     52 	if (sc->sc_mcread == NULL ||
     53 	    sc->sc_mcwrite == NULL)
     54 		panic("%s: invalid read/write functions", __func__);
     55 #endif
     56 
     57 	aprint_normal(": mc146818 compatible time-of-day clock");
     58 
     59 	handle = &sc->sc_handle;
     60 	handle->todr_dev = sc->sc_dev;
     61 	KASSERT(handle->todr_gettime == NULL);
     62 	KASSERT(handle->todr_settime == NULL);
     63 	if (handle->todr_gettime_ymdhms == NULL) {
     64 		handle->todr_gettime_ymdhms = mc146818_gettime_ymdhms;
     65 	}
     66 	if (handle->todr_settime_ymdhms == NULL) {
     67 		handle->todr_settime_ymdhms = mc146818_settime_ymdhms;
     68 	}
     69 
     70 	todr_attach(handle);
     71 }
     72 
     73 /*
     74  * todr_gettime function:
     75  *  Get time of day and convert it to a struct timeval.
     76  *  Return 0 on success, an error number othersize.
     77  */
     78 int
     79 mc146818_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
     80 {
     81 	struct mc146818_softc *sc = device_private(handle->todr_dev);
     82 	int s, timeout, cent, year;
     83 
     84 	s = splclock();		/* XXX really needed? */
     85 
     86 	timeout = 1000000;	/* XXX how long should we wait? */
     87 	for (;;) {
     88 		if (((*sc->sc_mcread)(sc, MC_REGA) & MC_REGA_UIP) == 0)
     89 			break;
     90 		if (--timeout < 0) {
     91 			printf("%s: timeout\n", __func__);
     92 			return EBUSY;
     93 		}
     94 	}
     95 
     96 #define	FROMREG(x)	((sc->sc_flag & MC146818_BCD) ? bcdtobin(x) : (x))
     97 
     98 	dt->dt_sec  = FROMREG((*sc->sc_mcread)(sc, MC_SEC));
     99 	dt->dt_min  = FROMREG((*sc->sc_mcread)(sc, MC_MIN));
    100 	dt->dt_hour = FROMREG((*sc->sc_mcread)(sc, MC_HOUR));
    101 	dt->dt_wday = FROMREG((*sc->sc_mcread)(sc, MC_DOW));
    102 	dt->dt_day  = FROMREG((*sc->sc_mcread)(sc, MC_DOM));
    103 	dt->dt_mon  = FROMREG((*sc->sc_mcread)(sc, MC_MONTH));
    104 	year        = FROMREG((*sc->sc_mcread)(sc, MC_YEAR));
    105 	if (sc->sc_getcent) {
    106 		cent = (*sc->sc_getcent)(sc);
    107 		year += cent * 100;
    108 	}
    109 
    110 #undef FROMREG
    111 
    112 	year += sc->sc_year0;
    113 	if (year < POSIX_BASE_YEAR &&
    114 	    (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
    115 		year += 100;
    116 	dt->dt_year = year;
    117 
    118 	splx(s);
    119 
    120 	return 0;
    121 }
    122 
    123 /*
    124  * todr_settime function:
    125  *  Set the time of day clock based on the value of the struct timeval arg.
    126  *  Return 0 on success, an error number othersize.
    127  */
    128 int
    129 mc146818_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt)
    130 {
    131 	struct mc146818_softc *sc = device_private(handle->todr_dev);
    132 	int s, cent, year;
    133 
    134 	s = splclock();		/* XXX really needed? */
    135 
    136 	/*
    137 	 * Disable RTC updates during clock updates
    138 	 */
    139 
    140 	(*sc->sc_mcwrite)(sc, MC_REGB,
    141 	    (*sc->sc_mcread)(sc, MC_REGB) | MC_REGB_SET);
    142 
    143 #define	TOREG(x)	((sc->sc_flag & MC146818_BCD) ? bintobcd(x) : (x))
    144 
    145 	(*sc->sc_mcwrite)(sc, MC_SEC, TOREG(dt->dt_sec));
    146 	(*sc->sc_mcwrite)(sc, MC_MIN, TOREG(dt->dt_min));
    147 	(*sc->sc_mcwrite)(sc, MC_HOUR, TOREG(dt->dt_hour));
    148 	(*sc->sc_mcwrite)(sc, MC_DOW, TOREG(dt->dt_wday));
    149 	(*sc->sc_mcwrite)(sc, MC_DOM, TOREG(dt->dt_day));
    150 	(*sc->sc_mcwrite)(sc, MC_MONTH, TOREG(dt->dt_mon));
    151 
    152 	year = dt->dt_year - sc->sc_year0;
    153 	if (sc->sc_setcent) {
    154 		cent = year / 100;
    155 		(*sc->sc_setcent)(sc, cent);
    156 		year -= cent * 100;
    157 	}
    158 	if (year > 99 &&
    159 	    (sc->sc_flag & MC146818_NO_CENT_ADJUST) == 0)
    160 		year -= 100;
    161 	(*sc->sc_mcwrite)(sc, MC_YEAR, TOREG(year));
    162 
    163 #undef TOREG
    164 
    165 	/*
    166 	 * Re-enable RTC updates
    167 	 */
    168 
    169 	(*sc->sc_mcwrite)(sc, MC_REGB,
    170 	    (*sc->sc_mcread)(sc, MC_REGB) & ~MC_REGB_SET);
    171 
    172 	splx(s);
    173 
    174 	return 0;
    175 }
    176