Home | History | Annotate | Line # | Download | only in isa
mkclock_isa.c revision 1.9.50.1
      1  1.9.50.1      matt /*	mkclock_isa.c,v 1.9 2005/12/11 12:18:20 christos Exp	*/
      2       1.1       scw 
      3       1.1       scw /*-
      4       1.1       scw  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5       1.1       scw  * All rights reserved.
      6       1.1       scw  *
      7       1.1       scw  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1       scw  * by Klaus J. Klein.
      9       1.1       scw  *
     10       1.1       scw  * Redistribution and use in source and binary forms, with or without
     11       1.1       scw  * modification, are permitted provided that the following conditions
     12       1.1       scw  * are met:
     13       1.1       scw  * 1. Redistributions of source code must retain the above copyright
     14       1.1       scw  *    notice, this list of conditions and the following disclaimer.
     15       1.1       scw  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       scw  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       scw  *    documentation and/or other materials provided with the distribution.
     18       1.1       scw  * 3. All advertising materials mentioning features or use of this software
     19       1.1       scw  *    must display the following acknowledgement:
     20       1.1       scw  *	This product includes software developed by the NetBSD
     21       1.1       scw  *	Foundation, Inc. and its contributors.
     22       1.1       scw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1       scw  *    contributors may be used to endorse or promote products derived
     24       1.1       scw  *    from this software without specific prior written permission.
     25       1.1       scw  *
     26       1.1       scw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1       scw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1       scw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1       scw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1       scw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1       scw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1       scw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1       scw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1       scw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1       scw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1       scw  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1       scw  */
     38       1.1       scw 
     39       1.1       scw /*
     40       1.1       scw  * Mostek MK48T18 time-of-day chip attachment to ISA bus, using two
     41       1.1       scw  * 8-bit ports for address selection and one 8-bit port for data.
     42       1.1       scw  */
     43       1.1       scw 
     44       1.1       scw #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     45  1.9.50.1      matt __KERNEL_RCSID(0, "mkclock_isa.c,v 1.9 2005/12/11 12:18:20 christos Exp");
     46       1.1       scw 
     47       1.1       scw #include <sys/param.h>
     48       1.1       scw #include <sys/kernel.h>
     49       1.1       scw #include <sys/systm.h>
     50       1.1       scw #include <sys/device.h>
     51       1.1       scw 
     52       1.1       scw #include <machine/bus.h>
     53       1.1       scw 
     54       1.1       scw #include <dev/clock_subr.h>
     55       1.1       scw #include <dev/ic/mk48txxreg.h>
     56       1.7   tsutsui #include <dev/ic/mk48txxvar.h>
     57       1.1       scw 
     58       1.1       scw #include <dev/isa/isavar.h>
     59       1.1       scw 
     60       1.1       scw 
     61       1.1       scw /* Offsets of registers into ISA I/O space */
     62       1.1       scw #define	MKCLOCK_STB0	0		/* Address low		*/
     63       1.1       scw #define	MKCLOCK_STB1	1		/* Address high		*/
     64       1.1       scw #define	MKCLOCK_DATA	3		/* Data port		*/
     65       1.1       scw 
     66       1.1       scw #define	MKCLOCK_NPORTS	(MKCLOCK_DATA - MKCLOCK_STB0 + 1)
     67       1.1       scw 
     68       1.1       scw 
     69       1.1       scw /* Autoconfiguration interface */
     70       1.1       scw int	mkclock_isa_match(struct device *, struct cfdata *, void *);
     71       1.1       scw void	mkclock_isa_attach(struct device *, struct device *, void *);
     72       1.1       scw 
     73       1.7   tsutsui CFATTACH_DECL(mkclock_isa, sizeof (struct mk48txx_softc),
     74       1.4   thorpej     mkclock_isa_match, mkclock_isa_attach, NULL, NULL);
     75       1.1       scw 
     76       1.1       scw 
     77       1.1       scw /* mk48txx interface */
     78       1.7   tsutsui uint8_t	mkclock_isa_nvrd(struct mk48txx_softc *, int);
     79       1.7   tsutsui void	mkclock_isa_nvwr(struct mk48txx_softc *, int, uint8_t);
     80       1.1       scw 
     81       1.1       scw 
     82       1.1       scw int
     83       1.1       scw mkclock_isa_match(struct device *parent, struct cfdata *match, void *aux)
     84       1.1       scw {
     85       1.1       scw 	struct isa_attach_args *ia = aux;
     86       1.7   tsutsui 	struct mk48txx_softc mk48txx, *sc;
     87       1.1       scw 	uint8_t csr, ocsr;
     88       1.1       scw 	unsigned int t1, t2;
     89       1.1       scw 	int found;
     90       1.1       scw 
     91       1.1       scw 	found = 0;
     92       1.1       scw 
     93       1.1       scw 	if (ia->ia_nio < 1 ||
     94       1.8  drochner 	    (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
     95       1.1       scw 	     ia->ia_io[0].ir_addr != 0x74))
     96       1.1       scw 		return (0);
     97       1.1       scw 
     98       1.1       scw         if (ia->ia_niomem > 0 &&
     99       1.8  drochner 	    (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
    100       1.1       scw 		return (0);
    101       1.1       scw 
    102       1.1       scw 	if (ia->ia_nirq > 0 &&
    103       1.8  drochner 	    (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ))
    104       1.1       scw 		return (0);
    105       1.1       scw 
    106       1.1       scw 	if (ia->ia_ndrq > 0 &&
    107       1.8  drochner 	    (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
    108       1.1       scw 		return (0);
    109       1.1       scw 
    110       1.1       scw 	/*
    111       1.1       scw 	 * Map I/O space, then try to determine if it's really there.
    112       1.1       scw 	 */
    113       1.7   tsutsui 	sc = &mk48txx;
    114       1.7   tsutsui 	sc->sc_bst = ia->ia_iot;
    115       1.7   tsutsui 	if (bus_space_map(sc->sc_bst, 0x74, MKCLOCK_NPORTS, 0, &sc->sc_bsh))
    116       1.1       scw 		return (0);
    117       1.1       scw 
    118       1.1       scw 	/* Supposedly no control bits are set after POST; check for this. */
    119       1.7   tsutsui 	ocsr = mkclock_isa_nvrd(sc, MK48T18_CLKOFF + MK48TXX_ICSR);
    120       1.1       scw 	if (ocsr != 0)
    121       1.1       scw 		goto unmap;
    122       1.1       scw 
    123       1.1       scw 	/* Set clock data to read mode, prohibiting updates from clock. */
    124       1.1       scw 	csr = ocsr | MK48TXX_CSR_READ;
    125       1.7   tsutsui 	mkclock_isa_nvwr(sc, MK48T18_CLKOFF + MK48TXX_ICSR, csr);
    126       1.1       scw 	/* Compare. */
    127       1.7   tsutsui 	if (mkclock_isa_nvrd(sc, MK48T18_CLKOFF + MK48TXX_ICSR) != csr)
    128       1.1       scw 		goto restore;
    129       1.1       scw 
    130       1.1       scw 	/* Read from the seconds counter. */
    131       1.7   tsutsui 	t1 = FROMBCD(mkclock_isa_nvrd(sc, MK48T18_CLKOFF + MK48TXX_ISEC));
    132       1.1       scw 	if (t1 > 59)
    133       1.1       scw 		goto restore;
    134       1.1       scw 
    135       1.1       scw 	/* Make it tick again, wait, then look again. */
    136       1.7   tsutsui 	mkclock_isa_nvwr(sc, MK48T18_CLKOFF + MK48TXX_ICSR, ocsr);
    137       1.1       scw 	DELAY(1100000);
    138       1.7   tsutsui 	mkclock_isa_nvwr(sc, MK48T18_CLKOFF + MK48TXX_ICSR, csr);
    139       1.7   tsutsui 	t2 = FROMBCD(mkclock_isa_nvrd(sc, MK48T18_CLKOFF + MK48TXX_ISEC));
    140       1.1       scw 	if (t2 > 59)
    141       1.1       scw 		goto restore;
    142       1.1       scw 
    143       1.1       scw 	/* If [1,2) seconds have passed since, call it a clock. */
    144       1.1       scw 	if ((t1 + 1) % 60 == t2 || (t1 + 2) % 60 == t2)
    145       1.1       scw 		found = 1;
    146       1.1       scw 
    147       1.1       scw  restore:
    148       1.7   tsutsui 	mkclock_isa_nvwr(sc, MK48T18_CLKOFF + MK48TXX_ICSR, ocsr);
    149       1.1       scw  unmap:
    150       1.7   tsutsui 	bus_space_unmap(sc->sc_bst, sc->sc_bsh, MKCLOCK_NPORTS);
    151       1.1       scw 
    152       1.1       scw 	if (found) {
    153       1.1       scw 		ia->ia_nio = 1;
    154       1.1       scw 		ia->ia_io[0].ir_addr = 0x74;
    155       1.1       scw 		ia->ia_io[0].ir_size = MKCLOCK_NPORTS;
    156       1.1       scw 
    157       1.1       scw 		ia->ia_niomem = 0;
    158       1.1       scw 		ia->ia_nirq = 0;
    159       1.1       scw 		ia->ia_ndrq = 0;
    160       1.1       scw 	}
    161       1.1       scw 
    162       1.1       scw 	return (found);
    163       1.1       scw }
    164       1.1       scw 
    165       1.1       scw void
    166       1.1       scw mkclock_isa_attach(struct device *parent, struct device *self, void *aux)
    167       1.1       scw {
    168       1.1       scw 	struct isa_attach_args *ia = aux;
    169       1.7   tsutsui 	struct mk48txx_softc *sc = (void *)self;
    170       1.1       scw 
    171       1.1       scw 	/* Map I/O space. */
    172       1.7   tsutsui 	sc->sc_bst = ia->ia_iot;
    173       1.7   tsutsui 	if (bus_space_map(sc->sc_bst, ia->ia_io[0].ir_addr,
    174       1.7   tsutsui 	    ia->ia_io[0].ir_size, 0, &sc->sc_bsh))
    175       1.1       scw 		panic("mkclock_isa_attach: couldn't map clock I/O space");
    176       1.1       scw 
    177       1.1       scw 	/* Attach to MI mk48txx driver. */
    178       1.7   tsutsui 	sc->sc_model = "mk48t18";
    179       1.7   tsutsui 	sc->sc_year0 = 1968;
    180       1.7   tsutsui 	sc->sc_nvrd = mkclock_isa_nvrd;
    181       1.7   tsutsui 	sc->sc_nvwr = mkclock_isa_nvwr;
    182       1.7   tsutsui 
    183       1.7   tsutsui 	mk48txx_attach(sc);
    184       1.1       scw 
    185       1.1       scw 	printf(" Timekeeper NVRAM/RTC\n");
    186       1.1       scw }
    187       1.1       scw 
    188       1.1       scw /*
    189       1.1       scw  * Bus access methods for MI mk48txx driver.
    190       1.1       scw  */
    191       1.1       scw uint8_t
    192       1.7   tsutsui mkclock_isa_nvrd(struct mk48txx_softc *sc, int off)
    193       1.1       scw {
    194       1.7   tsutsui 	bus_space_tag_t iot;
    195       1.7   tsutsui 	bus_space_handle_t ioh;
    196       1.1       scw 	uint8_t datum;
    197       1.1       scw 	int s;
    198       1.1       scw 
    199       1.7   tsutsui 	iot = sc->sc_bst;
    200       1.7   tsutsui 	ioh = sc->sc_bsh;
    201       1.7   tsutsui 
    202       1.1       scw 	s = splclock();
    203       1.1       scw 	bus_space_write_1(iot, ioh, MKCLOCK_STB0, off & 0xff);
    204       1.1       scw 	bus_space_write_1(iot, ioh, MKCLOCK_STB1, off >> 8);
    205       1.1       scw 	datum = bus_space_read_1(iot, ioh, MKCLOCK_DATA);
    206       1.1       scw 	splx(s);
    207       1.1       scw 
    208       1.1       scw 	return (datum);
    209       1.1       scw }
    210       1.1       scw 
    211       1.1       scw void
    212       1.7   tsutsui mkclock_isa_nvwr(struct mk48txx_softc *sc, int off, uint8_t datum)
    213       1.1       scw {
    214       1.7   tsutsui 	bus_space_tag_t iot;
    215       1.7   tsutsui 	bus_space_handle_t ioh;
    216       1.1       scw 	int s;
    217       1.7   tsutsui 
    218       1.7   tsutsui 	iot = sc->sc_bst;
    219       1.7   tsutsui 	ioh = sc->sc_bsh;
    220       1.1       scw 
    221       1.1       scw 	s = splclock();
    222       1.1       scw 	bus_space_write_1(iot, ioh, MKCLOCK_STB0, off & 0xff);
    223       1.1       scw 	bus_space_write_1(iot, ioh, MKCLOCK_STB1, off >> 8);
    224       1.1       scw 	bus_space_write_1(iot, ioh, MKCLOCK_DATA, datum);
    225       1.1       scw 	splx(s);
    226       1.1       scw }
    227