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