Home | History | Annotate | Line # | Download | only in cardbus
if_ath_cardbus.c revision 1.20.2.1
      1 /*	$NetBSD: if_ath_cardbus.c,v 1.20.2.1 2007/12/26 19:46:05 ad 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.20.2.1 2007/12/26 19:46:05 ad 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 #include <contrib/dev/ath/ah.h>
     83 
     84 #include <dev/pci/pcivar.h>
     85 #include <dev/pci/pcireg.h>
     86 #include <dev/pci/pcidevs.h>
     87 
     88 #include <dev/cardbus/cardbusvar.h>
     89 #include <dev/pci/pcidevs.h>
     90 
     91 /*
     92  * PCI configuration space registers
     93  */
     94 #define	ATH_PCI_MMBA		0x10	/* memory mapped base */
     95 
     96 struct ath_cardbus_softc {
     97 	struct ath_softc	sc_ath;
     98 
     99 	/* CardBus-specific goo. */
    100 	void	*sc_ih;			/* interrupt handle */
    101 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
    102 	cardbustag_t sc_tag;		/* our CardBus tag */
    103 	bus_size_t sc_mapsize;		/* the size of mapped bus space region */
    104 
    105 	pcireg_t sc_bar_val;		/* value of the BAR */
    106 
    107 	int	sc_intrline;		/* interrupt line */
    108 	bus_space_tag_t sc_iot;
    109 	bus_space_handle_t sc_ioh;
    110 };
    111 
    112 int	ath_cardbus_match(struct device *, struct cfdata *, void *);
    113 void	ath_cardbus_attach(struct device *, struct device *, void *);
    114 int	ath_cardbus_detach(struct device *, int);
    115 
    116 CFATTACH_DECL(ath_cardbus, sizeof(struct ath_cardbus_softc),
    117     ath_cardbus_match, ath_cardbus_attach, ath_cardbus_detach, ath_activate);
    118 
    119 void	ath_cardbus_setup(struct ath_cardbus_softc *);
    120 
    121 int	ath_cardbus_enable(struct ath_softc *);
    122 void	ath_cardbus_disable(struct ath_softc *);
    123 
    124 static bool
    125 ath_cardbus_resume(device_t dv)
    126 {
    127 	struct ath_cardbus_softc *csc = device_private(dv);
    128 
    129 	/* Insofar as I understand what the PCI retry timeout is
    130 	 * (it does not appear to be documented in any PCI standard,
    131 	 * and we don't have any Atheros documentation), disabling
    132 	 * it on resume does not seem to be justified.
    133 	 *
    134 	 * Taking a guess, the DMA engine counts down from the
    135 	 * retry timeout to 0 while it retries a delayed PCI
    136 	 * transaction.  When it reaches 0, it ceases retrying.
    137 	 * A PCI master is *never* supposed to stop retrying a
    138 	 * delayed transaction, though.
    139 	 *
    140 	 * Incidentally, while I am hopeful that cardbus_disable_retry()
    141 	 * does disable retries, because that would help to explain
    142 	 * some ath(4) lossage, I suspect that writing 0 to the
    143 	 * register does not disable *retries*, but it disables
    144 	 * the timeout.  That is, the device will *never* timeout.
    145 	 */
    146 #if 0
    147 	cardbus_devfunc_t ct = csc->sc_ct;
    148 	cardbus_chipset_tag_t cc = ct->ct_cc;
    149 	cardbus_function_tag_t cf = ct->ct_cf;
    150 	cardbus_disable_retry(cc, cf, csc->sc_tag);
    151 #endif
    152 	ath_resume(&csc->sc_ath);
    153 
    154 	return true;
    155 }
    156 
    157 int
    158 ath_cardbus_match(struct device *parent, struct cfdata *match,
    159     void *aux)
    160 {
    161 	struct cardbus_attach_args *ca = aux;
    162 	const char* devname;
    163 
    164 	devname = ath_hal_probe(PCI_VENDOR(ca->ca_id),
    165 				PCI_PRODUCT(ca->ca_id));
    166 
    167 	if (devname)
    168 		return (1);
    169 
    170 	return (0);
    171 }
    172 
    173 void
    174 ath_cardbus_attach(struct device *parent, struct device *self,
    175     void *aux)
    176 {
    177 	struct ath_cardbus_softc *csc = device_private(self);
    178 	struct ath_softc *sc = &csc->sc_ath;
    179 	struct cardbus_attach_args *ca = aux;
    180 	cardbus_devfunc_t ct = ca->ca_ct;
    181 	bus_addr_t adr;
    182 
    183 	sc->sc_dmat = ca->ca_dmat;
    184 	csc->sc_ct = ct;
    185 	csc->sc_tag = ca->ca_tag;
    186 
    187 	printf("\n");
    188 
    189 	/*
    190 	 * Power management hooks.
    191 	 */
    192 	sc->sc_enable = ath_cardbus_enable;
    193 	sc->sc_disable = ath_cardbus_disable;
    194 
    195 	/*
    196 	 * Map the device.
    197 	 */
    198 	if (Cardbus_mapreg_map(ct, ATH_PCI_MMBA, CARDBUS_MAPREG_TYPE_MEM, 0,
    199 	    &csc->sc_iot, &csc->sc_ioh, &adr, &csc->sc_mapsize) == 0) {
    200 #if rbus
    201 #else
    202 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    203 #endif
    204 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
    205 	}
    206 
    207 	else {
    208 		printf("%s: unable to map device registers\n",
    209 		    sc->sc_dev.dv_xname);
    210 		return;
    211 	}
    212 
    213 	sc->sc_st = HALTAG(csc->sc_iot);
    214 	sc->sc_sh = HALHANDLE(csc->sc_ioh);
    215 
    216 	/*
    217 	 * Set up the PCI configuration registers.
    218 	 */
    219 	ath_cardbus_setup(csc);
    220 
    221 	/* Remember which interrupt line. */
    222 	csc->sc_intrline = ca->ca_intrline;
    223 
    224 	ATH_LOCK_INIT(sc);
    225 
    226 	if (!pmf_device_register(self, NULL, ath_cardbus_resume))
    227 		aprint_error_dev(self, "couldn't establish power handler\n");
    228 	else
    229 		pmf_class_network_register(self, &sc->sc_if);
    230 
    231 	/*
    232 	 * Finish off the attach.
    233 	 */
    234 	ath_attach(PCI_PRODUCT(ca->ca_id), sc);
    235 }
    236 
    237 int
    238 ath_cardbus_detach(struct device *self, int flags)
    239 {
    240 	struct ath_cardbus_softc *csc = device_private(self);
    241 	struct ath_softc *sc = &csc->sc_ath;
    242 	struct cardbus_devfunc *ct = csc->sc_ct;
    243 	int rv;
    244 
    245 #if defined(DIAGNOSTIC)
    246 	if (ct == NULL)
    247 		panic("%s: data structure lacks", sc->sc_dev.dv_xname);
    248 #endif
    249 
    250 	rv = ath_detach(sc);
    251 	if (rv)
    252 		return (rv);
    253 
    254 	pmf_device_deregister(self);
    255 
    256 	/*
    257 	 * Unhook the interrupt handler.
    258 	 */
    259 	if (csc->sc_ih != NULL) {
    260 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    261 		csc->sc_ih = NULL;
    262 	}
    263 
    264 	/*
    265 	 * Release bus space and close window.
    266 	 */
    267 	Cardbus_mapreg_unmap(ct, ATH_PCI_MMBA, csc->sc_iot, csc->sc_ioh,
    268 	    csc->sc_mapsize);
    269 
    270 	ATH_LOCK_DESTROY(sc);
    271 
    272 	return (0);
    273 }
    274 
    275 int
    276 ath_cardbus_enable(struct ath_softc *sc)
    277 {
    278 	struct ath_cardbus_softc *csc = (void *) sc;
    279 	cardbus_devfunc_t ct = csc->sc_ct;
    280 	cardbus_chipset_tag_t cc = ct->ct_cc;
    281 	cardbus_function_tag_t cf = ct->ct_cf;
    282 
    283 	/*
    284 	 * Power on the socket.
    285 	 */
    286 	Cardbus_function_enable(ct);
    287 
    288 	/*
    289 	 * Set up the PCI configuration registers.
    290 	 */
    291 	ath_cardbus_setup(csc);
    292 
    293 	/*
    294 	 * Map and establish the interrupt.
    295 	 */
    296 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
    297 	    ath_intr, sc);
    298 	if (csc->sc_ih == NULL) {
    299 		printf("%s: unable to establish interrupt at %d\n",
    300 		    sc->sc_dev.dv_xname, csc->sc_intrline);
    301 		Cardbus_function_disable(csc->sc_ct);
    302 		return (1);
    303 	}
    304 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    305 		csc->sc_intrline);
    306 
    307 	return (0);
    308 }
    309 
    310 void
    311 ath_cardbus_disable(struct ath_softc *sc)
    312 {
    313 	struct ath_cardbus_softc *csc = (void *) sc;
    314 	cardbus_devfunc_t ct = csc->sc_ct;
    315 	cardbus_chipset_tag_t cc = ct->ct_cc;
    316 	cardbus_function_tag_t cf = ct->ct_cf;
    317 
    318 	/* Unhook the interrupt handler. */
    319 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    320 	csc->sc_ih = NULL;
    321 }
    322 
    323 void
    324 ath_cardbus_setup(struct ath_cardbus_softc *csc)
    325 {
    326 	cardbus_devfunc_t ct = csc->sc_ct;
    327 	cardbus_chipset_tag_t cc = ct->ct_cc;
    328 	cardbus_function_tag_t cf = ct->ct_cf;
    329 	int error;
    330 	pcireg_t reg;
    331 
    332 	if ((error = cardbus_set_powerstate(ct, csc->sc_tag,
    333 	    PCI_PWR_D0)) != 0)
    334 		aprint_debug("%s: cardbus_set_powerstate %d\n", __func__, error);
    335 
    336 	/* Program the BAR. */
    337 	cardbus_conf_write(cc, cf, csc->sc_tag, ATH_PCI_MMBA,
    338 	    csc->sc_bar_val);
    339 
    340 	/* Make sure the right access type is on the CardBus bridge. */
    341 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    342 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    343 
    344 	/* Enable the appropriate bits in the PCI CSR. */
    345 	reg = cardbus_conf_read(cc, cf, csc->sc_tag,
    346 	    CARDBUS_COMMAND_STATUS_REG);
    347 	reg |= CARDBUS_COMMAND_MASTER_ENABLE | CARDBUS_COMMAND_MEM_ENABLE;
    348 	cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_COMMAND_STATUS_REG,
    349 	    reg);
    350 }
    351