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