1 /* $OpenBSD: sdhc.c,v 1.25 2009/01/13 19:44:20 grange Exp $ */
2
3 /*
4 * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /*
20 * SD Host Controller driver based on the SD Host Controller Standard
21 * Simplified Specification Version 1.00 (www.sdcard.com).
22 */
23
24 #include <sys/param.h>
25 #include <sys/device.h>
26 #include <sys/kernel.h>
27 #include <sys/kthread.h>
28 #include <sys/malloc.h>
29 #include <sys/systm.h>
30
31 #include <dev/sdmmc/sdhcreg.h>
32 #include <dev/sdmmc/sdhcvar.h>
33 #include <dev/sdmmc/sdmmcchip.h>
34 #include <dev/sdmmc/sdmmcreg.h>
35 #include <dev/sdmmc/sdmmcvar.h>
36
37 #define SDHC_COMMAND_TIMEOUT hz
38 #define SDHC_BUFFER_TIMEOUT hz
39 #define SDHC_TRANSFER_TIMEOUT hz
40
41 struct sdhc_host {
42 struct sdhc_softc *sc; /* host controller device */
43 struct device *sdmmc; /* generic SD/MMC device */
44 bus_space_tag_t iot; /* host register set tag */
45 bus_space_handle_t ioh; /* host register set handle */
46 u_int clkbase; /* base clock frequency in KHz */
47 int maxblklen; /* maximum block length */
48 int flags; /* flags for this host */
49 u_int32_t ocr; /* OCR value from capabilities */
50 u_int8_t regs[14]; /* host controller state */
51 u_int16_t intr_status; /* soft interrupt status */
52 u_int16_t intr_error_status; /* soft error status */
53 };
54
55 #define HDEVNAME(hp) ((hp)->sc->sc_dev.dv_xname)
56
57 /* flag values */
58 #define SHF_USE_DMA 0x0001
59
60 #define HREAD1(hp, reg) \
61 (bus_space_read_1((hp)->iot, (hp)->ioh, (reg)))
62 #define HREAD2(hp, reg) \
63 (bus_space_read_2((hp)->iot, (hp)->ioh, (reg)))
64 #define HREAD4(hp, reg) \
65 (bus_space_read_4((hp)->iot, (hp)->ioh, (reg)))
66 #define HWRITE1(hp, reg, val) \
67 bus_space_write_1((hp)->iot, (hp)->ioh, (reg), (val))
68 #define HWRITE2(hp, reg, val) \
69 bus_space_write_2((hp)->iot, (hp)->ioh, (reg), (val))
70 #define HWRITE4(hp, reg, val) \
71 bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val))
72 #define HCLR1(hp, reg, bits) \
73 HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits))
74 #define HCLR2(hp, reg, bits) \
75 HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits))
76 #define HSET1(hp, reg, bits) \
77 HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits))
78 #define HSET2(hp, reg, bits) \
79 HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits))
80
81 int sdhc_host_reset(sdmmc_chipset_handle_t);
82 u_int32_t sdhc_host_ocr(sdmmc_chipset_handle_t);
83 int sdhc_host_maxblklen(sdmmc_chipset_handle_t);
84 int sdhc_card_detect(sdmmc_chipset_handle_t);
85 int sdhc_bus_power(sdmmc_chipset_handle_t, u_int32_t);
86 int sdhc_bus_clock(sdmmc_chipset_handle_t, int);
87 void sdhc_card_intr_mask(sdmmc_chipset_handle_t, int);
88 void sdhc_card_intr_ack(sdmmc_chipset_handle_t);
89 void sdhc_exec_command(sdmmc_chipset_handle_t, struct sdmmc_command *);
90 int sdhc_start_command(struct sdhc_host *, struct sdmmc_command *);
91 int sdhc_wait_state(struct sdhc_host *, u_int32_t, u_int32_t);
92 int sdhc_soft_reset(struct sdhc_host *, int);
93 int sdhc_wait_intr(struct sdhc_host *, int, int);
94 void sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *);
95 void sdhc_read_data(struct sdhc_host *, u_char *, int);
96 void sdhc_write_data(struct sdhc_host *, u_char *, int);
97
98 #ifdef SDHC_DEBUG
99 int sdhcdebug = 0;
100 #define DPRINTF(n,s) do { if ((n) <= sdhcdebug) printf s; } while (0)
101 void sdhc_dump_regs(struct sdhc_host *);
102 #else
103 #define DPRINTF(n,s) do {} while(0)
104 #endif
105
106 struct sdmmc_chip_functions sdhc_functions = {
107 /* host controller reset */
108 sdhc_host_reset,
109 /* host controller capabilities */
110 sdhc_host_ocr,
111 sdhc_host_maxblklen,
112 /* card detection */
113 sdhc_card_detect,
114 /* bus power and clock frequency */
115 sdhc_bus_power,
116 sdhc_bus_clock,
117 /* command execution */
118 sdhc_exec_command,
119 /* card interrupt */
120 sdhc_card_intr_mask,
121 sdhc_card_intr_ack
122 };
123
124 struct cfdriver sdhc_cd = {
125 NULL, "sdhc", DV_DULL
126 };
127
128 /*
129 * Called by attachment driver. For each SD card slot there is one SD
130 * host controller standard register set. (1.3)
131 */
132 int
sdhc_host_found(struct sdhc_softc * sc,bus_space_tag_t iot,bus_space_handle_t ioh,bus_size_t iosize,int usedma)133 sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot,
134 bus_space_handle_t ioh, bus_size_t iosize, int usedma)
135 {
136 struct sdmmcbus_attach_args saa;
137 struct sdhc_host *hp;
138 u_int32_t caps;
139 int error = 1;
140 #ifdef SDHC_DEBUG
141 u_int16_t version;
142
143 version = bus_space_read_2(iot, ioh, SDHC_HOST_CTL_VERSION);
144 printf("%s: SD Host Specification/Vendor Version ",
145 sc->sc_dev.dv_xname);
146 switch(SDHC_SPEC_VERSION(version)) {
147 case 0x00:
148 printf("1.0/%u\n", SDHC_VENDOR_VERSION(version));
149 break;
150 default:
151 printf(">1.0/%u\n", SDHC_VENDOR_VERSION(version));
152 break;
153 }
154 #endif
155
156 /* Allocate one more host structure. */
157 sc->sc_nhosts++;
158 hp = malloc(sizeof(*hp), M_DEVBUF, M_WAITOK | M_ZERO);
159 sc->sc_host[sc->sc_nhosts - 1] = hp;
160
161 /* Fill in the new host structure. */
162 hp->sc = sc;
163 hp->iot = iot;
164 hp->ioh = ioh;
165
166 /*
167 * Reset the host controller and enable interrupts.
168 */
169 (void)sdhc_host_reset(hp);
170
171 /* Determine host capabilities. */
172 caps = HREAD4(hp, SDHC_CAPABILITIES);
173
174 /* Use DMA if the host system and the controller support it. */
175 if (usedma && ISSET(caps, SDHC_DMA_SUPPORT))
176 SET(hp->flags, SHF_USE_DMA);
177
178 /*
179 * Determine the base clock frequency. (2.2.24)
180 */
181 if (SDHC_BASE_FREQ_KHZ(caps) != 0)
182 hp->clkbase = SDHC_BASE_FREQ_KHZ(caps);
183 if (hp->clkbase == 0) {
184 /* The attachment driver must tell us. */
185 printf("%s: base clock frequency unknown\n",
186 sc->sc_dev.dv_xname);
187 goto err;
188 } else if (hp->clkbase < 10000 || hp->clkbase > 63000) {
189 /* SDHC 1.0 supports only 10-63 MHz. */
190 printf("%s: base clock frequency out of range: %u MHz\n",
191 sc->sc_dev.dv_xname, hp->clkbase / 1000);
192 goto err;
193 }
194
195 /*
196 * XXX Set the data timeout counter value according to
197 * capabilities. (2.2.15)
198 */
199
200 /*
201 * Determine SD bus voltage levels supported by the controller.
202 */
203 if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V))
204 SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V);
205 if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V))
206 SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V);
207 if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V))
208 SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V);
209
210 /*
211 * Determine the maximum block length supported by the host
212 * controller. (2.2.24)
213 */
214 switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) {
215 case SDHC_MAX_BLK_LEN_512:
216 hp->maxblklen = 512;
217 break;
218 case SDHC_MAX_BLK_LEN_1024:
219 hp->maxblklen = 1024;
220 break;
221 case SDHC_MAX_BLK_LEN_2048:
222 hp->maxblklen = 2048;
223 break;
224 default:
225 hp->maxblklen = 1;
226 break;
227 }
228
229 /*
230 * Attach the generic SD/MMC bus driver. (The bus driver must
231 * not invoke any chipset functions before it is attached.)
232 */
233 bzero(&saa, sizeof(saa));
234 saa.saa_busname = "sdmmc";
235 saa.sct = &sdhc_functions;
236 saa.sch = hp;
237
238 hp->sdmmc = config_found(&sc->sc_dev, &saa, NULL);
239 if (hp->sdmmc == NULL) {
240 error = 0;
241 goto err;
242 }
243
244 return 0;
245
246 err:
247 free(hp, M_DEVBUF);
248 sc->sc_host[sc->sc_nhosts - 1] = NULL;
249 sc->sc_nhosts--;
250 return (error);
251 }
252
253 /*
254 * Power hook established by or called from attachment driver.
255 */
256 void
sdhc_power(int why,void * arg)257 sdhc_power(int why, void *arg)
258 {
259 struct sdhc_softc *sc = arg;
260 struct sdhc_host *hp;
261 int n, i;
262
263 switch(why) {
264 case PWR_STANDBY:
265 case PWR_SUSPEND:
266 /* XXX poll for command completion or suspend command
267 * in progress */
268
269 /* Save the host controller state. */
270 for (n = 0; n < sc->sc_nhosts; n++) {
271 hp = sc->sc_host[n];
272 for (i = 0; i < sizeof hp->regs; i++)
273 hp->regs[i] = HREAD1(hp, i);
274 }
275 break;
276
277 case PWR_RESUME:
278 /* Restore the host controller state. */
279 for (n = 0; n < sc->sc_nhosts; n++) {
280 hp = sc->sc_host[n];
281 (void)sdhc_host_reset(hp);
282 for (i = 0; i < sizeof hp->regs; i++)
283 HWRITE1(hp, i, hp->regs[i]);
284 }
285 break;
286 }
287 }
288
289 /*
290 * Shutdown hook established by or called from attachment driver.
291 */
292 void
sdhc_shutdown(void * arg)293 sdhc_shutdown(void *arg)
294 {
295 struct sdhc_softc *sc = arg;
296 struct sdhc_host *hp;
297 int i;
298
299 /* XXX chip locks up if we don't disable it before reboot. */
300 for (i = 0; i < sc->sc_nhosts; i++) {
301 hp = sc->sc_host[i];
302 (void)sdhc_host_reset(hp);
303 }
304 }
305
306 /*
307 * Reset the host controller. Called during initialization, when
308 * cards are removed, upon resume, and during error recovery.
309 */
310 int
sdhc_host_reset(sdmmc_chipset_handle_t sch)311 sdhc_host_reset(sdmmc_chipset_handle_t sch)
312 {
313 struct sdhc_host *hp = sch;
314 u_int16_t imask;
315 int error;
316 int s;
317
318 s = splsdmmc();
319
320 /* Disable all interrupts. */
321 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0);
322
323 /*
324 * Reset the entire host controller and wait up to 100ms for
325 * the controller to clear the reset bit.
326 */
327 if ((error = sdhc_soft_reset(hp, SDHC_RESET_ALL)) != 0) {
328 splx(s);
329 return (error);
330 }
331
332 /* Set data timeout counter value to max for now. */
333 HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX);
334
335 /* Enable interrupts. */
336 imask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION |
337 SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY |
338 SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT |
339 SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE;
340
341 HWRITE2(hp, SDHC_NINTR_STATUS_EN, imask);
342 HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK);
343 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, imask);
344 HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK);
345
346 splx(s);
347 return 0;
348 }
349
350 u_int32_t
sdhc_host_ocr(sdmmc_chipset_handle_t sch)351 sdhc_host_ocr(sdmmc_chipset_handle_t sch)
352 {
353 struct sdhc_host *hp = sch;
354 return hp->ocr;
355 }
356
357 int
sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)358 sdhc_host_maxblklen(sdmmc_chipset_handle_t sch)
359 {
360 struct sdhc_host *hp = sch;
361 return hp->maxblklen;
362 }
363
364 /*
365 * Return non-zero if the card is currently inserted.
366 */
367 int
sdhc_card_detect(sdmmc_chipset_handle_t sch)368 sdhc_card_detect(sdmmc_chipset_handle_t sch)
369 {
370 struct sdhc_host *hp = sch;
371 return ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED) ?
372 1 : 0;
373 }
374
375 /*
376 * Set or change SD bus voltage and enable or disable SD bus power.
377 * Return zero on success.
378 */
379 int
sdhc_bus_power(sdmmc_chipset_handle_t sch,u_int32_t ocr)380 sdhc_bus_power(sdmmc_chipset_handle_t sch, u_int32_t ocr)
381 {
382 struct sdhc_host *hp = sch;
383 u_int8_t vdd;
384 int s;
385
386 s = splsdmmc();
387
388 /*
389 * Disable bus power before voltage change.
390 */
391 if (!(hp->sc->sc_flags & SDHC_F_NOPWR0))
392 HWRITE1(hp, SDHC_POWER_CTL, 0);
393
394 /* If power is disabled, reset the host and return now. */
395 if (ocr == 0) {
396 splx(s);
397 (void)sdhc_host_reset(hp);
398 return 0;
399 }
400
401 /*
402 * Select the maximum voltage according to capabilities.
403 */
404 ocr &= hp->ocr;
405 if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V))
406 vdd = SDHC_VOLTAGE_3_3V;
407 else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V))
408 vdd = SDHC_VOLTAGE_3_0V;
409 else if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V))
410 vdd = SDHC_VOLTAGE_1_8V;
411 else {
412 /* Unsupported voltage level requested. */
413 splx(s);
414 return EINVAL;
415 }
416
417 /*
418 * Enable bus power. Wait at least 1 ms (or 74 clocks) plus
419 * voltage ramp until power rises.
420 */
421 HWRITE1(hp, SDHC_POWER_CTL, (vdd << SDHC_VOLTAGE_SHIFT) |
422 SDHC_BUS_POWER);
423 sdmmc_delay(10000);
424
425 /*
426 * The host system may not power the bus due to battery low,
427 * etc. In that case, the host controller should clear the
428 * bus power bit.
429 */
430 if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) {
431 splx(s);
432 return ENXIO;
433 }
434
435 splx(s);
436 return 0;
437 }
438
439 /*
440 * Return the smallest possible base clock frequency divisor value
441 * for the CLOCK_CTL register to produce `freq' (KHz).
442 */
443 static int
sdhc_clock_divisor(struct sdhc_host * hp,u_int freq)444 sdhc_clock_divisor(struct sdhc_host *hp, u_int freq)
445 {
446 int div;
447
448 for (div = 1; div <= 256; div *= 2)
449 if ((hp->clkbase / div) <= freq)
450 return (div / 2);
451 /* No divisor found. */
452 return -1;
453 }
454
455 /*
456 * Set or change SDCLK frequency or disable the SD clock.
457 * Return zero on success.
458 */
459 int
sdhc_bus_clock(sdmmc_chipset_handle_t sch,int freq)460 sdhc_bus_clock(sdmmc_chipset_handle_t sch, int freq)
461 {
462 struct sdhc_host *hp = sch;
463 int s;
464 int div;
465 int timo;
466 int error = 0;
467
468 s = splsdmmc();
469
470 #ifdef DIAGNOSTIC
471 /* Must not stop the clock if commands are in progress. */
472 if (ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK) &&
473 sdhc_card_detect(hp))
474 printf("sdhc_sdclk_frequency_select: command in progress\n");
475 #endif
476
477 /*
478 * Stop SD clock before changing the frequency.
479 */
480 HWRITE2(hp, SDHC_CLOCK_CTL, 0);
481 if (freq == SDMMC_SDCLK_OFF)
482 goto ret;
483
484 /*
485 * Set the minimum base clock frequency divisor.
486 */
487 if ((div = sdhc_clock_divisor(hp, freq)) < 0) {
488 /* Invalid base clock frequency or `freq' value. */
489 error = EINVAL;
490 goto ret;
491 }
492 HWRITE2(hp, SDHC_CLOCK_CTL, div << SDHC_SDCLK_DIV_SHIFT);
493
494 /*
495 * Start internal clock. Wait 10ms for stabilization.
496 */
497 HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE);
498 for (timo = 1000; timo > 0; timo--) {
499 if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL), SDHC_INTCLK_STABLE))
500 break;
501 sdmmc_delay(10);
502 }
503 if (timo == 0) {
504 error = ETIMEDOUT;
505 goto ret;
506 }
507
508 /*
509 * Enable SD clock.
510 */
511 HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE);
512
513 ret:
514 splx(s);
515 return error;
516 }
517
518 void
sdhc_card_intr_mask(sdmmc_chipset_handle_t sch,int enable)519 sdhc_card_intr_mask(sdmmc_chipset_handle_t sch, int enable)
520 {
521 struct sdhc_host *hp = sch;
522
523 if (enable) {
524 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
525 HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
526 } else {
527 HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT);
528 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
529 }
530 }
531
532 void
sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)533 sdhc_card_intr_ack(sdmmc_chipset_handle_t sch)
534 {
535 struct sdhc_host *hp = sch;
536
537 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
538 }
539
540 int
sdhc_wait_state(struct sdhc_host * hp,u_int32_t mask,u_int32_t value)541 sdhc_wait_state(struct sdhc_host *hp, u_int32_t mask, u_int32_t value)
542 {
543 u_int32_t state;
544 int timeout;
545
546 for (timeout = 10; timeout > 0; timeout--) {
547 if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask)
548 == value)
549 return 0;
550 sdmmc_delay(10000);
551 }
552 DPRINTF(0,("%s: timeout waiting for %x (state=%b)\n", HDEVNAME(hp),
553 value, state, SDHC_PRESENT_STATE_BITS));
554 return ETIMEDOUT;
555 }
556
557 void
sdhc_exec_command(sdmmc_chipset_handle_t sch,struct sdmmc_command * cmd)558 sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd)
559 {
560 struct sdhc_host *hp = sch;
561 int error;
562
563 /*
564 * Start the MMC command, or mark `cmd' as failed and return.
565 */
566 error = sdhc_start_command(hp, cmd);
567 if (error != 0) {
568 cmd->c_error = error;
569 SET(cmd->c_flags, SCF_ITSDONE);
570 return;
571 }
572
573 /*
574 * Wait until the command phase is done, or until the command
575 * is marked done for any other reason.
576 */
577 if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE,
578 SDHC_COMMAND_TIMEOUT)) {
579 cmd->c_error = ETIMEDOUT;
580 SET(cmd->c_flags, SCF_ITSDONE);
581 return;
582 }
583
584 /*
585 * The host controller removes bits [0:7] from the response
586 * data (CRC) and we pass the data up unchanged to the bus
587 * driver (without padding).
588 */
589 if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) {
590 if (ISSET(cmd->c_flags, SCF_RSP_136)) {
591 u_char *p = (u_char *)cmd->c_resp;
592 int i;
593
594 for (i = 0; i < 15; i++)
595 *p++ = HREAD1(hp, SDHC_RESPONSE + i);
596 } else
597 cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE);
598 }
599
600 /*
601 * If the command has data to transfer in any direction,
602 * execute the transfer now.
603 */
604 if (cmd->c_error == 0 && cmd->c_data != NULL)
605 sdhc_transfer_data(hp, cmd);
606
607 /* Turn off the LED. */
608 HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
609
610 DPRINTF(1,("%s: cmd %u done (flags=%#x error=%d)\n",
611 HDEVNAME(hp), cmd->c_opcode, cmd->c_flags, cmd->c_error));
612 SET(cmd->c_flags, SCF_ITSDONE);
613 }
614
615 int
sdhc_start_command(struct sdhc_host * hp,struct sdmmc_command * cmd)616 sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd)
617 {
618 u_int16_t blksize = 0;
619 u_int16_t blkcount = 0;
620 u_int16_t mode;
621 u_int16_t command;
622 int error;
623 int s;
624
625 DPRINTF(1,("%s: start cmd %u arg=%#x data=%#x dlen=%d flags=%#x "
626 "proc=\"%s\"\n", HDEVNAME(hp), cmd->c_opcode, cmd->c_arg,
627 cmd->c_data, cmd->c_datalen, cmd->c_flags, curproc ?
628 curproc->p_comm : ""));
629
630 /*
631 * The maximum block length for commands should be the minimum
632 * of the host buffer size and the card buffer size. (1.7.2)
633 */
634
635 /* Fragment the data into proper blocks. */
636 if (cmd->c_datalen > 0) {
637 blksize = MIN(cmd->c_datalen, cmd->c_blklen);
638 blkcount = cmd->c_datalen / blksize;
639 if (cmd->c_datalen % blksize > 0) {
640 /* XXX: Split this command. (1.7.4) */
641 printf("%s: data not a multiple of %d bytes\n",
642 HDEVNAME(hp), blksize);
643 return EINVAL;
644 }
645 }
646
647 /* Check limit imposed by 9-bit block count. (1.7.2) */
648 if (blkcount > SDHC_BLOCK_COUNT_MAX) {
649 printf("%s: too much data\n", HDEVNAME(hp));
650 return EINVAL;
651 }
652
653 /* Prepare transfer mode register value. (2.2.5) */
654 mode = 0;
655 if (ISSET(cmd->c_flags, SCF_CMD_READ))
656 mode |= SDHC_READ_MODE;
657 if (blkcount > 0) {
658 mode |= SDHC_BLOCK_COUNT_ENABLE;
659 if (blkcount > 1) {
660 mode |= SDHC_MULTI_BLOCK_MODE;
661 /* XXX only for memory commands? */
662 mode |= SDHC_AUTO_CMD12_ENABLE;
663 }
664 }
665 #ifdef notyet
666 if (ISSET(hp->flags, SHF_USE_DMA))
667 mode |= SDHC_DMA_ENABLE;
668 #endif
669
670 /*
671 * Prepare command register value. (2.2.6)
672 */
673 command = (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) <<
674 SDHC_COMMAND_INDEX_SHIFT;
675
676 if (ISSET(cmd->c_flags, SCF_RSP_CRC))
677 command |= SDHC_CRC_CHECK_ENABLE;
678 if (ISSET(cmd->c_flags, SCF_RSP_IDX))
679 command |= SDHC_INDEX_CHECK_ENABLE;
680 if (cmd->c_data != NULL)
681 command |= SDHC_DATA_PRESENT_SELECT;
682
683 if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT))
684 command |= SDHC_NO_RESPONSE;
685 else if (ISSET(cmd->c_flags, SCF_RSP_136))
686 command |= SDHC_RESP_LEN_136;
687 else if (ISSET(cmd->c_flags, SCF_RSP_BSY))
688 command |= SDHC_RESP_LEN_48_CHK_BUSY;
689 else
690 command |= SDHC_RESP_LEN_48;
691
692 /* Wait until command and data inhibit bits are clear. (1.5) */
693 if ((error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0)) != 0)
694 return error;
695
696 s = splsdmmc();
697
698 /* Alert the user not to remove the card. */
699 HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON);
700
701 /* XXX: Set DMA start address if SHF_USE_DMA is set. */
702
703 DPRINTF(1,("%s: cmd=%#x mode=%#x blksize=%d blkcount=%d\n",
704 HDEVNAME(hp), command, mode, blksize, blkcount));
705
706 /*
707 * Start a CPU data transfer. Writing to the high order byte
708 * of the SDHC_COMMAND register triggers the SD command. (1.5)
709 */
710 HWRITE2(hp, SDHC_TRANSFER_MODE, mode);
711 HWRITE2(hp, SDHC_BLOCK_SIZE, blksize);
712 if (blkcount > 1)
713 HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount);
714 HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg);
715 HWRITE2(hp, SDHC_COMMAND, command);
716
717 splx(s);
718 return 0;
719 }
720
721 void
sdhc_transfer_data(struct sdhc_host * hp,struct sdmmc_command * cmd)722 sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd)
723 {
724 u_char *datap = cmd->c_data;
725 int i, datalen;
726 int mask;
727 int error;
728
729 mask = ISSET(cmd->c_flags, SCF_CMD_READ) ?
730 SDHC_BUFFER_READ_ENABLE : SDHC_BUFFER_WRITE_ENABLE;
731 error = 0;
732 datalen = cmd->c_datalen;
733
734 DPRINTF(1,("%s: resp=%#x datalen=%d\n", HDEVNAME(hp),
735 MMC_R1(cmd->c_resp), datalen));
736
737 #ifdef SDHC_DEBUG
738 /* XXX I forgot why I wanted to know when this happens :-( */
739 if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) &&
740 ISSET(MMC_R1(cmd->c_resp), 0xcb00))
741 printf("%s: CMD52/53 error response flags %#x\n",
742 HDEVNAME(hp), MMC_R1(cmd->c_resp) & 0xff00);
743 #endif
744
745 while (datalen > 0) {
746 if (!sdhc_wait_intr(hp, SDHC_BUFFER_READ_READY|
747 SDHC_BUFFER_WRITE_READY, SDHC_BUFFER_TIMEOUT)) {
748 error = ETIMEDOUT;
749 break;
750 }
751
752 if ((error = sdhc_wait_state(hp, mask, mask)) != 0)
753 break;
754
755 i = MIN(datalen, cmd->c_blklen);
756 if (ISSET(cmd->c_flags, SCF_CMD_READ))
757 sdhc_read_data(hp, datap, i);
758 else
759 sdhc_write_data(hp, datap, i);
760
761 datap += i;
762 datalen -= i;
763 }
764
765 if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE,
766 SDHC_TRANSFER_TIMEOUT))
767 error = ETIMEDOUT;
768
769 if (error != 0)
770 cmd->c_error = error;
771 SET(cmd->c_flags, SCF_ITSDONE);
772
773 DPRINTF(1,("%s: data transfer done (error=%d)\n",
774 HDEVNAME(hp), cmd->c_error));
775 }
776
777 void
sdhc_read_data(struct sdhc_host * hp,u_char * datap,int datalen)778 sdhc_read_data(struct sdhc_host *hp, u_char *datap, int datalen)
779 {
780 while (datalen > 3) {
781 *(u_int32_t *)datap = HREAD4(hp, SDHC_DATA);
782 datap += 4;
783 datalen -= 4;
784 }
785 if (datalen > 0) {
786 u_int32_t rv = HREAD4(hp, SDHC_DATA);
787 do {
788 *datap++ = rv & 0xff;
789 rv = rv >> 8;
790 } while (--datalen > 0);
791 }
792 }
793
794 void
sdhc_write_data(struct sdhc_host * hp,u_char * datap,int datalen)795 sdhc_write_data(struct sdhc_host *hp, u_char *datap, int datalen)
796 {
797 while (datalen > 3) {
798 DPRINTF(3,("%08x\n", *(u_int32_t *)datap));
799 HWRITE4(hp, SDHC_DATA, *((u_int32_t *)datap));
800 datap += 4;
801 datalen -= 4;
802 }
803 if (datalen > 0) {
804 u_int32_t rv = *datap++;
805 if (datalen > 1)
806 rv |= *datap++ << 8;
807 if (datalen > 2)
808 rv |= *datap++ << 16;
809 DPRINTF(3,("rv %08x\n", rv));
810 HWRITE4(hp, SDHC_DATA, rv);
811 }
812 }
813
814 /* Prepare for another command. */
815 int
sdhc_soft_reset(struct sdhc_host * hp,int mask)816 sdhc_soft_reset(struct sdhc_host *hp, int mask)
817 {
818 int timo;
819
820 DPRINTF(1,("%s: software reset reg=%#x\n", HDEVNAME(hp), mask));
821
822 HWRITE1(hp, SDHC_SOFTWARE_RESET, mask);
823 for (timo = 10; timo > 0; timo--) {
824 if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask))
825 break;
826 sdmmc_delay(10000);
827 HWRITE1(hp, SDHC_SOFTWARE_RESET, 0);
828 }
829 if (timo == 0) {
830 DPRINTF(1,("%s: timeout reg=%#x\n", HDEVNAME(hp),
831 HREAD1(hp, SDHC_SOFTWARE_RESET)));
832 HWRITE1(hp, SDHC_SOFTWARE_RESET, 0);
833 return (ETIMEDOUT);
834 }
835
836 return (0);
837 }
838
839 int
sdhc_wait_intr(struct sdhc_host * hp,int mask,int timo)840 sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo)
841 {
842 int status;
843 int s;
844
845 mask |= SDHC_ERROR_INTERRUPT;
846
847 s = splsdmmc();
848 status = hp->intr_status & mask;
849 while (status == 0) {
850 if (tsleep(&hp->intr_status, PWAIT, "hcintr", timo)
851 == EWOULDBLOCK) {
852 status |= SDHC_ERROR_INTERRUPT;
853 break;
854 }
855 status = hp->intr_status & mask;
856 }
857 hp->intr_status &= ~status;
858
859 DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status,
860 hp->intr_error_status));
861
862 /* Command timeout has higher priority than command complete. */
863 if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
864 hp->intr_error_status = 0;
865 (void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD);
866 status = 0;
867 }
868
869 splx(s);
870 return status;
871 }
872
873 /*
874 * Established by attachment driver at interrupt priority IPL_SDMMC.
875 */
876 int
sdhc_intr(void * arg)877 sdhc_intr(void *arg)
878 {
879 struct sdhc_softc *sc = arg;
880 int host;
881 int done = 0;
882
883 /* We got an interrupt, but we don't know from which slot. */
884 for (host = 0; host < sc->sc_nhosts; host++) {
885 struct sdhc_host *hp = sc->sc_host[host];
886 u_int16_t status;
887
888 if (hp == NULL)
889 continue;
890
891 /* Find out which interrupts are pending. */
892 status = HREAD2(hp, SDHC_NINTR_STATUS);
893 if (!ISSET(status, SDHC_NINTR_STATUS_MASK))
894 continue; /* no interrupt for us */
895
896 /* Acknowledge the interrupts we are about to handle. */
897 HWRITE2(hp, SDHC_NINTR_STATUS, status);
898 DPRINTF(2,("%s: interrupt status=%b\n", HDEVNAME(hp),
899 status, SDHC_NINTR_STATUS_BITS));
900
901 /* Claim this interrupt. */
902 done = 1;
903
904 /*
905 * Service error interrupts.
906 */
907 if (ISSET(status, SDHC_ERROR_INTERRUPT)) {
908 u_int16_t error;
909
910 /* Acknowledge error interrupts. */
911 error = HREAD2(hp, SDHC_EINTR_STATUS);
912 HWRITE2(hp, SDHC_EINTR_STATUS, error);
913 DPRINTF(2,("%s: error interrupt, status=%b\n",
914 HDEVNAME(hp), error, SDHC_EINTR_STATUS_BITS));
915
916 if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR|
917 SDHC_DATA_TIMEOUT_ERROR)) {
918 hp->intr_error_status |= error;
919 hp->intr_status |= status;
920 wakeup(&hp->intr_status);
921 }
922 }
923
924 /*
925 * Wake up the sdmmc event thread to scan for cards.
926 */
927 if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION))
928 sdmmc_needs_discover(hp->sdmmc);
929
930 /*
931 * Wake up the blocking process to service command
932 * related interrupt(s).
933 */
934 if (ISSET(status, SDHC_BUFFER_READ_READY|
935 SDHC_BUFFER_WRITE_READY|SDHC_COMMAND_COMPLETE|
936 SDHC_TRANSFER_COMPLETE)) {
937 hp->intr_status |= status;
938 wakeup(&hp->intr_status);
939 }
940
941 /*
942 * Service SD card interrupts.
943 */
944 if (ISSET(status, SDHC_CARD_INTERRUPT)) {
945 DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp)));
946 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT);
947 sdmmc_card_intr(hp->sdmmc);
948 }
949 }
950 return done;
951 }
952
953 #ifdef SDHC_DEBUG
954 void
sdhc_dump_regs(struct sdhc_host * hp)955 sdhc_dump_regs(struct sdhc_host *hp)
956 {
957 printf("0x%02x PRESENT_STATE: %b\n", SDHC_PRESENT_STATE,
958 HREAD4(hp, SDHC_PRESENT_STATE), SDHC_PRESENT_STATE_BITS);
959 printf("0x%02x POWER_CTL: %x\n", SDHC_POWER_CTL,
960 HREAD1(hp, SDHC_POWER_CTL));
961 printf("0x%02x NINTR_STATUS: %x\n", SDHC_NINTR_STATUS,
962 HREAD2(hp, SDHC_NINTR_STATUS));
963 printf("0x%02x EINTR_STATUS: %x\n", SDHC_EINTR_STATUS,
964 HREAD2(hp, SDHC_EINTR_STATUS));
965 printf("0x%02x NINTR_STATUS_EN: %x\n", SDHC_NINTR_STATUS_EN,
966 HREAD2(hp, SDHC_NINTR_STATUS_EN));
967 printf("0x%02x EINTR_STATUS_EN: %x\n", SDHC_EINTR_STATUS_EN,
968 HREAD2(hp, SDHC_EINTR_STATUS_EN));
969 printf("0x%02x NINTR_SIGNAL_EN: %x\n", SDHC_NINTR_SIGNAL_EN,
970 HREAD2(hp, SDHC_NINTR_SIGNAL_EN));
971 printf("0x%02x EINTR_SIGNAL_EN: %x\n", SDHC_EINTR_SIGNAL_EN,
972 HREAD2(hp, SDHC_EINTR_SIGNAL_EN));
973 printf("0x%02x CAPABILITIES: %x\n", SDHC_CAPABILITIES,
974 HREAD4(hp, SDHC_CAPABILITIES));
975 printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES,
976 HREAD4(hp, SDHC_MAX_CAPABILITIES));
977 }
978 #endif
979