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