mmc.c revision 183449
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 183449 2008-09-28 23:24:52Z 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 65#include <dev/mmc/mmcreg.h> 66#include <dev/mmc/mmcbrvar.h> 67#include <dev/mmc/mmcvar.h> 68#include "mmcbr_if.h" 69#include "mmcbus_if.h" 70 71struct mmc_softc { 72 device_t dev; 73 struct mtx sc_mtx; 74 struct intr_config_hook config_intrhook; 75 device_t owner; 76 uint32_t last_rca; 77}; 78 79/* 80 * Per-card data 81 */ 82struct mmc_ivars { 83 uint32_t raw_cid[4]; /* Raw bits of the CID */ 84 uint32_t raw_csd[4]; /* Raw bits of the CSD */ 85 uint16_t rca; 86 enum mmc_card_mode mode; 87 struct mmc_cid cid; /* cid decoded */ 88 struct mmc_csd csd; /* csd decoded */ 89 u_char read_only; /* True when the device is read-only */ 90}; 91 92#define CMD_RETRIES 3 93 94/* bus entry points */ 95static int mmc_probe(device_t dev); 96static int mmc_attach(device_t dev); 97static int mmc_detach(device_t dev); 98 99#define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 100#define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 101#define MMC_LOCK_INIT(_sc) \ 102 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 103 "mmc", MTX_DEF) 104#define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 105#define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 106#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 107 108static void mmc_delayed_attach(void *); 109static void mmc_power_down(struct mmc_softc *sc); 110static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, 111 int retries); 112static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 113 uint32_t arg, uint32_t flags, uint32_t *resp, int retries); 114 115static void 116mmc_ms_delay(int ms) 117{ 118 DELAY(1000 * ms); /* XXX BAD */ 119} 120 121static int 122mmc_probe(device_t dev) 123{ 124 125 device_set_desc(dev, "MMC/SD bus"); 126 return (0); 127} 128 129static int 130mmc_attach(device_t dev) 131{ 132 struct mmc_softc *sc; 133 134 sc = device_get_softc(dev); 135 sc->dev = dev; 136 MMC_LOCK_INIT(sc); 137 138 /* We'll probe and attach our children later, but before / mount */ 139 sc->config_intrhook.ich_func = mmc_delayed_attach; 140 sc->config_intrhook.ich_arg = sc; 141 if (config_intrhook_establish(&sc->config_intrhook) != 0) 142 device_printf(dev, "config_intrhook_establish failed\n"); 143 return (0); 144} 145 146static int 147mmc_detach(device_t dev) 148{ 149 struct mmc_softc *sc = device_get_softc(dev); 150 device_t *kids; 151 int i, nkid; 152 153 /* kill children [ph33r]. -sorbo */ 154 if (device_get_children(sc->dev, &kids, &nkid) != 0) 155 return 0; 156 for (i = 0; i < nkid; i++) { 157 device_t kid = kids[i]; 158 void *ivar = device_get_ivars(kid); 159 160 device_detach(kid); 161 device_delete_child(sc->dev, kid); 162 free(ivar, M_DEVBUF); 163 } 164 free(kids, M_TEMP); 165 mmc_power_down(sc); 166 167 MMC_LOCK_DESTROY(sc); 168 169 return 0; 170} 171 172static int 173mmc_acquire_bus(device_t busdev, device_t dev) 174{ 175 struct mmc_softc *sc; 176 int err; 177 int rca; 178 179 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), dev); 180 if (err) 181 return (err); 182 sc = device_get_softc(busdev); 183 MMC_LOCK(sc); 184 if (sc->owner) 185 panic("mmc: host bridge didn't seralize us."); 186 sc->owner = dev; 187 MMC_UNLOCK(sc); 188 189 if (busdev != dev) { 190 // Keep track of the last rca that we've selected. If 191 // we're asked to do it again, don't. We never unselect 192 // unless the bus code itself wants the mmc bus. 193 rca = mmc_get_rca(dev); 194 if (sc->last_rca != rca) { 195 mmc_wait_for_command(sc, MMC_SELECT_CARD, rca << 16, 196 MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES); 197 sc->last_rca = rca; 198 } 199 // XXX should set bus width here? 200 } else { 201 // If there's a card selected, stand down. 202 if (sc->last_rca != 0) { 203 mmc_wait_for_command(sc, MMC_SELECT_CARD, 0, 204 MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES); 205 sc->last_rca = 0; 206 } 207 // XXX should set bus width here? 208 } 209 210 return (0); 211} 212 213static int 214mmc_release_bus(device_t busdev, device_t dev) 215{ 216 struct mmc_softc *sc; 217 int err; 218 219 sc = device_get_softc(busdev); 220 221 MMC_LOCK(sc); 222 if (!sc->owner) 223 panic("mmc: releasing unowned bus."); 224 if (sc->owner != dev) 225 panic("mmc: you don't own the bus. game over."); 226 MMC_UNLOCK(sc); 227 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), dev); 228 if (err) 229 return (err); 230 MMC_LOCK(sc); 231 sc->owner = NULL; 232 MMC_UNLOCK(sc); 233 return (0); 234} 235 236static void 237mmc_rescan_cards(struct mmc_softc *sc) 238{ 239 /* XXX: Look at the children and see if they respond to status */ 240} 241 242static uint32_t 243mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr) 244{ 245 246 return ocr & MMC_OCR_VOLTAGE; 247} 248 249static int 250mmc_highest_voltage(uint32_t ocr) 251{ 252 int i; 253 254 for (i = 30; i >= 0; i--) 255 if (ocr & (1 << i)) 256 return i; 257 return (-1); 258} 259 260static void 261mmc_wakeup(struct mmc_request *req) 262{ 263 struct mmc_softc *sc; 264 265// printf("Wakeup for req %p done_data %p\n", req, req->done_data); 266 sc = (struct mmc_softc *)req->done_data; 267 MMC_LOCK(sc); 268 req->flags |= MMC_REQ_DONE; 269 wakeup(req); 270 MMC_UNLOCK(sc); 271} 272 273static int 274mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req) 275{ 276 int err; 277 278 req->done = mmc_wakeup; 279 req->done_data = sc; 280// printf("Submitting request %p sc %p\n", req, sc); 281 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req); 282 MMC_LOCK(sc); 283 do { 284 err = msleep(req, &sc->sc_mtx, PZERO | PCATCH, "mmcreq", 285 hz / 10); 286 } while (!(req->flags & MMC_REQ_DONE) && err == EAGAIN); 287// printf("Request %p done with error %d\n", req, err); 288 MMC_UNLOCK(sc); 289 return (err); 290} 291 292static int 293mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req) 294{ 295 struct mmc_softc *sc = device_get_softc(brdev); 296 297 return mmc_wait_for_req(sc, req); 298} 299 300static int 301mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries) 302{ 303 struct mmc_request mreq; 304 305 memset(&mreq, 0, sizeof(mreq)); 306 memset(cmd->resp, 0, sizeof(cmd->resp)); 307 cmd->retries = retries; 308 cmd->data = NULL; 309 mreq.cmd = cmd; 310// printf("CMD: %x ARG %x\n", cmd->opcode, cmd->arg); 311 mmc_wait_for_req(sc, &mreq); 312 return (cmd->error); 313} 314 315static int 316mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca, 317 struct mmc_command *cmd, int retries) 318{ 319 struct mmc_command appcmd; 320 int err = MMC_ERR_NONE, i; 321 322 for (i = 0; i <= retries; i++) { 323 appcmd.opcode = MMC_APP_CMD; 324 appcmd.arg = rca << 16; 325 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 326 mmc_wait_for_cmd(sc, &appcmd, 0); 327 err = appcmd.error; 328 if (err != MMC_ERR_NONE) 329 continue; 330 if (!(appcmd.resp[0] & R1_APP_CMD)) 331 return MMC_ERR_FAILED; 332 mmc_wait_for_cmd(sc, cmd, 0); 333 err = cmd->error; 334 if (err == MMC_ERR_NONE) 335 break; 336 } 337 return (err); 338} 339 340static int 341mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode, 342 uint32_t arg, uint32_t flags, uint32_t *resp, int retries) 343{ 344 struct mmc_command cmd; 345 int err; 346 347 memset(&cmd, 0, sizeof(cmd)); 348 cmd.opcode = opcode; 349 cmd.arg = arg; 350 cmd.flags = flags; 351 err = mmc_wait_for_cmd(sc, &cmd, retries); 352 if (err) 353 return (err); 354 if (cmd.error) 355 return (cmd.error); 356 if (resp) { 357 if (flags & MMC_RSP_136) 358 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t)); 359 else 360 *resp = cmd.resp[0]; 361 } 362 return (0); 363} 364 365static void 366mmc_idle_cards(struct mmc_softc *sc) 367{ 368 device_t dev; 369 struct mmc_command cmd; 370 371 dev = sc->dev; 372 mmcbr_set_chip_select(dev, cs_high); 373 mmcbr_update_ios(dev); 374 mmc_ms_delay(1); 375 376 memset(&cmd, 0, sizeof(cmd)); 377 cmd.opcode = MMC_GO_IDLE_STATE; 378 cmd.arg = 0; 379 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC; 380 mmc_wait_for_cmd(sc, &cmd, 0); 381 mmc_ms_delay(1); 382 383 mmcbr_set_chip_select(dev, cs_dontcare); 384 mmcbr_update_ios(dev); 385 mmc_ms_delay(1); 386} 387 388static int 389mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 390{ 391 struct mmc_command cmd; 392 int err = MMC_ERR_NONE, i; 393 394 memset(&cmd, 0, sizeof(cmd)); 395 cmd.opcode = ACMD_SD_SEND_OP_COND; 396 cmd.arg = ocr; 397 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 398 399 for (i = 0; i < 100; i++) { 400 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES); 401 if (err != MMC_ERR_NONE) 402 break; 403 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || ocr == 0) 404 break; 405 err = MMC_ERR_TIMEOUT; 406 mmc_ms_delay(10); 407 } 408 if (rocr && err == MMC_ERR_NONE) 409 *rocr = cmd.resp[0]; 410 return err; 411} 412 413static int 414mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr) 415{ 416 struct mmc_command cmd; 417 int err = MMC_ERR_NONE, i; 418 419 memset(&cmd, 0, sizeof(cmd)); 420 cmd.opcode = MMC_SEND_OP_COND; 421 cmd.arg = ocr; 422 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR; 423 424 for (i = 0; i < 100; i++) { 425 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES); 426 if (err != MMC_ERR_NONE) 427 break; 428 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || ocr == 0) 429 break; 430 err = MMC_ERR_TIMEOUT; 431 mmc_ms_delay(10); 432 } 433 if (rocr && err == MMC_ERR_NONE) 434 *rocr = cmd.resp[0]; 435 return err; 436} 437 438static void 439mmc_power_up(struct mmc_softc *sc) 440{ 441 device_t dev; 442 443 dev = sc->dev; 444 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev))); 445 mmcbr_set_bus_mode(dev, opendrain); 446 mmcbr_set_chip_select(dev, cs_dontcare); 447 mmcbr_set_bus_width(dev, bus_width_1); 448 mmcbr_set_power_mode(dev, power_up); 449 mmcbr_set_clock(dev, 0); 450 mmcbr_update_ios(dev); 451 mmc_ms_delay(1); 452 453 mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev)); 454 mmcbr_set_power_mode(dev, power_on); 455 mmcbr_update_ios(dev); 456 mmc_ms_delay(2); 457} 458 459static void 460mmc_power_down(struct mmc_softc *sc) 461{ 462 device_t dev = sc->dev; 463 464 mmcbr_set_bus_mode(dev, opendrain); 465 mmcbr_set_chip_select(dev, cs_dontcare); 466 mmcbr_set_bus_width(dev, bus_width_1); 467 mmcbr_set_power_mode(dev, power_off); 468 mmcbr_set_clock(dev, 0); 469 mmcbr_update_ios(dev); 470} 471 472static uint32_t 473mmc_get_bits(uint32_t *bits, int start, int size) 474{ 475 const int i = 3 - (start / 32); 476 const int shift = start & 31; 477 uint32_t retval = bits[i] >> shift; 478 if (size + shift > 32) 479 retval |= bits[i - 1] << (32 - shift); 480 return retval & ((1 << size) - 1); 481} 482 483static void 484mmc_decode_cid(int is_sd, uint32_t *raw_cid, struct mmc_cid *cid) 485{ 486 int i; 487 488 memset(cid, 0, sizeof(*cid)); 489 if (is_sd) { 490 /* There's no version info, so we take it on faith */ 491 cid->mid = mmc_get_bits(raw_cid, 120, 8); 492 cid->oid = mmc_get_bits(raw_cid, 104, 16); 493 for (i = 0; i < 5; i++) 494 cid->pnm[i] = mmc_get_bits(raw_cid, 96 - i * 8, 8); 495 cid->prv = mmc_get_bits(raw_cid, 56, 8); 496 cid->psn = mmc_get_bits(raw_cid, 24, 32); 497 cid->mdt_year = mmc_get_bits(raw_cid, 12, 8) + 2001; 498 cid->mdt_month = mmc_get_bits(raw_cid, 8, 4); 499 } else { 500 // XXX write me 501 panic("write mmc cid decoder"); 502 } 503} 504 505static const int exp[8] = { 506 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000 507}; 508static const int mant[16] = { 509 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 510}; 511static const int cur_min[8] = { 512 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000 513}; 514static const int cur_max[8] = { 515 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000 516}; 517 518static void 519mmc_decode_csd(int is_sd, uint32_t *raw_csd, struct mmc_csd *csd) 520{ 521 int v; 522 int m; 523 int e; 524 525 memset(csd, 0, sizeof(*csd)); 526 if (is_sd) { 527 csd->csd_structure = v = mmc_get_bits(raw_csd, 126, 2); 528 if (v == 0) { 529 m = mmc_get_bits(raw_csd, 115, 4); 530 e = mmc_get_bits(raw_csd, 112, 3); 531 csd->tacc = exp[e] * mant[m] + 9 / 10; 532 csd->nsac = mmc_get_bits(raw_csd, 104, 8) * 100; 533 m = mmc_get_bits(raw_csd, 99, 4); 534 e = mmc_get_bits(raw_csd, 96, 3); 535 csd->tran_speed = exp[e] * 10000 * mant[m]; 536 csd->ccc = mmc_get_bits(raw_csd, 84, 12); 537 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 80, 4); 538 csd->read_bl_partial = mmc_get_bits(raw_csd, 79, 1); 539 csd->write_blk_misalign = mmc_get_bits(raw_csd, 78, 1); 540 csd->read_blk_misalign = mmc_get_bits(raw_csd, 77, 1); 541 csd->dsr_imp = mmc_get_bits(raw_csd, 76, 1); 542 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 59, 3)]; 543 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 56, 3)]; 544 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 53, 3)]; 545 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 50, 3)]; 546 m = mmc_get_bits(raw_csd, 62, 12); 547 e = mmc_get_bits(raw_csd, 47, 3); 548 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len; 549 csd->erase_blk_en = mmc_get_bits(raw_csd, 46, 1); 550 csd->sector_size = mmc_get_bits(raw_csd, 39, 7); 551 csd->wp_grp_size = mmc_get_bits(raw_csd, 32, 7); 552 csd->wp_grp_enable = mmc_get_bits(raw_csd, 31, 1); 553 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 26, 3); 554 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 22, 4); 555 csd->write_bl_partial = mmc_get_bits(raw_csd, 21, 1); 556 } else if (v == 1) { 557 panic("Write SDHC CSD parser"); 558 } else 559 panic("unknown SD CSD version"); 560 } else { 561 panic("Write a MMC CSD parser"); 562 } 563} 564 565static int 566mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid) 567{ 568 struct mmc_command cmd; 569 int err; 570 571 cmd.opcode = MMC_ALL_SEND_CID; 572 cmd.arg = 0; 573 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 574 err = mmc_wait_for_cmd(sc, &cmd, 0); 575 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 576 return (err); 577} 578 579static int 580mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcid) 581{ 582 struct mmc_command cmd; 583 int err; 584 585 cmd.opcode = MMC_SEND_CSD; 586 cmd.arg = rca << 16; 587 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR; 588 err = mmc_wait_for_cmd(sc, &cmd, 0); 589 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t)); 590 return (err); 591} 592 593static int 594mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp) 595{ 596 struct mmc_command cmd; 597 int err; 598 599 cmd.opcode = SD_SEND_RELATIVE_ADDR; 600 cmd.arg = 0; 601 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR; 602 err = mmc_wait_for_cmd(sc, &cmd, 0); 603 *resp = cmd.resp[0]; 604 return (err); 605} 606 607static void 608mmc_discover_cards(struct mmc_softc *sc) 609{ 610 struct mmc_ivars *ivar; 611 int err; 612 uint32_t resp; 613 device_t child; 614 615 while (1) { 616 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, M_WAITOK); 617 if (!ivar) 618 return; 619 err = mmc_all_send_cid(sc, ivar->raw_cid); 620 if (err == MMC_ERR_TIMEOUT) 621 break; 622 if (err != MMC_ERR_NONE) { 623 printf("Error reading CID %d\n", err); 624 break; 625 } 626 if (mmcbr_get_mode(sc->dev) == mode_sd) { 627 ivar->mode = mode_sd; 628 mmc_decode_cid(1, ivar->raw_cid, &ivar->cid); 629 mmc_send_relative_addr(sc, &resp); 630 ivar->rca = resp >> 16; 631 if (mmcbr_get_ro(sc->dev)) 632 ivar->read_only = 1; 633 mmc_send_csd(sc, ivar->rca, ivar->raw_csd); 634 mmc_decode_csd(1, ivar->raw_csd, &ivar->csd); 635 printf("SD CARD: %lld bytes\n", (long long) 636 ivar->csd.capacity); 637 child = device_add_child(sc->dev, NULL, -1); 638 device_set_ivars(child, ivar); 639 return; 640 } 641 panic("Write MMC card code here"); 642 } 643 free(ivar, M_DEVBUF); 644} 645 646static void 647mmc_go_discovery(struct mmc_softc *sc) 648{ 649 uint32_t ocr; 650 device_t dev; 651 652 dev = sc->dev; 653 if (mmcbr_get_power_mode(dev) != power_on) { 654 // First, try SD modes 655 mmcbr_set_mode(dev, mode_sd); 656 mmc_power_up(sc); 657 mmcbr_set_bus_mode(dev, pushpull); 658 mmc_idle_cards(sc); 659 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) { 660 // Failed, try MMC 661 mmcbr_set_mode(dev, mode_mmc); 662 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) 663 return; // Failed both, punt! XXX power down? 664 } 665 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr)); 666 if (mmcbr_get_ocr(dev) != 0) 667 mmc_idle_cards(sc); 668 } else { 669 mmcbr_set_bus_mode(dev, opendrain); 670 mmcbr_set_clock(dev, mmcbr_get_f_min(dev)); 671 mmcbr_update_ios(dev); 672 // XXX recompute vdd based on new cards? 673 } 674 /* 675 * Make sure that we have a mutually agreeable voltage to at least 676 * one card on the bus. 677 */ 678 if (mmcbr_get_ocr(dev) == 0) 679 return; 680 /* 681 * Reselect the cards after we've idled them above. 682 */ 683 if (mmcbr_get_mode(dev) == mode_sd) 684 mmc_send_app_op_cond(sc, mmcbr_get_ocr(dev), NULL); 685 else 686 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL); 687 mmc_discover_cards(sc); 688 689 mmcbr_set_bus_mode(dev, pushpull); 690 mmcbr_update_ios(dev); 691 bus_generic_attach(dev); 692// mmc_update_children_sysctl(dev); 693} 694 695static int 696mmc_calculate_clock(struct mmc_softc *sc) 697{ 698 int max_dtr = 0; 699 int nkid, i, f_min, f_max; 700 device_t *kids; 701 702 f_min = mmcbr_get_f_min(sc->dev); 703 f_max = mmcbr_get_f_max(sc->dev); 704 max_dtr = f_max; 705 if (device_get_children(sc->dev, &kids, &nkid) != 0) 706 panic("can't get children"); 707 for (i = 0; i < nkid; i++) 708 if (mmc_get_tran_speed(kids[i]) < max_dtr) 709 max_dtr = mmc_get_tran_speed(kids[i]); 710 free(kids, M_TEMP); 711 device_printf(sc->dev, "setting transfer rate to %d.%03dMHz\n", 712 max_dtr / 1000000, (max_dtr / 1000) % 1000); 713 return max_dtr; 714} 715 716static void 717mmc_scan(struct mmc_softc *sc) 718{ 719 device_t dev; 720 721 dev = sc->dev; 722 mmc_acquire_bus(dev, dev); 723 724 if (mmcbr_get_power_mode(dev) == power_on) 725 mmc_rescan_cards(sc); 726 mmc_go_discovery(sc); 727 mmcbr_set_clock(dev, mmc_calculate_clock(sc)); 728 mmcbr_update_ios(dev); 729 730 mmc_release_bus(dev, dev); 731 // XXX probe/attach/detach children? 732} 733 734static int 735mmc_read_ivar(device_t bus, device_t child, int which, u_char *result) 736{ 737 struct mmc_ivars *ivar = device_get_ivars(child); 738 739 switch (which) { 740 default: 741 return (EINVAL); 742 case MMC_IVAR_DSR_IMP: 743 *(int *)result = ivar->csd.dsr_imp; 744 break; 745 case MMC_IVAR_MEDIA_SIZE: 746 *(int *)result = ivar->csd.capacity; 747 break; 748 case MMC_IVAR_RCA: 749 *(int *)result = ivar->rca; 750 break; 751 case MMC_IVAR_SECTOR_SIZE: 752 *(int *)result = 512; 753 break; 754 case MMC_IVAR_TRAN_SPEED: 755 *(int *)result = ivar->csd.tran_speed; 756 break; 757 case MMC_IVAR_READ_ONLY: 758 *(int *)result = ivar->read_only; 759 break; 760 } 761 return (0); 762} 763 764static int 765mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 766{ 767 // None are writable ATM 768 switch (which) { 769 default: 770 return (EINVAL); 771 } 772 return (0); 773} 774 775 776static void 777mmc_delayed_attach(void *xsc) 778{ 779 struct mmc_softc *sc = xsc; 780 781 mmc_scan(sc); 782 config_intrhook_disestablish(&sc->config_intrhook); 783} 784 785static device_method_t mmc_methods[] = { 786 /* device_if */ 787 DEVMETHOD(device_probe, mmc_probe), 788 DEVMETHOD(device_attach, mmc_attach), 789 DEVMETHOD(device_detach, mmc_detach), 790 791 /* Bus interface */ 792 DEVMETHOD(bus_read_ivar, mmc_read_ivar), 793 DEVMETHOD(bus_write_ivar, mmc_write_ivar), 794 795 /* MMC Bus interface */ 796 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request), 797 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus), 798 DEVMETHOD(mmcbus_release_bus, mmc_release_bus), 799 800 {0, 0}, 801}; 802 803static driver_t mmc_driver = { 804 "mmc", 805 mmc_methods, 806 sizeof(struct mmc_softc), 807}; 808static devclass_t mmc_devclass; 809 810 811DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, 0, 0); 812DRIVER_MODULE(mmc, sdh, mmc_driver, mmc_devclass, 0, 0); 813