Home | History | Annotate | Line # | Download | only in pci
if_rtk_pci.c revision 1.25.2.1
      1 /*	$NetBSD: if_rtk_pci.c,v 1.25.2.1 2006/11/18 21:34:30 ad 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.25.2.1 2006/11/18 21:34:30 ad 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 #if !defined(__sh__)		/* XXX: dreamcast, landisk */
     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 	void *sc_powerhook;		/* powerhook ctxt. */
    101 	struct pci_conf_state sc_pciconf;
    102 };
    103 
    104 static const struct rtk_type rtk_pci_devs[] = {
    105 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129,
    106 		RTK_8129, "Realtek 8129 10/100BaseTX" },
    107 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
    108 		RTK_8139, "Realtek 8139 10/100BaseTX" },
    109 	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_MPX5030,
    110 		RTK_8139, "Accton MPX 5030/5038 10/100BaseTX" },
    111 	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139,
    112 		RTK_8139, "Delta Electronics 8139 10/100BaseTX" },
    113 	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139,
    114 		RTK_8139, "Addtron Technology 8139 10/100BaseTX" },
    115 	{ PCI_VENDOR_SEGA, PCI_PRODUCT_SEGA_BROADBAND,
    116 		RTK_8139, "SEGA Broadband Adapter" },
    117 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE530TXPLUS,
    118 		RTK_8139, "D-Link Systems DFE 530TX+" },
    119 	{ PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_BAYSTACK_21,
    120 		RTK_8139, "Baystack 21 (MPX EN5038) 10/100BaseTX" },
    121 	{ 0, 0, 0, NULL }
    122 };
    123 
    124 static int	rtk_pci_match(struct device *, struct cfdata *, void *);
    125 static void	rtk_pci_attach(struct device *, struct device *, void *);
    126 static void	rtk_pci_powerhook(int, void *);
    127 
    128 CFATTACH_DECL(rtk_pci, sizeof(struct rtk_pci_softc),
    129     rtk_pci_match, rtk_pci_attach, NULL, NULL);
    130 
    131 static const struct rtk_type *
    132 rtk_pci_lookup(const struct pci_attach_args *pa)
    133 {
    134 	const struct rtk_type *t;
    135 
    136 	for (t = rtk_pci_devs; t->rtk_name != NULL; t++) {
    137 		if (PCI_VENDOR(pa->pa_id) == t->rtk_vid &&
    138 		    PCI_PRODUCT(pa->pa_id) == t->rtk_did) {
    139 			return (t);
    140 		}
    141 	}
    142 	return (NULL);
    143 }
    144 
    145 static int
    146 rtk_pci_match(struct device *parent, struct cfdata *match,
    147     void *aux)
    148 {
    149 	struct pci_attach_args *pa = aux;
    150 
    151 	if (rtk_pci_lookup(pa) != NULL)
    152 		return (1);
    153 
    154 	return (0);
    155 }
    156 
    157 /*
    158  * Attach the interface. Allocate softc structures, do ifmedia
    159  * setup and ethernet/BPF attach.
    160  */
    161 static void
    162 rtk_pci_attach(struct device *parent, struct device *self, void *aux)
    163 {
    164 	struct rtk_pci_softc *psc = (struct rtk_pci_softc *)self;
    165 	struct rtk_softc *sc = &psc->sc_rtk;
    166 	pcireg_t command;
    167 	struct pci_attach_args *pa = aux;
    168 	pci_chipset_tag_t pc = pa->pa_pc;
    169 	pci_intr_handle_t ih;
    170 	const char *intrstr = NULL;
    171 	const struct rtk_type *t;
    172 	int pmreg;
    173 
    174 	psc->sc_pc = pa->pa_pc;
    175 	psc->sc_pcitag = pa->pa_tag;
    176 
    177 	t = rtk_pci_lookup(pa);
    178 	if (t == NULL) {
    179 		printf("\n");
    180 		panic("rtk_pci_attach: impossible");
    181 	}
    182 	printf(": %s (rev. 0x%02x)\n", t->rtk_name, PCI_REVISION(pa->pa_class));
    183 
    184 	/*
    185 	 * Handle power management nonsense.
    186 	 */
    187 
    188 	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
    189 		command = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR);
    190 		if (command & PCI_PMCSR_STATE_MASK) {
    191 			pcireg_t iobase, membase, irq;
    192 
    193 			/* Save important PCI config data. */
    194 			iobase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOIO);
    195 			membase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOMEM);
    196 			irq = pci_conf_read(pc, pa->pa_tag, PCI_INTERRUPT_REG);
    197 
    198 			/* Reset the power state. */
    199 			printf("%s: chip is in D%d power mode "
    200 			    "-- setting to D0\n", sc->sc_dev.dv_xname,
    201 			    command & PCI_PMCSR_STATE_MASK);
    202 			command &= ~PCI_PMCSR_STATE_MASK;
    203 			pci_conf_write(pc, pa->pa_tag,
    204 			    pmreg + PCI_PMCSR, command);
    205 
    206 			/* Restore PCI config data. */
    207 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOIO, iobase);
    208 			pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOMEM, membase);
    209 			pci_conf_write(pc, pa->pa_tag, PCI_INTERRUPT_REG, irq);
    210 		}
    211 	}
    212 
    213 	/*
    214 	 * Map control/status registers.
    215 	 */
    216 #ifdef RTK_USEIOSPACE
    217 	if (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    218 	    &sc->rtk_btag, &sc->rtk_bhandle, NULL, NULL)) {
    219 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    220 		return;
    221 	}
    222 #else
    223 	if (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    224 	    &sc->rtk_btag, &sc->rtk_bhandle, NULL, NULL)) {
    225 		printf("%s: can't map mem space\n", sc->sc_dev.dv_xname);
    226 		return;
    227 	}
    228 #endif
    229 
    230 	/* Allocate interrupt */
    231 	if (pci_intr_map(pa, &ih)) {
    232 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    233 		return;
    234 	}
    235 	intrstr = pci_intr_string(pc, ih);
    236 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rtk_intr, sc);
    237 	if (psc->sc_ih == NULL) {
    238 		printf("%s: couldn't establish interrupt",
    239 		    sc->sc_dev.dv_xname);
    240 		if (intrstr != NULL)
    241 			printf(" at %s", intrstr);
    242 		printf("\n");
    243 		return;
    244 	}
    245 
    246 	sc->rtk_type = t->rtk_basetype;
    247 
    248 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    249 
    250 	sc->sc_dmat = pa->pa_dmat;
    251 	sc->sc_flags |= RTK_ENABLED;
    252 
    253 	psc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
    254 	    rtk_pci_powerhook, psc);
    255 	if (psc->sc_powerhook == NULL)
    256 		printf("%s: WARNING: unable to establish pci power hook\n",
    257 			sc->sc_dev.dv_xname);
    258 
    259 	rtk_attach(sc);
    260 }
    261 
    262 static void
    263 rtk_pci_powerhook(int why, void *arg)
    264 {
    265 	struct rtk_pci_softc *sc = (struct rtk_pci_softc *)arg;
    266 	int s;
    267 
    268 	s = splnet();
    269 	switch (why) {
    270 	case PWR_SUSPEND:
    271 	case PWR_STANDBY:
    272 		pci_conf_capture(sc->sc_pc, sc->sc_pcitag, &sc->sc_pciconf);
    273 		break;
    274 	case PWR_RESUME:
    275 		pci_conf_restore(sc->sc_pc, sc->sc_pcitag, &sc->sc_pciconf);
    276 		break;
    277 	case PWR_SOFTSUSPEND:
    278 	case PWR_SOFTSTANDBY:
    279 	case PWR_SOFTRESUME:
    280 		break;
    281 	}
    282 	splx(s);
    283 }
    284