Home | History | Annotate | Line # | Download | only in pci
if_rtk_pci.c revision 1.33.2.1
      1  1.33.2.1       mjf /*	$NetBSD: if_rtk_pci.c,v 1.33.2.1 2007/11/19 00:48:11 mjf Exp $	*/
      2       1.1      haya 
      3       1.1      haya /*
      4       1.1      haya  * Copyright (c) 1997, 1998
      5       1.1      haya  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
      6       1.1      haya  *
      7       1.1      haya  * Redistribution and use in source and binary forms, with or without
      8       1.1      haya  * modification, are permitted provided that the following conditions
      9       1.1      haya  * are met:
     10       1.1      haya  * 1. Redistributions of source code must retain the above copyright
     11       1.1      haya  *    notice, this list of conditions and the following disclaimer.
     12       1.1      haya  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1      haya  *    notice, this list of conditions and the following disclaimer in the
     14       1.1      haya  *    documentation and/or other materials provided with the distribution.
     15       1.1      haya  * 3. All advertising materials mentioning features or use of this software
     16       1.1      haya  *    must display the following acknowledgement:
     17       1.1      haya  *	This product includes software developed by Bill Paul.
     18       1.1      haya  * 4. Neither the name of the author nor the names of any co-contributors
     19       1.1      haya  *    may be used to endorse or promote products derived from this software
     20       1.1      haya  *    without specific prior written permission.
     21       1.1      haya  *
     22       1.1      haya  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23       1.1      haya  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1      haya  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1      haya  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26       1.1      haya  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27       1.1      haya  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28       1.1      haya  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29       1.1      haya  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30       1.1      haya  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31       1.1      haya  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32       1.1      haya  * THE POSSIBILITY OF SUCH DAMAGE.
     33       1.1      haya  *
     34       1.1      haya  *	FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
     35       1.1      haya  */
     36       1.1      haya 
     37       1.1      haya /*
     38      1.20       wiz  * Realtek 8129/8139 PCI NIC driver
     39       1.1      haya  *
     40       1.1      haya  * Supports several extremely cheap PCI 10/100 adapters based on
     41      1.20       wiz  * the Realtek chipset. Datasheets can be obtained from
     42       1.1      haya  * www.realtek.com.tw.
     43       1.1      haya  *
     44       1.1      haya  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     45       1.1      haya  * Electrical Engineering Department
     46       1.1      haya  * Columbia University, New York City
     47       1.1      haya  */
     48       1.1      haya 
     49      1.10     lukem #include <sys/cdefs.h>
     50  1.33.2.1       mjf __KERNEL_RCSID(0, "$NetBSD: if_rtk_pci.c,v 1.33.2.1 2007/11/19 00:48:11 mjf Exp $");
     51       1.1      haya 
     52       1.1      haya #include <sys/param.h>
     53       1.1      haya #include <sys/systm.h>
     54       1.1      haya #include <sys/callout.h>
     55       1.1      haya #include <sys/device.h>
     56       1.1      haya #include <sys/sockio.h>
     57       1.1      haya #include <sys/mbuf.h>
     58       1.1      haya #include <sys/malloc.h>
     59       1.1      haya #include <sys/kernel.h>
     60       1.1      haya #include <sys/socket.h>
     61       1.1      haya 
     62       1.1      haya #include <net/if.h>
     63       1.1      haya #include <net/if_arp.h>
     64       1.1      haya #include <net/if_ether.h>
     65       1.1      haya #include <net/if_dl.h>
     66       1.1      haya #include <net/if_media.h>
     67       1.1      haya 
     68      1.33        ad #include <sys/bus.h>
     69       1.1      haya 
     70       1.1      haya #include <dev/pci/pcireg.h>
     71       1.1      haya #include <dev/pci/pcivar.h>
     72       1.1      haya #include <dev/pci/pcidevs.h>
     73       1.1      haya 
     74       1.1      haya #include <dev/mii/mii.h>
     75       1.1      haya #include <dev/mii/miivar.h>
     76       1.1      haya 
     77       1.1      haya #include <dev/ic/rtl81x9reg.h>
     78       1.1      haya #include <dev/ic/rtl81x9var.h>
     79       1.1      haya 
     80       1.2   thorpej struct rtk_pci_softc {
     81       1.2   thorpej 	struct rtk_softc sc_rtk;	/* real rtk softc */
     82      1.12   tsutsui 
     83       1.1      haya 	/* PCI-specific goo.*/
     84       1.1      haya 	void *sc_ih;
     85       1.1      haya 	pci_chipset_tag_t sc_pc; 	/* PCI chipset */
     86       1.1      haya 	pcitag_t sc_pcitag;		/* PCI tag */
     87      1.22  jmcneill 
     88      1.22  jmcneill 	void *sc_powerhook;		/* powerhook ctxt. */
     89      1.22  jmcneill 	struct pci_conf_state sc_pciconf;
     90       1.1      haya };
     91       1.1      haya 
     92       1.2   thorpej static const struct rtk_type rtk_pci_devs[] = {
     93       1.1      haya 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129,
     94      1.20       wiz 		RTK_8129, "Realtek 8129 10/100BaseTX" },
     95       1.1      haya 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
     96      1.20       wiz 		RTK_8139, "Realtek 8139 10/100BaseTX" },
     97       1.1      haya 	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_MPX5030,
     98      1.19      fvdl 		RTK_8139, "Accton MPX 5030/5038 10/100BaseTX" },
     99       1.1      haya 	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139,
    100      1.19      fvdl 		RTK_8139, "Delta Electronics 8139 10/100BaseTX" },
    101       1.1      haya 	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139,
    102      1.19      fvdl 		RTK_8139, "Addtron Technology 8139 10/100BaseTX" },
    103       1.5   thorpej 	{ PCI_VENDOR_SEGA, PCI_PRODUCT_SEGA_BROADBAND,
    104      1.19      fvdl 		RTK_8139, "SEGA Broadband Adapter" },
    105       1.9   thorpej 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE530TXPLUS,
    106      1.19      fvdl 		RTK_8139, "D-Link Systems DFE 530TX+" },
    107      1.24        he 	{ PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_BAYSTACK_21,
    108      1.24        he 		RTK_8139, "Baystack 21 (MPX EN5038) 10/100BaseTX" },
    109      1.19      fvdl 	{ 0, 0, 0, NULL }
    110       1.1      haya };
    111       1.1      haya 
    112  1.33.2.1       mjf static int	rtk_pci_match(device_t, struct cfdata *, void *);
    113  1.33.2.1       mjf static void	rtk_pci_attach(device_t, device_t, void *);
    114      1.22  jmcneill static void	rtk_pci_powerhook(int, void *);
    115       1.1      haya 
    116      1.14   thorpej CFATTACH_DECL(rtk_pci, sizeof(struct rtk_pci_softc),
    117      1.15   thorpej     rtk_pci_match, rtk_pci_attach, NULL, NULL);
    118       1.1      haya 
    119      1.21   thorpej static const struct rtk_type *
    120      1.21   thorpej rtk_pci_lookup(const struct pci_attach_args *pa)
    121       1.1      haya {
    122       1.2   thorpej 	const struct rtk_type *t;
    123       1.1      haya 
    124      1.12   tsutsui 	for (t = rtk_pci_devs; t->rtk_name != NULL; t++) {
    125       1.2   thorpej 		if (PCI_VENDOR(pa->pa_id) == t->rtk_vid &&
    126       1.2   thorpej 		    PCI_PRODUCT(pa->pa_id) == t->rtk_did) {
    127       1.1      haya 			return (t);
    128       1.1      haya 		}
    129       1.1      haya 	}
    130       1.1      haya 	return (NULL);
    131       1.1      haya }
    132       1.1      haya 
    133      1.21   thorpej static int
    134  1.33.2.1       mjf rtk_pci_match(device_t parent, struct cfdata *match,
    135      1.27  christos     void *aux)
    136       1.1      haya {
    137       1.1      haya 	struct pci_attach_args *pa = aux;
    138       1.1      haya 
    139       1.2   thorpej 	if (rtk_pci_lookup(pa) != NULL)
    140       1.1      haya 		return (1);
    141       1.2   thorpej 
    142       1.1      haya 	return (0);
    143       1.1      haya }
    144       1.1      haya 
    145       1.1      haya /*
    146       1.1      haya  * Attach the interface. Allocate softc structures, do ifmedia
    147       1.1      haya  * setup and ethernet/BPF attach.
    148       1.1      haya  */
    149      1.21   thorpej static void
    150  1.33.2.1       mjf rtk_pci_attach(device_t parent, device_t self, void *aux)
    151       1.1      haya {
    152  1.33.2.1       mjf 	struct rtk_pci_softc *psc = device_private(self);
    153       1.2   thorpej 	struct rtk_softc *sc = &psc->sc_rtk;
    154       1.2   thorpej 	pcireg_t command;
    155       1.1      haya 	struct pci_attach_args *pa = aux;
    156       1.1      haya 	pci_chipset_tag_t pc = pa->pa_pc;
    157       1.1      haya 	pci_intr_handle_t ih;
    158      1.31   tsutsui 	bus_space_tag_t iot, memt;
    159      1.31   tsutsui 	bus_space_handle_t ioh, memh;
    160       1.1      haya 	const char *intrstr = NULL;
    161       1.2   thorpej 	const struct rtk_type *t;
    162       1.2   thorpej 	int pmreg;
    163      1.31   tsutsui 	int ioh_valid, memh_valid;
    164       1.1      haya 
    165       1.1      haya 	psc->sc_pc = pa->pa_pc;
    166       1.1      haya 	psc->sc_pcitag = pa->pa_tag;
    167       1.1      haya 
    168       1.2   thorpej 	t = rtk_pci_lookup(pa);
    169       1.1      haya 	if (t == NULL) {
    170       1.1      haya 		printf("\n");
    171       1.2   thorpej 		panic("rtk_pci_attach: impossible");
    172       1.1      haya 	}
    173  1.33.2.1       mjf 
    174  1.33.2.1       mjf 	aprint_naive("\n");
    175  1.33.2.1       mjf 	aprint_normal(": %s (rev. 0x%02x)\n",
    176  1.33.2.1       mjf 		      t->rtk_name, PCI_REVISION(pa->pa_class));
    177       1.1      haya 
    178       1.1      haya 	/*
    179       1.1      haya 	 * Handle power management nonsense.
    180       1.1      haya 	 */
    181       1.1      haya 
    182       1.1      haya 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    183      1.28   tsutsui 		command = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR);
    184      1.28   tsutsui 		if (command & PCI_PMCSR_STATE_MASK) {
    185       1.2   thorpej 			pcireg_t iobase, membase, irq;
    186       1.1      haya 
    187       1.1      haya 			/* Save important PCI config data. */
    188       1.3   tsutsui 			iobase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOIO);
    189       1.3   tsutsui 			membase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOMEM);
    190      1.17   tsutsui 			irq = pci_conf_read(pc, pa->pa_tag, PCI_INTERRUPT_REG);
    191       1.1      haya 
    192       1.1      haya 			/* Reset the power state. */
    193  1.33.2.1       mjf 			aprint_normal_dev(self,
    194  1.33.2.1       mjf 			    "chip is in D%d power mode -- setting to D0\n",
    195      1.28   tsutsui 			    command & PCI_PMCSR_STATE_MASK);
    196      1.28   tsutsui 			command &= ~PCI_PMCSR_STATE_MASK;
    197      1.28   tsutsui 			pci_conf_write(pc, pa->pa_tag,
    198      1.28   tsutsui 			    pmreg + PCI_PMCSR, command);
    199       1.1      haya 
    200       1.1      haya 			/* Restore PCI config data. */
    201       1.3   tsutsui 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOIO, iobase);
    202       1.3   tsutsui 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOMEM, membase);
    203      1.17   tsutsui 			pci_conf_write(pc, pa->pa_tag, PCI_INTERRUPT_REG, irq);
    204       1.1      haya 		}
    205       1.1      haya 	}
    206       1.1      haya 
    207       1.1      haya 	/*
    208       1.1      haya 	 * Map control/status registers.
    209      1.31   tsutsui 	 *
    210      1.31   tsutsui 	 * The original FreeBSD's driver has the following comment:
    211      1.31   tsutsui 	 *
    212      1.31   tsutsui 	 *   Default to using PIO access for this driver. On SMP systems,
    213      1.31   tsutsui 	 *   there appear to be problems with memory mapped mode:
    214      1.31   tsutsui 	 *   it looks like doing too many memory mapped access
    215      1.31   tsutsui 	 *   back to back in rapid succession can hang the bus.
    216      1.31   tsutsui 	 *   I'm inclined to blame this on crummy design/construction
    217      1.31   tsutsui 	 *   on the part of Realtek. Memory mapped mode does appear to
    218      1.31   tsutsui 	 *   work on uniprocessor systems though.
    219      1.31   tsutsui 	 *
    220      1.31   tsutsui 	 * On NetBSD, some port doesn't support PCI I/O space properly,
    221      1.31   tsutsui 	 * so we try to map both and prefer I/O space to mem space.
    222       1.1      haya 	 */
    223      1.31   tsutsui 	ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    224      1.31   tsutsui 	    &iot, &ioh, NULL, NULL) == 0);
    225      1.31   tsutsui 	memh_valid = (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    226      1.31   tsutsui 	    &memt, &memh, NULL, NULL) == 0);
    227      1.31   tsutsui 	if (ioh_valid) {
    228      1.31   tsutsui 		sc->rtk_btag = iot;
    229      1.31   tsutsui 		sc->rtk_bhandle = ioh;
    230      1.31   tsutsui 	} else if (memh_valid) {
    231      1.31   tsutsui 		sc->rtk_btag = memt;
    232      1.31   tsutsui 		sc->rtk_bhandle = memh;
    233      1.31   tsutsui 	} else {
    234  1.33.2.1       mjf 		aprint_error_dev(self, "can't map registers\n");
    235       1.2   thorpej 		return;
    236       1.1      haya 	}
    237       1.1      haya 
    238       1.1      haya 	/* Allocate interrupt */
    239       1.4  sommerfe 	if (pci_intr_map(pa, &ih)) {
    240  1.33.2.1       mjf 		aprint_error_dev(self, "couldn't map interrupt\n");
    241       1.2   thorpej 		return;
    242       1.1      haya 	}
    243       1.1      haya 	intrstr = pci_intr_string(pc, ih);
    244       1.2   thorpej 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rtk_intr, sc);
    245       1.1      haya 	if (psc->sc_ih == NULL) {
    246  1.33.2.1       mjf 		aprint_error_dev(self, "couldn't establish interrupt");
    247       1.1      haya 		if (intrstr != NULL)
    248  1.33.2.1       mjf 			aprint_normal(" at %s", intrstr);
    249  1.33.2.1       mjf 		aprint_normal("\n");
    250       1.2   thorpej 		return;
    251       1.1      haya 	}
    252       1.1      haya 
    253      1.32   tsutsui 	if (t->rtk_basetype == RTK_8129)
    254      1.32   tsutsui 		sc->sc_quirk |= RTKQ_8129;
    255       1.2   thorpej 
    256  1.33.2.1       mjf 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    257       1.1      haya 
    258       1.1      haya 	sc->sc_dmat = pa->pa_dmat;
    259      1.18   kanaoka 	sc->sc_flags |= RTK_ENABLED;
    260      1.12   tsutsui 
    261  1.33.2.1       mjf 	psc->sc_powerhook = powerhook_establish(device_xname(self),
    262      1.26  jmcneill 	    rtk_pci_powerhook, psc);
    263      1.22  jmcneill 	if (psc->sc_powerhook == NULL)
    264  1.33.2.1       mjf 		aprint_error_dev(self,
    265  1.33.2.1       mjf 			"WARNING: unable to establish pci power hook\n");
    266      1.22  jmcneill 
    267       1.2   thorpej 	rtk_attach(sc);
    268       1.1      haya }
    269      1.22  jmcneill 
    270      1.22  jmcneill static void
    271      1.22  jmcneill rtk_pci_powerhook(int why, void *arg)
    272      1.22  jmcneill {
    273      1.22  jmcneill 	struct rtk_pci_softc *sc = (struct rtk_pci_softc *)arg;
    274      1.22  jmcneill 	int s;
    275      1.22  jmcneill 
    276      1.22  jmcneill 	s = splnet();
    277      1.22  jmcneill 	switch (why) {
    278      1.22  jmcneill 	case PWR_SUSPEND:
    279      1.22  jmcneill 	case PWR_STANDBY:
    280      1.22  jmcneill 		pci_conf_capture(sc->sc_pc, sc->sc_pcitag, &sc->sc_pciconf);
    281      1.22  jmcneill 		break;
    282      1.22  jmcneill 	case PWR_RESUME:
    283      1.22  jmcneill 		pci_conf_restore(sc->sc_pc, sc->sc_pcitag, &sc->sc_pciconf);
    284      1.22  jmcneill 		break;
    285      1.22  jmcneill 	case PWR_SOFTSUSPEND:
    286      1.22  jmcneill 	case PWR_SOFTSTANDBY:
    287      1.22  jmcneill 	case PWR_SOFTRESUME:
    288      1.22  jmcneill 		break;
    289      1.22  jmcneill 	}
    290      1.22  jmcneill 	splx(s);
    291      1.22  jmcneill }
    292