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