Home | History | Annotate | Line # | Download | only in cardbus
if_tlp_cardbus.c revision 1.10
      1 /*	$NetBSD: if_tlp_cardbus.c,v 1.10 2000/01/25 21:50:30 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 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  */
     48 
     49 #include "opt_inet.h"
     50 #include "opt_ns.h"
     51 #include "bpfilter.h"
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/mbuf.h>
     56 #include <sys/malloc.h>
     57 #include <sys/kernel.h>
     58 #include <sys/socket.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/errno.h>
     61 #include <sys/device.h>
     62 
     63 #include <machine/endian.h>
     64 
     65 #include <net/if.h>
     66 #include <net/if_dl.h>
     67 #include <net/if_media.h>
     68 #include <net/if_ether.h>
     69 
     70 #if NBPFILTER > 0
     71 #include <net/bpf.h>
     72 #endif
     73 
     74 #ifdef INET
     75 #include <netinet/in.h>
     76 #include <netinet/if_inarp.h>
     77 #endif
     78 
     79 #ifdef NS
     80 #include <netns/ns.h>
     81 #include <netns/ns_if.h>
     82 #endif
     83 
     84 #include <machine/bus.h>
     85 #include <machine/intr.h>
     86 
     87 #include <dev/mii/miivar.h>
     88 #include <dev/mii/mii_bitbang.h>
     89 
     90 #include <dev/ic/tulipreg.h>
     91 #include <dev/ic/tulipvar.h>
     92 
     93 #include <dev/pci/pcivar.h>
     94 #include <dev/pci/pcireg.h>
     95 #include <dev/pci/pcidevs.h>
     96 
     97 #include <dev/cardbus/cardbusvar.h>
     98 
     99 /*
    100  * PCI configuration space registers used by the Tulip.
    101  */
    102 #define	TULIP_PCI_IOBA		0x10	/* i/o mapped base */
    103 #define	TULIP_PCI_MMBA		0x14	/* memory mapped base */
    104 #define	TULIP_PCI_CFDA		0x40	/* configuration driver area */
    105 
    106 #define	CFDA_SLEEP		0x80000000	/* sleep mode */
    107 #define	CFDA_SNOOZE		0x40000000	/* snooze mode */
    108 
    109 struct tulip_cardbus_softc {
    110 	struct tulip_softc sc_tulip;	/* real Tulip softc */
    111 
    112 	/* CardBus-specific goo. */
    113 	void	*sc_ih;			/* interrupt handle */
    114 
    115 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
    116 	int	sc_intrline;		/* our interrupt line */
    117 	cardbustag_t sc_tag;
    118 
    119 	int	sc_cbenable;		/* what CardBus access type to enable */
    120 	int	sc_csr;			/* CSR bits */
    121 };
    122 
    123 int	tlp_cardbus_match __P((struct device *, struct cfdata *, void *));
    124 void	tlp_cardbus_attach __P((struct device *, struct device *, void *));
    125 
    126 struct cfattach tlp_cardbus_ca = {
    127 	sizeof(struct tulip_cardbus_softc),
    128 	    tlp_cardbus_match, tlp_cardbus_attach,
    129 };
    130 
    131 const struct tulip_cardbus_product {
    132 	u_int32_t	tcp_vendor;	/* PCI vendor ID */
    133 	u_int32_t	tcp_product;	/* PCI product ID */
    134 	tulip_chip_t	tcp_chip;	/* base Tulip chip type */
    135 	int		tcp_pmreg;	/* power management register offset */
    136 } tlp_cardbus_products[] = {
    137 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21142,
    138 	  TULIP_CHIP_21142,		0xe0 },
    139 
    140 	{ PCI_VENDOR_XIRCOM,		PCI_PRODUCT_XIRCOM_X3201_3_21143,
    141 	  TULIP_CHIP_X3201_3,		0xe0 },
    142 
    143 	{ 0,				0,
    144 	  TULIP_CHIP_INVALID,		0 },
    145 };
    146 
    147 void	tlp_cardbus_x3201_reset __P((struct tulip_softc *));
    148 
    149 const struct tulip_cardbus_product *tlp_cardbus_lookup
    150     __P((const struct cardbus_attach_args *));
    151 
    152 const struct tulip_cardbus_product *
    153 tlp_cardbus_lookup(ca)
    154 	const struct cardbus_attach_args *ca;
    155 {
    156 	const struct tulip_cardbus_product *tcp;
    157 
    158 	for (tcp = tlp_cardbus_products;
    159 	     tlp_chip_names[tcp->tcp_chip] != NULL;
    160 	     tcp++) {
    161 		if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
    162 		    PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
    163 			return (tcp);
    164 	}
    165 	return (NULL);
    166 }
    167 
    168 int
    169 tlp_cardbus_match(parent, match, aux)
    170 	struct device *parent;
    171 	struct cfdata *match;
    172 	void *aux;
    173 {
    174 	struct cardbus_attach_args *ca = aux;
    175 
    176 	if (tlp_cardbus_lookup(ca) != NULL)
    177 		return (1);
    178 
    179 	return (0);
    180 }
    181 
    182 void
    183 tlp_cardbus_attach(parent, self, aux)
    184 	struct device *parent, *self;
    185 	void *aux;
    186 {
    187 	struct tulip_cardbus_softc *csc = (void *) self;
    188 	struct tulip_softc *sc = &csc->sc_tulip;
    189 	struct cardbus_attach_args *ca = aux;
    190 	cardbus_devfunc_t ct = ca->ca_ct;
    191 	cardbus_chipset_tag_t cc = ct->ct_cc;
    192 	cardbus_function_tag_t cf = ct->ct_cf;
    193 	const struct tulip_cardbus_product *tcp;
    194 	u_int8_t enaddr[ETHER_ADDR_LEN];
    195 	pcireg_t reg;
    196 
    197 	sc->sc_devno = ca->ca_device;
    198 	sc->sc_dmat = ca->ca_dmat;
    199 	csc->sc_ct = ct;
    200 	csc->sc_tag = ca->ca_tag;
    201 	csc->sc_intrline = ca->ca_intrline;
    202 
    203 	tcp = tlp_cardbus_lookup(ca);
    204 	if (tcp == NULL) {
    205 		printf("\n");
    206 		panic("tlp_cardbus_attach: impossible");
    207 	}
    208 	sc->sc_chip = tcp->tcp_chip;
    209 
    210 	/*
    211 	 * By default, Tulip registers are 8 bytes long (4 bytes
    212 	 * followed by a 4 byte pad).
    213 	 */
    214 	sc->sc_regshift = 3;
    215 
    216 	/*
    217 	 * Some chips have a 128 byte SROM (6 address bits), and some
    218 	 * have a 512 byte SROM (8 address bits).  Default to 6; we'll
    219 	 * adjust below.
    220 	 */
    221 	sc->sc_srom_addrbits = 6;
    222 
    223 	/*
    224 	 * Get revision info, and set some chip-specific variables.
    225 	 */
    226 	sc->sc_rev = PCI_REVISION(ca->ca_class);
    227 	switch (sc->sc_chip) {
    228 	case TULIP_CHIP_21142:
    229 		if (sc->sc_rev >= 0x20)
    230 			sc->sc_chip = TULIP_CHIP_21143;
    231 		if (sc->sc_rev >= 0x41) {
    232 			/*
    233 			 * 21143 rev. 4.1 has a larger SROM.
    234 			 */
    235 			sc->sc_srom_addrbits = 8;
    236 		}
    237 		break;
    238 
    239 	default:
    240 		/* Nothing. */
    241 	}
    242 
    243 	printf(": %s Ethernet, pass %d.%d\n",
    244 	    tlp_chip_names[sc->sc_chip],
    245 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    246 
    247 	/*
    248 	 * Check to see if the device is in power-save mode, and
    249 	 * bring it out if necessary.
    250 	 */
    251 	switch (sc->sc_chip) {
    252 	case TULIP_CHIP_21142:
    253 	case TULIP_CHIP_21143:
    254 	case TULIP_CHIP_X3201_3:
    255 		/*
    256 		 * Clear the "sleep mode" bit in the CFDA register.
    257 		 */
    258 		reg = cardbus_conf_read(cc, cf, ca->ca_tag, TULIP_PCI_CFDA);
    259 		if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
    260 			cardbus_conf_write(cc, cf, ca->ca_tag, TULIP_PCI_CFDA,
    261 			    reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
    262 		break;
    263 
    264 	default:
    265 		/* Nothing. */
    266 	}
    267 
    268 	if (cardbus_get_capability(cc, cf, ca->ca_tag, PCI_CAP_PWRMGMT, 0, 0)) {
    269 		if (tcp->tcp_pmreg == 0) {
    270 			printf("%s: don't know location of PMCSR for this "
    271 			    "chip\n", sc->sc_dev.dv_xname);
    272 			return;
    273 		}
    274 		reg = cardbus_conf_read(cc, cf, ca->ca_tag,
    275 		    tcp->tcp_pmreg) & 0x03;
    276 #if 1 /* XXX Probably not right for CardBus. */
    277 		if (reg == 3) {
    278 			/*
    279 			 * The card has lost all configuration data in
    280 			 * this state, so punt.
    281 			 */
    282 			printf("%s: unable to wake up from power state D3\n",
    283 			    sc->sc_dev.dv_xname);
    284 			return;
    285 		}
    286 #endif
    287 		if (reg != 0) {
    288 			printf("%s: waking up from power state D%d\n",
    289 			    sc->sc_dev.dv_xname, reg);
    290 			cardbus_conf_write(cc, cf, ca->ca_tag,
    291 			    tcp->tcp_pmreg, 0);
    292 		}
    293 	}
    294 
    295 	/*
    296 	 * Map the device.
    297 	 */
    298 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
    299 	if (Cardbus_mapreg_map(csc->sc_ct, TULIP_PCI_MMBA,
    300 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    301 	    &sc->sc_st, &sc->sc_sh, NULL, NULL) == 0) {
    302 		csc->sc_cbenable = CARDBUS_MEM_ENABLE;
    303 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
    304 	} else if (Cardbus_mapreg_map(csc->sc_ct, TULIP_PCI_IOBA,
    305 	    PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, NULL, NULL) == 0) {
    306 		csc->sc_cbenable = CARDBUS_IO_ENABLE;
    307 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
    308 	} else {
    309 		printf("%s: unable to map device registers\n",
    310 		    sc->sc_dev.dv_xname);
    311 		return;
    312 	}
    313 
    314 	/* Make sure the right access type is on the CardBus bridge. */
    315 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cbenable);
    316 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    317 
    318 	/* Enable the appropriate bits in the PCI CSR. */
    319 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
    320 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    321 	reg |= csc->sc_csr;
    322 	cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
    323 
    324 	/*
    325 	 * Make sure the latency timer is set to some reasonable
    326 	 * value.
    327 	 */
    328 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_BHLC_REG);
    329 	if (PCI_LATTIMER(reg) < 0x20) {
    330 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    331 		reg |= (0x20 << PCI_LATTIMER_SHIFT);
    332 		cardbus_conf_write(cc, cf, ca->ca_tag, PCI_BHLC_REG, reg);
    333 	}
    334 
    335 	/*
    336 	 * Read the contents of the Ethernet Address ROM/SROM.
    337 	 */
    338 	memset(sc->sc_srom, 0, sizeof(sc->sc_srom));
    339 	switch (sc->sc_chip) {
    340 	case TULIP_CHIP_X3201_3:
    341 		/*
    342 		 * No SROM on this chip.
    343 		 */
    344 		break;
    345 
    346 	default:
    347 		tlp_read_srom(sc, 0, TULIP_ROM_SIZE(sc->sc_srom_addrbits) >> 1,
    348 		    sc->sc_srom);
    349 #if 0
    350 		{
    351 			int i;
    352 
    353 			printf("SROM CONTENTS:");
    354 			for (i = 0; i <
    355 			    TULIP_ROM_SIZE(sc->sc_srom_addrbits); i++) {
    356 				if ((i % 8) == 0)
    357 					printf("\n\t");
    358 				printf("0x%02x ", sc->sc_srom[i]);
    359 			}
    360 			printf("\n");
    361 		}
    362 #endif
    363 	}
    364 
    365 	/*
    366 	 * Deal with chip/board quirks.  This includes setting up
    367 	 * the mediasw, and extracting the Ethernet address from
    368 	 * the rombuf.
    369 	 */
    370 	switch (sc->sc_chip) {
    371 	case TULIP_CHIP_21142:
    372 	case TULIP_CHIP_21143:
    373 		/* Check for new format SROM. */
    374 		if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
    375 			/*
    376 			 * Not an ISV SROM; can't cope, for now.
    377 			 */
    378 			goto cant_cope;
    379 		} else {
    380 			/*
    381 			 * We start out with the 2114x ISV media switch.
    382 			 * When we search for quirks, we may change to
    383 			 * a different switch.
    384 			 */
    385 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    386 		}
    387 
    388 		/*
    389 		 * Bail out now if we can't deal with this board.
    390 		 */
    391 		if (sc->sc_mediasw == NULL)
    392 			goto cant_cope;
    393 		break;
    394 
    395 	case TULIP_CHIP_X3201_3:
    396 		/*
    397 		 * The X3201 doens't have an SROM.  Lift the MAC address
    398 		 * from the CIS.  Also, we have a special media switch:
    399 		 * MII-on-SIO, plus some special GPIO setup.
    400 		 */
    401 		memcpy(enaddr, ca->ca_cis.funce.network.netid, sizeof(enaddr));
    402 		sc->sc_reset = tlp_cardbus_x3201_reset;
    403 		sc->sc_mediasw = &tlp_sio_mii_mediasw;
    404 		break;
    405 
    406 	default:
    407  cant_cope:
    408 		printf("%s: sorry, unable to handle your board\n",
    409 		    sc->sc_dev.dv_xname);
    410 		return;
    411 	}
    412 
    413 	/*
    414 	 * Map and establish the interrupt.
    415 	 */
    416 	csc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_NET,
    417 	    tlp_intr, sc);
    418 	if (csc->sc_ih == NULL) {
    419 		printf("%s: unable to establish interrupt at %d\n",
    420 		    sc->sc_dev.dv_xname, ca->ca_intrline);
    421 		return;
    422 	}
    423 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    424 	    ca->ca_intrline);
    425 
    426 	/*
    427 	 * Finish off the attach.
    428 	 */
    429 	tlp_attach(sc, enaddr);
    430 }
    431 
    432 void
    433 tlp_cardbus_x3201_reset(sc)
    434 	struct tulip_softc *sc;
    435 {
    436 	u_int32_t reg;
    437 
    438 	reg = TULIP_READ(sc, CSR_SIAGEN);
    439 
    440 	/* make GP[2,0] outputs */
    441 	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
    442 	    0x00050000);
    443 	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
    444 }
    445