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