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