Home | History | Annotate | Line # | Download | only in cardbus
if_tlp_cardbus.c revision 1.32
      1  1.32  augustss /*	$NetBSD: if_tlp_cardbus.c,v 1.32 2002/02/17 21:21:58 augustss Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4  1.10   thorpej  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1   thorpej  * NASA Ames Research Center.
     10   1.1   thorpej  *
     11   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.1   thorpej  * modification, are permitted provided that the following conditions
     13   1.1   thorpej  * are met:
     14   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     20   1.1   thorpej  *    must display the following acknowledgement:
     21   1.1   thorpej  *	This product includes software developed by the NetBSD
     22   1.1   thorpej  *	Foundation, Inc. and its contributors.
     23   1.1   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.1   thorpej  *    contributors may be used to endorse or promote products derived
     25   1.1   thorpej  *    from this software without specific prior written permission.
     26   1.1   thorpej  *
     27   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1   thorpej  */
     39   1.1   thorpej 
     40   1.1   thorpej /*
     41   1.1   thorpej  * CardBus bus front-end for the Digital Semiconductor ``Tulip'' (21x4x)
     42   1.1   thorpej  * Ethernet controller family driver.
     43   1.1   thorpej  */
     44  1.31     lukem 
     45  1.31     lukem #include <sys/cdefs.h>
     46  1.32  augustss __KERNEL_RCSID(0, "$NetBSD: if_tlp_cardbus.c,v 1.32 2002/02/17 21:21:58 augustss Exp $");
     47   1.1   thorpej 
     48   1.1   thorpej #include "opt_inet.h"
     49   1.1   thorpej #include "opt_ns.h"
     50   1.1   thorpej #include "bpfilter.h"
     51   1.1   thorpej 
     52   1.1   thorpej #include <sys/param.h>
     53   1.1   thorpej #include <sys/systm.h>
     54   1.1   thorpej #include <sys/mbuf.h>
     55   1.1   thorpej #include <sys/malloc.h>
     56   1.1   thorpej #include <sys/kernel.h>
     57   1.1   thorpej #include <sys/socket.h>
     58   1.1   thorpej #include <sys/ioctl.h>
     59   1.1   thorpej #include <sys/errno.h>
     60   1.1   thorpej #include <sys/device.h>
     61   1.5   thorpej 
     62   1.5   thorpej #include <machine/endian.h>
     63   1.1   thorpej 
     64   1.1   thorpej #include <net/if.h>
     65   1.1   thorpej #include <net/if_dl.h>
     66   1.1   thorpej #include <net/if_media.h>
     67   1.1   thorpej #include <net/if_ether.h>
     68   1.1   thorpej 
     69   1.1   thorpej #if NBPFILTER > 0
     70   1.1   thorpej #include <net/bpf.h>
     71   1.1   thorpej #endif
     72   1.1   thorpej 
     73   1.1   thorpej #ifdef INET
     74   1.1   thorpej #include <netinet/in.h>
     75   1.1   thorpej #include <netinet/if_inarp.h>
     76   1.1   thorpej #endif
     77   1.1   thorpej 
     78   1.1   thorpej #ifdef NS
     79   1.1   thorpej #include <netns/ns.h>
     80   1.1   thorpej #include <netns/ns_if.h>
     81   1.1   thorpej #endif
     82   1.1   thorpej 
     83   1.1   thorpej #include <machine/bus.h>
     84   1.1   thorpej #include <machine/intr.h>
     85   1.1   thorpej 
     86   1.1   thorpej #include <dev/mii/miivar.h>
     87   1.1   thorpej #include <dev/mii/mii_bitbang.h>
     88   1.1   thorpej 
     89   1.1   thorpej #include <dev/ic/tulipreg.h>
     90   1.1   thorpej #include <dev/ic/tulipvar.h>
     91   1.1   thorpej 
     92   1.1   thorpej #include <dev/pci/pcivar.h>
     93   1.1   thorpej #include <dev/pci/pcireg.h>
     94   1.1   thorpej #include <dev/pci/pcidevs.h>
     95   1.1   thorpej 
     96   1.1   thorpej #include <dev/cardbus/cardbusvar.h>
     97  1.25   thorpej #include <dev/cardbus/cardbusdevs.h>
     98   1.1   thorpej 
     99   1.1   thorpej /*
    100   1.1   thorpej  * PCI configuration space registers used by the Tulip.
    101   1.1   thorpej  */
    102   1.1   thorpej #define	TULIP_PCI_IOBA		0x10	/* i/o mapped base */
    103   1.1   thorpej #define	TULIP_PCI_MMBA		0x14	/* memory mapped base */
    104   1.1   thorpej #define	TULIP_PCI_CFDA		0x40	/* configuration driver area */
    105   1.1   thorpej 
    106   1.1   thorpej #define	CFDA_SLEEP		0x80000000	/* sleep mode */
    107   1.4   thorpej #define	CFDA_SNOOZE		0x40000000	/* snooze mode */
    108   1.1   thorpej 
    109   1.1   thorpej struct tulip_cardbus_softc {
    110   1.1   thorpej 	struct tulip_softc sc_tulip;	/* real Tulip softc */
    111   1.1   thorpej 
    112   1.1   thorpej 	/* CardBus-specific goo. */
    113   1.1   thorpej 	void	*sc_ih;			/* interrupt handle */
    114   1.1   thorpej 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
    115  1.17   thorpej 	cardbustag_t sc_tag;		/* our CardBus tag */
    116   1.1   thorpej 	int	sc_csr;			/* CSR bits */
    117  1.17   thorpej 	bus_size_t sc_mapsize;		/* the size of mapped bus space
    118  1.17   thorpej 					   region */
    119  1.18   thorpej 
    120  1.18   thorpej 	int	sc_cben;		/* CardBus enables */
    121  1.18   thorpej 	int	sc_bar_reg;		/* which BAR to use */
    122  1.18   thorpej 	pcireg_t sc_bar_val;		/* value of the BAR */
    123  1.20   thorpej 
    124  1.20   thorpej 	int	sc_intrline;		/* interrupt line */
    125   1.1   thorpej };
    126   1.1   thorpej 
    127   1.1   thorpej int	tlp_cardbus_match __P((struct device *, struct cfdata *, void *));
    128   1.1   thorpej void	tlp_cardbus_attach __P((struct device *, struct device *, void *));
    129  1.11   thorpej int	tlp_cardbus_detach __P((struct device *, int));
    130   1.1   thorpej 
    131   1.1   thorpej struct cfattach tlp_cardbus_ca = {
    132   1.1   thorpej 	sizeof(struct tulip_cardbus_softc),
    133   1.1   thorpej 	    tlp_cardbus_match, tlp_cardbus_attach,
    134  1.11   thorpej 		tlp_cardbus_detach, tlp_activate,
    135   1.1   thorpej };
    136   1.1   thorpej 
    137   1.1   thorpej const struct tulip_cardbus_product {
    138   1.1   thorpej 	u_int32_t	tcp_vendor;	/* PCI vendor ID */
    139   1.1   thorpej 	u_int32_t	tcp_product;	/* PCI product ID */
    140   1.1   thorpej 	tulip_chip_t	tcp_chip;	/* base Tulip chip type */
    141   1.1   thorpej } tlp_cardbus_products[] = {
    142   1.1   thorpej 	{ PCI_VENDOR_DEC,		PCI_PRODUCT_DEC_21142,
    143  1.23   thorpej 	  TULIP_CHIP_21142 },
    144   1.1   thorpej 
    145  1.10   thorpej 	{ PCI_VENDOR_XIRCOM,		PCI_PRODUCT_XIRCOM_X3201_3_21143,
    146  1.23   thorpej 	  TULIP_CHIP_X3201_3 },
    147  1.27   kanaoka 
    148  1.27   kanaoka 	{ PCI_VENDOR_ADMTEK,		PCI_PRODUCT_ADMTEK_AN985,
    149  1.27   kanaoka 	  TULIP_CHIP_AN985 },
    150  1.10   thorpej 
    151  1.25   thorpej 	{ CARDBUS_VENDOR_ACCTON,	CARDBUS_PRODUCT_ACCTON_EN2242,
    152  1.25   thorpej 	  TULIP_CHIP_AN985 },
    153  1.25   thorpej 
    154  1.25   thorpej 	{ CARDBUS_VENDOR_ABOCOM,	CARDBUS_PRODUCT_ABOCOM_FE2500,
    155  1.26  christos 	  TULIP_CHIP_AN985 },
    156  1.26  christos 
    157  1.26  christos 	{ CARDBUS_VENDOR_ABOCOM,	CARDBUS_PRODUCT_ABOCOM_PCM200,
    158  1.32  augustss 	  TULIP_CHIP_AN985 },
    159  1.32  augustss 
    160  1.32  augustss 	{ CARDBUS_VENDOR_HAWKING,	CARDBUS_PRODUCT_HAWKING_PN672TX,
    161  1.25   thorpej 	  TULIP_CHIP_AN985 },
    162  1.25   thorpej 
    163   1.1   thorpej 	{ 0,				0,
    164  1.23   thorpej 	  TULIP_CHIP_INVALID },
    165   1.1   thorpej };
    166   1.1   thorpej 
    167  1.18   thorpej void	tlp_cardbus_setup __P((struct tulip_cardbus_softc *));
    168  1.17   thorpej 
    169  1.20   thorpej int	tlp_cardbus_enable __P((struct tulip_softc *));
    170  1.20   thorpej void	tlp_cardbus_disable __P((struct tulip_softc *));
    171  1.21   thorpej void	tlp_cardbus_power __P((struct tulip_softc *, int));
    172  1.20   thorpej 
    173  1.10   thorpej void	tlp_cardbus_x3201_reset __P((struct tulip_softc *));
    174  1.10   thorpej 
    175   1.1   thorpej const struct tulip_cardbus_product *tlp_cardbus_lookup
    176   1.1   thorpej     __P((const struct cardbus_attach_args *));
    177   1.1   thorpej 
    178   1.1   thorpej const struct tulip_cardbus_product *
    179   1.1   thorpej tlp_cardbus_lookup(ca)
    180   1.1   thorpej 	const struct cardbus_attach_args *ca;
    181   1.1   thorpej {
    182   1.1   thorpej 	const struct tulip_cardbus_product *tcp;
    183   1.1   thorpej 
    184   1.1   thorpej 	for (tcp = tlp_cardbus_products;
    185   1.9   thorpej 	     tlp_chip_names[tcp->tcp_chip] != NULL;
    186   1.1   thorpej 	     tcp++) {
    187   1.1   thorpej 		if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
    188   1.1   thorpej 		    PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
    189   1.1   thorpej 			return (tcp);
    190   1.1   thorpej 	}
    191   1.1   thorpej 	return (NULL);
    192   1.1   thorpej }
    193   1.1   thorpej 
    194   1.1   thorpej int
    195   1.1   thorpej tlp_cardbus_match(parent, match, aux)
    196   1.1   thorpej 	struct device *parent;
    197   1.1   thorpej 	struct cfdata *match;
    198   1.1   thorpej 	void *aux;
    199   1.1   thorpej {
    200   1.1   thorpej 	struct cardbus_attach_args *ca = aux;
    201   1.1   thorpej 
    202   1.1   thorpej 	if (tlp_cardbus_lookup(ca) != NULL)
    203   1.1   thorpej 		return (1);
    204   1.1   thorpej 
    205   1.1   thorpej 	return (0);
    206   1.1   thorpej }
    207   1.1   thorpej 
    208   1.1   thorpej void
    209   1.1   thorpej tlp_cardbus_attach(parent, self, aux)
    210   1.1   thorpej 	struct device *parent, *self;
    211   1.1   thorpej 	void *aux;
    212   1.1   thorpej {
    213  1.15   mycroft 	struct tulip_cardbus_softc *csc = (void *)self;
    214   1.1   thorpej 	struct tulip_softc *sc = &csc->sc_tulip;
    215   1.1   thorpej 	struct cardbus_attach_args *ca = aux;
    216   1.1   thorpej 	cardbus_devfunc_t ct = ca->ca_ct;
    217   1.1   thorpej 	const struct tulip_cardbus_product *tcp;
    218   1.1   thorpej 	u_int8_t enaddr[ETHER_ADDR_LEN];
    219  1.15   mycroft 	bus_addr_t adr;
    220  1.25   thorpej 	pcireg_t reg;
    221   1.1   thorpej 
    222   1.1   thorpej 	sc->sc_devno = ca->ca_device;
    223   1.1   thorpej 	sc->sc_dmat = ca->ca_dmat;
    224   1.1   thorpej 	csc->sc_ct = ct;
    225  1.17   thorpej 	csc->sc_tag = ca->ca_tag;
    226   1.1   thorpej 
    227   1.1   thorpej 	tcp = tlp_cardbus_lookup(ca);
    228   1.1   thorpej 	if (tcp == NULL) {
    229   1.1   thorpej 		printf("\n");
    230   1.1   thorpej 		panic("tlp_cardbus_attach: impossible");
    231   1.1   thorpej 	}
    232   1.1   thorpej 	sc->sc_chip = tcp->tcp_chip;
    233   1.1   thorpej 
    234   1.1   thorpej 	/*
    235   1.1   thorpej 	 * By default, Tulip registers are 8 bytes long (4 bytes
    236   1.1   thorpej 	 * followed by a 4 byte pad).
    237   1.1   thorpej 	 */
    238   1.1   thorpej 	sc->sc_regshift = 3;
    239   1.1   thorpej 
    240   1.1   thorpej 	/*
    241  1.20   thorpej 	 * Power management hooks.
    242  1.20   thorpej 	 */
    243  1.20   thorpej 	sc->sc_enable = tlp_cardbus_enable;
    244  1.20   thorpej 	sc->sc_disable = tlp_cardbus_disable;
    245  1.21   thorpej 	sc->sc_power = tlp_cardbus_power;
    246  1.20   thorpej 
    247  1.20   thorpej 	/*
    248   1.1   thorpej 	 * Get revision info, and set some chip-specific variables.
    249   1.1   thorpej 	 */
    250   1.1   thorpej 	sc->sc_rev = PCI_REVISION(ca->ca_class);
    251   1.1   thorpej 	switch (sc->sc_chip) {
    252   1.1   thorpej 	case TULIP_CHIP_21142:
    253   1.1   thorpej 		if (sc->sc_rev >= 0x20)
    254   1.1   thorpej 			sc->sc_chip = TULIP_CHIP_21143;
    255   1.1   thorpej 		break;
    256   1.1   thorpej 
    257  1.25   thorpej 	case TULIP_CHIP_AN985:
    258  1.25   thorpej 		/*
    259  1.25   thorpej 		 * The AN983 and AN985 are very similar, and are
    260  1.25   thorpej 		 * differentiated by a "signature" register that
    261  1.25   thorpej 		 * is like, but not identical, to a PCI ID register.
    262  1.25   thorpej 		 */
    263  1.25   thorpej 		reg = cardbus_conf_read(ct->ct_cc, ct->ct_cf, csc->sc_tag,
    264  1.25   thorpej 		    0x80);
    265  1.25   thorpej 		switch (reg) {
    266  1.25   thorpej 		case 0x09811317:
    267  1.25   thorpej 			sc->sc_chip = TULIP_CHIP_AN985;
    268  1.25   thorpej 			break;
    269  1.25   thorpej 
    270  1.25   thorpej 		case 0x09851317:
    271  1.25   thorpej 			sc->sc_chip = TULIP_CHIP_AN983;
    272  1.25   thorpej 			break;
    273  1.25   thorpej 
    274  1.25   thorpej 		}
    275  1.25   thorpej 		break;
    276  1.25   thorpej 
    277  1.29      onoe 	default:
    278  1.29      onoe 		/* Nothing. -- to make gcc happy */
    279  1.30   thorpej 		break;
    280   1.1   thorpej 	}
    281   1.1   thorpej 
    282   1.1   thorpej 	printf(": %s Ethernet, pass %d.%d\n",
    283   1.9   thorpej 	    tlp_chip_names[sc->sc_chip],
    284   1.1   thorpej 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    285   1.2   thorpej 
    286   1.2   thorpej 	/*
    287   1.1   thorpej 	 * Map the device.
    288   1.1   thorpej 	 */
    289   1.1   thorpej 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
    290  1.19   thorpej 	if (Cardbus_mapreg_map(ct, TULIP_PCI_IOBA,
    291  1.19   thorpej 	    PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
    292  1.19   thorpej 	    &csc->sc_mapsize) == 0) {
    293  1.19   thorpej #if rbus
    294  1.19   thorpej #else
    295  1.19   thorpej 		(*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
    296  1.19   thorpej #endif
    297  1.19   thorpej 		csc->sc_cben = CARDBUS_IO_ENABLE;
    298  1.19   thorpej 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
    299  1.19   thorpej 		csc->sc_bar_reg = TULIP_PCI_IOBA;
    300  1.19   thorpej 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
    301  1.19   thorpej 	} else if (Cardbus_mapreg_map(ct, TULIP_PCI_MMBA,
    302   1.1   thorpej 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    303  1.15   mycroft 	    &sc->sc_st, &sc->sc_sh, &adr, &csc->sc_mapsize) == 0) {
    304  1.15   mycroft #if rbus
    305  1.15   mycroft #else
    306  1.15   mycroft 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    307  1.15   mycroft #endif
    308  1.18   thorpej 		csc->sc_cben = CARDBUS_MEM_ENABLE;
    309   1.1   thorpej 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
    310  1.18   thorpej 		csc->sc_bar_reg = TULIP_PCI_MMBA;
    311  1.18   thorpej 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
    312   1.1   thorpej 	} else {
    313   1.1   thorpej 		printf("%s: unable to map device registers\n",
    314   1.1   thorpej 		    sc->sc_dev.dv_xname);
    315   1.1   thorpej 		return;
    316   1.1   thorpej 	}
    317   1.1   thorpej 
    318   1.7   thorpej 	/*
    319  1.18   thorpej 	 * Bring the chip out of powersave mode and initialize the
    320  1.18   thorpej 	 * configuration registers.
    321   1.7   thorpej 	 */
    322  1.18   thorpej 	tlp_cardbus_setup(csc);
    323   1.1   thorpej 
    324   1.1   thorpej 	/*
    325  1.10   thorpej 	 * Read the contents of the Ethernet Address ROM/SROM.
    326   1.1   thorpej 	 */
    327  1.10   thorpej 	switch (sc->sc_chip) {
    328  1.10   thorpej 	case TULIP_CHIP_X3201_3:
    329  1.10   thorpej 		/*
    330  1.10   thorpej 		 * No SROM on this chip.
    331  1.10   thorpej 		 */
    332  1.10   thorpej 		break;
    333  1.10   thorpej 
    334  1.10   thorpej 	default:
    335  1.15   mycroft 		if (tlp_read_srom(sc) == 0)
    336  1.15   mycroft 			goto cant_cope;
    337  1.15   mycroft 		break;
    338   1.1   thorpej 	}
    339   1.1   thorpej 
    340   1.1   thorpej 	/*
    341   1.1   thorpej 	 * Deal with chip/board quirks.  This includes setting up
    342   1.1   thorpej 	 * the mediasw, and extracting the Ethernet address from
    343   1.1   thorpej 	 * the rombuf.
    344   1.1   thorpej 	 */
    345   1.1   thorpej 	switch (sc->sc_chip) {
    346   1.1   thorpej 	case TULIP_CHIP_21142:
    347   1.1   thorpej 	case TULIP_CHIP_21143:
    348   1.1   thorpej 		/* Check for new format SROM. */
    349  1.24   thorpej 		if (tlp_isv_srom_enaddr(sc, enaddr) != 0) {
    350   1.1   thorpej 			/*
    351   1.1   thorpej 			 * We start out with the 2114x ISV media switch.
    352   1.1   thorpej 			 * When we search for quirks, we may change to
    353   1.1   thorpej 			 * a different switch.
    354   1.1   thorpej 			 */
    355   1.1   thorpej 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    356  1.24   thorpej 		} else if (tlp_parse_old_srom(sc, enaddr) == 0) {
    357  1.23   thorpej 			/*
    358  1.24   thorpej 			 * Not an ISV SROM, and not in old DEC Address
    359  1.24   thorpej 			 * ROM format.  Try to snarf it out of the CIS.
    360  1.23   thorpej 			 */
    361  1.23   thorpej 			if (ca->ca_cis.funce.network.netid_present == 0)
    362  1.23   thorpej 				goto cant_cope;
    363  1.23   thorpej 
    364  1.23   thorpej 			/* Grab the MAC address from the CIS. */
    365  1.23   thorpej 			memcpy(enaddr, ca->ca_cis.funce.network.netid,
    366  1.23   thorpej 			    sizeof(enaddr));
    367  1.24   thorpej 		}
    368  1.23   thorpej 
    369  1.24   thorpej 		/* XXX XXX XXX QUIRKS XXX XXX XXX */
    370  1.24   thorpej 
    371  1.24   thorpej 		/*
    372  1.24   thorpej 		 * If we don't already have a media switch, default to
    373  1.24   thorpej 		 * MII-over-SIO, with no special reset routine.
    374  1.24   thorpej 		 */
    375  1.24   thorpej 		if (sc->sc_mediasw == NULL) {
    376  1.24   thorpej 			printf("%s: defaulting to MII-over-SIO; no bets...\n",
    377  1.24   thorpej 			    sc->sc_dev.dv_xname);
    378  1.23   thorpej 			sc->sc_mediasw = &tlp_sio_mii_mediasw;
    379  1.23   thorpej 		}
    380  1.25   thorpej 		break;
    381  1.25   thorpej 
    382  1.25   thorpej 	case TULIP_CHIP_AN983:
    383  1.25   thorpej 	case TULIP_CHIP_AN985:
    384  1.25   thorpej 		/*
    385  1.25   thorpej 		 * The ADMtek AN985's Ethernet address is located
    386  1.25   thorpej 		 * at offset 8 of its EEPROM.
    387  1.25   thorpej 		 */
    388  1.25   thorpej 		memcpy(enaddr, &sc->sc_srom[8], ETHER_ADDR_LEN);
    389  1.25   thorpej 
    390  1.25   thorpej 		/*
    391  1.25   thorpej 		 * The ADMtek AN985 can be configured in Single-Chip
    392  1.25   thorpej 		 * mode or MAC-only mode.  Single-Chip uses the built-in
    393  1.25   thorpej 		 * PHY, MAC-only has an external PHY (usually HomePNA).
    394  1.25   thorpej 		 * The selection is based on an EEPROM setting, and both
    395  1.25   thorpej 		 * PHYs are access via MII attached to SIO.
    396  1.25   thorpej 		 *
    397  1.25   thorpej 		 * The AN985 "ghosts" the internal PHY onto all
    398  1.25   thorpej 		 * MII addresses, so we have to use a media init
    399  1.25   thorpej 		 * routine that limits the search.
    400  1.25   thorpej 		 * XXX How does this work with MAC-only mode?
    401  1.25   thorpej 		 */
    402  1.25   thorpej 		sc->sc_mediasw = &tlp_an985_mediasw;
    403   1.1   thorpej 		break;
    404   1.1   thorpej 
    405  1.10   thorpej 	case TULIP_CHIP_X3201_3:
    406   1.1   thorpej 		/*
    407  1.15   mycroft 		 * The X3201 doesn't have an SROM.  Lift the MAC address
    408  1.10   thorpej 		 * from the CIS.  Also, we have a special media switch:
    409  1.10   thorpej 		 * MII-on-SIO, plus some special GPIO setup.
    410   1.1   thorpej 		 */
    411  1.10   thorpej 		memcpy(enaddr, ca->ca_cis.funce.network.netid, sizeof(enaddr));
    412  1.10   thorpej 		sc->sc_reset = tlp_cardbus_x3201_reset;
    413  1.10   thorpej 		sc->sc_mediasw = &tlp_sio_mii_mediasw;
    414  1.10   thorpej 		break;
    415   1.1   thorpej 
    416  1.10   thorpej 	default:
    417  1.10   thorpej  cant_cope:
    418   1.1   thorpej 		printf("%s: sorry, unable to handle your board\n",
    419   1.1   thorpej 		    sc->sc_dev.dv_xname);
    420   1.1   thorpej 		return;
    421   1.1   thorpej 	}
    422   1.1   thorpej 
    423  1.20   thorpej 	/* Remember which interrupt line. */
    424  1.20   thorpej 	csc->sc_intrline = ca->ca_intrline;
    425  1.20   thorpej 
    426   1.1   thorpej 	/*
    427  1.23   thorpej 	 * The CardBus cards will make it to store-and-forward mode as
    428  1.23   thorpej 	 * soon as you put them under any kind of load, so just start
    429  1.23   thorpej 	 * out there.
    430  1.23   thorpej 	 */
    431  1.23   thorpej 	sc->sc_txthresh = TXTH_SF;
    432  1.23   thorpej 
    433  1.23   thorpej 	/*
    434  1.20   thorpej 	 * Finish off the attach.
    435   1.1   thorpej 	 */
    436  1.20   thorpej 	tlp_attach(sc, enaddr);
    437   1.1   thorpej 
    438   1.1   thorpej 	/*
    439  1.20   thorpej 	 * Power down the socket.
    440   1.1   thorpej 	 */
    441  1.20   thorpej 	Cardbus_function_disable(csc->sc_ct);
    442  1.11   thorpej }
    443  1.11   thorpej 
    444  1.11   thorpej int
    445  1.11   thorpej tlp_cardbus_detach(self, flags)
    446  1.11   thorpej 	struct device *self;
    447  1.11   thorpej 	int flags;
    448  1.11   thorpej {
    449  1.15   mycroft 	struct tulip_cardbus_softc *csc = (void *)self;
    450  1.11   thorpej 	struct tulip_softc *sc = &csc->sc_tulip;
    451  1.13  augustss 	struct cardbus_devfunc *ct = csc->sc_ct;
    452  1.11   thorpej 	int rv;
    453  1.12      haya 
    454  1.13  augustss #if defined(DIAGNOSTIC)
    455  1.20   thorpej 	if (ct == NULL)
    456  1.12      haya 		panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
    457  1.12      haya #endif
    458  1.11   thorpej 
    459  1.20   thorpej 	rv = tlp_detach(sc);
    460  1.20   thorpej 	if (rv)
    461  1.20   thorpej 		return (rv);
    462  1.15   mycroft 
    463  1.15   mycroft 	/*
    464  1.15   mycroft 	 * Unhook the interrupt handler.
    465  1.15   mycroft 	 */
    466  1.20   thorpej 	if (csc->sc_ih != NULL)
    467  1.20   thorpej 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    468  1.12      haya 
    469  1.15   mycroft 	/*
    470  1.20   thorpej 	 * Release bus space and close window.
    471  1.15   mycroft 	 */
    472  1.22   thorpej 	if (csc->sc_bar_reg != 0)
    473  1.22   thorpej 		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
    474  1.22   thorpej 		    sc->sc_st, sc->sc_sh, csc->sc_mapsize);
    475  1.12      haya 
    476  1.15   mycroft 	return (0);
    477  1.20   thorpej }
    478  1.20   thorpej 
    479  1.20   thorpej int
    480  1.20   thorpej tlp_cardbus_enable(sc)
    481  1.20   thorpej 	struct tulip_softc *sc;
    482  1.20   thorpej {
    483  1.20   thorpej 	struct tulip_cardbus_softc *csc = (void *) sc;
    484  1.20   thorpej 	cardbus_devfunc_t ct = csc->sc_ct;
    485  1.20   thorpej 	cardbus_chipset_tag_t cc = ct->ct_cc;
    486  1.20   thorpej 	cardbus_function_tag_t cf = ct->ct_cf;
    487  1.20   thorpej 
    488  1.20   thorpej 	/*
    489  1.20   thorpej 	 * Power on the socket.
    490  1.20   thorpej 	 */
    491  1.20   thorpej 	Cardbus_function_enable(ct);
    492  1.20   thorpej 
    493  1.20   thorpej 	/*
    494  1.20   thorpej 	 * Set up the PCI configuration registers.
    495  1.20   thorpej 	 */
    496  1.20   thorpej 	tlp_cardbus_setup(csc);
    497  1.20   thorpej 
    498  1.20   thorpej 	/*
    499  1.20   thorpej 	 * Map and establish the interrupt.
    500  1.20   thorpej 	 */
    501  1.20   thorpej 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
    502  1.20   thorpej 	    tlp_intr, sc);
    503  1.20   thorpej 	if (csc->sc_ih == NULL) {
    504  1.20   thorpej 		printf("%s: unable to establish interrupt at %d\n",
    505  1.20   thorpej 		    sc->sc_dev.dv_xname, csc->sc_intrline);
    506  1.20   thorpej 		Cardbus_function_disable(csc->sc_ct);
    507  1.20   thorpej 		return (1);
    508  1.20   thorpej 	}
    509  1.20   thorpej 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    510  1.20   thorpej 	    csc->sc_intrline);
    511  1.20   thorpej 
    512  1.20   thorpej 	return (0);
    513  1.20   thorpej }
    514  1.20   thorpej 
    515  1.20   thorpej void
    516  1.20   thorpej tlp_cardbus_disable(sc)
    517  1.20   thorpej 	struct tulip_softc *sc;
    518  1.20   thorpej {
    519  1.20   thorpej 	struct tulip_cardbus_softc *csc = (void *) sc;
    520  1.20   thorpej 	cardbus_devfunc_t ct = csc->sc_ct;
    521  1.20   thorpej 	cardbus_chipset_tag_t cc = ct->ct_cc;
    522  1.20   thorpej 	cardbus_function_tag_t cf = ct->ct_cf;
    523  1.20   thorpej 
    524  1.20   thorpej 	/* Unhook the interrupt handler. */
    525  1.20   thorpej 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    526  1.20   thorpej 	csc->sc_ih = NULL;
    527  1.20   thorpej 
    528  1.20   thorpej 	/* Power down the socket. */
    529  1.20   thorpej 	Cardbus_function_disable(ct);
    530  1.21   thorpej }
    531  1.21   thorpej 
    532  1.21   thorpej void
    533  1.21   thorpej tlp_cardbus_power(sc, why)
    534  1.21   thorpej 	struct tulip_softc *sc;
    535  1.21   thorpej 	int why;
    536  1.21   thorpej {
    537  1.21   thorpej 	struct tulip_cardbus_softc *csc = (void *) sc;
    538  1.21   thorpej 
    539  1.21   thorpej 	if (why == PWR_RESUME) {
    540  1.21   thorpej 		/*
    541  1.21   thorpej 		 * Give the PCI configuration registers a kick
    542  1.21   thorpej 		 * in the head.
    543  1.21   thorpej 		 */
    544  1.21   thorpej #ifdef DIAGNOSTIC
    545  1.21   thorpej 		if (TULIP_IS_ENABLED(sc) == 0)
    546  1.21   thorpej 			panic("tlp_cardbus_power");
    547  1.21   thorpej #endif
    548  1.21   thorpej 		tlp_cardbus_setup(csc);
    549  1.21   thorpej 	}
    550  1.17   thorpej }
    551  1.17   thorpej 
    552  1.17   thorpej void
    553  1.18   thorpej tlp_cardbus_setup(csc)
    554  1.17   thorpej 	struct tulip_cardbus_softc *csc;
    555  1.17   thorpej {
    556  1.17   thorpej 	struct tulip_softc *sc = &csc->sc_tulip;
    557  1.17   thorpej 	cardbus_devfunc_t ct = csc->sc_ct;
    558  1.17   thorpej 	cardbus_chipset_tag_t cc = ct->ct_cc;
    559  1.17   thorpej 	cardbus_function_tag_t cf = ct->ct_cf;
    560  1.17   thorpej 	pcireg_t reg;
    561  1.23   thorpej 	int pmreg;
    562  1.17   thorpej 
    563  1.17   thorpej 	/*
    564  1.17   thorpej 	 * Check to see if the device is in power-save mode, and
    565  1.17   thorpej 	 * bring it out if necessary.
    566  1.17   thorpej 	 */
    567  1.17   thorpej 	switch (sc->sc_chip) {
    568  1.17   thorpej 	case TULIP_CHIP_21142:
    569  1.17   thorpej 	case TULIP_CHIP_21143:
    570  1.17   thorpej 	case TULIP_CHIP_X3201_3:
    571  1.17   thorpej 		/*
    572  1.17   thorpej 		 * Clear the "sleep mode" bit in the CFDA register.
    573  1.17   thorpej 		 */
    574  1.17   thorpej 		reg = cardbus_conf_read(cc, cf, csc->sc_tag, TULIP_PCI_CFDA);
    575  1.17   thorpej 		if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
    576  1.17   thorpej 			cardbus_conf_write(cc, cf, csc->sc_tag, TULIP_PCI_CFDA,
    577  1.17   thorpej 			    reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
    578  1.17   thorpej 		break;
    579  1.29      onoe 
    580  1.29      onoe 	default:
    581  1.29      onoe 		/* Nothing. -- to make gcc happy */
    582  1.30   thorpej 		break;
    583  1.17   thorpej 	}
    584  1.17   thorpej 
    585  1.17   thorpej 	if (cardbus_get_capability(cc, cf, csc->sc_tag,
    586  1.23   thorpej 	    PCI_CAP_PWRMGMT, &pmreg, 0)) {
    587  1.23   thorpej 		reg = cardbus_conf_read(cc, cf, csc->sc_tag, pmreg + 4) & 0x03;
    588  1.17   thorpej #if 1 /* XXX Probably not right for CardBus. */
    589  1.17   thorpej 		if (reg == 3) {
    590  1.17   thorpej 			/*
    591  1.17   thorpej 			 * The card has lost all configuration data in
    592  1.17   thorpej 			 * this state, so punt.
    593  1.17   thorpej 			 */
    594  1.17   thorpej 			printf("%s: unable to wake up from power state D3\n",
    595  1.17   thorpej 			    sc->sc_dev.dv_xname);
    596  1.17   thorpej 			return;
    597  1.17   thorpej 		}
    598  1.17   thorpej #endif
    599  1.17   thorpej 		if (reg != 0) {
    600  1.17   thorpej 			printf("%s: waking up from power state D%d\n",
    601  1.17   thorpej 			    sc->sc_dev.dv_xname, reg);
    602  1.17   thorpej 			cardbus_conf_write(cc, cf, csc->sc_tag,
    603  1.23   thorpej 			    pmreg + 4, 0);
    604  1.17   thorpej 		}
    605  1.18   thorpej 	}
    606  1.18   thorpej 
    607  1.18   thorpej 	/* Make sure the right access type is on the CardBus bridge. */
    608  1.18   thorpej 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
    609  1.18   thorpej 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    610  1.18   thorpej 
    611  1.18   thorpej 	/* Program the BAR. */
    612  1.18   thorpej 	cardbus_conf_write(cc, cf, csc->sc_tag, csc->sc_bar_reg,
    613  1.18   thorpej 	    csc->sc_bar_val);
    614  1.18   thorpej 
    615  1.18   thorpej 	/* Enable the appropriate bits in the PCI CSR. */
    616  1.18   thorpej 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG);
    617  1.18   thorpej 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    618  1.18   thorpej 	reg |= csc->sc_csr;
    619  1.18   thorpej 	cardbus_conf_write(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
    620  1.18   thorpej 
    621  1.18   thorpej 	/*
    622  1.18   thorpej 	 * Make sure the latency timer is set to some reasonable
    623  1.18   thorpej 	 * value.
    624  1.18   thorpej 	 */
    625  1.18   thorpej 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, PCI_BHLC_REG);
    626  1.18   thorpej 	if (PCI_LATTIMER(reg) < 0x20) {
    627  1.18   thorpej 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    628  1.18   thorpej 		reg |= (0x20 << PCI_LATTIMER_SHIFT);
    629  1.18   thorpej 		cardbus_conf_write(cc, cf, csc->sc_tag, PCI_BHLC_REG, reg);
    630  1.17   thorpej 	}
    631  1.10   thorpej }
    632  1.10   thorpej 
    633  1.10   thorpej void
    634  1.10   thorpej tlp_cardbus_x3201_reset(sc)
    635  1.10   thorpej 	struct tulip_softc *sc;
    636  1.10   thorpej {
    637  1.10   thorpej 	u_int32_t reg;
    638  1.10   thorpej 
    639  1.10   thorpej 	reg = TULIP_READ(sc, CSR_SIAGEN);
    640  1.10   thorpej 
    641  1.10   thorpej 	/* make GP[2,0] outputs */
    642  1.10   thorpej 	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
    643  1.10   thorpej 	    0x00050000);
    644  1.10   thorpej 	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
    645   1.1   thorpej }
    646