xref: /netbsd-src/sys/dev/sdmmc/sdmmc_mem.c (revision a4ddc2c8fb9af816efe3b1c375a5530aef0e89e9)
1 /*	$NetBSD: sdmmc_mem.c,v 1.27 2012/12/20 03:51:04 jakllsch Exp $	*/
2 /*	$OpenBSD: sdmmc_mem.c,v 1.10 2009/01/09 10:55:22 jsg Exp $	*/
3 
4 /*
5  * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*-
21  * Copyright (C) 2007, 2008, 2009, 2010 NONAKA Kimihiro <nonaka@netbsd.org>
22  * All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  * 1. Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  * 2. Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in the
31  *    documentation and/or other materials provided with the distribution.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
34  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
36  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
37  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
42  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  */
44 
45 /* Routines for SD/MMC memory cards. */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: sdmmc_mem.c,v 1.27 2012/12/20 03:51:04 jakllsch Exp $");
49 
50 #ifdef _KERNEL_OPT
51 #include "opt_sdmmc.h"
52 #endif
53 
54 #include <sys/param.h>
55 #include <sys/kernel.h>
56 #include <sys/malloc.h>
57 #include <sys/systm.h>
58 #include <sys/device.h>
59 
60 #include <dev/sdmmc/sdmmcchip.h>
61 #include <dev/sdmmc/sdmmcreg.h>
62 #include <dev/sdmmc/sdmmcvar.h>
63 
64 #ifdef SDMMC_DEBUG
65 #define DPRINTF(s)	do { printf s; } while (/*CONSTCOND*/0)
66 #else
67 #define DPRINTF(s)	do {} while (/*CONSTCOND*/0)
68 #endif
69 
70 typedef struct { uint32_t _bits[512/32]; } __packed __aligned(4) sdmmc_bitfield512_t;
71 
72 static int sdmmc_mem_sd_init(struct sdmmc_softc *, struct sdmmc_function *);
73 static int sdmmc_mem_mmc_init(struct sdmmc_softc *, struct sdmmc_function *);
74 static int sdmmc_mem_send_cid(struct sdmmc_softc *, sdmmc_response *);
75 static int sdmmc_mem_send_csd(struct sdmmc_softc *, struct sdmmc_function *,
76     sdmmc_response *);
77 static int sdmmc_mem_send_scr(struct sdmmc_softc *, struct sdmmc_function *,
78     uint32_t scr[2]);
79 static int sdmmc_mem_decode_scr(struct sdmmc_softc *, struct sdmmc_function *);
80 static int sdmmc_mem_send_cxd_data(struct sdmmc_softc *, int, void *, size_t);
81 static int sdmmc_set_bus_width(struct sdmmc_function *, int);
82 static int sdmmc_mem_sd_switch(struct sdmmc_function *, int, int, int, sdmmc_bitfield512_t *);
83 static int sdmmc_mem_mmc_switch(struct sdmmc_function *, uint8_t, uint8_t,
84     uint8_t);
85 static int sdmmc_mem_spi_read_ocr(struct sdmmc_softc *, uint32_t, uint32_t *);
86 static int sdmmc_mem_single_read_block(struct sdmmc_function *, uint32_t,
87     u_char *, size_t);
88 static int sdmmc_mem_single_write_block(struct sdmmc_function *, uint32_t,
89     u_char *, size_t);
90 static int sdmmc_mem_read_block_subr(struct sdmmc_function *, uint32_t,
91     u_char *, size_t);
92 static int sdmmc_mem_write_block_subr(struct sdmmc_function *, uint32_t,
93     u_char *, size_t);
94 
95 /*
96  * Initialize SD/MMC memory cards and memory in SDIO "combo" cards.
97  */
98 int
99 sdmmc_mem_enable(struct sdmmc_softc *sc)
100 {
101 	uint32_t host_ocr;
102 	uint32_t card_ocr;
103 	uint32_t ocr = 0;
104 	int error;
105 
106 	SDMMC_LOCK(sc);
107 
108 	/* Set host mode to SD "combo" card or SD memory-only. */
109 	SET(sc->sc_flags, SMF_SD_MODE|SMF_MEM_MODE);
110 
111 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
112 		sdmmc_spi_chip_initialize(sc->sc_spi_sct, sc->sc_sch);
113 
114 	/* Reset memory (*must* do that before CMD55 or CMD1). */
115 	sdmmc_go_idle_state(sc);
116 
117 	/* Check SD Ver.2 */
118 	error = sdmmc_mem_send_if_cond(sc, 0x1aa, &card_ocr);
119 	if (error == 0 && card_ocr == 0x1aa)
120 		SET(ocr, MMC_OCR_HCS);
121 
122 	/*
123 	 * Read the SD/MMC memory OCR value by issuing CMD55 followed
124 	 * by ACMD41 to read the OCR value from memory-only SD cards.
125 	 * MMC cards will not respond to CMD55 or ACMD41 and this is
126 	 * how we distinguish them from SD cards.
127 	 */
128 mmc_mode:
129 	error = sdmmc_mem_send_op_cond(sc,
130 	  ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ? ocr : 0, &card_ocr);
131 	if (error) {
132 		if (ISSET(sc->sc_flags, SMF_SD_MODE) &&
133 		    !ISSET(sc->sc_flags, SMF_IO_MODE)) {
134 			/* Not a SD card, switch to MMC mode. */
135 			DPRINTF(("%s: switch to MMC mode\n", SDMMCDEVNAME(sc)));
136 			CLR(sc->sc_flags, SMF_SD_MODE);
137 			goto mmc_mode;
138 		}
139 		if (!ISSET(sc->sc_flags, SMF_SD_MODE)) {
140 			DPRINTF(("%s: couldn't read memory OCR\n",
141 			    SDMMCDEVNAME(sc)));
142 			goto out;
143 		} else {
144 			/* Not a "combo" card. */
145 			CLR(sc->sc_flags, SMF_MEM_MODE);
146 			error = 0;
147 			goto out;
148 		}
149 	}
150 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
151 		/* get card OCR */
152 		error = sdmmc_mem_spi_read_ocr(sc, ocr, &card_ocr);
153 		if (error) {
154 			DPRINTF(("%s: couldn't read SPI memory OCR\n",
155 			    SDMMCDEVNAME(sc)));
156 			goto out;
157 		}
158 	}
159 
160 	/* Set the lowest voltage supported by the card and host. */
161 	host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
162 	error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
163 	if (error) {
164 		DPRINTF(("%s: couldn't supply voltage requested by card\n",
165 		    SDMMCDEVNAME(sc)));
166 		goto out;
167 	}
168 	host_ocr &= card_ocr;
169 	host_ocr |= ocr;
170 
171 	/* Send the new OCR value until all cards are ready. */
172 	error = sdmmc_mem_send_op_cond(sc, host_ocr, NULL);
173 	if (error) {
174 		DPRINTF(("%s: couldn't send memory OCR\n", SDMMCDEVNAME(sc)));
175 		goto out;
176 	}
177 
178 out:
179 	SDMMC_UNLOCK(sc);
180 
181 	return error;
182 }
183 
184 /*
185  * Read the CSD and CID from all cards and assign each card a unique
186  * relative card address (RCA).  CMD2 is ignored by SDIO-only cards.
187  */
188 void
189 sdmmc_mem_scan(struct sdmmc_softc *sc)
190 {
191 	sdmmc_response resp;
192 	struct sdmmc_function *sf;
193 	uint16_t next_rca;
194 	int error;
195 	int retry;
196 
197 	SDMMC_LOCK(sc);
198 
199 	/*
200 	 * CMD2 is a broadcast command understood by SD cards and MMC
201 	 * cards.  All cards begin to respond to the command, but back
202 	 * off if another card drives the CMD line to a different level.
203 	 * Only one card will get its entire response through.  That
204 	 * card remains silent once it has been assigned a RCA.
205 	 */
206 	for (retry = 0; retry < 100; retry++) {
207 		error = sdmmc_mem_send_cid(sc, &resp);
208 		if (error) {
209 			if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) &&
210 			    error == ETIMEDOUT) {
211 				/* No more cards there. */
212 				break;
213 			}
214 			DPRINTF(("%s: couldn't read CID\n", SDMMCDEVNAME(sc)));
215 			break;
216 		}
217 
218 		/* In MMC mode, find the next available RCA. */
219 		next_rca = 1;
220 		if (!ISSET(sc->sc_flags, SMF_SD_MODE)) {
221 			SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list)
222 				next_rca++;
223 		}
224 
225 		/* Allocate a sdmmc_function structure. */
226 		sf = sdmmc_function_alloc(sc);
227 		sf->rca = next_rca;
228 
229 		/*
230 		 * Remember the CID returned in the CMD2 response for
231 		 * later decoding.
232 		 */
233 		memcpy(sf->raw_cid, resp, sizeof(sf->raw_cid));
234 
235 		/*
236 		 * Silence the card by assigning it a unique RCA, or
237 		 * querying it for its RCA in the case of SD.
238 		 */
239 		if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
240 			if (sdmmc_set_relative_addr(sc, sf) != 0) {
241 				aprint_error_dev(sc->sc_dev,
242 				    "couldn't set mem RCA\n");
243 				sdmmc_function_free(sf);
244 				break;
245 			}
246 		}
247 
248 		/*
249 		 * If this is a memory-only card, the card responding
250 		 * first becomes an alias for SDIO function 0.
251 		 */
252 		if (sc->sc_fn0 == NULL)
253 			sc->sc_fn0 = sf;
254 
255 		SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
256 
257 		/* only one function in SPI mode */
258 		if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
259 			break;
260 	}
261 
262 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
263 		/* Go to Data Transfer Mode, if possible. */
264 		sdmmc_chip_bus_rod(sc->sc_sct, sc->sc_sch, 0);
265 
266 	/*
267 	 * All cards are either inactive or awaiting further commands.
268 	 * Read the CSDs and decode the raw CID for each card.
269 	 */
270 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
271 		error = sdmmc_mem_send_csd(sc, sf, &resp);
272 		if (error) {
273 			SET(sf->flags, SFF_ERROR);
274 			continue;
275 		}
276 
277 		if (sdmmc_decode_csd(sc, resp, sf) != 0 ||
278 		    sdmmc_decode_cid(sc, sf->raw_cid, sf) != 0) {
279 			SET(sf->flags, SFF_ERROR);
280 			continue;
281 		}
282 
283 #ifdef SDMMC_DEBUG
284 		printf("%s: CID: ", SDMMCDEVNAME(sc));
285 		sdmmc_print_cid(&sf->cid);
286 #endif
287 	}
288 
289 	SDMMC_UNLOCK(sc);
290 }
291 
292 int
293 sdmmc_decode_csd(struct sdmmc_softc *sc, sdmmc_response resp,
294     struct sdmmc_function *sf)
295 {
296 	/* TRAN_SPEED(2:0): transfer rate exponent */
297 	static const int speed_exponent[8] = {
298 		100 *    1,	/* 100 Kbits/s */
299 		  1 * 1000,	/*   1 Mbits/s */
300 		 10 * 1000,	/*  10 Mbits/s */
301 		100 * 1000,	/* 100 Mbits/s */
302 		         0,
303 		         0,
304 		         0,
305 		         0,
306 	};
307 	/* TRAN_SPEED(6:3): time mantissa */
308 	static const int speed_mantissa[16] = {
309 		0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80,
310 	};
311 	struct sdmmc_csd *csd = &sf->csd;
312 	int e, m;
313 
314 	if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
315 		/*
316 		 * CSD version 1.0 corresponds to SD system
317 		 * specification version 1.0 - 1.10. (SanDisk, 3.5.3)
318 		 */
319 		csd->csdver = SD_CSD_CSDVER(resp);
320 		switch (csd->csdver) {
321 		case SD_CSD_CSDVER_2_0:
322 			DPRINTF(("%s: SD Ver.2.0\n", SDMMCDEVNAME(sc)));
323 			SET(sf->flags, SFF_SDHC);
324 			csd->capacity = SD_CSD_V2_CAPACITY(resp);
325 			csd->read_bl_len = SD_CSD_V2_BL_LEN;
326 			break;
327 
328 		case SD_CSD_CSDVER_1_0:
329 			DPRINTF(("%s: SD Ver.1.0\n", SDMMCDEVNAME(sc)));
330 			csd->capacity = SD_CSD_CAPACITY(resp);
331 			csd->read_bl_len = SD_CSD_READ_BL_LEN(resp);
332 			break;
333 
334 		default:
335 			aprint_error_dev(sc->sc_dev,
336 			    "unknown SD CSD structure version 0x%x\n",
337 			    csd->csdver);
338 			return 1;
339 		}
340 
341 		csd->mmcver = SD_CSD_MMCVER(resp);
342 		csd->write_bl_len = SD_CSD_WRITE_BL_LEN(resp);
343 		csd->r2w_factor = SD_CSD_R2W_FACTOR(resp);
344 		e = SD_CSD_SPEED_EXP(resp);
345 		m = SD_CSD_SPEED_MANT(resp);
346 		csd->tran_speed = speed_exponent[e] * speed_mantissa[m] / 10;
347 		csd->ccc = SD_CSD_CCC(resp);
348 	} else {
349 		csd->csdver = MMC_CSD_CSDVER(resp);
350 		if (csd->csdver == MMC_CSD_CSDVER_1_0) {
351 			aprint_error_dev(sc->sc_dev,
352 			    "unknown MMC CSD structure version 0x%x\n",
353 			    csd->csdver);
354 			return 1;
355 		}
356 
357 		csd->mmcver = MMC_CSD_MMCVER(resp);
358 		csd->capacity = MMC_CSD_CAPACITY(resp);
359 		csd->read_bl_len = MMC_CSD_READ_BL_LEN(resp);
360 		csd->write_bl_len = MMC_CSD_WRITE_BL_LEN(resp);
361 		csd->r2w_factor = MMC_CSD_R2W_FACTOR(resp);
362 		e = MMC_CSD_TRAN_SPEED_EXP(resp);
363 		m = MMC_CSD_TRAN_SPEED_MANT(resp);
364 		csd->tran_speed = speed_exponent[e] * speed_mantissa[m] / 10;
365 	}
366 	if ((1 << csd->read_bl_len) > SDMMC_SECTOR_SIZE)
367 		csd->capacity *= (1 << csd->read_bl_len) / SDMMC_SECTOR_SIZE;
368 
369 #ifdef SDMMC_DUMP_CSD
370 	sdmmc_print_csd(resp, csd);
371 #endif
372 
373 	return 0;
374 }
375 
376 int
377 sdmmc_decode_cid(struct sdmmc_softc *sc, sdmmc_response resp,
378     struct sdmmc_function *sf)
379 {
380 	struct sdmmc_cid *cid = &sf->cid;
381 
382 	if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
383 		cid->mid = SD_CID_MID(resp);
384 		cid->oid = SD_CID_OID(resp);
385 		SD_CID_PNM_CPY(resp, cid->pnm);
386 		cid->rev = SD_CID_REV(resp);
387 		cid->psn = SD_CID_PSN(resp);
388 		cid->mdt = SD_CID_MDT(resp);
389 	} else {
390 		switch(sf->csd.mmcver) {
391 		case MMC_CSD_MMCVER_1_0:
392 		case MMC_CSD_MMCVER_1_4:
393 			cid->mid = MMC_CID_MID_V1(resp);
394 			MMC_CID_PNM_V1_CPY(resp, cid->pnm);
395 			cid->rev = MMC_CID_REV_V1(resp);
396 			cid->psn = MMC_CID_PSN_V1(resp);
397 			cid->mdt = MMC_CID_MDT_V1(resp);
398 			break;
399 		case MMC_CSD_MMCVER_2_0:
400 		case MMC_CSD_MMCVER_3_1:
401 		case MMC_CSD_MMCVER_4_0:
402 			cid->mid = MMC_CID_MID_V2(resp);
403 			cid->oid = MMC_CID_OID_V2(resp);
404 			MMC_CID_PNM_V2_CPY(resp, cid->pnm);
405 			cid->psn = MMC_CID_PSN_V2(resp);
406 			break;
407 		default:
408 			aprint_error_dev(sc->sc_dev, "unknown MMC version %d\n",
409 			    sf->csd.mmcver);
410 			return 1;
411 		}
412 	}
413 	return 0;
414 }
415 
416 void
417 sdmmc_print_cid(struct sdmmc_cid *cid)
418 {
419 
420 	printf("mid=0x%02x oid=0x%04x pnm=\"%s\" rev=0x%02x psn=0x%08x"
421 	    " mdt=%03x\n", cid->mid, cid->oid, cid->pnm, cid->rev, cid->psn,
422 	    cid->mdt);
423 }
424 
425 #ifdef SDMMC_DUMP_CSD
426 void
427 sdmmc_print_csd(sdmmc_response resp, struct sdmmc_csd *csd)
428 {
429 
430 	printf("csdver = %d\n", csd->csdver);
431 	printf("mmcver = %d\n", csd->mmcver);
432 	printf("capacity = 0x%08x\n", csd->capacity);
433 	printf("read_bl_len = %d\n", csd->read_bl_len);
434 	printf("write_bl_len = %d\n", csd->write_bl_len);
435 	printf("r2w_factor = %d\n", csd->r2w_factor);
436 	printf("tran_speed = %d\n", csd->tran_speed);
437 	printf("ccc = 0x%x\n", csd->ccc);
438 }
439 #endif
440 
441 /*
442  * Initialize a SD/MMC memory card.
443  */
444 int
445 sdmmc_mem_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
446 {
447 	int error = 0;
448 
449 	SDMMC_LOCK(sc);
450 
451 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
452 		error = sdmmc_select_card(sc, sf);
453 		if (error)
454 			goto out;
455 	}
456 
457 	if (!ISSET(sf->flags, SFF_SDHC)) {
458 		error = sdmmc_mem_set_blocklen(sc, sf);
459 		if (error)
460 			goto out;
461 	}
462 
463 	if (ISSET(sc->sc_flags, SMF_SD_MODE))
464 		error = sdmmc_mem_sd_init(sc, sf);
465 	else
466 		error = sdmmc_mem_mmc_init(sc, sf);
467 
468 out:
469 	SDMMC_UNLOCK(sc);
470 
471 	return error;
472 }
473 
474 /*
475  * Get or set the card's memory OCR value (SD or MMC).
476  */
477 int
478 sdmmc_mem_send_op_cond(struct sdmmc_softc *sc, uint32_t ocr, uint32_t *ocrp)
479 {
480 	struct sdmmc_command cmd;
481 	int error;
482 	int retry;
483 
484 	/* Don't lock */
485 
486 	/*
487 	 * If we change the OCR value, retry the command until the OCR
488 	 * we receive in response has the "CARD BUSY" bit set, meaning
489 	 * that all cards are ready for identification.
490 	 */
491 	for (retry = 0; retry < 100; retry++) {
492 		memset(&cmd, 0, sizeof(cmd));
493 		cmd.c_arg = !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ?
494 		    ocr : (ocr & MMC_OCR_HCS);
495 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R3 | SCF_RSP_SPI_R1;
496 
497 		if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
498 			cmd.c_opcode = SD_APP_OP_COND;
499 			error = sdmmc_app_command(sc, NULL, &cmd);
500 		} else {
501 			cmd.c_opcode = MMC_SEND_OP_COND;
502 			error = sdmmc_mmc_command(sc, &cmd);
503 		}
504 		if (error)
505 			break;
506 
507 		if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
508 			if (!ISSET(MMC_SPI_R1(cmd.c_resp), R1_SPI_IDLE))
509 				break;
510 		} else {
511 			if (ISSET(MMC_R3(cmd.c_resp), MMC_OCR_MEM_READY) ||
512 			    ocr == 0)
513 				break;
514 		}
515 
516 		error = ETIMEDOUT;
517 		sdmmc_delay(10000);
518 	}
519 	if (error == 0 &&
520 	    ocrp != NULL &&
521 	    !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
522 		*ocrp = MMC_R3(cmd.c_resp);
523 	DPRINTF(("%s: sdmmc_mem_send_op_cond: error=%d, ocr=%#x\n",
524 	    SDMMCDEVNAME(sc), error, MMC_R3(cmd.c_resp)));
525 	return error;
526 }
527 
528 int
529 sdmmc_mem_send_if_cond(struct sdmmc_softc *sc, uint32_t ocr, uint32_t *ocrp)
530 {
531 	struct sdmmc_command cmd;
532 	int error;
533 
534 	/* Don't lock */
535 
536 	memset(&cmd, 0, sizeof(cmd));
537 	cmd.c_arg = ocr;
538 	cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R7 | SCF_RSP_SPI_R7;
539 	cmd.c_opcode = SD_SEND_IF_COND;
540 
541 	error = sdmmc_mmc_command(sc, &cmd);
542 	if (error == 0 && ocrp != NULL) {
543 		if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
544 			*ocrp = MMC_SPI_R7(cmd.c_resp);
545 		} else {
546 			*ocrp = MMC_R7(cmd.c_resp);
547 		}
548 		DPRINTF(("%s: sdmmc_mem_send_if_cond: error=%d, ocr=%#x\n",
549 		    SDMMCDEVNAME(sc), error, *ocrp));
550 	}
551 	return error;
552 }
553 
554 /*
555  * Set the read block length appropriately for this card, according to
556  * the card CSD register value.
557  */
558 int
559 sdmmc_mem_set_blocklen(struct sdmmc_softc *sc, struct sdmmc_function *sf)
560 {
561 	struct sdmmc_command cmd;
562 	int error;
563 
564 	/* Don't lock */
565 
566 	memset(&cmd, 0, sizeof(cmd));
567 	cmd.c_opcode = MMC_SET_BLOCKLEN;
568 	cmd.c_arg = SDMMC_SECTOR_SIZE;
569 	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
570 
571 	error = sdmmc_mmc_command(sc, &cmd);
572 
573 	DPRINTF(("%s: sdmmc_mem_set_blocklen: read_bl_len=%d sector_size=%d\n",
574 	    SDMMCDEVNAME(sc), 1 << sf->csd.read_bl_len, SDMMC_SECTOR_SIZE));
575 
576 	return error;
577 }
578 
579 /* make 512-bit BE quantity __bitfield()-compatible */
580 static void
581 sdmmc_be512_to_bitfield512(sdmmc_bitfield512_t *buf) {
582 	size_t i;
583 	uint32_t tmp0, tmp1;
584 	const size_t bitswords = __arraycount(buf->_bits);
585 	for (i = 0; i < bitswords/2; i++) {
586 		tmp0 = buf->_bits[i];
587 		tmp1 = buf->_bits[bitswords - 1 - i];
588 		buf->_bits[i] = be32toh(tmp1);
589 		buf->_bits[bitswords - 1 - i] = be32toh(tmp0);
590 	}
591 }
592 
593 static int
594 sdmmc_mem_sd_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
595 {
596 	static const struct {
597 		int v;
598 		int freq;
599 	} switch_group0_functions[] = {
600 		/* Default/SDR12 */
601 		{ MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V |
602 		  MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V,	 25000 },
603 
604 		/* High-Speed/SDR25 */
605 		{ MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V |
606 		  MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V,	 50000 },
607 
608 		/* SDR50 */
609 		{ MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V,	100000 },
610 
611 		/* SDR104 */
612 		{ MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V,	208000 },
613 
614 		/* DDR50 */
615 		{ MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V,	 50000 },
616 	};
617 	int host_ocr, support_func, best_func, error, g, i;
618 	sdmmc_bitfield512_t status; /* Switch Function Status */
619 
620 	error = sdmmc_mem_send_scr(sc, sf, sf->raw_scr);
621 	if (error) {
622 		aprint_error_dev(sc->sc_dev, "SD_SEND_SCR send failed.\n");
623 		return error;
624 	}
625 	error = sdmmc_mem_decode_scr(sc, sf);
626 	if (error)
627 		return error;
628 
629 	if (ISSET(sc->sc_caps, SMC_CAPS_4BIT_MODE) &&
630 	    ISSET(sf->scr.bus_width, SCR_SD_BUS_WIDTHS_4BIT)) {
631 		DPRINTF(("%s: change bus width\n", SDMMCDEVNAME(sc)));
632 		error = sdmmc_set_bus_width(sf, 4);
633 		if (error) {
634 			aprint_error_dev(sc->sc_dev,
635 			    "can't change bus width (%d bit)\n", 4);
636 			return error;
637 		}
638 		sf->width = 4;
639 	}
640 
641 	if (sf->scr.sd_spec >= SCR_SD_SPEC_VER_1_10 &&
642 	    ISSET(sf->csd.ccc, SD_CSD_CCC_SWITCH)) {
643 		DPRINTF(("%s: switch func mode 0\n", SDMMCDEVNAME(sc)));
644 		error = sdmmc_mem_sd_switch(sf, 0, 1, 0, &status);
645 		if (error) {
646 			aprint_error_dev(sc->sc_dev,
647 			    "switch func mode 0 failed\n");
648 			return error;
649 		}
650 
651 		host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
652 		support_func = SFUNC_STATUS_GROUP(&status, 1);
653 		best_func = 0;
654 		for (i = 0, g = 1;
655 		    i < __arraycount(switch_group0_functions); i++, g <<= 1) {
656 			if (!(switch_group0_functions[i].v & host_ocr))
657 				continue;
658 			if (g & support_func)
659 				best_func = i;
660 		}
661 		if (ISSET(sc->sc_caps, SMC_CAPS_SD_HIGHSPEED) &&
662 		    best_func != 0) {
663 			DPRINTF(("%s: switch func mode 1(func=%d)\n",
664 			    SDMMCDEVNAME(sc), best_func));
665 			error =
666 			    sdmmc_mem_sd_switch(sf, 1, 1, best_func, &status);
667 			if (error) {
668 				aprint_error_dev(sc->sc_dev,
669 				    "switch func mode 1 failed:"
670 				    " group 1 function %d(0x%2x)\n",
671 				    best_func, support_func);
672 				return error;
673 			}
674 			sf->csd.tran_speed =
675 			    switch_group0_functions[best_func].freq;
676 
677 			/* Wait 400KHz x 8 clock (2.5us * 8 + slop) */
678 			delay(25);
679 		}
680 	}
681 
682 	/* change bus clock */
683 	if (sc->sc_busclk > sf->csd.tran_speed)
684 		sc->sc_busclk = sf->csd.tran_speed;
685 	error = sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, sc->sc_busclk);
686 	if (error) {
687 		aprint_error_dev(sc->sc_dev, "can't change bus clock\n");
688 		return error;
689 	}
690 
691 	return 0;
692 }
693 
694 static int
695 sdmmc_mem_mmc_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
696 {
697 	int width, value, hs_timing, error;
698 	char ext_csd[512];
699 
700 	if (sf->csd.mmcver >= MMC_CSD_MMCVER_4_0) {
701 		error = sdmmc_mem_send_cxd_data(sc,
702 		    MMC_SEND_EXT_CSD, ext_csd, sizeof(ext_csd));
703 		if (error) {
704 			aprint_error_dev(sc->sc_dev, "can't read EXT_CSD\n");
705 			return error;
706 		}
707 		if ((sf->csd.csdver == MMC_CSD_CSDVER_EXT_CSD) &&
708 		    (ext_csd[EXT_CSD_STRUCTURE] > EXT_CSD_STRUCTURE_VER_1_2)) {
709 			aprint_error_dev(sc->sc_dev,
710 			    "unrecognised future version (%d)\n",
711 				ext_csd[EXT_CSD_STRUCTURE]);
712 			return error;
713 		}
714 		hs_timing = 0;
715 		switch (ext_csd[EXT_CSD_CARD_TYPE]) {
716 		case EXT_CSD_CARD_TYPE_26M:
717 			sf->csd.tran_speed = 26000;	/* 26MHz */
718 			break;
719 
720 		case EXT_CSD_CARD_TYPE_52M | EXT_CSD_CARD_TYPE_26M:
721 			sf->csd.tran_speed = 52000;	/* 52MHz */
722 			hs_timing = 1;
723 			break;
724 
725 		default:
726 			aprint_error_dev(sc->sc_dev,
727 			    "unknwon CARD_TYPE: 0x%x\n",
728 			    ext_csd[EXT_CSD_CARD_TYPE]);
729 			return error;
730 		}
731 
732 		if (!ISSET(sc->sc_caps, SMC_CAPS_MMC_HIGHSPEED)) {
733 			hs_timing = 0;
734 		}
735 		if (hs_timing) {
736 			error = sdmmc_mem_mmc_switch(sf, EXT_CSD_CMD_SET_NORMAL,
737 			    EXT_CSD_HS_TIMING, hs_timing);
738 			if (error) {
739 				aprint_error_dev(sc->sc_dev,
740 				    "can't change high speed\n");
741 				return error;
742 			}
743 		}
744 
745 		if (sc->sc_busclk > sf->csd.tran_speed)
746 			sc->sc_busclk = sf->csd.tran_speed;
747 		error =
748 		    sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, sc->sc_busclk);
749 		if (error) {
750 			aprint_error_dev(sc->sc_dev,
751 			    "can't change bus clock\n");
752 			return error;
753 		}
754 
755 		if (hs_timing) {
756 			error = sdmmc_mem_send_cxd_data(sc,
757 			    MMC_SEND_EXT_CSD, ext_csd, sizeof(ext_csd));
758 			if (error) {
759 				aprint_error_dev(sc->sc_dev,
760 				    "can't re-read EXT_CSD\n");
761 				return error;
762 			}
763 			if (ext_csd[EXT_CSD_HS_TIMING] != hs_timing) {
764 				aprint_error_dev(sc->sc_dev,
765 				    "HS_TIMING set failed\n");
766 				return EINVAL;
767 			}
768 		}
769 
770 		if (ISSET(sc->sc_caps, SMC_CAPS_8BIT_MODE)) {
771 			width = 8;
772 			value = EXT_CSD_BUS_WIDTH_8;
773 		} else if (ISSET(sc->sc_caps, SMC_CAPS_4BIT_MODE)) {
774 			width = 4;
775 			value = EXT_CSD_BUS_WIDTH_4;
776 		} else {
777 			width = 1;
778 			value = EXT_CSD_BUS_WIDTH_1;
779 		}
780 
781 		if (width != 1) {
782 			error = sdmmc_mem_mmc_switch(sf, EXT_CSD_CMD_SET_NORMAL,
783 			    EXT_CSD_BUS_WIDTH, value);
784 			if (error == 0)
785 				error = sdmmc_chip_bus_width(sc->sc_sct,
786 				    sc->sc_sch, width);
787 			else {
788 				DPRINTF(("%s: can't change bus width"
789 				    " (%d bit)\n", SDMMCDEVNAME(sc), width));
790 				return error;
791 			}
792 
793 			/* XXXX: need bus test? (using by CMD14 & CMD19) */
794 		}
795 		sf->width = width;
796 	} else {
797 		if (sc->sc_busclk > sf->csd.tran_speed)
798 			sc->sc_busclk = sf->csd.tran_speed;
799 		error =
800 		    sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, sc->sc_busclk);
801 		if (error) {
802 			aprint_error_dev(sc->sc_dev,
803 			    "can't change bus clock\n");
804 			return error;
805 		}
806 	}
807 
808 	return 0;
809 }
810 
811 static int
812 sdmmc_mem_send_cid(struct sdmmc_softc *sc, sdmmc_response *resp)
813 {
814 	struct sdmmc_command cmd;
815 	int error;
816 
817 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
818 		memset(&cmd, 0, sizeof cmd);
819 		cmd.c_opcode = MMC_ALL_SEND_CID;
820 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R2;
821 
822 		error = sdmmc_mmc_command(sc, &cmd);
823 	} else {
824 		error = sdmmc_mem_send_cxd_data(sc, MMC_SEND_CID, &cmd.c_resp,
825 		    sizeof(cmd.c_resp));
826 	}
827 
828 #ifdef SDMMC_DEBUG
829 	sdmmc_dump_data("CID", cmd.c_resp, sizeof(cmd.c_resp));
830 #endif
831 	if (error == 0 && resp != NULL)
832 		memcpy(resp, &cmd.c_resp, sizeof(*resp));
833 	return error;
834 }
835 
836 static int
837 sdmmc_mem_send_csd(struct sdmmc_softc *sc, struct sdmmc_function *sf,
838     sdmmc_response *resp)
839 {
840 	struct sdmmc_command cmd;
841 	int error;
842 
843 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
844 		memset(&cmd, 0, sizeof cmd);
845 		cmd.c_opcode = MMC_SEND_CSD;
846 		cmd.c_arg = MMC_ARG_RCA(sf->rca);
847 		cmd.c_flags = SCF_CMD_AC | SCF_RSP_R2;
848 
849 		error = sdmmc_mmc_command(sc, &cmd);
850 	} else {
851 		error = sdmmc_mem_send_cxd_data(sc, MMC_SEND_CSD, &cmd.c_resp,
852 		    sizeof(cmd.c_resp));
853 	}
854 
855 #ifdef SDMMC_DEBUG
856 	sdmmc_dump_data("CSD", cmd.c_resp, sizeof(cmd.c_resp));
857 #endif
858 	if (error == 0 && resp != NULL)
859 		memcpy(resp, &cmd.c_resp, sizeof(*resp));
860 	return error;
861 }
862 
863 static int
864 sdmmc_mem_send_scr(struct sdmmc_softc *sc, struct sdmmc_function *sf,
865     uint32_t scr[2])
866 {
867 	struct sdmmc_command cmd;
868 	bus_dma_segment_t ds[1];
869 	void *ptr = NULL;
870 	int datalen = 8;
871 	int rseg;
872 	int error = 0;
873 
874 	/* Don't lock */
875 
876 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
877 		error = bus_dmamem_alloc(sc->sc_dmat, datalen, PAGE_SIZE, 0,
878 		    ds, 1, &rseg, BUS_DMA_NOWAIT);
879 		if (error)
880 			goto out;
881 		error = bus_dmamem_map(sc->sc_dmat, ds, 1, datalen, &ptr,
882 		    BUS_DMA_NOWAIT);
883 		if (error)
884 			goto dmamem_free;
885 		error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, ptr, datalen,
886 		    NULL, BUS_DMA_NOWAIT|BUS_DMA_STREAMING|BUS_DMA_READ);
887 		if (error)
888 			goto dmamem_unmap;
889 
890 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
891 		    BUS_DMASYNC_PREREAD);
892 	} else {
893 		ptr = malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO);
894 		if (ptr == NULL)
895 			goto out;
896 	}
897 
898 	memset(&cmd, 0, sizeof(cmd));
899 	cmd.c_data = ptr;
900 	cmd.c_datalen = datalen;
901 	cmd.c_blklen = datalen;
902 	cmd.c_arg = 0;
903 	cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
904 	cmd.c_opcode = SD_APP_SEND_SCR;
905 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
906 		cmd.c_dmamap = sc->sc_dmap;
907 
908 	error = sdmmc_app_command(sc, sf, &cmd);
909 	if (error == 0) {
910 		if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
911 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
912 			    BUS_DMASYNC_POSTREAD);
913 		}
914 		memcpy(scr, ptr, datalen);
915 	}
916 
917 out:
918 	if (ptr != NULL) {
919 		if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
920 			bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
921 dmamem_unmap:
922 			bus_dmamem_unmap(sc->sc_dmat, ptr, datalen);
923 dmamem_free:
924 			bus_dmamem_free(sc->sc_dmat, ds, rseg);
925 		} else {
926 			free(ptr, M_DEVBUF);
927 		}
928 	}
929 	DPRINTF(("%s: sdmem_mem_send_scr: error = %d\n", SDMMCDEVNAME(sc),
930 	    error));
931 
932 #ifdef SDMMC_DEBUG
933 	if (error == 0)
934 		sdmmc_dump_data("SCR", scr, 8);
935 #endif
936 	return error;
937 }
938 
939 static int
940 sdmmc_mem_decode_scr(struct sdmmc_softc *sc, struct sdmmc_function *sf)
941 {
942 	sdmmc_response resp;
943 	int ver;
944 
945 	memset(resp, 0, sizeof(resp));
946 	/*
947 	 * Change the raw-scr received from the DMA stream to resp.
948 	 */
949 	resp[0] = be32toh(sf->raw_scr[1]) >> 8;		// LSW
950 	resp[1] = be32toh(sf->raw_scr[0]);		// MSW
951 	resp[0] |= (resp[1] & 0xff) << 24;
952 	resp[1] >>= 8;
953 
954 	ver = SCR_STRUCTURE(resp);
955 	sf->scr.sd_spec = SCR_SD_SPEC(resp);
956 	sf->scr.bus_width = SCR_SD_BUS_WIDTHS(resp);
957 
958 	DPRINTF(("%s: sdmmc_mem_decode_scr: %08x%08x spec=%d, bus width=%d\n",
959 	    SDMMCDEVNAME(sc), resp[1], resp[0],
960 	    sf->scr.sd_spec, sf->scr.bus_width));
961 
962 	if (ver != 0) {
963 		DPRINTF(("%s: unknown structure version: %d\n",
964 		    SDMMCDEVNAME(sc), ver));
965 		return EINVAL;
966 	}
967 	return 0;
968 }
969 
970 static int
971 sdmmc_mem_send_cxd_data(struct sdmmc_softc *sc, int opcode, void *data,
972     size_t datalen)
973 {
974 	struct sdmmc_command cmd;
975 	bus_dma_segment_t ds[1];
976 	void *ptr = NULL;
977 	int rseg;
978 	int error = 0;
979 
980 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
981 		error = bus_dmamem_alloc(sc->sc_dmat, datalen, PAGE_SIZE, 0, ds,
982 		    1, &rseg, BUS_DMA_NOWAIT);
983 		if (error)
984 			goto out;
985 		error = bus_dmamem_map(sc->sc_dmat, ds, 1, datalen, &ptr,
986 		    BUS_DMA_NOWAIT);
987 		if (error)
988 			goto dmamem_free;
989 		error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, ptr, datalen,
990 		    NULL, BUS_DMA_NOWAIT|BUS_DMA_STREAMING|BUS_DMA_READ);
991 		if (error)
992 			goto dmamem_unmap;
993 
994 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
995 		    BUS_DMASYNC_PREREAD);
996 	} else {
997 		ptr = malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO);
998 		if (ptr == NULL)
999 			goto out;
1000 	}
1001 
1002 	memset(&cmd, 0, sizeof(cmd));
1003 	cmd.c_data = ptr;
1004 	cmd.c_datalen = datalen;
1005 	cmd.c_blklen = datalen;
1006 	cmd.c_opcode = opcode;
1007 	cmd.c_arg = 0;
1008 	cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_SPI_R1;
1009 	if (opcode == MMC_SEND_EXT_CSD)
1010 		SET(cmd.c_flags, SCF_RSP_R1);
1011 	else
1012 		SET(cmd.c_flags, SCF_RSP_R2);
1013 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
1014 		cmd.c_dmamap = sc->sc_dmap;
1015 
1016 	error = sdmmc_mmc_command(sc, &cmd);
1017 	if (error == 0) {
1018 		if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1019 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1020 			    BUS_DMASYNC_POSTREAD);
1021 		}
1022 		memcpy(data, ptr, datalen);
1023 #ifdef SDMMC_DEBUG
1024 		sdmmc_dump_data("CXD", data, datalen);
1025 #endif
1026 	}
1027 
1028 out:
1029 	if (ptr != NULL) {
1030 		if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1031 			bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
1032 dmamem_unmap:
1033 			bus_dmamem_unmap(sc->sc_dmat, ptr, datalen);
1034 dmamem_free:
1035 			bus_dmamem_free(sc->sc_dmat, ds, rseg);
1036 		} else {
1037 			free(ptr, M_DEVBUF);
1038 		}
1039 	}
1040 	return error;
1041 }
1042 
1043 static int
1044 sdmmc_set_bus_width(struct sdmmc_function *sf, int width)
1045 {
1046 	struct sdmmc_softc *sc = sf->sc;
1047 	struct sdmmc_command cmd;
1048 	int error;
1049 
1050 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
1051 		return ENODEV;
1052 
1053 	memset(&cmd, 0, sizeof(cmd));
1054 	cmd.c_opcode = SD_APP_SET_BUS_WIDTH;
1055 	cmd.c_flags = SCF_RSP_R1 | SCF_CMD_AC;
1056 
1057 	switch (width) {
1058 	case 1:
1059 		cmd.c_arg = SD_ARG_BUS_WIDTH_1;
1060 		break;
1061 
1062 	case 4:
1063 		cmd.c_arg = SD_ARG_BUS_WIDTH_4;
1064 		break;
1065 
1066 	default:
1067 		return EINVAL;
1068 	}
1069 
1070 	error = sdmmc_app_command(sc, sf, &cmd);
1071 	if (error == 0)
1072 		error = sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch, width);
1073 	return error;
1074 }
1075 
1076 static int
1077 sdmmc_mem_sd_switch(struct sdmmc_function *sf, int mode, int group,
1078     int function, sdmmc_bitfield512_t *status)
1079 {
1080 	struct sdmmc_softc *sc = sf->sc;
1081 	struct sdmmc_command cmd;
1082 	bus_dma_segment_t ds[1];
1083 	void *ptr = NULL;
1084 	int gsft, rseg, error = 0;
1085 	const int statlen = 64;
1086 
1087 	if (sf->scr.sd_spec >= SCR_SD_SPEC_VER_1_10 &&
1088 	    !ISSET(sf->csd.ccc, SD_CSD_CCC_SWITCH))
1089 		return EINVAL;
1090 
1091 	if (group <= 0 || group > 6 ||
1092 	    function < 0 || function > 15)
1093 		return EINVAL;
1094 
1095 	gsft = (group - 1) << 2;
1096 
1097 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1098 		error = bus_dmamem_alloc(sc->sc_dmat, statlen, PAGE_SIZE, 0, ds,
1099 		    1, &rseg, BUS_DMA_NOWAIT);
1100 		if (error)
1101 			goto out;
1102 		error = bus_dmamem_map(sc->sc_dmat, ds, 1, statlen, &ptr,
1103 		    BUS_DMA_NOWAIT);
1104 		if (error)
1105 			goto dmamem_free;
1106 		error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, ptr, statlen,
1107 		    NULL, BUS_DMA_NOWAIT|BUS_DMA_STREAMING|BUS_DMA_READ);
1108 		if (error)
1109 			goto dmamem_unmap;
1110 
1111 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, statlen,
1112 		    BUS_DMASYNC_PREREAD);
1113 	} else {
1114 		ptr = malloc(statlen, M_DEVBUF, M_NOWAIT | M_ZERO);
1115 		if (ptr == NULL)
1116 			goto out;
1117 	}
1118 
1119 	memset(&cmd, 0, sizeof(cmd));
1120 	cmd.c_data = ptr;
1121 	cmd.c_datalen = statlen;
1122 	cmd.c_blklen = statlen;
1123 	cmd.c_opcode = SD_SEND_SWITCH_FUNC;
1124 	cmd.c_arg =
1125 	    (!!mode << 31) | (function << gsft) | (0x00ffffff & ~(0xf << gsft));
1126 	cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
1127 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
1128 		cmd.c_dmamap = sc->sc_dmap;
1129 
1130 	error = sdmmc_mmc_command(sc, &cmd);
1131 	if (error == 0) {
1132 		if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1133 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, statlen,
1134 			    BUS_DMASYNC_POSTREAD);
1135 		}
1136 		memcpy(status, ptr, statlen);
1137 	}
1138 
1139 out:
1140 	if (ptr != NULL) {
1141 		if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1142 			bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
1143 dmamem_unmap:
1144 			bus_dmamem_unmap(sc->sc_dmat, ptr, statlen);
1145 dmamem_free:
1146 			bus_dmamem_free(sc->sc_dmat, ds, rseg);
1147 		} else {
1148 			free(ptr, M_DEVBUF);
1149 		}
1150 	}
1151 
1152 	if (error == 0)
1153 		sdmmc_be512_to_bitfield512(status);
1154 
1155 	return error;
1156 }
1157 
1158 static int
1159 sdmmc_mem_mmc_switch(struct sdmmc_function *sf, uint8_t set, uint8_t index,
1160     uint8_t value)
1161 {
1162 	struct sdmmc_softc *sc = sf->sc;
1163 	struct sdmmc_command cmd;
1164 
1165 	memset(&cmd, 0, sizeof(cmd));
1166 	cmd.c_opcode = MMC_SWITCH;
1167 	cmd.c_arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1168 	    (index << 16) | (value << 8) | set;
1169 	cmd.c_flags = SCF_RSP_SPI_R1B | SCF_RSP_R1B | SCF_CMD_AC;
1170 
1171 	return sdmmc_mmc_command(sc, &cmd);
1172 }
1173 
1174 /*
1175  * SPI mode function
1176  */
1177 static int
1178 sdmmc_mem_spi_read_ocr(struct sdmmc_softc *sc, uint32_t hcs, uint32_t *card_ocr)
1179 {
1180 	struct sdmmc_command cmd;
1181 	int error;
1182 
1183 	memset(&cmd, 0, sizeof(cmd));
1184 	cmd.c_opcode = MMC_READ_OCR;
1185 	cmd.c_arg = hcs ? MMC_OCR_HCS : 0;
1186 	cmd.c_flags = SCF_RSP_SPI_R3;
1187 
1188 	error = sdmmc_mmc_command(sc, &cmd);
1189 	if (error == 0 && card_ocr != NULL)
1190 		*card_ocr = cmd.c_resp[1];
1191 	DPRINTF(("%s: sdmmc_mem_spi_read_ocr: error=%d, ocr=%#x\n",
1192 	    SDMMCDEVNAME(sc), error, cmd.c_resp[1]));
1193 	return error;
1194 }
1195 
1196 /*
1197  * read/write function
1198  */
1199 /* read */
1200 static int
1201 sdmmc_mem_single_read_block(struct sdmmc_function *sf, uint32_t blkno,
1202     u_char *data, size_t datalen)
1203 {
1204 	struct sdmmc_softc *sc __unused = sf->sc;
1205 	int error = 0;
1206 	int i;
1207 
1208 	KASSERT((datalen % SDMMC_SECTOR_SIZE) == 0);
1209 	KASSERT(!ISSET(sc->sc_caps, SMC_CAPS_DMA));
1210 
1211 	for (i = 0; i < datalen / SDMMC_SECTOR_SIZE; i++) {
1212 		error = sdmmc_mem_read_block_subr(sf, blkno + i,
1213 		    data + i * SDMMC_SECTOR_SIZE, SDMMC_SECTOR_SIZE);
1214 		if (error)
1215 			break;
1216 	}
1217 	return error;
1218 }
1219 
1220 static int
1221 sdmmc_mem_read_block_subr(struct sdmmc_function *sf, uint32_t blkno,
1222     u_char *data, size_t datalen)
1223 {
1224 	struct sdmmc_softc *sc = sf->sc;
1225 	struct sdmmc_command cmd;
1226 	int error, bbuf, seg, off, len, num;
1227 
1228 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
1229 		error = sdmmc_select_card(sc, sf);
1230 		if (error)
1231 			goto out;
1232 	}
1233 
1234 	bbuf = 0;
1235 	num = 0;
1236 	seg = off = len = 0;
1237 retry:
1238 	memset(&cmd, 0, sizeof(cmd));
1239 	cmd.c_data = data;
1240 	cmd.c_datalen = datalen;
1241 	cmd.c_blklen = SDMMC_SECTOR_SIZE;
1242 	cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
1243 	    MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE;
1244 	cmd.c_arg = blkno;
1245 	if (!ISSET(sf->flags, SFF_SDHC))
1246 		cmd.c_arg <<= SDMMC_SECTOR_SIZE_SB;
1247 	cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
1248 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1249 		cmd.c_dmamap = sc->sc_dmap;
1250 		if (!ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
1251 			len = sc->sc_dmap->dm_segs[seg].ds_len - off;
1252 			len &= ~(SDMMC_SECTOR_SIZE - 1);
1253 			cmd.c_datalen = len;
1254 			cmd.c_dmaseg = seg;
1255 			cmd.c_dmaoff = off;
1256 			bbuf = 0;
1257 			if (len == 0) {
1258 				/* Use bounce buffer */
1259 				bus_dmamap_sync(sc->sc_dmat, sf->bbuf_dmap,
1260 				    0, SDMMC_SECTOR_SIZE, BUS_DMASYNC_PREREAD);
1261 				cmd.c_datalen = SDMMC_SECTOR_SIZE;
1262 				cmd.c_dmamap = sf->bbuf_dmap;
1263 				cmd.c_dmaseg = 0;
1264 				cmd.c_dmaoff = 0;
1265 				bbuf = 1;
1266 				len = SDMMC_SECTOR_SIZE;
1267 			}
1268 			cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
1269 			    MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE;
1270 		}
1271 	}
1272 
1273 	error = sdmmc_mmc_command(sc, &cmd);
1274 	if (error)
1275 		goto out;
1276 
1277 	if (!ISSET(sc->sc_caps, SMC_CAPS_AUTO_STOP)) {
1278 		if (cmd.c_opcode == MMC_READ_BLOCK_MULTIPLE) {
1279 			memset(&cmd, 0, sizeof cmd);
1280 			cmd.c_opcode = MMC_STOP_TRANSMISSION;
1281 			cmd.c_arg = MMC_ARG_RCA(sf->rca);
1282 			cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B;
1283 			error = sdmmc_mmc_command(sc, &cmd);
1284 			if (error)
1285 				goto out;
1286 		}
1287 	}
1288 
1289 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
1290 		do {
1291 			memset(&cmd, 0, sizeof(cmd));
1292 			cmd.c_opcode = MMC_SEND_STATUS;
1293 			if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
1294 				cmd.c_arg = MMC_ARG_RCA(sf->rca);
1295 			cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R2;
1296 			error = sdmmc_mmc_command(sc, &cmd);
1297 			if (error)
1298 				break;
1299 			/* XXX time out */
1300 		} while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
1301 	}
1302 
1303 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
1304 	    !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
1305 		bus_dma_segment_t *dm_segs = sc->sc_dmap->dm_segs;
1306 
1307 		if (bbuf) {
1308 			bus_dmamap_sync(sc->sc_dmat, sf->bbuf_dmap,
1309 			    0, SDMMC_SECTOR_SIZE, BUS_DMASYNC_POSTREAD);
1310 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, num,
1311 			    SDMMC_SECTOR_SIZE, BUS_DMASYNC_POSTREAD);
1312 			memcpy(data, sf->bbuf, SDMMC_SECTOR_SIZE);
1313 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, num,
1314 			    SDMMC_SECTOR_SIZE, BUS_DMASYNC_PREREAD);
1315 		}
1316 		num += len;
1317 		data += len;
1318 		datalen -= len;
1319 		blkno += (len / SDMMC_SECTOR_SIZE);
1320 
1321 		while (off + len >= dm_segs[seg].ds_len) {
1322 			len -= dm_segs[seg++].ds_len;
1323 			off = 0;
1324 		}
1325 		off += len;
1326 
1327 		if (seg < sc->sc_dmap->dm_nsegs)
1328 			goto retry;
1329 	}
1330 
1331 out:
1332 	return error;
1333 }
1334 
1335 int
1336 sdmmc_mem_read_block(struct sdmmc_function *sf, uint32_t blkno, u_char *data,
1337     size_t datalen)
1338 {
1339 	struct sdmmc_softc *sc = sf->sc;
1340 	int error;
1341 
1342 	SDMMC_LOCK(sc);
1343 
1344 	if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) {
1345 		error = sdmmc_mem_single_read_block(sf, blkno, data, datalen);
1346 		goto out;
1347 	}
1348 
1349 	if (!ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1350 		error = sdmmc_mem_read_block_subr(sf, blkno, data, datalen);
1351 		goto out;
1352 	}
1353 
1354 	/* DMA transfer */
1355 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, data, datalen, NULL,
1356 	    BUS_DMA_NOWAIT|BUS_DMA_READ);
1357 	if (error)
1358 		goto out;
1359 
1360 #ifdef SDMMC_DEBUG
1361 	for (int i = 0; i < sc->sc_dmap->dm_nsegs; i++) {
1362 		printf("seg#%d: addr=%#lx, size=%#lx\n", i,
1363 		    (u_long)sc->sc_dmap->dm_segs[i].ds_addr,
1364 		    (u_long)sc->sc_dmap->dm_segs[i].ds_len);
1365 	}
1366 #endif
1367 
1368 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1369 	    BUS_DMASYNC_PREREAD);
1370 
1371 	error = sdmmc_mem_read_block_subr(sf, blkno, data, datalen);
1372 	if (error)
1373 		goto unload;
1374 
1375 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1376 	    BUS_DMASYNC_POSTREAD);
1377 unload:
1378 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
1379 
1380 out:
1381 	SDMMC_UNLOCK(sc);
1382 
1383 	return error;
1384 }
1385 
1386 /* write */
1387 static int
1388 sdmmc_mem_single_write_block(struct sdmmc_function *sf, uint32_t blkno,
1389     u_char *data, size_t datalen)
1390 {
1391 	struct sdmmc_softc *sc __unused = sf->sc;
1392 	int error = 0;
1393 	int i;
1394 
1395 	KASSERT((datalen % SDMMC_SECTOR_SIZE) == 0);
1396 	KASSERT(!ISSET(sc->sc_caps, SMC_CAPS_DMA));
1397 
1398 	for (i = 0; i < datalen / SDMMC_SECTOR_SIZE; i++) {
1399 		error = sdmmc_mem_write_block_subr(sf, blkno + i,
1400 		    data + i * SDMMC_SECTOR_SIZE, SDMMC_SECTOR_SIZE);
1401 		if (error)
1402 			break;
1403 	}
1404 	return error;
1405 }
1406 
1407 static int
1408 sdmmc_mem_write_block_subr(struct sdmmc_function *sf, uint32_t blkno,
1409     u_char *data, size_t datalen)
1410 {
1411 	struct sdmmc_softc *sc = sf->sc;
1412 	struct sdmmc_command cmd;
1413 	int error, bbuf, seg, off, len, num;
1414 
1415 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
1416 		error = sdmmc_select_card(sc, sf);
1417 		if (error)
1418 			goto out;
1419 	}
1420 
1421 	bbuf = 0;
1422 	num = 0;
1423 	seg = off = len = 0;
1424 retry:
1425 	memset(&cmd, 0, sizeof(cmd));
1426 	cmd.c_data = data;
1427 	cmd.c_datalen = datalen;
1428 	cmd.c_blklen = SDMMC_SECTOR_SIZE;
1429 	cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
1430 	    MMC_WRITE_BLOCK_MULTIPLE : MMC_WRITE_BLOCK_SINGLE;
1431 	cmd.c_arg = blkno;
1432 	if (!ISSET(sf->flags, SFF_SDHC))
1433 		cmd.c_arg <<= SDMMC_SECTOR_SIZE_SB;
1434 	cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R1;
1435 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1436 		cmd.c_dmamap = sc->sc_dmap;
1437 		if (!ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
1438 			len = sc->sc_dmap->dm_segs[seg].ds_len - off;
1439 			len &= ~(SDMMC_SECTOR_SIZE - 1);
1440 			cmd.c_datalen = len;
1441 			cmd.c_dmaseg = seg;
1442 			cmd.c_dmaoff = off;
1443 			bbuf = 0;
1444 			if (len == 0) {
1445 				/* Use bounce buffer */
1446 				bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, num,
1447 				    SDMMC_SECTOR_SIZE, BUS_DMASYNC_POSTWRITE);
1448 				memcpy(sf->bbuf, data, SDMMC_SECTOR_SIZE);
1449 				bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, num,
1450 				    SDMMC_SECTOR_SIZE, BUS_DMASYNC_PREWRITE);
1451 				bus_dmamap_sync(sc->sc_dmat, sf->bbuf_dmap, 0,
1452 				    SDMMC_SECTOR_SIZE, BUS_DMASYNC_PREWRITE);
1453 				cmd.c_datalen = SDMMC_SECTOR_SIZE;
1454 				cmd.c_dmamap = sf->bbuf_dmap;
1455 				cmd.c_dmaseg = 0;
1456 				cmd.c_dmaoff = 0;
1457 				bbuf = 1;
1458 				len = SDMMC_SECTOR_SIZE;
1459 			}
1460 			cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
1461 			    MMC_WRITE_BLOCK_MULTIPLE : MMC_WRITE_BLOCK_SINGLE;
1462 		}
1463 	}
1464 
1465 	error = sdmmc_mmc_command(sc, &cmd);
1466 	if (error)
1467 		goto out;
1468 
1469 	if (!ISSET(sc->sc_caps, SMC_CAPS_AUTO_STOP)) {
1470 		if (cmd.c_opcode == MMC_WRITE_BLOCK_MULTIPLE) {
1471 			memset(&cmd, 0, sizeof(cmd));
1472 			cmd.c_opcode = MMC_STOP_TRANSMISSION;
1473 			cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B;
1474 			error = sdmmc_mmc_command(sc, &cmd);
1475 			if (error)
1476 				goto out;
1477 		}
1478 	}
1479 
1480 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
1481 		do {
1482 			memset(&cmd, 0, sizeof(cmd));
1483 			cmd.c_opcode = MMC_SEND_STATUS;
1484 			if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
1485 				cmd.c_arg = MMC_ARG_RCA(sf->rca);
1486 			cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R2;
1487 			error = sdmmc_mmc_command(sc, &cmd);
1488 			if (error)
1489 				break;
1490 			/* XXX time out */
1491 		} while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
1492 	}
1493 
1494 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA) &&
1495 	    !ISSET(sc->sc_caps, SMC_CAPS_MULTI_SEG_DMA)) {
1496 		bus_dma_segment_t *dm_segs = sc->sc_dmap->dm_segs;
1497 
1498 		if (bbuf)
1499 			bus_dmamap_sync(sc->sc_dmat, sf->bbuf_dmap,
1500 			    0, SDMMC_SECTOR_SIZE, BUS_DMASYNC_POSTWRITE);
1501 		num += len;
1502 		data += len;
1503 		datalen -= len;
1504 		blkno += (len / SDMMC_SECTOR_SIZE);
1505 
1506 		while (off + len >= dm_segs[seg].ds_len) {
1507 			len -= dm_segs[seg++].ds_len;
1508 			off = 0;
1509 		}
1510 		off += len;
1511 
1512 		if (seg < sc->sc_dmap->dm_nsegs)
1513 			goto retry;
1514 	}
1515 
1516 out:
1517 	return error;
1518 }
1519 
1520 int
1521 sdmmc_mem_write_block(struct sdmmc_function *sf, uint32_t blkno, u_char *data,
1522     size_t datalen)
1523 {
1524 	struct sdmmc_softc *sc = sf->sc;
1525 	int error;
1526 
1527 	SDMMC_LOCK(sc);
1528 
1529 	if (sdmmc_chip_write_protect(sc->sc_sct, sc->sc_sch)) {
1530 		aprint_normal_dev(sc->sc_dev, "write-protected\n");
1531 		error = EIO;
1532 		goto out;
1533 	}
1534 
1535 	if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) {
1536 		error = sdmmc_mem_single_write_block(sf, blkno, data, datalen);
1537 		goto out;
1538 	}
1539 
1540 	if (!ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
1541 		error = sdmmc_mem_write_block_subr(sf, blkno, data, datalen);
1542 		goto out;
1543 	}
1544 
1545 	/* DMA transfer */
1546 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, data, datalen, NULL,
1547 	    BUS_DMA_NOWAIT|BUS_DMA_WRITE);
1548 	if (error)
1549 		goto out;
1550 
1551 #ifdef SDMMC_DEBUG
1552 	for (int i = 0; i < sc->sc_dmap->dm_nsegs; i++) {
1553 		printf("seg#%d: addr=%#lx, size=%#lx\n", i,
1554 		    (u_long)sc->sc_dmap->dm_segs[i].ds_addr,
1555 		    (u_long)sc->sc_dmap->dm_segs[i].ds_len);
1556 	}
1557 #endif
1558 
1559 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1560 	    BUS_DMASYNC_PREWRITE);
1561 
1562 	error = sdmmc_mem_write_block_subr(sf, blkno, data, datalen);
1563 	if (error)
1564 		goto unload;
1565 
1566 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
1567 	    BUS_DMASYNC_POSTWRITE);
1568 unload:
1569 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
1570 
1571 out:
1572 	SDMMC_UNLOCK(sc);
1573 
1574 	return error;
1575 }
1576