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; |
Giulio Benetti | 65b5ec1 | 2020-01-10 15:51:46 +0100 | [diff] [blame] | 81 | uint tuning_ctrl; /* on i.MX6/7/8/RT */ |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 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 | |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 664 | #ifdef MMC_SUPPORTS_TUNING |
| 665 | static int esdhc_change_pinstate(struct udevice *dev) |
| 666 | { |
| 667 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 668 | int ret; |
| 669 | |
| 670 | switch (priv->mode) { |
| 671 | case UHS_SDR50: |
| 672 | case UHS_DDR50: |
| 673 | ret = pinctrl_select_state(dev, "state_100mhz"); |
| 674 | break; |
| 675 | case UHS_SDR104: |
| 676 | case MMC_HS_200: |
| 677 | case MMC_HS_400: |
Peng Fan | 69b9d3a | 2019-07-10 09:35:26 +0000 | [diff] [blame] | 678 | case MMC_HS_400_ES: |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 679 | ret = pinctrl_select_state(dev, "state_200mhz"); |
| 680 | break; |
| 681 | default: |
| 682 | ret = pinctrl_select_state(dev, "default"); |
| 683 | break; |
| 684 | } |
| 685 | |
| 686 | if (ret) |
| 687 | printf("%s %d error\n", __func__, priv->mode); |
| 688 | |
| 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | static void esdhc_reset_tuning(struct mmc *mmc) |
| 693 | { |
| 694 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 695 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 696 | |
| 697 | if (priv->flags & ESDHC_FLAG_USDHC) { |
| 698 | if (priv->flags & ESDHC_FLAG_STD_TUNING) { |
| 699 | esdhc_clrbits32(®s->autoc12err, |
| 700 | MIX_CTRL_SMPCLK_SEL | |
| 701 | MIX_CTRL_EXE_TUNE); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | static void esdhc_set_strobe_dll(struct mmc *mmc) |
| 707 | { |
| 708 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 709 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 710 | u32 val; |
| 711 | |
| 712 | if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) { |
| 713 | writel(ESDHC_STROBE_DLL_CTRL_RESET, ®s->strobe_dllctrl); |
| 714 | |
| 715 | /* |
| 716 | * enable strobe dll ctrl and adjust the delay target |
| 717 | * for the uSDHC loopback read clock |
| 718 | */ |
| 719 | val = ESDHC_STROBE_DLL_CTRL_ENABLE | |
| 720 | (priv->strobe_dll_delay_target << |
| 721 | ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT); |
| 722 | writel(val, ®s->strobe_dllctrl); |
| 723 | /* wait 1us to make sure strobe dll status register stable */ |
| 724 | mdelay(1); |
| 725 | val = readl(®s->strobe_dllstat); |
| 726 | if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK)) |
| 727 | pr_warn("HS400 strobe DLL status REF not lock!\n"); |
| 728 | if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK)) |
| 729 | pr_warn("HS400 strobe DLL status SLV not lock!\n"); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | static int esdhc_set_timing(struct mmc *mmc) |
| 734 | { |
| 735 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 736 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 737 | u32 mixctrl; |
| 738 | |
| 739 | mixctrl = readl(®s->mixctrl); |
| 740 | mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN); |
| 741 | |
| 742 | switch (mmc->selected_mode) { |
| 743 | case MMC_LEGACY: |
| 744 | case SD_LEGACY: |
| 745 | esdhc_reset_tuning(mmc); |
| 746 | writel(mixctrl, ®s->mixctrl); |
| 747 | break; |
| 748 | case MMC_HS_400: |
Peng Fan | 69b9d3a | 2019-07-10 09:35:26 +0000 | [diff] [blame] | 749 | case MMC_HS_400_ES: |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 750 | mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN; |
| 751 | writel(mixctrl, ®s->mixctrl); |
| 752 | esdhc_set_strobe_dll(mmc); |
| 753 | break; |
| 754 | case MMC_HS: |
| 755 | case MMC_HS_52: |
| 756 | case MMC_HS_200: |
| 757 | case SD_HS: |
| 758 | case UHS_SDR12: |
| 759 | case UHS_SDR25: |
| 760 | case UHS_SDR50: |
| 761 | case UHS_SDR104: |
| 762 | writel(mixctrl, ®s->mixctrl); |
| 763 | break; |
| 764 | case UHS_DDR50: |
| 765 | case MMC_DDR_52: |
| 766 | mixctrl |= MIX_CTRL_DDREN; |
| 767 | writel(mixctrl, ®s->mixctrl); |
| 768 | break; |
| 769 | default: |
| 770 | printf("Not supported %d\n", mmc->selected_mode); |
| 771 | return -EINVAL; |
| 772 | } |
| 773 | |
| 774 | priv->mode = mmc->selected_mode; |
| 775 | |
| 776 | return esdhc_change_pinstate(mmc->dev); |
| 777 | } |
| 778 | |
| 779 | static int esdhc_set_voltage(struct mmc *mmc) |
| 780 | { |
| 781 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 782 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 783 | int ret; |
| 784 | |
| 785 | priv->signal_voltage = mmc->signal_voltage; |
| 786 | switch (mmc->signal_voltage) { |
| 787 | case MMC_SIGNAL_VOLTAGE_330: |
| 788 | if (priv->vs18_enable) |
| 789 | return -EIO; |
| 790 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 791 | if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) { |
| 792 | ret = regulator_set_value(priv->vqmmc_dev, 3300000); |
| 793 | if (ret) { |
| 794 | printf("Setting to 3.3V error"); |
| 795 | return -EIO; |
| 796 | } |
| 797 | /* Wait for 5ms */ |
| 798 | mdelay(5); |
| 799 | } |
| 800 | #endif |
| 801 | |
| 802 | esdhc_clrbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 803 | if (!(esdhc_read32(®s->vendorspec) & |
| 804 | ESDHC_VENDORSPEC_VSELECT)) |
| 805 | return 0; |
| 806 | |
| 807 | return -EAGAIN; |
| 808 | case MMC_SIGNAL_VOLTAGE_180: |
| 809 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 810 | if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) { |
| 811 | ret = regulator_set_value(priv->vqmmc_dev, 1800000); |
| 812 | if (ret) { |
| 813 | printf("Setting to 1.8V error"); |
| 814 | return -EIO; |
| 815 | } |
| 816 | } |
| 817 | #endif |
| 818 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 819 | if (esdhc_read32(®s->vendorspec) & ESDHC_VENDORSPEC_VSELECT) |
| 820 | return 0; |
| 821 | |
| 822 | return -EAGAIN; |
| 823 | case MMC_SIGNAL_VOLTAGE_120: |
| 824 | return -ENOTSUPP; |
| 825 | default: |
| 826 | return 0; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | static void esdhc_stop_tuning(struct mmc *mmc) |
| 831 | { |
| 832 | struct mmc_cmd cmd; |
| 833 | |
| 834 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
| 835 | cmd.cmdarg = 0; |
| 836 | cmd.resp_type = MMC_RSP_R1b; |
| 837 | |
| 838 | dm_mmc_send_cmd(mmc->dev, &cmd, NULL); |
| 839 | } |
| 840 | |
| 841 | static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode) |
| 842 | { |
| 843 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 844 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 845 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 846 | struct mmc *mmc = &plat->mmc; |
| 847 | u32 irqstaten = readl(®s->irqstaten); |
| 848 | u32 irqsigen = readl(®s->irqsigen); |
| 849 | int i, ret = -ETIMEDOUT; |
| 850 | u32 val, mixctrl; |
| 851 | |
| 852 | /* clock tuning is not needed for upto 52MHz */ |
| 853 | if (mmc->clock <= 52000000) |
| 854 | return 0; |
| 855 | |
| 856 | /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */ |
| 857 | if (priv->flags & ESDHC_FLAG_STD_TUNING) { |
| 858 | val = readl(®s->autoc12err); |
| 859 | mixctrl = readl(®s->mixctrl); |
| 860 | val &= ~MIX_CTRL_SMPCLK_SEL; |
| 861 | mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN); |
| 862 | |
| 863 | val |= MIX_CTRL_EXE_TUNE; |
| 864 | mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN; |
| 865 | |
| 866 | writel(val, ®s->autoc12err); |
| 867 | writel(mixctrl, ®s->mixctrl); |
| 868 | } |
| 869 | |
| 870 | /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */ |
| 871 | mixctrl = readl(®s->mixctrl); |
| 872 | mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK); |
| 873 | writel(mixctrl, ®s->mixctrl); |
| 874 | |
| 875 | writel(IRQSTATEN_BRR, ®s->irqstaten); |
| 876 | writel(IRQSTATEN_BRR, ®s->irqsigen); |
| 877 | |
| 878 | /* |
| 879 | * Issue opcode repeatedly till Execute Tuning is set to 0 or the number |
| 880 | * of loops reaches 40 times. |
| 881 | */ |
| 882 | for (i = 0; i < MAX_TUNING_LOOP; i++) { |
| 883 | u32 ctrl; |
| 884 | |
| 885 | if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) { |
| 886 | if (mmc->bus_width == 8) |
| 887 | writel(0x7080, ®s->blkattr); |
| 888 | else if (mmc->bus_width == 4) |
| 889 | writel(0x7040, ®s->blkattr); |
| 890 | } else { |
| 891 | writel(0x7040, ®s->blkattr); |
| 892 | } |
| 893 | |
| 894 | /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */ |
| 895 | val = readl(®s->mixctrl); |
| 896 | val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK); |
| 897 | writel(val, ®s->mixctrl); |
| 898 | |
| 899 | /* We are using STD tuning, no need to check return value */ |
| 900 | mmc_send_tuning(mmc, opcode, NULL); |
| 901 | |
| 902 | ctrl = readl(®s->autoc12err); |
| 903 | if ((!(ctrl & MIX_CTRL_EXE_TUNE)) && |
| 904 | (ctrl & MIX_CTRL_SMPCLK_SEL)) { |
| 905 | /* |
| 906 | * need to wait some time, make sure sd/mmc fininsh |
| 907 | * send out tuning data, otherwise, the sd/mmc can't |
| 908 | * response to any command when the card still out |
| 909 | * put the tuning data. |
| 910 | */ |
| 911 | mdelay(1); |
| 912 | ret = 0; |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | /* Add 1ms delay for SD and eMMC */ |
| 917 | mdelay(1); |
| 918 | } |
| 919 | |
| 920 | writel(irqstaten, ®s->irqstaten); |
| 921 | writel(irqsigen, ®s->irqsigen); |
| 922 | |
| 923 | esdhc_stop_tuning(mmc); |
| 924 | |
| 925 | return ret; |
| 926 | } |
| 927 | #endif |
| 928 | |
| 929 | static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc) |
| 930 | { |
| 931 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 932 | int ret __maybe_unused; |
Peng Fan | 2cdf52b | 2019-11-04 17:14:15 +0800 | [diff] [blame] | 933 | u32 clock; |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 934 | |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 935 | /* Set the clock speed */ |
Peng Fan | 2cdf52b | 2019-11-04 17:14:15 +0800 | [diff] [blame] | 936 | clock = mmc->clock; |
| 937 | if (clock < mmc->cfg->f_min) |
| 938 | clock = mmc->cfg->f_min; |
| 939 | |
| 940 | if (priv->clock != clock) |
| 941 | set_sysctl(priv, mmc, clock); |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 942 | |
| 943 | #ifdef MMC_SUPPORTS_TUNING |
| 944 | if (mmc->clk_disable) { |
| 945 | #ifdef CONFIG_FSL_USDHC |
| 946 | esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN); |
| 947 | #else |
| 948 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
| 949 | #endif |
| 950 | } else { |
| 951 | #ifdef CONFIG_FSL_USDHC |
| 952 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | |
| 953 | VENDORSPEC_CKEN); |
| 954 | #else |
| 955 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
| 956 | #endif |
| 957 | } |
| 958 | |
| 959 | if (priv->mode != mmc->selected_mode) { |
| 960 | ret = esdhc_set_timing(mmc); |
| 961 | if (ret) { |
| 962 | printf("esdhc_set_timing error %d\n", ret); |
| 963 | return ret; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | if (priv->signal_voltage != mmc->signal_voltage) { |
| 968 | ret = esdhc_set_voltage(mmc); |
| 969 | if (ret) { |
| 970 | printf("esdhc_set_voltage error %d\n", ret); |
| 971 | return ret; |
| 972 | } |
| 973 | } |
| 974 | #endif |
| 975 | |
| 976 | /* Set the bus width */ |
| 977 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
| 978 | |
| 979 | if (mmc->bus_width == 4) |
| 980 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
| 981 | else if (mmc->bus_width == 8) |
| 982 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 983 | |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc) |
| 988 | { |
| 989 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 990 | ulong start; |
| 991 | |
| 992 | /* Reset the entire host controller */ |
| 993 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
| 994 | |
| 995 | /* Wait until the controller is available */ |
| 996 | start = get_timer(0); |
| 997 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 998 | if (get_timer(start) > 1000) |
| 999 | return -ETIMEDOUT; |
| 1000 | } |
| 1001 | |
| 1002 | #if defined(CONFIG_FSL_USDHC) |
| 1003 | /* RSTA doesn't reset MMC_BOOT register, so manually reset it */ |
| 1004 | esdhc_write32(®s->mmcboot, 0x0); |
| 1005 | /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */ |
| 1006 | esdhc_write32(®s->mixctrl, 0x0); |
| 1007 | esdhc_write32(®s->clktunectrlstatus, 0x0); |
| 1008 | |
| 1009 | /* Put VEND_SPEC to default value */ |
| 1010 | if (priv->vs18_enable) |
| 1011 | esdhc_write32(®s->vendorspec, (VENDORSPEC_INIT | |
| 1012 | ESDHC_VENDORSPEC_VSELECT)); |
| 1013 | else |
| 1014 | esdhc_write32(®s->vendorspec, VENDORSPEC_INIT); |
| 1015 | |
| 1016 | /* Disable DLL_CTRL delay line */ |
| 1017 | esdhc_write32(®s->dllctrl, 0x0); |
| 1018 | #endif |
| 1019 | |
| 1020 | #ifndef ARCH_MXC |
| 1021 | /* Enable cache snooping */ |
| 1022 | esdhc_write32(®s->scr, 0x00000040); |
| 1023 | #endif |
| 1024 | |
| 1025 | #ifndef CONFIG_FSL_USDHC |
| 1026 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
| 1027 | #else |
| 1028 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN); |
| 1029 | #endif |
| 1030 | |
| 1031 | /* Set the initial clock speed */ |
| 1032 | mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE); |
| 1033 | |
| 1034 | /* Disable the BRR and BWR bits in IRQSTAT */ |
| 1035 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
| 1036 | |
| 1037 | #ifdef CONFIG_MCF5441x |
| 1038 | esdhc_write32(®s->proctl, PROCTL_INIT | PROCTL_D3CD); |
| 1039 | #else |
| 1040 | /* Put the PROCTL reg back to the default */ |
| 1041 | esdhc_write32(®s->proctl, PROCTL_INIT); |
| 1042 | #endif |
| 1043 | |
| 1044 | /* Set timout to the maximum value */ |
| 1045 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
| 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | static int esdhc_getcd_common(struct fsl_esdhc_priv *priv) |
| 1051 | { |
| 1052 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 1053 | int timeout = 1000; |
| 1054 | |
| 1055 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 1056 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 1057 | return 1; |
| 1058 | #endif |
| 1059 | |
| 1060 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 1061 | if (priv->non_removable) |
| 1062 | return 1; |
Fabio Estevam | 7e3d8a9 | 2020-01-06 20:11:27 -0300 | [diff] [blame] | 1063 | |
| 1064 | if (priv->broken_cd) |
| 1065 | return 1; |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 1066 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1067 | if (dm_gpio_is_valid(&priv->cd_gpio)) |
| 1068 | return dm_gpio_get_value(&priv->cd_gpio); |
| 1069 | #endif |
| 1070 | #endif |
| 1071 | |
| 1072 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 1073 | udelay(1000); |
| 1074 | |
| 1075 | return timeout > 0; |
| 1076 | } |
| 1077 | |
| 1078 | static int esdhc_reset(struct fsl_esdhc *regs) |
| 1079 | { |
| 1080 | ulong start; |
| 1081 | |
| 1082 | /* reset the controller */ |
| 1083 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
| 1084 | |
| 1085 | /* hardware clears the bit when it is done */ |
| 1086 | start = get_timer(0); |
| 1087 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 1088 | if (get_timer(start) > 100) { |
| 1089 | printf("MMC/SD: Reset never completed.\n"); |
| 1090 | return -ETIMEDOUT; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 1098 | static int esdhc_getcd(struct mmc *mmc) |
| 1099 | { |
| 1100 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1101 | |
| 1102 | return esdhc_getcd_common(priv); |
| 1103 | } |
| 1104 | |
| 1105 | static int esdhc_init(struct mmc *mmc) |
| 1106 | { |
| 1107 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1108 | |
| 1109 | return esdhc_init_common(priv, mmc); |
| 1110 | } |
| 1111 | |
| 1112 | static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 1113 | struct mmc_data *data) |
| 1114 | { |
| 1115 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1116 | |
| 1117 | return esdhc_send_cmd_common(priv, mmc, cmd, data); |
| 1118 | } |
| 1119 | |
| 1120 | static int esdhc_set_ios(struct mmc *mmc) |
| 1121 | { |
| 1122 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1123 | |
| 1124 | return esdhc_set_ios_common(priv, mmc); |
| 1125 | } |
| 1126 | |
| 1127 | static const struct mmc_ops esdhc_ops = { |
| 1128 | .getcd = esdhc_getcd, |
| 1129 | .init = esdhc_init, |
| 1130 | .send_cmd = esdhc_send_cmd, |
| 1131 | .set_ios = esdhc_set_ios, |
| 1132 | }; |
| 1133 | #endif |
| 1134 | |
| 1135 | static int fsl_esdhc_init(struct fsl_esdhc_priv *priv, |
| 1136 | struct fsl_esdhc_plat *plat) |
| 1137 | { |
| 1138 | struct mmc_config *cfg; |
| 1139 | struct fsl_esdhc *regs; |
| 1140 | u32 caps, voltage_caps; |
| 1141 | int ret; |
| 1142 | |
| 1143 | if (!priv) |
| 1144 | return -EINVAL; |
| 1145 | |
| 1146 | regs = priv->esdhc_regs; |
| 1147 | |
| 1148 | /* First reset the eSDHC controller */ |
| 1149 | ret = esdhc_reset(regs); |
| 1150 | if (ret) |
| 1151 | return ret; |
| 1152 | |
| 1153 | #ifdef CONFIG_MCF5441x |
| 1154 | /* ColdFire, using SDHC_DATA[3] for card detection */ |
| 1155 | esdhc_write32(®s->proctl, PROCTL_INIT | PROCTL_D3CD); |
| 1156 | #endif |
| 1157 | |
| 1158 | #ifndef CONFIG_FSL_USDHC |
| 1159 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
| 1160 | | SYSCTL_IPGEN | SYSCTL_CKEN); |
| 1161 | /* Clearing tuning bits in case ROM has set it already */ |
| 1162 | esdhc_write32(®s->mixctrl, 0); |
| 1163 | esdhc_write32(®s->autoc12err, 0); |
| 1164 | esdhc_write32(®s->clktunectrlstatus, 0); |
| 1165 | #else |
| 1166 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | |
| 1167 | VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN); |
| 1168 | #endif |
| 1169 | |
| 1170 | if (priv->vs18_enable) |
| 1171 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 1172 | |
| 1173 | writel(SDHCI_IRQ_EN_BITS, ®s->irqstaten); |
| 1174 | cfg = &plat->cfg; |
| 1175 | #ifndef CONFIG_DM_MMC |
| 1176 | memset(cfg, '\0', sizeof(*cfg)); |
| 1177 | #endif |
| 1178 | |
| 1179 | voltage_caps = 0; |
| 1180 | caps = esdhc_read32(®s->hostcapblt); |
| 1181 | |
| 1182 | #ifdef CONFIG_MCF5441x |
| 1183 | /* |
| 1184 | * MCF5441x RM declares in more points that sdhc clock speed must |
| 1185 | * never exceed 25 Mhz. From this, the HS bit needs to be disabled |
| 1186 | * from host capabilities. |
| 1187 | */ |
| 1188 | caps &= ~ESDHC_HOSTCAPBLT_HSS; |
| 1189 | #endif |
| 1190 | |
| 1191 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
| 1192 | caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | |
| 1193 | ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); |
| 1194 | #endif |
| 1195 | |
| 1196 | /* T4240 host controller capabilities register should have VS33 bit */ |
| 1197 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
| 1198 | caps = caps | ESDHC_HOSTCAPBLT_VS33; |
| 1199 | #endif |
| 1200 | |
| 1201 | if (caps & ESDHC_HOSTCAPBLT_VS18) |
| 1202 | voltage_caps |= MMC_VDD_165_195; |
| 1203 | if (caps & ESDHC_HOSTCAPBLT_VS30) |
| 1204 | voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; |
| 1205 | if (caps & ESDHC_HOSTCAPBLT_VS33) |
| 1206 | voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; |
| 1207 | |
| 1208 | cfg->name = "FSL_SDHC"; |
| 1209 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 1210 | cfg->ops = &esdhc_ops; |
| 1211 | #endif |
| 1212 | #ifdef CONFIG_SYS_SD_VOLTAGE |
| 1213 | cfg->voltages = CONFIG_SYS_SD_VOLTAGE; |
| 1214 | #else |
| 1215 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 1216 | #endif |
| 1217 | if ((cfg->voltages & voltage_caps) == 0) { |
| 1218 | printf("voltage not supported by controller\n"); |
| 1219 | return -1; |
| 1220 | } |
| 1221 | |
| 1222 | if (priv->bus_width == 8) |
| 1223 | cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
| 1224 | else if (priv->bus_width == 4) |
| 1225 | cfg->host_caps = MMC_MODE_4BIT; |
| 1226 | |
| 1227 | cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
| 1228 | #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE |
| 1229 | cfg->host_caps |= MMC_MODE_DDR_52MHz; |
| 1230 | #endif |
| 1231 | |
| 1232 | if (priv->bus_width > 0) { |
| 1233 | if (priv->bus_width < 8) |
| 1234 | cfg->host_caps &= ~MMC_MODE_8BIT; |
| 1235 | if (priv->bus_width < 4) |
| 1236 | cfg->host_caps &= ~MMC_MODE_4BIT; |
| 1237 | } |
| 1238 | |
| 1239 | if (caps & ESDHC_HOSTCAPBLT_HSS) |
| 1240 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
| 1241 | |
| 1242 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 1243 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
| 1244 | cfg->host_caps &= ~MMC_MODE_8BIT; |
| 1245 | #endif |
| 1246 | |
| 1247 | cfg->host_caps |= priv->caps; |
| 1248 | |
| 1249 | cfg->f_min = 400000; |
| 1250 | cfg->f_max = min(priv->sdhc_clk, (u32)200000000); |
| 1251 | |
| 1252 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 1253 | |
| 1254 | writel(0, ®s->dllctrl); |
| 1255 | if (priv->flags & ESDHC_FLAG_USDHC) { |
| 1256 | if (priv->flags & ESDHC_FLAG_STD_TUNING) { |
| 1257 | u32 val = readl(®s->tuning_ctrl); |
| 1258 | |
| 1259 | val |= ESDHC_STD_TUNING_EN; |
| 1260 | val &= ~ESDHC_TUNING_START_TAP_MASK; |
| 1261 | val |= priv->tuning_start_tap; |
| 1262 | val &= ~ESDHC_TUNING_STEP_MASK; |
| 1263 | val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT; |
| 1264 | writel(val, ®s->tuning_ctrl); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
| 1271 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 1272 | static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg, |
| 1273 | struct fsl_esdhc_priv *priv) |
| 1274 | { |
| 1275 | if (!cfg || !priv) |
| 1276 | return -EINVAL; |
| 1277 | |
| 1278 | priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); |
| 1279 | priv->bus_width = cfg->max_bus_width; |
| 1280 | priv->sdhc_clk = cfg->sdhc_clk; |
| 1281 | priv->wp_enable = cfg->wp_enable; |
| 1282 | priv->vs18_enable = cfg->vs18_enable; |
| 1283 | |
| 1284 | return 0; |
| 1285 | }; |
| 1286 | |
| 1287 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
| 1288 | { |
| 1289 | struct fsl_esdhc_plat *plat; |
| 1290 | struct fsl_esdhc_priv *priv; |
| 1291 | struct mmc *mmc; |
| 1292 | int ret; |
| 1293 | |
| 1294 | if (!cfg) |
| 1295 | return -EINVAL; |
| 1296 | |
| 1297 | priv = calloc(sizeof(struct fsl_esdhc_priv), 1); |
| 1298 | if (!priv) |
| 1299 | return -ENOMEM; |
| 1300 | plat = calloc(sizeof(struct fsl_esdhc_plat), 1); |
| 1301 | if (!plat) { |
| 1302 | free(priv); |
| 1303 | return -ENOMEM; |
| 1304 | } |
| 1305 | |
| 1306 | ret = fsl_esdhc_cfg_to_priv(cfg, priv); |
| 1307 | if (ret) { |
| 1308 | debug("%s xlate failure\n", __func__); |
| 1309 | free(plat); |
| 1310 | free(priv); |
| 1311 | return ret; |
| 1312 | } |
| 1313 | |
| 1314 | ret = fsl_esdhc_init(priv, plat); |
| 1315 | if (ret) { |
| 1316 | debug("%s init failure\n", __func__); |
| 1317 | free(plat); |
| 1318 | free(priv); |
| 1319 | return ret; |
| 1320 | } |
| 1321 | |
| 1322 | mmc = mmc_create(&plat->cfg, priv); |
| 1323 | if (!mmc) |
| 1324 | return -EIO; |
| 1325 | |
| 1326 | priv->mmc = mmc; |
| 1327 | |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
| 1331 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 1332 | { |
| 1333 | struct fsl_esdhc_cfg *cfg; |
| 1334 | |
| 1335 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
| 1336 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
| 1337 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
| 1338 | return fsl_esdhc_initialize(bis, cfg); |
| 1339 | } |
| 1340 | #endif |
| 1341 | |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1342 | #ifdef CONFIG_OF_LIBFDT |
| 1343 | __weak int esdhc_status_fixup(void *blob, const char *compat) |
| 1344 | { |
| 1345 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
| 1346 | if (!hwconfig("esdhc")) { |
| 1347 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 1348 | sizeof("disabled"), 1); |
| 1349 | return 1; |
| 1350 | } |
| 1351 | #endif |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 1356 | { |
| 1357 | const char *compat = "fsl,esdhc"; |
| 1358 | |
| 1359 | if (esdhc_status_fixup(blob, compat)) |
| 1360 | return; |
| 1361 | |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1362 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
| 1363 | gd->arch.sdhc_clk, 1); |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1364 | } |
| 1365 | #endif |
| 1366 | |
| 1367 | #if CONFIG_IS_ENABLED(DM_MMC) |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1368 | #include <asm/arch/clock.h> |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1369 | __weak void init_clk_usdhc(u32 index) |
| 1370 | { |
| 1371 | } |
| 1372 | |
| 1373 | static int fsl_esdhc_probe(struct udevice *dev) |
| 1374 | { |
| 1375 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 1376 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1377 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1378 | const void *fdt = gd->fdt_blob; |
| 1379 | int node = dev_of_offset(dev); |
| 1380 | struct esdhc_soc_data *data = |
| 1381 | (struct esdhc_soc_data *)dev_get_driver_data(dev); |
| 1382 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 1383 | struct udevice *vqmmc_dev; |
| 1384 | #endif |
| 1385 | fdt_addr_t addr; |
| 1386 | unsigned int val; |
| 1387 | struct mmc *mmc; |
| 1388 | #if !CONFIG_IS_ENABLED(BLK) |
| 1389 | struct blk_desc *bdesc; |
| 1390 | #endif |
| 1391 | int ret; |
| 1392 | |
| 1393 | addr = dev_read_addr(dev); |
| 1394 | if (addr == FDT_ADDR_T_NONE) |
| 1395 | return -EINVAL; |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1396 | priv->esdhc_regs = (struct fsl_esdhc *)addr; |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1397 | priv->dev = dev; |
| 1398 | priv->mode = -1; |
Peng Fan | 3766a48 | 2019-07-10 09:35:24 +0000 | [diff] [blame] | 1399 | if (data) |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1400 | priv->flags = data->flags; |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1401 | |
| 1402 | val = dev_read_u32_default(dev, "bus-width", -1); |
| 1403 | if (val == 8) |
| 1404 | priv->bus_width = 8; |
| 1405 | else if (val == 4) |
| 1406 | priv->bus_width = 4; |
| 1407 | else |
| 1408 | priv->bus_width = 1; |
| 1409 | |
| 1410 | val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1); |
| 1411 | priv->tuning_step = val; |
| 1412 | val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap", |
| 1413 | ESDHC_TUNING_START_TAP_DEFAULT); |
| 1414 | priv->tuning_start_tap = val; |
| 1415 | val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target", |
| 1416 | ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT); |
| 1417 | priv->strobe_dll_delay_target = val; |
| 1418 | |
Fabio Estevam | 7e3d8a9 | 2020-01-06 20:11:27 -0300 | [diff] [blame] | 1419 | if (dev_read_bool(dev, "broken-cd")) |
| 1420 | priv->broken_cd = 1; |
| 1421 | |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1422 | if (dev_read_bool(dev, "non-removable")) { |
| 1423 | priv->non_removable = 1; |
| 1424 | } else { |
| 1425 | priv->non_removable = 0; |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 1426 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1427 | gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, |
| 1428 | GPIOD_IS_IN); |
| 1429 | #endif |
| 1430 | } |
| 1431 | |
| 1432 | if (dev_read_prop(dev, "fsl,wp-controller", NULL)) { |
| 1433 | priv->wp_enable = 1; |
| 1434 | } else { |
| 1435 | priv->wp_enable = 0; |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 1436 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1437 | gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, |
| 1438 | GPIOD_IS_IN); |
| 1439 | #endif |
| 1440 | } |
| 1441 | |
| 1442 | priv->vs18_enable = 0; |
| 1443 | |
| 1444 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 1445 | /* |
| 1446 | * If emmc I/O has a fixed voltage at 1.8V, this must be provided, |
| 1447 | * otherwise, emmc will work abnormally. |
| 1448 | */ |
| 1449 | ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev); |
| 1450 | if (ret) { |
| 1451 | dev_dbg(dev, "no vqmmc-supply\n"); |
| 1452 | } else { |
| 1453 | ret = regulator_set_enable(vqmmc_dev, true); |
| 1454 | if (ret) { |
| 1455 | dev_err(dev, "fail to enable vqmmc-supply\n"); |
| 1456 | return ret; |
| 1457 | } |
| 1458 | |
| 1459 | if (regulator_get_value(vqmmc_dev) == 1800000) |
| 1460 | priv->vs18_enable = 1; |
| 1461 | } |
| 1462 | #endif |
| 1463 | |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1464 | /* |
| 1465 | * TODO: |
| 1466 | * Because lack of clk driver, if SDHC clk is not enabled, |
| 1467 | * need to enable it first before this driver is invoked. |
| 1468 | * |
| 1469 | * we use MXC_ESDHC_CLK to get clk freq. |
| 1470 | * If one would like to make this function work, |
| 1471 | * the aliases should be provided in dts as this: |
| 1472 | * |
| 1473 | * aliases { |
| 1474 | * mmc0 = &usdhc1; |
| 1475 | * mmc1 = &usdhc2; |
| 1476 | * mmc2 = &usdhc3; |
| 1477 | * mmc3 = &usdhc4; |
| 1478 | * }; |
| 1479 | * Then if your board only supports mmc2 and mmc3, but we can |
| 1480 | * correctly get the seq as 2 and 3, then let mxc_get_clock |
| 1481 | * work as expected. |
| 1482 | */ |
| 1483 | |
| 1484 | init_clk_usdhc(dev->seq); |
| 1485 | |
Giulio Benetti | dbdbc63 | 2020-01-10 15:51:45 +0100 | [diff] [blame] | 1486 | #if CONFIG_IS_ENABLED(CLK) |
| 1487 | /* Assigned clock already set clock */ |
| 1488 | ret = clk_get_by_name(dev, "per", &priv->per_clk); |
| 1489 | if (ret) { |
| 1490 | printf("Failed to get per_clk\n"); |
| 1491 | return ret; |
| 1492 | } |
| 1493 | ret = clk_enable(&priv->per_clk); |
| 1494 | if (ret) { |
| 1495 | printf("Failed to enable per_clk\n"); |
| 1496 | return ret; |
| 1497 | } |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1498 | |
Giulio Benetti | dbdbc63 | 2020-01-10 15:51:45 +0100 | [diff] [blame] | 1499 | priv->sdhc_clk = clk_get_rate(&priv->per_clk); |
| 1500 | #else |
| 1501 | priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq); |
| 1502 | if (priv->sdhc_clk <= 0) { |
| 1503 | dev_err(dev, "Unable to get clk for %s\n", dev->name); |
| 1504 | return -EINVAL; |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1505 | } |
Giulio Benetti | dbdbc63 | 2020-01-10 15:51:45 +0100 | [diff] [blame] | 1506 | #endif |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1507 | |
| 1508 | ret = fsl_esdhc_init(priv, plat); |
| 1509 | if (ret) { |
| 1510 | dev_err(dev, "fsl_esdhc_init failure\n"); |
| 1511 | return ret; |
| 1512 | } |
| 1513 | |
Peng Fan | 3766a48 | 2019-07-10 09:35:24 +0000 | [diff] [blame] | 1514 | ret = mmc_of_parse(dev, &plat->cfg); |
| 1515 | if (ret) |
| 1516 | return ret; |
| 1517 | |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1518 | mmc = &plat->mmc; |
| 1519 | mmc->cfg = &plat->cfg; |
| 1520 | mmc->dev = dev; |
| 1521 | #if !CONFIG_IS_ENABLED(BLK) |
| 1522 | mmc->priv = priv; |
| 1523 | |
| 1524 | /* Setup dsr related values */ |
| 1525 | mmc->dsr_imp = 0; |
| 1526 | mmc->dsr = ESDHC_DRIVER_STAGE_VALUE; |
| 1527 | /* Setup the universal parts of the block interface just once */ |
| 1528 | bdesc = mmc_get_blk_desc(mmc); |
| 1529 | bdesc->if_type = IF_TYPE_MMC; |
| 1530 | bdesc->removable = 1; |
| 1531 | bdesc->devnum = mmc_get_next_devnum(); |
| 1532 | bdesc->block_read = mmc_bread; |
| 1533 | bdesc->block_write = mmc_bwrite; |
| 1534 | bdesc->block_erase = mmc_berase; |
| 1535 | |
| 1536 | /* setup initial part type */ |
| 1537 | bdesc->part_type = mmc->cfg->part_type; |
| 1538 | mmc_list_add(mmc); |
| 1539 | #endif |
| 1540 | |
| 1541 | upriv->mmc = mmc; |
| 1542 | |
| 1543 | return esdhc_init_common(priv, mmc); |
| 1544 | } |
| 1545 | |
| 1546 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 1547 | static int fsl_esdhc_get_cd(struct udevice *dev) |
| 1548 | { |
| 1549 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1550 | |
| 1551 | return esdhc_getcd_common(priv); |
| 1552 | } |
| 1553 | |
| 1554 | static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 1555 | struct mmc_data *data) |
| 1556 | { |
| 1557 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1558 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1559 | |
| 1560 | return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data); |
| 1561 | } |
| 1562 | |
| 1563 | static int fsl_esdhc_set_ios(struct udevice *dev) |
| 1564 | { |
| 1565 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1566 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1567 | |
| 1568 | return esdhc_set_ios_common(priv, &plat->mmc); |
| 1569 | } |
| 1570 | |
Peng Fan | 69b9d3a | 2019-07-10 09:35:26 +0000 | [diff] [blame] | 1571 | #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
| 1572 | static int fsl_esdhc_set_enhanced_strobe(struct udevice *dev) |
| 1573 | { |
| 1574 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1575 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 1576 | u32 m; |
| 1577 | |
| 1578 | m = readl(®s->mixctrl); |
| 1579 | m |= MIX_CTRL_HS400_ES; |
| 1580 | writel(m, ®s->mixctrl); |
| 1581 | |
| 1582 | return 0; |
| 1583 | } |
| 1584 | #endif |
| 1585 | |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1586 | static const struct dm_mmc_ops fsl_esdhc_ops = { |
| 1587 | .get_cd = fsl_esdhc_get_cd, |
| 1588 | .send_cmd = fsl_esdhc_send_cmd, |
| 1589 | .set_ios = fsl_esdhc_set_ios, |
| 1590 | #ifdef MMC_SUPPORTS_TUNING |
| 1591 | .execute_tuning = fsl_esdhc_execute_tuning, |
| 1592 | #endif |
Peng Fan | 69b9d3a | 2019-07-10 09:35:26 +0000 | [diff] [blame] | 1593 | #if CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT) |
| 1594 | .set_enhanced_strobe = fsl_esdhc_set_enhanced_strobe, |
| 1595 | #endif |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1596 | }; |
| 1597 | #endif |
| 1598 | |
| 1599 | static struct esdhc_soc_data usdhc_imx7d_data = { |
| 1600 | .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
| 1601 | | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
| 1602 | | ESDHC_FLAG_HS400, |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1603 | }; |
| 1604 | |
Peng Fan | 457fe96 | 2019-07-10 09:35:28 +0000 | [diff] [blame] | 1605 | static struct esdhc_soc_data usdhc_imx8qm_data = { |
| 1606 | .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING | |
| 1607 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 | |
| 1608 | ESDHC_FLAG_HS400 | ESDHC_FLAG_HS400_ES, |
| 1609 | }; |
| 1610 | |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1611 | static const struct udevice_id fsl_esdhc_ids[] = { |
| 1612 | { .compatible = "fsl,imx53-esdhc", }, |
| 1613 | { .compatible = "fsl,imx6ul-usdhc", }, |
| 1614 | { .compatible = "fsl,imx6sx-usdhc", }, |
| 1615 | { .compatible = "fsl,imx6sl-usdhc", }, |
| 1616 | { .compatible = "fsl,imx6q-usdhc", }, |
| 1617 | { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,}, |
| 1618 | { .compatible = "fsl,imx7ulp-usdhc", }, |
Peng Fan | 457fe96 | 2019-07-10 09:35:28 +0000 | [diff] [blame] | 1619 | { .compatible = "fsl,imx8qm-usdhc", .data = (ulong)&usdhc_imx8qm_data,}, |
Peng Fan | d7689fa | 2019-11-04 17:31:17 +0800 | [diff] [blame] | 1620 | { .compatible = "fsl,imx8mm-usdhc", .data = (ulong)&usdhc_imx8qm_data,}, |
| 1621 | { .compatible = "fsl,imx8mn-usdhc", .data = (ulong)&usdhc_imx8qm_data,}, |
| 1622 | { .compatible = "fsl,imx8mq-usdhc", .data = (ulong)&usdhc_imx8qm_data,}, |
Giulio Benetti | 65b5ec1 | 2020-01-10 15:51:46 +0100 | [diff] [blame] | 1623 | { .compatible = "fsl,imxrt-usdhc", }, |
Yangbo Lu | 982f425 | 2019-06-21 11:42:27 +0800 | [diff] [blame] | 1624 | { .compatible = "fsl,esdhc", }, |
| 1625 | { /* sentinel */ } |
| 1626 | }; |
| 1627 | |
| 1628 | #if CONFIG_IS_ENABLED(BLK) |
| 1629 | static int fsl_esdhc_bind(struct udevice *dev) |
| 1630 | { |
| 1631 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1632 | |
| 1633 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 1634 | } |
| 1635 | #endif |
| 1636 | |
| 1637 | U_BOOT_DRIVER(fsl_esdhc) = { |
| 1638 | .name = "fsl-esdhc-mmc", |
| 1639 | .id = UCLASS_MMC, |
| 1640 | .of_match = fsl_esdhc_ids, |
| 1641 | .ops = &fsl_esdhc_ops, |
| 1642 | #if CONFIG_IS_ENABLED(BLK) |
| 1643 | .bind = fsl_esdhc_bind, |
| 1644 | #endif |
| 1645 | .probe = fsl_esdhc_probe, |
| 1646 | .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat), |
| 1647 | .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv), |
| 1648 | }; |
| 1649 | #endif |