Home | History | Annotate | Line # | Download | only in cardbus
if_ath_cardbus.c revision 1.17
      1 /*	$NetBSD: if_ath_cardbus.c,v 1.17 2006/10/12 01:30:55 christos 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.17 2006/10/12 01:30:55 christos 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 <machine/bus.h>
     75 #include <machine/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 	void	*sc_sdhook;
    111 };
    112 
    113 int	ath_cardbus_match(struct device *, struct cfdata *, void *);
    114 void	ath_cardbus_attach(struct device *, struct device *, void *);
    115 int	ath_cardbus_detach(struct device *, int);
    116 void	ath_cardbus_shutdown(void *arg);
    117 
    118 CFATTACH_DECL(ath_cardbus, sizeof(struct ath_cardbus_softc),
    119     ath_cardbus_match, ath_cardbus_attach, ath_cardbus_detach, ath_activate);
    120 
    121 void	ath_cardbus_setup(struct ath_cardbus_softc *);
    122 
    123 int	ath_cardbus_enable(struct ath_softc *);
    124 void	ath_cardbus_disable(struct ath_softc *);
    125 void	ath_cardbus_power(struct ath_softc *, int);
    126 
    127 int
    128 ath_cardbus_match(struct device *parent __unused, struct cfdata *match __unused,
    129     void *aux)
    130 {
    131 	struct cardbus_attach_args *ca = aux;
    132 	const char* devname;
    133 
    134 	devname = ath_hal_probe(PCI_VENDOR(ca->ca_id),
    135 				PCI_PRODUCT(ca->ca_id));
    136 
    137 	if (devname)
    138 		return (1);
    139 
    140 	return (0);
    141 }
    142 
    143 void
    144 ath_cardbus_attach(struct device *parent __unused, struct device *self,
    145     void *aux)
    146 {
    147 	struct ath_cardbus_softc *csc = device_private(self);
    148 	struct ath_softc *sc = &csc->sc_ath;
    149 	struct cardbus_attach_args *ca = aux;
    150 	cardbus_devfunc_t ct = ca->ca_ct;
    151 	bus_addr_t adr;
    152 
    153 	sc->sc_dmat = ca->ca_dmat;
    154 	csc->sc_ct = ct;
    155 	csc->sc_tag = ca->ca_tag;
    156 
    157 	printf("\n");
    158 
    159 	/*
    160 	 * Power management hooks.
    161 	 */
    162 	sc->sc_enable = ath_cardbus_enable;
    163 	sc->sc_disable = ath_cardbus_disable;
    164 	sc->sc_power = ath_cardbus_power;
    165 
    166 	csc->sc_sdhook = shutdownhook_establish(ath_cardbus_shutdown, csc);
    167 	if (csc->sc_sdhook == NULL) {
    168 		aprint_error("couldn't establish shutdown hook\n");
    169 		return;
    170 	}
    171 
    172 	/*
    173 	 * Map the device.
    174 	 */
    175 	if (Cardbus_mapreg_map(ct, ATH_PCI_MMBA, CARDBUS_MAPREG_TYPE_MEM, 0,
    176 	    &csc->sc_iot, &csc->sc_ioh, &adr, &csc->sc_mapsize) == 0) {
    177 #if rbus
    178 #else
    179 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    180 #endif
    181 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
    182 	}
    183 
    184 	else {
    185 		printf("%s: unable to map device registers\n",
    186 		    sc->sc_dev.dv_xname);
    187 		shutdownhook_disestablish(csc->sc_sdhook);
    188 		return;
    189 	}
    190 
    191 	sc->sc_st = HALTAG(csc->sc_iot);
    192 	sc->sc_sh = HALHANDLE(csc->sc_ioh);
    193 
    194 	/*
    195 	 * Set up the PCI configuration registers.
    196 	 */
    197 	ath_cardbus_setup(csc);
    198 
    199 	/* Remember which interrupt line. */
    200 	csc->sc_intrline = ca->ca_intrline;
    201 
    202 	/*
    203 	 * Finish off the attach.
    204 	 */
    205 	ath_attach(PCI_PRODUCT(ca->ca_id), sc);
    206 
    207 #ifdef ath_powerdown
    208 	/*
    209 	 * Power down the socket.
    210 	 */
    211 	Cardbus_function_disable(csc->sc_ct);
    212 #endif /* ath_powerdown */
    213 }
    214 
    215 int
    216 ath_cardbus_detach(struct device *self, int flags __unused)
    217 {
    218 	struct ath_cardbus_softc *csc = device_private(self);
    219 	struct ath_softc *sc = &csc->sc_ath;
    220 	struct cardbus_devfunc *ct = csc->sc_ct;
    221 	int rv;
    222 
    223 #if defined(DIAGNOSTIC)
    224 	if (ct == NULL)
    225 		panic("%s: data structure lacks", sc->sc_dev.dv_xname);
    226 #endif
    227 
    228 	shutdownhook_disestablish(csc->sc_sdhook);
    229 
    230 	rv = ath_detach(sc);
    231 	if (rv)
    232 		return (rv);
    233 
    234 	/*
    235 	 * Unhook the interrupt handler.
    236 	 */
    237 	if (csc->sc_ih != NULL)
    238 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    239 		csc->sc_ih = NULL;
    240 
    241 	/*
    242 	 * Release bus space and close window.
    243 	 */
    244 	Cardbus_mapreg_unmap(ct, ATH_PCI_MMBA, csc->sc_iot, csc->sc_ioh,
    245 	    csc->sc_mapsize);
    246 
    247 	return (0);
    248 }
    249 
    250 void
    251 ath_cardbus_shutdown(void *arg)
    252 {
    253 	struct ath_cardbus_softc *csc;
    254 
    255 	csc = arg;
    256 
    257 	ath_shutdown(&csc->sc_ath);
    258 }
    259 
    260 int
    261 ath_cardbus_enable(struct ath_softc *sc)
    262 {
    263 	struct ath_cardbus_softc *csc = (void *) sc;
    264 	cardbus_devfunc_t ct = csc->sc_ct;
    265 	cardbus_chipset_tag_t cc = ct->ct_cc;
    266 	cardbus_function_tag_t cf = ct->ct_cf;
    267 
    268 	/*
    269 	 * Power on the socket.
    270 	 */
    271 	Cardbus_function_enable(ct);
    272 
    273 	/*
    274 	 * Set up the PCI configuration registers.
    275 	 */
    276 	ath_cardbus_setup(csc);
    277 
    278 	/*
    279 	 * Map and establish the interrupt.
    280 	 */
    281 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
    282 	    ath_intr, sc);
    283 	if (csc->sc_ih == NULL) {
    284 		printf("%s: unable to establish interrupt at %d\n",
    285 		    sc->sc_dev.dv_xname, csc->sc_intrline);
    286 		Cardbus_function_disable(csc->sc_ct);
    287 		return (1);
    288 	}
    289 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    290 		csc->sc_intrline);
    291 
    292 	return (0);
    293 }
    294 
    295 void
    296 ath_cardbus_disable(struct ath_softc *sc)
    297 {
    298 	struct ath_cardbus_softc *csc = (void *) sc;
    299 	cardbus_devfunc_t ct = csc->sc_ct;
    300 	cardbus_chipset_tag_t cc = ct->ct_cc;
    301 	cardbus_function_tag_t cf = ct->ct_cf;
    302 
    303 	/* Unhook the interrupt handler. */
    304 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    305 	csc->sc_ih = NULL;
    306 
    307 #ifdef ath_powerdown
    308 	/* Power down the socket. */
    309 	Cardbus_function_disable(ct);
    310 #endif /* ath_powerdown */
    311 }
    312 
    313 void
    314 ath_cardbus_power(struct ath_softc *sc, int why)
    315 {
    316 	struct ath_cardbus_softc *csc = (void *) sc;
    317 
    318 	printf("%s: ath_cardbus_power\n", sc->sc_dev.dv_xname);
    319 
    320 	if (why == PWR_RESUME) {
    321 		/*
    322 		 * Give the PCI configuration registers a kick
    323 		 * in the head.
    324 		 */
    325 #ifdef DIAGNOSTIC
    326 		if (ATH_IS_ENABLED(sc) == 0)
    327 			panic("ath_cardbus_power");
    328 #endif
    329 		ath_cardbus_setup(csc);
    330 	}
    331 }
    332 
    333 void
    334 ath_cardbus_setup(struct ath_cardbus_softc *csc)
    335 {
    336 	struct ath_softc *sc = &csc->sc_ath;
    337 	cardbus_devfunc_t ct = csc->sc_ct;
    338 	cardbus_chipset_tag_t cc = ct->ct_cc;
    339 	cardbus_function_tag_t cf = ct->ct_cf;
    340 	pcireg_t reg;
    341 
    342 	(void)cardbus_setpowerstate(sc->sc_dev.dv_xname, ct, csc->sc_tag,
    343 	    PCI_PWR_D0);
    344 
    345 	/* Program the BAR. */
    346 	cardbus_conf_write(cc, cf, csc->sc_tag, ATH_PCI_MMBA,
    347 	    csc->sc_bar_val);
    348 
    349 	/* Make sure the right access type is on the CardBus bridge. */
    350 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    351 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    352 
    353 	/* Enable the appropriate bits in the PCI CSR. */
    354 	reg = cardbus_conf_read(cc, cf, csc->sc_tag,
    355 	    CARDBUS_COMMAND_STATUS_REG);
    356 	reg |= CARDBUS_COMMAND_MASTER_ENABLE | CARDBUS_COMMAND_MEM_ENABLE;
    357 	cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_COMMAND_STATUS_REG,
    358 	    reg);
    359 
    360 	/*
    361 	 * Make sure the latency timer is set to some reasonable
    362 	 * value.
    363 	 */
    364 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
    365 	if (CARDBUS_LATTIMER(reg) < 0x20) {
    366 		reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    367 		reg |= (0x20 << CARDBUS_LATTIMER_SHIFT);
    368 		cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
    369 	}
    370 }
    371