2 * Copyright (c) 2006 Bernd Walter. All rights reserved.
3 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
4 * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * Portions of this software may have been developed with reference to
27 * the SD Simplified Specification. The following disclaimer may apply:
29 * The following conditions apply to the release of the simplified
30 * specification ("Simplified Specification") by the SD Card Association and
31 * the SD Group. The Simplified Specification is a subset of the complete SD
32 * Specification which is owned by the SD Card Association and the SD
33 * Group. This Simplified Specification is provided on a non-confidential
34 * basis subject to the disclaimers below. Any implementation of the
35 * Simplified Specification may require a license from the SD Card
36 * Association, SD Group, SD-3C LLC or other third parties.
40 * The information contained in the Simplified Specification is presented only
41 * as a standard specification for SD Cards and SD Host/Ancillary products and
42 * is provided "AS-IS" without any representations or warranties of any
43 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
44 * Card Association for any damages, any infringements of patents or other
45 * right of the SD Group, SD-3C LLC, the SD Card Association or any third
46 * parties, which may result from its use. No license is granted by
47 * implication, estoppel or otherwise under any patent or other rights of the
48 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
49 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
50 * or the SD Card Association to disclose or distribute any technical
51 * information, know-how or other confidential information to any third party.
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/malloc.h>
62 #include <sys/module.h>
63 #include <sys/mutex.h>
65 #include <sys/endian.h>
66 #include <sys/sysctl.h>
69 #include <dev/mmc/bridge.h>
70 #include <dev/mmc/mmc_private.h>
71 #include <dev/mmc/mmc_subr.h>
72 #include <dev/mmc/mmcreg.h>
73 #include <dev/mmc/mmcbrvar.h>
74 #include <dev/mmc/mmcvar.h>
77 #include "mmcbus_if.h"
79 CTASSERT(bus_timing_max <= sizeof(uint32_t) * NBBY);
85 uint32_t raw_cid[4]; /* Raw bits of the CID */
86 uint32_t raw_csd[4]; /* Raw bits of the CSD */
87 uint32_t raw_scr[2]; /* Raw bits of the SCR */
88 uint8_t raw_ext_csd[MMC_EXTCSD_SIZE]; /* Raw bits of the EXT_CSD */
89 uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */
91 enum mmc_card_mode mode;
92 struct mmc_cid cid; /* cid decoded */
93 struct mmc_csd csd; /* csd decoded */
94 struct mmc_scr scr; /* scr decoded */
95 struct mmc_sd_status sd_status; /* SD_STATUS decoded */
96 u_char read_only; /* True when the device is read-only */
97 u_char bus_width; /* Bus width to use */
98 u_char high_cap; /* High Capacity card (block addressed) */
99 uint32_t sec_count; /* Card capacity in 512byte blocks */
100 uint32_t timings; /* Mask of bus timings supported */
101 uint32_t vccq_120; /* Mask of bus timings at VCCQ of 1.2 V */
102 uint32_t vccq_180; /* Mask of bus timings at VCCQ of 1.8 V */
103 uint32_t tran_speed; /* Max speed in normal mode */
104 uint32_t hs_tran_speed; /* Max speed in high speed mode */
105 uint32_t erase_sector; /* Card native erase sector size */
106 uint32_t cmd6_time; /* Generic switch timeout [us] */
107 char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
108 char card_sn_string[16];/* Formatted serial # for disk->d_ident */
111 #define CMD_RETRIES 3
113 static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
115 static int mmc_debug;
116 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &mmc_debug, 0,
119 /* bus entry points */
120 static int mmc_acquire_bus(device_t busdev, device_t dev);
121 static int mmc_attach(device_t dev);
122 static int mmc_child_location_str(device_t dev, device_t child, char *buf,
124 static int mmc_detach(device_t dev);
125 static int mmc_probe(device_t dev);
126 static int mmc_read_ivar(device_t bus, device_t child, int which,
128 static int mmc_release_bus(device_t busdev, device_t dev);
129 static int mmc_resume(device_t dev);
130 static int mmc_suspend(device_t dev);
131 static int mmc_wait_for_request(device_t brdev, device_t reqdev,
132 struct mmc_request *req);
133 static int mmc_write_ivar(device_t bus, device_t child, int which,
136 #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
137 #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
138 #define MMC_LOCK_INIT(_sc) \
139 mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->dev), \
141 #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx);
142 #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED);
143 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED);
145 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
146 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
147 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
148 struct mmc_sd_status *sd_status);
149 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
150 uint32_t *rawsdstatus);
151 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
153 static int mmc_calculate_clock(struct mmc_softc *sc);
154 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid,
156 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
157 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
158 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
159 static void mmc_delayed_attach(void *xsc);
160 static int mmc_delete_cards(struct mmc_softc *sc);
161 static void mmc_discover_cards(struct mmc_softc *sc);
162 static void mmc_format_card_id_string(struct mmc_ivars *ivar);
163 static void mmc_go_discovery(struct mmc_softc *sc);
164 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
166 static int mmc_highest_voltage(uint32_t ocr);
167 static void mmc_idle_cards(struct mmc_softc *sc);
168 static void mmc_ms_delay(int ms);
169 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
170 static void mmc_power_down(struct mmc_softc *sc);
171 static void mmc_power_up(struct mmc_softc *sc);
172 static void mmc_rescan_cards(struct mmc_softc *sc);
173 static void mmc_scan(struct mmc_softc *sc);
174 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
175 uint8_t value, uint8_t *res);
176 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
177 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
178 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
180 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
181 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
182 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
184 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
185 static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
186 static int mmc_set_card_bus_width(struct mmc_softc *sc, struct mmc_ivars *ivar);
187 static int mmc_set_power_class(struct mmc_softc *sc, struct mmc_ivars *ivar);
188 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
189 static int mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar,
190 enum mmc_bus_timing timing);
191 static int mmc_test_bus_width(struct mmc_softc *sc);
192 static uint32_t mmc_timing_to_dtr(struct mmc_ivars *ivar,
193 enum mmc_bus_timing timing);
194 static const char *mmc_timing_to_string(enum mmc_bus_timing timing);
195 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
196 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
197 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
198 static void mmc_wakeup(struct mmc_request *req);
204 DELAY(1000 * ms); /* XXX BAD */
208 mmc_probe(device_t dev)
211 device_set_desc(dev, "MMC/SD bus");
216 mmc_attach(device_t dev)
218 struct mmc_softc *sc;
220 sc = device_get_softc(dev);
224 /* We'll probe and attach our children later, but before / mount */
225 sc->config_intrhook.ich_func = mmc_delayed_attach;
226 sc->config_intrhook.ich_arg = sc;
227 if (config_intrhook_establish(&sc->config_intrhook) != 0)
228 device_printf(dev, "config_intrhook_establish failed\n");
233 mmc_detach(device_t dev)
235 struct mmc_softc *sc = device_get_softc(dev);
238 if ((err = mmc_delete_cards(sc)) != 0)
241 MMC_LOCK_DESTROY(sc);
247 mmc_suspend(device_t dev)
249 struct mmc_softc *sc = device_get_softc(dev);
252 err = bus_generic_suspend(dev);
260 mmc_resume(device_t dev)
262 struct mmc_softc *sc = device_get_softc(dev);
265 return (bus_generic_resume(dev));
269 mmc_acquire_bus(device_t busdev, device_t dev)
271 struct mmc_softc *sc;
272 struct mmc_ivars *ivar;
274 enum mmc_bus_timing timing;
276 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
279 sc = device_get_softc(busdev);
282 panic("mmc: host bridge didn't serialize us.");
288 * Keep track of the last rca that we've selected. If
289 * we're asked to do it again, don't. We never
290 * unselect unless the bus code itself wants the mmc
291 * bus, and constantly reselecting causes problems.
293 ivar = device_get_ivars(dev);
295 if (sc->last_rca != rca) {
296 if (mmc_select_card(sc, rca) != MMC_ERR_NONE) {
297 device_printf(sc->dev, "Card at relative "
298 "address %d failed to select.\n", rca);
302 timing = mmcbr_get_timing(busdev);
303 /* Prepare bus width for the new card. */
304 if (bootverbose || mmc_debug) {
305 device_printf(busdev,
306 "setting bus width to %d bits %s timing\n",
307 (ivar->bus_width == bus_width_4) ? 4 :
308 (ivar->bus_width == bus_width_8) ? 8 : 1,
309 mmc_timing_to_string(timing));
311 if (mmc_set_card_bus_width(sc, ivar) != MMC_ERR_NONE) {
312 device_printf(sc->dev, "Card at relative "
313 "address %d failed to set bus width.\n",
317 if (isset(&ivar->vccq_120, timing))
318 mmcbr_set_vccq(busdev, vccq_120);
319 else if (isset(&ivar->vccq_180, timing))
320 mmcbr_set_vccq(busdev, vccq_180);
322 mmcbr_set_vccq(busdev, vccq_330);
323 if (mmcbr_switch_vccq(busdev) != 0) {
324 device_printf(sc->dev, "Failed to set VCCQ "
325 "for card at relative address %d.\n", rca);
328 if (mmc_set_power_class(sc, ivar) != MMC_ERR_NONE) {
329 device_printf(sc->dev, "Card at relative "
330 "address %d failed to set power class.\n",
334 mmcbr_set_bus_width(busdev, ivar->bus_width);
335 mmcbr_update_ios(busdev);
339 * If there's a card selected, stand down.
341 if (sc->last_rca != 0) {
342 mmc_select_card(sc, 0);
351 mmc_release_bus(device_t busdev, device_t dev)
353 struct mmc_softc *sc;
356 sc = device_get_softc(busdev);
360 panic("mmc: releasing unowned bus.");
361 if (sc->owner != dev)
362 panic("mmc: you don't own the bus. game over.");
364 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
374 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
377 return (ocr & MMC_OCR_VOLTAGE);
381 mmc_highest_voltage(uint32_t ocr)
385 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
386 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
393 mmc_wakeup(struct mmc_request *req)
395 struct mmc_softc *sc;
397 sc = (struct mmc_softc *)req->done_data;
399 req->flags |= MMC_REQ_DONE;
405 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
408 req->done = mmc_wakeup;
411 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
412 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
413 if (req->cmd->data) {
414 printf(" data %d\n", (int)req->cmd->data->len);
418 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
420 while ((req->flags & MMC_REQ_DONE) == 0)
421 msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
423 if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
424 device_printf(sc->dev, "CMD%d RESULT: %d\n",
425 req->cmd->opcode, req->cmd->error);
430 mmc_wait_for_request(device_t brdev, device_t reqdev __unused,
431 struct mmc_request *req)
433 struct mmc_softc *sc = device_get_softc(brdev);
435 return (mmc_wait_for_req(sc, req));
439 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
440 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
442 struct mmc_command cmd;
445 memset(&cmd, 0, sizeof(cmd));
450 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, retries);
454 if (flags & MMC_RSP_136)
455 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
463 mmc_idle_cards(struct mmc_softc *sc)
466 struct mmc_command cmd;
469 mmcbr_set_chip_select(dev, cs_high);
470 mmcbr_update_ios(dev);
473 memset(&cmd, 0, sizeof(cmd));
474 cmd.opcode = MMC_GO_IDLE_STATE;
476 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
478 mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
481 mmcbr_set_chip_select(dev, cs_dontcare);
482 mmcbr_update_ios(dev);
487 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
489 struct mmc_command cmd;
490 int err = MMC_ERR_NONE, i;
492 memset(&cmd, 0, sizeof(cmd));
493 cmd.opcode = ACMD_SD_SEND_OP_COND;
495 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
498 for (i = 0; i < 1000; i++) {
499 err = mmc_wait_for_app_cmd(sc->dev, sc->dev, 0, &cmd,
501 if (err != MMC_ERR_NONE)
503 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
504 (ocr & MMC_OCR_VOLTAGE) == 0)
506 err = MMC_ERR_TIMEOUT;
509 if (rocr && err == MMC_ERR_NONE)
515 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
517 struct mmc_command cmd;
518 int err = MMC_ERR_NONE, i;
520 memset(&cmd, 0, sizeof(cmd));
521 cmd.opcode = MMC_SEND_OP_COND;
523 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
526 for (i = 0; i < 1000; i++) {
527 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
528 if (err != MMC_ERR_NONE)
530 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
531 (ocr & MMC_OCR_VOLTAGE) == 0)
533 err = MMC_ERR_TIMEOUT;
536 if (rocr && err == MMC_ERR_NONE)
542 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
544 struct mmc_command cmd;
547 memset(&cmd, 0, sizeof(cmd));
548 cmd.opcode = SD_SEND_IF_COND;
549 cmd.arg = (vhs << 8) + 0xAA;
550 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
553 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
558 mmc_power_up(struct mmc_softc *sc)
564 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
565 mmcbr_set_bus_mode(dev, opendrain);
566 mmcbr_set_chip_select(dev, cs_dontcare);
567 mmcbr_set_bus_width(dev, bus_width_1);
568 mmcbr_set_power_mode(dev, power_up);
569 mmcbr_set_clock(dev, 0);
570 mmcbr_update_ios(dev);
571 for (vccq = vccq_330; ; vccq--) {
572 mmcbr_set_vccq(dev, vccq);
573 if (mmcbr_switch_vccq(dev) == 0 || vccq == vccq_120)
578 mmcbr_set_clock(dev, SD_MMC_CARD_ID_FREQUENCY);
579 mmcbr_set_timing(dev, bus_timing_normal);
580 mmcbr_set_power_mode(dev, power_on);
581 mmcbr_update_ios(dev);
586 mmc_power_down(struct mmc_softc *sc)
588 device_t dev = sc->dev;
590 mmcbr_set_bus_mode(dev, opendrain);
591 mmcbr_set_chip_select(dev, cs_dontcare);
592 mmcbr_set_bus_width(dev, bus_width_1);
593 mmcbr_set_power_mode(dev, power_off);
594 mmcbr_set_clock(dev, 0);
595 mmcbr_set_timing(dev, bus_timing_normal);
596 mmcbr_update_ios(dev);
600 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
604 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
605 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
606 flags, NULL, CMD_RETRIES));
610 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
614 struct mmc_command cmd;
615 struct mmc_data data;
617 memset(&cmd, 0, sizeof(cmd));
618 memset(&data, 0, sizeof(data));
621 cmd.opcode = SD_SWITCH_FUNC;
622 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
623 cmd.arg = mode << 31; /* 0 - check, 1 - set */
624 cmd.arg |= 0x00FFFFFF;
625 cmd.arg &= ~(0xF << (grp * 4));
626 cmd.arg |= value << (grp * 4);
631 data.flags = MMC_DATA_READ;
633 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
638 mmc_set_card_bus_width(struct mmc_softc *sc, struct mmc_ivars *ivar)
640 struct mmc_command cmd;
644 if (mmcbr_get_mode(sc->dev) == mode_sd) {
645 memset(&cmd, 0, sizeof(cmd));
646 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
647 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
648 cmd.arg = SD_CLR_CARD_DETECT;
649 err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd,
653 memset(&cmd, 0, sizeof(cmd));
654 cmd.opcode = ACMD_SET_BUS_WIDTH;
655 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
656 switch (ivar->bus_width) {
658 cmd.arg = SD_BUS_WIDTH_1;
661 cmd.arg = SD_BUS_WIDTH_4;
664 return (MMC_ERR_INVALID);
666 err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd,
669 switch (ivar->bus_width) {
671 value = EXT_CSD_BUS_WIDTH_1;
674 switch (mmcbr_get_timing(sc->dev)) {
675 case bus_timing_mmc_ddr52:
676 case bus_timing_mmc_hs200:
677 case bus_timing_mmc_hs400:
678 case bus_timing_mmc_hs400es:
679 value = EXT_CSD_BUS_WIDTH_4_DDR;
682 value = EXT_CSD_BUS_WIDTH_4;
687 switch (mmcbr_get_timing(sc->dev)) {
688 case bus_timing_mmc_ddr52:
689 case bus_timing_mmc_hs200:
690 case bus_timing_mmc_hs400:
691 case bus_timing_mmc_hs400es:
692 value = EXT_CSD_BUS_WIDTH_8_DDR;
695 value = EXT_CSD_BUS_WIDTH_8;
700 return (MMC_ERR_INVALID);
702 err = mmc_switch(sc->dev, sc->dev, ivar->rca,
703 EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, value,
704 ivar->cmd6_time, true);
710 mmc_set_power_class(struct mmc_softc *sc, struct mmc_ivars *ivar)
713 const uint8_t *ext_csd;
718 if (mmcbr_get_mode(dev) != mode_mmc || ivar->csd.spec_vers < 4)
719 return (MMC_ERR_NONE);
722 ext_csd = ivar->raw_ext_csd;
723 clock = mmcbr_get_clock(dev);
724 switch (1 << mmcbr_get_vdd(dev)) {
725 case MMC_OCR_LOW_VOLTAGE:
726 if (clock <= MMC_TYPE_HS_26_MAX)
727 value = ext_csd[EXT_CSD_PWR_CL_26_195];
728 else if (clock <= MMC_TYPE_HS_52_MAX) {
729 if (mmcbr_get_timing(dev) >= bus_timing_mmc_ddr52 &&
730 ivar->bus_width >= bus_width_4)
731 value = ext_csd[EXT_CSD_PWR_CL_52_195_DDR];
733 value = ext_csd[EXT_CSD_PWR_CL_52_195];
734 } else if (clock <= MMC_TYPE_HS200_HS400ES_MAX)
735 value = ext_csd[EXT_CSD_PWR_CL_200_195];
737 case MMC_OCR_270_280:
738 case MMC_OCR_280_290:
739 case MMC_OCR_290_300:
740 case MMC_OCR_300_310:
741 case MMC_OCR_310_320:
742 case MMC_OCR_320_330:
743 case MMC_OCR_330_340:
744 case MMC_OCR_340_350:
745 case MMC_OCR_350_360:
746 if (clock <= MMC_TYPE_HS_26_MAX)
747 value = ext_csd[EXT_CSD_PWR_CL_26_360];
748 else if (clock <= MMC_TYPE_HS_52_MAX) {
749 if (mmcbr_get_timing(dev) == bus_timing_mmc_ddr52 &&
750 ivar->bus_width >= bus_width_4)
751 value = ext_csd[EXT_CSD_PWR_CL_52_360_DDR];
753 value = ext_csd[EXT_CSD_PWR_CL_52_360];
754 } else if (clock <= MMC_TYPE_HS200_HS400ES_MAX) {
755 if (ivar->bus_width == bus_width_8)
756 value = ext_csd[EXT_CSD_PWR_CL_200_360_DDR];
758 value = ext_csd[EXT_CSD_PWR_CL_200_360];
762 device_printf(dev, "No power class support for VDD 0x%x\n",
763 1 << mmcbr_get_vdd(dev));
764 return (MMC_ERR_INVALID);
767 if (ivar->bus_width == bus_width_8)
768 value = (value & EXT_CSD_POWER_CLASS_8BIT_MASK) >>
769 EXT_CSD_POWER_CLASS_8BIT_SHIFT;
771 value = (value & EXT_CSD_POWER_CLASS_4BIT_MASK) >>
772 EXT_CSD_POWER_CLASS_4BIT_SHIFT;
775 return (MMC_ERR_NONE);
777 return (mmc_switch(dev, dev, ivar->rca, EXT_CSD_CMD_SET_NORMAL,
778 EXT_CSD_POWER_CLASS, value, ivar->cmd6_time, true));
782 mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar,
783 enum mmc_bus_timing timing)
785 u_char switch_res[64];
789 if (mmcbr_get_mode(sc->dev) == mode_sd) {
791 case bus_timing_normal:
792 value = SD_SWITCH_NORMAL_MODE;
795 value = SD_SWITCH_HS_MODE;
798 return (MMC_ERR_INVALID);
800 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
802 if (err != MMC_ERR_NONE)
804 if ((switch_res[16] & 0xf) != value)
805 return (MMC_ERR_FAILED);
806 mmcbr_set_timing(sc->dev, timing);
807 mmcbr_update_ios(sc->dev);
810 case bus_timing_normal:
811 value = EXT_CSD_HS_TIMING_BC;
814 case bus_timing_mmc_ddr52:
815 value = EXT_CSD_HS_TIMING_HS;
818 return (MMC_ERR_INVALID);
820 err = mmc_switch(sc->dev, sc->dev, ivar->rca,
821 EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, value,
822 ivar->cmd6_time, false);
823 if (err != MMC_ERR_NONE)
825 mmcbr_set_timing(sc->dev, timing);
826 mmcbr_update_ios(sc->dev);
827 err = mmc_switch_status(sc->dev, sc->dev, ivar->rca,
833 static const uint8_t p8[8] = {
834 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
837 static const uint8_t p8ok[8] = {
838 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
841 static const uint8_t p4[4] = {
842 0x5A, 0x00, 0x00, 0x00
845 static const uint8_t p4ok[4] = {
846 0xA5, 0x00, 0x00, 0x00
850 mmc_test_bus_width(struct mmc_softc *sc)
852 struct mmc_command cmd;
853 struct mmc_data data;
857 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
858 mmcbr_set_bus_width(sc->dev, bus_width_8);
859 mmcbr_update_ios(sc->dev);
861 sc->squelched++; /* Errors are expected, squelch reporting. */
862 memset(&cmd, 0, sizeof(cmd));
863 memset(&data, 0, sizeof(data));
864 cmd.opcode = MMC_BUSTEST_W;
866 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
869 data.data = __DECONST(void *, p8);
871 data.flags = MMC_DATA_WRITE;
872 mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
874 memset(&cmd, 0, sizeof(cmd));
875 memset(&data, 0, sizeof(data));
876 cmd.opcode = MMC_BUSTEST_R;
878 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
883 data.flags = MMC_DATA_READ;
884 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
887 mmcbr_set_bus_width(sc->dev, bus_width_1);
888 mmcbr_update_ios(sc->dev);
890 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
891 return (bus_width_8);
894 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
895 mmcbr_set_bus_width(sc->dev, bus_width_4);
896 mmcbr_update_ios(sc->dev);
898 sc->squelched++; /* Errors are expected, squelch reporting. */
899 memset(&cmd, 0, sizeof(cmd));
900 memset(&data, 0, sizeof(data));
901 cmd.opcode = MMC_BUSTEST_W;
903 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
906 data.data = __DECONST(void *, p4);
908 data.flags = MMC_DATA_WRITE;
909 mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
911 memset(&cmd, 0, sizeof(cmd));
912 memset(&data, 0, sizeof(data));
913 cmd.opcode = MMC_BUSTEST_R;
915 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
920 data.flags = MMC_DATA_READ;
921 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
924 mmcbr_set_bus_width(sc->dev, bus_width_1);
925 mmcbr_update_ios(sc->dev);
927 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
928 return (bus_width_4);
930 return (bus_width_1);
934 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
936 const int i = (bit_len / 32) - (start / 32) - 1;
937 const int shift = start & 31;
938 uint32_t retval = bits[i] >> shift;
940 if (size + shift > 32)
941 retval |= bits[i - 1] << (32 - shift);
942 return (retval & ((1llu << size) - 1));
946 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
950 /* There's no version info, so we take it on faith */
951 memset(cid, 0, sizeof(*cid));
952 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
953 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
954 for (i = 0; i < 5; i++)
955 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
957 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
958 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
959 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
960 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
964 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid, bool is_4_41p)
968 /* There's no version info, so we take it on faith */
969 memset(cid, 0, sizeof(*cid));
970 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
971 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
972 for (i = 0; i < 6; i++)
973 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
975 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
976 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
977 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
978 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4);
980 cid->mdt_year += 2013;
982 cid->mdt_year += 1997;
986 mmc_format_card_id_string(struct mmc_ivars *ivar)
993 * Format a card ID string for use by the mmcsd driver, it's what
994 * appears between the <> in the following:
995 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
996 * 22.5MHz/4bit/128-block
998 * Also format just the card serial number, which the mmcsd driver will
999 * use as the disk->d_ident string.
1001 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
1002 * and our max formatted length is currently 55 bytes if every field
1003 * contains the largest value.
1005 * Sometimes the oid is two printable ascii chars; when it's not,
1006 * format it as 0xnnnn instead.
1008 c1 = (ivar->cid.oid >> 8) & 0x0ff;
1009 c2 = ivar->cid.oid & 0x0ff;
1010 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
1011 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
1013 snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
1014 snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
1015 "%08X", ivar->cid.psn);
1016 snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
1017 "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
1018 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
1019 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
1020 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
1021 ivar->cid.mid, oidstr);
1024 static const int exp[8] = {
1025 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
1028 static const int mant[16] = {
1029 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
1032 static const int cur_min[8] = {
1033 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
1036 static const int cur_max[8] = {
1037 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
1041 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
1047 memset(csd, 0, sizeof(*csd));
1048 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
1050 m = mmc_get_bits(raw_csd, 128, 115, 4);
1051 e = mmc_get_bits(raw_csd, 128, 112, 3);
1052 csd->tacc = (exp[e] * mant[m] + 9) / 10;
1053 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1054 m = mmc_get_bits(raw_csd, 128, 99, 4);
1055 e = mmc_get_bits(raw_csd, 128, 96, 3);
1056 csd->tran_speed = exp[e] * 10000 * mant[m];
1057 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1058 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1059 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1060 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1061 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1062 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1063 csd->vdd_r_curr_min =
1064 cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1065 csd->vdd_r_curr_max =
1066 cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1067 csd->vdd_w_curr_min =
1068 cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1069 csd->vdd_w_curr_max =
1070 cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1071 m = mmc_get_bits(raw_csd, 128, 62, 12);
1072 e = mmc_get_bits(raw_csd, 128, 47, 3);
1073 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1074 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1075 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1076 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1077 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1078 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1079 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1080 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1081 } else if (v == 1) {
1082 m = mmc_get_bits(raw_csd, 128, 115, 4);
1083 e = mmc_get_bits(raw_csd, 128, 112, 3);
1084 csd->tacc = (exp[e] * mant[m] + 9) / 10;
1085 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1086 m = mmc_get_bits(raw_csd, 128, 99, 4);
1087 e = mmc_get_bits(raw_csd, 128, 96, 3);
1088 csd->tran_speed = exp[e] * 10000 * mant[m];
1089 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1090 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1091 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1092 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1093 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1094 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1095 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) +
1097 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1098 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1099 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1100 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1101 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1102 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1103 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1105 panic("unknown SD CSD version");
1109 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
1114 memset(csd, 0, sizeof(*csd));
1115 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
1116 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1117 m = mmc_get_bits(raw_csd, 128, 115, 4);
1118 e = mmc_get_bits(raw_csd, 128, 112, 3);
1119 csd->tacc = exp[e] * mant[m] + 9 / 10;
1120 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1121 m = mmc_get_bits(raw_csd, 128, 99, 4);
1122 e = mmc_get_bits(raw_csd, 128, 96, 3);
1123 csd->tran_speed = exp[e] * 10000 * mant[m];
1124 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1125 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1126 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1127 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1128 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1129 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1130 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1131 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1132 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1133 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1134 m = mmc_get_bits(raw_csd, 128, 62, 12);
1135 e = mmc_get_bits(raw_csd, 128, 47, 3);
1136 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1137 csd->erase_blk_en = 0;
1138 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1139 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1140 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1141 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1142 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1143 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1144 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1148 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1150 unsigned int scr_struct;
1152 memset(scr, 0, sizeof(*scr));
1154 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1155 if (scr_struct != 0) {
1156 printf("Unrecognised SCR structure version %d\n",
1160 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1161 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1165 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1166 struct mmc_sd_status *sd_status)
1169 memset(sd_status, 0, sizeof(*sd_status));
1171 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1172 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1173 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1174 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1175 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1176 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1177 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1178 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1179 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1180 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1184 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1186 struct mmc_command cmd;
1189 memset(&cmd, 0, sizeof(cmd));
1190 cmd.opcode = MMC_ALL_SEND_CID;
1192 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1194 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1195 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1200 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1202 struct mmc_command cmd;
1205 memset(&cmd, 0, sizeof(cmd));
1206 cmd.opcode = MMC_SEND_CSD;
1207 cmd.arg = rca << 16;
1208 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1210 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1211 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1216 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1219 struct mmc_command cmd;
1220 struct mmc_data data;
1222 memset(&cmd, 0, sizeof(cmd));
1223 memset(&data, 0, sizeof(data));
1225 memset(rawscr, 0, 8);
1226 cmd.opcode = ACMD_SEND_SCR;
1227 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1233 data.flags = MMC_DATA_READ;
1235 err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES);
1236 rawscr[0] = be32toh(rawscr[0]);
1237 rawscr[1] = be32toh(rawscr[1]);
1242 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1244 struct mmc_command cmd;
1245 struct mmc_data data;
1248 memset(&cmd, 0, sizeof(cmd));
1249 memset(&data, 0, sizeof(data));
1251 memset(rawsdstatus, 0, 64);
1252 cmd.opcode = ACMD_SD_STATUS;
1253 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1257 data.data = rawsdstatus;
1259 data.flags = MMC_DATA_READ;
1261 err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES);
1262 for (i = 0; i < 16; i++)
1263 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1268 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1270 struct mmc_command cmd;
1273 memset(&cmd, 0, sizeof(cmd));
1274 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1275 cmd.arg = resp << 16;
1276 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1278 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1283 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1285 struct mmc_command cmd;
1288 memset(&cmd, 0, sizeof(cmd));
1289 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1291 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1293 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1294 *resp = cmd.resp[0];
1299 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1301 struct mmc_command cmd;
1304 memset(&cmd, 0, sizeof(cmd));
1305 cmd.opcode = MMC_SET_BLOCKLEN;
1307 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1309 err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1314 mmc_timing_to_dtr(struct mmc_ivars *ivar, enum mmc_bus_timing timing)
1318 case bus_timing_normal:
1319 return (ivar->tran_speed);
1321 return (ivar->hs_tran_speed);
1322 case bus_timing_uhs_sdr12:
1323 return (SD_SDR12_MAX);
1324 case bus_timing_uhs_sdr25:
1325 return (SD_SDR25_MAX);
1326 case bus_timing_uhs_ddr50:
1327 return (SD_DDR50_MAX);
1328 case bus_timing_uhs_sdr50:
1329 return (SD_SDR50_MAX);
1330 case bus_timing_uhs_sdr104:
1331 return (SD_SDR104_MAX);
1332 case bus_timing_mmc_ddr52:
1333 return (MMC_TYPE_DDR52_MAX);
1334 case bus_timing_mmc_hs200:
1335 case bus_timing_mmc_hs400:
1336 case bus_timing_mmc_hs400es:
1337 return (MMC_TYPE_HS200_HS400ES_MAX);
1343 mmc_timing_to_string(enum mmc_bus_timing timing)
1347 case bus_timing_normal:
1348 return ("normal speed");
1350 return ("high speed");
1351 case bus_timing_uhs_sdr12:
1352 case bus_timing_uhs_sdr25:
1353 case bus_timing_uhs_sdr50:
1354 case bus_timing_uhs_sdr104:
1355 return ("single data rate");
1356 case bus_timing_uhs_ddr50:
1357 case bus_timing_mmc_ddr52:
1358 return ("dual data rate");
1359 case bus_timing_mmc_hs200:
1361 case bus_timing_mmc_hs400:
1363 case bus_timing_mmc_hs400es:
1364 return ("HS400 with enhanced strobe");
1370 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1372 enum mmc_bus_timing max_timing, timing;
1374 device_printf(dev, "Card at relative address 0x%04x%s:\n",
1375 ivar->rca, newcard ? " added" : "");
1376 device_printf(dev, " card: %s\n", ivar->card_id_string);
1377 max_timing = bus_timing_normal;
1378 for (timing = bus_timing_max; timing > bus_timing_normal; timing--) {
1379 if (isset(&ivar->timings, timing)) {
1380 max_timing = timing;
1384 device_printf(dev, " bus: %ubit, %uMHz (%s timing)\n",
1385 (ivar->bus_width == bus_width_1 ? 1 :
1386 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1387 mmc_timing_to_dtr(ivar, timing) / 1000000,
1388 mmc_timing_to_string(timing));
1389 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1390 ivar->sec_count, ivar->erase_sector,
1391 ivar->read_only ? ", read-only" : "");
1395 mmc_discover_cards(struct mmc_softc *sc)
1397 u_char switch_res[64];
1398 uint32_t raw_cid[4];
1399 struct mmc_ivars *ivar = NULL;
1402 int devcount, err, host_caps, i, newcard;
1403 uint32_t resp, sec_count, status;
1406 host_caps = mmcbr_get_caps(sc->dev);
1407 if (bootverbose || mmc_debug)
1408 device_printf(sc->dev, "Probing cards\n");
1410 sc->squelched++; /* Errors are expected, squelch reporting. */
1411 err = mmc_all_send_cid(sc, raw_cid);
1413 if (err == MMC_ERR_TIMEOUT)
1415 if (err != MMC_ERR_NONE) {
1416 device_printf(sc->dev, "Error reading CID %d\n", err);
1420 if ((err = device_get_children(sc->dev, &devlist,
1423 for (i = 0; i < devcount; i++) {
1424 ivar = device_get_ivars(devlist[i]);
1425 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) ==
1431 free(devlist, M_TEMP);
1432 if (bootverbose || mmc_debug) {
1433 device_printf(sc->dev,
1434 "%sard detected (CID %08x%08x%08x%08x)\n",
1435 newcard ? "New c" : "C",
1436 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1439 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1441 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1443 if (mmcbr_get_ro(sc->dev))
1444 ivar->read_only = 1;
1445 ivar->bus_width = bus_width_1;
1446 setbit(&ivar->timings, bus_timing_normal);
1447 ivar->mode = mmcbr_get_mode(sc->dev);
1448 if (ivar->mode == mode_sd) {
1449 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1450 err = mmc_send_relative_addr(sc, &resp);
1451 if (err != MMC_ERR_NONE) {
1452 device_printf(sc->dev,
1453 "Error getting RCA %d\n", err);
1456 ivar->rca = resp >> 16;
1458 err = mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1459 if (err != MMC_ERR_NONE) {
1460 device_printf(sc->dev,
1461 "Error getting CSD %d\n", err);
1464 if (bootverbose || mmc_debug)
1465 device_printf(sc->dev,
1466 "%sard detected (CSD %08x%08x%08x%08x)\n",
1467 newcard ? "New c" : "C", ivar->raw_csd[0],
1468 ivar->raw_csd[1], ivar->raw_csd[2],
1470 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1471 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1472 if (ivar->csd.csd_structure > 0)
1474 ivar->tran_speed = ivar->csd.tran_speed;
1475 ivar->erase_sector = ivar->csd.erase_sector *
1476 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1478 err = mmc_send_status(sc->dev, sc->dev, ivar->rca,
1480 if (err != MMC_ERR_NONE) {
1481 device_printf(sc->dev,
1482 "Error reading card status %d\n", err);
1485 if ((status & R1_CARD_IS_LOCKED) != 0) {
1486 device_printf(sc->dev,
1487 "Card is password protected, skipping.\n");
1491 /* Get card SCR. Card must be selected to fetch it. */
1492 err = mmc_select_card(sc, ivar->rca);
1493 if (err != MMC_ERR_NONE) {
1494 device_printf(sc->dev,
1495 "Error selecting card %d\n", err);
1498 err = mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1499 if (err != MMC_ERR_NONE) {
1500 device_printf(sc->dev,
1501 "Error reading SCR %d\n", err);
1504 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1505 /* Get card switch capabilities (command class 10). */
1506 if ((ivar->scr.sda_vsn >= 1) &&
1507 (ivar->csd.ccc & (1 << 10))) {
1508 err = mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1509 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1511 if (err == MMC_ERR_NONE &&
1512 switch_res[13] & (1 << SD_SWITCH_HS_MODE)) {
1513 setbit(&ivar->timings, bus_timing_hs);
1514 ivar->hs_tran_speed = SD_HS_MAX;
1519 * We deselect then reselect the card here. Some cards
1520 * become unselected and timeout with the above two
1521 * commands, although the state tables / diagrams in the
1522 * standard suggest they go back to the transfer state.
1523 * Other cards don't become deselected, and if we
1524 * attempt to blindly re-select them, we get timeout
1525 * errors from some controllers. So we deselect then
1526 * reselect to handle all situations. The only thing we
1527 * use from the sd_status is the erase sector size, but
1528 * it is still nice to get that right.
1530 mmc_select_card(sc, 0);
1531 (void)mmc_select_card(sc, ivar->rca);
1532 (void)mmc_app_sd_status(sc, ivar->rca,
1533 ivar->raw_sd_status);
1534 mmc_app_decode_sd_status(ivar->raw_sd_status,
1536 if (ivar->sd_status.au_size != 0) {
1537 ivar->erase_sector =
1538 16 << ivar->sd_status.au_size;
1540 /* Find max supported bus width. */
1541 if ((host_caps & MMC_CAP_4_BIT_DATA) &&
1542 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1543 ivar->bus_width = bus_width_4;
1546 * Some cards that report maximum I/O block sizes
1547 * greater than 512 require the block length to be
1548 * set to 512, even though that is supposed to be
1549 * the default. Example:
1551 * Transcend 2GB SDSC card, CID:
1552 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1554 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1555 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1556 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1558 mmc_format_card_id_string(ivar);
1560 if (bootverbose || mmc_debug)
1561 mmc_log_card(sc->dev, ivar, newcard);
1564 child = device_add_child(sc->dev, NULL, -1);
1565 device_set_ivars(child, ivar);
1567 mmc_select_card(sc, 0);
1571 err = mmc_set_relative_addr(sc, ivar->rca);
1572 if (err != MMC_ERR_NONE) {
1573 device_printf(sc->dev, "Error setting RCA %d\n", err);
1577 err = mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1578 if (err != MMC_ERR_NONE) {
1579 device_printf(sc->dev, "Error getting CSD %d\n", err);
1582 if (bootverbose || mmc_debug)
1583 device_printf(sc->dev,
1584 "%sard detected (CSD %08x%08x%08x%08x)\n",
1585 newcard ? "New c" : "C", ivar->raw_csd[0],
1586 ivar->raw_csd[1], ivar->raw_csd[2],
1589 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1590 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1591 ivar->tran_speed = ivar->csd.tran_speed;
1592 ivar->erase_sector = ivar->csd.erase_sector *
1593 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1595 err = mmc_send_status(sc->dev, sc->dev, ivar->rca, &status);
1596 if (err != MMC_ERR_NONE) {
1597 device_printf(sc->dev,
1598 "Error reading card status %d\n", err);
1601 if ((status & R1_CARD_IS_LOCKED) != 0) {
1602 device_printf(sc->dev,
1603 "Card is password protected, skipping.\n");
1607 err = mmc_select_card(sc, ivar->rca);
1608 if (err != MMC_ERR_NONE) {
1609 device_printf(sc->dev, "Error selecting card %d\n",
1614 /* Only MMC >= 4.x devices support EXT_CSD. */
1615 if (ivar->csd.spec_vers >= 4) {
1616 err = mmc_send_ext_csd(sc->dev, sc->dev,
1618 if (err != MMC_ERR_NONE) {
1619 device_printf(sc->dev,
1620 "Error reading EXT_CSD %d\n", err);
1623 /* Handle extended capacity from EXT_CSD */
1624 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1625 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1626 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1627 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1628 if (sec_count != 0) {
1629 ivar->sec_count = sec_count;
1632 /* Get device speeds beyond normal mode. */
1633 if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] &
1634 EXT_CSD_CARD_TYPE_HS_52) != 0) {
1635 setbit(&ivar->timings, bus_timing_hs);
1636 ivar->hs_tran_speed = MMC_TYPE_HS_52_MAX;
1637 } else if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] &
1638 EXT_CSD_CARD_TYPE_HS_26) != 0) {
1639 setbit(&ivar->timings, bus_timing_hs);
1640 ivar->hs_tran_speed = MMC_TYPE_HS_26_MAX;
1642 if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] &
1643 EXT_CSD_CARD_TYPE_DDR_52_1_2V) != 0 &&
1644 (host_caps & MMC_CAP_SIGNALING_120) != 0) {
1645 setbit(&ivar->timings, bus_timing_mmc_ddr52);
1646 setbit(&ivar->vccq_120, bus_timing_mmc_ddr52);
1648 if ((ivar->raw_ext_csd[EXT_CSD_CARD_TYPE] &
1649 EXT_CSD_CARD_TYPE_DDR_52_1_8V) != 0 &&
1650 (host_caps & MMC_CAP_SIGNALING_180) != 0) {
1651 setbit(&ivar->timings, bus_timing_mmc_ddr52);
1652 setbit(&ivar->vccq_180, bus_timing_mmc_ddr52);
1655 * Determine generic switch timeout (provided in
1656 * units of 10 ms), defaulting to 500 ms.
1658 ivar->cmd6_time = 500 * 1000;
1659 if (ivar->csd.spec_vers >= 6)
1660 ivar->cmd6_time = 10 *
1661 ivar->raw_ext_csd[EXT_CSD_GEN_CMD6_TIME];
1662 /* Find max supported bus width. */
1663 ivar->bus_width = mmc_test_bus_width(sc);
1664 /* Handle HC erase sector size. */
1665 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1666 ivar->erase_sector = 1024 *
1667 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1668 err = mmc_switch(sc->dev, sc->dev, ivar->rca,
1669 EXT_CSD_CMD_SET_NORMAL,
1670 EXT_CSD_ERASE_GRP_DEF,
1671 EXT_CSD_ERASE_GRP_DEF_EN,
1672 ivar->cmd6_time, true);
1673 if (err != MMC_ERR_NONE) {
1674 device_printf(sc->dev,
1675 "Error setting erase group %d\n",
1683 * Some cards that report maximum I/O block sizes greater
1684 * than 512 require the block length to be set to 512, even
1685 * though that is supposed to be the default. Example:
1687 * Transcend 2GB SDSC card, CID:
1688 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1690 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1691 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1692 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1694 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid,
1695 ivar->raw_ext_csd[EXT_CSD_REV] >= 5);
1696 mmc_format_card_id_string(ivar);
1698 if (bootverbose || mmc_debug)
1699 mmc_log_card(sc->dev, ivar, newcard);
1702 child = device_add_child(sc->dev, NULL, -1);
1703 device_set_ivars(child, ivar);
1705 mmc_select_card(sc, 0);
1710 mmc_rescan_cards(struct mmc_softc *sc)
1712 struct mmc_ivars *ivar;
1714 int err, i, devcount;
1716 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1718 for (i = 0; i < devcount; i++) {
1719 ivar = device_get_ivars(devlist[i]);
1720 if (mmc_select_card(sc, ivar->rca) != MMC_ERR_NONE) {
1721 if (bootverbose || mmc_debug)
1722 device_printf(sc->dev,
1723 "Card at relative address %d lost.\n",
1725 device_delete_child(sc->dev, devlist[i]);
1726 free(ivar, M_DEVBUF);
1729 free(devlist, M_TEMP);
1730 mmc_select_card(sc, 0);
1734 mmc_delete_cards(struct mmc_softc *sc)
1736 struct mmc_ivars *ivar;
1738 int err, i, devcount;
1740 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1742 for (i = 0; i < devcount; i++) {
1743 ivar = device_get_ivars(devlist[i]);
1744 if (bootverbose || mmc_debug)
1745 device_printf(sc->dev,
1746 "Card at relative address %d deleted.\n",
1748 device_delete_child(sc->dev, devlist[i]);
1749 free(ivar, M_DEVBUF);
1751 free(devlist, M_TEMP);
1756 mmc_go_discovery(struct mmc_softc *sc)
1763 if (mmcbr_get_power_mode(dev) != power_on) {
1765 * First, try SD modes
1767 sc->squelched++; /* Errors are expected, squelch reporting. */
1768 mmcbr_set_mode(dev, mode_sd);
1770 mmcbr_set_bus_mode(dev, pushpull);
1771 if (bootverbose || mmc_debug)
1772 device_printf(sc->dev, "Probing bus\n");
1774 err = mmc_send_if_cond(sc, 1);
1775 if ((bootverbose || mmc_debug) && err == 0)
1776 device_printf(sc->dev,
1777 "SD 2.0 interface conditions: OK\n");
1778 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1779 if (bootverbose || mmc_debug)
1780 device_printf(sc->dev, "SD probe: failed\n");
1784 mmcbr_set_mode(dev, mode_mmc);
1785 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1786 if (bootverbose || mmc_debug)
1787 device_printf(sc->dev,
1788 "MMC probe: failed\n");
1789 ocr = 0; /* Failed both, powerdown. */
1790 } else if (bootverbose || mmc_debug)
1791 device_printf(sc->dev,
1792 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1793 } else if (bootverbose || mmc_debug)
1794 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n",
1798 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1799 if (mmcbr_get_ocr(dev) != 0)
1802 mmcbr_set_bus_mode(dev, opendrain);
1803 mmcbr_set_clock(dev, SD_MMC_CARD_ID_FREQUENCY);
1804 mmcbr_update_ios(dev);
1805 /* XXX recompute vdd based on new cards? */
1808 * Make sure that we have a mutually agreeable voltage to at least
1809 * one card on the bus.
1811 if (bootverbose || mmc_debug)
1812 device_printf(sc->dev, "Current OCR: 0x%08x\n",
1813 mmcbr_get_ocr(dev));
1814 if (mmcbr_get_ocr(dev) == 0) {
1815 device_printf(sc->dev, "No compatible cards found on bus\n");
1816 mmc_delete_cards(sc);
1821 * Reselect the cards after we've idled them above.
1823 if (mmcbr_get_mode(dev) == mode_sd) {
1824 err = mmc_send_if_cond(sc, 1);
1825 mmc_send_app_op_cond(sc,
1826 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1828 mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1829 mmc_discover_cards(sc);
1830 mmc_rescan_cards(sc);
1832 mmcbr_set_bus_mode(dev, pushpull);
1833 mmcbr_update_ios(dev);
1834 mmc_calculate_clock(sc);
1838 mmc_calculate_clock(struct mmc_softc *sc)
1841 struct mmc_ivars *ivar;
1842 int host_caps, i, nkid;
1843 uint32_t dtr, max_dtr;
1844 enum mmc_bus_timing max_timing, timing;
1847 max_dtr = mmcbr_get_f_max(sc->dev);
1848 host_caps = mmcbr_get_caps(sc->dev);
1849 if ((host_caps & MMC_CAP_MMC_DDR52) != 0)
1850 max_timing = bus_timing_mmc_ddr52;
1851 else if ((host_caps & MMC_CAP_HSPEED) != 0)
1852 max_timing = bus_timing_hs;
1854 max_timing = bus_timing_normal;
1855 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1856 panic("can't get children");
1859 for (i = 0; i < nkid; i++) {
1860 ivar = device_get_ivars(kids[i]);
1861 if (isclr(&ivar->timings, max_timing)) {
1862 for (timing = max_timing; timing >=
1863 bus_timing_normal; timing--) {
1864 if (isset(&ivar->timings, timing)) {
1865 max_timing = timing;
1871 dtr = mmc_timing_to_dtr(ivar, max_timing);
1872 if (dtr < max_dtr) {
1877 } while (changed == true);
1878 if (bootverbose || mmc_debug) {
1879 device_printf(sc->dev,
1880 "setting transfer rate to %d.%03dMHz (%s timing)\n",
1881 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1882 mmc_timing_to_string(max_timing));
1884 for (i = 0; i < nkid; i++) {
1885 ivar = device_get_ivars(kids[i]);
1886 if ((ivar->timings & ~(1 << bus_timing_normal)) == 0)
1888 if (mmc_select_card(sc, ivar->rca) != MMC_ERR_NONE ||
1889 mmc_set_timing(sc, ivar, max_timing) != MMC_ERR_NONE)
1890 device_printf(sc->dev, "Card at relative address %d "
1891 "failed to set timing.\n", ivar->rca);
1893 mmc_select_card(sc, 0);
1895 mmcbr_set_clock(sc->dev, max_dtr);
1896 mmcbr_update_ios(sc->dev);
1901 mmc_scan(struct mmc_softc *sc)
1903 device_t dev = sc->dev;
1905 mmc_acquire_bus(dev, dev);
1906 mmc_go_discovery(sc);
1907 mmc_release_bus(dev, dev);
1909 bus_generic_attach(dev);
1913 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1915 struct mmc_ivars *ivar = device_get_ivars(child);
1920 case MMC_IVAR_SPEC_VERS:
1921 *result = ivar->csd.spec_vers;
1923 case MMC_IVAR_DSR_IMP:
1924 *result = ivar->csd.dsr_imp;
1926 case MMC_IVAR_MEDIA_SIZE:
1927 *result = ivar->sec_count;
1930 *result = ivar->rca;
1932 case MMC_IVAR_SECTOR_SIZE:
1933 *result = MMC_SECTOR_SIZE;
1935 case MMC_IVAR_TRAN_SPEED:
1936 *result = mmcbr_get_clock(bus);
1938 case MMC_IVAR_READ_ONLY:
1939 *result = ivar->read_only;
1941 case MMC_IVAR_HIGH_CAP:
1942 *result = ivar->high_cap;
1944 case MMC_IVAR_CARD_TYPE:
1945 *result = ivar->mode;
1947 case MMC_IVAR_BUS_WIDTH:
1948 *result = ivar->bus_width;
1950 case MMC_IVAR_ERASE_SECTOR:
1951 *result = ivar->erase_sector;
1953 case MMC_IVAR_MAX_DATA:
1954 *result = mmcbr_get_max_data(bus);
1956 case MMC_IVAR_CARD_ID_STRING:
1957 *(char **)result = ivar->card_id_string;
1959 case MMC_IVAR_CARD_SN_STRING:
1960 *(char **)result = ivar->card_sn_string;
1967 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1971 * None are writable ATM
1977 mmc_delayed_attach(void *xsc)
1979 struct mmc_softc *sc = xsc;
1982 config_intrhook_disestablish(&sc->config_intrhook);
1986 mmc_child_location_str(device_t dev, device_t child, char *buf,
1990 snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1994 static device_method_t mmc_methods[] = {
1996 DEVMETHOD(device_probe, mmc_probe),
1997 DEVMETHOD(device_attach, mmc_attach),
1998 DEVMETHOD(device_detach, mmc_detach),
1999 DEVMETHOD(device_suspend, mmc_suspend),
2000 DEVMETHOD(device_resume, mmc_resume),
2003 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
2004 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
2005 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
2007 /* MMC Bus interface */
2008 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
2009 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
2010 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
2015 driver_t mmc_driver = {
2018 sizeof(struct mmc_softc),
2020 devclass_t mmc_devclass;
2022 MODULE_VERSION(mmc, MMC_VERSION);