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