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