1 /* 2 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc 3 * Andy Fleming 4 * 5 * Based vaguely on the pxa mmc code: 6 * (C) Copyright 2003 7 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net 8 * 9 * SPDX-License-Identifier: GPL-2.0+ 10 */ 11 12 #include <config.h> 13 #include <common.h> 14 #include <command.h> 15 #include <errno.h> 16 #include <hwconfig.h> 17 #include <mmc.h> 18 #include <part.h> 19 #include <malloc.h> 20 #include <fsl_esdhc.h> 21 #include <fdt_support.h> 22 #include <asm/io.h> 23 #include <dm.h> 24 #include <asm-generic/gpio.h> 25 26 DECLARE_GLOBAL_DATA_PTR; 27 28 #define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \ 29 IRQSTATEN_CINT | \ 30 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \ 31 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \ 32 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \ 33 IRQSTATEN_DINT) 34 35 struct fsl_esdhc { 36 uint dsaddr; /* SDMA system address register */ 37 uint blkattr; /* Block attributes register */ 38 uint cmdarg; /* Command argument register */ 39 uint xfertyp; /* Transfer type register */ 40 uint cmdrsp0; /* Command response 0 register */ 41 uint cmdrsp1; /* Command response 1 register */ 42 uint cmdrsp2; /* Command response 2 register */ 43 uint cmdrsp3; /* Command response 3 register */ 44 uint datport; /* Buffer data port register */ 45 uint prsstat; /* Present state register */ 46 uint proctl; /* Protocol control register */ 47 uint sysctl; /* System Control Register */ 48 uint irqstat; /* Interrupt status register */ 49 uint irqstaten; /* Interrupt status enable register */ 50 uint irqsigen; /* Interrupt signal enable register */ 51 uint autoc12err; /* Auto CMD error status register */ 52 uint hostcapblt; /* Host controller capabilities register */ 53 uint wml; /* Watermark level register */ 54 uint mixctrl; /* For USDHC */ 55 char reserved1[4]; /* reserved */ 56 uint fevt; /* Force event register */ 57 uint admaes; /* ADMA error status register */ 58 uint adsaddr; /* ADMA system address register */ 59 char reserved2[4]; 60 uint dllctrl; 61 uint dllstat; 62 uint clktunectrlstatus; 63 char reserved3[84]; 64 uint vendorspec; 65 uint mmcboot; 66 uint vendorspec2; 67 char reserved4[48]; 68 uint hostver; /* Host controller version register */ 69 char reserved5[4]; /* reserved */ 70 uint dmaerraddr; /* DMA error address register */ 71 char reserved6[4]; /* reserved */ 72 uint dmaerrattr; /* DMA error attribute register */ 73 char reserved7[4]; /* reserved */ 74 uint hostcapblt2; /* Host controller capabilities register 2 */ 75 char reserved8[8]; /* reserved */ 76 uint tcr; /* Tuning control register */ 77 char reserved9[28]; /* reserved */ 78 uint sddirctl; /* SD direction control register */ 79 char reserved10[712];/* reserved */ 80 uint scr; /* eSDHC control register */ 81 }; 82 83 /** 84 * struct fsl_esdhc_priv 85 * 86 * @esdhc_regs: registers of the sdhc controller 87 * @sdhc_clk: Current clk of the sdhc controller 88 * @bus_width: bus width, 1bit, 4bit or 8bit 89 * @cfg: mmc config 90 * @mmc: mmc 91 * Following is used when Driver Model is enabled for MMC 92 * @dev: pointer for the device 93 * @non_removable: 0: removable; 1: non-removable 94 * @wp_enable: 1: enable checking wp; 0: no check 95 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V 96 * @cd_gpio: gpio for card detection 97 * @wp_gpio: gpio for write protection 98 */ 99 struct fsl_esdhc_priv { 100 struct fsl_esdhc *esdhc_regs; 101 unsigned int sdhc_clk; 102 unsigned int bus_width; 103 struct mmc_config cfg; 104 struct mmc *mmc; 105 struct udevice *dev; 106 int non_removable; 107 int wp_enable; 108 int vs18_enable; 109 #ifdef CONFIG_DM_GPIO 110 struct gpio_desc cd_gpio; 111 struct gpio_desc wp_gpio; 112 #endif 113 }; 114 115 /* Return the XFERTYP flags for a given command and data packet */ 116 static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) 117 { 118 uint xfertyp = 0; 119 120 if (data) { 121 xfertyp |= XFERTYP_DPSEL; 122 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO 123 xfertyp |= XFERTYP_DMAEN; 124 #endif 125 if (data->blocks > 1) { 126 xfertyp |= XFERTYP_MSBSEL; 127 xfertyp |= XFERTYP_BCEN; 128 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 129 xfertyp |= XFERTYP_AC12EN; 130 #endif 131 } 132 133 if (data->flags & MMC_DATA_READ) 134 xfertyp |= XFERTYP_DTDSEL; 135 } 136 137 if (cmd->resp_type & MMC_RSP_CRC) 138 xfertyp |= XFERTYP_CCCEN; 139 if (cmd->resp_type & MMC_RSP_OPCODE) 140 xfertyp |= XFERTYP_CICEN; 141 if (cmd->resp_type & MMC_RSP_136) 142 xfertyp |= XFERTYP_RSPTYP_136; 143 else if (cmd->resp_type & MMC_RSP_BUSY) 144 xfertyp |= XFERTYP_RSPTYP_48_BUSY; 145 else if (cmd->resp_type & MMC_RSP_PRESENT) 146 xfertyp |= XFERTYP_RSPTYP_48; 147 148 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) 149 xfertyp |= XFERTYP_CMDTYP_ABORT; 150 151 return XFERTYP_CMD(cmd->cmdidx) | xfertyp; 152 } 153 154 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO 155 /* 156 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. 157 */ 158 static void 159 esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data) 160 { 161 struct fsl_esdhc_priv *priv = mmc->priv; 162 struct fsl_esdhc *regs = priv->esdhc_regs; 163 uint blocks; 164 char *buffer; 165 uint databuf; 166 uint size; 167 uint irqstat; 168 uint timeout; 169 170 if (data->flags & MMC_DATA_READ) { 171 blocks = data->blocks; 172 buffer = data->dest; 173 while (blocks) { 174 timeout = PIO_TIMEOUT; 175 size = data->blocksize; 176 irqstat = esdhc_read32(®s->irqstat); 177 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN) 178 && --timeout); 179 if (timeout <= 0) { 180 printf("\nData Read Failed in PIO Mode."); 181 return; 182 } 183 while (size && (!(irqstat & IRQSTAT_TC))) { 184 udelay(100); /* Wait before last byte transfer complete */ 185 irqstat = esdhc_read32(®s->irqstat); 186 databuf = in_le32(®s->datport); 187 *((uint *)buffer) = databuf; 188 buffer += 4; 189 size -= 4; 190 } 191 blocks--; 192 } 193 } else { 194 blocks = data->blocks; 195 buffer = (char *)data->src; 196 while (blocks) { 197 timeout = PIO_TIMEOUT; 198 size = data->blocksize; 199 irqstat = esdhc_read32(®s->irqstat); 200 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN) 201 && --timeout); 202 if (timeout <= 0) { 203 printf("\nData Write Failed in PIO Mode."); 204 return; 205 } 206 while (size && (!(irqstat & IRQSTAT_TC))) { 207 udelay(100); /* Wait before last byte transfer complete */ 208 databuf = *((uint *)buffer); 209 buffer += 4; 210 size -= 4; 211 irqstat = esdhc_read32(®s->irqstat); 212 out_le32(®s->datport, databuf); 213 } 214 blocks--; 215 } 216 } 217 } 218 #endif 219 220 static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data) 221 { 222 int timeout; 223 struct fsl_esdhc_priv *priv = mmc->priv; 224 struct fsl_esdhc *regs = priv->esdhc_regs; 225 #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) 226 dma_addr_t addr; 227 #endif 228 uint wml_value; 229 230 wml_value = data->blocksize/4; 231 232 if (data->flags & MMC_DATA_READ) { 233 if (wml_value > WML_RD_WML_MAX) 234 wml_value = WML_RD_WML_MAX_VAL; 235 236 esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); 237 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO 238 #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) 239 addr = virt_to_phys((void *)(data->dest)); 240 if (upper_32_bits(addr)) 241 printf("Error found for upper 32 bits\n"); 242 else 243 esdhc_write32(®s->dsaddr, lower_32_bits(addr)); 244 #else 245 esdhc_write32(®s->dsaddr, (u32)data->dest); 246 #endif 247 #endif 248 } else { 249 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO 250 flush_dcache_range((ulong)data->src, 251 (ulong)data->src+data->blocks 252 *data->blocksize); 253 #endif 254 if (wml_value > WML_WR_WML_MAX) 255 wml_value = WML_WR_WML_MAX_VAL; 256 if (priv->wp_enable) { 257 if ((esdhc_read32(®s->prsstat) & 258 PRSSTAT_WPSPL) == 0) { 259 printf("\nThe SD card is locked. Can not write to a locked card.\n\n"); 260 return -ETIMEDOUT; 261 } 262 } 263 264 esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, 265 wml_value << 16); 266 #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO 267 #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) 268 addr = virt_to_phys((void *)(data->src)); 269 if (upper_32_bits(addr)) 270 printf("Error found for upper 32 bits\n"); 271 else 272 esdhc_write32(®s->dsaddr, lower_32_bits(addr)); 273 #else 274 esdhc_write32(®s->dsaddr, (u32)data->src); 275 #endif 276 #endif 277 } 278 279 esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); 280 281 /* Calculate the timeout period for data transactions */ 282 /* 283 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles 284 * 2)Timeout period should be minimum 0.250sec as per SD Card spec 285 * So, Number of SD Clock cycles for 0.25sec should be minimum 286 * (SD Clock/sec * 0.25 sec) SD Clock cycles 287 * = (mmc->clock * 1/4) SD Clock cycles 288 * As 1) >= 2) 289 * => (2^(timeout+13)) >= mmc->clock * 1/4 290 * Taking log2 both the sides 291 * => timeout + 13 >= log2(mmc->clock/4) 292 * Rounding up to next power of 2 293 * => timeout + 13 = log2(mmc->clock/4) + 1 294 * => timeout + 13 = fls(mmc->clock/4) 295 * 296 * However, the MMC spec "It is strongly recommended for hosts to 297 * implement more than 500ms timeout value even if the card 298 * indicates the 250ms maximum busy length." Even the previous 299 * value of 300ms is known to be insufficient for some cards. 300 * So, we use 301 * => timeout + 13 = fls(mmc->clock/2) 302 */ 303 timeout = fls(mmc->clock/2); 304 timeout -= 13; 305 306 if (timeout > 14) 307 timeout = 14; 308 309 if (timeout < 0) 310 timeout = 0; 311 312 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 313 if ((timeout == 4) || (timeout == 8) || (timeout == 12)) 314 timeout++; 315 #endif 316 317 #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE 318 timeout = 0xE; 319 #endif 320 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); 321 322 return 0; 323 } 324 325 static void check_and_invalidate_dcache_range 326 (struct mmc_cmd *cmd, 327 struct mmc_data *data) { 328 unsigned start = 0; 329 unsigned end = 0; 330 unsigned size = roundup(ARCH_DMA_MINALIGN, 331 data->blocks*data->blocksize); 332 #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) 333 dma_addr_t addr; 334 335 addr = virt_to_phys((void *)(data->dest)); 336 if (upper_32_bits(addr)) 337 printf("Error found for upper 32 bits\n"); 338 else 339 start = lower_32_bits(addr); 340 #else 341 start = (unsigned)data->dest; 342 #endif 343 end = start + size; 344 invalidate_dcache_range(start, end); 345 } 346 347 /* 348 * Sends a command out on the bus. Takes the mmc pointer, 349 * a command pointer, and an optional data pointer. 350 */ 351 static int 352 esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) 353 { 354 int err = 0; 355 uint xfertyp; 356 uint irqstat; 357 struct fsl_esdhc_priv *priv = mmc->priv; 358 struct fsl_esdhc *regs = priv->esdhc_regs; 359 360 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 361 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) 362 return 0; 363 #endif 364 365 esdhc_write32(®s->irqstat, -1); 366 367 sync(); 368 369 /* Wait for the bus to be idle */ 370 while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || 371 (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) 372 ; 373 374 while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) 375 ; 376 377 /* Wait at least 8 SD clock cycles before the next command */ 378 /* 379 * Note: This is way more than 8 cycles, but 1ms seems to 380 * resolve timing issues with some cards 381 */ 382 udelay(1000); 383 384 /* Set up for a data transfer if we have one */ 385 if (data) { 386 err = esdhc_setup_data(mmc, data); 387 if(err) 388 return err; 389 390 if (data->flags & MMC_DATA_READ) 391 check_and_invalidate_dcache_range(cmd, data); 392 } 393 394 /* Figure out the transfer arguments */ 395 xfertyp = esdhc_xfertyp(cmd, data); 396 397 /* Mask all irqs */ 398 esdhc_write32(®s->irqsigen, 0); 399 400 /* Send the command */ 401 esdhc_write32(®s->cmdarg, cmd->cmdarg); 402 #if defined(CONFIG_FSL_USDHC) 403 esdhc_write32(®s->mixctrl, 404 (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F) 405 | (mmc->ddr_mode ? XFERTYP_DDREN : 0)); 406 esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000); 407 #else 408 esdhc_write32(®s->xfertyp, xfertyp); 409 #endif 410 411 /* Wait for the command to complete */ 412 while (!(esdhc_read32(®s->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE))) 413 ; 414 415 irqstat = esdhc_read32(®s->irqstat); 416 417 if (irqstat & CMD_ERR) { 418 err = -ECOMM; 419 goto out; 420 } 421 422 if (irqstat & IRQSTAT_CTOE) { 423 err = -ETIMEDOUT; 424 goto out; 425 } 426 427 /* Switch voltage to 1.8V if CMD11 succeeded */ 428 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) { 429 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); 430 431 printf("Run CMD11 1.8V switch\n"); 432 /* Sleep for 5 ms - max time for card to switch to 1.8V */ 433 udelay(5000); 434 } 435 436 /* Workaround for ESDHC errata ENGcm03648 */ 437 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { 438 int timeout = 6000; 439 440 /* Poll on DATA0 line for cmd with busy signal for 600 ms */ 441 while (timeout > 0 && !(esdhc_read32(®s->prsstat) & 442 PRSSTAT_DAT0)) { 443 udelay(100); 444 timeout--; 445 } 446 447 if (timeout <= 0) { 448 printf("Timeout waiting for DAT0 to go high!\n"); 449 err = -ETIMEDOUT; 450 goto out; 451 } 452 } 453 454 /* Copy the response to the response buffer */ 455 if (cmd->resp_type & MMC_RSP_136) { 456 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; 457 458 cmdrsp3 = esdhc_read32(®s->cmdrsp3); 459 cmdrsp2 = esdhc_read32(®s->cmdrsp2); 460 cmdrsp1 = esdhc_read32(®s->cmdrsp1); 461 cmdrsp0 = esdhc_read32(®s->cmdrsp0); 462 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); 463 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); 464 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); 465 cmd->response[3] = (cmdrsp0 << 8); 466 } else 467 cmd->response[0] = esdhc_read32(®s->cmdrsp0); 468 469 /* Wait until all of the blocks are transferred */ 470 if (data) { 471 #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO 472 esdhc_pio_read_write(mmc, data); 473 #else 474 do { 475 irqstat = esdhc_read32(®s->irqstat); 476 477 if (irqstat & IRQSTAT_DTOE) { 478 err = -ETIMEDOUT; 479 goto out; 480 } 481 482 if (irqstat & DATA_ERR) { 483 err = -ECOMM; 484 goto out; 485 } 486 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE); 487 488 /* 489 * Need invalidate the dcache here again to avoid any 490 * cache-fill during the DMA operations such as the 491 * speculative pre-fetching etc. 492 */ 493 if (data->flags & MMC_DATA_READ) 494 check_and_invalidate_dcache_range(cmd, data); 495 #endif 496 } 497 498 out: 499 /* Reset CMD and DATA portions on error */ 500 if (err) { 501 esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | 502 SYSCTL_RSTC); 503 while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) 504 ; 505 506 if (data) { 507 esdhc_write32(®s->sysctl, 508 esdhc_read32(®s->sysctl) | 509 SYSCTL_RSTD); 510 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) 511 ; 512 } 513 514 /* If this was CMD11, then notify that power cycle is needed */ 515 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) 516 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n"); 517 } 518 519 esdhc_write32(®s->irqstat, -1); 520 521 return err; 522 } 523 524 static void set_sysctl(struct mmc *mmc, uint clock) 525 { 526 int div = 1; 527 #ifdef ARCH_MXC 528 int pre_div = 1; 529 #else 530 int pre_div = 2; 531 #endif 532 int ddr_pre_div = mmc->ddr_mode ? 2 : 1; 533 struct fsl_esdhc_priv *priv = mmc->priv; 534 struct fsl_esdhc *regs = priv->esdhc_regs; 535 int sdhc_clk = priv->sdhc_clk; 536 uint clk; 537 538 if (clock < mmc->cfg->f_min) 539 clock = mmc->cfg->f_min; 540 541 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256) 542 pre_div *= 2; 543 544 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16) 545 div++; 546 547 pre_div >>= 1; 548 div -= 1; 549 550 clk = (pre_div << 8) | (div << 4); 551 552 #ifdef CONFIG_FSL_USDHC 553 esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN); 554 #else 555 esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); 556 #endif 557 558 esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); 559 560 udelay(10000); 561 562 #ifdef CONFIG_FSL_USDHC 563 esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN); 564 #else 565 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); 566 #endif 567 568 } 569 570 #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK 571 static void esdhc_clock_control(struct mmc *mmc, bool enable) 572 { 573 struct fsl_esdhc_priv *priv = mmc->priv; 574 struct fsl_esdhc *regs = priv->esdhc_regs; 575 u32 value; 576 u32 time_out; 577 578 value = esdhc_read32(®s->sysctl); 579 580 if (enable) 581 value |= SYSCTL_CKEN; 582 else 583 value &= ~SYSCTL_CKEN; 584 585 esdhc_write32(®s->sysctl, value); 586 587 time_out = 20; 588 value = PRSSTAT_SDSTB; 589 while (!(esdhc_read32(®s->prsstat) & value)) { 590 if (time_out == 0) { 591 printf("fsl_esdhc: Internal clock never stabilised.\n"); 592 break; 593 } 594 time_out--; 595 mdelay(1); 596 } 597 } 598 #endif 599 600 static int esdhc_set_ios(struct mmc *mmc) 601 { 602 struct fsl_esdhc_priv *priv = mmc->priv; 603 struct fsl_esdhc *regs = priv->esdhc_regs; 604 605 #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK 606 /* Select to use peripheral clock */ 607 esdhc_clock_control(mmc, false); 608 esdhc_setbits32(®s->scr, ESDHCCTL_PCS); 609 esdhc_clock_control(mmc, true); 610 #endif 611 /* Set the clock speed */ 612 set_sysctl(mmc, mmc->clock); 613 614 /* Set the bus width */ 615 esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); 616 617 if (mmc->bus_width == 4) 618 esdhc_setbits32(®s->proctl, PROCTL_DTW_4); 619 else if (mmc->bus_width == 8) 620 esdhc_setbits32(®s->proctl, PROCTL_DTW_8); 621 622 return 0; 623 } 624 625 static int esdhc_init(struct mmc *mmc) 626 { 627 struct fsl_esdhc_priv *priv = mmc->priv; 628 struct fsl_esdhc *regs = priv->esdhc_regs; 629 int timeout = 1000; 630 631 /* Reset the entire host controller */ 632 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); 633 634 /* Wait until the controller is available */ 635 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) 636 udelay(1000); 637 638 #if defined(CONFIG_FSL_USDHC) 639 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */ 640 esdhc_write32(®s->mmcboot, 0x0); 641 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */ 642 esdhc_write32(®s->mixctrl, 0x0); 643 esdhc_write32(®s->clktunectrlstatus, 0x0); 644 645 /* Put VEND_SPEC to default value */ 646 esdhc_write32(®s->vendorspec, VENDORSPEC_INIT); 647 648 /* Disable DLL_CTRL delay line */ 649 esdhc_write32(®s->dllctrl, 0x0); 650 #endif 651 652 #ifndef ARCH_MXC 653 /* Enable cache snooping */ 654 esdhc_write32(®s->scr, 0x00000040); 655 #endif 656 657 #ifndef CONFIG_FSL_USDHC 658 esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); 659 #else 660 esdhc_setbits32(®s->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN); 661 #endif 662 663 /* Set the initial clock speed */ 664 mmc_set_clock(mmc, 400000); 665 666 /* Disable the BRR and BWR bits in IRQSTAT */ 667 esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); 668 669 /* Put the PROCTL reg back to the default */ 670 esdhc_write32(®s->proctl, PROCTL_INIT); 671 672 /* Set timout to the maximum value */ 673 esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); 674 675 #ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT 676 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); 677 #endif 678 679 if (priv->vs18_enable) 680 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); 681 682 return 0; 683 } 684 685 static int esdhc_getcd(struct mmc *mmc) 686 { 687 struct fsl_esdhc_priv *priv = mmc->priv; 688 struct fsl_esdhc *regs = priv->esdhc_regs; 689 int timeout = 1000; 690 691 #ifdef CONFIG_ESDHC_DETECT_QUIRK 692 if (CONFIG_ESDHC_DETECT_QUIRK) 693 return 1; 694 #endif 695 696 #ifdef CONFIG_DM_MMC 697 if (priv->non_removable) 698 return 1; 699 #ifdef CONFIG_DM_GPIO 700 if (dm_gpio_is_valid(&priv->cd_gpio)) 701 return dm_gpio_get_value(&priv->cd_gpio); 702 #endif 703 #endif 704 705 while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) 706 udelay(1000); 707 708 return timeout > 0; 709 } 710 711 static void esdhc_reset(struct fsl_esdhc *regs) 712 { 713 unsigned long timeout = 100; /* wait max 100 ms */ 714 715 /* reset the controller */ 716 esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); 717 718 /* hardware clears the bit when it is done */ 719 while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) 720 udelay(1000); 721 if (!timeout) 722 printf("MMC/SD: Reset never completed.\n"); 723 } 724 725 static const struct mmc_ops esdhc_ops = { 726 .send_cmd = esdhc_send_cmd, 727 .set_ios = esdhc_set_ios, 728 .init = esdhc_init, 729 .getcd = esdhc_getcd, 730 }; 731 732 static int fsl_esdhc_init(struct fsl_esdhc_priv *priv) 733 { 734 struct fsl_esdhc *regs; 735 struct mmc *mmc; 736 u32 caps, voltage_caps; 737 738 if (!priv) 739 return -EINVAL; 740 741 regs = priv->esdhc_regs; 742 743 /* First reset the eSDHC controller */ 744 esdhc_reset(regs); 745 746 #ifndef CONFIG_FSL_USDHC 747 esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN 748 | SYSCTL_IPGEN | SYSCTL_CKEN); 749 #else 750 esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | 751 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN); 752 #endif 753 754 if (priv->vs18_enable) 755 esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); 756 757 writel(SDHCI_IRQ_EN_BITS, ®s->irqstaten); 758 memset(&priv->cfg, 0, sizeof(priv->cfg)); 759 760 voltage_caps = 0; 761 caps = esdhc_read32(®s->hostcapblt); 762 763 #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 764 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | 765 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); 766 #endif 767 768 /* T4240 host controller capabilities register should have VS33 bit */ 769 #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 770 caps = caps | ESDHC_HOSTCAPBLT_VS33; 771 #endif 772 773 if (caps & ESDHC_HOSTCAPBLT_VS18) 774 voltage_caps |= MMC_VDD_165_195; 775 if (caps & ESDHC_HOSTCAPBLT_VS30) 776 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; 777 if (caps & ESDHC_HOSTCAPBLT_VS33) 778 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; 779 780 priv->cfg.name = "FSL_SDHC"; 781 priv->cfg.ops = &esdhc_ops; 782 #ifdef CONFIG_SYS_SD_VOLTAGE 783 priv->cfg.voltages = CONFIG_SYS_SD_VOLTAGE; 784 #else 785 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34; 786 #endif 787 if ((priv->cfg.voltages & voltage_caps) == 0) { 788 printf("voltage not supported by controller\n"); 789 return -1; 790 } 791 792 if (priv->bus_width == 8) 793 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; 794 else if (priv->bus_width == 4) 795 priv->cfg.host_caps = MMC_MODE_4BIT; 796 797 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; 798 #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE 799 priv->cfg.host_caps |= MMC_MODE_DDR_52MHz; 800 #endif 801 802 if (priv->bus_width > 0) { 803 if (priv->bus_width < 8) 804 priv->cfg.host_caps &= ~MMC_MODE_8BIT; 805 if (priv->bus_width < 4) 806 priv->cfg.host_caps &= ~MMC_MODE_4BIT; 807 } 808 809 if (caps & ESDHC_HOSTCAPBLT_HSS) 810 priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; 811 812 #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK 813 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) 814 priv->cfg.host_caps &= ~MMC_MODE_8BIT; 815 #endif 816 817 priv->cfg.f_min = 400000; 818 priv->cfg.f_max = min(priv->sdhc_clk, (u32)52000000); 819 820 priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; 821 822 mmc = mmc_create(&priv->cfg, priv); 823 if (mmc == NULL) 824 return -1; 825 826 priv->mmc = mmc; 827 828 return 0; 829 } 830 831 #ifndef CONFIG_DM_MMC 832 static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg, 833 struct fsl_esdhc_priv *priv) 834 { 835 if (!cfg || !priv) 836 return -EINVAL; 837 838 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); 839 priv->bus_width = cfg->max_bus_width; 840 priv->sdhc_clk = cfg->sdhc_clk; 841 priv->wp_enable = cfg->wp_enable; 842 priv->vs18_enable = cfg->vs18_enable; 843 844 return 0; 845 }; 846 847 int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) 848 { 849 struct fsl_esdhc_priv *priv; 850 int ret; 851 852 if (!cfg) 853 return -EINVAL; 854 855 priv = calloc(sizeof(struct fsl_esdhc_priv), 1); 856 if (!priv) 857 return -ENOMEM; 858 859 ret = fsl_esdhc_cfg_to_priv(cfg, priv); 860 if (ret) { 861 debug("%s xlate failure\n", __func__); 862 free(priv); 863 return ret; 864 } 865 866 ret = fsl_esdhc_init(priv); 867 if (ret) { 868 debug("%s init failure\n", __func__); 869 free(priv); 870 return ret; 871 } 872 873 return 0; 874 } 875 876 int fsl_esdhc_mmc_init(bd_t *bis) 877 { 878 struct fsl_esdhc_cfg *cfg; 879 880 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); 881 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; 882 cfg->sdhc_clk = gd->arch.sdhc_clk; 883 return fsl_esdhc_initialize(bis, cfg); 884 } 885 #endif 886 887 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT 888 void mmc_adapter_card_type_ident(void) 889 { 890 u8 card_id; 891 u8 value; 892 893 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK; 894 gd->arch.sdhc_adapter = card_id; 895 896 switch (card_id) { 897 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45: 898 value = QIXIS_READ(brdcfg[5]); 899 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7); 900 QIXIS_WRITE(brdcfg[5], value); 901 break; 902 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY: 903 value = QIXIS_READ(pwr_ctl[1]); 904 value |= QIXIS_EVDD_BY_SDHC_VS; 905 QIXIS_WRITE(pwr_ctl[1], value); 906 break; 907 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44: 908 value = QIXIS_READ(brdcfg[5]); 909 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT); 910 QIXIS_WRITE(brdcfg[5], value); 911 break; 912 case QIXIS_ESDHC_ADAPTER_TYPE_RSV: 913 break; 914 case QIXIS_ESDHC_ADAPTER_TYPE_MMC: 915 break; 916 case QIXIS_ESDHC_ADAPTER_TYPE_SD: 917 break; 918 case QIXIS_ESDHC_NO_ADAPTER: 919 break; 920 default: 921 break; 922 } 923 } 924 #endif 925 926 #ifdef CONFIG_OF_LIBFDT 927 __weak int esdhc_status_fixup(void *blob, const char *compat) 928 { 929 #ifdef CONFIG_FSL_ESDHC_PIN_MUX 930 if (!hwconfig("esdhc")) { 931 do_fixup_by_compat(blob, compat, "status", "disabled", 932 sizeof("disabled"), 1); 933 return 1; 934 } 935 #endif 936 do_fixup_by_compat(blob, compat, "status", "okay", 937 sizeof("okay"), 1); 938 return 0; 939 } 940 941 void fdt_fixup_esdhc(void *blob, bd_t *bd) 942 { 943 const char *compat = "fsl,esdhc"; 944 945 if (esdhc_status_fixup(blob, compat)) 946 return; 947 948 #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK 949 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency", 950 gd->arch.sdhc_clk, 1); 951 #else 952 do_fixup_by_compat_u32(blob, compat, "clock-frequency", 953 gd->arch.sdhc_clk, 1); 954 #endif 955 #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT 956 do_fixup_by_compat_u32(blob, compat, "adapter-type", 957 (u32)(gd->arch.sdhc_adapter), 1); 958 #endif 959 } 960 #endif 961 962 #ifdef CONFIG_DM_MMC 963 #include <asm/arch/clock.h> 964 __weak void init_clk_usdhc(u32 index) 965 { 966 } 967 968 static int fsl_esdhc_probe(struct udevice *dev) 969 { 970 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); 971 struct fsl_esdhc_priv *priv = dev_get_priv(dev); 972 const void *fdt = gd->fdt_blob; 973 int node = dev_of_offset(dev); 974 fdt_addr_t addr; 975 unsigned int val; 976 int ret; 977 978 addr = devfdt_get_addr(dev); 979 if (addr == FDT_ADDR_T_NONE) 980 return -EINVAL; 981 982 priv->esdhc_regs = (struct fsl_esdhc *)addr; 983 priv->dev = dev; 984 985 val = fdtdec_get_int(fdt, node, "bus-width", -1); 986 if (val == 8) 987 priv->bus_width = 8; 988 else if (val == 4) 989 priv->bus_width = 4; 990 else 991 priv->bus_width = 1; 992 993 if (fdt_get_property(fdt, node, "non-removable", NULL)) { 994 priv->non_removable = 1; 995 } else { 996 priv->non_removable = 0; 997 #ifdef CONFIG_DM_GPIO 998 gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios", 999 0, &priv->cd_gpio, GPIOD_IS_IN); 1000 #endif 1001 } 1002 1003 priv->wp_enable = 1; 1004 1005 #ifdef CONFIG_DM_GPIO 1006 ret = gpio_request_by_name_nodev(offset_to_ofnode(node), "wp-gpios", 0, 1007 &priv->wp_gpio, GPIOD_IS_IN); 1008 if (ret) 1009 priv->wp_enable = 0; 1010 #endif 1011 /* 1012 * TODO: 1013 * Because lack of clk driver, if SDHC clk is not enabled, 1014 * need to enable it first before this driver is invoked. 1015 * 1016 * we use MXC_ESDHC_CLK to get clk freq. 1017 * If one would like to make this function work, 1018 * the aliases should be provided in dts as this: 1019 * 1020 * aliases { 1021 * mmc0 = &usdhc1; 1022 * mmc1 = &usdhc2; 1023 * mmc2 = &usdhc3; 1024 * mmc3 = &usdhc4; 1025 * }; 1026 * Then if your board only supports mmc2 and mmc3, but we can 1027 * correctly get the seq as 2 and 3, then let mxc_get_clock 1028 * work as expected. 1029 */ 1030 1031 init_clk_usdhc(dev->seq); 1032 1033 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq); 1034 if (priv->sdhc_clk <= 0) { 1035 dev_err(dev, "Unable to get clk for %s\n", dev->name); 1036 return -EINVAL; 1037 } 1038 1039 ret = fsl_esdhc_init(priv); 1040 if (ret) { 1041 dev_err(dev, "fsl_esdhc_init failure\n"); 1042 return ret; 1043 } 1044 1045 upriv->mmc = priv->mmc; 1046 priv->mmc->dev = dev; 1047 1048 return 0; 1049 } 1050 1051 static const struct udevice_id fsl_esdhc_ids[] = { 1052 { .compatible = "fsl,imx6ul-usdhc", }, 1053 { .compatible = "fsl,imx6sx-usdhc", }, 1054 { .compatible = "fsl,imx6sl-usdhc", }, 1055 { .compatible = "fsl,imx6q-usdhc", }, 1056 { .compatible = "fsl,imx7d-usdhc", }, 1057 { .compatible = "fsl,imx7ulp-usdhc", }, 1058 { .compatible = "fsl,esdhc", }, 1059 { /* sentinel */ } 1060 }; 1061 1062 U_BOOT_DRIVER(fsl_esdhc) = { 1063 .name = "fsl-esdhc-mmc", 1064 .id = UCLASS_MMC, 1065 .of_match = fsl_esdhc_ids, 1066 .probe = fsl_esdhc_probe, 1067 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv), 1068 }; 1069 #endif 1070