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