Home | History | Annotate | Line # | Download | only in cardbus
if_ath_cardbus.c revision 1.34
      1 /*	$NetBSD: if_ath_cardbus.c,v 1.34 2009/09/05 14:50:10 tsutsui Exp $ */
      2 /*
      3  * Copyright (c) 2003
      4  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Ichiro FUKUHARA.
     18  * 4. The name of the company nor the name of the author may be used to
     19  *    endorse or promote products derived from this software without specific
     20  *    prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     26  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 /*
     35  * CardBus bus front-end for the AR5001 Wireless LAN 802.11a/b/g CardBus.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: if_ath_cardbus.c,v 1.34 2009/09/05 14:50:10 tsutsui Exp $");
     40 
     41 #include "opt_inet.h"
     42 #include "bpfilter.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/malloc.h>
     48 #include <sys/kernel.h>
     49 #include <sys/socket.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/errno.h>
     52 #include <sys/device.h>
     53 
     54 #include <machine/endian.h>
     55 
     56 #include <net/if.h>
     57 #include <net/if_dl.h>
     58 #include <net/if_media.h>
     59 #include <net/if_ether.h>
     60 
     61 #include <net80211/ieee80211_netbsd.h>
     62 #include <net80211/ieee80211_var.h>
     63 
     64 #if NBPFILTER > 0
     65 #include <net/bpf.h>
     66 #endif
     67 
     68 #ifdef INET
     69 #include <netinet/in.h>
     70 #include <netinet/if_inarp.h>
     71 #endif
     72 
     73 
     74 #include <sys/bus.h>
     75 #include <sys/intr.h>
     76 
     77 #include <dev/mii/miivar.h>
     78 #include <dev/mii/mii_bitbang.h>
     79 
     80 #include <dev/ic/ath_netbsd.h>
     81 #include <dev/ic/athvar.h>
     82 
     83 #include <external/isc/atheros_hal/dist/ah.h>
     84 
     85 #include <dev/pci/pcivar.h>
     86 #include <dev/pci/pcireg.h>
     87 #include <dev/pci/pcidevs.h>
     88 
     89 #include <dev/cardbus/cardbusvar.h>
     90 #include <dev/pci/pcidevs.h>
     91 
     92 /*
     93  * PCI configuration space registers
     94  */
     95 #define	ATH_PCI_MMBA		0x10	/* memory mapped base */
     96 
     97 struct ath_cardbus_softc {
     98 	struct ath_softc	sc_ath;
     99 
    100 	/* CardBus-specific goo. */
    101 	void	*sc_ih;			/* interrupt handle */
    102 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
    103 	cardbustag_t sc_tag;		/* our CardBus tag */
    104 	bus_size_t sc_mapsize;		/* the size of mapped bus space region */
    105 
    106 	pcireg_t sc_bar_val;		/* value of the BAR */
    107 
    108 	cardbus_intr_line_t sc_intrline; /* interrupt line */
    109 	bus_space_tag_t sc_iot;
    110 	bus_space_handle_t sc_ioh;
    111 };
    112 
    113 int	ath_cardbus_match(device_t, cfdata_t, void *);
    114 void	ath_cardbus_attach(device_t, device_t, void *);
    115 int	ath_cardbus_detach(device_t, int);
    116 
    117 CFATTACH_DECL_NEW(ath_cardbus, sizeof(struct ath_cardbus_softc),
    118     ath_cardbus_match, ath_cardbus_attach, ath_cardbus_detach, NULL);
    119 
    120 void	ath_cardbus_setup(struct ath_cardbus_softc *);
    121 
    122 static bool
    123 ath_cardbus_suspend(device_t self PMF_FN_ARGS)
    124 {
    125 	struct ath_cardbus_softc *csc = device_private(self);
    126 
    127 	ath_suspend(&csc->sc_ath);
    128 	if (csc->sc_ih != NULL) {
    129 		cardbus_intr_disestablish(csc->sc_ct->ct_cc, csc->sc_ct->ct_cf,
    130 		    csc->sc_ih);
    131 		csc->sc_ih = NULL;
    132 	}
    133 	return true;
    134 }
    135 
    136 static bool
    137 ath_cardbus_resume(device_t self PMF_FN_ARGS)
    138 {
    139 	struct ath_cardbus_softc *csc = device_private(self);
    140 
    141 	csc->sc_ih = cardbus_intr_establish(csc->sc_ct->ct_cc,
    142 	    csc->sc_ct->ct_cf, csc->sc_intrline, IPL_NET, ath_intr,
    143 	    &csc->sc_ath);
    144 
    145 	if (csc->sc_ih == NULL) {
    146 		aprint_error_dev(self,
    147 		    "unable to establish interrupt\n");
    148 		return false;
    149 	}
    150 
    151 	return ath_resume(&csc->sc_ath);
    152 }
    153 
    154 int
    155 ath_cardbus_match(device_t parent, cfdata_t match, void *aux)
    156 {
    157 	struct cardbus_attach_args *ca = aux;
    158 	const char *devname;
    159 
    160 	devname = ath_hal_probe(PCI_VENDOR(ca->ca_id), PCI_PRODUCT(ca->ca_id));
    161 
    162 	if (devname)
    163 		return 1;
    164 
    165 	return 0;
    166 }
    167 
    168 void
    169 ath_cardbus_attach(device_t parent, device_t self, void *aux)
    170 {
    171 	struct ath_cardbus_softc *csc = device_private(self);
    172 	struct ath_softc *sc = &csc->sc_ath;
    173 	struct cardbus_attach_args *ca = aux;
    174 	cardbus_devfunc_t ct = ca->ca_ct;
    175 	bus_addr_t adr;
    176 
    177 	sc->sc_dev = self;
    178 	sc->sc_dmat = ca->ca_dmat;
    179 	csc->sc_ct = ct;
    180 	csc->sc_tag = ca->ca_tag;
    181 
    182 	aprint_normal("\n");
    183 
    184 	/*
    185 	 * Map the device.
    186 	 */
    187 	if (Cardbus_mapreg_map(ct, ATH_PCI_MMBA, PCI_MAPREG_TYPE_MEM, 0,
    188 	    &csc->sc_iot, &csc->sc_ioh, &adr, &csc->sc_mapsize) == 0) {
    189 #if rbus
    190 #else
    191 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    192 #endif
    193 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
    194 	} else {
    195 		aprint_error_dev(self, "unable to map device registers\n");
    196 		return;
    197 	}
    198 
    199 	sc->sc_st = HALTAG(csc->sc_iot);
    200 	sc->sc_sh = HALHANDLE(csc->sc_ioh);
    201 
    202 	/*
    203 	 * Set up the PCI configuration registers.
    204 	 */
    205 	ath_cardbus_setup(csc);
    206 
    207 	/* Remember which interrupt line. */
    208 	csc->sc_intrline = ca->ca_intrline;
    209 
    210 	ATH_LOCK_INIT(sc);
    211 
    212 	/*
    213 	 * Finish off the attach.
    214 	 */
    215 	if (ath_attach(PCI_PRODUCT(ca->ca_id), sc) != 0)
    216 		return;
    217 
    218 	if (pmf_device_register(self,
    219 	    ath_cardbus_suspend, ath_cardbus_resume)) {
    220 		pmf_class_network_register(self, &sc->sc_if);
    221 		pmf_device_suspend_self(self);
    222 	} else
    223 		aprint_error_dev(self, "couldn't establish power handler\n");
    224 }
    225 
    226 int
    227 ath_cardbus_detach(device_t self, int flags)
    228 {
    229 	struct ath_cardbus_softc *csc = device_private(self);
    230 	struct ath_softc *sc = &csc->sc_ath;
    231 	struct cardbus_devfunc *ct = csc->sc_ct;
    232 	int rv;
    233 
    234 #if defined(DIAGNOSTIC)
    235 	if (ct == NULL)
    236 		panic("%s: data structure lacks", device_xname(sc->sc_dev));
    237 #endif
    238 
    239 	rv = ath_detach(sc);
    240 	if (rv)
    241 		return (rv);
    242 
    243 	pmf_device_deregister(self);
    244 
    245 	/*
    246 	 * Unhook the interrupt handler.
    247 	 */
    248 	if (csc->sc_ih != NULL) {
    249 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    250 		csc->sc_ih = NULL;
    251 	}
    252 
    253 	/*
    254 	 * Release bus space and close window.
    255 	 */
    256 	Cardbus_mapreg_unmap(ct, ATH_PCI_MMBA, csc->sc_iot, csc->sc_ioh,
    257 	    csc->sc_mapsize);
    258 
    259 	ATH_LOCK_DESTROY(sc);
    260 
    261 	return (0);
    262 }
    263 
    264 void
    265 ath_cardbus_setup(struct ath_cardbus_softc *csc)
    266 {
    267 	cardbus_devfunc_t ct = csc->sc_ct;
    268 	cardbus_chipset_tag_t cc = ct->ct_cc;
    269 	cardbus_function_tag_t cf = ct->ct_cf;
    270 	int rc;
    271 	pcireg_t reg;
    272 
    273 	if ((rc = cardbus_set_powerstate(ct, csc->sc_tag, PCI_PWR_D0)) != 0)
    274 		aprint_debug("%s: cardbus_set_powerstate %d\n", __func__, rc);
    275 
    276 	/* Program the BAR. */
    277 	cardbus_conf_write(cc, cf, csc->sc_tag, ATH_PCI_MMBA, csc->sc_bar_val);
    278 
    279 	/* Enable the appropriate bits in the PCI CSR. */
    280 	reg = cardbus_conf_read(cc, cf, csc->sc_tag,
    281 	    PCI_COMMAND_STATUS_REG);
    282 	reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
    283 	cardbus_conf_write(cc, cf, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
    284 }
    285