Home | History | Annotate | Line # | Download | only in pci
if_tlp_pci.c revision 1.21
      1 /*	$NetBSD: if_tlp_pci.c,v 1.21 1999/10/28 23:32:37 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * 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  * PCI bus front-end for the Digital Semiconductor ``Tulip'' (21x4x)
     42  * Ethernet controller family driver.
     43  */
     44 
     45 #include "opt_inet.h"
     46 #include "opt_ns.h"
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/malloc.h>
     53 #include <sys/kernel.h>
     54 #include <sys/socket.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/errno.h>
     57 #include <sys/device.h>
     58 
     59 #include <net/if.h>
     60 #include <net/if_dl.h>
     61 #include <net/if_media.h>
     62 #include <net/if_ether.h>
     63 
     64 #if NBPFILTER > 0
     65 #include <net/bpf.h>
     66 #endif
     67 
     68 #ifdef INET
     69 #include <netinet/in.h>
     70 #include <netinet/if_inarp.h>
     71 #endif
     72 
     73 #ifdef NS
     74 #include <netns/ns.h>
     75 #include <netns/ns_if.h>
     76 #endif
     77 
     78 #include <machine/bus.h>
     79 #include <machine/intr.h>
     80 
     81 #include <dev/mii/miivar.h>
     82 
     83 #include <dev/ic/tulipreg.h>
     84 #include <dev/ic/tulipvar.h>
     85 
     86 #include <dev/pci/pcivar.h>
     87 #include <dev/pci/pcireg.h>
     88 #include <dev/pci/pcidevs.h>
     89 
     90 /*
     91  * PCI configuration space registers used by the Tulip.
     92  */
     93 #define	TULIP_PCI_IOBA		0x10	/* i/o mapped base */
     94 #define	TULIP_PCI_MMBA		0x14	/* memory mapped base */
     95 #define	TULIP_PCI_CFDA		0x40	/* configuration driver area */
     96 
     97 #define	CFDA_SLEEP		0x80000000	/* sleep mode */
     98 
     99 struct tulip_pci_softc {
    100 	struct tulip_softc sc_tulip;	/* real Tulip softc */
    101 
    102 	/* PCI-specific goo. */
    103 	void	*sc_ih;			/* interrupt handle */
    104 
    105 	pci_chipset_tag_t sc_pc;	/* our PCI chipset */
    106 	pcitag_t sc_pcitag;		/* our PCI tag */
    107 
    108 	int	sc_flags;		/* flags; see below */
    109 
    110 	LIST_HEAD(, tulip_pci_softc) sc_intrslaves;
    111 	LIST_ENTRY(tulip_pci_softc) sc_intrq;
    112 
    113 	/* Our {ROM,interrupt} master. */
    114 	struct tulip_pci_softc *sc_master;
    115 };
    116 
    117 /* sc_flags */
    118 #define	TULIP_PCI_SHAREDINTR	0x01	/* interrupt is shared */
    119 #define	TULIP_PCI_SLAVEINTR	0x02	/* interrupt is slave */
    120 #define	TULIP_PCI_SHAREDROM	0x04	/* ROM is shared */
    121 #define	TULIP_PCI_SLAVEROM	0x08	/* slave of shared ROM */
    122 
    123 int	tlp_pci_match __P((struct device *, struct cfdata *, void *));
    124 void	tlp_pci_attach __P((struct device *, struct device *, void *));
    125 
    126 struct cfattach tlp_pci_ca = {
    127 	sizeof(struct tulip_pci_softc), tlp_pci_match, tlp_pci_attach,
    128 };
    129 
    130 const struct tulip_pci_product {
    131 	u_int32_t	tpp_vendor;	/* PCI vendor ID */
    132 	u_int32_t	tpp_product;	/* PCI product ID */
    133 	tulip_chip_t	tpp_chip;	/* base Tulip chip type */
    134 	int		tpp_pmreg;	/* power management register offset */
    135 } tlp_pci_products[] = {
    136 #ifdef TLP_MATCH_21040
    137 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21040,
    138 	  TULIP_CHIP_21040,		0 },
    139 #endif
    140 #ifdef TLP_MATCH_21041
    141 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21041,
    142 	  TULIP_CHIP_21041,		0 },
    143 #endif
    144 #ifdef TLP_MATCH_21140
    145 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21140,
    146 	  TULIP_CHIP_21140,		0 },
    147 #endif
    148 #ifdef TLP_MATCH_21142
    149 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21142,
    150 	  TULIP_CHIP_21142,		0 },
    151 #endif
    152 
    153 	{ PCI_VENDOR_LITEON,		PCI_PRODUCT_LITEON_82C168,
    154 	  TULIP_CHIP_82C168,		0 },
    155 
    156 	/*
    157 	 * Note: This is like a MX98725 with Wake-On-LAN and a
    158 	 * 128-bit multicast hash table.
    159 	 */
    160 	{ PCI_VENDOR_LITEON,		PCI_PRODUCT_LITEON_82C115,
    161 	  TULIP_CHIP_82C115,		0x48 },
    162 
    163 	{ PCI_VENDOR_MACRONIX,		PCI_PRODUCT_MACRONIX_MX98713,
    164 	  TULIP_CHIP_MX98713,		0 },
    165 	{ PCI_VENDOR_MACRONIX,		PCI_PRODUCT_MACRONIX_MX987x5,
    166 	  TULIP_CHIP_MX98715,		0x48 },
    167 
    168 	{ PCI_VENDOR_COMPEX,		PCI_PRODUCT_COMPEX_RL100TX,
    169 	  TULIP_CHIP_MX98713,		0 },
    170 
    171 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C840F,
    172 	  TULIP_CHIP_WB89C840F,		0 },
    173 	{ PCI_VENDOR_COMPEX,		PCI_PRODUCT_COMPEX_RL100ATX,
    174 	  TULIP_CHIP_WB89C840F,		0 },
    175 
    176 #if 0
    177 	{ PCI_VENDOR_DAVICOM,		PCI_PRODUCT_DAVICOM_DM9102,
    178 	  TULIP_CHIP_DM9102,		0 },
    179 #endif
    180 
    181 	{ PCI_VENDOR_ADMTEK,		PCI_PRODUCT_ADMTEK_AL981,
    182 	  TULIP_CHIP_AL981,		0xc4 },
    183 
    184 #if 0
    185 	{ PCI_VENDOR_ASIX,		PCI_PRODUCT_ASIX_AX88140A,
    186 	  TULIP_CHIP_AX88140,		0 },
    187 #endif
    188 
    189 	{ 0,				0,
    190 	  TULIP_CHIP_INVALID,		0 },
    191 };
    192 
    193 struct tlp_pci_quirks {
    194 	void		(*tpq_func) __P((struct tulip_pci_softc *,
    195 			    const u_int8_t *));
    196 	u_int8_t	tpq_oui[3];
    197 };
    198 
    199 void	tlp_pci_dec_quirks __P((struct tulip_pci_softc *,
    200 	    const u_int8_t *));
    201 
    202 void	tlp_pci_znyx_21040_quirks __P((struct tulip_pci_softc *,
    203 	    const u_int8_t *));
    204 void	tlp_pci_smc_21040_quirks __P((struct tulip_pci_softc *,
    205 	    const u_int8_t *));
    206 void	tlp_pci_cogent_21040_quirks __P((struct tulip_pci_softc *,
    207 	    const u_int8_t *));
    208 void	tlp_pci_accton_21040_quirks __P((struct tulip_pci_softc *,
    209 	    const u_int8_t *));
    210 
    211 const struct tlp_pci_quirks tlp_pci_21040_quirks[] = {
    212 	{ tlp_pci_znyx_21040_quirks,	{ 0x00, 0xc0, 0x95 } },
    213 	{ tlp_pci_smc_21040_quirks,	{ 0x00, 0x00, 0xc0 } },
    214 	{ tlp_pci_cogent_21040_quirks,	{ 0x00, 0x00, 0x92 } },
    215 	{ tlp_pci_accton_21040_quirks,	{ 0x00, 0x00, 0xe8 } },
    216 	{ NULL,				{ 0, 0, 0 } }
    217 };
    218 
    219 const struct tlp_pci_quirks tlp_pci_21041_quirks[] = {
    220 	{ tlp_pci_dec_quirks,		{ 0x08, 0x00, 0x2b } },
    221 	{ tlp_pci_dec_quirks,		{ 0x00, 0x00, 0xf8 } },
    222 	{ NULL,				{ 0, 0, 0 } }
    223 };
    224 
    225 void	tlp_pci_asante_21140_quirks __P((struct tulip_pci_softc *,
    226 	    const u_int8_t *));
    227 
    228 const struct tlp_pci_quirks tlp_pci_21140_quirks[] = {
    229 	{ tlp_pci_dec_quirks,		{ 0x08, 0x00, 0x2b } },
    230 	{ tlp_pci_dec_quirks,		{ 0x00, 0x00, 0xf8 } },
    231 	{ tlp_pci_asante_21140_quirks,	{ 0x00, 0x00, 0x94 } },
    232 	{ NULL,				{ 0, 0, 0 } }
    233 };
    234 
    235 const struct tlp_pci_quirks tlp_pci_21142_quirks[] = {
    236 	{ tlp_pci_dec_quirks,		{ 0x08, 0x00, 0x2b } },
    237 	{ tlp_pci_dec_quirks,		{ 0x00, 0x00, 0xf8 } },
    238 	{ NULL,				{ 0, 0, 0 } }
    239 };
    240 
    241 const char *tlp_pci_chip_names[] = TULIP_CHIP_NAMES;
    242 
    243 int	tlp_pci_shared_intr __P((void *));
    244 
    245 const struct tulip_pci_product *tlp_pci_lookup
    246     __P((const struct pci_attach_args *));
    247 void tlp_pci_get_quirks __P((struct tulip_pci_softc *, const u_int8_t *,
    248     const struct tlp_pci_quirks *));
    249 void tlp_pci_check_slaved __P((struct tulip_pci_softc *, int, int));
    250 
    251 const struct tulip_pci_product *
    252 tlp_pci_lookup(pa)
    253 	const struct pci_attach_args *pa;
    254 {
    255 	const struct tulip_pci_product *tpp;
    256 
    257 	for (tpp = tlp_pci_products;
    258 	     tlp_pci_chip_names[tpp->tpp_chip] != NULL;
    259 	     tpp++) {
    260 		if (PCI_VENDOR(pa->pa_id) == tpp->tpp_vendor &&
    261 		    PCI_PRODUCT(pa->pa_id) == tpp->tpp_product)
    262 			return (tpp);
    263 	}
    264 	return (NULL);
    265 }
    266 
    267 void
    268 tlp_pci_get_quirks(psc, enaddr, tpq)
    269 	struct tulip_pci_softc *psc;
    270 	const u_int8_t *enaddr;
    271 	const struct tlp_pci_quirks *tpq;
    272 {
    273 
    274 	for (; tpq->tpq_func != NULL; tpq++) {
    275 		if (tpq->tpq_oui[0] == enaddr[0] &&
    276 		    tpq->tpq_oui[1] == enaddr[1] &&
    277 		    tpq->tpq_oui[2] == enaddr[2]) {
    278 			(*tpq->tpq_func)(psc, enaddr);
    279 			return;
    280 		}
    281 	}
    282 }
    283 
    284 void
    285 tlp_pci_check_slaved(psc, shared, slaved)
    286 	struct tulip_pci_softc *psc;
    287 	int shared, slaved;
    288 {
    289 	extern struct cfdriver tlp_cd;
    290 	struct tulip_pci_softc *cur, *best = NULL;
    291 	struct tulip_softc *sc = &psc->sc_tulip;
    292 	int i;
    293 
    294 	/*
    295 	 * First of all, find the lowest pcidev numbered device on our
    296 	 * bus marked as shared.  That should be our master.
    297 	 */
    298 	for (i = 0; i < tlp_cd.cd_ndevs; i++) {
    299 		if ((cur = tlp_cd.cd_devs[i]) == NULL)
    300 			continue;
    301 		if (cur->sc_tulip.sc_dev.dv_parent != sc->sc_dev.dv_parent)
    302 			continue;
    303 		if ((cur->sc_flags & shared) == 0)
    304 			continue;
    305 		if (cur == psc)
    306 			continue;
    307 		if (best == NULL ||
    308 		    best->sc_tulip.sc_devno > cur->sc_tulip.sc_devno)
    309 			best = cur;
    310 	}
    311 
    312 	if (best != NULL) {
    313 		psc->sc_master = best;
    314 		psc->sc_flags |= (shared | slaved);
    315 	}
    316 }
    317 
    318 int
    319 tlp_pci_match(parent, match, aux)
    320 	struct device *parent;
    321 	struct cfdata *match;
    322 	void *aux;
    323 {
    324 	struct pci_attach_args *pa = aux;
    325 
    326 	if (tlp_pci_lookup(pa) != NULL)
    327 		return (10);	/* beat if_de.c */
    328 
    329 	return (0);
    330 }
    331 
    332 void
    333 tlp_pci_attach(parent, self, aux)
    334 	struct device *parent, *self;
    335 	void *aux;
    336 {
    337 	struct tulip_pci_softc *psc = (void *) self;
    338 	struct tulip_softc *sc = &psc->sc_tulip;
    339 	struct pci_attach_args *pa = aux;
    340 	pci_chipset_tag_t pc = pa->pa_pc;
    341 	pci_intr_handle_t ih;
    342 	const char *intrstr = NULL;
    343 	bus_space_tag_t iot, memt;
    344 	bus_space_handle_t ioh, memh;
    345 	int ioh_valid, memh_valid, i, j;
    346 	const struct tulip_pci_product *tpp;
    347 	u_int8_t enaddr[ETHER_ADDR_LEN];
    348 	u_int32_t val;
    349 	pcireg_t reg;
    350 
    351 	sc->sc_devno = pa->pa_device;
    352 	psc->sc_pc = pa->pa_pc;
    353 	psc->sc_pcitag = pa->pa_tag;
    354 
    355 	LIST_INIT(&psc->sc_intrslaves);
    356 
    357 	tpp = tlp_pci_lookup(pa);
    358 	if (tpp == NULL) {
    359 		printf("\n");
    360 		panic("tlp_pci_attach: impossible");
    361 	}
    362 	sc->sc_chip = tpp->tpp_chip;
    363 
    364 	/*
    365 	 * By default, Tulip registers are 8 bytes long (4 bytes
    366 	 * followed by a 4 byte pad).
    367 	 */
    368 	sc->sc_regshift = 3;
    369 
    370 	/*
    371 	 * Get revision info, and set some chip-specific variables.
    372 	 */
    373 	sc->sc_rev = PCI_REVISION(pa->pa_class);
    374 	switch (sc->sc_chip) {
    375 	case TULIP_CHIP_21140:
    376 		if (sc->sc_rev >= 0x20)
    377 			sc->sc_chip = TULIP_CHIP_21140A;
    378 		break;
    379 
    380 	case TULIP_CHIP_21142:
    381 		if (sc->sc_rev >= 0x20)
    382 			sc->sc_chip = TULIP_CHIP_21143;
    383 		break;
    384 
    385 	case TULIP_CHIP_82C168:
    386 		if (sc->sc_rev >= 0x20)
    387 			sc->sc_chip = TULIP_CHIP_82C169;
    388 		break;
    389 
    390 	case TULIP_CHIP_MX98713:
    391 		if (sc->sc_rev >= 0x10)
    392 			sc->sc_chip = TULIP_CHIP_MX98713A;
    393 		break;
    394 
    395 	case TULIP_CHIP_MX98715:
    396 		if (sc->sc_rev >= 0x20)
    397 			sc->sc_chip = TULIP_CHIP_MX98715A;
    398 		if (sc->sc_rev >= 0x30)
    399 			sc->sc_chip = TULIP_CHIP_MX98725;
    400 		break;
    401 
    402 	case TULIP_CHIP_WB89C840F:
    403 		sc->sc_regshift = 2;
    404 		break;
    405 
    406 	case TULIP_CHIP_AX88140:
    407 		if (sc->sc_rev >= 0x10)
    408 			sc->sc_chip = TULIP_CHIP_AX88141;
    409 		break;
    410 
    411 	default:
    412 		/* Nothing. */
    413 	}
    414 
    415 	printf(": %s Ethernet, pass %d.%d\n",
    416 	    tlp_pci_chip_names[sc->sc_chip],
    417 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    418 
    419 	switch (sc->sc_chip) {
    420 	case TULIP_CHIP_21040:
    421 		if (sc->sc_rev < 0x20) {
    422 			printf("%s: 21040 must be at least pass 2.0\n",
    423 			    sc->sc_dev.dv_xname);
    424 			return;
    425 		}
    426 		break;
    427 
    428 	case TULIP_CHIP_21140:
    429 		if (sc->sc_rev < 0x11) {
    430 			printf("%s: 21140 must be at least pass 1.1\n",
    431 			    sc->sc_dev.dv_xname);
    432 			return;
    433 		}
    434 		break;
    435 
    436 	default:
    437 		/* Nothing. */
    438 	}
    439 
    440 	/*
    441 	 * Check to see if the device is in power-save mode, and
    442 	 * being it out if necessary.
    443 	 */
    444 	switch (sc->sc_chip) {
    445 	case TULIP_CHIP_21140:
    446 	case TULIP_CHIP_21140A:
    447 	case TULIP_CHIP_MX98713A:
    448 	case TULIP_CHIP_MX98715:
    449 	case TULIP_CHIP_MX98715A:
    450 	case TULIP_CHIP_MX98725:
    451 		/*
    452 		 * Clear the "sleep mode" bit in the CFDA register.
    453 		 */
    454 		reg = pci_conf_read(pc, pa->pa_tag, TULIP_PCI_CFDA);
    455 		if (reg & CFDA_SLEEP)
    456 			pci_conf_write(pc, pa->pa_tag, TULIP_PCI_CFDA,
    457 			    reg & ~CFDA_SLEEP);
    458 		break;
    459 
    460 	default:
    461 		/* Nothing. */
    462 	}
    463 
    464 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, 0, 0)) {
    465 		if (tpp->tpp_pmreg == 0) {
    466 			printf("%s: don't know location of PMCSR for this "
    467 			    "chip\n", sc->sc_dev.dv_xname);
    468 			return;
    469 		}
    470 		reg = pci_conf_read(pc, pa->pa_tag, tpp->tpp_pmreg) & 0x3;
    471 		if (reg == 3) {
    472 			/*
    473 			 * The card has lost all configuration data in
    474 			 * this state, so punt.
    475 			 */
    476 			printf("%s: unable to wake up from power state D3\n",
    477 			    sc->sc_dev.dv_xname);
    478 			return;
    479 		}
    480 		if (reg != 0) {
    481 			printf("%s: waking up from power state D%d\n",
    482 			    sc->sc_dev.dv_xname, reg);
    483 			pci_conf_write(pc, pa->pa_tag, tpp->tpp_pmreg, 0);
    484 		}
    485 	}
    486 
    487 	/*
    488 	 * Map the device.
    489 	 */
    490 	ioh_valid = (pci_mapreg_map(pa, TULIP_PCI_IOBA,
    491 	    PCI_MAPREG_TYPE_IO, 0,
    492 	    &iot, &ioh, NULL, NULL) == 0);
    493 	memh_valid = (pci_mapreg_map(pa, TULIP_PCI_MMBA,
    494 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    495 	    &memt, &memh, NULL, NULL) == 0);
    496 
    497 	if (memh_valid) {
    498 		sc->sc_st = memt;
    499 		sc->sc_sh = memh;
    500 	} else if (ioh_valid) {
    501 		sc->sc_st = iot;
    502 		sc->sc_sh = ioh;
    503 	} else {
    504 		printf(": unable to map device registers\n");
    505 		return;
    506 	}
    507 
    508 	sc->sc_dmat = pa->pa_dmat;
    509 
    510 	/*
    511 	 * Make sure bus mastering is enabled.
    512 	 */
    513 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    514 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    515 	    PCI_COMMAND_MASTER_ENABLE);
    516 
    517 	/*
    518 	 * Get the cacheline size.
    519 	 */
    520 	sc->sc_cacheline = PCI_CACHELINE(pci_conf_read(pc, pa->pa_tag,
    521 	    PCI_BHLC_REG));
    522 
    523 	/*
    524 	 * Read the contents of the Ethernet Address ROM/SROM.
    525 	 */
    526 	memset(sc->sc_srom, 0, sizeof(sc->sc_srom));
    527 	switch (sc->sc_chip) {
    528 	case TULIP_CHIP_21040:
    529 		TULIP_WRITE(sc, CSR_MIIROM, MIIROM_SROMCS);
    530 		for (i = 0; i < sizeof(sc->sc_srom); i++) {
    531 			for (j = 0; j < 10000; j++) {
    532 				val = TULIP_READ(sc, CSR_MIIROM);
    533 				if ((val & MIIROM_DN) == 0)
    534 					break;
    535 			}
    536 			sc->sc_srom[i] = val & MIIROM_DATA;
    537 		}
    538 		break;
    539 
    540 	case TULIP_CHIP_82C168:
    541 	case TULIP_CHIP_82C169:
    542 	    {
    543 		u_int16_t *rombuf = (u_int16_t *)sc->sc_srom;
    544 
    545 		/*
    546 		 * The Lite-On PNIC stores the Ethernet address in
    547 		 * the first 3 words of the EEPROM.  EEPROM access
    548 		 * is not like the other Tulip chips.
    549 		 */
    550 		for (i = 0; i < 3; i++) {
    551 			TULIP_WRITE(sc, CSR_PNIC_SROMCTL,
    552 			    PNIC_SROMCTL_READ | i);
    553 			for (j = 0; j < 500; j++) {
    554 				delay(2);
    555 				val = TULIP_READ(sc, CSR_MIIROM);
    556 				if ((val & PNIC_MIIROM_BUSY) == 0)
    557 					break;
    558 			}
    559 			if (val & PNIC_MIIROM_BUSY) {
    560 				printf("%s: EEPROM timed out\n",
    561 				    sc->sc_dev.dv_xname);
    562 				return;
    563 			}
    564 			rombuf[i] = bswap16(val & PNIC_MIIROM_DATA);
    565 		}
    566 		break;
    567 	    }
    568 
    569 	default:
    570 		tlp_read_srom(sc, 0, sizeof(sc->sc_srom) >> 1, sc->sc_srom);
    571 #if 0
    572 		printf("SROM CONTENTS:");
    573 		for (i = 0; i < sizeof(sc->sc_srom); i++) {
    574 			if ((i % 8) == 0)
    575 				printf("\n\t");
    576 			printf("0x%02x ", sc->sc_srom[i]);
    577 		}
    578 		printf("\n");
    579 #endif
    580 	}
    581 
    582 	/*
    583 	 * Deal with chip/board quirks.  This includes setting up
    584 	 * the mediasw, and extracting the Ethernet address from
    585 	 * the rombuf.
    586 	 */
    587 	switch (sc->sc_chip) {
    588 	case TULIP_CHIP_21040:
    589 		/* Check for a slaved ROM on a multi-port board. */
    590 		tlp_pci_check_slaved(psc, TULIP_PCI_SHAREDROM,
    591 		    TULIP_PCI_SLAVEROM);
    592 		if (psc->sc_flags & TULIP_PCI_SLAVEROM)
    593 			memcpy(sc->sc_srom, psc->sc_master->sc_tulip.sc_srom,
    594 			    sizeof(sc->sc_srom));
    595 
    596 		/*
    597 		 * Parse the Ethernet Address ROM.
    598 		 */
    599 		if (tlp_parse_old_srom(sc, enaddr) == 0) {
    600 			printf("%s: unable to decode Ethernet Address ROM\n",
    601 			    sc->sc_dev.dv_xname);
    602 			return;
    603 		}
    604 
    605 		/*
    606 		 * If we have a slaved ROM, adjust the Ethernet address.
    607 		 */
    608 		if (psc->sc_flags & TULIP_PCI_SLAVEROM)
    609 			enaddr[5] +=
    610 			    sc->sc_devno - psc->sc_master->sc_tulip.sc_devno;
    611 
    612 		/*
    613 		 * All 21040 boards start out with the same
    614 		 * media switch.
    615 		 */
    616 		sc->sc_mediasw = &tlp_21040_mediasw;
    617 
    618 		/*
    619 		 * Deal with any quirks this board might have.
    620 		 */
    621 		tlp_pci_get_quirks(psc, enaddr, tlp_pci_21040_quirks);
    622 		break;
    623 
    624 	case TULIP_CHIP_21041:
    625 		/* Check for a slaved ROM on a multi-port board. */
    626 		tlp_pci_check_slaved(psc, TULIP_PCI_SHAREDROM,
    627 		    TULIP_PCI_SLAVEROM);
    628 		if (psc->sc_flags & TULIP_PCI_SLAVEROM)
    629 			memcpy(sc->sc_srom, psc->sc_master->sc_tulip.sc_srom,
    630 			    sizeof(sc->sc_srom));
    631 
    632 		/* Check for new format SROM. */
    633 		if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
    634 			/*
    635 			 * Not an ISV SROM; try the old DEC Ethernet Address
    636 			 * ROM format.
    637 			 */
    638 			if (tlp_parse_old_srom(sc, enaddr) == 0) {
    639 				printf("%s: unable to decode Ethernet "
    640 				    "Address ROM\n", sc->sc_dev.dv_xname);
    641 				return;
    642 			}
    643 		}
    644 
    645 		/*
    646 		 * All 21041 boards use the same media switch; they all
    647 		 * work basically the same!  Yippee!
    648 		 */
    649 		sc->sc_mediasw = &tlp_21041_mediasw;
    650 
    651 		/*
    652 		 * Deal with any quirks this board might have.
    653 		 */
    654 		tlp_pci_get_quirks(psc, enaddr, tlp_pci_21041_quirks);
    655 		break;
    656 
    657 	case TULIP_CHIP_21140:
    658 	case TULIP_CHIP_21140A:
    659 		/* Check for new format SROM. */
    660 		if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
    661 			/*
    662 			 * Not an ISV SROM; try the old DEC Ethernet Address
    663 			 * ROM format.
    664 			 */
    665 			if (tlp_parse_old_srom(sc, enaddr) == 0) {
    666 				printf("%s: unable to decode Ethernet "
    667 				    "Address ROM\n", sc->sc_dev.dv_xname);
    668 				return;
    669 			}
    670 		} else {
    671 			/*
    672 			 * We start out with the 2114x ISV media switch.
    673 			 * When we search for quirks, we may change to
    674 			 * a different switch.
    675 			 */
    676 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    677 		}
    678 
    679 		/*
    680 		 * Deal with any quirks this board might have.
    681 		 */
    682 		tlp_pci_get_quirks(psc, enaddr, tlp_pci_21140_quirks);
    683 
    684 		/*
    685 		 * Bail out now if we can't deal with this board.
    686 		 */
    687 		if (sc->sc_mediasw == NULL)
    688 			goto cant_cope;
    689 		break;
    690 
    691 	case TULIP_CHIP_21142:
    692 	case TULIP_CHIP_21143:
    693 		/* Check for new format SROM. */
    694 		if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
    695 			/*
    696 			 * Not an ISV SROM; can't cope, for now.
    697 			 */
    698 			goto cant_cope;
    699 		} else {
    700 			/*
    701 			 * We start out with the 2114x ISV media switch.
    702 			 * When we search for quirks, we may change to
    703 			 * a different switch.
    704 			 */
    705 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    706 		}
    707 
    708 		/*
    709 		 * Deal with any quirks this board might have.
    710 		 */
    711 		tlp_pci_get_quirks(psc, enaddr, tlp_pci_21142_quirks);
    712 
    713 		/*
    714 		 * Bail out now if we can't deal with this board.
    715 		 */
    716 		if (sc->sc_mediasw == NULL)
    717 			goto cant_cope;
    718 		break;
    719 
    720 	case TULIP_CHIP_82C168:
    721 	case TULIP_CHIP_82C169:
    722 		/*
    723 		 * Lite-On PNIC's Ethernet address is the first 6
    724 		 * bytes of its EEPROM.
    725 		 */
    726 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
    727 
    728 		/*
    729 		 * Lite-On PNICs always use the same mediasw; we
    730 		 * select MII vs. internal NWAY automatically.
    731 		 */
    732 		sc->sc_mediasw = &tlp_pnic_mediasw;
    733 		break;
    734 
    735 	case TULIP_CHIP_MX98713:
    736 		/*
    737 		 * The Macronix MX98713 has an MII and GPIO, but no
    738 		 * internal Nway block.  This chip is basically a
    739 		 * perfect 21140A clone, with the exception of the
    740 		 * a magic register frobbing in order to make the
    741 		 * interface function.
    742 		 */
    743 		if (tlp_isv_srom_enaddr(sc, enaddr)) {
    744 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    745 			break;
    746 		}
    747 		/* FALLTHROUGH */
    748 
    749 	case TULIP_CHIP_82C115:
    750 		/*
    751 		 * Yippee!  The Lite-On 82C115 is a clone of
    752 		 * the MX98725 (the data sheet even says `MXIC'
    753 		 * on it)!  Imagine that, a clone of a clone.
    754 		 *
    755 		 * The differences are really minimal:
    756 		 *
    757 		 *	- Wake-On-LAN support
    758 		 *	- 128-bit multicast hash table, rather than
    759 		 *	  the standard 512-bit hash table
    760 		 */
    761 		/* FALLTHROUGH */
    762 
    763 	case TULIP_CHIP_MX98713A:
    764 	case TULIP_CHIP_MX98715A:
    765 	case TULIP_CHIP_MX98725:
    766 		/*
    767 		 * The MX98713A has an MII as well as an internal Nway block,
    768 		 * but no GPIO.  The MX98715 and MX98725 have an internal
    769 		 * Nway block only.
    770 		 *
    771 		 * The internal Nway block, unlike the Lite-On PNIC's, does
    772 		 * just that - performs Nway.  Once autonegotiation completes,
    773 		 * we must program the GPR media information into the chip.
    774 		 *
    775 		 * The byte offset of the Ethernet address is stored at
    776 		 * offset 0x70.
    777 		 */
    778 		memcpy(enaddr, &sc->sc_srom[sc->sc_srom[0x70]], ETHER_ADDR_LEN);
    779 		sc->sc_mediasw = &tlp_pmac_mediasw;
    780 		break;
    781 
    782 	case TULIP_CHIP_WB89C840F:
    783 		/*
    784 		 * Winbond 89C840F's Ethernet address is the first
    785 		 * 6 bytes of its EEPROM.
    786 		 */
    787 		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
    788 
    789 		/*
    790 		 * Winbond 89C840F has an MII attached to the SIO.
    791 		 */
    792 		sc->sc_mediasw = &tlp_sio_mii_mediasw;
    793 		break;
    794 
    795 	case TULIP_CHIP_AL981:
    796 		/*
    797 		 * The ADMtek AL981's Ethernet address is located
    798 		 * at offset 8 of its EEPROM.
    799 		 */
    800 		memcpy(enaddr, &sc->sc_srom[8], ETHER_ADDR_LEN);
    801 
    802 		/*
    803 		 * ADMtek AL981 has a built-in PHY accessed through
    804 		 * special registers.
    805 		 */
    806 		sc->sc_mediasw = &tlp_al981_mediasw;
    807 		break;
    808 
    809 	default:
    810  cant_cope:
    811 		printf("%s: sorry, unable to handle your board\n",
    812 		    sc->sc_dev.dv_xname);
    813 		return;
    814 	}
    815 
    816 	/*
    817 	 * Handle shared interrupts.
    818 	 */
    819 	if (psc->sc_flags & TULIP_PCI_SHAREDINTR) {
    820 		if (psc->sc_master)
    821 			psc->sc_flags |= TULIP_PCI_SLAVEINTR;
    822 		else {
    823 			tlp_pci_check_slaved(psc, TULIP_PCI_SHAREDINTR,
    824 			    TULIP_PCI_SLAVEINTR);
    825 			if (psc->sc_master == NULL)
    826 				psc->sc_master = psc;
    827 		}
    828 		LIST_INSERT_HEAD(&psc->sc_master->sc_intrslaves,
    829 		    psc, sc_intrq);
    830 	}
    831 
    832 	if (psc->sc_flags & TULIP_PCI_SLAVEINTR) {
    833 		printf("%s: sharing interrupt with %s\n",
    834 		    sc->sc_dev.dv_xname,
    835 		    psc->sc_master->sc_tulip.sc_dev.dv_xname);
    836 	} else {
    837 		/*
    838 		 * Map and establish our interrupt.
    839 		 */
    840 		if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
    841 		    pa->pa_intrline, &ih)) {
    842 			printf("%s: unable to map interrupt\n",
    843 			    sc->sc_dev.dv_xname);
    844 			return;
    845 		}
    846 		intrstr = pci_intr_string(pc, ih);
    847 		psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET,
    848 		    (psc->sc_flags & TULIP_PCI_SHAREDINTR) ?
    849 		    tlp_pci_shared_intr : tlp_intr, sc);
    850 		if (psc->sc_ih == NULL) {
    851 			printf("%s: unable to establish interrupt",
    852 			    sc->sc_dev.dv_xname);
    853 			if (intrstr != NULL)
    854 				printf(" at %s", intrstr);
    855 			printf("\n");
    856 			return;
    857 		}
    858 		printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname,
    859 		    intrstr);
    860 	}
    861 
    862 	/*
    863 	 * Finish off the attach.
    864 	 */
    865 	tlp_attach(sc, enaddr);
    866 }
    867 
    868 int
    869 tlp_pci_shared_intr(arg)
    870 	void *arg;
    871 {
    872 	struct tulip_pci_softc *master = arg, *slave;
    873 	int rv = 0;
    874 
    875 	for (slave = LIST_FIRST(&master->sc_intrslaves);
    876 	     slave != NULL;
    877 	     slave = LIST_NEXT(slave, sc_intrq))
    878 		rv |= tlp_intr(&slave->sc_tulip);
    879 
    880 	return (rv);
    881 }
    882 
    883 void
    884 tlp_pci_dec_quirks(psc, enaddr)
    885 	struct tulip_pci_softc *psc;
    886 	const u_int8_t *enaddr;
    887 {
    888 	struct tulip_softc *sc = &psc->sc_tulip;
    889 
    890 	/*
    891 	 * This isn't really a quirk-gathering device, really.  We
    892 	 * just want to get the spiffy DEC board name from the SROM.
    893 	 */
    894 	strcpy(sc->sc_name, "DEC ");
    895 
    896 	if (memcmp(&sc->sc_srom[29], "DE500", 5) == 0 ||
    897 	    memcmp(&sc->sc_srom[29], "DE450", 5) == 0)
    898 		memcpy(&sc->sc_name[4], &sc->sc_srom[29], 8);
    899 }
    900 
    901 void
    902 tlp_pci_znyx_21040_quirks(psc, enaddr)
    903 	struct tulip_pci_softc *psc;
    904 	const u_int8_t *enaddr;
    905 {
    906 	struct tulip_softc *sc = &psc->sc_tulip;
    907 	u_int16_t id = 0;
    908 
    909 	/*
    910 	 * If we have a slaved ROM, just copy the bits from the master.
    911 	 * This is in case we fail the ROM ID check (older boards) and
    912 	 * need to fall back on Ethernet address model checking; that
    913 	 * will fail for slave chips.
    914 	 */
    915 	if (psc->sc_flags & TULIP_PCI_SLAVEROM) {
    916 		strcpy(sc->sc_name, psc->sc_master->sc_tulip.sc_name);
    917 		sc->sc_mediasw = psc->sc_master->sc_tulip.sc_mediasw;
    918 		psc->sc_flags |=
    919 		    psc->sc_master->sc_flags & TULIP_PCI_SHAREDINTR;
    920 		return;
    921 	}
    922 
    923 	if (sc->sc_srom[32] == 0x4a && sc->sc_srom[33] == 0x52) {
    924 		id = sc->sc_srom[37] | (sc->sc_srom[36] << 8);
    925 		switch (id) {
    926  zx312:
    927 		case 0x0602:	/* ZX312 */
    928 			strcpy(sc->sc_name, "ZNYX ZX312");
    929 			return;
    930 
    931 		case 0x0622:	/* ZX312T */
    932 			strcpy(sc->sc_name, "ZNYX ZX312T");
    933 			sc->sc_mediasw = &tlp_21040_tp_mediasw;
    934 			return;
    935 
    936  zx314_inta:
    937 		case 0x0701:	/* ZX314 INTA */
    938 			psc->sc_flags |= TULIP_PCI_SHAREDINTR;
    939 			/* FALLTHROUGH */
    940 		case 0x0711:	/* ZX314 */
    941 			strcpy(sc->sc_name, "ZNYX ZX314");
    942 			psc->sc_flags |= TULIP_PCI_SHAREDROM;
    943 			sc->sc_mediasw = &tlp_21040_tp_mediasw;
    944 			return;
    945 
    946  zx315_inta:
    947 		case 0x0801:	/* ZX315 INTA */
    948 			psc->sc_flags |= TULIP_PCI_SHAREDINTR;
    949 			/* FALLTHROUGH */
    950 		case 0x0811:	/* ZX315 */
    951 			strcpy(sc->sc_name, "ZNYX ZX315");
    952 			psc->sc_flags |= TULIP_PCI_SHAREDROM;
    953 			return;
    954 
    955 		default:
    956 			id = 0;
    957 		}
    958 	}
    959 
    960 	/*
    961 	 * Deal with boards that have broken ROMs.
    962 	 */
    963 	if (id == 0) {
    964 		if ((enaddr[3] & ~3) == 0xf0 && (enaddr[5] & 3) == 0x00)
    965 			goto zx314_inta;
    966 		if ((enaddr[3] & ~3) == 0xf4 && (enaddr[5] & 1) == 0x00)
    967 			goto zx315_inta;
    968 		if ((enaddr[3] & ~3) == 0xec)
    969 			goto zx312;
    970 	}
    971 
    972 	strcpy(sc->sc_name, "ZNYX ZX31x");
    973 }
    974 
    975 void
    976 tlp_pci_smc_21040_quirks(psc, enaddr)
    977 	struct tulip_pci_softc *psc;
    978 	const u_int8_t *enaddr;
    979 {
    980 	struct tulip_softc *sc = &psc->sc_tulip;
    981 	u_int16_t id1, id2, ei;
    982 	int auibnc = 0, utp = 0;
    983 	char *cp;
    984 
    985 	id1 = sc->sc_srom[0x60] | (sc->sc_srom[0x61] << 8);
    986 	id2 = sc->sc_srom[0x62] | (sc->sc_srom[0x63] << 8);
    987 	ei  = sc->sc_srom[0x66] | (sc->sc_srom[0x67] << 8);
    988 
    989 	strcpy(sc->sc_name, "SMC 8432");
    990 	cp = &sc->sc_name[8];
    991 
    992 	if ((id1 & 1) == 0) {
    993 		*cp++ = 'B';
    994 		auibnc = 1;
    995 	}
    996 	if ((id1 & 0xff) > 0x32) {
    997 		*cp++ = 'T';
    998 		utp = 1;
    999 	}
   1000 	if ((id1 & 0x4000) == 0) {
   1001 		*cp++ = 'A';
   1002 		auibnc = 1;
   1003 	}
   1004 	if (id2 == 0x15) {
   1005 		sc->sc_name[7] = '4';
   1006 		*cp++ = '-';
   1007 		*cp++ = 'C';
   1008 		*cp++ = 'H';
   1009 		*cp++ = ei ? '2' : '1';
   1010 	}
   1011 	*cp = '\0';
   1012 
   1013 	if (utp != 0 && auibnc == 0)
   1014 		sc->sc_mediasw = &tlp_21040_tp_mediasw;
   1015 	else if (utp == 0 && auibnc != 0)
   1016 		sc->sc_mediasw = &tlp_21040_auibnc_mediasw;
   1017 }
   1018 
   1019 void
   1020 tlp_pci_cogent_21040_quirks(psc, enaddr)
   1021 	struct tulip_pci_softc *psc;
   1022 	const u_int8_t *enaddr;
   1023 {
   1024 
   1025 	strcpy(psc->sc_tulip.sc_name, "Cogent multi-port");
   1026 	psc->sc_flags |= TULIP_PCI_SHAREDINTR|TULIP_PCI_SHAREDROM;
   1027 }
   1028 
   1029 void
   1030 tlp_pci_accton_21040_quirks(psc, enaddr)
   1031 	struct tulip_pci_softc *psc;
   1032 	const u_int8_t *enaddr;
   1033 {
   1034 
   1035 	strcpy(psc->sc_tulip.sc_name, "ACCTON EN1203");
   1036 }
   1037 
   1038 void	tlp_pci_asante_21140_reset __P((struct tulip_softc *));
   1039 
   1040 void
   1041 tlp_pci_asante_21140_quirks(psc, enaddr)
   1042 	struct tulip_pci_softc *psc;
   1043 	const u_int8_t *enaddr;
   1044 {
   1045 	struct tulip_softc *sc = &psc->sc_tulip;
   1046 
   1047 	/*
   1048 	 * Some Asante boards don't use the ISV SROM format.  For
   1049 	 * those that don't, we initialize the GPIO direction bits,
   1050 	 * and provide our own reset hook, which resets the MII.
   1051 	 *
   1052 	 * All of these boards use SIO-attached-MII media.
   1053 	 */
   1054 	if (sc->sc_mediasw == &tlp_2114x_isv_mediasw)
   1055 		return;
   1056 
   1057 	strcpy(sc->sc_name, "Asante");
   1058 
   1059 	sc->sc_gp_dir = 0xbf;
   1060 	sc->sc_reset = tlp_pci_asante_21140_reset;
   1061 	sc->sc_mediasw = &tlp_sio_mii_mediasw;
   1062 }
   1063 
   1064 void
   1065 tlp_pci_asante_21140_reset(sc)
   1066 	struct tulip_softc *sc;
   1067 {
   1068 
   1069 	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | sc->sc_gp_dir);
   1070 	TULIP_WRITE(sc, CSR_GPP, 0x8);
   1071 	delay(100);
   1072 	TULIP_WRITE(sc, CSR_GPP, 0);
   1073 }
   1074