Home | History | Annotate | Line # | Download | only in sdmmc
sdhc.c revision 1.10
      1 /*	$NetBSD: sdhc.c,v 1.10 2012/02/02 22:49:17 nonaka Exp $	*/
      2 /*	$OpenBSD: sdhc.c,v 1.25 2009/01/13 19:44:20 grange Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 /*
     21  * SD Host Controller driver based on the SD Host Controller Standard
     22  * Simplified Specification Version 1.00 (www.sdcard.com).
     23  */
     24 
     25 #include <sys/cdefs.h>
     26 __KERNEL_RCSID(0, "$NetBSD: sdhc.c,v 1.10 2012/02/02 22:49:17 nonaka Exp $");
     27 
     28 #ifdef _KERNEL_OPT
     29 #include "opt_sdmmc.h"
     30 #endif
     31 
     32 #include <sys/param.h>
     33 #include <sys/device.h>
     34 #include <sys/kernel.h>
     35 #include <sys/kthread.h>
     36 #include <sys/malloc.h>
     37 #include <sys/systm.h>
     38 #include <sys/mutex.h>
     39 #include <sys/condvar.h>
     40 
     41 #include <dev/sdmmc/sdhcreg.h>
     42 #include <dev/sdmmc/sdhcvar.h>
     43 #include <dev/sdmmc/sdmmcchip.h>
     44 #include <dev/sdmmc/sdmmcreg.h>
     45 #include <dev/sdmmc/sdmmcvar.h>
     46 
     47 #ifdef SDHC_DEBUG
     48 int sdhcdebug = 1;
     49 #define DPRINTF(n,s)	do { if ((n) <= sdhcdebug) printf s; } while (0)
     50 void	sdhc_dump_regs(struct sdhc_host *);
     51 #else
     52 #define DPRINTF(n,s)	do {} while (0)
     53 #endif
     54 
     55 #define SDHC_COMMAND_TIMEOUT	hz
     56 #define SDHC_BUFFER_TIMEOUT	hz
     57 #define SDHC_TRANSFER_TIMEOUT	hz
     58 #define SDHC_DMA_TIMEOUT	hz
     59 
     60 struct sdhc_host {
     61 	struct sdhc_softc *sc;		/* host controller device */
     62 
     63 	bus_space_tag_t iot;		/* host register set tag */
     64 	bus_space_handle_t ioh;		/* host register set handle */
     65 	bus_dma_tag_t dmat;		/* host DMA tag */
     66 
     67 	device_t sdmmc;			/* generic SD/MMC device */
     68 
     69 	struct kmutex host_mtx;
     70 
     71 	u_int clkbase;			/* base clock frequency in KHz */
     72 	int maxblklen;			/* maximum block length */
     73 	uint32_t ocr;			/* OCR value from capabilities */
     74 
     75 	uint8_t regs[14];		/* host controller state */
     76 
     77 	uint16_t intr_status;		/* soft interrupt status */
     78 	uint16_t intr_error_status;	/* soft error status */
     79 	struct kmutex intr_mtx;
     80 	struct kcondvar intr_cv;
     81 
     82 	uint32_t flags;			/* flags for this host */
     83 #define SHF_USE_DMA		0x0001
     84 #define SHF_USE_4BIT_MODE	0x0002
     85 };
     86 
     87 #define HDEVNAME(hp)	(device_xname((hp)->sc->sc_dev))
     88 
     89 #define HREAD1(hp, reg)							\
     90 	(bus_space_read_1((hp)->iot, (hp)->ioh, (reg)))
     91 #define HREAD2(hp, reg)							\
     92 	(bus_space_read_2((hp)->iot, (hp)->ioh, (reg)))
     93 #define HREAD4(hp, reg)							\
     94 	(bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
     95 #define HWRITE1(hp, reg, val)						\
     96 	bus_space_write_1((hp)->iot, (hp)->ioh, (reg), (val))
     97 #define HWRITE2(hp, reg, val)						\
     98 	bus_space_write_2((hp)->iot, (hp)->ioh, (reg), (val))
     99 #define HWRITE4(hp, reg, val)						\
    100 	bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val))
    101 #define HCLR1(hp, reg, bits)						\
    102 	HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits))
    103 #define HCLR2(hp, reg, bits)						\
    104 	HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits))
    105 #define HSET1(hp, reg, bits)						\
    106 	HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits))
    107 #define HSET2(hp, reg, bits)						\
    108 	HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits))
    109 
    110 static int	sdhc_host_reset(sdmmc_chipset_handle_t);
    111 static int	sdhc_host_reset1(sdmmc_chipset_handle_t);
    112 static uint32_t	sdhc_host_ocr(sdmmc_chipset_handle_t);
    113 static int	sdhc_host_maxblklen(sdmmc_chipset_handle_t);
    114 static int	sdhc_card_detect(sdmmc_chipset_handle_t);
    115 static int	sdhc_write_protect(sdmmc_chipset_handle_t);
    116 static int	sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t);
    117 static int	sdhc_bus_clock(sdmmc_chipset_handle_t, int);
    118 static int	sdhc_bus_width(sdmmc_chipset_handle_t, int);
    119 static int	sdhc_bus_rod(sdmmc_chipset_handle_t, int);
    120 static void	sdhc_card_enable_intr(sdmmc_chipset_handle_t, int);
    121 static void	sdhc_card_intr_ack(sdmmc_chipset_handle_t);
    122 static void	sdhc_exec_command(sdmmc_chipset_handle_t,
    123 		    struct sdmmc_command *);
    124 static int	sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
    125 static int	sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t);
    126 static int	sdhc_soft_reset(struct sdhc_host *, int);
    127 static int	sdhc_wait_intr(struct sdhc_host *, int, int);
    128 static void	sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
    129 static int	sdhc_transfer_data_dma(struct sdhc_host *, struct sdmmc_command *);
    130 static int	sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *);
    131 static void	sdhc_read_data_pio(struct sdhc_host *, uint8_t *, int);
    132 static void	sdhc_write_data_pio(struct sdhc_host *, uint8_t *, int);
    133 
    134 static struct sdmmc_chip_functions sdhc_functions = {
    135 	/* host controller reset */
    136 	sdhc_host_reset,
    137 
    138 	/* host controller capabilities */
    139 	sdhc_host_ocr,
    140 	sdhc_host_maxblklen,
    141 
    142 	/* card detection */
    143 	sdhc_card_detect,
    144 
    145 	/* write protect */
    146 	sdhc_write_protect,
    147 
    148 	/* bus power, clock frequency and width */
    149 	sdhc_bus_power,
    150 	sdhc_bus_clock,
    151 	sdhc_bus_width,
    152 	sdhc_bus_rod,
    153 
    154 	/* command execution */
    155 	sdhc_exec_command,
    156 
    157 	/* card interrupt */
    158 	sdhc_card_enable_intr,
    159 	sdhc_card_intr_ack
    160 };
    161 
    162 /*
    163  * Called by attachment driver.  For each SD card slot there is one SD
    164  * host controller standard register set. (1.3)
    165  */
    166 int
    167 sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
    168     bus_space_handle_t ioh, bus_size_t iosize)
    169 {
    170 	struct sdmmcbus_attach_args saa;
    171 	struct sdhc_host *hp;
    172 	uint32_t caps;
    173 #ifdef SDHC_DEBUG
    174 	uint16_t sdhcver;
    175 
    176 	sdhcver = bus_space_read_2(iot, ioh, SDHC_HOST_CTL_VERSION);
    177 	aprint_normal_dev(sc->sc_dev, "SD Host Specification/Vendor Version ");
    178 	switch (SDHC_SPEC_VERSION(sdhcver)) {
    179 	case 0x00:
    180 		aprint_normal("1.0/%u\n", SDHC_VENDOR_VERSION(sdhcver));
    181 		break;
    182 
    183 	case 0x01:
    184 		aprint_normal("2.0/%u\n", SDHC_VENDOR_VERSION(sdhcver));
    185 		break;
    186 
    187 	default:
    188 		aprint_normal(">2.0/%u\n", SDHC_VENDOR_VERSION(sdhcver));
    189 		break;
    190 	}
    191 #endif
    192 
    193 	/* Allocate one more host structure. */
    194 	hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO);
    195 	if (hp == NULL) {
    196 		aprint_error_dev(sc->sc_dev,
    197 		    "couldn't alloc memory (sdhc host)\n");
    198 		goto err1;
    199 	}
    200 	sc->sc_host[sc->sc_nhosts++] = hp;
    201 
    202 	/* Fill in the new host structure. */
    203 	hp->sc = sc;
    204 	hp->iot = iot;
    205 	hp->ioh = ioh;
    206 	hp->dmat = sc->sc_dmat;
    207 
    208 	mutex_init(&hp->host_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    209 	mutex_init(&hp->intr_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    210 	cv_init(&hp->intr_cv, "sdhcintr");
    211 
    212 	/*
    213 	 * Reset the host controller and enable interrupts.
    214 	 */
    215 	(void)sdhc_host_reset(hp);
    216 
    217 	/* Determine host capabilities. */
    218 	mutex_enter(&hp->host_mtx);
    219 	caps = HREAD4(hp, SDHC_CAPABILITIES);
    220 	mutex_exit(&hp->host_mtx);
    221 
    222 #if notyet
    223 	/* Use DMA if the host system and the controller support it. */
    224 	if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA)
    225 	 || ((ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA)
    226 	   && ISSET(caps, SDHC_DMA_SUPPORT)))) {
    227 		SET(hp->flags, SHF_USE_DMA);
    228 		aprint_normal_dev(sc->sc_dev, "using DMA transfer\n");
    229 	}
    230 #endif
    231 
    232 	/*
    233 	 * Determine the base clock frequency. (2.2.24)
    234 	 */
    235 	if (SDHC_BASE_FREQ_KHZ(caps) != 0)
    236 		hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
    237 	if (hp->clkbase == 0) {
    238 		if (sc->sc_clkbase == 0) {
    239 			/* The attachment driver must tell us. */
    240 			aprint_error_dev(sc->sc_dev,"unknown base clock frequency\n");
    241 			goto err;
    242 		}
    243 		hp->clkbase = sc->sc_clkbase;
    244 	}
    245 	if (hp->clkbase < 10000 || hp->clkbase > 10000 * 256) {
    246 		/* SDHC 1.0 supports only 10-63 MHz. */
    247 		aprint_error_dev(sc->sc_dev,
    248 		    "base clock frequency out of range: %u MHz\n",
    249 		    hp->clkbase / 1000);
    250 		goto err;
    251 	}
    252 	DPRINTF(1,("%s: base clock frequency %u MHz\n",
    253 	    device_xname(sc->sc_dev), hp->clkbase / 1000));
    254 
    255 	/*
    256 	 * XXX Set the data timeout counter value according to
    257 	 * capabilities. (2.2.15)
    258 	 */
    259 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    260 
    261 	/*
    262 	 * Determine SD bus voltage levels supported by the controller.
    263 	 */
    264 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V))
    265 		SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V);
    266 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V))
    267 		SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
    268 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V))
    269 		SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
    270 
    271 	/*
    272 	 * Determine the maximum block length supported by the host
    273 	 * controller. (2.2.24)
    274 	 */
    275 	switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
    276 	case SDHC_MAX_BLK_LEN_512:
    277 		hp->maxblklen = 512;
    278 		break;
    279 
    280 	case SDHC_MAX_BLK_LEN_1024:
    281 		hp->maxblklen = 1024;
    282 		break;
    283 
    284 	case SDHC_MAX_BLK_LEN_2048:
    285 		hp->maxblklen = 2048;
    286 		break;
    287 
    288 	case SDHC_MAX_BLK_LEN_4096:
    289 		hp->maxblklen = 4096;
    290 		break;
    291 
    292 	default:
    293 		aprint_error_dev(sc->sc_dev, "max block length unknown\n");
    294 		goto err;
    295 	}
    296 	DPRINTF(1, ("%s: max block length %u byte%s\n",
    297 	    device_xname(sc->sc_dev), hp->maxblklen,
    298 	    hp->maxblklen > 1 ? "s" : ""));
    299 
    300 	/*
    301 	 * Attach the generic SD/MMC bus driver.  (The bus driver must
    302 	 * not invoke any chipset functions before it is attached.)
    303 	 */
    304 	memset(&saa, 0, sizeof(saa));
    305 	saa.saa_busname = "sdmmc";
    306 	saa.saa_sct = &sdhc_functions;
    307 	saa.saa_sch = hp;
    308 	saa.saa_dmat = hp->dmat;
    309 	saa.saa_clkmin = hp->clkbase / 256;
    310 	saa.saa_clkmax = hp->clkbase;
    311 	if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_DVS))
    312 		saa.saa_clkmin /= 16;
    313 	saa.saa_caps = SMC_CAPS_4BIT_MODE|SMC_CAPS_AUTO_STOP;
    314 #if notyet
    315 	if (ISSET(hp->flags, SHF_USE_DMA))
    316 		saa.saa_caps |= SMC_CAPS_DMA;
    317 #endif
    318 	hp->sdmmc = config_found(sc->sc_dev, &saa, NULL);
    319 
    320 	return 0;
    321 
    322 err:
    323 	cv_destroy(&hp->intr_cv);
    324 	mutex_destroy(&hp->intr_mtx);
    325 	mutex_destroy(&hp->host_mtx);
    326 	free(hp, M_DEVBUF);
    327 	sc->sc_host[--sc->sc_nhosts] = NULL;
    328 err1:
    329 	return 1;
    330 }
    331 
    332 int
    333 sdhc_detach(device_t dev, int flags)
    334 {
    335 	struct sdhc_host *hp = (struct sdhc_host *)dev;
    336 	struct sdhc_softc *sc = hp->sc;
    337 	int rv = 0;
    338 
    339 	if (hp->sdmmc)
    340 		rv = config_detach(hp->sdmmc, flags);
    341 
    342 	cv_destroy(&hp->intr_cv);
    343 	mutex_destroy(&hp->intr_mtx);
    344 	mutex_destroy(&hp->host_mtx);
    345 	free(hp, M_DEVBUF);
    346 	sc->sc_host[--sc->sc_nhosts] = NULL;
    347 
    348 	return rv;
    349 }
    350 
    351 bool
    352 sdhc_suspend(device_t dev, const pmf_qual_t *qual)
    353 {
    354 	struct sdhc_softc *sc = device_private(dev);
    355 	struct sdhc_host *hp;
    356 	int n, i;
    357 
    358 	/* XXX poll for command completion or suspend command
    359 	 * in progress */
    360 
    361 	/* Save the host controller state. */
    362 	for (n = 0; n < sc->sc_nhosts; n++) {
    363 		hp = sc->sc_host[n];
    364 		for (i = 0; i < sizeof hp->regs; i++)
    365 			hp->regs[i] = HREAD1(hp, i);
    366 	}
    367 	return true;
    368 }
    369 
    370 bool
    371 sdhc_resume(device_t dev, const pmf_qual_t *qual)
    372 {
    373 	struct sdhc_softc *sc = device_private(dev);
    374 	struct sdhc_host *hp;
    375 	int n, i;
    376 
    377 	/* Restore the host controller state. */
    378 	for (n = 0; n < sc->sc_nhosts; n++) {
    379 		hp = sc->sc_host[n];
    380 		(void)sdhc_host_reset(hp);
    381 		for (i = 0; i < sizeof hp->regs; i++)
    382 			HWRITE1(hp, i, hp->regs[i]);
    383 	}
    384 	return true;
    385 }
    386 
    387 bool
    388 sdhc_shutdown(device_t dev, int flags)
    389 {
    390 	struct sdhc_softc *sc = device_private(dev);
    391 	struct sdhc_host *hp;
    392 	int i;
    393 
    394 	/* XXX chip locks up if we don't disable it before reboot. */
    395 	for (i = 0; i < sc->sc_nhosts; i++) {
    396 		hp = sc->sc_host[i];
    397 		(void)sdhc_host_reset(hp);
    398 	}
    399 	return true;
    400 }
    401 
    402 /*
    403  * Reset the host controller.  Called during initialization, when
    404  * cards are removed, upon resume, and during error recovery.
    405  */
    406 static int
    407 sdhc_host_reset1(sdmmc_chipset_handle_t sch)
    408 {
    409 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    410 	uint16_t sdhcimask;
    411 	int error;
    412 
    413 	/* Don't lock. */
    414 
    415 	/* Disable all interrupts. */
    416 	HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
    417 
    418 	/*
    419 	 * Reset the entire host controller and wait up to 100ms for
    420 	 * the controller to clear the reset bit.
    421 	 */
    422 	error = sdhc_soft_reset(hp, SDHC_RESET_ALL);
    423 	if (error)
    424 		goto out;
    425 
    426 	/* Set data timeout counter value to max for now. */
    427 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    428 
    429 	/* Enable interrupts. */
    430 	sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
    431 	    SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
    432 	    SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
    433 	    SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
    434 	HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
    435 	HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
    436 	HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
    437 	HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
    438 
    439 out:
    440 	return error;
    441 }
    442 
    443 static int
    444 sdhc_host_reset(sdmmc_chipset_handle_t sch)
    445 {
    446 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    447 	int error;
    448 
    449 	mutex_enter(&hp->host_mtx);
    450 	error = sdhc_host_reset1(sch);
    451 	mutex_exit(&hp->host_mtx);
    452 
    453 	return error;
    454 }
    455 
    456 static uint32_t
    457 sdhc_host_ocr(sdmmc_chipset_handle_t sch)
    458 {
    459 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    460 
    461 	return hp->ocr;
    462 }
    463 
    464 static int
    465 sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
    466 {
    467 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    468 
    469 	return hp->maxblklen;
    470 }
    471 
    472 /*
    473  * Return non-zero if the card is currently inserted.
    474  */
    475 static int
    476 sdhc_card_detect(sdmmc_chipset_handle_t sch)
    477 {
    478 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    479 	int r;
    480 
    481 	mutex_enter(&hp->host_mtx);
    482 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED);
    483 	mutex_exit(&hp->host_mtx);
    484 
    485 	if (r)
    486 		return 1;
    487 	return 0;
    488 }
    489 
    490 /*
    491  * Return non-zero if the card is currently write-protected.
    492  */
    493 static int
    494 sdhc_write_protect(sdmmc_chipset_handle_t sch)
    495 {
    496 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    497 	int r;
    498 
    499 	mutex_enter(&hp->host_mtx);
    500 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH);
    501 	mutex_exit(&hp->host_mtx);
    502 
    503 	if (!r)
    504 		return 1;
    505 	return 0;
    506 }
    507 
    508 /*
    509  * Set or change SD bus voltage and enable or disable SD bus power.
    510  * Return zero on success.
    511  */
    512 static int
    513 sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    514 {
    515 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    516 	uint8_t vdd;
    517 	int error = 0;
    518 
    519 	mutex_enter(&hp->host_mtx);
    520 
    521 	/*
    522 	 * Disable bus power before voltage change.
    523 	 */
    524 	if (!(hp->sc->sc_flags & SDHC_FLAG_NO_PWR0))
    525 		HWRITE1(hp, SDHC_POWER_CTL, 0);
    526 
    527 	/* If power is disabled, reset the host and return now. */
    528 	if (ocr == 0) {
    529 		(void)sdhc_host_reset1(hp);
    530 		goto out;
    531 	}
    532 
    533 	/*
    534 	 * Select the lowest voltage according to capabilities.
    535 	 */
    536 	ocr &= hp->ocr;
    537 	if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V))
    538 		vdd = SDHC_VOLTAGE_1_8V;
    539 	else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V))
    540 		vdd = SDHC_VOLTAGE_3_0V;
    541 	else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V))
    542 		vdd = SDHC_VOLTAGE_3_3V;
    543 	else {
    544 		/* Unsupported voltage level requested. */
    545 		error = EINVAL;
    546 		goto out;
    547 	}
    548 
    549 	/*
    550 	 * Enable bus power.  Wait at least 1 ms (or 74 clocks) plus
    551 	 * voltage ramp until power rises.
    552 	 */
    553 	HWRITE1(hp, SDHC_POWER_CTL,
    554 	    (vdd << SDHC_VOLTAGE_SHIFT) | SDHC_BUS_POWER);
    555 	sdmmc_delay(10000);
    556 
    557 	/*
    558 	 * The host system may not power the bus due to battery low,
    559 	 * etc.  In that case, the host controller should clear the
    560 	 * bus power bit.
    561 	 */
    562 	if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
    563 		error = ENXIO;
    564 		goto out;
    565 	}
    566 
    567 out:
    568 	mutex_exit(&hp->host_mtx);
    569 
    570 	return error;
    571 }
    572 
    573 /*
    574  * Return the smallest possible base clock frequency divisor value
    575  * for the CLOCK_CTL register to produce `freq' (KHz).
    576  */
    577 static int
    578 sdhc_clock_divisor(struct sdhc_host *hp, u_int freq)
    579 {
    580 	int div;
    581 
    582 	if (hp->sc->sc_flags & SDHC_FLAG_HAVE_DVS) {
    583 		int dvs = (hp->clkbase + freq - 1) / freq;
    584 		div = 1;
    585 		for (div = 1; div <= 256; div <<= 1, dvs >>= 1) {
    586 			if (dvs <= 16) {
    587 				div <<= SDHC_SDCLK_DIV_SHIFT;
    588 				div |= (dvs - 1) << SDHC_SDCLK_DVS_SHIFT;
    589 				return div;
    590 			}
    591 		}
    592 	} else {
    593 		for (div = 1; div <= 256; div *= 2) {
    594 			if ((hp->clkbase / div) <= freq)
    595 				return (div / 2) << SDHC_SDCLK_DIV_SHIFT;
    596 		}
    597 	}
    598 
    599 	/* No divisor found. */
    600 	return -1;
    601 }
    602 
    603 /*
    604  * Set or change SDCLK frequency or disable the SD clock.
    605  * Return zero on success.
    606  */
    607 static int
    608 sdhc_bus_clock(sdmmc_chipset_handle_t sch, int freq)
    609 {
    610 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    611 	int div;
    612 	int timo;
    613 	int error = 0;
    614 #ifdef DIAGNOSTIC
    615 	int ispresent;
    616 #endif
    617 
    618 #ifdef DIAGNOSTIC
    619 	mutex_enter(&hp->host_mtx);
    620 	ispresent = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK);
    621 	mutex_exit(&hp->host_mtx);
    622 
    623 	/* Must not stop the clock if commands are in progress. */
    624 	if (ispresent && sdhc_card_detect(hp))
    625 		printf("%s: sdhc_sdclk_frequency_select: command in progress\n",
    626 		    device_xname(hp->sc->sc_dev));
    627 #endif
    628 
    629 	mutex_enter(&hp->host_mtx);
    630 
    631 	/*
    632 	 * Stop SD clock before changing the frequency.
    633 	 */
    634 	HWRITE2(hp, SDHC_CLOCK_CTL, 0);
    635 	if (freq == SDMMC_SDCLK_OFF)
    636 		goto out;
    637 
    638 	/*
    639 	 * Set the minimum base clock frequency divisor.
    640 	 */
    641 	if ((div = sdhc_clock_divisor(hp, freq)) < 0) {
    642 		/* Invalid base clock frequency or `freq' value. */
    643 		error = EINVAL;
    644 		goto out;
    645 	}
    646 	HWRITE2(hp, SDHC_CLOCK_CTL, div);
    647 
    648 	/*
    649 	 * Start internal clock.  Wait 10ms for stabilization.
    650 	 */
    651 	HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
    652 	for (timo = 1000; timo > 0; timo--) {
    653 		if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL), SDHC_INTCLK_STABLE))
    654 			break;
    655 		sdmmc_delay(10);
    656 	}
    657 	if (timo == 0) {
    658 		error = ETIMEDOUT;
    659 		goto out;
    660 	}
    661 
    662 	/*
    663 	 * Enable SD clock.
    664 	 */
    665 	HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
    666 
    667 	if (freq > 25000)
    668 		HSET1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
    669 	else
    670 		HCLR1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
    671 
    672 out:
    673 	mutex_exit(&hp->host_mtx);
    674 
    675 	return error;
    676 }
    677 
    678 static int
    679 sdhc_bus_width(sdmmc_chipset_handle_t sch, int width)
    680 {
    681 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    682 	int reg;
    683 
    684 	switch (width) {
    685 	case 1:
    686 	case 4:
    687 		break;
    688 
    689 	default:
    690 		DPRINTF(0,("%s: unsupported bus width (%d)\n",
    691 		    HDEVNAME(hp), width));
    692 		return 1;
    693 	}
    694 
    695 	mutex_enter(&hp->host_mtx);
    696 	reg = HREAD1(hp, SDHC_HOST_CTL);
    697 	reg &= ~SDHC_4BIT_MODE;
    698 	if (width == 4)
    699 		reg |= SDHC_4BIT_MODE;
    700 	HWRITE1(hp, SDHC_HOST_CTL, reg);
    701 	mutex_exit(&hp->host_mtx);
    702 
    703 	return 0;
    704 }
    705 
    706 static int
    707 sdhc_bus_rod(sdmmc_chipset_handle_t sch, int on)
    708 {
    709 
    710 	/* Nothing ?? */
    711 	return 0;
    712 }
    713 
    714 static void
    715 sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
    716 {
    717 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    718 
    719 	mutex_enter(&hp->host_mtx);
    720 	if (enable) {
    721 		HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
    722 		HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
    723 	} else {
    724 		HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
    725 		HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
    726 	}
    727 	mutex_exit(&hp->host_mtx);
    728 }
    729 
    730 static void
    731 sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
    732 {
    733 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    734 
    735 	mutex_enter(&hp->host_mtx);
    736 	HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
    737 	mutex_exit(&hp->host_mtx);
    738 }
    739 
    740 static int
    741 sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value)
    742 {
    743 	uint32_t state;
    744 	int timeout;
    745 
    746 	for (timeout = 10; timeout > 0; timeout--) {
    747 		if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value)
    748 			return 0;
    749 		sdmmc_delay(10000);
    750 	}
    751 	DPRINTF(0,("%s: timeout waiting for %x (state=%x)\n", HDEVNAME(hp),
    752 	    value, state));
    753 	return ETIMEDOUT;
    754 }
    755 
    756 static void
    757 sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
    758 {
    759 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    760 	int error;
    761 
    762 	/*
    763 	 * Start the MMC command, or mark `cmd' as failed and return.
    764 	 */
    765 	error = sdhc_start_command(hp, cmd);
    766 	if (error) {
    767 		cmd->c_error = error;
    768 		goto out;
    769 	}
    770 
    771 	/*
    772 	 * Wait until the command phase is done, or until the command
    773 	 * is marked done for any other reason.
    774 	 */
    775 	if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT)) {
    776 		cmd->c_error = ETIMEDOUT;
    777 		goto out;
    778 	}
    779 
    780 	/*
    781 	 * The host controller removes bits [0:7] from the response
    782 	 * data (CRC) and we pass the data up unchanged to the bus
    783 	 * driver (without padding).
    784 	 */
    785 	mutex_enter(&hp->host_mtx);
    786 	if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
    787 		if (ISSET(cmd->c_flags, SCF_RSP_136)) {
    788 			uint8_t *p = (uint8_t *)cmd->c_resp;
    789 			int i;
    790 
    791 			for (i = 0; i < 15; i++)
    792 				*p++ = HREAD1(hp, SDHC_RESPONSE + i);
    793 		} else {
    794 			cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE);
    795 		}
    796 	}
    797 	mutex_exit(&hp->host_mtx);
    798 	DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0]));
    799 
    800 	/*
    801 	 * If the command has data to transfer in any direction,
    802 	 * execute the transfer now.
    803 	 */
    804 	if (cmd->c_error == 0 && cmd->c_data != NULL)
    805 		sdhc_transfer_data(hp, cmd);
    806 
    807 out:
    808 	mutex_enter(&hp->host_mtx);
    809 	/* Turn off the LED. */
    810 	HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
    811 	mutex_exit(&hp->host_mtx);
    812 	SET(cmd->c_flags, SCF_ITSDONE);
    813 
    814 	DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp),
    815 	    cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort",
    816 	    cmd->c_flags, cmd->c_error));
    817 }
    818 
    819 static int
    820 sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
    821 {
    822 	uint16_t blksize = 0;
    823 	uint16_t blkcount = 0;
    824 	uint16_t mode;
    825 	uint16_t command;
    826 	int error;
    827 
    828 	DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x\n",
    829 	    HDEVNAME(hp), cmd->c_opcode, cmd->c_arg, cmd->c_data,
    830 	    cmd->c_datalen, cmd->c_flags));
    831 
    832 	/*
    833 	 * The maximum block length for commands should be the minimum
    834 	 * of the host buffer size and the card buffer size. (1.7.2)
    835 	 */
    836 
    837 	/* Fragment the data into proper blocks. */
    838 	if (cmd->c_datalen > 0) {
    839 		blksize = MIN(cmd->c_datalen, cmd->c_blklen);
    840 		blkcount = cmd->c_datalen / blksize;
    841 		if (cmd->c_datalen % blksize > 0) {
    842 			/* XXX: Split this command. (1.7.4) */
    843 			aprint_error_dev(hp->sc->sc_dev,
    844 			    "data not a multiple of %u bytes\n", blksize);
    845 			return EINVAL;
    846 		}
    847 	}
    848 
    849 	/* Check limit imposed by 9-bit block count. (1.7.2) */
    850 	if (blkcount > SDHC_BLOCK_COUNT_MAX) {
    851 		aprint_error_dev(hp->sc->sc_dev, "too much data\n");
    852 		return EINVAL;
    853 	}
    854 
    855 	/* Prepare transfer mode register value. (2.2.5) */
    856 	mode = 0;
    857 	if (ISSET(cmd->c_flags, SCF_CMD_READ))
    858 		mode |= SDHC_READ_MODE;
    859 	if (blkcount > 0) {
    860 		mode |= SDHC_BLOCK_COUNT_ENABLE;
    861 		if (blkcount > 1) {
    862 			mode |= SDHC_MULTI_BLOCK_MODE;
    863 			/* XXX only for memory commands? */
    864 			mode |= SDHC_AUTO_CMD12_ENABLE;
    865 		}
    866 	}
    867 	if (cmd->c_dmamap != NULL && cmd->c_datalen > 0) {
    868 		if (cmd->c_dmamap->dm_nsegs == 1) {
    869 			mode |= SDHC_DMA_ENABLE;
    870 		} else {
    871 			cmd->c_dmamap = NULL;
    872 		}
    873 	}
    874 
    875 	/*
    876 	 * Prepare command register value. (2.2.6)
    877 	 */
    878 	command =
    879 	 (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT;
    880 
    881 	if (ISSET(cmd->c_flags, SCF_RSP_CRC))
    882 		command |= SDHC_CRC_CHECK_ENABLE;
    883 	if (ISSET(cmd->c_flags, SCF_RSP_IDX))
    884 		command |= SDHC_INDEX_CHECK_ENABLE;
    885 	if (cmd->c_data != NULL)
    886 		command |= SDHC_DATA_PRESENT_SELECT;
    887 
    888 	if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
    889 		command |= SDHC_NO_RESPONSE;
    890 	else if (ISSET(cmd->c_flags, SCF_RSP_136))
    891 		command |= SDHC_RESP_LEN_136;
    892 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
    893 		command |= SDHC_RESP_LEN_48_CHK_BUSY;
    894 	else
    895 		command |= SDHC_RESP_LEN_48;
    896 
    897 	/* Wait until command and data inhibit bits are clear. (1.5) */
    898 	error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0);
    899 	if (error)
    900 		return error;
    901 
    902 	DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n",
    903 	    HDEVNAME(hp), blksize, blkcount, mode, command));
    904 
    905 	mutex_enter(&hp->host_mtx);
    906 
    907 	/* Alert the user not to remove the card. */
    908 	HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
    909 
    910 	/* Set DMA start address. */
    911 	if (ISSET(mode, SDHC_DMA_ENABLE))
    912 		HWRITE4(hp, SDHC_DMA_ADDR, cmd->c_dmamap->dm_segs[0].ds_addr);
    913 
    914 	/*
    915 	 * Start a CPU data transfer.  Writing to the high order byte
    916 	 * of the SDHC_COMMAND register triggers the SD command. (1.5)
    917 	 */
    918 	HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
    919 	HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
    920 	if (blkcount > 1)
    921 		HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
    922 	HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
    923 	HWRITE2(hp, SDHC_COMMAND, command);
    924 
    925 	mutex_exit(&hp->host_mtx);
    926 
    927 	return 0;
    928 }
    929 
    930 static void
    931 sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
    932 {
    933 	int error;
    934 
    935 	DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp),
    936 	    MMC_R1(cmd->c_resp), cmd->c_datalen));
    937 
    938 #ifdef SDHC_DEBUG
    939 	/* XXX I forgot why I wanted to know when this happens :-( */
    940 	if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
    941 	    ISSET(MMC_R1(cmd->c_resp), 0xcb00)) {
    942 		aprint_error_dev(hp->sc->sc_dev,
    943 		    "CMD52/53 error response flags %#x\n",
    944 		    MMC_R1(cmd->c_resp) & 0xff00);
    945 	}
    946 #endif
    947 
    948 	if (cmd->c_dmamap != NULL)
    949 		error = sdhc_transfer_data_dma(hp, cmd);
    950 	else
    951 		error = sdhc_transfer_data_pio(hp, cmd);
    952 	if (error)
    953 		cmd->c_error = error;
    954 	SET(cmd->c_flags, SCF_ITSDONE);
    955 
    956 	DPRINTF(1,("%s: data transfer done (error=%d)\n",
    957 	    HDEVNAME(hp), cmd->c_error));
    958 }
    959 
    960 static int
    961 sdhc_transfer_data_dma(struct sdhc_host *hp, struct sdmmc_command *cmd)
    962 {
    963 	bus_dmamap_t dmap = cmd->c_dmamap;
    964 	uint16_t blklen = cmd->c_blklen;
    965 	uint16_t blkcnt = cmd->c_datalen / blklen;
    966 	uint16_t remain;
    967 	int error = 0;
    968 
    969 	for (;;) {
    970 		if (!sdhc_wait_intr(hp,
    971 		    SDHC_DMA_INTERRUPT|SDHC_TRANSFER_COMPLETE,
    972 		    SDHC_DMA_TIMEOUT)) {
    973 			error = ETIMEDOUT;
    974 			break;
    975 		}
    976 
    977 		/* single block mode */
    978 		if (blkcnt == 1)
    979 			break;
    980 
    981 		/* multi block mode */
    982 		remain = HREAD2(hp, SDHC_BLOCK_COUNT);
    983 		if (remain == 0)
    984 			break;
    985 
    986 		HWRITE4(hp, SDHC_DMA_ADDR,
    987 		    dmap->dm_segs[0].ds_addr + (blkcnt - remain) * blklen);
    988 	}
    989 
    990 #if 0
    991 	if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
    992 	    SDHC_TRANSFER_TIMEOUT))
    993 		error = ETIMEDOUT;
    994 #endif
    995 
    996 	return error;
    997 }
    998 
    999 static int
   1000 sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1001 {
   1002 	uint8_t *data = cmd->c_data;
   1003 	int len, datalen;
   1004 	int mask;
   1005 	int error = 0;
   1006 
   1007 	mask = ISSET(cmd->c_flags, SCF_CMD_READ) ?
   1008 	    SDHC_BUFFER_READ_ENABLE : SDHC_BUFFER_WRITE_ENABLE;
   1009 	datalen = cmd->c_datalen;
   1010 
   1011 	while (datalen > 0) {
   1012 		if (!sdhc_wait_intr(hp,
   1013 		    SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY,
   1014 		    SDHC_BUFFER_TIMEOUT)) {
   1015 			error = ETIMEDOUT;
   1016 			break;
   1017 		}
   1018 
   1019 		error = sdhc_wait_state(hp, mask, mask);
   1020 		if (error)
   1021 			break;
   1022 
   1023 		len = MIN(datalen, cmd->c_blklen);
   1024 		if (ISSET(cmd->c_flags, SCF_CMD_READ))
   1025 			sdhc_read_data_pio(hp, data, len);
   1026 		else
   1027 			sdhc_write_data_pio(hp, data, len);
   1028 
   1029 		data += len;
   1030 		datalen -= len;
   1031 	}
   1032 
   1033 	if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
   1034 	    SDHC_TRANSFER_TIMEOUT))
   1035 		error = ETIMEDOUT;
   1036 
   1037 	return error;
   1038 }
   1039 
   1040 static void
   1041 sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, int datalen)
   1042 {
   1043 
   1044 	if (((__uintptr_t)data & 3) == 0) {
   1045 		while (datalen > 3) {
   1046 			*(uint32_t *)data = HREAD4(hp, SDHC_DATA);
   1047 			data += 4;
   1048 			datalen -= 4;
   1049 		}
   1050 		if (datalen > 1) {
   1051 			*(uint16_t *)data = HREAD2(hp, SDHC_DATA);
   1052 			data += 2;
   1053 			datalen -= 2;
   1054 		}
   1055 		if (datalen > 0) {
   1056 			*data = HREAD1(hp, SDHC_DATA);
   1057 			data += 1;
   1058 			datalen -= 1;
   1059 		}
   1060 	} else if (((__uintptr_t)data & 1) == 0) {
   1061 		while (datalen > 1) {
   1062 			*(uint16_t *)data = HREAD2(hp, SDHC_DATA);
   1063 			data += 2;
   1064 			datalen -= 2;
   1065 		}
   1066 		if (datalen > 0) {
   1067 			*data = HREAD1(hp, SDHC_DATA);
   1068 			data += 1;
   1069 			datalen -= 1;
   1070 		}
   1071 	} else {
   1072 		while (datalen > 0) {
   1073 			*data = HREAD1(hp, SDHC_DATA);
   1074 			data += 1;
   1075 			datalen -= 1;
   1076 		}
   1077 	}
   1078 }
   1079 
   1080 static void
   1081 sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, int datalen)
   1082 {
   1083 
   1084 	if (((__uintptr_t)data & 3) == 0) {
   1085 		while (datalen > 3) {
   1086 			HWRITE4(hp, SDHC_DATA, *(uint32_t *)data);
   1087 			data += 4;
   1088 			datalen -= 4;
   1089 		}
   1090 		if (datalen > 1) {
   1091 			HWRITE2(hp, SDHC_DATA, *(uint16_t *)data);
   1092 			data += 2;
   1093 			datalen -= 2;
   1094 		}
   1095 		if (datalen > 0) {
   1096 			HWRITE1(hp, SDHC_DATA, *data);
   1097 			data += 1;
   1098 			datalen -= 1;
   1099 		}
   1100 	} else if (((__uintptr_t)data & 1) == 0) {
   1101 		while (datalen > 1) {
   1102 			HWRITE2(hp, SDHC_DATA, *(uint16_t *)data);
   1103 			data += 2;
   1104 			datalen -= 2;
   1105 		}
   1106 		if (datalen > 0) {
   1107 			HWRITE1(hp, SDHC_DATA, *data);
   1108 			data += 1;
   1109 			datalen -= 1;
   1110 		}
   1111 	} else {
   1112 		while (datalen > 0) {
   1113 			HWRITE1(hp, SDHC_DATA, *data);
   1114 			data += 1;
   1115 			datalen -= 1;
   1116 		}
   1117 	}
   1118 }
   1119 
   1120 /* Prepare for another command. */
   1121 static int
   1122 sdhc_soft_reset(struct sdhc_host *hp, int mask)
   1123 {
   1124 	int timo;
   1125 
   1126 	DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask));
   1127 
   1128 	HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
   1129 	for (timo = 10; timo > 0; timo--) {
   1130 		if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
   1131 			break;
   1132 		sdmmc_delay(10000);
   1133 		HWRITE1(hp, SDHC_SOFTWARE_RESET, 0);
   1134 	}
   1135 	if (timo == 0) {
   1136 		DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp),
   1137 		    HREAD1(hp, SDHC_SOFTWARE_RESET)));
   1138 		HWRITE1(hp, SDHC_SOFTWARE_RESET, 0);
   1139 		return ETIMEDOUT;
   1140 	}
   1141 
   1142 	return 0;
   1143 }
   1144 
   1145 static int
   1146 sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo)
   1147 {
   1148 	int status;
   1149 
   1150 	mask |= SDHC_ERROR_INTERRUPT;
   1151 
   1152 	mutex_enter(&hp->intr_mtx);
   1153 	status = hp->intr_status & mask;
   1154 	while (status == 0) {
   1155 		if (cv_timedwait(&hp->intr_cv, &hp->intr_mtx, timo)
   1156 		    == EWOULDBLOCK) {
   1157 			status |= SDHC_ERROR_INTERRUPT;
   1158 			break;
   1159 		}
   1160 		status = hp->intr_status & mask;
   1161 	}
   1162 	hp->intr_status &= ~status;
   1163 
   1164 	DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status,
   1165 	    hp->intr_error_status));
   1166 
   1167 	/* Command timeout has higher priority than command complete. */
   1168 	if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
   1169 		hp->intr_error_status = 0;
   1170 		(void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
   1171 		status = 0;
   1172 	}
   1173 	mutex_exit(&hp->intr_mtx);
   1174 
   1175 	return status;
   1176 }
   1177 
   1178 /*
   1179  * Established by attachment driver at interrupt priority IPL_SDMMC.
   1180  */
   1181 int
   1182 sdhc_intr(void *arg)
   1183 {
   1184 	struct sdhc_softc *sc = (struct sdhc_softc *)arg;
   1185 	struct sdhc_host *hp;
   1186 	int host;
   1187 	int done = 0;
   1188 	uint16_t status;
   1189 	uint16_t error;
   1190 
   1191 	/* We got an interrupt, but we don't know from which slot. */
   1192 	for (host = 0; host < sc->sc_nhosts; host++) {
   1193 		hp = sc->sc_host[host];
   1194 		if (hp == NULL)
   1195 			continue;
   1196 
   1197 		/* Find out which interrupts are pending. */
   1198 		status = HREAD2(hp, SDHC_NINTR_STATUS);
   1199 		if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   1200 			continue; /* no interrupt for us */
   1201 
   1202 		/* Acknowledge the interrupts we are about to handle. */
   1203 		HWRITE2(hp, SDHC_NINTR_STATUS, status);
   1204 		DPRINTF(2,("%s: interrupt status=%x\n", HDEVNAME(hp),
   1205 		    status));
   1206 
   1207 		if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   1208 			continue;
   1209 
   1210 		/* Claim this interrupt. */
   1211 		done = 1;
   1212 
   1213 		/*
   1214 		 * Service error interrupts.
   1215 		 */
   1216 		if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
   1217 			/* Acknowledge error interrupts. */
   1218 			error = HREAD2(hp, SDHC_EINTR_STATUS);
   1219 			HWRITE2(hp, SDHC_EINTR_STATUS, error);
   1220 			DPRINTF(2,("%s: error interrupt, status=%x\n",
   1221 			    HDEVNAME(hp), error));
   1222 
   1223 			if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR|
   1224 			    SDHC_DATA_TIMEOUT_ERROR)) {
   1225 				hp->intr_error_status |= error;
   1226 				hp->intr_status |= status;
   1227 				cv_broadcast(&hp->intr_cv);
   1228 			}
   1229 		}
   1230 
   1231 		/*
   1232 		 * Wake up the sdmmc event thread to scan for cards.
   1233 		 */
   1234 		if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)) {
   1235 			sdmmc_needs_discover(hp->sdmmc);
   1236 #if 0
   1237 			HCLR2(hp, SDHC_NINTR_STATUS_EN,
   1238 			    status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
   1239 #endif
   1240 		}
   1241 
   1242 		/*
   1243 		 * Wake up the blocking process to service command
   1244 		 * related interrupt(s).
   1245 		 */
   1246 		if (ISSET(status, SDHC_BUFFER_READ_READY|
   1247 		    SDHC_BUFFER_WRITE_READY|SDHC_COMMAND_COMPLETE|
   1248 		    SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) {
   1249 			hp->intr_status |= status;
   1250 			cv_broadcast(&hp->intr_cv);
   1251 		}
   1252 
   1253 		/*
   1254 		 * Service SD card interrupts.
   1255 		 */
   1256 		if (ISSET(status, SDHC_CARD_INTERRUPT)) {
   1257 			DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp)));
   1258 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1259 			sdmmc_card_intr(hp->sdmmc);
   1260 		}
   1261 	}
   1262 
   1263 	return done;
   1264 }
   1265 
   1266 #ifdef SDHC_DEBUG
   1267 void
   1268 sdhc_dump_regs(struct sdhc_host *hp)
   1269 {
   1270 
   1271 	printf("0x%02x PRESENT_STATE:    %x\n", SDHC_PRESENT_STATE,
   1272 	    HREAD4(hp, SDHC_PRESENT_STATE));
   1273 	printf("0x%02x POWER_CTL:        %x\n", SDHC_POWER_CTL,
   1274 	    HREAD1(hp, SDHC_POWER_CTL));
   1275 	printf("0x%02x NINTR_STATUS:     %x\n", SDHC_NINTR_STATUS,
   1276 	    HREAD2(hp, SDHC_NINTR_STATUS));
   1277 	printf("0x%02x EINTR_STATUS:     %x\n", SDHC_EINTR_STATUS,
   1278 	    HREAD2(hp, SDHC_EINTR_STATUS));
   1279 	printf("0x%02x NINTR_STATUS_EN:  %x\n", SDHC_NINTR_STATUS_EN,
   1280 	    HREAD2(hp, SDHC_NINTR_STATUS_EN));
   1281 	printf("0x%02x EINTR_STATUS_EN:  %x\n", SDHC_EINTR_STATUS_EN,
   1282 	    HREAD2(hp, SDHC_EINTR_STATUS_EN));
   1283 	printf("0x%02x NINTR_SIGNAL_EN:  %x\n", SDHC_NINTR_SIGNAL_EN,
   1284 	    HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
   1285 	printf("0x%02x EINTR_SIGNAL_EN:  %x\n", SDHC_EINTR_SIGNAL_EN,
   1286 	    HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
   1287 	printf("0x%02x CAPABILITIES:     %x\n", SDHC_CAPABILITIES,
   1288 	    HREAD4(hp, SDHC_CAPABILITIES));
   1289 	printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
   1290 	    HREAD4(hp, SDHC_MAX_CAPABILITIES));
   1291 }
   1292 #endif
   1293