Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 2 | /* |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 3 | * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc |
Yangbo Lu | 9abf648 | 2020-05-19 11:06:43 +0800 | [diff] [blame] | 4 | * Copyright 2019-2020 NXP |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 5 | * Andy Fleming |
| 6 | * |
| 7 | * Based vaguely on the pxa mmc code: |
| 8 | * (C) Copyright 2003 |
| 9 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <config.h> |
| 13 | #include <common.h> |
| 14 | #include <command.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 15 | #include <cpu_func.h> |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 16 | #include <errno.h> |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 17 | #include <hwconfig.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 18 | #include <mmc.h> |
| 19 | #include <part.h> |
| 20 | #include <malloc.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 21 | #include <fsl_esdhc.h> |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 22 | #include <fdt_support.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 23 | #include <asm/cache.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 24 | #include <asm/io.h> |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 25 | #include <dm.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 26 | #include <dm/device_compat.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 27 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 28 | #include <linux/delay.h> |
Michael Walle | b1ba146 | 2020-09-23 12:42:48 +0200 | [diff] [blame] | 29 | #include <linux/dma-mapping.h> |
Michael Walle | 361a422 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 30 | #include <sdhci.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 31 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
| 34 | struct fsl_esdhc { |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 35 | uint dsaddr; /* SDMA system address register */ |
| 36 | uint blkattr; /* Block attributes register */ |
| 37 | uint cmdarg; /* Command argument register */ |
| 38 | uint xfertyp; /* Transfer type register */ |
| 39 | uint cmdrsp0; /* Command response 0 register */ |
| 40 | uint cmdrsp1; /* Command response 1 register */ |
| 41 | uint cmdrsp2; /* Command response 2 register */ |
| 42 | uint cmdrsp3; /* Command response 3 register */ |
| 43 | uint datport; /* Buffer data port register */ |
| 44 | uint prsstat; /* Present state register */ |
| 45 | uint proctl; /* Protocol control register */ |
| 46 | uint sysctl; /* System Control Register */ |
| 47 | uint irqstat; /* Interrupt status register */ |
| 48 | uint irqstaten; /* Interrupt status enable register */ |
| 49 | uint irqsigen; /* Interrupt signal enable register */ |
| 50 | uint autoc12err; /* Auto CMD error status register */ |
| 51 | uint hostcapblt; /* Host controller capabilities register */ |
| 52 | uint wml; /* Watermark level register */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 53 | char reserved1[8]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 54 | uint fevt; /* Force event register */ |
| 55 | uint admaes; /* ADMA error status register */ |
Michael Walle | 361a422 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 56 | uint adsaddrl; /* ADMA system address low register */ |
| 57 | uint adsaddrh; /* ADMA system address high register */ |
| 58 | char reserved2[156]; |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 59 | uint hostver; /* Host controller version register */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 60 | char reserved3[4]; /* reserved */ |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 61 | uint dmaerraddr; /* DMA error address register */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 62 | char reserved4[4]; /* reserved */ |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 63 | uint dmaerrattr; /* DMA error attribute register */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 64 | char reserved5[4]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 65 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 66 | char reserved6[8]; /* reserved */ |
| 67 | uint tbctl; /* Tuning block control register */ |
Yangbo Lu | db8f936 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 68 | char reserved7[32]; /* reserved */ |
| 69 | uint sdclkctl; /* SD clock control register */ |
| 70 | uint sdtimingctl; /* SD timing control register */ |
| 71 | char reserved8[20]; /* reserved */ |
| 72 | uint dllcfg0; /* DLL config 0 register */ |
Yangbo Lu | 8ee802f | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 73 | char reserved9[12]; /* reserved */ |
| 74 | uint dllstat0; /* DLL status 0 register */ |
| 75 | char reserved10[664];/* reserved */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 76 | uint esdhcctl; /* eSDHC control register */ |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 79 | struct fsl_esdhc_plat { |
| 80 | struct mmc_config cfg; |
| 81 | struct mmc mmc; |
| 82 | }; |
| 83 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 84 | /** |
| 85 | * struct fsl_esdhc_priv |
| 86 | * |
| 87 | * @esdhc_regs: registers of the sdhc controller |
| 88 | * @sdhc_clk: Current clk of the sdhc controller |
| 89 | * @bus_width: bus width, 1bit, 4bit or 8bit |
| 90 | * @cfg: mmc config |
| 91 | * @mmc: mmc |
| 92 | * Following is used when Driver Model is enabled for MMC |
| 93 | * @dev: pointer for the device |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 94 | * @cd_gpio: gpio for card detection |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 95 | * @wp_gpio: gpio for write protection |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 96 | */ |
| 97 | struct fsl_esdhc_priv { |
| 98 | struct fsl_esdhc *esdhc_regs; |
| 99 | unsigned int sdhc_clk; |
Yangbo Lu | f1bce08 | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 100 | bool is_sdhc_per_clk; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 101 | unsigned int clock; |
Yangbo Lu | 41dec2f | 2019-10-21 18:09:07 +0800 | [diff] [blame] | 102 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 103 | struct mmc *mmc; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 104 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 105 | struct udevice *dev; |
Michael Walle | 361a422 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 106 | struct sdhci_adma_desc *adma_desc_table; |
Michael Walle | b1ba146 | 2020-09-23 12:42:48 +0200 | [diff] [blame] | 107 | dma_addr_t dma_addr; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 108 | }; |
| 109 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 110 | /* Return the XFERTYP flags for a given command and data packet */ |
Kim Phillips | eafa90a | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 111 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 112 | { |
| 113 | uint xfertyp = 0; |
| 114 | |
| 115 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 116 | xfertyp |= XFERTYP_DPSEL; |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 117 | if (!IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO) && |
| 118 | cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK && |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 119 | cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200) |
| 120 | xfertyp |= XFERTYP_DMAEN; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 121 | if (data->blocks > 1) { |
| 122 | xfertyp |= XFERTYP_MSBSEL; |
| 123 | xfertyp |= XFERTYP_BCEN; |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 124 | if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111)) |
| 125 | xfertyp |= XFERTYP_AC12EN; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | if (data->flags & MMC_DATA_READ) |
| 129 | xfertyp |= XFERTYP_DTDSEL; |
| 130 | } |
| 131 | |
| 132 | if (cmd->resp_type & MMC_RSP_CRC) |
| 133 | xfertyp |= XFERTYP_CCCEN; |
| 134 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 135 | xfertyp |= XFERTYP_CICEN; |
| 136 | if (cmd->resp_type & MMC_RSP_136) |
| 137 | xfertyp |= XFERTYP_RSPTYP_136; |
| 138 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 139 | xfertyp |= XFERTYP_RSPTYP_48_BUSY; |
| 140 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 141 | xfertyp |= XFERTYP_RSPTYP_48; |
| 142 | |
Jason Liu | 4571de3 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 143 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 144 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
Yangbo Lu | 2550344 | 2016-01-21 17:33:19 +0800 | [diff] [blame] | 145 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 146 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 147 | } |
| 148 | |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 149 | /* |
| 150 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 151 | */ |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 152 | static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv, |
| 153 | struct mmc_data *data) |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 154 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 155 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 156 | uint blocks; |
| 157 | char *buffer; |
| 158 | uint databuf; |
| 159 | uint size; |
| 160 | uint irqstat; |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 161 | ulong start; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 162 | |
| 163 | if (data->flags & MMC_DATA_READ) { |
| 164 | blocks = data->blocks; |
| 165 | buffer = data->dest; |
| 166 | while (blocks) { |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 167 | start = get_timer(0); |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 168 | size = data->blocksize; |
| 169 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 170 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) { |
| 171 | if (get_timer(start) > PIO_TIMEOUT) { |
| 172 | printf("\nData Read Failed in PIO Mode."); |
| 173 | return; |
| 174 | } |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 175 | } |
| 176 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 177 | udelay(100); /* Wait before last byte transfer complete */ |
| 178 | irqstat = esdhc_read32(®s->irqstat); |
| 179 | databuf = in_le32(®s->datport); |
| 180 | *((uint *)buffer) = databuf; |
| 181 | buffer += 4; |
| 182 | size -= 4; |
| 183 | } |
| 184 | blocks--; |
| 185 | } |
| 186 | } else { |
| 187 | blocks = data->blocks; |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 188 | buffer = (char *)data->src; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 189 | while (blocks) { |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 190 | start = get_timer(0); |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 191 | size = data->blocksize; |
| 192 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 193 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) { |
| 194 | if (get_timer(start) > PIO_TIMEOUT) { |
| 195 | printf("\nData Write Failed in PIO Mode."); |
| 196 | return; |
| 197 | } |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 198 | } |
| 199 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 200 | udelay(100); /* Wait before last byte transfer complete */ |
| 201 | databuf = *((uint *)buffer); |
| 202 | buffer += 4; |
| 203 | size -= 4; |
| 204 | irqstat = esdhc_read32(®s->irqstat); |
| 205 | out_le32(®s->datport, databuf); |
| 206 | } |
| 207 | blocks--; |
| 208 | } |
| 209 | } |
| 210 | } |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 211 | |
Michael Walle | 7e48a02 | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 212 | static void esdhc_setup_watermark_level(struct fsl_esdhc_priv *priv, |
| 213 | struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 214 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 215 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Michael Walle | 7e48a02 | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 216 | uint wml_value = data->blocksize / 4; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 217 | |
| 218 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 219 | if (wml_value > WML_RD_WML_MAX) |
| 220 | wml_value = WML_RD_WML_MAX_VAL; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 221 | |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 222 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 223 | } else { |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 224 | if (wml_value > WML_WR_WML_MAX) |
| 225 | wml_value = WML_WR_WML_MAX_VAL; |
Yangbo Lu | 0cc127c | 2019-10-31 18:54:25 +0800 | [diff] [blame] | 226 | |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 227 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
Michael Walle | 7e48a02 | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 228 | wml_value << 16); |
| 229 | } |
| 230 | } |
Michael Walle | 7e48a02 | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 231 | |
| 232 | static void esdhc_setup_dma(struct fsl_esdhc_priv *priv, struct mmc_data *data) |
| 233 | { |
| 234 | uint trans_bytes = data->blocksize * data->blocks; |
| 235 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Michael Walle | 361a422 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 236 | phys_addr_t adma_addr; |
Michael Walle | 7e48a02 | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 237 | void *buf; |
| 238 | |
| 239 | if (data->flags & MMC_DATA_WRITE) |
| 240 | buf = (void *)data->src; |
| 241 | else |
| 242 | buf = data->dest; |
| 243 | |
| 244 | priv->dma_addr = dma_map_single(buf, trans_bytes, |
| 245 | mmc_get_dma_dir(data)); |
Michael Walle | 361a422 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 246 | |
| 247 | if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2) && |
| 248 | priv->adma_desc_table) { |
| 249 | debug("Using ADMA2\n"); |
| 250 | /* prefer ADMA2 if it is available */ |
| 251 | sdhci_prepare_adma_table(priv->adma_desc_table, data, |
| 252 | priv->dma_addr); |
| 253 | |
| 254 | adma_addr = virt_to_phys(priv->adma_desc_table); |
| 255 | esdhc_write32(®s->adsaddrl, lower_32_bits(adma_addr)); |
| 256 | if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT)) |
| 257 | esdhc_write32(®s->adsaddrh, upper_32_bits(adma_addr)); |
| 258 | esdhc_clrsetbits32(®s->proctl, PROCTL_DMAS_MASK, |
| 259 | PROCTL_DMAS_ADMA2); |
| 260 | } else { |
| 261 | debug("Using SDMA\n"); |
| 262 | if (upper_32_bits(priv->dma_addr)) |
| 263 | printf("Cannot use 64 bit addresses with SDMA\n"); |
| 264 | esdhc_write32(®s->dsaddr, lower_32_bits(priv->dma_addr)); |
| 265 | esdhc_clrsetbits32(®s->proctl, PROCTL_DMAS_MASK, |
| 266 | PROCTL_DMAS_SDMA); |
| 267 | } |
| 268 | |
Michael Walle | 7e48a02 | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 269 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
| 270 | } |
| 271 | |
| 272 | static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 273 | struct mmc_data *data) |
| 274 | { |
| 275 | int timeout; |
| 276 | bool is_write = data->flags & MMC_DATA_WRITE; |
| 277 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 278 | |
| 279 | if (is_write && !(esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL)) { |
| 280 | printf("Can not write to locked SD card.\n"); |
| 281 | return -EINVAL; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 284 | if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) |
| 285 | esdhc_setup_watermark_level(priv, data); |
| 286 | else |
| 287 | esdhc_setup_dma(priv, data); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 288 | |
| 289 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 290 | /* |
| 291 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 292 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 293 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 294 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 295 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 296 | * As 1) >= 2) |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 297 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 298 | * Taking log2 both the sides |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 299 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 300 | * Rounding up to next power of 2 |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 301 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 302 | * => timeout + 13 = fls(mmc->clock/4) |
Yangbo Lu | e978a31 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 303 | * |
| 304 | * However, the MMC spec "It is strongly recommended for hosts to |
| 305 | * implement more than 500ms timeout value even if the card |
| 306 | * indicates the 250ms maximum busy length." Even the previous |
| 307 | * value of 300ms is known to be insufficient for some cards. |
| 308 | * So, we use |
| 309 | * => timeout + 13 = fls(mmc->clock/2) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 310 | */ |
Yangbo Lu | e978a31 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 311 | timeout = fls(mmc->clock/2); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 312 | timeout -= 13; |
| 313 | |
| 314 | if (timeout > 14) |
| 315 | timeout = 14; |
| 316 | |
| 317 | if (timeout < 0) |
| 318 | timeout = 0; |
| 319 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 320 | if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC_A001) && |
| 321 | (timeout == 4 || timeout == 8 || timeout == 12)) |
Kumar Gala | 5103a03 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 322 | timeout++; |
Kumar Gala | 5103a03 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 323 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 324 | if (IS_ENABLED(ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE)) |
| 325 | timeout = 0xE; |
| 326 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 327 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 332 | /* |
| 333 | * Sends a command out on the bus. Takes the mmc pointer, |
| 334 | * a command pointer, and an optional data pointer. |
| 335 | */ |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 336 | static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 337 | struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 338 | { |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 339 | int err = 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 340 | uint xfertyp; |
| 341 | uint irqstat; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 342 | u32 flags = IRQSTAT_CC | IRQSTAT_CTOE; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 343 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Fabio Estevam | 29c2edb | 2018-11-19 10:31:53 -0200 | [diff] [blame] | 344 | unsigned long start; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 345 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 346 | if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111) && |
| 347 | cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 348 | return 0; |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 349 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 350 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 351 | |
| 352 | sync(); |
| 353 | |
| 354 | /* Wait for the bus to be idle */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 355 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 356 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 357 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 358 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 359 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 360 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 361 | |
| 362 | /* Wait at least 8 SD clock cycles before the next command */ |
| 363 | /* |
| 364 | * Note: This is way more than 8 cycles, but 1ms seems to |
| 365 | * resolve timing issues with some cards |
| 366 | */ |
| 367 | udelay(1000); |
| 368 | |
| 369 | /* Set up for a data transfer if we have one */ |
| 370 | if (data) { |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 371 | err = esdhc_setup_data(priv, mmc, data); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 372 | if(err) |
| 373 | return err; |
| 374 | } |
| 375 | |
| 376 | /* Figure out the transfer arguments */ |
| 377 | xfertyp = esdhc_xfertyp(cmd, data); |
| 378 | |
Andrew Gabbasov | 01b7735 | 2013-06-11 10:34:22 -0500 | [diff] [blame] | 379 | /* Mask all irqs */ |
| 380 | esdhc_write32(®s->irqsigen, 0); |
| 381 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 382 | /* Send the command */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 383 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
| 384 | esdhc_write32(®s->xfertyp, xfertyp); |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 385 | |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 386 | if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK || |
| 387 | cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) |
| 388 | flags = IRQSTAT_BRR; |
| 389 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 390 | /* Wait for the command to complete */ |
Fabio Estevam | 29c2edb | 2018-11-19 10:31:53 -0200 | [diff] [blame] | 391 | start = get_timer(0); |
| 392 | while (!(esdhc_read32(®s->irqstat) & flags)) { |
| 393 | if (get_timer(start) > 1000) { |
| 394 | err = -ETIMEDOUT; |
| 395 | goto out; |
| 396 | } |
| 397 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 398 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 399 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 400 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 401 | if (irqstat & CMD_ERR) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 402 | err = -ECOMM; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 403 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 406 | if (irqstat & IRQSTAT_CTOE) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 407 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 408 | goto out; |
| 409 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 410 | |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 411 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 412 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
Yangbo Lu | 253d5bd | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 413 | int timeout = 6000; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 414 | |
Yangbo Lu | 253d5bd | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 415 | /* Poll on DATA0 line for cmd with busy signal for 600 ms */ |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 416 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 417 | PRSSTAT_DAT0)) { |
| 418 | udelay(100); |
| 419 | timeout--; |
| 420 | } |
| 421 | |
| 422 | if (timeout <= 0) { |
| 423 | printf("Timeout waiting for DAT0 to go high!\n"); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 424 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 425 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 429 | /* Copy the response to the response buffer */ |
| 430 | if (cmd->resp_type & MMC_RSP_136) { |
| 431 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 432 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 433 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 434 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 435 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 436 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 437 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 438 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 439 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 440 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 441 | } else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 442 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 443 | |
| 444 | /* Wait until all of the blocks are transferred */ |
| 445 | if (data) { |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 446 | if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) { |
| 447 | esdhc_pio_read_write(priv, data); |
| 448 | } else { |
| 449 | flags = DATA_COMPLETE; |
| 450 | if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK || |
| 451 | cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) |
| 452 | flags = IRQSTAT_BRR; |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 453 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 454 | do { |
| 455 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 456 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 457 | if (irqstat & IRQSTAT_DTOE) { |
| 458 | err = -ETIMEDOUT; |
| 459 | goto out; |
| 460 | } |
Frans Meulenbroeks | 63fb5a7 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 461 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 462 | if (irqstat & DATA_ERR) { |
| 463 | err = -ECOMM; |
| 464 | goto out; |
| 465 | } |
| 466 | } while ((irqstat & flags) != flags); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 467 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 468 | /* |
| 469 | * Need invalidate the dcache here again to avoid any |
| 470 | * cache-fill during the DMA operations such as the |
| 471 | * speculative pre-fetching etc. |
| 472 | */ |
| 473 | dma_unmap_single(priv->dma_addr, |
| 474 | data->blocks * data->blocksize, |
| 475 | mmc_get_dma_dir(data)); |
| 476 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 477 | } |
| 478 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 479 | out: |
| 480 | /* Reset CMD and DATA portions on error */ |
| 481 | if (err) { |
| 482 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 483 | SYSCTL_RSTC); |
| 484 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 485 | ; |
| 486 | |
| 487 | if (data) { |
| 488 | esdhc_write32(®s->sysctl, |
| 489 | esdhc_read32(®s->sysctl) | |
| 490 | SYSCTL_RSTD); |
| 491 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 492 | ; |
| 493 | } |
| 494 | } |
| 495 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 496 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 497 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 498 | return err; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 499 | } |
| 500 | |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 501 | static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 502 | { |
Benoît Thébaudeau | b9b4f14 | 2018-01-16 22:44:18 +0100 | [diff] [blame] | 503 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 504 | int div = 1; |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 505 | int pre_div = 2; |
Yinbo Zhu | 6f883e5 | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 506 | unsigned int sdhc_clk = priv->sdhc_clk; |
| 507 | u32 time_out; |
| 508 | u32 value; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 509 | uint clk; |
| 510 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 511 | if (clock < mmc->cfg->f_min) |
| 512 | clock = mmc->cfg->f_min; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 513 | |
Yangbo Lu | 5d336d1 | 2019-10-21 18:09:09 +0800 | [diff] [blame] | 514 | while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256) |
Lukasz Majewski | b6a0427 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 515 | pre_div *= 2; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 516 | |
Yangbo Lu | 5d336d1 | 2019-10-21 18:09:09 +0800 | [diff] [blame] | 517 | while (sdhc_clk / (div * pre_div) > clock && div < 16) |
Lukasz Majewski | b6a0427 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 518 | div++; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 519 | |
Yangbo Lu | 30f6444 | 2020-09-01 16:58:06 +0800 | [diff] [blame] | 520 | mmc->clock = sdhc_clk / pre_div / div; |
| 521 | priv->clock = mmc->clock; |
| 522 | |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 523 | pre_div >>= 1; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 524 | div -= 1; |
| 525 | |
| 526 | clk = (pre_div << 8) | (div << 4); |
| 527 | |
Kumar Gala | cc4d122 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 528 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 529 | |
| 530 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 531 | |
Yinbo Zhu | 6f883e5 | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 532 | time_out = 20; |
| 533 | value = PRSSTAT_SDSTB; |
| 534 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 535 | if (time_out == 0) { |
| 536 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 537 | break; |
| 538 | } |
| 539 | time_out--; |
| 540 | mdelay(1); |
| 541 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 542 | |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 543 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 544 | } |
| 545 | |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 546 | static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable) |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 547 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 548 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 549 | u32 value; |
| 550 | u32 time_out; |
| 551 | |
| 552 | value = esdhc_read32(®s->sysctl); |
| 553 | |
| 554 | if (enable) |
| 555 | value |= SYSCTL_CKEN; |
| 556 | else |
| 557 | value &= ~SYSCTL_CKEN; |
| 558 | |
| 559 | esdhc_write32(®s->sysctl, value); |
| 560 | |
| 561 | time_out = 20; |
| 562 | value = PRSSTAT_SDSTB; |
| 563 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 564 | if (time_out == 0) { |
| 565 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 566 | break; |
| 567 | } |
| 568 | time_out--; |
| 569 | mdelay(1); |
| 570 | } |
| 571 | } |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 572 | |
Yangbo Lu | db8f936 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 573 | static void esdhc_flush_async_fifo(struct fsl_esdhc_priv *priv) |
| 574 | { |
| 575 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 576 | u32 time_out; |
| 577 | |
| 578 | esdhc_setbits32(®s->esdhcctl, ESDHCCTL_FAF); |
| 579 | |
| 580 | time_out = 20; |
| 581 | while (esdhc_read32(®s->esdhcctl) & ESDHCCTL_FAF) { |
| 582 | if (time_out == 0) { |
| 583 | printf("fsl_esdhc: Flush asynchronous FIFO timeout.\n"); |
| 584 | break; |
| 585 | } |
| 586 | time_out--; |
| 587 | mdelay(1); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | static void esdhc_tuning_block_enable(struct fsl_esdhc_priv *priv, |
| 592 | bool en) |
| 593 | { |
| 594 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 595 | |
| 596 | esdhc_clock_control(priv, false); |
| 597 | esdhc_flush_async_fifo(priv); |
| 598 | if (en) |
| 599 | esdhc_setbits32(®s->tbctl, TBCTL_TB_EN); |
| 600 | else |
| 601 | esdhc_clrbits32(®s->tbctl, TBCTL_TB_EN); |
| 602 | esdhc_clock_control(priv, true); |
| 603 | } |
| 604 | |
| 605 | static void esdhc_exit_hs400(struct fsl_esdhc_priv *priv) |
| 606 | { |
| 607 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 608 | |
| 609 | esdhc_clrbits32(®s->sdtimingctl, FLW_CTL_BG); |
| 610 | esdhc_clrbits32(®s->sdclkctl, CMD_CLK_CTL); |
| 611 | |
| 612 | esdhc_clock_control(priv, false); |
| 613 | esdhc_clrbits32(®s->tbctl, HS400_MODE); |
| 614 | esdhc_clock_control(priv, true); |
| 615 | |
| 616 | esdhc_clrbits32(®s->dllcfg0, DLL_FREQ_SEL | DLL_ENABLE); |
| 617 | esdhc_clrbits32(®s->tbctl, HS400_WNDW_ADJUST); |
| 618 | |
| 619 | esdhc_tuning_block_enable(priv, false); |
| 620 | } |
| 621 | |
Yangbo Lu | 8ee802f | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 622 | static int esdhc_set_timing(struct fsl_esdhc_priv *priv, enum bus_mode mode) |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 623 | { |
| 624 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 8ee802f | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 625 | ulong start; |
| 626 | u32 val; |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 627 | |
Yangbo Lu | db8f936 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 628 | /* Exit HS400 mode before setting any other mode */ |
| 629 | if (esdhc_read32(®s->tbctl) & HS400_MODE && |
| 630 | mode != MMC_HS_400) |
| 631 | esdhc_exit_hs400(priv); |
| 632 | |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 633 | esdhc_clock_control(priv, false); |
| 634 | |
| 635 | if (mode == MMC_HS_200) |
| 636 | esdhc_clrsetbits32(®s->autoc12err, UHSM_MASK, |
| 637 | UHSM_SDR104_HS200); |
Yangbo Lu | db8f936 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 638 | if (mode == MMC_HS_400) { |
| 639 | esdhc_setbits32(®s->tbctl, HS400_MODE); |
| 640 | esdhc_setbits32(®s->sdclkctl, CMD_CLK_CTL); |
| 641 | esdhc_clock_control(priv, true); |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 642 | |
Yangbo Lu | 78804de | 2020-09-01 16:58:07 +0800 | [diff] [blame] | 643 | if (priv->clock == 200000000) |
| 644 | esdhc_setbits32(®s->dllcfg0, DLL_FREQ_SEL); |
| 645 | |
| 646 | esdhc_setbits32(®s->dllcfg0, DLL_ENABLE); |
Yangbo Lu | 8ee802f | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 647 | |
| 648 | esdhc_setbits32(®s->dllcfg0, DLL_RESET); |
| 649 | udelay(1); |
| 650 | esdhc_clrbits32(®s->dllcfg0, DLL_RESET); |
| 651 | |
| 652 | start = get_timer(0); |
| 653 | val = DLL_STS_SLV_LOCK; |
| 654 | while (!(esdhc_read32(®s->dllstat0) & val)) { |
| 655 | if (get_timer(start) > 1000) { |
| 656 | printf("fsl_esdhc: delay chain lock timeout\n"); |
| 657 | return -ETIMEDOUT; |
| 658 | } |
| 659 | } |
| 660 | |
Yangbo Lu | db8f936 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 661 | esdhc_setbits32(®s->tbctl, HS400_WNDW_ADJUST); |
| 662 | |
| 663 | esdhc_clock_control(priv, false); |
| 664 | esdhc_flush_async_fifo(priv); |
| 665 | } |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 666 | esdhc_clock_control(priv, true); |
Yangbo Lu | 8ee802f | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 667 | return 0; |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 668 | } |
| 669 | |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 670 | static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 671 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 672 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 8ee802f | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 673 | int ret; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 674 | |
Yangbo Lu | f1bce08 | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 675 | if (priv->is_sdhc_per_clk) { |
| 676 | /* Select to use peripheral clock */ |
| 677 | esdhc_clock_control(priv, false); |
| 678 | esdhc_setbits32(®s->esdhcctl, ESDHCCTL_PCS); |
| 679 | esdhc_clock_control(priv, true); |
| 680 | } |
| 681 | |
Yangbo Lu | db8f936 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 682 | if (mmc->selected_mode == MMC_HS_400) |
| 683 | esdhc_tuning_block_enable(priv, true); |
| 684 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 685 | /* Set the clock speed */ |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 686 | if (priv->clock != mmc->clock) |
| 687 | set_sysctl(priv, mmc, mmc->clock); |
| 688 | |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 689 | /* Set timing */ |
Yangbo Lu | 8ee802f | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 690 | ret = esdhc_set_timing(priv, mmc->selected_mode); |
| 691 | if (ret) |
| 692 | return ret; |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 693 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 694 | /* Set the bus width */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 695 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 696 | |
| 697 | if (mmc->bus_width == 4) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 698 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 699 | else if (mmc->bus_width == 8) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 700 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 701 | |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 702 | return 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 703 | } |
| 704 | |
Rasmus Villemoes | ede2822 | 2020-01-30 12:06:45 +0000 | [diff] [blame] | 705 | static void esdhc_enable_cache_snooping(struct fsl_esdhc *regs) |
| 706 | { |
| 707 | #ifdef CONFIG_ARCH_MPC830X |
| 708 | immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
| 709 | sysconf83xx_t *sysconf = &immr->sysconf; |
| 710 | |
| 711 | setbits_be32(&sysconf->sdhccr, 0x02000000); |
| 712 | #else |
| 713 | esdhc_write32(®s->esdhcctl, 0x00000040); |
| 714 | #endif |
| 715 | } |
| 716 | |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 717 | static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 718 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 719 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Simon Glass | 201e828 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 720 | ulong start; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 721 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 722 | /* Reset the entire host controller */ |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 723 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 724 | |
| 725 | /* Wait until the controller is available */ |
Simon Glass | 201e828 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 726 | start = get_timer(0); |
| 727 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 728 | if (get_timer(start) > 1000) |
| 729 | return -ETIMEDOUT; |
| 730 | } |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 731 | |
Yangbo Lu | 1b5f0ba | 2020-09-01 16:58:02 +0800 | [diff] [blame] | 732 | /* Clean TBCTL[TB_EN] which is not able to be reset by reset all */ |
| 733 | esdhc_clrbits32(®s->tbctl, TBCTL_TB_EN); |
| 734 | |
Rasmus Villemoes | ede2822 | 2020-01-30 12:06:45 +0000 | [diff] [blame] | 735 | esdhc_enable_cache_snooping(regs); |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 736 | |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 737 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 738 | |
| 739 | /* Set the initial clock speed */ |
Yangbo Lu | 263ddfc | 2020-10-20 11:04:51 +0800 | [diff] [blame] | 740 | set_sysctl(priv, mmc, 400000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 741 | |
| 742 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 743 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 744 | |
| 745 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 746 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 747 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 748 | /* Set timout to the maximum value */ |
| 749 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 750 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 751 | return 0; |
| 752 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 753 | |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 754 | static int esdhc_getcd_common(struct fsl_esdhc_priv *priv) |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 755 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 756 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 757 | |
Haijun.Zhang | f7e27cc | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 758 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 759 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 760 | return 1; |
| 761 | #endif |
Yangbo Lu | 9abf648 | 2020-05-19 11:06:43 +0800 | [diff] [blame] | 762 | if (esdhc_read32(®s->prsstat) & PRSSTAT_CINS) |
| 763 | return 1; |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 764 | |
Yangbo Lu | 9abf648 | 2020-05-19 11:06:43 +0800 | [diff] [blame] | 765 | return 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 766 | } |
| 767 | |
Yangbo Lu | 5705973 | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 768 | static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv, |
| 769 | struct mmc_config *cfg) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 770 | { |
Yangbo Lu | 5705973 | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 771 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 5b05fc0 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 772 | u32 caps; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 773 | |
Wang Huan | 19060bd | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 774 | caps = esdhc_read32(®s->hostcapblt); |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 775 | if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC135)) |
| 776 | caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30); |
| 777 | if (IS_ENABLED(CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33)) |
| 778 | caps |= HOSTCAPBLT_VS33; |
Yangbo Lu | 5b05fc0 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 779 | if (caps & HOSTCAPBLT_VS18) |
| 780 | cfg->voltages |= MMC_VDD_165_195; |
| 781 | if (caps & HOSTCAPBLT_VS30) |
| 782 | cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31; |
| 783 | if (caps & HOSTCAPBLT_VS33) |
| 784 | cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 785 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 786 | cfg->name = "FSL_SDHC"; |
Abbas Raza | aad4659 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 787 | |
Yangbo Lu | 5b05fc0 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 788 | if (caps & HOSTCAPBLT_HSS) |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 789 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 790 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 791 | cfg->f_min = 400000; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 792 | cfg->f_max = min(priv->sdhc_clk, (u32)200000000); |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 793 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 794 | } |
| 795 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 796 | #ifdef CONFIG_OF_LIBFDT |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 797 | __weak int esdhc_status_fixup(void *blob, const char *compat) |
| 798 | { |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 799 | if (IS_ENABLED(CONFIG_FSL_ESDHC_PIN_MUX) && !hwconfig("esdhc")) { |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 800 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 801 | sizeof("disabled"), 1); |
| 802 | return 1; |
| 803 | } |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 804 | |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | |
Yangbo Lu | c927d65 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 808 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 809 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 810 | static int fsl_esdhc_get_cd(struct udevice *dev); |
Yangbo Lu | c927d65 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 811 | static void esdhc_disable_for_no_card(void *blob) |
| 812 | { |
| 813 | struct udevice *dev; |
| 814 | |
| 815 | for (uclass_first_device(UCLASS_MMC, &dev); |
| 816 | dev; |
| 817 | uclass_next_device(&dev)) { |
| 818 | char esdhc_path[50]; |
| 819 | |
| 820 | if (fsl_esdhc_get_cd(dev)) |
| 821 | continue; |
| 822 | |
| 823 | snprintf(esdhc_path, sizeof(esdhc_path), "/soc/esdhc@%lx", |
| 824 | (unsigned long)dev_read_addr(dev)); |
| 825 | do_fixup_by_path(blob, esdhc_path, "status", "disabled", |
| 826 | sizeof("disabled"), 1); |
| 827 | } |
| 828 | } |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 829 | #else |
| 830 | static void esdhc_disable_for_no_card(void *blob) |
| 831 | { |
| 832 | } |
Yangbo Lu | c927d65 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 833 | #endif |
| 834 | |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 835 | void fdt_fixup_esdhc(void *blob, struct bd_info *bd) |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 836 | { |
| 837 | const char *compat = "fsl,esdhc"; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 838 | |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 839 | if (esdhc_status_fixup(blob, compat)) |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 840 | return; |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 841 | |
| 842 | if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND)) |
| 843 | esdhc_disable_for_no_card(blob); |
| 844 | |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 845 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | e9adeca | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 846 | gd->arch.sdhc_clk, 1); |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 847 | } |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 848 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 849 | |
Yangbo Lu | 6187047 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 850 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 851 | static int esdhc_getcd(struct mmc *mmc) |
| 852 | { |
| 853 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 854 | |
| 855 | return esdhc_getcd_common(priv); |
| 856 | } |
| 857 | |
| 858 | static int esdhc_init(struct mmc *mmc) |
| 859 | { |
| 860 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 861 | |
| 862 | return esdhc_init_common(priv, mmc); |
| 863 | } |
| 864 | |
| 865 | static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 866 | struct mmc_data *data) |
| 867 | { |
| 868 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 869 | |
| 870 | return esdhc_send_cmd_common(priv, mmc, cmd, data); |
| 871 | } |
| 872 | |
| 873 | static int esdhc_set_ios(struct mmc *mmc) |
| 874 | { |
| 875 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 876 | |
| 877 | return esdhc_set_ios_common(priv, mmc); |
| 878 | } |
| 879 | |
| 880 | static const struct mmc_ops esdhc_ops = { |
| 881 | .getcd = esdhc_getcd, |
| 882 | .init = esdhc_init, |
| 883 | .send_cmd = esdhc_send_cmd, |
| 884 | .set_ios = esdhc_set_ios, |
| 885 | }; |
| 886 | |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 887 | int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg) |
Yangbo Lu | 6187047 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 888 | { |
| 889 | struct fsl_esdhc_plat *plat; |
| 890 | struct fsl_esdhc_priv *priv; |
| 891 | struct mmc_config *mmc_cfg; |
| 892 | struct mmc *mmc; |
| 893 | |
| 894 | if (!cfg) |
| 895 | return -EINVAL; |
| 896 | |
| 897 | priv = calloc(sizeof(struct fsl_esdhc_priv), 1); |
| 898 | if (!priv) |
| 899 | return -ENOMEM; |
| 900 | plat = calloc(sizeof(struct fsl_esdhc_plat), 1); |
| 901 | if (!plat) { |
| 902 | free(priv); |
| 903 | return -ENOMEM; |
| 904 | } |
| 905 | |
| 906 | priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); |
| 907 | priv->sdhc_clk = cfg->sdhc_clk; |
Yangbo Lu | f1bce08 | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 908 | if (gd->arch.sdhc_per_clk) |
| 909 | priv->is_sdhc_per_clk = true; |
Yangbo Lu | 6187047 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 910 | |
| 911 | mmc_cfg = &plat->cfg; |
| 912 | |
| 913 | if (cfg->max_bus_width == 8) { |
| 914 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT | |
| 915 | MMC_MODE_8BIT; |
| 916 | } else if (cfg->max_bus_width == 4) { |
| 917 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT; |
| 918 | } else if (cfg->max_bus_width == 1) { |
| 919 | mmc_cfg->host_caps |= MMC_MODE_1BIT; |
| 920 | } else { |
| 921 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT | |
| 922 | MMC_MODE_8BIT; |
| 923 | printf("No max bus width provided. Assume 8-bit supported.\n"); |
| 924 | } |
| 925 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 926 | if (IS_ENABLED(CONFIG_ESDHC_DETECT_8_BIT_QUIRK)) |
Yangbo Lu | 6187047 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 927 | mmc_cfg->host_caps &= ~MMC_MODE_8BIT; |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 928 | |
Yangbo Lu | 6187047 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 929 | mmc_cfg->ops = &esdhc_ops; |
| 930 | |
| 931 | fsl_esdhc_get_cfg_common(priv, mmc_cfg); |
| 932 | |
| 933 | mmc = mmc_create(mmc_cfg, priv); |
| 934 | if (!mmc) |
| 935 | return -EIO; |
| 936 | |
| 937 | priv->mmc = mmc; |
| 938 | return 0; |
| 939 | } |
| 940 | |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 941 | int fsl_esdhc_mmc_init(struct bd_info *bis) |
Yangbo Lu | 6187047 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 942 | { |
| 943 | struct fsl_esdhc_cfg *cfg; |
| 944 | |
| 945 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
| 946 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
Yangbo Lu | f1bce08 | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 947 | /* Prefer peripheral clock which provides higher frequency. */ |
| 948 | if (gd->arch.sdhc_per_clk) |
| 949 | cfg->sdhc_clk = gd->arch.sdhc_per_clk; |
| 950 | else |
| 951 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Yangbo Lu | 6187047 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 952 | return fsl_esdhc_initialize(bis, cfg); |
| 953 | } |
| 954 | #else /* DM_MMC */ |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 955 | static int fsl_esdhc_probe(struct udevice *dev) |
| 956 | { |
| 957 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 958 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 959 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
Michael Walle | 361a422 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 960 | u32 caps, hostver; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 961 | fdt_addr_t addr; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 962 | struct mmc *mmc; |
Yangbo Lu | c927d65 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 963 | int ret; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 964 | |
Simon Glass | 4aac33f | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 965 | addr = dev_read_addr(dev); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 966 | if (addr == FDT_ADDR_T_NONE) |
| 967 | return -EINVAL; |
Yinbo Zhu | b69e1d0 | 2019-04-11 11:01:50 +0000 | [diff] [blame] | 968 | #ifdef CONFIG_PPC |
| 969 | priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr); |
| 970 | #else |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 971 | priv->esdhc_regs = (struct fsl_esdhc *)addr; |
Yinbo Zhu | b69e1d0 | 2019-04-11 11:01:50 +0000 | [diff] [blame] | 972 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 973 | priv->dev = dev; |
| 974 | |
Michael Walle | 361a422 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 975 | if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2)) { |
| 976 | /* |
| 977 | * Only newer eSDHC controllers can do ADMA2 if the ADMA flag |
| 978 | * is set in the host capabilities register. |
| 979 | */ |
| 980 | caps = esdhc_read32(&priv->esdhc_regs->hostcapblt); |
| 981 | hostver = esdhc_read32(&priv->esdhc_regs->hostver); |
| 982 | if (caps & HOSTCAPBLT_DMAS && |
| 983 | HOSTVER_VENDOR(hostver) > VENDOR_V_22) { |
| 984 | priv->adma_desc_table = sdhci_adma_init(); |
| 985 | if (!priv->adma_desc_table) |
| 986 | debug("Could not allocate ADMA tables, falling back to SDMA\n"); |
| 987 | } |
| 988 | } |
| 989 | |
Yangbo Lu | f1bce08 | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 990 | if (gd->arch.sdhc_per_clk) { |
| 991 | priv->sdhc_clk = gd->arch.sdhc_per_clk; |
| 992 | priv->is_sdhc_per_clk = true; |
| 993 | } else { |
| 994 | priv->sdhc_clk = gd->arch.sdhc_clk; |
| 995 | } |
| 996 | |
Yangbo Lu | 5e81cbf | 2019-11-12 19:28:36 +0800 | [diff] [blame] | 997 | if (priv->sdhc_clk <= 0) { |
| 998 | dev_err(dev, "Unable to get clk for %s\n", dev->name); |
| 999 | return -EINVAL; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Yangbo Lu | 5705973 | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 1002 | fsl_esdhc_get_cfg_common(priv, &plat->cfg); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1003 | |
Yinbo Zhu | 6f883e5 | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 1004 | mmc_of_parse(dev, &plat->cfg); |
| 1005 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1006 | mmc = &plat->mmc; |
| 1007 | mmc->cfg = &plat->cfg; |
| 1008 | mmc->dev = dev; |
Yangbo Lu | 66fa035 | 2019-05-23 11:05:46 +0800 | [diff] [blame] | 1009 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1010 | upriv->mmc = mmc; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1011 | |
Yangbo Lu | c927d65 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 1012 | ret = esdhc_init_common(priv, mmc); |
| 1013 | if (ret) |
| 1014 | return ret; |
| 1015 | |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 1016 | if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND) && |
| 1017 | !fsl_esdhc_get_cd(dev)) |
Yangbo Lu | c927d65 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 1018 | esdhc_setbits32(&priv->esdhc_regs->proctl, PROCTL_VOLT_SEL); |
Michael Walle | 52faec3 | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 1019 | |
Yangbo Lu | c927d65 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 1020 | return 0; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1021 | } |
| 1022 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1023 | static int fsl_esdhc_get_cd(struct udevice *dev) |
| 1024 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1025 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1026 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1027 | |
Yangbo Lu | 08197cb | 2019-10-31 18:54:24 +0800 | [diff] [blame] | 1028 | if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE) |
| 1029 | return 1; |
| 1030 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1031 | return esdhc_getcd_common(priv); |
| 1032 | } |
| 1033 | |
| 1034 | static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 1035 | struct mmc_data *data) |
| 1036 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1037 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1038 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1039 | |
| 1040 | return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data); |
| 1041 | } |
| 1042 | |
| 1043 | static int fsl_esdhc_set_ios(struct udevice *dev) |
| 1044 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1045 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1046 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1047 | |
| 1048 | return esdhc_set_ios_common(priv, &plat->mmc); |
| 1049 | } |
| 1050 | |
Yangbo Lu | 1fdefd1 | 2020-09-01 16:58:00 +0800 | [diff] [blame] | 1051 | static int fsl_esdhc_reinit(struct udevice *dev) |
| 1052 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1053 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Yangbo Lu | 1fdefd1 | 2020-09-01 16:58:00 +0800 | [diff] [blame] | 1054 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1055 | |
| 1056 | return esdhc_init_common(priv, &plat->mmc); |
| 1057 | } |
| 1058 | |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1059 | #ifdef MMC_SUPPORTS_TUNING |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1060 | static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode) |
| 1061 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1062 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1063 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1064 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 1065 | u32 val, irqstaten; |
| 1066 | int i; |
| 1067 | |
| 1068 | esdhc_tuning_block_enable(priv, true); |
| 1069 | esdhc_setbits32(®s->autoc12err, EXECUTE_TUNING); |
| 1070 | |
| 1071 | irqstaten = esdhc_read32(®s->irqstaten); |
| 1072 | esdhc_write32(®s->irqstaten, IRQSTATEN_BRR); |
| 1073 | |
| 1074 | for (i = 0; i < MAX_TUNING_LOOP; i++) { |
| 1075 | mmc_send_tuning(&plat->mmc, opcode, NULL); |
| 1076 | mdelay(1); |
| 1077 | |
| 1078 | val = esdhc_read32(®s->autoc12err); |
| 1079 | if (!(val & EXECUTE_TUNING)) { |
| 1080 | if (val & SMPCLKSEL) |
| 1081 | break; |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | esdhc_write32(®s->irqstaten, irqstaten); |
| 1086 | |
Yangbo Lu | db8f936 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 1087 | if (i != MAX_TUNING_LOOP) { |
| 1088 | if (plat->mmc.hs400_tuning) |
| 1089 | esdhc_setbits32(®s->sdtimingctl, FLW_CTL_BG); |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1090 | return 0; |
Yangbo Lu | db8f936 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 1091 | } |
Yangbo Lu | b1a4247 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1092 | |
| 1093 | printf("fsl_esdhc: tuning failed!\n"); |
| 1094 | esdhc_clrbits32(®s->autoc12err, SMPCLKSEL); |
| 1095 | esdhc_clrbits32(®s->autoc12err, EXECUTE_TUNING); |
| 1096 | esdhc_tuning_block_enable(priv, false); |
| 1097 | return -ETIMEDOUT; |
| 1098 | } |
| 1099 | #endif |
| 1100 | |
Yangbo Lu | db8f936 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 1101 | int fsl_esdhc_hs400_prepare_ddr(struct udevice *dev) |
| 1102 | { |
| 1103 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1104 | |
| 1105 | esdhc_tuning_block_enable(priv, false); |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1109 | static const struct dm_mmc_ops fsl_esdhc_ops = { |
| 1110 | .get_cd = fsl_esdhc_get_cd, |
| 1111 | .send_cmd = fsl_esdhc_send_cmd, |
| 1112 | .set_ios = fsl_esdhc_set_ios, |
Yinbo Zhu | 6f883e5 | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 1113 | #ifdef MMC_SUPPORTS_TUNING |
| 1114 | .execute_tuning = fsl_esdhc_execute_tuning, |
| 1115 | #endif |
Yangbo Lu | 1fdefd1 | 2020-09-01 16:58:00 +0800 | [diff] [blame] | 1116 | .reinit = fsl_esdhc_reinit, |
Yangbo Lu | db8f936 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 1117 | .hs400_prepare_ddr = fsl_esdhc_hs400_prepare_ddr, |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1118 | }; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1119 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1120 | static const struct udevice_id fsl_esdhc_ids[] = { |
Yangbo Lu | a6473f8 | 2016-12-07 11:54:31 +0800 | [diff] [blame] | 1121 | { .compatible = "fsl,esdhc", }, |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1122 | { /* sentinel */ } |
| 1123 | }; |
| 1124 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1125 | static int fsl_esdhc_bind(struct udevice *dev) |
| 1126 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1127 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1128 | |
| 1129 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 1130 | } |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1131 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1132 | U_BOOT_DRIVER(fsl_esdhc) = { |
| 1133 | .name = "fsl-esdhc-mmc", |
| 1134 | .id = UCLASS_MMC, |
| 1135 | .of_match = fsl_esdhc_ids, |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1136 | .ops = &fsl_esdhc_ops, |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1137 | .bind = fsl_esdhc_bind, |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1138 | .probe = fsl_esdhc_probe, |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1139 | .plat_auto = sizeof(struct fsl_esdhc_plat), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1140 | .priv_auto = sizeof(struct fsl_esdhc_priv), |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1141 | }; |
| 1142 | #endif |