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