Home | History | Annotate | Line # | Download | only in pci
if_rtk_pci.c revision 1.32.8.1
      1 /*	$NetBSD: if_rtk_pci.c,v 1.32.8.1 2007/10/05 00:37:20 joerg 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.32.8.1 2007/10/05 00:37:20 joerg 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 #include <dev/ic/rtl81x9reg.h>
     78 #include <dev/ic/rtl81x9var.h>
     79 
     80 struct rtk_pci_softc {
     81 	struct rtk_softc sc_rtk;	/* real rtk softc */
     82 
     83 	/* PCI-specific goo.*/
     84 	void *sc_ih;
     85 };
     86 
     87 static const struct rtk_type rtk_pci_devs[] = {
     88 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129,
     89 		RTK_8129, "Realtek 8129 10/100BaseTX" },
     90 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
     91 		RTK_8139, "Realtek 8139 10/100BaseTX" },
     92 	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_MPX5030,
     93 		RTK_8139, "Accton MPX 5030/5038 10/100BaseTX" },
     94 	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139,
     95 		RTK_8139, "Delta Electronics 8139 10/100BaseTX" },
     96 	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139,
     97 		RTK_8139, "Addtron Technology 8139 10/100BaseTX" },
     98 	{ PCI_VENDOR_SEGA, PCI_PRODUCT_SEGA_BROADBAND,
     99 		RTK_8139, "SEGA Broadband Adapter" },
    100 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE530TXPLUS,
    101 		RTK_8139, "D-Link Systems DFE 530TX+" },
    102 	{ PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_BAYSTACK_21,
    103 		RTK_8139, "Baystack 21 (MPX EN5038) 10/100BaseTX" },
    104 	{ 0, 0, 0, NULL }
    105 };
    106 
    107 static int	rtk_pci_match(struct device *, struct cfdata *, void *);
    108 static void	rtk_pci_attach(struct device *, struct device *, void *);
    109 
    110 CFATTACH_DECL(rtk_pci, sizeof(struct rtk_pci_softc),
    111     rtk_pci_match, rtk_pci_attach, NULL, NULL);
    112 
    113 static const struct rtk_type *
    114 rtk_pci_lookup(const struct pci_attach_args *pa)
    115 {
    116 	const struct rtk_type *t;
    117 
    118 	for (t = rtk_pci_devs; t->rtk_name != NULL; t++) {
    119 		if (PCI_VENDOR(pa->pa_id) == t->rtk_vid &&
    120 		    PCI_PRODUCT(pa->pa_id) == t->rtk_did) {
    121 			return (t);
    122 		}
    123 	}
    124 	return (NULL);
    125 }
    126 
    127 static int
    128 rtk_pci_match(struct device *parent, struct cfdata *match,
    129     void *aux)
    130 {
    131 	struct pci_attach_args *pa = aux;
    132 
    133 	if (rtk_pci_lookup(pa) != NULL)
    134 		return (1);
    135 
    136 	return (0);
    137 }
    138 
    139 /*
    140  * Attach the interface. Allocate softc structures, do ifmedia
    141  * setup and ethernet/BPF attach.
    142  */
    143 static void
    144 rtk_pci_attach(struct device *parent, struct device *self, void *aux)
    145 {
    146 	struct rtk_pci_softc *psc = (struct rtk_pci_softc *)self;
    147 	struct rtk_softc *sc = &psc->sc_rtk;
    148 	pcireg_t command;
    149 	struct pci_attach_args *pa = aux;
    150 	pci_chipset_tag_t pc = pa->pa_pc;
    151 	pci_intr_handle_t ih;
    152 	bus_space_tag_t iot, memt;
    153 	bus_space_handle_t ioh, memh;
    154 	const char *intrstr = NULL;
    155 	const struct rtk_type *t;
    156 	int pmreg;
    157 	int ioh_valid, memh_valid;
    158 	pnp_status_t pnp_status;
    159 
    160 	t = rtk_pci_lookup(pa);
    161 	if (t == NULL) {
    162 		printf("\n");
    163 		panic("rtk_pci_attach: impossible");
    164 	}
    165 	printf(": %s (rev. 0x%02x)\n", t->rtk_name, PCI_REVISION(pa->pa_class));
    166 
    167 	/*
    168 	 * Handle power management nonsense.
    169 	 */
    170 
    171 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    172 		command = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR);
    173 		if (command & PCI_PMCSR_STATE_MASK) {
    174 			pcireg_t iobase, membase, irq;
    175 
    176 			/* Save important PCI config data. */
    177 			iobase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOIO);
    178 			membase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOMEM);
    179 			irq = pci_conf_read(pc, pa->pa_tag, PCI_INTERRUPT_REG);
    180 
    181 			/* Reset the power state. */
    182 			printf("%s: chip is in D%d power mode "
    183 			    "-- setting to D0\n", sc->sc_dev.dv_xname,
    184 			    command & PCI_PMCSR_STATE_MASK);
    185 			command &= ~PCI_PMCSR_STATE_MASK;
    186 			pci_conf_write(pc, pa->pa_tag,
    187 			    pmreg + PCI_PMCSR, command);
    188 
    189 			/* Restore PCI config data. */
    190 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOIO, iobase);
    191 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOMEM, membase);
    192 			pci_conf_write(pc, pa->pa_tag, PCI_INTERRUPT_REG, irq);
    193 		}
    194 	}
    195 
    196 	/*
    197 	 * Map control/status registers.
    198 	 *
    199 	 * The original FreeBSD's driver has the following comment:
    200 	 *
    201 	 *   Default to using PIO access for this driver. On SMP systems,
    202 	 *   there appear to be problems with memory mapped mode:
    203 	 *   it looks like doing too many memory mapped access
    204 	 *   back to back in rapid succession can hang the bus.
    205 	 *   I'm inclined to blame this on crummy design/construction
    206 	 *   on the part of Realtek. Memory mapped mode does appear to
    207 	 *   work on uniprocessor systems though.
    208 	 *
    209 	 * On NetBSD, some port doesn't support PCI I/O space properly,
    210 	 * so we try to map both and prefer I/O space to mem space.
    211 	 */
    212 	ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    213 	    &iot, &ioh, NULL, NULL) == 0);
    214 	memh_valid = (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    215 	    &memt, &memh, NULL, NULL) == 0);
    216 	if (ioh_valid) {
    217 		sc->rtk_btag = iot;
    218 		sc->rtk_bhandle = ioh;
    219 	} else if (memh_valid) {
    220 		sc->rtk_btag = memt;
    221 		sc->rtk_bhandle = memh;
    222 	} else {
    223 		aprint_error("%s: can't map registers\n", sc->sc_dev.dv_xname);
    224 		return;
    225 	}
    226 
    227 	/* Allocate interrupt */
    228 	if (pci_intr_map(pa, &ih)) {
    229 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    230 		return;
    231 	}
    232 	intrstr = pci_intr_string(pc, ih);
    233 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rtk_intr, sc);
    234 	if (psc->sc_ih == NULL) {
    235 		printf("%s: couldn't establish interrupt",
    236 		    sc->sc_dev.dv_xname);
    237 		if (intrstr != NULL)
    238 			printf(" at %s", intrstr);
    239 		printf("\n");
    240 		return;
    241 	}
    242 
    243 	if (t->rtk_basetype == RTK_8129)
    244 		sc->sc_quirk |= RTKQ_8129;
    245 
    246 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    247 
    248 	sc->sc_dmat = pa->pa_dmat;
    249 	sc->sc_flags |= RTK_ENABLED;
    250 
    251 	pnp_status = pci_net_generic_power_register(self,
    252 	    pa->pa_pc, pa->pa_tag, &sc->ethercom.ec_if, NULL, NULL);
    253 	if (pnp_status != PNP_STATUS_SUCCESS) {
    254 		aprint_error_dev(self, "couldn't establish power handler\n");
    255 	}
    256 
    257 	rtk_attach(sc);
    258 }
    259