1 /* $NetBSD: sdhc.c,v 1.1 2009/04/21 03:00:30 nonaka 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@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.1 2009/04/21 03:00:30 nonaka Exp $"); 27 28 #include <sys/param.h> 29 #include <sys/device.h> 30 #include <sys/kernel.h> 31 #include <sys/kthread.h> 32 #include <sys/malloc.h> 33 #include <sys/systm.h> 34 #include <sys/mutex.h> 35 #include <sys/condvar.h> 36 37 #include <dev/sdmmc/sdhcreg.h> 38 #include <dev/sdmmc/sdhcvar.h> 39 #include <dev/sdmmc/sdmmcchip.h> 40 #include <dev/sdmmc/sdmmcreg.h> 41 #include <dev/sdmmc/sdmmcvar.h> 42 43 #ifdef SDHC_DEBUG 44 int sdhcdebug = 1; 45 #define DPRINTF(n,s) do { if ((n) <= sdhcdebug) printf s; } while (0) 46 void sdhc_dump_regs(struct sdhc_host *); 47 #else 48 #define DPRINTF(n,s) do {} while (0) 49 #endif 50 51 #define SDHC_COMMAND_TIMEOUT hz 52 #define SDHC_BUFFER_TIMEOUT hz 53 #define SDHC_TRANSFER_TIMEOUT hz 54 #define SDHC_DMA_TIMEOUT hz 55 56 struct sdhc_host { 57 struct sdhc_softc *sc; /* host controller device */ 58 59 bus_space_tag_t iot; /* host register set tag */ 60 bus_space_handle_t ioh; /* host register set handle */ 61 bus_dma_tag_t dmat; /* host DMA tag */ 62 63 device_t sdmmc; /* generic SD/MMC device */ 64 65 struct kmutex host_mtx; 66 67 u_int clkbase; /* base clock frequency in KHz */ 68 int maxblklen; /* maximum block length */ 69 uint32_t ocr; /* OCR value from capabilities */ 70 71 uint8_t regs[14]; /* host controller state */ 72 73 uint16_t intr_status; /* soft interrupt status */ 74 uint16_t intr_error_status; /* soft error status */ 75 struct kmutex intr_mtx; 76 struct kcondvar intr_cv; 77 78 uint32_t flags; /* flags for this host */ 79 #define SHF_USE_DMA 0x0001 80 #define SHF_USE_4BIT_MODE 0x0002 81 }; 82 83 #define HDEVNAME(hp) (device_xname((hp)->sc->sc_dev)) 84 85 #define HREAD1(hp, reg) \ 86 (bus_space_read_1((hp)->iot, (hp)->ioh, (reg))) 87 #define HREAD2(hp, reg) \ 88 (bus_space_read_2((hp)->iot, (hp)->ioh, (reg))) 89 #define HREAD4(hp, reg) \ 90 (bus_space_read_4((hp)->iot, (hp)->ioh, (reg))) 91 #define HWRITE1(hp, reg, val) \ 92 bus_space_write_1((hp)->iot, (hp)->ioh, (reg), (val)) 93 #define HWRITE2(hp, reg, val) \ 94 bus_space_write_2((hp)->iot, (hp)->ioh, (reg), (val)) 95 #define HWRITE4(hp, reg, val) \ 96 bus_space_write_4((hp)->iot, (hp)->ioh, (reg), (val)) 97 #define HCLR1(hp, reg, bits) \ 98 HWRITE1((hp), (reg), HREAD1((hp), (reg)) & ~(bits)) 99 #define HCLR2(hp, reg, bits) \ 100 HWRITE2((hp), (reg), HREAD2((hp), (reg)) & ~(bits)) 101 #define HSET1(hp, reg, bits) \ 102 HWRITE1((hp), (reg), HREAD1((hp), (reg)) | (bits)) 103 #define HSET2(hp, reg, bits) \ 104 HWRITE2((hp), (reg), HREAD2((hp), (reg)) | (bits)) 105 106 static int sdhc_host_reset(sdmmc_chipset_handle_t); 107 static int sdhc_host_reset1(sdmmc_chipset_handle_t); 108 static uint32_t sdhc_host_ocr(sdmmc_chipset_handle_t); 109 static int sdhc_host_maxblklen(sdmmc_chipset_handle_t); 110 static int sdhc_card_detect(sdmmc_chipset_handle_t); 111 static int sdhc_write_protect(sdmmc_chipset_handle_t); 112 static int sdhc_bus_power(sdmmc_chipset_handle_t, uint32_t); 113 static int sdhc_bus_clock(sdmmc_chipset_handle_t, int); 114 static int sdhc_bus_width(sdmmc_chipset_handle_t, int); 115 static void sdhc_card_enable_intr(sdmmc_chipset_handle_t, int); 116 static void sdhc_card_intr_ack(sdmmc_chipset_handle_t); 117 static void sdhc_exec_command(sdmmc_chipset_handle_t, 118 struct sdmmc_command *); 119 static int sdhc_start_command(struct sdhc_host *, struct sdmmc_command *); 120 static int sdhc_wait_state(struct sdhc_host *, uint32_t, uint32_t); 121 static int sdhc_soft_reset(struct sdhc_host *, int); 122 static int sdhc_wait_intr(struct sdhc_host *, int, int); 123 static void sdhc_transfer_data(struct sdhc_host *, struct sdmmc_command *); 124 static int sdhc_transfer_data_pio(struct sdhc_host *, struct sdmmc_command *); 125 static void sdhc_read_data_pio(struct sdhc_host *, uint8_t *, int); 126 static void sdhc_write_data_pio(struct sdhc_host *, uint8_t *, int); 127 128 static struct sdmmc_chip_functions sdhc_functions = { 129 /* host controller reset */ 130 sdhc_host_reset, 131 132 /* host controller capabilities */ 133 sdhc_host_ocr, 134 sdhc_host_maxblklen, 135 136 /* card detection */ 137 sdhc_card_detect, 138 139 /* write protect */ 140 sdhc_write_protect, 141 142 /* bus power, clock frequency and width */ 143 sdhc_bus_power, 144 sdhc_bus_clock, 145 sdhc_bus_width, 146 147 /* command execution */ 148 sdhc_exec_command, 149 150 /* card interrupt */ 151 sdhc_card_enable_intr, 152 sdhc_card_intr_ack 153 }; 154 155 /* 156 * Called by attachment driver. For each SD card slot there is one SD 157 * host controller standard register set. (1.3) 158 */ 159 int 160 sdhc_host_found(struct sdhc_softc *sc, bus_space_tag_t iot, 161 bus_space_handle_t ioh, bus_size_t iosize) 162 { 163 struct sdmmcbus_attach_args saa; 164 struct sdhc_host *hp; 165 uint32_t caps; 166 #ifdef SDHC_DEBUG 167 uint16_t sdhcver; 168 169 sdhcver = bus_space_read_2(iot, ioh, SDHC_HOST_CTL_VERSION); 170 aprint_normal_dev(sc->sc_dev, "SD Host Specification/Vendor Version "); 171 switch (SDHC_SPEC_VERSION(sdhcver)) { 172 case 0x00: 173 aprint_normal("1.0/%u\n", SDHC_VENDOR_VERSION(sdhcver)); 174 break; 175 176 default: 177 aprint_normal(">1.0/%u\n", SDHC_VENDOR_VERSION(sdhcver)); 178 break; 179 } 180 #endif 181 182 /* Allocate one more host structure. */ 183 hp = malloc(sizeof(struct sdhc_host), M_DEVBUF, M_WAITOK|M_ZERO); 184 if (hp == NULL) { 185 aprint_error_dev(sc->sc_dev, 186 "couldn't alloc memory (sdhc host)\n"); 187 goto err1; 188 } 189 sc->sc_host[sc->sc_nhosts++] = hp; 190 191 /* Fill in the new host structure. */ 192 hp->sc = sc; 193 hp->iot = iot; 194 hp->ioh = ioh; 195 hp->dmat = sc->sc_dmat; 196 197 mutex_init(&hp->host_mtx, MUTEX_DEFAULT, IPL_SDMMC); 198 mutex_init(&hp->intr_mtx, MUTEX_DEFAULT, IPL_SDMMC); 199 cv_init(&hp->intr_cv, "sdhcintr"); 200 201 /* 202 * eset the host controller and enable interrupts. 203 */ 204 (void)sdhc_host_reset(hp); 205 206 /* Determine host capabilities. */ 207 mutex_enter(&hp->host_mtx); 208 caps = HREAD4(hp, SDHC_CAPABILITIES); 209 mutex_exit(&hp->host_mtx); 210 211 #if notyet 212 /* Use DMA if the host system and the controller support it. */ 213 if (ISSET(sc->sc_flags, SDHC_FLAG_FORCE_DMA) 214 || ((ISSET(sc->sc_flags, SDHC_FLAG_USE_DMA) 215 && ISSET(caps, SDHC_DMA_SUPPORT)))) { 216 SET(hp->flags, SHF_USE_DMA); 217 aprint_normal_dev(sc->sc_dev, "using DMA transfer\n"); 218 } 219 #endif 220 221 /* 222 * Determine the base clock frequency. (2.2.24) 223 */ 224 if (SDHC_BASE_FREQ_KHZ(caps) != 0) 225 hp->clkbase = SDHC_BASE_FREQ_KHZ(caps); 226 if (hp->clkbase == 0) { 227 /* The attachment driver must tell us. */ 228 aprint_error_dev(sc->sc_dev,"unknown base clock frequency\n"); 229 goto err; 230 } else if (hp->clkbase < 10000 || hp->clkbase > 63000) { 231 /* SDHC 1.0 supports only 10-63 MHz. */ 232 aprint_error_dev(sc->sc_dev, 233 "base clock frequency out of range: %u MHz\n", 234 hp->clkbase / 1000); 235 goto err; 236 } 237 DPRINTF(1,("%s: base clock frequency %u MHz\n", 238 device_xname(sc->sc_dev), hp->clkbase / 1000)); 239 240 /* 241 * XXX Set the data timeout counter value according to 242 * capabilities. (2.2.15) 243 */ 244 HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX); 245 246 /* 247 * Determine SD bus voltage levels supported by the controller. 248 */ 249 if (ISSET(caps, SDHC_VOLTAGE_SUPP_1_8V)) 250 SET(hp->ocr, MMC_OCR_1_7V_1_8V | MMC_OCR_1_8V_1_9V); 251 if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_0V)) 252 SET(hp->ocr, MMC_OCR_2_9V_3_0V | MMC_OCR_3_0V_3_1V); 253 if (ISSET(caps, SDHC_VOLTAGE_SUPP_3_3V)) 254 SET(hp->ocr, MMC_OCR_3_2V_3_3V | MMC_OCR_3_3V_3_4V); 255 256 /* 257 * Determine the maximum block length supported by the host 258 * controller. (2.2.24) 259 */ 260 switch((caps >> SDHC_MAX_BLK_LEN_SHIFT) & SDHC_MAX_BLK_LEN_MASK) { 261 case SDHC_MAX_BLK_LEN_512: 262 hp->maxblklen = 512; 263 break; 264 265 case SDHC_MAX_BLK_LEN_1024: 266 hp->maxblklen = 1024; 267 break; 268 269 case SDHC_MAX_BLK_LEN_2048: 270 hp->maxblklen = 2048; 271 break; 272 273 default: 274 aprint_error_dev(sc->sc_dev, "max block length unknown\n"); 275 goto err; 276 } 277 DPRINTF(1, ("%s: max block length %u byte%s\n", 278 device_xname(sc->sc_dev), hp->maxblklen, 279 hp->maxblklen > 1 ? "s" : "")); 280 281 /* 282 * Attach the generic SD/MMC bus driver. (The bus driver must 283 * not invoke any chipset functions before it is attached.) 284 */ 285 memset(&saa, 0, sizeof(saa)); 286 saa.saa_busname = "sdmmc"; 287 saa.saa_sct = &sdhc_functions; 288 saa.saa_sch = hp; 289 saa.saa_dmat = hp->dmat; 290 saa.saa_clkmin = hp->clkbase / 256; 291 saa.saa_clkmax = hp->clkbase; 292 saa.saa_caps = SMC_CAPS_4BIT_MODE|SMC_CAPS_AUTO_STOP; 293 #if notyet 294 if (ISSET(hp->flags, SHF_USE_DMA)) 295 saa.saa_caps |= SMC_CAPS_DMA; 296 #endif 297 298 hp->sdmmc = config_found(sc->sc_dev, &saa, NULL); 299 300 return 0; 301 302 err: 303 cv_destroy(&hp->intr_cv); 304 mutex_destroy(&hp->intr_mtx); 305 mutex_destroy(&hp->host_mtx); 306 free(hp, M_DEVBUF); 307 sc->sc_host[--sc->sc_nhosts] = NULL; 308 err1: 309 return 1; 310 } 311 312 bool 313 sdhc_suspend(device_t dev PMF_FN_ARGS) 314 { 315 struct sdhc_softc *sc = device_private(dev); 316 struct sdhc_host *hp; 317 int n, i; 318 319 /* XXX poll for command completion or suspend command 320 * in progress */ 321 322 /* Save the host controller state. */ 323 for (n = 0; n < sc->sc_nhosts; n++) { 324 hp = sc->sc_host[n]; 325 for (i = 0; i < sizeof hp->regs; i++) 326 hp->regs[i] = HREAD1(hp, i); 327 } 328 return true; 329 } 330 331 bool 332 sdhc_resume(device_t dev PMF_FN_ARGS) 333 { 334 struct sdhc_softc *sc = device_private(dev); 335 struct sdhc_host *hp; 336 int n, i; 337 338 /* Restore the host controller state. */ 339 for (n = 0; n < sc->sc_nhosts; n++) { 340 hp = sc->sc_host[n]; 341 (void)sdhc_host_reset(hp); 342 for (i = 0; i < sizeof hp->regs; i++) 343 HWRITE1(hp, i, hp->regs[i]); 344 } 345 return true; 346 } 347 348 bool 349 sdhc_shutdown(device_t dev, int flags) 350 { 351 struct sdhc_softc *sc = device_private(dev); 352 struct sdhc_host *hp; 353 int i; 354 355 /* XXX chip locks up if we don't disable it before reboot. */ 356 for (i = 0; i < sc->sc_nhosts; i++) { 357 hp = sc->sc_host[i]; 358 (void)sdhc_host_reset(hp); 359 } 360 return true; 361 } 362 363 /* 364 * Reset the host controller. Called during initialization, when 365 * cards are removed, upon resume, and during error recovery. 366 */ 367 static int 368 sdhc_host_reset1(sdmmc_chipset_handle_t sch) 369 { 370 struct sdhc_host *hp = (struct sdhc_host *)sch; 371 uint16_t sdhcimask; 372 int error; 373 374 /* Don't lock. */ 375 376 /* Disable all interrupts. */ 377 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, 0); 378 379 /* 380 * Reset the entire host controller and wait up to 100ms for 381 * the controller to clear the reset bit. 382 */ 383 error = sdhc_soft_reset(hp, SDHC_RESET_ALL); 384 if (error) 385 goto out; 386 387 /* Set data timeout counter value to max for now. */ 388 HWRITE1(hp, SDHC_TIMEOUT_CTL, SDHC_TIMEOUT_MAX); 389 390 /* Enable interrupts. */ 391 sdhcimask = SDHC_CARD_REMOVAL | SDHC_CARD_INSERTION | 392 SDHC_BUFFER_READ_READY | SDHC_BUFFER_WRITE_READY | 393 SDHC_DMA_INTERRUPT | SDHC_BLOCK_GAP_EVENT | 394 SDHC_TRANSFER_COMPLETE | SDHC_COMMAND_COMPLETE; 395 HWRITE2(hp, SDHC_NINTR_STATUS_EN, sdhcimask); 396 HWRITE2(hp, SDHC_EINTR_STATUS_EN, SDHC_EINTR_STATUS_MASK); 397 HWRITE2(hp, SDHC_NINTR_SIGNAL_EN, sdhcimask); 398 HWRITE2(hp, SDHC_EINTR_SIGNAL_EN, SDHC_EINTR_SIGNAL_MASK); 399 400 out: 401 return error; 402 } 403 404 static int 405 sdhc_host_reset(sdmmc_chipset_handle_t sch) 406 { 407 struct sdhc_host *hp = (struct sdhc_host *)sch; 408 int error; 409 410 mutex_enter(&hp->host_mtx); 411 error = sdhc_host_reset1(sch); 412 mutex_exit(&hp->host_mtx); 413 414 return error; 415 } 416 417 static uint32_t 418 sdhc_host_ocr(sdmmc_chipset_handle_t sch) 419 { 420 struct sdhc_host *hp = (struct sdhc_host *)sch; 421 422 return hp->ocr; 423 } 424 425 static int 426 sdhc_host_maxblklen(sdmmc_chipset_handle_t sch) 427 { 428 struct sdhc_host *hp = (struct sdhc_host *)sch; 429 430 return hp->maxblklen; 431 } 432 433 /* 434 * Return non-zero if the card is currently inserted. 435 */ 436 static int 437 sdhc_card_detect(sdmmc_chipset_handle_t sch) 438 { 439 struct sdhc_host *hp = (struct sdhc_host *)sch; 440 int r; 441 442 mutex_enter(&hp->host_mtx); 443 r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CARD_INSERTED); 444 mutex_exit(&hp->host_mtx); 445 446 if (r) 447 return 1; 448 return 0; 449 } 450 451 /* 452 * Return non-zero if the card is currently write-protected. 453 */ 454 static int 455 sdhc_write_protect(sdmmc_chipset_handle_t sch) 456 { 457 struct sdhc_host *hp = (struct sdhc_host *)sch; 458 int r; 459 460 mutex_enter(&hp->host_mtx); 461 r = ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_WRITE_PROTECT_SWITCH); 462 mutex_exit(&hp->host_mtx); 463 464 if (!r) 465 return 1; 466 return 0; 467 } 468 469 /* 470 * Set or change SD bus voltage and enable or disable SD bus power. 471 * Return zero on success. 472 */ 473 static int 474 sdhc_bus_power(sdmmc_chipset_handle_t sch, uint32_t ocr) 475 { 476 struct sdhc_host *hp = (struct sdhc_host *)sch; 477 uint8_t vdd; 478 int error = 0; 479 480 mutex_enter(&hp->host_mtx); 481 482 /* 483 * Disable bus power before voltage change. 484 */ 485 if (!(hp->sc->sc_flags & SDHC_FLAG_NO_PWR0)) 486 HWRITE1(hp, SDHC_POWER_CTL, 0); 487 488 /* If power is disabled, reset the host and return now. */ 489 if (ocr == 0) { 490 (void)sdhc_host_reset1(hp); 491 goto out; 492 } 493 494 /* 495 * Select the lowest voltage according to capabilities. 496 */ 497 ocr &= hp->ocr; 498 if (ISSET(ocr, MMC_OCR_1_7V_1_8V|MMC_OCR_1_8V_1_9V)) 499 vdd = SDHC_VOLTAGE_1_8V; 500 else if (ISSET(ocr, MMC_OCR_2_9V_3_0V|MMC_OCR_3_0V_3_1V)) 501 vdd = SDHC_VOLTAGE_3_0V; 502 else if (ISSET(ocr, MMC_OCR_3_2V_3_3V|MMC_OCR_3_3V_3_4V)) 503 vdd = SDHC_VOLTAGE_3_3V; 504 else { 505 /* Unsupported voltage level requested. */ 506 error = EINVAL; 507 goto out; 508 } 509 510 /* 511 * Enable bus power. Wait at least 1 ms (or 74 clocks) plus 512 * voltage ramp until power rises. 513 */ 514 HWRITE1(hp, SDHC_POWER_CTL, 515 (vdd << SDHC_VOLTAGE_SHIFT) | SDHC_BUS_POWER); 516 sdmmc_delay(10000); 517 518 /* 519 * The host system may not power the bus due to battery low, 520 * etc. In that case, the host controller should clear the 521 * bus power bit. 522 */ 523 if (!ISSET(HREAD1(hp, SDHC_POWER_CTL), SDHC_BUS_POWER)) { 524 error = ENXIO; 525 goto out; 526 } 527 528 out: 529 mutex_exit(&hp->host_mtx); 530 531 return error; 532 } 533 534 /* 535 * Return the smallest possible base clock frequency divisor value 536 * for the CLOCK_CTL register to produce `freq' (KHz). 537 */ 538 static int 539 sdhc_clock_divisor(struct sdhc_host *hp, u_int freq) 540 { 541 int div; 542 543 for (div = 1; div <= 256; div *= 2) 544 if ((hp->clkbase / div) <= freq) 545 return (div / 2); 546 /* No divisor found. */ 547 return -1; 548 } 549 550 /* 551 * Set or change SDCLK frequency or disable the SD clock. 552 * Return zero on success. 553 */ 554 static int 555 sdhc_bus_clock(sdmmc_chipset_handle_t sch, int freq) 556 { 557 struct sdhc_host *hp = (struct sdhc_host *)sch; 558 int div; 559 int timo; 560 int error = 0; 561 562 mutex_enter(&hp->host_mtx); 563 564 #ifdef DIAGNOSTIC 565 /* Must not stop the clock if commands are in progress. */ 566 if (ISSET(HREAD4(hp, SDHC_PRESENT_STATE), SDHC_CMD_INHIBIT_MASK) && 567 sdhc_card_detect(hp)) 568 printf("%s: sdhc_sdclk_frequency_select: command in progress\n", 569 device_xname(hp->sc->sc_dev)); 570 #endif 571 572 /* 573 * Stop SD clock before changing the frequency. 574 */ 575 HWRITE2(hp, SDHC_CLOCK_CTL, 0); 576 if (freq == SDMMC_SDCLK_OFF) 577 goto out; 578 579 /* 580 * Set the minimum base clock frequency divisor. 581 */ 582 if ((div = sdhc_clock_divisor(hp, freq)) < 0) { 583 /* Invalid base clock frequency or `freq' value. */ 584 error = EINVAL; 585 goto out; 586 } 587 HWRITE2(hp, SDHC_CLOCK_CTL, div << SDHC_SDCLK_DIV_SHIFT); 588 589 /* 590 * Start internal clock. Wait 10ms for stabilization. 591 */ 592 HSET2(hp, SDHC_CLOCK_CTL, SDHC_INTCLK_ENABLE); 593 for (timo = 1000; timo > 0; timo--) { 594 if (ISSET(HREAD2(hp, SDHC_CLOCK_CTL), SDHC_INTCLK_STABLE)) 595 break; 596 sdmmc_delay(10); 597 } 598 if (timo == 0) { 599 error = ETIMEDOUT; 600 goto out; 601 } 602 603 /* 604 * Enable SD clock. 605 */ 606 HSET2(hp, SDHC_CLOCK_CTL, SDHC_SDCLK_ENABLE); 607 608 out: 609 mutex_exit(&hp->host_mtx); 610 611 return error; 612 } 613 614 static int 615 sdhc_bus_width(sdmmc_chipset_handle_t sch, int width) 616 { 617 struct sdhc_host *hp = (struct sdhc_host *)sch; 618 int reg; 619 620 switch (width) { 621 case 1: 622 case 4: 623 break; 624 625 default: 626 DPRINTF(0,("%s: unsupported bus width (%d)\n", 627 HDEVNAME(hp), width)); 628 return 1; 629 } 630 631 mutex_enter(&hp->host_mtx); 632 reg = HREAD1(hp, SDHC_POWER_CTL); 633 reg &= ~SDHC_4BIT_MODE; 634 if (width == 4) 635 reg |= SDHC_4BIT_MODE; 636 HWRITE1(hp, SDHC_POWER_CTL, reg); 637 mutex_exit(&hp->host_mtx); 638 639 return 0; 640 } 641 642 static void 643 sdhc_card_enable_intr(sdmmc_chipset_handle_t sch, int enable) 644 { 645 struct sdhc_host *hp = (struct sdhc_host *)sch; 646 647 mutex_enter(&hp->host_mtx); 648 if (enable) { 649 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT); 650 HSET2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT); 651 } else { 652 HCLR2(hp, SDHC_NINTR_SIGNAL_EN, SDHC_CARD_INTERRUPT); 653 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT); 654 } 655 mutex_exit(&hp->host_mtx); 656 } 657 658 static void 659 sdhc_card_intr_ack(sdmmc_chipset_handle_t sch) 660 { 661 struct sdhc_host *hp = (struct sdhc_host *)sch; 662 663 mutex_enter(&hp->host_mtx); 664 HSET2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT); 665 mutex_exit(&hp->host_mtx); 666 } 667 668 static int 669 sdhc_wait_state(struct sdhc_host *hp, uint32_t mask, uint32_t value) 670 { 671 uint32_t state; 672 int timeout; 673 674 for (timeout = 10; timeout > 0; timeout--) { 675 if (((state = HREAD4(hp, SDHC_PRESENT_STATE)) & mask) == value) 676 return 0; 677 sdmmc_delay(10000); 678 } 679 DPRINTF(0,("%s: timeout waiting for %x (state=%x)\n", HDEVNAME(hp), 680 value, state)); 681 return ETIMEDOUT; 682 } 683 684 static void 685 sdhc_exec_command(sdmmc_chipset_handle_t sch, struct sdmmc_command *cmd) 686 { 687 struct sdhc_host *hp = (struct sdhc_host *)sch; 688 int error; 689 690 /* 691 * Start the MMC command, or mark `cmd' as failed and return. 692 */ 693 error = sdhc_start_command(hp, cmd); 694 if (error) { 695 cmd->c_error = error; 696 goto out; 697 } 698 699 /* 700 * Wait until the command phase is done, or until the command 701 * is marked done for any other reason. 702 */ 703 if (!sdhc_wait_intr(hp, SDHC_COMMAND_COMPLETE, SDHC_COMMAND_TIMEOUT)) { 704 cmd->c_error = ETIMEDOUT; 705 goto out; 706 } 707 708 /* 709 * The host controller removes bits [0:7] from the response 710 * data (CRC) and we pass the data up unchanged to the bus 711 * driver (without padding). 712 */ 713 mutex_enter(&hp->host_mtx); 714 if (cmd->c_error == 0 && ISSET(cmd->c_flags, SCF_RSP_PRESENT)) { 715 if (ISSET(cmd->c_flags, SCF_RSP_136)) { 716 uint8_t *p = (uint8_t *)cmd->c_resp; 717 int i; 718 719 for (i = 0; i < 15; i++) 720 *p++ = HREAD1(hp, SDHC_RESPONSE + i); 721 } else { 722 cmd->c_resp[0] = HREAD4(hp, SDHC_RESPONSE); 723 } 724 } 725 mutex_exit(&hp->host_mtx); 726 DPRINTF(1,("%s: resp = %08x\n", HDEVNAME(hp), cmd->c_resp[0])); 727 728 /* 729 * If the command has data to transfer in any direction, 730 * execute the transfer now. 731 */ 732 if (cmd->c_error == 0 && cmd->c_data != NULL) 733 sdhc_transfer_data(hp, cmd); 734 735 out: 736 mutex_enter(&hp->host_mtx); 737 /* Turn off the LED. */ 738 HCLR1(hp, SDHC_HOST_CTL, SDHC_LED_ON); 739 mutex_exit(&hp->host_mtx); 740 SET(cmd->c_flags, SCF_ITSDONE); 741 742 DPRINTF(1,("%s: cmd %d %s (flags=%08x error=%d)\n", HDEVNAME(hp), 743 cmd->c_opcode, (cmd->c_error == 0) ? "done" : "abort", 744 cmd->c_flags, cmd->c_error)); 745 } 746 747 static int 748 sdhc_start_command(struct sdhc_host *hp, struct sdmmc_command *cmd) 749 { 750 uint16_t blksize = 0; 751 uint16_t blkcount = 0; 752 uint16_t mode; 753 uint16_t command; 754 int error; 755 756 DPRINTF(1,("%s: start cmd %d arg=%08x data=%p dlen=%d flags=%08x " 757 "proc=%p \"%s\"\n", HDEVNAME(hp), cmd->c_opcode, cmd->c_arg, 758 cmd->c_data, cmd->c_datalen, cmd->c_flags, curproc, 759 curproc ? curproc->p_comm : "")); 760 761 /* 762 * The maximum block length for commands should be the minimum 763 * of the host buffer size and the card buffer size. (1.7.2) 764 */ 765 766 /* Fragment the data into proper blocks. */ 767 if (cmd->c_datalen > 0) { 768 blksize = MIN(cmd->c_datalen, cmd->c_blklen); 769 blkcount = cmd->c_datalen / blksize; 770 if (cmd->c_datalen % blksize > 0) { 771 /* XXX: Split this command. (1.7.4) */ 772 aprint_error_dev(hp->sc->sc_dev, 773 "data not a multiple of %u bytes\n", blksize); 774 return EINVAL; 775 } 776 } 777 778 /* Check limit imposed by 9-bit block count. (1.7.2) */ 779 if (blkcount > SDHC_BLOCK_COUNT_MAX) { 780 aprint_error_dev(hp->sc->sc_dev, "too much data\n"); 781 return EINVAL; 782 } 783 784 /* Prepare transfer mode register value. (2.2.5) */ 785 mode = 0; 786 if (ISSET(cmd->c_flags, SCF_CMD_READ)) 787 mode |= SDHC_READ_MODE; 788 if (blkcount > 0) { 789 mode |= SDHC_BLOCK_COUNT_ENABLE; 790 if (blkcount > 1) { 791 mode |= SDHC_MULTI_BLOCK_MODE; 792 /* XXX only for memory commands? */ 793 mode |= SDHC_AUTO_CMD12_ENABLE; 794 } 795 } 796 #if notyet 797 if (cmd->c_dmap != NULL && cmd->c_datalen > 0) 798 mode |= SDHC_DMA_ENABLE; 799 #endif 800 801 /* 802 * Prepare command register value. (2.2.6) 803 */ 804 command = 805 (cmd->c_opcode & SDHC_COMMAND_INDEX_MASK) << SDHC_COMMAND_INDEX_SHIFT; 806 807 if (ISSET(cmd->c_flags, SCF_RSP_CRC)) 808 command |= SDHC_CRC_CHECK_ENABLE; 809 if (ISSET(cmd->c_flags, SCF_RSP_IDX)) 810 command |= SDHC_INDEX_CHECK_ENABLE; 811 if (cmd->c_data != NULL) 812 command |= SDHC_DATA_PRESENT_SELECT; 813 814 if (!ISSET(cmd->c_flags, SCF_RSP_PRESENT)) 815 command |= SDHC_NO_RESPONSE; 816 else if (ISSET(cmd->c_flags, SCF_RSP_136)) 817 command |= SDHC_RESP_LEN_136; 818 else if (ISSET(cmd->c_flags, SCF_RSP_BSY)) 819 command |= SDHC_RESP_LEN_48_CHK_BUSY; 820 else 821 command |= SDHC_RESP_LEN_48; 822 823 /* Wait until command and data inhibit bits are clear. (1.5) */ 824 error = sdhc_wait_state(hp, SDHC_CMD_INHIBIT_MASK, 0); 825 if (error) 826 return error; 827 828 DPRINTF(1,("%s: writing cmd: blksize=%d blkcnt=%d mode=%04x cmd=%04x\n", 829 HDEVNAME(hp), blksize, blkcount, mode, command)); 830 831 mutex_enter(&hp->host_mtx); 832 833 /* Alert the user not to remove the card. */ 834 HSET1(hp, SDHC_HOST_CTL, SDHC_LED_ON); 835 836 /* 837 * Start a CPU data transfer. Writing to the high order byte 838 * of the SDHC_COMMAND register triggers the SD command. (1.5) 839 */ 840 HWRITE2(hp, SDHC_TRANSFER_MODE, mode); 841 HWRITE2(hp, SDHC_BLOCK_SIZE, blksize); 842 if (blkcount > 1) 843 HWRITE2(hp, SDHC_BLOCK_COUNT, blkcount); 844 HWRITE4(hp, SDHC_ARGUMENT, cmd->c_arg); 845 HWRITE2(hp, SDHC_COMMAND, command); 846 847 mutex_exit(&hp->host_mtx); 848 849 return 0; 850 } 851 852 static void 853 sdhc_transfer_data(struct sdhc_host *hp, struct sdmmc_command *cmd) 854 { 855 int error; 856 857 DPRINTF(1,("%s: data transfer: resp=%08x datalen=%u\n", HDEVNAME(hp), 858 MMC_R1(cmd->c_resp), cmd->c_datalen)); 859 860 #ifdef SDHC_DEBUG 861 /* XXX I forgot why I wanted to know when this happens :-( */ 862 if ((cmd->c_opcode == 52 || cmd->c_opcode == 53) && 863 ISSET(MMC_R1(cmd->c_resp), 0xcb00)) { 864 aprint_error_dev(hp->sc->sc_dev, 865 "CMD52/53 error response flags %#x\n", 866 MMC_R1(cmd->c_resp) & 0xff00); 867 } 868 #endif 869 870 error = sdhc_transfer_data_pio(hp, cmd); 871 if (error) 872 cmd->c_error = error; 873 SET(cmd->c_flags, SCF_ITSDONE); 874 875 DPRINTF(1,("%s: data transfer done (error=%d)\n", 876 HDEVNAME(hp), cmd->c_error)); 877 } 878 879 static int 880 sdhc_transfer_data_pio(struct sdhc_host *hp, struct sdmmc_command *cmd) 881 { 882 uint8_t *data = cmd->c_data; 883 int len, datalen; 884 int mask; 885 int error = 0; 886 887 mask = ISSET(cmd->c_flags, SCF_CMD_READ) ? 888 SDHC_BUFFER_READ_ENABLE : SDHC_BUFFER_WRITE_ENABLE; 889 datalen = cmd->c_datalen; 890 891 while (datalen > 0) { 892 if (!sdhc_wait_intr(hp, 893 SDHC_BUFFER_READ_READY|SDHC_BUFFER_WRITE_READY, 894 SDHC_BUFFER_TIMEOUT)) { 895 error = ETIMEDOUT; 896 break; 897 } 898 899 error = sdhc_wait_state(hp, mask, mask); 900 if (error) 901 break; 902 903 len = MIN(datalen, cmd->c_blklen); 904 if (ISSET(cmd->c_flags, SCF_CMD_READ)) 905 sdhc_read_data_pio(hp, data, len); 906 else 907 sdhc_write_data_pio(hp, data, len); 908 909 data += len; 910 datalen -= len; 911 } 912 913 if (error == 0 && !sdhc_wait_intr(hp, SDHC_TRANSFER_COMPLETE, 914 SDHC_TRANSFER_TIMEOUT)) 915 error = ETIMEDOUT; 916 917 return error; 918 } 919 920 static void 921 sdhc_read_data_pio(struct sdhc_host *hp, uint8_t *data, int datalen) 922 { 923 924 if (((__uintptr_t)data & 3) == 0) { 925 while (datalen > 3) { 926 *(uint32_t *)data = HREAD4(hp, SDHC_DATA); 927 data += 4; 928 datalen -= 4; 929 } 930 if (datalen > 1) { 931 *(uint16_t *)data = HREAD2(hp, SDHC_DATA); 932 data += 2; 933 datalen -= 2; 934 } 935 if (datalen > 0) { 936 *data = HREAD1(hp, SDHC_DATA); 937 data += 1; 938 datalen -= 1; 939 } 940 } else if (((__uintptr_t)data & 1) == 0) { 941 while (datalen > 1) { 942 *(uint16_t *)data = HREAD2(hp, SDHC_DATA); 943 data += 2; 944 datalen -= 2; 945 } 946 if (datalen > 0) { 947 *data = HREAD1(hp, SDHC_DATA); 948 data += 1; 949 datalen -= 1; 950 } 951 } else { 952 while (datalen > 0) { 953 *data = HREAD1(hp, SDHC_DATA); 954 data += 1; 955 datalen -= 1; 956 } 957 } 958 } 959 960 static void 961 sdhc_write_data_pio(struct sdhc_host *hp, uint8_t *data, int datalen) 962 { 963 964 if (((__uintptr_t)data & 3) == 0) { 965 while (datalen > 3) { 966 HWRITE4(hp, SDHC_DATA, *(uint32_t *)data); 967 data += 4; 968 datalen -= 4; 969 } 970 if (datalen > 1) { 971 HWRITE2(hp, SDHC_DATA, *(uint16_t *)data); 972 data += 2; 973 datalen -= 2; 974 } 975 if (datalen > 0) { 976 HWRITE1(hp, SDHC_DATA, *data); 977 data += 1; 978 datalen -= 1; 979 } 980 } else if (((__uintptr_t)data & 1) == 0) { 981 while (datalen > 1) { 982 HWRITE2(hp, SDHC_DATA, *(uint16_t *)data); 983 data += 2; 984 datalen -= 2; 985 } 986 if (datalen > 0) { 987 HWRITE1(hp, SDHC_DATA, *data); 988 data += 1; 989 datalen -= 1; 990 } 991 } else { 992 while (datalen > 0) { 993 HWRITE1(hp, SDHC_DATA, *data); 994 data += 1; 995 datalen -= 1; 996 } 997 } 998 } 999 1000 /* Prepare for another command. */ 1001 static int 1002 sdhc_soft_reset(struct sdhc_host *hp, int mask) 1003 { 1004 int timo; 1005 1006 DPRINTF(1,("%s: software reset reg=%08x\n", HDEVNAME(hp), mask)); 1007 1008 HWRITE1(hp, SDHC_SOFTWARE_RESET, mask); 1009 for (timo = 10; timo > 0; timo--) { 1010 if (!ISSET(HREAD1(hp, SDHC_SOFTWARE_RESET), mask)) 1011 break; 1012 sdmmc_delay(10000); 1013 HWRITE1(hp, SDHC_SOFTWARE_RESET, 0); 1014 } 1015 if (timo == 0) { 1016 DPRINTF(1,("%s: timeout reg=%08x\n", HDEVNAME(hp), 1017 HREAD1(hp, SDHC_SOFTWARE_RESET))); 1018 HWRITE1(hp, SDHC_SOFTWARE_RESET, 0); 1019 return ETIMEDOUT; 1020 } 1021 1022 return 0; 1023 } 1024 1025 static int 1026 sdhc_wait_intr(struct sdhc_host *hp, int mask, int timo) 1027 { 1028 int status; 1029 1030 mask |= SDHC_ERROR_INTERRUPT; 1031 1032 mutex_enter(&hp->intr_mtx); 1033 status = hp->intr_status & mask; 1034 while (status == 0) { 1035 if (cv_timedwait(&hp->intr_cv, &hp->intr_mtx, timo) 1036 == EWOULDBLOCK) { 1037 status |= SDHC_ERROR_INTERRUPT; 1038 break; 1039 } 1040 status = hp->intr_status & mask; 1041 } 1042 hp->intr_status &= ~status; 1043 1044 DPRINTF(2,("%s: intr status %#x error %#x\n", HDEVNAME(hp), status, 1045 hp->intr_error_status)); 1046 1047 /* Command timeout has higher priority than command complete. */ 1048 if (ISSET(status, SDHC_ERROR_INTERRUPT)) { 1049 hp->intr_error_status = 0; 1050 (void)sdhc_soft_reset(hp, SDHC_RESET_DAT|SDHC_RESET_CMD); 1051 status = 0; 1052 } 1053 mutex_exit(&hp->intr_mtx); 1054 1055 return status; 1056 } 1057 1058 /* 1059 * Established by attachment driver at interrupt priority IPL_SDMMC. 1060 */ 1061 int 1062 sdhc_intr(void *arg) 1063 { 1064 struct sdhc_softc *sc = (struct sdhc_softc *)arg; 1065 struct sdhc_host *hp; 1066 int host; 1067 int done = 0; 1068 uint16_t status; 1069 uint16_t error; 1070 1071 /* We got an interrupt, but we don't know from which slot. */ 1072 for (host = 0; host < sc->sc_nhosts; host++) { 1073 hp = sc->sc_host[host]; 1074 if (hp == NULL) 1075 continue; 1076 1077 /* Find out which interrupts are pending. */ 1078 status = HREAD2(hp, SDHC_NINTR_STATUS); 1079 if (!ISSET(status, SDHC_NINTR_STATUS_MASK)) 1080 continue; /* no interrupt for us */ 1081 1082 /* Acknowledge the interrupts we are about to handle. */ 1083 HWRITE2(hp, SDHC_NINTR_STATUS, status); 1084 DPRINTF(2,("%s: interrupt status=%x\n", HDEVNAME(hp), 1085 status)); 1086 1087 if (!ISSET(status, SDHC_NINTR_STATUS_MASK)) 1088 continue; 1089 1090 /* Claim this interrupt. */ 1091 done = 1; 1092 1093 /* 1094 * Service error interrupts. 1095 */ 1096 if (ISSET(status, SDHC_ERROR_INTERRUPT)) { 1097 /* Acknowledge error interrupts. */ 1098 error = HREAD2(hp, SDHC_EINTR_STATUS); 1099 HWRITE2(hp, SDHC_EINTR_STATUS, error); 1100 DPRINTF(2,("%s: error interrupt, status=%x\n", 1101 HDEVNAME(hp), error)); 1102 1103 if (ISSET(error, SDHC_CMD_TIMEOUT_ERROR| 1104 SDHC_DATA_TIMEOUT_ERROR)) { 1105 hp->intr_error_status |= error; 1106 hp->intr_status |= status; 1107 cv_broadcast(&hp->intr_cv); 1108 } 1109 } 1110 1111 /* 1112 * Wake up the sdmmc event thread to scan for cards. 1113 */ 1114 if (ISSET(status, SDHC_CARD_REMOVAL|SDHC_CARD_INSERTION)) 1115 sdmmc_needs_discover(hp->sdmmc); 1116 1117 /* 1118 * Wake up the blocking process to service command 1119 * related interrupt(s). 1120 */ 1121 if (ISSET(status, SDHC_BUFFER_READ_READY| 1122 SDHC_BUFFER_WRITE_READY|SDHC_COMMAND_COMPLETE| 1123 SDHC_TRANSFER_COMPLETE|SDHC_DMA_INTERRUPT)) { 1124 hp->intr_status |= status; 1125 cv_broadcast(&hp->intr_cv); 1126 } 1127 1128 /* 1129 * Service SD card interrupts. 1130 */ 1131 if (ISSET(status, SDHC_CARD_INTERRUPT)) { 1132 DPRINTF(0,("%s: card interrupt\n", HDEVNAME(hp))); 1133 HCLR2(hp, SDHC_NINTR_STATUS_EN, SDHC_CARD_INTERRUPT); 1134 sdmmc_card_intr(hp->sdmmc); 1135 } 1136 } 1137 1138 return done; 1139 } 1140 1141 #ifdef SDHC_DEBUG 1142 void 1143 sdhc_dump_regs(struct sdhc_host *hp) 1144 { 1145 1146 printf("0x%02x PRESENT_STATE: %x\n", SDHC_PRESENT_STATE, 1147 HREAD4(hp, SDHC_PRESENT_STATE)); 1148 printf("0x%02x POWER_CTL: %x\n", SDHC_POWER_CTL, 1149 HREAD1(hp, SDHC_POWER_CTL)); 1150 printf("0x%02x NINTR_STATUS: %x\n", SDHC_NINTR_STATUS, 1151 HREAD2(hp, SDHC_NINTR_STATUS)); 1152 printf("0x%02x EINTR_STATUS: %x\n", SDHC_EINTR_STATUS, 1153 HREAD2(hp, SDHC_EINTR_STATUS)); 1154 printf("0x%02x NINTR_STATUS_EN: %x\n", SDHC_NINTR_STATUS_EN, 1155 HREAD2(hp, SDHC_NINTR_STATUS_EN)); 1156 printf("0x%02x EINTR_STATUS_EN: %x\n", SDHC_EINTR_STATUS_EN, 1157 HREAD2(hp, SDHC_EINTR_STATUS_EN)); 1158 printf("0x%02x NINTR_SIGNAL_EN: %x\n", SDHC_NINTR_SIGNAL_EN, 1159 HREAD2(hp, SDHC_NINTR_SIGNAL_EN)); 1160 printf("0x%02x EINTR_SIGNAL_EN: %x\n", SDHC_EINTR_SIGNAL_EN, 1161 HREAD2(hp, SDHC_EINTR_SIGNAL_EN)); 1162 printf("0x%02x CAPABILITIES: %x\n", SDHC_CAPABILITIES, 1163 HREAD4(hp, SDHC_CAPABILITIES)); 1164 printf("0x%02x MAX_CAPABILITIES: %x\n", SDHC_MAX_CAPABILITIES, 1165 HREAD4(hp, SDHC_MAX_CAPABILITIES)); 1166 } 1167 #endif 1168