2 * Copyright (c) 2006 Bernd Walter. All rights reserved.
3 * Copyright (c) 2006 M. Warner Losh. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 * Portions of this software may have been developed with reference to
26 * the SD Simplified Specification. The following disclaimer may apply:
28 * The following conditions apply to the release of the simplified
29 * specification ("Simplified Specification") by the SD Card Association and
30 * the SD Group. The Simplified Specification is a subset of the complete SD
31 * Specification which is owned by the SD Card Association and the SD
32 * Group. This Simplified Specification is provided on a non-confidential
33 * basis subject to the disclaimers below. Any implementation of the
34 * Simplified Specification may require a license from the SD Card
35 * Association, SD Group, SD-3C LLC or other third parties.
39 * The information contained in the Simplified Specification is presented only
40 * as a standard specification for SD Cards and SD Host/Ancillary products and
41 * is provided "AS-IS" without any representations or warranties of any
42 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43 * Card Association for any damages, any infringements of patents or other
44 * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45 * parties, which may result from its use. No license is granted by
46 * implication, estoppel or otherwise under any patent or other rights of the
47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49 * or the SD Card Association to disclose or distribute any technical
50 * information, know-how or other confidential information to any third party.
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/malloc.h>
61 #include <sys/module.h>
62 #include <sys/mutex.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
67 #include <dev/mmc/mmcreg.h>
68 #include <dev/mmc/mmcbrvar.h>
69 #include <dev/mmc/mmcvar.h>
71 #include "mmcbus_if.h"
76 struct intr_config_hook config_intrhook;
85 uint32_t raw_cid[4]; /* Raw bits of the CID */
86 uint32_t raw_csd[4]; /* Raw bits of the CSD */
87 uint32_t raw_scr[2]; /* Raw bits of the SCR */
88 uint8_t raw_ext_csd[512]; /* Raw bits of the EXT_CSD */
89 uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */
91 enum mmc_card_mode mode;
92 struct mmc_cid cid; /* cid decoded */
93 struct mmc_csd csd; /* csd decoded */
94 struct mmc_scr scr; /* scr decoded */
95 struct mmc_sd_status sd_status; /* SD_STATUS decoded */
96 u_char read_only; /* True when the device is read-only */
97 u_char bus_width; /* Bus width to use */
98 u_char timing; /* Bus timing support */
99 u_char high_cap; /* High Capacity card (block addressed) */
100 uint32_t sec_count; /* Card capacity in 512byte blocks */
101 uint32_t tran_speed; /* Max speed in normal mode */
102 uint32_t hs_tran_speed; /* Max speed in high speed mode */
103 uint32_t erase_sector; /* Card native erase sector size */
104 char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
107 #define CMD_RETRIES 3
109 static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
111 static int mmc_debug;
112 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");
114 /* bus entry points */
115 static int mmc_acquire_bus(device_t busdev, device_t dev);
116 static int mmc_attach(device_t dev);
117 static int mmc_child_location_str(device_t dev, device_t child, char *buf,
119 static int mmc_detach(device_t dev);
120 static int mmc_probe(device_t dev);
121 static int mmc_read_ivar(device_t bus, device_t child, int which,
123 static int mmc_release_bus(device_t busdev, device_t dev);
124 static int mmc_resume(device_t dev);
125 static int mmc_suspend(device_t dev);
126 static int mmc_wait_for_request(device_t brdev, device_t reqdev,
127 struct mmc_request *req);
128 static int mmc_write_ivar(device_t bus, device_t child, int which,
131 #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
132 #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
133 #define MMC_LOCK_INIT(_sc) \
134 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
136 #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
137 #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
138 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
140 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
141 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
142 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
143 struct mmc_sd_status *sd_status);
144 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
145 uint32_t *rawsdstatus);
146 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
148 static int mmc_calculate_clock(struct mmc_softc *sc);
149 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid);
150 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
151 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
152 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
153 static void mmc_delayed_attach(void *xsc);
154 static int mmc_delete_cards(struct mmc_softc *sc);
155 static void mmc_discover_cards(struct mmc_softc *sc);
156 static void mmc_format_card_id_string(struct mmc_ivars *ivar);
157 static void mmc_go_discovery(struct mmc_softc *sc);
158 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
160 static int mmc_highest_voltage(uint32_t ocr);
161 static void mmc_idle_cards(struct mmc_softc *sc);
162 static void mmc_ms_delay(int ms);
163 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
164 static void mmc_power_down(struct mmc_softc *sc);
165 static void mmc_power_up(struct mmc_softc *sc);
166 static void mmc_rescan_cards(struct mmc_softc *sc);
167 static void mmc_scan(struct mmc_softc *sc);
168 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
169 uint8_t value, uint8_t *res);
170 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
171 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
172 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
174 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
175 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
176 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
177 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
179 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
180 static int mmc_send_status(struct mmc_softc *sc, uint16_t rca,
182 static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
183 static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca,
185 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
186 static int mmc_set_timing(struct mmc_softc *sc, int timing);
187 static int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index,
189 static int mmc_test_bus_width(struct mmc_softc *sc);
190 static int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
191 struct mmc_command *cmd, int retries);
192 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
194 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
195 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
196 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
197 static void mmc_wakeup(struct mmc_request *req);
203 DELAY(1000 * ms); /* XXX BAD */
207 mmc_probe(device_t dev)
210 device_set_desc(dev, "MMC/SD bus");
215 mmc_attach(device_t dev)
217 struct mmc_softc *sc;
219 sc = device_get_softc(dev);
223 /* We'll probe and attach our children later, but before / mount */
224 sc->config_intrhook.ich_func = mmc_delayed_attach;
225 sc->config_intrhook.ich_arg = sc;
226 if (config_intrhook_establish(&sc->config_intrhook) != 0)
227 device_printf(dev, "config_intrhook_establish failed\n");
232 mmc_detach(device_t dev)
234 struct mmc_softc *sc = device_get_softc(dev);
237 if ((err = mmc_delete_cards(sc)) != 0)
240 MMC_LOCK_DESTROY(sc);
246 mmc_suspend(device_t dev)
248 struct mmc_softc *sc = device_get_softc(dev);
251 err = bus_generic_suspend(dev);
259 mmc_resume(device_t dev)
261 struct mmc_softc *sc = device_get_softc(dev);
264 return (bus_generic_resume(dev));
268 mmc_acquire_bus(device_t busdev, device_t dev)
270 struct mmc_softc *sc;
271 struct mmc_ivars *ivar;
275 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
278 sc = device_get_softc(busdev);
281 panic("mmc: host bridge didn't serialize us.");
287 * Keep track of the last rca that we've selected. If
288 * we're asked to do it again, don't. We never
289 * unselect unless the bus code itself wants the mmc
290 * bus, and constantly reselecting causes problems.
292 rca = mmc_get_rca(dev);
293 if (sc->last_rca != rca) {
294 mmc_select_card(sc, rca);
296 /* Prepare bus width for the new card. */
297 ivar = device_get_ivars(dev);
298 if (bootverbose || mmc_debug) {
299 device_printf(busdev,
300 "setting bus width to %d bits\n",
301 (ivar->bus_width == bus_width_4) ? 4 :
302 (ivar->bus_width == bus_width_8) ? 8 : 1);
304 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
305 mmcbr_set_bus_width(busdev, ivar->bus_width);
306 mmcbr_update_ios(busdev);
310 * If there's a card selected, stand down.
312 if (sc->last_rca != 0) {
313 mmc_select_card(sc, 0);
322 mmc_release_bus(device_t busdev, device_t dev)
324 struct mmc_softc *sc;
327 sc = device_get_softc(busdev);
331 panic("mmc: releasing unowned bus.");
332 if (sc->owner != dev)
333 panic("mmc: you don't own the bus. game over.");
335 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
345 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
348 return (ocr & MMC_OCR_VOLTAGE);
352 mmc_highest_voltage(uint32_t ocr)
356 for (i = 30; i >= 0; i--)
363 mmc_wakeup(struct mmc_request *req)
365 struct mmc_softc *sc;
367 sc = (struct mmc_softc *)req->done_data;
369 req->flags |= MMC_REQ_DONE;
375 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
378 req->done = mmc_wakeup;
381 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
382 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
383 if (req->cmd->data) {
384 printf(" data %d\n", (int)req->cmd->data->len);
388 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
390 while ((req->flags & MMC_REQ_DONE) == 0)
391 msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
393 if (mmc_debug > 2 || (mmc_debug > 1 && req->cmd->error))
394 device_printf(sc->dev, "RESULT: %d\n", req->cmd->error);
399 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
401 struct mmc_softc *sc = device_get_softc(brdev);
403 return (mmc_wait_for_req(sc, req));
407 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
409 struct mmc_request mreq;
411 memset(&mreq, 0, sizeof(mreq));
412 memset(cmd->resp, 0, sizeof(cmd->resp));
413 cmd->retries = retries;
415 mmc_wait_for_req(sc, &mreq);
420 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
421 struct mmc_command *cmd, int retries)
423 struct mmc_command appcmd;
424 int err = MMC_ERR_NONE, i;
426 for (i = 0; i <= retries; i++) {
427 appcmd.opcode = MMC_APP_CMD;
428 appcmd.arg = rca << 16;
429 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
431 mmc_wait_for_cmd(sc, &appcmd, 0);
433 if (err != MMC_ERR_NONE)
435 if (!(appcmd.resp[0] & R1_APP_CMD))
436 return MMC_ERR_FAILED;
437 mmc_wait_for_cmd(sc, cmd, 0);
439 if (err == MMC_ERR_NONE)
446 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
447 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
449 struct mmc_command cmd;
452 memset(&cmd, 0, sizeof(cmd));
457 err = mmc_wait_for_cmd(sc, &cmd, retries);
463 if (flags & MMC_RSP_136)
464 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
472 mmc_idle_cards(struct mmc_softc *sc)
475 struct mmc_command cmd;
478 mmcbr_set_chip_select(dev, cs_high);
479 mmcbr_update_ios(dev);
482 memset(&cmd, 0, sizeof(cmd));
483 cmd.opcode = MMC_GO_IDLE_STATE;
485 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
487 mmc_wait_for_cmd(sc, &cmd, 0);
490 mmcbr_set_chip_select(dev, cs_dontcare);
491 mmcbr_update_ios(dev);
496 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
498 struct mmc_command cmd;
499 int err = MMC_ERR_NONE, i;
501 memset(&cmd, 0, sizeof(cmd));
502 cmd.opcode = ACMD_SD_SEND_OP_COND;
504 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
507 for (i = 0; i < 1000; i++) {
508 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
509 if (err != MMC_ERR_NONE)
511 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
512 (ocr & MMC_OCR_VOLTAGE) == 0)
514 err = MMC_ERR_TIMEOUT;
517 if (rocr && err == MMC_ERR_NONE)
523 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
525 struct mmc_command cmd;
526 int err = MMC_ERR_NONE, i;
528 memset(&cmd, 0, sizeof(cmd));
529 cmd.opcode = MMC_SEND_OP_COND;
531 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
534 for (i = 0; i < 1000; i++) {
535 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
536 if (err != MMC_ERR_NONE)
538 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
539 (ocr & MMC_OCR_VOLTAGE) == 0)
541 err = MMC_ERR_TIMEOUT;
544 if (rocr && err == MMC_ERR_NONE)
550 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
552 struct mmc_command cmd;
555 memset(&cmd, 0, sizeof(cmd));
556 cmd.opcode = SD_SEND_IF_COND;
557 cmd.arg = (vhs << 8) + 0xAA;
558 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
561 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
566 mmc_power_up(struct mmc_softc *sc)
571 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
572 mmcbr_set_bus_mode(dev, opendrain);
573 mmcbr_set_chip_select(dev, cs_dontcare);
574 mmcbr_set_bus_width(dev, bus_width_1);
575 mmcbr_set_power_mode(dev, power_up);
576 mmcbr_set_clock(dev, 0);
577 mmcbr_update_ios(dev);
580 mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev));
581 mmcbr_set_timing(dev, bus_timing_normal);
582 mmcbr_set_power_mode(dev, power_on);
583 mmcbr_update_ios(dev);
588 mmc_power_down(struct mmc_softc *sc)
590 device_t dev = sc->dev;
592 mmcbr_set_bus_mode(dev, opendrain);
593 mmcbr_set_chip_select(dev, cs_dontcare);
594 mmcbr_set_bus_width(dev, bus_width_1);
595 mmcbr_set_power_mode(dev, power_off);
596 mmcbr_set_clock(dev, 0);
597 mmcbr_set_timing(dev, bus_timing_normal);
598 mmcbr_update_ios(dev);
602 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
606 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
607 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
608 flags, NULL, CMD_RETRIES));
612 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
614 struct mmc_command cmd;
617 cmd.opcode = MMC_SWITCH_FUNC;
618 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
622 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
624 err = mmc_wait_for_cmd(sc, &cmd, 0);
629 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
633 struct mmc_command cmd;
634 struct mmc_data data;
636 memset(&cmd, 0, sizeof(struct mmc_command));
637 memset(&data, 0, sizeof(struct mmc_data));
640 cmd.opcode = SD_SWITCH_FUNC;
641 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
642 cmd.arg = mode << 31; /* 0 - check, 1 - set */
643 cmd.arg |= 0x00FFFFFF;
644 cmd.arg &= ~(0xF << (grp * 4));
645 cmd.arg |= value << (grp * 4);
650 data.flags = MMC_DATA_READ;
652 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
657 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
659 struct mmc_command cmd;
663 if (mmcbr_get_mode(sc->dev) == mode_sd) {
664 memset(&cmd, 0, sizeof(struct mmc_command));
665 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
666 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
667 cmd.arg = SD_CLR_CARD_DETECT;
668 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
671 memset(&cmd, 0, sizeof(struct mmc_command));
672 cmd.opcode = ACMD_SET_BUS_WIDTH;
673 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
676 cmd.arg = SD_BUS_WIDTH_1;
679 cmd.arg = SD_BUS_WIDTH_4;
682 return (MMC_ERR_INVALID);
684 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
688 value = EXT_CSD_BUS_WIDTH_1;
691 value = EXT_CSD_BUS_WIDTH_4;
694 value = EXT_CSD_BUS_WIDTH_8;
697 return (MMC_ERR_INVALID);
699 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
706 mmc_set_timing(struct mmc_softc *sc, int timing)
710 u_char switch_res[64];
713 case bus_timing_normal:
720 return (MMC_ERR_INVALID);
722 if (mmcbr_get_mode(sc->dev) == mode_sd)
723 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
726 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
727 EXT_CSD_HS_TIMING, value);
732 mmc_test_bus_width(struct mmc_softc *sc)
734 struct mmc_command cmd;
735 struct mmc_data data;
738 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
739 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
740 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, };
741 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
743 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
744 mmcbr_set_bus_width(sc->dev, bus_width_8);
745 mmcbr_update_ios(sc->dev);
747 cmd.opcode = MMC_BUSTEST_W;
749 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
754 data.flags = MMC_DATA_WRITE;
755 mmc_wait_for_cmd(sc, &cmd, 0);
757 cmd.opcode = MMC_BUSTEST_R;
759 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
764 data.flags = MMC_DATA_READ;
765 err = mmc_wait_for_cmd(sc, &cmd, 0);
767 mmcbr_set_bus_width(sc->dev, bus_width_1);
768 mmcbr_update_ios(sc->dev);
770 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
771 return (bus_width_8);
774 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
775 mmcbr_set_bus_width(sc->dev, bus_width_4);
776 mmcbr_update_ios(sc->dev);
778 cmd.opcode = MMC_BUSTEST_W;
780 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
785 data.flags = MMC_DATA_WRITE;
786 mmc_wait_for_cmd(sc, &cmd, 0);
788 cmd.opcode = MMC_BUSTEST_R;
790 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
795 data.flags = MMC_DATA_READ;
796 err = mmc_wait_for_cmd(sc, &cmd, 0);
798 mmcbr_set_bus_width(sc->dev, bus_width_1);
799 mmcbr_update_ios(sc->dev);
801 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
802 return (bus_width_4);
804 return (bus_width_1);
808 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
810 const int i = (bit_len / 32) - (start / 32) - 1;
811 const int shift = start & 31;
812 uint32_t retval = bits[i] >> shift;
813 if (size + shift > 32)
814 retval |= bits[i - 1] << (32 - shift);
815 return (retval & ((1llu << size) - 1));
819 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
823 /* There's no version info, so we take it on faith */
824 memset(cid, 0, sizeof(*cid));
825 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
826 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
827 for (i = 0; i < 5; i++)
828 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
830 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
831 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
832 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
833 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
837 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
841 /* There's no version info, so we take it on faith */
842 memset(cid, 0, sizeof(*cid));
843 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
844 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
845 for (i = 0; i < 6; i++)
846 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
848 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
849 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
850 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
851 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
855 mmc_format_card_id_string(struct mmc_ivars *ivar)
862 * Format a card ID string for use by the mmcsd driver, it's what
863 * appears between the <> in the following:
864 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
865 * 22.5MHz/4bit/128-block
867 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
868 * and our max formatted length is currently 55 bytes if every field
869 * contains the largest value.
871 * Sometimes the oid is two printable ascii chars; when it's not,
872 * format it as 0xnnnn instead.
874 c1 = (ivar->cid.oid >> 8) & 0x0ff;
875 c2 = ivar->cid.oid & 0x0ff;
876 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
877 snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
879 snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
880 snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
881 "%s%s %s %d.%d SN %u MFG %02d/%04d by %d %s",
882 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
883 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
884 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
885 ivar->cid.mid, oidstr);
888 static const int exp[8] = {
889 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
892 static const int mant[16] = {
893 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
896 static const int cur_min[8] = {
897 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
900 static const int cur_max[8] = {
901 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
905 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
911 memset(csd, 0, sizeof(*csd));
912 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
914 m = mmc_get_bits(raw_csd, 128, 115, 4);
915 e = mmc_get_bits(raw_csd, 128, 112, 3);
916 csd->tacc = (exp[e] * mant[m] + 9) / 10;
917 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
918 m = mmc_get_bits(raw_csd, 128, 99, 4);
919 e = mmc_get_bits(raw_csd, 128, 96, 3);
920 csd->tran_speed = exp[e] * 10000 * mant[m];
921 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
922 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
923 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
924 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
925 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
926 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
927 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
928 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
929 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
930 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
931 m = mmc_get_bits(raw_csd, 128, 62, 12);
932 e = mmc_get_bits(raw_csd, 128, 47, 3);
933 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
934 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
935 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
936 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
937 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
938 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
939 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
940 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
942 m = mmc_get_bits(raw_csd, 128, 115, 4);
943 e = mmc_get_bits(raw_csd, 128, 112, 3);
944 csd->tacc = (exp[e] * mant[m] + 9) / 10;
945 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
946 m = mmc_get_bits(raw_csd, 128, 99, 4);
947 e = mmc_get_bits(raw_csd, 128, 96, 3);
948 csd->tran_speed = exp[e] * 10000 * mant[m];
949 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
950 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
951 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
952 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
953 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
954 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
955 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
957 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
958 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
959 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
960 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
961 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
962 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
963 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
965 panic("unknown SD CSD version");
969 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
974 memset(csd, 0, sizeof(*csd));
975 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
976 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
977 m = mmc_get_bits(raw_csd, 128, 115, 4);
978 e = mmc_get_bits(raw_csd, 128, 112, 3);
979 csd->tacc = exp[e] * mant[m] + 9 / 10;
980 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
981 m = mmc_get_bits(raw_csd, 128, 99, 4);
982 e = mmc_get_bits(raw_csd, 128, 96, 3);
983 csd->tran_speed = exp[e] * 10000 * mant[m];
984 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
985 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
986 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
987 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
988 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
989 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
990 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
991 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
992 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
993 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
994 m = mmc_get_bits(raw_csd, 128, 62, 12);
995 e = mmc_get_bits(raw_csd, 128, 47, 3);
996 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
997 csd->erase_blk_en = 0;
998 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
999 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1000 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1001 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1002 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1003 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1004 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1008 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1010 unsigned int scr_struct;
1012 memset(scr, 0, sizeof(*scr));
1014 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1015 if (scr_struct != 0) {
1016 printf("Unrecognised SCR structure version %d\n",
1020 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1021 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1025 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1026 struct mmc_sd_status *sd_status)
1029 memset(sd_status, 0, sizeof(*sd_status));
1031 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1032 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1033 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1034 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1035 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1036 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1037 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1038 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1039 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1040 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1044 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1046 struct mmc_command cmd;
1049 cmd.opcode = MMC_ALL_SEND_CID;
1051 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1053 err = mmc_wait_for_cmd(sc, &cmd, 0);
1054 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1059 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1061 struct mmc_command cmd;
1064 cmd.opcode = MMC_SEND_CSD;
1065 cmd.arg = rca << 16;
1066 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1068 err = mmc_wait_for_cmd(sc, &cmd, 0);
1069 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1074 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1077 struct mmc_command cmd;
1078 struct mmc_data data;
1080 memset(&cmd, 0, sizeof(struct mmc_command));
1081 memset(&data, 0, sizeof(struct mmc_data));
1083 memset(rawscr, 0, 8);
1084 cmd.opcode = ACMD_SEND_SCR;
1085 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1091 data.flags = MMC_DATA_READ;
1093 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1094 rawscr[0] = be32toh(rawscr[0]);
1095 rawscr[1] = be32toh(rawscr[1]);
1100 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1103 struct mmc_command cmd;
1104 struct mmc_data data;
1106 memset(&cmd, 0, sizeof(struct mmc_command));
1107 memset(&data, 0, sizeof(struct mmc_data));
1109 memset(rawextcsd, 0, 512);
1110 cmd.opcode = MMC_SEND_EXT_CSD;
1111 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1115 data.data = rawextcsd;
1117 data.flags = MMC_DATA_READ;
1119 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1124 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1127 struct mmc_command cmd;
1128 struct mmc_data data;
1130 memset(&cmd, 0, sizeof(struct mmc_command));
1131 memset(&data, 0, sizeof(struct mmc_data));
1133 memset(rawsdstatus, 0, 64);
1134 cmd.opcode = ACMD_SD_STATUS;
1135 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1139 data.data = rawsdstatus;
1141 data.flags = MMC_DATA_READ;
1143 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1144 for (i = 0; i < 16; i++)
1145 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1150 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1152 struct mmc_command cmd;
1155 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1156 cmd.arg = resp << 16;
1157 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1159 err = mmc_wait_for_cmd(sc, &cmd, 0);
1164 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1166 struct mmc_command cmd;
1169 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1171 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1173 err = mmc_wait_for_cmd(sc, &cmd, 0);
1174 *resp = cmd.resp[0];
1179 mmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status)
1181 struct mmc_command cmd;
1184 cmd.opcode = MMC_SEND_STATUS;
1185 cmd.arg = rca << 16;
1186 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1188 err = mmc_wait_for_cmd(sc, &cmd, 0);
1189 *status = cmd.resp[0];
1194 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1196 struct mmc_command cmd;
1199 cmd.opcode = MMC_SET_BLOCKLEN;
1201 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1203 err = mmc_wait_for_cmd(sc, &cmd, 0);
1208 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1210 device_printf(dev, "Card at relative address %d%s:\n",
1211 ivar->rca, newcard ? " added" : "");
1212 device_printf(dev, " card: %s\n", ivar->card_id_string);
1213 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1214 (ivar->bus_width == bus_width_1 ? 1 :
1215 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1216 (ivar->timing == bus_timing_hs ?
1217 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1218 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1219 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1220 ivar->sec_count, ivar->erase_sector,
1221 ivar->read_only ? ", read-only" : "");
1225 mmc_discover_cards(struct mmc_softc *sc)
1227 struct mmc_ivars *ivar = NULL;
1229 int err, i, devcount, newcard;
1230 uint32_t raw_cid[4], resp, sec_count, status;
1233 u_char switch_res[64];
1235 if (bootverbose || mmc_debug)
1236 device_printf(sc->dev, "Probing cards\n");
1238 err = mmc_all_send_cid(sc, raw_cid);
1239 if (err == MMC_ERR_TIMEOUT)
1241 if (err != MMC_ERR_NONE) {
1242 device_printf(sc->dev, "Error reading CID %d\n", err);
1246 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1248 for (i = 0; i < devcount; i++) {
1249 ivar = device_get_ivars(devlist[i]);
1250 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1255 free(devlist, M_TEMP);
1256 if (bootverbose || mmc_debug) {
1257 device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1258 newcard ? "New c" : "C",
1259 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1262 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1266 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1268 if (mmcbr_get_ro(sc->dev))
1269 ivar->read_only = 1;
1270 ivar->bus_width = bus_width_1;
1271 ivar->timing = bus_timing_normal;
1272 ivar->mode = mmcbr_get_mode(sc->dev);
1273 if (ivar->mode == mode_sd) {
1274 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1275 mmc_send_relative_addr(sc, &resp);
1276 ivar->rca = resp >> 16;
1278 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1279 if (bootverbose || mmc_debug)
1280 device_printf(sc->dev,
1281 "%sard detected (CSD %08x%08x%08x%08x)\n",
1282 newcard ? "New c" : "C", ivar->raw_csd[0],
1283 ivar->raw_csd[1], ivar->raw_csd[2],
1285 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1286 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1287 if (ivar->csd.csd_structure > 0)
1289 ivar->tran_speed = ivar->csd.tran_speed;
1290 ivar->erase_sector = ivar->csd.erase_sector *
1291 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1293 err = mmc_send_status(sc, ivar->rca, &status);
1294 if (err != MMC_ERR_NONE) {
1295 device_printf(sc->dev,
1296 "Error reading card status %d\n", err);
1299 if ((status & R1_CARD_IS_LOCKED) != 0) {
1300 device_printf(sc->dev,
1301 "Card is password protected, skipping.\n");
1305 /* Get card SCR. Card must be selected to fetch it. */
1306 mmc_select_card(sc, ivar->rca);
1307 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1308 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1309 /* Get card switch capabilities (command class 10). */
1310 if ((ivar->scr.sda_vsn >= 1) &&
1311 (ivar->csd.ccc & (1<<10))) {
1312 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1313 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1315 if (switch_res[13] & 2) {
1316 ivar->timing = bus_timing_hs;
1317 ivar->hs_tran_speed = SD_MAX_HS;
1320 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1321 mmc_app_decode_sd_status(ivar->raw_sd_status,
1323 if (ivar->sd_status.au_size != 0) {
1324 ivar->erase_sector =
1325 16 << ivar->sd_status.au_size;
1327 mmc_select_card(sc, 0);
1328 /* Find max supported bus width. */
1329 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1330 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1331 ivar->bus_width = bus_width_4;
1334 * Some cards that report maximum I/O block sizes
1335 * greater than 512 require the block length to be
1336 * set to 512, even though that is supposed to be
1337 * the default. Example:
1339 * Transcend 2GB SDSC card, CID:
1340 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1342 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1343 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1344 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1346 mmc_format_card_id_string(ivar);
1348 if (bootverbose || mmc_debug)
1349 mmc_log_card(sc->dev, ivar, newcard);
1352 child = device_add_child(sc->dev, NULL, -1);
1353 device_set_ivars(child, ivar);
1357 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1359 mmc_set_relative_addr(sc, ivar->rca);
1361 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1362 if (bootverbose || mmc_debug)
1363 device_printf(sc->dev,
1364 "%sard detected (CSD %08x%08x%08x%08x)\n",
1365 newcard ? "New c" : "C", ivar->raw_csd[0],
1366 ivar->raw_csd[1], ivar->raw_csd[2],
1369 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1370 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1371 ivar->tran_speed = ivar->csd.tran_speed;
1372 ivar->erase_sector = ivar->csd.erase_sector *
1373 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1375 err = mmc_send_status(sc, ivar->rca, &status);
1376 if (err != MMC_ERR_NONE) {
1377 device_printf(sc->dev,
1378 "Error reading card status %d\n", err);
1381 if ((status & R1_CARD_IS_LOCKED) != 0) {
1382 device_printf(sc->dev,
1383 "Card is password protected, skipping.\n");
1387 /* Only MMC >= 4.x cards support EXT_CSD. */
1388 if (ivar->csd.spec_vers >= 4) {
1389 /* Card must be selected to fetch EXT_CSD. */
1390 mmc_select_card(sc, ivar->rca);
1391 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1392 /* Handle extended capacity from EXT_CSD */
1393 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1394 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1395 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1396 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1397 if (sec_count != 0) {
1398 ivar->sec_count = sec_count;
1401 /* Get card speed in high speed mode. */
1402 ivar->timing = bus_timing_hs;
1403 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1404 & EXT_CSD_CARD_TYPE_52)
1405 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1406 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1407 & EXT_CSD_CARD_TYPE_26)
1408 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1410 ivar->hs_tran_speed = ivar->tran_speed;
1411 /* Find max supported bus width. */
1412 ivar->bus_width = mmc_test_bus_width(sc);
1413 mmc_select_card(sc, 0);
1414 /* Handle HC erase sector size. */
1415 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1416 ivar->erase_sector = 1024 *
1417 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1418 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1419 EXT_CSD_ERASE_GRP_DEF, 1);
1422 ivar->bus_width = bus_width_1;
1423 ivar->timing = bus_timing_normal;
1427 * Some cards that report maximum I/O block sizes greater
1428 * than 512 require the block length to be set to 512, even
1429 * though that is supposed to be the default. Example:
1431 * Transcend 2GB SDSC card, CID:
1432 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1434 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1435 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1436 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1438 mmc_format_card_id_string(ivar);
1440 if (bootverbose || mmc_debug)
1441 mmc_log_card(sc->dev, ivar, newcard);
1444 child = device_add_child(sc->dev, NULL, -1);
1445 device_set_ivars(child, ivar);
1451 mmc_rescan_cards(struct mmc_softc *sc)
1453 struct mmc_ivars *ivar = NULL;
1455 int err, i, devcount;
1457 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1459 for (i = 0; i < devcount; i++) {
1460 ivar = device_get_ivars(devlist[i]);
1461 if (mmc_select_card(sc, ivar->rca)) {
1462 if (bootverbose || mmc_debug)
1463 device_printf(sc->dev, "Card at relative address %d lost.\n",
1465 device_delete_child(sc->dev, devlist[i]);
1466 free(ivar, M_DEVBUF);
1469 free(devlist, M_TEMP);
1470 mmc_select_card(sc, 0);
1474 mmc_delete_cards(struct mmc_softc *sc)
1476 struct mmc_ivars *ivar;
1478 int err, i, devcount;
1480 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1482 for (i = 0; i < devcount; i++) {
1483 ivar = device_get_ivars(devlist[i]);
1484 if (bootverbose || mmc_debug)
1485 device_printf(sc->dev, "Card at relative address %d deleted.\n",
1487 device_delete_child(sc->dev, devlist[i]);
1488 free(ivar, M_DEVBUF);
1490 free(devlist, M_TEMP);
1495 mmc_go_discovery(struct mmc_softc *sc)
1502 if (mmcbr_get_power_mode(dev) != power_on) {
1504 * First, try SD modes
1506 mmcbr_set_mode(dev, mode_sd);
1508 mmcbr_set_bus_mode(dev, pushpull);
1509 if (bootverbose || mmc_debug)
1510 device_printf(sc->dev, "Probing bus\n");
1512 err = mmc_send_if_cond(sc, 1);
1513 if ((bootverbose || mmc_debug) && err == 0)
1514 device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1515 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1516 if (bootverbose || mmc_debug)
1517 device_printf(sc->dev, "SD probe: failed\n");
1521 mmcbr_set_mode(dev, mode_mmc);
1522 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1523 if (bootverbose || mmc_debug)
1524 device_printf(sc->dev, "MMC probe: failed\n");
1525 ocr = 0; /* Failed both, powerdown. */
1526 } else if (bootverbose || mmc_debug)
1527 device_printf(sc->dev,
1528 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1529 } else if (bootverbose || mmc_debug)
1530 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1532 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1533 if (mmcbr_get_ocr(dev) != 0)
1536 mmcbr_set_bus_mode(dev, opendrain);
1537 mmcbr_set_clock(dev, mmcbr_get_f_min(dev));
1538 mmcbr_update_ios(dev);
1539 /* XXX recompute vdd based on new cards? */
1542 * Make sure that we have a mutually agreeable voltage to at least
1543 * one card on the bus.
1545 if (bootverbose || mmc_debug)
1546 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1547 if (mmcbr_get_ocr(dev) == 0) {
1548 mmc_delete_cards(sc);
1553 * Reselect the cards after we've idled them above.
1555 if (mmcbr_get_mode(dev) == mode_sd) {
1556 err = mmc_send_if_cond(sc, 1);
1557 mmc_send_app_op_cond(sc,
1558 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1560 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
1561 mmc_discover_cards(sc);
1562 mmc_rescan_cards(sc);
1564 mmcbr_set_bus_mode(dev, pushpull);
1565 mmcbr_update_ios(dev);
1566 mmc_calculate_clock(sc);
1567 bus_generic_attach(dev);
1568 /* mmc_update_children_sysctl(dev);*/
1572 mmc_calculate_clock(struct mmc_softc *sc)
1574 int max_dtr, max_hs_dtr, max_timing;
1575 int nkid, i, f_min, f_max;
1577 struct mmc_ivars *ivar;
1579 f_min = mmcbr_get_f_min(sc->dev);
1580 f_max = mmcbr_get_f_max(sc->dev);
1581 max_dtr = max_hs_dtr = f_max;
1582 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1583 max_timing = bus_timing_hs;
1585 max_timing = bus_timing_normal;
1586 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1587 panic("can't get children");
1588 for (i = 0; i < nkid; i++) {
1589 ivar = device_get_ivars(kids[i]);
1590 if (ivar->timing < max_timing)
1591 max_timing = ivar->timing;
1592 if (ivar->tran_speed < max_dtr)
1593 max_dtr = ivar->tran_speed;
1594 if (ivar->hs_tran_speed < max_hs_dtr)
1595 max_hs_dtr = ivar->hs_tran_speed;
1597 for (i = 0; i < nkid; i++) {
1598 ivar = device_get_ivars(kids[i]);
1599 if (ivar->timing == bus_timing_normal)
1601 mmc_select_card(sc, ivar->rca);
1602 mmc_set_timing(sc, max_timing);
1604 mmc_select_card(sc, 0);
1606 if (max_timing == bus_timing_hs)
1607 max_dtr = max_hs_dtr;
1608 if (bootverbose || mmc_debug) {
1609 device_printf(sc->dev,
1610 "setting transfer rate to %d.%03dMHz%s\n",
1611 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1612 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1614 mmcbr_set_timing(sc->dev, max_timing);
1615 mmcbr_set_clock(sc->dev, max_dtr);
1616 mmcbr_update_ios(sc->dev);
1621 mmc_scan(struct mmc_softc *sc)
1623 device_t dev = sc->dev;
1625 mmc_acquire_bus(dev, dev);
1626 mmc_go_discovery(sc);
1627 mmc_release_bus(dev, dev);
1631 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1633 struct mmc_ivars *ivar = device_get_ivars(child);
1638 case MMC_IVAR_DSR_IMP:
1639 *result = ivar->csd.dsr_imp;
1641 case MMC_IVAR_MEDIA_SIZE:
1642 *result = ivar->sec_count;
1645 *result = ivar->rca;
1647 case MMC_IVAR_SECTOR_SIZE:
1648 *result = MMC_SECTOR_SIZE;
1650 case MMC_IVAR_TRAN_SPEED:
1651 *result = mmcbr_get_clock(bus);
1653 case MMC_IVAR_READ_ONLY:
1654 *result = ivar->read_only;
1656 case MMC_IVAR_HIGH_CAP:
1657 *result = ivar->high_cap;
1659 case MMC_IVAR_CARD_TYPE:
1660 *result = ivar->mode;
1662 case MMC_IVAR_BUS_WIDTH:
1663 *result = ivar->bus_width;
1665 case MMC_IVAR_ERASE_SECTOR:
1666 *result = ivar->erase_sector;
1668 case MMC_IVAR_MAX_DATA:
1669 *result = mmcbr_get_max_data(bus);
1671 case MMC_IVAR_CARD_ID_STRING:
1672 *(char **)result = ivar->card_id_string;
1679 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1682 * None are writable ATM
1688 mmc_delayed_attach(void *xsc)
1690 struct mmc_softc *sc = xsc;
1693 config_intrhook_disestablish(&sc->config_intrhook);
1697 mmc_child_location_str(device_t dev, device_t child, char *buf,
1701 snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1705 static device_method_t mmc_methods[] = {
1707 DEVMETHOD(device_probe, mmc_probe),
1708 DEVMETHOD(device_attach, mmc_attach),
1709 DEVMETHOD(device_detach, mmc_detach),
1710 DEVMETHOD(device_suspend, mmc_suspend),
1711 DEVMETHOD(device_resume, mmc_resume),
1714 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1715 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1716 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1718 /* MMC Bus interface */
1719 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1720 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1721 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1726 static driver_t mmc_driver = {
1729 sizeof(struct mmc_softc),
1731 static devclass_t mmc_devclass;
1733 DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL);
1734 DRIVER_MODULE(mmc, sdhci, mmc_driver, mmc_devclass, NULL, NULL);