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