Home | History | Annotate | Line # | Download | only in cardbus
if_ath_cardbus.c revision 1.4.2.3
      1  1.4.2.3  skrll /*	$NetBSD: if_ath_cardbus.c,v 1.4.2.3 2004/08/12 11:41:22 skrll Exp $ */
      2  1.4.2.2  skrll /*
      3  1.4.2.2  skrll  * Copyright (c) 2003
      4  1.4.2.2  skrll  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      5  1.4.2.2  skrll  * All rights reserved.
      6  1.4.2.2  skrll  *
      7  1.4.2.2  skrll  * Redistribution and use in source and binary forms, with or without
      8  1.4.2.2  skrll  * modification, are permitted provided that the following conditions
      9  1.4.2.2  skrll  * are met:
     10  1.4.2.2  skrll  * 1. Redistributions of source code must retain the above copyright
     11  1.4.2.2  skrll  *    notice, this list of conditions and the following disclaimer.
     12  1.4.2.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.4.2.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     14  1.4.2.2  skrll  *    documentation and/or other materials provided with the distribution.
     15  1.4.2.2  skrll  * 3. All advertising materials mentioning features or use of this software
     16  1.4.2.2  skrll  *    must display the following acknowledgement:
     17  1.4.2.2  skrll  *	This product includes software developed by Ichiro FUKUHARA.
     18  1.4.2.2  skrll  * 4. The name of the company nor the name of the author may be used to
     19  1.4.2.2  skrll  *    endorse or promote products derived from this software without specific
     20  1.4.2.2  skrll  *    prior written permission.
     21  1.4.2.2  skrll  *
     22  1.4.2.2  skrll  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     23  1.4.2.2  skrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.4.2.2  skrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.4.2.2  skrll  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     26  1.4.2.2  skrll  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.4.2.2  skrll  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.4.2.2  skrll  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.4.2.2  skrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.4.2.2  skrll  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.4.2.2  skrll  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.4.2.2  skrll  * SUCH DAMAGE.
     33  1.4.2.2  skrll  */
     34  1.4.2.2  skrll /*
     35  1.4.2.2  skrll  * CardBus bus front-end for the AR5001 Wireless LAN 802.11a/b/g CardBus.
     36  1.4.2.2  skrll  */
     37  1.4.2.2  skrll 
     38  1.4.2.2  skrll #include <sys/cdefs.h>
     39  1.4.2.3  skrll __KERNEL_RCSID(0, "$NetBSD: if_ath_cardbus.c,v 1.4.2.3 2004/08/12 11:41:22 skrll Exp $");
     40  1.4.2.2  skrll 
     41  1.4.2.2  skrll #include "opt_inet.h"
     42  1.4.2.2  skrll #include "opt_ns.h"
     43  1.4.2.2  skrll #include "bpfilter.h"
     44  1.4.2.2  skrll 
     45  1.4.2.2  skrll #include <sys/param.h>
     46  1.4.2.2  skrll #include <sys/systm.h>
     47  1.4.2.2  skrll #include <sys/mbuf.h>
     48  1.4.2.2  skrll #include <sys/malloc.h>
     49  1.4.2.2  skrll #include <sys/kernel.h>
     50  1.4.2.2  skrll #include <sys/socket.h>
     51  1.4.2.2  skrll #include <sys/ioctl.h>
     52  1.4.2.2  skrll #include <sys/errno.h>
     53  1.4.2.2  skrll #include <sys/device.h>
     54  1.4.2.2  skrll 
     55  1.4.2.2  skrll #include <machine/endian.h>
     56  1.4.2.2  skrll 
     57  1.4.2.2  skrll #include <net/if.h>
     58  1.4.2.2  skrll #include <net/if_dl.h>
     59  1.4.2.2  skrll #include <net/if_media.h>
     60  1.4.2.2  skrll #include <net/if_ether.h>
     61  1.4.2.2  skrll 
     62  1.4.2.2  skrll #include <net80211/ieee80211_compat.h>
     63  1.4.2.2  skrll #include <net80211/ieee80211_var.h>
     64  1.4.2.2  skrll 
     65  1.4.2.2  skrll #if NBPFILTER > 0
     66  1.4.2.2  skrll #include <net/bpf.h>
     67  1.4.2.2  skrll #endif
     68  1.4.2.2  skrll 
     69  1.4.2.2  skrll #ifdef INET
     70  1.4.2.2  skrll #include <netinet/in.h>
     71  1.4.2.2  skrll #include <netinet/if_inarp.h>
     72  1.4.2.2  skrll #endif
     73  1.4.2.2  skrll 
     74  1.4.2.2  skrll #ifdef NS
     75  1.4.2.2  skrll #include <netns/ns.h>
     76  1.4.2.2  skrll #include <netns/ns_if.h>
     77  1.4.2.2  skrll #endif
     78  1.4.2.2  skrll 
     79  1.4.2.2  skrll #include <machine/bus.h>
     80  1.4.2.2  skrll #include <machine/intr.h>
     81  1.4.2.2  skrll 
     82  1.4.2.2  skrll #include <dev/mii/miivar.h>
     83  1.4.2.2  skrll #include <dev/mii/mii_bitbang.h>
     84  1.4.2.2  skrll 
     85  1.4.2.2  skrll #include <dev/ic/athcompat.h>
     86  1.4.2.2  skrll #include <dev/ic/athvar.h>
     87  1.4.2.2  skrll #include <../contrib/sys/dev/ic/athhal.h>
     88  1.4.2.2  skrll 
     89  1.4.2.2  skrll #include <dev/pci/pcivar.h>
     90  1.4.2.2  skrll #include <dev/pci/pcireg.h>
     91  1.4.2.2  skrll #include <dev/pci/pcidevs.h>
     92  1.4.2.2  skrll 
     93  1.4.2.2  skrll #include <dev/cardbus/cardbusvar.h>
     94  1.4.2.3  skrll #include <dev/pci/pcidevs.h>
     95  1.4.2.2  skrll 
     96  1.4.2.2  skrll /*
     97  1.4.2.2  skrll  * PCI configuration space registers
     98  1.4.2.2  skrll  */
     99  1.4.2.2  skrll #define	ATH_PCI_MMBA		0x10	/* memory mapped base */
    100  1.4.2.2  skrll 
    101  1.4.2.2  skrll struct ath_cardbus_softc {
    102  1.4.2.2  skrll 	struct ath_softc	sc_ath;
    103  1.4.2.2  skrll 
    104  1.4.2.2  skrll 	/* CardBus-specific goo. */
    105  1.4.2.2  skrll 	void	*sc_ih;			/* interrupt handle */
    106  1.4.2.2  skrll 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
    107  1.4.2.2  skrll 	cardbustag_t sc_tag;		/* our CardBus tag */
    108  1.4.2.2  skrll 	bus_size_t sc_mapsize;		/* the size of mapped bus space region */
    109  1.4.2.2  skrll 
    110  1.4.2.2  skrll 	pcireg_t sc_bar_val;		/* value of the BAR */
    111  1.4.2.2  skrll 
    112  1.4.2.2  skrll 	int	sc_intrline;		/* interrupt line */
    113  1.4.2.2  skrll };
    114  1.4.2.2  skrll 
    115  1.4.2.2  skrll int	ath_cardbus_match(struct device *, struct cfdata *, void *);
    116  1.4.2.2  skrll void	ath_cardbus_attach(struct device *, struct device *, void *);
    117  1.4.2.2  skrll int	ath_cardbus_detach(struct device *, int);
    118  1.4.2.2  skrll 
    119  1.4.2.2  skrll CFATTACH_DECL(ath_cardbus, sizeof(struct ath_cardbus_softc),
    120  1.4.2.2  skrll     ath_cardbus_match, ath_cardbus_attach, ath_cardbus_detach, ath_activate);
    121  1.4.2.2  skrll 
    122  1.4.2.2  skrll void	ath_cardbus_setup(struct ath_cardbus_softc *);
    123  1.4.2.2  skrll 
    124  1.4.2.2  skrll int	ath_cardbus_enable(struct ath_softc *);
    125  1.4.2.2  skrll void	ath_cardbus_disable(struct ath_softc *);
    126  1.4.2.2  skrll void	ath_cardbus_power(struct ath_softc *, int);
    127  1.4.2.2  skrll 
    128  1.4.2.2  skrll int
    129  1.4.2.2  skrll ath_cardbus_match(struct device *parent, struct cfdata *match,
    130  1.4.2.2  skrll 	void *aux)
    131  1.4.2.2  skrll {
    132  1.4.2.2  skrll 	struct cardbus_attach_args *ca = aux;
    133  1.4.2.2  skrll 	const char* devname;
    134  1.4.2.2  skrll 
    135  1.4.2.2  skrll 	devname = ath_hal_probe(PCI_VENDOR(ca->ca_id),
    136  1.4.2.2  skrll 				PCI_PRODUCT(ca->ca_id));
    137  1.4.2.2  skrll 
    138  1.4.2.2  skrll 	if (devname)
    139  1.4.2.2  skrll 		return (1);
    140  1.4.2.2  skrll 
    141  1.4.2.2  skrll 	return (0);
    142  1.4.2.2  skrll }
    143  1.4.2.2  skrll 
    144  1.4.2.2  skrll void
    145  1.4.2.2  skrll ath_cardbus_attach(struct device *parent, struct device *self,
    146  1.4.2.2  skrll 	void *aux)
    147  1.4.2.2  skrll {
    148  1.4.2.2  skrll 	struct ath_cardbus_softc *csc = (void *)self;
    149  1.4.2.2  skrll 	struct ath_softc *sc = &csc->sc_ath;
    150  1.4.2.2  skrll 	struct cardbus_attach_args *ca = aux;
    151  1.4.2.2  skrll 	cardbus_devfunc_t ct = ca->ca_ct;
    152  1.4.2.2  skrll 	bus_addr_t adr;
    153  1.4.2.2  skrll 
    154  1.4.2.2  skrll 	sc->sc_dmat = ca->ca_dmat;
    155  1.4.2.2  skrll 	csc->sc_ct = ct;
    156  1.4.2.2  skrll 	csc->sc_tag = ca->ca_tag;
    157  1.4.2.2  skrll 
    158  1.4.2.2  skrll 	/*
    159  1.4.2.2  skrll 	 * Power management hooks.
    160  1.4.2.2  skrll 	 */
    161  1.4.2.2  skrll 	sc->sc_enable = ath_cardbus_enable;
    162  1.4.2.2  skrll 	sc->sc_disable = ath_cardbus_disable;
    163  1.4.2.2  skrll 	sc->sc_power = ath_cardbus_power;
    164  1.4.2.2  skrll 
    165  1.4.2.2  skrll 	/*
    166  1.4.2.2  skrll 	 * Map the device.
    167  1.4.2.2  skrll 	 */
    168  1.4.2.2  skrll 	if (Cardbus_mapreg_map(ct, ATH_PCI_MMBA, CARDBUS_MAPREG_TYPE_MEM, 0,
    169  1.4.2.2  skrll 	    &sc->sc_st, &sc->sc_sh, &adr, &csc->sc_mapsize) == 0) {
    170  1.4.2.2  skrll #if rbus
    171  1.4.2.2  skrll #else
    172  1.4.2.2  skrll 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    173  1.4.2.2  skrll #endif
    174  1.4.2.2  skrll 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
    175  1.4.2.2  skrll 	}
    176  1.4.2.2  skrll 
    177  1.4.2.2  skrll 	else {
    178  1.4.2.2  skrll 		printf("%s: unable to map device registers\n",
    179  1.4.2.2  skrll 		    sc->sc_dev.dv_xname);
    180  1.4.2.2  skrll 		return;
    181  1.4.2.2  skrll 	}
    182  1.4.2.2  skrll 
    183  1.4.2.2  skrll 	/*
    184  1.4.2.2  skrll 	 * Set up the PCI configuration registers.
    185  1.4.2.2  skrll 	 */
    186  1.4.2.2  skrll 	ath_cardbus_setup(csc);
    187  1.4.2.2  skrll 
    188  1.4.2.2  skrll 	/* Remember which interrupt line. */
    189  1.4.2.2  skrll 	csc->sc_intrline = ca->ca_intrline;
    190  1.4.2.2  skrll 
    191  1.4.2.2  skrll 	/*
    192  1.4.2.2  skrll 	 * Finish off the attach.
    193  1.4.2.2  skrll 	 */
    194  1.4.2.2  skrll 	ath_attach(PCI_PRODUCT(ca->ca_id), sc);
    195  1.4.2.2  skrll 
    196  1.4.2.2  skrll 	/*
    197  1.4.2.2  skrll 	 * Power down the socket.
    198  1.4.2.2  skrll 	 */
    199  1.4.2.2  skrll 	Cardbus_function_disable(csc->sc_ct);
    200  1.4.2.2  skrll }
    201  1.4.2.2  skrll 
    202  1.4.2.2  skrll int
    203  1.4.2.2  skrll ath_cardbus_detach(struct device *self, int flags)
    204  1.4.2.2  skrll {
    205  1.4.2.2  skrll 	struct ath_cardbus_softc *csc = (void *)self;
    206  1.4.2.2  skrll 	struct ath_softc *sc = &csc->sc_ath;
    207  1.4.2.2  skrll 	struct cardbus_devfunc *ct = csc->sc_ct;
    208  1.4.2.2  skrll 	int rv;
    209  1.4.2.2  skrll 
    210  1.4.2.2  skrll #if defined(DIAGNOSTIC)
    211  1.4.2.2  skrll 	if (ct == NULL)
    212  1.4.2.2  skrll 		panic("%s: data structure lacks", sc->sc_dev.dv_xname);
    213  1.4.2.2  skrll #endif
    214  1.4.2.2  skrll 
    215  1.4.2.2  skrll 	rv = ath_detach(sc);
    216  1.4.2.2  skrll 	if (rv)
    217  1.4.2.2  skrll 		return (rv);
    218  1.4.2.2  skrll 
    219  1.4.2.2  skrll 	/*
    220  1.4.2.2  skrll 	 * Unhook the interrupt handler.
    221  1.4.2.2  skrll 	 */
    222  1.4.2.2  skrll 	if (csc->sc_ih != NULL)
    223  1.4.2.2  skrll 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    224  1.4.2.2  skrll 		csc->sc_ih = NULL;
    225  1.4.2.2  skrll 
    226  1.4.2.2  skrll 	/*
    227  1.4.2.2  skrll 	 * Release bus space and close window.
    228  1.4.2.2  skrll 	 */
    229  1.4.2.2  skrll 	Cardbus_mapreg_unmap(ct, ATH_PCI_MMBA,
    230  1.4.2.2  skrll 		    sc->sc_st, sc->sc_sh, csc->sc_mapsize);
    231  1.4.2.2  skrll 
    232  1.4.2.2  skrll 	return (0);
    233  1.4.2.2  skrll }
    234  1.4.2.2  skrll 
    235  1.4.2.2  skrll int
    236  1.4.2.2  skrll ath_cardbus_enable(struct ath_softc *sc)
    237  1.4.2.2  skrll {
    238  1.4.2.2  skrll 	struct ath_cardbus_softc *csc = (void *) sc;
    239  1.4.2.2  skrll 	cardbus_devfunc_t ct = csc->sc_ct;
    240  1.4.2.2  skrll 	cardbus_chipset_tag_t cc = ct->ct_cc;
    241  1.4.2.2  skrll 	cardbus_function_tag_t cf = ct->ct_cf;
    242  1.4.2.2  skrll 
    243  1.4.2.2  skrll 	/*
    244  1.4.2.2  skrll 	 * Power on the socket.
    245  1.4.2.2  skrll 	 */
    246  1.4.2.2  skrll 	Cardbus_function_enable(ct);
    247  1.4.2.2  skrll 
    248  1.4.2.2  skrll 	/*
    249  1.4.2.2  skrll 	 * Set up the PCI configuration registers.
    250  1.4.2.2  skrll 	 */
    251  1.4.2.2  skrll 	ath_cardbus_setup(csc);
    252  1.4.2.2  skrll 
    253  1.4.2.2  skrll 	/*
    254  1.4.2.2  skrll 	 * Map and establish the interrupt.
    255  1.4.2.2  skrll 	 */
    256  1.4.2.2  skrll 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
    257  1.4.2.2  skrll 	    ath_intr, sc);
    258  1.4.2.2  skrll 	if (csc->sc_ih == NULL) {
    259  1.4.2.2  skrll 		printf("%s: unable to establish interrupt at %d\n",
    260  1.4.2.2  skrll 		    sc->sc_dev.dv_xname, csc->sc_intrline);
    261  1.4.2.2  skrll 		Cardbus_function_disable(csc->sc_ct);
    262  1.4.2.2  skrll 		return (1);
    263  1.4.2.2  skrll 	}
    264  1.4.2.2  skrll 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    265  1.4.2.2  skrll 		csc->sc_intrline);
    266  1.4.2.2  skrll 
    267  1.4.2.2  skrll 	return (0);
    268  1.4.2.2  skrll }
    269  1.4.2.2  skrll 
    270  1.4.2.2  skrll void
    271  1.4.2.2  skrll ath_cardbus_disable(struct ath_softc *sc)
    272  1.4.2.2  skrll {
    273  1.4.2.2  skrll 	struct ath_cardbus_softc *csc = (void *) sc;
    274  1.4.2.2  skrll 	cardbus_devfunc_t ct = csc->sc_ct;
    275  1.4.2.2  skrll 	cardbus_chipset_tag_t cc = ct->ct_cc;
    276  1.4.2.2  skrll 	cardbus_function_tag_t cf = ct->ct_cf;
    277  1.4.2.2  skrll 
    278  1.4.2.2  skrll 	/* Unhook the interrupt handler. */
    279  1.4.2.2  skrll 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    280  1.4.2.2  skrll 	csc->sc_ih = NULL;
    281  1.4.2.2  skrll 
    282  1.4.2.2  skrll 	/* Power down the socket. */
    283  1.4.2.2  skrll 	Cardbus_function_disable(ct);
    284  1.4.2.2  skrll }
    285  1.4.2.2  skrll 
    286  1.4.2.2  skrll void
    287  1.4.2.2  skrll ath_cardbus_power(struct ath_softc *sc, int why)
    288  1.4.2.2  skrll {
    289  1.4.2.2  skrll 	struct ath_cardbus_softc *csc = (void *) sc;
    290  1.4.2.2  skrll 
    291  1.4.2.2  skrll 	printf("%s: ath_cardbus_power\n", sc->sc_dev.dv_xname);
    292  1.4.2.2  skrll 
    293  1.4.2.2  skrll 	if (why == PWR_RESUME) {
    294  1.4.2.2  skrll 		/*
    295  1.4.2.2  skrll 		 * Give the PCI configuration registers a kick
    296  1.4.2.2  skrll 		 * in the head.
    297  1.4.2.2  skrll 		 */
    298  1.4.2.2  skrll #ifdef DIAGNOSTIC
    299  1.4.2.2  skrll 		if (ATH_IS_ENABLED(sc) == 0)
    300  1.4.2.2  skrll 			panic("ath_cardbus_power");
    301  1.4.2.2  skrll #endif
    302  1.4.2.2  skrll 		ath_cardbus_setup(csc);
    303  1.4.2.2  skrll 	}
    304  1.4.2.2  skrll }
    305  1.4.2.2  skrll 
    306  1.4.2.2  skrll void
    307  1.4.2.2  skrll ath_cardbus_setup(struct ath_cardbus_softc *csc)
    308  1.4.2.2  skrll {
    309  1.4.2.2  skrll 	struct ath_softc *sc = &csc->sc_ath;
    310  1.4.2.2  skrll 	cardbus_devfunc_t ct = csc->sc_ct;
    311  1.4.2.2  skrll 	cardbus_chipset_tag_t cc = ct->ct_cc;
    312  1.4.2.2  skrll 	cardbus_function_tag_t cf = ct->ct_cf;
    313  1.4.2.2  skrll 	pcireg_t reg;
    314  1.4.2.2  skrll 
    315  1.4.2.2  skrll 	(void)cardbus_setpowerstate(sc->sc_dev.dv_xname, ct, csc->sc_tag,
    316  1.4.2.2  skrll 	    PCI_PWR_D0);
    317  1.4.2.2  skrll 
    318  1.4.2.2  skrll 	/* Program the BAR. */
    319  1.4.2.2  skrll 	cardbus_conf_write(cc, cf, csc->sc_tag, ATH_PCI_MMBA,
    320  1.4.2.2  skrll 	    csc->sc_bar_val);
    321  1.4.2.2  skrll 
    322  1.4.2.3  skrll 	/* Make sure the right access type is on the CardBus bridge. */
    323  1.4.2.3  skrll 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    324  1.4.2.3  skrll 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    325  1.4.2.3  skrll 
    326  1.4.2.2  skrll 	/* Enable the appropriate bits in the PCI CSR. */
    327  1.4.2.2  skrll 	reg = cardbus_conf_read(cc, cf, csc->sc_tag,
    328  1.4.2.2  skrll 	    CARDBUS_COMMAND_STATUS_REG);
    329  1.4.2.2  skrll 	reg |= CARDBUS_COMMAND_MASTER_ENABLE | CARDBUS_COMMAND_MEM_ENABLE;
    330  1.4.2.2  skrll 	cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_COMMAND_STATUS_REG,
    331  1.4.2.2  skrll 	    reg);
    332  1.4.2.2  skrll 
    333  1.4.2.2  skrll 	/*
    334  1.4.2.2  skrll 	 * Make sure the latency timer is set to some reasonable
    335  1.4.2.2  skrll 	 * value.
    336  1.4.2.2  skrll 	 */
    337  1.4.2.2  skrll 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
    338  1.4.2.2  skrll 	if (CARDBUS_LATTIMER(reg) < 0x20) {
    339  1.4.2.2  skrll 		reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    340  1.4.2.2  skrll 		reg |= (0x20 << CARDBUS_LATTIMER_SHIFT);
    341  1.4.2.2  skrll 		cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
    342  1.4.2.2  skrll 	}
    343  1.4.2.2  skrll }
    344