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