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