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