mmc.c revision 184452
1163516Simp/*-
2163516Simp * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3163516Simp * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4163516Simp *
5163516Simp * Redistribution and use in source and binary forms, with or without
6163516Simp * modification, are permitted provided that the following conditions
7163516Simp * are met:
8163516Simp * 1. Redistributions of source code must retain the above copyright
9163516Simp *    notice, this list of conditions and the following disclaimer.
10163516Simp * 2. Redistributions in binary form must reproduce the above copyright
11163516Simp *    notice, this list of conditions and the following disclaimer in the
12163516Simp *    documentation and/or other materials provided with the distribution.
13163516Simp *
14163516Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15163516Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16163516Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17163516Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18163516Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19163516Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20163516Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21163516Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22163516Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23163516Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24170002Simp *
25170002Simp * Portions of this software may have been developed with reference to
26170002Simp * the SD Simplified Specification.  The following disclaimer may apply:
27170002Simp *
28170002Simp * The following conditions apply to the release of the simplified
29170002Simp * specification ("Simplified Specification") by the SD Card Association and
30170002Simp * the SD Group. The Simplified Specification is a subset of the complete SD
31170002Simp * Specification which is owned by the SD Card Association and the SD
32170002Simp * Group. This Simplified Specification is provided on a non-confidential
33170002Simp * basis subject to the disclaimers below. Any implementation of the
34170002Simp * Simplified Specification may require a license from the SD Card
35170002Simp * Association, SD Group, SD-3C LLC or other third parties.
36170002Simp *
37170002Simp * Disclaimers:
38170002Simp *
39170002Simp * The information contained in the Simplified Specification is presented only
40170002Simp * as a standard specification for SD Cards and SD Host/Ancillary products and
41170002Simp * is provided "AS-IS" without any representations or warranties of any
42170002Simp * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43170002Simp * Card Association for any damages, any infringements of patents or other
44170002Simp * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45170002Simp * parties, which may result from its use. No license is granted by
46170002Simp * implication, estoppel or otherwise under any patent or other rights of the
47170002Simp * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48170002Simp * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49170002Simp * or the SD Card Association to disclose or distribute any technical
50170002Simp * information, know-how or other confidential information to any third party.
51163516Simp */
52163516Simp
53163516Simp#include <sys/cdefs.h>
54163516Simp__FBSDID("$FreeBSD: head/sys/dev/mmc/mmc.c 184452 2008-10-29 20:01:26Z mav $");
55163516Simp
56163516Simp#include <sys/param.h>
57163516Simp#include <sys/systm.h>
58163516Simp#include <sys/kernel.h>
59163516Simp#include <sys/malloc.h>
60163516Simp#include <sys/lock.h>
61163516Simp#include <sys/module.h>
62163516Simp#include <sys/mutex.h>
63163516Simp#include <sys/bus.h>
64183704Smav#include <sys/endian.h>
65163516Simp
66163516Simp#include <dev/mmc/mmcreg.h>
67163516Simp#include <dev/mmc/mmcbrvar.h>
68163516Simp#include <dev/mmc/mmcvar.h>
69163516Simp#include "mmcbr_if.h"
70163516Simp#include "mmcbus_if.h"
71163516Simp
72163516Simpstruct mmc_softc {
73163516Simp	device_t dev;
74163516Simp	struct mtx sc_mtx;
75163516Simp	struct intr_config_hook config_intrhook;
76163516Simp	device_t owner;
77163516Simp	uint32_t last_rca;
78163516Simp};
79163516Simp
80163516Simp/*
81163516Simp * Per-card data
82163516Simp */
83163516Simpstruct mmc_ivars {
84163516Simp	uint32_t raw_cid[4];	/* Raw bits of the CID */
85163516Simp	uint32_t raw_csd[4];	/* Raw bits of the CSD */
86183704Smav	uint32_t raw_scr[2];	/* Raw bits of the SCR */
87183704Smav	uint8_t raw_ext_csd[512];	/* Raw bits of the EXT_CSD */
88184033Smav	uint32_t raw_sd_status[16];	/* Raw bits of the SD_STATUS */
89163516Simp	uint16_t rca;
90163516Simp	enum mmc_card_mode mode;
91163516Simp	struct mmc_cid cid;	/* cid decoded */
92163516Simp	struct mmc_csd csd;	/* csd decoded */
93183704Smav	struct mmc_scr scr;	/* scr decoded */
94184033Smav	struct mmc_sd_status sd_status;	/* SD_STATUS decoded */
95183447Simp	u_char read_only;	/* True when the device is read-only */
96183704Smav	u_char bus_width;	/* Bus width to use */
97183704Smav	u_char timing;		/* Bus timing support */
98183731Smav	u_char high_cap;	/* High Capacity card (block addressed) */
99183731Smav	uint32_t sec_count;	/* Card capacity in 512byte blocks */
100183704Smav	uint32_t tran_speed;	/* Max speed in normal mode */
101183704Smav	uint32_t hs_tran_speed;	/* Max speed in high speed mode */
102184033Smav	uint32_t erase_sector;	/* Card native erase sector size */
103163516Simp};
104163516Simp
105163516Simp#define CMD_RETRIES	3
106163516Simp
107163516Simp/* bus entry points */
108163516Simpstatic int mmc_probe(device_t dev);
109163516Simpstatic int mmc_attach(device_t dev);
110163516Simpstatic int mmc_detach(device_t dev);
111163516Simp
112163516Simp#define MMC_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
113163516Simp#define	MMC_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
114183444Simp#define MMC_LOCK_INIT(_sc)					\
115183444Simp	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev),	\
116163516Simp	    "mmc", MTX_DEF)
117163516Simp#define MMC_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
118163516Simp#define MMC_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED);
119163516Simp#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
120163516Simp
121183763Smavstatic int mmc_calculate_clock(struct mmc_softc *sc);
122163516Simpstatic void mmc_delayed_attach(void *);
123183449Simpstatic void mmc_power_down(struct mmc_softc *sc);
124163516Simpstatic int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
125163516Simp    int retries);
126163516Simpstatic int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
127163516Simp    uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
128183704Smavstatic int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
129183763Smavstatic int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width);
130183704Smavstatic int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr);
131183704Smavstatic void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
132183704Smavstatic int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
133163516Simp
134163516Simpstatic void
135163516Simpmmc_ms_delay(int ms)
136163516Simp{
137163516Simp	DELAY(1000 * ms);	/* XXX BAD */
138163516Simp}
139163516Simp
140163516Simpstatic int
141163516Simpmmc_probe(device_t dev)
142163516Simp{
143163516Simp
144183445Simp	device_set_desc(dev, "MMC/SD bus");
145163516Simp	return (0);
146163516Simp}
147163516Simp
148163516Simpstatic int
149163516Simpmmc_attach(device_t dev)
150163516Simp{
151163516Simp	struct mmc_softc *sc;
152163516Simp
153163516Simp	sc = device_get_softc(dev);
154163516Simp	sc->dev = dev;
155163516Simp	MMC_LOCK_INIT(sc);
156163516Simp
157163516Simp	/* We'll probe and attach our children later, but before / mount */
158163516Simp	sc->config_intrhook.ich_func = mmc_delayed_attach;
159163516Simp	sc->config_intrhook.ich_arg = sc;
160163516Simp	if (config_intrhook_establish(&sc->config_intrhook) != 0)
161163516Simp		device_printf(dev, "config_intrhook_establish failed\n");
162163516Simp	return (0);
163163516Simp}
164163516Simp
165163516Simpstatic int
166163516Simpmmc_detach(device_t dev)
167163516Simp{
168169567Simp	struct mmc_softc *sc = device_get_softc(dev);
169169567Simp	device_t *kids;
170169567Simp	int i, nkid;
171169567Simp
172169567Simp	/* kill children [ph33r].  -sorbo */
173169567Simp	if (device_get_children(sc->dev, &kids, &nkid) != 0)
174183467Simp		return (0);
175169567Simp	for (i = 0; i < nkid; i++) {
176169567Simp		device_t kid = kids[i];
177169567Simp		void *ivar = device_get_ivars(kid);
178169567Simp
179169567Simp		device_detach(kid);
180169567Simp		device_delete_child(sc->dev, kid);
181169567Simp		free(ivar, M_DEVBUF);
182169567Simp	}
183169567Simp	free(kids, M_TEMP);
184183449Simp	mmc_power_down(sc);
185169567Simp
186169567Simp	MMC_LOCK_DESTROY(sc);
187169567Simp
188183467Simp	return (0);
189163516Simp}
190163516Simp
191163516Simpstatic int
192163516Simpmmc_acquire_bus(device_t busdev, device_t dev)
193163516Simp{
194163516Simp	struct mmc_softc *sc;
195183704Smav	struct mmc_ivars *ivar;
196163516Simp	int err;
197163516Simp	int rca;
198163516Simp
199183452Simp	err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
200163516Simp	if (err)
201163516Simp		return (err);
202163516Simp	sc = device_get_softc(busdev);
203163516Simp	MMC_LOCK(sc);
204163516Simp	if (sc->owner)
205163516Simp		panic("mmc: host bridge didn't seralize us.");
206163516Simp	sc->owner = dev;
207163516Simp	MMC_UNLOCK(sc);
208163516Simp
209163516Simp	if (busdev != dev) {
210183453Simp		/*
211183453Simp		 * Keep track of the last rca that we've selected.  If
212183453Simp		 * we're asked to do it again, don't.  We never
213183453Simp		 * unselect unless the bus code itself wants the mmc
214183453Simp		 * bus, and constantly reselecting causes problems.
215183453Simp		 */
216163516Simp		rca = mmc_get_rca(dev);
217163516Simp		if (sc->last_rca != rca) {
218183704Smav			mmc_select_card(sc, rca);
219163516Simp			sc->last_rca = rca;
220183704Smav			/* Prepare bus width for the new card. */
221183704Smav			ivar = device_get_ivars(dev);
222183763Smav			if (bootverbose) {
223183763Smav				device_printf(busdev,
224183763Smav				    "setting bus width to %d bits\n",
225183775Simp				    (ivar->bus_width == bus_width_4) ? 4 :
226183775Simp				    (ivar->bus_width == bus_width_8) ? 8 : 1);
227183763Smav			}
228183763Smav			mmc_set_card_bus_width(sc, rca, ivar->bus_width);
229183704Smav			mmcbr_set_bus_width(busdev, ivar->bus_width);
230183704Smav			mmcbr_update_ios(busdev);
231163516Simp		}
232163516Simp	} else {
233183453Simp		/*
234183453Simp		 * If there's a card selected, stand down.
235183453Simp		 */
236163516Simp		if (sc->last_rca != 0) {
237183704Smav			mmc_select_card(sc, 0);
238163516Simp			sc->last_rca = 0;
239163516Simp		}
240163516Simp	}
241163516Simp
242163516Simp	return (0);
243163516Simp}
244163516Simp
245163516Simpstatic int
246163516Simpmmc_release_bus(device_t busdev, device_t dev)
247163516Simp{
248163516Simp	struct mmc_softc *sc;
249163516Simp	int err;
250163516Simp
251163516Simp	sc = device_get_softc(busdev);
252163516Simp
253163516Simp	MMC_LOCK(sc);
254163516Simp	if (!sc->owner)
255163516Simp		panic("mmc: releasing unowned bus.");
256163516Simp	if (sc->owner != dev)
257163516Simp		panic("mmc: you don't own the bus.  game over.");
258163516Simp	MMC_UNLOCK(sc);
259183452Simp	err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
260163516Simp	if (err)
261163516Simp		return (err);
262163516Simp	MMC_LOCK(sc);
263163516Simp	sc->owner = NULL;
264163516Simp	MMC_UNLOCK(sc);
265163516Simp	return (0);
266163516Simp}
267163516Simp
268163516Simpstatic void
269163516Simpmmc_rescan_cards(struct mmc_softc *sc)
270163516Simp{
271163516Simp	/* XXX: Look at the children and see if they respond to status */
272163516Simp}
273163516Simp
274163516Simpstatic uint32_t
275163516Simpmmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
276163516Simp{
277183446Simp
278183467Simp	return (ocr & MMC_OCR_VOLTAGE);
279163516Simp}
280163516Simp
281163516Simpstatic int
282163516Simpmmc_highest_voltage(uint32_t ocr)
283163516Simp{
284163516Simp	int i;
285163516Simp
286163516Simp	for (i = 30; i >= 0; i--)
287163516Simp		if (ocr & (1 << i))
288183467Simp			return (i);
289163516Simp	return (-1);
290163516Simp}
291163516Simp
292163516Simpstatic void
293163516Simpmmc_wakeup(struct mmc_request *req)
294163516Simp{
295163516Simp	struct mmc_softc *sc;
296163516Simp
297183453Simp/*	printf("Wakeup for req %p done_data %p\n", req, req->done_data); */
298163516Simp	sc = (struct mmc_softc *)req->done_data;
299163516Simp	MMC_LOCK(sc);
300163516Simp	req->flags |= MMC_REQ_DONE;
301163516Simp	wakeup(req);
302163516Simp	MMC_UNLOCK(sc);
303163516Simp}
304163516Simp
305163516Simpstatic int
306163516Simpmmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
307163516Simp{
308163516Simp	int err;
309163516Simp
310163516Simp	req->done = mmc_wakeup;
311163516Simp	req->done_data = sc;
312183453Simp/*	printf("Submitting request %p sc %p\n", req, sc); */
313163516Simp	MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
314163516Simp	MMC_LOCK(sc);
315163516Simp	do {
316163516Simp		err = msleep(req, &sc->sc_mtx, PZERO | PCATCH, "mmcreq",
317163516Simp		    hz / 10);
318163516Simp	} while (!(req->flags & MMC_REQ_DONE) && err == EAGAIN);
319183453Simp/*	printf("Request %p done with error %d\n", req, err); */
320163516Simp	MMC_UNLOCK(sc);
321163516Simp	return (err);
322163516Simp}
323163516Simp
324163516Simpstatic int
325163516Simpmmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
326163516Simp{
327163516Simp	struct mmc_softc *sc = device_get_softc(brdev);
328163516Simp
329183467Simp	return (mmc_wait_for_req(sc, req));
330163516Simp}
331163516Simp
332163516Simpstatic int
333163516Simpmmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
334163516Simp{
335163516Simp	struct mmc_request mreq;
336163516Simp
337163516Simp	memset(&mreq, 0, sizeof(mreq));
338163516Simp	memset(cmd->resp, 0, sizeof(cmd->resp));
339163516Simp	cmd->retries = retries;
340163516Simp	mreq.cmd = cmd;
341183453Simp/*	printf("CMD: %x ARG %x\n", cmd->opcode, cmd->arg); */
342163516Simp	mmc_wait_for_req(sc, &mreq);
343163516Simp	return (cmd->error);
344163516Simp}
345163516Simp
346163516Simpstatic int
347163516Simpmmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
348163516Simp    struct mmc_command *cmd, int retries)
349163516Simp{
350163516Simp	struct mmc_command appcmd;
351163516Simp	int err = MMC_ERR_NONE, i;
352163516Simp
353163516Simp	for (i = 0; i <= retries; i++) {
354163516Simp		appcmd.opcode = MMC_APP_CMD;
355163516Simp		appcmd.arg = rca << 16;
356163516Simp		appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
357183470Simp		appcmd.data = NULL;
358163516Simp		mmc_wait_for_cmd(sc, &appcmd, 0);
359163516Simp		err = appcmd.error;
360163516Simp		if (err != MMC_ERR_NONE)
361163516Simp			continue;
362163516Simp		if (!(appcmd.resp[0] & R1_APP_CMD))
363163516Simp			return MMC_ERR_FAILED;
364163516Simp		mmc_wait_for_cmd(sc, cmd, 0);
365163516Simp		err = cmd->error;
366163516Simp		if (err == MMC_ERR_NONE)
367163516Simp			break;
368163516Simp	}
369163516Simp	return (err);
370163516Simp}
371163516Simp
372163516Simpstatic int
373163516Simpmmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
374163516Simp    uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
375163516Simp{
376163516Simp	struct mmc_command cmd;
377163516Simp	int err;
378163516Simp
379163516Simp	memset(&cmd, 0, sizeof(cmd));
380163516Simp	cmd.opcode = opcode;
381163516Simp	cmd.arg = arg;
382163516Simp	cmd.flags = flags;
383183470Simp	cmd.data = NULL;
384163516Simp	err = mmc_wait_for_cmd(sc, &cmd, retries);
385163516Simp	if (err)
386163516Simp		return (err);
387163516Simp	if (cmd.error)
388163516Simp		return (cmd.error);
389163516Simp	if (resp) {
390163516Simp		if (flags & MMC_RSP_136)
391163516Simp			memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
392163516Simp		else
393163516Simp			*resp = cmd.resp[0];
394163516Simp	}
395163516Simp	return (0);
396163516Simp}
397163516Simp
398163516Simpstatic void
399163516Simpmmc_idle_cards(struct mmc_softc *sc)
400163516Simp{
401163516Simp	device_t dev;
402163516Simp	struct mmc_command cmd;
403163516Simp
404163516Simp	dev = sc->dev;
405163516Simp	mmcbr_set_chip_select(dev, cs_high);
406163516Simp	mmcbr_update_ios(dev);
407163516Simp	mmc_ms_delay(1);
408163516Simp
409163516Simp	memset(&cmd, 0, sizeof(cmd));
410163516Simp	cmd.opcode = MMC_GO_IDLE_STATE;
411163516Simp	cmd.arg = 0;
412163516Simp	cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
413183470Simp	cmd.data = NULL;
414163516Simp	mmc_wait_for_cmd(sc, &cmd, 0);
415163516Simp	mmc_ms_delay(1);
416163516Simp
417163516Simp	mmcbr_set_chip_select(dev, cs_dontcare);
418163516Simp	mmcbr_update_ios(dev);
419163516Simp	mmc_ms_delay(1);
420163516Simp}
421163516Simp
422163516Simpstatic int
423163516Simpmmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
424163516Simp{
425163516Simp	struct mmc_command cmd;
426163516Simp	int err = MMC_ERR_NONE, i;
427163516Simp
428163516Simp	memset(&cmd, 0, sizeof(cmd));
429163516Simp	cmd.opcode = ACMD_SD_SEND_OP_COND;
430163516Simp	cmd.arg = ocr;
431163516Simp	cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
432183470Simp	cmd.data = NULL;
433163516Simp
434170337Simp	for (i = 0; i < 100; i++) {
435163516Simp		err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
436163516Simp		if (err != MMC_ERR_NONE)
437163516Simp			break;
438183709Smav		if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
439183709Smav		    (ocr & MMC_OCR_VOLTAGE) == 0)
440163516Simp			break;
441163516Simp		err = MMC_ERR_TIMEOUT;
442163516Simp		mmc_ms_delay(10);
443163516Simp	}
444163516Simp	if (rocr && err == MMC_ERR_NONE)
445163516Simp		*rocr = cmd.resp[0];
446183467Simp	return (err);
447163516Simp}
448163516Simp
449163516Simpstatic int
450163516Simpmmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
451163516Simp{
452163516Simp	struct mmc_command cmd;
453163516Simp	int err = MMC_ERR_NONE, i;
454163516Simp
455163516Simp	memset(&cmd, 0, sizeof(cmd));
456163516Simp	cmd.opcode = MMC_SEND_OP_COND;
457163516Simp	cmd.arg = ocr;
458163516Simp	cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
459183470Simp	cmd.data = NULL;
460163516Simp
461163516Simp	for (i = 0; i < 100; i++) {
462163516Simp		err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
463163516Simp		if (err != MMC_ERR_NONE)
464163516Simp			break;
465183709Smav		if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
466183709Smav		    (ocr & MMC_OCR_VOLTAGE) == 0)
467163516Simp			break;
468163516Simp		err = MMC_ERR_TIMEOUT;
469163516Simp		mmc_ms_delay(10);
470163516Simp	}
471163516Simp	if (rocr && err == MMC_ERR_NONE)
472163516Simp		*rocr = cmd.resp[0];
473183467Simp	return (err);
474163516Simp}
475163516Simp
476183704Smavstatic int
477183704Smavmmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
478183704Smav{
479183704Smav	struct mmc_command cmd;
480183704Smav	int err;
481183704Smav
482183704Smav	memset(&cmd, 0, sizeof(cmd));
483183704Smav	cmd.opcode = SD_SEND_IF_COND;
484183704Smav	cmd.arg = (vhs << 8) + 0xAA;
485183704Smav	cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
486183704Smav	cmd.data = NULL;
487183704Smav
488183704Smav	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
489183704Smav	return (err);
490183704Smav}
491183704Smav
492163516Simpstatic void
493163516Simpmmc_power_up(struct mmc_softc *sc)
494163516Simp{
495163516Simp	device_t dev;
496163516Simp
497163516Simp	dev = sc->dev;
498163516Simp	mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
499163516Simp	mmcbr_set_bus_mode(dev, opendrain);
500163516Simp	mmcbr_set_chip_select(dev, cs_dontcare);
501163516Simp	mmcbr_set_bus_width(dev, bus_width_1);
502163516Simp	mmcbr_set_power_mode(dev, power_up);
503163516Simp	mmcbr_set_clock(dev, 0);
504163516Simp	mmcbr_update_ios(dev);
505163516Simp	mmc_ms_delay(1);
506163516Simp
507163516Simp	mmcbr_set_clock(dev, mmcbr_get_f_min(sc->dev));
508183704Smav	mmcbr_set_timing(dev, bus_timing_normal);
509163516Simp	mmcbr_set_power_mode(dev, power_on);
510163516Simp	mmcbr_update_ios(dev);
511163516Simp	mmc_ms_delay(2);
512163516Simp}
513163516Simp
514183449Simpstatic void
515183449Simpmmc_power_down(struct mmc_softc *sc)
516183449Simp{
517183449Simp	device_t dev = sc->dev;
518183449Simp
519183449Simp	mmcbr_set_bus_mode(dev, opendrain);
520183449Simp	mmcbr_set_chip_select(dev, cs_dontcare);
521183449Simp	mmcbr_set_bus_width(dev, bus_width_1);
522183449Simp	mmcbr_set_power_mode(dev, power_off);
523183449Simp	mmcbr_set_clock(dev, 0);
524183704Smav	mmcbr_set_timing(dev, bus_timing_normal);
525183449Simp	mmcbr_update_ios(dev);
526183449Simp}
527183449Simp
528183704Smavstatic int
529183704Smavmmc_select_card(struct mmc_softc *sc, uint16_t rca)
530183704Smav{
531183775Simp	int flags;
532183775Simp
533183775Simp	flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
534183775Simp	return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
535183775Simp	    flags, NULL, CMD_RETRIES));
536183704Smav}
537183704Smav
538183704Smavstatic int
539183704Smavmmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
540183704Smav{
541183704Smav	struct mmc_command cmd;
542183704Smav	int err;
543183704Smav
544183704Smav	cmd.opcode = MMC_SWITCH_FUNC;
545183704Smav	cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
546183704Smav	    (index << 16) |
547183704Smav	    (value << 8) |
548183704Smav	    set;
549183704Smav	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
550183704Smav	cmd.data = NULL;
551183704Smav	err = mmc_wait_for_cmd(sc, &cmd, 0);
552183704Smav	return (err);
553183704Smav}
554183704Smav
555183704Smavstatic int
556183704Smavmmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value, uint8_t *res)
557183704Smav{
558183704Smav	int err;
559183704Smav	struct mmc_command cmd;
560183704Smav	struct mmc_data data;
561183704Smav
562183704Smav	memset(&cmd, 0, sizeof(struct mmc_command));
563183704Smav	memset(&data, 0, sizeof(struct mmc_data));
564183704Smav
565183704Smav	memset(res, 0, 64);
566183704Smav	cmd.opcode = SD_SWITCH_FUNC;
567183704Smav	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
568183704Smav	cmd.arg = mode << 31;
569183704Smav	cmd.arg |= 0x00FFFFFF;
570183705Smav	cmd.arg &= ~(0xF << (grp * 4));
571183705Smav	cmd.arg |= value << (grp * 4);
572183704Smav	cmd.data = &data;
573183704Smav
574183704Smav	data.data = res;
575183704Smav	data.len = 64;
576183704Smav	data.flags = MMC_DATA_READ;
577183704Smav
578183704Smav	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
579183704Smav	return (err);
580183704Smav}
581183704Smav
582183704Smavstatic int
583183763Smavmmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
584183704Smav{
585183704Smav	int err;
586183704Smav
587183704Smav	if (mmcbr_get_mode(sc->dev) == mode_sd) {
588183704Smav		struct mmc_command cmd;
589183704Smav
590183704Smav		memset(&cmd, 0, sizeof(struct mmc_command));
591183704Smav		cmd.opcode = ACMD_SET_BUS_WIDTH;
592183704Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
593183704Smav		switch (width) {
594183704Smav		case bus_width_1:
595183704Smav			cmd.arg = SD_BUS_WIDTH_1;
596183704Smav			break;
597183704Smav		case bus_width_4:
598183704Smav			cmd.arg = SD_BUS_WIDTH_4;
599183704Smav			break;
600183704Smav		default:
601183704Smav			return (MMC_ERR_INVALID);
602183704Smav		}
603183704Smav		err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
604183704Smav	} else {
605183704Smav		uint8_t	value;
606183704Smav
607183704Smav		switch (width) {
608183704Smav		case bus_width_1:
609183704Smav			value = EXT_CSD_BUS_WIDTH_1;
610183704Smav			break;
611183704Smav		case bus_width_4:
612183704Smav			value = EXT_CSD_BUS_WIDTH_4;
613183704Smav			break;
614183704Smav		case bus_width_8:
615183704Smav			value = EXT_CSD_BUS_WIDTH_8;
616183704Smav			break;
617183704Smav		default:
618183704Smav			return (MMC_ERR_INVALID);
619183704Smav		}
620183704Smav		err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, value);
621183704Smav	}
622183704Smav	return (err);
623183704Smav}
624183704Smav
625183704Smavstatic int
626183704Smavmmc_set_timing(struct mmc_softc *sc, int timing)
627183704Smav{
628183704Smav	int err;
629183704Smav	uint8_t	value;
630183704Smav
631183704Smav	switch (timing) {
632183704Smav	case bus_timing_normal:
633183704Smav		value = 0;
634183704Smav		break;
635183704Smav	case bus_timing_hs:
636183704Smav		value = 1;
637183704Smav		break;
638183704Smav	default:
639183704Smav		return (MMC_ERR_INVALID);
640183704Smav	}
641183704Smav	if (mmcbr_get_mode(sc->dev) == mode_sd) {
642183704Smav		u_char switch_res[64];
643183704Smav
644183704Smav		err = mmc_sd_switch(sc, 1, 0, value, switch_res);
645183704Smav	} else {
646183704Smav		err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
647183704Smav		    EXT_CSD_HS_TIMING, value);
648183704Smav	}
649183704Smav	return (err);
650183704Smav}
651183704Smav
652183704Smavstatic int
653183704Smavmmc_test_bus_width(struct mmc_softc *sc)
654183704Smav{
655183704Smav	struct mmc_command cmd;
656183704Smav	struct mmc_data data;
657183704Smav	int err;
658183704Smav	uint8_t buf[8];
659183704Smav	uint8_t	p8[8] =   { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
660183704Smav	uint8_t	p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
661183704Smav	uint8_t	p4[4] =   { 0x5A, 0x00, 0x00, 0x00, };
662183704Smav	uint8_t	p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
663183704Smav
664183704Smav	if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
665183704Smav		mmcbr_set_bus_width(sc->dev, bus_width_8);
666183704Smav		mmcbr_update_ios(sc->dev);
667183704Smav
668183704Smav		cmd.opcode = MMC_BUSTEST_W;
669183704Smav		cmd.arg = 0;
670183704Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
671183704Smav		cmd.data = &data;
672183704Smav
673183704Smav		data.data = p8;
674183704Smav		data.len = 8;
675183704Smav		data.flags = MMC_DATA_WRITE;
676183704Smav		mmc_wait_for_cmd(sc, &cmd, 0);
677183704Smav
678183704Smav		cmd.opcode = MMC_BUSTEST_R;
679183704Smav		cmd.arg = 0;
680183704Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
681183704Smav		cmd.data = &data;
682183704Smav
683183704Smav		data.data = buf;
684183704Smav		data.len = 8;
685183704Smav		data.flags = MMC_DATA_READ;
686183704Smav		err = mmc_wait_for_cmd(sc, &cmd, 0);
687183704Smav
688183704Smav		mmcbr_set_bus_width(sc->dev, bus_width_1);
689183704Smav		mmcbr_update_ios(sc->dev);
690183704Smav
691183704Smav		if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
692183704Smav			return (bus_width_8);
693183704Smav	}
694183704Smav
695183704Smav	if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
696183704Smav		mmcbr_set_bus_width(sc->dev, bus_width_4);
697183704Smav		mmcbr_update_ios(sc->dev);
698183704Smav
699183704Smav		cmd.opcode = MMC_BUSTEST_W;
700183704Smav		cmd.arg = 0;
701183704Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
702183704Smav		cmd.data = &data;
703183704Smav
704183704Smav		data.data = p4;
705183704Smav		data.len = 4;
706183704Smav		data.flags = MMC_DATA_WRITE;
707183704Smav		mmc_wait_for_cmd(sc, &cmd, 0);
708183704Smav
709183704Smav		cmd.opcode = MMC_BUSTEST_R;
710183704Smav		cmd.arg = 0;
711183704Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
712183704Smav		cmd.data = &data;
713183704Smav
714183704Smav		data.data = buf;
715183704Smav		data.len = 4;
716183704Smav		data.flags = MMC_DATA_READ;
717183704Smav		err = mmc_wait_for_cmd(sc, &cmd, 0);
718183704Smav
719183704Smav		mmcbr_set_bus_width(sc->dev, bus_width_1);
720183704Smav		mmcbr_update_ios(sc->dev);
721183704Smav
722183704Smav		if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
723183704Smav			return (bus_width_4);
724183704Smav	}
725183704Smav	return (bus_width_1);
726183704Smav}
727183704Smav
728163516Simpstatic uint32_t
729184033Smavmmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
730163516Simp{
731183729Simp	const int i = (bit_len / 32) - (start / 32) - 1;
732163516Simp	const int shift = start & 31;
733163516Simp	uint32_t retval = bits[i] >> shift;
734163516Simp	if (size + shift > 32)
735163516Simp		retval |= bits[i - 1] << (32 - shift);
736183467Simp	return (retval & ((1 << size) - 1));
737163516Simp}
738163516Simp
739163516Simpstatic void
740183729Simpmmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
741163516Simp{
742163516Simp	int i;
743163516Simp
744183729Simp	/* There's no version info, so we take it on faith */
745163516Simp	memset(cid, 0, sizeof(*cid));
746184033Smav	cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
747184033Smav	cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
748183729Simp	for (i = 0; i < 5; i++)
749184033Smav		cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
750184033Smav	cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
751184033Smav	cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
752184033Smav	cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2001;
753184033Smav	cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
754163516Simp}
755163516Simp
756183729Simpstatic void
757183729Simpmmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
758183729Simp{
759183729Simp	int i;
760183729Simp
761183729Simp	/* There's no version info, so we take it on faith */
762183729Simp	memset(cid, 0, sizeof(*cid));
763184033Smav	cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
764184033Smav	cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
765183729Simp	for (i = 0; i < 6; i++)
766184033Smav		cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
767184033Smav	cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
768184033Smav	cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
769184033Smav	cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
770184033Smav	cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
771183729Simp}
772183729Simp
773163516Simpstatic const int exp[8] = {
774163516Simp	1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
775163516Simp};
776163516Simpstatic const int mant[16] = {
777163516Simp	10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
778163516Simp};
779163516Simpstatic const int cur_min[8] = {
780163516Simp	500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
781163516Simp};
782163516Simpstatic const int cur_max[8] = {
783163516Simp	1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
784163516Simp};
785163516Simp
786163516Simpstatic void
787183729Simpmmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
788163516Simp{
789163516Simp	int v;
790163516Simp	int m;
791163516Simp	int e;
792163516Simp
793163516Simp	memset(csd, 0, sizeof(*csd));
794184033Smav	csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
795183729Simp	if (v == 0) {
796184033Smav		m = mmc_get_bits(raw_csd, 128, 115, 4);
797184033Smav		e = mmc_get_bits(raw_csd, 128, 112, 3);
798183704Smav		csd->tacc = exp[e] * mant[m] + 9 / 10;
799184033Smav		csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
800184033Smav		m = mmc_get_bits(raw_csd, 128, 99, 4);
801184033Smav		e = mmc_get_bits(raw_csd, 128, 96, 3);
802183704Smav		csd->tran_speed = exp[e] * 10000 * mant[m];
803184033Smav		csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
804184033Smav		csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
805184033Smav		csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
806184033Smav		csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
807184033Smav		csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
808184033Smav		csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
809184033Smav		csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
810184033Smav		csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
811184033Smav		csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
812184033Smav		csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
813184033Smav		m = mmc_get_bits(raw_csd, 128, 62, 12);
814184033Smav		e = mmc_get_bits(raw_csd, 128, 47, 3);
815183704Smav		csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
816184033Smav		csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
817184033Smav		csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
818184033Smav		csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
819184033Smav		csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
820184033Smav		csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
821184033Smav		csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
822184033Smav		csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
823183729Simp	} else if (v == 1) {
824184033Smav		m = mmc_get_bits(raw_csd, 128, 115, 4);
825184033Smav		e = mmc_get_bits(raw_csd, 128, 112, 3);
826183729Simp		csd->tacc = exp[e] * mant[m] + 9 / 10;
827184033Smav		csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
828184033Smav		m = mmc_get_bits(raw_csd, 128, 99, 4);
829184033Smav		e = mmc_get_bits(raw_csd, 128, 96, 3);
830183729Simp		csd->tran_speed = exp[e] * 10000 * mant[m];
831184033Smav		csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
832184033Smav		csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
833184033Smav		csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
834184033Smav		csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
835184033Smav		csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
836184033Smav		csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
837184033Smav		csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
838183729Simp		    512 * 1024;
839184033Smav		csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
840184033Smav		csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
841184033Smav		csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
842184033Smav		csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
843184033Smav		csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
844184033Smav		csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
845184033Smav		csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
846183729Simp	} else
847183729Simp		panic("unknown SD CSD version");
848163516Simp}
849163516Simp
850183704Smavstatic void
851183729Simpmmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
852183729Simp{
853183729Simp	int m;
854183729Simp	int e;
855183729Simp
856183729Simp	memset(csd, 0, sizeof(*csd));
857184033Smav	csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
858184033Smav	csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
859184033Smav	m = mmc_get_bits(raw_csd, 128, 115, 4);
860184033Smav	e = mmc_get_bits(raw_csd, 128, 112, 3);
861183729Simp	csd->tacc = exp[e] * mant[m] + 9 / 10;
862184033Smav	csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
863184033Smav	m = mmc_get_bits(raw_csd, 128, 99, 4);
864184033Smav	e = mmc_get_bits(raw_csd, 128, 96, 3);
865183729Simp	csd->tran_speed = exp[e] * 10000 * mant[m];
866184033Smav	csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
867184033Smav	csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
868184033Smav	csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
869184033Smav	csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
870184033Smav	csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
871184033Smav	csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
872184033Smav	csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
873184033Smav	csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
874184033Smav	csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
875184033Smav	csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
876184033Smav	m = mmc_get_bits(raw_csd, 128, 62, 12);
877184033Smav	e = mmc_get_bits(raw_csd, 128, 47, 3);
878183729Simp	csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
879184033Smav	csd->erase_blk_en = 0;
880184033Smav	csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
881184033Smav	    (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
882184033Smav	csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
883184033Smav	csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
884184033Smav	csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
885184033Smav	csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
886184033Smav	csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
887183729Simp}
888183729Simp
889183729Simpstatic void
890183704Smavmmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
891183704Smav{
892183704Smav	unsigned int scr_struct;
893183704Smav
894183704Smav	memset(scr, 0, sizeof(*scr));
895183729Simp
896184033Smav	scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
897183704Smav	if (scr_struct != 0) {
898183704Smav		printf("Unrecognised SCR structure version %d\n",
899183704Smav		    scr_struct);
900183704Smav		return;
901183704Smav	}
902184033Smav	scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
903184033Smav	scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
904183704Smav}
905183704Smav
906184033Smavstatic void
907184033Smavmmc_app_decode_sd_status(uint32_t *raw_sd_status,
908184033Smav    struct mmc_sd_status *sd_status)
909184033Smav{
910184033Smav
911184033Smav	memset(sd_status, 0, sizeof(*sd_status));
912184033Smav
913184033Smav	sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
914184033Smav	sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
915184033Smav	sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
916184033Smav	sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
917184033Smav	sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
918184033Smav	sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
919184033Smav	sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
920184033Smav	sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
921184033Smav	sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
922184033Smav	sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
923184033Smav}
924184033Smav
925163516Simpstatic int
926163516Simpmmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
927163516Simp{
928163516Simp	struct mmc_command cmd;
929163516Simp	int err;
930163516Simp
931163516Simp	cmd.opcode = MMC_ALL_SEND_CID;
932163516Simp	cmd.arg = 0;
933163516Simp	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
934183470Simp	cmd.data = NULL;
935163516Simp	err = mmc_wait_for_cmd(sc, &cmd, 0);
936163516Simp	memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
937163516Simp	return (err);
938163516Simp}
939163516Simp
940163516Simpstatic int
941163516Simpmmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcid)
942163516Simp{
943163516Simp	struct mmc_command cmd;
944163516Simp	int err;
945163516Simp
946163516Simp	cmd.opcode = MMC_SEND_CSD;
947163516Simp	cmd.arg = rca << 16;
948163516Simp	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
949183470Simp	cmd.data = NULL;
950163516Simp	err = mmc_wait_for_cmd(sc, &cmd, 0);
951163516Simp	memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
952163516Simp	return (err);
953163516Simp}
954163516Simp
955163516Simpstatic int
956183704Smavmmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
957183704Smav{
958183704Smav	int err;
959183704Smav	struct mmc_command cmd;
960183704Smav	struct mmc_data data;
961183704Smav
962183704Smav	memset(&cmd, 0, sizeof(struct mmc_command));
963183704Smav	memset(&data, 0, sizeof(struct mmc_data));
964183704Smav
965183704Smav	memset(rawscr, 0, 8);
966183704Smav	cmd.opcode = ACMD_SEND_SCR;
967183704Smav	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
968183704Smav	cmd.arg = 0;
969183704Smav	cmd.data = &data;
970183704Smav
971183704Smav	data.data = rawscr;
972183704Smav	data.len = 8;
973183704Smav	data.flags = MMC_DATA_READ;
974183704Smav
975183704Smav	err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
976183704Smav	rawscr[0] = be32toh(rawscr[0]);
977183704Smav	rawscr[1] = be32toh(rawscr[1]);
978183704Smav	return (err);
979183704Smav}
980183704Smav
981183704Smavstatic int
982183704Smavmmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
983183704Smav{
984183704Smav	int err;
985183704Smav	struct mmc_command cmd;
986183704Smav	struct mmc_data data;
987183704Smav
988183704Smav	memset(&cmd, 0, sizeof(struct mmc_command));
989183704Smav	memset(&data, 0, sizeof(struct mmc_data));
990183704Smav
991183704Smav	memset(rawextcsd, 0, 512);
992183704Smav	cmd.opcode = MMC_SEND_EXT_CSD;
993183704Smav	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
994183704Smav	cmd.arg = 0;
995183704Smav	cmd.data = &data;
996183704Smav
997183704Smav	data.data = rawextcsd;
998183704Smav	data.len = 512;
999183704Smav	data.flags = MMC_DATA_READ;
1000183704Smav
1001183704Smav	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1002183704Smav	return (err);
1003183704Smav}
1004183704Smav
1005183704Smavstatic int
1006184033Smavmmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1007184033Smav{
1008184033Smav	int err, i;
1009184033Smav	struct mmc_command cmd;
1010184033Smav	struct mmc_data data;
1011184033Smav
1012184033Smav	memset(&cmd, 0, sizeof(struct mmc_command));
1013184033Smav	memset(&data, 0, sizeof(struct mmc_data));
1014184033Smav
1015184033Smav	memset(rawsdstatus, 0, 64);
1016184033Smav	cmd.opcode = ACMD_SD_STATUS;
1017184033Smav	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1018184033Smav	cmd.arg = 0;
1019184033Smav	cmd.data = &data;
1020184033Smav
1021184033Smav	data.data = rawsdstatus;
1022184033Smav	data.len = 64;
1023184033Smav	data.flags = MMC_DATA_READ;
1024184033Smav
1025184033Smav	err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1026184033Smav	for (i = 0; i < 16; i++)
1027184033Smav	    rawsdstatus[i] = be32toh(rawsdstatus[i]);
1028184033Smav	return (err);
1029184033Smav}
1030184033Smav
1031184033Smavstatic int
1032183704Smavmmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1033183704Smav{
1034183704Smav	struct mmc_command cmd;
1035183704Smav	int err;
1036183704Smav
1037183704Smav	cmd.opcode = MMC_SET_RELATIVE_ADDR;
1038183704Smav	cmd.arg = resp << 16;
1039183704Smav	cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1040183704Smav	cmd.data = NULL;
1041183704Smav	err = mmc_wait_for_cmd(sc, &cmd, 0);
1042183704Smav	return (err);
1043183704Smav}
1044183704Smav
1045183704Smavstatic int
1046163516Simpmmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1047163516Simp{
1048163516Simp	struct mmc_command cmd;
1049163516Simp	int err;
1050163516Simp
1051163516Simp	cmd.opcode = SD_SEND_RELATIVE_ADDR;
1052163516Simp	cmd.arg = 0;
1053163516Simp	cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1054183470Simp	cmd.data = NULL;
1055163516Simp	err = mmc_wait_for_cmd(sc, &cmd, 0);
1056163516Simp	*resp = cmd.resp[0];
1057163516Simp	return (err);
1058163516Simp}
1059163516Simp
1060163516Simpstatic void
1061163516Simpmmc_discover_cards(struct mmc_softc *sc)
1062163516Simp{
1063163516Simp	struct mmc_ivars *ivar;
1064163516Simp	int err;
1065183731Smav	uint32_t resp, sec_count;
1066163516Simp	device_t child;
1067183704Smav	uint16_t rca = 2;
1068183704Smav	u_char switch_res[64];
1069163516Simp
1070163516Simp	while (1) {
1071183468Simp		ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1072183468Simp		    M_WAITOK | M_ZERO);
1073169567Simp		if (!ivar)
1074169567Simp			return;
1075163516Simp		err = mmc_all_send_cid(sc, ivar->raw_cid);
1076163516Simp		if (err == MMC_ERR_TIMEOUT)
1077163516Simp			break;
1078163516Simp		if (err != MMC_ERR_NONE) {
1079183468Simp			device_printf(sc->dev, "Error reading CID %d\n", err);
1080163516Simp			break;
1081163516Simp		}
1082183704Smav		if (mmcbr_get_ro(sc->dev))
1083183704Smav			ivar->read_only = 1;
1084183704Smav		ivar->bus_width = bus_width_1;
1085183704Smav		ivar->mode = mmcbr_get_mode(sc->dev);
1086183704Smav		if (ivar->mode == mode_sd) {
1087183729Simp			mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1088163516Simp			mmc_send_relative_addr(sc, &resp);
1089163516Simp			ivar->rca = resp >> 16;
1090183704Smav			/* Get card CSD. */
1091163516Simp			mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1092183729Simp			mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1093183731Smav			ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1094183704Smav			if (ivar->csd.csd_structure > 0)
1095183704Smav				ivar->high_cap = 1;
1096183704Smav			ivar->tran_speed = ivar->csd.tran_speed;
1097184033Smav			ivar->erase_sector = ivar->csd.erase_sector *
1098184033Smav			    ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1099183704Smav			/* Get card SCR. Card must be selected to fetch it. */
1100183704Smav			mmc_select_card(sc, ivar->rca);
1101183704Smav			mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1102183704Smav			mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1103183704Smav			/* Get card switch capabilities. */
1104183704Smav			if ((ivar->scr.sda_vsn >= 1) &&
1105183704Smav			    (ivar->csd.ccc & (1<<10))) {
1106183704Smav				mmc_sd_switch(sc, 0, 0, 0xF, switch_res);
1107183704Smav				if (switch_res[13] & 2) {
1108183704Smav					ivar->timing = bus_timing_hs;
1109183704Smav					ivar->hs_tran_speed = 50000000;
1110183704Smav				}
1111183704Smav			}
1112184033Smav			mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1113184033Smav			mmc_app_decode_sd_status(ivar->raw_sd_status,
1114184033Smav			    &ivar->sd_status);
1115184033Smav			if (ivar->sd_status.au_size != 0) {
1116184033Smav				ivar->erase_sector =
1117184033Smav				    16 << ivar->sd_status.au_size;
1118184033Smav			}
1119183704Smav			mmc_select_card(sc, 0);
1120183704Smav			/* Find max supported bus width. */
1121183704Smav			if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1122183704Smav			    (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1123183704Smav				ivar->bus_width = bus_width_4;
1124183704Smav			/* Add device. */
1125163516Simp			child = device_add_child(sc->dev, NULL, -1);
1126163516Simp			device_set_ivars(child, ivar);
1127169567Simp			return;
1128163516Simp		}
1129183729Simp		mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1130183704Smav		ivar->rca = rca++;
1131183704Smav		mmc_set_relative_addr(sc, ivar->rca);
1132183704Smav		/* Get card CSD. */
1133183704Smav		mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1134183729Simp		mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1135183731Smav		ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1136183704Smav		ivar->tran_speed = ivar->csd.tran_speed;
1137184033Smav		ivar->erase_sector = ivar->csd.erase_sector *
1138184033Smav		    ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1139183704Smav		/* Only MMC >= 4.x cards support EXT_CSD. */
1140183704Smav		if (ivar->csd.spec_vers >= 4) {
1141183704Smav			/* Card must be selected to fetch EXT_CSD. */
1142183704Smav			mmc_select_card(sc, ivar->rca);
1143183704Smav			mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1144183731Smav			/* Handle extended capacity from EXT_CSD */
1145183731Smav			sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1146183731Smav			    (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1147183731Smav			    (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1148183731Smav			    (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1149183731Smav			if (sec_count != 0) {
1150183731Smav				ivar->sec_count = sec_count;
1151183731Smav				ivar->high_cap = 1;
1152183731Smav			}
1153183704Smav			/* Get card speed in high speed mode. */
1154183704Smav			ivar->timing = bus_timing_hs;
1155183731Smav			if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1156183704Smav			    & EXT_CSD_CARD_TYPE_52)
1157183704Smav				ivar->hs_tran_speed = 52000000;
1158183731Smav			else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1159183704Smav			    & EXT_CSD_CARD_TYPE_26)
1160183704Smav				ivar->hs_tran_speed = 26000000;
1161183704Smav			else
1162183704Smav				ivar->hs_tran_speed = ivar->tran_speed;
1163183704Smav			/* Find max supported bus width. */
1164183704Smav			ivar->bus_width = mmc_test_bus_width(sc);
1165183704Smav			mmc_select_card(sc, 0);
1166184033Smav			/* Handle HC erase sector size. */
1167184033Smav			if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1168184033Smav				ivar->erase_sector = 1024 *
1169184033Smav				    ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1170184033Smav				mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1171184033Smav				    EXT_CSD_ERASE_GRP_DEF, 1);
1172184033Smav			}
1173183704Smav		} else {
1174183704Smav			ivar->bus_width = bus_width_1;
1175183704Smav			ivar->timing = bus_timing_normal;
1176183704Smav		}
1177183704Smav		/* Add device. */
1178183704Smav		child = device_add_child(sc->dev, NULL, -1);
1179183704Smav		device_set_ivars(child, ivar);
1180163516Simp	}
1181169567Simp	free(ivar, M_DEVBUF);
1182163516Simp}
1183163516Simp
1184163516Simpstatic void
1185163516Simpmmc_go_discovery(struct mmc_softc *sc)
1186163516Simp{
1187163516Simp	uint32_t ocr;
1188163516Simp	device_t dev;
1189183704Smav	int err;
1190163516Simp
1191163516Simp	dev = sc->dev;
1192163516Simp	if (mmcbr_get_power_mode(dev) != power_on) {
1193183453Simp		/*
1194183453Simp		 * First, try SD modes
1195183453Simp		 */
1196163516Simp		mmcbr_set_mode(dev, mode_sd);
1197163516Simp		mmc_power_up(sc);
1198163516Simp		mmcbr_set_bus_mode(dev, pushpull);
1199163516Simp		mmc_idle_cards(sc);
1200183704Smav		err = mmc_send_if_cond(sc, 1);
1201183775Simp		if (mmc_send_app_op_cond(sc, err ? 0 : MMC_OCR_CCS, &ocr) !=
1202183704Smav		    MMC_ERR_NONE) {
1203183453Simp			/*
1204183453Simp			 * Failed, try MMC
1205183453Simp			 */
1206163516Simp			mmcbr_set_mode(dev, mode_mmc);
1207163516Simp			if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE)
1208183453Simp				return;	/* Failed both, punt! XXX powerdown? */
1209163516Simp		}
1210163516Simp		mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1211163516Simp		if (mmcbr_get_ocr(dev) != 0)
1212163516Simp			mmc_idle_cards(sc);
1213163516Simp	} else {
1214163516Simp		mmcbr_set_bus_mode(dev, opendrain);
1215163516Simp		mmcbr_set_clock(dev, mmcbr_get_f_min(dev));
1216163516Simp		mmcbr_update_ios(dev);
1217183453Simp		/* XXX recompute vdd based on new cards? */
1218163516Simp	}
1219163516Simp	/*
1220163516Simp	 * Make sure that we have a mutually agreeable voltage to at least
1221163516Simp	 * one card on the bus.
1222163516Simp	 */
1223163516Simp	if (mmcbr_get_ocr(dev) == 0)
1224163516Simp		return;
1225163516Simp	/*
1226163516Simp	 * Reselect the cards after we've idled them above.
1227163516Simp	 */
1228183704Smav	if (mmcbr_get_mode(dev) == mode_sd) {
1229183704Smav		err = mmc_send_if_cond(sc, 1);
1230183704Smav		mmc_send_app_op_cond(sc,
1231183775Simp		    (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1232183704Smav	} else
1233163516Simp		mmc_send_op_cond(sc, mmcbr_get_ocr(dev), NULL);
1234163516Simp	mmc_discover_cards(sc);
1235163516Simp
1236163516Simp	mmcbr_set_bus_mode(dev, pushpull);
1237163516Simp	mmcbr_update_ios(dev);
1238183763Smav	mmc_calculate_clock(sc);
1239163516Simp	bus_generic_attach(dev);
1240183453Simp/*	mmc_update_children_sysctl(dev);*/
1241163516Simp}
1242163516Simp
1243163516Simpstatic int
1244163516Simpmmc_calculate_clock(struct mmc_softc *sc)
1245163516Simp{
1246183704Smav	int max_dtr, max_hs_dtr, max_timing;
1247163516Simp	int nkid, i, f_min, f_max;
1248163516Simp	device_t *kids;
1249183704Smav	struct mmc_ivars *ivar;
1250163516Simp
1251163516Simp	f_min = mmcbr_get_f_min(sc->dev);
1252163516Simp	f_max = mmcbr_get_f_max(sc->dev);
1253183704Smav	max_dtr = max_hs_dtr = f_max;
1254183704Smav	if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1255183704Smav		max_timing = bus_timing_hs;
1256183704Smav	else
1257183704Smav		max_timing = bus_timing_normal;
1258163516Simp	if (device_get_children(sc->dev, &kids, &nkid) != 0)
1259163516Simp		panic("can't get children");
1260183704Smav	for (i = 0; i < nkid; i++) {
1261183704Smav		ivar = device_get_ivars(kids[i]);
1262183704Smav		if (ivar->timing < max_timing)
1263183704Smav			max_timing = ivar->timing;
1264183704Smav		if (ivar->tran_speed < max_dtr)
1265183704Smav			max_dtr = ivar->tran_speed;
1266183704Smav		if (ivar->hs_tran_speed < max_dtr)
1267183704Smav			max_hs_dtr = ivar->hs_tran_speed;
1268183704Smav	}
1269183704Smav	for (i = 0; i < nkid; i++) {
1270183704Smav		ivar = device_get_ivars(kids[i]);
1271183704Smav		if (ivar->timing == bus_timing_normal)
1272183704Smav			continue;
1273183704Smav		mmc_select_card(sc, ivar->rca);
1274183704Smav		mmc_set_timing(sc, max_timing);
1275183704Smav	}
1276183704Smav	mmc_select_card(sc, 0);
1277163516Simp	free(kids, M_TEMP);
1278183704Smav	if (max_timing == bus_timing_hs)
1279183704Smav		max_dtr = max_hs_dtr;
1280183763Smav	if (bootverbose) {
1281183775Simp		device_printf(sc->dev,
1282183775Simp		    "setting transfer rate to %d.%03dMHz%s\n",
1283183763Smav		    max_dtr / 1000000, (max_dtr / 1000) % 1000,
1284183775Simp		    max_timing == bus_timing_hs ? " (high speed timing)" : "");
1285183763Smav	}
1286183704Smav	mmcbr_set_timing(sc->dev, max_timing);
1287183704Smav	mmcbr_set_clock(sc->dev, max_dtr);
1288183704Smav	mmcbr_update_ios(sc->dev);
1289183704Smav	return max_dtr;
1290163516Simp}
1291163516Simp
1292163516Simpstatic void
1293163516Simpmmc_scan(struct mmc_softc *sc)
1294163516Simp{
1295163516Simp	device_t dev;
1296163516Simp
1297163516Simp	dev = sc->dev;
1298163516Simp	mmc_acquire_bus(dev, dev);
1299163516Simp
1300163516Simp	if (mmcbr_get_power_mode(dev) == power_on)
1301163516Simp		mmc_rescan_cards(sc);
1302163516Simp	mmc_go_discovery(sc);
1303163516Simp
1304163516Simp	mmc_release_bus(dev, dev);
1305183453Simp	/* XXX probe/attach/detach children? */
1306163516Simp}
1307163516Simp
1308163516Simpstatic int
1309163516Simpmmc_read_ivar(device_t bus, device_t child, int which, u_char *result)
1310163516Simp{
1311163516Simp	struct mmc_ivars *ivar = device_get_ivars(child);
1312163516Simp
1313163516Simp	switch (which) {
1314163516Simp	default:
1315163516Simp		return (EINVAL);
1316163516Simp	case MMC_IVAR_DSR_IMP:
1317163516Simp		*(int *)result = ivar->csd.dsr_imp;
1318163516Simp		break;
1319163516Simp	case MMC_IVAR_MEDIA_SIZE:
1320183731Smav		*(off_t *)result = ivar->sec_count;
1321163516Simp		break;
1322163516Simp	case MMC_IVAR_RCA:
1323163516Simp		*(int *)result = ivar->rca;
1324163516Simp		break;
1325163516Simp	case MMC_IVAR_SECTOR_SIZE:
1326183542Simp		*(int *)result = MMC_SECTOR_SIZE;
1327163516Simp		break;
1328163516Simp	case MMC_IVAR_TRAN_SPEED:
1329183763Smav		*(int *)result = mmcbr_get_clock(bus);
1330163516Simp		break;
1331183447Simp	case MMC_IVAR_READ_ONLY:
1332183447Simp		*(int *)result = ivar->read_only;
1333183447Simp		break;
1334183704Smav	case MMC_IVAR_HIGH_CAP:
1335183704Smav		*(int *)result = ivar->high_cap;
1336183704Smav		break;
1337183763Smav	case MMC_IVAR_CARD_TYPE:
1338183763Smav		*(int *)result = ivar->mode;
1339183763Smav		break;
1340183763Smav	case MMC_IVAR_BUS_WIDTH:
1341183763Smav		*(int *)result = ivar->bus_width;
1342183763Smav		break;
1343184033Smav	case MMC_IVAR_ERASE_SECTOR:
1344184033Smav		*(int *)result = ivar->erase_sector;
1345184033Smav		break;
1346184452Smav	case MMC_IVAR_MAX_DATA:
1347184452Smav		*(int *)result = mmcbr_get_max_data(bus);
1348184452Smav		break;
1349163516Simp	}
1350163516Simp	return (0);
1351163516Simp}
1352163516Simp
1353163516Simpstatic int
1354163516Simpmmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1355163516Simp{
1356183453Simp	/*
1357183453Simp	 * None are writable ATM
1358183453Simp	 */
1359183453Simp	return (EINVAL);
1360163516Simp}
1361163516Simp
1362163516Simp
1363163516Simpstatic void
1364163516Simpmmc_delayed_attach(void *xsc)
1365163516Simp{
1366163516Simp	struct mmc_softc *sc = xsc;
1367163516Simp
1368163516Simp	mmc_scan(sc);
1369163516Simp	config_intrhook_disestablish(&sc->config_intrhook);
1370163516Simp}
1371163516Simp
1372163516Simpstatic device_method_t mmc_methods[] = {
1373163516Simp	/* device_if */
1374163516Simp	DEVMETHOD(device_probe, mmc_probe),
1375163516Simp	DEVMETHOD(device_attach, mmc_attach),
1376163516Simp	DEVMETHOD(device_detach, mmc_detach),
1377163516Simp
1378163516Simp	/* Bus interface */
1379163516Simp	DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1380163516Simp	DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1381163516Simp
1382163516Simp	/* MMC Bus interface */
1383163516Simp	DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1384163516Simp	DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1385163516Simp	DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1386163516Simp
1387163516Simp	{0, 0},
1388163516Simp};
1389163516Simp
1390163516Simpstatic driver_t mmc_driver = {
1391163516Simp	"mmc",
1392163516Simp	mmc_methods,
1393163516Simp	sizeof(struct mmc_softc),
1394163516Simp};
1395163516Simpstatic devclass_t mmc_devclass;
1396163516Simp
1397163516Simp
1398163516SimpDRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, 0, 0);
1399183708SmavDRIVER_MODULE(mmc, sdhci, mmc_driver, mmc_devclass, 0, 0);
1400