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> |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 26 | #include <dm/pinctrl.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 27 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Ye.Li | a3d6e38 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 30 | #define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \ |
| 31 | IRQSTATEN_CINT | \ |
| 32 | IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \ |
| 33 | IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \ |
| 34 | IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \ |
| 35 | IRQSTATEN_DINT) |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 36 | #define MAX_TUNING_LOOP 40 |
Ye.Li | a3d6e38 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 37 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 38 | struct fsl_esdhc { |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 39 | uint dsaddr; /* SDMA system address register */ |
| 40 | uint blkattr; /* Block attributes register */ |
| 41 | uint cmdarg; /* Command argument register */ |
| 42 | uint xfertyp; /* Transfer type register */ |
| 43 | uint cmdrsp0; /* Command response 0 register */ |
| 44 | uint cmdrsp1; /* Command response 1 register */ |
| 45 | uint cmdrsp2; /* Command response 2 register */ |
| 46 | uint cmdrsp3; /* Command response 3 register */ |
| 47 | uint datport; /* Buffer data port register */ |
| 48 | uint prsstat; /* Present state register */ |
| 49 | uint proctl; /* Protocol control register */ |
| 50 | uint sysctl; /* System Control Register */ |
| 51 | uint irqstat; /* Interrupt status register */ |
| 52 | uint irqstaten; /* Interrupt status enable register */ |
| 53 | uint irqsigen; /* Interrupt signal enable register */ |
| 54 | uint autoc12err; /* Auto CMD error status register */ |
| 55 | uint hostcapblt; /* Host controller capabilities register */ |
| 56 | uint wml; /* Watermark level register */ |
| 57 | uint mixctrl; /* For USDHC */ |
| 58 | char reserved1[4]; /* reserved */ |
| 59 | uint fevt; /* Force event register */ |
| 60 | uint admaes; /* ADMA error status register */ |
| 61 | uint adsaddr; /* ADMA system address register */ |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 62 | char reserved2[4]; |
| 63 | uint dllctrl; |
| 64 | uint dllstat; |
| 65 | uint clktunectrlstatus; |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 66 | char reserved3[4]; |
| 67 | uint strobe_dllctrl; |
| 68 | uint strobe_dllstat; |
| 69 | char reserved4[72]; |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 70 | uint vendorspec; |
| 71 | uint mmcboot; |
| 72 | uint vendorspec2; |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 73 | uint tuning_ctrl; /* on i.MX6/7/8 */ |
| 74 | char reserved5[44]; |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 75 | uint hostver; /* Host controller version register */ |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 76 | char reserved6[4]; /* reserved */ |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 77 | uint dmaerraddr; /* DMA error address register */ |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 78 | char reserved7[4]; /* reserved */ |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 79 | uint dmaerrattr; /* DMA error attribute register */ |
| 80 | char reserved8[4]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 81 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 82 | char reserved9[8]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 83 | uint tcr; /* Tuning control register */ |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 84 | char reserved10[28]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 85 | uint sddirctl; /* SD direction control register */ |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 86 | char reserved11[712];/* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 87 | uint scr; /* eSDHC control register */ |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 88 | }; |
| 89 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 90 | struct fsl_esdhc_plat { |
| 91 | struct mmc_config cfg; |
| 92 | struct mmc mmc; |
| 93 | }; |
| 94 | |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 95 | struct esdhc_soc_data { |
| 96 | u32 flags; |
| 97 | u32 caps; |
| 98 | }; |
| 99 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 100 | /** |
| 101 | * struct fsl_esdhc_priv |
| 102 | * |
| 103 | * @esdhc_regs: registers of the sdhc controller |
| 104 | * @sdhc_clk: Current clk of the sdhc controller |
| 105 | * @bus_width: bus width, 1bit, 4bit or 8bit |
| 106 | * @cfg: mmc config |
| 107 | * @mmc: mmc |
| 108 | * Following is used when Driver Model is enabled for MMC |
| 109 | * @dev: pointer for the device |
| 110 | * @non_removable: 0: removable; 1: non-removable |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 111 | * @wp_enable: 1: enable checking wp; 0: no check |
Peng Fan | 32a9179 | 2017-06-12 17:50:53 +0800 | [diff] [blame] | 112 | * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 113 | * @flags: ESDHC_FLAG_xx in include/fsl_esdhc.h |
| 114 | * @caps: controller capabilities |
| 115 | * @tuning_step: tuning step setting in tuning_ctrl register |
| 116 | * @start_tuning_tap: the start point for tuning in tuning_ctrl register |
| 117 | * @strobe_dll_delay_target: settings in strobe_dllctrl |
| 118 | * @signal_voltage: indicating the current voltage |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 119 | * @cd_gpio: gpio for card detection |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 120 | * @wp_gpio: gpio for write protection |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 121 | */ |
| 122 | struct fsl_esdhc_priv { |
| 123 | struct fsl_esdhc *esdhc_regs; |
| 124 | unsigned int sdhc_clk; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 125 | unsigned int clock; |
| 126 | unsigned int mode; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 127 | unsigned int bus_width; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 128 | #if !CONFIG_IS_ENABLED(BLK) |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 129 | struct mmc *mmc; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 130 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 131 | struct udevice *dev; |
| 132 | int non_removable; |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 133 | int wp_enable; |
Peng Fan | 32a9179 | 2017-06-12 17:50:53 +0800 | [diff] [blame] | 134 | int vs18_enable; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 135 | u32 flags; |
| 136 | u32 caps; |
| 137 | u32 tuning_step; |
| 138 | u32 tuning_start_tap; |
| 139 | u32 strobe_dll_delay_target; |
| 140 | u32 signal_voltage; |
| 141 | #if IS_ENABLED(CONFIG_DM_REGULATOR) |
| 142 | struct udevice *vqmmc_dev; |
| 143 | struct udevice *vmmc_dev; |
| 144 | #endif |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 145 | #ifdef CONFIG_DM_GPIO |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 146 | struct gpio_desc cd_gpio; |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 147 | struct gpio_desc wp_gpio; |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 148 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 149 | }; |
| 150 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 151 | /* Return the XFERTYP flags for a given command and data packet */ |
Kim Phillips | eafa90a | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 152 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 153 | { |
| 154 | uint xfertyp = 0; |
| 155 | |
| 156 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 157 | xfertyp |= XFERTYP_DPSEL; |
| 158 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 159 | xfertyp |= XFERTYP_DMAEN; |
| 160 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 161 | if (data->blocks > 1) { |
| 162 | xfertyp |= XFERTYP_MSBSEL; |
| 163 | xfertyp |= XFERTYP_BCEN; |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 164 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 165 | xfertyp |= XFERTYP_AC12EN; |
| 166 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (data->flags & MMC_DATA_READ) |
| 170 | xfertyp |= XFERTYP_DTDSEL; |
| 171 | } |
| 172 | |
| 173 | if (cmd->resp_type & MMC_RSP_CRC) |
| 174 | xfertyp |= XFERTYP_CCCEN; |
| 175 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 176 | xfertyp |= XFERTYP_CICEN; |
| 177 | if (cmd->resp_type & MMC_RSP_136) |
| 178 | xfertyp |= XFERTYP_RSPTYP_136; |
| 179 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 180 | xfertyp |= XFERTYP_RSPTYP_48_BUSY; |
| 181 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 182 | xfertyp |= XFERTYP_RSPTYP_48; |
| 183 | |
Jason Liu | 4571de3 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 184 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 185 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
Yangbo Lu | 2550344 | 2016-01-21 17:33:19 +0800 | [diff] [blame] | 186 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 187 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 188 | } |
| 189 | |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 190 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 191 | /* |
| 192 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 193 | */ |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 194 | static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv, |
| 195 | struct mmc_data *data) |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 196 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 197 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 198 | uint blocks; |
| 199 | char *buffer; |
| 200 | uint databuf; |
| 201 | uint size; |
| 202 | uint irqstat; |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 203 | ulong start; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 204 | |
| 205 | if (data->flags & MMC_DATA_READ) { |
| 206 | blocks = data->blocks; |
| 207 | buffer = data->dest; |
| 208 | while (blocks) { |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 209 | start = get_timer(0); |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 210 | size = data->blocksize; |
| 211 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 212 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) { |
| 213 | if (get_timer(start) > PIO_TIMEOUT) { |
| 214 | printf("\nData Read Failed in PIO Mode."); |
| 215 | return; |
| 216 | } |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 217 | } |
| 218 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 219 | udelay(100); /* Wait before last byte transfer complete */ |
| 220 | irqstat = esdhc_read32(®s->irqstat); |
| 221 | databuf = in_le32(®s->datport); |
| 222 | *((uint *)buffer) = databuf; |
| 223 | buffer += 4; |
| 224 | size -= 4; |
| 225 | } |
| 226 | blocks--; |
| 227 | } |
| 228 | } else { |
| 229 | blocks = data->blocks; |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 230 | buffer = (char *)data->src; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 231 | while (blocks) { |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 232 | start = get_timer(0); |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 233 | size = data->blocksize; |
| 234 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 235 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) { |
| 236 | if (get_timer(start) > PIO_TIMEOUT) { |
| 237 | printf("\nData Write Failed in PIO Mode."); |
| 238 | return; |
| 239 | } |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 240 | } |
| 241 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 242 | udelay(100); /* Wait before last byte transfer complete */ |
| 243 | databuf = *((uint *)buffer); |
| 244 | buffer += 4; |
| 245 | size -= 4; |
| 246 | irqstat = esdhc_read32(®s->irqstat); |
| 247 | out_le32(®s->datport, databuf); |
| 248 | } |
| 249 | blocks--; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | #endif |
| 254 | |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 255 | static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 256 | struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 257 | { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 258 | int timeout; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 259 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Peng Fan | eec2d43 | 2018-01-10 13:20:40 +0800 | [diff] [blame] | 260 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \ |
| 261 | defined(CONFIG_MX8M) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 262 | dma_addr_t addr; |
| 263 | #endif |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 264 | uint wml_value; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 265 | |
| 266 | wml_value = data->blocksize/4; |
| 267 | |
| 268 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 269 | if (wml_value > WML_RD_WML_MAX) |
| 270 | wml_value = WML_RD_WML_MAX_VAL; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 271 | |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 272 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 273 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Peng Fan | eec2d43 | 2018-01-10 13:20:40 +0800 | [diff] [blame] | 274 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \ |
| 275 | defined(CONFIG_MX8M) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 276 | addr = virt_to_phys((void *)(data->dest)); |
| 277 | if (upper_32_bits(addr)) |
| 278 | printf("Error found for upper 32 bits\n"); |
| 279 | else |
| 280 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 281 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 282 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 283 | #endif |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 284 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 285 | } else { |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 286 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 287 | flush_dcache_range((ulong)data->src, |
| 288 | (ulong)data->src+data->blocks |
| 289 | *data->blocksize); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 290 | #endif |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 291 | if (wml_value > WML_WR_WML_MAX) |
| 292 | wml_value = WML_WR_WML_MAX_VAL; |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 293 | if (priv->wp_enable) { |
| 294 | if ((esdhc_read32(®s->prsstat) & |
| 295 | PRSSTAT_WPSPL) == 0) { |
| 296 | 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] | 297 | return -ETIMEDOUT; |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 298 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 299 | } |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 300 | |
| 301 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 302 | wml_value << 16); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 303 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Peng Fan | eec2d43 | 2018-01-10 13:20:40 +0800 | [diff] [blame] | 304 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \ |
| 305 | defined(CONFIG_MX8M) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 306 | addr = virt_to_phys((void *)(data->src)); |
| 307 | if (upper_32_bits(addr)) |
| 308 | printf("Error found for upper 32 bits\n"); |
| 309 | else |
| 310 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 311 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 312 | esdhc_write32(®s->dsaddr, (u32)data->src); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 313 | #endif |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 314 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 317 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 318 | |
| 319 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 320 | /* |
| 321 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 322 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 323 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 324 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 325 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 326 | * As 1) >= 2) |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 327 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 328 | * Taking log2 both the sides |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 329 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 330 | * Rounding up to next power of 2 |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 331 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 332 | * => timeout + 13 = fls(mmc->clock/4) |
Yangbo Lu | e978a31 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 333 | * |
| 334 | * However, the MMC spec "It is strongly recommended for hosts to |
| 335 | * implement more than 500ms timeout value even if the card |
| 336 | * indicates the 250ms maximum busy length." Even the previous |
| 337 | * value of 300ms is known to be insufficient for some cards. |
| 338 | * So, we use |
| 339 | * => timeout + 13 = fls(mmc->clock/2) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 340 | */ |
Yangbo Lu | e978a31 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 341 | timeout = fls(mmc->clock/2); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 342 | timeout -= 13; |
| 343 | |
| 344 | if (timeout > 14) |
| 345 | timeout = 14; |
| 346 | |
| 347 | if (timeout < 0) |
| 348 | timeout = 0; |
| 349 | |
Kumar Gala | 5103a03 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 350 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 351 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 352 | timeout++; |
| 353 | #endif |
| 354 | |
Haijun.Zhang | 1336e2d | 2014-03-18 17:04:23 +0800 | [diff] [blame] | 355 | #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE |
| 356 | timeout = 0xE; |
| 357 | #endif |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 358 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 363 | static void check_and_invalidate_dcache_range |
| 364 | (struct mmc_cmd *cmd, |
| 365 | struct mmc_data *data) { |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 366 | unsigned start = 0; |
Yangbo Lu | cc634e2 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 367 | unsigned end = 0; |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 368 | unsigned size = roundup(ARCH_DMA_MINALIGN, |
| 369 | data->blocks*data->blocksize); |
Peng Fan | eec2d43 | 2018-01-10 13:20:40 +0800 | [diff] [blame] | 370 | #if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \ |
| 371 | defined(CONFIG_MX8M) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 372 | dma_addr_t addr; |
| 373 | |
| 374 | addr = virt_to_phys((void *)(data->dest)); |
| 375 | if (upper_32_bits(addr)) |
| 376 | printf("Error found for upper 32 bits\n"); |
| 377 | else |
| 378 | start = lower_32_bits(addr); |
Yangbo Lu | cc634e2 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 379 | #else |
| 380 | start = (unsigned)data->dest; |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 381 | #endif |
Yangbo Lu | cc634e2 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 382 | end = start + size; |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 383 | invalidate_dcache_range(start, end); |
| 384 | } |
Tom Rini | 10dc777 | 2014-05-23 09:19:05 -0400 | [diff] [blame] | 385 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 386 | /* |
| 387 | * Sends a command out on the bus. Takes the mmc pointer, |
| 388 | * a command pointer, and an optional data pointer. |
| 389 | */ |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 390 | static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 391 | struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 392 | { |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 393 | int err = 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 394 | uint xfertyp; |
| 395 | uint irqstat; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 396 | u32 flags = IRQSTAT_CC | IRQSTAT_CTOE; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 397 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 398 | |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 399 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 400 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 401 | return 0; |
| 402 | #endif |
| 403 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 404 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 405 | |
| 406 | sync(); |
| 407 | |
| 408 | /* Wait for the bus to be idle */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 409 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 410 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 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 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 414 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 415 | |
| 416 | /* Wait at least 8 SD clock cycles before the next command */ |
| 417 | /* |
| 418 | * Note: This is way more than 8 cycles, but 1ms seems to |
| 419 | * resolve timing issues with some cards |
| 420 | */ |
| 421 | udelay(1000); |
| 422 | |
| 423 | /* Set up for a data transfer if we have one */ |
| 424 | if (data) { |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 425 | err = esdhc_setup_data(priv, mmc, data); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 426 | if(err) |
| 427 | return err; |
Peng Fan | 4683b22 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 428 | |
| 429 | if (data->flags & MMC_DATA_READ) |
| 430 | check_and_invalidate_dcache_range(cmd, data); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* Figure out the transfer arguments */ |
| 434 | xfertyp = esdhc_xfertyp(cmd, data); |
| 435 | |
Andrew Gabbasov | 01b7735 | 2013-06-11 10:34:22 -0500 | [diff] [blame] | 436 | /* Mask all irqs */ |
| 437 | esdhc_write32(®s->irqsigen, 0); |
| 438 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 439 | /* Send the command */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 440 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 441 | #if defined(CONFIG_FSL_USDHC) |
| 442 | esdhc_write32(®s->mixctrl, |
Volodymyr Riazantsev | 0e1bf61 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 443 | (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F) |
| 444 | | (mmc->ddr_mode ? XFERTYP_DDREN : 0)); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 445 | esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000); |
| 446 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 447 | esdhc_write32(®s->xfertyp, xfertyp); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame] | 448 | #endif |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 449 | |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 450 | if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) || |
| 451 | (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) |
| 452 | flags = IRQSTAT_BRR; |
| 453 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 454 | /* Wait for the command to complete */ |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 455 | while (!(esdhc_read32(®s->irqstat) & flags)) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 456 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 457 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 458 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 459 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 460 | if (irqstat & CMD_ERR) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 461 | err = -ECOMM; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 462 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 465 | if (irqstat & IRQSTAT_CTOE) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 466 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 467 | goto out; |
| 468 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 469 | |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 470 | /* Switch voltage to 1.8V if CMD11 succeeded */ |
| 471 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) { |
| 472 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 473 | |
| 474 | printf("Run CMD11 1.8V switch\n"); |
| 475 | /* Sleep for 5 ms - max time for card to switch to 1.8V */ |
| 476 | udelay(5000); |
| 477 | } |
| 478 | |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 479 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 480 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
Yangbo Lu | 253d5bd | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 481 | int timeout = 6000; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 482 | |
Yangbo Lu | 253d5bd | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 483 | /* Poll on DATA0 line for cmd with busy signal for 600 ms */ |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 484 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 485 | PRSSTAT_DAT0)) { |
| 486 | udelay(100); |
| 487 | timeout--; |
| 488 | } |
| 489 | |
| 490 | if (timeout <= 0) { |
| 491 | printf("Timeout waiting for DAT0 to go high!\n"); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 492 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 493 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 497 | /* Copy the response to the response buffer */ |
| 498 | if (cmd->resp_type & MMC_RSP_136) { |
| 499 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 500 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 501 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 502 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 503 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 504 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 505 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 506 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 507 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 508 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 509 | } else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 510 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 511 | |
| 512 | /* Wait until all of the blocks are transferred */ |
| 513 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 514 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 515 | esdhc_pio_read_write(priv, data); |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 516 | #else |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 517 | flags = DATA_COMPLETE; |
| 518 | if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) || |
| 519 | (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) { |
| 520 | flags = IRQSTAT_BRR; |
| 521 | } |
| 522 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 523 | do { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 524 | irqstat = esdhc_read32(®s->irqstat); |
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 | if (irqstat & IRQSTAT_DTOE) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 527 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 528 | goto out; |
| 529 | } |
Frans Meulenbroeks | 63fb5a7 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 530 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 531 | if (irqstat & DATA_ERR) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 532 | err = -ECOMM; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 533 | goto out; |
| 534 | } |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 535 | } while ((irqstat & flags) != flags); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 536 | |
Peng Fan | 4683b22 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 537 | /* |
| 538 | * Need invalidate the dcache here again to avoid any |
| 539 | * cache-fill during the DMA operations such as the |
| 540 | * speculative pre-fetching etc. |
| 541 | */ |
Eric Nelson | 54899fc | 2013-04-03 12:31:56 +0000 | [diff] [blame] | 542 | if (data->flags & MMC_DATA_READ) |
| 543 | check_and_invalidate_dcache_range(cmd, data); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 544 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 545 | } |
| 546 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 547 | out: |
| 548 | /* Reset CMD and DATA portions on error */ |
| 549 | if (err) { |
| 550 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 551 | SYSCTL_RSTC); |
| 552 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 553 | ; |
| 554 | |
| 555 | if (data) { |
| 556 | esdhc_write32(®s->sysctl, |
| 557 | esdhc_read32(®s->sysctl) | |
| 558 | SYSCTL_RSTD); |
| 559 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 560 | ; |
| 561 | } |
Otavio Salvador | f022d36 | 2015-02-17 10:42:43 -0200 | [diff] [blame] | 562 | |
| 563 | /* If this was CMD11, then notify that power cycle is needed */ |
| 564 | if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) |
| 565 | 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] | 566 | } |
| 567 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 568 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 569 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 570 | return err; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 571 | } |
| 572 | |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 573 | 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] | 574 | { |
Benoît Thébaudeau | b9b4f14 | 2018-01-16 22:44:18 +0100 | [diff] [blame] | 575 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 576 | int div = 1; |
| 577 | #ifdef ARCH_MXC |
Benoît Thébaudeau | b9b4f14 | 2018-01-16 22:44:18 +0100 | [diff] [blame] | 578 | #ifdef CONFIG_MX53 |
| 579 | /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */ |
| 580 | int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1; |
| 581 | #else |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 582 | int pre_div = 1; |
Benoît Thébaudeau | b9b4f14 | 2018-01-16 22:44:18 +0100 | [diff] [blame] | 583 | #endif |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 584 | #else |
| 585 | int pre_div = 2; |
| 586 | #endif |
| 587 | int ddr_pre_div = mmc->ddr_mode ? 2 : 1; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 588 | int sdhc_clk = priv->sdhc_clk; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 589 | uint clk; |
| 590 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 591 | if (clock < mmc->cfg->f_min) |
| 592 | clock = mmc->cfg->f_min; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 593 | |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 594 | while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256) |
| 595 | pre_div *= 2; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 596 | |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 597 | while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16) |
| 598 | div++; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 599 | |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 600 | pre_div >>= 1; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 601 | div -= 1; |
| 602 | |
| 603 | clk = (pre_div << 8) | (div << 4); |
| 604 | |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 605 | #ifdef CONFIG_FSL_USDHC |
Ye Li | 84ecdf6 | 2016-06-15 10:53:01 +0800 | [diff] [blame] | 606 | esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN); |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 607 | #else |
Kumar Gala | cc4d122 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 608 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 609 | #endif |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 610 | |
| 611 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 612 | |
| 613 | udelay(10000); |
| 614 | |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 615 | #ifdef CONFIG_FSL_USDHC |
Ye Li | 84ecdf6 | 2016-06-15 10:53:01 +0800 | [diff] [blame] | 616 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN); |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 617 | #else |
| 618 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
| 619 | #endif |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 620 | |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 621 | priv->clock = clock; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 622 | } |
| 623 | |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 624 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 625 | static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable) |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 626 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 627 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 628 | u32 value; |
| 629 | u32 time_out; |
| 630 | |
| 631 | value = esdhc_read32(®s->sysctl); |
| 632 | |
| 633 | if (enable) |
| 634 | value |= SYSCTL_CKEN; |
| 635 | else |
| 636 | value &= ~SYSCTL_CKEN; |
| 637 | |
| 638 | esdhc_write32(®s->sysctl, value); |
| 639 | |
| 640 | time_out = 20; |
| 641 | value = PRSSTAT_SDSTB; |
| 642 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 643 | if (time_out == 0) { |
| 644 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 645 | break; |
| 646 | } |
| 647 | time_out--; |
| 648 | mdelay(1); |
| 649 | } |
| 650 | } |
| 651 | #endif |
| 652 | |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 653 | #ifdef MMC_SUPPORTS_TUNING |
| 654 | static int esdhc_change_pinstate(struct udevice *dev) |
| 655 | { |
| 656 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 657 | int ret; |
| 658 | |
| 659 | switch (priv->mode) { |
| 660 | case UHS_SDR50: |
| 661 | case UHS_DDR50: |
| 662 | ret = pinctrl_select_state(dev, "state_100mhz"); |
| 663 | break; |
| 664 | case UHS_SDR104: |
| 665 | case MMC_HS_200: |
| 666 | ret = pinctrl_select_state(dev, "state_200mhz"); |
| 667 | break; |
| 668 | default: |
| 669 | ret = pinctrl_select_state(dev, "default"); |
| 670 | break; |
| 671 | } |
| 672 | |
| 673 | if (ret) |
| 674 | printf("%s %d error\n", __func__, priv->mode); |
| 675 | |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | static void esdhc_reset_tuning(struct mmc *mmc) |
| 680 | { |
| 681 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 682 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 683 | |
| 684 | if (priv->flags & ESDHC_FLAG_USDHC) { |
| 685 | if (priv->flags & ESDHC_FLAG_STD_TUNING) { |
| 686 | esdhc_clrbits32(®s->autoc12err, |
| 687 | MIX_CTRL_SMPCLK_SEL | |
| 688 | MIX_CTRL_EXE_TUNE); |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | static int esdhc_set_timing(struct mmc *mmc) |
| 694 | { |
| 695 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 696 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 697 | u32 mixctrl; |
| 698 | |
| 699 | mixctrl = readl(®s->mixctrl); |
| 700 | mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN); |
| 701 | |
| 702 | switch (mmc->selected_mode) { |
| 703 | case MMC_LEGACY: |
| 704 | case SD_LEGACY: |
| 705 | esdhc_reset_tuning(mmc); |
| 706 | break; |
| 707 | case MMC_HS: |
| 708 | case MMC_HS_52: |
| 709 | case MMC_HS_200: |
| 710 | case SD_HS: |
| 711 | case UHS_SDR12: |
| 712 | case UHS_SDR25: |
| 713 | case UHS_SDR50: |
| 714 | case UHS_SDR104: |
| 715 | writel(mixctrl, ®s->mixctrl); |
| 716 | break; |
| 717 | case UHS_DDR50: |
| 718 | case MMC_DDR_52: |
| 719 | mixctrl |= MIX_CTRL_DDREN; |
| 720 | writel(mixctrl, ®s->mixctrl); |
| 721 | break; |
| 722 | default: |
| 723 | printf("Not supported %d\n", mmc->selected_mode); |
| 724 | return -EINVAL; |
| 725 | } |
| 726 | |
| 727 | priv->mode = mmc->selected_mode; |
| 728 | |
| 729 | return esdhc_change_pinstate(mmc->dev); |
| 730 | } |
| 731 | |
| 732 | static int esdhc_set_voltage(struct mmc *mmc) |
| 733 | { |
| 734 | struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev); |
| 735 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 736 | int ret; |
| 737 | |
| 738 | priv->signal_voltage = mmc->signal_voltage; |
| 739 | switch (mmc->signal_voltage) { |
| 740 | case MMC_SIGNAL_VOLTAGE_330: |
| 741 | if (priv->vs18_enable) |
| 742 | return -EIO; |
| 743 | #ifdef CONFIG_DM_REGULATOR |
| 744 | if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) { |
| 745 | ret = regulator_set_value(priv->vqmmc_dev, 3300000); |
| 746 | if (ret) { |
| 747 | printf("Setting to 3.3V error"); |
| 748 | return -EIO; |
| 749 | } |
| 750 | /* Wait for 5ms */ |
| 751 | mdelay(5); |
| 752 | } |
| 753 | #endif |
| 754 | |
| 755 | esdhc_clrbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 756 | if (!(esdhc_read32(®s->vendorspec) & |
| 757 | ESDHC_VENDORSPEC_VSELECT)) |
| 758 | return 0; |
| 759 | |
| 760 | return -EAGAIN; |
| 761 | case MMC_SIGNAL_VOLTAGE_180: |
| 762 | #ifdef CONFIG_DM_REGULATOR |
| 763 | if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) { |
| 764 | ret = regulator_set_value(priv->vqmmc_dev, 1800000); |
| 765 | if (ret) { |
| 766 | printf("Setting to 1.8V error"); |
| 767 | return -EIO; |
| 768 | } |
| 769 | } |
| 770 | #endif |
| 771 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 772 | if (esdhc_read32(®s->vendorspec) & ESDHC_VENDORSPEC_VSELECT) |
| 773 | return 0; |
| 774 | |
| 775 | return -EAGAIN; |
| 776 | case MMC_SIGNAL_VOLTAGE_120: |
| 777 | return -ENOTSUPP; |
| 778 | default: |
| 779 | return 0; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | static void esdhc_stop_tuning(struct mmc *mmc) |
| 784 | { |
| 785 | struct mmc_cmd cmd; |
| 786 | |
| 787 | cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
| 788 | cmd.cmdarg = 0; |
| 789 | cmd.resp_type = MMC_RSP_R1b; |
| 790 | |
| 791 | dm_mmc_send_cmd(mmc->dev, &cmd, NULL); |
| 792 | } |
| 793 | |
| 794 | static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode) |
| 795 | { |
| 796 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 797 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 798 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 799 | struct mmc *mmc = &plat->mmc; |
| 800 | u32 irqstaten = readl(®s->irqstaten); |
| 801 | u32 irqsigen = readl(®s->irqsigen); |
| 802 | int i, ret = -ETIMEDOUT; |
| 803 | u32 val, mixctrl; |
| 804 | |
| 805 | /* clock tuning is not needed for upto 52MHz */ |
| 806 | if (mmc->clock <= 52000000) |
| 807 | return 0; |
| 808 | |
| 809 | /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */ |
| 810 | if (priv->flags & ESDHC_FLAG_STD_TUNING) { |
| 811 | val = readl(®s->autoc12err); |
| 812 | mixctrl = readl(®s->mixctrl); |
| 813 | val &= ~MIX_CTRL_SMPCLK_SEL; |
| 814 | mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN); |
| 815 | |
| 816 | val |= MIX_CTRL_EXE_TUNE; |
| 817 | mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN; |
| 818 | |
| 819 | writel(val, ®s->autoc12err); |
| 820 | writel(mixctrl, ®s->mixctrl); |
| 821 | } |
| 822 | |
| 823 | /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */ |
| 824 | mixctrl = readl(®s->mixctrl); |
| 825 | mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK); |
| 826 | writel(mixctrl, ®s->mixctrl); |
| 827 | |
| 828 | writel(IRQSTATEN_BRR, ®s->irqstaten); |
| 829 | writel(IRQSTATEN_BRR, ®s->irqsigen); |
| 830 | |
| 831 | /* |
| 832 | * Issue opcode repeatedly till Execute Tuning is set to 0 or the number |
| 833 | * of loops reaches 40 times. |
| 834 | */ |
| 835 | for (i = 0; i < MAX_TUNING_LOOP; i++) { |
| 836 | u32 ctrl; |
| 837 | |
| 838 | if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) { |
| 839 | if (mmc->bus_width == 8) |
| 840 | writel(0x7080, ®s->blkattr); |
| 841 | else if (mmc->bus_width == 4) |
| 842 | writel(0x7040, ®s->blkattr); |
| 843 | } else { |
| 844 | writel(0x7040, ®s->blkattr); |
| 845 | } |
| 846 | |
| 847 | /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */ |
| 848 | val = readl(®s->mixctrl); |
| 849 | val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK); |
| 850 | writel(val, ®s->mixctrl); |
| 851 | |
| 852 | /* We are using STD tuning, no need to check return value */ |
| 853 | mmc_send_tuning(mmc, opcode, NULL); |
| 854 | |
| 855 | ctrl = readl(®s->autoc12err); |
| 856 | if ((!(ctrl & MIX_CTRL_EXE_TUNE)) && |
| 857 | (ctrl & MIX_CTRL_SMPCLK_SEL)) { |
| 858 | /* |
| 859 | * need to wait some time, make sure sd/mmc fininsh |
| 860 | * send out tuning data, otherwise, the sd/mmc can't |
| 861 | * response to any command when the card still out |
| 862 | * put the tuning data. |
| 863 | */ |
| 864 | mdelay(1); |
| 865 | ret = 0; |
| 866 | break; |
| 867 | } |
| 868 | |
| 869 | /* Add 1ms delay for SD and eMMC */ |
| 870 | mdelay(1); |
| 871 | } |
| 872 | |
| 873 | writel(irqstaten, ®s->irqstaten); |
| 874 | writel(irqsigen, ®s->irqsigen); |
| 875 | |
| 876 | esdhc_stop_tuning(mmc); |
| 877 | |
| 878 | return ret; |
| 879 | } |
| 880 | #endif |
| 881 | |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 882 | 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] | 883 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 884 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 885 | int ret __maybe_unused; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 886 | |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 887 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 888 | /* Select to use peripheral clock */ |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 889 | esdhc_clock_control(priv, false); |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 890 | esdhc_setbits32(®s->scr, ESDHCCTL_PCS); |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 891 | esdhc_clock_control(priv, true); |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 892 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 893 | /* Set the clock speed */ |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 894 | if (priv->clock != mmc->clock) |
| 895 | set_sysctl(priv, mmc, mmc->clock); |
| 896 | |
| 897 | #ifdef MMC_SUPPORTS_TUNING |
| 898 | if (mmc->clk_disable) { |
| 899 | #ifdef CONFIG_FSL_USDHC |
| 900 | esdhc_clrbits32(®s->vendorspec, VENDORSPEC_CKEN); |
| 901 | #else |
| 902 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
| 903 | #endif |
| 904 | } else { |
| 905 | #ifdef CONFIG_FSL_USDHC |
| 906 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | |
| 907 | VENDORSPEC_CKEN); |
| 908 | #else |
| 909 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
| 910 | #endif |
| 911 | } |
| 912 | |
| 913 | if (priv->mode != mmc->selected_mode) { |
| 914 | ret = esdhc_set_timing(mmc); |
| 915 | if (ret) { |
| 916 | printf("esdhc_set_timing error %d\n", ret); |
| 917 | return ret; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | if (priv->signal_voltage != mmc->signal_voltage) { |
| 922 | ret = esdhc_set_voltage(mmc); |
| 923 | if (ret) { |
| 924 | printf("esdhc_set_voltage error %d\n", ret); |
| 925 | return ret; |
| 926 | } |
| 927 | } |
| 928 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 929 | |
| 930 | /* Set the bus width */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 931 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 932 | |
| 933 | if (mmc->bus_width == 4) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 934 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 935 | else if (mmc->bus_width == 8) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 936 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 937 | |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 938 | return 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 939 | } |
| 940 | |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 941 | 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] | 942 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 943 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Simon Glass | 201e828 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 944 | ulong start; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 945 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 946 | /* Reset the entire host controller */ |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 947 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 948 | |
| 949 | /* Wait until the controller is available */ |
Simon Glass | 201e828 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 950 | start = get_timer(0); |
| 951 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 952 | if (get_timer(start) > 1000) |
| 953 | return -ETIMEDOUT; |
| 954 | } |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 955 | |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 956 | #if defined(CONFIG_FSL_USDHC) |
| 957 | /* RSTA doesn't reset MMC_BOOT register, so manually reset it */ |
| 958 | esdhc_write32(®s->mmcboot, 0x0); |
| 959 | /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */ |
| 960 | esdhc_write32(®s->mixctrl, 0x0); |
| 961 | esdhc_write32(®s->clktunectrlstatus, 0x0); |
| 962 | |
| 963 | /* Put VEND_SPEC to default value */ |
Peng Fan | db359ef | 2018-01-02 16:51:22 +0800 | [diff] [blame] | 964 | if (priv->vs18_enable) |
| 965 | esdhc_write32(®s->vendorspec, (VENDORSPEC_INIT | |
| 966 | ESDHC_VENDORSPEC_VSELECT)); |
| 967 | else |
| 968 | esdhc_write32(®s->vendorspec, VENDORSPEC_INIT); |
Peng Fan | f53225c | 2016-06-15 10:53:00 +0800 | [diff] [blame] | 969 | |
| 970 | /* Disable DLL_CTRL delay line */ |
| 971 | esdhc_write32(®s->dllctrl, 0x0); |
| 972 | #endif |
| 973 | |
Benoît Thébaudeau | 16e43f3 | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 974 | #ifndef ARCH_MXC |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 975 | /* Enable cache snooping */ |
Benoît Thébaudeau | 16e43f3 | 2012-08-13 07:28:16 +0000 | [diff] [blame] | 976 | esdhc_write32(®s->scr, 0x00000040); |
| 977 | #endif |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 978 | |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 979 | #ifndef CONFIG_FSL_USDHC |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 980 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Ye Li | 84ecdf6 | 2016-06-15 10:53:01 +0800 | [diff] [blame] | 981 | #else |
| 982 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN); |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 983 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 984 | |
| 985 | /* Set the initial clock speed */ |
Kishon Vijay Abraham I | 35f6782 | 2017-09-21 16:30:03 +0200 | [diff] [blame] | 986 | mmc_set_clock(mmc, 400000, false); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 987 | |
| 988 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 989 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 990 | |
| 991 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 992 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 993 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 994 | /* Set timout to the maximum value */ |
| 995 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 996 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 997 | return 0; |
| 998 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 999 | |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 1000 | static int esdhc_getcd_common(struct fsl_esdhc_priv *priv) |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 1001 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1002 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 1003 | int timeout = 1000; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 1004 | |
Haijun.Zhang | f7e27cc | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 1005 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 1006 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 1007 | return 1; |
| 1008 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1009 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1010 | #if CONFIG_IS_ENABLED(DM_MMC) |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1011 | if (priv->non_removable) |
| 1012 | return 1; |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 1013 | #ifdef CONFIG_DM_GPIO |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1014 | if (dm_gpio_is_valid(&priv->cd_gpio)) |
| 1015 | return dm_gpio_get_value(&priv->cd_gpio); |
| 1016 | #endif |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 1017 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1018 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 1019 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 1020 | udelay(1000); |
| 1021 | |
| 1022 | return timeout > 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1023 | } |
| 1024 | |
Simon Glass | 446e077 | 2017-07-29 11:35:19 -0600 | [diff] [blame] | 1025 | static int esdhc_reset(struct fsl_esdhc *regs) |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 1026 | { |
Simon Glass | 446e077 | 2017-07-29 11:35:19 -0600 | [diff] [blame] | 1027 | ulong start; |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 1028 | |
| 1029 | /* reset the controller */ |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 1030 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 1031 | |
| 1032 | /* hardware clears the bit when it is done */ |
Simon Glass | 446e077 | 2017-07-29 11:35:19 -0600 | [diff] [blame] | 1033 | start = get_timer(0); |
| 1034 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 1035 | if (get_timer(start) > 100) { |
| 1036 | printf("MMC/SD: Reset never completed.\n"); |
| 1037 | return -ETIMEDOUT; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | return 0; |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 1042 | } |
| 1043 | |
Simon Glass | e7881d8 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 1044 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 1045 | static int esdhc_getcd(struct mmc *mmc) |
| 1046 | { |
| 1047 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1048 | |
| 1049 | return esdhc_getcd_common(priv); |
| 1050 | } |
| 1051 | |
| 1052 | static int esdhc_init(struct mmc *mmc) |
| 1053 | { |
| 1054 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1055 | |
| 1056 | return esdhc_init_common(priv, mmc); |
| 1057 | } |
| 1058 | |
| 1059 | static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 1060 | struct mmc_data *data) |
| 1061 | { |
| 1062 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1063 | |
| 1064 | return esdhc_send_cmd_common(priv, mmc, cmd, data); |
| 1065 | } |
| 1066 | |
| 1067 | static int esdhc_set_ios(struct mmc *mmc) |
| 1068 | { |
| 1069 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 1070 | |
| 1071 | return esdhc_set_ios_common(priv, mmc); |
| 1072 | } |
| 1073 | |
Pantelis Antoniou | ab769f2 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 1074 | static const struct mmc_ops esdhc_ops = { |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 1075 | .getcd = esdhc_getcd, |
| 1076 | .init = esdhc_init, |
Pantelis Antoniou | ab769f2 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 1077 | .send_cmd = esdhc_send_cmd, |
| 1078 | .set_ios = esdhc_set_ios, |
Pantelis Antoniou | ab769f2 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 1079 | }; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1080 | #endif |
Pantelis Antoniou | ab769f2 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 1081 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1082 | static int fsl_esdhc_init(struct fsl_esdhc_priv *priv, |
| 1083 | struct fsl_esdhc_plat *plat) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1084 | { |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1085 | struct mmc_config *cfg; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 1086 | struct fsl_esdhc *regs; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 1087 | u32 caps, voltage_caps; |
Simon Glass | 446e077 | 2017-07-29 11:35:19 -0600 | [diff] [blame] | 1088 | int ret; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1089 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1090 | if (!priv) |
| 1091 | return -EINVAL; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 1092 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1093 | regs = priv->esdhc_regs; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 1094 | |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 1095 | /* First reset the eSDHC controller */ |
Simon Glass | 446e077 | 2017-07-29 11:35:19 -0600 | [diff] [blame] | 1096 | ret = esdhc_reset(regs); |
| 1097 | if (ret) |
| 1098 | return ret; |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 1099 | |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 1100 | #ifndef CONFIG_FSL_USDHC |
Jerry Huang | 975324a | 2012-05-17 23:57:02 +0000 | [diff] [blame] | 1101 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
| 1102 | | SYSCTL_IPGEN | SYSCTL_CKEN); |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1103 | /* Clearing tuning bits in case ROM has set it already */ |
| 1104 | esdhc_write32(®s->mixctrl, 0); |
| 1105 | esdhc_write32(®s->autoc12err, 0); |
| 1106 | esdhc_write32(®s->clktunectrlstatus, 0); |
Ye Li | 84ecdf6 | 2016-06-15 10:53:01 +0800 | [diff] [blame] | 1107 | #else |
| 1108 | esdhc_setbits32(®s->vendorspec, VENDORSPEC_PEREN | |
| 1109 | VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN); |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 1110 | #endif |
Jerry Huang | 975324a | 2012-05-17 23:57:02 +0000 | [diff] [blame] | 1111 | |
Peng Fan | 32a9179 | 2017-06-12 17:50:53 +0800 | [diff] [blame] | 1112 | if (priv->vs18_enable) |
| 1113 | esdhc_setbits32(®s->vendorspec, ESDHC_VENDORSPEC_VSELECT); |
| 1114 | |
Ye.Li | a3d6e38 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 1115 | writel(SDHCI_IRQ_EN_BITS, ®s->irqstaten); |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1116 | cfg = &plat->cfg; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1117 | #ifndef CONFIG_DM_MMC |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1118 | memset(cfg, '\0', sizeof(*cfg)); |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1119 | #endif |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1120 | |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 1121 | voltage_caps = 0; |
Wang Huan | 19060bd | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 1122 | caps = esdhc_read32(®s->hostcapblt); |
Roy Zang | 3b4456e | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 1123 | |
| 1124 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
| 1125 | caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | |
| 1126 | ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); |
| 1127 | #endif |
Haijun.Zhang | ef38f3f | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 1128 | |
| 1129 | /* T4240 host controller capabilities register should have VS33 bit */ |
| 1130 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
| 1131 | caps = caps | ESDHC_HOSTCAPBLT_VS33; |
| 1132 | #endif |
| 1133 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1134 | if (caps & ESDHC_HOSTCAPBLT_VS18) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 1135 | voltage_caps |= MMC_VDD_165_195; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1136 | if (caps & ESDHC_HOSTCAPBLT_VS30) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 1137 | voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1138 | if (caps & ESDHC_HOSTCAPBLT_VS33) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 1139 | voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; |
| 1140 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1141 | cfg->name = "FSL_SDHC"; |
Simon Glass | e7881d8 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 1142 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1143 | cfg->ops = &esdhc_ops; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1144 | #endif |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 1145 | #ifdef CONFIG_SYS_SD_VOLTAGE |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1146 | cfg->voltages = CONFIG_SYS_SD_VOLTAGE; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 1147 | #else |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1148 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 1149 | #endif |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1150 | if ((cfg->voltages & voltage_caps) == 0) { |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 1151 | printf("voltage not supported by controller\n"); |
| 1152 | return -1; |
| 1153 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1154 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1155 | if (priv->bus_width == 8) |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1156 | cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1157 | else if (priv->bus_width == 4) |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1158 | cfg->host_caps = MMC_MODE_4BIT; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1159 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1160 | cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
Volodymyr Riazantsev | 0e1bf61 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 1161 | #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1162 | cfg->host_caps |= MMC_MODE_DDR_52MHz; |
Volodymyr Riazantsev | 0e1bf61 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 1163 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1164 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1165 | if (priv->bus_width > 0) { |
| 1166 | if (priv->bus_width < 8) |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1167 | cfg->host_caps &= ~MMC_MODE_8BIT; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1168 | if (priv->bus_width < 4) |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1169 | cfg->host_caps &= ~MMC_MODE_4BIT; |
Abbas Raza | aad4659 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1172 | if (caps & ESDHC_HOSTCAPBLT_HSS) |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1173 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1174 | |
Haijun.Zhang | d47e3d2 | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 1175 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 1176 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1177 | cfg->host_caps &= ~MMC_MODE_8BIT; |
Haijun.Zhang | d47e3d2 | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 1178 | #endif |
| 1179 | |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1180 | cfg->host_caps |= priv->caps; |
| 1181 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1182 | cfg->f_min = 400000; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1183 | cfg->f_max = min(priv->sdhc_clk, (u32)200000000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1184 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1185 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 1186 | |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1187 | writel(0, ®s->dllctrl); |
| 1188 | if (priv->flags & ESDHC_FLAG_USDHC) { |
| 1189 | if (priv->flags & ESDHC_FLAG_STD_TUNING) { |
| 1190 | u32 val = readl(®s->tuning_ctrl); |
| 1191 | |
| 1192 | val |= ESDHC_STD_TUNING_EN; |
| 1193 | val &= ~ESDHC_TUNING_START_TAP_MASK; |
| 1194 | val |= priv->tuning_start_tap; |
| 1195 | val &= ~ESDHC_TUNING_STEP_MASK; |
| 1196 | val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT; |
| 1197 | writel(val, ®s->tuning_ctrl); |
| 1198 | } |
| 1199 | } |
| 1200 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1201 | return 0; |
| 1202 | } |
| 1203 | |
Simon Glass | 5248930 | 2017-07-29 11:35:28 -0600 | [diff] [blame] | 1204 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Jagan Teki | 2e87c44 | 2017-05-12 17:18:20 +0530 | [diff] [blame] | 1205 | static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg, |
| 1206 | struct fsl_esdhc_priv *priv) |
| 1207 | { |
| 1208 | if (!cfg || !priv) |
| 1209 | return -EINVAL; |
| 1210 | |
| 1211 | priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); |
| 1212 | priv->bus_width = cfg->max_bus_width; |
| 1213 | priv->sdhc_clk = cfg->sdhc_clk; |
| 1214 | priv->wp_enable = cfg->wp_enable; |
Peng Fan | 32a9179 | 2017-06-12 17:50:53 +0800 | [diff] [blame] | 1215 | priv->vs18_enable = cfg->vs18_enable; |
Jagan Teki | 2e87c44 | 2017-05-12 17:18:20 +0530 | [diff] [blame] | 1216 | |
| 1217 | return 0; |
| 1218 | }; |
| 1219 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1220 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
| 1221 | { |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1222 | struct fsl_esdhc_plat *plat; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1223 | struct fsl_esdhc_priv *priv; |
Simon Glass | d6eb25e | 2017-07-29 11:35:22 -0600 | [diff] [blame] | 1224 | struct mmc *mmc; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1225 | int ret; |
| 1226 | |
| 1227 | if (!cfg) |
| 1228 | return -EINVAL; |
| 1229 | |
| 1230 | priv = calloc(sizeof(struct fsl_esdhc_priv), 1); |
| 1231 | if (!priv) |
| 1232 | return -ENOMEM; |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1233 | plat = calloc(sizeof(struct fsl_esdhc_plat), 1); |
| 1234 | if (!plat) { |
| 1235 | free(priv); |
| 1236 | return -ENOMEM; |
| 1237 | } |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1238 | |
| 1239 | ret = fsl_esdhc_cfg_to_priv(cfg, priv); |
| 1240 | if (ret) { |
| 1241 | debug("%s xlate failure\n", __func__); |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1242 | free(plat); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1243 | free(priv); |
| 1244 | return ret; |
| 1245 | } |
| 1246 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1247 | ret = fsl_esdhc_init(priv, plat); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1248 | if (ret) { |
| 1249 | debug("%s init failure\n", __func__); |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1250 | free(plat); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1251 | free(priv); |
| 1252 | return ret; |
| 1253 | } |
| 1254 | |
Simon Glass | d6eb25e | 2017-07-29 11:35:22 -0600 | [diff] [blame] | 1255 | mmc = mmc_create(&plat->cfg, priv); |
| 1256 | if (!mmc) |
| 1257 | return -EIO; |
| 1258 | |
| 1259 | priv->mmc = mmc; |
| 1260 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1261 | return 0; |
| 1262 | } |
| 1263 | |
| 1264 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 1265 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 1266 | struct fsl_esdhc_cfg *cfg; |
| 1267 | |
Fabio Estevam | 88227a1 | 2012-12-27 08:51:08 +0000 | [diff] [blame] | 1268 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 1269 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
Simon Glass | e9adeca | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 1270 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 1271 | return fsl_esdhc_initialize(bis, cfg); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1272 | } |
Jagan Teki | 2e87c44 | 2017-05-12 17:18:20 +0530 | [diff] [blame] | 1273 | #endif |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 1274 | |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 1275 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 1276 | void mmc_adapter_card_type_ident(void) |
| 1277 | { |
| 1278 | u8 card_id; |
| 1279 | u8 value; |
| 1280 | |
| 1281 | card_id = QIXIS_READ(present) & QIXIS_SDID_MASK; |
| 1282 | gd->arch.sdhc_adapter = card_id; |
| 1283 | |
| 1284 | switch (card_id) { |
| 1285 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45: |
Yangbo Lu | cdc6955 | 2015-09-17 10:27:12 +0800 | [diff] [blame] | 1286 | value = QIXIS_READ(brdcfg[5]); |
| 1287 | value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7); |
| 1288 | QIXIS_WRITE(brdcfg[5], value); |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 1289 | break; |
| 1290 | case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY: |
Yangbo Lu | bf50be8 | 2015-09-17 10:27:48 +0800 | [diff] [blame] | 1291 | value = QIXIS_READ(pwr_ctl[1]); |
| 1292 | value |= QIXIS_EVDD_BY_SDHC_VS; |
| 1293 | QIXIS_WRITE(pwr_ctl[1], value); |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 1294 | break; |
| 1295 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44: |
| 1296 | value = QIXIS_READ(brdcfg[5]); |
| 1297 | value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT); |
| 1298 | QIXIS_WRITE(brdcfg[5], value); |
| 1299 | break; |
| 1300 | case QIXIS_ESDHC_ADAPTER_TYPE_RSV: |
| 1301 | break; |
| 1302 | case QIXIS_ESDHC_ADAPTER_TYPE_MMC: |
| 1303 | break; |
| 1304 | case QIXIS_ESDHC_ADAPTER_TYPE_SD: |
| 1305 | break; |
| 1306 | case QIXIS_ESDHC_NO_ADAPTER: |
| 1307 | break; |
| 1308 | default: |
| 1309 | break; |
| 1310 | } |
| 1311 | } |
| 1312 | #endif |
| 1313 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 1314 | #ifdef CONFIG_OF_LIBFDT |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 1315 | __weak int esdhc_status_fixup(void *blob, const char *compat) |
| 1316 | { |
| 1317 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
| 1318 | if (!hwconfig("esdhc")) { |
| 1319 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 1320 | sizeof("disabled"), 1); |
| 1321 | return 1; |
| 1322 | } |
| 1323 | #endif |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 1324 | return 0; |
| 1325 | } |
| 1326 | |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 1327 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 1328 | { |
| 1329 | const char *compat = "fsl,esdhc"; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 1330 | |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 1331 | if (esdhc_status_fixup(blob, compat)) |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 1332 | return; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 1333 | |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 1334 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 1335 | do_fixup_by_compat_u32(blob, compat, "peripheral-frequency", |
| 1336 | gd->arch.sdhc_clk, 1); |
| 1337 | #else |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 1338 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | e9adeca | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 1339 | gd->arch.sdhc_clk, 1); |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 1340 | #endif |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 1341 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 1342 | do_fixup_by_compat_u32(blob, compat, "adapter-type", |
| 1343 | (u32)(gd->arch.sdhc_adapter), 1); |
| 1344 | #endif |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 1345 | } |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 1346 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1347 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1348 | #if CONFIG_IS_ENABLED(DM_MMC) |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1349 | #include <asm/arch/clock.h> |
Peng Fan | b60f145 | 2017-02-22 16:21:55 +0800 | [diff] [blame] | 1350 | __weak void init_clk_usdhc(u32 index) |
| 1351 | { |
| 1352 | } |
| 1353 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1354 | static int fsl_esdhc_probe(struct udevice *dev) |
| 1355 | { |
| 1356 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1357 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1358 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1359 | const void *fdt = gd->fdt_blob; |
| 1360 | int node = dev_of_offset(dev); |
| 1361 | struct esdhc_soc_data *data = |
| 1362 | (struct esdhc_soc_data *)dev_get_driver_data(dev); |
York Sun | 9bb272e | 2017-08-08 15:45:13 -0700 | [diff] [blame] | 1363 | #ifdef CONFIG_DM_REGULATOR |
Peng Fan | 4483b7e | 2017-06-12 17:50:54 +0800 | [diff] [blame] | 1364 | struct udevice *vqmmc_dev; |
York Sun | 9bb272e | 2017-08-08 15:45:13 -0700 | [diff] [blame] | 1365 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1366 | fdt_addr_t addr; |
| 1367 | unsigned int val; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1368 | struct mmc *mmc; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1369 | int ret; |
| 1370 | |
Simon Glass | 4aac33f | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 1371 | addr = dev_read_addr(dev); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1372 | if (addr == FDT_ADDR_T_NONE) |
| 1373 | return -EINVAL; |
| 1374 | |
| 1375 | priv->esdhc_regs = (struct fsl_esdhc *)addr; |
| 1376 | priv->dev = dev; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1377 | priv->mode = -1; |
| 1378 | if (data) { |
| 1379 | priv->flags = data->flags; |
| 1380 | priv->caps = data->caps; |
| 1381 | } |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1382 | |
Simon Glass | 4aac33f | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 1383 | val = dev_read_u32_default(dev, "bus-width", -1); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1384 | if (val == 8) |
| 1385 | priv->bus_width = 8; |
| 1386 | else if (val == 4) |
| 1387 | priv->bus_width = 4; |
| 1388 | else |
| 1389 | priv->bus_width = 1; |
| 1390 | |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1391 | val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1); |
| 1392 | priv->tuning_step = val; |
| 1393 | val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap", |
| 1394 | ESDHC_TUNING_START_TAP_DEFAULT); |
| 1395 | priv->tuning_start_tap = val; |
| 1396 | val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target", |
| 1397 | ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT); |
| 1398 | priv->strobe_dll_delay_target = val; |
| 1399 | |
Simon Glass | 4aac33f | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 1400 | if (dev_read_bool(dev, "non-removable")) { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1401 | priv->non_removable = 1; |
| 1402 | } else { |
| 1403 | priv->non_removable = 0; |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 1404 | #ifdef CONFIG_DM_GPIO |
Simon Glass | 4aac33f | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 1405 | gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, |
| 1406 | GPIOD_IS_IN); |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 1407 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1408 | } |
| 1409 | |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 1410 | priv->wp_enable = 1; |
| 1411 | |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 1412 | #ifdef CONFIG_DM_GPIO |
Simon Glass | 4aac33f | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 1413 | ret = gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, |
| 1414 | GPIOD_IS_IN); |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 1415 | if (ret) |
| 1416 | priv->wp_enable = 0; |
Yangbo Lu | fc8048a | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 1417 | #endif |
Peng Fan | 4483b7e | 2017-06-12 17:50:54 +0800 | [diff] [blame] | 1418 | |
| 1419 | priv->vs18_enable = 0; |
| 1420 | |
| 1421 | #ifdef CONFIG_DM_REGULATOR |
| 1422 | /* |
| 1423 | * If emmc I/O has a fixed voltage at 1.8V, this must be provided, |
| 1424 | * otherwise, emmc will work abnormally. |
| 1425 | */ |
| 1426 | ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev); |
| 1427 | if (ret) { |
| 1428 | dev_dbg(dev, "no vqmmc-supply\n"); |
| 1429 | } else { |
| 1430 | ret = regulator_set_enable(vqmmc_dev, true); |
| 1431 | if (ret) { |
| 1432 | dev_err(dev, "fail to enable vqmmc-supply\n"); |
| 1433 | return ret; |
| 1434 | } |
| 1435 | |
| 1436 | if (regulator_get_value(vqmmc_dev) == 1800000) |
| 1437 | priv->vs18_enable = 1; |
| 1438 | } |
| 1439 | #endif |
| 1440 | |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1441 | if (fdt_get_property(fdt, node, "no-1-8-v", NULL)) |
| 1442 | priv->caps &= ~(UHS_CAPS | MMC_MODE_HS200); |
| 1443 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1444 | /* |
| 1445 | * TODO: |
| 1446 | * Because lack of clk driver, if SDHC clk is not enabled, |
| 1447 | * need to enable it first before this driver is invoked. |
| 1448 | * |
| 1449 | * we use MXC_ESDHC_CLK to get clk freq. |
| 1450 | * If one would like to make this function work, |
| 1451 | * the aliases should be provided in dts as this: |
| 1452 | * |
| 1453 | * aliases { |
| 1454 | * mmc0 = &usdhc1; |
| 1455 | * mmc1 = &usdhc2; |
| 1456 | * mmc2 = &usdhc3; |
| 1457 | * mmc3 = &usdhc4; |
| 1458 | * }; |
| 1459 | * Then if your board only supports mmc2 and mmc3, but we can |
| 1460 | * correctly get the seq as 2 and 3, then let mxc_get_clock |
| 1461 | * work as expected. |
| 1462 | */ |
Peng Fan | b60f145 | 2017-02-22 16:21:55 +0800 | [diff] [blame] | 1463 | |
| 1464 | init_clk_usdhc(dev->seq); |
| 1465 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1466 | priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq); |
| 1467 | if (priv->sdhc_clk <= 0) { |
| 1468 | dev_err(dev, "Unable to get clk for %s\n", dev->name); |
| 1469 | return -EINVAL; |
| 1470 | } |
| 1471 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1472 | ret = fsl_esdhc_init(priv, plat); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1473 | if (ret) { |
| 1474 | dev_err(dev, "fsl_esdhc_init failure\n"); |
| 1475 | return ret; |
| 1476 | } |
| 1477 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1478 | mmc = &plat->mmc; |
| 1479 | mmc->cfg = &plat->cfg; |
| 1480 | mmc->dev = dev; |
| 1481 | upriv->mmc = mmc; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1482 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1483 | return esdhc_init_common(priv, mmc); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1484 | } |
| 1485 | |
Simon Glass | e7881d8 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 1486 | #if CONFIG_IS_ENABLED(DM_MMC) |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1487 | static int fsl_esdhc_get_cd(struct udevice *dev) |
| 1488 | { |
| 1489 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1490 | |
| 1491 | return true; |
| 1492 | return esdhc_getcd_common(priv); |
| 1493 | } |
| 1494 | |
| 1495 | static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 1496 | struct mmc_data *data) |
| 1497 | { |
| 1498 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1499 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1500 | |
| 1501 | return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data); |
| 1502 | } |
| 1503 | |
| 1504 | static int fsl_esdhc_set_ios(struct udevice *dev) |
| 1505 | { |
| 1506 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1507 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1508 | |
| 1509 | return esdhc_set_ios_common(priv, &plat->mmc); |
| 1510 | } |
| 1511 | |
| 1512 | static const struct dm_mmc_ops fsl_esdhc_ops = { |
| 1513 | .get_cd = fsl_esdhc_get_cd, |
| 1514 | .send_cmd = fsl_esdhc_send_cmd, |
| 1515 | .set_ios = fsl_esdhc_set_ios, |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1516 | #ifdef MMC_SUPPORTS_TUNING |
| 1517 | .execute_tuning = fsl_esdhc_execute_tuning, |
| 1518 | #endif |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1519 | }; |
| 1520 | #endif |
| 1521 | |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1522 | static struct esdhc_soc_data usdhc_imx7d_data = { |
| 1523 | .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING |
| 1524 | | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200 |
| 1525 | | ESDHC_FLAG_HS400, |
| 1526 | .caps = UHS_CAPS | MMC_MODE_HS200 | MMC_MODE_DDR_52MHz | |
| 1527 | MMC_MODE_HS_52MHz | MMC_MODE_HS, |
| 1528 | }; |
| 1529 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1530 | static const struct udevice_id fsl_esdhc_ids[] = { |
| 1531 | { .compatible = "fsl,imx6ul-usdhc", }, |
| 1532 | { .compatible = "fsl,imx6sx-usdhc", }, |
| 1533 | { .compatible = "fsl,imx6sl-usdhc", }, |
| 1534 | { .compatible = "fsl,imx6q-usdhc", }, |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 1535 | { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,}, |
Peng Fan | b60f145 | 2017-02-22 16:21:55 +0800 | [diff] [blame] | 1536 | { .compatible = "fsl,imx7ulp-usdhc", }, |
Yangbo Lu | a6473f8 | 2016-12-07 11:54:31 +0800 | [diff] [blame] | 1537 | { .compatible = "fsl,esdhc", }, |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1538 | { /* sentinel */ } |
| 1539 | }; |
| 1540 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1541 | #if CONFIG_IS_ENABLED(BLK) |
| 1542 | static int fsl_esdhc_bind(struct udevice *dev) |
| 1543 | { |
| 1544 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1545 | |
| 1546 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 1547 | } |
| 1548 | #endif |
| 1549 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1550 | U_BOOT_DRIVER(fsl_esdhc) = { |
| 1551 | .name = "fsl-esdhc-mmc", |
| 1552 | .id = UCLASS_MMC, |
| 1553 | .of_match = fsl_esdhc_ids, |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1554 | .ops = &fsl_esdhc_ops, |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1555 | #if CONFIG_IS_ENABLED(BLK) |
| 1556 | .bind = fsl_esdhc_bind, |
| 1557 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1558 | .probe = fsl_esdhc_probe, |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1559 | .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat), |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1560 | .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv), |
| 1561 | }; |
| 1562 | #endif |