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