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> |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 15 | #include <errno.h> |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 16 | #include <hwconfig.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 17 | #include <mmc.h> |
| 18 | #include <part.h> |
Peng Fan | 4483b7e | 2017-06-12 17:50:54 +0800 | [diff] [blame^] | 19 | #include <power/regulator.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 20 | #include <malloc.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 21 | #include <fsl_esdhc.h> |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 22 | #include <fdt_support.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 23 | #include <asm/io.h> |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 24 | #include <dm.h> |
| 25 | #include <asm-generic/gpio.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 26 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
Ye.Li | a3d6e38 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 29 | #define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \ |
| 30 | IRQSTATEN_CINT | \ |
| 31 | IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \ |
| 32 | IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \ |
| 33 | IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \ |
| 34 | IRQSTATEN_DINT) |
| 35 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 36 | struct fsl_esdhc { |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 37 | uint dsaddr; /* SDMA system address register */ |
| 38 | uint blkattr; /* Block attributes register */ |
| 39 | uint cmdarg; /* Command argument register */ |
| 40 | uint xfertyp; /* Transfer type register */ |
| 41 | uint cmdrsp0; /* Command response 0 register */ |
| 42 | uint cmdrsp1; /* Command response 1 register */ |
| 43 | uint cmdrsp2; /* Command response 2 register */ |
| 44 | uint cmdrsp3; /* Command response 3 register */ |
| 45 | uint datport; /* Buffer data port register */ |
| 46 | uint prsstat; /* Present state register */ |
| 47 | uint proctl; /* Protocol control register */ |
| 48 | uint sysctl; /* System Control Register */ |
| 49 | uint irqstat; /* Interrupt status register */ |
| 50 | uint irqstaten; /* Interrupt status enable register */ |
| 51 | uint irqsigen; /* Interrupt signal enable register */ |
| 52 | uint autoc12err; /* Auto CMD error status register */ |
| 53 | uint hostcapblt; /* Host controller capabilities register */ |
| 54 | uint wml; /* Watermark level register */ |
| 55 | uint mixctrl; /* For USDHC */ |
| 56 | char reserved1[4]; /* reserved */ |
| 57 | uint fevt; /* Force event register */ |
| 58 | uint admaes; /* ADMA error status register */ |
| 59 | uint adsaddr; /* ADMA system address register */ |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 60 | char reserved2[4]; |
| 61 | uint dllctrl; |
| 62 | uint dllstat; |
| 63 | uint clktunectrlstatus; |
| 64 | char reserved3[84]; |
| 65 | uint vendorspec; |
| 66 | uint mmcboot; |
| 67 | uint vendorspec2; |
| 68 | char reserved4[48]; |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 69 | uint hostver; /* Host controller version register */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 70 | char reserved5[4]; /* reserved */ |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 71 | uint dmaerraddr; /* DMA error address register */ |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 72 | char reserved6[4]; /* reserved */ |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 73 | uint dmaerrattr; /* DMA error attribute register */ |
| 74 | char reserved7[4]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 75 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 76 | char reserved8[8]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 77 | uint tcr; /* Tuning control register */ |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 78 | char reserved9[28]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 79 | uint sddirctl; /* SD direction control register */ |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 80 | char reserved10[712];/* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 81 | uint scr; /* eSDHC control register */ |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 82 | }; |
| 83 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 84 | /** |
| 85 | * struct fsl_esdhc_priv |
| 86 | * |
| 87 | * @esdhc_regs: registers of the sdhc controller |
| 88 | * @sdhc_clk: Current clk of the sdhc controller |
| 89 | * @bus_width: bus width, 1bit, 4bit or 8bit |
| 90 | * @cfg: mmc config |
| 91 | * @mmc: mmc |
| 92 | * Following is used when Driver Model is enabled for MMC |
| 93 | * @dev: pointer for the device |
| 94 | * @non_removable: 0: removable; 1: non-removable |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 95 | * @wp_enable: 1: enable checking wp; 0: no check |
Peng Fan | 32a9179 | 2017-06-12 17:50:53 +0800 | [diff] [blame] | 96 | * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 97 | * @cd_gpio: gpio for card detection |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 98 | * @wp_gpio: gpio for write protection |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 99 | */ |
| 100 | struct fsl_esdhc_priv { |
| 101 | struct fsl_esdhc *esdhc_regs; |
| 102 | unsigned int sdhc_clk; |
| 103 | unsigned int bus_width; |
| 104 | struct mmc_config cfg; |
| 105 | struct mmc *mmc; |
| 106 | struct udevice *dev; |
| 107 | int non_removable; |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 108 | int wp_enable; |
Peng Fan | 32a9179 | 2017-06-12 17:50:53 +0800 | [diff] [blame] | 109 | int vs18_enable; |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 110 | #ifdef CONFIG_DM_GPIO |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 111 | struct gpio_desc cd_gpio; |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 112 | struct gpio_desc wp_gpio; |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 113 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 114 | }; |
| 115 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 116 | /* Return the XFERTYP flags for a given command and data packet */ |
Kim Phillips | eafa90a | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 117 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 118 | { |
| 119 | uint xfertyp = 0; |
| 120 | |
| 121 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 122 | xfertyp |= XFERTYP_DPSEL; |
| 123 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 124 | xfertyp |= XFERTYP_DMAEN; |
| 125 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 126 | if (data->blocks > 1) { |
| 127 | xfertyp |= XFERTYP_MSBSEL; |
| 128 | xfertyp |= XFERTYP_BCEN; |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 129 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 130 | xfertyp |= XFERTYP_AC12EN; |
| 131 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | if (data->flags & MMC_DATA_READ) |
| 135 | xfertyp |= XFERTYP_DTDSEL; |
| 136 | } |
| 137 | |
| 138 | if (cmd->resp_type & MMC_RSP_CRC) |
| 139 | xfertyp |= XFERTYP_CCCEN; |
| 140 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 141 | xfertyp |= XFERTYP_CICEN; |
| 142 | if (cmd->resp_type & MMC_RSP_136) |
| 143 | xfertyp |= XFERTYP_RSPTYP_136; |
| 144 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 145 | xfertyp |= XFERTYP_RSPTYP_48_BUSY; |
| 146 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 147 | xfertyp |= XFERTYP_RSPTYP_48; |
| 148 | |
Jason Liu | 4571de3 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 149 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 150 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
Yangbo Lu | 2550344 | 2016-01-21 17:33:19 +0800 | [diff] [blame] | 151 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 152 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 153 | } |
| 154 | |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 155 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 156 | /* |
| 157 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 158 | */ |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 159 | static void |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 160 | esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data) |
| 161 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 162 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 163 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 164 | uint blocks; |
| 165 | char *buffer; |
| 166 | uint databuf; |
| 167 | uint size; |
| 168 | uint irqstat; |
| 169 | uint timeout; |
| 170 | |
| 171 | if (data->flags & MMC_DATA_READ) { |
| 172 | blocks = data->blocks; |
| 173 | buffer = data->dest; |
| 174 | while (blocks) { |
| 175 | timeout = PIO_TIMEOUT; |
| 176 | size = data->blocksize; |
| 177 | irqstat = esdhc_read32(®s->irqstat); |
| 178 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN) |
| 179 | && --timeout); |
| 180 | if (timeout <= 0) { |
| 181 | printf("\nData Read Failed in PIO Mode."); |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 182 | return; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 183 | } |
| 184 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 185 | udelay(100); /* Wait before last byte transfer complete */ |
| 186 | irqstat = esdhc_read32(®s->irqstat); |
| 187 | databuf = in_le32(®s->datport); |
| 188 | *((uint *)buffer) = databuf; |
| 189 | buffer += 4; |
| 190 | size -= 4; |
| 191 | } |
| 192 | blocks--; |
| 193 | } |
| 194 | } else { |
| 195 | blocks = data->blocks; |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 196 | buffer = (char *)data->src; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 197 | while (blocks) { |
| 198 | timeout = PIO_TIMEOUT; |
| 199 | size = data->blocksize; |
| 200 | irqstat = esdhc_read32(®s->irqstat); |
| 201 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN) |
| 202 | && --timeout); |
| 203 | if (timeout <= 0) { |
| 204 | printf("\nData Write Failed in PIO Mode."); |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 205 | return; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 206 | } |
| 207 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 208 | udelay(100); /* Wait before last byte transfer complete */ |
| 209 | databuf = *((uint *)buffer); |
| 210 | buffer += 4; |
| 211 | size -= 4; |
| 212 | irqstat = esdhc_read32(®s->irqstat); |
| 213 | out_le32(®s->datport, databuf); |
| 214 | } |
| 215 | blocks--; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | #endif |
| 220 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 221 | static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data) |
| 222 | { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 223 | int timeout; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 224 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 225 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Eddy Petrișor | 9702ec0 | 2016-06-05 03:43:00 +0300 | [diff] [blame] | 226 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 227 | dma_addr_t addr; |
| 228 | #endif |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 229 | uint wml_value; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 230 | |
| 231 | wml_value = data->blocksize/4; |
| 232 | |
| 233 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 234 | if (wml_value > WML_RD_WML_MAX) |
| 235 | wml_value = WML_RD_WML_MAX_VAL; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 236 | |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 237 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 238 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eddy Petrișor | 9702ec0 | 2016-06-05 03:43:00 +0300 | [diff] [blame] | 239 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 240 | addr = virt_to_phys((void *)(data->dest)); |
| 241 | if (upper_32_bits(addr)) |
| 242 | printf("Error found for upper 32 bits\n"); |
| 243 | else |
| 244 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 245 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 246 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 247 | #endif |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 248 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 249 | } else { |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 250 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 251 | flush_dcache_range((ulong)data->src, |
| 252 | (ulong)data->src+data->blocks |
| 253 | *data->blocksize); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 254 | #endif |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 255 | if (wml_value > WML_WR_WML_MAX) |
| 256 | wml_value = WML_WR_WML_MAX_VAL; |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 257 | if (priv->wp_enable) { |
| 258 | if ((esdhc_read32(®s->prsstat) & |
| 259 | PRSSTAT_WPSPL) == 0) { |
| 260 | printf("\nThe SD card is locked. Can not write to a locked card.\n\n"); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 261 | return -ETIMEDOUT; |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 262 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 263 | } |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 264 | |
| 265 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 266 | wml_value << 16); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 267 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eddy Petrișor | 9702ec0 | 2016-06-05 03:43:00 +0300 | [diff] [blame] | 268 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 269 | addr = virt_to_phys((void *)(data->src)); |
| 270 | if (upper_32_bits(addr)) |
| 271 | printf("Error found for upper 32 bits\n"); |
| 272 | else |
| 273 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 274 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 275 | esdhc_write32(®s->dsaddr, (u32)data->src); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 276 | #endif |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 277 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 278 | } |
| 279 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 280 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 281 | |
| 282 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 283 | /* |
| 284 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 285 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 286 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 287 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 288 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 289 | * As 1) >= 2) |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 290 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 291 | * Taking log2 both the sides |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 292 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 293 | * Rounding up to next power of 2 |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 294 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 295 | * => timeout + 13 = fls(mmc->clock/4) |
Yangbo Lu | e978a31 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 296 | * |
| 297 | * However, the MMC spec "It is strongly recommended for hosts to |
| 298 | * implement more than 500ms timeout value even if the card |
| 299 | * indicates the 250ms maximum busy length." Even the previous |
| 300 | * value of 300ms is known to be insufficient for some cards. |
| 301 | * So, we use |
| 302 | * => timeout + 13 = fls(mmc->clock/2) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 303 | */ |
Yangbo Lu | e978a31 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 304 | timeout = fls(mmc->clock/2); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 305 | timeout -= 13; |
| 306 | |
| 307 | if (timeout > 14) |
| 308 | timeout = 14; |
| 309 | |
| 310 | if (timeout < 0) |
| 311 | timeout = 0; |
| 312 | |
Kumar Gala | 5103a03 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 313 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 314 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 315 | timeout++; |
| 316 | #endif |
| 317 | |
Haijun.Zhang | 1336e2d | 2014-03-18 17:04:23 +0800 | [diff] [blame] | 318 | #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE |
| 319 | timeout = 0xE; |
| 320 | #endif |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 321 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 326 | static void check_and_invalidate_dcache_range |
| 327 | (struct mmc_cmd *cmd, |
| 328 | struct mmc_data *data) { |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 329 | unsigned start = 0; |
Yangbo Lu | cc634e2 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 330 | unsigned end = 0; |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 331 | unsigned size = roundup(ARCH_DMA_MINALIGN, |
| 332 | data->blocks*data->blocksize); |
Eddy Petrișor | 9702ec0 | 2016-06-05 03:43:00 +0300 | [diff] [blame] | 333 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 334 | dma_addr_t addr; |
| 335 | |
| 336 | addr = virt_to_phys((void *)(data->dest)); |
| 337 | if (upper_32_bits(addr)) |
| 338 | printf("Error found for upper 32 bits\n"); |
| 339 | else |
| 340 | start = lower_32_bits(addr); |
Yangbo Lu | cc634e2 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 341 | #else |
| 342 | start = (unsigned)data->dest; |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 343 | #endif |
Yangbo Lu | cc634e2 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 344 | end = start + size; |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 345 | invalidate_dcache_range(start, end); |
| 346 | } |
Tom Rini | 10dc777 | 2014-05-23 09:19:05 -0400 | [diff] [blame] | 347 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 348 | /* |
| 349 | * Sends a command out on the bus. Takes the mmc pointer, |
| 350 | * a command pointer, and an optional data pointer. |
| 351 | */ |
| 352 | static int |
| 353 | esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 354 | { |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 355 | int err = 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 356 | uint xfertyp; |
| 357 | uint irqstat; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 358 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 359 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 360 | |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 361 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 362 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 363 | return 0; |
| 364 | #endif |
| 365 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 366 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 367 | |
| 368 | sync(); |
| 369 | |
| 370 | /* Wait for the bus to be idle */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 371 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 372 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 373 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 374 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 375 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 376 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 377 | |
| 378 | /* Wait at least 8 SD clock cycles before the next command */ |
| 379 | /* |
| 380 | * Note: This is way more than 8 cycles, but 1ms seems to |
| 381 | * resolve timing issues with some cards |
| 382 | */ |
| 383 | udelay(1000); |
| 384 | |
| 385 | /* Set up for a data transfer if we have one */ |
| 386 | if (data) { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 387 | err = esdhc_setup_data(mmc, data); |
| 388 | if(err) |
| 389 | return err; |
Peng Fan | 4683b22 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 390 | |
| 391 | if (data->flags & MMC_DATA_READ) |
| 392 | check_and_invalidate_dcache_range(cmd, data); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* Figure out the transfer arguments */ |
| 396 | xfertyp = esdhc_xfertyp(cmd, data); |
| 397 | |
Andrew Gabbasov | 01b7735 | 2013-06-11 10:34:22 -0500 | [diff] [blame] | 398 | /* Mask all irqs */ |
| 399 | esdhc_write32(®s->irqsigen, 0); |
| 400 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 401 | /* Send the command */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 402 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 403 | #if defined(CONFIG_FSL_USDHC) |
| 404 | esdhc_write32(®s->mixctrl, |
Volodymyr Riazantsev | 0e1bf61 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 405 | (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F) |
| 406 | | (mmc->ddr_mode ? XFERTYP_DDREN : 0)); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 407 | esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000); |
| 408 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 409 | esdhc_write32(®s->xfertyp, xfertyp); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 410 | #endif |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 411 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 412 | /* Wait for the command to complete */ |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 413 | while (!(esdhc_read32(®s->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE))) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 414 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 415 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 416 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 417 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 418 | if (irqstat & CMD_ERR) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 419 | err = -ECOMM; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 420 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 423 | if (irqstat & IRQSTAT_CTOE) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 424 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 425 | goto out; |
| 426 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 427 | |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 428 | /* Switch voltage to 1.8V if CMD11 succeeded */ |
| 429 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) { |
| 430 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 431 | |
| 432 | printf("Run CMD11 1.8V switch\n"); |
| 433 | /* Sleep for 5 ms - max time for card to switch to 1.8V */ |
| 434 | udelay(5000); |
| 435 | } |
| 436 | |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 437 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 438 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
Yangbo Lu | 253d5bd | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 439 | int timeout = 6000; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 440 | |
Yangbo Lu | 253d5bd | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 441 | /* Poll on DATA0 line for cmd with busy signal for 600 ms */ |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 442 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 443 | PRSSTAT_DAT0)) { |
| 444 | udelay(100); |
| 445 | timeout--; |
| 446 | } |
| 447 | |
| 448 | if (timeout <= 0) { |
| 449 | printf("Timeout waiting for DAT0 to go high!\n"); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 450 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 451 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 455 | /* Copy the response to the response buffer */ |
| 456 | if (cmd->resp_type & MMC_RSP_136) { |
| 457 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 458 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 459 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 460 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 461 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 462 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 463 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 464 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 465 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 466 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 467 | } else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 468 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 469 | |
| 470 | /* Wait until all of the blocks are transferred */ |
| 471 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 472 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 473 | esdhc_pio_read_write(mmc, data); |
| 474 | #else |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 475 | do { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 476 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 477 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 478 | if (irqstat & IRQSTAT_DTOE) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 479 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 480 | goto out; |
| 481 | } |
Frans Meulenbroeks | 63fb5a7 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 482 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 483 | if (irqstat & DATA_ERR) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 484 | err = -ECOMM; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 485 | goto out; |
| 486 | } |
Andrew Gabbasov | 9b74dc5 | 2013-04-07 23:06:08 +0000 | [diff] [blame] | 487 | } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 488 | |
Peng Fan | 4683b22 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 489 | /* |
| 490 | * Need invalidate the dcache here again to avoid any |
| 491 | * cache-fill during the DMA operations such as the |
| 492 | * speculative pre-fetching etc. |
| 493 | */ |
Eric Nelson | 54899fc | 2013-04-03 12:31:56 +0000 | [diff] [blame] | 494 | if (data->flags & MMC_DATA_READ) |
| 495 | check_and_invalidate_dcache_range(cmd, data); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 496 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 497 | } |
| 498 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 499 | out: |
| 500 | /* Reset CMD and DATA portions on error */ |
| 501 | if (err) { |
| 502 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 503 | SYSCTL_RSTC); |
| 504 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 505 | ; |
| 506 | |
| 507 | if (data) { |
| 508 | esdhc_write32(®s->sysctl, |
| 509 | esdhc_read32(®s->sysctl) | |
| 510 | SYSCTL_RSTD); |
| 511 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 512 | ; |
| 513 | } |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 514 | |
| 515 | /* If this was CMD11, then notify that power cycle is needed */ |
| 516 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) |
| 517 | 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] | 518 | } |
| 519 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 520 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 521 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 522 | return err; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Kim Phillips | eafa90a | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 525 | static void set_sysctl(struct mmc *mmc, uint clock) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 526 | { |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 527 | int div = 1; |
| 528 | #ifdef ARCH_MXC |
| 529 | int pre_div = 1; |
| 530 | #else |
| 531 | int pre_div = 2; |
| 532 | #endif |
| 533 | int ddr_pre_div = mmc->ddr_mode ? 2 : 1; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 534 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 535 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 536 | int sdhc_clk = priv->sdhc_clk; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 537 | uint clk; |
| 538 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 539 | if (clock < mmc->cfg->f_min) |
| 540 | clock = mmc->cfg->f_min; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 541 | |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 542 | while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256) |
| 543 | pre_div *= 2; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 544 | |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 545 | while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16) |
| 546 | div++; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 547 | |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 548 | pre_div >>= 1; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 549 | div -= 1; |
| 550 | |
| 551 | clk = (pre_div << 8) | (div << 4); |
| 552 | |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 553 | #ifdef CONFIG_FSL_USDHC |
Ye Li | 84ecdf6 | 2016-06-15 10:53:01 +0800 | [diff] [blame] | 554 | esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN); |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 555 | #else |
Kumar Gala | cc4d122 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 556 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 557 | #endif |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 558 | |
| 559 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 560 | |
| 561 | udelay(10000); |
| 562 | |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 563 | #ifdef CONFIG_FSL_USDHC |
Ye Li | 84ecdf6 | 2016-06-15 10:53:01 +0800 | [diff] [blame] | 564 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN); |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 565 | #else |
| 566 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
| 567 | #endif |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 568 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 569 | } |
| 570 | |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 571 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 572 | static void esdhc_clock_control(struct mmc *mmc, bool enable) |
| 573 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 574 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 575 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 576 | u32 value; |
| 577 | u32 time_out; |
| 578 | |
| 579 | value = esdhc_read32(®s->sysctl); |
| 580 | |
| 581 | if (enable) |
| 582 | value |= SYSCTL_CKEN; |
| 583 | else |
| 584 | value &= ~SYSCTL_CKEN; |
| 585 | |
| 586 | esdhc_write32(®s->sysctl, value); |
| 587 | |
| 588 | time_out = 20; |
| 589 | value = PRSSTAT_SDSTB; |
| 590 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 591 | if (time_out == 0) { |
| 592 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 593 | break; |
| 594 | } |
| 595 | time_out--; |
| 596 | mdelay(1); |
| 597 | } |
| 598 | } |
| 599 | #endif |
| 600 | |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 601 | static int esdhc_set_ios(struct mmc *mmc) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 602 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 603 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 604 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 605 | |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 606 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 607 | /* Select to use peripheral clock */ |
| 608 | esdhc_clock_control(mmc, false); |
| 609 | esdhc_setbits32(®s->scr, ESDHCCTL_PCS); |
| 610 | esdhc_clock_control(mmc, true); |
| 611 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 612 | /* Set the clock speed */ |
| 613 | set_sysctl(mmc, mmc->clock); |
| 614 | |
| 615 | /* Set the bus width */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 616 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 617 | |
| 618 | if (mmc->bus_width == 4) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 619 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 620 | else if (mmc->bus_width == 8) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 621 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 622 | |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 623 | return 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static int esdhc_init(struct mmc *mmc) |
| 627 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 628 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 629 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 630 | int timeout = 1000; |
| 631 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 632 | /* Reset the entire host controller */ |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 633 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 634 | |
| 635 | /* Wait until the controller is available */ |
| 636 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 637 | udelay(1000); |
| 638 | |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 639 | #if defined(CONFIG_FSL_USDHC) |
| 640 | /* RSTA doesn't reset MMC_BOOT register, so manually reset it */ |
| 641 | esdhc_write32(®s->mmcboot, 0x0); |
| 642 | /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */ |
| 643 | esdhc_write32(®s->mixctrl, 0x0); |
| 644 | esdhc_write32(®s->clktunectrlstatus, 0x0); |
| 645 | |
| 646 | /* Put VEND_SPEC to default value */ |
| 647 | esdhc_write32(®s->vendorspec, VENDORSPEC_INIT); |
| 648 | |
| 649 | /* Disable DLL_CTRL delay line */ |
| 650 | esdhc_write32(®s->dllctrl, 0x0); |
| 651 | #endif |
| 652 | |
Benoît Thébaudeau | 16e43f3 | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 653 | #ifndef ARCH_MXC |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 654 | /* Enable cache snooping */ |
Benoît Thébaudeau | 16e43f3 | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 655 | esdhc_write32(®s->scr, 0x00000040); |
| 656 | #endif |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 657 | |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 658 | #ifndef CONFIG_FSL_USDHC |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 659 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Ye Li | 84ecdf6 | 2016-06-15 10:53:01 +0800 | [diff] [blame] | 660 | #else |
| 661 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN); |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 662 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 663 | |
| 664 | /* Set the initial clock speed */ |
Jerry Huang | 4a6ee17 | 2010-11-25 17:06:07 +0000 | [diff] [blame] | 665 | mmc_set_clock(mmc, 400000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 666 | |
| 667 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 668 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 669 | |
| 670 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 671 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 672 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 673 | /* Set timout to the maximum value */ |
| 674 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 675 | |
Otavio Salvador | ee0c538 | 2015-02-17 10:42:44 -0200 | [diff] [blame] | 676 | #ifdef CONFIG_SYS_FSL_ESDHC_FORCE_VSELECT |
| 677 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 678 | #endif |
| 679 | |
Peng Fan | 32a9179 | 2017-06-12 17:50:53 +0800 | [diff] [blame] | 680 | if (priv->vs18_enable) |
| 681 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 682 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 683 | return 0; |
| 684 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 685 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 686 | static int esdhc_getcd(struct mmc *mmc) |
| 687 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 688 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 689 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 690 | int timeout = 1000; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 691 | |
Haijun.Zhang | f7e27cc | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 692 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 693 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 694 | return 1; |
| 695 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 696 | |
| 697 | #ifdef CONFIG_DM_MMC |
| 698 | if (priv->non_removable) |
| 699 | return 1; |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 700 | #ifdef CONFIG_DM_GPIO |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 701 | if (dm_gpio_is_valid(&priv->cd_gpio)) |
| 702 | return dm_gpio_get_value(&priv->cd_gpio); |
| 703 | #endif |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 704 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 705 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 706 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 707 | udelay(1000); |
| 708 | |
| 709 | return timeout > 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 710 | } |
| 711 | |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 712 | static void esdhc_reset(struct fsl_esdhc *regs) |
| 713 | { |
| 714 | unsigned long timeout = 100; /* wait max 100 ms */ |
| 715 | |
| 716 | /* reset the controller */ |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 717 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 718 | |
| 719 | /* hardware clears the bit when it is done */ |
| 720 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 721 | udelay(1000); |
| 722 | if (!timeout) |
| 723 | printf("MMC/SD: Reset never completed.\n"); |
| 724 | } |
| 725 | |
Pantelis Antoniou | ab769f2 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 726 | static const struct mmc_ops esdhc_ops = { |
| 727 | .send_cmd = esdhc_send_cmd, |
| 728 | .set_ios = esdhc_set_ios, |
| 729 | .init = esdhc_init, |
| 730 | .getcd = esdhc_getcd, |
| 731 | }; |
| 732 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 733 | static int fsl_esdhc_init(struct fsl_esdhc_priv *priv) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 734 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 735 | struct fsl_esdhc *regs; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 736 | struct mmc *mmc; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 737 | u32 caps, voltage_caps; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 738 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 739 | if (!priv) |
| 740 | return -EINVAL; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 741 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 742 | regs = priv->esdhc_regs; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 743 | |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 744 | /* First reset the eSDHC controller */ |
| 745 | esdhc_reset(regs); |
| 746 | |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 747 | #ifndef CONFIG_FSL_USDHC |
Jerry Huang | 975324a | 2012-05-17 23:57:02 +0000 | [diff] [blame] | 748 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
| 749 | | SYSCTL_IPGEN | SYSCTL_CKEN); |
Ye Li | 84ecdf6 | 2016-06-15 10:53:01 +0800 | [diff] [blame] | 750 | #else |
| 751 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | |
| 752 | VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN); |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 753 | #endif |
Jerry Huang | 975324a | 2012-05-17 23:57:02 +0000 | [diff] [blame] | 754 | |
Peng Fan | 32a9179 | 2017-06-12 17:50:53 +0800 | [diff] [blame] | 755 | if (priv->vs18_enable) |
| 756 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 757 | |
Ye.Li | a3d6e38 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 758 | writel(SDHCI_IRQ_EN_BITS, ®s->irqstaten); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 759 | memset(&priv->cfg, 0, sizeof(priv->cfg)); |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 760 | |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 761 | voltage_caps = 0; |
Wang Huan | 19060bd | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 762 | caps = esdhc_read32(®s->hostcapblt); |
Roy Zang | 3b4456e | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 763 | |
| 764 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
| 765 | caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | |
| 766 | ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); |
| 767 | #endif |
Haijun.Zhang | ef38f3f | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 768 | |
| 769 | /* T4240 host controller capabilities register should have VS33 bit */ |
| 770 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
| 771 | caps = caps | ESDHC_HOSTCAPBLT_VS33; |
| 772 | #endif |
| 773 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 774 | if (caps & ESDHC_HOSTCAPBLT_VS18) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 775 | voltage_caps |= MMC_VDD_165_195; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 776 | if (caps & ESDHC_HOSTCAPBLT_VS30) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 777 | voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 778 | if (caps & ESDHC_HOSTCAPBLT_VS33) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 779 | voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; |
| 780 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 781 | priv->cfg.name = "FSL_SDHC"; |
| 782 | priv->cfg.ops = &esdhc_ops; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 783 | #ifdef CONFIG_SYS_SD_VOLTAGE |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 784 | priv->cfg.voltages = CONFIG_SYS_SD_VOLTAGE; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 785 | #else |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 786 | priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 787 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 788 | if ((priv->cfg.voltages & voltage_caps) == 0) { |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 789 | printf("voltage not supported by controller\n"); |
| 790 | return -1; |
| 791 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 792 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 793 | if (priv->bus_width == 8) |
| 794 | priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
| 795 | else if (priv->bus_width == 4) |
| 796 | priv->cfg.host_caps = MMC_MODE_4BIT; |
| 797 | |
| 798 | priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
Volodymyr Riazantsev | 0e1bf61 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 799 | #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 800 | priv->cfg.host_caps |= MMC_MODE_DDR_52MHz; |
Volodymyr Riazantsev | 0e1bf61 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 801 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 802 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 803 | if (priv->bus_width > 0) { |
| 804 | if (priv->bus_width < 8) |
| 805 | priv->cfg.host_caps &= ~MMC_MODE_8BIT; |
| 806 | if (priv->bus_width < 4) |
| 807 | priv->cfg.host_caps &= ~MMC_MODE_4BIT; |
Abbas Raza | aad4659 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 810 | if (caps & ESDHC_HOSTCAPBLT_HSS) |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 811 | priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 812 | |
Haijun.Zhang | d47e3d2 | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 813 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 814 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 815 | priv->cfg.host_caps &= ~MMC_MODE_8BIT; |
Haijun.Zhang | d47e3d2 | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 816 | #endif |
| 817 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 818 | priv->cfg.f_min = 400000; |
| 819 | priv->cfg.f_max = min(priv->sdhc_clk, (u32)52000000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 820 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 821 | priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 822 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 823 | mmc = mmc_create(&priv->cfg, priv); |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 824 | if (mmc == NULL) |
| 825 | return -1; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 826 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 827 | priv->mmc = mmc; |
| 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | |
Jagan Teki | 2e87c44 | 2017-05-12 17:18:20 +0530 | [diff] [blame] | 832 | #ifndef CONFIG_DM_MMC |
| 833 | static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg, |
| 834 | struct fsl_esdhc_priv *priv) |
| 835 | { |
| 836 | if (!cfg || !priv) |
| 837 | return -EINVAL; |
| 838 | |
| 839 | priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); |
| 840 | priv->bus_width = cfg->max_bus_width; |
| 841 | priv->sdhc_clk = cfg->sdhc_clk; |
| 842 | priv->wp_enable = cfg->wp_enable; |
Peng Fan | 32a9179 | 2017-06-12 17:50:53 +0800 | [diff] [blame] | 843 | priv->vs18_enable = cfg->vs18_enable; |
Jagan Teki | 2e87c44 | 2017-05-12 17:18:20 +0530 | [diff] [blame] | 844 | |
| 845 | return 0; |
| 846 | }; |
| 847 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 848 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
| 849 | { |
| 850 | struct fsl_esdhc_priv *priv; |
| 851 | int ret; |
| 852 | |
| 853 | if (!cfg) |
| 854 | return -EINVAL; |
| 855 | |
| 856 | priv = calloc(sizeof(struct fsl_esdhc_priv), 1); |
| 857 | if (!priv) |
| 858 | return -ENOMEM; |
| 859 | |
| 860 | ret = fsl_esdhc_cfg_to_priv(cfg, priv); |
| 861 | if (ret) { |
| 862 | debug("%s xlate failure\n", __func__); |
| 863 | free(priv); |
| 864 | return ret; |
| 865 | } |
| 866 | |
| 867 | ret = fsl_esdhc_init(priv); |
| 868 | if (ret) { |
| 869 | debug("%s init failure\n", __func__); |
| 870 | free(priv); |
| 871 | return ret; |
| 872 | } |
| 873 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 878 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 879 | struct fsl_esdhc_cfg *cfg; |
| 880 | |
Fabio Estevam | 88227a1 | 2012-12-27 08:51:08 +0000 | [diff] [blame] | 881 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 882 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
Simon Glass | e9adeca | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 883 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 884 | return fsl_esdhc_initialize(bis, cfg); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 885 | } |
Jagan Teki | 2e87c44 | 2017-05-12 17:18:20 +0530 | [diff] [blame] | 886 | #endif |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 887 | |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 888 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 889 | void mmc_adapter_card_type_ident(void) |
| 890 | { |
| 891 | u8 card_id; |
| 892 | u8 value; |
| 893 | |
| 894 | card_id = QIXIS_READ(present) & QIXIS_SDID_MASK; |
| 895 | gd->arch.sdhc_adapter = card_id; |
| 896 | |
| 897 | switch (card_id) { |
| 898 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45: |
Yangbo Lu | cdc6955 | 2015-09-17 10:27:12 +0800 | [diff] [blame] | 899 | value = QIXIS_READ(brdcfg[5]); |
| 900 | value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7); |
| 901 | QIXIS_WRITE(brdcfg[5], value); |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 902 | break; |
| 903 | case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY: |
Yangbo Lu | bf50be8 | 2015-09-17 10:27:48 +0800 | [diff] [blame] | 904 | value = QIXIS_READ(pwr_ctl[1]); |
| 905 | value |= QIXIS_EVDD_BY_SDHC_VS; |
| 906 | QIXIS_WRITE(pwr_ctl[1], value); |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 907 | break; |
| 908 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44: |
| 909 | value = QIXIS_READ(brdcfg[5]); |
| 910 | value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT); |
| 911 | QIXIS_WRITE(brdcfg[5], value); |
| 912 | break; |
| 913 | case QIXIS_ESDHC_ADAPTER_TYPE_RSV: |
| 914 | break; |
| 915 | case QIXIS_ESDHC_ADAPTER_TYPE_MMC: |
| 916 | break; |
| 917 | case QIXIS_ESDHC_ADAPTER_TYPE_SD: |
| 918 | break; |
| 919 | case QIXIS_ESDHC_NO_ADAPTER: |
| 920 | break; |
| 921 | default: |
| 922 | break; |
| 923 | } |
| 924 | } |
| 925 | #endif |
| 926 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 927 | #ifdef CONFIG_OF_LIBFDT |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 928 | __weak int esdhc_status_fixup(void *blob, const char *compat) |
| 929 | { |
| 930 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
| 931 | if (!hwconfig("esdhc")) { |
| 932 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 933 | sizeof("disabled"), 1); |
| 934 | return 1; |
| 935 | } |
| 936 | #endif |
| 937 | do_fixup_by_compat(blob, compat, "status", "okay", |
| 938 | sizeof("okay"), 1); |
| 939 | return 0; |
| 940 | } |
| 941 | |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 942 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 943 | { |
| 944 | const char *compat = "fsl,esdhc"; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 945 | |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 946 | if (esdhc_status_fixup(blob, compat)) |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 947 | return; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 948 | |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 949 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 950 | do_fixup_by_compat_u32(blob, compat, "peripheral-frequency", |
| 951 | gd->arch.sdhc_clk, 1); |
| 952 | #else |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 953 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | e9adeca | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 954 | gd->arch.sdhc_clk, 1); |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 955 | #endif |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 956 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 957 | do_fixup_by_compat_u32(blob, compat, "adapter-type", |
| 958 | (u32)(gd->arch.sdhc_adapter), 1); |
| 959 | #endif |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 960 | } |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 961 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 962 | |
| 963 | #ifdef CONFIG_DM_MMC |
| 964 | #include <asm/arch/clock.h> |
Peng Fan | b60f145 | 2017-02-22 16:21:55 +0800 | [diff] [blame] | 965 | __weak void init_clk_usdhc(u32 index) |
| 966 | { |
| 967 | } |
| 968 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 969 | static int fsl_esdhc_probe(struct udevice *dev) |
| 970 | { |
| 971 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 972 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 973 | const void *fdt = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 974 | int node = dev_of_offset(dev); |
Peng Fan | 4483b7e | 2017-06-12 17:50:54 +0800 | [diff] [blame^] | 975 | struct udevice *vqmmc_dev; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 976 | fdt_addr_t addr; |
| 977 | unsigned int val; |
| 978 | int ret; |
| 979 | |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 980 | addr = devfdt_get_addr(dev); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 981 | if (addr == FDT_ADDR_T_NONE) |
| 982 | return -EINVAL; |
| 983 | |
| 984 | priv->esdhc_regs = (struct fsl_esdhc *)addr; |
| 985 | priv->dev = dev; |
| 986 | |
| 987 | val = fdtdec_get_int(fdt, node, "bus-width", -1); |
| 988 | if (val == 8) |
| 989 | priv->bus_width = 8; |
| 990 | else if (val == 4) |
| 991 | priv->bus_width = 4; |
| 992 | else |
| 993 | priv->bus_width = 1; |
| 994 | |
| 995 | if (fdt_get_property(fdt, node, "non-removable", NULL)) { |
| 996 | priv->non_removable = 1; |
| 997 | } else { |
| 998 | priv->non_removable = 0; |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 999 | #ifdef CONFIG_DM_GPIO |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1000 | gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios", |
| 1001 | 0, &priv->cd_gpio, GPIOD_IS_IN); |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 1002 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 1005 | priv->wp_enable = 1; |
| 1006 | |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 1007 | #ifdef CONFIG_DM_GPIO |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 1008 | ret = gpio_request_by_name_nodev(offset_to_ofnode(node), "wp-gpios", 0, |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 1009 | &priv->wp_gpio, GPIOD_IS_IN); |
| 1010 | if (ret) |
| 1011 | priv->wp_enable = 0; |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 1012 | #endif |
Peng Fan | 4483b7e | 2017-06-12 17:50:54 +0800 | [diff] [blame^] | 1013 | |
| 1014 | priv->vs18_enable = 0; |
| 1015 | |
| 1016 | #ifdef CONFIG_DM_REGULATOR |
| 1017 | /* |
| 1018 | * If emmc I/O has a fixed voltage at 1.8V, this must be provided, |
| 1019 | * otherwise, emmc will work abnormally. |
| 1020 | */ |
| 1021 | ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev); |
| 1022 | if (ret) { |
| 1023 | dev_dbg(dev, "no vqmmc-supply\n"); |
| 1024 | } else { |
| 1025 | ret = regulator_set_enable(vqmmc_dev, true); |
| 1026 | if (ret) { |
| 1027 | dev_err(dev, "fail to enable vqmmc-supply\n"); |
| 1028 | return ret; |
| 1029 | } |
| 1030 | |
| 1031 | if (regulator_get_value(vqmmc_dev) == 1800000) |
| 1032 | priv->vs18_enable = 1; |
| 1033 | } |
| 1034 | #endif |
| 1035 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1036 | /* |
| 1037 | * TODO: |
| 1038 | * Because lack of clk driver, if SDHC clk is not enabled, |
| 1039 | * need to enable it first before this driver is invoked. |
| 1040 | * |
| 1041 | * we use MXC_ESDHC_CLK to get clk freq. |
| 1042 | * If one would like to make this function work, |
| 1043 | * the aliases should be provided in dts as this: |
| 1044 | * |
| 1045 | * aliases { |
| 1046 | * mmc0 = &usdhc1; |
| 1047 | * mmc1 = &usdhc2; |
| 1048 | * mmc2 = &usdhc3; |
| 1049 | * mmc3 = &usdhc4; |
| 1050 | * }; |
| 1051 | * Then if your board only supports mmc2 and mmc3, but we can |
| 1052 | * correctly get the seq as 2 and 3, then let mxc_get_clock |
| 1053 | * work as expected. |
| 1054 | */ |
Peng Fan | b60f145 | 2017-02-22 16:21:55 +0800 | [diff] [blame] | 1055 | |
| 1056 | init_clk_usdhc(dev->seq); |
| 1057 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1058 | priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq); |
| 1059 | if (priv->sdhc_clk <= 0) { |
| 1060 | dev_err(dev, "Unable to get clk for %s\n", dev->name); |
| 1061 | return -EINVAL; |
| 1062 | } |
| 1063 | |
| 1064 | ret = fsl_esdhc_init(priv); |
| 1065 | if (ret) { |
| 1066 | dev_err(dev, "fsl_esdhc_init failure\n"); |
| 1067 | return ret; |
| 1068 | } |
| 1069 | |
| 1070 | upriv->mmc = priv->mmc; |
Peng Fan | 35ae994 | 2016-08-11 14:02:56 +0800 | [diff] [blame] | 1071 | priv->mmc->dev = dev; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1072 | |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | static const struct udevice_id fsl_esdhc_ids[] = { |
| 1077 | { .compatible = "fsl,imx6ul-usdhc", }, |
| 1078 | { .compatible = "fsl,imx6sx-usdhc", }, |
| 1079 | { .compatible = "fsl,imx6sl-usdhc", }, |
| 1080 | { .compatible = "fsl,imx6q-usdhc", }, |
| 1081 | { .compatible = "fsl,imx7d-usdhc", }, |
Peng Fan | b60f145 | 2017-02-22 16:21:55 +0800 | [diff] [blame] | 1082 | { .compatible = "fsl,imx7ulp-usdhc", }, |
Yangbo Lu | a6473f8 | 2016-12-07 11:54:31 +0800 | [diff] [blame] | 1083 | { .compatible = "fsl,esdhc", }, |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1084 | { /* sentinel */ } |
| 1085 | }; |
| 1086 | |
| 1087 | U_BOOT_DRIVER(fsl_esdhc) = { |
| 1088 | .name = "fsl-esdhc-mmc", |
| 1089 | .id = UCLASS_MMC, |
| 1090 | .of_match = fsl_esdhc_ids, |
| 1091 | .probe = fsl_esdhc_probe, |
| 1092 | .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv), |
| 1093 | }; |
| 1094 | #endif |