Home | History | Annotate | Line # | Download | only in pci
if_ne_pci.c revision 1.30
      1 /*	$NetBSD: if_ne_pci.c,v 1.30 2007/10/19 12:00:47 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 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 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: if_ne_pci.c,v 1.30 2007/10/19 12:00:47 ad Exp $");
     42 
     43 #include "opt_ipkdb.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/mbuf.h>
     48 #include <sys/syslog.h>
     49 #include <sys/socket.h>
     50 #include <sys/device.h>
     51 
     52 #include <net/if.h>
     53 #include <net/if_ether.h>
     54 #include <net/if_media.h>
     55 
     56 #include <sys/bus.h>
     57 #include <sys/intr.h>
     58 
     59 #ifdef IPKDB_NE_PCI
     60 #include <ipkdb/ipkdb.h>
     61 #endif
     62 
     63 #include <dev/pci/pcireg.h>
     64 #include <dev/pci/pcivar.h>
     65 #include <dev/pci/pcidevs.h>
     66 
     67 #include <dev/ic/dp8390reg.h>
     68 #include <dev/ic/dp8390var.h>
     69 
     70 #include <dev/ic/ne2000reg.h>
     71 #include <dev/ic/ne2000var.h>
     72 
     73 #include <dev/ic/rtl80x9reg.h>
     74 #include <dev/ic/rtl80x9var.h>
     75 
     76 struct ne_pci_softc {
     77 	struct ne2000_softc sc_ne2000;		/* real "ne2000" softc */
     78 
     79 	/* PCI-specific goo */
     80 	void *sc_ih;				/* interrupt handle */
     81 };
     82 
     83 static int	ne_pci_match(struct device *, struct cfdata *, void *);
     84 static void	ne_pci_attach(struct device *, struct device *, void *);
     85 
     86 CFATTACH_DECL(ne_pci, sizeof(struct ne_pci_softc),
     87     ne_pci_match, ne_pci_attach, NULL, NULL);
     88 
     89 #ifdef IPKDB_NE_PCI
     90 static struct ne_pci_softc ipkdb_softc;
     91 static pci_chipset_tag_t ipkdb_pc;
     92 static pcitag_t ipkdb_tag;
     93 static struct ipkdb_if *ne_kip;
     94 
     95 int ne_pci_ipkdb_attach(struct ipkdb_if *, bus_space_tag_t,       /* XXX */
     96 			pci_chipset_tag_t, int, int);
     97 
     98 static int ne_pci_isipkdb(pci_chipset_tag_t, pcitag_t);
     99 #endif
    100 
    101 static const struct ne_pci_product {
    102 	pci_vendor_id_t npp_vendor;
    103 	pci_product_id_t npp_product;
    104 	int (*npp_mediachange)(struct dp8390_softc *);
    105 	void (*npp_mediastatus)(struct dp8390_softc *, struct ifmediareq *);
    106 	void (*npp_init_card)(struct dp8390_softc *);
    107 	void (*npp_media_init)(struct dp8390_softc *);
    108 	const char *npp_name;
    109 } ne_pci_products[] = {
    110 	{ PCI_VENDOR_REALTEK,		PCI_PRODUCT_REALTEK_RT8029,
    111 	  rtl80x9_mediachange,		rtl80x9_mediastatus,
    112 	  rtl80x9_init_card,		rtl80x9_media_init,
    113 	  "Realtek 8029" },
    114 
    115 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C940F,
    116 	  NULL,				NULL,
    117 	  NULL,				NULL,
    118 	  "Winbond 89C940F" },
    119 
    120 	{ PCI_VENDOR_WINBOND,		PCI_PRODUCT_WINBOND_W89C940F_1,
    121 	  NULL,				NULL,
    122 	  NULL,				NULL,
    123 	  "Winbond 89C940F" },
    124 
    125 	{ PCI_VENDOR_VIATECH,		PCI_PRODUCT_VIATECH_VT86C926,
    126 	  NULL,				NULL,
    127 	  NULL,				NULL,
    128 	  "VIA Technologies VT86C926" },
    129 
    130 	{ PCI_VENDOR_SURECOM,		PCI_PRODUCT_SURECOM_NE34,
    131 	  NULL,				NULL,
    132 	  NULL,				NULL,
    133 	  "Surecom NE-34" },
    134 
    135 	{ PCI_VENDOR_NETVIN,		PCI_PRODUCT_NETVIN_5000,
    136 	  NULL,				NULL,
    137 	  NULL,				NULL,
    138 	  "NetVin 5000" },
    139 
    140 	/* XXX The following entries need sanity checking in pcidevs */
    141 	{ PCI_VENDOR_COMPEX,		PCI_PRODUCT_COMPEX_NE2KETHER,
    142 	  NULL,				NULL,
    143 	  NULL,				NULL,
    144 	  "Compex" },
    145 
    146 	{ PCI_VENDOR_PROLAN,		PCI_PRODUCT_PROLAN_NE2KETHER,
    147 	  NULL,				NULL,
    148 	  NULL,				NULL,
    149 	  "ProLAN" },
    150 
    151 	{ PCI_VENDOR_KTI,		PCI_PRODUCT_KTI_NE2KETHER,
    152 	  NULL,				NULL,
    153 	  NULL,				NULL,
    154 	  "KTI" },
    155 
    156 	{ 0,				0,
    157 	  NULL,				NULL,
    158 	  NULL,				NULL,
    159 	  NULL },
    160 };
    161 
    162 static const struct ne_pci_product *
    163 ne_pci_lookup(const struct pci_attach_args *pa)
    164 {
    165 	const struct ne_pci_product *npp;
    166 
    167 	for (npp = ne_pci_products; npp->npp_name != NULL; npp++) {
    168 		if (PCI_VENDOR(pa->pa_id) == npp->npp_vendor &&
    169 		    PCI_PRODUCT(pa->pa_id) == npp->npp_product)
    170 			return (npp);
    171 	}
    172 	return (NULL);
    173 }
    174 
    175 /*
    176  * PCI constants.
    177  * XXX These should be in a common file!
    178  */
    179 #define PCI_CBIO	0x10		/* Configuration Base IO Address */
    180 
    181 static int
    182 ne_pci_match(struct device *parent, struct cfdata *match,
    183     void *aux)
    184 {
    185 	struct pci_attach_args *pa = aux;
    186 
    187 	if (ne_pci_lookup(pa) != NULL)
    188 		return (1);
    189 
    190 	return (0);
    191 }
    192 
    193 static void
    194 ne_pci_attach(struct device *parent, struct device *self, void *aux)
    195 {
    196 	struct ne_pci_softc *psc = (struct ne_pci_softc *)self;
    197 	struct ne2000_softc *nsc = &psc->sc_ne2000;
    198 	struct dp8390_softc *dsc = &nsc->sc_dp8390;
    199 	struct pci_attach_args *pa = aux;
    200 	pci_chipset_tag_t pc = pa->pa_pc;
    201 	bus_space_tag_t nict;
    202 	bus_space_handle_t nich;
    203 	bus_space_tag_t asict;
    204 	bus_space_handle_t asich;
    205 	const char *intrstr;
    206 	const struct ne_pci_product *npp;
    207 	pci_intr_handle_t ih;
    208 	pcireg_t csr;
    209 
    210 	npp = ne_pci_lookup(pa);
    211 	if (npp == NULL) {
    212 		printf("\n");
    213 		panic("ne_pci_attach: impossible");
    214 	}
    215 
    216 	printf(": %s Ethernet\n", npp->npp_name);
    217 
    218 #ifdef IPKDB_NE_PCI
    219 	if (ne_pci_isipkdb(pc, pa->pa_tag)) {
    220 		nict = ipkdb_softc.sc_ne2000.sc_dp8390.sc_regt;
    221 		nich = ipkdb_softc.sc_ne2000.sc_dp8390.sc_regh;
    222 		ne_kip->port = nsc;
    223 	} else
    224 #endif
    225 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    226 	    &nict, &nich, NULL, NULL)) {
    227 		printf("%s: can't map i/o space\n", dsc->sc_dev.dv_xname);
    228 		return;
    229 	}
    230 
    231 	asict = nict;
    232 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
    233 	    NE2000_ASIC_NPORTS, &asich)) {
    234 		printf("%s: can't subregion i/o space\n", dsc->sc_dev.dv_xname);
    235 		return;
    236 	}
    237 
    238 	dsc->sc_regt = nict;
    239 	dsc->sc_regh = nich;
    240 
    241 	nsc->sc_asict = asict;
    242 	nsc->sc_asich = asich;
    243 
    244 	/* Enable the card. */
    245 	csr = pci_conf_read(pc, pa->pa_tag,
    246 	    PCI_COMMAND_STATUS_REG);
    247 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    248 	    csr | PCI_COMMAND_MASTER_ENABLE);
    249 
    250 	/* This interface is always enabled. */
    251 	dsc->sc_enabled = 1;
    252 
    253 	dsc->sc_mediachange = npp->npp_mediachange;
    254 	dsc->sc_mediastatus = npp->npp_mediastatus;
    255 	dsc->sc_media_init = npp->npp_media_init;
    256 	dsc->init_card = npp->npp_init_card;
    257 
    258 	/*
    259 	 * Do generic NE2000 attach.  This will read the station address
    260 	 * from the EEPROM.
    261 	 */
    262 	ne2000_attach(nsc, NULL);
    263 
    264 	/* Map and establish the interrupt. */
    265 	if (pci_intr_map(pa, &ih)) {
    266 		printf("%s: couldn't map interrupt\n", dsc->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, dp8390_intr, dsc);
    271 	if (psc->sc_ih == NULL) {
    272 		printf("%s: couldn't establish interrupt",
    273 		    dsc->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", dsc->sc_dev.dv_xname, intrstr);
    280 }
    281 
    282 #ifdef IPKDB_NE_PCI
    283 static int
    284 ne_pci_isipkdb(pci_chipset_tag_t pc, pcitag_t tag)
    285 {
    286 	return !memcmp(&pc, &ipkdb_pc, sizeof pc)
    287 		&& !memcmp(&tag, &ipkdb_tag, sizeof tag);
    288 }
    289 
    290 int
    291 ne_pci_ipkdb_attach(struct ipkdb_if *kip, bus_space_tag_t iot,
    292     pci_chipset_tag_t pc, int bus, int dev)
    293 {
    294 	struct pci_attach_args pa;
    295 	bus_space_tag_t nict, asict;
    296 	bus_space_handle_t nich, asich;
    297 	u_int32_t csr;
    298 
    299 	pa.pa_iot = iot;
    300 	pa.pa_pc = pc;
    301 	pa.pa_device = dev;
    302 	pa.pa_function = 0;
    303 	pa.pa_flags = PCI_FLAGS_IO_ENABLED;
    304 	pa.pa_tag = pci_make_tag(pc, bus, dev, /*func*/0);
    305 	pa.pa_id = pci_conf_read(pc, pa.pa_tag, PCI_ID_REG);
    306 	pa.pa_class = pci_conf_read(pc, pa.pa_tag, PCI_CLASS_REG);
    307 	if (ne_pci_lookup(&pa) == NULL)
    308 		return -1;
    309 
    310 	if (pci_mapreg_map(&pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    311 			&nict, &nich, NULL, NULL))
    312 		return -1;
    313 
    314 	asict = nict;
    315 	if (bus_space_subregion(nict, nich, NE2000_ASIC_OFFSET,
    316 				NE2000_ASIC_NPORTS, &asich)) {
    317 		bus_space_unmap(nict, nich, NE2000_NPORTS);
    318 		return -1;
    319 	}
    320 
    321 	/* Enable card */
    322 	csr = pci_conf_read(pc, pa.pa_tag, PCI_COMMAND_STATUS_REG);
    323 	pci_conf_write(pc, pa.pa_tag, PCI_COMMAND_STATUS_REG,
    324 			csr | PCI_COMMAND_MASTER_ENABLE);
    325 
    326 	ipkdb_softc.sc_ne2000.sc_dp8390.sc_regt = nict;
    327 	ipkdb_softc.sc_ne2000.sc_dp8390.sc_regh = nich;
    328 	ipkdb_softc.sc_ne2000.sc_asict = asict;
    329 	ipkdb_softc.sc_ne2000.sc_asich = asich;
    330 
    331 	kip->port = &ipkdb_softc;
    332 	ipkdb_pc = pc;
    333 	ipkdb_tag = pa.pa_tag;
    334 	ne_kip = kip;
    335 
    336 	if (ne2000_ipkdb_attach(kip) < 0) {
    337 		bus_space_unmap(nict, nich, NE2000_NPORTS);
    338 		return -1;
    339 	}
    340 
    341 	return 0;
    342 }
    343 #endif /* IPKDB_NE_PCI */
    344