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