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