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