Home | History | Annotate | Line # | Download | only in pci
if_rtk_pci.c revision 1.20.4.1
      1 /*	$NetBSD: if_rtk_pci.c,v 1.20.4.1 2006/03/17 17:23:45 riz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997, 1998
      5  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  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  *	FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
     35  */
     36 
     37 /*
     38  * Realtek 8129/8139 PCI NIC driver
     39  *
     40  * Supports several extremely cheap PCI 10/100 adapters based on
     41  * the Realtek chipset. Datasheets can be obtained from
     42  * www.realtek.com.tw.
     43  *
     44  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     45  * Electrical Engineering Department
     46  * Columbia University, New York City
     47  */
     48 
     49 #include <sys/cdefs.h>
     50 __KERNEL_RCSID(0, "$NetBSD: if_rtk_pci.c,v 1.20.4.1 2006/03/17 17:23:45 riz Exp $");
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/callout.h>
     55 #include <sys/device.h>
     56 #include <sys/sockio.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/malloc.h>
     59 #include <sys/kernel.h>
     60 #include <sys/socket.h>
     61 
     62 #include <net/if.h>
     63 #include <net/if_arp.h>
     64 #include <net/if_ether.h>
     65 #include <net/if_dl.h>
     66 #include <net/if_media.h>
     67 
     68 #include <machine/bus.h>
     69 
     70 #include <dev/pci/pcireg.h>
     71 #include <dev/pci/pcivar.h>
     72 #include <dev/pci/pcidevs.h>
     73 
     74 #include <dev/mii/mii.h>
     75 #include <dev/mii/miivar.h>
     76 
     77 /*
     78  * Default to using PIO access for this driver. On SMP systems,
     79  * there appear to be problems with memory mapped mode: it looks like
     80  * doing too many memory mapped access back to back in rapid succession
     81  * can hang the bus. I'm inclined to blame this on crummy design/construction
     82  * on the part of Realtek. Memory mapped mode does appear to work on
     83  * uniprocessor systems though.
     84  */
     85 #ifndef dreamcast		/* XXX */
     86 #define RTK_USEIOSPACE
     87 #endif
     88 
     89 #include <dev/ic/rtl81x9reg.h>
     90 #include <dev/ic/rtl81x9var.h>
     91 
     92 struct rtk_pci_softc {
     93 	struct rtk_softc sc_rtk;	/* real rtk softc */
     94 
     95 	/* PCI-specific goo.*/
     96 	void *sc_ih;
     97 	pci_chipset_tag_t sc_pc; 	/* PCI chipset */
     98 	pcitag_t sc_pcitag;		/* PCI tag */
     99 };
    100 
    101 static const struct rtk_type rtk_pci_devs[] = {
    102 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129,
    103 		RTK_8129, "Realtek 8129 10/100BaseTX" },
    104 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
    105 		RTK_8139, "Realtek 8139 10/100BaseTX" },
    106 	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_MPX5030,
    107 		RTK_8139, "Accton MPX 5030/5038 10/100BaseTX" },
    108 	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139,
    109 		RTK_8139, "Delta Electronics 8139 10/100BaseTX" },
    110 	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139,
    111 		RTK_8139, "Addtron Technology 8139 10/100BaseTX" },
    112 	{ PCI_VENDOR_SEGA, PCI_PRODUCT_SEGA_BROADBAND,
    113 		RTK_8139, "SEGA Broadband Adapter" },
    114 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE530TXPLUS,
    115 		RTK_8139, "D-Link Systems DFE 530TX+" },
    116 	{ PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_BAYSTACK_21,
    117 		RTK_8139, "Baystack 21 (MPX EN5038) 10/100BaseTX" },
    118 	{ 0, 0, 0, NULL }
    119 };
    120 
    121 const struct rtk_type *rtk_pci_lookup __P((const struct pci_attach_args *));
    122 
    123 int	rtk_pci_match __P((struct device *, struct cfdata *, void *));
    124 void	rtk_pci_attach __P((struct device *, struct device *, void *));
    125 
    126 CFATTACH_DECL(rtk_pci, sizeof(struct rtk_pci_softc),
    127     rtk_pci_match, rtk_pci_attach, NULL, NULL);
    128 
    129 const struct rtk_type *
    130 rtk_pci_lookup(pa)
    131 	const struct pci_attach_args *pa;
    132 {
    133 	const struct rtk_type *t;
    134 
    135 	for (t = rtk_pci_devs; t->rtk_name != NULL; t++) {
    136 		if (PCI_VENDOR(pa->pa_id) == t->rtk_vid &&
    137 		    PCI_PRODUCT(pa->pa_id) == t->rtk_did) {
    138 			return (t);
    139 		}
    140 	}
    141 	return (NULL);
    142 }
    143 
    144 int
    145 rtk_pci_match(parent, match, aux)
    146 	struct device *parent;
    147 	struct cfdata *match;
    148 	void *aux;
    149 {
    150 	struct pci_attach_args *pa = aux;
    151 
    152 	if (rtk_pci_lookup(pa) != NULL)
    153 		return (1);
    154 
    155 	return (0);
    156 }
    157 
    158 /*
    159  * Attach the interface. Allocate softc structures, do ifmedia
    160  * setup and ethernet/BPF attach.
    161  */
    162 void
    163 rtk_pci_attach(parent, self, aux)
    164 	struct device *parent, *self;
    165 	void *aux;
    166 {
    167 	struct rtk_pci_softc *psc = (struct rtk_pci_softc *)self;
    168 	struct rtk_softc *sc = &psc->sc_rtk;
    169 	pcireg_t command;
    170 	struct pci_attach_args *pa = aux;
    171 	pci_chipset_tag_t pc = pa->pa_pc;
    172 	pci_intr_handle_t ih;
    173 	const char *intrstr = NULL;
    174 	const struct rtk_type *t;
    175 	int pmreg;
    176 
    177 	psc->sc_pc = pa->pa_pc;
    178 	psc->sc_pcitag = pa->pa_tag;
    179 
    180 	t = rtk_pci_lookup(pa);
    181 	if (t == NULL) {
    182 		printf("\n");
    183 		panic("rtk_pci_attach: impossible");
    184 	}
    185 	printf(": %s\n", t->rtk_name);
    186 
    187 	/*
    188 	 * Handle power management nonsense.
    189 	 */
    190 
    191 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    192 		command = pci_conf_read(pc, pa->pa_tag, pmreg + 4);
    193 		if (command & RTK_PSTATE_MASK) {
    194 			pcireg_t iobase, membase, irq;
    195 
    196 			/* Save important PCI config data. */
    197 			iobase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOIO);
    198 			membase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOMEM);
    199 			irq = pci_conf_read(pc, pa->pa_tag, PCI_INTERRUPT_REG);
    200 
    201 			/* Reset the power state. */
    202 			printf("%s: chip is in D%d power mode "
    203 			    "-- setting to D0\n", sc->sc_dev.dv_xname,
    204 			    command & RTK_PSTATE_MASK);
    205 			command &= 0xFFFFFFFC;
    206 			pci_conf_write(pc, pa->pa_tag, pmreg + 4, command);
    207 
    208 			/* Restore PCI config data. */
    209 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOIO, iobase);
    210 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOMEM, membase);
    211 			pci_conf_write(pc, pa->pa_tag, PCI_INTERRUPT_REG, irq);
    212 		}
    213 	}
    214 
    215 	/*
    216 	 * Map control/status registers.
    217 	 */
    218 #ifdef RTK_USEIOSPACE
    219 	if (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    220 	    &sc->rtk_btag, &sc->rtk_bhandle, NULL, NULL)) {
    221 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    222 		return;
    223 	}
    224 #else
    225 	if (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    226 	    &sc->rtk_btag, &sc->rtk_bhandle, NULL, NULL)) {
    227 		printf("%s: can't map mem space\n", sc->sc_dev.dv_xname);
    228 		return;
    229 	}
    230 #endif
    231 
    232 	/* Allocate interrupt */
    233 	if (pci_intr_map(pa, &ih)) {
    234 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    235 		return;
    236 	}
    237 	intrstr = pci_intr_string(pc, ih);
    238 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rtk_intr, sc);
    239 	if (psc->sc_ih == NULL) {
    240 		printf("%s: couldn't establish interrupt",
    241 		    sc->sc_dev.dv_xname);
    242 		if (intrstr != NULL)
    243 			printf(" at %s", intrstr);
    244 		printf("\n");
    245 		return;
    246 	}
    247 
    248 	sc->rtk_type = t->rtk_basetype;
    249 
    250 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    251 
    252 	sc->sc_dmat = pa->pa_dmat;
    253 	sc->sc_flags |= RTK_ENABLED;
    254 
    255 	rtk_attach(sc);
    256 }
    257