xref: /freebsd/sys/dev/mmc/mmc.c (revision 282a3889ebf826db9839be296ff1dd903f6d6d6e)
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 
65 #include <dev/mmc/mmcreg.h>
66 #include <dev/mmc/mmcbrvar.h>
67 #include <dev/mmc/mmcvar.h>
68 #include "mmcbr_if.h"
69 #include "mmcbus_if.h"
70 
71 struct mmc_softc {
72 	device_t dev;
73 	struct mtx sc_mtx;
74 	struct intr_config_hook config_intrhook;
75 	device_t owner;
76 	uint32_t last_rca;
77 };
78 
79 /*
80  * Per-card data
81  */
82 struct mmc_ivars {
83 	uint32_t raw_cid[4];	/* Raw bits of the CID */
84 	uint32_t raw_csd[4];	/* Raw bits of the CSD */
85 	uint16_t rca;
86 	enum mmc_card_mode mode;
87 	struct mmc_cid cid;	/* cid decoded */
88 	struct mmc_csd csd;	/* csd decoded */
89 };
90 
91 #define CMD_RETRIES	3
92 
93 /* bus entry points */
94 static int mmc_probe(device_t dev);
95 static int mmc_attach(device_t dev);
96 static int mmc_detach(device_t dev);
97 
98 #define MMC_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
99 #define	MMC_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
100 #define MMC_LOCK_INIT(_sc) \
101 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \
102 	    "mmc", MTX_DEF)
103 #define MMC_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
104 #define MMC_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED);
105 #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
106 
107 static void mmc_delayed_attach(void *);
108 static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
109     int retries);
110 static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
111     uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
112 
113 static void
114 mmc_ms_delay(int ms)
115 {
116 	DELAY(1000 * ms);	/* XXX BAD */
117 }
118 
119 static int
120 mmc_probe(device_t dev)
121 {
122 
123 	device_set_desc(dev, "mmc/sd bus");
124 	return (0);
125 }
126 
127 static int
128 mmc_attach(device_t dev)
129 {
130 	struct mmc_softc *sc;
131 
132 	sc = device_get_softc(dev);
133 	sc->dev = dev;
134 	MMC_LOCK_INIT(sc);
135 
136 	/* We'll probe and attach our children later, but before / mount */
137 	sc->config_intrhook.ich_func = mmc_delayed_attach;
138 	sc->config_intrhook.ich_arg = sc;
139 	if (config_intrhook_establish(&sc->config_intrhook) != 0)
140 		device_printf(dev, "config_intrhook_establish failed\n");
141 	return (0);
142 }
143 
144 static int
145 mmc_detach(device_t dev)
146 {
147 	struct mmc_softc *sc = device_get_softc(dev);
148 	device_t *kids;
149 	int i, nkid;
150 
151 	/* kill children [ph33r].  -sorbo */
152 	if (device_get_children(sc->dev, &kids, &nkid) != 0)
153 		return 0;
154 	for (i = 0; i < nkid; i++) {
155 		device_t kid = kids[i];
156 		void *ivar = device_get_ivars(kid);
157 
158 		device_detach(kid);
159 		device_delete_child(sc->dev, kid);
160 		free(ivar, M_DEVBUF);
161 	}
162 	free(kids, M_TEMP);
163 
164 	MMC_LOCK_DESTROY(sc);
165 
166 	return 0;
167 }
168 
169 static int
170 mmc_acquire_bus(device_t busdev, device_t dev)
171 {
172 	struct mmc_softc *sc;
173 	int err;
174 	int rca;
175 
176 	err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), dev);
177 	if (err)
178 		return (err);
179 	sc = device_get_softc(busdev);
180 	MMC_LOCK(sc);
181 	if (sc->owner)
182 		panic("mmc: host bridge didn't seralize us.");
183 	sc->owner = dev;
184 	MMC_UNLOCK(sc);
185 
186 	if (busdev != dev) {
187 		// Keep track of the last rca that we've selected.  If
188 		// we're asked to do it again, don't.  We never unselect
189 		// unless the bus code itself wants the mmc bus.
190 		rca = mmc_get_rca(dev);
191 		if (sc->last_rca != rca) {
192 			mmc_wait_for_command(sc, MMC_SELECT_CARD, rca << 16,
193 			    MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES);
194 			sc->last_rca = rca;
195 		}
196 		// XXX should set bus width here?
197 	} else {
198 		// If there's a card selected, stand down.
199 		if (sc->last_rca != 0) {
200 			mmc_wait_for_command(sc, MMC_SELECT_CARD, 0,
201 			    MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES);
202 			sc->last_rca = 0;
203 		}
204 		// XXX should set bus width here?
205 	}
206 
207 	return (0);
208 }
209 
210 static int
211 mmc_release_bus(device_t busdev, device_t dev)
212 {
213 	struct mmc_softc *sc;
214 	int err;
215 
216 	sc = device_get_softc(busdev);
217 
218 	MMC_LOCK(sc);
219 	if (!sc->owner)
220 		panic("mmc: releasing unowned bus.");
221 	if (sc->owner != dev)
222 		panic("mmc: you don't own the bus.  game over.");
223 	MMC_UNLOCK(sc);
224 	err = MMCBR_RELEASE_HOST(device_get_parent(busdev), dev);
225 	if (err)
226 		return (err);
227 	MMC_LOCK(sc);
228 	sc->owner = NULL;
229 	MMC_UNLOCK(sc);
230 	return (0);
231 }
232 
233 static void
234 mmc_rescan_cards(struct mmc_softc *sc)
235 {
236 	/* XXX: Look at the children and see if they respond to status */
237 }
238 
239 static uint32_t
240 mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
241 {
242     // XXX
243 	return ocr;
244 }
245 
246 static int
247 mmc_highest_voltage(uint32_t ocr)
248 {
249 	int i;
250 
251 	for (i = 30; i >= 0; i--)
252 		if (ocr & (1 << i))
253 			return i;
254 	return (-1);
255 }
256 
257 static void
258 mmc_wakeup(struct mmc_request *req)
259 {
260 	struct mmc_softc *sc;
261 
262 //	printf("Wakeup for req %p done_data %p\n", req, req->done_data);
263 	sc = (struct mmc_softc *)req->done_data;
264 	MMC_LOCK(sc);
265 	req->flags |= MMC_REQ_DONE;
266 	wakeup(req);
267 	MMC_UNLOCK(sc);
268 }
269 
270 static int
271 mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
272 {
273 	int err;
274 
275 	req->done = mmc_wakeup;
276 	req->done_data = sc;
277 //	printf("Submitting request %p sc %p\n", req, sc);
278 	MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
279 	MMC_LOCK(sc);
280 	do {
281 		err = msleep(req, &sc->sc_mtx, PZERO | PCATCH, "mmcreq",
282 		    hz / 10);
283 	} while (!(req->flags & MMC_REQ_DONE) && err == EAGAIN);
284 //	printf("Request %p done with error %d\n", req, err);
285 	MMC_UNLOCK(sc);
286 	return (err);
287 }
288 
289 static int
290 mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
291 {
292 	struct mmc_softc *sc = device_get_softc(brdev);
293 
294 	return mmc_wait_for_req(sc, req);
295 }
296 
297 static int
298 mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
299 {
300 	struct mmc_request mreq;
301 
302 	memset(&mreq, 0, sizeof(mreq));
303 	memset(cmd->resp, 0, sizeof(cmd->resp));
304 	cmd->retries = retries;
305 	cmd->data = NULL;
306 	mreq.cmd = cmd;
307 //	printf("CMD: %x ARG %x\n", cmd->opcode, cmd->arg);
308 	mmc_wait_for_req(sc, &mreq);
309 	return (cmd->error);
310 }
311 
312 static int
313 mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
314     struct mmc_command *cmd, int retries)
315 {
316 	struct mmc_command appcmd;
317 	int err = MMC_ERR_NONE, i;
318 
319 	for (i = 0; i <= retries; i++) {
320 		appcmd.opcode = MMC_APP_CMD;
321 		appcmd.arg = rca << 16;
322 		appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
323 		mmc_wait_for_cmd(sc, &appcmd, 0);
324 		err = appcmd.error;
325 		if (err != MMC_ERR_NONE)
326 			continue;
327 		if (!(appcmd.resp[0] & R1_APP_CMD))
328 			return MMC_ERR_FAILED;
329 		mmc_wait_for_cmd(sc, cmd, 0);
330 		err = cmd->error;
331 		if (err == MMC_ERR_NONE)
332 			break;
333 	}
334 	return (err);
335 }
336 
337 static int
338 mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
339     uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
340 {
341 	struct mmc_command cmd;
342 	int err;
343 
344 	memset(&cmd, 0, sizeof(cmd));
345 	cmd.opcode = opcode;
346 	cmd.arg = arg;
347 	cmd.flags = flags;
348 	err = mmc_wait_for_cmd(sc, &cmd, retries);
349 	if (err)
350 		return (err);
351 	if (cmd.error)
352 		return (cmd.error);
353 	if (resp) {
354 		if (flags & MMC_RSP_136)
355 			memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
356 		else
357 			*resp = cmd.resp[0];
358 	}
359 	return (0);
360 }
361 
362 static void
363 mmc_idle_cards(struct mmc_softc *sc)
364 {
365 	device_t dev;
366 	struct mmc_command cmd;
367 
368 	dev = sc->dev;
369 	mmcbr_set_chip_select(dev, cs_high);
370 	mmcbr_update_ios(dev);
371 	mmc_ms_delay(1);
372 
373 	memset(&cmd, 0, sizeof(cmd));
374 	cmd.opcode = MMC_GO_IDLE_STATE;
375 	cmd.arg = 0;
376 	cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
377 	mmc_wait_for_cmd(sc, &cmd, 0);
378 	mmc_ms_delay(1);
379 
380 	mmcbr_set_chip_select(dev, cs_dontcare);
381 	mmcbr_update_ios(dev);
382 	mmc_ms_delay(1);
383 }
384 
385 static int
386 mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
387 {
388 	struct mmc_command cmd;
389 	int err = MMC_ERR_NONE, i;
390 
391 	memset(&cmd, 0, sizeof(cmd));
392 	cmd.opcode = ACMD_SD_SEND_OP_COND;
393 	cmd.arg = ocr;
394 	cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
395 
396 	for (i = 0; i < 100; i++) {
397 		err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
398 		if (err != MMC_ERR_NONE)
399 			break;
400 		if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || ocr == 0)
401 			break;
402 		err = MMC_ERR_TIMEOUT;
403 		mmc_ms_delay(10);
404 	}
405 	if (rocr && err == MMC_ERR_NONE)
406 		*rocr = cmd.resp[0];
407 	return err;
408 }
409 
410 static int
411 mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
412 {
413 	struct mmc_command cmd;
414 	int err = MMC_ERR_NONE, i;
415 
416 	memset(&cmd, 0, sizeof(cmd));
417 	cmd.opcode = MMC_SEND_OP_COND;
418 	cmd.arg = ocr;
419 	cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
420 
421 	for (i = 0; i < 100; i++) {
422 		err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
423 		if (err != MMC_ERR_NONE)
424 			break;
425 		if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || ocr == 0)
426 			break;
427 		err = MMC_ERR_TIMEOUT;
428 		mmc_ms_delay(10);
429 	}
430 	if (rocr && err == MMC_ERR_NONE)
431 		*rocr = cmd.resp[0];
432 	return err;
433 }
434 
435 static void
436 mmc_power_up(struct mmc_softc *sc)
437 {
438 	device_t dev;
439 
440 	dev = sc->dev;
441 	mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
442 	mmcbr_set_bus_mode(dev, opendrain);
443 	mmcbr_set_chip_select(dev, cs_dontcare);
444 	mmcbr_set_bus_width(dev, bus_width_1);
445 	mmcbr_set_power_mode(dev, power_up);
446 	mmcbr_set_clock(dev, 0);
447 	mmcbr_update_ios(dev);
448 	mmc_ms_delay(1);
449 
450 	mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev));
451 	mmcbr_set_power_mode(dev, power_on);
452 	mmcbr_update_ios(dev);
453 	mmc_ms_delay(2);
454 }
455 
456 // I wonder if the following is endian safe.
457 static uint32_t
458 mmc_get_bits(uint32_t *bits, int start, int size)
459 {
460 	const int i = 3 - (start / 32);
461 	const int shift = start & 31;
462 	uint32_t retval = bits[i] >> shift;
463 	if (size + shift > 32)
464 		retval |= bits[i - 1] << (32 - shift);
465 	return retval & ((1 << size) - 1);
466 }
467 
468 static void
469 mmc_decode_cid(int is_sd, uint32_t *raw_cid, struct mmc_cid *cid)
470 {
471 	int i;
472 
473 	memset(cid, 0, sizeof(*cid));
474 	if (is_sd) {
475 		/* There's no version info, so we take it on faith */
476 		cid->mid = mmc_get_bits(raw_cid, 120, 8);
477 		cid->oid = mmc_get_bits(raw_cid, 104, 16);
478 		for (i = 0; i < 5; i++)
479 			cid->pnm[i] = mmc_get_bits(raw_cid, 96 - i * 8, 8);
480 		cid->prv = mmc_get_bits(raw_cid, 56, 8);
481 		cid->psn = mmc_get_bits(raw_cid, 24, 32);
482 		cid->mdt_year = mmc_get_bits(raw_cid, 12, 8) + 2001;
483 		cid->mdt_month = mmc_get_bits(raw_cid, 8, 4);
484 	} else {
485 		// XXX write me
486 		panic("write mmc cid decoder");
487 	}
488 }
489 
490 static const int exp[8] = {
491 	1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
492 };
493 static const int mant[16] = {
494 	10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
495 };
496 static const int cur_min[8] = {
497 	500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
498 };
499 static const int cur_max[8] = {
500 	1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
501 };
502 
503 static void
504 mmc_decode_csd(int is_sd, uint32_t *raw_csd, struct mmc_csd *csd)
505 {
506 	int v;
507 	int m;
508 	int e;
509 
510 	memset(csd, 0, sizeof(*csd));
511 	if (is_sd) {
512 		csd->csd_structure = v = mmc_get_bits(raw_csd, 126, 2);
513 		if (v == 0) {
514 			m = mmc_get_bits(raw_csd, 115, 4);
515 			e = mmc_get_bits(raw_csd, 112, 3);
516 			csd->tacc = exp[e] * mant[m] + 9 / 10;
517 			csd->nsac = mmc_get_bits(raw_csd, 104, 8) * 100;
518 			m = mmc_get_bits(raw_csd, 99, 4);
519 			e = mmc_get_bits(raw_csd, 96, 3);
520 			csd->tran_speed = exp[e] * 10000 * mant[m];
521 			csd->ccc = mmc_get_bits(raw_csd, 84, 12);
522 			csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 80, 4);
523 			csd->read_bl_partial = mmc_get_bits(raw_csd, 79, 1);
524 			csd->write_blk_misalign = mmc_get_bits(raw_csd, 78, 1);
525 			csd->read_blk_misalign = mmc_get_bits(raw_csd, 77, 1);
526 			csd->dsr_imp = mmc_get_bits(raw_csd, 76, 1);
527 			csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 59, 3)];
528 			csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 56, 3)];
529 			csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 53, 3)];
530 			csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 50, 3)];
531 			m = mmc_get_bits(raw_csd, 62, 12);
532 			e = mmc_get_bits(raw_csd, 47, 3);
533 			csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
534 			csd->erase_blk_en = mmc_get_bits(raw_csd, 46, 1);
535 			csd->sector_size = mmc_get_bits(raw_csd, 39, 7);
536 			csd->wp_grp_size = mmc_get_bits(raw_csd, 32, 7);
537 			csd->wp_grp_enable = mmc_get_bits(raw_csd, 31, 1);
538 			csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 26, 3);
539 			csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 22, 4);
540 			csd->write_bl_partial = mmc_get_bits(raw_csd, 21, 1);
541 		} else if (v == 1) {
542 			panic("Write SDHC CSD parser");
543 		} else
544 			panic("unknown SD CSD version");
545 	} else {
546 		panic("Write a MMC CSD parser");
547 	}
548 }
549 
550 static int
551 mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
552 {
553 	struct mmc_command cmd;
554 	int err;
555 
556 	cmd.opcode = MMC_ALL_SEND_CID;
557 	cmd.arg = 0;
558 	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
559 	err = mmc_wait_for_cmd(sc, &cmd, 0);
560 	memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
561 	return (err);
562 }
563 
564 static int
565 mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcid)
566 {
567 	struct mmc_command cmd;
568 	int err;
569 
570 	cmd.opcode = MMC_SEND_CSD;
571 	cmd.arg = rca << 16;
572 	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
573 	err = mmc_wait_for_cmd(sc, &cmd, 0);
574 	memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
575 	return (err);
576 }
577 
578 static int
579 mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
580 {
581 	struct mmc_command cmd;
582 	int err;
583 
584 	cmd.opcode = SD_SEND_RELATIVE_ADDR;
585 	cmd.arg = 0;
586 	cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
587 	err = mmc_wait_for_cmd(sc, &cmd, 0);
588 	*resp = cmd.resp[0];
589 	return (err);
590 }
591 
592 static void
593 mmc_discover_cards(struct mmc_softc *sc)
594 {
595 	struct mmc_ivars *ivar;
596 	int err;
597 	uint32_t resp;
598 	device_t child;
599 
600 	while (1) {
601 		ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, M_WAITOK);
602 		if (!ivar)
603 			return;
604 		err = mmc_all_send_cid(sc, ivar->raw_cid);
605 		if (err == MMC_ERR_TIMEOUT)
606 			break;
607 		if (err != MMC_ERR_NONE) {
608 			printf("Error reading CID %d\n", err);
609 			break;
610 		}
611 		if (mmcbr_get_mode(sc->dev) == mode_sd) {
612 			ivar->mode = mode_sd;
613 			mmc_decode_cid(1, ivar->raw_cid, &ivar->cid);
614 			mmc_send_relative_addr(sc, &resp);
615 			ivar->rca = resp >> 16;
616 			// RO check
617 			mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
618 			mmc_decode_csd(1, ivar->raw_csd, &ivar->csd);
619 			printf("SD CARD: %lld bytes\n", (long long)
620 			    ivar->csd.capacity);
621 			child = device_add_child(sc->dev, NULL, -1);
622 			device_set_ivars(child, ivar);
623 			return;
624 		}
625 		panic("Write MMC card code here");
626 	}
627 	free(ivar, M_DEVBUF);
628 }
629 
630 static void
631 mmc_go_discovery(struct mmc_softc *sc)
632 {
633 	uint32_t ocr;
634 	device_t dev;
635 
636 	dev = sc->dev;
637 	if (mmcbr_get_power_mode(dev) != power_on) {
638 		// First, try SD modes
639 		mmcbr_set_mode(dev, mode_sd);
640 		mmc_power_up(sc);
641 		mmcbr_set_bus_mode(dev, pushpull);
642 		mmc_idle_cards(sc);
643 		if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
644 			// Failed, try MMC
645 			mmcbr_set_mode(dev, mode_mmc);
646 			if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE)
647 				return;	// Failed both, punt! XXX power down?
648 		}
649 		mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
650 		if (mmcbr_get_ocr(dev) != 0)
651 			mmc_idle_cards(sc);
652 	} else {
653 		mmcbr_set_bus_mode(dev, opendrain);
654 		mmcbr_set_clock(dev, mmcbr_get_f_min(dev));
655 		mmcbr_update_ios(dev);
656 		// XXX recompute vdd based on new cards?
657 	}
658 	/*
659 	 * Make sure that we have a mutually agreeable voltage to at least
660 	 * one card on the bus.
661 	 */
662 	if (mmcbr_get_ocr(dev) == 0)
663 		return;
664 	/*
665 	 * Reselect the cards after we've idled them above.
666 	 */
667 	if (mmcbr_get_mode(dev) == mode_sd)
668 		mmc_send_app_op_cond(sc, mmcbr_get_ocr(dev), NULL);
669 	else
670 		mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
671 	mmc_discover_cards(sc);
672 
673 	mmcbr_set_bus_mode(dev, pushpull);
674 	mmcbr_update_ios(dev);
675 	bus_generic_attach(dev);
676 //	mmc_update_children_sysctl(dev);
677 }
678 
679 static int
680 mmc_calculate_clock(struct mmc_softc *sc)
681 {
682 	int max_dtr = 0;
683 	int nkid, i, f_min, f_max;
684 	device_t *kids;
685 
686 	f_min = mmcbr_get_f_min(sc->dev);
687 	f_max = mmcbr_get_f_max(sc->dev);
688 	max_dtr = f_max;
689 	if (device_get_children(sc->dev, &kids, &nkid) != 0)
690 		panic("can't get children");
691 	for (i = 0; i < nkid; i++)
692 		if (mmc_get_tran_speed(kids[i]) < max_dtr)
693 			max_dtr = mmc_get_tran_speed(kids[i]);
694 	free(kids, M_TEMP);
695 	device_printf(sc->dev, "setting transfer rate to %d.%03dMHz\n",
696 	    max_dtr / 1000000, (max_dtr / 1000) % 1000);
697 	return max_dtr;
698 }
699 
700 static void
701 mmc_scan(struct mmc_softc *sc)
702 {
703 	device_t dev;
704 
705 	dev = sc->dev;
706 	mmc_acquire_bus(dev, dev);
707 
708 	if (mmcbr_get_power_mode(dev) == power_on)
709 		mmc_rescan_cards(sc);
710 	mmc_go_discovery(sc);
711 	mmcbr_set_clock(dev, mmc_calculate_clock(sc));
712 	mmcbr_update_ios(dev);
713 
714 	mmc_release_bus(dev, dev);
715 	// XXX probe/attach/detach children?
716 }
717 
718 static int
719 mmc_read_ivar(device_t bus, device_t child, int which, u_char *result)
720 {
721 	struct mmc_ivars *ivar = device_get_ivars(child);
722 
723 	switch (which) {
724 	default:
725 		return (EINVAL);
726 	case MMC_IVAR_DSR_IMP:
727 		*(int *)result = ivar->csd.dsr_imp;
728 		break;
729 	case MMC_IVAR_MEDIA_SIZE:
730 		*(int *)result = ivar->csd.capacity;
731 		break;
732 	case MMC_IVAR_RCA:
733 		*(int *)result = ivar->rca;
734 		break;
735 	case MMC_IVAR_SECTOR_SIZE:
736 		*(int *)result = 512;
737 		break;
738 	case MMC_IVAR_TRAN_SPEED:
739 		*(int *)result = ivar->csd.tran_speed;
740 		break;
741 	}
742 	return (0);
743 }
744 
745 static int
746 mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
747 {
748 	// None are writable ATM
749 	switch (which) {
750 	default:
751 		return (EINVAL);
752 	}
753 	return (0);
754 }
755 
756 
757 static void
758 mmc_delayed_attach(void *xsc)
759 {
760 	struct mmc_softc *sc = xsc;
761 
762 	mmc_scan(sc);
763 	config_intrhook_disestablish(&sc->config_intrhook);
764 }
765 
766 static device_method_t mmc_methods[] = {
767 	/* device_if */
768 	DEVMETHOD(device_probe, mmc_probe),
769 	DEVMETHOD(device_attach, mmc_attach),
770 	DEVMETHOD(device_detach, mmc_detach),
771 
772 	/* Bus interface */
773 	DEVMETHOD(bus_read_ivar, mmc_read_ivar),
774 	DEVMETHOD(bus_write_ivar, mmc_write_ivar),
775 
776 	/* MMC Bus interface */
777 	DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
778 	DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
779 	DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
780 
781 	{0, 0},
782 };
783 
784 static driver_t mmc_driver = {
785 	"mmc",
786 	mmc_methods,
787 	sizeof(struct mmc_softc),
788 };
789 static devclass_t mmc_devclass;
790 
791 
792 DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, 0, 0);
793 DRIVER_MODULE(mmc, sdh, mmc_driver, mmc_devclass, 0, 0);
794