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>
67 #include <dev/mmc/mmcreg.h>
68 #include <dev/mmc/mmcbrvar.h>
69 #include <dev/mmc/mmcvar.h>
71 #include "mmcbus_if.h"
76 struct intr_config_hook config_intrhook;
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[512]; /* 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 timing; /* Bus timing support */
99 u_char high_cap; /* High Capacity card (block addressed) */
100 uint32_t sec_count; /* Card capacity in 512byte blocks */
101 uint32_t tran_speed; /* Max speed in normal mode */
102 uint32_t hs_tran_speed; /* Max speed in high speed mode */
103 uint32_t erase_sector; /* Card native erase sector size */
104 char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
107 #define CMD_RETRIES 3
109 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
111 static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
113 static int mmc_debug;
114 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");
116 /* bus entry points */
117 static int mmc_acquire_bus(device_t busdev, device_t dev);
118 static int mmc_attach(device_t dev);
119 static int mmc_child_location_str(device_t dev, device_t child, char *buf,
121 static int mmc_detach(device_t dev);
122 static int mmc_probe(device_t dev);
123 static int mmc_read_ivar(device_t bus, device_t child, int which,
125 static int mmc_release_bus(device_t busdev, device_t dev);
126 static int mmc_resume(device_t dev);
127 static int mmc_suspend(device_t dev);
128 static int mmc_wait_for_request(device_t brdev, device_t reqdev,
129 struct mmc_request *req);
130 static int mmc_write_ivar(device_t bus, device_t child, int which,
133 #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
134 #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
135 #define MMC_LOCK_INIT(_sc) \
136 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
138 #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
139 #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
140 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
142 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
143 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
144 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
145 struct mmc_sd_status *sd_status);
146 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
147 uint32_t *rawsdstatus);
148 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
150 static int mmc_calculate_clock(struct mmc_softc *sc);
151 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid);
152 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
153 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
154 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
155 static void mmc_delayed_attach(void *xsc);
156 static int mmc_delete_cards(struct mmc_softc *sc);
157 static void mmc_discover_cards(struct mmc_softc *sc);
158 static void mmc_format_card_id_string(struct mmc_ivars *ivar);
159 static void mmc_go_discovery(struct mmc_softc *sc);
160 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
162 static int mmc_highest_voltage(uint32_t ocr);
163 static void mmc_idle_cards(struct mmc_softc *sc);
164 static void mmc_ms_delay(int ms);
165 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
166 static void mmc_power_down(struct mmc_softc *sc);
167 static void mmc_power_up(struct mmc_softc *sc);
168 static void mmc_rescan_cards(struct mmc_softc *sc);
169 static void mmc_scan(struct mmc_softc *sc);
170 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
171 uint8_t value, uint8_t *res);
172 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
173 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
174 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
176 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
177 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
178 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
179 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
181 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
182 static int mmc_send_status(struct mmc_softc *sc, uint16_t rca,
184 static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
185 static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca,
187 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
188 static int mmc_set_timing(struct mmc_softc *sc, int timing);
189 static int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index,
191 static int mmc_test_bus_width(struct mmc_softc *sc);
192 static int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
193 struct mmc_command *cmd, int retries);
194 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
196 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
197 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
198 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
199 static void mmc_wakeup(struct mmc_request *req);
205 DELAY(1000 * ms); /* XXX BAD */
209 mmc_probe(device_t dev)
212 device_set_desc(dev, "MMC/SD bus");
217 mmc_attach(device_t dev)
219 struct mmc_softc *sc;
221 sc = device_get_softc(dev);
225 /* We'll probe and attach our children later, but before / mount */
226 sc->config_intrhook.ich_func = mmc_delayed_attach;
227 sc->config_intrhook.ich_arg = sc;
228 if (config_intrhook_establish(&sc->config_intrhook) != 0)
229 device_printf(dev, "config_intrhook_establish failed\n");
234 mmc_detach(device_t dev)
236 struct mmc_softc *sc = device_get_softc(dev);
239 if ((err = mmc_delete_cards(sc)) != 0)
242 MMC_LOCK_DESTROY(sc);
248 mmc_suspend(device_t dev)
250 struct mmc_softc *sc = device_get_softc(dev);
253 err = bus_generic_suspend(dev);
261 mmc_resume(device_t dev)
263 struct mmc_softc *sc = device_get_softc(dev);
266 return (bus_generic_resume(dev));
270 mmc_acquire_bus(device_t busdev, device_t dev)
272 struct mmc_softc *sc;
273 struct mmc_ivars *ivar;
277 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
280 sc = device_get_softc(busdev);
283 panic("mmc: host bridge didn't serialize us.");
289 * Keep track of the last rca that we've selected. If
290 * we're asked to do it again, don't. We never
291 * unselect unless the bus code itself wants the mmc
292 * bus, and constantly reselecting causes problems.
294 rca = mmc_get_rca(dev);
295 if (sc->last_rca != rca) {
296 mmc_select_card(sc, rca);
298 /* Prepare bus width for the new card. */
299 ivar = device_get_ivars(dev);
300 if (bootverbose || mmc_debug) {
301 device_printf(busdev,
302 "setting bus width to %d bits\n",
303 (ivar->bus_width == bus_width_4) ? 4 :
304 (ivar->bus_width == bus_width_8) ? 8 : 1);
306 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
307 mmcbr_set_bus_width(busdev, ivar->bus_width);
308 mmcbr_update_ios(busdev);
312 * If there's a card selected, stand down.
314 if (sc->last_rca != 0) {
315 mmc_select_card(sc, 0);
324 mmc_release_bus(device_t busdev, device_t dev)
326 struct mmc_softc *sc;
329 sc = device_get_softc(busdev);
333 panic("mmc: releasing unowned bus.");
334 if (sc->owner != dev)
335 panic("mmc: you don't own the bus. game over.");
337 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
347 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
350 return (ocr & MMC_OCR_VOLTAGE);
354 mmc_highest_voltage(uint32_t ocr)
358 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
359 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
366 mmc_wakeup(struct mmc_request *req)
368 struct mmc_softc *sc;
370 sc = (struct mmc_softc *)req->done_data;
372 req->flags |= MMC_REQ_DONE;
378 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
381 req->done = mmc_wakeup;
384 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
385 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
386 if (req->cmd->data) {
387 printf(" data %d\n", (int)req->cmd->data->len);
391 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
393 while ((req->flags & MMC_REQ_DONE) == 0)
394 msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
396 if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
397 device_printf(sc->dev, "CMD%d RESULT: %d\n",
398 req->cmd->opcode, req->cmd->error);
403 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
405 struct mmc_softc *sc = device_get_softc(brdev);
407 return (mmc_wait_for_req(sc, req));
411 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
413 struct mmc_request mreq;
417 memset(&mreq, 0, sizeof(mreq));
418 memset(cmd->resp, 0, sizeof(cmd->resp));
419 cmd->retries = 0; /* Retries done here, not in hardware. */
422 if (mmc_wait_for_req(sc, &mreq) != 0)
423 err = MMC_ERR_FAILED;
426 } while (err != MMC_ERR_NONE && retries-- > 0);
432 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
433 struct mmc_command *cmd, int retries)
435 struct mmc_command appcmd;
439 memset(&appcmd, 0, sizeof(appcmd));
440 appcmd.opcode = MMC_APP_CMD;
441 appcmd.arg = rca << 16;
442 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
444 if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0)
445 err = MMC_ERR_FAILED;
448 if (err == MMC_ERR_NONE) {
449 if (!(appcmd.resp[0] & R1_APP_CMD))
450 err = MMC_ERR_FAILED;
451 else if (mmc_wait_for_cmd(sc, cmd, 0) != 0)
452 err = MMC_ERR_FAILED;
456 } while (err != MMC_ERR_NONE && retries-- > 0);
462 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
463 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
465 struct mmc_command cmd;
468 memset(&cmd, 0, sizeof(cmd));
473 err = mmc_wait_for_cmd(sc, &cmd, retries);
477 if (flags & MMC_RSP_136)
478 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
486 mmc_idle_cards(struct mmc_softc *sc)
489 struct mmc_command cmd;
492 mmcbr_set_chip_select(dev, cs_high);
493 mmcbr_update_ios(dev);
496 memset(&cmd, 0, sizeof(cmd));
497 cmd.opcode = MMC_GO_IDLE_STATE;
499 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
501 mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
504 mmcbr_set_chip_select(dev, cs_dontcare);
505 mmcbr_update_ios(dev);
510 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
512 struct mmc_command cmd;
513 int err = MMC_ERR_NONE, i;
515 memset(&cmd, 0, sizeof(cmd));
516 cmd.opcode = ACMD_SD_SEND_OP_COND;
518 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
521 for (i = 0; i < 1000; i++) {
522 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
523 if (err != MMC_ERR_NONE)
525 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
526 (ocr & MMC_OCR_VOLTAGE) == 0)
528 err = MMC_ERR_TIMEOUT;
531 if (rocr && err == MMC_ERR_NONE)
537 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
539 struct mmc_command cmd;
540 int err = MMC_ERR_NONE, i;
542 memset(&cmd, 0, sizeof(cmd));
543 cmd.opcode = MMC_SEND_OP_COND;
545 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
548 for (i = 0; i < 1000; i++) {
549 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
550 if (err != MMC_ERR_NONE)
552 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
553 (ocr & MMC_OCR_VOLTAGE) == 0)
555 err = MMC_ERR_TIMEOUT;
558 if (rocr && err == MMC_ERR_NONE)
564 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
566 struct mmc_command cmd;
569 memset(&cmd, 0, sizeof(cmd));
570 cmd.opcode = SD_SEND_IF_COND;
571 cmd.arg = (vhs << 8) + 0xAA;
572 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
575 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
580 mmc_power_up(struct mmc_softc *sc)
585 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
586 mmcbr_set_bus_mode(dev, opendrain);
587 mmcbr_set_chip_select(dev, cs_dontcare);
588 mmcbr_set_bus_width(dev, bus_width_1);
589 mmcbr_set_power_mode(dev, power_up);
590 mmcbr_set_clock(dev, 0);
591 mmcbr_update_ios(dev);
594 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
595 mmcbr_set_timing(dev, bus_timing_normal);
596 mmcbr_set_power_mode(dev, power_on);
597 mmcbr_update_ios(dev);
602 mmc_power_down(struct mmc_softc *sc)
604 device_t dev = sc->dev;
606 mmcbr_set_bus_mode(dev, opendrain);
607 mmcbr_set_chip_select(dev, cs_dontcare);
608 mmcbr_set_bus_width(dev, bus_width_1);
609 mmcbr_set_power_mode(dev, power_off);
610 mmcbr_set_clock(dev, 0);
611 mmcbr_set_timing(dev, bus_timing_normal);
612 mmcbr_update_ios(dev);
616 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
620 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
621 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
622 flags, NULL, CMD_RETRIES));
626 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
628 struct mmc_command cmd;
631 memset(&cmd, 0, sizeof(cmd));
632 cmd.opcode = MMC_SWITCH_FUNC;
633 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
637 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
639 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
644 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
648 struct mmc_command cmd;
649 struct mmc_data data;
651 memset(&cmd, 0, sizeof(cmd));
652 memset(&data, 0, sizeof(data));
655 cmd.opcode = SD_SWITCH_FUNC;
656 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
657 cmd.arg = mode << 31; /* 0 - check, 1 - set */
658 cmd.arg |= 0x00FFFFFF;
659 cmd.arg &= ~(0xF << (grp * 4));
660 cmd.arg |= value << (grp * 4);
665 data.flags = MMC_DATA_READ;
667 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
672 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
674 struct mmc_command cmd;
678 if (mmcbr_get_mode(sc->dev) == mode_sd) {
679 memset(&cmd, 0, sizeof(cmd));
680 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
681 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
682 cmd.arg = SD_CLR_CARD_DETECT;
683 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
686 memset(&cmd, 0, sizeof(cmd));
687 cmd.opcode = ACMD_SET_BUS_WIDTH;
688 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
691 cmd.arg = SD_BUS_WIDTH_1;
694 cmd.arg = SD_BUS_WIDTH_4;
697 return (MMC_ERR_INVALID);
699 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
703 value = EXT_CSD_BUS_WIDTH_1;
706 value = EXT_CSD_BUS_WIDTH_4;
709 value = EXT_CSD_BUS_WIDTH_8;
712 return (MMC_ERR_INVALID);
714 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
721 mmc_set_timing(struct mmc_softc *sc, int timing)
725 u_char switch_res[64];
728 case bus_timing_normal:
735 return (MMC_ERR_INVALID);
737 if (mmcbr_get_mode(sc->dev) == mode_sd)
738 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
741 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
742 EXT_CSD_HS_TIMING, value);
747 mmc_test_bus_width(struct mmc_softc *sc)
749 struct mmc_command cmd;
750 struct mmc_data data;
753 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
754 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
755 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, };
756 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
758 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
759 mmcbr_set_bus_width(sc->dev, bus_width_8);
760 mmcbr_update_ios(sc->dev);
762 memset(&cmd, 0, sizeof(cmd));
763 memset(&data, 0, sizeof(data));
764 cmd.opcode = MMC_BUSTEST_W;
766 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
771 data.flags = MMC_DATA_WRITE;
772 mmc_wait_for_cmd(sc, &cmd, 0);
774 memset(&cmd, 0, sizeof(cmd));
775 memset(&data, 0, sizeof(data));
776 cmd.opcode = MMC_BUSTEST_R;
778 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
783 data.flags = MMC_DATA_READ;
784 err = mmc_wait_for_cmd(sc, &cmd, 0);
786 mmcbr_set_bus_width(sc->dev, bus_width_1);
787 mmcbr_update_ios(sc->dev);
789 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
790 return (bus_width_8);
793 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
794 mmcbr_set_bus_width(sc->dev, bus_width_4);
795 mmcbr_update_ios(sc->dev);
797 memset(&cmd, 0, sizeof(cmd));
798 memset(&data, 0, sizeof(data));
799 cmd.opcode = MMC_BUSTEST_W;
801 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
806 data.flags = MMC_DATA_WRITE;
807 mmc_wait_for_cmd(sc, &cmd, 0);
809 memset(&cmd, 0, sizeof(cmd));
810 memset(&data, 0, sizeof(data));
811 cmd.opcode = MMC_BUSTEST_R;
813 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
818 data.flags = MMC_DATA_READ;
819 err = mmc_wait_for_cmd(sc, &cmd, 0);
821 mmcbr_set_bus_width(sc->dev, bus_width_1);
822 mmcbr_update_ios(sc->dev);
824 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
825 return (bus_width_4);
827 return (bus_width_1);
831 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
833 const int i = (bit_len / 32) - (start / 32) - 1;
834 const int shift = start & 31;
835 uint32_t retval = bits[i] >> shift;
836 if (size + shift > 32)
837 retval |= bits[i - 1] << (32 - shift);
838 return (retval & ((1llu << size) - 1));
842 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
846 /* There's no version info, so we take it on faith */
847 memset(cid, 0, sizeof(*cid));
848 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
849 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
850 for (i = 0; i < 5; i++)
851 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
853 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
854 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
855 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
856 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
860 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
864 /* There's no version info, so we take it on faith */
865 memset(cid, 0, sizeof(*cid));
866 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
867 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
868 for (i = 0; i < 6; i++)
869 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
871 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
872 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
873 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
874 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
878 mmc_format_card_id_string(struct mmc_ivars *ivar)
885 * Format a card ID string for use by the mmcsd driver, it's what
886 * appears between the <> in the following:
887 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
888 * 22.5MHz/4bit/128-block
890 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
891 * and our max formatted length is currently 55 bytes if every field
892 * contains the largest value.
894 * Sometimes the oid is two printable ascii chars; when it's not,
895 * format it as 0xnnnn instead.
897 c1 = (ivar->cid.oid >> 8) & 0x0ff;
898 c2 = ivar->cid.oid & 0x0ff;
899 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
900 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
902 snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
903 snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
904 "%s%s %s %d.%d SN %u MFG %02d/%04d by %d %s",
905 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
906 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
907 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
908 ivar->cid.mid, oidstr);
911 static const int exp[8] = {
912 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
915 static const int mant[16] = {
916 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
919 static const int cur_min[8] = {
920 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
923 static const int cur_max[8] = {
924 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
928 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
934 memset(csd, 0, sizeof(*csd));
935 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
937 m = mmc_get_bits(raw_csd, 128, 115, 4);
938 e = mmc_get_bits(raw_csd, 128, 112, 3);
939 csd->tacc = (exp[e] * mant[m] + 9) / 10;
940 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
941 m = mmc_get_bits(raw_csd, 128, 99, 4);
942 e = mmc_get_bits(raw_csd, 128, 96, 3);
943 csd->tran_speed = exp[e] * 10000 * mant[m];
944 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
945 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
946 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
947 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
948 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
949 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
950 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
951 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
952 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
953 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
954 m = mmc_get_bits(raw_csd, 128, 62, 12);
955 e = mmc_get_bits(raw_csd, 128, 47, 3);
956 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
957 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
958 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
959 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
960 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
961 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
962 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
963 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
965 m = mmc_get_bits(raw_csd, 128, 115, 4);
966 e = mmc_get_bits(raw_csd, 128, 112, 3);
967 csd->tacc = (exp[e] * mant[m] + 9) / 10;
968 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
969 m = mmc_get_bits(raw_csd, 128, 99, 4);
970 e = mmc_get_bits(raw_csd, 128, 96, 3);
971 csd->tran_speed = exp[e] * 10000 * mant[m];
972 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
973 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
974 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
975 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
976 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
977 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
978 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
980 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
981 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
982 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
983 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
984 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
985 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
986 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
988 panic("unknown SD CSD version");
992 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
997 memset(csd, 0, sizeof(*csd));
998 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
999 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1000 m = mmc_get_bits(raw_csd, 128, 115, 4);
1001 e = mmc_get_bits(raw_csd, 128, 112, 3);
1002 csd->tacc = exp[e] * mant[m] + 9 / 10;
1003 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1004 m = mmc_get_bits(raw_csd, 128, 99, 4);
1005 e = mmc_get_bits(raw_csd, 128, 96, 3);
1006 csd->tran_speed = exp[e] * 10000 * mant[m];
1007 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1008 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1009 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1010 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1011 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1012 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1013 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1014 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1015 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1016 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1017 m = mmc_get_bits(raw_csd, 128, 62, 12);
1018 e = mmc_get_bits(raw_csd, 128, 47, 3);
1019 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1020 csd->erase_blk_en = 0;
1021 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1022 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1023 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1024 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1025 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1026 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1027 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1031 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1033 unsigned int scr_struct;
1035 memset(scr, 0, sizeof(*scr));
1037 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1038 if (scr_struct != 0) {
1039 printf("Unrecognised SCR structure version %d\n",
1043 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1044 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1048 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1049 struct mmc_sd_status *sd_status)
1052 memset(sd_status, 0, sizeof(*sd_status));
1054 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1055 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1056 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1057 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1058 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1059 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1060 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1061 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1062 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1063 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1067 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1069 struct mmc_command cmd;
1072 memset(&cmd, 0, sizeof(cmd));
1073 cmd.opcode = MMC_ALL_SEND_CID;
1075 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1077 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1078 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1083 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1085 struct mmc_command cmd;
1088 memset(&cmd, 0, sizeof(cmd));
1089 cmd.opcode = MMC_SEND_CSD;
1090 cmd.arg = rca << 16;
1091 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1093 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1094 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1099 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1102 struct mmc_command cmd;
1103 struct mmc_data data;
1105 memset(&cmd, 0, sizeof(cmd));
1106 memset(&data, 0, sizeof(data));
1108 memset(rawscr, 0, 8);
1109 cmd.opcode = ACMD_SEND_SCR;
1110 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1116 data.flags = MMC_DATA_READ;
1118 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1119 rawscr[0] = be32toh(rawscr[0]);
1120 rawscr[1] = be32toh(rawscr[1]);
1125 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1128 struct mmc_command cmd;
1129 struct mmc_data data;
1131 memset(&cmd, 0, sizeof(cmd));
1132 memset(&data, 0, sizeof(data));
1134 memset(rawextcsd, 0, 512);
1135 cmd.opcode = MMC_SEND_EXT_CSD;
1136 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1140 data.data = rawextcsd;
1142 data.flags = MMC_DATA_READ;
1144 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1149 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1152 struct mmc_command cmd;
1153 struct mmc_data data;
1155 memset(&cmd, 0, sizeof(cmd));
1156 memset(&data, 0, sizeof(data));
1158 memset(rawsdstatus, 0, 64);
1159 cmd.opcode = ACMD_SD_STATUS;
1160 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1164 data.data = rawsdstatus;
1166 data.flags = MMC_DATA_READ;
1168 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1169 for (i = 0; i < 16; i++)
1170 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1175 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1177 struct mmc_command cmd;
1180 memset(&cmd, 0, sizeof(cmd));
1181 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1182 cmd.arg = resp << 16;
1183 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1185 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1190 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1192 struct mmc_command cmd;
1195 memset(&cmd, 0, sizeof(cmd));
1196 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1198 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1200 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1201 *resp = cmd.resp[0];
1206 mmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status)
1208 struct mmc_command cmd;
1211 memset(&cmd, 0, sizeof(cmd));
1212 cmd.opcode = MMC_SEND_STATUS;
1213 cmd.arg = rca << 16;
1214 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1216 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1217 *status = cmd.resp[0];
1222 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1224 struct mmc_command cmd;
1227 memset(&cmd, 0, sizeof(cmd));
1228 cmd.opcode = MMC_SET_BLOCKLEN;
1230 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1232 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1237 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1239 device_printf(dev, "Card at relative address 0x%04x%s:\n",
1240 ivar->rca, newcard ? " added" : "");
1241 device_printf(dev, " card: %s\n", ivar->card_id_string);
1242 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1243 (ivar->bus_width == bus_width_1 ? 1 :
1244 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1245 (ivar->timing == bus_timing_hs ?
1246 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1247 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1248 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1249 ivar->sec_count, ivar->erase_sector,
1250 ivar->read_only ? ", read-only" : "");
1254 mmc_discover_cards(struct mmc_softc *sc)
1256 struct mmc_ivars *ivar = NULL;
1258 int err, i, devcount, newcard;
1259 uint32_t raw_cid[4], resp, sec_count, status;
1262 u_char switch_res[64];
1264 if (bootverbose || mmc_debug)
1265 device_printf(sc->dev, "Probing cards\n");
1267 err = mmc_all_send_cid(sc, raw_cid);
1268 if (err == MMC_ERR_TIMEOUT)
1270 if (err != MMC_ERR_NONE) {
1271 device_printf(sc->dev, "Error reading CID %d\n", err);
1275 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1277 for (i = 0; i < devcount; i++) {
1278 ivar = device_get_ivars(devlist[i]);
1279 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1284 free(devlist, M_TEMP);
1285 if (bootverbose || mmc_debug) {
1286 device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1287 newcard ? "New c" : "C",
1288 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1291 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1293 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1295 if (mmcbr_get_ro(sc->dev))
1296 ivar->read_only = 1;
1297 ivar->bus_width = bus_width_1;
1298 ivar->timing = bus_timing_normal;
1299 ivar->mode = mmcbr_get_mode(sc->dev);
1300 if (ivar->mode == mode_sd) {
1301 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1302 mmc_send_relative_addr(sc, &resp);
1303 ivar->rca = resp >> 16;
1305 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1306 if (bootverbose || mmc_debug)
1307 device_printf(sc->dev,
1308 "%sard detected (CSD %08x%08x%08x%08x)\n",
1309 newcard ? "New c" : "C", ivar->raw_csd[0],
1310 ivar->raw_csd[1], ivar->raw_csd[2],
1312 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1313 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1314 if (ivar->csd.csd_structure > 0)
1316 ivar->tran_speed = ivar->csd.tran_speed;
1317 ivar->erase_sector = ivar->csd.erase_sector *
1318 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1320 err = mmc_send_status(sc, ivar->rca, &status);
1321 if (err != MMC_ERR_NONE) {
1322 device_printf(sc->dev,
1323 "Error reading card status %d\n", err);
1326 if ((status & R1_CARD_IS_LOCKED) != 0) {
1327 device_printf(sc->dev,
1328 "Card is password protected, skipping.\n");
1332 /* Get card SCR. Card must be selected to fetch it. */
1333 mmc_select_card(sc, ivar->rca);
1334 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1335 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1336 /* Get card switch capabilities (command class 10). */
1337 if ((ivar->scr.sda_vsn >= 1) &&
1338 (ivar->csd.ccc & (1<<10))) {
1339 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1340 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1342 if (switch_res[13] & 2) {
1343 ivar->timing = bus_timing_hs;
1344 ivar->hs_tran_speed = SD_MAX_HS;
1347 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1348 mmc_app_decode_sd_status(ivar->raw_sd_status,
1350 if (ivar->sd_status.au_size != 0) {
1351 ivar->erase_sector =
1352 16 << ivar->sd_status.au_size;
1354 mmc_select_card(sc, 0);
1355 /* Find max supported bus width. */
1356 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1357 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1358 ivar->bus_width = bus_width_4;
1361 * Some cards that report maximum I/O block sizes
1362 * greater than 512 require the block length to be
1363 * set to 512, even though that is supposed to be
1364 * the default. Example:
1366 * Transcend 2GB SDSC card, CID:
1367 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1369 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1370 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1371 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1373 mmc_format_card_id_string(ivar);
1375 if (bootverbose || mmc_debug)
1376 mmc_log_card(sc->dev, ivar, newcard);
1379 child = device_add_child(sc->dev, NULL, -1);
1380 device_set_ivars(child, ivar);
1384 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1386 mmc_set_relative_addr(sc, ivar->rca);
1388 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1389 if (bootverbose || mmc_debug)
1390 device_printf(sc->dev,
1391 "%sard detected (CSD %08x%08x%08x%08x)\n",
1392 newcard ? "New c" : "C", ivar->raw_csd[0],
1393 ivar->raw_csd[1], ivar->raw_csd[2],
1396 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1397 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1398 ivar->tran_speed = ivar->csd.tran_speed;
1399 ivar->erase_sector = ivar->csd.erase_sector *
1400 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1402 err = mmc_send_status(sc, ivar->rca, &status);
1403 if (err != MMC_ERR_NONE) {
1404 device_printf(sc->dev,
1405 "Error reading card status %d\n", err);
1408 if ((status & R1_CARD_IS_LOCKED) != 0) {
1409 device_printf(sc->dev,
1410 "Card is password protected, skipping.\n");
1414 /* Only MMC >= 4.x cards support EXT_CSD. */
1415 if (ivar->csd.spec_vers >= 4) {
1416 /* Card must be selected to fetch EXT_CSD. */
1417 mmc_select_card(sc, ivar->rca);
1418 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1419 /* Handle extended capacity from EXT_CSD */
1420 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1421 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1422 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1423 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1424 if (sec_count != 0) {
1425 ivar->sec_count = sec_count;
1428 /* Get card speed in high speed mode. */
1429 ivar->timing = bus_timing_hs;
1430 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1431 & EXT_CSD_CARD_TYPE_52)
1432 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1433 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1434 & EXT_CSD_CARD_TYPE_26)
1435 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1437 ivar->hs_tran_speed = ivar->tran_speed;
1438 /* Find max supported bus width. */
1439 ivar->bus_width = mmc_test_bus_width(sc);
1440 mmc_select_card(sc, 0);
1441 /* Handle HC erase sector size. */
1442 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1443 ivar->erase_sector = 1024 *
1444 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1445 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1446 EXT_CSD_ERASE_GRP_DEF, 1);
1449 ivar->bus_width = bus_width_1;
1450 ivar->timing = bus_timing_normal;
1454 * Some cards that report maximum I/O block sizes greater
1455 * than 512 require the block length to be set to 512, even
1456 * though that is supposed to be the default. Example:
1458 * Transcend 2GB SDSC card, CID:
1459 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1461 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1462 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1463 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1465 mmc_format_card_id_string(ivar);
1467 if (bootverbose || mmc_debug)
1468 mmc_log_card(sc->dev, ivar, newcard);
1471 child = device_add_child(sc->dev, NULL, -1);
1472 device_set_ivars(child, ivar);
1478 mmc_rescan_cards(struct mmc_softc *sc)
1480 struct mmc_ivars *ivar = NULL;
1482 int err, i, devcount;
1484 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1486 for (i = 0; i < devcount; i++) {
1487 ivar = device_get_ivars(devlist[i]);
1488 if (mmc_select_card(sc, ivar->rca)) {
1489 if (bootverbose || mmc_debug)
1490 device_printf(sc->dev, "Card at relative address %d lost.\n",
1492 device_delete_child(sc->dev, devlist[i]);
1493 free(ivar, M_DEVBUF);
1496 free(devlist, M_TEMP);
1497 mmc_select_card(sc, 0);
1501 mmc_delete_cards(struct mmc_softc *sc)
1503 struct mmc_ivars *ivar;
1505 int err, i, devcount;
1507 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1509 for (i = 0; i < devcount; i++) {
1510 ivar = device_get_ivars(devlist[i]);
1511 if (bootverbose || mmc_debug)
1512 device_printf(sc->dev, "Card at relative address %d deleted.\n",
1514 device_delete_child(sc->dev, devlist[i]);
1515 free(ivar, M_DEVBUF);
1517 free(devlist, M_TEMP);
1522 mmc_go_discovery(struct mmc_softc *sc)
1529 if (mmcbr_get_power_mode(dev) != power_on) {
1531 * First, try SD modes
1533 mmcbr_set_mode(dev, mode_sd);
1535 mmcbr_set_bus_mode(dev, pushpull);
1536 if (bootverbose || mmc_debug)
1537 device_printf(sc->dev, "Probing bus\n");
1539 err = mmc_send_if_cond(sc, 1);
1540 if ((bootverbose || mmc_debug) && err == 0)
1541 device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1542 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1543 if (bootverbose || mmc_debug)
1544 device_printf(sc->dev, "SD probe: failed\n");
1548 mmcbr_set_mode(dev, mode_mmc);
1549 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1550 if (bootverbose || mmc_debug)
1551 device_printf(sc->dev, "MMC probe: failed\n");
1552 ocr = 0; /* Failed both, powerdown. */
1553 } else if (bootverbose || mmc_debug)
1554 device_printf(sc->dev,
1555 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1556 } else if (bootverbose || mmc_debug)
1557 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1559 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1560 if (mmcbr_get_ocr(dev) != 0)
1563 mmcbr_set_bus_mode(dev, opendrain);
1564 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1565 mmcbr_update_ios(dev);
1566 /* XXX recompute vdd based on new cards? */
1569 * Make sure that we have a mutually agreeable voltage to at least
1570 * one card on the bus.
1572 if (bootverbose || mmc_debug)
1573 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1574 if (mmcbr_get_ocr(dev) == 0) {
1575 mmc_delete_cards(sc);
1580 * Reselect the cards after we've idled them above.
1582 if (mmcbr_get_mode(dev) == mode_sd) {
1583 err = mmc_send_if_cond(sc, 1);
1584 mmc_send_app_op_cond(sc,
1585 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1587 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
1588 mmc_discover_cards(sc);
1589 mmc_rescan_cards(sc);
1591 mmcbr_set_bus_mode(dev, pushpull);
1592 mmcbr_update_ios(dev);
1593 mmc_calculate_clock(sc);
1594 bus_generic_attach(dev);
1595 /* mmc_update_children_sysctl(dev);*/
1599 mmc_calculate_clock(struct mmc_softc *sc)
1601 int max_dtr, max_hs_dtr, max_timing;
1604 struct mmc_ivars *ivar;
1606 f_max = mmcbr_get_f_max(sc->dev);
1607 max_dtr = max_hs_dtr = f_max;
1608 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1609 max_timing = bus_timing_hs;
1611 max_timing = bus_timing_normal;
1612 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1613 panic("can't get children");
1614 for (i = 0; i < nkid; i++) {
1615 ivar = device_get_ivars(kids[i]);
1616 if (ivar->timing < max_timing)
1617 max_timing = ivar->timing;
1618 if (ivar->tran_speed < max_dtr)
1619 max_dtr = ivar->tran_speed;
1620 if (ivar->hs_tran_speed < max_hs_dtr)
1621 max_hs_dtr = ivar->hs_tran_speed;
1623 for (i = 0; i < nkid; i++) {
1624 ivar = device_get_ivars(kids[i]);
1625 if (ivar->timing == bus_timing_normal)
1627 mmc_select_card(sc, ivar->rca);
1628 mmc_set_timing(sc, max_timing);
1630 mmc_select_card(sc, 0);
1632 if (max_timing == bus_timing_hs)
1633 max_dtr = max_hs_dtr;
1634 if (bootverbose || mmc_debug) {
1635 device_printf(sc->dev,
1636 "setting transfer rate to %d.%03dMHz%s\n",
1637 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1638 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1640 mmcbr_set_timing(sc->dev, max_timing);
1641 mmcbr_set_clock(sc->dev, max_dtr);
1642 mmcbr_update_ios(sc->dev);
1647 mmc_scan(struct mmc_softc *sc)
1649 device_t dev = sc->dev;
1651 mmc_acquire_bus(dev, dev);
1652 mmc_go_discovery(sc);
1653 mmc_release_bus(dev, dev);
1657 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1659 struct mmc_ivars *ivar = device_get_ivars(child);
1664 case MMC_IVAR_DSR_IMP:
1665 *result = ivar->csd.dsr_imp;
1667 case MMC_IVAR_MEDIA_SIZE:
1668 *result = ivar->sec_count;
1671 *result = ivar->rca;
1673 case MMC_IVAR_SECTOR_SIZE:
1674 *result = MMC_SECTOR_SIZE;
1676 case MMC_IVAR_TRAN_SPEED:
1677 *result = mmcbr_get_clock(bus);
1679 case MMC_IVAR_READ_ONLY:
1680 *result = ivar->read_only;
1682 case MMC_IVAR_HIGH_CAP:
1683 *result = ivar->high_cap;
1685 case MMC_IVAR_CARD_TYPE:
1686 *result = ivar->mode;
1688 case MMC_IVAR_BUS_WIDTH:
1689 *result = ivar->bus_width;
1691 case MMC_IVAR_ERASE_SECTOR:
1692 *result = ivar->erase_sector;
1694 case MMC_IVAR_MAX_DATA:
1695 *result = mmcbr_get_max_data(bus);
1697 case MMC_IVAR_CARD_ID_STRING:
1698 *(char **)result = ivar->card_id_string;
1705 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1708 * None are writable ATM
1714 mmc_delayed_attach(void *xsc)
1716 struct mmc_softc *sc = xsc;
1719 config_intrhook_disestablish(&sc->config_intrhook);
1723 mmc_child_location_str(device_t dev, device_t child, char *buf,
1727 snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1731 static device_method_t mmc_methods[] = {
1733 DEVMETHOD(device_probe, mmc_probe),
1734 DEVMETHOD(device_attach, mmc_attach),
1735 DEVMETHOD(device_detach, mmc_detach),
1736 DEVMETHOD(device_suspend, mmc_suspend),
1737 DEVMETHOD(device_resume, mmc_resume),
1740 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1741 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1742 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1744 /* MMC Bus interface */
1745 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1746 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1747 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1752 static driver_t mmc_driver = {
1755 sizeof(struct mmc_softc),
1757 static devclass_t mmc_devclass;
1759 DRIVER_MODULE(mmc, ti_mmchs, mmc_driver, mmc_devclass, NULL, NULL);
1760 DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL);
1761 DRIVER_MODULE(mmc, sdhci_pci, mmc_driver, mmc_devclass, NULL, NULL);
1762 DRIVER_MODULE(mmc, sdhci_bcm, mmc_driver, mmc_devclass, NULL, NULL);
1763 DRIVER_MODULE(mmc, sdhci_fdt, mmc_driver, mmc_devclass, NULL, NULL);
1764 DRIVER_MODULE(mmc, sdhci_ti, mmc_driver, mmc_devclass, NULL, NULL);