Home | History | Annotate | Line # | Download | only in sdmmc
sdmmc_io.c revision 1.18
      1 /*	$NetBSD: sdmmc_io.c,v 1.18 2019/10/28 06:20:01 mlelstv 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.18 2019/10/28 06:20:01 mlelstv 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 			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)
    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 
    368 	error = sdmmc_mmc_command(sc, &cmd);
    369 	if (error == 0)
    370 		*datap = SD_R5_DATA(cmd.c_resp);
    371 
    372 	if (error) {
    373 		device_printf(sc->sc_dev,
    374 		    "direct I/O error %d, r=%d p=%p %s\n",
    375 		    error, reg, datap,
    376 		    ISSET(arg, SD_ARG_CMD53_WRITE) ? "write" : "read");
    377 	}
    378 
    379 	return error;
    380 }
    381 
    382 /*
    383  * Useful values of `arg' to pass in are either SD_ARG_CMD53_READ or
    384  * SD_ARG_CMD53_WRITE.  SD_ARG_CMD53_INCREMENT may be ORed into `arg'
    385  * to access successive register locations instead of accessing the
    386  * same register many times.
    387  */
    388 static int
    389 sdmmc_io_rw_extended(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    390     int reg, u_char *datap, int datalen, int arg)
    391 {
    392 	struct sdmmc_command cmd;
    393 	int error;
    394 
    395 	/* Don't lock */
    396 
    397 #if 0
    398 	/* Make sure the card is selected. */
    399 	error = sdmmc_select_card(sc, sf);
    400 	if (error)
    401 		return error;
    402 #endif
    403 
    404 	arg |= (((sf == NULL) ? 0 : sf->number) & SD_ARG_CMD53_FUNC_MASK) <<
    405 	    SD_ARG_CMD53_FUNC_SHIFT;
    406 	arg |= (reg & SD_ARG_CMD53_REG_MASK) <<
    407 	    SD_ARG_CMD53_REG_SHIFT;
    408 	arg |= (datalen & SD_ARG_CMD53_LENGTH_MASK) <<
    409 	    SD_ARG_CMD53_LENGTH_SHIFT;
    410 
    411 	memset(&cmd, 0, sizeof cmd);
    412 	cmd.c_opcode = SD_IO_RW_EXTENDED;
    413 	cmd.c_arg = arg;
    414 	cmd.c_flags = SCF_CMD_ADTC | SCF_RSP_R5;
    415 	cmd.c_data = datap;
    416 	cmd.c_datalen = datalen;
    417 	cmd.c_blklen = MIN(datalen, sf->blklen);
    418 
    419 	if (!ISSET(arg, SD_ARG_CMD53_WRITE))
    420 		cmd.c_flags |= SCF_CMD_READ;
    421 
    422 	error = sdmmc_mmc_command(sc, &cmd);
    423 
    424 	if (error) {
    425 		device_printf(sc->sc_dev,
    426 		    "extended I/O error %d, r=%d p=%p l=%d %s\n",
    427 		    error, reg, datap, datalen,
    428 		    ISSET(arg, SD_ARG_CMD53_WRITE) ? "write" : "read");
    429 	}
    430 
    431 	return error;
    432 }
    433 
    434 uint8_t
    435 sdmmc_io_read_1(struct sdmmc_function *sf, int reg)
    436 {
    437 	uint8_t data = 0;
    438 
    439 	/* Don't lock */
    440 
    441 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    442 	    SD_ARG_CMD52_READ);
    443 	return data;
    444 }
    445 
    446 void
    447 sdmmc_io_write_1(struct sdmmc_function *sf, int reg, uint8_t data)
    448 {
    449 
    450 	/* Don't lock */
    451 
    452 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    453 	    SD_ARG_CMD52_WRITE);
    454 }
    455 
    456 uint16_t
    457 sdmmc_io_read_2(struct sdmmc_function *sf, int reg)
    458 {
    459 	uint16_t data = 0;
    460 
    461 	/* Don't lock */
    462 
    463 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    464 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    465 	return data;
    466 }
    467 
    468 void
    469 sdmmc_io_write_2(struct sdmmc_function *sf, int reg, uint16_t data)
    470 {
    471 
    472 	/* Don't lock */
    473 
    474 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    475 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    476 }
    477 
    478 uint32_t
    479 sdmmc_io_read_4(struct sdmmc_function *sf, int reg)
    480 {
    481 	uint32_t data = 0;
    482 
    483 	/* Don't lock */
    484 
    485 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    486 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    487 	return data;
    488 }
    489 
    490 void
    491 sdmmc_io_write_4(struct sdmmc_function *sf, int reg, uint32_t data)
    492 {
    493 
    494 	/* Don't lock */
    495 
    496 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    497 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    498 }
    499 
    500 
    501 int
    502 sdmmc_io_read_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    503     int datalen)
    504 {
    505 	int blocks, bytes, error = 0;
    506 
    507 	/* Don't lock */
    508 
    509 	while (datalen >= sf->blklen) {
    510 		//blocks = imin(datalen / sf->blklen,
    511 		//              SD_ARG_CMD53_LENGTH_MAX);
    512 		blocks = 1;
    513 		bytes = blocks * sf->blklen;
    514 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    515 		    bytes, SD_ARG_CMD53_READ);
    516 		if (error)
    517 			goto error;
    518 		data += bytes;
    519 		datalen -= bytes;
    520 	}
    521 
    522 	if (datalen)
    523 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    524 		    SD_ARG_CMD53_READ);
    525 error:
    526 	return error;
    527 }
    528 
    529 int
    530 sdmmc_io_write_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    531     int datalen)
    532 {
    533 	int blocks, bytes, error = 0;
    534 
    535 	/* Don't lock */
    536 
    537 	while (datalen >= sf->blklen) {
    538 		//blocks = imin(datalen / sf->blklen,
    539 		//             SD_ARG_CMD53_LENGTH_MAX);
    540 		blocks = 1;
    541 		bytes = blocks * sf->blklen;
    542 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    543 		    bytes, SD_ARG_CMD53_WRITE);
    544 		if (error)
    545 			goto error;
    546 		data += bytes;
    547 		datalen -= bytes;
    548 	}
    549 
    550 	if (datalen)
    551 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    552 		    SD_ARG_CMD53_WRITE);
    553 error:
    554 	return error;
    555 }
    556 
    557 
    558 int
    559 sdmmc_io_read_region_1(struct sdmmc_function *sf, int reg, u_char *data,
    560     int datalen)
    561 {
    562 	int blocks, bytes, error = 0;
    563 
    564 	/* Don't lock */
    565 
    566 	while (datalen >= sf->blklen) {
    567 		//blocks = imin(datalen / sf->blklen,
    568 		//              SD_ARG_CMD53_LENGTH_MAX);
    569 		blocks = 1;
    570 		bytes = blocks * sf->blklen;
    571 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    572 		    bytes, SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    573 		if (error)
    574 			goto error;
    575 		reg += bytes;
    576 		data += bytes;
    577 		datalen -= bytes;
    578 	}
    579 
    580 	if (datalen)
    581 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    582 		    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    583 error:
    584 	return error;
    585 }
    586 
    587 int
    588 sdmmc_io_write_region_1(struct sdmmc_function *sf, int reg, u_char *data,
    589     int datalen)
    590 {
    591 	int blocks, bytes, error = 0;
    592 
    593 	/* Don't lock */
    594 
    595 	while (datalen >= sf->blklen) {
    596 		//blocks = imin(datalen / sf->blklen,
    597 		//              SD_ARG_CMD53_LENGTH_MAX);
    598 		blocks = 1;
    599 		bytes = blocks * sf->blklen;
    600 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    601 		    bytes, SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    602 		if (error)
    603 			goto error;
    604 		reg += bytes;
    605 		data += bytes;
    606 		datalen -= bytes;
    607 	}
    608 
    609 	if (datalen)
    610 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    611 		    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    612 error:
    613 	return error;
    614 }
    615 
    616 #if 0
    617 static int
    618 sdmmc_io_xchg(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    619     int reg, u_char *datap)
    620 {
    621 
    622 	/* Don't lock */
    623 
    624 	return sdmmc_io_rw_direct(sc, sf, reg, datap,
    625 	    SD_ARG_CMD52_WRITE|SD_ARG_CMD52_EXCHANGE);
    626 }
    627 #endif
    628 
    629 /*
    630  * Abort I/O function of the card
    631  */
    632 int
    633 sdmmc_io_function_abort(struct sdmmc_function *sf)
    634 {
    635 	u_char data = CCCR_CTL_AS(sf->number);
    636 
    637 	return sdmmc_io_rw_direct(sf->sc, NULL, SD_IO_CCCR_CTL, &data,
    638 	    SD_ARG_CMD52_WRITE);
    639 }
    640 
    641 /*
    642  * Reset the I/O functions of the card.
    643  */
    644 static void
    645 sdmmc_io_reset(struct sdmmc_softc *sc)
    646 {
    647 	u_char data = CCCR_CTL_RES;
    648 
    649 	if (sdmmc_io_rw_direct(sc, NULL, SD_IO_CCCR_CTL, &data,
    650 	    SD_ARG_CMD52_WRITE) == 0)
    651 		sdmmc_pause(100000, NULL); /* XXX SDMMC_LOCK */
    652 }
    653 
    654 /*
    655  * Get or set the card's I/O OCR value (SDIO).
    656  */
    657 static int
    658 sdmmc_io_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, u_int32_t *ocrp)
    659 {
    660 	struct sdmmc_command cmd;
    661 	int error;
    662 	int retry;
    663 
    664 	DPRINTF(("sdmmc_io_send_op_cond: ocr = %#x\n", ocr));
    665 
    666 	/* Don't lock */
    667 
    668 	/*
    669 	 * If we change the OCR value, retry the command until the OCR
    670 	 * we receive in response has the "CARD BUSY" bit set, meaning
    671 	 * that all cards are ready for identification.
    672 	 */
    673 	for (retry = 0; retry < 100; retry++) {
    674 		memset(&cmd, 0, sizeof cmd);
    675 		cmd.c_opcode = SD_IO_SEND_OP_COND;
    676 		cmd.c_arg = ocr;
    677 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R4 | SCF_TOUT_OK;
    678 
    679 		error = sdmmc_mmc_command(sc, &cmd);
    680 		if (error)
    681 			break;
    682 		if (ISSET(MMC_R4(cmd.c_resp), SD_IO_OCR_MEM_READY) || ocr == 0)
    683 			break;
    684 
    685 		error = ETIMEDOUT;
    686 		sdmmc_pause(10000, NULL);
    687 	}
    688 	if (error == 0 && ocrp != NULL)
    689 		*ocrp = MMC_R4(cmd.c_resp);
    690 
    691 	DPRINTF(("sdmmc_io_send_op_cond: error = %d\n", error));
    692 
    693 	return error;
    694 }
    695 
    696 /*
    697  * Card interrupt handling
    698  */
    699 
    700 void
    701 sdmmc_intr_enable(struct sdmmc_function *sf)
    702 {
    703 	struct sdmmc_softc *sc = sf->sc;
    704 	struct sdmmc_function *sf0 = sc->sc_fn0;
    705 	uint8_t reg;
    706 
    707 	SDMMC_LOCK(sc);
    708 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    709 	reg |= 1 << sf->number;
    710 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    711 	SDMMC_UNLOCK(sc);
    712 }
    713 
    714 void
    715 sdmmc_intr_disable(struct sdmmc_function *sf)
    716 {
    717 	struct sdmmc_softc *sc = sf->sc;
    718 	struct sdmmc_function *sf0 = sc->sc_fn0;
    719 	uint8_t reg;
    720 
    721 	SDMMC_LOCK(sc);
    722 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    723 	reg &= ~(1 << sf->number);
    724 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    725 	SDMMC_UNLOCK(sc);
    726 }
    727 
    728 /*
    729  * Establish a handler for the SDIO card interrupt.  Because the
    730  * interrupt may be shared with different SDIO functions, multiple
    731  * handlers can be established.
    732  */
    733 void *
    734 sdmmc_intr_establish(device_t dev, int (*fun)(void *), void *arg,
    735     const char *name)
    736 {
    737 	struct sdmmc_softc *sc = device_private(dev);
    738 	struct sdmmc_intr_handler *ih;
    739 
    740 	if (sc->sc_sct->card_enable_intr == NULL)
    741 		return NULL;
    742 
    743 	ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK|M_ZERO);
    744 	if (ih == NULL)
    745 		return NULL;
    746 
    747 	ih->ih_name = malloc(strlen(name) + 1, M_DEVBUF, M_WAITOK|M_ZERO);
    748 	if (ih->ih_name == NULL) {
    749 		free(ih, M_DEVBUF);
    750 		return NULL;
    751 	}
    752 	strlcpy(ih->ih_name, name, strlen(name));
    753 	ih->ih_softc = sc;
    754 	ih->ih_fun = fun;
    755 	ih->ih_arg = arg;
    756 
    757 	mutex_enter(&sc->sc_mtx);
    758 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    759 		sdmmc_intr_enable(sc->sc_fn0);
    760 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 1);
    761 	}
    762 	TAILQ_INSERT_TAIL(&sc->sc_intrq, ih, entry);
    763 	mutex_exit(&sc->sc_mtx);
    764 
    765 	return ih;
    766 }
    767 
    768 /*
    769  * Disestablish the given handler.
    770  */
    771 void
    772 sdmmc_intr_disestablish(void *cookie)
    773 {
    774 	struct sdmmc_intr_handler *ih = cookie;
    775 	struct sdmmc_softc *sc = ih->ih_softc;
    776 
    777 	if (sc->sc_sct->card_enable_intr == NULL)
    778 		return;
    779 
    780 	mutex_enter(&sc->sc_mtx);
    781 	TAILQ_REMOVE(&sc->sc_intrq, ih, entry);
    782 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    783 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 0);
    784 		sdmmc_intr_disable(sc->sc_fn0);
    785 	}
    786 	mutex_exit(&sc->sc_mtx);
    787 
    788 	free(ih->ih_name, M_DEVBUF);
    789 	free(ih, M_DEVBUF);
    790 }
    791 
    792 /*
    793  * Call established SDIO card interrupt handlers.  The host controller
    794  * must call this function from its own interrupt handler to handle an
    795  * SDIO interrupt from the card.
    796  */
    797 void
    798 sdmmc_card_intr(device_t dev)
    799 {
    800 	struct sdmmc_softc *sc = device_private(dev);
    801 
    802 	if (sc->sc_sct->card_enable_intr == NULL)
    803 		return;
    804 
    805 	mutex_enter(&sc->sc_intr_task_mtx);
    806 	if (!sdmmc_task_pending(&sc->sc_intr_task))
    807 		sdmmc_add_task(sc, &sc->sc_intr_task);
    808 	mutex_exit(&sc->sc_intr_task_mtx);
    809 }
    810 
    811 void
    812 sdmmc_intr_task(void *arg)
    813 {
    814 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    815 	struct sdmmc_intr_handler *ih;
    816 
    817 	mutex_enter(&sc->sc_mtx);
    818 	TAILQ_FOREACH(ih, &sc->sc_intrq, entry) {
    819 		/* XXX examine return value and do evcount stuff*/
    820 		(void)(*ih->ih_fun)(ih->ih_arg);
    821 	}
    822 	mutex_exit(&sc->sc_mtx);
    823 
    824 	sdmmc_chip_card_intr_ack(sc->sc_sct, sc->sc_sch);
    825 }
    826 
    827 int
    828 sdmmc_io_set_blocklen(struct sdmmc_function *sf,
    829      int blklen)
    830 {
    831 	struct sdmmc_softc *sc = sf->sc;
    832 	struct sdmmc_function *sf0 = sc->sc_fn0;
    833 	int error = EINVAL;
    834 
    835 	SDMMC_LOCK(sc);
    836 
    837 	if (blklen <= 0 ||
    838 	    blklen > sdmmc_chip_host_maxblklen(sc->sc_sct, sc->sc_sch))
    839 		goto err;
    840 
    841 	sdmmc_io_write_1(sf0, SD_IO_FBR(sf->number) +
    842 	    SD_IO_FBR_BLOCKLEN, blklen & 0xff);
    843 	sdmmc_io_write_1(sf0, SD_IO_FBR(sf->number) +
    844 	    SD_IO_FBR_BLOCKLEN + 1, (blklen >> 8) & 0xff);
    845 
    846 	sf->blklen = blklen;
    847 	error = 0;
    848 
    849 err:
    850 	SDMMC_UNLOCK(sc);
    851 
    852 	return error;
    853 }
    854 
    855