Home | History | Annotate | Line # | Download | only in pci
if_atw_pci.c revision 1.1
      1  1.1  dyoung /*	$NetBSD: if_atw_pci.c,v 1.1 2003/07/06 22:58:10 dyoung Exp $	*/
      2  1.1  dyoung 
      3  1.1  dyoung /*-
      4  1.1  dyoung  * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
      5  1.1  dyoung  * All rights reserved.
      6  1.1  dyoung  *
      7  1.1  dyoung  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  dyoung  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  dyoung  * NASA Ames Research Center; Charles M. Hannum; and David Young.
     10  1.1  dyoung  *
     11  1.1  dyoung  * Redistribution and use in source and binary forms, with or without
     12  1.1  dyoung  * modification, are permitted provided that the following conditions
     13  1.1  dyoung  * are met:
     14  1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
     15  1.1  dyoung  *    notice, this list of conditions and the following disclaimer.
     16  1.1  dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  dyoung  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  dyoung  *    documentation and/or other materials provided with the distribution.
     19  1.1  dyoung  * 3. All advertising materials mentioning features or use of this software
     20  1.1  dyoung  *    must display the following acknowledgement:
     21  1.1  dyoung  *	This product includes software developed by the NetBSD
     22  1.1  dyoung  *	Foundation, Inc. and its contributors.
     23  1.1  dyoung  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  dyoung  *    contributors may be used to endorse or promote products derived
     25  1.1  dyoung  *    from this software without specific prior written permission.
     26  1.1  dyoung  *
     27  1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  dyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  dyoung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  dyoung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  dyoung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  dyoung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  dyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  dyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  dyoung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  dyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  dyoung  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  dyoung  */
     39  1.1  dyoung 
     40  1.1  dyoung /*
     41  1.1  dyoung  * PCI bus front-end for the ADMtek ADM8211 802.11 MAC/BBP chip.
     42  1.1  dyoung  *
     43  1.1  dyoung  * Derived from the ``Tulip'' PCI bus front-end.
     44  1.1  dyoung  */
     45  1.1  dyoung 
     46  1.1  dyoung #include <sys/cdefs.h>
     47  1.1  dyoung __KERNEL_RCSID(0, "$NetBSD: if_atw_pci.c,v 1.1 2003/07/06 22:58:10 dyoung Exp $");
     48  1.1  dyoung 
     49  1.1  dyoung #include <sys/param.h>
     50  1.1  dyoung #include <sys/systm.h>
     51  1.1  dyoung #include <sys/mbuf.h>
     52  1.1  dyoung #include <sys/malloc.h>
     53  1.1  dyoung #include <sys/kernel.h>
     54  1.1  dyoung #include <sys/socket.h>
     55  1.1  dyoung #include <sys/ioctl.h>
     56  1.1  dyoung #include <sys/errno.h>
     57  1.1  dyoung #include <sys/device.h>
     58  1.1  dyoung 
     59  1.1  dyoung #include <machine/endian.h>
     60  1.1  dyoung 
     61  1.1  dyoung #include <net/if.h>
     62  1.1  dyoung #include <net/if_dl.h>
     63  1.1  dyoung #include <net/if_media.h>
     64  1.1  dyoung #include <net/if_ether.h>
     65  1.1  dyoung #include <net/if_ieee80211.h>
     66  1.1  dyoung 
     67  1.1  dyoung #include <machine/bus.h>
     68  1.1  dyoung #include <machine/intr.h>
     69  1.1  dyoung 
     70  1.1  dyoung #include <dev/ic/atwreg.h>
     71  1.1  dyoung #include <dev/ic/atwvar.h>
     72  1.1  dyoung 
     73  1.1  dyoung #include <dev/pci/pcivar.h>
     74  1.1  dyoung #include <dev/pci/pcireg.h>
     75  1.1  dyoung #include <dev/pci/pcidevs.h>
     76  1.1  dyoung 
     77  1.1  dyoung /*
     78  1.1  dyoung  * PCI configuration space registers used by the ADM8211.
     79  1.1  dyoung  */
     80  1.1  dyoung #define	ATW_PCI_IOBA		0x10	/* i/o mapped base */
     81  1.1  dyoung #define	ATW_PCI_MMBA		0x14	/* memory mapped base */
     82  1.1  dyoung 
     83  1.1  dyoung struct atw_pci_softc {
     84  1.1  dyoung 	struct atw_softc sc_atw;	/* real ADM8211 softc */
     85  1.1  dyoung 
     86  1.1  dyoung 	/* PCI-specific goo. */
     87  1.1  dyoung 	void	*sc_ih;			/* interrupt handle */
     88  1.1  dyoung 
     89  1.1  dyoung 	pci_chipset_tag_t sc_pc;	/* our PCI chipset */
     90  1.1  dyoung 	pcitag_t sc_pcitag;		/* our PCI tag */
     91  1.1  dyoung };
     92  1.1  dyoung 
     93  1.1  dyoung int	atw_pci_match __P((struct device *, struct cfdata *, void *));
     94  1.1  dyoung void	atw_pci_attach __P((struct device *, struct device *, void *));
     95  1.1  dyoung 
     96  1.1  dyoung CFATTACH_DECL(atw_pci, sizeof(struct atw_pci_softc),
     97  1.1  dyoung     atw_pci_match, atw_pci_attach, NULL, NULL);
     98  1.1  dyoung 
     99  1.1  dyoung const struct atw_pci_product {
    100  1.1  dyoung 	u_int32_t	app_vendor;	/* PCI vendor ID */
    101  1.1  dyoung 	u_int32_t	app_product;	/* PCI product ID */
    102  1.1  dyoung 	const char	*app_product_name;
    103  1.1  dyoung } atw_pci_products[] = {
    104  1.1  dyoung 	{ PCI_VENDOR_ADMTEK,		PCI_PRODUCT_ADMTEK_ADM8211,
    105  1.1  dyoung 	  "ADMtek ADM8211 802.11 MAC/BBP" },
    106  1.1  dyoung 
    107  1.1  dyoung 	{ 0,				0,				NULL },
    108  1.1  dyoung };
    109  1.1  dyoung 
    110  1.1  dyoung const struct atw_pci_product *atw_pci_lookup
    111  1.1  dyoung     __P((const struct pci_attach_args *));
    112  1.1  dyoung 
    113  1.1  dyoung const struct atw_pci_product *
    114  1.1  dyoung atw_pci_lookup(pa)
    115  1.1  dyoung 	const struct pci_attach_args *pa;
    116  1.1  dyoung {
    117  1.1  dyoung 	const struct atw_pci_product *app;
    118  1.1  dyoung 
    119  1.1  dyoung 	for (app = atw_pci_products;
    120  1.1  dyoung 	     app->app_product_name != NULL;
    121  1.1  dyoung 	     app++) {
    122  1.1  dyoung 		if (PCI_VENDOR(pa->pa_id) == app->app_vendor &&
    123  1.1  dyoung 		    PCI_PRODUCT(pa->pa_id) == app->app_product)
    124  1.1  dyoung 			return (app);
    125  1.1  dyoung 	}
    126  1.1  dyoung 	return (NULL);
    127  1.1  dyoung }
    128  1.1  dyoung 
    129  1.1  dyoung int
    130  1.1  dyoung atw_pci_match(parent, match, aux)
    131  1.1  dyoung 	struct device *parent;
    132  1.1  dyoung 	struct cfdata *match;
    133  1.1  dyoung 	void *aux;
    134  1.1  dyoung {
    135  1.1  dyoung 	struct pci_attach_args *pa = aux;
    136  1.1  dyoung 
    137  1.1  dyoung 	if (atw_pci_lookup(pa) != NULL)
    138  1.1  dyoung 		return (1);	/* beat if_de.c */
    139  1.1  dyoung 
    140  1.1  dyoung 	return (0);
    141  1.1  dyoung }
    142  1.1  dyoung 
    143  1.1  dyoung void
    144  1.1  dyoung atw_pci_attach(parent, self, aux)
    145  1.1  dyoung 	struct device *parent, *self;
    146  1.1  dyoung 	void *aux;
    147  1.1  dyoung {
    148  1.1  dyoung 	struct atw_pci_softc *psc = (void *) self;
    149  1.1  dyoung 	struct atw_softc *sc = &psc->sc_atw;
    150  1.1  dyoung 	struct pci_attach_args *pa = aux;
    151  1.1  dyoung 	pci_chipset_tag_t pc = pa->pa_pc;
    152  1.1  dyoung 	pci_intr_handle_t ih;
    153  1.1  dyoung 	const char *intrstr = NULL;
    154  1.1  dyoung 	bus_space_tag_t iot, memt;
    155  1.1  dyoung 	bus_space_handle_t ioh, memh;
    156  1.1  dyoung 	int ioh_valid, memh_valid;
    157  1.1  dyoung 	const struct atw_pci_product *app;
    158  1.1  dyoung 	pcireg_t reg;
    159  1.1  dyoung 	int pmreg, rev;
    160  1.1  dyoung 
    161  1.1  dyoung 	psc->sc_pc = pa->pa_pc;
    162  1.1  dyoung 	psc->sc_pcitag = pa->pa_tag;
    163  1.1  dyoung 
    164  1.1  dyoung 	app = atw_pci_lookup(pa);
    165  1.1  dyoung 	if (app == NULL) {
    166  1.1  dyoung 		printf("\n");
    167  1.1  dyoung 		panic("atw_pci_attach: impossible");
    168  1.1  dyoung 	}
    169  1.1  dyoung 
    170  1.1  dyoung 	/*
    171  1.1  dyoung 	 * No power management hooks.
    172  1.1  dyoung 	 * XXX Maybe we should add some!
    173  1.1  dyoung 	 */
    174  1.1  dyoung 	sc->sc_flags |= ATWF_ENABLED;
    175  1.1  dyoung 
    176  1.1  dyoung 	/*
    177  1.1  dyoung 	 * Get revision info, and set some chip-specific variables.
    178  1.1  dyoung 	 */
    179  1.1  dyoung 	rev = PCI_REVISION(pa->pa_class);
    180  1.1  dyoung 	printf(": %s, pass %d.%d\n", app->app_product_name,
    181  1.1  dyoung 	    (rev >> 4) & 0xf, rev & 0xf);
    182  1.1  dyoung 
    183  1.1  dyoung 	/*
    184  1.1  dyoung 	 * Check to see if the device is in power-save mode, and
    185  1.1  dyoung 	 * being it out if necessary.
    186  1.1  dyoung 	 *
    187  1.1  dyoung 	 * XXX This code comes almost verbatim from if_tlp_pci.c. I do
    188  1.1  dyoung 	 * not understand it. Tulip clears the "sleep mode" bit in the
    189  1.1  dyoung 	 * CFDA register, first.  There is an equivalent (?) register at the
    190  1.1  dyoung 	 * same place in the ADM8211, but the docs do not assign its bits
    191  1.1  dyoung 	 * any meanings. -dcy
    192  1.1  dyoung 	 */
    193  1.1  dyoung 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    194  1.1  dyoung 		reg = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR);
    195  1.1  dyoung 		switch (reg & PCI_PMCSR_STATE_MASK) {
    196  1.1  dyoung 		case PCI_PMCSR_STATE_D1:
    197  1.1  dyoung 		case PCI_PMCSR_STATE_D2:
    198  1.1  dyoung 			printf(": waking up from power state D%d\n%s",
    199  1.1  dyoung 			    reg & PCI_PMCSR_STATE_MASK, sc->sc_dev.dv_xname);
    200  1.1  dyoung 			pci_conf_write(pc, pa->pa_tag, pmreg + PCI_PMCSR,
    201  1.1  dyoung 			    (reg & ~PCI_PMCSR_STATE_MASK) |
    202  1.1  dyoung 			    PCI_PMCSR_STATE_D0);
    203  1.1  dyoung 			break;
    204  1.1  dyoung 		case PCI_PMCSR_STATE_D3:
    205  1.1  dyoung 			/*
    206  1.1  dyoung 			 * The card has lost all configuration data in
    207  1.1  dyoung 			 * this state, so punt.
    208  1.1  dyoung 			 */
    209  1.1  dyoung 			printf(": unable to wake up from power state D3, "
    210  1.1  dyoung 			       "reboot required.\n");
    211  1.1  dyoung 			pci_conf_write(pc, pa->pa_tag, pmreg + PCI_PMCSR,
    212  1.1  dyoung 			    (reg & ~PCI_PMCSR_STATE_MASK) |
    213  1.1  dyoung 			    PCI_PMCSR_STATE_D0);
    214  1.1  dyoung 			return;
    215  1.1  dyoung 		}
    216  1.1  dyoung 	}
    217  1.1  dyoung 
    218  1.1  dyoung 	/*
    219  1.1  dyoung 	 * Map the device.
    220  1.1  dyoung 	 */
    221  1.1  dyoung 	ioh_valid = (pci_mapreg_map(pa, ATW_PCI_IOBA,
    222  1.1  dyoung 	    PCI_MAPREG_TYPE_IO, 0,
    223  1.1  dyoung 	    &iot, &ioh, NULL, NULL) == 0);
    224  1.1  dyoung 	memh_valid = (pci_mapreg_map(pa, ATW_PCI_MMBA,
    225  1.1  dyoung 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    226  1.1  dyoung 	    &memt, &memh, NULL, NULL) == 0);
    227  1.1  dyoung 
    228  1.1  dyoung 	if (memh_valid) {
    229  1.1  dyoung 		sc->sc_st = memt;
    230  1.1  dyoung 		sc->sc_sh = memh;
    231  1.1  dyoung 	} else if (ioh_valid) {
    232  1.1  dyoung 		sc->sc_st = iot;
    233  1.1  dyoung 		sc->sc_sh = ioh;
    234  1.1  dyoung 	} else {
    235  1.1  dyoung 		printf(": unable to map device registers\n");
    236  1.1  dyoung 		return;
    237  1.1  dyoung 	}
    238  1.1  dyoung 
    239  1.1  dyoung 	sc->sc_dmat = pa->pa_dmat;
    240  1.1  dyoung 
    241  1.1  dyoung 	/*
    242  1.1  dyoung 	 * Make sure bus mastering is enabled.
    243  1.1  dyoung 	 */
    244  1.1  dyoung 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    245  1.1  dyoung 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    246  1.1  dyoung 	    PCI_COMMAND_MASTER_ENABLE);
    247  1.1  dyoung 
    248  1.1  dyoung 	/*
    249  1.1  dyoung 	 * Get the cacheline size.
    250  1.1  dyoung 	 */
    251  1.1  dyoung 	sc->sc_cacheline = PCI_CACHELINE(pci_conf_read(pc, pa->pa_tag,
    252  1.1  dyoung 	    PCI_BHLC_REG));
    253  1.1  dyoung 
    254  1.1  dyoung 	/*
    255  1.1  dyoung 	 * Get PCI data moving command info.
    256  1.1  dyoung 	 */
    257  1.1  dyoung 	if (pa->pa_flags & PCI_FLAGS_MRL_OKAY) /* read line */
    258  1.1  dyoung 		sc->sc_flags |= ATWF_MRL;
    259  1.1  dyoung 	if (pa->pa_flags & PCI_FLAGS_MRM_OKAY) /* read multiple */
    260  1.1  dyoung 		sc->sc_flags |= ATWF_MRM;
    261  1.1  dyoung 	if (pa->pa_flags & PCI_FLAGS_MWI_OKAY) /* write invalidate */
    262  1.1  dyoung 		sc->sc_flags |= ATWF_MWI;
    263  1.1  dyoung 
    264  1.1  dyoung 	/*
    265  1.1  dyoung 	 * Map and establish our interrupt.
    266  1.1  dyoung 	 */
    267  1.1  dyoung 	if (pci_intr_map(pa, &ih)) {
    268  1.1  dyoung 		printf("%s: unable to map interrupt\n",
    269  1.1  dyoung 		    sc->sc_dev.dv_xname);
    270  1.1  dyoung 		return;
    271  1.1  dyoung 	}
    272  1.1  dyoung 	intrstr = pci_intr_string(pc, ih);
    273  1.1  dyoung 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, atw_intr, sc);
    274  1.1  dyoung 	if (psc->sc_ih == NULL) {
    275  1.1  dyoung 		printf("%s: unable to establish interrupt",
    276  1.1  dyoung 		    sc->sc_dev.dv_xname);
    277  1.1  dyoung 		if (intrstr != NULL)
    278  1.1  dyoung 			printf(" at %s", intrstr);
    279  1.1  dyoung 		printf("\n");
    280  1.1  dyoung 		return;
    281  1.1  dyoung 	}
    282  1.1  dyoung 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    283  1.1  dyoung 
    284  1.1  dyoung 	/*
    285  1.1  dyoung 	 * Finish off the attach.
    286  1.1  dyoung 	 */
    287  1.1  dyoung 	atw_attach(sc);
    288  1.1  dyoung }
    289  1.1  dyoung 
    290