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