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