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