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