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