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) */
105 char card_sn_string[16];/* Formatted serial # for disk->d_ident */
108 #define CMD_RETRIES 3
110 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
112 static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
114 static int mmc_debug;
115 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");
117 /* bus entry points */
118 static int mmc_acquire_bus(device_t busdev, device_t dev);
119 static int mmc_attach(device_t dev);
120 static int mmc_child_location_str(device_t dev, device_t child, char *buf,
122 static int mmc_detach(device_t dev);
123 static int mmc_probe(device_t dev);
124 static int mmc_read_ivar(device_t bus, device_t child, int which,
126 static int mmc_release_bus(device_t busdev, device_t dev);
127 static int mmc_resume(device_t dev);
128 static int mmc_suspend(device_t dev);
129 static int mmc_wait_for_request(device_t brdev, device_t reqdev,
130 struct mmc_request *req);
131 static int mmc_write_ivar(device_t bus, device_t child, int which,
134 #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
135 #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
136 #define MMC_LOCK_INIT(_sc) \
137 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
139 #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
140 #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
141 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
143 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
144 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
145 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
146 struct mmc_sd_status *sd_status);
147 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
148 uint32_t *rawsdstatus);
149 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
151 static int mmc_calculate_clock(struct mmc_softc *sc);
152 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid);
153 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
154 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
155 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
156 static void mmc_delayed_attach(void *xsc);
157 static int mmc_delete_cards(struct mmc_softc *sc);
158 static void mmc_discover_cards(struct mmc_softc *sc);
159 static void mmc_format_card_id_string(struct mmc_ivars *ivar);
160 static void mmc_go_discovery(struct mmc_softc *sc);
161 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
163 static int mmc_highest_voltage(uint32_t ocr);
164 static void mmc_idle_cards(struct mmc_softc *sc);
165 static void mmc_ms_delay(int ms);
166 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
167 static void mmc_power_down(struct mmc_softc *sc);
168 static void mmc_power_up(struct mmc_softc *sc);
169 static void mmc_rescan_cards(struct mmc_softc *sc);
170 static void mmc_scan(struct mmc_softc *sc);
171 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
172 uint8_t value, uint8_t *res);
173 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
174 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
175 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
177 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
178 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
179 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
180 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
182 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
183 static int mmc_send_status(struct mmc_softc *sc, uint16_t rca,
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, uint16_t rca,
188 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
189 static int mmc_set_timing(struct mmc_softc *sc, int timing);
190 static int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index,
192 static int mmc_test_bus_width(struct mmc_softc *sc);
193 static int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
194 struct mmc_command *cmd, int retries);
195 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
197 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
198 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
199 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
200 static void mmc_wakeup(struct mmc_request *req);
206 DELAY(1000 * ms); /* XXX BAD */
210 mmc_probe(device_t dev)
213 device_set_desc(dev, "MMC/SD bus");
218 mmc_attach(device_t dev)
220 struct mmc_softc *sc;
222 sc = device_get_softc(dev);
226 /* We'll probe and attach our children later, but before / mount */
227 sc->config_intrhook.ich_func = mmc_delayed_attach;
228 sc->config_intrhook.ich_arg = sc;
229 if (config_intrhook_establish(&sc->config_intrhook) != 0)
230 device_printf(dev, "config_intrhook_establish failed\n");
235 mmc_detach(device_t dev)
237 struct mmc_softc *sc = device_get_softc(dev);
240 if ((err = mmc_delete_cards(sc)) != 0)
243 MMC_LOCK_DESTROY(sc);
249 mmc_suspend(device_t dev)
251 struct mmc_softc *sc = device_get_softc(dev);
254 err = bus_generic_suspend(dev);
262 mmc_resume(device_t dev)
264 struct mmc_softc *sc = device_get_softc(dev);
267 return (bus_generic_resume(dev));
271 mmc_acquire_bus(device_t busdev, device_t dev)
273 struct mmc_softc *sc;
274 struct mmc_ivars *ivar;
278 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
281 sc = device_get_softc(busdev);
284 panic("mmc: host bridge didn't serialize us.");
290 * Keep track of the last rca that we've selected. If
291 * we're asked to do it again, don't. We never
292 * unselect unless the bus code itself wants the mmc
293 * bus, and constantly reselecting causes problems.
295 rca = mmc_get_rca(dev);
296 if (sc->last_rca != rca) {
297 mmc_select_card(sc, rca);
299 /* Prepare bus width for the new card. */
300 ivar = device_get_ivars(dev);
301 if (bootverbose || mmc_debug) {
302 device_printf(busdev,
303 "setting bus width to %d bits\n",
304 (ivar->bus_width == bus_width_4) ? 4 :
305 (ivar->bus_width == bus_width_8) ? 8 : 1);
307 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
308 mmcbr_set_bus_width(busdev, ivar->bus_width);
309 mmcbr_update_ios(busdev);
313 * If there's a card selected, stand down.
315 if (sc->last_rca != 0) {
316 mmc_select_card(sc, 0);
325 mmc_release_bus(device_t busdev, device_t dev)
327 struct mmc_softc *sc;
330 sc = device_get_softc(busdev);
334 panic("mmc: releasing unowned bus.");
335 if (sc->owner != dev)
336 panic("mmc: you don't own the bus. game over.");
338 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
348 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
351 return (ocr & MMC_OCR_VOLTAGE);
355 mmc_highest_voltage(uint32_t ocr)
359 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
360 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
367 mmc_wakeup(struct mmc_request *req)
369 struct mmc_softc *sc;
371 sc = (struct mmc_softc *)req->done_data;
373 req->flags |= MMC_REQ_DONE;
379 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
382 req->done = mmc_wakeup;
385 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
386 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
387 if (req->cmd->data) {
388 printf(" data %d\n", (int)req->cmd->data->len);
392 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
394 while ((req->flags & MMC_REQ_DONE) == 0)
395 msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
397 if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
398 device_printf(sc->dev, "CMD%d RESULT: %d\n",
399 req->cmd->opcode, req->cmd->error);
404 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
406 struct mmc_softc *sc = device_get_softc(brdev);
408 return (mmc_wait_for_req(sc, req));
412 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
414 struct mmc_request mreq;
418 memset(&mreq, 0, sizeof(mreq));
419 memset(cmd->resp, 0, sizeof(cmd->resp));
420 cmd->retries = 0; /* Retries done here, not in hardware. */
423 if (mmc_wait_for_req(sc, &mreq) != 0)
424 err = MMC_ERR_FAILED;
427 } while (err != MMC_ERR_NONE && retries-- > 0);
433 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
434 struct mmc_command *cmd, int retries)
436 struct mmc_command appcmd;
440 memset(&appcmd, 0, sizeof(appcmd));
441 appcmd.opcode = MMC_APP_CMD;
442 appcmd.arg = rca << 16;
443 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
445 if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0)
446 err = MMC_ERR_FAILED;
449 if (err == MMC_ERR_NONE) {
450 if (!(appcmd.resp[0] & R1_APP_CMD))
451 err = MMC_ERR_FAILED;
452 else if (mmc_wait_for_cmd(sc, cmd, 0) != 0)
453 err = MMC_ERR_FAILED;
457 } while (err != MMC_ERR_NONE && retries-- > 0);
463 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
464 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
466 struct mmc_command cmd;
469 memset(&cmd, 0, sizeof(cmd));
474 err = mmc_wait_for_cmd(sc, &cmd, retries);
478 if (flags & MMC_RSP_136)
479 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
487 mmc_idle_cards(struct mmc_softc *sc)
490 struct mmc_command cmd;
493 mmcbr_set_chip_select(dev, cs_high);
494 mmcbr_update_ios(dev);
497 memset(&cmd, 0, sizeof(cmd));
498 cmd.opcode = MMC_GO_IDLE_STATE;
500 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
502 mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
505 mmcbr_set_chip_select(dev, cs_dontcare);
506 mmcbr_update_ios(dev);
511 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
513 struct mmc_command cmd;
514 int err = MMC_ERR_NONE, i;
516 memset(&cmd, 0, sizeof(cmd));
517 cmd.opcode = ACMD_SD_SEND_OP_COND;
519 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
522 for (i = 0; i < 1000; i++) {
523 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
524 if (err != MMC_ERR_NONE)
526 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
527 (ocr & MMC_OCR_VOLTAGE) == 0)
529 err = MMC_ERR_TIMEOUT;
532 if (rocr && err == MMC_ERR_NONE)
538 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
540 struct mmc_command cmd;
541 int err = MMC_ERR_NONE, i;
543 memset(&cmd, 0, sizeof(cmd));
544 cmd.opcode = MMC_SEND_OP_COND;
546 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
549 for (i = 0; i < 1000; i++) {
550 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
551 if (err != MMC_ERR_NONE)
553 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
554 (ocr & MMC_OCR_VOLTAGE) == 0)
556 err = MMC_ERR_TIMEOUT;
559 if (rocr && err == MMC_ERR_NONE)
565 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
567 struct mmc_command cmd;
570 memset(&cmd, 0, sizeof(cmd));
571 cmd.opcode = SD_SEND_IF_COND;
572 cmd.arg = (vhs << 8) + 0xAA;
573 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
576 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
581 mmc_power_up(struct mmc_softc *sc)
586 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
587 mmcbr_set_bus_mode(dev, opendrain);
588 mmcbr_set_chip_select(dev, cs_dontcare);
589 mmcbr_set_bus_width(dev, bus_width_1);
590 mmcbr_set_power_mode(dev, power_up);
591 mmcbr_set_clock(dev, 0);
592 mmcbr_update_ios(dev);
595 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
596 mmcbr_set_timing(dev, bus_timing_normal);
597 mmcbr_set_power_mode(dev, power_on);
598 mmcbr_update_ios(dev);
603 mmc_power_down(struct mmc_softc *sc)
605 device_t dev = sc->dev;
607 mmcbr_set_bus_mode(dev, opendrain);
608 mmcbr_set_chip_select(dev, cs_dontcare);
609 mmcbr_set_bus_width(dev, bus_width_1);
610 mmcbr_set_power_mode(dev, power_off);
611 mmcbr_set_clock(dev, 0);
612 mmcbr_set_timing(dev, bus_timing_normal);
613 mmcbr_update_ios(dev);
617 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
621 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
622 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
623 flags, NULL, CMD_RETRIES));
627 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
629 struct mmc_command cmd;
632 memset(&cmd, 0, sizeof(cmd));
633 cmd.opcode = MMC_SWITCH_FUNC;
634 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
638 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
640 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
645 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
649 struct mmc_command cmd;
650 struct mmc_data data;
652 memset(&cmd, 0, sizeof(cmd));
653 memset(&data, 0, sizeof(data));
656 cmd.opcode = SD_SWITCH_FUNC;
657 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
658 cmd.arg = mode << 31; /* 0 - check, 1 - set */
659 cmd.arg |= 0x00FFFFFF;
660 cmd.arg &= ~(0xF << (grp * 4));
661 cmd.arg |= value << (grp * 4);
666 data.flags = MMC_DATA_READ;
668 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
673 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
675 struct mmc_command cmd;
679 if (mmcbr_get_mode(sc->dev) == mode_sd) {
680 memset(&cmd, 0, sizeof(cmd));
681 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
682 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
683 cmd.arg = SD_CLR_CARD_DETECT;
684 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
687 memset(&cmd, 0, sizeof(cmd));
688 cmd.opcode = ACMD_SET_BUS_WIDTH;
689 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
692 cmd.arg = SD_BUS_WIDTH_1;
695 cmd.arg = SD_BUS_WIDTH_4;
698 return (MMC_ERR_INVALID);
700 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
704 value = EXT_CSD_BUS_WIDTH_1;
707 value = EXT_CSD_BUS_WIDTH_4;
710 value = EXT_CSD_BUS_WIDTH_8;
713 return (MMC_ERR_INVALID);
715 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
722 mmc_set_timing(struct mmc_softc *sc, int timing)
726 u_char switch_res[64];
729 case bus_timing_normal:
736 return (MMC_ERR_INVALID);
738 if (mmcbr_get_mode(sc->dev) == mode_sd)
739 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
742 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
743 EXT_CSD_HS_TIMING, value);
748 mmc_test_bus_width(struct mmc_softc *sc)
750 struct mmc_command cmd;
751 struct mmc_data data;
754 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
755 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
756 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, };
757 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
759 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
760 mmcbr_set_bus_width(sc->dev, bus_width_8);
761 mmcbr_update_ios(sc->dev);
763 memset(&cmd, 0, sizeof(cmd));
764 memset(&data, 0, sizeof(data));
765 cmd.opcode = MMC_BUSTEST_W;
767 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
772 data.flags = MMC_DATA_WRITE;
773 mmc_wait_for_cmd(sc, &cmd, 0);
775 memset(&cmd, 0, sizeof(cmd));
776 memset(&data, 0, sizeof(data));
777 cmd.opcode = MMC_BUSTEST_R;
779 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
784 data.flags = MMC_DATA_READ;
785 err = mmc_wait_for_cmd(sc, &cmd, 0);
787 mmcbr_set_bus_width(sc->dev, bus_width_1);
788 mmcbr_update_ios(sc->dev);
790 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
791 return (bus_width_8);
794 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
795 mmcbr_set_bus_width(sc->dev, bus_width_4);
796 mmcbr_update_ios(sc->dev);
798 memset(&cmd, 0, sizeof(cmd));
799 memset(&data, 0, sizeof(data));
800 cmd.opcode = MMC_BUSTEST_W;
802 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
807 data.flags = MMC_DATA_WRITE;
808 mmc_wait_for_cmd(sc, &cmd, 0);
810 memset(&cmd, 0, sizeof(cmd));
811 memset(&data, 0, sizeof(data));
812 cmd.opcode = MMC_BUSTEST_R;
814 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
819 data.flags = MMC_DATA_READ;
820 err = mmc_wait_for_cmd(sc, &cmd, 0);
822 mmcbr_set_bus_width(sc->dev, bus_width_1);
823 mmcbr_update_ios(sc->dev);
825 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
826 return (bus_width_4);
828 return (bus_width_1);
832 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
834 const int i = (bit_len / 32) - (start / 32) - 1;
835 const int shift = start & 31;
836 uint32_t retval = bits[i] >> shift;
837 if (size + shift > 32)
838 retval |= bits[i - 1] << (32 - shift);
839 return (retval & ((1llu << size) - 1));
843 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
847 /* There's no version info, so we take it on faith */
848 memset(cid, 0, sizeof(*cid));
849 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
850 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
851 for (i = 0; i < 5; i++)
852 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
854 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
855 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
856 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
857 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
861 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
865 /* There's no version info, so we take it on faith */
866 memset(cid, 0, sizeof(*cid));
867 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
868 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
869 for (i = 0; i < 6; i++)
870 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
872 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
873 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
874 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
875 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
879 mmc_format_card_id_string(struct mmc_ivars *ivar)
886 * Format a card ID string for use by the mmcsd driver, it's what
887 * appears between the <> in the following:
888 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
889 * 22.5MHz/4bit/128-block
891 * Also format just the card serial number, which the mmcsd driver will
892 * use as the disk->d_ident string.
894 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
895 * and our max formatted length is currently 55 bytes if every field
896 * contains the largest value.
898 * Sometimes the oid is two printable ascii chars; when it's not,
899 * format it as 0xnnnn instead.
901 c1 = (ivar->cid.oid >> 8) & 0x0ff;
902 c2 = ivar->cid.oid & 0x0ff;
903 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
904 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
906 snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
907 snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
908 "%08X", ivar->cid.psn);
909 snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
910 "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
911 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
912 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
913 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
914 ivar->cid.mid, oidstr);
917 static const int exp[8] = {
918 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
921 static const int mant[16] = {
922 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
925 static const int cur_min[8] = {
926 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
929 static const int cur_max[8] = {
930 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
934 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
940 memset(csd, 0, sizeof(*csd));
941 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
943 m = mmc_get_bits(raw_csd, 128, 115, 4);
944 e = mmc_get_bits(raw_csd, 128, 112, 3);
945 csd->tacc = (exp[e] * mant[m] + 9) / 10;
946 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
947 m = mmc_get_bits(raw_csd, 128, 99, 4);
948 e = mmc_get_bits(raw_csd, 128, 96, 3);
949 csd->tran_speed = exp[e] * 10000 * mant[m];
950 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
951 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
952 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
953 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
954 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
955 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
956 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
957 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
958 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
959 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
960 m = mmc_get_bits(raw_csd, 128, 62, 12);
961 e = mmc_get_bits(raw_csd, 128, 47, 3);
962 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
963 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
964 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
965 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
966 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
967 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
968 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
969 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
971 m = mmc_get_bits(raw_csd, 128, 115, 4);
972 e = mmc_get_bits(raw_csd, 128, 112, 3);
973 csd->tacc = (exp[e] * mant[m] + 9) / 10;
974 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
975 m = mmc_get_bits(raw_csd, 128, 99, 4);
976 e = mmc_get_bits(raw_csd, 128, 96, 3);
977 csd->tran_speed = exp[e] * 10000 * mant[m];
978 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
979 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
980 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
981 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
982 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
983 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
984 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
986 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
987 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
988 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
989 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
990 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
991 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
992 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
994 panic("unknown SD CSD version");
998 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
1003 memset(csd, 0, sizeof(*csd));
1004 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
1005 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1006 m = mmc_get_bits(raw_csd, 128, 115, 4);
1007 e = mmc_get_bits(raw_csd, 128, 112, 3);
1008 csd->tacc = exp[e] * mant[m] + 9 / 10;
1009 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1010 m = mmc_get_bits(raw_csd, 128, 99, 4);
1011 e = mmc_get_bits(raw_csd, 128, 96, 3);
1012 csd->tran_speed = exp[e] * 10000 * mant[m];
1013 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1014 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1015 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1016 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1017 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1018 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1019 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1020 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1021 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1022 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1023 m = mmc_get_bits(raw_csd, 128, 62, 12);
1024 e = mmc_get_bits(raw_csd, 128, 47, 3);
1025 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1026 csd->erase_blk_en = 0;
1027 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1028 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1029 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1030 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1031 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1032 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1033 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1037 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1039 unsigned int scr_struct;
1041 memset(scr, 0, sizeof(*scr));
1043 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1044 if (scr_struct != 0) {
1045 printf("Unrecognised SCR structure version %d\n",
1049 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1050 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1054 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1055 struct mmc_sd_status *sd_status)
1058 memset(sd_status, 0, sizeof(*sd_status));
1060 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1061 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1062 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1063 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1064 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1065 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1066 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1067 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1068 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1069 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1073 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1075 struct mmc_command cmd;
1078 memset(&cmd, 0, sizeof(cmd));
1079 cmd.opcode = MMC_ALL_SEND_CID;
1081 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1083 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1084 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1089 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1091 struct mmc_command cmd;
1094 memset(&cmd, 0, sizeof(cmd));
1095 cmd.opcode = MMC_SEND_CSD;
1096 cmd.arg = rca << 16;
1097 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1099 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1100 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1105 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1108 struct mmc_command cmd;
1109 struct mmc_data data;
1111 memset(&cmd, 0, sizeof(cmd));
1112 memset(&data, 0, sizeof(data));
1114 memset(rawscr, 0, 8);
1115 cmd.opcode = ACMD_SEND_SCR;
1116 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1122 data.flags = MMC_DATA_READ;
1124 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1125 rawscr[0] = be32toh(rawscr[0]);
1126 rawscr[1] = be32toh(rawscr[1]);
1131 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1134 struct mmc_command cmd;
1135 struct mmc_data data;
1137 memset(&cmd, 0, sizeof(cmd));
1138 memset(&data, 0, sizeof(data));
1140 memset(rawextcsd, 0, 512);
1141 cmd.opcode = MMC_SEND_EXT_CSD;
1142 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1146 data.data = rawextcsd;
1148 data.flags = MMC_DATA_READ;
1150 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1155 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1158 struct mmc_command cmd;
1159 struct mmc_data data;
1161 memset(&cmd, 0, sizeof(cmd));
1162 memset(&data, 0, sizeof(data));
1164 memset(rawsdstatus, 0, 64);
1165 cmd.opcode = ACMD_SD_STATUS;
1166 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1170 data.data = rawsdstatus;
1172 data.flags = MMC_DATA_READ;
1174 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1175 for (i = 0; i < 16; i++)
1176 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1181 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1183 struct mmc_command cmd;
1186 memset(&cmd, 0, sizeof(cmd));
1187 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1188 cmd.arg = resp << 16;
1189 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1191 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1196 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1198 struct mmc_command cmd;
1201 memset(&cmd, 0, sizeof(cmd));
1202 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1204 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1206 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1207 *resp = cmd.resp[0];
1212 mmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status)
1214 struct mmc_command cmd;
1217 memset(&cmd, 0, sizeof(cmd));
1218 cmd.opcode = MMC_SEND_STATUS;
1219 cmd.arg = rca << 16;
1220 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1222 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1223 *status = cmd.resp[0];
1228 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1230 struct mmc_command cmd;
1233 memset(&cmd, 0, sizeof(cmd));
1234 cmd.opcode = MMC_SET_BLOCKLEN;
1236 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1238 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1243 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1245 device_printf(dev, "Card at relative address 0x%04x%s:\n",
1246 ivar->rca, newcard ? " added" : "");
1247 device_printf(dev, " card: %s\n", ivar->card_id_string);
1248 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1249 (ivar->bus_width == bus_width_1 ? 1 :
1250 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1251 (ivar->timing == bus_timing_hs ?
1252 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1253 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1254 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1255 ivar->sec_count, ivar->erase_sector,
1256 ivar->read_only ? ", read-only" : "");
1260 mmc_discover_cards(struct mmc_softc *sc)
1262 struct mmc_ivars *ivar = NULL;
1264 int err, i, devcount, newcard;
1265 uint32_t raw_cid[4], resp, sec_count, status;
1268 u_char switch_res[64];
1270 if (bootverbose || mmc_debug)
1271 device_printf(sc->dev, "Probing cards\n");
1273 err = mmc_all_send_cid(sc, raw_cid);
1274 if (err == MMC_ERR_TIMEOUT)
1276 if (err != MMC_ERR_NONE) {
1277 device_printf(sc->dev, "Error reading CID %d\n", err);
1281 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1283 for (i = 0; i < devcount; i++) {
1284 ivar = device_get_ivars(devlist[i]);
1285 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1290 free(devlist, M_TEMP);
1291 if (bootverbose || mmc_debug) {
1292 device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1293 newcard ? "New c" : "C",
1294 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1297 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1299 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1301 if (mmcbr_get_ro(sc->dev))
1302 ivar->read_only = 1;
1303 ivar->bus_width = bus_width_1;
1304 ivar->timing = bus_timing_normal;
1305 ivar->mode = mmcbr_get_mode(sc->dev);
1306 if (ivar->mode == mode_sd) {
1307 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1308 mmc_send_relative_addr(sc, &resp);
1309 ivar->rca = resp >> 16;
1311 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1312 if (bootverbose || mmc_debug)
1313 device_printf(sc->dev,
1314 "%sard detected (CSD %08x%08x%08x%08x)\n",
1315 newcard ? "New c" : "C", ivar->raw_csd[0],
1316 ivar->raw_csd[1], ivar->raw_csd[2],
1318 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1319 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1320 if (ivar->csd.csd_structure > 0)
1322 ivar->tran_speed = ivar->csd.tran_speed;
1323 ivar->erase_sector = ivar->csd.erase_sector *
1324 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1326 err = mmc_send_status(sc, ivar->rca, &status);
1327 if (err != MMC_ERR_NONE) {
1328 device_printf(sc->dev,
1329 "Error reading card status %d\n", err);
1332 if ((status & R1_CARD_IS_LOCKED) != 0) {
1333 device_printf(sc->dev,
1334 "Card is password protected, skipping.\n");
1338 /* Get card SCR. Card must be selected to fetch it. */
1339 mmc_select_card(sc, ivar->rca);
1340 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1341 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1342 /* Get card switch capabilities (command class 10). */
1343 if ((ivar->scr.sda_vsn >= 1) &&
1344 (ivar->csd.ccc & (1<<10))) {
1345 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1346 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1348 if (switch_res[13] & 2) {
1349 ivar->timing = bus_timing_hs;
1350 ivar->hs_tran_speed = SD_MAX_HS;
1353 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1354 mmc_app_decode_sd_status(ivar->raw_sd_status,
1356 if (ivar->sd_status.au_size != 0) {
1357 ivar->erase_sector =
1358 16 << ivar->sd_status.au_size;
1360 mmc_select_card(sc, 0);
1361 /* Find max supported bus width. */
1362 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1363 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1364 ivar->bus_width = bus_width_4;
1367 * Some cards that report maximum I/O block sizes
1368 * greater than 512 require the block length to be
1369 * set to 512, even though that is supposed to be
1370 * the default. Example:
1372 * Transcend 2GB SDSC card, CID:
1373 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1375 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1376 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1377 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1379 mmc_format_card_id_string(ivar);
1381 if (bootverbose || mmc_debug)
1382 mmc_log_card(sc->dev, ivar, newcard);
1385 child = device_add_child(sc->dev, NULL, -1);
1386 device_set_ivars(child, ivar);
1390 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1392 mmc_set_relative_addr(sc, ivar->rca);
1394 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1395 if (bootverbose || mmc_debug)
1396 device_printf(sc->dev,
1397 "%sard detected (CSD %08x%08x%08x%08x)\n",
1398 newcard ? "New c" : "C", ivar->raw_csd[0],
1399 ivar->raw_csd[1], ivar->raw_csd[2],
1402 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1403 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1404 ivar->tran_speed = ivar->csd.tran_speed;
1405 ivar->erase_sector = ivar->csd.erase_sector *
1406 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1408 err = mmc_send_status(sc, ivar->rca, &status);
1409 if (err != MMC_ERR_NONE) {
1410 device_printf(sc->dev,
1411 "Error reading card status %d\n", err);
1414 if ((status & R1_CARD_IS_LOCKED) != 0) {
1415 device_printf(sc->dev,
1416 "Card is password protected, skipping.\n");
1420 /* Only MMC >= 4.x cards support EXT_CSD. */
1421 if (ivar->csd.spec_vers >= 4) {
1422 /* Card must be selected to fetch EXT_CSD. */
1423 mmc_select_card(sc, ivar->rca);
1424 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1425 /* Handle extended capacity from EXT_CSD */
1426 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1427 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1428 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1429 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1430 if (sec_count != 0) {
1431 ivar->sec_count = sec_count;
1434 /* Get card speed in high speed mode. */
1435 ivar->timing = bus_timing_hs;
1436 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1437 & EXT_CSD_CARD_TYPE_52)
1438 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1439 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1440 & EXT_CSD_CARD_TYPE_26)
1441 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1443 ivar->hs_tran_speed = ivar->tran_speed;
1444 /* Find max supported bus width. */
1445 ivar->bus_width = mmc_test_bus_width(sc);
1446 mmc_select_card(sc, 0);
1447 /* Handle HC erase sector size. */
1448 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1449 ivar->erase_sector = 1024 *
1450 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1451 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1452 EXT_CSD_ERASE_GRP_DEF, 1);
1455 ivar->bus_width = bus_width_1;
1456 ivar->timing = bus_timing_normal;
1460 * Some cards that report maximum I/O block sizes greater
1461 * than 512 require the block length to be set to 512, even
1462 * though that is supposed to be the default. Example:
1464 * Transcend 2GB SDSC card, CID:
1465 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1467 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1468 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1469 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1471 mmc_format_card_id_string(ivar);
1473 if (bootverbose || mmc_debug)
1474 mmc_log_card(sc->dev, ivar, newcard);
1477 child = device_add_child(sc->dev, NULL, -1);
1478 device_set_ivars(child, ivar);
1484 mmc_rescan_cards(struct mmc_softc *sc)
1486 struct mmc_ivars *ivar = NULL;
1488 int err, i, devcount;
1490 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1492 for (i = 0; i < devcount; i++) {
1493 ivar = device_get_ivars(devlist[i]);
1494 if (mmc_select_card(sc, ivar->rca)) {
1495 if (bootverbose || mmc_debug)
1496 device_printf(sc->dev, "Card at relative address %d lost.\n",
1498 device_delete_child(sc->dev, devlist[i]);
1499 free(ivar, M_DEVBUF);
1502 free(devlist, M_TEMP);
1503 mmc_select_card(sc, 0);
1507 mmc_delete_cards(struct mmc_softc *sc)
1509 struct mmc_ivars *ivar;
1511 int err, i, devcount;
1513 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1515 for (i = 0; i < devcount; i++) {
1516 ivar = device_get_ivars(devlist[i]);
1517 if (bootverbose || mmc_debug)
1518 device_printf(sc->dev, "Card at relative address %d deleted.\n",
1520 device_delete_child(sc->dev, devlist[i]);
1521 free(ivar, M_DEVBUF);
1523 free(devlist, M_TEMP);
1528 mmc_go_discovery(struct mmc_softc *sc)
1535 if (mmcbr_get_power_mode(dev) != power_on) {
1537 * First, try SD modes
1539 mmcbr_set_mode(dev, mode_sd);
1541 mmcbr_set_bus_mode(dev, pushpull);
1542 if (bootverbose || mmc_debug)
1543 device_printf(sc->dev, "Probing bus\n");
1545 err = mmc_send_if_cond(sc, 1);
1546 if ((bootverbose || mmc_debug) && err == 0)
1547 device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1548 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1549 if (bootverbose || mmc_debug)
1550 device_printf(sc->dev, "SD probe: failed\n");
1554 mmcbr_set_mode(dev, mode_mmc);
1555 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1556 if (bootverbose || mmc_debug)
1557 device_printf(sc->dev, "MMC probe: failed\n");
1558 ocr = 0; /* Failed both, powerdown. */
1559 } else if (bootverbose || mmc_debug)
1560 device_printf(sc->dev,
1561 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1562 } else if (bootverbose || mmc_debug)
1563 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1565 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1566 if (mmcbr_get_ocr(dev) != 0)
1569 mmcbr_set_bus_mode(dev, opendrain);
1570 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1571 mmcbr_update_ios(dev);
1572 /* XXX recompute vdd based on new cards? */
1575 * Make sure that we have a mutually agreeable voltage to at least
1576 * one card on the bus.
1578 if (bootverbose || mmc_debug)
1579 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1580 if (mmcbr_get_ocr(dev) == 0) {
1581 device_printf(sc->dev, "No compatible cards found on bus\n");
1582 mmc_delete_cards(sc);
1587 * Reselect the cards after we've idled them above.
1589 if (mmcbr_get_mode(dev) == mode_sd) {
1590 err = mmc_send_if_cond(sc, 1);
1591 mmc_send_app_op_cond(sc,
1592 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1594 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
1595 mmc_discover_cards(sc);
1596 mmc_rescan_cards(sc);
1598 mmcbr_set_bus_mode(dev, pushpull);
1599 mmcbr_update_ios(dev);
1600 mmc_calculate_clock(sc);
1601 bus_generic_attach(dev);
1602 /* mmc_update_children_sysctl(dev);*/
1606 mmc_calculate_clock(struct mmc_softc *sc)
1608 int max_dtr, max_hs_dtr, max_timing;
1611 struct mmc_ivars *ivar;
1613 f_max = mmcbr_get_f_max(sc->dev);
1614 max_dtr = max_hs_dtr = f_max;
1615 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1616 max_timing = bus_timing_hs;
1618 max_timing = bus_timing_normal;
1619 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1620 panic("can't get children");
1621 for (i = 0; i < nkid; i++) {
1622 ivar = device_get_ivars(kids[i]);
1623 if (ivar->timing < max_timing)
1624 max_timing = ivar->timing;
1625 if (ivar->tran_speed < max_dtr)
1626 max_dtr = ivar->tran_speed;
1627 if (ivar->hs_tran_speed < max_hs_dtr)
1628 max_hs_dtr = ivar->hs_tran_speed;
1630 for (i = 0; i < nkid; i++) {
1631 ivar = device_get_ivars(kids[i]);
1632 if (ivar->timing == bus_timing_normal)
1634 mmc_select_card(sc, ivar->rca);
1635 mmc_set_timing(sc, max_timing);
1637 mmc_select_card(sc, 0);
1639 if (max_timing == bus_timing_hs)
1640 max_dtr = max_hs_dtr;
1641 if (bootverbose || mmc_debug) {
1642 device_printf(sc->dev,
1643 "setting transfer rate to %d.%03dMHz%s\n",
1644 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1645 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1647 mmcbr_set_timing(sc->dev, max_timing);
1648 mmcbr_set_clock(sc->dev, max_dtr);
1649 mmcbr_update_ios(sc->dev);
1654 mmc_scan(struct mmc_softc *sc)
1656 device_t dev = sc->dev;
1658 mmc_acquire_bus(dev, dev);
1659 mmc_go_discovery(sc);
1660 mmc_release_bus(dev, dev);
1664 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1666 struct mmc_ivars *ivar = device_get_ivars(child);
1671 case MMC_IVAR_DSR_IMP:
1672 *result = ivar->csd.dsr_imp;
1674 case MMC_IVAR_MEDIA_SIZE:
1675 *result = ivar->sec_count;
1678 *result = ivar->rca;
1680 case MMC_IVAR_SECTOR_SIZE:
1681 *result = MMC_SECTOR_SIZE;
1683 case MMC_IVAR_TRAN_SPEED:
1684 *result = mmcbr_get_clock(bus);
1686 case MMC_IVAR_READ_ONLY:
1687 *result = ivar->read_only;
1689 case MMC_IVAR_HIGH_CAP:
1690 *result = ivar->high_cap;
1692 case MMC_IVAR_CARD_TYPE:
1693 *result = ivar->mode;
1695 case MMC_IVAR_BUS_WIDTH:
1696 *result = ivar->bus_width;
1698 case MMC_IVAR_ERASE_SECTOR:
1699 *result = ivar->erase_sector;
1701 case MMC_IVAR_MAX_DATA:
1702 *result = mmcbr_get_max_data(bus);
1704 case MMC_IVAR_CARD_ID_STRING:
1705 *(char **)result = ivar->card_id_string;
1707 case MMC_IVAR_CARD_SN_STRING:
1708 *(char **)result = ivar->card_sn_string;
1715 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1718 * None are writable ATM
1724 mmc_delayed_attach(void *xsc)
1726 struct mmc_softc *sc = xsc;
1729 config_intrhook_disestablish(&sc->config_intrhook);
1733 mmc_child_location_str(device_t dev, device_t child, char *buf,
1737 snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1741 static device_method_t mmc_methods[] = {
1743 DEVMETHOD(device_probe, mmc_probe),
1744 DEVMETHOD(device_attach, mmc_attach),
1745 DEVMETHOD(device_detach, mmc_detach),
1746 DEVMETHOD(device_suspend, mmc_suspend),
1747 DEVMETHOD(device_resume, mmc_resume),
1750 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1751 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1752 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1754 /* MMC Bus interface */
1755 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1756 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1757 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1762 static driver_t mmc_driver = {
1765 sizeof(struct mmc_softc),
1767 static devclass_t mmc_devclass;
1769 DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL);
1770 DRIVER_MODULE(mmc, sdhci_bcm, mmc_driver, mmc_devclass, NULL, NULL);
1771 DRIVER_MODULE(mmc, sdhci_fdt, mmc_driver, mmc_devclass, NULL, NULL);
1772 DRIVER_MODULE(mmc, sdhci_imx, mmc_driver, mmc_devclass, NULL, NULL);
1773 DRIVER_MODULE(mmc, sdhci_pci, mmc_driver, mmc_devclass, NULL, NULL);
1774 DRIVER_MODULE(mmc, sdhci_ti, mmc_driver, mmc_devclass, NULL, NULL);
1775 DRIVER_MODULE(mmc, ti_mmchs, mmc_driver, mmc_devclass, NULL, NULL);