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