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