Home | History | Annotate | Line # | Download | only in dev
      1  1.9   thorpej /*	$NetBSD: dsclock.c,v 1.9 2025/09/07 21:45:14 thorpej Exp $	*/
      2  1.1    rumble 
      3  1.1    rumble /*
      4  1.1    rumble  * Copyright (c) 2001 Rafal K. Boni
      5  1.1    rumble  * Copyright (c) 2001 Christopher Sekiya
      6  1.1    rumble  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      7  1.1    rumble  * All rights reserved.
      8  1.1    rumble  *
      9  1.1    rumble  * Portions of this code are derived from software contributed to The
     10  1.1    rumble  * NetBSD Foundation by Jason R. Thorpe of the Numerical Aerospace
     11  1.1    rumble  * Simulation Facility, NASA Ames Research Center.
     12  1.1    rumble  *
     13  1.1    rumble  * Redistribution and use in source and binary forms, with or without
     14  1.1    rumble  * modification, are permitted provided that the following conditions
     15  1.1    rumble  * are met:
     16  1.1    rumble  * 1. Redistributions of source code must retain the above copyright
     17  1.1    rumble  *    notice, this list of conditions and the following disclaimer.
     18  1.1    rumble  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1    rumble  *    notice, this list of conditions and the following disclaimer in the
     20  1.1    rumble  *    documentation and/or other materials provided with the distribution.
     21  1.1    rumble  * 3. The name of the author may not be used to endorse or promote products
     22  1.1    rumble  *    derived from this software without specific prior written permission.
     23  1.1    rumble  *
     24  1.1    rumble  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  1.1    rumble  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  1.1    rumble  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  1.1    rumble  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  1.1    rumble  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  1.1    rumble  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  1.1    rumble  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  1.1    rumble  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  1.1    rumble  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  1.1    rumble  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  1.1    rumble  */
     35  1.1    rumble 
     36  1.1    rumble #include <sys/cdefs.h>
     37  1.9   thorpej __KERNEL_RCSID(0, "$NetBSD: dsclock.c,v 1.9 2025/09/07 21:45:14 thorpej Exp $");
     38  1.1    rumble 
     39  1.1    rumble #include <sys/param.h>
     40  1.1    rumble #include <sys/kernel.h>
     41  1.1    rumble #include <sys/systm.h>
     42  1.1    rumble #include <sys/device.h>
     43  1.1    rumble 
     44  1.5    dyoung #include <sys/bus.h>
     45  1.1    rumble #include <machine/autoconf.h>
     46  1.1    rumble #include <machine/sysconf.h>
     47  1.1    rumble #include <machine/machtype.h>
     48  1.1    rumble 
     49  1.1    rumble #include <dev/clock_subr.h>
     50  1.1    rumble #include <dev/ic/ds1286reg.h>
     51  1.1    rumble 
     52  1.1    rumble #include <sgimips/sgimips/clockvar.h>
     53  1.1    rumble 
     54  1.1    rumble struct dsclock_softc {
     55  1.4   tsutsui 	device_t sc_dev;
     56  1.1    rumble 
     57  1.1    rumble 	struct todr_chip_handle sc_todrch;
     58  1.1    rumble 
     59  1.1    rumble 	/* RTC registers */
     60  1.1    rumble 	bus_space_tag_t sc_rtct;
     61  1.1    rumble 	bus_space_handle_t sc_rtch;
     62  1.1    rumble };
     63  1.1    rumble 
     64  1.4   tsutsui static int	dsclock_match(device_t, cfdata_t, void *);
     65  1.4   tsutsui static void	dsclock_attach(device_t, device_t, void *);
     66  1.3   tsutsui static int	dsclock_gettime_ymdhms(struct todr_chip_handle *,
     67  1.3   tsutsui 		    struct clock_ymdhms *);
     68  1.3   tsutsui static int	dsclock_settime_ymdhms(struct todr_chip_handle *,
     69  1.3   tsutsui 		    struct clock_ymdhms *);
     70  1.1    rumble 
     71  1.4   tsutsui CFATTACH_DECL_NEW(dsclock, sizeof(struct dsclock_softc),
     72  1.1    rumble     dsclock_match, dsclock_attach, NULL, NULL);
     73  1.1    rumble 
     74  1.4   tsutsui #define	ds1286_write(sc, reg, datum)					\
     75  1.7  macallan     bus_space_write_1((sc)->sc_rtct, (sc)->sc_rtch, (reg << 2) + 3, (datum))
     76  1.4   tsutsui #define	ds1286_read(sc, reg)						\
     77  1.7  macallan     bus_space_read_1((sc)->sc_rtct, (sc)->sc_rtch, (reg << 2) + 3)
     78  1.1    rumble 
     79  1.1    rumble static int
     80  1.4   tsutsui dsclock_match(device_t parent, cfdata_t cf, void *aux)
     81  1.1    rumble {
     82  1.1    rumble 	struct mainbus_attach_args *ma = aux;
     83  1.1    rumble 
     84  1.1    rumble 	if (mach_type == MACH_SGI_IP22 && ma->ma_addr == 0x1fbe0000)
     85  1.4   tsutsui 		return 1;
     86  1.1    rumble 
     87  1.4   tsutsui 	return 0;
     88  1.1    rumble }
     89  1.1    rumble 
     90  1.1    rumble static void
     91  1.4   tsutsui dsclock_attach(device_t parent, device_t self, void *aux)
     92  1.1    rumble {
     93  1.4   tsutsui 	struct dsclock_softc *sc = device_private(self);
     94  1.1    rumble 	struct mainbus_attach_args *ma = aux;
     95  1.1    rumble 	int err;
     96  1.1    rumble 
     97  1.4   tsutsui 	aprint_normal("\n");
     98  1.1    rumble 
     99  1.9   thorpej 	sc->sc_dev = self;
    100  1.7  macallan 	sc->sc_rtct = normal_memt;
    101  1.1    rumble 	if ((err = bus_space_map(sc->sc_rtct, ma->ma_addr, 0x1ffff,
    102  1.1    rumble 	    BUS_SPACE_MAP_LINEAR, &sc->sc_rtch)) != 0) {
    103  1.4   tsutsui 		aprint_error_dev(self,
    104  1.4   tsutsui 		    "unable to map RTC registers, error = %d\n", err);
    105  1.1    rumble 		return;
    106  1.1    rumble 	}
    107  1.1    rumble 
    108  1.9   thorpej 	sc->sc_todrch.todr_dev = self;
    109  1.3   tsutsui 	sc->sc_todrch.todr_gettime_ymdhms = dsclock_gettime_ymdhms;
    110  1.3   tsutsui 	sc->sc_todrch.todr_settime_ymdhms = dsclock_settime_ymdhms;
    111  1.1    rumble 
    112  1.1    rumble 	todr_attach(&sc->sc_todrch);
    113  1.1    rumble }
    114  1.1    rumble 
    115  1.1    rumble /*
    116  1.1    rumble  * Get the time of day, based on the clock's value and/or the base value.
    117  1.1    rumble  */
    118  1.1    rumble static int
    119  1.3   tsutsui dsclock_gettime_ymdhms(struct todr_chip_handle *todrch, struct clock_ymdhms *dt)
    120  1.1    rumble {
    121  1.9   thorpej 	struct dsclock_softc *sc = device_private(todrch->todr_dev);
    122  1.1    rumble 	ds1286_todregs regs;
    123  1.1    rumble 	int s;
    124  1.1    rumble 
    125  1.1    rumble 	s = splhigh();
    126  1.1    rumble 	DS1286_GETTOD(sc, &regs)
    127  1.1    rumble 	splx(s);
    128  1.1    rumble 
    129  1.6  christos 	dt->dt_sec = bcdtobin(regs[DS1286_SEC]);
    130  1.6  christos 	dt->dt_min = bcdtobin(regs[DS1286_MIN]);
    131  1.1    rumble 
    132  1.1    rumble 	if (regs[DS1286_HOUR] & DS1286_HOUR_12MODE) {
    133  1.3   tsutsui 		dt->dt_hour =
    134  1.6  christos 		    bcdtobin(regs[DS1286_HOUR] & DS1286_HOUR_12HR_MASK)
    135  1.3   tsutsui 		    + ((regs[DS1286_HOUR] & DS1286_HOUR_12HR_PM) ? 12 : 0);
    136  1.1    rumble 
    137  1.1    rumble 		/*
    138  1.1    rumble 		 * In AM/PM mode, hour range is 01-12, so adding in 12 hours
    139  1.1    rumble 		 * for PM gives us 01-24, whereas we want 00-23, so map hour
    140  1.1    rumble 		 * 24 to hour 0.
    141  1.1    rumble 		 */
    142  1.3   tsutsui 		if (dt->dt_hour == 24)
    143  1.3   tsutsui 			dt->dt_hour = 0;
    144  1.1    rumble 	} else {
    145  1.3   tsutsui 		 dt->dt_hour =
    146  1.6  christos 		    bcdtobin(regs[DS1286_HOUR] & DS1286_HOUR_24HR_MASK);
    147  1.1    rumble 	}
    148  1.1    rumble 
    149  1.6  christos 	dt->dt_wday = bcdtobin(regs[DS1286_DOW]);
    150  1.6  christos 	dt->dt_day = bcdtobin(regs[DS1286_DOM]);
    151  1.6  christos 	dt->dt_mon = bcdtobin(regs[DS1286_MONTH] & DS1286_MONTH_MASK);
    152  1.6  christos 	dt->dt_year = FROM_IRIX_YEAR(bcdtobin(regs[DS1286_YEAR]));
    153  1.1    rumble 
    154  1.4   tsutsui 	return 0;
    155  1.1    rumble }
    156  1.1    rumble 
    157  1.1    rumble /*
    158  1.1    rumble  * Reset the TODR based on the time value.
    159  1.1    rumble  */
    160  1.1    rumble static int
    161  1.3   tsutsui dsclock_settime_ymdhms(struct todr_chip_handle *todrch, struct clock_ymdhms *dt)
    162  1.1    rumble {
    163  1.9   thorpej 	struct dsclock_softc *sc = device_private(todrch->todr_dev);
    164  1.1    rumble 	ds1286_todregs regs;
    165  1.1    rumble 	int s;
    166  1.1    rumble 
    167  1.1    rumble 	s = splhigh();
    168  1.1    rumble 	DS1286_GETTOD(sc, &regs);
    169  1.1    rumble 	splx(s);
    170  1.1    rumble 
    171  1.1    rumble 	regs[DS1286_SUBSEC] = 0;
    172  1.6  christos 	regs[DS1286_SEC] = bintobcd(dt->dt_sec);
    173  1.6  christos 	regs[DS1286_MIN] = bintobcd(dt->dt_min);
    174  1.6  christos 	regs[DS1286_HOUR] = bintobcd(dt->dt_hour) & DS1286_HOUR_24HR_MASK;
    175  1.6  christos 	regs[DS1286_DOW] = bintobcd(dt->dt_wday);
    176  1.6  christos 	regs[DS1286_DOM] = bintobcd(dt->dt_day);
    177  1.1    rumble 
    178  1.1    rumble 	/* Leave wave-generator bits as set originally */
    179  1.1    rumble 	regs[DS1286_MONTH] &=  ~DS1286_MONTH_MASK;
    180  1.6  christos 	regs[DS1286_MONTH] |=  bintobcd(dt->dt_mon) & DS1286_MONTH_MASK;
    181  1.1    rumble 
    182  1.6  christos 	regs[DS1286_YEAR] = bintobcd(TO_IRIX_YEAR(dt->dt_year));
    183  1.1    rumble 
    184  1.1    rumble 	s = splhigh();
    185  1.1    rumble 	DS1286_PUTTOD(sc, &regs);
    186  1.1    rumble 	splx(s);
    187  1.1    rumble 
    188  1.4   tsutsui 	return 0;
    189  1.1    rumble }
    190