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