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