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