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_RW, &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 = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
984 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
985 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
986 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
987 m = mmc_get_bits(raw_csd, 128, 62, 12);
988 e = mmc_get_bits(raw_csd, 128, 47, 3);
989 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
990 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
991 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
992 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
993 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
994 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
995 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
996 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
998 m = mmc_get_bits(raw_csd, 128, 115, 4);
999 e = mmc_get_bits(raw_csd, 128, 112, 3);
1000 csd->tacc = (exp[e] * mant[m] + 9) / 10;
1001 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1002 m = mmc_get_bits(raw_csd, 128, 99, 4);
1003 e = mmc_get_bits(raw_csd, 128, 96, 3);
1004 csd->tran_speed = exp[e] * 10000 * mant[m];
1005 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1006 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1007 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1008 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1009 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1010 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1011 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
1013 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1014 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1015 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1016 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1017 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1018 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1019 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1021 panic("unknown SD CSD version");
1025 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
1030 memset(csd, 0, sizeof(*csd));
1031 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
1032 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1033 m = mmc_get_bits(raw_csd, 128, 115, 4);
1034 e = mmc_get_bits(raw_csd, 128, 112, 3);
1035 csd->tacc = exp[e] * mant[m] + 9 / 10;
1036 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1037 m = mmc_get_bits(raw_csd, 128, 99, 4);
1038 e = mmc_get_bits(raw_csd, 128, 96, 3);
1039 csd->tran_speed = exp[e] * 10000 * mant[m];
1040 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1041 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1042 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1043 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1044 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1045 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1046 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1047 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1048 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1049 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1050 m = mmc_get_bits(raw_csd, 128, 62, 12);
1051 e = mmc_get_bits(raw_csd, 128, 47, 3);
1052 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1053 csd->erase_blk_en = 0;
1054 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1055 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1056 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1057 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1058 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1059 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1060 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1064 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1066 unsigned int scr_struct;
1068 memset(scr, 0, sizeof(*scr));
1070 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1071 if (scr_struct != 0) {
1072 printf("Unrecognised SCR structure version %d\n",
1076 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1077 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1081 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1082 struct mmc_sd_status *sd_status)
1085 memset(sd_status, 0, sizeof(*sd_status));
1087 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1088 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1089 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1090 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1091 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1092 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1093 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1094 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1095 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1096 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1100 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1102 struct mmc_command cmd;
1105 memset(&cmd, 0, sizeof(cmd));
1106 cmd.opcode = MMC_ALL_SEND_CID;
1108 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1110 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1111 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1116 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1118 struct mmc_command cmd;
1121 memset(&cmd, 0, sizeof(cmd));
1122 cmd.opcode = MMC_SEND_CSD;
1123 cmd.arg = rca << 16;
1124 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1126 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1127 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1132 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1135 struct mmc_command cmd;
1136 struct mmc_data data;
1138 memset(&cmd, 0, sizeof(cmd));
1139 memset(&data, 0, sizeof(data));
1141 memset(rawscr, 0, 8);
1142 cmd.opcode = ACMD_SEND_SCR;
1143 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1149 data.flags = MMC_DATA_READ;
1151 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1152 rawscr[0] = be32toh(rawscr[0]);
1153 rawscr[1] = be32toh(rawscr[1]);
1158 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1161 struct mmc_command cmd;
1162 struct mmc_data data;
1164 memset(&cmd, 0, sizeof(cmd));
1165 memset(&data, 0, sizeof(data));
1167 memset(rawextcsd, 0, 512);
1168 cmd.opcode = MMC_SEND_EXT_CSD;
1169 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1173 data.data = rawextcsd;
1175 data.flags = MMC_DATA_READ;
1177 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1182 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1185 struct mmc_command cmd;
1186 struct mmc_data data;
1188 memset(&cmd, 0, sizeof(cmd));
1189 memset(&data, 0, sizeof(data));
1191 memset(rawsdstatus, 0, 64);
1192 cmd.opcode = ACMD_SD_STATUS;
1193 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1197 data.data = rawsdstatus;
1199 data.flags = MMC_DATA_READ;
1201 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1202 for (i = 0; i < 16; i++)
1203 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1208 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1210 struct mmc_command cmd;
1213 memset(&cmd, 0, sizeof(cmd));
1214 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1215 cmd.arg = resp << 16;
1216 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1218 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1223 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1225 struct mmc_command cmd;
1228 memset(&cmd, 0, sizeof(cmd));
1229 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1231 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1233 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1234 *resp = cmd.resp[0];
1239 mmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status)
1241 struct mmc_command cmd;
1244 memset(&cmd, 0, sizeof(cmd));
1245 cmd.opcode = MMC_SEND_STATUS;
1246 cmd.arg = rca << 16;
1247 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1249 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1250 *status = cmd.resp[0];
1255 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1257 struct mmc_command cmd;
1260 memset(&cmd, 0, sizeof(cmd));
1261 cmd.opcode = MMC_SET_BLOCKLEN;
1263 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1265 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1270 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1272 device_printf(dev, "Card at relative address 0x%04x%s:\n",
1273 ivar->rca, newcard ? " added" : "");
1274 device_printf(dev, " card: %s\n", ivar->card_id_string);
1275 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1276 (ivar->bus_width == bus_width_1 ? 1 :
1277 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1278 (ivar->timing == bus_timing_hs ?
1279 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1280 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1281 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1282 ivar->sec_count, ivar->erase_sector,
1283 ivar->read_only ? ", read-only" : "");
1287 mmc_discover_cards(struct mmc_softc *sc)
1289 struct mmc_ivars *ivar = NULL;
1291 int err, i, devcount, newcard;
1292 uint32_t raw_cid[4], resp, sec_count, status;
1295 u_char switch_res[64];
1297 if (bootverbose || mmc_debug)
1298 device_printf(sc->dev, "Probing cards\n");
1300 sc->squelched++; /* Errors are expected, squelch reporting. */
1301 err = mmc_all_send_cid(sc, raw_cid);
1303 if (err == MMC_ERR_TIMEOUT)
1305 if (err != MMC_ERR_NONE) {
1306 device_printf(sc->dev, "Error reading CID %d\n", err);
1310 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1312 for (i = 0; i < devcount; i++) {
1313 ivar = device_get_ivars(devlist[i]);
1314 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1319 free(devlist, M_TEMP);
1320 if (bootverbose || mmc_debug) {
1321 device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1322 newcard ? "New c" : "C",
1323 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1326 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1328 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1330 if (mmcbr_get_ro(sc->dev))
1331 ivar->read_only = 1;
1332 ivar->bus_width = bus_width_1;
1333 ivar->timing = bus_timing_normal;
1334 ivar->mode = mmcbr_get_mode(sc->dev);
1335 if (ivar->mode == mode_sd) {
1336 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1337 mmc_send_relative_addr(sc, &resp);
1338 ivar->rca = resp >> 16;
1340 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1341 if (bootverbose || mmc_debug)
1342 device_printf(sc->dev,
1343 "%sard detected (CSD %08x%08x%08x%08x)\n",
1344 newcard ? "New c" : "C", ivar->raw_csd[0],
1345 ivar->raw_csd[1], ivar->raw_csd[2],
1347 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1348 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1349 if (ivar->csd.csd_structure > 0)
1351 ivar->tran_speed = ivar->csd.tran_speed;
1352 ivar->erase_sector = ivar->csd.erase_sector *
1353 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1355 err = mmc_send_status(sc, ivar->rca, &status);
1356 if (err != MMC_ERR_NONE) {
1357 device_printf(sc->dev,
1358 "Error reading card status %d\n", err);
1361 if ((status & R1_CARD_IS_LOCKED) != 0) {
1362 device_printf(sc->dev,
1363 "Card is password protected, skipping.\n");
1367 /* Get card SCR. Card must be selected to fetch it. */
1368 mmc_select_card(sc, ivar->rca);
1369 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1370 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1371 /* Get card switch capabilities (command class 10). */
1372 if ((ivar->scr.sda_vsn >= 1) &&
1373 (ivar->csd.ccc & (1<<10))) {
1374 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1375 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1377 if (switch_res[13] & 2) {
1378 ivar->timing = bus_timing_hs;
1379 ivar->hs_tran_speed = SD_MAX_HS;
1382 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1383 mmc_app_decode_sd_status(ivar->raw_sd_status,
1385 if (ivar->sd_status.au_size != 0) {
1386 ivar->erase_sector =
1387 16 << ivar->sd_status.au_size;
1389 mmc_select_card(sc, 0);
1390 /* Find max supported bus width. */
1391 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1392 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1393 ivar->bus_width = bus_width_4;
1396 * Some cards that report maximum I/O block sizes
1397 * greater than 512 require the block length to be
1398 * set to 512, even though that is supposed to be
1399 * the default. Example:
1401 * Transcend 2GB SDSC card, CID:
1402 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1404 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1405 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1406 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1408 mmc_format_card_id_string(ivar);
1410 if (bootverbose || mmc_debug)
1411 mmc_log_card(sc->dev, ivar, newcard);
1414 child = device_add_child(sc->dev, NULL, -1);
1415 device_set_ivars(child, ivar);
1419 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1421 mmc_set_relative_addr(sc, ivar->rca);
1423 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1424 if (bootverbose || mmc_debug)
1425 device_printf(sc->dev,
1426 "%sard detected (CSD %08x%08x%08x%08x)\n",
1427 newcard ? "New c" : "C", ivar->raw_csd[0],
1428 ivar->raw_csd[1], ivar->raw_csd[2],
1431 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1432 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1433 ivar->tran_speed = ivar->csd.tran_speed;
1434 ivar->erase_sector = ivar->csd.erase_sector *
1435 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1437 err = mmc_send_status(sc, ivar->rca, &status);
1438 if (err != MMC_ERR_NONE) {
1439 device_printf(sc->dev,
1440 "Error reading card status %d\n", err);
1443 if ((status & R1_CARD_IS_LOCKED) != 0) {
1444 device_printf(sc->dev,
1445 "Card is password protected, skipping.\n");
1449 /* Only MMC >= 4.x cards support EXT_CSD. */
1450 if (ivar->csd.spec_vers >= 4) {
1451 /* Card must be selected to fetch EXT_CSD. */
1452 mmc_select_card(sc, ivar->rca);
1453 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1454 /* Handle extended capacity from EXT_CSD */
1455 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1456 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1457 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1458 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1459 if (sec_count != 0) {
1460 ivar->sec_count = sec_count;
1463 /* Get card speed in high speed mode. */
1464 ivar->timing = bus_timing_hs;
1465 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1466 & EXT_CSD_CARD_TYPE_52)
1467 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1468 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1469 & EXT_CSD_CARD_TYPE_26)
1470 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1472 ivar->hs_tran_speed = ivar->tran_speed;
1473 /* Find max supported bus width. */
1474 ivar->bus_width = mmc_test_bus_width(sc);
1475 mmc_select_card(sc, 0);
1476 /* Handle HC erase sector size. */
1477 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1478 ivar->erase_sector = 1024 *
1479 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1480 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1481 EXT_CSD_ERASE_GRP_DEF, 1);
1484 ivar->bus_width = bus_width_1;
1485 ivar->timing = bus_timing_normal;
1489 * Some cards that report maximum I/O block sizes greater
1490 * than 512 require the block length to be set to 512, even
1491 * though that is supposed to be the default. Example:
1493 * Transcend 2GB SDSC card, CID:
1494 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1496 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1497 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1498 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1500 mmc_format_card_id_string(ivar);
1502 if (bootverbose || mmc_debug)
1503 mmc_log_card(sc->dev, ivar, newcard);
1506 child = device_add_child(sc->dev, NULL, -1);
1507 device_set_ivars(child, ivar);
1513 mmc_rescan_cards(struct mmc_softc *sc)
1515 struct mmc_ivars *ivar = NULL;
1517 int err, i, devcount;
1519 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1521 for (i = 0; i < devcount; i++) {
1522 ivar = device_get_ivars(devlist[i]);
1523 if (mmc_select_card(sc, ivar->rca)) {
1524 if (bootverbose || mmc_debug)
1525 device_printf(sc->dev, "Card at relative address %d lost.\n",
1527 device_delete_child(sc->dev, devlist[i]);
1528 free(ivar, M_DEVBUF);
1531 free(devlist, M_TEMP);
1532 mmc_select_card(sc, 0);
1536 mmc_delete_cards(struct mmc_softc *sc)
1538 struct mmc_ivars *ivar;
1540 int err, i, devcount;
1542 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1544 for (i = 0; i < devcount; i++) {
1545 ivar = device_get_ivars(devlist[i]);
1546 if (bootverbose || mmc_debug)
1547 device_printf(sc->dev, "Card at relative address %d deleted.\n",
1549 device_delete_child(sc->dev, devlist[i]);
1550 free(ivar, M_DEVBUF);
1552 free(devlist, M_TEMP);
1557 mmc_go_discovery(struct mmc_softc *sc)
1564 if (mmcbr_get_power_mode(dev) != power_on) {
1566 * First, try SD modes
1568 sc->squelched++; /* Errors are expected, squelch reporting. */
1569 mmcbr_set_mode(dev, mode_sd);
1571 mmcbr_set_bus_mode(dev, pushpull);
1572 if (bootverbose || mmc_debug)
1573 device_printf(sc->dev, "Probing bus\n");
1575 err = mmc_send_if_cond(sc, 1);
1576 if ((bootverbose || mmc_debug) && err == 0)
1577 device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1578 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1579 if (bootverbose || mmc_debug)
1580 device_printf(sc->dev, "SD probe: failed\n");
1584 mmcbr_set_mode(dev, mode_mmc);
1585 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1586 if (bootverbose || mmc_debug)
1587 device_printf(sc->dev, "MMC probe: failed\n");
1588 ocr = 0; /* Failed both, powerdown. */
1589 } else if (bootverbose || mmc_debug)
1590 device_printf(sc->dev,
1591 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1592 } else if (bootverbose || mmc_debug)
1593 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1596 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1597 if (mmcbr_get_ocr(dev) != 0)
1600 mmcbr_set_bus_mode(dev, opendrain);
1601 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1602 mmcbr_update_ios(dev);
1603 /* XXX recompute vdd based on new cards? */
1606 * Make sure that we have a mutually agreeable voltage to at least
1607 * one card on the bus.
1609 if (bootverbose || mmc_debug)
1610 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1611 if (mmcbr_get_ocr(dev) == 0) {
1612 device_printf(sc->dev, "No compatible cards found on bus\n");
1613 mmc_delete_cards(sc);
1618 * Reselect the cards after we've idled them above.
1620 if (mmcbr_get_mode(dev) == mode_sd) {
1621 err = mmc_send_if_cond(sc, 1);
1622 mmc_send_app_op_cond(sc,
1623 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1625 mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1626 mmc_discover_cards(sc);
1627 mmc_rescan_cards(sc);
1629 mmcbr_set_bus_mode(dev, pushpull);
1630 mmcbr_update_ios(dev);
1631 mmc_calculate_clock(sc);
1632 bus_generic_attach(dev);
1633 /* mmc_update_children_sysctl(dev);*/
1637 mmc_calculate_clock(struct mmc_softc *sc)
1639 int max_dtr, max_hs_dtr, max_timing;
1642 struct mmc_ivars *ivar;
1644 f_max = mmcbr_get_f_max(sc->dev);
1645 max_dtr = max_hs_dtr = f_max;
1646 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1647 max_timing = bus_timing_hs;
1649 max_timing = bus_timing_normal;
1650 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1651 panic("can't get children");
1652 for (i = 0; i < nkid; i++) {
1653 ivar = device_get_ivars(kids[i]);
1654 if (ivar->timing < max_timing)
1655 max_timing = ivar->timing;
1656 if (ivar->tran_speed < max_dtr)
1657 max_dtr = ivar->tran_speed;
1658 if (ivar->hs_tran_speed < max_hs_dtr)
1659 max_hs_dtr = ivar->hs_tran_speed;
1661 for (i = 0; i < nkid; i++) {
1662 ivar = device_get_ivars(kids[i]);
1663 if (ivar->timing == bus_timing_normal)
1665 mmc_select_card(sc, ivar->rca);
1666 mmc_set_timing(sc, max_timing);
1668 mmc_select_card(sc, 0);
1670 if (max_timing == bus_timing_hs)
1671 max_dtr = max_hs_dtr;
1672 if (bootverbose || mmc_debug) {
1673 device_printf(sc->dev,
1674 "setting transfer rate to %d.%03dMHz%s\n",
1675 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1676 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1678 mmcbr_set_timing(sc->dev, max_timing);
1679 mmcbr_set_clock(sc->dev, max_dtr);
1680 mmcbr_update_ios(sc->dev);
1685 mmc_scan(struct mmc_softc *sc)
1687 device_t dev = sc->dev;
1689 mmc_acquire_bus(dev, dev);
1690 mmc_go_discovery(sc);
1691 mmc_release_bus(dev, dev);
1695 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1697 struct mmc_ivars *ivar = device_get_ivars(child);
1702 case MMC_IVAR_DSR_IMP:
1703 *result = ivar->csd.dsr_imp;
1705 case MMC_IVAR_MEDIA_SIZE:
1706 *result = ivar->sec_count;
1709 *result = ivar->rca;
1711 case MMC_IVAR_SECTOR_SIZE:
1712 *result = MMC_SECTOR_SIZE;
1714 case MMC_IVAR_TRAN_SPEED:
1715 *result = mmcbr_get_clock(bus);
1717 case MMC_IVAR_READ_ONLY:
1718 *result = ivar->read_only;
1720 case MMC_IVAR_HIGH_CAP:
1721 *result = ivar->high_cap;
1723 case MMC_IVAR_CARD_TYPE:
1724 *result = ivar->mode;
1726 case MMC_IVAR_BUS_WIDTH:
1727 *result = ivar->bus_width;
1729 case MMC_IVAR_ERASE_SECTOR:
1730 *result = ivar->erase_sector;
1732 case MMC_IVAR_MAX_DATA:
1733 *result = mmcbr_get_max_data(bus);
1735 case MMC_IVAR_CARD_ID_STRING:
1736 *(char **)result = ivar->card_id_string;
1738 case MMC_IVAR_CARD_SN_STRING:
1739 *(char **)result = ivar->card_sn_string;
1746 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1749 * None are writable ATM
1755 mmc_delayed_attach(void *xsc)
1757 struct mmc_softc *sc = xsc;
1760 config_intrhook_disestablish(&sc->config_intrhook);
1764 mmc_child_location_str(device_t dev, device_t child, char *buf,
1768 snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1772 static device_method_t mmc_methods[] = {
1774 DEVMETHOD(device_probe, mmc_probe),
1775 DEVMETHOD(device_attach, mmc_attach),
1776 DEVMETHOD(device_detach, mmc_detach),
1777 DEVMETHOD(device_suspend, mmc_suspend),
1778 DEVMETHOD(device_resume, mmc_resume),
1781 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1782 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1783 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1785 /* MMC Bus interface */
1786 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1787 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1788 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1793 static driver_t mmc_driver = {
1796 sizeof(struct mmc_softc),
1798 static devclass_t mmc_devclass;
1800 DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL);
1801 DRIVER_MODULE(mmc, sdhci_bcm, mmc_driver, mmc_devclass, NULL, NULL);
1802 DRIVER_MODULE(mmc, sdhci_fdt, mmc_driver, mmc_devclass, NULL, NULL);
1803 DRIVER_MODULE(mmc, sdhci_imx, mmc_driver, mmc_devclass, NULL, NULL);
1804 DRIVER_MODULE(mmc, sdhci_pci, mmc_driver, mmc_devclass, NULL, NULL);
1805 DRIVER_MODULE(mmc, sdhci_ti, mmc_driver, mmc_devclass, NULL, NULL);
1806 DRIVER_MODULE(mmc, ti_mmchs, mmc_driver, mmc_devclass, NULL, NULL);