Home | History | Annotate | Line # | Download | only in sdmmc
      1 /*	$NetBSD: sdmmc_io.c,v 1.26 2026/01/17 13:56:08 jmcneill Exp $	*/
      2 /*	$OpenBSD: sdmmc_io.c,v 1.10 2007/09/17 01:33:33 krw 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 /* Routines for SD I/O cards. */
     21 
     22 #include <sys/cdefs.h>
     23 __KERNEL_RCSID(0, "$NetBSD: sdmmc_io.c,v 1.26 2026/01/17 13:56:08 jmcneill Exp $");
     24 
     25 #ifdef _KERNEL_OPT
     26 #include "opt_sdmmc.h"
     27 #endif
     28 
     29 #include <sys/param.h>
     30 #include <sys/kernel.h>
     31 #include <sys/malloc.h>
     32 #include <sys/proc.h>
     33 #include <sys/systm.h>
     34 
     35 #include <dev/sdmmc/sdmmc_ioreg.h>
     36 #include <dev/sdmmc/sdmmcchip.h>
     37 #include <dev/sdmmc/sdmmcreg.h>
     38 #include <dev/sdmmc/sdmmcvar.h>
     39 
     40 #ifdef SDMMC_DEBUG
     41 #define DPRINTF(s)	do { printf s; } while (0)
     42 #else
     43 #define DPRINTF(s)	do {} while (0)
     44 #endif
     45 
     46 struct sdmmc_intr_handler {
     47 	struct sdmmc_softc *ih_softc;
     48 	char *ih_name;
     49 	int (*ih_fun)(void *);
     50 	void *ih_arg;
     51 	TAILQ_ENTRY(sdmmc_intr_handler) entry;
     52 };
     53 
     54 static int	sdmmc_io_rw_direct(struct sdmmc_softc *,
     55 		    struct sdmmc_function *, int, u_char *, int, bool);
     56 static int	sdmmc_io_rw_extended(struct sdmmc_softc *,
     57 		    struct sdmmc_function *, int, u_char *, int, int);
     58 #if 0
     59 static int	sdmmc_io_xchg(struct sdmmc_softc *, struct sdmmc_function *,
     60 		    int, u_char *);
     61 #endif
     62 static void	sdmmc_io_reset(struct sdmmc_softc *);
     63 static int	sdmmc_io_send_op_cond(struct sdmmc_softc *, uint32_t,
     64 		    uint32_t *);
     65 
     66 /*
     67  * Initialize SD I/O card functions (before memory cards).  The host
     68  * system and controller must support card interrupts in order to use
     69  * I/O functions.
     70  */
     71 int
     72 sdmmc_io_enable(struct sdmmc_softc *sc)
     73 {
     74 	uint32_t host_ocr;
     75 	uint32_t card_ocr;
     76 	int error;
     77 
     78 	SDMMC_LOCK(sc);
     79 
     80 	/* Set host mode to SD "combo" card. */
     81 	SET(sc->sc_flags, SMF_SD_MODE|SMF_IO_MODE|SMF_MEM_MODE);
     82 
     83 	/* Reset I/O functions. */
     84 	sdmmc_io_reset(sc);
     85 
     86 	/*
     87 	 * Read the I/O OCR value, determine the number of I/O
     88 	 * functions and whether memory is also present (a "combo
     89 	 * card") by issuing CMD5.  SD memory-only and MMC cards
     90 	 * do not respond to CMD5.
     91 	 */
     92 	error = sdmmc_io_send_op_cond(sc, 0, &card_ocr);
     93 	if (error) {
     94 		/* No SDIO card; switch to SD memory-only mode. */
     95 		CLR(sc->sc_flags, SMF_IO_MODE);
     96 		error = 0;
     97 		goto out;
     98 	}
     99 
    100 	/* Parse the additional bits in the I/O OCR value. */
    101 	if (!ISSET(card_ocr, SD_IO_OCR_MEM_PRESENT)) {
    102 		/* SDIO card without memory (not a "combo card"). */
    103 		DPRINTF(("%s: no memory present\n", SDMMCDEVNAME(sc)));
    104 		CLR(sc->sc_flags, SMF_MEM_MODE);
    105 	}
    106 	sc->sc_function_count = SD_IO_OCR_NUM_FUNCTIONS(card_ocr);
    107 	if (sc->sc_function_count == 0) {
    108 		/* Useless SDIO card without any I/O functions. */
    109 		DPRINTF(("%s: no I/O functions\n", SDMMCDEVNAME(sc)));
    110 		CLR(sc->sc_flags, SMF_IO_MODE);
    111 		error = 0;
    112 		goto out;
    113 	}
    114 	card_ocr &= SD_IO_OCR_MASK;
    115 
    116 	/* Set the lowest voltage supported by the card and host. */
    117 	host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
    118 	error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
    119 	if (error) {
    120 		aprint_error_dev(sc->sc_dev,
    121 		    "couldn't supply voltage requested by card\n");
    122 		goto out;
    123 	}
    124 
    125 	/* Send the new OCR value until all cards are ready. */
    126 	error = sdmmc_io_send_op_cond(sc, host_ocr, NULL);
    127 	if (error) {
    128 		aprint_error_dev(sc->sc_dev, "couldn't send I/O OCR\n");
    129 		goto out;
    130 	}
    131 
    132 out:
    133 	SDMMC_UNLOCK(sc);
    134 
    135 	return error;
    136 }
    137 
    138 /*
    139  * Allocate sdmmc_function structures for SD card I/O function
    140  * (including function 0).
    141  */
    142 void
    143 sdmmc_io_scan(struct sdmmc_softc *sc)
    144 {
    145 	struct sdmmc_function *sf0, *sf;
    146 	int error;
    147 	int i;
    148 
    149 	SDMMC_LOCK(sc);
    150 
    151 	sf0 = sdmmc_function_alloc(sc);
    152 	sf0->number = 0;
    153 	error = sdmmc_set_relative_addr(sc, sf0);
    154 	if (error) {
    155 		aprint_error_dev(sc->sc_dev, "couldn't set I/O RCA\n");
    156 		SET(sf0->flags, SFF_ERROR);
    157 		goto out;
    158 	}
    159 	sc->sc_fn0 = sf0;
    160 	SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf0, sf_list);
    161 
    162 	/* Go to Data Transfer Mode, if possible. */
    163 	sdmmc_chip_bus_rod(sc->sc_sct, sc->sc_sch, 0);
    164 
    165 	/* Verify that the RCA has been set by selecting the card. */
    166 	error = sdmmc_select_card(sc, sf0);
    167 	if (error) {
    168 		aprint_error_dev(sc->sc_dev, "couldn't select I/O RCA %d\n",
    169 		    sf0->rca);
    170 		SET(sf0->flags, SFF_ERROR);
    171 		goto out;
    172 	}
    173 
    174 	for (i = 1; i <= sc->sc_function_count; i++) {
    175 		sf = sdmmc_function_alloc(sc);
    176 		sf->number = i;
    177 		sf->rca = sf0->rca;
    178 		SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
    179 	}
    180 
    181 out:
    182 	SDMMC_UNLOCK(sc);
    183 }
    184 
    185 /*
    186  * Initialize SDIO card functions.
    187  */
    188 int
    189 sdmmc_io_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    190 {
    191 	struct sdmmc_function *sf0 = sc->sc_fn0;
    192 	int error = 0;
    193 	uint8_t reg;
    194 
    195 	SDMMC_LOCK(sc);
    196 
    197 	sf->blklen = sdmmc_chip_host_maxblklen(sc->sc_sct, sc->sc_sch);
    198 
    199 	if (sf->number == 0) {
    200 		reg = sdmmc_io_read_1(sf, SD_IO_CCCR_CAPABILITY);
    201 		if (!(reg & CCCR_CAPS_LSC) || (reg & CCCR_CAPS_4BLS)) {
    202 			sdmmc_io_write_1(sf, SD_IO_CCCR_BUS_WIDTH,
    203 			    CCCR_BUS_WIDTH_4);
    204 			sf->width = 4;
    205 			error = sdmmc_chip_bus_width(sc->sc_sct, sc->sc_sch,
    206 			    sf->width);
    207 			if (error)
    208 				aprint_error_dev(sc->sc_dev,
    209 				    "can't change bus width\n");
    210 		}
    211 
    212 		error = sdmmc_read_cis(sf, &sf->cis);
    213 		if (error) {
    214 			aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
    215 			SET(sf->flags, SFF_ERROR);
    216 			goto out;
    217 		}
    218 
    219 		sdmmc_check_cis_quirks(sf);
    220 
    221 #ifdef SDMMC_DEBUG
    222 		if (sdmmcdebug)
    223 			sdmmc_print_cis(sf);
    224 #endif
    225 
    226 		reg = sdmmc_io_read_1(sf, SD_IO_CCCR_HIGH_SPEED);
    227 		if (reg & CCCR_HIGH_SPEED_SHS) {
    228 			reg |= CCCR_HIGH_SPEED_EHS;
    229 			sdmmc_io_write_1(sf, SD_IO_CCCR_HIGH_SPEED, reg);
    230 			sf->csd.tran_speed = 50000;	/* 50MHz */
    231 
    232 			/* Wait 400KHz x 8 clock */
    233 			sdmmc_delay(20);
    234 		}
    235 		if (sc->sc_busclk > sf->csd.tran_speed)
    236 			sc->sc_busclk = sf->csd.tran_speed;
    237 		error =
    238 		    sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, sc->sc_busclk,
    239 			false);
    240 		if (error)
    241 			aprint_error_dev(sc->sc_dev,
    242 			    "can't change bus clock\n");
    243 
    244 		aprint_normal_dev(sc->sc_dev, "%u-bit width,", sf->width);
    245 		if ((sc->sc_busclk / 1000) != 0)
    246 			aprint_normal(" %u.%03u MHz\n",
    247 			    sc->sc_busclk / 1000, sc->sc_busclk % 1000);
    248 		else
    249 			aprint_normal(" %u KHz\n", sc->sc_busclk % 1000);
    250 
    251 
    252 	} else {
    253 		reg = sdmmc_io_read_1(sf0, SD_IO_FBR(sf->number) + 0x000);
    254 		sf->interface = FBR_STD_FUNC_IF_CODE(reg);
    255 		if (sf->interface == 0x0f)
    256 			sf->interface =
    257 			    sdmmc_io_read_1(sf0, SD_IO_FBR(sf->number) + 0x001);
    258 		error = sdmmc_read_cis(sf, &sf->cis);
    259 		if (error) {
    260 			aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
    261 			SET(sf->flags, SFF_ERROR);
    262 			goto out;
    263 		}
    264 
    265 		sdmmc_check_cis_quirks(sf);
    266 
    267 #ifdef SDMMC_DEBUG
    268 		if (sdmmcdebug)
    269 			sdmmc_print_cis(sf);
    270 #endif
    271 	}
    272 
    273 out:
    274 	SDMMC_UNLOCK(sc);
    275 
    276 	return error;
    277 }
    278 
    279 /*
    280  * Indicate whether the function is ready to operate.
    281  */
    282 static int
    283 sdmmc_io_function_ready(struct sdmmc_function *sf)
    284 {
    285 	struct sdmmc_softc *sc = sf->sc;
    286 	struct sdmmc_function *sf0 = sc->sc_fn0;
    287 	uint8_t reg;
    288 
    289 	if (sf->number == 0)
    290 		return 1;	/* FN0 is always ready */
    291 
    292 	SDMMC_LOCK(sc);
    293 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_IOREADY);
    294 	SDMMC_UNLOCK(sc);
    295 	return (reg & (1 << sf->number)) != 0;
    296 }
    297 
    298 int
    299 sdmmc_io_function_enable(struct sdmmc_function *sf)
    300 {
    301 	struct sdmmc_softc *sc = sf->sc;
    302 	struct sdmmc_function *sf0 = sc->sc_fn0;
    303 	uint8_t reg;
    304 	int retry;
    305 
    306 	if (sf->number == 0)
    307 		return 0;	/* FN0 is always enabled */
    308 
    309 	SDMMC_LOCK(sc);
    310 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
    311 	SET(reg, (1U << sf->number));
    312 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
    313 	SDMMC_UNLOCK(sc);
    314 
    315 	retry = 5;
    316 	while (!sdmmc_io_function_ready(sf) && retry-- > 0)
    317 		kpause("pause", false, hz, NULL);
    318 	return (retry >= 0) ? 0 : ETIMEDOUT;
    319 }
    320 
    321 /*
    322  * Disable the I/O function.  Return zero if the function was
    323  * disabled successfully.
    324  */
    325 void
    326 sdmmc_io_function_disable(struct sdmmc_function *sf)
    327 {
    328 	struct sdmmc_softc *sc = sf->sc;
    329 	struct sdmmc_function *sf0 = sc->sc_fn0;
    330 	uint8_t reg;
    331 
    332 	if (sf->number == 0)
    333 		return;		/* FN0 is always enabled */
    334 
    335 	SDMMC_LOCK(sc);
    336 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
    337 	CLR(reg, (1U << sf->number));
    338 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
    339 	SDMMC_UNLOCK(sc);
    340 }
    341 
    342 static int
    343 sdmmc_io_rw_direct(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    344     int reg, u_char *datap, int arg, bool toutok)
    345 {
    346 	struct sdmmc_command cmd;
    347 	int error;
    348 
    349 	/* Don't lock */
    350 
    351 	/* Make sure the card is selected. */
    352 	error = sdmmc_select_card(sc, sf);
    353 	if (error)
    354 		return error;
    355 
    356 	arg |= ((sf == NULL ? 0 : sf->number) & SD_ARG_CMD52_FUNC_MASK) <<
    357 	    SD_ARG_CMD52_FUNC_SHIFT;
    358 	arg |= (reg & SD_ARG_CMD52_REG_MASK) <<
    359 	    SD_ARG_CMD52_REG_SHIFT;
    360 	arg |= (*datap & SD_ARG_CMD52_DATA_MASK) <<
    361 	    SD_ARG_CMD52_DATA_SHIFT;
    362 
    363 	memset(&cmd, 0, sizeof cmd);
    364 	cmd.c_opcode = SD_IO_RW_DIRECT;
    365 	cmd.c_arg = arg;
    366 	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
    367 	if (toutok)
    368 		cmd.c_flags |= SCF_TOUT_OK;
    369 
    370 	error = sdmmc_mmc_command(sc, &cmd);
    371 	if (error == 0)
    372 		*datap = SD_R5_DATA(cmd.c_resp);
    373 
    374 	if (error && error != ETIMEDOUT) {
    375 		device_printf(sc->sc_dev,
    376 		    "direct I/O error %d, r=%d p=%p %s\n",
    377 		    error, reg, datap,
    378 		    ISSET(arg, SD_ARG_CMD52_WRITE) ? "write" : "read");
    379 	}
    380 
    381 	return error;
    382 }
    383 
    384 /*
    385  * Useful values of `arg' to pass in are either SD_ARG_CMD53_READ or
    386  * SD_ARG_CMD53_WRITE.  SD_ARG_CMD53_INCREMENT may be ORed into `arg'
    387  * to access successive register locations instead of accessing the
    388  * same register many times.  SD_ARG_CMD53_BLOCK_MODE may be ORed
    389  * into `arg' to indicate that the length is a number of blocks.
    390  */
    391 static int
    392 sdmmc_io_rw_extended(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    393     int reg, u_char *datap, int len, int arg)
    394 {
    395 	struct sdmmc_command cmd;
    396 	int error, datalen;
    397 
    398 	/* Don't lock */
    399 
    400 	datalen = len;
    401         if (ISSET(arg, SD_ARG_CMD53_BLOCK_MODE))
    402 		datalen *= sf->blklen;
    403 
    404 #if 0
    405 	/* Make sure the card is selected. */
    406 	error = sdmmc_select_card(sc, sf);
    407 	if (error)
    408 		return error;
    409 #endif
    410 
    411 	arg |= (((sf == NULL) ? 0 : sf->number) & SD_ARG_CMD53_FUNC_MASK) <<
    412 	    SD_ARG_CMD53_FUNC_SHIFT;
    413 	arg |= (reg & SD_ARG_CMD53_REG_MASK) <<
    414 	    SD_ARG_CMD53_REG_SHIFT;
    415 	arg |= (len & SD_ARG_CMD53_LENGTH_MASK) <<
    416 	    SD_ARG_CMD53_LENGTH_SHIFT;
    417 
    418 	memset(&cmd, 0, sizeof cmd);
    419 	cmd.c_opcode = SD_IO_RW_EXTENDED;
    420 	cmd.c_arg = arg;
    421 	cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R5 | SCF_NO_STOP;
    422 	cmd.c_data = datap;
    423 	cmd.c_datalen = datalen;
    424 	cmd.c_blklen = MIN(datalen, sf->blklen);
    425 
    426 	if (!ISSET(arg, SD_ARG_CMD53_WRITE))
    427 		cmd.c_flags |= SCF_CMD_READ;
    428 
    429 	if (ISSET(sc->sc_caps, SMC_CAPS_DMA) && datalen > 8) {
    430 		int lflags, preops, postops;
    431 
    432 		if (cmd.c_flags & SCF_CMD_READ) {
    433 			lflags = BUS_DMA_READ;
    434 			preops = BUS_DMASYNC_PREREAD;
    435 			postops = BUS_DMASYNC_POSTREAD;
    436 		} else {
    437 			lflags = BUS_DMA_WRITE;
    438 			preops = BUS_DMASYNC_PREWRITE;
    439 			postops = BUS_DMASYNC_POSTWRITE;
    440 		}
    441 
    442 		error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmap,
    443 		    datap, datalen, NULL, lflags);
    444 		if (error != 0) {
    445 			DPRINTF(("%s: dmamap load error = %d\n", __func__,
    446 			    error));
    447 			goto do_pio;
    448 		}
    449 		if (!sdmmc_alignment_ok(sc, sc->sc_dmap)) {
    450 			bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
    451 			goto do_pio;
    452 		}
    453 
    454 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap,
    455 		    0, datalen, preops);
    456 		cmd.c_dmamap = sc->sc_dmap;
    457 		error = sdmmc_mmc_command(sc, &cmd);
    458 		if (error == 0) {
    459 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmap,
    460 			    0, datalen, postops);
    461 		}
    462 
    463 		bus_dmamap_unload(sc->sc_dmat, sc->sc_dmap);
    464 	} else {
    465 do_pio:
    466 		error = sdmmc_mmc_command(sc, &cmd);
    467 	}
    468 
    469 	if (error) {
    470 		device_printf(sc->sc_dev,
    471 		    "extended I/O error %d, r=%d p=%p l=%d %s\n",
    472 		    error, reg, datap, datalen,
    473 		    ISSET(arg, SD_ARG_CMD53_WRITE) ? "write" : "read");
    474 	}
    475 
    476 	return error;
    477 }
    478 
    479 uint8_t
    480 sdmmc_io_read_1(struct sdmmc_function *sf, int reg)
    481 {
    482 	uint8_t data = 0;
    483 
    484 	/* Don't lock */
    485 
    486 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    487 	    SD_ARG_CMD52_READ, false);
    488 	return data;
    489 }
    490 
    491 void
    492 sdmmc_io_write_1(struct sdmmc_function *sf, int reg, uint8_t data)
    493 {
    494 
    495 	/* Don't lock */
    496 
    497 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    498 	    SD_ARG_CMD52_WRITE, false);
    499 }
    500 
    501 uint16_t
    502 sdmmc_io_read_2(struct sdmmc_function *sf, int reg)
    503 {
    504 	uint16_t data = 0;
    505 
    506 	/* Don't lock */
    507 
    508 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    509 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    510 	return data;
    511 }
    512 
    513 void
    514 sdmmc_io_write_2(struct sdmmc_function *sf, int reg, uint16_t data)
    515 {
    516 
    517 	/* Don't lock */
    518 
    519 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    520 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    521 }
    522 
    523 uint32_t
    524 sdmmc_io_read_4(struct sdmmc_function *sf, int reg)
    525 {
    526 	uint32_t data = 0;
    527 
    528 	/* Don't lock */
    529 
    530 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    531 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    532 	return data;
    533 }
    534 
    535 void
    536 sdmmc_io_write_4(struct sdmmc_function *sf, int reg, uint32_t data)
    537 {
    538 
    539 	/* Don't lock */
    540 
    541 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    542 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    543 }
    544 
    545 
    546 int
    547 sdmmc_io_read_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    548     int datalen)
    549 {
    550 	int blocks, error = 0;
    551 
    552 	/* Don't lock */
    553 
    554 	while (datalen >= sf->blklen) {
    555 		blocks = imin(datalen / sf->blklen,
    556 		              SD_ARG_CMD53_LENGTH_MAX);
    557 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    558 		    blocks, SD_ARG_CMD53_READ | SD_ARG_CMD53_BLOCK_MODE);
    559 		if (error)
    560 			goto error;
    561 		data += blocks * sf->blklen;
    562 		datalen -= blocks * sf->blklen;
    563 	}
    564 
    565 	if (datalen)
    566 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    567 		    SD_ARG_CMD53_READ);
    568 error:
    569 	return error;
    570 }
    571 
    572 int
    573 sdmmc_io_write_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    574     int datalen)
    575 {
    576 	int blocks, error = 0;
    577 
    578 	/* Don't lock */
    579 
    580 	while (datalen >= sf->blklen) {
    581 		blocks = imin(datalen / sf->blklen,
    582 		             SD_ARG_CMD53_LENGTH_MAX);
    583 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    584 		    blocks, SD_ARG_CMD53_WRITE | SD_ARG_CMD53_BLOCK_MODE);
    585 		if (error)
    586 			goto error;
    587 		data += blocks * sf->blklen;
    588 		datalen -= blocks * sf->blklen;
    589 	}
    590 
    591 	if (datalen)
    592 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    593 		    SD_ARG_CMD53_WRITE);
    594 error:
    595 	return error;
    596 }
    597 
    598 
    599 int
    600 sdmmc_io_read_region_1(struct sdmmc_function *sf, int reg, u_char *data,
    601     int datalen)
    602 {
    603 	int blocks, error = 0;
    604 
    605 	/* Don't lock */
    606 
    607 	while (datalen >= sf->blklen) {
    608 		blocks = imin(datalen / sf->blklen,
    609 		              SD_ARG_CMD53_LENGTH_MAX);
    610 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    611 		    blocks, SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT |
    612 		    SD_ARG_CMD53_BLOCK_MODE);
    613 		if (error)
    614 			goto error;
    615 		reg += blocks * sf->blklen;
    616 		data += blocks * sf->blklen;
    617 		datalen -= blocks * sf->blklen;
    618 	}
    619 
    620 	if (datalen)
    621 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    622 		    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    623 error:
    624 	return error;
    625 }
    626 
    627 int
    628 sdmmc_io_write_region_1(struct sdmmc_function *sf, int reg, u_char *data,
    629     int datalen)
    630 {
    631 	int blocks, error = 0;
    632 
    633 	/* Don't lock */
    634 
    635 	while (datalen >= sf->blklen) {
    636 		blocks = imin(datalen / sf->blklen,
    637 		              SD_ARG_CMD53_LENGTH_MAX);
    638 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    639 		    blocks, SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT |
    640 		    SD_ARG_CMD53_BLOCK_MODE);
    641 		if (error)
    642 			goto error;
    643 		reg += blocks * sf->blklen;
    644 		data += blocks * sf->blklen;
    645 		datalen -= blocks * sf->blklen;
    646 	}
    647 
    648 	if (datalen)
    649 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    650 		    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    651 error:
    652 	return error;
    653 }
    654 
    655 #if 0
    656 static int
    657 sdmmc_io_xchg(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    658     int reg, u_char *datap)
    659 {
    660 
    661 	/* Don't lock */
    662 
    663 	return sdmmc_io_rw_direct(sc, sf, reg, datap,
    664 	    SD_ARG_CMD52_WRITE|SD_ARG_CMD52_EXCHANGE, false);
    665 }
    666 #endif
    667 
    668 /*
    669  * Abort I/O function of the card
    670  */
    671 int
    672 sdmmc_io_function_abort(struct sdmmc_function *sf)
    673 {
    674 	u_char data = CCCR_CTL_AS(sf->number);
    675 
    676 	return sdmmc_io_rw_direct(sf->sc, NULL, SD_IO_CCCR_CTL, &data,
    677 	    SD_ARG_CMD52_WRITE, true);
    678 }
    679 
    680 /*
    681  * Reset the I/O functions of the card.
    682  */
    683 static void
    684 sdmmc_io_reset(struct sdmmc_softc *sc)
    685 {
    686 	u_char data = CCCR_CTL_RES;
    687 
    688 	if (sdmmc_io_rw_direct(sc, NULL, SD_IO_CCCR_CTL, &data,
    689 	    SD_ARG_CMD52_WRITE, true) == 0)
    690 		sdmmc_pause(100000, NULL); /* XXX SDMMC_LOCK */
    691 }
    692 
    693 /*
    694  * Get or set the card's I/O OCR value (SDIO).
    695  */
    696 static int
    697 sdmmc_io_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, u_int32_t *ocrp)
    698 {
    699 	struct sdmmc_command cmd;
    700 	int error;
    701 	int retry;
    702 
    703 	DPRINTF(("sdmmc_io_send_op_cond: ocr = %#x\n", ocr));
    704 
    705 	/* Don't lock */
    706 
    707 	/*
    708 	 * If we change the OCR value, retry the command until the OCR
    709 	 * we receive in response has the "CARD BUSY" bit set, meaning
    710 	 * that all cards are ready for identification.
    711 	 */
    712 	for (retry = 0; retry < 100; retry++) {
    713 		memset(&cmd, 0, sizeof cmd);
    714 		cmd.c_opcode = SD_IO_SEND_OP_COND;
    715 		cmd.c_arg = ocr;
    716 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R4 | SCF_TOUT_OK;
    717 
    718 		error = sdmmc_mmc_command(sc, &cmd);
    719 		if (error)
    720 			break;
    721 		if (ISSET(MMC_R4(cmd.c_resp), SD_IO_OCR_MEM_READY) || ocr == 0)
    722 			break;
    723 
    724 		error = ETIMEDOUT;
    725 		sdmmc_pause(10000, NULL);
    726 	}
    727 	if (error == 0 && ocrp != NULL)
    728 		*ocrp = MMC_R4(cmd.c_resp);
    729 
    730 	DPRINTF(("sdmmc_io_send_op_cond: error = %d\n", error));
    731 
    732 	return error;
    733 }
    734 
    735 /*
    736  * Card interrupt handling
    737  */
    738 
    739 void
    740 sdmmc_intr_enable(struct sdmmc_function *sf)
    741 {
    742 	struct sdmmc_softc *sc = sf->sc;
    743 	struct sdmmc_function *sf0 = sc->sc_fn0;
    744 	uint8_t reg;
    745 
    746 	SDMMC_LOCK(sc);
    747 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    748 	reg |= 1 << sf->number;
    749 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    750 	SDMMC_UNLOCK(sc);
    751 }
    752 
    753 void
    754 sdmmc_intr_disable(struct sdmmc_function *sf)
    755 {
    756 	struct sdmmc_softc *sc = sf->sc;
    757 	struct sdmmc_function *sf0 = sc->sc_fn0;
    758 	uint8_t reg;
    759 
    760 	SDMMC_LOCK(sc);
    761 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    762 	reg &= ~(1 << sf->number);
    763 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    764 	SDMMC_UNLOCK(sc);
    765 }
    766 
    767 /*
    768  * Establish a handler for the SDIO card interrupt.  Because the
    769  * interrupt may be shared with different SDIO functions, multiple
    770  * handlers can be established.
    771  */
    772 void *
    773 sdmmc_intr_establish(device_t dev, int (*fun)(void *), void *arg,
    774     const char *name)
    775 {
    776 	struct sdmmc_softc *sc = device_private(dev);
    777 	struct sdmmc_intr_handler *ih;
    778 
    779 	if (sc->sc_sct->card_enable_intr == NULL)
    780 		return NULL;
    781 
    782 	ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK|M_ZERO);
    783 	if (ih == NULL)
    784 		return NULL;
    785 
    786 	ih->ih_name = malloc(strlen(name) + 1, M_DEVBUF, M_WAITOK|M_ZERO);
    787 	if (ih->ih_name == NULL) {
    788 		free(ih, M_DEVBUF);
    789 		return NULL;
    790 	}
    791 	strlcpy(ih->ih_name, name, strlen(name));
    792 	ih->ih_softc = sc;
    793 	ih->ih_fun = fun;
    794 	ih->ih_arg = arg;
    795 
    796 	mutex_enter(&sc->sc_mtx);
    797 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    798 		sdmmc_intr_enable(sc->sc_fn0);
    799 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 1);
    800 	}
    801 	TAILQ_INSERT_TAIL(&sc->sc_intrq, ih, entry);
    802 	mutex_exit(&sc->sc_mtx);
    803 
    804 	return ih;
    805 }
    806 
    807 /*
    808  * Disestablish the given handler.
    809  */
    810 void
    811 sdmmc_intr_disestablish(void *cookie)
    812 {
    813 	struct sdmmc_intr_handler *ih = cookie;
    814 	struct sdmmc_softc *sc = ih->ih_softc;
    815 
    816 	if (sc->sc_sct->card_enable_intr == NULL)
    817 		return;
    818 
    819 	mutex_enter(&sc->sc_mtx);
    820 	TAILQ_REMOVE(&sc->sc_intrq, ih, entry);
    821 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    822 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 0);
    823 		sdmmc_intr_disable(sc->sc_fn0);
    824 	}
    825 	mutex_exit(&sc->sc_mtx);
    826 
    827 	free(ih->ih_name, M_DEVBUF);
    828 	free(ih, M_DEVBUF);
    829 }
    830 
    831 /*
    832  * Call established SDIO card interrupt handlers.  The host controller
    833  * must call this function from its own interrupt handler to handle an
    834  * SDIO interrupt from the card.
    835  */
    836 void
    837 sdmmc_card_intr(device_t dev)
    838 {
    839 	struct sdmmc_softc *sc = device_private(dev);
    840 
    841 	if (sc->sc_sct->card_enable_intr == NULL)
    842 		return;
    843 
    844 	sdmmc_add_task(sc, &sc->sc_intr_task);
    845 }
    846 
    847 void
    848 sdmmc_intr_task(void *arg)
    849 {
    850 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    851 	struct sdmmc_intr_handler *ih;
    852 
    853 	mutex_enter(&sc->sc_mtx);
    854 	TAILQ_FOREACH(ih, &sc->sc_intrq, entry) {
    855 		/* XXX examine return value and do evcount stuff*/
    856 		(void)(*ih->ih_fun)(ih->ih_arg);
    857 	}
    858 	mutex_exit(&sc->sc_mtx);
    859 
    860 	sdmmc_chip_card_intr_ack(sc->sc_sct, sc->sc_sch);
    861 }
    862 
    863 int
    864 sdmmc_io_set_blocklen(struct sdmmc_function *sf,
    865      int blklen)
    866 {
    867 	struct sdmmc_softc *sc = sf->sc;
    868 	struct sdmmc_function *sf0 = sc->sc_fn0;
    869 	int error = EINVAL;
    870 
    871 	SDMMC_LOCK(sc);
    872 
    873 	if (blklen <= 0 ||
    874 	    blklen > sdmmc_chip_host_maxblklen(sc->sc_sct, sc->sc_sch))
    875 		goto err;
    876 
    877 	sdmmc_io_write_1(sf0, SD_IO_FBR(sf->number) +
    878 	    SD_IO_FBR_BLOCKLEN, blklen & 0xff);
    879 	sdmmc_io_write_1(sf0, SD_IO_FBR(sf->number) +
    880 	    SD_IO_FBR_BLOCKLEN + 1, (blklen >> 8) & 0xff);
    881 
    882 	sf->blklen = blklen;
    883 	error = 0;
    884 
    885 err:
    886 	SDMMC_UNLOCK(sc);
    887 
    888 	return error;
    889 }
    890 
    891