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