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