Home | History | Annotate | Line # | Download | only in sdmmc
sdhc.c revision 1.7.2.3
      1 /*	$NetBSD: sdhc.c,v 1.7.2.3 2011/12/24 01:33:58 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.7.2.3 2011/12/24 01:33:58 matt Exp $");
     27 
     28 #include <sys/param.h>
     29 #include <sys/device.h>
     30 #include <sys/kernel.h>
     31 #include <sys/kthread.h>
     32 #include <sys/malloc.h>
     33 #include <sys/systm.h>
     34 #include <sys/mutex.h>
     35 #include <sys/condvar.h>
     36 
     37 #include <dev/sdmmc/sdhcreg.h>
     38 #include <dev/sdmmc/sdhcvar.h>
     39 #include <dev/sdmmc/sdmmcchip.h>
     40 #include <dev/sdmmc/sdmmcreg.h>
     41 #include <dev/sdmmc/sdmmcvar.h>
     42 
     43 #ifdef SDHC_DEBUG
     44 int sdhcdebug = 2;
     45 #define DPRINTF(n,s)	do { if ((n) <= sdhcdebug) printf s; } while (0)
     46 void	sdhc_dump_regs(struct sdhc_host *);
     47 #else
     48 #define DPRINTF(n,s)	do {} while (0)
     49 #endif
     50 
     51 #define SDHC_COMMAND_TIMEOUT	hz
     52 #define SDHC_BUFFER_TIMEOUT	hz
     53 #define SDHC_TRANSFER_TIMEOUT	hz
     54 #define SDHC_DMA_TIMEOUT	hz
     55 
     56 struct sdhc_host {
     57 	struct sdhc_softc *sc;		/* host controller device */
     58 
     59 	bus_space_tag_t iot;		/* host register set tag */
     60 	bus_space_handle_t ioh;		/* host register set handle */
     61 	bus_dma_tag_t dmat;		/* host DMA tag */
     62 
     63 	device_t sdmmc;			/* generic SD/MMC device */
     64 
     65 	struct kmutex host_mtx;
     66 
     67 	u_int clkbase;			/* base clock frequency in KHz */
     68 	int maxblklen;			/* maximum block length */
     69 	uint32_t ocr;			/* OCR value from capabilities */
     70 
     71 	uint8_t regs[14];		/* host controller state */
     72 
     73 	uint16_t intr_status;		/* soft interrupt status */
     74 	uint16_t intr_error_status;	/* soft error status */
     75 	struct kmutex intr_mtx;
     76 	struct kcondvar intr_cv;
     77 
     78 	uint32_t flags;			/* flags for this host */
     79 #define SHF_USE_DMA		0x0001
     80 #define SHF_USE_4BIT_MODE	0x0002
     81 };
     82 
     83 #define HDEVNAME(hp)	(device_xname((hp)->sc->sc_dev))
     84 
     85 #define HREAD1(hp, reg)							\
     86 	(bus_space_read_1((hp)->iot, (hp)->ioh, (reg)))
     87 #define HREAD2(hp, reg)							\
     88 	(bus_space_read_2((hp)->iot, (hp)->ioh, (reg)))
     89 #define HREAD4(hp, reg)							\
     90 	(bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
     91 #define HWRITE1(hp, reg, val)						\
     92 	bus_space_write_1((hp)->iot, (hp)->ioh, (reg), (val))
     93 #define HWRITE2(hp, reg, val)						\
     94 	bus_space_write_2((hp)->iot, (hp)->ioh, (reg), (val))
     95 #define HWRITE4(hp, reg, val)						\
     96 	bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val))
     97 #define HCLR1(hp, reg, bits)						\
     98 	HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits))
     99 #define HCLR2(hp, reg, bits)						\
    100 	HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits))
    101 #define HSET1(hp, reg, bits)						\
    102 	HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits))
    103 #define HSET2(hp, reg, bits)						\
    104 	HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits))
    105 
    106 static int	sdhc_host_reset(sdmmc_chipset_handle_t);
    107 static int	sdhc_host_reset1(sdmmc_chipset_handle_t);
    108 static uint32_t	sdhc_host_ocr(sdmmc_chipset_handle_t);
    109 static int	sdhc_host_maxblklen(sdmmc_chipset_handle_t);
    110 static int	sdhc_card_detect(sdmmc_chipset_handle_t);
    111 static int	sdhc_write_protect(sdmmc_chipset_handle_t);
    112 static int	sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t);
    113 static int	sdhc_bus_clock(sdmmc_chipset_handle_t, int);
    114 static int	sdhc_bus_width(sdmmc_chipset_handle_t, int);
    115 static void	sdhc_card_enable_intr(sdmmc_chipset_handle_t, int);
    116 static void	sdhc_card_intr_ack(sdmmc_chipset_handle_t);
    117 static void	sdhc_exec_command(sdmmc_chipset_handle_t,
    118 		    struct sdmmc_command *);
    119 static int	sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
    120 static int	sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t);
    121 static int	sdhc_soft_reset(struct sdhc_host *, int);
    122 static int	sdhc_wait_intr(struct sdhc_host *, int, int);
    123 static void	sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
    124 static int	sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *);
    125 static void	sdhc_read_data_pio(struct sdhc_host *, uint8_t *, int);
    126 static void	sdhc_write_data_pio(struct sdhc_host *, uint8_t *, int);
    127 
    128 static struct sdmmc_chip_functions sdhc_functions = {
    129 	/* host controller reset */
    130 	sdhc_host_reset,
    131 
    132 	/* host controller capabilities */
    133 	sdhc_host_ocr,
    134 	sdhc_host_maxblklen,
    135 
    136 	/* card detection */
    137 	sdhc_card_detect,
    138 
    139 	/* write protect */
    140 	sdhc_write_protect,
    141 
    142 	/* bus power, clock frequency and width */
    143 	sdhc_bus_power,
    144 	sdhc_bus_clock,
    145 	sdhc_bus_width,
    146 
    147 	/* command execution */
    148 	sdhc_exec_command,
    149 
    150 	/* card interrupt */
    151 	sdhc_card_enable_intr,
    152 	sdhc_card_intr_ack
    153 };
    154 
    155 /*
    156  * Called by attachment driver.  For each SD card slot there is one SD
    157  * host controller standard register set. (1.3)
    158  */
    159 int
    160 sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
    161     bus_space_handle_t ioh, bus_size_t iosize)
    162 {
    163 	struct sdmmcbus_attach_args saa;
    164 	struct sdhc_host *hp;
    165 	uint32_t caps;
    166 #ifdef SDHC_DEBUG
    167 	uint16_t sdhcver;
    168 
    169 	sdhcver = bus_space_read_2(iot, ioh, SDHC_HOST_CTL_VERSION);
    170 	aprint_normal_dev(sc->sc_dev, "SD Host Specification/Vendor Version ");
    171 	switch (SDHC_SPEC_VERSION(sdhcver)) {
    172 	case 0x00:
    173 		aprint_normal("1.0/%u\n", SDHC_VENDOR_VERSION(sdhcver));
    174 		break;
    175 
    176 	default:
    177 		aprint_normal(">1.0/%u\n", SDHC_VENDOR_VERSION(sdhcver));
    178 		break;
    179 	}
    180 #endif
    181 
    182 	/* Allocate one more host structure. */
    183 	hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO);
    184 	if (hp == NULL) {
    185 		aprint_error_dev(sc->sc_dev,
    186 		    "couldn't alloc memory (sdhc host)\n");
    187 		goto err1;
    188 	}
    189 	sc->sc_host[sc->sc_nhosts++] = hp;
    190 
    191 	/* Fill in the new host structure. */
    192 	hp->sc = sc;
    193 	hp->iot = iot;
    194 	hp->ioh = ioh;
    195 	hp->dmat = sc->sc_dmat;
    196 
    197 	mutex_init(&hp->host_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    198 	mutex_init(&hp->intr_mtx, MUTEX_DEFAULT, IPL_SDMMC);
    199 	cv_init(&hp->intr_cv, "sdhcintr");
    200 
    201 	/*
    202 	 * Reset the host controller and enable interrupts.
    203 	 */
    204 	(void)sdhc_host_reset(hp);
    205 
    206 	/* Determine host capabilities. */
    207 	mutex_enter(&hp->host_mtx);
    208 	caps = HREAD4(hp, SDHC_CAPABILITIES);
    209 	mutex_exit(&hp->host_mtx);
    210 
    211 	DPRINTF(1,("%s: caps=0x%08x\n", device_xname(sc->sc_dev), caps));
    212 
    213 #if notyet
    214 	/* Use DMA if the host system and the controller support it. */
    215 	if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA)
    216 	 || ((ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA)
    217 	   && ISSET(caps, SDHC_DMA_SUPPORT)))) {
    218 		SET(hp->flags, SHF_USE_DMA);
    219 		aprint_normal_dev(sc->sc_dev, "using DMA transfer\n");
    220 	}
    221 #endif
    222 
    223 	/*
    224 	 * Determine the base clock frequency. (2.2.24)
    225 	 */
    226 	if (SDHC_BASE_FREQ_KHZ(caps) != 0)
    227 		hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
    228 	if (hp->clkbase == 0) {
    229 		/* The attachment driver must tell us. */
    230 		aprint_error_dev(sc->sc_dev,"unknown base clock frequency\n");
    231 		goto err;
    232 	} else if (hp->clkbase < 10000 || hp->clkbase > 255000) {
    233 		/* SDHC 1.0 supports only 10-63 MHz. */
    234 		/* SDHC 1.0 supports only 10-255 MHz. */
    235 		aprint_error_dev(sc->sc_dev,
    236 		    "base clock frequency out of range: %u MHz\n",
    237 		    hp->clkbase / 1000);
    238 		goto err;
    239 	}
    240 	DPRINTF(1,("%s: base clock frequency %u MHz\n",
    241 	    device_xname(sc->sc_dev), hp->clkbase / 1000));
    242 
    243 	/*
    244 	 * XXX Set the data timeout counter value according to
    245 	 * capabilities. (2.2.15)
    246 	 */
    247 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    248 
    249 	/*
    250 	 * Determine SD bus voltage levels supported by the controller.
    251 	 */
    252 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V))
    253 		SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V);
    254 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V))
    255 		SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
    256 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V))
    257 		SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
    258 
    259 	/*
    260 	 * Determine the maximum block length supported by the host
    261 	 * controller. (2.2.24)
    262 	 */
    263 	switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
    264 	case SDHC_MAX_BLK_LEN_512:
    265 		hp->maxblklen = 512;
    266 		break;
    267 
    268 	case SDHC_MAX_BLK_LEN_1024:
    269 		hp->maxblklen = 1024;
    270 		break;
    271 
    272 	case SDHC_MAX_BLK_LEN_2048:
    273 		hp->maxblklen = 2048;
    274 		break;
    275 
    276 	case SDHC_MAX_BLK_LEN_4096:
    277 		hp->maxblklen = 4096;
    278 		break;
    279 
    280 	default:
    281 		aprint_error_dev(sc->sc_dev, "max block length unknown\n");
    282 		goto err;
    283 	}
    284 	DPRINTF(1, ("%s: max block length %u byte%s\n",
    285 	    device_xname(sc->sc_dev), hp->maxblklen,
    286 	    hp->maxblklen > 1 ? "s" : ""));
    287 
    288 #if 0
    289 	if (sc->sc_flags & SDHC_FLAG_HAS_CGM) {
    290 		uint16_t clk = HREAD2(hp, SDHC_CLOCK_CTL);
    291 		clk |= SDHC_SDCLK_CGM;
    292 		HWRITE2(hp, SDHC_CLOCK_CTL, clk);
    293 		clk = HREAD2(hp, SDHC_CLOCK_CTL);
    294 		if ((clk & SDHC_SDCLK_CGM) == 0) {
    295 			sc->sc_flags &= ~SDHC_FLAG_HAS_CGM;
    296 			DPRINTF(1, ("%s: CGM indicated but not supported\n",
    297 			    device_xname(sc->sc_dev)));
    298 		}
    299 	}
    300 #endif
    301 
    302 	/*
    303 	 * Attach the generic SD/MMC bus driver.  (The bus driver must
    304 	 * not invoke any chipset functions before it is attached.)
    305 	 */
    306 	memset(&saa, 0, sizeof(saa));
    307 	saa.saa_busname = "sdmmc";
    308 	saa.saa_sct = &sdhc_functions;
    309 	saa.saa_sch = hp;
    310 	saa.saa_dmat = hp->dmat;
    311 	if (sc->sc_flags & SDHC_FLAG_HAS_CGM) {
    312 		saa.saa_clkmin = hp->clkbase / 2046;
    313 	} else {
    314 		saa.saa_clkmin = hp->clkbase / 256;
    315 	}
    316 	saa.saa_clkmax = hp->clkbase;
    317 	saa.saa_caps = SMC_CAPS_4BIT_MODE|SMC_CAPS_AUTO_STOP;
    318 	DPRINTF(1, ("%s: clkmin=%d clkmax=%d\n",
    319 	    device_xname(sc->sc_dev), saa.saa_clkmin, saa.saa_clkmax));
    320 #if notyet
    321 	if (ISSET(hp->flags, SHF_USE_DMA))
    322 		saa.saa_caps |= SMC_CAPS_DMA;
    323 #endif
    324 
    325 	hp->sdmmc = config_found(sc->sc_dev, &saa, NULL);
    326 
    327 	return 0;
    328 
    329 err:
    330 	cv_destroy(&hp->intr_cv);
    331 	mutex_destroy(&hp->intr_mtx);
    332 	mutex_destroy(&hp->host_mtx);
    333 	free(hp, M_DEVBUF);
    334 	sc->sc_host[--sc->sc_nhosts] = NULL;
    335 err1:
    336 	return 1;
    337 }
    338 
    339 bool
    340 sdhc_suspend(device_t dev PMF_FN_ARGS)
    341 {
    342 	struct sdhc_softc *sc = device_private(dev);
    343 	struct sdhc_host *hp;
    344 	int n, i;
    345 
    346 	/* XXX poll for command completion or suspend command
    347 	 * in progress */
    348 
    349 	/* Save the host controller state. */
    350 	for (n = 0; n < sc->sc_nhosts; n++) {
    351 		hp = sc->sc_host[n];
    352 		for (i = 0; i < sizeof hp->regs; i++)
    353 			hp->regs[i] = HREAD1(hp, i);
    354 	}
    355 	return true;
    356 }
    357 
    358 bool
    359 sdhc_resume(device_t dev PMF_FN_ARGS)
    360 {
    361 	struct sdhc_softc *sc = device_private(dev);
    362 	struct sdhc_host *hp;
    363 	int n, i;
    364 
    365 	/* Restore the host controller state. */
    366 	for (n = 0; n < sc->sc_nhosts; n++) {
    367 		hp = sc->sc_host[n];
    368 		(void)sdhc_host_reset(hp);
    369 		for (i = 0; i < sizeof hp->regs; i++)
    370 			HWRITE1(hp, i, hp->regs[i]);
    371 	}
    372 	return true;
    373 }
    374 
    375 bool
    376 sdhc_shutdown(device_t dev, int flags)
    377 {
    378 	struct sdhc_softc *sc = device_private(dev);
    379 	struct sdhc_host *hp;
    380 	int i;
    381 
    382 	/* XXX chip locks up if we don't disable it before reboot. */
    383 	for (i = 0; i < sc->sc_nhosts; i++) {
    384 		hp = sc->sc_host[i];
    385 		(void)sdhc_host_reset(hp);
    386 	}
    387 	return true;
    388 }
    389 
    390 /*
    391  * Reset the host controller.  Called during initialization, when
    392  * cards are removed, upon resume, and during error recovery.
    393  */
    394 static int
    395 sdhc_host_reset1(sdmmc_chipset_handle_t sch)
    396 {
    397 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    398 	uint16_t sdhcimask;
    399 	int error;
    400 
    401 	/* Don't lock. */
    402 
    403 	/* Disable all interrupts. */
    404 	HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
    405 
    406 	/*
    407 	 * Reset the entire host controller and wait up to 100ms for
    408 	 * the controller to clear the reset bit.
    409 	 */
    410 	error = sdhc_soft_reset(hp, SDHC_RESET_ALL);
    411 	if (error)
    412 		goto out;
    413 
    414 	/* Set data timeout counter value to max for now. */
    415 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    416 
    417 	/* Enable interrupts. */
    418 	sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
    419 	    SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
    420 	    SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
    421 	    SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
    422 	HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
    423 	HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
    424 	HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
    425 	HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
    426 
    427 out:
    428 	return error;
    429 }
    430 
    431 static int
    432 sdhc_host_reset(sdmmc_chipset_handle_t sch)
    433 {
    434 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    435 	int error;
    436 
    437 	mutex_enter(&hp->host_mtx);
    438 	error = sdhc_host_reset1(sch);
    439 	mutex_exit(&hp->host_mtx);
    440 
    441 	return error;
    442 }
    443 
    444 static uint32_t
    445 sdhc_host_ocr(sdmmc_chipset_handle_t sch)
    446 {
    447 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    448 
    449 	return hp->ocr;
    450 }
    451 
    452 static int
    453 sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
    454 {
    455 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    456 
    457 	return hp->maxblklen;
    458 }
    459 
    460 /*
    461  * Return non-zero if the card is currently inserted.
    462  */
    463 static int
    464 sdhc_card_detect(sdmmc_chipset_handle_t sch)
    465 {
    466 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    467 	int r;
    468 
    469 	mutex_enter(&hp->host_mtx);
    470 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED);
    471 	mutex_exit(&hp->host_mtx);
    472 
    473 	if (r)
    474 		return 1;
    475 	return 0;
    476 }
    477 
    478 /*
    479  * Return non-zero if the card is currently write-protected.
    480  */
    481 static int
    482 sdhc_write_protect(sdmmc_chipset_handle_t sch)
    483 {
    484 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    485 	int r;
    486 
    487 	mutex_enter(&hp->host_mtx);
    488 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH);
    489 	mutex_exit(&hp->host_mtx);
    490 
    491 	if (!r)
    492 		return 1;
    493 	return 0;
    494 }
    495 
    496 /*
    497  * Set or change SD bus voltage and enable or disable SD bus power.
    498  * Return zero on success.
    499  */
    500 static int
    501 sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    502 {
    503 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    504 	uint8_t vdd;
    505 	int error = 0;
    506 
    507 	mutex_enter(&hp->host_mtx);
    508 
    509 	/*
    510 	 * Disable bus power before voltage change.
    511 	 */
    512 	if (!(hp->sc->sc_flags & SDHC_FLAG_NO_PWR0))
    513 		HWRITE1(hp, SDHC_POWER_CTL, 0);
    514 
    515 	/* If power is disabled, reset the host and return now. */
    516 	if (ocr == 0) {
    517 		(void)sdhc_host_reset1(hp);
    518 		goto out;
    519 	}
    520 
    521 	/*
    522 	 * Select the lowest voltage according to capabilities.
    523 	 */
    524 	ocr &= hp->ocr;
    525 	if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V))
    526 		vdd = SDHC_VOLTAGE_1_8V;
    527 	else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V))
    528 		vdd = SDHC_VOLTAGE_3_0V;
    529 	else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V))
    530 		vdd = SDHC_VOLTAGE_3_3V;
    531 	else {
    532 		/* Unsupported voltage level requested. */
    533 		error = EINVAL;
    534 		goto out;
    535 	}
    536 
    537 	/*
    538 	 * Enable bus power.  Wait at least 1 ms (or 74 clocks) plus
    539 	 * voltage ramp until power rises.
    540 	 */
    541 	HWRITE1(hp, SDHC_POWER_CTL,
    542 	    (vdd << SDHC_VOLTAGE_SHIFT) | SDHC_BUS_POWER);
    543 	sdmmc_delay(10000);
    544 
    545 	/*
    546 	 * The host system may not power the bus due to battery low,
    547 	 * etc.  In that case, the host controller should clear the
    548 	 * bus power bit.
    549 	 */
    550 	if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
    551 		error = ENXIO;
    552 		goto out;
    553 	}
    554 
    555 out:
    556 	mutex_exit(&hp->host_mtx);
    557 
    558 	return error;
    559 }
    560 
    561 /*
    562  * Return the smallest possible base clock frequency divisor value
    563  * for the CLOCK_CTL register to produce `freq' (KHz).
    564  */
    565 static int
    566 sdhc_clock_divisor(struct sdhc_host *hp, u_int freq, int *divp)
    567 {
    568 	int div;
    569 
    570 	if (hp->sc->sc_flags & SDHC_FLAG_HAS_CGM) {
    571 		for (div = hp->clkbase / freq; div <= 0x3ff; div++) {
    572 			if ((hp->clkbase / div) <= freq) {
    573 				*divp = SDHC_SDCLK_CGM
    574 				    | ((div & 0x300) << SDHC_SDCLK_XDIV_SHIFT)
    575 				    | ((div & 0x0ff) << SDHC_SDCLK_DIV_SHIFT);
    576 				return false;
    577 			}
    578 		}
    579 		/* No divisor found. */
    580 		return true;
    581 	}
    582 	for (div = 1; div <= 256; div *= 2) {
    583 		if ((hp->clkbase / div) <= freq) {
    584 			*divp = (div / 2) << SDHC_SDCLK_DIV_SHIFT;
    585 			return false;
    586 		}
    587 	}
    588 
    589 	/* No divisor found. */
    590 	return true;
    591 }
    592 
    593 /*
    594  * Set or change SDCLK frequency or disable the SD clock.
    595  * Return zero on success.
    596  */
    597 static int
    598 sdhc_bus_clock(sdmmc_chipset_handle_t sch, int freq)
    599 {
    600 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    601 	int div;
    602 	int timo;
    603 	int error = 0;
    604 #ifdef DIAGNOSTIC
    605 	int ispresent;
    606 #endif
    607 
    608 #ifdef DIAGNOSTIC
    609 	mutex_enter(&hp->host_mtx);
    610 	ispresent = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK);
    611 	mutex_exit(&hp->host_mtx);
    612 
    613 	/* Must not stop the clock if commands are in progress. */
    614 	if (ispresent && sdhc_card_detect(hp))
    615 		printf("%s: sdhc_sdclk_frequency_select: command in progress\n",
    616 		    device_xname(hp->sc->sc_dev));
    617 #endif
    618 
    619 	mutex_enter(&hp->host_mtx);
    620 
    621 	/*
    622 	 * Stop SD clock before changing the frequency.
    623 	 */
    624 	HWRITE2(hp, SDHC_CLOCK_CTL, 0);
    625 	if (freq == SDMMC_SDCLK_OFF)
    626 		goto out;
    627 
    628 	/*
    629 	 * Set the minimum base clock frequency divisor.
    630 	 */
    631 	if (sdhc_clock_divisor(hp, freq, &div)) {
    632 		/* Invalid base clock frequency or `freq' value. */
    633 		error = EINVAL;
    634 		goto out;
    635 	}
    636 	HWRITE2(hp, SDHC_CLOCK_CTL, div << SDHC_SDCLK_DIV_SHIFT);
    637 
    638 	/*
    639 	 * Start internal clock.  Wait 10ms for stabilization.
    640 	 */
    641 	HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
    642 	for (timo = 1000; timo > 0; timo--) {
    643 		if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL), SDHC_INTCLK_STABLE))
    644 			break;
    645 		sdmmc_delay(10);
    646 	}
    647 	if (timo == 0) {
    648 		error = ETIMEDOUT;
    649 		goto out;
    650 	}
    651 
    652 	/*
    653 	 * Enable SD clock.
    654 	 */
    655 	HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
    656 
    657 out:
    658 	mutex_exit(&hp->host_mtx);
    659 
    660 	return error;
    661 }
    662 
    663 static int
    664 sdhc_bus_width(sdmmc_chipset_handle_t sch, int width)
    665 {
    666 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    667 	int reg;
    668 
    669 	switch (width) {
    670 	case 1:
    671 	case 4:
    672 		break;
    673 
    674 	default:
    675 		DPRINTF(0,("%s: unsupported bus width (%d)\n",
    676 		    HDEVNAME(hp), width));
    677 		return 1;
    678 	}
    679 
    680 	mutex_enter(&hp->host_mtx);
    681 	reg = HREAD1(hp, SDHC_POWER_CTL);
    682 	reg &= ~SDHC_4BIT_MODE;
    683 	if (width == 4)
    684 		reg |= SDHC_4BIT_MODE;
    685 	HWRITE1(hp, SDHC_POWER_CTL, reg);
    686 	mutex_exit(&hp->host_mtx);
    687 
    688 	return 0;
    689 }
    690 
    691 static void
    692 sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
    693 {
    694 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    695 
    696 	mutex_enter(&hp->host_mtx);
    697 	if (enable) {
    698 		HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
    699 		HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
    700 	} else {
    701 		HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
    702 		HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
    703 	}
    704 	mutex_exit(&hp->host_mtx);
    705 }
    706 
    707 static void
    708 sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
    709 {
    710 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    711 
    712 	mutex_enter(&hp->host_mtx);
    713 	HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
    714 	mutex_exit(&hp->host_mtx);
    715 }
    716 
    717 static int
    718 sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value)
    719 {
    720 	uint32_t state;
    721 	int timeout;
    722 
    723 	for (timeout = 10; timeout > 0; timeout--) {
    724 		if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value)
    725 			return 0;
    726 		sdmmc_delay(10000);
    727 	}
    728 	DPRINTF(0,("%s: timeout waiting for %x (state=%x)\n", HDEVNAME(hp),
    729 	    value, state));
    730 	return ETIMEDOUT;
    731 }
    732 
    733 static void
    734 sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
    735 {
    736 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    737 	int error;
    738 
    739 	/*
    740 	 * Start the MMC command, or mark `cmd' as failed and return.
    741 	 */
    742 	error = sdhc_start_command(hp, cmd);
    743 	if (error) {
    744 		cmd->c_error = error;
    745 		goto out;
    746 	}
    747 
    748 	/*
    749 	 * Wait until the command phase is done, or until the command
    750 	 * is marked done for any other reason.
    751 	 */
    752 	if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT)) {
    753 		cmd->c_error = ETIMEDOUT;
    754 		goto out;
    755 	}
    756 
    757 	/*
    758 	 * The host controller removes bits [0:7] from the response
    759 	 * data (CRC) and we pass the data up unchanged to the bus
    760 	 * driver (without padding).
    761 	 */
    762 	mutex_enter(&hp->host_mtx);
    763 	if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
    764 		if (ISSET(cmd->c_flags, SCF_RSP_136)) {
    765 			uint8_t *p = (uint8_t *)cmd->c_resp;
    766 			int i;
    767 
    768 			for (i = 0; i < 15; i++)
    769 				*p++ = HREAD1(hp, SDHC_RESPONSE + i);
    770 		} else {
    771 			cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE);
    772 		}
    773 	}
    774 	mutex_exit(&hp->host_mtx);
    775 	DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0]));
    776 
    777 	/*
    778 	 * If the command has data to transfer in any direction,
    779 	 * execute the transfer now.
    780 	 */
    781 	if (cmd->c_error == 0 && cmd->c_data != NULL)
    782 		sdhc_transfer_data(hp, cmd);
    783 
    784 out:
    785 	mutex_enter(&hp->host_mtx);
    786 	/* Turn off the LED. */
    787 	HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
    788 	mutex_exit(&hp->host_mtx);
    789 	SET(cmd->c_flags, SCF_ITSDONE);
    790 
    791 	DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp),
    792 	    cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort",
    793 	    cmd->c_flags, cmd->c_error));
    794 }
    795 
    796 static int
    797 sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
    798 {
    799 	uint16_t blksize = 0;
    800 	uint16_t blkcount = 0;
    801 	uint16_t mode;
    802 	uint16_t command;
    803 	int error;
    804 
    805 	DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x "
    806 	    "proc=%p \"%s\"\n", HDEVNAME(hp), cmd->c_opcode, cmd->c_arg,
    807 	    cmd->c_data, cmd->c_datalen, cmd->c_flags, curproc,
    808 	    curproc ? curproc->p_comm : ""));
    809 
    810 	/*
    811 	 * The maximum block length for commands should be the minimum
    812 	 * of the host buffer size and the card buffer size. (1.7.2)
    813 	 */
    814 
    815 	/* Fragment the data into proper blocks. */
    816 	if (cmd->c_datalen > 0) {
    817 		blksize = MIN(cmd->c_datalen, cmd->c_blklen);
    818 		blkcount = cmd->c_datalen / blksize;
    819 		if (cmd->c_datalen % blksize > 0) {
    820 			/* XXX: Split this command. (1.7.4) */
    821 			aprint_error_dev(hp->sc->sc_dev,
    822 			    "data not a multiple of %u bytes\n", blksize);
    823 			return EINVAL;
    824 		}
    825 	}
    826 
    827 	/* Check limit imposed by 9-bit block count. (1.7.2) */
    828 	if (blkcount > SDHC_BLOCK_COUNT_MAX) {
    829 		aprint_error_dev(hp->sc->sc_dev, "too much data\n");
    830 		return EINVAL;
    831 	}
    832 
    833 	/* Prepare transfer mode register value. (2.2.5) */
    834 	mode = 0;
    835 	if (ISSET(cmd->c_flags, SCF_CMD_READ))
    836 		mode |= SDHC_READ_MODE;
    837 	if (blkcount > 0) {
    838 		mode |= SDHC_BLOCK_COUNT_ENABLE;
    839 		if (blkcount > 1) {
    840 			mode |= SDHC_MULTI_BLOCK_MODE;
    841 			/* XXX only for memory commands? */
    842 			mode |= SDHC_AUTO_CMD12_ENABLE;
    843 		}
    844 	}
    845 #if notyet
    846 	if (cmd->c_dmap != NULL && cmd->c_datalen > 0)
    847 		mode |= SDHC_DMA_ENABLE;
    848 #endif
    849 
    850 	/*
    851 	 * Prepare command register value. (2.2.6)
    852 	 */
    853 	command =
    854 	 (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT;
    855 
    856 	if (ISSET(cmd->c_flags, SCF_RSP_CRC))
    857 		command |= SDHC_CRC_CHECK_ENABLE;
    858 	if (ISSET(cmd->c_flags, SCF_RSP_IDX))
    859 		command |= SDHC_INDEX_CHECK_ENABLE;
    860 	if (cmd->c_data != NULL)
    861 		command |= SDHC_DATA_PRESENT_SELECT;
    862 
    863 	if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
    864 		command |= SDHC_NO_RESPONSE;
    865 	else if (ISSET(cmd->c_flags, SCF_RSP_136))
    866 		command |= SDHC_RESP_LEN_136;
    867 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
    868 		command |= SDHC_RESP_LEN_48_CHK_BUSY;
    869 	else
    870 		command |= SDHC_RESP_LEN_48;
    871 
    872 	/* Wait until command and data inhibit bits are clear. (1.5) */
    873 	error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0);
    874 	if (error)
    875 		return error;
    876 
    877 	DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n",
    878 	    HDEVNAME(hp), blksize, blkcount, mode, command));
    879 
    880 	mutex_enter(&hp->host_mtx);
    881 
    882 	/* Alert the user not to remove the card. */
    883 	HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
    884 
    885 	/*
    886 	 * Start a CPU data transfer.  Writing to the high order byte
    887 	 * of the SDHC_COMMAND register triggers the SD command. (1.5)
    888 	 */
    889 	HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
    890 	HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
    891 	if (blkcount > 1)
    892 		HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
    893 	HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
    894 	HWRITE2(hp, SDHC_COMMAND, command);
    895 
    896 	mutex_exit(&hp->host_mtx);
    897 
    898 	return 0;
    899 }
    900 
    901 static void
    902 sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
    903 {
    904 	int error;
    905 
    906 	DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp),
    907 	    MMC_R1(cmd->c_resp), cmd->c_datalen));
    908 
    909 #ifdef SDHC_DEBUG
    910 	/* XXX I forgot why I wanted to know when this happens :-( */
    911 	if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
    912 	    ISSET(MMC_R1(cmd->c_resp), 0xcb00)) {
    913 		aprint_error_dev(hp->sc->sc_dev,
    914 		    "CMD52/53 error response flags %#x\n",
    915 		    MMC_R1(cmd->c_resp) & 0xff00);
    916 	}
    917 #endif
    918 
    919 	error = sdhc_transfer_data_pio(hp, cmd);
    920 	if (error)
    921 		cmd->c_error = error;
    922 	SET(cmd->c_flags, SCF_ITSDONE);
    923 
    924 	DPRINTF(1,("%s: data transfer done (error=%d)\n",
    925 	    HDEVNAME(hp), cmd->c_error));
    926 }
    927 
    928 static int
    929 sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd)
    930 {
    931 	uint8_t *data = cmd->c_data;
    932 	int len, datalen;
    933 	int mask;
    934 	int error = 0;
    935 
    936 	mask = ISSET(cmd->c_flags, SCF_CMD_READ) ?
    937 	    SDHC_BUFFER_READ_ENABLE : SDHC_BUFFER_WRITE_ENABLE;
    938 	datalen = cmd->c_datalen;
    939 
    940 	while (datalen > 0) {
    941 		if (!sdhc_wait_intr(hp,
    942 		    SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY,
    943 		    SDHC_BUFFER_TIMEOUT)) {
    944 			error = ETIMEDOUT;
    945 			break;
    946 		}
    947 
    948 		error = sdhc_wait_state(hp, mask, mask);
    949 		if (error)
    950 			break;
    951 
    952 		len = MIN(datalen, cmd->c_blklen);
    953 		if (ISSET(cmd->c_flags, SCF_CMD_READ))
    954 			sdhc_read_data_pio(hp, data, len);
    955 		else
    956 			sdhc_write_data_pio(hp, data, len);
    957 
    958 		data += len;
    959 		datalen -= len;
    960 	}
    961 
    962 	if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
    963 	    SDHC_TRANSFER_TIMEOUT))
    964 		error = ETIMEDOUT;
    965 
    966 	return error;
    967 }
    968 
    969 static void
    970 sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, int datalen)
    971 {
    972 
    973 	if (((__uintptr_t)data & 3) == 0) {
    974 		while (datalen > 3) {
    975 			*(uint32_t *)data = HREAD4(hp, SDHC_DATA);
    976 			data += 4;
    977 			datalen -= 4;
    978 		}
    979 		if (datalen > 1) {
    980 			*(uint16_t *)data = HREAD2(hp, SDHC_DATA);
    981 			data += 2;
    982 			datalen -= 2;
    983 		}
    984 		if (datalen > 0) {
    985 			*data = HREAD1(hp, SDHC_DATA);
    986 			data += 1;
    987 			datalen -= 1;
    988 		}
    989 	} else if (((__uintptr_t)data & 1) == 0) {
    990 		while (datalen > 1) {
    991 			*(uint16_t *)data = HREAD2(hp, SDHC_DATA);
    992 			data += 2;
    993 			datalen -= 2;
    994 		}
    995 		if (datalen > 0) {
    996 			*data = HREAD1(hp, SDHC_DATA);
    997 			data += 1;
    998 			datalen -= 1;
    999 		}
   1000 	} else {
   1001 		while (datalen > 0) {
   1002 			*data = HREAD1(hp, SDHC_DATA);
   1003 			data += 1;
   1004 			datalen -= 1;
   1005 		}
   1006 	}
   1007 }
   1008 
   1009 static void
   1010 sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, int datalen)
   1011 {
   1012 
   1013 	if (((__uintptr_t)data & 3) == 0) {
   1014 		while (datalen > 3) {
   1015 			HWRITE4(hp, SDHC_DATA, *(uint32_t *)data);
   1016 			data += 4;
   1017 			datalen -= 4;
   1018 		}
   1019 		if (datalen > 1) {
   1020 			HWRITE2(hp, SDHC_DATA, *(uint16_t *)data);
   1021 			data += 2;
   1022 			datalen -= 2;
   1023 		}
   1024 		if (datalen > 0) {
   1025 			HWRITE1(hp, SDHC_DATA, *data);
   1026 			data += 1;
   1027 			datalen -= 1;
   1028 		}
   1029 	} else if (((__uintptr_t)data & 1) == 0) {
   1030 		while (datalen > 1) {
   1031 			HWRITE2(hp, SDHC_DATA, *(uint16_t *)data);
   1032 			data += 2;
   1033 			datalen -= 2;
   1034 		}
   1035 		if (datalen > 0) {
   1036 			HWRITE1(hp, SDHC_DATA, *data);
   1037 			data += 1;
   1038 			datalen -= 1;
   1039 		}
   1040 	} else {
   1041 		while (datalen > 0) {
   1042 			HWRITE1(hp, SDHC_DATA, *data);
   1043 			data += 1;
   1044 			datalen -= 1;
   1045 		}
   1046 	}
   1047 }
   1048 
   1049 /* Prepare for another command. */
   1050 static int
   1051 sdhc_soft_reset(struct sdhc_host *hp, int mask)
   1052 {
   1053 	int timo;
   1054 
   1055 	DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask));
   1056 
   1057 	HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
   1058 	for (timo = 10; timo > 0; timo--) {
   1059 		if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
   1060 			break;
   1061 		sdmmc_delay(10000);
   1062 		HWRITE1(hp, SDHC_SOFTWARE_RESET, 0);
   1063 	}
   1064 	if (timo == 0) {
   1065 		DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp),
   1066 		    HREAD1(hp, SDHC_SOFTWARE_RESET)));
   1067 		HWRITE1(hp, SDHC_SOFTWARE_RESET, 0);
   1068 		return ETIMEDOUT;
   1069 	}
   1070 
   1071 	return 0;
   1072 }
   1073 
   1074 static int
   1075 sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo)
   1076 {
   1077 	int status;
   1078 
   1079 	mask |= SDHC_ERROR_INTERRUPT;
   1080 
   1081 	mutex_enter(&hp->intr_mtx);
   1082 	status = hp->intr_status & mask;
   1083 	while (status == 0) {
   1084 		if (cv_timedwait(&hp->intr_cv, &hp->intr_mtx, timo)
   1085 		    == EWOULDBLOCK) {
   1086 			status |= SDHC_ERROR_INTERRUPT;
   1087 			break;
   1088 		}
   1089 		status = hp->intr_status & mask;
   1090 	}
   1091 	hp->intr_status &= ~status;
   1092 
   1093 	DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status,
   1094 	    hp->intr_error_status));
   1095 
   1096 	/* Command timeout has higher priority than command complete. */
   1097 	if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
   1098 		hp->intr_error_status = 0;
   1099 		(void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
   1100 		status = 0;
   1101 	}
   1102 	mutex_exit(&hp->intr_mtx);
   1103 
   1104 	return status;
   1105 }
   1106 
   1107 /*
   1108  * Established by attachment driver at interrupt priority IPL_SDMMC.
   1109  */
   1110 int
   1111 sdhc_intr(void *arg)
   1112 {
   1113 	struct sdhc_softc *sc = (struct sdhc_softc *)arg;
   1114 	struct sdhc_host *hp;
   1115 	int host;
   1116 	int done = 0;
   1117 	uint16_t status;
   1118 	uint16_t error;
   1119 
   1120 	/* We got an interrupt, but we don't know from which slot. */
   1121 	for (host = 0; host < sc->sc_nhosts; host++) {
   1122 		hp = sc->sc_host[host];
   1123 		if (hp == NULL)
   1124 			continue;
   1125 
   1126 		/* Find out which interrupts are pending. */
   1127 		status = HREAD2(hp, SDHC_NINTR_STATUS);
   1128 		if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   1129 			continue; /* no interrupt for us */
   1130 
   1131 		/* Acknowledge the interrupts we are about to handle. */
   1132 		HWRITE2(hp, SDHC_NINTR_STATUS, status);
   1133 		DPRINTF(2,("%s: interrupt status=%x\n", HDEVNAME(hp),
   1134 		    status));
   1135 
   1136 		if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   1137 			continue;
   1138 
   1139 		/* Claim this interrupt. */
   1140 		done = 1;
   1141 
   1142 		/*
   1143 		 * Service error interrupts.
   1144 		 */
   1145 		if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
   1146 			/* Acknowledge error interrupts. */
   1147 			error = HREAD2(hp, SDHC_EINTR_STATUS);
   1148 			HWRITE2(hp, SDHC_EINTR_STATUS, error);
   1149 			DPRINTF(2,("%s: error interrupt, status=%x\n",
   1150 			    HDEVNAME(hp), error));
   1151 
   1152 			if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR|
   1153 			    SDHC_DATA_TIMEOUT_ERROR)) {
   1154 				hp->intr_error_status |= error;
   1155 				hp->intr_status |= status;
   1156 				cv_broadcast(&hp->intr_cv);
   1157 			}
   1158 		}
   1159 
   1160 		/*
   1161 		 * Wake up the sdmmc event thread to scan for cards.
   1162 		 */
   1163 		if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION))
   1164 			sdmmc_needs_discover(hp->sdmmc);
   1165 
   1166 		/*
   1167 		 * Wake up the blocking process to service command
   1168 		 * related interrupt(s).
   1169 		 */
   1170 		if (ISSET(status, SDHC_BUFFER_READ_READY|
   1171 		    SDHC_BUFFER_WRITE_READY|SDHC_COMMAND_COMPLETE|
   1172 		    SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) {
   1173 			hp->intr_status |= status;
   1174 			cv_broadcast(&hp->intr_cv);
   1175 		}
   1176 
   1177 		/*
   1178 		 * Service SD card interrupts.
   1179 		 */
   1180 		if (ISSET(status, SDHC_CARD_INTERRUPT)) {
   1181 			DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp)));
   1182 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1183 			sdmmc_card_intr(hp->sdmmc);
   1184 		}
   1185 	}
   1186 
   1187 	return done;
   1188 }
   1189 
   1190 #ifdef SDHC_DEBUG
   1191 void
   1192 sdhc_dump_regs(struct sdhc_host *hp)
   1193 {
   1194 
   1195 	printf("0x%02x PRESENT_STATE:    %x\n", SDHC_PRESENT_STATE,
   1196 	    HREAD4(hp, SDHC_PRESENT_STATE));
   1197 	printf("0x%02x POWER_CTL:        %x\n", SDHC_POWER_CTL,
   1198 	    HREAD1(hp, SDHC_POWER_CTL));
   1199 	printf("0x%02x NINTR_STATUS:     %x\n", SDHC_NINTR_STATUS,
   1200 	    HREAD2(hp, SDHC_NINTR_STATUS));
   1201 	printf("0x%02x EINTR_STATUS:     %x\n", SDHC_EINTR_STATUS,
   1202 	    HREAD2(hp, SDHC_EINTR_STATUS));
   1203 	printf("0x%02x NINTR_STATUS_EN:  %x\n", SDHC_NINTR_STATUS_EN,
   1204 	    HREAD2(hp, SDHC_NINTR_STATUS_EN));
   1205 	printf("0x%02x EINTR_STATUS_EN:  %x\n", SDHC_EINTR_STATUS_EN,
   1206 	    HREAD2(hp, SDHC_EINTR_STATUS_EN));
   1207 	printf("0x%02x NINTR_SIGNAL_EN:  %x\n", SDHC_NINTR_SIGNAL_EN,
   1208 	    HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
   1209 	printf("0x%02x EINTR_SIGNAL_EN:  %x\n", SDHC_EINTR_SIGNAL_EN,
   1210 	    HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
   1211 	printf("0x%02x CAPABILITIES:     %x\n", SDHC_CAPABILITIES,
   1212 	    HREAD4(hp, SDHC_CAPABILITIES));
   1213 	printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
   1214 	    HREAD4(hp, SDHC_MAX_CAPABILITIES));
   1215 }
   1216 #endif
   1217