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