xref: /freebsd/sys/dev/mmc/mmc.c (revision e9bb9da34d6de1e4974169495c355e2c8e0771fc)
1114b4164SWarner Losh /*-
2114b4164SWarner Losh  * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3114b4164SWarner Losh  * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4114b4164SWarner Losh  *
5114b4164SWarner Losh  * Redistribution and use in source and binary forms, with or without
6114b4164SWarner Losh  * modification, are permitted provided that the following conditions
7114b4164SWarner Losh  * are met:
8114b4164SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9114b4164SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10114b4164SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11114b4164SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12114b4164SWarner Losh  *    documentation and/or other materials provided with the distribution.
13114b4164SWarner Losh  *
14114b4164SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15114b4164SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16114b4164SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17114b4164SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18114b4164SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19114b4164SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20114b4164SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21114b4164SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22114b4164SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23114b4164SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2414eced72SWarner Losh  *
2514eced72SWarner Losh  * Portions of this software may have been developed with reference to
2614eced72SWarner Losh  * the SD Simplified Specification.  The following disclaimer may apply:
2714eced72SWarner Losh  *
2814eced72SWarner Losh  * The following conditions apply to the release of the simplified
2914eced72SWarner Losh  * specification ("Simplified Specification") by the SD Card Association and
3014eced72SWarner Losh  * the SD Group. The Simplified Specification is a subset of the complete SD
3114eced72SWarner Losh  * Specification which is owned by the SD Card Association and the SD
3214eced72SWarner Losh  * Group. This Simplified Specification is provided on a non-confidential
3314eced72SWarner Losh  * basis subject to the disclaimers below. Any implementation of the
3414eced72SWarner Losh  * Simplified Specification may require a license from the SD Card
3514eced72SWarner Losh  * Association, SD Group, SD-3C LLC or other third parties.
3614eced72SWarner Losh  *
3714eced72SWarner Losh  * Disclaimers:
3814eced72SWarner Losh  *
3914eced72SWarner Losh  * The information contained in the Simplified Specification is presented only
4014eced72SWarner Losh  * as a standard specification for SD Cards and SD Host/Ancillary products and
4114eced72SWarner Losh  * is provided "AS-IS" without any representations or warranties of any
4214eced72SWarner Losh  * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
4314eced72SWarner Losh  * Card Association for any damages, any infringements of patents or other
4414eced72SWarner Losh  * right of the SD Group, SD-3C LLC, the SD Card Association or any third
4514eced72SWarner Losh  * parties, which may result from its use. No license is granted by
4614eced72SWarner Losh  * implication, estoppel or otherwise under any patent or other rights of the
4714eced72SWarner Losh  * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
4814eced72SWarner Losh  * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
4914eced72SWarner Losh  * or the SD Card Association to disclose or distribute any technical
5014eced72SWarner Losh  * information, know-how or other confidential information to any third party.
51114b4164SWarner Losh  */
52114b4164SWarner Losh 
53114b4164SWarner Losh #include <sys/cdefs.h>
54114b4164SWarner Losh __FBSDID("$FreeBSD$");
55114b4164SWarner Losh 
56114b4164SWarner Losh #include <sys/param.h>
57114b4164SWarner Losh #include <sys/systm.h>
58114b4164SWarner Losh #include <sys/kernel.h>
59114b4164SWarner Losh #include <sys/malloc.h>
60114b4164SWarner Losh #include <sys/lock.h>
61114b4164SWarner Losh #include <sys/module.h>
62114b4164SWarner Losh #include <sys/mutex.h>
63114b4164SWarner Losh #include <sys/bus.h>
64114b4164SWarner Losh 
65114b4164SWarner Losh #include <dev/mmc/mmcreg.h>
66114b4164SWarner Losh #include <dev/mmc/mmcbrvar.h>
67114b4164SWarner Losh #include <dev/mmc/mmcvar.h>
68114b4164SWarner Losh #include "mmcbr_if.h"
69114b4164SWarner Losh #include "mmcbus_if.h"
70114b4164SWarner Losh 
71114b4164SWarner Losh struct mmc_softc {
72114b4164SWarner Losh 	device_t dev;
73114b4164SWarner Losh 	struct mtx sc_mtx;
74114b4164SWarner Losh 	struct intr_config_hook config_intrhook;
75114b4164SWarner Losh 	device_t owner;
76114b4164SWarner Losh 	uint32_t last_rca;
77114b4164SWarner Losh };
78114b4164SWarner Losh 
79114b4164SWarner Losh /*
80114b4164SWarner Losh  * Per-card data
81114b4164SWarner Losh  */
82114b4164SWarner Losh struct mmc_ivars {
83114b4164SWarner Losh 	uint32_t raw_cid[4];	/* Raw bits of the CID */
84114b4164SWarner Losh 	uint32_t raw_csd[4];	/* Raw bits of the CSD */
85114b4164SWarner Losh 	uint16_t rca;
86114b4164SWarner Losh 	enum mmc_card_mode mode;
87114b4164SWarner Losh 	struct mmc_cid cid;	/* cid decoded */
88114b4164SWarner Losh 	struct mmc_csd csd;	/* csd decoded */
8940fab6e7SWarner Losh 	u_char read_only;	/* True when the device is read-only */
90114b4164SWarner Losh };
91114b4164SWarner Losh 
92114b4164SWarner Losh #define CMD_RETRIES	3
93114b4164SWarner Losh 
94114b4164SWarner Losh /* bus entry points */
95114b4164SWarner Losh static int mmc_probe(device_t dev);
96114b4164SWarner Losh static int mmc_attach(device_t dev);
97114b4164SWarner Losh static int mmc_detach(device_t dev);
98114b4164SWarner Losh 
99114b4164SWarner Losh #define MMC_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
100114b4164SWarner Losh #define	MMC_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
101114b4164SWarner Losh #define MMC_LOCK_INIT(_sc)					\
102114b4164SWarner Losh 	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev),	\
103114b4164SWarner Losh 	    "mmc", MTX_DEF)
104114b4164SWarner Losh #define MMC_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
105114b4164SWarner Losh #define MMC_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED);
106114b4164SWarner Losh #define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
107114b4164SWarner Losh 
108114b4164SWarner Losh static void mmc_delayed_attach(void *);
1095d2f6510SWarner Losh static void mmc_power_down(struct mmc_softc *sc);
110114b4164SWarner Losh static int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
111114b4164SWarner Losh     int retries);
112114b4164SWarner Losh static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
113114b4164SWarner Losh     uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
114114b4164SWarner Losh 
115114b4164SWarner Losh static void
116114b4164SWarner Losh mmc_ms_delay(int ms)
117114b4164SWarner Losh {
118114b4164SWarner Losh 	DELAY(1000 * ms);	/* XXX BAD */
119114b4164SWarner Losh }
120114b4164SWarner Losh 
121114b4164SWarner Losh static int
122114b4164SWarner Losh mmc_probe(device_t dev)
123114b4164SWarner Losh {
124114b4164SWarner Losh 
1252703cdbbSWarner Losh 	device_set_desc(dev, "MMC/SD bus");
126114b4164SWarner Losh 	return (0);
127114b4164SWarner Losh }
128114b4164SWarner Losh 
129114b4164SWarner Losh static int
130114b4164SWarner Losh mmc_attach(device_t dev)
131114b4164SWarner Losh {
132114b4164SWarner Losh 	struct mmc_softc *sc;
133114b4164SWarner Losh 
134114b4164SWarner Losh 	sc = device_get_softc(dev);
135114b4164SWarner Losh 	sc->dev = dev;
136114b4164SWarner Losh 	MMC_LOCK_INIT(sc);
137114b4164SWarner Losh 
138114b4164SWarner Losh 	/* We'll probe and attach our children later, but before / mount */
139114b4164SWarner Losh 	sc->config_intrhook.ich_func = mmc_delayed_attach;
140114b4164SWarner Losh 	sc->config_intrhook.ich_arg = sc;
141114b4164SWarner Losh 	if (config_intrhook_establish(&sc->config_intrhook) != 0)
142114b4164SWarner Losh 		device_printf(dev, "config_intrhook_establish failed\n");
143114b4164SWarner Losh 	return (0);
144114b4164SWarner Losh }
145114b4164SWarner Losh 
146114b4164SWarner Losh static int
147114b4164SWarner Losh mmc_detach(device_t dev)
148114b4164SWarner Losh {
1490e1466acSWarner Losh 	struct mmc_softc *sc = device_get_softc(dev);
1500e1466acSWarner Losh 	device_t *kids;
1510e1466acSWarner Losh 	int i, nkid;
1520e1466acSWarner Losh 
1530e1466acSWarner Losh 	/* kill children [ph33r].  -sorbo */
1540e1466acSWarner Losh 	if (device_get_children(sc->dev, &kids, &nkid) != 0)
1550e1466acSWarner Losh 		return 0;
1560e1466acSWarner Losh 	for (i = 0; i < nkid; i++) {
1570e1466acSWarner Losh 		device_t kid = kids[i];
1580e1466acSWarner Losh 		void *ivar = device_get_ivars(kid);
1590e1466acSWarner Losh 
1600e1466acSWarner Losh 		device_detach(kid);
1610e1466acSWarner Losh 		device_delete_child(sc->dev, kid);
1620e1466acSWarner Losh 		free(ivar, M_DEVBUF);
1630e1466acSWarner Losh 	}
1640e1466acSWarner Losh 	free(kids, M_TEMP);
1655d2f6510SWarner Losh 	mmc_power_down(sc);
1660e1466acSWarner Losh 
1670e1466acSWarner Losh 	MMC_LOCK_DESTROY(sc);
1680e1466acSWarner Losh 
1690e1466acSWarner Losh 	return 0;
170114b4164SWarner Losh }
171114b4164SWarner Losh 
172114b4164SWarner Losh static int
173114b4164SWarner Losh mmc_acquire_bus(device_t busdev, device_t dev)
174114b4164SWarner Losh {
175114b4164SWarner Losh 	struct mmc_softc *sc;
176114b4164SWarner Losh 	int err;
177114b4164SWarner Losh 	int rca;
178114b4164SWarner Losh 
179e9bb9da3SWarner Losh 	err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
180114b4164SWarner Losh 	if (err)
181114b4164SWarner Losh 		return (err);
182114b4164SWarner Losh 	sc = device_get_softc(busdev);
183114b4164SWarner Losh 	MMC_LOCK(sc);
184114b4164SWarner Losh 	if (sc->owner)
185114b4164SWarner Losh 		panic("mmc: host bridge didn't seralize us.");
186114b4164SWarner Losh 	sc->owner = dev;
187114b4164SWarner Losh 	MMC_UNLOCK(sc);
188114b4164SWarner Losh 
189114b4164SWarner Losh 	if (busdev != dev) {
190114b4164SWarner Losh 		// Keep track of the last rca that we've selected.  If
191114b4164SWarner Losh 		// we're asked to do it again, don't.  We never unselect
192114b4164SWarner Losh 		// unless the bus code itself wants the mmc bus.
193114b4164SWarner Losh 		rca = mmc_get_rca(dev);
194114b4164SWarner Losh 		if (sc->last_rca != rca) {
195114b4164SWarner Losh 			mmc_wait_for_command(sc, MMC_SELECT_CARD, rca << 16,
196114b4164SWarner Losh 			    MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES);
197114b4164SWarner Losh 			sc->last_rca = rca;
198114b4164SWarner Losh 		}
199114b4164SWarner Losh 		// XXX should set bus width here?
200114b4164SWarner Losh 	} else {
201114b4164SWarner Losh 		// If there's a card selected, stand down.
202114b4164SWarner Losh 		if (sc->last_rca != 0) {
203114b4164SWarner Losh 			mmc_wait_for_command(sc, MMC_SELECT_CARD, 0,
204114b4164SWarner Losh 			    MMC_RSP_R1 | MMC_CMD_AC, NULL, CMD_RETRIES);
205114b4164SWarner Losh 			sc->last_rca = 0;
206114b4164SWarner Losh 		}
207114b4164SWarner Losh 		// XXX should set bus width here?
208114b4164SWarner Losh 	}
209114b4164SWarner Losh 
210114b4164SWarner Losh 	return (0);
211114b4164SWarner Losh }
212114b4164SWarner Losh 
213114b4164SWarner Losh static int
214114b4164SWarner Losh mmc_release_bus(device_t busdev, device_t dev)
215114b4164SWarner Losh {
216114b4164SWarner Losh 	struct mmc_softc *sc;
217114b4164SWarner Losh 	int err;
218114b4164SWarner Losh 
219114b4164SWarner Losh 	sc = device_get_softc(busdev);
220114b4164SWarner Losh 
221114b4164SWarner Losh 	MMC_LOCK(sc);
222114b4164SWarner Losh 	if (!sc->owner)
223114b4164SWarner Losh 		panic("mmc: releasing unowned bus.");
224114b4164SWarner Losh 	if (sc->owner != dev)
225114b4164SWarner Losh 		panic("mmc: you don't own the bus.  game over.");
226114b4164SWarner Losh 	MMC_UNLOCK(sc);
227e9bb9da3SWarner Losh 	err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
228114b4164SWarner Losh 	if (err)
229114b4164SWarner Losh 		return (err);
230114b4164SWarner Losh 	MMC_LOCK(sc);
231114b4164SWarner Losh 	sc->owner = NULL;
232114b4164SWarner Losh 	MMC_UNLOCK(sc);
233114b4164SWarner Losh 	return (0);
234114b4164SWarner Losh }
235114b4164SWarner Losh 
236114b4164SWarner Losh static void
237114b4164SWarner Losh mmc_rescan_cards(struct mmc_softc *sc)
238114b4164SWarner Losh {
239114b4164SWarner Losh 	/* XXX: Look at the children and see if they respond to status */
240114b4164SWarner Losh }
241114b4164SWarner Losh 
242114b4164SWarner Losh static uint32_t
243114b4164SWarner Losh mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
244114b4164SWarner Losh {
2458aaa15e2SWarner Losh 
2468aaa15e2SWarner Losh 	return ocr & MMC_OCR_VOLTAGE;
247114b4164SWarner Losh }
248114b4164SWarner Losh 
249114b4164SWarner Losh static int
250114b4164SWarner Losh mmc_highest_voltage(uint32_t ocr)
251114b4164SWarner Losh {
252114b4164SWarner Losh 	int i;
253114b4164SWarner Losh 
254114b4164SWarner Losh 	for (i = 30; i >= 0; i--)
255114b4164SWarner Losh 		if (ocr & (1 << i))
256114b4164SWarner Losh 			return i;
257114b4164SWarner Losh 	return (-1);
258114b4164SWarner Losh }
259114b4164SWarner Losh 
260114b4164SWarner Losh static void
261114b4164SWarner Losh mmc_wakeup(struct mmc_request *req)
262114b4164SWarner Losh {
263114b4164SWarner Losh 	struct mmc_softc *sc;
264114b4164SWarner Losh 
265114b4164SWarner Losh //	printf("Wakeup for req %p done_data %p\n", req, req->done_data);
266114b4164SWarner Losh 	sc = (struct mmc_softc *)req->done_data;
267114b4164SWarner Losh 	MMC_LOCK(sc);
268114b4164SWarner Losh 	req->flags |= MMC_REQ_DONE;
269114b4164SWarner Losh 	wakeup(req);
270114b4164SWarner Losh 	MMC_UNLOCK(sc);
271114b4164SWarner Losh }
272114b4164SWarner Losh 
273114b4164SWarner Losh static int
274114b4164SWarner Losh mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
275114b4164SWarner Losh {
276114b4164SWarner Losh 	int err;
277114b4164SWarner Losh 
278114b4164SWarner Losh 	req->done = mmc_wakeup;
279114b4164SWarner Losh 	req->done_data = sc;
280114b4164SWarner Losh //	printf("Submitting request %p sc %p\n", req, sc);
281114b4164SWarner Losh 	MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
282114b4164SWarner Losh 	MMC_LOCK(sc);
283114b4164SWarner Losh 	do {
284114b4164SWarner Losh 		err = msleep(req, &sc->sc_mtx, PZERO | PCATCH, "mmcreq",
285114b4164SWarner Losh 		    hz / 10);
286114b4164SWarner Losh 	} while (!(req->flags & MMC_REQ_DONE) && err == EAGAIN);
287114b4164SWarner Losh //	printf("Request %p done with error %d\n", req, err);
288114b4164SWarner Losh 	MMC_UNLOCK(sc);
289114b4164SWarner Losh 	return (err);
290114b4164SWarner Losh }
291114b4164SWarner Losh 
292114b4164SWarner Losh static int
293114b4164SWarner Losh mmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
294114b4164SWarner Losh {
295114b4164SWarner Losh 	struct mmc_softc *sc = device_get_softc(brdev);
296114b4164SWarner Losh 
297114b4164SWarner Losh 	return mmc_wait_for_req(sc, req);
298114b4164SWarner Losh }
299114b4164SWarner Losh 
300114b4164SWarner Losh static int
301114b4164SWarner Losh mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
302114b4164SWarner Losh {
303114b4164SWarner Losh 	struct mmc_request mreq;
304114b4164SWarner Losh 
305114b4164SWarner Losh 	memset(&mreq, 0, sizeof(mreq));
306114b4164SWarner Losh 	memset(cmd->resp, 0, sizeof(cmd->resp));
307114b4164SWarner Losh 	cmd->retries = retries;
308114b4164SWarner Losh 	cmd->data = NULL;
309114b4164SWarner Losh 	mreq.cmd = cmd;
310114b4164SWarner Losh //	printf("CMD: %x ARG %x\n", cmd->opcode, cmd->arg);
311114b4164SWarner Losh 	mmc_wait_for_req(sc, &mreq);
312114b4164SWarner Losh 	return (cmd->error);
313114b4164SWarner Losh }
314114b4164SWarner Losh 
315114b4164SWarner Losh static int
316114b4164SWarner Losh mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
317114b4164SWarner Losh     struct mmc_command *cmd, int retries)
318114b4164SWarner Losh {
319114b4164SWarner Losh 	struct mmc_command appcmd;
320114b4164SWarner Losh 	int err = MMC_ERR_NONE, i;
321114b4164SWarner Losh 
322114b4164SWarner Losh 	for (i = 0; i <= retries; i++) {
323114b4164SWarner Losh 		appcmd.opcode = MMC_APP_CMD;
324114b4164SWarner Losh 		appcmd.arg = rca << 16;
325114b4164SWarner Losh 		appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
326114b4164SWarner Losh 		mmc_wait_for_cmd(sc, &appcmd, 0);
327114b4164SWarner Losh 		err = appcmd.error;
328114b4164SWarner Losh 		if (err != MMC_ERR_NONE)
329114b4164SWarner Losh 			continue;
330114b4164SWarner Losh 		if (!(appcmd.resp[0] & R1_APP_CMD))
331114b4164SWarner Losh 			return MMC_ERR_FAILED;
332114b4164SWarner Losh 		mmc_wait_for_cmd(sc, cmd, 0);
333114b4164SWarner Losh 		err = cmd->error;
334114b4164SWarner Losh 		if (err == MMC_ERR_NONE)
335114b4164SWarner Losh 			break;
336114b4164SWarner Losh 	}
337114b4164SWarner Losh 	return (err);
338114b4164SWarner Losh }
339114b4164SWarner Losh 
340114b4164SWarner Losh static int
341114b4164SWarner Losh mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
342114b4164SWarner Losh     uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
343114b4164SWarner Losh {
344114b4164SWarner Losh 	struct mmc_command cmd;
345114b4164SWarner Losh 	int err;
346114b4164SWarner Losh 
347114b4164SWarner Losh 	memset(&cmd, 0, sizeof(cmd));
348114b4164SWarner Losh 	cmd.opcode = opcode;
349114b4164SWarner Losh 	cmd.arg = arg;
350114b4164SWarner Losh 	cmd.flags = flags;
351114b4164SWarner Losh 	err = mmc_wait_for_cmd(sc, &cmd, retries);
352114b4164SWarner Losh 	if (err)
353114b4164SWarner Losh 		return (err);
354114b4164SWarner Losh 	if (cmd.error)
355114b4164SWarner Losh 		return (cmd.error);
356114b4164SWarner Losh 	if (resp) {
357114b4164SWarner Losh 		if (flags & MMC_RSP_136)
358114b4164SWarner Losh 			memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
359114b4164SWarner Losh 		else
360114b4164SWarner Losh 			*resp = cmd.resp[0];
361114b4164SWarner Losh 	}
362114b4164SWarner Losh 	return (0);
363114b4164SWarner Losh }
364114b4164SWarner Losh 
365114b4164SWarner Losh static void
366114b4164SWarner Losh mmc_idle_cards(struct mmc_softc *sc)
367114b4164SWarner Losh {
368114b4164SWarner Losh 	device_t dev;
369114b4164SWarner Losh 	struct mmc_command cmd;
370114b4164SWarner Losh 
371114b4164SWarner Losh 	dev = sc->dev;
372114b4164SWarner Losh 	mmcbr_set_chip_select(dev, cs_high);
373114b4164SWarner Losh 	mmcbr_update_ios(dev);
374114b4164SWarner Losh 	mmc_ms_delay(1);
375114b4164SWarner Losh 
376114b4164SWarner Losh 	memset(&cmd, 0, sizeof(cmd));
377114b4164SWarner Losh 	cmd.opcode = MMC_GO_IDLE_STATE;
378114b4164SWarner Losh 	cmd.arg = 0;
379114b4164SWarner Losh 	cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
380114b4164SWarner Losh 	mmc_wait_for_cmd(sc, &cmd, 0);
381114b4164SWarner Losh 	mmc_ms_delay(1);
382114b4164SWarner Losh 
383114b4164SWarner Losh 	mmcbr_set_chip_select(dev, cs_dontcare);
384114b4164SWarner Losh 	mmcbr_update_ios(dev);
385114b4164SWarner Losh 	mmc_ms_delay(1);
386114b4164SWarner Losh }
387114b4164SWarner Losh 
388114b4164SWarner Losh static int
389114b4164SWarner Losh mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
390114b4164SWarner Losh {
391114b4164SWarner Losh 	struct mmc_command cmd;
392114b4164SWarner Losh 	int err = MMC_ERR_NONE, i;
393114b4164SWarner Losh 
394114b4164SWarner Losh 	memset(&cmd, 0, sizeof(cmd));
395114b4164SWarner Losh 	cmd.opcode = ACMD_SD_SEND_OP_COND;
396114b4164SWarner Losh 	cmd.arg = ocr;
397114b4164SWarner Losh 	cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
398114b4164SWarner Losh 
3996634080bSWarner Losh 	for (i = 0; i < 100; i++) {
400114b4164SWarner Losh 		err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
401114b4164SWarner Losh 		if (err != MMC_ERR_NONE)
402114b4164SWarner Losh 			break;
403114b4164SWarner Losh 		if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || ocr == 0)
404114b4164SWarner Losh 			break;
405114b4164SWarner Losh 		err = MMC_ERR_TIMEOUT;
406114b4164SWarner Losh 		mmc_ms_delay(10);
407114b4164SWarner Losh 	}
408114b4164SWarner Losh 	if (rocr && err == MMC_ERR_NONE)
409114b4164SWarner Losh 		*rocr = cmd.resp[0];
410114b4164SWarner Losh 	return err;
411114b4164SWarner Losh }
412114b4164SWarner Losh 
413114b4164SWarner Losh static int
414114b4164SWarner Losh mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
415114b4164SWarner Losh {
416114b4164SWarner Losh 	struct mmc_command cmd;
417114b4164SWarner Losh 	int err = MMC_ERR_NONE, i;
418114b4164SWarner Losh 
419114b4164SWarner Losh 	memset(&cmd, 0, sizeof(cmd));
420114b4164SWarner Losh 	cmd.opcode = MMC_SEND_OP_COND;
421114b4164SWarner Losh 	cmd.arg = ocr;
422114b4164SWarner Losh 	cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
423114b4164SWarner Losh 
424114b4164SWarner Losh 	for (i = 0; i < 100; i++) {
425114b4164SWarner Losh 		err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
426114b4164SWarner Losh 		if (err != MMC_ERR_NONE)
427114b4164SWarner Losh 			break;
428114b4164SWarner Losh 		if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) || ocr == 0)
429114b4164SWarner Losh 			break;
430114b4164SWarner Losh 		err = MMC_ERR_TIMEOUT;
431114b4164SWarner Losh 		mmc_ms_delay(10);
432114b4164SWarner Losh 	}
433114b4164SWarner Losh 	if (rocr && err == MMC_ERR_NONE)
434114b4164SWarner Losh 		*rocr = cmd.resp[0];
435114b4164SWarner Losh 	return err;
436114b4164SWarner Losh }
437114b4164SWarner Losh 
438114b4164SWarner Losh static void
439114b4164SWarner Losh mmc_power_up(struct mmc_softc *sc)
440114b4164SWarner Losh {
441114b4164SWarner Losh 	device_t dev;
442114b4164SWarner Losh 
443114b4164SWarner Losh 	dev = sc->dev;
444114b4164SWarner Losh 	mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
445114b4164SWarner Losh 	mmcbr_set_bus_mode(dev, opendrain);
446114b4164SWarner Losh 	mmcbr_set_chip_select(dev, cs_dontcare);
447114b4164SWarner Losh 	mmcbr_set_bus_width(dev, bus_width_1);
448114b4164SWarner Losh 	mmcbr_set_power_mode(dev, power_up);
449114b4164SWarner Losh 	mmcbr_set_clock(dev, 0);
450114b4164SWarner Losh 	mmcbr_update_ios(dev);
451114b4164SWarner Losh 	mmc_ms_delay(1);
452114b4164SWarner Losh 
453114b4164SWarner Losh 	mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev));
454114b4164SWarner Losh 	mmcbr_set_power_mode(dev, power_on);
455114b4164SWarner Losh 	mmcbr_update_ios(dev);
456114b4164SWarner Losh 	mmc_ms_delay(2);
457114b4164SWarner Losh }
458114b4164SWarner Losh 
4595d2f6510SWarner Losh static void
4605d2f6510SWarner Losh mmc_power_down(struct mmc_softc *sc)
4615d2f6510SWarner Losh {
4625d2f6510SWarner Losh 	device_t dev = sc->dev;
4635d2f6510SWarner Losh 
4645d2f6510SWarner Losh 	mmcbr_set_bus_mode(dev, opendrain);
4655d2f6510SWarner Losh 	mmcbr_set_chip_select(dev, cs_dontcare);
4665d2f6510SWarner Losh 	mmcbr_set_bus_width(dev, bus_width_1);
4675d2f6510SWarner Losh 	mmcbr_set_power_mode(dev, power_off);
4685d2f6510SWarner Losh 	mmcbr_set_clock(dev, 0);
4695d2f6510SWarner Losh 	mmcbr_update_ios(dev);
4705d2f6510SWarner Losh }
4715d2f6510SWarner Losh 
472114b4164SWarner Losh static uint32_t
473114b4164SWarner Losh mmc_get_bits(uint32_t *bits, int start, int size)
474114b4164SWarner Losh {
475114b4164SWarner Losh 	const int i = 3 - (start / 32);
476114b4164SWarner Losh 	const int shift = start & 31;
477114b4164SWarner Losh 	uint32_t retval = bits[i] >> shift;
478114b4164SWarner Losh 	if (size + shift > 32)
479114b4164SWarner Losh 		retval |= bits[i - 1] << (32 - shift);
480114b4164SWarner Losh 	return retval & ((1 << size) - 1);
481114b4164SWarner Losh }
482114b4164SWarner Losh 
483114b4164SWarner Losh static void
484114b4164SWarner Losh mmc_decode_cid(int is_sd, uint32_t *raw_cid, struct mmc_cid *cid)
485114b4164SWarner Losh {
486114b4164SWarner Losh 	int i;
487114b4164SWarner Losh 
488114b4164SWarner Losh 	memset(cid, 0, sizeof(*cid));
489114b4164SWarner Losh 	if (is_sd) {
490114b4164SWarner Losh 		/* There's no version info, so we take it on faith */
491114b4164SWarner Losh 		cid->mid = mmc_get_bits(raw_cid, 120, 8);
492114b4164SWarner Losh 		cid->oid = mmc_get_bits(raw_cid, 104, 16);
493114b4164SWarner Losh 		for (i = 0; i < 5; i++)
494114b4164SWarner Losh 			cid->pnm[i] = mmc_get_bits(raw_cid, 96 - i * 8, 8);
495114b4164SWarner Losh 		cid->prv = mmc_get_bits(raw_cid, 56, 8);
496114b4164SWarner Losh 		cid->psn = mmc_get_bits(raw_cid, 24, 32);
497114b4164SWarner Losh 		cid->mdt_year = mmc_get_bits(raw_cid, 12, 8) + 2001;
498114b4164SWarner Losh 		cid->mdt_month = mmc_get_bits(raw_cid, 8, 4);
499114b4164SWarner Losh 	} else {
500114b4164SWarner Losh 		// XXX write me
501114b4164SWarner Losh 		panic("write mmc cid decoder");
502114b4164SWarner Losh 	}
503114b4164SWarner Losh }
504114b4164SWarner Losh 
505114b4164SWarner Losh static const int exp[8] = {
506114b4164SWarner Losh 	1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
507114b4164SWarner Losh };
508114b4164SWarner Losh static const int mant[16] = {
509114b4164SWarner Losh 	10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
510114b4164SWarner Losh };
511114b4164SWarner Losh static const int cur_min[8] = {
512114b4164SWarner Losh 	500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
513114b4164SWarner Losh };
514114b4164SWarner Losh static const int cur_max[8] = {
515114b4164SWarner Losh 	1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
516114b4164SWarner Losh };
517114b4164SWarner Losh 
518114b4164SWarner Losh static void
519114b4164SWarner Losh mmc_decode_csd(int is_sd, uint32_t *raw_csd, struct mmc_csd *csd)
520114b4164SWarner Losh {
521114b4164SWarner Losh 	int v;
522114b4164SWarner Losh 	int m;
523114b4164SWarner Losh 	int e;
524114b4164SWarner Losh 
525114b4164SWarner Losh 	memset(csd, 0, sizeof(*csd));
526114b4164SWarner Losh 	if (is_sd) {
527114b4164SWarner Losh 		csd->csd_structure = v = mmc_get_bits(raw_csd, 126, 2);
528114b4164SWarner Losh 		if (v == 0) {
529114b4164SWarner Losh 			m = mmc_get_bits(raw_csd, 115, 4);
530114b4164SWarner Losh 			e = mmc_get_bits(raw_csd, 112, 3);
531114b4164SWarner Losh 			csd->tacc = exp[e] * mant[m] + 9 / 10;
532114b4164SWarner Losh 			csd->nsac = mmc_get_bits(raw_csd, 104, 8) * 100;
533114b4164SWarner Losh 			m = mmc_get_bits(raw_csd, 99, 4);
534114b4164SWarner Losh 			e = mmc_get_bits(raw_csd, 96, 3);
535114b4164SWarner Losh 			csd->tran_speed = exp[e] * 10000 * mant[m];
536114b4164SWarner Losh 			csd->ccc = mmc_get_bits(raw_csd, 84, 12);
537114b4164SWarner Losh 			csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 80, 4);
538114b4164SWarner Losh 			csd->read_bl_partial = mmc_get_bits(raw_csd, 79, 1);
539114b4164SWarner Losh 			csd->write_blk_misalign = mmc_get_bits(raw_csd, 78, 1);
540114b4164SWarner Losh 			csd->read_blk_misalign = mmc_get_bits(raw_csd, 77, 1);
541114b4164SWarner Losh 			csd->dsr_imp = mmc_get_bits(raw_csd, 76, 1);
542114b4164SWarner Losh 			csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 59, 3)];
543114b4164SWarner Losh 			csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 56, 3)];
544114b4164SWarner Losh 			csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 53, 3)];
545114b4164SWarner Losh 			csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 50, 3)];
546114b4164SWarner Losh 			m = mmc_get_bits(raw_csd, 62, 12);
547114b4164SWarner Losh 			e = mmc_get_bits(raw_csd, 47, 3);
548114b4164SWarner Losh 			csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
549114b4164SWarner Losh 			csd->erase_blk_en = mmc_get_bits(raw_csd, 46, 1);
550114b4164SWarner Losh 			csd->sector_size = mmc_get_bits(raw_csd, 39, 7);
551114b4164SWarner Losh 			csd->wp_grp_size = mmc_get_bits(raw_csd, 32, 7);
552114b4164SWarner Losh 			csd->wp_grp_enable = mmc_get_bits(raw_csd, 31, 1);
553114b4164SWarner Losh 			csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 26, 3);
554114b4164SWarner Losh 			csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 22, 4);
555114b4164SWarner Losh 			csd->write_bl_partial = mmc_get_bits(raw_csd, 21, 1);
556114b4164SWarner Losh 		} else if (v == 1) {
557114b4164SWarner Losh 			panic("Write SDHC CSD parser");
558114b4164SWarner Losh 		} else
559114b4164SWarner Losh 			panic("unknown SD CSD version");
560114b4164SWarner Losh 	} else {
561114b4164SWarner Losh 		panic("Write a MMC CSD parser");
562114b4164SWarner Losh 	}
563114b4164SWarner Losh }
564114b4164SWarner Losh 
565114b4164SWarner Losh static int
566114b4164SWarner Losh mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
567114b4164SWarner Losh {
568114b4164SWarner Losh 	struct mmc_command cmd;
569114b4164SWarner Losh 	int err;
570114b4164SWarner Losh 
571114b4164SWarner Losh 	cmd.opcode = MMC_ALL_SEND_CID;
572114b4164SWarner Losh 	cmd.arg = 0;
573114b4164SWarner Losh 	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
574114b4164SWarner Losh 	err = mmc_wait_for_cmd(sc, &cmd, 0);
575114b4164SWarner Losh 	memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
576114b4164SWarner Losh 	return (err);
577114b4164SWarner Losh }
578114b4164SWarner Losh 
579114b4164SWarner Losh static int
580114b4164SWarner Losh mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcid)
581114b4164SWarner Losh {
582114b4164SWarner Losh 	struct mmc_command cmd;
583114b4164SWarner Losh 	int err;
584114b4164SWarner Losh 
585114b4164SWarner Losh 	cmd.opcode = MMC_SEND_CSD;
586114b4164SWarner Losh 	cmd.arg = rca << 16;
587114b4164SWarner Losh 	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
588114b4164SWarner Losh 	err = mmc_wait_for_cmd(sc, &cmd, 0);
589114b4164SWarner Losh 	memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
590114b4164SWarner Losh 	return (err);
591114b4164SWarner Losh }
592114b4164SWarner Losh 
593114b4164SWarner Losh static int
594114b4164SWarner Losh mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
595114b4164SWarner Losh {
596114b4164SWarner Losh 	struct mmc_command cmd;
597114b4164SWarner Losh 	int err;
598114b4164SWarner Losh 
599114b4164SWarner Losh 	cmd.opcode = SD_SEND_RELATIVE_ADDR;
600114b4164SWarner Losh 	cmd.arg = 0;
601114b4164SWarner Losh 	cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
602114b4164SWarner Losh 	err = mmc_wait_for_cmd(sc, &cmd, 0);
603114b4164SWarner Losh 	*resp = cmd.resp[0];
604114b4164SWarner Losh 	return (err);
605114b4164SWarner Losh }
606114b4164SWarner Losh 
607114b4164SWarner Losh static void
608114b4164SWarner Losh mmc_discover_cards(struct mmc_softc *sc)
609114b4164SWarner Losh {
610114b4164SWarner Losh 	struct mmc_ivars *ivar;
611114b4164SWarner Losh 	int err;
612114b4164SWarner Losh 	uint32_t resp;
613114b4164SWarner Losh 	device_t child;
614114b4164SWarner Losh 
615114b4164SWarner Losh 	while (1) {
616114b4164SWarner Losh 		ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF, M_WAITOK);
6170e1466acSWarner Losh 		if (!ivar)
6180e1466acSWarner Losh 			return;
619114b4164SWarner Losh 		err = mmc_all_send_cid(sc, ivar->raw_cid);
620114b4164SWarner Losh 		if (err == MMC_ERR_TIMEOUT)
621114b4164SWarner Losh 			break;
622114b4164SWarner Losh 		if (err != MMC_ERR_NONE) {
623114b4164SWarner Losh 			printf("Error reading CID %d\n", err);
624114b4164SWarner Losh 			break;
625114b4164SWarner Losh 		}
626114b4164SWarner Losh 		if (mmcbr_get_mode(sc->dev) == mode_sd) {
627114b4164SWarner Losh 			ivar->mode = mode_sd;
628114b4164SWarner Losh 			mmc_decode_cid(1, ivar->raw_cid, &ivar->cid);
629114b4164SWarner Losh 			mmc_send_relative_addr(sc, &resp);
630114b4164SWarner Losh 			ivar->rca = resp >> 16;
63140fab6e7SWarner Losh 			if (mmcbr_get_ro(sc->dev))
63240fab6e7SWarner Losh 				ivar->read_only = 1;
633114b4164SWarner Losh 			mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
634114b4164SWarner Losh 			mmc_decode_csd(1, ivar->raw_csd, &ivar->csd);
6350e1466acSWarner Losh 			printf("SD CARD: %lld bytes\n", (long long)
6360e1466acSWarner Losh 			    ivar->csd.capacity);
637114b4164SWarner Losh 			child = device_add_child(sc->dev, NULL, -1);
638114b4164SWarner Losh 			device_set_ivars(child, ivar);
6390e1466acSWarner Losh 			return;
640114b4164SWarner Losh 		}
641114b4164SWarner Losh 		panic("Write MMC card code here");
642114b4164SWarner Losh 	}
6430e1466acSWarner Losh 	free(ivar, M_DEVBUF);
644114b4164SWarner Losh }
645114b4164SWarner Losh 
646114b4164SWarner Losh static void
647114b4164SWarner Losh mmc_go_discovery(struct mmc_softc *sc)
648114b4164SWarner Losh {
649114b4164SWarner Losh 	uint32_t ocr;
650114b4164SWarner Losh 	device_t dev;
651114b4164SWarner Losh 
652114b4164SWarner Losh 	dev = sc->dev;
653114b4164SWarner Losh 	if (mmcbr_get_power_mode(dev) != power_on) {
654114b4164SWarner Losh 		// First, try SD modes
655114b4164SWarner Losh 		mmcbr_set_mode(dev, mode_sd);
656114b4164SWarner Losh 		mmc_power_up(sc);
657114b4164SWarner Losh 		mmcbr_set_bus_mode(dev, pushpull);
658114b4164SWarner Losh 		mmc_idle_cards(sc);
659114b4164SWarner Losh 		if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
660114b4164SWarner Losh 			// Failed, try MMC
661114b4164SWarner Losh 			mmcbr_set_mode(dev, mode_mmc);
662114b4164SWarner Losh 			if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE)
663114b4164SWarner Losh 				return;	// Failed both, punt! XXX power down?
664114b4164SWarner Losh 		}
665114b4164SWarner Losh 		mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
666114b4164SWarner Losh 		if (mmcbr_get_ocr(dev) != 0)
667114b4164SWarner Losh 			mmc_idle_cards(sc);
668114b4164SWarner Losh 	} else {
669114b4164SWarner Losh 		mmcbr_set_bus_mode(dev, opendrain);
670114b4164SWarner Losh 		mmcbr_set_clock(dev, mmcbr_get_f_min(dev));
671114b4164SWarner Losh 		mmcbr_update_ios(dev);
672114b4164SWarner Losh 		// XXX recompute vdd based on new cards?
673114b4164SWarner Losh 	}
674114b4164SWarner Losh 	/*
675114b4164SWarner Losh 	 * Make sure that we have a mutually agreeable voltage to at least
676114b4164SWarner Losh 	 * one card on the bus.
677114b4164SWarner Losh 	 */
678114b4164SWarner Losh 	if (mmcbr_get_ocr(dev) == 0)
679114b4164SWarner Losh 		return;
680114b4164SWarner Losh 	/*
681114b4164SWarner Losh 	 * Reselect the cards after we've idled them above.
682114b4164SWarner Losh 	 */
683114b4164SWarner Losh 	if (mmcbr_get_mode(dev) == mode_sd)
684114b4164SWarner Losh 		mmc_send_app_op_cond(sc, mmcbr_get_ocr(dev), NULL);
685114b4164SWarner Losh 	else
686114b4164SWarner Losh 		mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
687114b4164SWarner Losh 	mmc_discover_cards(sc);
688114b4164SWarner Losh 
689114b4164SWarner Losh 	mmcbr_set_bus_mode(dev, pushpull);
690114b4164SWarner Losh 	mmcbr_update_ios(dev);
691114b4164SWarner Losh 	bus_generic_attach(dev);
692114b4164SWarner Losh //	mmc_update_children_sysctl(dev);
693114b4164SWarner Losh }
694114b4164SWarner Losh 
695114b4164SWarner Losh static int
696114b4164SWarner Losh mmc_calculate_clock(struct mmc_softc *sc)
697114b4164SWarner Losh {
698114b4164SWarner Losh 	int max_dtr = 0;
699114b4164SWarner Losh 	int nkid, i, f_min, f_max;
700114b4164SWarner Losh 	device_t *kids;
701114b4164SWarner Losh 
702114b4164SWarner Losh 	f_min = mmcbr_get_f_min(sc->dev);
703114b4164SWarner Losh 	f_max = mmcbr_get_f_max(sc->dev);
704114b4164SWarner Losh 	max_dtr = f_max;
705114b4164SWarner Losh 	if (device_get_children(sc->dev, &kids, &nkid) != 0)
706114b4164SWarner Losh 		panic("can't get children");
707114b4164SWarner Losh 	for (i = 0; i < nkid; i++)
708114b4164SWarner Losh 		if (mmc_get_tran_speed(kids[i]) < max_dtr)
709114b4164SWarner Losh 			max_dtr = mmc_get_tran_speed(kids[i]);
710114b4164SWarner Losh 	free(kids, M_TEMP);
711114b4164SWarner Losh 	device_printf(sc->dev, "setting transfer rate to %d.%03dMHz\n",
712114b4164SWarner Losh 	    max_dtr / 1000000, (max_dtr / 1000) % 1000);
713114b4164SWarner Losh 	return max_dtr;
714114b4164SWarner Losh }
715114b4164SWarner Losh 
716114b4164SWarner Losh static void
717114b4164SWarner Losh mmc_scan(struct mmc_softc *sc)
718114b4164SWarner Losh {
719114b4164SWarner Losh 	device_t dev;
720114b4164SWarner Losh 
721114b4164SWarner Losh 	dev = sc->dev;
722114b4164SWarner Losh 	mmc_acquire_bus(dev, dev);
723114b4164SWarner Losh 
724114b4164SWarner Losh 	if (mmcbr_get_power_mode(dev) == power_on)
725114b4164SWarner Losh 		mmc_rescan_cards(sc);
726114b4164SWarner Losh 	mmc_go_discovery(sc);
727114b4164SWarner Losh 	mmcbr_set_clock(dev, mmc_calculate_clock(sc));
728114b4164SWarner Losh 	mmcbr_update_ios(dev);
729114b4164SWarner Losh 
730114b4164SWarner Losh 	mmc_release_bus(dev, dev);
731114b4164SWarner Losh 	// XXX probe/attach/detach children?
732114b4164SWarner Losh }
733114b4164SWarner Losh 
734114b4164SWarner Losh static int
735114b4164SWarner Losh mmc_read_ivar(device_t bus, device_t child, int which, u_char *result)
736114b4164SWarner Losh {
737114b4164SWarner Losh 	struct mmc_ivars *ivar = device_get_ivars(child);
738114b4164SWarner Losh 
739114b4164SWarner Losh 	switch (which) {
740114b4164SWarner Losh 	default:
741114b4164SWarner Losh 		return (EINVAL);
742114b4164SWarner Losh 	case MMC_IVAR_DSR_IMP:
743114b4164SWarner Losh 		*(int *)result = ivar->csd.dsr_imp;
744114b4164SWarner Losh 		break;
745114b4164SWarner Losh 	case MMC_IVAR_MEDIA_SIZE:
746114b4164SWarner Losh 		*(int *)result = ivar->csd.capacity;
747114b4164SWarner Losh 		break;
748114b4164SWarner Losh 	case MMC_IVAR_RCA:
749114b4164SWarner Losh 		*(int *)result = ivar->rca;
750114b4164SWarner Losh 		break;
751114b4164SWarner Losh 	case MMC_IVAR_SECTOR_SIZE:
752114b4164SWarner Losh 		*(int *)result = 512;
753114b4164SWarner Losh 		break;
754114b4164SWarner Losh 	case MMC_IVAR_TRAN_SPEED:
755114b4164SWarner Losh 		*(int *)result = ivar->csd.tran_speed;
756114b4164SWarner Losh 		break;
75740fab6e7SWarner Losh 	case MMC_IVAR_READ_ONLY:
75840fab6e7SWarner Losh 		*(int *)result = ivar->read_only;
75940fab6e7SWarner Losh 		break;
760114b4164SWarner Losh 	}
761114b4164SWarner Losh 	return (0);
762114b4164SWarner Losh }
763114b4164SWarner Losh 
764114b4164SWarner Losh static int
765114b4164SWarner Losh mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
766114b4164SWarner Losh {
767114b4164SWarner Losh 	// None are writable ATM
768114b4164SWarner Losh 	switch (which) {
769114b4164SWarner Losh 	default:
770114b4164SWarner Losh 		return (EINVAL);
771114b4164SWarner Losh 	}
772114b4164SWarner Losh 	return (0);
773114b4164SWarner Losh }
774114b4164SWarner Losh 
775114b4164SWarner Losh 
776114b4164SWarner Losh static void
777114b4164SWarner Losh mmc_delayed_attach(void *xsc)
778114b4164SWarner Losh {
779114b4164SWarner Losh 	struct mmc_softc *sc = xsc;
780114b4164SWarner Losh 
781114b4164SWarner Losh 	mmc_scan(sc);
782114b4164SWarner Losh 	config_intrhook_disestablish(&sc->config_intrhook);
783114b4164SWarner Losh }
784114b4164SWarner Losh 
785114b4164SWarner Losh static device_method_t mmc_methods[] = {
786114b4164SWarner Losh 	/* device_if */
787114b4164SWarner Losh 	DEVMETHOD(device_probe, mmc_probe),
788114b4164SWarner Losh 	DEVMETHOD(device_attach, mmc_attach),
789114b4164SWarner Losh 	DEVMETHOD(device_detach, mmc_detach),
790114b4164SWarner Losh 
791114b4164SWarner Losh 	/* Bus interface */
792114b4164SWarner Losh 	DEVMETHOD(bus_read_ivar, mmc_read_ivar),
793114b4164SWarner Losh 	DEVMETHOD(bus_write_ivar, mmc_write_ivar),
794114b4164SWarner Losh 
795114b4164SWarner Losh 	/* MMC Bus interface */
796114b4164SWarner Losh 	DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
797114b4164SWarner Losh 	DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
798114b4164SWarner Losh 	DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
799114b4164SWarner Losh 
800114b4164SWarner Losh 	{0, 0},
801114b4164SWarner Losh };
802114b4164SWarner Losh 
803114b4164SWarner Losh static driver_t mmc_driver = {
804114b4164SWarner Losh 	"mmc",
805114b4164SWarner Losh 	mmc_methods,
806114b4164SWarner Losh 	sizeof(struct mmc_softc),
807114b4164SWarner Losh };
808114b4164SWarner Losh static devclass_t mmc_devclass;
809114b4164SWarner Losh 
810114b4164SWarner Losh 
811114b4164SWarner Losh DRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, 0, 0);
8120e1466acSWarner Losh DRIVER_MODULE(mmc, sdh, mmc_driver, mmc_devclass, 0, 0);
813