1 /* $NetBSD: if_gem_pci.c,v 1.43 2010/11/13 13:52:06 uebayasi 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.43 2010/11/13 13:52:06 uebayasi 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 <net/if.h> 50 #include <net/if_dl.h> 51 #include <net/if_media.h> 52 #include <net/if_ether.h> 53 54 #include <net/bpf.h> 55 56 #include <sys/bus.h> 57 #include <sys/intr.h> 58 59 #include <dev/mii/mii.h> 60 #include <dev/mii/miivar.h> 61 #include <dev/mii/mii_bitbang.h> 62 63 #include <dev/ic/gemreg.h> 64 #include <dev/ic/gemvar.h> 65 66 #include <dev/pci/pcivar.h> 67 #include <dev/pci/pcireg.h> 68 #include <dev/pci/pcidevs.h> 69 #include <prop/proplib.h> 70 71 struct gem_pci_softc { 72 struct gem_softc gsc_gem; /* GEM device */ 73 void *gsc_ih; 74 pci_chipset_tag_t gsc_pc; 75 pci_intr_handle_t gsc_handle; 76 }; 77 78 static bool gem_pci_estintr(struct gem_pci_softc *); 79 static bool gem_pci_suspend(device_t, const pmf_qual_t *); 80 static bool gem_pci_resume(device_t, const pmf_qual_t *); 81 static int gem_pci_detach(device_t, int); 82 83 int gem_pci_match(device_t, cfdata_t, void *); 84 void gem_pci_attach(device_t, device_t, void *); 85 86 CFATTACH_DECL3_NEW(gem_pci, sizeof(struct gem_pci_softc), 87 gem_pci_match, gem_pci_attach, gem_pci_detach, NULL, NULL, NULL, 88 DVF_DETACH_SHUTDOWN); 89 90 /* 91 * Attach routines need to be split out to different bus-specific files. 92 */ 93 94 int 95 gem_pci_match(device_t parent, cfdata_t cf, void *aux) 96 { 97 struct pci_attach_args *pa = aux; 98 99 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN && 100 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK || 101 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK)) 102 return (1); 103 104 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE && 105 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC || 106 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2 || 107 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC3 || 108 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_SHASTA_GMAC || 109 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_K2_GMAC || 110 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_SHASTA_GMAC || 111 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_INTREPID2_GMAC)) 112 return (1); 113 114 115 return (0); 116 } 117 118 static inline int 119 gempromvalid(u_int8_t* buf) 120 { 121 return buf[0] == 0x18 && buf[1] == 0x00 && /* structure length */ 122 buf[2] == 0x00 && /* revision */ 123 (buf[3] == 0x00 || /* hme */ 124 buf[3] == 0x80) && /* qfe */ 125 buf[4] == PCI_SUBCLASS_NETWORK_ETHERNET && /* subclass code */ 126 buf[5] == PCI_CLASS_NETWORK; /* class code */ 127 } 128 129 static inline int 130 isshared_pins(u_int8_t* buf) 131 { 132 return buf[0] == 's' && buf[1] == 'h' && buf[2] == 'a' && 133 buf[3] == 'r' && buf[4] == 'e' && buf[5] == 'd' && 134 buf[6] == '-' && buf[7] == 'p' && buf[8] == 'i' && 135 buf[9] == 'n' && buf[10] == 's'; 136 } 137 138 static inline int 139 isserdes(u_int8_t* buf) 140 { 141 return buf[0] == 's' && buf[1] == 'e' && buf[2] == 'r' && 142 buf[3] == 'd' && buf[4] == 'e' && buf[5] == 's'; 143 } 144 145 void 146 gem_pci_attach(device_t parent, device_t self, void *aux) 147 { 148 struct pci_attach_args *pa = aux; 149 struct gem_pci_softc *gsc = device_private(self); 150 struct gem_softc *sc = &gsc->gsc_gem; 151 char devinfo[256]; 152 prop_data_t data; 153 uint8_t enaddr[ETHER_ADDR_LEN]; 154 u_int8_t *enp; 155 bus_space_handle_t romh; 156 u_int8_t buf[0x0800]; 157 int dataoff, vpdoff, serdes; 158 int i, got_addr = 0; 159 #ifdef GEM_DEBUG 160 int j; 161 #endif 162 struct pci_vpd *vpd; 163 static const u_int8_t promhdr[] = { 0x55, 0xaa }; 164 #define PROMHDR_PTR_DATA 0x18 165 static const u_int8_t promdat[] = { 166 0x50, 0x43, 0x49, 0x52, /* "PCIR" */ 167 PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8, 168 PCI_PRODUCT_SUN_GEMNETWORK & 0xff, 169 PCI_PRODUCT_SUN_GEMNETWORK >> 8 170 }; 171 #define PROMDATA_PTR_VPD 0x08 172 #define PROMDATA_DATA2 0x0a 173 174 aprint_naive(": Ethernet controller\n"); 175 176 sc->sc_dev = self; 177 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 178 sc->sc_chiprev = PCI_REVISION(pa->pa_class); 179 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, sc->sc_chiprev); 180 181 /* 182 * Some Sun GEMs/ERIs do have their intpin register bogusly set to 0, 183 * although it should be 1. correct that. 184 */ 185 if (pa->pa_intrpin == 0) 186 pa->pa_intrpin = 1; 187 188 sc->sc_variant = GEM_UNKNOWN; 189 190 sc->sc_dmatag = pa->pa_dmat; 191 192 sc->sc_flags |= GEM_PCI; 193 194 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN) { 195 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK) 196 sc->sc_variant = GEM_SUN_GEM; 197 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK) 198 sc->sc_variant = GEM_SUN_ERI; 199 } else if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE) { 200 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC || 201 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2 || 202 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC3 || 203 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_SHASTA_GMAC || 204 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_INTREPID2_GMAC) 205 sc->sc_variant = GEM_APPLE_GMAC; 206 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_K2_GMAC) 207 sc->sc_variant = GEM_APPLE_K2_GMAC; 208 } 209 210 if (sc->sc_variant == GEM_UNKNOWN) { 211 aprint_error_dev(sc->sc_dev, "unknown adaptor\n"); 212 return; 213 } 214 215 #define PCI_GEM_BASEADDR (PCI_MAPREG_START + 0x00) 216 217 /* XXX Need to check for a 64-bit mem BAR? */ 218 if (pci_mapreg_map(pa, PCI_GEM_BASEADDR, 219 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0, 220 &sc->sc_bustag, &sc->sc_h1, NULL, &sc->sc_size) != 0) 221 { 222 aprint_error_dev(sc->sc_dev, "unable to map device registers\n"); 223 return; 224 } 225 if (bus_space_subregion(sc->sc_bustag, sc->sc_h1, 226 GEM_PCI_BANK2_OFFSET, GEM_PCI_BANK2_SIZE, &sc->sc_h2)) { 227 aprint_error_dev(sc->sc_dev, "unable to create bank 2 subregion\n"); 228 return; 229 } 230 231 if ((data = prop_dictionary_get(device_properties(sc->sc_dev), 232 "mac-address")) != NULL) { 233 memcpy(enaddr, prop_data_data_nocopy(data), ETHER_ADDR_LEN); 234 got_addr = 1; 235 if ((data = prop_dictionary_get(device_properties(sc->sc_dev), 236 "shared-pins")) != NULL) { 237 memcpy(buf, prop_data_data_nocopy(data), 238 prop_data_size(data)); 239 if (isserdes(buf)) { 240 sc->sc_flags |= GEM_SERDES; 241 } 242 } 243 } else { 244 /* 245 * Dig out VPD (vital product data) and acquire Ethernet address. 246 * The VPD of gem resides in the PCI PROM (PCI FCode). 247 */ 248 /* 249 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later) 250 * chapter 2 describes the data structure. 251 */ 252 253 enp = NULL; 254 255 if (sc->sc_variant == GEM_SUN_GEM && 256 (bus_space_subregion(sc->sc_bustag, sc->sc_h1, 257 GEM_PCI_ROM_OFFSET, GEM_PCI_ROM_SIZE, &romh)) == 0) { 258 259 /* read PCI Expansion PROM Header */ 260 bus_space_read_region_1(sc->sc_bustag, 261 romh, 0, buf, sizeof buf); 262 263 /* Check for "shared-pins = serdes" in FCode. */ 264 i = 0; 265 serdes = 0; 266 while (i < (sizeof buf) - sizeof "serdes") { 267 if (!serdes) { 268 if (isserdes(&buf[i])) 269 serdes = 1; 270 } else { 271 if (isshared_pins(&buf[i])) 272 serdes = 2; 273 } 274 if (serdes == 2) { 275 sc->sc_flags |= GEM_SERDES; 276 break; 277 } 278 i++; 279 } 280 #ifdef GEM_DEBUG 281 /* PROM dump */ 282 printf("%s: PROM dump (0x0000 to %04lx)\n", device_xname(sc->sc_dev), 283 (sizeof buf) - 1); 284 i = 0; 285 j = 0; 286 printf(" %04x ", i); 287 while (i < sizeof buf) { 288 printf("%02x ", buf[i]); 289 if (i && !(i % 8)) 290 printf(" "); 291 if (i && !(i % 16)) { 292 printf(" "); 293 while (j < i) { 294 if (buf[j] > 31 && buf[j] < 128) 295 printf("%c", buf[j]); 296 else 297 printf("."); 298 j++; 299 } 300 j = i; 301 printf("\n %04x ", i); 302 } 303 i++; 304 } 305 printf("\n"); 306 #endif 307 308 if (memcmp(buf, promhdr, sizeof promhdr) == 0 && 309 (dataoff = (buf[PROMHDR_PTR_DATA] | 310 (buf[PROMHDR_PTR_DATA + 1] << 8))) >= 0x1c) { 311 312 /* read PCI Expansion PROM Data */ 313 bus_space_read_region_1(sc->sc_bustag, romh, dataoff, 314 buf, 64); 315 if (memcmp(buf, promdat, sizeof promdat) == 0 && 316 gempromvalid(buf + PROMDATA_DATA2) && 317 (vpdoff = (buf[PROMDATA_PTR_VPD] | 318 (buf[PROMDATA_PTR_VPD + 1] << 8))) >= 0x1c) { 319 320 /* 321 * The VPD of gem is not in PCI 2.2 standard 322 * format. The length in the resource header 323 * is in big endian, and resources are not 324 * properly terminated (only one resource 325 * and no end tag). 326 */ 327 /* read PCI VPD */ 328 bus_space_read_region_1(sc->sc_bustag, romh, 329 vpdoff, buf, 64); 330 vpd = (void *)(buf + 3); 331 if (PCI_VPDRES_ISLARGE(buf[0]) && 332 PCI_VPDRES_LARGE_NAME(buf[0]) 333 == PCI_VPDRES_TYPE_VPD && 334 vpd->vpd_key0 == 0x4e /* N */ && 335 vpd->vpd_key1 == 0x41 /* A */ && 336 vpd->vpd_len == ETHER_ADDR_LEN) { 337 /* 338 * Ethernet address found 339 */ 340 enp = buf + 6; 341 } 342 } 343 } 344 } 345 346 if (enp) { 347 memcpy(enaddr, enp, ETHER_ADDR_LEN); 348 got_addr = 1; 349 } 350 } 351 if (!got_addr) { 352 printf("%s: no Ethernet address found\n", device_xname(sc->sc_dev)); 353 /* should we bail here? */ 354 } 355 356 if (pci_intr_map(pa, &gsc->gsc_handle) != 0) { 357 aprint_error_dev(sc->sc_dev, "unable to map interrupt\n"); 358 return; 359 } 360 gsc->gsc_pc = pa->pa_pc; 361 gem_pci_estintr(gsc); 362 363 /* Finish off the attach. */ 364 gem_attach(sc, enaddr); 365 366 if (pmf_device_register1(sc->sc_dev, 367 gem_pci_suspend, gem_pci_resume, gem_shutdown)) 368 pmf_class_network_register(sc->sc_dev, &sc->sc_ethercom.ec_if); 369 else 370 aprint_error_dev(sc->sc_dev, 371 "could not establish power handlers\n"); 372 } 373 374 static bool 375 gem_pci_suspend(device_t self, const pmf_qual_t *qual) 376 { 377 struct gem_pci_softc *gsc = device_private(self); 378 379 if (gsc->gsc_ih != NULL) { 380 pci_intr_disestablish(gsc->gsc_pc, gsc->gsc_ih); 381 gsc->gsc_ih = NULL; 382 } 383 384 return true; 385 } 386 387 static bool 388 gem_pci_estintr(struct gem_pci_softc *gsc) 389 { 390 struct gem_softc *sc = &gsc->gsc_gem; 391 const char *intrstr; 392 393 intrstr = pci_intr_string(gsc->gsc_pc, gsc->gsc_handle); 394 gsc->gsc_ih = pci_intr_establish(gsc->gsc_pc, gsc->gsc_handle, IPL_NET, 395 gem_intr, sc); 396 if (gsc->gsc_ih == NULL) { 397 aprint_error_dev(sc->sc_dev, "unable to establish interrupt"); 398 if (intrstr != NULL) 399 aprint_error(" at %s", intrstr); 400 aprint_error("\n"); 401 return false; 402 } 403 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr); 404 return true; 405 } 406 407 static bool 408 gem_pci_resume(device_t self, const pmf_qual_t *qual) 409 { 410 struct gem_pci_softc *gsc = device_private(self); 411 412 return gem_pci_estintr(gsc); 413 } 414 415 static int 416 gem_pci_detach(device_t self, int flags) 417 { 418 int rc; 419 struct gem_pci_softc *gsc = device_private(self); 420 struct gem_softc *sc = &gsc->gsc_gem; 421 422 switch (sc->sc_att_stage) { 423 case GEM_ATT_BACKEND_2: 424 pmf_device_deregister(self); 425 sc->sc_att_stage = GEM_ATT_FINISHED; 426 /*FALLTHROUGH*/ 427 default: 428 if ((rc = gem_detach(sc, flags)) != 0) 429 return rc; 430 /*FALLTHROUGH*/ 431 case GEM_ATT_BACKEND_1: 432 if (gsc->gsc_ih != NULL) 433 pci_intr_disestablish(gsc->gsc_pc, gsc->gsc_ih); 434 435 bus_space_unmap(sc->sc_bustag, sc->sc_h1, sc->sc_size); 436 /*FALLTHROUGH*/ 437 case GEM_ATT_BACKEND_0: 438 sc->sc_att_stage = GEM_ATT_BACKEND_0; 439 break; 440 } 441 return 0; 442 } 443