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