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