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