1 /*- 2 * Copyright (c) 2012 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Paul Fleischer <paul@xpg.dk> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * All SD/MMC code is taken from various files in sys/dev/sdmmc 32 */ 33 /* 34 * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org> 35 * 36 * Permission to use, copy, modify, and distribute this software for any 37 * purpose with or without fee is hereby granted, provided that the above 38 * copyright notice and this permission notice appear in all copies. 39 * 40 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 41 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 42 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 43 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 44 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 45 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 46 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 47 */ 48 49 /*- 50 * Copyright (c) 2007-2010 NONAKA Kimihiro <nonaka@netbsd.org> 51 * All rights reserved. 52 * 53 * Redistribution and use in source and binary forms, with or without 54 * modification, are permitted provided that the following conditions 55 * are met: 56 * 1. Redistributions of source code must retain the above copyright 57 * notice, this list of conditions and the following disclaimer. 58 * 2. Redistributions in binary form must reproduce the above copyright 59 * notice, this list of conditions and the following disclaimer in the 60 * documentation and/or other materials provided with the distribution. 61 * 62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 72 * SUCH DAMAGE. 73 */ 74 75 #include <machine/limits.h> 76 77 #include <sys/param.h> 78 #include <sys/types.h> 79 #include <sys/disklabel.h> 80 81 #include <netinet/in.h> 82 83 #include <lib/libsa/stand.h> 84 85 #include <lib/libkern/libkern.h> 86 #include <lib/libsa/stand.h> 87 #include <lib/libsa/iodesc.h> 88 89 #include <dev/sdmmc/sdmmcreg.h> 90 #include "dev_sdmmc.h" 91 #include "s3csdi.h" 92 93 //#define SDMMC_DEBUG 94 #ifdef SDMMC_DEBUG 95 #define DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0) 96 #else 97 #define DPRINTF(s) do {} while (/*CONSTCOND*/0) 98 #endif 99 100 /* SD/MMC device driver structure */ 101 struct sdifdv { 102 char* name; 103 int (*match)(unsigned); 104 void* (*init)(unsigned, uint32_t*); 105 int (*host_ocr)(void*); 106 int (*bus_clock)(void*, int); 107 int (*bus_power)(void*, int); 108 int (*bus_width)(void*, int); 109 void (*exec_cmd)(void*, struct sdmmc_command*); 110 int (*get_max_bus_clock)(void*); 111 void* priv; 112 }; 113 114 struct sdmmc_softc; 115 116 /* Structure used for of->f_devdata */ 117 struct sdmmc_part { 118 struct sdmmc_softc *sc; 119 struct partition *part; 120 }; 121 122 /* SD/MMC driver structure */ 123 struct sdmmc_softc { 124 uint32_t flags; 125 uint32_t caps; 126 uint16_t rca; /* relative card address */ 127 sdmmc_response raw_cid; /* temp. storage for decoding */ 128 uint32_t raw_scr[2]; 129 struct sdmmc_csd csd; /* decoded CSD value */ 130 struct sdmmc_cid cid; /* decoded CID value */ 131 struct sdmmc_scr scr; 132 int busclk; 133 struct sdifdv *sdifdv; 134 struct disklabel sc_label; 135 int npartitions; 136 struct sdmmc_part partitions[MAXPARTITIONS]; 137 }; 138 139 static struct sdifdv vnifdv[] = { 140 {"S3C SD/MMC", s3csd_match, s3csd_init, s3csd_host_ocr, 141 s3csd_bus_clock, s3csd_bus_power, s3csd_bus_width, s3csd_exec_cmd, 142 s3csd_get_max_bus_clock} 143 }; 144 static int nnifdv = sizeof(vnifdv)/sizeof(vnifdv[0]); 145 146 static struct sdmmc_softc sdmmc_softc; 147 static uint8_t sdmmc_initialized = FALSE; 148 149 extern time_t getsecs(); 150 extern time_t getusecs(); 151 extern void usleep(int); 152 153 /* Local functions */ 154 static int sdmmc_getdisklabel(struct sdmmc_softc *sc); 155 static int sdmmc_init(unsigned int tag); 156 static int sdmmc_enable(struct sdmmc_softc*); 157 158 static int sdmmc_mem_send_if_cond(struct sdmmc_softc*, uint32_t, uint32_t*); 159 static int sdmmc_mmc_command(struct sdmmc_softc*, struct sdmmc_command*); 160 static void sdmmc_go_idle_state(struct sdmmc_softc*); 161 static int sdmmc_mem_send_op_cond(struct sdmmc_softc*, uint32_t, uint32_t *); 162 static int sdmmc_set_bus_power(struct sdmmc_softc*, uint32_t, uint32_t); 163 static int sdmmc_app_command(struct sdmmc_softc*, uint16_t, 164 struct sdmmc_command*); 165 static int sdmmc_mmc_command(struct sdmmc_softc*, struct sdmmc_command*); 166 static int sdmmc_scan(struct sdmmc_softc*); 167 static void sdmmc_mem_scan(struct sdmmc_softc*); 168 static int sdmmc_set_relative_addr(struct sdmmc_softc*); 169 static int sdmmc_mem_send_cid(struct sdmmc_softc*, sdmmc_response*); 170 171 static int sdmmc_mem_send_csd(struct sdmmc_softc*, sdmmc_response*); 172 static int sdmmc_decode_csd(struct sdmmc_softc*, sdmmc_response); 173 static int sdmmc_decode_cid(struct sdmmc_softc*, sdmmc_response); 174 175 static int sdmmc_mem_read_block(struct sdmmc_softc*, uint32_t, u_char*, size_t); 176 static int sdmmc_select_card(struct sdmmc_softc*); 177 static int sdmmc_mem_set_blocklen(struct sdmmc_softc*); 178 179 static int sdmmc_mem_send_scr(struct sdmmc_softc*, uint32_t[2]); 180 static int sdmmc_mem_decode_scr(struct sdmmc_softc*); 181 static int sdmmc_set_bus_width(struct sdmmc_softc*, int); 182 static int sdmmc_mem_sd_switch(struct sdmmc_softc *, int, int, int, void*); 183 184 #ifdef SDMMC_DEBUG 185 static void sdmmc_dump_data(const char*, void*, size_t); 186 static void sdmmc_print_cid(struct sdmmc_cid*); 187 static void sdmmc_dump_command(struct sdmmc_softc*, struct sdmmc_command*); 188 #endif 189 190 int 191 sdmmc_open(struct open_file *of, ...) 192 { 193 va_list ap; 194 int unit __unused, part; 195 196 va_start(ap, of); 197 unit = va_arg(ap, u_int); /* Not used for now */ 198 part = va_arg(ap, u_int); 199 va_end(ap); 200 201 /* Simply try to initialize SD mem sub system. */ 202 if( !sdmmc_init(0) ) { 203 return 1; 204 } 205 206 of->f_devdata = (void*)&sdmmc_softc.partitions[part]; 207 208 return 0; 209 } 210 211 int 212 sdmmc_close(struct open_file *f) 213 { 214 return (0); 215 } 216 217 int 218 sdmmc_get_fstype(void *p) { 219 struct sdmmc_part *part = (struct sdmmc_part*)p; 220 221 return part->part->p_fstype; 222 } 223 224 225 int 226 sdmmc_strategy(void *d, int f, daddr_t b, size_t s, void *buf, size_t *r) 227 { 228 struct sdmmc_part *part = (struct sdmmc_part*)d; 229 unsigned int offset; 230 switch(f) { 231 case F_READ: 232 offset = part->part->p_offset + b; 233 *r = s; 234 if(sdmmc_mem_read_block(part->sc, offset, buf, s) == 0) 235 return 0; 236 else 237 return EIO; 238 default: 239 printf("Unsupported operation\n"); 240 break; 241 } 242 return (EIO); 243 } 244 245 int 246 sdmmc_getdisklabel(struct sdmmc_softc *sc) 247 { 248 char *msg; 249 int sector, i, n; 250 size_t rsize; 251 struct mbr_partition *dp, *bsdp; 252 struct disklabel *lp; 253 /*uint8_t *buf = wd->sc_buf;*/ 254 uint8_t buf[DEV_BSIZE]; 255 256 lp = &sc->sc_label; 257 memset(lp, 0, sizeof(struct disklabel)); 258 259 sector = 0; 260 if (sdmmc_strategy(&sc->partitions[0], F_READ, MBR_BBSECTOR, DEV_BSIZE, 261 buf, &rsize)) 262 return EOFFSET; 263 264 dp = (struct mbr_partition *)(buf + MBR_PART_OFFSET); 265 bsdp = NULL; 266 for (i = 0; i < MBR_PART_COUNT; i++, dp++) { 267 if (dp->mbrp_type == MBR_PTYPE_NETBSD) { 268 bsdp = dp; 269 break; 270 } 271 } 272 if (!bsdp) { 273 /* generate fake disklabel */ 274 lp->d_secsize = DEV_BSIZE; 275 /*lp->d_ntracks = wd->sc_params.atap_heads; 276 lp->d_nsectors = wd->sc_params.atap_sectors; 277 lp->d_ncylinders = wd->sc_params.atap_cylinders;*/ 278 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors; 279 lp->d_type = DKTYPE_FLASH; 280 /*strncpy(lp->d_typename, (char *)wd->sc_params.atap_model, 16);*/ 281 strncpy(lp->d_packname, "fictitious", 16); 282 /*if (wd->sc_capacity > UINT32_MAX) 283 lp->d_secperunit = UINT32_MAX; 284 else 285 lp->d_secperunit = wd->sc_capacity;*/ 286 lp->d_rpm = 3600; 287 lp->d_interleave = 1; 288 lp->d_flags = 0; 289 lp->d_partitions[RAW_PART].p_offset = 0; 290 lp->d_partitions[RAW_PART].p_size = 291 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE); 292 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED; 293 lp->d_magic = DISKMAGIC; 294 lp->d_magic2 = DISKMAGIC; 295 lp->d_checksum = dkcksum(lp); 296 297 dp = (struct mbr_partition *)(buf + MBR_PART_OFFSET); 298 n = 'e' - 'a'; 299 for (i = 0; i < MBR_PART_COUNT; i++, dp++) { 300 if (dp->mbrp_type == MBR_PTYPE_UNUSED) 301 continue; 302 lp->d_partitions[n].p_offset = bswap32(dp->mbrp_start); 303 lp->d_partitions[n].p_size = bswap32(dp->mbrp_size); 304 switch (dp->mbrp_type) { 305 case MBR_PTYPE_FAT12: 306 case MBR_PTYPE_FAT16S: 307 case MBR_PTYPE_FAT16B: 308 case MBR_PTYPE_FAT32: 309 case MBR_PTYPE_FAT32L: 310 case MBR_PTYPE_FAT16L: 311 lp->d_partitions[n].p_fstype = FS_MSDOS; 312 break; 313 case MBR_PTYPE_LNXEXT2: 314 lp->d_partitions[n].p_fstype = FS_EX2FS; 315 break; 316 default: 317 lp->d_partitions[n].p_fstype = FS_OTHER; 318 break; 319 } 320 n += 1; 321 } 322 lp->d_npartitions = n; 323 } 324 else { 325 sector = bsdp->mbrp_start; 326 if (sdmmc_strategy(&sc->partitions[0], F_READ, 327 sector + LABELSECTOR, DEV_BSIZE, 328 buf, &rsize)) 329 return EOFFSET; 330 msg = getdisklabel((char *)buf + LABELOFFSET, &sc->sc_label); 331 if (msg != NULL) 332 printf("getdisklabel: %s\n", msg); 333 } 334 /*DPRINTF(("label info: d_secsize %d, d_nsectors %d, d_ncylinders %d," 335 "d_ntracks %d, d_secpercyl %d\n", 336 wd->sc_label.d_secsize, 337 wd->sc_label.d_nsectors, 338 wd->sc_label.d_ncylinders, 339 wd->sc_label.d_ntracks, 340 wd->sc_label.d_secpercyl));*/ 341 342 return 0; 343 } 344 345 void 346 sdmmc_delay(int us) { 347 usleep(us); 348 } 349 350 /* Initialize the SD/MMC subsystem. Return 1 on success, and 0 on error. 351 In case of error, errno will be set to a sane value. 352 */ 353 int 354 sdmmc_init(unsigned int tag) 355 { 356 struct sdifdv *dv; 357 int n; 358 int error; 359 struct sdmmc_softc *sc = &sdmmc_softc; 360 char status[64]; 361 362 if (sdmmc_initialized) { 363 printf("SD/MMC already initialized\n"); 364 return 1; 365 } 366 367 for (n = 0; n < nnifdv; n++) { 368 dv = &vnifdv[n]; 369 if ((*dv->match)(tag) > 0) 370 goto found; 371 } 372 errno = ENODEV; 373 return 0; 374 found: 375 sc->caps = 0; 376 /* Init should return NULL if no card is present. */ 377 sc->sdifdv->priv = (*dv->init)(tag, &sc->caps); 378 if (sc->sdifdv->priv == NULL) { 379 /* We expect that the device initialization sets 380 errno properly */ 381 return 0; 382 } 383 384 sc->flags = 0; 385 sc->sdifdv = dv; 386 387 /* Perform SD-card initialization. */ 388 if( sdmmc_enable(sc) ) { 389 printf("Failed to enable SD interface\n"); 390 errno = EIO; 391 return 0; 392 } 393 sc->busclk = sc->sdifdv->get_max_bus_clock(sc->sdifdv->priv); 394 395 if (sdmmc_scan(sc)) { 396 printf("No functions\n"); 397 errno = EIO; 398 return 0; 399 } 400 401 if (sdmmc_select_card(sc)) { 402 printf("Failed to select card\n"); 403 errno = EIO; 404 return 0; 405 } 406 407 if (!ISSET(sc->flags, SMF_CARD_SDHC)) { 408 sdmmc_mem_set_blocklen(sc); 409 } 410 411 /* change bus width if supported */ 412 if (ISSET(sc->flags, SMF_SD_MODE) ) { 413 error = sdmmc_mem_send_scr(sc, sc->raw_scr); 414 if (error) { 415 DPRINTF(("SD_SEND_SCR send failed.\n")); 416 errno = EIO; 417 return 0; 418 } 419 error = sdmmc_mem_decode_scr(sc); 420 if (error) { 421 errno = EIO; 422 return 0; 423 } 424 425 if (ISSET(sc->caps, SMC_CAPS_4BIT_MODE) && 426 ISSET(sc->scr.bus_width, SCR_SD_BUS_WIDTHS_4BIT)) { 427 error = sdmmc_set_bus_width(sc, 4); 428 if (error) { 429 DPRINTF(("can't change bus width" 430 " (%d bit)\n", 4)); 431 errno = EIO; 432 return 0; 433 } 434 } 435 436 #if 1 437 if (sc->scr.sd_spec >= SCR_SD_SPEC_VER_1_10 && 438 ISSET(sc->csd.ccc, SD_CSD_CCC_SWITCH)) { 439 DPRINTF(("switch func mode 0\n")); 440 error = sdmmc_mem_sd_switch(sc, 0, 1, 0, status); 441 if (error) { 442 printf("switch func mode 0 failed\n"); 443 errno = error; 444 return 0; 445 } 446 } 447 #endif 448 sc->sdifdv->bus_clock(sc->sdifdv->priv, sc->busclk); 449 } 450 451 /* Prepare dummy partition[0] entry used by sdmmc_getdisklabel() */ 452 sc->partitions[0].sc = sc; 453 sc->partitions[0].part->p_offset = 0; 454 455 if(sdmmc_getdisklabel(sc)) { 456 errno = EOFFSET; 457 return 0; 458 } 459 460 sc->npartitions = sc->sc_label.d_npartitions; 461 for(n=0; n<sc->sc_label.d_npartitions; n++) { 462 sc->partitions[n].part = &sc->sc_label.d_partitions[n]; 463 sc->partitions[n].sc = sc; 464 } 465 466 sdmmc_initialized = TRUE; 467 468 return 1; 469 } 470 471 int 472 sdmmc_enable(struct sdmmc_softc *sc) 473 { 474 uint32_t card_ocr; 475 uint32_t ocr = 0; 476 uint32_t host_ocr; 477 int error; 478 479 /* 1. Set the maximum power supported by bus */ 480 /* For now, we expect the init function to set the maximum 481 voltage. And if that is not supported by the SD-card we 482 just cannot work with it. 483 */ 484 485 sc->busclk = 400; 486 /* 2. Clock bus at minimum frequency */ 487 sc->sdifdv->bus_clock(sc->sdifdv->priv, 400); 488 489 /* We expect that the above call has performed any waiting needed.*/ 490 491 /* Initialize SD/MMC memory card(s), which is the only thing 492 we support. 493 */ 494 495 /* Set host mode to SD "combo" card or SD memory-only. */ 496 SET(sc->flags, SMF_SD_MODE|SMF_MEM_MODE); 497 498 sdmmc_go_idle_state(sc); 499 500 error = sdmmc_mem_send_if_cond(sc, 0x1aa, &card_ocr); 501 if (error == 0 && card_ocr == 0x1aa) 502 SET(ocr, MMC_OCR_HCS); 503 504 /* 505 * Read the SD/MMC memory OCR value by issuing CMD55 followed 506 * by ACMD41 to read the OCR value from memory-only SD cards. 507 * MMC cards will not respond to CMD55 or ACMD41 and this is 508 * how we distinguish them from SD cards. 509 */ 510 mmc_mode: 511 error = sdmmc_mem_send_op_cond(sc, 512 ISSET(sc->caps, SMC_CAPS_SPI_MODE) ? ocr : 0, &card_ocr); 513 if (error) { 514 if (ISSET(sc->flags, SMF_SD_MODE) && 515 !ISSET(sc->flags, SMF_IO_MODE)) { 516 /* Not a SD card, switch to MMC mode. */ 517 DPRINTF(("Switch to MMC mode\n")); 518 CLR(sc->flags, SMF_SD_MODE); 519 goto mmc_mode; 520 } 521 if (!ISSET(sc->flags, SMF_SD_MODE)) { 522 DPRINTF(("couldn't read memory OCR\n")); 523 goto out; 524 } else { 525 /* Not a "combo" card. */ 526 CLR(sc->flags, SMF_MEM_MODE); 527 error = 0; 528 goto out; 529 } 530 } 531 #if 0 /* SPI NOT SUPPORTED */ 532 if (ISSET(ssc->caps, SMC_CAPS_SPI_MODE)) { 533 /* get card OCR */ 534 error = sdmmc_mem_spi_read_ocr(sc, ocr, &card_ocr); 535 if (error) { 536 DPRINTF(("%s: couldn't read SPI memory OCR\n", 537 SDMMCDEVNAME(sc))); 538 goto out; 539 } 540 } 541 #endif 542 543 /* Set the lowest voltage supported by the card and host. */ 544 host_ocr = sc->sdifdv->host_ocr(sc->sdifdv->priv); 545 error = sdmmc_set_bus_power(sc, host_ocr, card_ocr); 546 if (error) { 547 DPRINTF(("Couldn't supply voltage requested by card\n")); 548 goto out; 549 } 550 host_ocr &= card_ocr; 551 host_ocr |= ocr; 552 553 /* Send the new OCR value until all cards are ready. */ 554 error = sdmmc_mem_send_op_cond(sc, host_ocr, NULL); 555 if (error) { 556 DPRINTF(("Couldn't send memory OCR\n")); 557 goto out; 558 } 559 560 out: 561 return error; 562 } 563 564 int 565 sdmmc_mem_send_if_cond(struct sdmmc_softc *sc, uint32_t ocr, uint32_t *ocrp) 566 { 567 struct sdmmc_command cmd; 568 int error; 569 570 memset(&cmd, 0, sizeof(cmd)); 571 cmd.c_arg = ocr; 572 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R7 | SCF_RSP_SPI_R7; 573 cmd.c_opcode = SD_SEND_IF_COND; 574 575 error = sdmmc_mmc_command(sc, &cmd); 576 if (error == 0 && ocrp != NULL) { 577 *ocrp = MMC_R7(cmd.c_resp); 578 } 579 580 return error; 581 } 582 583 void 584 sdmmc_go_idle_state(struct sdmmc_softc *sc) 585 { 586 struct sdmmc_command cmd; 587 588 memset(&cmd, 0, sizeof(cmd)); 589 cmd.c_opcode = MMC_GO_IDLE_STATE; 590 cmd.c_flags = SCF_CMD_BC | SCF_RSP_R0 | SCF_RSP_SPI_R1; 591 592 (void)sdmmc_mmc_command(sc, &cmd); 593 } 594 int 595 sdmmc_mem_send_op_cond(struct sdmmc_softc *sc, uint32_t ocr, uint32_t *ocrp) 596 { 597 struct sdmmc_command cmd; 598 int error; 599 int retry; 600 601 602 /* 603 * If we change the OCR value, retry the command until the OCR 604 * we receive in response has the "CARD BUSY" bit set, meaning 605 * that all cards are ready for identification. 606 */ 607 for (retry = 0; retry < 100; retry++) { 608 memset(&cmd, 0, sizeof(cmd)); 609 cmd.c_arg = !ISSET(sc->caps, SMC_CAPS_SPI_MODE) ? 610 ocr : (ocr & MMC_OCR_HCS); 611 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R3 | SCF_RSP_SPI_R1; 612 613 if (ISSET(sc->flags, SMF_SD_MODE)) { 614 cmd.c_opcode = SD_APP_OP_COND; 615 error = sdmmc_app_command(sc, 0, &cmd); 616 } else { 617 cmd.c_opcode = MMC_SEND_OP_COND; 618 error = sdmmc_mmc_command(sc, &cmd); 619 } 620 if (error) 621 break; 622 623 if (ISSET(sc->caps, SMC_CAPS_SPI_MODE)) { 624 if (!ISSET(MMC_SPI_R1(cmd.c_resp), R1_SPI_IDLE)) 625 break; 626 } else { 627 if (ISSET(MMC_R3(cmd.c_resp), MMC_OCR_MEM_READY) || 628 ocr == 0) 629 break; 630 } 631 632 error = ETIMEDOUT; 633 sdmmc_delay(10000); 634 } 635 if (error == 0 && 636 ocrp != NULL && 637 !ISSET(sc->caps, SMC_CAPS_SPI_MODE)) 638 *ocrp = MMC_R3(cmd.c_resp); 639 DPRINTF(("sdmmc_mem_send_op_cond: error=%d, ocr=%x\n", 640 error, MMC_R3(cmd.c_resp))); 641 return error; 642 } 643 644 /* 645 * Set the lowest bus voltage supported by the card and the host. 646 */ 647 int 648 sdmmc_set_bus_power(struct sdmmc_softc *sc, uint32_t host_ocr, 649 uint32_t card_ocr) 650 { 651 uint32_t bit; 652 653 /* Mask off unsupported voltage levels and select the lowest. */ 654 DPRINTF(("host_ocr=%x ", host_ocr)); 655 host_ocr &= card_ocr; 656 for (bit = 4; bit < 23; bit++) { 657 if (ISSET(host_ocr, (1 << bit))) { 658 host_ocr &= (3 << bit); 659 break; 660 } 661 } 662 DPRINTF(("card_ocr=%x new_ocr=%x\n", card_ocr, host_ocr)); 663 664 if (host_ocr == 0 || 665 sc->sdifdv->bus_power(sc->sdifdv->priv, host_ocr) != 0) 666 return 1; 667 return 0; 668 } 669 670 int 671 sdmmc_app_command(struct sdmmc_softc *sc, uint16_t rca, 672 struct sdmmc_command *cmd) 673 { 674 struct sdmmc_command acmd; 675 int error; 676 677 memset(&acmd, 0, sizeof(acmd)); 678 acmd.c_opcode = MMC_APP_CMD; 679 if (rca != 0) { 680 acmd.c_arg = rca << 16; 681 acmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1; 682 } else { 683 acmd.c_arg = 0; 684 acmd.c_flags = SCF_CMD_BCR | SCF_RSP_R1 | SCF_RSP_SPI_R1; 685 } 686 687 error = sdmmc_mmc_command(sc, &acmd); 688 if (error == 0) { 689 if (!ISSET(sc->caps, SMC_CAPS_SPI_MODE) && 690 !ISSET(MMC_R1(acmd.c_resp), MMC_R1_APP_CMD)) { 691 /* Card does not support application commands. */ 692 error = ENODEV; 693 } else { 694 error = sdmmc_mmc_command(sc, cmd); 695 } 696 } 697 DPRINTF(("sdmmc_app_command: done (error=%d)\n", error)); 698 return error; 699 } 700 701 void 702 sdmmc_dump_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd) 703 { 704 int i; 705 706 printf("cmd %u arg=%x data=%p dlen=%d flags=%x (error %d)\n", 707 cmd->c_opcode, cmd->c_arg, cmd->c_data, 708 cmd->c_datalen, cmd->c_flags, cmd->c_error); 709 710 if (cmd->c_error ) 711 return; 712 713 printf("resp="); 714 if (ISSET(cmd->c_flags, SCF_RSP_136)) 715 for (i = 0; i < sizeof cmd->c_resp; i++) 716 printf("%02x ", ((uint8_t *)cmd->c_resp)[i]); 717 else if (ISSET(cmd->c_flags, SCF_RSP_PRESENT)) 718 for (i = 0; i < 4; i++) 719 printf("%02x ", ((uint8_t *)cmd->c_resp)[i]); 720 else 721 printf("none"); 722 printf("\n"); 723 } 724 725 int 726 sdmmc_mmc_command(struct sdmmc_softc *sc, struct sdmmc_command *cmd) 727 { 728 int error; 729 730 DPRINTF(("sdmmc_mmc_command: cmd=%d, arg=%x, flags=%x\n", 731 cmd->c_opcode, cmd->c_arg, cmd->c_flags)); 732 733 #if 0 734 #if defined(DIAGNOSTIC) || defined(SDMMC_DEBUG) 735 if (cmd->c_data && !ISSET(sc->caps, SMC_CAPS_SPI_MODE)) { 736 if (sc->sc_card == NULL) 737 panic("%s: deselected card\n", DEVNAME(sc)); 738 } 739 #endif 740 #endif 741 742 sc->sdifdv->exec_cmd(sc->sdifdv->priv, cmd); 743 744 #ifdef SDMMC_DEBUG 745 746 sdmmc_dump_command(sc, cmd); 747 748 #endif 749 750 error = cmd->c_error; 751 752 DPRINTF(("sdmmc_mmc_command: error=%d\n", error)); 753 754 return error; 755 } 756 757 /* 758 * Scan for I/O functions and memory cards on the bus, allocating a 759 * sdmmc_function structure for each. 760 */ 761 int 762 sdmmc_scan(struct sdmmc_softc *sc) 763 { 764 765 #if 0 /* SPI NOT SUPPORTED */ 766 if (!ISSET(sc->caps, SMC_CAPS_SPI_MODE)) { 767 /* Scan for I/O functions. */ 768 if (ISSET(sc->sc_flags, SMF_IO_MODE)) 769 sdmmc_io_scan(sc); 770 } 771 #endif 772 773 /* Scan for memory cards on the bus. */ 774 if (ISSET(sc->flags, SMF_MEM_MODE)) 775 sdmmc_mem_scan(sc); 776 777 DPRINTF(("Bus clock speed: %d\n", sc->busclk)); 778 return sc->sdifdv->bus_clock(sc->sdifdv->priv, sc->busclk); 779 } 780 781 /* 782 * Read the CSD and CID from all cards and assign each card a unique 783 * relative card address (RCA). CMD2 is ignored by SDIO-only cards. 784 */ 785 void 786 sdmmc_mem_scan(struct sdmmc_softc *sc) 787 { 788 sdmmc_response resp; 789 //struct sdmmc_function *sf; 790 // uint16_t next_rca; 791 int error; 792 int retry; 793 794 /* 795 * CMD2 is a broadcast command understood by SD cards and MMC 796 * cards. All cards begin to respond to the command, but back 797 * off if another card drives the CMD line to a different level. 798 * Only one card will get its entire response through. That 799 * card remains silent once it has been assigned a RCA. 800 */ 801 for (retry = 0; retry < 100; retry++) { 802 error = sdmmc_mem_send_cid(sc, &resp); 803 if (error) { 804 if (!ISSET(sc->caps, SMC_CAPS_SPI_MODE) && 805 error == ETIMEDOUT) { 806 /* No more cards there. */ 807 break; 808 } 809 DPRINTF(("Couldn't read CID\n")); 810 break; 811 } 812 813 /* In MMC mode, find the next available RCA. */ 814 /*next_rca = 1; 815 if (!ISSET(dv->flags, SMF_SD_MODE)) { 816 SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) 817 next_rca++; 818 }*/ 819 820 /* Allocate a sdmmc_function structure. */ 821 /*sf = sdmmc_function_alloc(sc); 822 sf->rca = next_rca;*/ 823 824 /* 825 * Remember the CID returned in the CMD2 response for 826 * later decoding. 827 */ 828 memcpy(sc->raw_cid, resp, sizeof(sc->raw_cid)); 829 830 /* 831 * Silence the card by assigning it a unique RCA, or 832 * querying it for its RCA in the case of SD. 833 */ 834 if (!ISSET(sc->caps, SMC_CAPS_SPI_MODE)) { 835 if (sdmmc_set_relative_addr(sc) != 0) { 836 DPRINTF(("couldn't set mem RCA\n")); 837 break; 838 } 839 } 840 841 /* 842 * If this is a memory-only card, the card responding 843 * first becomes an alias for SDIO function 0. 844 */ 845 /*if (sc->sc_fn0 == NULL) 846 sc->sc_fn0 = sf; 847 848 SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);*/ 849 850 /* only one function in SPI mode */ 851 /*if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) 852 break;*/ 853 } 854 855 /* 856 * All cards are either inactive or awaiting further commands. 857 * Read the CSDs and decode the raw CID for each card. 858 */ 859 /* SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {*/ 860 error = sdmmc_mem_send_csd(sc, &resp); 861 if (error) { 862 /*SET(sf->flags, SFF_ERROR); 863 continue;*/ 864 } 865 866 if (sdmmc_decode_csd(sc, resp) != 0 || 867 sdmmc_decode_cid(sc, sc->raw_cid) != 0) { 868 /*SET(sf->flags, SFF_ERROR); 869 continue;*/ 870 } 871 872 #ifdef SDMMC_DEBUG 873 printf("CID: "); 874 sdmmc_print_cid(&sc->cid); 875 #endif 876 /* }*/ 877 } 878 879 /* 880 * Retrieve (SD) or set (MMC) the relative card address (RCA). 881 */ 882 int 883 sdmmc_set_relative_addr(struct sdmmc_softc *sc) 884 { 885 struct sdmmc_command cmd; 886 int error; 887 888 /* Don't lock */ 889 890 if (ISSET(sc->caps, SMC_CAPS_SPI_MODE)) 891 return EIO; 892 893 memset(&cmd, 0, sizeof(cmd)); 894 if (ISSET(sc->flags, SMF_SD_MODE)) { 895 cmd.c_opcode = SD_SEND_RELATIVE_ADDR; 896 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R6; 897 } else { 898 cmd.c_opcode = MMC_SET_RELATIVE_ADDR; 899 cmd.c_arg = MMC_ARG_RCA(sc->rca); 900 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1; 901 } 902 error = sdmmc_mmc_command(sc, &cmd); 903 if (error) 904 return error; 905 906 if (ISSET(sc->flags, SMF_SD_MODE)) 907 sc->rca = SD_R6_RCA(cmd.c_resp); 908 909 return 0; 910 } 911 912 int 913 sdmmc_mem_send_cid(struct sdmmc_softc *sc, sdmmc_response *resp) 914 { 915 struct sdmmc_command cmd; 916 int error; 917 918 919 memset(&cmd, 0, sizeof cmd); 920 cmd.c_opcode = MMC_ALL_SEND_CID; 921 cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R2; 922 923 error = sdmmc_mmc_command(sc, &cmd); 924 925 #ifdef SDMMC_DEBUG 926 sdmmc_dump_data("CID", cmd.c_resp, sizeof(cmd.c_resp)); 927 #endif 928 if (error == 0 && resp != NULL) 929 memcpy(resp, &cmd.c_resp, sizeof(*resp)); 930 return error; 931 } 932 933 void 934 sdmmc_dump_data(const char *title, void *ptr, size_t size) 935 { 936 char buf[16]; 937 uint8_t *p = ptr; 938 int i, j; 939 940 printf("sdmmc_dump_data: %s\n", title ? title : ""); 941 printf("--------+--------------------------------------------------+------------------+\n"); 942 printf("offset | +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f | data |\n"); 943 printf("--------+--------------------------------------------------+------------------+\n"); 944 for (i = 0; i < (int)size; i++) { 945 if ((i % 16) == 0) { 946 printf("%08x| ", i); 947 } else if ((i % 16) == 8) { 948 printf(" "); 949 } 950 951 printf("%02x ", p[i]); 952 buf[i % 16] = p[i]; 953 954 if ((i % 16) == 15) { 955 printf("| "); 956 for (j = 0; j < 16; j++) { 957 if (buf[j] >= 0x20 && buf[j] <= 0x7e) { 958 printf("%c", buf[j]); 959 } else { 960 printf("."); 961 } 962 } 963 printf(" |\n"); 964 } 965 } 966 if ((i % 16) != 0) { 967 j = (i % 16); 968 for (; j < 16; j++) { 969 printf(" "); 970 if ((j % 16) == 8) { 971 printf(" "); 972 } 973 } 974 975 printf("| "); 976 for (j = 0; j < (i % 16); j++) { 977 if (buf[j] >= 0x20 && buf[j] <= 0x7e) { 978 printf("%c", buf[j]); 979 } else { 980 printf("."); 981 } 982 } 983 for (; j < 16; j++) { 984 printf(" "); 985 } 986 printf(" |\n"); 987 } 988 printf("--------+--------------------------------------------------+------------------+\n"); 989 } 990 991 int 992 sdmmc_mem_send_csd(struct sdmmc_softc *sc, sdmmc_response *resp) 993 { 994 struct sdmmc_command cmd; 995 int error; 996 997 memset(&cmd, 0, sizeof cmd); 998 cmd.c_opcode = MMC_SEND_CSD; 999 cmd.c_arg = MMC_ARG_RCA(sc->rca); 1000 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R2; 1001 1002 error = sdmmc_mmc_command(sc, &cmd); 1003 1004 #ifdef SDMMC_DEBUG 1005 sdmmc_dump_data("CSD", cmd.c_resp, sizeof(cmd.c_resp)); 1006 #endif 1007 if (error == 0 && resp != NULL) 1008 memcpy(resp, &cmd.c_resp, sizeof(*resp)); 1009 return error; 1010 } 1011 1012 int 1013 sdmmc_decode_csd(struct sdmmc_softc *sc, sdmmc_response resp) 1014 { 1015 /* TRAN_SPEED(2:0): transfer rate exponent */ 1016 static const int speed_exponent[8] = { 1017 100 * 1, /* 100 Kbits/s */ 1018 1 * 1000, /* 1 Mbits/s */ 1019 10 * 1000, /* 10 Mbits/s */ 1020 100 * 1000, /* 100 Mbits/s */ 1021 0, 1022 0, 1023 0, 1024 0, 1025 }; 1026 /* TRAN_SPEED(6:3): time mantissa */ 1027 static const int speed_mantissa[16] = { 1028 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80, 1029 }; 1030 struct sdmmc_csd *csd = &sc->csd; 1031 int e, m; 1032 1033 if (ISSET(sc->flags, SMF_SD_MODE)) { 1034 /* 1035 * CSD version 1.0 corresponds to SD system 1036 * specification version 1.0 - 1.10. (SanDisk, 3.5.3) 1037 */ 1038 csd->csdver = SD_CSD_CSDVER(resp); 1039 switch (csd->csdver) { 1040 case SD_CSD_CSDVER_2_0: 1041 DPRINTF(("SD Ver.2.0\n")); 1042 SET(sc->flags, SMF_CARD_SDHC); 1043 csd->capacity = SD_CSD_V2_CAPACITY(resp); 1044 csd->read_bl_len = SD_CSD_V2_BL_LEN; 1045 csd->ccc = SD_CSD_CCC(resp); 1046 break; 1047 1048 case SD_CSD_CSDVER_1_0: 1049 DPRINTF(("SD Ver.1.0\n")); 1050 csd->capacity = SD_CSD_CAPACITY(resp); 1051 csd->read_bl_len = SD_CSD_READ_BL_LEN(resp); 1052 break; 1053 1054 default: 1055 printf("unknown SD CSD structure version 0x%x\n", 1056 csd->csdver); 1057 return 1; 1058 } 1059 1060 csd->mmcver = SD_CSD_MMCVER(resp); 1061 csd->write_bl_len = SD_CSD_WRITE_BL_LEN(resp); 1062 csd->r2w_factor = SD_CSD_R2W_FACTOR(resp); 1063 e = SD_CSD_SPEED_EXP(resp); 1064 m = SD_CSD_SPEED_MANT(resp); 1065 csd->tran_speed = speed_exponent[e] * speed_mantissa[m] / 10; 1066 } else { 1067 csd->csdver = MMC_CSD_CSDVER(resp); 1068 if (csd->csdver == MMC_CSD_CSDVER_1_0 ) { 1069 printf("unknown MMC CSD structure version 0x%x\n", 1070 csd->csdver); 1071 return 1; 1072 } 1073 1074 csd->mmcver = MMC_CSD_MMCVER(resp); 1075 csd->capacity = MMC_CSD_CAPACITY(resp); 1076 csd->read_bl_len = MMC_CSD_READ_BL_LEN(resp); 1077 csd->write_bl_len = MMC_CSD_WRITE_BL_LEN(resp); 1078 csd->r2w_factor = MMC_CSD_R2W_FACTOR(resp); 1079 e = MMC_CSD_TRAN_SPEED_EXP(resp); 1080 m = MMC_CSD_TRAN_SPEED_MANT(resp); 1081 csd->tran_speed = speed_exponent[e] * speed_mantissa[m] / 10; 1082 } 1083 if ((1 << csd->read_bl_len) > SDMMC_SECTOR_SIZE) 1084 csd->capacity *= (1 << csd->read_bl_len) / SDMMC_SECTOR_SIZE; 1085 1086 1087 if (sc->busclk > csd->tran_speed) 1088 sc->busclk = csd->tran_speed; 1089 1090 #ifdef SDMMC_DUMP_CSD 1091 sdmmc_print_csd(resp, csd); 1092 #endif 1093 1094 return 0; 1095 } 1096 1097 int 1098 sdmmc_decode_cid(struct sdmmc_softc *sc, sdmmc_response resp) 1099 { 1100 struct sdmmc_cid *cid = &sc->cid; 1101 1102 if (ISSET(sc->flags, SMF_SD_MODE)) { 1103 cid->mid = SD_CID_MID(resp); 1104 cid->oid = SD_CID_OID(resp); 1105 SD_CID_PNM_CPY(resp, cid->pnm); 1106 cid->rev = SD_CID_REV(resp); 1107 cid->psn = SD_CID_PSN(resp); 1108 cid->mdt = SD_CID_MDT(resp); 1109 } else { 1110 switch(sc->csd.mmcver) { 1111 case MMC_CSD_MMCVER_1_0: 1112 case MMC_CSD_MMCVER_1_4: 1113 cid->mid = MMC_CID_MID_V1(resp); 1114 MMC_CID_PNM_V1_CPY(resp, cid->pnm); 1115 cid->rev = MMC_CID_REV_V1(resp); 1116 cid->psn = MMC_CID_PSN_V1(resp); 1117 cid->mdt = MMC_CID_MDT_V1(resp); 1118 break; 1119 case MMC_CSD_MMCVER_2_0: 1120 case MMC_CSD_MMCVER_3_1: 1121 case MMC_CSD_MMCVER_4_0: 1122 cid->mid = MMC_CID_MID_V2(resp); 1123 cid->oid = MMC_CID_OID_V2(resp); 1124 MMC_CID_PNM_V2_CPY(resp, cid->pnm); 1125 cid->psn = MMC_CID_PSN_V2(resp); 1126 break; 1127 default: 1128 printf("unknown MMC version %d\n", 1129 sc->csd.mmcver); 1130 return 1; 1131 } 1132 } 1133 return 0; 1134 } 1135 1136 void 1137 sdmmc_print_cid(struct sdmmc_cid *cid) 1138 { 1139 1140 printf("mid=0x%02x oid=0x%04x pnm=\"%s\" rev=0x%02x psn=0x%08x" 1141 " mdt=%03x\n", cid->mid, cid->oid, cid->pnm, cid->rev, cid->psn, 1142 cid->mdt); 1143 } 1144 1145 int 1146 sdmmc_mem_read_block(struct sdmmc_softc *sc, uint32_t blkno, 1147 u_char *data, size_t datalen) 1148 { 1149 struct sdmmc_command cmd; 1150 int error; 1151 1152 memset(&cmd, 0, sizeof(cmd)); 1153 cmd.c_data = data; 1154 cmd.c_datalen = datalen; 1155 cmd.c_blklen = SDMMC_SECTOR_SIZE; 1156 cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ? 1157 MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE; 1158 cmd.c_arg = blkno; 1159 if (!ISSET(sc->flags, SMF_CARD_SDHC)) 1160 cmd.c_arg <<= SDMMC_SECTOR_SIZE_SB; 1161 DPRINTF(("Reading block %d (%d)\n", blkno, cmd.c_arg)); 1162 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1; 1163 1164 error = sdmmc_mmc_command(sc, &cmd); 1165 if (error) 1166 goto out; 1167 1168 if (!ISSET(sc->caps, SMC_CAPS_AUTO_STOP)) { 1169 if (cmd.c_opcode == MMC_READ_BLOCK_MULTIPLE) { 1170 memset(&cmd, 0, sizeof cmd); 1171 cmd.c_opcode = MMC_STOP_TRANSMISSION; 1172 cmd.c_arg = MMC_ARG_RCA(sc->rca); 1173 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B; 1174 error = sdmmc_mmc_command(sc, &cmd); 1175 if (error) 1176 goto out; 1177 } 1178 } 1179 1180 /*if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {*/ 1181 do { 1182 memset(&cmd, 0, sizeof(cmd)); 1183 cmd.c_opcode = MMC_SEND_STATUS; 1184 cmd.c_arg = MMC_ARG_RCA(sc->rca); 1185 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R2; 1186 error = sdmmc_mmc_command(sc, &cmd); 1187 if (error) 1188 break; 1189 /* XXX time out */ 1190 } while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA)); 1191 /*}*/ 1192 1193 out: 1194 return error; 1195 } 1196 1197 int 1198 sdmmc_select_card(struct sdmmc_softc *sc) 1199 { 1200 struct sdmmc_command cmd; 1201 int error; 1202 1203 /* Don't lock */ 1204 1205 /* if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) 1206 return EIO;*/ 1207 1208 /*if (sc->sc_card == sf 1209 || (sf && sc->sc_card && sc->sc_card->rca == sf->rca)) { 1210 sc->sc_card = sf; 1211 return 0; 1212 }*/ 1213 1214 memset(&cmd, 0, sizeof(cmd)); 1215 cmd.c_opcode = MMC_SELECT_CARD; 1216 cmd.c_arg = (sc == NULL) ? 0 : MMC_ARG_RCA(sc->rca); 1217 cmd.c_flags = SCF_CMD_AC | ((sc == NULL) ? SCF_RSP_R0 : SCF_RSP_R1); 1218 error = sdmmc_mmc_command(sc, &cmd); 1219 /*if (error == 0 || sf == NULL) 1220 sc->sc_card = sf;*/ 1221 1222 return error; 1223 } 1224 1225 /* 1226 * Set the read block length appropriately for this card, according to 1227 * the card CSD register value. 1228 */ 1229 int 1230 sdmmc_mem_set_blocklen(struct sdmmc_softc *sc) 1231 { 1232 struct sdmmc_command cmd; 1233 int error; 1234 1235 /* Don't lock */ 1236 1237 memset(&cmd, 0, sizeof(cmd)); 1238 cmd.c_opcode = MMC_SET_BLOCKLEN; 1239 cmd.c_arg = SDMMC_SECTOR_SIZE; 1240 cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1; 1241 1242 error = sdmmc_mmc_command(sc, &cmd); 1243 1244 DPRINTF(("sdmmc_mem_set_blocklen: read_bl_len=%d sector_size=%d\n", 1245 1 << sc->csd.read_bl_len, SDMMC_SECTOR_SIZE)); 1246 1247 return error; 1248 } 1249 1250 int 1251 sdmmc_mem_send_scr(struct sdmmc_softc *sc, uint32_t scr[2]) 1252 { 1253 struct sdmmc_command cmd; 1254 void *ptr = NULL; 1255 int datalen = 8; 1256 int error = 0; 1257 1258 ptr = alloc(datalen); //malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO); 1259 if (ptr == NULL) 1260 goto out; 1261 1262 memset(&cmd, 0, sizeof(cmd)); 1263 cmd.c_data = ptr; 1264 cmd.c_datalen = datalen; 1265 cmd.c_blklen = datalen; 1266 cmd.c_arg = 0; 1267 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1; 1268 cmd.c_opcode = SD_APP_SEND_SCR; 1269 1270 error = sdmmc_app_command(sc, sc->rca, &cmd); 1271 if (error == 0) { 1272 memcpy(scr, ptr, datalen); 1273 } 1274 1275 out: 1276 if (ptr != NULL) { 1277 dealloc(ptr, datalen); 1278 } 1279 DPRINTF(("sdmem_mem_send_scr: error = %d\n", 1280 error)); 1281 if (error) 1282 return error; 1283 #ifdef SDMMC_DEBUG 1284 sdmmc_dump_data("SCR", scr, 8); 1285 #endif 1286 return error; 1287 } 1288 1289 int 1290 sdmmc_mem_decode_scr(struct sdmmc_softc *sc) 1291 { 1292 sdmmc_response resp; 1293 int ver; 1294 1295 memset(resp, 0, sizeof(resp)); 1296 /*resp[0] = sc->raw_scr[1]; 1297 resp[1] = sc->raw_scr[0];*/ 1298 /* 1299 * Change the raw-scr received from the DMA stream to resp. 1300 */ 1301 resp[0] = be32toh(sc->raw_scr[1]) >> 8; // LSW 1302 resp[1] = be32toh(sc->raw_scr[0]); // MSW 1303 resp[0] |= (resp[1] & 0xff) << 24; 1304 resp[1] >>= 8; 1305 resp[0] = htole32(resp[0]); 1306 resp[1] = htole32(resp[1]); 1307 1308 ver = SCR_STRUCTURE(resp); 1309 sc->scr.sd_spec = SCR_SD_SPEC(resp); 1310 sc->scr.bus_width = SCR_SD_BUS_WIDTHS(resp); 1311 1312 DPRINTF(("sdmmc_mem_decode_scr: spec=%d, bus width=%d\n", 1313 sc->scr.sd_spec, sc->scr.bus_width)); 1314 1315 if (ver != 0) { 1316 DPRINTF(("unknown structure version: %d\n", 1317 ver)); 1318 return EINVAL; 1319 } 1320 return 0; 1321 } 1322 1323 int 1324 sdmmc_set_bus_width(struct sdmmc_softc *sc, int width) 1325 { 1326 struct sdmmc_command cmd; 1327 int error; 1328 1329 if (ISSET(sc->caps, SMC_CAPS_SPI_MODE)) 1330 return ENODEV; 1331 1332 memset(&cmd, 0, sizeof(cmd)); 1333 cmd.c_opcode = SD_APP_SET_BUS_WIDTH; 1334 cmd.c_flags = SCF_RSP_R1 | SCF_CMD_AC; 1335 1336 switch (width) { 1337 case 1: 1338 cmd.c_arg = SD_ARG_BUS_WIDTH_1; 1339 break; 1340 1341 case 4: 1342 cmd.c_arg = SD_ARG_BUS_WIDTH_4; 1343 break; 1344 1345 default: 1346 return EINVAL; 1347 } 1348 1349 error = sdmmc_app_command(sc, sc->rca, &cmd); 1350 if (error == 0) 1351 error = sc->sdifdv->bus_width(sc->sdifdv->priv, width); 1352 return error; 1353 } 1354 1355 #if 1 1356 static int 1357 sdmmc_mem_sd_switch(struct sdmmc_softc *sc, int mode, int group, 1358 int function, void *status) 1359 { 1360 struct sdmmc_command cmd; 1361 void *ptr = NULL; 1362 int gsft, error = 0; 1363 const int statlen = 64; 1364 1365 if (sc->scr.sd_spec >= SCR_SD_SPEC_VER_1_10 && 1366 !ISSET(sc->csd.ccc, SD_CSD_CCC_SWITCH)) 1367 return EINVAL; 1368 1369 if (group <= 0 || group > 6 || 1370 function < 0 || function > 16) 1371 return EINVAL; 1372 1373 gsft = (group - 1) << 2; 1374 1375 ptr = alloc(statlen); 1376 if (ptr == NULL) 1377 goto out; 1378 1379 memset(&cmd, 0, sizeof(cmd)); 1380 cmd.c_data = ptr; 1381 cmd.c_datalen = statlen; 1382 cmd.c_blklen = statlen; 1383 cmd.c_opcode = SD_SEND_SWITCH_FUNC; 1384 cmd.c_arg = 1385 (!!mode << 31) | (function << gsft) | (0x00ffffff & ~(0xf << gsft)); 1386 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1; 1387 1388 error = sdmmc_mmc_command(sc, &cmd); 1389 if (error == 0) { 1390 memcpy(status, ptr, statlen); 1391 } 1392 1393 out: 1394 if (ptr != NULL) { 1395 dealloc(ptr, statlen); 1396 } 1397 return error; 1398 } 1399 #endif 1400