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