Home | History | Annotate | Line # | Download | only in pci
if_ep_pci.c revision 1.43.22.2
      1 /*	$NetBSD: if_ep_pci.c,v 1.43.22.2 2006/12/10 07:17:43 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 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  * Copyright (c) 1997 Jonathan Stone <jonathan (at) NetBSD.org>
     42  * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) beer.org>
     43  * All rights reserved.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. All advertising materials mentioning features or use of this software
     54  *    must display the following acknowledgement:
     55  *      This product includes software developed by Herb Peyerl.
     56  * 4. The name of Herb Peyerl may not be used to endorse or promote products
     57  *    derived from this software without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     60  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     61  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     62  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     63  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     64  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     65  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     66  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     67  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     68  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 
     71 #include <sys/cdefs.h>
     72 __KERNEL_RCSID(0, "$NetBSD: if_ep_pci.c,v 1.43.22.2 2006/12/10 07:17:43 yamt Exp $");
     73 
     74 #include <sys/param.h>
     75 #include <sys/systm.h>
     76 #include <sys/mbuf.h>
     77 #include <sys/socket.h>
     78 #include <sys/ioctl.h>
     79 #include <sys/errno.h>
     80 #include <sys/syslog.h>
     81 #include <sys/select.h>
     82 #include <sys/device.h>
     83 
     84 #include <net/if.h>
     85 #include <net/if_dl.h>
     86 #include <net/if_ether.h>
     87 #include <net/if_media.h>
     88 
     89 #include <machine/cpu.h>
     90 #include <machine/bus.h>
     91 #include <machine/intr.h>
     92 
     93 #include <dev/mii/miivar.h>
     94 
     95 #include <dev/ic/elink3var.h>
     96 #include <dev/ic/elink3reg.h>
     97 
     98 #include <dev/pci/pcivar.h>
     99 #include <dev/pci/pcireg.h>
    100 #include <dev/pci/pcidevs.h>
    101 
    102 /*
    103  * PCI constants.
    104  */
    105 #define PCI_CBIO		0x10    /* Configuration Base IO Address */
    106 
    107 static int	ep_pci_match(struct device *, struct cfdata *, void *);
    108 static void	ep_pci_attach(struct device *, struct device *, void *);
    109 
    110 CFATTACH_DECL(ep_pci, sizeof(struct ep_softc),
    111     ep_pci_match, ep_pci_attach, NULL, NULL);
    112 
    113 static struct ep_pci_product {
    114 	u_int32_t	epp_prodid;	/* PCI product ID */
    115 	u_short		epp_chipset;	/* 3Com chipset used */
    116 	int		epp_flags;	/* initial softc flags */
    117 	const char	*epp_name;	/* device name */
    118 } ep_pci_products[] = {
    119 	{ PCI_PRODUCT_3COM_3C590,	ELINK_CHIPSET_VORTEX,
    120 	  0,
    121 	  "3c590 Ethernet" },
    122 
    123 	/*
    124 	 * Note: The 3c595-MII is an MII connector for an
    125 	 * external PHY.  We treat it as `manual' in the
    126 	 * core driver.
    127 	 */
    128 	{ PCI_PRODUCT_3COM_3C595TX,	ELINK_CHIPSET_VORTEX,
    129 	  0,
    130 	  "3c595-TX 10/100 Ethernet" },
    131 	{ PCI_PRODUCT_3COM_3C595T4,	ELINK_CHIPSET_VORTEX,
    132 	  0,
    133 	  "3c595-T4 10/100 Ethernet" },
    134 	{ PCI_PRODUCT_3COM_3C595MII,	ELINK_CHIPSET_VORTEX,
    135 	  0,
    136 	  "3c595-MII 10/100 Ethernet" },
    137 
    138 	{ PCI_PRODUCT_3COM_3C900TPO,	ELINK_CHIPSET_BOOMERANG,
    139 	  0,
    140 	  "3c900-TPO Ethernet" },
    141 	{ PCI_PRODUCT_3COM_3C900COMBO,	ELINK_CHIPSET_BOOMERANG,
    142 	  0,
    143 	  "3c900-COMBO Ethernet" },
    144 
    145 	{ PCI_PRODUCT_3COM_3C905TX,	ELINK_CHIPSET_BOOMERANG,
    146 	  ELINK_FLAGS_MII,
    147 	  "3c905-TX 10/100 Ethernet" },
    148 	{ PCI_PRODUCT_3COM_3C905T4,	ELINK_CHIPSET_BOOMERANG,
    149 	  ELINK_FLAGS_MII,
    150 	  "3c905-T4 10/100 Ethernet" },
    151 
    152 	{ 0,				0,
    153 	  0,				NULL },
    154 };
    155 
    156 static const struct ep_pci_product *
    157 ep_pci_lookup(const struct pci_attach_args *pa)
    158 {
    159 	struct ep_pci_product *epp;
    160 
    161 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3COM)
    162 		return (NULL);
    163 
    164 	for (epp = ep_pci_products; epp->epp_name != NULL; epp++)
    165 		if (PCI_PRODUCT(pa->pa_id) == epp->epp_prodid)
    166 			return (epp);
    167 
    168 	return (NULL);
    169 }
    170 
    171 static int
    172 ep_pci_match(struct device *parent, struct cfdata *match,
    173     void *aux)
    174 {
    175 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    176 
    177 	if (ep_pci_lookup(pa) != NULL)
    178 		return (1);
    179 
    180 	return (0);
    181 }
    182 
    183 static void
    184 ep_pci_attach(struct device *parent, struct device *self, void *aux)
    185 {
    186 	struct ep_softc *sc = (void *)self;
    187 	struct pci_attach_args *pa = aux;
    188 	pci_chipset_tag_t pc = pa->pa_pc;
    189 	pci_intr_handle_t ih;
    190 	const struct ep_pci_product *epp;
    191 	const char *intrstr = NULL;
    192 
    193 	aprint_naive(": Ethernet controller\n");
    194 
    195 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    196 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    197 		aprint_error(": can't map i/o space\n");
    198 		return;
    199 	}
    200 
    201 	epp = ep_pci_lookup(pa);
    202 	if (epp == NULL) {
    203 		printf("\n");
    204 		panic("ep_pci_attach: impossible");
    205 	}
    206 
    207 	aprint_normal(": 3Com %s\n", epp->epp_name);
    208 
    209 	sc->enable = NULL;
    210 	sc->disable = NULL;
    211 	sc->enabled = 1;
    212 
    213 	sc->bustype = ELINK_BUS_PCI;
    214 	sc->ep_flags = epp->epp_flags;
    215 
    216 	/* Enable the card. */
    217 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    218 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    219 	    PCI_COMMAND_MASTER_ENABLE);
    220 
    221 	/* Map and establish the interrupt. */
    222 	if (pci_intr_map(pa, &ih)) {
    223 		aprint_error("%s: couldn't map interrupt\n",
    224 		    sc->sc_dev.dv_xname);
    225 		return;
    226 	}
    227 	intrstr = pci_intr_string(pc, ih);
    228 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, epintr, sc);
    229 	if (sc->sc_ih == NULL) {
    230 		aprint_error("%s: couldn't establish interrupt",
    231 		    sc->sc_dev.dv_xname);
    232 		if (intrstr != NULL)
    233 			aprint_normal(" at %s", intrstr);
    234 		aprint_normal("\n");
    235 		return;
    236 	}
    237 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    238 
    239 	epconfig(sc, epp->epp_chipset, NULL);
    240 }
    241