kernel - Fix mmcsd read/write issues
[dragonfly.git] / sys / bus / mmc / mmc.c
blob8925adb9de25069a6920eacc66c0ca873b770f0f
1 /*-
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
7 * are met:
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.
37 * Disclaimers:
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>
59 #include <sys/lock.h>
60 #include <sys/module.h>
61 #include <sys/spinlock.h>
62 #include <sys/bus.h>
63 #include <sys/endian.h>
64 #include <sys/sysctl.h>
65 #include <sys/time.h>
67 #include <bus/mmc/mmcreg.h>
68 #include <bus/mmc/mmcbrvar.h>
69 #include <bus/mmc/mmcvar.h>
70 #include "mmcbr_if.h"
71 #include "mmcbus_if.h"
73 struct mmc_softc {
74 device_t dev;
75 struct lock sc_lock;
76 struct intr_config_hook config_intrhook;
77 device_t owner;
78 uint32_t last_rca;
79 int squelched; /* suppress reporting of (expected) errors */
80 int log_count;
81 int retries; /* retries @ 10ms */
82 struct timeval log_time;
85 #define LOG_PPS 5 /* Log no more than 5 errors per second. */
88 * Per-card data
90 struct mmc_ivars {
91 uint32_t raw_cid[4]; /* Raw bits of the CID */
92 uint32_t raw_csd[4]; /* Raw bits of the CSD */
93 uint32_t raw_scr[2]; /* Raw bits of the SCR */
94 uint8_t raw_ext_csd[512]; /* Raw bits of the EXT_CSD */
95 uint32_t raw_sd_status[16]; /* Raw bits of the SD_STATUS */
96 uint16_t rca;
97 enum mmc_card_mode mode;
98 struct mmc_cid cid; /* cid decoded */
99 struct mmc_csd csd; /* csd decoded */
100 struct mmc_scr scr; /* scr decoded */
101 struct mmc_sd_status sd_status; /* SD_STATUS decoded */
102 u_char read_only; /* True when the device is read-only */
103 u_char bus_width; /* Bus width to use */
104 u_char timing; /* Bus timing support */
105 u_char high_cap; /* High Capacity card (block addressed) */
106 uint32_t sec_count; /* Card capacity in 512byte blocks */
107 uint32_t tran_speed; /* Max speed in normal mode */
108 uint32_t hs_tran_speed; /* Max speed in high speed mode */
109 uint32_t erase_sector; /* Card native erase sector size */
110 char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
111 char card_sn_string[16];/* Formatted serial # for disk->d_ident */
114 #define CMD_RETRIES 3
116 #define CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
118 SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
120 static int mmc_debug;
121 SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");
123 /* bus entry points */
124 static int mmc_acquire_bus(device_t busdev, device_t dev);
125 static int mmc_attach(device_t dev);
126 static int mmc_child_location_str(device_t dev, device_t child, char *buf,
127 size_t buflen);
128 static int mmc_detach(device_t dev);
129 static int mmc_probe(device_t dev);
130 static int mmc_read_ivar(device_t bus, device_t child, int which,
131 uintptr_t *result);
132 static int mmc_release_bus(device_t busdev, device_t dev);
133 static int mmc_resume(device_t dev);
134 static int mmc_suspend(device_t dev);
135 static int mmc_wait_for_request(device_t brdev, device_t reqdev,
136 struct mmc_request *req);
137 static int mmc_write_ivar(device_t bus, device_t child, int which,
138 uintptr_t value);
140 #define MMC_LOCK(_sc) lockmgr(&(_sc)->sc_lock, LK_EXCLUSIVE)
141 #define MMC_UNLOCK(_sc) lockmgr(&(_sc)->sc_lock, LK_RELEASE)
142 #define MMC_LOCK_INIT(_sc) lockinit(&(_sc)->sc_lock, "mmc", 0, LK_CANRECURSE)
143 #define MMC_LOCK_DESTROY(_sc) lockuninit(&(_sc)->sc_lock);
144 #define MMC_ASSERT_LOCKED(_sc) KKASSERT(lockstatus(&(_sc)->sc_lock, curthread) != 0);
145 #define MMC_ASSERT_UNLOCKED(_sc) KKASSERT(lockstatus(&(_sc)->sc_lock, curthread) == 0);
147 static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
148 static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
149 static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
150 struct mmc_sd_status *sd_status);
151 static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
152 uint32_t *rawsdstatus);
153 static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
154 uint32_t *rawscr);
155 static int mmc_calculate_clock(struct mmc_softc *sc);
156 static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid);
157 static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
158 static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
159 static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
160 static void mmc_delayed_attach(void *xsc);
161 static int mmc_delete_cards(struct mmc_softc *sc);
162 static void mmc_discover_cards(struct mmc_softc *sc);
163 static void mmc_format_card_id_string(struct mmc_ivars *ivar);
164 static void mmc_go_discovery(struct mmc_softc *sc);
165 static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
166 int size);
167 static int mmc_highest_voltage(uint32_t ocr);
168 static void mmc_idle_cards(struct mmc_softc *sc);
169 static void mmc_ms_delay(int ms);
170 static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
171 static void mmc_power_down(struct mmc_softc *sc);
172 static void mmc_power_up(struct mmc_softc *sc);
173 static void mmc_rescan_cards(struct mmc_softc *sc);
174 static void mmc_scan(struct mmc_softc *sc);
175 static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
176 uint8_t value, uint8_t *res);
177 static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
178 static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
179 static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
180 uint32_t *rocr);
181 static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
182 static int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
183 static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
184 static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
185 uint32_t *rocr);
186 static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
187 static int mmc_send_status(struct mmc_softc *sc, uint16_t rca,
188 uint32_t *status);
189 static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
190 static int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca,
191 int width);
192 static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
193 static int mmc_set_timing(struct mmc_softc *sc, int timing);
194 static int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index,
195 uint8_t value);
196 static int mmc_test_bus_width(struct mmc_softc *sc);
197 static int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
198 struct mmc_command *cmd, int retries);
199 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
200 int retries);
201 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
202 uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
203 static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
204 static void mmc_wakeup(struct mmc_request *req);
206 static void
207 mmc_ms_delay(int ms)
209 int timo;
210 int dummy;
212 timo = ms * hz / 1000;
213 if (timo > 0)
214 tsleep(&dummy, 0, "mmcslp", timo);
215 else
216 DELAY(1000 * ms);
219 static int
220 mmc_probe(device_t dev)
223 device_set_desc(dev, "MMC/SD bus");
224 return (0);
227 static int
228 mmc_attach(device_t dev)
230 struct mmc_softc *sc;
232 sc = device_get_softc(dev);
233 sc->dev = dev;
234 sc->retries = 1000; /* 10 seconds worth */
235 MMC_LOCK_INIT(sc);
237 /* We'll probe and attach our children later, but before / mount */
238 sc->config_intrhook.ich_func = mmc_delayed_attach;
239 sc->config_intrhook.ich_arg = sc;
240 sc->config_intrhook.ich_desc = "mmc";
241 if (config_intrhook_establish(&sc->config_intrhook) != 0)
242 device_printf(dev, "config_intrhook_establish failed\n");
243 return (0);
246 static int
247 mmc_detach(device_t dev)
249 struct mmc_softc *sc = device_get_softc(dev);
250 int err;
252 if ((err = mmc_delete_cards(sc)) != 0)
253 return (err);
254 mmc_power_down(sc);
255 MMC_LOCK_DESTROY(sc);
257 return (0);
260 static int
261 mmc_suspend(device_t dev)
263 struct mmc_softc *sc = device_get_softc(dev);
264 int err;
266 err = bus_generic_suspend(dev);
267 if (err)
268 return (err);
269 mmc_power_down(sc);
270 return (0);
273 static int
274 mmc_resume(device_t dev)
276 struct mmc_softc *sc = device_get_softc(dev);
278 mmc_scan(sc);
279 return (bus_generic_resume(dev));
282 static int
283 mmc_acquire_bus(device_t busdev, device_t dev)
285 struct mmc_softc *sc;
286 struct mmc_ivars *ivar;
287 int err;
288 int rca;
290 err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
291 if (err)
292 return (err);
293 sc = device_get_softc(busdev);
294 MMC_LOCK(sc);
295 if (sc->owner)
296 panic("mmc: host bridge didn't serialize us.");
297 sc->owner = dev;
298 MMC_UNLOCK(sc);
300 if (busdev != dev) {
302 * Keep track of the last rca that we've selected. If
303 * we're asked to do it again, don't. We never
304 * unselect unless the bus code itself wants the mmc
305 * bus, and constantly reselecting causes problems.
307 rca = mmc_get_rca(dev);
308 if (sc->last_rca != rca) {
309 mmc_select_card(sc, rca);
310 sc->last_rca = rca;
311 /* Prepare bus width for the new card. */
312 ivar = device_get_ivars(dev);
313 if (bootverbose || mmc_debug) {
314 device_printf(busdev,
315 "setting bus width to %d bits\n",
316 (ivar->bus_width == bus_width_4) ? 4 :
317 (ivar->bus_width == bus_width_8) ? 8 : 1);
319 mmc_set_card_bus_width(sc, rca, ivar->bus_width);
320 mmcbr_set_bus_width(busdev, ivar->bus_width);
321 mmcbr_update_ios(busdev);
323 } else {
325 * If there's a card selected, stand down.
327 if (sc->last_rca != 0) {
328 mmc_select_card(sc, 0);
329 sc->last_rca = 0;
333 return (0);
336 static int
337 mmc_release_bus(device_t busdev, device_t dev)
339 struct mmc_softc *sc;
340 int err;
342 sc = device_get_softc(busdev);
344 MMC_LOCK(sc);
345 if (!sc->owner)
346 panic("mmc: releasing unowned bus.");
347 if (sc->owner != dev)
348 panic("mmc: you don't own the bus. game over.");
349 MMC_UNLOCK(sc);
350 err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
351 if (err)
352 return (err);
353 MMC_LOCK(sc);
354 sc->owner = NULL;
355 MMC_UNLOCK(sc);
356 return (0);
359 static uint32_t
360 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
363 return (ocr & MMC_OCR_VOLTAGE);
366 static int
367 mmc_highest_voltage(uint32_t ocr)
369 int i;
371 for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
372 i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
373 if (ocr & (1 << i))
374 return (i);
375 return (-1);
378 static void
379 mmc_wakeup(struct mmc_request *req)
381 struct mmc_softc *sc;
383 sc = (struct mmc_softc *)req->done_data;
384 MMC_LOCK(sc);
385 req->flags |= MMC_REQ_DONE;
386 MMC_UNLOCK(sc);
387 wakeup(req);
390 static int
391 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
394 req->done = mmc_wakeup;
395 req->done_data = sc;
396 if (mmc_debug > 1) {
397 device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
398 req->cmd->opcode, req->cmd->arg, req->cmd->flags);
399 if (req->cmd->data) {
400 kprintf(" data %d\n", (int)req->cmd->data->len);
401 } else
402 kprintf("\n");
404 MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
405 MMC_LOCK(sc);
406 while ((req->flags & MMC_REQ_DONE) == 0)
407 lksleep(req, &sc->sc_lock, 0, "mmcreq", 0);
408 MMC_UNLOCK(sc);
409 if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
410 device_printf(sc->dev, "CMD%d RESULT: %d\n",
411 req->cmd->opcode, req->cmd->error);
412 return (0);
415 static int
416 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
418 struct mmc_softc *sc = device_get_softc(brdev);
420 return (mmc_wait_for_req(sc, req));
423 static int
424 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
426 struct mmc_request mreq;
427 int err;
429 do {
430 memset(&mreq, 0, sizeof(mreq));
431 memset(cmd->resp, 0, sizeof(cmd->resp));
432 cmd->retries = 0; /* Retries done here, not in hardware. */
433 cmd->mrq = &mreq;
434 mreq.cmd = cmd;
435 if (mmc_wait_for_req(sc, &mreq) != 0)
436 err = MMC_ERR_FAILED;
437 else
438 err = cmd->error;
439 } while (err != MMC_ERR_NONE && retries-- > 0);
441 if (err != MMC_ERR_NONE && sc->squelched == 0) {
442 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
443 device_printf(sc->dev, "CMD%d failed, RESULT: %d\n",
444 cmd->opcode, err);
448 return (err);
451 static int
452 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
453 struct mmc_command *cmd, int retries)
455 struct mmc_command appcmd;
456 int err;
458 /* Squelch error reporting at lower levels, we report below. */
459 sc->squelched++;
460 do {
461 memset(&appcmd, 0, sizeof(appcmd));
462 appcmd.opcode = MMC_APP_CMD;
463 appcmd.arg = rca << 16;
464 appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
465 appcmd.data = NULL;
466 if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0)
467 err = MMC_ERR_FAILED;
468 else
469 err = appcmd.error;
470 if (err == MMC_ERR_NONE) {
471 if (!(appcmd.resp[0] & R1_APP_CMD))
472 err = MMC_ERR_FAILED;
473 else if (mmc_wait_for_cmd(sc, cmd, 0) != 0)
474 err = MMC_ERR_FAILED;
475 else
476 err = cmd->error;
478 } while (err != MMC_ERR_NONE && retries-- > 0);
479 sc->squelched--;
481 if (err != MMC_ERR_NONE && sc->squelched == 0) {
482 if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
483 device_printf(sc->dev, "ACMD%d failed, RESULT: %d\n",
484 cmd->opcode, err);
488 return (err);
491 static int
492 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
493 uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
495 struct mmc_command cmd;
496 int err;
498 memset(&cmd, 0, sizeof(cmd));
499 cmd.opcode = opcode;
500 cmd.arg = arg;
501 cmd.flags = flags;
502 cmd.data = NULL;
503 err = mmc_wait_for_cmd(sc, &cmd, retries);
504 if (err)
505 return (err);
506 if (resp) {
507 if (flags & MMC_RSP_136)
508 memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
509 else
510 *resp = cmd.resp[0];
512 return (0);
515 static void
516 mmc_idle_cards(struct mmc_softc *sc)
518 device_t dev;
519 struct mmc_command cmd;
521 dev = sc->dev;
522 mmcbr_set_chip_select(dev, cs_high);
523 mmcbr_update_ios(dev);
524 mmc_ms_delay(1);
526 memset(&cmd, 0, sizeof(cmd));
527 cmd.opcode = MMC_GO_IDLE_STATE;
528 cmd.arg = 0;
529 cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
530 cmd.data = NULL;
531 mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
532 mmc_ms_delay(1);
534 mmcbr_set_chip_select(dev, cs_dontcare);
535 mmcbr_update_ios(dev);
536 mmc_ms_delay(1);
539 static int
540 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
542 struct mmc_command cmd;
543 int err = MMC_ERR_NONE, i;
545 memset(&cmd, 0, sizeof(cmd));
546 cmd.opcode = ACMD_SD_SEND_OP_COND;
547 cmd.arg = ocr;
548 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
549 cmd.data = NULL;
551 for (i = 0; i < sc->retries; i++) {
552 err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
553 if (err != MMC_ERR_NONE)
554 break;
555 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
556 (ocr & MMC_OCR_VOLTAGE) == 0)
557 break;
558 err = MMC_ERR_TIMEOUT;
559 mmc_ms_delay(10);
562 if (rocr && err == MMC_ERR_NONE)
563 *rocr = cmd.resp[0];
565 if (err == MMC_ERR_TIMEOUT)
566 sc->retries = 100; /* 1 second */
567 else
568 sc->retries = 1000; /* 10 seconds */
570 return (err);
573 static int
574 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
576 struct mmc_command cmd;
577 int err = MMC_ERR_NONE, i;
579 memset(&cmd, 0, sizeof(cmd));
580 cmd.opcode = MMC_SEND_OP_COND;
581 cmd.arg = ocr;
582 cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
583 cmd.data = NULL;
585 for (i = 0; i < sc->retries; i++) {
586 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
587 if (err != MMC_ERR_NONE)
588 break;
589 if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
590 (ocr & MMC_OCR_VOLTAGE) == 0)
591 break;
592 err = MMC_ERR_TIMEOUT;
593 mmc_ms_delay(10);
596 if (rocr && err == MMC_ERR_NONE)
597 *rocr = cmd.resp[0];
599 if (err == MMC_ERR_TIMEOUT)
600 sc->retries = 100; /* 1 second */
601 else
602 sc->retries = 1000; /* 10 seconds */
604 return (err);
607 static int
608 mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
610 struct mmc_command cmd;
611 int err;
613 memset(&cmd, 0, sizeof(cmd));
614 cmd.opcode = SD_SEND_IF_COND;
615 cmd.arg = (vhs << 8) + 0xAA;
616 cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
617 cmd.data = NULL;
619 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
620 return (err);
623 static void
624 mmc_power_up(struct mmc_softc *sc)
626 device_t dev;
628 dev = sc->dev;
629 mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
630 mmcbr_set_bus_mode(dev, opendrain);
631 mmcbr_set_chip_select(dev, cs_dontcare);
632 mmcbr_set_bus_width(dev, bus_width_1);
633 mmcbr_set_power_mode(dev, power_up);
634 mmcbr_set_clock(dev, 0);
635 mmcbr_update_ios(dev);
636 mmc_ms_delay(1);
638 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
639 mmcbr_set_timing(dev, bus_timing_normal);
640 mmcbr_set_power_mode(dev, power_on);
641 mmcbr_update_ios(dev);
642 mmc_ms_delay(2);
645 static void
646 mmc_power_down(struct mmc_softc *sc)
648 device_t dev = sc->dev;
650 mmcbr_set_bus_mode(dev, opendrain);
651 mmcbr_set_chip_select(dev, cs_dontcare);
652 mmcbr_set_bus_width(dev, bus_width_1);
653 mmcbr_set_power_mode(dev, power_off);
654 mmcbr_set_clock(dev, 0);
655 mmcbr_set_timing(dev, bus_timing_normal);
656 mmcbr_update_ios(dev);
659 static int
660 mmc_select_card(struct mmc_softc *sc, uint16_t rca)
662 int flags;
664 flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
665 return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
666 flags, NULL, CMD_RETRIES));
669 static int
670 mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
672 struct mmc_command cmd;
673 int err;
675 memset(&cmd, 0, sizeof(cmd));
676 cmd.opcode = MMC_SWITCH_FUNC;
677 cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
678 (index << 16) |
679 (value << 8) |
680 set;
681 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
682 cmd.data = NULL;
683 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
684 return (err);
687 static int
688 mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
689 uint8_t *res)
691 int err;
692 struct mmc_command cmd;
693 struct mmc_data data;
695 memset(&cmd, 0, sizeof(cmd));
696 memset(&data, 0, sizeof(data));
697 memset(res, 0, 64);
699 cmd.opcode = SD_SWITCH_FUNC;
700 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
701 cmd.arg = mode << 31; /* 0 - check, 1 - set */
702 cmd.arg |= 0x00FFFFFF;
703 cmd.arg &= ~(0xF << (grp * 4));
704 cmd.arg |= value << (grp * 4);
705 cmd.data = &data;
707 data.data = res;
708 data.len = 64;
709 data.flags = MMC_DATA_READ;
711 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
712 return (err);
715 static int
716 mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
718 struct mmc_command cmd;
719 int err;
720 uint8_t value;
722 if (mmcbr_get_mode(sc->dev) == mode_sd) {
723 memset(&cmd, 0, sizeof(cmd));
724 cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
725 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
726 cmd.arg = SD_CLR_CARD_DETECT;
727 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
728 if (err != 0)
729 return (err);
730 memset(&cmd, 0, sizeof(cmd));
731 cmd.opcode = ACMD_SET_BUS_WIDTH;
732 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
733 switch (width) {
734 case bus_width_1:
735 cmd.arg = SD_BUS_WIDTH_1;
736 break;
737 case bus_width_4:
738 cmd.arg = SD_BUS_WIDTH_4;
739 break;
740 default:
741 return (MMC_ERR_INVALID);
743 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
744 } else {
745 switch (width) {
746 case bus_width_1:
747 value = EXT_CSD_BUS_WIDTH_1;
748 break;
749 case bus_width_4:
750 value = EXT_CSD_BUS_WIDTH_4;
751 break;
752 case bus_width_8:
753 value = EXT_CSD_BUS_WIDTH_8;
754 break;
755 default:
756 return (MMC_ERR_INVALID);
758 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
759 value);
761 return (err);
764 static int
765 mmc_set_timing(struct mmc_softc *sc, int timing)
767 int err;
768 uint8_t value;
769 u_char switch_res[64];
771 switch (timing) {
772 case bus_timing_normal:
773 value = 0;
774 break;
775 case bus_timing_hs:
776 value = 1;
777 break;
778 default:
779 return (MMC_ERR_INVALID);
781 if (mmcbr_get_mode(sc->dev) == mode_sd)
782 err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
783 value, switch_res);
784 else
785 err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
786 EXT_CSD_HS_TIMING, value);
787 return (err);
790 static int
791 mmc_test_bus_width(struct mmc_softc *sc)
793 struct mmc_command cmd;
794 struct mmc_data data;
795 int err;
796 uint8_t buf[8];
797 uint8_t p8[8] = { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
798 uint8_t p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
799 uint8_t p4[4] = { 0x5A, 0x00, 0x00, 0x00, };
800 uint8_t p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
802 if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
803 mmcbr_set_bus_width(sc->dev, bus_width_8);
804 mmcbr_update_ios(sc->dev);
806 sc->squelched++; /* Errors are expected, squelch reporting. */
807 memset(&cmd, 0, sizeof(cmd));
808 memset(&data, 0, sizeof(data));
809 cmd.opcode = MMC_BUSTEST_W;
810 cmd.arg = 0;
811 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
812 cmd.data = &data;
814 data.data = p8;
815 data.len = 8;
816 data.flags = MMC_DATA_WRITE;
817 mmc_wait_for_cmd(sc, &cmd, 0);
819 memset(&cmd, 0, sizeof(cmd));
820 memset(&data, 0, sizeof(data));
821 cmd.opcode = MMC_BUSTEST_R;
822 cmd.arg = 0;
823 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
824 cmd.data = &data;
826 data.data = buf;
827 data.len = 8;
828 data.flags = MMC_DATA_READ;
829 err = mmc_wait_for_cmd(sc, &cmd, 0);
830 sc->squelched--;
832 mmcbr_set_bus_width(sc->dev, bus_width_1);
833 mmcbr_update_ios(sc->dev);
835 if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
836 return (bus_width_8);
839 if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
840 mmcbr_set_bus_width(sc->dev, bus_width_4);
841 mmcbr_update_ios(sc->dev);
843 sc->squelched++; /* Errors are expected, squelch reporting. */
844 memset(&cmd, 0, sizeof(cmd));
845 memset(&data, 0, sizeof(data));
846 cmd.opcode = MMC_BUSTEST_W;
847 cmd.arg = 0;
848 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
849 cmd.data = &data;
851 data.data = p4;
852 data.len = 4;
853 data.flags = MMC_DATA_WRITE;
854 mmc_wait_for_cmd(sc, &cmd, 0);
856 memset(&cmd, 0, sizeof(cmd));
857 memset(&data, 0, sizeof(data));
858 cmd.opcode = MMC_BUSTEST_R;
859 cmd.arg = 0;
860 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
861 cmd.data = &data;
863 data.data = buf;
864 data.len = 4;
865 data.flags = MMC_DATA_READ;
866 err = mmc_wait_for_cmd(sc, &cmd, 0);
867 sc->squelched--;
869 mmcbr_set_bus_width(sc->dev, bus_width_1);
870 mmcbr_update_ios(sc->dev);
872 if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
873 return (bus_width_4);
875 return (bus_width_1);
878 static uint32_t
879 mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
881 const int i = (bit_len / 32) - (start / 32) - 1;
882 const int shift = start & 31;
883 uint32_t retval = bits[i] >> shift;
884 if (size + shift > 32)
885 retval |= bits[i - 1] << (32 - shift);
886 return (retval & ((1llu << size) - 1));
889 static void
890 mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
892 int i;
894 /* There's no version info, so we take it on faith */
895 memset(cid, 0, sizeof(*cid));
896 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
897 cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
898 for (i = 0; i < 5; i++)
899 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
900 cid->pnm[5] = 0;
901 cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
902 cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
903 cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
904 cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
907 static void
908 mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
910 int i;
912 /* There's no version info, so we take it on faith */
913 memset(cid, 0, sizeof(*cid));
914 cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
915 cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
916 for (i = 0; i < 6; i++)
917 cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
918 cid->pnm[6] = 0;
919 cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
920 cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
921 cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
922 cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
925 static void
926 mmc_format_card_id_string(struct mmc_ivars *ivar)
928 char oidstr[8];
929 uint8_t c1;
930 uint8_t c2;
933 * Format a card ID string for use by the mmcsd driver, it's what
934 * appears between the <> in the following:
935 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
936 * 22.5MHz/4bit/128-block
938 * Also format just the card serial number, which the mmcsd driver will
939 * use as the disk->d_ident string.
941 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
942 * and our max formatted length is currently 55 bytes if every field
943 * contains the largest value.
945 * Sometimes the oid is two printable ascii chars; when it's not,
946 * format it as 0xnnnn instead.
948 c1 = (ivar->cid.oid >> 8) & 0x0ff;
949 c2 = ivar->cid.oid & 0x0ff;
950 if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
951 ksnprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
952 else
953 ksnprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
954 ksnprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
955 "%08X", ivar->cid.psn);
956 ksnprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
957 "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
958 ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
959 ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
960 ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
961 ivar->cid.mid, oidstr);
964 static const int exp[8] = {
965 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
968 static const int mant[16] = {
969 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
972 static const int cur_min[8] = {
973 500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
976 static const int cur_max[8] = {
977 1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
980 static void
981 mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
983 int v;
984 int m;
985 int e;
987 memset(csd, 0, sizeof(*csd));
988 csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
989 if (v == 0) {
990 m = mmc_get_bits(raw_csd, 128, 115, 4);
991 e = mmc_get_bits(raw_csd, 128, 112, 3);
992 csd->tacc = (exp[e] * mant[m] + 9) / 10;
993 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
994 m = mmc_get_bits(raw_csd, 128, 99, 4);
995 e = mmc_get_bits(raw_csd, 128, 96, 3);
996 csd->tran_speed = exp[e] * 10000 * mant[m];
997 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
998 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
999 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1000 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1001 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1002 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1003 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1004 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1005 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1006 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1007 m = mmc_get_bits(raw_csd, 128, 62, 12);
1008 e = mmc_get_bits(raw_csd, 128, 47, 3);
1009 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1010 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1011 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1012 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1013 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1014 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1015 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1016 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1017 } else if (v == 1) {
1018 m = mmc_get_bits(raw_csd, 128, 115, 4);
1019 e = mmc_get_bits(raw_csd, 128, 112, 3);
1020 csd->tacc = (exp[e] * mant[m] + 9) / 10;
1021 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1022 m = mmc_get_bits(raw_csd, 128, 99, 4);
1023 e = mmc_get_bits(raw_csd, 128, 96, 3);
1024 csd->tran_speed = exp[e] * 10000 * mant[m];
1025 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1026 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1027 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1028 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1029 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1030 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1031 csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
1032 512 * 1024;
1033 csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1034 csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1035 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1036 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1037 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1038 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1039 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1040 } else
1041 panic("unknown SD CSD version");
1044 static void
1045 mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
1047 int m;
1048 int e;
1050 memset(csd, 0, sizeof(*csd));
1051 csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
1052 csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1053 m = mmc_get_bits(raw_csd, 128, 115, 4);
1054 e = mmc_get_bits(raw_csd, 128, 112, 3);
1055 csd->tacc = exp[e] * mant[m] + 9 / 10;
1056 csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1057 m = mmc_get_bits(raw_csd, 128, 99, 4);
1058 e = mmc_get_bits(raw_csd, 128, 96, 3);
1059 csd->tran_speed = exp[e] * 10000 * mant[m];
1060 csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1061 csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1062 csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1063 csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1064 csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1065 csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1066 csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1067 csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1068 csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1069 csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1070 m = mmc_get_bits(raw_csd, 128, 62, 12);
1071 e = mmc_get_bits(raw_csd, 128, 47, 3);
1072 csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1073 csd->erase_blk_en = 0;
1074 csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1075 (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1076 csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1077 csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1078 csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1079 csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1080 csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1083 static void
1084 mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1086 unsigned int scr_struct;
1088 memset(scr, 0, sizeof(*scr));
1090 scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1091 if (scr_struct != 0) {
1092 kprintf("Unrecognised SCR structure version %d\n",
1093 scr_struct);
1094 return;
1096 scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1097 scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1100 static void
1101 mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1102 struct mmc_sd_status *sd_status)
1105 memset(sd_status, 0, sizeof(*sd_status));
1107 sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1108 sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1109 sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1110 sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1111 sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1112 sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1113 sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1114 sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1115 sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1116 sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1119 static int
1120 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1122 struct mmc_command cmd;
1123 int err;
1125 memset(&cmd, 0, sizeof(cmd));
1126 cmd.opcode = MMC_ALL_SEND_CID;
1127 cmd.arg = 0;
1128 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1129 cmd.data = NULL;
1130 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1131 memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1132 return (err);
1135 static int
1136 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1138 struct mmc_command cmd;
1139 int err;
1141 memset(&cmd, 0, sizeof(cmd));
1142 cmd.opcode = MMC_SEND_CSD;
1143 cmd.arg = rca << 16;
1144 cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1145 cmd.data = NULL;
1146 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1147 memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1148 return (err);
1151 static int
1152 mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1154 int err;
1155 struct mmc_command cmd;
1156 struct mmc_data data;
1158 memset(&cmd, 0, sizeof(cmd));
1159 memset(&data, 0, sizeof(data));
1161 memset(rawscr, 0, 8);
1162 cmd.opcode = ACMD_SEND_SCR;
1163 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1164 cmd.arg = 0;
1165 cmd.data = &data;
1167 data.data = rawscr;
1168 data.len = 8;
1169 data.flags = MMC_DATA_READ;
1171 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1172 rawscr[0] = be32toh(rawscr[0]);
1173 rawscr[1] = be32toh(rawscr[1]);
1174 return (err);
1177 static int
1178 mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1180 int err;
1181 struct mmc_command cmd;
1182 struct mmc_data data;
1184 memset(&cmd, 0, sizeof(cmd));
1185 memset(&data, 0, sizeof(data));
1187 memset(rawextcsd, 0, 512);
1188 cmd.opcode = MMC_SEND_EXT_CSD;
1189 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1190 cmd.arg = 0;
1191 cmd.data = &data;
1193 data.data = rawextcsd;
1194 data.len = 512;
1195 data.flags = MMC_DATA_READ;
1197 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1198 return (err);
1201 static int
1202 mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1204 int err, i;
1205 struct mmc_command cmd;
1206 struct mmc_data data;
1208 memset(&cmd, 0, sizeof(cmd));
1209 memset(&data, 0, sizeof(data));
1211 memset(rawsdstatus, 0, 64);
1212 cmd.opcode = ACMD_SD_STATUS;
1213 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1214 cmd.arg = 0;
1215 cmd.data = &data;
1217 data.data = rawsdstatus;
1218 data.len = 64;
1219 data.flags = MMC_DATA_READ;
1221 err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1222 for (i = 0; i < 16; i++)
1223 rawsdstatus[i] = be32toh(rawsdstatus[i]);
1224 return (err);
1227 static int
1228 mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1230 struct mmc_command cmd;
1231 int err;
1233 memset(&cmd, 0, sizeof(cmd));
1234 cmd.opcode = MMC_SET_RELATIVE_ADDR;
1235 cmd.arg = resp << 16;
1236 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1237 cmd.data = NULL;
1238 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1239 return (err);
1242 static int
1243 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1245 struct mmc_command cmd;
1246 int err;
1248 memset(&cmd, 0, sizeof(cmd));
1249 cmd.opcode = SD_SEND_RELATIVE_ADDR;
1250 cmd.arg = 0;
1251 cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1252 cmd.data = NULL;
1253 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1254 *resp = cmd.resp[0];
1255 return (err);
1258 static int
1259 mmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status)
1261 struct mmc_command cmd;
1262 int err;
1264 memset(&cmd, 0, sizeof(cmd));
1265 cmd.opcode = MMC_SEND_STATUS;
1266 cmd.arg = rca << 16;
1267 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1268 cmd.data = NULL;
1269 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1270 *status = cmd.resp[0];
1271 return (err);
1274 static int
1275 mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1277 struct mmc_command cmd;
1278 int err;
1280 memset(&cmd, 0, sizeof(cmd));
1281 cmd.opcode = MMC_SET_BLOCKLEN;
1282 cmd.arg = len;
1283 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1284 cmd.data = NULL;
1285 err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1286 return (err);
1289 static void
1290 mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1292 device_printf(dev, "Card at relative address 0x%04x%s:\n",
1293 ivar->rca, newcard ? " added" : "");
1294 device_printf(dev, " card: %s\n", ivar->card_id_string);
1295 device_printf(dev, " bus: %ubit, %uMHz%s\n",
1296 (ivar->bus_width == bus_width_1 ? 1 :
1297 (ivar->bus_width == bus_width_4 ? 4 : 8)),
1298 (ivar->timing == bus_timing_hs ?
1299 ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1300 ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1301 device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1302 ivar->sec_count, ivar->erase_sector,
1303 ivar->read_only ? ", read-only" : "");
1306 static void
1307 mmc_discover_cards(struct mmc_softc *sc)
1309 struct mmc_ivars *ivar = NULL;
1310 device_t *devlist;
1311 int err, i, devcount, newcard;
1312 uint32_t raw_cid[4], resp, sec_count, status;
1313 device_t child;
1314 uint16_t rca = 2;
1315 u_char switch_res[64];
1317 if (bootverbose || mmc_debug)
1318 device_printf(sc->dev, "Probing cards\n");
1319 while (1) {
1320 sc->squelched++; /* Errors are expected, squelch reporting. */
1321 err = mmc_all_send_cid(sc, raw_cid);
1322 sc->squelched--;
1323 if (err == MMC_ERR_TIMEOUT)
1324 break;
1325 if (err != MMC_ERR_NONE) {
1326 device_printf(sc->dev, "Error reading CID %d\n", err);
1327 break;
1329 newcard = 1;
1330 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1331 return;
1332 for (i = 0; i < devcount; i++) {
1333 ivar = device_get_ivars(devlist[i]);
1334 if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1335 newcard = 0;
1336 break;
1339 kfree(devlist, M_TEMP);
1340 if (bootverbose || mmc_debug) {
1341 device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1342 newcard ? "New c" : "C",
1343 raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1345 if (newcard) {
1346 ivar = kmalloc(sizeof(struct mmc_ivars), M_DEVBUF,
1347 M_WAITOK | M_ZERO);
1348 memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1350 if (mmcbr_get_ro(sc->dev))
1351 ivar->read_only = 1;
1352 ivar->bus_width = bus_width_1;
1353 ivar->timing = bus_timing_normal;
1354 ivar->mode = mmcbr_get_mode(sc->dev);
1355 if (ivar->mode == mode_sd) {
1356 mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1357 mmc_send_relative_addr(sc, &resp);
1358 ivar->rca = resp >> 16;
1359 /* Get card CSD. */
1360 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1361 if (bootverbose || mmc_debug)
1362 device_printf(sc->dev,
1363 "%sard detected (CSD %08x%08x%08x%08x)\n",
1364 newcard ? "New c" : "C", ivar->raw_csd[0],
1365 ivar->raw_csd[1], ivar->raw_csd[2],
1366 ivar->raw_csd[3]);
1367 mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1368 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1369 if (ivar->csd.csd_structure > 0)
1370 ivar->high_cap = 1;
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);
1379 break;
1381 if ((status & R1_CARD_IS_LOCKED) != 0) {
1382 device_printf(sc->dev,
1383 "Card is password protected, skipping.\n");
1384 break;
1387 /* Get card SCR. Card must be selected to fetch it. */
1388 mmc_select_card(sc, ivar->rca);
1389 mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1390 mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1391 /* Get card switch capabilities (command class 10). */
1392 if ((ivar->scr.sda_vsn >= 1) &&
1393 (ivar->csd.ccc & (1<<10))) {
1394 mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1395 SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1396 switch_res);
1397 if (switch_res[13] & 2) {
1398 ivar->timing = bus_timing_hs;
1399 ivar->hs_tran_speed = SD_MAX_HS;
1404 * We deselect then reselect the card here. Some cards
1405 * become unselected and timeout with the above two
1406 * commands, although the state tables / diagrams in the
1407 * standard suggest they go back to the transfer state.
1408 * Other cards don't become deselected, and if we
1409 * atttempt to blindly re-select them, we get timeout
1410 * errors from some controllers. So we deselect then
1411 * reselect to handle all situations. The only thing we
1412 * use from the sd_status is the erase sector size, but
1413 * it is still nice to get that right.
1415 mmc_select_card(sc, 0);
1416 mmc_select_card(sc, ivar->rca);
1417 mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1418 mmc_app_decode_sd_status(ivar->raw_sd_status,
1419 &ivar->sd_status);
1420 if (ivar->sd_status.au_size != 0) {
1421 ivar->erase_sector =
1422 16 << ivar->sd_status.au_size;
1424 /* Find max supported bus width. */
1425 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1426 (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1427 ivar->bus_width = bus_width_4;
1430 * Some cards that report maximum I/O block sizes
1431 * greater than 512 require the block length to be
1432 * set to 512, even though that is supposed to be
1433 * the default. Example:
1435 * Transcend 2GB SDSC card, CID:
1436 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1438 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1439 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1440 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1442 mmc_format_card_id_string(ivar);
1444 if (bootverbose || mmc_debug)
1445 mmc_log_card(sc->dev, ivar, newcard);
1446 if (newcard) {
1447 /* Add device. */
1448 child = device_add_child(sc->dev, NULL, -1);
1449 device_set_ivars(child, ivar);
1451 mmc_select_card(sc, 0);
1452 return;
1454 mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1455 ivar->rca = rca++;
1456 mmc_set_relative_addr(sc, ivar->rca);
1457 /* Get card CSD. */
1458 mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1459 if (bootverbose || mmc_debug)
1460 device_printf(sc->dev,
1461 "%sard detected (CSD %08x%08x%08x%08x)\n",
1462 newcard ? "New c" : "C", ivar->raw_csd[0],
1463 ivar->raw_csd[1], ivar->raw_csd[2],
1464 ivar->raw_csd[3]);
1466 mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1467 ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1468 ivar->tran_speed = ivar->csd.tran_speed;
1469 ivar->erase_sector = ivar->csd.erase_sector *
1470 ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1472 err = mmc_send_status(sc, ivar->rca, &status);
1473 if (err != MMC_ERR_NONE) {
1474 device_printf(sc->dev,
1475 "Error reading card status %d\n", err);
1476 break;
1478 if ((status & R1_CARD_IS_LOCKED) != 0) {
1479 device_printf(sc->dev,
1480 "Card is password protected, skipping.\n");
1481 break;
1484 mmc_select_card(sc, ivar->rca);
1486 /* Only MMC >= 4.x cards support EXT_CSD. */
1487 if (ivar->csd.spec_vers >= 4) {
1488 mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1489 /* Handle extended capacity from EXT_CSD */
1490 sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1491 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1492 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1493 (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1494 if (sec_count != 0) {
1495 ivar->sec_count = sec_count;
1496 ivar->high_cap = 1;
1498 /* Get card speed in high speed mode. */
1499 ivar->timing = bus_timing_hs;
1500 if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1501 & EXT_CSD_CARD_TYPE_52)
1502 ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1503 else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1504 & EXT_CSD_CARD_TYPE_26)
1505 ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1506 else
1507 ivar->hs_tran_speed = ivar->tran_speed;
1508 /* Find max supported bus width. */
1509 ivar->bus_width = mmc_test_bus_width(sc);
1510 /* Handle HC erase sector size. */
1511 if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1512 ivar->erase_sector = 1024 *
1513 ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1514 mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1515 EXT_CSD_ERASE_GRP_DEF, 1);
1517 } else {
1518 ivar->bus_width = bus_width_1;
1519 ivar->timing = bus_timing_normal;
1523 * Some cards that report maximum I/O block sizes greater
1524 * than 512 require the block length to be set to 512, even
1525 * though that is supposed to be the default. Example:
1527 * Transcend 2GB SDSC card, CID:
1528 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1530 if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1531 ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1532 mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1534 mmc_format_card_id_string(ivar);
1536 if (bootverbose || mmc_debug)
1537 mmc_log_card(sc->dev, ivar, newcard);
1538 if (newcard) {
1539 /* Add device. */
1540 child = device_add_child(sc->dev, NULL, -1);
1541 device_set_ivars(child, ivar);
1543 mmc_select_card(sc, 0);
1547 static void
1548 mmc_rescan_cards(struct mmc_softc *sc)
1550 struct mmc_ivars *ivar = NULL;
1551 device_t *devlist;
1552 int err, i, devcount;
1554 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1555 return;
1556 for (i = 0; i < devcount; i++) {
1557 ivar = device_get_ivars(devlist[i]);
1558 if (mmc_select_card(sc, ivar->rca)) {
1559 if (bootverbose || mmc_debug)
1560 device_printf(sc->dev, "Card at relative address %d lost.\n",
1561 ivar->rca);
1562 device_delete_child(sc->dev, devlist[i]);
1563 kfree(ivar, M_DEVBUF);
1566 kfree(devlist, M_TEMP);
1567 mmc_select_card(sc, 0);
1570 static int
1571 mmc_delete_cards(struct mmc_softc *sc)
1573 struct mmc_ivars *ivar;
1574 device_t *devlist;
1575 int err, i, devcount;
1577 if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1578 return (err);
1579 for (i = 0; i < devcount; i++) {
1580 ivar = device_get_ivars(devlist[i]);
1581 if (bootverbose || mmc_debug)
1582 device_printf(sc->dev, "Card at relative address %d deleted.\n",
1583 ivar->rca);
1584 device_delete_child(sc->dev, devlist[i]);
1585 kfree(ivar, M_DEVBUF);
1587 kfree(devlist, M_TEMP);
1588 return (0);
1591 static void
1592 mmc_go_discovery(struct mmc_softc *sc)
1594 uint32_t ocr;
1595 device_t dev;
1596 int err;
1598 dev = sc->dev;
1599 if (mmcbr_get_power_mode(dev) != power_on) {
1601 * First, try SD modes
1603 sc->squelched++; /* Errors are expected, squelch reporting. */
1604 mmcbr_set_mode(dev, mode_sd);
1605 mmc_power_up(sc);
1606 mmcbr_set_bus_mode(dev, pushpull);
1607 if (bootverbose || mmc_debug)
1608 device_printf(sc->dev, "Probing bus\n");
1609 mmc_idle_cards(sc);
1610 err = mmc_send_if_cond(sc, 1);
1611 if ((bootverbose || mmc_debug) && err == 0)
1612 device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1613 if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1614 if (bootverbose || mmc_debug)
1615 device_printf(sc->dev, "SD probe: failed\n");
1617 * Failed, try MMC
1619 mmcbr_set_mode(dev, mode_mmc);
1620 if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1621 if (bootverbose || mmc_debug)
1622 device_printf(sc->dev, "MMC probe: failed\n");
1623 ocr = 0; /* Failed both, powerdown. */
1624 } else if (bootverbose || mmc_debug)
1625 device_printf(sc->dev,
1626 "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1627 } else if (bootverbose || mmc_debug)
1628 device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1629 sc->squelched--;
1631 mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1632 if (mmcbr_get_ocr(dev) != 0)
1633 mmc_idle_cards(sc);
1634 } else {
1635 mmcbr_set_bus_mode(dev, opendrain);
1636 mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1637 mmcbr_update_ios(dev);
1638 /* XXX recompute vdd based on new cards? */
1641 * Make sure that we have a mutually agreeable voltage to at least
1642 * one card on the bus.
1644 if (bootverbose || mmc_debug)
1645 device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1646 if (mmcbr_get_ocr(dev) == 0) {
1647 device_printf(sc->dev, "No compatible cards found on bus\n");
1648 mmc_delete_cards(sc);
1649 mmc_power_down(sc);
1650 return;
1653 * Reselect the cards after we've idled them above.
1655 if (mmcbr_get_mode(dev) == mode_sd) {
1656 err = mmc_send_if_cond(sc, 1);
1657 mmc_send_app_op_cond(sc,
1658 (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1659 } else
1660 mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1661 mmc_discover_cards(sc);
1662 mmc_rescan_cards(sc);
1664 mmcbr_set_bus_mode(dev, pushpull);
1665 mmcbr_update_ios(dev);
1666 mmc_calculate_clock(sc);
1667 bus_generic_attach(dev);
1668 /* mmc_update_children_sysctl(dev);*/
1671 static int
1672 mmc_calculate_clock(struct mmc_softc *sc)
1674 int max_dtr, max_hs_dtr, max_timing;
1675 int nkid, i, f_max;
1676 device_t *kids;
1677 struct mmc_ivars *ivar;
1679 f_max = mmcbr_get_f_max(sc->dev);
1680 max_dtr = max_hs_dtr = f_max;
1681 if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1682 max_timing = bus_timing_hs;
1683 else
1684 max_timing = bus_timing_normal;
1685 if (device_get_children(sc->dev, &kids, &nkid) != 0)
1686 panic("can't get children");
1687 for (i = 0; i < nkid; i++) {
1688 ivar = device_get_ivars(kids[i]);
1689 if (ivar->timing < max_timing)
1690 max_timing = ivar->timing;
1691 if (ivar->tran_speed < max_dtr)
1692 max_dtr = ivar->tran_speed;
1693 if (ivar->hs_tran_speed < max_hs_dtr)
1694 max_hs_dtr = ivar->hs_tran_speed;
1696 for (i = 0; i < nkid; i++) {
1697 ivar = device_get_ivars(kids[i]);
1698 if (ivar->timing == bus_timing_normal)
1699 continue;
1700 mmc_select_card(sc, ivar->rca);
1701 mmc_set_timing(sc, max_timing);
1703 mmc_select_card(sc, 0);
1704 kfree(kids, M_TEMP);
1705 if (max_timing == bus_timing_hs)
1706 max_dtr = max_hs_dtr;
1707 if (bootverbose || mmc_debug) {
1708 device_printf(sc->dev,
1709 "setting transfer rate to %d.%03dMHz%s\n",
1710 max_dtr / 1000000, (max_dtr / 1000) % 1000,
1711 max_timing == bus_timing_hs ? " (high speed timing)" : "");
1713 mmcbr_set_timing(sc->dev, max_timing);
1714 mmcbr_set_clock(sc->dev, max_dtr);
1715 mmcbr_update_ios(sc->dev);
1716 return max_dtr;
1719 static void
1720 mmc_scan(struct mmc_softc *sc)
1722 device_t dev = sc->dev;
1724 mmc_acquire_bus(dev, dev);
1725 mmc_go_discovery(sc);
1726 mmc_release_bus(dev, dev);
1729 static int
1730 mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1732 struct mmc_ivars *ivar = device_get_ivars(child);
1734 switch (which) {
1735 default:
1736 return (EINVAL);
1737 case MMC_IVAR_DSR_IMP:
1738 *(int *)result = ivar->csd.dsr_imp;
1739 break;
1740 case MMC_IVAR_MEDIA_SIZE:
1741 *(off_t *)result = ivar->sec_count;
1742 break;
1743 case MMC_IVAR_RCA:
1744 *(int *)result = ivar->rca;
1745 break;
1746 case MMC_IVAR_SECTOR_SIZE:
1747 *(int *)result = MMC_SECTOR_SIZE;
1748 break;
1749 case MMC_IVAR_TRAN_SPEED:
1750 *(int *)result = mmcbr_get_clock(bus);
1751 break;
1752 case MMC_IVAR_READ_ONLY:
1753 *(int *)result = ivar->read_only;
1754 break;
1755 case MMC_IVAR_HIGH_CAP:
1756 *(int *)result = ivar->high_cap;
1757 break;
1758 case MMC_IVAR_CARD_TYPE:
1759 *(int *)result = ivar->mode;
1760 break;
1761 case MMC_IVAR_BUS_WIDTH:
1762 *(int *)result = ivar->bus_width;
1763 break;
1764 case MMC_IVAR_ERASE_SECTOR:
1765 *(int *)result = ivar->erase_sector;
1766 break;
1767 case MMC_IVAR_MAX_DATA:
1768 *(int *)result = mmcbr_get_max_data(bus);
1769 break;
1770 case MMC_IVAR_CARD_ID_STRING:
1771 *(char **)result = ivar->card_id_string;
1772 break;
1773 case MMC_IVAR_CARD_SN_STRING:
1774 *(char **)result = ivar->card_sn_string;
1775 break;
1777 return (0);
1780 static int
1781 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1784 * None are writable ATM
1786 return (EINVAL);
1790 static void
1791 mmc_delayed_attach(void *xsc)
1793 struct mmc_softc *sc = xsc;
1795 mmc_scan(sc);
1796 config_intrhook_disestablish(&sc->config_intrhook);
1799 static int
1800 mmc_child_location_str(device_t dev, device_t child, char *buf,
1801 size_t buflen)
1804 ksnprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1805 return (0);
1808 static device_method_t mmc_methods[] = {
1809 /* device_if */
1810 DEVMETHOD(device_probe, mmc_probe),
1811 DEVMETHOD(device_attach, mmc_attach),
1812 DEVMETHOD(device_detach, mmc_detach),
1813 DEVMETHOD(device_suspend, mmc_suspend),
1814 DEVMETHOD(device_resume, mmc_resume),
1816 /* Bus interface */
1817 DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1818 DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1819 DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1821 /* MMC Bus interface */
1822 DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1823 DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1824 DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1826 DEVMETHOD_END
1829 static driver_t mmc_driver = {
1830 "mmc",
1831 mmc_methods,
1832 sizeof(struct mmc_softc),
1834 static devclass_t mmc_devclass;
1837 DRIVER_MODULE(mmc, sdhci_pci, mmc_driver, mmc_devclass, NULL, NULL);
1838 DRIVER_MODULE(mmc, sdhci_acpi, mmc_driver, mmc_devclass, NULL, NULL);