Yangbo Lu | fa33d20 | 2019-06-21 11:42:27 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc |
| 4 | * Copyright 2019 NXP Semiconductors |
| 5 | * Andy Fleming |
| 6 | * Yangbo Lu <yangbo.lu@nxp.com> |
| 7 | * |
| 8 | * Based vaguely on the pxa mmc code: |
| 9 | * (C) Copyright 2003 |
| 10 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
| 11 | */ |
| 12 | |
| 13 | #include <config.h> |
| 14 | #include <common.h> |
| 15 | #include <command.h> |
| 16 | #include <clk.h> |
| 17 | #include <errno.h> |
| 18 | #include <hwconfig.h> |
| 19 | #include <mmc.h> |
| 20 | #include <part.h> |
| 21 | #include <power/regulator.h> |
| 22 | #include <malloc.h> |
| 23 | #include <fsl_esdhc_imx.h> |
| 24 | #include <fdt_support.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <dm.h> |
| 27 | #include <asm-generic/gpio.h> |
| 28 | #include <dm/pinctrl.h> |
| 29 | |
| 30 | #if !CONFIG_IS_ENABLED(BLK) |
| 31 | #include "mmc_private.h" |
| 32 | #endif |
| 33 | |
| 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
| 36 | #define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \ |
| 37 | IRQSTATEN_CINT | \ |
| 38 | IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \ |
| 39 | IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \ |
| 40 | IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \ |
| 41 | IRQSTATEN_DINT) |
| 42 | #define MAX_TUNING_LOOP 40 |
| 43 | #define ESDHC_DRIVER_STAGE_VALUE 0xffffffff |
| 44 | |
| 45 | struct fsl_esdhc { |
| 46 | uint dsaddr; /* SDMA system address register */ |
| 47 | uint blkattr; /* Block attributes register */ |
| 48 | uint cmdarg; /* Command argument register */ |
| 49 | uint xfertyp; /* Transfer type register */ |
| 50 | uint cmdrsp0; /* Command response 0 register */ |
| 51 | uint cmdrsp1; /* Command response 1 register */ |
| 52 | uint cmdrsp2; /* Command response 2 register */ |
| 53 | uint cmdrsp3; /* Command response 3 register */ |
| 54 | uint datport; /* Buffer data port register */ |
| 55 | uint prsstat; /* Present state register */ |
| 56 | uint proctl; /* Protocol control register */ |
| 57 | uint sysctl; /* System Control Register */ |
| 58 | uint irqstat; /* Interrupt status register */ |
| 59 | uint irqstaten; /* Interrupt status enable register */ |
| 60 | uint irqsigen; /* Interrupt signal enable register */ |
| 61 | uint autoc12err; /* Auto CMD error status register */ |
| 62 | uint hostcapblt; /* Host controller capabilities register */ |
| 63 | uint wml; /* Watermark level register */ |
| 64 | uint mixctrl; /* For USDHC */ |
| 65 | char reserved1[4]; /* reserved */ |
| 66 | uint fevt; /* Force event register */ |
| 67 | uint admaes; /* ADMA error status register */ |
| 68 | uint adsaddr; /* ADMA system address register */ |
| 69 | char reserved2[4]; |
| 70 | uint dllctrl; |
| 71 | uint dllstat; |
| 72 | uint clktunectrlstatus; |
| 73 | char reserved3[4]; |
| 74 | uint strobe_dllctrl; |
| 75 | uint strobe_dllstat; |
| 76 | char reserved4[72]; |
| 77 | uint vendorspec; |
| 78 | uint mmcboot; |
| 79 | uint vendorspec2; |
| 80 | uint tuning_ctrl; /* on i.MX6/7/8 */ |
| 81 | char reserved5[44]; |
| 82 | uint hostver; /* Host controller version register */ |
| 83 | char reserved6[4]; /* reserved */ |
| 84 | uint dmaerraddr; /* DMA error address register */ |
| 85 | char reserved7[4]; /* reserved */ |
| 86 | uint dmaerrattr; /* DMA error attribute register */ |
| 87 | char reserved8[4]; /* reserved */ |
| 88 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
| 89 | char reserved9[8]; /* reserved */ |
| 90 | uint tcr; /* Tuning control register */ |
| 91 | char reserved10[28]; /* reserved */ |
| 92 | uint sddirctl; /* SD direction control register */ |
| 93 | char reserved11[712];/* reserved */ |
| 94 | uint scr; /* eSDHC control register */ |
| 95 | }; |
| 96 | |
| 97 | struct fsl_esdhc_plat { |
| 98 | struct mmc_config cfg; |
| 99 | struct mmc mmc; |
| 100 | }; |
| 101 | |
| 102 | struct esdhc_soc_data { |
| 103 | u32 flags; |
| 104 | u32 caps; |
| 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * struct fsl_esdhc_priv |
| 109 | * |
| 110 | * @esdhc_regs: registers of the sdhc controller |
| 111 | * @sdhc_clk: Current clk of the sdhc controller |
| 112 | * @bus_width: bus width, 1bit, 4bit or 8bit |
| 113 | * @cfg: mmc config |
| 114 | * @mmc: mmc |
| 115 | * Following is used when Driver Model is enabled for MMC |
| 116 | * @dev: pointer for the device |
| 117 | * @non_removable: 0: removable; 1: non-removable |
| 118 | * @wp_enable: 1: enable checking wp; 0: no check |
| 119 | * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V |
| 120 | * @flags: ESDHC_FLAG_xx in include/fsl_esdhc_imx.h |
| 121 | * @caps: controller capabilities |
| 122 | * @tuning_step: tuning step setting in tuning_ctrl register |
| 123 | * @start_tuning_tap: the start point for tuning in tuning_ctrl register |
| 124 | * @strobe_dll_delay_target: settings in strobe_dllctrl |
| 125 | * @signal_voltage: indicating the current voltage |
| 126 | * @cd_gpio: gpio for card detection |
| 127 | * @wp_gpio: gpio for write protection |
| 128 | */ |
| 129 | struct fsl_esdhc_priv { |
| 130 | struct fsl_esdhc *esdhc_regs; |
| 131 | unsigned int sdhc_clk; |
| 132 | struct clk per_clk; |
| 133 | unsigned int clock; |
| 134 | unsigned int mode; |
| 135 | unsigned int bus_width; |
| 136 | #if !CONFIG_IS_ENABLED(BLK) |
| 137 | struct mmc *mmc; |
| 138 | #endif |
| 139 | struct udevice *dev; |
| 140 | int non_removable; |
| 141 | int wp_enable; |
| 142 | int vs18_enable; |
| 143 | u32 flags; |
| 144 | u32 caps; |
| 145 | u32 tuning_step; |
| 146 | u32 tuning_start_tap; |
| 147 | u32 strobe_dll_delay_target; |
| 148 | u32 signal_voltage; |
| 149 | #if IS_ENABLED(CONFIG_DM_REGULATOR) |
| 150 | struct udevice *vqmmc_dev; |
| 151 | struct udevice *vmmc_dev; |
| 152 | #endif |
| 153 | #ifdef CONFIG_DM_GPIO |
| 154 | struct gpio_desc cd_gpio; |
| 155 | struct gpio_desc wp_gpio; |
| 156 | #endif |
| 157 | }; |
| 158 | |
| 159 | /* Return the XFERTYP flags for a given command and data packet */ |
| 160 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
| 161 | { |
| 162 | uint xfertyp = 0; |
| 163 | |
| 164 | if (data) { |
| 165 | xfertyp |= XFERTYP_DPSEL; |
| 166 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 167 | xfertyp |= XFERTYP_DMAEN; |
| 168 | #endif |
| 169 | if (data->blocks > 1) { |
| 170 | xfertyp |= XFERTYP_MSBSEL; |
| 171 | xfertyp |= XFERTYP_BCEN; |
| 172 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 173 | xfertyp |= XFERTYP_AC12EN; |
| 174 | #endif |
| 175 | } |
| 176 | |
| 177 | if (data->flags & MMC_DATA_READ) |
| 178 | xfertyp |= XFERTYP_DTDSEL; |
| 179 | } |
| 180 | |
| 181 | if (cmd->resp_type & MMC_RSP_CRC) |
| 182 | xfertyp |= XFERTYP_CCCEN; |
| 183 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 184 | xfertyp |= XFERTYP_CICEN; |
| 185 | if (cmd->resp_type & MMC_RSP_136) |
| 186 | xfertyp |= XFERTYP_RSPTYP_136; |
| 187 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 188 | xfertyp |= XFERTYP_RSPTYP_48_BUSY; |
| 189 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 190 | xfertyp |= XFERTYP_RSPTYP_48; |
| 191 | |
| 192 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 193 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
| 194 | |
| 195 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 196 | } |
| 197 | |
| 198 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 199 | /* |
| 200 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 201 | */ |
| 202 | static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv, |
| 203 | struct mmc_data *data) |
| 204 | { |
| 205 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 206 | uint blocks; |
| 207 | char *buffer; |
| 208 | uint databuf; |
| 209 | uint size; |
| 210 | uint irqstat; |
| 211 | ulong start; |
| 212 | |
| 213 | if (data->flags & MMC_DATA_READ) { |
| 214 | blocks = data->blocks; |
| 215 | buffer = data->dest; |
| 216 | while (blocks) { |
| 217 | start = get_timer(0); |
| 218 | size = data->blocksize; |
| 219 | irqstat = esdhc_read32(®s->irqstat); |
| 220 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) { |
| 221 | if (get_timer(start) > PIO_TIMEOUT) { |
| 222 | printf("\nData Read Failed in PIO Mode."); |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 227 | udelay(100); /* Wait before last byte transfer complete */ |
| 228 | irqstat = esdhc_read32(®s->irqstat); |
| 229 | databuf = in_le32(®s->datport); |
| 230 | *((uint *)buffer) = databuf; |
| 231 | buffer += 4; |
| 232 | size -= 4; |
| 233 | } |
| 234 | blocks--; |
| 235 | } |
| 236 | } else { |
| 237 | blocks = data->blocks; |
| 238 | buffer = (char *)data->src; |
| 239 | while (blocks) { |
| 240 | start = get_timer(0); |
| 241 | size = data->blocksize; |
| 242 | irqstat = esdhc_read32(®s->irqstat); |
| 243 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) { |
| 244 | if (get_timer(start) > PIO_TIMEOUT) { |
| 245 | printf("\nData Write Failed in PIO Mode."); |
| 246 | return; |
| 247 | } |
| 248 | } |
| 249 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 250 | udelay(100); /* Wait before last byte transfer complete */ |
| 251 | databuf = *((uint *)buffer); |
| 252 | buffer += 4; |
| 253 | size -= 4; |
| 254 | irqstat = esdhc_read32(®s->irqstat); |
| 255 | out_le32(®s->datport, databuf); |
| 256 | } |
| 257 | blocks--; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | #endif |
| 262 | |
| 263 | static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 264 | struct mmc_data *data) |
| 265 | { |
| 266 | int timeout; |
| 267 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 268 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \ |
| 269 | defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) |
| 270 | dma_addr_t addr; |
| 271 | #endif |
| 272 | uint wml_value; |
| 273 | |
| 274 | wml_value = data->blocksize/4; |
| 275 | |
| 276 | if (data->flags & MMC_DATA_READ) { |
| 277 | if (wml_value > WML_RD_WML_MAX) |
| 278 | wml_value = WML_RD_WML_MAX_VAL; |
| 279 | |
| 280 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
| 281 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 282 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \ |
| 283 | defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) |
| 284 | addr = virt_to_phys((void *)(data->dest)); |
| 285 | if (upper_32_bits(addr)) |
| 286 | printf("Error found for upper 32 bits\n"); |
| 287 | else |
| 288 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 289 | #else |
| 290 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
| 291 | #endif |
| 292 | #endif |
| 293 | } else { |
| 294 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 295 | flush_dcache_range((ulong)data->src, |
| 296 | (ulong)data->src+data->blocks |
| 297 | *data->blocksize); |
| 298 | #endif |
| 299 | if (wml_value > WML_WR_WML_MAX) |
| 300 | wml_value = WML_WR_WML_MAX_VAL; |
| 301 | if (priv->wp_enable) { |
| 302 | if ((esdhc_read32(®s->prsstat) & |
| 303 | PRSSTAT_WPSPL) == 0) { |
| 304 | printf("\nThe SD card is locked. Can not write to a locked card.\n\n"); |
| 305 | return -ETIMEDOUT; |
| 306 | } |
| 307 | } else { |
| 308 | #ifdef CONFIG_DM_GPIO |
| 309 | if (dm_gpio_is_valid(&priv->wp_gpio) && dm_gpio_get_value(&priv->wp_gpio)) { |
| 310 | printf("\nThe SD card is locked. Can not write to a locked card.\n\n"); |
| 311 | return -ETIMEDOUT; |
| 312 | } |
| 313 | #endif |
| 314 | } |
| 315 | |
| 316 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 317 | wml_value << 16); |
| 318 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 319 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \ |
| 320 | defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) |
| 321 | addr = virt_to_phys((void *)(data->src)); |
| 322 | if (upper_32_bits(addr)) |
| 323 | printf("Error found for upper 32 bits\n"); |
| 324 | else |
| 325 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 326 | #else |
| 327 | esdhc_write32(®s->dsaddr, (u32)data->src); |
| 328 | #endif |
| 329 | #endif |
| 330 | } |
| 331 | |
| 332 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
| 333 | |
| 334 | /* Calculate the timeout period for data transactions */ |
| 335 | /* |
| 336 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 337 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 338 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 339 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
| 340 | * = (mmc->clock * 1/4) SD Clock cycles |
| 341 | * As 1) >= 2) |
| 342 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
| 343 | * Taking log2 both the sides |
| 344 | * => timeout + 13 >= log2(mmc->clock/4) |
| 345 | * Rounding up to next power of 2 |
| 346 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 347 | * => timeout + 13 = fls(mmc->clock/4) |
| 348 | * |
| 349 | * However, the MMC spec "It is strongly recommended for hosts to |
| 350 | * implement more than 500ms timeout value even if the card |
| 351 | * indicates the 250ms maximum busy length." Even the previous |
| 352 | * value of 300ms is known to be insufficient for some cards. |
| 353 | * So, we use |
| 354 | * => timeout + 13 = fls(mmc->clock/2) |
| 355 | */ |
| 356 | timeout = fls(mmc->clock/2); |
| 357 | timeout -= 13; |
| 358 | |
| 359 | if (timeout > 14) |
| 360 | timeout = 14; |
| 361 | |
| 362 | if (timeout < 0) |
| 363 | timeout = 0; |
| 364 | |
| 365 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 366 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 367 | timeout++; |
| 368 | #endif |
| 369 | |
| 370 | #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE |
| 371 | timeout = 0xE; |
| 372 | #endif |
| 373 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static void check_and_invalidate_dcache_range |
| 379 | (struct mmc_cmd *cmd, |
| 380 | struct mmc_data *data) { |
| 381 | unsigned start = 0; |
| 382 | unsigned end = 0; |
| 383 | unsigned size = roundup(ARCH_DMA_MINALIGN, |
| 384 | data->blocks*data->blocksize); |
| 385 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \ |
| 386 | defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) |
| 387 | dma_addr_t addr; |
| 388 | |
| 389 | addr = virt_to_phys((void *)(data->dest)); |
| 390 | if (upper_32_bits(addr)) |
| 391 | printf("Error found for upper 32 bits\n"); |
| 392 | else |
| 393 | start = lower_32_bits(addr); |
| 394 | #else |
| 395 | start = (unsigned)data->dest; |
| 396 | #endif |
| 397 | end = start + size; |
| 398 | invalidate_dcache_range(start, end); |
| 399 | } |
| 400 | |
| 401 | #ifdef CONFIG_MCF5441x |
| 402 | /* |
| 403 | * Swaps 32-bit words to little-endian byte order. |
| 404 | */ |
| 405 | static inline void sd_swap_dma_buff(struct mmc_data *data) |
| 406 | { |
| 407 | int i, size = data->blocksize >> 2; |
| 408 | u32 *buffer = (u32 *)data->dest; |
| 409 | u32 sw; |
| 410 | |
| 411 | while (data->blocks--) { |
| 412 | for (i = 0; i < size; i++) { |
| 413 | sw = __sw32(*buffer); |
| 414 | *buffer++ = sw; |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | #endif |
| 419 | |
| 420 | /* |
| 421 | * Sends a command out on the bus. Takes the mmc pointer, |
| 422 | * a command pointer, and an optional data pointer. |
| 423 | */ |
| 424 | static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 425 | struct mmc_cmd *cmd, struct mmc_data *data) |
| 426 | { |
| 427 | int err = 0; |
| 428 | uint xfertyp; |
| 429 | uint irqstat; |
| 430 | u32 flags = IRQSTAT_CC | IRQSTAT_CTOE; |
| 431 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 432 | unsigned long start; |
| 433 | |
| 434 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 435 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 436 | return 0; |
| 437 | #endif |
| 438 | |
| 439 | esdhc_write32(®s->irqstat, -1); |
| 440 | |
| 441 | sync(); |
| 442 | |
| 443 | /* Wait for the bus to be idle */ |
| 444 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 445 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 446 | ; |
| 447 | |
| 448 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 449 | ; |
| 450 | |
| 451 | /* Wait at least 8 SD clock cycles before the next command */ |
| 452 | /* |
| 453 | * Note: This is way more than 8 cycles, but 1ms seems to |
| 454 | * resolve timing issues with some cards |
| 455 | */ |
| 456 | udelay(1000); |
| 457 | |
| 458 | /* Set up for a data transfer if we have one */ |
| 459 | if (data) { |
| 460 | err = esdhc_setup_data(priv, mmc, data); |
| 461 | if(err) |
| 462 | return err; |
| 463 | |
| 464 | if (data->flags & MMC_DATA_READ) |
| 465 | check_and_invalidate_dcache_range(cmd, data); |
| 466 | } |
| 467 | |
| 468 | /* Figure out the transfer arguments */ |
| 469 | xfertyp = esdhc_xfertyp(cmd, data); |
| 470 | |
| 471 | /* Mask all irqs */ |
| 472 | esdhc_write32(®s->irqsigen, 0); |
| 473 | |
| 474 | /* Send the command */ |
| 475 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
| 476 | #if defined(CONFIG_FSL_USDHC) |
| 477 | esdhc_write32(®s->mixctrl, |
| 478 | (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F) |
| 479 | | (mmc->ddr_mode ? XFERTYP_DDREN : 0)); |
| 480 | esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000); |
| 481 | #else |
| 482 | esdhc_write32(®s->xfertyp, xfertyp); |
| 483 | #endif |
| 484 | |
| 485 | if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) || |
| 486 | (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) |
| 487 | flags = IRQSTAT_BRR; |
| 488 | |
| 489 | /* Wait for the command to complete */ |
| 490 | start = get_timer(0); |
| 491 | while (!(esdhc_read32(®s->irqstat) & flags)) { |
| 492 | if (get_timer(start) > 1000) { |
| 493 | err = -ETIMEDOUT; |
| 494 | goto out; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | irqstat = esdhc_read32(®s->irqstat); |
| 499 | |
| 500 | if (irqstat & CMD_ERR) { |
| 501 | err = -ECOMM; |
| 502 | goto out; |
| 503 | } |
| 504 | |
| 505 | if (irqstat & IRQSTAT_CTOE) { |
| 506 | err = -ETIMEDOUT; |
| 507 | goto out; |
| 508 | } |
| 509 | |
| 510 | /* Switch voltage to 1.8V if CMD11 succeeded */ |
| 511 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) { |
| 512 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 513 | |
| 514 | printf("Run CMD11 1.8V switch\n"); |
| 515 | /* Sleep for 5 ms - max time for card to switch to 1.8V */ |
| 516 | udelay(5000); |
| 517 | } |
| 518 | |
| 519 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 520 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
| 521 | int timeout = 6000; |
| 522 | |
| 523 | /* Poll on DATA0 line for cmd with busy signal for 600 ms */ |
| 524 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 525 | PRSSTAT_DAT0)) { |
| 526 | udelay(100); |
| 527 | timeout--; |
| 528 | } |
| 529 | |
| 530 | if (timeout <= 0) { |
| 531 | printf("Timeout waiting for DAT0 to go high!\n"); |
| 532 | err = -ETIMEDOUT; |
| 533 | goto out; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /* Copy the response to the response buffer */ |
| 538 | if (cmd->resp_type & MMC_RSP_136) { |
| 539 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 540 | |
| 541 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 542 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 543 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 544 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
| 545 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 546 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 547 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 548 | cmd->response[3] = (cmdrsp0 << 8); |
| 549 | } else |
| 550 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
| 551 | |
| 552 | /* Wait until all of the blocks are transferred */ |
| 553 | if (data) { |
| 554 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 555 | esdhc_pio_read_write(priv, data); |
| 556 | #else |
| 557 | flags = DATA_COMPLETE; |
| 558 | if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) || |
| 559 | (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) { |
| 560 | flags = IRQSTAT_BRR; |
| 561 | } |
| 562 | |
| 563 | do { |
| 564 | irqstat = esdhc_read32(®s->irqstat); |
| 565 | |
| 566 | if (irqstat & IRQSTAT_DTOE) { |
| 567 | err = -ETIMEDOUT; |
| 568 | goto out; |
| 569 | } |
| 570 | |
| 571 | if (irqstat & DATA_ERR) { |
| 572 | err = -ECOMM; |
| 573 | goto out; |
| 574 | } |
| 575 | } while ((irqstat & flags) != flags); |
| 576 | |
| 577 | /* |
| 578 | * Need invalidate the dcache here again to avoid any |
| 579 | * cache-fill during the DMA operations such as the |
| 580 | * speculative pre-fetching etc. |
| 581 | */ |
| 582 | if (data->flags & MMC_DATA_READ) { |
| 583 | check_and_invalidate_dcache_range(cmd, data); |
| 584 | #ifdef CONFIG_MCF5441x |
| 585 | sd_swap_dma_buff(data); |
| 586 | #endif |
| 587 | } |
| 588 | #endif |
| 589 | } |
| 590 | |
| 591 | out: |
| 592 | /* Reset CMD and DATA portions on error */ |
| 593 | if (err) { |
| 594 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 595 | SYSCTL_RSTC); |
| 596 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 597 | ; |
| 598 | |
| 599 | if (data) { |
| 600 | esdhc_write32(®s->sysctl, |
| 601 | esdhc_read32(®s->sysctl) | |
| 602 | SYSCTL_RSTD); |
| 603 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 604 | ; |
| 605 | } |
| 606 | |
| 607 | /* If this was CMD11, then notify that power cycle is needed */ |
| 608 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) |
| 609 | printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n"); |
| 610 | } |
| 611 | |
| 612 | esdhc_write32(®s->irqstat, -1); |
| 613 | |
| 614 | return err; |
| 615 | } |
| 616 | |
| 617 | static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock) |
| 618 | { |
| 619 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 620 | int div = 1; |
| 621 | #ifdef ARCH_MXC |
| 622 | #ifdef CONFIG_MX53 |
| 623 | /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */ |
| 624 | int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1; |
| 625 | #else |
| 626 | int pre_div = 1; |
| 627 | #endif |
| 628 | #else |
| 629 | int pre_div = 2; |
| 630 | #endif |
| 631 | int ddr_pre_div = mmc->ddr_mode ? 2 : 1; |
| 632 | int sdhc_clk = priv->sdhc_clk; |
| 633 | uint clk; |
| 634 | |
| 635 | if (clock < mmc->cfg->f_min) |
| 636 | clock = mmc->cfg->f_min; |
| 637 | |
| 638 | while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256) |
| 639 | pre_div *= 2; |
| 640 | |
| 641 | while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16) |
| 642 | div++; |
| 643 | |
| 644 | pre_div >>= 1; |
| 645 | div -= 1; |
| 646 | |
| 647 | clk = (pre_div << 8) | (div << 4); |
| 648 | |
| 649 | #ifdef CONFIG_FSL_USDHC |
| 650 | esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN); |
| 651 | #else |
| 652 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
| 653 | #endif |
| 654 | |
| 655 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
| 656 | |
| 657 | udelay(10000); |
| 658 | |
| 659 | #ifdef CONFIG_FSL_USDHC |
| 660 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN); |
| 661 | #else |
| 662 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
| 663 | #endif |
| 664 | |
| 665 | priv->clock = clock; |
| 666 | } |
| 667 | |
| 668 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 669 | static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable) |
| 670 | { |
| 671 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 672 | u32 value; |
| 673 | u32 time_out; |
| 674 | |
| 675 | value = esdhc_read32(®s->sysctl); |
| 676 | |
| 677 | if (enable) |
| 678 | value |= SYSCTL_CKEN; |
| 679 | else |
| 680 | value &= ~SYSCTL_CKEN; |
| 681 | |
| 682 | esdhc_write32(®s->sysctl, value); |
| 683 | |
| 684 | time_out = 20; |
| 685 | value = PRSSTAT_SDSTB; |
| 686 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 687 | if (time_out == 0) { |
| 688 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 689 | break; |
| 690 | } |
| 691 | time_out--; |
| 692 | mdelay(1); |
| 693 | } |
| 694 | } |
| 695 | #endif |
| 696 | |
| 697 | #ifdef MMC_SUPPORTS_TUNING |
| 698 | static int esdhc_change_pinstate(struct udevice *dev) |
| 699 | { |
| 700 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 701 | int ret; |
| 702 | |
| 703 | switch (priv->mode) { |
| 704 | case UHS_SDR50: |
| 705 | case UHS_DDR50: |
| 706 | ret = pinctrl_select_state(dev, "state_100mhz"); |
| 707 | break; |
| 708 | case UHS_SDR104: |
| 709 | case MMC_HS_200: |
| 710 | case MMC_HS_400: |
| 711 | ret = pinctrl_select_state(dev, "state_200mhz"); |
| 712 | break; |
| 713 | default: |
| 714 | ret = pinctrl_select_state(dev, "default"); |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | if (ret) |
| 719 | printf("%s %d error\n", __func__, priv->mode); |
| 720 | |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | static void esdhc_reset_tuning(struct mmc *mmc) |
| 725 | { |
| 726 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 727 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 728 | |
| 729 | if (priv->flags & ESDHC_FLAG_USDHC) { |
| 730 | if (priv->flags & ESDHC_FLAG_STD_TUNING) { |
| 731 | esdhc_clrbits32(®s->autoc12err, |
| 732 | MIX_CTRL_SMPCLK_SEL | |
| 733 | MIX_CTRL_EXE_TUNE); |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | static void esdhc_set_strobe_dll(struct mmc *mmc) |
| 739 | { |
| 740 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 741 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 742 | u32 val; |
| 743 | |
| 744 | if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) { |
| 745 | writel(ESDHC_STROBE_DLL_CTRL_RESET, ®s->strobe_dllctrl); |
| 746 | |
| 747 | /* |
| 748 | * enable strobe dll ctrl and adjust the delay target |
| 749 | * for the uSDHC loopback read clock |
| 750 | */ |
| 751 | val = ESDHC_STROBE_DLL_CTRL_ENABLE | |
| 752 | (priv->strobe_dll_delay_target << |
| 753 | ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT); |
| 754 | writel(val, ®s->strobe_dllctrl); |
| 755 | /* wait 1us to make sure strobe dll status register stable */ |
| 756 | mdelay(1); |
| 757 | val = readl(®s->strobe_dllstat); |
| 758 | if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK)) |
| 759 | pr_warn("HS400 strobe DLL status REF not lock!\n"); |
| 760 | if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK)) |
| 761 | pr_warn("HS400 strobe DLL status SLV not lock!\n"); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | static int esdhc_set_timing(struct mmc *mmc) |
| 766 | { |
| 767 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 768 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 769 | u32 mixctrl; |
| 770 | |
| 771 | mixctrl = readl(®s->mixctrl); |
| 772 | mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN); |
| 773 | |
| 774 | switch (mmc->selected_mode) { |
| 775 | case MMC_LEGACY: |
| 776 | case SD_LEGACY: |
| 777 | esdhc_reset_tuning(mmc); |
| 778 | writel(mixctrl, ®s->mixctrl); |
| 779 | break; |
| 780 | case MMC_HS_400: |
| 781 | mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN; |
| 782 | writel(mixctrl, ®s->mixctrl); |
| 783 | esdhc_set_strobe_dll(mmc); |
| 784 | break; |
| 785 | case MMC_HS: |
| 786 | case MMC_HS_52: |
| 787 | case MMC_HS_200: |
| 788 | case SD_HS: |
| 789 | case UHS_SDR12: |
| 790 | case UHS_SDR25: |
| 791 | case UHS_SDR50: |
| 792 | case UHS_SDR104: |
| 793 | writel(mixctrl, ®s->mixctrl); |
| 794 | break; |
| 795 | case UHS_DDR50: |
| 796 | case MMC_DDR_52: |
| 797 | mixctrl |= MIX_CTRL_DDREN; |
| 798 | writel(mixctrl, ®s->mixctrl); |
| 799 | break; |
| 800 | default: |
| 801 | printf("Not supported %d\n", mmc->selected_mode); |
| 802 | return -EINVAL; |
| 803 | } |
| 804 | |
| 805 | priv->mode = mmc->selected_mode; |
| 806 | |
| 807 | return esdhc_change_pinstate(mmc->dev); |
| 808 | } |
| 809 | |
| 810 | static int esdhc_set_voltage(struct mmc *mmc) |
| 811 | { |
| 812 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 813 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 814 | int ret; |
| 815 | |
| 816 | priv->signal_voltage = mmc->signal_voltage; |
| 817 | switch (mmc->signal_voltage) { |
| 818 | case MMC_SIGNAL_VOLTAGE_330: |
| 819 | if (priv->vs18_enable) |
| 820 | return -EIO; |
| 821 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 822 | if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) { |
| 823 | ret = regulator_set_value(priv->vqmmc_dev, 3300000); |
| 824 | if (ret) { |
| 825 | printf("Setting to 3.3V error"); |
| 826 | return -EIO; |
| 827 | } |
| 828 | /* Wait for 5ms */ |
| 829 | mdelay(5); |
| 830 | } |
| 831 | #endif |
| 832 | |
| 833 | esdhc_clrbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 834 | if (!(esdhc_read32(®s->vendorspec) & |
| 835 | ESDHC_VENDORSPEC_VSELECT)) |
| 836 | return 0; |
| 837 | |
| 838 | return -EAGAIN; |
| 839 | case MMC_SIGNAL_VOLTAGE_180: |
| 840 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 841 | if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) { |
| 842 | ret = regulator_set_value(priv->vqmmc_dev, 1800000); |
| 843 | if (ret) { |
| 844 | printf("Setting to 1.8V error"); |
| 845 | return -EIO; |
| 846 | } |
| 847 | } |
| 848 | #endif |
| 849 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 850 | if (esdhc_read32(®s->vendorspec) & ESDHC_VENDORSPEC_VSELECT) |
| 851 | return 0; |
| 852 | |
| 853 | return -EAGAIN; |
| 854 | case MMC_SIGNAL_VOLTAGE_120: |
| 855 | return -ENOTSUPP; |
| 856 | default: |
| 857 | return 0; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | static void esdhc_stop_tuning(struct mmc *mmc) |
| 862 | { |
| 863 | struct mmc_cmd cmd; |
| 864 | |
| 865 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
| 866 | cmd.cmdarg = 0; |
| 867 | cmd.resp_type = MMC_RSP_R1b; |
| 868 | |
| 869 | dm_mmc_send_cmd(mmc->dev, &cmd, NULL); |
| 870 | } |
| 871 | |
| 872 | static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode) |
| 873 | { |
| 874 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 875 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 876 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 877 | struct mmc *mmc = &plat->mmc; |
| 878 | u32 irqstaten = readl(®s->irqstaten); |
| 879 | u32 irqsigen = readl(®s->irqsigen); |
| 880 | int i, ret = -ETIMEDOUT; |
| 881 | u32 val, mixctrl; |
| 882 | |
| 883 | /* clock tuning is not needed for upto 52MHz */ |
| 884 | if (mmc->clock <= 52000000) |
| 885 | return 0; |
| 886 | |
| 887 | /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */ |
| 888 | if (priv->flags & ESDHC_FLAG_STD_TUNING) { |
| 889 | val = readl(®s->autoc12err); |
| 890 | mixctrl = readl(®s->mixctrl); |
| 891 | val &= ~MIX_CTRL_SMPCLK_SEL; |
| 892 | mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN); |
| 893 | |
| 894 | val |= MIX_CTRL_EXE_TUNE; |
| 895 | mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN; |
| 896 | |
| 897 | writel(val, ®s->autoc12err); |
| 898 | writel(mixctrl, ®s->mixctrl); |
| 899 | } |
| 900 | |
| 901 | /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */ |
| 902 | mixctrl = readl(®s->mixctrl); |
| 903 | mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK); |
| 904 | writel(mixctrl, ®s->mixctrl); |
| 905 | |
| 906 | writel(IRQSTATEN_BRR, ®s->irqstaten); |
| 907 | writel(IRQSTATEN_BRR, ®s->irqsigen); |
| 908 | |
| 909 | /* |
| 910 | * Issue opcode repeatedly till Execute Tuning is set to 0 or the number |
| 911 | * of loops reaches 40 times. |
| 912 | */ |
| 913 | for (i = 0; i < MAX_TUNING_LOOP; i++) { |
| 914 | u32 ctrl; |
| 915 | |
| 916 | if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) { |
| 917 | if (mmc->bus_width == 8) |
| 918 | writel(0x7080, ®s->blkattr); |
| 919 | else if (mmc->bus_width == 4) |
| 920 | writel(0x7040, ®s->blkattr); |
| 921 | } else { |
| 922 | writel(0x7040, ®s->blkattr); |
| 923 | } |
| 924 | |
| 925 | /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */ |
| 926 | val = readl(®s->mixctrl); |
| 927 | val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK); |
| 928 | writel(val, ®s->mixctrl); |
| 929 | |
| 930 | /* We are using STD tuning, no need to check return value */ |
| 931 | mmc_send_tuning(mmc, opcode, NULL); |
| 932 | |
| 933 | ctrl = readl(®s->autoc12err); |
| 934 | if ((!(ctrl & MIX_CTRL_EXE_TUNE)) && |
| 935 | (ctrl & MIX_CTRL_SMPCLK_SEL)) { |
| 936 | /* |
| 937 | * need to wait some time, make sure sd/mmc fininsh |
| 938 | * send out tuning data, otherwise, the sd/mmc can't |
| 939 | * response to any command when the card still out |
| 940 | * put the tuning data. |
| 941 | */ |
| 942 | mdelay(1); |
| 943 | ret = 0; |
| 944 | break; |
| 945 | } |
| 946 | |
| 947 | /* Add 1ms delay for SD and eMMC */ |
| 948 | mdelay(1); |
| 949 | } |
| 950 | |
| 951 | writel(irqstaten, ®s->irqstaten); |
| 952 | writel(irqsigen, ®s->irqsigen); |
| 953 | |
| 954 | esdhc_stop_tuning(mmc); |
| 955 | |
| 956 | return ret; |
| 957 | } |
| 958 | #endif |
| 959 | |
| 960 | static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc) |
| 961 | { |
| 962 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 963 | int ret __maybe_unused; |
| 964 | |
| 965 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 966 | /* Select to use peripheral clock */ |
| 967 | esdhc_clock_control(priv, false); |
| 968 | esdhc_setbits32(®s->scr, ESDHCCTL_PCS); |
| 969 | esdhc_clock_control(priv, true); |
| 970 | #endif |
| 971 | /* Set the clock speed */ |
| 972 | if (priv->clock != mmc->clock) |
| 973 | set_sysctl(priv, mmc, mmc->clock); |
| 974 | |
| 975 | #ifdef MMC_SUPPORTS_TUNING |
| 976 | if (mmc->clk_disable) { |
| 977 | #ifdef CONFIG_FSL_USDHC |
| 978 | esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN); |
| 979 | #else |
| 980 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
| 981 | #endif |
| 982 | } else { |
| 983 | #ifdef CONFIG_FSL_USDHC |
| 984 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | |
| 985 | VENDORSPEC_CKEN); |
| 986 | #else |
| 987 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
| 988 | #endif |
| 989 | } |
| 990 | |
| 991 | if (priv->mode != mmc->selected_mode) { |
| 992 | ret = esdhc_set_timing(mmc); |
| 993 | if (ret) { |
| 994 | printf("esdhc_set_timing error %d\n", ret); |
| 995 | return ret; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | if (priv->signal_voltage != mmc->signal_voltage) { |
| 1000 | ret = esdhc_set_voltage(mmc); |
| 1001 | if (ret) { |
| 1002 | printf("esdhc_set_voltage error %d\n", ret); |
| 1003 | return ret; |
| 1004 | } |
| 1005 | } |
| 1006 | #endif |
| 1007 | |
| 1008 | /* Set the bus width */ |
| 1009 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
| 1010 | |
| 1011 | if (mmc->bus_width == 4) |
| 1012 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
| 1013 | else if (mmc->bus_width == 8) |
| 1014 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 1015 | |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc) |
| 1020 | { |
| 1021 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 1022 | ulong start; |
| 1023 | |
| 1024 | /* Reset the entire host controller */ |
| 1025 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
| 1026 | |
| 1027 | /* Wait until the controller is available */ |
| 1028 | start = get_timer(0); |
| 1029 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 1030 | if (get_timer(start) > 1000) |
| 1031 | return -ETIMEDOUT; |
| 1032 | } |
| 1033 | |
| 1034 | #if defined(CONFIG_FSL_USDHC) |
| 1035 | /* RSTA doesn't reset MMC_BOOT register, so manually reset it */ |
| 1036 | esdhc_write32(®s->mmcboot, 0x0); |
| 1037 | /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */ |
| 1038 | esdhc_write32(®s->mixctrl, 0x0); |
| 1039 | esdhc_write32(®s->clktunectrlstatus, 0x0); |
| 1040 | |
| 1041 | /* Put VEND_SPEC to default value */ |
| 1042 | if (priv->vs18_enable) |
| 1043 | esdhc_write32(®s->vendorspec, (VENDORSPEC_INIT | |
| 1044 | ESDHC_VENDORSPEC_VSELECT)); |
| 1045 | else |
| 1046 | esdhc_write32(®s->vendorspec, VENDORSPEC_INIT); |
| 1047 | |
| 1048 | /* Disable DLL_CTRL delay line */ |
| 1049 | esdhc_write32(®s->dllctrl, 0x0); |
| 1050 | #endif |
| 1051 | |
| 1052 | #ifndef ARCH_MXC |
| 1053 | /* Enable cache snooping */ |
| 1054 | esdhc_write32(®s->scr, 0x00000040); |
| 1055 | #endif |
| 1056 | |
| 1057 | #ifndef CONFIG_FSL_USDHC |
| 1058 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
| 1059 | #else |
| 1060 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN); |
| 1061 | #endif |
| 1062 | |
| 1063 | /* Set the initial clock speed */ |
| 1064 | mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE); |
| 1065 | |
| 1066 | /* Disable the BRR and BWR bits in IRQSTAT */ |
| 1067 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
| 1068 | |
| 1069 | #ifdef CONFIG_MCF5441x |
| 1070 | esdhc_write32(®s->proctl, PROCTL_INIT | PROCTL_D3CD); |
| 1071 | #else |
| 1072 | /* Put the PROCTL reg back to the default */ |
| 1073 | esdhc_write32(®s->proctl, PROCTL_INIT); |
| 1074 | #endif |
| 1075 | |
| 1076 | /* Set timout to the maximum value */ |
| 1077 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
| 1078 | |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | static int esdhc_getcd_common(struct fsl_esdhc_priv *priv) |
| 1083 | { |
| 1084 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 1085 | int timeout = 1000; |
| 1086 | |
| 1087 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 1088 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 1089 | return 1; |
| 1090 | #endif |
| 1091 | |
| 1092 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 1093 | if (priv->non_removable) |
| 1094 | return 1; |
| 1095 | #ifdef CONFIG_DM_GPIO |
| 1096 | if (dm_gpio_is_valid(&priv->cd_gpio)) |
| 1097 | return dm_gpio_get_value(&priv->cd_gpio); |
| 1098 | #endif |
| 1099 | #endif |
| 1100 | |
| 1101 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 1102 | udelay(1000); |
| 1103 | |
| 1104 | return timeout > 0; |
| 1105 | } |
| 1106 | |
| 1107 | static int esdhc_reset(struct fsl_esdhc *regs) |
| 1108 | { |
| 1109 | ulong start; |
| 1110 | |
| 1111 | /* reset the controller */ |
| 1112 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
| 1113 | |
| 1114 | /* hardware clears the bit when it is done */ |
| 1115 | start = get_timer(0); |
| 1116 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 1117 | if (get_timer(start) > 100) { |
| 1118 | printf("MMC/SD: Reset never completed.\n"); |
| 1119 | return -ETIMEDOUT; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 1127 | static int esdhc_getcd(struct mmc *mmc) |
| 1128 | { |
| 1129 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1130 | |
| 1131 | return esdhc_getcd_common(priv); |
| 1132 | } |
| 1133 | |
| 1134 | static int esdhc_init(struct mmc *mmc) |
| 1135 | { |
| 1136 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1137 | |
| 1138 | return esdhc_init_common(priv, mmc); |
| 1139 | } |
| 1140 | |
| 1141 | static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 1142 | struct mmc_data *data) |
| 1143 | { |
| 1144 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1145 | |
| 1146 | return esdhc_send_cmd_common(priv, mmc, cmd, data); |
| 1147 | } |
| 1148 | |
| 1149 | static int esdhc_set_ios(struct mmc *mmc) |
| 1150 | { |
| 1151 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1152 | |
| 1153 | return esdhc_set_ios_common(priv, mmc); |
| 1154 | } |
| 1155 | |
| 1156 | static const struct mmc_ops esdhc_ops = { |
| 1157 | .getcd = esdhc_getcd, |
| 1158 | .init = esdhc_init, |
| 1159 | .send_cmd = esdhc_send_cmd, |
| 1160 | .set_ios = esdhc_set_ios, |
| 1161 | }; |
| 1162 | #endif |
| 1163 | |
| 1164 | static int fsl_esdhc_init(struct fsl_esdhc_priv *priv, |
| 1165 | struct fsl_esdhc_plat *plat) |
| 1166 | { |
| 1167 | struct mmc_config *cfg; |
| 1168 | struct fsl_esdhc *regs; |
| 1169 | u32 caps, voltage_caps; |
| 1170 | int ret; |
| 1171 | |
| 1172 | if (!priv) |
| 1173 | return -EINVAL; |
| 1174 | |
| 1175 | regs = priv->esdhc_regs; |
| 1176 | |
| 1177 | /* First reset the eSDHC controller */ |
| 1178 | ret = esdhc_reset(regs); |
| 1179 | if (ret) |
| 1180 | return ret; |
| 1181 | |
| 1182 | #ifdef CONFIG_MCF5441x |
| 1183 | /* ColdFire, using SDHC_DATA[3] for card detection */ |
| 1184 | esdhc_write32(®s->proctl, PROCTL_INIT | PROCTL_D3CD); |
| 1185 | #endif |
| 1186 | |
| 1187 | #ifndef CONFIG_FSL_USDHC |
| 1188 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
| 1189 | | SYSCTL_IPGEN | SYSCTL_CKEN); |
| 1190 | /* Clearing tuning bits in case ROM has set it already */ |
| 1191 | esdhc_write32(®s->mixctrl, 0); |
| 1192 | esdhc_write32(®s->autoc12err, 0); |
| 1193 | esdhc_write32(®s->clktunectrlstatus, 0); |
| 1194 | #else |
| 1195 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | |
| 1196 | VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN); |
| 1197 | #endif |
| 1198 | |
| 1199 | if (priv->vs18_enable) |
| 1200 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 1201 | |
| 1202 | writel(SDHCI_IRQ_EN_BITS, ®s->irqstaten); |
| 1203 | cfg = &plat->cfg; |
| 1204 | #ifndef CONFIG_DM_MMC |
| 1205 | memset(cfg, '\0', sizeof(*cfg)); |
| 1206 | #endif |
| 1207 | |
| 1208 | voltage_caps = 0; |
| 1209 | caps = esdhc_read32(®s->hostcapblt); |
| 1210 | |
| 1211 | #ifdef CONFIG_MCF5441x |
| 1212 | /* |
| 1213 | * MCF5441x RM declares in more points that sdhc clock speed must |
| 1214 | * never exceed 25 Mhz. From this, the HS bit needs to be disabled |
| 1215 | * from host capabilities. |
| 1216 | */ |
| 1217 | caps &= ~ESDHC_HOSTCAPBLT_HSS; |
| 1218 | #endif |
| 1219 | |
| 1220 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
| 1221 | caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | |
| 1222 | ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); |
| 1223 | #endif |
| 1224 | |
| 1225 | /* T4240 host controller capabilities register should have VS33 bit */ |
| 1226 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
| 1227 | caps = caps | ESDHC_HOSTCAPBLT_VS33; |
| 1228 | #endif |
| 1229 | |
| 1230 | if (caps & ESDHC_HOSTCAPBLT_VS18) |
| 1231 | voltage_caps |= MMC_VDD_165_195; |
| 1232 | if (caps & ESDHC_HOSTCAPBLT_VS30) |
| 1233 | voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; |
| 1234 | if (caps & ESDHC_HOSTCAPBLT_VS33) |
| 1235 | voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; |
| 1236 | |
| 1237 | cfg->name = "FSL_SDHC"; |
| 1238 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 1239 | cfg->ops = &esdhc_ops; |
| 1240 | #endif |
| 1241 | #ifdef CONFIG_SYS_SD_VOLTAGE |
| 1242 | cfg->voltages = CONFIG_SYS_SD_VOLTAGE; |
| 1243 | #else |
| 1244 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 1245 | #endif |
| 1246 | if ((cfg->voltages & voltage_caps) == 0) { |
| 1247 | printf("voltage not supported by controller\n"); |
| 1248 | return -1; |
| 1249 | } |
| 1250 | |
| 1251 | if (priv->bus_width == 8) |
| 1252 | cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
| 1253 | else if (priv->bus_width == 4) |
| 1254 | cfg->host_caps = MMC_MODE_4BIT; |
| 1255 | |
| 1256 | cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
| 1257 | #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE |
| 1258 | cfg->host_caps |= MMC_MODE_DDR_52MHz; |
| 1259 | #endif |
| 1260 | |
| 1261 | if (priv->bus_width > 0) { |
| 1262 | if (priv->bus_width < 8) |
| 1263 | cfg->host_caps &= ~MMC_MODE_8BIT; |
| 1264 | if (priv->bus_width < 4) |
| 1265 | cfg->host_caps &= ~MMC_MODE_4BIT; |
| 1266 | } |
| 1267 | |
| 1268 | if (caps & ESDHC_HOSTCAPBLT_HSS) |
| 1269 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
| 1270 | |
| 1271 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 1272 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
| 1273 | cfg->host_caps &= ~MMC_MODE_8BIT; |
| 1274 | #endif |
| 1275 | |
| 1276 | cfg->host_caps |= priv->caps; |
| 1277 | |
| 1278 | cfg->f_min = 400000; |
| 1279 | cfg->f_max = min(priv->sdhc_clk, (u32)200000000); |
| 1280 | |
| 1281 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 1282 | |
| 1283 | writel(0, ®s->dllctrl); |
| 1284 | if (priv->flags & ESDHC_FLAG_USDHC) { |
| 1285 | if (priv->flags & ESDHC_FLAG_STD_TUNING) { |
| 1286 | u32 val = readl(®s->tuning_ctrl); |
| 1287 | |
| 1288 | val |= ESDHC_STD_TUNING_EN; |
| 1289 | val &= ~ESDHC_TUNING_START_TAP_MASK; |
| 1290 | val |= priv->tuning_start_tap; |
| 1291 | val &= ~ESDHC_TUNING_STEP_MASK; |
| 1292 | val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT; |
| 1293 | writel(val, ®s->tuning_ctrl); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | return 0; |
| 1298 | } |
| 1299 | |
| 1300 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 1301 | static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg, |
| 1302 | struct fsl_esdhc_priv *priv) |
| 1303 | { |
| 1304 | if (!cfg || !priv) |
| 1305 | return -EINVAL; |
| 1306 | |
| 1307 | priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); |
| 1308 | priv->bus_width = cfg->max_bus_width; |
| 1309 | priv->sdhc_clk = cfg->sdhc_clk; |
| 1310 | priv->wp_enable = cfg->wp_enable; |
| 1311 | priv->vs18_enable = cfg->vs18_enable; |
| 1312 | |
| 1313 | return 0; |
| 1314 | }; |
| 1315 | |
| 1316 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
| 1317 | { |
| 1318 | struct fsl_esdhc_plat *plat; |
| 1319 | struct fsl_esdhc_priv *priv; |
| 1320 | struct mmc *mmc; |
| 1321 | int ret; |
| 1322 | |
| 1323 | if (!cfg) |
| 1324 | return -EINVAL; |
| 1325 | |
| 1326 | priv = calloc(sizeof(struct fsl_esdhc_priv), 1); |
| 1327 | if (!priv) |
| 1328 | return -ENOMEM; |
| 1329 | plat = calloc(sizeof(struct fsl_esdhc_plat), 1); |
| 1330 | if (!plat) { |
| 1331 | free(priv); |
| 1332 | return -ENOMEM; |
| 1333 | } |
| 1334 | |
| 1335 | ret = fsl_esdhc_cfg_to_priv(cfg, priv); |
| 1336 | if (ret) { |
| 1337 | debug("%s xlate failure\n", __func__); |
| 1338 | free(plat); |
| 1339 | free(priv); |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
| 1343 | ret = fsl_esdhc_init(priv, plat); |
| 1344 | if (ret) { |
| 1345 | debug("%s init failure\n", __func__); |
| 1346 | free(plat); |
| 1347 | free(priv); |
| 1348 | return ret; |
| 1349 | } |
| 1350 | |
| 1351 | mmc = mmc_create(&plat->cfg, priv); |
| 1352 | if (!mmc) |
| 1353 | return -EIO; |
| 1354 | |
| 1355 | priv->mmc = mmc; |
| 1356 | |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
| 1360 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 1361 | { |
| 1362 | struct fsl_esdhc_cfg *cfg; |
| 1363 | |
| 1364 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
| 1365 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
| 1366 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
| 1367 | return fsl_esdhc_initialize(bis, cfg); |
| 1368 | } |
| 1369 | #endif |
| 1370 | |
| 1371 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 1372 | void mmc_adapter_card_type_ident(void) |
| 1373 | { |
| 1374 | u8 card_id; |
| 1375 | u8 value; |
| 1376 | |
| 1377 | card_id = QIXIS_READ(present) & QIXIS_SDID_MASK; |
| 1378 | gd->arch.sdhc_adapter = card_id; |
| 1379 | |
| 1380 | switch (card_id) { |
| 1381 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45: |
| 1382 | value = QIXIS_READ(brdcfg[5]); |
| 1383 | value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7); |
| 1384 | QIXIS_WRITE(brdcfg[5], value); |
| 1385 | break; |
| 1386 | case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY: |
| 1387 | value = QIXIS_READ(pwr_ctl[1]); |
| 1388 | value |= QIXIS_EVDD_BY_SDHC_VS; |
| 1389 | QIXIS_WRITE(pwr_ctl[1], value); |
| 1390 | break; |
| 1391 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44: |
| 1392 | value = QIXIS_READ(brdcfg[5]); |
| 1393 | value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT); |
| 1394 | QIXIS_WRITE(brdcfg[5], value); |
| 1395 | break; |
| 1396 | case QIXIS_ESDHC_ADAPTER_TYPE_RSV: |
| 1397 | break; |
| 1398 | case QIXIS_ESDHC_ADAPTER_TYPE_MMC: |
| 1399 | break; |
| 1400 | case QIXIS_ESDHC_ADAPTER_TYPE_SD: |
| 1401 | break; |
| 1402 | case QIXIS_ESDHC_NO_ADAPTER: |
| 1403 | break; |
| 1404 | default: |
| 1405 | break; |
| 1406 | } |
| 1407 | } |
| 1408 | #endif |
| 1409 | |
| 1410 | #ifdef CONFIG_OF_LIBFDT |
| 1411 | __weak int esdhc_status_fixup(void *blob, const char *compat) |
| 1412 | { |
| 1413 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
| 1414 | if (!hwconfig("esdhc")) { |
| 1415 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 1416 | sizeof("disabled"), 1); |
| 1417 | return 1; |
| 1418 | } |
| 1419 | #endif |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
| 1423 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 1424 | { |
| 1425 | const char *compat = "fsl,esdhc"; |
| 1426 | |
| 1427 | if (esdhc_status_fixup(blob, compat)) |
| 1428 | return; |
| 1429 | |
| 1430 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 1431 | do_fixup_by_compat_u32(blob, compat, "peripheral-frequency", |
| 1432 | gd->arch.sdhc_clk, 1); |
| 1433 | #else |
| 1434 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
| 1435 | gd->arch.sdhc_clk, 1); |
| 1436 | #endif |
| 1437 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 1438 | do_fixup_by_compat_u32(blob, compat, "adapter-type", |
| 1439 | (u32)(gd->arch.sdhc_adapter), 1); |
| 1440 | #endif |
| 1441 | } |
| 1442 | #endif |
| 1443 | |
| 1444 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 1445 | #ifndef CONFIG_PPC |
| 1446 | #include <asm/arch/clock.h> |
| 1447 | #endif |
| 1448 | __weak void init_clk_usdhc(u32 index) |
| 1449 | { |
| 1450 | } |
| 1451 | |
| 1452 | static int fsl_esdhc_probe(struct udevice *dev) |
| 1453 | { |
| 1454 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 1455 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1456 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1457 | const void *fdt = gd->fdt_blob; |
| 1458 | int node = dev_of_offset(dev); |
| 1459 | struct esdhc_soc_data *data = |
| 1460 | (struct esdhc_soc_data *)dev_get_driver_data(dev); |
| 1461 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 1462 | struct udevice *vqmmc_dev; |
| 1463 | #endif |
| 1464 | fdt_addr_t addr; |
| 1465 | unsigned int val; |
| 1466 | struct mmc *mmc; |
| 1467 | #if !CONFIG_IS_ENABLED(BLK) |
| 1468 | struct blk_desc *bdesc; |
| 1469 | #endif |
| 1470 | int ret; |
| 1471 | |
| 1472 | addr = dev_read_addr(dev); |
| 1473 | if (addr == FDT_ADDR_T_NONE) |
| 1474 | return -EINVAL; |
| 1475 | #ifdef CONFIG_PPC |
| 1476 | priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr); |
| 1477 | #else |
| 1478 | priv->esdhc_regs = (struct fsl_esdhc *)addr; |
| 1479 | #endif |
| 1480 | priv->dev = dev; |
| 1481 | priv->mode = -1; |
| 1482 | if (data) { |
| 1483 | priv->flags = data->flags; |
| 1484 | priv->caps = data->caps; |
| 1485 | } |
| 1486 | |
| 1487 | val = dev_read_u32_default(dev, "bus-width", -1); |
| 1488 | if (val == 8) |
| 1489 | priv->bus_width = 8; |
| 1490 | else if (val == 4) |
| 1491 | priv->bus_width = 4; |
| 1492 | else |
| 1493 | priv->bus_width = 1; |
| 1494 | |
| 1495 | val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1); |
| 1496 | priv->tuning_step = val; |
| 1497 | val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap", |
| 1498 | ESDHC_TUNING_START_TAP_DEFAULT); |
| 1499 | priv->tuning_start_tap = val; |
| 1500 | val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target", |
| 1501 | ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT); |
| 1502 | priv->strobe_dll_delay_target = val; |
| 1503 | |
| 1504 | if (dev_read_bool(dev, "non-removable")) { |
| 1505 | priv->non_removable = 1; |
| 1506 | } else { |
| 1507 | priv->non_removable = 0; |
| 1508 | #ifdef CONFIG_DM_GPIO |
| 1509 | gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, |
| 1510 | GPIOD_IS_IN); |
| 1511 | #endif |
| 1512 | } |
| 1513 | |
| 1514 | if (dev_read_prop(dev, "fsl,wp-controller", NULL)) { |
| 1515 | priv->wp_enable = 1; |
| 1516 | } else { |
| 1517 | priv->wp_enable = 0; |
| 1518 | #ifdef CONFIG_DM_GPIO |
| 1519 | gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, |
| 1520 | GPIOD_IS_IN); |
| 1521 | #endif |
| 1522 | } |
| 1523 | |
| 1524 | priv->vs18_enable = 0; |
| 1525 | |
| 1526 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 1527 | /* |
| 1528 | * If emmc I/O has a fixed voltage at 1.8V, this must be provided, |
| 1529 | * otherwise, emmc will work abnormally. |
| 1530 | */ |
| 1531 | ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev); |
| 1532 | if (ret) { |
| 1533 | dev_dbg(dev, "no vqmmc-supply\n"); |
| 1534 | } else { |
| 1535 | ret = regulator_set_enable(vqmmc_dev, true); |
| 1536 | if (ret) { |
| 1537 | dev_err(dev, "fail to enable vqmmc-supply\n"); |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
| 1541 | if (regulator_get_value(vqmmc_dev) == 1800000) |
| 1542 | priv->vs18_enable = 1; |
| 1543 | } |
| 1544 | #endif |
| 1545 | |
| 1546 | if (fdt_get_property(fdt, node, "no-1-8-v", NULL)) |
| 1547 | priv->caps &= ~(UHS_CAPS | MMC_MODE_HS200 | MMC_MODE_HS400); |
| 1548 | |
| 1549 | /* |
| 1550 | * TODO: |
| 1551 | * Because lack of clk driver, if SDHC clk is not enabled, |
| 1552 | * need to enable it first before this driver is invoked. |
| 1553 | * |
| 1554 | * we use MXC_ESDHC_CLK to get clk freq. |
| 1555 | * If one would like to make this function work, |
| 1556 | * the aliases should be provided in dts as this: |
| 1557 | * |
| 1558 | * aliases { |
| 1559 | * mmc0 = &usdhc1; |
| 1560 | * mmc1 = &usdhc2; |
| 1561 | * mmc2 = &usdhc3; |
| 1562 | * mmc3 = &usdhc4; |
| 1563 | * }; |
| 1564 | * Then if your board only supports mmc2 and mmc3, but we can |
| 1565 | * correctly get the seq as 2 and 3, then let mxc_get_clock |
| 1566 | * work as expected. |
| 1567 | */ |
| 1568 | |
| 1569 | init_clk_usdhc(dev->seq); |
| 1570 | |
| 1571 | if (IS_ENABLED(CONFIG_CLK)) { |
| 1572 | /* Assigned clock already set clock */ |
| 1573 | ret = clk_get_by_name(dev, "per", &priv->per_clk); |
| 1574 | if (ret) { |
| 1575 | printf("Failed to get per_clk\n"); |
| 1576 | return ret; |
| 1577 | } |
| 1578 | ret = clk_enable(&priv->per_clk); |
| 1579 | if (ret) { |
| 1580 | printf("Failed to enable per_clk\n"); |
| 1581 | return ret; |
| 1582 | } |
| 1583 | |
| 1584 | priv->sdhc_clk = clk_get_rate(&priv->per_clk); |
| 1585 | } else { |
| 1586 | #ifndef CONFIG_PPC |
| 1587 | priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq); |
| 1588 | #else |
| 1589 | priv->sdhc_clk = gd->arch.sdhc_clk; |
| 1590 | #endif |
| 1591 | if (priv->sdhc_clk <= 0) { |
| 1592 | dev_err(dev, "Unable to get clk for %s\n", dev->name); |
| 1593 | return -EINVAL; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | ret = fsl_esdhc_init(priv, plat); |
| 1598 | if (ret) { |
| 1599 | dev_err(dev, "fsl_esdhc_init failure\n"); |
| 1600 | return ret; |
| 1601 | } |
| 1602 | |
| 1603 | mmc = &plat->mmc; |
| 1604 | mmc->cfg = &plat->cfg; |
| 1605 | mmc->dev = dev; |
| 1606 | #if !CONFIG_IS_ENABLED(BLK) |
| 1607 | mmc->priv = priv; |
| 1608 | |
| 1609 | /* Setup dsr related values */ |
| 1610 | mmc->dsr_imp = 0; |
| 1611 | mmc->dsr = ESDHC_DRIVER_STAGE_VALUE; |
| 1612 | /* Setup the universal parts of the block interface just once */ |
| 1613 | bdesc = mmc_get_blk_desc(mmc); |
| 1614 | bdesc->if_type = IF_TYPE_MMC; |
| 1615 | bdesc->removable = 1; |
| 1616 | bdesc->devnum = mmc_get_next_devnum(); |
| 1617 | bdesc->block_read = mmc_bread; |
| 1618 | bdesc->block_write = mmc_bwrite; |
| 1619 | bdesc->block_erase = mmc_berase; |
| 1620 | |
| 1621 | /* setup initial part type */ |
| 1622 | bdesc->part_type = mmc->cfg->part_type; |
| 1623 | mmc_list_add(mmc); |
| 1624 | #endif |
| 1625 | |
| 1626 | upriv->mmc = mmc; |
| 1627 | |
| 1628 | return esdhc_init_common(priv, mmc); |
| 1629 | } |
| 1630 | |
| 1631 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 1632 | static int fsl_esdhc_get_cd(struct udevice *dev) |
| 1633 | { |
| 1634 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1635 | |
| 1636 | return esdhc_getcd_common(priv); |
| 1637 | } |
| 1638 | |
| 1639 | static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 1640 | struct mmc_data *data) |
| 1641 | { |
| 1642 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1643 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1644 | |
| 1645 | return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data); |
| 1646 | } |
| 1647 | |
| 1648 | static int fsl_esdhc_set_ios(struct udevice *dev) |
| 1649 | { |
| 1650 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1651 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1652 | |
| 1653 | return esdhc_set_ios_common(priv, &plat->mmc); |
| 1654 | } |
| 1655 | |
| 1656 | static const struct dm_mmc_ops fsl_esdhc_ops = { |
| 1657 | .get_cd = fsl_esdhc_get_cd, |
| 1658 | .send_cmd = fsl_esdhc_send_cmd, |
| 1659 | .set_ios = fsl_esdhc_set_ios, |
| 1660 | #ifdef MMC_SUPPORTS_TUNING |
| 1661 | .execute_tuning = fsl_esdhc_execute_tuning, |
| 1662 | #endif |
| 1663 | }; |
| 1664 | #endif |
| 1665 | |
| 1666 | static struct esdhc_soc_data usdhc_imx7d_data = { |
| 1667 | .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
| 1668 | | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
| 1669 | | ESDHC_FLAG_HS400, |
| 1670 | .caps = UHS_CAPS | MMC_MODE_HS200 | MMC_MODE_DDR_52MHz | |
| 1671 | MMC_MODE_HS_52MHz | MMC_MODE_HS, |
| 1672 | }; |
| 1673 | |
| 1674 | static const struct udevice_id fsl_esdhc_ids[] = { |
| 1675 | { .compatible = "fsl,imx53-esdhc", }, |
| 1676 | { .compatible = "fsl,imx6ul-usdhc", }, |
| 1677 | { .compatible = "fsl,imx6sx-usdhc", }, |
| 1678 | { .compatible = "fsl,imx6sl-usdhc", }, |
| 1679 | { .compatible = "fsl,imx6q-usdhc", }, |
| 1680 | { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,}, |
| 1681 | { .compatible = "fsl,imx7ulp-usdhc", }, |
| 1682 | { .compatible = "fsl,esdhc", }, |
| 1683 | { /* sentinel */ } |
| 1684 | }; |
| 1685 | |
| 1686 | #if CONFIG_IS_ENABLED(BLK) |
| 1687 | static int fsl_esdhc_bind(struct udevice *dev) |
| 1688 | { |
| 1689 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1690 | |
| 1691 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 1692 | } |
| 1693 | #endif |
| 1694 | |
| 1695 | U_BOOT_DRIVER(fsl_esdhc) = { |
| 1696 | .name = "fsl-esdhc-mmc", |
| 1697 | .id = UCLASS_MMC, |
| 1698 | .of_match = fsl_esdhc_ids, |
| 1699 | .ops = &fsl_esdhc_ops, |
| 1700 | #if CONFIG_IS_ENABLED(BLK) |
| 1701 | .bind = fsl_esdhc_bind, |
| 1702 | #endif |
| 1703 | .probe = fsl_esdhc_probe, |
| 1704 | .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat), |
| 1705 | .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv), |
| 1706 | }; |
| 1707 | #endif |