Home | History | Annotate | Line # | Download | only in pci
if_rtk_pci.c revision 1.40.2.1
      1  1.40.2.1  uebayasi /*	$NetBSD: if_rtk_pci.c,v 1.40.2.1 2010/08/17 06:46:26 uebayasi 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.40.2.1  uebayasi __KERNEL_RCSID(0, "$NetBSD: if_rtk_pci.c,v 1.40.2.1 2010/08/17 06:46:26 uebayasi 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.40.2.1  uebayasi 	pci_chipset_tag_t sc_pc;
     86       1.1      haya };
     87       1.1      haya 
     88       1.2   thorpej static const struct rtk_type rtk_pci_devs[] = {
     89       1.1      haya 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129,
     90      1.20       wiz 		RTK_8129, "Realtek 8129 10/100BaseTX" },
     91       1.1      haya 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
     92      1.20       wiz 		RTK_8139, "Realtek 8139 10/100BaseTX" },
     93      1.37   tsutsui 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8138,
     94      1.37   tsutsui 		RTK_8139, "Realtek 8138 10/100BaseTX" },
     95      1.37   tsutsui 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139D,
     96      1.37   tsutsui 		RTK_8139, "Realtek 8139D 10/100BaseTX" },
     97      1.37   tsutsui 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8100,
     98      1.37   tsutsui 		RTK_8139, "Realtek 8100 10/100BaseTX" },
     99       1.1      haya 	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_MPX5030,
    100      1.19      fvdl 		RTK_8139, "Accton MPX 5030/5038 10/100BaseTX" },
    101       1.1      haya 	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139,
    102      1.19      fvdl 		RTK_8139, "Delta Electronics 8139 10/100BaseTX" },
    103       1.1      haya 	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139,
    104      1.19      fvdl 		RTK_8139, "Addtron Technology 8139 10/100BaseTX" },
    105       1.5   thorpej 	{ PCI_VENDOR_SEGA, PCI_PRODUCT_SEGA_BROADBAND,
    106      1.19      fvdl 		RTK_8139, "SEGA Broadband Adapter" },
    107       1.9   thorpej 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE530TXPLUS,
    108      1.19      fvdl 		RTK_8139, "D-Link Systems DFE 530TX+" },
    109      1.24        he 	{ PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_BAYSTACK_21,
    110      1.24        he 		RTK_8139, "Baystack 21 (MPX EN5038) 10/100BaseTX" },
    111       1.1      haya };
    112       1.1      haya 
    113      1.38    cegger static int	rtk_pci_match(device_t, cfdata_t, void *);
    114      1.34       uwe static void	rtk_pci_attach(device_t, device_t, void *);
    115  1.40.2.1  uebayasi static int	rtk_pci_detach(device_t, int);
    116       1.1      haya 
    117      1.36   tsutsui CFATTACH_DECL_NEW(rtk_pci, sizeof(struct rtk_pci_softc),
    118  1.40.2.1  uebayasi     rtk_pci_match, rtk_pci_attach, rtk_pci_detach, NULL);
    119       1.1      haya 
    120      1.21   thorpej static const struct rtk_type *
    121      1.21   thorpej rtk_pci_lookup(const struct pci_attach_args *pa)
    122       1.1      haya {
    123  1.40.2.1  uebayasi 	int i;
    124       1.1      haya 
    125  1.40.2.1  uebayasi 	for (i = 0; i < __arraycount(rtk_pci_devs); i++) {
    126  1.40.2.1  uebayasi 		if (PCI_VENDOR(pa->pa_id) != rtk_pci_devs[i].rtk_vid)
    127  1.40.2.1  uebayasi 			continue;
    128  1.40.2.1  uebayasi 		if (PCI_PRODUCT(pa->pa_id) == rtk_pci_devs[i].rtk_did)
    129  1.40.2.1  uebayasi 			return &rtk_pci_devs[i];
    130       1.1      haya 	}
    131  1.40.2.1  uebayasi 
    132      1.36   tsutsui 	return NULL;
    133       1.1      haya }
    134       1.1      haya 
    135      1.21   thorpej static int
    136      1.36   tsutsui rtk_pci_match(device_t parent, cfdata_t cf, void *aux)
    137       1.1      haya {
    138       1.1      haya 	struct pci_attach_args *pa = aux;
    139       1.1      haya 
    140       1.2   thorpej 	if (rtk_pci_lookup(pa) != NULL)
    141      1.36   tsutsui 		return 1;
    142       1.2   thorpej 
    143      1.36   tsutsui 	return 0;
    144       1.1      haya }
    145       1.1      haya 
    146       1.1      haya /*
    147       1.1      haya  * Attach the interface. Allocate softc structures, do ifmedia
    148       1.1      haya  * setup and ethernet/BPF attach.
    149       1.1      haya  */
    150      1.21   thorpej static void
    151      1.34       uwe rtk_pci_attach(device_t parent, device_t self, void *aux)
    152       1.1      haya {
    153      1.34       uwe 	struct rtk_pci_softc *psc = device_private(self);
    154       1.2   thorpej 	struct rtk_softc *sc = &psc->sc_rtk;
    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.40.2.1  uebayasi 	bus_size_t iosize, memsize;
    161       1.1      haya 	const char *intrstr = NULL;
    162       1.2   thorpej 	const struct rtk_type *t;
    163  1.40.2.1  uebayasi 	bool ioh_valid, memh_valid;
    164       1.1      haya 
    165      1.36   tsutsui 	sc->sc_dev = self;
    166  1.40.2.1  uebayasi 	psc->sc_pc = pa->pa_pc;
    167      1.36   tsutsui 
    168       1.2   thorpej 	t = rtk_pci_lookup(pa);
    169  1.40.2.1  uebayasi 	KASSERT(t != NULL);
    170      1.34       uwe 
    171      1.34       uwe 	aprint_naive("\n");
    172      1.34       uwe 	aprint_normal(": %s (rev. 0x%02x)\n",
    173      1.36   tsutsui 	    t->rtk_name, PCI_REVISION(pa->pa_class));
    174       1.1      haya 
    175       1.1      haya 	/*
    176       1.1      haya 	 * Map control/status registers.
    177      1.31   tsutsui 	 *
    178      1.31   tsutsui 	 * The original FreeBSD's driver has the following comment:
    179      1.31   tsutsui 	 *
    180      1.31   tsutsui 	 *   Default to using PIO access for this driver. On SMP systems,
    181      1.31   tsutsui 	 *   there appear to be problems with memory mapped mode:
    182      1.31   tsutsui 	 *   it looks like doing too many memory mapped access
    183      1.31   tsutsui 	 *   back to back in rapid succession can hang the bus.
    184      1.31   tsutsui 	 *   I'm inclined to blame this on crummy design/construction
    185      1.31   tsutsui 	 *   on the part of Realtek. Memory mapped mode does appear to
    186      1.31   tsutsui 	 *   work on uniprocessor systems though.
    187      1.31   tsutsui 	 *
    188  1.40.2.1  uebayasi 	 * On NetBSD, some ports don't support PCI I/O space properly,
    189      1.31   tsutsui 	 * so we try to map both and prefer I/O space to mem space.
    190       1.1      haya 	 */
    191      1.31   tsutsui 	ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    192  1.40.2.1  uebayasi 	    &iot, &ioh, NULL, &iosize) == 0);
    193      1.31   tsutsui 	memh_valid = (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    194  1.40.2.1  uebayasi 	    &memt, &memh, NULL, &memsize) == 0);
    195      1.31   tsutsui 	if (ioh_valid) {
    196      1.31   tsutsui 		sc->rtk_btag = iot;
    197      1.31   tsutsui 		sc->rtk_bhandle = ioh;
    198  1.40.2.1  uebayasi 		sc->rtk_bsize = iosize;
    199  1.40.2.1  uebayasi 		if (memh_valid)
    200  1.40.2.1  uebayasi 			bus_space_unmap(memt, memh, memsize);
    201      1.31   tsutsui 	} else if (memh_valid) {
    202      1.31   tsutsui 		sc->rtk_btag = memt;
    203      1.31   tsutsui 		sc->rtk_bhandle = memh;
    204  1.40.2.1  uebayasi 		sc->rtk_bsize = memsize;
    205      1.31   tsutsui 	} else {
    206      1.34       uwe 		aprint_error_dev(self, "can't map registers\n");
    207       1.2   thorpej 		return;
    208       1.1      haya 	}
    209       1.1      haya 
    210       1.1      haya 	/* Allocate interrupt */
    211       1.4  sommerfe 	if (pci_intr_map(pa, &ih)) {
    212      1.34       uwe 		aprint_error_dev(self, "couldn't map interrupt\n");
    213       1.2   thorpej 		return;
    214       1.1      haya 	}
    215       1.1      haya 	intrstr = pci_intr_string(pc, ih);
    216       1.2   thorpej 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rtk_intr, sc);
    217       1.1      haya 	if (psc->sc_ih == NULL) {
    218      1.34       uwe 		aprint_error_dev(self, "couldn't establish interrupt");
    219       1.1      haya 		if (intrstr != NULL)
    220      1.40     njoly 			aprint_error(" at %s", intrstr);
    221      1.40     njoly 		aprint_error("\n");
    222       1.2   thorpej 		return;
    223       1.1      haya 	}
    224       1.1      haya 
    225      1.32   tsutsui 	if (t->rtk_basetype == RTK_8129)
    226      1.32   tsutsui 		sc->sc_quirk |= RTKQ_8129;
    227       1.2   thorpej 
    228      1.34       uwe 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    229       1.1      haya 
    230       1.1      haya 	sc->sc_dmat = pa->pa_dmat;
    231      1.18   kanaoka 	sc->sc_flags |= RTK_ENABLED;
    232      1.12   tsutsui 
    233      1.39   tsutsui 	if (pmf_device_register(self, NULL, NULL))
    234      1.39   tsutsui 		pmf_class_network_register(self, &sc->ethercom.ec_if);
    235      1.39   tsutsui 	else
    236      1.35  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    237      1.22  jmcneill 
    238       1.2   thorpej 	rtk_attach(sc);
    239       1.1      haya }
    240  1.40.2.1  uebayasi 
    241  1.40.2.1  uebayasi static int
    242  1.40.2.1  uebayasi rtk_pci_detach(device_t self, int flags)
    243  1.40.2.1  uebayasi {
    244  1.40.2.1  uebayasi 	struct rtk_pci_softc *psc = device_private(self);
    245  1.40.2.1  uebayasi 	struct rtk_softc *sc = &psc->sc_rtk;
    246  1.40.2.1  uebayasi 	int rv;
    247  1.40.2.1  uebayasi 
    248  1.40.2.1  uebayasi 	/* rtk_stop() */
    249  1.40.2.1  uebayasi 	sc->ethercom.ec_if.if_stop(&sc->ethercom.ec_if, 0);
    250  1.40.2.1  uebayasi 
    251  1.40.2.1  uebayasi 	rv = rtk_detach(sc);
    252  1.40.2.1  uebayasi 	if (rv)
    253  1.40.2.1  uebayasi 		return rv;
    254  1.40.2.1  uebayasi 
    255  1.40.2.1  uebayasi 	if (psc->sc_ih != NULL) {
    256  1.40.2.1  uebayasi 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
    257  1.40.2.1  uebayasi 		psc->sc_ih = NULL;
    258  1.40.2.1  uebayasi 	}
    259  1.40.2.1  uebayasi 
    260  1.40.2.1  uebayasi 	if (sc->rtk_bsize != 0) {
    261  1.40.2.1  uebayasi 		bus_space_unmap(sc->rtk_btag, sc->rtk_bhandle, sc->rtk_bsize);
    262  1.40.2.1  uebayasi 		sc->rtk_bsize = 0;
    263  1.40.2.1  uebayasi 	}
    264  1.40.2.1  uebayasi 
    265  1.40.2.1  uebayasi 	return 0;
    266  1.40.2.1  uebayasi }
    267