Home | History | Annotate | Line # | Download | only in sdmmc
sdmmc_mem.c revision 1.11
      1 /*	$NetBSD: sdmmc_mem.c,v 1.11 2010/09/23 12:03:27 kiyohara 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 (at) 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-2010 NONAKA Kimihiro <nonaka (at) 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 REGENTS AND CONTRIBUTORS ``AS IS'' AND
     34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     43  * SUCH DAMAGE.
     44  */
     45 
     46 /* Routines for SD/MMC memory cards. */
     47 
     48 #include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: sdmmc_mem.c,v 1.11 2010/09/23 12:03:27 kiyohara Exp $");
     50 
     51 #include <sys/param.h>
     52 #include <sys/kernel.h>
     53 #include <sys/malloc.h>
     54 #include <sys/systm.h>
     55 #include <sys/device.h>
     56 
     57 #include <uvm/uvm_extern.h>
     58 
     59 #include <dev/sdmmc/sdmmcchip.h>
     60 #include <dev/sdmmc/sdmmcreg.h>
     61 #include <dev/sdmmc/sdmmcvar.h>
     62 
     63 #ifdef SDMMC_DEBUG
     64 #define DPRINTF(s)	do { printf s; } while (/*CONSTCOND*/0)
     65 #else
     66 #define DPRINTF(s)	do {} while (/*CONSTCOND*/0)
     67 #endif
     68 
     69 static int sdmmc_mem_send_cid(struct sdmmc_softc *, sdmmc_response *);
     70 static int sdmmc_mem_send_csd(struct sdmmc_softc *, struct sdmmc_function *,
     71     sdmmc_response *);
     72 static int sdmmc_mem_send_scr(struct sdmmc_softc *, struct sdmmc_function *,
     73     uint32_t scr[2]);
     74 static int sdmmc_mem_decode_scr(struct sdmmc_softc *, struct sdmmc_function *);
     75 static int sdmmc_mem_send_cxd_data(struct sdmmc_softc *, int, void *, size_t);
     76 static int sdmmc_set_bus_width(struct sdmmc_function *, int);
     77 static int sdmmc_mem_mmc_switch(struct sdmmc_function *, uint8_t, uint8_t,
     78     uint8_t);
     79 static int sdmmc_mem_spi_read_ocr(struct sdmmc_softc *, uint32_t, uint32_t *);
     80 static int sdmmc_mem_single_read_block(struct sdmmc_function *, uint32_t,
     81     u_char *, size_t);
     82 static int sdmmc_mem_single_write_block(struct sdmmc_function *, uint32_t,
     83     u_char *, size_t);
     84 static int sdmmc_mem_read_block_subr(struct sdmmc_function *, uint32_t,
     85     u_char *, size_t);
     86 static int sdmmc_mem_write_block_subr(struct sdmmc_function *, uint32_t,
     87     u_char *, size_t);
     88 
     89 /*
     90  * Initialize SD/MMC memory cards and memory in SDIO "combo" cards.
     91  */
     92 int
     93 sdmmc_mem_enable(struct sdmmc_softc *sc)
     94 {
     95 	uint32_t host_ocr;
     96 	uint32_t card_ocr;
     97 	uint32_t ocr = 0;
     98 	int error;
     99 
    100 	SDMMC_LOCK(sc);
    101 
    102 	/* Set host mode to SD "combo" card or SD memory-only. */
    103 	SET(sc->sc_flags, SMF_SD_MODE|SMF_MEM_MODE);
    104 
    105 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
    106 		sdmmc_spi_chip_initialize(sc->sc_spi_sct, sc->sc_sch);
    107 
    108 	/* Reset memory (*must* do that before CMD55 or CMD1). */
    109 	sdmmc_go_idle_state(sc);
    110 
    111 	/* Check SD Ver.2 */
    112 	error = sdmmc_mem_send_if_cond(sc, 0x1aa, &card_ocr);
    113 	if (error == 0 && card_ocr == 0x1aa)
    114 		SET(ocr, MMC_OCR_HCS);
    115 
    116 	/*
    117 	 * Read the SD/MMC memory OCR value by issuing CMD55 followed
    118 	 * by ACMD41 to read the OCR value from memory-only SD cards.
    119 	 * MMC cards will not respond to CMD55 or ACMD41 and this is
    120 	 * how we distinguish them from SD cards.
    121 	 */
    122 mmc_mode:
    123 	error = sdmmc_mem_send_op_cond(sc,
    124 	  ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ? ocr : 0, &card_ocr);
    125 	if (error) {
    126 		if (ISSET(sc->sc_flags, SMF_SD_MODE) &&
    127 		    !ISSET(sc->sc_flags, SMF_IO_MODE)) {
    128 			/* Not a SD card, switch to MMC mode. */
    129 			DPRINTF(("%s: switch to MMC mode\n", SDMMCDEVNAME(sc)));
    130 			CLR(sc->sc_flags, SMF_SD_MODE);
    131 			goto mmc_mode;
    132 		}
    133 		if (!ISSET(sc->sc_flags, SMF_SD_MODE)) {
    134 			DPRINTF(("%s: couldn't read memory OCR\n",
    135 			    SDMMCDEVNAME(sc)));
    136 			goto out;
    137 		} else {
    138 			/* Not a "combo" card. */
    139 			CLR(sc->sc_flags, SMF_MEM_MODE);
    140 			error = 0;
    141 			goto out;
    142 		}
    143 	}
    144 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    145 		/* get card OCR */
    146 		error = sdmmc_mem_spi_read_ocr(sc, ocr, &card_ocr);
    147 		if (error) {
    148 			DPRINTF(("%s: couldn't read SPI memory OCR\n",
    149 			    SDMMCDEVNAME(sc)));
    150 			goto out;
    151 		}
    152 	}
    153 
    154 	/* Set the lowest voltage supported by the card and host. */
    155 	host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
    156 	error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
    157 	if (error) {
    158 		DPRINTF(("%s: couldn't supply voltage requested by card\n",
    159 		    SDMMCDEVNAME(sc)));
    160 		goto out;
    161 	}
    162 	host_ocr &= card_ocr;
    163 	host_ocr |= ocr;
    164 
    165 	/* Send the new OCR value until all cards are ready. */
    166 	error = sdmmc_mem_send_op_cond(sc, host_ocr, NULL);
    167 	if (error) {
    168 		DPRINTF(("%s: couldn't send memory OCR\n", SDMMCDEVNAME(sc)));
    169 		goto out;
    170 	}
    171 
    172 out:
    173 	SDMMC_UNLOCK(sc);
    174 
    175 	return error;
    176 }
    177 
    178 /*
    179  * Read the CSD and CID from all cards and assign each card a unique
    180  * relative card address (RCA).  CMD2 is ignored by SDIO-only cards.
    181  */
    182 void
    183 sdmmc_mem_scan(struct sdmmc_softc *sc)
    184 {
    185 	sdmmc_response resp;
    186 	struct sdmmc_function *sf;
    187 	uint16_t next_rca;
    188 	int error;
    189 	int retry;
    190 
    191 	SDMMC_LOCK(sc);
    192 
    193 	/*
    194 	 * CMD2 is a broadcast command understood by SD cards and MMC
    195 	 * cards.  All cards begin to respond to the command, but back
    196 	 * off if another card drives the CMD line to a different level.
    197 	 * Only one card will get its entire response through.  That
    198 	 * card remains silent once it has been assigned a RCA.
    199 	 */
    200 	for (retry = 0; retry < 100; retry++) {
    201 		error = sdmmc_mem_send_cid(sc, &resp);
    202 		if (error) {
    203 			if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) &&
    204 			    error == ETIMEDOUT) {
    205 				/* No more cards there. */
    206 				break;
    207 			}
    208 			DPRINTF(("%s: couldn't read CID\n", SDMMCDEVNAME(sc)));
    209 			break;
    210 		}
    211 
    212 		/* In MMC mode, find the next available RCA. */
    213 		next_rca = 1;
    214 		if (!ISSET(sc->sc_flags, SMF_SD_MODE)) {
    215 			SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list)
    216 				next_rca++;
    217 		}
    218 
    219 		/* Allocate a sdmmc_function structure. */
    220 		sf = sdmmc_function_alloc(sc);
    221 		sf->rca = next_rca;
    222 
    223 		/*
    224 		 * Remember the CID returned in the CMD2 response for
    225 		 * later decoding.
    226 		 */
    227 		memcpy(sf->raw_cid, resp, sizeof(sf->raw_cid));
    228 
    229 		/*
    230 		 * Silence the card by assigning it a unique RCA, or
    231 		 * querying it for its RCA in the case of SD.
    232 		 */
    233 		if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    234 			if (sdmmc_set_relative_addr(sc, sf) != 0) {
    235 				aprint_error_dev(sc->sc_dev,
    236 				    "couldn't set mem RCA\n");
    237 				sdmmc_function_free(sf);
    238 				break;
    239 			}
    240 		}
    241 
    242 		/*
    243 		 * If this is a memory-only card, the card responding
    244 		 * first becomes an alias for SDIO function 0.
    245 		 */
    246 		if (sc->sc_fn0 == NULL)
    247 			sc->sc_fn0 = sf;
    248 
    249 		SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
    250 
    251 		/* only one function in SPI mode */
    252 		if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
    253 			break;
    254 	}
    255 
    256 	/*
    257 	 * All cards are either inactive or awaiting further commands.
    258 	 * Read the CSDs and decode the raw CID for each card.
    259 	 */
    260 	SIMPLEQ_FOREACH(sf, &sc->sf_head, sf_list) {
    261 		error = sdmmc_mem_send_csd(sc, sf, &resp);
    262 		if (error) {
    263 			SET(sf->flags, SFF_ERROR);
    264 			continue;
    265 		}
    266 
    267 		if (sdmmc_decode_csd(sc, resp, sf) != 0 ||
    268 		    sdmmc_decode_cid(sc, sf->raw_cid, sf) != 0) {
    269 			SET(sf->flags, SFF_ERROR);
    270 			continue;
    271 		}
    272 
    273 #ifdef SDMMC_DEBUG
    274 		printf("%s: CID: ", SDMMCDEVNAME(sc));
    275 		sdmmc_print_cid(&sf->cid);
    276 #endif
    277 	}
    278 
    279 	SDMMC_UNLOCK(sc);
    280 }
    281 
    282 int
    283 sdmmc_decode_csd(struct sdmmc_softc *sc, sdmmc_response resp,
    284     struct sdmmc_function *sf)
    285 {
    286 	/* TRAN_SPEED(2:0): transfer rate exponent */
    287 	static const int speed_exponent[8] = {
    288 		100 *    1,	/* 100 Kbits/s */
    289 		  1 * 1000,	/*   1 Mbits/s */
    290 		 10 * 1000,	/*  10 Mbits/s */
    291 		100 * 1000,	/* 100 Mbits/s */
    292 		         0,
    293 		         0,
    294 		         0,
    295 		         0,
    296 	};
    297 	/* TRAN_SPEED(6:3): time mantissa */
    298 	static const int speed_mantissa[16] = {
    299 		0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80,
    300 	};
    301 	struct sdmmc_csd *csd = &sf->csd;
    302 	int e, m;
    303 
    304 	if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
    305 		/*
    306 		 * CSD version 1.0 corresponds to SD system
    307 		 * specification version 1.0 - 1.10. (SanDisk, 3.5.3)
    308 		 */
    309 		csd->csdver = SD_CSD_CSDVER(resp);
    310 		switch (csd->csdver) {
    311 		case SD_CSD_CSDVER_2_0:
    312 			DPRINTF(("%s: SD Ver.2.0\n", SDMMCDEVNAME(sc)));
    313 			SET(sf->flags, SFF_SDHC);
    314 			csd->capacity = SD_CSD_V2_CAPACITY(resp);
    315 			csd->read_bl_len = SD_CSD_V2_BL_LEN;
    316 			break;
    317 
    318 		case SD_CSD_CSDVER_1_0:
    319 			DPRINTF(("%s: SD Ver.1.0\n", SDMMCDEVNAME(sc)));
    320 			csd->capacity = SD_CSD_CAPACITY(resp);
    321 			csd->read_bl_len = SD_CSD_READ_BL_LEN(resp);
    322 			break;
    323 
    324 		default:
    325 			aprint_error_dev(sc->sc_dev,
    326 			    "unknown SD CSD structure version 0x%x\n",
    327 			    csd->csdver);
    328 			return 1;
    329 		}
    330 
    331 		csd->mmcver = SD_CSD_MMCVER(resp);
    332 		csd->write_bl_len = SD_CSD_WRITE_BL_LEN(resp);
    333 		csd->r2w_factor = SD_CSD_R2W_FACTOR(resp);
    334 		e = SD_CSD_SPEED_EXP(resp);
    335 		m = SD_CSD_SPEED_MANT(resp);
    336 		csd->tran_speed = speed_exponent[e] * speed_mantissa[m] / 10;
    337 	} else {
    338 		csd->csdver = MMC_CSD_CSDVER(resp);
    339 		if (csd->csdver != MMC_CSD_CSDVER_1_0 &&
    340 		    csd->csdver != MMC_CSD_CSDVER_2_0) {
    341 			aprint_error_dev(sc->sc_dev,
    342 			    "unknown MMC CSD structure version 0x%x\n",
    343 			    csd->csdver);
    344 			return 1;
    345 		}
    346 
    347 		csd->mmcver = MMC_CSD_MMCVER(resp);
    348 		csd->capacity = MMC_CSD_CAPACITY(resp);
    349 		csd->read_bl_len = MMC_CSD_READ_BL_LEN(resp);
    350 		csd->write_bl_len = MMC_CSD_WRITE_BL_LEN(resp);
    351 		csd->r2w_factor = MMC_CSD_R2W_FACTOR(resp);
    352 		e = MMC_CSD_TRAN_SPEED_EXP(resp);
    353 		m = MMC_CSD_TRAN_SPEED_MANT(resp);
    354 		csd->tran_speed = speed_exponent[e] * speed_mantissa[m] / 10;
    355 	}
    356 	if ((1 << csd->read_bl_len) > SDMMC_SECTOR_SIZE)
    357 		csd->capacity *= (1 << csd->read_bl_len) / SDMMC_SECTOR_SIZE;
    358 
    359 	if (sc->sc_busclk > csd->tran_speed)
    360 		sc->sc_busclk = csd->tran_speed;
    361 
    362 #ifdef SDMMC_DUMP_CSD
    363 	sdmmc_print_csd(resp, csd);
    364 #endif
    365 
    366 	return 0;
    367 }
    368 
    369 int
    370 sdmmc_decode_cid(struct sdmmc_softc *sc, sdmmc_response resp,
    371     struct sdmmc_function *sf)
    372 {
    373 	struct sdmmc_cid *cid = &sf->cid;
    374 
    375 	if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
    376 		cid->mid = SD_CID_MID(resp);
    377 		cid->oid = SD_CID_OID(resp);
    378 		SD_CID_PNM_CPY(resp, cid->pnm);
    379 		cid->rev = SD_CID_REV(resp);
    380 		cid->psn = SD_CID_PSN(resp);
    381 		cid->mdt = SD_CID_MDT(resp);
    382 	} else {
    383 		switch(sf->csd.mmcver) {
    384 		case MMC_CSD_MMCVER_1_0:
    385 		case MMC_CSD_MMCVER_1_4:
    386 			cid->mid = MMC_CID_MID_V1(resp);
    387 			MMC_CID_PNM_V1_CPY(resp, cid->pnm);
    388 			cid->rev = MMC_CID_REV_V1(resp);
    389 			cid->psn = MMC_CID_PSN_V1(resp);
    390 			cid->mdt = MMC_CID_MDT_V1(resp);
    391 			break;
    392 		case MMC_CSD_MMCVER_2_0:
    393 		case MMC_CSD_MMCVER_3_1:
    394 		case MMC_CSD_MMCVER_4_0:
    395 			cid->mid = MMC_CID_MID_V2(resp);
    396 			cid->oid = MMC_CID_OID_V2(resp);
    397 			MMC_CID_PNM_V2_CPY(resp, cid->pnm);
    398 			cid->psn = MMC_CID_PSN_V2(resp);
    399 			break;
    400 		default:
    401 			aprint_error_dev(sc->sc_dev, "unknown MMC version %d\n",
    402 			    sf->csd.mmcver);
    403 			return 1;
    404 		}
    405 	}
    406 	return 0;
    407 }
    408 
    409 void
    410 sdmmc_print_cid(struct sdmmc_cid *cid)
    411 {
    412 
    413 	printf("mid=0x%02x oid=0x%04x pnm=\"%s\" rev=0x%02x psn=0x%08x"
    414 	    " mdt=%03x\n", cid->mid, cid->oid, cid->pnm, cid->rev, cid->psn,
    415 	    cid->mdt);
    416 }
    417 
    418 #ifdef SDMMC_DUMP_CSD
    419 void
    420 sdmmc_print_csd(sdmmc_response resp, struct sdmmc_csd *csd)
    421 {
    422 
    423 	printf("csdver = %d\n", csd->csdver);
    424 	printf("mmcver = %d\n", csd->mmcver);
    425 	printf("capacity = %08x\n", csd->capacity);
    426 	printf("read_bl_len = %d\n", csd->read_bl_len);
    427 	printf("write_cl_len = %d\n", csd->write_bl_len);
    428 	printf("r2w_factor = %d\n", csd->r2w_factor);
    429 	printf("tran_speed = %d\n", csd->tran_speed);
    430 }
    431 #endif
    432 
    433 /*
    434  * Initialize a SD/MMC memory card.
    435  */
    436 int
    437 sdmmc_mem_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    438 {
    439 	int width, value, error = 0;
    440 
    441 	SDMMC_LOCK(sc);
    442 
    443 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    444 		error = sdmmc_select_card(sc, sf);
    445 		if (error)
    446 			goto out;
    447 	}
    448 
    449 	if (!ISSET(sf->flags, SFF_SDHC)) {
    450 		error = sdmmc_mem_set_blocklen(sc, sf);
    451 		if (error)
    452 			goto out;
    453 	}
    454 
    455         /* change bus width if supported */
    456 	sf->width = 1;
    457 	if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
    458 		error = sdmmc_mem_send_scr(sc, sf, sf->raw_scr);
    459 		if (error) {
    460 			DPRINTF(("%s: SD_SEND_SCR send failed.\n",
    461 			    SDMMCDEVNAME(sc)));
    462 			goto out;
    463 		}
    464 		error = sdmmc_mem_decode_scr(sc, sf);
    465 		if (error)
    466 			goto out;
    467 
    468 		if (ISSET(sc->sc_caps, SMC_CAPS_4BIT_MODE) &&
    469 		    ISSET(sf->scr.bus_width, SCR_SD_BUS_WIDTHS_4BIT)) {
    470 			error = sdmmc_set_bus_width(sf, 4);
    471 			if (error) {
    472 				DPRINTF(("%s: can't change bus width"
    473 				    " (%d bit)\n", SDMMCDEVNAME(sc), 4));
    474 				goto out;
    475 			}
    476 			sf->width = 4;
    477 		}
    478 	} else if (sf->csd.mmcver >= MMC_CSD_MMCVER_4_0) {
    479 		if (ISSET(sc->sc_caps, SMC_CAPS_8BIT_MODE)) {
    480 			width = 8;
    481 			value = EXT_CSD_BUS_WIDTH_8;
    482 		} else if (ISSET(sc->sc_caps, SMC_CAPS_4BIT_MODE)) {
    483 			width = 4;
    484 			value = EXT_CSD_BUS_WIDTH_4;
    485 		} else {
    486 			width = 1;
    487 			value = EXT_CSD_BUS_WIDTH_1;
    488 		}
    489 
    490 		if (width != 1) {
    491 			error = sdmmc_mem_mmc_switch(sf, EXT_CSD_CMD_SET_NORMAL,
    492 			    EXT_CSD_BUS_WIDTH, value);
    493 			if (error == 0)
    494 				error = sdmmc_chip_bus_width(sc->sc_sct,
    495 				    sc->sc_sch, width);
    496 			else {
    497 				DPRINTF(("%s: can't change bus width"
    498 				    " (%d bit)\n", SDMMCDEVNAME(sc), width));
    499 				goto out;
    500 			}
    501 
    502 			/* XXXX: need bus test? (using by CMD14 & CMD19) */
    503 
    504 			sf->width = width;
    505 		}
    506 	}
    507 
    508 out:
    509 	SDMMC_UNLOCK(sc);
    510 
    511 	return error;
    512 }
    513 
    514 /*
    515  * Get or set the card's memory OCR value (SD or MMC).
    516  */
    517 int
    518 sdmmc_mem_send_op_cond(struct sdmmc_softc *sc, uint32_t ocr, uint32_t *ocrp)
    519 {
    520 	struct sdmmc_command cmd;
    521 	int error;
    522 	int retry;
    523 
    524 	/* Don't lock */
    525 
    526 	/*
    527 	 * If we change the OCR value, retry the command until the OCR
    528 	 * we receive in response has the "CARD BUSY" bit set, meaning
    529 	 * that all cards are ready for identification.
    530 	 */
    531 	for (retry = 0; retry < 100; retry++) {
    532 		memset(&cmd, 0, sizeof(cmd));
    533 		cmd.c_arg = !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE) ?
    534 		    ocr : (ocr & MMC_OCR_HCS);
    535 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R3 | SCF_RSP_SPI_R1;
    536 
    537 		if (ISSET(sc->sc_flags, SMF_SD_MODE)) {
    538 			cmd.c_opcode = SD_APP_OP_COND;
    539 			error = sdmmc_app_command(sc, NULL, &cmd);
    540 		} else {
    541 			cmd.c_opcode = MMC_SEND_OP_COND;
    542 			error = sdmmc_mmc_command(sc, &cmd);
    543 		}
    544 		if (error)
    545 			break;
    546 
    547 		if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    548 			if (!ISSET(MMC_SPI_R1(cmd.c_resp), R1_SPI_IDLE))
    549 				break;
    550 		} else {
    551 			if (ISSET(MMC_R3(cmd.c_resp), MMC_OCR_MEM_READY) ||
    552 			    ocr == 0)
    553 				break;
    554 		}
    555 
    556 		error = ETIMEDOUT;
    557 		sdmmc_delay(10000);
    558 	}
    559 	if (error == 0 &&
    560 	    ocrp != NULL &&
    561 	    !ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
    562 		*ocrp = MMC_R3(cmd.c_resp);
    563 	DPRINTF(("%s: sdmmc_mem_send_op_cond: error=%d, ocr=%#x\n",
    564 	    SDMMCDEVNAME(sc), error, MMC_R3(cmd.c_resp)));
    565 	return error;
    566 }
    567 
    568 int
    569 sdmmc_mem_send_if_cond(struct sdmmc_softc *sc, uint32_t ocr, uint32_t *ocrp)
    570 {
    571 	struct sdmmc_command cmd;
    572 	int error;
    573 
    574 	/* Don't lock */
    575 
    576 	memset(&cmd, 0, sizeof(cmd));
    577 	cmd.c_arg = ocr;
    578 	cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R7 | SCF_RSP_SPI_R7;
    579 	cmd.c_opcode = SD_SEND_IF_COND;
    580 
    581 	error = sdmmc_mmc_command(sc, &cmd);
    582 	if (error == 0 && ocrp != NULL) {
    583 		if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    584 			*ocrp = MMC_SPI_R7(cmd.c_resp);
    585 		} else {
    586 			*ocrp = MMC_R7(cmd.c_resp);
    587 		}
    588 		DPRINTF(("%s: sdmmc_mem_send_if_cond: error=%d, ocr=%#x\n",
    589 		    SDMMCDEVNAME(sc), error, *ocrp));
    590 	}
    591 	return error;
    592 }
    593 
    594 /*
    595  * Set the read block length appropriately for this card, according to
    596  * the card CSD register value.
    597  */
    598 int
    599 sdmmc_mem_set_blocklen(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    600 {
    601 	struct sdmmc_command cmd;
    602 	int error;
    603 
    604 	/* Don't lock */
    605 
    606 	memset(&cmd, 0, sizeof(cmd));
    607 	cmd.c_opcode = MMC_SET_BLOCKLEN;
    608 	cmd.c_arg = SDMMC_SECTOR_SIZE;
    609 	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R1;
    610 
    611 	error = sdmmc_mmc_command(sc, &cmd);
    612 
    613 	DPRINTF(("%s: sdmmc_mem_set_blocklen: read_bl_len=%d sector_size=%d\n",
    614 	    SDMMCDEVNAME(sc), 1 << sf->csd.read_bl_len, SDMMC_SECTOR_SIZE));
    615 
    616 	return error;
    617 }
    618 
    619 static int
    620 sdmmc_mem_send_cid(struct sdmmc_softc *sc, sdmmc_response *resp)
    621 {
    622 	struct sdmmc_command cmd;
    623 	int error;
    624 
    625 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    626 		memset(&cmd, 0, sizeof cmd);
    627 		cmd.c_opcode = MMC_ALL_SEND_CID;
    628 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R2;
    629 
    630 		error = sdmmc_mmc_command(sc, &cmd);
    631 	} else {
    632 		error = sdmmc_mem_send_cxd_data(sc, MMC_SEND_CID, &cmd.c_resp,
    633 		    sizeof(cmd.c_resp));
    634 	}
    635 
    636 #ifdef SDMMC_DEBUG
    637 	sdmmc_dump_data("CID", cmd.c_resp, sizeof(cmd.c_resp));
    638 #endif
    639 	if (error == 0 && resp != NULL)
    640 		memcpy(resp, &cmd.c_resp, sizeof(*resp));
    641 	return error;
    642 }
    643 
    644 static int
    645 sdmmc_mem_send_csd(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    646     sdmmc_response *resp)
    647 {
    648 	struct sdmmc_command cmd;
    649 	int error;
    650 
    651 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    652 		memset(&cmd, 0, sizeof cmd);
    653 		cmd.c_opcode = MMC_SEND_CSD;
    654 		cmd.c_arg = MMC_ARG_RCA(sf->rca);
    655 		cmd.c_flags = SCF_CMD_AC | SCF_RSP_R2;
    656 
    657 		error = sdmmc_mmc_command(sc, &cmd);
    658 	} else {
    659 		error = sdmmc_mem_send_cxd_data(sc, MMC_SEND_CSD, &cmd.c_resp,
    660 		    sizeof(cmd.c_resp));
    661 	}
    662 
    663 #ifdef SDMMC_DEBUG
    664 	sdmmc_dump_data("CSD", cmd.c_resp, sizeof(cmd.c_resp));
    665 #endif
    666 	if (error == 0 && resp != NULL)
    667 		memcpy(resp, &cmd.c_resp, sizeof(*resp));
    668 	return error;
    669 }
    670 
    671 static int
    672 sdmmc_mem_send_scr(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    673     uint32_t scr[2])
    674 {
    675 	struct sdmmc_command cmd;
    676 	bus_dma_segment_t ds[1];
    677 	void *ptr = NULL;
    678 	int datalen = 8;
    679 	int rseg;
    680 	int error = 0;
    681 
    682 	/* Don't lock */
    683 
    684 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
    685 		error = bus_dmamem_alloc(sc->sc_dmat, datalen, PAGE_SIZE, 0,
    686 		    ds, 1, &rseg, BUS_DMA_NOWAIT);
    687 		if (error)
    688 			goto out;
    689 		error = bus_dmamem_map(sc->sc_dmat, ds, 1, datalen, &ptr,
    690 		    BUS_DMA_NOWAIT);
    691 		if (error)
    692 			goto dmamem_free;
    693 		error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, ptr, datalen,
    694 		    NULL, BUS_DMA_NOWAIT|BUS_DMA_STREAMING|BUS_DMA_READ);
    695 		if (error)
    696 			goto dmamem_unmap;
    697 
    698 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
    699 		    BUS_DMASYNC_PREREAD);
    700 	} else {
    701 		ptr = malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO);
    702 		if (ptr == NULL)
    703 			goto out;
    704 	}
    705 
    706 	memset(&cmd, 0, sizeof(cmd));
    707 	cmd.c_data = ptr;
    708 	cmd.c_datalen = datalen;
    709 	cmd.c_blklen = datalen;
    710 	cmd.c_arg = 0;
    711 	cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
    712 	cmd.c_opcode = SD_APP_SEND_SCR;
    713 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
    714 		cmd.c_dmamap = sc->sc_dmap;
    715 
    716 	error = sdmmc_app_command(sc, sf, &cmd);
    717 	if (error == 0) {
    718 		if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
    719 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
    720 			    BUS_DMASYNC_POSTREAD);
    721 		}
    722 		memcpy(scr, ptr, datalen);
    723 	}
    724 
    725 out:
    726 	if (ptr != NULL) {
    727 		if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
    728 			bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
    729 dmamem_unmap:
    730 			bus_dmamem_unmap(sc->sc_dmat, ptr, datalen);
    731 dmamem_free:
    732 			bus_dmamem_free(sc->sc_dmat, ds, rseg);
    733 		} else {
    734 			free(ptr, M_DEVBUF);
    735 		}
    736 	}
    737 	DPRINTF(("%s: sdmem_mem_send_scr: error = %d\n", SDMMCDEVNAME(sc),
    738 	    error));
    739 
    740 #ifdef SDMMC_DEBUG
    741 	if (error == 0)
    742 		sdmmc_dump_data("SCR", scr, 8);
    743 #endif
    744 	return error;
    745 }
    746 
    747 static int
    748 sdmmc_mem_decode_scr(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    749 {
    750 	sdmmc_response resp;
    751 	int ver;
    752 
    753 	memset(resp, 0, sizeof(resp));
    754 	/*
    755 	 * Change the raw-scr received from the DMA stream to resp.
    756 	 */
    757 	resp[0] = be32toh(sf->raw_scr[1]);
    758 	resp[1] = be32toh(sf->raw_scr[0]) >> 8;
    759 
    760 	ver = SCR_STRUCTURE(resp);
    761 	sf->scr.sd_spec = SCR_SD_SPEC(resp);
    762 	sf->scr.bus_width = SCR_SD_BUS_WIDTHS(resp);
    763 
    764 	DPRINTF(("%s: sdmmc_mem_decode_scr: spec=%d, bus width=%d\n",
    765 	    SDMMCDEVNAME(sc), sf->scr.sd_spec, sf->scr.bus_width));
    766 
    767 	if (ver != 0) {
    768 		DPRINTF(("%s: unknown structure version: %d\n",
    769 		    SDMMCDEVNAME(sc), ver));
    770 		return EINVAL;
    771 	}
    772 	return 0;
    773 }
    774 
    775 static int
    776 sdmmc_mem_send_cxd_data(struct sdmmc_softc *sc, int opcode, void *data,
    777     size_t datalen)
    778 {
    779 	struct sdmmc_command cmd;
    780 	bus_dma_segment_t ds[1];
    781 	void *ptr = NULL;
    782 	int rseg;
    783 	int error = 0;
    784 
    785 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
    786 		error = bus_dmamem_alloc(sc->sc_dmat, datalen, PAGE_SIZE, 0, ds,
    787 		    1, &rseg, BUS_DMA_NOWAIT);
    788 		if (error)
    789 			goto out;
    790 		error = bus_dmamem_map(sc->sc_dmat, ds, 1, datalen, &ptr,
    791 		    BUS_DMA_NOWAIT);
    792 		if (error)
    793 			goto dmamem_free;
    794 		error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, ptr, datalen,
    795 		    NULL, BUS_DMA_NOWAIT|BUS_DMA_STREAMING|BUS_DMA_READ);
    796 		if (error)
    797 			goto dmamem_unmap;
    798 
    799 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
    800 		    BUS_DMASYNC_PREREAD);
    801 	} else {
    802 		ptr = malloc(datalen, M_DEVBUF, M_NOWAIT | M_ZERO);
    803 		if (ptr == NULL)
    804 			goto out;
    805 	}
    806 
    807 	memset(&cmd, 0, sizeof(cmd));
    808 	cmd.c_data = ptr;
    809 	cmd.c_datalen = datalen;
    810 	cmd.c_blklen = datalen;
    811 	cmd.c_opcode = opcode;
    812 	cmd.c_arg = 0;
    813 	cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_SPI_R1;
    814 	if (opcode == MMC_SEND_EXT_CSD)
    815 		SET(cmd.c_flags, SCF_RSP_R1);
    816 	else
    817 		SET(cmd.c_flags, SCF_RSP_R2);
    818 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
    819 		cmd.c_dmamap = sc->sc_dmap;
    820 
    821 	error = sdmmc_mmc_command(sc, &cmd);
    822 	if (error == 0) {
    823 		if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
    824 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
    825 			    BUS_DMASYNC_POSTREAD);
    826 		}
    827 		memcpy(data, ptr, datalen);
    828 	}
    829 
    830 out:
    831 	if (ptr != NULL) {
    832 		if (ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
    833 			bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
    834 dmamem_unmap:
    835 			bus_dmamem_unmap(sc->sc_dmat, ptr, datalen);
    836 dmamem_free:
    837 			bus_dmamem_free(sc->sc_dmat, ds, rseg);
    838 		} else {
    839 			free(ptr, M_DEVBUF);
    840 		}
    841 	}
    842 	return error;
    843 }
    844 
    845 int
    846 sdmmc_mem_send_extcsd(struct sdmmc_softc *sc)
    847 {
    848 	char buf[512];
    849 	int error;
    850 
    851 	error = sdmmc_mem_send_cxd_data(sc, MMC_SEND_EXT_CSD, buf, 512);
    852 
    853 	/*XXX*/
    854 
    855 	return error;
    856 }
    857 
    858 static int
    859 sdmmc_set_bus_width(struct sdmmc_function *sf, int width)
    860 {
    861 	struct sdmmc_softc *sc = sf->sc;
    862 	struct sdmmc_command cmd;
    863 	int error;
    864 
    865 	if (ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
    866 		return ENODEV;
    867 
    868 	memset(&cmd, 0, sizeof(cmd));
    869 	cmd.c_opcode = SD_APP_SET_BUS_WIDTH;
    870 	cmd.c_flags = SCF_RSP_R1 | SCF_CMD_AC;
    871 
    872 	switch (width) {
    873 	case 1:
    874 		cmd.c_arg = SD_ARG_BUS_WIDTH_1;
    875 		break;
    876 
    877 	case 4:
    878 		cmd.c_arg = SD_ARG_BUS_WIDTH_4;
    879 		break;
    880 
    881 	default:
    882 		return EINVAL;
    883 	}
    884 
    885 	error = sdmmc_app_command(sc, sf, &cmd);
    886 	if (error == 0)
    887 		error = sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch, width);
    888 	return error;
    889 }
    890 
    891 static int
    892 sdmmc_mem_mmc_switch(struct sdmmc_function *sf, uint8_t set, uint8_t index,
    893     uint8_t value)
    894 {
    895 	struct sdmmc_softc *sc = sf->sc;
    896 	struct sdmmc_command cmd;
    897 
    898 	memset(&cmd, 0, sizeof(cmd));
    899 	cmd.c_opcode = MMC_SWITCH;
    900 	cmd.c_arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
    901 	    (index << 16) | (value << 8) | set;
    902 	cmd.c_flags = SCF_RSP_SPI_R1B | SCF_RSP_R1B | SCF_CMD_AC;
    903 
    904 	return sdmmc_mmc_command(sc, &cmd);
    905 }
    906 
    907 /*
    908  * SPI mode function
    909  */
    910 static int
    911 sdmmc_mem_spi_read_ocr(struct sdmmc_softc *sc, uint32_t hcs, uint32_t *card_ocr)
    912 {
    913 	struct sdmmc_command cmd;
    914 	int error;
    915 
    916 	memset(&cmd, 0, sizeof(cmd));
    917 	cmd.c_opcode = MMC_READ_OCR;
    918 	cmd.c_arg = hcs ? MMC_OCR_HCS : 0;
    919 	cmd.c_flags = SCF_RSP_SPI_R3;
    920 
    921 	error = sdmmc_mmc_command(sc, &cmd);
    922 	if (error == 0 && card_ocr != NULL)
    923 		*card_ocr = cmd.c_resp[1];
    924 	DPRINTF(("%s: sdmmc_mem_spi_read_ocr: error=%d, ocr=%#x\n",
    925 	    SDMMCDEVNAME(sc), error, cmd.c_resp[1]));
    926 	return error;
    927 }
    928 
    929 /*
    930  * read/write function
    931  */
    932 /* read */
    933 static int
    934 sdmmc_mem_single_read_block(struct sdmmc_function *sf, uint32_t blkno,
    935     u_char *data, size_t datalen)
    936 {
    937 	int error = 0;
    938 	int i;
    939 
    940 	KASSERT((datalen % SDMMC_SECTOR_SIZE) == 0);
    941 
    942 	for (i = 0; i < datalen / SDMMC_SECTOR_SIZE; i++) {
    943 		error = sdmmc_mem_read_block_subr(sf, blkno + i,
    944 		    data + i * SDMMC_SECTOR_SIZE, SDMMC_SECTOR_SIZE);
    945 		if (error)
    946 			break;
    947 	}
    948 	return error;
    949 }
    950 
    951 static int
    952 sdmmc_mem_read_block_subr(struct sdmmc_function *sf, uint32_t blkno,
    953     u_char *data, size_t datalen)
    954 {
    955 	struct sdmmc_softc *sc = sf->sc;
    956 	struct sdmmc_command cmd;
    957 	int error;
    958 
    959 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    960 		error = sdmmc_select_card(sc, sf);
    961 		if (error)
    962 			goto out;
    963 	}
    964 
    965 	memset(&cmd, 0, sizeof(cmd));
    966 	cmd.c_data = data;
    967 	cmd.c_datalen = datalen;
    968 	cmd.c_blklen = SDMMC_SECTOR_SIZE;
    969 	cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
    970 	    MMC_READ_BLOCK_MULTIPLE : MMC_READ_BLOCK_SINGLE;
    971 	cmd.c_arg = blkno;
    972 	if (!ISSET(sf->flags, SFF_SDHC))
    973 		cmd.c_arg <<= SDMMC_SECTOR_SIZE_SB;
    974 	cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1 | SCF_RSP_SPI_R1;
    975 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
    976 		cmd.c_dmamap = sc->sc_dmap;
    977 
    978 	error = sdmmc_mmc_command(sc, &cmd);
    979 	if (error)
    980 		goto out;
    981 
    982 	if (!ISSET(sc->sc_caps, SMC_CAPS_AUTO_STOP)) {
    983 		if (cmd.c_opcode == MMC_READ_BLOCK_MULTIPLE) {
    984 			memset(&cmd, 0, sizeof cmd);
    985 			cmd.c_opcode = MMC_STOP_TRANSMISSION;
    986 			cmd.c_arg = MMC_ARG_RCA(sf->rca);
    987 			cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B;
    988 			error = sdmmc_mmc_command(sc, &cmd);
    989 			if (error)
    990 				goto out;
    991 		}
    992 	}
    993 
    994 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
    995 		do {
    996 			memset(&cmd, 0, sizeof(cmd));
    997 			cmd.c_opcode = MMC_SEND_STATUS;
    998 			if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
    999 				cmd.c_arg = MMC_ARG_RCA(sf->rca);
   1000 			cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R2;
   1001 			error = sdmmc_mmc_command(sc, &cmd);
   1002 			if (error)
   1003 				break;
   1004 			/* XXX time out */
   1005 		} while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
   1006 	}
   1007 
   1008 out:
   1009 	return error;
   1010 }
   1011 
   1012 int
   1013 sdmmc_mem_read_block(struct sdmmc_function *sf, uint32_t blkno, u_char *data,
   1014     size_t datalen)
   1015 {
   1016 	struct sdmmc_softc *sc = sf->sc;
   1017 	int error;
   1018 
   1019 	SDMMC_LOCK(sc);
   1020 
   1021 	if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) {
   1022 		error = sdmmc_mem_single_read_block(sf, blkno, data, datalen);
   1023 		goto out;
   1024 	}
   1025 
   1026 	if (!ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
   1027 		error = sdmmc_mem_read_block_subr(sf, blkno, data, datalen);
   1028 		goto out;
   1029 	}
   1030 
   1031 	/* DMA transfer */
   1032 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, data, datalen, NULL,
   1033 	    BUS_DMA_NOWAIT|BUS_DMA_READ);
   1034 	if (error)
   1035 		goto out;
   1036 
   1037 #ifdef SDMMC_DEBUG
   1038 	for (int i = 0; i < sc->sc_dmap->dm_nsegs; i++) {
   1039 		printf("seg#%d: addr=%#lx, size=%#lx\n", i,
   1040 		    (u_long)sc->sc_dmap->dm_segs[i].ds_addr,
   1041 		    (u_long)sc->sc_dmap->dm_segs[i].ds_len);
   1042 	}
   1043 #endif
   1044 
   1045 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
   1046 	    BUS_DMASYNC_PREREAD);
   1047 
   1048 	error = sdmmc_mem_read_block_subr(sf, blkno, data, datalen);
   1049 	if (error)
   1050 		goto unload;
   1051 
   1052 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
   1053 	    BUS_DMASYNC_POSTREAD);
   1054 unload:
   1055 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
   1056 
   1057 out:
   1058 	SDMMC_UNLOCK(sc);
   1059 
   1060 	return error;
   1061 }
   1062 
   1063 /* write */
   1064 static int
   1065 sdmmc_mem_single_write_block(struct sdmmc_function *sf, uint32_t blkno,
   1066     u_char *data, size_t datalen)
   1067 {
   1068 	int error = 0;
   1069 	int i;
   1070 
   1071 	KASSERT((datalen % SDMMC_SECTOR_SIZE) == 0);
   1072 
   1073 	for (i = 0; i < datalen / SDMMC_SECTOR_SIZE; i++) {
   1074 		error = sdmmc_mem_write_block_subr(sf, blkno + i,
   1075 		    data + i * SDMMC_SECTOR_SIZE, SDMMC_SECTOR_SIZE);
   1076 		if (error)
   1077 			break;
   1078 	}
   1079 	return error;
   1080 }
   1081 
   1082 static int
   1083 sdmmc_mem_write_block_subr(struct sdmmc_function *sf, uint32_t blkno,
   1084     u_char *data, size_t datalen)
   1085 {
   1086 	struct sdmmc_softc *sc = sf->sc;
   1087 	struct sdmmc_command cmd;
   1088 	int error;
   1089 
   1090 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
   1091 		error = sdmmc_select_card(sc, sf);
   1092 		if (error)
   1093 			goto out;
   1094 	}
   1095 
   1096 	memset(&cmd, 0, sizeof(cmd));
   1097 	cmd.c_data = data;
   1098 	cmd.c_datalen = datalen;
   1099 	cmd.c_blklen = SDMMC_SECTOR_SIZE;
   1100 	cmd.c_opcode = (cmd.c_datalen / cmd.c_blklen) > 1 ?
   1101 	    MMC_WRITE_BLOCK_MULTIPLE : MMC_WRITE_BLOCK_SINGLE;
   1102 	cmd.c_arg = blkno;
   1103 	if (!ISSET(sf->flags, SFF_SDHC))
   1104 		cmd.c_arg <<= SDMMC_SECTOR_SIZE_SB;
   1105 	cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R1;
   1106 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA))
   1107 		cmd.c_dmamap = sc->sc_dmap;
   1108 
   1109 	error = sdmmc_mmc_command(sc, &cmd);
   1110 	if (error)
   1111 		goto out;
   1112 
   1113 	if (!ISSET(sc->sc_caps, SMC_CAPS_AUTO_STOP)) {
   1114 		if (cmd.c_opcode == MMC_WRITE_BLOCK_MULTIPLE) {
   1115 			memset(&cmd, 0, sizeof(cmd));
   1116 			cmd.c_opcode = MMC_STOP_TRANSMISSION;
   1117 			cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1B | SCF_RSP_SPI_R1B;
   1118 			error = sdmmc_mmc_command(sc, &cmd);
   1119 			if (error)
   1120 				goto out;
   1121 		}
   1122 	}
   1123 
   1124 	if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE)) {
   1125 		do {
   1126 			memset(&cmd, 0, sizeof(cmd));
   1127 			cmd.c_opcode = MMC_SEND_STATUS;
   1128 			if (!ISSET(sc->sc_caps, SMC_CAPS_SPI_MODE))
   1129 				cmd.c_arg = MMC_ARG_RCA(sf->rca);
   1130 			cmd.c_flags = SCF_CMD_AC | SCF_RSP_R1 | SCF_RSP_SPI_R2;
   1131 			error = sdmmc_mmc_command(sc, &cmd);
   1132 			if (error)
   1133 				break;
   1134 			/* XXX time out */
   1135 		} while (!ISSET(MMC_R1(cmd.c_resp), MMC_R1_READY_FOR_DATA));
   1136 	}
   1137 
   1138 out:
   1139 	return error;
   1140 }
   1141 
   1142 int
   1143 sdmmc_mem_write_block(struct sdmmc_function *sf, uint32_t blkno, u_char *data,
   1144     size_t datalen)
   1145 {
   1146 	struct sdmmc_softc *sc = sf->sc;
   1147 	int error;
   1148 
   1149 	SDMMC_LOCK(sc);
   1150 
   1151 	if (sdmmc_chip_write_protect(sc->sc_sct, sc->sc_sch)) {
   1152 		aprint_normal_dev(sc->sc_dev, "write-protected\n");
   1153 		error = EIO;
   1154 		goto out;
   1155 	}
   1156 
   1157 	if (ISSET(sc->sc_caps, SMC_CAPS_SINGLE_ONLY)) {
   1158 		error = sdmmc_mem_single_write_block(sf, blkno, data, datalen);
   1159 		goto out;
   1160 	}
   1161 
   1162 	if (!ISSET(sc->sc_caps, SMC_CAPS_DMA)) {
   1163 		error = sdmmc_mem_write_block_subr(sf, blkno, data, datalen);
   1164 		goto out;
   1165 	}
   1166 
   1167 	/* DMA transfer */
   1168 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap, data, datalen, NULL,
   1169 	    BUS_DMA_NOWAIT|BUS_DMA_WRITE);
   1170 	if (error)
   1171 		goto out;
   1172 
   1173 #ifdef SDMMC_DEBUG
   1174 	for (int i = 0; i < sc->sc_dmap->dm_nsegs; i++) {
   1175 		printf("seg#%d: addr=%#lx, size=%#lx\n", i,
   1176 		    (u_long)sc->sc_dmap->dm_segs[i].ds_addr,
   1177 		    (u_long)sc->sc_dmap->dm_segs[i].ds_len);
   1178 	}
   1179 #endif
   1180 
   1181 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
   1182 	    BUS_DMASYNC_PREWRITE);
   1183 
   1184 	error = sdmmc_mem_write_block_subr(sf, blkno, data, datalen);
   1185 	if (error)
   1186 		goto unload;
   1187 
   1188 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap, 0, datalen,
   1189 	    BUS_DMASYNC_POSTWRITE);
   1190 unload:
   1191 	bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
   1192 
   1193 out:
   1194 	SDMMC_UNLOCK(sc);
   1195 
   1196 	return error;
   1197 }
   1198