Home | History | Annotate | Line # | Download | only in pci
if_gem_pci.c revision 1.8.2.3
      1  1.8.2.3  jdolecek /*	$NetBSD: if_gem_pci.c,v 1.8.2.3 2002/06/23 17:47:40 jdolecek Exp $ */
      2  1.8.2.2   thorpej 
      3  1.8.2.2   thorpej /*
      4  1.8.2.2   thorpej  *
      5  1.8.2.2   thorpej  * Copyright (C) 2001 Eduardo Horvath.
      6  1.8.2.2   thorpej  * All rights reserved.
      7  1.8.2.2   thorpej  *
      8  1.8.2.2   thorpej  *
      9  1.8.2.2   thorpej  * Redistribution and use in source and binary forms, with or without
     10  1.8.2.2   thorpej  * modification, are permitted provided that the following conditions
     11  1.8.2.2   thorpej  * are met:
     12  1.8.2.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     13  1.8.2.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     14  1.8.2.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.8.2.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16  1.8.2.2   thorpej  *    documentation and/or other materials provided with the distribution.
     17  1.8.2.2   thorpej  *
     18  1.8.2.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
     19  1.8.2.2   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.8.2.2   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.8.2.2   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
     22  1.8.2.2   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.8.2.2   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.8.2.2   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.8.2.2   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.8.2.2   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.8.2.2   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.8.2.2   thorpej  * SUCH DAMAGE.
     29  1.8.2.2   thorpej  *
     30  1.8.2.2   thorpej  */
     31  1.8.2.2   thorpej 
     32  1.8.2.2   thorpej /*
     33  1.8.2.2   thorpej  * PCI bindings for Sun GEM ethernet controllers.
     34  1.8.2.2   thorpej  */
     35  1.8.2.2   thorpej 
     36  1.8.2.2   thorpej #include <sys/cdefs.h>
     37  1.8.2.3  jdolecek __KERNEL_RCSID(0, "$NetBSD: if_gem_pci.c,v 1.8.2.3 2002/06/23 17:47:40 jdolecek Exp $");
     38  1.8.2.2   thorpej 
     39  1.8.2.2   thorpej #include <sys/param.h>
     40  1.8.2.2   thorpej #include <sys/systm.h>
     41  1.8.2.2   thorpej #include <sys/malloc.h>
     42  1.8.2.2   thorpej #include <sys/kernel.h>
     43  1.8.2.2   thorpej #include <sys/socket.h>
     44  1.8.2.2   thorpej #include <sys/errno.h>
     45  1.8.2.2   thorpej #include <sys/device.h>
     46  1.8.2.2   thorpej 
     47  1.8.2.2   thorpej #include <machine/endian.h>
     48  1.8.2.2   thorpej 
     49  1.8.2.2   thorpej #include <uvm/uvm_extern.h>
     50  1.8.2.2   thorpej 
     51  1.8.2.2   thorpej #include <net/if.h>
     52  1.8.2.2   thorpej #include <net/if_dl.h>
     53  1.8.2.2   thorpej #include <net/if_media.h>
     54  1.8.2.2   thorpej #include <net/if_ether.h>
     55  1.8.2.2   thorpej 
     56  1.8.2.2   thorpej #if NBPFILTER > 0
     57  1.8.2.2   thorpej #include <net/bpf.h>
     58  1.8.2.2   thorpej #endif
     59  1.8.2.2   thorpej 
     60  1.8.2.2   thorpej #include <machine/bus.h>
     61  1.8.2.2   thorpej #include <machine/intr.h>
     62  1.8.2.2   thorpej 
     63  1.8.2.2   thorpej #include <dev/mii/mii.h>
     64  1.8.2.2   thorpej #include <dev/mii/miivar.h>
     65  1.8.2.2   thorpej #include <dev/mii/mii_bitbang.h>
     66  1.8.2.2   thorpej 
     67  1.8.2.2   thorpej #include <dev/ic/gemreg.h>
     68  1.8.2.2   thorpej #include <dev/ic/gemvar.h>
     69  1.8.2.2   thorpej 
     70  1.8.2.2   thorpej #include <dev/pci/pcivar.h>
     71  1.8.2.2   thorpej #include <dev/pci/pcireg.h>
     72  1.8.2.2   thorpej #include <dev/pci/pcidevs.h>
     73  1.8.2.2   thorpej 
     74  1.8.2.2   thorpej /* XXX Should use Properties when that's fleshed out. */
     75  1.8.2.2   thorpej #ifdef macppc
     76  1.8.2.2   thorpej #include <dev/ofw/openfirm.h>
     77  1.8.2.2   thorpej #endif /* macppc */
     78  1.8.2.2   thorpej 
     79  1.8.2.2   thorpej struct gem_pci_softc {
     80  1.8.2.2   thorpej 	struct	gem_softc	gsc_gem;	/* GEM device */
     81  1.8.2.2   thorpej 	void			*gsc_ih;
     82  1.8.2.2   thorpej };
     83  1.8.2.2   thorpej 
     84  1.8.2.2   thorpej int	gem_match_pci __P((struct device *, struct cfdata *, void *));
     85  1.8.2.2   thorpej void	gem_attach_pci __P((struct device *, struct device *, void *));
     86  1.8.2.2   thorpej 
     87  1.8.2.2   thorpej struct cfattach gem_pci_ca = {
     88  1.8.2.2   thorpej 	sizeof(struct gem_pci_softc), gem_match_pci, gem_attach_pci
     89  1.8.2.2   thorpej };
     90  1.8.2.2   thorpej 
     91  1.8.2.2   thorpej /*
     92  1.8.2.2   thorpej  * Attach routines need to be split out to different bus-specific files.
     93  1.8.2.2   thorpej  */
     94  1.8.2.2   thorpej 
     95  1.8.2.2   thorpej int
     96  1.8.2.2   thorpej gem_match_pci(parent, cf, aux)
     97  1.8.2.2   thorpej 	struct device *parent;
     98  1.8.2.2   thorpej 	struct cfdata *cf;
     99  1.8.2.2   thorpej 	void *aux;
    100  1.8.2.2   thorpej {
    101  1.8.2.2   thorpej 	struct pci_attach_args *pa = aux;
    102  1.8.2.2   thorpej 
    103  1.8.2.2   thorpej 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
    104  1.8.2.3  jdolecek 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK ||
    105  1.8.2.3  jdolecek 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK))
    106  1.8.2.2   thorpej 		return (1);
    107  1.8.2.2   thorpej 
    108  1.8.2.2   thorpej 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    109  1.8.2.3  jdolecek 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC ||
    110  1.8.2.3  jdolecek 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2))
    111  1.8.2.2   thorpej 		return (1);
    112  1.8.2.2   thorpej 
    113  1.8.2.2   thorpej 
    114  1.8.2.2   thorpej 	return (0);
    115  1.8.2.2   thorpej }
    116  1.8.2.2   thorpej 
    117  1.8.2.2   thorpej void
    118  1.8.2.2   thorpej gem_attach_pci(parent, self, aux)
    119  1.8.2.2   thorpej 	struct device *parent, *self;
    120  1.8.2.2   thorpej 	void *aux;
    121  1.8.2.2   thorpej {
    122  1.8.2.2   thorpej 	struct pci_attach_args *pa = aux;
    123  1.8.2.2   thorpej 	struct gem_pci_softc *gsc = (void *)self;
    124  1.8.2.2   thorpej 	struct gem_softc *sc = &gsc->gsc_gem;
    125  1.8.2.2   thorpej 	pci_intr_handle_t ih;
    126  1.8.2.2   thorpej 	const char *intrstr;
    127  1.8.2.2   thorpej 	char devinfo[256];
    128  1.8.2.2   thorpej 	uint8_t enaddr[ETHER_ADDR_LEN];
    129  1.8.2.2   thorpej 
    130  1.8.2.2   thorpej 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    131  1.8.2.2   thorpej 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    132  1.8.2.2   thorpej 
    133  1.8.2.2   thorpej 	sc->sc_dmatag = pa->pa_dmat;
    134  1.8.2.2   thorpej 
    135  1.8.2.2   thorpej 	sc->sc_pci = 1;		/* XXX */
    136  1.8.2.2   thorpej 
    137  1.8.2.3  jdolecek 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
    138  1.8.2.3  jdolecek 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK ||
    139  1.8.2.3  jdolecek 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK))
    140  1.8.2.3  jdolecek 		sc->sc_variant = GEM_SUN_GEM;
    141  1.8.2.3  jdolecek 	else if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    142  1.8.2.3  jdolecek 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC ||
    143  1.8.2.3  jdolecek 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2))
    144  1.8.2.3  jdolecek 		sc->sc_variant = GEM_APPLE_GMAC;
    145  1.8.2.3  jdolecek 
    146  1.8.2.2   thorpej #define PCI_GEM_BASEADDR	(PCI_MAPREG_START + 0x00)
    147  1.8.2.2   thorpej 
    148  1.8.2.2   thorpej 	/* XXX Need to check for a 64-bit mem BAR? */
    149  1.8.2.2   thorpej 	if (pci_mapreg_map(pa, PCI_GEM_BASEADDR,
    150  1.8.2.2   thorpej 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
    151  1.8.2.2   thorpej 	    &sc->sc_bustag, &sc->sc_h, NULL, NULL) != 0)
    152  1.8.2.2   thorpej 	{
    153  1.8.2.2   thorpej 		printf("%s: unable to map device registers\n",
    154  1.8.2.2   thorpej 		    sc->sc_dev.dv_xname);
    155  1.8.2.2   thorpej 		return;
    156  1.8.2.2   thorpej 	}
    157  1.8.2.2   thorpej 
    158  1.8.2.2   thorpej 	/*
    159  1.8.2.2   thorpej 	 * XXX This should be done with properties, when those are
    160  1.8.2.2   thorpej 	 * XXX fleshed out.
    161  1.8.2.2   thorpej 	 */
    162  1.8.2.2   thorpej #ifdef __sparc__
    163  1.8.2.2   thorpej 	{
    164  1.8.2.2   thorpej 		extern void myetheraddr __P((u_char *));
    165  1.8.2.2   thorpej 		myetheraddr(enaddr);
    166  1.8.2.2   thorpej 	}
    167  1.8.2.2   thorpej #endif /* __sparc__ */
    168  1.8.2.2   thorpej #ifdef macppc
    169  1.8.2.2   thorpej 	{
    170  1.8.2.2   thorpej 		int node;
    171  1.8.2.2   thorpej 
    172  1.8.2.2   thorpej 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    173  1.8.2.2   thorpej 		if (node == 0) {
    174  1.8.2.2   thorpej 			printf("%s: unable to locate OpenFirmware node\n",
    175  1.8.2.2   thorpej 			    sc->sc_dev.dv_xname);
    176  1.8.2.2   thorpej 			return;
    177  1.8.2.2   thorpej 		}
    178  1.8.2.2   thorpej 
    179  1.8.2.2   thorpej 		OF_getprop(node, "local-mac-address", enaddr, sizeof(enaddr));
    180  1.8.2.2   thorpej 	}
    181  1.8.2.2   thorpej #endif /* macppc */
    182  1.8.2.2   thorpej 
    183  1.8.2.2   thorpej 	if (pci_intr_map(pa, &ih) != 0) {
    184  1.8.2.2   thorpej 		printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
    185  1.8.2.2   thorpej 		return;
    186  1.8.2.2   thorpej 	}
    187  1.8.2.2   thorpej 	intrstr = pci_intr_string(pa->pa_pc, ih);
    188  1.8.2.2   thorpej 	gsc->gsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, gem_intr, sc);
    189  1.8.2.2   thorpej 	if (gsc->gsc_ih == NULL) {
    190  1.8.2.2   thorpej 		printf("%s: unable to establish interrupt",
    191  1.8.2.2   thorpej 		    sc->sc_dev.dv_xname);
    192  1.8.2.2   thorpej 		if (intrstr != NULL)
    193  1.8.2.2   thorpej 			printf(" at %s", intrstr);
    194  1.8.2.2   thorpej 		printf("\n");
    195  1.8.2.2   thorpej 		return;
    196  1.8.2.2   thorpej 	}
    197  1.8.2.2   thorpej 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    198  1.8.2.2   thorpej 
    199  1.8.2.2   thorpej 	/* Finish off the attach. */
    200  1.8.2.2   thorpej 	gem_attach(sc, enaddr);
    201  1.8.2.2   thorpej }
    202