Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1 | /* |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 2 | * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 3 | * Andy Fleming |
| 4 | * |
| 5 | * Based vaguely on the pxa mmc code: |
| 6 | * (C) Copyright 2003 |
| 7 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
| 8 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
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> |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 15 | #include <hwconfig.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 16 | #include <mmc.h> |
| 17 | #include <part.h> |
| 18 | #include <malloc.h> |
| 19 | #include <mmc.h> |
| 20 | #include <fsl_esdhc.h> |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 21 | #include <fdt_support.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 22 | #include <asm/io.h> |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 23 | #include <dm.h> |
| 24 | #include <asm-generic/gpio.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 | |
Ye.Li | 3d46c31 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 28 | #define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \ |
| 29 | IRQSTATEN_CINT | \ |
| 30 | IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \ |
| 31 | IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \ |
| 32 | IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \ |
| 33 | IRQSTATEN_DINT) |
| 34 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 35 | struct fsl_esdhc { |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 36 | uint dsaddr; /* SDMA system address register */ |
| 37 | uint blkattr; /* Block attributes register */ |
| 38 | uint cmdarg; /* Command argument register */ |
| 39 | uint xfertyp; /* Transfer type register */ |
| 40 | uint cmdrsp0; /* Command response 0 register */ |
| 41 | uint cmdrsp1; /* Command response 1 register */ |
| 42 | uint cmdrsp2; /* Command response 2 register */ |
| 43 | uint cmdrsp3; /* Command response 3 register */ |
| 44 | uint datport; /* Buffer data port register */ |
| 45 | uint prsstat; /* Present state register */ |
| 46 | uint proctl; /* Protocol control register */ |
| 47 | uint sysctl; /* System Control Register */ |
| 48 | uint irqstat; /* Interrupt status register */ |
| 49 | uint irqstaten; /* Interrupt status enable register */ |
| 50 | uint irqsigen; /* Interrupt signal enable register */ |
| 51 | uint autoc12err; /* Auto CMD error status register */ |
| 52 | uint hostcapblt; /* Host controller capabilities register */ |
| 53 | uint wml; /* Watermark level register */ |
| 54 | uint mixctrl; /* For USDHC */ |
| 55 | char reserved1[4]; /* reserved */ |
| 56 | uint fevt; /* Force event register */ |
| 57 | uint admaes; /* ADMA error status register */ |
| 58 | uint adsaddr; /* ADMA system address register */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 59 | char reserved2[100]; /* reserved */ |
| 60 | uint vendorspec; /* Vendor Specific register */ |
Peng Fan | a4f9c6b | 2015-03-10 15:35:46 +0800 | [diff] [blame] | 61 | char reserved3[56]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 62 | uint hostver; /* Host controller version register */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 63 | char reserved4[4]; /* reserved */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 64 | uint dmaerraddr; /* DMA error address register */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 65 | char reserved5[4]; /* reserved */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 66 | uint dmaerrattr; /* DMA error attribute register */ |
| 67 | char reserved6[4]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 68 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 69 | char reserved7[8]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 70 | uint tcr; /* Tuning control register */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 71 | char reserved8[28]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 72 | uint sddirctl; /* SD direction control register */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 73 | char reserved9[712]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 74 | uint scr; /* eSDHC control register */ |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 75 | }; |
| 76 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 77 | /** |
| 78 | * struct fsl_esdhc_priv |
| 79 | * |
| 80 | * @esdhc_regs: registers of the sdhc controller |
| 81 | * @sdhc_clk: Current clk of the sdhc controller |
| 82 | * @bus_width: bus width, 1bit, 4bit or 8bit |
| 83 | * @cfg: mmc config |
| 84 | * @mmc: mmc |
| 85 | * Following is used when Driver Model is enabled for MMC |
| 86 | * @dev: pointer for the device |
| 87 | * @non_removable: 0: removable; 1: non-removable |
| 88 | * @cd_gpio: gpio for card detection |
| 89 | */ |
| 90 | struct fsl_esdhc_priv { |
| 91 | struct fsl_esdhc *esdhc_regs; |
| 92 | unsigned int sdhc_clk; |
| 93 | unsigned int bus_width; |
| 94 | struct mmc_config cfg; |
| 95 | struct mmc *mmc; |
| 96 | struct udevice *dev; |
| 97 | int non_removable; |
| 98 | struct gpio_desc cd_gpio; |
| 99 | }; |
| 100 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 101 | /* Return the XFERTYP flags for a given command and data packet */ |
Kim Phillips | f9e0b60 | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 102 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 103 | { |
| 104 | uint xfertyp = 0; |
| 105 | |
| 106 | if (data) { |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 107 | xfertyp |= XFERTYP_DPSEL; |
| 108 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 109 | xfertyp |= XFERTYP_DMAEN; |
| 110 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 111 | if (data->blocks > 1) { |
| 112 | xfertyp |= XFERTYP_MSBSEL; |
| 113 | xfertyp |= XFERTYP_BCEN; |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 114 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 115 | xfertyp |= XFERTYP_AC12EN; |
| 116 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (data->flags & MMC_DATA_READ) |
| 120 | xfertyp |= XFERTYP_DTDSEL; |
| 121 | } |
| 122 | |
| 123 | if (cmd->resp_type & MMC_RSP_CRC) |
| 124 | xfertyp |= XFERTYP_CCCEN; |
| 125 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 126 | xfertyp |= XFERTYP_CICEN; |
| 127 | if (cmd->resp_type & MMC_RSP_136) |
| 128 | xfertyp |= XFERTYP_RSPTYP_136; |
| 129 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 130 | xfertyp |= XFERTYP_RSPTYP_48_BUSY; |
| 131 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 132 | xfertyp |= XFERTYP_RSPTYP_48; |
| 133 | |
Jason Liu | bef0ff0 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 134 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 135 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
Yangbo Lu | b73a3d6 | 2016-01-21 17:33:19 +0800 | [diff] [blame] | 136 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 137 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 138 | } |
| 139 | |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 140 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 141 | /* |
| 142 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 143 | */ |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 144 | static void |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 145 | esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data) |
| 146 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 147 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 148 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 149 | uint blocks; |
| 150 | char *buffer; |
| 151 | uint databuf; |
| 152 | uint size; |
| 153 | uint irqstat; |
| 154 | uint timeout; |
| 155 | |
| 156 | if (data->flags & MMC_DATA_READ) { |
| 157 | blocks = data->blocks; |
| 158 | buffer = data->dest; |
| 159 | while (blocks) { |
| 160 | timeout = PIO_TIMEOUT; |
| 161 | size = data->blocksize; |
| 162 | irqstat = esdhc_read32(®s->irqstat); |
| 163 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN) |
| 164 | && --timeout); |
| 165 | if (timeout <= 0) { |
| 166 | printf("\nData Read Failed in PIO Mode."); |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 167 | return; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 168 | } |
| 169 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 170 | udelay(100); /* Wait before last byte transfer complete */ |
| 171 | irqstat = esdhc_read32(®s->irqstat); |
| 172 | databuf = in_le32(®s->datport); |
| 173 | *((uint *)buffer) = databuf; |
| 174 | buffer += 4; |
| 175 | size -= 4; |
| 176 | } |
| 177 | blocks--; |
| 178 | } |
| 179 | } else { |
| 180 | blocks = data->blocks; |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 181 | buffer = (char *)data->src; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 182 | while (blocks) { |
| 183 | timeout = PIO_TIMEOUT; |
| 184 | size = data->blocksize; |
| 185 | irqstat = esdhc_read32(®s->irqstat); |
| 186 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN) |
| 187 | && --timeout); |
| 188 | if (timeout <= 0) { |
| 189 | printf("\nData Write Failed in PIO Mode."); |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 190 | return; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 191 | } |
| 192 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 193 | udelay(100); /* Wait before last byte transfer complete */ |
| 194 | databuf = *((uint *)buffer); |
| 195 | buffer += 4; |
| 196 | size -= 4; |
| 197 | irqstat = esdhc_read32(®s->irqstat); |
| 198 | out_le32(®s->datport, databuf); |
| 199 | } |
| 200 | blocks--; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | #endif |
| 205 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 206 | static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data) |
| 207 | { |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 208 | int timeout; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 209 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 210 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Eddy Petrișor | 5178dc1 | 2016-06-05 03:43:00 +0300 | [diff] [blame] | 211 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 212 | dma_addr_t addr; |
| 213 | #endif |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 214 | uint wml_value; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 215 | |
| 216 | wml_value = data->blocksize/4; |
| 217 | |
| 218 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 0244963 | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 219 | if (wml_value > WML_RD_WML_MAX) |
| 220 | wml_value = WML_RD_WML_MAX_VAL; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 221 | |
Roy Zang | e5853af | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 222 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 223 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eddy Petrișor | 5178dc1 | 2016-06-05 03:43:00 +0300 | [diff] [blame] | 224 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 225 | addr = virt_to_phys((void *)(data->dest)); |
| 226 | if (upper_32_bits(addr)) |
| 227 | printf("Error found for upper 32 bits\n"); |
| 228 | else |
| 229 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 230 | #else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 231 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 232 | #endif |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 233 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 234 | } else { |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 235 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 236 | flush_dcache_range((ulong)data->src, |
| 237 | (ulong)data->src+data->blocks |
| 238 | *data->blocksize); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 239 | #endif |
Priyanka Jain | 0244963 | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 240 | if (wml_value > WML_WR_WML_MAX) |
| 241 | wml_value = WML_WR_WML_MAX_VAL; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 242 | if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) { |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 243 | printf("\nThe SD card is locked. Can not write to a locked card.\n\n"); |
| 244 | return TIMEOUT; |
| 245 | } |
Roy Zang | e5853af | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 246 | |
| 247 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 248 | wml_value << 16); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 249 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eddy Petrișor | 5178dc1 | 2016-06-05 03:43:00 +0300 | [diff] [blame] | 250 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 251 | addr = virt_to_phys((void *)(data->src)); |
| 252 | if (upper_32_bits(addr)) |
| 253 | printf("Error found for upper 32 bits\n"); |
| 254 | else |
| 255 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 256 | #else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 257 | esdhc_write32(®s->dsaddr, (u32)data->src); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 258 | #endif |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 259 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 260 | } |
| 261 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 262 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 263 | |
| 264 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 265 | /* |
| 266 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 267 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 268 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 269 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 270 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 271 | * As 1) >= 2) |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 272 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 273 | * Taking log2 both the sides |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 274 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 275 | * Rounding up to next power of 2 |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 276 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 277 | * => timeout + 13 = fls(mmc->clock/4) |
Yangbo Lu | 9d7f321 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 278 | * |
| 279 | * However, the MMC spec "It is strongly recommended for hosts to |
| 280 | * implement more than 500ms timeout value even if the card |
| 281 | * indicates the 250ms maximum busy length." Even the previous |
| 282 | * value of 300ms is known to be insufficient for some cards. |
| 283 | * So, we use |
| 284 | * => timeout + 13 = fls(mmc->clock/2) |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 285 | */ |
Yangbo Lu | 9d7f321 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 286 | timeout = fls(mmc->clock/2); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 287 | timeout -= 13; |
| 288 | |
| 289 | if (timeout > 14) |
| 290 | timeout = 14; |
| 291 | |
| 292 | if (timeout < 0) |
| 293 | timeout = 0; |
| 294 | |
Kumar Gala | 9a878d5 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 295 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 296 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 297 | timeout++; |
| 298 | #endif |
| 299 | |
Haijun.Zhang | edeb83a | 2014-03-18 17:04:23 +0800 | [diff] [blame] | 300 | #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE |
| 301 | timeout = 0xE; |
| 302 | #endif |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 303 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 308 | static void check_and_invalidate_dcache_range |
| 309 | (struct mmc_cmd *cmd, |
| 310 | struct mmc_data *data) { |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 311 | unsigned start = 0; |
Yangbo Lu | e7702c6 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 312 | unsigned end = 0; |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 313 | unsigned size = roundup(ARCH_DMA_MINALIGN, |
| 314 | data->blocks*data->blocksize); |
Eddy Petrișor | 5178dc1 | 2016-06-05 03:43:00 +0300 | [diff] [blame] | 315 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 316 | dma_addr_t addr; |
| 317 | |
| 318 | addr = virt_to_phys((void *)(data->dest)); |
| 319 | if (upper_32_bits(addr)) |
| 320 | printf("Error found for upper 32 bits\n"); |
| 321 | else |
| 322 | start = lower_32_bits(addr); |
Yangbo Lu | e7702c6 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 323 | #else |
| 324 | start = (unsigned)data->dest; |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 325 | #endif |
Yangbo Lu | e7702c6 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 326 | end = start + size; |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 327 | invalidate_dcache_range(start, end); |
| 328 | } |
Tom Rini | 239dd25 | 2014-05-23 09:19:05 -0400 | [diff] [blame] | 329 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 330 | /* |
| 331 | * Sends a command out on the bus. Takes the mmc pointer, |
| 332 | * a command pointer, and an optional data pointer. |
| 333 | */ |
| 334 | static int |
| 335 | esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 336 | { |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 337 | int err = 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 338 | uint xfertyp; |
| 339 | uint irqstat; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 340 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 341 | struct fsl_esdhc *regs = priv->esdhc_regs; |
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) { |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 369 | err = esdhc_setup_data(mmc, data); |
| 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); |
Jason Liu | 9919d64 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 385 | #if defined(CONFIG_FSL_USDHC) |
| 386 | esdhc_write32(®s->mixctrl, |
Volodymyr Riazantsev | d251e11 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 387 | (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F) |
| 388 | | (mmc->ddr_mode ? XFERTYP_DDREN : 0)); |
Jason Liu | 9919d64 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 389 | esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000); |
| 390 | #else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 391 | esdhc_write32(®s->xfertyp, xfertyp); |
Jason Liu | 9919d64 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 392 | #endif |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 393 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 394 | /* Wait for the command to complete */ |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 395 | while (!(esdhc_read32(®s->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE))) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 396 | ; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 397 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 398 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 399 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 400 | if (irqstat & CMD_ERR) { |
| 401 | err = COMM_ERR; |
| 402 | goto out; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 405 | if (irqstat & IRQSTAT_CTOE) { |
| 406 | err = TIMEOUT; |
| 407 | goto out; |
| 408 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 409 | |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 410 | /* Switch voltage to 1.8V if CMD11 succeeded */ |
| 411 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) { |
| 412 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 413 | |
| 414 | printf("Run CMD11 1.8V switch\n"); |
| 415 | /* Sleep for 5 ms - max time for card to switch to 1.8V */ |
| 416 | udelay(5000); |
| 417 | } |
| 418 | |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 419 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 420 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
Yangbo Lu | 3ffa851 | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 421 | int timeout = 6000; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 422 | |
Yangbo Lu | 3ffa851 | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 423 | /* Poll on DATA0 line for cmd with busy signal for 600 ms */ |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 424 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 425 | PRSSTAT_DAT0)) { |
| 426 | udelay(100); |
| 427 | timeout--; |
| 428 | } |
| 429 | |
| 430 | if (timeout <= 0) { |
| 431 | printf("Timeout waiting for DAT0 to go high!\n"); |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 432 | err = TIMEOUT; |
| 433 | goto out; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 437 | /* Copy the response to the response buffer */ |
| 438 | if (cmd->resp_type & MMC_RSP_136) { |
| 439 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 440 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 441 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 442 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 443 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 444 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 445 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 446 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 447 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 448 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 449 | } else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 450 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 451 | |
| 452 | /* Wait until all of the blocks are transferred */ |
| 453 | if (data) { |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 454 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 455 | esdhc_pio_read_write(mmc, data); |
| 456 | #else |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 457 | do { |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 458 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 459 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 460 | if (irqstat & IRQSTAT_DTOE) { |
| 461 | err = TIMEOUT; |
| 462 | goto out; |
| 463 | } |
Frans Meulenbroeks | 010ba98 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 464 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 465 | if (irqstat & DATA_ERR) { |
| 466 | err = COMM_ERR; |
| 467 | goto out; |
| 468 | } |
Andrew Gabbasov | 4a92962 | 2013-04-07 23:06:08 +0000 | [diff] [blame] | 469 | } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 470 | |
Peng Fan | 9cb5e99 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 471 | /* |
| 472 | * Need invalidate the dcache here again to avoid any |
| 473 | * cache-fill during the DMA operations such as the |
| 474 | * speculative pre-fetching etc. |
| 475 | */ |
Eric Nelson | 70e6869 | 2013-04-03 12:31:56 +0000 | [diff] [blame] | 476 | if (data->flags & MMC_DATA_READ) |
| 477 | check_and_invalidate_dcache_range(cmd, data); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 478 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 479 | } |
| 480 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 481 | out: |
| 482 | /* Reset CMD and DATA portions on error */ |
| 483 | if (err) { |
| 484 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 485 | SYSCTL_RSTC); |
| 486 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 487 | ; |
| 488 | |
| 489 | if (data) { |
| 490 | esdhc_write32(®s->sysctl, |
| 491 | esdhc_read32(®s->sysctl) | |
| 492 | SYSCTL_RSTD); |
| 493 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 494 | ; |
| 495 | } |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 496 | |
| 497 | /* If this was CMD11, then notify that power cycle is needed */ |
| 498 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) |
| 499 | printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n"); |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 500 | } |
| 501 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 502 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 503 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 504 | return err; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 505 | } |
| 506 | |
Kim Phillips | f9e0b60 | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 507 | static void set_sysctl(struct mmc *mmc, uint clock) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 508 | { |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 509 | int div, pre_div; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 510 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 511 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 512 | int sdhc_clk = priv->sdhc_clk; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 513 | uint clk; |
| 514 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 515 | if (clock < mmc->cfg->f_min) |
| 516 | clock = mmc->cfg->f_min; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 517 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 518 | if (sdhc_clk / 16 > clock) { |
| 519 | for (pre_div = 2; pre_div < 256; pre_div *= 2) |
| 520 | if ((sdhc_clk / pre_div) <= (clock * 16)) |
| 521 | break; |
| 522 | } else |
| 523 | pre_div = 2; |
| 524 | |
| 525 | for (div = 1; div <= 16; div++) |
| 526 | if ((sdhc_clk / (div * pre_div)) <= clock) |
| 527 | break; |
| 528 | |
Volodymyr Riazantsev | d251e11 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 529 | pre_div >>= mmc->ddr_mode ? 2 : 1; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 530 | div -= 1; |
| 531 | |
| 532 | clk = (pre_div << 8) | (div << 4); |
| 533 | |
Eric Nelson | c8e615c | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 534 | #ifdef CONFIG_FSL_USDHC |
| 535 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
| 536 | #else |
Kumar Gala | 09876a3 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 537 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Eric Nelson | c8e615c | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 538 | #endif |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 539 | |
| 540 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 541 | |
| 542 | udelay(10000); |
| 543 | |
Eric Nelson | c8e615c | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 544 | #ifdef CONFIG_FSL_USDHC |
| 545 | esdhc_clrbits32(®s->sysctl, SYSCTL_RSTA); |
| 546 | #else |
| 547 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
| 548 | #endif |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 549 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 550 | } |
| 551 | |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 552 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 553 | static void esdhc_clock_control(struct mmc *mmc, bool enable) |
| 554 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 555 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 556 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 557 | u32 value; |
| 558 | u32 time_out; |
| 559 | |
| 560 | value = esdhc_read32(®s->sysctl); |
| 561 | |
| 562 | if (enable) |
| 563 | value |= SYSCTL_CKEN; |
| 564 | else |
| 565 | value &= ~SYSCTL_CKEN; |
| 566 | |
| 567 | esdhc_write32(®s->sysctl, value); |
| 568 | |
| 569 | time_out = 20; |
| 570 | value = PRSSTAT_SDSTB; |
| 571 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 572 | if (time_out == 0) { |
| 573 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 574 | break; |
| 575 | } |
| 576 | time_out--; |
| 577 | mdelay(1); |
| 578 | } |
| 579 | } |
| 580 | #endif |
| 581 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 582 | static void esdhc_set_ios(struct mmc *mmc) |
| 583 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 584 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 585 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 586 | |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 587 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 588 | /* Select to use peripheral clock */ |
| 589 | esdhc_clock_control(mmc, false); |
| 590 | esdhc_setbits32(®s->scr, ESDHCCTL_PCS); |
| 591 | esdhc_clock_control(mmc, true); |
| 592 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 593 | /* Set the clock speed */ |
| 594 | set_sysctl(mmc, mmc->clock); |
| 595 | |
| 596 | /* Set the bus width */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 597 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 598 | |
| 599 | if (mmc->bus_width == 4) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 600 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 601 | else if (mmc->bus_width == 8) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 602 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 603 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static int esdhc_init(struct mmc *mmc) |
| 607 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 608 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 609 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 610 | int timeout = 1000; |
| 611 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 612 | /* Reset the entire host controller */ |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 613 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 614 | |
| 615 | /* Wait until the controller is available */ |
| 616 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 617 | udelay(1000); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 618 | |
Benoît Thébaudeau | c08d11c | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 619 | #ifndef ARCH_MXC |
P.V.Suresh | 7b1868b | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 620 | /* Enable cache snooping */ |
Benoît Thébaudeau | c08d11c | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 621 | esdhc_write32(®s->scr, 0x00000040); |
| 622 | #endif |
P.V.Suresh | 7b1868b | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 623 | |
Eric Nelson | c8e615c | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 624 | #ifndef CONFIG_FSL_USDHC |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 625 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Eric Nelson | c8e615c | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 626 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 627 | |
| 628 | /* Set the initial clock speed */ |
Jerry Huang | 0caea1a | 2010-11-25 17:06:07 +0000 | [diff] [blame] | 629 | mmc_set_clock(mmc, 400000); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 630 | |
| 631 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 632 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 633 | |
| 634 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 635 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 636 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 637 | /* Set timout to the maximum value */ |
| 638 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 639 | |
Otavio Salvador | 12b2a87 | 2015-02-17 10:42:44 -0200 | [diff] [blame] | 640 | #ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT |
| 641 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 642 | #endif |
| 643 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static int esdhc_getcd(struct mmc *mmc) |
| 648 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 649 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 650 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 651 | int timeout = 1000; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 652 | |
Haijun.Zhang | 05f5854 | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 653 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 654 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 655 | return 1; |
| 656 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 657 | |
| 658 | #ifdef CONFIG_DM_MMC |
| 659 | if (priv->non_removable) |
| 660 | return 1; |
| 661 | |
| 662 | if (dm_gpio_is_valid(&priv->cd_gpio)) |
| 663 | return dm_gpio_get_value(&priv->cd_gpio); |
| 664 | #endif |
| 665 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 666 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 667 | udelay(1000); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 668 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 669 | return timeout > 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 670 | } |
| 671 | |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 672 | static void esdhc_reset(struct fsl_esdhc *regs) |
| 673 | { |
| 674 | unsigned long timeout = 100; /* wait max 100 ms */ |
| 675 | |
| 676 | /* reset the controller */ |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 677 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 678 | |
| 679 | /* hardware clears the bit when it is done */ |
| 680 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 681 | udelay(1000); |
| 682 | if (!timeout) |
| 683 | printf("MMC/SD: Reset never completed.\n"); |
| 684 | } |
| 685 | |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 686 | static const struct mmc_ops esdhc_ops = { |
| 687 | .send_cmd = esdhc_send_cmd, |
| 688 | .set_ios = esdhc_set_ios, |
| 689 | .init = esdhc_init, |
| 690 | .getcd = esdhc_getcd, |
| 691 | }; |
| 692 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 693 | static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg, |
| 694 | struct fsl_esdhc_priv *priv) |
| 695 | { |
| 696 | if (!cfg || !priv) |
| 697 | return -EINVAL; |
| 698 | |
| 699 | priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); |
| 700 | priv->bus_width = cfg->max_bus_width; |
| 701 | priv->sdhc_clk = cfg->sdhc_clk; |
| 702 | |
| 703 | return 0; |
| 704 | }; |
| 705 | |
| 706 | static int fsl_esdhc_init(struct fsl_esdhc_priv *priv) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 707 | { |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 708 | struct fsl_esdhc *regs; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 709 | struct mmc *mmc; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 710 | u32 caps, voltage_caps; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 711 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 712 | if (!priv) |
| 713 | return -EINVAL; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 714 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 715 | regs = priv->esdhc_regs; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 716 | |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 717 | /* First reset the eSDHC controller */ |
| 718 | esdhc_reset(regs); |
| 719 | |
Eric Nelson | c8e615c | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 720 | #ifndef CONFIG_FSL_USDHC |
Jerry Huang | 4e3bfa0 | 2012-05-17 23:57:02 +0000 | [diff] [blame] | 721 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
| 722 | | SYSCTL_IPGEN | SYSCTL_CKEN); |
Eric Nelson | c8e615c | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 723 | #endif |
Jerry Huang | 4e3bfa0 | 2012-05-17 23:57:02 +0000 | [diff] [blame] | 724 | |
Ye.Li | 3d46c31 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 725 | writel(SDHCI_IRQ_EN_BITS, ®s->irqstaten); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 726 | memset(&priv->cfg, 0, sizeof(priv->cfg)); |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 727 | |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 728 | voltage_caps = 0; |
Wang Huan | c929213 | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 729 | caps = esdhc_read32(®s->hostcapblt); |
Roy Zang | 3935661 | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 730 | |
| 731 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
| 732 | caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | |
| 733 | ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); |
| 734 | #endif |
Haijun.Zhang | 8a065e9 | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 735 | |
| 736 | /* T4240 host controller capabilities register should have VS33 bit */ |
| 737 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
| 738 | caps = caps | ESDHC_HOSTCAPBLT_VS33; |
| 739 | #endif |
| 740 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 741 | if (caps & ESDHC_HOSTCAPBLT_VS18) |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 742 | voltage_caps |= MMC_VDD_165_195; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 743 | if (caps & ESDHC_HOSTCAPBLT_VS30) |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 744 | voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 745 | if (caps & ESDHC_HOSTCAPBLT_VS33) |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 746 | voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; |
| 747 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 748 | priv->cfg.name = "FSL_SDHC"; |
| 749 | priv->cfg.ops = &esdhc_ops; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 750 | #ifdef CONFIG_SYS_SD_VOLTAGE |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 751 | priv->cfg.voltages = CONFIG_SYS_SD_VOLTAGE; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 752 | #else |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 753 | priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 754 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 755 | if ((priv->cfg.voltages & voltage_caps) == 0) { |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 756 | printf("voltage not supported by controller\n"); |
| 757 | return -1; |
| 758 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 759 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 760 | if (priv->bus_width == 8) |
| 761 | priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
| 762 | else if (priv->bus_width == 4) |
| 763 | priv->cfg.host_caps = MMC_MODE_4BIT; |
| 764 | |
| 765 | priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
Volodymyr Riazantsev | d251e11 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 766 | #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 767 | priv->cfg.host_caps |= MMC_MODE_DDR_52MHz; |
Volodymyr Riazantsev | d251e11 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 768 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 769 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 770 | if (priv->bus_width > 0) { |
| 771 | if (priv->bus_width < 8) |
| 772 | priv->cfg.host_caps &= ~MMC_MODE_8BIT; |
| 773 | if (priv->bus_width < 4) |
| 774 | priv->cfg.host_caps &= ~MMC_MODE_4BIT; |
Abbas Raza | e6bf977 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 777 | if (caps & ESDHC_HOSTCAPBLT_HSS) |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 778 | priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 779 | |
Haijun.Zhang | f0fe8ad | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 780 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 781 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 782 | priv->cfg.host_caps &= ~MMC_MODE_8BIT; |
Haijun.Zhang | f0fe8ad | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 783 | #endif |
| 784 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 785 | priv->cfg.f_min = 400000; |
| 786 | priv->cfg.f_max = min(priv->sdhc_clk, (u32)52000000); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 787 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 788 | priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 789 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 790 | mmc = mmc_create(&priv->cfg, priv); |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 791 | if (mmc == NULL) |
| 792 | return -1; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 793 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 794 | priv->mmc = mmc; |
| 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
| 800 | { |
| 801 | struct fsl_esdhc_priv *priv; |
| 802 | int ret; |
| 803 | |
| 804 | if (!cfg) |
| 805 | return -EINVAL; |
| 806 | |
| 807 | priv = calloc(sizeof(struct fsl_esdhc_priv), 1); |
| 808 | if (!priv) |
| 809 | return -ENOMEM; |
| 810 | |
| 811 | ret = fsl_esdhc_cfg_to_priv(cfg, priv); |
| 812 | if (ret) { |
| 813 | debug("%s xlate failure\n", __func__); |
| 814 | free(priv); |
| 815 | return ret; |
| 816 | } |
| 817 | |
| 818 | ret = fsl_esdhc_init(priv); |
| 819 | if (ret) { |
| 820 | debug("%s init failure\n", __func__); |
| 821 | free(priv); |
| 822 | return ret; |
| 823 | } |
| 824 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 829 | { |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 830 | struct fsl_esdhc_cfg *cfg; |
| 831 | |
Fabio Estevam | 6592a99 | 2012-12-27 08:51:08 +0000 | [diff] [blame] | 832 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 833 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
Simon Glass | 9e247d1 | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 834 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 835 | return fsl_esdhc_initialize(bis, cfg); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 836 | } |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 837 | |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 838 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 839 | void mmc_adapter_card_type_ident(void) |
| 840 | { |
| 841 | u8 card_id; |
| 842 | u8 value; |
| 843 | |
| 844 | card_id = QIXIS_READ(present) & QIXIS_SDID_MASK; |
| 845 | gd->arch.sdhc_adapter = card_id; |
| 846 | |
| 847 | switch (card_id) { |
| 848 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45: |
Yangbo Lu | 81eacd6 | 2015-09-17 10:27:12 +0800 | [diff] [blame] | 849 | value = QIXIS_READ(brdcfg[5]); |
| 850 | value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7); |
| 851 | QIXIS_WRITE(brdcfg[5], value); |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 852 | break; |
| 853 | case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY: |
Yangbo Lu | c6799ce | 2015-09-17 10:27:48 +0800 | [diff] [blame] | 854 | value = QIXIS_READ(pwr_ctl[1]); |
| 855 | value |= QIXIS_EVDD_BY_SDHC_VS; |
| 856 | QIXIS_WRITE(pwr_ctl[1], value); |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 857 | break; |
| 858 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44: |
| 859 | value = QIXIS_READ(brdcfg[5]); |
| 860 | value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT); |
| 861 | QIXIS_WRITE(brdcfg[5], value); |
| 862 | break; |
| 863 | case QIXIS_ESDHC_ADAPTER_TYPE_RSV: |
| 864 | break; |
| 865 | case QIXIS_ESDHC_ADAPTER_TYPE_MMC: |
| 866 | break; |
| 867 | case QIXIS_ESDHC_ADAPTER_TYPE_SD: |
| 868 | break; |
| 869 | case QIXIS_ESDHC_NO_ADAPTER: |
| 870 | break; |
| 871 | default: |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | #endif |
| 876 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 877 | #ifdef CONFIG_OF_LIBFDT |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 878 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 879 | { |
| 880 | const char *compat = "fsl,esdhc"; |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 881 | |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 882 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 883 | if (!hwconfig("esdhc")) { |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 884 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 885 | 8 + 1, 1); |
| 886 | return; |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 887 | } |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 888 | #endif |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 889 | |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 890 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 891 | do_fixup_by_compat_u32(blob, compat, "peripheral-frequency", |
| 892 | gd->arch.sdhc_clk, 1); |
| 893 | #else |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 894 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | 9e247d1 | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 895 | gd->arch.sdhc_clk, 1); |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 896 | #endif |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 897 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 898 | do_fixup_by_compat_u32(blob, compat, "adapter-type", |
| 899 | (u32)(gd->arch.sdhc_adapter), 1); |
| 900 | #endif |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 901 | do_fixup_by_compat(blob, compat, "status", "okay", |
| 902 | 4 + 1, 1); |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 903 | } |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 904 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 905 | |
| 906 | #ifdef CONFIG_DM_MMC |
| 907 | #include <asm/arch/clock.h> |
| 908 | static int fsl_esdhc_probe(struct udevice *dev) |
| 909 | { |
| 910 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 911 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 912 | const void *fdt = gd->fdt_blob; |
| 913 | int node = dev->of_offset; |
| 914 | fdt_addr_t addr; |
| 915 | unsigned int val; |
| 916 | int ret; |
| 917 | |
| 918 | addr = dev_get_addr(dev); |
| 919 | if (addr == FDT_ADDR_T_NONE) |
| 920 | return -EINVAL; |
| 921 | |
| 922 | priv->esdhc_regs = (struct fsl_esdhc *)addr; |
| 923 | priv->dev = dev; |
| 924 | |
| 925 | val = fdtdec_get_int(fdt, node, "bus-width", -1); |
| 926 | if (val == 8) |
| 927 | priv->bus_width = 8; |
| 928 | else if (val == 4) |
| 929 | priv->bus_width = 4; |
| 930 | else |
| 931 | priv->bus_width = 1; |
| 932 | |
| 933 | if (fdt_get_property(fdt, node, "non-removable", NULL)) { |
| 934 | priv->non_removable = 1; |
| 935 | } else { |
| 936 | priv->non_removable = 0; |
| 937 | gpio_request_by_name_nodev(fdt, node, "cd-gpios", 0, |
| 938 | &priv->cd_gpio, GPIOD_IS_IN); |
| 939 | } |
| 940 | |
| 941 | /* |
| 942 | * TODO: |
| 943 | * Because lack of clk driver, if SDHC clk is not enabled, |
| 944 | * need to enable it first before this driver is invoked. |
| 945 | * |
| 946 | * we use MXC_ESDHC_CLK to get clk freq. |
| 947 | * If one would like to make this function work, |
| 948 | * the aliases should be provided in dts as this: |
| 949 | * |
| 950 | * aliases { |
| 951 | * mmc0 = &usdhc1; |
| 952 | * mmc1 = &usdhc2; |
| 953 | * mmc2 = &usdhc3; |
| 954 | * mmc3 = &usdhc4; |
| 955 | * }; |
| 956 | * Then if your board only supports mmc2 and mmc3, but we can |
| 957 | * correctly get the seq as 2 and 3, then let mxc_get_clock |
| 958 | * work as expected. |
| 959 | */ |
| 960 | priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq); |
| 961 | if (priv->sdhc_clk <= 0) { |
| 962 | dev_err(dev, "Unable to get clk for %s\n", dev->name); |
| 963 | return -EINVAL; |
| 964 | } |
| 965 | |
| 966 | ret = fsl_esdhc_init(priv); |
| 967 | if (ret) { |
| 968 | dev_err(dev, "fsl_esdhc_init failure\n"); |
| 969 | return ret; |
| 970 | } |
| 971 | |
| 972 | upriv->mmc = priv->mmc; |
| 973 | |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | static const struct udevice_id fsl_esdhc_ids[] = { |
| 978 | { .compatible = "fsl,imx6ul-usdhc", }, |
| 979 | { .compatible = "fsl,imx6sx-usdhc", }, |
| 980 | { .compatible = "fsl,imx6sl-usdhc", }, |
| 981 | { .compatible = "fsl,imx6q-usdhc", }, |
| 982 | { .compatible = "fsl,imx7d-usdhc", }, |
| 983 | { /* sentinel */ } |
| 984 | }; |
| 985 | |
| 986 | U_BOOT_DRIVER(fsl_esdhc) = { |
| 987 | .name = "fsl-esdhc-mmc", |
| 988 | .id = UCLASS_MMC, |
| 989 | .of_match = fsl_esdhc_ids, |
| 990 | .probe = fsl_esdhc_probe, |
| 991 | .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv), |
| 992 | }; |
| 993 | #endif |