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