Home | History | Annotate | Line # | Download | only in pci
if_re_pci.c revision 1.4
      1  1.4      yamt /*	$NetBSD: if_re_pci.c,v 1.4 2005/01/18 11:11:58 yamt Exp $	*/
      2  1.4      yamt 
      3  1.4      yamt /*
      4  1.4      yamt  * Copyright (c) 1997, 1998-2003
      5  1.4      yamt  *	Bill Paul <wpaul (at) windriver.com>.  All rights reserved.
      6  1.4      yamt  *
      7  1.4      yamt  * Redistribution and use in source and binary forms, with or without
      8  1.4      yamt  * modification, are permitted provided that the following conditions
      9  1.4      yamt  * are met:
     10  1.4      yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.4      yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.4      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.4      yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.4      yamt  *    documentation and/or other materials provided with the distribution.
     15  1.4      yamt  * 3. All advertising materials mentioning features or use of this software
     16  1.4      yamt  *    must display the following acknowledgement:
     17  1.4      yamt  *	This product includes software developed by Bill Paul.
     18  1.4      yamt  * 4. Neither the name of the author nor the names of any co-contributors
     19  1.4      yamt  *    may be used to endorse or promote products derived from this software
     20  1.4      yamt  *    without specific prior written permission.
     21  1.4      yamt  *
     22  1.4      yamt  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  1.4      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.4      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.4      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  1.4      yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.4      yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.4      yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.4      yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.4      yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.4      yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  1.4      yamt  * THE POSSIBILITY OF SUCH DAMAGE.
     33  1.4      yamt  */
     34  1.4      yamt 
     35  1.4      yamt /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
     36  1.4      yamt 
     37  1.1  jonathan /*
     38  1.1  jonathan  * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
     39  1.1  jonathan  *
     40  1.1  jonathan  * Written by Bill Paul <wpaul (at) windriver.com>
     41  1.1  jonathan  * Senior Networking Software Engineer
     42  1.1  jonathan  * Wind River Systems
     43  1.1  jonathan  *
     44  1.1  jonathan  * NetBSD bus-specific frontends for written by
     45  1.1  jonathan  * Jonathan Stone <jonathan (at) netbsd.org>
     46  1.1  jonathan  */
     47  1.1  jonathan 
     48  1.1  jonathan #include "bpfilter.h"
     49  1.1  jonathan #include "vlan.h"
     50  1.1  jonathan 
     51  1.1  jonathan #include <sys/types.h>
     52  1.1  jonathan 
     53  1.1  jonathan #include <machine/bus.h>
     54  1.1  jonathan 
     55  1.1  jonathan #include <dev/pci/pcireg.h>
     56  1.1  jonathan #include <dev/pci/pcivar.h>
     57  1.1  jonathan #include <dev/pci/pcidevs.h>
     58  1.1  jonathan 
     59  1.1  jonathan #include <sys/param.h>
     60  1.1  jonathan #include <sys/endian.h>
     61  1.1  jonathan #include <sys/systm.h>
     62  1.1  jonathan #include <sys/sockio.h>
     63  1.1  jonathan #include <sys/mbuf.h>
     64  1.1  jonathan #include <sys/malloc.h>
     65  1.1  jonathan #include <sys/kernel.h>
     66  1.1  jonathan #include <sys/socket.h>
     67  1.1  jonathan #include <sys/device.h>
     68  1.1  jonathan 
     69  1.1  jonathan #include <net/if.h>
     70  1.1  jonathan #include <net/if_arp.h>
     71  1.1  jonathan #include <net/if_dl.h>
     72  1.1  jonathan #include <net/if_ether.h>
     73  1.1  jonathan #include <net/if_media.h>
     74  1.1  jonathan #include <net/if_vlanvar.h>
     75  1.1  jonathan 
     76  1.1  jonathan #include <dev/mii/mii.h>
     77  1.1  jonathan #include <dev/mii/miivar.h>
     78  1.1  jonathan 
     79  1.1  jonathan /*
     80  1.1  jonathan  * Default to using PIO access for this driver.
     81  1.1  jonathan  */
     82  1.1  jonathan #define RE_USEIOSPACE
     83  1.1  jonathan 
     84  1.1  jonathan #include <dev/ic/rtl81x9reg.h>
     85  1.1  jonathan #include <dev/ic/rtl81x9var.h>
     86  1.1  jonathan #include <dev/ic/rtl8169var.h>
     87  1.1  jonathan 
     88  1.1  jonathan struct re_pci_softc {
     89  1.1  jonathan 	struct rtk_softc sc_rtk;
     90  1.1  jonathan 
     91  1.1  jonathan 	void *sc_ih;
     92  1.1  jonathan 	pci_chipset_tag_t sc_pc;
     93  1.1  jonathan 	pcitag_t sc_pcitag;
     94  1.1  jonathan };
     95  1.1  jonathan 
     96  1.1  jonathan static int	re_pci_probe(struct device *, struct cfdata *, void *);
     97  1.1  jonathan static void	re_pci_attach(struct device *, struct device *, void *);
     98  1.1  jonathan 
     99  1.1  jonathan /*
    100  1.1  jonathan  * Various supported device vendors/types and their names.
    101  1.1  jonathan  */
    102  1.1  jonathan static struct rtk_type re_devs[] = {
    103  1.1  jonathan 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139, RTK_HWREV_8139CPLUS,
    104  1.1  jonathan 		"RealTek 8139C+ 10/100BaseTX" },
    105  1.1  jonathan 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RTK_HWREV_8169,
    106  1.1  jonathan 		"RealTek 8169 Gigabit Ethernet" },
    107  1.1  jonathan 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RTK_HWREV_8169S,
    108  1.1  jonathan 		"RealTek 8169S Single-chip Gigabit Ethernet" },
    109  1.1  jonathan 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RTK_HWREV_8110S,
    110  1.1  jonathan 		"RealTek 8110S Single-chip Gigabit Ethernet" },
    111  1.1  jonathan 	{ 0, 0, 0, NULL }
    112  1.1  jonathan };
    113  1.1  jonathan 
    114  1.1  jonathan static struct rtk_hwrev re_hwrevs[] = {
    115  1.1  jonathan 	{ RTK_HWREV_8139, RTK_8139,  "" },
    116  1.1  jonathan 	{ RTK_HWREV_8139A, RTK_8139, "A" },
    117  1.1  jonathan 	{ RTK_HWREV_8139AG, RTK_8139, "A-G" },
    118  1.1  jonathan 	{ RTK_HWREV_8139B, RTK_8139, "B" },
    119  1.1  jonathan 	{ RTK_HWREV_8130, RTK_8139, "8130" },
    120  1.1  jonathan 	{ RTK_HWREV_8139C, RTK_8139, "C" },
    121  1.1  jonathan 	{ RTK_HWREV_8139D, RTK_8139, "8139D/8100B/8100C" },
    122  1.1  jonathan 	{ RTK_HWREV_8139CPLUS, RTK_8139CPLUS, "C+"},
    123  1.1  jonathan 	{ RTK_HWREV_8169, RTK_8169, "8169"},
    124  1.1  jonathan 	{ RTK_HWREV_8169S, RTK_8169, "8169S"},
    125  1.1  jonathan 	{ RTK_HWREV_8110S, RTK_8169, "8110S"},
    126  1.1  jonathan 	{ RTK_HWREV_8100, RTK_8139, "8100"},
    127  1.1  jonathan 	{ RTK_HWREV_8101, RTK_8139, "8101"},
    128  1.1  jonathan 	{ 0, 0, NULL }
    129  1.1  jonathan };
    130  1.1  jonathan 
    131  1.1  jonathan CFATTACH_DECL(re_pci, sizeof(struct re_pci_softc), re_pci_probe, re_pci_attach,
    132  1.1  jonathan 	      NULL, NULL);
    133  1.1  jonathan 
    134  1.1  jonathan /*
    135  1.1  jonathan  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
    136  1.1  jonathan  * IDs against our list and return a device name if we find a match.
    137  1.1  jonathan  */
    138  1.1  jonathan static int
    139  1.1  jonathan re_pci_probe(struct device *parent, struct cfdata *match, void *aux)
    140  1.1  jonathan {
    141  1.1  jonathan 	struct rtk_type		*t;
    142  1.1  jonathan 	struct pci_attach_args	*pa = aux;
    143  1.1  jonathan 	bus_space_tag_t		rtk_btag;
    144  1.1  jonathan 	bus_space_handle_t	rtk_bhandle;
    145  1.1  jonathan 	bus_size_t		bsize;
    146  1.1  jonathan 	u_int32_t		hwrev;
    147  1.1  jonathan 
    148  1.1  jonathan 	t = re_devs;
    149  1.1  jonathan 
    150  1.3   kanaoka 	while (t->rtk_name != NULL) {
    151  1.1  jonathan 		if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
    152  1.1  jonathan 		    (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
    153  1.1  jonathan 
    154  1.1  jonathan 			/*
    155  1.1  jonathan 			 * Temporarily map the I/O space
    156  1.1  jonathan 			 * so we can read the chip ID register.
    157  1.1  jonathan 			 */
    158  1.1  jonathan 			if (pci_mapreg_map(pa, RTK_PCI_LOIO,
    159  1.1  jonathan 			    PCI_MAPREG_TYPE_IO, 0, &rtk_btag,
    160  1.1  jonathan 			    &rtk_bhandle, NULL, &bsize)) {
    161  1.3   kanaoka 				aprint_error("can't map i/o space\n");
    162  1.1  jonathan 				return 0;
    163  1.1  jonathan 			}
    164  1.1  jonathan 			hwrev = bus_space_read_4(rtk_btag, rtk_bhandle,
    165  1.1  jonathan 			    RTK_TXCFG) & RTK_TXCFG_HWREV;
    166  1.1  jonathan 			bus_space_unmap(rtk_btag, rtk_bhandle, bsize);
    167  1.1  jonathan 			if (t->rtk_basetype == hwrev)
    168  1.1  jonathan 				return 2;	/* defeat rtk(4) */
    169  1.1  jonathan 		}
    170  1.1  jonathan 		t++;
    171  1.1  jonathan 	}
    172  1.1  jonathan 
    173  1.1  jonathan 	return 0;
    174  1.1  jonathan }
    175  1.1  jonathan 
    176  1.1  jonathan static void
    177  1.1  jonathan re_pci_attach(struct device *parent, struct device *self, void *aux)
    178  1.1  jonathan {
    179  1.1  jonathan 	struct re_pci_softc	*psc = (void *)self;
    180  1.1  jonathan 	struct rtk_softc	*sc = &psc->sc_rtk;
    181  1.1  jonathan 	struct pci_attach_args 	*pa = aux;
    182  1.1  jonathan 	pci_chipset_tag_t pc = pa->pa_pc;
    183  1.1  jonathan 	pci_intr_handle_t ih;
    184  1.1  jonathan 	const char *intrstr = NULL;
    185  1.1  jonathan 	struct rtk_type		*t;
    186  1.1  jonathan 	struct rtk_hwrev	*hw_rev;
    187  1.1  jonathan 	int			hwrev;
    188  1.1  jonathan 	int			error = 0;
    189  1.3   kanaoka 	int			pmreg;
    190  1.1  jonathan 	pcireg_t		command;
    191  1.2   kanaoka 	bus_size_t		bsize;
    192  1.1  jonathan 
    193  1.1  jonathan 
    194  1.1  jonathan 	/*
    195  1.1  jonathan 	 * Handle power management nonsense.
    196  1.1  jonathan 	 */
    197  1.3   kanaoka 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    198  1.3   kanaoka 		command = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR);
    199  1.3   kanaoka 		if (command & RTK_PSTATE_MASK) {
    200  1.3   kanaoka 			u_int32_t		iobase, membase, irq;
    201  1.3   kanaoka 
    202  1.3   kanaoka 			/* Save important PCI config data. */
    203  1.3   kanaoka 			iobase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOIO);
    204  1.3   kanaoka 			membase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOMEM);
    205  1.3   kanaoka 			irq = pci_conf_read(pc, pa->pa_tag, PCI_INTERRUPT_REG);
    206  1.3   kanaoka 
    207  1.3   kanaoka 			/* Reset the power state. */
    208  1.3   kanaoka 			aprint_normal("%s: chip is is in D%d power mode "
    209  1.3   kanaoka 		    	    "-- setting to D0\n", sc->sc_dev.dv_xname,
    210  1.3   kanaoka 		    	    command & RTK_PSTATE_MASK);
    211  1.3   kanaoka 
    212  1.3   kanaoka 			command &= ~RTK_PSTATE_MASK;
    213  1.3   kanaoka 			pci_conf_write(pc, pa->pa_tag,
    214  1.3   kanaoka 			    pmreg + PCI_PMCSR, command);
    215  1.3   kanaoka 
    216  1.3   kanaoka 			/* Restore PCI config data. */
    217  1.3   kanaoka 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOIO, iobase);
    218  1.3   kanaoka 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOMEM, membase);
    219  1.3   kanaoka 			pci_conf_write(pc, pa->pa_tag, PCI_INTERRUPT_REG, irq);
    220  1.3   kanaoka 		}
    221  1.3   kanaoka 	}
    222  1.1  jonathan 
    223  1.1  jonathan 	/*
    224  1.1  jonathan 	 * Map control/status registers.
    225  1.1  jonathan 	 */
    226  1.1  jonathan 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    227  1.1  jonathan 	command |= PCI_COMMAND_MASTER_ENABLE;
    228  1.1  jonathan 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
    229  1.1  jonathan 
    230  1.1  jonathan #ifdef RE_USEIOSPACE
    231  1.1  jonathan 	if (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    232  1.2   kanaoka 	    &sc->rtk_btag, &sc->rtk_bhandle, NULL, &bsize)) {
    233  1.3   kanaoka 		aprint_error("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    234  1.2   kanaoka 		return;
    235  1.1  jonathan 	}
    236  1.1  jonathan #else
    237  1.1  jonathan 	if (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    238  1.2   kanaoka 	    &sc->rtk_btag, &sc->rtk_bhandle, NULL, &bsize)) {
    239  1.3   kanaoka 		aprint_error("%s: can't map mem space\n", sc->sc_dev.dv_xname);
    240  1.2   kanaoka 		return;
    241  1.1  jonathan 	}
    242  1.1  jonathan #endif
    243  1.1  jonathan 	t = re_devs;
    244  1.1  jonathan 	hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
    245  1.1  jonathan 
    246  1.3   kanaoka 	while (t->rtk_name != NULL) {
    247  1.1  jonathan 		if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
    248  1.1  jonathan 		    (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
    249  1.1  jonathan 
    250  1.1  jonathan 			if (t->rtk_basetype == hwrev)
    251  1.1  jonathan 				break;
    252  1.1  jonathan 		}
    253  1.1  jonathan 		t++;
    254  1.1  jonathan 	}
    255  1.3   kanaoka 	aprint_normal(": %s\n", t->rtk_name);
    256  1.1  jonathan 
    257  1.1  jonathan 	hw_rev = re_hwrevs;
    258  1.1  jonathan 	hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
    259  1.1  jonathan 	while (hw_rev->rtk_desc != NULL) {
    260  1.1  jonathan 		if (hw_rev->rtk_rev == hwrev) {
    261  1.1  jonathan 			sc->rtk_type = hw_rev->rtk_type;
    262  1.1  jonathan 			break;
    263  1.1  jonathan 		}
    264  1.1  jonathan 		hw_rev++;
    265  1.1  jonathan 	}
    266  1.1  jonathan 
    267  1.1  jonathan 	sc->sc_dmat = pa->pa_dmat;
    268  1.1  jonathan 
    269  1.1  jonathan 	/*
    270  1.1  jonathan 	 * No power/enable/disable machinery for PCI attac;
    271  1.1  jonathan 	 * mark the card enabled now.
    272  1.1  jonathan 	 */
    273  1.1  jonathan 	sc->sc_flags |= RTK_ENABLED;
    274  1.1  jonathan 
    275  1.1  jonathan 	/* Hook interrupt last to avoid having to lock softc */
    276  1.1  jonathan 	/* Allocate interrupt */
    277  1.1  jonathan 	if (pci_intr_map(pa, &ih)) {
    278  1.3   kanaoka 		aprint_error("%s: couldn't map interrupt\n",
    279  1.3   kanaoka 		    sc->sc_dev.dv_xname);
    280  1.2   kanaoka 		return;
    281  1.1  jonathan 	}
    282  1.1  jonathan 	intrstr = pci_intr_string(pc, ih);
    283  1.1  jonathan 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc);
    284  1.1  jonathan 	if (psc->sc_ih == NULL) {
    285  1.3   kanaoka 		aprint_error("%s: couldn't establish interrupt",
    286  1.1  jonathan 		    sc->sc_dev.dv_xname);
    287  1.1  jonathan 		if (intrstr != NULL)
    288  1.3   kanaoka 			aprint_error(" at %s", intrstr);
    289  1.3   kanaoka 		aprint_error("\n");
    290  1.1  jonathan 		return;
    291  1.1  jonathan 	}
    292  1.1  jonathan 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    293  1.1  jonathan 
    294  1.2   kanaoka 	re_attach(sc);
    295  1.2   kanaoka 
    296  1.2   kanaoka 	/*
    297  1.2   kanaoka 	 * Perform hardware diagnostic.
    298  1.2   kanaoka 	 * XXX: this diagnostic only makes sense for attachemnts with 64-bit
    299  1.2   kanaoka 	 * busses: PCI, but not CardBus.
    300  1.2   kanaoka 	 */
    301  1.2   kanaoka 	error = re_diag(sc);
    302  1.2   kanaoka 	if (error) {
    303  1.2   kanaoka 		aprint_error(
    304  1.2   kanaoka 		    "%s: attach aborted due to hardware diag failure\n",
    305  1.2   kanaoka 		    sc->sc_dev.dv_xname);
    306  1.2   kanaoka 
    307  1.1  jonathan 		re_detach(sc);
    308  1.2   kanaoka 
    309  1.2   kanaoka 		if (psc->sc_ih != NULL) {
    310  1.2   kanaoka 			pci_intr_disestablish(pc, psc->sc_ih);
    311  1.2   kanaoka 			psc->sc_ih = NULL;
    312  1.2   kanaoka 		}
    313  1.2   kanaoka 
    314  1.3   kanaoka 		if (bsize)
    315  1.2   kanaoka 			bus_space_unmap(sc->rtk_btag, sc->rtk_bhandle, bsize);
    316  1.2   kanaoka 	}
    317  1.1  jonathan }
    318