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.
52 * $FreeBSD: src/sys/dev/mmc/mmc.c,v 1.38 2009/08/20 19:17:53 jhb Exp $
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/malloc.h>
60 #include <sys/module.h>
61 #include <sys/spinlock.h>
63 #include <sys/endian.h>
64 #include <sys/sysctl.h>
67 #include <bus/mmc/mmcreg.h>
68 #include <bus/mmc/mmcbrvar.h>
69 #include <bus/mmc/mmcvar.h>
71 #include "mmcbus_if.h"
76 struct intr_config_hook config_intrhook;
79 int squelched; /* suppress reporting of (expected) errors */
81 struct timeval log_time;
84 #define LOG_PPS 5 /* Log no more than 5 errors per second. */
90 uint32_t raw_cid[4]; /* Raw bits of the CID */
91 uint32_t raw_csd[4]; /* Raw bits of the CSD */
92 uint32_t raw_scr[2]; /* Raw bits of the SCR */
93 uint8_t raw_ext_csd[512]; /* Raw bits of the EXT_CSD */
94 uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */
96 enum mmc_card_mode mode;
97 struct mmc_cid cid; /* cid decoded */
98 struct mmc_csd csd; /* csd decoded */
99 struct mmc_scr scr; /* scr decoded */
100 struct mmc_sd_status sd_status; /* SD_STATUS decoded */
101 u_char read_only; /* True when the device is read-only */
102 u_char bus_width; /* Bus width to use */
103 u_char timing; /* Bus timing support */
104 u_char high_cap; /* High Capacity card (block addressed) */
105 uint32_t sec_count; /* Card capacity in 512byte blocks */
106 uint32_t tran_speed; /* Max speed in normal mode */
107 uint32_t hs_tran_speed; /* Max speed in high speed mode */
108 uint32_t erase_sector; /* Card native erase sector size */
109 char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
110 char card_sn_string[16];/* Formatted serial # for disk->d_ident */
113 #define CMD_RETRIES 3
115 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
117 SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
119 static int mmc_debug;
120 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");
122 /* bus entry points */
123 static int mmc_acquire_bus(device_t busdev, device_t dev);
124 static int mmc_attach(device_t dev);
125 static int mmc_child_location_str(device_t dev, device_t child, char *buf,
127 static int mmc_detach(device_t dev);
128 static int mmc_probe(device_t dev);
129 static int mmc_read_ivar(device_t bus, device_t child, int which,
131 static int mmc_release_bus(device_t busdev, device_t dev);
132 static int mmc_resume(device_t dev);
133 static int mmc_suspend(device_t dev);
134 static int mmc_wait_for_request(device_t brdev, device_t reqdev,
135 struct mmc_request *req);
136 static int mmc_write_ivar(device_t bus, device_t child, int which,
139 #define MMC_LOCK(_sc) lockmgr(&(_sc)->sc_lock, LK_EXCLUSIVE)
140 #define MMC_UNLOCK(_sc) lockmgr(&(_sc)->sc_lock, LK_RELEASE)
141 #define MMC_LOCK_INIT(_sc) lockinit(&(_sc)->sc_lock, "mmc", 0, LK_CANRECURSE)
142 #define MMC_LOCK_DESTROY(_sc) lockuninit(&(_sc)->sc_lock);
143 #define MMC_ASSERT_LOCKED(_sc) KKASSERT(lockstatus(&(_sc)->sc_lock, curthread) != 0);
144 #define MMC_ASSERT_UNLOCKED(_sc) KKASSERT(lockstatus(&(_sc)->sc_lock, curthread) == 0);
146 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
147 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
148 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
149 struct mmc_sd_status *sd_status);
150 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
151 uint32_t *rawsdstatus);
152 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
154 static int mmc_calculate_clock(struct mmc_softc *sc);
155 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid);
156 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
157 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
158 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
159 static void mmc_delayed_attach(void *xsc);
160 static int mmc_delete_cards(struct mmc_softc *sc);
161 static void mmc_discover_cards(struct mmc_softc *sc);
162 static void mmc_format_card_id_string(struct mmc_ivars *ivar);
163 static void mmc_go_discovery(struct mmc_softc *sc);
164 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
166 static int mmc_highest_voltage(uint32_t ocr);
167 static void mmc_idle_cards(struct mmc_softc *sc);
168 static void mmc_ms_delay(int ms);
169 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
170 static void mmc_power_down(struct mmc_softc *sc);
171 static void mmc_power_up(struct mmc_softc *sc);
172 static void mmc_rescan_cards(struct mmc_softc *sc);
173 static void mmc_scan(struct mmc_softc *sc);
174 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
175 uint8_t value, uint8_t *res);
176 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
177 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
178 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
180 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
181 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
182 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
183 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
185 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
186 static int mmc_send_status(struct mmc_softc *sc, uint16_t rca,
188 static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
189 static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca,
191 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
192 static int mmc_set_timing(struct mmc_softc *sc, int timing);
193 static int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index,
195 static int mmc_test_bus_width(struct mmc_softc *sc);
196 static int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
197 struct mmc_command *cmd, int retries);
198 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
200 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
201 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
202 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
203 static void mmc_wakeup(struct mmc_request *req);
209 DELAY(1000 * ms); /* XXX BAD */
213 mmc_probe(device_t dev)
216 device_set_desc(dev, "MMC/SD bus");
221 mmc_attach(device_t dev)
223 struct mmc_softc *sc;
225 sc = device_get_softc(dev);
229 /* We'll probe and attach our children later, but before / mount */
230 sc->config_intrhook.ich_func = mmc_delayed_attach;
231 sc->config_intrhook.ich_arg = sc;
232 sc->config_intrhook.ich_desc = "mmc";
233 if (config_intrhook_establish(&sc->config_intrhook) != 0)
234 device_printf(dev, "config_intrhook_establish failed\n");
239 mmc_detach(device_t dev)
241 struct mmc_softc *sc = device_get_softc(dev);
244 if ((err = mmc_delete_cards(sc)) != 0)
247 MMC_LOCK_DESTROY(sc);
253 mmc_suspend(device_t dev)
255 struct mmc_softc *sc = device_get_softc(dev);
258 err = bus_generic_suspend(dev);
266 mmc_resume(device_t dev)
268 struct mmc_softc *sc = device_get_softc(dev);
271 return (bus_generic_resume(dev));
275 mmc_acquire_bus(device_t busdev, device_t dev)
277 struct mmc_softc *sc;
278 struct mmc_ivars *ivar;
282 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
285 sc = device_get_softc(busdev);
288 panic("mmc: host bridge didn't serialize us.");
294 * Keep track of the last rca that we've selected. If
295 * we're asked to do it again, don't. We never
296 * unselect unless the bus code itself wants the mmc
297 * bus, and constantly reselecting causes problems.
299 rca = mmc_get_rca(dev);
300 if (sc->last_rca != rca) {
301 mmc_select_card(sc, rca);
303 /* Prepare bus width for the new card. */
304 ivar = device_get_ivars(dev);
305 if (bootverbose || mmc_debug) {
306 device_printf(busdev,
307 "setting bus width to %d bits\n",
308 (ivar->bus_width == bus_width_4) ? 4 :
309 (ivar->bus_width == bus_width_8) ? 8 : 1);
311 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
312 mmcbr_set_bus_width(busdev, ivar->bus_width);
313 mmcbr_update_ios(busdev);
317 * If there's a card selected, stand down.
319 if (sc->last_rca != 0) {
320 mmc_select_card(sc, 0);
329 mmc_release_bus(device_t busdev, device_t dev)
331 struct mmc_softc *sc;
334 sc = device_get_softc(busdev);
338 panic("mmc: releasing unowned bus.");
339 if (sc->owner != dev)
340 panic("mmc: you don't own the bus. game over.");
342 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
352 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
355 return (ocr & MMC_OCR_VOLTAGE);
359 mmc_highest_voltage(uint32_t ocr)
363 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
364 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
371 mmc_wakeup(struct mmc_request *req)
373 struct mmc_softc *sc;
375 sc = (struct mmc_softc *)req->done_data;
377 req->flags |= MMC_REQ_DONE;
383 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
386 req->done = mmc_wakeup;
389 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
390 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
391 if (req->cmd->data) {
392 kprintf(" data %d\n", (int)req->cmd->data->len);
396 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
398 while ((req->flags & MMC_REQ_DONE) == 0)
399 lksleep(req, &sc->sc_lock, 0, "mmcreq", 0);
401 if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
402 device_printf(sc->dev, "CMD%d RESULT: %d\n",
403 req->cmd->opcode, req->cmd->error);
408 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
410 struct mmc_softc *sc = device_get_softc(brdev);
412 return (mmc_wait_for_req(sc, req));
416 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
418 struct mmc_request mreq;
422 memset(&mreq, 0, sizeof(mreq));
423 memset(cmd->resp, 0, sizeof(cmd->resp));
424 cmd->retries = 0; /* Retries done here, not in hardware. */
427 if (mmc_wait_for_req(sc, &mreq) != 0)
428 err = MMC_ERR_FAILED;
431 } while (err != MMC_ERR_NONE && retries-- > 0);
433 if (err != MMC_ERR_NONE && sc->squelched == 0) {
434 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
435 device_printf(sc->dev, "CMD%d failed, RESULT: %d\n",
444 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
445 struct mmc_command *cmd, int retries)
447 struct mmc_command appcmd;
450 /* Squelch error reporting at lower levels, we report below. */
453 memset(&appcmd, 0, sizeof(appcmd));
454 appcmd.opcode = MMC_APP_CMD;
455 appcmd.arg = rca << 16;
456 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
458 if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0)
459 err = MMC_ERR_FAILED;
462 if (err == MMC_ERR_NONE) {
463 if (!(appcmd.resp[0] & R1_APP_CMD))
464 err = MMC_ERR_FAILED;
465 else if (mmc_wait_for_cmd(sc, cmd, 0) != 0)
466 err = MMC_ERR_FAILED;
470 } while (err != MMC_ERR_NONE && retries-- > 0);
473 if (err != MMC_ERR_NONE && sc->squelched == 0) {
474 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
475 device_printf(sc->dev, "ACMD%d failed, RESULT: %d\n",
484 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
485 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
487 struct mmc_command cmd;
490 memset(&cmd, 0, sizeof(cmd));
495 err = mmc_wait_for_cmd(sc, &cmd, retries);
499 if (flags & MMC_RSP_136)
500 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
508 mmc_idle_cards(struct mmc_softc *sc)
511 struct mmc_command cmd;
514 mmcbr_set_chip_select(dev, cs_high);
515 mmcbr_update_ios(dev);
518 memset(&cmd, 0, sizeof(cmd));
519 cmd.opcode = MMC_GO_IDLE_STATE;
521 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
523 mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
526 mmcbr_set_chip_select(dev, cs_dontcare);
527 mmcbr_update_ios(dev);
532 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
534 struct mmc_command cmd;
535 int err = MMC_ERR_NONE, i;
537 memset(&cmd, 0, sizeof(cmd));
538 cmd.opcode = ACMD_SD_SEND_OP_COND;
540 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
543 for (i = 0; i < 1000; i++) {
544 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
545 if (err != MMC_ERR_NONE)
547 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
548 (ocr & MMC_OCR_VOLTAGE) == 0)
550 err = MMC_ERR_TIMEOUT;
553 if (rocr && err == MMC_ERR_NONE)
559 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
561 struct mmc_command cmd;
562 int err = MMC_ERR_NONE, i;
564 memset(&cmd, 0, sizeof(cmd));
565 cmd.opcode = MMC_SEND_OP_COND;
567 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
570 for (i = 0; i < 1000; i++) {
571 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
572 if (err != MMC_ERR_NONE)
574 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
575 (ocr & MMC_OCR_VOLTAGE) == 0)
577 err = MMC_ERR_TIMEOUT;
580 if (rocr && err == MMC_ERR_NONE)
586 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
588 struct mmc_command cmd;
591 memset(&cmd, 0, sizeof(cmd));
592 cmd.opcode = SD_SEND_IF_COND;
593 cmd.arg = (vhs << 8) + 0xAA;
594 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
597 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
602 mmc_power_up(struct mmc_softc *sc)
607 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
608 mmcbr_set_bus_mode(dev, opendrain);
609 mmcbr_set_chip_select(dev, cs_dontcare);
610 mmcbr_set_bus_width(dev, bus_width_1);
611 mmcbr_set_power_mode(dev, power_up);
612 mmcbr_set_clock(dev, 0);
613 mmcbr_update_ios(dev);
616 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
617 mmcbr_set_timing(dev, bus_timing_normal);
618 mmcbr_set_power_mode(dev, power_on);
619 mmcbr_update_ios(dev);
624 mmc_power_down(struct mmc_softc *sc)
626 device_t dev = sc->dev;
628 mmcbr_set_bus_mode(dev, opendrain);
629 mmcbr_set_chip_select(dev, cs_dontcare);
630 mmcbr_set_bus_width(dev, bus_width_1);
631 mmcbr_set_power_mode(dev, power_off);
632 mmcbr_set_clock(dev, 0);
633 mmcbr_set_timing(dev, bus_timing_normal);
634 mmcbr_update_ios(dev);
638 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
642 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
643 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
644 flags, NULL, CMD_RETRIES));
648 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
650 struct mmc_command cmd;
653 memset(&cmd, 0, sizeof(cmd));
654 cmd.opcode = MMC_SWITCH_FUNC;
655 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
659 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
661 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
666 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
670 struct mmc_command cmd;
671 struct mmc_data data;
673 memset(&cmd, 0, sizeof(cmd));
674 memset(&data, 0, sizeof(data));
677 cmd.opcode = SD_SWITCH_FUNC;
678 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
679 cmd.arg = mode << 31; /* 0 - check, 1 - set */
680 cmd.arg |= 0x00FFFFFF;
681 cmd.arg &= ~(0xF << (grp * 4));
682 cmd.arg |= value << (grp * 4);
687 data.flags = MMC_DATA_READ;
689 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
694 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
696 struct mmc_command cmd;
700 if (mmcbr_get_mode(sc->dev) == mode_sd) {
701 memset(&cmd, 0, sizeof(cmd));
702 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
703 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
704 cmd.arg = SD_CLR_CARD_DETECT;
705 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
708 memset(&cmd, 0, sizeof(cmd));
709 cmd.opcode = ACMD_SET_BUS_WIDTH;
710 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
713 cmd.arg = SD_BUS_WIDTH_1;
716 cmd.arg = SD_BUS_WIDTH_4;
719 return (MMC_ERR_INVALID);
721 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
725 value = EXT_CSD_BUS_WIDTH_1;
728 value = EXT_CSD_BUS_WIDTH_4;
731 value = EXT_CSD_BUS_WIDTH_8;
734 return (MMC_ERR_INVALID);
736 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
743 mmc_set_timing(struct mmc_softc *sc, int timing)
747 u_char switch_res[64];
750 case bus_timing_normal:
757 return (MMC_ERR_INVALID);
759 if (mmcbr_get_mode(sc->dev) == mode_sd)
760 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
763 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
764 EXT_CSD_HS_TIMING, value);
769 mmc_test_bus_width(struct mmc_softc *sc)
771 struct mmc_command cmd;
772 struct mmc_data data;
775 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
776 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
777 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, };
778 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
780 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
781 mmcbr_set_bus_width(sc->dev, bus_width_8);
782 mmcbr_update_ios(sc->dev);
784 sc->squelched++; /* Errors are expected, squelch reporting. */
785 memset(&cmd, 0, sizeof(cmd));
786 memset(&data, 0, sizeof(data));
787 cmd.opcode = MMC_BUSTEST_W;
789 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
794 data.flags = MMC_DATA_WRITE;
795 mmc_wait_for_cmd(sc, &cmd, 0);
797 memset(&cmd, 0, sizeof(cmd));
798 memset(&data, 0, sizeof(data));
799 cmd.opcode = MMC_BUSTEST_R;
801 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
806 data.flags = MMC_DATA_READ;
807 err = mmc_wait_for_cmd(sc, &cmd, 0);
810 mmcbr_set_bus_width(sc->dev, bus_width_1);
811 mmcbr_update_ios(sc->dev);
813 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
814 return (bus_width_8);
817 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
818 mmcbr_set_bus_width(sc->dev, bus_width_4);
819 mmcbr_update_ios(sc->dev);
821 sc->squelched++; /* Errors are expected, squelch reporting. */
822 memset(&cmd, 0, sizeof(cmd));
823 memset(&data, 0, sizeof(data));
824 cmd.opcode = MMC_BUSTEST_W;
826 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
831 data.flags = MMC_DATA_WRITE;
832 mmc_wait_for_cmd(sc, &cmd, 0);
834 memset(&cmd, 0, sizeof(cmd));
835 memset(&data, 0, sizeof(data));
836 cmd.opcode = MMC_BUSTEST_R;
838 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
843 data.flags = MMC_DATA_READ;
844 err = mmc_wait_for_cmd(sc, &cmd, 0);
847 mmcbr_set_bus_width(sc->dev, bus_width_1);
848 mmcbr_update_ios(sc->dev);
850 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
851 return (bus_width_4);
853 return (bus_width_1);
857 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
859 const int i = (bit_len / 32) - (start / 32) - 1;
860 const int shift = start & 31;
861 uint32_t retval = bits[i] >> shift;
862 if (size + shift > 32)
863 retval |= bits[i - 1] << (32 - shift);
864 return (retval & ((1llu << size) - 1));
868 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
872 /* There's no version info, so we take it on faith */
873 memset(cid, 0, sizeof(*cid));
874 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
875 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
876 for (i = 0; i < 5; i++)
877 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
879 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
880 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
881 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
882 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
886 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
890 /* There's no version info, so we take it on faith */
891 memset(cid, 0, sizeof(*cid));
892 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
893 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
894 for (i = 0; i < 6; i++)
895 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
897 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
898 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
899 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
900 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
904 mmc_format_card_id_string(struct mmc_ivars *ivar)
911 * Format a card ID string for use by the mmcsd driver, it's what
912 * appears between the <> in the following:
913 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
914 * 22.5MHz/4bit/128-block
916 * Also format just the card serial number, which the mmcsd driver will
917 * use as the disk->d_ident string.
919 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
920 * and our max formatted length is currently 55 bytes if every field
921 * contains the largest value.
923 * Sometimes the oid is two printable ascii chars; when it's not,
924 * format it as 0xnnnn instead.
926 c1 = (ivar->cid.oid >> 8) & 0x0ff;
927 c2 = ivar->cid.oid & 0x0ff;
928 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
929 ksnprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
931 ksnprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
932 ksnprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
933 "%08X", ivar->cid.psn);
934 ksnprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
935 "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
936 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
937 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
938 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
939 ivar->cid.mid, oidstr);
942 static const int exp[8] = {
943 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
946 static const int mant[16] = {
947 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
950 static const int cur_min[8] = {
951 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
954 static const int cur_max[8] = {
955 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
959 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
965 memset(csd, 0, sizeof(*csd));
966 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
968 m = mmc_get_bits(raw_csd, 128, 115, 4);
969 e = mmc_get_bits(raw_csd, 128, 112, 3);
970 csd->tacc = (exp[e] * mant[m] + 9) / 10;
971 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
972 m = mmc_get_bits(raw_csd, 128, 99, 4);
973 e = mmc_get_bits(raw_csd, 128, 96, 3);
974 csd->tran_speed = exp[e] * 10000 * mant[m];
975 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
976 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
977 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
978 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
979 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
980 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
981 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
982 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
983 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
984 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
985 m = mmc_get_bits(raw_csd, 128, 62, 12);
986 e = mmc_get_bits(raw_csd, 128, 47, 3);
987 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
988 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
989 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
990 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
991 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
992 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
993 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
994 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
996 m = mmc_get_bits(raw_csd, 128, 115, 4);
997 e = mmc_get_bits(raw_csd, 128, 112, 3);
998 csd->tacc = (exp[e] * mant[m] + 9) / 10;
999 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1000 m = mmc_get_bits(raw_csd, 128, 99, 4);
1001 e = mmc_get_bits(raw_csd, 128, 96, 3);
1002 csd->tran_speed = exp[e] * 10000 * mant[m];
1003 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1004 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1005 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1006 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1007 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1008 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1009 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
1011 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1012 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1013 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1014 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1015 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1016 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1017 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1019 panic("unknown SD CSD version");
1023 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
1028 memset(csd, 0, sizeof(*csd));
1029 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
1030 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1031 m = mmc_get_bits(raw_csd, 128, 115, 4);
1032 e = mmc_get_bits(raw_csd, 128, 112, 3);
1033 csd->tacc = exp[e] * mant[m] + 9 / 10;
1034 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1035 m = mmc_get_bits(raw_csd, 128, 99, 4);
1036 e = mmc_get_bits(raw_csd, 128, 96, 3);
1037 csd->tran_speed = exp[e] * 10000 * mant[m];
1038 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1039 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1040 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1041 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1042 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1043 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1044 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1045 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1046 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1047 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1048 m = mmc_get_bits(raw_csd, 128, 62, 12);
1049 e = mmc_get_bits(raw_csd, 128, 47, 3);
1050 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1051 csd->erase_blk_en = 0;
1052 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1053 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1054 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1055 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1056 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1057 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1058 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1062 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1064 unsigned int scr_struct;
1066 memset(scr, 0, sizeof(*scr));
1068 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1069 if (scr_struct != 0) {
1070 kprintf("Unrecognised SCR structure version %d\n",
1074 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1075 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1079 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1080 struct mmc_sd_status *sd_status)
1083 memset(sd_status, 0, sizeof(*sd_status));
1085 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1086 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1087 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1088 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1089 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1090 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1091 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1092 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1093 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1094 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1098 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1100 struct mmc_command cmd;
1103 memset(&cmd, 0, sizeof(cmd));
1104 cmd.opcode = MMC_ALL_SEND_CID;
1106 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1108 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1109 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1114 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1116 struct mmc_command cmd;
1119 memset(&cmd, 0, sizeof(cmd));
1120 cmd.opcode = MMC_SEND_CSD;
1121 cmd.arg = rca << 16;
1122 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1124 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1125 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1130 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1133 struct mmc_command cmd;
1134 struct mmc_data data;
1136 memset(&cmd, 0, sizeof(cmd));
1137 memset(&data, 0, sizeof(data));
1139 memset(rawscr, 0, 8);
1140 cmd.opcode = ACMD_SEND_SCR;
1141 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1147 data.flags = MMC_DATA_READ;
1149 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1150 rawscr[0] = be32toh(rawscr[0]);
1151 rawscr[1] = be32toh(rawscr[1]);
1156 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1159 struct mmc_command cmd;
1160 struct mmc_data data;
1162 memset(&cmd, 0, sizeof(cmd));
1163 memset(&data, 0, sizeof(data));
1165 memset(rawextcsd, 0, 512);
1166 cmd.opcode = MMC_SEND_EXT_CSD;
1167 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1171 data.data = rawextcsd;
1173 data.flags = MMC_DATA_READ;
1175 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1180 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1183 struct mmc_command cmd;
1184 struct mmc_data data;
1186 memset(&cmd, 0, sizeof(cmd));
1187 memset(&data, 0, sizeof(data));
1189 memset(rawsdstatus, 0, 64);
1190 cmd.opcode = ACMD_SD_STATUS;
1191 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1195 data.data = rawsdstatus;
1197 data.flags = MMC_DATA_READ;
1199 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1200 for (i = 0; i < 16; i++)
1201 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1206 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1208 struct mmc_command cmd;
1211 memset(&cmd, 0, sizeof(cmd));
1212 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1213 cmd.arg = resp << 16;
1214 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1216 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1221 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1223 struct mmc_command cmd;
1226 memset(&cmd, 0, sizeof(cmd));
1227 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1229 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1231 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1232 *resp = cmd.resp[0];
1237 mmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status)
1239 struct mmc_command cmd;
1242 memset(&cmd, 0, sizeof(cmd));
1243 cmd.opcode = MMC_SEND_STATUS;
1244 cmd.arg = rca << 16;
1245 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1247 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1248 *status = cmd.resp[0];
1253 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1255 struct mmc_command cmd;
1258 memset(&cmd, 0, sizeof(cmd));
1259 cmd.opcode = MMC_SET_BLOCKLEN;
1261 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1263 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1268 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1270 device_printf(dev, "Card at relative address 0x%04x%s:\n",
1271 ivar->rca, newcard ? " added" : "");
1272 device_printf(dev, " card: %s\n", ivar->card_id_string);
1273 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1274 (ivar->bus_width == bus_width_1 ? 1 :
1275 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1276 (ivar->timing == bus_timing_hs ?
1277 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1278 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1279 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1280 ivar->sec_count, ivar->erase_sector,
1281 ivar->read_only ? ", read-only" : "");
1285 mmc_discover_cards(struct mmc_softc *sc)
1287 struct mmc_ivars *ivar = NULL;
1289 int err, i, devcount, newcard;
1290 uint32_t raw_cid[4], resp, sec_count, status;
1293 u_char switch_res[64];
1295 if (bootverbose || mmc_debug)
1296 device_printf(sc->dev, "Probing cards\n");
1298 sc->squelched++; /* Errors are expected, squelch reporting. */
1299 err = mmc_all_send_cid(sc, raw_cid);
1301 if (err == MMC_ERR_TIMEOUT)
1303 if (err != MMC_ERR_NONE) {
1304 device_printf(sc->dev, "Error reading CID %d\n", err);
1308 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1310 for (i = 0; i < devcount; i++) {
1311 ivar = device_get_ivars(devlist[i]);
1312 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1317 kfree(devlist, M_TEMP);
1318 if (bootverbose || mmc_debug) {
1319 device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1320 newcard ? "New c" : "C",
1321 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1324 ivar = kmalloc(sizeof(struct mmc_ivars), M_DEVBUF,
1326 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1328 if (mmcbr_get_ro(sc->dev))
1329 ivar->read_only = 1;
1330 ivar->bus_width = bus_width_1;
1331 ivar->timing = bus_timing_normal;
1332 ivar->mode = mmcbr_get_mode(sc->dev);
1333 if (ivar->mode == mode_sd) {
1334 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1335 mmc_send_relative_addr(sc, &resp);
1336 ivar->rca = resp >> 16;
1338 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1339 if (bootverbose || mmc_debug)
1340 device_printf(sc->dev,
1341 "%sard detected (CSD %08x%08x%08x%08x)\n",
1342 newcard ? "New c" : "C", ivar->raw_csd[0],
1343 ivar->raw_csd[1], ivar->raw_csd[2],
1345 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1346 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1347 if (ivar->csd.csd_structure > 0)
1349 ivar->tran_speed = ivar->csd.tran_speed;
1350 ivar->erase_sector = ivar->csd.erase_sector *
1351 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1353 err = mmc_send_status(sc, ivar->rca, &status);
1354 if (err != MMC_ERR_NONE) {
1355 device_printf(sc->dev,
1356 "Error reading card status %d\n", err);
1359 if ((status & R1_CARD_IS_LOCKED) != 0) {
1360 device_printf(sc->dev,
1361 "Card is password protected, skipping.\n");
1365 /* Get card SCR. Card must be selected to fetch it. */
1366 mmc_select_card(sc, ivar->rca);
1367 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1368 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1369 /* Get card switch capabilities (command class 10). */
1370 if ((ivar->scr.sda_vsn >= 1) &&
1371 (ivar->csd.ccc & (1<<10))) {
1372 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1373 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1375 if (switch_res[13] & 2) {
1376 ivar->timing = bus_timing_hs;
1377 ivar->hs_tran_speed = SD_MAX_HS;
1382 * We deselect then reselect the card here. Some cards
1383 * become unselected and timeout with the above two
1384 * commands, although the state tables / diagrams in the
1385 * standard suggest they go back to the transfer state.
1386 * Other cards don't become deselected, and if we
1387 * atttempt to blindly re-select them, we get timeout
1388 * errors from some controllers. So we deselect then
1389 * reselect to handle all situations. The only thing we
1390 * use from the sd_status is the erase sector size, but
1391 * it is still nice to get that right.
1393 mmc_select_card(sc, 0);
1394 mmc_select_card(sc, ivar->rca);
1395 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1396 mmc_app_decode_sd_status(ivar->raw_sd_status,
1398 if (ivar->sd_status.au_size != 0) {
1399 ivar->erase_sector =
1400 16 << ivar->sd_status.au_size;
1402 /* Find max supported bus width. */
1403 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1404 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1405 ivar->bus_width = bus_width_4;
1408 * Some cards that report maximum I/O block sizes
1409 * greater than 512 require the block length to be
1410 * set to 512, even though that is supposed to be
1411 * the default. Example:
1413 * Transcend 2GB SDSC card, CID:
1414 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1416 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1417 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1418 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1420 mmc_format_card_id_string(ivar);
1422 if (bootverbose || mmc_debug)
1423 mmc_log_card(sc->dev, ivar, newcard);
1426 child = device_add_child(sc->dev, NULL, -1);
1427 device_set_ivars(child, ivar);
1429 mmc_select_card(sc, 0);
1432 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1434 mmc_set_relative_addr(sc, ivar->rca);
1436 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1437 if (bootverbose || mmc_debug)
1438 device_printf(sc->dev,
1439 "%sard detected (CSD %08x%08x%08x%08x)\n",
1440 newcard ? "New c" : "C", ivar->raw_csd[0],
1441 ivar->raw_csd[1], ivar->raw_csd[2],
1444 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1445 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1446 ivar->tran_speed = ivar->csd.tran_speed;
1447 ivar->erase_sector = ivar->csd.erase_sector *
1448 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1450 err = mmc_send_status(sc, ivar->rca, &status);
1451 if (err != MMC_ERR_NONE) {
1452 device_printf(sc->dev,
1453 "Error reading card status %d\n", err);
1456 if ((status & R1_CARD_IS_LOCKED) != 0) {
1457 device_printf(sc->dev,
1458 "Card is password protected, skipping.\n");
1462 mmc_select_card(sc, ivar->rca);
1464 /* Only MMC >= 4.x cards support EXT_CSD. */
1465 if (ivar->csd.spec_vers >= 4) {
1466 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1467 /* Handle extended capacity from EXT_CSD */
1468 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1469 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1470 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1471 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1472 if (sec_count != 0) {
1473 ivar->sec_count = sec_count;
1476 /* Get card speed in high speed mode. */
1477 ivar->timing = bus_timing_hs;
1478 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1479 & EXT_CSD_CARD_TYPE_52)
1480 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1481 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1482 & EXT_CSD_CARD_TYPE_26)
1483 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1485 ivar->hs_tran_speed = ivar->tran_speed;
1486 /* Find max supported bus width. */
1487 ivar->bus_width = mmc_test_bus_width(sc);
1488 /* Handle HC erase sector size. */
1489 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1490 ivar->erase_sector = 1024 *
1491 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1492 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1493 EXT_CSD_ERASE_GRP_DEF, 1);
1496 ivar->bus_width = bus_width_1;
1497 ivar->timing = bus_timing_normal;
1501 * Some cards that report maximum I/O block sizes greater
1502 * than 512 require the block length to be set to 512, even
1503 * though that is supposed to be the default. Example:
1505 * Transcend 2GB SDSC card, CID:
1506 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1508 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1509 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1510 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1512 mmc_format_card_id_string(ivar);
1514 if (bootverbose || mmc_debug)
1515 mmc_log_card(sc->dev, ivar, newcard);
1518 child = device_add_child(sc->dev, NULL, -1);
1519 device_set_ivars(child, ivar);
1521 mmc_select_card(sc, 0);
1526 mmc_rescan_cards(struct mmc_softc *sc)
1528 struct mmc_ivars *ivar = NULL;
1530 int err, i, devcount;
1532 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1534 for (i = 0; i < devcount; i++) {
1535 ivar = device_get_ivars(devlist[i]);
1536 if (mmc_select_card(sc, ivar->rca)) {
1537 if (bootverbose || mmc_debug)
1538 device_printf(sc->dev, "Card at relative address %d lost.\n",
1540 device_delete_child(sc->dev, devlist[i]);
1541 kfree(ivar, M_DEVBUF);
1544 kfree(devlist, M_TEMP);
1545 mmc_select_card(sc, 0);
1549 mmc_delete_cards(struct mmc_softc *sc)
1551 struct mmc_ivars *ivar;
1553 int err, i, devcount;
1555 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1557 for (i = 0; i < devcount; i++) {
1558 ivar = device_get_ivars(devlist[i]);
1559 if (bootverbose || mmc_debug)
1560 device_printf(sc->dev, "Card at relative address %d deleted.\n",
1562 device_delete_child(sc->dev, devlist[i]);
1563 kfree(ivar, M_DEVBUF);
1565 kfree(devlist, M_TEMP);
1570 mmc_go_discovery(struct mmc_softc *sc)
1577 if (mmcbr_get_power_mode(dev) != power_on) {
1579 * First, try SD modes
1581 sc->squelched++; /* Errors are expected, squelch reporting. */
1582 mmcbr_set_mode(dev, mode_sd);
1584 mmcbr_set_bus_mode(dev, pushpull);
1585 if (bootverbose || mmc_debug)
1586 device_printf(sc->dev, "Probing bus\n");
1588 err = mmc_send_if_cond(sc, 1);
1589 if ((bootverbose || mmc_debug) && err == 0)
1590 device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1591 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1592 if (bootverbose || mmc_debug)
1593 device_printf(sc->dev, "SD probe: failed\n");
1597 mmcbr_set_mode(dev, mode_mmc);
1598 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1599 if (bootverbose || mmc_debug)
1600 device_printf(sc->dev, "MMC probe: failed\n");
1601 ocr = 0; /* Failed both, powerdown. */
1602 } else if (bootverbose || mmc_debug)
1603 device_printf(sc->dev,
1604 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1605 } else if (bootverbose || mmc_debug)
1606 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1609 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1610 if (mmcbr_get_ocr(dev) != 0)
1613 mmcbr_set_bus_mode(dev, opendrain);
1614 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1615 mmcbr_update_ios(dev);
1616 /* XXX recompute vdd based on new cards? */
1619 * Make sure that we have a mutually agreeable voltage to at least
1620 * one card on the bus.
1622 if (bootverbose || mmc_debug)
1623 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1624 if (mmcbr_get_ocr(dev) == 0) {
1625 device_printf(sc->dev, "No compatible cards found on bus\n");
1626 mmc_delete_cards(sc);
1631 * Reselect the cards after we've idled them above.
1633 if (mmcbr_get_mode(dev) == mode_sd) {
1634 err = mmc_send_if_cond(sc, 1);
1635 mmc_send_app_op_cond(sc,
1636 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1638 mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1639 mmc_discover_cards(sc);
1640 mmc_rescan_cards(sc);
1642 mmcbr_set_bus_mode(dev, pushpull);
1643 mmcbr_update_ios(dev);
1644 mmc_calculate_clock(sc);
1645 bus_generic_attach(dev);
1646 /* mmc_update_children_sysctl(dev);*/
1650 mmc_calculate_clock(struct mmc_softc *sc)
1652 int max_dtr, max_hs_dtr, max_timing;
1655 struct mmc_ivars *ivar;
1657 f_max = mmcbr_get_f_max(sc->dev);
1658 max_dtr = max_hs_dtr = f_max;
1659 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1660 max_timing = bus_timing_hs;
1662 max_timing = bus_timing_normal;
1663 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1664 panic("can't get children");
1665 for (i = 0; i < nkid; i++) {
1666 ivar = device_get_ivars(kids[i]);
1667 if (ivar->timing < max_timing)
1668 max_timing = ivar->timing;
1669 if (ivar->tran_speed < max_dtr)
1670 max_dtr = ivar->tran_speed;
1671 if (ivar->hs_tran_speed < max_hs_dtr)
1672 max_hs_dtr = ivar->hs_tran_speed;
1674 for (i = 0; i < nkid; i++) {
1675 ivar = device_get_ivars(kids[i]);
1676 if (ivar->timing == bus_timing_normal)
1678 mmc_select_card(sc, ivar->rca);
1679 mmc_set_timing(sc, max_timing);
1681 mmc_select_card(sc, 0);
1682 kfree(kids, M_TEMP);
1683 if (max_timing == bus_timing_hs)
1684 max_dtr = max_hs_dtr;
1685 if (bootverbose || mmc_debug) {
1686 device_printf(sc->dev,
1687 "setting transfer rate to %d.%03dMHz%s\n",
1688 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1689 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1691 mmcbr_set_timing(sc->dev, max_timing);
1692 mmcbr_set_clock(sc->dev, max_dtr);
1693 mmcbr_update_ios(sc->dev);
1698 mmc_scan(struct mmc_softc *sc)
1700 device_t dev = sc->dev;
1702 mmc_acquire_bus(dev, dev);
1703 mmc_go_discovery(sc);
1704 mmc_release_bus(dev, dev);
1708 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1710 struct mmc_ivars *ivar = device_get_ivars(child);
1715 case MMC_IVAR_DSR_IMP:
1716 *(int *)result = ivar->csd.dsr_imp;
1718 case MMC_IVAR_MEDIA_SIZE:
1719 *(off_t *)result = ivar->sec_count;
1722 *(int *)result = ivar->rca;
1724 case MMC_IVAR_SECTOR_SIZE:
1725 *(int *)result = MMC_SECTOR_SIZE;
1727 case MMC_IVAR_TRAN_SPEED:
1728 *(int *)result = mmcbr_get_clock(bus);
1730 case MMC_IVAR_READ_ONLY:
1731 *(int *)result = ivar->read_only;
1733 case MMC_IVAR_HIGH_CAP:
1734 *(int *)result = ivar->high_cap;
1736 case MMC_IVAR_CARD_TYPE:
1737 *(int *)result = ivar->mode;
1739 case MMC_IVAR_BUS_WIDTH:
1740 *(int *)result = ivar->bus_width;
1742 case MMC_IVAR_ERASE_SECTOR:
1743 *(int *)result = ivar->erase_sector;
1745 case MMC_IVAR_MAX_DATA:
1746 *(int *)result = mmcbr_get_max_data(bus);
1748 case MMC_IVAR_CARD_ID_STRING:
1749 *(char **)result = ivar->card_id_string;
1751 case MMC_IVAR_CARD_SN_STRING:
1752 *(char **)result = ivar->card_sn_string;
1759 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1762 * None are writable ATM
1769 mmc_delayed_attach(void *xsc)
1771 struct mmc_softc *sc = xsc;
1774 config_intrhook_disestablish(&sc->config_intrhook);
1778 mmc_child_location_str(device_t dev, device_t child, char *buf,
1782 ksnprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1786 static device_method_t mmc_methods[] = {
1788 DEVMETHOD(device_probe, mmc_probe),
1789 DEVMETHOD(device_attach, mmc_attach),
1790 DEVMETHOD(device_detach, mmc_detach),
1791 DEVMETHOD(device_suspend, mmc_suspend),
1792 DEVMETHOD(device_resume, mmc_resume),
1795 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1796 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1797 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1799 /* MMC Bus interface */
1800 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1801 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1802 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1807 static driver_t mmc_driver = {
1810 sizeof(struct mmc_softc),
1812 static devclass_t mmc_devclass;
1815 DRIVER_MODULE(mmc, sdhci_pci, mmc_driver, mmc_devclass, NULL, NULL);
1816 DRIVER_MODULE(mmc, sdhci_acpi, mmc_driver, mmc_devclass, NULL, NULL);