2 * Copyright (c) 2006 Bernd Walter. All rights reserved.
3 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * Portions of this software may have been developed with reference to
26 * the SD Simplified Specification. The following disclaimer may apply:
28 * The following conditions apply to the release of the simplified
29 * specification ("Simplified Specification") by the SD Card Association and
30 * the SD Group. The Simplified Specification is a subset of the complete SD
31 * Specification which is owned by the SD Card Association and the SD
32 * Group. This Simplified Specification is provided on a non-confidential
33 * basis subject to the disclaimers below. Any implementation of the
34 * Simplified Specification may require a license from the SD Card
35 * Association, SD Group, SD-3C LLC or other third parties.
39 * The information contained in the Simplified Specification is presented only
40 * as a standard specification for SD Cards and SD Host/Ancillary products and
41 * is provided "AS-IS" without any representations or warranties of any
42 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43 * Card Association for any damages, any infringements of patents or other
44 * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45 * parties, which may result from its use. No license is granted by
46 * implication, estoppel or otherwise under any patent or other rights of the
47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49 * or the SD Card Association to disclose or distribute any technical
50 * information, know-how or other confidential information to any third party.
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/module.h>
62 #include <sys/mutex.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
68 #include <dev/mmc/mmcreg.h>
69 #include <dev/mmc/mmcbrvar.h>
70 #include <dev/mmc/mmcvar.h>
72 #include "mmcbus_if.h"
77 struct intr_config_hook config_intrhook;
80 int squelched; /* suppress reporting of (expected) errors */
82 struct timeval log_time;
85 #define LOG_PPS 5 /* Log no more than 5 errors per second. */
91 uint32_t raw_cid[4]; /* Raw bits of the CID */
92 uint32_t raw_csd[4]; /* Raw bits of the CSD */
93 uint32_t raw_scr[2]; /* Raw bits of the SCR */
94 uint8_t raw_ext_csd[512]; /* Raw bits of the EXT_CSD */
95 uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */
97 enum mmc_card_mode mode;
98 struct mmc_cid cid; /* cid decoded */
99 struct mmc_csd csd; /* csd decoded */
100 struct mmc_scr scr; /* scr decoded */
101 struct mmc_sd_status sd_status; /* SD_STATUS decoded */
102 u_char read_only; /* True when the device is read-only */
103 u_char bus_width; /* Bus width to use */
104 u_char timing; /* Bus timing support */
105 u_char high_cap; /* High Capacity card (block addressed) */
106 uint32_t sec_count; /* Card capacity in 512byte blocks */
107 uint32_t tran_speed; /* Max speed in normal mode */
108 uint32_t hs_tran_speed; /* Max speed in high speed mode */
109 uint32_t erase_sector; /* Card native erase sector size */
110 char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
111 char card_sn_string[16];/* Formatted serial # for disk->d_ident */
114 #define CMD_RETRIES 3
116 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
118 static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
120 static int mmc_debug;
121 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &mmc_debug, 0,
124 /* bus entry points */
125 static int mmc_acquire_bus(device_t busdev, device_t dev);
126 static int mmc_attach(device_t dev);
127 static int mmc_child_location_str(device_t dev, device_t child, char *buf,
129 static int mmc_detach(device_t dev);
130 static int mmc_probe(device_t dev);
131 static int mmc_read_ivar(device_t bus, device_t child, int which,
133 static int mmc_release_bus(device_t busdev, device_t dev);
134 static int mmc_resume(device_t dev);
135 static int mmc_suspend(device_t dev);
136 static int mmc_wait_for_request(device_t brdev, device_t reqdev,
137 struct mmc_request *req);
138 static int mmc_write_ivar(device_t bus, device_t child, int which,
141 #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
142 #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
143 #define MMC_LOCK_INIT(_sc) \
144 mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->dev), \
146 #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx);
147 #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED);
148 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED);
150 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
151 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
152 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
153 struct mmc_sd_status *sd_status);
154 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
155 uint32_t *rawsdstatus);
156 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
158 static int mmc_calculate_clock(struct mmc_softc *sc);
159 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid);
160 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
161 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
162 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
163 static void mmc_delayed_attach(void *xsc);
164 static int mmc_delete_cards(struct mmc_softc *sc);
165 static void mmc_discover_cards(struct mmc_softc *sc);
166 static void mmc_format_card_id_string(struct mmc_ivars *ivar);
167 static void mmc_go_discovery(struct mmc_softc *sc);
168 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
170 static int mmc_highest_voltage(uint32_t ocr);
171 static void mmc_idle_cards(struct mmc_softc *sc);
172 static void mmc_ms_delay(int ms);
173 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
174 static void mmc_power_down(struct mmc_softc *sc);
175 static void mmc_power_up(struct mmc_softc *sc);
176 static void mmc_rescan_cards(struct mmc_softc *sc);
177 static void mmc_scan(struct mmc_softc *sc);
178 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
179 uint8_t value, uint8_t *res);
180 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
181 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
182 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
184 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
185 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
186 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
187 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
189 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
190 static int mmc_send_status(struct mmc_softc *sc, uint16_t rca,
192 static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
193 static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca,
195 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
196 static int mmc_set_timing(struct mmc_softc *sc, int timing);
197 static int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index,
199 static int mmc_test_bus_width(struct mmc_softc *sc);
200 static int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
201 struct mmc_command *cmd, int retries);
202 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
204 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
205 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
206 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
207 static void mmc_wakeup(struct mmc_request *req);
213 DELAY(1000 * ms); /* XXX BAD */
217 mmc_probe(device_t dev)
220 device_set_desc(dev, "MMC/SD bus");
225 mmc_attach(device_t dev)
227 struct mmc_softc *sc;
229 sc = device_get_softc(dev);
233 /* We'll probe and attach our children later, but before / mount */
234 sc->config_intrhook.ich_func = mmc_delayed_attach;
235 sc->config_intrhook.ich_arg = sc;
236 if (config_intrhook_establish(&sc->config_intrhook) != 0)
237 device_printf(dev, "config_intrhook_establish failed\n");
242 mmc_detach(device_t dev)
244 struct mmc_softc *sc = device_get_softc(dev);
247 if ((err = mmc_delete_cards(sc)) != 0)
250 MMC_LOCK_DESTROY(sc);
256 mmc_suspend(device_t dev)
258 struct mmc_softc *sc = device_get_softc(dev);
261 err = bus_generic_suspend(dev);
269 mmc_resume(device_t dev)
271 struct mmc_softc *sc = device_get_softc(dev);
274 return (bus_generic_resume(dev));
278 mmc_acquire_bus(device_t busdev, device_t dev)
280 struct mmc_softc *sc;
281 struct mmc_ivars *ivar;
285 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
288 sc = device_get_softc(busdev);
291 panic("mmc: host bridge didn't serialize us.");
297 * Keep track of the last rca that we've selected. If
298 * we're asked to do it again, don't. We never
299 * unselect unless the bus code itself wants the mmc
300 * bus, and constantly reselecting causes problems.
302 rca = mmc_get_rca(dev);
303 if (sc->last_rca != rca) {
304 mmc_select_card(sc, rca);
306 /* Prepare bus width for the new card. */
307 ivar = device_get_ivars(dev);
308 if (bootverbose || mmc_debug) {
309 device_printf(busdev,
310 "setting bus width to %d bits\n",
311 (ivar->bus_width == bus_width_4) ? 4 :
312 (ivar->bus_width == bus_width_8) ? 8 : 1);
314 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
315 mmcbr_set_bus_width(busdev, ivar->bus_width);
316 mmcbr_update_ios(busdev);
320 * If there's a card selected, stand down.
322 if (sc->last_rca != 0) {
323 mmc_select_card(sc, 0);
332 mmc_release_bus(device_t busdev, device_t dev)
334 struct mmc_softc *sc;
337 sc = device_get_softc(busdev);
341 panic("mmc: releasing unowned bus.");
342 if (sc->owner != dev)
343 panic("mmc: you don't own the bus. game over.");
345 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
355 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
358 return (ocr & MMC_OCR_VOLTAGE);
362 mmc_highest_voltage(uint32_t ocr)
366 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
367 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
374 mmc_wakeup(struct mmc_request *req)
376 struct mmc_softc *sc;
378 sc = (struct mmc_softc *)req->done_data;
380 req->flags |= MMC_REQ_DONE;
386 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
389 req->done = mmc_wakeup;
392 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
393 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
394 if (req->cmd->data) {
395 printf(" data %d\n", (int)req->cmd->data->len);
399 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
401 while ((req->flags & MMC_REQ_DONE) == 0)
402 msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
404 if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
405 device_printf(sc->dev, "CMD%d RESULT: %d\n",
406 req->cmd->opcode, req->cmd->error);
411 mmc_wait_for_request(device_t brdev, device_t reqdev __unused,
412 struct mmc_request *req)
414 struct mmc_softc *sc = device_get_softc(brdev);
416 return (mmc_wait_for_req(sc, req));
420 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
422 struct mmc_request mreq;
426 memset(&mreq, 0, sizeof(mreq));
427 memset(cmd->resp, 0, sizeof(cmd->resp));
428 cmd->retries = 0; /* Retries done here, not in hardware. */
431 if (mmc_wait_for_req(sc, &mreq) != 0)
432 err = MMC_ERR_FAILED;
435 } while (err != MMC_ERR_NONE && retries-- > 0);
437 if (err != MMC_ERR_NONE && sc->squelched == 0) {
438 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
439 device_printf(sc->dev, "CMD%d failed, RESULT: %d\n",
448 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
449 struct mmc_command *cmd, int retries)
451 struct mmc_command appcmd;
454 /* Squelch error reporting at lower levels, we report below. */
457 memset(&appcmd, 0, sizeof(appcmd));
458 appcmd.opcode = MMC_APP_CMD;
459 appcmd.arg = rca << 16;
460 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
462 if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0)
463 err = MMC_ERR_FAILED;
466 if (err == MMC_ERR_NONE) {
467 if (!(appcmd.resp[0] & R1_APP_CMD))
468 err = MMC_ERR_FAILED;
469 else if (mmc_wait_for_cmd(sc, cmd, 0) != 0)
470 err = MMC_ERR_FAILED;
474 } while (err != MMC_ERR_NONE && retries-- > 0);
477 if (err != MMC_ERR_NONE && sc->squelched == 0) {
478 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
479 device_printf(sc->dev, "ACMD%d failed, RESULT: %d\n",
488 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
489 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
491 struct mmc_command cmd;
494 memset(&cmd, 0, sizeof(cmd));
499 err = mmc_wait_for_cmd(sc, &cmd, retries);
503 if (flags & MMC_RSP_136)
504 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
512 mmc_idle_cards(struct mmc_softc *sc)
515 struct mmc_command cmd;
518 mmcbr_set_chip_select(dev, cs_high);
519 mmcbr_update_ios(dev);
522 memset(&cmd, 0, sizeof(cmd));
523 cmd.opcode = MMC_GO_IDLE_STATE;
525 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
527 mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
530 mmcbr_set_chip_select(dev, cs_dontcare);
531 mmcbr_update_ios(dev);
536 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
538 struct mmc_command cmd;
539 int err = MMC_ERR_NONE, i;
541 memset(&cmd, 0, sizeof(cmd));
542 cmd.opcode = ACMD_SD_SEND_OP_COND;
544 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
547 for (i = 0; i < 1000; i++) {
548 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
549 if (err != MMC_ERR_NONE)
551 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
552 (ocr & MMC_OCR_VOLTAGE) == 0)
554 err = MMC_ERR_TIMEOUT;
557 if (rocr && err == MMC_ERR_NONE)
563 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
565 struct mmc_command cmd;
566 int err = MMC_ERR_NONE, i;
568 memset(&cmd, 0, sizeof(cmd));
569 cmd.opcode = MMC_SEND_OP_COND;
571 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
574 for (i = 0; i < 1000; i++) {
575 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
576 if (err != MMC_ERR_NONE)
578 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
579 (ocr & MMC_OCR_VOLTAGE) == 0)
581 err = MMC_ERR_TIMEOUT;
584 if (rocr && err == MMC_ERR_NONE)
590 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
592 struct mmc_command cmd;
595 memset(&cmd, 0, sizeof(cmd));
596 cmd.opcode = SD_SEND_IF_COND;
597 cmd.arg = (vhs << 8) + 0xAA;
598 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
601 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
606 mmc_power_up(struct mmc_softc *sc)
611 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
612 mmcbr_set_bus_mode(dev, opendrain);
613 mmcbr_set_chip_select(dev, cs_dontcare);
614 mmcbr_set_bus_width(dev, bus_width_1);
615 mmcbr_set_power_mode(dev, power_up);
616 mmcbr_set_clock(dev, 0);
617 mmcbr_update_ios(dev);
620 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
621 mmcbr_set_timing(dev, bus_timing_normal);
622 mmcbr_set_power_mode(dev, power_on);
623 mmcbr_update_ios(dev);
628 mmc_power_down(struct mmc_softc *sc)
630 device_t dev = sc->dev;
632 mmcbr_set_bus_mode(dev, opendrain);
633 mmcbr_set_chip_select(dev, cs_dontcare);
634 mmcbr_set_bus_width(dev, bus_width_1);
635 mmcbr_set_power_mode(dev, power_off);
636 mmcbr_set_clock(dev, 0);
637 mmcbr_set_timing(dev, bus_timing_normal);
638 mmcbr_update_ios(dev);
642 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
646 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
647 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
648 flags, NULL, CMD_RETRIES));
652 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
654 struct mmc_command cmd;
657 memset(&cmd, 0, sizeof(cmd));
658 cmd.opcode = MMC_SWITCH_FUNC;
659 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
663 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
665 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
670 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
674 struct mmc_command cmd;
675 struct mmc_data data;
677 memset(&cmd, 0, sizeof(cmd));
678 memset(&data, 0, sizeof(data));
681 cmd.opcode = SD_SWITCH_FUNC;
682 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
683 cmd.arg = mode << 31; /* 0 - check, 1 - set */
684 cmd.arg |= 0x00FFFFFF;
685 cmd.arg &= ~(0xF << (grp * 4));
686 cmd.arg |= value << (grp * 4);
691 data.flags = MMC_DATA_READ;
693 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
698 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
700 struct mmc_command cmd;
704 if (mmcbr_get_mode(sc->dev) == mode_sd) {
705 memset(&cmd, 0, sizeof(cmd));
706 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
707 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
708 cmd.arg = SD_CLR_CARD_DETECT;
709 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
712 memset(&cmd, 0, sizeof(cmd));
713 cmd.opcode = ACMD_SET_BUS_WIDTH;
714 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
717 cmd.arg = SD_BUS_WIDTH_1;
720 cmd.arg = SD_BUS_WIDTH_4;
723 return (MMC_ERR_INVALID);
725 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
729 value = EXT_CSD_BUS_WIDTH_1;
732 value = EXT_CSD_BUS_WIDTH_4;
735 value = EXT_CSD_BUS_WIDTH_8;
738 return (MMC_ERR_INVALID);
740 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
747 mmc_set_timing(struct mmc_softc *sc, int timing)
749 u_char switch_res[64];
754 case bus_timing_normal:
761 return (MMC_ERR_INVALID);
763 if (mmcbr_get_mode(sc->dev) == mode_sd)
764 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
767 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
768 EXT_CSD_HS_TIMING, value);
772 static const uint8_t p8[8] = {
773 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
776 static const uint8_t p8ok[8] = {
777 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
780 static const uint8_t p4[4] = {
781 0x5A, 0x00, 0x00, 0x00
784 static const uint8_t p4ok[4] = {
785 0xA5, 0x00, 0x00, 0x00
789 mmc_test_bus_width(struct mmc_softc *sc)
791 struct mmc_command cmd;
792 struct mmc_data data;
796 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
797 mmcbr_set_bus_width(sc->dev, bus_width_8);
798 mmcbr_update_ios(sc->dev);
800 sc->squelched++; /* Errors are expected, squelch reporting. */
801 memset(&cmd, 0, sizeof(cmd));
802 memset(&data, 0, sizeof(data));
803 cmd.opcode = MMC_BUSTEST_W;
805 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
808 data.data = __DECONST(void *, p8);
810 data.flags = MMC_DATA_WRITE;
811 mmc_wait_for_cmd(sc, &cmd, 0);
813 memset(&cmd, 0, sizeof(cmd));
814 memset(&data, 0, sizeof(data));
815 cmd.opcode = MMC_BUSTEST_R;
817 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
822 data.flags = MMC_DATA_READ;
823 err = mmc_wait_for_cmd(sc, &cmd, 0);
826 mmcbr_set_bus_width(sc->dev, bus_width_1);
827 mmcbr_update_ios(sc->dev);
829 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
830 return (bus_width_8);
833 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
834 mmcbr_set_bus_width(sc->dev, bus_width_4);
835 mmcbr_update_ios(sc->dev);
837 sc->squelched++; /* Errors are expected, squelch reporting. */
838 memset(&cmd, 0, sizeof(cmd));
839 memset(&data, 0, sizeof(data));
840 cmd.opcode = MMC_BUSTEST_W;
842 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
845 data.data = __DECONST(void *, p4);
847 data.flags = MMC_DATA_WRITE;
848 mmc_wait_for_cmd(sc, &cmd, 0);
850 memset(&cmd, 0, sizeof(cmd));
851 memset(&data, 0, sizeof(data));
852 cmd.opcode = MMC_BUSTEST_R;
854 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
859 data.flags = MMC_DATA_READ;
860 err = mmc_wait_for_cmd(sc, &cmd, 0);
863 mmcbr_set_bus_width(sc->dev, bus_width_1);
864 mmcbr_update_ios(sc->dev);
866 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
867 return (bus_width_4);
869 return (bus_width_1);
873 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
875 const int i = (bit_len / 32) - (start / 32) - 1;
876 const int shift = start & 31;
877 uint32_t retval = bits[i] >> shift;
878 if (size + shift > 32)
879 retval |= bits[i - 1] << (32 - shift);
880 return (retval & ((1llu << size) - 1));
884 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
888 /* There's no version info, so we take it on faith */
889 memset(cid, 0, sizeof(*cid));
890 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
891 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
892 for (i = 0; i < 5; i++)
893 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
895 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
896 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
897 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
898 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
902 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
906 /* There's no version info, so we take it on faith */
907 memset(cid, 0, sizeof(*cid));
908 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
909 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
910 for (i = 0; i < 6; i++)
911 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
913 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
914 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
915 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
916 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
920 mmc_format_card_id_string(struct mmc_ivars *ivar)
927 * Format a card ID string for use by the mmcsd driver, it's what
928 * appears between the <> in the following:
929 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
930 * 22.5MHz/4bit/128-block
932 * Also format just the card serial number, which the mmcsd driver will
933 * use as the disk->d_ident string.
935 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
936 * and our max formatted length is currently 55 bytes if every field
937 * contains the largest value.
939 * Sometimes the oid is two printable ascii chars; when it's not,
940 * format it as 0xnnnn instead.
942 c1 = (ivar->cid.oid >> 8) & 0x0ff;
943 c2 = ivar->cid.oid & 0x0ff;
944 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
945 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
947 snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
948 snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
949 "%08X", ivar->cid.psn);
950 snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
951 "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
952 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
953 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
954 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
955 ivar->cid.mid, oidstr);
958 static const int exp[8] = {
959 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
962 static const int mant[16] = {
963 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
966 static const int cur_min[8] = {
967 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
970 static const int cur_max[8] = {
971 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
975 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
981 memset(csd, 0, sizeof(*csd));
982 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
984 m = mmc_get_bits(raw_csd, 128, 115, 4);
985 e = mmc_get_bits(raw_csd, 128, 112, 3);
986 csd->tacc = (exp[e] * mant[m] + 9) / 10;
987 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
988 m = mmc_get_bits(raw_csd, 128, 99, 4);
989 e = mmc_get_bits(raw_csd, 128, 96, 3);
990 csd->tran_speed = exp[e] * 10000 * mant[m];
991 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
992 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
993 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
994 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
995 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
996 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
997 csd->vdd_r_curr_min =
998 cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
999 csd->vdd_r_curr_max =
1000 cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1001 csd->vdd_w_curr_min =
1002 cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1003 csd->vdd_w_curr_max =
1004 cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1005 m = mmc_get_bits(raw_csd, 128, 62, 12);
1006 e = mmc_get_bits(raw_csd, 128, 47, 3);
1007 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1008 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1009 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1010 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1011 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1012 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1013 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1014 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1015 } else if (v == 1) {
1016 m = mmc_get_bits(raw_csd, 128, 115, 4);
1017 e = mmc_get_bits(raw_csd, 128, 112, 3);
1018 csd->tacc = (exp[e] * mant[m] + 9) / 10;
1019 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1020 m = mmc_get_bits(raw_csd, 128, 99, 4);
1021 e = mmc_get_bits(raw_csd, 128, 96, 3);
1022 csd->tran_speed = exp[e] * 10000 * mant[m];
1023 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1024 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1025 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1026 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1027 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1028 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1029 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) +
1031 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1032 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1033 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1034 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1035 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1036 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1037 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1039 panic("unknown SD CSD version");
1043 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
1048 memset(csd, 0, sizeof(*csd));
1049 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
1050 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1051 m = mmc_get_bits(raw_csd, 128, 115, 4);
1052 e = mmc_get_bits(raw_csd, 128, 112, 3);
1053 csd->tacc = exp[e] * mant[m] + 9 / 10;
1054 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1055 m = mmc_get_bits(raw_csd, 128, 99, 4);
1056 e = mmc_get_bits(raw_csd, 128, 96, 3);
1057 csd->tran_speed = exp[e] * 10000 * mant[m];
1058 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1059 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1060 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1061 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1062 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1063 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1064 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1065 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1066 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1067 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1068 m = mmc_get_bits(raw_csd, 128, 62, 12);
1069 e = mmc_get_bits(raw_csd, 128, 47, 3);
1070 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1071 csd->erase_blk_en = 0;
1072 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1073 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1074 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1075 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1076 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1077 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1078 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1082 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1084 unsigned int scr_struct;
1086 memset(scr, 0, sizeof(*scr));
1088 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1089 if (scr_struct != 0) {
1090 printf("Unrecognised SCR structure version %d\n",
1094 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1095 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1099 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1100 struct mmc_sd_status *sd_status)
1103 memset(sd_status, 0, sizeof(*sd_status));
1105 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1106 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1107 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1108 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1109 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1110 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1111 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1112 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1113 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1114 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1118 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1120 struct mmc_command cmd;
1123 memset(&cmd, 0, sizeof(cmd));
1124 cmd.opcode = MMC_ALL_SEND_CID;
1126 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1128 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1129 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1134 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1136 struct mmc_command cmd;
1139 memset(&cmd, 0, sizeof(cmd));
1140 cmd.opcode = MMC_SEND_CSD;
1141 cmd.arg = rca << 16;
1142 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1144 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1145 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1150 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1153 struct mmc_command cmd;
1154 struct mmc_data data;
1156 memset(&cmd, 0, sizeof(cmd));
1157 memset(&data, 0, sizeof(data));
1159 memset(rawscr, 0, 8);
1160 cmd.opcode = ACMD_SEND_SCR;
1161 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1167 data.flags = MMC_DATA_READ;
1169 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1170 rawscr[0] = be32toh(rawscr[0]);
1171 rawscr[1] = be32toh(rawscr[1]);
1176 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1178 struct mmc_command cmd;
1179 struct mmc_data data;
1182 memset(&cmd, 0, sizeof(cmd));
1183 memset(&data, 0, sizeof(data));
1185 memset(rawextcsd, 0, 512);
1186 cmd.opcode = MMC_SEND_EXT_CSD;
1187 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1191 data.data = rawextcsd;
1193 data.flags = MMC_DATA_READ;
1195 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1200 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1202 struct mmc_command cmd;
1203 struct mmc_data data;
1206 memset(&cmd, 0, sizeof(cmd));
1207 memset(&data, 0, sizeof(data));
1209 memset(rawsdstatus, 0, 64);
1210 cmd.opcode = ACMD_SD_STATUS;
1211 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1215 data.data = rawsdstatus;
1217 data.flags = MMC_DATA_READ;
1219 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1220 for (i = 0; i < 16; i++)
1221 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1226 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1228 struct mmc_command cmd;
1231 memset(&cmd, 0, sizeof(cmd));
1232 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1233 cmd.arg = resp << 16;
1234 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1236 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1241 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1243 struct mmc_command cmd;
1246 memset(&cmd, 0, sizeof(cmd));
1247 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1249 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1251 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1252 *resp = cmd.resp[0];
1257 mmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status)
1259 struct mmc_command cmd;
1262 memset(&cmd, 0, sizeof(cmd));
1263 cmd.opcode = MMC_SEND_STATUS;
1264 cmd.arg = rca << 16;
1265 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1267 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1268 *status = cmd.resp[0];
1273 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1275 struct mmc_command cmd;
1278 memset(&cmd, 0, sizeof(cmd));
1279 cmd.opcode = MMC_SET_BLOCKLEN;
1281 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1283 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1288 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1290 device_printf(dev, "Card at relative address 0x%04x%s:\n",
1291 ivar->rca, newcard ? " added" : "");
1292 device_printf(dev, " card: %s\n", ivar->card_id_string);
1293 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1294 (ivar->bus_width == bus_width_1 ? 1 :
1295 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1296 (ivar->timing == bus_timing_hs ?
1297 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1298 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1299 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1300 ivar->sec_count, ivar->erase_sector,
1301 ivar->read_only ? ", read-only" : "");
1305 mmc_discover_cards(struct mmc_softc *sc)
1307 u_char switch_res[64];
1308 uint32_t raw_cid[4];
1309 struct mmc_ivars *ivar = NULL;
1312 int err, i, devcount, newcard;
1313 uint32_t resp, sec_count, status;
1316 if (bootverbose || mmc_debug)
1317 device_printf(sc->dev, "Probing cards\n");
1319 sc->squelched++; /* Errors are expected, squelch reporting. */
1320 err = mmc_all_send_cid(sc, raw_cid);
1322 if (err == MMC_ERR_TIMEOUT)
1324 if (err != MMC_ERR_NONE) {
1325 device_printf(sc->dev, "Error reading CID %d\n", err);
1329 if ((err = device_get_children(sc->dev, &devlist,
1332 for (i = 0; i < devcount; i++) {
1333 ivar = device_get_ivars(devlist[i]);
1334 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) ==
1340 free(devlist, M_TEMP);
1341 if (bootverbose || mmc_debug) {
1342 device_printf(sc->dev,
1343 "%sard detected (CID %08x%08x%08x%08x)\n",
1344 newcard ? "New c" : "C",
1345 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1348 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1350 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1352 if (mmcbr_get_ro(sc->dev))
1353 ivar->read_only = 1;
1354 ivar->bus_width = bus_width_1;
1355 ivar->timing = bus_timing_normal;
1356 ivar->mode = mmcbr_get_mode(sc->dev);
1357 if (ivar->mode == mode_sd) {
1358 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1359 mmc_send_relative_addr(sc, &resp);
1360 ivar->rca = resp >> 16;
1362 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1363 if (bootverbose || mmc_debug)
1364 device_printf(sc->dev,
1365 "%sard detected (CSD %08x%08x%08x%08x)\n",
1366 newcard ? "New c" : "C", ivar->raw_csd[0],
1367 ivar->raw_csd[1], ivar->raw_csd[2],
1369 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1370 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1371 if (ivar->csd.csd_structure > 0)
1373 ivar->tran_speed = ivar->csd.tran_speed;
1374 ivar->erase_sector = ivar->csd.erase_sector *
1375 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1377 err = mmc_send_status(sc, ivar->rca, &status);
1378 if (err != MMC_ERR_NONE) {
1379 device_printf(sc->dev,
1380 "Error reading card status %d\n", err);
1383 if ((status & R1_CARD_IS_LOCKED) != 0) {
1384 device_printf(sc->dev,
1385 "Card is password protected, skipping.\n");
1389 /* Get card SCR. Card must be selected to fetch it. */
1390 mmc_select_card(sc, ivar->rca);
1391 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1392 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1393 /* Get card switch capabilities (command class 10). */
1394 if ((ivar->scr.sda_vsn >= 1) &&
1395 (ivar->csd.ccc & (1 << 10))) {
1396 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1397 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1399 if (switch_res[13] & 2) {
1400 ivar->timing = bus_timing_hs;
1401 ivar->hs_tran_speed = SD_MAX_HS;
1406 * We deselect then reselect the card here. Some cards
1407 * become unselected and timeout with the above two
1408 * commands, although the state tables / diagrams in the
1409 * standard suggest they go back to the transfer state.
1410 * Other cards don't become deselected, and if we
1411 * attempt to blindly re-select them, we get timeout
1412 * errors from some controllers. So we deselect then
1413 * reselect to handle all situations. The only thing we
1414 * use from the sd_status is the erase sector size, but
1415 * it is still nice to get that right.
1417 mmc_select_card(sc, 0);
1418 mmc_select_card(sc, ivar->rca);
1419 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1420 mmc_app_decode_sd_status(ivar->raw_sd_status,
1422 if (ivar->sd_status.au_size != 0) {
1423 ivar->erase_sector =
1424 16 << ivar->sd_status.au_size;
1426 /* Find max supported bus width. */
1427 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1428 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1429 ivar->bus_width = bus_width_4;
1432 * Some cards that report maximum I/O block sizes
1433 * greater than 512 require the block length to be
1434 * set to 512, even though that is supposed to be
1435 * the default. Example:
1437 * Transcend 2GB SDSC card, CID:
1438 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1440 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1441 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1442 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1444 mmc_format_card_id_string(ivar);
1446 if (bootverbose || mmc_debug)
1447 mmc_log_card(sc->dev, ivar, newcard);
1450 child = device_add_child(sc->dev, NULL, -1);
1451 device_set_ivars(child, ivar);
1453 mmc_select_card(sc, 0);
1456 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1458 mmc_set_relative_addr(sc, ivar->rca);
1460 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1461 if (bootverbose || mmc_debug)
1462 device_printf(sc->dev,
1463 "%sard detected (CSD %08x%08x%08x%08x)\n",
1464 newcard ? "New c" : "C", ivar->raw_csd[0],
1465 ivar->raw_csd[1], ivar->raw_csd[2],
1468 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1469 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1470 ivar->tran_speed = ivar->csd.tran_speed;
1471 ivar->erase_sector = ivar->csd.erase_sector *
1472 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1474 err = mmc_send_status(sc, ivar->rca, &status);
1475 if (err != MMC_ERR_NONE) {
1476 device_printf(sc->dev,
1477 "Error reading card status %d\n", err);
1480 if ((status & R1_CARD_IS_LOCKED) != 0) {
1481 device_printf(sc->dev,
1482 "Card is password protected, skipping.\n");
1486 mmc_select_card(sc, ivar->rca);
1488 /* Only MMC >= 4.x cards support EXT_CSD. */
1489 if (ivar->csd.spec_vers >= 4) {
1490 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1491 /* Handle extended capacity from EXT_CSD */
1492 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1493 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1494 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1495 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1496 if (sec_count != 0) {
1497 ivar->sec_count = sec_count;
1500 /* Get card speed in high speed mode. */
1501 ivar->timing = bus_timing_hs;
1502 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1503 & EXT_CSD_CARD_TYPE_52)
1504 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1505 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1506 & EXT_CSD_CARD_TYPE_26)
1507 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1509 ivar->hs_tran_speed = ivar->tran_speed;
1510 /* Find max supported bus width. */
1511 ivar->bus_width = mmc_test_bus_width(sc);
1512 /* Handle HC erase sector size. */
1513 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1514 ivar->erase_sector = 1024 *
1515 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1516 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1517 EXT_CSD_ERASE_GRP_DEF, 1);
1520 ivar->bus_width = bus_width_1;
1521 ivar->timing = bus_timing_normal;
1525 * Some cards that report maximum I/O block sizes greater
1526 * than 512 require the block length to be set to 512, even
1527 * though that is supposed to be the default. Example:
1529 * Transcend 2GB SDSC card, CID:
1530 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1532 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1533 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1534 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1536 mmc_format_card_id_string(ivar);
1538 if (bootverbose || mmc_debug)
1539 mmc_log_card(sc->dev, ivar, newcard);
1542 child = device_add_child(sc->dev, NULL, -1);
1543 device_set_ivars(child, ivar);
1545 mmc_select_card(sc, 0);
1550 mmc_rescan_cards(struct mmc_softc *sc)
1552 struct mmc_ivars *ivar;
1554 int err, i, devcount;
1556 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1558 for (i = 0; i < devcount; i++) {
1559 ivar = device_get_ivars(devlist[i]);
1560 if (mmc_select_card(sc, ivar->rca)) {
1561 if (bootverbose || mmc_debug)
1562 device_printf(sc->dev,
1563 "Card at relative address %d lost.\n",
1565 device_delete_child(sc->dev, devlist[i]);
1566 free(ivar, M_DEVBUF);
1569 free(devlist, M_TEMP);
1570 mmc_select_card(sc, 0);
1574 mmc_delete_cards(struct mmc_softc *sc)
1576 struct mmc_ivars *ivar;
1578 int err, i, devcount;
1580 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1582 for (i = 0; i < devcount; i++) {
1583 ivar = device_get_ivars(devlist[i]);
1584 if (bootverbose || mmc_debug)
1585 device_printf(sc->dev,
1586 "Card at relative address %d deleted.\n",
1588 device_delete_child(sc->dev, devlist[i]);
1589 free(ivar, M_DEVBUF);
1591 free(devlist, M_TEMP);
1596 mmc_go_discovery(struct mmc_softc *sc)
1603 if (mmcbr_get_power_mode(dev) != power_on) {
1605 * First, try SD modes
1607 sc->squelched++; /* Errors are expected, squelch reporting. */
1608 mmcbr_set_mode(dev, mode_sd);
1610 mmcbr_set_bus_mode(dev, pushpull);
1611 if (bootverbose || mmc_debug)
1612 device_printf(sc->dev, "Probing bus\n");
1614 err = mmc_send_if_cond(sc, 1);
1615 if ((bootverbose || mmc_debug) && err == 0)
1616 device_printf(sc->dev,
1617 "SD 2.0 interface conditions: OK\n");
1618 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1619 if (bootverbose || mmc_debug)
1620 device_printf(sc->dev, "SD probe: failed\n");
1624 mmcbr_set_mode(dev, mode_mmc);
1625 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1626 if (bootverbose || mmc_debug)
1627 device_printf(sc->dev,
1628 "MMC probe: failed\n");
1629 ocr = 0; /* Failed both, powerdown. */
1630 } else if (bootverbose || mmc_debug)
1631 device_printf(sc->dev,
1632 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1633 } else if (bootverbose || mmc_debug)
1634 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n",
1638 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1639 if (mmcbr_get_ocr(dev) != 0)
1642 mmcbr_set_bus_mode(dev, opendrain);
1643 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1644 mmcbr_update_ios(dev);
1645 /* XXX recompute vdd based on new cards? */
1648 * Make sure that we have a mutually agreeable voltage to at least
1649 * one card on the bus.
1651 if (bootverbose || mmc_debug)
1652 device_printf(sc->dev, "Current OCR: 0x%08x\n",
1653 mmcbr_get_ocr(dev));
1654 if (mmcbr_get_ocr(dev) == 0) {
1655 device_printf(sc->dev, "No compatible cards found on bus\n");
1656 mmc_delete_cards(sc);
1661 * Reselect the cards after we've idled them above.
1663 if (mmcbr_get_mode(dev) == mode_sd) {
1664 err = mmc_send_if_cond(sc, 1);
1665 mmc_send_app_op_cond(sc,
1666 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1668 mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1669 mmc_discover_cards(sc);
1670 mmc_rescan_cards(sc);
1672 mmcbr_set_bus_mode(dev, pushpull);
1673 mmcbr_update_ios(dev);
1674 mmc_calculate_clock(sc);
1675 bus_generic_attach(dev);
1676 /* mmc_update_children_sysctl(dev);*/
1680 mmc_calculate_clock(struct mmc_softc *sc)
1683 struct mmc_ivars *ivar;
1684 int i, f_max, max_dtr, max_hs_dtr, max_timing, nkid;
1686 f_max = mmcbr_get_f_max(sc->dev);
1687 max_dtr = max_hs_dtr = f_max;
1688 if (mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED)
1689 max_timing = bus_timing_hs;
1691 max_timing = bus_timing_normal;
1692 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1693 panic("can't get children");
1694 for (i = 0; i < nkid; i++) {
1695 ivar = device_get_ivars(kids[i]);
1696 if (ivar->timing < max_timing)
1697 max_timing = ivar->timing;
1698 if (ivar->tran_speed < max_dtr)
1699 max_dtr = ivar->tran_speed;
1700 if (ivar->hs_tran_speed < max_hs_dtr)
1701 max_hs_dtr = ivar->hs_tran_speed;
1703 for (i = 0; i < nkid; i++) {
1704 ivar = device_get_ivars(kids[i]);
1705 if (ivar->timing == bus_timing_normal)
1707 mmc_select_card(sc, ivar->rca);
1708 mmc_set_timing(sc, max_timing);
1710 mmc_select_card(sc, 0);
1712 if (max_timing == bus_timing_hs)
1713 max_dtr = max_hs_dtr;
1714 if (bootverbose || mmc_debug) {
1715 device_printf(sc->dev,
1716 "setting transfer rate to %d.%03dMHz%s\n",
1717 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1718 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1720 mmcbr_set_timing(sc->dev, max_timing);
1721 mmcbr_set_clock(sc->dev, max_dtr);
1722 mmcbr_update_ios(sc->dev);
1727 mmc_scan(struct mmc_softc *sc)
1729 device_t dev = sc->dev;
1731 mmc_acquire_bus(dev, dev);
1732 mmc_go_discovery(sc);
1733 mmc_release_bus(dev, dev);
1737 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1739 struct mmc_ivars *ivar = device_get_ivars(child);
1744 case MMC_IVAR_DSR_IMP:
1745 *result = ivar->csd.dsr_imp;
1747 case MMC_IVAR_MEDIA_SIZE:
1748 *result = ivar->sec_count;
1751 *result = ivar->rca;
1753 case MMC_IVAR_SECTOR_SIZE:
1754 *result = MMC_SECTOR_SIZE;
1756 case MMC_IVAR_TRAN_SPEED:
1757 *result = mmcbr_get_clock(bus);
1759 case MMC_IVAR_READ_ONLY:
1760 *result = ivar->read_only;
1762 case MMC_IVAR_HIGH_CAP:
1763 *result = ivar->high_cap;
1765 case MMC_IVAR_CARD_TYPE:
1766 *result = ivar->mode;
1768 case MMC_IVAR_BUS_WIDTH:
1769 *result = ivar->bus_width;
1771 case MMC_IVAR_ERASE_SECTOR:
1772 *result = ivar->erase_sector;
1774 case MMC_IVAR_MAX_DATA:
1775 *result = mmcbr_get_max_data(bus);
1777 case MMC_IVAR_CARD_ID_STRING:
1778 *(char **)result = ivar->card_id_string;
1780 case MMC_IVAR_CARD_SN_STRING:
1781 *(char **)result = ivar->card_sn_string;
1788 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1792 * None are writable ATM
1798 mmc_delayed_attach(void *xsc)
1800 struct mmc_softc *sc = xsc;
1803 config_intrhook_disestablish(&sc->config_intrhook);
1807 mmc_child_location_str(device_t dev, device_t child, char *buf,
1811 snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1815 static device_method_t mmc_methods[] = {
1817 DEVMETHOD(device_probe, mmc_probe),
1818 DEVMETHOD(device_attach, mmc_attach),
1819 DEVMETHOD(device_detach, mmc_detach),
1820 DEVMETHOD(device_suspend, mmc_suspend),
1821 DEVMETHOD(device_resume, mmc_resume),
1824 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1825 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1826 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1828 /* MMC Bus interface */
1829 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1830 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1831 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1836 driver_t mmc_driver = {
1839 sizeof(struct mmc_softc),
1841 devclass_t mmc_devclass;
1843 MODULE_VERSION(mmc, 1);