mmc.c revision 183729
1/*- 2 * Copyright (c) 2006 Bernd Walter. All rights reserved. 3 * Copyright (c) 2006 M. Warner Losh. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * Portions of this software may have been developed with reference to 26 * the SD Simplified Specification. The following disclaimer may apply: 27 * 28 * The following conditions apply to the release of the simplified 29 * specification ("Simplified Specification") by the SD Card Association and 30 * the SD Group. The Simplified Specification is a subset of the complete SD 31 * Specification which is owned by the SD Card Association and the SD 32 * Group. This Simplified Specification is provided on a non-confidential 33 * basis subject to the disclaimers below. Any implementation of the 34 * Simplified Specification may require a license from the SD Card 35 * Association, SD Group, SD-3C LLC or other third parties. 36 * 37 * Disclaimers: 38 * 39 * The information contained in the Simplified Specification is presented only 40 * as a standard specification for SD Cards and SD Host/Ancillary products and 41 * is provided "AS-IS" without any representations or warranties of any 42 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD 43 * Card Association for any damages, any infringements of patents or other 44 * right of the SD Group, SD-3C LLC, the SD Card Association or any third 45 * parties, which may result from its use. No license is granted by 46 * implication, estoppel or otherwise under any patent or other rights of the 47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing 48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC 49 * or the SD Card Association to disclose or distribute any technical 50 * information, know-how or other confidential information to any third party. 51 */ 52 53#include <sys/cdefs.h> 54__FBSDID("$FreeBSD: head/sys/dev/mmc/mmc.c 183729 2008-10-09 19:47:28Z imp $"); 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 66#include <dev/mmc/mmcreg.h> 67#include <dev/mmc/mmcbrvar.h> 68#include <dev/mmc/mmcvar.h> 69#include "mmcbr_if.h" 70#include "mmcbus_if.h" 71 72struct mmc_softc { 73 device_t dev; 74 struct mtx sc_mtx; 75 struct intr_config_hook config_intrhook; 76 device_t owner; 77 uint32_t last_rca; 78}; 79 80/* 81 * Per-card data 82 */ 83struct mmc_ivars { 84 uint32_t raw_cid[4]; /* Raw bits of the CID */ 85 uint32_t raw_csd[4]; /* Raw bits of the CSD */ 86 uint32_t raw_scr[2]; /* Raw bits of the SCR */ 87 uint8_t raw_ext_csd[512]; /* Raw bits of the EXT_CSD */ 88 uint16_t rca; 89 enum mmc_card_mode mode; 90 struct mmc_cid cid; /* cid decoded */ 91 struct mmc_csd csd; /* csd decoded */ 92 struct mmc_scr scr; /* scr 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 */ 97 uint32_t tran_speed; /* Max speed in normal mode */ 98 uint32_t hs_tran_speed; /* Max speed in high speed mode */ 99}; 100 101#define CMD_RETRIES 3 102 103/* bus entry points */ 104static int mmc_probe(device_t dev); 105static int mmc_attach(device_t dev); 106static int mmc_detach(device_t dev); 107 108#define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 109#define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 110#define MMC_LOCK_INIT(_sc) \ 111 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 112 "mmc", MTX_DEF) 113#define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 114#define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 115#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 116 117static void mmc_delayed_attach(void *); 118static void mmc_power_down(struct mmc_softc *sc); 119static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, 120 int retries); 121static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 122 uint32_t arg, uint32_t flags, uint32_t *resp, int retries); 123static int mmc_select_card(struct mmc_softc *sc, uint16_t rca); 124static int mmc_set_bus_width(struct mmc_softc *sc, uint16_t rca, int width); 125static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr); 126static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr); 127static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd); 128 129static void 130mmc_ms_delay(int ms) 131{ 132 DELAY(1000 * ms); /* XXX BAD */ 133} 134 135static int 136mmc_probe(device_t dev) 137{ 138 139 device_set_desc(dev, "MMC/SD bus"); 140 return (0); 141} 142 143static int 144mmc_attach(device_t dev) 145{ 146 struct mmc_softc *sc; 147 148 sc = device_get_softc(dev); 149 sc->dev = dev; 150 MMC_LOCK_INIT(sc); 151 152 /* We'll probe and attach our children later, but before / mount */ 153 sc->config_intrhook.ich_func = mmc_delayed_attach; 154 sc->config_intrhook.ich_arg = sc; 155 if (config_intrhook_establish(&sc->config_intrhook) != 0) 156 device_printf(dev, "config_intrhook_establish failed\n"); 157 return (0); 158} 159 160static int 161mmc_detach(device_t dev) 162{ 163 struct mmc_softc *sc = device_get_softc(dev); 164 device_t *kids; 165 int i, nkid; 166 167 /* kill children [ph33r]. -sorbo */ 168 if (device_get_children(sc->dev, &kids, &nkid) != 0) 169 return (0); 170 for (i = 0; i < nkid; i++) { 171 device_t kid = kids[i]; 172 void *ivar = device_get_ivars(kid); 173 174 device_detach(kid); 175 device_delete_child(sc->dev, kid); 176 free(ivar, M_DEVBUF); 177 } 178 free(kids, M_TEMP); 179 mmc_power_down(sc); 180 181 MMC_LOCK_DESTROY(sc); 182 183 return (0); 184} 185 186static int 187mmc_acquire_bus(device_t busdev, device_t dev) 188{ 189 struct mmc_softc *sc; 190 struct mmc_ivars *ivar; 191 int err; 192 int rca; 193 194 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev); 195 if (err) 196 return (err); 197 sc = device_get_softc(busdev); 198 MMC_LOCK(sc); 199 if (sc->owner) 200 panic("mmc: host bridge didn't seralize us."); 201 sc->owner = dev; 202 MMC_UNLOCK(sc); 203 204 if (busdev != dev) { 205 /* 206 * Keep track of the last rca that we've selected. If 207 * we're asked to do it again, don't. We never 208 * unselect unless the bus code itself wants the mmc 209 * bus, and constantly reselecting causes problems. 210 */ 211 rca = mmc_get_rca(dev); 212 if (sc->last_rca != rca) { 213 mmc_select_card(sc, rca); 214 sc->last_rca = rca; 215 /* Prepare bus width for the new card. */ 216 ivar = device_get_ivars(dev); 217 device_printf(busdev, 218 "setting bus width to %d bits\n", 219 (ivar->bus_width == bus_width_4)?4: 220 (ivar->bus_width == bus_width_8)?8:1); 221 mmc_set_bus_width(sc, rca, ivar->bus_width); 222 mmcbr_set_bus_width(busdev, ivar->bus_width); 223 mmcbr_update_ios(busdev); 224 } 225 } else { 226 /* 227 * If there's a card selected, stand down. 228 */ 229 if (sc->last_rca != 0) { 230 mmc_select_card(sc, 0); 231 sc->last_rca = 0; 232 } 233 } 234 235 return (0); 236} 237 238static int 239mmc_release_bus(device_t busdev, device_t dev) 240{ 241 struct mmc_softc *sc; 242 int err; 243 244 sc = device_get_softc(busdev); 245 246 MMC_LOCK(sc); 247 if (!sc->owner) 248 panic("mmc: releasing unowned bus."); 249 if (sc->owner != dev) 250 panic("mmc: you don't own the bus. game over."); 251 MMC_UNLOCK(sc); 252 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev); 253 if (err) 254 return (err); 255 MMC_LOCK(sc); 256 sc->owner = NULL; 257 MMC_UNLOCK(sc); 258 return (0); 259} 260 261static void 262mmc_rescan_cards(struct mmc_softc *sc) 263{ 264 /* XXX: Look at the children and see if they respond to status */ 265} 266 267static uint32_t 268mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr) 269{ 270 271 return (ocr & MMC_OCR_VOLTAGE); 272} 273 274static int 275mmc_highest_voltage(uint32_t ocr) 276{ 277 int i; 278 279 for (i = 30; i >= 0; i--) 280 if (ocr & (1 << i)) 281 return (i); 282 return (-1); 283} 284 285static void 286mmc_wakeup(struct mmc_request *req) 287{ 288 struct mmc_softc *sc; 289 290/* printf("Wakeup for req %p done_data %p\n", req, req->done_data); */ 291 sc = (struct mmc_softc *)req->done_data; 292 MMC_LOCK(sc); 293 req->flags |= MMC_REQ_DONE; 294 wakeup(req); 295 MMC_UNLOCK(sc); 296} 297 298static int 299mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req) 300{ 301 int err; 302 303 req->done = mmc_wakeup; 304 req->done_data = sc; 305/* printf("Submitting request %p sc %p\n", req, sc); */ 306 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req); 307 MMC_LOCK(sc); 308 do { 309 err = msleep(req, &sc->sc_mtx, PZERO | PCATCH, "mmcreq", 310 hz / 10); 311 } while (!(req->flags & MMC_REQ_DONE) && err == EAGAIN); 312/* printf("Request %p done with error %d\n", req, err); */ 313 MMC_UNLOCK(sc); 314 return (err); 315} 316 317static int 318mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req) 319{ 320 struct mmc_softc *sc = device_get_softc(brdev); 321 322 return (mmc_wait_for_req(sc, req)); 323} 324 325static int 326mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries) 327{ 328 struct mmc_request mreq; 329 330 memset(&mreq, 0, sizeof(mreq)); 331 memset(cmd->resp, 0, sizeof(cmd->resp)); 332 cmd->retries = retries; 333 mreq.cmd = cmd; 334/* printf("CMD: %x ARG %x\n", cmd->opcode, cmd->arg); */ 335 mmc_wait_for_req(sc, &mreq); 336 return (cmd->error); 337} 338 339static int 340mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca, 341 struct mmc_command *cmd, int retries) 342{ 343 struct mmc_command appcmd; 344 int err = MMC_ERR_NONE, i; 345 346 for (i = 0; i <= retries; i++) { 347 appcmd.opcode = MMC_APP_CMD; 348 appcmd.arg = rca << 16; 349 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 350 appcmd.data = NULL; 351 mmc_wait_for_cmd(sc, &appcmd, 0); 352 err = appcmd.error; 353 if (err != MMC_ERR_NONE) 354 continue; 355 if (!(appcmd.resp[0] & R1_APP_CMD)) 356 return MMC_ERR_FAILED; 357 mmc_wait_for_cmd(sc, cmd, 0); 358 err = cmd->error; 359 if (err == MMC_ERR_NONE) 360 break; 361 } 362 return (err); 363} 364 365static int 366mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 367 uint32_t arg, uint32_t flags, uint32_t *resp, int retries) 368{ 369 struct mmc_command cmd; 370 int err; 371 372 memset(&cmd, 0, sizeof(cmd)); 373 cmd.opcode = opcode; 374 cmd.arg = arg; 375 cmd.flags = flags; 376 cmd.data = NULL; 377 err = mmc_wait_for_cmd(sc, &cmd, retries); 378 if (err) 379 return (err); 380 if (cmd.error) 381 return (cmd.error); 382 if (resp) { 383 if (flags & MMC_RSP_136) 384 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t)); 385 else 386 *resp = cmd.resp[0]; 387 } 388 return (0); 389} 390 391static void 392mmc_idle_cards(struct mmc_softc *sc) 393{ 394 device_t dev; 395 struct mmc_command cmd; 396 397 dev = sc->dev; 398 mmcbr_set_chip_select(dev, cs_high); 399 mmcbr_update_ios(dev); 400 mmc_ms_delay(1); 401 402 memset(&cmd, 0, sizeof(cmd)); 403 cmd.opcode = MMC_GO_IDLE_STATE; 404 cmd.arg = 0; 405 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 406 cmd.data = NULL; 407 mmc_wait_for_cmd(sc, &cmd, 0); 408 mmc_ms_delay(1); 409 410 mmcbr_set_chip_select(dev, cs_dontcare); 411 mmcbr_update_ios(dev); 412 mmc_ms_delay(1); 413} 414 415static int 416mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 417{ 418 struct mmc_command cmd; 419 int err = MMC_ERR_NONE, i; 420 421 memset(&cmd, 0, sizeof(cmd)); 422 cmd.opcode = ACMD_SD_SEND_OP_COND; 423 cmd.arg = ocr; 424 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 425 cmd.data = NULL; 426 427 for (i = 0; i < 100; i++) { 428 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES); 429 if (err != MMC_ERR_NONE) 430 break; 431 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 432 (ocr & MMC_OCR_VOLTAGE) == 0) 433 break; 434 err = MMC_ERR_TIMEOUT; 435 mmc_ms_delay(10); 436 } 437 if (rocr && err == MMC_ERR_NONE) 438 *rocr = cmd.resp[0]; 439 return (err); 440} 441 442static int 443mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 444{ 445 struct mmc_command cmd; 446 int err = MMC_ERR_NONE, i; 447 448 memset(&cmd, 0, sizeof(cmd)); 449 cmd.opcode = MMC_SEND_OP_COND; 450 cmd.arg = ocr; 451 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 452 cmd.data = NULL; 453 454 for (i = 0; i < 100; i++) { 455 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 456 if (err != MMC_ERR_NONE) 457 break; 458 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || 459 (ocr & MMC_OCR_VOLTAGE) == 0) 460 break; 461 err = MMC_ERR_TIMEOUT; 462 mmc_ms_delay(10); 463 } 464 if (rocr && err == MMC_ERR_NONE) 465 *rocr = cmd.resp[0]; 466 return (err); 467} 468 469static int 470mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs) 471{ 472 struct mmc_command cmd; 473 int err; 474 475 memset(&cmd, 0, sizeof(cmd)); 476 cmd.opcode = SD_SEND_IF_COND; 477 cmd.arg = (vhs << 8) + 0xAA; 478 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR; 479 cmd.data = NULL; 480 481 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 482 return (err); 483} 484 485static void 486mmc_power_up(struct mmc_softc *sc) 487{ 488 device_t dev; 489 490 dev = sc->dev; 491 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev))); 492 mmcbr_set_bus_mode(dev, opendrain); 493 mmcbr_set_chip_select(dev, cs_dontcare); 494 mmcbr_set_bus_width(dev, bus_width_1); 495 mmcbr_set_power_mode(dev, power_up); 496 mmcbr_set_clock(dev, 0); 497 mmcbr_update_ios(dev); 498 mmc_ms_delay(1); 499 500 mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev)); 501 mmcbr_set_timing(dev, bus_timing_normal); 502 mmcbr_set_power_mode(dev, power_on); 503 mmcbr_update_ios(dev); 504 mmc_ms_delay(2); 505} 506 507static void 508mmc_power_down(struct mmc_softc *sc) 509{ 510 device_t dev = sc->dev; 511 512 mmcbr_set_bus_mode(dev, opendrain); 513 mmcbr_set_chip_select(dev, cs_dontcare); 514 mmcbr_set_bus_width(dev, bus_width_1); 515 mmcbr_set_power_mode(dev, power_off); 516 mmcbr_set_clock(dev, 0); 517 mmcbr_set_timing(dev, bus_timing_normal); 518 mmcbr_update_ios(dev); 519} 520 521static int 522mmc_select_card(struct mmc_softc *sc, uint16_t rca) 523{ 524 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, ((uint32_t)rca) << 16, 525 MMC_RSP_R1B | MMC_CMD_AC, NULL, CMD_RETRIES)); 526} 527 528static int 529mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value) 530{ 531 struct mmc_command cmd; 532 int err; 533 534 cmd.opcode = MMC_SWITCH_FUNC; 535 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) | 536 (index << 16) | 537 (value << 8) | 538 set; 539 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; 540 cmd.data = NULL; 541 err = mmc_wait_for_cmd(sc, &cmd, 0); 542 return (err); 543} 544 545static int 546mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value, uint8_t *res) 547{ 548 int err; 549 struct mmc_command cmd; 550 struct mmc_data data; 551 552 memset(&cmd, 0, sizeof(struct mmc_command)); 553 memset(&data, 0, sizeof(struct mmc_data)); 554 555 memset(res, 0, 64); 556 cmd.opcode = SD_SWITCH_FUNC; 557 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 558 cmd.arg = mode << 31; 559 cmd.arg |= 0x00FFFFFF; 560 cmd.arg &= ~(0xF << (grp * 4)); 561 cmd.arg |= value << (grp * 4); 562 cmd.data = &data; 563 564 data.data = res; 565 data.len = 64; 566 data.flags = MMC_DATA_READ; 567 568 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 569 return (err); 570} 571 572static int 573mmc_set_bus_width(struct mmc_softc *sc, uint16_t rca, int width) 574{ 575 int err; 576 577 if (mmcbr_get_mode(sc->dev) == mode_sd) { 578 struct mmc_command cmd; 579 580 memset(&cmd, 0, sizeof(struct mmc_command)); 581 cmd.opcode = ACMD_SET_BUS_WIDTH; 582 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 583 switch (width) { 584 case bus_width_1: 585 cmd.arg = SD_BUS_WIDTH_1; 586 break; 587 case bus_width_4: 588 cmd.arg = SD_BUS_WIDTH_4; 589 break; 590 default: 591 return (MMC_ERR_INVALID); 592 } 593 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 594 } else { 595 uint8_t value; 596 597 switch (width) { 598 case bus_width_1: 599 value = EXT_CSD_BUS_WIDTH_1; 600 break; 601 case bus_width_4: 602 value = EXT_CSD_BUS_WIDTH_4; 603 break; 604 case bus_width_8: 605 value = EXT_CSD_BUS_WIDTH_8; 606 break; 607 default: 608 return (MMC_ERR_INVALID); 609 } 610 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, value); 611 } 612 return (err); 613} 614 615static int 616mmc_set_timing(struct mmc_softc *sc, int timing) 617{ 618 int err; 619 uint8_t value; 620 621 switch (timing) { 622 case bus_timing_normal: 623 value = 0; 624 break; 625 case bus_timing_hs: 626 value = 1; 627 break; 628 default: 629 return (MMC_ERR_INVALID); 630 } 631 if (mmcbr_get_mode(sc->dev) == mode_sd) { 632 u_char switch_res[64]; 633 634 err = mmc_sd_switch(sc, 1, 0, value, switch_res); 635 } else { 636 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, 637 EXT_CSD_HS_TIMING, value); 638 } 639 return (err); 640} 641 642static int 643mmc_test_bus_width(struct mmc_softc *sc) 644{ 645 struct mmc_command cmd; 646 struct mmc_data data; 647 int err; 648 uint8_t buf[8]; 649 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 650 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 651 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, }; 652 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, }; 653 654 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) { 655 mmcbr_set_bus_width(sc->dev, bus_width_8); 656 mmcbr_update_ios(sc->dev); 657 658 cmd.opcode = MMC_BUSTEST_W; 659 cmd.arg = 0; 660 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 661 cmd.data = &data; 662 663 data.data = p8; 664 data.len = 8; 665 data.flags = MMC_DATA_WRITE; 666 mmc_wait_for_cmd(sc, &cmd, 0); 667 668 cmd.opcode = MMC_BUSTEST_R; 669 cmd.arg = 0; 670 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 671 cmd.data = &data; 672 673 data.data = buf; 674 data.len = 8; 675 data.flags = MMC_DATA_READ; 676 err = mmc_wait_for_cmd(sc, &cmd, 0); 677 678 mmcbr_set_bus_width(sc->dev, bus_width_1); 679 mmcbr_update_ios(sc->dev); 680 681 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0) 682 return (bus_width_8); 683 } 684 685 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) { 686 mmcbr_set_bus_width(sc->dev, bus_width_4); 687 mmcbr_update_ios(sc->dev); 688 689 cmd.opcode = MMC_BUSTEST_W; 690 cmd.arg = 0; 691 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 692 cmd.data = &data; 693 694 data.data = p4; 695 data.len = 4; 696 data.flags = MMC_DATA_WRITE; 697 mmc_wait_for_cmd(sc, &cmd, 0); 698 699 cmd.opcode = MMC_BUSTEST_R; 700 cmd.arg = 0; 701 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 702 cmd.data = &data; 703 704 data.data = buf; 705 data.len = 4; 706 data.flags = MMC_DATA_READ; 707 err = mmc_wait_for_cmd(sc, &cmd, 0); 708 709 mmcbr_set_bus_width(sc->dev, bus_width_1); 710 mmcbr_update_ios(sc->dev); 711 712 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0) 713 return (bus_width_4); 714 } 715 return (bus_width_1); 716} 717 718static uint32_t 719mmc_get_bits(uint32_t *bits, int start, int size) 720{ 721 const int bit_len = 128; 722 const int i = (bit_len / 32) - (start / 32) - 1; 723 const int shift = start & 31; 724 uint32_t retval = bits[i] >> shift; 725 if (size + shift > 32) 726 retval |= bits[i - 1] << (32 - shift); 727 return (retval & ((1 << size) - 1)); 728} 729 730static void 731mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid) 732{ 733 int i; 734 735 /* There's no version info, so we take it on faith */ 736 memset(cid, 0, sizeof(*cid)); 737 cid->mid = mmc_get_bits(raw_cid, 120, 8); 738 cid->oid = mmc_get_bits(raw_cid, 104, 16); 739 for (i = 0; i < 5; i++) 740 cid->pnm[i] = mmc_get_bits(raw_cid, 96 - i * 8, 8); 741 cid->prv = mmc_get_bits(raw_cid, 56, 8); 742 cid->psn = mmc_get_bits(raw_cid, 24, 32); 743 cid->mdt_year = mmc_get_bits(raw_cid, 12, 8) + 2001; 744 cid->mdt_month = mmc_get_bits(raw_cid, 8, 4); 745} 746 747static void 748mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid) 749{ 750 int i; 751 752 /* There's no version info, so we take it on faith */ 753 memset(cid, 0, sizeof(*cid)); 754 cid->mid = mmc_get_bits(raw_cid, 120, 8); 755 cid->oid = mmc_get_bits(raw_cid, 104, 8); 756 for (i = 0; i < 6; i++) 757 cid->pnm[i] = mmc_get_bits(raw_cid, 96 - i * 8, 8); 758 cid->prv = mmc_get_bits(raw_cid, 48, 8); 759 cid->psn = mmc_get_bits(raw_cid, 16, 32); 760 cid->mdt_month = mmc_get_bits(raw_cid, 12, 4); 761 cid->mdt_year = mmc_get_bits(raw_cid, 8, 4) + 1997; 762} 763 764static const int exp[8] = { 765 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 766}; 767static const int mant[16] = { 768 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 769}; 770static const int cur_min[8] = { 771 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000 772}; 773static const int cur_max[8] = { 774 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000 775}; 776 777static void 778mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd) 779{ 780 int v; 781 int m; 782 int e; 783 784 memset(csd, 0, sizeof(*csd)); 785 csd->csd_structure = v = mmc_get_bits(raw_csd, 126, 2); 786 if (v == 0) { 787 m = mmc_get_bits(raw_csd, 115, 4); 788 e = mmc_get_bits(raw_csd, 112, 3); 789 csd->tacc = exp[e] * mant[m] + 9 / 10; 790 csd->nsac = mmc_get_bits(raw_csd, 104, 8) * 100; 791 m = mmc_get_bits(raw_csd, 99, 4); 792 e = mmc_get_bits(raw_csd, 96, 3); 793 csd->tran_speed = exp[e] * 10000 * mant[m]; 794 csd->ccc = mmc_get_bits(raw_csd, 84, 12); 795 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 80, 4); 796 csd->read_bl_partial = mmc_get_bits(raw_csd, 79, 1); 797 csd->write_blk_misalign = mmc_get_bits(raw_csd, 78, 1); 798 csd->read_blk_misalign = mmc_get_bits(raw_csd, 77, 1); 799 csd->dsr_imp = mmc_get_bits(raw_csd, 76, 1); 800 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 59, 3)]; 801 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 56, 3)]; 802 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 53, 3)]; 803 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 50, 3)]; 804 m = mmc_get_bits(raw_csd, 62, 12); 805 e = mmc_get_bits(raw_csd, 47, 3); 806 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 807 csd->erase_blk_en = mmc_get_bits(raw_csd, 46, 1); 808 csd->sector_size = mmc_get_bits(raw_csd, 39, 7); 809 csd->wp_grp_size = mmc_get_bits(raw_csd, 32, 7); 810 csd->wp_grp_enable = mmc_get_bits(raw_csd, 31, 1); 811 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 26, 3); 812 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 22, 4); 813 csd->write_bl_partial = mmc_get_bits(raw_csd, 21, 1); 814 } else if (v == 1) { 815 m = mmc_get_bits(raw_csd, 115, 4); 816 e = mmc_get_bits(raw_csd, 112, 3); 817 csd->tacc = exp[e] * mant[m] + 9 / 10; 818 csd->nsac = mmc_get_bits(raw_csd, 104, 8) * 100; 819 m = mmc_get_bits(raw_csd, 99, 4); 820 e = mmc_get_bits(raw_csd, 96, 3); 821 csd->tran_speed = exp[e] * 10000 * mant[m]; 822 csd->ccc = mmc_get_bits(raw_csd, 84, 12); 823 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 80, 4); 824 csd->read_bl_partial = mmc_get_bits(raw_csd, 79, 1); 825 csd->write_blk_misalign = mmc_get_bits(raw_csd, 78, 1); 826 csd->read_blk_misalign = mmc_get_bits(raw_csd, 77, 1); 827 csd->dsr_imp = mmc_get_bits(raw_csd, 76, 1); 828 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 48, 22) + 1) * 829 512 * 1024; 830 csd->erase_blk_en = mmc_get_bits(raw_csd, 46, 1); 831 csd->sector_size = mmc_get_bits(raw_csd, 39, 7); 832 csd->wp_grp_size = mmc_get_bits(raw_csd, 32, 7); 833 csd->wp_grp_enable = mmc_get_bits(raw_csd, 31, 1); 834 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 26, 3); 835 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 22, 4); 836 csd->write_bl_partial = mmc_get_bits(raw_csd, 21, 1); 837 } else 838 panic("unknown SD CSD version"); 839} 840 841static void 842mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd) 843{ 844 int m; 845 int e; 846 847 memset(csd, 0, sizeof(*csd)); 848 csd->csd_structure = mmc_get_bits(raw_csd, 126, 2); 849 csd->spec_vers = mmc_get_bits(raw_csd, 122, 4); 850 m = mmc_get_bits(raw_csd, 115, 4); 851 e = mmc_get_bits(raw_csd, 112, 3); 852 csd->tacc = exp[e] * mant[m] + 9 / 10; 853 csd->nsac = mmc_get_bits(raw_csd, 104, 8) * 100; 854 m = mmc_get_bits(raw_csd, 99, 4); 855 e = mmc_get_bits(raw_csd, 96, 3); 856 csd->tran_speed = exp[e] * 10000 * mant[m]; 857 csd->ccc = mmc_get_bits(raw_csd, 84, 12); 858 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 80, 4); 859 csd->read_bl_partial = mmc_get_bits(raw_csd, 79, 1); 860 csd->write_blk_misalign = mmc_get_bits(raw_csd, 78, 1); 861 csd->read_blk_misalign = mmc_get_bits(raw_csd, 77, 1); 862 csd->dsr_imp = mmc_get_bits(raw_csd, 76, 1); 863 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 59, 3)]; 864 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 56, 3)]; 865 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 53, 3)]; 866 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 50, 3)]; 867 m = mmc_get_bits(raw_csd, 62, 12); 868 e = mmc_get_bits(raw_csd, 47, 3); 869 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 870// csd->erase_blk_en = mmc_get_bits(raw_csd, 46, 1); 871// csd->sector_size = mmc_get_bits(raw_csd, 39, 7); 872 csd->wp_grp_size = mmc_get_bits(raw_csd, 32, 5); 873 csd->wp_grp_enable = mmc_get_bits(raw_csd, 31, 1); 874 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 26, 3); 875 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 22, 4); 876 csd->write_bl_partial = mmc_get_bits(raw_csd, 21, 1); 877} 878 879static void 880mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr) 881{ 882 unsigned int scr_struct; 883 uint32_t tmp[4]; 884 885 tmp[3] = raw_scr[1]; 886 tmp[2] = raw_scr[0]; 887 888 memset(scr, 0, sizeof(*scr)); 889 890 scr_struct = mmc_get_bits(tmp, 60, 4); 891 if (scr_struct != 0) { 892 printf("Unrecognised SCR structure version %d\n", 893 scr_struct); 894 return; 895 } 896 scr->sda_vsn = mmc_get_bits(tmp, 56, 4); 897 scr->bus_widths = mmc_get_bits(tmp, 48, 4); 898} 899 900static int 901mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid) 902{ 903 struct mmc_command cmd; 904 int err; 905 906 cmd.opcode = MMC_ALL_SEND_CID; 907 cmd.arg = 0; 908 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 909 cmd.data = NULL; 910 err = mmc_wait_for_cmd(sc, &cmd, 0); 911 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 912 return (err); 913} 914 915static int 916mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcid) 917{ 918 struct mmc_command cmd; 919 int err; 920 921 cmd.opcode = MMC_SEND_CSD; 922 cmd.arg = rca << 16; 923 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 924 cmd.data = NULL; 925 err = mmc_wait_for_cmd(sc, &cmd, 0); 926 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 927 return (err); 928} 929 930static int 931mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr) 932{ 933 int err; 934 struct mmc_command cmd; 935 struct mmc_data data; 936 937 memset(&cmd, 0, sizeof(struct mmc_command)); 938 memset(&data, 0, sizeof(struct mmc_data)); 939 940 memset(rawscr, 0, 8); 941 cmd.opcode = ACMD_SEND_SCR; 942 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 943 cmd.arg = 0; 944 cmd.data = &data; 945 946 data.data = rawscr; 947 data.len = 8; 948 data.flags = MMC_DATA_READ; 949 950 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES); 951 rawscr[0] = be32toh(rawscr[0]); 952 rawscr[1] = be32toh(rawscr[1]); 953 return (err); 954} 955 956static int 957mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd) 958{ 959 int err; 960 struct mmc_command cmd; 961 struct mmc_data data; 962 963 memset(&cmd, 0, sizeof(struct mmc_command)); 964 memset(&data, 0, sizeof(struct mmc_data)); 965 966 memset(rawextcsd, 0, 512); 967 cmd.opcode = MMC_SEND_EXT_CSD; 968 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; 969 cmd.arg = 0; 970 cmd.data = &data; 971 972 data.data = rawextcsd; 973 data.len = 512; 974 data.flags = MMC_DATA_READ; 975 976 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 977 return (err); 978} 979 980static int 981mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp) 982{ 983 struct mmc_command cmd; 984 int err; 985 986 cmd.opcode = MMC_SET_RELATIVE_ADDR; 987 cmd.arg = resp << 16; 988 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 989 cmd.data = NULL; 990 err = mmc_wait_for_cmd(sc, &cmd, 0); 991 return (err); 992} 993 994static int 995mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp) 996{ 997 struct mmc_command cmd; 998 int err; 999 1000 cmd.opcode = SD_SEND_RELATIVE_ADDR; 1001 cmd.arg = 0; 1002 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 1003 cmd.data = NULL; 1004 err = mmc_wait_for_cmd(sc, &cmd, 0); 1005 *resp = cmd.resp[0]; 1006 return (err); 1007} 1008 1009static void 1010mmc_discover_cards(struct mmc_softc *sc) 1011{ 1012 struct mmc_ivars *ivar; 1013 int err; 1014 uint32_t resp; 1015 device_t child; 1016 uint16_t rca = 2; 1017 u_char switch_res[64]; 1018 1019 while (1) { 1020 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, 1021 M_WAITOK | M_ZERO); 1022 if (!ivar) 1023 return; 1024 err = mmc_all_send_cid(sc, ivar->raw_cid); 1025 if (err == MMC_ERR_TIMEOUT) 1026 break; 1027 if (err != MMC_ERR_NONE) { 1028 device_printf(sc->dev, "Error reading CID %d\n", err); 1029 break; 1030 } 1031 if (mmcbr_get_ro(sc->dev)) 1032 ivar->read_only = 1; 1033 ivar->bus_width = bus_width_1; 1034 ivar->mode = mmcbr_get_mode(sc->dev); 1035 if (ivar->mode == mode_sd) { 1036 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid); 1037 mmc_send_relative_addr(sc, &resp); 1038 ivar->rca = resp >> 16; 1039 /* Get card CSD. */ 1040 mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 1041 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd); 1042 if (ivar->csd.csd_structure > 0) 1043 ivar->high_cap = 1; 1044 ivar->tran_speed = ivar->csd.tran_speed; 1045 /* Get card SCR. Card must be selected to fetch it. */ 1046 mmc_select_card(sc, ivar->rca); 1047 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr); 1048 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr); 1049 /* Get card switch capabilities. */ 1050 if ((ivar->scr.sda_vsn >= 1) && 1051 (ivar->csd.ccc & (1<<10))) { 1052 mmc_sd_switch(sc, 0, 0, 0xF, switch_res); 1053 if (switch_res[13] & 2) { 1054 ivar->timing = bus_timing_hs; 1055 ivar->hs_tran_speed = 50000000; 1056 } 1057 } 1058 mmc_select_card(sc, 0); 1059 /* Find max supported bus width. */ 1060 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) && 1061 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) 1062 ivar->bus_width = bus_width_4; 1063 /* Add device. */ 1064 child = device_add_child(sc->dev, NULL, -1); 1065 device_set_ivars(child, ivar); 1066 return; 1067 } 1068 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid); 1069 ivar->rca = rca++; 1070 mmc_set_relative_addr(sc, ivar->rca); 1071 /* Get card CSD. */ 1072 mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 1073 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd); 1074 ivar->tran_speed = ivar->csd.tran_speed; 1075 /* Only MMC >= 4.x cards support EXT_CSD. */ 1076 if (ivar->csd.spec_vers >= 4) { 1077 /* Card must be selected to fetch EXT_CSD. */ 1078 mmc_select_card(sc, ivar->rca); 1079 mmc_send_ext_csd(sc, ivar->raw_ext_csd); 1080 /* Get card speed in high speed mode. */ 1081 ivar->timing = bus_timing_hs; 1082 if (((uint8_t *)(ivar->raw_ext_csd))[EXT_CSD_CARD_TYPE] 1083 & EXT_CSD_CARD_TYPE_52) 1084 ivar->hs_tran_speed = 52000000; 1085 else if (((uint8_t *)(ivar->raw_ext_csd))[EXT_CSD_CARD_TYPE] 1086 & EXT_CSD_CARD_TYPE_26) 1087 ivar->hs_tran_speed = 26000000; 1088 else 1089 ivar->hs_tran_speed = ivar->tran_speed; 1090 /* Find max supported bus width. */ 1091 ivar->bus_width = mmc_test_bus_width(sc); 1092 mmc_select_card(sc, 0); 1093 } else { 1094 ivar->bus_width = bus_width_1; 1095 ivar->timing = bus_timing_normal; 1096 } 1097 /* Add device. */ 1098 child = device_add_child(sc->dev, NULL, -1); 1099 device_set_ivars(child, ivar); 1100 } 1101 free(ivar, M_DEVBUF); 1102} 1103 1104static void 1105mmc_go_discovery(struct mmc_softc *sc) 1106{ 1107 uint32_t ocr; 1108 device_t dev; 1109 int err; 1110 1111 dev = sc->dev; 1112 if (mmcbr_get_power_mode(dev) != power_on) { 1113 /* 1114 * First, try SD modes 1115 */ 1116 mmcbr_set_mode(dev, mode_sd); 1117 mmc_power_up(sc); 1118 mmcbr_set_bus_mode(dev, pushpull); 1119 mmc_idle_cards(sc); 1120 err = mmc_send_if_cond(sc, 1); 1121 if (mmc_send_app_op_cond(sc, err?0:MMC_OCR_CCS, &ocr) != 1122 MMC_ERR_NONE) { 1123 /* 1124 * Failed, try MMC 1125 */ 1126 mmcbr_set_mode(dev, mode_mmc); 1127 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) 1128 return; /* Failed both, punt! XXX powerdown? */ 1129 } 1130 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr)); 1131 if (mmcbr_get_ocr(dev) != 0) 1132 mmc_idle_cards(sc); 1133 } else { 1134 mmcbr_set_bus_mode(dev, opendrain); 1135 mmcbr_set_clock(dev, mmcbr_get_f_min(dev)); 1136 mmcbr_update_ios(dev); 1137 /* XXX recompute vdd based on new cards? */ 1138 } 1139 /* 1140 * Make sure that we have a mutually agreeable voltage to at least 1141 * one card on the bus. 1142 */ 1143 if (mmcbr_get_ocr(dev) == 0) 1144 return; 1145 /* 1146 * Reselect the cards after we've idled them above. 1147 */ 1148 if (mmcbr_get_mode(dev) == mode_sd) { 1149 err = mmc_send_if_cond(sc, 1); 1150 mmc_send_app_op_cond(sc, 1151 (err?0:MMC_OCR_CCS)|mmcbr_get_ocr(dev), NULL); 1152 } else 1153 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL); 1154 mmc_discover_cards(sc); 1155 1156 mmcbr_set_bus_mode(dev, pushpull); 1157 mmcbr_update_ios(dev); 1158 bus_generic_attach(dev); 1159/* mmc_update_children_sysctl(dev);*/ 1160} 1161 1162static int 1163mmc_calculate_clock(struct mmc_softc *sc) 1164{ 1165 int max_dtr, max_hs_dtr, max_timing; 1166 int nkid, i, f_min, f_max; 1167 device_t *kids; 1168 struct mmc_ivars *ivar; 1169 1170 f_min = mmcbr_get_f_min(sc->dev); 1171 f_max = mmcbr_get_f_max(sc->dev); 1172 max_dtr = max_hs_dtr = f_max; 1173 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED)) 1174 max_timing = bus_timing_hs; 1175 else 1176 max_timing = bus_timing_normal; 1177 if (device_get_children(sc->dev, &kids, &nkid) != 0) 1178 panic("can't get children"); 1179 for (i = 0; i < nkid; i++) { 1180 ivar = device_get_ivars(kids[i]); 1181 if (ivar->timing < max_timing) 1182 max_timing = ivar->timing; 1183 if (ivar->tran_speed < max_dtr) 1184 max_dtr = ivar->tran_speed; 1185 if (ivar->hs_tran_speed < max_dtr) 1186 max_hs_dtr = ivar->hs_tran_speed; 1187 } 1188 for (i = 0; i < nkid; i++) { 1189 ivar = device_get_ivars(kids[i]); 1190 if (ivar->timing == bus_timing_normal) 1191 continue; 1192 mmc_select_card(sc, ivar->rca); 1193 mmc_set_timing(sc, max_timing); 1194 } 1195 mmc_select_card(sc, 0); 1196 free(kids, M_TEMP); 1197 if (max_timing == bus_timing_hs) 1198 max_dtr = max_hs_dtr; 1199 device_printf(sc->dev, "setting transfer rate to %d.%03dMHz%s\n", 1200 max_dtr / 1000000, (max_dtr / 1000) % 1000, 1201 (max_timing == bus_timing_hs)?" with high speed timing":""); 1202 mmcbr_set_timing(sc->dev, max_timing); 1203 mmcbr_set_clock(sc->dev, max_dtr); 1204 mmcbr_update_ios(sc->dev); 1205 return max_dtr; 1206} 1207 1208static void 1209mmc_scan(struct mmc_softc *sc) 1210{ 1211 device_t dev; 1212 1213 dev = sc->dev; 1214 mmc_acquire_bus(dev, dev); 1215 1216 if (mmcbr_get_power_mode(dev) == power_on) 1217 mmc_rescan_cards(sc); 1218 mmc_go_discovery(sc); 1219 mmc_calculate_clock(sc); 1220 1221 mmc_release_bus(dev, dev); 1222 /* XXX probe/attach/detach children? */ 1223} 1224 1225static int 1226mmc_read_ivar(device_t bus, device_t child, int which, u_char *result) 1227{ 1228 struct mmc_ivars *ivar = device_get_ivars(child); 1229 1230 switch (which) { 1231 default: 1232 return (EINVAL); 1233 case MMC_IVAR_DSR_IMP: 1234 *(int *)result = ivar->csd.dsr_imp; 1235 break; 1236 case MMC_IVAR_MEDIA_SIZE: 1237 *(off_t *)result = ivar->csd.capacity / MMC_SECTOR_SIZE; 1238 break; 1239 case MMC_IVAR_RCA: 1240 *(int *)result = ivar->rca; 1241 break; 1242 case MMC_IVAR_SECTOR_SIZE: 1243 *(int *)result = MMC_SECTOR_SIZE; 1244 break; 1245 case MMC_IVAR_TRAN_SPEED: 1246 *(int *)result = ivar->csd.tran_speed; 1247 break; 1248 case MMC_IVAR_READ_ONLY: 1249 *(int *)result = ivar->read_only; 1250 break; 1251 case MMC_IVAR_HIGH_CAP: 1252 *(int *)result = ivar->high_cap; 1253 break; 1254 } 1255 return (0); 1256} 1257 1258static int 1259mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 1260{ 1261 /* 1262 * None are writable ATM 1263 */ 1264 return (EINVAL); 1265} 1266 1267 1268static void 1269mmc_delayed_attach(void *xsc) 1270{ 1271 struct mmc_softc *sc = xsc; 1272 1273 mmc_scan(sc); 1274 config_intrhook_disestablish(&sc->config_intrhook); 1275} 1276 1277static device_method_t mmc_methods[] = { 1278 /* device_if */ 1279 DEVMETHOD(device_probe, mmc_probe), 1280 DEVMETHOD(device_attach, mmc_attach), 1281 DEVMETHOD(device_detach, mmc_detach), 1282 1283 /* Bus interface */ 1284 DEVMETHOD(bus_read_ivar, mmc_read_ivar), 1285 DEVMETHOD(bus_write_ivar, mmc_write_ivar), 1286 1287 /* MMC Bus interface */ 1288 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request), 1289 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus), 1290 DEVMETHOD(mmcbus_release_bus, mmc_release_bus), 1291 1292 {0, 0}, 1293}; 1294 1295static driver_t mmc_driver = { 1296 "mmc", 1297 mmc_methods, 1298 sizeof(struct mmc_softc), 1299}; 1300static devclass_t mmc_devclass; 1301 1302 1303DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, 0, 0); 1304DRIVER_MODULE(mmc, sdhci, mmc_driver, mmc_devclass, 0, 0); 1305