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