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