sdhc.c revision 1.80 1 /* $NetBSD: sdhc.c,v 1.80 2015/08/05 12:28:47 jmcneill Exp $ */
2 /* $OpenBSD: sdhc.c,v 1.25 2009/01/13 19:44:20 grange Exp $ */
3
4 /*
5 * Copyright (c) 2006 Uwe Stuehler <uwe (at) openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /*
21 * SD Host Controller driver based on the SD Host Controller Standard
22 * Simplified Specification Version 1.00 (www.sdcard.com).
23 */
24
25 #include <sys/cdefs.h>
26 __KERNEL_RCSID(0, "$NetBSD: sdhc.c,v 1.80 2015/08/05 12:28:47 jmcneill Exp $");
27
28 #ifdef _KERNEL_OPT
29 #include "opt_sdmmc.h"
30 #endif
31
32 #include <sys/param.h>
33 #include <sys/device.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/systm.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/atomic.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*3)
59 #define SDHC_TUNING_TIMEOUT hz
60
61 struct sdhc_host {
62 struct sdhc_softc *sc; /* host controller device */
63
64 bus_space_tag_t iot; /* host register set tag */
65 bus_space_handle_t ioh; /* host register set handle */
66 bus_size_t ios; /* host register space size */
67 bus_dma_tag_t dmat; /* host DMA tag */
68
69 device_t sdmmc; /* generic SD/MMC device */
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 kmutex_t intr_lock;
80 kcondvar_t intr_cv;
81
82 callout_t tuning_timer;
83 int tuning_timing;
84 u_int tuning_timer_count;
85 u_int tuning_timer_pending;
86
87 int specver; /* spec. version */
88
89 uint32_t flags; /* flags for this host */
90 #define SHF_USE_DMA 0x0001
91 #define SHF_USE_4BIT_MODE 0x0002
92 #define SHF_USE_8BIT_MODE 0x0004
93 #define SHF_MODE_DMAEN 0x0008 /* needs SDHC_DMA_ENABLE in mode */
94 #define SHF_USE_ADMA2_32 0x0010
95 #define SHF_USE_ADMA2_64 0x0020
96 #define SHF_USE_ADMA2_MASK 0x0030
97
98 bus_dmamap_t adma_map;
99 bus_dma_segment_t adma_segs[1];
100 void *adma2;
101 };
102
103 #define HDEVNAME(hp) (device_xname((hp)->sc->sc_dev))
104
105 static uint8_t
106 hread1(struct sdhc_host *hp, bus_size_t reg)
107 {
108
109 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
110 return bus_space_read_1(hp->iot, hp->ioh, reg);
111 return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 3));
112 }
113
114 static uint16_t
115 hread2(struct sdhc_host *hp, bus_size_t reg)
116 {
117
118 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS))
119 return bus_space_read_2(hp->iot, hp->ioh, reg);
120 return bus_space_read_4(hp->iot, hp->ioh, reg & -4) >> (8 * (reg & 2));
121 }
122
123 #define HREAD1(hp, reg) hread1(hp, reg)
124 #define HREAD2(hp, reg) hread2(hp, reg)
125 #define HREAD4(hp, reg) \
126 (bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
127
128
129 static void
130 hwrite1(struct sdhc_host *hp, bus_size_t o, uint8_t val)
131 {
132
133 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
134 bus_space_write_1(hp->iot, hp->ioh, o, val);
135 } else {
136 const size_t shift = 8 * (o & 3);
137 o &= -4;
138 uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
139 tmp = (val << shift) | (tmp & ~(0xff << shift));
140 bus_space_write_4(hp->iot, hp->ioh, o, tmp);
141 }
142 }
143
144 static void
145 hwrite2(struct sdhc_host *hp, bus_size_t o, uint16_t val)
146 {
147
148 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
149 bus_space_write_2(hp->iot, hp->ioh, o, val);
150 } else {
151 const size_t shift = 8 * (o & 2);
152 o &= -4;
153 uint32_t tmp = bus_space_read_4(hp->iot, hp->ioh, o);
154 tmp = (val << shift) | (tmp & ~(0xffff << shift));
155 bus_space_write_4(hp->iot, hp->ioh, o, tmp);
156 }
157 }
158
159 #define HWRITE1(hp, reg, val) hwrite1(hp, reg, val)
160 #define HWRITE2(hp, reg, val) hwrite2(hp, reg, val)
161 #define HWRITE4(hp, reg, val) \
162 bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val))
163
164 #define HCLR1(hp, reg, bits) \
165 do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits)); while (0)
166 #define HCLR2(hp, reg, bits) \
167 do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits)); while (0)
168 #define HCLR4(hp, reg, bits) \
169 do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) & ~(bits)); while (0)
170 #define HSET1(hp, reg, bits) \
171 do if (bits) HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits)); while (0)
172 #define HSET2(hp, reg, bits) \
173 do if (bits) HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits)); while (0)
174 #define HSET4(hp, reg, bits) \
175 do if (bits) HWRITE4((hp), (reg), HREAD4((hp), (reg)) | (bits)); while (0)
176
177 static int sdhc_host_reset(sdmmc_chipset_handle_t);
178 static int sdhc_host_reset1(sdmmc_chipset_handle_t);
179 static uint32_t sdhc_host_ocr(sdmmc_chipset_handle_t);
180 static int sdhc_host_maxblklen(sdmmc_chipset_handle_t);
181 static int sdhc_card_detect(sdmmc_chipset_handle_t);
182 static int sdhc_write_protect(sdmmc_chipset_handle_t);
183 static int sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t);
184 static int sdhc_bus_clock_ddr(sdmmc_chipset_handle_t, int, bool);
185 static int sdhc_bus_width(sdmmc_chipset_handle_t, int);
186 static int sdhc_bus_rod(sdmmc_chipset_handle_t, int);
187 static void sdhc_card_enable_intr(sdmmc_chipset_handle_t, int);
188 static void sdhc_card_intr_ack(sdmmc_chipset_handle_t);
189 static void sdhc_exec_command(sdmmc_chipset_handle_t,
190 struct sdmmc_command *);
191 static int sdhc_signal_voltage(sdmmc_chipset_handle_t, int);
192 static int sdhc_execute_tuning(sdmmc_chipset_handle_t, int);
193 static void sdhc_tuning_timer(void *);
194 static int sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
195 static int sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t);
196 static int sdhc_soft_reset(struct sdhc_host *, int);
197 static int sdhc_wait_intr(struct sdhc_host *, int, int);
198 static void sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
199 static int sdhc_transfer_data_dma(struct sdhc_host *, struct sdmmc_command *);
200 static int sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *);
201 static void sdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
202 static void sdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
203 static void esdhc_read_data_pio(struct sdhc_host *, uint8_t *, u_int);
204 static void esdhc_write_data_pio(struct sdhc_host *, uint8_t *, u_int);
205
206 static struct sdmmc_chip_functions sdhc_functions = {
207 /* host controller reset */
208 .host_reset = sdhc_host_reset,
209
210 /* host controller capabilities */
211 .host_ocr = sdhc_host_ocr,
212 .host_maxblklen = sdhc_host_maxblklen,
213
214 /* card detection */
215 .card_detect = sdhc_card_detect,
216
217 /* write protect */
218 .write_protect = sdhc_write_protect,
219
220 /* bus power, clock frequency, width and ROD(OpenDrain/PushPull) */
221 .bus_power = sdhc_bus_power,
222 .bus_clock = NULL, /* see sdhc_bus_clock_ddr */
223 .bus_width = sdhc_bus_width,
224 .bus_rod = sdhc_bus_rod,
225
226 /* command execution */
227 .exec_command = sdhc_exec_command,
228
229 /* card interrupt */
230 .card_enable_intr = sdhc_card_enable_intr,
231 .card_intr_ack = sdhc_card_intr_ack,
232
233 /* UHS functions */
234 .signal_voltage = sdhc_signal_voltage,
235 .bus_clock_ddr = sdhc_bus_clock_ddr,
236 .execute_tuning = sdhc_execute_tuning,
237 };
238
239 static int
240 sdhc_cfprint(void *aux, const char *pnp)
241 {
242 const struct sdmmcbus_attach_args * const saa = aux;
243 const struct sdhc_host * const hp = saa->saa_sch;
244
245 if (pnp) {
246 aprint_normal("sdmmc at %s", pnp);
247 }
248 for (size_t host = 0; host < hp->sc->sc_nhosts; host++) {
249 if (hp->sc->sc_host[host] == hp) {
250 aprint_normal(" slot %zu", host);
251 }
252 }
253
254 return UNCONF;
255 }
256
257 /*
258 * Called by attachment driver. For each SD card slot there is one SD
259 * host controller standard register set. (1.3)
260 */
261 int
262 sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
263 bus_space_handle_t ioh, bus_size_t iosize)
264 {
265 struct sdmmcbus_attach_args saa;
266 struct sdhc_host *hp;
267 uint32_t caps, caps2;
268 uint16_t sdhcver;
269 int error;
270
271 /* Allocate one more host structure. */
272 hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO);
273 if (hp == NULL) {
274 aprint_error_dev(sc->sc_dev,
275 "couldn't alloc memory (sdhc host)\n");
276 goto err1;
277 }
278 sc->sc_host[sc->sc_nhosts++] = hp;
279
280 /* Fill in the new host structure. */
281 hp->sc = sc;
282 hp->iot = iot;
283 hp->ioh = ioh;
284 hp->ios = iosize;
285 hp->dmat = sc->sc_dmat;
286
287 mutex_init(&hp->intr_lock, MUTEX_DEFAULT, IPL_SDMMC);
288 cv_init(&hp->intr_cv, "sdhcintr");
289 callout_init(&hp->tuning_timer, CALLOUT_MPSAFE);
290 callout_setfunc(&hp->tuning_timer, sdhc_tuning_timer, hp);
291
292 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
293 sdhcver = HREAD4(hp, SDHC_ESDHC_HOST_CTL_VERSION);
294 } else {
295 sdhcver = HREAD2(hp, SDHC_HOST_CTL_VERSION);
296 }
297 aprint_normal_dev(sc->sc_dev, "SDHC ");
298 hp->specver = SDHC_SPEC_VERSION(sdhcver);
299 switch (SDHC_SPEC_VERSION(sdhcver)) {
300 case SDHC_SPEC_VERS_100:
301 aprint_normal("1.0");
302 break;
303
304 case SDHC_SPEC_VERS_200:
305 aprint_normal("2.0");
306 break;
307
308 case SDHC_SPEC_VERS_300:
309 aprint_normal("3.0");
310 break;
311
312 case SDHC_SPEC_VERS_400:
313 aprint_normal("4.0");
314 break;
315
316 default:
317 aprint_normal("unknown version(0x%x)",
318 SDHC_SPEC_VERSION(sdhcver));
319 break;
320 }
321 aprint_normal(", rev %u", SDHC_VENDOR_VERSION(sdhcver));
322
323 /*
324 * Reset the host controller and enable interrupts.
325 */
326 (void)sdhc_host_reset(hp);
327
328 /* Determine host capabilities. */
329 if (ISSET(sc->sc_flags, SDHC_FLAG_HOSTCAPS)) {
330 caps = sc->sc_caps;
331 caps2 = sc->sc_caps2;
332 } else {
333 caps = sc->sc_caps = HREAD4(hp, SDHC_CAPABILITIES);
334 if (hp->specver >= SDHC_SPEC_VERS_300) {
335 caps2 = sc->sc_caps2 = HREAD4(hp, SDHC_CAPABILITIES2);
336 } else {
337 caps2 = sc->sc_caps2 = 0;
338 }
339 }
340
341 const u_int retuning_mode = (caps2 >> SDHC_RETUNING_MODES_SHIFT) &
342 SDHC_RETUNING_MODES_MASK;
343 if (retuning_mode == SDHC_RETUNING_MODE_1) {
344 hp->tuning_timer_count = (caps2 >> SDHC_TIMER_COUNT_SHIFT) &
345 SDHC_TIMER_COUNT_MASK;
346 if (hp->tuning_timer_count == 0xf)
347 hp->tuning_timer_count = 0;
348 if (hp->tuning_timer_count)
349 hp->tuning_timer_count =
350 1 << (hp->tuning_timer_count - 1);
351 }
352
353 /*
354 * Use DMA if the host system and the controller support it.
355 * Suports integrated or external DMA egine, with or without
356 * SDHC_DMA_ENABLE in the command.
357 */
358 if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA) ||
359 (ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA &&
360 ISSET(caps, SDHC_DMA_SUPPORT)))) {
361 SET(hp->flags, SHF_USE_DMA);
362
363 if (ISSET(sc->sc_flags, SDHC_FLAG_USE_ADMA2) &&
364 ISSET(caps, SDHC_ADMA2_SUPP)) {
365 SET(hp->flags, SHF_MODE_DMAEN);
366 /*
367 * 64-bit mode was present in the 2.00 spec, removed
368 * from 3.00, and re-added in 4.00 with a different
369 * descriptor layout. We only support 2.00 and 3.00
370 * descriptors for now.
371 */
372 if (hp->specver == SDHC_SPEC_VERS_200 &&
373 ISSET(caps, SDHC_64BIT_SYS_BUS)) {
374 SET(hp->flags, SHF_USE_ADMA2_64);
375 aprint_normal(", 64-bit ADMA2");
376 } else {
377 SET(hp->flags, SHF_USE_ADMA2_32);
378 aprint_normal(", 32-bit ADMA2");
379 }
380 } else {
381 if (!ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA) ||
382 ISSET(sc->sc_flags, SDHC_FLAG_EXTDMA_DMAEN))
383 SET(hp->flags, SHF_MODE_DMAEN);
384 if (sc->sc_vendor_transfer_data_dma) {
385 aprint_normal(", platform DMA");
386 } else {
387 aprint_normal(", SDMA");
388 }
389 }
390 } else {
391 aprint_normal(", PIO");
392 }
393
394 /*
395 * Determine the base clock frequency. (2.2.24)
396 */
397 if (hp->specver >= SDHC_SPEC_VERS_300) {
398 hp->clkbase = SDHC_BASE_V3_FREQ_KHZ(caps);
399 } else {
400 hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
401 }
402 if (hp->clkbase == 0 ||
403 ISSET(sc->sc_flags, SDHC_FLAG_NO_CLKBASE)) {
404 if (sc->sc_clkbase == 0) {
405 /* The attachment driver must tell us. */
406 aprint_error_dev(sc->sc_dev,
407 "unknown base clock frequency\n");
408 goto err;
409 }
410 hp->clkbase = sc->sc_clkbase;
411 }
412 if (hp->clkbase < 10000 || hp->clkbase > 10000 * 256) {
413 /* SDHC 1.0 supports only 10-63 MHz. */
414 aprint_error_dev(sc->sc_dev,
415 "base clock frequency out of range: %u MHz\n",
416 hp->clkbase / 1000);
417 goto err;
418 }
419 aprint_normal(", %u kHz", hp->clkbase);
420
421 /*
422 * XXX Set the data timeout counter value according to
423 * capabilities. (2.2.15)
424 */
425 HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
426 #if 1
427 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
428 HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
429 #endif
430
431 if (ISSET(caps, SDHC_EMBEDDED_SLOT))
432 aprint_normal(", embedded slot");
433
434 /*
435 * Determine SD bus voltage levels supported by the controller.
436 */
437 aprint_normal(",");
438 if (ISSET(caps, SDHC_HIGH_SPEED_SUPP)) {
439 SET(hp->ocr, MMC_OCR_HCS);
440 aprint_normal(" HS");
441 }
442 if (ISSET(caps2, SDHC_SDR50_SUPP)) {
443 SET(hp->ocr, MMC_OCR_S18A);
444 aprint_normal(" SDR50");
445 }
446 if (ISSET(caps2, SDHC_DDR50_SUPP)) {
447 SET(hp->ocr, MMC_OCR_S18A);
448 aprint_normal(" DDR50");
449 }
450 if (ISSET(caps2, SDHC_SDR104_SUPP)) {
451 SET(hp->ocr, MMC_OCR_S18A);
452 aprint_normal(" SDR104 HS200");
453 }
454 if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V)) {
455 SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V);
456 aprint_normal(" 1.8V");
457 }
458 if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V)) {
459 SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
460 aprint_normal(" 3.0V");
461 }
462 if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V)) {
463 SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
464 aprint_normal(" 3.3V");
465 }
466 if (hp->specver >= SDHC_SPEC_VERS_300) {
467 aprint_normal(", re-tuning mode %d", retuning_mode + 1);
468 if (hp->tuning_timer_count)
469 aprint_normal(" (%us timer)", hp->tuning_timer_count);
470 }
471
472 /*
473 * Determine the maximum block length supported by the host
474 * controller. (2.2.24)
475 */
476 switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
477 case SDHC_MAX_BLK_LEN_512:
478 hp->maxblklen = 512;
479 break;
480
481 case SDHC_MAX_BLK_LEN_1024:
482 hp->maxblklen = 1024;
483 break;
484
485 case SDHC_MAX_BLK_LEN_2048:
486 hp->maxblklen = 2048;
487 break;
488
489 case SDHC_MAX_BLK_LEN_4096:
490 hp->maxblklen = 4096;
491 break;
492
493 default:
494 aprint_error_dev(sc->sc_dev, "max block length unknown\n");
495 goto err;
496 }
497 aprint_normal(", %u byte blocks", hp->maxblklen);
498 aprint_normal("\n");
499
500 if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
501 int rseg;
502
503 /* Allocate ADMA2 descriptor memory */
504 error = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, PAGE_SIZE,
505 PAGE_SIZE, hp->adma_segs, 1, &rseg, BUS_DMA_WAITOK);
506 if (error) {
507 aprint_error_dev(sc->sc_dev,
508 "ADMA2 dmamem_alloc failed (%d)\n", error);
509 goto adma_done;
510 }
511 error = bus_dmamem_map(sc->sc_dmat, hp->adma_segs, rseg,
512 PAGE_SIZE, (void **)&hp->adma2, BUS_DMA_WAITOK);
513 if (error) {
514 aprint_error_dev(sc->sc_dev,
515 "ADMA2 dmamem_map failed (%d)\n", error);
516 goto adma_done;
517 }
518 error = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1, PAGE_SIZE,
519 0, BUS_DMA_WAITOK, &hp->adma_map);
520 if (error) {
521 aprint_error_dev(sc->sc_dev,
522 "ADMA2 dmamap_create failed (%d)\n", error);
523 goto adma_done;
524 }
525 error = bus_dmamap_load(sc->sc_dmat, hp->adma_map,
526 hp->adma2, PAGE_SIZE, NULL,
527 BUS_DMA_WAITOK|BUS_DMA_WRITE);
528 if (error) {
529 aprint_error_dev(sc->sc_dev,
530 "ADMA2 dmamap_load failed (%d)\n", error);
531 goto adma_done;
532 }
533
534 memset(hp->adma2, 0, PAGE_SIZE);
535
536 adma_done:
537 if (error)
538 CLR(hp->flags, SHF_USE_ADMA2_MASK);
539 }
540
541 /*
542 * Attach the generic SD/MMC bus driver. (The bus driver must
543 * not invoke any chipset functions before it is attached.)
544 */
545 memset(&saa, 0, sizeof(saa));
546 saa.saa_busname = "sdmmc";
547 saa.saa_sct = &sdhc_functions;
548 saa.saa_sch = hp;
549 saa.saa_dmat = hp->dmat;
550 saa.saa_clkmax = hp->clkbase;
551 if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_CGM))
552 saa.saa_clkmin = hp->clkbase / 256 / 2046;
553 else if (ISSET(sc->sc_flags, SDHC_FLAG_HAVE_DVS))
554 saa.saa_clkmin = hp->clkbase / 256 / 16;
555 else if (hp->sc->sc_clkmsk != 0)
556 saa.saa_clkmin = hp->clkbase / (hp->sc->sc_clkmsk >>
557 (ffs(hp->sc->sc_clkmsk) - 1));
558 else if (hp->specver >= SDHC_SPEC_VERS_300)
559 saa.saa_clkmin = hp->clkbase / 0x3ff;
560 else
561 saa.saa_clkmin = hp->clkbase / 256;
562 saa.saa_caps = SMC_CAPS_4BIT_MODE|SMC_CAPS_AUTO_STOP;
563 if (ISSET(sc->sc_flags, SDHC_FLAG_8BIT_MODE))
564 saa.saa_caps |= SMC_CAPS_8BIT_MODE;
565 if (ISSET(caps, SDHC_HIGH_SPEED_SUPP))
566 saa.saa_caps |= SMC_CAPS_SD_HIGHSPEED;
567 if (ISSET(caps2, SDHC_SDR104_SUPP))
568 saa.saa_caps |= SMC_CAPS_UHS_SDR104 |
569 SMC_CAPS_UHS_SDR50 |
570 SMC_CAPS_MMC_HS200;
571 if (ISSET(caps2, SDHC_SDR50_SUPP))
572 saa.saa_caps |= SMC_CAPS_UHS_SDR50;
573 if (ISSET(caps2, SDHC_DDR50_SUPP))
574 saa.saa_caps |= SMC_CAPS_UHS_DDR50;
575 if (ISSET(hp->flags, SHF_USE_DMA)) {
576 saa.saa_caps |= SMC_CAPS_DMA;
577 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
578 saa.saa_caps |= SMC_CAPS_MULTI_SEG_DMA;
579 }
580 if (ISSET(sc->sc_flags, SDHC_FLAG_SINGLE_ONLY))
581 saa.saa_caps |= SMC_CAPS_SINGLE_ONLY;
582 if (ISSET(sc->sc_flags, SDHC_FLAG_POLL_CARD_DET))
583 saa.saa_caps |= SMC_CAPS_POLL_CARD_DET;
584 hp->sdmmc = config_found(sc->sc_dev, &saa, sdhc_cfprint);
585
586 return 0;
587
588 err:
589 callout_destroy(&hp->tuning_timer);
590 cv_destroy(&hp->intr_cv);
591 mutex_destroy(&hp->intr_lock);
592 free(hp, M_DEVBUF);
593 sc->sc_host[--sc->sc_nhosts] = NULL;
594 err1:
595 return 1;
596 }
597
598 int
599 sdhc_detach(struct sdhc_softc *sc, int flags)
600 {
601 struct sdhc_host *hp;
602 int rv = 0;
603
604 for (size_t n = 0; n < sc->sc_nhosts; n++) {
605 hp = sc->sc_host[n];
606 if (hp == NULL)
607 continue;
608 if (hp->sdmmc != NULL) {
609 rv = config_detach(hp->sdmmc, flags);
610 if (rv)
611 break;
612 hp->sdmmc = NULL;
613 }
614 /* disable interrupts */
615 if ((flags & DETACH_FORCE) == 0) {
616 mutex_enter(&hp->intr_lock);
617 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
618 HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
619 } else {
620 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
621 }
622 sdhc_soft_reset(hp, SDHC_RESET_ALL);
623 mutex_exit(&hp->intr_lock);
624 }
625 callout_halt(&hp->tuning_timer, NULL);
626 callout_destroy(&hp->tuning_timer);
627 cv_destroy(&hp->intr_cv);
628 mutex_destroy(&hp->intr_lock);
629 if (hp->ios > 0) {
630 bus_space_unmap(hp->iot, hp->ioh, hp->ios);
631 hp->ios = 0;
632 }
633 if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
634 bus_dmamap_unload(sc->sc_dmat, hp->adma_map);
635 bus_dmamap_destroy(sc->sc_dmat, hp->adma_map);
636 bus_dmamem_unmap(sc->sc_dmat, hp->adma2, PAGE_SIZE);
637 bus_dmamem_free(sc->sc_dmat, hp->adma_segs, 1);
638 }
639 free(hp, M_DEVBUF);
640 sc->sc_host[n] = NULL;
641 }
642
643 return rv;
644 }
645
646 bool
647 sdhc_suspend(device_t dev, const pmf_qual_t *qual)
648 {
649 struct sdhc_softc *sc = device_private(dev);
650 struct sdhc_host *hp;
651 size_t i;
652
653 /* XXX poll for command completion or suspend command
654 * in progress */
655
656 /* Save the host controller state. */
657 for (size_t n = 0; n < sc->sc_nhosts; n++) {
658 hp = sc->sc_host[n];
659 if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
660 for (i = 0; i < sizeof hp->regs; i += 4) {
661 uint32_t v = HREAD4(hp, i);
662 hp->regs[i + 0] = (v >> 0);
663 hp->regs[i + 1] = (v >> 8);
664 if (i + 3 < sizeof hp->regs) {
665 hp->regs[i + 2] = (v >> 16);
666 hp->regs[i + 3] = (v >> 24);
667 }
668 }
669 } else {
670 for (i = 0; i < sizeof hp->regs; i++) {
671 hp->regs[i] = HREAD1(hp, i);
672 }
673 }
674 }
675 return true;
676 }
677
678 bool
679 sdhc_resume(device_t dev, const pmf_qual_t *qual)
680 {
681 struct sdhc_softc *sc = device_private(dev);
682 struct sdhc_host *hp;
683 size_t i;
684
685 /* Restore the host controller state. */
686 for (size_t n = 0; n < sc->sc_nhosts; n++) {
687 hp = sc->sc_host[n];
688 (void)sdhc_host_reset(hp);
689 if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
690 for (i = 0; i < sizeof hp->regs; i += 4) {
691 if (i + 3 < sizeof hp->regs) {
692 HWRITE4(hp, i,
693 (hp->regs[i + 0] << 0)
694 | (hp->regs[i + 1] << 8)
695 | (hp->regs[i + 2] << 16)
696 | (hp->regs[i + 3] << 24));
697 } else {
698 HWRITE4(hp, i,
699 (hp->regs[i + 0] << 0)
700 | (hp->regs[i + 1] << 8));
701 }
702 }
703 } else {
704 for (i = 0; i < sizeof hp->regs; i++) {
705 HWRITE1(hp, i, hp->regs[i]);
706 }
707 }
708 }
709 return true;
710 }
711
712 bool
713 sdhc_shutdown(device_t dev, int flags)
714 {
715 struct sdhc_softc *sc = device_private(dev);
716 struct sdhc_host *hp;
717
718 /* XXX chip locks up if we don't disable it before reboot. */
719 for (size_t i = 0; i < sc->sc_nhosts; i++) {
720 hp = sc->sc_host[i];
721 (void)sdhc_host_reset(hp);
722 }
723 return true;
724 }
725
726 /*
727 * Reset the host controller. Called during initialization, when
728 * cards are removed, upon resume, and during error recovery.
729 */
730 static int
731 sdhc_host_reset1(sdmmc_chipset_handle_t sch)
732 {
733 struct sdhc_host *hp = (struct sdhc_host *)sch;
734 uint32_t sdhcimask;
735 int error;
736
737 KASSERT(mutex_owned(&hp->intr_lock));
738
739 /* Disable all interrupts. */
740 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
741 HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, 0);
742 } else {
743 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
744 }
745
746 /*
747 * Reset the entire host controller and wait up to 100ms for
748 * the controller to clear the reset bit.
749 */
750 error = sdhc_soft_reset(hp, SDHC_RESET_ALL);
751 if (error)
752 goto out;
753
754 /* Set data timeout counter value to max for now. */
755 HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
756 #if 1
757 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
758 HWRITE4(hp, SDHC_NINTR_STATUS, SDHC_CMD_TIMEOUT_ERROR << 16);
759 #endif
760
761 /* Enable interrupts. */
762 sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
763 SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
764 SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
765 SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
766 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
767 sdhcimask |= SDHC_EINTR_STATUS_MASK << 16;
768 HWRITE4(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
769 sdhcimask ^=
770 (SDHC_EINTR_STATUS_MASK ^ SDHC_EINTR_SIGNAL_MASK) << 16;
771 sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
772 HWRITE4(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
773 } else {
774 HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask);
775 HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
776 sdhcimask ^= SDHC_BUFFER_READ_READY ^ SDHC_BUFFER_WRITE_READY;
777 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask);
778 HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
779 }
780
781 out:
782 return error;
783 }
784
785 static int
786 sdhc_host_reset(sdmmc_chipset_handle_t sch)
787 {
788 struct sdhc_host *hp = (struct sdhc_host *)sch;
789 int error;
790
791 mutex_enter(&hp->intr_lock);
792 error = sdhc_host_reset1(sch);
793 mutex_exit(&hp->intr_lock);
794
795 return error;
796 }
797
798 static uint32_t
799 sdhc_host_ocr(sdmmc_chipset_handle_t sch)
800 {
801 struct sdhc_host *hp = (struct sdhc_host *)sch;
802
803 return hp->ocr;
804 }
805
806 static int
807 sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
808 {
809 struct sdhc_host *hp = (struct sdhc_host *)sch;
810
811 return hp->maxblklen;
812 }
813
814 /*
815 * Return non-zero if the card is currently inserted.
816 */
817 static int
818 sdhc_card_detect(sdmmc_chipset_handle_t sch)
819 {
820 struct sdhc_host *hp = (struct sdhc_host *)sch;
821 int r;
822
823 if (hp->sc->sc_vendor_card_detect)
824 return (*hp->sc->sc_vendor_card_detect)(hp->sc);
825
826 r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED);
827
828 return r ? 1 : 0;
829 }
830
831 /*
832 * Return non-zero if the card is currently write-protected.
833 */
834 static int
835 sdhc_write_protect(sdmmc_chipset_handle_t sch)
836 {
837 struct sdhc_host *hp = (struct sdhc_host *)sch;
838 int r;
839
840 if (hp->sc->sc_vendor_write_protect)
841 return (*hp->sc->sc_vendor_write_protect)(hp->sc);
842
843 r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH);
844
845 return r ? 0 : 1;
846 }
847
848 /*
849 * Set or change SD bus voltage and enable or disable SD bus power.
850 * Return zero on success.
851 */
852 static int
853 sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr)
854 {
855 struct sdhc_host *hp = (struct sdhc_host *)sch;
856 uint8_t vdd;
857 int error = 0;
858 const uint32_t pcmask =
859 ~(SDHC_BUS_POWER | (SDHC_VOLTAGE_MASK << SDHC_VOLTAGE_SHIFT));
860
861 mutex_enter(&hp->intr_lock);
862
863 /*
864 * Disable bus power before voltage change.
865 */
866 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)
867 && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_PWR0))
868 HWRITE1(hp, SDHC_POWER_CTL, 0);
869
870 /* If power is disabled, reset the host and return now. */
871 if (ocr == 0) {
872 (void)sdhc_host_reset1(hp);
873 callout_halt(&hp->tuning_timer, &hp->intr_lock);
874 goto out;
875 }
876
877 /*
878 * Select the lowest voltage according to capabilities.
879 */
880 ocr &= hp->ocr;
881 if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V)) {
882 vdd = SDHC_VOLTAGE_1_8V;
883 } else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V)) {
884 vdd = SDHC_VOLTAGE_3_0V;
885 } else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) {
886 vdd = SDHC_VOLTAGE_3_3V;
887 } else {
888 /* Unsupported voltage level requested. */
889 error = EINVAL;
890 goto out;
891 }
892
893 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
894 /*
895 * Enable bus power. Wait at least 1 ms (or 74 clocks) plus
896 * voltage ramp until power rises.
897 */
898
899 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_SINGLE_POWER_WRITE)) {
900 HWRITE1(hp, SDHC_POWER_CTL,
901 (vdd << SDHC_VOLTAGE_SHIFT) | SDHC_BUS_POWER);
902 } else {
903 HWRITE1(hp, SDHC_POWER_CTL,
904 HREAD1(hp, SDHC_POWER_CTL) & pcmask);
905 sdmmc_delay(1);
906 HWRITE1(hp, SDHC_POWER_CTL,
907 (vdd << SDHC_VOLTAGE_SHIFT));
908 sdmmc_delay(1);
909 HSET1(hp, SDHC_POWER_CTL, SDHC_BUS_POWER);
910 sdmmc_delay(10000);
911 }
912
913 /*
914 * The host system may not power the bus due to battery low,
915 * etc. In that case, the host controller should clear the
916 * bus power bit.
917 */
918 if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
919 error = ENXIO;
920 goto out;
921 }
922 }
923
924 out:
925 mutex_exit(&hp->intr_lock);
926
927 return error;
928 }
929
930 /*
931 * Return the smallest possible base clock frequency divisor value
932 * for the CLOCK_CTL register to produce `freq' (KHz).
933 */
934 static bool
935 sdhc_clock_divisor(struct sdhc_host *hp, u_int freq, u_int *divp)
936 {
937 u_int div;
938
939 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_CGM)) {
940 for (div = hp->clkbase / freq; div <= 0x3ff; div++) {
941 if ((hp->clkbase / div) <= freq) {
942 *divp = SDHC_SDCLK_CGM
943 | ((div & 0x300) << SDHC_SDCLK_XDIV_SHIFT)
944 | ((div & 0x0ff) << SDHC_SDCLK_DIV_SHIFT);
945 //freq = hp->clkbase / div;
946 return true;
947 }
948 }
949 /* No divisor found. */
950 return false;
951 }
952 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_HAVE_DVS)) {
953 u_int dvs = (hp->clkbase + freq - 1) / freq;
954 u_int roundup = dvs & 1;
955 for (dvs >>= 1, div = 1; div <= 256; div <<= 1, dvs >>= 1) {
956 if (dvs + roundup <= 16) {
957 dvs += roundup - 1;
958 *divp = (div << SDHC_SDCLK_DIV_SHIFT)
959 | (dvs << SDHC_SDCLK_DVS_SHIFT);
960 DPRINTF(2,
961 ("%s: divisor for freq %u is %u * %u\n",
962 HDEVNAME(hp), freq, div * 2, dvs + 1));
963 //freq = hp->clkbase / (div * 2) * (dvs + 1);
964 return true;
965 }
966 /*
967 * If we drop bits, we need to round up the divisor.
968 */
969 roundup |= dvs & 1;
970 }
971 /* No divisor found. */
972 return false;
973 }
974 if (hp->sc->sc_clkmsk != 0) {
975 div = howmany(hp->clkbase, freq);
976 if (div > (hp->sc->sc_clkmsk >> (ffs(hp->sc->sc_clkmsk) - 1)))
977 return false;
978 *divp = div << (ffs(hp->sc->sc_clkmsk) - 1);
979 //freq = hp->clkbase / div;
980 return true;
981 }
982 if (hp->specver >= SDHC_SPEC_VERS_300) {
983 div = howmany(hp->clkbase, freq);
984 div = div > 1 ? howmany(div, 2) : 0;
985 if (div > 0x3ff)
986 return false;
987 *divp = (((div >> 8) & SDHC_SDCLK_XDIV_MASK)
988 << SDHC_SDCLK_XDIV_SHIFT) |
989 (((div >> 0) & SDHC_SDCLK_DIV_MASK)
990 << SDHC_SDCLK_DIV_SHIFT);
991 //freq = hp->clkbase / (div ? div * 2 : 1);
992 return true;
993 } else {
994 for (div = 1; div <= 256; div *= 2) {
995 if ((hp->clkbase / div) <= freq) {
996 *divp = (div / 2) << SDHC_SDCLK_DIV_SHIFT;
997 //freq = hp->clkbase / div;
998 return true;
999 }
1000 }
1001 /* No divisor found. */
1002 return false;
1003 }
1004 /* No divisor found. */
1005 return false;
1006 }
1007
1008 /*
1009 * Set or change SDCLK frequency or disable the SD clock.
1010 * Return zero on success.
1011 */
1012 static int
1013 sdhc_bus_clock_ddr(sdmmc_chipset_handle_t sch, int freq, bool ddr)
1014 {
1015 struct sdhc_host *hp = (struct sdhc_host *)sch;
1016 u_int div;
1017 u_int timo;
1018 int16_t reg;
1019 int error = 0;
1020 bool present __diagused;
1021
1022 mutex_enter(&hp->intr_lock);
1023
1024 #ifdef DIAGNOSTIC
1025 present = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK);
1026
1027 /* Must not stop the clock if commands are in progress. */
1028 if (present && sdhc_card_detect(hp)) {
1029 aprint_normal_dev(hp->sc->sc_dev,
1030 "%s: command in progress\n", __func__);
1031 }
1032 #endif
1033
1034 if (hp->sc->sc_vendor_bus_clock) {
1035 error = (*hp->sc->sc_vendor_bus_clock)(hp->sc, freq);
1036 if (error != 0)
1037 goto out;
1038 }
1039
1040 /*
1041 * Stop SD clock before changing the frequency.
1042 */
1043 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1044 HCLR4(hp, SDHC_CLOCK_CTL, 0xfff8);
1045 if (freq == SDMMC_SDCLK_OFF) {
1046 HSET4(hp, SDHC_CLOCK_CTL, 0x80f0);
1047 goto out;
1048 }
1049 } else {
1050 HCLR2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
1051 if (freq == SDMMC_SDCLK_OFF)
1052 goto out;
1053 }
1054
1055 if (hp->specver >= SDHC_SPEC_VERS_300) {
1056 HCLR2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_MASK);
1057 if (freq > 100000) {
1058 HSET2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_SDR104);
1059 } else if (freq > 50000) {
1060 HSET2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_SDR50);
1061 } else if (freq > 25000) {
1062 if (ddr) {
1063 HSET2(hp, SDHC_HOST_CTL2,
1064 SDHC_UHS_MODE_SELECT_DDR50);
1065 } else {
1066 HSET2(hp, SDHC_HOST_CTL2,
1067 SDHC_UHS_MODE_SELECT_SDR25);
1068 }
1069 } else if (freq > 400) {
1070 HSET2(hp, SDHC_HOST_CTL2, SDHC_UHS_MODE_SELECT_SDR12);
1071 }
1072 }
1073
1074 /*
1075 * Set the minimum base clock frequency divisor.
1076 */
1077 if (!sdhc_clock_divisor(hp, freq, &div)) {
1078 /* Invalid base clock frequency or `freq' value. */
1079 aprint_error_dev(hp->sc->sc_dev,
1080 "Invalid bus clock %d kHz\n", freq);
1081 error = EINVAL;
1082 goto out;
1083 }
1084 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1085 HWRITE4(hp, SDHC_CLOCK_CTL,
1086 div | (SDHC_TIMEOUT_MAX << 16));
1087 } else {
1088 reg = HREAD2(hp, SDHC_CLOCK_CTL);
1089 reg &= (SDHC_INTCLK_STABLE | SDHC_INTCLK_ENABLE);
1090 HWRITE2(hp, SDHC_CLOCK_CTL, reg | div);
1091 }
1092
1093 /*
1094 * Start internal clock. Wait 10ms for stabilization.
1095 */
1096 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1097 sdmmc_delay(10000);
1098 HSET4(hp, SDHC_CLOCK_CTL,
1099 8 | SDHC_INTCLK_ENABLE | SDHC_INTCLK_STABLE);
1100 } else {
1101 HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
1102 for (timo = 1000; timo > 0; timo--) {
1103 if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL),
1104 SDHC_INTCLK_STABLE))
1105 break;
1106 sdmmc_delay(10);
1107 }
1108 if (timo == 0) {
1109 error = ETIMEDOUT;
1110 goto out;
1111 }
1112 }
1113
1114 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1115 HSET1(hp, SDHC_SOFTWARE_RESET, SDHC_INIT_ACTIVE);
1116 /*
1117 * Sending 80 clocks at 400kHz takes 200us.
1118 * So delay for that time + slop and then
1119 * check a few times for completion.
1120 */
1121 sdmmc_delay(210);
1122 for (timo = 10; timo > 0; timo--) {
1123 if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET),
1124 SDHC_INIT_ACTIVE))
1125 break;
1126 sdmmc_delay(10);
1127 }
1128 DPRINTF(2,("%s: %u init spins\n", __func__, 10 - timo));
1129
1130 /*
1131 * Enable SD clock.
1132 */
1133 HSET4(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
1134 } else {
1135 /*
1136 * Enable SD clock.
1137 */
1138 HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
1139
1140 if (freq > 25000 &&
1141 !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_HS_BIT))
1142 HSET1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
1143 else
1144 HCLR1(hp, SDHC_HOST_CTL, SDHC_HIGH_SPEED);
1145 }
1146
1147 out:
1148 mutex_exit(&hp->intr_lock);
1149
1150 return error;
1151 }
1152
1153 static int
1154 sdhc_bus_width(sdmmc_chipset_handle_t sch, int width)
1155 {
1156 struct sdhc_host *hp = (struct sdhc_host *)sch;
1157 int reg;
1158
1159 switch (width) {
1160 case 1:
1161 case 4:
1162 break;
1163
1164 case 8:
1165 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_8BIT_MODE))
1166 break;
1167 /* FALLTHROUGH */
1168 default:
1169 DPRINTF(0,("%s: unsupported bus width (%d)\n",
1170 HDEVNAME(hp), width));
1171 return 1;
1172 }
1173
1174 mutex_enter(&hp->intr_lock);
1175
1176 reg = HREAD1(hp, SDHC_HOST_CTL);
1177 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1178 reg &= ~(SDHC_4BIT_MODE|SDHC_ESDHC_8BIT_MODE);
1179 if (width == 4)
1180 reg |= SDHC_4BIT_MODE;
1181 else if (width == 8)
1182 reg |= SDHC_ESDHC_8BIT_MODE;
1183 } else {
1184 reg &= ~SDHC_4BIT_MODE;
1185 if (hp->specver >= SDHC_SPEC_VERS_300) {
1186 reg &= ~SDHC_8BIT_MODE;
1187 }
1188 if (width == 4) {
1189 reg |= SDHC_4BIT_MODE;
1190 } else if (width == 8 && hp->specver >= SDHC_SPEC_VERS_300) {
1191 reg |= SDHC_8BIT_MODE;
1192 }
1193 }
1194 HWRITE1(hp, SDHC_HOST_CTL, reg);
1195
1196 mutex_exit(&hp->intr_lock);
1197
1198 return 0;
1199 }
1200
1201 static int
1202 sdhc_bus_rod(sdmmc_chipset_handle_t sch, int on)
1203 {
1204 struct sdhc_host *hp = (struct sdhc_host *)sch;
1205
1206 if (hp->sc->sc_vendor_rod)
1207 return (*hp->sc->sc_vendor_rod)(hp->sc, on);
1208
1209 return 0;
1210 }
1211
1212 static void
1213 sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable)
1214 {
1215 struct sdhc_host *hp = (struct sdhc_host *)sch;
1216
1217 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1218 mutex_enter(&hp->intr_lock);
1219 if (enable) {
1220 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
1221 HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
1222 } else {
1223 HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
1224 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
1225 }
1226 mutex_exit(&hp->intr_lock);
1227 }
1228 }
1229
1230 static void
1231 sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
1232 {
1233 struct sdhc_host *hp = (struct sdhc_host *)sch;
1234
1235 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1236 mutex_enter(&hp->intr_lock);
1237 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
1238 mutex_exit(&hp->intr_lock);
1239 }
1240 }
1241
1242 static int
1243 sdhc_signal_voltage(sdmmc_chipset_handle_t sch, int signal_voltage)
1244 {
1245 struct sdhc_host *hp = (struct sdhc_host *)sch;
1246
1247 mutex_enter(&hp->intr_lock);
1248 switch (signal_voltage) {
1249 case SDMMC_SIGNAL_VOLTAGE_180:
1250 HSET2(hp, SDHC_HOST_CTL2, SDHC_1_8V_SIGNAL_EN);
1251 break;
1252 case SDMMC_SIGNAL_VOLTAGE_330:
1253 HCLR2(hp, SDHC_HOST_CTL2, SDHC_1_8V_SIGNAL_EN);
1254 break;
1255 default:
1256 return EINVAL;
1257 }
1258 mutex_exit(&hp->intr_lock);
1259
1260 return 0;
1261 }
1262
1263 /*
1264 * Sampling clock tuning procedure (UHS)
1265 */
1266 static int
1267 sdhc_execute_tuning(sdmmc_chipset_handle_t sch, int timing)
1268 {
1269 struct sdhc_host *hp = (struct sdhc_host *)sch;
1270 struct sdmmc_command cmd;
1271 uint8_t hostctl;
1272 int opcode, error, retry = 40;
1273
1274 hp->tuning_timing = timing;
1275
1276 switch (timing) {
1277 case SDMMC_TIMING_MMC_HS200:
1278 opcode = MMC_SEND_TUNING_BLOCK_HS200;
1279 break;
1280 case SDMMC_TIMING_UHS_SDR50:
1281 if (!ISSET(hp->sc->sc_caps2, SDHC_TUNING_SDR50))
1282 return 0;
1283 /* FALLTHROUGH */
1284 case SDMMC_TIMING_UHS_SDR104:
1285 opcode = MMC_SEND_TUNING_BLOCK;
1286 break;
1287 default:
1288 return EINVAL;
1289 }
1290
1291 hostctl = HREAD1(hp, SDHC_HOST_CTL);
1292
1293 /* enable buffer read ready interrupt */
1294 HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_BUFFER_READ_READY);
1295 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_BUFFER_READ_READY);
1296
1297 /* disable DMA */
1298 HCLR1(hp, SDHC_HOST_CTL, SDHC_DMA_SELECT);
1299
1300 /* reset tuning circuit */
1301 HCLR2(hp, SDHC_HOST_CTL2, SDHC_SAMPLING_CLOCK_SEL);
1302
1303 /* start of tuning */
1304 HWRITE2(hp, SDHC_HOST_CTL2, SDHC_EXECUTE_TUNING);
1305
1306 mutex_enter(&hp->intr_lock);
1307 do {
1308 memset(&cmd, 0, sizeof(cmd));
1309 cmd.c_opcode = opcode;
1310 cmd.c_arg = 0;
1311 cmd.c_flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1;
1312 if (ISSET(hostctl, SDHC_8BIT_MODE)) {
1313 cmd.c_blklen = cmd.c_datalen = 128;
1314 } else {
1315 cmd.c_blklen = cmd.c_datalen = 64;
1316 }
1317
1318 error = sdhc_start_command(hp, &cmd);
1319 if (error)
1320 break;
1321
1322 if (!sdhc_wait_intr(hp, SDHC_BUFFER_READ_READY,
1323 SDHC_TUNING_TIMEOUT)) {
1324 break;
1325 }
1326
1327 delay(1000);
1328 } while (HREAD2(hp, SDHC_HOST_CTL2) & SDHC_EXECUTE_TUNING && --retry);
1329 mutex_exit(&hp->intr_lock);
1330
1331 /* disable buffer read ready interrupt */
1332 HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_BUFFER_READ_READY);
1333 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_BUFFER_READ_READY);
1334
1335 if (HREAD2(hp, SDHC_HOST_CTL2) & SDHC_EXECUTE_TUNING) {
1336 HCLR2(hp, SDHC_HOST_CTL2,
1337 SDHC_SAMPLING_CLOCK_SEL|SDHC_EXECUTE_TUNING);
1338 sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
1339 aprint_error_dev(hp->sc->sc_dev,
1340 "tuning did not complete, using fixed sampling clock\n");
1341 return EIO; /* tuning did not complete */
1342 }
1343
1344 if ((HREAD2(hp, SDHC_HOST_CTL2) & SDHC_SAMPLING_CLOCK_SEL) == 0) {
1345 HCLR2(hp, SDHC_HOST_CTL2,
1346 SDHC_SAMPLING_CLOCK_SEL|SDHC_EXECUTE_TUNING);
1347 sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
1348 aprint_error_dev(hp->sc->sc_dev,
1349 "tuning failed, using fixed sampling clock\n");
1350 return EIO; /* tuning failed */
1351 }
1352
1353 if (hp->tuning_timer_count) {
1354 callout_schedule(&hp->tuning_timer,
1355 hz * hp->tuning_timer_count);
1356 }
1357
1358 return 0; /* tuning completed */
1359 }
1360
1361 static void
1362 sdhc_tuning_timer(void *arg)
1363 {
1364 struct sdhc_host *hp = arg;
1365
1366 atomic_swap_uint(&hp->tuning_timer_pending, 1);
1367 }
1368
1369 static int
1370 sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value)
1371 {
1372 uint32_t state;
1373 int timeout;
1374
1375 for (timeout = 10000; timeout > 0; timeout--) {
1376 if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value)
1377 return 0;
1378 sdmmc_delay(10);
1379 }
1380 aprint_error_dev(hp->sc->sc_dev, "timeout waiting for mask %#x value %#x (state=%#x)\n",
1381 mask, value, state);
1382 return ETIMEDOUT;
1383 }
1384
1385 static void
1386 sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
1387 {
1388 struct sdhc_host *hp = (struct sdhc_host *)sch;
1389 int error;
1390
1391 mutex_enter(&hp->intr_lock);
1392
1393 if (atomic_cas_uint(&hp->tuning_timer_pending, 1, 0) == 1) {
1394 (void)sdhc_execute_tuning(hp, hp->tuning_timing);
1395 }
1396
1397 if (cmd->c_data && ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1398 const uint16_t ready = SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY;
1399 if (ISSET(hp->flags, SHF_USE_DMA)) {
1400 HCLR2(hp, SDHC_NINTR_SIGNAL_EN, ready);
1401 HCLR2(hp, SDHC_NINTR_STATUS_EN, ready);
1402 } else {
1403 HSET2(hp, SDHC_NINTR_SIGNAL_EN, ready);
1404 HSET2(hp, SDHC_NINTR_STATUS_EN, ready);
1405 }
1406 }
1407
1408 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_TIMEOUT)) {
1409 const uint16_t eintr = SDHC_CMD_TIMEOUT_ERROR;
1410 if (cmd->c_data != NULL) {
1411 HCLR2(hp, SDHC_EINTR_SIGNAL_EN, eintr);
1412 HCLR2(hp, SDHC_EINTR_STATUS_EN, eintr);
1413 } else {
1414 HSET2(hp, SDHC_EINTR_SIGNAL_EN, eintr);
1415 HSET2(hp, SDHC_EINTR_STATUS_EN, eintr);
1416 }
1417 }
1418
1419 /*
1420 * Start the MMC command, or mark `cmd' as failed and return.
1421 */
1422 error = sdhc_start_command(hp, cmd);
1423 if (error) {
1424 cmd->c_error = error;
1425 goto out;
1426 }
1427
1428 /*
1429 * Wait until the command phase is done, or until the command
1430 * is marked done for any other reason.
1431 */
1432 if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT)) {
1433 cmd->c_error = ETIMEDOUT;
1434 goto out;
1435 }
1436
1437 /*
1438 * The host controller removes bits [0:7] from the response
1439 * data (CRC) and we pass the data up unchanged to the bus
1440 * driver (without padding).
1441 */
1442 if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
1443 cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE + 0);
1444 if (ISSET(cmd->c_flags, SCF_RSP_136)) {
1445 cmd->c_resp[1] = HREAD4(hp, SDHC_RESPONSE + 4);
1446 cmd->c_resp[2] = HREAD4(hp, SDHC_RESPONSE + 8);
1447 cmd->c_resp[3] = HREAD4(hp, SDHC_RESPONSE + 12);
1448 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_RSP136_CRC)) {
1449 cmd->c_resp[0] = (cmd->c_resp[0] >> 8) |
1450 (cmd->c_resp[1] << 24);
1451 cmd->c_resp[1] = (cmd->c_resp[1] >> 8) |
1452 (cmd->c_resp[2] << 24);
1453 cmd->c_resp[2] = (cmd->c_resp[2] >> 8) |
1454 (cmd->c_resp[3] << 24);
1455 cmd->c_resp[3] = (cmd->c_resp[3] >> 8);
1456 }
1457 }
1458 }
1459 DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0]));
1460
1461 /*
1462 * If the command has data to transfer in any direction,
1463 * execute the transfer now.
1464 */
1465 if (cmd->c_error == 0 && cmd->c_data != NULL)
1466 sdhc_transfer_data(hp, cmd);
1467 else if (ISSET(cmd->c_flags, SCF_RSP_BSY)) {
1468 if (!sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE, hz * 10)) {
1469 cmd->c_error = ETIMEDOUT;
1470 goto out;
1471 }
1472 }
1473
1474 out:
1475 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)
1476 && !ISSET(hp->sc->sc_flags, SDHC_FLAG_NO_LED_ON)) {
1477 /* Turn off the LED. */
1478 HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
1479 }
1480 SET(cmd->c_flags, SCF_ITSDONE);
1481
1482 mutex_exit(&hp->intr_lock);
1483
1484 DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp),
1485 cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort",
1486 cmd->c_flags, cmd->c_error));
1487 }
1488
1489 static int
1490 sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
1491 {
1492 struct sdhc_softc * const sc = hp->sc;
1493 uint16_t blksize = 0;
1494 uint16_t blkcount = 0;
1495 uint16_t mode;
1496 uint16_t command;
1497 int error;
1498
1499 KASSERT(mutex_owned(&hp->intr_lock));
1500
1501 DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x, status=%#x\n",
1502 HDEVNAME(hp), cmd->c_opcode, cmd->c_arg, cmd->c_data,
1503 cmd->c_datalen, cmd->c_flags, HREAD4(hp, SDHC_NINTR_STATUS)));
1504
1505 /*
1506 * The maximum block length for commands should be the minimum
1507 * of the host buffer size and the card buffer size. (1.7.2)
1508 */
1509
1510 /* Fragment the data into proper blocks. */
1511 if (cmd->c_datalen > 0) {
1512 blksize = MIN(cmd->c_datalen, cmd->c_blklen);
1513 blkcount = cmd->c_datalen / blksize;
1514 if (cmd->c_datalen % blksize > 0) {
1515 /* XXX: Split this command. (1.7.4) */
1516 aprint_error_dev(sc->sc_dev,
1517 "data not a multiple of %u bytes\n", blksize);
1518 return EINVAL;
1519 }
1520 }
1521
1522 /* Check limit imposed by 9-bit block count. (1.7.2) */
1523 if (blkcount > SDHC_BLOCK_COUNT_MAX) {
1524 aprint_error_dev(sc->sc_dev, "too much data\n");
1525 return EINVAL;
1526 }
1527
1528 /* Prepare transfer mode register value. (2.2.5) */
1529 mode = SDHC_BLOCK_COUNT_ENABLE;
1530 if (ISSET(cmd->c_flags, SCF_CMD_READ))
1531 mode |= SDHC_READ_MODE;
1532 if (blkcount > 1) {
1533 mode |= SDHC_MULTI_BLOCK_MODE;
1534 /* XXX only for memory commands? */
1535 mode |= SDHC_AUTO_CMD12_ENABLE;
1536 }
1537 if (cmd->c_dmamap != NULL && cmd->c_datalen > 0 &&
1538 ISSET(hp->flags, SHF_MODE_DMAEN)) {
1539 mode |= SDHC_DMA_ENABLE;
1540 }
1541
1542 /*
1543 * Prepare command register value. (2.2.6)
1544 */
1545 command = (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT;
1546
1547 if (ISSET(cmd->c_flags, SCF_RSP_CRC))
1548 command |= SDHC_CRC_CHECK_ENABLE;
1549 if (ISSET(cmd->c_flags, SCF_RSP_IDX))
1550 command |= SDHC_INDEX_CHECK_ENABLE;
1551 if (cmd->c_datalen > 0)
1552 command |= SDHC_DATA_PRESENT_SELECT;
1553
1554 if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
1555 command |= SDHC_NO_RESPONSE;
1556 else if (ISSET(cmd->c_flags, SCF_RSP_136))
1557 command |= SDHC_RESP_LEN_136;
1558 else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
1559 command |= SDHC_RESP_LEN_48_CHK_BUSY;
1560 else
1561 command |= SDHC_RESP_LEN_48;
1562
1563 /* Wait until command and data inhibit bits are clear. (1.5) */
1564 error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0);
1565 if (error) {
1566 aprint_error_dev(sc->sc_dev, "command or data phase inhibited\n");
1567 return error;
1568 }
1569
1570 DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n",
1571 HDEVNAME(hp), blksize, blkcount, mode, command));
1572
1573 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1574 blksize |= (MAX(0, PAGE_SHIFT - 12) & SDHC_DMA_BOUNDARY_MASK) <<
1575 SDHC_DMA_BOUNDARY_SHIFT; /* PAGE_SIZE DMA boundary */
1576 }
1577
1578 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1579 /* Alert the user not to remove the card. */
1580 HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
1581 }
1582
1583 /* Set DMA start address. */
1584 if (ISSET(hp->flags, SHF_USE_ADMA2_MASK) && cmd->c_data != NULL) {
1585 for (int seg = 0; seg < cmd->c_dmamap->dm_nsegs; seg++) {
1586 bus_addr_t paddr =
1587 cmd->c_dmamap->dm_segs[seg].ds_addr;
1588 uint16_t len =
1589 cmd->c_dmamap->dm_segs[seg].ds_len == 65536 ?
1590 0 : cmd->c_dmamap->dm_segs[seg].ds_len;
1591 uint16_t attr =
1592 SDHC_ADMA2_VALID | SDHC_ADMA2_ACT_TRANS;
1593 if (seg == cmd->c_dmamap->dm_nsegs - 1) {
1594 attr |= SDHC_ADMA2_END;
1595 }
1596 if (ISSET(hp->flags, SHF_USE_ADMA2_32)) {
1597 struct sdhc_adma2_descriptor32 *desc =
1598 hp->adma2;
1599 desc[seg].attribute = htole16(attr);
1600 desc[seg].length = htole16(len);
1601 desc[seg].address = htole32(paddr);
1602 } else {
1603 struct sdhc_adma2_descriptor64 *desc =
1604 hp->adma2;
1605 desc[seg].attribute = htole16(attr);
1606 desc[seg].length = htole16(len);
1607 desc[seg].address = htole32(paddr & 0xffffffff);
1608 desc[seg].address_hi = htole32(
1609 (uint64_t)paddr >> 32);
1610 }
1611 }
1612 if (ISSET(hp->flags, SHF_USE_ADMA2_32)) {
1613 struct sdhc_adma2_descriptor32 *desc = hp->adma2;
1614 desc[cmd->c_dmamap->dm_nsegs].attribute = htole16(0);
1615 } else {
1616 struct sdhc_adma2_descriptor64 *desc = hp->adma2;
1617 desc[cmd->c_dmamap->dm_nsegs].attribute = htole16(0);
1618 }
1619 bus_dmamap_sync(sc->sc_dmat, hp->adma_map, 0, PAGE_SIZE,
1620 BUS_DMASYNC_PREWRITE);
1621 HCLR1(hp, SDHC_HOST_CTL, SDHC_DMA_SELECT);
1622 HSET1(hp, SDHC_HOST_CTL, SDHC_DMA_SELECT_ADMA2);
1623
1624 const bus_addr_t desc_addr = hp->adma_map->dm_segs[0].ds_addr;
1625
1626 HWRITE4(hp, SDHC_ADMA_SYSTEM_ADDR, desc_addr & 0xffffffff);
1627 if (ISSET(hp->flags, SHF_USE_ADMA2_64)) {
1628 HWRITE4(hp, SDHC_ADMA_SYSTEM_ADDR + 4,
1629 (uint64_t)desc_addr >> 32);
1630 }
1631 } else if (ISSET(mode, SDHC_DMA_ENABLE) &&
1632 !ISSET(sc->sc_flags, SDHC_FLAG_EXTERNAL_DMA)) {
1633 HWRITE4(hp, SDHC_DMA_ADDR, cmd->c_dmamap->dm_segs[0].ds_addr);
1634 }
1635
1636 /*
1637 * Start a CPU data transfer. Writing to the high order byte
1638 * of the SDHC_COMMAND register triggers the SD command. (1.5)
1639 */
1640 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
1641 HWRITE4(hp, SDHC_BLOCK_SIZE, blksize | (blkcount << 16));
1642 HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
1643 HWRITE4(hp, SDHC_TRANSFER_MODE, mode | (command << 16));
1644 } else {
1645 HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
1646 HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
1647 HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
1648 HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
1649 HWRITE2(hp, SDHC_COMMAND, command);
1650 }
1651
1652 return 0;
1653 }
1654
1655 static void
1656 sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
1657 {
1658 struct sdhc_softc *sc = hp->sc;
1659 int error;
1660
1661 KASSERT(mutex_owned(&hp->intr_lock));
1662
1663 DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp),
1664 MMC_R1(cmd->c_resp), cmd->c_datalen));
1665
1666 #ifdef SDHC_DEBUG
1667 /* XXX I forgot why I wanted to know when this happens :-( */
1668 if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
1669 ISSET(MMC_R1(cmd->c_resp), 0xcb00)) {
1670 aprint_error_dev(hp->sc->sc_dev,
1671 "CMD52/53 error response flags %#x\n",
1672 MMC_R1(cmd->c_resp) & 0xff00);
1673 }
1674 #endif
1675
1676 if (cmd->c_dmamap != NULL) {
1677 if (hp->sc->sc_vendor_transfer_data_dma != NULL) {
1678 error = hp->sc->sc_vendor_transfer_data_dma(sc, cmd);
1679 if (error == 0 && !sdhc_wait_intr(hp,
1680 SDHC_TRANSFER_COMPLETE, SDHC_DMA_TIMEOUT)) {
1681 error = ETIMEDOUT;
1682 }
1683 } else {
1684 error = sdhc_transfer_data_dma(hp, cmd);
1685 }
1686 } else
1687 error = sdhc_transfer_data_pio(hp, cmd);
1688 if (error)
1689 cmd->c_error = error;
1690 SET(cmd->c_flags, SCF_ITSDONE);
1691
1692 DPRINTF(1,("%s: data transfer done (error=%d)\n",
1693 HDEVNAME(hp), cmd->c_error));
1694 }
1695
1696 static int
1697 sdhc_transfer_data_dma(struct sdhc_host *hp, struct sdmmc_command *cmd)
1698 {
1699 bus_dma_segment_t *dm_segs = cmd->c_dmamap->dm_segs;
1700 bus_addr_t posaddr;
1701 bus_addr_t segaddr;
1702 bus_size_t seglen;
1703 u_int seg = 0;
1704 int error = 0;
1705 int status;
1706
1707 KASSERT(mutex_owned(&hp->intr_lock));
1708 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_DMA_INTERRUPT);
1709 KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_DMA_INTERRUPT);
1710 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
1711 KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
1712
1713 for (;;) {
1714 status = sdhc_wait_intr(hp,
1715 SDHC_DMA_INTERRUPT|SDHC_TRANSFER_COMPLETE,
1716 SDHC_DMA_TIMEOUT);
1717
1718 if (status & SDHC_TRANSFER_COMPLETE) {
1719 break;
1720 }
1721 if (!status) {
1722 error = ETIMEDOUT;
1723 break;
1724 }
1725
1726 if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
1727 continue;
1728 }
1729
1730 if ((status & SDHC_DMA_INTERRUPT) == 0) {
1731 continue;
1732 }
1733
1734 /* DMA Interrupt (boundary crossing) */
1735
1736 segaddr = dm_segs[seg].ds_addr;
1737 seglen = dm_segs[seg].ds_len;
1738 posaddr = HREAD4(hp, SDHC_DMA_ADDR);
1739
1740 if ((seg == (cmd->c_dmamap->dm_nsegs-1)) && (posaddr == (segaddr + seglen))) {
1741 continue;
1742 }
1743 if ((posaddr >= segaddr) && (posaddr < (segaddr + seglen)))
1744 HWRITE4(hp, SDHC_DMA_ADDR, posaddr);
1745 else if ((posaddr >= segaddr) && (posaddr == (segaddr + seglen)) && (seg + 1) < cmd->c_dmamap->dm_nsegs)
1746 HWRITE4(hp, SDHC_DMA_ADDR, dm_segs[++seg].ds_addr);
1747 KASSERT(seg < cmd->c_dmamap->dm_nsegs);
1748 }
1749
1750 if (ISSET(hp->flags, SHF_USE_ADMA2_MASK)) {
1751 bus_dmamap_sync(hp->sc->sc_dmat, hp->adma_map, 0,
1752 PAGE_SIZE, BUS_DMASYNC_POSTWRITE);
1753 }
1754
1755 return error;
1756 }
1757
1758 static int
1759 sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd)
1760 {
1761 uint8_t *data = cmd->c_data;
1762 void (*pio_func)(struct sdhc_host *, uint8_t *, u_int);
1763 u_int len, datalen;
1764 u_int imask;
1765 u_int pmask;
1766 int error = 0;
1767
1768 KASSERT(mutex_owned(&hp->intr_lock));
1769
1770 if (ISSET(cmd->c_flags, SCF_CMD_READ)) {
1771 imask = SDHC_BUFFER_READ_READY;
1772 pmask = SDHC_BUFFER_READ_ENABLE;
1773 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1774 pio_func = esdhc_read_data_pio;
1775 } else {
1776 pio_func = sdhc_read_data_pio;
1777 }
1778 } else {
1779 imask = SDHC_BUFFER_WRITE_READY;
1780 pmask = SDHC_BUFFER_WRITE_ENABLE;
1781 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
1782 pio_func = esdhc_write_data_pio;
1783 } else {
1784 pio_func = sdhc_write_data_pio;
1785 }
1786 }
1787 datalen = cmd->c_datalen;
1788
1789 KASSERT(mutex_owned(&hp->intr_lock));
1790 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & imask);
1791 KASSERT(HREAD2(hp, SDHC_NINTR_STATUS_EN) & SDHC_TRANSFER_COMPLETE);
1792 KASSERT(HREAD2(hp, SDHC_NINTR_SIGNAL_EN) & SDHC_TRANSFER_COMPLETE);
1793
1794 while (datalen > 0) {
1795 if (!ISSET(HREAD4(hp, SDHC_PRESENT_STATE), imask)) {
1796 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
1797 HSET4(hp, SDHC_NINTR_SIGNAL_EN, imask);
1798 } else {
1799 HSET2(hp, SDHC_NINTR_SIGNAL_EN, imask);
1800 }
1801 if (!sdhc_wait_intr(hp, imask, SDHC_BUFFER_TIMEOUT)) {
1802 error = ETIMEDOUT;
1803 break;
1804 }
1805
1806 error = sdhc_wait_state(hp, pmask, pmask);
1807 if (error)
1808 break;
1809 }
1810
1811 len = MIN(datalen, cmd->c_blklen);
1812 (*pio_func)(hp, data, len);
1813 DPRINTF(2,("%s: pio data transfer %u @ %p\n",
1814 HDEVNAME(hp), len, data));
1815
1816 data += len;
1817 datalen -= len;
1818 }
1819
1820 if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
1821 SDHC_TRANSFER_TIMEOUT))
1822 error = ETIMEDOUT;
1823
1824 return error;
1825 }
1826
1827 static void
1828 sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
1829 {
1830
1831 if (((__uintptr_t)data & 3) == 0) {
1832 while (datalen > 3) {
1833 *(uint32_t *)data = le32toh(HREAD4(hp, SDHC_DATA));
1834 data += 4;
1835 datalen -= 4;
1836 }
1837 if (datalen > 1) {
1838 *(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
1839 data += 2;
1840 datalen -= 2;
1841 }
1842 if (datalen > 0) {
1843 *data = HREAD1(hp, SDHC_DATA);
1844 data += 1;
1845 datalen -= 1;
1846 }
1847 } else if (((__uintptr_t)data & 1) == 0) {
1848 while (datalen > 1) {
1849 *(uint16_t *)data = le16toh(HREAD2(hp, SDHC_DATA));
1850 data += 2;
1851 datalen -= 2;
1852 }
1853 if (datalen > 0) {
1854 *data = HREAD1(hp, SDHC_DATA);
1855 data += 1;
1856 datalen -= 1;
1857 }
1858 } else {
1859 while (datalen > 0) {
1860 *data = HREAD1(hp, SDHC_DATA);
1861 data += 1;
1862 datalen -= 1;
1863 }
1864 }
1865 }
1866
1867 static void
1868 sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
1869 {
1870
1871 if (((__uintptr_t)data & 3) == 0) {
1872 while (datalen > 3) {
1873 HWRITE4(hp, SDHC_DATA, htole32(*(uint32_t *)data));
1874 data += 4;
1875 datalen -= 4;
1876 }
1877 if (datalen > 1) {
1878 HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
1879 data += 2;
1880 datalen -= 2;
1881 }
1882 if (datalen > 0) {
1883 HWRITE1(hp, SDHC_DATA, *data);
1884 data += 1;
1885 datalen -= 1;
1886 }
1887 } else if (((__uintptr_t)data & 1) == 0) {
1888 while (datalen > 1) {
1889 HWRITE2(hp, SDHC_DATA, htole16(*(uint16_t *)data));
1890 data += 2;
1891 datalen -= 2;
1892 }
1893 if (datalen > 0) {
1894 HWRITE1(hp, SDHC_DATA, *data);
1895 data += 1;
1896 datalen -= 1;
1897 }
1898 } else {
1899 while (datalen > 0) {
1900 HWRITE1(hp, SDHC_DATA, *data);
1901 data += 1;
1902 datalen -= 1;
1903 }
1904 }
1905 }
1906
1907 static void
1908 esdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
1909 {
1910 uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
1911 uint32_t v;
1912
1913 const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_READ_SHIFT) & SDHC_WATERMARK_READ_MASK;
1914 size_t count = 0;
1915
1916 while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
1917 if (count == 0) {
1918 /*
1919 * If we've drained "watermark" words, we need to wait
1920 * a little bit so the read FIFO can refill.
1921 */
1922 sdmmc_delay(10);
1923 count = watermark;
1924 }
1925 v = HREAD4(hp, SDHC_DATA);
1926 v = le32toh(v);
1927 *(uint32_t *)data = v;
1928 data += 4;
1929 datalen -= 4;
1930 status = HREAD2(hp, SDHC_NINTR_STATUS);
1931 count--;
1932 }
1933 if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
1934 if (count == 0) {
1935 sdmmc_delay(10);
1936 }
1937 v = HREAD4(hp, SDHC_DATA);
1938 v = le32toh(v);
1939 do {
1940 *data++ = v;
1941 v >>= 8;
1942 } while (--datalen > 0);
1943 }
1944 }
1945
1946 static void
1947 esdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, u_int datalen)
1948 {
1949 uint16_t status = HREAD2(hp, SDHC_NINTR_STATUS);
1950 uint32_t v;
1951
1952 const size_t watermark = (HREAD4(hp, SDHC_WATERMARK_LEVEL) >> SDHC_WATERMARK_WRITE_SHIFT) & SDHC_WATERMARK_WRITE_MASK;
1953 size_t count = watermark;
1954
1955 while (datalen > 3 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
1956 if (count == 0) {
1957 sdmmc_delay(10);
1958 count = watermark;
1959 }
1960 v = *(uint32_t *)data;
1961 v = htole32(v);
1962 HWRITE4(hp, SDHC_DATA, v);
1963 data += 4;
1964 datalen -= 4;
1965 status = HREAD2(hp, SDHC_NINTR_STATUS);
1966 count--;
1967 }
1968 if (datalen > 0 && !ISSET(status, SDHC_TRANSFER_COMPLETE)) {
1969 if (count == 0) {
1970 sdmmc_delay(10);
1971 }
1972 v = *(uint32_t *)data;
1973 v = htole32(v);
1974 HWRITE4(hp, SDHC_DATA, v);
1975 }
1976 }
1977
1978 /* Prepare for another command. */
1979 static int
1980 sdhc_soft_reset(struct sdhc_host *hp, int mask)
1981 {
1982 int timo;
1983
1984 KASSERT(mutex_owned(&hp->intr_lock));
1985
1986 DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask));
1987
1988 /* Request the reset. */
1989 HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
1990
1991 /*
1992 * If necessary, wait for the controller to set the bits to
1993 * acknowledge the reset.
1994 */
1995 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_WAIT_RESET) &&
1996 ISSET(mask, (SDHC_RESET_DAT | SDHC_RESET_CMD))) {
1997 for (timo = 10000; timo > 0; timo--) {
1998 if (ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
1999 break;
2000 /* Short delay because I worry we may miss it... */
2001 sdmmc_delay(1);
2002 }
2003 if (timo == 0)
2004 return ETIMEDOUT;
2005 }
2006
2007 /*
2008 * Wait for the controller to clear the bits to indicate that
2009 * the reset has completed.
2010 */
2011 for (timo = 10; timo > 0; timo--) {
2012 if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
2013 break;
2014 sdmmc_delay(10000);
2015 }
2016 if (timo == 0) {
2017 DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp),
2018 HREAD1(hp, SDHC_SOFTWARE_RESET)));
2019 return ETIMEDOUT;
2020 }
2021
2022 if (ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
2023 HSET4(hp, SDHC_DMA_CTL, SDHC_DMA_SNOOP);
2024 }
2025
2026 return 0;
2027 }
2028
2029 static int
2030 sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo)
2031 {
2032 int status;
2033
2034 KASSERT(mutex_owned(&hp->intr_lock));
2035
2036 mask |= SDHC_ERROR_INTERRUPT;
2037
2038 status = hp->intr_status & mask;
2039 while (status == 0) {
2040 if (cv_timedwait(&hp->intr_cv, &hp->intr_lock, timo)
2041 == EWOULDBLOCK) {
2042 status |= SDHC_ERROR_INTERRUPT;
2043 break;
2044 }
2045 status = hp->intr_status & mask;
2046 }
2047 hp->intr_status &= ~status;
2048
2049 DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status,
2050 hp->intr_error_status));
2051
2052 /* Command timeout has higher priority than command complete. */
2053 if (ISSET(status, SDHC_ERROR_INTERRUPT) || hp->intr_error_status) {
2054 hp->intr_error_status = 0;
2055 hp->intr_status &= ~SDHC_ERROR_INTERRUPT;
2056 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED)) {
2057 (void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
2058 }
2059 status = 0;
2060 }
2061
2062 return status;
2063 }
2064
2065 /*
2066 * Established by attachment driver at interrupt priority IPL_SDMMC.
2067 */
2068 int
2069 sdhc_intr(void *arg)
2070 {
2071 struct sdhc_softc *sc = (struct sdhc_softc *)arg;
2072 struct sdhc_host *hp;
2073 int done = 0;
2074 uint16_t status;
2075 uint16_t error;
2076
2077 /* We got an interrupt, but we don't know from which slot. */
2078 for (size_t host = 0; host < sc->sc_nhosts; host++) {
2079 hp = sc->sc_host[host];
2080 if (hp == NULL)
2081 continue;
2082
2083 mutex_enter(&hp->intr_lock);
2084
2085 if (ISSET(sc->sc_flags, SDHC_FLAG_32BIT_ACCESS)) {
2086 /* Find out which interrupts are pending. */
2087 uint32_t xstatus = HREAD4(hp, SDHC_NINTR_STATUS);
2088 status = xstatus;
2089 error = xstatus >> 16;
2090 if (error)
2091 xstatus |= SDHC_ERROR_INTERRUPT;
2092 else if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
2093 goto next_port; /* no interrupt for us */
2094 /* Acknowledge the interrupts we are about to handle. */
2095 HWRITE4(hp, SDHC_NINTR_STATUS, xstatus);
2096 } else {
2097 /* Find out which interrupts are pending. */
2098 error = 0;
2099 status = HREAD2(hp, SDHC_NINTR_STATUS);
2100 if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
2101 goto next_port; /* no interrupt for us */
2102 /* Acknowledge the interrupts we are about to handle. */
2103 HWRITE2(hp, SDHC_NINTR_STATUS, status);
2104 if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
2105 /* Acknowledge error interrupts. */
2106 error = HREAD2(hp, SDHC_EINTR_STATUS);
2107 HWRITE2(hp, SDHC_EINTR_STATUS, error);
2108 }
2109 }
2110
2111 DPRINTF(2,("%s: interrupt status=%x error=%x\n", HDEVNAME(hp),
2112 status, error));
2113
2114 /* Claim this interrupt. */
2115 done = 1;
2116
2117 if (ISSET(error, SDHC_ADMA_ERROR)) {
2118 uint8_t adma_err = HREAD1(hp, SDHC_ADMA_ERROR_STATUS);
2119 printf("%s: ADMA error, status %02x\n", HDEVNAME(hp),
2120 adma_err);
2121 }
2122
2123 /*
2124 * Service error interrupts.
2125 */
2126 if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR|
2127 SDHC_DATA_TIMEOUT_ERROR)) {
2128 hp->intr_error_status |= error;
2129 hp->intr_status |= status;
2130 cv_broadcast(&hp->intr_cv);
2131 }
2132
2133 /*
2134 * Wake up the sdmmc event thread to scan for cards.
2135 */
2136 if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)) {
2137 if (hp->sdmmc != NULL) {
2138 sdmmc_needs_discover(hp->sdmmc);
2139 }
2140 if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
2141 HCLR4(hp, SDHC_NINTR_STATUS_EN,
2142 status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
2143 HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
2144 status & (SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION));
2145 }
2146 }
2147
2148 /*
2149 * Schedule re-tuning process (UHS).
2150 */
2151 if (ISSET(status, SDHC_RETUNING_EVENT)) {
2152 atomic_swap_uint(&hp->tuning_timer_pending, 1);
2153 }
2154
2155 /*
2156 * Wake up the blocking process to service command
2157 * related interrupt(s).
2158 */
2159 if (ISSET(status, SDHC_COMMAND_COMPLETE|
2160 SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY|
2161 SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) {
2162 hp->intr_status |= status;
2163 if (ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)) {
2164 HCLR4(hp, SDHC_NINTR_SIGNAL_EN,
2165 status & (SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY));
2166 }
2167 cv_broadcast(&hp->intr_cv);
2168 }
2169
2170 /*
2171 * Service SD card interrupts.
2172 */
2173 if (!ISSET(sc->sc_flags, SDHC_FLAG_ENHANCED)
2174 && ISSET(status, SDHC_CARD_INTERRUPT)) {
2175 DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp)));
2176 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
2177 sdmmc_card_intr(hp->sdmmc);
2178 }
2179 next_port:
2180 mutex_exit(&hp->intr_lock);
2181 }
2182
2183 return done;
2184 }
2185
2186 kmutex_t *
2187 sdhc_host_lock(struct sdhc_host *hp)
2188 {
2189 return &hp->intr_lock;
2190 }
2191
2192 #ifdef SDHC_DEBUG
2193 void
2194 sdhc_dump_regs(struct sdhc_host *hp)
2195 {
2196
2197 printf("0x%02x PRESENT_STATE: %x\n", SDHC_PRESENT_STATE,
2198 HREAD4(hp, SDHC_PRESENT_STATE));
2199 if (!ISSET(hp->sc->sc_flags, SDHC_FLAG_ENHANCED))
2200 printf("0x%02x POWER_CTL: %x\n", SDHC_POWER_CTL,
2201 HREAD1(hp, SDHC_POWER_CTL));
2202 printf("0x%02x NINTR_STATUS: %x\n", SDHC_NINTR_STATUS,
2203 HREAD2(hp, SDHC_NINTR_STATUS));
2204 printf("0x%02x EINTR_STATUS: %x\n", SDHC_EINTR_STATUS,
2205 HREAD2(hp, SDHC_EINTR_STATUS));
2206 printf("0x%02x NINTR_STATUS_EN: %x\n", SDHC_NINTR_STATUS_EN,
2207 HREAD2(hp, SDHC_NINTR_STATUS_EN));
2208 printf("0x%02x EINTR_STATUS_EN: %x\n", SDHC_EINTR_STATUS_EN,
2209 HREAD2(hp, SDHC_EINTR_STATUS_EN));
2210 printf("0x%02x NINTR_SIGNAL_EN: %x\n", SDHC_NINTR_SIGNAL_EN,
2211 HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
2212 printf("0x%02x EINTR_SIGNAL_EN: %x\n", SDHC_EINTR_SIGNAL_EN,
2213 HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
2214 printf("0x%02x CAPABILITIES: %x\n", SDHC_CAPABILITIES,
2215 HREAD4(hp, SDHC_CAPABILITIES));
2216 printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
2217 HREAD4(hp, SDHC_MAX_CAPABILITIES));
2218 }
2219 #endif
2220