Home | History | Annotate | Line # | Download | only in cardbus
if_rtw_cardbus.c revision 1.26
      1 /* $NetBSD: if_rtw_cardbus.c,v 1.26 2008/05/14 19:24:48 dyoung Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
      5  *
      6  * Adapted for the RTL8180 by David Young.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of David Young may not be used to endorse or promote
     17  *    products derived from this software without specific prior
     18  *    written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
     21  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     23  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL David
     24  * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     26  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     31  * OF SUCH DAMAGE.
     32  */
     33 /*-
     34  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
     35  * All rights reserved.
     36  *
     37  * This code is derived from software contributed to The NetBSD Foundation
     38  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
     39  * NASA Ames Research Center.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     51  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     52  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     53  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     54  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     60  * POSSIBILITY OF SUCH DAMAGE.
     61  */
     62 
     63 /*
     64  * Cardbus front-end for the Realtek RTL8180 802.11 MAC/BBP driver.
     65  *
     66  * TBD factor with atw, tlp Cardbus front-ends?
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: if_rtw_cardbus.c,v 1.26 2008/05/14 19:24:48 dyoung Exp $");
     71 
     72 #include "opt_inet.h"
     73 #include "bpfilter.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/mbuf.h>
     78 #include <sys/malloc.h>
     79 #include <sys/kernel.h>
     80 #include <sys/socket.h>
     81 #include <sys/ioctl.h>
     82 #include <sys/errno.h>
     83 #include <sys/device.h>
     84 
     85 #include <machine/endian.h>
     86 
     87 #include <net/if.h>
     88 #include <net/if_dl.h>
     89 #include <net/if_media.h>
     90 #include <net/if_ether.h>
     91 
     92 #include <net80211/ieee80211_netbsd.h>
     93 #include <net80211/ieee80211_radiotap.h>
     94 #include <net80211/ieee80211_var.h>
     95 
     96 #if NBPFILTER > 0
     97 #include <net/bpf.h>
     98 #endif
     99 
    100 #ifdef INET
    101 #include <netinet/in.h>
    102 #include <netinet/if_inarp.h>
    103 #endif
    104 
    105 
    106 #include <sys/bus.h>
    107 #include <sys/intr.h>
    108 
    109 #include <dev/ic/rtwreg.h>
    110 #include <dev/ic/rtwvar.h>
    111 
    112 #include <dev/pci/pcivar.h>
    113 #include <dev/pci/pcireg.h>
    114 #include <dev/pci/pcidevs.h>
    115 
    116 #include <dev/cardbus/cardbusvar.h>
    117 #include <dev/pci/pcidevs.h>
    118 
    119 /*
    120  * PCI configuration space registers used by the RTL8180.
    121  */
    122 #define	RTW_PCI_IOBA		0x10	/* i/o mapped base */
    123 #define	RTW_PCI_MMBA		0x14	/* memory mapped base */
    124 
    125 struct rtw_cardbus_softc {
    126 	struct rtw_softc sc_rtw;	/* real RTL8180 softc */
    127 
    128 	/* CardBus-specific goo. */
    129 	void			*sc_ih;		/* interrupt handle */
    130 	cardbus_devfunc_t	sc_ct;		/* our CardBus devfuncs */
    131 	cardbustag_t		sc_tag;		/* our CardBus tag */
    132 	int			sc_csr;		/* CSR bits */
    133 	bus_size_t		sc_mapsize;	/* size of the mapped bus space
    134 						 * region
    135 						 */
    136 
    137 	int			sc_bar_reg;	/* which BAR to use */
    138 	pcireg_t		sc_bar_val;	/* value of the BAR */
    139 
    140 	int			sc_intrline;	/* interrupt line */
    141 };
    142 
    143 int	rtw_cardbus_match(device_t, struct cfdata *, void *);
    144 void	rtw_cardbus_attach(device_t, device_t, void *);
    145 int	rtw_cardbus_detach(device_t, int);
    146 
    147 CFATTACH_DECL_NEW(rtw_cardbus, sizeof(struct rtw_cardbus_softc),
    148     rtw_cardbus_match, rtw_cardbus_attach, rtw_cardbus_detach, NULL);
    149 
    150 void	rtw_cardbus_setup(struct rtw_cardbus_softc *);
    151 
    152 bool rtw_cardbus_resume(device_t PMF_FN_PROTO);
    153 bool rtw_cardbus_suspend(device_t PMF_FN_PROTO);
    154 
    155 const struct rtw_cardbus_product *rtw_cardbus_lookup(
    156      const struct cardbus_attach_args *);
    157 
    158 const struct rtw_cardbus_product {
    159 	u_int32_t	 rcp_vendor;	/* PCI vendor ID */
    160 	u_int32_t	 rcp_product;	/* PCI product ID */
    161 	const char	*rcp_product_name;
    162 } rtw_cardbus_products[] = {
    163 	{ PCI_VENDOR_REALTEK,		PCI_PRODUCT_REALTEK_RT8180,
    164 	  "Realtek RTL8180 802.11 MAC/BBP" },
    165 
    166 	{ PCI_VENDOR_BELKIN,		PCI_PRODUCT_BELKIN_F5D6020V3,
    167 	  "Belkin F5D6020v3 802.11b (RTL8180 MAC/BBP)" },
    168 
    169 	{ PCI_VENDOR_DLINK,		PCI_PRODUCT_DLINK_DWL610,
    170 	  "DWL-610 D-Link Air 802.11b (RTL8180 MAC/BBP)" },
    171 
    172 	{ 0,				0,	NULL },
    173 };
    174 
    175 const struct rtw_cardbus_product *
    176 rtw_cardbus_lookup(const struct cardbus_attach_args *ca)
    177 {
    178 	const struct rtw_cardbus_product *rcp;
    179 
    180 	for (rcp = rtw_cardbus_products; rcp->rcp_product_name != NULL; rcp++) {
    181 		if (PCI_VENDOR(ca->ca_id) == rcp->rcp_vendor &&
    182 		    PCI_PRODUCT(ca->ca_id) == rcp->rcp_product)
    183 			return rcp;
    184 	}
    185 	return NULL;
    186 }
    187 
    188 int
    189 rtw_cardbus_match(device_t parent, struct cfdata *match, void *aux)
    190 {
    191 	struct cardbus_attach_args *ca = aux;
    192 
    193 	if (rtw_cardbus_lookup(ca) != NULL)
    194 		return 1;
    195 
    196 	return 0;
    197 }
    198 
    199 static void
    200 rtw_cardbus_intr_ack(struct rtw_regs *regs)
    201 {
    202 	RTW_WRITE(regs, RTW_FER, RTW_FER_INTR);
    203 }
    204 
    205 static void
    206 rtw_cardbus_funcregen(struct rtw_regs *regs, int enable)
    207 {
    208 	u_int32_t reg;
    209 	rtw_config0123_enable(regs, 1);
    210 	reg = RTW_READ(regs, RTW_CONFIG3);
    211 	if (enable)
    212 		RTW_WRITE(regs, RTW_CONFIG3, reg | RTW_CONFIG3_FUNCREGEN);
    213 	else
    214 		RTW_WRITE(regs, RTW_CONFIG3, reg & ~RTW_CONFIG3_FUNCREGEN);
    215 	rtw_config0123_enable(regs, 0);
    216 }
    217 
    218 void
    219 rtw_cardbus_attach(device_t parent, device_t self, void *aux)
    220 {
    221 	struct rtw_cardbus_softc *csc = device_private(self);
    222 	struct rtw_softc *sc = &csc->sc_rtw;
    223 	struct rtw_regs *regs = &sc->sc_regs;
    224 	struct cardbus_attach_args *ca = aux;
    225 	cardbus_devfunc_t ct = ca->ca_ct;
    226 	const struct rtw_cardbus_product *rcp;
    227 	bus_addr_t adr;
    228 	int rev;
    229 
    230 	sc->sc_dev = self;
    231 	sc->sc_dmat = ca->ca_dmat;
    232 	csc->sc_ct = ct;
    233 	csc->sc_tag = ca->ca_tag;
    234 
    235 	rcp = rtw_cardbus_lookup(ca);
    236 	if (rcp == NULL) {
    237 		printf("\n");
    238 		panic("rtw_cardbus_attach: impossible");
    239 	}
    240 
    241 	sc->sc_intr_ack = rtw_cardbus_intr_ack;
    242 
    243 	/* Get revision info. */
    244 	rev = PCI_REVISION(ca->ca_class);
    245 
    246 	printf(": %s\n", rcp->rcp_product_name);
    247 
    248 	RTW_DPRINTF(RTW_DEBUG_ATTACH,
    249 	    ("%s: pass %d.%d signature %08x\n", device_xname(self),
    250 	     (rev >> 4) & 0xf, rev & 0xf,
    251 	     cardbus_conf_read(ct->ct_cc, ct->ct_cf, csc->sc_tag, 0x80)));
    252 
    253 	/*
    254 	 * Map the device.
    255 	 */
    256 	csc->sc_csr = CARDBUS_COMMAND_MASTER_ENABLE |
    257 	              CARDBUS_COMMAND_PARITY_ENABLE |
    258 		      CARDBUS_COMMAND_SERR_ENABLE;
    259 	if (Cardbus_mapreg_map(ct, RTW_PCI_MMBA, CARDBUS_MAPREG_TYPE_MEM, 0,
    260 	    &regs->r_bt, &regs->r_bh, &adr, &regs->r_sz) == 0) {
    261 		RTW_DPRINTF(RTW_DEBUG_ATTACH,
    262 		    ("%s: %s mapped %" PRIuMAX " bytes mem space\n",
    263 		     device_xname(self), __func__, (uintmax_t)regs->r_sz));
    264 #if rbus
    265 #else
    266 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    267 #endif
    268 		csc->sc_csr |= CARDBUS_COMMAND_MEM_ENABLE;
    269 		csc->sc_bar_reg = RTW_PCI_MMBA;
    270 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
    271 	} else if (Cardbus_mapreg_map(ct, RTW_PCI_IOBA, CARDBUS_MAPREG_TYPE_IO,
    272 	    0, &regs->r_bt, &regs->r_bh, &adr, &regs->r_sz) == 0) {
    273 		RTW_DPRINTF(RTW_DEBUG_ATTACH,
    274 		    ("%s: %s mapped %" PRIuMAX " bytes I/O space\n",
    275 		     device_xname(self), __func__, (uintmax_t)regs->r_sz));
    276 #if rbus
    277 #else
    278 		(*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
    279 #endif
    280 		csc->sc_csr |= CARDBUS_COMMAND_IO_ENABLE;
    281 		csc->sc_bar_reg = RTW_PCI_IOBA;
    282 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_IO;
    283 	} else {
    284 		aprint_error_dev(self, "unable to map device registers\n");
    285 		return;
    286 	}
    287 
    288 	/*
    289 	 * Bring the chip out of powersave mode and initialize the
    290 	 * configuration registers.
    291 	 */
    292 	rtw_cardbus_setup(csc);
    293 
    294 	/* Remember which interrupt line. */
    295 	csc->sc_intrline = ca->ca_intrline;
    296 
    297 	aprint_normal_dev(self, "interrupting at %d\n", csc->sc_intrline);
    298 	/*
    299 	 * Finish off the attach.
    300 	 */
    301 	rtw_attach(sc);
    302 
    303 	rtw_cardbus_funcregen(regs, 1);
    304 
    305 	RTW_WRITE(regs, RTW_FEMR, RTW_FEMR_INTR);
    306 	RTW_WRITE(regs, RTW_FER, RTW_FER_INTR);
    307 
    308 	if (!pmf_device_register(self, rtw_cardbus_suspend, rtw_cardbus_resume))
    309 		aprint_error_dev(self, "couldn't establish power handler\n");
    310 	else {
    311 		pmf_class_network_register(self, &sc->sc_if);
    312 		/*
    313 		 * Power down the socket.
    314 		 */
    315 		pmf_device_suspend_self(self);
    316 	}
    317 }
    318 
    319 int
    320 rtw_cardbus_detach(device_t self, int flags)
    321 {
    322 	struct rtw_cardbus_softc *csc = device_private(self);
    323 	struct rtw_softc *sc = &csc->sc_rtw;
    324 	struct rtw_regs *regs = &sc->sc_regs;
    325 	struct cardbus_devfunc *ct = csc->sc_ct;
    326 	int rc;
    327 
    328 #if defined(DIAGNOSTIC)
    329 	if (ct == NULL)
    330 		panic("%s: data structure lacks", device_xname(self));
    331 #endif
    332 
    333 	if ((rc = rtw_detach(sc)) != 0)
    334 		return rc;
    335 
    336 	/*
    337 	 * Unhook the interrupt handler.
    338 	 */
    339 	if (csc->sc_ih != NULL)
    340 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    341 
    342 	/*
    343 	 * Release bus space and close window.
    344 	 */
    345 	if (csc->sc_bar_reg != 0)
    346 		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
    347 		    regs->r_bt, regs->r_bh, regs->r_sz);
    348 
    349 	return 0;
    350 }
    351 
    352 bool
    353 rtw_cardbus_resume(device_t self PMF_FN_ARGS)
    354 {
    355 	struct rtw_cardbus_softc *csc = device_private(self);
    356 	struct rtw_softc *sc = &csc->sc_rtw;
    357 	cardbus_devfunc_t ct = csc->sc_ct;
    358 	cardbus_chipset_tag_t cc = ct->ct_cc;
    359 	cardbus_function_tag_t cf = ct->ct_cf;
    360 
    361 	/*
    362 	 * Map and establish the interrupt.
    363 	 */
    364 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
    365 	    rtw_intr, sc);
    366 	if (csc->sc_ih == NULL) {
    367 		aprint_error_dev(sc->sc_dev,
    368 		    "unable to establish interrupt at %d\n", csc->sc_intrline);
    369 		return false;
    370 	}
    371 
    372 	rtw_cardbus_funcregen(&sc->sc_regs, 1);
    373 
    374 	RTW_WRITE(&sc->sc_regs, RTW_FEMR, RTW_FEMR_INTR);
    375 	RTW_WRITE(&sc->sc_regs, RTW_FER, RTW_FER_INTR);
    376 
    377 	return rtw_resume(self, flags);
    378 }
    379 
    380 bool
    381 rtw_cardbus_suspend(device_t self PMF_FN_ARGS)
    382 {
    383 	struct rtw_cardbus_softc *csc = device_private(self);
    384 	struct rtw_softc *sc = &csc->sc_rtw;
    385 	cardbus_devfunc_t ct = csc->sc_ct;
    386 	cardbus_chipset_tag_t cc = ct->ct_cc;
    387 	cardbus_function_tag_t cf = ct->ct_cf;
    388 
    389 	if (!rtw_suspend(self, flags))
    390 		return false;
    391 
    392 	RTW_WRITE(&sc->sc_regs, RTW_FEMR,
    393 	    RTW_READ(&sc->sc_regs, RTW_FEMR) & ~RTW_FEMR_INTR);
    394 
    395 	rtw_cardbus_funcregen(&sc->sc_regs, 0);
    396 
    397 	/* Unhook the interrupt handler. */
    398 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    399 	csc->sc_ih = NULL;
    400 	return true;
    401 }
    402 
    403 void
    404 rtw_cardbus_setup(struct rtw_cardbus_softc *csc)
    405 {
    406 	cardbustag_t tag = csc->sc_tag;
    407 	cardbus_devfunc_t ct = csc->sc_ct;
    408 	cardbus_chipset_tag_t cc = ct->ct_cc;
    409 	cardbusreg_t bhlc, csr, lattimer;
    410 	cardbus_function_tag_t cf = ct->ct_cf;
    411 
    412 	(void)cardbus_set_powerstate(ct, tag, PCI_PWR_D0);
    413 
    414 	/* I believe the datasheet tries to warn us that the RTL8180
    415 	 * wants for 16 (0x10) to divide the latency timer.
    416 	 */
    417 	bhlc = cardbus_conf_read(cc, cf, tag, CARDBUS_BHLC_REG);
    418 	lattimer = rounddown(PCI_LATTIMER(bhlc), 0x10);
    419 	if (PCI_LATTIMER(bhlc) != lattimer) {
    420 		bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    421 		bhlc |= (lattimer << PCI_LATTIMER_SHIFT);
    422 		cardbus_conf_write(cc, cf, tag, CARDBUS_BHLC_REG, bhlc);
    423 	}
    424 
    425 	/* Program the BAR. */
    426 	cardbus_conf_write(cc, cf, tag, csc->sc_bar_reg, csc->sc_bar_val);
    427 
    428 	/* Enable the appropriate bits in the PCI CSR. */
    429 	csr = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG);
    430 	csr &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    431 	csr |= csc->sc_csr;
    432 	cardbus_conf_write(cc, cf, tag, PCI_COMMAND_STATUS_REG, csr);
    433 }
    434