1 /* $OpenBSD: sdmmc.c,v 1.48 2017/12/24 12:55:52 kettenis Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org> 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 19 /* 20 * Host controller independent SD/MMC bus driver based on information 21 * from SanDisk SD Card Product Manual Revision 2.2 (SanDisk), SDIO 22 * Simple Specification Version 1.0 (SDIO) and the Linux "mmc" driver. 23 */ 24 25 #include <sys/param.h> 26 #include <sys/device.h> 27 #include <sys/kernel.h> 28 #include <sys/kthread.h> 29 #include <sys/malloc.h> 30 #include <sys/rwlock.h> 31 #include <sys/systm.h> 32 33 #include <scsi/scsi_all.h> 34 #include <scsi/scsiconf.h> 35 36 #include <dev/sdmmc/sdmmc_scsi.h> 37 #include <dev/sdmmc/sdmmcchip.h> 38 #include <dev/sdmmc/sdmmcreg.h> 39 #include <dev/sdmmc/sdmmcvar.h> 40 41 #ifdef SDMMC_IOCTL 42 #include "bio.h" 43 #if NBIO < 1 44 #undef SDMMC_IOCTL 45 #endif 46 #include <dev/biovar.h> 47 #endif 48 49 int sdmmc_match(struct device *, void *, void *); 50 void sdmmc_attach(struct device *, struct device *, void *); 51 int sdmmc_detach(struct device *, int); 52 int sdmmc_activate(struct device *, int); 53 54 void sdmmc_create_thread(void *); 55 void sdmmc_task_thread(void *); 56 void sdmmc_discover_task(void *); 57 void sdmmc_card_attach(struct sdmmc_softc *); 58 void sdmmc_card_detach(struct sdmmc_softc *, int); 59 int sdmmc_enable(struct sdmmc_softc *); 60 void sdmmc_disable(struct sdmmc_softc *); 61 int sdmmc_scan(struct sdmmc_softc *); 62 int sdmmc_init(struct sdmmc_softc *); 63 #ifdef SDMMC_IOCTL 64 int sdmmc_ioctl(struct device *, u_long, caddr_t); 65 #endif 66 67 #ifdef SDMMC_DEBUG 68 int sdmmcdebug = 0; 69 extern int sdhcdebug; /* XXX should have a sdmmc_chip_debug() function */ 70 void sdmmc_dump_command(struct sdmmc_softc *, struct sdmmc_command *); 71 #define DPRINTF(n,s) do { if ((n) <= sdmmcdebug) printf s; } while (0) 72 #else 73 #define DPRINTF(n,s) do {} while (0) 74 #endif 75 76 struct cfattach sdmmc_ca = { 77 sizeof(struct sdmmc_softc), sdmmc_match, sdmmc_attach, sdmmc_detach, 78 sdmmc_activate 79 }; 80 81 struct cfdriver sdmmc_cd = { 82 NULL, "sdmmc", DV_DULL 83 }; 84 85 int 86 sdmmc_match(struct device *parent, void *match, void *aux) 87 { 88 struct cfdata *cf = match; 89 struct sdmmcbus_attach_args *saa = aux; 90 91 return strcmp(saa->saa_busname, cf->cf_driver->cd_name) == 0; 92 } 93 94 void 95 sdmmc_attach(struct device *parent, struct device *self, void *aux) 96 { 97 struct sdmmc_softc *sc = (struct sdmmc_softc *)self; 98 struct sdmmcbus_attach_args *saa = aux; 99 int error; 100 101 if (ISSET(saa->caps, SMC_CAPS_8BIT_MODE)) 102 printf(": 8-bit"); 103 else if (ISSET(saa->caps, SMC_CAPS_4BIT_MODE)) 104 printf(": 4-bit"); 105 else 106 printf(": 1-bit"); 107 if (ISSET(saa->caps, SMC_CAPS_SD_HIGHSPEED)) 108 printf(", sd high-speed"); 109 if (ISSET(saa->caps, SMC_CAPS_MMC_HIGHSPEED)) 110 printf(", mmc high-speed"); 111 if (ISSET(saa->caps, SMC_CAPS_DMA)) 112 printf(", dma"); 113 printf("\n"); 114 115 sc->sct = saa->sct; 116 sc->sch = saa->sch; 117 sc->sc_dmat = saa->dmat; 118 sc->sc_dmap = saa->dmap; 119 sc->sc_flags = saa->flags; 120 sc->sc_caps = saa->caps; 121 sc->sc_max_xfer = saa->max_xfer; 122 123 if (ISSET(sc->sc_caps, SMC_CAPS_DMA) && sc->sc_dmap == NULL) { 124 error = bus_dmamap_create(sc->sc_dmat, MAXPHYS, SDMMC_MAXNSEGS, 125 MAXPHYS, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &sc->sc_dmap); 126 if (error) { 127 printf("%s: can't create DMA map\n", DEVNAME(sc)); 128 return; 129 } 130 } 131 132 SIMPLEQ_INIT(&sc->sf_head); 133 TAILQ_INIT(&sc->sc_tskq); 134 TAILQ_INIT(&sc->sc_intrq); 135 sdmmc_init_task(&sc->sc_discover_task, sdmmc_discover_task, sc); 136 sdmmc_init_task(&sc->sc_intr_task, sdmmc_intr_task, sc); 137 rw_init(&sc->sc_lock, DEVNAME(sc)); 138 139 #ifdef SDMMC_IOCTL 140 if (bio_register(self, sdmmc_ioctl) != 0) 141 printf("%s: unable to register ioctl\n", DEVNAME(sc)); 142 #endif 143 144 /* 145 * Create the event thread that will attach and detach cards 146 * and perform other lengthy operations. Enter config_pending 147 * state until the discovery task has run for the first time. 148 */ 149 SET(sc->sc_flags, SMF_CONFIG_PENDING); 150 config_pending_incr(); 151 kthread_create_deferred(sdmmc_create_thread, sc); 152 } 153 154 int 155 sdmmc_detach(struct device *self, int flags) 156 { 157 struct sdmmc_softc *sc = (struct sdmmc_softc *)self; 158 159 sc->sc_dying = 1; 160 while (sc->sc_task_thread != NULL) { 161 wakeup(&sc->sc_tskq); 162 tsleep(sc, PWAIT, "mmcdie", 0); 163 } 164 165 if (sc->sc_dmap) 166 bus_dmamap_destroy(sc->sc_dmat, sc->sc_dmap); 167 168 return 0; 169 } 170 171 int 172 sdmmc_activate(struct device *self, int act) 173 { 174 struct sdmmc_softc *sc = (struct sdmmc_softc *)self; 175 int rv = 0; 176 177 switch (act) { 178 case DVACT_SUSPEND: 179 rv = config_activate_children(self, act); 180 /* If card in slot, cause a detach/re-attach */ 181 if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) 182 sc->sc_dying = -1; 183 break; 184 case DVACT_RESUME: 185 rv = config_activate_children(self, act); 186 wakeup(&sc->sc_tskq); 187 break; 188 default: 189 rv = config_activate_children(self, act); 190 break; 191 } 192 return (rv); 193 } 194 195 void 196 sdmmc_create_thread(void *arg) 197 { 198 struct sdmmc_softc *sc = arg; 199 200 if (kthread_create(sdmmc_task_thread, sc, &sc->sc_task_thread, 201 DEVNAME(sc)) != 0) 202 printf("%s: can't create task thread\n", DEVNAME(sc)); 203 204 } 205 206 void 207 sdmmc_task_thread(void *arg) 208 { 209 struct sdmmc_softc *sc = arg; 210 struct sdmmc_task *task; 211 int s; 212 213 restart: 214 sdmmc_needs_discover(&sc->sc_dev); 215 216 s = splsdmmc(); 217 while (!sc->sc_dying) { 218 for (task = TAILQ_FIRST(&sc->sc_tskq); task != NULL; 219 task = TAILQ_FIRST(&sc->sc_tskq)) { 220 splx(s); 221 sdmmc_del_task(task); 222 task->func(task->arg); 223 s = splsdmmc(); 224 } 225 tsleep(&sc->sc_tskq, PWAIT, "mmctsk", 0); 226 } 227 splx(s); 228 229 if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) { 230 rw_enter_write(&sc->sc_lock); 231 sdmmc_card_detach(sc, DETACH_FORCE); 232 rw_exit(&sc->sc_lock); 233 } 234 235 /* 236 * During a suspend, the card is detached since we do not know 237 * if it is the same upon wakeup. Go re-discover the bus. 238 */ 239 if (sc->sc_dying == -1) { 240 CLR(sc->sc_flags, SMF_CARD_PRESENT); 241 sc->sc_dying = 0; 242 goto restart; 243 } 244 sc->sc_task_thread = NULL; 245 wakeup(sc); 246 kthread_exit(0); 247 } 248 249 void 250 sdmmc_add_task(struct sdmmc_softc *sc, struct sdmmc_task *task) 251 { 252 int s; 253 254 s = splsdmmc(); 255 TAILQ_INSERT_TAIL(&sc->sc_tskq, task, next); 256 task->onqueue = 1; 257 task->sc = sc; 258 wakeup(&sc->sc_tskq); 259 splx(s); 260 } 261 262 void 263 sdmmc_del_task(struct sdmmc_task *task) 264 { 265 struct sdmmc_softc *sc = task->sc; 266 int s; 267 268 if (sc == NULL) 269 return; 270 271 s = splsdmmc(); 272 task->sc = NULL; 273 task->onqueue = 0; 274 TAILQ_REMOVE(&sc->sc_tskq, task, next); 275 splx(s); 276 } 277 278 void 279 sdmmc_needs_discover(struct device *self) 280 { 281 struct sdmmc_softc *sc = (struct sdmmc_softc *)self; 282 283 if (!sdmmc_task_pending(&sc->sc_discover_task)) 284 sdmmc_add_task(sc, &sc->sc_discover_task); 285 } 286 287 void 288 sdmmc_discover_task(void *arg) 289 { 290 struct sdmmc_softc *sc = arg; 291 292 if (sdmmc_chip_card_detect(sc->sct, sc->sch)) { 293 if (!ISSET(sc->sc_flags, SMF_CARD_PRESENT)) { 294 SET(sc->sc_flags, SMF_CARD_PRESENT); 295 sdmmc_card_attach(sc); 296 } 297 } else { 298 if (ISSET(sc->sc_flags, SMF_CARD_PRESENT)) { 299 CLR(sc->sc_flags, SMF_CARD_PRESENT); 300 rw_enter_write(&sc->sc_lock); 301 sdmmc_card_detach(sc, DETACH_FORCE); 302 rw_exit(&sc->sc_lock); 303 } 304 } 305 306 if (ISSET(sc->sc_flags, SMF_CONFIG_PENDING)) { 307 CLR(sc->sc_flags, SMF_CONFIG_PENDING); 308 config_pending_decr(); 309 } 310 } 311 312 /* 313 * Called from process context when a card is present. 314 */ 315 void 316 sdmmc_card_attach(struct sdmmc_softc *sc) 317 { 318 DPRINTF(1,("%s: attach card\n", DEVNAME(sc))); 319 320 rw_enter_write(&sc->sc_lock); 321 CLR(sc->sc_flags, SMF_CARD_ATTACHED); 322 323 /* 324 * Power up the card (or card stack). 325 */ 326 if (sdmmc_enable(sc) != 0) { 327 printf("%s: can't enable card\n", DEVNAME(sc)); 328 goto err; 329 } 330 331 /* 332 * Scan for I/O functions and memory cards on the bus, 333 * allocating a sdmmc_function structure for each. 334 */ 335 if (sdmmc_scan(sc) != 0) { 336 printf("%s: no functions\n", DEVNAME(sc)); 337 goto err; 338 } 339 340 /* 341 * Initialize the I/O functions and memory cards. 342 */ 343 if (sdmmc_init(sc) != 0) { 344 printf("%s: init failed\n", DEVNAME(sc)); 345 goto err; 346 } 347 348 /* Attach SCSI emulation for memory cards. */ 349 if (ISSET(sc->sc_flags, SMF_MEM_MODE)) 350 sdmmc_scsi_attach(sc); 351 352 /* Attach I/O function drivers. */ 353 if (ISSET(sc->sc_flags, SMF_IO_MODE)) 354 sdmmc_io_attach(sc); 355 356 SET(sc->sc_flags, SMF_CARD_ATTACHED); 357 rw_exit(&sc->sc_lock); 358 return; 359 err: 360 sdmmc_card_detach(sc, DETACH_FORCE); 361 rw_exit(&sc->sc_lock); 362 } 363 364 /* 365 * Called from process context with DETACH_* flags from <sys/device.h> 366 * when cards are gone. 367 */ 368 void 369 sdmmc_card_detach(struct sdmmc_softc *sc, int flags) 370 { 371 struct sdmmc_function *sf, *sfnext; 372 373 rw_assert_wrlock(&sc->sc_lock); 374 375 DPRINTF(1,("%s: detach card\n", DEVNAME(sc))); 376 377 if (ISSET(sc->sc_flags, SMF_CARD_ATTACHED)) { 378 /* Detach I/O function drivers. */ 379 if (ISSET(sc->sc_flags, SMF_IO_MODE)) 380 sdmmc_io_detach(sc); 381 382 /* Detach the SCSI emulation for memory cards. */ 383 if (ISSET(sc->sc_flags, SMF_MEM_MODE)) 384 sdmmc_scsi_detach(sc); 385 386 CLR(sc->sc_flags, SMF_CARD_ATTACHED); 387 } 388 389 /* Power down. */ 390 sdmmc_disable(sc); 391 392 /* Free all sdmmc_function structures. */ 393 for (sf = SIMPLEQ_FIRST(&sc->sf_head); sf != NULL; sf = sfnext) { 394 sfnext = SIMPLEQ_NEXT(sf, sf_list); 395 sdmmc_function_free(sf); 396 } 397 SIMPLEQ_INIT(&sc->sf_head); 398 sc->sc_function_count = 0; 399 sc->sc_fn0 = NULL; 400 } 401 402 int 403 sdmmc_enable(struct sdmmc_softc *sc) 404 { 405 u_int32_t host_ocr; 406 int error; 407 408 rw_assert_wrlock(&sc->sc_lock); 409 410 /* 411 * Calculate the equivalent of the card OCR from the host 412 * capabilities and select the maximum supported bus voltage. 413 */ 414 host_ocr = sdmmc_chip_host_ocr(sc->sct, sc->sch); 415 error = sdmmc_chip_bus_power(sc->sct, sc->sch, host_ocr); 416 if (error != 0) { 417 printf("%s: can't supply bus power\n", DEVNAME(sc)); 418 goto err; 419 } 420 421 /* 422 * Select the minimum clock frequency. 423 */ 424 error = sdmmc_chip_bus_clock(sc->sct, sc->sch, 425 SDMMC_SDCLK_400KHZ, SDMMC_TIMING_LEGACY); 426 if (error != 0) { 427 printf("%s: can't supply clock\n", DEVNAME(sc)); 428 goto err; 429 } 430 431 /* XXX wait for card to power up */ 432 sdmmc_delay(250000); 433 434 /* Initialize SD I/O card function(s). */ 435 if ((error = sdmmc_io_enable(sc)) != 0) 436 goto err; 437 438 /* Initialize SD/MMC memory card(s). */ 439 if (ISSET(sc->sc_flags, SMF_MEM_MODE) && 440 (error = sdmmc_mem_enable(sc)) != 0) 441 goto err; 442 443 err: 444 if (error != 0) 445 sdmmc_disable(sc); 446 447 return error; 448 } 449 450 void 451 sdmmc_disable(struct sdmmc_softc *sc) 452 { 453 /* XXX complete commands if card is still present. */ 454 455 rw_assert_wrlock(&sc->sc_lock); 456 457 /* Make sure no card is still selected. */ 458 (void)sdmmc_select_card(sc, NULL); 459 460 /* Turn off bus power and clock. */ 461 (void)sdmmc_chip_bus_clock(sc->sct, sc->sch, 462 SDMMC_SDCLK_OFF, SDMMC_TIMING_LEGACY); 463 (void)sdmmc_chip_bus_power(sc->sct, sc->sch, 0); 464 } 465 466 /* 467 * Set the lowest bus voltage supported by the card and the host. 468 */ 469 int 470 sdmmc_set_bus_power(struct sdmmc_softc *sc, u_int32_t host_ocr, 471 u_int32_t card_ocr) 472 { 473 u_int32_t bit; 474 475 rw_assert_wrlock(&sc->sc_lock); 476 477 /* Mask off unsupported voltage levels and select the lowest. */ 478 DPRINTF(1,("%s: host_ocr=%x ", DEVNAME(sc), host_ocr)); 479 host_ocr &= card_ocr; 480 for (bit = 4; bit < 23; bit++) { 481 if (ISSET(host_ocr, 1<<bit)) { 482 host_ocr &= 3<<bit; 483 break; 484 } 485 } 486 DPRINTF(1,("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr)); 487 488 if (host_ocr == 0 || 489 sdmmc_chip_bus_power(sc->sct, sc->sch, host_ocr) != 0) 490 return 1; 491 return 0; 492 } 493 494 struct sdmmc_function * 495 sdmmc_function_alloc(struct sdmmc_softc *sc) 496 { 497 struct sdmmc_function *sf; 498 499 sf = (struct sdmmc_function *)malloc(sizeof *sf, M_DEVBUF, 500 M_WAITOK | M_ZERO); 501 sf->sc = sc; 502 sf->number = -1; 503 sf->cis.manufacturer = SDMMC_VENDOR_INVALID; 504 sf->cis.product = SDMMC_PRODUCT_INVALID; 505 sf->cis.function = SDMMC_FUNCTION_INVALID; 506 return sf; 507 } 508 509 void 510 sdmmc_function_free(struct sdmmc_function *sf) 511 { 512 free(sf, M_DEVBUF, sizeof *sf); 513 } 514 515 /* 516 * Scan for I/O functions and memory cards on the bus, allocating a 517 * sdmmc_function structure for each. 518 */ 519 int 520 sdmmc_scan(struct sdmmc_softc *sc) 521 { 522 523 rw_assert_wrlock(&sc->sc_lock); 524 525 /* Scan for I/O functions. */ 526 if (ISSET(sc->sc_flags, SMF_IO_MODE)) 527 sdmmc_io_scan(sc); 528 529 /* Scan for memory cards on the bus. */ 530 if (ISSET(sc->sc_flags, SMF_MEM_MODE)) 531 sdmmc_mem_scan(sc); 532 533 /* There should be at least one function now. */ 534 if (SIMPLEQ_EMPTY(&sc->sf_head)) { 535 printf("%s: can't identify card\n", DEVNAME(sc)); 536 return 1; 537 } 538 return 0; 539 } 540 541 /* 542 * Initialize all the distinguished functions of the card, be it I/O 543 * or memory functions. 544 */ 545 int 546 sdmmc_init(struct sdmmc_softc *sc) 547 { 548 struct sdmmc_function *sf; 549 550 rw_assert_wrlock(&sc->sc_lock); 551 552 /* Initialize all identified card functions. */ 553 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) { 554 if (ISSET(sc->sc_flags, SMF_IO_MODE) && 555 sdmmc_io_init(sc, sf) != 0) 556 printf("%s: i/o init failed\n", DEVNAME(sc)); 557 558 if (ISSET(sc->sc_flags, SMF_MEM_MODE) && 559 sdmmc_mem_init(sc, sf) != 0) 560 printf("%s: mem init failed\n", DEVNAME(sc)); 561 } 562 563 /* Any good functions left after initialization? */ 564 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) { 565 if (!ISSET(sf->flags, SFF_ERROR)) 566 return 0; 567 } 568 /* No, we should probably power down the card. */ 569 return 1; 570 } 571 572 void 573 sdmmc_delay(u_int usecs) 574 { 575 int nticks = usecs / (1000000 / hz); 576 577 if (!cold && nticks > 0) 578 tsleep(&sdmmc_delay, PWAIT, "mmcdly", nticks); 579 else 580 delay(usecs); 581 } 582 583 int 584 sdmmc_app_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd) 585 { 586 struct sdmmc_command acmd; 587 int error; 588 589 rw_assert_wrlock(&sc->sc_lock); 590 591 bzero(&acmd, sizeof acmd); 592 acmd.c_opcode = MMC_APP_CMD; 593 acmd.c_arg = 0; 594 if (sc->sc_card != NULL) { 595 acmd.c_arg = sc->sc_card->rca << 16; 596 } 597 acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 598 599 error = sdmmc_mmc_command(sc, &acmd); 600 if (error != 0) { 601 return error; 602 } 603 604 if (!ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) { 605 /* Card does not support application commands. */ 606 return ENODEV; 607 } 608 609 error = sdmmc_mmc_command(sc, cmd); 610 return error; 611 } 612 613 /* 614 * Execute MMC command and data transfers. All interactions with the 615 * host controller to complete the command happen in the context of 616 * the current process. 617 */ 618 int 619 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd) 620 { 621 int error; 622 623 rw_assert_wrlock(&sc->sc_lock); 624 625 sdmmc_chip_exec_command(sc->sct, sc->sch, cmd); 626 627 #ifdef SDMMC_DEBUG 628 sdmmc_dump_command(sc, cmd); 629 #endif 630 631 error = cmd->c_error; 632 wakeup(cmd); 633 634 return error; 635 } 636 637 /* 638 * Send the "GO IDLE STATE" command. 639 */ 640 void 641 sdmmc_go_idle_state(struct sdmmc_softc *sc) 642 { 643 struct sdmmc_command cmd; 644 645 rw_assert_wrlock(&sc->sc_lock); 646 647 bzero(&cmd, sizeof cmd); 648 cmd.c_opcode = MMC_GO_IDLE_STATE; 649 cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0; 650 651 (void)sdmmc_mmc_command(sc, &cmd); 652 } 653 654 /* 655 * Send the "SEND_IF_COND" command, to check operating condition 656 */ 657 int 658 sdmmc_send_if_cond(struct sdmmc_softc *sc, uint32_t card_ocr) 659 { 660 struct sdmmc_command cmd; 661 uint8_t pat = 0x23; /* any pattern will do here */ 662 uint8_t res; 663 664 rw_assert_wrlock(&sc->sc_lock); 665 666 bzero(&cmd, sizeof cmd); 667 668 cmd.c_opcode = SD_SEND_IF_COND; 669 cmd.c_arg = ((card_ocr & SD_OCR_VOL_MASK) != 0) << 8 | pat; 670 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R7; 671 672 if (sdmmc_mmc_command(sc, &cmd) != 0) 673 return 1; 674 675 res = cmd.c_resp[0]; 676 if (res != pat) 677 return 1; 678 else 679 return 0; 680 } 681 682 /* 683 * Retrieve (SD) or set (MMC) the relative card address (RCA). 684 */ 685 int 686 sdmmc_set_relative_addr(struct sdmmc_softc *sc, 687 struct sdmmc_function *sf) 688 { 689 struct sdmmc_command cmd; 690 691 rw_assert_wrlock(&sc->sc_lock); 692 693 bzero(&cmd, sizeof cmd); 694 695 if (ISSET(sc->sc_flags, SMF_SD_MODE)) { 696 cmd.c_opcode = SD_SEND_RELATIVE_ADDR; 697 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6; 698 } else { 699 cmd.c_opcode = MMC_SET_RELATIVE_ADDR; 700 cmd.c_arg = MMC_ARG_RCA(sf->rca); 701 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 702 } 703 704 if (sdmmc_mmc_command(sc, &cmd) != 0) 705 return 1; 706 707 if (ISSET(sc->sc_flags, SMF_SD_MODE)) 708 sf->rca = SD_R6_RCA(cmd.c_resp); 709 return 0; 710 } 711 712 int 713 sdmmc_select_card(struct sdmmc_softc *sc, struct sdmmc_function *sf) 714 { 715 struct sdmmc_command cmd; 716 int error; 717 718 rw_assert_wrlock(&sc->sc_lock); 719 720 if (sc->sc_card == sf || (sf && sc->sc_card && 721 sc->sc_card->rca == sf->rca)) { 722 sc->sc_card = sf; 723 return 0; 724 } 725 726 bzero(&cmd, sizeof cmd); 727 cmd.c_opcode = MMC_SELECT_CARD; 728 cmd.c_arg = sf == NULL ? 0 : MMC_ARG_RCA(sf->rca); 729 cmd.c_flags = SCF_CMD_AC | (sf == NULL ? SCF_RSP_R0 : SCF_RSP_R1); 730 error = sdmmc_mmc_command(sc, &cmd); 731 if (error == 0 || sf == NULL) 732 sc->sc_card = sf; 733 return error; 734 } 735 736 #ifdef SDMMC_IOCTL 737 int 738 sdmmc_ioctl(struct device *self, u_long request, caddr_t addr) 739 { 740 struct sdmmc_softc *sc = (struct sdmmc_softc *)self; 741 struct sdmmc_command *ucmd; 742 struct sdmmc_command cmd; 743 void *data; 744 int error = 0; 745 746 switch (request) { 747 #ifdef SDMMC_DEBUG 748 case SDIOCSETDEBUG: 749 sdmmcdebug = (((struct bio_sdmmc_debug *)addr)->debug) & 0xff; 750 sdhcdebug = (((struct bio_sdmmc_debug *)addr)->debug >> 8) & 0xff; 751 break; 752 #endif 753 754 case SDIOCEXECMMC: 755 case SDIOCEXECAPP: 756 ucmd = &((struct bio_sdmmc_command *)addr)->cmd; 757 758 /* Refuse to transfer more than 512K per command. */ 759 if (ucmd->c_datalen > 524288) 760 return ENOMEM; 761 762 /* Verify that the data buffer is safe to copy. */ 763 if ((ucmd->c_datalen > 0 && ucmd->c_data == NULL) || 764 (ucmd->c_datalen < 1 && ucmd->c_data != NULL) || 765 ucmd->c_datalen < 0) 766 return EINVAL; 767 768 bzero(&cmd, sizeof cmd); 769 cmd.c_opcode = ucmd->c_opcode; 770 cmd.c_arg = ucmd->c_arg; 771 cmd.c_flags = ucmd->c_flags; 772 cmd.c_blklen = ucmd->c_blklen; 773 774 if (ucmd->c_data) { 775 data = malloc(ucmd->c_datalen, M_TEMP, 776 M_WAITOK | M_CANFAIL); 777 if (data == NULL) 778 return ENOMEM; 779 error = copyin(ucmd->c_data, data, ucmd->c_datalen); 780 if (error != 0) 781 goto exec_done; 782 783 cmd.c_data = data; 784 cmd.c_datalen = ucmd->c_datalen; 785 } 786 787 rw_enter_write(&sc->sc_lock); 788 if (request == SDIOCEXECMMC) 789 error = sdmmc_mmc_command(sc, &cmd); 790 else 791 error = sdmmc_app_command(sc, &cmd); 792 rw_exit(&sc->sc_lock); 793 if (error && !cmd.c_error) 794 cmd.c_error = error; 795 796 bcopy(&cmd.c_resp, ucmd->c_resp, sizeof cmd.c_resp); 797 ucmd->c_flags = cmd.c_flags; 798 ucmd->c_error = cmd.c_error; 799 800 if (ucmd->c_data) 801 error = copyout(data, ucmd->c_data, ucmd->c_datalen); 802 else 803 error = 0; 804 805 exec_done: 806 if (ucmd->c_data) 807 free(data, M_TEMP, ucmd->c_datalen); 808 break; 809 810 default: 811 return ENOTTY; 812 } 813 return error; 814 } 815 #endif 816 817 #ifdef SDMMC_DEBUG 818 void 819 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd) 820 { 821 int i; 822 823 rw_assert_wrlock(&sc->sc_lock); 824 825 DPRINTF(1,("%s: cmd %u arg=%#x data=%p dlen=%d flags=%#x " 826 "proc=\"%s\" (error %d)\n", DEVNAME(sc), cmd->c_opcode, 827 cmd->c_arg, cmd->c_data, cmd->c_datalen, cmd->c_flags, 828 curproc ? curproc->p_p->ps_comm : "", cmd->c_error)); 829 830 if (cmd->c_error || sdmmcdebug < 1) 831 return; 832 833 printf("%s: resp=", DEVNAME(sc)); 834 if (ISSET(cmd->c_flags, SCF_RSP_136)) 835 for (i = 0; i < sizeof cmd->c_resp; i++) 836 printf("%02x ", ((u_char *)cmd->c_resp)[i]); 837 else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT)) 838 for (i = 0; i < 4; i++) 839 printf("%02x ", ((u_char *)cmd->c_resp)[i]); 840 printf("\n"); 841 } 842 #endif 843