Home | History | Annotate | Line # | Download | only in cardbus
      1  1.72   thorpej /*	$NetBSD: if_tlp_cardbus.c,v 1.72 2022/09/25 17:33:19 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  *
     20   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1   thorpej  */
     32   1.1   thorpej 
     33   1.1   thorpej /*
     34   1.1   thorpej  * CardBus bus front-end for the Digital Semiconductor ``Tulip'' (21x4x)
     35   1.1   thorpej  * Ethernet controller family driver.
     36   1.1   thorpej  */
     37  1.31     lukem 
     38  1.31     lukem #include <sys/cdefs.h>
     39  1.72   thorpej __KERNEL_RCSID(0, "$NetBSD: if_tlp_cardbus.c,v 1.72 2022/09/25 17:33:19 thorpej Exp $");
     40   1.1   thorpej 
     41   1.1   thorpej #include "opt_inet.h"
     42   1.1   thorpej 
     43   1.1   thorpej #include <sys/param.h>
     44  1.45     perry #include <sys/systm.h>
     45  1.45     perry #include <sys/mbuf.h>
     46   1.1   thorpej #include <sys/kernel.h>
     47   1.1   thorpej #include <sys/socket.h>
     48   1.1   thorpej #include <sys/ioctl.h>
     49   1.1   thorpej #include <sys/errno.h>
     50   1.1   thorpej #include <sys/device.h>
     51   1.5   thorpej 
     52   1.5   thorpej #include <machine/endian.h>
     53  1.45     perry 
     54   1.1   thorpej #include <net/if.h>
     55   1.1   thorpej #include <net/if_dl.h>
     56   1.1   thorpej #include <net/if_media.h>
     57   1.1   thorpej #include <net/if_ether.h>
     58   1.1   thorpej 
     59   1.1   thorpej #ifdef INET
     60  1.45     perry #include <netinet/in.h>
     61   1.1   thorpej #include <netinet/if_inarp.h>
     62   1.1   thorpej #endif
     63   1.1   thorpej 
     64   1.1   thorpej 
     65  1.54        ad #include <sys/bus.h>
     66  1.54        ad #include <sys/intr.h>
     67   1.1   thorpej 
     68   1.1   thorpej #include <dev/mii/miivar.h>
     69   1.1   thorpej #include <dev/mii/mii_bitbang.h>
     70   1.1   thorpej 
     71   1.1   thorpej #include <dev/ic/tulipreg.h>
     72   1.1   thorpej #include <dev/ic/tulipvar.h>
     73   1.1   thorpej 
     74   1.1   thorpej #include <dev/pci/pcivar.h>
     75   1.1   thorpej #include <dev/pci/pcireg.h>
     76   1.1   thorpej #include <dev/pci/pcidevs.h>
     77   1.1   thorpej 
     78   1.1   thorpej #include <dev/cardbus/cardbusvar.h>
     79  1.43   mycroft #include <dev/pci/pcidevs.h>
     80   1.1   thorpej 
     81   1.1   thorpej /*
     82   1.1   thorpej  * PCI configuration space registers used by the Tulip.
     83   1.1   thorpej  */
     84  1.69    dyoung #define TULIP_PCI_IOBA PCI_BAR(0)	/* i/o mapped base */
     85  1.69    dyoung #define TULIP_PCI_MMBA PCI_BAR(1)	/* memory mapped base */
     86   1.1   thorpej #define	TULIP_PCI_CFDA		0x40	/* configuration driver area */
     87   1.1   thorpej 
     88   1.1   thorpej #define	CFDA_SLEEP		0x80000000	/* sleep mode */
     89   1.4   thorpej #define	CFDA_SNOOZE		0x40000000	/* snooze mode */
     90   1.1   thorpej 
     91   1.1   thorpej struct tulip_cardbus_softc {
     92   1.1   thorpej 	struct tulip_softc sc_tulip;	/* real Tulip softc */
     93   1.1   thorpej 
     94   1.1   thorpej 	/* CardBus-specific goo. */
     95   1.1   thorpej 	void	*sc_ih;			/* interrupt handle */
     96   1.1   thorpej 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
     97  1.63    dyoung 	pcitag_t sc_tag;		/* our CardBus tag */
     98  1.65    dyoung 	pcireg_t sc_csr;
     99  1.17   thorpej 	bus_size_t sc_mapsize;		/* the size of mapped bus space
    100  1.17   thorpej 					   region */
    101  1.18   thorpej 
    102  1.18   thorpej 	int	sc_bar_reg;		/* which BAR to use */
    103  1.18   thorpej 	pcireg_t sc_bar_val;		/* value of the BAR */
    104   1.1   thorpej };
    105   1.1   thorpej 
    106  1.61    cegger int	tlp_cardbus_match(device_t, cfdata_t, void *);
    107  1.61    cegger void	tlp_cardbus_attach(device_t, device_t, void *);
    108  1.61    cegger int	tlp_cardbus_detach(device_t, int);
    109   1.1   thorpej 
    110  1.61    cegger CFATTACH_DECL_NEW(tlp_cardbus, sizeof(struct tulip_cardbus_softc),
    111  1.37   thorpej     tlp_cardbus_match, tlp_cardbus_attach, tlp_cardbus_detach, tlp_activate);
    112   1.1   thorpej 
    113   1.1   thorpej const struct tulip_cardbus_product {
    114   1.1   thorpej 	u_int32_t	tcp_vendor;	/* PCI vendor ID */
    115   1.1   thorpej 	u_int32_t	tcp_product;	/* PCI product ID */
    116   1.1   thorpej 	tulip_chip_t	tcp_chip;	/* base Tulip chip type */
    117   1.1   thorpej } tlp_cardbus_products[] = {
    118  1.43   mycroft 	{ PCI_VENDOR_DEC,	PCI_PRODUCT_DEC_21142,
    119  1.23   thorpej 	  TULIP_CHIP_21142 },
    120   1.1   thorpej 
    121  1.43   mycroft 	{ PCI_VENDOR_XIRCOM,	PCI_PRODUCT_XIRCOM_X3201_3_21143,
    122  1.23   thorpej 	  TULIP_CHIP_X3201_3 },
    123  1.27   kanaoka 
    124  1.50   gdamore 	{ PCI_VENDOR_ADMTEK,	PCI_PRODUCT_ADMTEK_AN983,
    125  1.43   mycroft 	  TULIP_CHIP_AN985 },
    126  1.43   mycroft 
    127  1.43   mycroft 	{ PCI_VENDOR_ACCTON,	PCI_PRODUCT_ACCTON_EN2242,
    128  1.27   kanaoka 	  TULIP_CHIP_AN985 },
    129  1.10   thorpej 
    130  1.43   mycroft 	{ PCI_VENDOR_ABOCOM,	PCI_PRODUCT_ABOCOM_FE2500,
    131  1.25   thorpej 	  TULIP_CHIP_AN985 },
    132  1.25   thorpej 
    133  1.43   mycroft 	{ PCI_VENDOR_ABOCOM,	PCI_PRODUCT_ABOCOM_PCM200,
    134  1.26  christos 	  TULIP_CHIP_AN985 },
    135  1.26  christos 
    136  1.43   mycroft 	{ PCI_VENDOR_ABOCOM,	PCI_PRODUCT_ABOCOM_FE2500MX,
    137  1.38      onoe 	  TULIP_CHIP_AN985 },
    138  1.38      onoe 
    139  1.43   mycroft 	{ PCI_VENDOR_HAWKING,	PCI_PRODUCT_HAWKING_PN672TX,
    140  1.32  augustss 	  TULIP_CHIP_AN985 },
    141  1.32  augustss 
    142  1.50   gdamore 	{ PCI_VENDOR_ADMTEK,	PCI_PRODUCT_ADMTEK_AN985,
    143  1.39   mycroft 	  TULIP_CHIP_AN985 },
    144  1.39   mycroft 
    145  1.43   mycroft 	{ PCI_VENDOR_MICROSOFT,	PCI_PRODUCT_MICROSOFT_MN120,
    146  1.25   thorpej 	  TULIP_CHIP_AN985 },
    147  1.25   thorpej 
    148  1.46      tron 	{ PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_PCMPC200,
    149  1.46      tron 	  TULIP_CHIP_AN985 },
    150  1.46      tron 
    151   1.1   thorpej 	{ 0,				0,
    152  1.23   thorpej 	  TULIP_CHIP_INVALID },
    153   1.1   thorpej };
    154   1.1   thorpej 
    155  1.33   mycroft struct tlp_cardbus_quirks {
    156  1.44     perry 	void		(*tpq_func)(struct tulip_cardbus_softc *,
    157  1.44     perry 			    const u_int8_t *);
    158  1.33   mycroft 	u_int8_t	tpq_oui[3];
    159  1.33   mycroft };
    160  1.33   mycroft 
    161  1.44     perry void	tlp_cardbus_lxt_quirks(struct tulip_cardbus_softc *,
    162  1.44     perry 	    const u_int8_t *);
    163  1.33   mycroft 
    164  1.33   mycroft const struct tlp_cardbus_quirks tlp_cardbus_21142_quirks[] = {
    165  1.33   mycroft 	{ tlp_cardbus_lxt_quirks,	{ 0x00, 0x40, 0x05 } },
    166  1.33   mycroft 	{ NULL,				{ 0, 0, 0 } }
    167  1.33   mycroft };
    168  1.33   mycroft 
    169  1.44     perry void	tlp_cardbus_setup(struct tulip_cardbus_softc *);
    170  1.17   thorpej 
    171  1.44     perry int	tlp_cardbus_enable(struct tulip_softc *);
    172  1.44     perry void	tlp_cardbus_disable(struct tulip_softc *);
    173  1.44     perry void	tlp_cardbus_power(struct tulip_softc *, int);
    174  1.20   thorpej 
    175  1.44     perry void	tlp_cardbus_x3201_reset(struct tulip_softc *);
    176  1.10   thorpej 
    177   1.1   thorpej const struct tulip_cardbus_product *tlp_cardbus_lookup
    178  1.44     perry    (const struct cardbus_attach_args *);
    179  1.44     perry void tlp_cardbus_get_quirks(struct tulip_cardbus_softc *,
    180  1.44     perry     const u_int8_t *, const struct tlp_cardbus_quirks *);
    181   1.1   thorpej 
    182   1.1   thorpej const struct tulip_cardbus_product *
    183  1.60       dsl tlp_cardbus_lookup(const struct cardbus_attach_args *ca)
    184   1.1   thorpej {
    185   1.1   thorpej 	const struct tulip_cardbus_product *tcp;
    186   1.1   thorpej 
    187  1.68  christos 	for (tcp = tlp_cardbus_products; tcp->tcp_chip != TULIP_CHIP_INVALID;
    188   1.1   thorpej 	     tcp++) {
    189   1.1   thorpej 		if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
    190   1.1   thorpej 		    PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
    191   1.1   thorpej 			return (tcp);
    192   1.1   thorpej 	}
    193   1.1   thorpej 	return (NULL);
    194   1.1   thorpej }
    195   1.1   thorpej 
    196  1.33   mycroft void
    197  1.71   msaitoh tlp_cardbus_get_quirks(struct tulip_cardbus_softc *csc, const u_int8_t *enaddr,
    198  1.71   msaitoh     const struct tlp_cardbus_quirks *tpq)
    199  1.33   mycroft {
    200  1.33   mycroft 
    201  1.33   mycroft 	for (; tpq->tpq_func != NULL; tpq++) {
    202  1.33   mycroft 		if (tpq->tpq_oui[0] == enaddr[0] &&
    203  1.33   mycroft 		    tpq->tpq_oui[1] == enaddr[1] &&
    204  1.33   mycroft 		    tpq->tpq_oui[2] == enaddr[2]) {
    205  1.33   mycroft 			(*tpq->tpq_func)(csc, enaddr);
    206  1.33   mycroft 			return;
    207  1.33   mycroft 		}
    208  1.33   mycroft 	}
    209  1.33   mycroft }
    210  1.33   mycroft 
    211   1.1   thorpej int
    212  1.71   msaitoh tlp_cardbus_match(device_t parent, cfdata_t match, void *aux)
    213   1.1   thorpej {
    214   1.1   thorpej 	struct cardbus_attach_args *ca = aux;
    215   1.1   thorpej 
    216   1.1   thorpej 	if (tlp_cardbus_lookup(ca) != NULL)
    217   1.1   thorpej 		return (1);
    218   1.1   thorpej 
    219   1.1   thorpej 	return (0);
    220   1.1   thorpej }
    221   1.1   thorpej 
    222   1.1   thorpej void
    223  1.71   msaitoh tlp_cardbus_attach(device_t parent, device_t self, void *aux)
    224   1.1   thorpej {
    225  1.49   thorpej 	struct tulip_cardbus_softc *csc = device_private(self);
    226   1.1   thorpej 	struct tulip_softc *sc = &csc->sc_tulip;
    227   1.1   thorpej 	struct cardbus_attach_args *ca = aux;
    228   1.1   thorpej 	cardbus_devfunc_t ct = ca->ca_ct;
    229   1.1   thorpej 	const struct tulip_cardbus_product *tcp;
    230   1.1   thorpej 	u_int8_t enaddr[ETHER_ADDR_LEN];
    231  1.15   mycroft 	bus_addr_t adr;
    232  1.25   thorpej 	pcireg_t reg;
    233   1.1   thorpej 
    234  1.61    cegger 	sc->sc_dev = self;
    235  1.47  drochner 	sc->sc_devno = 0;
    236   1.1   thorpej 	sc->sc_dmat = ca->ca_dmat;
    237   1.1   thorpej 	csc->sc_ct = ct;
    238  1.17   thorpej 	csc->sc_tag = ca->ca_tag;
    239   1.1   thorpej 
    240   1.1   thorpej 	tcp = tlp_cardbus_lookup(ca);
    241   1.1   thorpej 	if (tcp == NULL) {
    242   1.1   thorpej 		printf("\n");
    243   1.1   thorpej 		panic("tlp_cardbus_attach: impossible");
    244   1.1   thorpej 	}
    245   1.1   thorpej 	sc->sc_chip = tcp->tcp_chip;
    246   1.1   thorpej 
    247   1.1   thorpej 	/*
    248   1.1   thorpej 	 * By default, Tulip registers are 8 bytes long (4 bytes
    249   1.1   thorpej 	 * followed by a 4 byte pad).
    250   1.1   thorpej 	 */
    251   1.1   thorpej 	sc->sc_regshift = 3;
    252   1.1   thorpej 
    253   1.1   thorpej 	/*
    254  1.20   thorpej 	 * Power management hooks.
    255  1.20   thorpej 	 */
    256  1.20   thorpej 	sc->sc_enable = tlp_cardbus_enable;
    257  1.20   thorpej 	sc->sc_disable = tlp_cardbus_disable;
    258  1.21   thorpej 	sc->sc_power = tlp_cardbus_power;
    259  1.20   thorpej 
    260  1.20   thorpej 	/*
    261   1.1   thorpej 	 * Get revision info, and set some chip-specific variables.
    262   1.1   thorpej 	 */
    263   1.1   thorpej 	sc->sc_rev = PCI_REVISION(ca->ca_class);
    264   1.1   thorpej 	switch (sc->sc_chip) {
    265   1.1   thorpej 	case TULIP_CHIP_21142:
    266   1.1   thorpej 		if (sc->sc_rev >= 0x20)
    267   1.1   thorpej 			sc->sc_chip = TULIP_CHIP_21143;
    268   1.1   thorpej 		break;
    269   1.1   thorpej 
    270  1.25   thorpej 	case TULIP_CHIP_AN985:
    271  1.25   thorpej 		/*
    272  1.25   thorpej 		 * The AN983 and AN985 are very similar, and are
    273  1.25   thorpej 		 * differentiated by a "signature" register that
    274  1.25   thorpej 		 * is like, but not identical, to a PCI ID register.
    275  1.25   thorpej 		 */
    276  1.66    dyoung 		reg = Cardbus_conf_read(ct, csc->sc_tag, 0x80);
    277  1.25   thorpej 		switch (reg) {
    278  1.25   thorpej 		case 0x09811317:
    279  1.25   thorpej 			sc->sc_chip = TULIP_CHIP_AN985;
    280  1.25   thorpej 			break;
    281  1.25   thorpej 
    282  1.25   thorpej 		case 0x09851317:
    283  1.25   thorpej 			sc->sc_chip = TULIP_CHIP_AN983;
    284  1.25   thorpej 			break;
    285  1.25   thorpej 
    286  1.25   thorpej 		}
    287  1.25   thorpej 		break;
    288  1.25   thorpej 
    289  1.29      onoe 	default:
    290  1.29      onoe 		/* Nothing. -- to make gcc happy */
    291  1.30   thorpej 		break;
    292   1.1   thorpej 	}
    293   1.1   thorpej 
    294  1.71   msaitoh 	aprint_normal(": %s Ethernet, pass %d.%d\n",
    295  1.68  christos 	    tlp_chip_name(sc->sc_chip),
    296   1.1   thorpej 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    297   1.2   thorpej 
    298   1.2   thorpej 	/*
    299   1.1   thorpej 	 * Map the device.
    300   1.1   thorpej 	 */
    301  1.66    dyoung 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
    302  1.33   mycroft 	if (Cardbus_mapreg_map(ct, TULIP_PCI_MMBA,
    303  1.66    dyoung 	    PCI_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &adr,
    304  1.33   mycroft 	    &csc->sc_mapsize) == 0) {
    305  1.66    dyoung 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
    306  1.33   mycroft 		csc->sc_bar_reg = TULIP_PCI_MMBA;
    307  1.66    dyoung 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
    308  1.33   mycroft 	} else if (Cardbus_mapreg_map(ct, TULIP_PCI_IOBA,
    309  1.66    dyoung 	    PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
    310  1.19   thorpej 	    &csc->sc_mapsize) == 0) {
    311  1.66    dyoung 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
    312  1.19   thorpej 		csc->sc_bar_reg = TULIP_PCI_IOBA;
    313  1.66    dyoung 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
    314   1.1   thorpej 	} else {
    315  1.61    cegger 		aprint_error_dev(self, "unable to map device registers\n");
    316   1.1   thorpej 		return;
    317   1.1   thorpej 	}
    318   1.1   thorpej 
    319   1.7   thorpej 	/*
    320  1.18   thorpej 	 * Bring the chip out of powersave mode and initialize the
    321  1.18   thorpej 	 * configuration registers.
    322   1.7   thorpej 	 */
    323  1.18   thorpej 	tlp_cardbus_setup(csc);
    324   1.1   thorpej 
    325   1.1   thorpej 	/*
    326  1.10   thorpej 	 * Read the contents of the Ethernet Address ROM/SROM.
    327   1.1   thorpej 	 */
    328  1.10   thorpej 	switch (sc->sc_chip) {
    329  1.10   thorpej 	case TULIP_CHIP_X3201_3:
    330  1.10   thorpej 		/*
    331  1.10   thorpej 		 * No SROM on this chip.
    332  1.10   thorpej 		 */
    333  1.10   thorpej 		break;
    334  1.10   thorpej 
    335  1.10   thorpej 	default:
    336  1.15   mycroft 		if (tlp_read_srom(sc) == 0)
    337  1.15   mycroft 			goto cant_cope;
    338  1.15   mycroft 		break;
    339   1.1   thorpej 	}
    340   1.1   thorpej 
    341   1.1   thorpej 	/*
    342   1.1   thorpej 	 * Deal with chip/board quirks.  This includes setting up
    343   1.1   thorpej 	 * the mediasw, and extracting the Ethernet address from
    344   1.1   thorpej 	 * the rombuf.
    345   1.1   thorpej 	 */
    346   1.1   thorpej 	switch (sc->sc_chip) {
    347   1.1   thorpej 	case TULIP_CHIP_21142:
    348   1.1   thorpej 	case TULIP_CHIP_21143:
    349   1.1   thorpej 		/* Check for new format SROM. */
    350  1.24   thorpej 		if (tlp_isv_srom_enaddr(sc, enaddr) != 0) {
    351   1.1   thorpej 			/*
    352   1.1   thorpej 			 * We start out with the 2114x ISV media switch.
    353   1.1   thorpej 			 * When we search for quirks, we may change to
    354   1.1   thorpej 			 * a different switch.
    355   1.1   thorpej 			 */
    356   1.1   thorpej 			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
    357  1.24   thorpej 		} else if (tlp_parse_old_srom(sc, enaddr) == 0) {
    358  1.23   thorpej 			/*
    359  1.24   thorpej 			 * Not an ISV SROM, and not in old DEC Address
    360  1.24   thorpej 			 * ROM format.  Try to snarf it out of the CIS.
    361  1.23   thorpej 			 */
    362  1.23   thorpej 			if (ca->ca_cis.funce.network.netid_present == 0)
    363  1.23   thorpej 				goto cant_cope;
    364  1.23   thorpej 
    365  1.23   thorpej 			/* Grab the MAC address from the CIS. */
    366  1.23   thorpej 			memcpy(enaddr, ca->ca_cis.funce.network.netid,
    367  1.23   thorpej 			    sizeof(enaddr));
    368  1.24   thorpej 		}
    369  1.23   thorpej 
    370  1.33   mycroft 		/*
    371  1.33   mycroft 		 * Deal with any quirks this board might have.
    372  1.33   mycroft 		 */
    373  1.33   mycroft 		tlp_cardbus_get_quirks(csc, enaddr, tlp_cardbus_21142_quirks);
    374  1.24   thorpej 
    375  1.24   thorpej 		/*
    376  1.24   thorpej 		 * If we don't already have a media switch, default to
    377  1.24   thorpej 		 * MII-over-SIO, with no special reset routine.
    378  1.24   thorpej 		 */
    379  1.24   thorpej 		if (sc->sc_mediasw == NULL) {
    380  1.71   msaitoh 			aprint_normal("%s: defaulting to MII-over-SIO; "
    381  1.71   msaitoh 			    "no bets...\n", device_xname(self));
    382  1.23   thorpej 			sc->sc_mediasw = &tlp_sio_mii_mediasw;
    383  1.23   thorpej 		}
    384  1.25   thorpej 		break;
    385  1.25   thorpej 
    386  1.25   thorpej 	case TULIP_CHIP_AN983:
    387  1.25   thorpej 	case TULIP_CHIP_AN985:
    388  1.25   thorpej 		/*
    389  1.25   thorpej 		 * The ADMtek AN985's Ethernet address is located
    390  1.25   thorpej 		 * at offset 8 of its EEPROM.
    391  1.25   thorpej 		 */
    392  1.25   thorpej 		memcpy(enaddr, &sc->sc_srom[8], ETHER_ADDR_LEN);
    393  1.25   thorpej 
    394  1.25   thorpej 		/*
    395  1.25   thorpej 		 * The ADMtek AN985 can be configured in Single-Chip
    396  1.25   thorpej 		 * mode or MAC-only mode.  Single-Chip uses the built-in
    397  1.25   thorpej 		 * PHY, MAC-only has an external PHY (usually HomePNA).
    398  1.25   thorpej 		 * The selection is based on an EEPROM setting, and both
    399  1.25   thorpej 		 * PHYs are access via MII attached to SIO.
    400  1.25   thorpej 		 *
    401  1.25   thorpej 		 * The AN985 "ghosts" the internal PHY onto all
    402  1.25   thorpej 		 * MII addresses, so we have to use a media init
    403  1.25   thorpej 		 * routine that limits the search.
    404  1.25   thorpej 		 * XXX How does this work with MAC-only mode?
    405  1.25   thorpej 		 */
    406  1.25   thorpej 		sc->sc_mediasw = &tlp_an985_mediasw;
    407   1.1   thorpej 		break;
    408   1.1   thorpej 
    409  1.10   thorpej 	case TULIP_CHIP_X3201_3:
    410   1.1   thorpej 		/*
    411  1.15   mycroft 		 * The X3201 doesn't have an SROM.  Lift the MAC address
    412  1.10   thorpej 		 * from the CIS.  Also, we have a special media switch:
    413  1.10   thorpej 		 * MII-on-SIO, plus some special GPIO setup.
    414   1.1   thorpej 		 */
    415  1.10   thorpej 		memcpy(enaddr, ca->ca_cis.funce.network.netid, sizeof(enaddr));
    416  1.10   thorpej 		sc->sc_reset = tlp_cardbus_x3201_reset;
    417  1.10   thorpej 		sc->sc_mediasw = &tlp_sio_mii_mediasw;
    418  1.10   thorpej 		break;
    419   1.1   thorpej 
    420  1.10   thorpej 	default:
    421  1.10   thorpej  cant_cope:
    422  1.71   msaitoh 		aprint_error_dev(self, "sorry, unable to handle your board\n");
    423   1.1   thorpej 		return;
    424   1.1   thorpej 	}
    425   1.1   thorpej 
    426   1.1   thorpej 	/*
    427  1.20   thorpej 	 * Finish off the attach.
    428   1.1   thorpej 	 */
    429  1.20   thorpej 	tlp_attach(sc, enaddr);
    430   1.1   thorpej 
    431   1.1   thorpej 	/*
    432  1.20   thorpej 	 * Power down the socket.
    433   1.1   thorpej 	 */
    434  1.20   thorpej 	Cardbus_function_disable(csc->sc_ct);
    435  1.11   thorpej }
    436  1.11   thorpej 
    437  1.11   thorpej int
    438  1.61    cegger tlp_cardbus_detach(device_t self, int flags)
    439  1.11   thorpej {
    440  1.49   thorpej 	struct tulip_cardbus_softc *csc = device_private(self);
    441  1.11   thorpej 	struct tulip_softc *sc = &csc->sc_tulip;
    442  1.13  augustss 	struct cardbus_devfunc *ct = csc->sc_ct;
    443  1.11   thorpej 	int rv;
    444  1.12      haya 
    445  1.13  augustss #if defined(DIAGNOSTIC)
    446  1.20   thorpej 	if (ct == NULL)
    447  1.61    cegger 		panic("%s: data structure lacks", device_xname(self));
    448  1.12      haya #endif
    449  1.11   thorpej 
    450  1.20   thorpej 	rv = tlp_detach(sc);
    451  1.20   thorpej 	if (rv)
    452  1.20   thorpej 		return (rv);
    453  1.15   mycroft 
    454  1.15   mycroft 	/*
    455  1.15   mycroft 	 * Unhook the interrupt handler.
    456  1.15   mycroft 	 */
    457  1.20   thorpej 	if (csc->sc_ih != NULL)
    458  1.67    dyoung 		Cardbus_intr_disestablish(ct, csc->sc_ih);
    459  1.12      haya 
    460  1.15   mycroft 	/*
    461  1.20   thorpej 	 * Release bus space and close window.
    462  1.15   mycroft 	 */
    463  1.22   thorpej 	if (csc->sc_bar_reg != 0)
    464  1.22   thorpej 		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
    465  1.22   thorpej 		    sc->sc_st, sc->sc_sh, csc->sc_mapsize);
    466  1.12      haya 
    467  1.15   mycroft 	return (0);
    468  1.20   thorpej }
    469  1.20   thorpej 
    470  1.20   thorpej int
    471  1.60       dsl tlp_cardbus_enable(struct tulip_softc *sc)
    472  1.20   thorpej {
    473  1.20   thorpej 	struct tulip_cardbus_softc *csc = (void *) sc;
    474  1.20   thorpej 	cardbus_devfunc_t ct = csc->sc_ct;
    475  1.20   thorpej 
    476  1.20   thorpej 	/*
    477  1.20   thorpej 	 * Power on the socket.
    478  1.20   thorpej 	 */
    479  1.20   thorpej 	Cardbus_function_enable(ct);
    480  1.20   thorpej 
    481  1.20   thorpej 	/*
    482  1.20   thorpej 	 * Set up the PCI configuration registers.
    483  1.20   thorpej 	 */
    484  1.20   thorpej 	tlp_cardbus_setup(csc);
    485  1.20   thorpej 
    486  1.20   thorpej 	/*
    487  1.20   thorpej 	 * Map and establish the interrupt.
    488  1.20   thorpej 	 */
    489  1.70  drochner 	csc->sc_ih = Cardbus_intr_establish(ct, IPL_NET, tlp_intr, sc);
    490  1.20   thorpej 	if (csc->sc_ih == NULL) {
    491  1.61    cegger 		aprint_error_dev(sc->sc_dev,
    492  1.59  drochner 				 "unable to establish interrupt\n");
    493  1.20   thorpej 		Cardbus_function_disable(csc->sc_ct);
    494  1.20   thorpej 		return (1);
    495  1.20   thorpej 	}
    496  1.20   thorpej 	return (0);
    497  1.20   thorpej }
    498  1.20   thorpej 
    499  1.20   thorpej void
    500  1.60       dsl tlp_cardbus_disable(struct tulip_softc *sc)
    501  1.20   thorpej {
    502  1.20   thorpej 	struct tulip_cardbus_softc *csc = (void *) sc;
    503  1.20   thorpej 	cardbus_devfunc_t ct = csc->sc_ct;
    504  1.20   thorpej 
    505  1.20   thorpej 	/* Unhook the interrupt handler. */
    506  1.67    dyoung 	Cardbus_intr_disestablish(ct, csc->sc_ih);
    507  1.20   thorpej 	csc->sc_ih = NULL;
    508  1.20   thorpej 
    509  1.20   thorpej 	/* Power down the socket. */
    510  1.20   thorpej 	Cardbus_function_disable(ct);
    511  1.21   thorpej }
    512  1.21   thorpej 
    513  1.21   thorpej void
    514  1.60       dsl tlp_cardbus_power(struct tulip_softc *sc, int why)
    515  1.21   thorpej {
    516  1.21   thorpej 
    517  1.43   mycroft 	switch (why) {
    518  1.43   mycroft 	case PWR_RESUME:
    519  1.43   mycroft 		tlp_cardbus_enable(sc);
    520  1.43   mycroft 		break;
    521  1.43   mycroft 	case PWR_SUSPEND:
    522  1.43   mycroft 		tlp_cardbus_disable(sc);
    523  1.43   mycroft 		break;
    524  1.21   thorpej 	}
    525  1.17   thorpej }
    526  1.17   thorpej 
    527  1.17   thorpej void
    528  1.60       dsl tlp_cardbus_setup(struct tulip_cardbus_softc *csc)
    529  1.17   thorpej {
    530  1.17   thorpej 	struct tulip_softc *sc = &csc->sc_tulip;
    531  1.17   thorpej 	cardbus_devfunc_t ct = csc->sc_ct;
    532  1.17   thorpej 	pcireg_t reg;
    533  1.17   thorpej 
    534  1.17   thorpej 	/*
    535  1.17   thorpej 	 * Check to see if the device is in power-save mode, and
    536  1.17   thorpej 	 * bring it out if necessary.
    537  1.17   thorpej 	 */
    538  1.17   thorpej 	switch (sc->sc_chip) {
    539  1.17   thorpej 	case TULIP_CHIP_21142:
    540  1.17   thorpej 	case TULIP_CHIP_21143:
    541  1.17   thorpej 	case TULIP_CHIP_X3201_3:
    542  1.17   thorpej 		/*
    543  1.17   thorpej 		 * Clear the "sleep mode" bit in the CFDA register.
    544  1.17   thorpej 		 */
    545  1.66    dyoung 		reg = Cardbus_conf_read(ct, csc->sc_tag, TULIP_PCI_CFDA);
    546  1.17   thorpej 		if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
    547  1.66    dyoung 			Cardbus_conf_write(ct, csc->sc_tag, TULIP_PCI_CFDA,
    548  1.17   thorpej 			    reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
    549  1.17   thorpej 		break;
    550  1.29      onoe 
    551  1.29      onoe 	default:
    552  1.29      onoe 		/* Nothing. -- to make gcc happy */
    553  1.30   thorpej 		break;
    554  1.17   thorpej 	}
    555  1.17   thorpej 
    556  1.55  jmcneill 	(void)cardbus_set_powerstate(ct, csc->sc_tag, PCI_PWR_D0);
    557  1.18   thorpej 
    558  1.42   mycroft 	/* Program the BAR. */
    559  1.66    dyoung 	Cardbus_conf_write(ct, csc->sc_tag, csc->sc_bar_reg, csc->sc_bar_val);
    560  1.42   mycroft 
    561  1.18   thorpej 	/* Enable the appropriate bits in the PCI CSR. */
    562  1.66    dyoung 	reg = Cardbus_conf_read(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG);
    563  1.18   thorpej 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    564  1.18   thorpej 	reg |= csc->sc_csr;
    565  1.66    dyoung 	Cardbus_conf_write(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
    566  1.18   thorpej 
    567  1.18   thorpej 	/*
    568  1.18   thorpej 	 * Make sure the latency timer is set to some reasonable
    569  1.18   thorpej 	 * value.
    570  1.18   thorpej 	 */
    571  1.66    dyoung 	reg = Cardbus_conf_read(ct, csc->sc_tag, PCI_BHLC_REG);
    572  1.18   thorpej 	if (PCI_LATTIMER(reg) < 0x20) {
    573  1.18   thorpej 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    574  1.18   thorpej 		reg |= (0x20 << PCI_LATTIMER_SHIFT);
    575  1.66    dyoung 		Cardbus_conf_write(ct, csc->sc_tag, PCI_BHLC_REG, reg);
    576  1.17   thorpej 	}
    577  1.10   thorpej }
    578  1.10   thorpej 
    579  1.10   thorpej void
    580  1.60       dsl tlp_cardbus_x3201_reset(struct tulip_softc *sc)
    581  1.10   thorpej {
    582  1.10   thorpej 	u_int32_t reg;
    583  1.10   thorpej 
    584  1.10   thorpej 	reg = TULIP_READ(sc, CSR_SIAGEN);
    585  1.10   thorpej 
    586  1.10   thorpej 	/* make GP[2,0] outputs */
    587  1.10   thorpej 	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
    588  1.10   thorpej 	    0x00050000);
    589  1.10   thorpej 	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
    590  1.33   mycroft }
    591  1.33   mycroft 
    592  1.33   mycroft void
    593  1.71   msaitoh tlp_cardbus_lxt_quirks(struct tulip_cardbus_softc *csc, const u_int8_t *enaddr)
    594  1.33   mycroft {
    595  1.33   mycroft 	struct tulip_softc *sc = &csc->sc_tulip;
    596  1.33   mycroft 
    597  1.33   mycroft 	sc->sc_mediasw = &tlp_sio_mii_mediasw;
    598   1.1   thorpej }
    599