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