blob: 4528345c676202898a5dc62b773bf9b7aa1f5fac [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleming50586ef2008-10-30 16:47:16 -05002/*
Jerry Huangd621da02011-01-06 23:42:19 -06003 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Andy Fleming50586ef2008-10-30 16:47:16 -05004 * 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 Fleming50586ef2008-10-30 16:47:16 -05009 */
10
11#include <config.h>
12#include <common.h>
13#include <command.h>
Jaehoon Chung915ffa52016-07-19 16:33:36 +090014#include <errno.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040015#include <hwconfig.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050016#include <mmc.h>
17#include <part.h>
Peng Fan4483b7e2017-06-12 17:50:54 +080018#include <power/regulator.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050019#include <malloc.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050020#include <fsl_esdhc.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040021#include <fdt_support.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050022#include <asm/io.h>
Peng Fan96f04072016-03-25 14:16:56 +080023#include <dm.h>
24#include <asm-generic/gpio.h>
Peng Fan51313b42018-01-21 19:00:24 +080025#include <dm/pinctrl.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050026
Andy Fleming50586ef2008-10-30 16:47:16 -050027DECLARE_GLOBAL_DATA_PTR;
28
Ye.Lia3d6e382014-11-04 15:35:49 +080029#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
30 IRQSTATEN_CINT | \
31 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
32 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
33 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
34 IRQSTATEN_DINT)
Peng Fan51313b42018-01-21 19:00:24 +080035#define MAX_TUNING_LOOP 40
Ye.Lia3d6e382014-11-04 15:35:49 +080036
Andy Fleming50586ef2008-10-30 16:47:16 -050037struct fsl_esdhc {
Haijun.Zhang511948b2013-10-30 11:37:55 +080038 uint dsaddr; /* SDMA system address register */
39 uint blkattr; /* Block attributes register */
40 uint cmdarg; /* Command argument register */
41 uint xfertyp; /* Transfer type register */
42 uint cmdrsp0; /* Command response 0 register */
43 uint cmdrsp1; /* Command response 1 register */
44 uint cmdrsp2; /* Command response 2 register */
45 uint cmdrsp3; /* Command response 3 register */
46 uint datport; /* Buffer data port register */
47 uint prsstat; /* Present state register */
48 uint proctl; /* Protocol control register */
49 uint sysctl; /* System Control Register */
50 uint irqstat; /* Interrupt status register */
51 uint irqstaten; /* Interrupt status enable register */
52 uint irqsigen; /* Interrupt signal enable register */
53 uint autoc12err; /* Auto CMD error status register */
54 uint hostcapblt; /* Host controller capabilities register */
55 uint wml; /* Watermark level register */
56 uint mixctrl; /* For USDHC */
57 char reserved1[4]; /* reserved */
58 uint fevt; /* Force event register */
59 uint admaes; /* ADMA error status register */
60 uint adsaddr; /* ADMA system address register */
Peng Fanf53225c2016-06-15 10:53:00 +080061 char reserved2[4];
62 uint dllctrl;
63 uint dllstat;
64 uint clktunectrlstatus;
Peng Fan59d37822018-01-21 19:00:22 +080065 char reserved3[4];
66 uint strobe_dllctrl;
67 uint strobe_dllstat;
68 char reserved4[72];
Peng Fanf53225c2016-06-15 10:53:00 +080069 uint vendorspec;
70 uint mmcboot;
71 uint vendorspec2;
Peng Fan59d37822018-01-21 19:00:22 +080072 uint tuning_ctrl; /* on i.MX6/7/8 */
73 char reserved5[44];
Haijun.Zhang511948b2013-10-30 11:37:55 +080074 uint hostver; /* Host controller version register */
Otavio Salvadorf022d362015-02-17 10:42:43 -020075 char reserved6[4]; /* reserved */
Peng Fan59d37822018-01-21 19:00:22 +080076 uint dmaerraddr; /* DMA error address register */
Peng Fanf53225c2016-06-15 10:53:00 +080077 char reserved7[4]; /* reserved */
Peng Fan59d37822018-01-21 19:00:22 +080078 uint dmaerrattr; /* DMA error attribute register */
79 char reserved8[4]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080080 uint hostcapblt2; /* Host controller capabilities register 2 */
Peng Fan59d37822018-01-21 19:00:22 +080081 char reserved9[8]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080082 uint tcr; /* Tuning control register */
Peng Fan59d37822018-01-21 19:00:22 +080083 char reserved10[28]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080084 uint sddirctl; /* SD direction control register */
Peng Fan59d37822018-01-21 19:00:22 +080085 char reserved11[712];/* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080086 uint scr; /* eSDHC control register */
Andy Fleming50586ef2008-10-30 16:47:16 -050087};
88
Simon Glasse88e1d92017-07-29 11:35:21 -060089struct fsl_esdhc_plat {
90 struct mmc_config cfg;
91 struct mmc mmc;
92};
93
Peng Fan51313b42018-01-21 19:00:24 +080094struct esdhc_soc_data {
95 u32 flags;
96 u32 caps;
97};
98
Peng Fan96f04072016-03-25 14:16:56 +080099/**
100 * struct fsl_esdhc_priv
101 *
102 * @esdhc_regs: registers of the sdhc controller
103 * @sdhc_clk: Current clk of the sdhc controller
104 * @bus_width: bus width, 1bit, 4bit or 8bit
105 * @cfg: mmc config
106 * @mmc: mmc
107 * Following is used when Driver Model is enabled for MMC
108 * @dev: pointer for the device
109 * @non_removable: 0: removable; 1: non-removable
Peng Fan14831512016-06-15 10:53:02 +0800110 * @wp_enable: 1: enable checking wp; 0: no check
Peng Fan32a91792017-06-12 17:50:53 +0800111 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
Peng Fan51313b42018-01-21 19:00:24 +0800112 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc.h
113 * @caps: controller capabilities
114 * @tuning_step: tuning step setting in tuning_ctrl register
115 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
116 * @strobe_dll_delay_target: settings in strobe_dllctrl
117 * @signal_voltage: indicating the current voltage
Peng Fan96f04072016-03-25 14:16:56 +0800118 * @cd_gpio: gpio for card detection
Peng Fan14831512016-06-15 10:53:02 +0800119 * @wp_gpio: gpio for write protection
Peng Fan96f04072016-03-25 14:16:56 +0800120 */
121struct fsl_esdhc_priv {
122 struct fsl_esdhc *esdhc_regs;
123 unsigned int sdhc_clk;
Peng Fan51313b42018-01-21 19:00:24 +0800124 unsigned int clock;
125 unsigned int mode;
Peng Fan96f04072016-03-25 14:16:56 +0800126 unsigned int bus_width;
Simon Glass653282b2017-07-29 11:35:24 -0600127#if !CONFIG_IS_ENABLED(BLK)
Peng Fan96f04072016-03-25 14:16:56 +0800128 struct mmc *mmc;
Simon Glass653282b2017-07-29 11:35:24 -0600129#endif
Peng Fan96f04072016-03-25 14:16:56 +0800130 struct udevice *dev;
131 int non_removable;
Peng Fan14831512016-06-15 10:53:02 +0800132 int wp_enable;
Peng Fan32a91792017-06-12 17:50:53 +0800133 int vs18_enable;
Peng Fan51313b42018-01-21 19:00:24 +0800134 u32 flags;
135 u32 caps;
136 u32 tuning_step;
137 u32 tuning_start_tap;
138 u32 strobe_dll_delay_target;
139 u32 signal_voltage;
140#if IS_ENABLED(CONFIG_DM_REGULATOR)
141 struct udevice *vqmmc_dev;
142 struct udevice *vmmc_dev;
143#endif
Yangbo Lufc8048a2016-12-07 11:54:30 +0800144#ifdef CONFIG_DM_GPIO
Peng Fan96f04072016-03-25 14:16:56 +0800145 struct gpio_desc cd_gpio;
Peng Fan14831512016-06-15 10:53:02 +0800146 struct gpio_desc wp_gpio;
Yangbo Lufc8048a2016-12-07 11:54:30 +0800147#endif
Peng Fan96f04072016-03-25 14:16:56 +0800148};
149
Andy Fleming50586ef2008-10-30 16:47:16 -0500150/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +0000151static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500152{
153 uint xfertyp = 0;
154
155 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530156 xfertyp |= XFERTYP_DPSEL;
157#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
158 xfertyp |= XFERTYP_DMAEN;
159#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500160 if (data->blocks > 1) {
161 xfertyp |= XFERTYP_MSBSEL;
162 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -0600163#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
164 xfertyp |= XFERTYP_AC12EN;
165#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500166 }
167
168 if (data->flags & MMC_DATA_READ)
169 xfertyp |= XFERTYP_DTDSEL;
170 }
171
172 if (cmd->resp_type & MMC_RSP_CRC)
173 xfertyp |= XFERTYP_CCCEN;
174 if (cmd->resp_type & MMC_RSP_OPCODE)
175 xfertyp |= XFERTYP_CICEN;
176 if (cmd->resp_type & MMC_RSP_136)
177 xfertyp |= XFERTYP_RSPTYP_136;
178 else if (cmd->resp_type & MMC_RSP_BUSY)
179 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
180 else if (cmd->resp_type & MMC_RSP_PRESENT)
181 xfertyp |= XFERTYP_RSPTYP_48;
182
Jason Liu4571de32011-03-22 01:32:31 +0000183 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
184 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lu25503442016-01-21 17:33:19 +0800185
Andy Fleming50586ef2008-10-30 16:47:16 -0500186 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
187}
188
Dipen Dudhat77c14582009-10-05 15:41:58 +0530189#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
190/*
191 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
192 */
Simon Glass09b465f2017-07-29 11:35:17 -0600193static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
194 struct mmc_data *data)
Dipen Dudhat77c14582009-10-05 15:41:58 +0530195{
Peng Fan96f04072016-03-25 14:16:56 +0800196 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530197 uint blocks;
198 char *buffer;
199 uint databuf;
200 uint size;
201 uint irqstat;
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100202 ulong start;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530203
204 if (data->flags & MMC_DATA_READ) {
205 blocks = data->blocks;
206 buffer = data->dest;
207 while (blocks) {
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100208 start = get_timer(0);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530209 size = data->blocksize;
210 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100211 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
212 if (get_timer(start) > PIO_TIMEOUT) {
213 printf("\nData Read Failed in PIO Mode.");
214 return;
215 }
Dipen Dudhat77c14582009-10-05 15:41:58 +0530216 }
217 while (size && (!(irqstat & IRQSTAT_TC))) {
218 udelay(100); /* Wait before last byte transfer complete */
219 irqstat = esdhc_read32(&regs->irqstat);
220 databuf = in_le32(&regs->datport);
221 *((uint *)buffer) = databuf;
222 buffer += 4;
223 size -= 4;
224 }
225 blocks--;
226 }
227 } else {
228 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200229 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530230 while (blocks) {
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100231 start = get_timer(0);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530232 size = data->blocksize;
233 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100234 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
235 if (get_timer(start) > PIO_TIMEOUT) {
236 printf("\nData Write Failed in PIO Mode.");
237 return;
238 }
Dipen Dudhat77c14582009-10-05 15:41:58 +0530239 }
240 while (size && (!(irqstat & IRQSTAT_TC))) {
241 udelay(100); /* Wait before last byte transfer complete */
242 databuf = *((uint *)buffer);
243 buffer += 4;
244 size -= 4;
245 irqstat = esdhc_read32(&regs->irqstat);
246 out_le32(&regs->datport, databuf);
247 }
248 blocks--;
249 }
250 }
251}
252#endif
253
Simon Glass09b465f2017-07-29 11:35:17 -0600254static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
255 struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500256{
Andy Fleming50586ef2008-10-30 16:47:16 -0500257 int timeout;
Peng Fan96f04072016-03-25 14:16:56 +0800258 struct fsl_esdhc *regs = priv->esdhc_regs;
Peng Faneec2d432018-01-10 13:20:40 +0800259#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
260 defined(CONFIG_MX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700261 dma_addr_t addr;
262#endif
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200263 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500264
265 wml_value = data->blocksize/4;
266
267 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530268 if (wml_value > WML_RD_WML_MAX)
269 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500270
Roy Zangab467c52010-02-09 18:23:33 +0800271 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li71689772014-02-20 18:00:57 +0800272#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Peng Faneec2d432018-01-10 13:20:40 +0800273#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
274 defined(CONFIG_MX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700275 addr = virt_to_phys((void *)(data->dest));
276 if (upper_32_bits(addr))
277 printf("Error found for upper 32 bits\n");
278 else
279 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
280#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100281 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li71689772014-02-20 18:00:57 +0800282#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700283#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500284 } else {
Ye.Li71689772014-02-20 18:00:57 +0800285#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-04-25 14:28:48 +0000286 flush_dcache_range((ulong)data->src,
287 (ulong)data->src+data->blocks
288 *data->blocksize);
Ye.Li71689772014-02-20 18:00:57 +0800289#endif
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530290 if (wml_value > WML_WR_WML_MAX)
291 wml_value = WML_WR_WML_MAX_VAL;
Peng Fan14831512016-06-15 10:53:02 +0800292 if (priv->wp_enable) {
293 if ((esdhc_read32(&regs->prsstat) &
294 PRSSTAT_WPSPL) == 0) {
295 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900296 return -ETIMEDOUT;
Peng Fan14831512016-06-15 10:53:02 +0800297 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500298 }
Roy Zangab467c52010-02-09 18:23:33 +0800299
300 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
301 wml_value << 16);
Ye.Li71689772014-02-20 18:00:57 +0800302#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Peng Faneec2d432018-01-10 13:20:40 +0800303#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
304 defined(CONFIG_MX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700305 addr = virt_to_phys((void *)(data->src));
306 if (upper_32_bits(addr))
307 printf("Error found for upper 32 bits\n");
308 else
309 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
310#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100311 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li71689772014-02-20 18:00:57 +0800312#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700313#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500314 }
315
Stefano Babicc67bee12010-02-05 15:11:27 +0100316 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500317
318 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530319 /*
320 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
321 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
322 * So, Number of SD Clock cycles for 0.25sec should be minimum
323 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500324 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530325 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500326 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530327 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500328 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530329 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500330 * => timeout + 13 = log2(mmc->clock/4) + 1
331 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lue978a312015-12-30 14:19:30 +0800332 *
333 * However, the MMC spec "It is strongly recommended for hosts to
334 * implement more than 500ms timeout value even if the card
335 * indicates the 250ms maximum busy length." Even the previous
336 * value of 300ms is known to be insufficient for some cards.
337 * So, we use
338 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530339 */
Yangbo Lue978a312015-12-30 14:19:30 +0800340 timeout = fls(mmc->clock/2);
Andy Fleming50586ef2008-10-30 16:47:16 -0500341 timeout -= 13;
342
343 if (timeout > 14)
344 timeout = 14;
345
346 if (timeout < 0)
347 timeout = 0;
348
Kumar Gala5103a032011-01-29 15:36:10 -0600349#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
350 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
351 timeout++;
352#endif
353
Haijun.Zhang1336e2d2014-03-18 17:04:23 +0800354#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
355 timeout = 0xE;
356#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100357 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500358
359 return 0;
360}
361
Eric Nelsone576bd92012-04-25 14:28:48 +0000362static void check_and_invalidate_dcache_range
363 (struct mmc_cmd *cmd,
364 struct mmc_data *data) {
Yangbo Lu8b064602015-03-20 19:28:31 -0700365 unsigned start = 0;
Yangbo Lucc634e22016-05-12 19:12:58 +0800366 unsigned end = 0;
Eric Nelsone576bd92012-04-25 14:28:48 +0000367 unsigned size = roundup(ARCH_DMA_MINALIGN,
368 data->blocks*data->blocksize);
Peng Faneec2d432018-01-10 13:20:40 +0800369#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
370 defined(CONFIG_MX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700371 dma_addr_t addr;
372
373 addr = virt_to_phys((void *)(data->dest));
374 if (upper_32_bits(addr))
375 printf("Error found for upper 32 bits\n");
376 else
377 start = lower_32_bits(addr);
Yangbo Lucc634e22016-05-12 19:12:58 +0800378#else
379 start = (unsigned)data->dest;
Yangbo Lu8b064602015-03-20 19:28:31 -0700380#endif
Yangbo Lucc634e22016-05-12 19:12:58 +0800381 end = start + size;
Eric Nelsone576bd92012-04-25 14:28:48 +0000382 invalidate_dcache_range(start, end);
383}
Tom Rini10dc7772014-05-23 09:19:05 -0400384
Andy Fleming50586ef2008-10-30 16:47:16 -0500385/*
386 * Sends a command out on the bus. Takes the mmc pointer,
387 * a command pointer, and an optional data pointer.
388 */
Simon Glass9586aa62017-07-29 11:35:18 -0600389static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
390 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500391{
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500392 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500393 uint xfertyp;
394 uint irqstat;
Peng Fan51313b42018-01-21 19:00:24 +0800395 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fan96f04072016-03-25 14:16:56 +0800396 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500397
Jerry Huangd621da02011-01-06 23:42:19 -0600398#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
399 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
400 return 0;
401#endif
402
Stefano Babicc67bee12010-02-05 15:11:27 +0100403 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500404
405 sync();
406
407 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100408 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
409 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
410 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500411
Stefano Babicc67bee12010-02-05 15:11:27 +0100412 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
413 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500414
415 /* Wait at least 8 SD clock cycles before the next command */
416 /*
417 * Note: This is way more than 8 cycles, but 1ms seems to
418 * resolve timing issues with some cards
419 */
420 udelay(1000);
421
422 /* Set up for a data transfer if we have one */
423 if (data) {
Simon Glass09b465f2017-07-29 11:35:17 -0600424 err = esdhc_setup_data(priv, mmc, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500425 if(err)
426 return err;
Peng Fan4683b222015-06-25 10:32:26 +0800427
428 if (data->flags & MMC_DATA_READ)
429 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500430 }
431
432 /* Figure out the transfer arguments */
433 xfertyp = esdhc_xfertyp(cmd, data);
434
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500435 /* Mask all irqs */
436 esdhc_write32(&regs->irqsigen, 0);
437
Andy Fleming50586ef2008-10-30 16:47:16 -0500438 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100439 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-11-25 00:18:04 +0000440#if defined(CONFIG_FSL_USDHC)
441 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500442 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
443 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu46927082011-11-25 00:18:04 +0000444 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
445#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100446 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000447#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000448
Peng Fan51313b42018-01-21 19:00:24 +0800449 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
450 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
451 flags = IRQSTAT_BRR;
452
Andy Fleming50586ef2008-10-30 16:47:16 -0500453 /* Wait for the command to complete */
Peng Fan51313b42018-01-21 19:00:24 +0800454 while (!(esdhc_read32(&regs->irqstat) & flags))
Stefano Babicc67bee12010-02-05 15:11:27 +0100455 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500456
Stefano Babicc67bee12010-02-05 15:11:27 +0100457 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500458
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500459 if (irqstat & CMD_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900460 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500461 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000462 }
463
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500464 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900465 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500466 goto out;
467 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500468
Otavio Salvadorf022d362015-02-17 10:42:43 -0200469 /* Switch voltage to 1.8V if CMD11 succeeded */
470 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
471 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
472
473 printf("Run CMD11 1.8V switch\n");
474 /* Sleep for 5 ms - max time for card to switch to 1.8V */
475 udelay(5000);
476 }
477
Dirk Behme7a5b8022012-03-26 03:13:05 +0000478 /* Workaround for ESDHC errata ENGcm03648 */
479 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800480 int timeout = 6000;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000481
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800482 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000483 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
484 PRSSTAT_DAT0)) {
485 udelay(100);
486 timeout--;
487 }
488
489 if (timeout <= 0) {
490 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900491 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500492 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000493 }
494 }
495
Andy Fleming50586ef2008-10-30 16:47:16 -0500496 /* Copy the response to the response buffer */
497 if (cmd->resp_type & MMC_RSP_136) {
498 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
499
Stefano Babicc67bee12010-02-05 15:11:27 +0100500 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
501 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
502 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
503 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530504 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
505 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
506 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
507 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500508 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100509 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500510
511 /* Wait until all of the blocks are transferred */
512 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530513#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass09b465f2017-07-29 11:35:17 -0600514 esdhc_pio_read_write(priv, data);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530515#else
Peng Fan51313b42018-01-21 19:00:24 +0800516 flags = DATA_COMPLETE;
517 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
518 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
519 flags = IRQSTAT_BRR;
520 }
521
Andy Fleming50586ef2008-10-30 16:47:16 -0500522 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100523 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500524
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500525 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900526 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500527 goto out;
528 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000529
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500530 if (irqstat & DATA_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900531 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500532 goto out;
533 }
Peng Fan51313b42018-01-21 19:00:24 +0800534 } while ((irqstat & flags) != flags);
Ye.Li71689772014-02-20 18:00:57 +0800535
Peng Fan4683b222015-06-25 10:32:26 +0800536 /*
537 * Need invalidate the dcache here again to avoid any
538 * cache-fill during the DMA operations such as the
539 * speculative pre-fetching etc.
540 */
Eric Nelson54899fc2013-04-03 12:31:56 +0000541 if (data->flags & MMC_DATA_READ)
542 check_and_invalidate_dcache_range(cmd, data);
Ye.Li71689772014-02-20 18:00:57 +0800543#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500544 }
545
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500546out:
547 /* Reset CMD and DATA portions on error */
548 if (err) {
549 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
550 SYSCTL_RSTC);
551 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
552 ;
553
554 if (data) {
555 esdhc_write32(&regs->sysctl,
556 esdhc_read32(&regs->sysctl) |
557 SYSCTL_RSTD);
558 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
559 ;
560 }
Otavio Salvadorf022d362015-02-17 10:42:43 -0200561
562 /* If this was CMD11, then notify that power cycle is needed */
563 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
564 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500565 }
566
Stefano Babicc67bee12010-02-05 15:11:27 +0100567 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500568
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500569 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500570}
571
Simon Glass09b465f2017-07-29 11:35:17 -0600572static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500573{
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100574 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200575 int div = 1;
576#ifdef ARCH_MXC
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100577#ifdef CONFIG_MX53
578 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
579 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
580#else
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200581 int pre_div = 1;
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100582#endif
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200583#else
584 int pre_div = 2;
585#endif
586 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
Peng Fan96f04072016-03-25 14:16:56 +0800587 int sdhc_clk = priv->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500588 uint clk;
589
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200590 if (clock < mmc->cfg->f_min)
591 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100592
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200593 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
594 pre_div *= 2;
Andy Fleming50586ef2008-10-30 16:47:16 -0500595
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200596 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
597 div++;
Andy Fleming50586ef2008-10-30 16:47:16 -0500598
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200599 pre_div >>= 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500600 div -= 1;
601
602 clk = (pre_div << 8) | (div << 4);
603
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700604#ifdef CONFIG_FSL_USDHC
Ye Li84ecdf62016-06-15 10:53:01 +0800605 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700606#else
Kumar Galacc4d1222010-03-18 15:51:05 -0500607 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700608#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100609
610 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500611
612 udelay(10000);
613
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700614#ifdef CONFIG_FSL_USDHC
Ye Li84ecdf62016-06-15 10:53:01 +0800615 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700616#else
617 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
618#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100619
Peng Fan51313b42018-01-21 19:00:24 +0800620 priv->clock = clock;
Andy Fleming50586ef2008-10-30 16:47:16 -0500621}
622
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800623#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
Simon Glass09b465f2017-07-29 11:35:17 -0600624static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800625{
Peng Fan96f04072016-03-25 14:16:56 +0800626 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800627 u32 value;
628 u32 time_out;
629
630 value = esdhc_read32(&regs->sysctl);
631
632 if (enable)
633 value |= SYSCTL_CKEN;
634 else
635 value &= ~SYSCTL_CKEN;
636
637 esdhc_write32(&regs->sysctl, value);
638
639 time_out = 20;
640 value = PRSSTAT_SDSTB;
641 while (!(esdhc_read32(&regs->prsstat) & value)) {
642 if (time_out == 0) {
643 printf("fsl_esdhc: Internal clock never stabilised.\n");
644 break;
645 }
646 time_out--;
647 mdelay(1);
648 }
649}
650#endif
651
Peng Fan51313b42018-01-21 19:00:24 +0800652#ifdef MMC_SUPPORTS_TUNING
653static int esdhc_change_pinstate(struct udevice *dev)
654{
655 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
656 int ret;
657
658 switch (priv->mode) {
659 case UHS_SDR50:
660 case UHS_DDR50:
661 ret = pinctrl_select_state(dev, "state_100mhz");
662 break;
663 case UHS_SDR104:
664 case MMC_HS_200:
665 ret = pinctrl_select_state(dev, "state_200mhz");
666 break;
667 default:
668 ret = pinctrl_select_state(dev, "default");
669 break;
670 }
671
672 if (ret)
673 printf("%s %d error\n", __func__, priv->mode);
674
675 return ret;
676}
677
678static void esdhc_reset_tuning(struct mmc *mmc)
679{
680 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
681 struct fsl_esdhc *regs = priv->esdhc_regs;
682
683 if (priv->flags & ESDHC_FLAG_USDHC) {
684 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
685 esdhc_clrbits32(&regs->autoc12err,
686 MIX_CTRL_SMPCLK_SEL |
687 MIX_CTRL_EXE_TUNE);
688 }
689 }
690}
691
692static int esdhc_set_timing(struct mmc *mmc)
693{
694 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
695 struct fsl_esdhc *regs = priv->esdhc_regs;
696 u32 mixctrl;
697
698 mixctrl = readl(&regs->mixctrl);
699 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
700
701 switch (mmc->selected_mode) {
702 case MMC_LEGACY:
703 case SD_LEGACY:
704 esdhc_reset_tuning(mmc);
705 break;
706 case MMC_HS:
707 case MMC_HS_52:
708 case MMC_HS_200:
709 case SD_HS:
710 case UHS_SDR12:
711 case UHS_SDR25:
712 case UHS_SDR50:
713 case UHS_SDR104:
714 writel(mixctrl, &regs->mixctrl);
715 break;
716 case UHS_DDR50:
717 case MMC_DDR_52:
718 mixctrl |= MIX_CTRL_DDREN;
719 writel(mixctrl, &regs->mixctrl);
720 break;
721 default:
722 printf("Not supported %d\n", mmc->selected_mode);
723 return -EINVAL;
724 }
725
726 priv->mode = mmc->selected_mode;
727
728 return esdhc_change_pinstate(mmc->dev);
729}
730
731static int esdhc_set_voltage(struct mmc *mmc)
732{
733 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
734 struct fsl_esdhc *regs = priv->esdhc_regs;
735 int ret;
736
737 priv->signal_voltage = mmc->signal_voltage;
738 switch (mmc->signal_voltage) {
739 case MMC_SIGNAL_VOLTAGE_330:
740 if (priv->vs18_enable)
741 return -EIO;
742#ifdef CONFIG_DM_REGULATOR
743 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
744 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
745 if (ret) {
746 printf("Setting to 3.3V error");
747 return -EIO;
748 }
749 /* Wait for 5ms */
750 mdelay(5);
751 }
752#endif
753
754 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
755 if (!(esdhc_read32(&regs->vendorspec) &
756 ESDHC_VENDORSPEC_VSELECT))
757 return 0;
758
759 return -EAGAIN;
760 case MMC_SIGNAL_VOLTAGE_180:
761#ifdef CONFIG_DM_REGULATOR
762 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
763 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
764 if (ret) {
765 printf("Setting to 1.8V error");
766 return -EIO;
767 }
768 }
769#endif
770 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
771 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
772 return 0;
773
774 return -EAGAIN;
775 case MMC_SIGNAL_VOLTAGE_120:
776 return -ENOTSUPP;
777 default:
778 return 0;
779 }
780}
781
782static void esdhc_stop_tuning(struct mmc *mmc)
783{
784 struct mmc_cmd cmd;
785
786 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
787 cmd.cmdarg = 0;
788 cmd.resp_type = MMC_RSP_R1b;
789
790 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
791}
792
793static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
794{
795 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
796 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
797 struct fsl_esdhc *regs = priv->esdhc_regs;
798 struct mmc *mmc = &plat->mmc;
799 u32 irqstaten = readl(&regs->irqstaten);
800 u32 irqsigen = readl(&regs->irqsigen);
801 int i, ret = -ETIMEDOUT;
802 u32 val, mixctrl;
803
804 /* clock tuning is not needed for upto 52MHz */
805 if (mmc->clock <= 52000000)
806 return 0;
807
808 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
809 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
810 val = readl(&regs->autoc12err);
811 mixctrl = readl(&regs->mixctrl);
812 val &= ~MIX_CTRL_SMPCLK_SEL;
813 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
814
815 val |= MIX_CTRL_EXE_TUNE;
816 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
817
818 writel(val, &regs->autoc12err);
819 writel(mixctrl, &regs->mixctrl);
820 }
821
822 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
823 mixctrl = readl(&regs->mixctrl);
824 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
825 writel(mixctrl, &regs->mixctrl);
826
827 writel(IRQSTATEN_BRR, &regs->irqstaten);
828 writel(IRQSTATEN_BRR, &regs->irqsigen);
829
830 /*
831 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
832 * of loops reaches 40 times.
833 */
834 for (i = 0; i < MAX_TUNING_LOOP; i++) {
835 u32 ctrl;
836
837 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
838 if (mmc->bus_width == 8)
839 writel(0x7080, &regs->blkattr);
840 else if (mmc->bus_width == 4)
841 writel(0x7040, &regs->blkattr);
842 } else {
843 writel(0x7040, &regs->blkattr);
844 }
845
846 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
847 val = readl(&regs->mixctrl);
848 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
849 writel(val, &regs->mixctrl);
850
851 /* We are using STD tuning, no need to check return value */
852 mmc_send_tuning(mmc, opcode, NULL);
853
854 ctrl = readl(&regs->autoc12err);
855 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
856 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
857 /*
858 * need to wait some time, make sure sd/mmc fininsh
859 * send out tuning data, otherwise, the sd/mmc can't
860 * response to any command when the card still out
861 * put the tuning data.
862 */
863 mdelay(1);
864 ret = 0;
865 break;
866 }
867
868 /* Add 1ms delay for SD and eMMC */
869 mdelay(1);
870 }
871
872 writel(irqstaten, &regs->irqstaten);
873 writel(irqsigen, &regs->irqsigen);
874
875 esdhc_stop_tuning(mmc);
876
877 return ret;
878}
879#endif
880
Simon Glass9586aa62017-07-29 11:35:18 -0600881static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500882{
Peng Fan96f04072016-03-25 14:16:56 +0800883 struct fsl_esdhc *regs = priv->esdhc_regs;
Peng Fan51313b42018-01-21 19:00:24 +0800884 int ret __maybe_unused;
Andy Fleming50586ef2008-10-30 16:47:16 -0500885
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800886#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
887 /* Select to use peripheral clock */
Simon Glass09b465f2017-07-29 11:35:17 -0600888 esdhc_clock_control(priv, false);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800889 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
Simon Glass09b465f2017-07-29 11:35:17 -0600890 esdhc_clock_control(priv, true);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800891#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500892 /* Set the clock speed */
Peng Fan51313b42018-01-21 19:00:24 +0800893 if (priv->clock != mmc->clock)
894 set_sysctl(priv, mmc, mmc->clock);
895
896#ifdef MMC_SUPPORTS_TUNING
897 if (mmc->clk_disable) {
898#ifdef CONFIG_FSL_USDHC
899 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
900#else
901 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
902#endif
903 } else {
904#ifdef CONFIG_FSL_USDHC
905 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
906 VENDORSPEC_CKEN);
907#else
908 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
909#endif
910 }
911
912 if (priv->mode != mmc->selected_mode) {
913 ret = esdhc_set_timing(mmc);
914 if (ret) {
915 printf("esdhc_set_timing error %d\n", ret);
916 return ret;
917 }
918 }
919
920 if (priv->signal_voltage != mmc->signal_voltage) {
921 ret = esdhc_set_voltage(mmc);
922 if (ret) {
923 printf("esdhc_set_voltage error %d\n", ret);
924 return ret;
925 }
926 }
927#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500928
929 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100930 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500931
932 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100933 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500934 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100935 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
936
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900937 return 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500938}
939
Simon Glass9586aa62017-07-29 11:35:18 -0600940static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500941{
Peng Fan96f04072016-03-25 14:16:56 +0800942 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass201e8282017-07-29 11:35:20 -0600943 ulong start;
Andy Fleming50586ef2008-10-30 16:47:16 -0500944
Stefano Babicc67bee12010-02-05 15:11:27 +0100945 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200946 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100947
948 /* Wait until the controller is available */
Simon Glass201e8282017-07-29 11:35:20 -0600949 start = get_timer(0);
950 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
951 if (get_timer(start) > 1000)
952 return -ETIMEDOUT;
953 }
Stefano Babicc67bee12010-02-05 15:11:27 +0100954
Peng Fanf53225c2016-06-15 10:53:00 +0800955#if defined(CONFIG_FSL_USDHC)
956 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
957 esdhc_write32(&regs->mmcboot, 0x0);
958 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
959 esdhc_write32(&regs->mixctrl, 0x0);
960 esdhc_write32(&regs->clktunectrlstatus, 0x0);
961
962 /* Put VEND_SPEC to default value */
Peng Fandb359ef2018-01-02 16:51:22 +0800963 if (priv->vs18_enable)
964 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
965 ESDHC_VENDORSPEC_VSELECT));
966 else
967 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
Peng Fanf53225c2016-06-15 10:53:00 +0800968
969 /* Disable DLL_CTRL delay line */
970 esdhc_write32(&regs->dllctrl, 0x0);
971#endif
972
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000973#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530974 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +0000975 esdhc_write32(&regs->scr, 0x00000040);
976#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530977
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700978#ifndef CONFIG_FSL_USDHC
Dirk Behmea61da722013-07-15 15:44:29 +0200979 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Ye Li84ecdf62016-06-15 10:53:01 +0800980#else
981 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700982#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500983
984 /* Set the initial clock speed */
Jaehoon Chung65117182018-01-26 19:25:29 +0900985 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
Andy Fleming50586ef2008-10-30 16:47:16 -0500986
987 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100988 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500989
990 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100991 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500992
Stefano Babicc67bee12010-02-05 15:11:27 +0100993 /* Set timout to the maximum value */
994 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500995
Thierry Redingd48d2e22012-01-02 01:15:38 +0000996 return 0;
997}
Andy Fleming50586ef2008-10-30 16:47:16 -0500998
Simon Glass9586aa62017-07-29 11:35:18 -0600999static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Redingd48d2e22012-01-02 01:15:38 +00001000{
Peng Fan96f04072016-03-25 14:16:56 +08001001 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Redingd48d2e22012-01-02 01:15:38 +00001002 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +01001003
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +08001004#ifdef CONFIG_ESDHC_DETECT_QUIRK
1005 if (CONFIG_ESDHC_DETECT_QUIRK)
1006 return 1;
1007#endif
Peng Fan96f04072016-03-25 14:16:56 +08001008
Simon Glass653282b2017-07-29 11:35:24 -06001009#if CONFIG_IS_ENABLED(DM_MMC)
Peng Fan96f04072016-03-25 14:16:56 +08001010 if (priv->non_removable)
1011 return 1;
Yangbo Lufc8048a2016-12-07 11:54:30 +08001012#ifdef CONFIG_DM_GPIO
Peng Fan96f04072016-03-25 14:16:56 +08001013 if (dm_gpio_is_valid(&priv->cd_gpio))
1014 return dm_gpio_get_value(&priv->cd_gpio);
1015#endif
Yangbo Lufc8048a2016-12-07 11:54:30 +08001016#endif
Peng Fan96f04072016-03-25 14:16:56 +08001017
Thierry Redingd48d2e22012-01-02 01:15:38 +00001018 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1019 udelay(1000);
1020
1021 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -05001022}
1023
Simon Glass446e0772017-07-29 11:35:19 -06001024static int esdhc_reset(struct fsl_esdhc *regs)
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001025{
Simon Glass446e0772017-07-29 11:35:19 -06001026 ulong start;
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001027
1028 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +02001029 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001030
1031 /* hardware clears the bit when it is done */
Simon Glass446e0772017-07-29 11:35:19 -06001032 start = get_timer(0);
1033 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1034 if (get_timer(start) > 100) {
1035 printf("MMC/SD: Reset never completed.\n");
1036 return -ETIMEDOUT;
1037 }
1038 }
1039
1040 return 0;
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001041}
1042
Simon Glasse7881d82017-07-29 11:35:31 -06001043#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass9586aa62017-07-29 11:35:18 -06001044static int esdhc_getcd(struct mmc *mmc)
1045{
1046 struct fsl_esdhc_priv *priv = mmc->priv;
1047
1048 return esdhc_getcd_common(priv);
1049}
1050
1051static int esdhc_init(struct mmc *mmc)
1052{
1053 struct fsl_esdhc_priv *priv = mmc->priv;
1054
1055 return esdhc_init_common(priv, mmc);
1056}
1057
1058static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1059 struct mmc_data *data)
1060{
1061 struct fsl_esdhc_priv *priv = mmc->priv;
1062
1063 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1064}
1065
1066static int esdhc_set_ios(struct mmc *mmc)
1067{
1068 struct fsl_esdhc_priv *priv = mmc->priv;
1069
1070 return esdhc_set_ios_common(priv, mmc);
1071}
1072
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001073static const struct mmc_ops esdhc_ops = {
Simon Glass9586aa62017-07-29 11:35:18 -06001074 .getcd = esdhc_getcd,
1075 .init = esdhc_init,
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001076 .send_cmd = esdhc_send_cmd,
1077 .set_ios = esdhc_set_ios,
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001078};
Simon Glass653282b2017-07-29 11:35:24 -06001079#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001080
Simon Glasse88e1d92017-07-29 11:35:21 -06001081static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1082 struct fsl_esdhc_plat *plat)
Andy Fleming50586ef2008-10-30 16:47:16 -05001083{
Simon Glasse88e1d92017-07-29 11:35:21 -06001084 struct mmc_config *cfg;
Stefano Babicc67bee12010-02-05 15:11:27 +01001085 struct fsl_esdhc *regs;
Li Yang030955c2010-11-25 17:06:09 +00001086 u32 caps, voltage_caps;
Simon Glass446e0772017-07-29 11:35:19 -06001087 int ret;
Andy Fleming50586ef2008-10-30 16:47:16 -05001088
Peng Fan96f04072016-03-25 14:16:56 +08001089 if (!priv)
1090 return -EINVAL;
Stefano Babicc67bee12010-02-05 15:11:27 +01001091
Peng Fan96f04072016-03-25 14:16:56 +08001092 regs = priv->esdhc_regs;
Stefano Babicc67bee12010-02-05 15:11:27 +01001093
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001094 /* First reset the eSDHC controller */
Simon Glass446e0772017-07-29 11:35:19 -06001095 ret = esdhc_reset(regs);
1096 if (ret)
1097 return ret;
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001098
Eric Nelsonf0b5f232015-12-04 12:32:48 -07001099#ifndef CONFIG_FSL_USDHC
Jerry Huang975324a2012-05-17 23:57:02 +00001100 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1101 | SYSCTL_IPGEN | SYSCTL_CKEN);
Peng Fan51313b42018-01-21 19:00:24 +08001102 /* Clearing tuning bits in case ROM has set it already */
1103 esdhc_write32(&regs->mixctrl, 0);
1104 esdhc_write32(&regs->autoc12err, 0);
1105 esdhc_write32(&regs->clktunectrlstatus, 0);
Ye Li84ecdf62016-06-15 10:53:01 +08001106#else
1107 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1108 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -07001109#endif
Jerry Huang975324a2012-05-17 23:57:02 +00001110
Peng Fan32a91792017-06-12 17:50:53 +08001111 if (priv->vs18_enable)
1112 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1113
Ye.Lia3d6e382014-11-04 15:35:49 +08001114 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Simon Glasse88e1d92017-07-29 11:35:21 -06001115 cfg = &plat->cfg;
Simon Glass653282b2017-07-29 11:35:24 -06001116#ifndef CONFIG_DM_MMC
Simon Glasse88e1d92017-07-29 11:35:21 -06001117 memset(cfg, '\0', sizeof(*cfg));
Simon Glass653282b2017-07-29 11:35:24 -06001118#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001119
Li Yang030955c2010-11-25 17:06:09 +00001120 voltage_caps = 0;
Wang Huan19060bd2014-09-05 13:52:40 +08001121 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -06001122
1123#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1124 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1125 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1126#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +08001127
1128/* T4240 host controller capabilities register should have VS33 bit */
1129#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1130 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1131#endif
1132
Andy Fleming50586ef2008-10-30 16:47:16 -05001133 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +00001134 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -05001135 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +00001136 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -05001137 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +00001138 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1139
Simon Glasse88e1d92017-07-29 11:35:21 -06001140 cfg->name = "FSL_SDHC";
Simon Glasse7881d82017-07-29 11:35:31 -06001141#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glasse88e1d92017-07-29 11:35:21 -06001142 cfg->ops = &esdhc_ops;
Simon Glass653282b2017-07-29 11:35:24 -06001143#endif
Li Yang030955c2010-11-25 17:06:09 +00001144#ifdef CONFIG_SYS_SD_VOLTAGE
Simon Glasse88e1d92017-07-29 11:35:21 -06001145 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +00001146#else
Simon Glasse88e1d92017-07-29 11:35:21 -06001147 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +00001148#endif
Simon Glasse88e1d92017-07-29 11:35:21 -06001149 if ((cfg->voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +00001150 printf("voltage not supported by controller\n");
1151 return -1;
1152 }
Andy Fleming50586ef2008-10-30 16:47:16 -05001153
Peng Fan96f04072016-03-25 14:16:56 +08001154 if (priv->bus_width == 8)
Simon Glasse88e1d92017-07-29 11:35:21 -06001155 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +08001156 else if (priv->bus_width == 4)
Simon Glasse88e1d92017-07-29 11:35:21 -06001157 cfg->host_caps = MMC_MODE_4BIT;
Peng Fan96f04072016-03-25 14:16:56 +08001158
Simon Glasse88e1d92017-07-29 11:35:21 -06001159 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -05001160#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Simon Glasse88e1d92017-07-29 11:35:21 -06001161 cfg->host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -05001162#endif
Andy Fleming50586ef2008-10-30 16:47:16 -05001163
Peng Fan96f04072016-03-25 14:16:56 +08001164 if (priv->bus_width > 0) {
1165 if (priv->bus_width < 8)
Simon Glasse88e1d92017-07-29 11:35:21 -06001166 cfg->host_caps &= ~MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +08001167 if (priv->bus_width < 4)
Simon Glasse88e1d92017-07-29 11:35:21 -06001168 cfg->host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +00001169 }
1170
Andy Fleming50586ef2008-10-30 16:47:16 -05001171 if (caps & ESDHC_HOSTCAPBLT_HSS)
Simon Glasse88e1d92017-07-29 11:35:21 -06001172 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -05001173
Haijun.Zhangd47e3d22014-01-10 13:52:18 +08001174#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1175 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Simon Glasse88e1d92017-07-29 11:35:21 -06001176 cfg->host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +08001177#endif
1178
Peng Fan51313b42018-01-21 19:00:24 +08001179 cfg->host_caps |= priv->caps;
1180
Simon Glasse88e1d92017-07-29 11:35:21 -06001181 cfg->f_min = 400000;
Peng Fan51313b42018-01-21 19:00:24 +08001182 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Andy Fleming50586ef2008-10-30 16:47:16 -05001183
Simon Glasse88e1d92017-07-29 11:35:21 -06001184 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001185
Peng Fan51313b42018-01-21 19:00:24 +08001186 writel(0, &regs->dllctrl);
1187 if (priv->flags & ESDHC_FLAG_USDHC) {
1188 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
1189 u32 val = readl(&regs->tuning_ctrl);
1190
1191 val |= ESDHC_STD_TUNING_EN;
1192 val &= ~ESDHC_TUNING_START_TAP_MASK;
1193 val |= priv->tuning_start_tap;
1194 val &= ~ESDHC_TUNING_STEP_MASK;
1195 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
1196 writel(val, &regs->tuning_ctrl);
1197 }
1198 }
1199
Peng Fan96f04072016-03-25 14:16:56 +08001200 return 0;
1201}
1202
Simon Glass52489302017-07-29 11:35:28 -06001203#if !CONFIG_IS_ENABLED(DM_MMC)
Jagan Teki2e87c442017-05-12 17:18:20 +05301204static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1205 struct fsl_esdhc_priv *priv)
1206{
1207 if (!cfg || !priv)
1208 return -EINVAL;
1209
1210 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1211 priv->bus_width = cfg->max_bus_width;
1212 priv->sdhc_clk = cfg->sdhc_clk;
1213 priv->wp_enable = cfg->wp_enable;
Peng Fan32a91792017-06-12 17:50:53 +08001214 priv->vs18_enable = cfg->vs18_enable;
Jagan Teki2e87c442017-05-12 17:18:20 +05301215
1216 return 0;
1217};
1218
Peng Fan96f04072016-03-25 14:16:56 +08001219int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
1220{
Simon Glasse88e1d92017-07-29 11:35:21 -06001221 struct fsl_esdhc_plat *plat;
Peng Fan96f04072016-03-25 14:16:56 +08001222 struct fsl_esdhc_priv *priv;
Simon Glassd6eb25e2017-07-29 11:35:22 -06001223 struct mmc *mmc;
Peng Fan96f04072016-03-25 14:16:56 +08001224 int ret;
1225
1226 if (!cfg)
1227 return -EINVAL;
1228
1229 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1230 if (!priv)
1231 return -ENOMEM;
Simon Glasse88e1d92017-07-29 11:35:21 -06001232 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1233 if (!plat) {
1234 free(priv);
1235 return -ENOMEM;
1236 }
Peng Fan96f04072016-03-25 14:16:56 +08001237
1238 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1239 if (ret) {
1240 debug("%s xlate failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -06001241 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +08001242 free(priv);
1243 return ret;
1244 }
1245
Simon Glasse88e1d92017-07-29 11:35:21 -06001246 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +08001247 if (ret) {
1248 debug("%s init failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -06001249 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +08001250 free(priv);
1251 return ret;
1252 }
1253
Simon Glassd6eb25e2017-07-29 11:35:22 -06001254 mmc = mmc_create(&plat->cfg, priv);
1255 if (!mmc)
1256 return -EIO;
1257
1258 priv->mmc = mmc;
1259
Andy Fleming50586ef2008-10-30 16:47:16 -05001260 return 0;
1261}
1262
1263int fsl_esdhc_mmc_init(bd_t *bis)
1264{
Stefano Babicc67bee12010-02-05 15:11:27 +01001265 struct fsl_esdhc_cfg *cfg;
1266
Fabio Estevam88227a12012-12-27 08:51:08 +00001267 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +01001268 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +00001269 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +01001270 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -05001271}
Jagan Teki2e87c442017-05-12 17:18:20 +05301272#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001273
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001274#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1275void mmc_adapter_card_type_ident(void)
1276{
1277 u8 card_id;
1278 u8 value;
1279
1280 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
1281 gd->arch.sdhc_adapter = card_id;
1282
1283 switch (card_id) {
1284 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lucdc69552015-09-17 10:27:12 +08001285 value = QIXIS_READ(brdcfg[5]);
1286 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
1287 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001288 break;
1289 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Lubf50be82015-09-17 10:27:48 +08001290 value = QIXIS_READ(pwr_ctl[1]);
1291 value |= QIXIS_EVDD_BY_SDHC_VS;
1292 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001293 break;
1294 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
1295 value = QIXIS_READ(brdcfg[5]);
1296 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
1297 QIXIS_WRITE(brdcfg[5], value);
1298 break;
1299 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
1300 break;
1301 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
1302 break;
1303 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
1304 break;
1305 case QIXIS_ESDHC_NO_ADAPTER:
1306 break;
1307 default:
1308 break;
1309 }
1310}
1311#endif
1312
Stefano Babicc67bee12010-02-05 15:11:27 +01001313#ifdef CONFIG_OF_LIBFDT
Yangbo Lufce1e162017-01-17 10:43:54 +08001314__weak int esdhc_status_fixup(void *blob, const char *compat)
1315{
1316#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1317 if (!hwconfig("esdhc")) {
1318 do_fixup_by_compat(blob, compat, "status", "disabled",
1319 sizeof("disabled"), 1);
1320 return 1;
1321 }
1322#endif
Yangbo Lufce1e162017-01-17 10:43:54 +08001323 return 0;
1324}
1325
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001326void fdt_fixup_esdhc(void *blob, bd_t *bd)
1327{
1328 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001329
Yangbo Lufce1e162017-01-17 10:43:54 +08001330 if (esdhc_status_fixup(blob, compat))
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +08001331 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001332
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +08001333#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
1334 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
1335 gd->arch.sdhc_clk, 1);
1336#else
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001337 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +00001338 gd->arch.sdhc_clk, 1);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +08001339#endif
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001340#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1341 do_fixup_by_compat_u32(blob, compat, "adapter-type",
1342 (u32)(gd->arch.sdhc_adapter), 1);
1343#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001344}
Stefano Babicc67bee12010-02-05 15:11:27 +01001345#endif
Peng Fan96f04072016-03-25 14:16:56 +08001346
Simon Glass653282b2017-07-29 11:35:24 -06001347#if CONFIG_IS_ENABLED(DM_MMC)
Peng Fan96f04072016-03-25 14:16:56 +08001348#include <asm/arch/clock.h>
Peng Fanb60f1452017-02-22 16:21:55 +08001349__weak void init_clk_usdhc(u32 index)
1350{
1351}
1352
Peng Fan96f04072016-03-25 14:16:56 +08001353static int fsl_esdhc_probe(struct udevice *dev)
1354{
1355 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glasse88e1d92017-07-29 11:35:21 -06001356 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fan96f04072016-03-25 14:16:56 +08001357 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Peng Fan51313b42018-01-21 19:00:24 +08001358 const void *fdt = gd->fdt_blob;
1359 int node = dev_of_offset(dev);
1360 struct esdhc_soc_data *data =
1361 (struct esdhc_soc_data *)dev_get_driver_data(dev);
York Sun9bb272e2017-08-08 15:45:13 -07001362#ifdef CONFIG_DM_REGULATOR
Peng Fan4483b7e2017-06-12 17:50:54 +08001363 struct udevice *vqmmc_dev;
York Sun9bb272e2017-08-08 15:45:13 -07001364#endif
Peng Fan96f04072016-03-25 14:16:56 +08001365 fdt_addr_t addr;
1366 unsigned int val;
Simon Glass653282b2017-07-29 11:35:24 -06001367 struct mmc *mmc;
Peng Fan96f04072016-03-25 14:16:56 +08001368 int ret;
1369
Simon Glass4aac33f2017-07-29 11:35:23 -06001370 addr = dev_read_addr(dev);
Peng Fan96f04072016-03-25 14:16:56 +08001371 if (addr == FDT_ADDR_T_NONE)
1372 return -EINVAL;
1373
1374 priv->esdhc_regs = (struct fsl_esdhc *)addr;
1375 priv->dev = dev;
Peng Fan51313b42018-01-21 19:00:24 +08001376 priv->mode = -1;
1377 if (data) {
1378 priv->flags = data->flags;
1379 priv->caps = data->caps;
1380 }
Peng Fan96f04072016-03-25 14:16:56 +08001381
Simon Glass4aac33f2017-07-29 11:35:23 -06001382 val = dev_read_u32_default(dev, "bus-width", -1);
Peng Fan96f04072016-03-25 14:16:56 +08001383 if (val == 8)
1384 priv->bus_width = 8;
1385 else if (val == 4)
1386 priv->bus_width = 4;
1387 else
1388 priv->bus_width = 1;
1389
Peng Fan51313b42018-01-21 19:00:24 +08001390 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1391 priv->tuning_step = val;
1392 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1393 ESDHC_TUNING_START_TAP_DEFAULT);
1394 priv->tuning_start_tap = val;
1395 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1396 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1397 priv->strobe_dll_delay_target = val;
1398
Simon Glass4aac33f2017-07-29 11:35:23 -06001399 if (dev_read_bool(dev, "non-removable")) {
Peng Fan96f04072016-03-25 14:16:56 +08001400 priv->non_removable = 1;
1401 } else {
1402 priv->non_removable = 0;
Yangbo Lufc8048a2016-12-07 11:54:30 +08001403#ifdef CONFIG_DM_GPIO
Simon Glass4aac33f2017-07-29 11:35:23 -06001404 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1405 GPIOD_IS_IN);
Yangbo Lufc8048a2016-12-07 11:54:30 +08001406#endif
Peng Fan96f04072016-03-25 14:16:56 +08001407 }
1408
Peng Fan14831512016-06-15 10:53:02 +08001409 priv->wp_enable = 1;
1410
Yangbo Lufc8048a2016-12-07 11:54:30 +08001411#ifdef CONFIG_DM_GPIO
Simon Glass4aac33f2017-07-29 11:35:23 -06001412 ret = gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1413 GPIOD_IS_IN);
Peng Fan14831512016-06-15 10:53:02 +08001414 if (ret)
1415 priv->wp_enable = 0;
Yangbo Lufc8048a2016-12-07 11:54:30 +08001416#endif
Peng Fan4483b7e2017-06-12 17:50:54 +08001417
1418 priv->vs18_enable = 0;
1419
1420#ifdef CONFIG_DM_REGULATOR
1421 /*
1422 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1423 * otherwise, emmc will work abnormally.
1424 */
1425 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1426 if (ret) {
1427 dev_dbg(dev, "no vqmmc-supply\n");
1428 } else {
1429 ret = regulator_set_enable(vqmmc_dev, true);
1430 if (ret) {
1431 dev_err(dev, "fail to enable vqmmc-supply\n");
1432 return ret;
1433 }
1434
1435 if (regulator_get_value(vqmmc_dev) == 1800000)
1436 priv->vs18_enable = 1;
1437 }
1438#endif
1439
Peng Fan51313b42018-01-21 19:00:24 +08001440 if (fdt_get_property(fdt, node, "no-1-8-v", NULL))
1441 priv->caps &= ~(UHS_CAPS | MMC_MODE_HS200);
1442
Peng Fan96f04072016-03-25 14:16:56 +08001443 /*
1444 * TODO:
1445 * Because lack of clk driver, if SDHC clk is not enabled,
1446 * need to enable it first before this driver is invoked.
1447 *
1448 * we use MXC_ESDHC_CLK to get clk freq.
1449 * If one would like to make this function work,
1450 * the aliases should be provided in dts as this:
1451 *
1452 * aliases {
1453 * mmc0 = &usdhc1;
1454 * mmc1 = &usdhc2;
1455 * mmc2 = &usdhc3;
1456 * mmc3 = &usdhc4;
1457 * };
1458 * Then if your board only supports mmc2 and mmc3, but we can
1459 * correctly get the seq as 2 and 3, then let mxc_get_clock
1460 * work as expected.
1461 */
Peng Fanb60f1452017-02-22 16:21:55 +08001462
1463 init_clk_usdhc(dev->seq);
1464
Peng Fan96f04072016-03-25 14:16:56 +08001465 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1466 if (priv->sdhc_clk <= 0) {
1467 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1468 return -EINVAL;
1469 }
1470
Simon Glasse88e1d92017-07-29 11:35:21 -06001471 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +08001472 if (ret) {
1473 dev_err(dev, "fsl_esdhc_init failure\n");
1474 return ret;
1475 }
1476
Simon Glass653282b2017-07-29 11:35:24 -06001477 mmc = &plat->mmc;
1478 mmc->cfg = &plat->cfg;
1479 mmc->dev = dev;
1480 upriv->mmc = mmc;
Peng Fan96f04072016-03-25 14:16:56 +08001481
Simon Glass653282b2017-07-29 11:35:24 -06001482 return esdhc_init_common(priv, mmc);
Peng Fan96f04072016-03-25 14:16:56 +08001483}
1484
Simon Glasse7881d82017-07-29 11:35:31 -06001485#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass653282b2017-07-29 11:35:24 -06001486static int fsl_esdhc_get_cd(struct udevice *dev)
1487{
1488 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1489
1490 return true;
1491 return esdhc_getcd_common(priv);
1492}
1493
1494static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1495 struct mmc_data *data)
1496{
1497 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1498 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1499
1500 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1501}
1502
1503static int fsl_esdhc_set_ios(struct udevice *dev)
1504{
1505 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1506 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1507
1508 return esdhc_set_ios_common(priv, &plat->mmc);
1509}
1510
1511static const struct dm_mmc_ops fsl_esdhc_ops = {
1512 .get_cd = fsl_esdhc_get_cd,
1513 .send_cmd = fsl_esdhc_send_cmd,
1514 .set_ios = fsl_esdhc_set_ios,
Peng Fan51313b42018-01-21 19:00:24 +08001515#ifdef MMC_SUPPORTS_TUNING
1516 .execute_tuning = fsl_esdhc_execute_tuning,
1517#endif
Simon Glass653282b2017-07-29 11:35:24 -06001518};
1519#endif
1520
Peng Fan51313b42018-01-21 19:00:24 +08001521static struct esdhc_soc_data usdhc_imx7d_data = {
1522 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1523 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1524 | ESDHC_FLAG_HS400,
1525 .caps = UHS_CAPS | MMC_MODE_HS200 | MMC_MODE_DDR_52MHz |
1526 MMC_MODE_HS_52MHz | MMC_MODE_HS,
1527};
1528
Peng Fan96f04072016-03-25 14:16:56 +08001529static const struct udevice_id fsl_esdhc_ids[] = {
1530 { .compatible = "fsl,imx6ul-usdhc", },
1531 { .compatible = "fsl,imx6sx-usdhc", },
1532 { .compatible = "fsl,imx6sl-usdhc", },
1533 { .compatible = "fsl,imx6q-usdhc", },
Peng Fan51313b42018-01-21 19:00:24 +08001534 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
Peng Fanb60f1452017-02-22 16:21:55 +08001535 { .compatible = "fsl,imx7ulp-usdhc", },
Yangbo Lua6473f82016-12-07 11:54:31 +08001536 { .compatible = "fsl,esdhc", },
Peng Fan96f04072016-03-25 14:16:56 +08001537 { /* sentinel */ }
1538};
1539
Simon Glass653282b2017-07-29 11:35:24 -06001540#if CONFIG_IS_ENABLED(BLK)
1541static int fsl_esdhc_bind(struct udevice *dev)
1542{
1543 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1544
1545 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1546}
1547#endif
1548
Peng Fan96f04072016-03-25 14:16:56 +08001549U_BOOT_DRIVER(fsl_esdhc) = {
1550 .name = "fsl-esdhc-mmc",
1551 .id = UCLASS_MMC,
1552 .of_match = fsl_esdhc_ids,
Simon Glass653282b2017-07-29 11:35:24 -06001553 .ops = &fsl_esdhc_ops,
Simon Glass653282b2017-07-29 11:35:24 -06001554#if CONFIG_IS_ENABLED(BLK)
1555 .bind = fsl_esdhc_bind,
1556#endif
Peng Fan96f04072016-03-25 14:16:56 +08001557 .probe = fsl_esdhc_probe,
Simon Glasse88e1d92017-07-29 11:35:21 -06001558 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fan96f04072016-03-25 14:16:56 +08001559 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1560};
1561#endif