Home | History | Annotate | Line # | Download | only in cardbus
      1  1.27  drochner /*	$NetBSD: if_re_cardbus.c,v 1.27 2011/08/01 11:20:27 drochner Exp $	*/
      2   1.1  jonathan 
      3   1.1  jonathan /*
      4   1.1  jonathan  * Copyright (c) 2004 Jonathan Stone
      5   1.1  jonathan  * All rights reserved.
      6   1.1  jonathan  *
      7   1.1  jonathan  * Redistribution and use in source and binary forms, with or without
      8   1.1  jonathan  * modification, are permitted provided that the following conditions
      9   1.1  jonathan  * are met:
     10   1.1  jonathan  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jonathan  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jonathan  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jonathan  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jonathan  *    documentation and/or other materials provided with the distribution.
     15   1.1  jonathan  * 3. The name of the author may not be used to endorse or promote products
     16   1.1  jonathan  *    derived from this software without specific prior written permission.
     17   1.1  jonathan  *
     18   1.1  jonathan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1  jonathan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1  jonathan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1  jonathan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1  jonathan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1  jonathan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1  jonathan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1  jonathan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1  jonathan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27   1.1  jonathan  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1  jonathan  */
     29   1.1  jonathan 
     30   1.1  jonathan /*
     31   1.1  jonathan  * if_re_cardbus.c:
     32   1.1  jonathan  *	Cardbus specific routines for Realtek 8169 ethernet adapter.
     33   1.1  jonathan  *	Tested for :
     34   1.1  jonathan  *		Netgear GA-511 (8169S)
     35   1.3   kanaoka  *		Buffalo LPC-CB-CLGT
     36   1.1  jonathan  */
     37   1.1  jonathan 
     38   1.1  jonathan #include <sys/cdefs.h>
     39  1.27  drochner __KERNEL_RCSID(0, "$NetBSD: if_re_cardbus.c,v 1.27 2011/08/01 11:20:27 drochner Exp $");
     40   1.1  jonathan 
     41   1.1  jonathan #include <sys/param.h>
     42   1.1  jonathan #include <sys/systm.h>
     43   1.1  jonathan #include <sys/device.h>
     44   1.1  jonathan 
     45   1.1  jonathan #include <net/if.h>
     46   1.1  jonathan #include <net/if_ether.h>
     47   1.1  jonathan #include <net/if_media.h>
     48   1.1  jonathan 
     49  1.14        ad #include <sys/bus.h>
     50   1.1  jonathan 
     51   1.1  jonathan #include <dev/pci/pcireg.h>
     52   1.1  jonathan #include <dev/pci/pcivar.h>
     53   1.1  jonathan #include <dev/pci/pcidevs.h>
     54   1.1  jonathan 
     55   1.1  jonathan #include <dev/cardbus/cardbusvar.h>
     56   1.1  jonathan 
     57   1.1  jonathan #include <dev/mii/mii.h>
     58   1.1  jonathan #include <dev/mii/miivar.h>
     59   1.1  jonathan 
     60   1.1  jonathan /*
     61   1.1  jonathan  * Default to using PIO access for this driver. On SMP systems,
     62   1.1  jonathan  * there appear to be problems with memory mapped mode: it looks like
     63   1.1  jonathan  * doing too many memory mapped access back to back in rapid succession
     64   1.1  jonathan  * can hang the bus. I'm inclined to blame this on crummy design/construction
     65   1.1  jonathan  * on the part of Realtek. Memory mapped mode does appear to work on
     66   1.1  jonathan  * uniprocessor systems though.
     67   1.1  jonathan  */
     68   1.5     perry #define RTK_USEIOSPACE
     69   1.1  jonathan 
     70   1.1  jonathan #include <dev/ic/rtl81x9reg.h>
     71   1.1  jonathan #include <dev/ic/rtl81x9var.h>
     72   1.1  jonathan 
     73   1.1  jonathan #include <dev/ic/rtl8169var.h>
     74   1.1  jonathan 
     75   1.1  jonathan /*
     76   1.1  jonathan  * Various supported device vendors/types and their names.
     77   1.1  jonathan  */
     78   1.1  jonathan static const struct rtk_type re_cardbus_devs[] = {
     79   1.1  jonathan 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169,
     80   1.1  jonathan 		RTK_8169, "Realtek 10/100/1000baseT" },
     81   1.1  jonathan 	{ 0, 0, 0, NULL }
     82   1.1  jonathan };
     83   1.1  jonathan 
     84  1.17   tsutsui static int  re_cardbus_match(device_t, cfdata_t, void *);
     85  1.17   tsutsui static void re_cardbus_attach(device_t, device_t, void *);
     86  1.17   tsutsui static int  re_cardbus_detach(device_t, int);
     87   1.1  jonathan 
     88   1.1  jonathan struct re_cardbus_softc {
     89   1.5     perry 	struct rtk_softc sc_rtk;	/* real rtk softc */
     90   1.1  jonathan 
     91   1.1  jonathan 	/* CardBus-specific goo. */
     92   1.1  jonathan 	void *sc_ih;
     93   1.1  jonathan 	cardbus_devfunc_t sc_ct;
     94  1.21    dyoung 	pcitag_t sc_tag;
     95  1.23    dyoung 	pcireg_t sc_csr;
     96   1.1  jonathan 	int sc_bar_reg;
     97   1.1  jonathan 	pcireg_t sc_bar_val;
     98   1.1  jonathan };
     99   1.1  jonathan 
    100  1.17   tsutsui CFATTACH_DECL_NEW(re_cardbus, sizeof(struct re_cardbus_softc),
    101   1.1  jonathan     re_cardbus_match, re_cardbus_attach, re_cardbus_detach, re_activate);
    102   1.1  jonathan 
    103   1.4   kanaoka const struct rtk_type *re_cardbus_lookup(const struct cardbus_attach_args *);
    104   1.1  jonathan 
    105   1.4   kanaoka void re_cardbus_setup(struct re_cardbus_softc *);
    106   1.1  jonathan 
    107   1.4   kanaoka int re_cardbus_enable(struct rtk_softc *);
    108   1.4   kanaoka void re_cardbus_disable(struct rtk_softc *);
    109   1.1  jonathan 
    110   1.1  jonathan const struct rtk_type *
    111   1.4   kanaoka re_cardbus_lookup(const struct cardbus_attach_args *ca)
    112   1.1  jonathan {
    113   1.1  jonathan 	const struct rtk_type *t;
    114   1.1  jonathan 
    115   1.1  jonathan 	for (t = re_cardbus_devs; t->rtk_name != NULL; t++) {
    116  1.24    dyoung 		if (PCI_VENDOR(ca->ca_id) == t->rtk_vid &&
    117  1.24    dyoung 		    PCI_PRODUCT(ca->ca_id) == t->rtk_did) {
    118   1.4   kanaoka 			return t;
    119   1.1  jonathan 		}
    120   1.1  jonathan 	}
    121   1.4   kanaoka 	return NULL;
    122   1.1  jonathan }
    123   1.1  jonathan 
    124   1.1  jonathan int
    125  1.17   tsutsui re_cardbus_match(device_t parent, cfdata_t cf, void *aux)
    126   1.1  jonathan {
    127   1.1  jonathan 	struct cardbus_attach_args *ca = aux;
    128   1.1  jonathan 
    129   1.1  jonathan 	if (re_cardbus_lookup(ca) != NULL)
    130   1.4   kanaoka 		return 1;
    131   1.1  jonathan 
    132   1.4   kanaoka 	return 0;
    133   1.1  jonathan }
    134   1.1  jonathan 
    135   1.1  jonathan 
    136   1.1  jonathan void
    137  1.17   tsutsui re_cardbus_attach(device_t parent, device_t self, void *aux)
    138   1.1  jonathan {
    139   1.7   thorpej 	struct re_cardbus_softc *csc = device_private(self);
    140   1.1  jonathan 	struct rtk_softc *sc = &csc->sc_rtk;
    141   1.1  jonathan 	struct cardbus_attach_args *ca = aux;
    142   1.1  jonathan 	cardbus_devfunc_t ct = ca->ca_ct;
    143   1.1  jonathan 	const struct rtk_type *t;
    144   1.1  jonathan 	bus_addr_t adr;
    145   1.1  jonathan 
    146  1.17   tsutsui 	sc->sc_dev = self;
    147   1.1  jonathan 	sc->sc_dmat = ca->ca_dmat;
    148   1.1  jonathan 	csc->sc_ct = ct;
    149   1.1  jonathan 	csc->sc_tag = ca->ca_tag;
    150   1.1  jonathan 
    151   1.5     perry 	t = re_cardbus_lookup(ca);
    152   1.5     perry 	if (t == NULL) {
    153   1.5     perry 		aprint_error("\n");
    154  1.17   tsutsui 		panic("%s: impossible", __func__);
    155   1.5     perry 	 }
    156   1.5     perry 	aprint_normal(": %s\n", t->rtk_name);
    157   1.5     perry 
    158   1.1  jonathan 	/*
    159   1.1  jonathan 	 * Power management hooks.
    160   1.1  jonathan 	 */
    161   1.1  jonathan 	sc->sc_enable = re_cardbus_enable;
    162   1.1  jonathan 	sc->sc_disable = re_cardbus_disable;
    163   1.1  jonathan 
    164   1.1  jonathan 	/*
    165   1.1  jonathan 	 * Map control/status registers.
    166   1.1  jonathan 	 */
    167  1.24    dyoung 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
    168   1.1  jonathan #ifdef RTK_USEIOSPACE
    169  1.24    dyoung 	if (Cardbus_mapreg_map(ct, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    170  1.26  jakllsch 	    &sc->rtk_btag, &sc->rtk_bhandle, &adr, &sc->rtk_bsize) == 0) {
    171  1.24    dyoung 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
    172   1.1  jonathan 		csc->sc_bar_reg = RTK_PCI_LOIO;
    173  1.24    dyoung 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
    174   1.1  jonathan 	}
    175   1.1  jonathan #else
    176  1.24    dyoung 	if (Cardbus_mapreg_map(ct, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    177  1.26  jakllsch 	    &sc->rtk_btag, &sc->rtk_bhandle, &adr, &sc->rtk_bsize) == 0) {
    178  1.24    dyoung 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
    179   1.1  jonathan 		csc->sc_bar_reg = RTK_PCI_LOMEM;
    180  1.24    dyoung 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
    181   1.1  jonathan 	}
    182   1.1  jonathan #endif
    183   1.1  jonathan 	else {
    184  1.17   tsutsui 		aprint_error_dev(self, "unable to map deviceregisters\n");
    185   1.1  jonathan 		return;
    186   1.1  jonathan 	}
    187   1.1  jonathan 	/*
    188   1.1  jonathan 	 * Handle power management nonsense and initialize the
    189   1.1  jonathan 	 * configuration registers.
    190   1.1  jonathan 	 */
    191   1.1  jonathan 	re_cardbus_setup(csc);
    192   1.1  jonathan 
    193   1.1  jonathan 	sc->sc_dmat = ca->ca_dmat;
    194   1.1  jonathan 	re_attach(sc);
    195   1.1  jonathan 
    196   1.1  jonathan 	/*
    197   1.1  jonathan 	 * Power down the socket.
    198   1.1  jonathan 	 */
    199   1.1  jonathan 	Cardbus_function_disable(csc->sc_ct);
    200   1.1  jonathan }
    201   1.1  jonathan 
    202   1.5     perry int
    203  1.17   tsutsui re_cardbus_detach(device_t self, int flags)
    204   1.1  jonathan {
    205   1.7   thorpej 	struct re_cardbus_softc *csc = device_private(self);
    206   1.1  jonathan 	struct rtk_softc *sc = &csc->sc_rtk;
    207   1.1  jonathan 	struct cardbus_devfunc *ct = csc->sc_ct;
    208  1.17   tsutsui 	int rv;
    209   1.1  jonathan 
    210   1.1  jonathan #ifdef DIAGNOSTIC
    211   1.1  jonathan 	if (ct == NULL)
    212   1.1  jonathan 		panic("%s: cardbus softc, cardbus_devfunc NULL",
    213  1.17   tsutsui 		      device_xname(self));
    214   1.1  jonathan #endif
    215  1.15  jmcneill 
    216   1.1  jonathan 	rv = re_detach(sc);
    217   1.1  jonathan 	if (rv)
    218   1.4   kanaoka 		return rv;
    219  1.15  jmcneill 
    220   1.1  jonathan 	/*
    221   1.1  jonathan 	 * Unhook the interrupt handler.
    222   1.1  jonathan 	 */
    223   1.1  jonathan 	if (csc->sc_ih != NULL)
    224  1.25    dyoung 		Cardbus_intr_disestablish(ct, csc->sc_ih);
    225   1.5     perry 
    226   1.1  jonathan 	/*
    227   1.1  jonathan 	 * Release bus space and close window.
    228   1.1  jonathan 	 */
    229   1.1  jonathan 	if (csc->sc_bar_reg != 0)
    230   1.1  jonathan 		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
    231  1.26  jakllsch 		    sc->rtk_btag, sc->rtk_bhandle, sc->rtk_bsize);
    232   1.1  jonathan 
    233   1.4   kanaoka 	return 0;
    234   1.1  jonathan }
    235   1.1  jonathan 
    236   1.5     perry void
    237   1.1  jonathan re_cardbus_setup(struct re_cardbus_softc *csc)
    238   1.1  jonathan {
    239   1.1  jonathan 	struct rtk_softc *sc = &csc->sc_rtk;
    240   1.1  jonathan 	cardbus_devfunc_t ct = csc->sc_ct;
    241   1.1  jonathan 	cardbus_chipset_tag_t cc = ct->ct_cc;
    242   1.1  jonathan 	cardbus_function_tag_t cf = ct->ct_cf;
    243  1.17   tsutsui 	pcireg_t reg, command;
    244  1.17   tsutsui 	int pmreg;
    245   1.1  jonathan 
    246   1.1  jonathan 	/*
    247   1.1  jonathan 	 * Handle power management nonsense.
    248   1.1  jonathan 	 */
    249   1.1  jonathan 	if (cardbus_get_capability(cc, cf, csc->sc_tag,
    250   1.1  jonathan 	    PCI_CAP_PWRMGMT, &pmreg, 0)) {
    251  1.24    dyoung 		command = Cardbus_conf_read(ct, csc->sc_tag,
    252   1.1  jonathan 		    pmreg + PCI_PMCSR);
    253  1.10    dogcow 		if (command & PCI_PMCSR_STATE_MASK) {
    254  1.17   tsutsui 			pcireg_t iobase, membase, irq;
    255   1.1  jonathan 
    256   1.1  jonathan 			/* Save important PCI config data. */
    257  1.24    dyoung 			iobase = Cardbus_conf_read(ct, csc->sc_tag,
    258   1.1  jonathan 			    RTK_PCI_LOIO);
    259  1.24    dyoung 			membase = Cardbus_conf_read(ct, csc->sc_tag,
    260   1.1  jonathan 			    RTK_PCI_LOMEM);
    261  1.24    dyoung 			irq = Cardbus_conf_read(ct, csc->sc_tag,
    262  1.24    dyoung 			    PCI_INTERRUPT_REG);
    263   1.1  jonathan 
    264   1.1  jonathan 			/* Reset the power state. */
    265  1.17   tsutsui 			aprint_normal_dev(sc->sc_dev,
    266  1.17   tsutsui 			    "chip is in D%d power mode -- setting to D0\n",
    267  1.10    dogcow 			    command & PCI_PMCSR_STATE_MASK);
    268  1.10    dogcow 			command &= ~PCI_PMCSR_STATE_MASK;
    269  1.24    dyoung 			Cardbus_conf_write(ct, csc->sc_tag,
    270   1.1  jonathan 			    pmreg + PCI_PMCSR, command);
    271   1.1  jonathan 
    272   1.1  jonathan 			/* Restore PCI config data. */
    273  1.24    dyoung 			Cardbus_conf_write(ct, csc->sc_tag,
    274   1.1  jonathan 			    RTK_PCI_LOIO, iobase);
    275  1.24    dyoung 			Cardbus_conf_write(ct, csc->sc_tag,
    276   1.1  jonathan 			    RTK_PCI_LOMEM, membase);
    277  1.24    dyoung 			Cardbus_conf_write(ct, csc->sc_tag,
    278  1.24    dyoung 			    PCI_INTERRUPT_REG, irq);
    279   1.1  jonathan 		}
    280   1.1  jonathan 	}
    281   1.1  jonathan 
    282   1.1  jonathan 	/* Program the BAR */
    283  1.24    dyoung 	Cardbus_conf_write(ct, csc->sc_tag, csc->sc_bar_reg, csc->sc_bar_val);
    284   1.1  jonathan 
    285   1.1  jonathan 	/* Enable the appropriate bits in the CARDBUS CSR. */
    286  1.24    dyoung 	reg = Cardbus_conf_read(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG);
    287  1.24    dyoung 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
    288   1.1  jonathan 	reg |= csc->sc_csr;
    289  1.24    dyoung 	Cardbus_conf_write(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
    290   1.1  jonathan 
    291   1.1  jonathan 	/*
    292   1.1  jonathan 	 * Make sure the latency timer is set to some reasonable
    293   1.1  jonathan 	 * value.
    294   1.1  jonathan 	 */
    295  1.24    dyoung 	reg = Cardbus_conf_read(ct, csc->sc_tag, PCI_BHLC_REG);
    296  1.24    dyoung 	if (PCI_LATTIMER(reg) < 0x40) {
    297  1.24    dyoung 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
    298  1.24    dyoung 		reg |= (0x40 << PCI_LATTIMER_SHIFT);
    299  1.24    dyoung 		Cardbus_conf_write(ct, csc->sc_tag, PCI_BHLC_REG, reg);
    300   1.1  jonathan 	}
    301   1.1  jonathan }
    302   1.1  jonathan 
    303   1.5     perry int
    304   1.1  jonathan re_cardbus_enable(struct rtk_softc *sc)
    305   1.1  jonathan {
    306  1.17   tsutsui 	struct re_cardbus_softc *csc = (struct re_cardbus_softc *)sc;
    307   1.1  jonathan 	cardbus_devfunc_t ct = csc->sc_ct;
    308   1.1  jonathan 
    309   1.1  jonathan 	/*
    310   1.1  jonathan 	 * Power on the socket.
    311   1.1  jonathan 	 */
    312   1.1  jonathan 	Cardbus_function_enable(ct);
    313   1.1  jonathan 
    314   1.1  jonathan 	/*
    315   1.1  jonathan 	 * Set up the PCI configuration registers.
    316   1.1  jonathan 	 */
    317   1.1  jonathan 	re_cardbus_setup(csc);
    318   1.1  jonathan 
    319   1.1  jonathan 	/*
    320   1.1  jonathan 	 * Map and establish the interrupt.
    321   1.1  jonathan 	 */
    322  1.27  drochner 	csc->sc_ih = Cardbus_intr_establish(ct, IPL_NET, re_intr, sc);
    323   1.1  jonathan 	if (csc->sc_ih == NULL) {
    324  1.17   tsutsui 		aprint_error_dev(sc->sc_dev,
    325  1.18  drochner 		    "unable to establish interrupt\n");
    326   1.1  jonathan 		Cardbus_function_disable(csc->sc_ct);
    327   1.4   kanaoka 		return 1;
    328   1.1  jonathan 	}
    329   1.4   kanaoka 	return 0;
    330   1.1  jonathan }
    331   1.1  jonathan 
    332   1.5     perry void
    333   1.1  jonathan re_cardbus_disable(struct rtk_softc *sc)
    334   1.1  jonathan {
    335  1.17   tsutsui 	struct re_cardbus_softc *csc = (struct re_cardbus_softc *)sc;
    336   1.1  jonathan 	cardbus_devfunc_t ct = csc->sc_ct;
    337   1.1  jonathan 
    338   1.1  jonathan 	/* Unhook the interrupt handler. */
    339  1.25    dyoung 	Cardbus_intr_disestablish(ct, csc->sc_ih);
    340   1.1  jonathan 	csc->sc_ih = NULL;
    341   1.1  jonathan 
    342   1.1  jonathan 	/* Power down the socket. */
    343   1.1  jonathan 	Cardbus_function_disable(ct);
    344   1.1  jonathan }
    345