Home | History | Annotate | Line # | Download | only in cardbus
if_tlp_cardbus.c revision 1.1
      1 /*	$NetBSD: if_tlp_cardbus.c,v 1.1 1999/11/19 18:23:35 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 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  * CardBus bus front-end for the Digital Semiconductor ``Tulip'' (21x4x)
     42  * Ethernet controller family driver.
     43  *
     44  * TODO:
     45  *
     46  *	- power management
     47  *	- add support for the Xircom clone
     48  */
     49 
     50 #include "opt_inet.h"
     51 #include "opt_ns.h"
     52 #include "bpfilter.h"
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/malloc.h>
     58 #include <sys/kernel.h>
     59 #include <sys/socket.h>
     60 #include <sys/ioctl.h>
     61 #include <sys/errno.h>
     62 #include <sys/device.h>
     63 
     64 #include <net/if.h>
     65 #include <net/if_dl.h>
     66 #include <net/if_media.h>
     67 #include <net/if_ether.h>
     68 
     69 #if NBPFILTER > 0
     70 #include <net/bpf.h>
     71 #endif
     72 
     73 #ifdef INET
     74 #include <netinet/in.h>
     75 #include <netinet/if_inarp.h>
     76 #endif
     77 
     78 #ifdef NS
     79 #include <netns/ns.h>
     80 #include <netns/ns_if.h>
     81 #endif
     82 
     83 #include <machine/bus.h>
     84 #include <machine/intr.h>
     85 
     86 #include <dev/mii/miivar.h>
     87 #include <dev/mii/mii_bitbang.h>
     88 
     89 #include <dev/ic/tulipreg.h>
     90 #include <dev/ic/tulipvar.h>
     91 
     92 #include <dev/pci/pcivar.h>
     93 #include <dev/pci/pcireg.h>
     94 #include <dev/pci/pcidevs.h>
     95 
     96 #include <dev/cardbus/cardbusvar.h>
     97 
     98 /*
     99  * PCI configuration space registers used by the Tulip.
    100  */
    101 #define	TULIP_PCI_IOBA		0x10	/* i/o mapped base */
    102 #define	TULIP_PCI_MMBA		0x14	/* memory mapped base */
    103 #define	TULIP_PCI_CFDA		0x40	/* configuration driver area */
    104 
    105 #define	CFDA_SLEEP		0x80000000	/* sleep mode */
    106 
    107 struct tulip_cardbus_softc {
    108 	struct tulip_softc sc_tulip;	/* real Tulip softc */
    109 
    110 	/* CardBus-specific goo. */
    111 	void	*sc_ih;			/* interrupt handle */
    112 
    113 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
    114 	int	sc_intrline;		/* our interrupt line */
    115 	cardbustag_t sc_tag;
    116 
    117 	int	sc_cbenable;		/* what CardBus access type to enable */
    118 	int	sc_csr;			/* CSR bits */
    119 };
    120 
    121 int	tlp_cardbus_match __P((struct device *, struct cfdata *, void *));
    122 void	tlp_cardbus_attach __P((struct device *, struct device *, void *));
    123 
    124 struct cfattach tlp_cardbus_ca = {
    125 	sizeof(struct tulip_cardbus_softc),
    126 	    tlp_cardbus_match, tlp_cardbus_attach,
    127 };
    128 
    129 const struct tulip_cardbus_product {
    130 	u_int32_t	tcp_vendor;	/* PCI vendor ID */
    131 	u_int32_t	tcp_product;	/* PCI product ID */
    132 	tulip_chip_t	tcp_chip;	/* base Tulip chip type */
    133 	int		tcp_pmreg;	/* power management register offset */
    134 } tlp_cardbus_products[] = {
    135 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21142,
    136 	  TULIP_CHIP_21142,		0 },
    137 
    138 	{ 0,				0,
    139 	  TULIP_CHIP_INVALID,		0 },
    140 };
    141 
    142 const char *tlp_cardbus_chip_names[] = TULIP_CHIP_NAMES;
    143 
    144 const struct tulip_cardbus_product *tlp_cardbus_lookup
    145     __P((const struct cardbus_attach_args *));
    146 
    147 const struct tulip_cardbus_product *
    148 tlp_cardbus_lookup(ca)
    149 	const struct cardbus_attach_args *ca;
    150 {
    151 	const struct tulip_cardbus_product *tcp;
    152 
    153 	for (tcp = tlp_cardbus_products;
    154 	     tlp_cardbus_chip_names[tcp->tcp_chip] != NULL;
    155 	     tcp++) {
    156 		if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
    157 		    PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
    158 			return (tcp);
    159 	}
    160 	return (NULL);
    161 }
    162 
    163 int
    164 tlp_cardbus_match(parent, match, aux)
    165 	struct device *parent;
    166 	struct cfdata *match;
    167 	void *aux;
    168 {
    169 	struct cardbus_attach_args *ca = aux;
    170 
    171 	if (tlp_cardbus_lookup(ca) != NULL)
    172 		return (1);
    173 
    174 	return (0);
    175 }
    176 
    177 void
    178 tlp_cardbus_attach(parent, self, aux)
    179 	struct device *parent, *self;
    180 	void *aux;
    181 {
    182 	struct tulip_cardbus_softc *csc = (void *) self;
    183 	struct tulip_softc *sc = &csc->sc_tulip;
    184 	struct cardbus_attach_args *ca = aux;
    185 	cardbus_devfunc_t ct = ca->ca_ct;
    186 	cardbus_chipset_tag_t cc = ct->ct_cc;
    187 	cardbus_function_tag_t cf = ct->ct_cf;
    188 	const struct tulip_cardbus_product *tcp;
    189 	u_int8_t enaddr[ETHER_ADDR_LEN];
    190 	pcireg_t reg;
    191 
    192 	sc->sc_devno = ca->ca_device;
    193 	sc->sc_dmat = ca->ca_dmat;
    194 	csc->sc_ct = ct;
    195 	csc->sc_tag = ca->ca_tag;
    196 	csc->sc_intrline = ca->ca_intrline;
    197 
    198 	tcp = tlp_cardbus_lookup(ca);
    199 	if (tcp == NULL) {
    200 		printf("\n");
    201 		panic("tlp_cardbus_attach: impossible");
    202 	}
    203 	sc->sc_chip = tcp->tcp_chip;
    204 
    205 	/*
    206 	 * By default, Tulip registers are 8 bytes long (4 bytes
    207 	 * followed by a 4 byte pad).
    208 	 */
    209 	sc->sc_regshift = 3;
    210 
    211 	/*
    212 	 * Get revision info, and set some chip-specific variables.
    213 	 */
    214 	sc->sc_rev = PCI_REVISION(ca->ca_class);
    215 	switch (sc->sc_chip) {
    216 	case TULIP_CHIP_21142:
    217 		if (sc->sc_rev >= 0x20)
    218 			sc->sc_chip = TULIP_CHIP_21143;
    219 		break;
    220 
    221 	default:
    222 		/* Nothing. */
    223 	}
    224 
    225 	printf(": %s Ethernet, pass %d.%d\n",
    226 	    tlp_cardbus_chip_names[sc->sc_chip],
    227 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    228 
    229 	/*
    230 	 * Map the device.
    231 	 */
    232 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
    233 	if (Cardbus_mapreg_map(csc->sc_ct, TULIP_PCI_MMBA,
    234 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    235 	    &sc->sc_st, &sc->sc_sh, NULL, NULL) == 0) {
    236 		csc->sc_cbenable = CARDBUS_MEM_ENABLE;
    237 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
    238 	} else if (Cardbus_mapreg_map(csc->sc_ct, TULIP_PCI_IOBA,
    239 	    PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, NULL, NULL) == 0) {
    240 		csc->sc_cbenable = CARDBUS_IO_ENABLE;
    241 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
    242 	} else {
    243 		printf("%s: unable to map device registers\n",
    244 		    sc->sc_dev.dv_xname);
    245 		return;
    246 	}
    247 
    248 	/* Make sure the right access type is on the CardBus bridge. */
    249 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cbenable);
    250 
    251 	/* Enable the appropriate bits in the PCI CSR. */
    252 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
    253 	reg = (reg & ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE)) |
    254 	    csc->sc_csr;
    255 	cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
    256 
    257 	/*
    258 	 * Read the contents of the Ethernet Address ROM/SROM.  Some
    259 	 * chips have a 128 byte SROM (6 address bits), and some
    260 	 * have a 512 byte SROM (8 address bits).
    261 	 */
    262 	sc->sc_srom_addrbits = 6;
    263  try_again:
    264 	memset(sc->sc_srom, 0, sizeof(sc->sc_srom));
    265 	tlp_read_srom(sc, 0, TULIP_ROM_SIZE(sc->sc_srom_addrbits) >> 1,
    266 	    sc->sc_srom);
    267 #if 0
    268 	{
    269 		int i;
    270 
    271 		printf("SROM CONTENTS:");
    272 		for (i = 0; i < TULIP_ROM_SIZE(sc->sc_srom_addrbits); i++) {
    273 			if ((i % 8) == 0)
    274 				printf("\n\t");
    275 			printf("0x%02x ", sc->sc_srom[i]);
    276 		}
    277 		printf("\n");
    278 	}
    279 #endif
    280 
    281 	/*
    282 	 * Deal with chip/board quirks.  This includes setting up
    283 	 * the mediasw, and extracting the Ethernet address from
    284 	 * the rombuf.
    285 	 */
    286 	switch (sc->sc_chip) {
    287 	case TULIP_CHIP_21142:
    288 	case TULIP_CHIP_21143:
    289 		/* Check for new format SROM. */
    290 		if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
    291 			/*
    292 			 * Not an ISV SROM; can't cope, for now.
    293 			 */
    294 			goto cant_cope;
    295 		} else {
    296 			/*
    297 			 * We start out with the 2114x ISV media switch.
    298 			 * When we search for quirks, we may change to
    299 			 * a different switch.
    300 			 */
    301 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    302 		}
    303 
    304 		/*
    305 		 * Bail out now if we can't deal with this board.
    306 		 */
    307 		if (sc->sc_mediasw == NULL)
    308 			goto cant_cope;
    309 		break;
    310 
    311 	default:
    312  cant_cope:
    313 		/*
    314 		 * Try reading it again, with larger SROM
    315 		 * size, if we haven't already.
    316 		 */
    317 		if (sc->sc_srom_addrbits != 8) {
    318 			sc->sc_srom_addrbits = 8;
    319 			goto try_again;
    320 		}
    321 
    322 		printf("%s: sorry, unable to handle your board\n",
    323 		    sc->sc_dev.dv_xname);
    324 		return;
    325 	}
    326 
    327 	/*
    328 	 * Map and establish the interrupt.
    329 	 */
    330 	csc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
    331 	    tlp_intr, sc);
    332 	if (csc->sc_ih == NULL) {
    333 		printf("%s: unable to establish interrupt at %d\n",
    334 		    sc->sc_dev.dv_xname, ca->ca_intrline);
    335 		return;
    336 	}
    337 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    338 	    ca->ca_intrline);
    339 
    340 	/*
    341 	 * Finish off the attach.
    342 	 */
    343 	tlp_attach(sc, enaddr);
    344 }
    345