mmc.c revision 283128
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 283128 2015-05-19 21:16:53Z imp $");
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>
65187875Smav#include <sys/sysctl.h>
66275951Sian#include <sys/time.h>
67163516Simp
68163516Simp#include <dev/mmc/mmcreg.h>
69163516Simp#include <dev/mmc/mmcbrvar.h>
70163516Simp#include <dev/mmc/mmcvar.h>
71163516Simp#include "mmcbr_if.h"
72163516Simp#include "mmcbus_if.h"
73163516Simp
74163516Simpstruct mmc_softc {
75163516Simp	device_t dev;
76163516Simp	struct mtx sc_mtx;
77163516Simp	struct intr_config_hook config_intrhook;
78163516Simp	device_t owner;
79163516Simp	uint32_t last_rca;
80275951Sian	int	 squelched; /* suppress reporting of (expected) errors */
81275951Sian	int	 log_count;
82275951Sian	struct timeval log_time;
83163516Simp};
84163516Simp
85275951Sian#define	LOG_PPS		5 /* Log no more than 5 errors per second. */
86275951Sian
87163516Simp/*
88163516Simp * Per-card data
89163516Simp */
90163516Simpstruct mmc_ivars {
91163516Simp	uint32_t raw_cid[4];	/* Raw bits of the CID */
92163516Simp	uint32_t raw_csd[4];	/* Raw bits of the CSD */
93183704Smav	uint32_t raw_scr[2];	/* Raw bits of the SCR */
94183704Smav	uint8_t raw_ext_csd[512];	/* Raw bits of the EXT_CSD */
95184033Smav	uint32_t raw_sd_status[16];	/* Raw bits of the SD_STATUS */
96163516Simp	uint16_t rca;
97163516Simp	enum mmc_card_mode mode;
98163516Simp	struct mmc_cid cid;	/* cid decoded */
99163516Simp	struct mmc_csd csd;	/* csd decoded */
100183704Smav	struct mmc_scr scr;	/* scr decoded */
101184033Smav	struct mmc_sd_status sd_status;	/* SD_STATUS decoded */
102183447Simp	u_char read_only;	/* True when the device is read-only */
103183704Smav	u_char bus_width;	/* Bus width to use */
104183704Smav	u_char timing;		/* Bus timing support */
105183731Smav	u_char high_cap;	/* High Capacity card (block addressed) */
106183731Smav	uint32_t sec_count;	/* Card capacity in 512byte blocks */
107183704Smav	uint32_t tran_speed;	/* Max speed in normal mode */
108183704Smav	uint32_t hs_tran_speed;	/* Max speed in high speed mode */
109184033Smav	uint32_t erase_sector;	/* Card native erase sector size */
110234524Smarius	char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
111269341Sian	char card_sn_string[16];/* Formatted serial # for disk->d_ident */
112163516Simp};
113163516Simp
114163516Simp#define CMD_RETRIES	3
115163516Simp
116254716Sian#define	CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
117254427Sian
118227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
119187875Smav
120188044Simpstatic int mmc_debug;
121187875SmavSYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RW, &mmc_debug, 0, "Debug level");
122187875Smav
123163516Simp/* bus entry points */
124236491Smariusstatic int mmc_acquire_bus(device_t busdev, device_t dev);
125163516Simpstatic int mmc_attach(device_t dev);
126236491Smariusstatic int mmc_child_location_str(device_t dev, device_t child, char *buf,
127236491Smarius    size_t buflen);
128163516Simpstatic int mmc_detach(device_t dev);
129236491Smariusstatic int mmc_probe(device_t dev);
130236491Smariusstatic int mmc_read_ivar(device_t bus, device_t child, int which,
131236491Smarius    uintptr_t *result);
132236491Smariusstatic int mmc_release_bus(device_t busdev, device_t dev);
133236491Smariusstatic int mmc_resume(device_t dev);
134185721Smavstatic int mmc_suspend(device_t dev);
135236491Smariusstatic int mmc_wait_for_request(device_t brdev, device_t reqdev,
136236491Smarius    struct mmc_request *req);
137236491Smariusstatic int mmc_write_ivar(device_t bus, device_t child, int which,
138236491Smarius    uintptr_t value);
139163516Simp
140163516Simp#define MMC_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
141163516Simp#define	MMC_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
142183444Simp#define MMC_LOCK_INIT(_sc)					\
143183444Simp	mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev),	\
144163516Simp	    "mmc", MTX_DEF)
145163516Simp#define MMC_LOCK_DESTROY(_sc)	mtx_destroy(&_sc->sc_mtx);
146163516Simp#define MMC_ASSERT_LOCKED(_sc)	mtx_assert(&_sc->sc_mtx, MA_OWNED);
147163516Simp#define MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);
148163516Simp
149236491Smariusstatic int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
150236491Smariusstatic void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
151236491Smariusstatic void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
152236491Smarius    struct mmc_sd_status *sd_status);
153236491Smariusstatic int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
154236491Smarius    uint32_t *rawsdstatus);
155236491Smariusstatic int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
156236491Smarius    uint32_t *rawscr);
157183763Smavstatic int mmc_calculate_clock(struct mmc_softc *sc);
158236491Smariusstatic void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid);
159236491Smariusstatic void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
160236491Smariusstatic void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
161236491Smariusstatic void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
162236491Smariusstatic void mmc_delayed_attach(void *xsc);
163236491Smariusstatic int mmc_delete_cards(struct mmc_softc *sc);
164236491Smariusstatic void mmc_discover_cards(struct mmc_softc *sc);
165236491Smariusstatic void mmc_format_card_id_string(struct mmc_ivars *ivar);
166236491Smariusstatic void mmc_go_discovery(struct mmc_softc *sc);
167236491Smariusstatic uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
168236491Smarius    int size);
169236491Smariusstatic int mmc_highest_voltage(uint32_t ocr);
170236491Smariusstatic void mmc_idle_cards(struct mmc_softc *sc);
171236491Smariusstatic void mmc_ms_delay(int ms);
172236491Smariusstatic void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
173183449Simpstatic void mmc_power_down(struct mmc_softc *sc);
174236491Smariusstatic void mmc_power_up(struct mmc_softc *sc);
175236491Smariusstatic void mmc_rescan_cards(struct mmc_softc *sc);
176236491Smariusstatic void mmc_scan(struct mmc_softc *sc);
177236491Smariusstatic int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
178236491Smarius    uint8_t value, uint8_t *res);
179236491Smariusstatic int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
180236491Smariusstatic uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
181236491Smariusstatic int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
182236491Smarius    uint32_t *rocr);
183236491Smariusstatic int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
184236491Smariusstatic int mmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd);
185236491Smariusstatic int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
186236491Smariusstatic int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
187236491Smarius    uint32_t *rocr);
188236491Smariusstatic int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
189236491Smariusstatic int mmc_send_status(struct mmc_softc *sc, uint16_t rca,
190236491Smarius    uint32_t *status);
191236491Smariusstatic int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
192236491Smariusstatic int mmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca,
193236491Smarius    int width);
194236491Smariusstatic int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
195236491Smariusstatic int mmc_set_timing(struct mmc_softc *sc, int timing);
196236491Smariusstatic int mmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index,
197236491Smarius    uint8_t value);
198236491Smariusstatic int mmc_test_bus_width(struct mmc_softc *sc);
199236491Smariusstatic int mmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
200236491Smarius    struct mmc_command *cmd, int retries);
201163516Simpstatic int mmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd,
202163516Simp    int retries);
203163516Simpstatic int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
204163516Simp    uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
205236491Smariusstatic int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
206236491Smariusstatic void mmc_wakeup(struct mmc_request *req);
207163516Simp
208163516Simpstatic void
209163516Simpmmc_ms_delay(int ms)
210163516Simp{
211236491Smarius
212163516Simp	DELAY(1000 * ms);	/* XXX BAD */
213163516Simp}
214163516Simp
215163516Simpstatic int
216163516Simpmmc_probe(device_t dev)
217163516Simp{
218163516Simp
219183445Simp	device_set_desc(dev, "MMC/SD bus");
220163516Simp	return (0);
221163516Simp}
222163516Simp
223163516Simpstatic int
224163516Simpmmc_attach(device_t dev)
225163516Simp{
226163516Simp	struct mmc_softc *sc;
227163516Simp
228163516Simp	sc = device_get_softc(dev);
229163516Simp	sc->dev = dev;
230163516Simp	MMC_LOCK_INIT(sc);
231163516Simp
232163516Simp	/* We'll probe and attach our children later, but before / mount */
233163516Simp	sc->config_intrhook.ich_func = mmc_delayed_attach;
234163516Simp	sc->config_intrhook.ich_arg = sc;
235163516Simp	if (config_intrhook_establish(&sc->config_intrhook) != 0)
236163516Simp		device_printf(dev, "config_intrhook_establish failed\n");
237163516Simp	return (0);
238163516Simp}
239163516Simp
240163516Simpstatic int
241163516Simpmmc_detach(device_t dev)
242163516Simp{
243169567Simp	struct mmc_softc *sc = device_get_softc(dev);
244185721Smav	int err;
245169567Simp
246185721Smav	if ((err = mmc_delete_cards(sc)) != 0)
247185721Smav		return (err);
248183449Simp	mmc_power_down(sc);
249169567Simp	MMC_LOCK_DESTROY(sc);
250169567Simp
251183467Simp	return (0);
252163516Simp}
253163516Simp
254163516Simpstatic int
255185721Smavmmc_suspend(device_t dev)
256185721Smav{
257185721Smav	struct mmc_softc *sc = device_get_softc(dev);
258185721Smav	int err;
259185721Smav
260185721Smav	err = bus_generic_suspend(dev);
261185721Smav	if (err)
262185721Smav	        return (err);
263185721Smav	mmc_power_down(sc);
264185721Smav	return (0);
265185721Smav}
266185721Smav
267185721Smavstatic int
268185721Smavmmc_resume(device_t dev)
269185721Smav{
270185721Smav	struct mmc_softc *sc = device_get_softc(dev);
271185721Smav
272185721Smav	mmc_scan(sc);
273185721Smav	return (bus_generic_resume(dev));
274185721Smav}
275185721Smav
276185721Smavstatic int
277163516Simpmmc_acquire_bus(device_t busdev, device_t dev)
278163516Simp{
279163516Simp	struct mmc_softc *sc;
280183704Smav	struct mmc_ivars *ivar;
281163516Simp	int err;
282163516Simp	int rca;
283163516Simp
284183452Simp	err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
285163516Simp	if (err)
286163516Simp		return (err);
287163516Simp	sc = device_get_softc(busdev);
288163516Simp	MMC_LOCK(sc);
289163516Simp	if (sc->owner)
290236156Smarius		panic("mmc: host bridge didn't serialize us.");
291163516Simp	sc->owner = dev;
292163516Simp	MMC_UNLOCK(sc);
293163516Simp
294163516Simp	if (busdev != dev) {
295183453Simp		/*
296183453Simp		 * Keep track of the last rca that we've selected.  If
297183453Simp		 * we're asked to do it again, don't.  We never
298183453Simp		 * unselect unless the bus code itself wants the mmc
299183453Simp		 * bus, and constantly reselecting causes problems.
300183453Simp		 */
301163516Simp		rca = mmc_get_rca(dev);
302163516Simp		if (sc->last_rca != rca) {
303183704Smav			mmc_select_card(sc, rca);
304163516Simp			sc->last_rca = rca;
305183704Smav			/* Prepare bus width for the new card. */
306183704Smav			ivar = device_get_ivars(dev);
307187875Smav			if (bootverbose || mmc_debug) {
308183763Smav				device_printf(busdev,
309183763Smav				    "setting bus width to %d bits\n",
310183775Simp				    (ivar->bus_width == bus_width_4) ? 4 :
311183775Simp				    (ivar->bus_width == bus_width_8) ? 8 : 1);
312183763Smav			}
313183763Smav			mmc_set_card_bus_width(sc, rca, ivar->bus_width);
314183704Smav			mmcbr_set_bus_width(busdev, ivar->bus_width);
315183704Smav			mmcbr_update_ios(busdev);
316163516Simp		}
317163516Simp	} else {
318183453Simp		/*
319183453Simp		 * If there's a card selected, stand down.
320183453Simp		 */
321163516Simp		if (sc->last_rca != 0) {
322183704Smav			mmc_select_card(sc, 0);
323163516Simp			sc->last_rca = 0;
324163516Simp		}
325163516Simp	}
326163516Simp
327163516Simp	return (0);
328163516Simp}
329163516Simp
330163516Simpstatic int
331163516Simpmmc_release_bus(device_t busdev, device_t dev)
332163516Simp{
333163516Simp	struct mmc_softc *sc;
334163516Simp	int err;
335163516Simp
336163516Simp	sc = device_get_softc(busdev);
337163516Simp
338163516Simp	MMC_LOCK(sc);
339163516Simp	if (!sc->owner)
340163516Simp		panic("mmc: releasing unowned bus.");
341163516Simp	if (sc->owner != dev)
342163516Simp		panic("mmc: you don't own the bus.  game over.");
343163516Simp	MMC_UNLOCK(sc);
344183452Simp	err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
345163516Simp	if (err)
346163516Simp		return (err);
347163516Simp	MMC_LOCK(sc);
348163516Simp	sc->owner = NULL;
349163516Simp	MMC_UNLOCK(sc);
350163516Simp	return (0);
351163516Simp}
352163516Simp
353163516Simpstatic uint32_t
354163516Simpmmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
355163516Simp{
356183446Simp
357183467Simp	return (ocr & MMC_OCR_VOLTAGE);
358163516Simp}
359163516Simp
360163516Simpstatic int
361163516Simpmmc_highest_voltage(uint32_t ocr)
362163516Simp{
363163516Simp	int i;
364163516Simp
365245755Sgonzo	for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
366245755Sgonzo	    i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
367163516Simp		if (ocr & (1 << i))
368183467Simp			return (i);
369163516Simp	return (-1);
370163516Simp}
371163516Simp
372163516Simpstatic void
373163516Simpmmc_wakeup(struct mmc_request *req)
374163516Simp{
375163516Simp	struct mmc_softc *sc;
376163516Simp
377163516Simp	sc = (struct mmc_softc *)req->done_data;
378163516Simp	MMC_LOCK(sc);
379163516Simp	req->flags |= MMC_REQ_DONE;
380185721Smav	MMC_UNLOCK(sc);
381163516Simp	wakeup(req);
382163516Simp}
383163516Simp
384163516Simpstatic int
385163516Simpmmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
386163516Simp{
387163516Simp
388163516Simp	req->done = mmc_wakeup;
389163516Simp	req->done_data = sc;
390187877Smav	if (mmc_debug > 1) {
391187875Smav		device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
392187875Smav		    req->cmd->opcode, req->cmd->arg, req->cmd->flags);
393187875Smav		if (req->cmd->data) {
394187875Smav			printf(" data %d\n", (int)req->cmd->data->len);
395187875Smav		} else
396187875Smav			printf("\n");
397187875Smav	}
398163516Simp	MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
399163516Simp	MMC_LOCK(sc);
400185721Smav	while ((req->flags & MMC_REQ_DONE) == 0)
401185721Smav		msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
402163516Simp	MMC_UNLOCK(sc);
403254431Sian	if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
404254431Sian		device_printf(sc->dev, "CMD%d RESULT: %d\n",
405254431Sian		    req->cmd->opcode, req->cmd->error);
406185721Smav	return (0);
407163516Simp}
408163516Simp
409163516Simpstatic int
410163516Simpmmc_wait_for_request(device_t brdev, device_t reqdev, struct mmc_request *req)
411163516Simp{
412163516Simp	struct mmc_softc *sc = device_get_softc(brdev);
413163516Simp
414183467Simp	return (mmc_wait_for_req(sc, req));
415163516Simp}
416163516Simp
417163516Simpstatic int
418163516Simpmmc_wait_for_cmd(struct mmc_softc *sc, struct mmc_command *cmd, int retries)
419163516Simp{
420163516Simp	struct mmc_request mreq;
421254431Sian	int err;
422163516Simp
423254431Sian	do {
424254431Sian		memset(&mreq, 0, sizeof(mreq));
425254431Sian		memset(cmd->resp, 0, sizeof(cmd->resp));
426254431Sian		cmd->retries = 0; /* Retries done here, not in hardware. */
427254431Sian		cmd->mrq = &mreq;
428254431Sian		mreq.cmd = cmd;
429254431Sian		if (mmc_wait_for_req(sc, &mreq) != 0)
430254431Sian			err = MMC_ERR_FAILED;
431254431Sian		else
432254431Sian			err = cmd->error;
433254431Sian	} while (err != MMC_ERR_NONE && retries-- > 0);
434254431Sian
435275951Sian	if (err != MMC_ERR_NONE && sc->squelched == 0) {
436275951Sian		if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
437275951Sian			device_printf(sc->dev, "CMD%d failed, RESULT: %d\n",
438275951Sian			    cmd->opcode, err);
439275951Sian		}
440275951Sian	}
441275951Sian
442254431Sian	return (err);
443163516Simp}
444163516Simp
445163516Simpstatic int
446163516Simpmmc_wait_for_app_cmd(struct mmc_softc *sc, uint32_t rca,
447163516Simp    struct mmc_command *cmd, int retries)
448163516Simp{
449163516Simp	struct mmc_command appcmd;
450254431Sian	int err;
451163516Simp
452275951Sian	/* Squelch error reporting at lower levels, we report below. */
453275951Sian	sc->squelched++;
454254431Sian	do {
455254432Sian		memset(&appcmd, 0, sizeof(appcmd));
456163516Simp		appcmd.opcode = MMC_APP_CMD;
457163516Simp		appcmd.arg = rca << 16;
458163516Simp		appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
459183470Simp		appcmd.data = NULL;
460254431Sian		if (mmc_wait_for_cmd(sc, &appcmd, 0) != 0)
461254431Sian			err = MMC_ERR_FAILED;
462254431Sian		else
463254431Sian			err = appcmd.error;
464254431Sian		if (err == MMC_ERR_NONE) {
465254431Sian			if (!(appcmd.resp[0] & R1_APP_CMD))
466254431Sian				err = MMC_ERR_FAILED;
467254716Sian			else if (mmc_wait_for_cmd(sc, cmd, 0) != 0)
468254716Sian				err = MMC_ERR_FAILED;
469254431Sian			else
470254431Sian				err = cmd->error;
471254431Sian		}
472254431Sian	} while (err != MMC_ERR_NONE && retries-- > 0);
473275951Sian	sc->squelched--;
474254431Sian
475275951Sian	if (err != MMC_ERR_NONE && sc->squelched == 0) {
476275951Sian		if (ppsratecheck(&sc->log_time, &sc->log_count, LOG_PPS)) {
477275951Sian			device_printf(sc->dev, "ACMD%d failed, RESULT: %d\n",
478275951Sian			    cmd->opcode, err);
479275951Sian		}
480275951Sian	}
481275951Sian
482163516Simp	return (err);
483163516Simp}
484163516Simp
485163516Simpstatic int
486163516Simpmmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
487163516Simp    uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
488163516Simp{
489163516Simp	struct mmc_command cmd;
490163516Simp	int err;
491163516Simp
492163516Simp	memset(&cmd, 0, sizeof(cmd));
493163516Simp	cmd.opcode = opcode;
494163516Simp	cmd.arg = arg;
495163516Simp	cmd.flags = flags;
496183470Simp	cmd.data = NULL;
497163516Simp	err = mmc_wait_for_cmd(sc, &cmd, retries);
498163516Simp	if (err)
499163516Simp		return (err);
500163516Simp	if (resp) {
501163516Simp		if (flags & MMC_RSP_136)
502163516Simp			memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
503163516Simp		else
504163516Simp			*resp = cmd.resp[0];
505163516Simp	}
506163516Simp	return (0);
507163516Simp}
508163516Simp
509163516Simpstatic void
510163516Simpmmc_idle_cards(struct mmc_softc *sc)
511163516Simp{
512163516Simp	device_t dev;
513163516Simp	struct mmc_command cmd;
514163516Simp
515163516Simp	dev = sc->dev;
516163516Simp	mmcbr_set_chip_select(dev, cs_high);
517163516Simp	mmcbr_update_ios(dev);
518163516Simp	mmc_ms_delay(1);
519163516Simp
520163516Simp	memset(&cmd, 0, sizeof(cmd));
521163516Simp	cmd.opcode = MMC_GO_IDLE_STATE;
522163516Simp	cmd.arg = 0;
523163516Simp	cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
524183470Simp	cmd.data = NULL;
525254431Sian	mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
526163516Simp	mmc_ms_delay(1);
527163516Simp
528163516Simp	mmcbr_set_chip_select(dev, cs_dontcare);
529163516Simp	mmcbr_update_ios(dev);
530163516Simp	mmc_ms_delay(1);
531163516Simp}
532163516Simp
533163516Simpstatic int
534163516Simpmmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
535163516Simp{
536163516Simp	struct mmc_command cmd;
537163516Simp	int err = MMC_ERR_NONE, i;
538163516Simp
539163516Simp	memset(&cmd, 0, sizeof(cmd));
540163516Simp	cmd.opcode = ACMD_SD_SEND_OP_COND;
541163516Simp	cmd.arg = ocr;
542163516Simp	cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
543183470Simp	cmd.data = NULL;
544163516Simp
545216941Spjd	for (i = 0; i < 1000; i++) {
546163516Simp		err = mmc_wait_for_app_cmd(sc, 0, &cmd, CMD_RETRIES);
547163516Simp		if (err != MMC_ERR_NONE)
548163516Simp			break;
549183709Smav		if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
550183709Smav		    (ocr & MMC_OCR_VOLTAGE) == 0)
551163516Simp			break;
552163516Simp		err = MMC_ERR_TIMEOUT;
553163516Simp		mmc_ms_delay(10);
554163516Simp	}
555163516Simp	if (rocr && err == MMC_ERR_NONE)
556163516Simp		*rocr = cmd.resp[0];
557183467Simp	return (err);
558163516Simp}
559163516Simp
560163516Simpstatic int
561163516Simpmmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
562163516Simp{
563163516Simp	struct mmc_command cmd;
564163516Simp	int err = MMC_ERR_NONE, i;
565163516Simp
566163516Simp	memset(&cmd, 0, sizeof(cmd));
567163516Simp	cmd.opcode = MMC_SEND_OP_COND;
568163516Simp	cmd.arg = ocr;
569163516Simp	cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
570183470Simp	cmd.data = NULL;
571163516Simp
572216941Spjd	for (i = 0; i < 1000; i++) {
573163516Simp		err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
574163516Simp		if (err != MMC_ERR_NONE)
575163516Simp			break;
576183709Smav		if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
577183709Smav		    (ocr & MMC_OCR_VOLTAGE) == 0)
578163516Simp			break;
579163516Simp		err = MMC_ERR_TIMEOUT;
580163516Simp		mmc_ms_delay(10);
581163516Simp	}
582163516Simp	if (rocr && err == MMC_ERR_NONE)
583163516Simp		*rocr = cmd.resp[0];
584183467Simp	return (err);
585163516Simp}
586163516Simp
587183704Smavstatic int
588183704Smavmmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
589183704Smav{
590183704Smav	struct mmc_command cmd;
591183704Smav	int err;
592183704Smav
593183704Smav	memset(&cmd, 0, sizeof(cmd));
594183704Smav	cmd.opcode = SD_SEND_IF_COND;
595183704Smav	cmd.arg = (vhs << 8) + 0xAA;
596183704Smav	cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
597183704Smav	cmd.data = NULL;
598183704Smav
599183704Smav	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
600183704Smav	return (err);
601183704Smav}
602183704Smav
603163516Simpstatic void
604163516Simpmmc_power_up(struct mmc_softc *sc)
605163516Simp{
606163516Simp	device_t dev;
607163516Simp
608163516Simp	dev = sc->dev;
609163516Simp	mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
610163516Simp	mmcbr_set_bus_mode(dev, opendrain);
611163516Simp	mmcbr_set_chip_select(dev, cs_dontcare);
612163516Simp	mmcbr_set_bus_width(dev, bus_width_1);
613163516Simp	mmcbr_set_power_mode(dev, power_up);
614163516Simp	mmcbr_set_clock(dev, 0);
615163516Simp	mmcbr_update_ios(dev);
616163516Simp	mmc_ms_delay(1);
617163516Simp
618254427Sian	mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
619183704Smav	mmcbr_set_timing(dev, bus_timing_normal);
620163516Simp	mmcbr_set_power_mode(dev, power_on);
621163516Simp	mmcbr_update_ios(dev);
622163516Simp	mmc_ms_delay(2);
623163516Simp}
624163516Simp
625183449Simpstatic void
626183449Simpmmc_power_down(struct mmc_softc *sc)
627183449Simp{
628183449Simp	device_t dev = sc->dev;
629183449Simp
630183449Simp	mmcbr_set_bus_mode(dev, opendrain);
631183449Simp	mmcbr_set_chip_select(dev, cs_dontcare);
632183449Simp	mmcbr_set_bus_width(dev, bus_width_1);
633183449Simp	mmcbr_set_power_mode(dev, power_off);
634183449Simp	mmcbr_set_clock(dev, 0);
635183704Smav	mmcbr_set_timing(dev, bus_timing_normal);
636183449Simp	mmcbr_update_ios(dev);
637183449Simp}
638183449Simp
639183704Smavstatic int
640183704Smavmmc_select_card(struct mmc_softc *sc, uint16_t rca)
641183704Smav{
642183775Simp	int flags;
643183775Simp
644183775Simp	flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
645183775Simp	return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
646183775Simp	    flags, NULL, CMD_RETRIES));
647183704Smav}
648183704Smav
649183704Smavstatic int
650183704Smavmmc_switch(struct mmc_softc *sc, uint8_t set, uint8_t index, uint8_t value)
651183704Smav{
652183704Smav	struct mmc_command cmd;
653183704Smav	int err;
654183704Smav
655254432Sian	memset(&cmd, 0, sizeof(cmd));
656183704Smav	cmd.opcode = MMC_SWITCH_FUNC;
657183704Smav	cmd.arg = (MMC_SWITCH_FUNC_WR << 24) |
658183704Smav	    (index << 16) |
659183704Smav	    (value << 8) |
660183704Smav	    set;
661183704Smav	cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
662183704Smav	cmd.data = NULL;
663254431Sian	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
664183704Smav	return (err);
665183704Smav}
666183704Smav
667183704Smavstatic int
668188044Simpmmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
669188044Simp    uint8_t *res)
670183704Smav{
671183704Smav	int err;
672183704Smav	struct mmc_command cmd;
673183704Smav	struct mmc_data data;
674183704Smav
675254432Sian	memset(&cmd, 0, sizeof(cmd));
676254432Sian	memset(&data, 0, sizeof(data));
677188044Simp	memset(res, 0, 64);
678183704Smav
679183704Smav	cmd.opcode = SD_SWITCH_FUNC;
680183704Smav	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
681188044Simp	cmd.arg = mode << 31;			/* 0 - check, 1 - set */
682183704Smav	cmd.arg |= 0x00FFFFFF;
683183705Smav	cmd.arg &= ~(0xF << (grp * 4));
684183705Smav	cmd.arg |= value << (grp * 4);
685183704Smav	cmd.data = &data;
686183704Smav
687183704Smav	data.data = res;
688183704Smav	data.len = 64;
689183704Smav	data.flags = MMC_DATA_READ;
690183704Smav
691183704Smav	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
692183704Smav	return (err);
693183704Smav}
694183704Smav
695183704Smavstatic int
696183763Smavmmc_set_card_bus_width(struct mmc_softc *sc, uint16_t rca, int width)
697183704Smav{
698187546Simp	struct mmc_command cmd;
699183704Smav	int err;
700187546Simp	uint8_t	value;
701183704Smav
702183704Smav	if (mmcbr_get_mode(sc->dev) == mode_sd) {
703254432Sian		memset(&cmd, 0, sizeof(cmd));
704234524Smarius		cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
705234524Smarius		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
706234524Smarius		cmd.arg = SD_CLR_CARD_DETECT;
707234524Smarius		err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
708234524Smarius		if (err != 0)
709234524Smarius			return (err);
710254432Sian		memset(&cmd, 0, sizeof(cmd));
711183704Smav		cmd.opcode = ACMD_SET_BUS_WIDTH;
712183704Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
713183704Smav		switch (width) {
714183704Smav		case bus_width_1:
715183704Smav			cmd.arg = SD_BUS_WIDTH_1;
716183704Smav			break;
717183704Smav		case bus_width_4:
718183704Smav			cmd.arg = SD_BUS_WIDTH_4;
719183704Smav			break;
720183704Smav		default:
721183704Smav			return (MMC_ERR_INVALID);
722183704Smav		}
723183704Smav		err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
724183704Smav	} else {
725183704Smav		switch (width) {
726183704Smav		case bus_width_1:
727183704Smav			value = EXT_CSD_BUS_WIDTH_1;
728183704Smav			break;
729183704Smav		case bus_width_4:
730183704Smav			value = EXT_CSD_BUS_WIDTH_4;
731183704Smav			break;
732183704Smav		case bus_width_8:
733183704Smav			value = EXT_CSD_BUS_WIDTH_8;
734183704Smav			break;
735183704Smav		default:
736183704Smav			return (MMC_ERR_INVALID);
737183704Smav		}
738187546Simp		err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
739187546Simp		    value);
740183704Smav	}
741183704Smav	return (err);
742183704Smav}
743183704Smav
744183704Smavstatic int
745183704Smavmmc_set_timing(struct mmc_softc *sc, int timing)
746183704Smav{
747183704Smav	int err;
748183704Smav	uint8_t	value;
749187543Simp	u_char switch_res[64];
750183704Smav
751183704Smav	switch (timing) {
752183704Smav	case bus_timing_normal:
753183704Smav		value = 0;
754183704Smav		break;
755183704Smav	case bus_timing_hs:
756183704Smav		value = 1;
757183704Smav		break;
758183704Smav	default:
759183704Smav		return (MMC_ERR_INVALID);
760183704Smav	}
761187543Simp	if (mmcbr_get_mode(sc->dev) == mode_sd)
762188044Simp		err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
763188044Simp		    value, switch_res);
764187543Simp	else
765183704Smav		err = mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
766183704Smav		    EXT_CSD_HS_TIMING, value);
767183704Smav	return (err);
768183704Smav}
769183704Smav
770183704Smavstatic int
771183704Smavmmc_test_bus_width(struct mmc_softc *sc)
772183704Smav{
773183704Smav	struct mmc_command cmd;
774183704Smav	struct mmc_data data;
775183704Smav	int err;
776183704Smav	uint8_t buf[8];
777183704Smav	uint8_t	p8[8] =   { 0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
778183704Smav	uint8_t	p8ok[8] = { 0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
779183704Smav	uint8_t	p4[4] =   { 0x5A, 0x00, 0x00, 0x00, };
780183704Smav	uint8_t	p4ok[4] = { 0xA5, 0x00, 0x00, 0x00, };
781183704Smav
782183704Smav	if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
783183704Smav		mmcbr_set_bus_width(sc->dev, bus_width_8);
784183704Smav		mmcbr_update_ios(sc->dev);
785183704Smav
786275951Sian		sc->squelched++; /* Errors are expected, squelch reporting. */
787254432Sian		memset(&cmd, 0, sizeof(cmd));
788254432Sian		memset(&data, 0, sizeof(data));
789183704Smav		cmd.opcode = MMC_BUSTEST_W;
790183704Smav		cmd.arg = 0;
791183704Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
792183704Smav		cmd.data = &data;
793183704Smav
794183704Smav		data.data = p8;
795183704Smav		data.len = 8;
796183704Smav		data.flags = MMC_DATA_WRITE;
797183704Smav		mmc_wait_for_cmd(sc, &cmd, 0);
798183704Smav
799254432Sian		memset(&cmd, 0, sizeof(cmd));
800254432Sian		memset(&data, 0, sizeof(data));
801183704Smav		cmd.opcode = MMC_BUSTEST_R;
802183704Smav		cmd.arg = 0;
803183704Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
804183704Smav		cmd.data = &data;
805183704Smav
806183704Smav		data.data = buf;
807183704Smav		data.len = 8;
808183704Smav		data.flags = MMC_DATA_READ;
809183704Smav		err = mmc_wait_for_cmd(sc, &cmd, 0);
810275951Sian		sc->squelched--;
811183704Smav
812183704Smav		mmcbr_set_bus_width(sc->dev, bus_width_1);
813183704Smav		mmcbr_update_ios(sc->dev);
814183704Smav
815183704Smav		if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
816183704Smav			return (bus_width_8);
817183704Smav	}
818183704Smav
819183704Smav	if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
820183704Smav		mmcbr_set_bus_width(sc->dev, bus_width_4);
821183704Smav		mmcbr_update_ios(sc->dev);
822183704Smav
823275951Sian		sc->squelched++; /* Errors are expected, squelch reporting. */
824254432Sian		memset(&cmd, 0, sizeof(cmd));
825254432Sian		memset(&data, 0, sizeof(data));
826183704Smav		cmd.opcode = MMC_BUSTEST_W;
827183704Smav		cmd.arg = 0;
828183704Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
829183704Smav		cmd.data = &data;
830183704Smav
831183704Smav		data.data = p4;
832183704Smav		data.len = 4;
833183704Smav		data.flags = MMC_DATA_WRITE;
834183704Smav		mmc_wait_for_cmd(sc, &cmd, 0);
835183704Smav
836254432Sian		memset(&cmd, 0, sizeof(cmd));
837254432Sian		memset(&data, 0, sizeof(data));
838183704Smav		cmd.opcode = MMC_BUSTEST_R;
839183704Smav		cmd.arg = 0;
840183704Smav		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
841183704Smav		cmd.data = &data;
842183704Smav
843183704Smav		data.data = buf;
844183704Smav		data.len = 4;
845183704Smav		data.flags = MMC_DATA_READ;
846183704Smav		err = mmc_wait_for_cmd(sc, &cmd, 0);
847275951Sian		sc->squelched--;
848183704Smav
849183704Smav		mmcbr_set_bus_width(sc->dev, bus_width_1);
850183704Smav		mmcbr_update_ios(sc->dev);
851183704Smav
852183704Smav		if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
853183704Smav			return (bus_width_4);
854183704Smav	}
855183704Smav	return (bus_width_1);
856183704Smav}
857183704Smav
858163516Simpstatic uint32_t
859184033Smavmmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
860163516Simp{
861183729Simp	const int i = (bit_len / 32) - (start / 32) - 1;
862163516Simp	const int shift = start & 31;
863163516Simp	uint32_t retval = bits[i] >> shift;
864163516Simp	if (size + shift > 32)
865163516Simp		retval |= bits[i - 1] << (32 - shift);
866217509Smav	return (retval & ((1llu << size) - 1));
867163516Simp}
868163516Simp
869163516Simpstatic void
870183729Simpmmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
871163516Simp{
872163516Simp	int i;
873163516Simp
874183729Simp	/* There's no version info, so we take it on faith */
875163516Simp	memset(cid, 0, sizeof(*cid));
876184033Smav	cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
877184033Smav	cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
878183729Simp	for (i = 0; i < 5; i++)
879184033Smav		cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
880187875Smav	cid->pnm[5] = 0;
881184033Smav	cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
882184033Smav	cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
883187875Smav	cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
884184033Smav	cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
885163516Simp}
886163516Simp
887183729Simpstatic void
888183729Simpmmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid)
889183729Simp{
890183729Simp	int i;
891183729Simp
892183729Simp	/* There's no version info, so we take it on faith */
893183729Simp	memset(cid, 0, sizeof(*cid));
894184033Smav	cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
895184033Smav	cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
896183729Simp	for (i = 0; i < 6; i++)
897184033Smav		cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
898187875Smav	cid->pnm[6] = 0;
899184033Smav	cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
900184033Smav	cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
901184033Smav	cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
902184033Smav	cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4) + 1997;
903183729Simp}
904183729Simp
905234524Smariusstatic void
906234524Smariusmmc_format_card_id_string(struct mmc_ivars *ivar)
907234524Smarius{
908234524Smarius	char oidstr[8];
909234524Smarius	uint8_t c1;
910234524Smarius	uint8_t c2;
911234524Smarius
912234524Smarius	/*
913234524Smarius	 * Format a card ID string for use by the mmcsd driver, it's what
914234524Smarius	 * appears between the <> in the following:
915234524Smarius	 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
916234524Smarius	 * 22.5MHz/4bit/128-block
917234524Smarius	 *
918269341Sian	 * Also format just the card serial number, which the mmcsd driver will
919269341Sian	 * use as the disk->d_ident string.
920269341Sian	 *
921234524Smarius	 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
922234524Smarius	 * and our max formatted length is currently 55 bytes if every field
923234524Smarius	 * contains the largest value.
924234524Smarius	 *
925234524Smarius	 * Sometimes the oid is two printable ascii chars; when it's not,
926234524Smarius	 * format it as 0xnnnn instead.
927234524Smarius	 */
928234524Smarius	c1 = (ivar->cid.oid >> 8) & 0x0ff;
929234524Smarius	c2 = ivar->cid.oid & 0x0ff;
930234524Smarius	if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
931234524Smarius		snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
932234524Smarius	else
933234524Smarius		snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
934269341Sian	snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
935269341Sian	    "%08X", ivar->cid.psn);
936234524Smarius	snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
937269341Sian	    "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
938234524Smarius	    ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
939234524Smarius	    ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
940234524Smarius	    ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
941234524Smarius	    ivar->cid.mid, oidstr);
942234524Smarius}
943234524Smarius
944163516Simpstatic const int exp[8] = {
945163516Simp	1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
946163516Simp};
947234524Smarius
948163516Simpstatic const int mant[16] = {
949234524Smarius	0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
950163516Simp};
951234524Smarius
952163516Simpstatic const int cur_min[8] = {
953163516Simp	500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
954163516Simp};
955234524Smarius
956163516Simpstatic const int cur_max[8] = {
957163516Simp	1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
958163516Simp};
959163516Simp
960163516Simpstatic void
961183729Simpmmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
962163516Simp{
963163516Simp	int v;
964163516Simp	int m;
965163516Simp	int e;
966163516Simp
967163516Simp	memset(csd, 0, sizeof(*csd));
968184033Smav	csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
969183729Simp	if (v == 0) {
970184033Smav		m = mmc_get_bits(raw_csd, 128, 115, 4);
971184033Smav		e = mmc_get_bits(raw_csd, 128, 112, 3);
972236156Smarius		csd->tacc = (exp[e] * mant[m] + 9) / 10;
973184033Smav		csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
974184033Smav		m = mmc_get_bits(raw_csd, 128, 99, 4);
975184033Smav		e = mmc_get_bits(raw_csd, 128, 96, 3);
976183704Smav		csd->tran_speed = exp[e] * 10000 * mant[m];
977184033Smav		csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
978184033Smav		csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
979184033Smav		csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
980184033Smav		csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
981184033Smav		csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
982184033Smav		csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
983184033Smav		csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
984184033Smav		csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
985184033Smav		csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
986184033Smav		csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
987184033Smav		m = mmc_get_bits(raw_csd, 128, 62, 12);
988184033Smav		e = mmc_get_bits(raw_csd, 128, 47, 3);
989183704Smav		csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
990184033Smav		csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
991184033Smav		csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
992184033Smav		csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
993184033Smav		csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
994184033Smav		csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
995184033Smav		csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
996184033Smav		csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
997183729Simp	} else if (v == 1) {
998184033Smav		m = mmc_get_bits(raw_csd, 128, 115, 4);
999184033Smav		e = mmc_get_bits(raw_csd, 128, 112, 3);
1000236156Smarius		csd->tacc = (exp[e] * mant[m] + 9) / 10;
1001184033Smav		csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1002184033Smav		m = mmc_get_bits(raw_csd, 128, 99, 4);
1003184033Smav		e = mmc_get_bits(raw_csd, 128, 96, 3);
1004183729Simp		csd->tran_speed = exp[e] * 10000 * mant[m];
1005184033Smav		csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1006184033Smav		csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1007184033Smav		csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1008184033Smav		csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1009184033Smav		csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1010184033Smav		csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1011184033Smav		csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) + 1) *
1012183729Simp		    512 * 1024;
1013184033Smav		csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
1014184033Smav		csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
1015184033Smav		csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
1016184033Smav		csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1017184033Smav		csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1018184033Smav		csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1019184033Smav		csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1020183729Simp	} else
1021183729Simp		panic("unknown SD CSD version");
1022163516Simp}
1023163516Simp
1024183704Smavstatic void
1025183729Simpmmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
1026183729Simp{
1027183729Simp	int m;
1028183729Simp	int e;
1029183729Simp
1030183729Simp	memset(csd, 0, sizeof(*csd));
1031184033Smav	csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
1032184033Smav	csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
1033184033Smav	m = mmc_get_bits(raw_csd, 128, 115, 4);
1034184033Smav	e = mmc_get_bits(raw_csd, 128, 112, 3);
1035183729Simp	csd->tacc = exp[e] * mant[m] + 9 / 10;
1036184033Smav	csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
1037184033Smav	m = mmc_get_bits(raw_csd, 128, 99, 4);
1038184033Smav	e = mmc_get_bits(raw_csd, 128, 96, 3);
1039183729Simp	csd->tran_speed = exp[e] * 10000 * mant[m];
1040184033Smav	csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
1041184033Smav	csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
1042184033Smav	csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
1043184033Smav	csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
1044184033Smav	csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
1045184033Smav	csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
1046184033Smav	csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
1047184033Smav	csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
1048184033Smav	csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
1049184033Smav	csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
1050184033Smav	m = mmc_get_bits(raw_csd, 128, 62, 12);
1051184033Smav	e = mmc_get_bits(raw_csd, 128, 47, 3);
1052183729Simp	csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
1053184033Smav	csd->erase_blk_en = 0;
1054184033Smav	csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
1055184033Smav	    (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
1056184033Smav	csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
1057184033Smav	csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
1058184033Smav	csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
1059184033Smav	csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1060184033Smav	csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1061183729Simp}
1062183729Simp
1063183729Simpstatic void
1064183704Smavmmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1065183704Smav{
1066183704Smav	unsigned int scr_struct;
1067183704Smav
1068183704Smav	memset(scr, 0, sizeof(*scr));
1069183729Simp
1070184033Smav	scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1071183704Smav	if (scr_struct != 0) {
1072183704Smav		printf("Unrecognised SCR structure version %d\n",
1073183704Smav		    scr_struct);
1074183704Smav		return;
1075183704Smav	}
1076184033Smav	scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1077184033Smav	scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1078183704Smav}
1079183704Smav
1080184033Smavstatic void
1081184033Smavmmc_app_decode_sd_status(uint32_t *raw_sd_status,
1082184033Smav    struct mmc_sd_status *sd_status)
1083184033Smav{
1084184033Smav
1085184033Smav	memset(sd_status, 0, sizeof(*sd_status));
1086184033Smav
1087184033Smav	sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1088184033Smav	sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1089184033Smav	sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1090184033Smav	sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1091184033Smav	sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1092184033Smav	sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1093184033Smav	sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1094184033Smav	sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1095184033Smav	sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1096184033Smav	sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1097184033Smav}
1098184033Smav
1099163516Simpstatic int
1100163516Simpmmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1101163516Simp{
1102163516Simp	struct mmc_command cmd;
1103163516Simp	int err;
1104163516Simp
1105254432Sian	memset(&cmd, 0, sizeof(cmd));
1106163516Simp	cmd.opcode = MMC_ALL_SEND_CID;
1107163516Simp	cmd.arg = 0;
1108163516Simp	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1109183470Simp	cmd.data = NULL;
1110254431Sian	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1111163516Simp	memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1112163516Simp	return (err);
1113163516Simp}
1114163516Simp
1115163516Simpstatic int
1116236156Smariusmmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1117163516Simp{
1118163516Simp	struct mmc_command cmd;
1119163516Simp	int err;
1120163516Simp
1121254432Sian	memset(&cmd, 0, sizeof(cmd));
1122163516Simp	cmd.opcode = MMC_SEND_CSD;
1123163516Simp	cmd.arg = rca << 16;
1124163516Simp	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1125183470Simp	cmd.data = NULL;
1126254431Sian	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1127236156Smarius	memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1128163516Simp	return (err);
1129163516Simp}
1130163516Simp
1131163516Simpstatic int
1132183704Smavmmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1133183704Smav{
1134183704Smav	int err;
1135183704Smav	struct mmc_command cmd;
1136183704Smav	struct mmc_data data;
1137183704Smav
1138254432Sian	memset(&cmd, 0, sizeof(cmd));
1139254432Sian	memset(&data, 0, sizeof(data));
1140183704Smav
1141183704Smav	memset(rawscr, 0, 8);
1142183704Smav	cmd.opcode = ACMD_SEND_SCR;
1143183704Smav	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1144183704Smav	cmd.arg = 0;
1145183704Smav	cmd.data = &data;
1146183704Smav
1147183704Smav	data.data = rawscr;
1148183704Smav	data.len = 8;
1149183704Smav	data.flags = MMC_DATA_READ;
1150183704Smav
1151183704Smav	err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1152183704Smav	rawscr[0] = be32toh(rawscr[0]);
1153183704Smav	rawscr[1] = be32toh(rawscr[1]);
1154183704Smav	return (err);
1155183704Smav}
1156183704Smav
1157183704Smavstatic int
1158183704Smavmmc_send_ext_csd(struct mmc_softc *sc, uint8_t *rawextcsd)
1159183704Smav{
1160183704Smav	int err;
1161183704Smav	struct mmc_command cmd;
1162183704Smav	struct mmc_data data;
1163183704Smav
1164254432Sian	memset(&cmd, 0, sizeof(cmd));
1165254432Sian	memset(&data, 0, sizeof(data));
1166183704Smav
1167183704Smav	memset(rawextcsd, 0, 512);
1168183704Smav	cmd.opcode = MMC_SEND_EXT_CSD;
1169183704Smav	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1170183704Smav	cmd.arg = 0;
1171183704Smav	cmd.data = &data;
1172183704Smav
1173183704Smav	data.data = rawextcsd;
1174183704Smav	data.len = 512;
1175183704Smav	data.flags = MMC_DATA_READ;
1176183704Smav
1177183704Smav	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1178183704Smav	return (err);
1179183704Smav}
1180183704Smav
1181183704Smavstatic int
1182184033Smavmmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1183184033Smav{
1184184033Smav	int err, i;
1185184033Smav	struct mmc_command cmd;
1186184033Smav	struct mmc_data data;
1187184033Smav
1188254432Sian	memset(&cmd, 0, sizeof(cmd));
1189254432Sian	memset(&data, 0, sizeof(data));
1190184033Smav
1191184033Smav	memset(rawsdstatus, 0, 64);
1192184033Smav	cmd.opcode = ACMD_SD_STATUS;
1193184033Smav	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1194184033Smav	cmd.arg = 0;
1195184033Smav	cmd.data = &data;
1196184033Smav
1197184033Smav	data.data = rawsdstatus;
1198184033Smav	data.len = 64;
1199184033Smav	data.flags = MMC_DATA_READ;
1200184033Smav
1201184033Smav	err = mmc_wait_for_app_cmd(sc, rca, &cmd, CMD_RETRIES);
1202184033Smav	for (i = 0; i < 16; i++)
1203184033Smav	    rawsdstatus[i] = be32toh(rawsdstatus[i]);
1204184033Smav	return (err);
1205184033Smav}
1206184033Smav
1207184033Smavstatic int
1208183704Smavmmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1209183704Smav{
1210183704Smav	struct mmc_command cmd;
1211183704Smav	int err;
1212183704Smav
1213254432Sian	memset(&cmd, 0, sizeof(cmd));
1214183704Smav	cmd.opcode = MMC_SET_RELATIVE_ADDR;
1215183704Smav	cmd.arg = resp << 16;
1216183704Smav	cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1217183704Smav	cmd.data = NULL;
1218254431Sian	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1219183704Smav	return (err);
1220183704Smav}
1221183704Smav
1222183704Smavstatic int
1223163516Simpmmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1224163516Simp{
1225163516Simp	struct mmc_command cmd;
1226163516Simp	int err;
1227163516Simp
1228254432Sian	memset(&cmd, 0, sizeof(cmd));
1229163516Simp	cmd.opcode = SD_SEND_RELATIVE_ADDR;
1230163516Simp	cmd.arg = 0;
1231163516Simp	cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1232183470Simp	cmd.data = NULL;
1233254431Sian	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1234163516Simp	*resp = cmd.resp[0];
1235163516Simp	return (err);
1236163516Simp}
1237163516Simp
1238236156Smariusstatic int
1239236156Smariusmmc_send_status(struct mmc_softc *sc, uint16_t rca, uint32_t *status)
1240236156Smarius{
1241236156Smarius	struct mmc_command cmd;
1242236156Smarius	int err;
1243236156Smarius
1244254432Sian	memset(&cmd, 0, sizeof(cmd));
1245236156Smarius	cmd.opcode = MMC_SEND_STATUS;
1246236156Smarius	cmd.arg = rca << 16;
1247236156Smarius	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1248236156Smarius	cmd.data = NULL;
1249254431Sian	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1250236156Smarius	*status = cmd.resp[0];
1251236156Smarius	return (err);
1252236156Smarius}
1253236156Smarius
1254236156Smariusstatic int
1255236156Smariusmmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1256236156Smarius{
1257236156Smarius	struct mmc_command cmd;
1258236156Smarius	int err;
1259236156Smarius
1260254432Sian	memset(&cmd, 0, sizeof(cmd));
1261236156Smarius	cmd.opcode = MMC_SET_BLOCKLEN;
1262236156Smarius	cmd.arg = len;
1263236156Smarius	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1264236156Smarius	cmd.data = NULL;
1265254431Sian	err = mmc_wait_for_cmd(sc, &cmd, CMD_RETRIES);
1266236156Smarius	return (err);
1267236156Smarius}
1268236156Smarius
1269163516Simpstatic void
1270187875Smavmmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1271187875Smav{
1272254425Sian	device_printf(dev, "Card at relative address 0x%04x%s:\n",
1273187875Smav	    ivar->rca, newcard ? " added" : "");
1274234524Smarius	device_printf(dev, " card: %s\n", ivar->card_id_string);
1275187875Smav	device_printf(dev, " bus: %ubit, %uMHz%s\n",
1276187875Smav	    (ivar->bus_width == bus_width_1 ? 1 :
1277187875Smav	    (ivar->bus_width == bus_width_4 ? 4 : 8)),
1278187875Smav	    (ivar->timing == bus_timing_hs ?
1279187875Smav		ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1280187875Smav	    ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1281187875Smav	device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1282187875Smav	    ivar->sec_count, ivar->erase_sector,
1283187875Smav	    ivar->read_only ? ", read-only" : "");
1284187875Smav}
1285187875Smav
1286187875Smavstatic void
1287163516Simpmmc_discover_cards(struct mmc_softc *sc)
1288163516Simp{
1289185721Smav	struct mmc_ivars *ivar = NULL;
1290185721Smav	device_t *devlist;
1291185721Smav	int err, i, devcount, newcard;
1292236156Smarius	uint32_t raw_cid[4], resp, sec_count, status;
1293163516Simp	device_t child;
1294183704Smav	uint16_t rca = 2;
1295183704Smav	u_char switch_res[64];
1296163516Simp
1297187875Smav	if (bootverbose || mmc_debug)
1298187875Smav		device_printf(sc->dev, "Probing cards\n");
1299163516Simp	while (1) {
1300275951Sian		sc->squelched++; /* Errors are expected, squelch reporting. */
1301185721Smav		err = mmc_all_send_cid(sc, raw_cid);
1302275951Sian		sc->squelched--;
1303163516Simp		if (err == MMC_ERR_TIMEOUT)
1304163516Simp			break;
1305163516Simp		if (err != MMC_ERR_NONE) {
1306183468Simp			device_printf(sc->dev, "Error reading CID %d\n", err);
1307163516Simp			break;
1308163516Simp		}
1309185721Smav		newcard = 1;
1310185721Smav		if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1311185721Smav			return;
1312185721Smav		for (i = 0; i < devcount; i++) {
1313185721Smav			ivar = device_get_ivars(devlist[i]);
1314185721Smav			if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) == 0) {
1315185721Smav				newcard = 0;
1316185721Smav				break;
1317185721Smav			}
1318185721Smav		}
1319185721Smav		free(devlist, M_TEMP);
1320187875Smav		if (bootverbose || mmc_debug) {
1321187875Smav			device_printf(sc->dev, "%sard detected (CID %08x%08x%08x%08x)\n",
1322187875Smav			    newcard ? "New c" : "C",
1323187875Smav			    raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1324187875Smav		}
1325185721Smav		if (newcard) {
1326185721Smav			ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1327185721Smav			    M_WAITOK | M_ZERO);
1328185721Smav			memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1329185721Smav		}
1330183704Smav		if (mmcbr_get_ro(sc->dev))
1331183704Smav			ivar->read_only = 1;
1332183704Smav		ivar->bus_width = bus_width_1;
1333188044Simp		ivar->timing = bus_timing_normal;
1334183704Smav		ivar->mode = mmcbr_get_mode(sc->dev);
1335183704Smav		if (ivar->mode == mode_sd) {
1336183729Simp			mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1337163516Simp			mmc_send_relative_addr(sc, &resp);
1338163516Simp			ivar->rca = resp >> 16;
1339183704Smav			/* Get card CSD. */
1340163516Simp			mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1341236156Smarius			if (bootverbose || mmc_debug)
1342236156Smarius				device_printf(sc->dev,
1343236156Smarius				    "%sard detected (CSD %08x%08x%08x%08x)\n",
1344236156Smarius				    newcard ? "New c" : "C", ivar->raw_csd[0],
1345236156Smarius				    ivar->raw_csd[1], ivar->raw_csd[2],
1346236156Smarius				    ivar->raw_csd[3]);
1347183729Simp			mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1348183731Smav			ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1349183704Smav			if (ivar->csd.csd_structure > 0)
1350183704Smav				ivar->high_cap = 1;
1351183704Smav			ivar->tran_speed = ivar->csd.tran_speed;
1352184033Smav			ivar->erase_sector = ivar->csd.erase_sector *
1353184033Smav			    ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1354236156Smarius
1355236156Smarius			err = mmc_send_status(sc, ivar->rca, &status);
1356236156Smarius			if (err != MMC_ERR_NONE) {
1357236156Smarius				device_printf(sc->dev,
1358236156Smarius				    "Error reading card status %d\n", err);
1359236156Smarius				break;
1360236156Smarius			}
1361236156Smarius			if ((status & R1_CARD_IS_LOCKED) != 0) {
1362236156Smarius				device_printf(sc->dev,
1363236156Smarius				    "Card is password protected, skipping.\n");
1364236156Smarius				break;
1365236156Smarius			}
1366236156Smarius
1367183704Smav			/* Get card SCR. Card must be selected to fetch it. */
1368183704Smav			mmc_select_card(sc, ivar->rca);
1369183704Smav			mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1370183704Smav			mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1371188044Simp			/* Get card switch capabilities (command class 10). */
1372183704Smav			if ((ivar->scr.sda_vsn >= 1) &&
1373183704Smav			    (ivar->csd.ccc & (1<<10))) {
1374188044Simp				mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1375188044Simp				    SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1376188044Simp				    switch_res);
1377183704Smav				if (switch_res[13] & 2) {
1378183704Smav					ivar->timing = bus_timing_hs;
1379188044Simp					ivar->hs_tran_speed = SD_MAX_HS;
1380183704Smav				}
1381183704Smav			}
1382283128Simp
1383283128Simp			/*
1384283128Simp			 * We reselect the card here. Some cards become
1385283128Simp			 * unselected and timeout with the above two commands,
1386283128Simp			 * although the state tables / diagrams in the standard
1387283128Simp			 * suggest they go back to the transfer state. The only
1388283128Simp			 * thing we use from the sd_status is the erase sector
1389283128Simp			 * size, but it is still nice to get that right. It is
1390283128Simp			 * normally harmless for cards not misbehaving. The
1391283128Simp			 * Atmel bridge will complain about this command timing
1392283128Simp			 * out. Others seem to handle it correctly, so it may
1393283128Simp			 * be a combination of card and controller.
1394283128Simp			 */
1395283128Simp			mmc_select_card(sc, ivar->rca);
1396184033Smav			mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1397184033Smav			mmc_app_decode_sd_status(ivar->raw_sd_status,
1398184033Smav			    &ivar->sd_status);
1399184033Smav			if (ivar->sd_status.au_size != 0) {
1400184033Smav				ivar->erase_sector =
1401184033Smav				    16 << ivar->sd_status.au_size;
1402184033Smav			}
1403183704Smav			/* Find max supported bus width. */
1404183704Smav			if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1405183704Smav			    (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1406183704Smav				ivar->bus_width = bus_width_4;
1407236156Smarius
1408236156Smarius			/*
1409236156Smarius			 * Some cards that report maximum I/O block sizes
1410236156Smarius			 * greater than 512 require the block length to be
1411236156Smarius			 * set to 512, even though that is supposed to be
1412236156Smarius			 * the default.  Example:
1413236156Smarius			 *
1414236156Smarius			 * Transcend 2GB SDSC card, CID:
1415236156Smarius			 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1416236156Smarius			 */
1417236156Smarius			if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1418236156Smarius			    ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1419236156Smarius				mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1420236156Smarius
1421234524Smarius			mmc_format_card_id_string(ivar);
1422236156Smarius
1423187875Smav			if (bootverbose || mmc_debug)
1424187875Smav				mmc_log_card(sc->dev, ivar, newcard);
1425185721Smav			if (newcard) {
1426185721Smav				/* Add device. */
1427185721Smav				child = device_add_child(sc->dev, NULL, -1);
1428185721Smav				device_set_ivars(child, ivar);
1429185721Smav			}
1430275905Simp			mmc_select_card(sc, 0);
1431169567Simp			return;
1432163516Simp		}
1433183729Simp		mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid);
1434183704Smav		ivar->rca = rca++;
1435183704Smav		mmc_set_relative_addr(sc, ivar->rca);
1436183704Smav		/* Get card CSD. */
1437183704Smav		mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1438236156Smarius		if (bootverbose || mmc_debug)
1439236156Smarius			device_printf(sc->dev,
1440236156Smarius			    "%sard detected (CSD %08x%08x%08x%08x)\n",
1441236156Smarius			    newcard ? "New c" : "C", ivar->raw_csd[0],
1442236156Smarius			    ivar->raw_csd[1], ivar->raw_csd[2],
1443236156Smarius			    ivar->raw_csd[3]);
1444236156Smarius
1445183729Simp		mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1446183731Smav		ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1447183704Smav		ivar->tran_speed = ivar->csd.tran_speed;
1448184033Smav		ivar->erase_sector = ivar->csd.erase_sector *
1449184033Smav		    ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1450236156Smarius
1451236156Smarius		err = mmc_send_status(sc, ivar->rca, &status);
1452236156Smarius		if (err != MMC_ERR_NONE) {
1453236156Smarius			device_printf(sc->dev,
1454236156Smarius			    "Error reading card status %d\n", err);
1455236156Smarius			break;
1456236156Smarius		}
1457236156Smarius		if ((status & R1_CARD_IS_LOCKED) != 0) {
1458236156Smarius			device_printf(sc->dev,
1459236156Smarius			    "Card is password protected, skipping.\n");
1460236156Smarius			break;
1461236156Smarius		}
1462236156Smarius
1463276106Simp		mmc_select_card(sc, ivar->rca);
1464276106Simp
1465183704Smav		/* Only MMC >= 4.x cards support EXT_CSD. */
1466183704Smav		if (ivar->csd.spec_vers >= 4) {
1467183704Smav			mmc_send_ext_csd(sc, ivar->raw_ext_csd);
1468183731Smav			/* Handle extended capacity from EXT_CSD */
1469183731Smav			sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1470183731Smav			    (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1471183731Smav			    (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1472183731Smav			    (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1473183731Smav			if (sec_count != 0) {
1474183731Smav				ivar->sec_count = sec_count;
1475183731Smav				ivar->high_cap = 1;
1476183731Smav			}
1477183704Smav			/* Get card speed in high speed mode. */
1478183704Smav			ivar->timing = bus_timing_hs;
1479183731Smav			if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1480183704Smav			    & EXT_CSD_CARD_TYPE_52)
1481188044Simp				ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1482183731Smav			else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1483183704Smav			    & EXT_CSD_CARD_TYPE_26)
1484188044Simp				ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1485183704Smav			else
1486183704Smav				ivar->hs_tran_speed = ivar->tran_speed;
1487183704Smav			/* Find max supported bus width. */
1488183704Smav			ivar->bus_width = mmc_test_bus_width(sc);
1489184033Smav			/* Handle HC erase sector size. */
1490184033Smav			if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1491184033Smav				ivar->erase_sector = 1024 *
1492184033Smav				    ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1493184033Smav				mmc_switch(sc, EXT_CSD_CMD_SET_NORMAL,
1494184033Smav				    EXT_CSD_ERASE_GRP_DEF, 1);
1495184033Smav			}
1496183704Smav		} else {
1497183704Smav			ivar->bus_width = bus_width_1;
1498183704Smav			ivar->timing = bus_timing_normal;
1499183704Smav		}
1500236156Smarius
1501236156Smarius		/*
1502236156Smarius		 * Some cards that report maximum I/O block sizes greater
1503236156Smarius		 * than 512 require the block length to be set to 512, even
1504236156Smarius		 * though that is supposed to be the default.  Example:
1505236156Smarius		 *
1506236156Smarius		 * Transcend 2GB SDSC card, CID:
1507236156Smarius		 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1508236156Smarius		 */
1509236156Smarius		if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1510236156Smarius		    ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1511236156Smarius			mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1512236156Smarius
1513234524Smarius		mmc_format_card_id_string(ivar);
1514236156Smarius
1515187875Smav		if (bootverbose || mmc_debug)
1516187875Smav			mmc_log_card(sc->dev, ivar, newcard);
1517185721Smav		if (newcard) {
1518185721Smav			/* Add device. */
1519185721Smav			child = device_add_child(sc->dev, NULL, -1);
1520185721Smav			device_set_ivars(child, ivar);
1521185721Smav		}
1522276106Simp		mmc_select_card(sc, 0);
1523163516Simp	}
1524163516Simp}
1525163516Simp
1526163516Simpstatic void
1527185721Smavmmc_rescan_cards(struct mmc_softc *sc)
1528185721Smav{
1529185721Smav	struct mmc_ivars *ivar = NULL;
1530185721Smav	device_t *devlist;
1531185721Smav	int err, i, devcount;
1532185721Smav
1533185721Smav	if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1534185721Smav		return;
1535185721Smav	for (i = 0; i < devcount; i++) {
1536185721Smav		ivar = device_get_ivars(devlist[i]);
1537185721Smav		if (mmc_select_card(sc, ivar->rca)) {
1538187875Smav			if (bootverbose || mmc_debug)
1539187875Smav				device_printf(sc->dev, "Card at relative address %d lost.\n",
1540187875Smav				    ivar->rca);
1541185721Smav			device_delete_child(sc->dev, devlist[i]);
1542185721Smav			free(ivar, M_DEVBUF);
1543185721Smav		}
1544185721Smav	}
1545185721Smav	free(devlist, M_TEMP);
1546185721Smav	mmc_select_card(sc, 0);
1547185721Smav}
1548185721Smav
1549185721Smavstatic int
1550185721Smavmmc_delete_cards(struct mmc_softc *sc)
1551185721Smav{
1552185721Smav	struct mmc_ivars *ivar;
1553185721Smav	device_t *devlist;
1554185721Smav	int err, i, devcount;
1555185721Smav
1556185721Smav	if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1557185721Smav		return (err);
1558185721Smav	for (i = 0; i < devcount; i++) {
1559185721Smav		ivar = device_get_ivars(devlist[i]);
1560187875Smav		if (bootverbose || mmc_debug)
1561187875Smav			device_printf(sc->dev, "Card at relative address %d deleted.\n",
1562187875Smav			    ivar->rca);
1563185721Smav		device_delete_child(sc->dev, devlist[i]);
1564185721Smav		free(ivar, M_DEVBUF);
1565185721Smav	}
1566185721Smav	free(devlist, M_TEMP);
1567185721Smav	return (0);
1568185721Smav}
1569185721Smav
1570185721Smavstatic void
1571163516Simpmmc_go_discovery(struct mmc_softc *sc)
1572163516Simp{
1573163516Simp	uint32_t ocr;
1574163516Simp	device_t dev;
1575183704Smav	int err;
1576163516Simp
1577163516Simp	dev = sc->dev;
1578163516Simp	if (mmcbr_get_power_mode(dev) != power_on) {
1579183453Simp		/*
1580183453Simp		 * First, try SD modes
1581183453Simp		 */
1582275951Sian		sc->squelched++; /* Errors are expected, squelch reporting. */
1583163516Simp		mmcbr_set_mode(dev, mode_sd);
1584163516Simp		mmc_power_up(sc);
1585163516Simp		mmcbr_set_bus_mode(dev, pushpull);
1586187875Smav		if (bootverbose || mmc_debug)
1587187875Smav			device_printf(sc->dev, "Probing bus\n");
1588163516Simp		mmc_idle_cards(sc);
1589183704Smav		err = mmc_send_if_cond(sc, 1);
1590187875Smav		if ((bootverbose || mmc_debug) && err == 0)
1591187875Smav			device_printf(sc->dev, "SD 2.0 interface conditions: OK\n");
1592236156Smarius		if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1593187875Smav			if (bootverbose || mmc_debug)
1594187875Smav				device_printf(sc->dev, "SD probe: failed\n");
1595183453Simp			/*
1596183453Simp			 * Failed, try MMC
1597183453Simp			 */
1598163516Simp			mmcbr_set_mode(dev, mode_mmc);
1599187875Smav			if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1600187875Smav				if (bootverbose || mmc_debug)
1601187875Smav					device_printf(sc->dev, "MMC probe: failed\n");
1602185721Smav				ocr = 0; /* Failed both, powerdown. */
1603187875Smav			} else if (bootverbose || mmc_debug)
1604187875Smav				device_printf(sc->dev,
1605187875Smav				    "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1606187875Smav		} else if (bootverbose || mmc_debug)
1607187875Smav			device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n", ocr);
1608275951Sian		sc->squelched--;
1609187875Smav
1610163516Simp		mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1611163516Simp		if (mmcbr_get_ocr(dev) != 0)
1612163516Simp			mmc_idle_cards(sc);
1613163516Simp	} else {
1614163516Simp		mmcbr_set_bus_mode(dev, opendrain);
1615254427Sian		mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1616163516Simp		mmcbr_update_ios(dev);
1617183453Simp		/* XXX recompute vdd based on new cards? */
1618163516Simp	}
1619163516Simp	/*
1620163516Simp	 * Make sure that we have a mutually agreeable voltage to at least
1621163516Simp	 * one card on the bus.
1622163516Simp	 */
1623187875Smav	if (bootverbose || mmc_debug)
1624187875Smav		device_printf(sc->dev, "Current OCR: 0x%08x\n", mmcbr_get_ocr(dev));
1625185721Smav	if (mmcbr_get_ocr(dev) == 0) {
1626261944Sian		device_printf(sc->dev, "No compatible cards found on bus\n");
1627185721Smav		mmc_delete_cards(sc);
1628185721Smav		mmc_power_down(sc);
1629163516Simp		return;
1630185721Smav	}
1631163516Simp	/*
1632163516Simp	 * Reselect the cards after we've idled them above.
1633163516Simp	 */
1634183704Smav	if (mmcbr_get_mode(dev) == mode_sd) {
1635183704Smav		err = mmc_send_if_cond(sc, 1);
1636183704Smav		mmc_send_app_op_cond(sc,
1637183775Simp		    (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1638183704Smav	} else
1639279359Sian		mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1640163516Simp	mmc_discover_cards(sc);
1641185721Smav	mmc_rescan_cards(sc);
1642163516Simp
1643163516Simp	mmcbr_set_bus_mode(dev, pushpull);
1644163516Simp	mmcbr_update_ios(dev);
1645183763Smav	mmc_calculate_clock(sc);
1646163516Simp	bus_generic_attach(dev);
1647183453Simp/*	mmc_update_children_sysctl(dev);*/
1648163516Simp}
1649163516Simp
1650163516Simpstatic int
1651163516Simpmmc_calculate_clock(struct mmc_softc *sc)
1652163516Simp{
1653183704Smav	int max_dtr, max_hs_dtr, max_timing;
1654254427Sian	int nkid, i, f_max;
1655163516Simp	device_t *kids;
1656183704Smav	struct mmc_ivars *ivar;
1657163516Simp
1658163516Simp	f_max = mmcbr_get_f_max(sc->dev);
1659183704Smav	max_dtr = max_hs_dtr = f_max;
1660183704Smav	if ((mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED))
1661183704Smav		max_timing = bus_timing_hs;
1662183704Smav	else
1663183704Smav		max_timing = bus_timing_normal;
1664163516Simp	if (device_get_children(sc->dev, &kids, &nkid) != 0)
1665163516Simp		panic("can't get children");
1666183704Smav	for (i = 0; i < nkid; i++) {
1667183704Smav		ivar = device_get_ivars(kids[i]);
1668183704Smav		if (ivar->timing < max_timing)
1669183704Smav			max_timing = ivar->timing;
1670183704Smav		if (ivar->tran_speed < max_dtr)
1671183704Smav			max_dtr = ivar->tran_speed;
1672187525Smav		if (ivar->hs_tran_speed < max_hs_dtr)
1673183704Smav			max_hs_dtr = ivar->hs_tran_speed;
1674183704Smav	}
1675183704Smav	for (i = 0; i < nkid; i++) {
1676183704Smav		ivar = device_get_ivars(kids[i]);
1677183704Smav		if (ivar->timing == bus_timing_normal)
1678183704Smav			continue;
1679183704Smav		mmc_select_card(sc, ivar->rca);
1680183704Smav		mmc_set_timing(sc, max_timing);
1681183704Smav	}
1682183704Smav	mmc_select_card(sc, 0);
1683163516Simp	free(kids, M_TEMP);
1684183704Smav	if (max_timing == bus_timing_hs)
1685183704Smav		max_dtr = max_hs_dtr;
1686187875Smav	if (bootverbose || mmc_debug) {
1687183775Simp		device_printf(sc->dev,
1688183775Simp		    "setting transfer rate to %d.%03dMHz%s\n",
1689183763Smav		    max_dtr / 1000000, (max_dtr / 1000) % 1000,
1690183775Simp		    max_timing == bus_timing_hs ? " (high speed timing)" : "");
1691183763Smav	}
1692183704Smav	mmcbr_set_timing(sc->dev, max_timing);
1693183704Smav	mmcbr_set_clock(sc->dev, max_dtr);
1694183704Smav	mmcbr_update_ios(sc->dev);
1695183704Smav	return max_dtr;
1696163516Simp}
1697163516Simp
1698163516Simpstatic void
1699163516Simpmmc_scan(struct mmc_softc *sc)
1700163516Simp{
1701185721Smav	device_t dev = sc->dev;
1702163516Simp
1703163516Simp	mmc_acquire_bus(dev, dev);
1704163516Simp	mmc_go_discovery(sc);
1705163516Simp	mmc_release_bus(dev, dev);
1706163516Simp}
1707163516Simp
1708163516Simpstatic int
1709189727Simpmmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1710163516Simp{
1711163516Simp	struct mmc_ivars *ivar = device_get_ivars(child);
1712163516Simp
1713163516Simp	switch (which) {
1714163516Simp	default:
1715163516Simp		return (EINVAL);
1716163516Simp	case MMC_IVAR_DSR_IMP:
1717222475Sjchandra		*result = ivar->csd.dsr_imp;
1718163516Simp		break;
1719163516Simp	case MMC_IVAR_MEDIA_SIZE:
1720222475Sjchandra		*result = ivar->sec_count;
1721163516Simp		break;
1722163516Simp	case MMC_IVAR_RCA:
1723222475Sjchandra		*result = ivar->rca;
1724163516Simp		break;
1725163516Simp	case MMC_IVAR_SECTOR_SIZE:
1726222475Sjchandra		*result = MMC_SECTOR_SIZE;
1727163516Simp		break;
1728163516Simp	case MMC_IVAR_TRAN_SPEED:
1729222475Sjchandra		*result = mmcbr_get_clock(bus);
1730163516Simp		break;
1731183447Simp	case MMC_IVAR_READ_ONLY:
1732222475Sjchandra		*result = ivar->read_only;
1733183447Simp		break;
1734183704Smav	case MMC_IVAR_HIGH_CAP:
1735222475Sjchandra		*result = ivar->high_cap;
1736183704Smav		break;
1737183763Smav	case MMC_IVAR_CARD_TYPE:
1738222475Sjchandra		*result = ivar->mode;
1739183763Smav		break;
1740183763Smav	case MMC_IVAR_BUS_WIDTH:
1741222475Sjchandra		*result = ivar->bus_width;
1742183763Smav		break;
1743184033Smav	case MMC_IVAR_ERASE_SECTOR:
1744222475Sjchandra		*result = ivar->erase_sector;
1745184033Smav		break;
1746184452Smav	case MMC_IVAR_MAX_DATA:
1747222475Sjchandra		*result = mmcbr_get_max_data(bus);
1748184452Smav		break;
1749234524Smarius	case MMC_IVAR_CARD_ID_STRING:
1750234524Smarius		*(char **)result = ivar->card_id_string;
1751234524Smarius		break;
1752269341Sian	case MMC_IVAR_CARD_SN_STRING:
1753269341Sian		*(char **)result = ivar->card_sn_string;
1754269341Sian		break;
1755163516Simp	}
1756163516Simp	return (0);
1757163516Simp}
1758163516Simp
1759163516Simpstatic int
1760163516Simpmmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1761163516Simp{
1762183453Simp	/*
1763183453Simp	 * None are writable ATM
1764183453Simp	 */
1765183453Simp	return (EINVAL);
1766163516Simp}
1767163516Simp
1768163516Simpstatic void
1769163516Simpmmc_delayed_attach(void *xsc)
1770163516Simp{
1771163516Simp	struct mmc_softc *sc = xsc;
1772163516Simp
1773163516Simp	mmc_scan(sc);
1774163516Simp	config_intrhook_disestablish(&sc->config_intrhook);
1775163516Simp}
1776163516Simp
1777208441Smavstatic int
1778208441Smavmmc_child_location_str(device_t dev, device_t child, char *buf,
1779208441Smav    size_t buflen)
1780208441Smav{
1781208441Smav
1782208441Smav	snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1783208441Smav	return (0);
1784208441Smav}
1785208441Smav
1786163516Simpstatic device_method_t mmc_methods[] = {
1787163516Simp	/* device_if */
1788163516Simp	DEVMETHOD(device_probe, mmc_probe),
1789163516Simp	DEVMETHOD(device_attach, mmc_attach),
1790163516Simp	DEVMETHOD(device_detach, mmc_detach),
1791185721Smav	DEVMETHOD(device_suspend, mmc_suspend),
1792185721Smav	DEVMETHOD(device_resume, mmc_resume),
1793163516Simp
1794163516Simp	/* Bus interface */
1795163516Simp	DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1796163516Simp	DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1797208441Smav	DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1798163516Simp
1799163516Simp	/* MMC Bus interface */
1800163516Simp	DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1801163516Simp	DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1802163516Simp	DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1803163516Simp
1804234524Smarius	DEVMETHOD_END
1805163516Simp};
1806163516Simp
1807163516Simpstatic driver_t mmc_driver = {
1808163516Simp	"mmc",
1809163516Simp	mmc_methods,
1810163516Simp	sizeof(struct mmc_softc),
1811163516Simp};
1812163516Simpstatic devclass_t mmc_devclass;
1813163516Simp
1814280905SganboldDRIVER_MODULE(mmc, aml8726_mmc, mmc_driver, mmc_devclass, NULL, NULL);
1815280905SganboldDRIVER_MODULE(mmc, aml8726_sdxc, mmc_driver, mmc_devclass, NULL, NULL);
1816188044SimpDRIVER_MODULE(mmc, at91_mci, mmc_driver, mmc_devclass, NULL, NULL);
1817242321SgonzoDRIVER_MODULE(mmc, sdhci_bcm, mmc_driver, mmc_devclass, NULL, NULL);
1818249999SwkoszekDRIVER_MODULE(mmc, sdhci_fdt, mmc_driver, mmc_devclass, NULL, NULL);
1819261424SianDRIVER_MODULE(mmc, sdhci_imx, mmc_driver, mmc_devclass, NULL, NULL);
1820261424SianDRIVER_MODULE(mmc, sdhci_pci, mmc_driver, mmc_devclass, NULL, NULL);
1821254559SianDRIVER_MODULE(mmc, sdhci_ti, mmc_driver, mmc_devclass, NULL, NULL);
1822261424SianDRIVER_MODULE(mmc, ti_mmchs, mmc_driver, mmc_devclass, NULL, NULL);
1823272712SbrDRIVER_MODULE(mmc, dwmmc, mmc_driver, mmc_devclass, NULL, NULL);
1824261424Sian
1825