Home | History | Annotate | Line # | Download | only in pci
if_fxp_pci.c revision 1.18
      1 /*	$NetBSD: if_fxp_pci.c,v 1.18 2001/07/08 18:02:28 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999, 2000, 2001 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 Intel i82557 fast Ethernet controller
     42  * driver.  Works with Intel Etherexpress Pro 10+, 100B, 100+ cards.
     43  */
     44 
     45 #include "rnd.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/malloc.h>
     51 #include <sys/kernel.h>
     52 #include <sys/socket.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/errno.h>
     55 #include <sys/device.h>
     56 
     57 #if NRND > 0
     58 #include <sys/rnd.h>
     59 #endif
     60 
     61 #include <machine/endian.h>
     62 
     63 #include <net/if.h>
     64 #include <net/if_dl.h>
     65 #include <net/if_media.h>
     66 #include <net/if_ether.h>
     67 
     68 #include <machine/bus.h>
     69 #include <machine/intr.h>
     70 
     71 #include <dev/mii/miivar.h>
     72 
     73 #include <dev/ic/i82557reg.h>
     74 #include <dev/ic/i82557var.h>
     75 
     76 #include <dev/pci/pcivar.h>
     77 #include <dev/pci/pcireg.h>
     78 #include <dev/pci/pcidevs.h>
     79 
     80 struct fxp_pci_softc {
     81 	struct fxp_softc psc_fxp;
     82 
     83 	pci_chipset_tag_t psc_pc;	/* pci chipset tag */
     84 	pcireg_t psc_regs[0x20>>2];	/* saved PCI config regs (sparse) */
     85 	pcitag_t psc_tag;		/* pci register tag */
     86 	void *psc_powerhook;		/* power hook */
     87 };
     88 
     89 int	fxp_pci_match __P((struct device *, struct cfdata *, void *));
     90 void	fxp_pci_attach __P((struct device *, struct device *, void *));
     91 
     92 static void	fxp_pci_confreg_restore __P((struct fxp_pci_softc *psc));
     93 static void	fxp_pci_power __P((int why, void *arg));
     94 
     95 struct cfattach fxp_pci_ca = {
     96 	sizeof(struct fxp_pci_softc), fxp_pci_match, fxp_pci_attach
     97 };
     98 
     99 const struct fxp_pci_product {
    100 	u_int32_t	fpp_prodid;	/* PCI product ID */
    101 	const char	*fpp_name;	/* device name */
    102 } fxp_pci_products[] = {
    103 	{ PCI_PRODUCT_INTEL_82557,
    104 	  "Intel i82557 Ethernet" },
    105 	{ PCI_PRODUCT_INTEL_82559ER,
    106 	  "Intel i82559ER Ethernet" },
    107 	{ PCI_PRODUCT_INTEL_IN_BUSINESS,
    108 	  "Intel InBusiness Ethernet" },
    109 	{ PCI_PRODUCT_INTEL_82801BA_LAN,
    110 	  "Intel i82562 Ethernet" },
    111 	{ 0,
    112 	  NULL },
    113 };
    114 
    115 static const struct fxp_pci_product *
    116 fxp_pci_lookup(const struct pci_attach_args *pa)
    117 {
    118 	const struct fxp_pci_product *fpp;
    119 
    120 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
    121 		return (NULL);
    122 
    123 	for (fpp = fxp_pci_products; fpp->fpp_name != NULL; fpp++)
    124 		if (PCI_PRODUCT(pa->pa_id) == fpp->fpp_prodid)
    125 			return (fpp);
    126 
    127 	return (NULL);
    128 }
    129 
    130 int
    131 fxp_pci_match(parent, match, aux)
    132 	struct device *parent;
    133 	struct cfdata *match;
    134 	void *aux;
    135 {
    136 	struct pci_attach_args *pa = aux;
    137 
    138 	if (fxp_pci_lookup(pa) != NULL)
    139 		return (1);
    140 
    141 	return (0);
    142 }
    143 
    144 /*
    145  * Restore PCI configuration registers that may have been clobbered.
    146  * This is necessary due to bugs on the Sony VAIO Z505-series on-board
    147  * ethernet, after an APM suspend/resume, as well as after an ACPI
    148  * D3->D0 transition.  We call this function from a power hook after
    149  * APM resume events, as well as after the ACPI D3->D0 transition.
    150  */
    151 static void
    152 fxp_pci_confreg_restore(psc)
    153         struct fxp_pci_softc *psc;
    154 {
    155 	pcireg_t reg;
    156 
    157 #if 0
    158 	/*
    159 	 * Check to see if the command register is blank -- if so, then
    160 	 * we'll assume that all the clobberable-registers have been
    161 	 * clobbered.
    162 	 */
    163 
    164 	/*
    165 	 * In general, the above metric is accurate. Unfortunately,
    166 	 * it is inaccurate across a hibernation. Ideally APM/ACPI
    167 	 * code should take note of hibernation events and execute
    168 	 * a hibernation wakeup hook, but at present a hibernation wake
    169 	 * is indistinguishable from a suspend wake.
    170 	 */
    171 
    172 	if (((reg = pci_conf_read(psc->psc_pc, psc->psc_tag,
    173 	    PCI_COMMAND_STATUS_REG)) & 0xffff) != 0)
    174 		return;
    175 #else
    176 	reg = pci_conf_read(psc->psc_pc, psc->psc_tag, PCI_COMMAND_STATUS_REG);
    177 #endif
    178 
    179 	pci_conf_write(psc->psc_pc, psc->psc_tag,
    180 	    PCI_COMMAND_STATUS_REG,
    181 	    (reg & 0xffff0000) |
    182 	    (psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] & 0xffff));
    183 	pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_BHLC_REG,
    184 	    psc->psc_regs[PCI_BHLC_REG>>2]);
    185 	pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x0,
    186 	    psc->psc_regs[(PCI_MAPREG_START+0x0)>>2]);
    187 	pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x4,
    188 	    psc->psc_regs[(PCI_MAPREG_START+0x4)>>2]);
    189 	pci_conf_write(psc->psc_pc, psc->psc_tag, PCI_MAPREG_START+0x8,
    190 	    psc->psc_regs[(PCI_MAPREG_START+0x8)>>2]);
    191 }
    192 
    193 
    194 /*
    195  * Power handler routine. Called when the system is transitioning into/out
    196  * of power save modes. We restore the (bashed) PCI configuration registers
    197  * on a resume.
    198  */
    199 static void
    200 fxp_pci_power(why, arg)
    201 	int why;
    202 	void *arg;
    203 {
    204 	struct fxp_pci_softc *psc = arg;
    205 
    206 	if (why == PWR_RESUME)
    207 		fxp_pci_confreg_restore(psc);
    208 }
    209 
    210 void
    211 fxp_pci_attach(parent, self, aux)
    212 	struct device *parent, *self;
    213 	void *aux;
    214 {
    215 	struct fxp_pci_softc *psc = (struct fxp_pci_softc *)self;
    216 	struct fxp_softc *sc = (struct fxp_softc *)self;
    217 	struct pci_attach_args *pa = aux;
    218 	pci_chipset_tag_t pc = pa->pa_pc;
    219 	pci_intr_handle_t ih;
    220 	const struct fxp_pci_product *fpp;
    221 	const char *intrstr = NULL;
    222 	bus_space_tag_t iot, memt;
    223 	bus_space_handle_t ioh, memh;
    224 	int ioh_valid, memh_valid;
    225 	bus_addr_t addr;
    226 	bus_size_t size;
    227 	int flags;
    228  	int pci_pwrmgmt_cap_reg, pci_pwrmgmt_csr_reg;
    229 
    230 	sc->sc_enabled = 1;
    231 	sc->sc_enable = NULL;
    232 	sc->sc_disable = NULL;
    233 
    234 	/*
    235 	 * Map control/status registers.
    236 	 */
    237 	ioh_valid = (pci_mapreg_map(pa, FXP_PCI_IOBA,
    238 	    PCI_MAPREG_TYPE_IO, 0,
    239 	    &iot, &ioh, NULL, NULL) == 0);
    240 
    241 	/*
    242 	 * Version 2.1 of the PCI spec, page 196, "Address Maps":
    243 	 *
    244 	 *	Prefetchable
    245 	 *
    246 	 *	Set to one if there are no side effects on reads, the
    247 	 *	device returns all bytes regardless of the byte enables,
    248 	 *	and host bridges can merge processor writes into this
    249 	 *	range without causing errors.  Bit must be set to zero
    250 	 *	otherwise.
    251 	 *
    252 	 * The 82557 incorrectly sets the "prefetchable" bit, resulting
    253 	 * in errors on systems which will do merged reads and writes.
    254 	 * These errors manifest themselves as all-bits-set when reading
    255 	 * from the EEPROM or other < 4 byte registers.
    256 	 *
    257 	 * We must work around this problem by always forcing the mapping
    258 	 * for memory space to be uncacheable.  On systems which cannot
    259 	 * create an uncacheable mapping (because the firmware mapped it
    260 	 * into only cacheable/prefetchable space due to the "prefetchable"
    261 	 * bit), we can fall back onto i/o mapped access.
    262 	 */
    263 	memh_valid = 0;
    264 	memt = pa->pa_memt;
    265 	if (((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) != 0) &&
    266 	    pci_mapreg_info(pa->pa_pc, pa->pa_tag, FXP_PCI_MMBA,
    267 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
    268 	    &addr, &size, &flags) == 0) {
    269 		flags &= ~BUS_SPACE_MAP_PREFETCHABLE;
    270 		if (bus_space_map(memt, addr, size, flags, &memh) == 0)
    271 			memh_valid = 1;
    272 	}
    273 
    274 	if (memh_valid) {
    275 		sc->sc_st = memt;
    276 		sc->sc_sh = memh;
    277 	} else if (ioh_valid) {
    278 		sc->sc_st = iot;
    279 		sc->sc_sh = ioh;
    280 	} else {
    281 		printf(": unable to map device registers\n");
    282 		return;
    283 	}
    284 
    285 	sc->sc_dmat = pa->pa_dmat;
    286 
    287 	fpp = fxp_pci_lookup(pa);
    288 	if (fpp == NULL) {
    289 		printf("\n");
    290 		panic("fxp_pci_attach: impossible");
    291 	}
    292 
    293 	sc->sc_rev = PCI_REVISION(pa->pa_class);
    294 
    295 	switch (fpp->fpp_prodid) {
    296 	case PCI_PRODUCT_INTEL_82557:
    297 	case PCI_PRODUCT_INTEL_82559ER:
    298 	case PCI_PRODUCT_INTEL_IN_BUSINESS:
    299 	    {
    300 		const char *chipname = NULL;
    301 
    302 		if (sc->sc_rev >= FXP_REV_82558_A4) {
    303 			chipname = "i82558 Ethernet";
    304 			/*
    305 			 * Enable the MWI command for memory writes.
    306 			 */
    307 			if (pa->pa_flags & PCI_FLAGS_MWI_OKAY)
    308 				sc->sc_flags |= FXPF_MWI;
    309 		}
    310 		if (sc->sc_rev >= FXP_REV_82559_A0)
    311 			chipname = "i82559 Ethernet";
    312 		if (sc->sc_rev >= FXP_REV_82559S_A)
    313 			chipname = "i82559S Ethernet";
    314 		if (sc->sc_rev >= FXP_REV_82550)
    315 			chipname = "i82550 Ethernet";
    316 
    317 		printf(": %s, rev %d\n", chipname != NULL ? chipname :
    318 		    fpp->fpp_name, sc->sc_rev);
    319 		break;
    320 	    }
    321 
    322 	case PCI_PRODUCT_INTEL_82801BA_LAN:
    323 		printf(": %s, rev %d\n", fpp->fpp_name, sc->sc_rev);
    324 
    325 		/*
    326 		 * The 82801BA Ethernet has a bug which requires us to send a
    327 		 * NOP before a CU_RESUME if we're in 10baseT mode.
    328 		 */
    329 		if (fpp->fpp_prodid == PCI_PRODUCT_INTEL_82801BA_LAN)
    330 			sc->sc_flags |= FXPF_HAS_RESUME_BUG;
    331 		break;
    332 
    333 	case PCI_PRODUCT_INTEL_PRO_100_VE_0:
    334 	case PCI_PRODUCT_INTEL_PRO_100_VE_1:
    335 	case PCI_PRODUCT_INTEL_PRO_100_VM_0:
    336 	case PCI_PRODUCT_INTEL_PRO_100_VM_1:
    337 	case PCI_PRODUCT_INTEL_82562EH_HPNA_0:
    338 	case PCI_PRODUCT_INTEL_82562EH_HPNA_1:
    339 	case PCI_PRODUCT_INTEL_82562EH_HPNA_2:
    340 	case PCI_PRODUCT_INTEL_PRO_100_VM_2:
    341 		printf(": %s, rev %d\n", fpp->fpp_name, sc->sc_rev);
    342 
    343 		/*
    344 		 * ICH3 chips apparently have problems with the enhanced
    345 		 * features, so just treat them as an i82557.  It also
    346 		 * has the resume bug that the ICH2 has.
    347 		 */
    348 		sc->sc_rev = 1;
    349 		sc->sc_flags |= FXPF_HAS_RESUME_BUG;
    350 		break;
    351 	}
    352 
    353 	/* Make sure bus-mastering is enabled. */
    354 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    355 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    356 	    PCI_COMMAND_MASTER_ENABLE);
    357 
    358   	/*
    359 	 * Under some circumstances (such as APM suspend/resume
    360 	 * cycles, and across ACPI power state changes), the
    361 	 * i82257-family can lose the contents of critical PCI
    362 	 * configuration registers, causing the card to be
    363 	 * non-responsive and useless.  This occurs on the Sony VAIO
    364 	 * Z505-series, among others.  Preserve them here so they can
    365 	 * be later restored (by fxp_pci_confreg_restore()).
    366 	 */
    367 	psc->psc_pc = pc;
    368 	psc->psc_tag = pa->pa_tag;
    369 	psc->psc_regs[PCI_COMMAND_STATUS_REG>>2] =
    370 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    371 	psc->psc_regs[PCI_BHLC_REG>>2] =
    372 	    pci_conf_read(pc, pa->pa_tag, PCI_BHLC_REG);
    373 	psc->psc_regs[(PCI_MAPREG_START+0x0)>>2] =
    374 	    pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x0);
    375 	psc->psc_regs[(PCI_MAPREG_START+0x4)>>2] =
    376 	    pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x4);
    377 	psc->psc_regs[(PCI_MAPREG_START+0x8)>>2] =
    378 	    pci_conf_read(pc, pa->pa_tag, PCI_MAPREG_START+0x8);
    379 
    380 	/*
    381 	 * Work around BIOS ACPI bugs where the chip is inadvertantly
    382 	 * left in ACPI D3 (lowest power state).  First confirm the device
    383 	 * supports ACPI power management, then move it to the D0 (fully
    384 	 * functional) state if it is not already there.
    385 	 */
    386 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT,
    387 	    &pci_pwrmgmt_cap_reg, 0)) {
    388 		pcireg_t reg;
    389 
    390 		pci_pwrmgmt_csr_reg = pci_pwrmgmt_cap_reg + 4;
    391 		reg = pci_conf_read(pc, pa->pa_tag, pci_pwrmgmt_csr_reg);
    392 		if ((reg & PCI_PMCSR_STATE_MASK) != PCI_PMCSR_STATE_D0) {
    393 		    pci_conf_write(pc, pa->pa_tag, pci_pwrmgmt_csr_reg,
    394 			(reg & ~PCI_PMCSR_STATE_MASK) |
    395 			PCI_PMCSR_STATE_D0);
    396 		}
    397 	}
    398 	/* Restore PCI configuration registers. */
    399 	fxp_pci_confreg_restore(psc);
    400 
    401 	/*
    402 	 * Map and establish our interrupt.
    403 	 */
    404 	if (pci_intr_map(pa, &ih)) {
    405 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    406 		return;
    407 	}
    408 	intrstr = pci_intr_string(pc, ih);
    409 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, fxp_intr, sc);
    410 	if (sc->sc_ih == NULL) {
    411 		printf("%s: couldn't establish interrupt",
    412 		    sc->sc_dev.dv_xname);
    413 		if (intrstr != NULL)
    414 			printf(" at %s", intrstr);
    415 		printf("\n");
    416 		return;
    417 	}
    418 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    419 
    420 	/* Finish off the attach. */
    421 	fxp_attach(sc);
    422 
    423 	/* Add a suspend hook to restore PCI config state */
    424 	psc->psc_powerhook = powerhook_establish(fxp_pci_power, psc);
    425 	if (psc->psc_powerhook == NULL)
    426 		printf ("%s: WARNING: unable to establish pci power hook\n",
    427 		    sc->sc_dev.dv_xname);
    428 
    429 }
    430