Home | History | Annotate | Line # | Download | only in pci
if_bwi_pci.c revision 1.6
      1 /*	$NetBSD: if_bwi_pci.c,v 1.6 2009/04/26 10:07:48 cegger Exp $	*/
      2 /*	$OpenBSD: if_bwi_pci.c,v 1.6 2008/02/14 22:10:02 brad Exp $ */
      3 
      4 /*
      5  * Copyright (c) 2007 Marcus Glocker <mglocker (at) openbsd.org>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 /*
     21  * Broadcom AirForce BCM43xx IEEE 802.11b/g wireless network driver
     22  * PCI front end
     23  */
     24 
     25 #include "bpfilter.h"
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: if_bwi_pci.c,v 1.6 2009/04/26 10:07:48 cegger Exp $");
     29 
     30 #include <sys/param.h>
     31 #include <sys/callout.h>
     32 #include <sys/device.h>
     33 #include <sys/kernel.h>
     34 #include <sys/malloc.h>
     35 #include <sys/mbuf.h>
     36 #include <sys/socket.h>
     37 #include <sys/sockio.h>
     38 #include <sys/systm.h>
     39 
     40 #include <machine/bus.h>
     41 
     42 #include <net/if.h>
     43 #include <net/if_dl.h>
     44 #include <net/if_ether.h>
     45 #include <net/if_media.h>
     46 
     47 #include <net80211/ieee80211_var.h>
     48 #include <net80211/ieee80211_amrr.h>
     49 #include <net80211/ieee80211_radiotap.h>
     50 
     51 #include <dev/ic/bwivar.h>
     52 
     53 #include <dev/pci/pcireg.h>
     54 #include <dev/pci/pcivar.h>
     55 #include <dev/pci/pcidevs.h>
     56 
     57 /* Base Address Register */
     58 #define BWI_PCI_BAR0	0x10
     59 
     60 static int	bwi_pci_match(device_t, cfdata_t, void *);
     61 static void	bwi_pci_attach(device_t, device_t, void *);
     62 static int	bwi_pci_detach(device_t, int);
     63 static void	bwi_pci_conf_write(void *, uint32_t, uint32_t);
     64 static uint32_t	bwi_pci_conf_read(void *, uint32_t);
     65 
     66 struct bwi_pci_softc {
     67 	struct bwi_softc	 psc_bwi;
     68 
     69 	pci_chipset_tag_t        psc_pc;
     70 	pcitag_t		 psc_pcitag;
     71 
     72 	bus_size_t		 psc_mapsize;
     73 };
     74 
     75 CFATTACH_DECL_NEW(bwi_pci, sizeof(struct bwi_pci_softc),
     76     bwi_pci_match, bwi_pci_attach, bwi_pci_detach, NULL);
     77 
     78 static int
     79 bwi_pci_match(device_t parent, cfdata_t match, void *aux)
     80 {
     81 	struct pci_attach_args *pa = aux;
     82 
     83 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_BROADCOM)
     84 		return (0);
     85 
     86 	switch (PCI_PRODUCT(pa->pa_id)) {
     87 	case PCI_PRODUCT_BROADCOM_BCM4303:
     88 	case PCI_PRODUCT_BROADCOM_BCM4306:
     89 	case PCI_PRODUCT_BROADCOM_BCM4306_2:
     90 	case PCI_PRODUCT_BROADCOM_BCM4307:
     91 	case PCI_PRODUCT_BROADCOM_BCM4309:
     92 	case PCI_PRODUCT_BROADCOM_BCM4311:
     93 	case PCI_PRODUCT_BROADCOM_BCM4312:
     94 	case PCI_PRODUCT_BROADCOM_BCM4318:
     95 	case PCI_PRODUCT_BROADCOM_BCM4319:
     96 	case PCI_PRODUCT_BROADCOM_BCM4322:
     97 	case PCI_PRODUCT_BROADCOM_BCM43XG:
     98 	case PCI_PRODUCT_BROADCOM_BCM4328:
     99 		return (1);
    100 	}
    101 
    102 	return (0);
    103 }
    104 
    105 static void
    106 bwi_pci_attach(device_t parent, device_t self, void *aux)
    107 {
    108 	struct bwi_pci_softc *psc = device_private(self);
    109 	struct pci_attach_args *pa = aux;
    110 	struct bwi_softc *sc = &psc->psc_bwi;
    111 	const char *intrstr = NULL;
    112 	pci_intr_handle_t ih;
    113 	pcireg_t memtype, reg;
    114 
    115 	sc->sc_dev = self;
    116 	sc->sc_dmat = pa->pa_dmat;
    117 	psc->psc_pc = pa->pa_pc;
    118 	psc->psc_pcitag = pa->pa_tag;
    119 
    120 	/* map control / status registers */
    121 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BWI_PCI_BAR0);
    122 	switch (memtype) {
    123 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    124 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    125 		if (pci_mapreg_map(pa, BWI_PCI_BAR0,
    126 		    memtype, 0, &sc->sc_mem_bt, &sc->sc_mem_bh,
    127 		    NULL, &psc->psc_mapsize) == 0)
    128 			break;
    129 	default:
    130 		aprint_error(": could not map memory space\n");
    131 		return;
    132 	}
    133 
    134         aprint_normal("\n");
    135 
    136 	/* map interrupt */
    137 	if (pci_intr_map(pa, &ih) != 0) {
    138 		aprint_error_dev(self, "could not map interrupt\n");
    139 		return;
    140 	}
    141 
    142 	/* establish interrupt */
    143 	intrstr = pci_intr_string(psc->psc_pc, ih);
    144 	sc->sc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_NET, bwi_intr, sc);
    145 	if (sc->sc_ih == NULL) {
    146 		aprint_error_dev(self, "could not establish interrupt");
    147 		if (intrstr != NULL)
    148 			aprint_error(" at %s", intrstr);
    149 		aprint_error("\n");
    150 		return;
    151 	}
    152 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    153 
    154 	/* we need to access PCI config space from the driver */
    155 	sc->sc_conf_write = bwi_pci_conf_write;
    156 	sc->sc_conf_read = bwi_pci_conf_read;
    157 
    158 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    159 
    160 	sc->sc_pci_revid = PCI_REVISION(pa->pa_class);
    161 	sc->sc_pci_did = PCI_PRODUCT(pa->pa_id);
    162 	sc->sc_pci_subvid = PCI_VENDOR(reg);
    163 	sc->sc_pci_subdid = PCI_PRODUCT(reg);
    164 
    165 	if (!pmf_device_register(self, bwi_suspend, bwi_resume))
    166 		aprint_error_dev(self, "couldn't establish power handler\n");
    167 
    168 	bwi_attach(sc);
    169 }
    170 
    171 int
    172 bwi_pci_detach(device_t self, int flags)
    173 {
    174 	struct bwi_pci_softc *psc = device_private(self);
    175 	struct bwi_softc *sc = &psc->psc_bwi;
    176 
    177 	pmf_device_deregister(self);
    178 
    179 	bwi_detach(sc);
    180 
    181 	if (sc->sc_ih != NULL) {
    182 		pci_intr_disestablish(psc->psc_pc, sc->sc_ih);
    183 		sc->sc_ih = NULL;
    184 	}
    185 
    186 	return (0);
    187 }
    188 
    189 static void
    190 bwi_pci_conf_write(void *sc, uint32_t reg, uint32_t val)
    191 {
    192 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)sc;
    193 
    194 	pci_conf_write(psc->psc_pc, psc->psc_pcitag, reg, val);
    195 }
    196 
    197 static uint32_t
    198 bwi_pci_conf_read(void *sc, uint32_t reg)
    199 {
    200 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)sc;
    201 
    202 	return (pci_conf_read(psc->psc_pc, psc->psc_pcitag, reg));
    203 }
    204