2 * Copyright (c) 2006 Bernd Walter. All rights reserved.
3 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
25 * Portions of this software may have been developed with reference to
26 * the SD Simplified Specification. The following disclaimer may apply:
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.
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.
52 * $FreeBSD: src/sys/dev/mmc/mmc.c,v 1.38 2009/08/20 19:17:53 jhb Exp $
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
60 #include <sys/module.h>
61 #include <sys/spinlock.h>
63 #include <sys/endian.h>
64 #include <sys/sysctl.h>
66 #include <bus/mmc/mmcreg.h>
67 #include <bus/mmc/mmcbrvar.h>
68 #include <bus/mmc/mmcvar.h>
70 #include "mmcbus_if.h"
75 struct intr_config_hook config_intrhook;
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 uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */
90 enum mmc_card_mode mode;
91 struct mmc_cid cid; /* cid decoded */
92 struct mmc_csd csd; /* csd decoded */
93 struct mmc_scr scr; /* scr decoded */
94 struct mmc_sd_status sd_status; /* SD_STATUS decoded */
95 u_char read_only; /* True when the device is read-only */
96 u_char bus_width; /* Bus width to use */
97 u_char timing; /* Bus timing support */
98 u_char high_cap; /* High Capacity card (block addressed) */
99 uint32_t sec_count; /* Card capacity in 512byte blocks */
100 uint32_t tran_speed; /* Max speed in normal mode */
101 uint32_t hs_tran_speed; /* Max speed in high speed mode */
102 uint32_t erase_sector; /* Card native erase sector size */
105 #define CMD_RETRIES 3
107 SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
109 static int mmc_debug;
110 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");
112 /* bus entry points */
113 static int mmc_probe(device_t dev);
114 static int mmc_attach(device_t dev);
115 static int mmc_detach(device_t dev);
116 static int mmc_suspend(device_t dev);
117 static int mmc_resume(device_t dev);
119 #define MMC_LOCK(_sc) lockmgr(&(_sc)->sc_lock, LK_EXCLUSIVE)
120 #define MMC_UNLOCK(_sc) lockmgr(&(_sc)->sc_lock, LK_RELEASE)
121 #define MMC_LOCK_INIT(_sc) lockinit(&(_sc)->sc_lock, "mmc", 0, LK_CANRECURSE)
122 #define MMC_LOCK_DESTROY(_sc) lockuninit(&(_sc)->sc_lock);
123 #define MMC_ASSERT_LOCKED(_sc) KKASSERT(lockstatus(&(_sc)->sc_lock, curthread) != 0);
124 #define MMC_ASSERT_UNLOCKED(_sc) KKASSERT(lockstatus(&(_sc)->sc_lock, curthread) == 0);
126 static int mmc_calculate_clock(struct mmc_softc *sc);
127 static void mmc_delayed_attach(void *);
128 static void mmc_power_down(struct mmc_softc *sc);
129 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
131 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
132 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
133 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
134 static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width);
135 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr);
136 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
137 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
138 static void mmc_scan(struct mmc_softc *sc);
139 static int mmc_delete_cards(struct mmc_softc *sc);
144 DELAY(1000 * ms); /* XXX BAD */
148 mmc_probe(device_t dev)
151 device_set_desc(dev, "MMC/SD bus");
156 mmc_attach(device_t dev)
158 struct mmc_softc *sc;
160 sc = device_get_softc(dev);
164 /* We'll probe and attach our children later, but before / mount */
165 sc->config_intrhook.ich_func = mmc_delayed_attach;
166 sc->config_intrhook.ich_arg = sc;
167 if (config_intrhook_establish(&sc->config_intrhook) != 0)
168 device_printf(dev, "config_intrhook_establish failed\n");
173 mmc_detach(device_t dev)
175 struct mmc_softc *sc = device_get_softc(dev);
178 if ((err = mmc_delete_cards(sc)) != 0)
181 MMC_LOCK_DESTROY(sc);
187 mmc_suspend(device_t dev)
189 struct mmc_softc *sc = device_get_softc(dev);
192 err = bus_generic_suspend(dev);
200 mmc_resume(device_t dev)
202 struct mmc_softc *sc = device_get_softc(dev);
205 return (bus_generic_resume(dev));
209 mmc_acquire_bus(device_t busdev, device_t dev)
211 struct mmc_softc *sc;
212 struct mmc_ivars *ivar;
216 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
219 sc = device_get_softc(busdev);
222 panic("mmc: host bridge didn't seralize us.");
228 * Keep track of the last rca that we've selected. If
229 * we're asked to do it again, don't. We never
230 * unselect unless the bus code itself wants the mmc
231 * bus, and constantly reselecting causes problems.
233 rca = mmc_get_rca(dev);
234 if (sc->last_rca != rca) {
235 mmc_select_card(sc, rca);
237 /* Prepare bus width for the new card. */
238 ivar = device_get_ivars(dev);
239 if (bootverbose || mmc_debug) {
240 device_printf(busdev,
241 "setting bus width to %d bits\n",
242 (ivar->bus_width == bus_width_4) ? 4 :
243 (ivar->bus_width == bus_width_8) ? 8 : 1);
245 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
246 mmcbr_set_bus_width(busdev, ivar->bus_width);
247 mmcbr_update_ios(busdev);
251 * If there's a card selected, stand down.
253 if (sc->last_rca != 0) {
254 mmc_select_card(sc, 0);
263 mmc_release_bus(device_t busdev, device_t dev)
265 struct mmc_softc *sc;
268 sc = device_get_softc(busdev);
272 panic("mmc: releasing unowned bus.");
273 if (sc->owner != dev)
274 panic("mmc: you don't own the bus. game over.");
276 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
286 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
289 return (ocr & MMC_OCR_VOLTAGE);
293 mmc_highest_voltage(uint32_t ocr)
297 for (i = 30; i >= 0; i--)
304 mmc_wakeup(struct mmc_request *req)
306 struct mmc_softc *sc;
308 sc = (struct mmc_softc *)req->done_data;
310 req->flags |= MMC_REQ_DONE;
316 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
319 req->done = mmc_wakeup;
322 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
323 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
324 if (req->cmd->data) {
325 kprintf(" data %d\n", (int)req->cmd->data->len);
329 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
331 while ((req->flags & MMC_REQ_DONE) == 0)
332 lksleep(req, &sc->sc_lock, 0, "mmcreq", 0);
334 if (mmc_debug > 2 || (mmc_debug > 1 && req->cmd->error))
335 device_printf(sc->dev, "RESULT: %d\n", req->cmd->error);
340 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
342 struct mmc_softc *sc = device_get_softc(brdev);
344 return (mmc_wait_for_req(sc, req));
348 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
350 struct mmc_request mreq;
352 memset(&mreq, 0, sizeof(mreq));
353 memset(cmd->resp, 0, sizeof(cmd->resp));
354 cmd->retries = retries;
356 mmc_wait_for_req(sc, &mreq);
361 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
362 struct mmc_command *cmd, int retries)
364 struct mmc_command appcmd;
365 int err = MMC_ERR_NONE, i;
367 for (i = 0; i <= retries; i++) {
368 appcmd.opcode = MMC_APP_CMD;
369 appcmd.arg = rca << 16;
370 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
372 mmc_wait_for_cmd(sc, &appcmd, 0);
374 if (err != MMC_ERR_NONE)
376 if (!(appcmd.resp[0] & R1_APP_CMD))
377 return MMC_ERR_FAILED;
378 mmc_wait_for_cmd(sc, cmd, 0);
380 if (err == MMC_ERR_NONE)
387 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
388 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
390 struct mmc_command cmd;
393 memset(&cmd, 0, sizeof(cmd));
398 err = mmc_wait_for_cmd(sc, &cmd, retries);
404 if (flags & MMC_RSP_136)
405 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
413 mmc_idle_cards(struct mmc_softc *sc)
416 struct mmc_command cmd;
419 mmcbr_set_chip_select(dev, cs_high);
420 mmcbr_update_ios(dev);
423 memset(&cmd, 0, sizeof(cmd));
424 cmd.opcode = MMC_GO_IDLE_STATE;
426 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
428 mmc_wait_for_cmd(sc, &cmd, 0);
431 mmcbr_set_chip_select(dev, cs_dontcare);
432 mmcbr_update_ios(dev);
437 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
439 struct mmc_command cmd;
440 int err = MMC_ERR_NONE, i;
442 memset(&cmd, 0, sizeof(cmd));
443 cmd.opcode = ACMD_SD_SEND_OP_COND;
445 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
448 for (i = 0; i < 100; i++) {
449 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
450 if (err != MMC_ERR_NONE)
452 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
453 (ocr & MMC_OCR_VOLTAGE) == 0)
455 err = MMC_ERR_TIMEOUT;
458 if (rocr && err == MMC_ERR_NONE)
464 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
466 struct mmc_command cmd;
467 int err = MMC_ERR_NONE, i;
469 memset(&cmd, 0, sizeof(cmd));
470 cmd.opcode = MMC_SEND_OP_COND;
472 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
475 for (i = 0; i < 100; i++) {
476 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
477 if (err != MMC_ERR_NONE)
479 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
480 (ocr & MMC_OCR_VOLTAGE) == 0)
482 err = MMC_ERR_TIMEOUT;
485 if (rocr && err == MMC_ERR_NONE)
491 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
493 struct mmc_command cmd;
496 memset(&cmd, 0, sizeof(cmd));
497 cmd.opcode = SD_SEND_IF_COND;
498 cmd.arg = (vhs << 8) + 0xAA;
499 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
502 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
507 mmc_power_up(struct mmc_softc *sc)
512 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
513 mmcbr_set_bus_mode(dev, opendrain);
514 mmcbr_set_chip_select(dev, cs_dontcare);
515 mmcbr_set_bus_width(dev, bus_width_1);
516 mmcbr_set_power_mode(dev, power_up);
517 mmcbr_set_clock(dev, 0);
518 mmcbr_update_ios(dev);
521 mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev));
522 mmcbr_set_timing(dev, bus_timing_normal);
523 mmcbr_set_power_mode(dev, power_on);
524 mmcbr_update_ios(dev);
529 mmc_power_down(struct mmc_softc *sc)
531 device_t dev = sc->dev;
533 mmcbr_set_bus_mode(dev, opendrain);
534 mmcbr_set_chip_select(dev, cs_dontcare);
535 mmcbr_set_bus_width(dev, bus_width_1);
536 mmcbr_set_power_mode(dev, power_off);
537 mmcbr_set_clock(dev, 0);
538 mmcbr_set_timing(dev, bus_timing_normal);
539 mmcbr_update_ios(dev);
543 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
547 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
548 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
549 flags, NULL, CMD_RETRIES));
553 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
555 struct mmc_command cmd;
558 cmd.opcode = MMC_SWITCH_FUNC;
559 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
563 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
565 err = mmc_wait_for_cmd(sc, &cmd, 0);
570 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
574 struct mmc_command cmd;
575 struct mmc_data data;
577 memset(&cmd, 0, sizeof(struct mmc_command));
578 memset(&data, 0, sizeof(struct mmc_data));
581 cmd.opcode = SD_SWITCH_FUNC;
582 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
583 cmd.arg = mode << 31; /* 0 - check, 1 - set */
584 cmd.arg |= 0x00FFFFFF;
585 cmd.arg &= ~(0xF << (grp * 4));
586 cmd.arg |= value << (grp * 4);
591 data.flags = MMC_DATA_READ;
593 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
598 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
600 struct mmc_command cmd;
604 if (mmcbr_get_mode(sc->dev) == mode_sd) {
605 memset(&cmd, 0, sizeof(struct mmc_command));
606 cmd.opcode = ACMD_SET_BUS_WIDTH;
607 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
610 cmd.arg = SD_BUS_WIDTH_1;
613 cmd.arg = SD_BUS_WIDTH_4;
616 return (MMC_ERR_INVALID);
618 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
622 value = EXT_CSD_BUS_WIDTH_1;
625 value = EXT_CSD_BUS_WIDTH_4;
628 value = EXT_CSD_BUS_WIDTH_8;
631 return (MMC_ERR_INVALID);
633 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
640 mmc_set_timing(struct mmc_softc *sc, int timing)
644 u_char switch_res[64];
647 case bus_timing_normal:
654 return (MMC_ERR_INVALID);
656 if (mmcbr_get_mode(sc->dev) == mode_sd)
657 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
660 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
661 EXT_CSD_HS_TIMING, value);
666 mmc_test_bus_width(struct mmc_softc *sc)
668 struct mmc_command cmd;
669 struct mmc_data data;
672 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
673 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
674 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, };
675 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
677 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
678 mmcbr_set_bus_width(sc->dev, bus_width_8);
679 mmcbr_update_ios(sc->dev);
681 cmd.opcode = MMC_BUSTEST_W;
683 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
688 data.flags = MMC_DATA_WRITE;
689 mmc_wait_for_cmd(sc, &cmd, 0);
691 cmd.opcode = MMC_BUSTEST_R;
693 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
698 data.flags = MMC_DATA_READ;
699 err = mmc_wait_for_cmd(sc, &cmd, 0);
701 mmcbr_set_bus_width(sc->dev, bus_width_1);
702 mmcbr_update_ios(sc->dev);
704 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
705 return (bus_width_8);
708 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
709 mmcbr_set_bus_width(sc->dev, bus_width_4);
710 mmcbr_update_ios(sc->dev);
712 cmd.opcode = MMC_BUSTEST_W;
714 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
719 data.flags = MMC_DATA_WRITE;
720 mmc_wait_for_cmd(sc, &cmd, 0);
722 cmd.opcode = MMC_BUSTEST_R;
724 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
729 data.flags = MMC_DATA_READ;
730 err = mmc_wait_for_cmd(sc, &cmd, 0);
732 mmcbr_set_bus_width(sc->dev, bus_width_1);
733 mmcbr_update_ios(sc->dev);
735 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
736 return (bus_width_4);
738 return (bus_width_1);
742 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
744 const int i = (bit_len / 32) - (start / 32) - 1;
745 const int shift = start & 31;
746 uint32_t retval = bits[i] >> shift;
747 if (size + shift > 32)
748 retval |= bits[i - 1] << (32 - shift);
749 return (retval & ((1 << size) - 1));
753 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
757 /* There's no version info, so we take it on faith */
758 memset(cid, 0, sizeof(*cid));
759 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
760 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
761 for (i = 0; i < 5; i++)
762 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
764 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
765 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
766 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
767 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
771 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
775 /* There's no version info, so we take it on faith */
776 memset(cid, 0, sizeof(*cid));
777 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
778 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
779 for (i = 0; i < 6; i++)
780 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
782 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
783 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
784 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
785 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
788 static const int exp[8] = {
789 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
791 static const int mant[16] = {
792 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
794 static const int cur_min[8] = {
795 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
797 static const int cur_max[8] = {
798 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
802 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
808 memset(csd, 0, sizeof(*csd));
809 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
811 m = mmc_get_bits(raw_csd, 128, 115, 4);
812 e = mmc_get_bits(raw_csd, 128, 112, 3);
813 csd->tacc = exp[e] * mant[m] + 9 / 10;
814 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
815 m = mmc_get_bits(raw_csd, 128, 99, 4);
816 e = mmc_get_bits(raw_csd, 128, 96, 3);
817 csd->tran_speed = exp[e] * 10000 * mant[m];
818 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
819 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
820 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
821 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
822 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
823 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
824 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
825 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
826 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
827 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
828 m = mmc_get_bits(raw_csd, 128, 62, 12);
829 e = mmc_get_bits(raw_csd, 128, 47, 3);
830 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
831 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
832 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
833 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
834 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
835 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
836 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
837 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
839 m = mmc_get_bits(raw_csd, 128, 115, 4);
840 e = mmc_get_bits(raw_csd, 128, 112, 3);
841 csd->tacc = exp[e] * mant[m] + 9 / 10;
842 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
843 m = mmc_get_bits(raw_csd, 128, 99, 4);
844 e = mmc_get_bits(raw_csd, 128, 96, 3);
845 csd->tran_speed = exp[e] * 10000 * mant[m];
846 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
847 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
848 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
849 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
850 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
851 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
852 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
854 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
855 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
856 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
857 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
858 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
859 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
860 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
862 panic("unknown SD CSD version");
866 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
871 memset(csd, 0, sizeof(*csd));
872 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
873 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
874 m = mmc_get_bits(raw_csd, 128, 115, 4);
875 e = mmc_get_bits(raw_csd, 128, 112, 3);
876 csd->tacc = exp[e] * mant[m] + 9 / 10;
877 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
878 m = mmc_get_bits(raw_csd, 128, 99, 4);
879 e = mmc_get_bits(raw_csd, 128, 96, 3);
880 csd->tran_speed = exp[e] * 10000 * mant[m];
881 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
882 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
883 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
884 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
885 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
886 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
887 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
888 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
889 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
890 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
891 m = mmc_get_bits(raw_csd, 128, 62, 12);
892 e = mmc_get_bits(raw_csd, 128, 47, 3);
893 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
894 csd->erase_blk_en = 0;
895 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
896 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
897 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
898 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
899 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
900 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
901 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
905 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
907 unsigned int scr_struct;
909 memset(scr, 0, sizeof(*scr));
911 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
912 if (scr_struct != 0) {
913 kprintf("Unrecognised SCR structure version %d\n",
917 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
918 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
922 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
923 struct mmc_sd_status *sd_status)
926 memset(sd_status, 0, sizeof(*sd_status));
928 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
929 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
930 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
931 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
932 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
933 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
934 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
935 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
936 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
937 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
941 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
943 struct mmc_command cmd;
946 cmd.opcode = MMC_ALL_SEND_CID;
948 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
950 err = mmc_wait_for_cmd(sc, &cmd, 0);
951 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
956 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcid)
958 struct mmc_command cmd;
961 cmd.opcode = MMC_SEND_CSD;
963 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
965 err = mmc_wait_for_cmd(sc, &cmd, 0);
966 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
971 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
974 struct mmc_command cmd;
975 struct mmc_data data;
977 memset(&cmd, 0, sizeof(struct mmc_command));
978 memset(&data, 0, sizeof(struct mmc_data));
980 memset(rawscr, 0, 8);
981 cmd.opcode = ACMD_SEND_SCR;
982 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
988 data.flags = MMC_DATA_READ;
990 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
991 rawscr[0] = be32toh(rawscr[0]);
992 rawscr[1] = be32toh(rawscr[1]);
997 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1000 struct mmc_command cmd;
1001 struct mmc_data data;
1003 memset(&cmd, 0, sizeof(struct mmc_command));
1004 memset(&data, 0, sizeof(struct mmc_data));
1006 memset(rawextcsd, 0, 512);
1007 cmd.opcode = MMC_SEND_EXT_CSD;
1008 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1012 data.data = rawextcsd;
1014 data.flags = MMC_DATA_READ;
1016 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1021 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1024 struct mmc_command cmd;
1025 struct mmc_data data;
1027 memset(&cmd, 0, sizeof(struct mmc_command));
1028 memset(&data, 0, sizeof(struct mmc_data));
1030 memset(rawsdstatus, 0, 64);
1031 cmd.opcode = ACMD_SD_STATUS;
1032 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1036 data.data = rawsdstatus;
1038 data.flags = MMC_DATA_READ;
1040 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1041 for (i = 0; i < 16; i++)
1042 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1047 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1049 struct mmc_command cmd;
1052 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1053 cmd.arg = resp << 16;
1054 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1056 err = mmc_wait_for_cmd(sc, &cmd, 0);
1061 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1063 struct mmc_command cmd;
1066 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1068 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1070 err = mmc_wait_for_cmd(sc, &cmd, 0);
1071 *resp = cmd.resp[0];
1076 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1078 device_printf(dev, "Card at relative address %d%s:\n",
1079 ivar->rca, newcard ? " added" : "");
1080 device_printf(dev, " card: %s%s (0x%x/0x%x/\"%s\" rev %d.%d "
1081 "m/d %02d.%04d s/n %08x)\n",
1082 ivar->mode == mode_sd ? "SD" : "MMC",
1083 ivar->high_cap ? " High Capacity" : "",
1084 ivar->cid.mid, ivar->cid.oid,
1085 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
1086 ivar->cid.mdt_month, ivar->cid.mdt_year, ivar->cid.psn);
1087 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1088 (ivar->bus_width == bus_width_1 ? 1 :
1089 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1090 (ivar->timing == bus_timing_hs ?
1091 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1092 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1093 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1094 ivar->sec_count, ivar->erase_sector,
1095 ivar->read_only ? ", read-only" : "");
1099 mmc_discover_cards(struct mmc_softc *sc)
1101 struct mmc_ivars *ivar = NULL;
1103 int err, i, devcount, newcard;
1104 uint32_t raw_cid[4];
1105 uint32_t resp, sec_count;
1108 u_char switch_res[64];
1110 if (bootverbose || mmc_debug)
1111 device_printf(sc->dev, "Probing cards\n");
1113 err = mmc_all_send_cid(sc, raw_cid);
1114 if (err == MMC_ERR_TIMEOUT)
1116 if (err != MMC_ERR_NONE) {
1117 device_printf(sc->dev, "Error reading CID %d\n", err);
1121 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1123 for (i = 0; i < devcount; i++) {
1124 ivar = device_get_ivars(devlist[i]);
1125 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1130 kfree(devlist, M_TEMP);
1131 if (bootverbose || mmc_debug) {
1132 device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1133 newcard ? "New c" : "C",
1134 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1137 ivar = kmalloc(sizeof(struct mmc_ivars), M_DEVBUF,
1139 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1141 if (mmcbr_get_ro(sc->dev))
1142 ivar->read_only = 1;
1143 ivar->bus_width = bus_width_1;
1144 ivar->timing = bus_timing_normal;
1145 ivar->mode = mmcbr_get_mode(sc->dev);
1146 if (ivar->mode == mode_sd) {
1147 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1148 mmc_send_relative_addr(sc, &resp);
1149 ivar->rca = resp >> 16;
1151 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1152 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1153 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1154 if (ivar->csd.csd_structure > 0)
1156 ivar->tran_speed = ivar->csd.tran_speed;
1157 ivar->erase_sector = ivar->csd.erase_sector *
1158 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1159 /* Get card SCR. Card must be selected to fetch it. */
1160 mmc_select_card(sc, ivar->rca);
1161 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1162 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1163 /* Get card switch capabilities (command class 10). */
1164 if ((ivar->scr.sda_vsn >= 1) &&
1165 (ivar->csd.ccc & (1<<10))) {
1166 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1167 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1169 if (switch_res[13] & 2) {
1170 ivar->timing = bus_timing_hs;
1171 ivar->hs_tran_speed = SD_MAX_HS;
1174 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1175 mmc_app_decode_sd_status(ivar->raw_sd_status,
1177 if (ivar->sd_status.au_size != 0) {
1178 ivar->erase_sector =
1179 16 << ivar->sd_status.au_size;
1181 mmc_select_card(sc, 0);
1182 /* Find max supported bus width. */
1183 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1184 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1185 ivar->bus_width = bus_width_4;
1186 if (bootverbose || mmc_debug)
1187 mmc_log_card(sc->dev, ivar, newcard);
1190 child = device_add_child(sc->dev, NULL, -1);
1191 device_set_ivars(child, ivar);
1195 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1197 mmc_set_relative_addr(sc, ivar->rca);
1199 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1200 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1201 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1202 ivar->tran_speed = ivar->csd.tran_speed;
1203 ivar->erase_sector = ivar->csd.erase_sector *
1204 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1205 /* Only MMC >= 4.x cards support EXT_CSD. */
1206 if (ivar->csd.spec_vers >= 4) {
1207 /* Card must be selected to fetch EXT_CSD. */
1208 mmc_select_card(sc, ivar->rca);
1209 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1210 /* Handle extended capacity from EXT_CSD */
1211 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1212 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1213 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1214 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1215 if (sec_count != 0) {
1216 ivar->sec_count = sec_count;
1219 /* Get card speed in high speed mode. */
1220 ivar->timing = bus_timing_hs;
1221 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1222 & EXT_CSD_CARD_TYPE_52)
1223 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1224 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1225 & EXT_CSD_CARD_TYPE_26)
1226 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1228 ivar->hs_tran_speed = ivar->tran_speed;
1229 /* Find max supported bus width. */
1230 ivar->bus_width = mmc_test_bus_width(sc);
1231 mmc_select_card(sc, 0);
1232 /* Handle HC erase sector size. */
1233 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1234 ivar->erase_sector = 1024 *
1235 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1236 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1237 EXT_CSD_ERASE_GRP_DEF, 1);
1240 ivar->bus_width = bus_width_1;
1241 ivar->timing = bus_timing_normal;
1243 if (bootverbose || mmc_debug)
1244 mmc_log_card(sc->dev, ivar, newcard);
1247 child = device_add_child(sc->dev, NULL, -1);
1248 device_set_ivars(child, ivar);
1254 mmc_rescan_cards(struct mmc_softc *sc)
1256 struct mmc_ivars *ivar = NULL;
1258 int err, i, devcount;
1260 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1262 for (i = 0; i < devcount; i++) {
1263 ivar = device_get_ivars(devlist[i]);
1264 if (mmc_select_card(sc, ivar->rca)) {
1265 if (bootverbose || mmc_debug)
1266 device_printf(sc->dev, "Card at relative address %d lost.\n",
1268 device_delete_child(sc->dev, devlist[i]);
1269 kfree(ivar, M_DEVBUF);
1272 kfree(devlist, M_TEMP);
1273 mmc_select_card(sc, 0);
1277 mmc_delete_cards(struct mmc_softc *sc)
1279 struct mmc_ivars *ivar;
1281 int err, i, devcount;
1283 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1285 for (i = 0; i < devcount; i++) {
1286 ivar = device_get_ivars(devlist[i]);
1287 if (bootverbose || mmc_debug)
1288 device_printf(sc->dev, "Card at relative address %d deleted.\n",
1290 device_delete_child(sc->dev, devlist[i]);
1291 kfree(ivar, M_DEVBUF);
1293 kfree(devlist, M_TEMP);
1298 mmc_go_discovery(struct mmc_softc *sc)
1305 if (mmcbr_get_power_mode(dev) != power_on) {
1307 * First, try SD modes
1309 mmcbr_set_mode(dev, mode_sd);
1311 mmcbr_set_bus_mode(dev, pushpull);
1312 if (bootverbose || mmc_debug)
1313 device_printf(sc->dev, "Probing bus\n");
1315 err = mmc_send_if_cond(sc, 1);
1316 if ((bootverbose || mmc_debug) && err == 0)
1317 device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1318 if (mmc_send_app_op_cond(sc, err ? 0 : MMC_OCR_CCS, &ocr) !=
1320 if (bootverbose || mmc_debug)
1321 device_printf(sc->dev, "SD probe: failed\n");
1325 mmcbr_set_mode(dev, mode_mmc);
1326 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1327 if (bootverbose || mmc_debug)
1328 device_printf(sc->dev, "MMC probe: failed\n");
1329 ocr = 0; /* Failed both, powerdown. */
1330 } else if (bootverbose || mmc_debug)
1331 device_printf(sc->dev,
1332 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1333 } else if (bootverbose || mmc_debug)
1334 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1336 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1337 if (mmcbr_get_ocr(dev) != 0)
1340 mmcbr_set_bus_mode(dev, opendrain);
1341 mmcbr_set_clock(dev, mmcbr_get_f_min(dev));
1342 mmcbr_update_ios(dev);
1343 /* XXX recompute vdd based on new cards? */
1346 * Make sure that we have a mutually agreeable voltage to at least
1347 * one card on the bus.
1349 if (bootverbose || mmc_debug)
1350 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1351 if (mmcbr_get_ocr(dev) == 0) {
1352 mmc_delete_cards(sc);
1357 * Reselect the cards after we've idled them above.
1359 if (mmcbr_get_mode(dev) == mode_sd) {
1360 err = mmc_send_if_cond(sc, 1);
1361 mmc_send_app_op_cond(sc,
1362 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1364 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
1365 mmc_discover_cards(sc);
1366 mmc_rescan_cards(sc);
1368 mmcbr_set_bus_mode(dev, pushpull);
1369 mmcbr_update_ios(dev);
1370 mmc_calculate_clock(sc);
1371 bus_generic_attach(dev);
1372 /* mmc_update_children_sysctl(dev);*/
1376 mmc_calculate_clock(struct mmc_softc *sc)
1378 int max_dtr, max_hs_dtr, max_timing;
1379 int nkid, i, f_min, f_max;
1381 struct mmc_ivars *ivar;
1383 f_min = mmcbr_get_f_min(sc->dev);
1384 f_max = mmcbr_get_f_max(sc->dev);
1385 max_dtr = max_hs_dtr = f_max;
1386 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1387 max_timing = bus_timing_hs;
1389 max_timing = bus_timing_normal;
1390 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1391 panic("can't get children");
1392 for (i = 0; i < nkid; i++) {
1393 ivar = device_get_ivars(kids[i]);
1394 if (ivar->timing < max_timing)
1395 max_timing = ivar->timing;
1396 if (ivar->tran_speed < max_dtr)
1397 max_dtr = ivar->tran_speed;
1398 if (ivar->hs_tran_speed < max_hs_dtr)
1399 max_hs_dtr = ivar->hs_tran_speed;
1401 for (i = 0; i < nkid; i++) {
1402 ivar = device_get_ivars(kids[i]);
1403 if (ivar->timing == bus_timing_normal)
1405 mmc_select_card(sc, ivar->rca);
1406 mmc_set_timing(sc, max_timing);
1408 mmc_select_card(sc, 0);
1409 kfree(kids, M_TEMP);
1410 if (max_timing == bus_timing_hs)
1411 max_dtr = max_hs_dtr;
1412 if (bootverbose || mmc_debug) {
1413 device_printf(sc->dev,
1414 "setting transfer rate to %d.%03dMHz%s\n",
1415 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1416 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1418 mmcbr_set_timing(sc->dev, max_timing);
1419 mmcbr_set_clock(sc->dev, max_dtr);
1420 mmcbr_update_ios(sc->dev);
1425 mmc_scan(struct mmc_softc *sc)
1427 device_t dev = sc->dev;
1429 mmc_acquire_bus(dev, dev);
1430 mmc_go_discovery(sc);
1431 mmc_release_bus(dev, dev);
1435 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1437 struct mmc_ivars *ivar = device_get_ivars(child);
1442 case MMC_IVAR_DSR_IMP:
1443 *(int *)result = ivar->csd.dsr_imp;
1445 case MMC_IVAR_MEDIA_SIZE:
1446 *(off_t *)result = ivar->sec_count;
1449 *(int *)result = ivar->rca;
1451 case MMC_IVAR_SECTOR_SIZE:
1452 *(int *)result = MMC_SECTOR_SIZE;
1454 case MMC_IVAR_TRAN_SPEED:
1455 *(int *)result = mmcbr_get_clock(bus);
1457 case MMC_IVAR_READ_ONLY:
1458 *(int *)result = ivar->read_only;
1460 case MMC_IVAR_HIGH_CAP:
1461 *(int *)result = ivar->high_cap;
1463 case MMC_IVAR_CARD_TYPE:
1464 *(int *)result = ivar->mode;
1466 case MMC_IVAR_BUS_WIDTH:
1467 *(int *)result = ivar->bus_width;
1469 case MMC_IVAR_ERASE_SECTOR:
1470 *(int *)result = ivar->erase_sector;
1472 case MMC_IVAR_MAX_DATA:
1473 *(int *)result = mmcbr_get_max_data(bus);
1480 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1483 * None are writable ATM
1490 mmc_delayed_attach(void *xsc)
1492 struct mmc_softc *sc = xsc;
1495 config_intrhook_disestablish(&sc->config_intrhook);
1498 static device_method_t mmc_methods[] = {
1500 DEVMETHOD(device_probe, mmc_probe),
1501 DEVMETHOD(device_attach, mmc_attach),
1502 DEVMETHOD(device_detach, mmc_detach),
1503 DEVMETHOD(device_suspend, mmc_suspend),
1504 DEVMETHOD(device_resume, mmc_resume),
1507 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1508 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1510 /* MMC Bus interface */
1511 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1512 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1513 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1518 static driver_t mmc_driver = {
1521 sizeof(struct mmc_softc),
1523 static devclass_t mmc_devclass;
1526 DRIVER_MODULE(mmc, sdhci, mmc_driver, mmc_devclass, NULL, NULL);