Home | History | Annotate | Line # | Download | only in sdmmc
sdhc.c revision 1.103.2.1
      1 /*	$NetBSD: sdhc.c,v 1.103.2.1 2020/02/25 18:40:43 martin 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.103.2.1 2020/02/25 18:40:43 martin 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/malloc.h>
     36 #include <sys/systm.h>
     37 #include <sys/mutex.h>
     38 #include <sys/condvar.h>
     39 #include <sys/atomic.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*3)
     59 #define SDHC_TUNING_TIMEOUT	hz
     60 
     61 struct sdhc_host {
     62 	struct sdhc_softc *sc;		/* host controller device */
     63 
     64 	bus_space_tag_t iot;		/* host register set tag */
     65 	bus_space_handle_t ioh;		/* host register set handle */
     66 	bus_size_t ios;			/* host register space size */
     67 	bus_dma_tag_t dmat;		/* host DMA tag */
     68 
     69 	device_t sdmmc;			/* generic SD/MMC device */
     70 
     71 	u_int clkbase;			/* base clock frequency in KHz */
     72 	int maxblklen;			/* maximum block length */
     73 	uint32_t ocr;			/* OCR value from capabilities */
     74 
     75 	uint8_t regs[14];		/* host controller state */
     76 
     77 	uint16_t intr_status;		/* soft interrupt status */
     78 	uint16_t intr_error_status;	/* soft error status */
     79 	kmutex_t intr_lock;
     80 	kcondvar_t intr_cv;
     81 
     82 	callout_t tuning_timer;
     83 	int tuning_timing;
     84 	u_int tuning_timer_count;
     85 	u_int tuning_timer_pending;
     86 
     87 	int specver;			/* spec. version */
     88 
     89 	uint32_t flags;			/* flags for this host */
     90 #define SHF_USE_DMA		0x0001
     91 #define SHF_USE_4BIT_MODE	0x0002
     92 #define SHF_USE_8BIT_MODE	0x0004
     93 #define SHF_MODE_DMAEN		0x0008 /* needs SDHC_DMA_ENABLE in mode */
     94 #define SHF_USE_ADMA2_32	0x0010
     95 #define SHF_USE_ADMA2_64	0x0020
     96 #define SHF_USE_ADMA2_MASK	0x0030
     97 
     98 	bus_dmamap_t		adma_map;
     99 	bus_dma_segment_t	adma_segs[1];
    100 	void			*adma2;
    101 
    102 	uint8_t			vdd;	/* last vdd setting */
    103 };
    104 
    105 #define HDEVNAME(hp)	(device_xname((hp)->sc->sc_dev))
    106 
    107 static uint8_t
    108 hread1(struct sdhc_host *hp, bus_size_t reg)
    109 {
    110 
    111 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
    112 		return bus_space_read_1(hp->iot, hp->ioh, reg);
    113 	return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 3));
    114 }
    115 
    116 static uint16_t
    117 hread2(struct sdhc_host *hp, bus_size_t reg)
    118 {
    119 
    120 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
    121 		return bus_space_read_2(hp->iot, hp->ioh, reg);
    122 	return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 2));
    123 }
    124 
    125 #define HREAD1(hp, reg)		hread1(hp, reg)
    126 #define HREAD2(hp, reg)		hread2(hp, reg)
    127 #define HREAD4(hp, reg)		\
    128 	(bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
    129 
    130 
    131 static void
    132 hwrite1(struct sdhc_host *hp, bus_size_t o, uint8_t val)
    133 {
    134 
    135 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    136 		bus_space_write_1(hp->iot, hp->ioh, o, val);
    137 	} else {
    138 		const size_t shift = 8 * (o & 3);
    139 		o &= -4;
    140 		uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
    141 		tmp = (val << shift) | (tmp & ~(0xff << shift));
    142 		bus_space_write_4(hp->iot, hp->ioh, o, tmp);
    143 	}
    144 }
    145 
    146 static void
    147 hwrite2(struct sdhc_host *hp, bus_size_t o, uint16_t val)
    148 {
    149 
    150 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    151 		bus_space_write_2(hp->iot, hp->ioh, o, val);
    152 	} else {
    153 		const size_t shift = 8 * (o & 2);
    154 		o &= -4;
    155 		uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
    156 		tmp = (val << shift) | (tmp & ~(0xffff << shift));
    157 		bus_space_write_4(hp->iot, hp->ioh, o, tmp);
    158 	}
    159 }
    160 
    161 static void
    162 hwrite4(struct sdhc_host *hp, bus_size_t o, uint32_t val)
    163 {
    164 
    165 	bus_space_write_4(hp->iot, hp->ioh, o, val);
    166 }
    167 
    168 #define HWRITE1(hp, reg, val)		hwrite1(hp, reg, val)
    169 #define HWRITE2(hp, reg, val)		hwrite2(hp, reg, val)
    170 #define HWRITE4(hp, reg, val)		hwrite4(hp, reg, val)
    171 
    172 #define HCLR1(hp, reg, bits)						\
    173 	do if ((bits) != 0) HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits)); while (0)
    174 #define HCLR2(hp, reg, bits)						\
    175 	do if ((bits) != 0) HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits)); while (0)
    176 #define HCLR4(hp, reg, bits)						\
    177 	do if ((bits) != 0) HWRITE4((hp), (reg), HREAD4((hp), (reg)) & ~(bits)); while (0)
    178 #define HSET1(hp, reg, bits)						\
    179 	do if ((bits) != 0) HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits)); while (0)
    180 #define HSET2(hp, reg, bits)						\
    181 	do if ((bits) != 0) HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits)); while (0)
    182 #define HSET4(hp, reg, bits)						\
    183 	do if ((bits) != 0) HWRITE4((hp), (reg), HREAD4((hp), (reg)) | (bits)); while (0)
    184 
    185 static int	sdhc_host_reset(sdmmc_chipset_handle_t);
    186 static int	sdhc_host_reset1(sdmmc_chipset_handle_t);
    187 static uint32_t	sdhc_host_ocr(sdmmc_chipset_handle_t);
    188 static int	sdhc_host_maxblklen(sdmmc_chipset_handle_t);
    189 static int	sdhc_card_detect(sdmmc_chipset_handle_t);
    190 static int	sdhc_write_protect(sdmmc_chipset_handle_t);
    191 static int	sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t);
    192 static int	sdhc_bus_clock_ddr(sdmmc_chipset_handle_t, int, bool);
    193 static int	sdhc_bus_width(sdmmc_chipset_handle_t, int);
    194 static int	sdhc_bus_rod(sdmmc_chipset_handle_t, int);
    195 static void	sdhc_card_enable_intr(sdmmc_chipset_handle_t, int);
    196 static void	sdhc_card_intr_ack(sdmmc_chipset_handle_t);
    197 static void	sdhc_exec_command(sdmmc_chipset_handle_t,
    198 		    struct sdmmc_command *);
    199 static int	sdhc_signal_voltage(sdmmc_chipset_handle_t, int);
    200 static int	sdhc_execute_tuning1(struct sdhc_host *, int);
    201 static int	sdhc_execute_tuning(sdmmc_chipset_handle_t, int);
    202 static void	sdhc_tuning_timer(void *);
    203 static void	sdhc_hw_reset(sdmmc_chipset_handle_t);
    204 static int	sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
    205 static int	sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t);
    206 static int	sdhc_soft_reset(struct sdhc_host *, int);
    207 static int	sdhc_wait_intr(struct sdhc_host *, int, int, bool);
    208 static void	sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
    209 static int	sdhc_transfer_data_dma(struct sdhc_host *, struct sdmmc_command *);
    210 static int	sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *);
    211 static void	sdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
    212 static void	sdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
    213 static void	esdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
    214 static void	esdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
    215 
    216 static struct sdmmc_chip_functions sdhc_functions = {
    217 	/* host controller reset */
    218 	.host_reset = sdhc_host_reset,
    219 
    220 	/* host controller capabilities */
    221 	.host_ocr = sdhc_host_ocr,
    222 	.host_maxblklen = sdhc_host_maxblklen,
    223 
    224 	/* card detection */
    225 	.card_detect = sdhc_card_detect,
    226 
    227 	/* write protect */
    228 	.write_protect = sdhc_write_protect,
    229 
    230 	/* bus power, clock frequency, width and ROD(OpenDrain/PushPull) */
    231 	.bus_power = sdhc_bus_power,
    232 	.bus_clock = NULL,	/* see sdhc_bus_clock_ddr */
    233 	.bus_width = sdhc_bus_width,
    234 	.bus_rod = sdhc_bus_rod,
    235 
    236 	/* command execution */
    237 	.exec_command = sdhc_exec_command,
    238 
    239 	/* card interrupt */
    240 	.card_enable_intr = sdhc_card_enable_intr,
    241 	.card_intr_ack = sdhc_card_intr_ack,
    242 
    243 	/* UHS functions */
    244 	.signal_voltage = sdhc_signal_voltage,
    245 	.bus_clock_ddr = sdhc_bus_clock_ddr,
    246 	.execute_tuning = sdhc_execute_tuning,
    247 	.hw_reset = sdhc_hw_reset,
    248 };
    249 
    250 static int
    251 sdhc_cfprint(void *aux, const char *pnp)
    252 {
    253 	const struct sdmmcbus_attach_args * const saa = aux;
    254 	const struct sdhc_host * const hp = saa->saa_sch;
    255 
    256 	if (pnp) {
    257 		aprint_normal("sdmmc at %s", pnp);
    258 	}
    259 	for (size_t host = 0; host < hp->sc->sc_nhosts; host++) {
    260 		if (hp->sc->sc_host[host] == hp) {
    261 			aprint_normal(" slot %zu", host);
    262 		}
    263 	}
    264 
    265 	return UNCONF;
    266 }
    267 
    268 /*
    269  * Called by attachment driver.  For each SD card slot there is one SD
    270  * host controller standard register set. (1.3)
    271  */
    272 int
    273 sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
    274     bus_space_handle_t ioh, bus_size_t iosize)
    275 {
    276 	struct sdmmcbus_attach_args saa;
    277 	struct sdhc_host *hp;
    278 	uint32_t caps, caps2;
    279 	uint16_t sdhcver;
    280 	int error;
    281 
    282 	/* Allocate one more host structure. */
    283 	hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO);
    284 	if (hp == NULL) {
    285 		aprint_error_dev(sc->sc_dev,
    286 		    "couldn't alloc memory (sdhc host)\n");
    287 		goto err1;
    288 	}
    289 	sc->sc_host[sc->sc_nhosts++] = hp;
    290 
    291 	/* Fill in the new host structure. */
    292 	hp->sc = sc;
    293 	hp->iot = iot;
    294 	hp->ioh = ioh;
    295 	hp->ios = iosize;
    296 	hp->dmat = sc->sc_dmat;
    297 
    298 	mutex_init(&hp->intr_lock, MUTEX_DEFAULT, IPL_SDMMC);
    299 	cv_init(&hp->intr_cv, "sdhcintr");
    300 	callout_init(&hp->tuning_timer, CALLOUT_MPSAFE);
    301 	callout_setfunc(&hp->tuning_timer, sdhc_tuning_timer, hp);
    302 
    303 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
    304 		sdhcver = SDHC_SPEC_VERS_300 << SDHC_SPEC_VERS_SHIFT;
    305 	} else if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    306 		sdhcver = HREAD4(hp, SDHC_ESDHC_HOST_CTL_VERSION);
    307 	} else if (iosize <= SDHC_HOST_CTL_VERSION) {
    308 		sdhcver = SDHC_SPEC_NOVERS << SDHC_SPEC_VERS_SHIFT;
    309 	} else {
    310 		sdhcver = HREAD2(hp, SDHC_HOST_CTL_VERSION);
    311 	}
    312 	aprint_normal_dev(sc->sc_dev, "SDHC ");
    313 	hp->specver = SDHC_SPEC_VERSION(sdhcver);
    314 	switch (SDHC_SPEC_VERSION(sdhcver)) {
    315 	case SDHC_SPEC_VERS_100:
    316 		aprint_normal("1.0");
    317 		break;
    318 	case SDHC_SPEC_VERS_200:
    319 		aprint_normal("2.0");
    320 		break;
    321 	case SDHC_SPEC_VERS_300:
    322 		aprint_normal("3.0");
    323 		break;
    324 	case SDHC_SPEC_VERS_400:
    325 		aprint_normal("4.0");
    326 		break;
    327 	case SDHC_SPEC_NOVERS:
    328 		hp->specver = -1;
    329 		aprint_normal("NO-VERS");
    330 		break;
    331 	default:
    332 		aprint_normal("unknown version(0x%x)",
    333 		    SDHC_SPEC_VERSION(sdhcver));
    334 		break;
    335 	}
    336 	if (SDHC_SPEC_VERSION(sdhcver) != SDHC_SPEC_NOVERS)
    337 		aprint_normal(", rev %u", SDHC_VENDOR_VERSION(sdhcver));
    338 
    339 	/*
    340 	 * Reset the host controller and enable interrupts.
    341 	 */
    342 	(void)sdhc_host_reset(hp);
    343 
    344 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
    345 		/* init uSDHC registers */
    346 		HWRITE4(hp, SDHC_MMC_BOOT, 0);
    347 		HWRITE4(hp, SDHC_HOST_CTL, SDHC_USDHC_BURST_LEN_EN |
    348 		    SDHC_USDHC_HOST_CTL_RESV23 | SDHC_USDHC_EMODE_LE);
    349 		HWRITE4(hp, SDHC_WATERMARK_LEVEL,
    350 		    (0x10 << SDHC_WATERMARK_WR_BRST_SHIFT) |
    351 		    (0x40 << SDHC_WATERMARK_WRITE_SHIFT) |
    352 		    (0x10 << SDHC_WATERMARK_RD_BRST_SHIFT) |
    353 		    (0x40 << SDHC_WATERMARK_READ_SHIFT));
    354 		HSET4(hp, SDHC_VEND_SPEC,
    355 		    SDHC_VEND_SPEC_MBO |
    356 		    SDHC_VEND_SPEC_CARD_CLK_SOFT_EN |
    357 		    SDHC_VEND_SPEC_IPG_PERCLK_SOFT_EN |
    358 		    SDHC_VEND_SPEC_HCLK_SOFT_EN |
    359 		    SDHC_VEND_SPEC_IPG_CLK_SOFT_EN |
    360 		    SDHC_VEND_SPEC_AC12_WR_CHKBUSY_EN |
    361 		    SDHC_VEND_SPEC_FRC_SDCLK_ON);
    362 	}
    363 
    364 	/* Determine host capabilities. */
    365 	if (ISSET(sc->sc_flags, SDHC_FLAG_HOSTCAPS)) {
    366 		caps = sc->sc_caps;
    367 		caps2 = sc->sc_caps2;
    368 	} else if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
    369 		/* uSDHC capability register is little bit different */
    370 		caps = HREAD4(hp, SDHC_CAPABILITIES);
    371 		caps |= SDHC_8BIT_SUPP;
    372 		if (caps & SDHC_ADMA1_SUPP)
    373 			caps |= SDHC_ADMA2_SUPP;
    374 		sc->sc_caps = caps;
    375 		/* uSDHC has no SDHC_CAPABILITIES2 register */
    376 		caps2 = sc->sc_caps2 = SDHC_SDR50_SUPP | SDHC_DDR50_SUPP;
    377 	} else {
    378 		caps = sc->sc_caps = HREAD4(hp, SDHC_CAPABILITIES);
    379 		if (hp->specver >= SDHC_SPEC_VERS_300) {
    380 			caps2 = sc->sc_caps2 = HREAD4(hp, SDHC_CAPABILITIES2);
    381 		} else {
    382 			caps2 = sc->sc_caps2 = 0;
    383 		}
    384 	}
    385 
    386 	const u_int retuning_mode = (caps2 >> SDHC_RETUNING_MODES_SHIFT) &
    387 	    SDHC_RETUNING_MODES_MASK;
    388 	if (retuning_mode == SDHC_RETUNING_MODE_1) {
    389 		hp->tuning_timer_count = (caps2 >> SDHC_TIMER_COUNT_SHIFT) &
    390 		    SDHC_TIMER_COUNT_MASK;
    391 		if (hp->tuning_timer_count == 0xf)
    392 			hp->tuning_timer_count = 0;
    393 		if (hp->tuning_timer_count)
    394 			hp->tuning_timer_count =
    395 			    1 << (hp->tuning_timer_count - 1);
    396 	}
    397 
    398 	/*
    399 	 * Use DMA if the host system and the controller support it.
    400 	 * Suports integrated or external DMA egine, with or without
    401 	 * SDHC_DMA_ENABLE in the command.
    402 	 */
    403 	if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA) ||
    404 	    (ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA &&
    405 	     ISSET(caps, SDHC_DMA_SUPPORT)))) {
    406 		SET(hp->flags, SHF_USE_DMA);
    407 
    408 		if (ISSET(sc->sc_flags, SDHC_FLAG_USE_ADMA2) &&
    409 		    ISSET(caps, SDHC_ADMA2_SUPP)) {
    410 			SET(hp->flags, SHF_MODE_DMAEN);
    411 			/*
    412 			 * 64-bit mode was present in the 2.00 spec, removed
    413 			 * from 3.00, and re-added in 4.00 with a different
    414 			 * descriptor layout. We only support 2.00 and 3.00
    415 			 * descriptors for now.
    416 			 */
    417 			if (hp->specver == SDHC_SPEC_VERS_200 &&
    418 			    ISSET(caps, SDHC_64BIT_SYS_BUS)) {
    419 				SET(hp->flags, SHF_USE_ADMA2_64);
    420 				aprint_normal(", 64-bit ADMA2");
    421 			} else {
    422 				SET(hp->flags, SHF_USE_ADMA2_32);
    423 				aprint_normal(", 32-bit ADMA2");
    424 			}
    425 		} else {
    426 			if (!ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA) ||
    427 			    ISSET(sc->sc_flags, SDHC_FLAG_EXTDMA_DMAEN))
    428 				SET(hp->flags, SHF_MODE_DMAEN);
    429 			if (sc->sc_vendor_transfer_data_dma) {
    430 				aprint_normal(", platform DMA");
    431 			} else {
    432 				aprint_normal(", SDMA");
    433 			}
    434 		}
    435 	} else {
    436 		aprint_normal(", PIO");
    437 	}
    438 
    439 	/*
    440 	 * Determine the base clock frequency. (2.2.24)
    441 	 */
    442 	if (hp->specver >= SDHC_SPEC_VERS_300) {
    443 		hp->clkbase = SDHC_BASE_V3_FREQ_KHZ(caps);
    444 	} else {
    445 		hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
    446 	}
    447 	if (hp->clkbase == 0 ||
    448 	    ISSET(sc->sc_flags, SDHC_FLAG_NO_CLKBASE)) {
    449 		if (sc->sc_clkbase == 0) {
    450 			/* The attachment driver must tell us. */
    451 			aprint_error_dev(sc->sc_dev,
    452 			    "unknown base clock frequency\n");
    453 			goto err;
    454 		}
    455 		hp->clkbase = sc->sc_clkbase;
    456 	}
    457 	if (hp->clkbase < 10000 || hp->clkbase > 10000 * 256) {
    458 		/* SDHC 1.0 supports only 10-63 MHz. */
    459 		aprint_error_dev(sc->sc_dev,
    460 		    "base clock frequency out of range: %u MHz\n",
    461 		    hp->clkbase / 1000);
    462 		goto err;
    463 	}
    464 	aprint_normal(", %u kHz", hp->clkbase);
    465 
    466 	/*
    467 	 * XXX Set the data timeout counter value according to
    468 	 * capabilities. (2.2.15)
    469 	 */
    470 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    471 #if 1
    472 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    473 		HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
    474 #endif
    475 
    476 	if (ISSET(caps, SDHC_EMBEDDED_SLOT))
    477 		aprint_normal(", embedded slot");
    478 
    479 	/*
    480 	 * Determine SD bus voltage levels supported by the controller.
    481 	 */
    482 	aprint_normal(",");
    483 	if (ISSET(caps, SDHC_HIGH_SPEED_SUPP)) {
    484 		SET(hp->ocr, MMC_OCR_HCS);
    485 		aprint_normal(" HS");
    486 	}
    487 	if (ISSET(caps2, SDHC_SDR50_SUPP)) {
    488 		SET(hp->ocr, MMC_OCR_S18A);
    489 		aprint_normal(" SDR50");
    490 	}
    491 	if (ISSET(caps2, SDHC_DDR50_SUPP)) {
    492 		SET(hp->ocr, MMC_OCR_S18A);
    493 		aprint_normal(" DDR50");
    494 	}
    495 	if (ISSET(caps2, SDHC_SDR104_SUPP)) {
    496 		SET(hp->ocr, MMC_OCR_S18A);
    497 		aprint_normal(" SDR104 HS200");
    498 	}
    499 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V)) {
    500 		SET(hp->ocr, MMC_OCR_1_65V_1_95V);
    501 		aprint_normal(" 1.8V");
    502 	}
    503 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V)) {
    504 		SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
    505 		aprint_normal(" 3.0V");
    506 	}
    507 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V)) {
    508 		SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
    509 		aprint_normal(" 3.3V");
    510 	}
    511 	if (hp->specver >= SDHC_SPEC_VERS_300) {
    512 		aprint_normal(", re-tuning mode %d", retuning_mode + 1);
    513 		if (hp->tuning_timer_count)
    514 			aprint_normal(" (%us timer)", hp->tuning_timer_count);
    515 	}
    516 
    517 	/*
    518 	 * Determine the maximum block length supported by the host
    519 	 * controller. (2.2.24)
    520 	 */
    521 	switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
    522 	case SDHC_MAX_BLK_LEN_512:
    523 		hp->maxblklen = 512;
    524 		break;
    525 
    526 	case SDHC_MAX_BLK_LEN_1024:
    527 		hp->maxblklen = 1024;
    528 		break;
    529 
    530 	case SDHC_MAX_BLK_LEN_2048:
    531 		hp->maxblklen = 2048;
    532 		break;
    533 
    534 	case SDHC_MAX_BLK_LEN_4096:
    535 		hp->maxblklen = 4096;
    536 		break;
    537 
    538 	default:
    539 		aprint_error_dev(sc->sc_dev, "max block length unknown\n");
    540 		goto err;
    541 	}
    542 	aprint_normal(", %u byte blocks", hp->maxblklen);
    543 	aprint_normal("\n");
    544 
    545 	if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
    546 		int rseg;
    547 
    548 		/* Allocate ADMA2 descriptor memory */
    549 		error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
    550 		    PAGE_SIZE, hp->adma_segs, 1, &rseg, BUS_DMA_WAITOK);
    551 		if (error) {
    552 			aprint_error_dev(sc->sc_dev,
    553 			    "ADMA2 dmamem_alloc failed (%d)\n", error);
    554 			goto adma_done;
    555 		}
    556 		error = bus_dmamem_map(sc->sc_dmat, hp->adma_segs, rseg,
    557 		    PAGE_SIZE, (void **)&hp->adma2, BUS_DMA_WAITOK);
    558 		if (error) {
    559 			aprint_error_dev(sc->sc_dev,
    560 			    "ADMA2 dmamem_map failed (%d)\n", error);
    561 			goto adma_done;
    562 		}
    563 		error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
    564 		    0, BUS_DMA_WAITOK, &hp->adma_map);
    565 		if (error) {
    566 			aprint_error_dev(sc->sc_dev,
    567 			    "ADMA2 dmamap_create failed (%d)\n", error);
    568 			goto adma_done;
    569 		}
    570 		error = bus_dmamap_load(sc->sc_dmat, hp->adma_map,
    571 		    hp->adma2, PAGE_SIZE, NULL,
    572 		    BUS_DMA_WAITOK|BUS_DMA_WRITE);
    573 		if (error) {
    574 			aprint_error_dev(sc->sc_dev,
    575 			    "ADMA2 dmamap_load failed (%d)\n", error);
    576 			goto adma_done;
    577 		}
    578 
    579 		memset(hp->adma2, 0, PAGE_SIZE);
    580 
    581 adma_done:
    582 		if (error)
    583 			CLR(hp->flags, SHF_USE_ADMA2_MASK);
    584 	}
    585 
    586 	/*
    587 	 * Attach the generic SD/MMC bus driver.  (The bus driver must
    588 	 * not invoke any chipset functions before it is attached.)
    589 	 */
    590 	memset(&saa, 0, sizeof(saa));
    591 	saa.saa_busname = "sdmmc";
    592 	saa.saa_sct = &sdhc_functions;
    593 	saa.saa_sch = hp;
    594 	saa.saa_dmat = hp->dmat;
    595 	saa.saa_clkmax = hp->clkbase;
    596 	if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_CGM))
    597 		saa.saa_clkmin = hp->clkbase / 256 / 2046;
    598 	else if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_DVS))
    599 		saa.saa_clkmin = hp->clkbase / 256 / 16;
    600 	else if (hp->sc->sc_clkmsk != 0)
    601 		saa.saa_clkmin = hp->clkbase / (hp->sc->sc_clkmsk >>
    602 		    (ffs(hp->sc->sc_clkmsk) - 1));
    603 	else if (hp->specver >= SDHC_SPEC_VERS_300)
    604 		saa.saa_clkmin = hp->clkbase / 0x3ff;
    605 	else
    606 		saa.saa_clkmin = hp->clkbase / 256;
    607 	if (!ISSET(sc->sc_flags, SDHC_FLAG_NO_AUTO_STOP))
    608 		saa.saa_caps |= SMC_CAPS_AUTO_STOP;
    609 	saa.saa_caps |= SMC_CAPS_4BIT_MODE;
    610 	if (ISSET(sc->sc_flags, SDHC_FLAG_8BIT_MODE))
    611 		saa.saa_caps |= SMC_CAPS_8BIT_MODE;
    612 	if (ISSET(caps, SDHC_HIGH_SPEED_SUPP))
    613 		saa.saa_caps |= SMC_CAPS_SD_HIGHSPEED;
    614 	if (ISSET(caps2, SDHC_SDR104_SUPP))
    615 		saa.saa_caps |= SMC_CAPS_UHS_SDR104 |
    616 				SMC_CAPS_UHS_SDR50 |
    617 				SMC_CAPS_MMC_HS200;
    618 	if (ISSET(caps2, SDHC_SDR50_SUPP))
    619 		saa.saa_caps |= SMC_CAPS_UHS_SDR50;
    620 	if (ISSET(caps2, SDHC_DDR50_SUPP))
    621 		saa.saa_caps |= SMC_CAPS_UHS_DDR50;
    622 	if (ISSET(hp->flags, SHF_USE_DMA)) {
    623 		saa.saa_caps |= SMC_CAPS_DMA;
    624 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    625 			saa.saa_caps |= SMC_CAPS_MULTI_SEG_DMA;
    626 	}
    627 	if (ISSET(sc->sc_flags, SDHC_FLAG_SINGLE_ONLY))
    628 		saa.saa_caps |= SMC_CAPS_SINGLE_ONLY;
    629 	if (ISSET(sc->sc_flags, SDHC_FLAG_POLL_CARD_DET))
    630 		saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
    631 	hp->sdmmc = config_found(sc->sc_dev, &saa, sdhc_cfprint);
    632 
    633 	return 0;
    634 
    635 err:
    636 	callout_destroy(&hp->tuning_timer);
    637 	cv_destroy(&hp->intr_cv);
    638 	mutex_destroy(&hp->intr_lock);
    639 	free(hp, M_DEVBUF);
    640 	sc->sc_host[--sc->sc_nhosts] = NULL;
    641 err1:
    642 	return 1;
    643 }
    644 
    645 int
    646 sdhc_detach(struct sdhc_softc *sc, int flags)
    647 {
    648 	struct sdhc_host *hp;
    649 	int rv = 0;
    650 
    651 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    652 		hp = sc->sc_host[n];
    653 		if (hp == NULL)
    654 			continue;
    655 		if (hp->sdmmc != NULL) {
    656 			rv = config_detach(hp->sdmmc, flags);
    657 			if (rv)
    658 				break;
    659 			hp->sdmmc = NULL;
    660 		}
    661 		/* disable interrupts */
    662 		if ((flags & DETACH_FORCE) == 0) {
    663 			mutex_enter(&hp->intr_lock);
    664 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    665 				HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
    666 			} else {
    667 				HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
    668 			}
    669 			sdhc_soft_reset(hp, SDHC_RESET_ALL);
    670 			mutex_exit(&hp->intr_lock);
    671 		}
    672 		callout_halt(&hp->tuning_timer, NULL);
    673 		callout_destroy(&hp->tuning_timer);
    674 		cv_destroy(&hp->intr_cv);
    675 		mutex_destroy(&hp->intr_lock);
    676 		if (hp->ios > 0) {
    677 			bus_space_unmap(hp->iot, hp->ioh, hp->ios);
    678 			hp->ios = 0;
    679 		}
    680 		if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
    681 			bus_dmamap_unload(sc->sc_dmat, hp->adma_map);
    682 			bus_dmamap_destroy(sc->sc_dmat, hp->adma_map);
    683 			bus_dmamem_unmap(sc->sc_dmat, hp->adma2, PAGE_SIZE);
    684 			bus_dmamem_free(sc->sc_dmat, hp->adma_segs, 1);
    685 		}
    686 		free(hp, M_DEVBUF);
    687 		sc->sc_host[n] = NULL;
    688 	}
    689 
    690 	return rv;
    691 }
    692 
    693 bool
    694 sdhc_suspend(device_t dev, const pmf_qual_t *qual)
    695 {
    696 	struct sdhc_softc *sc = device_private(dev);
    697 	struct sdhc_host *hp;
    698 	size_t i;
    699 
    700 	/* XXX poll for command completion or suspend command
    701 	 * in progress */
    702 
    703 	/* Save the host controller state. */
    704 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    705 		hp = sc->sc_host[n];
    706 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    707 			for (i = 0; i < sizeof hp->regs; i += 4) {
    708 				uint32_t v = HREAD4(hp, i);
    709 				hp->regs[i + 0] = (v >> 0);
    710 				hp->regs[i + 1] = (v >> 8);
    711 				if (i + 3 < sizeof hp->regs) {
    712 					hp->regs[i + 2] = (v >> 16);
    713 					hp->regs[i + 3] = (v >> 24);
    714 				}
    715 			}
    716 		} else {
    717 			for (i = 0; i < sizeof hp->regs; i++) {
    718 				hp->regs[i] = HREAD1(hp, i);
    719 			}
    720 		}
    721 	}
    722 	return true;
    723 }
    724 
    725 bool
    726 sdhc_resume(device_t dev, const pmf_qual_t *qual)
    727 {
    728 	struct sdhc_softc *sc = device_private(dev);
    729 	struct sdhc_host *hp;
    730 	size_t i;
    731 
    732 	/* Restore the host controller state. */
    733 	for (size_t n = 0; n < sc->sc_nhosts; n++) {
    734 		hp = sc->sc_host[n];
    735 		(void)sdhc_host_reset(hp);
    736 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    737 			for (i = 0; i < sizeof hp->regs; i += 4) {
    738 				if (i + 3 < sizeof hp->regs) {
    739 					HWRITE4(hp, i,
    740 					    (hp->regs[i + 0] << 0)
    741 					    | (hp->regs[i + 1] << 8)
    742 					    | (hp->regs[i + 2] << 16)
    743 					    | (hp->regs[i + 3] << 24));
    744 				} else {
    745 					HWRITE4(hp, i,
    746 					    (hp->regs[i + 0] << 0)
    747 					    | (hp->regs[i + 1] << 8));
    748 				}
    749 			}
    750 		} else {
    751 			for (i = 0; i < sizeof hp->regs; i++) {
    752 				HWRITE1(hp, i, hp->regs[i]);
    753 			}
    754 		}
    755 	}
    756 	return true;
    757 }
    758 
    759 bool
    760 sdhc_shutdown(device_t dev, int flags)
    761 {
    762 	struct sdhc_softc *sc = device_private(dev);
    763 	struct sdhc_host *hp;
    764 
    765 	/* XXX chip locks up if we don't disable it before reboot. */
    766 	for (size_t i = 0; i < sc->sc_nhosts; i++) {
    767 		hp = sc->sc_host[i];
    768 		(void)sdhc_host_reset(hp);
    769 	}
    770 	return true;
    771 }
    772 
    773 /*
    774  * Reset the host controller.  Called during initialization, when
    775  * cards are removed, upon resume, and during error recovery.
    776  */
    777 static int
    778 sdhc_host_reset1(sdmmc_chipset_handle_t sch)
    779 {
    780 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    781 	uint32_t sdhcimask;
    782 	int error;
    783 
    784 	KASSERT(mutex_owned(&hp->intr_lock));
    785 
    786 	/* Disable all interrupts. */
    787 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    788 		HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
    789 	} else {
    790 		HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
    791 	}
    792 
    793 	/* Let sdhc_bus_power restore power */
    794 	hp->vdd = 0;
    795 
    796 	/*
    797 	 * Reset the entire host controller and wait up to 100ms for
    798 	 * the controller to clear the reset bit.
    799 	 */
    800 	error = sdhc_soft_reset(hp, SDHC_RESET_ALL);
    801 	if (error)
    802 		goto out;
    803 
    804 	/* Set data timeout counter value to max for now. */
    805 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
    806 #if 1
    807 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
    808 		HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
    809 #endif
    810 
    811 	/* Enable interrupts. */
    812 	sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
    813 	    SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
    814 	    SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
    815 	    SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
    816 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
    817 		sdhcimask |= SDHC_EINTR_STATUS_MASK << 16;
    818 		HWRITE4(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
    819 		sdhcimask ^=
    820 		    (SDHC_EINTR_STATUS_MASK ^ SDHC_EINTR_SIGNAL_MASK) << 16;
    821 		sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
    822 		HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
    823 	} else {
    824 		HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
    825 		HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
    826 		sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
    827 		HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
    828 		HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
    829 	}
    830 
    831 out:
    832 	return error;
    833 }
    834 
    835 static int
    836 sdhc_host_reset(sdmmc_chipset_handle_t sch)
    837 {
    838 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    839 	int error;
    840 
    841 	mutex_enter(&hp->intr_lock);
    842 	error = sdhc_host_reset1(sch);
    843 	mutex_exit(&hp->intr_lock);
    844 
    845 	return error;
    846 }
    847 
    848 static uint32_t
    849 sdhc_host_ocr(sdmmc_chipset_handle_t sch)
    850 {
    851 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    852 
    853 	return hp->ocr;
    854 }
    855 
    856 static int
    857 sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
    858 {
    859 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    860 
    861 	return hp->maxblklen;
    862 }
    863 
    864 /*
    865  * Return non-zero if the card is currently inserted.
    866  */
    867 static int
    868 sdhc_card_detect(sdmmc_chipset_handle_t sch)
    869 {
    870 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    871 	int r;
    872 
    873 	if (hp->sc->sc_vendor_card_detect)
    874 		return (*hp->sc->sc_vendor_card_detect)(hp->sc);
    875 
    876 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED);
    877 
    878 	return r ? 1 : 0;
    879 }
    880 
    881 /*
    882  * Return non-zero if the card is currently write-protected.
    883  */
    884 static int
    885 sdhc_write_protect(sdmmc_chipset_handle_t sch)
    886 {
    887 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    888 	int r;
    889 
    890 	if (hp->sc->sc_vendor_write_protect)
    891 		return (*hp->sc->sc_vendor_write_protect)(hp->sc);
    892 
    893 	r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH);
    894 
    895 	return r ? 0 : 1;
    896 }
    897 
    898 /*
    899  * Set or change SD bus voltage and enable or disable SD bus power.
    900  * Return zero on success.
    901  */
    902 static int
    903 sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
    904 {
    905 	struct sdhc_host *hp = (struct sdhc_host *)sch;
    906 	uint8_t vdd;
    907 	int error = 0;
    908 	const uint32_t pcmask =
    909 	    ~(SDHC_BUS_POWER | (SDHC_VOLTAGE_MASK << SDHC_VOLTAGE_SHIFT));
    910 	uint32_t reg;
    911 
    912 	mutex_enter(&hp->intr_lock);
    913 
    914 	/*
    915 	 * Disable bus power before voltage change.
    916 	 */
    917 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)
    918 	    && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_PWR0)) {
    919 		hp->vdd = 0;
    920 		HWRITE1(hp, SDHC_POWER_CTL, 0);
    921 	}
    922 
    923 	/* If power is disabled, reset the host and return now. */
    924 	if (ocr == 0) {
    925 		(void)sdhc_host_reset1(hp);
    926 		callout_halt(&hp->tuning_timer, &hp->intr_lock);
    927 		goto out;
    928 	}
    929 
    930 	/*
    931 	 * Select the lowest voltage according to capabilities.
    932 	 */
    933 	ocr &= hp->ocr;
    934 	if (ISSET(ocr, MMC_OCR_1_65V_1_95V)) {
    935 		vdd = SDHC_VOLTAGE_1_8V;
    936 	} else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V)) {
    937 		vdd = SDHC_VOLTAGE_3_0V;
    938 	} else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) {
    939 		vdd = SDHC_VOLTAGE_3_3V;
    940 	} else {
    941 		/* Unsupported voltage level requested. */
    942 		error = EINVAL;
    943 		goto out;
    944 	}
    945 
    946 	/*
    947 	 * Did voltage change ?
    948 	 */
    949 	if (vdd == hp->vdd)
    950 		goto out;
    951 
    952 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
    953 		/*
    954 		 * Enable bus power.  Wait at least 1 ms (or 74 clocks) plus
    955 		 * voltage ramp until power rises.
    956 		 */
    957 
    958 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_SINGLE_POWER_WRITE)) {
    959 			HWRITE1(hp, SDHC_POWER_CTL,
    960 			    (vdd << SDHC_VOLTAGE_SHIFT) | SDHC_BUS_POWER);
    961 		} else {
    962 			reg = HREAD1(hp, SDHC_POWER_CTL) & pcmask;
    963 			HWRITE1(hp, SDHC_POWER_CTL, reg);
    964 			sdmmc_delay(1);
    965 			reg |= (vdd << SDHC_VOLTAGE_SHIFT);
    966 			HWRITE1(hp, SDHC_POWER_CTL, reg);
    967 			sdmmc_delay(1);
    968 			reg |= SDHC_BUS_POWER;
    969 			HWRITE1(hp, SDHC_POWER_CTL, reg);
    970 			sdmmc_delay(10000);
    971 		}
    972 
    973 		/*
    974 		 * The host system may not power the bus due to battery low,
    975 		 * etc.  In that case, the host controller should clear the
    976 		 * bus power bit.
    977 		 */
    978 		if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
    979 			error = ENXIO;
    980 			goto out;
    981 		}
    982 	}
    983 
    984 	/* power successfully changed */
    985 	hp->vdd = vdd;
    986 
    987 out:
    988 	mutex_exit(&hp->intr_lock);
    989 
    990 	return error;
    991 }
    992 
    993 /*
    994  * Return the smallest possible base clock frequency divisor value
    995  * for the CLOCK_CTL register to produce `freq' (KHz).
    996  */
    997 static bool
    998 sdhc_clock_divisor(struct sdhc_host *hp, u_int freq, u_int *divp)
    999 {
   1000 	u_int div;
   1001 
   1002 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_CGM)) {
   1003 		for (div = hp->clkbase / freq; div <= 0x3ff; div++) {
   1004 			if ((hp->clkbase / div) <= freq) {
   1005 				*divp = SDHC_SDCLK_CGM
   1006 				    | ((div & 0x300) << SDHC_SDCLK_XDIV_SHIFT)
   1007 				    | ((div & 0x0ff) << SDHC_SDCLK_DIV_SHIFT);
   1008 				//freq = hp->clkbase / div;
   1009 				return true;
   1010 			}
   1011 		}
   1012 		/* No divisor found. */
   1013 		return false;
   1014 	}
   1015 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_DVS)) {
   1016 		u_int dvs = (hp->clkbase + freq - 1) / freq;
   1017 		u_int roundup = dvs & 1;
   1018 		for (dvs >>= 1, div = 1; div <= 256; div <<= 1, dvs >>= 1) {
   1019 			if (dvs + roundup <= 16) {
   1020 				dvs += roundup - 1;
   1021 				*divp = (div << SDHC_SDCLK_DIV_SHIFT)
   1022 				    |   (dvs << SDHC_SDCLK_DVS_SHIFT);
   1023 				DPRINTF(2,
   1024 				    ("%s: divisor for freq %u is %u * %u\n",
   1025 				    HDEVNAME(hp), freq, div * 2, dvs + 1));
   1026 				//freq = hp->clkbase / (div * 2) * (dvs + 1);
   1027 				return true;
   1028 			}
   1029 			/*
   1030 			 * If we drop bits, we need to round up the divisor.
   1031 			 */
   1032 			roundup |= dvs & 1;
   1033 		}
   1034 		/* No divisor found. */
   1035 		return false;
   1036 	}
   1037 	if (hp->sc->sc_clkmsk != 0) {
   1038 		div = howmany(hp->clkbase, freq);
   1039 		if (div > (hp->sc->sc_clkmsk >> (ffs(hp->sc->sc_clkmsk) - 1)))
   1040 			return false;
   1041 		*divp = div << (ffs(hp->sc->sc_clkmsk) - 1);
   1042 		//freq = hp->clkbase / div;
   1043 		return true;
   1044 	}
   1045 	if (hp->specver >= SDHC_SPEC_VERS_300) {
   1046 		div = howmany(hp->clkbase, freq);
   1047 		div = div > 1 ? howmany(div, 2) : 0;
   1048 		if (div > 0x3ff)
   1049 			return false;
   1050 		*divp = (((div >> 8) & SDHC_SDCLK_XDIV_MASK)
   1051 			 << SDHC_SDCLK_XDIV_SHIFT) |
   1052 			(((div >> 0) & SDHC_SDCLK_DIV_MASK)
   1053 			 << SDHC_SDCLK_DIV_SHIFT);
   1054 		//freq = hp->clkbase / (div ? div * 2 : 1);
   1055 		return true;
   1056 	} else {
   1057 		for (div = 1; div <= 256; div *= 2) {
   1058 			if ((hp->clkbase / div) <= freq) {
   1059 				*divp = (div / 2) << SDHC_SDCLK_DIV_SHIFT;
   1060 				//freq = hp->clkbase / div;
   1061 				return true;
   1062 			}
   1063 		}
   1064 		/* No divisor found. */
   1065 		return false;
   1066 	}
   1067 	/* No divisor found. */
   1068 	return false;
   1069 }
   1070 
   1071 /*
   1072  * Set or change SDCLK frequency or disable the SD clock.
   1073  * Return zero on success.
   1074  */
   1075 static int
   1076 sdhc_bus_clock_ddr(sdmmc_chipset_handle_t sch, int freq, bool ddr)
   1077 {
   1078 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1079 	u_int div;
   1080 	u_int timo;
   1081 	int16_t reg;
   1082 	int error = 0;
   1083 	bool present __diagused;
   1084 
   1085 	mutex_enter(&hp->intr_lock);
   1086 
   1087 #ifdef DIAGNOSTIC
   1088 	present = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK);
   1089 
   1090 	/* Must not stop the clock if commands are in progress. */
   1091 	if (present && sdhc_card_detect(hp)) {
   1092 		aprint_normal_dev(hp->sc->sc_dev,
   1093 		    "%s: command in progress\n", __func__);
   1094 	}
   1095 #endif
   1096 
   1097 	if (hp->sc->sc_vendor_bus_clock) {
   1098 		error = (*hp->sc->sc_vendor_bus_clock)(hp->sc, freq);
   1099 		if (error != 0)
   1100 			goto out;
   1101 	}
   1102 
   1103 	/*
   1104 	 * Stop SD clock before changing the frequency.
   1105 	 */
   1106 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
   1107 		HCLR4(hp, SDHC_VEND_SPEC,
   1108 		    SDHC_VEND_SPEC_CARD_CLK_SOFT_EN |
   1109 		    SDHC_VEND_SPEC_FRC_SDCLK_ON);
   1110 		if (freq == SDMMC_SDCLK_OFF) {
   1111 			goto out;
   1112 		}
   1113 	} else if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1114 		HCLR4(hp, SDHC_CLOCK_CTL, 0xfff8);
   1115 		if (freq == SDMMC_SDCLK_OFF) {
   1116 			HSET4(hp, SDHC_CLOCK_CTL, 0x80f0);
   1117 			goto out;
   1118 		}
   1119 	} else {
   1120 		HCLR2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
   1121 		if (freq == SDMMC_SDCLK_OFF)
   1122 			goto out;
   1123 	}
   1124 
   1125 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
   1126 		if (ddr)
   1127 			HSET4(hp, SDHC_MIX_CTRL, SDHC_USDHC_DDR_EN);
   1128 		else
   1129 			HCLR4(hp, SDHC_MIX_CTRL, SDHC_USDHC_DDR_EN);
   1130 	} else if (hp->specver >= SDHC_SPEC_VERS_300) {
   1131 		HCLR2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_MASK);
   1132 		if (freq > 100000) {
   1133 			HSET2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_SDR104);
   1134 		} else if (freq > 50000) {
   1135 			if (ddr) {
   1136 				HSET2(hp, SDHC_HOST_CTL2,
   1137 				    SDHC_UHS_MODE_SELECT_DDR50);
   1138 			} else {
   1139 				HSET2(hp, SDHC_HOST_CTL2,
   1140 				    SDHC_UHS_MODE_SELECT_SDR50);
   1141 			}
   1142 		} else if (freq > 25000) {
   1143 			if (ddr) {
   1144 				HSET2(hp, SDHC_HOST_CTL2,
   1145 				    SDHC_UHS_MODE_SELECT_DDR50);
   1146 			} else {
   1147 				HSET2(hp, SDHC_HOST_CTL2,
   1148 				    SDHC_UHS_MODE_SELECT_SDR25);
   1149 			}
   1150 		} else if (freq > 400) {
   1151 			HSET2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_SDR12);
   1152 		}
   1153 	}
   1154 
   1155 	/*
   1156 	 * Slow down Ricoh 5U823 controller that isn't reliable
   1157 	 * at 100MHz bus clock.
   1158 	 */
   1159 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_SLOW_SDR50)) {
   1160 		if (freq == 100000)
   1161 			--freq;
   1162 	}
   1163 
   1164 	/*
   1165 	 * Set the minimum base clock frequency divisor.
   1166 	 */
   1167 	if (!sdhc_clock_divisor(hp, freq, &div)) {
   1168 		/* Invalid base clock frequency or `freq' value. */
   1169 		aprint_error_dev(hp->sc->sc_dev,
   1170 			"Invalid bus clock %d kHz\n", freq);
   1171 		error = EINVAL;
   1172 		goto out;
   1173 	}
   1174 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
   1175 		if (ddr) {
   1176 			/* in ddr mode, divisor >>= 1 */
   1177 			div = ((div >> 1) & (SDHC_SDCLK_DIV_MASK <<
   1178 			    SDHC_SDCLK_DIV_SHIFT)) |
   1179 			    (div & (SDHC_SDCLK_DVS_MASK <<
   1180 			    SDHC_SDCLK_DVS_SHIFT));
   1181 		}
   1182 		for (timo = 1000; timo > 0; timo--) {
   1183 			if (ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_SDSTB))
   1184 				break;
   1185 			sdmmc_delay(10);
   1186 		}
   1187 		HWRITE4(hp, SDHC_CLOCK_CTL,
   1188 		    div | (SDHC_TIMEOUT_MAX << 16) | 0x0f);
   1189 	} else if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1190 		HWRITE4(hp, SDHC_CLOCK_CTL,
   1191 		    div | (SDHC_TIMEOUT_MAX << 16));
   1192 	} else {
   1193 		reg = HREAD2(hp, SDHC_CLOCK_CTL);
   1194 		reg &= (SDHC_INTCLK_STABLE | SDHC_INTCLK_ENABLE);
   1195 		HWRITE2(hp, SDHC_CLOCK_CTL, reg | div);
   1196 	}
   1197 
   1198 	/*
   1199 	 * Start internal clock.  Wait 10ms for stabilization.
   1200 	 */
   1201 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
   1202 		HSET4(hp, SDHC_VEND_SPEC,
   1203 		    SDHC_VEND_SPEC_CARD_CLK_SOFT_EN |
   1204 		    SDHC_VEND_SPEC_FRC_SDCLK_ON);
   1205 	} else if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1206 		sdmmc_delay(10000);
   1207 		HSET4(hp, SDHC_CLOCK_CTL,
   1208 		    8 | SDHC_INTCLK_ENABLE | SDHC_INTCLK_STABLE);
   1209 	} else {
   1210 		HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
   1211 		for (timo = 1000; timo > 0; timo--) {
   1212 			if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL),
   1213 			    SDHC_INTCLK_STABLE))
   1214 				break;
   1215 			sdmmc_delay(10);
   1216 		}
   1217 		if (timo == 0) {
   1218 			error = ETIMEDOUT;
   1219 			DPRINTF(1,("%s: timeout\n", __func__));
   1220 			goto out;
   1221 		}
   1222 	}
   1223 
   1224 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   1225 		HSET1(hp, SDHC_SOFTWARE_RESET, SDHC_INIT_ACTIVE);
   1226 		/*
   1227 		 * Sending 80 clocks at 400kHz takes 200us.
   1228 		 * So delay for that time + slop and then
   1229 		 * check a few times for completion.
   1230 		 */
   1231 		sdmmc_delay(210);
   1232 		for (timo = 10; timo > 0; timo--) {
   1233 			if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET),
   1234 			    SDHC_INIT_ACTIVE))
   1235 				break;
   1236 			sdmmc_delay(10);
   1237 		}
   1238 		DPRINTF(2,("%s: %u init spins\n", __func__, 10 - timo));
   1239 
   1240 		/*
   1241 		 * Enable SD clock.
   1242 		 */
   1243 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
   1244 			HSET4(hp, SDHC_VEND_SPEC,
   1245 			    SDHC_VEND_SPEC_CARD_CLK_SOFT_EN |
   1246 			    SDHC_VEND_SPEC_FRC_SDCLK_ON);
   1247 		} else {
   1248 			HSET4(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
   1249 		}
   1250 	} else {
   1251 		/*
   1252 		 * Enable SD clock.
   1253 		 */
   1254 		HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
   1255 
   1256 		if (freq > 25000 &&
   1257 		    !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_HS_BIT))
   1258 			HSET1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
   1259 		else
   1260 			HCLR1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
   1261 	}
   1262 
   1263 	if (hp->sc->sc_vendor_bus_clock_post) {
   1264 		error = (*hp->sc->sc_vendor_bus_clock_post)(hp->sc, freq);
   1265 		if (error != 0)
   1266 			goto out;
   1267 	}
   1268 
   1269 out:
   1270 	mutex_exit(&hp->intr_lock);
   1271 
   1272 	return error;
   1273 }
   1274 
   1275 static int
   1276 sdhc_bus_width(sdmmc_chipset_handle_t sch, int width)
   1277 {
   1278 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1279 	int reg;
   1280 
   1281 	switch (width) {
   1282 	case 1:
   1283 	case 4:
   1284 		break;
   1285 
   1286 	case 8:
   1287 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_8BIT_MODE))
   1288 			break;
   1289 		/* FALLTHROUGH */
   1290 	default:
   1291 		DPRINTF(0,("%s: unsupported bus width (%d)\n",
   1292 		    HDEVNAME(hp), width));
   1293 		return 1;
   1294 	}
   1295 
   1296 	if (hp->sc->sc_vendor_bus_width) {
   1297 		const int error = hp->sc->sc_vendor_bus_width(hp->sc, width);
   1298 		if (error != 0)
   1299 			return error;
   1300 	}
   1301 
   1302 	mutex_enter(&hp->intr_lock);
   1303 
   1304 	reg = HREAD1(hp, SDHC_HOST_CTL);
   1305 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   1306 		reg &= ~(SDHC_4BIT_MODE|SDHC_ESDHC_8BIT_MODE);
   1307 		if (width == 4)
   1308 			reg |= SDHC_4BIT_MODE;
   1309 		else if (width == 8)
   1310 			reg |= SDHC_ESDHC_8BIT_MODE;
   1311 	} else {
   1312 		reg &= ~SDHC_4BIT_MODE;
   1313 		if (hp->specver >= SDHC_SPEC_VERS_300) {
   1314 			reg &= ~SDHC_8BIT_MODE;
   1315 		}
   1316 		if (width == 4) {
   1317 			reg |= SDHC_4BIT_MODE;
   1318 		} else if (width == 8 && hp->specver >= SDHC_SPEC_VERS_300) {
   1319 			reg |= SDHC_8BIT_MODE;
   1320 		}
   1321 	}
   1322 	HWRITE1(hp, SDHC_HOST_CTL, reg);
   1323 
   1324 	mutex_exit(&hp->intr_lock);
   1325 
   1326 	return 0;
   1327 }
   1328 
   1329 static int
   1330 sdhc_bus_rod(sdmmc_chipset_handle_t sch, int on)
   1331 {
   1332 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1333 
   1334 	if (hp->sc->sc_vendor_rod)
   1335 		return (*hp->sc->sc_vendor_rod)(hp->sc, on);
   1336 
   1337 	return 0;
   1338 }
   1339 
   1340 static void
   1341 sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
   1342 {
   1343 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1344 
   1345 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   1346 		mutex_enter(&hp->intr_lock);
   1347 		if (enable) {
   1348 			HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1349 			HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
   1350 		} else {
   1351 			HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
   1352 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1353 		}
   1354 		mutex_exit(&hp->intr_lock);
   1355 	}
   1356 }
   1357 
   1358 static void
   1359 sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
   1360 {
   1361 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1362 
   1363 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   1364 		mutex_enter(&hp->intr_lock);
   1365 		HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   1366 		mutex_exit(&hp->intr_lock);
   1367 	}
   1368 }
   1369 
   1370 static int
   1371 sdhc_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
   1372 {
   1373 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1374 	int error = 0;
   1375 
   1376 	if (hp->specver < SDHC_SPEC_VERS_300)
   1377 		return EINVAL;
   1378 
   1379 	mutex_enter(&hp->intr_lock);
   1380 	switch (signal_voltage) {
   1381 	case SDMMC_SIGNAL_VOLTAGE_180:
   1382 		if (hp->sc->sc_vendor_signal_voltage != NULL) {
   1383 			error = hp->sc->sc_vendor_signal_voltage(hp->sc,
   1384 			    signal_voltage);
   1385 			if (error != 0)
   1386 				break;
   1387 		}
   1388 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC))
   1389 			HSET2(hp, SDHC_HOST_CTL2, SDHC_1_8V_SIGNAL_EN);
   1390 		break;
   1391 	case SDMMC_SIGNAL_VOLTAGE_330:
   1392 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC))
   1393 			HCLR2(hp, SDHC_HOST_CTL2, SDHC_1_8V_SIGNAL_EN);
   1394 		if (hp->sc->sc_vendor_signal_voltage != NULL) {
   1395 			error = hp->sc->sc_vendor_signal_voltage(hp->sc,
   1396 			    signal_voltage);
   1397 			if (error != 0)
   1398 				break;
   1399 		}
   1400 		break;
   1401 	default:
   1402 		error = EINVAL;
   1403 		break;
   1404 	}
   1405 	mutex_exit(&hp->intr_lock);
   1406 
   1407 	return error;
   1408 }
   1409 
   1410 /*
   1411  * Sampling clock tuning procedure (UHS)
   1412  */
   1413 static int
   1414 sdhc_execute_tuning1(struct sdhc_host *hp, int timing)
   1415 {
   1416 	struct sdmmc_command cmd;
   1417 	uint8_t hostctl;
   1418 	int opcode, error, retry = 40;
   1419 
   1420 	KASSERT(mutex_owned(&hp->intr_lock));
   1421 
   1422 	hp->tuning_timing = timing;
   1423 
   1424 	switch (timing) {
   1425 	case SDMMC_TIMING_MMC_HS200:
   1426 		opcode = MMC_SEND_TUNING_BLOCK_HS200;
   1427 		break;
   1428 	case SDMMC_TIMING_UHS_SDR50:
   1429 		if (!ISSET(hp->sc->sc_caps2, SDHC_TUNING_SDR50))
   1430 			return 0;
   1431 		/* FALLTHROUGH */
   1432 	case SDMMC_TIMING_UHS_SDR104:
   1433 		opcode = MMC_SEND_TUNING_BLOCK;
   1434 		break;
   1435 	default:
   1436 		return EINVAL;
   1437 	}
   1438 
   1439 	hostctl = HREAD1(hp, SDHC_HOST_CTL);
   1440 
   1441 	/* enable buffer read ready interrupt */
   1442 	HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_BUFFER_READ_READY);
   1443 	HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_BUFFER_READ_READY);
   1444 
   1445 	/* disable DMA */
   1446 	HCLR1(hp, SDHC_HOST_CTL, SDHC_DMA_SELECT);
   1447 
   1448 	/* reset tuning circuit */
   1449 	HCLR2(hp, SDHC_HOST_CTL2, SDHC_SAMPLING_CLOCK_SEL);
   1450 
   1451 	/* start of tuning */
   1452 	HWRITE2(hp, SDHC_HOST_CTL2, SDHC_EXECUTE_TUNING);
   1453 
   1454 	do {
   1455 		memset(&cmd, 0, sizeof(cmd));
   1456 		cmd.c_opcode = opcode;
   1457 		cmd.c_arg = 0;
   1458 		cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1;
   1459 		if (ISSET(hostctl, SDHC_8BIT_MODE)) {
   1460 			cmd.c_blklen = cmd.c_datalen = 128;
   1461 		} else {
   1462 			cmd.c_blklen = cmd.c_datalen = 64;
   1463 		}
   1464 
   1465 		error = sdhc_start_command(hp, &cmd);
   1466 		if (error)
   1467 			break;
   1468 
   1469 		if (!sdhc_wait_intr(hp, SDHC_BUFFER_READ_READY,
   1470 		    SDHC_TUNING_TIMEOUT, false)) {
   1471 			break;
   1472 		}
   1473 
   1474 		delay(1000);
   1475 	} while (HREAD2(hp, SDHC_HOST_CTL2) & SDHC_EXECUTE_TUNING && --retry);
   1476 
   1477 	/* disable buffer read ready interrupt */
   1478 	HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_BUFFER_READ_READY);
   1479 	HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_BUFFER_READ_READY);
   1480 
   1481 	if (HREAD2(hp, SDHC_HOST_CTL2) & SDHC_EXECUTE_TUNING) {
   1482 		HCLR2(hp, SDHC_HOST_CTL2,
   1483 		    SDHC_SAMPLING_CLOCK_SEL|SDHC_EXECUTE_TUNING);
   1484 		sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
   1485 		aprint_error_dev(hp->sc->sc_dev,
   1486 		    "tuning did not complete, using fixed sampling clock\n");
   1487 		return 0;		/* tuning did not complete */
   1488 	}
   1489 
   1490 	if ((HREAD2(hp, SDHC_HOST_CTL2) & SDHC_SAMPLING_CLOCK_SEL) == 0) {
   1491 		HCLR2(hp, SDHC_HOST_CTL2,
   1492 		    SDHC_SAMPLING_CLOCK_SEL|SDHC_EXECUTE_TUNING);
   1493 		sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
   1494 		aprint_error_dev(hp->sc->sc_dev,
   1495 		    "tuning failed, using fixed sampling clock\n");
   1496 		return 0;		/* tuning failed */
   1497 	}
   1498 
   1499 	if (hp->tuning_timer_count) {
   1500 		callout_schedule(&hp->tuning_timer,
   1501 		    hz * hp->tuning_timer_count);
   1502 	}
   1503 
   1504 	return 0;		/* tuning completed */
   1505 }
   1506 
   1507 static int
   1508 sdhc_execute_tuning(sdmmc_chipset_handle_t sch, int timing)
   1509 {
   1510 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1511 	int error;
   1512 
   1513 	mutex_enter(&hp->intr_lock);
   1514 	error = sdhc_execute_tuning1(hp, timing);
   1515 	mutex_exit(&hp->intr_lock);
   1516 	return error;
   1517 }
   1518 
   1519 static void
   1520 sdhc_tuning_timer(void *arg)
   1521 {
   1522 	struct sdhc_host *hp = arg;
   1523 
   1524 	atomic_swap_uint(&hp->tuning_timer_pending, 1);
   1525 }
   1526 
   1527 static void
   1528 sdhc_hw_reset(sdmmc_chipset_handle_t sch)
   1529 {
   1530 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1531 	struct sdhc_softc *sc = hp->sc;
   1532 
   1533 	if (sc->sc_vendor_hw_reset != NULL)
   1534 		sc->sc_vendor_hw_reset(sc, hp);
   1535 }
   1536 
   1537 static int
   1538 sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value)
   1539 {
   1540 	uint32_t state;
   1541 	int timeout;
   1542 
   1543 	for (timeout = 100000; timeout > 0; timeout--) {
   1544 		if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value)
   1545 			return 0;
   1546 		sdmmc_delay(10);
   1547 	}
   1548 	aprint_error_dev(hp->sc->sc_dev, "timeout waiting for mask %#x value %#x (state=%#x)\n",
   1549 	    mask, value, state);
   1550 	return ETIMEDOUT;
   1551 }
   1552 
   1553 static void
   1554 sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
   1555 {
   1556 	struct sdhc_host *hp = (struct sdhc_host *)sch;
   1557 	int error;
   1558 	bool probing;
   1559 
   1560 	mutex_enter(&hp->intr_lock);
   1561 
   1562 	if (atomic_cas_uint(&hp->tuning_timer_pending, 1, 0) == 1) {
   1563 		(void)sdhc_execute_tuning1(hp, hp->tuning_timing);
   1564 	}
   1565 
   1566 	if (cmd->c_data &&
   1567 	    ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   1568 		const uint16_t ready = SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY;
   1569 		if (ISSET(hp->flags, SHF_USE_DMA)) {
   1570 			HCLR2(hp, SDHC_NINTR_SIGNAL_EN, ready);
   1571 			HCLR2(hp, SDHC_NINTR_STATUS_EN, ready);
   1572 		} else {
   1573 			HSET2(hp, SDHC_NINTR_SIGNAL_EN, ready);
   1574 			HSET2(hp, SDHC_NINTR_STATUS_EN, ready);
   1575 		}
   1576 	}
   1577 
   1578 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_TIMEOUT)) {
   1579 		const uint16_t eintr = SDHC_CMD_TIMEOUT_ERROR;
   1580 		if (cmd->c_data != NULL) {
   1581 			HCLR2(hp, SDHC_EINTR_SIGNAL_EN, eintr);
   1582 			HCLR2(hp, SDHC_EINTR_STATUS_EN, eintr);
   1583 		} else {
   1584 			HSET2(hp, SDHC_EINTR_SIGNAL_EN, eintr);
   1585 			HSET2(hp, SDHC_EINTR_STATUS_EN, eintr);
   1586 		}
   1587 	}
   1588 
   1589 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_STOP_WITH_TC)) {
   1590 		if (cmd->c_opcode == MMC_STOP_TRANSMISSION)
   1591 			SET(cmd->c_flags, SCF_RSP_BSY);
   1592 	}
   1593 
   1594 	/*
   1595 	 * Start the MMC command, or mark `cmd' as failed and return.
   1596 	 */
   1597 	error = sdhc_start_command(hp, cmd);
   1598 	if (error) {
   1599 		cmd->c_error = error;
   1600 		goto out;
   1601 	}
   1602 
   1603 	/*
   1604 	 * Wait until the command phase is done, or until the command
   1605 	 * is marked done for any other reason.
   1606 	 */
   1607 	probing = (cmd->c_flags & SCF_TOUT_OK) != 0;
   1608 	if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT*3, probing)) {
   1609 		DPRINTF(1,("%s: timeout for command\n", __func__));
   1610 		sdmmc_delay(50);
   1611 		cmd->c_error = ETIMEDOUT;
   1612 		goto out;
   1613 	}
   1614 
   1615 	/*
   1616 	 * The host controller removes bits [0:7] from the response
   1617 	 * data (CRC) and we pass the data up unchanged to the bus
   1618 	 * driver (without padding).
   1619 	 */
   1620 	if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
   1621 		cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE + 0);
   1622 		if (ISSET(cmd->c_flags, SCF_RSP_136)) {
   1623 			cmd->c_resp[1] = HREAD4(hp, SDHC_RESPONSE + 4);
   1624 			cmd->c_resp[2] = HREAD4(hp, SDHC_RESPONSE + 8);
   1625 			cmd->c_resp[3] = HREAD4(hp, SDHC_RESPONSE + 12);
   1626 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_RSP136_CRC)) {
   1627 				cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
   1628 				    (cmd->c_resp[1] << 24);
   1629 				cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
   1630 				    (cmd->c_resp[2] << 24);
   1631 				cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
   1632 				    (cmd->c_resp[3] << 24);
   1633 				cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
   1634 			}
   1635 		}
   1636 	}
   1637 	DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0]));
   1638 
   1639 	/*
   1640 	 * If the command has data to transfer in any direction,
   1641 	 * execute the transfer now.
   1642 	 */
   1643 	if (cmd->c_error == 0 && cmd->c_data != NULL)
   1644 		sdhc_transfer_data(hp, cmd);
   1645 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY)) {
   1646 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_BUSY_INTR) &&
   1647 		    !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE, hz * 10, false)) {
   1648 			DPRINTF(1,("%s: sdhc_exec_command: RSP_BSY\n",
   1649 			    HDEVNAME(hp)));
   1650 			cmd->c_error = ETIMEDOUT;
   1651 			goto out;
   1652 		}
   1653 	}
   1654 
   1655 out:
   1656 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)
   1657 	    && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_LED_ON)) {
   1658 		/* Turn off the LED. */
   1659 		HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
   1660 	}
   1661 	SET(cmd->c_flags, SCF_ITSDONE);
   1662 
   1663 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_AUTO_STOP) &&
   1664 	    cmd->c_opcode == MMC_STOP_TRANSMISSION)
   1665 		(void)sdhc_soft_reset(hp, SDHC_RESET_CMD|SDHC_RESET_DAT);
   1666 
   1667 	mutex_exit(&hp->intr_lock);
   1668 
   1669 	DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp),
   1670 	    cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort",
   1671 	    cmd->c_flags, cmd->c_error));
   1672 }
   1673 
   1674 static int
   1675 sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1676 {
   1677 	struct sdhc_softc * const sc = hp->sc;
   1678 	uint16_t blksize = 0;
   1679 	uint16_t blkcount = 0;
   1680 	uint16_t mode;
   1681 	uint16_t command;
   1682 	uint32_t pmask;
   1683 	int error;
   1684 
   1685 	KASSERT(mutex_owned(&hp->intr_lock));
   1686 
   1687 	DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x, status=%#x\n",
   1688 	    HDEVNAME(hp), cmd->c_opcode, cmd->c_arg, cmd->c_data,
   1689 	    cmd->c_datalen, cmd->c_flags, HREAD4(hp, SDHC_NINTR_STATUS)));
   1690 
   1691 	/*
   1692 	 * The maximum block length for commands should be the minimum
   1693 	 * of the host buffer size and the card buffer size. (1.7.2)
   1694 	 */
   1695 
   1696 	/* Fragment the data into proper blocks. */
   1697 	if (cmd->c_datalen > 0) {
   1698 		blksize = MIN(cmd->c_datalen, cmd->c_blklen);
   1699 		blkcount = cmd->c_datalen / blksize;
   1700 		if (cmd->c_datalen % blksize > 0) {
   1701 			/* XXX: Split this command. (1.7.4) */
   1702 			aprint_error_dev(sc->sc_dev,
   1703 			    "data not a multiple of %u bytes\n", blksize);
   1704 			return EINVAL;
   1705 		}
   1706 	}
   1707 
   1708 	/* Check limit imposed by 9-bit block count. (1.7.2) */
   1709 	if (blkcount > SDHC_BLOCK_COUNT_MAX) {
   1710 		aprint_error_dev(sc->sc_dev, "too much data\n");
   1711 		return EINVAL;
   1712 	}
   1713 
   1714 	/* Prepare transfer mode register value. (2.2.5) */
   1715 	mode = SDHC_BLOCK_COUNT_ENABLE;
   1716 	if (ISSET(cmd->c_flags, SCF_CMD_READ))
   1717 		mode |= SDHC_READ_MODE;
   1718 	if (blkcount > 1) {
   1719 		mode |= SDHC_MULTI_BLOCK_MODE;
   1720 		/* XXX only for memory commands? */
   1721 		if (!ISSET(sc->sc_flags, SDHC_FLAG_NO_AUTO_STOP))
   1722 			mode |= SDHC_AUTO_CMD12_ENABLE;
   1723 	}
   1724 	if (cmd->c_dmamap != NULL && cmd->c_datalen > 0 &&
   1725 	    ISSET(hp->flags,  SHF_MODE_DMAEN)) {
   1726 		mode |= SDHC_DMA_ENABLE;
   1727 	}
   1728 
   1729 	/*
   1730 	 * Prepare command register value. (2.2.6)
   1731 	 */
   1732 	command = (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT;
   1733 
   1734 	if (ISSET(cmd->c_flags, SCF_RSP_CRC))
   1735 		command |= SDHC_CRC_CHECK_ENABLE;
   1736 	if (ISSET(cmd->c_flags, SCF_RSP_IDX))
   1737 		command |= SDHC_INDEX_CHECK_ENABLE;
   1738 	if (cmd->c_datalen > 0)
   1739 		command |= SDHC_DATA_PRESENT_SELECT;
   1740 
   1741 	if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
   1742 		command |= SDHC_NO_RESPONSE;
   1743 	else if (ISSET(cmd->c_flags, SCF_RSP_136))
   1744 		command |= SDHC_RESP_LEN_136;
   1745 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
   1746 		command |= SDHC_RESP_LEN_48_CHK_BUSY;
   1747 	else
   1748 		command |= SDHC_RESP_LEN_48;
   1749 
   1750 	/* Wait until command and optionally data inhibit bits are clear. (1.5) */
   1751 	pmask = SDHC_CMD_INHIBIT_CMD;
   1752 	if (cmd->c_flags & (SCF_CMD_ADTC|SCF_RSP_BSY))
   1753 		pmask |= SDHC_CMD_INHIBIT_DAT;
   1754 	error = sdhc_wait_state(hp, pmask, 0);
   1755 	if (error) {
   1756 		(void) sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
   1757 		device_printf(sc->sc_dev, "command or data phase inhibited\n");
   1758 		return error;
   1759 	}
   1760 
   1761 	DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n",
   1762 	    HDEVNAME(hp), blksize, blkcount, mode, command));
   1763 
   1764 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   1765 		blksize |= (MAX(0, PAGE_SHIFT - 12) & SDHC_DMA_BOUNDARY_MASK) <<
   1766 		    SDHC_DMA_BOUNDARY_SHIFT;	/* PAGE_SIZE DMA boundary */
   1767 	}
   1768 
   1769 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   1770 		/* Alert the user not to remove the card. */
   1771 		HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
   1772 	}
   1773 
   1774 	/* Set DMA start address. */
   1775 	if (ISSET(hp->flags, SHF_USE_ADMA2_MASK) && cmd->c_data != NULL) {
   1776 		for (int seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
   1777 			bus_addr_t paddr =
   1778 			    cmd->c_dmamap->dm_segs[seg].ds_addr;
   1779 			uint16_t len =
   1780 			    cmd->c_dmamap->dm_segs[seg].ds_len == 65536 ?
   1781 			    0 : cmd->c_dmamap->dm_segs[seg].ds_len;
   1782 			uint16_t attr =
   1783 			    SDHC_ADMA2_VALID | SDHC_ADMA2_ACT_TRANS;
   1784 			if (seg == cmd->c_dmamap->dm_nsegs - 1) {
   1785 				attr |= SDHC_ADMA2_END;
   1786 			}
   1787 			if (ISSET(hp->flags, SHF_USE_ADMA2_32)) {
   1788 				struct sdhc_adma2_descriptor32 *desc =
   1789 				    hp->adma2;
   1790 				desc[seg].attribute = htole16(attr);
   1791 				desc[seg].length = htole16(len);
   1792 				desc[seg].address = htole32(paddr);
   1793 			} else {
   1794 				struct sdhc_adma2_descriptor64 *desc =
   1795 				    hp->adma2;
   1796 				desc[seg].attribute = htole16(attr);
   1797 				desc[seg].length = htole16(len);
   1798 				desc[seg].address = htole32(paddr & 0xffffffff);
   1799 				desc[seg].address_hi = htole32(
   1800 				    (uint64_t)paddr >> 32);
   1801 			}
   1802 		}
   1803 		if (ISSET(hp->flags, SHF_USE_ADMA2_32)) {
   1804 			struct sdhc_adma2_descriptor32 *desc = hp->adma2;
   1805 			desc[cmd->c_dmamap->dm_nsegs].attribute = htole16(0);
   1806 		} else {
   1807 			struct sdhc_adma2_descriptor64 *desc = hp->adma2;
   1808 			desc[cmd->c_dmamap->dm_nsegs].attribute = htole16(0);
   1809 		}
   1810 		bus_dmamap_sync(sc->sc_dmat, hp->adma_map, 0, PAGE_SIZE,
   1811 		    BUS_DMASYNC_PREWRITE);
   1812 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
   1813 			HCLR4(hp, SDHC_HOST_CTL, SDHC_USDHC_DMA_SELECT);
   1814 			HSET4(hp, SDHC_HOST_CTL, SDHC_USDHC_DMA_SELECT_ADMA2);
   1815 		} else {
   1816 			HCLR1(hp, SDHC_HOST_CTL, SDHC_DMA_SELECT);
   1817 			HSET1(hp, SDHC_HOST_CTL, SDHC_DMA_SELECT_ADMA2);
   1818 		}
   1819 
   1820 		const bus_addr_t desc_addr = hp->adma_map->dm_segs[0].ds_addr;
   1821 
   1822 		HWRITE4(hp, SDHC_ADMA_SYSTEM_ADDR, desc_addr & 0xffffffff);
   1823 		if (ISSET(hp->flags, SHF_USE_ADMA2_64)) {
   1824 			HWRITE4(hp, SDHC_ADMA_SYSTEM_ADDR + 4,
   1825 			    (uint64_t)desc_addr >> 32);
   1826 		}
   1827 	} else if (ISSET(mode, SDHC_DMA_ENABLE) &&
   1828 	    !ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA)) {
   1829 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
   1830 			HCLR4(hp, SDHC_HOST_CTL, SDHC_USDHC_DMA_SELECT);
   1831 		}
   1832 		HWRITE4(hp, SDHC_DMA_ADDR, cmd->c_dmamap->dm_segs[0].ds_addr);
   1833 	}
   1834 
   1835 	/*
   1836 	 * Start a CPU data transfer.  Writing to the high order byte
   1837 	 * of the SDHC_COMMAND register triggers the SD command. (1.5)
   1838 	 */
   1839 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   1840 		HWRITE4(hp, SDHC_BLOCK_SIZE, blksize | (blkcount << 16));
   1841 		HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
   1842 		if (ISSET(hp->sc->sc_flags, SDHC_FLAG_USDHC)) {
   1843 			/* mode bits is in MIX_CTRL register on uSDHC */
   1844 			HWRITE4(hp, SDHC_MIX_CTRL, mode |
   1845 			    (HREAD4(hp, SDHC_MIX_CTRL) &
   1846 			    ~(SDHC_MULTI_BLOCK_MODE |
   1847 			    SDHC_READ_MODE |
   1848 			    SDHC_AUTO_CMD12_ENABLE |
   1849 			    SDHC_BLOCK_COUNT_ENABLE |
   1850 			    SDHC_DMA_ENABLE)));
   1851 			HWRITE4(hp, SDHC_TRANSFER_MODE, command << 16);
   1852 		} else {
   1853 			HWRITE4(hp, SDHC_TRANSFER_MODE, mode | (command << 16));
   1854 		}
   1855 	} else {
   1856 		HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
   1857 		HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
   1858 		HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
   1859 		HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
   1860 		HWRITE2(hp, SDHC_COMMAND, command);
   1861 	}
   1862 
   1863 	return 0;
   1864 }
   1865 
   1866 static void
   1867 sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1868 {
   1869 	struct sdhc_softc *sc = hp->sc;
   1870 	int error;
   1871 
   1872 	KASSERT(mutex_owned(&hp->intr_lock));
   1873 
   1874 	DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp),
   1875 	    MMC_R1(cmd->c_resp), cmd->c_datalen));
   1876 
   1877 #ifdef SDHC_DEBUG
   1878 	/* XXX I forgot why I wanted to know when this happens :-( */
   1879 	if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
   1880 	    ISSET(MMC_R1(cmd->c_resp), 0xcb00)) {
   1881 		aprint_error_dev(hp->sc->sc_dev,
   1882 		    "CMD52/53 error response flags %#x\n",
   1883 		    MMC_R1(cmd->c_resp) & 0xff00);
   1884 	}
   1885 #endif
   1886 
   1887 	if (cmd->c_dmamap != NULL) {
   1888 		if (hp->sc->sc_vendor_transfer_data_dma != NULL) {
   1889 			error = hp->sc->sc_vendor_transfer_data_dma(sc, cmd);
   1890 			if (error == 0 && !sdhc_wait_intr(hp,
   1891 			    SDHC_TRANSFER_COMPLETE, SDHC_DMA_TIMEOUT, false)) {
   1892 				DPRINTF(1,("%s: timeout\n", __func__));
   1893 				error = ETIMEDOUT;
   1894 			}
   1895 		} else {
   1896 			error = sdhc_transfer_data_dma(hp, cmd);
   1897 		}
   1898 	} else
   1899 		error = sdhc_transfer_data_pio(hp, cmd);
   1900 	if (error)
   1901 		cmd->c_error = error;
   1902 	SET(cmd->c_flags, SCF_ITSDONE);
   1903 
   1904 	DPRINTF(1,("%s: data transfer done (error=%d)\n",
   1905 	    HDEVNAME(hp), cmd->c_error));
   1906 }
   1907 
   1908 static int
   1909 sdhc_transfer_data_dma(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1910 {
   1911 	bus_dma_segment_t *dm_segs = cmd->c_dmamap->dm_segs;
   1912 	bus_addr_t posaddr;
   1913 	bus_addr_t segaddr;
   1914 	bus_size_t seglen;
   1915 	u_int seg = 0;
   1916 	int error = 0;
   1917 	int status;
   1918 
   1919 	KASSERT(mutex_owned(&hp->intr_lock));
   1920 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_DMA_INTERRUPT);
   1921 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_DMA_INTERRUPT);
   1922 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
   1923 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
   1924 
   1925 	for (;;) {
   1926 		status = sdhc_wait_intr(hp,
   1927 		    SDHC_DMA_INTERRUPT|SDHC_TRANSFER_COMPLETE,
   1928 		    SDHC_DMA_TIMEOUT, false);
   1929 
   1930 		if (status & SDHC_TRANSFER_COMPLETE) {
   1931 			break;
   1932 		}
   1933 		if (!status) {
   1934 			DPRINTF(1,("%s: timeout\n", __func__));
   1935 			error = ETIMEDOUT;
   1936 			break;
   1937 		}
   1938 
   1939 		if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
   1940 			continue;
   1941 		}
   1942 
   1943 		if ((status & SDHC_DMA_INTERRUPT) == 0) {
   1944 			continue;
   1945 		}
   1946 
   1947 		/* DMA Interrupt (boundary crossing) */
   1948 
   1949 		segaddr = dm_segs[seg].ds_addr;
   1950 		seglen = dm_segs[seg].ds_len;
   1951 		posaddr = HREAD4(hp, SDHC_DMA_ADDR);
   1952 
   1953 		if ((seg == (cmd->c_dmamap->dm_nsegs-1)) && (posaddr == (segaddr + seglen))) {
   1954 			continue;
   1955 		}
   1956 		if ((posaddr >= segaddr) && (posaddr < (segaddr + seglen)))
   1957 			HWRITE4(hp, SDHC_DMA_ADDR, posaddr);
   1958 		else if ((posaddr >= segaddr) && (posaddr == (segaddr + seglen)) && (seg + 1) < cmd->c_dmamap->dm_nsegs)
   1959 			HWRITE4(hp, SDHC_DMA_ADDR, dm_segs[++seg].ds_addr);
   1960 		KASSERT(seg < cmd->c_dmamap->dm_nsegs);
   1961 	}
   1962 
   1963 	if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
   1964 		bus_dmamap_sync(hp->sc->sc_dmat, hp->adma_map, 0,
   1965 		    PAGE_SIZE, BUS_DMASYNC_POSTWRITE);
   1966 	}
   1967 
   1968 	return error;
   1969 }
   1970 
   1971 static int
   1972 sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd)
   1973 {
   1974 	uint8_t *data = cmd->c_data;
   1975 	void (*pio_func)(struct sdhc_host *, uint8_t *, u_int);
   1976 	u_int len, datalen;
   1977 	u_int imask;
   1978 	u_int pmask;
   1979 	int error = 0;
   1980 
   1981 	KASSERT(mutex_owned(&hp->intr_lock));
   1982 
   1983 	if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
   1984 		imask = SDHC_BUFFER_READ_READY;
   1985 		pmask = SDHC_BUFFER_READ_ENABLE;
   1986 		if (ISSET(hp->sc->sc_flags,
   1987 		    SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   1988 			pio_func = esdhc_read_data_pio;
   1989 		} else {
   1990 			pio_func = sdhc_read_data_pio;
   1991 		}
   1992 	} else {
   1993 		imask = SDHC_BUFFER_WRITE_READY;
   1994 		pmask = SDHC_BUFFER_WRITE_ENABLE;
   1995 		if (ISSET(hp->sc->sc_flags,
   1996 		    SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   1997 			pio_func = esdhc_write_data_pio;
   1998 		} else {
   1999 			pio_func = sdhc_write_data_pio;
   2000 		}
   2001 	}
   2002 	datalen = cmd->c_datalen;
   2003 
   2004 	KASSERT(mutex_owned(&hp->intr_lock));
   2005 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & imask);
   2006 	KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
   2007 	KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
   2008 
   2009 	while (datalen > 0) {
   2010 		if (!ISSET(HREAD4(hp, SDHC_PRESENT_STATE), pmask)) {
   2011 			if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   2012 				HSET4(hp, SDHC_NINTR_SIGNAL_EN, imask);
   2013 			} else {
   2014 				HSET2(hp, SDHC_NINTR_SIGNAL_EN, imask);
   2015 			}
   2016 			if (!sdhc_wait_intr(hp, imask, SDHC_BUFFER_TIMEOUT, false)) {
   2017 				DPRINTF(1,("%s: timeout\n", __func__));
   2018 				error = ETIMEDOUT;
   2019 				break;
   2020 			}
   2021 
   2022 			error = sdhc_wait_state(hp, pmask, pmask);
   2023 			if (error)
   2024 				break;
   2025 		}
   2026 
   2027 		len = MIN(datalen, cmd->c_blklen);
   2028 		(*pio_func)(hp, data, len);
   2029 		DPRINTF(2,("%s: pio data transfer %u @ %p\n",
   2030 		    HDEVNAME(hp), len, data));
   2031 
   2032 		data += len;
   2033 		datalen -= len;
   2034 	}
   2035 
   2036 	if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
   2037 	    SDHC_TRANSFER_TIMEOUT, false)) {
   2038 		DPRINTF(1,("%s: timeout for transfer\n", __func__));
   2039 		error = ETIMEDOUT;
   2040 	}
   2041 
   2042 	return error;
   2043 }
   2044 
   2045 static void
   2046 sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   2047 {
   2048 
   2049 	if (((__uintptr_t)data & 3) == 0) {
   2050 		while (datalen > 3) {
   2051 			*(uint32_t *)data = le32toh(HREAD4(hp, SDHC_DATA));
   2052 			data += 4;
   2053 			datalen -= 4;
   2054 		}
   2055 		if (datalen > 1) {
   2056 			*(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
   2057 			data += 2;
   2058 			datalen -= 2;
   2059 		}
   2060 		if (datalen > 0) {
   2061 			*data = HREAD1(hp, SDHC_DATA);
   2062 			data += 1;
   2063 			datalen -= 1;
   2064 		}
   2065 	} else if (((__uintptr_t)data & 1) == 0) {
   2066 		while (datalen > 1) {
   2067 			*(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
   2068 			data += 2;
   2069 			datalen -= 2;
   2070 		}
   2071 		if (datalen > 0) {
   2072 			*data = HREAD1(hp, SDHC_DATA);
   2073 			data += 1;
   2074 			datalen -= 1;
   2075 		}
   2076 	} else {
   2077 		while (datalen > 0) {
   2078 			*data = HREAD1(hp, SDHC_DATA);
   2079 			data += 1;
   2080 			datalen -= 1;
   2081 		}
   2082 	}
   2083 }
   2084 
   2085 static void
   2086 sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   2087 {
   2088 
   2089 	if (((__uintptr_t)data & 3) == 0) {
   2090 		while (datalen > 3) {
   2091 			HWRITE4(hp, SDHC_DATA, htole32(*(uint32_t *)data));
   2092 			data += 4;
   2093 			datalen -= 4;
   2094 		}
   2095 		if (datalen > 1) {
   2096 			HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
   2097 			data += 2;
   2098 			datalen -= 2;
   2099 		}
   2100 		if (datalen > 0) {
   2101 			HWRITE1(hp, SDHC_DATA, *data);
   2102 			data += 1;
   2103 			datalen -= 1;
   2104 		}
   2105 	} else if (((__uintptr_t)data & 1) == 0) {
   2106 		while (datalen > 1) {
   2107 			HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
   2108 			data += 2;
   2109 			datalen -= 2;
   2110 		}
   2111 		if (datalen > 0) {
   2112 			HWRITE1(hp, SDHC_DATA, *data);
   2113 			data += 1;
   2114 			datalen -= 1;
   2115 		}
   2116 	} else {
   2117 		while (datalen > 0) {
   2118 			HWRITE1(hp, SDHC_DATA, *data);
   2119 			data += 1;
   2120 			datalen -= 1;
   2121 		}
   2122 	}
   2123 }
   2124 
   2125 static void
   2126 esdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   2127 {
   2128 	uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
   2129 	uint32_t v;
   2130 
   2131 	const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_READ_SHIFT) & SDHC_WATERMARK_READ_MASK;
   2132 	size_t count = 0;
   2133 
   2134 	while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   2135 		if (count == 0) {
   2136 			/*
   2137 			 * If we've drained "watermark" words, we need to wait
   2138 			 * a little bit so the read FIFO can refill.
   2139 			 */
   2140 			sdmmc_delay(10);
   2141 			count = watermark;
   2142 		}
   2143 		v = HREAD4(hp, SDHC_DATA);
   2144 		v = le32toh(v);
   2145 		*(uint32_t *)data = v;
   2146 		data += 4;
   2147 		datalen -= 4;
   2148 		status = HREAD2(hp, SDHC_NINTR_STATUS);
   2149 		count--;
   2150 	}
   2151 	if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   2152 		if (count == 0) {
   2153 			sdmmc_delay(10);
   2154 		}
   2155 		v = HREAD4(hp, SDHC_DATA);
   2156 		v = le32toh(v);
   2157 		do {
   2158 			*data++ = v;
   2159 			v >>= 8;
   2160 		} while (--datalen > 0);
   2161 	}
   2162 }
   2163 
   2164 static void
   2165 esdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
   2166 {
   2167 	uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
   2168 	uint32_t v;
   2169 
   2170 	const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_WRITE_SHIFT) & SDHC_WATERMARK_WRITE_MASK;
   2171 	size_t count = watermark;
   2172 
   2173 	while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   2174 		if (count == 0) {
   2175 			sdmmc_delay(10);
   2176 			count = watermark;
   2177 		}
   2178 		v = *(uint32_t *)data;
   2179 		v = htole32(v);
   2180 		HWRITE4(hp, SDHC_DATA, v);
   2181 		data += 4;
   2182 		datalen -= 4;
   2183 		status = HREAD2(hp, SDHC_NINTR_STATUS);
   2184 		count--;
   2185 	}
   2186 	if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
   2187 		if (count == 0) {
   2188 			sdmmc_delay(10);
   2189 		}
   2190 		v = *(uint32_t *)data;
   2191 		v = htole32(v);
   2192 		HWRITE4(hp, SDHC_DATA, v);
   2193 	}
   2194 }
   2195 
   2196 /* Prepare for another command. */
   2197 static int
   2198 sdhc_soft_reset(struct sdhc_host *hp, int mask)
   2199 {
   2200 	int timo;
   2201 
   2202 	KASSERT(mutex_owned(&hp->intr_lock));
   2203 
   2204 	DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask));
   2205 
   2206 	/* Request the reset.  */
   2207 	HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
   2208 
   2209 	/*
   2210 	 * If necessary, wait for the controller to set the bits to
   2211 	 * acknowledge the reset.
   2212 	 */
   2213 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_WAIT_RESET) &&
   2214 	    ISSET(mask, (SDHC_RESET_DAT | SDHC_RESET_CMD))) {
   2215 		for (timo = 10000; timo > 0; timo--) {
   2216 			if (ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
   2217 				break;
   2218 			/* Short delay because I worry we may miss it...  */
   2219 			sdmmc_delay(1);
   2220 		}
   2221 		if (timo == 0) {
   2222 			DPRINTF(1,("%s: timeout for reset on\n", __func__));
   2223 			return ETIMEDOUT;
   2224 		}
   2225 	}
   2226 
   2227 	/*
   2228 	 * Wait for the controller to clear the bits to indicate that
   2229 	 * the reset has completed.
   2230 	 */
   2231 	for (timo = 10; timo > 0; timo--) {
   2232 		if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
   2233 			break;
   2234 		sdmmc_delay(10000);
   2235 	}
   2236 	if (timo == 0) {
   2237 		DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp),
   2238 		    HREAD1(hp, SDHC_SOFTWARE_RESET)));
   2239 		return ETIMEDOUT;
   2240 	}
   2241 
   2242 	if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
   2243 		HSET4(hp, SDHC_DMA_CTL, SDHC_DMA_SNOOP);
   2244 	}
   2245 
   2246 	return 0;
   2247 }
   2248 
   2249 static int
   2250 sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo, bool probing)
   2251 {
   2252 	int status, error, nointr;
   2253 
   2254 	KASSERT(mutex_owned(&hp->intr_lock));
   2255 
   2256 	mask |= SDHC_ERROR_INTERRUPT;
   2257 
   2258 	nointr = 0;
   2259 	status = hp->intr_status & mask;
   2260 	while (status == 0) {
   2261 		if (cv_timedwait(&hp->intr_cv, &hp->intr_lock, timo)
   2262 		    == EWOULDBLOCK) {
   2263 			nointr = 1;
   2264 			break;
   2265 		}
   2266 		status = hp->intr_status & mask;
   2267 	}
   2268 	error = hp->intr_error_status;
   2269 
   2270 	DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status,
   2271 	    error));
   2272 
   2273 	hp->intr_status &= ~status;
   2274 	hp->intr_error_status &= ~error;
   2275 
   2276 	if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
   2277 		if (ISSET(error, SDHC_DMA_ERROR))
   2278 			device_printf(hp->sc->sc_dev,"dma error\n");
   2279 		if (ISSET(error, SDHC_ADMA_ERROR))
   2280 			device_printf(hp->sc->sc_dev,"adma error\n");
   2281 		if (ISSET(error, SDHC_AUTO_CMD12_ERROR))
   2282 			device_printf(hp->sc->sc_dev,"auto_cmd12 error\n");
   2283 		if (ISSET(error, SDHC_CURRENT_LIMIT_ERROR))
   2284 			device_printf(hp->sc->sc_dev,"current limit error\n");
   2285 		if (ISSET(error, SDHC_DATA_END_BIT_ERROR))
   2286 			device_printf(hp->sc->sc_dev,"data end bit error\n");
   2287 		if (ISSET(error, SDHC_DATA_CRC_ERROR))
   2288 			device_printf(hp->sc->sc_dev,"data crc error\n");
   2289 		if (ISSET(error, SDHC_DATA_TIMEOUT_ERROR))
   2290 			device_printf(hp->sc->sc_dev,"data timeout error\n");
   2291 		if (ISSET(error, SDHC_CMD_INDEX_ERROR))
   2292 			device_printf(hp->sc->sc_dev,"cmd index error\n");
   2293 		if (ISSET(error, SDHC_CMD_END_BIT_ERROR))
   2294 			device_printf(hp->sc->sc_dev,"cmd end bit error\n");
   2295 		if (ISSET(error, SDHC_CMD_CRC_ERROR))
   2296 			device_printf(hp->sc->sc_dev,"cmd crc error\n");
   2297 		if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR)) {
   2298 			if (!probing)
   2299 				device_printf(hp->sc->sc_dev,"cmd timeout error\n");
   2300 #ifdef SDHC_DEBUG
   2301 			else if (sdhcdebug > 0)
   2302 				device_printf(hp->sc->sc_dev,"cmd timeout (expected)\n");
   2303 #endif
   2304 		}
   2305 		if ((error & ~SDHC_EINTR_STATUS_MASK) != 0)
   2306 			device_printf(hp->sc->sc_dev,"vendor error %#x\n",
   2307 				(error & ~SDHC_EINTR_STATUS_MASK));
   2308 		if (error == 0)
   2309 			device_printf(hp->sc->sc_dev,"no error\n");
   2310 
   2311 		/* Command timeout has higher priority than command complete. */
   2312 		if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR))
   2313 			CLR(status, SDHC_COMMAND_COMPLETE);
   2314 
   2315 		/* Transfer complete has higher priority than data timeout. */
   2316 		if (ISSET(status, SDHC_TRANSFER_COMPLETE))
   2317 			CLR(error, SDHC_DATA_TIMEOUT_ERROR);
   2318 	}
   2319 
   2320 	if (nointr ||
   2321 	    (ISSET(status, SDHC_ERROR_INTERRUPT) && error)) {
   2322 		if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
   2323 			(void)sdhc_soft_reset(hp, SDHC_RESET_CMD|SDHC_RESET_DAT);
   2324 		hp->intr_error_status = 0;
   2325 		status = 0;
   2326 	}
   2327 
   2328 	return status;
   2329 }
   2330 
   2331 /*
   2332  * Established by attachment driver at interrupt priority IPL_SDMMC.
   2333  */
   2334 int
   2335 sdhc_intr(void *arg)
   2336 {
   2337 	struct sdhc_softc *sc = (struct sdhc_softc *)arg;
   2338 	struct sdhc_host *hp;
   2339 	int done = 0;
   2340 	uint16_t status;
   2341 	uint16_t error;
   2342 
   2343 	/* We got an interrupt, but we don't know from which slot. */
   2344 	for (size_t host = 0; host < sc->sc_nhosts; host++) {
   2345 		hp = sc->sc_host[host];
   2346 		if (hp == NULL)
   2347 			continue;
   2348 
   2349 		mutex_enter(&hp->intr_lock);
   2350 
   2351 		if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
   2352 			/* Find out which interrupts are pending. */
   2353 			uint32_t xstatus = HREAD4(hp, SDHC_NINTR_STATUS);
   2354 			status = xstatus;
   2355 			error = xstatus >> 16;
   2356 			if (ISSET(sc->sc_flags, SDHC_FLAG_USDHC) &&
   2357 			    (xstatus & SDHC_TRANSFER_COMPLETE) &&
   2358 			    !(xstatus & SDHC_DMA_INTERRUPT)) {
   2359 				/* read again due to uSDHC errata */
   2360 				status = xstatus = HREAD4(hp,
   2361 				    SDHC_NINTR_STATUS);
   2362 				error = xstatus >> 16;
   2363 			}
   2364 			if (ISSET(sc->sc_flags,
   2365 			    SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   2366 				if ((error & SDHC_NINTR_STATUS_MASK) != 0)
   2367 					SET(status, SDHC_ERROR_INTERRUPT);
   2368 			}
   2369 			if (error)
   2370 				xstatus |= SDHC_ERROR_INTERRUPT;
   2371 			else if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   2372 				goto next_port; /* no interrupt for us */
   2373 			/* Acknowledge the interrupts we are about to handle. */
   2374 			HWRITE4(hp, SDHC_NINTR_STATUS, xstatus);
   2375 		} else {
   2376 			/* Find out which interrupts are pending. */
   2377 			error = 0;
   2378 			status = HREAD2(hp, SDHC_NINTR_STATUS);
   2379 			if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
   2380 				goto next_port; /* no interrupt for us */
   2381 			/* Acknowledge the interrupts we are about to handle. */
   2382 			HWRITE2(hp, SDHC_NINTR_STATUS, status);
   2383 			if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
   2384 				/* Acknowledge error interrupts. */
   2385 				error = HREAD2(hp, SDHC_EINTR_STATUS);
   2386 				HWRITE2(hp, SDHC_EINTR_STATUS, error);
   2387 			}
   2388 		}
   2389 
   2390 		DPRINTF(2,("%s: interrupt status=%x error=%x\n", HDEVNAME(hp),
   2391 		    status, error));
   2392 
   2393 		/* Claim this interrupt. */
   2394 		done = 1;
   2395 
   2396 		if (ISSET(status, SDHC_ERROR_INTERRUPT) &&
   2397 		    ISSET(error, SDHC_ADMA_ERROR)) {
   2398 			uint8_t adma_err = HREAD1(hp, SDHC_ADMA_ERROR_STATUS);
   2399 			printf("%s: ADMA error, status %02x\n", HDEVNAME(hp),
   2400 			    adma_err);
   2401 		}
   2402 
   2403 		/*
   2404 		 * Wake up the sdmmc event thread to scan for cards.
   2405 		 */
   2406 		if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)) {
   2407 			if (hp->sdmmc != NULL) {
   2408 				sdmmc_needs_discover(hp->sdmmc);
   2409 			}
   2410 			if (ISSET(sc->sc_flags,
   2411 			    SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   2412 				HCLR4(hp, SDHC_NINTR_STATUS_EN,
   2413 				    status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
   2414 				HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
   2415 				    status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
   2416 			}
   2417 		}
   2418 
   2419 		/*
   2420 		 * Schedule re-tuning process (UHS).
   2421 		 */
   2422 		if (ISSET(status, SDHC_RETUNING_EVENT)) {
   2423 			atomic_swap_uint(&hp->tuning_timer_pending, 1);
   2424 		}
   2425 
   2426 		/*
   2427 		 * Wake up the blocking process to service command
   2428 		 * related interrupt(s).
   2429 		 */
   2430 		if (ISSET(status, SDHC_COMMAND_COMPLETE|SDHC_ERROR_INTERRUPT|
   2431 		    SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY|
   2432 		    SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) {
   2433 			hp->intr_error_status |= error;
   2434 			hp->intr_status |= status;
   2435 			if (ISSET(sc->sc_flags,
   2436 			    SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)) {
   2437 				HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
   2438 				    status & (SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY));
   2439 			}
   2440 			cv_broadcast(&hp->intr_cv);
   2441 		}
   2442 
   2443 		/*
   2444 		 * Service SD card interrupts.
   2445 		 */
   2446 		if (!ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED | SDHC_FLAG_USDHC)
   2447 		    && ISSET(status, SDHC_CARD_INTERRUPT)) {
   2448 			DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp)));
   2449 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
   2450 			sdmmc_card_intr(hp->sdmmc);
   2451 		}
   2452 next_port:
   2453 		mutex_exit(&hp->intr_lock);
   2454 	}
   2455 
   2456 	return done;
   2457 }
   2458 
   2459 kmutex_t *
   2460 sdhc_host_lock(struct sdhc_host *hp)
   2461 {
   2462 	return &hp->intr_lock;
   2463 }
   2464 
   2465 uint8_t
   2466 sdhc_host_read_1(struct sdhc_host *hp, int reg)
   2467 {
   2468 	return HREAD1(hp, reg);
   2469 }
   2470 
   2471 uint16_t
   2472 sdhc_host_read_2(struct sdhc_host *hp, int reg)
   2473 {
   2474 	return HREAD2(hp, reg);
   2475 }
   2476 
   2477 uint32_t
   2478 sdhc_host_read_4(struct sdhc_host *hp, int reg)
   2479 {
   2480 	return HREAD4(hp, reg);
   2481 }
   2482 
   2483 void
   2484 sdhc_host_write_1(struct sdhc_host *hp, int reg, uint8_t val)
   2485 {
   2486 	HWRITE1(hp, reg, val);
   2487 }
   2488 
   2489 void
   2490 sdhc_host_write_2(struct sdhc_host *hp, int reg, uint16_t val)
   2491 {
   2492 	HWRITE2(hp, reg, val);
   2493 }
   2494 
   2495 void
   2496 sdhc_host_write_4(struct sdhc_host *hp, int reg, uint32_t val)
   2497 {
   2498 	HWRITE4(hp, reg, val);
   2499 }
   2500 
   2501 #ifdef SDHC_DEBUG
   2502 void
   2503 sdhc_dump_regs(struct sdhc_host *hp)
   2504 {
   2505 
   2506 	printf("0x%02x PRESENT_STATE:    %x\n", SDHC_PRESENT_STATE,
   2507 	    HREAD4(hp, SDHC_PRESENT_STATE));
   2508 	if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
   2509 		printf("0x%02x POWER_CTL:        %x\n", SDHC_POWER_CTL,
   2510 		    HREAD1(hp, SDHC_POWER_CTL));
   2511 	printf("0x%02x NINTR_STATUS:     %x\n", SDHC_NINTR_STATUS,
   2512 	    HREAD2(hp, SDHC_NINTR_STATUS));
   2513 	printf("0x%02x EINTR_STATUS:     %x\n", SDHC_EINTR_STATUS,
   2514 	    HREAD2(hp, SDHC_EINTR_STATUS));
   2515 	printf("0x%02x NINTR_STATUS_EN:  %x\n", SDHC_NINTR_STATUS_EN,
   2516 	    HREAD2(hp, SDHC_NINTR_STATUS_EN));
   2517 	printf("0x%02x EINTR_STATUS_EN:  %x\n", SDHC_EINTR_STATUS_EN,
   2518 	    HREAD2(hp, SDHC_EINTR_STATUS_EN));
   2519 	printf("0x%02x NINTR_SIGNAL_EN:  %x\n", SDHC_NINTR_SIGNAL_EN,
   2520 	    HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
   2521 	printf("0x%02x EINTR_SIGNAL_EN:  %x\n", SDHC_EINTR_SIGNAL_EN,
   2522 	    HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
   2523 	printf("0x%02x CAPABILITIES:     %x\n", SDHC_CAPABILITIES,
   2524 	    HREAD4(hp, SDHC_CAPABILITIES));
   2525 	printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
   2526 	    HREAD4(hp, SDHC_MAX_CAPABILITIES));
   2527 }
   2528 #endif
   2529