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