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