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