1 /*- 2 * Copyright (c) 2008 Alexander Motin <mav@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 #include <sys/callout.h> 33 #include <sys/conf.h> 34 #include <sys/kernel.h> 35 #include <sys/lock.h> 36 #include <sys/module.h> 37 #include <sys/mutex.h> 38 #include <sys/resource.h> 39 #include <sys/rman.h> 40 #include <sys/sysctl.h> 41 #include <sys/taskqueue.h> 42 43 #include <machine/bus.h> 44 #include <machine/resource.h> 45 #include <machine/stdarg.h> 46 47 #include <dev/mmc/bridge.h> 48 #include <dev/mmc/mmcreg.h> 49 #include <dev/mmc/mmcbrvar.h> 50 51 #include "mmcbr_if.h" 52 #include "sdhci.h" 53 #include "sdhci_if.h" 54 55 SYSCTL_NODE(_hw, OID_AUTO, sdhci, CTLFLAG_RD, 0, "sdhci driver"); 56 57 static int sdhci_debug; 58 SYSCTL_INT(_hw_sdhci, OID_AUTO, debug, CTLFLAG_RWTUN, &sdhci_debug, 0, "Debug level"); 59 60 #define RD1(slot, off) SDHCI_READ_1((slot)->bus, (slot), (off)) 61 #define RD2(slot, off) SDHCI_READ_2((slot)->bus, (slot), (off)) 62 #define RD4(slot, off) SDHCI_READ_4((slot)->bus, (slot), (off)) 63 #define RD_MULTI_4(slot, off, ptr, count) \ 64 SDHCI_READ_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 65 66 #define WR1(slot, off, val) SDHCI_WRITE_1((slot)->bus, (slot), (off), (val)) 67 #define WR2(slot, off, val) SDHCI_WRITE_2((slot)->bus, (slot), (off), (val)) 68 #define WR4(slot, off, val) SDHCI_WRITE_4((slot)->bus, (slot), (off), (val)) 69 #define WR_MULTI_4(slot, off, ptr, count) \ 70 SDHCI_WRITE_MULTI_4((slot)->bus, (slot), (off), (ptr), (count)) 71 72 static void sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock); 73 static void sdhci_start(struct sdhci_slot *slot); 74 static void sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data); 75 76 static void sdhci_card_task(void *, int); 77 78 /* helper routines */ 79 #define SDHCI_LOCK(_slot) mtx_lock(&(_slot)->mtx) 80 #define SDHCI_UNLOCK(_slot) mtx_unlock(&(_slot)->mtx) 81 #define SDHCI_LOCK_INIT(_slot) \ 82 mtx_init(&_slot->mtx, "SD slot mtx", "sdhci", MTX_DEF) 83 #define SDHCI_LOCK_DESTROY(_slot) mtx_destroy(&_slot->mtx); 84 #define SDHCI_ASSERT_LOCKED(_slot) mtx_assert(&_slot->mtx, MA_OWNED); 85 #define SDHCI_ASSERT_UNLOCKED(_slot) mtx_assert(&_slot->mtx, MA_NOTOWNED); 86 87 #define SDHCI_DEFAULT_MAX_FREQ 50 88 89 #define SDHCI_200_MAX_DIVIDER 256 90 #define SDHCI_300_MAX_DIVIDER 2046 91 92 static void 93 sdhci_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 94 { 95 if (error != 0) { 96 printf("getaddr: error %d\n", error); 97 return; 98 } 99 *(bus_addr_t *)arg = segs[0].ds_addr; 100 } 101 102 static int 103 slot_printf(struct sdhci_slot *slot, const char * fmt, ...) 104 { 105 va_list ap; 106 int retval; 107 108 retval = printf("%s-slot%d: ", 109 device_get_nameunit(slot->bus), slot->num); 110 111 va_start(ap, fmt); 112 retval += vprintf(fmt, ap); 113 va_end(ap); 114 return (retval); 115 } 116 117 static void 118 sdhci_dumpregs(struct sdhci_slot *slot) 119 { 120 slot_printf(slot, 121 "============== REGISTER DUMP ==============\n"); 122 123 slot_printf(slot, "Sys addr: 0x%08x | Version: 0x%08x\n", 124 RD4(slot, SDHCI_DMA_ADDRESS), RD2(slot, SDHCI_HOST_VERSION)); 125 slot_printf(slot, "Blk size: 0x%08x | Blk cnt: 0x%08x\n", 126 RD2(slot, SDHCI_BLOCK_SIZE), RD2(slot, SDHCI_BLOCK_COUNT)); 127 slot_printf(slot, "Argument: 0x%08x | Trn mode: 0x%08x\n", 128 RD4(slot, SDHCI_ARGUMENT), RD2(slot, SDHCI_TRANSFER_MODE)); 129 slot_printf(slot, "Present: 0x%08x | Host ctl: 0x%08x\n", 130 RD4(slot, SDHCI_PRESENT_STATE), RD1(slot, SDHCI_HOST_CONTROL)); 131 slot_printf(slot, "Power: 0x%08x | Blk gap: 0x%08x\n", 132 RD1(slot, SDHCI_POWER_CONTROL), RD1(slot, SDHCI_BLOCK_GAP_CONTROL)); 133 slot_printf(slot, "Wake-up: 0x%08x | Clock: 0x%08x\n", 134 RD1(slot, SDHCI_WAKE_UP_CONTROL), RD2(slot, SDHCI_CLOCK_CONTROL)); 135 slot_printf(slot, "Timeout: 0x%08x | Int stat: 0x%08x\n", 136 RD1(slot, SDHCI_TIMEOUT_CONTROL), RD4(slot, SDHCI_INT_STATUS)); 137 slot_printf(slot, "Int enab: 0x%08x | Sig enab: 0x%08x\n", 138 RD4(slot, SDHCI_INT_ENABLE), RD4(slot, SDHCI_SIGNAL_ENABLE)); 139 slot_printf(slot, "AC12 err: 0x%08x | Slot int: 0x%08x\n", 140 RD2(slot, SDHCI_ACMD12_ERR), RD2(slot, SDHCI_SLOT_INT_STATUS)); 141 slot_printf(slot, "Caps: 0x%08x | Max curr: 0x%08x\n", 142 RD4(slot, SDHCI_CAPABILITIES), RD4(slot, SDHCI_MAX_CURRENT)); 143 144 slot_printf(slot, 145 "===========================================\n"); 146 } 147 148 static void 149 sdhci_reset(struct sdhci_slot *slot, uint8_t mask) 150 { 151 int timeout; 152 153 if (slot->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) { 154 if (!(RD4(slot, SDHCI_PRESENT_STATE) & 155 SDHCI_CARD_PRESENT)) 156 return; 157 } 158 159 /* Some controllers need this kick or reset won't work. */ 160 if ((mask & SDHCI_RESET_ALL) == 0 && 161 (slot->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)) { 162 uint32_t clock; 163 164 /* This is to force an update */ 165 clock = slot->clock; 166 slot->clock = 0; 167 sdhci_set_clock(slot, clock); 168 } 169 170 if (mask & SDHCI_RESET_ALL) { 171 slot->clock = 0; 172 slot->power = 0; 173 } 174 175 WR1(slot, SDHCI_SOFTWARE_RESET, mask); 176 177 if (slot->quirks & SDHCI_QUIRK_WAITFOR_RESET_ASSERTED) { 178 /* 179 * Resets on TI OMAPs and AM335x are incompatible with SDHCI 180 * specification. The reset bit has internal propagation delay, 181 * so a fast read after write returns 0 even if reset process is 182 * in progress. The workaround is to poll for 1 before polling 183 * for 0. In the worst case, if we miss seeing it asserted the 184 * time we spent waiting is enough to ensure the reset finishes. 185 */ 186 timeout = 10000; 187 while ((RD1(slot, SDHCI_SOFTWARE_RESET) & mask) != mask) { 188 if (timeout <= 0) 189 break; 190 timeout--; 191 DELAY(1); 192 } 193 } 194 195 /* Wait max 100 ms */ 196 timeout = 10000; 197 /* Controller clears the bits when it's done */ 198 while (RD1(slot, SDHCI_SOFTWARE_RESET) & mask) { 199 if (timeout <= 0) { 200 slot_printf(slot, "Reset 0x%x never completed.\n", 201 mask); 202 sdhci_dumpregs(slot); 203 return; 204 } 205 timeout--; 206 DELAY(10); 207 } 208 } 209 210 static void 211 sdhci_init(struct sdhci_slot *slot) 212 { 213 214 sdhci_reset(slot, SDHCI_RESET_ALL); 215 216 /* Enable interrupts. */ 217 slot->intmask = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT | 218 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT | SDHCI_INT_INDEX | 219 SDHCI_INT_END_BIT | SDHCI_INT_CRC | SDHCI_INT_TIMEOUT | 220 SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT | 221 SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 222 SDHCI_INT_DMA_END | SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE | 223 SDHCI_INT_ACMD12ERR; 224 WR4(slot, SDHCI_INT_ENABLE, slot->intmask); 225 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 226 } 227 228 static void 229 sdhci_set_clock(struct sdhci_slot *slot, uint32_t clock) 230 { 231 uint32_t res; 232 uint16_t clk; 233 uint16_t div; 234 int timeout; 235 236 if (clock == slot->clock) 237 return; 238 slot->clock = clock; 239 240 /* Turn off the clock. */ 241 clk = RD2(slot, SDHCI_CLOCK_CONTROL); 242 WR2(slot, SDHCI_CLOCK_CONTROL, clk & ~SDHCI_CLOCK_CARD_EN); 243 /* If no clock requested - left it so. */ 244 if (clock == 0) 245 return; 246 247 /* Recalculate timeout clock frequency based on the new sd clock. */ 248 if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) 249 slot->timeout_clk = slot->clock / 1000; 250 251 if (slot->version < SDHCI_SPEC_300) { 252 /* Looking for highest freq <= clock. */ 253 res = slot->max_clk; 254 for (div = 1; div < SDHCI_200_MAX_DIVIDER; div <<= 1) { 255 if (res <= clock) 256 break; 257 res >>= 1; 258 } 259 /* Divider 1:1 is 0x00, 2:1 is 0x01, 256:1 is 0x80 ... */ 260 div >>= 1; 261 } 262 else { 263 /* Version 3.0 divisors are multiples of two up to 1023*2 */ 264 if (clock >= slot->max_clk) 265 div = 0; 266 else { 267 for (div = 2; div < SDHCI_300_MAX_DIVIDER; div += 2) { 268 if ((slot->max_clk / div) <= clock) 269 break; 270 } 271 } 272 div >>= 1; 273 } 274 275 if (bootverbose || sdhci_debug) 276 slot_printf(slot, "Divider %d for freq %d (max %d)\n", 277 div, clock, slot->max_clk); 278 279 /* Now we have got divider, set it. */ 280 clk = (div & SDHCI_DIVIDER_MASK) << SDHCI_DIVIDER_SHIFT; 281 clk |= ((div >> SDHCI_DIVIDER_MASK_LEN) & SDHCI_DIVIDER_HI_MASK) 282 << SDHCI_DIVIDER_HI_SHIFT; 283 284 WR2(slot, SDHCI_CLOCK_CONTROL, clk); 285 /* Enable clock. */ 286 clk |= SDHCI_CLOCK_INT_EN; 287 WR2(slot, SDHCI_CLOCK_CONTROL, clk); 288 /* Wait up to 10 ms until it stabilize. */ 289 timeout = 10; 290 while (!((clk = RD2(slot, SDHCI_CLOCK_CONTROL)) 291 & SDHCI_CLOCK_INT_STABLE)) { 292 if (timeout == 0) { 293 slot_printf(slot, 294 "Internal clock never stabilised.\n"); 295 sdhci_dumpregs(slot); 296 return; 297 } 298 timeout--; 299 DELAY(1000); 300 } 301 /* Pass clock signal to the bus. */ 302 clk |= SDHCI_CLOCK_CARD_EN; 303 WR2(slot, SDHCI_CLOCK_CONTROL, clk); 304 } 305 306 static void 307 sdhci_set_power(struct sdhci_slot *slot, u_char power) 308 { 309 uint8_t pwr; 310 311 if (slot->power == power) 312 return; 313 314 slot->power = power; 315 316 /* Turn off the power. */ 317 pwr = 0; 318 WR1(slot, SDHCI_POWER_CONTROL, pwr); 319 /* If power down requested - left it so. */ 320 if (power == 0) 321 return; 322 /* Set voltage. */ 323 switch (1 << power) { 324 case MMC_OCR_LOW_VOLTAGE: 325 pwr |= SDHCI_POWER_180; 326 break; 327 case MMC_OCR_290_300: 328 case MMC_OCR_300_310: 329 pwr |= SDHCI_POWER_300; 330 break; 331 case MMC_OCR_320_330: 332 case MMC_OCR_330_340: 333 pwr |= SDHCI_POWER_330; 334 break; 335 } 336 WR1(slot, SDHCI_POWER_CONTROL, pwr); 337 /* Turn on the power. */ 338 pwr |= SDHCI_POWER_ON; 339 WR1(slot, SDHCI_POWER_CONTROL, pwr); 340 } 341 342 static void 343 sdhci_read_block_pio(struct sdhci_slot *slot) 344 { 345 uint32_t data; 346 char *buffer; 347 size_t left; 348 349 buffer = slot->curcmd->data->data; 350 buffer += slot->offset; 351 /* Transfer one block at a time. */ 352 left = min(512, slot->curcmd->data->len - slot->offset); 353 slot->offset += left; 354 355 /* If we are too fast, broken controllers return zeroes. */ 356 if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) 357 DELAY(10); 358 /* Handle unaligned and aligned buffer cases. */ 359 if ((intptr_t)buffer & 3) { 360 while (left > 3) { 361 data = RD4(slot, SDHCI_BUFFER); 362 buffer[0] = data; 363 buffer[1] = (data >> 8); 364 buffer[2] = (data >> 16); 365 buffer[3] = (data >> 24); 366 buffer += 4; 367 left -= 4; 368 } 369 } else { 370 RD_MULTI_4(slot, SDHCI_BUFFER, 371 (uint32_t *)buffer, left >> 2); 372 left &= 3; 373 } 374 /* Handle uneven size case. */ 375 if (left > 0) { 376 data = RD4(slot, SDHCI_BUFFER); 377 while (left > 0) { 378 *(buffer++) = data; 379 data >>= 8; 380 left--; 381 } 382 } 383 } 384 385 static void 386 sdhci_write_block_pio(struct sdhci_slot *slot) 387 { 388 uint32_t data = 0; 389 char *buffer; 390 size_t left; 391 392 buffer = slot->curcmd->data->data; 393 buffer += slot->offset; 394 /* Transfer one block at a time. */ 395 left = min(512, slot->curcmd->data->len - slot->offset); 396 slot->offset += left; 397 398 /* Handle unaligned and aligned buffer cases. */ 399 if ((intptr_t)buffer & 3) { 400 while (left > 3) { 401 data = buffer[0] + 402 (buffer[1] << 8) + 403 (buffer[2] << 16) + 404 (buffer[3] << 24); 405 left -= 4; 406 buffer += 4; 407 WR4(slot, SDHCI_BUFFER, data); 408 } 409 } else { 410 WR_MULTI_4(slot, SDHCI_BUFFER, 411 (uint32_t *)buffer, left >> 2); 412 left &= 3; 413 } 414 /* Handle uneven size case. */ 415 if (left > 0) { 416 while (left > 0) { 417 data <<= 8; 418 data += *(buffer++); 419 left--; 420 } 421 WR4(slot, SDHCI_BUFFER, data); 422 } 423 } 424 425 static void 426 sdhci_transfer_pio(struct sdhci_slot *slot) 427 { 428 429 /* Read as many blocks as possible. */ 430 if (slot->curcmd->data->flags & MMC_DATA_READ) { 431 while (RD4(slot, SDHCI_PRESENT_STATE) & 432 SDHCI_DATA_AVAILABLE) { 433 sdhci_read_block_pio(slot); 434 if (slot->offset >= slot->curcmd->data->len) 435 break; 436 } 437 } else { 438 while (RD4(slot, SDHCI_PRESENT_STATE) & 439 SDHCI_SPACE_AVAILABLE) { 440 sdhci_write_block_pio(slot); 441 if (slot->offset >= slot->curcmd->data->len) 442 break; 443 } 444 } 445 } 446 447 static void 448 sdhci_card_delay(void *arg) 449 { 450 struct sdhci_slot *slot = arg; 451 452 taskqueue_enqueue(taskqueue_swi_giant, &slot->card_task); 453 } 454 455 static void 456 sdhci_card_task(void *arg, int pending) 457 { 458 struct sdhci_slot *slot = arg; 459 460 SDHCI_LOCK(slot); 461 if (RD4(slot, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT) { 462 if (slot->dev == NULL) { 463 /* If card is present - attach mmc bus. */ 464 slot->dev = device_add_child(slot->bus, "mmc", -1); 465 device_set_ivars(slot->dev, slot); 466 SDHCI_UNLOCK(slot); 467 device_probe_and_attach(slot->dev); 468 } else 469 SDHCI_UNLOCK(slot); 470 } else { 471 if (slot->dev != NULL) { 472 /* If no card present - detach mmc bus. */ 473 device_t d = slot->dev; 474 slot->dev = NULL; 475 SDHCI_UNLOCK(slot); 476 device_delete_child(slot->bus, d); 477 } else 478 SDHCI_UNLOCK(slot); 479 } 480 } 481 482 int 483 sdhci_init_slot(device_t dev, struct sdhci_slot *slot, int num) 484 { 485 uint32_t caps, freq; 486 int err; 487 488 SDHCI_LOCK_INIT(slot); 489 slot->num = num; 490 slot->bus = dev; 491 492 /* Allocate DMA tag. */ 493 err = bus_dma_tag_create(bus_get_dma_tag(dev), 494 DMA_BLOCK_SIZE, 0, BUS_SPACE_MAXADDR_32BIT, 495 BUS_SPACE_MAXADDR, NULL, NULL, 496 DMA_BLOCK_SIZE, 1, DMA_BLOCK_SIZE, 497 BUS_DMA_ALLOCNOW, NULL, NULL, 498 &slot->dmatag); 499 if (err != 0) { 500 device_printf(dev, "Can't create DMA tag\n"); 501 SDHCI_LOCK_DESTROY(slot); 502 return (err); 503 } 504 /* Allocate DMA memory. */ 505 err = bus_dmamem_alloc(slot->dmatag, (void **)&slot->dmamem, 506 BUS_DMA_NOWAIT, &slot->dmamap); 507 if (err != 0) { 508 device_printf(dev, "Can't alloc DMA memory\n"); 509 SDHCI_LOCK_DESTROY(slot); 510 return (err); 511 } 512 /* Map the memory. */ 513 err = bus_dmamap_load(slot->dmatag, slot->dmamap, 514 (void *)slot->dmamem, DMA_BLOCK_SIZE, 515 sdhci_getaddr, &slot->paddr, 0); 516 if (err != 0 || slot->paddr == 0) { 517 device_printf(dev, "Can't load DMA memory\n"); 518 SDHCI_LOCK_DESTROY(slot); 519 if(err) 520 return (err); 521 else 522 return (EFAULT); 523 } 524 525 /* Initialize slot. */ 526 sdhci_init(slot); 527 slot->version = (RD2(slot, SDHCI_HOST_VERSION) 528 >> SDHCI_SPEC_VER_SHIFT) & SDHCI_SPEC_VER_MASK; 529 if (slot->quirks & SDHCI_QUIRK_MISSING_CAPS) 530 caps = slot->caps; 531 else 532 caps = RD4(slot, SDHCI_CAPABILITIES); 533 /* Calculate base clock frequency. */ 534 if (slot->version >= SDHCI_SPEC_300) 535 freq = (caps & SDHCI_CLOCK_V3_BASE_MASK) >> 536 SDHCI_CLOCK_BASE_SHIFT; 537 else 538 freq = (caps & SDHCI_CLOCK_BASE_MASK) >> 539 SDHCI_CLOCK_BASE_SHIFT; 540 if (freq != 0) 541 slot->max_clk = freq * 1000000; 542 /* 543 * If the frequency wasn't in the capabilities and the hardware driver 544 * hasn't already set max_clk we're probably not going to work right 545 * with an assumption, so complain about it. 546 */ 547 if (slot->max_clk == 0) { 548 slot->max_clk = SDHCI_DEFAULT_MAX_FREQ * 1000000; 549 device_printf(dev, "Hardware doesn't specify base clock " 550 "frequency, using %dMHz as default.\n", SDHCI_DEFAULT_MAX_FREQ); 551 } 552 /* Calculate timeout clock frequency. */ 553 if (slot->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK) { 554 slot->timeout_clk = slot->max_clk / 1000; 555 } else { 556 slot->timeout_clk = 557 (caps & SDHCI_TIMEOUT_CLK_MASK) >> SDHCI_TIMEOUT_CLK_SHIFT; 558 if (caps & SDHCI_TIMEOUT_CLK_UNIT) 559 slot->timeout_clk *= 1000; 560 } 561 /* 562 * If the frequency wasn't in the capabilities and the hardware driver 563 * hasn't already set timeout_clk we'll probably work okay using the 564 * max timeout, but still mention it. 565 */ 566 if (slot->timeout_clk == 0) { 567 device_printf(dev, "Hardware doesn't specify timeout clock " 568 "frequency, setting BROKEN_TIMEOUT quirk.\n"); 569 slot->quirks |= SDHCI_QUIRK_BROKEN_TIMEOUT_VAL; 570 } 571 572 slot->host.f_min = SDHCI_MIN_FREQ(slot->bus, slot); 573 slot->host.f_max = slot->max_clk; 574 slot->host.host_ocr = 0; 575 if (caps & SDHCI_CAN_VDD_330) 576 slot->host.host_ocr |= MMC_OCR_320_330 | MMC_OCR_330_340; 577 if (caps & SDHCI_CAN_VDD_300) 578 slot->host.host_ocr |= MMC_OCR_290_300 | MMC_OCR_300_310; 579 if (caps & SDHCI_CAN_VDD_180) 580 slot->host.host_ocr |= MMC_OCR_LOW_VOLTAGE; 581 if (slot->host.host_ocr == 0) { 582 device_printf(dev, "Hardware doesn't report any " 583 "support voltages.\n"); 584 } 585 slot->host.caps = MMC_CAP_4_BIT_DATA; 586 if (caps & SDHCI_CAN_DO_8BITBUS) 587 slot->host.caps |= MMC_CAP_8_BIT_DATA; 588 if (caps & SDHCI_CAN_DO_HISPD) 589 slot->host.caps |= MMC_CAP_HSPEED; 590 /* Decide if we have usable DMA. */ 591 if (caps & SDHCI_CAN_DO_DMA) 592 slot->opt |= SDHCI_HAVE_DMA; 593 594 if (slot->quirks & SDHCI_QUIRK_BROKEN_DMA) 595 slot->opt &= ~SDHCI_HAVE_DMA; 596 if (slot->quirks & SDHCI_QUIRK_FORCE_DMA) 597 slot->opt |= SDHCI_HAVE_DMA; 598 599 /* 600 * Use platform-provided transfer backend 601 * with PIO as a fallback mechanism 602 */ 603 if (slot->opt & SDHCI_PLATFORM_TRANSFER) 604 slot->opt &= ~SDHCI_HAVE_DMA; 605 606 if (bootverbose || sdhci_debug) { 607 slot_printf(slot, "%uMHz%s %s%s%s%s %s\n", 608 slot->max_clk / 1000000, 609 (caps & SDHCI_CAN_DO_HISPD) ? " HS" : "", 610 (caps & MMC_CAP_8_BIT_DATA) ? "8bits" : 611 ((caps & MMC_CAP_4_BIT_DATA) ? "4bits" : "1bit"), 612 (caps & SDHCI_CAN_VDD_330) ? " 3.3V" : "", 613 (caps & SDHCI_CAN_VDD_300) ? " 3.0V" : "", 614 (caps & SDHCI_CAN_VDD_180) ? " 1.8V" : "", 615 (slot->opt & SDHCI_HAVE_DMA) ? "DMA" : "PIO"); 616 sdhci_dumpregs(slot); 617 } 618 619 slot->timeout = 10; 620 SYSCTL_ADD_INT(device_get_sysctl_ctx(slot->bus), 621 SYSCTL_CHILDREN(device_get_sysctl_tree(slot->bus)), OID_AUTO, 622 "timeout", CTLFLAG_RW, &slot->timeout, 0, 623 "Maximum timeout for SDHCI transfers (in secs)"); 624 TASK_INIT(&slot->card_task, 0, sdhci_card_task, slot); 625 callout_init(&slot->card_callout, 1); 626 callout_init_mtx(&slot->timeout_callout, &slot->mtx, 0); 627 628 return (0); 629 } 630 631 void 632 sdhci_start_slot(struct sdhci_slot *slot) 633 { 634 sdhci_card_task(slot, 0); 635 } 636 637 int 638 sdhci_cleanup_slot(struct sdhci_slot *slot) 639 { 640 device_t d; 641 642 callout_drain(&slot->timeout_callout); 643 callout_drain(&slot->card_callout); 644 taskqueue_drain(taskqueue_swi_giant, &slot->card_task); 645 646 SDHCI_LOCK(slot); 647 d = slot->dev; 648 slot->dev = NULL; 649 SDHCI_UNLOCK(slot); 650 if (d != NULL) 651 device_delete_child(slot->bus, d); 652 653 SDHCI_LOCK(slot); 654 sdhci_reset(slot, SDHCI_RESET_ALL); 655 SDHCI_UNLOCK(slot); 656 bus_dmamap_unload(slot->dmatag, slot->dmamap); 657 bus_dmamem_free(slot->dmatag, slot->dmamem, slot->dmamap); 658 bus_dma_tag_destroy(slot->dmatag); 659 660 SDHCI_LOCK_DESTROY(slot); 661 662 return (0); 663 } 664 665 int 666 sdhci_generic_suspend(struct sdhci_slot *slot) 667 { 668 sdhci_reset(slot, SDHCI_RESET_ALL); 669 670 return (0); 671 } 672 673 int 674 sdhci_generic_resume(struct sdhci_slot *slot) 675 { 676 sdhci_init(slot); 677 678 return (0); 679 } 680 681 uint32_t 682 sdhci_generic_min_freq(device_t brdev, struct sdhci_slot *slot) 683 { 684 if (slot->version >= SDHCI_SPEC_300) 685 return (slot->max_clk / SDHCI_300_MAX_DIVIDER); 686 else 687 return (slot->max_clk / SDHCI_200_MAX_DIVIDER); 688 } 689 690 int 691 sdhci_generic_update_ios(device_t brdev, device_t reqdev) 692 { 693 struct sdhci_slot *slot = device_get_ivars(reqdev); 694 struct mmc_ios *ios = &slot->host.ios; 695 696 SDHCI_LOCK(slot); 697 /* Do full reset on bus power down to clear from any state. */ 698 if (ios->power_mode == power_off) { 699 WR4(slot, SDHCI_SIGNAL_ENABLE, 0); 700 sdhci_init(slot); 701 } 702 /* Configure the bus. */ 703 sdhci_set_clock(slot, ios->clock); 704 sdhci_set_power(slot, (ios->power_mode == power_off) ? 0 : ios->vdd); 705 if (ios->bus_width == bus_width_8) { 706 slot->hostctrl |= SDHCI_CTRL_8BITBUS; 707 slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 708 } else if (ios->bus_width == bus_width_4) { 709 slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 710 slot->hostctrl |= SDHCI_CTRL_4BITBUS; 711 } else if (ios->bus_width == bus_width_1) { 712 slot->hostctrl &= ~SDHCI_CTRL_8BITBUS; 713 slot->hostctrl &= ~SDHCI_CTRL_4BITBUS; 714 } else { 715 panic("Invalid bus width: %d", ios->bus_width); 716 } 717 if (ios->timing == bus_timing_hs && 718 !(slot->quirks & SDHCI_QUIRK_DONT_SET_HISPD_BIT)) 719 slot->hostctrl |= SDHCI_CTRL_HISPD; 720 else 721 slot->hostctrl &= ~SDHCI_CTRL_HISPD; 722 WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl); 723 /* Some controllers like reset after bus changes. */ 724 if(slot->quirks & SDHCI_QUIRK_RESET_ON_IOS) 725 sdhci_reset(slot, SDHCI_RESET_CMD | SDHCI_RESET_DATA); 726 727 SDHCI_UNLOCK(slot); 728 return (0); 729 } 730 731 static void 732 sdhci_req_done(struct sdhci_slot *slot) 733 { 734 struct mmc_request *req; 735 736 if (slot->req != NULL && slot->curcmd != NULL) { 737 callout_stop(&slot->timeout_callout); 738 req = slot->req; 739 slot->req = NULL; 740 slot->curcmd = NULL; 741 req->done(req); 742 } 743 } 744 745 static void 746 sdhci_timeout(void *arg) 747 { 748 struct sdhci_slot *slot = arg; 749 750 if (slot->curcmd != NULL) { 751 slot_printf(slot, " Controller timeout\n"); 752 sdhci_dumpregs(slot); 753 sdhci_reset(slot, SDHCI_RESET_CMD|SDHCI_RESET_DATA); 754 slot->curcmd->error = MMC_ERR_TIMEOUT; 755 sdhci_req_done(slot); 756 } else { 757 slot_printf(slot, " Spurious timeout - no active command\n"); 758 } 759 } 760 761 static void 762 sdhci_set_transfer_mode(struct sdhci_slot *slot, 763 struct mmc_data *data) 764 { 765 uint16_t mode; 766 767 if (data == NULL) 768 return; 769 770 mode = SDHCI_TRNS_BLK_CNT_EN; 771 if (data->len > 512) 772 mode |= SDHCI_TRNS_MULTI; 773 if (data->flags & MMC_DATA_READ) 774 mode |= SDHCI_TRNS_READ; 775 if (slot->req->stop) 776 mode |= SDHCI_TRNS_ACMD12; 777 if (slot->flags & SDHCI_USE_DMA) 778 mode |= SDHCI_TRNS_DMA; 779 780 WR2(slot, SDHCI_TRANSFER_MODE, mode); 781 } 782 783 static void 784 sdhci_start_command(struct sdhci_slot *slot, struct mmc_command *cmd) 785 { 786 int flags, timeout; 787 uint32_t mask, state; 788 789 slot->curcmd = cmd; 790 slot->cmd_done = 0; 791 792 cmd->error = MMC_ERR_NONE; 793 794 /* This flags combination is not supported by controller. */ 795 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) { 796 slot_printf(slot, "Unsupported response type!\n"); 797 cmd->error = MMC_ERR_FAILED; 798 sdhci_req_done(slot); 799 return; 800 } 801 802 /* Read controller present state. */ 803 state = RD4(slot, SDHCI_PRESENT_STATE); 804 /* Do not issue command if there is no card, clock or power. 805 * Controller will not detect timeout without clock active. */ 806 if ((state & SDHCI_CARD_PRESENT) == 0 || 807 slot->power == 0 || 808 slot->clock == 0) { 809 cmd->error = MMC_ERR_FAILED; 810 sdhci_req_done(slot); 811 return; 812 } 813 /* Always wait for free CMD bus. */ 814 mask = SDHCI_CMD_INHIBIT; 815 /* Wait for free DAT if we have data or busy signal. */ 816 if (cmd->data || (cmd->flags & MMC_RSP_BUSY)) 817 mask |= SDHCI_DAT_INHIBIT; 818 /* We shouldn't wait for DAT for stop commands. */ 819 if (cmd == slot->req->stop) 820 mask &= ~SDHCI_DAT_INHIBIT; 821 /* 822 * Wait for bus no more then 250 ms. Typically there will be no wait 823 * here at all, but when writing a crash dump we may be bypassing the 824 * host platform's interrupt handler, and in some cases that handler 825 * may be working around hardware quirks such as not respecting r1b 826 * busy indications. In those cases, this wait-loop serves the purpose 827 * of waiting for the prior command and data transfers to be done, and 828 * SD cards are allowed to take up to 250ms for write and erase ops. 829 * (It's usually more like 20-30ms in the real world.) 830 */ 831 timeout = 250; 832 while (state & mask) { 833 if (timeout == 0) { 834 slot_printf(slot, "Controller never released " 835 "inhibit bit(s).\n"); 836 sdhci_dumpregs(slot); 837 cmd->error = MMC_ERR_FAILED; 838 sdhci_req_done(slot); 839 return; 840 } 841 timeout--; 842 DELAY(1000); 843 state = RD4(slot, SDHCI_PRESENT_STATE); 844 } 845 846 /* Prepare command flags. */ 847 if (!(cmd->flags & MMC_RSP_PRESENT)) 848 flags = SDHCI_CMD_RESP_NONE; 849 else if (cmd->flags & MMC_RSP_136) 850 flags = SDHCI_CMD_RESP_LONG; 851 else if (cmd->flags & MMC_RSP_BUSY) 852 flags = SDHCI_CMD_RESP_SHORT_BUSY; 853 else 854 flags = SDHCI_CMD_RESP_SHORT; 855 if (cmd->flags & MMC_RSP_CRC) 856 flags |= SDHCI_CMD_CRC; 857 if (cmd->flags & MMC_RSP_OPCODE) 858 flags |= SDHCI_CMD_INDEX; 859 if (cmd->data) 860 flags |= SDHCI_CMD_DATA; 861 if (cmd->opcode == MMC_STOP_TRANSMISSION) 862 flags |= SDHCI_CMD_TYPE_ABORT; 863 /* Prepare data. */ 864 sdhci_start_data(slot, cmd->data); 865 /* 866 * Interrupt aggregation: To reduce total number of interrupts 867 * group response interrupt with data interrupt when possible. 868 * If there going to be data interrupt, mask response one. 869 */ 870 if (slot->data_done == 0) { 871 WR4(slot, SDHCI_SIGNAL_ENABLE, 872 slot->intmask &= ~SDHCI_INT_RESPONSE); 873 } 874 /* Set command argument. */ 875 WR4(slot, SDHCI_ARGUMENT, cmd->arg); 876 /* Set data transfer mode. */ 877 sdhci_set_transfer_mode(slot, cmd->data); 878 /* Start command. */ 879 WR2(slot, SDHCI_COMMAND_FLAGS, (cmd->opcode << 8) | (flags & 0xff)); 880 /* Start timeout callout. */ 881 callout_reset(&slot->timeout_callout, slot->timeout * hz, 882 sdhci_timeout, slot); 883 } 884 885 static void 886 sdhci_finish_command(struct sdhci_slot *slot) 887 { 888 int i; 889 890 slot->cmd_done = 1; 891 /* Interrupt aggregation: Restore command interrupt. 892 * Main restore point for the case when command interrupt 893 * happened first. */ 894 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask |= SDHCI_INT_RESPONSE); 895 /* In case of error - reset host and return. */ 896 if (slot->curcmd->error) { 897 sdhci_reset(slot, SDHCI_RESET_CMD); 898 sdhci_reset(slot, SDHCI_RESET_DATA); 899 sdhci_start(slot); 900 return; 901 } 902 /* If command has response - fetch it. */ 903 if (slot->curcmd->flags & MMC_RSP_PRESENT) { 904 if (slot->curcmd->flags & MMC_RSP_136) { 905 /* CRC is stripped so we need one byte shift. */ 906 uint8_t extra = 0; 907 for (i = 0; i < 4; i++) { 908 uint32_t val = RD4(slot, SDHCI_RESPONSE + i * 4); 909 if (slot->quirks & SDHCI_QUIRK_DONT_SHIFT_RESPONSE) 910 slot->curcmd->resp[3 - i] = val; 911 else { 912 slot->curcmd->resp[3 - i] = 913 (val << 8) | extra; 914 extra = val >> 24; 915 } 916 } 917 } else 918 slot->curcmd->resp[0] = RD4(slot, SDHCI_RESPONSE); 919 } 920 /* If data ready - finish. */ 921 if (slot->data_done) 922 sdhci_start(slot); 923 } 924 925 static void 926 sdhci_start_data(struct sdhci_slot *slot, struct mmc_data *data) 927 { 928 uint32_t target_timeout, current_timeout; 929 uint8_t div; 930 931 if (data == NULL && (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 932 slot->data_done = 1; 933 return; 934 } 935 936 slot->data_done = 0; 937 938 /* Calculate and set data timeout.*/ 939 /* XXX: We should have this from mmc layer, now assume 1 sec. */ 940 if (slot->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL) { 941 div = 0xE; 942 } else { 943 target_timeout = 1000000; 944 div = 0; 945 current_timeout = (1 << 13) * 1000 / slot->timeout_clk; 946 while (current_timeout < target_timeout && div < 0xE) { 947 ++div; 948 current_timeout <<= 1; 949 } 950 /* Compensate for an off-by-one error in the CaFe chip.*/ 951 if (div < 0xE && 952 (slot->quirks & SDHCI_QUIRK_INCR_TIMEOUT_CONTROL)) { 953 ++div; 954 } 955 } 956 WR1(slot, SDHCI_TIMEOUT_CONTROL, div); 957 958 if (data == NULL) 959 return; 960 961 /* Use DMA if possible. */ 962 if ((slot->opt & SDHCI_HAVE_DMA)) 963 slot->flags |= SDHCI_USE_DMA; 964 /* If data is small, broken DMA may return zeroes instead of data, */ 965 if ((slot->quirks & SDHCI_QUIRK_BROKEN_TIMINGS) && 966 (data->len <= 512)) 967 slot->flags &= ~SDHCI_USE_DMA; 968 /* Some controllers require even block sizes. */ 969 if ((slot->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE) && 970 ((data->len) & 0x3)) 971 slot->flags &= ~SDHCI_USE_DMA; 972 /* Load DMA buffer. */ 973 if (slot->flags & SDHCI_USE_DMA) { 974 if (data->flags & MMC_DATA_READ) 975 bus_dmamap_sync(slot->dmatag, slot->dmamap, 976 BUS_DMASYNC_PREREAD); 977 else { 978 memcpy(slot->dmamem, data->data, 979 (data->len < DMA_BLOCK_SIZE) ? 980 data->len : DMA_BLOCK_SIZE); 981 bus_dmamap_sync(slot->dmatag, slot->dmamap, 982 BUS_DMASYNC_PREWRITE); 983 } 984 WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 985 /* Interrupt aggregation: Mask border interrupt 986 * for the last page and unmask else. */ 987 if (data->len == DMA_BLOCK_SIZE) 988 slot->intmask &= ~SDHCI_INT_DMA_END; 989 else 990 slot->intmask |= SDHCI_INT_DMA_END; 991 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 992 } 993 /* Current data offset for both PIO and DMA. */ 994 slot->offset = 0; 995 /* Set block size and request IRQ on 4K border. */ 996 WR2(slot, SDHCI_BLOCK_SIZE, 997 SDHCI_MAKE_BLKSZ(DMA_BOUNDARY, (data->len < 512)?data->len:512)); 998 /* Set block count. */ 999 WR2(slot, SDHCI_BLOCK_COUNT, (data->len + 511) / 512); 1000 } 1001 1002 void 1003 sdhci_finish_data(struct sdhci_slot *slot) 1004 { 1005 struct mmc_data *data = slot->curcmd->data; 1006 1007 /* Interrupt aggregation: Restore command interrupt. 1008 * Auxiliary restore point for the case when data interrupt 1009 * happened first. */ 1010 if (!slot->cmd_done) { 1011 WR4(slot, SDHCI_SIGNAL_ENABLE, 1012 slot->intmask |= SDHCI_INT_RESPONSE); 1013 } 1014 /* Unload rest of data from DMA buffer. */ 1015 if (!slot->data_done && (slot->flags & SDHCI_USE_DMA)) { 1016 if (data->flags & MMC_DATA_READ) { 1017 size_t left = data->len - slot->offset; 1018 bus_dmamap_sync(slot->dmatag, slot->dmamap, 1019 BUS_DMASYNC_POSTREAD); 1020 memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1021 (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1022 } else 1023 bus_dmamap_sync(slot->dmatag, slot->dmamap, 1024 BUS_DMASYNC_POSTWRITE); 1025 } 1026 slot->data_done = 1; 1027 /* If there was error - reset the host. */ 1028 if (slot->curcmd->error) { 1029 sdhci_reset(slot, SDHCI_RESET_CMD); 1030 sdhci_reset(slot, SDHCI_RESET_DATA); 1031 sdhci_start(slot); 1032 return; 1033 } 1034 /* If we already have command response - finish. */ 1035 if (slot->cmd_done) 1036 sdhci_start(slot); 1037 } 1038 1039 static void 1040 sdhci_start(struct sdhci_slot *slot) 1041 { 1042 struct mmc_request *req; 1043 1044 req = slot->req; 1045 if (req == NULL) 1046 return; 1047 1048 if (!(slot->flags & CMD_STARTED)) { 1049 slot->flags |= CMD_STARTED; 1050 sdhci_start_command(slot, req->cmd); 1051 return; 1052 } 1053 /* We don't need this until using Auto-CMD12 feature 1054 if (!(slot->flags & STOP_STARTED) && req->stop) { 1055 slot->flags |= STOP_STARTED; 1056 sdhci_start_command(slot, req->stop); 1057 return; 1058 } 1059 */ 1060 if (sdhci_debug > 1) 1061 slot_printf(slot, "result: %d\n", req->cmd->error); 1062 if (!req->cmd->error && 1063 (slot->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)) { 1064 sdhci_reset(slot, SDHCI_RESET_CMD); 1065 sdhci_reset(slot, SDHCI_RESET_DATA); 1066 } 1067 1068 sdhci_req_done(slot); 1069 } 1070 1071 int 1072 sdhci_generic_request(device_t brdev, device_t reqdev, struct mmc_request *req) 1073 { 1074 struct sdhci_slot *slot = device_get_ivars(reqdev); 1075 1076 SDHCI_LOCK(slot); 1077 if (slot->req != NULL) { 1078 SDHCI_UNLOCK(slot); 1079 return (EBUSY); 1080 } 1081 if (sdhci_debug > 1) { 1082 slot_printf(slot, "CMD%u arg %#x flags %#x dlen %u dflags %#x\n", 1083 req->cmd->opcode, req->cmd->arg, req->cmd->flags, 1084 (req->cmd->data)?(u_int)req->cmd->data->len:0, 1085 (req->cmd->data)?req->cmd->data->flags:0); 1086 } 1087 slot->req = req; 1088 slot->flags = 0; 1089 sdhci_start(slot); 1090 SDHCI_UNLOCK(slot); 1091 if (dumping) { 1092 while (slot->req != NULL) { 1093 sdhci_generic_intr(slot); 1094 DELAY(10); 1095 } 1096 } 1097 return (0); 1098 } 1099 1100 int 1101 sdhci_generic_get_ro(device_t brdev, device_t reqdev) 1102 { 1103 struct sdhci_slot *slot = device_get_ivars(reqdev); 1104 uint32_t val; 1105 1106 SDHCI_LOCK(slot); 1107 val = RD4(slot, SDHCI_PRESENT_STATE); 1108 SDHCI_UNLOCK(slot); 1109 return (!(val & SDHCI_WRITE_PROTECT)); 1110 } 1111 1112 int 1113 sdhci_generic_acquire_host(device_t brdev, device_t reqdev) 1114 { 1115 struct sdhci_slot *slot = device_get_ivars(reqdev); 1116 int err = 0; 1117 1118 SDHCI_LOCK(slot); 1119 while (slot->bus_busy) 1120 msleep(slot, &slot->mtx, 0, "sdhciah", 0); 1121 slot->bus_busy++; 1122 /* Activate led. */ 1123 WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl |= SDHCI_CTRL_LED); 1124 SDHCI_UNLOCK(slot); 1125 return (err); 1126 } 1127 1128 int 1129 sdhci_generic_release_host(device_t brdev, device_t reqdev) 1130 { 1131 struct sdhci_slot *slot = device_get_ivars(reqdev); 1132 1133 SDHCI_LOCK(slot); 1134 /* Deactivate led. */ 1135 WR1(slot, SDHCI_HOST_CONTROL, slot->hostctrl &= ~SDHCI_CTRL_LED); 1136 slot->bus_busy--; 1137 SDHCI_UNLOCK(slot); 1138 wakeup(slot); 1139 return (0); 1140 } 1141 1142 static void 1143 sdhci_cmd_irq(struct sdhci_slot *slot, uint32_t intmask) 1144 { 1145 1146 if (!slot->curcmd) { 1147 slot_printf(slot, "Got command interrupt 0x%08x, but " 1148 "there is no active command.\n", intmask); 1149 sdhci_dumpregs(slot); 1150 return; 1151 } 1152 if (intmask & SDHCI_INT_TIMEOUT) 1153 slot->curcmd->error = MMC_ERR_TIMEOUT; 1154 else if (intmask & SDHCI_INT_CRC) 1155 slot->curcmd->error = MMC_ERR_BADCRC; 1156 else if (intmask & (SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) 1157 slot->curcmd->error = MMC_ERR_FIFO; 1158 1159 sdhci_finish_command(slot); 1160 } 1161 1162 static void 1163 sdhci_data_irq(struct sdhci_slot *slot, uint32_t intmask) 1164 { 1165 1166 if (!slot->curcmd) { 1167 slot_printf(slot, "Got data interrupt 0x%08x, but " 1168 "there is no active command.\n", intmask); 1169 sdhci_dumpregs(slot); 1170 return; 1171 } 1172 if (slot->curcmd->data == NULL && 1173 (slot->curcmd->flags & MMC_RSP_BUSY) == 0) { 1174 slot_printf(slot, "Got data interrupt 0x%08x, but " 1175 "there is no active data operation.\n", 1176 intmask); 1177 sdhci_dumpregs(slot); 1178 return; 1179 } 1180 if (intmask & SDHCI_INT_DATA_TIMEOUT) 1181 slot->curcmd->error = MMC_ERR_TIMEOUT; 1182 else if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_END_BIT)) 1183 slot->curcmd->error = MMC_ERR_BADCRC; 1184 if (slot->curcmd->data == NULL && 1185 (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL | 1186 SDHCI_INT_DMA_END))) { 1187 slot_printf(slot, "Got data interrupt 0x%08x, but " 1188 "there is busy-only command.\n", intmask); 1189 sdhci_dumpregs(slot); 1190 slot->curcmd->error = MMC_ERR_INVALID; 1191 } 1192 if (slot->curcmd->error) { 1193 /* No need to continue after any error. */ 1194 goto done; 1195 } 1196 1197 /* Handle PIO interrupt. */ 1198 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) { 1199 if ((slot->opt & SDHCI_PLATFORM_TRANSFER) && 1200 SDHCI_PLATFORM_WILL_HANDLE(slot->bus, slot)) { 1201 SDHCI_PLATFORM_START_TRANSFER(slot->bus, slot, &intmask); 1202 slot->flags |= PLATFORM_DATA_STARTED; 1203 } else 1204 sdhci_transfer_pio(slot); 1205 } 1206 /* Handle DMA border. */ 1207 if (intmask & SDHCI_INT_DMA_END) { 1208 struct mmc_data *data = slot->curcmd->data; 1209 size_t left; 1210 1211 /* Unload DMA buffer... */ 1212 left = data->len - slot->offset; 1213 if (data->flags & MMC_DATA_READ) { 1214 bus_dmamap_sync(slot->dmatag, slot->dmamap, 1215 BUS_DMASYNC_POSTREAD); 1216 memcpy((u_char*)data->data + slot->offset, slot->dmamem, 1217 (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1218 } else { 1219 bus_dmamap_sync(slot->dmatag, slot->dmamap, 1220 BUS_DMASYNC_POSTWRITE); 1221 } 1222 /* ... and reload it again. */ 1223 slot->offset += DMA_BLOCK_SIZE; 1224 left = data->len - slot->offset; 1225 if (data->flags & MMC_DATA_READ) { 1226 bus_dmamap_sync(slot->dmatag, slot->dmamap, 1227 BUS_DMASYNC_PREREAD); 1228 } else { 1229 memcpy(slot->dmamem, (u_char*)data->data + slot->offset, 1230 (left < DMA_BLOCK_SIZE)?left:DMA_BLOCK_SIZE); 1231 bus_dmamap_sync(slot->dmatag, slot->dmamap, 1232 BUS_DMASYNC_PREWRITE); 1233 } 1234 /* Interrupt aggregation: Mask border interrupt 1235 * for the last page. */ 1236 if (left == DMA_BLOCK_SIZE) { 1237 slot->intmask &= ~SDHCI_INT_DMA_END; 1238 WR4(slot, SDHCI_SIGNAL_ENABLE, slot->intmask); 1239 } 1240 /* Restart DMA. */ 1241 WR4(slot, SDHCI_DMA_ADDRESS, slot->paddr); 1242 } 1243 /* We have got all data. */ 1244 if (intmask & SDHCI_INT_DATA_END) { 1245 if (slot->flags & PLATFORM_DATA_STARTED) { 1246 slot->flags &= ~PLATFORM_DATA_STARTED; 1247 SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 1248 } else 1249 sdhci_finish_data(slot); 1250 } 1251 done: 1252 if (slot->curcmd != NULL && slot->curcmd->error != 0) { 1253 if (slot->flags & PLATFORM_DATA_STARTED) { 1254 slot->flags &= ~PLATFORM_DATA_STARTED; 1255 SDHCI_PLATFORM_FINISH_TRANSFER(slot->bus, slot); 1256 } else 1257 sdhci_finish_data(slot); 1258 return; 1259 } 1260 } 1261 1262 static void 1263 sdhci_acmd_irq(struct sdhci_slot *slot) 1264 { 1265 uint16_t err; 1266 1267 err = RD4(slot, SDHCI_ACMD12_ERR); 1268 if (!slot->curcmd) { 1269 slot_printf(slot, "Got AutoCMD12 error 0x%04x, but " 1270 "there is no active command.\n", err); 1271 sdhci_dumpregs(slot); 1272 return; 1273 } 1274 slot_printf(slot, "Got AutoCMD12 error 0x%04x\n", err); 1275 sdhci_reset(slot, SDHCI_RESET_CMD); 1276 } 1277 1278 void 1279 sdhci_generic_intr(struct sdhci_slot *slot) 1280 { 1281 uint32_t intmask; 1282 1283 SDHCI_LOCK(slot); 1284 /* Read slot interrupt status. */ 1285 intmask = RD4(slot, SDHCI_INT_STATUS); 1286 if (intmask == 0 || intmask == 0xffffffff) { 1287 SDHCI_UNLOCK(slot); 1288 return; 1289 } 1290 if (sdhci_debug > 2) 1291 slot_printf(slot, "Interrupt %#x\n", intmask); 1292 1293 /* Handle card presence interrupts. */ 1294 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) { 1295 WR4(slot, SDHCI_INT_STATUS, intmask & 1296 (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)); 1297 1298 if (intmask & SDHCI_INT_CARD_REMOVE) { 1299 if (bootverbose || sdhci_debug) 1300 slot_printf(slot, "Card removed\n"); 1301 callout_stop(&slot->card_callout); 1302 taskqueue_enqueue(taskqueue_swi_giant, 1303 &slot->card_task); 1304 } 1305 if (intmask & SDHCI_INT_CARD_INSERT) { 1306 if (bootverbose || sdhci_debug) 1307 slot_printf(slot, "Card inserted\n"); 1308 callout_reset(&slot->card_callout, hz / 2, 1309 sdhci_card_delay, slot); 1310 } 1311 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE); 1312 } 1313 /* Handle command interrupts. */ 1314 if (intmask & SDHCI_INT_CMD_MASK) { 1315 WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_CMD_MASK); 1316 sdhci_cmd_irq(slot, intmask & SDHCI_INT_CMD_MASK); 1317 } 1318 /* Handle data interrupts. */ 1319 if (intmask & SDHCI_INT_DATA_MASK) { 1320 WR4(slot, SDHCI_INT_STATUS, intmask & SDHCI_INT_DATA_MASK); 1321 /* Dont call data_irq in case of errored command */ 1322 if ((intmask & SDHCI_INT_CMD_ERROR_MASK) == 0) 1323 sdhci_data_irq(slot, intmask & SDHCI_INT_DATA_MASK); 1324 } 1325 /* Handle AutoCMD12 error interrupt. */ 1326 if (intmask & SDHCI_INT_ACMD12ERR) { 1327 WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_ACMD12ERR); 1328 sdhci_acmd_irq(slot); 1329 } 1330 intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK); 1331 intmask &= ~SDHCI_INT_ACMD12ERR; 1332 intmask &= ~SDHCI_INT_ERROR; 1333 /* Handle bus power interrupt. */ 1334 if (intmask & SDHCI_INT_BUS_POWER) { 1335 WR4(slot, SDHCI_INT_STATUS, SDHCI_INT_BUS_POWER); 1336 slot_printf(slot, 1337 "Card is consuming too much power!\n"); 1338 intmask &= ~SDHCI_INT_BUS_POWER; 1339 } 1340 /* The rest is unknown. */ 1341 if (intmask) { 1342 WR4(slot, SDHCI_INT_STATUS, intmask); 1343 slot_printf(slot, "Unexpected interrupt 0x%08x.\n", 1344 intmask); 1345 sdhci_dumpregs(slot); 1346 } 1347 1348 SDHCI_UNLOCK(slot); 1349 } 1350 1351 int 1352 sdhci_generic_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 1353 { 1354 struct sdhci_slot *slot = device_get_ivars(child); 1355 1356 switch (which) { 1357 default: 1358 return (EINVAL); 1359 case MMCBR_IVAR_BUS_MODE: 1360 *result = slot->host.ios.bus_mode; 1361 break; 1362 case MMCBR_IVAR_BUS_WIDTH: 1363 *result = slot->host.ios.bus_width; 1364 break; 1365 case MMCBR_IVAR_CHIP_SELECT: 1366 *result = slot->host.ios.chip_select; 1367 break; 1368 case MMCBR_IVAR_CLOCK: 1369 *result = slot->host.ios.clock; 1370 break; 1371 case MMCBR_IVAR_F_MIN: 1372 *result = slot->host.f_min; 1373 break; 1374 case MMCBR_IVAR_F_MAX: 1375 *result = slot->host.f_max; 1376 break; 1377 case MMCBR_IVAR_HOST_OCR: 1378 *result = slot->host.host_ocr; 1379 break; 1380 case MMCBR_IVAR_MODE: 1381 *result = slot->host.mode; 1382 break; 1383 case MMCBR_IVAR_OCR: 1384 *result = slot->host.ocr; 1385 break; 1386 case MMCBR_IVAR_POWER_MODE: 1387 *result = slot->host.ios.power_mode; 1388 break; 1389 case MMCBR_IVAR_VDD: 1390 *result = slot->host.ios.vdd; 1391 break; 1392 case MMCBR_IVAR_CAPS: 1393 *result = slot->host.caps; 1394 break; 1395 case MMCBR_IVAR_TIMING: 1396 *result = slot->host.ios.timing; 1397 break; 1398 case MMCBR_IVAR_MAX_DATA: 1399 *result = 65535; 1400 break; 1401 } 1402 return (0); 1403 } 1404 1405 int 1406 sdhci_generic_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 1407 { 1408 struct sdhci_slot *slot = device_get_ivars(child); 1409 1410 switch (which) { 1411 default: 1412 return (EINVAL); 1413 case MMCBR_IVAR_BUS_MODE: 1414 slot->host.ios.bus_mode = value; 1415 break; 1416 case MMCBR_IVAR_BUS_WIDTH: 1417 slot->host.ios.bus_width = value; 1418 break; 1419 case MMCBR_IVAR_CHIP_SELECT: 1420 slot->host.ios.chip_select = value; 1421 break; 1422 case MMCBR_IVAR_CLOCK: 1423 if (value > 0) { 1424 uint32_t max_clock; 1425 uint32_t clock; 1426 int i; 1427 1428 max_clock = slot->max_clk; 1429 clock = max_clock; 1430 1431 if (slot->version < SDHCI_SPEC_300) { 1432 for (i = 0; i < SDHCI_200_MAX_DIVIDER; 1433 i <<= 1) { 1434 if (clock <= value) 1435 break; 1436 clock >>= 1; 1437 } 1438 } 1439 else { 1440 for (i = 0; i < SDHCI_300_MAX_DIVIDER; 1441 i += 2) { 1442 if (clock <= value) 1443 break; 1444 clock = max_clock / (i + 2); 1445 } 1446 } 1447 1448 slot->host.ios.clock = clock; 1449 } else 1450 slot->host.ios.clock = 0; 1451 break; 1452 case MMCBR_IVAR_MODE: 1453 slot->host.mode = value; 1454 break; 1455 case MMCBR_IVAR_OCR: 1456 slot->host.ocr = value; 1457 break; 1458 case MMCBR_IVAR_POWER_MODE: 1459 slot->host.ios.power_mode = value; 1460 break; 1461 case MMCBR_IVAR_VDD: 1462 slot->host.ios.vdd = value; 1463 break; 1464 case MMCBR_IVAR_TIMING: 1465 slot->host.ios.timing = value; 1466 break; 1467 case MMCBR_IVAR_CAPS: 1468 case MMCBR_IVAR_HOST_OCR: 1469 case MMCBR_IVAR_F_MIN: 1470 case MMCBR_IVAR_F_MAX: 1471 case MMCBR_IVAR_MAX_DATA: 1472 return (EINVAL); 1473 } 1474 return (0); 1475 } 1476 1477 MODULE_VERSION(sdhci, 1); 1478