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