Home | History | Annotate | Line # | Download | only in pci
if_atw_pci.c revision 1.18.4.2
      1  1.18.4.2      yamt /*	$NetBSD: if_atw_pci.c,v 1.18.4.2 2009/05/04 08:12:56 yamt 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  *
     20       1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1    dyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1    dyoung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1    dyoung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1    dyoung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1    dyoung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1    dyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1    dyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1    dyoung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1    dyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1    dyoung  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1    dyoung  */
     32       1.1    dyoung 
     33       1.1    dyoung /*
     34       1.1    dyoung  * PCI bus front-end for the ADMtek ADM8211 802.11 MAC/BBP chip.
     35       1.1    dyoung  *
     36       1.1    dyoung  * Derived from the ``Tulip'' PCI bus front-end.
     37       1.1    dyoung  */
     38       1.1    dyoung 
     39       1.1    dyoung #include <sys/cdefs.h>
     40  1.18.4.2      yamt __KERNEL_RCSID(0, "$NetBSD: if_atw_pci.c,v 1.18.4.2 2009/05/04 08:12:56 yamt Exp $");
     41       1.1    dyoung 
     42       1.1    dyoung #include <sys/param.h>
     43       1.9     perry #include <sys/systm.h>
     44       1.9     perry #include <sys/mbuf.h>
     45       1.1    dyoung #include <sys/malloc.h>
     46       1.1    dyoung #include <sys/kernel.h>
     47       1.1    dyoung #include <sys/socket.h>
     48       1.1    dyoung #include <sys/ioctl.h>
     49       1.1    dyoung #include <sys/errno.h>
     50       1.1    dyoung #include <sys/device.h>
     51       1.1    dyoung 
     52       1.1    dyoung #include <machine/endian.h>
     53       1.9     perry 
     54       1.1    dyoung #include <net/if.h>
     55       1.1    dyoung #include <net/if_dl.h>
     56       1.1    dyoung #include <net/if_media.h>
     57       1.1    dyoung #include <net/if_ether.h>
     58       1.2    dyoung 
     59      1.10    dyoung #include <net80211/ieee80211_netbsd.h>
     60       1.3    dyoung #include <net80211/ieee80211_radiotap.h>
     61       1.2    dyoung #include <net80211/ieee80211_var.h>
     62       1.1    dyoung 
     63      1.15        ad #include <sys/bus.h>
     64      1.15        ad #include <sys/intr.h>
     65       1.1    dyoung 
     66       1.1    dyoung #include <dev/ic/atwreg.h>
     67       1.6    dyoung #include <dev/ic/rf3000reg.h>
     68       1.6    dyoung #include <dev/ic/si4136reg.h>
     69       1.1    dyoung #include <dev/ic/atwvar.h>
     70       1.1    dyoung 
     71       1.1    dyoung #include <dev/pci/pcivar.h>
     72       1.1    dyoung #include <dev/pci/pcireg.h>
     73       1.1    dyoung #include <dev/pci/pcidevs.h>
     74       1.1    dyoung 
     75       1.1    dyoung /*
     76       1.1    dyoung  * PCI configuration space registers used by the ADM8211.
     77       1.1    dyoung  */
     78       1.1    dyoung #define	ATW_PCI_IOBA		0x10	/* i/o mapped base */
     79       1.1    dyoung #define	ATW_PCI_MMBA		0x14	/* memory mapped base */
     80       1.1    dyoung 
     81       1.1    dyoung struct atw_pci_softc {
     82       1.4    dyoung 	struct atw_softc	psc_atw;	/* real ADM8211 softc */
     83       1.1    dyoung 
     84       1.4    dyoung 	pci_intr_handle_t	psc_ih;		/* interrupt handle */
     85       1.4    dyoung 	void			*psc_intrcookie;
     86       1.1    dyoung 
     87       1.4    dyoung 	pci_chipset_tag_t	psc_pc;		/* our PCI chipset */
     88       1.4    dyoung 	pcitag_t		psc_pcitag;	/* our PCI tag */
     89       1.1    dyoung };
     90       1.1    dyoung 
     91      1.16    dyoung static int	atw_pci_match(device_t, struct cfdata *, void *);
     92      1.16    dyoung static void	atw_pci_attach(device_t, device_t, void *);
     93       1.1    dyoung 
     94  1.18.4.2      yamt CFATTACH_DECL_NEW(atw_pci, sizeof(struct atw_pci_softc),
     95       1.1    dyoung     atw_pci_match, atw_pci_attach, NULL, NULL);
     96       1.1    dyoung 
     97       1.8   thorpej static const struct atw_pci_product {
     98       1.1    dyoung 	u_int32_t	app_vendor;	/* PCI vendor ID */
     99       1.1    dyoung 	u_int32_t	app_product;	/* PCI product ID */
    100       1.1    dyoung 	const char	*app_product_name;
    101       1.1    dyoung } atw_pci_products[] = {
    102       1.1    dyoung 	{ PCI_VENDOR_ADMTEK,		PCI_PRODUCT_ADMTEK_ADM8211,
    103       1.1    dyoung 	  "ADMtek ADM8211 802.11 MAC/BBP" },
    104       1.1    dyoung 
    105       1.1    dyoung 	{ 0,				0,				NULL },
    106       1.1    dyoung };
    107       1.1    dyoung 
    108       1.8   thorpej static const struct atw_pci_product *
    109       1.5    dyoung atw_pci_lookup(const struct pci_attach_args *pa)
    110       1.1    dyoung {
    111       1.1    dyoung 	const struct atw_pci_product *app;
    112       1.1    dyoung 
    113       1.1    dyoung 	for (app = atw_pci_products;
    114       1.1    dyoung 	     app->app_product_name != NULL;
    115       1.1    dyoung 	     app++) {
    116       1.1    dyoung 		if (PCI_VENDOR(pa->pa_id) == app->app_vendor &&
    117       1.1    dyoung 		    PCI_PRODUCT(pa->pa_id) == app->app_product)
    118       1.1    dyoung 			return (app);
    119       1.1    dyoung 	}
    120       1.1    dyoung 	return (NULL);
    121       1.1    dyoung }
    122       1.1    dyoung 
    123       1.8   thorpej static int
    124  1.18.4.2      yamt atw_pci_match(device_t parent, cfdata_t match, void *aux)
    125       1.1    dyoung {
    126       1.1    dyoung 	struct pci_attach_args *pa = aux;
    127       1.1    dyoung 
    128       1.1    dyoung 	if (atw_pci_lookup(pa) != NULL)
    129       1.2    dyoung 		return (1);
    130       1.1    dyoung 
    131       1.1    dyoung 	return (0);
    132       1.1    dyoung }
    133       1.1    dyoung 
    134       1.4    dyoung static int
    135       1.4    dyoung atw_pci_enable(struct atw_softc *sc)
    136       1.4    dyoung {
    137  1.18.4.2      yamt 	struct atw_pci_softc *psc = (struct atw_pci_softc *)sc;
    138       1.4    dyoung 
    139       1.4    dyoung 	/* Establish the interrupt. */
    140       1.4    dyoung 	psc->psc_intrcookie = pci_intr_establish(psc->psc_pc, psc->psc_ih,
    141       1.4    dyoung 	    IPL_NET, atw_intr, sc);
    142       1.4    dyoung 	if (psc->psc_intrcookie == NULL) {
    143  1.18.4.2      yamt 		aprint_error_dev(sc->sc_dev,
    144      1.18    dyoung 		    "unable to establish interrupt\n");
    145       1.4    dyoung 		return (1);
    146       1.4    dyoung 	}
    147       1.4    dyoung 
    148       1.4    dyoung 	return (0);
    149       1.4    dyoung }
    150       1.4    dyoung 
    151       1.4    dyoung static void
    152       1.4    dyoung atw_pci_disable(struct atw_softc *sc)
    153       1.4    dyoung {
    154  1.18.4.2      yamt 	struct atw_pci_softc *psc = (struct atw_pci_softc *)sc;
    155       1.4    dyoung 
    156       1.4    dyoung 	/* Unhook the interrupt handler. */
    157       1.4    dyoung 	pci_intr_disestablish(psc->psc_pc, psc->psc_intrcookie);
    158       1.4    dyoung 	psc->psc_intrcookie = NULL;
    159       1.4    dyoung }
    160       1.4    dyoung 
    161       1.8   thorpej static void
    162      1.16    dyoung atw_pci_attach(device_t parent, device_t self, void *aux)
    163       1.1    dyoung {
    164      1.16    dyoung 	struct atw_pci_softc *psc = device_private(self);
    165       1.4    dyoung 	struct atw_softc *sc = &psc->psc_atw;
    166       1.1    dyoung 	struct pci_attach_args *pa = aux;
    167       1.1    dyoung 	pci_chipset_tag_t pc = pa->pa_pc;
    168       1.1    dyoung 	const char *intrstr = NULL;
    169       1.1    dyoung 	bus_space_tag_t iot, memt;
    170       1.1    dyoung 	bus_space_handle_t ioh, memh;
    171       1.1    dyoung 	int ioh_valid, memh_valid;
    172       1.1    dyoung 	const struct atw_pci_product *app;
    173      1.12  christos 	int error;
    174       1.1    dyoung 
    175  1.18.4.2      yamt 	sc->sc_dev = self;
    176  1.18.4.2      yamt 
    177       1.4    dyoung 	psc->psc_pc = pa->pa_pc;
    178       1.4    dyoung 	psc->psc_pcitag = pa->pa_tag;
    179       1.1    dyoung 
    180       1.1    dyoung 	app = atw_pci_lookup(pa);
    181       1.1    dyoung 	if (app == NULL) {
    182       1.1    dyoung 		printf("\n");
    183       1.1    dyoung 		panic("atw_pci_attach: impossible");
    184       1.1    dyoung 	}
    185       1.1    dyoung 
    186       1.1    dyoung 	/*
    187       1.1    dyoung 	 * No power management hooks.
    188       1.1    dyoung 	 * XXX Maybe we should add some!
    189       1.1    dyoung 	 */
    190       1.1    dyoung 	sc->sc_flags |= ATWF_ENABLED;
    191       1.1    dyoung 
    192       1.1    dyoung 	/*
    193       1.1    dyoung 	 * Get revision info, and set some chip-specific variables.
    194       1.1    dyoung 	 */
    195       1.7    dyoung 	sc->sc_rev = PCI_REVISION(pa->pa_class);
    196       1.7    dyoung 	printf(": %s, revision %d.%d\n", app->app_product_name,
    197       1.7    dyoung 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    198       1.1    dyoung 
    199      1.12  christos 	/* power up chip */
    200      1.17    dyoung 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self,
    201      1.12  christos 	    NULL)) && error != EOPNOTSUPP) {
    202  1.18.4.2      yamt 		aprint_error_dev(self, "cannot activate %d\n", error);
    203      1.12  christos 		return;
    204       1.1    dyoung 	}
    205       1.1    dyoung 
    206       1.1    dyoung 	/*
    207       1.1    dyoung 	 * Map the device.
    208       1.1    dyoung 	 */
    209       1.1    dyoung 	ioh_valid = (pci_mapreg_map(pa, ATW_PCI_IOBA,
    210       1.1    dyoung 	    PCI_MAPREG_TYPE_IO, 0,
    211       1.1    dyoung 	    &iot, &ioh, NULL, NULL) == 0);
    212       1.1    dyoung 	memh_valid = (pci_mapreg_map(pa, ATW_PCI_MMBA,
    213       1.1    dyoung 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    214       1.1    dyoung 	    &memt, &memh, NULL, NULL) == 0);
    215       1.1    dyoung 
    216       1.1    dyoung 	if (memh_valid) {
    217       1.1    dyoung 		sc->sc_st = memt;
    218       1.1    dyoung 		sc->sc_sh = memh;
    219       1.1    dyoung 	} else if (ioh_valid) {
    220       1.1    dyoung 		sc->sc_st = iot;
    221       1.1    dyoung 		sc->sc_sh = ioh;
    222       1.1    dyoung 	} else {
    223       1.1    dyoung 		printf(": unable to map device registers\n");
    224       1.1    dyoung 		return;
    225       1.1    dyoung 	}
    226       1.1    dyoung 
    227       1.1    dyoung 	sc->sc_dmat = pa->pa_dmat;
    228       1.1    dyoung 
    229       1.1    dyoung 	/*
    230       1.1    dyoung 	 * Make sure bus mastering is enabled.
    231       1.1    dyoung 	 */
    232       1.1    dyoung 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    233       1.1    dyoung 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    234       1.1    dyoung 	    PCI_COMMAND_MASTER_ENABLE);
    235       1.1    dyoung 
    236       1.1    dyoung 	/*
    237       1.1    dyoung 	 * Get the cacheline size.
    238       1.1    dyoung 	 */
    239       1.1    dyoung 	sc->sc_cacheline = PCI_CACHELINE(pci_conf_read(pc, pa->pa_tag,
    240       1.1    dyoung 	    PCI_BHLC_REG));
    241       1.1    dyoung 
    242       1.1    dyoung 	/*
    243       1.1    dyoung 	 * Get PCI data moving command info.
    244       1.1    dyoung 	 */
    245       1.1    dyoung 	if (pa->pa_flags & PCI_FLAGS_MRL_OKAY) /* read line */
    246       1.1    dyoung 		sc->sc_flags |= ATWF_MRL;
    247       1.1    dyoung 	if (pa->pa_flags & PCI_FLAGS_MRM_OKAY) /* read multiple */
    248       1.1    dyoung 		sc->sc_flags |= ATWF_MRM;
    249       1.1    dyoung 	if (pa->pa_flags & PCI_FLAGS_MWI_OKAY) /* write invalidate */
    250       1.1    dyoung 		sc->sc_flags |= ATWF_MWI;
    251       1.1    dyoung 
    252       1.1    dyoung 	/*
    253       1.1    dyoung 	 * Map and establish our interrupt.
    254       1.1    dyoung 	 */
    255       1.4    dyoung 	if (pci_intr_map(pa, &psc->psc_ih)) {
    256  1.18.4.2      yamt 		aprint_error_dev(self, "unable to map interrupt\n");
    257       1.1    dyoung 		return;
    258       1.1    dyoung 	}
    259       1.9     perry 	intrstr = pci_intr_string(pc, psc->psc_ih);
    260       1.4    dyoung 	psc->psc_intrcookie = pci_intr_establish(pc, psc->psc_ih, IPL_NET,
    261       1.4    dyoung 	    atw_intr, sc);
    262       1.4    dyoung 	if (psc->psc_intrcookie == NULL) {
    263  1.18.4.2      yamt 		aprint_error_dev(self, "unable to establish interrupt");
    264       1.1    dyoung 		if (intrstr != NULL)
    265      1.18    dyoung 			aprint_error(" at %s", intrstr);
    266      1.18    dyoung 		aprint_error("\n");
    267       1.1    dyoung 		return;
    268       1.1    dyoung 	}
    269       1.4    dyoung 
    270  1.18.4.2      yamt 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    271       1.1    dyoung 
    272       1.4    dyoung 	sc->sc_enable = atw_pci_enable;
    273       1.4    dyoung 	sc->sc_disable = atw_pci_disable;
    274       1.4    dyoung 
    275       1.1    dyoung 	/*
    276       1.1    dyoung 	 * Finish off the attach.
    277       1.1    dyoung 	 */
    278       1.1    dyoung 	atw_attach(sc);
    279       1.1    dyoung }
    280