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