1 /* $NetBSD: if_le_pci.c,v 1.43 2005/12/11 12:22:49 christos Exp $ */ 2 /* $FreeBSD: src/sys/dev/le/if_le_pci.c,v 1.4 2006/06/05 15:14:14 marius Exp $ */ 3 4 5 /*- 6 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace 11 * Simulation Facility, NASA Ames Research Center. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the NetBSD 24 * Foundation, Inc. and its contributors. 25 * 4. Neither the name of The NetBSD Foundation nor the names of its 26 * contributors may be used to endorse or promote products derived 27 * from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42 /*- 43 * Copyright (c) 1992, 1993 44 * The Regents of the University of California. All rights reserved. 45 * 46 * This code is derived from software contributed to Berkeley by 47 * Ralph Campbell and Rick Macklem. 48 * 49 * Redistribution and use in source and binary forms, with or without 50 * modification, are permitted provided that the following conditions 51 * are met: 52 * 1. Redistributions of source code must retain the above copyright 53 * notice, this list of conditions and the following disclaimer. 54 * 2. Redistributions in binary form must reproduce the above copyright 55 * notice, this list of conditions and the following disclaimer in the 56 * documentation and/or other materials provided with the distribution. 57 * 3. Neither the name of the University nor the names of its contributors 58 * may be used to endorse or promote products derived from this software 59 * without specific prior written permission. 60 * 61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 71 * SUCH DAMAGE. 72 * 73 * @(#)if_le.c 8.2 (Berkeley) 11/16/93 74 */ 75 76 #include <sys/param.h> 77 #include <sys/systm.h> 78 #include <sys/bus.h> 79 #include <sys/endian.h> 80 #include <sys/kernel.h> 81 #include <sys/interrupt.h> 82 #include <sys/lock.h> 83 #include <sys/module.h> 84 #include <sys/resource.h> 85 #include <sys/rman.h> 86 #include <sys/socket.h> 87 88 #include <net/ethernet.h> 89 #include <net/if.h> 90 #include <net/if_media.h> 91 #include <net/if_arp.h> 92 #include <net/ifq_var.h> 93 94 #include <bus/pci/pcireg.h> 95 #include <bus/pci/pcivar.h> 96 97 #include <dev/netif/lnc/lancereg.h> 98 #include <dev/netif/lnc/lancevar.h> 99 #include <dev/netif/lnc/am79900var.h> 100 101 #define AMD_VENDOR 0x1022 102 #define AMD_PCNET_PCI 0x2000 103 #define AMD_PCNET_HOME 0x2001 104 #define PCNET_MEMSIZE (32*1024) 105 #define PCNET_PCI_RDP 0x10 106 #define PCNET_PCI_RAP 0x12 107 #define PCNET_PCI_BDP 0x16 108 109 #define LNC_PROBE_PRIORITY -100 110 111 struct le_pci_softc { 112 struct am79900_softc sc_am79900; /* glue to MI code */ 113 114 int sc_rrid; 115 struct resource *sc_rres; 116 bus_space_tag_t sc_regt; 117 bus_space_handle_t sc_regh; 118 119 int sc_irid; 120 struct resource *sc_ires; 121 void *sc_ih; 122 123 bus_dma_tag_t sc_pdmat; 124 bus_dma_tag_t sc_dmat; 125 bus_dmamap_t sc_dmam; 126 }; 127 128 static device_probe_t le_pci_probe; 129 static device_attach_t le_pci_attach; 130 static device_detach_t le_pci_detach; 131 static device_resume_t le_pci_resume; 132 static device_suspend_t le_pci_suspend; 133 134 static device_method_t le_pci_methods[] = { 135 /* Device interface */ 136 DEVMETHOD(device_probe, le_pci_probe), 137 DEVMETHOD(device_attach, le_pci_attach), 138 DEVMETHOD(device_detach, le_pci_detach), 139 /* We can just use the suspend method here. */ 140 DEVMETHOD(device_shutdown, le_pci_suspend), 141 DEVMETHOD(device_suspend, le_pci_suspend), 142 DEVMETHOD(device_resume, le_pci_resume), 143 144 DEVMETHOD_END 145 }; 146 147 DEFINE_CLASS_0(lnc, le_pci_driver, le_pci_methods, sizeof(struct le_pci_softc)); 148 DRIVER_MODULE(lnc, pci, le_pci_driver, le_devclass, NULL, NULL); 149 MODULE_DEPEND(lnc, ether, 1, 1, 1); 150 151 static const int le_home_supmedia[] = { 152 IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0, 0) 153 }; 154 155 static const int le_pci_supmedia[] = { 156 IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 157 IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, IFM_FDX, 0), 158 IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0), 159 IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0), 160 IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0), 161 IFM_MAKEWORD(IFM_ETHER, IFM_10_5, IFM_FDX, 0) 162 }; 163 164 static void le_pci_wrbcr(struct lance_softc *, uint16_t, uint16_t); 165 static uint16_t le_pci_rdbcr(struct lance_softc *, uint16_t); 166 static void le_pci_wrcsr(struct lance_softc *, uint16_t, uint16_t); 167 static uint16_t le_pci_rdcsr(struct lance_softc *, uint16_t); 168 static int le_pci_mediachange(struct lance_softc *); 169 static void le_pci_hwreset(struct lance_softc *); 170 static bus_dmamap_callback_t le_pci_dma_callback; 171 172 static void 173 le_pci_wrbcr(struct lance_softc *sc, uint16_t port, uint16_t val) 174 { 175 struct le_pci_softc *lesc = (struct le_pci_softc *)sc; 176 177 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, port); 178 bus_space_barrier(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, 2, 179 BUS_SPACE_BARRIER_WRITE); 180 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_BDP, val); 181 } 182 183 static uint16_t 184 le_pci_rdbcr(struct lance_softc *sc, uint16_t port) 185 { 186 struct le_pci_softc *lesc = (struct le_pci_softc *)sc; 187 188 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, port); 189 bus_space_barrier(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, 2, 190 BUS_SPACE_BARRIER_WRITE); 191 return (bus_space_read_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_BDP)); 192 } 193 194 static void 195 le_pci_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val) 196 { 197 struct le_pci_softc *lesc = (struct le_pci_softc *)sc; 198 199 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, port); 200 bus_space_barrier(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, 2, 201 BUS_SPACE_BARRIER_WRITE); 202 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RDP, val); 203 } 204 205 static uint16_t 206 le_pci_rdcsr(struct lance_softc *sc, uint16_t port) 207 { 208 struct le_pci_softc *lesc = (struct le_pci_softc *)sc; 209 210 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, port); 211 bus_space_barrier(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, 2, 212 BUS_SPACE_BARRIER_WRITE); 213 return (bus_space_read_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RDP)); 214 } 215 216 static int 217 le_pci_mediachange(struct lance_softc *sc) 218 { 219 struct ifmedia *ifm = &sc->sc_media; 220 uint16_t reg; 221 222 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 223 return (EINVAL); 224 225 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) 226 le_pci_wrbcr(sc, LE_BCR49, 227 (le_pci_rdbcr(sc, LE_BCR49) & ~LE_B49_PHYSEL) | 0x1); 228 else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) 229 le_pci_wrbcr(sc, LE_BCR2, 230 le_pci_rdbcr(sc, LE_BCR2) | LE_B2_ASEL); 231 else { 232 le_pci_wrbcr(sc, LE_BCR2, 233 le_pci_rdbcr(sc, LE_BCR2) & ~LE_B2_ASEL); 234 235 reg = le_pci_rdcsr(sc, LE_CSR15); 236 reg &= ~LE_C15_PORTSEL(LE_PORTSEL_MASK); 237 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T) 238 reg |= LE_C15_PORTSEL(LE_PORTSEL_10T); 239 else 240 reg |= LE_C15_PORTSEL(LE_PORTSEL_AUI); 241 le_pci_wrcsr(sc, LE_CSR15, reg); 242 } 243 244 reg = le_pci_rdbcr(sc, LE_BCR9); 245 if (IFM_OPTIONS(ifm->ifm_media) & IFM_FDX) { 246 reg |= LE_B9_FDEN; 247 /* 248 * Allow FDX on AUI only if explicitly chosen, 249 * not in autoselect mode. 250 */ 251 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_5) 252 reg |= LE_B9_AUIFD; 253 else 254 reg &= ~LE_B9_AUIFD; 255 } else 256 reg &= ~LE_B9_FDEN; 257 le_pci_wrbcr(sc, LE_BCR9, reg); 258 259 return (0); 260 } 261 262 static void 263 le_pci_hwreset(struct lance_softc *sc) 264 { 265 266 /* 267 * Chip is stopped. Set software style to PCnet-PCI (32-bit). 268 * Actually, am79900.c implements ILACC support (hence its 269 * name) but unfortunately VMware does not. As far as this 270 * driver is concerned that should not make a difference 271 * though, as the settings used have the same meaning for 272 * both, ILACC and PCnet-PCI (note that there would be a 273 * difference for the ADD_FCS/NO_FCS bit if used). 274 */ 275 le_pci_wrbcr(sc, LE_BCR20, LE_B20_SSTYLE_PCNETPCI2); 276 } 277 278 static void 279 le_pci_dma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 280 { 281 struct lance_softc *sc = (struct lance_softc *)xsc; 282 283 if (error != 0) 284 return; 285 KASSERT(nsegs == 1, ("%s: bad DMA segment count", __func__)); 286 sc->sc_addr = segs[0].ds_addr; 287 } 288 289 static int 290 le_pci_probe(device_t dev) 291 { 292 293 if (pci_get_vendor(dev) != AMD_VENDOR) 294 return (ENXIO); 295 296 switch (pci_get_device(dev)) { 297 case AMD_PCNET_PCI: 298 device_set_desc(dev, "AMD PCnet-PCI"); 299 /* Let pcn(4) win. */ 300 return (LNC_PROBE_PRIORITY); 301 case AMD_PCNET_HOME: 302 device_set_desc(dev, "AMD PCnet-Home"); 303 /* Let pcn(4) win. */ 304 return (LNC_PROBE_PRIORITY); 305 default: 306 return (ENXIO); 307 } 308 } 309 310 static int 311 le_pci_attach(device_t dev) 312 { 313 struct le_pci_softc *lesc; 314 struct lance_softc *sc; 315 int error, i; 316 317 lesc = device_get_softc(dev); 318 sc = &lesc->sc_am79900.lsc; 319 320 pci_enable_busmaster(dev); 321 pci_enable_io(dev, PCIM_CMD_PORTEN); 322 323 lesc->sc_rrid = PCIR_BAR(0); 324 lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 325 &lesc->sc_rrid, RF_ACTIVE); 326 if (lesc->sc_rres == NULL) { 327 device_printf(dev, "cannot allocate registers\n"); 328 error = ENXIO; 329 goto fail_mtx; 330 } 331 lesc->sc_regt = rman_get_bustag(lesc->sc_rres); 332 lesc->sc_regh = rman_get_bushandle(lesc->sc_rres); 333 334 lesc->sc_irid = 0; 335 if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, 336 &lesc->sc_irid, RF_SHAREABLE | RF_ACTIVE)) == NULL) { 337 device_printf(dev, "cannot allocate interrupt\n"); 338 error = ENXIO; 339 goto fail_rres; 340 } 341 342 error = bus_dma_tag_create( 343 NULL, /* parent */ 344 1, 0, /* alignment, boundary */ 345 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 346 BUS_SPACE_MAXADDR, /* highaddr */ 347 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 348 0, /* nsegments */ 349 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 350 BUS_DMA_WAITOK, /* flags */ 351 &lesc->sc_pdmat); 352 if (error != 0) { 353 device_printf(dev, "cannot allocate parent DMA tag\n"); 354 goto fail_ires; 355 } 356 357 sc->sc_memsize = PCNET_MEMSIZE; 358 /* 359 * For Am79C970A, Am79C971 and Am79C978 the init block must be 2-byte 360 * aligned and the ring descriptors must be 16-byte aligned when using 361 * a 32-bit software style. 362 */ 363 error = bus_dma_tag_create( 364 lesc->sc_pdmat, /* parent */ 365 16, 0, /* alignment, boundary */ 366 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 367 BUS_SPACE_MAXADDR, /* highaddr */ 368 sc->sc_memsize, /* maxsize */ 369 1, /* nsegments */ 370 sc->sc_memsize, /* maxsegsize */ 371 BUS_DMA_WAITOK, /* flags */ 372 &lesc->sc_dmat); 373 if (error != 0) { 374 device_printf(dev, "cannot allocate buffer DMA tag\n"); 375 goto fail_pdtag; 376 } 377 378 error = bus_dmamem_alloc(lesc->sc_dmat, (void **)&sc->sc_mem, 379 BUS_DMA_WAITOK | BUS_DMA_COHERENT, &lesc->sc_dmam); 380 if (error != 0) { 381 device_printf(dev, "cannot allocate DMA buffer memory\n"); 382 goto fail_dtag; 383 } 384 385 sc->sc_addr = 0; 386 error = bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmam, sc->sc_mem, 387 sc->sc_memsize, le_pci_dma_callback, sc, 0); 388 if (error != 0 || sc->sc_addr == 0) { 389 device_printf(dev, "cannot load DMA buffer map\n"); 390 goto fail_dmem; 391 } 392 393 sc->sc_flags = LE_BSWAP; 394 sc->sc_conf3 = 0; 395 396 sc->sc_mediastatus = NULL; 397 switch (pci_get_device(dev)) { 398 case AMD_PCNET_HOME: 399 sc->sc_mediachange = le_pci_mediachange; 400 sc->sc_supmedia = le_home_supmedia; 401 sc->sc_nsupmedia = sizeof(le_home_supmedia) / sizeof(int); 402 sc->sc_defaultmedia = le_home_supmedia[0]; 403 break; 404 default: 405 sc->sc_mediachange = le_pci_mediachange; 406 sc->sc_supmedia = le_pci_supmedia; 407 sc->sc_nsupmedia = sizeof(le_pci_supmedia) / sizeof(int); 408 sc->sc_defaultmedia = le_pci_supmedia[0]; 409 } 410 411 /* 412 * Extract the physical MAC address from the ROM. 413 */ 414 for (i = 0; i < sizeof(sc->sc_enaddr); i++) 415 sc->sc_enaddr[i] = 416 bus_space_read_1(lesc->sc_regt, lesc->sc_regh, i); 417 418 sc->sc_copytodesc = lance_copytobuf_contig; 419 sc->sc_copyfromdesc = lance_copyfrombuf_contig; 420 sc->sc_copytobuf = lance_copytobuf_contig; 421 sc->sc_copyfrombuf = lance_copyfrombuf_contig; 422 sc->sc_zerobuf = lance_zerobuf_contig; 423 424 sc->sc_rdcsr = le_pci_rdcsr; 425 sc->sc_wrcsr = le_pci_wrcsr; 426 sc->sc_hwreset = le_pci_hwreset; 427 sc->sc_hwinit = NULL; 428 sc->sc_hwintr = NULL; 429 sc->sc_nocarrier = NULL; 430 431 error = am79900_config(&lesc->sc_am79900, device_get_name(dev), 432 device_get_unit(dev)); 433 if (error != 0) { 434 device_printf(dev, "cannot attach Am79900\n"); 435 goto fail_dmap; 436 } 437 438 ifq_set_cpuid(&sc->ifp->if_snd, rman_get_cpuid(lesc->sc_ires)); 439 440 error = bus_setup_intr(dev, lesc->sc_ires, INTR_MPSAFE, 441 am79900_intr, sc, &lesc->sc_ih, sc->ifp->if_serializer); 442 if (error != 0) { 443 device_printf(dev, "cannot set up interrupt\n"); 444 goto fail_am79900; 445 } 446 447 return (0); 448 449 fail_am79900: 450 am79900_detach(&lesc->sc_am79900); 451 fail_dmap: 452 bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam); 453 fail_dmem: 454 bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam); 455 fail_dtag: 456 bus_dma_tag_destroy(lesc->sc_dmat); 457 fail_pdtag: 458 bus_dma_tag_destroy(lesc->sc_pdmat); 459 fail_ires: 460 bus_release_resource(dev, SYS_RES_IRQ, lesc->sc_irid, lesc->sc_ires); 461 fail_rres: 462 bus_release_resource(dev, SYS_RES_IOPORT, lesc->sc_rrid, lesc->sc_rres); 463 fail_mtx: 464 return (error); 465 } 466 467 static int 468 le_pci_detach(device_t dev) 469 { 470 struct le_pci_softc *lesc; 471 struct lance_softc *sc; 472 473 lesc = device_get_softc(dev); 474 sc = &lesc->sc_am79900.lsc; 475 476 if (device_is_attached(dev)) { 477 lwkt_serialize_enter(sc->ifp->if_serializer); 478 lance_stop(sc); 479 bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih); 480 lwkt_serialize_exit(sc->ifp->if_serializer); 481 482 am79900_detach(&lesc->sc_am79900); 483 } 484 485 if (lesc->sc_ires) 486 bus_release_resource(dev, SYS_RES_IRQ, lesc->sc_irid, lesc->sc_ires); 487 if (lesc->sc_rres) 488 bus_release_resource(dev, SYS_RES_IOPORT, lesc->sc_rrid, lesc->sc_rres); 489 if (lesc->sc_dmam) { 490 bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam); 491 bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam); 492 } 493 if (lesc->sc_dmat) 494 bus_dma_tag_destroy(lesc->sc_dmat); 495 if (lesc->sc_pdmat) 496 bus_dma_tag_destroy(lesc->sc_pdmat); 497 498 return (0); 499 } 500 501 static int 502 le_pci_suspend(device_t dev) 503 { 504 struct le_pci_softc *lesc; 505 506 lesc = device_get_softc(dev); 507 508 lance_suspend(&lesc->sc_am79900.lsc); 509 510 return (0); 511 } 512 513 static int 514 le_pci_resume(device_t dev) 515 { 516 struct le_pci_softc *lesc; 517 518 lesc = device_get_softc(dev); 519 520 lance_resume(&lesc->sc_am79900.lsc); 521 522 return (0); 523 } 524