Home | History | Annotate | Line # | Download | only in pci
if_gem_pci.c revision 1.29
      1 /*	$NetBSD: if_gem_pci.c,v 1.29 2008/04/10 19:13:37 cegger Exp $ */
      2 
      3 /*
      4  *
      5  * Copyright (C) 2001 Eduardo Horvath.
      6  * All rights reserved.
      7  *
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  *
     30  */
     31 
     32 /*
     33  * PCI bindings for Apple GMAC, Sun ERI and Sun GEM Ethernet controllers
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: if_gem_pci.c,v 1.29 2008/04/10 19:13:37 cegger Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/malloc.h>
     42 #include <sys/kernel.h>
     43 #include <sys/socket.h>
     44 #include <sys/errno.h>
     45 #include <sys/device.h>
     46 
     47 #include <machine/endian.h>
     48 
     49 #include <uvm/uvm_extern.h>
     50 
     51 #include <net/if.h>
     52 #include <net/if_dl.h>
     53 #include <net/if_media.h>
     54 #include <net/if_ether.h>
     55 
     56 #if NBPFILTER > 0
     57 #include <net/bpf.h>
     58 #endif
     59 
     60 #include <sys/bus.h>
     61 #include <sys/intr.h>
     62 
     63 #include <dev/mii/mii.h>
     64 #include <dev/mii/miivar.h>
     65 #include <dev/mii/mii_bitbang.h>
     66 
     67 #include <dev/ic/gemreg.h>
     68 #include <dev/ic/gemvar.h>
     69 
     70 #include <dev/pci/pcivar.h>
     71 #include <dev/pci/pcireg.h>
     72 #include <dev/pci/pcidevs.h>
     73 
     74 /* XXX Should use Properties when that's fleshed out. */
     75 #ifdef macppc
     76 #include <dev/ofw/openfirm.h>
     77 #endif /* macppc */
     78 #ifdef __sparc__
     79 #include <machine/promlib.h>
     80 #endif
     81 
     82 #ifndef GEM_USE_LOCAL_MAC_ADDRESS
     83 #if defined (macppc) || defined (__sparc__)
     84 #define GEM_USE_LOCAL_MAC_ADDRESS	0	/* use system-wide address */
     85 #else
     86 #define GEM_USE_LOCAL_MAC_ADDRESS	1
     87 #endif
     88 #endif
     89 
     90 
     91 struct gem_pci_softc {
     92 	struct	gem_softc	gsc_gem;	/* GEM device */
     93 	void			*gsc_ih;
     94 };
     95 
     96 int	gem_match_pci(struct device *, struct cfdata *, void *);
     97 void	gem_attach_pci(struct device *, struct device *, void *);
     98 
     99 CFATTACH_DECL(gem_pci, sizeof(struct gem_pci_softc),
    100     gem_match_pci, gem_attach_pci, NULL, NULL);
    101 
    102 /*
    103  * Attach routines need to be split out to different bus-specific files.
    104  */
    105 
    106 int
    107 gem_match_pci(parent, cf, aux)
    108 	struct device *parent;
    109 	struct cfdata *cf;
    110 	void *aux;
    111 {
    112 	struct pci_attach_args *pa = aux;
    113 
    114 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
    115 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK ||
    116 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK))
    117 		return (1);
    118 
    119 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
    120 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC ||
    121 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2 ||
    122 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC3 ||
    123  	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_SHASTA_GMAC ||
    124 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_K2_GMAC ||
    125 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_INTREPID2_GMAC))
    126 		return (1);
    127 
    128 
    129 	return (0);
    130 }
    131 
    132 #if GEM_USE_LOCAL_MAC_ADDRESS
    133 static inline int
    134 gempromvalid(u_int8_t* buf)
    135 {
    136 	return buf[0] == 0x18 && buf[1] == 0x00 &&	/* structure length */
    137 	    buf[2] == 0x00 &&				/* revision */
    138 	    (buf[3] == 0x00 ||				/* hme */
    139 	     buf[3] == 0x80) &&				/* qfe */
    140 	    buf[4] == PCI_SUBCLASS_NETWORK_ETHERNET &&	/* subclass code */
    141 	    buf[5] == PCI_CLASS_NETWORK;		/* class code */
    142 }
    143 
    144 static inline int
    145 isshared_pins(u_int8_t* buf)
    146 {
    147 	return buf[0] == 's' && buf[1] == 'h' && buf[2] == 'a' &&
    148 	    buf[3] == 'r' && buf[4] == 'e' && buf[5] == 'd' &&
    149 	    buf[6] == '-' && buf[7] == 'p' && buf[8] == 'i' &&
    150 	    buf[9] == 'n' && buf[10] == 's';
    151 }
    152 #endif
    153 
    154 static inline int
    155 isserdes(u_int8_t* buf)
    156 {
    157 	return buf[0] == 's' && buf[1] == 'e' && buf[2] == 'r' &&
    158 	    buf[3] == 'd' && buf[4] == 'e' && buf[5] == 's';
    159 }
    160 
    161 void
    162 gem_attach_pci(parent, self, aux)
    163 	struct device *parent, *self;
    164 	void *aux;
    165 {
    166 	struct pci_attach_args *pa = aux;
    167 	struct gem_pci_softc *gsc = (void *)self;
    168 	struct gem_softc *sc = &gsc->gsc_gem;
    169 	pci_intr_handle_t ih;
    170 	const char *intrstr;
    171 	char devinfo[256];
    172 	uint8_t enaddr[ETHER_ADDR_LEN];
    173 #if GEM_USE_LOCAL_MAC_ADDRESS
    174 	u_int8_t		*enp;
    175 	bus_space_handle_t	romh;
    176 	u_int8_t		buf[0x0800];
    177 	int			dataoff, vpdoff, serdes;
    178 #if GEM_USE_LOCAL_MAC_ADDRESS || defined(GEM_DEBUG)
    179 	int i;
    180 #endif
    181 #ifdef GEM_DEBUG
    182 	int j;
    183 #endif
    184 	struct pci_vpd		*vpd;
    185 	static const u_int8_t promhdr[] = { 0x55, 0xaa };
    186 #define PROMHDR_PTR_DATA	0x18
    187 	static const u_int8_t promdat[] = {
    188 		0x50, 0x43, 0x49, 0x52,		/* "PCIR" */
    189 		PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8,
    190 		PCI_PRODUCT_SUN_GEMNETWORK & 0xff,
    191 		PCI_PRODUCT_SUN_GEMNETWORK >> 8
    192 	};
    193 #define PROMDATA_PTR_VPD	0x08
    194 #define PROMDATA_DATA2		0x0a
    195 #endif  /* GEM_USE_LOCAL_MAC_ADDRESS */
    196 
    197 	aprint_naive(": Ethernet controller\n");
    198 
    199 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    200 	sc->sc_chiprev = PCI_REVISION(pa->pa_class);
    201 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, sc->sc_chiprev);
    202 
    203 	/*
    204 	 * Some Sun GEMs/ERIs do have their intpin register bogusly set to 0,
    205 	 * although it should be 1. correct that.
    206 	 */
    207 	if (pa->pa_intrpin == 0)
    208 		pa->pa_intrpin = 1;
    209 
    210 	sc->sc_variant = GEM_UNKNOWN;
    211 
    212 	sc->sc_dmatag = pa->pa_dmat;
    213 
    214 	sc->sc_flags |= GEM_PCI;
    215 
    216 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN) {
    217 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK)
    218 			sc->sc_variant = GEM_SUN_GEM;
    219 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK)
    220 			sc->sc_variant = GEM_SUN_ERI;
    221 	} else if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE) {
    222 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC ||
    223 		     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2 ||
    224 		     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC3 ||
    225 		     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_SHASTA_GMAC ||
    226 		     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_INTREPID2_GMAC)
    227 			sc->sc_variant = GEM_APPLE_GMAC;
    228 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_K2_GMAC)
    229 			sc->sc_variant = GEM_APPLE_K2_GMAC;
    230 	}
    231 
    232 	if (sc->sc_variant == GEM_UNKNOWN) {
    233 		aprint_error_dev(&sc->sc_dev, "unknown adaptor\n");
    234 		return;
    235 	}
    236 
    237 #define PCI_GEM_BASEADDR	(PCI_MAPREG_START + 0x00)
    238 
    239 	/* XXX Need to check for a 64-bit mem BAR? */
    240 	if (pci_mapreg_map(pa, PCI_GEM_BASEADDR,
    241 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
    242 	    &sc->sc_bustag, &sc->sc_h1, NULL, NULL) != 0)
    243 	{
    244 		aprint_error_dev(&sc->sc_dev, "unable to map device registers\n");
    245 		return;
    246 	}
    247 	if (bus_space_subregion(sc->sc_bustag, sc->sc_h1,
    248 	    GEM_PCI_BANK2_OFFSET, GEM_PCI_BANK2_SIZE, &sc->sc_h2)) {
    249 		aprint_error_dev(&sc->sc_dev, "unable to create bank 2 subregion\n");
    250 		return;
    251 	}
    252 
    253 #if GEM_USE_LOCAL_MAC_ADDRESS
    254 	/*
    255 	 * Dig out VPD (vital product data) and acquire Ethernet address.
    256 	 * The VPD of gem resides in the PCI PROM (PCI FCode).
    257 	 */
    258 	/*
    259 	 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later)
    260 	 * chapter 2 describes the data structure.
    261 	 */
    262 
    263 	enp = NULL;
    264 
    265 	if (sc->sc_variant == GEM_SUN_GEM &&
    266 	    (bus_space_subregion(sc->sc_bustag, sc->sc_h1,
    267 	    GEM_PCI_ROM_OFFSET, GEM_PCI_ROM_SIZE, &romh)) == 0) {
    268 
    269 		/* read PCI Expansion PROM Header */
    270 		bus_space_read_region_1(sc->sc_bustag,
    271 		    romh, 0, buf, sizeof buf);
    272 
    273 		/* Check for "shared-pins = serdes" in FCode. */
    274 		i = 0;
    275 		serdes = 0;
    276 		while (i < (sizeof buf) - sizeof "serdes") {
    277 			if (!serdes) {
    278 				if (isserdes(&buf[i]))
    279 					serdes = 1;
    280 			} else {
    281 				if (isshared_pins(&buf[i]))
    282 					serdes = 2;
    283 			}
    284 			if (serdes == 2) {
    285 				sc->sc_flags |= GEM_SERDES;
    286 				break;
    287 			}
    288 			i++;
    289 		}
    290 #ifdef GEM_DEBUG
    291 		/* PROM dump */
    292 		printf("%s: PROM dump (0x0000 to %04lx)\n", device_xname(&sc->sc_dev),
    293 		    (sizeof buf) - 1);
    294 		i = 0;
    295 		j = 0;
    296 		printf("  %04x  ", i);
    297 		while (i < sizeof buf) {
    298 			printf("%02x ", buf[i]);
    299 			if (i && !(i % 8))
    300 				printf(" ");
    301 			if (i && !(i % 16)) {
    302 				printf(" ");
    303 				while (j < i) {
    304 					if (buf[j] > 31 && buf[j] < 128)
    305 						printf("%c", buf[j]);
    306 					else
    307 						printf(".");
    308 					j++;
    309 				}
    310 				j = i;
    311 				printf("\n  %04x  ", i);
    312 				}
    313 			i++;
    314 		}
    315 		printf("\n");
    316 #endif
    317 
    318 		if (memcmp(buf, promhdr, sizeof promhdr) == 0 &&
    319 		    (dataoff = (buf[PROMHDR_PTR_DATA] |
    320 			(buf[PROMHDR_PTR_DATA + 1] << 8))) >= 0x1c) {
    321 
    322 			/* read PCI Expansion PROM Data */
    323 			bus_space_read_region_1(sc->sc_bustag, romh, dataoff,
    324 			    buf, 64);
    325 			if (memcmp(buf, promdat, sizeof promdat) == 0 &&
    326 			    gempromvalid(buf + PROMDATA_DATA2) &&
    327 			    (vpdoff = (buf[PROMDATA_PTR_VPD] |
    328 				(buf[PROMDATA_PTR_VPD + 1] << 8))) >= 0x1c) {
    329 
    330 				/*
    331 				 * The VPD of gem is not in PCI 2.2 standard
    332 				 * format.  The length in the resource header
    333 				 * is in big endian, and resources are not
    334 				 * properly terminated (only one resource
    335 				 * and no end tag).
    336 				 */
    337 				/* read PCI VPD */
    338 				bus_space_read_region_1(sc->sc_bustag, romh,
    339 				    vpdoff, buf, 64);
    340 				vpd = (void *)(buf + 3);
    341 				if (PCI_VPDRES_ISLARGE(buf[0]) &&
    342 				    PCI_VPDRES_LARGE_NAME(buf[0])
    343 					== PCI_VPDRES_TYPE_VPD &&
    344 				    vpd->vpd_key0 == 0x4e /* N */ &&
    345 				    vpd->vpd_key1 == 0x41 /* A */ &&
    346 				    vpd->vpd_len == ETHER_ADDR_LEN) {
    347 					/*
    348 					 * Ethernet address found
    349 					 */
    350 					enp = buf + 6;
    351 				}
    352 			}
    353 		}
    354 	}
    355 
    356 	if (enp)
    357 		memcpy(enaddr, enp, ETHER_ADDR_LEN);
    358 	else
    359 #endif  /* GEM_USE_LOCAL_MAC_ADDRESS */
    360 #ifdef __sparc__
    361 	{
    362 		if (strcmp(prom_getpropstring(PCITAG_NODE(pa->pa_tag),
    363 		    "shared-pins"), "serdes") == 0)
    364 			sc->sc_flags |= GEM_SERDES;
    365 		prom_getether(PCITAG_NODE(pa->pa_tag), enaddr);
    366 	}
    367 #else
    368 #ifdef macppc
    369 	{
    370 		int node;
    371 		char sp[6];	/* "serdes" */
    372 
    373 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    374 		if (node == 0) {
    375 			aprint_error_dev(&sc->sc_dev, "unable to locate OpenFirmware node\n");
    376 			return;
    377 		}
    378 
    379 		OF_getprop(node, "shared-pins", sp, sizeof(sp));
    380 		if (isserdes(sp))
    381 			sc->sc_flags |= GEM_SERDES;
    382 		OF_getprop(node, "local-mac-address", enaddr, sizeof(enaddr));
    383 	}
    384 #else
    385 		printf("%s: no Ethernet address found\n", device_xname(&sc->sc_dev));
    386 #endif /* macppc */
    387 #endif /* __sparc__ */
    388 
    389 	if (pci_intr_map(pa, &ih) != 0) {
    390 		aprint_error_dev(&sc->sc_dev, "unable to map interrupt\n");
    391 		return;
    392 	}
    393 	intrstr = pci_intr_string(pa->pa_pc, ih);
    394 	gsc->gsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, gem_intr, sc);
    395 	if (gsc->gsc_ih == NULL) {
    396 		aprint_error_dev(&sc->sc_dev, "unable to establish interrupt");
    397 		if (intrstr != NULL)
    398 			aprint_normal(" at %s", intrstr);
    399 		aprint_normal("\n");
    400 		return;
    401 	}
    402 	aprint_normal_dev(&sc->sc_dev, "interrupting at %s\n", intrstr);
    403 
    404 	/* Finish off the attach. */
    405 	gem_attach(sc, enaddr);
    406 }
    407