1 /* $OpenBSD: sdmmc_mem.c,v 1.12 2009/04/07 16:35:52 blambert 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/kernel.h> 23 #include <sys/malloc.h> 24 #include <sys/systm.h> 25 26 #include <dev/sdmmc/sdmmcchip.h> 27 #include <dev/sdmmc/sdmmcreg.h> 28 #include <dev/sdmmc/sdmmcvar.h> 29 30 int sdmmc_decode_csd(struct sdmmc_softc *, sdmmc_response, 31 struct sdmmc_function *); 32 int sdmmc_decode_cid(struct sdmmc_softc *, sdmmc_response, 33 struct sdmmc_function *); 34 void sdmmc_print_cid(struct sdmmc_cid *); 35 36 int sdmmc_mem_send_op_cond(struct sdmmc_softc *, u_int32_t, u_int32_t *); 37 int sdmmc_mem_set_blocklen(struct sdmmc_softc *, struct sdmmc_function *); 38 39 #ifdef SDMMC_DEBUG 40 #define DPRINTF(s) printf s 41 #else 42 #define DPRINTF(s) /**/ 43 #endif 44 45 /* 46 * Initialize SD/MMC memory cards and memory in SDIO "combo" cards. 47 */ 48 int 49 sdmmc_mem_enable(struct sdmmc_softc *sc) 50 { 51 u_int32_t host_ocr; 52 u_int32_t card_ocr; 53 54 SDMMC_ASSERT_LOCKED(sc); 55 56 /* Set host mode to SD "combo" card or SD memory-only. */ 57 SET(sc->sc_flags, SMF_SD_MODE|SMF_MEM_MODE); 58 59 /* Reset memory (*must* do that before CMD55 or CMD1). */ 60 sdmmc_go_idle_state(sc); 61 62 /* 63 * Read the SD/MMC memory OCR value by issuing CMD55 followed 64 * by ACMD41 to read the OCR value from memory-only SD cards. 65 * MMC cards will not respond to CMD55 or ACMD41 and this is 66 * how we distinguish them from SD cards. 67 */ 68 mmc_mode: 69 if (sdmmc_mem_send_op_cond(sc, 0, &card_ocr) != 0) { 70 if (ISSET(sc->sc_flags, SMF_SD_MODE) && 71 !ISSET(sc->sc_flags, SMF_IO_MODE)) { 72 /* Not a SD card, switch to MMC mode. */ 73 CLR(sc->sc_flags, SMF_SD_MODE); 74 goto mmc_mode; 75 } 76 if (!ISSET(sc->sc_flags, SMF_SD_MODE)) { 77 DPRINTF(("%s: can't read memory OCR\n", 78 SDMMCDEVNAME(sc))); 79 return 1; 80 } else { 81 /* Not a "combo" card. */ 82 CLR(sc->sc_flags, SMF_MEM_MODE); 83 return 0; 84 } 85 } 86 87 /* Set the lowest voltage supported by the card and host. */ 88 host_ocr = sdmmc_chip_host_ocr(sc->sct, sc->sch); 89 if (sdmmc_set_bus_power(sc, host_ocr, card_ocr) != 0) { 90 DPRINTF(("%s: can't supply voltage requested by card\n", 91 SDMMCDEVNAME(sc))); 92 return 1; 93 } 94 95 /* Tell the card(s) to enter the idle state (again). */ 96 sdmmc_go_idle_state(sc); 97 98 if (sdmmc_send_if_cond(sc, card_ocr) == 0) 99 host_ocr |= SD_OCR_SDHC_CAP; 100 101 /* Send the new OCR value until all cards are ready. */ 102 if (sdmmc_mem_send_op_cond(sc, host_ocr, NULL) != 0) { 103 DPRINTF(("%s: can't send memory OCR\n", SDMMCDEVNAME(sc))); 104 return 1; 105 } 106 return 0; 107 } 108 109 /* 110 * Read the CSD and CID from all cards and assign each card a unique 111 * relative card address (RCA). CMD2 is ignored by SDIO-only cards. 112 */ 113 void 114 sdmmc_mem_scan(struct sdmmc_softc *sc) 115 { 116 struct sdmmc_command cmd; 117 struct sdmmc_function *sf; 118 u_int16_t next_rca; 119 int error; 120 int i; 121 122 SDMMC_ASSERT_LOCKED(sc); 123 124 /* 125 * CMD2 is a broadcast command understood by SD cards and MMC 126 * cards. All cards begin to respond to the command, but back 127 * off if another card drives the CMD line to a different level. 128 * Only one card will get its entire response through. That 129 * card remains silent once it has been assigned a RCA. 130 */ 131 for (i = 0; i < 100; i++) { 132 bzero(&cmd, sizeof cmd); 133 cmd.c_opcode = MMC_ALL_SEND_CID; 134 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R2; 135 136 error = sdmmc_mmc_command(sc, &cmd); 137 if (error == ETIMEDOUT) { 138 /* No more cards there. */ 139 break; 140 } else if (error != 0) { 141 DPRINTF(("%s: can't read CID\n", SDMMCDEVNAME(sc))); 142 break; 143 } 144 145 /* In MMC mode, find the next available RCA. */ 146 next_rca = 1; 147 if (!ISSET(sc->sc_flags, SMF_SD_MODE)) 148 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) 149 next_rca++; 150 151 /* Allocate a sdmmc_function structure. */ 152 sf = sdmmc_function_alloc(sc); 153 sf->rca = next_rca; 154 155 /* 156 * Remember the CID returned in the CMD2 response for 157 * later decoding. 158 */ 159 bcopy(cmd.c_resp, sf->raw_cid, sizeof sf->raw_cid); 160 161 /* 162 * Silence the card by assigning it a unique RCA, or 163 * querying it for its RCA in the case of SD. 164 */ 165 if (sdmmc_set_relative_addr(sc, sf) != 0) { 166 printf("%s: can't set mem RCA\n", SDMMCDEVNAME(sc)); 167 sdmmc_function_free(sf); 168 break; 169 } 170 171 #if 0 172 /* Verify that the RCA has been set by selecting the card. */ 173 if (sdmmc_select_card(sc, sf) != 0) { 174 printf("%s: can't select mem RCA %d\n", 175 SDMMCDEVNAME(sc), sf->rca); 176 sdmmc_function_free(sf); 177 break; 178 } 179 180 /* Deselect. */ 181 (void)sdmmc_select_card(sc, NULL); 182 #endif 183 184 /* 185 * If this is a memory-only card, the card responding 186 * first becomes an alias for SDIO function 0. 187 */ 188 if (sc->sc_fn0 == NULL) 189 sc->sc_fn0 = sf; 190 191 SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list); 192 } 193 194 /* 195 * All cards are either inactive or awaiting further commands. 196 * Read the CSDs and decode the raw CID for each card. 197 */ 198 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) { 199 bzero(&cmd, sizeof cmd); 200 cmd.c_opcode = MMC_SEND_CSD; 201 cmd.c_arg = MMC_ARG_RCA(sf->rca); 202 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R2; 203 204 if (sdmmc_mmc_command(sc, &cmd) != 0) { 205 SET(sf->flags, SFF_ERROR); 206 continue; 207 } 208 209 if (sdmmc_decode_csd(sc, cmd.c_resp, sf) != 0 || 210 sdmmc_decode_cid(sc, sf->raw_cid, sf) != 0) { 211 SET(sf->flags, SFF_ERROR); 212 continue; 213 } 214 215 #ifdef SDMMC_DEBUG 216 printf("%s: CID: ", SDMMCDEVNAME(sc)); 217 sdmmc_print_cid(&sf->cid); 218 #endif 219 } 220 } 221 222 int 223 sdmmc_decode_csd(struct sdmmc_softc *sc, sdmmc_response resp, 224 struct sdmmc_function *sf) 225 { 226 struct sdmmc_csd *csd = &sf->csd; 227 228 if (ISSET(sc->sc_flags, SMF_SD_MODE)) { 229 /* 230 * CSD version 1.0 corresponds to SD system 231 * specification version 1.0 - 1.10. (SanDisk, 3.5.3) 232 */ 233 csd->csdver = SD_CSD_CSDVER(resp); 234 switch (csd->csdver) { 235 case SD_CSD_CSDVER_2_0: 236 sf->flags |= SFF_SDHC; 237 csd->capacity = SD_CSD_V2_CAPACITY(resp); 238 csd->read_bl_len = SD_CSD_V2_BL_LEN; 239 break; 240 case SD_CSD_CSDVER_1_0: 241 csd->capacity = SD_CSD_CAPACITY(resp); 242 csd->read_bl_len = SD_CSD_READ_BL_LEN(resp); 243 break; 244 default: 245 printf("%s: unknown SD CSD structure version 0x%x\n", 246 SDMMCDEVNAME(sc), csd->csdver); 247 return 1; 248 break; 249 } 250 251 } else { 252 csd->csdver = MMC_CSD_CSDVER(resp); 253 if (csd->csdver != MMC_CSD_CSDVER_1_0 && 254 csd->csdver != MMC_CSD_CSDVER_2_0) { 255 printf("%s: unknown MMC CSD structure version 0x%x\n", 256 SDMMCDEVNAME(sc), csd->csdver); 257 return 1; 258 } 259 260 csd->mmcver = MMC_CSD_MMCVER(resp); 261 csd->capacity = MMC_CSD_CAPACITY(resp); 262 csd->read_bl_len = MMC_CSD_READ_BL_LEN(resp); 263 } 264 csd->sector_size = MIN(1 << csd->read_bl_len, 265 sdmmc_chip_host_maxblklen(sc->sct, sc->sch)); 266 if (csd->sector_size < (1<<csd->read_bl_len)) 267 csd->capacity *= (1<<csd->read_bl_len) / 268 csd->sector_size; 269 270 return 0; 271 } 272 273 int 274 sdmmc_decode_cid(struct sdmmc_softc *sc, sdmmc_response resp, 275 struct sdmmc_function *sf) 276 { 277 struct sdmmc_cid *cid = &sf->cid; 278 279 if (ISSET(sc->sc_flags, SMF_SD_MODE)) { 280 cid->mid = SD_CID_MID(resp); 281 cid->oid = SD_CID_OID(resp); 282 SD_CID_PNM_CPY(resp, cid->pnm); 283 cid->rev = SD_CID_REV(resp); 284 cid->psn = SD_CID_PSN(resp); 285 cid->mdt = SD_CID_MDT(resp); 286 } else { 287 switch(sf->csd.mmcver) { 288 case MMC_CSD_MMCVER_1_0: 289 case MMC_CSD_MMCVER_1_4: 290 cid->mid = MMC_CID_MID_V1(resp); 291 MMC_CID_PNM_V1_CPY(resp, cid->pnm); 292 cid->rev = MMC_CID_REV_V1(resp); 293 cid->psn = MMC_CID_PSN_V1(resp); 294 cid->mdt = MMC_CID_MDT_V1(resp); 295 break; 296 case MMC_CSD_MMCVER_2_0: 297 case MMC_CSD_MMCVER_3_1: 298 case MMC_CSD_MMCVER_4_0: 299 cid->mid = MMC_CID_MID_V2(resp); 300 cid->oid = MMC_CID_OID_V2(resp); 301 MMC_CID_PNM_V2_CPY(resp, cid->pnm); 302 cid->psn = MMC_CID_PSN_V2(resp); 303 break; 304 default: 305 printf("%s: unknown MMC version %d\n", 306 SDMMCDEVNAME(sc), sf->csd.mmcver); 307 return 1; 308 } 309 } 310 return 0; 311 } 312 313 #ifdef SDMMC_DEBUG 314 void 315 sdmmc_print_cid(struct sdmmc_cid *cid) 316 { 317 printf("mid=0x%02x oid=0x%04x pnm=\"%s\" rev=0x%02x psn=0x%08x" 318 " mdt=%03x\n", cid->mid, cid->oid, cid->pnm, cid->rev, cid->psn, 319 cid->mdt); 320 } 321 #endif 322 323 /* 324 * Initialize a SD/MMC memory card. 325 */ 326 int 327 sdmmc_mem_init(struct sdmmc_softc *sc, struct sdmmc_function *sf) 328 { 329 int error = 0; 330 331 SDMMC_ASSERT_LOCKED(sc); 332 333 if (sdmmc_select_card(sc, sf) != 0 || 334 sdmmc_mem_set_blocklen(sc, sf) != 0) 335 error = 1; 336 return error; 337 } 338 339 /* 340 * Get or set the card's memory OCR value (SD or MMC). 341 */ 342 int 343 sdmmc_mem_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, 344 u_int32_t *ocrp) 345 { 346 struct sdmmc_command cmd; 347 int error; 348 int i; 349 350 SDMMC_ASSERT_LOCKED(sc); 351 352 /* 353 * If we change the OCR value, retry the command until the OCR 354 * we receive in response has the "CARD BUSY" bit set, meaning 355 * that all cards are ready for identification. 356 */ 357 for (i = 0; i < 100; i++) { 358 bzero(&cmd, sizeof cmd); 359 cmd.c_arg = ocr; 360 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R3; 361 362 if (ISSET(sc->sc_flags, SMF_SD_MODE)) { 363 cmd.c_opcode = SD_APP_OP_COND; 364 error = sdmmc_app_command(sc, &cmd); 365 } else { 366 cmd.c_opcode = MMC_SEND_OP_COND; 367 error = sdmmc_mmc_command(sc, &cmd); 368 } 369 if (error != 0) 370 break; 371 if (ISSET(MMC_R3(cmd.c_resp), MMC_OCR_MEM_READY) || 372 ocr == 0) 373 break; 374 error = ETIMEDOUT; 375 sdmmc_delay(10000); 376 } 377 if (error == 0 && ocrp != NULL) 378 *ocrp = MMC_R3(cmd.c_resp); 379 380 return error; 381 } 382 383 /* 384 * Set the read block length appropriately for this card, according to 385 * the card CSD register value. 386 */ 387 int 388 sdmmc_mem_set_blocklen(struct sdmmc_softc *sc, struct sdmmc_function *sf) 389 { 390 struct sdmmc_command cmd; 391 392 SDMMC_ASSERT_LOCKED(sc); 393 394 bzero(&cmd, sizeof cmd); 395 cmd.c_opcode = MMC_SET_BLOCKLEN; 396 cmd.c_arg = sf->csd.sector_size; 397 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 398 DPRINTF(("%s: read_bl_len=%d sector_size=%d\n", SDMMCDEVNAME(sc), 399 1 << sf->csd.read_bl_len, sf->csd.sector_size)); 400 401 return sdmmc_mmc_command(sc, &cmd); 402 } 403 404 int 405 sdmmc_mem_read_block(struct sdmmc_function *sf, int blkno, u_char *data, 406 size_t datalen) 407 { 408 struct sdmmc_softc *sc = sf->sc; 409 struct sdmmc_command cmd; 410 int error; 411 412 SDMMC_LOCK(sc); 413 414 if ((error = sdmmc_select_card(sc, sf)) != 0) 415 goto err; 416 417 bzero(&cmd, sizeof cmd); 418 cmd.c_data = data; 419 cmd.c_datalen = datalen; 420 cmd.c_blklen = sf->csd.sector_size; 421 cmd.c_opcode = (datalen / cmd.c_blklen) > 1 ? 422 MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE; 423 if (sf->flags & SFF_SDHC) 424 cmd.c_arg = blkno; 425 else 426 cmd.c_arg = blkno << 9; 427 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1; 428 429 error = sdmmc_mmc_command(sc, &cmd); 430 if (error != 0) 431 goto err; 432 433 if (ISSET(sc->sc_flags, SMF_STOP_AFTER_MULTIPLE) && 434 cmd.c_opcode == MMC_READ_BLOCK_MULTIPLE) { 435 bzero(&cmd, sizeof cmd); 436 cmd.c_opcode = MMC_STOP_TRANSMISSION; 437 cmd.c_arg = MMC_ARG_RCA(sf->rca); 438 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B; 439 error = sdmmc_mmc_command(sc, &cmd); 440 if (error != 0) 441 goto err; 442 } 443 444 do { 445 bzero(&cmd, sizeof cmd); 446 cmd.c_opcode = MMC_SEND_STATUS; 447 cmd.c_arg = MMC_ARG_RCA(sf->rca); 448 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 449 error = sdmmc_mmc_command(sc, &cmd); 450 if (error != 0) 451 break; 452 /* XXX time out */ 453 } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA)); 454 455 err: 456 SDMMC_UNLOCK(sc); 457 return error; 458 } 459 460 int 461 sdmmc_mem_write_block(struct sdmmc_function *sf, int blkno, u_char *data, 462 size_t datalen) 463 { 464 struct sdmmc_softc *sc = sf->sc; 465 struct sdmmc_command cmd; 466 int error; 467 468 SDMMC_LOCK(sc); 469 470 if ((error = sdmmc_select_card(sc, sf)) != 0) 471 goto err; 472 473 bzero(&cmd, sizeof cmd); 474 cmd.c_data = data; 475 cmd.c_datalen = datalen; 476 cmd.c_blklen = sf->csd.sector_size; 477 cmd.c_opcode = (datalen / cmd.c_blklen) > 1 ? 478 MMC_WRITE_BLOCK_MULTIPLE : MMC_WRITE_BLOCK_SINGLE; 479 if (sf->flags & SFF_SDHC) 480 cmd.c_arg = blkno; 481 else 482 cmd.c_arg = blkno << 9; 483 cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R1; 484 485 error = sdmmc_mmc_command(sc, &cmd); 486 if (error != 0) 487 goto err; 488 489 if (ISSET(sc->sc_flags, SMF_STOP_AFTER_MULTIPLE) && 490 cmd.c_opcode == MMC_WRITE_BLOCK_MULTIPLE) { 491 bzero(&cmd, sizeof cmd); 492 cmd.c_opcode = MMC_STOP_TRANSMISSION; 493 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B; 494 error = sdmmc_mmc_command(sc, &cmd); 495 if (error != 0) 496 goto err; 497 } 498 499 do { 500 bzero(&cmd, sizeof cmd); 501 cmd.c_opcode = MMC_SEND_STATUS; 502 cmd.c_arg = MMC_ARG_RCA(sf->rca); 503 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 504 error = sdmmc_mmc_command(sc, &cmd); 505 if (error != 0) 506 break; 507 /* XXX time out */ 508 } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA)); 509 510 err: 511 SDMMC_UNLOCK(sc); 512 return error; 513 } 514