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