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 */
106 #define CMD_RETRIES 3
108 SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
110 static int mmc_debug;
111 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");
113 /* bus entry points */
114 static int mmc_probe(device_t dev);
115 static int mmc_attach(device_t dev);
116 static int mmc_detach(device_t dev);
117 static int mmc_suspend(device_t dev);
118 static int mmc_resume(device_t dev);
120 #define MMC_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
121 #define MMC_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
122 #define MMC_LOCK_INIT(_sc) \
123 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
125 #define MMC_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);
126 #define MMC_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);
127 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
129 static int mmc_calculate_clock(struct mmc_softc *sc);
130 static void mmc_delayed_attach(void *);
131 static void mmc_power_down(struct mmc_softc *sc);
132 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
134 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
135 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
136 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
137 static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width);
138 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr);
139 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
140 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
141 static void mmc_scan(struct mmc_softc *sc);
142 static int mmc_delete_cards(struct mmc_softc *sc);
147 DELAY(1000 * ms); /* XXX BAD */
151 mmc_probe(device_t dev)
154 device_set_desc(dev, "MMC/SD bus");
159 mmc_attach(device_t dev)
161 struct mmc_softc *sc;
163 sc = device_get_softc(dev);
167 /* We'll probe and attach our children later, but before / mount */
168 sc->config_intrhook.ich_func = mmc_delayed_attach;
169 sc->config_intrhook.ich_arg = sc;
170 if (config_intrhook_establish(&sc->config_intrhook) != 0)
171 device_printf(dev, "config_intrhook_establish failed\n");
176 mmc_detach(device_t dev)
178 struct mmc_softc *sc = device_get_softc(dev);
181 if ((err = mmc_delete_cards(sc)) != 0)
184 MMC_LOCK_DESTROY(sc);
190 mmc_suspend(device_t dev)
192 struct mmc_softc *sc = device_get_softc(dev);
195 err = bus_generic_suspend(dev);
203 mmc_resume(device_t dev)
205 struct mmc_softc *sc = device_get_softc(dev);
208 return (bus_generic_resume(dev));
212 mmc_acquire_bus(device_t busdev, device_t dev)
214 struct mmc_softc *sc;
215 struct mmc_ivars *ivar;
219 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
222 sc = device_get_softc(busdev);
225 panic("mmc: host bridge didn't seralize us.");
231 * Keep track of the last rca that we've selected. If
232 * we're asked to do it again, don't. We never
233 * unselect unless the bus code itself wants the mmc
234 * bus, and constantly reselecting causes problems.
236 rca = mmc_get_rca(dev);
237 if (sc->last_rca != rca) {
238 mmc_select_card(sc, rca);
240 /* Prepare bus width for the new card. */
241 ivar = device_get_ivars(dev);
242 if (bootverbose || mmc_debug) {
243 device_printf(busdev,
244 "setting bus width to %d bits\n",
245 (ivar->bus_width == bus_width_4) ? 4 :
246 (ivar->bus_width == bus_width_8) ? 8 : 1);
248 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
249 mmcbr_set_bus_width(busdev, ivar->bus_width);
250 mmcbr_update_ios(busdev);
254 * If there's a card selected, stand down.
256 if (sc->last_rca != 0) {
257 mmc_select_card(sc, 0);
266 mmc_release_bus(device_t busdev, device_t dev)
268 struct mmc_softc *sc;
271 sc = device_get_softc(busdev);
275 panic("mmc: releasing unowned bus.");
276 if (sc->owner != dev)
277 panic("mmc: you don't own the bus. game over.");
279 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
289 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
292 return (ocr & MMC_OCR_VOLTAGE);
296 mmc_highest_voltage(uint32_t ocr)
300 for (i = 30; i >= 0; i--)
307 mmc_wakeup(struct mmc_request *req)
309 struct mmc_softc *sc;
311 sc = (struct mmc_softc *)req->done_data;
313 req->flags |= MMC_REQ_DONE;
319 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
322 req->done = mmc_wakeup;
325 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
326 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
327 if (req->cmd->data) {
328 printf(" data %d\n", (int)req->cmd->data->len);
332 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
334 while ((req->flags & MMC_REQ_DONE) == 0)
335 msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
337 if (mmc_debug > 2 || (mmc_debug > 1 && req->cmd->error))
338 device_printf(sc->dev, "RESULT: %d\n", req->cmd->error);
343 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
345 struct mmc_softc *sc = device_get_softc(brdev);
347 return (mmc_wait_for_req(sc, req));
351 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
353 struct mmc_request mreq;
355 memset(&mreq, 0, sizeof(mreq));
356 memset(cmd->resp, 0, sizeof(cmd->resp));
357 cmd->retries = retries;
359 mmc_wait_for_req(sc, &mreq);
364 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
365 struct mmc_command *cmd, int retries)
367 struct mmc_command appcmd;
368 int err = MMC_ERR_NONE, i;
370 for (i = 0; i <= retries; i++) {
371 appcmd.opcode = MMC_APP_CMD;
372 appcmd.arg = rca << 16;
373 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
375 mmc_wait_for_cmd(sc, &appcmd, 0);
377 if (err != MMC_ERR_NONE)
379 if (!(appcmd.resp[0] & R1_APP_CMD))
380 return MMC_ERR_FAILED;
381 mmc_wait_for_cmd(sc, cmd, 0);
383 if (err == MMC_ERR_NONE)
390 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
391 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
393 struct mmc_command cmd;
396 memset(&cmd, 0, sizeof(cmd));
401 err = mmc_wait_for_cmd(sc, &cmd, retries);
407 if (flags & MMC_RSP_136)
408 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
416 mmc_idle_cards(struct mmc_softc *sc)
419 struct mmc_command cmd;
422 mmcbr_set_chip_select(dev, cs_high);
423 mmcbr_update_ios(dev);
426 memset(&cmd, 0, sizeof(cmd));
427 cmd.opcode = MMC_GO_IDLE_STATE;
429 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
431 mmc_wait_for_cmd(sc, &cmd, 0);
434 mmcbr_set_chip_select(dev, cs_dontcare);
435 mmcbr_update_ios(dev);
440 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
442 struct mmc_command cmd;
443 int err = MMC_ERR_NONE, i;
445 memset(&cmd, 0, sizeof(cmd));
446 cmd.opcode = ACMD_SD_SEND_OP_COND;
448 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
451 for (i = 0; i < 100; i++) {
452 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
453 if (err != MMC_ERR_NONE)
455 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
456 (ocr & MMC_OCR_VOLTAGE) == 0)
458 err = MMC_ERR_TIMEOUT;
461 if (rocr && err == MMC_ERR_NONE)
467 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
469 struct mmc_command cmd;
470 int err = MMC_ERR_NONE, i;
472 memset(&cmd, 0, sizeof(cmd));
473 cmd.opcode = MMC_SEND_OP_COND;
475 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
478 for (i = 0; i < 100; i++) {
479 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
480 if (err != MMC_ERR_NONE)
482 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
483 (ocr & MMC_OCR_VOLTAGE) == 0)
485 err = MMC_ERR_TIMEOUT;
488 if (rocr && err == MMC_ERR_NONE)
494 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
496 struct mmc_command cmd;
499 memset(&cmd, 0, sizeof(cmd));
500 cmd.opcode = SD_SEND_IF_COND;
501 cmd.arg = (vhs << 8) + 0xAA;
502 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
505 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
510 mmc_power_up(struct mmc_softc *sc)
515 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
516 mmcbr_set_bus_mode(dev, opendrain);
517 mmcbr_set_chip_select(dev, cs_dontcare);
518 mmcbr_set_bus_width(dev, bus_width_1);
519 mmcbr_set_power_mode(dev, power_up);
520 mmcbr_set_clock(dev, 0);
521 mmcbr_update_ios(dev);
524 mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev));
525 mmcbr_set_timing(dev, bus_timing_normal);
526 mmcbr_set_power_mode(dev, power_on);
527 mmcbr_update_ios(dev);
532 mmc_power_down(struct mmc_softc *sc)
534 device_t dev = sc->dev;
536 mmcbr_set_bus_mode(dev, opendrain);
537 mmcbr_set_chip_select(dev, cs_dontcare);
538 mmcbr_set_bus_width(dev, bus_width_1);
539 mmcbr_set_power_mode(dev, power_off);
540 mmcbr_set_clock(dev, 0);
541 mmcbr_set_timing(dev, bus_timing_normal);
542 mmcbr_update_ios(dev);
546 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
550 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
551 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
552 flags, NULL, CMD_RETRIES));
556 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
558 struct mmc_command cmd;
561 cmd.opcode = MMC_SWITCH_FUNC;
562 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
566 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
568 err = mmc_wait_for_cmd(sc, &cmd, 0);
573 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
577 struct mmc_command cmd;
578 struct mmc_data data;
580 memset(&cmd, 0, sizeof(struct mmc_command));
581 memset(&data, 0, sizeof(struct mmc_data));
584 cmd.opcode = SD_SWITCH_FUNC;
585 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
586 cmd.arg = mode << 31; /* 0 - check, 1 - set */
587 cmd.arg |= 0x00FFFFFF;
588 cmd.arg &= ~(0xF << (grp * 4));
589 cmd.arg |= value << (grp * 4);
594 data.flags = MMC_DATA_READ;
596 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
601 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
603 struct mmc_command cmd;
607 if (mmcbr_get_mode(sc->dev) == mode_sd) {
608 memset(&cmd, 0, sizeof(struct mmc_command));
609 cmd.opcode = ACMD_SET_BUS_WIDTH;
610 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
613 cmd.arg = SD_BUS_WIDTH_1;
616 cmd.arg = SD_BUS_WIDTH_4;
619 return (MMC_ERR_INVALID);
621 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
625 value = EXT_CSD_BUS_WIDTH_1;
628 value = EXT_CSD_BUS_WIDTH_4;
631 value = EXT_CSD_BUS_WIDTH_8;
634 return (MMC_ERR_INVALID);
636 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
643 mmc_set_timing(struct mmc_softc *sc, int timing)
647 u_char switch_res[64];
650 case bus_timing_normal:
657 return (MMC_ERR_INVALID);
659 if (mmcbr_get_mode(sc->dev) == mode_sd)
660 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
663 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
664 EXT_CSD_HS_TIMING, value);
669 mmc_test_bus_width(struct mmc_softc *sc)
671 struct mmc_command cmd;
672 struct mmc_data data;
675 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
676 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
677 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, };
678 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
680 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
681 mmcbr_set_bus_width(sc->dev, bus_width_8);
682 mmcbr_update_ios(sc->dev);
684 cmd.opcode = MMC_BUSTEST_W;
686 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
691 data.flags = MMC_DATA_WRITE;
692 mmc_wait_for_cmd(sc, &cmd, 0);
694 cmd.opcode = MMC_BUSTEST_R;
696 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
701 data.flags = MMC_DATA_READ;
702 err = mmc_wait_for_cmd(sc, &cmd, 0);
704 mmcbr_set_bus_width(sc->dev, bus_width_1);
705 mmcbr_update_ios(sc->dev);
707 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
708 return (bus_width_8);
711 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
712 mmcbr_set_bus_width(sc->dev, bus_width_4);
713 mmcbr_update_ios(sc->dev);
715 cmd.opcode = MMC_BUSTEST_W;
717 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
722 data.flags = MMC_DATA_WRITE;
723 mmc_wait_for_cmd(sc, &cmd, 0);
725 cmd.opcode = MMC_BUSTEST_R;
727 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
732 data.flags = MMC_DATA_READ;
733 err = mmc_wait_for_cmd(sc, &cmd, 0);
735 mmcbr_set_bus_width(sc->dev, bus_width_1);
736 mmcbr_update_ios(sc->dev);
738 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
739 return (bus_width_4);
741 return (bus_width_1);
745 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
747 const int i = (bit_len / 32) - (start / 32) - 1;
748 const int shift = start & 31;
749 uint32_t retval = bits[i] >> shift;
750 if (size + shift > 32)
751 retval |= bits[i - 1] << (32 - shift);
752 return (retval & ((1 << size) - 1));
756 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
760 /* There's no version info, so we take it on faith */
761 memset(cid, 0, sizeof(*cid));
762 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
763 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
764 for (i = 0; i < 5; i++)
765 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
767 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
768 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
769 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
770 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
774 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
778 /* There's no version info, so we take it on faith */
779 memset(cid, 0, sizeof(*cid));
780 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
781 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
782 for (i = 0; i < 6; i++)
783 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
785 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
786 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
787 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
788 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
791 static const int exp[8] = {
792 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
794 static const int mant[16] = {
795 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
797 static const int cur_min[8] = {
798 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
800 static const int cur_max[8] = {
801 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
805 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
811 memset(csd, 0, sizeof(*csd));
812 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
814 m = mmc_get_bits(raw_csd, 128, 115, 4);
815 e = mmc_get_bits(raw_csd, 128, 112, 3);
816 csd->tacc = exp[e] * mant[m] + 9 / 10;
817 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
818 m = mmc_get_bits(raw_csd, 128, 99, 4);
819 e = mmc_get_bits(raw_csd, 128, 96, 3);
820 csd->tran_speed = exp[e] * 10000 * mant[m];
821 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
822 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
823 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
824 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
825 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
826 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
827 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
828 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
829 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
830 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
831 m = mmc_get_bits(raw_csd, 128, 62, 12);
832 e = mmc_get_bits(raw_csd, 128, 47, 3);
833 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
834 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
835 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
836 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
837 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
838 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
839 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
840 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
842 m = mmc_get_bits(raw_csd, 128, 115, 4);
843 e = mmc_get_bits(raw_csd, 128, 112, 3);
844 csd->tacc = exp[e] * mant[m] + 9 / 10;
845 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
846 m = mmc_get_bits(raw_csd, 128, 99, 4);
847 e = mmc_get_bits(raw_csd, 128, 96, 3);
848 csd->tran_speed = exp[e] * 10000 * mant[m];
849 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
850 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
851 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
852 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
853 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
854 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
855 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
857 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
858 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
859 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
860 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
861 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
862 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
863 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
865 panic("unknown SD CSD version");
869 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
874 memset(csd, 0, sizeof(*csd));
875 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
876 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
877 m = mmc_get_bits(raw_csd, 128, 115, 4);
878 e = mmc_get_bits(raw_csd, 128, 112, 3);
879 csd->tacc = exp[e] * mant[m] + 9 / 10;
880 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
881 m = mmc_get_bits(raw_csd, 128, 99, 4);
882 e = mmc_get_bits(raw_csd, 128, 96, 3);
883 csd->tran_speed = exp[e] * 10000 * mant[m];
884 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
885 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
886 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
887 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
888 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
889 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
890 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
891 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
892 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
893 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
894 m = mmc_get_bits(raw_csd, 128, 62, 12);
895 e = mmc_get_bits(raw_csd, 128, 47, 3);
896 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
897 csd->erase_blk_en = 0;
898 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
899 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
900 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
901 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
902 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
903 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
904 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
908 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
910 unsigned int scr_struct;
912 memset(scr, 0, sizeof(*scr));
914 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
915 if (scr_struct != 0) {
916 printf("Unrecognised SCR structure version %d\n",
920 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
921 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
925 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
926 struct mmc_sd_status *sd_status)
929 memset(sd_status, 0, sizeof(*sd_status));
931 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
932 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
933 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
934 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
935 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
936 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
937 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
938 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
939 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
940 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
944 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
946 struct mmc_command cmd;
949 cmd.opcode = MMC_ALL_SEND_CID;
951 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
953 err = mmc_wait_for_cmd(sc, &cmd, 0);
954 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
959 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcid)
961 struct mmc_command cmd;
964 cmd.opcode = MMC_SEND_CSD;
966 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
968 err = mmc_wait_for_cmd(sc, &cmd, 0);
969 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
974 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
977 struct mmc_command cmd;
978 struct mmc_data data;
980 memset(&cmd, 0, sizeof(struct mmc_command));
981 memset(&data, 0, sizeof(struct mmc_data));
983 memset(rawscr, 0, 8);
984 cmd.opcode = ACMD_SEND_SCR;
985 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
991 data.flags = MMC_DATA_READ;
993 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
994 rawscr[0] = be32toh(rawscr[0]);
995 rawscr[1] = be32toh(rawscr[1]);
1000 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1003 struct mmc_command cmd;
1004 struct mmc_data data;
1006 memset(&cmd, 0, sizeof(struct mmc_command));
1007 memset(&data, 0, sizeof(struct mmc_data));
1009 memset(rawextcsd, 0, 512);
1010 cmd.opcode = MMC_SEND_EXT_CSD;
1011 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1015 data.data = rawextcsd;
1017 data.flags = MMC_DATA_READ;
1019 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1024 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1027 struct mmc_command cmd;
1028 struct mmc_data data;
1030 memset(&cmd, 0, sizeof(struct mmc_command));
1031 memset(&data, 0, sizeof(struct mmc_data));
1033 memset(rawsdstatus, 0, 64);
1034 cmd.opcode = ACMD_SD_STATUS;
1035 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1039 data.data = rawsdstatus;
1041 data.flags = MMC_DATA_READ;
1043 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1044 for (i = 0; i < 16; i++)
1045 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1050 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1052 struct mmc_command cmd;
1055 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1056 cmd.arg = resp << 16;
1057 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1059 err = mmc_wait_for_cmd(sc, &cmd, 0);
1064 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1066 struct mmc_command cmd;
1069 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1071 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1073 err = mmc_wait_for_cmd(sc, &cmd, 0);
1074 *resp = cmd.resp[0];
1079 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1081 device_printf(dev, "Card at relative address %d%s:\n",
1082 ivar->rca, newcard ? " added" : "");
1083 device_printf(dev, " card: %s%s (0x%x/0x%x/\"%s\" rev %d.%d "
1084 "m/d %02d.%04d s/n %08x)\n",
1085 ivar->mode == mode_sd ? "SD" : "MMC",
1086 ivar->high_cap ? " High Capacity" : "",
1087 ivar->cid.mid, ivar->cid.oid,
1088 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
1089 ivar->cid.mdt_month, ivar->cid.mdt_year, ivar->cid.psn);
1090 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1091 (ivar->bus_width == bus_width_1 ? 1 :
1092 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1093 (ivar->timing == bus_timing_hs ?
1094 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1095 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1096 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1097 ivar->sec_count, ivar->erase_sector,
1098 ivar->read_only ? ", read-only" : "");
1102 mmc_discover_cards(struct mmc_softc *sc)
1104 struct mmc_ivars *ivar = NULL;
1106 int err, i, devcount, newcard;
1107 uint32_t raw_cid[4];
1108 uint32_t resp, sec_count;
1111 u_char switch_res[64];
1113 if (bootverbose || mmc_debug)
1114 device_printf(sc->dev, "Probing cards\n");
1116 err = mmc_all_send_cid(sc, raw_cid);
1117 if (err == MMC_ERR_TIMEOUT)
1119 if (err != MMC_ERR_NONE) {
1120 device_printf(sc->dev, "Error reading CID %d\n", err);
1124 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1126 for (i = 0; i < devcount; i++) {
1127 ivar = device_get_ivars(devlist[i]);
1128 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1133 free(devlist, M_TEMP);
1134 if (bootverbose || mmc_debug) {
1135 device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1136 newcard ? "New c" : "C",
1137 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1140 ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1144 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1146 if (mmcbr_get_ro(sc->dev))
1147 ivar->read_only = 1;
1148 ivar->bus_width = bus_width_1;
1149 ivar->timing = bus_timing_normal;
1150 ivar->mode = mmcbr_get_mode(sc->dev);
1151 if (ivar->mode == mode_sd) {
1152 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1153 mmc_send_relative_addr(sc, &resp);
1154 ivar->rca = resp >> 16;
1156 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1157 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1158 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1159 if (ivar->csd.csd_structure > 0)
1161 ivar->tran_speed = ivar->csd.tran_speed;
1162 ivar->erase_sector = ivar->csd.erase_sector *
1163 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1164 /* Get card SCR. Card must be selected to fetch it. */
1165 mmc_select_card(sc, ivar->rca);
1166 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1167 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1168 /* Get card switch capabilities (command class 10). */
1169 if ((ivar->scr.sda_vsn >= 1) &&
1170 (ivar->csd.ccc & (1<<10))) {
1171 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1172 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1174 if (switch_res[13] & 2) {
1175 ivar->timing = bus_timing_hs;
1176 ivar->hs_tran_speed = SD_MAX_HS;
1179 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1180 mmc_app_decode_sd_status(ivar->raw_sd_status,
1182 if (ivar->sd_status.au_size != 0) {
1183 ivar->erase_sector =
1184 16 << ivar->sd_status.au_size;
1186 mmc_select_card(sc, 0);
1187 /* Find max supported bus width. */
1188 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1189 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1190 ivar->bus_width = bus_width_4;
1191 if (bootverbose || mmc_debug)
1192 mmc_log_card(sc->dev, ivar, newcard);
1195 child = device_add_child(sc->dev, NULL, -1);
1196 device_set_ivars(child, ivar);
1200 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1202 mmc_set_relative_addr(sc, ivar->rca);
1204 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1205 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1206 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1207 ivar->tran_speed = ivar->csd.tran_speed;
1208 ivar->erase_sector = ivar->csd.erase_sector *
1209 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1210 /* Only MMC >= 4.x cards support EXT_CSD. */
1211 if (ivar->csd.spec_vers >= 4) {
1212 /* Card must be selected to fetch EXT_CSD. */
1213 mmc_select_card(sc, ivar->rca);
1214 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1215 /* Handle extended capacity from EXT_CSD */
1216 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1217 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1218 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1219 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1220 if (sec_count != 0) {
1221 ivar->sec_count = sec_count;
1224 /* Get card speed in high speed mode. */
1225 ivar->timing = bus_timing_hs;
1226 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1227 & EXT_CSD_CARD_TYPE_52)
1228 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1229 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1230 & EXT_CSD_CARD_TYPE_26)
1231 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1233 ivar->hs_tran_speed = ivar->tran_speed;
1234 /* Find max supported bus width. */
1235 ivar->bus_width = mmc_test_bus_width(sc);
1236 mmc_select_card(sc, 0);
1237 /* Handle HC erase sector size. */
1238 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1239 ivar->erase_sector = 1024 *
1240 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1241 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1242 EXT_CSD_ERASE_GRP_DEF, 1);
1245 ivar->bus_width = bus_width_1;
1246 ivar->timing = bus_timing_normal;
1248 if (bootverbose || mmc_debug)
1249 mmc_log_card(sc->dev, ivar, newcard);
1252 child = device_add_child(sc->dev, NULL, -1);
1253 device_set_ivars(child, ivar);
1259 mmc_rescan_cards(struct mmc_softc *sc)
1261 struct mmc_ivars *ivar = NULL;
1263 int err, i, devcount;
1265 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1267 for (i = 0; i < devcount; i++) {
1268 ivar = device_get_ivars(devlist[i]);
1269 if (mmc_select_card(sc, ivar->rca)) {
1270 if (bootverbose || mmc_debug)
1271 device_printf(sc->dev, "Card at relative address %d lost.\n",
1273 device_delete_child(sc->dev, devlist[i]);
1274 free(ivar, M_DEVBUF);
1277 free(devlist, M_TEMP);
1278 mmc_select_card(sc, 0);
1282 mmc_delete_cards(struct mmc_softc *sc)
1284 struct mmc_ivars *ivar;
1286 int err, i, devcount;
1288 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1290 for (i = 0; i < devcount; i++) {
1291 ivar = device_get_ivars(devlist[i]);
1292 if (bootverbose || mmc_debug)
1293 device_printf(sc->dev, "Card at relative address %d deleted.\n",
1295 device_delete_child(sc->dev, devlist[i]);
1296 free(ivar, M_DEVBUF);
1298 free(devlist, M_TEMP);
1303 mmc_go_discovery(struct mmc_softc *sc)
1310 if (mmcbr_get_power_mode(dev) != power_on) {
1312 * First, try SD modes
1314 mmcbr_set_mode(dev, mode_sd);
1316 mmcbr_set_bus_mode(dev, pushpull);
1317 if (bootverbose || mmc_debug)
1318 device_printf(sc->dev, "Probing bus\n");
1320 err = mmc_send_if_cond(sc, 1);
1321 if ((bootverbose || mmc_debug) && err == 0)
1322 device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1323 if (mmc_send_app_op_cond(sc, err ? 0 : MMC_OCR_CCS, &ocr) !=
1325 if (bootverbose || mmc_debug)
1326 device_printf(sc->dev, "SD probe: failed\n");
1330 mmcbr_set_mode(dev, mode_mmc);
1331 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1332 if (bootverbose || mmc_debug)
1333 device_printf(sc->dev, "MMC probe: failed\n");
1334 ocr = 0; /* Failed both, powerdown. */
1335 } else if (bootverbose || mmc_debug)
1336 device_printf(sc->dev,
1337 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1338 } else if (bootverbose || mmc_debug)
1339 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1341 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1342 if (mmcbr_get_ocr(dev) != 0)
1345 mmcbr_set_bus_mode(dev, opendrain);
1346 mmcbr_set_clock(dev, mmcbr_get_f_min(dev));
1347 mmcbr_update_ios(dev);
1348 /* XXX recompute vdd based on new cards? */
1351 * Make sure that we have a mutually agreeable voltage to at least
1352 * one card on the bus.
1354 if (bootverbose || mmc_debug)
1355 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1356 if (mmcbr_get_ocr(dev) == 0) {
1357 mmc_delete_cards(sc);
1362 * Reselect the cards after we've idled them above.
1364 if (mmcbr_get_mode(dev) == mode_sd) {
1365 err = mmc_send_if_cond(sc, 1);
1366 mmc_send_app_op_cond(sc,
1367 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1369 mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
1370 mmc_discover_cards(sc);
1371 mmc_rescan_cards(sc);
1373 mmcbr_set_bus_mode(dev, pushpull);
1374 mmcbr_update_ios(dev);
1375 mmc_calculate_clock(sc);
1376 bus_generic_attach(dev);
1377 /* mmc_update_children_sysctl(dev);*/
1381 mmc_calculate_clock(struct mmc_softc *sc)
1383 int max_dtr, max_hs_dtr, max_timing;
1384 int nkid, i, f_min, f_max;
1386 struct mmc_ivars *ivar;
1388 f_min = mmcbr_get_f_min(sc->dev);
1389 f_max = mmcbr_get_f_max(sc->dev);
1390 max_dtr = max_hs_dtr = f_max;
1391 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1392 max_timing = bus_timing_hs;
1394 max_timing = bus_timing_normal;
1395 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1396 panic("can't get children");
1397 for (i = 0; i < nkid; i++) {
1398 ivar = device_get_ivars(kids[i]);
1399 if (ivar->timing < max_timing)
1400 max_timing = ivar->timing;
1401 if (ivar->tran_speed < max_dtr)
1402 max_dtr = ivar->tran_speed;
1403 if (ivar->hs_tran_speed < max_hs_dtr)
1404 max_hs_dtr = ivar->hs_tran_speed;
1406 for (i = 0; i < nkid; i++) {
1407 ivar = device_get_ivars(kids[i]);
1408 if (ivar->timing == bus_timing_normal)
1410 mmc_select_card(sc, ivar->rca);
1411 mmc_set_timing(sc, max_timing);
1413 mmc_select_card(sc, 0);
1415 if (max_timing == bus_timing_hs)
1416 max_dtr = max_hs_dtr;
1417 if (bootverbose || mmc_debug) {
1418 device_printf(sc->dev,
1419 "setting transfer rate to %d.%03dMHz%s\n",
1420 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1421 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1423 mmcbr_set_timing(sc->dev, max_timing);
1424 mmcbr_set_clock(sc->dev, max_dtr);
1425 mmcbr_update_ios(sc->dev);
1430 mmc_scan(struct mmc_softc *sc)
1432 device_t dev = sc->dev;
1434 mmc_acquire_bus(dev, dev);
1435 mmc_go_discovery(sc);
1436 mmc_release_bus(dev, dev);
1440 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1442 struct mmc_ivars *ivar = device_get_ivars(child);
1447 case MMC_IVAR_DSR_IMP:
1448 *(int *)result = ivar->csd.dsr_imp;
1450 case MMC_IVAR_MEDIA_SIZE:
1451 *(off_t *)result = ivar->sec_count;
1454 *(int *)result = ivar->rca;
1456 case MMC_IVAR_SECTOR_SIZE:
1457 *(int *)result = MMC_SECTOR_SIZE;
1459 case MMC_IVAR_TRAN_SPEED:
1460 *(int *)result = mmcbr_get_clock(bus);
1462 case MMC_IVAR_READ_ONLY:
1463 *(int *)result = ivar->read_only;
1465 case MMC_IVAR_HIGH_CAP:
1466 *(int *)result = ivar->high_cap;
1468 case MMC_IVAR_CARD_TYPE:
1469 *(int *)result = ivar->mode;
1471 case MMC_IVAR_BUS_WIDTH:
1472 *(int *)result = ivar->bus_width;
1474 case MMC_IVAR_ERASE_SECTOR:
1475 *(int *)result = ivar->erase_sector;
1477 case MMC_IVAR_MAX_DATA:
1478 *(int *)result = mmcbr_get_max_data(bus);
1485 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1488 * None are writable ATM
1495 mmc_delayed_attach(void *xsc)
1497 struct mmc_softc *sc = xsc;
1502 config_intrhook_disestablish(&sc->config_intrhook);
1505 static device_method_t mmc_methods[] = {
1507 DEVMETHOD(device_probe, mmc_probe),
1508 DEVMETHOD(device_attach, mmc_attach),
1509 DEVMETHOD(device_detach, mmc_detach),
1510 DEVMETHOD(device_suspend, mmc_suspend),
1511 DEVMETHOD(device_resume, mmc_resume),
1514 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1515 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1517 /* MMC Bus interface */
1518 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1519 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1520 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1525 static driver_t mmc_driver = {
1528 sizeof(struct mmc_softc),
1530 static devclass_t mmc_devclass;
1533 DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL);
1534 DRIVER_MODULE(mmc, sdhci, mmc_driver, mmc_devclass, NULL, NULL);