Home | History | Annotate | Line # | Download | only in sdmmc
sdmmc_io.c revision 1.16
      1 /*	$NetBSD: sdmmc_io.c,v 1.16 2019/09/02 11:09:42 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.16 2019/09/02 11:09:42 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);
     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 			delay(1);
    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 	} else {
    244 		reg = sdmmc_io_read_1(sf0, SD_IO_FBR(sf->number) + 0x000);
    245 		sf->interface = FBR_STD_FUNC_IF_CODE(reg);
    246 		if (sf->interface == 0x0f)
    247 			sf->interface =
    248 			    sdmmc_io_read_1(sf0, SD_IO_FBR(sf->number) + 0x001);
    249 		error = sdmmc_read_cis(sf, &sf->cis);
    250 		if (error) {
    251 			aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
    252 			SET(sf->flags, SFF_ERROR);
    253 			goto out;
    254 		}
    255 
    256 		sdmmc_check_cis_quirks(sf);
    257 
    258 #ifdef SDMMC_DEBUG
    259 		if (sdmmcdebug)
    260 			sdmmc_print_cis(sf);
    261 #endif
    262 	}
    263 
    264 out:
    265 	SDMMC_UNLOCK(sc);
    266 
    267 	return error;
    268 }
    269 
    270 /*
    271  * Indicate whether the function is ready to operate.
    272  */
    273 static int
    274 sdmmc_io_function_ready(struct sdmmc_function *sf)
    275 {
    276 	struct sdmmc_softc *sc = sf->sc;
    277 	struct sdmmc_function *sf0 = sc->sc_fn0;
    278 	uint8_t reg;
    279 
    280 	if (sf->number == 0)
    281 		return 1;	/* FN0 is always ready */
    282 
    283 	SDMMC_LOCK(sc);
    284 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_IOREADY);
    285 	SDMMC_UNLOCK(sc);
    286 	return (reg & (1 << sf->number)) != 0;
    287 }
    288 
    289 int
    290 sdmmc_io_function_enable(struct sdmmc_function *sf)
    291 {
    292 	struct sdmmc_softc *sc = sf->sc;
    293 	struct sdmmc_function *sf0 = sc->sc_fn0;
    294 	uint8_t reg;
    295 	int retry;
    296 
    297 	if (sf->number == 0)
    298 		return 0;	/* FN0 is always enabled */
    299 
    300 	SDMMC_LOCK(sc);
    301 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
    302 	SET(reg, (1U << sf->number));
    303 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
    304 	SDMMC_UNLOCK(sc);
    305 
    306 	retry = 5;
    307 	while (!sdmmc_io_function_ready(sf) && retry-- > 0)
    308 		kpause("pause", false, hz, NULL);
    309 	return (retry >= 0) ? 0 : ETIMEDOUT;
    310 }
    311 
    312 /*
    313  * Disable the I/O function.  Return zero if the function was
    314  * disabled successfully.
    315  */
    316 void
    317 sdmmc_io_function_disable(struct sdmmc_function *sf)
    318 {
    319 	struct sdmmc_softc *sc = sf->sc;
    320 	struct sdmmc_function *sf0 = sc->sc_fn0;
    321 	uint8_t reg;
    322 
    323 	if (sf->number == 0)
    324 		return;		/* FN0 is always enabled */
    325 
    326 	SDMMC_LOCK(sc);
    327 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
    328 	CLR(reg, (1U << sf->number));
    329 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
    330 	SDMMC_UNLOCK(sc);
    331 }
    332 
    333 static int
    334 sdmmc_io_rw_direct(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    335     int reg, u_char *datap, int arg)
    336 {
    337 	struct sdmmc_command cmd;
    338 	int error;
    339 
    340 	/* Don't lock */
    341 
    342 	/* Make sure the card is selected. */
    343 	error = sdmmc_select_card(sc, sf);
    344 	if (error)
    345 		return error;
    346 
    347 	arg |= ((sf == NULL ? 0 : sf->number) & SD_ARG_CMD52_FUNC_MASK) <<
    348 	    SD_ARG_CMD52_FUNC_SHIFT;
    349 	arg |= (reg & SD_ARG_CMD52_REG_MASK) <<
    350 	    SD_ARG_CMD52_REG_SHIFT;
    351 	arg |= (*datap & SD_ARG_CMD52_DATA_MASK) <<
    352 	    SD_ARG_CMD52_DATA_SHIFT;
    353 
    354 	memset(&cmd, 0, sizeof cmd);
    355 	cmd.c_opcode = SD_IO_RW_DIRECT;
    356 	cmd.c_arg = arg;
    357 	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
    358 
    359 	error = sdmmc_mmc_command(sc, &cmd);
    360 	*datap = SD_R5_DATA(cmd.c_resp);
    361 
    362 	return error;
    363 }
    364 
    365 /*
    366  * Useful values of `arg' to pass in are either SD_ARG_CMD53_READ or
    367  * SD_ARG_CMD53_WRITE.  SD_ARG_CMD53_INCREMENT may be ORed into `arg'
    368  * to access successive register locations instead of accessing the
    369  * same register many times.
    370  */
    371 static int
    372 sdmmc_io_rw_extended(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    373     int reg, u_char *datap, int datalen, int arg)
    374 {
    375 	struct sdmmc_command cmd;
    376 	int error;
    377 
    378 	/* Don't lock */
    379 
    380 #if 0
    381 	/* Make sure the card is selected. */
    382 	error = sdmmc_select_card(sc, sf);
    383 	if (error)
    384 		return error;
    385 #endif
    386 
    387 	arg |= (((sf == NULL) ? 0 : sf->number) & SD_ARG_CMD53_FUNC_MASK) <<
    388 	    SD_ARG_CMD53_FUNC_SHIFT;
    389 	arg |= (reg & SD_ARG_CMD53_REG_MASK) <<
    390 	    SD_ARG_CMD53_REG_SHIFT;
    391 	arg |= (datalen & SD_ARG_CMD53_LENGTH_MASK) <<
    392 	    SD_ARG_CMD53_LENGTH_SHIFT;
    393 
    394 	memset(&cmd, 0, sizeof cmd);
    395 	cmd.c_opcode = SD_IO_RW_EXTENDED;
    396 	cmd.c_arg = arg;
    397 	cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R5;
    398 	cmd.c_data = datap;
    399 	cmd.c_datalen = datalen;
    400 	cmd.c_blklen = MIN(datalen, sf->blklen);
    401 
    402 	if (!ISSET(arg, SD_ARG_CMD53_WRITE))
    403 		cmd.c_flags |= SCF_CMD_READ;
    404 
    405 	error = sdmmc_mmc_command(sc, &cmd);
    406 
    407 	return error;
    408 }
    409 
    410 uint8_t
    411 sdmmc_io_read_1(struct sdmmc_function *sf, int reg)
    412 {
    413 	uint8_t data = 0;
    414 
    415 	/* Don't lock */
    416 
    417 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    418 	    SD_ARG_CMD52_READ);
    419 	return data;
    420 }
    421 
    422 void
    423 sdmmc_io_write_1(struct sdmmc_function *sf, int reg, uint8_t data)
    424 {
    425 
    426 	/* Don't lock */
    427 
    428 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    429 	    SD_ARG_CMD52_WRITE);
    430 }
    431 
    432 uint16_t
    433 sdmmc_io_read_2(struct sdmmc_function *sf, int reg)
    434 {
    435 	uint16_t data = 0;
    436 
    437 	/* Don't lock */
    438 
    439 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    440 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    441 	return data;
    442 }
    443 
    444 void
    445 sdmmc_io_write_2(struct sdmmc_function *sf, int reg, uint16_t data)
    446 {
    447 
    448 	/* Don't lock */
    449 
    450 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    451 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    452 }
    453 
    454 uint32_t
    455 sdmmc_io_read_4(struct sdmmc_function *sf, int reg)
    456 {
    457 	uint32_t data = 0;
    458 
    459 	/* Don't lock */
    460 
    461 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    462 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    463 	return data;
    464 }
    465 
    466 void
    467 sdmmc_io_write_4(struct sdmmc_function *sf, int reg, uint32_t data)
    468 {
    469 
    470 	/* Don't lock */
    471 
    472 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    473 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    474 }
    475 
    476 
    477 int
    478 sdmmc_io_read_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    479     int datalen)
    480 {
    481 	int blocks, bytes, error = 0;
    482 
    483 	/* Don't lock */
    484 
    485 	while (datalen >= sf->blklen) {
    486 		//blocks = imin(datalen / sf->blklen,
    487 		//              SD_ARG_CMD53_LENGTH_MAX);
    488 		blocks = 1;
    489 		bytes = blocks * sf->blklen;
    490 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    491 		    bytes, SD_ARG_CMD53_READ);
    492 		if (error)
    493 			goto error;
    494 		data += bytes;
    495 		datalen -= bytes;
    496 	}
    497 
    498 	if (datalen)
    499 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    500 		    SD_ARG_CMD53_READ);
    501 error:
    502 	return error;
    503 }
    504 
    505 int
    506 sdmmc_io_write_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    507     int datalen)
    508 {
    509 	int blocks, bytes, error = 0;
    510 
    511 	/* Don't lock */
    512 
    513 	while (datalen >= sf->blklen) {
    514 		//blocks = imin(datalen / sf->blklen,
    515 		//             SD_ARG_CMD53_LENGTH_MAX);
    516 		blocks = 1;
    517 		bytes = blocks * sf->blklen;
    518 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    519 		    bytes, SD_ARG_CMD53_WRITE);
    520 		if (error)
    521 			goto error;
    522 		data += bytes;
    523 		datalen -= bytes;
    524 	}
    525 
    526 	if (datalen)
    527 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    528 		    SD_ARG_CMD53_WRITE);
    529 error:
    530 	return error;
    531 }
    532 
    533 
    534 int
    535 sdmmc_io_read_region_1(struct sdmmc_function *sf, int reg, u_char *data,
    536     int datalen)
    537 {
    538 	int blocks, bytes, error = 0;
    539 
    540 	/* Don't lock */
    541 
    542 	while (datalen >= sf->blklen) {
    543 		//blocks = imin(datalen / sf->blklen,
    544 		//              SD_ARG_CMD53_LENGTH_MAX);
    545 		blocks = 1;
    546 		bytes = blocks * sf->blklen;
    547 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    548 		    bytes, SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    549 		if (error)
    550 			goto error;
    551 		reg += bytes;
    552 		data += bytes;
    553 		datalen -= bytes;
    554 	}
    555 
    556 	if (datalen)
    557 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    558 		    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    559 error:
    560 	return error;
    561 }
    562 
    563 int
    564 sdmmc_io_write_region_1(struct sdmmc_function *sf, int reg, u_char *data,
    565     int datalen)
    566 {
    567 	int blocks, bytes, error = 0;
    568 
    569 	/* Don't lock */
    570 
    571 	while (datalen >= sf->blklen) {
    572 		//blocks = imin(datalen / sf->blklen,
    573 		//              SD_ARG_CMD53_LENGTH_MAX);
    574 		blocks = 1;
    575 		bytes = blocks * sf->blklen;
    576 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    577 		    bytes, SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    578 		if (error)
    579 			goto error;
    580 		reg += bytes;
    581 		data += bytes;
    582 		datalen -= bytes;
    583 	}
    584 
    585 	if (datalen)
    586 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    587 		    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    588 error:
    589 	return error;
    590 }
    591 
    592 #if 0
    593 static int
    594 sdmmc_io_xchg(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    595     int reg, u_char *datap)
    596 {
    597 
    598 	/* Don't lock */
    599 
    600 	return sdmmc_io_rw_direct(sc, sf, reg, datap,
    601 	    SD_ARG_CMD52_WRITE|SD_ARG_CMD52_EXCHANGE);
    602 }
    603 #endif
    604 
    605 /*
    606  * Reset the I/O functions of the card.
    607  */
    608 static void
    609 sdmmc_io_reset(struct sdmmc_softc *sc)
    610 {
    611 	u_char data = CCCR_CTL_RES;
    612 
    613 	if (sdmmc_io_rw_direct(sc, NULL, SD_IO_CCCR_CTL, &data,
    614 	    SD_ARG_CMD52_WRITE) == 0)
    615 		sdmmc_delay(100000);
    616 }
    617 
    618 /*
    619  * Get or set the card's I/O OCR value (SDIO).
    620  */
    621 static int
    622 sdmmc_io_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, u_int32_t *ocrp)
    623 {
    624 	struct sdmmc_command cmd;
    625 	int error;
    626 	int retry;
    627 
    628 	DPRINTF(("sdmmc_io_send_op_cond: ocr = %#x\n", ocr));
    629 
    630 	/* Don't lock */
    631 
    632 	/*
    633 	 * If we change the OCR value, retry the command until the OCR
    634 	 * we receive in response has the "CARD BUSY" bit set, meaning
    635 	 * that all cards are ready for identification.
    636 	 */
    637 	for (retry = 0; retry < 100; retry++) {
    638 		memset(&cmd, 0, sizeof cmd);
    639 		cmd.c_opcode = SD_IO_SEND_OP_COND;
    640 		cmd.c_arg = ocr;
    641 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R4 | SCF_TOUT_OK;
    642 
    643 		error = sdmmc_mmc_command(sc, &cmd);
    644 		if (error)
    645 			break;
    646 		if (ISSET(MMC_R4(cmd.c_resp), SD_IO_OCR_MEM_READY) || ocr == 0)
    647 			break;
    648 
    649 		error = ETIMEDOUT;
    650 		sdmmc_delay(10000);
    651 	}
    652 	if (error == 0 && ocrp != NULL)
    653 		*ocrp = MMC_R4(cmd.c_resp);
    654 
    655 	DPRINTF(("sdmmc_io_send_op_cond: error = %d\n", error));
    656 
    657 	return error;
    658 }
    659 
    660 /*
    661  * Card interrupt handling
    662  */
    663 
    664 void
    665 sdmmc_intr_enable(struct sdmmc_function *sf)
    666 {
    667 	struct sdmmc_softc *sc = sf->sc;
    668 	struct sdmmc_function *sf0 = sc->sc_fn0;
    669 	uint8_t reg;
    670 
    671 	SDMMC_LOCK(sc);
    672 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    673 	reg |= 1 << sf->number;
    674 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    675 	SDMMC_UNLOCK(sc);
    676 }
    677 
    678 void
    679 sdmmc_intr_disable(struct sdmmc_function *sf)
    680 {
    681 	struct sdmmc_softc *sc = sf->sc;
    682 	struct sdmmc_function *sf0 = sc->sc_fn0;
    683 	uint8_t reg;
    684 
    685 	SDMMC_LOCK(sc);
    686 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    687 	reg &= ~(1 << sf->number);
    688 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    689 	SDMMC_UNLOCK(sc);
    690 }
    691 
    692 /*
    693  * Establish a handler for the SDIO card interrupt.  Because the
    694  * interrupt may be shared with different SDIO functions, multiple
    695  * handlers can be established.
    696  */
    697 void *
    698 sdmmc_intr_establish(device_t dev, int (*fun)(void *), void *arg,
    699     const char *name)
    700 {
    701 	struct sdmmc_softc *sc = device_private(dev);
    702 	struct sdmmc_intr_handler *ih;
    703 
    704 	if (sc->sc_sct->card_enable_intr == NULL)
    705 		return NULL;
    706 
    707 	ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK|M_ZERO);
    708 	if (ih == NULL)
    709 		return NULL;
    710 
    711 	ih->ih_name = malloc(strlen(name) + 1, M_DEVBUF, M_WAITOK|M_ZERO);
    712 	if (ih->ih_name == NULL) {
    713 		free(ih, M_DEVBUF);
    714 		return NULL;
    715 	}
    716 	strlcpy(ih->ih_name, name, strlen(name));
    717 	ih->ih_softc = sc;
    718 	ih->ih_fun = fun;
    719 	ih->ih_arg = arg;
    720 
    721 	mutex_enter(&sc->sc_mtx);
    722 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    723 		sdmmc_intr_enable(sc->sc_fn0);
    724 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 1);
    725 	}
    726 	TAILQ_INSERT_TAIL(&sc->sc_intrq, ih, entry);
    727 	mutex_exit(&sc->sc_mtx);
    728 
    729 	return ih;
    730 }
    731 
    732 /*
    733  * Disestablish the given handler.
    734  */
    735 void
    736 sdmmc_intr_disestablish(void *cookie)
    737 {
    738 	struct sdmmc_intr_handler *ih = cookie;
    739 	struct sdmmc_softc *sc = ih->ih_softc;
    740 
    741 	if (sc->sc_sct->card_enable_intr == NULL)
    742 		return;
    743 
    744 	mutex_enter(&sc->sc_mtx);
    745 	TAILQ_REMOVE(&sc->sc_intrq, ih, entry);
    746 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    747 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 0);
    748 		sdmmc_intr_disable(sc->sc_fn0);
    749 	}
    750 	mutex_exit(&sc->sc_mtx);
    751 
    752 	free(ih->ih_name, M_DEVBUF);
    753 	free(ih, M_DEVBUF);
    754 }
    755 
    756 /*
    757  * Call established SDIO card interrupt handlers.  The host controller
    758  * must call this function from its own interrupt handler to handle an
    759  * SDIO interrupt from the card.
    760  */
    761 void
    762 sdmmc_card_intr(device_t dev)
    763 {
    764 	struct sdmmc_softc *sc = device_private(dev);
    765 
    766 	if (sc->sc_sct->card_enable_intr == NULL)
    767 		return;
    768 
    769 	mutex_enter(&sc->sc_intr_task_mtx);
    770 	if (!sdmmc_task_pending(&sc->sc_intr_task))
    771 		sdmmc_add_task(sc, &sc->sc_intr_task);
    772 	mutex_exit(&sc->sc_intr_task_mtx);
    773 }
    774 
    775 void
    776 sdmmc_intr_task(void *arg)
    777 {
    778 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    779 	struct sdmmc_intr_handler *ih;
    780 
    781 	mutex_enter(&sc->sc_mtx);
    782 	TAILQ_FOREACH(ih, &sc->sc_intrq, entry) {
    783 		/* XXX examine return value and do evcount stuff*/
    784 		(void)(*ih->ih_fun)(ih->ih_arg);
    785 	}
    786 	mutex_exit(&sc->sc_mtx);
    787 
    788 	sdmmc_chip_card_intr_ack(sc->sc_sct, sc->sc_sch);
    789 }
    790 
    791 int
    792 sdmmc_io_set_blocklen(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    793      int blklen)
    794 {
    795 	struct sdmmc_function *sf0 = sc->sc_fn0;
    796 	int error = EINVAL;
    797 
    798 	SDMMC_LOCK(sc);
    799 
    800 	if (blklen <= 0 ||
    801 	    blklen > sdmmc_chip_host_maxblklen(sc->sc_sct, sc->sc_sch))
    802 		goto err;
    803 
    804 	sdmmc_io_write_1(sf0, SD_IO_FBR(sf->number) +
    805 	    SD_IO_FBR_BLOCKLEN, blklen & 0xff);
    806 	sdmmc_io_write_1(sf0, SD_IO_FBR(sf->number) +
    807 	    SD_IO_FBR_BLOCKLEN + 1, (blklen >> 8) & 0xff);
    808 
    809 	sf->blklen = blklen;
    810 	error = 0;
    811 
    812 err:
    813 	SDMMC_UNLOCK(sc);
    814 
    815 	return error;
    816 }
    817