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