Home | History | Annotate | Line # | Download | only in isa
mkclock_isa.c revision 1.4
      1  1.4  thorpej /*	$NetBSD: mkclock_isa.c,v 1.4 2002/10/02 04:49:10 thorpej 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.4  thorpej __KERNEL_RCSID(0, "$NetBSD: mkclock_isa.c,v 1.4 2002/10/02 04:49:10 thorpej 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.1      scw 
     57  1.1      scw #include <dev/isa/isavar.h>
     58  1.1      scw 
     59  1.1      scw 
     60  1.1      scw /* Offsets of registers into ISA I/O space */
     61  1.1      scw #define	MKCLOCK_STB0	0		/* Address low		*/
     62  1.1      scw #define	MKCLOCK_STB1	1		/* Address high		*/
     63  1.1      scw #define	MKCLOCK_DATA	3		/* Data port		*/
     64  1.1      scw 
     65  1.1      scw #define	MKCLOCK_NPORTS	(MKCLOCK_DATA - MKCLOCK_STB0 + 1)
     66  1.1      scw 
     67  1.1      scw 
     68  1.1      scw struct mkclock_isa_softc {
     69  1.1      scw 	struct device		sc_dev;		/* Base device */
     70  1.1      scw 
     71  1.1      scw 	bus_space_tag_t		sc_iot;		/* I/O space access */
     72  1.1      scw 	bus_space_handle_t	sc_ioh;
     73  1.1      scw 
     74  1.1      scw 	todr_chip_handle_t	sc_todr;	/* MI todr interface handle */
     75  1.1      scw };
     76  1.1      scw 
     77  1.1      scw 
     78  1.1      scw /* Autoconfiguration interface */
     79  1.1      scw int	mkclock_isa_match(struct device *, struct cfdata *, void *);
     80  1.1      scw void	mkclock_isa_attach(struct device *, struct device *, void *);
     81  1.1      scw 
     82  1.4  thorpej CFATTACH_DECL(mkclock_isa, sizeof (struct mkclock_isa_softc),
     83  1.4  thorpej     mkclock_isa_match, mkclock_isa_attach, NULL, NULL);
     84  1.1      scw 
     85  1.1      scw 
     86  1.1      scw /* mk48txx interface */
     87  1.1      scw uint8_t	mkclock_isa_nvrd(bus_space_tag_t, bus_space_handle_t, int);
     88  1.1      scw void	mkclock_isa_nvwr(bus_space_tag_t, bus_space_handle_t, int, uint8_t);
     89  1.1      scw 
     90  1.1      scw 
     91  1.1      scw int
     92  1.1      scw mkclock_isa_match(struct device *parent, struct cfdata *match, void *aux)
     93  1.1      scw {
     94  1.1      scw 	struct isa_attach_args *ia = aux;
     95  1.1      scw 	bus_space_handle_t ioh;
     96  1.1      scw 	uint8_t csr, ocsr;
     97  1.1      scw 	unsigned int t1, t2;
     98  1.1      scw 	int found;
     99  1.1      scw 
    100  1.1      scw 	found = 0;
    101  1.1      scw 
    102  1.1      scw 	if (ia->ia_nio < 1 ||
    103  1.1      scw 	    (ia->ia_io[0].ir_addr != ISACF_PORT_DEFAULT &&
    104  1.1      scw 	     ia->ia_io[0].ir_addr != 0x74))
    105  1.1      scw 		return (0);
    106  1.1      scw 
    107  1.1      scw         if (ia->ia_niomem > 0 &&
    108  1.1      scw 	    (ia->ia_iomem[0].ir_addr != ISACF_IOMEM_DEFAULT))
    109  1.1      scw 		return (0);
    110  1.1      scw 
    111  1.1      scw 	if (ia->ia_nirq > 0 &&
    112  1.1      scw 	    (ia->ia_irq[0].ir_irq != ISACF_IRQ_DEFAULT))
    113  1.1      scw 		return (0);
    114  1.1      scw 
    115  1.1      scw 	if (ia->ia_ndrq > 0 &&
    116  1.1      scw 	    (ia->ia_drq[0].ir_drq != ISACF_DRQ_DEFAULT))
    117  1.1      scw 		return (0);
    118  1.1      scw 
    119  1.1      scw 	/*
    120  1.1      scw 	 * Map I/O space, then try to determine if it's really there.
    121  1.1      scw 	 */
    122  1.1      scw 	if (bus_space_map(ia->ia_iot, 0x74, MKCLOCK_NPORTS, 0, &ioh))
    123  1.1      scw 		return (0);
    124  1.1      scw 
    125  1.1      scw 	/* Supposedly no control bits are set after POST; check for this. */
    126  1.1      scw 	ocsr = mkclock_isa_nvrd(ia->ia_iot, ioh, MK48T18_CLKOFF + MK48TXX_ICSR);
    127  1.1      scw 	if (ocsr != 0)
    128  1.1      scw 		goto unmap;
    129  1.1      scw 
    130  1.1      scw 	/* Set clock data to read mode, prohibiting updates from clock. */
    131  1.1      scw 	csr = ocsr | MK48TXX_CSR_READ;
    132  1.1      scw 	mkclock_isa_nvwr(ia->ia_iot, ioh, MK48T18_CLKOFF + MK48TXX_ICSR, csr);
    133  1.1      scw 	/* Compare. */
    134  1.1      scw 	if (mkclock_isa_nvrd(ia->ia_iot, ioh, MK48T18_CLKOFF + MK48TXX_ICSR)
    135  1.1      scw 	    != csr)
    136  1.1      scw 		goto restore;
    137  1.1      scw 
    138  1.1      scw 	/* Read from the seconds counter. */
    139  1.1      scw 	t1 = FROMBCD(mkclock_isa_nvrd(ia->ia_iot, ioh,
    140  1.1      scw 	    MK48T18_CLKOFF + MK48TXX_ISEC));
    141  1.1      scw 	if (t1 > 59)
    142  1.1      scw 		goto restore;
    143  1.1      scw 
    144  1.1      scw 	/* Make it tick again, wait, then look again. */
    145  1.1      scw 	mkclock_isa_nvwr(ia->ia_iot, ioh, MK48T18_CLKOFF + MK48TXX_ICSR, ocsr);
    146  1.1      scw 	DELAY(1100000);
    147  1.1      scw 	mkclock_isa_nvwr(ia->ia_iot, ioh, MK48T18_CLKOFF + MK48TXX_ICSR, csr);
    148  1.1      scw 	t2 = FROMBCD(mkclock_isa_nvrd(ia->ia_iot, ioh,
    149  1.1      scw 	    MK48T18_CLKOFF + MK48TXX_ISEC));
    150  1.1      scw 	if (t2 > 59)
    151  1.1      scw 		goto restore;
    152  1.1      scw 
    153  1.1      scw 	/* If [1,2) seconds have passed since, call it a clock. */
    154  1.1      scw 	if ((t1 + 1) % 60 == t2 || (t1 + 2) % 60 == t2)
    155  1.1      scw 		found = 1;
    156  1.1      scw 
    157  1.1      scw  restore:
    158  1.1      scw 	mkclock_isa_nvwr(ia->ia_iot, ioh, MK48T18_CLKOFF + MK48TXX_ICSR, ocsr);
    159  1.1      scw  unmap:
    160  1.1      scw 	bus_space_unmap(ia->ia_iot, ioh, MKCLOCK_NPORTS);
    161  1.1      scw 
    162  1.1      scw 	if (found) {
    163  1.1      scw 		ia->ia_nio = 1;
    164  1.1      scw 		ia->ia_io[0].ir_addr = 0x74;
    165  1.1      scw 		ia->ia_io[0].ir_size = MKCLOCK_NPORTS;
    166  1.1      scw 
    167  1.1      scw 		ia->ia_niomem = 0;
    168  1.1      scw 		ia->ia_nirq = 0;
    169  1.1      scw 		ia->ia_ndrq = 0;
    170  1.1      scw 	}
    171  1.1      scw 
    172  1.1      scw 	return (found);
    173  1.1      scw }
    174  1.1      scw 
    175  1.1      scw void
    176  1.1      scw mkclock_isa_attach(struct device *parent, struct device *self, void *aux)
    177  1.1      scw {
    178  1.1      scw 	struct isa_attach_args *ia = aux;
    179  1.1      scw 	struct mkclock_isa_softc *sc = (struct mkclock_isa_softc *)self;
    180  1.1      scw 	extern void clock_rtc_config(todr_chip_handle_t);
    181  1.1      scw 
    182  1.1      scw 	/* Map I/O space. */
    183  1.1      scw 	sc->sc_iot = ia->ia_iot;
    184  1.1      scw 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
    185  1.1      scw 	    ia->ia_io[0].ir_size, 0, &sc->sc_ioh))
    186  1.1      scw 		panic("mkclock_isa_attach: couldn't map clock I/O space");
    187  1.1      scw 
    188  1.1      scw 	/* Attach to MI mk48txx driver. */
    189  1.1      scw 	sc->sc_todr = mk48txx_attach(sc->sc_iot, sc->sc_ioh, "mk48t18", 1968,
    190  1.1      scw 	    mkclock_isa_nvrd, mkclock_isa_nvwr);
    191  1.1      scw 	if (sc->sc_todr == NULL)
    192  1.2      wiz 		panic("\nmkclock_isa_attach: mk48txx attach failed");
    193  1.1      scw 
    194  1.1      scw 	printf(" Timekeeper NVRAM/RTC\n");
    195  1.1      scw 
    196  1.1      scw 	clock_rtc_config(sc->sc_todr);
    197  1.1      scw }
    198  1.1      scw 
    199  1.1      scw /*
    200  1.1      scw  * Bus access methods for MI mk48txx driver.
    201  1.1      scw  */
    202  1.1      scw uint8_t
    203  1.1      scw mkclock_isa_nvrd(bus_space_tag_t iot, bus_space_handle_t ioh, int off)
    204  1.1      scw {
    205  1.1      scw 	uint8_t datum;
    206  1.1      scw 	int s;
    207  1.1      scw 
    208  1.1      scw 	s = splclock();
    209  1.1      scw 	bus_space_write_1(iot, ioh, MKCLOCK_STB0, off & 0xff);
    210  1.1      scw 	bus_space_write_1(iot, ioh, MKCLOCK_STB1, off >> 8);
    211  1.1      scw 	datum = bus_space_read_1(iot, ioh, MKCLOCK_DATA);
    212  1.1      scw 	splx(s);
    213  1.1      scw 
    214  1.1      scw 	return (datum);
    215  1.1      scw }
    216  1.1      scw 
    217  1.1      scw void
    218  1.1      scw mkclock_isa_nvwr(bus_space_tag_t iot, bus_space_handle_t ioh, int off,
    219  1.1      scw                  uint8_t datum)
    220  1.1      scw {
    221  1.1      scw 	int s;
    222  1.1      scw 
    223  1.1      scw 	s = splclock();
    224  1.1      scw 	bus_space_write_1(iot, ioh, MKCLOCK_STB0, off & 0xff);
    225  1.1      scw 	bus_space_write_1(iot, ioh, MKCLOCK_STB1, off >> 8);
    226  1.1      scw 	bus_space_write_1(iot, ioh, MKCLOCK_DATA, datum);
    227  1.1      scw 	splx(s);
    228  1.1      scw }
    229