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