1 /*- 2 * Copyright (c) 2006 Bernd Walter. All rights reserved. 3 * Copyright (c) 2006 M. Warner Losh. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * Portions of this software may have been developed with reference to 26 * the SD Simplified Specification. The following disclaimer may apply: 27 * 28 * The following conditions apply to the release of the simplified 29 * specification ("Simplified Specification") by the SD Card Association and 30 * the SD Group. The Simplified Specification is a subset of the complete SD 31 * Specification which is owned by the SD Card Association and the SD 32 * Group. This Simplified Specification is provided on a non-confidential 33 * basis subject to the disclaimers below. Any implementation of the 34 * Simplified Specification may require a license from the SD Card 35 * Association, SD Group, SD-3C LLC or other third parties. 36 * 37 * Disclaimers: 38 * 39 * The information contained in the Simplified Specification is presented only 40 * as a standard specification for SD Cards and SD Host/Ancillary products and 41 * is provided "AS-IS" without any representations or warranties of any 42 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 43 * Card Association for any damages, any infringements of patents or other 44 * right of the SD Group, SD-3C LLC, the SD Card Association or any third 45 * parties, which may result from its use. No license is granted by 46 * implication, estoppel or otherwise under any patent or other rights of the 47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 49 * or the SD Card Association to disclose or distribute any technical 50 * information, know-how or other confidential information to any third party. 51 */ 52 53 #include <sys/cdefs.h> 54 __FBSDID("$FreeBSD$"); 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/kernel.h> 59 #include <sys/malloc.h> 60 #include <sys/lock.h> 61 #include <sys/module.h> 62 #include <sys/mutex.h> 63 #include <sys/bus.h> 64 #include <sys/endian.h> 65 #include <sys/sysctl.h> 66 #include <sys/time.h> 67 68 #include <dev/mmc/mmcreg.h> 69 #include <dev/mmc/mmcbrvar.h> 70 #include <dev/mmc/mmcvar.h> 71 #include "mmcbr_if.h" 72 #include "mmcbus_if.h" 73 74 struct mmc_softc { 75 device_t dev; 76 struct mtx sc_mtx; 77 struct intr_config_hook config_intrhook; 78 device_t owner; 79 uint32_t last_rca; 80 int squelched; /* suppress reporting of (expected) errors */ 81 int log_count; 82 struct timeval log_time; 83 }; 84 85 #define LOG_PPS 5 /* Log no more than 5 errors per second. */ 86 87 /* 88 * Per-card data 89 */ 90 struct mmc_ivars { 91 uint32_t raw_cid[4]; /* Raw bits of the CID */ 92 uint32_t raw_csd[4]; /* Raw bits of the CSD */ 93 uint32_t raw_scr[2]; /* Raw bits of the SCR */ 94 uint8_t raw_ext_csd[512]; /* Raw bits of the EXT_CSD */ 95 uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */ 96 uint16_t rca; 97 enum mmc_card_mode mode; 98 struct mmc_cid cid; /* cid decoded */ 99 struct mmc_csd csd; /* csd decoded */ 100 struct mmc_scr scr; /* scr decoded */ 101 struct mmc_sd_status sd_status; /* SD_STATUS decoded */ 102 u_char read_only; /* True when the device is read-only */ 103 u_char bus_width; /* Bus width to use */ 104 u_char timing; /* Bus timing support */ 105 u_char high_cap; /* High Capacity card (block addressed) */ 106 uint32_t sec_count; /* Card capacity in 512byte blocks */ 107 uint32_t tran_speed; /* Max speed in normal mode */ 108 uint32_t hs_tran_speed; /* Max speed in high speed mode */ 109 uint32_t erase_sector; /* Card native erase sector size */ 110 char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */ 111 char card_sn_string[16];/* Formatted serial # for disk->d_ident */ 112 }; 113 114 #define CMD_RETRIES 3 115 116 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */ 117 118 static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver"); 119 120 static int mmc_debug; 121 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &mmc_debug, 0, "Debug level"); 122 123 /* bus entry points */ 124 static int mmc_acquire_bus(device_t busdev, device_t dev); 125 static int mmc_attach(device_t dev); 126 static int mmc_child_location_str(device_t dev, device_t child, char *buf, 127 size_t buflen); 128 static int mmc_detach(device_t dev); 129 static int mmc_probe(device_t dev); 130 static int mmc_read_ivar(device_t bus, device_t child, int which, 131 uintptr_t *result); 132 static int mmc_release_bus(device_t busdev, device_t dev); 133 static int mmc_resume(device_t dev); 134 static int mmc_suspend(device_t dev); 135 static int mmc_wait_for_request(device_t brdev, device_t reqdev, 136 struct mmc_request *req); 137 static int mmc_write_ivar(device_t bus, device_t child, int which, 138 uintptr_t value); 139 140 #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 141 #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 142 #define MMC_LOCK_INIT(_sc) \ 143 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 144 "mmc", MTX_DEF) 145 #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 146 #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 147 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 148 149 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid); 150 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr); 151 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status, 152 struct mmc_sd_status *sd_status); 153 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, 154 uint32_t *rawsdstatus); 155 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, 156 uint32_t *rawscr); 157 static int mmc_calculate_clock(struct mmc_softc *sc); 158 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid); 159 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid); 160 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd); 161 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd); 162 static void mmc_delayed_attach(void *xsc); 163 static int mmc_delete_cards(struct mmc_softc *sc); 164 static void mmc_discover_cards(struct mmc_softc *sc); 165 static void mmc_format_card_id_string(struct mmc_ivars *ivar); 166 static void mmc_go_discovery(struct mmc_softc *sc); 167 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start, 168 int size); 169 static int mmc_highest_voltage(uint32_t ocr); 170 static void mmc_idle_cards(struct mmc_softc *sc); 171 static void mmc_ms_delay(int ms); 172 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard); 173 static void mmc_power_down(struct mmc_softc *sc); 174 static void mmc_power_up(struct mmc_softc *sc); 175 static void mmc_rescan_cards(struct mmc_softc *sc); 176 static void mmc_scan(struct mmc_softc *sc); 177 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, 178 uint8_t value, uint8_t *res); 179 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca); 180 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr); 181 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, 182 uint32_t *rocr); 183 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd); 184 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd); 185 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs); 186 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, 187 uint32_t *rocr); 188 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp); 189 static int mmc_send_status(struct mmc_softc *sc, uint16_t rca, 190 uint32_t *status); 191 static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len); 192 static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, 193 int width); 194 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp); 195 static int mmc_set_timing(struct mmc_softc *sc, int timing); 196 static int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, 197 uint8_t value); 198 static int mmc_test_bus_width(struct mmc_softc *sc); 199 static int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca, 200 struct mmc_command *cmd, int retries); 201 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, 202 int retries); 203 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 204 uint32_t arg, uint32_t flags, uint32_t *resp, int retries); 205 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req); 206 static void mmc_wakeup(struct mmc_request *req); 207 208 static void 209 mmc_ms_delay(int ms) 210 { 211 212 DELAY(1000 * ms); /* XXX BAD */ 213 } 214 215 static int 216 mmc_probe(device_t dev) 217 { 218 219 device_set_desc(dev, "MMC/SD bus"); 220 return (0); 221 } 222 223 static int 224 mmc_attach(device_t dev) 225 { 226 struct mmc_softc *sc; 227 228 sc = device_get_softc(dev); 229 sc->dev = dev; 230 MMC_LOCK_INIT(sc); 231 232 /* We'll probe and attach our children later, but before / mount */ 233 sc->config_intrhook.ich_func = mmc_delayed_attach; 234 sc->config_intrhook.ich_arg = sc; 235 if (config_intrhook_establish(&sc->config_intrhook) != 0) 236 device_printf(dev, "config_intrhook_establish failed\n"); 237 return (0); 238 } 239 240 static int 241 mmc_detach(device_t dev) 242 { 243 struct mmc_softc *sc = device_get_softc(dev); 244 int err; 245 246 if ((err = mmc_delete_cards(sc)) != 0) 247 return (err); 248 mmc_power_down(sc); 249 MMC_LOCK_DESTROY(sc); 250 251 return (0); 252 } 253 254 static int 255 mmc_suspend(device_t dev) 256 { 257 struct mmc_softc *sc = device_get_softc(dev); 258 int err; 259 260 err = bus_generic_suspend(dev); 261 if (err) 262 return (err); 263 mmc_power_down(sc); 264 return (0); 265 } 266 267 static int 268 mmc_resume(device_t dev) 269 { 270 struct mmc_softc *sc = device_get_softc(dev); 271 272 mmc_scan(sc); 273 return (bus_generic_resume(dev)); 274 } 275 276 static int 277 mmc_acquire_bus(device_t busdev, device_t dev) 278 { 279 struct mmc_softc *sc; 280 struct mmc_ivars *ivar; 281 int err; 282 int rca; 283 284 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev); 285 if (err) 286 return (err); 287 sc = device_get_softc(busdev); 288 MMC_LOCK(sc); 289 if (sc->owner) 290 panic("mmc: host bridge didn't serialize us."); 291 sc->owner = dev; 292 MMC_UNLOCK(sc); 293 294 if (busdev != dev) { 295 /* 296 * Keep track of the last rca that we've selected. If 297 * we're asked to do it again, don't. We never 298 * unselect unless the bus code itself wants the mmc 299 * bus, and constantly reselecting causes problems. 300 */ 301 rca = mmc_get_rca(dev); 302 if (sc->last_rca != rca) { 303 mmc_select_card(sc, rca); 304 sc->last_rca = rca; 305 /* Prepare bus width for the new card. */ 306 ivar = device_get_ivars(dev); 307 if (bootverbose || mmc_debug) { 308 device_printf(busdev, 309 "setting bus width to %d bits\n", 310 (ivar->bus_width == bus_width_4) ? 4 : 311 (ivar->bus_width == bus_width_8) ? 8 : 1); 312 } 313 mmc_set_card_bus_width(sc, rca, ivar->bus_width); 314 mmcbr_set_bus_width(busdev, ivar->bus_width); 315 mmcbr_update_ios(busdev); 316 } 317 } else { 318 /* 319 * If there's a card selected, stand down. 320 */ 321 if (sc->last_rca != 0) { 322 mmc_select_card(sc, 0); 323 sc->last_rca = 0; 324 } 325 } 326 327 return (0); 328 } 329 330 static int 331 mmc_release_bus(device_t busdev, device_t dev) 332 { 333 struct mmc_softc *sc; 334 int err; 335 336 sc = device_get_softc(busdev); 337 338 MMC_LOCK(sc); 339 if (!sc->owner) 340 panic("mmc: releasing unowned bus."); 341 if (sc->owner != dev) 342 panic("mmc: you don't own the bus. game over."); 343 MMC_UNLOCK(sc); 344 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev); 345 if (err) 346 return (err); 347 MMC_LOCK(sc); 348 sc->owner = NULL; 349 MMC_UNLOCK(sc); 350 return (0); 351 } 352 353 static uint32_t 354 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr) 355 { 356 357 return (ocr & MMC_OCR_VOLTAGE); 358 } 359 360 static int 361 mmc_highest_voltage(uint32_t ocr) 362 { 363 int i; 364 365 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT; 366 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--) 367 if (ocr & (1 << i)) 368 return (i); 369 return (-1); 370 } 371 372 static void 373 mmc_wakeup(struct mmc_request *req) 374 { 375 struct mmc_softc *sc; 376 377 sc = (struct mmc_softc *)req->done_data; 378 MMC_LOCK(sc); 379 req->flags |= MMC_REQ_DONE; 380 MMC_UNLOCK(sc); 381 wakeup(req); 382 } 383 384 static int 385 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req) 386 { 387 388 req->done = mmc_wakeup; 389 req->done_data = sc; 390 if (mmc_debug > 1) { 391 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x", 392 req->cmd->opcode, req->cmd->arg, req->cmd->flags); 393 if (req->cmd->data) { 394 printf(" data %d\n", (int)req->cmd->data->len); 395 } else 396 printf("\n"); 397 } 398 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req); 399 MMC_LOCK(sc); 400 while ((req->flags & MMC_REQ_DONE) == 0) 401 msleep(req, &sc->sc_mtx, 0, "mmcreq", 0); 402 MMC_UNLOCK(sc); 403 if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE)) 404 device_printf(sc->dev, "CMD%d RESULT: %d\n", 405 req->cmd->opcode, req->cmd->error); 406 return (0); 407 } 408 409 static int 410 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req) 411 { 412 struct mmc_softc *sc = device_get_softc(brdev); 413 414 return (mmc_wait_for_req(sc, req)); 415 } 416 417 static int 418 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries) 419 { 420 struct mmc_request mreq; 421 int err; 422 423 do { 424 memset(&mreq, 0, sizeof(mreq)); 425 memset(cmd->resp, 0, sizeof(cmd->resp)); 426 cmd->retries = 0; /* Retries done here, not in hardware. */ 427 cmd->mrq = &mreq; 428 mreq.cmd = cmd; 429 if (mmc_wait_for_req(sc, &mreq) != 0) 430 err = MMC_ERR_FAILED; 431 else 432 err = cmd->error; 433 } while (err != MMC_ERR_NONE && retries-- > 0); 434 435 if (err != MMC_ERR_NONE && sc->squelched == 0) { 436 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) { 437 device_printf(sc->dev, "CMD%d failed, RESULT: %d\n", 438 cmd->opcode, err); 439 } 440 } 441 442 return (err); 443 } 444 445 static int 446 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca, 447 struct mmc_command *cmd, int retries) 448 { 449 struct mmc_command appcmd; 450 int err; 451 452 /* Squelch error reporting at lower levels, we report below. */ 453 sc->squelched++; 454 do { 455 memset(&appcmd, 0, sizeof(appcmd)); 456 appcmd.opcode = MMC_APP_CMD; 457 appcmd.arg = rca << 16; 458 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 459 appcmd.data = NULL; 460 if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0) 461 err = MMC_ERR_FAILED; 462 else 463 err = appcmd.error; 464 if (err == MMC_ERR_NONE) { 465 if (!(appcmd.resp[0] & R1_APP_CMD)) 466 err = MMC_ERR_FAILED; 467 else if (mmc_wait_for_cmd(sc, cmd, 0) != 0) 468 err = MMC_ERR_FAILED; 469 else 470 err = cmd->error; 471 } 472 } while (err != MMC_ERR_NONE && retries-- > 0); 473 sc->squelched--; 474 475 if (err != MMC_ERR_NONE && sc->squelched == 0) { 476 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) { 477 device_printf(sc->dev, "ACMD%d failed, RESULT: %d\n", 478 cmd->opcode, err); 479 } 480 } 481 482 return (err); 483 } 484 485 static int 486 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 487 uint32_t arg, uint32_t flags, uint32_t *resp, int retries) 488 { 489 struct mmc_command cmd; 490 int err; 491 492 memset(&cmd, 0, sizeof(cmd)); 493 cmd.opcode = opcode; 494 cmd.arg = arg; 495 cmd.flags = flags; 496 cmd.data = NULL; 497 err = mmc_wait_for_cmd(sc, &cmd, retries); 498 if (err) 499 return (err); 500 if (resp) { 501 if (flags & MMC_RSP_136) 502 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t)); 503 else 504 *resp = cmd.resp[0]; 505 } 506 return (0); 507 } 508 509 static void 510 mmc_idle_cards(struct mmc_softc *sc) 511 { 512 device_t dev; 513 struct mmc_command cmd; 514 515 dev = sc->dev; 516 mmcbr_set_chip_select(dev, cs_high); 517 mmcbr_update_ios(dev); 518 mmc_ms_delay(1); 519 520 memset(&cmd, 0, sizeof(cmd)); 521 cmd.opcode = MMC_GO_IDLE_STATE; 522 cmd.arg = 0; 523 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 524 cmd.data = NULL; 525 mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 526 mmc_ms_delay(1); 527 528 mmcbr_set_chip_select(dev, cs_dontcare); 529 mmcbr_update_ios(dev); 530 mmc_ms_delay(1); 531 } 532 533 static int 534 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 535 { 536 struct mmc_command cmd; 537 int err = MMC_ERR_NONE, i; 538 539 memset(&cmd, 0, sizeof(cmd)); 540 cmd.opcode = ACMD_SD_SEND_OP_COND; 541 cmd.arg = ocr; 542 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 543 cmd.data = NULL; 544 545 for (i = 0; i < 1000; i++) { 546 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES); 547 if (err != MMC_ERR_NONE) 548 break; 549 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 550 (ocr & MMC_OCR_VOLTAGE) == 0) 551 break; 552 err = MMC_ERR_TIMEOUT; 553 mmc_ms_delay(10); 554 } 555 if (rocr && err == MMC_ERR_NONE) 556 *rocr = cmd.resp[0]; 557 return (err); 558 } 559 560 static int 561 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 562 { 563 struct mmc_command cmd; 564 int err = MMC_ERR_NONE, i; 565 566 memset(&cmd, 0, sizeof(cmd)); 567 cmd.opcode = MMC_SEND_OP_COND; 568 cmd.arg = ocr; 569 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 570 cmd.data = NULL; 571 572 for (i = 0; i < 1000; i++) { 573 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 574 if (err != MMC_ERR_NONE) 575 break; 576 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 577 (ocr & MMC_OCR_VOLTAGE) == 0) 578 break; 579 err = MMC_ERR_TIMEOUT; 580 mmc_ms_delay(10); 581 } 582 if (rocr && err == MMC_ERR_NONE) 583 *rocr = cmd.resp[0]; 584 return (err); 585 } 586 587 static int 588 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs) 589 { 590 struct mmc_command cmd; 591 int err; 592 593 memset(&cmd, 0, sizeof(cmd)); 594 cmd.opcode = SD_SEND_IF_COND; 595 cmd.arg = (vhs << 8) + 0xAA; 596 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR; 597 cmd.data = NULL; 598 599 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 600 return (err); 601 } 602 603 static void 604 mmc_power_up(struct mmc_softc *sc) 605 { 606 device_t dev; 607 608 dev = sc->dev; 609 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev))); 610 mmcbr_set_bus_mode(dev, opendrain); 611 mmcbr_set_chip_select(dev, cs_dontcare); 612 mmcbr_set_bus_width(dev, bus_width_1); 613 mmcbr_set_power_mode(dev, power_up); 614 mmcbr_set_clock(dev, 0); 615 mmcbr_update_ios(dev); 616 mmc_ms_delay(1); 617 618 mmcbr_set_clock(dev, CARD_ID_FREQUENCY); 619 mmcbr_set_timing(dev, bus_timing_normal); 620 mmcbr_set_power_mode(dev, power_on); 621 mmcbr_update_ios(dev); 622 mmc_ms_delay(2); 623 } 624 625 static void 626 mmc_power_down(struct mmc_softc *sc) 627 { 628 device_t dev = sc->dev; 629 630 mmcbr_set_bus_mode(dev, opendrain); 631 mmcbr_set_chip_select(dev, cs_dontcare); 632 mmcbr_set_bus_width(dev, bus_width_1); 633 mmcbr_set_power_mode(dev, power_off); 634 mmcbr_set_clock(dev, 0); 635 mmcbr_set_timing(dev, bus_timing_normal); 636 mmcbr_update_ios(dev); 637 } 638 639 static int 640 mmc_select_card(struct mmc_softc *sc, uint16_t rca) 641 { 642 int flags; 643 644 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC; 645 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16, 646 flags, NULL, CMD_RETRIES)); 647 } 648 649 static int 650 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value) 651 { 652 struct mmc_command cmd; 653 int err; 654 655 memset(&cmd, 0, sizeof(cmd)); 656 cmd.opcode = MMC_SWITCH_FUNC; 657 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) | 658 (index << 16) | 659 (value << 8) | 660 set; 661 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 662 cmd.data = NULL; 663 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 664 return (err); 665 } 666 667 static int 668 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value, 669 uint8_t *res) 670 { 671 int err; 672 struct mmc_command cmd; 673 struct mmc_data data; 674 675 memset(&cmd, 0, sizeof(cmd)); 676 memset(&data, 0, sizeof(data)); 677 memset(res, 0, 64); 678 679 cmd.opcode = SD_SWITCH_FUNC; 680 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 681 cmd.arg = mode << 31; /* 0 - check, 1 - set */ 682 cmd.arg |= 0x00FFFFFF; 683 cmd.arg &= ~(0xF << (grp * 4)); 684 cmd.arg |= value << (grp * 4); 685 cmd.data = &data; 686 687 data.data = res; 688 data.len = 64; 689 data.flags = MMC_DATA_READ; 690 691 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 692 return (err); 693 } 694 695 static int 696 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width) 697 { 698 struct mmc_command cmd; 699 int err; 700 uint8_t value; 701 702 if (mmcbr_get_mode(sc->dev) == mode_sd) { 703 memset(&cmd, 0, sizeof(cmd)); 704 cmd.opcode = ACMD_SET_CLR_CARD_DETECT; 705 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 706 cmd.arg = SD_CLR_CARD_DETECT; 707 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 708 if (err != 0) 709 return (err); 710 memset(&cmd, 0, sizeof(cmd)); 711 cmd.opcode = ACMD_SET_BUS_WIDTH; 712 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 713 switch (width) { 714 case bus_width_1: 715 cmd.arg = SD_BUS_WIDTH_1; 716 break; 717 case bus_width_4: 718 cmd.arg = SD_BUS_WIDTH_4; 719 break; 720 default: 721 return (MMC_ERR_INVALID); 722 } 723 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 724 } else { 725 switch (width) { 726 case bus_width_1: 727 value = EXT_CSD_BUS_WIDTH_1; 728 break; 729 case bus_width_4: 730 value = EXT_CSD_BUS_WIDTH_4; 731 break; 732 case bus_width_8: 733 value = EXT_CSD_BUS_WIDTH_8; 734 break; 735 default: 736 return (MMC_ERR_INVALID); 737 } 738 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, 739 value); 740 } 741 return (err); 742 } 743 744 static int 745 mmc_set_timing(struct mmc_softc *sc, int timing) 746 { 747 int err; 748 uint8_t value; 749 u_char switch_res[64]; 750 751 switch (timing) { 752 case bus_timing_normal: 753 value = 0; 754 break; 755 case bus_timing_hs: 756 value = 1; 757 break; 758 default: 759 return (MMC_ERR_INVALID); 760 } 761 if (mmcbr_get_mode(sc->dev) == mode_sd) 762 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1, 763 value, switch_res); 764 else 765 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, 766 EXT_CSD_HS_TIMING, value); 767 return (err); 768 } 769 770 static int 771 mmc_test_bus_width(struct mmc_softc *sc) 772 { 773 struct mmc_command cmd; 774 struct mmc_data data; 775 int err; 776 uint8_t buf[8]; 777 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 778 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 779 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, }; 780 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, }; 781 782 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) { 783 mmcbr_set_bus_width(sc->dev, bus_width_8); 784 mmcbr_update_ios(sc->dev); 785 786 sc->squelched++; /* Errors are expected, squelch reporting. */ 787 memset(&cmd, 0, sizeof(cmd)); 788 memset(&data, 0, sizeof(data)); 789 cmd.opcode = MMC_BUSTEST_W; 790 cmd.arg = 0; 791 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 792 cmd.data = &data; 793 794 data.data = p8; 795 data.len = 8; 796 data.flags = MMC_DATA_WRITE; 797 mmc_wait_for_cmd(sc, &cmd, 0); 798 799 memset(&cmd, 0, sizeof(cmd)); 800 memset(&data, 0, sizeof(data)); 801 cmd.opcode = MMC_BUSTEST_R; 802 cmd.arg = 0; 803 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 804 cmd.data = &data; 805 806 data.data = buf; 807 data.len = 8; 808 data.flags = MMC_DATA_READ; 809 err = mmc_wait_for_cmd(sc, &cmd, 0); 810 sc->squelched--; 811 812 mmcbr_set_bus_width(sc->dev, bus_width_1); 813 mmcbr_update_ios(sc->dev); 814 815 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0) 816 return (bus_width_8); 817 } 818 819 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) { 820 mmcbr_set_bus_width(sc->dev, bus_width_4); 821 mmcbr_update_ios(sc->dev); 822 823 sc->squelched++; /* Errors are expected, squelch reporting. */ 824 memset(&cmd, 0, sizeof(cmd)); 825 memset(&data, 0, sizeof(data)); 826 cmd.opcode = MMC_BUSTEST_W; 827 cmd.arg = 0; 828 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 829 cmd.data = &data; 830 831 data.data = p4; 832 data.len = 4; 833 data.flags = MMC_DATA_WRITE; 834 mmc_wait_for_cmd(sc, &cmd, 0); 835 836 memset(&cmd, 0, sizeof(cmd)); 837 memset(&data, 0, sizeof(data)); 838 cmd.opcode = MMC_BUSTEST_R; 839 cmd.arg = 0; 840 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 841 cmd.data = &data; 842 843 data.data = buf; 844 data.len = 4; 845 data.flags = MMC_DATA_READ; 846 err = mmc_wait_for_cmd(sc, &cmd, 0); 847 sc->squelched--; 848 849 mmcbr_set_bus_width(sc->dev, bus_width_1); 850 mmcbr_update_ios(sc->dev); 851 852 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0) 853 return (bus_width_4); 854 } 855 return (bus_width_1); 856 } 857 858 static uint32_t 859 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size) 860 { 861 const int i = (bit_len / 32) - (start / 32) - 1; 862 const int shift = start & 31; 863 uint32_t retval = bits[i] >> shift; 864 if (size + shift > 32) 865 retval |= bits[i - 1] << (32 - shift); 866 return (retval & ((1llu << size) - 1)); 867 } 868 869 static void 870 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid) 871 { 872 int i; 873 874 /* There's no version info, so we take it on faith */ 875 memset(cid, 0, sizeof(*cid)); 876 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 877 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16); 878 for (i = 0; i < 5; i++) 879 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 880 cid->pnm[5] = 0; 881 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8); 882 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32); 883 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000; 884 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4); 885 } 886 887 static void 888 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid) 889 { 890 int i; 891 892 /* There's no version info, so we take it on faith */ 893 memset(cid, 0, sizeof(*cid)); 894 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8); 895 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8); 896 for (i = 0; i < 6; i++) 897 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8); 898 cid->pnm[6] = 0; 899 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8); 900 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32); 901 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4); 902 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997; 903 } 904 905 static void 906 mmc_format_card_id_string(struct mmc_ivars *ivar) 907 { 908 char oidstr[8]; 909 uint8_t c1; 910 uint8_t c2; 911 912 /* 913 * Format a card ID string for use by the mmcsd driver, it's what 914 * appears between the <> in the following: 915 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0 916 * 22.5MHz/4bit/128-block 917 * 918 * Also format just the card serial number, which the mmcsd driver will 919 * use as the disk->d_ident string. 920 * 921 * The card_id_string in mmc_ivars is currently allocated as 64 bytes, 922 * and our max formatted length is currently 55 bytes if every field 923 * contains the largest value. 924 * 925 * Sometimes the oid is two printable ascii chars; when it's not, 926 * format it as 0xnnnn instead. 927 */ 928 c1 = (ivar->cid.oid >> 8) & 0x0ff; 929 c2 = ivar->cid.oid & 0x0ff; 930 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f) 931 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2); 932 else 933 snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid); 934 snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string), 935 "%08X", ivar->cid.psn); 936 snprintf(ivar->card_id_string, sizeof(ivar->card_id_string), 937 "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s", 938 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "", 939 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f, 940 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year, 941 ivar->cid.mid, oidstr); 942 } 943 944 static const int exp[8] = { 945 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 946 }; 947 948 static const int mant[16] = { 949 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 950 }; 951 952 static const int cur_min[8] = { 953 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000 954 }; 955 956 static const int cur_max[8] = { 957 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000 958 }; 959 960 static void 961 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd) 962 { 963 int v; 964 int m; 965 int e; 966 967 memset(csd, 0, sizeof(*csd)); 968 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2); 969 if (v == 0) { 970 m = mmc_get_bits(raw_csd, 128, 115, 4); 971 e = mmc_get_bits(raw_csd, 128, 112, 3); 972 csd->tacc = (exp[e] * mant[m] + 9) / 10; 973 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 974 m = mmc_get_bits(raw_csd, 128, 99, 4); 975 e = mmc_get_bits(raw_csd, 128, 96, 3); 976 csd->tran_speed = exp[e] * 10000 * mant[m]; 977 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 978 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 979 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 980 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 981 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 982 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 983 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 984 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 985 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 986 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 987 m = mmc_get_bits(raw_csd, 128, 62, 12); 988 e = mmc_get_bits(raw_csd, 128, 47, 3); 989 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 990 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 991 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 992 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 993 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 994 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 995 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 996 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 997 } else if (v == 1) { 998 m = mmc_get_bits(raw_csd, 128, 115, 4); 999 e = mmc_get_bits(raw_csd, 128, 112, 3); 1000 csd->tacc = (exp[e] * mant[m] + 9) / 10; 1001 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 1002 m = mmc_get_bits(raw_csd, 128, 99, 4); 1003 e = mmc_get_bits(raw_csd, 128, 96, 3); 1004 csd->tran_speed = exp[e] * 10000 * mant[m]; 1005 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 1006 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 1007 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 1008 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 1009 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 1010 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 1011 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) * 1012 512 * 1024; 1013 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1); 1014 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1; 1015 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7); 1016 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 1017 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 1018 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 1019 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 1020 } else 1021 panic("unknown SD CSD version"); 1022 } 1023 1024 static void 1025 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd) 1026 { 1027 int m; 1028 int e; 1029 1030 memset(csd, 0, sizeof(*csd)); 1031 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2); 1032 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4); 1033 m = mmc_get_bits(raw_csd, 128, 115, 4); 1034 e = mmc_get_bits(raw_csd, 128, 112, 3); 1035 csd->tacc = exp[e] * mant[m] + 9 / 10; 1036 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100; 1037 m = mmc_get_bits(raw_csd, 128, 99, 4); 1038 e = mmc_get_bits(raw_csd, 128, 96, 3); 1039 csd->tran_speed = exp[e] * 10000 * mant[m]; 1040 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12); 1041 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4); 1042 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1); 1043 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1); 1044 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1); 1045 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1); 1046 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)]; 1047 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)]; 1048 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)]; 1049 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)]; 1050 m = mmc_get_bits(raw_csd, 128, 62, 12); 1051 e = mmc_get_bits(raw_csd, 128, 47, 3); 1052 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 1053 csd->erase_blk_en = 0; 1054 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) * 1055 (mmc_get_bits(raw_csd, 128, 37, 5) + 1); 1056 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5); 1057 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1); 1058 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3); 1059 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4); 1060 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1); 1061 } 1062 1063 static void 1064 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr) 1065 { 1066 unsigned int scr_struct; 1067 1068 memset(scr, 0, sizeof(*scr)); 1069 1070 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4); 1071 if (scr_struct != 0) { 1072 printf("Unrecognised SCR structure version %d\n", 1073 scr_struct); 1074 return; 1075 } 1076 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4); 1077 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4); 1078 } 1079 1080 static void 1081 mmc_app_decode_sd_status(uint32_t *raw_sd_status, 1082 struct mmc_sd_status *sd_status) 1083 { 1084 1085 memset(sd_status, 0, sizeof(*sd_status)); 1086 1087 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2); 1088 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1); 1089 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16); 1090 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12); 1091 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8); 1092 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8); 1093 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4); 1094 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16); 1095 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6); 1096 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2); 1097 } 1098 1099 static int 1100 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid) 1101 { 1102 struct mmc_command cmd; 1103 int err; 1104 1105 memset(&cmd, 0, sizeof(cmd)); 1106 cmd.opcode = MMC_ALL_SEND_CID; 1107 cmd.arg = 0; 1108 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 1109 cmd.data = NULL; 1110 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1111 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 1112 return (err); 1113 } 1114 1115 static int 1116 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd) 1117 { 1118 struct mmc_command cmd; 1119 int err; 1120 1121 memset(&cmd, 0, sizeof(cmd)); 1122 cmd.opcode = MMC_SEND_CSD; 1123 cmd.arg = rca << 16; 1124 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 1125 cmd.data = NULL; 1126 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1127 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t)); 1128 return (err); 1129 } 1130 1131 static int 1132 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr) 1133 { 1134 int err; 1135 struct mmc_command cmd; 1136 struct mmc_data data; 1137 1138 memset(&cmd, 0, sizeof(cmd)); 1139 memset(&data, 0, sizeof(data)); 1140 1141 memset(rawscr, 0, 8); 1142 cmd.opcode = ACMD_SEND_SCR; 1143 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1144 cmd.arg = 0; 1145 cmd.data = &data; 1146 1147 data.data = rawscr; 1148 data.len = 8; 1149 data.flags = MMC_DATA_READ; 1150 1151 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 1152 rawscr[0] = be32toh(rawscr[0]); 1153 rawscr[1] = be32toh(rawscr[1]); 1154 return (err); 1155 } 1156 1157 static int 1158 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd) 1159 { 1160 int err; 1161 struct mmc_command cmd; 1162 struct mmc_data data; 1163 1164 memset(&cmd, 0, sizeof(cmd)); 1165 memset(&data, 0, sizeof(data)); 1166 1167 memset(rawextcsd, 0, 512); 1168 cmd.opcode = MMC_SEND_EXT_CSD; 1169 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1170 cmd.arg = 0; 1171 cmd.data = &data; 1172 1173 data.data = rawextcsd; 1174 data.len = 512; 1175 data.flags = MMC_DATA_READ; 1176 1177 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1178 return (err); 1179 } 1180 1181 static int 1182 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus) 1183 { 1184 int err, i; 1185 struct mmc_command cmd; 1186 struct mmc_data data; 1187 1188 memset(&cmd, 0, sizeof(cmd)); 1189 memset(&data, 0, sizeof(data)); 1190 1191 memset(rawsdstatus, 0, 64); 1192 cmd.opcode = ACMD_SD_STATUS; 1193 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1194 cmd.arg = 0; 1195 cmd.data = &data; 1196 1197 data.data = rawsdstatus; 1198 data.len = 64; 1199 data.flags = MMC_DATA_READ; 1200 1201 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 1202 for (i = 0; i < 16; i++) 1203 rawsdstatus[i] = be32toh(rawsdstatus[i]); 1204 return (err); 1205 } 1206 1207 static int 1208 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp) 1209 { 1210 struct mmc_command cmd; 1211 int err; 1212 1213 memset(&cmd, 0, sizeof(cmd)); 1214 cmd.opcode = MMC_SET_RELATIVE_ADDR; 1215 cmd.arg = resp << 16; 1216 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 1217 cmd.data = NULL; 1218 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1219 return (err); 1220 } 1221 1222 static int 1223 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp) 1224 { 1225 struct mmc_command cmd; 1226 int err; 1227 1228 memset(&cmd, 0, sizeof(cmd)); 1229 cmd.opcode = SD_SEND_RELATIVE_ADDR; 1230 cmd.arg = 0; 1231 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 1232 cmd.data = NULL; 1233 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1234 *resp = cmd.resp[0]; 1235 return (err); 1236 } 1237 1238 static int 1239 mmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status) 1240 { 1241 struct mmc_command cmd; 1242 int err; 1243 1244 memset(&cmd, 0, sizeof(cmd)); 1245 cmd.opcode = MMC_SEND_STATUS; 1246 cmd.arg = rca << 16; 1247 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1248 cmd.data = NULL; 1249 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1250 *status = cmd.resp[0]; 1251 return (err); 1252 } 1253 1254 static int 1255 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len) 1256 { 1257 struct mmc_command cmd; 1258 int err; 1259 1260 memset(&cmd, 0, sizeof(cmd)); 1261 cmd.opcode = MMC_SET_BLOCKLEN; 1262 cmd.arg = len; 1263 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1264 cmd.data = NULL; 1265 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 1266 return (err); 1267 } 1268 1269 static void 1270 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard) 1271 { 1272 device_printf(dev, "Card at relative address 0x%04x%s:\n", 1273 ivar->rca, newcard ? " added" : ""); 1274 device_printf(dev, " card: %s\n", ivar->card_id_string); 1275 device_printf(dev, " bus: %ubit, %uMHz%s\n", 1276 (ivar->bus_width == bus_width_1 ? 1 : 1277 (ivar->bus_width == bus_width_4 ? 4 : 8)), 1278 (ivar->timing == bus_timing_hs ? 1279 ivar->hs_tran_speed : ivar->tran_speed) / 1000000, 1280 ivar->timing == bus_timing_hs ? ", high speed timing" : ""); 1281 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n", 1282 ivar->sec_count, ivar->erase_sector, 1283 ivar->read_only ? ", read-only" : ""); 1284 } 1285 1286 static void 1287 mmc_discover_cards(struct mmc_softc *sc) 1288 { 1289 struct mmc_ivars *ivar = NULL; 1290 device_t *devlist; 1291 int err, i, devcount, newcard; 1292 uint32_t raw_cid[4], resp, sec_count, status; 1293 device_t child; 1294 uint16_t rca = 2; 1295 u_char switch_res[64]; 1296 1297 if (bootverbose || mmc_debug) 1298 device_printf(sc->dev, "Probing cards\n"); 1299 while (1) { 1300 sc->squelched++; /* Errors are expected, squelch reporting. */ 1301 err = mmc_all_send_cid(sc, raw_cid); 1302 sc->squelched--; 1303 if (err == MMC_ERR_TIMEOUT) 1304 break; 1305 if (err != MMC_ERR_NONE) { 1306 device_printf(sc->dev, "Error reading CID %d\n", err); 1307 break; 1308 } 1309 newcard = 1; 1310 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1311 return; 1312 for (i = 0; i < devcount; i++) { 1313 ivar = device_get_ivars(devlist[i]); 1314 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) { 1315 newcard = 0; 1316 break; 1317 } 1318 } 1319 free(devlist, M_TEMP); 1320 if (bootverbose || mmc_debug) { 1321 device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n", 1322 newcard ? "New c" : "C", 1323 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]); 1324 } 1325 if (newcard) { 1326 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, 1327 M_WAITOK | M_ZERO); 1328 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid)); 1329 } 1330 if (mmcbr_get_ro(sc->dev)) 1331 ivar->read_only = 1; 1332 ivar->bus_width = bus_width_1; 1333 ivar->timing = bus_timing_normal; 1334 ivar->mode = mmcbr_get_mode(sc->dev); 1335 if (ivar->mode == mode_sd) { 1336 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid); 1337 mmc_send_relative_addr(sc, &resp); 1338 ivar->rca = resp >> 16; 1339 /* Get card CSD. */ 1340 mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 1341 if (bootverbose || mmc_debug) 1342 device_printf(sc->dev, 1343 "%sard detected (CSD %08x%08x%08x%08x)\n", 1344 newcard ? "New c" : "C", ivar->raw_csd[0], 1345 ivar->raw_csd[1], ivar->raw_csd[2], 1346 ivar->raw_csd[3]); 1347 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd); 1348 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE; 1349 if (ivar->csd.csd_structure > 0) 1350 ivar->high_cap = 1; 1351 ivar->tran_speed = ivar->csd.tran_speed; 1352 ivar->erase_sector = ivar->csd.erase_sector * 1353 ivar->csd.write_bl_len / MMC_SECTOR_SIZE; 1354 1355 err = mmc_send_status(sc, ivar->rca, &status); 1356 if (err != MMC_ERR_NONE) { 1357 device_printf(sc->dev, 1358 "Error reading card status %d\n", err); 1359 break; 1360 } 1361 if ((status & R1_CARD_IS_LOCKED) != 0) { 1362 device_printf(sc->dev, 1363 "Card is password protected, skipping.\n"); 1364 break; 1365 } 1366 1367 /* Get card SCR. Card must be selected to fetch it. */ 1368 mmc_select_card(sc, ivar->rca); 1369 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr); 1370 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr); 1371 /* Get card switch capabilities (command class 10). */ 1372 if ((ivar->scr.sda_vsn >= 1) && 1373 (ivar->csd.ccc & (1<<10))) { 1374 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK, 1375 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE, 1376 switch_res); 1377 if (switch_res[13] & 2) { 1378 ivar->timing = bus_timing_hs; 1379 ivar->hs_tran_speed = SD_MAX_HS; 1380 } 1381 } 1382 1383 /* 1384 * We deselect then reselect the card here. Some cards 1385 * become unselected and timeout with the above two 1386 * commands, although the state tables / diagrams in the 1387 * standard suggest they go back to the transfer state. 1388 * Other cards don't become deselected, and if we 1389 * atttempt to blindly re-select them, we get timeout 1390 * errors from some controllers. So we deselect then 1391 * reselect to handle all situations. The only thing we 1392 * use from the sd_status is the erase sector size, but 1393 * it is still nice to get that right. 1394 */ 1395 mmc_select_card(sc, 0); 1396 mmc_select_card(sc, ivar->rca); 1397 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status); 1398 mmc_app_decode_sd_status(ivar->raw_sd_status, 1399 &ivar->sd_status); 1400 if (ivar->sd_status.au_size != 0) { 1401 ivar->erase_sector = 1402 16 << ivar->sd_status.au_size; 1403 } 1404 /* Find max supported bus width. */ 1405 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) && 1406 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) 1407 ivar->bus_width = bus_width_4; 1408 1409 /* 1410 * Some cards that report maximum I/O block sizes 1411 * greater than 512 require the block length to be 1412 * set to 512, even though that is supposed to be 1413 * the default. Example: 1414 * 1415 * Transcend 2GB SDSC card, CID: 1416 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000 1417 */ 1418 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE || 1419 ivar->csd.write_bl_len != MMC_SECTOR_SIZE) 1420 mmc_set_blocklen(sc, MMC_SECTOR_SIZE); 1421 1422 mmc_format_card_id_string(ivar); 1423 1424 if (bootverbose || mmc_debug) 1425 mmc_log_card(sc->dev, ivar, newcard); 1426 if (newcard) { 1427 /* Add device. */ 1428 child = device_add_child(sc->dev, NULL, -1); 1429 device_set_ivars(child, ivar); 1430 } 1431 mmc_select_card(sc, 0); 1432 return; 1433 } 1434 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid); 1435 ivar->rca = rca++; 1436 mmc_set_relative_addr(sc, ivar->rca); 1437 /* Get card CSD. */ 1438 mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 1439 if (bootverbose || mmc_debug) 1440 device_printf(sc->dev, 1441 "%sard detected (CSD %08x%08x%08x%08x)\n", 1442 newcard ? "New c" : "C", ivar->raw_csd[0], 1443 ivar->raw_csd[1], ivar->raw_csd[2], 1444 ivar->raw_csd[3]); 1445 1446 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd); 1447 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE; 1448 ivar->tran_speed = ivar->csd.tran_speed; 1449 ivar->erase_sector = ivar->csd.erase_sector * 1450 ivar->csd.write_bl_len / MMC_SECTOR_SIZE; 1451 1452 err = mmc_send_status(sc, ivar->rca, &status); 1453 if (err != MMC_ERR_NONE) { 1454 device_printf(sc->dev, 1455 "Error reading card status %d\n", err); 1456 break; 1457 } 1458 if ((status & R1_CARD_IS_LOCKED) != 0) { 1459 device_printf(sc->dev, 1460 "Card is password protected, skipping.\n"); 1461 break; 1462 } 1463 1464 mmc_select_card(sc, ivar->rca); 1465 1466 /* Only MMC >= 4.x cards support EXT_CSD. */ 1467 if (ivar->csd.spec_vers >= 4) { 1468 mmc_send_ext_csd(sc, ivar->raw_ext_csd); 1469 /* Handle extended capacity from EXT_CSD */ 1470 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] + 1471 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) + 1472 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) + 1473 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24); 1474 if (sec_count != 0) { 1475 ivar->sec_count = sec_count; 1476 ivar->high_cap = 1; 1477 } 1478 /* Get card speed in high speed mode. */ 1479 ivar->timing = bus_timing_hs; 1480 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] 1481 & EXT_CSD_CARD_TYPE_52) 1482 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS; 1483 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] 1484 & EXT_CSD_CARD_TYPE_26) 1485 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS; 1486 else 1487 ivar->hs_tran_speed = ivar->tran_speed; 1488 /* Find max supported bus width. */ 1489 ivar->bus_width = mmc_test_bus_width(sc); 1490 /* Handle HC erase sector size. */ 1491 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) { 1492 ivar->erase_sector = 1024 * 1493 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE]; 1494 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, 1495 EXT_CSD_ERASE_GRP_DEF, 1); 1496 } 1497 } else { 1498 ivar->bus_width = bus_width_1; 1499 ivar->timing = bus_timing_normal; 1500 } 1501 1502 /* 1503 * Some cards that report maximum I/O block sizes greater 1504 * than 512 require the block length to be set to 512, even 1505 * though that is supposed to be the default. Example: 1506 * 1507 * Transcend 2GB SDSC card, CID: 1508 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000 1509 */ 1510 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE || 1511 ivar->csd.write_bl_len != MMC_SECTOR_SIZE) 1512 mmc_set_blocklen(sc, MMC_SECTOR_SIZE); 1513 1514 mmc_format_card_id_string(ivar); 1515 1516 if (bootverbose || mmc_debug) 1517 mmc_log_card(sc->dev, ivar, newcard); 1518 if (newcard) { 1519 /* Add device. */ 1520 child = device_add_child(sc->dev, NULL, -1); 1521 device_set_ivars(child, ivar); 1522 } 1523 mmc_select_card(sc, 0); 1524 } 1525 } 1526 1527 static void 1528 mmc_rescan_cards(struct mmc_softc *sc) 1529 { 1530 struct mmc_ivars *ivar = NULL; 1531 device_t *devlist; 1532 int err, i, devcount; 1533 1534 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1535 return; 1536 for (i = 0; i < devcount; i++) { 1537 ivar = device_get_ivars(devlist[i]); 1538 if (mmc_select_card(sc, ivar->rca)) { 1539 if (bootverbose || mmc_debug) 1540 device_printf(sc->dev, "Card at relative address %d lost.\n", 1541 ivar->rca); 1542 device_delete_child(sc->dev, devlist[i]); 1543 free(ivar, M_DEVBUF); 1544 } 1545 } 1546 free(devlist, M_TEMP); 1547 mmc_select_card(sc, 0); 1548 } 1549 1550 static int 1551 mmc_delete_cards(struct mmc_softc *sc) 1552 { 1553 struct mmc_ivars *ivar; 1554 device_t *devlist; 1555 int err, i, devcount; 1556 1557 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0) 1558 return (err); 1559 for (i = 0; i < devcount; i++) { 1560 ivar = device_get_ivars(devlist[i]); 1561 if (bootverbose || mmc_debug) 1562 device_printf(sc->dev, "Card at relative address %d deleted.\n", 1563 ivar->rca); 1564 device_delete_child(sc->dev, devlist[i]); 1565 free(ivar, M_DEVBUF); 1566 } 1567 free(devlist, M_TEMP); 1568 return (0); 1569 } 1570 1571 static void 1572 mmc_go_discovery(struct mmc_softc *sc) 1573 { 1574 uint32_t ocr; 1575 device_t dev; 1576 int err; 1577 1578 dev = sc->dev; 1579 if (mmcbr_get_power_mode(dev) != power_on) { 1580 /* 1581 * First, try SD modes 1582 */ 1583 sc->squelched++; /* Errors are expected, squelch reporting. */ 1584 mmcbr_set_mode(dev, mode_sd); 1585 mmc_power_up(sc); 1586 mmcbr_set_bus_mode(dev, pushpull); 1587 if (bootverbose || mmc_debug) 1588 device_printf(sc->dev, "Probing bus\n"); 1589 mmc_idle_cards(sc); 1590 err = mmc_send_if_cond(sc, 1); 1591 if ((bootverbose || mmc_debug) && err == 0) 1592 device_printf(sc->dev, "SD 2.0 interface conditions: OK\n"); 1593 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 1594 if (bootverbose || mmc_debug) 1595 device_printf(sc->dev, "SD probe: failed\n"); 1596 /* 1597 * Failed, try MMC 1598 */ 1599 mmcbr_set_mode(dev, mode_mmc); 1600 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 1601 if (bootverbose || mmc_debug) 1602 device_printf(sc->dev, "MMC probe: failed\n"); 1603 ocr = 0; /* Failed both, powerdown. */ 1604 } else if (bootverbose || mmc_debug) 1605 device_printf(sc->dev, 1606 "MMC probe: OK (OCR: 0x%08x)\n", ocr); 1607 } else if (bootverbose || mmc_debug) 1608 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr); 1609 sc->squelched--; 1610 1611 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr)); 1612 if (mmcbr_get_ocr(dev) != 0) 1613 mmc_idle_cards(sc); 1614 } else { 1615 mmcbr_set_bus_mode(dev, opendrain); 1616 mmcbr_set_clock(dev, CARD_ID_FREQUENCY); 1617 mmcbr_update_ios(dev); 1618 /* XXX recompute vdd based on new cards? */ 1619 } 1620 /* 1621 * Make sure that we have a mutually agreeable voltage to at least 1622 * one card on the bus. 1623 */ 1624 if (bootverbose || mmc_debug) 1625 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev)); 1626 if (mmcbr_get_ocr(dev) == 0) { 1627 device_printf(sc->dev, "No compatible cards found on bus\n"); 1628 mmc_delete_cards(sc); 1629 mmc_power_down(sc); 1630 return; 1631 } 1632 /* 1633 * Reselect the cards after we've idled them above. 1634 */ 1635 if (mmcbr_get_mode(dev) == mode_sd) { 1636 err = mmc_send_if_cond(sc, 1); 1637 mmc_send_app_op_cond(sc, 1638 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL); 1639 } else 1640 mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL); 1641 mmc_discover_cards(sc); 1642 mmc_rescan_cards(sc); 1643 1644 mmcbr_set_bus_mode(dev, pushpull); 1645 mmcbr_update_ios(dev); 1646 mmc_calculate_clock(sc); 1647 bus_generic_attach(dev); 1648 /* mmc_update_children_sysctl(dev);*/ 1649 } 1650 1651 static int 1652 mmc_calculate_clock(struct mmc_softc *sc) 1653 { 1654 int max_dtr, max_hs_dtr, max_timing; 1655 int nkid, i, f_max; 1656 device_t *kids; 1657 struct mmc_ivars *ivar; 1658 1659 f_max = mmcbr_get_f_max(sc->dev); 1660 max_dtr = max_hs_dtr = f_max; 1661 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED)) 1662 max_timing = bus_timing_hs; 1663 else 1664 max_timing = bus_timing_normal; 1665 if (device_get_children(sc->dev, &kids, &nkid) != 0) 1666 panic("can't get children"); 1667 for (i = 0; i < nkid; i++) { 1668 ivar = device_get_ivars(kids[i]); 1669 if (ivar->timing < max_timing) 1670 max_timing = ivar->timing; 1671 if (ivar->tran_speed < max_dtr) 1672 max_dtr = ivar->tran_speed; 1673 if (ivar->hs_tran_speed < max_hs_dtr) 1674 max_hs_dtr = ivar->hs_tran_speed; 1675 } 1676 for (i = 0; i < nkid; i++) { 1677 ivar = device_get_ivars(kids[i]); 1678 if (ivar->timing == bus_timing_normal) 1679 continue; 1680 mmc_select_card(sc, ivar->rca); 1681 mmc_set_timing(sc, max_timing); 1682 } 1683 mmc_select_card(sc, 0); 1684 free(kids, M_TEMP); 1685 if (max_timing == bus_timing_hs) 1686 max_dtr = max_hs_dtr; 1687 if (bootverbose || mmc_debug) { 1688 device_printf(sc->dev, 1689 "setting transfer rate to %d.%03dMHz%s\n", 1690 max_dtr / 1000000, (max_dtr / 1000) % 1000, 1691 max_timing == bus_timing_hs ? " (high speed timing)" : ""); 1692 } 1693 mmcbr_set_timing(sc->dev, max_timing); 1694 mmcbr_set_clock(sc->dev, max_dtr); 1695 mmcbr_update_ios(sc->dev); 1696 return max_dtr; 1697 } 1698 1699 static void 1700 mmc_scan(struct mmc_softc *sc) 1701 { 1702 device_t dev = sc->dev; 1703 1704 mmc_acquire_bus(dev, dev); 1705 mmc_go_discovery(sc); 1706 mmc_release_bus(dev, dev); 1707 } 1708 1709 static int 1710 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 1711 { 1712 struct mmc_ivars *ivar = device_get_ivars(child); 1713 1714 switch (which) { 1715 default: 1716 return (EINVAL); 1717 case MMC_IVAR_DSR_IMP: 1718 *result = ivar->csd.dsr_imp; 1719 break; 1720 case MMC_IVAR_MEDIA_SIZE: 1721 *result = ivar->sec_count; 1722 break; 1723 case MMC_IVAR_RCA: 1724 *result = ivar->rca; 1725 break; 1726 case MMC_IVAR_SECTOR_SIZE: 1727 *result = MMC_SECTOR_SIZE; 1728 break; 1729 case MMC_IVAR_TRAN_SPEED: 1730 *result = mmcbr_get_clock(bus); 1731 break; 1732 case MMC_IVAR_READ_ONLY: 1733 *result = ivar->read_only; 1734 break; 1735 case MMC_IVAR_HIGH_CAP: 1736 *result = ivar->high_cap; 1737 break; 1738 case MMC_IVAR_CARD_TYPE: 1739 *result = ivar->mode; 1740 break; 1741 case MMC_IVAR_BUS_WIDTH: 1742 *result = ivar->bus_width; 1743 break; 1744 case MMC_IVAR_ERASE_SECTOR: 1745 *result = ivar->erase_sector; 1746 break; 1747 case MMC_IVAR_MAX_DATA: 1748 *result = mmcbr_get_max_data(bus); 1749 break; 1750 case MMC_IVAR_CARD_ID_STRING: 1751 *(char **)result = ivar->card_id_string; 1752 break; 1753 case MMC_IVAR_CARD_SN_STRING: 1754 *(char **)result = ivar->card_sn_string; 1755 break; 1756 } 1757 return (0); 1758 } 1759 1760 static int 1761 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 1762 { 1763 /* 1764 * None are writable ATM 1765 */ 1766 return (EINVAL); 1767 } 1768 1769 static void 1770 mmc_delayed_attach(void *xsc) 1771 { 1772 struct mmc_softc *sc = xsc; 1773 1774 mmc_scan(sc); 1775 config_intrhook_disestablish(&sc->config_intrhook); 1776 } 1777 1778 static int 1779 mmc_child_location_str(device_t dev, device_t child, char *buf, 1780 size_t buflen) 1781 { 1782 1783 snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child)); 1784 return (0); 1785 } 1786 1787 static device_method_t mmc_methods[] = { 1788 /* device_if */ 1789 DEVMETHOD(device_probe, mmc_probe), 1790 DEVMETHOD(device_attach, mmc_attach), 1791 DEVMETHOD(device_detach, mmc_detach), 1792 DEVMETHOD(device_suspend, mmc_suspend), 1793 DEVMETHOD(device_resume, mmc_resume), 1794 1795 /* Bus interface */ 1796 DEVMETHOD(bus_read_ivar, mmc_read_ivar), 1797 DEVMETHOD(bus_write_ivar, mmc_write_ivar), 1798 DEVMETHOD(bus_child_location_str, mmc_child_location_str), 1799 1800 /* MMC Bus interface */ 1801 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request), 1802 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus), 1803 DEVMETHOD(mmcbus_release_bus, mmc_release_bus), 1804 1805 DEVMETHOD_END 1806 }; 1807 1808 driver_t mmc_driver = { 1809 "mmc", 1810 mmc_methods, 1811 sizeof(struct mmc_softc), 1812 }; 1813 devclass_t mmc_devclass; 1814