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