1 /* $NetBSD: cardbus.c,v 1.116 2025/10/04 04:48:49 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 1998, 1999 and 2000 5 * HAYAKAWA Koichi. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: cardbus.c,v 1.116 2025/10/04 04:48:49 thorpej Exp $"); 31 32 #include "opt_cardbus.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/device.h> 37 #include <sys/malloc.h> 38 #include <sys/kernel.h> 39 #include <sys/syslog.h> 40 #include <sys/proc.h> 41 #include <sys/reboot.h> /* for AB_* needed by bootverbose */ 42 43 #include <sys/bus.h> 44 45 #include <dev/cardbus/cardbusvar.h> 46 #include <dev/pci/pcidevs.h> 47 48 #include <dev/cardbus/cardbus_exrom.h> 49 50 #include <dev/pci/pcivar.h> /* XXX */ 51 #include <dev/pci/pcireg.h> /* XXX */ 52 53 #include <dev/pcmcia/pcmciareg.h> 54 55 #include "locators.h" 56 57 #if defined CARDBUS_DEBUG 58 #define STATIC 59 #define DPRINTF(a) printf a 60 #else 61 #define STATIC static 62 #define DPRINTF(a) 63 #endif 64 65 66 STATIC void cardbusattach(device_t, device_t, void *); 67 STATIC int cardbusdetach(device_t, int); 68 STATIC int cardbusmatch(device_t, cfdata_t, void *); 69 int cardbus_rescan(device_t, const char *, const int *); 70 void cardbus_childdetached(device_t, device_t); 71 static int cardbusprint(void *, const char *); 72 73 typedef void (*tuple_decode_func)(u_int8_t*, int, void*); 74 75 static int decode_tuples(u_int8_t *, int, tuple_decode_func, void*); 76 #ifdef CARDBUS_DEBUG 77 static void print_tuple(u_int8_t*, int, void*); 78 #endif 79 80 static int cardbus_read_tuples(struct cardbus_attach_args *, 81 pcireg_t, u_int8_t *, size_t); 82 83 static void enable_function(struct cardbus_softc *, int, int); 84 static void disable_function(struct cardbus_softc *, int); 85 86 static void cardbus_child_register(device_t); 87 88 CFATTACH_DECL3_NEW(cardbus, sizeof(struct cardbus_softc), 89 cardbusmatch, cardbusattach, cardbusdetach, NULL, 90 cardbus_rescan, cardbus_childdetached, DVF_DETACH_SHUTDOWN); 91 92 #ifndef __NetBSD_Version__ 93 struct cfdriver cardbus_cd = { 94 NULL, "cardbus", DV_DULL 95 }; 96 #endif 97 98 99 STATIC int 100 cardbusmatch(device_t parent, cfdata_t cf, void *aux) 101 { 102 103 return (1); 104 } 105 106 STATIC void 107 cardbusattach(device_t parent, device_t self, void *aux) 108 { 109 struct cardbus_softc *sc = device_private(self); 110 struct cbslot_attach_args *cba = aux; 111 112 sc->sc_dev = self; 113 114 sc->sc_bus = cba->cba_bus; 115 sc->sc_cacheline = cba->cba_cacheline; 116 sc->sc_max_lattimer = MIN(0xf8, cba->cba_max_lattimer); 117 118 aprint_naive("\n"); 119 aprint_normal(": bus %d", sc->sc_bus); 120 if (bootverbose) 121 aprint_normal(" cacheline 0x%x, lattimer 0x%x", 122 sc->sc_cacheline, sc->sc_max_lattimer); 123 aprint_normal("\n"); 124 125 sc->sc_iot = cba->cba_iot; /* CardBus I/O space tag */ 126 sc->sc_memt = cba->cba_memt; /* CardBus MEM space tag */ 127 sc->sc_dmat = cba->cba_dmat; /* DMA tag */ 128 sc->sc_cc = cba->cba_cc; 129 sc->sc_cf = cba->cba_cf; 130 131 sc->sc_rbus_iot = cba->cba_rbus_iot; 132 sc->sc_rbus_memt = cba->cba_rbus_memt; 133 134 if (!pmf_device_register(self, NULL, NULL)) 135 aprint_error_dev(self, "couldn't establish power handler\n"); 136 } 137 138 STATIC int 139 cardbusdetach(device_t self, int flags) 140 { 141 int rc; 142 143 if ((rc = config_detach_children(self, flags)) != 0) 144 return rc; 145 146 pmf_device_deregister(self); 147 return 0; 148 } 149 150 static int 151 cardbus_read_tuples(struct cardbus_attach_args *ca, pcireg_t cis_ptr, 152 u_int8_t *tuples, size_t len) 153 { 154 struct cardbus_softc *sc = ca->ca_ct->ct_sc; 155 cardbus_chipset_tag_t cc = ca->ca_ct->ct_cc; 156 cardbus_function_tag_t cf = ca->ca_ct->ct_cf; 157 pcitag_t tag = ca->ca_tag; 158 pcireg_t command; 159 bus_space_tag_t bar_tag; 160 bus_space_handle_t bar_memh; 161 bus_size_t bar_size; 162 bus_addr_t bar_addr; 163 pcireg_t reg; 164 int found = 0; 165 int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK; 166 size_t mlen, n, tlen; 167 int i, j; 168 169 memset(tuples, 0, len); 170 171 cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK; 172 173 switch (cardbus_space) { 174 case CARDBUS_CIS_ASI_TUPLE: 175 DPRINTF(("%s: reading CIS data from configuration space\n", 176 device_xname(sc->sc_dev))); 177 for (i = cis_ptr, j = 0; i < 0xff; i += 4) { 178 u_int32_t e = (*cf->cardbus_conf_read)(cc, tag, i); 179 tuples[j] = 0xff & e; 180 e >>= 8; 181 tuples[j + 1] = 0xff & e; 182 e >>= 8; 183 tuples[j + 2] = 0xff & e; 184 e >>= 8; 185 tuples[j + 3] = 0xff & e; 186 j += 4; 187 } 188 found++; 189 break; 190 191 case CARDBUS_CIS_ASI_BAR0: 192 case CARDBUS_CIS_ASI_BAR1: 193 case CARDBUS_CIS_ASI_BAR2: 194 case CARDBUS_CIS_ASI_BAR3: 195 case CARDBUS_CIS_ASI_BAR4: 196 case CARDBUS_CIS_ASI_BAR5: 197 case CARDBUS_CIS_ASI_ROM: 198 if (cardbus_space == CARDBUS_CIS_ASI_ROM) { 199 reg = CARDBUS_ROM_REG; 200 DPRINTF(("%s: reading CIS data from ROM\n", 201 device_xname(sc->sc_dev))); 202 } else { 203 reg = CARDBUS_CIS_ASI_BAR(cardbus_space); 204 DPRINTF(("%s: reading CIS data from BAR%d\n", 205 device_xname(sc->sc_dev), cardbus_space - 1)); 206 } 207 208 /* 209 * XXX zero register so mapreg_map doesn't get confused by old 210 * contents. 211 */ 212 cardbus_conf_write(cc, cf, tag, reg, 0); 213 if (Cardbus_mapreg_map(ca->ca_ct, reg, 214 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 215 0, &bar_tag, &bar_memh, &bar_addr, &bar_size)) { 216 aprint_error_dev(sc->sc_dev, "failed to map memory\n"); 217 return (1); 218 } 219 aprint_debug_dev(sc->sc_dev, "mapped %ju bytes at 0x%jx\n", 220 (uintmax_t)bar_size, (uintmax_t)bar_addr); 221 222 if (cardbus_space == CARDBUS_CIS_ASI_ROM) { 223 pcireg_t exrom; 224 int save; 225 struct cardbus_rom_image_head rom_image; 226 struct cardbus_rom_image *p; 227 228 save = splhigh(); 229 /* enable rom address decoder */ 230 exrom = cardbus_conf_read(cc, cf, tag, reg); 231 cardbus_conf_write(cc, cf, tag, reg, exrom | 1); 232 233 command = cardbus_conf_read(cc, cf, tag, 234 PCI_COMMAND_STATUS_REG); 235 cardbus_conf_write(cc, cf, tag, 236 PCI_COMMAND_STATUS_REG, 237 command | PCI_COMMAND_MEM_ENABLE); 238 239 if (cardbus_read_exrom(bar_tag, bar_memh, &rom_image)) 240 goto out; 241 242 SIMPLEQ_FOREACH(p, &rom_image, next) { 243 if (p->rom_image == 244 CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) { 245 bus_space_read_region_1(p->romt, 246 p->romh, CARDBUS_CIS_ADDR(cis_ptr), 247 tuples, MIN(p->image_size, len)); 248 found++; 249 break; 250 } 251 } 252 while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) { 253 SIMPLEQ_REMOVE_HEAD(&rom_image, next); 254 free(p, M_DEVBUF); 255 } 256 out: 257 exrom = cardbus_conf_read(cc, cf, tag, reg); 258 cardbus_conf_write(cc, cf, tag, reg, exrom & ~1); 259 splx(save); 260 } else { 261 command = cardbus_conf_read(cc, cf, tag, 262 PCI_COMMAND_STATUS_REG); 263 cardbus_conf_write(cc, cf, tag, 264 PCI_COMMAND_STATUS_REG, 265 command | PCI_COMMAND_MEM_ENABLE); 266 267 mlen = MIN(bar_size - MIN(bar_size, cis_ptr), len); 268 for (n = 0; n < mlen; ) { 269 tuples[n] = bus_space_read_1(bar_tag, bar_memh, 270 cis_ptr+n); 271 if (tuples[n] == PCMCIA_CISTPL_END) 272 break; 273 if (tuples[n] == PCMCIA_CISTPL_NULL) { 274 n++; 275 continue; 276 } 277 n++; 278 tuples[n] = bus_space_read_1(bar_tag, bar_memh, 279 cis_ptr+n); 280 tlen = tuples[n]; 281 n++; 282 if (n+tlen >= mlen) 283 break; 284 bus_space_read_region_1(bar_tag, bar_memh, 285 cis_ptr+n, tuples+n, tlen); 286 n += tlen; 287 } 288 found++; 289 } 290 command = cardbus_conf_read(cc, cf, tag, 291 PCI_COMMAND_STATUS_REG); 292 cardbus_conf_write(cc, cf, tag, PCI_COMMAND_STATUS_REG, 293 command & ~PCI_COMMAND_MEM_ENABLE); 294 cardbus_conf_write(cc, cf, tag, reg, 0); 295 296 Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh, 297 bar_size); 298 break; 299 300 #ifdef DIAGNOSTIC 301 default: 302 panic("%s: bad CIS space (%d)", device_xname(sc->sc_dev), 303 cardbus_space); 304 #endif 305 } 306 return (!found); 307 } 308 309 static void 310 parse_tuple(u_int8_t *tuple, int len, void *data) 311 { 312 struct cardbus_cis_info *cis = data; 313 char *p; 314 int i, bar_index; 315 316 switch (tuple[0]) { 317 case PCMCIA_CISTPL_MANFID: 318 if (tuple[1] != 4) { 319 DPRINTF(("%s: wrong length manufacturer id (%d)\n", 320 __func__, tuple[1])); 321 break; 322 } 323 cis->manufacturer = tuple[2] | (tuple[3] << 8); 324 cis->product = tuple[4] | (tuple[5] << 8); 325 break; 326 327 case PCMCIA_CISTPL_VERS_1: 328 memcpy(cis->cis1_info_buf, tuple + 2, tuple[1]); 329 i = 0; 330 p = cis->cis1_info_buf + 2; 331 while (i < 332 sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) { 333 if (p >= cis->cis1_info_buf + tuple[1] || *p == '\xff') 334 break; 335 cis->cis1_info[i++] = p; 336 while (*p != '\0' && *p != '\xff') 337 p++; 338 if (*p == '\0') 339 p++; 340 } 341 break; 342 343 case PCMCIA_CISTPL_BAR: 344 if (tuple[1] != 6) { 345 DPRINTF(("%s: BAR with short length (%d)\n", 346 __func__, tuple[1])); 347 break; 348 } 349 bar_index = tuple[2] & 7; 350 if (bar_index == 0) { 351 DPRINTF(("%s: invalid ASI in BAR tuple\n", __func__)); 352 break; 353 } 354 bar_index--; 355 cis->bar[bar_index].flags = tuple[2]; 356 cis->bar[bar_index].size = 357 (tuple[4] << 0) | 358 (tuple[5] << 8) | 359 (tuple[6] << 16) | 360 (tuple[7] << 24); 361 break; 362 363 case PCMCIA_CISTPL_FUNCID: 364 cis->funcid = tuple[2]; 365 break; 366 367 case PCMCIA_CISTPL_FUNCE: 368 switch (cis->funcid) { 369 case PCMCIA_FUNCTION_SERIAL: 370 if (tuple[1] >= 2 && 371 /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */ 372 tuple[2] == 0) { 373 cis->funce.serial.uart_type = tuple[3] & 0x1f; 374 cis->funce.serial.uart_present = 1; 375 } 376 break; 377 378 case PCMCIA_FUNCTION_NETWORK: 379 if (tuple[1] >= 8 && 380 tuple[2] == PCMCIA_TPLFE_TYPE_LAN_NID) { 381 if (tuple[3] > 382 sizeof(cis->funce.network.netid)) { 383 DPRINTF(("%s: unknown network id type " 384 "(len = %d)\n", 385 __func__, tuple[3])); 386 } else { 387 cis->funce.network.netid_present = 1; 388 memcpy(cis->funce.network.netid, 389 tuple + 4, tuple[3]); 390 } 391 } 392 break; 393 } 394 break; 395 } 396 } 397 398 /* 399 * int cardbus_attach_card(struct cardbus_softc *sc) 400 * 401 * This function attaches the card on the slot: turns on power, 402 * reads and analyses tuple, sets configuration index. 403 * 404 * This function returns the number of recognised device functions. 405 * If no functions are recognised, return 0. 406 */ 407 int 408 cardbus_attach_card(struct cardbus_softc *sc) 409 { 410 cardbus_chipset_tag_t cc; 411 cardbus_function_tag_t cf; 412 int cdstatus; 413 static int wildcard[CARDBUSCF_NLOCS] = { 414 CARDBUSCF_FUNCTION_DEFAULT 415 }; 416 417 cc = sc->sc_cc; 418 cf = sc->sc_cf; 419 420 DPRINTF(("cardbus_attach_card: cb%d start\n", 421 device_unit(sc->sc_dev))); 422 423 /* inspect initial voltage */ 424 if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) { 425 DPRINTF(("%s: no CardBus card on cb%d\n", __func__, 426 device_unit(sc->sc_dev))); 427 return (0); 428 } 429 430 device_pmf_driver_set_child_register(sc->sc_dev, cardbus_child_register); 431 cardbus_rescan(sc->sc_dev, "cardbus", wildcard); 432 return (1); /* XXX */ 433 } 434 435 int 436 cardbus_rescan(device_t self, const char *ifattr, 437 const int *locators) 438 { 439 struct cardbus_softc *sc = device_private(self); 440 cardbus_chipset_tag_t cc; 441 cardbus_function_tag_t cf; 442 pcitag_t tag; 443 pcireg_t id, class, cis_ptr; 444 pcireg_t bhlc, icr, lattimer; 445 int cdstatus; 446 int function, nfunction; 447 device_t csc; 448 cardbus_devfunc_t ct; 449 450 cc = sc->sc_cc; 451 cf = sc->sc_cf; 452 453 /* inspect initial voltage */ 454 if ((cdstatus = (*cf->cardbus_ctrl)(cc, CARDBUS_CD)) == 0) { 455 DPRINTF(("%s: no CardBus card on cb%d\n", __func__, 456 device_unit(sc->sc_dev))); 457 return (0); 458 } 459 460 /* 461 * XXX use fake function 8 to keep power on during whole 462 * configuration. 463 */ 464 enable_function(sc, cdstatus, 8); 465 function = 0; 466 467 tag = cardbus_make_tag(cc, cf, sc->sc_bus, function); 468 469 /* 470 * Wait until power comes up. Maximum 500 ms. 471 * 472 * XXX What is this for? The bridge driver ought to have waited 473 * XXX already. 474 */ 475 { 476 int i; 477 478 for (i = 0; i < 5; ++i) { 479 id = cardbus_conf_read(cc, cf, tag, PCI_ID_REG); 480 if (id != 0xffffffff && id != 0) { 481 break; 482 } 483 if (cold) { /* before kernel thread invoked */ 484 delay(100 * 1000); 485 } else { /* thread context */ 486 if (tsleep((void *)sc, PCATCH, "cardbus", 487 hz / 10) != EWOULDBLOCK) { 488 break; 489 } 490 } 491 } 492 aprint_debug_dev(self, "id reg valid in %d iterations\n", i); 493 if (i == 5) { 494 return (EIO); 495 } 496 } 497 498 bhlc = cardbus_conf_read(cc, cf, tag, PCI_BHLC_REG); 499 DPRINTF(("%s bhlc 0x%08x -> ", device_xname(sc->sc_dev), bhlc)); 500 nfunction = PCI_HDRTYPE_MULTIFN(bhlc) ? 8 : 1; 501 502 for (function = 0; function < nfunction; function++) { 503 struct cardbus_attach_args ca; 504 int locs[CARDBUSCF_NLOCS]; 505 506 if (locators[CARDBUSCF_FUNCTION] != 507 CARDBUSCF_FUNCTION_DEFAULT && 508 locators[CARDBUSCF_FUNCTION] != function) 509 continue; 510 511 if (sc->sc_funcs[function]) 512 continue; 513 514 tag = cardbus_make_tag(cc, cf, sc->sc_bus, function); 515 516 id = cardbus_conf_read(cc, cf, tag, PCI_ID_REG); 517 class = cardbus_conf_read(cc, cf, tag, PCI_CLASS_REG); 518 cis_ptr = cardbus_conf_read(cc, cf, tag, CARDBUS_CIS_REG); 519 520 /* Invalid vendor ID value? */ 521 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID) { 522 continue; 523 } 524 525 DPRINTF(("cardbus_attach_card: " 526 "Vendor 0x%x, Product 0x%x, CIS 0x%x\n", 527 PCI_VENDOR(id), PCI_PRODUCT(id), cis_ptr)); 528 529 enable_function(sc, cdstatus, function); 530 531 /* clean up every BAR */ 532 cardbus_conf_write(cc, cf, tag, PCI_BAR0, 0); 533 cardbus_conf_write(cc, cf, tag, PCI_BAR1, 0); 534 cardbus_conf_write(cc, cf, tag, PCI_BAR2, 0); 535 cardbus_conf_write(cc, cf, tag, PCI_BAR3, 0); 536 cardbus_conf_write(cc, cf, tag, PCI_BAR4, 0); 537 cardbus_conf_write(cc, cf, tag, PCI_BAR5, 0); 538 cardbus_conf_write(cc, cf, tag, CARDBUS_ROM_REG, 0); 539 540 /* set initial latency and cacheline size */ 541 bhlc = cardbus_conf_read(cc, cf, tag, PCI_BHLC_REG); 542 icr = cardbus_conf_read(cc, cf, tag, PCI_INTERRUPT_REG); 543 DPRINTF(("%s func%d icr 0x%08x bhlc 0x%08x -> ", 544 device_xname(sc->sc_dev), function, icr, bhlc)); 545 bhlc &= ~(PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT); 546 bhlc |= (sc->sc_cacheline & PCI_CACHELINE_MASK) << 547 PCI_CACHELINE_SHIFT; 548 /* 549 * Set the initial value of the Latency Timer. 550 * 551 * While a PCI device owns the bus, its Latency 552 * Timer counts down bus cycles from its initial 553 * value to 0. Minimum Grant tells for how long 554 * the device wants to own the bus once it gets 555 * access, in units of 250ns. 556 * 557 * On a 33 MHz bus, there are 8 cycles per 250ns. 558 * So I multiply the Minimum Grant by 8 to find 559 * out the initial value of the Latency Timer. 560 * 561 * Avoid setting a Latency Timer less than 0x10, 562 * since the old code did not do that. 563 */ 564 lattimer = 565 MIN(sc->sc_max_lattimer, MAX(0x10, 8 * PCI_MIN_GNT(icr))); 566 if (PCI_LATTIMER(bhlc) < lattimer) { 567 bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT); 568 bhlc |= (lattimer << PCI_LATTIMER_SHIFT); 569 } 570 571 cardbus_conf_write(cc, cf, tag, PCI_BHLC_REG, bhlc); 572 bhlc = cardbus_conf_read(cc, cf, tag, PCI_BHLC_REG); 573 DPRINTF(("0x%08x\n", bhlc)); 574 575 /* 576 * We need to allocate the ct here, since we might 577 * need it when reading the CIS 578 */ 579 ct = malloc(sizeof(struct cardbus_devfunc), 580 M_DEVBUF, M_WAITOK); 581 ct->ct_bhlc = bhlc; 582 ct->ct_cc = sc->sc_cc; 583 ct->ct_cf = sc->sc_cf; 584 ct->ct_bus = sc->sc_bus; 585 ct->ct_func = function; 586 ct->ct_sc = sc; 587 sc->sc_funcs[function] = ct; 588 589 memset(&ca, 0, sizeof(ca)); 590 591 ca.ca_ct = ct; 592 593 ca.ca_iot = sc->sc_iot; 594 ca.ca_memt = sc->sc_memt; 595 ca.ca_dmat = sc->sc_dmat; 596 597 ca.ca_rbus_iot = sc->sc_rbus_iot; 598 ca.ca_rbus_memt= sc->sc_rbus_memt; 599 600 ca.ca_tag = tag; 601 ca.ca_bus = sc->sc_bus; 602 ca.ca_function = function; 603 ca.ca_id = id; 604 ca.ca_class = class; 605 606 if (cis_ptr != 0) { 607 #define TUPLESIZE 2048 608 u_int8_t *tuple = malloc(TUPLESIZE, M_DEVBUF, M_WAITOK); 609 if (cardbus_read_tuples(&ca, cis_ptr, 610 tuple, TUPLESIZE)) { 611 printf("cardbus_attach_card: " 612 "failed to read CIS\n"); 613 } else { 614 #ifdef CARDBUS_DEBUG 615 decode_tuples(tuple, TUPLESIZE, 616 print_tuple, NULL); 617 #endif 618 decode_tuples(tuple, TUPLESIZE, 619 parse_tuple, &ca.ca_cis); 620 } 621 free(tuple, M_DEVBUF); 622 } 623 624 locs[CARDBUSCF_FUNCTION] = function; 625 626 if ((csc = config_found(sc->sc_dev, &ca, cardbusprint, 627 CFARGS(.submatch = config_stdsubmatch, 628 .locators = locs))) == NULL) { 629 /* do not match */ 630 disable_function(sc, function); 631 sc->sc_funcs[function] = NULL; 632 free(ct, M_DEVBUF); 633 } else { 634 /* found */ 635 ct->ct_device = csc; 636 } 637 } 638 /* 639 * XXX power down pseudo function 8 (this will power down the card 640 * if no functions were attached). 641 */ 642 disable_function(sc, 8); 643 644 return (0); 645 } 646 647 static int 648 cardbusprint(void *aux, const char *pnp) 649 { 650 struct cardbus_attach_args *ca = aux; 651 char devinfo[256]; 652 int i; 653 654 if (pnp) { 655 pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo, 656 sizeof(devinfo)); 657 for (i = 0; i < 4; i++) { 658 if (ca->ca_cis.cis1_info[i] == NULL) 659 break; 660 if (i) 661 aprint_normal(", "); 662 aprint_normal("%s", ca->ca_cis.cis1_info[i]); 663 } 664 aprint_verbose("%s(manufacturer 0x%x, product 0x%x)", 665 i ? " " : "", 666 ca->ca_cis.manufacturer, ca->ca_cis.product); 667 aprint_normal(" %s at %s", devinfo, pnp); 668 } 669 aprint_normal(" function %d", ca->ca_function); 670 671 return (UNCONF); 672 } 673 674 /* 675 * void cardbus_detach_card(struct cardbus_softc *sc) 676 * 677 * This function detaches the card on the slot: detach device data 678 * structure and turns off the power. 679 * 680 * This function must not be called under interrupt context. 681 */ 682 void 683 cardbus_detach_card(struct cardbus_softc *sc) 684 { 685 int f; 686 struct cardbus_devfunc *ct; 687 688 for (f = 0; f < 8; f++) { 689 ct = sc->sc_funcs[f]; 690 if (!ct) 691 continue; 692 693 DPRINTF(("%s: detaching %s\n", device_xname(sc->sc_dev), 694 device_xname(ct->ct_device))); 695 /* call device detach function */ 696 697 if (config_detach(ct->ct_device, 0) != 0) { 698 aprint_error_dev(sc->sc_dev, 699 "cannot detach dev %s, function %d\n", 700 device_xname(ct->ct_device), ct->ct_func); 701 } 702 } 703 704 sc->sc_poweron_func = 0; 705 (*sc->sc_cf->cardbus_power)(sc->sc_cc, 706 CARDBUS_VCC_0V | CARDBUS_VPP_0V); 707 } 708 709 void 710 cardbus_childdetached(device_t self, device_t child) 711 { 712 struct cardbus_softc *sc = device_private(self); 713 struct cardbus_devfunc *ct; 714 715 ct = sc->sc_funcs[device_locator(child, CARDBUSCF_FUNCTION)]; 716 KASSERT(ct->ct_device == child); 717 718 sc->sc_poweron_func &= ~(1 << ct->ct_func); 719 sc->sc_funcs[ct->ct_func] = NULL; 720 free(ct, M_DEVBUF); 721 } 722 723 void * 724 Cardbus_intr_establish(cardbus_devfunc_t ct, 725 int level, int (*func)(void *), void *arg) 726 { 727 return cardbus_intr_establish(ct->ct_cc, ct->ct_cf, level, func, 728 arg); 729 } 730 731 /* 732 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg) 733 * Interrupt handler of pccard. 734 * args: 735 * cardbus_chipset_tag_t *cc 736 * int irq: 737 */ 738 void * 739 cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf, 740 int level, int (*func)(void *), void *arg) 741 { 742 743 DPRINTF(("- cardbus_intr_establish\n")); 744 return ((*cf->cardbus_intr_establish)(cc, level, func, arg)); 745 } 746 747 void 748 Cardbus_intr_disestablish(cardbus_devfunc_t ct, void *handler) 749 { 750 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, handler); 751 } 752 753 /* 754 * void cardbus_intr_disestablish(cc, cf, handler) 755 * Interrupt handler of pccard. 756 * args: 757 * cardbus_chipset_tag_t *cc 758 */ 759 void 760 cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf, 761 void *handler) 762 { 763 764 DPRINTF(("- pccard_intr_disestablish\n")); 765 (*cf->cardbus_intr_disestablish)(cc, handler); 766 } 767 768 /* 769 * XXX this should be merged with cardbus_function_{enable,disable}, 770 * but we don't have a ct when these functions are called. 771 */ 772 static void 773 enable_function(struct cardbus_softc *sc, int cdstatus, int function) 774 { 775 776 if (sc->sc_poweron_func == 0) { 777 /* switch to 3V and/or wait for power to stabilize */ 778 if (cdstatus & CARDBUS_3V_CARD) { 779 /* 780 * sc_poweron_func must be substituted before 781 * entering sleep, in order to avoid turn on 782 * power twice. 783 */ 784 sc->sc_poweron_func |= (1 << function); 785 (*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_3V); 786 } else { 787 /* No cards other than 3.3V cards. */ 788 return; 789 } 790 (*sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET); 791 } 792 sc->sc_poweron_func |= (1 << function); 793 } 794 795 static void 796 disable_function(struct cardbus_softc *sc, int function) 797 { 798 cardbus_devfunc_t ct; 799 device_t dv; 800 int i; 801 802 sc->sc_poweron_func &= ~(1 << function); 803 if (sc->sc_poweron_func != 0) 804 return; 805 for (i = 0; i < __arraycount(sc->sc_funcs); i++) { 806 if ((ct = sc->sc_funcs[i]) == NULL) 807 continue; 808 dv = ct->ct_device; 809 if (device_getprop_bool(dv, "pmf-no-powerdown")) 810 return; 811 } 812 /* power-off because no functions are enabled */ 813 (*sc->sc_cf->cardbus_power)(sc->sc_cc, CARDBUS_VCC_0V); 814 } 815 816 /* 817 * int cardbus_function_enable(struct cardbus_softc *sc, int func) 818 * 819 * This function enables a function on a card. When no power is 820 * applied on the card, power will be applied on it. 821 */ 822 int 823 cardbus_function_enable(struct cardbus_softc *sc, int func) 824 { 825 cardbus_chipset_tag_t cc = sc->sc_cc; 826 cardbus_function_tag_t cf = sc->sc_cf; 827 cardbus_devfunc_t ct; 828 pcireg_t command; 829 pcitag_t tag; 830 831 DPRINTF(("entering cardbus_function_enable... ")); 832 833 /* entering critical area */ 834 835 /* XXX: sc_vold should be used */ 836 enable_function(sc, CARDBUS_3V_CARD, func); 837 838 /* exiting critical area */ 839 840 tag = cardbus_make_tag(cc, cf, sc->sc_bus, func); 841 842 command = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG); 843 command |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_IO_ENABLE | 844 PCI_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */ 845 846 cardbus_conf_write(cc, cf, tag, PCI_COMMAND_STATUS_REG, command); 847 848 if ((ct = sc->sc_funcs[func]) != NULL) 849 Cardbus_conf_write(ct, tag, PCI_BHLC_REG, ct->ct_bhlc); 850 851 DPRINTF(("%x\n", sc->sc_poweron_func)); 852 853 return (0); 854 } 855 856 /* 857 * int cardbus_function_disable(struct cardbus_softc *, int func) 858 * 859 * This function disable a function on a card. When no functions are 860 * enabled, it turns off the power. 861 */ 862 int 863 cardbus_function_disable(struct cardbus_softc *sc, int func) 864 { 865 866 DPRINTF(("entering cardbus_function_disable... ")); 867 868 disable_function(sc, func); 869 870 return (0); 871 } 872 873 /* 874 * int cardbus_get_capability(cardbus_chipset_tag_t cc, 875 * cardbus_function_tag_t cf, pcitag_t tag, int capid, int *offset, 876 * pcireg_t *value) 877 * 878 * Find the specified PCI capability. 879 */ 880 int 881 cardbus_get_capability(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf, 882 pcitag_t tag, int capid, int *offset, pcireg_t *value) 883 { 884 pcireg_t reg; 885 unsigned int ofs; 886 887 reg = cardbus_conf_read(cc, cf, tag, PCI_COMMAND_STATUS_REG); 888 if (!(reg & PCI_STATUS_CAPLIST_SUPPORT)) 889 return (0); 890 891 ofs = PCI_CAPLIST_PTR(cardbus_conf_read(cc, cf, tag, 892 PCI_CAPLISTPTR_REG)); 893 while (ofs != 0) { 894 #ifdef DIAGNOSTIC 895 if ((ofs & 3) || (ofs < 0x40)) 896 panic("cardbus_get_capability"); 897 #endif 898 reg = cardbus_conf_read(cc, cf, tag, ofs); 899 if (PCI_CAPLIST_CAP(reg) == capid) { 900 if (offset) 901 *offset = ofs; 902 if (value) 903 *value = reg; 904 return (1); 905 } 906 ofs = PCI_CAPLIST_NEXT(reg); 907 } 908 909 return (0); 910 } 911 912 /* 913 * below this line, there are some functions for decoding tuples. 914 * They should go out from this file. 915 */ 916 917 static u_int8_t * 918 decode_tuple(u_int8_t *, u_int8_t *, tuple_decode_func, void *); 919 920 static int 921 decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data) 922 { 923 u_int8_t *tp = tuple; 924 925 if (PCMCIA_CISTPL_LINKTARGET != *tuple) { 926 DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple)); 927 return (0); 928 } 929 930 while ((tp = decode_tuple(tp, tuple + buflen, func, data)) != NULL) 931 ; 932 933 return (1); 934 } 935 936 static u_int8_t * 937 decode_tuple(u_int8_t *tuple, u_int8_t *end, 938 tuple_decode_func func, void *data) 939 { 940 u_int8_t type; 941 u_int8_t len; 942 943 type = tuple[0]; 944 switch (type) { 945 case PCMCIA_CISTPL_NULL: 946 case PCMCIA_CISTPL_END: 947 len = 1; 948 break; 949 default: 950 if (tuple + 2 > end) 951 return (NULL); 952 len = tuple[1] + 2; 953 break; 954 } 955 956 if (tuple + len > end) 957 return (NULL); 958 959 (*func)(tuple, len, data); 960 961 if (type == PCMCIA_CISTPL_END || tuple + len == end) 962 return (NULL); 963 964 return (tuple + len); 965 } 966 967 /* 968 * XXX: this is another reason why this code should be shared with PCI. 969 */ 970 static int 971 cardbus_get_powerstate_int(cardbus_devfunc_t ct, pcitag_t tag, 972 pcireg_t *state, int offset) 973 { 974 pcireg_t value, now; 975 cardbus_chipset_tag_t cc = ct->ct_cc; 976 cardbus_function_tag_t cf = ct->ct_cf; 977 978 value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR); 979 now = value & PCI_PMCSR_STATE_MASK; 980 switch (now) { 981 case PCI_PMCSR_STATE_D0: 982 case PCI_PMCSR_STATE_D1: 983 case PCI_PMCSR_STATE_D2: 984 case PCI_PMCSR_STATE_D3: 985 *state = now; 986 return 0; 987 default: 988 return EINVAL; 989 } 990 } 991 992 int 993 cardbus_get_powerstate(cardbus_devfunc_t ct, pcitag_t tag, pcireg_t *state) 994 { 995 cardbus_chipset_tag_t cc = ct->ct_cc; 996 cardbus_function_tag_t cf = ct->ct_cf; 997 int offset; 998 pcireg_t value; 999 1000 if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset, &value)) 1001 return EOPNOTSUPP; 1002 1003 return cardbus_get_powerstate_int(ct, tag, state, offset); 1004 } 1005 1006 static int 1007 cardbus_set_powerstate_int(cardbus_devfunc_t ct, pcitag_t tag, 1008 pcireg_t state, int offset, pcireg_t cap_reg) 1009 { 1010 cardbus_chipset_tag_t cc = ct->ct_cc; 1011 cardbus_function_tag_t cf = ct->ct_cf; 1012 1013 pcireg_t value, cap, now; 1014 1015 KASSERT((offset & 0x3) == 0); 1016 1017 cap = cap_reg >> PCI_PMCR_SHIFT; 1018 value = cardbus_conf_read(cc, cf, tag, offset + PCI_PMCSR); 1019 now = value & PCI_PMCSR_STATE_MASK; 1020 value &= ~PCI_PMCSR_STATE_MASK; 1021 1022 if (now == state) 1023 return 0; 1024 switch (state) { 1025 case PCI_PMCSR_STATE_D0: 1026 break; 1027 case PCI_PMCSR_STATE_D1: 1028 if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3) { 1029 printf("invalid transition from %d to D1\n", (int)now); 1030 return EINVAL; 1031 } 1032 if (!(cap & PCI_PMCR_D1SUPP)) { 1033 printf("D1 not supported\n"); 1034 return EOPNOTSUPP; 1035 } 1036 break; 1037 case PCI_PMCSR_STATE_D2: 1038 if (now == PCI_PMCSR_STATE_D3) { 1039 printf("invalid transition from %d to D2\n", (int)now); 1040 return EINVAL; 1041 } 1042 if (!(cap & PCI_PMCR_D2SUPP)) { 1043 printf("D2 not supported\n"); 1044 return EOPNOTSUPP; 1045 } 1046 break; 1047 case PCI_PMCSR_STATE_D3: 1048 break; 1049 default: 1050 return EINVAL; 1051 } 1052 value |= state; 1053 cardbus_conf_write(cc, cf, tag, offset + PCI_PMCSR, value); 1054 if (state == PCI_PMCSR_STATE_D3 || now == PCI_PMCSR_STATE_D3) 1055 DELAY(10000); 1056 else if (state == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D2) 1057 DELAY(200); 1058 1059 return 0; 1060 } 1061 1062 int 1063 cardbus_set_powerstate(cardbus_devfunc_t ct, pcitag_t tag, pcireg_t state) 1064 { 1065 cardbus_chipset_tag_t cc = ct->ct_cc; 1066 cardbus_function_tag_t cf = ct->ct_cf; 1067 int offset; 1068 pcireg_t value; 1069 1070 if (!cardbus_get_capability(cc, cf, tag, PCI_CAP_PWRMGMT, &offset, 1071 &value)) 1072 return EOPNOTSUPP; 1073 1074 return cardbus_set_powerstate_int(ct, tag, state, offset, value); 1075 } 1076 1077 #ifdef CARDBUS_DEBUG 1078 static const char *tuple_name(int); 1079 static const char *tuple_names[] = { 1080 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */ 1081 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */ 1082 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */ 1083 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */ 1084 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */ 1085 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A", 1086 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY", 1087 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A", 1088 "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */ 1089 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */ 1090 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */ 1091 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */ 1092 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */ 1093 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */ 1094 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */ 1095 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */ 1096 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER", 1097 "DATE", "BATTERY", "ORG" 1098 }; 1099 #define NAME_LEN(x) (sizeof x / sizeof(x[0])) 1100 1101 static const char * 1102 tuple_name(int type) 1103 { 1104 1105 if (0 <= type && type < NAME_LEN(tuple_names)) { 1106 return (tuple_names[type]); 1107 } else if (type == 0xff) { 1108 return ("END"); 1109 } else { 1110 return ("Reserved"); 1111 } 1112 } 1113 1114 static void 1115 print_tuple(u_int8_t *tuple, int len, void *data) 1116 { 1117 int i; 1118 1119 printf("tuple: %s len %d\n", tuple_name(tuple[0]), len); 1120 1121 for (i = 0; i < len; ++i) { 1122 if (i % 16 == 0) { 1123 printf(" 0x%2x:", i); 1124 } 1125 printf(" %x", tuple[i]); 1126 if (i % 16 == 15) { 1127 printf("\n"); 1128 } 1129 } 1130 if (i % 16 != 0) { 1131 printf("\n"); 1132 } 1133 } 1134 #endif 1135 1136 void 1137 cardbus_conf_capture(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf, 1138 pcitag_t tag, struct cardbus_conf_state *pcs) 1139 { 1140 int off; 1141 1142 for (off = 0; off < 16; off++) 1143 pcs->reg[off] = cardbus_conf_read(cc, cf, tag, (off * 4)); 1144 } 1145 1146 void 1147 cardbus_conf_restore(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf, 1148 pcitag_t tag, struct cardbus_conf_state *pcs) 1149 { 1150 int off; 1151 pcireg_t val; 1152 1153 for (off = 15; off >= 0; off--) { 1154 val = cardbus_conf_read(cc, cf, tag, (off * 4)); 1155 if (val != pcs->reg[off]) 1156 cardbus_conf_write(cc, cf,tag, (off * 4), pcs->reg[off]); 1157 } 1158 } 1159 1160 struct cardbus_child_power { 1161 struct cardbus_conf_state p_cardbusconf; 1162 cardbus_devfunc_t p_ct; 1163 pcitag_t p_tag; 1164 cardbus_chipset_tag_t p_cc; 1165 cardbus_function_tag_t p_cf; 1166 pcireg_t p_pm_cap; 1167 bool p_has_pm; 1168 int p_pm_offset; 1169 }; 1170 1171 static bool 1172 cardbus_child_suspend(device_t dv, const pmf_qual_t *qual) 1173 { 1174 struct cardbus_child_power *priv = device_pmf_bus_private(dv); 1175 1176 cardbus_conf_capture(priv->p_cc, priv->p_cf, priv->p_tag, 1177 &priv->p_cardbusconf); 1178 1179 if (priv->p_has_pm && 1180 cardbus_set_powerstate_int(priv->p_ct, priv->p_tag, 1181 PCI_PMCSR_STATE_D3, priv->p_pm_offset, priv->p_pm_cap)) { 1182 aprint_error_dev(dv, "unsupported state, continuing.\n"); 1183 return false; 1184 } 1185 1186 Cardbus_function_disable(priv->p_ct); 1187 1188 return true; 1189 } 1190 1191 static bool 1192 cardbus_child_resume(device_t dv, const pmf_qual_t *qual) 1193 { 1194 struct cardbus_child_power *priv = device_pmf_bus_private(dv); 1195 1196 Cardbus_function_enable(priv->p_ct); 1197 1198 if (priv->p_has_pm && 1199 cardbus_set_powerstate_int(priv->p_ct, priv->p_tag, 1200 PCI_PMCSR_STATE_D0, priv->p_pm_offset, priv->p_pm_cap)) { 1201 aprint_error_dev(dv, "unsupported state, continuing.\n"); 1202 return false; 1203 } 1204 1205 cardbus_conf_restore(priv->p_cc, priv->p_cf, priv->p_tag, 1206 &priv->p_cardbusconf); 1207 1208 return true; 1209 } 1210 1211 static void 1212 cardbus_child_deregister(device_t dv) 1213 { 1214 struct cardbus_child_power *priv = device_pmf_bus_private(dv); 1215 1216 free(priv, M_DEVBUF); 1217 } 1218 1219 static void 1220 cardbus_child_register(device_t child) 1221 { 1222 device_t self = device_parent(child); 1223 struct cardbus_softc *sc = device_private(self); 1224 struct cardbus_devfunc *ct; 1225 struct cardbus_child_power *priv; 1226 int off; 1227 pcireg_t reg; 1228 1229 ct = sc->sc_funcs[device_locator(child, CARDBUSCF_FUNCTION)]; 1230 1231 priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK); 1232 1233 priv->p_ct = ct; 1234 priv->p_cc = ct->ct_cc; 1235 priv->p_cf = ct->ct_cf; 1236 priv->p_tag = cardbus_make_tag(priv->p_cc, priv->p_cf, ct->ct_bus, 1237 ct->ct_func); 1238 1239 if (cardbus_get_capability(priv->p_cc, priv->p_cf, priv->p_tag, 1240 PCI_CAP_PWRMGMT, &off, ®)) { 1241 priv->p_has_pm = true; 1242 priv->p_pm_offset = off; 1243 priv->p_pm_cap = reg; 1244 } else { 1245 priv->p_has_pm = false; 1246 priv->p_pm_offset = -1; 1247 } 1248 1249 device_pmf_bus_register(child, priv, cardbus_child_suspend, 1250 cardbus_child_resume, 0, cardbus_child_deregister); 1251 } 1252