Home | History | Annotate | Line # | Download | only in pci
if_epic_pci.c revision 1.16
      1 /*	$NetBSD: if_epic_pci.c,v 1.16 2001/05/17 17:35:13 drochner 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 Standard Microsystems Corp. 83C170
     42  * Ethernet PCI Integrated Controller (EPIC/100) 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/smc83c170reg.h>
     84 #include <dev/ic/smc83c170var.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 EPIC.
     92  */
     93 #define	EPIC_PCI_IOBA		0x10	/* i/o mapped base */
     94 #define	EPIC_PCI_MMBA		0x14	/* memory mapped base */
     95 
     96 struct epic_pci_softc {
     97 	struct epic_softc sc_epic;	/* real EPIC softc */
     98 
     99 	/* PCI-specific goo. */
    100 	void	*sc_ih;			/* interrupt handle */
    101 };
    102 
    103 int	epic_pci_match(struct device *, struct cfdata *, void *);
    104 void	epic_pci_attach(struct device *, struct device *, void *);
    105 
    106 struct cfattach epic_pci_ca = {
    107 	sizeof(struct epic_pci_softc), epic_pci_match, epic_pci_attach,
    108 };
    109 
    110 const struct epic_pci_product {
    111 	u_int32_t	epp_prodid;	/* PCI product ID */
    112 	const char	*epp_name;	/* device name */
    113 } epic_pci_products[] = {
    114 	{ PCI_PRODUCT_SMC_83C170,	"SMC 83c170 Fast Ethernet" },
    115 	{ PCI_PRODUCT_SMC_83C175,	"SMC 83c175 Fast Ethernet" },
    116 	{ 0,				NULL },
    117 };
    118 
    119 const struct epic_pci_product *epic_pci_lookup(const struct pci_attach_args *);
    120 
    121 const struct epic_pci_product *
    122 epic_pci_lookup(pa)
    123 	const struct pci_attach_args *pa;
    124 {
    125 	const struct epic_pci_product *epp;
    126 
    127 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SMC)
    128 		return (NULL);
    129 
    130 	for (epp = epic_pci_products; epp->epp_name != NULL; epp++)
    131 		if (PCI_PRODUCT(pa->pa_id) == epp->epp_prodid)
    132 			return (epp);
    133 
    134 	return (NULL);
    135 }
    136 
    137 const struct epic_pci_subsys_info {
    138 	pcireg_t subsysid;
    139 	int flags;
    140 } epic_pci_subsys_info[] = {
    141 	{ PCI_ID_CODE(PCI_VENDOR_SMC, 0xa024), /* SMC9432BTX1 */
    142 	  EPIC_HAS_BNC },
    143 	{ PCI_ID_CODE(PCI_VENDOR_SMC, 0xa016), /* SMC9432FTX */
    144 	  EPIC_HAS_MII_FIBER | EPIC_DUPLEXLED_ON_694 },
    145 	{ 0xffffffff,
    146 	  0 }
    147 };
    148 
    149 const struct epic_pci_subsys_info *
    150   epic_pci_subsys_lookup(const struct pci_attach_args *);
    151 
    152 const struct epic_pci_subsys_info *
    153 epic_pci_subsys_lookup(pa)
    154 	const struct pci_attach_args *pa;
    155 {
    156 	pci_chipset_tag_t pc = pa->pa_pc;
    157 	pcireg_t reg;
    158 	const struct epic_pci_subsys_info *esp;
    159 
    160 	reg = pci_conf_read(pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    161 
    162 	for (esp = epic_pci_subsys_info; esp->subsysid != 0xffffffff; esp++)
    163 		if (esp->subsysid == reg)
    164 			return (esp);
    165 
    166 	return (NULL);
    167 }
    168 
    169 int
    170 epic_pci_match(parent, match, aux)
    171 	struct device *parent;
    172 	struct cfdata *match;
    173 	void *aux;
    174 {
    175 	struct pci_attach_args *pa = aux;
    176 
    177 	if (epic_pci_lookup(pa) != NULL)
    178 		return (1);
    179 
    180 	return (0);
    181 }
    182 
    183 void
    184 epic_pci_attach(parent, self, aux)
    185 	struct device *parent, *self;
    186 	void *aux;
    187 {
    188 	struct epic_pci_softc *psc = (struct epic_pci_softc *)self;
    189 	struct epic_softc *sc = &psc->sc_epic;
    190 	struct pci_attach_args *pa = aux;
    191 	pci_chipset_tag_t pc = pa->pa_pc;
    192 	pci_intr_handle_t ih;
    193 	const char *intrstr = NULL;
    194 	const struct epic_pci_product *epp;
    195 	const struct epic_pci_subsys_info *esp;
    196 	bus_space_tag_t iot, memt;
    197 	bus_space_handle_t ioh, memh;
    198 	pcireg_t reg;
    199 	int pmreg, ioh_valid, memh_valid;
    200 
    201 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    202 		reg = pci_conf_read(pc, pa->pa_tag, pmreg + 4);
    203 		switch (reg & PCI_PMCSR_STATE_MASK) {
    204 		case PCI_PMCSR_STATE_D1:
    205 		case PCI_PMCSR_STATE_D2:
    206 			printf(": waking up from power state D%d\n%s",
    207 			    reg & PCI_PMCSR_STATE_MASK, sc->sc_dev.dv_xname);
    208 			pci_conf_write(pc, pa->pa_tag, pmreg + 4,
    209 			    (reg & ~PCI_PMCSR_STATE_MASK) |
    210 			    PCI_PMCSR_STATE_D0);
    211 			break;
    212 		case PCI_PMCSR_STATE_D3:
    213 			/*
    214 			 * IO and MEM are disabled. We can't enable
    215 			 * the card because the BARs might be invalid.
    216 			 */
    217 			printf(": unable to wake up from power state D3, "
    218 			       "reboot required.\n");
    219 			pci_conf_write(pc, pa->pa_tag, pmreg + 4,
    220 			    (reg & ~PCI_PMCSR_STATE_MASK) |
    221 			    PCI_PMCSR_STATE_D0);
    222 			return;
    223 		}
    224 	}
    225 
    226 	/*
    227 	 * Map the device.
    228 	 */
    229 	ioh_valid = (pci_mapreg_map(pa, EPIC_PCI_IOBA,
    230 	    PCI_MAPREG_TYPE_IO, 0,
    231 	    &iot, &ioh, NULL, NULL) == 0);
    232 	memh_valid = (pci_mapreg_map(pa, EPIC_PCI_MMBA,
    233 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    234 	    &memt, &memh, NULL, NULL) == 0);
    235 
    236 	if (memh_valid) {
    237 		sc->sc_st = memt;
    238 		sc->sc_sh = memh;
    239 	} else if (ioh_valid) {
    240 		sc->sc_st = iot;
    241 		sc->sc_sh = ioh;
    242 	} else {
    243 		printf(": unable to map device registers\n");
    244 		return;
    245 	}
    246 
    247 	sc->sc_dmat = pa->pa_dmat;
    248 
    249 	epp = epic_pci_lookup(pa);
    250 	if (epp == NULL) {
    251 		printf("\n");
    252 		panic("epic_pci_attach: impossible");
    253 	}
    254 
    255 	printf(": %s, rev. %d\n", epp->epp_name, PCI_REVISION(pa->pa_class));
    256 
    257 	/* Make sure bus mastering is enabled. */
    258 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    259 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    260 	    PCI_COMMAND_MASTER_ENABLE);
    261 
    262 	/*
    263 	 * Map and establish our interrupt.
    264 	 */
    265 	if (pci_intr_map(pa, &ih)) {
    266 		printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
    267 		return;
    268 	}
    269 	intrstr = pci_intr_string(pc, ih);
    270 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, epic_intr, sc);
    271 	if (psc->sc_ih == NULL) {
    272 		printf("%s: unable to establish interrupt",
    273 		    sc->sc_dev.dv_xname);
    274 		if (intrstr != NULL)
    275 			printf(" at %s", intrstr);
    276 		printf("\n");
    277 		return;
    278 	}
    279 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    280 
    281 	esp = epic_pci_subsys_lookup(pa);
    282 	if (esp)
    283 		sc->sc_hwflags = esp->flags;
    284 
    285 	/*
    286 	 * Finish off the attach.
    287 	 */
    288 	epic_attach(sc);
    289 }
    290