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, "Debug level");
123 /* bus entry points */
124 static int mmc_acquire_bus(device_t busdev, device_t dev);
125 static int mmc_attach(device_t dev);
126 static int mmc_child_location_str(device_t dev, device_t child, char *buf,
128 static int mmc_detach(device_t dev);
129 static int mmc_probe(device_t dev);
130 static int mmc_read_ivar(device_t bus, device_t child, int which,
132 static int mmc_release_bus(device_t busdev, device_t dev);
133 static int mmc_resume(device_t dev);
134 static int mmc_suspend(device_t dev);
135 static int mmc_wait_for_request(device_t brdev, device_t reqdev,
136 struct mmc_request *req);
137 static int mmc_write_ivar(device_t bus, device_t child, int which,
140 #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
141 #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
142 #define MMC_LOCK_INIT(_sc) \
143 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
145 #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
146 #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
147 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
149 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
150 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
151 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
152 struct mmc_sd_status *sd_status);
153 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
154 uint32_t *rawsdstatus);
155 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
157 static int mmc_calculate_clock(struct mmc_softc *sc);
158 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid);
159 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
160 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
161 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
162 static void mmc_delayed_attach(void *xsc);
163 static int mmc_delete_cards(struct mmc_softc *sc);
164 static void mmc_discover_cards(struct mmc_softc *sc);
165 static void mmc_format_card_id_string(struct mmc_ivars *ivar);
166 static void mmc_go_discovery(struct mmc_softc *sc);
167 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
169 static int mmc_highest_voltage(uint32_t ocr);
170 static void mmc_idle_cards(struct mmc_softc *sc);
171 static void mmc_ms_delay(int ms);
172 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
173 static void mmc_power_down(struct mmc_softc *sc);
174 static void mmc_power_up(struct mmc_softc *sc);
175 static void mmc_rescan_cards(struct mmc_softc *sc);
176 static void mmc_scan(struct mmc_softc *sc);
177 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
178 uint8_t value, uint8_t *res);
179 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
180 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
181 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
183 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
184 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
185 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
186 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
188 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
189 static int mmc_send_status(struct mmc_softc *sc, uint16_t rca,
191 static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
192 static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca,
194 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
195 static int mmc_set_timing(struct mmc_softc *sc, int timing);
196 static int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index,
198 static int mmc_test_bus_width(struct mmc_softc *sc);
199 static int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
200 struct mmc_command *cmd, int retries);
201 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
203 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
204 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
205 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
206 static void mmc_wakeup(struct mmc_request *req);
212 DELAY(1000 * ms); /* XXX BAD */
216 mmc_probe(device_t dev)
219 device_set_desc(dev, "MMC/SD bus");
224 mmc_attach(device_t dev)
226 struct mmc_softc *sc;
228 sc = device_get_softc(dev);
232 /* We'll probe and attach our children later, but before / mount */
233 sc->config_intrhook.ich_func = mmc_delayed_attach;
234 sc->config_intrhook.ich_arg = sc;
235 if (config_intrhook_establish(&sc->config_intrhook) != 0)
236 device_printf(dev, "config_intrhook_establish failed\n");
241 mmc_detach(device_t dev)
243 struct mmc_softc *sc = device_get_softc(dev);
246 if ((err = mmc_delete_cards(sc)) != 0)
249 MMC_LOCK_DESTROY(sc);
255 mmc_suspend(device_t dev)
257 struct mmc_softc *sc = device_get_softc(dev);
260 err = bus_generic_suspend(dev);
268 mmc_resume(device_t dev)
270 struct mmc_softc *sc = device_get_softc(dev);
273 return (bus_generic_resume(dev));
277 mmc_acquire_bus(device_t busdev, device_t dev)
279 struct mmc_softc *sc;
280 struct mmc_ivars *ivar;
284 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
287 sc = device_get_softc(busdev);
290 panic("mmc: host bridge didn't serialize us.");
296 * Keep track of the last rca that we've selected. If
297 * we're asked to do it again, don't. We never
298 * unselect unless the bus code itself wants the mmc
299 * bus, and constantly reselecting causes problems.
301 rca = mmc_get_rca(dev);
302 if (sc->last_rca != rca) {
303 mmc_select_card(sc, rca);
305 /* Prepare bus width for the new card. */
306 ivar = device_get_ivars(dev);
307 if (bootverbose || mmc_debug) {
308 device_printf(busdev,
309 "setting bus width to %d bits\n",
310 (ivar->bus_width == bus_width_4) ? 4 :
311 (ivar->bus_width == bus_width_8) ? 8 : 1);
313 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
314 mmcbr_set_bus_width(busdev, ivar->bus_width);
315 mmcbr_update_ios(busdev);
319 * If there's a card selected, stand down.
321 if (sc->last_rca != 0) {
322 mmc_select_card(sc, 0);
331 mmc_release_bus(device_t busdev, device_t dev)
333 struct mmc_softc *sc;
336 sc = device_get_softc(busdev);
340 panic("mmc: releasing unowned bus.");
341 if (sc->owner != dev)
342 panic("mmc: you don't own the bus. game over.");
344 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
354 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
357 return (ocr & MMC_OCR_VOLTAGE);
361 mmc_highest_voltage(uint32_t ocr)
365 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
366 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
373 mmc_wakeup(struct mmc_request *req)
375 struct mmc_softc *sc;
377 sc = (struct mmc_softc *)req->done_data;
379 req->flags |= MMC_REQ_DONE;
385 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
388 req->done = mmc_wakeup;
391 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
392 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
393 if (req->cmd->data) {
394 printf(" data %d\n", (int)req->cmd->data->len);
398 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
400 while ((req->flags & MMC_REQ_DONE) == 0)
401 msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
403 if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
404 device_printf(sc->dev, "CMD%d RESULT: %d\n",
405 req->cmd->opcode, req->cmd->error);
410 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
412 struct mmc_softc *sc = device_get_softc(brdev);
414 return (mmc_wait_for_req(sc, req));
418 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
420 struct mmc_request mreq;
424 memset(&mreq, 0, sizeof(mreq));
425 memset(cmd->resp, 0, sizeof(cmd->resp));
426 cmd->retries = 0; /* Retries done here, not in hardware. */
429 if (mmc_wait_for_req(sc, &mreq) != 0)
430 err = MMC_ERR_FAILED;
433 } while (err != MMC_ERR_NONE && retries-- > 0);
435 if (err != MMC_ERR_NONE && sc->squelched == 0) {
436 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
437 device_printf(sc->dev, "CMD%d failed, RESULT: %d\n",
446 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
447 struct mmc_command *cmd, int retries)
449 struct mmc_command appcmd;
452 /* Squelch error reporting at lower levels, we report below. */
455 memset(&appcmd, 0, sizeof(appcmd));
456 appcmd.opcode = MMC_APP_CMD;
457 appcmd.arg = rca << 16;
458 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
460 if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0)
461 err = MMC_ERR_FAILED;
464 if (err == MMC_ERR_NONE) {
465 if (!(appcmd.resp[0] & R1_APP_CMD))
466 err = MMC_ERR_FAILED;
467 else if (mmc_wait_for_cmd(sc, cmd, 0) != 0)
468 err = MMC_ERR_FAILED;
472 } while (err != MMC_ERR_NONE && retries-- > 0);
475 if (err != MMC_ERR_NONE && sc->squelched == 0) {
476 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
477 device_printf(sc->dev, "ACMD%d failed, RESULT: %d\n",
486 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
487 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
489 struct mmc_command cmd;
492 memset(&cmd, 0, sizeof(cmd));
497 err = mmc_wait_for_cmd(sc, &cmd, retries);
501 if (flags & MMC_RSP_136)
502 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
510 mmc_idle_cards(struct mmc_softc *sc)
513 struct mmc_command cmd;
516 mmcbr_set_chip_select(dev, cs_high);
517 mmcbr_update_ios(dev);
520 memset(&cmd, 0, sizeof(cmd));
521 cmd.opcode = MMC_GO_IDLE_STATE;
523 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
525 mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
528 mmcbr_set_chip_select(dev, cs_dontcare);
529 mmcbr_update_ios(dev);
534 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
536 struct mmc_command cmd;
537 int err = MMC_ERR_NONE, i;
539 memset(&cmd, 0, sizeof(cmd));
540 cmd.opcode = ACMD_SD_SEND_OP_COND;
542 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
545 for (i = 0; i < 1000; i++) {
546 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
547 if (err != MMC_ERR_NONE)
549 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
550 (ocr & MMC_OCR_VOLTAGE) == 0)
552 err = MMC_ERR_TIMEOUT;
555 if (rocr && err == MMC_ERR_NONE)
561 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
563 struct mmc_command cmd;
564 int err = MMC_ERR_NONE, i;
566 memset(&cmd, 0, sizeof(cmd));
567 cmd.opcode = MMC_SEND_OP_COND;
569 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
572 for (i = 0; i < 1000; i++) {
573 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
574 if (err != MMC_ERR_NONE)
576 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
577 (ocr & MMC_OCR_VOLTAGE) == 0)
579 err = MMC_ERR_TIMEOUT;
582 if (rocr && err == MMC_ERR_NONE)
588 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
590 struct mmc_command cmd;
593 memset(&cmd, 0, sizeof(cmd));
594 cmd.opcode = SD_SEND_IF_COND;
595 cmd.arg = (vhs << 8) + 0xAA;
596 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
599 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
604 mmc_power_up(struct mmc_softc *sc)
609 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
610 mmcbr_set_bus_mode(dev, opendrain);
611 mmcbr_set_chip_select(dev, cs_dontcare);
612 mmcbr_set_bus_width(dev, bus_width_1);
613 mmcbr_set_power_mode(dev, power_up);
614 mmcbr_set_clock(dev, 0);
615 mmcbr_update_ios(dev);
618 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
619 mmcbr_set_timing(dev, bus_timing_normal);
620 mmcbr_set_power_mode(dev, power_on);
621 mmcbr_update_ios(dev);
626 mmc_power_down(struct mmc_softc *sc)
628 device_t dev = sc->dev;
630 mmcbr_set_bus_mode(dev, opendrain);
631 mmcbr_set_chip_select(dev, cs_dontcare);
632 mmcbr_set_bus_width(dev, bus_width_1);
633 mmcbr_set_power_mode(dev, power_off);
634 mmcbr_set_clock(dev, 0);
635 mmcbr_set_timing(dev, bus_timing_normal);
636 mmcbr_update_ios(dev);
640 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
644 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
645 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
646 flags, NULL, CMD_RETRIES));
650 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
652 struct mmc_command cmd;
655 memset(&cmd, 0, sizeof(cmd));
656 cmd.opcode = MMC_SWITCH_FUNC;
657 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
661 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
663 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
668 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
672 struct mmc_command cmd;
673 struct mmc_data data;
675 memset(&cmd, 0, sizeof(cmd));
676 memset(&data, 0, sizeof(data));
679 cmd.opcode = SD_SWITCH_FUNC;
680 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
681 cmd.arg = mode << 31; /* 0 - check, 1 - set */
682 cmd.arg |= 0x00FFFFFF;
683 cmd.arg &= ~(0xF << (grp * 4));
684 cmd.arg |= value << (grp * 4);
689 data.flags = MMC_DATA_READ;
691 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
696 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
698 struct mmc_command cmd;
702 if (mmcbr_get_mode(sc->dev) == mode_sd) {
703 memset(&cmd, 0, sizeof(cmd));
704 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
705 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
706 cmd.arg = SD_CLR_CARD_DETECT;
707 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
710 memset(&cmd, 0, sizeof(cmd));
711 cmd.opcode = ACMD_SET_BUS_WIDTH;
712 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
715 cmd.arg = SD_BUS_WIDTH_1;
718 cmd.arg = SD_BUS_WIDTH_4;
721 return (MMC_ERR_INVALID);
723 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
727 value = EXT_CSD_BUS_WIDTH_1;
730 value = EXT_CSD_BUS_WIDTH_4;
733 value = EXT_CSD_BUS_WIDTH_8;
736 return (MMC_ERR_INVALID);
738 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
745 mmc_set_timing(struct mmc_softc *sc, int timing)
749 u_char switch_res[64];
752 case bus_timing_normal:
759 return (MMC_ERR_INVALID);
761 if (mmcbr_get_mode(sc->dev) == mode_sd)
762 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
765 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
766 EXT_CSD_HS_TIMING, value);
771 mmc_test_bus_width(struct mmc_softc *sc)
773 struct mmc_command cmd;
774 struct mmc_data data;
777 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
778 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
779 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, };
780 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
782 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
783 mmcbr_set_bus_width(sc->dev, bus_width_8);
784 mmcbr_update_ios(sc->dev);
786 sc->squelched++; /* Errors are expected, squelch reporting. */
787 memset(&cmd, 0, sizeof(cmd));
788 memset(&data, 0, sizeof(data));
789 cmd.opcode = MMC_BUSTEST_W;
791 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
796 data.flags = MMC_DATA_WRITE;
797 mmc_wait_for_cmd(sc, &cmd, 0);
799 memset(&cmd, 0, sizeof(cmd));
800 memset(&data, 0, sizeof(data));
801 cmd.opcode = MMC_BUSTEST_R;
803 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
808 data.flags = MMC_DATA_READ;
809 err = mmc_wait_for_cmd(sc, &cmd, 0);
812 mmcbr_set_bus_width(sc->dev, bus_width_1);
813 mmcbr_update_ios(sc->dev);
815 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
816 return (bus_width_8);
819 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
820 mmcbr_set_bus_width(sc->dev, bus_width_4);
821 mmcbr_update_ios(sc->dev);
823 sc->squelched++; /* Errors are expected, squelch reporting. */
824 memset(&cmd, 0, sizeof(cmd));
825 memset(&data, 0, sizeof(data));
826 cmd.opcode = MMC_BUSTEST_W;
828 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
833 data.flags = MMC_DATA_WRITE;
834 mmc_wait_for_cmd(sc, &cmd, 0);
836 memset(&cmd, 0, sizeof(cmd));
837 memset(&data, 0, sizeof(data));
838 cmd.opcode = MMC_BUSTEST_R;
840 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
845 data.flags = MMC_DATA_READ;
846 err = mmc_wait_for_cmd(sc, &cmd, 0);
849 mmcbr_set_bus_width(sc->dev, bus_width_1);
850 mmcbr_update_ios(sc->dev);
852 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
853 return (bus_width_4);
855 return (bus_width_1);
859 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
861 const int i = (bit_len / 32) - (start / 32) - 1;
862 const int shift = start & 31;
863 uint32_t retval = bits[i] >> shift;
864 if (size + shift > 32)
865 retval |= bits[i - 1] << (32 - shift);
866 return (retval & ((1llu << size) - 1));
870 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
874 /* There's no version info, so we take it on faith */
875 memset(cid, 0, sizeof(*cid));
876 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
877 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
878 for (i = 0; i < 5; i++)
879 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
881 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
882 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
883 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
884 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
888 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
892 /* There's no version info, so we take it on faith */
893 memset(cid, 0, sizeof(*cid));
894 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
895 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
896 for (i = 0; i < 6; i++)
897 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
899 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
900 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
901 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
902 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
906 mmc_format_card_id_string(struct mmc_ivars *ivar)
913 * Format a card ID string for use by the mmcsd driver, it's what
914 * appears between the <> in the following:
915 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
916 * 22.5MHz/4bit/128-block
918 * Also format just the card serial number, which the mmcsd driver will
919 * use as the disk->d_ident string.
921 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
922 * and our max formatted length is currently 55 bytes if every field
923 * contains the largest value.
925 * Sometimes the oid is two printable ascii chars; when it's not,
926 * format it as 0xnnnn instead.
928 c1 = (ivar->cid.oid >> 8) & 0x0ff;
929 c2 = ivar->cid.oid & 0x0ff;
930 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
931 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
933 snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
934 snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
935 "%08X", ivar->cid.psn);
936 snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
937 "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
938 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
939 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
940 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
941 ivar->cid.mid, oidstr);
944 static const int exp[8] = {
945 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
948 static const int mant[16] = {
949 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
952 static const int cur_min[8] = {
953 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
956 static const int cur_max[8] = {
957 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
961 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
967 memset(csd, 0, sizeof(*csd));
968 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
970 m = mmc_get_bits(raw_csd, 128, 115, 4);
971 e = mmc_get_bits(raw_csd, 128, 112, 3);
972 csd->tacc = (exp[e] * mant[m] + 9) / 10;
973 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
974 m = mmc_get_bits(raw_csd, 128, 99, 4);
975 e = mmc_get_bits(raw_csd, 128, 96, 3);
976 csd->tran_speed = exp[e] * 10000 * mant[m];
977 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
978 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
979 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
980 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
981 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
982 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
983 csd->vdd_r_curr_min =
984 cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
985 csd->vdd_r_curr_max =
986 cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
987 csd->vdd_w_curr_min =
988 cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
989 csd->vdd_w_curr_max =
990 cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
991 m = mmc_get_bits(raw_csd, 128, 62, 12);
992 e = mmc_get_bits(raw_csd, 128, 47, 3);
993 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
994 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
995 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
996 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
997 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
998 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
999 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1000 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1001 } else if (v == 1) {
1002 m = mmc_get_bits(raw_csd, 128, 115, 4);
1003 e = mmc_get_bits(raw_csd, 128, 112, 3);
1004 csd->tacc = (exp[e] * mant[m] + 9) / 10;
1005 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1006 m = mmc_get_bits(raw_csd, 128, 99, 4);
1007 e = mmc_get_bits(raw_csd, 128, 96, 3);
1008 csd->tran_speed = exp[e] * 10000 * mant[m];
1009 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1010 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1011 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1012 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1013 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1014 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1015 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) +
1017 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1018 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1019 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1020 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1021 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1022 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1023 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1025 panic("unknown SD CSD version");
1029 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
1034 memset(csd, 0, sizeof(*csd));
1035 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
1036 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1037 m = mmc_get_bits(raw_csd, 128, 115, 4);
1038 e = mmc_get_bits(raw_csd, 128, 112, 3);
1039 csd->tacc = exp[e] * mant[m] + 9 / 10;
1040 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1041 m = mmc_get_bits(raw_csd, 128, 99, 4);
1042 e = mmc_get_bits(raw_csd, 128, 96, 3);
1043 csd->tran_speed = exp[e] * 10000 * mant[m];
1044 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1045 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1046 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1047 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1048 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1049 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1050 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1051 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1052 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1053 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1054 m = mmc_get_bits(raw_csd, 128, 62, 12);
1055 e = mmc_get_bits(raw_csd, 128, 47, 3);
1056 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1057 csd->erase_blk_en = 0;
1058 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1059 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1060 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1061 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1062 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1063 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1064 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1068 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1070 unsigned int scr_struct;
1072 memset(scr, 0, sizeof(*scr));
1074 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1075 if (scr_struct != 0) {
1076 printf("Unrecognised SCR structure version %d\n",
1080 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1081 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1085 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1086 struct mmc_sd_status *sd_status)
1089 memset(sd_status, 0, sizeof(*sd_status));
1091 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1092 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1093 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1094 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1095 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1096 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1097 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1098 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1099 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1100 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1104 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1106 struct mmc_command cmd;
1109 memset(&cmd, 0, sizeof(cmd));
1110 cmd.opcode = MMC_ALL_SEND_CID;
1112 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1114 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1115 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1120 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1122 struct mmc_command cmd;
1125 memset(&cmd, 0, sizeof(cmd));
1126 cmd.opcode = MMC_SEND_CSD;
1127 cmd.arg = rca << 16;
1128 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1130 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1131 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1136 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1139 struct mmc_command cmd;
1140 struct mmc_data data;
1142 memset(&cmd, 0, sizeof(cmd));
1143 memset(&data, 0, sizeof(data));
1145 memset(rawscr, 0, 8);
1146 cmd.opcode = ACMD_SEND_SCR;
1147 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1153 data.flags = MMC_DATA_READ;
1155 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1156 rawscr[0] = be32toh(rawscr[0]);
1157 rawscr[1] = be32toh(rawscr[1]);
1162 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1165 struct mmc_command cmd;
1166 struct mmc_data data;
1168 memset(&cmd, 0, sizeof(cmd));
1169 memset(&data, 0, sizeof(data));
1171 memset(rawextcsd, 0, 512);
1172 cmd.opcode = MMC_SEND_EXT_CSD;
1173 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1177 data.data = rawextcsd;
1179 data.flags = MMC_DATA_READ;
1181 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1186 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1189 struct mmc_command cmd;
1190 struct mmc_data data;
1192 memset(&cmd, 0, sizeof(cmd));
1193 memset(&data, 0, sizeof(data));
1195 memset(rawsdstatus, 0, 64);
1196 cmd.opcode = ACMD_SD_STATUS;
1197 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1201 data.data = rawsdstatus;
1203 data.flags = MMC_DATA_READ;
1205 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1206 for (i = 0; i < 16; i++)
1207 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1212 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1214 struct mmc_command cmd;
1217 memset(&cmd, 0, sizeof(cmd));
1218 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1219 cmd.arg = resp << 16;
1220 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1222 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1227 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1229 struct mmc_command cmd;
1232 memset(&cmd, 0, sizeof(cmd));
1233 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1235 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1237 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1238 *resp = cmd.resp[0];
1243 mmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status)
1245 struct mmc_command cmd;
1248 memset(&cmd, 0, sizeof(cmd));
1249 cmd.opcode = MMC_SEND_STATUS;
1250 cmd.arg = rca << 16;
1251 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1253 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1254 *status = cmd.resp[0];
1259 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1261 struct mmc_command cmd;
1264 memset(&cmd, 0, sizeof(cmd));
1265 cmd.opcode = MMC_SET_BLOCKLEN;
1267 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1269 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1274 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1276 device_printf(dev, "Card at relative address 0x%04x%s:\n",
1277 ivar->rca, newcard ? " added" : "");
1278 device_printf(dev, " card: %s\n", ivar->card_id_string);
1279 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1280 (ivar->bus_width == bus_width_1 ? 1 :
1281 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1282 (ivar->timing == bus_timing_hs ?
1283 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1284 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1285 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1286 ivar->sec_count, ivar->erase_sector,
1287 ivar->read_only ? ", read-only" : "");
1291 mmc_discover_cards(struct mmc_softc *sc)
1293 struct mmc_ivars *ivar = NULL;
1295 int err, i, devcount, newcard;
1296 uint32_t raw_cid[4], resp, sec_count, status;
1299 u_char switch_res[64];
1301 if (bootverbose || mmc_debug)
1302 device_printf(sc->dev, "Probing cards\n");
1304 sc->squelched++; /* Errors are expected, squelch reporting. */
1305 err = mmc_all_send_cid(sc, raw_cid);
1307 if (err == MMC_ERR_TIMEOUT)
1309 if (err != MMC_ERR_NONE) {
1310 device_printf(sc->dev, "Error reading CID %d\n", err);
1314 if ((err = device_get_children(sc->dev, &devlist,
1317 for (i = 0; i < devcount; i++) {
1318 ivar = device_get_ivars(devlist[i]);
1319 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) ==
1325 free(devlist, M_TEMP);
1326 if (bootverbose || mmc_debug) {
1327 device_printf(sc->dev,
1328 "%sard detected (CID %08x%08x%08x%08x)\n",
1329 newcard ? "New c" : "C",
1330 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1333 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1335 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1337 if (mmcbr_get_ro(sc->dev))
1338 ivar->read_only = 1;
1339 ivar->bus_width = bus_width_1;
1340 ivar->timing = bus_timing_normal;
1341 ivar->mode = mmcbr_get_mode(sc->dev);
1342 if (ivar->mode == mode_sd) {
1343 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1344 mmc_send_relative_addr(sc, &resp);
1345 ivar->rca = resp >> 16;
1347 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1348 if (bootverbose || mmc_debug)
1349 device_printf(sc->dev,
1350 "%sard detected (CSD %08x%08x%08x%08x)\n",
1351 newcard ? "New c" : "C", ivar->raw_csd[0],
1352 ivar->raw_csd[1], ivar->raw_csd[2],
1354 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1355 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1356 if (ivar->csd.csd_structure > 0)
1358 ivar->tran_speed = ivar->csd.tran_speed;
1359 ivar->erase_sector = ivar->csd.erase_sector *
1360 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1362 err = mmc_send_status(sc, ivar->rca, &status);
1363 if (err != MMC_ERR_NONE) {
1364 device_printf(sc->dev,
1365 "Error reading card status %d\n", err);
1368 if ((status & R1_CARD_IS_LOCKED) != 0) {
1369 device_printf(sc->dev,
1370 "Card is password protected, skipping.\n");
1374 /* Get card SCR. Card must be selected to fetch it. */
1375 mmc_select_card(sc, ivar->rca);
1376 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1377 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1378 /* Get card switch capabilities (command class 10). */
1379 if ((ivar->scr.sda_vsn >= 1) &&
1380 (ivar->csd.ccc & (1 << 10))) {
1381 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1382 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1384 if (switch_res[13] & 2) {
1385 ivar->timing = bus_timing_hs;
1386 ivar->hs_tran_speed = SD_MAX_HS;
1391 * We deselect then reselect the card here. Some cards
1392 * become unselected and timeout with the above two
1393 * commands, although the state tables / diagrams in the
1394 * standard suggest they go back to the transfer state.
1395 * Other cards don't become deselected, and if we
1396 * atttempt to blindly re-select them, we get timeout
1397 * errors from some controllers. So we deselect then
1398 * reselect to handle all situations. The only thing we
1399 * use from the sd_status is the erase sector size, but
1400 * it is still nice to get that right.
1402 mmc_select_card(sc, 0);
1403 mmc_select_card(sc, ivar->rca);
1404 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1405 mmc_app_decode_sd_status(ivar->raw_sd_status,
1407 if (ivar->sd_status.au_size != 0) {
1408 ivar->erase_sector =
1409 16 << ivar->sd_status.au_size;
1411 /* Find max supported bus width. */
1412 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1413 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1414 ivar->bus_width = bus_width_4;
1417 * Some cards that report maximum I/O block sizes
1418 * greater than 512 require the block length to be
1419 * set to 512, even though that is supposed to be
1420 * the default. Example:
1422 * Transcend 2GB SDSC card, CID:
1423 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1425 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1426 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1427 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1429 mmc_format_card_id_string(ivar);
1431 if (bootverbose || mmc_debug)
1432 mmc_log_card(sc->dev, ivar, newcard);
1435 child = device_add_child(sc->dev, NULL, -1);
1436 device_set_ivars(child, ivar);
1438 mmc_select_card(sc, 0);
1441 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1443 mmc_set_relative_addr(sc, ivar->rca);
1445 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1446 if (bootverbose || mmc_debug)
1447 device_printf(sc->dev,
1448 "%sard detected (CSD %08x%08x%08x%08x)\n",
1449 newcard ? "New c" : "C", ivar->raw_csd[0],
1450 ivar->raw_csd[1], ivar->raw_csd[2],
1453 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1454 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1455 ivar->tran_speed = ivar->csd.tran_speed;
1456 ivar->erase_sector = ivar->csd.erase_sector *
1457 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1459 err = mmc_send_status(sc, ivar->rca, &status);
1460 if (err != MMC_ERR_NONE) {
1461 device_printf(sc->dev,
1462 "Error reading card status %d\n", err);
1465 if ((status & R1_CARD_IS_LOCKED) != 0) {
1466 device_printf(sc->dev,
1467 "Card is password protected, skipping.\n");
1471 mmc_select_card(sc, ivar->rca);
1473 /* Only MMC >= 4.x cards support EXT_CSD. */
1474 if (ivar->csd.spec_vers >= 4) {
1475 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1476 /* Handle extended capacity from EXT_CSD */
1477 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1478 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1479 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1480 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1481 if (sec_count != 0) {
1482 ivar->sec_count = sec_count;
1485 /* Get card speed in high speed mode. */
1486 ivar->timing = bus_timing_hs;
1487 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1488 & EXT_CSD_CARD_TYPE_52)
1489 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1490 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1491 & EXT_CSD_CARD_TYPE_26)
1492 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1494 ivar->hs_tran_speed = ivar->tran_speed;
1495 /* Find max supported bus width. */
1496 ivar->bus_width = mmc_test_bus_width(sc);
1497 /* Handle HC erase sector size. */
1498 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1499 ivar->erase_sector = 1024 *
1500 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1501 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1502 EXT_CSD_ERASE_GRP_DEF, 1);
1505 ivar->bus_width = bus_width_1;
1506 ivar->timing = bus_timing_normal;
1510 * Some cards that report maximum I/O block sizes greater
1511 * than 512 require the block length to be set to 512, even
1512 * though that is supposed to be the default. Example:
1514 * Transcend 2GB SDSC card, CID:
1515 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1517 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1518 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1519 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1521 mmc_format_card_id_string(ivar);
1523 if (bootverbose || mmc_debug)
1524 mmc_log_card(sc->dev, ivar, newcard);
1527 child = device_add_child(sc->dev, NULL, -1);
1528 device_set_ivars(child, ivar);
1530 mmc_select_card(sc, 0);
1535 mmc_rescan_cards(struct mmc_softc *sc)
1537 struct mmc_ivars *ivar = NULL;
1539 int err, i, devcount;
1541 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1543 for (i = 0; i < devcount; i++) {
1544 ivar = device_get_ivars(devlist[i]);
1545 if (mmc_select_card(sc, ivar->rca)) {
1546 if (bootverbose || mmc_debug)
1547 device_printf(sc->dev,
1548 "Card at relative address %d lost.\n",
1550 device_delete_child(sc->dev, devlist[i]);
1551 free(ivar, M_DEVBUF);
1554 free(devlist, M_TEMP);
1555 mmc_select_card(sc, 0);
1559 mmc_delete_cards(struct mmc_softc *sc)
1561 struct mmc_ivars *ivar;
1563 int err, i, devcount;
1565 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1567 for (i = 0; i < devcount; i++) {
1568 ivar = device_get_ivars(devlist[i]);
1569 if (bootverbose || mmc_debug)
1570 device_printf(sc->dev,
1571 "Card at relative address %d deleted.\n",
1573 device_delete_child(sc->dev, devlist[i]);
1574 free(ivar, M_DEVBUF);
1576 free(devlist, M_TEMP);
1581 mmc_go_discovery(struct mmc_softc *sc)
1588 if (mmcbr_get_power_mode(dev) != power_on) {
1590 * First, try SD modes
1592 sc->squelched++; /* Errors are expected, squelch reporting. */
1593 mmcbr_set_mode(dev, mode_sd);
1595 mmcbr_set_bus_mode(dev, pushpull);
1596 if (bootverbose || mmc_debug)
1597 device_printf(sc->dev, "Probing bus\n");
1599 err = mmc_send_if_cond(sc, 1);
1600 if ((bootverbose || mmc_debug) && err == 0)
1601 device_printf(sc->dev,
1602 "SD 2.0 interface conditions: OK\n");
1603 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1604 if (bootverbose || mmc_debug)
1605 device_printf(sc->dev, "SD probe: failed\n");
1609 mmcbr_set_mode(dev, mode_mmc);
1610 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1611 if (bootverbose || mmc_debug)
1612 device_printf(sc->dev,
1613 "MMC probe: failed\n");
1614 ocr = 0; /* Failed both, powerdown. */
1615 } else if (bootverbose || mmc_debug)
1616 device_printf(sc->dev,
1617 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1618 } else if (bootverbose || mmc_debug)
1619 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n",
1623 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1624 if (mmcbr_get_ocr(dev) != 0)
1627 mmcbr_set_bus_mode(dev, opendrain);
1628 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1629 mmcbr_update_ios(dev);
1630 /* XXX recompute vdd based on new cards? */
1633 * Make sure that we have a mutually agreeable voltage to at least
1634 * one card on the bus.
1636 if (bootverbose || mmc_debug)
1637 device_printf(sc->dev, "Current OCR: 0x%08x\n",
1638 mmcbr_get_ocr(dev));
1639 if (mmcbr_get_ocr(dev) == 0) {
1640 device_printf(sc->dev, "No compatible cards found on bus\n");
1641 mmc_delete_cards(sc);
1646 * Reselect the cards after we've idled them above.
1648 if (mmcbr_get_mode(dev) == mode_sd) {
1649 err = mmc_send_if_cond(sc, 1);
1650 mmc_send_app_op_cond(sc,
1651 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1653 mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1654 mmc_discover_cards(sc);
1655 mmc_rescan_cards(sc);
1657 mmcbr_set_bus_mode(dev, pushpull);
1658 mmcbr_update_ios(dev);
1659 mmc_calculate_clock(sc);
1660 bus_generic_attach(dev);
1661 /* mmc_update_children_sysctl(dev);*/
1665 mmc_calculate_clock(struct mmc_softc *sc)
1667 int max_dtr, max_hs_dtr, max_timing;
1670 struct mmc_ivars *ivar;
1672 f_max = mmcbr_get_f_max(sc->dev);
1673 max_dtr = max_hs_dtr = f_max;
1674 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1675 max_timing = bus_timing_hs;
1677 max_timing = bus_timing_normal;
1678 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1679 panic("can't get children");
1680 for (i = 0; i < nkid; i++) {
1681 ivar = device_get_ivars(kids[i]);
1682 if (ivar->timing < max_timing)
1683 max_timing = ivar->timing;
1684 if (ivar->tran_speed < max_dtr)
1685 max_dtr = ivar->tran_speed;
1686 if (ivar->hs_tran_speed < max_hs_dtr)
1687 max_hs_dtr = ivar->hs_tran_speed;
1689 for (i = 0; i < nkid; i++) {
1690 ivar = device_get_ivars(kids[i]);
1691 if (ivar->timing == bus_timing_normal)
1693 mmc_select_card(sc, ivar->rca);
1694 mmc_set_timing(sc, max_timing);
1696 mmc_select_card(sc, 0);
1698 if (max_timing == bus_timing_hs)
1699 max_dtr = max_hs_dtr;
1700 if (bootverbose || mmc_debug) {
1701 device_printf(sc->dev,
1702 "setting transfer rate to %d.%03dMHz%s\n",
1703 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1704 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1706 mmcbr_set_timing(sc->dev, max_timing);
1707 mmcbr_set_clock(sc->dev, max_dtr);
1708 mmcbr_update_ios(sc->dev);
1713 mmc_scan(struct mmc_softc *sc)
1715 device_t dev = sc->dev;
1717 mmc_acquire_bus(dev, dev);
1718 mmc_go_discovery(sc);
1719 mmc_release_bus(dev, dev);
1723 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1725 struct mmc_ivars *ivar = device_get_ivars(child);
1730 case MMC_IVAR_DSR_IMP:
1731 *result = ivar->csd.dsr_imp;
1733 case MMC_IVAR_MEDIA_SIZE:
1734 *result = ivar->sec_count;
1737 *result = ivar->rca;
1739 case MMC_IVAR_SECTOR_SIZE:
1740 *result = MMC_SECTOR_SIZE;
1742 case MMC_IVAR_TRAN_SPEED:
1743 *result = mmcbr_get_clock(bus);
1745 case MMC_IVAR_READ_ONLY:
1746 *result = ivar->read_only;
1748 case MMC_IVAR_HIGH_CAP:
1749 *result = ivar->high_cap;
1751 case MMC_IVAR_CARD_TYPE:
1752 *result = ivar->mode;
1754 case MMC_IVAR_BUS_WIDTH:
1755 *result = ivar->bus_width;
1757 case MMC_IVAR_ERASE_SECTOR:
1758 *result = ivar->erase_sector;
1760 case MMC_IVAR_MAX_DATA:
1761 *result = mmcbr_get_max_data(bus);
1763 case MMC_IVAR_CARD_ID_STRING:
1764 *(char **)result = ivar->card_id_string;
1766 case MMC_IVAR_CARD_SN_STRING:
1767 *(char **)result = ivar->card_sn_string;
1774 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1778 * None are writable ATM
1784 mmc_delayed_attach(void *xsc)
1786 struct mmc_softc *sc = xsc;
1789 config_intrhook_disestablish(&sc->config_intrhook);
1793 mmc_child_location_str(device_t dev, device_t child, char *buf,
1797 snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1801 static device_method_t mmc_methods[] = {
1803 DEVMETHOD(device_probe, mmc_probe),
1804 DEVMETHOD(device_attach, mmc_attach),
1805 DEVMETHOD(device_detach, mmc_detach),
1806 DEVMETHOD(device_suspend, mmc_suspend),
1807 DEVMETHOD(device_resume, mmc_resume),
1810 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1811 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1812 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1814 /* MMC Bus interface */
1815 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1816 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1817 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1822 driver_t mmc_driver = {
1825 sizeof(struct mmc_softc),
1827 devclass_t mmc_devclass;
1829 MODULE_VERSION(mmc, 1);