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