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