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