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 | |
Wang Huan | 19060bd | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 99 | #if defined(CONFIG_MX53) || defined(CONFIG_PPC_T4240) || defined(CONFIG_LS102XA) |
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; |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 177 | |
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); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 187 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 188 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 189 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 190 | } else { |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 191 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 192 | flush_dcache_range((ulong)data->src, |
| 193 | (ulong)data->src+data->blocks |
| 194 | *data->blocksize); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 195 | #endif |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 196 | if (wml_value > WML_WR_WML_MAX) |
| 197 | wml_value = WML_WR_WML_MAX_VAL; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 198 | if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 199 | printf("\nThe SD card is locked. Can not write to a locked card.\n\n"); |
| 200 | return TIMEOUT; |
| 201 | } |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 202 | |
| 203 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 204 | wml_value << 16); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 205 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 206 | esdhc_write32(®s->dsaddr, (u32)data->src); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 207 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 210 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 211 | |
| 212 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 213 | /* |
| 214 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 215 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 216 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 217 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 218 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 219 | * As 1) >= 2) |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 220 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 221 | * Taking log2 both the sides |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 222 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 223 | * Rounding up to next power of 2 |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 224 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 225 | * => timeout + 13 = fls(mmc->clock/4) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 226 | */ |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 227 | timeout = fls(mmc->clock/4); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 228 | timeout -= 13; |
| 229 | |
| 230 | if (timeout > 14) |
| 231 | timeout = 14; |
| 232 | |
| 233 | if (timeout < 0) |
| 234 | timeout = 0; |
| 235 | |
Kumar Gala | 5103a03 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 236 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 237 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 238 | timeout++; |
| 239 | #endif |
| 240 | |
Haijun.Zhang | 1336e2d | 2014-03-18 17:04:23 +0800 | [diff] [blame] | 241 | #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE |
| 242 | timeout = 0xE; |
| 243 | #endif |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 244 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Tom Rini | 10dc777 | 2014-05-23 09:19:05 -0400 | [diff] [blame] | 249 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 250 | static void check_and_invalidate_dcache_range |
| 251 | (struct mmc_cmd *cmd, |
| 252 | struct mmc_data *data) { |
| 253 | unsigned start = (unsigned)data->dest ; |
| 254 | unsigned size = roundup(ARCH_DMA_MINALIGN, |
| 255 | data->blocks*data->blocksize); |
| 256 | unsigned end = start+size ; |
| 257 | invalidate_dcache_range(start, end); |
| 258 | } |
Tom Rini | 10dc777 | 2014-05-23 09:19:05 -0400 | [diff] [blame] | 259 | #endif |
| 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 | { |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 268 | int err = 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 269 | uint xfertyp; |
| 270 | uint irqstat; |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 271 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 272 | volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 273 | |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 274 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 275 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 276 | return 0; |
| 277 | #endif |
| 278 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 279 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 280 | |
| 281 | sync(); |
| 282 | |
| 283 | /* Wait for the bus to be idle */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 284 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 285 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 286 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 287 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 288 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 289 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 290 | |
| 291 | /* Wait at least 8 SD clock cycles before the next command */ |
| 292 | /* |
| 293 | * Note: This is way more than 8 cycles, but 1ms seems to |
| 294 | * resolve timing issues with some cards |
| 295 | */ |
| 296 | udelay(1000); |
| 297 | |
| 298 | /* Set up for a data transfer if we have one */ |
| 299 | if (data) { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 300 | err = esdhc_setup_data(mmc, data); |
| 301 | if(err) |
| 302 | return err; |
| 303 | } |
| 304 | |
| 305 | /* Figure out the transfer arguments */ |
| 306 | xfertyp = esdhc_xfertyp(cmd, data); |
| 307 | |
Andrew Gabbasov | 01b7735 | 2013-06-11 10:34:22 -0500 | [diff] [blame] | 308 | /* Mask all irqs */ |
| 309 | esdhc_write32(®s->irqsigen, 0); |
| 310 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 311 | /* Send the command */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 312 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 313 | #if defined(CONFIG_FSL_USDHC) |
| 314 | esdhc_write32(®s->mixctrl, |
| 315 | (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)); |
| 316 | esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000); |
| 317 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 318 | esdhc_write32(®s->xfertyp, xfertyp); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 319 | #endif |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 320 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 321 | /* Wait for the command to complete */ |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 322 | while (!(esdhc_read32(®s->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE))) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 323 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 324 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 325 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 326 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 327 | if (irqstat & CMD_ERR) { |
| 328 | err = COMM_ERR; |
| 329 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 332 | if (irqstat & IRQSTAT_CTOE) { |
| 333 | err = TIMEOUT; |
| 334 | goto out; |
| 335 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 336 | |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 337 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 338 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
| 339 | int timeout = 2500; |
| 340 | |
| 341 | /* Poll on DATA0 line for cmd with busy signal for 250 ms */ |
| 342 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 343 | PRSSTAT_DAT0)) { |
| 344 | udelay(100); |
| 345 | timeout--; |
| 346 | } |
| 347 | |
| 348 | if (timeout <= 0) { |
| 349 | printf("Timeout waiting for DAT0 to go high!\n"); |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 350 | err = TIMEOUT; |
| 351 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 355 | /* Copy the response to the response buffer */ |
| 356 | if (cmd->resp_type & MMC_RSP_136) { |
| 357 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 358 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 359 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 360 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 361 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 362 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 363 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 364 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 365 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 366 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 367 | } else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 368 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 369 | |
| 370 | /* Wait until all of the blocks are transferred */ |
| 371 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 372 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 373 | esdhc_pio_read_write(mmc, data); |
| 374 | #else |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 375 | do { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 376 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 377 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 378 | if (irqstat & IRQSTAT_DTOE) { |
| 379 | err = TIMEOUT; |
| 380 | goto out; |
| 381 | } |
Frans Meulenbroeks | 63fb5a7 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 382 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 383 | if (irqstat & DATA_ERR) { |
| 384 | err = COMM_ERR; |
| 385 | goto out; |
| 386 | } |
Andrew Gabbasov | 9b74dc5 | 2013-04-07 23:06:08 +0000 | [diff] [blame] | 387 | } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 388 | |
Eric Nelson | 54899fc | 2013-04-03 12:31:56 +0000 | [diff] [blame] | 389 | if (data->flags & MMC_DATA_READ) |
| 390 | check_and_invalidate_dcache_range(cmd, data); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 391 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 394 | out: |
| 395 | /* Reset CMD and DATA portions on error */ |
| 396 | if (err) { |
| 397 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 398 | SYSCTL_RSTC); |
| 399 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 400 | ; |
| 401 | |
| 402 | if (data) { |
| 403 | esdhc_write32(®s->sysctl, |
| 404 | esdhc_read32(®s->sysctl) | |
| 405 | SYSCTL_RSTD); |
| 406 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 407 | ; |
| 408 | } |
| 409 | } |
| 410 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 411 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 412 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 413 | return err; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 414 | } |
| 415 | |
Kim Phillips | eafa90a | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 416 | static void set_sysctl(struct mmc *mmc, uint clock) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 417 | { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 418 | int div, pre_div; |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 419 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 420 | 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] | 421 | int sdhc_clk = cfg->sdhc_clk; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 422 | uint clk; |
| 423 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 424 | if (clock < mmc->cfg->f_min) |
| 425 | clock = mmc->cfg->f_min; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 426 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 427 | if (sdhc_clk / 16 > clock) { |
| 428 | for (pre_div = 2; pre_div < 256; pre_div *= 2) |
| 429 | if ((sdhc_clk / pre_div) <= (clock * 16)) |
| 430 | break; |
| 431 | } else |
| 432 | pre_div = 2; |
| 433 | |
| 434 | for (div = 1; div <= 16; div++) |
| 435 | if ((sdhc_clk / (div * pre_div)) <= clock) |
| 436 | break; |
| 437 | |
| 438 | pre_div >>= 1; |
| 439 | div -= 1; |
| 440 | |
| 441 | clk = (pre_div << 8) | (div << 4); |
| 442 | |
Kumar Gala | cc4d122 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 443 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 444 | |
| 445 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 446 | |
| 447 | udelay(10000); |
| 448 | |
Kumar Gala | cc4d122 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 449 | clk = SYSCTL_PEREN | SYSCTL_CKEN; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 450 | |
| 451 | esdhc_setbits32(®s->sysctl, clk); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static void esdhc_set_ios(struct mmc *mmc) |
| 455 | { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 456 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 457 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 458 | |
| 459 | /* Set the clock speed */ |
| 460 | set_sysctl(mmc, mmc->clock); |
| 461 | |
| 462 | /* Set the bus width */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 463 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 464 | |
| 465 | if (mmc->bus_width == 4) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 466 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 467 | else if (mmc->bus_width == 8) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 468 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 469 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static int esdhc_init(struct mmc *mmc) |
| 473 | { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 474 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 475 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 476 | int timeout = 1000; |
| 477 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 478 | /* Reset the entire host controller */ |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 479 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 480 | |
| 481 | /* Wait until the controller is available */ |
| 482 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 483 | udelay(1000); |
| 484 | |
Benoît Thébaudeau | 16e43f3 | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 485 | #ifndef ARCH_MXC |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 486 | /* Enable cache snooping */ |
Benoît Thébaudeau | 16e43f3 | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 487 | esdhc_write32(®s->scr, 0x00000040); |
| 488 | #endif |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 489 | |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 490 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 491 | |
| 492 | /* Set the initial clock speed */ |
Jerry Huang | 4a6ee17 | 2010-11-25 17:06:07 +0000 | [diff] [blame] | 493 | mmc_set_clock(mmc, 400000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 494 | |
| 495 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 496 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 497 | |
| 498 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 499 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 500 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 501 | /* Set timout to the maximum value */ |
| 502 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 503 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 504 | return 0; |
| 505 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 506 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 507 | static int esdhc_getcd(struct mmc *mmc) |
| 508 | { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 509 | struct fsl_esdhc_cfg *cfg = mmc->priv; |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 510 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
| 511 | int timeout = 1000; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 512 | |
Haijun.Zhang | f7e27cc | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 513 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 514 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 515 | return 1; |
| 516 | #endif |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 517 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 518 | udelay(1000); |
| 519 | |
| 520 | return timeout > 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 521 | } |
| 522 | |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 523 | static void esdhc_reset(struct fsl_esdhc *regs) |
| 524 | { |
| 525 | unsigned long timeout = 100; /* wait max 100 ms */ |
| 526 | |
| 527 | /* reset the controller */ |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 528 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 529 | |
| 530 | /* hardware clears the bit when it is done */ |
| 531 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 532 | udelay(1000); |
| 533 | if (!timeout) |
| 534 | printf("MMC/SD: Reset never completed.\n"); |
| 535 | } |
| 536 | |
Pantelis Antoniou | ab769f2 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 537 | static const struct mmc_ops esdhc_ops = { |
| 538 | .send_cmd = esdhc_send_cmd, |
| 539 | .set_ios = esdhc_set_ios, |
| 540 | .init = esdhc_init, |
| 541 | .getcd = esdhc_getcd, |
| 542 | }; |
| 543 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 544 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 545 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 546 | struct fsl_esdhc *regs; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 547 | struct mmc *mmc; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 548 | u32 caps, voltage_caps; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 549 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 550 | if (!cfg) |
| 551 | return -1; |
| 552 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 553 | regs = (struct fsl_esdhc *)cfg->esdhc_base; |
| 554 | |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 555 | /* First reset the eSDHC controller */ |
| 556 | esdhc_reset(regs); |
| 557 | |
Jerry Huang | 975324a | 2012-05-17 23:57:02 +0000 | [diff] [blame] | 558 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
| 559 | | SYSCTL_IPGEN | SYSCTL_CKEN); |
| 560 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 561 | memset(&cfg->cfg, 0, sizeof(cfg->cfg)); |
| 562 | |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 563 | voltage_caps = 0; |
Wang Huan | 19060bd | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 564 | caps = esdhc_read32(®s->hostcapblt); |
Roy Zang | 3b4456e | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 565 | |
| 566 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
| 567 | caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | |
| 568 | ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); |
| 569 | #endif |
Haijun.Zhang | ef38f3f | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 570 | |
| 571 | /* T4240 host controller capabilities register should have VS33 bit */ |
| 572 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
| 573 | caps = caps | ESDHC_HOSTCAPBLT_VS33; |
| 574 | #endif |
| 575 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 576 | if (caps & ESDHC_HOSTCAPBLT_VS18) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 577 | voltage_caps |= MMC_VDD_165_195; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 578 | if (caps & ESDHC_HOSTCAPBLT_VS30) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 579 | voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 580 | if (caps & ESDHC_HOSTCAPBLT_VS33) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 581 | voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; |
| 582 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 583 | cfg->cfg.name = "FSL_SDHC"; |
| 584 | cfg->cfg.ops = &esdhc_ops; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 585 | #ifdef CONFIG_SYS_SD_VOLTAGE |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 586 | cfg->cfg.voltages = CONFIG_SYS_SD_VOLTAGE; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 587 | #else |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 588 | cfg->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 589 | #endif |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 590 | if ((cfg->cfg.voltages & voltage_caps) == 0) { |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 591 | printf("voltage not supported by controller\n"); |
| 592 | return -1; |
| 593 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 594 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 595 | 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] | 596 | |
Abbas Raza | aad4659 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 597 | if (cfg->max_bus_width > 0) { |
| 598 | if (cfg->max_bus_width < 8) |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 599 | cfg->cfg.host_caps &= ~MMC_MODE_8BIT; |
Abbas Raza | aad4659 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 600 | if (cfg->max_bus_width < 4) |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 601 | cfg->cfg.host_caps &= ~MMC_MODE_4BIT; |
Abbas Raza | aad4659 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 604 | if (caps & ESDHC_HOSTCAPBLT_HSS) |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 605 | cfg->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 606 | |
Haijun.Zhang | d47e3d2 | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 607 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 608 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 609 | cfg->cfg.host_caps &= ~MMC_MODE_8BIT; |
Haijun.Zhang | d47e3d2 | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 610 | #endif |
| 611 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 612 | cfg->cfg.f_min = 400000; |
Masahiro Yamada | c79cba3 | 2014-09-18 13:28:06 +0900 | [diff] [blame] | 613 | cfg->cfg.f_max = min(gd->arch.sdhc_clk, 52000000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 614 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 615 | cfg->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 616 | |
| 617 | mmc = mmc_create(&cfg->cfg, cfg); |
| 618 | if (mmc == NULL) |
| 619 | return -1; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 625 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 626 | struct fsl_esdhc_cfg *cfg; |
| 627 | |
Fabio Estevam | 88227a1 | 2012-12-27 08:51:08 +0000 | [diff] [blame] | 628 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 629 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
Simon Glass | e9adeca | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 630 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 631 | return fsl_esdhc_initialize(bis, cfg); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 632 | } |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 633 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 634 | #ifdef CONFIG_OF_LIBFDT |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 635 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 636 | { |
| 637 | const char *compat = "fsl,esdhc"; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 638 | |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 639 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 640 | if (!hwconfig("esdhc")) { |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 641 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 642 | 8 + 1, 1); |
| 643 | return; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 644 | } |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 645 | #endif |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 646 | |
| 647 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | e9adeca | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 648 | gd->arch.sdhc_clk, 1); |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 649 | |
| 650 | do_fixup_by_compat(blob, compat, "status", "okay", |
| 651 | 4 + 1, 1); |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 652 | } |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 653 | #endif |