1 /*- 2 * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org> 3 * Copyright (c) 2017 Marius Strobl <marius@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/callout.h> 34 #include <sys/conf.h> 35 #include <sys/kernel.h> 36 #include <sys/kobj.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/module.h> 40 #include <sys/mutex.h> 41 #include <sys/resource.h> 42 #include <sys/rman.h> 43 #include <sys/sysctl.h> 44 #include <sys/taskqueue.h> 45 46 #include <machine/bus.h> 47 #include <machine/resource.h> 48 #include <machine/stdarg.h> 49 50 #include <dev/mmc/bridge.h> 51 #include <dev/mmc/mmcreg.h> 52 #include <dev/mmc/mmcbrvar.h> 53 54 #include <dev/sdhci/sdhci.h> 55 56 #include <cam/cam.h> 57 #include <cam/cam_ccb.h> 58 #include <cam/cam_debug.h> 59 #include <cam/cam_sim.h> 60 #include <cam/cam_xpt_sim.h> 61 62 #include "mmcbr_if.h" 63 #include "sdhci_if.h" 64 65 #include "opt_mmccam.h" 66 67 SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver"); 68 69 static int sdhci_debug = 0; 70 SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, 71 "Debug level"); 72 u_int sdhci_quirk_clear = 0; 73 SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_clear, CTLFLAG_RWTUN, &sdhci_quirk_clear, 74 0, "Mask of quirks to clear"); 75 u_int sdhci_quirk_set = 0; 76 SYSCTL_INT(_hw_sdhci, OID_AUTO, quirk_set, CTLFLAG_RWTUN, &sdhci_quirk_set, 0, 77 "Mask of quirks to set"); 78 79 #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off)) 80 #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off)) 81 #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off)) 82 #define RD_MULTI_4(slot, off, ptr, count) \ 83 SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 84 85 #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val)) 86 #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val)) 87 #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val)) 88 #define WR_MULTI_4(slot, off, ptr, count) \ 89 SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 90 91 static void sdhci_card_poll(void *arg); 92 static void sdhci_card_task(void *arg, int pending); 93 static int sdhci_exec_tuning(struct sdhci_slot *slot, bool reset); 94 static void sdhci_req_wakeup(struct mmc_request *req); 95 static void sdhci_retune(void *arg); 96 static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 97 static void sdhci_start(struct sdhci_slot *slot); 98 static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data); 99 100 #ifdef MMCCAM 101 /* CAM-related */ 102 int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock); 103 static int sdhci_cam_update_ios(struct sdhci_slot *slot); 104 static int sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb); 105 static void sdhci_cam_action(struct cam_sim *sim, union ccb *ccb); 106 static void sdhci_cam_poll(struct cam_sim *sim); 107 static int sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb); 108 #endif 109 110 /* helper routines */ 111 static void sdhci_dumpregs(struct sdhci_slot *slot); 112 static int slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 113 __printflike(2, 3); 114 static uint32_t sdhci_tuning_intmask(struct sdhci_slot *slot); 115 116 #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 117 #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 118 #define SDHCI_LOCK_INIT(_slot) \ 119 mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 120 #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 121 #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 122 #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 123 124 #define SDHCI_DEFAULT_MAX_FREQ 50 125 126 #define SDHCI_200_MAX_DIVIDER 256 127 #define SDHCI_300_MAX_DIVIDER 2046 128 129 #define SDHCI_CARD_PRESENT_TICKS (hz / 5) 130 #define SDHCI_INSERT_DELAY_TICKS (hz / 2) 131 132 /* 133 * Broadcom BCM577xx Controller Constants 134 */ 135 /* Maximum divider supported by the default clock source. */ 136 #define BCM577XX_DEFAULT_MAX_DIVIDER 256 137 /* Alternative clock's base frequency. */ 138 #define BCM577XX_ALT_CLOCK_BASE 63000000 139 140 #define BCM577XX_HOST_CONTROL 0x198 141 #define BCM577XX_CTRL_CLKSEL_MASK 0xFFFFCFFF 142 #define BCM577XX_CTRL_CLKSEL_SHIFT 12 143 #define BCM577XX_CTRL_CLKSEL_DEFAULT 0x0 144 #define BCM577XX_CTRL_CLKSEL_64MHZ 0x3 145 146 static void 147 sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 148 { 149 150 if (error != 0) { 151 printf("getaddr: error %d\n", error); 152 return; 153 } 154 *(bus_addr_t *)arg = segs[0].ds_addr; 155 } 156 157 static int 158 slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 159 { 160 va_list ap; 161 int retval; 162 163 retval = printf("%s-slot%d: ", 164 device_get_nameunit(slot->bus), slot->num); 165 166 va_start(ap, fmt); 167 retval += vprintf(fmt, ap); 168 va_end(ap); 169 return (retval); 170 } 171 172 static void 173 sdhci_dumpregs(struct sdhci_slot *slot) 174 { 175 176 slot_printf(slot, 177 "============== REGISTER DUMP ==============\n"); 178 179 slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n", 180 RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 181 slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 182 RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 183 slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n", 184 RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 185 slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n", 186 RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 187 slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n", 188 RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 189 slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n", 190 RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 191 slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n", 192 RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 193 slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 194 RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 195 slot_printf(slot, "AC12 err: 0x%08x | Host ctl2:0x%08x\n", 196 RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_HOST_CONTROL2)); 197 slot_printf(slot, "Caps: 0x%08x | Caps2: 0x%08x\n", 198 RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_CAPABILITIES2)); 199 slot_printf(slot, "Max curr: 0x%08x | ADMA err: 0x%08x\n", 200 RD4(slot, SDHCI_MAX_CURRENT), RD1(slot, SDHCI_ADMA_ERR)); 201 slot_printf(slot, "ADMA addr:0x%08x | Slot int: 0x%08x\n", 202 RD4(slot, SDHCI_ADMA_ADDRESS_LO), RD2(slot, SDHCI_SLOT_INT_STATUS)); 203 204 slot_printf(slot, 205 "===========================================\n"); 206 } 207 208 static void 209 sdhci_reset(struct sdhci_slot *slot, uint8_t mask) 210 { 211 int timeout; 212 uint32_t clock; 213 214 if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 215 if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot)) 216 return; 217 } 218 219 /* Some controllers need this kick or reset won't work. */ 220 if ((mask & SDHCI_RESET_ALL) == 0 && 221 (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 222 /* This is to force an update */ 223 clock = slot->clock; 224 slot->clock = 0; 225 sdhci_set_clock(slot, clock); 226 } 227 228 if (mask & SDHCI_RESET_ALL) { 229 slot->clock = 0; 230 slot->power = 0; 231 } 232 233 WR1(slot, SDHCI_SOFTWARE_RESET, mask); 234 235 if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 236 /* 237 * Resets on TI OMAPs and AM335x are incompatible with SDHCI 238 * specification. The reset bit has internal propagation delay, 239 * so a fast read after write returns 0 even if reset process is 240 * in progress. The workaround is to poll for 1 before polling 241 * for 0. In the worst case, if we miss seeing it asserted the 242 * time we spent waiting is enough to ensure the reset finishes. 243 */ 244 timeout = 10000; 245 while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 246 if (timeout <= 0) 247 break; 248 timeout--; 249 DELAY(1); 250 } 251 } 252 253 /* Wait max 100 ms */ 254 timeout = 10000; 255 /* Controller clears the bits when it's done */ 256 while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 257 if (timeout <= 0) { 258 slot_printf(slot, "Reset 0x%x never completed.\n", 259 mask); 260 sdhci_dumpregs(slot); 261 return; 262 } 263 timeout--; 264 DELAY(10); 265 } 266 } 267 268 static uint32_t 269 sdhci_tuning_intmask(struct sdhci_slot *slot) 270 { 271 uint32_t intmask; 272 273 intmask = 0; 274 if (slot->opt & SDHCI_TUNING_SUPPORTED) { 275 intmask |= SDHCI_INT_TUNEERR; 276 if (slot->retune_mode == SDHCI_RETUNE_MODE_2 || 277 slot->retune_mode == SDHCI_RETUNE_MODE_3) 278 intmask |= SDHCI_INT_RETUNE; 279 } 280 return (intmask); 281 } 282 283 static void 284 sdhci_init(struct sdhci_slot *slot) 285 { 286 287 sdhci_reset(slot, SDHCI_RESET_ALL); 288 289 /* Enable interrupts. */ 290 slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 291 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 292 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 293 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 294 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 295 SDHCI_INT_ACMD12ERR; 296 297 if (!(slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 298 !(slot->opt & SDHCI_NON_REMOVABLE)) { 299 slot->intmask |= SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT; 300 } 301 302 WR4(slot, SDHCI_INT_ENABLE, slot->intmask | sdhci_tuning_intmask(slot)); 303 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 304 } 305 306 static void 307 sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 308 { 309 uint32_t clk_base; 310 uint32_t clk_sel; 311 uint32_t res; 312 uint16_t clk; 313 uint16_t div; 314 int timeout; 315 316 if (clock == slot->clock) 317 return; 318 slot->clock = clock; 319 320 /* Turn off the clock. */ 321 clk = RD2(slot, SDHCI_CLOCK_CONTROL); 322 WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 323 /* If no clock requested - leave it so. */ 324 if (clock == 0) 325 return; 326 327 /* Determine the clock base frequency */ 328 clk_base = slot->max_clk; 329 if (slot->quirks & SDHCI_QUIRK_BCM577XX_400KHZ_CLKSRC) { 330 clk_sel = RD2(slot, BCM577XX_HOST_CONTROL) & 331 BCM577XX_CTRL_CLKSEL_MASK; 332 333 /* 334 * Select clock source appropriate for the requested frequency. 335 */ 336 if ((clk_base / BCM577XX_DEFAULT_MAX_DIVIDER) > clock) { 337 clk_base = BCM577XX_ALT_CLOCK_BASE; 338 clk_sel |= (BCM577XX_CTRL_CLKSEL_64MHZ << 339 BCM577XX_CTRL_CLKSEL_SHIFT); 340 } else { 341 clk_sel |= (BCM577XX_CTRL_CLKSEL_DEFAULT << 342 BCM577XX_CTRL_CLKSEL_SHIFT); 343 } 344 345 WR2(slot, BCM577XX_HOST_CONTROL, clk_sel); 346 } 347 348 /* Recalculate timeout clock frequency based on the new sd clock. */ 349 if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 350 slot->timeout_clk = slot->clock / 1000; 351 352 if (slot->version < SDHCI_SPEC_300) { 353 /* Looking for highest freq <= clock. */ 354 res = clk_base; 355 for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 356 if (res <= clock) 357 break; 358 res >>= 1; 359 } 360 /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 361 div >>= 1; 362 } else { 363 /* Version 3.0 divisors are multiples of two up to 1023 * 2 */ 364 if (clock >= clk_base) 365 div = 0; 366 else { 367 for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 368 if ((clk_base / div) <= clock) 369 break; 370 } 371 } 372 div >>= 1; 373 } 374 375 if (bootverbose || sdhci_debug) 376 slot_printf(slot, "Divider %d for freq %d (base %d)\n", 377 div, clock, clk_base); 378 379 /* Now we have got divider, set it. */ 380 clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 381 clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 382 << SDHCI_DIVIDER_HI_SHIFT; 383 384 WR2(slot, SDHCI_CLOCK_CONTROL, clk); 385 /* Enable clock. */ 386 clk |= SDHCI_CLOCK_INT_EN; 387 WR2(slot, SDHCI_CLOCK_CONTROL, clk); 388 /* Wait up to 10 ms until it stabilize. */ 389 timeout = 10; 390 while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 391 & SDHCI_CLOCK_INT_STABLE)) { 392 if (timeout == 0) { 393 slot_printf(slot, 394 "Internal clock never stabilised.\n"); 395 sdhci_dumpregs(slot); 396 return; 397 } 398 timeout--; 399 DELAY(1000); 400 } 401 /* Pass clock signal to the bus. */ 402 clk |= SDHCI_CLOCK_CARD_EN; 403 WR2(slot, SDHCI_CLOCK_CONTROL, clk); 404 } 405 406 static void 407 sdhci_set_power(struct sdhci_slot *slot, u_char power) 408 { 409 int i; 410 uint8_t pwr; 411 412 if (slot->power == power) 413 return; 414 415 slot->power = power; 416 417 /* Turn off the power. */ 418 pwr = 0; 419 WR1(slot, SDHCI_POWER_CONTROL, pwr); 420 /* If power down requested - leave it so. */ 421 if (power == 0) 422 return; 423 /* Set voltage. */ 424 switch (1 << power) { 425 case MMC_OCR_LOW_VOLTAGE: 426 pwr |= SDHCI_POWER_180; 427 break; 428 case MMC_OCR_290_300: 429 case MMC_OCR_300_310: 430 pwr |= SDHCI_POWER_300; 431 break; 432 case MMC_OCR_320_330: 433 case MMC_OCR_330_340: 434 pwr |= SDHCI_POWER_330; 435 break; 436 } 437 WR1(slot, SDHCI_POWER_CONTROL, pwr); 438 /* 439 * Turn on VDD1 power. Note that at least some Intel controllers can 440 * fail to enable bus power on the first try after transiting from D3 441 * to D0, so we give them up to 2 ms. 442 */ 443 pwr |= SDHCI_POWER_ON; 444 for (i = 0; i < 20; i++) { 445 WR1(slot, SDHCI_POWER_CONTROL, pwr); 446 if (RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON) 447 break; 448 DELAY(100); 449 } 450 if (!(RD1(slot, SDHCI_POWER_CONTROL) & SDHCI_POWER_ON)) 451 slot_printf(slot, "Bus power failed to enable"); 452 453 if (slot->quirks & SDHCI_QUIRK_INTEL_POWER_UP_RESET) { 454 WR1(slot, SDHCI_POWER_CONTROL, pwr | 0x10); 455 DELAY(10); 456 WR1(slot, SDHCI_POWER_CONTROL, pwr); 457 DELAY(300); 458 } 459 } 460 461 static void 462 sdhci_read_block_pio(struct sdhci_slot *slot) 463 { 464 uint32_t data; 465 char *buffer; 466 size_t left; 467 468 buffer = slot->curcmd->data->data; 469 buffer += slot->offset; 470 /* Transfer one block at a time. */ 471 left = min(512, slot->curcmd->data->len - slot->offset); 472 slot->offset += left; 473 474 /* If we are too fast, broken controllers return zeroes. */ 475 if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 476 DELAY(10); 477 /* Handle unaligned and aligned buffer cases. */ 478 if ((intptr_t)buffer & 3) { 479 while (left > 3) { 480 data = RD4(slot, SDHCI_BUFFER); 481 buffer[0] = data; 482 buffer[1] = (data >> 8); 483 buffer[2] = (data >> 16); 484 buffer[3] = (data >> 24); 485 buffer += 4; 486 left -= 4; 487 } 488 } else { 489 RD_MULTI_4(slot, SDHCI_BUFFER, 490 (uint32_t *)buffer, left >> 2); 491 left &= 3; 492 } 493 /* Handle uneven size case. */ 494 if (left > 0) { 495 data = RD4(slot, SDHCI_BUFFER); 496 while (left > 0) { 497 *(buffer++) = data; 498 data >>= 8; 499 left--; 500 } 501 } 502 } 503 504 static void 505 sdhci_write_block_pio(struct sdhci_slot *slot) 506 { 507 uint32_t data = 0; 508 char *buffer; 509 size_t left; 510 511 buffer = slot->curcmd->data->data; 512 buffer += slot->offset; 513 /* Transfer one block at a time. */ 514 left = min(512, slot->curcmd->data->len - slot->offset); 515 slot->offset += left; 516 517 /* Handle unaligned and aligned buffer cases. */ 518 if ((intptr_t)buffer & 3) { 519 while (left > 3) { 520 data = buffer[0] + 521 (buffer[1] << 8) + 522 (buffer[2] << 16) + 523 (buffer[3] << 24); 524 left -= 4; 525 buffer += 4; 526 WR4(slot, SDHCI_BUFFER, data); 527 } 528 } else { 529 WR_MULTI_4(slot, SDHCI_BUFFER, 530 (uint32_t *)buffer, left >> 2); 531 left &= 3; 532 } 533 /* Handle uneven size case. */ 534 if (left > 0) { 535 while (left > 0) { 536 data <<= 8; 537 data += *(buffer++); 538 left--; 539 } 540 WR4(slot, SDHCI_BUFFER, data); 541 } 542 } 543 544 static void 545 sdhci_transfer_pio(struct sdhci_slot *slot) 546 { 547 548 /* Read as many blocks as possible. */ 549 if (slot->curcmd->data->flags & MMC_DATA_READ) { 550 while (RD4(slot, SDHCI_PRESENT_STATE) & 551 SDHCI_DATA_AVAILABLE) { 552 sdhci_read_block_pio(slot); 553 if (slot->offset >= slot->curcmd->data->len) 554 break; 555 } 556 } else { 557 while (RD4(slot, SDHCI_PRESENT_STATE) & 558 SDHCI_SPACE_AVAILABLE) { 559 sdhci_write_block_pio(slot); 560 if (slot->offset >= slot->curcmd->data->len) 561 break; 562 } 563 } 564 } 565 566 static void 567 sdhci_card_task(void *arg, int pending __unused) 568 { 569 struct sdhci_slot *slot = arg; 570 device_t d; 571 572 SDHCI_LOCK(slot); 573 if (SDHCI_GET_CARD_PRESENT(slot->bus, slot)) { 574 #ifdef MMCCAM 575 if (slot->card_present == 0) { 576 #else 577 if (slot->dev == NULL) { 578 #endif 579 /* If card is present - attach mmc bus. */ 580 if (bootverbose || sdhci_debug) 581 slot_printf(slot, "Card inserted\n"); 582 #ifdef MMCCAM 583 slot->card_present = 1; 584 union ccb *ccb; 585 uint32_t pathid; 586 pathid = cam_sim_path(slot->sim); 587 ccb = xpt_alloc_ccb_nowait(); 588 if (ccb == NULL) { 589 slot_printf(slot, "Unable to alloc CCB for rescan\n"); 590 SDHCI_UNLOCK(slot); 591 return; 592 } 593 594 /* 595 * We create a rescan request for BUS:0:0, since the card 596 * will be at lun 0. 597 */ 598 if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 599 /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) { 600 slot_printf(slot, "Unable to create path for rescan\n"); 601 SDHCI_UNLOCK(slot); 602 xpt_free_ccb(ccb); 603 return; 604 } 605 SDHCI_UNLOCK(slot); 606 xpt_rescan(ccb); 607 #else 608 d = slot->dev = device_add_child(slot->bus, "mmc", -1); 609 SDHCI_UNLOCK(slot); 610 if (d) { 611 device_set_ivars(d, slot); 612 (void)device_probe_and_attach(d); 613 } 614 #endif 615 } else 616 SDHCI_UNLOCK(slot); 617 } else { 618 #ifdef MMCCAM 619 if (slot->card_present == 1) { 620 #else 621 if (slot->dev != NULL) { 622 #endif 623 /* If no card present - detach mmc bus. */ 624 if (bootverbose || sdhci_debug) 625 slot_printf(slot, "Card removed\n"); 626 d = slot->dev; 627 slot->dev = NULL; 628 #ifdef MMCCAM 629 slot->card_present = 0; 630 union ccb *ccb; 631 uint32_t pathid; 632 pathid = cam_sim_path(slot->sim); 633 ccb = xpt_alloc_ccb_nowait(); 634 if (ccb == NULL) { 635 slot_printf(slot, "Unable to alloc CCB for rescan\n"); 636 SDHCI_UNLOCK(slot); 637 return; 638 } 639 640 /* 641 * We create a rescan request for BUS:0:0, since the card 642 * will be at lun 0. 643 */ 644 if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, 645 /* target */ 0, /* lun */ 0) != CAM_REQ_CMP) { 646 slot_printf(slot, "Unable to create path for rescan\n"); 647 SDHCI_UNLOCK(slot); 648 xpt_free_ccb(ccb); 649 return; 650 } 651 SDHCI_UNLOCK(slot); 652 xpt_rescan(ccb); 653 #else 654 slot->intmask &= ~sdhci_tuning_intmask(slot); 655 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 656 slot->opt &= ~SDHCI_TUNING_ENABLED; 657 SDHCI_UNLOCK(slot); 658 callout_drain(&slot->retune_callout); 659 device_delete_child(slot->bus, d); 660 #endif 661 } else 662 SDHCI_UNLOCK(slot); 663 } 664 } 665 666 static void 667 sdhci_handle_card_present_locked(struct sdhci_slot *slot, bool is_present) 668 { 669 bool was_present; 670 671 /* 672 * If there was no card and now there is one, schedule the task to 673 * create the child device after a short delay. The delay is to 674 * debounce the card insert (sometimes the card detect pin stabilizes 675 * before the other pins have made good contact). 676 * 677 * If there was a card present and now it's gone, immediately schedule 678 * the task to delete the child device. No debouncing -- gone is gone, 679 * because once power is removed, a full card re-init is needed, and 680 * that happens by deleting and recreating the child device. 681 */ 682 #ifdef MMCCAM 683 was_present = slot->card_present; 684 #else 685 was_present = slot->dev != NULL; 686 #endif 687 if (!was_present && is_present) { 688 taskqueue_enqueue_timeout(taskqueue_swi_giant, 689 &slot->card_delayed_task, -SDHCI_INSERT_DELAY_TICKS); 690 } else if (was_present && !is_present) { 691 taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 692 } 693 } 694 695 void 696 sdhci_handle_card_present(struct sdhci_slot *slot, bool is_present) 697 { 698 699 SDHCI_LOCK(slot); 700 sdhci_handle_card_present_locked(slot, is_present); 701 SDHCI_UNLOCK(slot); 702 } 703 704 static void 705 sdhci_card_poll(void *arg) 706 { 707 struct sdhci_slot *slot = arg; 708 709 sdhci_handle_card_present(slot, 710 SDHCI_GET_CARD_PRESENT(slot->bus, slot)); 711 callout_reset(&slot->card_poll_callout, SDHCI_CARD_PRESENT_TICKS, 712 sdhci_card_poll, slot); 713 } 714 715 int 716 sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 717 { 718 kobjop_desc_t kobj_desc; 719 kobj_method_t *kobj_method; 720 uint32_t caps, caps2, freq, host_caps; 721 int err; 722 723 SDHCI_LOCK_INIT(slot); 724 725 slot->num = num; 726 slot->bus = dev; 727 728 /* Allocate DMA tag. */ 729 err = bus_dma_tag_create(bus_get_dma_tag(dev), 730 DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 731 BUS_SPACE_MAXADDR, NULL, NULL, 732 DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE, 733 BUS_DMA_ALLOCNOW, NULL, NULL, 734 &slot->dmatag); 735 if (err != 0) { 736 device_printf(dev, "Can't create DMA tag\n"); 737 SDHCI_LOCK_DESTROY(slot); 738 return (err); 739 } 740 /* Allocate DMA memory. */ 741 err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 742 BUS_DMA_NOWAIT, &slot->dmamap); 743 if (err != 0) { 744 device_printf(dev, "Can't alloc DMA memory\n"); 745 SDHCI_LOCK_DESTROY(slot); 746 return (err); 747 } 748 /* Map the memory. */ 749 err = bus_dmamap_load(slot->dmatag, slot->dmamap, 750 (void *)slot->dmamem, DMA_BLOCK_SIZE, 751 sdhci_getaddr, &slot->paddr, 0); 752 if (err != 0 || slot->paddr == 0) { 753 device_printf(dev, "Can't load DMA memory\n"); 754 SDHCI_LOCK_DESTROY(slot); 755 if (err) 756 return (err); 757 else 758 return (EFAULT); 759 } 760 761 slot->version = (RD2(slot, SDHCI_HOST_VERSION) 762 >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 763 if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) { 764 caps = slot->caps; 765 caps2 = slot->caps2; 766 } else { 767 caps = RD4(slot, SDHCI_CAPABILITIES); 768 if (slot->version >= SDHCI_SPEC_300) 769 caps2 = RD4(slot, SDHCI_CAPABILITIES2); 770 else 771 caps2 = 0; 772 } 773 /* Calculate base clock frequency. */ 774 if (slot->version >= SDHCI_SPEC_300) 775 freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 776 SDHCI_CLOCK_BASE_SHIFT; 777 else 778 freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 779 SDHCI_CLOCK_BASE_SHIFT; 780 if (freq != 0) 781 slot->max_clk = freq * 1000000; 782 /* 783 * If the frequency wasn't in the capabilities and the hardware driver 784 * hasn't already set max_clk we're probably not going to work right 785 * with an assumption, so complain about it. 786 */ 787 if (slot->max_clk == 0) { 788 slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 789 device_printf(dev, "Hardware doesn't specify base clock " 790 "frequency, using %dMHz as default.\n", 791 SDHCI_DEFAULT_MAX_FREQ); 792 } 793 /* Calculate/set timeout clock frequency. */ 794 if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 795 slot->timeout_clk = slot->max_clk / 1000; 796 } else if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_1MHZ) { 797 slot->timeout_clk = 1000; 798 } else { 799 slot->timeout_clk = (caps & SDHCI_TIMEOUT_CLK_MASK) >> 800 SDHCI_TIMEOUT_CLK_SHIFT; 801 if (caps & SDHCI_TIMEOUT_CLK_UNIT) 802 slot->timeout_clk *= 1000; 803 } 804 /* 805 * If the frequency wasn't in the capabilities and the hardware driver 806 * hasn't already set timeout_clk we'll probably work okay using the 807 * max timeout, but still mention it. 808 */ 809 if (slot->timeout_clk == 0) { 810 device_printf(dev, "Hardware doesn't specify timeout clock " 811 "frequency, setting BROKEN_TIMEOUT quirk.\n"); 812 slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 813 } 814 815 slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 816 slot->host.f_max = slot->max_clk; 817 slot->host.host_ocr = 0; 818 if (caps & SDHCI_CAN_VDD_330) 819 slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 820 if (caps & SDHCI_CAN_VDD_300) 821 slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 822 if (caps & SDHCI_CAN_VDD_180) 823 slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 824 if (slot->host.host_ocr == 0) { 825 device_printf(dev, "Hardware doesn't report any " 826 "support voltages.\n"); 827 } 828 829 host_caps = MMC_CAP_4_BIT_DATA; 830 if (caps & SDHCI_CAN_DO_8BITBUS) 831 host_caps |= MMC_CAP_8_BIT_DATA; 832 if (caps & SDHCI_CAN_DO_HISPD) 833 host_caps |= MMC_CAP_HSPEED; 834 if (slot->quirks & SDHCI_QUIRK_BOOT_NOACC) 835 host_caps |= MMC_CAP_BOOT_NOACC; 836 if (slot->quirks & SDHCI_QUIRK_WAIT_WHILE_BUSY) 837 host_caps |= MMC_CAP_WAIT_WHILE_BUSY; 838 839 /* Determine supported UHS-I and eMMC modes. */ 840 if (caps2 & (SDHCI_CAN_SDR50 | SDHCI_CAN_SDR104 | SDHCI_CAN_DDR50)) 841 host_caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25; 842 if (caps2 & SDHCI_CAN_SDR104) { 843 host_caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50; 844 if (!(slot->quirks & SDHCI_QUIRK_BROKEN_MMC_HS200)) 845 host_caps |= MMC_CAP_MMC_HS200; 846 } else if (caps2 & SDHCI_CAN_SDR50) 847 host_caps |= MMC_CAP_UHS_SDR50; 848 if (caps2 & SDHCI_CAN_DDR50 && 849 !(slot->quirks & SDHCI_QUIRK_BROKEN_UHS_DDR50)) 850 host_caps |= MMC_CAP_UHS_DDR50; 851 if (slot->quirks & SDHCI_QUIRK_MMC_DDR52) 852 host_caps |= MMC_CAP_MMC_DDR52; 853 if (slot->quirks & SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 && 854 caps2 & SDHCI_CAN_MMC_HS400) 855 host_caps |= MMC_CAP_MMC_HS400; 856 857 /* 858 * Disable UHS-I and eMMC modes if the set_uhs_timing method is the 859 * default NULL implementation. 860 */ 861 kobj_desc = &sdhci_set_uhs_timing_desc; 862 kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 863 kobj_desc); 864 if (kobj_method == &kobj_desc->deflt) 865 host_caps &= ~(MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 866 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 867 MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | MMC_CAP_MMC_HS400); 868 869 #define SDHCI_CAP_MODES_TUNING(caps2) \ 870 (((caps2) & SDHCI_TUNE_SDR50 ? MMC_CAP_UHS_SDR50 : 0) | \ 871 MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_MMC_HS200 | \ 872 MMC_CAP_MMC_HS400) 873 874 /* 875 * Disable UHS-I and eMMC modes that require (re-)tuning if either 876 * the tune or re-tune method is the default NULL implementation. 877 */ 878 kobj_desc = &mmcbr_tune_desc; 879 kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 880 kobj_desc); 881 if (kobj_method == &kobj_desc->deflt) 882 goto no_tuning; 883 kobj_desc = &mmcbr_retune_desc; 884 kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 885 kobj_desc); 886 if (kobj_method == &kobj_desc->deflt) { 887 no_tuning: 888 host_caps &= ~(SDHCI_CAP_MODES_TUNING(caps2)); 889 } 890 891 /* Allocate tuning structures and determine tuning parameters. */ 892 if (host_caps & SDHCI_CAP_MODES_TUNING(caps2)) { 893 slot->opt |= SDHCI_TUNING_SUPPORTED; 894 slot->tune_req = malloc(sizeof(*slot->tune_req), M_DEVBUF, 895 M_WAITOK); 896 slot->tune_cmd = malloc(sizeof(*slot->tune_cmd), M_DEVBUF, 897 M_WAITOK); 898 slot->tune_data = malloc(sizeof(*slot->tune_data), M_DEVBUF, 899 M_WAITOK); 900 if (caps2 & SDHCI_TUNE_SDR50) 901 slot->opt |= SDHCI_SDR50_NEEDS_TUNING; 902 slot->retune_mode = (caps2 & SDHCI_RETUNE_MODES_MASK) >> 903 SDHCI_RETUNE_MODES_SHIFT; 904 if (slot->retune_mode == SDHCI_RETUNE_MODE_1) { 905 slot->retune_count = (caps2 & SDHCI_RETUNE_CNT_MASK) >> 906 SDHCI_RETUNE_CNT_SHIFT; 907 if (slot->retune_count > 0xb) { 908 device_printf(dev, "Unknown re-tuning count " 909 "%x, using 1 sec\n", slot->retune_count); 910 slot->retune_count = 1; 911 } else if (slot->retune_count != 0) 912 slot->retune_count = 913 1 << (slot->retune_count - 1); 914 } 915 } 916 917 #undef SDHCI_CAP_MODES_TUNING 918 919 /* Determine supported VCCQ signaling levels. */ 920 host_caps |= MMC_CAP_SIGNALING_330; 921 if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 922 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_DDR50 | MMC_CAP_UHS_SDR104 | 923 MMC_CAP_MMC_DDR52_180 | MMC_CAP_MMC_HS200_180 | 924 MMC_CAP_MMC_HS400_180)) 925 host_caps |= MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180; 926 927 /* 928 * Disable 1.2 V and 1.8 V signaling if the switch_vccq method is the 929 * default NULL implementation. Disable 1.2 V support if it's the 930 * generic SDHCI implementation. 931 */ 932 kobj_desc = &mmcbr_switch_vccq_desc; 933 kobj_method = kobj_lookup_method(((kobj_t)dev)->ops->cls, NULL, 934 kobj_desc); 935 if (kobj_method == &kobj_desc->deflt) 936 host_caps &= ~(MMC_CAP_SIGNALING_120 | MMC_CAP_SIGNALING_180); 937 else if (kobj_method->func == (kobjop_t)sdhci_generic_switch_vccq) 938 host_caps &= ~MMC_CAP_SIGNALING_120; 939 940 /* Determine supported driver types (type B is always mandatory). */ 941 if (caps2 & SDHCI_CAN_DRIVE_TYPE_A) 942 host_caps |= MMC_CAP_DRIVER_TYPE_A; 943 if (caps2 & SDHCI_CAN_DRIVE_TYPE_C) 944 host_caps |= MMC_CAP_DRIVER_TYPE_C; 945 if (caps2 & SDHCI_CAN_DRIVE_TYPE_D) 946 host_caps |= MMC_CAP_DRIVER_TYPE_D; 947 slot->host.caps = host_caps; 948 949 /* Decide if we have usable DMA. */ 950 if (caps & SDHCI_CAN_DO_DMA) 951 slot->opt |= SDHCI_HAVE_DMA; 952 953 if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 954 slot->opt &= ~SDHCI_HAVE_DMA; 955 if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 956 slot->opt |= SDHCI_HAVE_DMA; 957 if (slot->quirks & SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE) 958 slot->opt |= SDHCI_NON_REMOVABLE; 959 960 /* 961 * Use platform-provided transfer backend 962 * with PIO as a fallback mechanism 963 */ 964 if (slot->opt & SDHCI_PLATFORM_TRANSFER) 965 slot->opt &= ~SDHCI_HAVE_DMA; 966 967 if (bootverbose || sdhci_debug) { 968 slot_printf(slot, 969 "%uMHz%s %s VDD:%s%s%s VCCQ: 3.3V%s%s DRV: B%s%s%s %s\n", 970 slot->max_clk / 1000000, 971 (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 972 (host_caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 973 ((host_caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 974 (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 975 (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 976 (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "", 977 (host_caps & MMC_CAP_SIGNALING_180) ? " 1.8V" : "", 978 (host_caps & MMC_CAP_SIGNALING_120) ? " 1.2V" : "", 979 (host_caps & MMC_CAP_DRIVER_TYPE_A) ? "A" : "", 980 (host_caps & MMC_CAP_DRIVER_TYPE_C) ? "C" : "", 981 (host_caps & MMC_CAP_DRIVER_TYPE_D) ? "D" : "", 982 (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO"); 983 if (host_caps & (MMC_CAP_MMC_DDR52 | MMC_CAP_MMC_HS200 | 984 MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) 985 slot_printf(slot, "eMMC:%s%s%s%s\n", 986 (host_caps & MMC_CAP_MMC_DDR52) ? " DDR52" : "", 987 (host_caps & MMC_CAP_MMC_HS200) ? " HS200" : "", 988 (host_caps & MMC_CAP_MMC_HS400) ? " HS400" : "", 989 ((host_caps & 990 (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) == 991 (MMC_CAP_MMC_HS400 | MMC_CAP_MMC_ENH_STROBE)) ? 992 " HS400ES" : ""); 993 if (host_caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 994 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104)) 995 slot_printf(slot, "UHS-I:%s%s%s%s%s\n", 996 (host_caps & MMC_CAP_UHS_SDR12) ? " SDR12" : "", 997 (host_caps & MMC_CAP_UHS_SDR25) ? " SDR25" : "", 998 (host_caps & MMC_CAP_UHS_SDR50) ? " SDR50" : "", 999 (host_caps & MMC_CAP_UHS_SDR104) ? " SDR104" : "", 1000 (host_caps & MMC_CAP_UHS_DDR50) ? " DDR50" : ""); 1001 if (slot->opt & SDHCI_TUNING_SUPPORTED) 1002 slot_printf(slot, "Re-tuning count %d secs, mode %d\n", 1003 slot->retune_count, slot->retune_mode + 1); 1004 sdhci_dumpregs(slot); 1005 } 1006 1007 slot->timeout = 10; 1008 SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 1009 SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 1010 "timeout", CTLFLAG_RW, &slot->timeout, 0, 1011 "Maximum timeout for SDHCI transfers (in secs)"); 1012 TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 1013 TIMEOUT_TASK_INIT(taskqueue_swi_giant, &slot->card_delayed_task, 0, 1014 sdhci_card_task, slot); 1015 callout_init(&slot->card_poll_callout, 1); 1016 callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 1017 callout_init_mtx(&slot->retune_callout, &slot->mtx, 0); 1018 1019 if ((slot->quirks & SDHCI_QUIRK_POLL_CARD_PRESENT) && 1020 !(slot->opt & SDHCI_NON_REMOVABLE)) { 1021 callout_reset(&slot->card_poll_callout, 1022 SDHCI_CARD_PRESENT_TICKS, sdhci_card_poll, slot); 1023 } 1024 1025 sdhci_init(slot); 1026 1027 return (0); 1028 } 1029 1030 void 1031 sdhci_start_slot(struct sdhci_slot *slot) 1032 { 1033 1034 sdhci_card_task(slot, 0); 1035 } 1036 1037 int 1038 sdhci_cleanup_slot(struct sdhci_slot *slot) 1039 { 1040 device_t d; 1041 1042 callout_drain(&slot->timeout_callout); 1043 callout_drain(&slot->card_poll_callout); 1044 callout_drain(&slot->retune_callout); 1045 taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 1046 taskqueue_drain_timeout(taskqueue_swi_giant, &slot->card_delayed_task); 1047 1048 SDHCI_LOCK(slot); 1049 d = slot->dev; 1050 slot->dev = NULL; 1051 SDHCI_UNLOCK(slot); 1052 if (d != NULL) 1053 device_delete_child(slot->bus, d); 1054 1055 SDHCI_LOCK(slot); 1056 sdhci_reset(slot, SDHCI_RESET_ALL); 1057 SDHCI_UNLOCK(slot); 1058 bus_dmamap_unload(slot->dmatag, slot->dmamap); 1059 bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 1060 bus_dma_tag_destroy(slot->dmatag); 1061 if (slot->opt & SDHCI_TUNING_SUPPORTED) { 1062 free(slot->tune_req, M_DEVBUF); 1063 free(slot->tune_cmd, M_DEVBUF); 1064 free(slot->tune_data, M_DEVBUF); 1065 } 1066 1067 SDHCI_LOCK_DESTROY(slot); 1068 1069 return (0); 1070 } 1071 1072 int 1073 sdhci_generic_suspend(struct sdhci_slot *slot) 1074 { 1075 1076 /* 1077 * We expect the MMC layer to issue initial tuning after resume. 1078 * Otherwise, we'd need to indicate re-tuning including circuit reset 1079 * being required at least for re-tuning modes 1 and 2 ourselves. 1080 */ 1081 callout_drain(&slot->retune_callout); 1082 SDHCI_LOCK(slot); 1083 slot->opt &= ~SDHCI_TUNING_ENABLED; 1084 sdhci_reset(slot, SDHCI_RESET_ALL); 1085 SDHCI_UNLOCK(slot); 1086 1087 return (0); 1088 } 1089 1090 int 1091 sdhci_generic_resume(struct sdhci_slot *slot) 1092 { 1093 1094 SDHCI_LOCK(slot); 1095 sdhci_init(slot); 1096 SDHCI_UNLOCK(slot); 1097 1098 return (0); 1099 } 1100 1101 uint32_t 1102 sdhci_generic_min_freq(device_t brdev __unused, struct sdhci_slot *slot) 1103 { 1104 1105 if (slot->version >= SDHCI_SPEC_300) 1106 return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 1107 else 1108 return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 1109 } 1110 1111 bool 1112 sdhci_generic_get_card_present(device_t brdev __unused, struct sdhci_slot *slot) 1113 { 1114 1115 if (slot->opt & SDHCI_NON_REMOVABLE) 1116 return true; 1117 1118 return (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT); 1119 } 1120 1121 void 1122 sdhci_generic_set_uhs_timing(device_t brdev __unused, struct sdhci_slot *slot) 1123 { 1124 struct mmc_ios *ios; 1125 uint16_t hostctrl2; 1126 1127 if (slot->version < SDHCI_SPEC_300) 1128 return; 1129 1130 SDHCI_ASSERT_LOCKED(slot); 1131 ios = &slot->host.ios; 1132 sdhci_set_clock(slot, 0); 1133 hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1134 hostctrl2 &= ~SDHCI_CTRL2_UHS_MASK; 1135 if (ios->clock > SD_SDR50_MAX) { 1136 if (ios->timing == bus_timing_mmc_hs400 || 1137 ios->timing == bus_timing_mmc_hs400es) 1138 hostctrl2 |= SDHCI_CTRL2_MMC_HS400; 1139 else 1140 hostctrl2 |= SDHCI_CTRL2_UHS_SDR104; 1141 } 1142 else if (ios->clock > SD_SDR25_MAX) 1143 hostctrl2 |= SDHCI_CTRL2_UHS_SDR50; 1144 else if (ios->clock > SD_SDR12_MAX) { 1145 if (ios->timing == bus_timing_uhs_ddr50 || 1146 ios->timing == bus_timing_mmc_ddr52) 1147 hostctrl2 |= SDHCI_CTRL2_UHS_DDR50; 1148 else 1149 hostctrl2 |= SDHCI_CTRL2_UHS_SDR25; 1150 } else if (ios->clock > SD_MMC_CARD_ID_FREQUENCY) 1151 hostctrl2 |= SDHCI_CTRL2_UHS_SDR12; 1152 WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 1153 sdhci_set_clock(slot, ios->clock); 1154 } 1155 1156 int 1157 sdhci_generic_update_ios(device_t brdev, device_t reqdev) 1158 { 1159 struct sdhci_slot *slot = device_get_ivars(reqdev); 1160 struct mmc_ios *ios = &slot->host.ios; 1161 1162 SDHCI_LOCK(slot); 1163 /* Do full reset on bus power down to clear from any state. */ 1164 if (ios->power_mode == power_off) { 1165 WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 1166 sdhci_init(slot); 1167 } 1168 /* Configure the bus. */ 1169 sdhci_set_clock(slot, ios->clock); 1170 sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 1171 if (ios->bus_width == bus_width_8) { 1172 slot->hostctrl |= SDHCI_CTRL_8BITBUS; 1173 slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 1174 } else if (ios->bus_width == bus_width_4) { 1175 slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 1176 slot->hostctrl |= SDHCI_CTRL_4BITBUS; 1177 } else if (ios->bus_width == bus_width_1) { 1178 slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 1179 slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 1180 } else { 1181 panic("Invalid bus width: %d", ios->bus_width); 1182 } 1183 if (ios->clock > SD_SDR12_MAX && 1184 !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 1185 slot->hostctrl |= SDHCI_CTRL_HISPD; 1186 else 1187 slot->hostctrl &= ~SDHCI_CTRL_HISPD; 1188 WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 1189 SDHCI_SET_UHS_TIMING(brdev, slot); 1190 /* Some controllers like reset after bus changes. */ 1191 if (slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 1192 sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1193 1194 SDHCI_UNLOCK(slot); 1195 return (0); 1196 } 1197 1198 int 1199 sdhci_generic_switch_vccq(device_t brdev __unused, device_t reqdev) 1200 { 1201 struct sdhci_slot *slot = device_get_ivars(reqdev); 1202 enum mmc_vccq vccq; 1203 int err; 1204 uint16_t hostctrl2; 1205 1206 if (slot->version < SDHCI_SPEC_300) 1207 return (0); 1208 1209 err = 0; 1210 vccq = slot->host.ios.vccq; 1211 SDHCI_LOCK(slot); 1212 sdhci_set_clock(slot, 0); 1213 hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1214 switch (vccq) { 1215 case vccq_330: 1216 if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 1217 goto done; 1218 hostctrl2 &= ~SDHCI_CTRL2_S18_ENABLE; 1219 WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 1220 DELAY(5000); 1221 hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1222 if (!(hostctrl2 & SDHCI_CTRL2_S18_ENABLE)) 1223 goto done; 1224 err = EAGAIN; 1225 break; 1226 case vccq_180: 1227 if (!(slot->host.caps & MMC_CAP_SIGNALING_180)) { 1228 err = EINVAL; 1229 goto done; 1230 } 1231 if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 1232 goto done; 1233 hostctrl2 |= SDHCI_CTRL2_S18_ENABLE; 1234 WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2); 1235 DELAY(5000); 1236 hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1237 if (hostctrl2 & SDHCI_CTRL2_S18_ENABLE) 1238 goto done; 1239 err = EAGAIN; 1240 break; 1241 default: 1242 slot_printf(slot, 1243 "Attempt to set unsupported signaling voltage\n"); 1244 err = EINVAL; 1245 break; 1246 } 1247 done: 1248 sdhci_set_clock(slot, slot->host.ios.clock); 1249 SDHCI_UNLOCK(slot); 1250 return (err); 1251 } 1252 1253 int 1254 sdhci_generic_tune(device_t brdev __unused, device_t reqdev, bool hs400) 1255 { 1256 struct sdhci_slot *slot = device_get_ivars(reqdev); 1257 struct mmc_ios *ios = &slot->host.ios; 1258 struct mmc_command *tune_cmd; 1259 struct mmc_data *tune_data; 1260 uint32_t opcode; 1261 int err; 1262 1263 if (!(slot->opt & SDHCI_TUNING_SUPPORTED)) 1264 return (0); 1265 1266 slot->retune_ticks = slot->retune_count * hz; 1267 opcode = MMC_SEND_TUNING_BLOCK; 1268 SDHCI_LOCK(slot); 1269 switch (ios->timing) { 1270 case bus_timing_mmc_hs400: 1271 slot_printf(slot, "HS400 must be tuned in HS200 mode\n"); 1272 SDHCI_UNLOCK(slot); 1273 return (EINVAL); 1274 case bus_timing_mmc_hs200: 1275 /* 1276 * In HS400 mode, controllers use the data strobe line to 1277 * latch data from the devices so periodic re-tuning isn't 1278 * expected to be required. 1279 */ 1280 if (hs400) 1281 slot->retune_ticks = 0; 1282 opcode = MMC_SEND_TUNING_BLOCK_HS200; 1283 break; 1284 case bus_timing_uhs_ddr50: 1285 case bus_timing_uhs_sdr104: 1286 break; 1287 case bus_timing_uhs_sdr50: 1288 if (slot->opt & SDHCI_SDR50_NEEDS_TUNING) 1289 break; 1290 /* FALLTHROUGH */ 1291 default: 1292 SDHCI_UNLOCK(slot); 1293 return (0); 1294 } 1295 1296 tune_cmd = slot->tune_cmd; 1297 memset(tune_cmd, 0, sizeof(*tune_cmd)); 1298 tune_cmd->opcode = opcode; 1299 tune_cmd->flags = MMC_RSP_R1 | MMC_CMD_ADTC; 1300 tune_data = tune_cmd->data = slot->tune_data; 1301 memset(tune_data, 0, sizeof(*tune_data)); 1302 tune_data->len = (opcode == MMC_SEND_TUNING_BLOCK_HS200 && 1303 ios->bus_width == bus_width_8) ? MMC_TUNING_LEN_HS200 : 1304 MMC_TUNING_LEN; 1305 tune_data->flags = MMC_DATA_READ; 1306 tune_data->mrq = tune_cmd->mrq = slot->tune_req; 1307 1308 slot->opt &= ~SDHCI_TUNING_ENABLED; 1309 err = sdhci_exec_tuning(slot, true); 1310 if (err == 0) { 1311 slot->opt |= SDHCI_TUNING_ENABLED; 1312 slot->intmask |= sdhci_tuning_intmask(slot); 1313 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1314 if (slot->retune_ticks) { 1315 callout_reset(&slot->retune_callout, slot->retune_ticks, 1316 sdhci_retune, slot); 1317 } 1318 } 1319 SDHCI_UNLOCK(slot); 1320 return (err); 1321 } 1322 1323 int 1324 sdhci_generic_retune(device_t brdev __unused, device_t reqdev, bool reset) 1325 { 1326 struct sdhci_slot *slot = device_get_ivars(reqdev); 1327 int err; 1328 1329 if (!(slot->opt & SDHCI_TUNING_ENABLED)) 1330 return (0); 1331 1332 /* HS400 must be tuned in HS200 mode. */ 1333 if (slot->host.ios.timing == bus_timing_mmc_hs400) 1334 return (EINVAL); 1335 1336 SDHCI_LOCK(slot); 1337 err = sdhci_exec_tuning(slot, reset); 1338 /* 1339 * There are two ways sdhci_exec_tuning() can fail: 1340 * EBUSY should not actually happen when requests are only issued 1341 * with the host properly acquired, and 1342 * EIO re-tuning failed (but it did work initially). 1343 * 1344 * In both cases, we should retry at later point if periodic re-tuning 1345 * is enabled. Note that due to slot->retune_req not being cleared in 1346 * these failure cases, the MMC layer should trigger another attempt at 1347 * re-tuning with the next request anyway, though. 1348 */ 1349 if (slot->retune_ticks) { 1350 callout_reset(&slot->retune_callout, slot->retune_ticks, 1351 sdhci_retune, slot); 1352 } 1353 SDHCI_UNLOCK(slot); 1354 return (err); 1355 } 1356 1357 static int 1358 sdhci_exec_tuning(struct sdhci_slot *slot, bool reset) 1359 { 1360 struct mmc_request *tune_req; 1361 struct mmc_command *tune_cmd; 1362 int i; 1363 uint32_t intmask; 1364 uint16_t hostctrl2; 1365 u_char opt; 1366 1367 SDHCI_ASSERT_LOCKED(slot); 1368 if (slot->req != NULL) 1369 return (EBUSY); 1370 1371 /* Tuning doesn't work with DMA enabled. */ 1372 opt = slot->opt; 1373 slot->opt = opt & ~SDHCI_HAVE_DMA; 1374 1375 /* 1376 * Ensure that as documented, SDHCI_INT_DATA_AVAIL is the only 1377 * kind of interrupt we receive in response to a tuning request. 1378 */ 1379 intmask = slot->intmask; 1380 slot->intmask = SDHCI_INT_DATA_AVAIL; 1381 WR4(slot, SDHCI_SIGNAL_ENABLE, SDHCI_INT_DATA_AVAIL); 1382 1383 hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1384 if (reset) 1385 hostctrl2 &= ~SDHCI_CTRL2_SAMPLING_CLOCK; 1386 else 1387 hostctrl2 |= SDHCI_CTRL2_SAMPLING_CLOCK; 1388 WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 | SDHCI_CTRL2_EXEC_TUNING); 1389 1390 tune_req = slot->tune_req; 1391 tune_cmd = slot->tune_cmd; 1392 for (i = 0; i < MMC_TUNING_MAX; i++) { 1393 memset(tune_req, 0, sizeof(*tune_req)); 1394 tune_req->cmd = tune_cmd; 1395 tune_req->done = sdhci_req_wakeup; 1396 tune_req->done_data = slot; 1397 slot->req = tune_req; 1398 slot->flags = 0; 1399 sdhci_start(slot); 1400 while (!(tune_req->flags & MMC_REQ_DONE)) 1401 msleep(tune_req, &slot->mtx, 0, "sdhciet", 0); 1402 if (!(tune_req->flags & MMC_TUNE_DONE)) 1403 break; 1404 hostctrl2 = RD2(slot, SDHCI_HOST_CONTROL2); 1405 if (!(hostctrl2 & SDHCI_CTRL2_EXEC_TUNING)) 1406 break; 1407 if (tune_cmd->opcode == MMC_SEND_TUNING_BLOCK) 1408 DELAY(1000); 1409 } 1410 1411 slot->opt = opt; 1412 slot->intmask = intmask; 1413 WR4(slot, SDHCI_SIGNAL_ENABLE, intmask); 1414 1415 if ((hostctrl2 & (SDHCI_CTRL2_EXEC_TUNING | 1416 SDHCI_CTRL2_SAMPLING_CLOCK)) == SDHCI_CTRL2_SAMPLING_CLOCK) { 1417 slot->retune_req = 0; 1418 return (0); 1419 } 1420 1421 slot_printf(slot, "Tuning failed, using fixed sampling clock\n"); 1422 WR2(slot, SDHCI_HOST_CONTROL2, hostctrl2 & ~(SDHCI_CTRL2_EXEC_TUNING | 1423 SDHCI_CTRL2_SAMPLING_CLOCK)); 1424 sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1425 return (EIO); 1426 } 1427 1428 static void 1429 sdhci_retune(void *arg) 1430 { 1431 struct sdhci_slot *slot = arg; 1432 1433 slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 1434 } 1435 1436 #ifdef MMCCAM 1437 static void 1438 sdhci_req_done(struct sdhci_slot *slot) 1439 { 1440 union ccb *ccb; 1441 1442 if (__predict_false(sdhci_debug > 1)) 1443 slot_printf(slot, "%s\n", __func__); 1444 if (slot->ccb != NULL && slot->curcmd != NULL) { 1445 callout_stop(&slot->timeout_callout); 1446 ccb = slot->ccb; 1447 slot->ccb = NULL; 1448 slot->curcmd = NULL; 1449 1450 /* Tell CAM the request is finished */ 1451 struct ccb_mmcio *mmcio; 1452 mmcio = &ccb->mmcio; 1453 1454 ccb->ccb_h.status = 1455 (mmcio->cmd.error == 0 ? CAM_REQ_CMP : CAM_REQ_CMP_ERR); 1456 xpt_done(ccb); 1457 } 1458 } 1459 #else 1460 static void 1461 sdhci_req_done(struct sdhci_slot *slot) 1462 { 1463 struct mmc_request *req; 1464 1465 if (slot->req != NULL && slot->curcmd != NULL) { 1466 callout_stop(&slot->timeout_callout); 1467 req = slot->req; 1468 slot->req = NULL; 1469 slot->curcmd = NULL; 1470 req->done(req); 1471 } 1472 } 1473 #endif 1474 1475 static void 1476 sdhci_req_wakeup(struct mmc_request *req) 1477 { 1478 struct sdhci_slot *slot; 1479 1480 slot = req->done_data; 1481 req->flags |= MMC_REQ_DONE; 1482 wakeup(req); 1483 } 1484 1485 static void 1486 sdhci_timeout(void *arg) 1487 { 1488 struct sdhci_slot *slot = arg; 1489 1490 if (slot->curcmd != NULL) { 1491 slot_printf(slot, "Controller timeout\n"); 1492 sdhci_dumpregs(slot); 1493 sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 1494 slot->curcmd->error = MMC_ERR_TIMEOUT; 1495 sdhci_req_done(slot); 1496 } else { 1497 slot_printf(slot, "Spurious timeout - no active command\n"); 1498 } 1499 } 1500 1501 static void 1502 sdhci_set_transfer_mode(struct sdhci_slot *slot, struct mmc_data *data) 1503 { 1504 uint16_t mode; 1505 1506 if (data == NULL) 1507 return; 1508 1509 mode = SDHCI_TRNS_BLK_CNT_EN; 1510 if (data->len > 512) 1511 mode |= SDHCI_TRNS_MULTI; 1512 if (data->flags & MMC_DATA_READ) 1513 mode |= SDHCI_TRNS_READ; 1514 #ifdef MMCCAM 1515 struct ccb_mmcio *mmcio; 1516 mmcio = &slot->ccb->mmcio; 1517 if (mmcio->stop.opcode == MMC_STOP_TRANSMISSION 1518 && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) 1519 mode |= SDHCI_TRNS_ACMD12; 1520 #else 1521 if (slot->req->stop && !(slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) 1522 mode |= SDHCI_TRNS_ACMD12; 1523 #endif 1524 if (slot->flags & SDHCI_USE_DMA) 1525 mode |= SDHCI_TRNS_DMA; 1526 1527 WR2(slot, SDHCI_TRANSFER_MODE, mode); 1528 } 1529 1530 static void 1531 sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 1532 { 1533 int flags, timeout; 1534 uint32_t mask; 1535 1536 slot->curcmd = cmd; 1537 slot->cmd_done = 0; 1538 1539 cmd->error = MMC_ERR_NONE; 1540 1541 /* This flags combination is not supported by controller. */ 1542 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 1543 slot_printf(slot, "Unsupported response type!\n"); 1544 cmd->error = MMC_ERR_FAILED; 1545 sdhci_req_done(slot); 1546 return; 1547 } 1548 1549 /* 1550 * Do not issue command if there is no card, clock or power. 1551 * Controller will not detect timeout without clock active. 1552 */ 1553 if (!SDHCI_GET_CARD_PRESENT(slot->bus, slot) || 1554 slot->power == 0 || 1555 slot->clock == 0) { 1556 slot_printf(slot, 1557 "Cannot issue a command (power=%d clock=%d)", 1558 slot->power, slot->clock); 1559 cmd->error = MMC_ERR_FAILED; 1560 sdhci_req_done(slot); 1561 return; 1562 } 1563 /* Always wait for free CMD bus. */ 1564 mask = SDHCI_CMD_INHIBIT; 1565 /* Wait for free DAT if we have data or busy signal. */ 1566 if (cmd->data != NULL || (cmd->flags & MMC_RSP_BUSY)) 1567 mask |= SDHCI_DAT_INHIBIT; 1568 /* 1569 * We shouldn't wait for DAT for stop commands or CMD19/CMD21. Note 1570 * that these latter are also special in that SDHCI_CMD_DATA should 1571 * be set below but no actual data is ever read from the controller. 1572 */ 1573 #ifdef MMCCAM 1574 if (cmd == &slot->ccb->mmcio.stop || 1575 #else 1576 if (cmd == slot->req->stop || 1577 #endif 1578 __predict_false(cmd->opcode == MMC_SEND_TUNING_BLOCK || 1579 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)) 1580 mask &= ~SDHCI_DAT_INHIBIT; 1581 /* 1582 * Wait for bus no more then 250 ms. Typically there will be no wait 1583 * here at all, but when writing a crash dump we may be bypassing the 1584 * host platform's interrupt handler, and in some cases that handler 1585 * may be working around hardware quirks such as not respecting r1b 1586 * busy indications. In those cases, this wait-loop serves the purpose 1587 * of waiting for the prior command and data transfers to be done, and 1588 * SD cards are allowed to take up to 250ms for write and erase ops. 1589 * (It's usually more like 20-30ms in the real world.) 1590 */ 1591 timeout = 250; 1592 while (mask & RD4(slot, SDHCI_PRESENT_STATE)) { 1593 if (timeout == 0) { 1594 slot_printf(slot, "Controller never released " 1595 "inhibit bit(s).\n"); 1596 sdhci_dumpregs(slot); 1597 cmd->error = MMC_ERR_FAILED; 1598 sdhci_req_done(slot); 1599 return; 1600 } 1601 timeout--; 1602 DELAY(1000); 1603 } 1604 1605 /* Prepare command flags. */ 1606 if (!(cmd->flags & MMC_RSP_PRESENT)) 1607 flags = SDHCI_CMD_RESP_NONE; 1608 else if (cmd->flags & MMC_RSP_136) 1609 flags = SDHCI_CMD_RESP_LONG; 1610 else if (cmd->flags & MMC_RSP_BUSY) 1611 flags = SDHCI_CMD_RESP_SHORT_BUSY; 1612 else 1613 flags = SDHCI_CMD_RESP_SHORT; 1614 if (cmd->flags & MMC_RSP_CRC) 1615 flags |= SDHCI_CMD_CRC; 1616 if (cmd->flags & MMC_RSP_OPCODE) 1617 flags |= SDHCI_CMD_INDEX; 1618 if (cmd->data != NULL) 1619 flags |= SDHCI_CMD_DATA; 1620 if (cmd->opcode == MMC_STOP_TRANSMISSION) 1621 flags |= SDHCI_CMD_TYPE_ABORT; 1622 /* Prepare data. */ 1623 sdhci_start_data(slot, cmd->data); 1624 /* 1625 * Interrupt aggregation: To reduce total number of interrupts 1626 * group response interrupt with data interrupt when possible. 1627 * If there going to be data interrupt, mask response one. 1628 */ 1629 if (slot->data_done == 0) { 1630 WR4(slot, SDHCI_SIGNAL_ENABLE, 1631 slot->intmask &= ~SDHCI_INT_RESPONSE); 1632 } 1633 /* Set command argument. */ 1634 WR4(slot, SDHCI_ARGUMENT, cmd->arg); 1635 /* Set data transfer mode. */ 1636 sdhci_set_transfer_mode(slot, cmd->data); 1637 if (__predict_false(sdhci_debug > 1)) 1638 slot_printf(slot, "Starting command!\n"); 1639 /* Start command. */ 1640 WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 1641 /* Start timeout callout. */ 1642 callout_reset(&slot->timeout_callout, slot->timeout * hz, 1643 sdhci_timeout, slot); 1644 } 1645 1646 static void 1647 sdhci_finish_command(struct sdhci_slot *slot) 1648 { 1649 int i; 1650 uint32_t val; 1651 uint8_t extra; 1652 1653 if (__predict_false(sdhci_debug > 1)) 1654 slot_printf(slot, "%s: called, err %d flags %d\n", 1655 __func__, slot->curcmd->error, slot->curcmd->flags); 1656 slot->cmd_done = 1; 1657 /* 1658 * Interrupt aggregation: Restore command interrupt. 1659 * Main restore point for the case when command interrupt 1660 * happened first. 1661 */ 1662 if (__predict_true(slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK && 1663 slot->curcmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)) 1664 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= 1665 SDHCI_INT_RESPONSE); 1666 /* In case of error - reset host and return. */ 1667 if (slot->curcmd->error) { 1668 if (slot->curcmd->error == MMC_ERR_BADCRC) 1669 slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1670 sdhci_reset(slot, SDHCI_RESET_CMD); 1671 sdhci_reset(slot, SDHCI_RESET_DATA); 1672 sdhci_start(slot); 1673 return; 1674 } 1675 /* If command has response - fetch it. */ 1676 if (slot->curcmd->flags & MMC_RSP_PRESENT) { 1677 if (slot->curcmd->flags & MMC_RSP_136) { 1678 /* CRC is stripped so we need one byte shift. */ 1679 extra = 0; 1680 for (i = 0; i < 4; i++) { 1681 val = RD4(slot, SDHCI_RESPONSE + i * 4); 1682 if (slot->quirks & 1683 SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 1684 slot->curcmd->resp[3 - i] = val; 1685 else { 1686 slot->curcmd->resp[3 - i] = 1687 (val << 8) | extra; 1688 extra = val >> 24; 1689 } 1690 } 1691 } else 1692 slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 1693 } 1694 if (__predict_false(sdhci_debug > 1)) 1695 printf("Resp: %02x %02x %02x %02x\n", 1696 slot->curcmd->resp[0], slot->curcmd->resp[1], 1697 slot->curcmd->resp[2], slot->curcmd->resp[3]); 1698 1699 /* If data ready - finish. */ 1700 if (slot->data_done) 1701 sdhci_start(slot); 1702 } 1703 1704 static void 1705 sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data) 1706 { 1707 uint32_t target_timeout, current_timeout; 1708 uint8_t div; 1709 1710 if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1711 slot->data_done = 1; 1712 return; 1713 } 1714 1715 slot->data_done = 0; 1716 1717 /* Calculate and set data timeout.*/ 1718 /* XXX: We should have this from mmc layer, now assume 1 sec. */ 1719 if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 1720 div = 0xE; 1721 } else { 1722 target_timeout = 1000000; 1723 div = 0; 1724 current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 1725 while (current_timeout < target_timeout && div < 0xE) { 1726 ++div; 1727 current_timeout <<= 1; 1728 } 1729 /* Compensate for an off-by-one error in the CaFe chip.*/ 1730 if (div < 0xE && 1731 (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 1732 ++div; 1733 } 1734 } 1735 WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 1736 1737 if (data == NULL) 1738 return; 1739 1740 /* Use DMA if possible. */ 1741 if ((slot->opt & SDHCI_HAVE_DMA)) 1742 slot->flags |= SDHCI_USE_DMA; 1743 /* If data is small, broken DMA may return zeroes instead of data, */ 1744 if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 1745 (data->len <= 512)) 1746 slot->flags &= ~SDHCI_USE_DMA; 1747 /* Some controllers require even block sizes. */ 1748 if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 1749 ((data->len) & 0x3)) 1750 slot->flags &= ~SDHCI_USE_DMA; 1751 /* Load DMA buffer. */ 1752 if (slot->flags & SDHCI_USE_DMA) { 1753 if (data->flags & MMC_DATA_READ) 1754 bus_dmamap_sync(slot->dmatag, slot->dmamap, 1755 BUS_DMASYNC_PREREAD); 1756 else { 1757 memcpy(slot->dmamem, data->data, 1758 (data->len < DMA_BLOCK_SIZE) ? 1759 data->len : DMA_BLOCK_SIZE); 1760 bus_dmamap_sync(slot->dmatag, slot->dmamap, 1761 BUS_DMASYNC_PREWRITE); 1762 } 1763 WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1764 /* Interrupt aggregation: Mask border interrupt 1765 * for the last page and unmask else. */ 1766 if (data->len == DMA_BLOCK_SIZE) 1767 slot->intmask &= ~SDHCI_INT_DMA_END; 1768 else 1769 slot->intmask |= SDHCI_INT_DMA_END; 1770 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1771 } 1772 /* Current data offset for both PIO and DMA. */ 1773 slot->offset = 0; 1774 /* Set block size and request IRQ on 4K border. */ 1775 WR2(slot, SDHCI_BLOCK_SIZE, SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, 1776 (data->len < 512) ? data->len : 512)); 1777 /* Set block count. */ 1778 WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512); 1779 1780 if (__predict_false(sdhci_debug > 1)) 1781 slot_printf(slot, "Block size: %02x, count %lu\n", 1782 (unsigned int)SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512) ? data->len : 512), 1783 (unsigned long)(data->len + 511) / 512); 1784 } 1785 1786 void 1787 sdhci_finish_data(struct sdhci_slot *slot) 1788 { 1789 struct mmc_data *data = slot->curcmd->data; 1790 size_t left; 1791 1792 /* Interrupt aggregation: Restore command interrupt. 1793 * Auxiliary restore point for the case when data interrupt 1794 * happened first. */ 1795 if (!slot->cmd_done) { 1796 WR4(slot, SDHCI_SIGNAL_ENABLE, 1797 slot->intmask |= SDHCI_INT_RESPONSE); 1798 } 1799 /* Unload rest of data from DMA buffer. */ 1800 if (!slot->data_done && (slot->flags & SDHCI_USE_DMA) && 1801 slot->curcmd->data != NULL) { 1802 if (data->flags & MMC_DATA_READ) { 1803 left = data->len - slot->offset; 1804 bus_dmamap_sync(slot->dmatag, slot->dmamap, 1805 BUS_DMASYNC_POSTREAD); 1806 memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1807 (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE); 1808 } else 1809 bus_dmamap_sync(slot->dmatag, slot->dmamap, 1810 BUS_DMASYNC_POSTWRITE); 1811 } 1812 slot->data_done = 1; 1813 /* If there was error - reset the host. */ 1814 if (slot->curcmd->error) { 1815 if (slot->curcmd->error == MMC_ERR_BADCRC) 1816 slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 1817 sdhci_reset(slot, SDHCI_RESET_CMD); 1818 sdhci_reset(slot, SDHCI_RESET_DATA); 1819 sdhci_start(slot); 1820 return; 1821 } 1822 /* If we already have command response - finish. */ 1823 if (slot->cmd_done) 1824 sdhci_start(slot); 1825 } 1826 1827 #ifdef MMCCAM 1828 static void 1829 sdhci_start(struct sdhci_slot *slot) 1830 { 1831 union ccb *ccb; 1832 1833 ccb = slot->ccb; 1834 if (ccb == NULL) 1835 return; 1836 1837 struct ccb_mmcio *mmcio; 1838 mmcio = &ccb->mmcio; 1839 1840 if (!(slot->flags & CMD_STARTED)) { 1841 slot->flags |= CMD_STARTED; 1842 sdhci_start_command(slot, &mmcio->cmd); 1843 return; 1844 } 1845 1846 /* 1847 * Old stack doesn't use this! 1848 * Enabling this code causes significant performance degradation 1849 * and IRQ storms on BBB, Wandboard behaves fine. 1850 * Not using this code does no harm... 1851 if (!(slot->flags & STOP_STARTED) && mmcio->stop.opcode != 0) { 1852 slot->flags |= STOP_STARTED; 1853 sdhci_start_command(slot, &mmcio->stop); 1854 return; 1855 } 1856 */ 1857 if (__predict_false(sdhci_debug > 1)) 1858 slot_printf(slot, "result: %d\n", mmcio->cmd.error); 1859 if (mmcio->cmd.error == 0 && 1860 (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 1861 sdhci_reset(slot, SDHCI_RESET_CMD); 1862 sdhci_reset(slot, SDHCI_RESET_DATA); 1863 } 1864 1865 sdhci_req_done(slot); 1866 } 1867 #else 1868 static void 1869 sdhci_start(struct sdhci_slot *slot) 1870 { 1871 struct mmc_request *req; 1872 1873 req = slot->req; 1874 if (req == NULL) 1875 return; 1876 1877 if (!(slot->flags & CMD_STARTED)) { 1878 slot->flags |= CMD_STARTED; 1879 sdhci_start_command(slot, req->cmd); 1880 return; 1881 } 1882 if ((slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP) && 1883 !(slot->flags & STOP_STARTED) && req->stop) { 1884 slot->flags |= STOP_STARTED; 1885 sdhci_start_command(slot, req->stop); 1886 return; 1887 } 1888 if (__predict_false(sdhci_debug > 1)) 1889 slot_printf(slot, "result: %d\n", req->cmd->error); 1890 if (!req->cmd->error && 1891 ((slot->curcmd == req->stop && 1892 (slot->quirks & SDHCI_QUIRK_BROKEN_AUTO_STOP)) || 1893 (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) { 1894 sdhci_reset(slot, SDHCI_RESET_CMD); 1895 sdhci_reset(slot, SDHCI_RESET_DATA); 1896 } 1897 1898 sdhci_req_done(slot); 1899 } 1900 #endif 1901 1902 int 1903 sdhci_generic_request(device_t brdev __unused, device_t reqdev, 1904 struct mmc_request *req) 1905 { 1906 struct sdhci_slot *slot = device_get_ivars(reqdev); 1907 1908 SDHCI_LOCK(slot); 1909 if (slot->req != NULL) { 1910 SDHCI_UNLOCK(slot); 1911 return (EBUSY); 1912 } 1913 if (__predict_false(sdhci_debug > 1)) { 1914 slot_printf(slot, 1915 "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 1916 req->cmd->opcode, req->cmd->arg, req->cmd->flags, 1917 (req->cmd->data)?(u_int)req->cmd->data->len:0, 1918 (req->cmd->data)?req->cmd->data->flags:0); 1919 } 1920 slot->req = req; 1921 slot->flags = 0; 1922 sdhci_start(slot); 1923 SDHCI_UNLOCK(slot); 1924 if (dumping) { 1925 while (slot->req != NULL) { 1926 sdhci_generic_intr(slot); 1927 DELAY(10); 1928 } 1929 } 1930 return (0); 1931 } 1932 1933 int 1934 sdhci_generic_get_ro(device_t brdev __unused, device_t reqdev) 1935 { 1936 struct sdhci_slot *slot = device_get_ivars(reqdev); 1937 uint32_t val; 1938 1939 SDHCI_LOCK(slot); 1940 val = RD4(slot, SDHCI_PRESENT_STATE); 1941 SDHCI_UNLOCK(slot); 1942 return (!(val & SDHCI_WRITE_PROTECT)); 1943 } 1944 1945 int 1946 sdhci_generic_acquire_host(device_t brdev __unused, device_t reqdev) 1947 { 1948 struct sdhci_slot *slot = device_get_ivars(reqdev); 1949 int err = 0; 1950 1951 SDHCI_LOCK(slot); 1952 while (slot->bus_busy) 1953 msleep(slot, &slot->mtx, 0, "sdhciah", 0); 1954 slot->bus_busy++; 1955 /* Activate led. */ 1956 WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 1957 SDHCI_UNLOCK(slot); 1958 return (err); 1959 } 1960 1961 int 1962 sdhci_generic_release_host(device_t brdev __unused, device_t reqdev) 1963 { 1964 struct sdhci_slot *slot = device_get_ivars(reqdev); 1965 1966 SDHCI_LOCK(slot); 1967 /* Deactivate led. */ 1968 WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 1969 slot->bus_busy--; 1970 SDHCI_UNLOCK(slot); 1971 wakeup(slot); 1972 return (0); 1973 } 1974 1975 static void 1976 sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 1977 { 1978 1979 if (!slot->curcmd) { 1980 slot_printf(slot, "Got command interrupt 0x%08x, but " 1981 "there is no active command.\n", intmask); 1982 sdhci_dumpregs(slot); 1983 return; 1984 } 1985 if (intmask & SDHCI_INT_TIMEOUT) 1986 slot->curcmd->error = MMC_ERR_TIMEOUT; 1987 else if (intmask & SDHCI_INT_CRC) 1988 slot->curcmd->error = MMC_ERR_BADCRC; 1989 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 1990 slot->curcmd->error = MMC_ERR_FIFO; 1991 1992 sdhci_finish_command(slot); 1993 } 1994 1995 static void 1996 sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 1997 { 1998 struct mmc_data *data; 1999 size_t left; 2000 2001 if (!slot->curcmd) { 2002 slot_printf(slot, "Got data interrupt 0x%08x, but " 2003 "there is no active command.\n", intmask); 2004 sdhci_dumpregs(slot); 2005 return; 2006 } 2007 if (slot->curcmd->data == NULL && 2008 (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 2009 slot_printf(slot, "Got data interrupt 0x%08x, but " 2010 "there is no active data operation.\n", 2011 intmask); 2012 sdhci_dumpregs(slot); 2013 return; 2014 } 2015 if (intmask & SDHCI_INT_DATA_TIMEOUT) 2016 slot->curcmd->error = MMC_ERR_TIMEOUT; 2017 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 2018 slot->curcmd->error = MMC_ERR_BADCRC; 2019 if (slot->curcmd->data == NULL && 2020 (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 2021 SDHCI_INT_DMA_END))) { 2022 slot_printf(slot, "Got data interrupt 0x%08x, but " 2023 "there is busy-only command.\n", intmask); 2024 sdhci_dumpregs(slot); 2025 slot->curcmd->error = MMC_ERR_INVALID; 2026 } 2027 if (slot->curcmd->error) { 2028 /* No need to continue after any error. */ 2029 goto done; 2030 } 2031 2032 /* Handle tuning completion interrupt. */ 2033 if (__predict_false((intmask & SDHCI_INT_DATA_AVAIL) && 2034 (slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK || 2035 slot->curcmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) { 2036 slot->req->flags |= MMC_TUNE_DONE; 2037 sdhci_finish_command(slot); 2038 sdhci_finish_data(slot); 2039 return; 2040 } 2041 /* Handle PIO interrupt. */ 2042 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 2043 if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 2044 SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 2045 SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, 2046 &intmask); 2047 slot->flags |= PLATFORM_DATA_STARTED; 2048 } else 2049 sdhci_transfer_pio(slot); 2050 } 2051 /* Handle DMA border. */ 2052 if (intmask & SDHCI_INT_DMA_END) { 2053 data = slot->curcmd->data; 2054 2055 /* Unload DMA buffer ... */ 2056 left = data->len - slot->offset; 2057 if (data->flags & MMC_DATA_READ) { 2058 bus_dmamap_sync(slot->dmatag, slot->dmamap, 2059 BUS_DMASYNC_POSTREAD); 2060 memcpy((u_char*)data->data + slot->offset, slot->dmamem, 2061 (left < DMA_BLOCK_SIZE) ? left : DMA_BLOCK_SIZE); 2062 } else { 2063 bus_dmamap_sync(slot->dmatag, slot->dmamap, 2064 BUS_DMASYNC_POSTWRITE); 2065 } 2066 /* ... and reload it again. */ 2067 slot->offset += DMA_BLOCK_SIZE; 2068 left = data->len - slot->offset; 2069 if (data->flags & MMC_DATA_READ) { 2070 bus_dmamap_sync(slot->dmatag, slot->dmamap, 2071 BUS_DMASYNC_PREREAD); 2072 } else { 2073 memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 2074 (left < DMA_BLOCK_SIZE)? left : DMA_BLOCK_SIZE); 2075 bus_dmamap_sync(slot->dmatag, slot->dmamap, 2076 BUS_DMASYNC_PREWRITE); 2077 } 2078 /* Interrupt aggregation: Mask border interrupt 2079 * for the last page. */ 2080 if (left == DMA_BLOCK_SIZE) { 2081 slot->intmask &= ~SDHCI_INT_DMA_END; 2082 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2083 } 2084 /* Restart DMA. */ 2085 WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 2086 } 2087 /* We have got all data. */ 2088 if (intmask & SDHCI_INT_DATA_END) { 2089 if (slot->flags & PLATFORM_DATA_STARTED) { 2090 slot->flags &= ~PLATFORM_DATA_STARTED; 2091 SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2092 } else 2093 sdhci_finish_data(slot); 2094 } 2095 done: 2096 if (slot->curcmd != NULL && slot->curcmd->error != 0) { 2097 if (slot->flags & PLATFORM_DATA_STARTED) { 2098 slot->flags &= ~PLATFORM_DATA_STARTED; 2099 SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 2100 } else 2101 sdhci_finish_data(slot); 2102 } 2103 } 2104 2105 static void 2106 sdhci_acmd_irq(struct sdhci_slot *slot) 2107 { 2108 uint16_t err; 2109 2110 err = RD4(slot, SDHCI_ACMD12_ERR); 2111 if (!slot->curcmd) { 2112 slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 2113 "there is no active command.\n", err); 2114 sdhci_dumpregs(slot); 2115 return; 2116 } 2117 slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err); 2118 sdhci_reset(slot, SDHCI_RESET_CMD); 2119 } 2120 2121 void 2122 sdhci_generic_intr(struct sdhci_slot *slot) 2123 { 2124 uint32_t intmask, present; 2125 2126 SDHCI_LOCK(slot); 2127 /* Read slot interrupt status. */ 2128 intmask = RD4(slot, SDHCI_INT_STATUS); 2129 if (intmask == 0 || intmask == 0xffffffff) { 2130 SDHCI_UNLOCK(slot); 2131 return; 2132 } 2133 if (__predict_false(sdhci_debug > 2)) 2134 slot_printf(slot, "Interrupt %#x\n", intmask); 2135 2136 /* Handle tuning error interrupt. */ 2137 if (__predict_false(intmask & SDHCI_INT_TUNEERR)) { 2138 slot_printf(slot, "Tuning error indicated\n"); 2139 slot->retune_req |= SDHCI_RETUNE_REQ_RESET; 2140 if (slot->curcmd) { 2141 slot->curcmd->error = MMC_ERR_BADCRC; 2142 sdhci_finish_command(slot); 2143 } 2144 } 2145 /* Handle re-tuning interrupt. */ 2146 if (__predict_false(intmask & SDHCI_INT_RETUNE)) 2147 slot->retune_req |= SDHCI_RETUNE_REQ_NEEDED; 2148 /* Handle card presence interrupts. */ 2149 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 2150 present = (intmask & SDHCI_INT_CARD_INSERT) != 0; 2151 slot->intmask &= 2152 ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 2153 slot->intmask |= present ? SDHCI_INT_CARD_REMOVE : 2154 SDHCI_INT_CARD_INSERT; 2155 WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 2156 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 2157 WR4(slot, SDHCI_INT_STATUS, intmask & 2158 (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 2159 sdhci_handle_card_present_locked(slot, present); 2160 } 2161 /* Handle command interrupts. */ 2162 if (intmask & SDHCI_INT_CMD_MASK) { 2163 WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 2164 sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 2165 } 2166 /* Handle data interrupts. */ 2167 if (intmask & SDHCI_INT_DATA_MASK) { 2168 WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 2169 /* Don't call data_irq in case of errored command. */ 2170 if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 2171 sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 2172 } 2173 /* Handle AutoCMD12 error interrupt. */ 2174 if (intmask & SDHCI_INT_ACMD12ERR) { 2175 WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 2176 sdhci_acmd_irq(slot); 2177 } 2178 /* Handle bus power interrupt. */ 2179 if (intmask & SDHCI_INT_BUS_POWER) { 2180 WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 2181 slot_printf(slot, "Card is consuming too much power!\n"); 2182 } 2183 intmask &= ~(SDHCI_INT_ERROR | SDHCI_INT_TUNEERR | SDHCI_INT_RETUNE | 2184 SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE | SDHCI_INT_CMD_MASK | 2185 SDHCI_INT_DATA_MASK | SDHCI_INT_ACMD12ERR | SDHCI_INT_BUS_POWER); 2186 /* The rest is unknown. */ 2187 if (intmask) { 2188 WR4(slot, SDHCI_INT_STATUS, intmask); 2189 slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 2190 intmask); 2191 sdhci_dumpregs(slot); 2192 } 2193 2194 SDHCI_UNLOCK(slot); 2195 } 2196 2197 int 2198 sdhci_generic_read_ivar(device_t bus, device_t child, int which, 2199 uintptr_t *result) 2200 { 2201 struct sdhci_slot *slot = device_get_ivars(child); 2202 2203 switch (which) { 2204 default: 2205 return (EINVAL); 2206 case MMCBR_IVAR_BUS_MODE: 2207 *result = slot->host.ios.bus_mode; 2208 break; 2209 case MMCBR_IVAR_BUS_WIDTH: 2210 *result = slot->host.ios.bus_width; 2211 break; 2212 case MMCBR_IVAR_CHIP_SELECT: 2213 *result = slot->host.ios.chip_select; 2214 break; 2215 case MMCBR_IVAR_CLOCK: 2216 *result = slot->host.ios.clock; 2217 break; 2218 case MMCBR_IVAR_F_MIN: 2219 *result = slot->host.f_min; 2220 break; 2221 case MMCBR_IVAR_F_MAX: 2222 *result = slot->host.f_max; 2223 break; 2224 case MMCBR_IVAR_HOST_OCR: 2225 *result = slot->host.host_ocr; 2226 break; 2227 case MMCBR_IVAR_MODE: 2228 *result = slot->host.mode; 2229 break; 2230 case MMCBR_IVAR_OCR: 2231 *result = slot->host.ocr; 2232 break; 2233 case MMCBR_IVAR_POWER_MODE: 2234 *result = slot->host.ios.power_mode; 2235 break; 2236 case MMCBR_IVAR_VDD: 2237 *result = slot->host.ios.vdd; 2238 break; 2239 case MMCBR_IVAR_RETUNE_REQ: 2240 if (slot->opt & SDHCI_TUNING_ENABLED) { 2241 if (slot->retune_req & SDHCI_RETUNE_REQ_RESET) { 2242 *result = retune_req_reset; 2243 break; 2244 } 2245 if (slot->retune_req & SDHCI_RETUNE_REQ_NEEDED) { 2246 *result = retune_req_normal; 2247 break; 2248 } 2249 } 2250 *result = retune_req_none; 2251 break; 2252 case MMCBR_IVAR_VCCQ: 2253 *result = slot->host.ios.vccq; 2254 break; 2255 case MMCBR_IVAR_CAPS: 2256 *result = slot->host.caps; 2257 break; 2258 case MMCBR_IVAR_TIMING: 2259 *result = slot->host.ios.timing; 2260 break; 2261 case MMCBR_IVAR_MAX_DATA: 2262 /* 2263 * Re-tuning modes 1 and 2 restrict the maximum data length 2264 * per read/write command to 4 MiB. 2265 */ 2266 if (slot->opt & SDHCI_TUNING_ENABLED && 2267 (slot->retune_mode == SDHCI_RETUNE_MODE_1 || 2268 slot->retune_mode == SDHCI_RETUNE_MODE_2)) { 2269 *result = 4 * 1024 * 1024 / MMC_SECTOR_SIZE; 2270 break; 2271 } 2272 *result = 65535; 2273 break; 2274 case MMCBR_IVAR_MAX_BUSY_TIMEOUT: 2275 /* 2276 * Currently, sdhci_start_data() hardcodes 1 s for all CMDs. 2277 */ 2278 *result = 1000000; 2279 break; 2280 } 2281 return (0); 2282 } 2283 2284 int 2285 sdhci_generic_write_ivar(device_t bus, device_t child, int which, 2286 uintptr_t value) 2287 { 2288 struct sdhci_slot *slot = device_get_ivars(child); 2289 uint32_t clock, max_clock; 2290 int i; 2291 2292 if (sdhci_debug > 1) 2293 slot_printf(slot, "%s: var=%d\n", __func__, which); 2294 switch (which) { 2295 default: 2296 return (EINVAL); 2297 case MMCBR_IVAR_BUS_MODE: 2298 slot->host.ios.bus_mode = value; 2299 break; 2300 case MMCBR_IVAR_BUS_WIDTH: 2301 slot->host.ios.bus_width = value; 2302 break; 2303 case MMCBR_IVAR_CHIP_SELECT: 2304 slot->host.ios.chip_select = value; 2305 break; 2306 case MMCBR_IVAR_CLOCK: 2307 if (value > 0) { 2308 max_clock = slot->max_clk; 2309 clock = max_clock; 2310 2311 if (slot->version < SDHCI_SPEC_300) { 2312 for (i = 0; i < SDHCI_200_MAX_DIVIDER; 2313 i <<= 1) { 2314 if (clock <= value) 2315 break; 2316 clock >>= 1; 2317 } 2318 } else { 2319 for (i = 0; i < SDHCI_300_MAX_DIVIDER; 2320 i += 2) { 2321 if (clock <= value) 2322 break; 2323 clock = max_clock / (i + 2); 2324 } 2325 } 2326 2327 slot->host.ios.clock = clock; 2328 } else 2329 slot->host.ios.clock = 0; 2330 break; 2331 case MMCBR_IVAR_MODE: 2332 slot->host.mode = value; 2333 break; 2334 case MMCBR_IVAR_OCR: 2335 slot->host.ocr = value; 2336 break; 2337 case MMCBR_IVAR_POWER_MODE: 2338 slot->host.ios.power_mode = value; 2339 break; 2340 case MMCBR_IVAR_VDD: 2341 slot->host.ios.vdd = value; 2342 break; 2343 case MMCBR_IVAR_VCCQ: 2344 slot->host.ios.vccq = value; 2345 break; 2346 case MMCBR_IVAR_TIMING: 2347 slot->host.ios.timing = value; 2348 break; 2349 case MMCBR_IVAR_CAPS: 2350 case MMCBR_IVAR_HOST_OCR: 2351 case MMCBR_IVAR_F_MIN: 2352 case MMCBR_IVAR_F_MAX: 2353 case MMCBR_IVAR_MAX_DATA: 2354 case MMCBR_IVAR_RETUNE_REQ: 2355 return (EINVAL); 2356 } 2357 return (0); 2358 } 2359 2360 #ifdef MMCCAM 2361 void 2362 sdhci_cam_start_slot(struct sdhci_slot *slot) 2363 { 2364 if ((slot->devq = cam_simq_alloc(1)) == NULL) { 2365 goto fail; 2366 } 2367 2368 mtx_init(&slot->sim_mtx, "sdhcisim", NULL, MTX_DEF); 2369 slot->sim = cam_sim_alloc(sdhci_cam_action, sdhci_cam_poll, 2370 "sdhci_slot", slot, device_get_unit(slot->bus), 2371 &slot->sim_mtx, 1, 1, slot->devq); 2372 2373 if (slot->sim == NULL) { 2374 cam_simq_free(slot->devq); 2375 slot_printf(slot, "cannot allocate CAM SIM\n"); 2376 goto fail; 2377 } 2378 2379 mtx_lock(&slot->sim_mtx); 2380 if (xpt_bus_register(slot->sim, slot->bus, 0) != 0) { 2381 slot_printf(slot, 2382 "cannot register SCSI pass-through bus\n"); 2383 cam_sim_free(slot->sim, FALSE); 2384 cam_simq_free(slot->devq); 2385 mtx_unlock(&slot->sim_mtx); 2386 goto fail; 2387 } 2388 2389 mtx_unlock(&slot->sim_mtx); 2390 /* End CAM-specific init */ 2391 slot->card_present = 0; 2392 sdhci_card_task(slot, 0); 2393 return; 2394 2395 fail: 2396 if (slot->sim != NULL) { 2397 mtx_lock(&slot->sim_mtx); 2398 xpt_bus_deregister(cam_sim_path(slot->sim)); 2399 cam_sim_free(slot->sim, FALSE); 2400 mtx_unlock(&slot->sim_mtx); 2401 } 2402 2403 if (slot->devq != NULL) 2404 cam_simq_free(slot->devq); 2405 } 2406 2407 static void 2408 sdhci_cam_handle_mmcio(struct cam_sim *sim, union ccb *ccb) 2409 { 2410 struct sdhci_slot *slot; 2411 2412 slot = cam_sim_softc(sim); 2413 2414 sdhci_cam_request(slot, ccb); 2415 } 2416 2417 void 2418 sdhci_cam_action(struct cam_sim *sim, union ccb *ccb) 2419 { 2420 struct sdhci_slot *slot; 2421 2422 slot = cam_sim_softc(sim); 2423 if (slot == NULL) { 2424 ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2425 xpt_done(ccb); 2426 return; 2427 } 2428 2429 mtx_assert(&slot->sim_mtx, MA_OWNED); 2430 2431 switch (ccb->ccb_h.func_code) { 2432 case XPT_PATH_INQ: 2433 { 2434 struct ccb_pathinq *cpi; 2435 2436 cpi = &ccb->cpi; 2437 cpi->version_num = 1; 2438 cpi->hba_inquiry = 0; 2439 cpi->target_sprt = 0; 2440 cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN; 2441 cpi->hba_eng_cnt = 0; 2442 cpi->max_target = 0; 2443 cpi->max_lun = 0; 2444 cpi->initiator_id = 1; 2445 cpi->maxio = MAXPHYS; 2446 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2447 strncpy(cpi->hba_vid, "Deglitch Networks", HBA_IDLEN); 2448 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2449 cpi->unit_number = cam_sim_unit(sim); 2450 cpi->bus_id = cam_sim_bus(sim); 2451 cpi->base_transfer_speed = 100; /* XXX WTF? */ 2452 cpi->protocol = PROTO_MMCSD; 2453 cpi->protocol_version = SCSI_REV_0; 2454 cpi->transport = XPORT_MMCSD; 2455 cpi->transport_version = 0; 2456 2457 cpi->ccb_h.status = CAM_REQ_CMP; 2458 break; 2459 } 2460 case XPT_GET_TRAN_SETTINGS: 2461 { 2462 struct ccb_trans_settings *cts = &ccb->cts; 2463 2464 if (sdhci_debug > 1) 2465 slot_printf(slot, "Got XPT_GET_TRAN_SETTINGS\n"); 2466 2467 cts->protocol = PROTO_MMCSD; 2468 cts->protocol_version = 1; 2469 cts->transport = XPORT_MMCSD; 2470 cts->transport_version = 1; 2471 cts->xport_specific.valid = 0; 2472 cts->proto_specific.mmc.host_ocr = slot->host.host_ocr; 2473 cts->proto_specific.mmc.host_f_min = slot->host.f_min; 2474 cts->proto_specific.mmc.host_f_max = slot->host.f_max; 2475 cts->proto_specific.mmc.host_caps = slot->host.caps; 2476 memcpy(&cts->proto_specific.mmc.ios, &slot->host.ios, sizeof(struct mmc_ios)); 2477 ccb->ccb_h.status = CAM_REQ_CMP; 2478 break; 2479 } 2480 case XPT_SET_TRAN_SETTINGS: 2481 { 2482 if (sdhci_debug > 1) 2483 slot_printf(slot, "Got XPT_SET_TRAN_SETTINGS\n"); 2484 sdhci_cam_settran_settings(slot, ccb); 2485 ccb->ccb_h.status = CAM_REQ_CMP; 2486 break; 2487 } 2488 case XPT_RESET_BUS: 2489 if (sdhci_debug > 1) 2490 slot_printf(slot, "Got XPT_RESET_BUS, ACK it...\n"); 2491 ccb->ccb_h.status = CAM_REQ_CMP; 2492 break; 2493 case XPT_MMC_IO: 2494 /* 2495 * Here is the HW-dependent part of 2496 * sending the command to the underlying h/w 2497 * At some point in the future an interrupt comes. 2498 * Then the request will be marked as completed. 2499 */ 2500 if (__predict_false(sdhci_debug > 1)) 2501 slot_printf(slot, "Got XPT_MMC_IO\n"); 2502 ccb->ccb_h.status = CAM_REQ_INPROG; 2503 2504 sdhci_cam_handle_mmcio(sim, ccb); 2505 return; 2506 /* NOTREACHED */ 2507 break; 2508 default: 2509 ccb->ccb_h.status = CAM_REQ_INVALID; 2510 break; 2511 } 2512 xpt_done(ccb); 2513 return; 2514 } 2515 2516 void 2517 sdhci_cam_poll(struct cam_sim *sim) 2518 { 2519 return; 2520 } 2521 2522 int sdhci_cam_get_possible_host_clock(struct sdhci_slot *slot, int proposed_clock) { 2523 int max_clock, clock, i; 2524 2525 if (proposed_clock == 0) 2526 return 0; 2527 max_clock = slot->max_clk; 2528 clock = max_clock; 2529 2530 if (slot->version < SDHCI_SPEC_300) { 2531 for (i = 0; i < SDHCI_200_MAX_DIVIDER; 2532 i <<= 1) { 2533 if (clock <= proposed_clock) 2534 break; 2535 clock >>= 1; 2536 } 2537 } else { 2538 for (i = 0; i < SDHCI_300_MAX_DIVIDER; 2539 i += 2) { 2540 if (clock <= proposed_clock) 2541 break; 2542 clock = max_clock / (i + 2); 2543 } 2544 } 2545 return clock; 2546 } 2547 2548 int 2549 sdhci_cam_settran_settings(struct sdhci_slot *slot, union ccb *ccb) 2550 { 2551 struct mmc_ios *ios; 2552 struct mmc_ios *new_ios; 2553 struct ccb_trans_settings_mmc *cts; 2554 2555 ios = &slot->host.ios; 2556 2557 cts = &ccb->cts.proto_specific.mmc; 2558 new_ios = &cts->ios; 2559 2560 /* Update only requested fields */ 2561 if (cts->ios_valid & MMC_CLK) { 2562 ios->clock = sdhci_cam_get_possible_host_clock(slot, new_ios->clock); 2563 slot_printf(slot, "Clock => %d\n", ios->clock); 2564 } 2565 if (cts->ios_valid & MMC_VDD) { 2566 ios->vdd = new_ios->vdd; 2567 slot_printf(slot, "VDD => %d\n", ios->vdd); 2568 } 2569 if (cts->ios_valid & MMC_CS) { 2570 ios->chip_select = new_ios->chip_select; 2571 slot_printf(slot, "CS => %d\n", ios->chip_select); 2572 } 2573 if (cts->ios_valid & MMC_BW) { 2574 ios->bus_width = new_ios->bus_width; 2575 slot_printf(slot, "Bus width => %d\n", ios->bus_width); 2576 } 2577 if (cts->ios_valid & MMC_PM) { 2578 ios->power_mode = new_ios->power_mode; 2579 slot_printf(slot, "Power mode => %d\n", ios->power_mode); 2580 } 2581 if (cts->ios_valid & MMC_BT) { 2582 ios->timing = new_ios->timing; 2583 slot_printf(slot, "Timing => %d\n", ios->timing); 2584 } 2585 if (cts->ios_valid & MMC_BM) { 2586 ios->bus_mode = new_ios->bus_mode; 2587 slot_printf(slot, "Bus mode => %d\n", ios->bus_mode); 2588 } 2589 2590 /* XXX Provide a way to call a chip-specific IOS update, required for TI */ 2591 return (sdhci_cam_update_ios(slot)); 2592 } 2593 2594 int 2595 sdhci_cam_update_ios(struct sdhci_slot *slot) 2596 { 2597 struct mmc_ios *ios = &slot->host.ios; 2598 2599 slot_printf(slot, "%s: power_mode=%d, clk=%d, bus_width=%d, timing=%d\n", 2600 __func__, ios->power_mode, ios->clock, ios->bus_width, ios->timing); 2601 SDHCI_LOCK(slot); 2602 /* Do full reset on bus power down to clear from any state. */ 2603 if (ios->power_mode == power_off) { 2604 WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 2605 sdhci_init(slot); 2606 } 2607 /* Configure the bus. */ 2608 sdhci_set_clock(slot, ios->clock); 2609 sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 2610 if (ios->bus_width == bus_width_8) { 2611 slot->hostctrl |= SDHCI_CTRL_8BITBUS; 2612 slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2613 } else if (ios->bus_width == bus_width_4) { 2614 slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2615 slot->hostctrl |= SDHCI_CTRL_4BITBUS; 2616 } else if (ios->bus_width == bus_width_1) { 2617 slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 2618 slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 2619 } else { 2620 panic("Invalid bus width: %d", ios->bus_width); 2621 } 2622 if (ios->timing == bus_timing_hs && 2623 !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 2624 slot->hostctrl |= SDHCI_CTRL_HISPD; 2625 else 2626 slot->hostctrl &= ~SDHCI_CTRL_HISPD; 2627 WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 2628 /* Some controllers like reset after bus changes. */ 2629 if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 2630 sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 2631 2632 SDHCI_UNLOCK(slot); 2633 return (0); 2634 } 2635 2636 int 2637 sdhci_cam_request(struct sdhci_slot *slot, union ccb *ccb) 2638 { 2639 struct ccb_mmcio *mmcio; 2640 2641 mmcio = &ccb->mmcio; 2642 2643 SDHCI_LOCK(slot); 2644 /* if (slot->req != NULL) { 2645 SDHCI_UNLOCK(slot); 2646 return (EBUSY); 2647 } 2648 */ 2649 if (__predict_false(sdhci_debug > 1)) { 2650 slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 2651 mmcio->cmd.opcode, mmcio->cmd.arg, mmcio->cmd.flags, 2652 mmcio->cmd.data != NULL ? (unsigned int) mmcio->cmd.data->len : 0, 2653 mmcio->cmd.data != NULL ? mmcio->cmd.data->flags: 0); 2654 } 2655 if (mmcio->cmd.data != NULL) { 2656 if (mmcio->cmd.data->len == 0 || mmcio->cmd.data->flags == 0) 2657 panic("data->len = %d, data->flags = %d -- something is b0rked", 2658 (int)mmcio->cmd.data->len, mmcio->cmd.data->flags); 2659 } 2660 slot->ccb = ccb; 2661 slot->flags = 0; 2662 sdhci_start(slot); 2663 SDHCI_UNLOCK(slot); 2664 if (dumping) { 2665 while (slot->ccb != NULL) { 2666 sdhci_generic_intr(slot); 2667 DELAY(10); 2668 } 2669 } 2670 return (0); 2671 } 2672 #endif /* MMCCAM */ 2673 2674 MODULE_VERSION(sdhci, 1); 2675