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