1 /* $OpenBSD: if_wi_pci.c,v 1.35 2003/06/17 21:56:25 millert Exp $ */ 2 3 /* 4 * Copyright (c) 2001-2003 Todd C. Miller <Todd.Miller@courtesan.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 * 18 * Sponsored in part by the Defense Advanced Research Projects 19 * Agency (DARPA) and Air Force Research Laboratory, Air Force 20 * Materiel Command, USAF, under agreement number F39502-99-1-0512. 21 */ 22 23 /* 24 * PCI attachment for the Wavelan driver. There are two basic types 25 * of PCI card supported: 26 * 27 * 1) Cards based on the Prism2.5 Mini-PCI chipset 28 * 2) Cards that use a dumb PCMCIA->PCI bridge 29 * 30 * Only the first type are "true" PCI cards. 31 * 32 * The latter are simply PCMCIA cards (or the guts of same) with some 33 * type of dumb PCMCIA->PCI bridge. They are "dumb" in that they 34 * are not true PCMCIA bridges and really just serve to deal with 35 * the different interrupt types and timings of the ISA vs. PCI bus. 36 * 37 * The following bridge types are supported: 38 * o PLX 9052 (the most common) 39 * o TMD 7160 (found in some NDC/Sohoware NCP130 cards) 40 * o ACEX EP1K30 (really a PLD, found in Symbol cards and their OEMs) 41 */ 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/device.h> 46 #include <sys/timeout.h> 47 #include <sys/socket.h> 48 49 #include <net/if.h> 50 #include <net/if_dl.h> 51 #include <net/if_media.h> 52 53 #ifdef INET 54 #include <netinet/in.h> 55 #include <netinet/if_ether.h> 56 #endif 57 58 #include <net/if_ieee80211.h> 59 60 #include <machine/bus.h> 61 62 #include <dev/pci/pcireg.h> 63 #include <dev/pci/pcivar.h> 64 #include <dev/pci/pcidevs.h> 65 66 #include <dev/ic/if_wireg.h> 67 #include <dev/ic/if_wi_ieee.h> 68 #include <dev/ic/if_wivar.h> 69 70 /* For printing CIS of the actual PCMCIA card */ 71 #define CIS_MFG_NAME_OFFSET 0x16 72 #define CIS_INFO_SIZE 256 73 74 const struct wi_pci_product *wi_pci_lookup(struct pci_attach_args *pa); 75 int wi_pci_match(struct device *, void *, void *); 76 void wi_pci_attach(struct device *, struct device *, void *); 77 int wi_pci_acex_attach(struct pci_attach_args *pa, struct wi_softc *sc); 78 int wi_pci_plx_attach(struct pci_attach_args *pa, struct wi_softc *sc); 79 int wi_pci_tmd_attach(struct pci_attach_args *pa, struct wi_softc *sc); 80 int wi_pci_native_attach(struct pci_attach_args *pa, struct wi_softc *sc); 81 int wi_pci_common_attach(struct pci_attach_args *pa, struct wi_softc *sc); 82 void wi_pci_plx_print_cis(struct wi_softc *); 83 84 struct cfattach wi_pci_ca = { 85 sizeof (struct wi_softc), wi_pci_match, wi_pci_attach 86 }; 87 88 static const struct wi_pci_product { 89 pci_vendor_id_t pp_vendor; 90 pci_product_id_t pp_product; 91 int (*pp_attach)(struct pci_attach_args *pa, struct wi_softc *sc); 92 } wi_pci_products[] = { 93 { PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P, wi_pci_plx_attach }, 94 { PCI_VENDOR_GLOBALSUN, PCI_PRODUCT_GLOBALSUN_GL24110P02, wi_pci_plx_attach }, 95 { PCI_VENDOR_EUMITCOM, PCI_PRODUCT_EUMITCOM_WL11000P, wi_pci_plx_attach }, 96 { PCI_VENDOR_USR2, PCI_PRODUCT_USR2_WL11000P, wi_pci_plx_attach }, 97 { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CRWE777A, wi_pci_plx_attach }, 98 { PCI_VENDOR_NETGEAR, PCI_PRODUCT_NETGEAR_MA301, wi_pci_plx_attach }, 99 { PCI_VENDOR_EFFICIENTNETS, PCI_PRODUCT_EFFICIENTNETS_SS1023, wi_pci_plx_attach }, 100 { PCI_VENDOR_NDC, PCI_PRODUCT_NDC_NCP130, wi_pci_plx_attach }, 101 { PCI_VENDOR_NDC, PCI_PRODUCT_NDC_NCP130A2, wi_pci_tmd_attach }, 102 { PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_MINI_PCI_WLAN, wi_pci_native_attach }, 103 { PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_211818A, wi_pci_acex_attach }, 104 { PCI_VENDOR_SYMBOL, PCI_PRODUCT_SYMBOL_LA41X3, wi_pci_acex_attach }, 105 { 0, 0, 0 } 106 }; 107 108 const struct wi_pci_product * 109 wi_pci_lookup(struct pci_attach_args *pa) 110 { 111 const struct wi_pci_product *pp; 112 113 for (pp = wi_pci_products; pp->pp_product != 0; pp++) { 114 if (PCI_VENDOR(pa->pa_id) == pp->pp_vendor && 115 PCI_PRODUCT(pa->pa_id) == pp->pp_product) 116 return (pp); 117 } 118 119 return (NULL); 120 } 121 122 int 123 wi_pci_match(struct device *parent, void *match, void *aux) 124 { 125 return (wi_pci_lookup(aux) != NULL); 126 } 127 128 void 129 wi_pci_attach(struct device *parent, struct device *self, void *aux) 130 { 131 struct wi_softc *sc = (struct wi_softc *)self; 132 struct pci_attach_args *pa = aux; 133 const struct wi_pci_product *pp; 134 135 pp = wi_pci_lookup(pa); 136 if (pp->pp_attach(pa, sc) != 0) 137 return; 138 wi_attach(sc); 139 } 140 141 /* 142 * ACEX EP1K30-based PCMCIA->PCI bridge attachment. 143 * 144 * The ACEX EP1K30 is a programmable logic device (PLD) used as a 145 * PCMCIA->PCI bridge on the Symbol LA4123 and its OEM equivalents 146 * (such as the Nortel E-mobility 211818-A). There are 3 I/O ports: 147 * BAR0 at 0x10 appears to be a command port. 148 * BAR1 at 0x14 contains COR at offset 0xe0. 149 * BAR2 at 0x18 maps the actual PCMCIA card. 150 * 151 * The datasheet for the ACEX EP1K30 is available from Altera but that 152 * doesn't really help much since we don't know how it is programmed. 153 * Details for this attachment were gleaned from a version of the 154 * Linux orinoco driver modified by Tobias Hoffmann based on 155 * what he discoverd from the Windows driver. 156 */ 157 int 158 wi_pci_acex_attach(struct pci_attach_args *pa, struct wi_softc *sc) 159 { 160 bus_space_handle_t commandh, localh, ioh; 161 bus_space_tag_t commandt, localt; 162 bus_space_tag_t iot = pa->pa_iot; 163 bus_size_t commandsize, localsize, iosize; 164 int i; 165 166 if (pci_mapreg_map(pa, WI_ACEX_CMDRES, PCI_MAPREG_TYPE_IO, 167 0, &commandt, &commandh, NULL, &commandsize, 0) != 0) { 168 printf(": can't map command I/O space\n"); 169 return (ENXIO); 170 } 171 172 if (pci_mapreg_map(pa, WI_ACEX_LOCALRES, PCI_MAPREG_TYPE_IO, 173 0, &localt, &localh, NULL, &localsize, 0) != 0) { 174 printf(": can't map local I/O space\n"); 175 bus_space_unmap(commandt, commandh, commandsize); 176 return (ENXIO); 177 } 178 sc->wi_ltag = localt; 179 sc->wi_lhandle = localh; 180 181 if (pci_mapreg_map(pa, WI_TMD_IORES, PCI_MAPREG_TYPE_IO, 182 0, &iot, &ioh, NULL, &iosize, 0) != 0) { 183 printf(": can't map I/O space\n"); 184 bus_space_unmap(localt, localh, localsize); 185 bus_space_unmap(commandt, commandh, commandsize); 186 return (ENXIO); 187 } 188 sc->wi_btag = iot; 189 sc->wi_bhandle = ioh; 190 191 /* 192 * Setup bridge chip. 193 */ 194 if (bus_space_read_4(commandt, commandh, 0) & 1) { 195 printf(": bridge not ready\n"); 196 bus_space_unmap(iot, ioh, iosize); 197 bus_space_unmap(localt, localh, localsize); 198 bus_space_unmap(commandt, commandh, commandsize); 199 return (ENXIO); 200 } 201 bus_space_write_4(commandt, commandh, 2, 0x118); 202 bus_space_write_4(commandt, commandh, 2, 0x108); 203 DELAY(30 * 1000); 204 bus_space_write_4(commandt, commandh, 2, 0x8); 205 for (i = 0; i < 30; i++) { 206 DELAY(30 * 1000); 207 if (bus_space_read_4(commandt, commandh, 0) & 0x10) 208 break; 209 } 210 if (i == 30) { 211 printf(": bridge timeout\n"); 212 bus_space_unmap(iot, ioh, iosize); 213 bus_space_unmap(localt, localh, localsize); 214 bus_space_unmap(commandt, commandh, commandsize); 215 return (ENXIO); 216 } 217 if ((bus_space_read_4(localt, localh, 0xe0) & 1) || 218 (bus_space_read_4(localt, localh, 0xe2) & 1) || 219 (bus_space_read_4(localt, localh, 0xe4) & 1)) { 220 printf(": failed bridge setup\n"); 221 bus_space_unmap(iot, ioh, iosize); 222 bus_space_unmap(localt, localh, localsize); 223 bus_space_unmap(commandt, commandh, commandsize); 224 return (ENXIO); 225 } 226 227 if (wi_pci_common_attach(pa, sc) != 0) { 228 bus_space_unmap(iot, ioh, iosize); 229 bus_space_unmap(localt, localh, localsize); 230 bus_space_unmap(commandt, commandh, commandsize); 231 return (ENXIO); 232 } 233 234 /* 235 * Enable I/O mode and level interrupts on the embedded PCMCIA 236 * card. 237 */ 238 bus_space_write_1(localt, localh, WI_ACEX_COR_OFFSET, WI_COR_IOMODE); 239 sc->wi_cor_offset = WI_ACEX_COR_OFFSET; 240 241 /* Unmap registers we no longer need access to. */ 242 bus_space_unmap(commandt, commandh, commandsize); 243 244 return (0); 245 } 246 247 /* 248 * PLX 9052-based PCMCIA->PCI bridge attachment. 249 * 250 * These are often sold as "PCI wireless card adapters" and are 251 * sold by several vendors. Most are simply rebadged versions of the 252 * Eumitcom WL11000P or Global Sun Technology GL24110P02. 253 * These cards use the PLX 9052 dumb bridge chip to connect a PCMCIA 254 * wireless card to the PCI bus. Because it is a dumb bridge and 255 * not a true PCMCIA bridge, the PCMCIA subsystem is not involved 256 * (or even required). The PLX 9052 provides multiple PCI address 257 * space mappings. The primary mappings at PCI registers 0x10 (mem) 258 * and 0x14 (I/O) are for the PLX chip itself, *NOT* the PCMCIA card. 259 * The mem and I/O spaces for the PCMCIA card are mapped to 0x18 and 260 * 0x1C respectively. 261 * The PLX 9050/9052 datasheet may be downloaded from PLX at 262 * http://www.plxtech.com/products/toolbox/9050.htm 263 */ 264 int 265 wi_pci_plx_attach(struct pci_attach_args *pa, struct wi_softc *sc) 266 { 267 bus_space_handle_t localh, ioh, memh; 268 bus_space_tag_t localt; 269 bus_space_tag_t iot = pa->pa_iot; 270 bus_space_tag_t memt = pa->pa_memt; 271 bus_addr_t localbase; 272 bus_size_t localsize, memsize, iosize; 273 u_int32_t intcsr; 274 275 if (pci_mapreg_map(pa, WI_PLX_MEMRES, PCI_MAPREG_TYPE_MEM, 0, 276 &memt, &memh, NULL, &memsize, 0) != 0) { 277 printf(": can't map mem space\n"); 278 return (ENXIO); 279 } 280 sc->wi_ltag = memt; 281 sc->wi_lhandle = memh; 282 283 if (pci_mapreg_map(pa, WI_PLX_IORES, 284 PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, NULL, &iosize, 0) != 0) { 285 printf(": can't map I/O space\n"); 286 bus_space_unmap(memt, memh, memsize); 287 return (ENXIO); 288 } 289 sc->wi_btag = iot; 290 sc->wi_bhandle = ioh; 291 292 /* 293 * Some cards, such as the PLX version of the NDC NCP130, 294 * don't have the PLX local registers mapped. In general 295 * this is OK since on those cards the serial EEPROM has 296 * already set things up for us. 297 * As such, we don't consider an error here to be fatal. 298 */ 299 localsize = 0; 300 if (pci_mapreg_type(pa->pa_pc, pa->pa_tag, WI_PLX_LOCALRES) 301 == PCI_MAPREG_TYPE_IO) { 302 if (pci_io_find(pa->pa_pc, pa->pa_tag, 303 WI_PLX_LOCALRES, &localbase, &localsize) != 0) 304 printf(": can't find PLX I/O space\n"); 305 if (localsize != 0) { 306 if (bus_space_map(pa->pa_iot, localbase, 307 localsize, 0, &localh) != 0) { 308 printf(": can't map PLX I/O space\n"); 309 localsize = 0; 310 } else 311 localt = pa->pa_iot; 312 } 313 } 314 315 if (wi_pci_common_attach(pa, sc) != 0) { 316 if (localsize) 317 bus_space_unmap(localt, localh, localsize); 318 bus_space_unmap(iot, ioh, iosize); 319 bus_space_unmap(memt, memh, memsize); 320 return (ENXIO); 321 } 322 323 if (localsize != 0) { 324 intcsr = bus_space_read_4(localt, localh, 325 WI_PLX_INTCSR); 326 327 /* 328 * The Netgear MA301 has local interrupt 1 active 329 * when there is no card in the adapter. We bail 330 * early in this case since our attempt to check 331 * for the presence of a card later will hang the 332 * MA301. 333 */ 334 if (intcsr & WI_PLX_LINT1STAT) { 335 printf("\n%s: no PCMCIA card detected in bridge card\n", 336 WI_PRT_ARG(sc)); 337 pci_intr_disestablish(pa->pa_pc, sc->sc_ih); 338 if (localsize) 339 bus_space_unmap(localt, localh, localsize); 340 bus_space_unmap(iot, ioh, iosize); 341 bus_space_unmap(memt, memh, memsize); 342 return (ENXIO); 343 } 344 345 /* 346 * Enable PCI interrupts on the PLX chip if they are 347 * not already enabled. On most adapters the serial 348 * EEPROM has done this for us but some (such as 349 * the Netgear MA301) do not. 350 */ 351 if (!(intcsr & WI_PLX_INTEN)) { 352 intcsr |= WI_PLX_INTEN; 353 bus_space_write_4(localt, localh, WI_PLX_INTCSR, 354 intcsr); 355 } 356 } 357 358 /* 359 * Enable I/O mode and level interrupts on the PCMCIA card. 360 * The PCMCIA card's COR is the first byte after the CIS. 361 */ 362 bus_space_write_1(memt, memh, WI_PLX_COR_OFFSET, WI_COR_IOMODE); 363 sc->wi_cor_offset = WI_PLX_COR_OFFSET; 364 365 if (localsize != 0) { 366 /* 367 * Test the presence of a wi(4) card by writing 368 * a magic number to the first software support 369 * register and then reading it back. 370 */ 371 CSR_WRITE_2(sc, WI_SW0, WI_DRVR_MAGIC); 372 DELAY(1000); 373 if (CSR_READ_2(sc, WI_SW0) != WI_DRVR_MAGIC) { 374 printf("\n%s: no PCMCIA card detected in bridge card\n", 375 WI_PRT_ARG(sc)); 376 pci_intr_disestablish(pa->pa_pc, sc->sc_ih); 377 if (localsize) 378 bus_space_unmap(localt, localh, localsize); 379 bus_space_unmap(iot, ioh, iosize); 380 bus_space_unmap(memt, memh, memsize); 381 return (ENXIO); 382 } 383 384 /* Unmap registers we no longer need access to. */ 385 bus_space_unmap(localt, localh, localsize); 386 387 /* Print PCMCIA card's CIS strings. */ 388 wi_pci_plx_print_cis(sc); 389 } 390 391 return (0); 392 } 393 394 /* 395 * TMD 7160-based PCMCIA->PCI bridge attachment. 396 * 397 * The TMD7160 dumb bridge chip is used on some versions of the 398 * NDC/Sohoware NCP130. The TMD7160 provides two PCI I/O registers. 399 * The first, at 0x14, maps to the Prism2 COR. 400 * The second, at 0x18, is for the Prism2 chip itself. 401 * 402 * The datasheet for the TMD7160 does not seem to be publicly available. 403 * Details for this attachment were gleaned from a version of the 404 * Linux WLAN driver modified by NDC. 405 */ 406 int 407 wi_pci_tmd_attach(struct pci_attach_args *pa, struct wi_softc *sc) 408 { 409 bus_space_handle_t localh, ioh; 410 bus_space_tag_t localt; 411 bus_space_tag_t iot = pa->pa_iot; 412 bus_size_t localsize, iosize; 413 414 if (pci_mapreg_map(pa, WI_TMD_LOCALRES, PCI_MAPREG_TYPE_IO, 415 0, &localt, &localh, NULL, &localsize, 0) != 0) { 416 printf(": can't map TMD I/O space\n"); 417 return (ENXIO); 418 } 419 sc->wi_ltag = localt; 420 sc->wi_lhandle = localh; 421 422 if (pci_mapreg_map(pa, WI_TMD_IORES, PCI_MAPREG_TYPE_IO, 423 0, &iot, &ioh, NULL, &iosize, 0) != 0) { 424 printf(": can't map I/O space\n"); 425 bus_space_unmap(localt, localh, localsize); 426 return (ENXIO); 427 } 428 sc->wi_btag = iot; 429 sc->wi_bhandle = ioh; 430 431 if (wi_pci_common_attach(pa, sc) != 0) { 432 bus_space_unmap(iot, ioh, iosize); 433 bus_space_unmap(localt, localh, localsize); 434 return (ENXIO); 435 } 436 437 /* 438 * Enable I/O mode and level interrupts on the embedded PCMCIA 439 * card. The PCMCIA card's COR is the first byte of BAR 0. 440 */ 441 bus_space_write_1(localt, localh, 0, WI_COR_IOMODE); 442 sc->wi_cor_offset = 0; 443 444 return (0); 445 } 446 447 int 448 wi_pci_native_attach(struct pci_attach_args *pa, struct wi_softc *sc) 449 { 450 bus_space_handle_t ioh; 451 bus_space_tag_t iot = pa->pa_iot; 452 bus_size_t iosize; 453 454 if (pci_mapreg_map(pa, WI_PCI_CBMA, PCI_MAPREG_TYPE_MEM, 455 0, &iot, &ioh, NULL, &iosize, 0) != 0) { 456 printf(": can't map mem space\n"); 457 return (ENXIO); 458 } 459 sc->wi_ltag = iot; 460 sc->wi_lhandle = ioh; 461 sc->wi_btag = iot; 462 sc->wi_bhandle = ioh; 463 sc->sc_pci = 1; 464 465 if (wi_pci_common_attach(pa, sc) != 0) { 466 bus_space_unmap(iot, ioh, iosize); 467 return (ENXIO); 468 } 469 470 /* Do a soft reset of the HFA3842 MAC core */ 471 bus_space_write_2(iot, ioh, WI_PCI_COR_OFFSET, WI_COR_SOFT_RESET); 472 DELAY(100*1000); /* 100 m sec */ 473 bus_space_write_2(iot, ioh, WI_PCI_COR_OFFSET, WI_COR_CLEAR); 474 DELAY(100*1000); /* 100 m sec */ 475 sc->wi_cor_offset = WI_PCI_COR_OFFSET; 476 477 return (0); 478 } 479 480 int 481 wi_pci_common_attach(struct pci_attach_args *pa, struct wi_softc *sc) 482 { 483 pci_intr_handle_t ih; 484 pci_chipset_tag_t pc = pa->pa_pc; 485 const char *intrstr; 486 487 /* Make sure interrupts are disabled. */ 488 CSR_WRITE_2(sc, WI_INT_EN, 0); 489 CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF); 490 491 /* Map and establish the interrupt. */ 492 if (pci_intr_map(pa, &ih)) { 493 printf(": couldn't map interrupt\n"); 494 return (ENXIO); 495 } 496 intrstr = pci_intr_string(pc, ih); 497 sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc, 498 sc->sc_dev.dv_xname); 499 if (sc->sc_ih == NULL) { 500 printf(": couldn't establish interrupt"); 501 if (intrstr != NULL) 502 printf(" at %s", intrstr); 503 printf("\n"); 504 return (ENXIO); 505 } 506 printf(": %s", intrstr); 507 508 return (0); 509 } 510 511 void 512 wi_pci_plx_print_cis(struct wi_softc *sc) 513 { 514 int i, stringno; 515 char cisbuf[CIS_INFO_SIZE]; 516 char *cis_strings[3]; 517 u_int8_t value; 518 const u_int8_t cis_magic[] = { 519 0x01, 0x03, 0x00, 0x00, 0xff, 0x17, 0x04, 0x67 520 }; 521 522 /* Make sure the CIS data is valid. */ 523 for (i = 0; i < 8; i++) { 524 value = bus_space_read_1(sc->wi_ltag, sc->wi_lhandle, i * 2); 525 if (value != cis_magic[i]) 526 return; 527 } 528 529 cis_strings[0] = cisbuf; 530 stringno = 0; 531 for (i = 0; i < CIS_INFO_SIZE && stringno < 3; i++) { 532 cisbuf[i] = bus_space_read_1(sc->wi_ltag, 533 sc->wi_lhandle, (CIS_MFG_NAME_OFFSET + i) * 2); 534 if (cisbuf[i] == '\0' && ++stringno < 3) 535 cis_strings[stringno] = &cisbuf[i + 1]; 536 } 537 cisbuf[CIS_INFO_SIZE - 1] = '\0'; 538 printf("\n%s: \"%s, %s, %s\"", WI_PRT_ARG(sc), 539 cis_strings[0], cis_strings[1], cis_strings[2]); 540 } 541