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