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