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