Home | History | Annotate | Line # | Download | only in cardbus
if_tlp_cardbus.c revision 1.21
      1 /*	$NetBSD: if_tlp_cardbus.c,v 1.21 2000/03/19 21:45:24 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 	Cardbus_mapreg_unmap(ct, csc->sc_bar_reg, sc->sc_st, sc->sc_sh,
    396 	    csc->sc_mapsize);
    397 
    398 	return (0);
    399 }
    400 
    401 int
    402 tlp_cardbus_enable(sc)
    403 	struct tulip_softc *sc;
    404 {
    405 	struct tulip_cardbus_softc *csc = (void *) sc;
    406 	cardbus_devfunc_t ct = csc->sc_ct;
    407 	cardbus_chipset_tag_t cc = ct->ct_cc;
    408 	cardbus_function_tag_t cf = ct->ct_cf;
    409 
    410 	/*
    411 	 * Power on the socket.
    412 	 */
    413 	Cardbus_function_enable(ct);
    414 
    415 	/*
    416 	 * Set up the PCI configuration registers.
    417 	 */
    418 	tlp_cardbus_setup(csc);
    419 
    420 	/*
    421 	 * Map and establish the interrupt.
    422 	 */
    423 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
    424 	    tlp_intr, sc);
    425 	if (csc->sc_ih == NULL) {
    426 		printf("%s: unable to establish interrupt at %d\n",
    427 		    sc->sc_dev.dv_xname, csc->sc_intrline);
    428 		Cardbus_function_disable(csc->sc_ct);
    429 		return (1);
    430 	}
    431 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    432 	    csc->sc_intrline);
    433 
    434 	return (0);
    435 }
    436 
    437 void
    438 tlp_cardbus_disable(sc)
    439 	struct tulip_softc *sc;
    440 {
    441 	struct tulip_cardbus_softc *csc = (void *) sc;
    442 	cardbus_devfunc_t ct = csc->sc_ct;
    443 	cardbus_chipset_tag_t cc = ct->ct_cc;
    444 	cardbus_function_tag_t cf = ct->ct_cf;
    445 
    446 	/* Unhook the interrupt handler. */
    447 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    448 	csc->sc_ih = NULL;
    449 
    450 	/* Power down the socket. */
    451 	Cardbus_function_disable(ct);
    452 }
    453 
    454 void
    455 tlp_cardbus_power(sc, why)
    456 	struct tulip_softc *sc;
    457 	int why;
    458 {
    459 	struct tulip_cardbus_softc *csc = (void *) sc;
    460 
    461 	if (why == PWR_RESUME) {
    462 		/*
    463 		 * Give the PCI configuration registers a kick
    464 		 * in the head.
    465 		 */
    466 #ifdef DIAGNOSTIC
    467 		if (TULIP_IS_ENABLED(sc) == 0)
    468 			panic("tlp_cardbus_power");
    469 #endif
    470 		tlp_cardbus_setup(csc);
    471 	}
    472 }
    473 
    474 void
    475 tlp_cardbus_setup(csc)
    476 	struct tulip_cardbus_softc *csc;
    477 {
    478 	struct tulip_softc *sc = &csc->sc_tulip;
    479 	const struct tulip_cardbus_product *tcp = csc->sc_product;
    480 	cardbus_devfunc_t ct = csc->sc_ct;
    481 	cardbus_chipset_tag_t cc = ct->ct_cc;
    482 	cardbus_function_tag_t cf = ct->ct_cf;
    483 	pcireg_t reg;
    484 
    485 	/*
    486 	 * Check to see if the device is in power-save mode, and
    487 	 * bring it out if necessary.
    488 	 */
    489 	switch (sc->sc_chip) {
    490 	case TULIP_CHIP_21142:
    491 	case TULIP_CHIP_21143:
    492 	case TULIP_CHIP_X3201_3:
    493 		/*
    494 		 * Clear the "sleep mode" bit in the CFDA register.
    495 		 */
    496 		reg = cardbus_conf_read(cc, cf, csc->sc_tag, TULIP_PCI_CFDA);
    497 		if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
    498 			cardbus_conf_write(cc, cf, csc->sc_tag, TULIP_PCI_CFDA,
    499 			    reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
    500 		break;
    501 
    502 	default:
    503 		/* Nothing. */
    504 	}
    505 
    506 	if (cardbus_get_capability(cc, cf, csc->sc_tag,
    507 	    PCI_CAP_PWRMGMT, 0, 0)) {
    508 		if (tcp->tcp_pmreg == 0) {
    509 			printf("%s: don't know location of PMCSR for this "
    510 			    "chip\n", sc->sc_dev.dv_xname);
    511 			return;
    512 		}
    513 		reg = cardbus_conf_read(cc, cf, csc->sc_tag,
    514 		    tcp->tcp_pmreg) & 0x03;
    515 #if 1 /* XXX Probably not right for CardBus. */
    516 		if (reg == 3) {
    517 			/*
    518 			 * The card has lost all configuration data in
    519 			 * this state, so punt.
    520 			 */
    521 			printf("%s: unable to wake up from power state D3\n",
    522 			    sc->sc_dev.dv_xname);
    523 			return;
    524 		}
    525 #endif
    526 		if (reg != 0) {
    527 			printf("%s: waking up from power state D%d\n",
    528 			    sc->sc_dev.dv_xname, reg);
    529 			cardbus_conf_write(cc, cf, csc->sc_tag,
    530 			    tcp->tcp_pmreg, 0);
    531 		}
    532 	}
    533 
    534 	/* Make sure the right access type is on the CardBus bridge. */
    535 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
    536 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    537 
    538 	/* Program the BAR. */
    539 	cardbus_conf_write(cc, cf, csc->sc_tag, csc->sc_bar_reg,
    540 	    csc->sc_bar_val);
    541 
    542 	/* Enable the appropriate bits in the PCI CSR. */
    543 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG);
    544 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    545 	reg |= csc->sc_csr;
    546 	cardbus_conf_write(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
    547 
    548 	/*
    549 	 * Make sure the latency timer is set to some reasonable
    550 	 * value.
    551 	 */
    552 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_BHLC_REG);
    553 	if (PCI_LATTIMER(reg) < 0x20) {
    554 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    555 		reg |= (0x20 << PCI_LATTIMER_SHIFT);
    556 		cardbus_conf_write(cc, cf, csc->sc_tag, PCI_BHLC_REG, reg);
    557 	}
    558 }
    559 
    560 void
    561 tlp_cardbus_x3201_reset(sc)
    562 	struct tulip_softc *sc;
    563 {
    564 	u_int32_t reg;
    565 
    566 	reg = TULIP_READ(sc, CSR_SIAGEN);
    567 
    568 	/* make GP[2,0] outputs */
    569 	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
    570 	    0x00050000);
    571 	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
    572 }
    573