Home | History | Annotate | Line # | Download | only in cardbus
if_rtk_cardbus.c revision 1.6
      1 /*	$NetBSD: if_rtk_cardbus.c,v 1.6 2001/11/13 12:51:13 lukem 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.6 2001/11/13 12:51:13 lukem 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_REALTEK, CARDBUS_PRODUCT_REALTEK_RT8138,
    110 		"RealTek 8138 10/100BaseTX", RTK_8139 },
    111 	{ CARDBUS_VENDOR_REALTEK, CARDBUS_PRODUCT_REALTEK_RT8139,
    112 		"RealTek 8139 10/100BaseTX", RTK_8139 },
    113 	{ CARDBUS_VENDOR_COREGA, CARDBUS_PRODUCT_COREGA_CB_TXD,
    114 		"Corega FEther CB-TXD 10/100BaseTX", RTK_8139 },
    115 	{ 0, 0, NULL, 0 }
    116 };
    117 
    118 static int rtk_cardbus_match	__P((struct device *, struct cfdata *, void *));
    119 static void rtk_cardbus_attach	__P((struct device *, struct device *, void *));
    120 static int rtk_cardbus_detach	__P((struct device *, int));
    121 
    122 struct rtk_cardbus_softc {
    123 	struct rtk_softc sc_rtk;	/* real rtk softc */
    124 
    125 	/* CardBus-specific goo. */
    126 	void *sc_ih;
    127 	cardbus_devfunc_t sc_ct;
    128 	cardbustag_t sc_tag;
    129 	int sc_csr;
    130 	int sc_cben;
    131 	int sc_bar_reg;
    132 	pcireg_t sc_bar_val;
    133 	bus_size_t sc_mapsize;
    134 	int sc_intrline;
    135 };
    136 
    137 struct cfattach rtk_cardbus_ca = {
    138 	sizeof(struct rtk_cardbus_softc),
    139 	rtk_cardbus_match, rtk_cardbus_attach,
    140 	rtk_cardbus_detach, rtk_activate,
    141 };
    142 
    143 const struct rtk_type *rtk_cardbus_lookup
    144 	__P((const struct cardbus_attach_args *));
    145 
    146 void rtk_cardbus_setup		__P((struct rtk_cardbus_softc *));
    147 
    148 int rtk_cardbus_enable		__P((struct rtk_softc *));
    149 void rtk_cardbus_disable	__P((struct rtk_softc *));
    150 void rtk_cardbus_power		__P((struct rtk_softc *, int));
    151 const struct rtk_type *
    152 rtk_cardbus_lookup(ca)
    153 	const struct cardbus_attach_args *ca;
    154 {
    155 	const struct rtk_type *t;
    156 
    157 	for (t = rtk_cardbus_devs; t->rtk_name != NULL; t++){
    158 		if (CARDBUS_VENDOR(ca->ca_id) == t->rtk_vid &&
    159 		    CARDBUS_PRODUCT(ca->ca_id) == t->rtk_did) {
    160 			return (t);
    161 		}
    162 	}
    163 	return (NULL);
    164 }
    165 
    166 int
    167 rtk_cardbus_match(parent, match, aux)
    168 	struct device *parent;
    169 	struct cfdata *match;
    170 	void *aux;
    171 {
    172 	struct cardbus_attach_args *ca = aux;
    173 
    174 	if (rtk_cardbus_lookup(ca) != NULL)
    175 		return (1);
    176 
    177 	return (0);
    178 }
    179 
    180 
    181 void
    182 rtk_cardbus_attach(parent, self, aux)
    183 	struct device *parent, *self;
    184 	void *aux;
    185 {
    186 	struct rtk_cardbus_softc *csc = (struct rtk_cardbus_softc *)self;
    187 	struct rtk_softc *sc = &csc->sc_rtk;
    188 	struct cardbus_attach_args *ca = aux;
    189 	cardbus_devfunc_t ct = ca->ca_ct;
    190 	const struct rtk_type *t;
    191 	bus_addr_t adr;
    192 
    193 	sc->sc_dmat = ca->ca_dmat;
    194 	csc->sc_ct = ct;
    195 	csc->sc_tag = ca->ca_tag;
    196 	csc->sc_intrline = ca->ca_intrline;
    197 
    198 	t = rtk_cardbus_lookup(ca);
    199 	if (t == NULL) {
    200 		printf("\n");
    201 		panic("rtk_cardbus_attach: impossible");
    202 	 }
    203 	printf(": %s\n", t->rtk_name);
    204 
    205 	/*
    206 	 * Power management hooks.
    207 	 */
    208 	sc->sc_enable = rtk_cardbus_enable;
    209 	sc->sc_disable = rtk_cardbus_disable;
    210 	sc->sc_power = rtk_cardbus_power;
    211 
    212 	/*
    213 	 * Map control/status registers.
    214 	 */
    215 	csc->sc_csr = CARDBUS_COMMAND_MASTER_ENABLE;
    216 #ifdef RTK_USEIOSPACE
    217 	if (Cardbus_mapreg_map(ct, RTK_PCI_LOIO, CARDBUS_MAPREG_TYPE_IO, 0,
    218 	    &sc->rtk_btag, &sc->rtk_bhandle, &adr, &csc->sc_mapsize) == 0) {
    219 #if rbus
    220 #else
    221 		(*ct->ct_cf->cardbus_io_open)(cc, 0, adr, adr+csc->sc_mapsize);
    222 #endif
    223 		csc->sc_cben = CARDBUS_IO_ENABLE;
    224 		csc->sc_csr |= CARDBUS_COMMAND_IO_ENABLE;
    225 		csc->sc_bar_reg = RTK_PCI_LOIO;
    226 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_IO;
    227 	}
    228 #else
    229 	if (Cardbus_mapreg_map(ct, RTK_PCI_LOMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
    230 	    &sc->rtk_btag, &sc->rtk_bhandle, &adr, &csc->sc_mapsize) == 0) {
    231 #if rbus
    232 #else
    233 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    234 #endif
    235 		csc->sc_cben = CARDBUS_MEM_ENABLE;
    236 		csc->sc_csr |= CARDBUS_COMMAND_MEM_ENABLE;
    237 		csc->sc_bar_reg = RTK_PCI_LOMEM;
    238 		csc->sc_bar_val = adr | CARDBUS_MAPREG_TYPE_MEM;
    239 	}
    240 #endif
    241 	else {
    242 		printf("%s: unable to map deviceregisters\n",
    243 			 sc->sc_dev.dv_xname);
    244 		return;
    245 	}
    246 	/*
    247 	 * Handle power management nonsense and initialize the
    248 	 * configuration registers.
    249 	 */
    250 	rtk_cardbus_setup(csc);
    251 	sc->rtk_type = t->rtk_type;
    252 
    253 	rtk_attach(sc);
    254 
    255 	/*
    256 	 * Power down the socket.
    257 	 */
    258 	Cardbus_function_disable(csc->sc_ct);
    259 }
    260 
    261 int
    262 rtk_cardbus_detach(self, flags)
    263 	struct device *self;
    264 	int flags;
    265 {
    266 	struct rtk_cardbus_softc *csc = (void *) self;
    267 	struct rtk_softc *sc = &csc->sc_rtk;
    268 	struct cardbus_devfunc *ct = csc->sc_ct;
    269 	int	rv;
    270 
    271 #ifdef DIAGNOSTIC
    272 	if (ct == NULL)
    273 		panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
    274 #endif
    275 	rv = rtk_detach(sc);
    276 	if (rv)
    277 		return (rv);
    278 	/*
    279 	 * Unhook the interrut handler.
    280 	 */
    281 	if (csc->sc_ih != NULL)
    282 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
    283 
    284 	/*
    285 	 * Release bus space and close window.
    286 	 */
    287 	if (csc->sc_bar_reg != 0)
    288 		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
    289 			sc->rtk_btag, sc->rtk_bhandle, csc->sc_mapsize);
    290 
    291 	return (0);
    292 }
    293 
    294 void
    295 rtk_cardbus_setup(csc)
    296 	struct rtk_cardbus_softc *csc;
    297 {
    298 	struct rtk_softc *sc = &csc->sc_rtk;
    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 	pcireg_t	reg,command;
    303 	int		pmreg;
    304 
    305 	/*
    306 	 * Handle power management nonsense.
    307 	 */
    308 	if (cardbus_get_capability(cc, cf, csc->sc_tag,
    309 	    PCI_CAP_PWRMGMT, &pmreg, 0)) {
    310 		command = cardbus_conf_read(cc, cf, csc->sc_tag, pmreg + 4);
    311 		if (command & RTK_PSTATE_MASK) {
    312 			pcireg_t		iobase, membase, irq;
    313 
    314 			/* Save important PCI config data. */
    315 			iobase = cardbus_conf_read(cc, cf, csc->sc_tag,
    316 			    RTK_PCI_LOIO);
    317 			membase = cardbus_conf_read(cc, cf,csc->sc_tag,
    318 			    RTK_PCI_LOMEM);
    319 			irq = cardbus_conf_read(cc, cf,csc->sc_tag,
    320 			    PCI_PRODUCT_DELTA_8139);
    321 
    322 			/* Reset the power state. */
    323 			printf("%s: chip is is in D%d power mode "
    324 			    "-- setting to D0\n", sc->sc_dev.dv_xname,
    325 			    command & RTK_PSTATE_MASK);
    326 			command &= 0xFFFFFFFC;
    327 			cardbus_conf_write(cc, cf, csc->sc_tag,
    328 			    pmreg + 4, command);
    329 
    330 			/* Restore PCI config data. */
    331 			cardbus_conf_write(cc, cf, csc->sc_tag,
    332 			    RTK_PCI_LOIO, iobase);
    333 			cardbus_conf_write(cc, cf, csc->sc_tag,
    334 			    RTK_PCI_LOMEM, membase);
    335 			cardbus_conf_write(cc, cf, csc->sc_tag,
    336 			    PCI_PRODUCT_DELTA_8139, irq);
    337 		}
    338 	}
    339 
    340 	/* Make sure the right access type is on the CardBus bridge. */
    341 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
    342 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    343 
    344 	/* Program the BAR */
    345 	cardbus_conf_write(cc, cf, csc->sc_tag,
    346 		csc->sc_bar_reg, csc->sc_bar_val);
    347 
    348 	/* Enable the appropriate bits in the CARDBUS CSR. */
    349 	reg = cardbus_conf_read(cc, cf, csc->sc_tag,
    350 	    CARDBUS_COMMAND_STATUS_REG);
    351 	reg &= ~(CARDBUS_COMMAND_IO_ENABLE|CARDBUS_COMMAND_MEM_ENABLE);
    352 	reg |= csc->sc_csr;
    353 	cardbus_conf_write(cc, cf, csc->sc_tag,
    354 	    CARDBUS_COMMAND_STATUS_REG, reg);
    355 
    356 	/*
    357 	 * Make sure the latency timer is set to some reasonable
    358 	 * value.
    359 	 */
    360 	reg = cardbus_conf_read(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG);
    361 	if (CARDBUS_LATTIMER(reg) < 0x20) {
    362 		reg &= ~(CARDBUS_LATTIMER_MASK << CARDBUS_LATTIMER_SHIFT);
    363 		reg |= (0x20 << CARDBUS_LATTIMER_SHIFT);
    364 		cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BHLC_REG, reg);
    365 	}
    366 }
    367 
    368 int
    369 rtk_cardbus_enable(sc)
    370 	struct rtk_softc *sc;
    371 {
    372 	struct rtk_cardbus_softc *csc = (void *) sc;
    373 	cardbus_devfunc_t ct = csc->sc_ct;
    374 	cardbus_chipset_tag_t cc = ct->ct_cc;
    375 	cardbus_function_tag_t cf = ct->ct_cf;
    376 
    377 	/*
    378 	 * Power on the socket.
    379 	 */
    380 	Cardbus_function_enable(ct);
    381 
    382 	/*
    383 	 * Set up the PCI configuration registers.
    384 	 */
    385 	rtk_cardbus_setup(csc);
    386 
    387 	/*
    388 	 * Map and establish the interrupt.
    389 	 */
    390 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline,
    391 		IPL_NET, rtk_intr, sc);
    392 	if (csc->sc_ih == NULL) {
    393 		printf("%s: unable to establish interrupt at %d\n",
    394 			sc->sc_dev.dv_xname, csc->sc_intrline);
    395 		Cardbus_function_disable(csc->sc_ct);
    396 		return (1);
    397 	}
    398 	printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
    399 		csc->sc_intrline);
    400 	return (0);
    401 }
    402 
    403 void
    404 rtk_cardbus_disable(sc)
    405 	struct rtk_softc *sc;
    406 {
    407 	struct rtk_cardbus_softc *csc = (void *) sc;
    408 	cardbus_devfunc_t ct = csc->sc_ct;
    409 	cardbus_chipset_tag_t cc = ct->ct_cc;
    410 	cardbus_function_tag_t cf = ct->ct_cf;
    411 
    412 	/* Unhook the interrupt handler. */
    413 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    414 
    415 	/* Power down the socket. */
    416 	Cardbus_function_disable(ct);
    417 }
    418 
    419 void
    420 rtk_cardbus_power(sc, why)
    421 	struct rtk_softc *sc;
    422 	int why;
    423 {
    424 	struct rtk_cardbus_softc *csc = (void *) sc;
    425 
    426 	if (why == PWR_RESUME) {
    427 		/*
    428 		 * Give the PCI configuration registers a kick
    429 		 * in the head.
    430 		 */
    431 #ifdef DIAGNOSTIC
    432 		if (RTK_IS_ENABLED(sc) == 0)
    433 			panic("rtk_cardbus_power");
    434 #endif
    435 		rtk_cardbus_setup(csc);
    436 	}
    437 }
    438