Home | History | Annotate | Line # | Download | only in pnpbus
      1 /* $NetBSD: mcclock_pnpbus.c,v 1.11 2023/12/20 15:29:06 thorpej Exp $ */
      2 /*-
      3  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Tim Rightnour
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * PNPBus driver for the mc146818 compatible time-of-day clock found on all
     33  * IBM Prep machines.  This one is only discernable from the motorola clock
     34  * part by the interface portion of the residual data. (or by the small vendor
     35  * item chip id)
     36  * NOTE: The IBM machines actually have either a dallas 1385 or a dallas 1585
     37  * chip.  This driver should probably be replaced by something that does that
     38  * one day.
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: mcclock_pnpbus.c,v 1.11 2023/12/20 15:29:06 thorpej Exp $");
     43 
     44 #include <sys/types.h>
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/device.h>
     48 
     49 #include <sys/bus.h>
     50 #include <machine/intr.h>
     51 #include <machine/isa_machdep.h>
     52 #include <machine/residual.h>
     53 #include <machine/chpidpnp.h>
     54 /* XXX */
     55 #include <machine/pio.h>
     56 
     57 #include <dev/clock_subr.h>
     58 #include <dev/ic/mc146818reg.h>
     59 #include <dev/ic/mc146818var.h>
     60 #include <dev/ic/ds1687reg.h>
     61 
     62 #include <dev/isa/isavar.h>
     63 
     64 #include <prep/pnpbus/pnpbusvar.h>
     65 
     66 static int	mcclock_pnpbus_probe(device_t, cfdata_t, void *);
     67 static void	mcclock_pnpbus_attach(device_t, device_t, void *);
     68 
     69 extern struct cfdriver mcclock_cd;
     70 
     71 CFATTACH_DECL_NEW(mcclock_pnpbus, sizeof(struct mc146818_softc),
     72     mcclock_pnpbus_probe, mcclock_pnpbus_attach, NULL, NULL);
     73 
     74 void	mcclock_pnpbus_write(struct mc146818_softc *, u_int, u_int);
     75 u_int	mcclock_pnpbus_read(struct mc146818_softc *, u_int);
     76 void	ds1585_reboot(void);
     77 
     78 int have_ds1585 = 0;
     79 #define MCCLOCK_STD_DEV		0
     80 
     81 static int
     82 mcclock_pnpbus_probe(device_t parent, cfdata_t cf, void *aux)
     83 {
     84 	struct pnpbus_dev_attach_args *pna = aux;
     85 	int ret = 0;
     86 
     87 	if (strcmp(pna->pna_devid, "PNP0B00") == 0 &&
     88 	    pna->subtype == RealTimeClock && pna->chipid == DallasRTC)
     89 		ret = 1;
     90 	if (strcmp(pna->pna_devid, "PNP0B00") == 0 &&
     91 	    pna->subtype == RealTimeClock && pna->chipid == Dallas1585)
     92 		ret = 1;
     93 
     94 	if (ret)
     95 		pnpbus_scan(pna, pna->pna_ppc_dev);
     96 	return ret;
     97 }
     98 
     99 static void
    100 mcclock_pnpbus_attach(device_t parent, device_t self, void *aux)
    101 {
    102 	struct mc146818_softc *sc = device_private(self);
    103 	struct pnpbus_dev_attach_args *pna = aux;
    104 
    105 	sc->sc_dev = self;
    106 	sc->sc_bst = pna->pna_iot;
    107 	if (pnpbus_io_map(&pna->pna_res, 0, &sc->sc_bst, &sc->sc_bsh)) {
    108 		/* XXX should we panic instead? */
    109 		aprint_error(": couldn't map clock I/O space\n");
    110 		return;
    111 	}
    112 
    113 	if (pna->chipid == Dallas1585)
    114 		have_ds1585 = 1;
    115 	sc->sc_year0 = 1900;
    116 	sc->sc_mcread = mcclock_pnpbus_read;
    117 	sc->sc_mcwrite = mcclock_pnpbus_write;
    118 	sc->sc_flag = MC146818_BCD;
    119 	mc146818_attach(sc);
    120 
    121 	aprint_normal("\n");
    122 
    123 	(*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR);
    124 }
    125 
    126 void
    127 ds1585_reboot(void)
    128 {
    129 	struct mc146818_softc *sc;
    130 	int i, j;
    131 
    132 	if (!have_ds1585)
    133 		return;
    134 
    135 	sc = device_lookup_private(&mcclock_cd, MCCLOCK_STD_DEV);
    136 
    137 	/* monitors b0: 05,03,01  b1: 49, WIE=1 */
    138 
    139 	(*sc->sc_mcwrite)(sc, DS1687_ASEC, 0);
    140 
    141 	/* If we are nearing 59 minutes on the hour, the math is too complex
    142 	 * to compute out when to reboot, so we just wait for the clock to
    143 	 * flip back over to zero, then set the alarm time.
    144 	 */
    145 	i = (*sc->sc_mcread)(sc, DS1687_MIN);
    146 	if (i == 0x58) {
    147 		(*sc->sc_mcwrite)(sc, DS1687_AMIN, 0x59);
    148 		(*sc->sc_mcwrite)(sc, DS1687_ASEC, 0x59);
    149 		i = 0x59;
    150 	} else {
    151 		while (i == 0x59) {
    152 			delay(100);
    153 			i = (*sc->sc_mcread)(sc, DS1687_MIN);
    154 		}
    155 		i += 2;
    156 		if ((i & 0x0f) > 8)
    157 			i = (i & 0xf0) + 0x10;
    158 		(*sc->sc_mcwrite)(sc, DS1687_AMIN, i);
    159 	}
    160 	i = (*sc->sc_mcread)(sc, DS1687_HOUR);
    161 	(*sc->sc_mcwrite)(sc, DS1687_AHOUR, i);
    162 	j = (*sc->sc_mcread)(sc, DS1687_DOM); /* get date */
    163 	i = (*sc->sc_mcread)(sc, DS1687_CONTROLA);
    164 	i |= DS1687_BANK1; /* set DV0 on */
    165 	(*sc->sc_mcwrite)(sc, DS1687_CONTROLA, i);
    166 	/* write today into wakeup */
    167 	(*sc->sc_mcwrite)(sc, DS1687_BANK1_ADATE, j);
    168 	i = (*sc->sc_mcread)(sc, 0x4b); /* bank1 reg B */
    169 	i |= 0x82; /* WIE + ABE */
    170 	(*sc->sc_mcwrite)(sc, 0x4b, i);
    171 	i = (*sc->sc_mcread)(sc, 0x4b);
    172 	/* XXX power control bit on 7024/7025 */
    173 	outb(PREP_BUS_SPACE_IO + 0x856, 1);
    174 	for (;;);
    175 }
    176 
    177 void
    178 mcclock_pnpbus_write(struct mc146818_softc *sc, u_int reg, u_int datum)
    179 {
    180 	bus_space_tag_t iot;
    181 	bus_space_handle_t ioh;
    182 
    183 	iot = sc->sc_bst;
    184 	ioh = sc->sc_bsh;
    185 
    186 	bus_space_write_1(iot, ioh, 0, reg);
    187 	bus_space_write_1(iot, ioh, 1, datum);
    188 }
    189 
    190 u_int
    191 mcclock_pnpbus_read(struct mc146818_softc *sc, u_int reg)
    192 {
    193 	bus_space_tag_t iot;
    194 	bus_space_handle_t ioh;
    195 	u_int datum;
    196 
    197 	iot = sc->sc_bst;
    198 	ioh = sc->sc_bsh;
    199 
    200 	bus_space_write_1(iot, ioh, 0, reg);
    201 	datum = bus_space_read_1(iot, ioh, 1);
    202 
    203 	return (datum);
    204 }
    205