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