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