Home | History | Annotate | Line # | Download | only in sdmmc
sdmmc_io.c revision 1.3
      1 /*	$NetBSD: sdmmc_io.c,v 1.3 2010/10/07 12:24:23 kiyohara 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.3 2010/10/07 12:24:23 kiyohara Exp $");
     24 
     25 #include <sys/param.h>
     26 #include <sys/kernel.h>
     27 #include <sys/malloc.h>
     28 #include <sys/proc.h>
     29 #include <sys/systm.h>
     30 
     31 #include <dev/sdmmc/sdmmc_ioreg.h>
     32 #include <dev/sdmmc/sdmmcchip.h>
     33 #include <dev/sdmmc/sdmmcreg.h>
     34 #include <dev/sdmmc/sdmmcvar.h>
     35 
     36 #ifdef SDMMC_DEBUG
     37 #define DPRINTF(s)	do { printf s; } while (0)
     38 #else
     39 #define DPRINTF(s)	do {} while (0)
     40 #endif
     41 
     42 struct sdmmc_intr_handler {
     43 	struct sdmmc_softc *ih_softc;
     44 	char *ih_name;
     45 	int (*ih_fun)(void *);
     46 	void *ih_arg;
     47 	TAILQ_ENTRY(sdmmc_intr_handler) entry;
     48 };
     49 
     50 static int	sdmmc_io_rw_direct(struct sdmmc_softc *,
     51 		    struct sdmmc_function *, int, u_char *, int);
     52 static int	sdmmc_io_rw_extended(struct sdmmc_softc *,
     53 		    struct sdmmc_function *, int, u_char *, int, int);
     54 #if 0
     55 static int	sdmmc_io_xchg(struct sdmmc_softc *, struct sdmmc_function *,
     56 		    int, u_char *);
     57 #endif
     58 static void	sdmmc_io_reset(struct sdmmc_softc *);
     59 static int	sdmmc_io_send_op_cond(struct sdmmc_softc *, uint32_t,
     60 		    uint32_t *);
     61 
     62 /*
     63  * Initialize SD I/O card functions (before memory cards).  The host
     64  * system and controller must support card interrupts in order to use
     65  * I/O functions.
     66  */
     67 int
     68 sdmmc_io_enable(struct sdmmc_softc *sc)
     69 {
     70 	uint32_t host_ocr;
     71 	uint32_t card_ocr;
     72 	int error;
     73 
     74 	SDMMC_LOCK(sc);
     75 
     76 	/* Set host mode to SD "combo" card. */
     77 	SET(sc->sc_flags, SMF_SD_MODE|SMF_IO_MODE|SMF_MEM_MODE);
     78 
     79 	/* Reset I/O functions. */
     80 	sdmmc_io_reset(sc);
     81 
     82 	/*
     83 	 * Read the I/O OCR value, determine the number of I/O
     84 	 * functions and whether memory is also present (a "combo
     85 	 * card") by issuing CMD5.  SD memory-only and MMC cards
     86 	 * do not respond to CMD5.
     87 	 */
     88 	error = sdmmc_io_send_op_cond(sc, 0, &card_ocr);
     89 	if (error) {
     90 		/* No SDIO card; switch to SD memory-only mode. */
     91 		CLR(sc->sc_flags, SMF_IO_MODE);
     92 		error = 0;
     93 		goto out;
     94 	}
     95 
     96 	/* Parse the additional bits in the I/O OCR value. */
     97 	if (!ISSET(card_ocr, SD_IO_OCR_MEM_PRESENT)) {
     98 		/* SDIO card without memory (not a "combo card"). */
     99 		DPRINTF(("%s: no memory present\n", SDMMCDEVNAME(sc)));
    100 		CLR(sc->sc_flags, SMF_MEM_MODE);
    101 	}
    102 	sc->sc_function_count = SD_IO_OCR_NUM_FUNCTIONS(card_ocr);
    103 	if (sc->sc_function_count == 0) {
    104 		/* Useless SDIO card without any I/O functions. */
    105 		DPRINTF(("%s: no I/O functions\n", SDMMCDEVNAME(sc)));
    106 		CLR(sc->sc_flags, SMF_IO_MODE);
    107 		error = 0;
    108 		goto out;
    109 	}
    110 	card_ocr &= SD_IO_OCR_MASK;
    111 
    112 	/* Set the lowest voltage supported by the card and host. */
    113 	host_ocr = sdmmc_chip_host_ocr(sc->sc_sct, sc->sc_sch);
    114 	error = sdmmc_set_bus_power(sc, host_ocr, card_ocr);
    115 	if (error) {
    116 		aprint_error_dev(sc->sc_dev,
    117 		    "couldn't supply voltage requested by card\n");
    118 		goto out;
    119 	}
    120 
    121 	/* Reset I/O functions (again). */
    122 	sdmmc_io_reset(sc);
    123 
    124 	/* Send the new OCR value until all cards are ready. */
    125 	error = sdmmc_io_send_op_cond(sc, host_ocr, NULL);
    126 	if (error) {
    127 		aprint_error_dev(sc->sc_dev, "couldn't send I/O OCR\n");
    128 		goto out;
    129 	}
    130 
    131 out:
    132 	SDMMC_UNLOCK(sc);
    133 
    134 	return error;
    135 }
    136 
    137 /*
    138  * Allocate sdmmc_function structures for SD card I/O function
    139  * (including function 0).
    140  */
    141 void
    142 sdmmc_io_scan(struct sdmmc_softc *sc)
    143 {
    144 	struct sdmmc_function *sf0, *sf;
    145 	int error;
    146 	int i;
    147 
    148 	SDMMC_LOCK(sc);
    149 
    150 	sf0 = sdmmc_function_alloc(sc);
    151 	sf0->number = 0;
    152 	error = sdmmc_set_relative_addr(sc, sf0);
    153 	if (error) {
    154 		aprint_error_dev(sc->sc_dev, "couldn't set I/O RCA\n");
    155 		SET(sf0->flags, SFF_ERROR);
    156 		goto out;
    157 	}
    158 	sc->sc_fn0 = sf0;
    159 	SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf0, sf_list);
    160 
    161 	/* Go to Data Transfer Mode, if possible. */
    162 	sdmmc_chip_bus_rod(sc->sc_sct, sc->sc_sch, 0);
    163 
    164 	/* Verify that the RCA has been set by selecting the card. */
    165 	error = sdmmc_select_card(sc, sf0);
    166 	if (error) {
    167 		aprint_error_dev(sc->sc_dev, "couldn't select I/O RCA %d\n",
    168 		    sf0->rca);
    169 		SET(sf0->flags, SFF_ERROR);
    170 		goto out;
    171 	}
    172 
    173 	for (i = 1; i <= sc->sc_function_count; i++) {
    174 		sf = sdmmc_function_alloc(sc);
    175 		sf->number = i;
    176 		sf->rca = sf0->rca;
    177 		SIMPLEQ_INSERT_TAIL(&sc->sf_head, sf, sf_list);
    178 	}
    179 
    180 out:
    181 	SDMMC_UNLOCK(sc);
    182 }
    183 
    184 /*
    185  * Initialize SDIO card functions.
    186  */
    187 int
    188 sdmmc_io_init(struct sdmmc_softc *sc, struct sdmmc_function *sf)
    189 {
    190 	int error = 0;
    191 
    192 	SDMMC_LOCK(sc);
    193 
    194 	if (sf->number == 0) {
    195 		sdmmc_io_write_1(sf, SD_IO_CCCR_BUS_WIDTH, CCCR_BUS_WIDTH_1);
    196 
    197 		error = sdmmc_read_cis(sf, &sf->cis);
    198 		if (error) {
    199 			aprint_error_dev(sc->sc_dev, "couldn't read CIS\n");
    200 			SET(sf->flags, SFF_ERROR);
    201 			goto out;
    202 		}
    203 
    204 		sdmmc_check_cis_quirks(sf);
    205 
    206 #ifdef SDMMC_DEBUG
    207 		if (sdmmcdebug)
    208 			sdmmc_print_cis(sf);
    209 #endif
    210 
    211 		if (sc->sc_busclk > sf->csd.tran_speed)
    212 			sc->sc_busclk = sf->csd.tran_speed;
    213 		error =
    214 		    sdmmc_chip_bus_clock(sc->sc_sct, sc->sc_sch, sc->sc_busclk);
    215 		if (error)
    216 			aprint_error_dev(sc->sc_dev,
    217 			    "can't change bus clock\n");
    218 	}
    219 
    220 out:
    221 	SDMMC_UNLOCK(sc);
    222 
    223 	return error;
    224 }
    225 
    226 /*
    227  * Indicate whether the function is ready to operate.
    228  */
    229 static int
    230 sdmmc_io_function_ready(struct sdmmc_function *sf)
    231 {
    232 	struct sdmmc_softc *sc = sf->sc;
    233 	struct sdmmc_function *sf0 = sc->sc_fn0;
    234 	uint8_t reg;
    235 
    236 	if (sf->number == 0)
    237 		return 1;	/* FN0 is always ready */
    238 
    239 	SDMMC_LOCK(sc);
    240 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_IOREADY);
    241 	SDMMC_UNLOCK(sc);
    242 	return (reg & (1 << sf->number)) != 0;
    243 }
    244 
    245 int
    246 sdmmc_io_function_enable(struct sdmmc_function *sf)
    247 {
    248 	struct sdmmc_softc *sc = sf->sc;
    249 	struct sdmmc_function *sf0 = sc->sc_fn0;
    250 	uint8_t reg;
    251 	int retry;
    252 
    253 	if (sf->number == 0)
    254 		return 0;	/* FN0 is always enabled */
    255 
    256 	SDMMC_LOCK(sc);
    257 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
    258 	SET(reg, (1U << sf->number));
    259 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
    260 	SDMMC_UNLOCK(sc);
    261 
    262 	retry = 5;
    263 	while (!sdmmc_io_function_ready(sf) && retry-- > 0)
    264 		kpause("pause", false, hz, NULL);
    265 	return (retry >= 0) ? 0 : ETIMEDOUT;
    266 }
    267 
    268 /*
    269  * Disable the I/O function.  Return zero if the function was
    270  * disabled successfully.
    271  */
    272 void
    273 sdmmc_io_function_disable(struct sdmmc_function *sf)
    274 {
    275 	struct sdmmc_softc *sc = sf->sc;
    276 	struct sdmmc_function *sf0 = sc->sc_fn0;
    277 	uint8_t reg;
    278 
    279 	if (sf->number == 0)
    280 		return;		/* FN0 is always enabled */
    281 
    282 	SDMMC_LOCK(sc);
    283 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_ENABLE);
    284 	CLR(reg, (1U << sf->number));
    285 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_ENABLE, reg);
    286 	SDMMC_UNLOCK(sc);
    287 }
    288 
    289 static int
    290 sdmmc_io_rw_direct(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    291     int reg, u_char *datap, int arg)
    292 {
    293 	struct sdmmc_command cmd;
    294 	int error;
    295 
    296 	/* Don't lock */
    297 
    298 	/* Make sure the card is selected. */
    299 	error = sdmmc_select_card(sc, sf);
    300 	if (error)
    301 		return error;
    302 
    303 	arg |= ((sf == NULL ? 0 : sf->number) & SD_ARG_CMD52_FUNC_MASK) <<
    304 	    SD_ARG_CMD52_FUNC_SHIFT;
    305 	arg |= (reg & SD_ARG_CMD52_REG_MASK) <<
    306 	    SD_ARG_CMD52_REG_SHIFT;
    307 	arg |= (*datap & SD_ARG_CMD52_DATA_MASK) <<
    308 	    SD_ARG_CMD52_DATA_SHIFT;
    309 
    310 	memset(&cmd, 0, sizeof cmd);
    311 	cmd.c_opcode = SD_IO_RW_DIRECT;
    312 	cmd.c_arg = arg;
    313 	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
    314 
    315 	error = sdmmc_mmc_command(sc, &cmd);
    316 	*datap = SD_R5_DATA(cmd.c_resp);
    317 
    318 	return error;
    319 }
    320 
    321 /*
    322  * Useful values of `arg' to pass in are either SD_ARG_CMD53_READ or
    323  * SD_ARG_CMD53_WRITE.  SD_ARG_CMD53_INCREMENT may be ORed into `arg'
    324  * to access successive register locations instead of accessing the
    325  * same register many times.
    326  */
    327 static int
    328 sdmmc_io_rw_extended(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    329     int reg, u_char *datap, int datalen, int arg)
    330 {
    331 	struct sdmmc_command cmd;
    332 	int error;
    333 
    334 	/* Don't lock */
    335 
    336 #if 0
    337 	/* Make sure the card is selected. */
    338 	error = sdmmc_select_card(sc, sf);
    339 	if (error)
    340 		return error;
    341 #endif
    342 
    343 	arg |= (((sf == NULL) ? 0 : sf->number) & SD_ARG_CMD53_FUNC_MASK) <<
    344 	    SD_ARG_CMD53_FUNC_SHIFT;
    345 	arg |= (reg & SD_ARG_CMD53_REG_MASK) <<
    346 	    SD_ARG_CMD53_REG_SHIFT;
    347 	arg |= (datalen & SD_ARG_CMD53_LENGTH_MASK) <<
    348 	    SD_ARG_CMD53_LENGTH_SHIFT;
    349 
    350 	memset(&cmd, 0, sizeof cmd);
    351 	cmd.c_opcode = SD_IO_RW_EXTENDED;
    352 	cmd.c_arg = arg;
    353 	cmd.c_flags = SCF_CMD_AC | SCF_RSP_R5;
    354 	cmd.c_data = datap;
    355 	cmd.c_datalen = datalen;
    356 	cmd.c_blklen = MIN(datalen,
    357 	    sdmmc_chip_host_maxblklen(sc->sc_sct,sc->sc_sch));
    358 	if (!ISSET(arg, SD_ARG_CMD53_WRITE))
    359 		cmd.c_flags |= SCF_CMD_READ;
    360 
    361 	error = sdmmc_mmc_command(sc, &cmd);
    362 
    363 	return error;
    364 }
    365 
    366 uint8_t
    367 sdmmc_io_read_1(struct sdmmc_function *sf, int reg)
    368 {
    369 	uint8_t data = 0;
    370 
    371 	/* Don't lock */
    372 
    373 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    374 	    SD_ARG_CMD52_READ);
    375 	return data;
    376 }
    377 
    378 void
    379 sdmmc_io_write_1(struct sdmmc_function *sf, int reg, uint8_t data)
    380 {
    381 
    382 	/* Don't lock */
    383 
    384 	(void)sdmmc_io_rw_direct(sf->sc, sf, reg, (u_char *)&data,
    385 	    SD_ARG_CMD52_WRITE);
    386 }
    387 
    388 uint16_t
    389 sdmmc_io_read_2(struct sdmmc_function *sf, int reg)
    390 {
    391 	uint16_t data = 0;
    392 
    393 	/* Don't lock */
    394 
    395 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    396 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    397 	return data;
    398 }
    399 
    400 void
    401 sdmmc_io_write_2(struct sdmmc_function *sf, int reg, uint16_t data)
    402 {
    403 
    404 	/* Don't lock */
    405 
    406 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 2,
    407 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    408 }
    409 
    410 uint32_t
    411 sdmmc_io_read_4(struct sdmmc_function *sf, int reg)
    412 {
    413 	uint32_t data = 0;
    414 
    415 	/* Don't lock */
    416 
    417 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    418 	    SD_ARG_CMD53_READ | SD_ARG_CMD53_INCREMENT);
    419 	return data;
    420 }
    421 
    422 void
    423 sdmmc_io_write_4(struct sdmmc_function *sf, int reg, uint32_t data)
    424 {
    425 
    426 	/* Don't lock */
    427 
    428 	(void)sdmmc_io_rw_extended(sf->sc, sf, reg, (u_char *)&data, 4,
    429 	    SD_ARG_CMD53_WRITE | SD_ARG_CMD53_INCREMENT);
    430 }
    431 
    432 
    433 int
    434 sdmmc_io_read_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    435     int datalen)
    436 {
    437 	int error;
    438 
    439 	/* Don't lock */
    440 
    441 	while (datalen > SD_ARG_CMD53_LENGTH_MAX) {
    442 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    443 		    SD_ARG_CMD53_LENGTH_MAX, SD_ARG_CMD53_READ);
    444 		if (error)
    445 			goto error;
    446 		data += SD_ARG_CMD53_LENGTH_MAX;
    447 		datalen -= SD_ARG_CMD53_LENGTH_MAX;
    448 	}
    449 
    450 	error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    451 	    SD_ARG_CMD53_READ);
    452 error:
    453 	return error;
    454 }
    455 
    456 int
    457 sdmmc_io_write_multi_1(struct sdmmc_function *sf, int reg, u_char *data,
    458     int datalen)
    459 {
    460 	int error;
    461 
    462 	/* Don't lock */
    463 
    464 	while (datalen > SD_ARG_CMD53_LENGTH_MAX) {
    465 		error = sdmmc_io_rw_extended(sf->sc, sf, reg, data,
    466 		    SD_ARG_CMD53_LENGTH_MAX, SD_ARG_CMD53_WRITE);
    467 		if (error)
    468 			goto error;
    469 		data += SD_ARG_CMD53_LENGTH_MAX;
    470 		datalen -= SD_ARG_CMD53_LENGTH_MAX;
    471 	}
    472 
    473 	error = sdmmc_io_rw_extended(sf->sc, sf, reg, data, datalen,
    474 	    SD_ARG_CMD53_WRITE);
    475 error:
    476 	return error;
    477 }
    478 
    479 #if 0
    480 static int
    481 sdmmc_io_xchg(struct sdmmc_softc *sc, struct sdmmc_function *sf,
    482     int reg, u_char *datap)
    483 {
    484 
    485 	/* Don't lock */
    486 
    487 	return sdmmc_io_rw_direct(sc, sf, reg, datap,
    488 	    SD_ARG_CMD52_WRITE|SD_ARG_CMD52_EXCHANGE);
    489 }
    490 #endif
    491 
    492 /*
    493  * Reset the I/O functions of the card.
    494  */
    495 static void
    496 sdmmc_io_reset(struct sdmmc_softc *sc)
    497 {
    498 
    499 	/* Don't lock */
    500 #if 0 /* XXX command fails */
    501 	(void)sdmmc_io_write(sc, NULL, SD_IO_REG_CCCR_CTL, CCCR_CTL_RES);
    502 	sdmmc_delay(100000);
    503 #endif
    504 }
    505 
    506 /*
    507  * Get or set the card's I/O OCR value (SDIO).
    508  */
    509 static int
    510 sdmmc_io_send_op_cond(struct sdmmc_softc *sc, u_int32_t ocr, u_int32_t *ocrp)
    511 {
    512 	struct sdmmc_command cmd;
    513 	int error;
    514 	int retry;
    515 
    516 	DPRINTF(("sdmmc_io_send_op_cond: ocr = %#x\n", ocr));
    517 
    518 	/* Don't lock */
    519 
    520 	/*
    521 	 * If we change the OCR value, retry the command until the OCR
    522 	 * we receive in response has the "CARD BUSY" bit set, meaning
    523 	 * that all cards are ready for identification.
    524 	 */
    525 	for (retry = 0; retry < 100; retry++) {
    526 		memset(&cmd, 0, sizeof cmd);
    527 		cmd.c_opcode = SD_IO_SEND_OP_COND;
    528 		cmd.c_arg = ocr;
    529 		cmd.c_flags = SCF_CMD_BCR | SCF_RSP_R4;
    530 
    531 		error = sdmmc_mmc_command(sc, &cmd);
    532 		if (error)
    533 			break;
    534 		if (ISSET(MMC_R4(cmd.c_resp), SD_IO_OCR_MEM_READY) || ocr == 0)
    535 			break;
    536 
    537 		error = ETIMEDOUT;
    538 		sdmmc_delay(10000);
    539 	}
    540 	if (error == 0 && ocrp != NULL)
    541 		*ocrp = MMC_R4(cmd.c_resp);
    542 
    543 	DPRINTF(("sdmmc_io_send_op_cond: error = %d\n", error));
    544 
    545 	return error;
    546 }
    547 
    548 /*
    549  * Card interrupt handling
    550  */
    551 
    552 void
    553 sdmmc_intr_enable(struct sdmmc_function *sf)
    554 {
    555 	struct sdmmc_softc *sc = sf->sc;
    556 	struct sdmmc_function *sf0 = sc->sc_fn0;
    557 	uint8_t reg;
    558 
    559 	SDMMC_LOCK(sc);
    560 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    561 	reg |= 1 << sf->number;
    562 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    563 	SDMMC_UNLOCK(sc);
    564 }
    565 
    566 void
    567 sdmmc_intr_disable(struct sdmmc_function *sf)
    568 {
    569 	struct sdmmc_softc *sc = sf->sc;
    570 	struct sdmmc_function *sf0 = sc->sc_fn0;
    571 	uint8_t reg;
    572 
    573 	SDMMC_LOCK(sc);
    574 	reg = sdmmc_io_read_1(sf0, SD_IO_CCCR_FN_INTEN);
    575 	reg &= ~(1 << sf->number);
    576 	sdmmc_io_write_1(sf0, SD_IO_CCCR_FN_INTEN, reg);
    577 	SDMMC_UNLOCK(sc);
    578 }
    579 
    580 /*
    581  * Establish a handler for the SDIO card interrupt.  Because the
    582  * interrupt may be shared with different SDIO functions, multiple
    583  * handlers can be established.
    584  */
    585 void *
    586 sdmmc_intr_establish(device_t dev, int (*fun)(void *), void *arg,
    587     const char *name)
    588 {
    589 	struct sdmmc_softc *sc = device_private(dev);
    590 	struct sdmmc_intr_handler *ih;
    591 	int s;
    592 
    593 	if (sc->sc_sct->card_enable_intr == NULL)
    594 		return NULL;
    595 
    596 	ih = malloc(sizeof *ih, M_DEVBUF, M_WAITOK|M_CANFAIL|M_ZERO);
    597 	if (ih == NULL)
    598 		return NULL;
    599 
    600 	ih->ih_name = malloc(strlen(name) + 1, M_DEVBUF,
    601 	    M_WAITOK|M_CANFAIL|M_ZERO);
    602 	if (ih->ih_name == NULL) {
    603 		free(ih, M_DEVBUF);
    604 		return NULL;
    605 	}
    606 	strlcpy(ih->ih_name, name, strlen(name));
    607 	ih->ih_softc = sc;
    608 	ih->ih_fun = fun;
    609 	ih->ih_arg = arg;
    610 
    611 	s = splhigh();
    612 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    613 		sdmmc_intr_enable(sc->sc_fn0);
    614 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 1);
    615 	}
    616 	TAILQ_INSERT_TAIL(&sc->sc_intrq, ih, entry);
    617 	splx(s);
    618 
    619 	return ih;
    620 }
    621 
    622 /*
    623  * Disestablish the given handler.
    624  */
    625 void
    626 sdmmc_intr_disestablish(void *cookie)
    627 {
    628 	struct sdmmc_intr_handler *ih = cookie;
    629 	struct sdmmc_softc *sc = ih->ih_softc;
    630 	int s;
    631 
    632 	if (sc->sc_sct->card_enable_intr == NULL)
    633 		return;
    634 
    635 	s = splhigh();
    636 	TAILQ_REMOVE(&sc->sc_intrq, ih, entry);
    637 	if (TAILQ_EMPTY(&sc->sc_intrq)) {
    638 		sdmmc_chip_card_enable_intr(sc->sc_sct, sc->sc_sch, 0);
    639 		sdmmc_intr_disable(sc->sc_fn0);
    640 	}
    641 	splx(s);
    642 
    643 	free(ih->ih_name, M_DEVBUF);
    644 	free(ih, M_DEVBUF);
    645 }
    646 
    647 /*
    648  * Call established SDIO card interrupt handlers.  The host controller
    649  * must call this function from its own interrupt handler to handle an
    650  * SDIO interrupt from the card.
    651  */
    652 void
    653 sdmmc_card_intr(device_t dev)
    654 {
    655 	struct sdmmc_softc *sc = device_private(dev);
    656 
    657 	if (sc->sc_sct->card_enable_intr) {
    658 		mutex_enter(&sc->sc_intr_task_mtx);
    659 		if (!sdmmc_task_pending(&sc->sc_intr_task))
    660 			sdmmc_add_task(sc, &sc->sc_intr_task);
    661 		mutex_exit(&sc->sc_intr_task_mtx);
    662 	}
    663 }
    664 
    665 void
    666 sdmmc_intr_task(void *arg)
    667 {
    668 	struct sdmmc_softc *sc = (struct sdmmc_softc *)arg;
    669 	struct sdmmc_intr_handler *ih;
    670 	int s;
    671 
    672 	s = splsdmmc();
    673 	TAILQ_FOREACH(ih, &sc->sc_intrq, entry) {
    674 		splx(s);
    675 		/* XXX examine return value and do evcount stuff*/
    676 		(void)(*ih->ih_fun)(ih->ih_arg);
    677 		s = splsdmmc();
    678 	}
    679 	sdmmc_chip_card_intr_ack(sc->sc_sct, sc->sc_sch);
    680 	splx(s);
    681 }
    682