Home | History | Annotate | Line # | Download | only in pci
if_bwi_pci.c revision 1.11
      1  1.11    dyoung /*	$NetBSD: if_bwi_pci.c,v 1.11 2011/05/18 01:02:43 dyoung 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 
     26   1.2  macallan #include <sys/cdefs.h>
     27  1.11    dyoung __KERNEL_RCSID(0, "$NetBSD: if_bwi_pci.c,v 1.11 2011/05/18 01:02:43 dyoung Exp $");
     28   1.2  macallan 
     29   1.1  macallan #include <sys/param.h>
     30   1.2  macallan #include <sys/callout.h>
     31   1.2  macallan #include <sys/device.h>
     32   1.2  macallan #include <sys/kernel.h>
     33   1.2  macallan #include <sys/malloc.h>
     34   1.1  macallan #include <sys/mbuf.h>
     35   1.1  macallan #include <sys/socket.h>
     36   1.2  macallan #include <sys/sockio.h>
     37   1.1  macallan #include <sys/systm.h>
     38   1.1  macallan 
     39  1.11    dyoung #include <sys/bus.h>
     40   1.1  macallan 
     41   1.1  macallan #include <net/if.h>
     42   1.1  macallan #include <net/if_dl.h>
     43   1.2  macallan #include <net/if_ether.h>
     44   1.1  macallan #include <net/if_media.h>
     45   1.1  macallan 
     46   1.1  macallan #include <net80211/ieee80211_var.h>
     47   1.1  macallan #include <net80211/ieee80211_amrr.h>
     48   1.1  macallan #include <net80211/ieee80211_radiotap.h>
     49   1.1  macallan 
     50   1.1  macallan #include <dev/ic/bwivar.h>
     51   1.1  macallan 
     52   1.1  macallan #include <dev/pci/pcireg.h>
     53   1.1  macallan #include <dev/pci/pcivar.h>
     54   1.1  macallan #include <dev/pci/pcidevs.h>
     55   1.1  macallan 
     56   1.1  macallan /* Base Address Register */
     57   1.1  macallan #define BWI_PCI_BAR0	0x10
     58   1.1  macallan 
     59   1.5    cegger static int	bwi_pci_match(device_t, cfdata_t, void *);
     60   1.5    cegger static void	bwi_pci_attach(device_t, device_t, void *);
     61   1.5    cegger static int	bwi_pci_detach(device_t, int);
     62   1.2  macallan static void	bwi_pci_conf_write(void *, uint32_t, uint32_t);
     63   1.2  macallan static uint32_t	bwi_pci_conf_read(void *, uint32_t);
     64   1.1  macallan 
     65   1.1  macallan struct bwi_pci_softc {
     66   1.1  macallan 	struct bwi_softc	 psc_bwi;
     67   1.1  macallan 
     68   1.1  macallan 	pci_chipset_tag_t        psc_pc;
     69   1.1  macallan 	pcitag_t		 psc_pcitag;
     70   1.1  macallan 
     71   1.1  macallan 	bus_size_t		 psc_mapsize;
     72   1.1  macallan };
     73   1.1  macallan 
     74   1.6    cegger CFATTACH_DECL_NEW(bwi_pci, sizeof(struct bwi_pci_softc),
     75   1.2  macallan     bwi_pci_match, bwi_pci_attach, bwi_pci_detach, NULL);
     76   1.2  macallan 
     77   1.2  macallan static int
     78   1.5    cegger bwi_pci_match(device_t parent, cfdata_t match, void *aux)
     79   1.2  macallan {
     80   1.2  macallan 	struct pci_attach_args *pa = aux;
     81   1.2  macallan 
     82   1.2  macallan 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_BROADCOM)
     83   1.2  macallan 		return (0);
     84   1.1  macallan 
     85   1.2  macallan 	switch (PCI_PRODUCT(pa->pa_id)) {
     86   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM4303:
     87   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM4306:
     88   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM4306_2:
     89   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM4307:
     90   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM4309:
     91   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM4311:
     92   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM4312:
     93   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM4318:
     94   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM4319:
     95   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM4322:
     96   1.2  macallan 	case PCI_PRODUCT_BROADCOM_BCM43XG:
     97   1.3    cegger 	case PCI_PRODUCT_BROADCOM_BCM4328:
     98   1.2  macallan 		return (1);
     99   1.2  macallan 	}
    100   1.1  macallan 
    101   1.2  macallan 	return (0);
    102   1.1  macallan }
    103   1.1  macallan 
    104   1.2  macallan static void
    105   1.5    cegger bwi_pci_attach(device_t parent, device_t self, void *aux)
    106   1.1  macallan {
    107   1.5    cegger 	struct bwi_pci_softc *psc = device_private(self);
    108   1.1  macallan 	struct pci_attach_args *pa = aux;
    109   1.1  macallan 	struct bwi_softc *sc = &psc->psc_bwi;
    110   1.1  macallan 	const char *intrstr = NULL;
    111   1.1  macallan 	pci_intr_handle_t ih;
    112   1.1  macallan 	pcireg_t memtype, reg;
    113   1.8    cegger 	int error = 0;
    114   1.1  macallan 
    115   1.7    cegger 	aprint_naive("\n");
    116  1.10       phx 	aprint_normal(": Broadcom Wireless\n");
    117   1.7    cegger 
    118   1.6    cegger 	sc->sc_dev = self;
    119   1.1  macallan 	sc->sc_dmat = pa->pa_dmat;
    120   1.1  macallan 	psc->psc_pc = pa->pa_pc;
    121   1.1  macallan 	psc->psc_pcitag = pa->pa_tag;
    122   1.1  macallan 
    123   1.1  macallan 	/* map control / status registers */
    124   1.1  macallan 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BWI_PCI_BAR0);
    125   1.2  macallan 	switch (memtype) {
    126   1.2  macallan 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    127   1.2  macallan 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    128   1.7    cegger 		break;
    129   1.2  macallan 	default:
    130   1.7    cegger 		aprint_error_dev(self, "invalid base address register\n");
    131   1.1  macallan 		return;
    132   1.1  macallan 	}
    133   1.1  macallan 
    134   1.7    cegger 	if (pci_mapreg_map(pa, BWI_PCI_BAR0,
    135   1.7    cegger 	    memtype, 0, &sc->sc_mem_bt, &sc->sc_mem_bh,
    136   1.7    cegger 	    NULL, &psc->psc_mapsize) != 0)
    137   1.7    cegger 	{
    138   1.7    cegger 		aprint_error_dev(self, "could not map mem space\n");
    139   1.7    cegger 		return;
    140   1.7    cegger 	}
    141   1.2  macallan 
    142   1.1  macallan 	/* map interrupt */
    143   1.1  macallan 	if (pci_intr_map(pa, &ih) != 0) {
    144   1.2  macallan 		aprint_error_dev(self, "could not map interrupt\n");
    145   1.7    cegger 		goto fail;
    146   1.1  macallan 	}
    147   1.1  macallan 
    148   1.1  macallan 	/* establish interrupt */
    149   1.1  macallan 	intrstr = pci_intr_string(psc->psc_pc, ih);
    150   1.2  macallan 	sc->sc_ih = pci_intr_establish(psc->psc_pc, ih, IPL_NET, bwi_intr, sc);
    151   1.2  macallan 	if (sc->sc_ih == NULL) {
    152   1.2  macallan 		aprint_error_dev(self, "could not establish interrupt");
    153   1.1  macallan 		if (intrstr != NULL)
    154   1.2  macallan 			aprint_error(" at %s", intrstr);
    155   1.2  macallan 		aprint_error("\n");
    156   1.7    cegger 		goto fail;
    157   1.1  macallan 	}
    158   1.2  macallan 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    159   1.1  macallan 
    160   1.1  macallan 	/* we need to access PCI config space from the driver */
    161   1.1  macallan 	sc->sc_conf_write = bwi_pci_conf_write;
    162   1.1  macallan 	sc->sc_conf_read = bwi_pci_conf_read;
    163   1.1  macallan 
    164   1.1  macallan 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    165   1.1  macallan 
    166   1.1  macallan 	sc->sc_pci_revid = PCI_REVISION(pa->pa_class);
    167   1.1  macallan 	sc->sc_pci_did = PCI_PRODUCT(pa->pa_id);
    168   1.1  macallan 	sc->sc_pci_subvid = PCI_VENDOR(reg);
    169   1.1  macallan 	sc->sc_pci_subdid = PCI_PRODUCT(reg);
    170   1.1  macallan 
    171   1.4    kefren 	if (!pmf_device_register(self, bwi_suspend, bwi_resume))
    172   1.4    kefren 		aprint_error_dev(self, "couldn't establish power handler\n");
    173   1.4    kefren 
    174   1.8    cegger 	error = bwi_attach(sc);
    175   1.8    cegger 	if (error)
    176   1.8    cegger 		goto fail;
    177   1.7    cegger 	return;
    178   1.7    cegger 
    179   1.7    cegger fail:
    180   1.7    cegger 	if (sc->sc_ih) {
    181   1.7    cegger 		pci_intr_disestablish(psc->psc_pc, sc->sc_ih);
    182   1.7    cegger 		sc->sc_ih = NULL;
    183   1.7    cegger 	}
    184   1.7    cegger 	if (psc->psc_mapsize) {
    185   1.7    cegger 		bus_space_unmap(sc->sc_mem_bt, sc->sc_mem_bh, psc->psc_mapsize);
    186   1.7    cegger 		psc->psc_mapsize = 0;
    187   1.7    cegger 	}
    188   1.7    cegger 	return;
    189   1.1  macallan }
    190   1.1  macallan 
    191   1.1  macallan int
    192   1.5    cegger bwi_pci_detach(device_t self, int flags)
    193   1.1  macallan {
    194   1.5    cegger 	struct bwi_pci_softc *psc = device_private(self);
    195   1.1  macallan 	struct bwi_softc *sc = &psc->psc_bwi;
    196   1.1  macallan 
    197   1.4    kefren 	pmf_device_deregister(self);
    198   1.4    kefren 
    199   1.1  macallan 	bwi_detach(sc);
    200   1.2  macallan 
    201   1.2  macallan 	if (sc->sc_ih != NULL) {
    202   1.2  macallan 		pci_intr_disestablish(psc->psc_pc, sc->sc_ih);
    203   1.2  macallan 		sc->sc_ih = NULL;
    204   1.2  macallan 	}
    205   1.1  macallan 
    206   1.1  macallan 	return (0);
    207   1.1  macallan }
    208   1.1  macallan 
    209   1.2  macallan static void
    210   1.5    cegger bwi_pci_conf_write(void *sc, uint32_t reg, uint32_t val)
    211   1.1  macallan {
    212   1.5    cegger 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)sc;
    213   1.1  macallan 
    214   1.1  macallan 	pci_conf_write(psc->psc_pc, psc->psc_pcitag, reg, val);
    215   1.1  macallan }
    216   1.1  macallan 
    217   1.2  macallan static uint32_t
    218   1.5    cegger bwi_pci_conf_read(void *sc, uint32_t reg)
    219   1.1  macallan {
    220   1.5    cegger 	struct bwi_pci_softc *psc = (struct bwi_pci_softc *)sc;
    221   1.1  macallan 
    222   1.1  macallan 	return (pci_conf_read(psc->psc_pc, psc->psc_pcitag, reg));
    223   1.1  macallan }
    224