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