Home | History | Annotate | Line # | Download | only in sdmmc
sdhc.c revision 1.54
      1 /*	$NetBSD: sdhc.c,v 1.54 2015/02/27 15:53:09 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.54 2015/02/27 15:53:09 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_size_t ios;			/* host register space size */
     66 	bus_dma_tag_t dmat;		/* host DMA tag */
     67 
     68 	device_t sdmmc;			/* generic SD/MMC device */
     69 
     70 	struct kmutex host_mtx;
     71 
     72 	u_int clkbase;			/* base clock frequency in KHz */
     73 	int maxblklen;			/* maximum block length */
     74 	uint32_t ocr;			/* OCR value from capabilities */
     75 
     76 	uint8_t regs[14];		/* host controller state */
     77 
     78 	uint16_t intr_status;		/* soft interrupt status */
     79 	uint16_t intr_error_status;	/* soft error status */
     80 	struct kmutex intr_mtx;
     81 	struct kcondvar intr_cv;
     82 
     83 	int specver;			/* spec. version */
     84 
     85 	uint32_t flags;			/* flags for this host */
     86 #define SHF_USE_DMA		0x0001
     87 #define SHF_USE_4BIT_MODE	0x0002
     88 #define SHF_USE_8BIT_MODE	0x0004
     89 };
     90 
     91 #define HDEVNAME(hp)	(device_xname((hp)->sc->sc_dev))
     92 
     93 static uint8_t
     94 hread1(struct sdhc_host *hp, bus_size_t reg)
     95 {
     96 
     97 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
     98 		return bus_space_read_1(hp->iot, hp->ioh, reg);
     99 	return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 3));
    100 }
    101 
    102 static uint16_t
    103 hread2(struct sdhc_host *hp, bus_size_t reg)
    104 {
    105 
    106 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
    107 		return bus_space_read_2(hp->iot, hp->ioh, reg);
    108 	return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 2));
    109 }
    110 
    111 #define HREAD1(hp, reg)		hread1(hp, reg)
    112 #define HREAD2(hp, reg)		hread2(hp, reg)
    113 #define HREAD4(hp, reg)		\
    114 	(bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
    115 
    116 
    117 static void
    118 hwrite1(struct sdhc_host *hp, bus_size_t o, uint8_t val)
    119 {
    120 
    121 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    122 		bus_space_write_1(hp->iot, hp->ioh, o, val);
    123 	} else {
    124 		const size_t shift = 8 * (o & 3);
    125 		o &= -4;
    126 		uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
    127 		tmp = (val << shift) | (tmp & ~(0xff << shift));
    128 		bus_space_write_4(hp->iot, hp->ioh, o, tmp);
    129 	}
    130 }
    131 
    132 static void
    133 hwrite2(struct sdhc_host *hp, bus_size_t o, uint16_t val)
    134 {
    135 
    136 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    137 		bus_space_write_2(hp->iot, hp->ioh, o, val);
    138 	} else {
    139 		const size_t shift = 8 * (o & 2);
    140 		o &= -4;
    141 		uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
    142 		tmp = (val << shift) | (tmp & ~(0xffff << shift));
    143 		bus_space_write_4(hp->iot, hp->ioh, o, tmp);
    144 	}
    145 }
    146 
    147 #define HWRITE1(hp, reg, val)		hwrite1(hp, reg, val)
    148 #define HWRITE2(hp, reg, val)		hwrite2(hp, reg, val)
    149 #define HWRITE4(hp, reg, val)						\
    150 	bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val))
    151 
    152 #define HCLR1(hp, reg, bits)						\
    153 	do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits)); while (0)
    154 #define HCLR2(hp, reg, bits)						\
    155 	do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits)); while (0)
    156 #define HCLR4(hp, reg, bits)						\
    157 	do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) & ~(bits)); while (0)
    158 #define HSET1(hp, reg, bits)						\
    159 	do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits)); while (0)
    160 #define HSET2(hp, reg, bits)						\
    161 	do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits)); while (0)
    162 #define HSET4(hp, reg, bits)						\
    163 	do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) | (bits)); while (0)
    164 
    165 static int	sdhc_host_reset(sdmmc_chipset_handle_t);
    166 static int	sdhc_host_reset1(sdmmc_chipset_handle_t);
    167 static uint32_t	sdhc_host_ocr(sdmmc_chipset_handle_t);
    168 static int	sdhc_host_maxblklen(sdmmc_chipset_handle_t);
    169 static int	sdhc_card_detect(sdmmc_chipset_handle_t);
    170 static int	sdhc_write_protect(sdmmc_chipset_handle_t);
    171 static int	sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t);
    172 static int	sdhc_bus_clock(sdmmc_chipset_handle_t, int);
    173 static int	sdhc_bus_width(sdmmc_chipset_handle_t, int);
    174 static int	sdhc_bus_rod(sdmmc_chipset_handle_t, int);
    175 static void	sdhc_card_enable_intr(sdmmc_chipset_handle_t, int);
    176 static void	sdhc_card_intr_ack(sdmmc_chipset_handle_t);
    177 static void	sdhc_exec_command(sdmmc_chipset_handle_t,
    178 		    struct sdmmc_command *);
    179 static int	sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
    180 static int	sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t);
    181 static int	sdhc_soft_reset(struct sdhc_host *, int);
    182 static int	sdhc_wait_intr(struct sdhc_host *, int, int);
    183 static void	sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
    184 static int	sdhc_transfer_data_dma(struct sdhc_host *, struct sdmmc_command *);
    185 static int	sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *);
    186 static void	sdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
    187 static void	sdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
    188 static void	esdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
    189 static void	esdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
    190 
    191 
    192 static struct sdmmc_chip_functions sdhc_functions = {
    193 	/* host controller reset */
    194 	sdhc_host_reset,
    195 
    196 	/* host controller capabilities */
    197 	sdhc_host_ocr,
    198 	sdhc_host_maxblklen,
    199 
    200 	/* card detection */
    201 	sdhc_card_detect,
    202 
    203 	/* write protect */
    204 	sdhc_write_protect,
    205 
    206 	/* bus power, clock frequency and width */
    207 	sdhc_bus_power,
    208 	sdhc_bus_clock,
    209 	sdhc_bus_width,
    210 	sdhc_bus_rod,
    211 
    212 	/* command execution */
    213 	sdhc_exec_command,
    214 
    215 	/* card interrupt */
    216 	sdhc_card_enable_intr,
    217 	sdhc_card_intr_ack
    218 };
    219 
    220 static int
    221 sdhc_cfprint(void *aux, const char *pnp)
    222 {
    223 	const struct sdmmcbus_attach_args * const saa = aux;
    224 	const struct sdhc_host * const hp = saa->saa_sch;
    225 
    226 	if (pnp) {
    227 		aprint_normal("sdmmc at %s", pnp);
    228 	}
    229 	for (size_t host = 0; host < hp->sc->sc_nhosts; host++) {
    230 		if (hp->sc->sc_host[host] == hp) {
    231 			aprint_normal(" slot %zu", host);
    232 		}
    233 	}
    234 
    235 	return UNCONF;
    236 }
    237 
    238 /*
    239  * Called by attachment driver.  For each SD card slot there is one SD
    240  * host controller standard register set. (1.3)
    241  */
    242 int
    243 sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
    244     bus_space_handle_t ioh, bus_size_t iosize)
    245 {
    246 	struct sdmmcbus_attach_args saa;
    247 	struct sdhc_host *hp;
    248 	uint32_t caps;
    249 	uint16_t sdhcver;
    250 
    251 	/* Allocate one more host structure. */
    252 	hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO);
    253 	if (hp == NULL) {
    254 		aprint_error_dev(sc->sc_dev,
    255 		    "couldn't alloc memory (sdhc host)\n");
    256 		goto err1;
    257 	}
    258 	sc->sc_host[sc->sc_nhosts++] = hp;
    259 
    260 	/* Fill in the new host structure. */
    261 	hp->sc = sc;
    262 	hp->iot = iot;
    263 	hp->ioh = ioh;
    264 	hp->ios = iosize;
    265 	hp->dmat = sc->sc_dmat;
    266 
    267 	mutex_init(&hp->host_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    268 	mutex_init(&hp->intr_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    269 	cv_init(&hp->intr_cv, "sdhcintr");
    270 
    271 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    272 		sdhcver = HREAD4(hp, SDHC_ESDHC_HOST_CTL_VERSION);
    273 	} else {
    274 		sdhcver = HREAD2(hp, SDHC_HOST_CTL_VERSION);
    275 	}
    276 	aprint_normal_dev(sc->sc_dev, "SD Host Specification ");
    277 	hp->specver = SDHC_SPEC_VERSION(sdhcver);
    278 	switch (SDHC_SPEC_VERSION(sdhcver)) {
    279 	case SDHC_SPEC_VERS_100:
    280 		aprint_normal("1.0");
    281 		break;
    282 
    283 	case SDHC_SPEC_VERS_200:
    284 		aprint_normal("2.0");
    285 		break;
    286 
    287 	case SDHC_SPEC_VERS_300:
    288 		aprint_normal("3.0");
    289 		break;
    290 
    291 	default:
    292 		aprint_normal("unknown version(0x%x)",
    293 		    SDHC_SPEC_VERSION(sdhcver));
    294 		break;
    295 	}
    296 	aprint_normal(", rev.%u\n", SDHC_VENDOR_VERSION(sdhcver));
    297 
    298 	/*
    299 	 * Reset the host controller and enable interrupts.
    300 	 */
    301 	(void)sdhc_host_reset(hp);
    302 
    303 	/* Determine host capabilities. */
    304 	if (ISSET(sc->sc_flags, SDHC_FLAG_HOSTCAPS)) {
    305 		caps = sc->sc_caps;
    306 	} else {
    307 		mutex_enter(&hp->host_mtx);
    308 		caps = HREAD4(hp, SDHC_CAPABILITIES);
    309 		mutex_exit(&hp->host_mtx);
    310 	}
    311 
    312 	/* Use DMA if the host system and the controller support it. */
    313 	if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA) ||
    314 	    (ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA &&
    315 	     ISSET(caps, SDHC_DMA_SUPPORT)))) {
    316 		SET(hp->flags, SHF_USE_DMA);
    317 		aprint_normal_dev(sc->sc_dev, "using DMA transfer\n");
    318 	}
    319 
    320 	/*
    321 	 * Determine the base clock frequency. (2.2.24)
    322 	 */
    323 	if (hp->specver == SDHC_SPEC_VERS_300) {
    324 		hp->clkbase = SDHC_BASE_V3_FREQ_KHZ(caps);
    325 	} else {
    326 		hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
    327 	}
    328 	if (hp->clkbase == 0) {
    329 		if (sc->sc_clkbase == 0) {
    330 			/* The attachment driver must tell us. */
    331 			aprint_error_dev(sc->sc_dev,
    332 			    "unknown base clock frequency\n");
    333 			goto err;
    334 		}
    335 		hp->clkbase = sc->sc_clkbase;
    336 	}
    337 	if (hp->clkbase < 10000 || hp->clkbase > 10000 * 256) {
    338 		/* SDHC 1.0 supports only 10-63 MHz. */
    339 		aprint_error_dev(sc->sc_dev,
    340 		    "base clock frequency out of range: %u MHz\n",
    341 		    hp->clkbase / 1000);
    342 		goto err;
    343 	}
    344 	DPRINTF(1,("%s: base clock frequency %u MHz\n",
    345 	    device_xname(sc->sc_dev), hp->clkbase / 1000));
    346 
    347 	/*
    348 	 * XXX Set the data timeout counter value according to
    349 	 * capabilities. (2.2.15)
    350 	 */
    351 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    352 #if 1
    353 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    354 		HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
    355 #endif
    356 
    357 	/*
    358 	 * Determine SD bus voltage levels supported by the controller.
    359 	 */
    360 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V) &&
    361 	    (hp->specver < SDHC_SPEC_VERS_300 ||
    362 	     ISSET(caps, SDHC_EMBEDDED_SLOT))) {
    363 		SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V);
    364 	}
    365 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V)) {
    366 		SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
    367 	}
    368 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V)) {
    369 		SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
    370 	}
    371 
    372 	/*
    373 	 * Determine the maximum block length supported by the host
    374 	 * controller. (2.2.24)
    375 	 */
    376 	switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
    377 	case SDHC_MAX_BLK_LEN_512:
    378 		hp->maxblklen = 512;
    379 		break;
    380 
    381 	case SDHC_MAX_BLK_LEN_1024:
    382 		hp->maxblklen = 1024;
    383 		break;
    384 
    385 	case SDHC_MAX_BLK_LEN_2048:
    386 		hp->maxblklen = 2048;
    387 		break;
    388 
    389 	case SDHC_MAX_BLK_LEN_4096:
    390 		hp->maxblklen = 4096;
    391 		break;
    392 
    393 	default:
    394 		aprint_error_dev(sc->sc_dev, "max block length unknown\n");
    395 		goto err;
    396 	}
    397 	DPRINTF(1, ("%s: max block length %u byte%s\n",
    398 	    device_xname(sc->sc_dev), hp->maxblklen,
    399 	    hp->maxblklen > 1 ? "s" : ""));
    400 
    401 	/*
    402 	 * Attach the generic SD/MMC bus driver.  (The bus driver must
    403 	 * not invoke any chipset functions before it is attached.)
    404 	 */
    405 	memset(&saa, 0, sizeof(saa));
    406 	saa.saa_busname = "sdmmc";
    407 	saa.saa_sct = &sdhc_functions;
    408 	saa.saa_sch = hp;
    409 	saa.saa_dmat = hp->dmat;
    410 	saa.saa_clkmax = hp->clkbase;
    411 	if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_CGM))
    412 		saa.saa_clkmin = hp->clkbase / 256 / 2046;
    413 	else if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_DVS))
    414 		saa.saa_clkmin = hp->clkbase / 256 / 16;
    415 	else if (hp->sc->sc_clkmsk != 0)
    416 		saa.saa_clkmin = hp->clkbase / (hp->sc->sc_clkmsk >>
    417 		    (ffs(hp->sc->sc_clkmsk) - 1));
    418 	else if (hp->specver == SDHC_SPEC_VERS_300)
    419 		saa.saa_clkmin = hp->clkbase / 0x3ff;
    420 	else
    421 		saa.saa_clkmin = hp->clkbase / 256;
    422 	saa.saa_caps = SMC_CAPS_4BIT_MODE|SMC_CAPS_AUTO_STOP;
    423 	if (ISSET(sc->sc_flags, SDHC_FLAG_8BIT_MODE))
    424 		saa.saa_caps |= SMC_CAPS_8BIT_MODE;
    425 	if (ISSET(caps, SDHC_HIGH_SPEED_SUPP))
    426 		saa.saa_caps |= SMC_CAPS_SD_HIGHSPEED;
    427 	if (ISSET(hp->flags, SHF_USE_DMA)) {
    428 		saa.saa_caps |= SMC_CAPS_DMA;
    429 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    430 			saa.saa_caps |= SMC_CAPS_MULTI_SEG_DMA;
    431 	}
    432 	if (ISSET(sc->sc_flags, SDHC_FLAG_SINGLE_ONLY))
    433 		saa.saa_caps |= SMC_CAPS_SINGLE_ONLY;
    434 	hp->sdmmc = config_found(sc->sc_dev, &saa, sdhc_cfprint);
    435 
    436 	return 0;
    437 
    438 err:
    439 	cv_destroy(&hp->intr_cv);
    440 	mutex_destroy(&hp->intr_mtx);
    441 	mutex_destroy(&hp->host_mtx);
    442 	free(hp, M_DEVBUF);
    443 	sc->sc_host[--sc->sc_nhosts] = NULL;
    444 err1:
    445 	return 1;
    446 }
    447 
    448 int
    449 sdhc_detach(struct sdhc_softc *sc, int flags)
    450 {
    451 	struct sdhc_host *hp;
    452 	int rv = 0;
    453 
    454 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    455 		hp = sc->sc_host[n];
    456 		if (hp == NULL)
    457 			continue;
    458 		if (hp->sdmmc != NULL) {
    459 			rv = config_detach(hp->sdmmc, flags);
    460 			if (rv)
    461 				break;
    462 			hp->sdmmc = NULL;
    463 		}
    464 		/* disable interrupts */
    465 		if ((flags & DETACH_FORCE) == 0) {
    466 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    467 				HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
    468 			} else {
    469 				HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
    470 			}
    471 			sdhc_soft_reset(hp, SDHC_RESET_ALL);
    472 		}
    473 		cv_destroy(&hp->intr_cv);
    474 		mutex_destroy(&hp->intr_mtx);
    475 		mutex_destroy(&hp->host_mtx);
    476 		if (hp->ios > 0) {
    477 			bus_space_unmap(hp->iot, hp->ioh, hp->ios);
    478 			hp->ios = 0;
    479 		}
    480 		free(hp, M_DEVBUF);
    481 		sc->sc_host[n] = NULL;
    482 	}
    483 
    484 	return rv;
    485 }
    486 
    487 bool
    488 sdhc_suspend(device_t dev, const pmf_qual_t *qual)
    489 {
    490 	struct sdhc_softc *sc = device_private(dev);
    491 	struct sdhc_host *hp;
    492 	size_t i;
    493 
    494 	/* XXX poll for command completion or suspend command
    495 	 * in progress */
    496 
    497 	/* Save the host controller state. */
    498 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    499 		hp = sc->sc_host[n];
    500 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    501 			for (i = 0; i < sizeof hp->regs; i += 4) {
    502 				uint32_t v = HREAD4(hp, i);
    503 				hp->regs[i + 0] = (v >> 0);
    504 				hp->regs[i + 1] = (v >> 8);
    505 				if (i + 3 < sizeof hp->regs) {
    506 					hp->regs[i + 2] = (v >> 16);
    507 					hp->regs[i + 3] = (v >> 24);
    508 				}
    509 			}
    510 		} else {
    511 			for (i = 0; i < sizeof hp->regs; i++) {
    512 				hp->regs[i] = HREAD1(hp, i);
    513 			}
    514 		}
    515 	}
    516 	return true;
    517 }
    518 
    519 bool
    520 sdhc_resume(device_t dev, const pmf_qual_t *qual)
    521 {
    522 	struct sdhc_softc *sc = device_private(dev);
    523 	struct sdhc_host *hp;
    524 	size_t i;
    525 
    526 	/* Restore the host controller state. */
    527 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    528 		hp = sc->sc_host[n];
    529 		(void)sdhc_host_reset(hp);
    530 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    531 			for (i = 0; i < sizeof hp->regs; i += 4) {
    532 				if (i + 3 < sizeof hp->regs) {
    533 					HWRITE4(hp, i,
    534 					    (hp->regs[i + 0] << 0)
    535 					    | (hp->regs[i + 1] << 8)
    536 					    | (hp->regs[i + 2] << 16)
    537 					    | (hp->regs[i + 3] << 24));
    538 				} else {
    539 					HWRITE4(hp, i,
    540 					    (hp->regs[i + 0] << 0)
    541 					    | (hp->regs[i + 1] << 8));
    542 				}
    543 			}
    544 		} else {
    545 			for (i = 0; i < sizeof hp->regs; i++) {
    546 				HWRITE1(hp, i, hp->regs[i]);
    547 			}
    548 		}
    549 	}
    550 	return true;
    551 }
    552 
    553 bool
    554 sdhc_shutdown(device_t dev, int flags)
    555 {
    556 	struct sdhc_softc *sc = device_private(dev);
    557 	struct sdhc_host *hp;
    558 
    559 	/* XXX chip locks up if we don't disable it before reboot. */
    560 	for (size_t i = 0; i < sc->sc_nhosts; i++) {
    561 		hp = sc->sc_host[i];
    562 		(void)sdhc_host_reset(hp);
    563 	}
    564 	return true;
    565 }
    566 
    567 /*
    568  * Reset the host controller.  Called during initialization, when
    569  * cards are removed, upon resume, and during error recovery.
    570  */
    571 static int
    572 sdhc_host_reset1(sdmmc_chipset_handle_t sch)
    573 {
    574 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    575 	uint32_t sdhcimask;
    576 	int error;
    577 
    578 	/* Don't lock. */
    579 
    580 	/* Disable all interrupts. */
    581 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    582 		HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
    583 	} else {
    584 		HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
    585 	}
    586 
    587 	/*
    588 	 * Reset the entire host controller and wait up to 100ms for
    589 	 * the controller to clear the reset bit.
    590 	 */
    591 	error = sdhc_soft_reset(hp, SDHC_RESET_ALL);
    592 	if (error)
    593 		goto out;
    594 
    595 	/* Set data timeout counter value to max for now. */
    596 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    597 #if 1
    598 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    599 		HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
    600 #endif
    601 
    602 	/* Enable interrupts. */
    603 	mutex_enter(&hp->intr_mtx);
    604 	sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
    605 	    SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
    606 	    SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
    607 	    SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
    608 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    609 		sdhcimask |= SDHC_EINTR_STATUS_MASK << 16;
    610 		HWRITE4(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
    611 		sdhcimask ^=
    612 		    (SDHC_EINTR_STATUS_MASK ^ SDHC_EINTR_SIGNAL_MASK) << 16;
    613 		sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
    614 		HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
    615 	} else {
    616 		HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
    617 		HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
    618 		sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
    619 		HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
    620 		HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
    621 	}
    622 	mutex_exit(&hp->intr_mtx);
    623 
    624 out:
    625 	return error;
    626 }
    627 
    628 static int
    629 sdhc_host_reset(sdmmc_chipset_handle_t sch)
    630 {
    631 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    632 	int error;
    633 
    634 	mutex_enter(&hp->host_mtx);
    635 	error = sdhc_host_reset1(sch);
    636 	mutex_exit(&hp->host_mtx);
    637 
    638 	return error;
    639 }
    640 
    641 static uint32_t
    642 sdhc_host_ocr(sdmmc_chipset_handle_t sch)
    643 {
    644 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    645 
    646 	return hp->ocr;
    647 }
    648 
    649 static int
    650 sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
    651 {
    652 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    653 
    654 	return hp->maxblklen;
    655 }
    656 
    657 /*
    658  * Return non-zero if the card is currently inserted.
    659  */
    660 static int
    661 sdhc_card_detect(sdmmc_chipset_handle_t sch)
    662 {
    663 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    664 	int r;
    665 
    666 	if (hp->sc->sc_vendor_card_detect)
    667 		return (*hp->sc->sc_vendor_card_detect)(hp->sc);
    668 
    669 	mutex_enter(&hp->host_mtx);
    670 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED);
    671 	mutex_exit(&hp->host_mtx);
    672 
    673 	return r ? 1 : 0;
    674 }
    675 
    676 /*
    677  * Return non-zero if the card is currently write-protected.
    678  */
    679 static int
    680 sdhc_write_protect(sdmmc_chipset_handle_t sch)
    681 {
    682 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    683 	int r;
    684 
    685 	if (hp->sc->sc_vendor_write_protect)
    686 		return (*hp->sc->sc_vendor_write_protect)(hp->sc);
    687 
    688 	mutex_enter(&hp->host_mtx);
    689 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH);
    690 	mutex_exit(&hp->host_mtx);
    691 
    692 	return r ? 0 : 1;
    693 }
    694 
    695 /*
    696  * Set or change SD bus voltage and enable or disable SD bus power.
    697  * Return zero on success.
    698  */
    699 static int
    700 sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    701 {
    702 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    703 	uint8_t vdd;
    704 	int error = 0;
    705 	const uint32_t pcmask =
    706 	    ~(SDHC_BUS_POWER | (SDHC_VOLTAGE_MASK << SDHC_VOLTAGE_SHIFT));
    707 
    708 	mutex_enter(&hp->host_mtx);
    709 
    710 	/*
    711 	 * Disable bus power before voltage change.
    712 	 */
    713 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)
    714 	    && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_PWR0))
    715 		HWRITE1(hp, SDHC_POWER_CTL, 0);
    716 
    717 	/* If power is disabled, reset the host and return now. */
    718 	if (ocr == 0) {
    719 		(void)sdhc_host_reset1(hp);
    720 		goto out;
    721 	}
    722 
    723 	/*
    724 	 * Select the lowest voltage according to capabilities.
    725 	 */
    726 	ocr &= hp->ocr;
    727 	if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V)) {
    728 		vdd = SDHC_VOLTAGE_1_8V;
    729 	} else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V)) {
    730 		vdd = SDHC_VOLTAGE_3_0V;
    731 	} else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) {
    732 		vdd = SDHC_VOLTAGE_3_3V;
    733 	} else {
    734 		/* Unsupported voltage level requested. */
    735 		error = EINVAL;
    736 		goto out;
    737 	}
    738 
    739 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    740 		/*
    741 		 * Enable bus power.  Wait at least 1 ms (or 74 clocks) plus
    742 		 * voltage ramp until power rises.
    743 		 */
    744 		HWRITE1(hp, SDHC_POWER_CTL,
    745 		    HREAD1(hp, SDHC_POWER_CTL) & pcmask);
    746 		sdmmc_delay(1);
    747 		HWRITE1(hp, SDHC_POWER_CTL, (vdd << SDHC_VOLTAGE_SHIFT));
    748 		sdmmc_delay(1);
    749 		HSET1(hp, SDHC_POWER_CTL, SDHC_BUS_POWER);
    750 		sdmmc_delay(10000);
    751 
    752 		/*
    753 		 * The host system may not power the bus due to battery low,
    754 		 * etc.  In that case, the host controller should clear the
    755 		 * bus power bit.
    756 		 */
    757 		if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
    758 			error = ENXIO;
    759 			goto out;
    760 		}
    761 	}
    762 
    763 out:
    764 	mutex_exit(&hp->host_mtx);
    765 
    766 	return error;
    767 }
    768 
    769 /*
    770  * Return the smallest possible base clock frequency divisor value
    771  * for the CLOCK_CTL register to produce `freq' (KHz).
    772  */
    773 static bool
    774 sdhc_clock_divisor(struct sdhc_host *hp, u_int freq, u_int *divp)
    775 {
    776 	u_int div;
    777 
    778 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_CGM)) {
    779 		for (div = hp->clkbase / freq; div <= 0x3ff; div++) {
    780 			if ((hp->clkbase / div) <= freq) {
    781 				*divp = SDHC_SDCLK_CGM
    782 				    | ((div & 0x300) << SDHC_SDCLK_XDIV_SHIFT)
    783 				    | ((div & 0x0ff) << SDHC_SDCLK_DIV_SHIFT);
    784 				//freq = hp->clkbase / div;
    785 				return true;
    786 			}
    787 		}
    788 		/* No divisor found. */
    789 		return false;
    790 	}
    791 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_DVS)) {
    792 		u_int dvs = (hp->clkbase + freq - 1) / freq;
    793 		u_int roundup = dvs & 1;
    794 		for (dvs >>= 1, div = 1; div <= 256; div <<= 1, dvs >>= 1) {
    795 			if (dvs + roundup <= 16) {
    796 				dvs += roundup - 1;
    797 				*divp = (div << SDHC_SDCLK_DIV_SHIFT)
    798 				    |   (dvs << SDHC_SDCLK_DVS_SHIFT);
    799 				DPRINTF(2,
    800 				    ("%s: divisor for freq %u is %u * %u\n",
    801 				    HDEVNAME(hp), freq, div * 2, dvs + 1));
    802 				//freq = hp->clkbase / (div * 2) * (dvs + 1);
    803 				return true;
    804 			}
    805 			/*
    806 			 * If we drop bits, we need to round up the divisor.
    807 			 */
    808 			roundup |= dvs & 1;
    809 		}
    810 		/* No divisor found. */
    811 		return false;
    812 	}
    813 	if (hp->sc->sc_clkmsk != 0) {
    814 		div = howmany(hp->clkbase, freq);
    815 		if (div > (hp->sc->sc_clkmsk >> (ffs(hp->sc->sc_clkmsk) - 1)))
    816 			return false;
    817 		*divp = div << (ffs(hp->sc->sc_clkmsk) - 1);
    818 		//freq = hp->clkbase / div;
    819 		return true;
    820 	}
    821 	if (hp->specver == SDHC_SPEC_VERS_300) {
    822 		div = howmany(hp->clkbase, freq);
    823 		div = div > 1 ? howmany(div, 2) : 0;
    824 		if (div > 0x3ff)
    825 			return false;
    826 		*divp = (((div >> 8) & SDHC_SDCLK_XDIV_MASK)
    827 			 << SDHC_SDCLK_XDIV_SHIFT) |
    828 			(((div >> 0) & SDHC_SDCLK_DIV_MASK)
    829 			 << SDHC_SDCLK_DIV_SHIFT);
    830 		//freq = hp->clkbase / div;
    831 		return true;
    832 	} else {
    833 		for (div = 1; div <= 256; div *= 2) {
    834 			if ((hp->clkbase / div) <= freq) {
    835 				*divp = (div / 2) << SDHC_SDCLK_DIV_SHIFT;
    836 				//freq = hp->clkbase / div;
    837 				return true;
    838 			}
    839 		}
    840 		/* No divisor found. */
    841 		return false;
    842 	}
    843 	/* No divisor found. */
    844 	return false;
    845 }
    846 
    847 /*
    848  * Set or change SDCLK frequency or disable the SD clock.
    849  * Return zero on success.
    850  */
    851 static int
    852 sdhc_bus_clock(sdmmc_chipset_handle_t sch, int freq)
    853 {
    854 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    855 	u_int div;
    856 	u_int timo;
    857 	int16_t reg;
    858 	int error = 0;
    859 #ifdef DIAGNOSTIC
    860 	bool present;
    861 
    862 	mutex_enter(&hp->host_mtx);
    863 	present = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK);
    864 	mutex_exit(&hp->host_mtx);
    865 
    866 	/* Must not stop the clock if commands are in progress. */
    867 	if (present && sdhc_card_detect(hp)) {
    868 		aprint_normal_dev(hp->sc->sc_dev,
    869 		    "%s: command in progress\n", __func__);
    870 	}
    871 #endif
    872 
    873 	mutex_enter(&hp->host_mtx);
    874 
    875 	if (hp->sc->sc_vendor_bus_clock) {
    876 		error = (*hp->sc->sc_vendor_bus_clock)(hp->sc, freq);
    877 		if (error != 0)
    878 			goto out;
    879 	}
    880 
    881 	/*
    882 	 * Stop SD clock before changing the frequency.
    883 	 */
    884 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    885 		HCLR4(hp, SDHC_CLOCK_CTL, 0xfff8);
    886 		if (freq == SDMMC_SDCLK_OFF) {
    887 			HSET4(hp, SDHC_CLOCK_CTL, 0x80f0);
    888 			goto out;
    889 		}
    890 	} else {
    891 		HCLR2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
    892 		if (freq == SDMMC_SDCLK_OFF)
    893 			goto out;
    894 	}
    895 
    896 	/*
    897 	 * Set the minimum base clock frequency divisor.
    898 	 */
    899 	if (!sdhc_clock_divisor(hp, freq, &div)) {
    900 		/* Invalid base clock frequency or `freq' value. */
    901 		error = EINVAL;
    902 		goto out;
    903 	}
    904 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    905 		HWRITE4(hp, SDHC_CLOCK_CTL,
    906 		    div | (SDHC_TIMEOUT_MAX << 16));
    907 	} else {
    908 		reg = HREAD2(hp, SDHC_CLOCK_CTL);
    909 		reg &= (SDHC_INTCLK_STABLE | SDHC_INTCLK_ENABLE);
    910 		HWRITE2(hp, SDHC_CLOCK_CTL, reg | div);
    911 	}
    912 
    913 	/*
    914 	 * Start internal clock.  Wait 10ms for stabilization.
    915 	 */
    916 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    917 		sdmmc_delay(10000);
    918 		HSET4(hp, SDHC_CLOCK_CTL,
    919 		    8 | SDHC_INTCLK_ENABLE | SDHC_INTCLK_STABLE);
    920 	} else {
    921 		HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
    922 		for (timo = 1000; timo > 0; timo--) {
    923 			if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL),
    924 			    SDHC_INTCLK_STABLE))
    925 				break;
    926 			sdmmc_delay(10);
    927 		}
    928 		if (timo == 0) {
    929 			error = ETIMEDOUT;
    930 			goto out;
    931 		}
    932 	}
    933 
    934 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    935 		HSET1(hp, SDHC_SOFTWARE_RESET, SDHC_INIT_ACTIVE);
    936 		/*
    937 		 * Sending 80 clocks at 400kHz takes 200us.
    938 		 * So delay for that time + slop and then
    939 		 * check a few times for completion.
    940 		 */
    941 		sdmmc_delay(210);
    942 		for (timo = 10; timo > 0; timo--) {
    943 			if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET),
    944 			    SDHC_INIT_ACTIVE))
    945 				break;
    946 			sdmmc_delay(10);
    947 		}
    948 		DPRINTF(2,("%s: %u init spins\n", __func__, 10 - timo));
    949 
    950 		/*
    951 		 * Enable SD clock.
    952 		 */
    953 		HSET4(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
    954 	} else {
    955 		/*
    956 		 * Enable SD clock.
    957 		 */
    958 		HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
    959 
    960 		if (freq > 25000 &&
    961 		    !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_HS_BIT))
    962 			HSET1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
    963 		else
    964 			HCLR1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
    965 	}
    966 
    967 out:
    968 	mutex_exit(&hp->host_mtx);
    969 
    970 	return error;
    971 }
    972 
    973 static int
    974 sdhc_bus_width(sdmmc_chipset_handle_t sch, int width)
    975 {
    976 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    977 	int reg;
    978 
    979 	switch (width) {
    980 	case 1:
    981 	case 4:
    982 		break;
    983 
    984 	case 8:
    985 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_8BIT_MODE))
    986 			break;
    987 		/* FALLTHROUGH */
    988 	default:
    989 		DPRINTF(0,("%s: unsupported bus width (%d)\n",
    990 		    HDEVNAME(hp), width));
    991 		return 1;
    992 	}
    993 
    994 	mutex_enter(&hp->host_mtx);
    995 	reg = HREAD1(hp, SDHC_HOST_CTL);
    996 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    997 		reg &= ~(SDHC_4BIT_MODE|SDHC_ESDHC_8BIT_MODE);
    998 		if (width == 4)
    999 			reg |= SDHC_4BIT_MODE;
   1000 		else if (width == 8)
   1001 			reg |= SDHC_ESDHC_8BIT_MODE;
   1002 	} else {
   1003 		reg &= ~SDHC_4BIT_MODE;
   1004 		if (width == 4)
   1005 			reg |= SDHC_4BIT_MODE;
   1006 	}
   1007 	HWRITE1(hp, SDHC_HOST_CTL, reg);
   1008 	mutex_exit(&hp->host_mtx);
   1009 
   1010 	return 0;
   1011 }
   1012 
   1013 static int
   1014 sdhc_bus_rod(sdmmc_chipset_handle_t sch, int on)
   1015 {
   1016 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1017 
   1018 	if (hp->sc->sc_vendor_rod)
   1019 		return (*hp->sc->sc_vendor_rod)(hp->sc, on);
   1020 
   1021 	return 0;
   1022 }
   1023 
   1024 static void
   1025 sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
   1026 {
   1027 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1028 
   1029 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1030 		mutex_enter(&hp->intr_mtx);
   1031 		if (enable) {
   1032 			HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1033 			HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
   1034 		} else {
   1035 			HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
   1036 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1037 		}
   1038 		mutex_exit(&hp->intr_mtx);
   1039 	}
   1040 }
   1041 
   1042 static void
   1043 sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
   1044 {
   1045 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1046 
   1047 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1048 		mutex_enter(&hp->intr_mtx);
   1049 		HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1050 		mutex_exit(&hp->intr_mtx);
   1051 	}
   1052 }
   1053 
   1054 static int
   1055 sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value)
   1056 {
   1057 	uint32_t state;
   1058 	int timeout;
   1059 
   1060 	for (timeout = 10; timeout > 0; timeout--) {
   1061 		if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value)
   1062 			return 0;
   1063 		sdmmc_delay(10000);
   1064 	}
   1065 	DPRINTF(0,("%s: timeout waiting for %x (state=%x)\n", HDEVNAME(hp),
   1066 	    value, state));
   1067 	return ETIMEDOUT;
   1068 }
   1069 
   1070 static void
   1071 sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
   1072 {
   1073 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1074 	int error;
   1075 
   1076 	if (cmd->c_data && ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1077 		const uint16_t ready = SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY;
   1078 		mutex_enter(&hp->intr_mtx);
   1079 		if (ISSET(hp->flags, SHF_USE_DMA)) {
   1080 			HCLR2(hp, SDHC_NINTR_SIGNAL_EN, ready);
   1081 			HCLR2(hp, SDHC_NINTR_STATUS_EN, ready);
   1082 		} else {
   1083 			HSET2(hp, SDHC_NINTR_SIGNAL_EN, ready);
   1084 			HSET2(hp, SDHC_NINTR_STATUS_EN, ready);
   1085 		}
   1086 		mutex_exit(&hp->intr_mtx);
   1087 	}
   1088 
   1089 	/*
   1090 	 * Start the MMC command, or mark `cmd' as failed and return.
   1091 	 */
   1092 	error = sdhc_start_command(hp, cmd);
   1093 	if (error) {
   1094 		cmd->c_error = error;
   1095 		goto out;
   1096 	}
   1097 
   1098 	/*
   1099 	 * Wait until the command phase is done, or until the command
   1100 	 * is marked done for any other reason.
   1101 	 */
   1102 	if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT)) {
   1103 		cmd->c_error = ETIMEDOUT;
   1104 		goto out;
   1105 	}
   1106 
   1107 	/*
   1108 	 * The host controller removes bits [0:7] from the response
   1109 	 * data (CRC) and we pass the data up unchanged to the bus
   1110 	 * driver (without padding).
   1111 	 */
   1112 	mutex_enter(&hp->host_mtx);
   1113 	if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
   1114 		cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE + 0);
   1115 		if (ISSET(cmd->c_flags, SCF_RSP_136)) {
   1116 			cmd->c_resp[1] = HREAD4(hp, SDHC_RESPONSE + 4);
   1117 			cmd->c_resp[2] = HREAD4(hp, SDHC_RESPONSE + 8);
   1118 			cmd->c_resp[3] = HREAD4(hp, SDHC_RESPONSE + 12);
   1119 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_RSP136_CRC)) {
   1120 				cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
   1121 				    (cmd->c_resp[1] << 24);
   1122 				cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
   1123 				    (cmd->c_resp[2] << 24);
   1124 				cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
   1125 				    (cmd->c_resp[3] << 24);
   1126 				cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
   1127 			}
   1128 		}
   1129 	}
   1130 	mutex_exit(&hp->host_mtx);
   1131 	DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0]));
   1132 
   1133 	/*
   1134 	 * If the command has data to transfer in any direction,
   1135 	 * execute the transfer now.
   1136 	 */
   1137 	if (cmd->c_error == 0 && cmd->c_data != NULL)
   1138 		sdhc_transfer_data(hp, cmd);
   1139 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY)) {
   1140 		if (!sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE, hz * 10)) {
   1141 			cmd->c_error = ETIMEDOUT;
   1142 			goto out;
   1143 		}
   1144 	}
   1145 
   1146 out:
   1147 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)
   1148 	    && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_LED_ON)) {
   1149 		mutex_enter(&hp->host_mtx);
   1150 		/* Turn off the LED. */
   1151 		HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
   1152 		mutex_exit(&hp->host_mtx);
   1153 	}
   1154 	SET(cmd->c_flags, SCF_ITSDONE);
   1155 
   1156 	DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp),
   1157 	    cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort",
   1158 	    cmd->c_flags, cmd->c_error));
   1159 }
   1160 
   1161 static int
   1162 sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1163 {
   1164 	struct sdhc_softc * const sc = hp->sc;
   1165 	uint16_t blksize = 0;
   1166 	uint16_t blkcount = 0;
   1167 	uint16_t mode;
   1168 	uint16_t command;
   1169 	int error;
   1170 
   1171 	DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x, status=%#x\n",
   1172 	    HDEVNAME(hp), cmd->c_opcode, cmd->c_arg, cmd->c_data,
   1173 	    cmd->c_datalen, cmd->c_flags, HREAD4(hp, SDHC_NINTR_STATUS)));
   1174 
   1175 	/*
   1176 	 * The maximum block length for commands should be the minimum
   1177 	 * of the host buffer size and the card buffer size. (1.7.2)
   1178 	 */
   1179 
   1180 	/* Fragment the data into proper blocks. */
   1181 	if (cmd->c_datalen > 0) {
   1182 		blksize = MIN(cmd->c_datalen, cmd->c_blklen);
   1183 		blkcount = cmd->c_datalen / blksize;
   1184 		if (cmd->c_datalen % blksize > 0) {
   1185 			/* XXX: Split this command. (1.7.4) */
   1186 			aprint_error_dev(sc->sc_dev,
   1187 			    "data not a multiple of %u bytes\n", blksize);
   1188 			return EINVAL;
   1189 		}
   1190 	}
   1191 
   1192 	/* Check limit imposed by 9-bit block count. (1.7.2) */
   1193 	if (blkcount > SDHC_BLOCK_COUNT_MAX) {
   1194 		aprint_error_dev(sc->sc_dev, "too much data\n");
   1195 		return EINVAL;
   1196 	}
   1197 
   1198 	/* Prepare transfer mode register value. (2.2.5) */
   1199 	mode = SDHC_BLOCK_COUNT_ENABLE;
   1200 	if (ISSET(cmd->c_flags, SCF_CMD_READ))
   1201 		mode |= SDHC_READ_MODE;
   1202 	if (blkcount > 1) {
   1203 		mode |= SDHC_MULTI_BLOCK_MODE;
   1204 		/* XXX only for memory commands? */
   1205 		mode |= SDHC_AUTO_CMD12_ENABLE;
   1206 	}
   1207 	if (cmd->c_dmamap != NULL && cmd->c_datalen > 0 &&
   1208 	    !ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA)) {
   1209 		mode |= SDHC_DMA_ENABLE;
   1210 	}
   1211 
   1212 	/*
   1213 	 * Prepare command register value. (2.2.6)
   1214 	 */
   1215 	command = (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT;
   1216 
   1217 	if (ISSET(cmd->c_flags, SCF_RSP_CRC))
   1218 		command |= SDHC_CRC_CHECK_ENABLE;
   1219 	if (ISSET(cmd->c_flags, SCF_RSP_IDX))
   1220 		command |= SDHC_INDEX_CHECK_ENABLE;
   1221 	if (cmd->c_data != NULL)
   1222 		command |= SDHC_DATA_PRESENT_SELECT;
   1223 
   1224 	if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
   1225 		command |= SDHC_NO_RESPONSE;
   1226 	else if (ISSET(cmd->c_flags, SCF_RSP_136))
   1227 		command |= SDHC_RESP_LEN_136;
   1228 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
   1229 		command |= SDHC_RESP_LEN_48_CHK_BUSY;
   1230 	else
   1231 		command |= SDHC_RESP_LEN_48;
   1232 
   1233 	/* Wait until command and data inhibit bits are clear. (1.5) */
   1234 	error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0);
   1235 	if (error)
   1236 		return error;
   1237 
   1238 	DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n",
   1239 	    HDEVNAME(hp), blksize, blkcount, mode, command));
   1240 
   1241 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1242 		blksize |= (MAX(0, PAGE_SHIFT - 12) & SDHC_DMA_BOUNDARY_MASK) <<
   1243 		    SDHC_DMA_BOUNDARY_SHIFT;	/* PAGE_SIZE DMA boundary */
   1244 	}
   1245 
   1246 	mutex_enter(&hp->host_mtx);
   1247 
   1248 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1249 		/* Alert the user not to remove the card. */
   1250 		HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
   1251 	}
   1252 
   1253 	/* Set DMA start address. */
   1254 	if (ISSET(mode, SDHC_DMA_ENABLE))
   1255 		HWRITE4(hp, SDHC_DMA_ADDR, cmd->c_dmamap->dm_segs[0].ds_addr);
   1256 
   1257 	/*
   1258 	 * Start a CPU data transfer.  Writing to the high order byte
   1259 	 * of the SDHC_COMMAND register triggers the SD command. (1.5)
   1260 	 */
   1261 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   1262 		HWRITE4(hp, SDHC_BLOCK_SIZE, blksize | (blkcount << 16));
   1263 		HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
   1264 		HWRITE4(hp, SDHC_TRANSFER_MODE, mode | (command << 16));
   1265 	} else {
   1266 		HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
   1267 		HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
   1268 		HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
   1269 		HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
   1270 		HWRITE2(hp, SDHC_COMMAND, command);
   1271 	}
   1272 
   1273 	mutex_exit(&hp->host_mtx);
   1274 
   1275 	return 0;
   1276 }
   1277 
   1278 static void
   1279 sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1280 {
   1281 	struct sdhc_softc *sc = hp->sc;
   1282 	int error;
   1283 
   1284 	DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp),
   1285 	    MMC_R1(cmd->c_resp), cmd->c_datalen));
   1286 
   1287 #ifdef SDHC_DEBUG
   1288 	/* XXX I forgot why I wanted to know when this happens :-( */
   1289 	if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
   1290 	    ISSET(MMC_R1(cmd->c_resp), 0xcb00)) {
   1291 		aprint_error_dev(hp->sc->sc_dev,
   1292 		    "CMD52/53 error response flags %#x\n",
   1293 		    MMC_R1(cmd->c_resp) & 0xff00);
   1294 	}
   1295 #endif
   1296 
   1297 	if (cmd->c_dmamap != NULL) {
   1298 		if (hp->sc->sc_vendor_transfer_data_dma != NULL) {
   1299 			error = hp->sc->sc_vendor_transfer_data_dma(sc, cmd);
   1300 			if (error == 0 && !sdhc_wait_intr(hp,
   1301 			    SDHC_TRANSFER_COMPLETE, SDHC_TRANSFER_TIMEOUT)) {
   1302 				error = ETIMEDOUT;
   1303 			}
   1304 		} else {
   1305 			error = sdhc_transfer_data_dma(hp, cmd);
   1306 		}
   1307 	} else
   1308 		error = sdhc_transfer_data_pio(hp, cmd);
   1309 	if (error)
   1310 		cmd->c_error = error;
   1311 	SET(cmd->c_flags, SCF_ITSDONE);
   1312 
   1313 	DPRINTF(1,("%s: data transfer done (error=%d)\n",
   1314 	    HDEVNAME(hp), cmd->c_error));
   1315 }
   1316 
   1317 static int
   1318 sdhc_transfer_data_dma(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1319 {
   1320 	bus_dma_segment_t *dm_segs = cmd->c_dmamap->dm_segs;
   1321 	bus_addr_t posaddr;
   1322 	bus_addr_t segaddr;
   1323 	bus_size_t seglen;
   1324 	u_int seg = 0;
   1325 	int error = 0;
   1326 	int status;
   1327 
   1328 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_DMA_INTERRUPT);
   1329 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_DMA_INTERRUPT);
   1330 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
   1331 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
   1332 
   1333 	for (;;) {
   1334 		status = sdhc_wait_intr(hp,
   1335 		    SDHC_DMA_INTERRUPT|SDHC_TRANSFER_COMPLETE,
   1336 		    SDHC_DMA_TIMEOUT);
   1337 
   1338 		if (status & SDHC_TRANSFER_COMPLETE) {
   1339 			break;
   1340 		}
   1341 		if (!status) {
   1342 			error = ETIMEDOUT;
   1343 			break;
   1344 		}
   1345 		if ((status & SDHC_DMA_INTERRUPT) == 0) {
   1346 			continue;
   1347 		}
   1348 
   1349 		/* DMA Interrupt (boundary crossing) */
   1350 
   1351 		segaddr = dm_segs[seg].ds_addr;
   1352 		seglen = dm_segs[seg].ds_len;
   1353 		mutex_enter(&hp->host_mtx);
   1354 		posaddr = HREAD4(hp, SDHC_DMA_ADDR);
   1355 		mutex_exit(&hp->host_mtx);
   1356 
   1357 		if ((seg == (cmd->c_dmamap->dm_nsegs-1)) && (posaddr == (segaddr + seglen))) {
   1358 			continue;
   1359 		}
   1360 		mutex_enter(&hp->host_mtx);
   1361 		if ((posaddr >= segaddr) && (posaddr < (segaddr + seglen)))
   1362 			HWRITE4(hp, SDHC_DMA_ADDR, posaddr);
   1363 		else if ((posaddr >= segaddr) && (posaddr == (segaddr + seglen)) && (seg + 1) < cmd->c_dmamap->dm_nsegs)
   1364 			HWRITE4(hp, SDHC_DMA_ADDR, dm_segs[++seg].ds_addr);
   1365 		mutex_exit(&hp->host_mtx);
   1366 		KASSERT(seg < cmd->c_dmamap->dm_nsegs);
   1367 	}
   1368 
   1369 	return error;
   1370 }
   1371 
   1372 static int
   1373 sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1374 {
   1375 	uint8_t *data = cmd->c_data;
   1376 	void (*pio_func)(struct sdhc_host *, uint8_t *, u_int);
   1377 	u_int len, datalen;
   1378 	u_int imask;
   1379 	u_int pmask;
   1380 	int error = 0;
   1381 
   1382 	if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
   1383 		imask = SDHC_BUFFER_READ_READY;
   1384 		pmask = SDHC_BUFFER_READ_ENABLE;
   1385 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1386 			pio_func = esdhc_read_data_pio;
   1387 		} else {
   1388 			pio_func = sdhc_read_data_pio;
   1389 		}
   1390 	} else {
   1391 		imask = SDHC_BUFFER_WRITE_READY;
   1392 		pmask = SDHC_BUFFER_WRITE_ENABLE;
   1393 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1394 			pio_func = esdhc_write_data_pio;
   1395 		} else {
   1396 			pio_func = sdhc_write_data_pio;
   1397 		}
   1398 	}
   1399 	datalen = cmd->c_datalen;
   1400 
   1401 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & imask);
   1402 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
   1403 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
   1404 
   1405 	while (datalen > 0) {
   1406 		if (!ISSET(HREAD4(hp, SDHC_PRESENT_STATE), imask)) {
   1407 			mutex_enter(&hp->intr_mtx);
   1408 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   1409 				HSET4(hp, SDHC_NINTR_SIGNAL_EN, imask);
   1410 			} else {
   1411 				HSET2(hp, SDHC_NINTR_SIGNAL_EN, imask);
   1412 			}
   1413 			mutex_exit(&hp->intr_mtx);
   1414 			if (!sdhc_wait_intr(hp, imask, SDHC_BUFFER_TIMEOUT)) {
   1415 				error = ETIMEDOUT;
   1416 				break;
   1417 			}
   1418 
   1419 			error = sdhc_wait_state(hp, pmask, pmask);
   1420 			if (error)
   1421 				break;
   1422 		}
   1423 
   1424 		len = MIN(datalen, cmd->c_blklen);
   1425 		(*pio_func)(hp, data, len);
   1426 		DPRINTF(2,("%s: pio data transfer %u @ %p\n",
   1427 		    HDEVNAME(hp), len, data));
   1428 
   1429 		data += len;
   1430 		datalen -= len;
   1431 	}
   1432 
   1433 	if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
   1434 	    SDHC_TRANSFER_TIMEOUT))
   1435 		error = ETIMEDOUT;
   1436 
   1437 	return error;
   1438 }
   1439 
   1440 static void
   1441 sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1442 {
   1443 
   1444 	if (((__uintptr_t)data & 3) == 0) {
   1445 		while (datalen > 3) {
   1446 			*(uint32_t *)data = le32toh(HREAD4(hp, SDHC_DATA));
   1447 			data += 4;
   1448 			datalen -= 4;
   1449 		}
   1450 		if (datalen > 1) {
   1451 			*(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
   1452 			data += 2;
   1453 			datalen -= 2;
   1454 		}
   1455 		if (datalen > 0) {
   1456 			*data = HREAD1(hp, SDHC_DATA);
   1457 			data += 1;
   1458 			datalen -= 1;
   1459 		}
   1460 	} else if (((__uintptr_t)data & 1) == 0) {
   1461 		while (datalen > 1) {
   1462 			*(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
   1463 			data += 2;
   1464 			datalen -= 2;
   1465 		}
   1466 		if (datalen > 0) {
   1467 			*data = HREAD1(hp, SDHC_DATA);
   1468 			data += 1;
   1469 			datalen -= 1;
   1470 		}
   1471 	} else {
   1472 		while (datalen > 0) {
   1473 			*data = HREAD1(hp, SDHC_DATA);
   1474 			data += 1;
   1475 			datalen -= 1;
   1476 		}
   1477 	}
   1478 }
   1479 
   1480 static void
   1481 sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1482 {
   1483 
   1484 	if (((__uintptr_t)data & 3) == 0) {
   1485 		while (datalen > 3) {
   1486 			HWRITE4(hp, SDHC_DATA, htole32(*(uint32_t *)data));
   1487 			data += 4;
   1488 			datalen -= 4;
   1489 		}
   1490 		if (datalen > 1) {
   1491 			HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
   1492 			data += 2;
   1493 			datalen -= 2;
   1494 		}
   1495 		if (datalen > 0) {
   1496 			HWRITE1(hp, SDHC_DATA, *data);
   1497 			data += 1;
   1498 			datalen -= 1;
   1499 		}
   1500 	} else if (((__uintptr_t)data & 1) == 0) {
   1501 		while (datalen > 1) {
   1502 			HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
   1503 			data += 2;
   1504 			datalen -= 2;
   1505 		}
   1506 		if (datalen > 0) {
   1507 			HWRITE1(hp, SDHC_DATA, *data);
   1508 			data += 1;
   1509 			datalen -= 1;
   1510 		}
   1511 	} else {
   1512 		while (datalen > 0) {
   1513 			HWRITE1(hp, SDHC_DATA, *data);
   1514 			data += 1;
   1515 			datalen -= 1;
   1516 		}
   1517 	}
   1518 }
   1519 
   1520 static void
   1521 esdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1522 {
   1523 	uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
   1524 	uint32_t v;
   1525 
   1526 	const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_READ_SHIFT) & SDHC_WATERMARK_READ_MASK;
   1527 	size_t count = 0;
   1528 
   1529 	while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1530 		if (count == 0) {
   1531 			/*
   1532 			 * If we've drained "watermark" words, we need to wait
   1533 			 * a little bit so the read FIFO can refill.
   1534 			 */
   1535 			sdmmc_delay(10);
   1536 			count = watermark;
   1537 		}
   1538 		v = HREAD4(hp, SDHC_DATA);
   1539 		v = le32toh(v);
   1540 		*(uint32_t *)data = v;
   1541 		data += 4;
   1542 		datalen -= 4;
   1543 		status = HREAD2(hp, SDHC_NINTR_STATUS);
   1544 		count--;
   1545 	}
   1546 	if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1547 		if (count == 0) {
   1548 			sdmmc_delay(10);
   1549 		}
   1550 		v = HREAD4(hp, SDHC_DATA);
   1551 		v = le32toh(v);
   1552 		do {
   1553 			*data++ = v;
   1554 			v >>= 8;
   1555 		} while (--datalen > 0);
   1556 	}
   1557 }
   1558 
   1559 static void
   1560 esdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   1561 {
   1562 	uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
   1563 	uint32_t v;
   1564 
   1565 	const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_WRITE_SHIFT) & SDHC_WATERMARK_WRITE_MASK;
   1566 	size_t count = watermark;
   1567 
   1568 	while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1569 		if (count == 0) {
   1570 			sdmmc_delay(10);
   1571 			count = watermark;
   1572 		}
   1573 		v = *(uint32_t *)data;
   1574 		v = htole32(v);
   1575 		HWRITE4(hp, SDHC_DATA, v);
   1576 		data += 4;
   1577 		datalen -= 4;
   1578 		status = HREAD2(hp, SDHC_NINTR_STATUS);
   1579 		count--;
   1580 	}
   1581 	if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   1582 		if (count == 0) {
   1583 			sdmmc_delay(10);
   1584 		}
   1585 		v = *(uint32_t *)data;
   1586 		v = htole32(v);
   1587 		HWRITE4(hp, SDHC_DATA, v);
   1588 	}
   1589 }
   1590 
   1591 /* Prepare for another command. */
   1592 static int
   1593 sdhc_soft_reset(struct sdhc_host *hp, int mask)
   1594 {
   1595 	int timo;
   1596 
   1597 	DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask));
   1598 
   1599 	/* Request the reset.  */
   1600 	HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
   1601 
   1602 	/*
   1603 	 * If necessary, wait for the controller to set the bits to
   1604 	 * acknowledge the reset.
   1605 	 */
   1606 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_WAIT_RESET) &&
   1607 	    ISSET(mask, (SDHC_RESET_DAT | SDHC_RESET_CMD))) {
   1608 		for (timo = 10000; timo > 0; timo--) {
   1609 			if (ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
   1610 				break;
   1611 			/* Short delay because I worry we may miss it...  */
   1612 			sdmmc_delay(1);
   1613 		}
   1614 		if (timo == 0)
   1615 			return ETIMEDOUT;
   1616 	}
   1617 
   1618 	/*
   1619 	 * Wait for the controller to clear the bits to indicate that
   1620 	 * the reset has completed.
   1621 	 */
   1622 	for (timo = 10; timo > 0; timo--) {
   1623 		if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
   1624 			break;
   1625 		sdmmc_delay(10000);
   1626 	}
   1627 	if (timo == 0) {
   1628 		DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp),
   1629 		    HREAD1(hp, SDHC_SOFTWARE_RESET)));
   1630 		return ETIMEDOUT;
   1631 	}
   1632 
   1633 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1634 		HSET4(hp, SDHC_DMA_CTL, SDHC_DMA_SNOOP);
   1635 	}
   1636 
   1637 	return 0;
   1638 }
   1639 
   1640 static int
   1641 sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo)
   1642 {
   1643 	int status;
   1644 
   1645 	mask |= SDHC_ERROR_INTERRUPT;
   1646 
   1647 	mutex_enter(&hp->intr_mtx);
   1648 	status = hp->intr_status & mask;
   1649 	while (status == 0) {
   1650 		if (cv_timedwait(&hp->intr_cv, &hp->intr_mtx, timo)
   1651 		    == EWOULDBLOCK) {
   1652 			status |= SDHC_ERROR_INTERRUPT;
   1653 			break;
   1654 		}
   1655 		status = hp->intr_status & mask;
   1656 	}
   1657 	hp->intr_status &= ~status;
   1658 
   1659 	DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status,
   1660 	    hp->intr_error_status));
   1661 
   1662 	/* Command timeout has higher priority than command complete. */
   1663 	if (ISSET(status, SDHC_ERROR_INTERRUPT) || hp->intr_error_status) {
   1664 		hp->intr_error_status = 0;
   1665 		hp->intr_status &= ~SDHC_ERROR_INTERRUPT;
   1666 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1667 		    (void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
   1668 		}
   1669 		status = 0;
   1670 	}
   1671 	mutex_exit(&hp->intr_mtx);
   1672 
   1673 	return status;
   1674 }
   1675 
   1676 /*
   1677  * Established by attachment driver at interrupt priority IPL_SDMMC.
   1678  */
   1679 int
   1680 sdhc_intr(void *arg)
   1681 {
   1682 	struct sdhc_softc *sc = (struct sdhc_softc *)arg;
   1683 	struct sdhc_host *hp;
   1684 	int done = 0;
   1685 	uint16_t status;
   1686 	uint16_t error;
   1687 
   1688 	/* We got an interrupt, but we don't know from which slot. */
   1689 	for (size_t host = 0; host < sc->sc_nhosts; host++) {
   1690 		hp = sc->sc_host[host];
   1691 		if (hp == NULL)
   1692 			continue;
   1693 
   1694 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   1695 			/* Find out which interrupts are pending. */
   1696 			uint32_t xstatus = HREAD4(hp, SDHC_NINTR_STATUS);
   1697 			status = xstatus;
   1698 			error = xstatus >> 16;
   1699 			if (error)
   1700 				xstatus |= SDHC_ERROR_INTERRUPT;
   1701 			else if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   1702 				continue; /* no interrupt for us */
   1703 			/* Acknowledge the interrupts we are about to handle. */
   1704 			HWRITE4(hp, SDHC_NINTR_STATUS, xstatus);
   1705 		} else {
   1706 			/* Find out which interrupts are pending. */
   1707 			error = 0;
   1708 			status = HREAD2(hp, SDHC_NINTR_STATUS);
   1709 			if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   1710 				continue; /* no interrupt for us */
   1711 			/* Acknowledge the interrupts we are about to handle. */
   1712 			HWRITE2(hp, SDHC_NINTR_STATUS, status);
   1713 			if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
   1714 				/* Acknowledge error interrupts. */
   1715 				error = HREAD2(hp, SDHC_EINTR_STATUS);
   1716 				HWRITE2(hp, SDHC_EINTR_STATUS, error);
   1717 			}
   1718 		}
   1719 
   1720 		DPRINTF(2,("%s: interrupt status=%x error=%x\n", HDEVNAME(hp),
   1721 		    status, error));
   1722 
   1723 		mutex_enter(&hp->intr_mtx);
   1724 
   1725 		/* Claim this interrupt. */
   1726 		done = 1;
   1727 
   1728 		/*
   1729 		 * Service error interrupts.
   1730 		 */
   1731 		if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR|
   1732 		    SDHC_DATA_TIMEOUT_ERROR)) {
   1733 			hp->intr_error_status |= error;
   1734 			hp->intr_status |= status;
   1735 			cv_broadcast(&hp->intr_cv);
   1736 		}
   1737 
   1738 		/*
   1739 		 * Wake up the sdmmc event thread to scan for cards.
   1740 		 */
   1741 		if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)) {
   1742 			if (hp->sdmmc != NULL) {
   1743 				sdmmc_needs_discover(hp->sdmmc);
   1744 			}
   1745 			if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1746 				HCLR4(hp, SDHC_NINTR_STATUS_EN,
   1747 				    status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
   1748 				HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
   1749 				    status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
   1750 			}
   1751 		}
   1752 
   1753 		/*
   1754 		 * Wake up the blocking process to service command
   1755 		 * related interrupt(s).
   1756 		 */
   1757 		if (ISSET(status, SDHC_COMMAND_COMPLETE|
   1758 		    SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY|
   1759 		    SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) {
   1760 			hp->intr_status |= status;
   1761 			if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1762 				HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
   1763 				    status & (SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY));
   1764 			}
   1765 			cv_broadcast(&hp->intr_cv);
   1766 		}
   1767 
   1768 		/*
   1769 		 * Service SD card interrupts.
   1770 		 */
   1771 		if (!ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)
   1772 		    && ISSET(status, SDHC_CARD_INTERRUPT)) {
   1773 			DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp)));
   1774 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1775 			sdmmc_card_intr(hp->sdmmc);
   1776 		}
   1777 		mutex_exit(&hp->intr_mtx);
   1778 	}
   1779 
   1780 	return done;
   1781 }
   1782 
   1783 #ifdef SDHC_DEBUG
   1784 void
   1785 sdhc_dump_regs(struct sdhc_host *hp)
   1786 {
   1787 
   1788 	printf("0x%02x PRESENT_STATE:    %x\n", SDHC_PRESENT_STATE,
   1789 	    HREAD4(hp, SDHC_PRESENT_STATE));
   1790 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
   1791 		printf("0x%02x POWER_CTL:        %x\n", SDHC_POWER_CTL,
   1792 		    HREAD1(hp, SDHC_POWER_CTL));
   1793 	printf("0x%02x NINTR_STATUS:     %x\n", SDHC_NINTR_STATUS,
   1794 	    HREAD2(hp, SDHC_NINTR_STATUS));
   1795 	printf("0x%02x EINTR_STATUS:     %x\n", SDHC_EINTR_STATUS,
   1796 	    HREAD2(hp, SDHC_EINTR_STATUS));
   1797 	printf("0x%02x NINTR_STATUS_EN:  %x\n", SDHC_NINTR_STATUS_EN,
   1798 	    HREAD2(hp, SDHC_NINTR_STATUS_EN));
   1799 	printf("0x%02x EINTR_STATUS_EN:  %x\n", SDHC_EINTR_STATUS_EN,
   1800 	    HREAD2(hp, SDHC_EINTR_STATUS_EN));
   1801 	printf("0x%02x NINTR_SIGNAL_EN:  %x\n", SDHC_NINTR_SIGNAL_EN,
   1802 	    HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
   1803 	printf("0x%02x EINTR_SIGNAL_EN:  %x\n", SDHC_EINTR_SIGNAL_EN,
   1804 	    HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
   1805 	printf("0x%02x CAPABILITIES:     %x\n", SDHC_CAPABILITIES,
   1806 	    HREAD4(hp, SDHC_CAPABILITIES));
   1807 	printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
   1808 	    HREAD4(hp, SDHC_MAX_CAPABILITIES));
   1809 }
   1810 #endif
   1811