Home | History | Annotate | Line # | Download | only in cardbus
if_rtk_cardbus.c revision 1.15
      1 /*	$NetBSD: if_rtk_cardbus.c,v 1.15 2003/06/26 21:14:44 ichiro Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2000 Masanori Kanaoka
      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. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * if_rtk_cardbus.c:
     32  *	Cardbus specific routines for RealTek 8139 ethernet adapter.
     33  *	Tested for
     34  *		- elecom-Laneed	LD-10/100CBA (Accton MPX5030)
     35  *		- MELCO		LPC3-TX-CB   (RealTek 8139)
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: if_rtk_cardbus.c,v 1.15 2003/06/26 21:14:44 ichiro Exp $");
     40 
     41 #include "opt_inet.h"
     42 #include "opt_ns.h"
     43 #include "bpfilter.h"
     44 #include "rnd.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/callout.h>
     49 #include <sys/device.h>
     50 #include <sys/sockio.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/malloc.h>
     53 #include <sys/kernel.h>
     54 #include <sys/socket.h>
     55 
     56 #include <net/if.h>
     57 #include <net/if_arp.h>
     58 #include <net/if_ether.h>
     59 #include <net/if_dl.h>
     60 #include <net/if_media.h>
     61 #ifdef INET
     62 #include <netinet/in.h>
     63 #include <netinet/if_inarp.h>
     64 #endif
     65 #ifdef NS
     66 #include <netns/ns.h>
     67 #include <netns/ns_if.h>
     68 #endif
     69 
     70 #if NBPFILTER > 0
     71 #include <net/bpf.h>
     72 #endif
     73 #if NRND > 0
     74 #include <sys/rnd.h>
     75 #endif
     76 
     77 #include <machine/bus.h>
     78 
     79 #include <dev/pci/pcireg.h>
     80 #include <dev/pci/pcivar.h>
     81 #include <dev/pci/pcidevs.h>
     82 
     83 #include <dev/cardbus/cardbusvar.h>
     84 #include <dev/cardbus/cardbusdevs.h>
     85 
     86 #include <dev/mii/mii.h>
     87 #include <dev/mii/miivar.h>
     88 
     89 /*
     90  * Default to using PIO access for this driver. On SMP systems,
     91  * there appear to be problems with memory mapped mode: it looks like
     92  * doing too many memory mapped access back to back in rapid succession
     93  * can hang the bus. I'm inclined to blame this on crummy design/construction
     94  * on the part of RealTek. Memory mapped mode does appear to work on
     95  * uniprocessor systems though.
     96  */
     97 #define RTK_USEIOSPACE
     98 
     99 #include <dev/ic/rtl81x9reg.h>
    100 #include <dev/ic/rtl81x9var.h>
    101 
    102 /*
    103  * Various supported device vendors/types and their names.
    104  */
    105 static const struct rtk_type rtk_cardbus_devs[] = {
    106 	{ CARDBUS_VENDOR_ACCTON, CARDBUS_PRODUCT_ACCTON_MPX5030,
    107 		"Accton MPX 5030/5038 10/100BaseTX",
    108 		RTK_8139 },
    109 	{ CARDBUS_VENDOR_DLINK, CARDBUS_PRODUCT_DLINK_DFE_690TXD,
    110 		"D-Link DFE-690TXD 10/100BaseTX", RTK_8139 },
    111 	{ CARDBUS_VENDOR_REALTEK, CARDBUS_PRODUCT_REALTEK_RT8138,
    112 		"RealTek 8138 10/100BaseTX", RTK_8139 },
    113 	{ CARDBUS_VENDOR_REALTEK, CARDBUS_PRODUCT_REALTEK_RT8139,
    114 		"RealTek 8139 10/100BaseTX", RTK_8139 },
    115 	{ CARDBUS_VENDOR_COREGA, CARDBUS_PRODUCT_COREGA_CB_TXD,
    116 		"Corega FEther CB-TXD 10/100BaseTX", RTK_8139 },
    117 	{ CARDBUS_VENDOR_COREGA, CARDBUS_PRODUCT_COREGA_2CB_TXD,
    118 		"Corega FEther II CB-TXD 10/100BaseTX", RTK_8139 },
    119 	{ CARDBUS_VENDOR_PLANEX, CARDBUS_PRODUCT_PLANEX_FNW_3603_TX,
    120 		"Planex FNW-3603 10/100BaseTX", RTK_8139 },
    121 	{ CARDBUS_VENDOR_PLANEX, CARDBUS_PRODUCT_PLANEX_FNW_3800_TX,
    122 		"Planex 10/100BaseTX FNW-3800-TX", RTK_8139 },
    123 	{ CARDBUS_VENDOR_ABOCOM, CARDBUS_PRODUCT_ABOCOM_FE2000VX,
    124 		"AboCom FE2000VX 10/100BaseTX", RTK_8139 },
    125 
    126 	{ 0, 0, NULL, 0 }
    127 };
    128 
    129 static int rtk_cardbus_match	__P((struct device *, struct cfdata *, void *));
    130 static void rtk_cardbus_attach	__P((struct device *, struct device *, void *));
    131 static int rtk_cardbus_detach	__P((struct device *, int));
    132 
    133 struct rtk_cardbus_softc {
    134 	struct rtk_softc sc_rtk;	/* real rtk softc */
    135 
    136 	/* CardBus-specific goo. */
    137 	void *sc_ih;
    138 	cardbus_devfunc_t sc_ct;
    139 	cardbustag_t sc_tag;
    140 	int sc_csr;
    141 	int sc_cben;
    142 	int sc_bar_reg;
    143 	pcireg_t sc_bar_val;
    144 	bus_size_t sc_mapsize;
    145 	int sc_intrline;
    146 };
    147 
    148 CFATTACH_DECL(rtk_cardbus, sizeof(struct rtk_cardbus_softc),
    149     rtk_cardbus_match, rtk_cardbus_attach, rtk_cardbus_detach, rtk_activate);
    150 
    151 const struct rtk_type *rtk_cardbus_lookup
    152 	__P((const struct cardbus_attach_args *));
    153 
    154 void rtk_cardbus_setup		__P((struct rtk_cardbus_softc *));
    155 
    156 int rtk_cardbus_enable		__P((struct rtk_softc *));
    157 void rtk_cardbus_disable	__P((struct rtk_softc *));
    158 void rtk_cardbus_power		__P((struct rtk_softc *, int));
    159 const struct rtk_type *
    160 rtk_cardbus_lookup(ca)
    161 	const struct cardbus_attach_args *ca;
    162 {
    163 	const struct rtk_type *t;
    164 
    165 	for (t = rtk_cardbus_devs; t->rtk_name != NULL; t++){
    166 		if (CARDBUS_VENDOR(ca->ca_id) == t->rtk_vid &&
    167 		    CARDBUS_PRODUCT(ca->ca_id) == t->rtk_did) {
    168 			return (t);
    169 		}
    170 	}
    171 	return (NULL);
    172 }
    173 
    174 int
    175 rtk_cardbus_match(parent, match, aux)
    176 	struct device *parent;
    177 	struct cfdata *match;
    178 	void *aux;
    179 {
    180 	struct cardbus_attach_args *ca = aux;
    181 
    182 	if (rtk_cardbus_lookup(ca) != NULL)
    183 		return (1);
    184 
    185 	return (0);
    186 }
    187 
    188 
    189 void
    190 rtk_cardbus_attach(parent, self, aux)
    191 	struct device *parent, *self;
    192 	void *aux;
    193 {
    194 	struct rtk_cardbus_softc *csc = (struct rtk_cardbus_softc *)self;
    195 	struct rtk_softc *sc = &csc->sc_rtk;
    196 	struct cardbus_attach_args *ca = aux;
    197 	cardbus_devfunc_t ct = ca->ca_ct;
    198 	const struct rtk_type *t;
    199 	bus_addr_t adr;
    200 
    201 	sc->sc_dmat = ca->ca_dmat;
    202 	csc->sc_ct = ct;
    203 	csc->sc_tag = ca->ca_tag;
    204 	csc->sc_intrline = ca->ca_intrline;
    205 
    206 	t = rtk_cardbus_lookup(ca);
    207 	if (t == NULL) {
    208 		printf("\n");
    209 		panic("rtk_cardbus_attach: impossible");
    210 	 }
    211 	printf(": %s\n", t->rtk_name);
    212 
    213 	/*
    214 	 * Power management hooks.
    215 	 */
    216 	sc->sc_enable = rtk_cardbus_enable;
    217 	sc->sc_disable = rtk_cardbus_disable;
    218 	sc->sc_power = rtk_cardbus_power;
    219 
    220 	/*
    221 	 * Map control/status registers.
    222 	 */
    223 	csc->sc_csr = CARDBUS_COMMAND_MASTER_ENABLE;
    224 #ifdef RTK_USEIOSPACE
    225 	if (Cardbus_mapreg_map(ct, RTK_PCI_LOIO, CARDBUS_MAPREG_TYPE_IO, 0,
    226 	    &sc->rtk_btag, &sc->rtk_bhandle, &adr, &csc->sc_mapsize) == 0) {
    227 #if rbus
    228 #else
    229 		(*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
    230 #endif
    231 		csc->sc_cben = CARDBUS_IO_ENABLE;
    232 		csc->sc_csr |= CARDBUS_COMMAND_IO_ENABLE;
    233 		csc->sc_bar_reg = RTK_PCI_LOIO;
    234 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_IO;
    235 	}
    236 #else
    237 	if (Cardbus_mapreg_map(ct, RTK_PCI_LOMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
    238 	    &sc->rtk_btag, &sc->rtk_bhandle, &adr, &csc->sc_mapsize) == 0) {
    239 #if rbus
    240 #else
    241 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    242 #endif
    243 		csc->sc_cben = CARDBUS_MEM_ENABLE;
    244 		csc->sc_csr |= CARDBUS_COMMAND_MEM_ENABLE;
    245 		csc->sc_bar_reg = RTK_PCI_LOMEM;
    246 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
    247 	}
    248 #endif
    249 	else {
    250 		printf("%s: unable to map deviceregisters\n",
    251 			 sc->sc_dev.dv_xname);
    252 		return;
    253 	}
    254 	/*
    255 	 * Handle power management nonsense and initialize the
    256 	 * configuration registers.
    257 	 */
    258 	rtk_cardbus_setup(csc);
    259 	sc->rtk_type = t->rtk_type;
    260 
    261 	rtk_attach(sc);
    262 
    263 	/*
    264 	 * Power down the socket.
    265 	 */
    266 	Cardbus_function_disable(csc->sc_ct);
    267 }
    268 
    269 int
    270 rtk_cardbus_detach(self, flags)
    271 	struct device *self;
    272 	int flags;
    273 {
    274 	struct rtk_cardbus_softc *csc = (void *) self;
    275 	struct rtk_softc *sc = &csc->sc_rtk;
    276 	struct cardbus_devfunc *ct = csc->sc_ct;
    277 	int	rv;
    278 
    279 #ifdef DIAGNOSTIC
    280 	if (ct == NULL)
    281 		panic("%s: data structure lacks", sc->sc_dev.dv_xname);
    282 #endif
    283 	rv = rtk_detach(sc);
    284 	if (rv)
    285 		return (rv);
    286 	/*
    287 	 * Unhook the interrut handler.
    288 	 */
    289 	if (csc->sc_ih != NULL)
    290 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    291 
    292 	/*
    293 	 * Release bus space and close window.
    294 	 */
    295 	if (csc->sc_bar_reg != 0)
    296 		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
    297 			sc->rtk_btag, sc->rtk_bhandle, csc->sc_mapsize);
    298 
    299 	return (0);
    300 }
    301 
    302 void
    303 rtk_cardbus_setup(csc)
    304 	struct rtk_cardbus_softc *csc;
    305 {
    306 	struct rtk_softc *sc = &csc->sc_rtk;
    307 	cardbus_devfunc_t ct = csc->sc_ct;
    308 	cardbus_chipset_tag_t cc = ct->ct_cc;
    309 	cardbus_function_tag_t cf = ct->ct_cf;
    310 	pcireg_t	reg,command;
    311 	int		pmreg;
    312 
    313 	/*
    314 	 * Handle power management nonsense.
    315 	 */
    316 	if (cardbus_get_capability(cc, cf, csc->sc_tag,
    317 	    PCI_CAP_PWRMGMT, &pmreg, 0)) {
    318 		command = cardbus_conf_read(cc, cf, csc->sc_tag, pmreg + 4);
    319 		if (command & RTK_PSTATE_MASK) {
    320 			pcireg_t		iobase, membase, irq;
    321 
    322 			/* Save important PCI config data. */
    323 			iobase = cardbus_conf_read(cc, cf, csc->sc_tag,
    324 			    RTK_PCI_LOIO);
    325 			membase = cardbus_conf_read(cc, cf,csc->sc_tag,
    326 			    RTK_PCI_LOMEM);
    327 			irq = cardbus_conf_read(cc, cf,csc->sc_tag,
    328 			    CARDBUS_INTERRUPT_REG);
    329 
    330 			/* Reset the power state. */
    331 			printf("%s: chip is in D%d power mode "
    332 			    "-- setting to D0\n", sc->sc_dev.dv_xname,
    333 			    command & RTK_PSTATE_MASK);
    334 			command &= 0xFFFFFFFC;
    335 			cardbus_conf_write(cc, cf, csc->sc_tag,
    336 			    pmreg + 4, command);
    337 
    338 			/* Restore PCI config data. */
    339 			cardbus_conf_write(cc, cf, csc->sc_tag,
    340 			    RTK_PCI_LOIO, iobase);
    341 			cardbus_conf_write(cc, cf, csc->sc_tag,
    342 			    RTK_PCI_LOMEM, membase);
    343 			cardbus_conf_write(cc, cf, csc->sc_tag,
    344 			    CARDBUS_INTERRUPT_REG, irq);
    345 		}
    346 	}
    347 
    348 	/* Make sure the right access type is on the CardBus bridge. */
    349 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
    350 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    351 
    352 	/* Program the BAR */
    353 	cardbus_conf_write(cc, cf, csc->sc_tag,
    354 		csc->sc_bar_reg, csc->sc_bar_val);
    355 
    356 	/* Enable the appropriate bits in the CARDBUS CSR. */
    357 	reg = cardbus_conf_read(cc, cf, csc->sc_tag,
    358 	    CARDBUS_COMMAND_STATUS_REG);
    359 	reg &= ~(CARDBUS_COMMAND_IO_ENABLE|CARDBUS_COMMAND_MEM_ENABLE);
    360 	reg |= csc->sc_csr;
    361 	cardbus_conf_write(cc, cf, csc->sc_tag,
    362 	    CARDBUS_COMMAND_STATUS_REG, reg);
    363 
    364 	/*
    365 	 * Make sure the latency timer is set to some reasonable
    366 	 * value.
    367 	 */
    368 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
    369 	if (CARDBUS_LATTIMER(reg) < 0x20) {
    370 		reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    371 		reg |= (0x20 << CARDBUS_LATTIMER_SHIFT);
    372 		cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
    373 	}
    374 }
    375 
    376 int
    377 rtk_cardbus_enable(sc)
    378 	struct rtk_softc *sc;
    379 {
    380 	struct rtk_cardbus_softc *csc = (void *) sc;
    381 	cardbus_devfunc_t ct = csc->sc_ct;
    382 	cardbus_chipset_tag_t cc = ct->ct_cc;
    383 	cardbus_function_tag_t cf = ct->ct_cf;
    384 
    385 	/*
    386 	 * Power on the socket.
    387 	 */
    388 	Cardbus_function_enable(ct);
    389 
    390 	/*
    391 	 * Set up the PCI configuration registers.
    392 	 */
    393 	rtk_cardbus_setup(csc);
    394 
    395 	/*
    396 	 * Map and establish the interrupt.
    397 	 */
    398 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline,
    399 		IPL_NET, rtk_intr, sc);
    400 	if (csc->sc_ih == NULL) {
    401 		printf("%s: unable to establish interrupt at %d\n",
    402 			sc->sc_dev.dv_xname, csc->sc_intrline);
    403 		Cardbus_function_disable(csc->sc_ct);
    404 		return (1);
    405 	}
    406 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    407 		csc->sc_intrline);
    408 	return (0);
    409 }
    410 
    411 void
    412 rtk_cardbus_disable(sc)
    413 	struct rtk_softc *sc;
    414 {
    415 	struct rtk_cardbus_softc *csc = (void *) sc;
    416 	cardbus_devfunc_t ct = csc->sc_ct;
    417 	cardbus_chipset_tag_t cc = ct->ct_cc;
    418 	cardbus_function_tag_t cf = ct->ct_cf;
    419 
    420 	/* Unhook the interrupt handler. */
    421 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    422 
    423 	/* Power down the socket. */
    424 	Cardbus_function_disable(ct);
    425 }
    426 
    427 void
    428 rtk_cardbus_power(sc, why)
    429 	struct rtk_softc *sc;
    430 	int why;
    431 {
    432 	struct rtk_cardbus_softc *csc = (void *) sc;
    433 
    434 	if (why == PWR_RESUME) {
    435 		/*
    436 		 * Give the PCI configuration registers a kick
    437 		 * in the head.
    438 		 */
    439 #ifdef DIAGNOSTIC
    440 		if (RTK_IS_ENABLED(sc) == 0)
    441 			panic("rtk_cardbus_power");
    442 #endif
    443 		rtk_cardbus_setup(csc);
    444 	}
    445 }
    446