1 /* $OpenBSD: sdmmc_mem.c,v 1.18 2013/10/22 16:49:27 syl 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 /* Routines for SD/MMC memory cards. */ 20 21 #include <sys/param.h> 22 #include <sys/device.h> 23 #include <sys/kernel.h> 24 #include <sys/malloc.h> 25 #include <sys/systm.h> 26 27 #include <dev/sdmmc/sdmmcchip.h> 28 #include <dev/sdmmc/sdmmcreg.h> 29 #include <dev/sdmmc/sdmmcvar.h> 30 31 int sdmmc_decode_csd(struct sdmmc_softc *, sdmmc_response, 32 struct sdmmc_function *); 33 int sdmmc_decode_cid(struct sdmmc_softc *, sdmmc_response, 34 struct sdmmc_function *); 35 void sdmmc_print_cid(struct sdmmc_cid *); 36 37 int sdmmc_mem_send_op_cond(struct sdmmc_softc *, u_int32_t, u_int32_t *); 38 int sdmmc_mem_set_blocklen(struct sdmmc_softc *, struct sdmmc_function *); 39 40 int sdmmc_mem_send_cxd_data(struct sdmmc_softc *, int, void *, size_t); 41 int sdmmc_mem_mmc_switch(struct sdmmc_function *, uint8_t, uint8_t, uint8_t); 42 43 int sdmmc_mem_sd_init(struct sdmmc_softc *, struct sdmmc_function *); 44 int sdmmc_mem_mmc_init(struct sdmmc_softc *, struct sdmmc_function *); 45 int sdmmc_mem_single_read_block(struct sdmmc_function *, int, u_char *, 46 size_t); 47 int sdmmc_mem_read_block_subr(struct sdmmc_function *, int, u_char *, 48 size_t); 49 int sdmmc_mem_single_write_block(struct sdmmc_function *, int, u_char *, 50 size_t); 51 int sdmmc_mem_write_block_subr(struct sdmmc_function *, int, u_char *, 52 size_t); 53 54 #ifdef SDMMC_DEBUG 55 #define DPRINTF(s) printf s 56 #else 57 #define DPRINTF(s) /**/ 58 #endif 59 60 /* 61 * Initialize SD/MMC memory cards and memory in SDIO "combo" cards. 62 */ 63 int 64 sdmmc_mem_enable(struct sdmmc_softc *sc) 65 { 66 u_int32_t host_ocr; 67 u_int32_t card_ocr; 68 69 rw_assert_wrlock(&sc->sc_lock); 70 71 /* Set host mode to SD "combo" card or SD memory-only. */ 72 SET(sc->sc_flags, SMF_SD_MODE|SMF_MEM_MODE); 73 74 /* Reset memory (*must* do that before CMD55 or CMD1). */ 75 sdmmc_go_idle_state(sc); 76 77 /* 78 * Read the SD/MMC memory OCR value by issuing CMD55 followed 79 * by ACMD41 to read the OCR value from memory-only SD cards. 80 * MMC cards will not respond to CMD55 or ACMD41 and this is 81 * how we distinguish them from SD cards. 82 */ 83 mmc_mode: 84 if (sdmmc_mem_send_op_cond(sc, 0, &card_ocr) != 0) { 85 if (ISSET(sc->sc_flags, SMF_SD_MODE) && 86 !ISSET(sc->sc_flags, SMF_IO_MODE)) { 87 /* Not a SD card, switch to MMC mode. */ 88 CLR(sc->sc_flags, SMF_SD_MODE); 89 goto mmc_mode; 90 } 91 if (!ISSET(sc->sc_flags, SMF_SD_MODE)) { 92 DPRINTF(("%s: can't read memory OCR\n", 93 DEVNAME(sc))); 94 return 1; 95 } else { 96 /* Not a "combo" card. */ 97 CLR(sc->sc_flags, SMF_MEM_MODE); 98 return 0; 99 } 100 } 101 102 /* Set the lowest voltage supported by the card and host. */ 103 host_ocr = sdmmc_chip_host_ocr(sc->sct, sc->sch); 104 if (sdmmc_set_bus_power(sc, host_ocr, card_ocr) != 0) { 105 DPRINTF(("%s: can't supply voltage requested by card\n", 106 DEVNAME(sc))); 107 return 1; 108 } 109 110 /* Tell the card(s) to enter the idle state (again). */ 111 sdmmc_go_idle_state(sc); 112 113 host_ocr &= card_ocr; /* only allow the common voltages */ 114 115 if (sdmmc_send_if_cond(sc, card_ocr) == 0) 116 host_ocr |= SD_OCR_SDHC_CAP; 117 118 /* Send the new OCR value until all cards are ready. */ 119 if (sdmmc_mem_send_op_cond(sc, host_ocr, NULL) != 0) { 120 DPRINTF(("%s: can't send memory OCR\n", DEVNAME(sc))); 121 return 1; 122 } 123 return 0; 124 } 125 126 /* 127 * Read the CSD and CID from all cards and assign each card a unique 128 * relative card address (RCA). CMD2 is ignored by SDIO-only cards. 129 */ 130 void 131 sdmmc_mem_scan(struct sdmmc_softc *sc) 132 { 133 struct sdmmc_command cmd; 134 struct sdmmc_function *sf; 135 u_int16_t next_rca; 136 int error; 137 int i; 138 139 rw_assert_wrlock(&sc->sc_lock); 140 141 /* 142 * CMD2 is a broadcast command understood by SD cards and MMC 143 * cards. All cards begin to respond to the command, but back 144 * off if another card drives the CMD line to a different level. 145 * Only one card will get its entire response through. That 146 * card remains silent once it has been assigned a RCA. 147 */ 148 for (i = 0; i < 100; i++) { 149 bzero(&cmd, sizeof cmd); 150 cmd.c_opcode = MMC_ALL_SEND_CID; 151 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R2; 152 153 error = sdmmc_mmc_command(sc, &cmd); 154 if (error == ETIMEDOUT) { 155 /* No more cards there. */ 156 break; 157 } else if (error != 0) { 158 DPRINTF(("%s: can't read CID\n", DEVNAME(sc))); 159 break; 160 } 161 162 /* In MMC mode, find the next available RCA. */ 163 next_rca = 1; 164 if (!ISSET(sc->sc_flags, SMF_SD_MODE)) 165 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) 166 next_rca++; 167 168 /* Allocate a sdmmc_function structure. */ 169 sf = sdmmc_function_alloc(sc); 170 sf->rca = next_rca; 171 172 /* 173 * Remember the CID returned in the CMD2 response for 174 * later decoding. 175 */ 176 bcopy(cmd.c_resp, sf->raw_cid, sizeof sf->raw_cid); 177 178 /* 179 * Silence the card by assigning it a unique RCA, or 180 * querying it for its RCA in the case of SD. 181 */ 182 if (sdmmc_set_relative_addr(sc, sf) != 0) { 183 printf("%s: can't set mem RCA\n", DEVNAME(sc)); 184 sdmmc_function_free(sf); 185 break; 186 } 187 188 #if 0 189 /* Verify that the RCA has been set by selecting the card. */ 190 if (sdmmc_select_card(sc, sf) != 0) { 191 printf("%s: can't select mem RCA %d\n", 192 DEVNAME(sc), sf->rca); 193 sdmmc_function_free(sf); 194 break; 195 } 196 197 /* Deselect. */ 198 (void)sdmmc_select_card(sc, NULL); 199 #endif 200 201 /* 202 * If this is a memory-only card, the card responding 203 * first becomes an alias for SDIO function 0. 204 */ 205 if (sc->sc_fn0 == NULL) 206 sc->sc_fn0 = sf; 207 208 SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list); 209 } 210 211 /* 212 * All cards are either inactive or awaiting further commands. 213 * Read the CSDs and decode the raw CID for each card. 214 */ 215 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) { 216 bzero(&cmd, sizeof cmd); 217 cmd.c_opcode = MMC_SEND_CSD; 218 cmd.c_arg = MMC_ARG_RCA(sf->rca); 219 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R2; 220 221 if (sdmmc_mmc_command(sc, &cmd) != 0) { 222 SET(sf->flags, SFF_ERROR); 223 continue; 224 } 225 226 if (sdmmc_decode_csd(sc, cmd.c_resp, sf) != 0 || 227 sdmmc_decode_cid(sc, sf->raw_cid, sf) != 0) { 228 SET(sf->flags, SFF_ERROR); 229 continue; 230 } 231 232 #ifdef SDMMC_DEBUG 233 printf("%s: CID: ", DEVNAME(sc)); 234 sdmmc_print_cid(&sf->cid); 235 #endif 236 } 237 } 238 239 int 240 sdmmc_decode_csd(struct sdmmc_softc *sc, sdmmc_response resp, 241 struct sdmmc_function *sf) 242 { 243 struct sdmmc_csd *csd = &sf->csd; 244 245 if (ISSET(sc->sc_flags, SMF_SD_MODE)) { 246 /* 247 * CSD version 1.0 corresponds to SD system 248 * specification version 1.0 - 1.10. (SanDisk, 3.5.3) 249 */ 250 csd->csdver = SD_CSD_CSDVER(resp); 251 switch (csd->csdver) { 252 case SD_CSD_CSDVER_2_0: 253 sf->flags |= SFF_SDHC; 254 csd->capacity = SD_CSD_V2_CAPACITY(resp); 255 csd->read_bl_len = SD_CSD_V2_BL_LEN; 256 break; 257 case SD_CSD_CSDVER_1_0: 258 csd->capacity = SD_CSD_CAPACITY(resp); 259 csd->read_bl_len = SD_CSD_READ_BL_LEN(resp); 260 break; 261 default: 262 printf("%s: unknown SD CSD structure version 0x%x\n", 263 DEVNAME(sc), csd->csdver); 264 return 1; 265 break; 266 } 267 268 } else { 269 csd->csdver = MMC_CSD_CSDVER(resp); 270 if (csd->csdver == MMC_CSD_CSDVER_1_0 || 271 csd->csdver == MMC_CSD_CSDVER_2_0 || 272 csd->csdver == MMC_CSD_CSDVER_EXT_CSD) { 273 csd->mmcver = MMC_CSD_MMCVER(resp); 274 csd->capacity = MMC_CSD_CAPACITY(resp); 275 csd->read_bl_len = MMC_CSD_READ_BL_LEN(resp); 276 } else { 277 printf("%s: unknown MMC CSD structure version 0x%x\n", 278 DEVNAME(sc), csd->csdver); 279 return 1; 280 } 281 } 282 csd->sector_size = MIN(1 << csd->read_bl_len, 283 sdmmc_chip_host_maxblklen(sc->sct, sc->sch)); 284 if (csd->sector_size < (1<<csd->read_bl_len)) 285 csd->capacity *= (1<<csd->read_bl_len) / 286 csd->sector_size; 287 288 return 0; 289 } 290 291 int 292 sdmmc_decode_cid(struct sdmmc_softc *sc, sdmmc_response resp, 293 struct sdmmc_function *sf) 294 { 295 struct sdmmc_cid *cid = &sf->cid; 296 297 if (ISSET(sc->sc_flags, SMF_SD_MODE)) { 298 cid->mid = SD_CID_MID(resp); 299 cid->oid = SD_CID_OID(resp); 300 SD_CID_PNM_CPY(resp, cid->pnm); 301 cid->rev = SD_CID_REV(resp); 302 cid->psn = SD_CID_PSN(resp); 303 cid->mdt = SD_CID_MDT(resp); 304 } else { 305 switch(sf->csd.mmcver) { 306 case MMC_CSD_MMCVER_1_0: 307 case MMC_CSD_MMCVER_1_4: 308 cid->mid = MMC_CID_MID_V1(resp); 309 MMC_CID_PNM_V1_CPY(resp, cid->pnm); 310 cid->rev = MMC_CID_REV_V1(resp); 311 cid->psn = MMC_CID_PSN_V1(resp); 312 cid->mdt = MMC_CID_MDT_V1(resp); 313 break; 314 case MMC_CSD_MMCVER_2_0: 315 case MMC_CSD_MMCVER_3_1: 316 case MMC_CSD_MMCVER_4_0: 317 cid->mid = MMC_CID_MID_V2(resp); 318 cid->oid = MMC_CID_OID_V2(resp); 319 MMC_CID_PNM_V2_CPY(resp, cid->pnm); 320 cid->psn = MMC_CID_PSN_V2(resp); 321 break; 322 default: 323 printf("%s: unknown MMC version %d\n", 324 DEVNAME(sc), sf->csd.mmcver); 325 return 1; 326 } 327 } 328 return 0; 329 } 330 331 #ifdef SDMMC_DEBUG 332 void 333 sdmmc_print_cid(struct sdmmc_cid *cid) 334 { 335 printf("mid=0x%02x oid=0x%04x pnm=\"%s\" rev=0x%02x psn=0x%08x" 336 " mdt=%03x\n", cid->mid, cid->oid, cid->pnm, cid->rev, cid->psn, 337 cid->mdt); 338 } 339 #endif 340 341 int 342 sdmmc_mem_send_cxd_data(struct sdmmc_softc *sc, int opcode, void *data, 343 size_t datalen) 344 { 345 struct sdmmc_command cmd; 346 void *ptr = NULL; 347 int error = 0; 348 349 ptr = malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO); 350 if (ptr == NULL) { 351 error = ENOMEM; 352 goto out; 353 } 354 355 memset(&cmd, 0, sizeof(cmd)); 356 cmd.c_data = ptr; 357 cmd.c_datalen = datalen; 358 cmd.c_blklen = datalen; 359 cmd.c_opcode = opcode; 360 cmd.c_arg = 0; 361 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ; 362 if (opcode == MMC_SEND_EXT_CSD) 363 SET(cmd.c_flags, SCF_RSP_R1); 364 else 365 SET(cmd.c_flags, SCF_RSP_R2); 366 367 error = sdmmc_mmc_command(sc, &cmd); 368 if (error == 0) 369 memcpy(data, ptr, datalen); 370 371 out: 372 if (ptr != NULL) 373 free(ptr, M_DEVBUF); 374 375 return error; 376 } 377 378 int 379 sdmmc_mem_mmc_switch(struct sdmmc_function *sf, uint8_t set, uint8_t index, 380 uint8_t value) 381 { 382 struct sdmmc_softc *sc = sf->sc; 383 struct sdmmc_command cmd; 384 385 memset(&cmd, 0, sizeof(cmd)); 386 cmd.c_opcode = MMC_SWITCH; 387 cmd.c_arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | 388 (index << 16) | (value << 8) | set; 389 cmd.c_flags = SCF_RSP_R1B | SCF_CMD_AC; 390 391 return sdmmc_mmc_command(sc, &cmd); 392 } 393 394 /* 395 * Initialize a SD/MMC memory card. 396 */ 397 int 398 sdmmc_mem_init(struct sdmmc_softc *sc, struct sdmmc_function *sf) 399 { 400 int error = 0; 401 402 rw_assert_wrlock(&sc->sc_lock); 403 404 if (sdmmc_select_card(sc, sf) != 0 || 405 sdmmc_mem_set_blocklen(sc, sf) != 0) 406 error = 1; 407 408 if (ISSET(sc->sc_flags, SMF_SD_MODE)) 409 error = sdmmc_mem_sd_init(sc, sf); 410 else 411 error = sdmmc_mem_mmc_init(sc, sf); 412 413 return error; 414 } 415 416 int 417 sdmmc_mem_sd_init(struct sdmmc_softc *sc, struct sdmmc_function *sf) 418 { 419 /* XXX */ 420 421 return 0; 422 } 423 424 int 425 sdmmc_mem_mmc_init(struct sdmmc_softc *sc, struct sdmmc_function *sf) 426 { 427 int error = 0; 428 u_int8_t ext_csd[512]; 429 int speed = 0; 430 int hs_timing = 0; 431 432 if (sf->csd.mmcver >= MMC_CSD_MMCVER_4_0) { 433 /* read EXT_CSD */ 434 error = sdmmc_mem_send_cxd_data(sc, 435 MMC_SEND_EXT_CSD, ext_csd, sizeof(ext_csd)); 436 if (error != 0) { 437 SET(sf->flags, SFF_ERROR); 438 printf("%s: can't read EXT_CSD\n", DEVNAME(sc)); 439 return error; 440 } 441 442 switch (ext_csd[EXT_CSD_CARD_TYPE]) { 443 case EXT_CSD_CARD_TYPE_26M: 444 speed = 26000; 445 break; 446 case EXT_CSD_CARD_TYPE_52M: 447 case EXT_CSD_CARD_TYPE_52M_V18: 448 case EXT_CSD_CARD_TYPE_52M_V12: 449 case EXT_CSD_CARD_TYPE_52M_V12_18: 450 speed = 52000; 451 hs_timing = 1; 452 break; 453 default: 454 printf("%s: unknown CARD_TYPE 0x%x\n", DEVNAME(sc), 455 ext_csd[EXT_CSD_CARD_TYPE]); 456 } 457 if (!ISSET(sc->sc_caps, SMC_CAPS_MMC_HIGHSPEED)) 458 hs_timing = 0; 459 460 if (hs_timing) { 461 /* switch to high speed timing */ 462 error = sdmmc_mem_mmc_switch(sf, EXT_CSD_CMD_SET_NORMAL, 463 EXT_CSD_HS_TIMING, hs_timing); 464 if (error != 0) { 465 printf("%s: can't change high speed\n", 466 DEVNAME(sc)); 467 return error; 468 } 469 } 470 471 error = 472 sdmmc_chip_bus_clock(sc->sct, sc->sch, speed); 473 if (error != 0) { 474 printf("%s: can't change bus clock\n", DEVNAME(sc)); 475 return error; 476 } 477 478 if (hs_timing) { 479 /* read EXT_CSD again */ 480 error = sdmmc_mem_send_cxd_data(sc, 481 MMC_SEND_EXT_CSD, ext_csd, sizeof(ext_csd)); 482 if (error != 0) { 483 printf("%s: can't re-read EXT_CSD\n", DEVNAME(sc)); 484 return error; 485 } 486 if (ext_csd[EXT_CSD_HS_TIMING] != 1) { 487 printf("%s, HS_TIMING set failed\n", DEVNAME(sc)); 488 return EINVAL; 489 } 490 } 491 } 492 493 return error; 494 } 495 496 /* 497 * Get or set the card's memory OCR value (SD or MMC). 498 */ 499 int 500 sdmmc_mem_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, 501 u_int32_t *ocrp) 502 { 503 struct sdmmc_command cmd; 504 int error; 505 int i; 506 507 rw_assert_wrlock(&sc->sc_lock); 508 509 /* 510 * If we change the OCR value, retry the command until the OCR 511 * we receive in response has the "CARD BUSY" bit set, meaning 512 * that all cards are ready for identification. 513 */ 514 for (i = 0; i < 100; i++) { 515 bzero(&cmd, sizeof cmd); 516 cmd.c_arg = ocr; 517 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R3; 518 519 if (ISSET(sc->sc_flags, SMF_SD_MODE)) { 520 cmd.c_opcode = SD_APP_OP_COND; 521 error = sdmmc_app_command(sc, &cmd); 522 } else { 523 cmd.c_opcode = MMC_SEND_OP_COND; 524 error = sdmmc_mmc_command(sc, &cmd); 525 } 526 if (error != 0) 527 break; 528 if (ISSET(MMC_R3(cmd.c_resp), MMC_OCR_MEM_READY) || 529 ocr == 0) 530 break; 531 error = ETIMEDOUT; 532 sdmmc_delay(10000); 533 } 534 if (error == 0 && ocrp != NULL) 535 *ocrp = MMC_R3(cmd.c_resp); 536 537 return error; 538 } 539 540 /* 541 * Set the read block length appropriately for this card, according to 542 * the card CSD register value. 543 */ 544 int 545 sdmmc_mem_set_blocklen(struct sdmmc_softc *sc, struct sdmmc_function *sf) 546 { 547 struct sdmmc_command cmd; 548 549 rw_assert_wrlock(&sc->sc_lock); 550 551 bzero(&cmd, sizeof cmd); 552 cmd.c_opcode = MMC_SET_BLOCKLEN; 553 cmd.c_arg = sf->csd.sector_size; 554 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 555 DPRINTF(("%s: read_bl_len=%d sector_size=%d\n", DEVNAME(sc), 556 1 << sf->csd.read_bl_len, sf->csd.sector_size)); 557 558 return sdmmc_mmc_command(sc, &cmd); 559 } 560 561 int 562 sdmmc_mem_read_block_subr(struct sdmmc_function *sf, int blkno, u_char *data, 563 size_t datalen) 564 { 565 struct sdmmc_softc *sc = sf->sc; 566 struct sdmmc_command cmd; 567 int error; 568 569 570 if ((error = sdmmc_select_card(sc, sf)) != 0) 571 goto err; 572 573 bzero(&cmd, sizeof cmd); 574 cmd.c_data = data; 575 cmd.c_datalen = datalen; 576 cmd.c_blklen = sf->csd.sector_size; 577 cmd.c_opcode = (datalen / cmd.c_blklen) > 1 ? 578 MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE; 579 if (sf->flags & SFF_SDHC) 580 cmd.c_arg = blkno; 581 else 582 cmd.c_arg = blkno << 9; 583 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1; 584 585 error = sdmmc_mmc_command(sc, &cmd); 586 if (error != 0) 587 goto err; 588 589 if (ISSET(sc->sc_flags, SMF_STOP_AFTER_MULTIPLE) && 590 cmd.c_opcode == MMC_READ_BLOCK_MULTIPLE) { 591 bzero(&cmd, sizeof cmd); 592 cmd.c_opcode = MMC_STOP_TRANSMISSION; 593 cmd.c_arg = MMC_ARG_RCA(sf->rca); 594 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B; 595 error = sdmmc_mmc_command(sc, &cmd); 596 if (error != 0) 597 goto err; 598 } 599 600 do { 601 bzero(&cmd, sizeof cmd); 602 cmd.c_opcode = MMC_SEND_STATUS; 603 cmd.c_arg = MMC_ARG_RCA(sf->rca); 604 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 605 error = sdmmc_mmc_command(sc, &cmd); 606 if (error != 0) 607 break; 608 /* XXX time out */ 609 } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA)); 610 611 err: 612 return (error); 613 } 614 615 int 616 sdmmc_mem_single_read_block(struct sdmmc_function *sf, int blkno, u_char *data, 617 size_t datalen) 618 { 619 int error; 620 int i; 621 622 for (i = 0; i < datalen / sf->csd.sector_size; i++) { 623 error = sdmmc_mem_read_block_subr(sf, blkno + i, data + i * 624 sf->csd.sector_size, sf->csd.sector_size); 625 if (error) 626 break; 627 } 628 629 return (error); 630 } 631 632 int 633 sdmmc_mem_read_block(struct sdmmc_function *sf, int blkno, u_char *data, 634 size_t datalen) 635 { 636 struct sdmmc_softc *sc = sf->sc; 637 int error; 638 639 rw_enter_write(&sc->sc_lock); 640 641 if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) { 642 error = sdmmc_mem_single_read_block(sf, blkno, data, datalen); 643 } else { 644 error = sdmmc_mem_read_block_subr(sf, blkno, data, datalen); 645 } 646 647 rw_exit(&sc->sc_lock); 648 return (error); 649 } 650 651 int 652 sdmmc_mem_write_block_subr(struct sdmmc_function *sf, int blkno, u_char *data, 653 size_t datalen) 654 { 655 struct sdmmc_softc *sc = sf->sc; 656 struct sdmmc_command cmd; 657 int error; 658 659 if ((error = sdmmc_select_card(sc, sf)) != 0) 660 goto err; 661 662 bzero(&cmd, sizeof cmd); 663 cmd.c_data = data; 664 cmd.c_datalen = datalen; 665 cmd.c_blklen = sf->csd.sector_size; 666 cmd.c_opcode = (datalen / cmd.c_blklen) > 1 ? 667 MMC_WRITE_BLOCK_MULTIPLE : MMC_WRITE_BLOCK_SINGLE; 668 if (sf->flags & SFF_SDHC) 669 cmd.c_arg = blkno; 670 else 671 cmd.c_arg = blkno << 9; 672 cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R1; 673 674 error = sdmmc_mmc_command(sc, &cmd); 675 if (error != 0) 676 goto err; 677 678 if (ISSET(sc->sc_flags, SMF_STOP_AFTER_MULTIPLE) && 679 cmd.c_opcode == MMC_WRITE_BLOCK_MULTIPLE) { 680 bzero(&cmd, sizeof cmd); 681 cmd.c_opcode = MMC_STOP_TRANSMISSION; 682 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B; 683 error = sdmmc_mmc_command(sc, &cmd); 684 if (error != 0) 685 goto err; 686 } 687 688 do { 689 bzero(&cmd, sizeof cmd); 690 cmd.c_opcode = MMC_SEND_STATUS; 691 cmd.c_arg = MMC_ARG_RCA(sf->rca); 692 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 693 error = sdmmc_mmc_command(sc, &cmd); 694 if (error != 0) 695 break; 696 /* XXX time out */ 697 } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA)); 698 699 err: 700 return (error); 701 } 702 703 int 704 sdmmc_mem_single_write_block(struct sdmmc_function *sf, int blkno, u_char *data, 705 size_t datalen) 706 { 707 int error; 708 int i; 709 710 for (i = 0; i < datalen / sf->csd.sector_size; i++) { 711 error = sdmmc_mem_write_block_subr(sf, blkno + i, data + i * 712 sf->csd.sector_size, sf->csd.sector_size); 713 if (error) 714 break; 715 } 716 717 return (error); 718 } 719 720 int 721 sdmmc_mem_write_block(struct sdmmc_function *sf, int blkno, u_char *data, 722 size_t datalen) 723 { 724 struct sdmmc_softc *sc = sf->sc; 725 int error; 726 727 rw_enter_write(&sc->sc_lock); 728 729 if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) { 730 error = sdmmc_mem_single_write_block(sf, blkno, data, datalen); 731 } else { 732 error = sdmmc_mem_write_block_subr(sf, blkno, data, datalen); 733 } 734 735 rw_exit(&sc->sc_lock); 736 return (error); 737 } 738