Home | History | Annotate | Line # | Download | only in cardbus
if_tlp_cardbus.c revision 1.22
      1 /*	$NetBSD: if_tlp_cardbus.c,v 1.22 2000/03/22 01:35:14 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 
     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 <machine/endian.h>
     60 
     61 #include <net/if.h>
     62 #include <net/if_dl.h>
     63 #include <net/if_media.h>
     64 #include <net/if_ether.h>
     65 
     66 #if NBPFILTER > 0
     67 #include <net/bpf.h>
     68 #endif
     69 
     70 #ifdef INET
     71 #include <netinet/in.h>
     72 #include <netinet/if_inarp.h>
     73 #endif
     74 
     75 #ifdef NS
     76 #include <netns/ns.h>
     77 #include <netns/ns_if.h>
     78 #endif
     79 
     80 #include <machine/bus.h>
     81 #include <machine/intr.h>
     82 
     83 #include <dev/mii/miivar.h>
     84 #include <dev/mii/mii_bitbang.h>
     85 
     86 #include <dev/ic/tulipreg.h>
     87 #include <dev/ic/tulipvar.h>
     88 
     89 #include <dev/pci/pcivar.h>
     90 #include <dev/pci/pcireg.h>
     91 #include <dev/pci/pcidevs.h>
     92 
     93 #include <dev/cardbus/cardbusvar.h>
     94 
     95 /*
     96  * PCI configuration space registers used by the Tulip.
     97  */
     98 #define	TULIP_PCI_IOBA		0x10	/* i/o mapped base */
     99 #define	TULIP_PCI_MMBA		0x14	/* memory mapped base */
    100 #define	TULIP_PCI_CFDA		0x40	/* configuration driver area */
    101 
    102 #define	CFDA_SLEEP		0x80000000	/* sleep mode */
    103 #define	CFDA_SNOOZE		0x40000000	/* snooze mode */
    104 
    105 struct tulip_cardbus_softc {
    106 	struct tulip_softc sc_tulip;	/* real Tulip softc */
    107 
    108 	/* CardBus-specific goo. */
    109 	void	*sc_ih;			/* interrupt handle */
    110 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
    111 	cardbustag_t sc_tag;		/* our CardBus tag */
    112 	int	sc_csr;			/* CSR bits */
    113 	bus_size_t sc_mapsize;		/* the size of mapped bus space
    114 					   region */
    115 					/* product info */
    116 	const struct tulip_cardbus_product *sc_product;
    117 
    118 	int	sc_cben;		/* CardBus enables */
    119 	int	sc_bar_reg;		/* which BAR to use */
    120 	pcireg_t sc_bar_val;		/* value of the BAR */
    121 
    122 	int	sc_intrline;		/* interrupt line */
    123 };
    124 
    125 int	tlp_cardbus_match __P((struct device *, struct cfdata *, void *));
    126 void	tlp_cardbus_attach __P((struct device *, struct device *, void *));
    127 int	tlp_cardbus_detach __P((struct device *, int));
    128 
    129 struct cfattach tlp_cardbus_ca = {
    130 	sizeof(struct tulip_cardbus_softc),
    131 	    tlp_cardbus_match, tlp_cardbus_attach,
    132 		tlp_cardbus_detach, tlp_activate,
    133 };
    134 
    135 const struct tulip_cardbus_product {
    136 	u_int32_t	tcp_vendor;	/* PCI vendor ID */
    137 	u_int32_t	tcp_product;	/* PCI product ID */
    138 	tulip_chip_t	tcp_chip;	/* base Tulip chip type */
    139 	int		tcp_pmreg;	/* power management register offset */
    140 } tlp_cardbus_products[] = {
    141 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21142,
    142 	  TULIP_CHIP_21142,		0xe0 },
    143 
    144 	{ PCI_VENDOR_XIRCOM,		PCI_PRODUCT_XIRCOM_X3201_3_21143,
    145 	  TULIP_CHIP_X3201_3,		0xe0 },
    146 
    147 	{ 0,				0,
    148 	  TULIP_CHIP_INVALID,		0 },
    149 };
    150 
    151 void	tlp_cardbus_setup __P((struct tulip_cardbus_softc *));
    152 
    153 int	tlp_cardbus_enable __P((struct tulip_softc *));
    154 void	tlp_cardbus_disable __P((struct tulip_softc *));
    155 void	tlp_cardbus_power __P((struct tulip_softc *, int));
    156 
    157 void	tlp_cardbus_x3201_reset __P((struct tulip_softc *));
    158 
    159 const struct tulip_cardbus_product *tlp_cardbus_lookup
    160     __P((const struct cardbus_attach_args *));
    161 
    162 const struct tulip_cardbus_product *
    163 tlp_cardbus_lookup(ca)
    164 	const struct cardbus_attach_args *ca;
    165 {
    166 	const struct tulip_cardbus_product *tcp;
    167 
    168 	for (tcp = tlp_cardbus_products;
    169 	     tlp_chip_names[tcp->tcp_chip] != NULL;
    170 	     tcp++) {
    171 		if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
    172 		    PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
    173 			return (tcp);
    174 	}
    175 	return (NULL);
    176 }
    177 
    178 int
    179 tlp_cardbus_match(parent, match, aux)
    180 	struct device *parent;
    181 	struct cfdata *match;
    182 	void *aux;
    183 {
    184 	struct cardbus_attach_args *ca = aux;
    185 
    186 	if (tlp_cardbus_lookup(ca) != NULL)
    187 		return (1);
    188 
    189 	return (0);
    190 }
    191 
    192 void
    193 tlp_cardbus_attach(parent, self, aux)
    194 	struct device *parent, *self;
    195 	void *aux;
    196 {
    197 	struct tulip_cardbus_softc *csc = (void *)self;
    198 	struct tulip_softc *sc = &csc->sc_tulip;
    199 	struct cardbus_attach_args *ca = aux;
    200 	cardbus_devfunc_t ct = ca->ca_ct;
    201 	const struct tulip_cardbus_product *tcp;
    202 	u_int8_t enaddr[ETHER_ADDR_LEN];
    203 	bus_addr_t adr;
    204 
    205 	sc->sc_devno = ca->ca_device;
    206 	sc->sc_dmat = ca->ca_dmat;
    207 	csc->sc_ct = ct;
    208 	csc->sc_tag = ca->ca_tag;
    209 
    210 	tcp = tlp_cardbus_lookup(ca);
    211 	if (tcp == NULL) {
    212 		printf("\n");
    213 		panic("tlp_cardbus_attach: impossible");
    214 	}
    215 	sc->sc_chip = tcp->tcp_chip;
    216 	csc->sc_product = tcp;
    217 
    218 	/*
    219 	 * By default, Tulip registers are 8 bytes long (4 bytes
    220 	 * followed by a 4 byte pad).
    221 	 */
    222 	sc->sc_regshift = 3;
    223 
    224 	/*
    225 	 * Power management hooks.
    226 	 */
    227 	sc->sc_enable = tlp_cardbus_enable;
    228 	sc->sc_disable = tlp_cardbus_disable;
    229 	sc->sc_power = tlp_cardbus_power;
    230 
    231 	/*
    232 	 * Get revision info, and set some chip-specific variables.
    233 	 */
    234 	sc->sc_rev = PCI_REVISION(ca->ca_class);
    235 	switch (sc->sc_chip) {
    236 	case TULIP_CHIP_21142:
    237 		if (sc->sc_rev >= 0x20)
    238 			sc->sc_chip = TULIP_CHIP_21143;
    239 		break;
    240 
    241 	default:
    242 		/* Nothing. */
    243 	}
    244 
    245 	printf(": %s Ethernet, pass %d.%d\n",
    246 	    tlp_chip_names[sc->sc_chip],
    247 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    248 
    249 	/*
    250 	 * Map the device.
    251 	 */
    252 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
    253 	if (Cardbus_mapreg_map(ct, TULIP_PCI_IOBA,
    254 	    PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
    255 	    &csc->sc_mapsize) == 0) {
    256 #if rbus
    257 #else
    258 		(*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
    259 #endif
    260 		csc->sc_cben = CARDBUS_IO_ENABLE;
    261 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
    262 		csc->sc_bar_reg = TULIP_PCI_IOBA;
    263 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
    264 	} else if (Cardbus_mapreg_map(ct, TULIP_PCI_MMBA,
    265 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    266 	    &sc->sc_st, &sc->sc_sh, &adr, &csc->sc_mapsize) == 0) {
    267 #if rbus
    268 #else
    269 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    270 #endif
    271 		csc->sc_cben = CARDBUS_MEM_ENABLE;
    272 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
    273 		csc->sc_bar_reg = TULIP_PCI_MMBA;
    274 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
    275 	} else {
    276 		printf("%s: unable to map device registers\n",
    277 		    sc->sc_dev.dv_xname);
    278 		return;
    279 	}
    280 
    281 	/*
    282 	 * Bring the chip out of powersave mode and initialize the
    283 	 * configuration registers.
    284 	 */
    285 	tlp_cardbus_setup(csc);
    286 
    287 	/*
    288 	 * Read the contents of the Ethernet Address ROM/SROM.
    289 	 */
    290 	switch (sc->sc_chip) {
    291 	case TULIP_CHIP_X3201_3:
    292 		/*
    293 		 * No SROM on this chip.
    294 		 */
    295 		break;
    296 
    297 	default:
    298 		if (tlp_read_srom(sc) == 0)
    299 			goto cant_cope;
    300 		break;
    301 	}
    302 
    303 	/*
    304 	 * Deal with chip/board quirks.  This includes setting up
    305 	 * the mediasw, and extracting the Ethernet address from
    306 	 * the rombuf.
    307 	 */
    308 	switch (sc->sc_chip) {
    309 	case TULIP_CHIP_21142:
    310 	case TULIP_CHIP_21143:
    311 		/* Check for new format SROM. */
    312 		if (tlp_isv_srom_enaddr(sc, enaddr) == 0) {
    313 			/*
    314 			 * Not an ISV SROM; try the old DEC Ethernet Address
    315 			 * ROM format.
    316 			 */
    317 			if (tlp_parse_old_srom(sc, enaddr) == 0)
    318 				goto cant_cope;
    319 		} else {
    320 			/*
    321 			 * We start out with the 2114x ISV media switch.
    322 			 * When we search for quirks, we may change to
    323 			 * a different switch.
    324 			 */
    325 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    326 		}
    327 
    328 		/*
    329 		 * Bail out now if we can't deal with this board.
    330 		 */
    331 		if (sc->sc_mediasw == NULL)
    332 			goto cant_cope;
    333 		break;
    334 
    335 	case TULIP_CHIP_X3201_3:
    336 		/*
    337 		 * The X3201 doesn't have an SROM.  Lift the MAC address
    338 		 * from the CIS.  Also, we have a special media switch:
    339 		 * MII-on-SIO, plus some special GPIO setup.
    340 		 */
    341 		memcpy(enaddr, ca->ca_cis.funce.network.netid, sizeof(enaddr));
    342 		sc->sc_reset = tlp_cardbus_x3201_reset;
    343 		sc->sc_mediasw = &tlp_sio_mii_mediasw;
    344 		break;
    345 
    346 	default:
    347  cant_cope:
    348 		printf("%s: sorry, unable to handle your board\n",
    349 		    sc->sc_dev.dv_xname);
    350 		return;
    351 	}
    352 
    353 	/* Remember which interrupt line. */
    354 	csc->sc_intrline = ca->ca_intrline;
    355 
    356 	/*
    357 	 * Finish off the attach.
    358 	 */
    359 	tlp_attach(sc, enaddr);
    360 
    361 	/*
    362 	 * Power down the socket.
    363 	 */
    364 	Cardbus_function_disable(csc->sc_ct);
    365 }
    366 
    367 int
    368 tlp_cardbus_detach(self, flags)
    369 	struct device *self;
    370 	int flags;
    371 {
    372 	struct tulip_cardbus_softc *csc = (void *)self;
    373 	struct tulip_softc *sc = &csc->sc_tulip;
    374 	struct cardbus_devfunc *ct = csc->sc_ct;
    375 	int rv;
    376 
    377 #if defined(DIAGNOSTIC)
    378 	if (ct == NULL)
    379 		panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
    380 #endif
    381 
    382 	rv = tlp_detach(sc);
    383 	if (rv)
    384 		return (rv);
    385 
    386 	/*
    387 	 * Unhook the interrupt handler.
    388 	 */
    389 	if (csc->sc_ih != NULL)
    390 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    391 
    392 	/*
    393 	 * Release bus space and close window.
    394 	 */
    395 	if (csc->sc_bar_reg != 0)
    396 		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
    397 		    sc->sc_st, sc->sc_sh, csc->sc_mapsize);
    398 
    399 	return (0);
    400 }
    401 
    402 int
    403 tlp_cardbus_enable(sc)
    404 	struct tulip_softc *sc;
    405 {
    406 	struct tulip_cardbus_softc *csc = (void *) sc;
    407 	cardbus_devfunc_t ct = csc->sc_ct;
    408 	cardbus_chipset_tag_t cc = ct->ct_cc;
    409 	cardbus_function_tag_t cf = ct->ct_cf;
    410 
    411 	/*
    412 	 * Power on the socket.
    413 	 */
    414 	Cardbus_function_enable(ct);
    415 
    416 	/*
    417 	 * Set up the PCI configuration registers.
    418 	 */
    419 	tlp_cardbus_setup(csc);
    420 
    421 	/*
    422 	 * Map and establish the interrupt.
    423 	 */
    424 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
    425 	    tlp_intr, sc);
    426 	if (csc->sc_ih == NULL) {
    427 		printf("%s: unable to establish interrupt at %d\n",
    428 		    sc->sc_dev.dv_xname, csc->sc_intrline);
    429 		Cardbus_function_disable(csc->sc_ct);
    430 		return (1);
    431 	}
    432 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    433 	    csc->sc_intrline);
    434 
    435 	return (0);
    436 }
    437 
    438 void
    439 tlp_cardbus_disable(sc)
    440 	struct tulip_softc *sc;
    441 {
    442 	struct tulip_cardbus_softc *csc = (void *) sc;
    443 	cardbus_devfunc_t ct = csc->sc_ct;
    444 	cardbus_chipset_tag_t cc = ct->ct_cc;
    445 	cardbus_function_tag_t cf = ct->ct_cf;
    446 
    447 	/* Unhook the interrupt handler. */
    448 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    449 	csc->sc_ih = NULL;
    450 
    451 	/* Power down the socket. */
    452 	Cardbus_function_disable(ct);
    453 }
    454 
    455 void
    456 tlp_cardbus_power(sc, why)
    457 	struct tulip_softc *sc;
    458 	int why;
    459 {
    460 	struct tulip_cardbus_softc *csc = (void *) sc;
    461 
    462 	if (why == PWR_RESUME) {
    463 		/*
    464 		 * Give the PCI configuration registers a kick
    465 		 * in the head.
    466 		 */
    467 #ifdef DIAGNOSTIC
    468 		if (TULIP_IS_ENABLED(sc) == 0)
    469 			panic("tlp_cardbus_power");
    470 #endif
    471 		tlp_cardbus_setup(csc);
    472 	}
    473 }
    474 
    475 void
    476 tlp_cardbus_setup(csc)
    477 	struct tulip_cardbus_softc *csc;
    478 {
    479 	struct tulip_softc *sc = &csc->sc_tulip;
    480 	const struct tulip_cardbus_product *tcp = csc->sc_product;
    481 	cardbus_devfunc_t ct = csc->sc_ct;
    482 	cardbus_chipset_tag_t cc = ct->ct_cc;
    483 	cardbus_function_tag_t cf = ct->ct_cf;
    484 	pcireg_t reg;
    485 
    486 	/*
    487 	 * Check to see if the device is in power-save mode, and
    488 	 * bring it out if necessary.
    489 	 */
    490 	switch (sc->sc_chip) {
    491 	case TULIP_CHIP_21142:
    492 	case TULIP_CHIP_21143:
    493 	case TULIP_CHIP_X3201_3:
    494 		/*
    495 		 * Clear the "sleep mode" bit in the CFDA register.
    496 		 */
    497 		reg = cardbus_conf_read(cc, cf, csc->sc_tag, TULIP_PCI_CFDA);
    498 		if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
    499 			cardbus_conf_write(cc, cf, csc->sc_tag, TULIP_PCI_CFDA,
    500 			    reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
    501 		break;
    502 
    503 	default:
    504 		/* Nothing. */
    505 	}
    506 
    507 	if (cardbus_get_capability(cc, cf, csc->sc_tag,
    508 	    PCI_CAP_PWRMGMT, 0, 0)) {
    509 		if (tcp->tcp_pmreg == 0) {
    510 			printf("%s: don't know location of PMCSR for this "
    511 			    "chip\n", sc->sc_dev.dv_xname);
    512 			return;
    513 		}
    514 		reg = cardbus_conf_read(cc, cf, csc->sc_tag,
    515 		    tcp->tcp_pmreg) & 0x03;
    516 #if 1 /* XXX Probably not right for CardBus. */
    517 		if (reg == 3) {
    518 			/*
    519 			 * The card has lost all configuration data in
    520 			 * this state, so punt.
    521 			 */
    522 			printf("%s: unable to wake up from power state D3\n",
    523 			    sc->sc_dev.dv_xname);
    524 			return;
    525 		}
    526 #endif
    527 		if (reg != 0) {
    528 			printf("%s: waking up from power state D%d\n",
    529 			    sc->sc_dev.dv_xname, reg);
    530 			cardbus_conf_write(cc, cf, csc->sc_tag,
    531 			    tcp->tcp_pmreg, 0);
    532 		}
    533 	}
    534 
    535 	/* Make sure the right access type is on the CardBus bridge. */
    536 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
    537 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    538 
    539 	/* Program the BAR. */
    540 	cardbus_conf_write(cc, cf, csc->sc_tag, csc->sc_bar_reg,
    541 	    csc->sc_bar_val);
    542 
    543 	/* Enable the appropriate bits in the PCI CSR. */
    544 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG);
    545 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    546 	reg |= csc->sc_csr;
    547 	cardbus_conf_write(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
    548 
    549 	/*
    550 	 * Make sure the latency timer is set to some reasonable
    551 	 * value.
    552 	 */
    553 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_BHLC_REG);
    554 	if (PCI_LATTIMER(reg) < 0x20) {
    555 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    556 		reg |= (0x20 << PCI_LATTIMER_SHIFT);
    557 		cardbus_conf_write(cc, cf, csc->sc_tag, PCI_BHLC_REG, reg);
    558 	}
    559 }
    560 
    561 void
    562 tlp_cardbus_x3201_reset(sc)
    563 	struct tulip_softc *sc;
    564 {
    565 	u_int32_t reg;
    566 
    567 	reg = TULIP_READ(sc, CSR_SIAGEN);
    568 
    569 	/* make GP[2,0] outputs */
    570 	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
    571 	    0x00050000);
    572 	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
    573 }
    574