1 /* $NetBSD: if_le_isa.c,v 1.45 2007/10/19 12:00:18 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998 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. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)if_le.c 8.2 (Berkeley) 11/16/93 72 */ 73 74 #include <sys/cdefs.h> 75 __KERNEL_RCSID(0, "$NetBSD: if_le_isa.c,v 1.45 2007/10/19 12:00:18 ad Exp $"); 76 77 #include <sys/param.h> 78 #include <sys/systm.h> 79 #include <sys/mbuf.h> 80 #include <sys/syslog.h> 81 #include <sys/socket.h> 82 #include <sys/device.h> 83 84 #include <uvm/uvm_extern.h> 85 86 #include <net/if.h> 87 #include <net/if_ether.h> 88 #include <net/if_media.h> 89 90 #include <sys/cpu.h> 91 #include <sys/intr.h> 92 #include <sys/bus.h> 93 94 #include <dev/isa/isareg.h> 95 #include <dev/isa/isavar.h> 96 #include <dev/isa/isadmavar.h> 97 98 #include <dev/ic/lancereg.h> 99 #include <dev/ic/lancevar.h> 100 #include <dev/ic/am7990reg.h> 101 #include <dev/ic/am7990var.h> 102 103 #include <dev/isa/if_levar.h> 104 105 int ne2100_isa_probe(struct device *, struct cfdata *, void *); 106 int bicc_isa_probe(struct device *, struct cfdata *, void *); 107 void le_dummyattach(struct device *, struct device *, void *); 108 int le_dummyprobe(struct device *, struct cfdata *, void *); 109 void le_ne2100_attach(struct device *, struct device *, void *); 110 void le_bicc_attach(struct device *, struct device *, void *); 111 112 CFATTACH_DECL(nele, sizeof(struct device), 113 ne2100_isa_probe, le_dummyattach, NULL, NULL); 114 115 CFATTACH_DECL(le_nele, sizeof(struct le_softc), 116 le_dummyprobe, le_ne2100_attach, NULL, NULL); 117 118 CFATTACH_DECL(bicc, sizeof(struct device), 119 bicc_isa_probe, le_dummyattach, NULL, NULL); 120 121 CFATTACH_DECL(le_bicc, sizeof(struct le_softc), 122 le_dummyprobe, le_bicc_attach, NULL, NULL); 123 124 struct le_isa_params { 125 const char *name; 126 int iosize, rap, rdp; 127 int macstart, macstride; 128 } ne2100_params = { 129 "NE2100", 130 24, NE2100_RAP, NE2100_RDP, 131 0, 1 132 }, bicc_params = { 133 "BICC Isolan", 134 16, BICC_RAP, BICC_RDP, 135 0, 2 136 }; 137 138 int lance_isa_probe(struct isa_attach_args *, struct le_isa_params *, int); 139 void le_isa_attach(struct device *, struct le_softc *, 140 struct isa_attach_args *, struct le_isa_params *); 141 142 int le_isa_intredge(void *); 143 144 #if defined(_KERNEL_OPT) 145 #include "opt_ddb.h" 146 #endif 147 148 #ifdef DDB 149 #define integrate 150 #define hide 151 #else 152 #define integrate static inline 153 #define hide static 154 #endif 155 156 hide void le_isa_wrcsr(struct lance_softc *, u_int16_t, u_int16_t); 157 hide u_int16_t le_isa_rdcsr(struct lance_softc *, u_int16_t); 158 159 #define LE_ISA_MEMSIZE 16384 160 161 hide void 162 le_isa_wrcsr(sc, port, val) 163 struct lance_softc *sc; 164 u_int16_t port, val; 165 { 166 struct le_softc *lesc = (struct le_softc *)sc; 167 bus_space_tag_t iot = lesc->sc_iot; 168 bus_space_handle_t ioh = lesc->sc_ioh; 169 170 bus_space_write_2(iot, ioh, lesc->sc_rap, port); 171 bus_space_write_2(iot, ioh, lesc->sc_rdp, val); 172 } 173 174 hide u_int16_t 175 le_isa_rdcsr(sc, port) 176 struct lance_softc *sc; 177 u_int16_t port; 178 { 179 struct le_softc *lesc = (struct le_softc *)sc; 180 bus_space_tag_t iot = lesc->sc_iot; 181 bus_space_handle_t ioh = lesc->sc_ioh; 182 u_int16_t val; 183 184 bus_space_write_2(iot, ioh, lesc->sc_rap, port); 185 val = bus_space_read_2(iot, ioh, lesc->sc_rdp); 186 return (val); 187 } 188 189 int 190 ne2100_isa_probe(struct device *parent, struct cfdata *match, 191 void *aux) 192 { 193 return (lance_isa_probe(aux, &ne2100_params, match->cf_flags)); 194 } 195 196 int 197 bicc_isa_probe(struct device *parent, struct cfdata *match, void *aux) 198 { 199 return (lance_isa_probe(aux, &bicc_params, match->cf_flags)); 200 } 201 202 /* 203 * Determine which chip is present on the card. 204 */ 205 int 206 lance_isa_probe(ia, p, flags) 207 struct isa_attach_args *ia; 208 struct le_isa_params *p; 209 int flags; 210 { 211 bus_space_tag_t iot = ia->ia_iot; 212 bus_space_handle_t ioh; 213 int rap, rdp; 214 int rv = 0; 215 216 if (ia->ia_nio < 1) 217 return (0); 218 if (ia->ia_nirq < 1) 219 return (0); 220 if (ia->ia_ndrq < 1) 221 return (0); 222 223 if (ISA_DIRECT_CONFIG(ia)) 224 return (0); 225 226 /* Disallow wildcarded i/o address. */ 227 if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT) 228 return (0); 229 if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) 230 return (0); 231 if ((flags & LANCEISA_FLAG_LOCALBUS) == 0 && 232 ia->ia_drq[0].ir_drq == ISA_UNKNOWN_DRQ) 233 return (0); 234 235 /* Map i/o space. */ 236 if (bus_space_map(iot, ia->ia_io[0].ir_addr, p->iosize, 0, &ioh)) 237 return (0); 238 239 rap = p->rap; 240 rdp = p->rdp; 241 242 /* Stop the LANCE chip and put it in a known state. */ 243 bus_space_write_2(iot, ioh, rap, LE_CSR0); 244 bus_space_write_2(iot, ioh, rdp, LE_C0_STOP); 245 delay(100); 246 247 bus_space_write_2(iot, ioh, rap, LE_CSR0); 248 if (bus_space_read_2(iot, ioh, rdp) != LE_C0_STOP) 249 goto bad; 250 251 bus_space_write_2(iot, ioh, rap, LE_CSR3); 252 bus_space_write_2(iot, ioh, rdp, 0); 253 254 ia->ia_nio = 1; 255 ia->ia_io[0].ir_size = p->iosize; 256 257 ia->ia_nirq = 1; 258 259 if ((flags & LANCEISA_FLAG_LOCALBUS) != 0 && 260 ia->ia_drq[0].ir_drq == ISA_UNKNOWN_DRQ) 261 ia->ia_ndrq = 0; 262 else 263 ia->ia_ndrq = 1; 264 265 ia->ia_niomem = 0; 266 267 rv = 1; 268 269 bad: 270 bus_space_unmap(iot, ioh, p->iosize); 271 return (rv); 272 } 273 274 void 275 le_dummyattach(struct device *parent, struct device *self, 276 void *aux) 277 { 278 printf("\n"); 279 280 config_found(self, aux, 0); 281 } 282 283 int 284 le_dummyprobe(struct device *parent, struct cfdata *match, 285 void *aux) 286 { 287 return (1); 288 } 289 290 void 291 le_ne2100_attach(parent, self, aux) 292 struct device *parent, *self; 293 void *aux; 294 { 295 le_isa_attach(parent, (void *)self, aux, &ne2100_params); 296 } 297 298 void 299 le_bicc_attach(parent, self, aux) 300 struct device *parent, *self; 301 void *aux; 302 { 303 le_isa_attach(parent, (void *)self, aux, &bicc_params); 304 } 305 306 void 307 le_isa_attach(struct device *parent, struct le_softc *lesc, 308 struct isa_attach_args *ia, struct le_isa_params *p) 309 { 310 struct lance_softc *sc = &lesc->sc_am7990.lsc; 311 bus_space_tag_t iot = ia->ia_iot; 312 bus_space_handle_t ioh; 313 bus_dma_tag_t dmat = ia->ia_dmat; 314 bus_dma_segment_t seg; 315 int i, rseg, error; 316 317 printf(": %s Ethernet\n", p->name); 318 319 if (bus_space_map(iot, ia->ia_io[0].ir_addr, p->iosize, 0, &ioh)) 320 panic("%s: can't map io", sc->sc_dev.dv_xname); 321 322 /* 323 * Extract the physical MAC address from the ROM. 324 */ 325 for (i = 0; i < sizeof(sc->sc_enaddr); i++) 326 sc->sc_enaddr[i] = 327 bus_space_read_1(iot, ioh, p->macstart + i * p->macstride); 328 329 lesc->sc_iot = iot; 330 lesc->sc_ioh = ioh; 331 lesc->sc_dmat = dmat; 332 lesc->sc_rap = p->rap; 333 lesc->sc_rdp = p->rdp; 334 335 /* 336 * Allocate a DMA area for the card. 337 */ 338 if (bus_dmamem_alloc(dmat, LE_ISA_MEMSIZE, PAGE_SIZE, 0, &seg, 1, 339 &rseg, BUS_DMA_NOWAIT)) { 340 printf("%s: couldn't allocate memory for card\n", 341 sc->sc_dev.dv_xname); 342 return; 343 } 344 if (bus_dmamem_map(dmat, &seg, rseg, LE_ISA_MEMSIZE, 345 (void **)&sc->sc_mem, 346 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) { 347 printf("%s: couldn't map memory for card\n", 348 sc->sc_dev.dv_xname); 349 return; 350 } 351 352 /* 353 * Create and load the DMA map for the DMA area. 354 */ 355 if (bus_dmamap_create(dmat, LE_ISA_MEMSIZE, 1, 356 LE_ISA_MEMSIZE, 0, BUS_DMA_NOWAIT, &lesc->sc_dmam)) { 357 printf("%s: couldn't create DMA map\n", 358 sc->sc_dev.dv_xname); 359 bus_dmamem_free(dmat, &seg, rseg); 360 return; 361 } 362 if (bus_dmamap_load(dmat, lesc->sc_dmam, 363 sc->sc_mem, LE_ISA_MEMSIZE, NULL, BUS_DMA_NOWAIT)) { 364 printf("%s: coundn't load DMA map\n", 365 sc->sc_dev.dv_xname); 366 bus_dmamem_free(dmat, &seg, rseg); 367 return; 368 } 369 370 sc->sc_conf3 = 0; 371 sc->sc_addr = lesc->sc_dmam->dm_segs[0].ds_addr; 372 sc->sc_memsize = LE_ISA_MEMSIZE; 373 374 sc->sc_copytodesc = lance_copytobuf_contig; 375 sc->sc_copyfromdesc = lance_copyfrombuf_contig; 376 sc->sc_copytobuf = lance_copytobuf_contig; 377 sc->sc_copyfrombuf = lance_copyfrombuf_contig; 378 sc->sc_zerobuf = lance_zerobuf_contig; 379 380 sc->sc_rdcsr = le_isa_rdcsr; 381 sc->sc_wrcsr = le_isa_wrcsr; 382 sc->sc_hwinit = NULL; 383 384 if (ia->ia_ndrq > 0) { 385 if ((error = isa_dmacascade(ia->ia_ic, 386 ia->ia_drq[0].ir_drq)) != 0) { 387 printf("%s: unable to cascade DRQ, error = %d\n", 388 sc->sc_dev.dv_xname, error); 389 return; 390 } 391 } 392 393 lesc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq, 394 IST_EDGE, IPL_NET, le_isa_intredge, sc); 395 396 printf("%s", sc->sc_dev.dv_xname); 397 am7990_config(&lesc->sc_am7990); 398 } 399 400 /* 401 * Controller interrupt. 402 */ 403 int 404 le_isa_intredge(arg) 405 void *arg; 406 { 407 408 if (am7990_intr(arg) == 0) 409 return (0); 410 for (;;) 411 if (am7990_intr(arg) == 0) 412 return (1); 413 } 414