Home | History | Annotate | Line # | Download | only in pci
if_re_pci.c revision 1.3
      1  1.1  jonathan /*
      2  1.1  jonathan  * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
      3  1.1  jonathan  *
      4  1.1  jonathan  * Written by Bill Paul <wpaul (at) windriver.com>
      5  1.1  jonathan  * Senior Networking Software Engineer
      6  1.1  jonathan  * Wind River Systems
      7  1.1  jonathan  *
      8  1.1  jonathan  * NetBSD bus-specific frontends for written by
      9  1.1  jonathan  * Jonathan Stone <jonathan (at) netbsd.org>
     10  1.1  jonathan  */
     11  1.1  jonathan 
     12  1.1  jonathan #include "bpfilter.h"
     13  1.1  jonathan #include "vlan.h"
     14  1.1  jonathan 
     15  1.1  jonathan #include <sys/types.h>
     16  1.1  jonathan 
     17  1.1  jonathan #include <machine/bus.h>
     18  1.1  jonathan 
     19  1.1  jonathan #include <dev/pci/pcireg.h>
     20  1.1  jonathan #include <dev/pci/pcivar.h>
     21  1.1  jonathan #include <dev/pci/pcidevs.h>
     22  1.1  jonathan 
     23  1.1  jonathan #include <sys/param.h>
     24  1.1  jonathan #include <sys/endian.h>
     25  1.1  jonathan #include <sys/systm.h>
     26  1.1  jonathan #include <sys/sockio.h>
     27  1.1  jonathan #include <sys/mbuf.h>
     28  1.1  jonathan #include <sys/malloc.h>
     29  1.1  jonathan #include <sys/kernel.h>
     30  1.1  jonathan #include <sys/socket.h>
     31  1.1  jonathan #include <sys/device.h>
     32  1.1  jonathan 
     33  1.1  jonathan #include <net/if.h>
     34  1.1  jonathan #include <net/if_arp.h>
     35  1.1  jonathan #include <net/if_dl.h>
     36  1.1  jonathan #include <net/if_ether.h>
     37  1.1  jonathan #include <net/if_media.h>
     38  1.1  jonathan #include <net/if_vlanvar.h>
     39  1.1  jonathan 
     40  1.1  jonathan #include <dev/mii/mii.h>
     41  1.1  jonathan #include <dev/mii/miivar.h>
     42  1.1  jonathan 
     43  1.1  jonathan /*
     44  1.1  jonathan  * Default to using PIO access for this driver.
     45  1.1  jonathan  */
     46  1.1  jonathan #define RE_USEIOSPACE
     47  1.1  jonathan 
     48  1.1  jonathan #include <dev/ic/rtl81x9reg.h>
     49  1.1  jonathan #include <dev/ic/rtl81x9var.h>
     50  1.1  jonathan #include <dev/ic/rtl8169var.h>
     51  1.1  jonathan 
     52  1.1  jonathan struct re_pci_softc {
     53  1.1  jonathan 	struct rtk_softc sc_rtk;
     54  1.1  jonathan 
     55  1.1  jonathan 	void *sc_ih;
     56  1.1  jonathan 	pci_chipset_tag_t sc_pc;
     57  1.1  jonathan 	pcitag_t sc_pcitag;
     58  1.1  jonathan };
     59  1.1  jonathan 
     60  1.1  jonathan static int	re_pci_probe(struct device *, struct cfdata *, void *);
     61  1.1  jonathan static void	re_pci_attach(struct device *, struct device *, void *);
     62  1.1  jonathan 
     63  1.1  jonathan /*
     64  1.1  jonathan  * Various supported device vendors/types and their names.
     65  1.1  jonathan  */
     66  1.1  jonathan static struct rtk_type re_devs[] = {
     67  1.1  jonathan 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139, RTK_HWREV_8139CPLUS,
     68  1.1  jonathan 		"RealTek 8139C+ 10/100BaseTX" },
     69  1.1  jonathan 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RTK_HWREV_8169,
     70  1.1  jonathan 		"RealTek 8169 Gigabit Ethernet" },
     71  1.1  jonathan 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RTK_HWREV_8169S,
     72  1.1  jonathan 		"RealTek 8169S Single-chip Gigabit Ethernet" },
     73  1.1  jonathan 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RTK_HWREV_8110S,
     74  1.1  jonathan 		"RealTek 8110S Single-chip Gigabit Ethernet" },
     75  1.1  jonathan 	{ 0, 0, 0, NULL }
     76  1.1  jonathan };
     77  1.1  jonathan 
     78  1.1  jonathan static struct rtk_hwrev re_hwrevs[] = {
     79  1.1  jonathan 	{ RTK_HWREV_8139, RTK_8139,  "" },
     80  1.1  jonathan 	{ RTK_HWREV_8139A, RTK_8139, "A" },
     81  1.1  jonathan 	{ RTK_HWREV_8139AG, RTK_8139, "A-G" },
     82  1.1  jonathan 	{ RTK_HWREV_8139B, RTK_8139, "B" },
     83  1.1  jonathan 	{ RTK_HWREV_8130, RTK_8139, "8130" },
     84  1.1  jonathan 	{ RTK_HWREV_8139C, RTK_8139, "C" },
     85  1.1  jonathan 	{ RTK_HWREV_8139D, RTK_8139, "8139D/8100B/8100C" },
     86  1.1  jonathan 	{ RTK_HWREV_8139CPLUS, RTK_8139CPLUS, "C+"},
     87  1.1  jonathan 	{ RTK_HWREV_8169, RTK_8169, "8169"},
     88  1.1  jonathan 	{ RTK_HWREV_8169S, RTK_8169, "8169S"},
     89  1.1  jonathan 	{ RTK_HWREV_8110S, RTK_8169, "8110S"},
     90  1.1  jonathan 	{ RTK_HWREV_8100, RTK_8139, "8100"},
     91  1.1  jonathan 	{ RTK_HWREV_8101, RTK_8139, "8101"},
     92  1.1  jonathan 	{ 0, 0, NULL }
     93  1.1  jonathan };
     94  1.1  jonathan 
     95  1.1  jonathan CFATTACH_DECL(re_pci, sizeof(struct re_pci_softc), re_pci_probe, re_pci_attach,
     96  1.1  jonathan 	      NULL, NULL);
     97  1.1  jonathan 
     98  1.1  jonathan /*
     99  1.1  jonathan  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
    100  1.1  jonathan  * IDs against our list and return a device name if we find a match.
    101  1.1  jonathan  */
    102  1.1  jonathan static int
    103  1.1  jonathan re_pci_probe(struct device *parent, struct cfdata *match, void *aux)
    104  1.1  jonathan {
    105  1.1  jonathan 	struct rtk_type		*t;
    106  1.1  jonathan 	struct pci_attach_args	*pa = aux;
    107  1.1  jonathan 	bus_space_tag_t		rtk_btag;
    108  1.1  jonathan 	bus_space_handle_t	rtk_bhandle;
    109  1.1  jonathan 	bus_size_t		bsize;
    110  1.1  jonathan 	u_int32_t		hwrev;
    111  1.1  jonathan 
    112  1.1  jonathan 	t = re_devs;
    113  1.1  jonathan 
    114  1.3   kanaoka 	while (t->rtk_name != NULL) {
    115  1.1  jonathan 		if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
    116  1.1  jonathan 		    (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
    117  1.1  jonathan 
    118  1.1  jonathan 			/*
    119  1.1  jonathan 			 * Temporarily map the I/O space
    120  1.1  jonathan 			 * so we can read the chip ID register.
    121  1.1  jonathan 			 */
    122  1.1  jonathan 			if (pci_mapreg_map(pa, RTK_PCI_LOIO,
    123  1.1  jonathan 			    PCI_MAPREG_TYPE_IO, 0, &rtk_btag,
    124  1.1  jonathan 			    &rtk_bhandle, NULL, &bsize)) {
    125  1.3   kanaoka 				aprint_error("can't map i/o space\n");
    126  1.1  jonathan 				return 0;
    127  1.1  jonathan 			}
    128  1.1  jonathan 			hwrev = bus_space_read_4(rtk_btag, rtk_bhandle,
    129  1.1  jonathan 			    RTK_TXCFG) & RTK_TXCFG_HWREV;
    130  1.1  jonathan 			bus_space_unmap(rtk_btag, rtk_bhandle, bsize);
    131  1.1  jonathan 			if (t->rtk_basetype == hwrev)
    132  1.1  jonathan 				return 2;	/* defeat rtk(4) */
    133  1.1  jonathan 		}
    134  1.1  jonathan 		t++;
    135  1.1  jonathan 	}
    136  1.1  jonathan 
    137  1.1  jonathan 	return 0;
    138  1.1  jonathan }
    139  1.1  jonathan 
    140  1.1  jonathan static void
    141  1.1  jonathan re_pci_attach(struct device *parent, struct device *self, void *aux)
    142  1.1  jonathan {
    143  1.1  jonathan 	struct re_pci_softc	*psc = (void *)self;
    144  1.1  jonathan 	struct rtk_softc	*sc = &psc->sc_rtk;
    145  1.1  jonathan 	struct pci_attach_args 	*pa = aux;
    146  1.1  jonathan 	pci_chipset_tag_t pc = pa->pa_pc;
    147  1.1  jonathan 	pci_intr_handle_t ih;
    148  1.1  jonathan 	const char *intrstr = NULL;
    149  1.1  jonathan 	struct rtk_type		*t;
    150  1.1  jonathan 	struct rtk_hwrev	*hw_rev;
    151  1.1  jonathan 	int			hwrev;
    152  1.1  jonathan 	int			error = 0;
    153  1.3   kanaoka 	int			pmreg;
    154  1.1  jonathan 	pcireg_t		command;
    155  1.2   kanaoka 	bus_size_t		bsize;
    156  1.1  jonathan 
    157  1.1  jonathan 
    158  1.1  jonathan 	/*
    159  1.1  jonathan 	 * Handle power management nonsense.
    160  1.1  jonathan 	 */
    161  1.3   kanaoka 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    162  1.3   kanaoka 		command = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR);
    163  1.3   kanaoka 		if (command & RTK_PSTATE_MASK) {
    164  1.3   kanaoka 			u_int32_t		iobase, membase, irq;
    165  1.3   kanaoka 
    166  1.3   kanaoka 			/* Save important PCI config data. */
    167  1.3   kanaoka 			iobase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOIO);
    168  1.3   kanaoka 			membase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOMEM);
    169  1.3   kanaoka 			irq = pci_conf_read(pc, pa->pa_tag, PCI_INTERRUPT_REG);
    170  1.3   kanaoka 
    171  1.3   kanaoka 			/* Reset the power state. */
    172  1.3   kanaoka 			aprint_normal("%s: chip is is in D%d power mode "
    173  1.3   kanaoka 		    	    "-- setting to D0\n", sc->sc_dev.dv_xname,
    174  1.3   kanaoka 		    	    command & RTK_PSTATE_MASK);
    175  1.3   kanaoka 
    176  1.3   kanaoka 			command &= ~RTK_PSTATE_MASK;
    177  1.3   kanaoka 			pci_conf_write(pc, pa->pa_tag,
    178  1.3   kanaoka 			    pmreg + PCI_PMCSR, command);
    179  1.3   kanaoka 
    180  1.3   kanaoka 			/* Restore PCI config data. */
    181  1.3   kanaoka 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOIO, iobase);
    182  1.3   kanaoka 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOMEM, membase);
    183  1.3   kanaoka 			pci_conf_write(pc, pa->pa_tag, PCI_INTERRUPT_REG, irq);
    184  1.3   kanaoka 		}
    185  1.3   kanaoka 	}
    186  1.1  jonathan 
    187  1.1  jonathan 	/*
    188  1.1  jonathan 	 * Map control/status registers.
    189  1.1  jonathan 	 */
    190  1.1  jonathan 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    191  1.1  jonathan 	command |= PCI_COMMAND_MASTER_ENABLE;
    192  1.1  jonathan 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
    193  1.1  jonathan 
    194  1.1  jonathan #ifdef RE_USEIOSPACE
    195  1.1  jonathan 	if (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    196  1.2   kanaoka 	    &sc->rtk_btag, &sc->rtk_bhandle, NULL, &bsize)) {
    197  1.3   kanaoka 		aprint_error("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    198  1.2   kanaoka 		return;
    199  1.1  jonathan 	}
    200  1.1  jonathan #else
    201  1.1  jonathan 	if (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    202  1.2   kanaoka 	    &sc->rtk_btag, &sc->rtk_bhandle, NULL, &bsize)) {
    203  1.3   kanaoka 		aprint_error("%s: can't map mem space\n", sc->sc_dev.dv_xname);
    204  1.2   kanaoka 		return;
    205  1.1  jonathan 	}
    206  1.1  jonathan #endif
    207  1.1  jonathan 	t = re_devs;
    208  1.1  jonathan 	hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
    209  1.1  jonathan 
    210  1.3   kanaoka 	while (t->rtk_name != NULL) {
    211  1.1  jonathan 		if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
    212  1.1  jonathan 		    (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
    213  1.1  jonathan 
    214  1.1  jonathan 			if (t->rtk_basetype == hwrev)
    215  1.1  jonathan 				break;
    216  1.1  jonathan 		}
    217  1.1  jonathan 		t++;
    218  1.1  jonathan 	}
    219  1.3   kanaoka 	aprint_normal(": %s\n", t->rtk_name);
    220  1.1  jonathan 
    221  1.1  jonathan 	hw_rev = re_hwrevs;
    222  1.1  jonathan 	hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
    223  1.1  jonathan 	while (hw_rev->rtk_desc != NULL) {
    224  1.1  jonathan 		if (hw_rev->rtk_rev == hwrev) {
    225  1.1  jonathan 			sc->rtk_type = hw_rev->rtk_type;
    226  1.1  jonathan 			break;
    227  1.1  jonathan 		}
    228  1.1  jonathan 		hw_rev++;
    229  1.1  jonathan 	}
    230  1.1  jonathan 
    231  1.1  jonathan 	sc->sc_dmat = pa->pa_dmat;
    232  1.1  jonathan 
    233  1.1  jonathan 	/*
    234  1.1  jonathan 	 * No power/enable/disable machinery for PCI attac;
    235  1.1  jonathan 	 * mark the card enabled now.
    236  1.1  jonathan 	 */
    237  1.1  jonathan 	sc->sc_flags |= RTK_ENABLED;
    238  1.1  jonathan 
    239  1.1  jonathan 	/* Hook interrupt last to avoid having to lock softc */
    240  1.1  jonathan 	/* Allocate interrupt */
    241  1.1  jonathan 	if (pci_intr_map(pa, &ih)) {
    242  1.3   kanaoka 		aprint_error("%s: couldn't map interrupt\n",
    243  1.3   kanaoka 		    sc->sc_dev.dv_xname);
    244  1.2   kanaoka 		return;
    245  1.1  jonathan 	}
    246  1.1  jonathan 	intrstr = pci_intr_string(pc, ih);
    247  1.1  jonathan 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc);
    248  1.1  jonathan 	if (psc->sc_ih == NULL) {
    249  1.3   kanaoka 		aprint_error("%s: couldn't establish interrupt",
    250  1.1  jonathan 		    sc->sc_dev.dv_xname);
    251  1.1  jonathan 		if (intrstr != NULL)
    252  1.3   kanaoka 			aprint_error(" at %s", intrstr);
    253  1.3   kanaoka 		aprint_error("\n");
    254  1.1  jonathan 		return;
    255  1.1  jonathan 	}
    256  1.1  jonathan 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    257  1.1  jonathan 
    258  1.2   kanaoka 	re_attach(sc);
    259  1.2   kanaoka 
    260  1.2   kanaoka 	/*
    261  1.2   kanaoka 	 * Perform hardware diagnostic.
    262  1.2   kanaoka 	 * XXX: this diagnostic only makes sense for attachemnts with 64-bit
    263  1.2   kanaoka 	 * busses: PCI, but not CardBus.
    264  1.2   kanaoka 	 */
    265  1.2   kanaoka 	error = re_diag(sc);
    266  1.2   kanaoka 	if (error) {
    267  1.2   kanaoka 		aprint_error(
    268  1.2   kanaoka 		    "%s: attach aborted due to hardware diag failure\n",
    269  1.2   kanaoka 		    sc->sc_dev.dv_xname);
    270  1.2   kanaoka 
    271  1.1  jonathan 		re_detach(sc);
    272  1.2   kanaoka 
    273  1.2   kanaoka 		if (psc->sc_ih != NULL) {
    274  1.2   kanaoka 			pci_intr_disestablish(pc, psc->sc_ih);
    275  1.2   kanaoka 			psc->sc_ih = NULL;
    276  1.2   kanaoka 		}
    277  1.2   kanaoka 
    278  1.3   kanaoka 		if (bsize)
    279  1.2   kanaoka 			bus_space_unmap(sc->rtk_btag, sc->rtk_bhandle, bsize);
    280  1.2   kanaoka 	}
    281  1.1  jonathan }
    282