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