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