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 93 #include <bus/pci/pcireg.h> 94 #include <bus/pci/pcivar.h> 95 96 #include <dev/netif/lnc/lancereg.h> 97 #include <dev/netif/lnc/lancevar.h> 98 #include <dev/netif/lnc/am79900var.h> 99 100 #define AMD_VENDOR 0x1022 101 #define AMD_PCNET_PCI 0x2000 102 #define AMD_PCNET_HOME 0x2001 103 #define PCNET_MEMSIZE (32*1024) 104 #define PCNET_PCI_RDP 0x10 105 #define PCNET_PCI_RAP 0x12 106 #define PCNET_PCI_BDP 0x16 107 108 #define LNC_PROBE_PRIORITY -100 109 110 struct le_pci_softc { 111 struct am79900_softc sc_am79900; /* glue to MI code */ 112 113 int sc_rrid; 114 struct resource *sc_rres; 115 bus_space_tag_t sc_regt; 116 bus_space_handle_t sc_regh; 117 118 int sc_irid; 119 struct resource *sc_ires; 120 void *sc_ih; 121 122 bus_dma_tag_t sc_pdmat; 123 bus_dma_tag_t sc_dmat; 124 bus_dmamap_t sc_dmam; 125 }; 126 127 static device_probe_t le_pci_probe; 128 static device_attach_t le_pci_attach; 129 static device_detach_t le_pci_detach; 130 static device_resume_t le_pci_resume; 131 static device_suspend_t le_pci_suspend; 132 133 static device_method_t le_pci_methods[] = { 134 /* Device interface */ 135 DEVMETHOD(device_probe, le_pci_probe), 136 DEVMETHOD(device_attach, le_pci_attach), 137 DEVMETHOD(device_detach, le_pci_detach), 138 /* We can just use the suspend method here. */ 139 DEVMETHOD(device_shutdown, le_pci_suspend), 140 DEVMETHOD(device_suspend, le_pci_suspend), 141 DEVMETHOD(device_resume, le_pci_resume), 142 143 { 0, 0 } 144 }; 145 146 DEFINE_CLASS_0(lnc, le_pci_driver, le_pci_methods, sizeof(struct le_pci_softc)); 147 DRIVER_MODULE(lnc, pci, le_pci_driver, le_devclass, NULL, NULL); 148 MODULE_DEPEND(lnc, ether, 1, 1, 1); 149 150 static const int le_home_supmedia[] = { 151 IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0, 0) 152 }; 153 154 static const int le_pci_supmedia[] = { 155 IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 156 IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, IFM_FDX, 0), 157 IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0), 158 IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0), 159 IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, 0), 160 IFM_MAKEWORD(IFM_ETHER, IFM_10_5, IFM_FDX, 0) 161 }; 162 163 static void le_pci_wrbcr(struct lance_softc *, uint16_t, uint16_t); 164 static uint16_t le_pci_rdbcr(struct lance_softc *, uint16_t); 165 static void le_pci_wrcsr(struct lance_softc *, uint16_t, uint16_t); 166 static uint16_t le_pci_rdcsr(struct lance_softc *, uint16_t); 167 static int le_pci_mediachange(struct lance_softc *); 168 static void le_pci_hwreset(struct lance_softc *); 169 static bus_dmamap_callback_t le_pci_dma_callback; 170 171 static void 172 le_pci_wrbcr(struct lance_softc *sc, uint16_t port, uint16_t val) 173 { 174 struct le_pci_softc *lesc = (struct le_pci_softc *)sc; 175 176 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, port); 177 bus_space_barrier(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, 2, 178 BUS_SPACE_BARRIER_WRITE); 179 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_BDP, val); 180 } 181 182 static uint16_t 183 le_pci_rdbcr(struct lance_softc *sc, uint16_t port) 184 { 185 struct le_pci_softc *lesc = (struct le_pci_softc *)sc; 186 187 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, port); 188 bus_space_barrier(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, 2, 189 BUS_SPACE_BARRIER_WRITE); 190 return (bus_space_read_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_BDP)); 191 } 192 193 static void 194 le_pci_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val) 195 { 196 struct le_pci_softc *lesc = (struct le_pci_softc *)sc; 197 198 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, port); 199 bus_space_barrier(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, 2, 200 BUS_SPACE_BARRIER_WRITE); 201 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RDP, val); 202 } 203 204 static uint16_t 205 le_pci_rdcsr(struct lance_softc *sc, uint16_t port) 206 { 207 struct le_pci_softc *lesc = (struct le_pci_softc *)sc; 208 209 bus_space_write_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, port); 210 bus_space_barrier(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RAP, 2, 211 BUS_SPACE_BARRIER_WRITE); 212 return (bus_space_read_2(lesc->sc_regt, lesc->sc_regh, PCNET_PCI_RDP)); 213 } 214 215 static int 216 le_pci_mediachange(struct lance_softc *sc) 217 { 218 struct ifmedia *ifm = &sc->sc_media; 219 uint16_t reg; 220 221 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 222 return (EINVAL); 223 224 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) 225 le_pci_wrbcr(sc, LE_BCR49, 226 (le_pci_rdbcr(sc, LE_BCR49) & ~LE_B49_PHYSEL) | 0x1); 227 else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) 228 le_pci_wrbcr(sc, LE_BCR2, 229 le_pci_rdbcr(sc, LE_BCR2) | LE_B2_ASEL); 230 else { 231 le_pci_wrbcr(sc, LE_BCR2, 232 le_pci_rdbcr(sc, LE_BCR2) & ~LE_B2_ASEL); 233 234 reg = le_pci_rdcsr(sc, LE_CSR15); 235 reg &= ~LE_C15_PORTSEL(LE_PORTSEL_MASK); 236 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T) 237 reg |= LE_C15_PORTSEL(LE_PORTSEL_10T); 238 else 239 reg |= LE_C15_PORTSEL(LE_PORTSEL_AUI); 240 le_pci_wrcsr(sc, LE_CSR15, reg); 241 } 242 243 reg = le_pci_rdbcr(sc, LE_BCR9); 244 if (IFM_OPTIONS(ifm->ifm_media) & IFM_FDX) { 245 reg |= LE_B9_FDEN; 246 /* 247 * Allow FDX on AUI only if explicitly chosen, 248 * not in autoselect mode. 249 */ 250 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_5) 251 reg |= LE_B9_AUIFD; 252 else 253 reg &= ~LE_B9_AUIFD; 254 } else 255 reg &= ~LE_B9_FDEN; 256 le_pci_wrbcr(sc, LE_BCR9, reg); 257 258 return (0); 259 } 260 261 static void 262 le_pci_hwreset(struct lance_softc *sc) 263 { 264 265 /* 266 * Chip is stopped. Set software style to PCnet-PCI (32-bit). 267 * Actually, am79900.c implements ILACC support (hence its 268 * name) but unfortunately VMware does not. As far as this 269 * driver is concerned that should not make a difference 270 * though, as the settings used have the same meaning for 271 * both, ILACC and PCnet-PCI (note that there would be a 272 * difference for the ADD_FCS/NO_FCS bit if used). 273 */ 274 le_pci_wrbcr(sc, LE_BCR20, LE_B20_SSTYLE_PCNETPCI2); 275 } 276 277 static void 278 le_pci_dma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 279 { 280 struct lance_softc *sc = (struct lance_softc *)xsc; 281 282 if (error != 0) 283 return; 284 KASSERT(nsegs == 1, ("%s: bad DMA segment count", __func__)); 285 sc->sc_addr = segs[0].ds_addr; 286 } 287 288 static int 289 le_pci_probe(device_t dev) 290 { 291 292 if (pci_get_vendor(dev) != AMD_VENDOR) 293 return (ENXIO); 294 295 switch (pci_get_device(dev)) { 296 case AMD_PCNET_PCI: 297 device_set_desc(dev, "AMD PCnet-PCI"); 298 /* Let pcn(4) win. */ 299 return (LNC_PROBE_PRIORITY); 300 case AMD_PCNET_HOME: 301 device_set_desc(dev, "AMD PCnet-Home"); 302 /* Let pcn(4) win. */ 303 return (LNC_PROBE_PRIORITY); 304 default: 305 return (ENXIO); 306 } 307 } 308 309 static int 310 le_pci_attach(device_t dev) 311 { 312 struct le_pci_softc *lesc; 313 struct lance_softc *sc; 314 int error, i; 315 316 lesc = device_get_softc(dev); 317 sc = &lesc->sc_am79900.lsc; 318 319 pci_enable_busmaster(dev); 320 pci_enable_io(dev, PCIM_CMD_PORTEN); 321 322 lesc->sc_rrid = PCIR_BAR(0); 323 lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_IOPORT, 324 &lesc->sc_rrid, RF_ACTIVE); 325 if (lesc->sc_rres == NULL) { 326 device_printf(dev, "cannot allocate registers\n"); 327 error = ENXIO; 328 goto fail_mtx; 329 } 330 lesc->sc_regt = rman_get_bustag(lesc->sc_rres); 331 lesc->sc_regh = rman_get_bushandle(lesc->sc_rres); 332 333 lesc->sc_irid = 0; 334 if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, 335 &lesc->sc_irid, RF_SHAREABLE | RF_ACTIVE)) == NULL) { 336 device_printf(dev, "cannot allocate interrupt\n"); 337 error = ENXIO; 338 goto fail_rres; 339 } 340 341 error = bus_dma_tag_create( 342 NULL, /* parent */ 343 1, 0, /* alignment, boundary */ 344 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 345 BUS_SPACE_MAXADDR, /* highaddr */ 346 NULL, NULL, /* filter, filterarg */ 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 NULL, NULL, /* filter, filterarg */ 369 sc->sc_memsize, /* maxsize */ 370 1, /* nsegments */ 371 sc->sc_memsize, /* maxsegsize */ 372 BUS_DMA_WAITOK, /* flags */ 373 &lesc->sc_dmat); 374 if (error != 0) { 375 device_printf(dev, "cannot allocate buffer DMA tag\n"); 376 goto fail_pdtag; 377 } 378 379 error = bus_dmamem_alloc(lesc->sc_dmat, (void **)&sc->sc_mem, 380 BUS_DMA_WAITOK | BUS_DMA_COHERENT, &lesc->sc_dmam); 381 if (error != 0) { 382 device_printf(dev, "cannot allocate DMA buffer memory\n"); 383 goto fail_dtag; 384 } 385 386 sc->sc_addr = 0; 387 error = bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmam, sc->sc_mem, 388 sc->sc_memsize, le_pci_dma_callback, sc, 0); 389 if (error != 0 || sc->sc_addr == 0) { 390 device_printf(dev, "cannot load DMA buffer map\n"); 391 goto fail_dmem; 392 } 393 394 sc->sc_flags = LE_BSWAP; 395 sc->sc_conf3 = 0; 396 397 sc->sc_mediastatus = NULL; 398 switch (pci_get_device(dev)) { 399 case AMD_PCNET_HOME: 400 sc->sc_mediachange = le_pci_mediachange; 401 sc->sc_supmedia = le_home_supmedia; 402 sc->sc_nsupmedia = sizeof(le_home_supmedia) / sizeof(int); 403 sc->sc_defaultmedia = le_home_supmedia[0]; 404 break; 405 default: 406 sc->sc_mediachange = le_pci_mediachange; 407 sc->sc_supmedia = le_pci_supmedia; 408 sc->sc_nsupmedia = sizeof(le_pci_supmedia) / sizeof(int); 409 sc->sc_defaultmedia = le_pci_supmedia[0]; 410 } 411 412 /* 413 * Extract the physical MAC address from the ROM. 414 */ 415 for (i = 0; i < sizeof(sc->sc_enaddr); i++) 416 sc->sc_enaddr[i] = 417 bus_space_read_1(lesc->sc_regt, lesc->sc_regh, i); 418 419 sc->sc_copytodesc = lance_copytobuf_contig; 420 sc->sc_copyfromdesc = lance_copyfrombuf_contig; 421 sc->sc_copytobuf = lance_copytobuf_contig; 422 sc->sc_copyfrombuf = lance_copyfrombuf_contig; 423 sc->sc_zerobuf = lance_zerobuf_contig; 424 425 sc->sc_rdcsr = le_pci_rdcsr; 426 sc->sc_wrcsr = le_pci_wrcsr; 427 sc->sc_hwreset = le_pci_hwreset; 428 sc->sc_hwinit = NULL; 429 sc->sc_hwintr = NULL; 430 sc->sc_nocarrier = NULL; 431 432 error = am79900_config(&lesc->sc_am79900, device_get_name(dev), 433 device_get_unit(dev)); 434 if (error != 0) { 435 device_printf(dev, "cannot attach Am79900\n"); 436 goto fail_dmap; 437 } 438 439 error = bus_setup_intr(dev, lesc->sc_ires, INTR_MPSAFE, 440 am79900_intr, sc, &lesc->sc_ih, sc->ifp->if_serializer); 441 if (error != 0) { 442 device_printf(dev, "cannot set up interrupt\n"); 443 goto fail_am79900; 444 } 445 446 sc->ifp->if_cpuid = ithread_cpuid(rman_get_start(lesc->sc_ires)); 447 KKASSERT(sc->ifp->if_cpuid >= 0 && sc->ifp->if_cpuid < ncpus); 448 449 return (0); 450 451 fail_am79900: 452 am79900_detach(&lesc->sc_am79900); 453 fail_dmap: 454 bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam); 455 fail_dmem: 456 bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam); 457 fail_dtag: 458 bus_dma_tag_destroy(lesc->sc_dmat); 459 fail_pdtag: 460 bus_dma_tag_destroy(lesc->sc_pdmat); 461 fail_ires: 462 bus_release_resource(dev, SYS_RES_IRQ, lesc->sc_irid, lesc->sc_ires); 463 fail_rres: 464 bus_release_resource(dev, SYS_RES_IOPORT, lesc->sc_rrid, lesc->sc_rres); 465 fail_mtx: 466 return (error); 467 } 468 469 static int 470 le_pci_detach(device_t dev) 471 { 472 struct le_pci_softc *lesc; 473 struct lance_softc *sc; 474 475 lesc = device_get_softc(dev); 476 sc = &lesc->sc_am79900.lsc; 477 478 if (device_is_attached(dev)) { 479 lwkt_serialize_enter(sc->ifp->if_serializer); 480 lance_stop(sc); 481 bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih); 482 lwkt_serialize_exit(sc->ifp->if_serializer); 483 484 am79900_detach(&lesc->sc_am79900); 485 } 486 487 if (lesc->sc_ires) 488 bus_release_resource(dev, SYS_RES_IRQ, lesc->sc_irid, lesc->sc_ires); 489 if (lesc->sc_rres) 490 bus_release_resource(dev, SYS_RES_IOPORT, lesc->sc_rrid, lesc->sc_rres); 491 if (lesc->sc_dmam) { 492 bus_dmamap_unload(lesc->sc_dmat, lesc->sc_dmam); 493 bus_dmamem_free(lesc->sc_dmat, sc->sc_mem, lesc->sc_dmam); 494 } 495 if (lesc->sc_dmat) 496 bus_dma_tag_destroy(lesc->sc_dmat); 497 if (lesc->sc_pdmat) 498 bus_dma_tag_destroy(lesc->sc_pdmat); 499 500 return (0); 501 } 502 503 static int 504 le_pci_suspend(device_t dev) 505 { 506 struct le_pci_softc *lesc; 507 508 lesc = device_get_softc(dev); 509 510 lance_suspend(&lesc->sc_am79900.lsc); 511 512 return (0); 513 } 514 515 static int 516 le_pci_resume(device_t dev) 517 { 518 struct le_pci_softc *lesc; 519 520 lesc = device_get_softc(dev); 521 522 lance_resume(&lesc->sc_am79900.lsc); 523 524 return (0); 525 } 526