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