1 /* $OpenBSD: cardbus.c,v 1.45 2010/08/25 21:37:59 kettenis Exp $ */ 2 /* $NetBSD: cardbus.c,v 1.24 2000/04/02 19:11:37 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1997, 1998, 1999 and 2000 6 * HAYAKAWA Koichi. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/device.h> 34 #include <sys/malloc.h> 35 #include <sys/kernel.h> 36 #include <sys/syslog.h> 37 #include <sys/proc.h> 38 39 #include <machine/bus.h> 40 41 #include <dev/cardbus/cardbusvar.h> 42 #include <dev/pci/pcidevs.h> 43 44 #include <dev/cardbus/cardbus_exrom.h> 45 46 #include <dev/pci/pcivar.h> /* XXX */ 47 #include <dev/pci/pcireg.h> /* XXX */ 48 49 #include <dev/pcmcia/pcmciareg.h> 50 51 #ifdef CARDBUS_DEBUG 52 #define STATIC 53 #define DPRINTF(a) printf a 54 #else 55 #ifdef DDB 56 #define STATIC 57 #else 58 #define STATIC static 59 #endif 60 #define DPRINTF(a) 61 #endif 62 63 STATIC void cardbusattach(struct device *, struct device *, void *); 64 /* STATIC int cardbusprint(void *, const char *); */ 65 66 STATIC int cardbusmatch(struct device *, void *, void *); 67 STATIC int cardbussubmatch(struct device *, void *, void *); 68 STATIC int cardbusprint(void *, const char *); 69 70 typedef void (*tuple_decode_func)(u_int8_t *, int, void *); 71 72 STATIC int decode_tuples(u_int8_t *, int, tuple_decode_func, void *); 73 STATIC void parse_tuple(u_int8_t *, int, void *); 74 #ifdef CARDBUS_DEBUG 75 static void print_tuple(u_int8_t *, int, void *); 76 #endif 77 78 STATIC int cardbus_read_tuples(struct cardbus_attach_args *, 79 pcireg_t, u_int8_t *, size_t); 80 81 STATIC void enable_function(struct cardbus_softc *, int, int); 82 STATIC void disable_function(struct cardbus_softc *, int); 83 84 85 struct cfattach cardbus_ca = { 86 sizeof(struct cardbus_softc), cardbusmatch, cardbusattach, 87 NULL, config_activate_children 88 }; 89 90 struct cfdriver cardbus_cd = { 91 NULL, "cardbus", DV_DULL 92 }; 93 94 STATIC int 95 cardbusmatch(struct device *parent, void *match, void *aux) 96 { 97 struct cfdata *cf = match; 98 struct cbslot_attach_args *cba = aux; 99 100 if (strcmp(cba->cba_busname, cf->cf_driver->cd_name)) { 101 DPRINTF(("cardbusmatch: busname differs %s <=> %s\n", 102 cba->cba_busname, cf->cf_driver->cd_name)); 103 return (0); 104 } 105 106 return (1); 107 } 108 109 STATIC void 110 cardbusattach(struct device *parent, struct device *self, void *aux) 111 { 112 struct cardbus_softc *sc = (void *)self; 113 struct cbslot_attach_args *cba = aux; 114 int cdstatus; 115 116 sc->sc_bus = cba->cba_bus; 117 sc->sc_device = 0; 118 sc->sc_intrline = cba->cba_intrline; 119 sc->sc_cacheline = cba->cba_cacheline; 120 sc->sc_lattimer = cba->cba_lattimer; 121 122 printf(": bus %d device %d", sc->sc_bus, sc->sc_device); 123 printf(" cacheline 0x%x, lattimer 0x%x\n", 124 sc->sc_cacheline,sc->sc_lattimer); 125 126 sc->sc_iot = cba->cba_iot; /* CardBus I/O space tag */ 127 sc->sc_memt = cba->cba_memt; /* CardBus MEM space tag */ 128 sc->sc_dmat = cba->cba_dmat; /* DMA tag */ 129 sc->sc_cc = cba->cba_cc; 130 sc->sc_pc = cba->cba_pc; 131 sc->sc_cf = cba->cba_cf; 132 sc->sc_rbus_iot = cba->cba_rbus_iot; 133 sc->sc_rbus_memt = cba->cba_rbus_memt; 134 135 sc->sc_funcs = NULL; 136 137 cdstatus = 0; 138 } 139 140 STATIC int 141 cardbus_read_tuples(struct cardbus_attach_args *ca, pcireg_t cis_ptr, 142 u_int8_t *tuples, size_t len) 143 { 144 struct cardbus_softc *sc = ca->ca_ct->ct_sc; 145 pci_chipset_tag_t pc = ca->ca_pc; 146 pcitag_t tag = ca->ca_tag; 147 pcireg_t command; 148 int found = 0; 149 150 int i, j; 151 int cardbus_space = cis_ptr & CARDBUS_CIS_ASIMASK; 152 bus_space_tag_t bar_tag; 153 bus_space_handle_t bar_memh; 154 bus_size_t bar_size; 155 bus_addr_t bar_addr; 156 157 int reg; 158 159 memset(tuples, 0, len); 160 161 cis_ptr = cis_ptr & CARDBUS_CIS_ADDRMASK; 162 163 switch (cardbus_space) { 164 case CARDBUS_CIS_ASI_TUPLE: 165 DPRINTF(("%s: reading CIS data from configuration space\n", 166 sc->sc_dev.dv_xname)); 167 for (i = cis_ptr, j = 0; i < 0xff; i += 4) { 168 u_int32_t e = pci_conf_read(pc, tag, i); 169 tuples[j] = 0xff & e; 170 e >>= 8; 171 tuples[j + 1] = 0xff & e; 172 e >>= 8; 173 tuples[j + 2] = 0xff & e; 174 e >>= 8; 175 tuples[j + 3] = 0xff & e; 176 j += 4; 177 } 178 found++; 179 break; 180 181 case CARDBUS_CIS_ASI_BAR0: 182 case CARDBUS_CIS_ASI_BAR1: 183 case CARDBUS_CIS_ASI_BAR2: 184 case CARDBUS_CIS_ASI_BAR3: 185 case CARDBUS_CIS_ASI_BAR4: 186 case CARDBUS_CIS_ASI_BAR5: 187 case CARDBUS_CIS_ASI_ROM: 188 if (cardbus_space == CARDBUS_CIS_ASI_ROM) { 189 reg = CARDBUS_ROM_REG; 190 DPRINTF(("%s: reading CIS data from ROM\n", 191 sc->sc_dev.dv_xname)); 192 } else { 193 reg = CARDBUS_BASE0_REG + (cardbus_space - 1) * 4; 194 DPRINTF(("%s: reading CIS data from BAR%d\n", 195 sc->sc_dev.dv_xname, cardbus_space - 1)); 196 } 197 198 /* XXX zero register so mapreg_map doesn't get confused by old 199 contents */ 200 pci_conf_write(pc, tag, reg, 0); 201 if (Cardbus_mapreg_map(ca->ca_ct, reg, 202 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0, 203 &bar_tag, &bar_memh, &bar_addr, &bar_size)) { 204 printf("%s: can't map memory\n", 205 sc->sc_dev.dv_xname); 206 return (1); 207 } 208 209 if (cardbus_space == CARDBUS_CIS_ASI_ROM) { 210 pcireg_t exrom; 211 int save; 212 struct cardbus_rom_image_head rom_image; 213 struct cardbus_rom_image *p; 214 215 save = splhigh(); 216 /* enable rom address decoder */ 217 exrom = pci_conf_read(pc, tag, reg); 218 pci_conf_write(pc, tag, reg, exrom | 1); 219 220 command = pci_conf_read(pc, tag, 221 PCI_COMMAND_STATUS_REG); 222 pci_conf_write(pc, tag, 223 PCI_COMMAND_STATUS_REG, 224 command | PCI_COMMAND_MEM_ENABLE); 225 226 if (cardbus_read_exrom(ca->ca_memt, bar_memh, 227 &rom_image)) 228 goto out; 229 230 for (p = SIMPLEQ_FIRST(&rom_image); p; 231 p = SIMPLEQ_NEXT(p, next)) { 232 if (p->rom_image == 233 CARDBUS_CIS_ASI_ROM_IMAGE(cis_ptr)) { 234 bus_space_read_region_1(p->romt, 235 p->romh, CARDBUS_CIS_ADDR(cis_ptr), 236 tuples, MIN(p->image_size, len)); 237 found++; 238 break; 239 } 240 } 241 242 out: 243 while ((p = SIMPLEQ_FIRST(&rom_image)) != NULL) { 244 SIMPLEQ_REMOVE_HEAD(&rom_image, next); 245 free(p, M_DEVBUF); 246 } 247 exrom = pci_conf_read(pc, tag, reg); 248 pci_conf_write(pc, tag, reg, exrom & ~1); 249 splx(save); 250 } else { 251 command = pci_conf_read(pc, tag, 252 PCI_COMMAND_STATUS_REG); 253 pci_conf_write(pc, tag, 254 PCI_COMMAND_STATUS_REG, 255 command | PCI_COMMAND_MEM_ENABLE); 256 /* XXX byte order? */ 257 bus_space_read_region_1(ca->ca_memt, bar_memh, 258 cis_ptr, tuples, 256); 259 found++; 260 } 261 command = pci_conf_read(pc, tag, 262 PCI_COMMAND_STATUS_REG); 263 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, 264 command & ~PCI_COMMAND_MEM_ENABLE); 265 pci_conf_write(pc, tag, reg, 0); 266 267 Cardbus_mapreg_unmap(ca->ca_ct, reg, bar_tag, bar_memh, 268 bar_size); 269 break; 270 271 #ifdef DIAGNOSTIC 272 default: 273 panic("%s: bad CIS space (%d)", sc->sc_dev.dv_xname, 274 cardbus_space); 275 #endif 276 } 277 return (!found); 278 } 279 280 STATIC void 281 parse_tuple(u_int8_t *tuple, int len, void *data) 282 { 283 struct cardbus_cis_info *cis = data; 284 int bar_index; 285 int i; 286 char *p; 287 288 switch (tuple[0]) { 289 case PCMCIA_CISTPL_MANFID: 290 if (tuple[1] < 4) { 291 DPRINTF(("%s: wrong length manufacturer id (%d)\n", 292 __func__, tuple[1])); 293 break; 294 } 295 cis->manufacturer = tuple[2] | (tuple[3] << 8); 296 cis->product = tuple[4] | (tuple[5] << 8); 297 break; 298 case PCMCIA_CISTPL_VERS_1: 299 bcopy(tuple + 2, cis->cis1_info_buf, tuple[1]); 300 i = 0; 301 p = cis->cis1_info_buf + 2; 302 while (i < 303 sizeof(cis->cis1_info) / sizeof(cis->cis1_info[0])) { 304 if (p >= cis->cis1_info_buf + tuple[1] || *p == '\xff') 305 break; 306 cis->cis1_info[i++] = p; 307 while (*p != '\0' && *p != '\xff') 308 p++; 309 if (*p == '\0') 310 p++; 311 } 312 break; 313 case PCMCIA_CISTPL_BAR: 314 if (tuple[1] != 6) { 315 DPRINTF(("%s: BAR with short length (%d)\n", 316 __func__, tuple[1])); 317 break; 318 } 319 bar_index = tuple[2] & 7; 320 if (bar_index == 0) { 321 DPRINTF(("%s: invalid ASI in BAR tuple\n", 322 __func__)); 323 break; 324 } 325 bar_index--; 326 cis->bar[bar_index].flags = tuple[2]; 327 cis->bar[bar_index].size = (tuple[4] << 0) | 328 (tuple[5] << 8) | (tuple[6] << 16) | (tuple[7] << 24); 329 break; 330 case PCMCIA_CISTPL_FUNCID: 331 cis->funcid = tuple[2]; 332 break; 333 334 case PCMCIA_CISTPL_FUNCE: 335 switch (cis->funcid) { 336 case PCMCIA_FUNCTION_SERIAL: 337 if (tuple[1] >= 2 && 338 tuple[2] == 0 339 /* XXX PCMCIA_TPLFE_TYPE_SERIAL_??? */) { 340 cis->funce.serial.uart_type = tuple[3] & 0x1f; 341 cis->funce.serial.uart_present = 1; 342 } 343 break; 344 case PCMCIA_FUNCTION_NETWORK: 345 if (tuple[1] >= 8 && tuple[2] == 346 PCMCIA_TPLFE_TYPE_LAN_NID) { 347 if (tuple[3] > 348 sizeof(cis->funce.network.netid)) { 349 DPRINTF(("%s: unknown network id type" 350 " (len = %d)\n", __func__, 351 tuple[3])); 352 } else { 353 cis->funce.network.netid_present = 1; 354 bcopy(tuple + 4, 355 cis->funce.network.netid, tuple[3]); 356 } 357 } 358 } 359 break; 360 } 361 } 362 363 /* 364 * int cardbus_attach_card(struct cardbus_softc *sc) 365 * 366 * This function attaches the card on the slot: turns on power, 367 * reads and analyses tuple, sets configuration index. 368 * 369 * This function returns the number of recognised device functions. 370 * If no functions are recognised, return 0. 371 */ 372 int 373 cardbus_attach_card(struct cardbus_softc *sc) 374 { 375 cardbus_chipset_tag_t cc; 376 cardbus_function_tag_t cf; 377 int cdstatus; 378 pcitag_t tag; 379 pcireg_t id, class, cis_ptr; 380 pcireg_t bhlc; 381 u_int8_t *tuple; 382 int function, nfunction; 383 struct cardbus_devfunc **previous_next = &(sc->sc_funcs); 384 struct device *csc; 385 int no_work_funcs = 0; 386 cardbus_devfunc_t ct; 387 pci_chipset_tag_t pc = sc->sc_pc; 388 int i; 389 390 cc = sc->sc_cc; 391 cf = sc->sc_cf; 392 393 DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit)); 394 395 /* inspect initial voltage */ 396 if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) { 397 DPRINTF(("cardbusattach: no CardBus card on cb%d\n", 398 sc->sc_dev.dv_unit)); 399 return (0); 400 } 401 402 /* XXX use fake function 8 to keep power on during whole configuration */ 403 enable_function(sc, cdstatus, 8); 404 405 function = 0; 406 407 tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device, function); 408 409 /* Wait until power comes up. Maximum 500 ms. */ 410 for (i = 0; i < 5; ++i) { 411 id = pci_conf_read(pc, tag, PCI_ID_REG); 412 if (id != 0xffffffff && id != 0) 413 break; 414 if (cold) { /* before kernel thread invoked */ 415 delay(100*1000); 416 } else { /* thread context */ 417 if (tsleep((void *)sc, PCATCH, "cardbus", 418 hz/10) != EWOULDBLOCK) { 419 break; 420 } 421 } 422 } 423 if (i == 5) 424 return (0); 425 426 bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG); 427 DPRINTF(("%s bhlc 0x%08x -> ", sc->sc_dev.dv_xname, bhlc)); 428 nfunction = PCI_HDRTYPE_MULTIFN(bhlc) ? 8 : 1; 429 430 tuple = malloc(2048, M_TEMP, M_NOWAIT); 431 if (tuple == NULL) 432 panic("no room for cardbus tuples"); 433 434 for (function = 0; function < nfunction; function++) { 435 struct cardbus_attach_args ca; 436 437 tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device, 438 function); 439 440 id = pci_conf_read(pc, tag, PCI_ID_REG); 441 class = pci_conf_read(pc, tag, PCI_CLASS_REG); 442 cis_ptr = pci_conf_read(pc, tag, CARDBUS_CIS_REG); 443 444 /* Invalid vendor ID value? */ 445 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID) 446 continue; 447 448 DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, " 449 "CIS 0x%x\n", PCI_VENDOR(id), PCI_PRODUCT(id), 450 cis_ptr)); 451 452 enable_function(sc, cdstatus, function); 453 454 /* clean up every BAR */ 455 pci_conf_write(pc, tag, CARDBUS_BASE0_REG, 0); 456 pci_conf_write(pc, tag, CARDBUS_BASE1_REG, 0); 457 pci_conf_write(pc, tag, CARDBUS_BASE2_REG, 0); 458 pci_conf_write(pc, tag, CARDBUS_BASE3_REG, 0); 459 pci_conf_write(pc, tag, CARDBUS_BASE4_REG, 0); 460 pci_conf_write(pc, tag, CARDBUS_BASE5_REG, 0); 461 pci_conf_write(pc, tag, CARDBUS_ROM_REG, 0); 462 463 /* set initial latency and cacheline size */ 464 bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG); 465 DPRINTF(("%s func%d bhlc 0x%08x -> ", sc->sc_dev.dv_xname, 466 function, bhlc)); 467 bhlc &= ~((PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT) | 468 (PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT)); 469 bhlc |= ((sc->sc_cacheline & PCI_CACHELINE_MASK) << 470 PCI_CACHELINE_SHIFT); 471 bhlc |= ((sc->sc_lattimer & PCI_LATTIMER_MASK) << 472 PCI_LATTIMER_SHIFT); 473 474 pci_conf_write(pc, tag, PCI_BHLC_REG, bhlc); 475 bhlc = pci_conf_read(pc, tag, PCI_BHLC_REG); 476 DPRINTF(("0x%08x\n", bhlc)); 477 478 if (PCI_LATTIMER(bhlc) < 0x10) { 479 bhlc &= ~(PCI_LATTIMER_MASK << 480 PCI_LATTIMER_SHIFT); 481 bhlc |= (0x10 << PCI_LATTIMER_SHIFT); 482 pci_conf_write(pc, tag, PCI_BHLC_REG, 483 bhlc); 484 } 485 486 /* 487 * We need to allocate the ct here, since we might 488 * need it when reading the CIS 489 */ 490 if ((ct = 491 (cardbus_devfunc_t)malloc(sizeof(struct cardbus_devfunc), 492 M_DEVBUF, M_NOWAIT)) == NULL) 493 panic("no room for cardbus_tag"); 494 495 ct->ct_cc = sc->sc_cc; 496 ct->ct_cf = sc->sc_cf; 497 ct->ct_bus = sc->sc_bus; 498 ct->ct_dev = sc->sc_device; 499 ct->ct_func = function; 500 ct->ct_sc = sc; 501 ct->ct_next = NULL; 502 *previous_next = ct; 503 504 memset(&ca, 0, sizeof(ca)); 505 506 ca.ca_unit = sc->sc_dev.dv_unit; 507 ca.ca_ct = ct; 508 509 ca.ca_iot = sc->sc_iot; 510 ca.ca_memt = sc->sc_memt; 511 ca.ca_dmat = sc->sc_dmat; 512 ca.ca_rbus_iot = sc->sc_rbus_iot; 513 ca.ca_rbus_memt = sc->sc_rbus_memt; 514 ca.ca_tag = tag; 515 ca.ca_bus = sc->sc_bus; 516 ca.ca_device = sc->sc_device; 517 ca.ca_function = function; 518 ca.ca_id = id; 519 ca.ca_class = class; 520 ca.ca_pc = sc->sc_pc; 521 522 ca.ca_intrline = sc->sc_intrline; 523 524 if (cis_ptr != 0) { 525 if (cardbus_read_tuples(&ca, cis_ptr, tuple, 2048)) { 526 printf("cardbus_attach_card: failed to " 527 "read CIS\n"); 528 } else { 529 #ifdef CARDBUS_DEBUG 530 decode_tuples(tuple, 2048, print_tuple, NULL); 531 #endif 532 decode_tuples(tuple, 2048, parse_tuple, 533 &ca.ca_cis); 534 } 535 } 536 537 if ((csc = config_found_sm((void *)sc, &ca, cardbusprint, 538 cardbussubmatch)) == NULL) { 539 /* do not match */ 540 disable_function(sc, function); 541 free(ct, M_DEVBUF); 542 *previous_next = NULL; 543 } else { 544 /* found */ 545 previous_next = &(ct->ct_next); 546 ct->ct_device = csc; 547 ++no_work_funcs; 548 } 549 } 550 /* 551 * XXX power down pseudo function 8 (this will power down the card 552 * if no functions were attached). 553 */ 554 disable_function(sc, 8); 555 free(tuple, M_TEMP); 556 557 return (no_work_funcs); 558 } 559 560 STATIC int 561 cardbussubmatch(struct device *parent, void *match, void *aux) 562 { 563 struct cfdata *cf = match; 564 struct cardbus_attach_args *ca = aux; 565 566 if (cf->cardbuscf_dev != CARDBUS_UNK_DEV && 567 cf->cardbuscf_dev != ca->ca_unit) { 568 return (0); 569 } 570 if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION && 571 cf->cardbuscf_function != ca->ca_function) { 572 return (0); 573 } 574 575 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 576 } 577 578 STATIC int 579 cardbusprint(void *aux, const char *pnp) 580 { 581 struct cardbus_attach_args *ca = aux; 582 char devinfo[256]; 583 584 if (pnp) { 585 pci_devinfo(ca->ca_id, ca->ca_class, 1, devinfo, 586 sizeof(devinfo)); 587 printf("%s at %s", devinfo, pnp); 588 } 589 printf(" dev %d function %d", ca->ca_device, ca->ca_function); 590 if (!pnp) { 591 pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, 592 sizeof(devinfo)); 593 printf(" %s", devinfo); 594 } 595 596 return (UNCONF); 597 } 598 599 /* 600 * void cardbus_detach_card(struct cardbus_softc *sc) 601 * 602 * This function detaches the card on the slot: detach device data 603 * structure and turns off the power. 604 * 605 * This function must not be called under interrupt context. 606 */ 607 void 608 cardbus_detach_card(struct cardbus_softc *sc) 609 { 610 struct cardbus_devfunc *ct, *ct_next, **prev_next; 611 612 prev_next = &(sc->sc_funcs->ct_next); 613 614 for (ct = sc->sc_funcs; ct != NULL; ct = ct_next) { 615 struct device *fndev = ct->ct_device; 616 ct_next = ct->ct_next; 617 618 DPRINTF(("%s: detaching %s\n", sc->sc_dev.dv_xname, 619 fndev->dv_xname)); 620 /* call device detach function */ 621 622 if (config_detach(fndev, 0) != 0) { 623 printf("%s: cannot detach dev %s, function %d\n", 624 sc->sc_dev.dv_xname, fndev->dv_xname, ct->ct_func); 625 prev_next = &(ct->ct_next); 626 } else { 627 sc->sc_poweron_func &= ~(1 << ct->ct_func); 628 *prev_next = ct->ct_next; 629 free(ct, M_DEVBUF); 630 } 631 } 632 633 sc->sc_poweron_func = 0; 634 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V | CARDBUS_VPP_0V); 635 } 636 637 /* 638 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg, name) 639 * Interrupt handler of pccard. 640 * args: 641 * cardbus_chipset_tag_t *cc 642 * int irq: 643 */ 644 void * 645 cardbus_intr_establish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf, 646 cardbus_intr_handle_t irq, int level, int (*func)(void *), void *arg, 647 const char *name) 648 { 649 DPRINTF(("- cardbus_intr_establish: irq %d\n", irq)); 650 651 return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg, name); 652 } 653 654 /* 655 * void cardbus_intr_disestablish(cc, cf, handler) 656 * Interrupt handler of pccard. 657 * args: 658 * cardbus_chipset_tag_t *cc 659 */ 660 void 661 cardbus_intr_disestablish(cardbus_chipset_tag_t cc, cardbus_function_tag_t cf, 662 void *handler) 663 { 664 DPRINTF(("- pccard_intr_disestablish\n")); 665 666 (*cf->cardbus_intr_disestablish)(cc, handler); 667 } 668 669 /* XXX this should be merged with cardbus_function_{enable,disable}, 670 but we don't have a ct when these functions are called */ 671 672 STATIC void 673 enable_function(struct cardbus_softc *sc, int cdstatus, int function) 674 { 675 if (sc->sc_poweron_func == 0) { 676 /* switch to 3V and/or wait for power to stabilize */ 677 if (cdstatus & CARDBUS_3V_CARD) { 678 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_3V); 679 } else { 680 /* No cards other than 3.3V cards. */ 681 return; 682 } 683 (sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_RESET); 684 } 685 sc->sc_poweron_func |= (1 << function); 686 } 687 688 STATIC void 689 disable_function(struct cardbus_softc *sc, int function) 690 { 691 sc->sc_poweron_func &= ~(1 << function); 692 if (sc->sc_poweron_func == 0) { 693 /* power-off because no functions are enabled */ 694 sc->sc_cf->cardbus_power(sc->sc_cc, CARDBUS_VCC_0V); 695 } 696 } 697 698 /* 699 * int cardbus_function_enable(struct cardbus_softc *sc, int func) 700 * 701 * This function enables a function on a card. When no power is 702 * applied on the card, power will be applied on it. 703 */ 704 int 705 cardbus_function_enable(struct cardbus_softc *sc, int func) 706 { 707 pci_chipset_tag_t pc = sc->sc_pc; 708 pcireg_t command; 709 pcitag_t tag; 710 711 DPRINTF(("entering cardbus_function_enable... ")); 712 713 /* entering critical area */ 714 715 /* XXX: sc_vold should be used */ 716 enable_function(sc, CARDBUS_3V_CARD, func); 717 718 /* exiting critical area */ 719 720 tag = pci_make_tag(pc, sc->sc_bus, sc->sc_device, func); 721 722 command = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG); 723 command |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_IO_ENABLE | 724 PCI_COMMAND_MASTER_ENABLE); /* XXX: good guess needed */ 725 726 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, command); 727 728 DPRINTF(("%x\n", sc->sc_poweron_func)); 729 730 return (0); 731 } 732 733 /* 734 * int cardbus_function_disable(struct cardbus_softc *, int func) 735 * 736 * This function disable a function on a card. When no functions are 737 * enabled, it turns off the power. 738 */ 739 int 740 cardbus_function_disable(struct cardbus_softc *sc, int func) 741 { 742 DPRINTF(("entering cardbus_function_disable... ")); 743 744 disable_function(sc, func); 745 746 return (0); 747 } 748 749 int 750 cardbus_matchbyid(struct cardbus_attach_args *ca, 751 const struct pci_matchid *ids, int nent) 752 { 753 const struct pci_matchid *pm; 754 int i; 755 756 for (i = 0, pm = ids; i < nent; i++, pm++) 757 if (PCI_VENDOR(ca->ca_id) == pm->pm_vid && 758 PCI_PRODUCT(ca->ca_id) == pm->pm_pid) 759 return (1); 760 return (0); 761 } 762 763 /* 764 * below this line, there are some functions for decoding tuples. 765 * They should go out from this file. 766 */ 767 768 STATIC u_int8_t * 769 decode_tuple(u_int8_t *, u_int8_t *, tuple_decode_func, void *); 770 771 STATIC int 772 decode_tuples(u_int8_t *tuple, int buflen, tuple_decode_func func, void *data) 773 { 774 u_int8_t *tp = tuple; 775 776 if (PCMCIA_CISTPL_LINKTARGET != *tuple) { 777 DPRINTF(("WRONG TUPLE: 0x%x\n", *tuple)); 778 return (0); 779 } 780 781 while ((tp = decode_tuple(tp, tuple + buflen, func, data)) != NULL) 782 ; 783 784 return (1); 785 } 786 787 STATIC u_int8_t * 788 decode_tuple(u_int8_t *tuple, u_int8_t *end, tuple_decode_func func, 789 void *data) 790 { 791 u_int8_t type; 792 u_int8_t len; 793 794 type = tuple[0]; 795 switch (type) { 796 case PCMCIA_CISTPL_NULL: 797 case PCMCIA_CISTPL_END: 798 len = 1; 799 break; 800 default: 801 if (tuple + 2 > end) 802 return (NULL); 803 len = tuple[1] + 2; 804 break; 805 } 806 807 if (tuple + len > end) 808 return (NULL); 809 810 (*func)(tuple, len, data); 811 812 if (PCMCIA_CISTPL_END == type || tuple + len == end) 813 return (NULL); 814 815 return (tuple + len); 816 } 817 818 #ifdef CARDBUS_DEBUG 819 static char *tuple_name(int type); 820 821 static char * 822 tuple_name(int type) 823 { 824 static char *tuple_name_s [] = { 825 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */ 826 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */ 827 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */ 828 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */ 829 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */ 830 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A", /* 14-17 */ 831 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY", /* 18-1B */ 832 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", /* 1C-1E */ 833 "DEVICE_GEO_A", "MANFID", "FUNCID", "FUNCE", "SWIL", /* 1F-23 */ 834 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */ 835 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */ 836 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */ 837 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */ 838 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */ 839 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */ 840 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */ 841 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER", /* 40-43 */ 842 "DATE", "BATTERY", "ORG", "FORMAT_A" /* 44-47 */ 843 }; 844 845 if (type > 0 && type < nitems(tuple_name_s)) 846 return (tuple_name_s[type]); 847 else if (0xff == type) 848 return ("END"); 849 else 850 return ("Reserved"); 851 } 852 853 static void 854 print_tuple(u_int8_t *tuple, int len, void *data) 855 { 856 int i; 857 858 printf("tuple: %s len %d\n", tuple_name(tuple[0]), len); 859 860 for (i = 0; i < len; ++i) { 861 if (i % 16 == 0) 862 printf(" 0x%02x:", i); 863 printf(" %x",tuple[i]); 864 if (i % 16 == 15) 865 printf("\n"); 866 } 867 if (i % 16 != 0) 868 printf("\n"); 869 } 870 #endif 871