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