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 | 4cc119b | 2019-05-23 11:05:46 +0800 | [diff] [blame] | 4 | * Copyright 2019 NXP Semiconductors |
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> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 23 | #include <asm/io.h> |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 24 | #include <dm.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 25 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
| 28 | struct fsl_esdhc { |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 29 | uint dsaddr; /* SDMA system address register */ |
| 30 | uint blkattr; /* Block attributes register */ |
| 31 | uint cmdarg; /* Command argument register */ |
| 32 | uint xfertyp; /* Transfer type register */ |
| 33 | uint cmdrsp0; /* Command response 0 register */ |
| 34 | uint cmdrsp1; /* Command response 1 register */ |
| 35 | uint cmdrsp2; /* Command response 2 register */ |
| 36 | uint cmdrsp3; /* Command response 3 register */ |
| 37 | uint datport; /* Buffer data port register */ |
| 38 | uint prsstat; /* Present state register */ |
| 39 | uint proctl; /* Protocol control register */ |
| 40 | uint sysctl; /* System Control Register */ |
| 41 | uint irqstat; /* Interrupt status register */ |
| 42 | uint irqstaten; /* Interrupt status enable register */ |
| 43 | uint irqsigen; /* Interrupt signal enable register */ |
| 44 | uint autoc12err; /* Auto CMD error status register */ |
| 45 | uint hostcapblt; /* Host controller capabilities register */ |
| 46 | uint wml; /* Watermark level register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 47 | char reserved1[8]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 48 | uint fevt; /* Force event register */ |
| 49 | uint admaes; /* ADMA error status register */ |
| 50 | uint adsaddr; /* ADMA system address register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 51 | char reserved2[160]; |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 52 | uint hostver; /* Host controller version register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 53 | char reserved3[4]; /* reserved */ |
Peng Fan | b9b4236 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 54 | uint dmaerraddr; /* DMA error address register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 55 | char reserved4[4]; /* reserved */ |
Peng Fan | b9b4236 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 56 | uint dmaerrattr; /* DMA error attribute register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 57 | char reserved5[4]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 58 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 59 | char reserved6[756]; /* reserved */ |
| 60 | uint esdhcctl; /* eSDHC control register */ |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 63 | struct fsl_esdhc_plat { |
| 64 | struct mmc_config cfg; |
| 65 | struct mmc mmc; |
| 66 | }; |
| 67 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 68 | /** |
| 69 | * struct fsl_esdhc_priv |
| 70 | * |
| 71 | * @esdhc_regs: registers of the sdhc controller |
| 72 | * @sdhc_clk: Current clk of the sdhc controller |
| 73 | * @bus_width: bus width, 1bit, 4bit or 8bit |
| 74 | * @cfg: mmc config |
| 75 | * @mmc: mmc |
| 76 | * Following is used when Driver Model is enabled for MMC |
| 77 | * @dev: pointer for the device |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 78 | * @cd_gpio: gpio for card detection |
Peng Fan | 01eb1c4 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 79 | * @wp_gpio: gpio for write protection |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 80 | */ |
| 81 | struct fsl_esdhc_priv { |
| 82 | struct fsl_esdhc *esdhc_regs; |
| 83 | unsigned int sdhc_clk; |
Yangbo Lu | 1ca7a9f | 2019-12-19 18:59:30 +0800 | [diff] [blame^] | 84 | bool is_sdhc_per_clk; |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 85 | unsigned int clock; |
Yangbo Lu | 77f2632 | 2019-10-21 18:09:07 +0800 | [diff] [blame] | 86 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 87 | struct mmc *mmc; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 88 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 89 | struct udevice *dev; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 90 | }; |
| 91 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 92 | /* Return the XFERTYP flags for a given command and data packet */ |
Kim Phillips | f9e0b60 | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 93 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 94 | { |
| 95 | uint xfertyp = 0; |
| 96 | |
| 97 | if (data) { |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 98 | xfertyp |= XFERTYP_DPSEL; |
| 99 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 100 | xfertyp |= XFERTYP_DMAEN; |
| 101 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 102 | if (data->blocks > 1) { |
| 103 | xfertyp |= XFERTYP_MSBSEL; |
| 104 | xfertyp |= XFERTYP_BCEN; |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 105 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 106 | xfertyp |= XFERTYP_AC12EN; |
| 107 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if (data->flags & MMC_DATA_READ) |
| 111 | xfertyp |= XFERTYP_DTDSEL; |
| 112 | } |
| 113 | |
| 114 | if (cmd->resp_type & MMC_RSP_CRC) |
| 115 | xfertyp |= XFERTYP_CCCEN; |
| 116 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 117 | xfertyp |= XFERTYP_CICEN; |
| 118 | if (cmd->resp_type & MMC_RSP_136) |
| 119 | xfertyp |= XFERTYP_RSPTYP_136; |
| 120 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 121 | xfertyp |= XFERTYP_RSPTYP_48_BUSY; |
| 122 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 123 | xfertyp |= XFERTYP_RSPTYP_48; |
| 124 | |
Jason Liu | bef0ff0 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 125 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 126 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
Yangbo Lu | b73a3d6 | 2016-01-21 17:33:19 +0800 | [diff] [blame] | 127 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 128 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 129 | } |
| 130 | |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 131 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 132 | /* |
| 133 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 134 | */ |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 135 | static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv, |
| 136 | struct mmc_data *data) |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 137 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 138 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 139 | uint blocks; |
| 140 | char *buffer; |
| 141 | uint databuf; |
| 142 | uint size; |
| 143 | uint irqstat; |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 144 | ulong start; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 145 | |
| 146 | if (data->flags & MMC_DATA_READ) { |
| 147 | blocks = data->blocks; |
| 148 | buffer = data->dest; |
| 149 | while (blocks) { |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 150 | start = get_timer(0); |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 151 | size = data->blocksize; |
| 152 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 153 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) { |
| 154 | if (get_timer(start) > PIO_TIMEOUT) { |
| 155 | printf("\nData Read Failed in PIO Mode."); |
| 156 | return; |
| 157 | } |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 158 | } |
| 159 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 160 | udelay(100); /* Wait before last byte transfer complete */ |
| 161 | irqstat = esdhc_read32(®s->irqstat); |
| 162 | databuf = in_le32(®s->datport); |
| 163 | *((uint *)buffer) = databuf; |
| 164 | buffer += 4; |
| 165 | size -= 4; |
| 166 | } |
| 167 | blocks--; |
| 168 | } |
| 169 | } else { |
| 170 | blocks = data->blocks; |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 171 | buffer = (char *)data->src; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 172 | while (blocks) { |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 173 | start = get_timer(0); |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 174 | size = data->blocksize; |
| 175 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 176 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) { |
| 177 | if (get_timer(start) > PIO_TIMEOUT) { |
| 178 | printf("\nData Write Failed in PIO Mode."); |
| 179 | return; |
| 180 | } |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 181 | } |
| 182 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 183 | udelay(100); /* Wait before last byte transfer complete */ |
| 184 | databuf = *((uint *)buffer); |
| 185 | buffer += 4; |
| 186 | size -= 4; |
| 187 | irqstat = esdhc_read32(®s->irqstat); |
| 188 | out_le32(®s->datport, databuf); |
| 189 | } |
| 190 | blocks--; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | #endif |
| 195 | |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 196 | static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 197 | struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 198 | { |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 199 | int timeout; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 200 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 201 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 202 | dma_addr_t addr; |
| 203 | #endif |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 204 | uint wml_value; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 205 | |
| 206 | wml_value = data->blocksize/4; |
| 207 | |
| 208 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 0244963 | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 209 | if (wml_value > WML_RD_WML_MAX) |
| 210 | wml_value = WML_RD_WML_MAX_VAL; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 211 | |
Roy Zang | e5853af | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 212 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 213 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
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 | addr = virt_to_phys((void *)(data->dest)); |
| 216 | if (upper_32_bits(addr)) |
| 217 | printf("Error found for upper 32 bits\n"); |
| 218 | else |
| 219 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 220 | #else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 221 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 222 | #endif |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 223 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 224 | } else { |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 225 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 226 | flush_dcache_range((ulong)data->src, |
| 227 | (ulong)data->src+data->blocks |
| 228 | *data->blocksize); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 229 | #endif |
Priyanka Jain | 0244963 | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 230 | if (wml_value > WML_WR_WML_MAX) |
| 231 | wml_value = WML_WR_WML_MAX_VAL; |
Yangbo Lu | f3bcc83 | 2019-10-31 18:54:25 +0800 | [diff] [blame] | 232 | |
| 233 | if (!(esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL)) { |
| 234 | printf("Can not write to locked SD card.\n"); |
| 235 | return -EINVAL; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 236 | } |
Roy Zang | e5853af | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 237 | |
| 238 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 239 | wml_value << 16); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 240 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 241 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 242 | addr = virt_to_phys((void *)(data->src)); |
| 243 | if (upper_32_bits(addr)) |
| 244 | printf("Error found for upper 32 bits\n"); |
| 245 | else |
| 246 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 247 | #else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 248 | esdhc_write32(®s->dsaddr, (u32)data->src); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 249 | #endif |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 250 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 251 | } |
| 252 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 253 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 254 | |
| 255 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 256 | /* |
| 257 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 258 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 259 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 260 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 261 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 262 | * As 1) >= 2) |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 263 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 264 | * Taking log2 both the sides |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 265 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 266 | * Rounding up to next power of 2 |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 267 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 268 | * => timeout + 13 = fls(mmc->clock/4) |
Yangbo Lu | 9d7f321 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 269 | * |
| 270 | * However, the MMC spec "It is strongly recommended for hosts to |
| 271 | * implement more than 500ms timeout value even if the card |
| 272 | * indicates the 250ms maximum busy length." Even the previous |
| 273 | * value of 300ms is known to be insufficient for some cards. |
| 274 | * So, we use |
| 275 | * => timeout + 13 = fls(mmc->clock/2) |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 276 | */ |
Yangbo Lu | 9d7f321 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 277 | timeout = fls(mmc->clock/2); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 278 | timeout -= 13; |
| 279 | |
| 280 | if (timeout > 14) |
| 281 | timeout = 14; |
| 282 | |
| 283 | if (timeout < 0) |
| 284 | timeout = 0; |
| 285 | |
Kumar Gala | 9a878d5 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 286 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 287 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 288 | timeout++; |
| 289 | #endif |
| 290 | |
Haijun.Zhang | edeb83a | 2014-03-18 17:04:23 +0800 | [diff] [blame] | 291 | #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE |
| 292 | timeout = 0xE; |
| 293 | #endif |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 294 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 299 | static void check_and_invalidate_dcache_range |
| 300 | (struct mmc_cmd *cmd, |
| 301 | struct mmc_data *data) { |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 302 | unsigned start = 0; |
Yangbo Lu | e7702c6 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 303 | unsigned end = 0; |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 304 | unsigned size = roundup(ARCH_DMA_MINALIGN, |
| 305 | data->blocks*data->blocksize); |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 306 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 307 | dma_addr_t addr; |
| 308 | |
| 309 | addr = virt_to_phys((void *)(data->dest)); |
| 310 | if (upper_32_bits(addr)) |
| 311 | printf("Error found for upper 32 bits\n"); |
| 312 | else |
| 313 | start = lower_32_bits(addr); |
Yangbo Lu | e7702c6 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 314 | #else |
| 315 | start = (unsigned)data->dest; |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 316 | #endif |
Yangbo Lu | e7702c6 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 317 | end = start + size; |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 318 | invalidate_dcache_range(start, end); |
| 319 | } |
Angelo Dureghello | 520a669 | 2019-01-19 10:40:38 +0100 | [diff] [blame] | 320 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 321 | /* |
| 322 | * Sends a command out on the bus. Takes the mmc pointer, |
| 323 | * a command pointer, and an optional data pointer. |
| 324 | */ |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 325 | static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 326 | struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 327 | { |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 328 | int err = 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 329 | uint xfertyp; |
| 330 | uint irqstat; |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 331 | u32 flags = IRQSTAT_CC | IRQSTAT_CTOE; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 332 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Fabio Estevam | 7300ef5 | 2018-11-19 10:31:53 -0200 | [diff] [blame] | 333 | unsigned long start; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 334 | |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 335 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 336 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 337 | return 0; |
| 338 | #endif |
| 339 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 340 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 341 | |
| 342 | sync(); |
| 343 | |
| 344 | /* Wait for the bus to be idle */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 345 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 346 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 347 | ; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 348 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 349 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 350 | ; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 351 | |
| 352 | /* Wait at least 8 SD clock cycles before the next command */ |
| 353 | /* |
| 354 | * Note: This is way more than 8 cycles, but 1ms seems to |
| 355 | * resolve timing issues with some cards |
| 356 | */ |
| 357 | udelay(1000); |
| 358 | |
| 359 | /* Set up for a data transfer if we have one */ |
| 360 | if (data) { |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 361 | err = esdhc_setup_data(priv, mmc, data); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 362 | if(err) |
| 363 | return err; |
Peng Fan | 9cb5e99 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 364 | |
| 365 | if (data->flags & MMC_DATA_READ) |
| 366 | check_and_invalidate_dcache_range(cmd, data); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* Figure out the transfer arguments */ |
| 370 | xfertyp = esdhc_xfertyp(cmd, data); |
| 371 | |
Andrew Gabbasov | 4816b7a | 2013-06-11 10:34:22 -0500 | [diff] [blame] | 372 | /* Mask all irqs */ |
| 373 | esdhc_write32(®s->irqsigen, 0); |
| 374 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 375 | /* Send the command */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 376 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
| 377 | esdhc_write32(®s->xfertyp, xfertyp); |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 378 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 379 | /* Wait for the command to complete */ |
Fabio Estevam | 7300ef5 | 2018-11-19 10:31:53 -0200 | [diff] [blame] | 380 | start = get_timer(0); |
| 381 | while (!(esdhc_read32(®s->irqstat) & flags)) { |
| 382 | if (get_timer(start) > 1000) { |
| 383 | err = -ETIMEDOUT; |
| 384 | goto out; |
| 385 | } |
| 386 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 387 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 388 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 389 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 390 | if (irqstat & CMD_ERR) { |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 391 | err = -ECOMM; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 392 | goto out; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 395 | if (irqstat & IRQSTAT_CTOE) { |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 396 | err = -ETIMEDOUT; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 397 | goto out; |
| 398 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 399 | |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 400 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 401 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
Yangbo Lu | 3ffa851 | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 402 | int timeout = 6000; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 403 | |
Yangbo Lu | 3ffa851 | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 404 | /* Poll on DATA0 line for cmd with busy signal for 600 ms */ |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 405 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 406 | PRSSTAT_DAT0)) { |
| 407 | udelay(100); |
| 408 | timeout--; |
| 409 | } |
| 410 | |
| 411 | if (timeout <= 0) { |
| 412 | printf("Timeout waiting for DAT0 to go high!\n"); |
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; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 418 | /* Copy the response to the response buffer */ |
| 419 | if (cmd->resp_type & MMC_RSP_136) { |
| 420 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 421 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 422 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 423 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 424 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 425 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 426 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 427 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 428 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 429 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 430 | } else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 431 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 432 | |
| 433 | /* Wait until all of the blocks are transferred */ |
| 434 | if (data) { |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 435 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 436 | esdhc_pio_read_write(priv, data); |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 437 | #else |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 438 | do { |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 439 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 440 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 441 | if (irqstat & IRQSTAT_DTOE) { |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 442 | err = -ETIMEDOUT; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 443 | goto out; |
| 444 | } |
Frans Meulenbroeks | 010ba98 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 445 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 446 | if (irqstat & DATA_ERR) { |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 447 | err = -ECOMM; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 448 | goto out; |
| 449 | } |
Yinbo Zhu | 101d3ef | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 450 | } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 451 | |
Peng Fan | 9cb5e99 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 452 | /* |
| 453 | * Need invalidate the dcache here again to avoid any |
| 454 | * cache-fill during the DMA operations such as the |
| 455 | * speculative pre-fetching etc. |
| 456 | */ |
Angelo Dureghello | 520a669 | 2019-01-19 10:40:38 +0100 | [diff] [blame] | 457 | if (data->flags & MMC_DATA_READ) { |
Eric Nelson | 70e6869 | 2013-04-03 12:31:56 +0000 | [diff] [blame] | 458 | check_and_invalidate_dcache_range(cmd, data); |
Angelo Dureghello | 520a669 | 2019-01-19 10:40:38 +0100 | [diff] [blame] | 459 | } |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 460 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 461 | } |
| 462 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 463 | out: |
| 464 | /* Reset CMD and DATA portions on error */ |
| 465 | if (err) { |
| 466 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 467 | SYSCTL_RSTC); |
| 468 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 469 | ; |
| 470 | |
| 471 | if (data) { |
| 472 | esdhc_write32(®s->sysctl, |
| 473 | esdhc_read32(®s->sysctl) | |
| 474 | SYSCTL_RSTD); |
| 475 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 476 | ; |
| 477 | } |
| 478 | } |
| 479 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 480 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 481 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 482 | return err; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 483 | } |
| 484 | |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 485 | 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] | 486 | { |
Benoît Thébaudeau | 22464e0 | 2018-01-16 22:44:18 +0100 | [diff] [blame] | 487 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Benoît Thébaudeau | e16e922 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 488 | int div = 1; |
Benoît Thébaudeau | e16e922 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 489 | int pre_div = 2; |
Yinbo Zhu | 101d3ef | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 490 | unsigned int sdhc_clk = priv->sdhc_clk; |
| 491 | u32 time_out; |
| 492 | u32 value; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 493 | uint clk; |
| 494 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 495 | if (clock < mmc->cfg->f_min) |
| 496 | clock = mmc->cfg->f_min; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 497 | |
Yangbo Lu | 4ee9b86 | 2019-10-21 18:09:09 +0800 | [diff] [blame] | 498 | while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256) |
Lukasz Majewski | 2a52183 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 499 | pre_div *= 2; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 500 | |
Yangbo Lu | 4ee9b86 | 2019-10-21 18:09:09 +0800 | [diff] [blame] | 501 | while (sdhc_clk / (div * pre_div) > clock && div < 16) |
Lukasz Majewski | 2a52183 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 502 | div++; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 503 | |
Benoît Thébaudeau | e16e922 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 504 | pre_div >>= 1; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 505 | div -= 1; |
| 506 | |
| 507 | clk = (pre_div << 8) | (div << 4); |
| 508 | |
Kumar Gala | 09876a3 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 509 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 510 | |
| 511 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 512 | |
Yinbo Zhu | 101d3ef | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 513 | time_out = 20; |
| 514 | value = PRSSTAT_SDSTB; |
| 515 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 516 | if (time_out == 0) { |
| 517 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 518 | break; |
| 519 | } |
| 520 | time_out--; |
| 521 | mdelay(1); |
| 522 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 523 | |
Eric Nelson | c8e615c | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 524 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 525 | } |
| 526 | |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 527 | static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable) |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 528 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 529 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 530 | u32 value; |
| 531 | u32 time_out; |
| 532 | |
| 533 | value = esdhc_read32(®s->sysctl); |
| 534 | |
| 535 | if (enable) |
| 536 | value |= SYSCTL_CKEN; |
| 537 | else |
| 538 | value &= ~SYSCTL_CKEN; |
| 539 | |
| 540 | esdhc_write32(®s->sysctl, value); |
| 541 | |
| 542 | time_out = 20; |
| 543 | value = PRSSTAT_SDSTB; |
| 544 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 545 | if (time_out == 0) { |
| 546 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 547 | break; |
| 548 | } |
| 549 | time_out--; |
| 550 | mdelay(1); |
| 551 | } |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 552 | } |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 553 | |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 554 | 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] | 555 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 556 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 557 | |
Yangbo Lu | 1ca7a9f | 2019-12-19 18:59:30 +0800 | [diff] [blame^] | 558 | if (priv->is_sdhc_per_clk) { |
| 559 | /* Select to use peripheral clock */ |
| 560 | esdhc_clock_control(priv, false); |
| 561 | esdhc_setbits32(®s->esdhcctl, ESDHCCTL_PCS); |
| 562 | esdhc_clock_control(priv, true); |
| 563 | } |
| 564 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 565 | /* Set the clock speed */ |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 566 | if (priv->clock != mmc->clock) |
| 567 | set_sysctl(priv, mmc, mmc->clock); |
| 568 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 569 | /* Set the bus width */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 570 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 571 | |
| 572 | if (mmc->bus_width == 4) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 573 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 574 | else if (mmc->bus_width == 8) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 575 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 576 | |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 577 | return 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 578 | } |
| 579 | |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 580 | 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] | 581 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 582 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Simon Glass | 0c3ef22 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 583 | ulong start; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 584 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 585 | /* Reset the entire host controller */ |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 586 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 587 | |
| 588 | /* Wait until the controller is available */ |
Simon Glass | 0c3ef22 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 589 | start = get_timer(0); |
| 590 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 591 | if (get_timer(start) > 1000) |
| 592 | return -ETIMEDOUT; |
| 593 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 594 | |
P.V.Suresh | 7b1868b | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 595 | /* Enable cache snooping */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 596 | esdhc_write32(®s->esdhcctl, 0x00000040); |
P.V.Suresh | 7b1868b | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 597 | |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 598 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 599 | |
| 600 | /* Set the initial clock speed */ |
Jaehoon Chung | 239cb2f | 2018-01-26 19:25:29 +0900 | [diff] [blame] | 601 | mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 602 | |
| 603 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 604 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 605 | |
| 606 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 607 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 608 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 609 | /* Set timout to the maximum value */ |
| 610 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 611 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 612 | return 0; |
| 613 | } |
| 614 | |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 615 | static int esdhc_getcd_common(struct fsl_esdhc_priv *priv) |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 616 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 617 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 618 | int timeout = 1000; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 619 | |
Haijun.Zhang | 05f5854 | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 620 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 621 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 622 | return 1; |
| 623 | #endif |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 624 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 625 | udelay(1000); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 626 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 627 | return timeout > 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 628 | } |
| 629 | |
Yangbo Lu | b64dc8d | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 630 | static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv, |
| 631 | struct mmc_config *cfg) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 632 | { |
Yangbo Lu | b64dc8d | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 633 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 63267b4 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 634 | u32 caps; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 635 | |
Wang Huan | c929213 | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 636 | caps = esdhc_read32(®s->hostcapblt); |
Roy Zang | 3935661 | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 637 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
Yangbo Lu | 63267b4 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 638 | caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30); |
Roy Zang | 3935661 | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 639 | #endif |
Haijun.Zhang | 8a065e9 | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 640 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
Yangbo Lu | 63267b4 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 641 | caps |= HOSTCAPBLT_VS33; |
Haijun.Zhang | 8a065e9 | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 642 | #endif |
Yangbo Lu | 63267b4 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 643 | if (caps & HOSTCAPBLT_VS18) |
| 644 | cfg->voltages |= MMC_VDD_165_195; |
| 645 | if (caps & HOSTCAPBLT_VS30) |
| 646 | cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31; |
| 647 | if (caps & HOSTCAPBLT_VS33) |
| 648 | cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 649 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 650 | cfg->name = "FSL_SDHC"; |
Abbas Raza | e6bf977 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 651 | |
Yangbo Lu | 63267b4 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 652 | if (caps & HOSTCAPBLT_HSS) |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 653 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 654 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 655 | cfg->f_min = 400000; |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 656 | cfg->f_max = min(priv->sdhc_clk, (u32)200000000); |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 657 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 658 | } |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 659 | |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 660 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 661 | void mmc_adapter_card_type_ident(void) |
| 662 | { |
| 663 | u8 card_id; |
| 664 | u8 value; |
| 665 | |
| 666 | card_id = QIXIS_READ(present) & QIXIS_SDID_MASK; |
| 667 | gd->arch.sdhc_adapter = card_id; |
| 668 | |
| 669 | switch (card_id) { |
| 670 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45: |
Yangbo Lu | 81eacd6 | 2015-09-17 10:27:12 +0800 | [diff] [blame] | 671 | value = QIXIS_READ(brdcfg[5]); |
| 672 | value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7); |
| 673 | QIXIS_WRITE(brdcfg[5], value); |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 674 | break; |
| 675 | case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY: |
Yangbo Lu | c6799ce | 2015-09-17 10:27:48 +0800 | [diff] [blame] | 676 | value = QIXIS_READ(pwr_ctl[1]); |
| 677 | value |= QIXIS_EVDD_BY_SDHC_VS; |
| 678 | QIXIS_WRITE(pwr_ctl[1], value); |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 679 | break; |
| 680 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44: |
| 681 | value = QIXIS_READ(brdcfg[5]); |
| 682 | value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT); |
| 683 | QIXIS_WRITE(brdcfg[5], value); |
| 684 | break; |
| 685 | case QIXIS_ESDHC_ADAPTER_TYPE_RSV: |
| 686 | break; |
| 687 | case QIXIS_ESDHC_ADAPTER_TYPE_MMC: |
| 688 | break; |
| 689 | case QIXIS_ESDHC_ADAPTER_TYPE_SD: |
| 690 | break; |
| 691 | case QIXIS_ESDHC_NO_ADAPTER: |
| 692 | break; |
| 693 | default: |
| 694 | break; |
| 695 | } |
| 696 | } |
| 697 | #endif |
| 698 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 699 | #ifdef CONFIG_OF_LIBFDT |
Yangbo Lu | d84139c | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 700 | __weak int esdhc_status_fixup(void *blob, const char *compat) |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 701 | { |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 702 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 703 | if (!hwconfig("esdhc")) { |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 704 | do_fixup_by_compat(blob, compat, "status", "disabled", |
Yangbo Lu | d84139c | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 705 | sizeof("disabled"), 1); |
| 706 | return 1; |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 707 | } |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 708 | #endif |
Yangbo Lu | d84139c | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 713 | { |
| 714 | const char *compat = "fsl,esdhc"; |
| 715 | |
| 716 | if (esdhc_status_fixup(blob, compat)) |
| 717 | return; |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 718 | |
| 719 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | 9e247d1 | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 720 | gd->arch.sdhc_clk, 1); |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 721 | } |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 722 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 723 | |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 724 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 725 | static int esdhc_getcd(struct mmc *mmc) |
| 726 | { |
| 727 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 728 | |
| 729 | return esdhc_getcd_common(priv); |
| 730 | } |
| 731 | |
| 732 | static int esdhc_init(struct mmc *mmc) |
| 733 | { |
| 734 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 735 | |
| 736 | return esdhc_init_common(priv, mmc); |
| 737 | } |
| 738 | |
| 739 | static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 740 | struct mmc_data *data) |
| 741 | { |
| 742 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 743 | |
| 744 | return esdhc_send_cmd_common(priv, mmc, cmd, data); |
| 745 | } |
| 746 | |
| 747 | static int esdhc_set_ios(struct mmc *mmc) |
| 748 | { |
| 749 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 750 | |
| 751 | return esdhc_set_ios_common(priv, mmc); |
| 752 | } |
| 753 | |
| 754 | static const struct mmc_ops esdhc_ops = { |
| 755 | .getcd = esdhc_getcd, |
| 756 | .init = esdhc_init, |
| 757 | .send_cmd = esdhc_send_cmd, |
| 758 | .set_ios = esdhc_set_ios, |
| 759 | }; |
| 760 | |
| 761 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
| 762 | { |
| 763 | struct fsl_esdhc_plat *plat; |
| 764 | struct fsl_esdhc_priv *priv; |
| 765 | struct mmc_config *mmc_cfg; |
| 766 | struct mmc *mmc; |
| 767 | |
| 768 | if (!cfg) |
| 769 | return -EINVAL; |
| 770 | |
| 771 | priv = calloc(sizeof(struct fsl_esdhc_priv), 1); |
| 772 | if (!priv) |
| 773 | return -ENOMEM; |
| 774 | plat = calloc(sizeof(struct fsl_esdhc_plat), 1); |
| 775 | if (!plat) { |
| 776 | free(priv); |
| 777 | return -ENOMEM; |
| 778 | } |
| 779 | |
| 780 | priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); |
| 781 | priv->sdhc_clk = cfg->sdhc_clk; |
Yangbo Lu | 1ca7a9f | 2019-12-19 18:59:30 +0800 | [diff] [blame^] | 782 | if (gd->arch.sdhc_per_clk) |
| 783 | priv->is_sdhc_per_clk = true; |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 784 | |
| 785 | mmc_cfg = &plat->cfg; |
| 786 | |
| 787 | if (cfg->max_bus_width == 8) { |
| 788 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT | |
| 789 | MMC_MODE_8BIT; |
| 790 | } else if (cfg->max_bus_width == 4) { |
| 791 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT; |
| 792 | } else if (cfg->max_bus_width == 1) { |
| 793 | mmc_cfg->host_caps |= MMC_MODE_1BIT; |
| 794 | } else { |
| 795 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT | |
| 796 | MMC_MODE_8BIT; |
| 797 | printf("No max bus width provided. Assume 8-bit supported.\n"); |
| 798 | } |
| 799 | |
| 800 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 801 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
| 802 | mmc_cfg->host_caps &= ~MMC_MODE_8BIT; |
| 803 | #endif |
| 804 | mmc_cfg->ops = &esdhc_ops; |
| 805 | |
| 806 | fsl_esdhc_get_cfg_common(priv, mmc_cfg); |
| 807 | |
| 808 | mmc = mmc_create(mmc_cfg, priv); |
| 809 | if (!mmc) |
| 810 | return -EIO; |
| 811 | |
| 812 | priv->mmc = mmc; |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 817 | { |
| 818 | struct fsl_esdhc_cfg *cfg; |
| 819 | |
| 820 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
| 821 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
Yangbo Lu | 1ca7a9f | 2019-12-19 18:59:30 +0800 | [diff] [blame^] | 822 | /* Prefer peripheral clock which provides higher frequency. */ |
| 823 | if (gd->arch.sdhc_per_clk) |
| 824 | cfg->sdhc_clk = gd->arch.sdhc_per_clk; |
| 825 | else |
| 826 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 827 | return fsl_esdhc_initialize(bis, cfg); |
| 828 | } |
| 829 | #else /* DM_MMC */ |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 830 | static int fsl_esdhc_probe(struct udevice *dev) |
| 831 | { |
| 832 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 833 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 834 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 835 | fdt_addr_t addr; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 836 | struct mmc *mmc; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 837 | |
Simon Glass | 80e9df4 | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 838 | addr = dev_read_addr(dev); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 839 | if (addr == FDT_ADDR_T_NONE) |
| 840 | return -EINVAL; |
Yinbo Zhu | 583d5e9 | 2019-04-11 11:01:50 +0000 | [diff] [blame] | 841 | #ifdef CONFIG_PPC |
| 842 | priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr); |
| 843 | #else |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 844 | priv->esdhc_regs = (struct fsl_esdhc *)addr; |
Yinbo Zhu | 583d5e9 | 2019-04-11 11:01:50 +0000 | [diff] [blame] | 845 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 846 | priv->dev = dev; |
| 847 | |
Yangbo Lu | 1ca7a9f | 2019-12-19 18:59:30 +0800 | [diff] [blame^] | 848 | if (gd->arch.sdhc_per_clk) { |
| 849 | priv->sdhc_clk = gd->arch.sdhc_per_clk; |
| 850 | priv->is_sdhc_per_clk = true; |
| 851 | } else { |
| 852 | priv->sdhc_clk = gd->arch.sdhc_clk; |
| 853 | } |
| 854 | |
Yangbo Lu | b8626e4 | 2019-11-12 19:28:36 +0800 | [diff] [blame] | 855 | if (priv->sdhc_clk <= 0) { |
| 856 | dev_err(dev, "Unable to get clk for %s\n", dev->name); |
| 857 | return -EINVAL; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 858 | } |
| 859 | |
Yangbo Lu | b64dc8d | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 860 | fsl_esdhc_get_cfg_common(priv, &plat->cfg); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 861 | |
Yinbo Zhu | 101d3ef | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 862 | mmc_of_parse(dev, &plat->cfg); |
| 863 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 864 | mmc = &plat->mmc; |
| 865 | mmc->cfg = &plat->cfg; |
| 866 | mmc->dev = dev; |
Yangbo Lu | 4cc119b | 2019-05-23 11:05:46 +0800 | [diff] [blame] | 867 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 868 | upriv->mmc = mmc; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 869 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 870 | return esdhc_init_common(priv, mmc); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 871 | } |
| 872 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 873 | static int fsl_esdhc_get_cd(struct udevice *dev) |
| 874 | { |
Yangbo Lu | 9fed28d | 2019-10-31 18:54:24 +0800 | [diff] [blame] | 875 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 876 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 877 | |
Yangbo Lu | 9fed28d | 2019-10-31 18:54:24 +0800 | [diff] [blame] | 878 | if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE) |
| 879 | return 1; |
| 880 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 881 | return esdhc_getcd_common(priv); |
| 882 | } |
| 883 | |
| 884 | static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 885 | struct mmc_data *data) |
| 886 | { |
| 887 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 888 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 889 | |
| 890 | return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data); |
| 891 | } |
| 892 | |
| 893 | static int fsl_esdhc_set_ios(struct udevice *dev) |
| 894 | { |
| 895 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 896 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 897 | |
| 898 | return esdhc_set_ios_common(priv, &plat->mmc); |
| 899 | } |
| 900 | |
| 901 | static const struct dm_mmc_ops fsl_esdhc_ops = { |
| 902 | .get_cd = fsl_esdhc_get_cd, |
| 903 | .send_cmd = fsl_esdhc_send_cmd, |
| 904 | .set_ios = fsl_esdhc_set_ios, |
Yinbo Zhu | 101d3ef | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 905 | #ifdef MMC_SUPPORTS_TUNING |
| 906 | .execute_tuning = fsl_esdhc_execute_tuning, |
| 907 | #endif |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 908 | }; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 909 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 910 | static const struct udevice_id fsl_esdhc_ids[] = { |
Yangbo Lu | 2a99b60 | 2016-12-07 11:54:31 +0800 | [diff] [blame] | 911 | { .compatible = "fsl,esdhc", }, |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 912 | { /* sentinel */ } |
| 913 | }; |
| 914 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 915 | static int fsl_esdhc_bind(struct udevice *dev) |
| 916 | { |
| 917 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 918 | |
| 919 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 920 | } |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 921 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 922 | U_BOOT_DRIVER(fsl_esdhc) = { |
| 923 | .name = "fsl-esdhc-mmc", |
| 924 | .id = UCLASS_MMC, |
| 925 | .of_match = fsl_esdhc_ids, |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 926 | .ops = &fsl_esdhc_ops, |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 927 | .bind = fsl_esdhc_bind, |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 928 | .probe = fsl_esdhc_probe, |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 929 | .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat), |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 930 | .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv), |
| 931 | }; |
| 932 | #endif |