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