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