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