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.
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/module.h>
62 #include <sys/mutex.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
68 #include <dev/mmc/bridge.h>
69 #include <dev/mmc/mmc_private.h>
70 #include <dev/mmc/mmc_subr.h>
71 #include <dev/mmc/mmcreg.h>
72 #include <dev/mmc/mmcbrvar.h>
73 #include <dev/mmc/mmcvar.h>
76 #include "mmcbus_if.h"
82 uint32_t raw_cid[4]; /* Raw bits of the CID */
83 uint32_t raw_csd[4]; /* Raw bits of the CSD */
84 uint32_t raw_scr[2]; /* Raw bits of the SCR */
85 uint8_t raw_ext_csd[MMC_EXTCSD_SIZE]; /* Raw bits of the EXT_CSD */
86 uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */
88 enum mmc_card_mode mode;
89 struct mmc_cid cid; /* cid decoded */
90 struct mmc_csd csd; /* csd decoded */
91 struct mmc_scr scr; /* scr decoded */
92 struct mmc_sd_status sd_status; /* SD_STATUS decoded */
93 u_char read_only; /* True when the device is read-only */
94 u_char bus_width; /* Bus width to use */
95 u_char timing; /* Bus timing support */
96 u_char high_cap; /* High Capacity card (block addressed) */
97 uint32_t sec_count; /* Card capacity in 512byte blocks */
98 uint32_t tran_speed; /* Max speed in normal mode */
99 uint32_t hs_tran_speed; /* Max speed in high speed mode */
100 uint32_t erase_sector; /* Card native erase sector size */
101 uint32_t cmd6_time; /* Generic switch timeout [us] */
102 char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
103 char card_sn_string[16];/* Formatted serial # for disk->d_ident */
106 #define CMD_RETRIES 3
108 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
110 static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
112 static int mmc_debug;
113 TUNABLE_INT("hw.mmc.debug", &mmc_debug);
114 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &mmc_debug, 0,
117 /* bus entry points */
118 static int mmc_acquire_bus(device_t busdev, device_t dev);
119 static int mmc_attach(device_t dev);
120 static int mmc_child_location_str(device_t dev, device_t child, char *buf,
122 static int mmc_detach(device_t dev);
123 static int mmc_probe(device_t dev);
124 static int mmc_read_ivar(device_t bus, device_t child, int which,
126 static int mmc_release_bus(device_t busdev, device_t dev);
127 static int mmc_resume(device_t dev);
128 static int mmc_suspend(device_t dev);
129 static int mmc_wait_for_request(device_t brdev, device_t reqdev,
130 struct mmc_request *req);
131 static int mmc_write_ivar(device_t bus, device_t child, int which,
134 #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
135 #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
136 #define MMC_LOCK_INIT(_sc) \
137 mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->dev), \
139 #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx);
140 #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED);
141 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED);
143 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
144 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
145 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
146 struct mmc_sd_status *sd_status);
147 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
148 uint32_t *rawsdstatus);
149 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
151 static int mmc_calculate_clock(struct mmc_softc *sc);
152 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid,
154 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
155 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
156 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
157 static void mmc_delayed_attach(void *xsc);
158 static int mmc_delete_cards(struct mmc_softc *sc);
159 static void mmc_discover_cards(struct mmc_softc *sc);
160 static void mmc_format_card_id_string(struct mmc_ivars *ivar);
161 static void mmc_go_discovery(struct mmc_softc *sc);
162 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
164 static int mmc_highest_voltage(uint32_t ocr);
165 static void mmc_idle_cards(struct mmc_softc *sc);
166 static void mmc_ms_delay(int ms);
167 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
168 static void mmc_power_down(struct mmc_softc *sc);
169 static void mmc_power_up(struct mmc_softc *sc);
170 static void mmc_rescan_cards(struct mmc_softc *sc);
171 static void mmc_scan(struct mmc_softc *sc);
172 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
173 uint8_t value, uint8_t *res);
174 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
175 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
176 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
178 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
179 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
180 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
182 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
183 static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
184 static int mmc_set_card_bus_width(struct mmc_softc *sc,
185 struct mmc_ivars *ivar);
186 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
187 static int mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar,
189 static int mmc_test_bus_width(struct mmc_softc *sc);
190 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
191 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
192 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
193 static void mmc_wakeup(struct mmc_request *req);
199 DELAY(1000 * ms); /* XXX BAD */
203 mmc_probe(device_t dev)
206 device_set_desc(dev, "MMC/SD bus");
211 mmc_attach(device_t dev)
213 struct mmc_softc *sc;
215 sc = device_get_softc(dev);
219 /* We'll probe and attach our children later, but before / mount */
220 sc->config_intrhook.ich_func = mmc_delayed_attach;
221 sc->config_intrhook.ich_arg = sc;
222 if (config_intrhook_establish(&sc->config_intrhook) != 0)
223 device_printf(dev, "config_intrhook_establish failed\n");
228 mmc_detach(device_t dev)
230 struct mmc_softc *sc = device_get_softc(dev);
233 if ((err = mmc_delete_cards(sc)) != 0)
236 MMC_LOCK_DESTROY(sc);
242 mmc_suspend(device_t dev)
244 struct mmc_softc *sc = device_get_softc(dev);
247 err = bus_generic_suspend(dev);
255 mmc_resume(device_t dev)
257 struct mmc_softc *sc = device_get_softc(dev);
260 return (bus_generic_resume(dev));
264 mmc_acquire_bus(device_t busdev, device_t dev)
266 struct mmc_softc *sc;
267 struct mmc_ivars *ivar;
271 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
274 sc = device_get_softc(busdev);
277 panic("mmc: host bridge didn't serialize us.");
283 * Keep track of the last rca that we've selected. If
284 * we're asked to do it again, don't. We never
285 * unselect unless the bus code itself wants the mmc
286 * bus, and constantly reselecting causes problems.
288 ivar = device_get_ivars(dev);
290 if (sc->last_rca != rca) {
291 mmc_select_card(sc, rca);
293 /* Prepare bus width for the new card. */
294 if (bootverbose || mmc_debug) {
295 device_printf(busdev,
296 "setting bus width to %d bits\n",
297 (ivar->bus_width == bus_width_4) ? 4 :
298 (ivar->bus_width == bus_width_8) ? 8 : 1);
300 mmc_set_card_bus_width(sc, ivar);
301 mmcbr_set_bus_width(busdev, ivar->bus_width);
302 mmcbr_update_ios(busdev);
306 * If there's a card selected, stand down.
308 if (sc->last_rca != 0) {
309 mmc_select_card(sc, 0);
318 mmc_release_bus(device_t busdev, device_t dev)
320 struct mmc_softc *sc;
323 sc = device_get_softc(busdev);
327 panic("mmc: releasing unowned bus.");
328 if (sc->owner != dev)
329 panic("mmc: you don't own the bus. game over.");
331 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
341 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
344 return (ocr & MMC_OCR_VOLTAGE);
348 mmc_highest_voltage(uint32_t ocr)
352 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
353 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
360 mmc_wakeup(struct mmc_request *req)
362 struct mmc_softc *sc;
364 sc = (struct mmc_softc *)req->done_data;
366 req->flags |= MMC_REQ_DONE;
372 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
375 req->done = mmc_wakeup;
378 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
379 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
380 if (req->cmd->data) {
381 printf(" data %d\n", (int)req->cmd->data->len);
385 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
387 while ((req->flags & MMC_REQ_DONE) == 0)
388 msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
390 if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
391 device_printf(sc->dev, "CMD%d RESULT: %d\n",
392 req->cmd->opcode, req->cmd->error);
397 mmc_wait_for_request(device_t brdev, device_t reqdev __unused,
398 struct mmc_request *req)
400 struct mmc_softc *sc = device_get_softc(brdev);
402 return (mmc_wait_for_req(sc, req));
406 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
407 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
409 struct mmc_command cmd;
412 memset(&cmd, 0, sizeof(cmd));
417 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, retries);
421 if (flags & MMC_RSP_136)
422 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
430 mmc_idle_cards(struct mmc_softc *sc)
433 struct mmc_command cmd;
436 mmcbr_set_chip_select(dev, cs_high);
437 mmcbr_update_ios(dev);
440 memset(&cmd, 0, sizeof(cmd));
441 cmd.opcode = MMC_GO_IDLE_STATE;
443 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
445 mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
448 mmcbr_set_chip_select(dev, cs_dontcare);
449 mmcbr_update_ios(dev);
454 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
456 struct mmc_command cmd;
457 int err = MMC_ERR_NONE, i;
459 memset(&cmd, 0, sizeof(cmd));
460 cmd.opcode = ACMD_SD_SEND_OP_COND;
462 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
465 for (i = 0; i < 1000; i++) {
466 err = mmc_wait_for_app_cmd(sc->dev, sc->dev, 0, &cmd,
468 if (err != MMC_ERR_NONE)
470 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
471 (ocr & MMC_OCR_VOLTAGE) == 0)
473 err = MMC_ERR_TIMEOUT;
476 if (rocr && err == MMC_ERR_NONE)
482 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
484 struct mmc_command cmd;
485 int err = MMC_ERR_NONE, i;
487 memset(&cmd, 0, sizeof(cmd));
488 cmd.opcode = MMC_SEND_OP_COND;
490 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
493 for (i = 0; i < 1000; i++) {
494 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
495 if (err != MMC_ERR_NONE)
497 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
498 (ocr & MMC_OCR_VOLTAGE) == 0)
500 err = MMC_ERR_TIMEOUT;
503 if (rocr && err == MMC_ERR_NONE)
509 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
511 struct mmc_command cmd;
514 memset(&cmd, 0, sizeof(cmd));
515 cmd.opcode = SD_SEND_IF_COND;
516 cmd.arg = (vhs << 8) + 0xAA;
517 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
520 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
525 mmc_power_up(struct mmc_softc *sc)
530 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
531 mmcbr_set_bus_mode(dev, opendrain);
532 mmcbr_set_chip_select(dev, cs_dontcare);
533 mmcbr_set_bus_width(dev, bus_width_1);
534 mmcbr_set_power_mode(dev, power_up);
535 mmcbr_set_clock(dev, 0);
536 mmcbr_update_ios(dev);
539 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
540 mmcbr_set_timing(dev, bus_timing_normal);
541 mmcbr_set_power_mode(dev, power_on);
542 mmcbr_update_ios(dev);
547 mmc_power_down(struct mmc_softc *sc)
549 device_t dev = sc->dev;
551 mmcbr_set_bus_mode(dev, opendrain);
552 mmcbr_set_chip_select(dev, cs_dontcare);
553 mmcbr_set_bus_width(dev, bus_width_1);
554 mmcbr_set_power_mode(dev, power_off);
555 mmcbr_set_clock(dev, 0);
556 mmcbr_set_timing(dev, bus_timing_normal);
557 mmcbr_update_ios(dev);
561 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
565 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
566 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
567 flags, NULL, CMD_RETRIES));
571 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
575 struct mmc_command cmd;
576 struct mmc_data data;
578 memset(&cmd, 0, sizeof(cmd));
579 memset(&data, 0, sizeof(data));
582 cmd.opcode = SD_SWITCH_FUNC;
583 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
584 cmd.arg = mode << 31; /* 0 - check, 1 - set */
585 cmd.arg |= 0x00FFFFFF;
586 cmd.arg &= ~(0xF << (grp * 4));
587 cmd.arg |= value << (grp * 4);
592 data.flags = MMC_DATA_READ;
594 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
599 mmc_set_card_bus_width(struct mmc_softc *sc, struct mmc_ivars *ivar)
601 struct mmc_command cmd;
605 if (mmcbr_get_mode(sc->dev) == mode_sd) {
606 memset(&cmd, 0, sizeof(cmd));
607 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
608 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
609 cmd.arg = SD_CLR_CARD_DETECT;
610 err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd,
614 memset(&cmd, 0, sizeof(cmd));
615 cmd.opcode = ACMD_SET_BUS_WIDTH;
616 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
617 switch (ivar->bus_width) {
619 cmd.arg = SD_BUS_WIDTH_1;
622 cmd.arg = SD_BUS_WIDTH_4;
625 return (MMC_ERR_INVALID);
627 err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd,
630 switch (ivar->bus_width) {
632 value = EXT_CSD_BUS_WIDTH_1;
635 value = EXT_CSD_BUS_WIDTH_4;
638 value = EXT_CSD_BUS_WIDTH_8;
641 return (MMC_ERR_INVALID);
643 err = mmc_switch(sc->dev, sc->dev, ivar->rca,
644 EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, value,
645 ivar->cmd6_time, true);
651 mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar, int timing)
653 u_char switch_res[64];
658 case bus_timing_normal:
665 return (MMC_ERR_INVALID);
667 if (mmcbr_get_mode(sc->dev) == mode_sd) {
668 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
670 if (err != MMC_ERR_NONE)
672 if ((switch_res[16] & 0xf) != value)
673 return (MMC_ERR_FAILED);
674 mmcbr_set_timing(sc->dev, timing);
675 mmcbr_update_ios(sc->dev);
677 err = mmc_switch(sc->dev, sc->dev, ivar->rca,
678 EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, value,
679 ivar->cmd6_time, false);
680 if (err != MMC_ERR_NONE)
682 mmcbr_set_timing(sc->dev, timing);
683 mmcbr_update_ios(sc->dev);
684 err = mmc_switch_status(sc->dev, sc->dev, ivar->rca,
690 static const uint8_t p8[8] = {
691 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
694 static const uint8_t p8ok[8] = {
695 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
698 static const uint8_t p4[4] = {
699 0x5A, 0x00, 0x00, 0x00
702 static const uint8_t p4ok[4] = {
703 0xA5, 0x00, 0x00, 0x00
707 mmc_test_bus_width(struct mmc_softc *sc)
709 struct mmc_command cmd;
710 struct mmc_data data;
714 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
715 mmcbr_set_bus_width(sc->dev, bus_width_8);
716 mmcbr_update_ios(sc->dev);
718 sc->squelched++; /* Errors are expected, squelch reporting. */
719 memset(&cmd, 0, sizeof(cmd));
720 memset(&data, 0, sizeof(data));
721 cmd.opcode = MMC_BUSTEST_W;
723 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
726 data.data = __DECONST(void *, p8);
728 data.flags = MMC_DATA_WRITE;
729 mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
731 memset(&cmd, 0, sizeof(cmd));
732 memset(&data, 0, sizeof(data));
733 cmd.opcode = MMC_BUSTEST_R;
735 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
740 data.flags = MMC_DATA_READ;
741 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
744 mmcbr_set_bus_width(sc->dev, bus_width_1);
745 mmcbr_update_ios(sc->dev);
747 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
748 return (bus_width_8);
751 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
752 mmcbr_set_bus_width(sc->dev, bus_width_4);
753 mmcbr_update_ios(sc->dev);
755 sc->squelched++; /* Errors are expected, squelch reporting. */
756 memset(&cmd, 0, sizeof(cmd));
757 memset(&data, 0, sizeof(data));
758 cmd.opcode = MMC_BUSTEST_W;
760 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
763 data.data = __DECONST(void *, p4);
765 data.flags = MMC_DATA_WRITE;
766 mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
768 memset(&cmd, 0, sizeof(cmd));
769 memset(&data, 0, sizeof(data));
770 cmd.opcode = MMC_BUSTEST_R;
772 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
777 data.flags = MMC_DATA_READ;
778 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
781 mmcbr_set_bus_width(sc->dev, bus_width_1);
782 mmcbr_update_ios(sc->dev);
784 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
785 return (bus_width_4);
787 return (bus_width_1);
791 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
793 const int i = (bit_len / 32) - (start / 32) - 1;
794 const int shift = start & 31;
795 uint32_t retval = bits[i] >> shift;
797 if (size + shift > 32)
798 retval |= bits[i - 1] << (32 - shift);
799 return (retval & ((1llu << size) - 1));
803 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
807 /* There's no version info, so we take it on faith */
808 memset(cid, 0, sizeof(*cid));
809 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
810 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
811 for (i = 0; i < 5; i++)
812 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
814 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
815 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
816 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
817 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
821 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid, bool is_4_41p)
825 /* There's no version info, so we take it on faith */
826 memset(cid, 0, sizeof(*cid));
827 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
828 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
829 for (i = 0; i < 6; i++)
830 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
832 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
833 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
834 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
835 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4);
837 cid->mdt_year += 2013;
839 cid->mdt_year += 1997;
843 mmc_format_card_id_string(struct mmc_ivars *ivar)
850 * Format a card ID string for use by the mmcsd driver, it's what
851 * appears between the <> in the following:
852 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
853 * 22.5MHz/4bit/128-block
855 * Also format just the card serial number, which the mmcsd driver will
856 * use as the disk->d_ident string.
858 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
859 * and our max formatted length is currently 55 bytes if every field
860 * contains the largest value.
862 * Sometimes the oid is two printable ascii chars; when it's not,
863 * format it as 0xnnnn instead.
865 c1 = (ivar->cid.oid >> 8) & 0x0ff;
866 c2 = ivar->cid.oid & 0x0ff;
867 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
868 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
870 snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
871 snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
872 "%08X", ivar->cid.psn);
873 snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
874 "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
875 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
876 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
877 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
878 ivar->cid.mid, oidstr);
881 static const int exp[8] = {
882 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
885 static const int mant[16] = {
886 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
889 static const int cur_min[8] = {
890 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
893 static const int cur_max[8] = {
894 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
898 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
904 memset(csd, 0, sizeof(*csd));
905 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
907 m = mmc_get_bits(raw_csd, 128, 115, 4);
908 e = mmc_get_bits(raw_csd, 128, 112, 3);
909 csd->tacc = (exp[e] * mant[m] + 9) / 10;
910 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
911 m = mmc_get_bits(raw_csd, 128, 99, 4);
912 e = mmc_get_bits(raw_csd, 128, 96, 3);
913 csd->tran_speed = exp[e] * 10000 * mant[m];
914 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
915 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
916 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
917 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
918 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
919 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
920 csd->vdd_r_curr_min =
921 cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
922 csd->vdd_r_curr_max =
923 cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
924 csd->vdd_w_curr_min =
925 cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
926 csd->vdd_w_curr_max =
927 cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
928 m = mmc_get_bits(raw_csd, 128, 62, 12);
929 e = mmc_get_bits(raw_csd, 128, 47, 3);
930 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
931 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
932 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
933 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
934 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
935 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
936 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
937 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
939 m = mmc_get_bits(raw_csd, 128, 115, 4);
940 e = mmc_get_bits(raw_csd, 128, 112, 3);
941 csd->tacc = (exp[e] * mant[m] + 9) / 10;
942 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
943 m = mmc_get_bits(raw_csd, 128, 99, 4);
944 e = mmc_get_bits(raw_csd, 128, 96, 3);
945 csd->tran_speed = exp[e] * 10000 * mant[m];
946 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
947 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
948 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
949 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
950 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
951 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
952 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) +
954 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
955 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
956 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
957 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
958 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
959 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
960 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
962 panic("unknown SD CSD version");
966 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
971 memset(csd, 0, sizeof(*csd));
972 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
973 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
974 m = mmc_get_bits(raw_csd, 128, 115, 4);
975 e = mmc_get_bits(raw_csd, 128, 112, 3);
976 csd->tacc = exp[e] * mant[m] + 9 / 10;
977 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
978 m = mmc_get_bits(raw_csd, 128, 99, 4);
979 e = mmc_get_bits(raw_csd, 128, 96, 3);
980 csd->tran_speed = exp[e] * 10000 * mant[m];
981 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
982 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
983 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
984 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
985 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
986 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
987 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
988 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
989 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
990 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
991 m = mmc_get_bits(raw_csd, 128, 62, 12);
992 e = mmc_get_bits(raw_csd, 128, 47, 3);
993 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
994 csd->erase_blk_en = 0;
995 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
996 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
997 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
998 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
999 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1000 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1001 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1005 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1007 unsigned int scr_struct;
1009 memset(scr, 0, sizeof(*scr));
1011 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1012 if (scr_struct != 0) {
1013 printf("Unrecognised SCR structure version %d\n",
1017 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1018 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1022 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1023 struct mmc_sd_status *sd_status)
1026 memset(sd_status, 0, sizeof(*sd_status));
1028 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1029 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1030 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1031 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1032 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1033 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1034 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1035 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1036 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1037 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1041 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1043 struct mmc_command cmd;
1046 memset(&cmd, 0, sizeof(cmd));
1047 cmd.opcode = MMC_ALL_SEND_CID;
1049 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1051 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1052 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1057 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1059 struct mmc_command cmd;
1062 memset(&cmd, 0, sizeof(cmd));
1063 cmd.opcode = MMC_SEND_CSD;
1064 cmd.arg = rca << 16;
1065 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1067 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1068 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1073 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1076 struct mmc_command cmd;
1077 struct mmc_data data;
1079 memset(&cmd, 0, sizeof(cmd));
1080 memset(&data, 0, sizeof(data));
1082 memset(rawscr, 0, 8);
1083 cmd.opcode = ACMD_SEND_SCR;
1084 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1090 data.flags = MMC_DATA_READ;
1092 err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES);
1093 rawscr[0] = be32toh(rawscr[0]);
1094 rawscr[1] = be32toh(rawscr[1]);
1099 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1101 struct mmc_command cmd;
1102 struct mmc_data data;
1105 memset(&cmd, 0, sizeof(cmd));
1106 memset(&data, 0, sizeof(data));
1108 memset(rawsdstatus, 0, 64);
1109 cmd.opcode = ACMD_SD_STATUS;
1110 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1114 data.data = rawsdstatus;
1116 data.flags = MMC_DATA_READ;
1118 err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES);
1119 for (i = 0; i < 16; i++)
1120 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1125 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1127 struct mmc_command cmd;
1130 memset(&cmd, 0, sizeof(cmd));
1131 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1132 cmd.arg = resp << 16;
1133 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1135 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1140 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1142 struct mmc_command cmd;
1145 memset(&cmd, 0, sizeof(cmd));
1146 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1148 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1150 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1151 *resp = cmd.resp[0];
1156 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1158 struct mmc_command cmd;
1161 memset(&cmd, 0, sizeof(cmd));
1162 cmd.opcode = MMC_SET_BLOCKLEN;
1164 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1166 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1171 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1174 device_printf(dev, "Card at relative address 0x%04x%s:\n",
1175 ivar->rca, newcard ? " added" : "");
1176 device_printf(dev, " card: %s\n", ivar->card_id_string);
1177 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1178 (ivar->bus_width == bus_width_1 ? 1 :
1179 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1180 (ivar->timing == bus_timing_hs ?
1181 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1182 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1183 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1184 ivar->sec_count, ivar->erase_sector,
1185 ivar->read_only ? ", read-only" : "");
1189 mmc_discover_cards(struct mmc_softc *sc)
1191 u_char switch_res[64];
1192 uint32_t raw_cid[4];
1193 struct mmc_ivars *ivar = NULL;
1196 int err, i, devcount, newcard;
1197 uint32_t resp, sec_count, status;
1200 if (bootverbose || mmc_debug)
1201 device_printf(sc->dev, "Probing cards\n");
1203 sc->squelched++; /* Errors are expected, squelch reporting. */
1204 err = mmc_all_send_cid(sc, raw_cid);
1206 if (err == MMC_ERR_TIMEOUT)
1208 if (err != MMC_ERR_NONE) {
1209 device_printf(sc->dev, "Error reading CID %d\n", err);
1213 if ((err = device_get_children(sc->dev, &devlist,
1216 for (i = 0; i < devcount; i++) {
1217 ivar = device_get_ivars(devlist[i]);
1218 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) ==
1224 free(devlist, M_TEMP);
1225 if (bootverbose || mmc_debug) {
1226 device_printf(sc->dev,
1227 "%sard detected (CID %08x%08x%08x%08x)\n",
1228 newcard ? "New c" : "C",
1229 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1232 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1234 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1236 if (mmcbr_get_ro(sc->dev))
1237 ivar->read_only = 1;
1238 ivar->bus_width = bus_width_1;
1239 ivar->timing = bus_timing_normal;
1240 ivar->mode = mmcbr_get_mode(sc->dev);
1241 if (ivar->mode == mode_sd) {
1242 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1243 mmc_send_relative_addr(sc, &resp);
1244 ivar->rca = resp >> 16;
1246 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1247 if (bootverbose || mmc_debug)
1248 device_printf(sc->dev,
1249 "%sard detected (CSD %08x%08x%08x%08x)\n",
1250 newcard ? "New c" : "C", ivar->raw_csd[0],
1251 ivar->raw_csd[1], ivar->raw_csd[2],
1253 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1254 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1255 if (ivar->csd.csd_structure > 0)
1257 ivar->tran_speed = ivar->csd.tran_speed;
1258 ivar->erase_sector = ivar->csd.erase_sector *
1259 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1261 err = mmc_send_status(sc->dev, sc->dev, ivar->rca,
1263 if (err != MMC_ERR_NONE) {
1264 device_printf(sc->dev,
1265 "Error reading card status %d\n", err);
1268 if ((status & R1_CARD_IS_LOCKED) != 0) {
1269 device_printf(sc->dev,
1270 "Card is password protected, skipping.\n");
1274 /* Get card SCR. Card must be selected to fetch it. */
1275 mmc_select_card(sc, ivar->rca);
1276 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1277 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1278 /* Get card switch capabilities (command class 10). */
1279 if ((ivar->scr.sda_vsn >= 1) &&
1280 (ivar->csd.ccc & (1 << 10))) {
1281 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1282 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1284 if (switch_res[13] & (1 << SD_SWITCH_HS_MODE)) {
1285 ivar->timing = bus_timing_hs;
1286 ivar->hs_tran_speed = SD_MAX_HS;
1291 * We deselect then reselect the card here. Some cards
1292 * become unselected and timeout with the above two
1293 * commands, although the state tables / diagrams in the
1294 * standard suggest they go back to the transfer state.
1295 * Other cards don't become deselected, and if we
1296 * attempt to blindly re-select them, we get timeout
1297 * errors from some controllers. So we deselect then
1298 * reselect to handle all situations. The only thing we
1299 * use from the sd_status is the erase sector size, but
1300 * it is still nice to get that right.
1302 mmc_select_card(sc, 0);
1303 mmc_select_card(sc, ivar->rca);
1304 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1305 mmc_app_decode_sd_status(ivar->raw_sd_status,
1307 if (ivar->sd_status.au_size != 0) {
1308 ivar->erase_sector =
1309 16 << ivar->sd_status.au_size;
1311 /* Find max supported bus width. */
1312 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1313 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1314 ivar->bus_width = bus_width_4;
1317 * Some cards that report maximum I/O block sizes
1318 * greater than 512 require the block length to be
1319 * set to 512, even though that is supposed to be
1320 * the default. Example:
1322 * Transcend 2GB SDSC card, CID:
1323 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1325 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1326 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1327 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1329 mmc_format_card_id_string(ivar);
1331 if (bootverbose || mmc_debug)
1332 mmc_log_card(sc->dev, ivar, newcard);
1335 child = device_add_child(sc->dev, NULL, -1);
1336 device_set_ivars(child, ivar);
1338 mmc_select_card(sc, 0);
1342 mmc_set_relative_addr(sc, ivar->rca);
1344 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1345 if (bootverbose || mmc_debug)
1346 device_printf(sc->dev,
1347 "%sard detected (CSD %08x%08x%08x%08x)\n",
1348 newcard ? "New c" : "C", ivar->raw_csd[0],
1349 ivar->raw_csd[1], ivar->raw_csd[2],
1352 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1353 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1354 ivar->tran_speed = ivar->csd.tran_speed;
1355 ivar->erase_sector = ivar->csd.erase_sector *
1356 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1358 err = mmc_send_status(sc->dev, sc->dev, ivar->rca, &status);
1359 if (err != MMC_ERR_NONE) {
1360 device_printf(sc->dev,
1361 "Error reading card status %d\n", err);
1364 if ((status & R1_CARD_IS_LOCKED) != 0) {
1365 device_printf(sc->dev,
1366 "Card is password protected, skipping.\n");
1370 mmc_select_card(sc, ivar->rca);
1372 /* Only MMC >= 4.x devices support EXT_CSD. */
1373 if (ivar->csd.spec_vers >= 4) {
1374 err = mmc_send_ext_csd(sc->dev, sc->dev,
1376 if (err != MMC_ERR_NONE) {
1377 device_printf(sc->dev,
1378 "Error reading EXT_CSD %d\n", err);
1381 /* Handle extended capacity from EXT_CSD */
1382 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1383 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1384 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1385 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1386 if (sec_count != 0) {
1387 ivar->sec_count = sec_count;
1390 /* Get card speed in high speed mode. */
1391 ivar->timing = bus_timing_hs;
1392 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1393 & EXT_CSD_CARD_TYPE_52)
1394 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1395 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1396 & EXT_CSD_CARD_TYPE_26)
1397 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1399 ivar->hs_tran_speed = ivar->tran_speed;
1401 * Determine generic switch timeout (provided in
1402 * units of 10 ms), defaulting to 500 ms.
1404 ivar->cmd6_time = 500 * 1000;
1405 if (ivar->csd.spec_vers >= 6)
1406 ivar->cmd6_time = 10 *
1407 ivar->raw_ext_csd[EXT_CSD_GEN_CMD6_TIME];
1408 /* Find max supported bus width. */
1409 ivar->bus_width = mmc_test_bus_width(sc);
1410 /* Handle HC erase sector size. */
1411 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1412 ivar->erase_sector = 1024 *
1413 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1414 err = mmc_switch(sc->dev, sc->dev, ivar->rca,
1415 EXT_CSD_CMD_SET_NORMAL,
1416 EXT_CSD_ERASE_GRP_DEF,
1417 EXT_CSD_ERASE_GRP_DEF_EN,
1418 ivar->cmd6_time, true);
1419 if (err != MMC_ERR_NONE) {
1420 device_printf(sc->dev,
1421 "Error setting erase group %d\n",
1427 ivar->bus_width = bus_width_1;
1428 ivar->timing = bus_timing_normal;
1432 * Some cards that report maximum I/O block sizes greater
1433 * than 512 require the block length to be set to 512, even
1434 * though that is supposed to be the default. Example:
1436 * Transcend 2GB SDSC card, CID:
1437 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1439 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1440 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1441 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1443 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid,
1444 ivar->raw_ext_csd[EXT_CSD_REV] >= 5);
1445 mmc_format_card_id_string(ivar);
1447 if (bootverbose || mmc_debug)
1448 mmc_log_card(sc->dev, ivar, newcard);
1451 child = device_add_child(sc->dev, NULL, -1);
1452 device_set_ivars(child, ivar);
1454 mmc_select_card(sc, 0);
1459 mmc_rescan_cards(struct mmc_softc *sc)
1461 struct mmc_ivars *ivar;
1463 int err, i, devcount;
1465 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1467 for (i = 0; i < devcount; i++) {
1468 ivar = device_get_ivars(devlist[i]);
1469 if (mmc_select_card(sc, ivar->rca) != MMC_ERR_NONE) {
1470 if (bootverbose || mmc_debug)
1471 device_printf(sc->dev,
1472 "Card at relative address %d lost.\n",
1474 device_delete_child(sc->dev, devlist[i]);
1475 free(ivar, M_DEVBUF);
1478 free(devlist, M_TEMP);
1479 mmc_select_card(sc, 0);
1483 mmc_delete_cards(struct mmc_softc *sc)
1485 struct mmc_ivars *ivar;
1487 int err, i, devcount;
1489 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1491 for (i = 0; i < devcount; i++) {
1492 ivar = device_get_ivars(devlist[i]);
1493 if (bootverbose || mmc_debug)
1494 device_printf(sc->dev,
1495 "Card at relative address %d deleted.\n",
1497 device_delete_child(sc->dev, devlist[i]);
1498 free(ivar, M_DEVBUF);
1500 free(devlist, M_TEMP);
1505 mmc_go_discovery(struct mmc_softc *sc)
1512 if (mmcbr_get_power_mode(dev) != power_on) {
1514 * First, try SD modes
1516 sc->squelched++; /* Errors are expected, squelch reporting. */
1517 mmcbr_set_mode(dev, mode_sd);
1519 mmcbr_set_bus_mode(dev, pushpull);
1520 if (bootverbose || mmc_debug)
1521 device_printf(sc->dev, "Probing bus\n");
1523 err = mmc_send_if_cond(sc, 1);
1524 if ((bootverbose || mmc_debug) && err == 0)
1525 device_printf(sc->dev,
1526 "SD 2.0 interface conditions: OK\n");
1527 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1528 if (bootverbose || mmc_debug)
1529 device_printf(sc->dev, "SD probe: failed\n");
1533 mmcbr_set_mode(dev, mode_mmc);
1534 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1535 if (bootverbose || mmc_debug)
1536 device_printf(sc->dev,
1537 "MMC probe: failed\n");
1538 ocr = 0; /* Failed both, powerdown. */
1539 } else if (bootverbose || mmc_debug)
1540 device_printf(sc->dev,
1541 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1542 } else if (bootverbose || mmc_debug)
1543 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n",
1547 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1548 if (mmcbr_get_ocr(dev) != 0)
1551 mmcbr_set_bus_mode(dev, opendrain);
1552 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1553 mmcbr_update_ios(dev);
1554 /* XXX recompute vdd based on new cards? */
1557 * Make sure that we have a mutually agreeable voltage to at least
1558 * one card on the bus.
1560 if (bootverbose || mmc_debug)
1561 device_printf(sc->dev, "Current OCR: 0x%08x\n",
1562 mmcbr_get_ocr(dev));
1563 if (mmcbr_get_ocr(dev) == 0) {
1564 device_printf(sc->dev, "No compatible cards found on bus\n");
1565 mmc_delete_cards(sc);
1570 * Reselect the cards after we've idled them above.
1572 if (mmcbr_get_mode(dev) == mode_sd) {
1573 err = mmc_send_if_cond(sc, 1);
1574 mmc_send_app_op_cond(sc,
1575 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1577 mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1578 mmc_discover_cards(sc);
1579 mmc_rescan_cards(sc);
1581 mmcbr_set_bus_mode(dev, pushpull);
1582 mmcbr_update_ios(dev);
1583 mmc_calculate_clock(sc);
1587 mmc_calculate_clock(struct mmc_softc *sc)
1590 struct mmc_ivars *ivar;
1591 int i, f_max, max_dtr, max_hs_dtr, max_timing, nkid;
1593 f_max = mmcbr_get_f_max(sc->dev);
1594 max_dtr = max_hs_dtr = f_max;
1595 if (mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED)
1596 max_timing = bus_timing_hs;
1598 max_timing = bus_timing_normal;
1599 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1600 panic("can't get children");
1601 for (i = 0; i < nkid; i++) {
1602 ivar = device_get_ivars(kids[i]);
1603 if (ivar->timing < max_timing)
1604 max_timing = ivar->timing;
1605 if (ivar->tran_speed < max_dtr)
1606 max_dtr = ivar->tran_speed;
1607 if (ivar->hs_tran_speed < max_hs_dtr)
1608 max_hs_dtr = ivar->hs_tran_speed;
1610 if (bootverbose || mmc_debug) {
1611 device_printf(sc->dev,
1612 "setting transfer rate to %d.%03dMHz%s\n",
1613 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1614 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1616 for (i = 0; i < nkid; i++) {
1617 ivar = device_get_ivars(kids[i]);
1618 if (ivar->timing == bus_timing_normal)
1620 mmc_select_card(sc, ivar->rca);
1621 mmc_set_timing(sc, ivar, max_timing);
1623 mmc_select_card(sc, 0);
1625 if (max_timing == bus_timing_hs)
1626 max_dtr = max_hs_dtr;
1627 mmcbr_set_clock(sc->dev, max_dtr);
1628 mmcbr_update_ios(sc->dev);
1633 mmc_scan(struct mmc_softc *sc)
1635 device_t dev = sc->dev;
1637 mmc_acquire_bus(dev, dev);
1638 mmc_go_discovery(sc);
1639 mmc_release_bus(dev, dev);
1641 bus_generic_attach(dev);
1645 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1647 struct mmc_ivars *ivar = device_get_ivars(child);
1652 case MMC_IVAR_SPEC_VERS:
1653 *result = ivar->csd.spec_vers;
1655 case MMC_IVAR_DSR_IMP:
1656 *result = ivar->csd.dsr_imp;
1658 case MMC_IVAR_MEDIA_SIZE:
1659 *result = ivar->sec_count;
1662 *result = ivar->rca;
1664 case MMC_IVAR_SECTOR_SIZE:
1665 *result = MMC_SECTOR_SIZE;
1667 case MMC_IVAR_TRAN_SPEED:
1668 *result = mmcbr_get_clock(bus);
1670 case MMC_IVAR_READ_ONLY:
1671 *result = ivar->read_only;
1673 case MMC_IVAR_HIGH_CAP:
1674 *result = ivar->high_cap;
1676 case MMC_IVAR_CARD_TYPE:
1677 *result = ivar->mode;
1679 case MMC_IVAR_BUS_WIDTH:
1680 *result = ivar->bus_width;
1682 case MMC_IVAR_ERASE_SECTOR:
1683 *result = ivar->erase_sector;
1685 case MMC_IVAR_MAX_DATA:
1686 *result = mmcbr_get_max_data(bus);
1688 case MMC_IVAR_CARD_ID_STRING:
1689 *(char **)result = ivar->card_id_string;
1691 case MMC_IVAR_CARD_SN_STRING:
1692 *(char **)result = ivar->card_sn_string;
1699 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1703 * None are writable ATM
1709 mmc_delayed_attach(void *xsc)
1711 struct mmc_softc *sc = xsc;
1714 config_intrhook_disestablish(&sc->config_intrhook);
1718 mmc_child_location_str(device_t dev, device_t child, char *buf,
1722 snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1726 static device_method_t mmc_methods[] = {
1728 DEVMETHOD(device_probe, mmc_probe),
1729 DEVMETHOD(device_attach, mmc_attach),
1730 DEVMETHOD(device_detach, mmc_detach),
1731 DEVMETHOD(device_suspend, mmc_suspend),
1732 DEVMETHOD(device_resume, mmc_resume),
1735 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1736 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1737 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1739 /* MMC Bus interface */
1740 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1741 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1742 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1747 driver_t mmc_driver = {
1750 sizeof(struct mmc_softc),
1752 devclass_t mmc_devclass;
1754 MODULE_VERSION(mmc, MMC_VERSION);