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