Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1 | /* |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 2 | * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc |
Andy Fleming | 50586ef | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 50586ef | 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 | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 15 | #include <hwconfig.h> |
Andy Fleming | 50586ef | 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 | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 21 | #include <fdt_support.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 22 | #include <asm/io.h> |
| 23 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
Ye.Li | a3d6e38 | 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 | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 33 | struct fsl_esdhc { |
Haijun.Zhang | 511948b | 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 | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 57 | char reserved2[100]; /* reserved */ |
| 58 | uint vendorspec; /* Vendor Specific register */ |
| 59 | char reserved3[59]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 60 | uint hostver; /* Host controller version register */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 61 | char reserved4[4]; /* reserved */ |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 62 | uint dmaerraddr; /* DMA error address register */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 63 | char reserved5[4]; /* reserved */ |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 64 | uint dmaerrattr; /* DMA error attribute register */ |
| 65 | char reserved6[4]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 66 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 67 | char reserved7[8]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 68 | uint tcr; /* Tuning control register */ |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 69 | char reserved8[28]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 70 | uint sddirctl; /* SD direction control register */ |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 71 | char reserved9[712]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 72 | uint scr; /* eSDHC control register */ |
Andy Fleming | 50586ef | 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 | eafa90a | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 76 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 77 | { |
| 78 | uint xfertyp = 0; |
| 79 | |
| 80 | if (data) { |
Dipen Dudhat | 77c1458 | 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 | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 85 | if (data->blocks > 1) { |
| 86 | xfertyp |= XFERTYP_MSBSEL; |
| 87 | xfertyp |= XFERTYP_BCEN; |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 88 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 89 | xfertyp |= XFERTYP_AC12EN; |
| 90 | #endif |
Andy Fleming | 50586ef | 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 | |
Wang Huan | 19060bd | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 108 | #if defined(CONFIG_MX53) || defined(CONFIG_PPC_T4240) || defined(CONFIG_LS102XA) |
Jason Liu | 4571de3 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 109 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 110 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
| 111 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 112 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 113 | } |
| 114 | |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 115 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 116 | /* |
| 117 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 118 | */ |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 119 | static void |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 120 | esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data) |
| 121 | { |
Ira Snyder | 8eee2bd | 2011-12-23 08:30:40 +0000 | [diff] [blame] | 122 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
| 123 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 124 | uint blocks; |
| 125 | char *buffer; |
| 126 | uint databuf; |
| 127 | uint size; |
| 128 | uint irqstat; |
| 129 | uint timeout; |
| 130 | |
| 131 | if (data->flags & MMC_DATA_READ) { |
| 132 | blocks = data->blocks; |
| 133 | buffer = data->dest; |
| 134 | while (blocks) { |
| 135 | timeout = PIO_TIMEOUT; |
| 136 | size = data->blocksize; |
| 137 | irqstat = esdhc_read32(®s->irqstat); |
| 138 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN) |
| 139 | && --timeout); |
| 140 | if (timeout <= 0) { |
| 141 | printf("\nData Read Failed in PIO Mode."); |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 142 | return; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 143 | } |
| 144 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 145 | udelay(100); /* Wait before last byte transfer complete */ |
| 146 | irqstat = esdhc_read32(®s->irqstat); |
| 147 | databuf = in_le32(®s->datport); |
| 148 | *((uint *)buffer) = databuf; |
| 149 | buffer += 4; |
| 150 | size -= 4; |
| 151 | } |
| 152 | blocks--; |
| 153 | } |
| 154 | } else { |
| 155 | blocks = data->blocks; |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 156 | buffer = (char *)data->src; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 157 | while (blocks) { |
| 158 | timeout = PIO_TIMEOUT; |
| 159 | size = data->blocksize; |
| 160 | irqstat = esdhc_read32(®s->irqstat); |
| 161 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN) |
| 162 | && --timeout); |
| 163 | if (timeout <= 0) { |
| 164 | printf("\nData Write Failed in PIO Mode."); |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 165 | return; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 166 | } |
| 167 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 168 | udelay(100); /* Wait before last byte transfer complete */ |
| 169 | databuf = *((uint *)buffer); |
| 170 | buffer += 4; |
| 171 | size -= 4; |
| 172 | irqstat = esdhc_read32(®s->irqstat); |
| 173 | out_le32(®s->datport, databuf); |
| 174 | } |
| 175 | blocks--; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | #endif |
| 180 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 181 | static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data) |
| 182 | { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 183 | int timeout; |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 184 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 185 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 186 | |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 187 | uint wml_value; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 188 | |
| 189 | wml_value = data->blocksize/4; |
| 190 | |
| 191 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 192 | if (wml_value > WML_RD_WML_MAX) |
| 193 | wml_value = WML_RD_WML_MAX_VAL; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 194 | |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 195 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 196 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 197 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 198 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 199 | } else { |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 200 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 201 | flush_dcache_range((ulong)data->src, |
| 202 | (ulong)data->src+data->blocks |
| 203 | *data->blocksize); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 204 | #endif |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 205 | if (wml_value > WML_WR_WML_MAX) |
| 206 | wml_value = WML_WR_WML_MAX_VAL; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 207 | if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 208 | printf("\nThe SD card is locked. Can not write to a locked card.\n\n"); |
| 209 | return TIMEOUT; |
| 210 | } |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 211 | |
| 212 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 213 | wml_value << 16); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 214 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 215 | esdhc_write32(®s->dsaddr, (u32)data->src); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 216 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 219 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 220 | |
| 221 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 222 | /* |
| 223 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 224 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 225 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 226 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 227 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 228 | * As 1) >= 2) |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 229 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 230 | * Taking log2 both the sides |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 231 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 232 | * Rounding up to next power of 2 |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 233 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 234 | * => timeout + 13 = fls(mmc->clock/4) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 235 | */ |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 236 | timeout = fls(mmc->clock/4); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 237 | timeout -= 13; |
| 238 | |
| 239 | if (timeout > 14) |
| 240 | timeout = 14; |
| 241 | |
| 242 | if (timeout < 0) |
| 243 | timeout = 0; |
| 244 | |
Kumar Gala | 5103a03 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 245 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 246 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 247 | timeout++; |
| 248 | #endif |
| 249 | |
Haijun.Zhang | 1336e2d | 2014-03-18 17:04:23 +0800 | [diff] [blame] | 250 | #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE |
| 251 | timeout = 0xE; |
| 252 | #endif |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 253 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Tom Rini | 10dc777 | 2014-05-23 09:19:05 -0400 | [diff] [blame] | 258 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 259 | static void check_and_invalidate_dcache_range |
| 260 | (struct mmc_cmd *cmd, |
| 261 | struct mmc_data *data) { |
| 262 | unsigned start = (unsigned)data->dest ; |
| 263 | unsigned size = roundup(ARCH_DMA_MINALIGN, |
| 264 | data->blocks*data->blocksize); |
| 265 | unsigned end = start+size ; |
| 266 | invalidate_dcache_range(start, end); |
| 267 | } |
Tom Rini | 10dc777 | 2014-05-23 09:19:05 -0400 | [diff] [blame] | 268 | #endif |
| 269 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 270 | /* |
| 271 | * Sends a command out on the bus. Takes the mmc pointer, |
| 272 | * a command pointer, and an optional data pointer. |
| 273 | */ |
| 274 | static int |
| 275 | esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 276 | { |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 277 | int err = 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 278 | uint xfertyp; |
| 279 | uint irqstat; |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 280 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 281 | volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 282 | |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 283 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 284 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 285 | return 0; |
| 286 | #endif |
| 287 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 288 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 289 | |
| 290 | sync(); |
| 291 | |
| 292 | /* Wait for the bus to be idle */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 293 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 294 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 295 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 296 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 297 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 298 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 299 | |
| 300 | /* Wait at least 8 SD clock cycles before the next command */ |
| 301 | /* |
| 302 | * Note: This is way more than 8 cycles, but 1ms seems to |
| 303 | * resolve timing issues with some cards |
| 304 | */ |
| 305 | udelay(1000); |
| 306 | |
| 307 | /* Set up for a data transfer if we have one */ |
| 308 | if (data) { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 309 | err = esdhc_setup_data(mmc, data); |
| 310 | if(err) |
| 311 | return err; |
| 312 | } |
| 313 | |
| 314 | /* Figure out the transfer arguments */ |
| 315 | xfertyp = esdhc_xfertyp(cmd, data); |
| 316 | |
Andrew Gabbasov | 01b7735 | 2013-06-11 10:34:22 -0500 | [diff] [blame] | 317 | /* Mask all irqs */ |
| 318 | esdhc_write32(®s->irqsigen, 0); |
| 319 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 320 | /* Send the command */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 321 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 322 | #if defined(CONFIG_FSL_USDHC) |
| 323 | esdhc_write32(®s->mixctrl, |
| 324 | (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)); |
| 325 | esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000); |
| 326 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 327 | esdhc_write32(®s->xfertyp, xfertyp); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 328 | #endif |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 329 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 330 | /* Wait for the command to complete */ |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 331 | while (!(esdhc_read32(®s->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE))) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 332 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 333 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 334 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 335 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 336 | if (irqstat & CMD_ERR) { |
| 337 | err = COMM_ERR; |
| 338 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 341 | if (irqstat & IRQSTAT_CTOE) { |
| 342 | err = TIMEOUT; |
| 343 | goto out; |
| 344 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 345 | |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 346 | /* Switch voltage to 1.8V if CMD11 succeeded */ |
| 347 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) { |
| 348 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 349 | |
| 350 | printf("Run CMD11 1.8V switch\n"); |
| 351 | /* Sleep for 5 ms - max time for card to switch to 1.8V */ |
| 352 | udelay(5000); |
| 353 | } |
| 354 | |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 355 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 356 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
| 357 | int timeout = 2500; |
| 358 | |
| 359 | /* Poll on DATA0 line for cmd with busy signal for 250 ms */ |
| 360 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 361 | PRSSTAT_DAT0)) { |
| 362 | udelay(100); |
| 363 | timeout--; |
| 364 | } |
| 365 | |
| 366 | if (timeout <= 0) { |
| 367 | printf("Timeout waiting for DAT0 to go high!\n"); |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 368 | err = TIMEOUT; |
| 369 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 373 | /* Copy the response to the response buffer */ |
| 374 | if (cmd->resp_type & MMC_RSP_136) { |
| 375 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 376 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 377 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 378 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 379 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 380 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 381 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 382 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 383 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 384 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 385 | } else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 386 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 387 | |
| 388 | /* Wait until all of the blocks are transferred */ |
| 389 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 390 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 391 | esdhc_pio_read_write(mmc, data); |
| 392 | #else |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 393 | do { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 394 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 395 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 396 | if (irqstat & IRQSTAT_DTOE) { |
| 397 | err = TIMEOUT; |
| 398 | goto out; |
| 399 | } |
Frans Meulenbroeks | 63fb5a7 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 400 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 401 | if (irqstat & DATA_ERR) { |
| 402 | err = COMM_ERR; |
| 403 | goto out; |
| 404 | } |
Andrew Gabbasov | 9b74dc5 | 2013-04-07 23:06:08 +0000 | [diff] [blame] | 405 | } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 406 | |
Eric Nelson | 54899fc | 2013-04-03 12:31:56 +0000 | [diff] [blame] | 407 | if (data->flags & MMC_DATA_READ) |
| 408 | check_and_invalidate_dcache_range(cmd, data); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 409 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 412 | out: |
| 413 | /* Reset CMD and DATA portions on error */ |
| 414 | if (err) { |
| 415 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 416 | SYSCTL_RSTC); |
| 417 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 418 | ; |
| 419 | |
| 420 | if (data) { |
| 421 | esdhc_write32(®s->sysctl, |
| 422 | esdhc_read32(®s->sysctl) | |
| 423 | SYSCTL_RSTD); |
| 424 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 425 | ; |
| 426 | } |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 427 | |
| 428 | /* If this was CMD11, then notify that power cycle is needed */ |
| 429 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) |
| 430 | printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n"); |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 431 | } |
| 432 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 433 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 434 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 435 | return err; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 436 | } |
| 437 | |
Kim Phillips | eafa90a | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 438 | static void set_sysctl(struct mmc *mmc, uint clock) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 439 | { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 440 | int div, pre_div; |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 441 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 442 | volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Benoît Thébaudeau | a2ac1b3 | 2012-10-01 08:36:25 +0000 | [diff] [blame] | 443 | int sdhc_clk = cfg->sdhc_clk; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 444 | uint clk; |
| 445 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 446 | if (clock < mmc->cfg->f_min) |
| 447 | clock = mmc->cfg->f_min; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 448 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 449 | if (sdhc_clk / 16 > clock) { |
| 450 | for (pre_div = 2; pre_div < 256; pre_div *= 2) |
| 451 | if ((sdhc_clk / pre_div) <= (clock * 16)) |
| 452 | break; |
| 453 | } else |
| 454 | pre_div = 2; |
| 455 | |
| 456 | for (div = 1; div <= 16; div++) |
| 457 | if ((sdhc_clk / (div * pre_div)) <= clock) |
| 458 | break; |
| 459 | |
| 460 | pre_div >>= 1; |
| 461 | div -= 1; |
| 462 | |
| 463 | clk = (pre_div << 8) | (div << 4); |
| 464 | |
Kumar Gala | cc4d122 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 465 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 466 | |
| 467 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 468 | |
| 469 | udelay(10000); |
| 470 | |
Kumar Gala | cc4d122 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 471 | clk = SYSCTL_PEREN | SYSCTL_CKEN; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 472 | |
| 473 | esdhc_setbits32(®s->sysctl, clk); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static void esdhc_set_ios(struct mmc *mmc) |
| 477 | { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 478 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 479 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 480 | |
| 481 | /* Set the clock speed */ |
| 482 | set_sysctl(mmc, mmc->clock); |
| 483 | |
| 484 | /* Set the bus width */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 485 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 486 | |
| 487 | if (mmc->bus_width == 4) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 488 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 489 | else if (mmc->bus_width == 8) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 490 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 491 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static int esdhc_init(struct mmc *mmc) |
| 495 | { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 496 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 497 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 498 | int timeout = 1000; |
| 499 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 500 | /* Reset the entire host controller */ |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 501 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 502 | |
| 503 | /* Wait until the controller is available */ |
| 504 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 505 | udelay(1000); |
| 506 | |
Benoît Thébaudeau | 16e43f3 | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 507 | #ifndef ARCH_MXC |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 508 | /* Enable cache snooping */ |
Benoît Thébaudeau | 16e43f3 | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 509 | esdhc_write32(®s->scr, 0x00000040); |
| 510 | #endif |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 511 | |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 512 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 513 | |
| 514 | /* Set the initial clock speed */ |
Jerry Huang | 4a6ee17 | 2010-11-25 17:06:07 +0000 | [diff] [blame] | 515 | mmc_set_clock(mmc, 400000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 516 | |
| 517 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 518 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 519 | |
| 520 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 521 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 522 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 523 | /* Set timout to the maximum value */ |
| 524 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 525 | |
Otavio Salvador | ee0c538 | 2015-02-17 10:42:44 -0200 | [diff] [blame^] | 526 | #ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT |
| 527 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 528 | #endif |
| 529 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 530 | return 0; |
| 531 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 532 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 533 | static int esdhc_getcd(struct mmc *mmc) |
| 534 | { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 535 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 536 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
| 537 | int timeout = 1000; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 538 | |
Haijun.Zhang | f7e27cc | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 539 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 540 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 541 | return 1; |
| 542 | #endif |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 543 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 544 | udelay(1000); |
| 545 | |
| 546 | return timeout > 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 547 | } |
| 548 | |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 549 | static void esdhc_reset(struct fsl_esdhc *regs) |
| 550 | { |
| 551 | unsigned long timeout = 100; /* wait max 100 ms */ |
| 552 | |
| 553 | /* reset the controller */ |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 554 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 555 | |
| 556 | /* hardware clears the bit when it is done */ |
| 557 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 558 | udelay(1000); |
| 559 | if (!timeout) |
| 560 | printf("MMC/SD: Reset never completed.\n"); |
| 561 | } |
| 562 | |
Pantelis Antoniou | ab769f2 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 563 | static const struct mmc_ops esdhc_ops = { |
| 564 | .send_cmd = esdhc_send_cmd, |
| 565 | .set_ios = esdhc_set_ios, |
| 566 | .init = esdhc_init, |
| 567 | .getcd = esdhc_getcd, |
| 568 | }; |
| 569 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 570 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 571 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 572 | struct fsl_esdhc *regs; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 573 | struct mmc *mmc; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 574 | u32 caps, voltage_caps; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 575 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 576 | if (!cfg) |
| 577 | return -1; |
| 578 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 579 | regs = (struct fsl_esdhc *)cfg->esdhc_base; |
| 580 | |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 581 | /* First reset the eSDHC controller */ |
| 582 | esdhc_reset(regs); |
| 583 | |
Jerry Huang | 975324a | 2012-05-17 23:57:02 +0000 | [diff] [blame] | 584 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
| 585 | | SYSCTL_IPGEN | SYSCTL_CKEN); |
| 586 | |
Ye.Li | a3d6e38 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 587 | writel(SDHCI_IRQ_EN_BITS, ®s->irqstaten); |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 588 | memset(&cfg->cfg, 0, sizeof(cfg->cfg)); |
| 589 | |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 590 | voltage_caps = 0; |
Wang Huan | 19060bd | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 591 | caps = esdhc_read32(®s->hostcapblt); |
Roy Zang | 3b4456e | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 592 | |
| 593 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
| 594 | caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | |
| 595 | ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); |
| 596 | #endif |
Haijun.Zhang | ef38f3f | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 597 | |
| 598 | /* T4240 host controller capabilities register should have VS33 bit */ |
| 599 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
| 600 | caps = caps | ESDHC_HOSTCAPBLT_VS33; |
| 601 | #endif |
| 602 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 603 | if (caps & ESDHC_HOSTCAPBLT_VS18) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 604 | voltage_caps |= MMC_VDD_165_195; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 605 | if (caps & ESDHC_HOSTCAPBLT_VS30) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 606 | voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 607 | if (caps & ESDHC_HOSTCAPBLT_VS33) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 608 | voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; |
| 609 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 610 | cfg->cfg.name = "FSL_SDHC"; |
| 611 | cfg->cfg.ops = &esdhc_ops; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 612 | #ifdef CONFIG_SYS_SD_VOLTAGE |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 613 | cfg->cfg.voltages = CONFIG_SYS_SD_VOLTAGE; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 614 | #else |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 615 | cfg->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 616 | #endif |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 617 | if ((cfg->cfg.voltages & voltage_caps) == 0) { |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 618 | printf("voltage not supported by controller\n"); |
| 619 | return -1; |
| 620 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 621 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 622 | cfg->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HC; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 623 | |
Abbas Raza | aad4659 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 624 | if (cfg->max_bus_width > 0) { |
| 625 | if (cfg->max_bus_width < 8) |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 626 | cfg->cfg.host_caps &= ~MMC_MODE_8BIT; |
Abbas Raza | aad4659 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 627 | if (cfg->max_bus_width < 4) |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 628 | cfg->cfg.host_caps &= ~MMC_MODE_4BIT; |
Abbas Raza | aad4659 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 631 | if (caps & ESDHC_HOSTCAPBLT_HSS) |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 632 | cfg->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 633 | |
Haijun.Zhang | d47e3d2 | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 634 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 635 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 636 | cfg->cfg.host_caps &= ~MMC_MODE_8BIT; |
Haijun.Zhang | d47e3d2 | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 637 | #endif |
| 638 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 639 | cfg->cfg.f_min = 400000; |
Tom Rini | 21008ad | 2014-11-26 11:22:29 -0500 | [diff] [blame] | 640 | cfg->cfg.f_max = min(cfg->sdhc_clk, (u32)52000000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 641 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 642 | cfg->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 643 | |
| 644 | mmc = mmc_create(&cfg->cfg, cfg); |
| 645 | if (mmc == NULL) |
| 646 | return -1; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 652 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 653 | struct fsl_esdhc_cfg *cfg; |
| 654 | |
Fabio Estevam | 88227a1 | 2012-12-27 08:51:08 +0000 | [diff] [blame] | 655 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 656 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
Simon Glass | e9adeca | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 657 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 658 | return fsl_esdhc_initialize(bis, cfg); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 659 | } |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 660 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 661 | #ifdef CONFIG_OF_LIBFDT |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 662 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 663 | { |
| 664 | const char *compat = "fsl,esdhc"; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 665 | |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 666 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 667 | if (!hwconfig("esdhc")) { |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 668 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 669 | 8 + 1, 1); |
| 670 | return; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 671 | } |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 672 | #endif |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 673 | |
| 674 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | e9adeca | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 675 | gd->arch.sdhc_clk, 1); |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 676 | |
| 677 | do_fixup_by_compat(blob, compat, "status", "okay", |
| 678 | 4 + 1, 1); |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 679 | } |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 680 | #endif |