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