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> |
| 23 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
Ye.Li | 3d46c31 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 26 | #define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \ |
| 27 | IRQSTATEN_CINT | \ |
| 28 | IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \ |
| 29 | IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \ |
| 30 | IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \ |
| 31 | IRQSTATEN_DINT) |
| 32 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 33 | struct fsl_esdhc { |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 34 | uint dsaddr; /* SDMA system address register */ |
| 35 | uint blkattr; /* Block attributes register */ |
| 36 | uint cmdarg; /* Command argument register */ |
| 37 | uint xfertyp; /* Transfer type register */ |
| 38 | uint cmdrsp0; /* Command response 0 register */ |
| 39 | uint cmdrsp1; /* Command response 1 register */ |
| 40 | uint cmdrsp2; /* Command response 2 register */ |
| 41 | uint cmdrsp3; /* Command response 3 register */ |
| 42 | uint datport; /* Buffer data port register */ |
| 43 | uint prsstat; /* Present state register */ |
| 44 | uint proctl; /* Protocol control register */ |
| 45 | uint sysctl; /* System Control Register */ |
| 46 | uint irqstat; /* Interrupt status register */ |
| 47 | uint irqstaten; /* Interrupt status enable register */ |
| 48 | uint irqsigen; /* Interrupt signal enable register */ |
| 49 | uint autoc12err; /* Auto CMD error status register */ |
| 50 | uint hostcapblt; /* Host controller capabilities register */ |
| 51 | uint wml; /* Watermark level register */ |
| 52 | uint mixctrl; /* For USDHC */ |
| 53 | char reserved1[4]; /* reserved */ |
| 54 | uint fevt; /* Force event register */ |
| 55 | uint admaes; /* ADMA error status register */ |
| 56 | uint adsaddr; /* ADMA system address register */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 57 | char reserved2[100]; /* reserved */ |
| 58 | uint vendorspec; /* Vendor Specific register */ |
Peng Fan | a4f9c6b | 2015-03-10 15:35:46 +0800 | [diff] [blame] | 59 | char reserved3[56]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 60 | uint hostver; /* Host controller version register */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 61 | char reserved4[4]; /* reserved */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 62 | uint dmaerraddr; /* DMA error address register */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 63 | char reserved5[4]; /* reserved */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 64 | uint dmaerrattr; /* DMA error attribute register */ |
| 65 | char reserved6[4]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 66 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 67 | char reserved7[8]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 68 | uint tcr; /* Tuning control register */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 69 | char reserved8[28]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 70 | uint sddirctl; /* SD direction control register */ |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 71 | char reserved9[712]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 72 | uint scr; /* eSDHC control register */ |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /* Return the XFERTYP flags for a given command and data packet */ |
Kim Phillips | f9e0b60 | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 76 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 77 | { |
| 78 | uint xfertyp = 0; |
| 79 | |
| 80 | if (data) { |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 81 | xfertyp |= XFERTYP_DPSEL; |
| 82 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 83 | xfertyp |= XFERTYP_DMAEN; |
| 84 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 85 | if (data->blocks > 1) { |
| 86 | xfertyp |= XFERTYP_MSBSEL; |
| 87 | xfertyp |= XFERTYP_BCEN; |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 88 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 89 | xfertyp |= XFERTYP_AC12EN; |
| 90 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | if (data->flags & MMC_DATA_READ) |
| 94 | xfertyp |= XFERTYP_DTDSEL; |
| 95 | } |
| 96 | |
| 97 | if (cmd->resp_type & MMC_RSP_CRC) |
| 98 | xfertyp |= XFERTYP_CCCEN; |
| 99 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 100 | xfertyp |= XFERTYP_CICEN; |
| 101 | if (cmd->resp_type & MMC_RSP_136) |
| 102 | xfertyp |= XFERTYP_RSPTYP_136; |
| 103 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 104 | xfertyp |= XFERTYP_RSPTYP_48_BUSY; |
| 105 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 106 | xfertyp |= XFERTYP_RSPTYP_48; |
| 107 | |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 108 | #if defined(CONFIG_MX53) || defined(CONFIG_PPC_T4240) || \ |
| 109 | defined(CONFIG_LS102XA) || defined(CONFIG_LS2085A) |
Jason Liu | bef0ff0 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 110 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 111 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
| 112 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 113 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 114 | } |
| 115 | |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 116 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 117 | /* |
| 118 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 119 | */ |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 120 | static void |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 121 | esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data) |
| 122 | { |
Ira Snyder | 66a722e | 2011-12-23 08:30:40 +0000 | [diff] [blame] | 123 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
| 124 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 125 | uint blocks; |
| 126 | char *buffer; |
| 127 | uint databuf; |
| 128 | uint size; |
| 129 | uint irqstat; |
| 130 | uint timeout; |
| 131 | |
| 132 | if (data->flags & MMC_DATA_READ) { |
| 133 | blocks = data->blocks; |
| 134 | buffer = data->dest; |
| 135 | while (blocks) { |
| 136 | timeout = PIO_TIMEOUT; |
| 137 | size = data->blocksize; |
| 138 | irqstat = esdhc_read32(®s->irqstat); |
| 139 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN) |
| 140 | && --timeout); |
| 141 | if (timeout <= 0) { |
| 142 | printf("\nData Read Failed in PIO Mode."); |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 143 | return; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 144 | } |
| 145 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 146 | udelay(100); /* Wait before last byte transfer complete */ |
| 147 | irqstat = esdhc_read32(®s->irqstat); |
| 148 | databuf = in_le32(®s->datport); |
| 149 | *((uint *)buffer) = databuf; |
| 150 | buffer += 4; |
| 151 | size -= 4; |
| 152 | } |
| 153 | blocks--; |
| 154 | } |
| 155 | } else { |
| 156 | blocks = data->blocks; |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 157 | buffer = (char *)data->src; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 158 | while (blocks) { |
| 159 | timeout = PIO_TIMEOUT; |
| 160 | size = data->blocksize; |
| 161 | irqstat = esdhc_read32(®s->irqstat); |
| 162 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN) |
| 163 | && --timeout); |
| 164 | if (timeout <= 0) { |
| 165 | printf("\nData Write Failed in PIO Mode."); |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 166 | return; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 167 | } |
| 168 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 169 | udelay(100); /* Wait before last byte transfer complete */ |
| 170 | databuf = *((uint *)buffer); |
| 171 | buffer += 4; |
| 172 | size -= 4; |
| 173 | irqstat = esdhc_read32(®s->irqstat); |
| 174 | out_le32(®s->datport, databuf); |
| 175 | } |
| 176 | blocks--; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | #endif |
| 181 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 182 | static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data) |
| 183 | { |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 184 | int timeout; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 185 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 186 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 187 | #ifdef CONFIG_LS2085A |
| 188 | dma_addr_t addr; |
| 189 | #endif |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 190 | uint wml_value; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 191 | |
| 192 | wml_value = data->blocksize/4; |
| 193 | |
| 194 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 0244963 | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 195 | if (wml_value > WML_RD_WML_MAX) |
| 196 | wml_value = WML_RD_WML_MAX_VAL; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 197 | |
Roy Zang | e5853af | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 198 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 199 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 200 | #ifdef CONFIG_LS2085A |
| 201 | addr = virt_to_phys((void *)(data->dest)); |
| 202 | if (upper_32_bits(addr)) |
| 203 | printf("Error found for upper 32 bits\n"); |
| 204 | else |
| 205 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 206 | #else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 207 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 208 | #endif |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 209 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 210 | } else { |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 211 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 212 | flush_dcache_range((ulong)data->src, |
| 213 | (ulong)data->src+data->blocks |
| 214 | *data->blocksize); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 215 | #endif |
Priyanka Jain | 0244963 | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 216 | if (wml_value > WML_WR_WML_MAX) |
| 217 | wml_value = WML_WR_WML_MAX_VAL; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 218 | if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) { |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 219 | printf("\nThe SD card is locked. Can not write to a locked card.\n\n"); |
| 220 | return TIMEOUT; |
| 221 | } |
Roy Zang | e5853af | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 222 | |
| 223 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 224 | wml_value << 16); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 225 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 226 | #ifdef CONFIG_LS2085A |
| 227 | addr = virt_to_phys((void *)(data->src)); |
| 228 | if (upper_32_bits(addr)) |
| 229 | printf("Error found for upper 32 bits\n"); |
| 230 | else |
| 231 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 232 | #else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 233 | esdhc_write32(®s->dsaddr, (u32)data->src); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 234 | #endif |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 235 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 236 | } |
| 237 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 238 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 239 | |
| 240 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 241 | /* |
| 242 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 243 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 244 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 245 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 246 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 247 | * As 1) >= 2) |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 248 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 249 | * Taking log2 both the sides |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 250 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 251 | * Rounding up to next power of 2 |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 252 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 253 | * => timeout + 13 = fls(mmc->clock/4) |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 254 | */ |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 255 | timeout = fls(mmc->clock/4); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 256 | timeout -= 13; |
| 257 | |
| 258 | if (timeout > 14) |
| 259 | timeout = 14; |
| 260 | |
| 261 | if (timeout < 0) |
| 262 | timeout = 0; |
| 263 | |
Kumar Gala | 9a878d5 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 264 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 265 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 266 | timeout++; |
| 267 | #endif |
| 268 | |
Haijun.Zhang | edeb83a | 2014-03-18 17:04:23 +0800 | [diff] [blame] | 269 | #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE |
| 270 | timeout = 0xE; |
| 271 | #endif |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 272 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Tom Rini | 239dd25 | 2014-05-23 09:19:05 -0400 | [diff] [blame] | 277 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 278 | static void check_and_invalidate_dcache_range |
| 279 | (struct mmc_cmd *cmd, |
| 280 | struct mmc_data *data) { |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 281 | #ifdef CONFIG_LS2085A |
| 282 | unsigned start = 0; |
| 283 | #else |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 284 | unsigned start = (unsigned)data->dest ; |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 285 | #endif |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 286 | unsigned size = roundup(ARCH_DMA_MINALIGN, |
| 287 | data->blocks*data->blocksize); |
| 288 | unsigned end = start+size ; |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 289 | #ifdef CONFIG_LS2085A |
| 290 | dma_addr_t addr; |
| 291 | |
| 292 | addr = virt_to_phys((void *)(data->dest)); |
| 293 | if (upper_32_bits(addr)) |
| 294 | printf("Error found for upper 32 bits\n"); |
| 295 | else |
| 296 | start = lower_32_bits(addr); |
| 297 | #endif |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 298 | invalidate_dcache_range(start, end); |
| 299 | } |
Tom Rini | 239dd25 | 2014-05-23 09:19:05 -0400 | [diff] [blame] | 300 | #endif |
| 301 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 302 | /* |
| 303 | * Sends a command out on the bus. Takes the mmc pointer, |
| 304 | * a command pointer, and an optional data pointer. |
| 305 | */ |
| 306 | static int |
| 307 | esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 308 | { |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 309 | int err = 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 310 | uint xfertyp; |
| 311 | uint irqstat; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 312 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 313 | volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 314 | |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 315 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 316 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 317 | return 0; |
| 318 | #endif |
| 319 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 320 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 321 | |
| 322 | sync(); |
| 323 | |
| 324 | /* Wait for the bus to be idle */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 325 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 326 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 327 | ; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 328 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 329 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 330 | ; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 331 | |
| 332 | /* Wait at least 8 SD clock cycles before the next command */ |
| 333 | /* |
| 334 | * Note: This is way more than 8 cycles, but 1ms seems to |
| 335 | * resolve timing issues with some cards |
| 336 | */ |
| 337 | udelay(1000); |
| 338 | |
| 339 | /* Set up for a data transfer if we have one */ |
| 340 | if (data) { |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 341 | err = esdhc_setup_data(mmc, data); |
| 342 | if(err) |
| 343 | return err; |
| 344 | } |
| 345 | |
| 346 | /* Figure out the transfer arguments */ |
| 347 | xfertyp = esdhc_xfertyp(cmd, data); |
| 348 | |
Andrew Gabbasov | 4816b7a | 2013-06-11 10:34:22 -0500 | [diff] [blame] | 349 | /* Mask all irqs */ |
| 350 | esdhc_write32(®s->irqsigen, 0); |
| 351 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 352 | /* Send the command */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 353 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
Jason Liu | 9919d64 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 354 | #if defined(CONFIG_FSL_USDHC) |
| 355 | esdhc_write32(®s->mixctrl, |
Volodymyr Riazantsev | d251e11 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 356 | (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F) |
| 357 | | (mmc->ddr_mode ? XFERTYP_DDREN : 0)); |
Jason Liu | 9919d64 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 358 | esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000); |
| 359 | #else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 360 | esdhc_write32(®s->xfertyp, xfertyp); |
Jason Liu | 9919d64 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 361 | #endif |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 362 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 363 | /* Wait for the command to complete */ |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 364 | while (!(esdhc_read32(®s->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE))) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 365 | ; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 366 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 367 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 368 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 369 | if (irqstat & CMD_ERR) { |
| 370 | err = COMM_ERR; |
| 371 | goto out; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 374 | if (irqstat & IRQSTAT_CTOE) { |
| 375 | err = TIMEOUT; |
| 376 | goto out; |
| 377 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 378 | |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 379 | /* Switch voltage to 1.8V if CMD11 succeeded */ |
| 380 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) { |
| 381 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 382 | |
| 383 | printf("Run CMD11 1.8V switch\n"); |
| 384 | /* Sleep for 5 ms - max time for card to switch to 1.8V */ |
| 385 | udelay(5000); |
| 386 | } |
| 387 | |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 388 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 389 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
Yangbo Lu | 3ffa851 | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 390 | int timeout = 6000; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 391 | |
Yangbo Lu | 3ffa851 | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 392 | /* Poll on DATA0 line for cmd with busy signal for 600 ms */ |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 393 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 394 | PRSSTAT_DAT0)) { |
| 395 | udelay(100); |
| 396 | timeout--; |
| 397 | } |
| 398 | |
| 399 | if (timeout <= 0) { |
| 400 | printf("Timeout waiting for DAT0 to go high!\n"); |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 401 | err = TIMEOUT; |
| 402 | goto out; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 406 | /* Copy the response to the response buffer */ |
| 407 | if (cmd->resp_type & MMC_RSP_136) { |
| 408 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 409 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 410 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 411 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 412 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 413 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 414 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 415 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 416 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 417 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 418 | } else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 419 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 420 | |
| 421 | /* Wait until all of the blocks are transferred */ |
| 422 | if (data) { |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 423 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 424 | esdhc_pio_read_write(mmc, data); |
| 425 | #else |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 426 | do { |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 427 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 428 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 429 | if (irqstat & IRQSTAT_DTOE) { |
| 430 | err = TIMEOUT; |
| 431 | goto out; |
| 432 | } |
Frans Meulenbroeks | 010ba98 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 433 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 434 | if (irqstat & DATA_ERR) { |
| 435 | err = COMM_ERR; |
| 436 | goto out; |
| 437 | } |
Andrew Gabbasov | 4a92962 | 2013-04-07 23:06:08 +0000 | [diff] [blame] | 438 | } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 439 | |
Eric Nelson | 70e6869 | 2013-04-03 12:31:56 +0000 | [diff] [blame] | 440 | if (data->flags & MMC_DATA_READ) |
| 441 | check_and_invalidate_dcache_range(cmd, data); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 442 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 443 | } |
| 444 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 445 | out: |
| 446 | /* Reset CMD and DATA portions on error */ |
| 447 | if (err) { |
| 448 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 449 | SYSCTL_RSTC); |
| 450 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 451 | ; |
| 452 | |
| 453 | if (data) { |
| 454 | esdhc_write32(®s->sysctl, |
| 455 | esdhc_read32(®s->sysctl) | |
| 456 | SYSCTL_RSTD); |
| 457 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 458 | ; |
| 459 | } |
Otavio Salvador | fad3e06 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 460 | |
| 461 | /* If this was CMD11, then notify that power cycle is needed */ |
| 462 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) |
| 463 | 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] | 464 | } |
| 465 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 466 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 467 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 468 | return err; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 469 | } |
| 470 | |
Kim Phillips | f9e0b60 | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 471 | static void set_sysctl(struct mmc *mmc, uint clock) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 472 | { |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 473 | int div, pre_div; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 474 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 475 | volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Benoît Thébaudeau | c58ff34 | 2012-10-01 08:36:25 +0000 | [diff] [blame] | 476 | int sdhc_clk = cfg->sdhc_clk; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 477 | uint clk; |
| 478 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 479 | if (clock < mmc->cfg->f_min) |
| 480 | clock = mmc->cfg->f_min; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 481 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 482 | if (sdhc_clk / 16 > clock) { |
| 483 | for (pre_div = 2; pre_div < 256; pre_div *= 2) |
| 484 | if ((sdhc_clk / pre_div) <= (clock * 16)) |
| 485 | break; |
| 486 | } else |
| 487 | pre_div = 2; |
| 488 | |
| 489 | for (div = 1; div <= 16; div++) |
| 490 | if ((sdhc_clk / (div * pre_div)) <= clock) |
| 491 | break; |
| 492 | |
Volodymyr Riazantsev | d251e11 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 493 | pre_div >>= mmc->ddr_mode ? 2 : 1; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 494 | div -= 1; |
| 495 | |
| 496 | clk = (pre_div << 8) | (div << 4); |
| 497 | |
Kumar Gala | 09876a3 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 498 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 499 | |
| 500 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 501 | |
| 502 | udelay(10000); |
| 503 | |
Kumar Gala | 09876a3 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 504 | clk = SYSCTL_PEREN | SYSCTL_CKEN; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 505 | |
| 506 | esdhc_setbits32(®s->sysctl, clk); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 507 | } |
| 508 | |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 509 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 510 | static void esdhc_clock_control(struct mmc *mmc, bool enable) |
| 511 | { |
| 512 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
| 513 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
| 514 | u32 value; |
| 515 | u32 time_out; |
| 516 | |
| 517 | value = esdhc_read32(®s->sysctl); |
| 518 | |
| 519 | if (enable) |
| 520 | value |= SYSCTL_CKEN; |
| 521 | else |
| 522 | value &= ~SYSCTL_CKEN; |
| 523 | |
| 524 | esdhc_write32(®s->sysctl, value); |
| 525 | |
| 526 | time_out = 20; |
| 527 | value = PRSSTAT_SDSTB; |
| 528 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 529 | if (time_out == 0) { |
| 530 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 531 | break; |
| 532 | } |
| 533 | time_out--; |
| 534 | mdelay(1); |
| 535 | } |
| 536 | } |
| 537 | #endif |
| 538 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 539 | static void esdhc_set_ios(struct mmc *mmc) |
| 540 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 541 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 542 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 543 | |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 544 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 545 | /* Select to use peripheral clock */ |
| 546 | esdhc_clock_control(mmc, false); |
| 547 | esdhc_setbits32(®s->scr, ESDHCCTL_PCS); |
| 548 | esdhc_clock_control(mmc, true); |
| 549 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 550 | /* Set the clock speed */ |
| 551 | set_sysctl(mmc, mmc->clock); |
| 552 | |
| 553 | /* Set the bus width */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 554 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 555 | |
| 556 | if (mmc->bus_width == 4) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 557 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 558 | else if (mmc->bus_width == 8) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 559 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 560 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | static int esdhc_init(struct mmc *mmc) |
| 564 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 565 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 566 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 567 | int timeout = 1000; |
| 568 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 569 | /* Reset the entire host controller */ |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 570 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 571 | |
| 572 | /* Wait until the controller is available */ |
| 573 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 574 | udelay(1000); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 575 | |
Benoît Thébaudeau | c08d11c | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 576 | #ifndef ARCH_MXC |
P.V.Suresh | 7b1868b | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 577 | /* Enable cache snooping */ |
Benoît Thébaudeau | c08d11c | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 578 | esdhc_write32(®s->scr, 0x00000040); |
| 579 | #endif |
P.V.Suresh | 7b1868b | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 580 | |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 581 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 582 | |
| 583 | /* Set the initial clock speed */ |
Jerry Huang | 0caea1a | 2010-11-25 17:06:07 +0000 | [diff] [blame] | 584 | mmc_set_clock(mmc, 400000); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 585 | |
| 586 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 587 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 588 | |
| 589 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 590 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 591 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 592 | /* Set timout to the maximum value */ |
| 593 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 594 | |
Otavio Salvador | 12b2a87 | 2015-02-17 10:42:44 -0200 | [diff] [blame] | 595 | #ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT |
| 596 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 597 | #endif |
| 598 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static int esdhc_getcd(struct mmc *mmc) |
| 603 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 604 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 605 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
| 606 | int timeout = 1000; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 607 | |
Haijun.Zhang | 05f5854 | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 608 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 609 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 610 | return 1; |
| 611 | #endif |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 612 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 613 | udelay(1000); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 614 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 615 | return timeout > 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 616 | } |
| 617 | |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 618 | static void esdhc_reset(struct fsl_esdhc *regs) |
| 619 | { |
| 620 | unsigned long timeout = 100; /* wait max 100 ms */ |
| 621 | |
| 622 | /* reset the controller */ |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 623 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 624 | |
| 625 | /* hardware clears the bit when it is done */ |
| 626 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 627 | udelay(1000); |
| 628 | if (!timeout) |
| 629 | printf("MMC/SD: Reset never completed.\n"); |
| 630 | } |
| 631 | |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 632 | static const struct mmc_ops esdhc_ops = { |
| 633 | .send_cmd = esdhc_send_cmd, |
| 634 | .set_ios = esdhc_set_ios, |
| 635 | .init = esdhc_init, |
| 636 | .getcd = esdhc_getcd, |
| 637 | }; |
| 638 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 639 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 640 | { |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 641 | struct fsl_esdhc *regs; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 642 | struct mmc *mmc; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 643 | u32 caps, voltage_caps; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 644 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 645 | if (!cfg) |
| 646 | return -1; |
| 647 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 648 | regs = (struct fsl_esdhc *)cfg->esdhc_base; |
| 649 | |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 650 | /* First reset the eSDHC controller */ |
| 651 | esdhc_reset(regs); |
| 652 | |
Jerry Huang | 4e3bfa0 | 2012-05-17 23:57:02 +0000 | [diff] [blame] | 653 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
| 654 | | SYSCTL_IPGEN | SYSCTL_CKEN); |
| 655 | |
Ye.Li | 3d46c31 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 656 | writel(SDHCI_IRQ_EN_BITS, ®s->irqstaten); |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 657 | memset(&cfg->cfg, 0, sizeof(cfg->cfg)); |
| 658 | |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 659 | voltage_caps = 0; |
Wang Huan | c929213 | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 660 | caps = esdhc_read32(®s->hostcapblt); |
Roy Zang | 3935661 | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 661 | |
| 662 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
| 663 | caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | |
| 664 | ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); |
| 665 | #endif |
Haijun.Zhang | 8a065e9 | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 666 | |
| 667 | /* T4240 host controller capabilities register should have VS33 bit */ |
| 668 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
| 669 | caps = caps | ESDHC_HOSTCAPBLT_VS33; |
| 670 | #endif |
| 671 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 672 | if (caps & ESDHC_HOSTCAPBLT_VS18) |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 673 | voltage_caps |= MMC_VDD_165_195; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 674 | if (caps & ESDHC_HOSTCAPBLT_VS30) |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 675 | voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 676 | if (caps & ESDHC_HOSTCAPBLT_VS33) |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 677 | voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; |
| 678 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 679 | cfg->cfg.name = "FSL_SDHC"; |
| 680 | cfg->cfg.ops = &esdhc_ops; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 681 | #ifdef CONFIG_SYS_SD_VOLTAGE |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 682 | cfg->cfg.voltages = CONFIG_SYS_SD_VOLTAGE; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 683 | #else |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 684 | cfg->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 685 | #endif |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 686 | if ((cfg->cfg.voltages & voltage_caps) == 0) { |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 687 | printf("voltage not supported by controller\n"); |
| 688 | return -1; |
| 689 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 690 | |
Rob Herring | 5fd3edd | 2015-03-23 17:56:59 -0500 | [diff] [blame] | 691 | cfg->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
Volodymyr Riazantsev | d251e11 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 692 | #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE |
| 693 | cfg->cfg.host_caps |= MMC_MODE_DDR_52MHz; |
| 694 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 695 | |
Abbas Raza | e6bf977 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 696 | if (cfg->max_bus_width > 0) { |
| 697 | if (cfg->max_bus_width < 8) |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 698 | cfg->cfg.host_caps &= ~MMC_MODE_8BIT; |
Abbas Raza | e6bf977 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 699 | if (cfg->max_bus_width < 4) |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 700 | cfg->cfg.host_caps &= ~MMC_MODE_4BIT; |
Abbas Raza | e6bf977 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 703 | if (caps & ESDHC_HOSTCAPBLT_HSS) |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 704 | cfg->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 705 | |
Haijun.Zhang | f0fe8ad | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 706 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 707 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 708 | cfg->cfg.host_caps &= ~MMC_MODE_8BIT; |
Haijun.Zhang | f0fe8ad | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 709 | #endif |
| 710 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 711 | cfg->cfg.f_min = 400000; |
Tom Rini | 2907a30 | 2014-11-26 11:22:29 -0500 | [diff] [blame] | 712 | cfg->cfg.f_max = min(cfg->sdhc_clk, (u32)52000000); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 713 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 714 | cfg->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 715 | |
| 716 | mmc = mmc_create(&cfg->cfg, cfg); |
| 717 | if (mmc == NULL) |
| 718 | return -1; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 724 | { |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 725 | struct fsl_esdhc_cfg *cfg; |
| 726 | |
Fabio Estevam | 6592a99 | 2012-12-27 08:51:08 +0000 | [diff] [blame] | 727 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 728 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
Simon Glass | 9e247d1 | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 729 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 730 | return fsl_esdhc_initialize(bis, cfg); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 731 | } |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 732 | |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 733 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 734 | void mmc_adapter_card_type_ident(void) |
| 735 | { |
| 736 | u8 card_id; |
| 737 | u8 value; |
| 738 | |
| 739 | card_id = QIXIS_READ(present) & QIXIS_SDID_MASK; |
| 740 | gd->arch.sdhc_adapter = card_id; |
| 741 | |
| 742 | switch (card_id) { |
| 743 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45: |
| 744 | break; |
| 745 | case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY: |
| 746 | break; |
| 747 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44: |
| 748 | value = QIXIS_READ(brdcfg[5]); |
| 749 | value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT); |
| 750 | QIXIS_WRITE(brdcfg[5], value); |
| 751 | break; |
| 752 | case QIXIS_ESDHC_ADAPTER_TYPE_RSV: |
| 753 | break; |
| 754 | case QIXIS_ESDHC_ADAPTER_TYPE_MMC: |
| 755 | break; |
| 756 | case QIXIS_ESDHC_ADAPTER_TYPE_SD: |
| 757 | break; |
| 758 | case QIXIS_ESDHC_NO_ADAPTER: |
| 759 | break; |
| 760 | default: |
| 761 | break; |
| 762 | } |
| 763 | } |
| 764 | #endif |
| 765 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 766 | #ifdef CONFIG_OF_LIBFDT |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 767 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 768 | { |
| 769 | const char *compat = "fsl,esdhc"; |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 770 | |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 771 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 772 | if (!hwconfig("esdhc")) { |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 773 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 774 | 8 + 1, 1); |
| 775 | return; |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 776 | } |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 777 | #endif |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 778 | |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 779 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 780 | do_fixup_by_compat_u32(blob, compat, "peripheral-frequency", |
| 781 | gd->arch.sdhc_clk, 1); |
| 782 | #else |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 783 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | 9e247d1 | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 784 | gd->arch.sdhc_clk, 1); |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 785 | #endif |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 786 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 787 | do_fixup_by_compat_u32(blob, compat, "adapter-type", |
| 788 | (u32)(gd->arch.sdhc_adapter), 1); |
| 789 | #endif |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 790 | do_fixup_by_compat(blob, compat, "status", "okay", |
| 791 | 4 + 1, 1); |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 792 | } |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 793 | #endif |