Home | History | Annotate | Line # | Download | only in pnpbus
mcclock_pnpbus.c revision 1.2
      1 /* $NetBSD: mcclock_pnpbus.c,v 1.2 2006/06/23 03:08:41 garbled 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  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * PNPBus driver for the mc146818 compatible time-of-day clock found on all
     40  * IBM Prep machines.  This one is only discernable from the motorola clock
     41  * part by the interface portion of the residual data. (or by the small vendor
     42  * item chip id)
     43  * NOTE: The IBM machines actually have either a dallas 1385 or a dallas 1585
     44  * chip.  This driver should probably be replaced by something that does that
     45  * one day.
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: mcclock_pnpbus.c,v 1.2 2006/06/23 03:08:41 garbled Exp $");
     50 
     51 #include <sys/types.h>
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/device.h>
     55 #include <sys/malloc.h>
     56 
     57 #include <machine/bus.h>
     58 #include <machine/intr.h>
     59 #include <machine/isa_machdep.h>
     60 #include <machine/residual.h>
     61 #include <machine/chpidpnp.h>
     62 
     63 #include <dev/clock_subr.h>
     64 #include <dev/ic/mc146818reg.h>
     65 #include <dev/ic/mc146818var.h>
     66 
     67 #include <dev/isa/isavar.h>
     68 
     69 #include <prep/pnpbus/pnpbusvar.h>
     70 
     71 static int	mcclock_pnpbus_probe(struct device *, struct cfdata *, void *);
     72 static void	mcclock_pnpbus_attach(struct device *, struct device *, void *);
     73 
     74 CFATTACH_DECL(mcclock_pnpbus, sizeof (struct mc146818_softc),
     75     mcclock_pnpbus_probe, mcclock_pnpbus_attach, NULL, NULL);
     76 
     77 void	mcclock_pnpbus_write(struct mc146818_softc *, u_int, u_int);
     78 u_int	mcclock_pnpbus_read(struct mc146818_softc *, u_int);
     79 
     80 static int
     81 mcclock_pnpbus_probe(struct device *parent, struct cfdata *match, void *aux)
     82 {
     83 	struct pnpbus_dev_attach_args *pna = aux;
     84 	int ret = 0;
     85 
     86 	if (strcmp(pna->pna_devid, "PNP0B00") == 0 &&
     87 	    pna->subtype == RealTimeClock && pna->chipid == DallasRTC)
     88 		ret = 1;
     89 	if (strcmp(pna->pna_devid, "PNP0B00") == 0 &&
     90 	    pna->subtype == RealTimeClock && pna->chipid == Dallas1585)
     91 		ret = 1;
     92 
     93 	if (ret)
     94 		pnpbus_scan(pna, pna->pna_ppc_dev);
     95 	return ret;
     96 }
     97 
     98 static void
     99 mcclock_pnpbus_attach(struct device *parent, struct device *self, void *aux)
    100 {
    101 	struct mc146818_softc *sc = (void *)self;
    102 	struct pnpbus_dev_attach_args *pna = aux;
    103 
    104 	sc->sc_bst = pna->pna_iot;
    105 	if (pnpbus_io_map(&pna->pna_res, 0, &sc->sc_bst, &sc->sc_bsh)) {
    106 		/* XXX should we panic instead? */
    107 		aprint_error("%s: couldn't map clock I/O space",
    108 		    device_xname(self));
    109 		return;
    110 	}
    111 
    112 	sc->sc_year0 = 1900;
    113 	sc->sc_mcread = mcclock_pnpbus_read;
    114 	sc->sc_mcwrite = mcclock_pnpbus_write;
    115 	sc->sc_flag = MC146818_BCD;
    116 	mc146818_attach(sc);
    117 
    118 	aprint_normal("\n");
    119 
    120 	(*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR);
    121 	todr_attach(&sc->sc_handle);
    122 }
    123 
    124 void
    125 mcclock_pnpbus_write(struct mc146818_softc *sc, u_int reg, u_int datum)
    126 {
    127 	bus_space_tag_t iot;
    128 	bus_space_handle_t ioh;
    129 
    130 	iot = sc->sc_bst;
    131 	ioh = sc->sc_bsh;
    132 
    133 	bus_space_write_1(iot, ioh, 0, reg);
    134 	bus_space_write_1(iot, ioh, 1, datum);
    135 }
    136 
    137 u_int
    138 mcclock_pnpbus_read(struct mc146818_softc *sc, u_int reg)
    139 {
    140 	bus_space_tag_t iot;
    141 	bus_space_handle_t ioh;
    142 	u_int datum;
    143 
    144 	iot = sc->sc_bst;
    145 	ioh = sc->sc_bsh;
    146 
    147 	bus_space_write_1(iot, ioh, 0, reg);
    148 	datum = bus_space_read_1(iot, ioh, 1);
    149 
    150 	return (datum);
    151 }
    152