Home | History | Annotate | Line # | Download | only in cardbus
if_bwi_cardbus.c revision 1.1
      1  1.1  christos /*	$NetBSD: if_bwi_cardbus.c,v 1.1 2012/04/08 20:49:29 christos Exp $ */
      2  1.1  christos /*	$OpenBSD: if_bwi_cardbus.c,v 1.13 2010/08/06 05:26:24 mglocker Exp $ */
      3  1.1  christos 
      4  1.1  christos /*
      5  1.1  christos  * Copyright (c) 2007 Marcus Glocker <mglocker (at) openbsd.org>
      6  1.1  christos  * Copyright (c) 2006 Claudio Jeker <claudio (at) openbsd.org>
      7  1.1  christos  *
      8  1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      9  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     10  1.1  christos  * copyright notice and this permission notice appear in all copies.
     11  1.1  christos  *
     12  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14  1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18  1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19  1.1  christos  */
     20  1.1  christos 
     21  1.1  christos /*
     22  1.1  christos  * Cardbus front-end for the Broadcom AirForce
     23  1.1  christos  */
     24  1.1  christos 
     25  1.1  christos #include <sys/cdefs.h>
     26  1.1  christos __KERNEL_RCSID(0,"$NetBSD: if_bwi_cardbus.c,v 1.1 2012/04/08 20:49:29 christos Exp $");
     27  1.1  christos 
     28  1.1  christos #include "opt_inet.h"
     29  1.1  christos 
     30  1.1  christos #include <sys/param.h>
     31  1.1  christos #include <sys/callout.h>
     32  1.1  christos #include <sys/device.h>
     33  1.1  christos #include <sys/kernel.h>
     34  1.1  christos #include <sys/malloc.h>
     35  1.1  christos #include <sys/mbuf.h>
     36  1.1  christos #include <sys/socket.h>
     37  1.1  christos #include <sys/sockio.h>
     38  1.1  christos #include <sys/systm.h>
     39  1.1  christos #include <sys/errno.h>
     40  1.1  christos 
     41  1.1  christos #include <machine/endian.h>
     42  1.1  christos 
     43  1.1  christos #include <net/if.h>
     44  1.1  christos #include <net/if_dl.h>
     45  1.1  christos #include <net/if_ether.h>
     46  1.1  christos #include <net/if_media.h>
     47  1.1  christos 
     48  1.1  christos #include <net80211/ieee80211_var.h>
     49  1.1  christos #include <net80211/ieee80211_amrr.h>
     50  1.1  christos #include <net80211/ieee80211_radiotap.h>
     51  1.1  christos 
     52  1.1  christos #include <dev/pci/pcireg.h>
     53  1.1  christos #include <dev/pci/pcivar.h>
     54  1.1  christos 
     55  1.1  christos #include <dev/cardbus/cardbusvar.h>
     56  1.1  christos 
     57  1.1  christos #include <dev/ic/bwireg.h>
     58  1.1  christos #include <dev/ic/bwivar.h>
     59  1.1  christos #include <dev/pci/pcidevs.h>
     60  1.1  christos 
     61  1.1  christos struct bwi_cardbus_softc {
     62  1.1  christos 	struct bwi_softc	 csc_bwi;
     63  1.1  christos 
     64  1.1  christos 	/* cardbus specific goo */
     65  1.1  christos 	cardbus_devfunc_t	 csc_ct;
     66  1.1  christos 	pcitag_t		 csc_tag;
     67  1.1  christos 	void			*csc_ih;
     68  1.1  christos 
     69  1.1  christos 	bus_size_t		 csc_mapsize;
     70  1.1  christos 	pcireg_t		 csc_bar_val;
     71  1.1  christos 	int			 csc_intrline;
     72  1.1  christos 	pci_chipset_tag_t	 csc_pc;
     73  1.1  christos };
     74  1.1  christos 
     75  1.1  christos int		bwi_cardbus_match(device_t, cfdata_t, void*);
     76  1.1  christos void		bwi_cardbus_attach(device_t, device_t, void *);
     77  1.1  christos int		bwi_cardbus_detach(device_t, int);
     78  1.1  christos void		bwi_cardbus_setup(struct bwi_cardbus_softc *);
     79  1.1  christos int		bwi_cardbus_enable(struct bwi_softc *);
     80  1.1  christos void		bwi_cardbus_disable(struct bwi_softc *);
     81  1.1  christos void		bwi_cardbus_conf_write(void *, uint32_t, uint32_t);
     82  1.1  christos uint32_t	bwi_cardbus_conf_read(void *, uint32_t);
     83  1.1  christos 
     84  1.1  christos CFATTACH_DECL_NEW(bwi_cardbus, sizeof (struct bwi_cardbus_softc),
     85  1.1  christos     bwi_cardbus_match, bwi_cardbus_attach, bwi_cardbus_detach, NULL);
     86  1.1  christos 
     87  1.1  christos int
     88  1.1  christos bwi_cardbus_match(device_t parent, cfdata_t match, void *aux)
     89  1.1  christos {
     90  1.1  christos 	struct cardbus_attach_args *ca = aux;
     91  1.1  christos 
     92  1.1  christos 	if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_BROADCOM) {
     93  1.1  christos 		switch (PCI_PRODUCT(ca->ca_id)) {
     94  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4303:
     95  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4306:
     96  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4306_2:
     97  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4307:
     98  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4309:
     99  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4311:
    100  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4312:
    101  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4318:
    102  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4319:
    103  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4322:
    104  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM43XG:
    105  1.1  christos 		case PCI_PRODUCT_BROADCOM_BCM4328:
    106  1.1  christos 			return (1);
    107  1.1  christos 		default:
    108  1.1  christos 			return (0);
    109  1.1  christos 		}
    110  1.1  christos 	}
    111  1.1  christos 
    112  1.1  christos 	return (0);
    113  1.1  christos }
    114  1.1  christos 
    115  1.1  christos void
    116  1.1  christos bwi_cardbus_attach(device_t parent, device_t self, void *aux)
    117  1.1  christos {
    118  1.1  christos 	struct bwi_cardbus_softc *csc = device_private(self);
    119  1.1  christos 	struct cardbus_attach_args *ca = aux;
    120  1.1  christos 	struct bwi_softc *sc = &csc->csc_bwi;
    121  1.1  christos 	cardbus_devfunc_t ct = ca->ca_ct;
    122  1.1  christos 	pcireg_t reg;
    123  1.1  christos 	bus_addr_t base;
    124  1.1  christos 	int error;
    125  1.1  christos 
    126  1.1  christos 	sc->sc_dev = self;
    127  1.1  christos 	sc->sc_dmat = ca->ca_dmat;
    128  1.1  christos 	csc->csc_ct = ct;
    129  1.1  christos 	csc->csc_tag = ca->ca_tag;
    130  1.1  christos 
    131  1.1  christos 	/* power management hooks */
    132  1.1  christos 	sc->sc_enable = bwi_cardbus_enable;
    133  1.1  christos 	sc->sc_disable = bwi_cardbus_disable;
    134  1.1  christos 	//sc->sc_power = bwi_cardbus_power;
    135  1.1  christos 
    136  1.1  christos 	/* map control/status registers */
    137  1.1  christos 	error = Cardbus_mapreg_map(ct, BWI_PCIR_BAR,
    138  1.1  christos 	    PCI_MAPREG_TYPE_MEM, 0, &sc->sc_mem_bt,
    139  1.1  christos 	    &sc->sc_mem_bh, &base, &csc->csc_mapsize);
    140  1.1  christos 	if (error != 0) {
    141  1.1  christos 		printf(": can't map mem space\n");
    142  1.1  christos 		return;
    143  1.1  christos 	}
    144  1.1  christos 	csc->csc_bar_val = base | PCI_MAPREG_TYPE_MEM;
    145  1.1  christos 
    146  1.1  christos 	/* set up the PCI configuration registers */
    147  1.1  christos 	bwi_cardbus_setup(csc);
    148  1.1  christos 
    149  1.1  christos 	printf(": irq %d", csc->csc_intrline);
    150  1.1  christos 
    151  1.1  christos 	/* we need to access Cardbus config space from the driver */
    152  1.1  christos 	sc->sc_conf_read = bwi_cardbus_conf_read;
    153  1.1  christos 	sc->sc_conf_write = bwi_cardbus_conf_write;
    154  1.1  christos 
    155  1.1  christos 	reg = (sc->sc_conf_read)(sc, PCI_SUBSYS_ID_REG);
    156  1.1  christos 
    157  1.1  christos 	sc->sc_pci_revid = PCI_REVISION(ca->ca_class);
    158  1.1  christos 	sc->sc_pci_did = PCI_PRODUCT(ca->ca_id);
    159  1.1  christos 	sc->sc_pci_subvid = PCI_VENDOR(reg);
    160  1.1  christos 	sc->sc_pci_subdid = PCI_PRODUCT(reg);
    161  1.1  christos 
    162  1.1  christos 	error = bwi_attach(sc);
    163  1.1  christos 	if (error != 0)
    164  1.1  christos 		bwi_cardbus_detach(sc->sc_dev, 0);
    165  1.1  christos 
    166  1.1  christos 	Cardbus_function_disable(ct);
    167  1.1  christos }
    168  1.1  christos 
    169  1.1  christos int
    170  1.1  christos bwi_cardbus_detach(device_t self, int flags)
    171  1.1  christos {
    172  1.1  christos 	struct bwi_cardbus_softc *csc = device_private(self);
    173  1.1  christos 	struct bwi_softc *sc = &csc->csc_bwi;
    174  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    175  1.1  christos 	cardbus_chipset_tag_t cc = ct->ct_cc;
    176  1.1  christos 	cardbus_function_tag_t cf = ct->ct_cf;
    177  1.1  christos 
    178  1.1  christos #ifdef DIAGNOSTIC
    179  1.1  christos 	if (ct == NULL)
    180  1.1  christos 		panic("%s: data structure lacks", device_xname(self));
    181  1.1  christos 
    182  1.1  christos #endif
    183  1.1  christos 
    184  1.1  christos 	bwi_detach(sc);
    185  1.1  christos 
    186  1.1  christos 	/* unhook the interrupt handler */
    187  1.1  christos 	if (csc->csc_ih != NULL) {
    188  1.1  christos 		cardbus_intr_disestablish(cc, cf, csc->csc_ih);
    189  1.1  christos 		csc->csc_ih = NULL;
    190  1.1  christos 	}
    191  1.1  christos 
    192  1.1  christos 	/* release bus space and close window */
    193  1.1  christos 	Cardbus_mapreg_unmap(ct, BWI_PCIR_BAR, sc->sc_mem_bt,
    194  1.1  christos 	    sc->sc_mem_bh, csc->csc_mapsize);
    195  1.1  christos 
    196  1.1  christos 	return (0);
    197  1.1  christos }
    198  1.1  christos 
    199  1.1  christos void
    200  1.1  christos bwi_cardbus_setup(struct bwi_cardbus_softc *csc)
    201  1.1  christos {
    202  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    203  1.1  christos 	cardbus_chipset_tag_t cc = ct->ct_cc;
    204  1.1  christos 	pci_chipset_tag_t pc = csc->csc_pc;
    205  1.1  christos 	cardbus_function_tag_t cf = ct->ct_cf;
    206  1.1  christos 	pcireg_t reg;
    207  1.1  christos 
    208  1.1  christos 	/* program the BAR */
    209  1.1  christos 	cardbus_conf_write(cc, cf, csc->csc_tag, BWI_PCIR_BAR,
    210  1.1  christos 	    csc->csc_bar_val);
    211  1.1  christos 
    212  1.1  christos 	/* make sure the right access type is on the cardbus bridge */
    213  1.1  christos 	(*cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    214  1.1  christos 	(*cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    215  1.1  christos 
    216  1.1  christos 	/* enable the appropriate bits in the PCI CSR */
    217  1.1  christos 	reg = pci_conf_read(pc, csc->csc_tag,
    218  1.1  christos 	    PCI_COMMAND_STATUS_REG);
    219  1.1  christos 	reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
    220  1.1  christos 	Cardbus_conf_write(ct, csc->csc_tag, PCI_COMMAND_STATUS_REG,
    221  1.1  christos 	    reg);
    222  1.1  christos }
    223  1.1  christos 
    224  1.1  christos int
    225  1.1  christos bwi_cardbus_enable(struct bwi_softc *sc)
    226  1.1  christos {
    227  1.1  christos 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
    228  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    229  1.1  christos 	cardbus_chipset_tag_t cc = ct->ct_cc;
    230  1.1  christos 	cardbus_function_tag_t cf = ct->ct_cf;
    231  1.1  christos 
    232  1.1  christos 	/* power on the socket */
    233  1.1  christos 	Cardbus_function_enable(ct);
    234  1.1  christos 
    235  1.1  christos 	/* setup the PCI configuration registers */
    236  1.1  christos 	bwi_cardbus_setup(csc);
    237  1.1  christos 
    238  1.1  christos 	/* map and establish the interrupt handler */
    239  1.1  christos 	csc->csc_ih = cardbus_intr_establish(cc, cf, IPL_NET,
    240  1.1  christos 	    bwi_intr, sc);
    241  1.1  christos 	if (csc->csc_ih == NULL) {
    242  1.1  christos 		aprint_error_dev(sc->sc_dev,
    243  1.1  christos 		    "unable to establish interrupt\n");
    244  1.1  christos 		Cardbus_function_disable(ct);
    245  1.1  christos 		return false;
    246  1.1  christos 	}
    247  1.1  christos 
    248  1.1  christos 	return (0);
    249  1.1  christos }
    250  1.1  christos 
    251  1.1  christos void
    252  1.1  christos bwi_cardbus_disable(struct bwi_softc *sc)
    253  1.1  christos {
    254  1.1  christos 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)sc;
    255  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    256  1.1  christos 	cardbus_chipset_tag_t cc = ct->ct_cc;
    257  1.1  christos 	cardbus_function_tag_t cf = ct->ct_cf;
    258  1.1  christos 
    259  1.1  christos 	/* unhook the interrupt handler */
    260  1.1  christos 	cardbus_intr_disestablish(cc, cf, csc->csc_ih);
    261  1.1  christos 	csc->csc_ih = NULL;
    262  1.1  christos 
    263  1.1  christos 	/* power down the socket */
    264  1.1  christos 	Cardbus_function_disable(ct);
    265  1.1  christos }
    266  1.1  christos 
    267  1.1  christos void
    268  1.1  christos bwi_cardbus_conf_write(void *self, uint32_t reg, uint32_t val)
    269  1.1  christos {
    270  1.1  christos 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)self;
    271  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    272  1.1  christos 	cardbus_chipset_tag_t cc = ct->ct_cc;
    273  1.1  christos 	cardbus_function_tag_t cf = ct->ct_cf;
    274  1.1  christos 
    275  1.1  christos 	cardbus_conf_write(cc, cf, csc->csc_tag, reg, val);
    276  1.1  christos }
    277  1.1  christos 
    278  1.1  christos uint32_t
    279  1.1  christos bwi_cardbus_conf_read(void *self, uint32_t reg)
    280  1.1  christos {
    281  1.1  christos 	struct bwi_cardbus_softc *csc = (struct bwi_cardbus_softc *)self;
    282  1.1  christos 	cardbus_devfunc_t ct = csc->csc_ct;
    283  1.1  christos 	cardbus_chipset_tag_t cc = ct->ct_cc;
    284  1.1  christos 	cardbus_function_tag_t cf = ct->ct_cf;
    285  1.1  christos 
    286  1.1  christos 	return (cardbus_conf_read(cc, cf, csc->csc_tag, reg));
    287  1.1  christos }
    288