1 1.19 thorpej /* $NetBSD: depca.c,v 1.19 2021/08/07 16:19:12 thorpej Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /*- 4 1.1 thorpej * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc. 5 1.1 thorpej * All rights reserved. 6 1.1 thorpej * 7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation 8 1.1 thorpej * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 9 1.1 thorpej * Simulation Facility, NASA Ames Research Center. 10 1.1 thorpej * 11 1.1 thorpej * Redistribution and use in source and binary forms, with or without 12 1.1 thorpej * modification, are permitted provided that the following conditions 13 1.1 thorpej * are met: 14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 15 1.1 thorpej * notice, this list of conditions and the following disclaimer. 16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 18 1.1 thorpej * documentation and/or other materials provided with the distribution. 19 1.1 thorpej * 20 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE. 31 1.1 thorpej */ 32 1.1 thorpej 33 1.1 thorpej /*- 34 1.1 thorpej * Copyright (c) 1992, 1993 35 1.1 thorpej * The Regents of the University of California. All rights reserved. 36 1.1 thorpej * 37 1.1 thorpej * This code is derived from software contributed to Berkeley by 38 1.1 thorpej * Ralph Campbell and Rick Macklem. 39 1.1 thorpej * 40 1.1 thorpej * Redistribution and use in source and binary forms, with or without 41 1.1 thorpej * modification, are permitted provided that the following conditions 42 1.1 thorpej * are met: 43 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 44 1.1 thorpej * notice, this list of conditions and the following disclaimer. 45 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 46 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 47 1.1 thorpej * documentation and/or other materials provided with the distribution. 48 1.10 agc * 3. Neither the name of the University nor the names of its contributors 49 1.1 thorpej * may be used to endorse or promote products derived from this software 50 1.1 thorpej * without specific prior written permission. 51 1.1 thorpej * 52 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 53 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 56 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 1.1 thorpej * SUCH DAMAGE. 63 1.1 thorpej * 64 1.1 thorpej * @(#)if_le.c 8.2 (Berkeley) 11/16/93 65 1.1 thorpej */ 66 1.4 lukem 67 1.4 lukem #include <sys/cdefs.h> 68 1.19 thorpej __KERNEL_RCSID(0, "$NetBSD: depca.c,v 1.19 2021/08/07 16:19:12 thorpej Exp $"); 69 1.1 thorpej 70 1.1 thorpej #include <sys/param.h> 71 1.1 thorpej #include <sys/systm.h> 72 1.1 thorpej #include <sys/mbuf.h> 73 1.1 thorpej #include <sys/syslog.h> 74 1.1 thorpej #include <sys/socket.h> 75 1.1 thorpej #include <sys/device.h> 76 1.1 thorpej 77 1.1 thorpej #include <net/if.h> 78 1.1 thorpej #include <net/if_ether.h> 79 1.1 thorpej #include <net/if_media.h> 80 1.1 thorpej 81 1.15 ad #include <sys/cpu.h> 82 1.15 ad #include <sys/intr.h> 83 1.15 ad #include <sys/bus.h> 84 1.1 thorpej 85 1.1 thorpej #include <dev/isa/isareg.h> 86 1.1 thorpej #include <dev/isa/isavar.h> 87 1.1 thorpej 88 1.1 thorpej #include <dev/ic/lancereg.h> 89 1.1 thorpej #include <dev/ic/lancevar.h> 90 1.1 thorpej #include <dev/ic/am7990reg.h> 91 1.1 thorpej #include <dev/ic/am7990var.h> 92 1.1 thorpej #include <dev/ic/depcareg.h> 93 1.1 thorpej #include <dev/ic/depcavar.h> 94 1.1 thorpej 95 1.1 thorpej struct le_depca_softc { 96 1.1 thorpej struct am7990_softc sc_am7990; /* glue to MI code */ 97 1.1 thorpej 98 1.1 thorpej void *sc_ih; 99 1.1 thorpej }; 100 1.1 thorpej 101 1.16 tsutsui int le_depca_match(device_t, cfdata_t, void *); 102 1.16 tsutsui void le_depca_attach(device_t, device_t, void *); 103 1.1 thorpej 104 1.16 tsutsui CFATTACH_DECL_NEW(le_depca, sizeof(struct le_depca_softc), 105 1.8 thorpej le_depca_match, le_depca_attach, NULL, NULL); 106 1.1 thorpej 107 1.1 thorpej void depca_copytobuf(struct lance_softc *, void *, int, int); 108 1.1 thorpej void depca_copyfrombuf(struct lance_softc *, void *, int, int); 109 1.1 thorpej void depca_zerobuf(struct lance_softc *, int, int); 110 1.1 thorpej 111 1.1 thorpej struct depca_attach_args { 112 1.1 thorpej const char *da_name; 113 1.1 thorpej }; 114 1.1 thorpej 115 1.1 thorpej int depca_print(void *, const char *); 116 1.1 thorpej 117 1.1 thorpej void 118 1.1 thorpej depca_attach(struct depca_softc *sc) 119 1.1 thorpej { 120 1.1 thorpej struct depca_attach_args da; 121 1.1 thorpej 122 1.1 thorpej da.da_name = "le"; 123 1.1 thorpej 124 1.19 thorpej (void)config_found(sc->sc_dev, &da, depca_print, CFARGS_NONE); 125 1.1 thorpej } 126 1.1 thorpej 127 1.1 thorpej int 128 1.1 thorpej depca_print(void *aux, const char *pnp) 129 1.1 thorpej { 130 1.1 thorpej struct depca_attach_args *da = aux; 131 1.1 thorpej 132 1.1 thorpej if (pnp) 133 1.9 thorpej aprint_normal("%s at %s", da->da_name, pnp); 134 1.1 thorpej 135 1.1 thorpej return (UNCONF); 136 1.1 thorpej } 137 1.1 thorpej 138 1.1 thorpej void 139 1.16 tsutsui depca_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val) 140 1.1 thorpej { 141 1.16 tsutsui struct depca_softc *dsc = device_private(device_parent(sc->sc_dev)); 142 1.1 thorpej 143 1.1 thorpej bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RAP, port); 144 1.1 thorpej bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RDP, val); 145 1.1 thorpej } 146 1.1 thorpej 147 1.16 tsutsui uint16_t 148 1.16 tsutsui depca_rdcsr(struct lance_softc *sc, uint16_t port) 149 1.1 thorpej { 150 1.16 tsutsui struct depca_softc *dsc = device_private(device_parent(sc->sc_dev)); 151 1.1 thorpej 152 1.1 thorpej bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RAP, port); 153 1.1 thorpej return (bus_space_read_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_RDP)); 154 1.1 thorpej } 155 1.1 thorpej 156 1.1 thorpej int 157 1.16 tsutsui depca_readprom(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t *laddr) 158 1.1 thorpej { 159 1.1 thorpej int port, i; 160 1.1 thorpej 161 1.1 thorpej /* 162 1.1 thorpej * Extract the physical MAC address from the ROM. 163 1.1 thorpej * 164 1.1 thorpej * The address PROM is 32 bytes wide, and we access it through 165 1.1 thorpej * a single I/O port. On each read, it rotates to the next 166 1.1 thorpej * position. We find the ethernet address by looking for a 167 1.1 thorpej * particular sequence of bytes (0xff, 0x00, 0x55, 0xaa, 0xff, 168 1.1 thorpej * 0x00, 0x55, 0xaa), and then reading the next 8 bytes (the 169 1.1 thorpej * ethernet address and a checksum). 170 1.1 thorpej * 171 1.1 thorpej * It appears that the PROM can be at one of two locations, so 172 1.1 thorpej * we just try both. 173 1.1 thorpej */ 174 1.1 thorpej port = DEPCA_ADP; 175 1.1 thorpej for (i = 0; i < 32; i++) 176 1.1 thorpej if (bus_space_read_1(iot, ioh, port) == 0xff && 177 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x00 && 178 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x55 && 179 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xaa && 180 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xff && 181 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x00 && 182 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x55 && 183 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xaa) 184 1.1 thorpej goto found; 185 1.1 thorpej port = DEPCA_ADP + 1; 186 1.1 thorpej for (i = 0; i < 32; i++) 187 1.1 thorpej if (bus_space_read_1(iot, ioh, port) == 0xff && 188 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x00 && 189 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x55 && 190 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xaa && 191 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xff && 192 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x00 && 193 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0x55 && 194 1.1 thorpej bus_space_read_1(iot, ioh, port) == 0xaa) 195 1.1 thorpej goto found; 196 1.16 tsutsui aprint_error("depca: address not found\n"); 197 1.1 thorpej return (-1); 198 1.1 thorpej 199 1.1 thorpej found: 200 1.1 thorpej 201 1.1 thorpej if (laddr) { 202 1.1 thorpej for (i = 0; i < 6; i++) 203 1.1 thorpej laddr[i] = bus_space_read_1(iot, ioh, port); 204 1.1 thorpej } 205 1.1 thorpej 206 1.1 thorpej #if 0 207 1.1 thorpej sum = 208 1.1 thorpej (laddr[0] << 2) + 209 1.1 thorpej (laddr[1] << 10) + 210 1.1 thorpej (laddr[2] << 1) + 211 1.1 thorpej (laddr[3] << 9) + 212 1.1 thorpej (laddr[4] << 0) + 213 1.1 thorpej (laddr[5] << 8); 214 1.1 thorpej sum = (sum & 0xffff) + (sum >> 16); 215 1.1 thorpej sum = (sum & 0xffff) + (sum >> 16); 216 1.1 thorpej 217 1.1 thorpej rom_sum = bus_space_read_1(iot, ioh, port); 218 1.1 thorpej rom_sum |= bus_space_read_1(iot, ioh, port) << 8; 219 1.1 thorpej 220 1.1 thorpej if (sum != rom_sum) { 221 1.16 tsutsui aprint_error("depca: checksum mismatch; " 222 1.16 tsutsui "calculated %04x != read %04x", sum, rom_sum); 223 1.1 thorpej return (-1); 224 1.1 thorpej } 225 1.1 thorpej #endif 226 1.1 thorpej 227 1.1 thorpej return (0); 228 1.1 thorpej } 229 1.1 thorpej 230 1.1 thorpej int 231 1.16 tsutsui le_depca_match(device_t parent, cfdata_t cf, void *aux) 232 1.1 thorpej { 233 1.1 thorpej struct depca_attach_args *da = aux; 234 1.1 thorpej 235 1.16 tsutsui return (strcmp(da->da_name, cf->cf_name) == 0); 236 1.1 thorpej } 237 1.1 thorpej 238 1.1 thorpej void 239 1.16 tsutsui le_depca_attach(device_t parent, device_t self, void *aux) 240 1.1 thorpej { 241 1.16 tsutsui struct depca_softc *dsc = device_private(parent); 242 1.16 tsutsui struct le_depca_softc *lesc = device_private(self); 243 1.1 thorpej struct lance_softc *sc = &lesc->sc_am7990.lsc; 244 1.16 tsutsui uint8_t val; 245 1.1 thorpej int i; 246 1.1 thorpej 247 1.16 tsutsui sc->sc_dev = self; 248 1.16 tsutsui aprint_error("\n"); 249 1.1 thorpej 250 1.1 thorpej /* I/O and memory spaces already mapped. */ 251 1.1 thorpej 252 1.1 thorpej if (depca_readprom(dsc->sc_iot, dsc->sc_ioh, sc->sc_enaddr)) { 253 1.16 tsutsui aprint_error_dev(self, "can't read PROM\n"); 254 1.1 thorpej return; 255 1.1 thorpej } 256 1.1 thorpej 257 1.1 thorpej bus_space_write_2(dsc->sc_iot, dsc->sc_ioh, DEPCA_CSR, 258 1.1 thorpej DEPCA_CSR_DUM | DEPCA_CSR_IEN | DEPCA_CSR_SHE | 259 1.1 thorpej (dsc->sc_memsize == 32*1024 ? DEPCA_CSR_LOW32K : 0)); 260 1.1 thorpej 261 1.1 thorpej val = 0xff; 262 1.1 thorpej for (;;) { 263 1.16 tsutsui uint8_t cv; 264 1.1 thorpej 265 1.1 thorpej bus_space_set_region_1(dsc->sc_memt, dsc->sc_memh, 0, val, 266 1.1 thorpej dsc->sc_memsize); 267 1.1 thorpej for (i = 0; i < dsc->sc_memsize; i++) { 268 1.1 thorpej cv = bus_space_read_1(dsc->sc_memt, dsc->sc_memh, i); 269 1.1 thorpej if (cv != val) { 270 1.16 tsutsui aprint_error_dev(self, 271 1.16 tsutsui "failed to clear memory at %d " 272 1.1 thorpej "(0x%02x != 0x%02x)\n", 273 1.16 tsutsui i, cv, val); 274 1.1 thorpej return; 275 1.1 thorpej } 276 1.1 thorpej } 277 1.1 thorpej if (val == 0x00) 278 1.1 thorpej break; 279 1.1 thorpej val -= 0x55; 280 1.1 thorpej } 281 1.1 thorpej 282 1.1 thorpej sc->sc_conf3 = LE_C3_ACON; 283 1.1 thorpej sc->sc_mem = 0; /* Not used. */ 284 1.1 thorpej sc->sc_addr = 0; 285 1.1 thorpej sc->sc_memsize = dsc->sc_memsize; 286 1.1 thorpej 287 1.1 thorpej sc->sc_copytodesc = depca_copytobuf; 288 1.1 thorpej sc->sc_copyfromdesc = depca_copyfrombuf; 289 1.1 thorpej sc->sc_copytobuf = depca_copytobuf; 290 1.1 thorpej sc->sc_copyfrombuf = depca_copyfrombuf; 291 1.1 thorpej sc->sc_zerobuf = depca_zerobuf; 292 1.1 thorpej 293 1.1 thorpej sc->sc_rdcsr = depca_rdcsr; 294 1.1 thorpej sc->sc_wrcsr = depca_wrcsr; 295 1.1 thorpej sc->sc_hwinit = NULL; 296 1.1 thorpej 297 1.16 tsutsui aprint_error("%s", device_xname(self)); 298 1.1 thorpej am7990_config(&lesc->sc_am7990); 299 1.1 thorpej 300 1.1 thorpej lesc->sc_ih = (*dsc->sc_intr_establish)(dsc, sc); 301 1.1 thorpej } 302 1.1 thorpej 303 1.1 thorpej /* 304 1.1 thorpej * Controller interrupt. 305 1.1 thorpej */ 306 1.1 thorpej int 307 1.1 thorpej depca_intredge(void *arg) 308 1.1 thorpej { 309 1.1 thorpej 310 1.1 thorpej if (am7990_intr(arg) == 0) 311 1.1 thorpej return (0); 312 1.1 thorpej for (;;) 313 1.1 thorpej if (am7990_intr(arg) == 0) 314 1.1 thorpej return (1); 315 1.1 thorpej } 316 1.1 thorpej 317 1.1 thorpej /* 318 1.1 thorpej * DEPCA shared memory access functions. 319 1.1 thorpej */ 320 1.1 thorpej 321 1.1 thorpej void 322 1.1 thorpej depca_copytobuf(struct lance_softc *sc, void *from, int boff, int len) 323 1.1 thorpej { 324 1.16 tsutsui struct depca_softc *dsc = device_private(device_parent(sc->sc_dev)); 325 1.1 thorpej 326 1.1 thorpej bus_space_write_region_1(dsc->sc_memt, dsc->sc_memh, boff, 327 1.1 thorpej from, len); 328 1.1 thorpej } 329 1.1 thorpej 330 1.1 thorpej void 331 1.1 thorpej depca_copyfrombuf(struct lance_softc *sc, void *to, int boff, int len) 332 1.1 thorpej { 333 1.16 tsutsui struct depca_softc *dsc = device_private(device_parent(sc->sc_dev)); 334 1.1 thorpej 335 1.1 thorpej bus_space_read_region_1(dsc->sc_memt, dsc->sc_memh, boff, 336 1.1 thorpej to, len); 337 1.1 thorpej } 338 1.1 thorpej 339 1.1 thorpej void 340 1.1 thorpej depca_zerobuf(struct lance_softc *sc, int boff, int len) 341 1.1 thorpej { 342 1.16 tsutsui struct depca_softc *dsc = device_private(device_parent(sc->sc_dev)); 343 1.1 thorpej 344 1.1 thorpej bus_space_set_region_1(dsc->sc_memt, dsc->sc_memh, boff, 345 1.1 thorpej 0x00, len); 346 1.1 thorpej } 347