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