xref: /openbsd-src/sys/dev/sdmmc/sdhc.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: sdhc.c,v 1.34 2011/07/31 16:55:01 kettenis Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * SD Host Controller driver based on the SD Host Controller Standard
21  * Simplified Specification Version 1.00 (www.sdcard.com).
22  */
23 
24 #include <sys/param.h>
25 #include <sys/device.h>
26 #include <sys/kernel.h>
27 #include <sys/kthread.h>
28 #include <sys/malloc.h>
29 #include <sys/systm.h>
30 
31 #include <dev/sdmmc/sdhcreg.h>
32 #include <dev/sdmmc/sdhcvar.h>
33 #include <dev/sdmmc/sdmmcchip.h>
34 #include <dev/sdmmc/sdmmcreg.h>
35 #include <dev/sdmmc/sdmmcvar.h>
36 
37 #define SDHC_COMMAND_TIMEOUT	hz
38 #define SDHC_BUFFER_TIMEOUT	hz
39 #define SDHC_TRANSFER_TIMEOUT	hz
40 
41 struct sdhc_host {
42 	struct sdhc_softc *sc;		/* host controller device */
43 	struct device *sdmmc;		/* generic SD/MMC device */
44 	bus_space_tag_t iot;		/* host register set tag */
45 	bus_space_handle_t ioh;		/* host register set handle */
46 	u_int clkbase;			/* base clock frequency in KHz */
47 	int maxblklen;			/* maximum block length */
48 	int flags;			/* flags for this host */
49 	u_int32_t ocr;			/* OCR value from capabilities */
50 	u_int8_t regs[14];		/* host controller state */
51 	u_int16_t intr_status;		/* soft interrupt status */
52 	u_int16_t intr_error_status;	/* soft error status */
53 };
54 
55 /* flag values */
56 #define SHF_USE_DMA		0x0001
57 
58 #define HREAD1(hp, reg)							\
59 	(bus_space_read_1((hp)->iot, (hp)->ioh, (reg)))
60 #define HREAD2(hp, reg)							\
61 	(bus_space_read_2((hp)->iot, (hp)->ioh, (reg)))
62 #define HREAD4(hp, reg)							\
63 	(bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
64 #define HWRITE1(hp, reg, val)						\
65 	bus_space_write_1((hp)->iot, (hp)->ioh, (reg), (val))
66 #define HWRITE2(hp, reg, val)						\
67 	bus_space_write_2((hp)->iot, (hp)->ioh, (reg), (val))
68 #define HWRITE4(hp, reg, val)						\
69 	bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val))
70 #define HCLR1(hp, reg, bits)						\
71 	HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits))
72 #define HCLR2(hp, reg, bits)						\
73 	HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits))
74 #define HSET1(hp, reg, bits)						\
75 	HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits))
76 #define HSET2(hp, reg, bits)						\
77 	HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits))
78 
79 int	sdhc_host_reset(sdmmc_chipset_handle_t);
80 u_int32_t sdhc_host_ocr(sdmmc_chipset_handle_t);
81 int	sdhc_host_maxblklen(sdmmc_chipset_handle_t);
82 int	sdhc_card_detect(sdmmc_chipset_handle_t);
83 int	sdhc_bus_power(sdmmc_chipset_handle_t, u_int32_t);
84 int	sdhc_bus_clock(sdmmc_chipset_handle_t, int);
85 void	sdhc_card_intr_mask(sdmmc_chipset_handle_t, int);
86 void	sdhc_card_intr_ack(sdmmc_chipset_handle_t);
87 void	sdhc_exec_command(sdmmc_chipset_handle_t, struct sdmmc_command *);
88 int	sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
89 int	sdhc_wait_state(struct sdhc_host *, u_int32_t, u_int32_t);
90 int	sdhc_soft_reset(struct sdhc_host *, int);
91 int	sdhc_wait_intr(struct sdhc_host *, int, int);
92 void	sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
93 void	sdhc_read_data(struct sdhc_host *, u_char *, int);
94 void	sdhc_write_data(struct sdhc_host *, u_char *, int);
95 
96 #ifdef SDHC_DEBUG
97 int sdhcdebug = 0;
98 #define DPRINTF(n,s)	do { if ((n) <= sdhcdebug) printf s; } while (0)
99 void	sdhc_dump_regs(struct sdhc_host *);
100 #else
101 #define DPRINTF(n,s)	do {} while(0)
102 #endif
103 
104 struct sdmmc_chip_functions sdhc_functions = {
105 	/* host controller reset */
106 	sdhc_host_reset,
107 	/* host controller capabilities */
108 	sdhc_host_ocr,
109 	sdhc_host_maxblklen,
110 	/* card detection */
111 	sdhc_card_detect,
112 	/* bus power and clock frequency */
113 	sdhc_bus_power,
114 	sdhc_bus_clock,
115 	/* command execution */
116 	sdhc_exec_command,
117 	/* card interrupt */
118 	sdhc_card_intr_mask,
119 	sdhc_card_intr_ack
120 };
121 
122 struct cfdriver sdhc_cd = {
123 	NULL, "sdhc", DV_DULL
124 };
125 
126 /*
127  * Called by attachment driver.  For each SD card slot there is one SD
128  * host controller standard register set. (1.3)
129  */
130 int
131 sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
132     bus_space_handle_t ioh, bus_size_t iosize, int usedma, u_int32_t caps)
133 {
134 	struct sdmmcbus_attach_args saa;
135 	struct sdhc_host *hp;
136 	int error = 1;
137 #ifdef SDHC_DEBUG
138 	u_int16_t version;
139 
140 	version = bus_space_read_2(iot, ioh, SDHC_HOST_CTL_VERSION);
141 	printf("%s: SD Host Specification/Vendor Version ",
142 	    sc->sc_dev.dv_xname);
143 	switch(SDHC_SPEC_VERSION(version)) {
144 	case 0x00:
145 		printf("1.0/%u\n", SDHC_VENDOR_VERSION(version));
146 		break;
147 	default:
148 		printf(">1.0/%u\n", SDHC_VENDOR_VERSION(version));
149 		break;
150 	}
151 #endif
152 
153 	/* Allocate one more host structure. */
154 	sc->sc_nhosts++;
155 	hp = malloc(sizeof(*hp), M_DEVBUF, M_WAITOK | M_ZERO);
156 	sc->sc_host[sc->sc_nhosts - 1] = hp;
157 
158 	/* Fill in the new host structure. */
159 	hp->sc = sc;
160 	hp->iot = iot;
161 	hp->ioh = ioh;
162 
163 	/*
164 	 * Reset the host controller and enable interrupts.
165 	 */
166 	(void)sdhc_host_reset(hp);
167 
168 	/* Determine host capabilities. */
169 	if (caps == 0)
170 		caps = HREAD4(hp, SDHC_CAPABILITIES);
171 
172 	/* Use DMA if the host system and the controller support it. */
173 	if (usedma && ISSET(caps, SDHC_DMA_SUPPORT))
174 		SET(hp->flags, SHF_USE_DMA);
175 
176 	/*
177 	 * Determine the base clock frequency. (2.2.24)
178 	 */
179 	if (SDHC_BASE_FREQ_KHZ(caps) != 0)
180 		hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
181 	if (hp->clkbase == 0) {
182 		/* The attachment driver must tell us. */
183 		printf("%s: base clock frequency unknown\n",
184 		    sc->sc_dev.dv_xname);
185 		goto err;
186 	} else if (hp->clkbase < 10000 || hp->clkbase > 63000) {
187 		/* SDHC 1.0 supports only 10-63 MHz. */
188 		printf("%s: base clock frequency out of range: %u MHz\n",
189 		    sc->sc_dev.dv_xname, hp->clkbase / 1000);
190 		goto err;
191 	}
192 
193 	/*
194 	 * XXX Set the data timeout counter value according to
195 	 * capabilities. (2.2.15)
196 	 */
197 
198 	/*
199 	 * Determine SD bus voltage levels supported by the controller.
200 	 */
201 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V))
202 		SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V);
203 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V))
204 		SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
205 	if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V))
206 		SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
207 
208 	/*
209 	 * Determine the maximum block length supported by the host
210 	 * controller. (2.2.24)
211 	 */
212 	switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
213 	case SDHC_MAX_BLK_LEN_512:
214 		hp->maxblklen = 512;
215 		break;
216 	case SDHC_MAX_BLK_LEN_1024:
217 		hp->maxblklen = 1024;
218 		break;
219 	case SDHC_MAX_BLK_LEN_2048:
220 		hp->maxblklen = 2048;
221 		break;
222 	default:
223 		hp->maxblklen = 1;
224 		break;
225 	}
226 
227 	/*
228 	 * Attach the generic SD/MMC bus driver.  (The bus driver must
229 	 * not invoke any chipset functions before it is attached.)
230 	 */
231 	bzero(&saa, sizeof(saa));
232 	saa.saa_busname = "sdmmc";
233 	saa.sct = &sdhc_functions;
234 	saa.sch = hp;
235 
236 	hp->sdmmc = config_found(&sc->sc_dev, &saa, NULL);
237 	if (hp->sdmmc == NULL) {
238 		error = 0;
239 		goto err;
240 	}
241 
242 	return 0;
243 
244 err:
245 	free(hp, M_DEVBUF);
246 	sc->sc_host[sc->sc_nhosts - 1] = NULL;
247 	sc->sc_nhosts--;
248 	return (error);
249 }
250 
251 int
252 sdhc_activate(struct device *self, int act)
253 {
254 	struct sdhc_softc *sc = (struct sdhc_softc *)self;
255 	struct sdhc_host *hp;
256 	int n, i, rv = 0;
257 
258 	switch (act) {
259 	case DVACT_QUIESCE:
260 		rv = config_activate_children(self, act);
261 		break;
262 	case DVACT_SUSPEND:
263 		/* XXX poll for command completion or suspend command
264 		 * in progress */
265 
266 		/* Save the host controller state. */
267 		for (n = 0; n < sc->sc_nhosts; n++) {
268 			hp = sc->sc_host[n];
269 			for (i = 0; i < sizeof hp->regs; i++)
270 				hp->regs[i] = HREAD1(hp, i);
271 		}
272 		rv = config_activate_children(self, act);
273 		break;
274 	case DVACT_RESUME:
275 		/* Restore the host controller state. */
276 		for (n = 0; n < sc->sc_nhosts; n++) {
277 			hp = sc->sc_host[n];
278 			(void)sdhc_host_reset(hp);
279 			for (i = 0; i < sizeof hp->regs; i++)
280 				HWRITE1(hp, i, hp->regs[i]);
281 		}
282 		rv = config_activate_children(self, act);
283 		break;
284 	}
285 	return (rv);
286 }
287 
288 /*
289  * Shutdown hook established by or called from attachment driver.
290  */
291 void
292 sdhc_shutdown(void *arg)
293 {
294 	struct sdhc_softc *sc = arg;
295 	struct sdhc_host *hp;
296 	int i;
297 
298 	/* XXX chip locks up if we don't disable it before reboot. */
299 	for (i = 0; i < sc->sc_nhosts; i++) {
300 		hp = sc->sc_host[i];
301 		(void)sdhc_host_reset(hp);
302 	}
303 }
304 
305 /*
306  * Reset the host controller.  Called during initialization, when
307  * cards are removed, upon resume, and during error recovery.
308  */
309 int
310 sdhc_host_reset(sdmmc_chipset_handle_t sch)
311 {
312 	struct sdhc_host *hp = sch;
313 	u_int16_t imask;
314 	int error;
315 	int s;
316 
317 	s = splsdmmc();
318 
319 	/* Disable all interrupts. */
320 	HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
321 
322 	/*
323 	 * Reset the entire host controller and wait up to 100ms for
324 	 * the controller to clear the reset bit.
325 	 */
326 	if ((error = sdhc_soft_reset(hp, SDHC_RESET_ALL)) != 0) {
327 		splx(s);
328 		return (error);
329 	}
330 
331 	/* Set data timeout counter value to max for now. */
332 	HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
333 
334 	/* Enable interrupts. */
335 	imask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
336 	    SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
337 	    SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
338 	    SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
339 
340 	HWRITE2(hp, SDHC_NINTR_STATUS_EN, imask);
341 	HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
342 	HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, imask);
343 	HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
344 
345 	splx(s);
346 	return 0;
347 }
348 
349 u_int32_t
350 sdhc_host_ocr(sdmmc_chipset_handle_t sch)
351 {
352 	struct sdhc_host *hp = sch;
353 	return hp->ocr;
354 }
355 
356 int
357 sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
358 {
359 	struct sdhc_host *hp = sch;
360 	return hp->maxblklen;
361 }
362 
363 /*
364  * Return non-zero if the card is currently inserted.
365  */
366 int
367 sdhc_card_detect(sdmmc_chipset_handle_t sch)
368 {
369 	struct sdhc_host *hp = sch;
370 	return ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED) ?
371 	    1 : 0;
372 }
373 
374 /*
375  * Set or change SD bus voltage and enable or disable SD bus power.
376  * Return zero on success.
377  */
378 int
379 sdhc_bus_power(sdmmc_chipset_handle_t sch, u_int32_t ocr)
380 {
381 	struct sdhc_host *hp = sch;
382 	u_int8_t vdd;
383 	int s;
384 
385 	s = splsdmmc();
386 
387 	/*
388 	 * Disable bus power before voltage change.
389 	 */
390 	if (!(hp->sc->sc_flags & SDHC_F_NOPWR0))
391 		HWRITE1(hp, SDHC_POWER_CTL, 0);
392 
393 	/* If power is disabled, reset the host and return now. */
394 	if (ocr == 0) {
395 		splx(s);
396 		(void)sdhc_host_reset(hp);
397 		return 0;
398 	}
399 
400 	/*
401 	 * Select the maximum voltage according to capabilities.
402 	 */
403 	ocr &= hp->ocr;
404 	if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V))
405 		vdd = SDHC_VOLTAGE_3_3V;
406 	else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V))
407 		vdd = SDHC_VOLTAGE_3_0V;
408 	else if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V))
409 		vdd = SDHC_VOLTAGE_1_8V;
410 	else {
411 		/* Unsupported voltage level requested. */
412 		splx(s);
413 		return EINVAL;
414 	}
415 
416 	/*
417 	 * Enable bus power.  Wait at least 1 ms (or 74 clocks) plus
418 	 * voltage ramp until power rises.
419 	 */
420 	HWRITE1(hp, SDHC_POWER_CTL, (vdd << SDHC_VOLTAGE_SHIFT) |
421 	    SDHC_BUS_POWER);
422 	sdmmc_delay(10000);
423 
424 	/*
425 	 * The host system may not power the bus due to battery low,
426 	 * etc.  In that case, the host controller should clear the
427 	 * bus power bit.
428 	 */
429 	if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
430 		splx(s);
431 		return ENXIO;
432 	}
433 
434 	splx(s);
435 	return 0;
436 }
437 
438 /*
439  * Return the smallest possible base clock frequency divisor value
440  * for the CLOCK_CTL register to produce `freq' (KHz).
441  */
442 static int
443 sdhc_clock_divisor(struct sdhc_host *hp, u_int freq)
444 {
445 	int div;
446 
447 	for (div = 1; div <= 256; div *= 2)
448 		if ((hp->clkbase / div) <= freq)
449 			return (div / 2);
450 	/* No divisor found. */
451 	return -1;
452 }
453 
454 /*
455  * Set or change SDCLK frequency or disable the SD clock.
456  * Return zero on success.
457  */
458 int
459 sdhc_bus_clock(sdmmc_chipset_handle_t sch, int freq)
460 {
461 	struct sdhc_host *hp = sch;
462 	int s;
463 	int div;
464 	int timo;
465 	int error = 0;
466 
467 	s = splsdmmc();
468 
469 #ifdef DIAGNOSTIC
470 	/* Must not stop the clock if commands are in progress. */
471 	if (ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK) &&
472 	    sdhc_card_detect(hp))
473 		printf("sdhc_sdclk_frequency_select: command in progress\n");
474 #endif
475 
476 	/*
477 	 * Stop SD clock before changing the frequency.
478 	 */
479 	HWRITE2(hp, SDHC_CLOCK_CTL, 0);
480 	if (freq == SDMMC_SDCLK_OFF)
481 		goto ret;
482 
483 	/*
484 	 * Set the minimum base clock frequency divisor.
485 	 */
486 	if ((div = sdhc_clock_divisor(hp, freq)) < 0) {
487 		/* Invalid base clock frequency or `freq' value. */
488 		error = EINVAL;
489 		goto ret;
490 	}
491 	HWRITE2(hp, SDHC_CLOCK_CTL, div << SDHC_SDCLK_DIV_SHIFT);
492 
493 	/*
494 	 * Start internal clock.  Wait 10ms for stabilization.
495 	 */
496 	HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
497 	for (timo = 1000; timo > 0; timo--) {
498 		if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL), SDHC_INTCLK_STABLE))
499 			break;
500 		sdmmc_delay(10);
501 	}
502 	if (timo == 0) {
503 		error = ETIMEDOUT;
504 		goto ret;
505 	}
506 
507 	/*
508 	 * Enable SD clock.
509 	 */
510 	HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
511 
512 ret:
513 	splx(s);
514 	return error;
515 }
516 
517 void
518 sdhc_card_intr_mask(sdmmc_chipset_handle_t sch, int enable)
519 {
520 	struct sdhc_host *hp = sch;
521 
522 	if (enable) {
523 		HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
524 		HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
525 	} else {
526 		HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
527 		HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
528 	}
529 }
530 
531 void
532 sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
533 {
534 	struct sdhc_host *hp = sch;
535 
536 	HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
537 }
538 
539 int
540 sdhc_wait_state(struct sdhc_host *hp, u_int32_t mask, u_int32_t value)
541 {
542 	u_int32_t state;
543 	int timeout;
544 
545 	for (timeout = 10; timeout > 0; timeout--) {
546 		if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask)
547 		    == value)
548 			return 0;
549 		sdmmc_delay(10000);
550 	}
551 	DPRINTF(0,("%s: timeout waiting for %x (state=%b)\n", DEVNAME(hp->sc),
552 	    value, state, SDHC_PRESENT_STATE_BITS));
553 	return ETIMEDOUT;
554 }
555 
556 void
557 sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
558 {
559 	struct sdhc_host *hp = sch;
560 	int error;
561 
562 	/*
563 	 * Start the MMC command, or mark `cmd' as failed and return.
564 	 */
565 	error = sdhc_start_command(hp, cmd);
566 	if (error != 0) {
567 		cmd->c_error = error;
568 		SET(cmd->c_flags, SCF_ITSDONE);
569 		return;
570 	}
571 
572 	/*
573 	 * Wait until the command phase is done, or until the command
574 	 * is marked done for any other reason.
575 	 */
576 	if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE,
577 	    SDHC_COMMAND_TIMEOUT)) {
578 		cmd->c_error = ETIMEDOUT;
579 		SET(cmd->c_flags, SCF_ITSDONE);
580 		return;
581 	}
582 
583 	/*
584 	 * The host controller removes bits [0:7] from the response
585 	 * data (CRC) and we pass the data up unchanged to the bus
586 	 * driver (without padding).
587 	 */
588 	if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
589 		if (ISSET(cmd->c_flags, SCF_RSP_136)) {
590 			u_char *p = (u_char *)cmd->c_resp;
591 			int i;
592 
593 			for (i = 0; i < 15; i++)
594 				*p++ = HREAD1(hp, SDHC_RESPONSE + i);
595 		} else
596 			cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE);
597 	}
598 
599 	/*
600 	 * If the command has data to transfer in any direction,
601 	 * execute the transfer now.
602 	 */
603 	if (cmd->c_error == 0 && cmd->c_data != NULL)
604 		sdhc_transfer_data(hp, cmd);
605 
606 	/* Turn off the LED. */
607 	HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
608 
609 	DPRINTF(1,("%s: cmd %u done (flags=%#x error=%d)\n",
610 	    DEVNAME(hp->sc), cmd->c_opcode, cmd->c_flags, cmd->c_error));
611 	SET(cmd->c_flags, SCF_ITSDONE);
612 }
613 
614 int
615 sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
616 {
617 	u_int16_t blksize = 0;
618 	u_int16_t blkcount = 0;
619 	u_int16_t mode;
620 	u_int16_t command;
621 	int error;
622 	int s;
623 
624 	DPRINTF(1,("%s: start cmd %u arg=%#x data=%#x dlen=%d flags=%#x "
625 	    "proc=\"%s\"\n", DEVNAME(hp->sc), cmd->c_opcode, cmd->c_arg,
626 	    cmd->c_data, cmd->c_datalen, cmd->c_flags, curproc ?
627 	    curproc->p_comm : ""));
628 
629 	/*
630 	 * The maximum block length for commands should be the minimum
631 	 * of the host buffer size and the card buffer size. (1.7.2)
632 	 */
633 
634 	/* Fragment the data into proper blocks. */
635 	if (cmd->c_datalen > 0) {
636 		blksize = MIN(cmd->c_datalen, cmd->c_blklen);
637 		blkcount = cmd->c_datalen / blksize;
638 		if (cmd->c_datalen % blksize > 0) {
639 			/* XXX: Split this command. (1.7.4) */
640 			printf("%s: data not a multiple of %d bytes\n",
641 			    DEVNAME(hp->sc), blksize);
642 			return EINVAL;
643 		}
644 	}
645 
646 	/* Check limit imposed by 9-bit block count. (1.7.2) */
647 	if (blkcount > SDHC_BLOCK_COUNT_MAX) {
648 		printf("%s: too much data\n", DEVNAME(hp->sc));
649 		return EINVAL;
650 	}
651 
652 	/* Prepare transfer mode register value. (2.2.5) */
653 	mode = 0;
654 	if (ISSET(cmd->c_flags, SCF_CMD_READ))
655 		mode |= SDHC_READ_MODE;
656 	if (blkcount > 0) {
657 		mode |= SDHC_BLOCK_COUNT_ENABLE;
658 		if (blkcount > 1) {
659 			mode |= SDHC_MULTI_BLOCK_MODE;
660 			/* XXX only for memory commands? */
661 			mode |= SDHC_AUTO_CMD12_ENABLE;
662 		}
663 	}
664 #ifdef notyet
665 	if (ISSET(hp->flags, SHF_USE_DMA))
666 		mode |= SDHC_DMA_ENABLE;
667 #endif
668 
669 	/*
670 	 * Prepare command register value. (2.2.6)
671 	 */
672 	command = (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) <<
673 	    SDHC_COMMAND_INDEX_SHIFT;
674 
675 	if (ISSET(cmd->c_flags, SCF_RSP_CRC))
676 		command |= SDHC_CRC_CHECK_ENABLE;
677 	if (ISSET(cmd->c_flags, SCF_RSP_IDX))
678 		command |= SDHC_INDEX_CHECK_ENABLE;
679 	if (cmd->c_data != NULL)
680 		command |= SDHC_DATA_PRESENT_SELECT;
681 
682 	if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
683 		command |= SDHC_NO_RESPONSE;
684 	else if (ISSET(cmd->c_flags, SCF_RSP_136))
685 		command |= SDHC_RESP_LEN_136;
686 	else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
687 		command |= SDHC_RESP_LEN_48_CHK_BUSY;
688 	else
689 		command |= SDHC_RESP_LEN_48;
690 
691 	/* Wait until command and data inhibit bits are clear. (1.5) */
692 	if ((error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0)) != 0)
693 		return error;
694 
695 	s = splsdmmc();
696 
697 	/* Alert the user not to remove the card. */
698 	HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
699 
700 	/* XXX: Set DMA start address if SHF_USE_DMA is set. */
701 
702 	DPRINTF(1,("%s: cmd=%#x mode=%#x blksize=%d blkcount=%d\n",
703 	    DEVNAME(hp->sc), command, mode, blksize, blkcount));
704 
705 	/*
706 	 * Start a CPU data transfer.  Writing to the high order byte
707 	 * of the SDHC_COMMAND register triggers the SD command. (1.5)
708 	 */
709 	HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
710 	HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
711 	if (blkcount > 1)
712 		HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
713 	HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
714 	HWRITE2(hp, SDHC_COMMAND, command);
715 
716 	splx(s);
717 	return 0;
718 }
719 
720 void
721 sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
722 {
723 	u_char *datap = cmd->c_data;
724 	int i, datalen;
725 	int mask;
726 	int error;
727 
728 	mask = ISSET(cmd->c_flags, SCF_CMD_READ) ?
729 	    SDHC_BUFFER_READ_ENABLE : SDHC_BUFFER_WRITE_ENABLE;
730 	error = 0;
731 	datalen = cmd->c_datalen;
732 
733 	DPRINTF(1,("%s: resp=%#x datalen=%d\n", DEVNAME(hp->sc),
734 	    MMC_R1(cmd->c_resp), datalen));
735 
736 #ifdef SDHC_DEBUG
737 	/* XXX I forgot why I wanted to know when this happens :-( */
738 	if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
739 	    ISSET(MMC_R1(cmd->c_resp), 0xcb00))
740 		printf("%s: CMD52/53 error response flags %#x\n",
741 		    DEVNAME(hp->sc), MMC_R1(cmd->c_resp) & 0xff00);
742 #endif
743 
744 	while (datalen > 0) {
745 		if (!sdhc_wait_intr(hp, SDHC_BUFFER_READ_READY|
746 		    SDHC_BUFFER_WRITE_READY, SDHC_BUFFER_TIMEOUT)) {
747 			error = ETIMEDOUT;
748 			break;
749 		}
750 
751 		if ((error = sdhc_wait_state(hp, mask, mask)) != 0)
752 			break;
753 
754 		i = MIN(datalen, cmd->c_blklen);
755 		if (ISSET(cmd->c_flags, SCF_CMD_READ))
756 			sdhc_read_data(hp, datap, i);
757 		else
758 			sdhc_write_data(hp, datap, i);
759 
760 		datap += i;
761 		datalen -= i;
762 	}
763 
764 	if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
765 	    SDHC_TRANSFER_TIMEOUT))
766 		error = ETIMEDOUT;
767 
768 	if (error != 0)
769 		cmd->c_error = error;
770 	SET(cmd->c_flags, SCF_ITSDONE);
771 
772 	DPRINTF(1,("%s: data transfer done (error=%d)\n",
773 	    DEVNAME(hp->sc), cmd->c_error));
774 }
775 
776 void
777 sdhc_read_data(struct sdhc_host *hp, u_char *datap, int datalen)
778 {
779 	while (datalen > 3) {
780 		*(u_int32_t *)datap = HREAD4(hp, SDHC_DATA);
781 		datap += 4;
782 		datalen -= 4;
783 	}
784 	if (datalen > 0) {
785 		u_int32_t rv = HREAD4(hp, SDHC_DATA);
786 		do {
787 			*datap++ = rv & 0xff;
788 			rv = rv >> 8;
789 		} while (--datalen > 0);
790 	}
791 }
792 
793 void
794 sdhc_write_data(struct sdhc_host *hp, u_char *datap, int datalen)
795 {
796 	while (datalen > 3) {
797 		DPRINTF(3,("%08x\n", *(u_int32_t *)datap));
798 		HWRITE4(hp, SDHC_DATA, *((u_int32_t *)datap));
799 		datap += 4;
800 		datalen -= 4;
801 	}
802 	if (datalen > 0) {
803 		u_int32_t rv = *datap++;
804 		if (datalen > 1)
805 			rv |= *datap++ << 8;
806 		if (datalen > 2)
807 			rv |= *datap++ << 16;
808 		DPRINTF(3,("rv %08x\n", rv));
809 		HWRITE4(hp, SDHC_DATA, rv);
810 	}
811 }
812 
813 /* Prepare for another command. */
814 int
815 sdhc_soft_reset(struct sdhc_host *hp, int mask)
816 {
817 	int timo;
818 
819 	DPRINTF(1,("%s: software reset reg=%#x\n", DEVNAME(hp->sc), mask));
820 
821 	HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
822 	for (timo = 10; timo > 0; timo--) {
823 		if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
824 			break;
825 		sdmmc_delay(10000);
826 		HWRITE1(hp, SDHC_SOFTWARE_RESET, 0);
827 	}
828 	if (timo == 0) {
829 		DPRINTF(1,("%s: timeout reg=%#x\n", DEVNAME(hp->sc),
830 		    HREAD1(hp, SDHC_SOFTWARE_RESET)));
831 		HWRITE1(hp, SDHC_SOFTWARE_RESET, 0);
832 		return (ETIMEDOUT);
833 	}
834 
835 	return (0);
836 }
837 
838 int
839 sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo)
840 {
841 	int status;
842 	int s;
843 
844 	mask |= SDHC_ERROR_INTERRUPT;
845 
846 	s = splsdmmc();
847 	status = hp->intr_status & mask;
848 	while (status == 0) {
849 		if (tsleep(&hp->intr_status, PWAIT, "hcintr", timo)
850 		    == EWOULDBLOCK) {
851 			status |= SDHC_ERROR_INTERRUPT;
852 			break;
853 		}
854 		status = hp->intr_status & mask;
855 	}
856 	hp->intr_status &= ~status;
857 
858 	DPRINTF(2,("%s: intr status %#x error %#x\n", DEVNAME(hp->sc), status,
859 	    hp->intr_error_status));
860 
861 	/* Command timeout has higher priority than command complete. */
862 	if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
863 		hp->intr_error_status = 0;
864 		(void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
865 		status = 0;
866 	}
867 
868 	splx(s);
869 	return status;
870 }
871 
872 /*
873  * Established by attachment driver at interrupt priority IPL_SDMMC.
874  */
875 int
876 sdhc_intr(void *arg)
877 {
878 	struct sdhc_softc *sc = arg;
879 	int host;
880 	int done = 0;
881 
882 	/* We got an interrupt, but we don't know from which slot. */
883 	for (host = 0; host < sc->sc_nhosts; host++) {
884 		struct sdhc_host *hp = sc->sc_host[host];
885 		u_int16_t status;
886 
887 		if (hp == NULL)
888 			continue;
889 
890 		/* Find out which interrupts are pending. */
891 		status = HREAD2(hp, SDHC_NINTR_STATUS);
892 		if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
893 			continue; /* no interrupt for us */
894 
895 		/* Acknowledge the interrupts we are about to handle. */
896 		HWRITE2(hp, SDHC_NINTR_STATUS, status);
897 		DPRINTF(2,("%s: interrupt status=%b\n", DEVNAME(hp->sc),
898 		    status, SDHC_NINTR_STATUS_BITS));
899 
900 		/* Claim this interrupt. */
901 		done = 1;
902 
903 		/*
904 		 * Service error interrupts.
905 		 */
906 		if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
907 			u_int16_t error;
908 
909 			/* Acknowledge error interrupts. */
910 			error = HREAD2(hp, SDHC_EINTR_STATUS);
911 			HWRITE2(hp, SDHC_EINTR_STATUS, error);
912 			DPRINTF(2,("%s: error interrupt, status=%b\n",
913 			    DEVNAME(hp->sc), error, SDHC_EINTR_STATUS_BITS));
914 
915 			if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR|
916 			    SDHC_DATA_TIMEOUT_ERROR)) {
917 				hp->intr_error_status |= error;
918 				hp->intr_status |= status;
919 				wakeup(&hp->intr_status);
920 			}
921 		}
922 
923 		/*
924 		 * Wake up the sdmmc event thread to scan for cards.
925 		 */
926 		if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION))
927 			sdmmc_needs_discover(hp->sdmmc);
928 
929 		/*
930 		 * Wake up the blocking process to service command
931 		 * related interrupt(s).
932 		 */
933 		if (ISSET(status, SDHC_BUFFER_READ_READY|
934 		    SDHC_BUFFER_WRITE_READY|SDHC_COMMAND_COMPLETE|
935 		    SDHC_TRANSFER_COMPLETE)) {
936 			hp->intr_status |= status;
937 			wakeup(&hp->intr_status);
938 		}
939 
940 		/*
941 		 * Service SD card interrupts.
942 		 */
943 		if (ISSET(status, SDHC_CARD_INTERRUPT)) {
944 			DPRINTF(0,("%s: card interrupt\n", DEVNAME(hp->sc)));
945 			HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
946 			sdmmc_card_intr(hp->sdmmc);
947 		}
948 	}
949 	return done;
950 }
951 
952 #ifdef SDHC_DEBUG
953 void
954 sdhc_dump_regs(struct sdhc_host *hp)
955 {
956 	printf("0x%02x PRESENT_STATE:    %b\n", SDHC_PRESENT_STATE,
957 	    HREAD4(hp, SDHC_PRESENT_STATE), SDHC_PRESENT_STATE_BITS);
958 	printf("0x%02x POWER_CTL:        %x\n", SDHC_POWER_CTL,
959 	    HREAD1(hp, SDHC_POWER_CTL));
960 	printf("0x%02x NINTR_STATUS:     %x\n", SDHC_NINTR_STATUS,
961 	    HREAD2(hp, SDHC_NINTR_STATUS));
962 	printf("0x%02x EINTR_STATUS:     %x\n", SDHC_EINTR_STATUS,
963 	    HREAD2(hp, SDHC_EINTR_STATUS));
964 	printf("0x%02x NINTR_STATUS_EN:  %x\n", SDHC_NINTR_STATUS_EN,
965 	    HREAD2(hp, SDHC_NINTR_STATUS_EN));
966 	printf("0x%02x EINTR_STATUS_EN:  %x\n", SDHC_EINTR_STATUS_EN,
967 	    HREAD2(hp, SDHC_EINTR_STATUS_EN));
968 	printf("0x%02x NINTR_SIGNAL_EN:  %x\n", SDHC_NINTR_SIGNAL_EN,
969 	    HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
970 	printf("0x%02x EINTR_SIGNAL_EN:  %x\n", SDHC_EINTR_SIGNAL_EN,
971 	    HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
972 	printf("0x%02x CAPABILITIES:     %x\n", SDHC_CAPABILITIES,
973 	    HREAD4(hp, SDHC_CAPABILITIES));
974 	printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
975 	    HREAD4(hp, SDHC_MAX_CAPABILITIES));
976 }
977 #endif
978