mmc.c revision 318197
1/*-
2 * Copyright (c) 2006 Bernd Walter.  All rights reserved.
3 * Copyright (c) 2006 M. Warner Losh.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Portions of this software may have been developed with reference to
26 * the SD Simplified Specification.  The following disclaimer may apply:
27 *
28 * The following conditions apply to the release of the simplified
29 * specification ("Simplified Specification") by the SD Card Association and
30 * the SD Group. The Simplified Specification is a subset of the complete SD
31 * Specification which is owned by the SD Card Association and the SD
32 * Group. This Simplified Specification is provided on a non-confidential
33 * basis subject to the disclaimers below. Any implementation of the
34 * Simplified Specification may require a license from the SD Card
35 * Association, SD Group, SD-3C LLC or other third parties.
36 *
37 * Disclaimers:
38 *
39 * The information contained in the Simplified Specification is presented only
40 * as a standard specification for SD Cards and SD Host/Ancillary products and
41 * is provided "AS-IS" without any representations or warranties of any
42 * kind. No responsibility is assumed by the SD Group, SD-3C LLC or the SD
43 * Card Association for any damages, any infringements of patents or other
44 * right of the SD Group, SD-3C LLC, the SD Card Association or any third
45 * parties, which may result from its use. No license is granted by
46 * implication, estoppel or otherwise under any patent or other rights of the
47 * SD Group, SD-3C LLC, the SD Card Association or any third party. Nothing
48 * herein shall be construed as an obligation by the SD Group, the SD-3C LLC
49 * or the SD Card Association to disclose or distribute any technical
50 * information, know-how or other confidential information to any third party.
51 */
52
53#include <sys/cdefs.h>
54__FBSDID("$FreeBSD: stable/11/sys/dev/mmc/mmc.c 318197 2017-05-11 20:55:11Z marius $");
55
56#include <sys/param.h>
57#include <sys/systm.h>
58#include <sys/kernel.h>
59#include <sys/malloc.h>
60#include <sys/lock.h>
61#include <sys/module.h>
62#include <sys/mutex.h>
63#include <sys/bus.h>
64#include <sys/endian.h>
65#include <sys/sysctl.h>
66#include <sys/time.h>
67
68#include <dev/mmc/bridge.h>
69#include <dev/mmc/mmc_private.h>
70#include <dev/mmc/mmc_subr.h>
71#include <dev/mmc/mmcreg.h>
72#include <dev/mmc/mmcbrvar.h>
73#include <dev/mmc/mmcvar.h>
74
75#include "mmcbr_if.h"
76#include "mmcbus_if.h"
77
78/*
79 * Per-card data
80 */
81struct mmc_ivars {
82	uint32_t raw_cid[4];	/* Raw bits of the CID */
83	uint32_t raw_csd[4];	/* Raw bits of the CSD */
84	uint32_t raw_scr[2];	/* Raw bits of the SCR */
85	uint8_t raw_ext_csd[MMC_EXTCSD_SIZE]; /* Raw bits of the EXT_CSD */
86	uint32_t raw_sd_status[16];	/* Raw bits of the SD_STATUS */
87	uint16_t rca;
88	enum mmc_card_mode mode;
89	struct mmc_cid cid;	/* cid decoded */
90	struct mmc_csd csd;	/* csd decoded */
91	struct mmc_scr scr;	/* scr decoded */
92	struct mmc_sd_status sd_status;	/* SD_STATUS decoded */
93	u_char read_only;	/* True when the device is read-only */
94	u_char bus_width;	/* Bus width to use */
95	u_char timing;		/* Bus timing support */
96	u_char high_cap;	/* High Capacity card (block addressed) */
97	uint32_t sec_count;	/* Card capacity in 512byte blocks */
98	uint32_t tran_speed;	/* Max speed in normal mode */
99	uint32_t hs_tran_speed;	/* Max speed in high speed mode */
100	uint32_t erase_sector;	/* Card native erase sector size */
101	uint32_t cmd6_time;	/* Generic switch timeout [us] */
102	char card_id_string[64];/* Formatted CID info (serial, MFG, etc) */
103	char card_sn_string[16];/* Formatted serial # for disk->d_ident */
104};
105
106#define	CMD_RETRIES	3
107
108#define	CARD_ID_FREQUENCY 400000 /* Spec requires 400kHz max during ID phase. */
109
110static SYSCTL_NODE(_hw, OID_AUTO, mmc, CTLFLAG_RD, NULL, "mmc driver");
111
112static int mmc_debug;
113SYSCTL_INT(_hw_mmc, OID_AUTO, debug, CTLFLAG_RWTUN, &mmc_debug, 0,
114    "Debug level");
115
116/* bus entry points */
117static int mmc_acquire_bus(device_t busdev, device_t dev);
118static int mmc_attach(device_t dev);
119static int mmc_child_location_str(device_t dev, device_t child, char *buf,
120    size_t buflen);
121static int mmc_detach(device_t dev);
122static int mmc_probe(device_t dev);
123static int mmc_read_ivar(device_t bus, device_t child, int which,
124    uintptr_t *result);
125static int mmc_release_bus(device_t busdev, device_t dev);
126static int mmc_resume(device_t dev);
127static int mmc_suspend(device_t dev);
128static int mmc_wait_for_request(device_t brdev, device_t reqdev,
129    struct mmc_request *req);
130static int mmc_write_ivar(device_t bus, device_t child, int which,
131    uintptr_t value);
132
133#define	MMC_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
134#define	MMC_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
135#define	MMC_LOCK_INIT(_sc)						\
136	mtx_init(&(_sc)->sc_mtx, device_get_nameunit((_sc)->dev),	\
137	    "mmc", MTX_DEF)
138#define	MMC_LOCK_DESTROY(_sc)	mtx_destroy(&(_sc)->sc_mtx);
139#define	MMC_ASSERT_LOCKED(_sc)	mtx_assert(&(_sc)->sc_mtx, MA_OWNED);
140#define	MMC_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED);
141
142static int mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid);
143static void mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr);
144static void mmc_app_decode_sd_status(uint32_t *raw_sd_status,
145    struct mmc_sd_status *sd_status);
146static int mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca,
147    uint32_t *rawsdstatus);
148static int mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca,
149    uint32_t *rawscr);
150static int mmc_calculate_clock(struct mmc_softc *sc);
151static void mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid,
152    bool is_4_41p);
153static void mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid);
154static void mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd);
155static void mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd);
156static void mmc_delayed_attach(void *xsc);
157static int mmc_delete_cards(struct mmc_softc *sc);
158static void mmc_discover_cards(struct mmc_softc *sc);
159static void mmc_format_card_id_string(struct mmc_ivars *ivar);
160static void mmc_go_discovery(struct mmc_softc *sc);
161static uint32_t mmc_get_bits(uint32_t *bits, int bit_len, int start,
162    int size);
163static int mmc_highest_voltage(uint32_t ocr);
164static void mmc_idle_cards(struct mmc_softc *sc);
165static void mmc_ms_delay(int ms);
166static void mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard);
167static void mmc_power_down(struct mmc_softc *sc);
168static void mmc_power_up(struct mmc_softc *sc);
169static void mmc_rescan_cards(struct mmc_softc *sc);
170static void mmc_scan(struct mmc_softc *sc);
171static int mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp,
172    uint8_t value, uint8_t *res);
173static int mmc_select_card(struct mmc_softc *sc, uint16_t rca);
174static uint32_t mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr);
175static int mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr,
176    uint32_t *rocr);
177static int mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd);
178static int mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs);
179static int mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr,
180    uint32_t *rocr);
181static int mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp);
182static int mmc_set_blocklen(struct mmc_softc *sc, uint32_t len);
183static int mmc_set_card_bus_width(struct mmc_softc *sc,
184    struct mmc_ivars *ivar);
185static int mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp);
186static int mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar,
187    int timing);
188static int mmc_test_bus_width(struct mmc_softc *sc);
189static int mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
190    uint32_t arg, uint32_t flags, uint32_t *resp, int retries);
191static int mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req);
192static void mmc_wakeup(struct mmc_request *req);
193
194static void
195mmc_ms_delay(int ms)
196{
197
198	DELAY(1000 * ms);	/* XXX BAD */
199}
200
201static int
202mmc_probe(device_t dev)
203{
204
205	device_set_desc(dev, "MMC/SD bus");
206	return (0);
207}
208
209static int
210mmc_attach(device_t dev)
211{
212	struct mmc_softc *sc;
213
214	sc = device_get_softc(dev);
215	sc->dev = dev;
216	MMC_LOCK_INIT(sc);
217
218	/* We'll probe and attach our children later, but before / mount */
219	sc->config_intrhook.ich_func = mmc_delayed_attach;
220	sc->config_intrhook.ich_arg = sc;
221	if (config_intrhook_establish(&sc->config_intrhook) != 0)
222		device_printf(dev, "config_intrhook_establish failed\n");
223	return (0);
224}
225
226static int
227mmc_detach(device_t dev)
228{
229	struct mmc_softc *sc = device_get_softc(dev);
230	int err;
231
232	if ((err = mmc_delete_cards(sc)) != 0)
233		return (err);
234	mmc_power_down(sc);
235	MMC_LOCK_DESTROY(sc);
236
237	return (0);
238}
239
240static int
241mmc_suspend(device_t dev)
242{
243	struct mmc_softc *sc = device_get_softc(dev);
244	int err;
245
246	err = bus_generic_suspend(dev);
247	if (err)
248		return (err);
249	mmc_power_down(sc);
250	return (0);
251}
252
253static int
254mmc_resume(device_t dev)
255{
256	struct mmc_softc *sc = device_get_softc(dev);
257
258	mmc_scan(sc);
259	return (bus_generic_resume(dev));
260}
261
262static int
263mmc_acquire_bus(device_t busdev, device_t dev)
264{
265	struct mmc_softc *sc;
266	struct mmc_ivars *ivar;
267	int err;
268	int rca;
269
270	err = MMCBR_ACQUIRE_HOST(device_get_parent(busdev), busdev);
271	if (err)
272		return (err);
273	sc = device_get_softc(busdev);
274	MMC_LOCK(sc);
275	if (sc->owner)
276		panic("mmc: host bridge didn't serialize us.");
277	sc->owner = dev;
278	MMC_UNLOCK(sc);
279
280	if (busdev != dev) {
281		/*
282		 * Keep track of the last rca that we've selected.  If
283		 * we're asked to do it again, don't.  We never
284		 * unselect unless the bus code itself wants the mmc
285		 * bus, and constantly reselecting causes problems.
286		 */
287		ivar = device_get_ivars(dev);
288		rca = ivar->rca;
289		if (sc->last_rca != rca) {
290			mmc_select_card(sc, rca);
291			sc->last_rca = rca;
292			/* Prepare bus width for the new card. */
293			if (bootverbose || mmc_debug) {
294				device_printf(busdev,
295				    "setting bus width to %d bits\n",
296				    (ivar->bus_width == bus_width_4) ? 4 :
297				    (ivar->bus_width == bus_width_8) ? 8 : 1);
298			}
299			mmc_set_card_bus_width(sc, ivar);
300			mmcbr_set_bus_width(busdev, ivar->bus_width);
301			mmcbr_update_ios(busdev);
302		}
303	} else {
304		/*
305		 * If there's a card selected, stand down.
306		 */
307		if (sc->last_rca != 0) {
308			mmc_select_card(sc, 0);
309			sc->last_rca = 0;
310		}
311	}
312
313	return (0);
314}
315
316static int
317mmc_release_bus(device_t busdev, device_t dev)
318{
319	struct mmc_softc *sc;
320	int err;
321
322	sc = device_get_softc(busdev);
323
324	MMC_LOCK(sc);
325	if (!sc->owner)
326		panic("mmc: releasing unowned bus.");
327	if (sc->owner != dev)
328		panic("mmc: you don't own the bus.  game over.");
329	MMC_UNLOCK(sc);
330	err = MMCBR_RELEASE_HOST(device_get_parent(busdev), busdev);
331	if (err)
332		return (err);
333	MMC_LOCK(sc);
334	sc->owner = NULL;
335	MMC_UNLOCK(sc);
336	return (0);
337}
338
339static uint32_t
340mmc_select_vdd(struct mmc_softc *sc, uint32_t ocr)
341{
342
343	return (ocr & MMC_OCR_VOLTAGE);
344}
345
346static int
347mmc_highest_voltage(uint32_t ocr)
348{
349	int i;
350
351	for (i = MMC_OCR_MAX_VOLTAGE_SHIFT;
352	    i >= MMC_OCR_MIN_VOLTAGE_SHIFT; i--)
353		if (ocr & (1 << i))
354			return (i);
355	return (-1);
356}
357
358static void
359mmc_wakeup(struct mmc_request *req)
360{
361	struct mmc_softc *sc;
362
363	sc = (struct mmc_softc *)req->done_data;
364	MMC_LOCK(sc);
365	req->flags |= MMC_REQ_DONE;
366	MMC_UNLOCK(sc);
367	wakeup(req);
368}
369
370static int
371mmc_wait_for_req(struct mmc_softc *sc, struct mmc_request *req)
372{
373
374	req->done = mmc_wakeup;
375	req->done_data = sc;
376	if (mmc_debug > 1) {
377		device_printf(sc->dev, "REQUEST: CMD%d arg %#x flags %#x",
378		    req->cmd->opcode, req->cmd->arg, req->cmd->flags);
379		if (req->cmd->data) {
380			printf(" data %d\n", (int)req->cmd->data->len);
381		} else
382			printf("\n");
383	}
384	MMCBR_REQUEST(device_get_parent(sc->dev), sc->dev, req);
385	MMC_LOCK(sc);
386	while ((req->flags & MMC_REQ_DONE) == 0)
387		msleep(req, &sc->sc_mtx, 0, "mmcreq", 0);
388	MMC_UNLOCK(sc);
389	if (mmc_debug > 2 || (mmc_debug > 0 && req->cmd->error != MMC_ERR_NONE))
390		device_printf(sc->dev, "CMD%d RESULT: %d\n",
391		    req->cmd->opcode, req->cmd->error);
392	return (0);
393}
394
395static int
396mmc_wait_for_request(device_t brdev, device_t reqdev __unused,
397    struct mmc_request *req)
398{
399	struct mmc_softc *sc = device_get_softc(brdev);
400
401	return (mmc_wait_for_req(sc, req));
402}
403
404static int
405mmc_wait_for_command(struct mmc_softc *sc, uint32_t opcode,
406    uint32_t arg, uint32_t flags, uint32_t *resp, int retries)
407{
408	struct mmc_command cmd;
409	int err;
410
411	memset(&cmd, 0, sizeof(cmd));
412	cmd.opcode = opcode;
413	cmd.arg = arg;
414	cmd.flags = flags;
415	cmd.data = NULL;
416	err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, retries);
417	if (err)
418		return (err);
419	if (resp) {
420		if (flags & MMC_RSP_136)
421			memcpy(resp, cmd.resp, 4 * sizeof(uint32_t));
422		else
423			*resp = cmd.resp[0];
424	}
425	return (0);
426}
427
428static void
429mmc_idle_cards(struct mmc_softc *sc)
430{
431	device_t dev;
432	struct mmc_command cmd;
433
434	dev = sc->dev;
435	mmcbr_set_chip_select(dev, cs_high);
436	mmcbr_update_ios(dev);
437	mmc_ms_delay(1);
438
439	memset(&cmd, 0, sizeof(cmd));
440	cmd.opcode = MMC_GO_IDLE_STATE;
441	cmd.arg = 0;
442	cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
443	cmd.data = NULL;
444	mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
445	mmc_ms_delay(1);
446
447	mmcbr_set_chip_select(dev, cs_dontcare);
448	mmcbr_update_ios(dev);
449	mmc_ms_delay(1);
450}
451
452static int
453mmc_send_app_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
454{
455	struct mmc_command cmd;
456	int err = MMC_ERR_NONE, i;
457
458	memset(&cmd, 0, sizeof(cmd));
459	cmd.opcode = ACMD_SD_SEND_OP_COND;
460	cmd.arg = ocr;
461	cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
462	cmd.data = NULL;
463
464	for (i = 0; i < 1000; i++) {
465		err = mmc_wait_for_app_cmd(sc->dev, sc->dev, 0, &cmd,
466		    CMD_RETRIES);
467		if (err != MMC_ERR_NONE)
468			break;
469		if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
470		    (ocr & MMC_OCR_VOLTAGE) == 0)
471			break;
472		err = MMC_ERR_TIMEOUT;
473		mmc_ms_delay(10);
474	}
475	if (rocr && err == MMC_ERR_NONE)
476		*rocr = cmd.resp[0];
477	return (err);
478}
479
480static int
481mmc_send_op_cond(struct mmc_softc *sc, uint32_t ocr, uint32_t *rocr)
482{
483	struct mmc_command cmd;
484	int err = MMC_ERR_NONE, i;
485
486	memset(&cmd, 0, sizeof(cmd));
487	cmd.opcode = MMC_SEND_OP_COND;
488	cmd.arg = ocr;
489	cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
490	cmd.data = NULL;
491
492	for (i = 0; i < 1000; i++) {
493		err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
494		if (err != MMC_ERR_NONE)
495			break;
496		if ((cmd.resp[0] & MMC_OCR_CARD_BUSY) ||
497		    (ocr & MMC_OCR_VOLTAGE) == 0)
498			break;
499		err = MMC_ERR_TIMEOUT;
500		mmc_ms_delay(10);
501	}
502	if (rocr && err == MMC_ERR_NONE)
503		*rocr = cmd.resp[0];
504	return (err);
505}
506
507static int
508mmc_send_if_cond(struct mmc_softc *sc, uint8_t vhs)
509{
510	struct mmc_command cmd;
511	int err;
512
513	memset(&cmd, 0, sizeof(cmd));
514	cmd.opcode = SD_SEND_IF_COND;
515	cmd.arg = (vhs << 8) + 0xAA;
516	cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
517	cmd.data = NULL;
518
519	err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
520	return (err);
521}
522
523static void
524mmc_power_up(struct mmc_softc *sc)
525{
526	device_t dev;
527
528	dev = sc->dev;
529	mmcbr_set_vdd(dev, mmc_highest_voltage(mmcbr_get_host_ocr(dev)));
530	mmcbr_set_bus_mode(dev, opendrain);
531	mmcbr_set_chip_select(dev, cs_dontcare);
532	mmcbr_set_bus_width(dev, bus_width_1);
533	mmcbr_set_power_mode(dev, power_up);
534	mmcbr_set_clock(dev, 0);
535	mmcbr_update_ios(dev);
536	mmc_ms_delay(1);
537
538	mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
539	mmcbr_set_timing(dev, bus_timing_normal);
540	mmcbr_set_power_mode(dev, power_on);
541	mmcbr_update_ios(dev);
542	mmc_ms_delay(2);
543}
544
545static void
546mmc_power_down(struct mmc_softc *sc)
547{
548	device_t dev = sc->dev;
549
550	mmcbr_set_bus_mode(dev, opendrain);
551	mmcbr_set_chip_select(dev, cs_dontcare);
552	mmcbr_set_bus_width(dev, bus_width_1);
553	mmcbr_set_power_mode(dev, power_off);
554	mmcbr_set_clock(dev, 0);
555	mmcbr_set_timing(dev, bus_timing_normal);
556	mmcbr_update_ios(dev);
557}
558
559static int
560mmc_select_card(struct mmc_softc *sc, uint16_t rca)
561{
562	int flags;
563
564	flags = (rca ? MMC_RSP_R1B : MMC_RSP_NONE) | MMC_CMD_AC;
565	return (mmc_wait_for_command(sc, MMC_SELECT_CARD, (uint32_t)rca << 16,
566	    flags, NULL, CMD_RETRIES));
567}
568
569static int
570mmc_sd_switch(struct mmc_softc *sc, uint8_t mode, uint8_t grp, uint8_t value,
571    uint8_t *res)
572{
573	int err;
574	struct mmc_command cmd;
575	struct mmc_data data;
576
577	memset(&cmd, 0, sizeof(cmd));
578	memset(&data, 0, sizeof(data));
579	memset(res, 0, 64);
580
581	cmd.opcode = SD_SWITCH_FUNC;
582	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
583	cmd.arg = mode << 31;			/* 0 - check, 1 - set */
584	cmd.arg |= 0x00FFFFFF;
585	cmd.arg &= ~(0xF << (grp * 4));
586	cmd.arg |= value << (grp * 4);
587	cmd.data = &data;
588
589	data.data = res;
590	data.len = 64;
591	data.flags = MMC_DATA_READ;
592
593	err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
594	return (err);
595}
596
597static int
598mmc_set_card_bus_width(struct mmc_softc *sc, struct mmc_ivars *ivar)
599{
600	struct mmc_command cmd;
601	int err;
602	uint8_t	value;
603
604	if (mmcbr_get_mode(sc->dev) == mode_sd) {
605		memset(&cmd, 0, sizeof(cmd));
606		cmd.opcode = ACMD_SET_CLR_CARD_DETECT;
607		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
608		cmd.arg = SD_CLR_CARD_DETECT;
609		err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd,
610		    CMD_RETRIES);
611		if (err != 0)
612			return (err);
613		memset(&cmd, 0, sizeof(cmd));
614		cmd.opcode = ACMD_SET_BUS_WIDTH;
615		cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
616		switch (ivar->bus_width) {
617		case bus_width_1:
618			cmd.arg = SD_BUS_WIDTH_1;
619			break;
620		case bus_width_4:
621			cmd.arg = SD_BUS_WIDTH_4;
622			break;
623		default:
624			return (MMC_ERR_INVALID);
625		}
626		err = mmc_wait_for_app_cmd(sc->dev, sc->dev, ivar->rca, &cmd,
627		    CMD_RETRIES);
628	} else {
629		switch (ivar->bus_width) {
630		case bus_width_1:
631			value = EXT_CSD_BUS_WIDTH_1;
632			break;
633		case bus_width_4:
634			value = EXT_CSD_BUS_WIDTH_4;
635			break;
636		case bus_width_8:
637			value = EXT_CSD_BUS_WIDTH_8;
638			break;
639		default:
640			return (MMC_ERR_INVALID);
641		}
642		err = mmc_switch(sc->dev, sc->dev, ivar->rca,
643		    EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH, value,
644		    ivar->cmd6_time, true);
645	}
646	return (err);
647}
648
649static int
650mmc_set_timing(struct mmc_softc *sc, struct mmc_ivars *ivar, int timing)
651{
652	u_char switch_res[64];
653	uint8_t	value;
654	int err;
655
656	switch (timing) {
657	case bus_timing_normal:
658		value = 0;
659		break;
660	case bus_timing_hs:
661		value = 1;
662		break;
663	default:
664		return (MMC_ERR_INVALID);
665	}
666	if (mmcbr_get_mode(sc->dev) == mode_sd) {
667		err = mmc_sd_switch(sc, SD_SWITCH_MODE_SET, SD_SWITCH_GROUP1,
668		    value, switch_res);
669		if (err != MMC_ERR_NONE)
670			return (err);
671		if ((switch_res[16] & 0xf) != value)
672			return (MMC_ERR_FAILED);
673		mmcbr_set_timing(sc->dev, timing);
674		mmcbr_update_ios(sc->dev);
675	} else {
676		err = mmc_switch(sc->dev, sc->dev, ivar->rca,
677		    EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, value,
678		    ivar->cmd6_time, false);
679		if (err != MMC_ERR_NONE)
680			return (err);
681		mmcbr_set_timing(sc->dev, timing);
682		mmcbr_update_ios(sc->dev);
683		err = mmc_switch_status(sc->dev, sc->dev, ivar->rca,
684		    ivar->cmd6_time);
685	}
686	return (err);
687}
688
689static const uint8_t p8[8] = {
690	0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
691};
692
693static const uint8_t p8ok[8] = {
694	0xAA, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
695};
696
697static const uint8_t p4[4] = {
698	0x5A, 0x00, 0x00, 0x00
699};
700
701static const uint8_t p4ok[4] = {
702	0xA5, 0x00, 0x00, 0x00
703};
704
705static int
706mmc_test_bus_width(struct mmc_softc *sc)
707{
708	struct mmc_command cmd;
709	struct mmc_data data;
710	uint8_t buf[8];
711	int err;
712
713	if (mmcbr_get_caps(sc->dev) & MMC_CAP_8_BIT_DATA) {
714		mmcbr_set_bus_width(sc->dev, bus_width_8);
715		mmcbr_update_ios(sc->dev);
716
717		sc->squelched++; /* Errors are expected, squelch reporting. */
718		memset(&cmd, 0, sizeof(cmd));
719		memset(&data, 0, sizeof(data));
720		cmd.opcode = MMC_BUSTEST_W;
721		cmd.arg = 0;
722		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
723		cmd.data = &data;
724
725		data.data = __DECONST(void *, p8);
726		data.len = 8;
727		data.flags = MMC_DATA_WRITE;
728		mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
729
730		memset(&cmd, 0, sizeof(cmd));
731		memset(&data, 0, sizeof(data));
732		cmd.opcode = MMC_BUSTEST_R;
733		cmd.arg = 0;
734		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
735		cmd.data = &data;
736
737		data.data = buf;
738		data.len = 8;
739		data.flags = MMC_DATA_READ;
740		err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
741		sc->squelched--;
742
743		mmcbr_set_bus_width(sc->dev, bus_width_1);
744		mmcbr_update_ios(sc->dev);
745
746		if (err == MMC_ERR_NONE && memcmp(buf, p8ok, 8) == 0)
747			return (bus_width_8);
748	}
749
750	if (mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) {
751		mmcbr_set_bus_width(sc->dev, bus_width_4);
752		mmcbr_update_ios(sc->dev);
753
754		sc->squelched++; /* Errors are expected, squelch reporting. */
755		memset(&cmd, 0, sizeof(cmd));
756		memset(&data, 0, sizeof(data));
757		cmd.opcode = MMC_BUSTEST_W;
758		cmd.arg = 0;
759		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
760		cmd.data = &data;
761
762		data.data = __DECONST(void *, p4);
763		data.len = 4;
764		data.flags = MMC_DATA_WRITE;
765		mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
766
767		memset(&cmd, 0, sizeof(cmd));
768		memset(&data, 0, sizeof(data));
769		cmd.opcode = MMC_BUSTEST_R;
770		cmd.arg = 0;
771		cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
772		cmd.data = &data;
773
774		data.data = buf;
775		data.len = 4;
776		data.flags = MMC_DATA_READ;
777		err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, 0);
778		sc->squelched--;
779
780		mmcbr_set_bus_width(sc->dev, bus_width_1);
781		mmcbr_update_ios(sc->dev);
782
783		if (err == MMC_ERR_NONE && memcmp(buf, p4ok, 4) == 0)
784			return (bus_width_4);
785	}
786	return (bus_width_1);
787}
788
789static uint32_t
790mmc_get_bits(uint32_t *bits, int bit_len, int start, int size)
791{
792	const int i = (bit_len / 32) - (start / 32) - 1;
793	const int shift = start & 31;
794	uint32_t retval = bits[i] >> shift;
795
796	if (size + shift > 32)
797		retval |= bits[i - 1] << (32 - shift);
798	return (retval & ((1llu << size) - 1));
799}
800
801static void
802mmc_decode_cid_sd(uint32_t *raw_cid, struct mmc_cid *cid)
803{
804	int i;
805
806	/* There's no version info, so we take it on faith */
807	memset(cid, 0, sizeof(*cid));
808	cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
809	cid->oid = mmc_get_bits(raw_cid, 128, 104, 16);
810	for (i = 0; i < 5; i++)
811		cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
812	cid->pnm[5] = 0;
813	cid->prv = mmc_get_bits(raw_cid, 128, 56, 8);
814	cid->psn = mmc_get_bits(raw_cid, 128, 24, 32);
815	cid->mdt_year = mmc_get_bits(raw_cid, 128, 12, 8) + 2000;
816	cid->mdt_month = mmc_get_bits(raw_cid, 128, 8, 4);
817}
818
819static void
820mmc_decode_cid_mmc(uint32_t *raw_cid, struct mmc_cid *cid, bool is_4_41p)
821{
822	int i;
823
824	/* There's no version info, so we take it on faith */
825	memset(cid, 0, sizeof(*cid));
826	cid->mid = mmc_get_bits(raw_cid, 128, 120, 8);
827	cid->oid = mmc_get_bits(raw_cid, 128, 104, 8);
828	for (i = 0; i < 6; i++)
829		cid->pnm[i] = mmc_get_bits(raw_cid, 128, 96 - i * 8, 8);
830	cid->pnm[6] = 0;
831	cid->prv = mmc_get_bits(raw_cid, 128, 48, 8);
832	cid->psn = mmc_get_bits(raw_cid, 128, 16, 32);
833	cid->mdt_month = mmc_get_bits(raw_cid, 128, 12, 4);
834	cid->mdt_year = mmc_get_bits(raw_cid, 128, 8, 4);
835	if (is_4_41p)
836		cid->mdt_year += 2013;
837	else
838		cid->mdt_year += 1997;
839}
840
841static void
842mmc_format_card_id_string(struct mmc_ivars *ivar)
843{
844	char oidstr[8];
845	uint8_t c1;
846	uint8_t c2;
847
848	/*
849	 * Format a card ID string for use by the mmcsd driver, it's what
850	 * appears between the <> in the following:
851	 * mmcsd0: 968MB <SD SD01G 8.0 SN 2686905 Mfg 08/2008 by 3 TN> at mmc0
852	 * 22.5MHz/4bit/128-block
853	 *
854	 * Also format just the card serial number, which the mmcsd driver will
855	 * use as the disk->d_ident string.
856	 *
857	 * The card_id_string in mmc_ivars is currently allocated as 64 bytes,
858	 * and our max formatted length is currently 55 bytes if every field
859	 * contains the largest value.
860	 *
861	 * Sometimes the oid is two printable ascii chars; when it's not,
862	 * format it as 0xnnnn instead.
863	 */
864	c1 = (ivar->cid.oid >> 8) & 0x0ff;
865	c2 = ivar->cid.oid & 0x0ff;
866	if (c1 > 0x1f && c1 < 0x7f && c2 > 0x1f && c2 < 0x7f)
867		snprintf(oidstr, sizeof(oidstr), "%c%c", c1, c2);
868	else
869		snprintf(oidstr, sizeof(oidstr), "0x%04x", ivar->cid.oid);
870	snprintf(ivar->card_sn_string, sizeof(ivar->card_sn_string),
871	    "%08X", ivar->cid.psn);
872	snprintf(ivar->card_id_string, sizeof(ivar->card_id_string),
873	    "%s%s %s %d.%d SN %08X MFG %02d/%04d by %d %s",
874	    ivar->mode == mode_sd ? "SD" : "MMC", ivar->high_cap ? "HC" : "",
875	    ivar->cid.pnm, ivar->cid.prv >> 4, ivar->cid.prv & 0x0f,
876	    ivar->cid.psn, ivar->cid.mdt_month, ivar->cid.mdt_year,
877	    ivar->cid.mid, oidstr);
878}
879
880static const int exp[8] = {
881	1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
882};
883
884static const int mant[16] = {
885	0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
886};
887
888static const int cur_min[8] = {
889	500, 1000, 5000, 10000, 25000, 35000, 60000, 100000
890};
891
892static const int cur_max[8] = {
893	1000, 5000, 10000, 25000, 35000, 45000, 800000, 200000
894};
895
896static void
897mmc_decode_csd_sd(uint32_t *raw_csd, struct mmc_csd *csd)
898{
899	int v;
900	int m;
901	int e;
902
903	memset(csd, 0, sizeof(*csd));
904	csd->csd_structure = v = mmc_get_bits(raw_csd, 128, 126, 2);
905	if (v == 0) {
906		m = mmc_get_bits(raw_csd, 128, 115, 4);
907		e = mmc_get_bits(raw_csd, 128, 112, 3);
908		csd->tacc = (exp[e] * mant[m] + 9) / 10;
909		csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
910		m = mmc_get_bits(raw_csd, 128, 99, 4);
911		e = mmc_get_bits(raw_csd, 128, 96, 3);
912		csd->tran_speed = exp[e] * 10000 * mant[m];
913		csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
914		csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
915		csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
916		csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
917		csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
918		csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
919		csd->vdd_r_curr_min =
920		    cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
921		csd->vdd_r_curr_max =
922		    cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
923		csd->vdd_w_curr_min =
924		    cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
925		csd->vdd_w_curr_max =
926		    cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
927		m = mmc_get_bits(raw_csd, 128, 62, 12);
928		e = mmc_get_bits(raw_csd, 128, 47, 3);
929		csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
930		csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
931		csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
932		csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
933		csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
934		csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
935		csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
936		csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
937	} else if (v == 1) {
938		m = mmc_get_bits(raw_csd, 128, 115, 4);
939		e = mmc_get_bits(raw_csd, 128, 112, 3);
940		csd->tacc = (exp[e] * mant[m] + 9) / 10;
941		csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
942		m = mmc_get_bits(raw_csd, 128, 99, 4);
943		e = mmc_get_bits(raw_csd, 128, 96, 3);
944		csd->tran_speed = exp[e] * 10000 * mant[m];
945		csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
946		csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
947		csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
948		csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
949		csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
950		csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
951		csd->capacity = ((uint64_t)mmc_get_bits(raw_csd, 128, 48, 22) +
952		    1) * 512 * 1024;
953		csd->erase_blk_en = mmc_get_bits(raw_csd, 128, 46, 1);
954		csd->erase_sector = mmc_get_bits(raw_csd, 128, 39, 7) + 1;
955		csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 7);
956		csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
957		csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
958		csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
959		csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
960	} else
961		panic("unknown SD CSD version");
962}
963
964static void
965mmc_decode_csd_mmc(uint32_t *raw_csd, struct mmc_csd *csd)
966{
967	int m;
968	int e;
969
970	memset(csd, 0, sizeof(*csd));
971	csd->csd_structure = mmc_get_bits(raw_csd, 128, 126, 2);
972	csd->spec_vers = mmc_get_bits(raw_csd, 128, 122, 4);
973	m = mmc_get_bits(raw_csd, 128, 115, 4);
974	e = mmc_get_bits(raw_csd, 128, 112, 3);
975	csd->tacc = exp[e] * mant[m] + 9 / 10;
976	csd->nsac = mmc_get_bits(raw_csd, 128, 104, 8) * 100;
977	m = mmc_get_bits(raw_csd, 128, 99, 4);
978	e = mmc_get_bits(raw_csd, 128, 96, 3);
979	csd->tran_speed = exp[e] * 10000 * mant[m];
980	csd->ccc = mmc_get_bits(raw_csd, 128, 84, 12);
981	csd->read_bl_len = 1 << mmc_get_bits(raw_csd, 128, 80, 4);
982	csd->read_bl_partial = mmc_get_bits(raw_csd, 128, 79, 1);
983	csd->write_blk_misalign = mmc_get_bits(raw_csd, 128, 78, 1);
984	csd->read_blk_misalign = mmc_get_bits(raw_csd, 128, 77, 1);
985	csd->dsr_imp = mmc_get_bits(raw_csd, 128, 76, 1);
986	csd->vdd_r_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 59, 3)];
987	csd->vdd_r_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 56, 3)];
988	csd->vdd_w_curr_min = cur_min[mmc_get_bits(raw_csd, 128, 53, 3)];
989	csd->vdd_w_curr_max = cur_max[mmc_get_bits(raw_csd, 128, 50, 3)];
990	m = mmc_get_bits(raw_csd, 128, 62, 12);
991	e = mmc_get_bits(raw_csd, 128, 47, 3);
992	csd->capacity = ((1 + m) << (e + 2)) * csd->read_bl_len;
993	csd->erase_blk_en = 0;
994	csd->erase_sector = (mmc_get_bits(raw_csd, 128, 42, 5) + 1) *
995	    (mmc_get_bits(raw_csd, 128, 37, 5) + 1);
996	csd->wp_grp_size = mmc_get_bits(raw_csd, 128, 32, 5);
997	csd->wp_grp_enable = mmc_get_bits(raw_csd, 128, 31, 1);
998	csd->r2w_factor = 1 << mmc_get_bits(raw_csd, 128, 26, 3);
999	csd->write_bl_len = 1 << mmc_get_bits(raw_csd, 128, 22, 4);
1000	csd->write_bl_partial = mmc_get_bits(raw_csd, 128, 21, 1);
1001}
1002
1003static void
1004mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
1005{
1006	unsigned int scr_struct;
1007
1008	memset(scr, 0, sizeof(*scr));
1009
1010	scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
1011	if (scr_struct != 0) {
1012		printf("Unrecognised SCR structure version %d\n",
1013		    scr_struct);
1014		return;
1015	}
1016	scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
1017	scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
1018}
1019
1020static void
1021mmc_app_decode_sd_status(uint32_t *raw_sd_status,
1022    struct mmc_sd_status *sd_status)
1023{
1024
1025	memset(sd_status, 0, sizeof(*sd_status));
1026
1027	sd_status->bus_width = mmc_get_bits(raw_sd_status, 512, 510, 2);
1028	sd_status->secured_mode = mmc_get_bits(raw_sd_status, 512, 509, 1);
1029	sd_status->card_type = mmc_get_bits(raw_sd_status, 512, 480, 16);
1030	sd_status->prot_area = mmc_get_bits(raw_sd_status, 512, 448, 12);
1031	sd_status->speed_class = mmc_get_bits(raw_sd_status, 512, 440, 8);
1032	sd_status->perf_move = mmc_get_bits(raw_sd_status, 512, 432, 8);
1033	sd_status->au_size = mmc_get_bits(raw_sd_status, 512, 428, 4);
1034	sd_status->erase_size = mmc_get_bits(raw_sd_status, 512, 408, 16);
1035	sd_status->erase_timeout = mmc_get_bits(raw_sd_status, 512, 402, 6);
1036	sd_status->erase_offset = mmc_get_bits(raw_sd_status, 512, 400, 2);
1037}
1038
1039static int
1040mmc_all_send_cid(struct mmc_softc *sc, uint32_t *rawcid)
1041{
1042	struct mmc_command cmd;
1043	int err;
1044
1045	memset(&cmd, 0, sizeof(cmd));
1046	cmd.opcode = MMC_ALL_SEND_CID;
1047	cmd.arg = 0;
1048	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1049	cmd.data = NULL;
1050	err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1051	memcpy(rawcid, cmd.resp, 4 * sizeof(uint32_t));
1052	return (err);
1053}
1054
1055static int
1056mmc_send_csd(struct mmc_softc *sc, uint16_t rca, uint32_t *rawcsd)
1057{
1058	struct mmc_command cmd;
1059	int err;
1060
1061	memset(&cmd, 0, sizeof(cmd));
1062	cmd.opcode = MMC_SEND_CSD;
1063	cmd.arg = rca << 16;
1064	cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
1065	cmd.data = NULL;
1066	err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1067	memcpy(rawcsd, cmd.resp, 4 * sizeof(uint32_t));
1068	return (err);
1069}
1070
1071static int
1072mmc_app_send_scr(struct mmc_softc *sc, uint16_t rca, uint32_t *rawscr)
1073{
1074	int err;
1075	struct mmc_command cmd;
1076	struct mmc_data data;
1077
1078	memset(&cmd, 0, sizeof(cmd));
1079	memset(&data, 0, sizeof(data));
1080
1081	memset(rawscr, 0, 8);
1082	cmd.opcode = ACMD_SEND_SCR;
1083	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1084	cmd.arg = 0;
1085	cmd.data = &data;
1086
1087	data.data = rawscr;
1088	data.len = 8;
1089	data.flags = MMC_DATA_READ;
1090
1091	err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES);
1092	rawscr[0] = be32toh(rawscr[0]);
1093	rawscr[1] = be32toh(rawscr[1]);
1094	return (err);
1095}
1096
1097static int
1098mmc_app_sd_status(struct mmc_softc *sc, uint16_t rca, uint32_t *rawsdstatus)
1099{
1100	struct mmc_command cmd;
1101	struct mmc_data data;
1102	int err, i;
1103
1104	memset(&cmd, 0, sizeof(cmd));
1105	memset(&data, 0, sizeof(data));
1106
1107	memset(rawsdstatus, 0, 64);
1108	cmd.opcode = ACMD_SD_STATUS;
1109	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1110	cmd.arg = 0;
1111	cmd.data = &data;
1112
1113	data.data = rawsdstatus;
1114	data.len = 64;
1115	data.flags = MMC_DATA_READ;
1116
1117	err = mmc_wait_for_app_cmd(sc->dev, sc->dev, rca, &cmd, CMD_RETRIES);
1118	for (i = 0; i < 16; i++)
1119	    rawsdstatus[i] = be32toh(rawsdstatus[i]);
1120	return (err);
1121}
1122
1123static int
1124mmc_set_relative_addr(struct mmc_softc *sc, uint16_t resp)
1125{
1126	struct mmc_command cmd;
1127	int err;
1128
1129	memset(&cmd, 0, sizeof(cmd));
1130	cmd.opcode = MMC_SET_RELATIVE_ADDR;
1131	cmd.arg = resp << 16;
1132	cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1133	cmd.data = NULL;
1134	err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1135	return (err);
1136}
1137
1138static int
1139mmc_send_relative_addr(struct mmc_softc *sc, uint32_t *resp)
1140{
1141	struct mmc_command cmd;
1142	int err;
1143
1144	memset(&cmd, 0, sizeof(cmd));
1145	cmd.opcode = SD_SEND_RELATIVE_ADDR;
1146	cmd.arg = 0;
1147	cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
1148	cmd.data = NULL;
1149	err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1150	*resp = cmd.resp[0];
1151	return (err);
1152}
1153
1154static int
1155mmc_set_blocklen(struct mmc_softc *sc, uint32_t len)
1156{
1157	struct mmc_command cmd;
1158	int err;
1159
1160	memset(&cmd, 0, sizeof(cmd));
1161	cmd.opcode = MMC_SET_BLOCKLEN;
1162	cmd.arg = len;
1163	cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1164	cmd.data = NULL;
1165	err = mmc_wait_for_cmd(sc->dev, sc->dev, &cmd, CMD_RETRIES);
1166	return (err);
1167}
1168
1169static void
1170mmc_log_card(device_t dev, struct mmc_ivars *ivar, int newcard)
1171{
1172
1173	device_printf(dev, "Card at relative address 0x%04x%s:\n",
1174	    ivar->rca, newcard ? " added" : "");
1175	device_printf(dev, " card: %s\n", ivar->card_id_string);
1176	device_printf(dev, " bus: %ubit, %uMHz%s\n",
1177	    (ivar->bus_width == bus_width_1 ? 1 :
1178	    (ivar->bus_width == bus_width_4 ? 4 : 8)),
1179	    (ivar->timing == bus_timing_hs ?
1180		ivar->hs_tran_speed : ivar->tran_speed) / 1000000,
1181	    ivar->timing == bus_timing_hs ? ", high speed timing" : "");
1182	device_printf(dev, " memory: %u blocks, erase sector %u blocks%s\n",
1183	    ivar->sec_count, ivar->erase_sector,
1184	    ivar->read_only ? ", read-only" : "");
1185}
1186
1187static void
1188mmc_discover_cards(struct mmc_softc *sc)
1189{
1190	u_char switch_res[64];
1191	uint32_t raw_cid[4];
1192	struct mmc_ivars *ivar = NULL;
1193	device_t *devlist;
1194	device_t child;
1195	int err, i, devcount, newcard;
1196	uint32_t resp, sec_count, status;
1197	uint16_t rca = 2;
1198
1199	if (bootverbose || mmc_debug)
1200		device_printf(sc->dev, "Probing cards\n");
1201	while (1) {
1202		sc->squelched++; /* Errors are expected, squelch reporting. */
1203		err = mmc_all_send_cid(sc, raw_cid);
1204		sc->squelched--;
1205		if (err == MMC_ERR_TIMEOUT)
1206			break;
1207		if (err != MMC_ERR_NONE) {
1208			device_printf(sc->dev, "Error reading CID %d\n", err);
1209			break;
1210		}
1211		newcard = 1;
1212		if ((err = device_get_children(sc->dev, &devlist,
1213		    &devcount)) != 0)
1214			return;
1215		for (i = 0; i < devcount; i++) {
1216			ivar = device_get_ivars(devlist[i]);
1217			if (memcmp(ivar->raw_cid, raw_cid, sizeof(raw_cid)) ==
1218			    0) {
1219				newcard = 0;
1220				break;
1221			}
1222		}
1223		free(devlist, M_TEMP);
1224		if (bootverbose || mmc_debug) {
1225			device_printf(sc->dev,
1226			    "%sard detected (CID %08x%08x%08x%08x)\n",
1227			    newcard ? "New c" : "C",
1228			    raw_cid[0], raw_cid[1], raw_cid[2], raw_cid[3]);
1229		}
1230		if (newcard) {
1231			ivar = malloc(sizeof(struct mmc_ivars), M_DEVBUF,
1232			    M_WAITOK | M_ZERO);
1233			memcpy(ivar->raw_cid, raw_cid, sizeof(raw_cid));
1234		}
1235		if (mmcbr_get_ro(sc->dev))
1236			ivar->read_only = 1;
1237		ivar->bus_width = bus_width_1;
1238		ivar->timing = bus_timing_normal;
1239		ivar->mode = mmcbr_get_mode(sc->dev);
1240		if (ivar->mode == mode_sd) {
1241			mmc_decode_cid_sd(ivar->raw_cid, &ivar->cid);
1242			mmc_send_relative_addr(sc, &resp);
1243			ivar->rca = resp >> 16;
1244			/* Get card CSD. */
1245			mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1246			if (bootverbose || mmc_debug)
1247				device_printf(sc->dev,
1248				    "%sard detected (CSD %08x%08x%08x%08x)\n",
1249				    newcard ? "New c" : "C", ivar->raw_csd[0],
1250				    ivar->raw_csd[1], ivar->raw_csd[2],
1251				    ivar->raw_csd[3]);
1252			mmc_decode_csd_sd(ivar->raw_csd, &ivar->csd);
1253			ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1254			if (ivar->csd.csd_structure > 0)
1255				ivar->high_cap = 1;
1256			ivar->tran_speed = ivar->csd.tran_speed;
1257			ivar->erase_sector = ivar->csd.erase_sector *
1258			    ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1259
1260			err = mmc_send_status(sc->dev, sc->dev, ivar->rca,
1261			    &status);
1262			if (err != MMC_ERR_NONE) {
1263				device_printf(sc->dev,
1264				    "Error reading card status %d\n", err);
1265				break;
1266			}
1267			if ((status & R1_CARD_IS_LOCKED) != 0) {
1268				device_printf(sc->dev,
1269				    "Card is password protected, skipping.\n");
1270				break;
1271			}
1272
1273			/* Get card SCR.  Card must be selected to fetch it. */
1274			mmc_select_card(sc, ivar->rca);
1275			mmc_app_send_scr(sc, ivar->rca, ivar->raw_scr);
1276			mmc_app_decode_scr(ivar->raw_scr, &ivar->scr);
1277			/* Get card switch capabilities (command class 10). */
1278			if ((ivar->scr.sda_vsn >= 1) &&
1279			    (ivar->csd.ccc & (1 << 10))) {
1280				mmc_sd_switch(sc, SD_SWITCH_MODE_CHECK,
1281				    SD_SWITCH_GROUP1, SD_SWITCH_NOCHANGE,
1282				    switch_res);
1283				if (switch_res[13] & (1 << SD_SWITCH_HS_MODE)) {
1284					ivar->timing = bus_timing_hs;
1285					ivar->hs_tran_speed = SD_MAX_HS;
1286				}
1287			}
1288
1289			/*
1290			 * We deselect then reselect the card here.  Some cards
1291			 * become unselected and timeout with the above two
1292			 * commands, although the state tables / diagrams in the
1293			 * standard suggest they go back to the transfer state.
1294			 * Other cards don't become deselected, and if we
1295			 * attempt to blindly re-select them, we get timeout
1296			 * errors from some controllers.  So we deselect then
1297			 * reselect to handle all situations.  The only thing we
1298			 * use from the sd_status is the erase sector size, but
1299			 * it is still nice to get that right.
1300			 */
1301			mmc_select_card(sc, 0);
1302			mmc_select_card(sc, ivar->rca);
1303			mmc_app_sd_status(sc, ivar->rca, ivar->raw_sd_status);
1304			mmc_app_decode_sd_status(ivar->raw_sd_status,
1305			    &ivar->sd_status);
1306			if (ivar->sd_status.au_size != 0) {
1307				ivar->erase_sector =
1308				    16 << ivar->sd_status.au_size;
1309			}
1310			/* Find max supported bus width. */
1311			if ((mmcbr_get_caps(sc->dev) & MMC_CAP_4_BIT_DATA) &&
1312			    (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
1313				ivar->bus_width = bus_width_4;
1314
1315			/*
1316			 * Some cards that report maximum I/O block sizes
1317			 * greater than 512 require the block length to be
1318			 * set to 512, even though that is supposed to be
1319			 * the default.  Example:
1320			 *
1321			 * Transcend 2GB SDSC card, CID:
1322			 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1323			 */
1324			if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1325			    ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1326				mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1327
1328			mmc_format_card_id_string(ivar);
1329
1330			if (bootverbose || mmc_debug)
1331				mmc_log_card(sc->dev, ivar, newcard);
1332			if (newcard) {
1333				/* Add device. */
1334				child = device_add_child(sc->dev, NULL, -1);
1335				device_set_ivars(child, ivar);
1336			}
1337			mmc_select_card(sc, 0);
1338			return;
1339		}
1340		ivar->rca = rca++;
1341		mmc_set_relative_addr(sc, ivar->rca);
1342		/* Get card CSD. */
1343		mmc_send_csd(sc, ivar->rca, ivar->raw_csd);
1344		if (bootverbose || mmc_debug)
1345			device_printf(sc->dev,
1346			    "%sard detected (CSD %08x%08x%08x%08x)\n",
1347			    newcard ? "New c" : "C", ivar->raw_csd[0],
1348			    ivar->raw_csd[1], ivar->raw_csd[2],
1349			    ivar->raw_csd[3]);
1350
1351		mmc_decode_csd_mmc(ivar->raw_csd, &ivar->csd);
1352		ivar->sec_count = ivar->csd.capacity / MMC_SECTOR_SIZE;
1353		ivar->tran_speed = ivar->csd.tran_speed;
1354		ivar->erase_sector = ivar->csd.erase_sector *
1355		    ivar->csd.write_bl_len / MMC_SECTOR_SIZE;
1356
1357		err = mmc_send_status(sc->dev, sc->dev, ivar->rca, &status);
1358		if (err != MMC_ERR_NONE) {
1359			device_printf(sc->dev,
1360			    "Error reading card status %d\n", err);
1361			break;
1362		}
1363		if ((status & R1_CARD_IS_LOCKED) != 0) {
1364			device_printf(sc->dev,
1365			    "Card is password protected, skipping.\n");
1366			break;
1367		}
1368
1369		mmc_select_card(sc, ivar->rca);
1370
1371		/* Only MMC >= 4.x devices support EXT_CSD. */
1372		if (ivar->csd.spec_vers >= 4) {
1373			err = mmc_send_ext_csd(sc->dev, sc->dev,
1374			    ivar->raw_ext_csd);
1375			if (err != MMC_ERR_NONE) {
1376				device_printf(sc->dev,
1377				    "Error reading EXT_CSD %d\n", err);
1378				break;
1379			}
1380			/* Handle extended capacity from EXT_CSD */
1381			sec_count = ivar->raw_ext_csd[EXT_CSD_SEC_CNT] +
1382			    (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 1] << 8) +
1383			    (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 2] << 16) +
1384			    (ivar->raw_ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1385			if (sec_count != 0) {
1386				ivar->sec_count = sec_count;
1387				ivar->high_cap = 1;
1388			}
1389			/* Get card speed in high speed mode. */
1390			ivar->timing = bus_timing_hs;
1391			if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1392			    & EXT_CSD_CARD_TYPE_52)
1393				ivar->hs_tran_speed = MMC_TYPE_52_MAX_HS;
1394			else if (ivar->raw_ext_csd[EXT_CSD_CARD_TYPE]
1395			    & EXT_CSD_CARD_TYPE_26)
1396				ivar->hs_tran_speed = MMC_TYPE_26_MAX_HS;
1397			else
1398				ivar->hs_tran_speed = ivar->tran_speed;
1399			/*
1400			 * Determine generic switch timeout (provided in
1401			 * units of 10 ms), defaulting to 500 ms.
1402			 */
1403			ivar->cmd6_time = 500 * 1000;
1404			if (ivar->csd.spec_vers >= 6)
1405				ivar->cmd6_time = 10 *
1406				    ivar->raw_ext_csd[EXT_CSD_GEN_CMD6_TIME];
1407			/* Find max supported bus width. */
1408			ivar->bus_width = mmc_test_bus_width(sc);
1409			/* Handle HC erase sector size. */
1410			if (ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE] != 0) {
1411				ivar->erase_sector = 1024 *
1412				    ivar->raw_ext_csd[EXT_CSD_ERASE_GRP_SIZE];
1413				err = mmc_switch(sc->dev, sc->dev, ivar->rca,
1414				    EXT_CSD_CMD_SET_NORMAL,
1415				    EXT_CSD_ERASE_GRP_DEF,
1416				    EXT_CSD_ERASE_GRP_DEF_EN,
1417				    ivar->cmd6_time, true);
1418				if (err != MMC_ERR_NONE) {
1419					device_printf(sc->dev,
1420					    "Error setting erase group %d\n",
1421					    err);
1422					break;
1423				}
1424			}
1425		} else {
1426			ivar->bus_width = bus_width_1;
1427			ivar->timing = bus_timing_normal;
1428		}
1429
1430		/*
1431		 * Some cards that report maximum I/O block sizes greater
1432		 * than 512 require the block length to be set to 512, even
1433		 * though that is supposed to be the default.  Example:
1434		 *
1435		 * Transcend 2GB SDSC card, CID:
1436		 * mid=0x1b oid=0x534d pnm="00000" prv=1.0 mdt=00.2000
1437		 */
1438		if (ivar->csd.read_bl_len != MMC_SECTOR_SIZE ||
1439		    ivar->csd.write_bl_len != MMC_SECTOR_SIZE)
1440			mmc_set_blocklen(sc, MMC_SECTOR_SIZE);
1441
1442		mmc_decode_cid_mmc(ivar->raw_cid, &ivar->cid,
1443		    ivar->raw_ext_csd[EXT_CSD_REV] >= 5);
1444		mmc_format_card_id_string(ivar);
1445
1446		if (bootverbose || mmc_debug)
1447			mmc_log_card(sc->dev, ivar, newcard);
1448		if (newcard) {
1449			/* Add device. */
1450			child = device_add_child(sc->dev, NULL, -1);
1451			device_set_ivars(child, ivar);
1452		}
1453		mmc_select_card(sc, 0);
1454	}
1455}
1456
1457static void
1458mmc_rescan_cards(struct mmc_softc *sc)
1459{
1460	struct mmc_ivars *ivar;
1461	device_t *devlist;
1462	int err, i, devcount;
1463
1464	if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1465		return;
1466	for (i = 0; i < devcount; i++) {
1467		ivar = device_get_ivars(devlist[i]);
1468		if (mmc_select_card(sc, ivar->rca) != MMC_ERR_NONE) {
1469			if (bootverbose || mmc_debug)
1470				device_printf(sc->dev,
1471				    "Card at relative address %d lost.\n",
1472				    ivar->rca);
1473			device_delete_child(sc->dev, devlist[i]);
1474			free(ivar, M_DEVBUF);
1475		}
1476	}
1477	free(devlist, M_TEMP);
1478	mmc_select_card(sc, 0);
1479}
1480
1481static int
1482mmc_delete_cards(struct mmc_softc *sc)
1483{
1484	struct mmc_ivars *ivar;
1485	device_t *devlist;
1486	int err, i, devcount;
1487
1488	if ((err = device_get_children(sc->dev, &devlist, &devcount)) != 0)
1489		return (err);
1490	for (i = 0; i < devcount; i++) {
1491		ivar = device_get_ivars(devlist[i]);
1492		if (bootverbose || mmc_debug)
1493			device_printf(sc->dev,
1494			    "Card at relative address %d deleted.\n",
1495			    ivar->rca);
1496		device_delete_child(sc->dev, devlist[i]);
1497		free(ivar, M_DEVBUF);
1498	}
1499	free(devlist, M_TEMP);
1500	return (0);
1501}
1502
1503static void
1504mmc_go_discovery(struct mmc_softc *sc)
1505{
1506	uint32_t ocr;
1507	device_t dev;
1508	int err;
1509
1510	dev = sc->dev;
1511	if (mmcbr_get_power_mode(dev) != power_on) {
1512		/*
1513		 * First, try SD modes
1514		 */
1515		sc->squelched++; /* Errors are expected, squelch reporting. */
1516		mmcbr_set_mode(dev, mode_sd);
1517		mmc_power_up(sc);
1518		mmcbr_set_bus_mode(dev, pushpull);
1519		if (bootverbose || mmc_debug)
1520			device_printf(sc->dev, "Probing bus\n");
1521		mmc_idle_cards(sc);
1522		err = mmc_send_if_cond(sc, 1);
1523		if ((bootverbose || mmc_debug) && err == 0)
1524			device_printf(sc->dev,
1525			    "SD 2.0 interface conditions: OK\n");
1526		if (mmc_send_app_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1527			if (bootverbose || mmc_debug)
1528				device_printf(sc->dev, "SD probe: failed\n");
1529			/*
1530			 * Failed, try MMC
1531			 */
1532			mmcbr_set_mode(dev, mode_mmc);
1533			if (mmc_send_op_cond(sc, 0, &ocr) != MMC_ERR_NONE) {
1534				if (bootverbose || mmc_debug)
1535					device_printf(sc->dev,
1536					    "MMC probe: failed\n");
1537				ocr = 0; /* Failed both, powerdown. */
1538			} else if (bootverbose || mmc_debug)
1539				device_printf(sc->dev,
1540				    "MMC probe: OK (OCR: 0x%08x)\n", ocr);
1541		} else if (bootverbose || mmc_debug)
1542			device_printf(sc->dev, "SD probe: OK (OCR: 0x%08x)\n",
1543			    ocr);
1544		sc->squelched--;
1545
1546		mmcbr_set_ocr(dev, mmc_select_vdd(sc, ocr));
1547		if (mmcbr_get_ocr(dev) != 0)
1548			mmc_idle_cards(sc);
1549	} else {
1550		mmcbr_set_bus_mode(dev, opendrain);
1551		mmcbr_set_clock(dev, CARD_ID_FREQUENCY);
1552		mmcbr_update_ios(dev);
1553		/* XXX recompute vdd based on new cards? */
1554	}
1555	/*
1556	 * Make sure that we have a mutually agreeable voltage to at least
1557	 * one card on the bus.
1558	 */
1559	if (bootverbose || mmc_debug)
1560		device_printf(sc->dev, "Current OCR: 0x%08x\n",
1561		    mmcbr_get_ocr(dev));
1562	if (mmcbr_get_ocr(dev) == 0) {
1563		device_printf(sc->dev, "No compatible cards found on bus\n");
1564		mmc_delete_cards(sc);
1565		mmc_power_down(sc);
1566		return;
1567	}
1568	/*
1569	 * Reselect the cards after we've idled them above.
1570	 */
1571	if (mmcbr_get_mode(dev) == mode_sd) {
1572		err = mmc_send_if_cond(sc, 1);
1573		mmc_send_app_op_cond(sc,
1574		    (err ? 0 : MMC_OCR_CCS) | mmcbr_get_ocr(dev), NULL);
1575	} else
1576		mmc_send_op_cond(sc, MMC_OCR_CCS | mmcbr_get_ocr(dev), NULL);
1577	mmc_discover_cards(sc);
1578	mmc_rescan_cards(sc);
1579
1580	mmcbr_set_bus_mode(dev, pushpull);
1581	mmcbr_update_ios(dev);
1582	mmc_calculate_clock(sc);
1583}
1584
1585static int
1586mmc_calculate_clock(struct mmc_softc *sc)
1587{
1588	device_t *kids;
1589	struct mmc_ivars *ivar;
1590	int i, f_max, max_dtr, max_hs_dtr, max_timing, nkid;
1591
1592	f_max = mmcbr_get_f_max(sc->dev);
1593	max_dtr = max_hs_dtr = f_max;
1594	if (mmcbr_get_caps(sc->dev) & MMC_CAP_HSPEED)
1595		max_timing = bus_timing_hs;
1596	else
1597		max_timing = bus_timing_normal;
1598	if (device_get_children(sc->dev, &kids, &nkid) != 0)
1599		panic("can't get children");
1600	for (i = 0; i < nkid; i++) {
1601		ivar = device_get_ivars(kids[i]);
1602		if (ivar->timing < max_timing)
1603			max_timing = ivar->timing;
1604		if (ivar->tran_speed < max_dtr)
1605			max_dtr = ivar->tran_speed;
1606		if (ivar->hs_tran_speed < max_hs_dtr)
1607			max_hs_dtr = ivar->hs_tran_speed;
1608	}
1609	if (bootverbose || mmc_debug) {
1610		device_printf(sc->dev,
1611		    "setting transfer rate to %d.%03dMHz%s\n",
1612		    max_dtr / 1000000, (max_dtr / 1000) % 1000,
1613		    max_timing == bus_timing_hs ? " (high speed timing)" : "");
1614	}
1615	for (i = 0; i < nkid; i++) {
1616		ivar = device_get_ivars(kids[i]);
1617		if (ivar->timing == bus_timing_normal)
1618			continue;
1619		mmc_select_card(sc, ivar->rca);
1620		mmc_set_timing(sc, ivar, max_timing);
1621	}
1622	mmc_select_card(sc, 0);
1623	free(kids, M_TEMP);
1624	if (max_timing == bus_timing_hs)
1625		max_dtr = max_hs_dtr;
1626	mmcbr_set_clock(sc->dev, max_dtr);
1627	mmcbr_update_ios(sc->dev);
1628	return (max_dtr);
1629}
1630
1631static void
1632mmc_scan(struct mmc_softc *sc)
1633{
1634	device_t dev = sc->dev;
1635
1636	mmc_acquire_bus(dev, dev);
1637	mmc_go_discovery(sc);
1638	mmc_release_bus(dev, dev);
1639
1640	bus_generic_attach(dev);
1641}
1642
1643static int
1644mmc_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
1645{
1646	struct mmc_ivars *ivar = device_get_ivars(child);
1647
1648	switch (which) {
1649	default:
1650		return (EINVAL);
1651	case MMC_IVAR_SPEC_VERS:
1652		*result = ivar->csd.spec_vers;
1653		break;
1654	case MMC_IVAR_DSR_IMP:
1655		*result = ivar->csd.dsr_imp;
1656		break;
1657	case MMC_IVAR_MEDIA_SIZE:
1658		*result = ivar->sec_count;
1659		break;
1660	case MMC_IVAR_RCA:
1661		*result = ivar->rca;
1662		break;
1663	case MMC_IVAR_SECTOR_SIZE:
1664		*result = MMC_SECTOR_SIZE;
1665		break;
1666	case MMC_IVAR_TRAN_SPEED:
1667		*result = mmcbr_get_clock(bus);
1668		break;
1669	case MMC_IVAR_READ_ONLY:
1670		*result = ivar->read_only;
1671		break;
1672	case MMC_IVAR_HIGH_CAP:
1673		*result = ivar->high_cap;
1674		break;
1675	case MMC_IVAR_CARD_TYPE:
1676		*result = ivar->mode;
1677		break;
1678	case MMC_IVAR_BUS_WIDTH:
1679		*result = ivar->bus_width;
1680		break;
1681	case MMC_IVAR_ERASE_SECTOR:
1682		*result = ivar->erase_sector;
1683		break;
1684	case MMC_IVAR_MAX_DATA:
1685		*result = mmcbr_get_max_data(bus);
1686		break;
1687	case MMC_IVAR_CARD_ID_STRING:
1688		*(char **)result = ivar->card_id_string;
1689		break;
1690	case MMC_IVAR_CARD_SN_STRING:
1691		*(char **)result = ivar->card_sn_string;
1692		break;
1693	}
1694	return (0);
1695}
1696
1697static int
1698mmc_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
1699{
1700
1701	/*
1702	 * None are writable ATM
1703	 */
1704	return (EINVAL);
1705}
1706
1707static void
1708mmc_delayed_attach(void *xsc)
1709{
1710	struct mmc_softc *sc = xsc;
1711
1712	mmc_scan(sc);
1713	config_intrhook_disestablish(&sc->config_intrhook);
1714}
1715
1716static int
1717mmc_child_location_str(device_t dev, device_t child, char *buf,
1718    size_t buflen)
1719{
1720
1721	snprintf(buf, buflen, "rca=0x%04x", mmc_get_rca(child));
1722	return (0);
1723}
1724
1725static device_method_t mmc_methods[] = {
1726	/* device_if */
1727	DEVMETHOD(device_probe, mmc_probe),
1728	DEVMETHOD(device_attach, mmc_attach),
1729	DEVMETHOD(device_detach, mmc_detach),
1730	DEVMETHOD(device_suspend, mmc_suspend),
1731	DEVMETHOD(device_resume, mmc_resume),
1732
1733	/* Bus interface */
1734	DEVMETHOD(bus_read_ivar, mmc_read_ivar),
1735	DEVMETHOD(bus_write_ivar, mmc_write_ivar),
1736	DEVMETHOD(bus_child_location_str, mmc_child_location_str),
1737
1738	/* MMC Bus interface */
1739	DEVMETHOD(mmcbus_wait_for_request, mmc_wait_for_request),
1740	DEVMETHOD(mmcbus_acquire_bus, mmc_acquire_bus),
1741	DEVMETHOD(mmcbus_release_bus, mmc_release_bus),
1742
1743	DEVMETHOD_END
1744};
1745
1746driver_t mmc_driver = {
1747	"mmc",
1748	mmc_methods,
1749	sizeof(struct mmc_softc),
1750};
1751devclass_t mmc_devclass;
1752
1753MODULE_VERSION(mmc, MMC_VERSION);
1754