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