Home | History | Annotate | Line # | Download | only in pci
if_re_pci.c revision 1.43.6.1
      1 /*	$NetBSD: if_re_pci.c,v 1.43.6.1 2015/06/06 14:40:09 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998-2003
      5  *	Bill Paul <wpaul (at) windriver.com>.  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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
     36 
     37 /*
     38  * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
     39  *
     40  * Written by Bill Paul <wpaul (at) windriver.com>
     41  * Senior Networking Software Engineer
     42  * Wind River Systems
     43  *
     44  * NetBSD bus-specific frontends for written by
     45  * Jonathan Stone <jonathan (at) netbsd.org>
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: if_re_pci.c,v 1.43.6.1 2015/06/06 14:40:09 skrll Exp $");
     50 
     51 #include <sys/types.h>
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/device.h>
     56 
     57 #include <net/if.h>
     58 #include <net/if_ether.h>
     59 #include <net/if_media.h>
     60 
     61 #include <sys/bus.h>
     62 
     63 #include <dev/mii/mii.h>
     64 #include <dev/mii/miivar.h>
     65 
     66 #include <dev/pci/pcireg.h>
     67 #include <dev/pci/pcivar.h>
     68 #include <dev/pci/pcidevs.h>
     69 
     70 #include <dev/ic/rtl81x9reg.h>
     71 #include <dev/ic/rtl81x9var.h>
     72 #include <dev/ic/rtl8169var.h>
     73 
     74 struct re_pci_softc {
     75 	struct rtk_softc sc_rtk;
     76 
     77 	void *sc_ih;
     78 	pci_chipset_tag_t sc_pc;
     79 };
     80 
     81 static int	re_pci_match(device_t, cfdata_t, void *);
     82 static void	re_pci_attach(device_t, device_t, void *);
     83 static int	re_pci_detach(device_t, int);
     84 
     85 CFATTACH_DECL_NEW(re_pci, sizeof(struct re_pci_softc),
     86     re_pci_match, re_pci_attach, re_pci_detach, NULL);
     87 
     88 /*
     89  * Various supported device vendors/types and their names.
     90  */
     91 static const struct rtk_type re_devs[] = {
     92 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
     93 	    RTK_8139CPLUS,
     94 	    "RealTek 8139C+ 10/100BaseTX" },
     95 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8101E,
     96 	    RTK_8101E,
     97 	    "RealTek 8100E/8101E/8102E/8102EL PCIe 10/100BaseTX" },
     98 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168,
     99 	    RTK_8168,
    100 	    "RealTek 8168/8111 PCIe Gigabit Ethernet" },
    101 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169,
    102 	    RTK_8169,
    103 	    "RealTek 8169/8110 Gigabit Ethernet" },
    104 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169SC,
    105 	    RTK_8169,
    106 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
    107 	{ PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_LAPCIGT,
    108 	    RTK_8169,
    109 	    "Corega CG-LAPCIGT Gigabit Ethernet" },
    110 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T,
    111 	    RTK_8169,
    112 	    "D-Link DGE-528T Gigabit Ethernet" },
    113 	{ PCI_VENDOR_USR2, PCI_PRODUCT_USR2_USR997902,
    114 	    RTK_8169,
    115 	    "US Robotics (3Com) USR997902 Gigabit Ethernet" },
    116 	{ PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1032,
    117 	    RTK_8169,
    118 	    "Linksys EG1032 rev. 3 Gigabit Ethernet" },
    119 };
    120 
    121 static const struct rtk_type *
    122 re_pci_lookup(const struct pci_attach_args * pa)
    123 {
    124 	int i;
    125 
    126 	for(i = 0; i < __arraycount(re_devs); i++) {
    127 		if (PCI_VENDOR(pa->pa_id) != re_devs[i].rtk_vid)
    128 			continue;
    129 		if (PCI_PRODUCT(pa->pa_id) == re_devs[i].rtk_did)
    130 			return &re_devs[i];
    131 	}
    132 
    133 	return NULL;
    134 }
    135 
    136 #define RE_LINKSYS_EG1032_SUBID	0x00241737
    137 
    138 /*
    139  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
    140  * IDs against our list and return a device name if we find a match.
    141  */
    142 static int
    143 re_pci_match(device_t parent, cfdata_t cf, void *aux)
    144 {
    145 	struct pci_attach_args *pa = aux;
    146 	pcireg_t subid;
    147 
    148 	subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
    149 
    150 	/* special-case Linksys EG1032, since rev 2 uses sk(4) */
    151 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS &&
    152 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032) {
    153 		if (subid != RE_LINKSYS_EG1032_SUBID)
    154 			return 0;
    155 	}
    156 
    157 	/* Don't match 8139 other than C-PLUS */
    158 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK &&
    159 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139) {
    160 		if (PCI_REVISION(pa->pa_class) != 0x20)
    161 			return 0;
    162 	}
    163 
    164 	if (re_pci_lookup(pa) != NULL)
    165 		return 2;	/* defeat rtk(4) */
    166 
    167 	return 0;
    168 }
    169 
    170 static void
    171 re_pci_attach(device_t parent, device_t self, void *aux)
    172 {
    173 	struct re_pci_softc *psc = device_private(self);
    174 	struct rtk_softc *sc = &psc->sc_rtk;
    175 	struct pci_attach_args *pa = aux;
    176 	pci_chipset_tag_t pc = pa->pa_pc;
    177 	pci_intr_handle_t ih;
    178 	const char *intrstr = NULL;
    179 	const struct rtk_type *t;
    180 	uint32_t hwrev;
    181 	pcireg_t command, memtype;
    182 	bool ioh_valid, memh_valid;
    183 	bus_space_tag_t iot, memt;
    184 	bus_space_handle_t ioh, memh;
    185 	bus_size_t iosize, memsize;
    186 	char intrbuf[PCI_INTRSTR_LEN];
    187 
    188 	sc->sc_dev = self;
    189 	psc->sc_pc = pa->pa_pc;
    190 
    191 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    192 	command |= PCI_COMMAND_MASTER_ENABLE;
    193 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
    194 
    195 	/*
    196 	 * Map control/status registers.
    197 	 */
    198 	ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    199 	    &iot, &ioh, NULL, &iosize) == 0);
    200 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RTK_PCI_LOMEM);
    201 	switch (memtype) {
    202 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    203 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    204 		memh_valid =
    205 		    (pci_mapreg_map(pa, RTK_PCI_LOMEM,
    206 		        memtype, 0, &memt, &memh, NULL, &memsize) == 0) ||
    207 		    (pci_mapreg_map(pa, RTK_PCI_LOMEM + 4,
    208 			PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT,
    209 			0, &memt, &memh, NULL, &memsize) == 0);
    210 		break;
    211 	default:
    212 		memh_valid = 0;
    213 		break;
    214 	}
    215 
    216 	if (ioh_valid) {
    217 		sc->rtk_btag = iot;
    218 		sc->rtk_bhandle = ioh;
    219 		sc->rtk_bsize = iosize;
    220 		if (memh_valid)
    221 			bus_space_unmap(memt, memh, memsize);
    222 	} else if (memh_valid) {
    223 		sc->rtk_btag = memt;
    224 		sc->rtk_bhandle = memh;
    225 		sc->rtk_bsize = memsize;
    226 	} else {
    227 		aprint_error(": can't map registers\n");
    228 		return;
    229 	}
    230 
    231 	t = re_pci_lookup(pa);
    232 	KASSERT(t != NULL);
    233 
    234 	pci_aprint_devinfo_fancy(pa, NULL, t->rtk_name, 1);
    235 
    236 	if (t->rtk_basetype == RTK_8139CPLUS)
    237 		sc->sc_quirk |= RTKQ_8139CPLUS;
    238 
    239 	if (t->rtk_basetype == RTK_8168 ||
    240 	    t->rtk_basetype == RTK_8101E)
    241 		sc->sc_quirk |= RTKQ_PCIE;
    242 
    243 	if (pci_dma64_available(pa) && (sc->sc_quirk & RTKQ_PCIE))
    244 		sc->sc_dmat = pa->pa_dmat64;
    245 	else
    246 		sc->sc_dmat = pa->pa_dmat;
    247 
    248 	/*
    249 	 * No power/enable/disable machinery for PCI attach;
    250 	 * mark the card enabled now.
    251 	 */
    252 	sc->sc_flags |= RTK_ENABLED;
    253 
    254 	/* Hook interrupt last to avoid having to lock softc */
    255 	/* Allocate interrupt */
    256 	if (pci_intr_map(pa, &ih)) {
    257 		aprint_error_dev(self, "couldn't map interrupt\n");
    258 		return;
    259 	}
    260 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
    261 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc);
    262 	if (psc->sc_ih == NULL) {
    263 		aprint_error_dev(self, "couldn't establish interrupt");
    264 		if (intrstr != NULL)
    265 			aprint_error(" at %s", intrstr);
    266 		aprint_error("\n");
    267 		return;
    268 	}
    269 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    270 
    271 	re_attach(sc);
    272 
    273 	hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
    274 
    275 	/*
    276 	 * Perform hardware diagnostic on the original RTL8169.
    277 	 * Some 32-bit cards were incorrectly wired and would
    278 	 * malfunction if plugged into a 64-bit slot.
    279 	 */
    280 	if (hwrev == RTK_HWREV_8169) {
    281 		if (re_diag(sc)) {
    282 			re_pci_detach(self, 0);
    283 			aprint_error_dev(self, "disabled\n");
    284 		}
    285 	}
    286 }
    287 
    288 static int
    289 re_pci_detach(device_t self, int flags)
    290 {
    291 	struct re_pci_softc *psc = device_private(self);
    292 	struct rtk_softc *sc = &psc->sc_rtk;
    293 	int rv;
    294 
    295 	if ((sc->sc_flags & RTK_ATTACHED) != 0)
    296 		/* re_stop() */
    297 		sc->ethercom.ec_if.if_stop(&sc->ethercom.ec_if, 0);
    298 
    299 	rv = re_detach(sc);
    300 	if (rv)
    301 		return rv;
    302 
    303 	if (psc->sc_ih != NULL) {
    304 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
    305 		psc->sc_ih = NULL;
    306 	}
    307 
    308 	if (sc->rtk_bsize != 0) {
    309 		bus_space_unmap(sc->rtk_btag, sc->rtk_bhandle, sc->rtk_bsize);
    310 		sc->rtk_bsize = 0;
    311 	}
    312 
    313 	return 0;
    314 }
    315