blob: 6a191a1765473331997271ca6b7d3a8aa6ac6cf1 [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
Yangbo Lu66fa0352019-05-23 11:05:46 +08004 * Copyright 2019 NXP Semiconductors
Andy Fleming50586ef2008-10-30 16:47:16 -05005 * Andy Fleming
6 *
7 * Based vaguely on the pxa mmc code:
8 * (C) Copyright 2003
9 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
Andy Fleming50586ef2008-10-30 16:47:16 -050010 */
11
12#include <config.h>
13#include <common.h>
14#include <command.h>
Peng Fan3cb14502018-10-18 14:28:35 +020015#include <clk.h>
Jaehoon Chung915ffa52016-07-19 16:33:36 +090016#include <errno.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040017#include <hwconfig.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050018#include <mmc.h>
19#include <part.h>
Peng Fan4483b7e2017-06-12 17:50:54 +080020#include <power/regulator.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050021#include <malloc.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050022#include <fsl_esdhc.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040023#include <fdt_support.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050024#include <asm/io.h>
Peng Fan96f04072016-03-25 14:16:56 +080025#include <dm.h>
26#include <asm-generic/gpio.h>
Peng Fan51313b42018-01-21 19:00:24 +080027#include <dm/pinctrl.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050028
Yangbo Lu66fa0352019-05-23 11:05:46 +080029#if !CONFIG_IS_ENABLED(BLK)
30#include "mmc_private.h"
31#endif
32
Andy Fleming50586ef2008-10-30 16:47:16 -050033DECLARE_GLOBAL_DATA_PTR;
34
Ye.Lia3d6e382014-11-04 15:35:49 +080035#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
36 IRQSTATEN_CINT | \
37 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
38 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
39 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
40 IRQSTATEN_DINT)
Peng Fan51313b42018-01-21 19:00:24 +080041#define MAX_TUNING_LOOP 40
Yangbo Lu66fa0352019-05-23 11:05:46 +080042#define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
Ye.Lia3d6e382014-11-04 15:35:49 +080043
Andy Fleming50586ef2008-10-30 16:47:16 -050044struct fsl_esdhc {
Haijun.Zhang511948b2013-10-30 11:37:55 +080045 uint dsaddr; /* SDMA system address register */
46 uint blkattr; /* Block attributes register */
47 uint cmdarg; /* Command argument register */
48 uint xfertyp; /* Transfer type register */
49 uint cmdrsp0; /* Command response 0 register */
50 uint cmdrsp1; /* Command response 1 register */
51 uint cmdrsp2; /* Command response 2 register */
52 uint cmdrsp3; /* Command response 3 register */
53 uint datport; /* Buffer data port register */
54 uint prsstat; /* Present state register */
55 uint proctl; /* Protocol control register */
56 uint sysctl; /* System Control Register */
57 uint irqstat; /* Interrupt status register */
58 uint irqstaten; /* Interrupt status enable register */
59 uint irqsigen; /* Interrupt signal enable register */
60 uint autoc12err; /* Auto CMD error status register */
61 uint hostcapblt; /* Host controller capabilities register */
62 uint wml; /* Watermark level register */
63 uint mixctrl; /* For USDHC */
64 char reserved1[4]; /* reserved */
65 uint fevt; /* Force event register */
66 uint admaes; /* ADMA error status register */
67 uint adsaddr; /* ADMA system address register */
Peng Fanf53225c2016-06-15 10:53:00 +080068 char reserved2[4];
69 uint dllctrl;
70 uint dllstat;
71 uint clktunectrlstatus;
Peng Fan59d37822018-01-21 19:00:22 +080072 char reserved3[4];
73 uint strobe_dllctrl;
74 uint strobe_dllstat;
75 char reserved4[72];
Peng Fanf53225c2016-06-15 10:53:00 +080076 uint vendorspec;
77 uint mmcboot;
78 uint vendorspec2;
Peng Fan59d37822018-01-21 19:00:22 +080079 uint tuning_ctrl; /* on i.MX6/7/8 */
80 char reserved5[44];
Haijun.Zhang511948b2013-10-30 11:37:55 +080081 uint hostver; /* Host controller version register */
Otavio Salvadorf022d362015-02-17 10:42:43 -020082 char reserved6[4]; /* reserved */
Peng Fan59d37822018-01-21 19:00:22 +080083 uint dmaerraddr; /* DMA error address register */
Peng Fanf53225c2016-06-15 10:53:00 +080084 char reserved7[4]; /* reserved */
Peng Fan59d37822018-01-21 19:00:22 +080085 uint dmaerrattr; /* DMA error attribute register */
86 char reserved8[4]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080087 uint hostcapblt2; /* Host controller capabilities register 2 */
Peng Fan59d37822018-01-21 19:00:22 +080088 char reserved9[8]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080089 uint tcr; /* Tuning control register */
Peng Fan59d37822018-01-21 19:00:22 +080090 char reserved10[28]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080091 uint sddirctl; /* SD direction control register */
Peng Fan59d37822018-01-21 19:00:22 +080092 char reserved11[712];/* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080093 uint scr; /* eSDHC control register */
Andy Fleming50586ef2008-10-30 16:47:16 -050094};
95
Simon Glasse88e1d92017-07-29 11:35:21 -060096struct fsl_esdhc_plat {
97 struct mmc_config cfg;
98 struct mmc mmc;
99};
100
Peng Fan51313b42018-01-21 19:00:24 +0800101struct esdhc_soc_data {
102 u32 flags;
103 u32 caps;
104};
105
Peng Fan96f04072016-03-25 14:16:56 +0800106/**
107 * struct fsl_esdhc_priv
108 *
109 * @esdhc_regs: registers of the sdhc controller
110 * @sdhc_clk: Current clk of the sdhc controller
111 * @bus_width: bus width, 1bit, 4bit or 8bit
112 * @cfg: mmc config
113 * @mmc: mmc
114 * Following is used when Driver Model is enabled for MMC
115 * @dev: pointer for the device
116 * @non_removable: 0: removable; 1: non-removable
Peng Fan14831512016-06-15 10:53:02 +0800117 * @wp_enable: 1: enable checking wp; 0: no check
Peng Fan32a91792017-06-12 17:50:53 +0800118 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
Peng Fan51313b42018-01-21 19:00:24 +0800119 * @flags: ESDHC_FLAG_xx in include/fsl_esdhc.h
120 * @caps: controller capabilities
121 * @tuning_step: tuning step setting in tuning_ctrl register
122 * @start_tuning_tap: the start point for tuning in tuning_ctrl register
123 * @strobe_dll_delay_target: settings in strobe_dllctrl
124 * @signal_voltage: indicating the current voltage
Peng Fan96f04072016-03-25 14:16:56 +0800125 * @cd_gpio: gpio for card detection
Peng Fan14831512016-06-15 10:53:02 +0800126 * @wp_gpio: gpio for write protection
Peng Fan96f04072016-03-25 14:16:56 +0800127 */
128struct fsl_esdhc_priv {
129 struct fsl_esdhc *esdhc_regs;
130 unsigned int sdhc_clk;
Peng Fan3cb14502018-10-18 14:28:35 +0200131 struct clk per_clk;
Peng Fan51313b42018-01-21 19:00:24 +0800132 unsigned int clock;
133 unsigned int mode;
Peng Fan96f04072016-03-25 14:16:56 +0800134 unsigned int bus_width;
Simon Glass653282b2017-07-29 11:35:24 -0600135#if !CONFIG_IS_ENABLED(BLK)
Peng Fan96f04072016-03-25 14:16:56 +0800136 struct mmc *mmc;
Simon Glass653282b2017-07-29 11:35:24 -0600137#endif
Peng Fan96f04072016-03-25 14:16:56 +0800138 struct udevice *dev;
139 int non_removable;
Peng Fan14831512016-06-15 10:53:02 +0800140 int wp_enable;
Peng Fan32a91792017-06-12 17:50:53 +0800141 int vs18_enable;
Peng Fan51313b42018-01-21 19:00:24 +0800142 u32 flags;
143 u32 caps;
144 u32 tuning_step;
145 u32 tuning_start_tap;
146 u32 strobe_dll_delay_target;
147 u32 signal_voltage;
148#if IS_ENABLED(CONFIG_DM_REGULATOR)
149 struct udevice *vqmmc_dev;
150 struct udevice *vmmc_dev;
151#endif
Yangbo Lufc8048a2016-12-07 11:54:30 +0800152#ifdef CONFIG_DM_GPIO
Peng Fan96f04072016-03-25 14:16:56 +0800153 struct gpio_desc cd_gpio;
Peng Fan14831512016-06-15 10:53:02 +0800154 struct gpio_desc wp_gpio;
Yangbo Lufc8048a2016-12-07 11:54:30 +0800155#endif
Peng Fan96f04072016-03-25 14:16:56 +0800156};
157
Andy Fleming50586ef2008-10-30 16:47:16 -0500158/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +0000159static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500160{
161 uint xfertyp = 0;
162
163 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530164 xfertyp |= XFERTYP_DPSEL;
165#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
166 xfertyp |= XFERTYP_DMAEN;
167#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500168 if (data->blocks > 1) {
169 xfertyp |= XFERTYP_MSBSEL;
170 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -0600171#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
172 xfertyp |= XFERTYP_AC12EN;
173#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500174 }
175
176 if (data->flags & MMC_DATA_READ)
177 xfertyp |= XFERTYP_DTDSEL;
178 }
179
180 if (cmd->resp_type & MMC_RSP_CRC)
181 xfertyp |= XFERTYP_CCCEN;
182 if (cmd->resp_type & MMC_RSP_OPCODE)
183 xfertyp |= XFERTYP_CICEN;
184 if (cmd->resp_type & MMC_RSP_136)
185 xfertyp |= XFERTYP_RSPTYP_136;
186 else if (cmd->resp_type & MMC_RSP_BUSY)
187 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
188 else if (cmd->resp_type & MMC_RSP_PRESENT)
189 xfertyp |= XFERTYP_RSPTYP_48;
190
Jason Liu4571de32011-03-22 01:32:31 +0000191 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
192 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lu25503442016-01-21 17:33:19 +0800193
Andy Fleming50586ef2008-10-30 16:47:16 -0500194 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
195}
196
Dipen Dudhat77c14582009-10-05 15:41:58 +0530197#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
198/*
199 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
200 */
Simon Glass09b465f2017-07-29 11:35:17 -0600201static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
202 struct mmc_data *data)
Dipen Dudhat77c14582009-10-05 15:41:58 +0530203{
Peng Fan96f04072016-03-25 14:16:56 +0800204 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530205 uint blocks;
206 char *buffer;
207 uint databuf;
208 uint size;
209 uint irqstat;
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100210 ulong start;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530211
212 if (data->flags & MMC_DATA_READ) {
213 blocks = data->blocks;
214 buffer = data->dest;
215 while (blocks) {
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100216 start = get_timer(0);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530217 size = data->blocksize;
218 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100219 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
220 if (get_timer(start) > PIO_TIMEOUT) {
221 printf("\nData Read Failed in PIO Mode.");
222 return;
223 }
Dipen Dudhat77c14582009-10-05 15:41:58 +0530224 }
225 while (size && (!(irqstat & IRQSTAT_TC))) {
226 udelay(100); /* Wait before last byte transfer complete */
227 irqstat = esdhc_read32(&regs->irqstat);
228 databuf = in_le32(&regs->datport);
229 *((uint *)buffer) = databuf;
230 buffer += 4;
231 size -= 4;
232 }
233 blocks--;
234 }
235 } else {
236 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200237 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530238 while (blocks) {
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100239 start = get_timer(0);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530240 size = data->blocksize;
241 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100242 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
243 if (get_timer(start) > PIO_TIMEOUT) {
244 printf("\nData Write Failed in PIO Mode.");
245 return;
246 }
Dipen Dudhat77c14582009-10-05 15:41:58 +0530247 }
248 while (size && (!(irqstat & IRQSTAT_TC))) {
249 udelay(100); /* Wait before last byte transfer complete */
250 databuf = *((uint *)buffer);
251 buffer += 4;
252 size -= 4;
253 irqstat = esdhc_read32(&regs->irqstat);
254 out_le32(&regs->datport, databuf);
255 }
256 blocks--;
257 }
258 }
259}
260#endif
261
Simon Glass09b465f2017-07-29 11:35:17 -0600262static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
263 struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500264{
Andy Fleming50586ef2008-10-30 16:47:16 -0500265 int timeout;
Peng Fan96f04072016-03-25 14:16:56 +0800266 struct fsl_esdhc *regs = priv->esdhc_regs;
Peng Faneec2d432018-01-10 13:20:40 +0800267#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
Peng Fancd357ad2018-11-20 10:19:25 +0000268 defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700269 dma_addr_t addr;
270#endif
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200271 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500272
273 wml_value = data->blocksize/4;
274
275 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530276 if (wml_value > WML_RD_WML_MAX)
277 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500278
Roy Zangab467c52010-02-09 18:23:33 +0800279 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li71689772014-02-20 18:00:57 +0800280#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Peng Faneec2d432018-01-10 13:20:40 +0800281#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
Peng Fancd357ad2018-11-20 10:19:25 +0000282 defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700283 addr = virt_to_phys((void *)(data->dest));
284 if (upper_32_bits(addr))
285 printf("Error found for upper 32 bits\n");
286 else
287 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
288#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100289 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li71689772014-02-20 18:00:57 +0800290#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700291#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500292 } else {
Ye.Li71689772014-02-20 18:00:57 +0800293#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-04-25 14:28:48 +0000294 flush_dcache_range((ulong)data->src,
295 (ulong)data->src+data->blocks
296 *data->blocksize);
Ye.Li71689772014-02-20 18:00:57 +0800297#endif
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530298 if (wml_value > WML_WR_WML_MAX)
299 wml_value = WML_WR_WML_MAX_VAL;
Peng Fan14831512016-06-15 10:53:02 +0800300 if (priv->wp_enable) {
301 if ((esdhc_read32(&regs->prsstat) &
302 PRSSTAT_WPSPL) == 0) {
303 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900304 return -ETIMEDOUT;
Peng Fan14831512016-06-15 10:53:02 +0800305 }
Ye Lida8e1f32019-01-07 09:10:27 +0000306 } else {
307#ifdef CONFIG_DM_GPIO
308 if (dm_gpio_is_valid(&priv->wp_gpio) && dm_gpio_get_value(&priv->wp_gpio)) {
309 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
310 return -ETIMEDOUT;
311 }
312#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500313 }
Roy Zangab467c52010-02-09 18:23:33 +0800314
315 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
316 wml_value << 16);
Ye.Li71689772014-02-20 18:00:57 +0800317#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Peng Faneec2d432018-01-10 13:20:40 +0800318#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
Peng Fancd357ad2018-11-20 10:19:25 +0000319 defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700320 addr = virt_to_phys((void *)(data->src));
321 if (upper_32_bits(addr))
322 printf("Error found for upper 32 bits\n");
323 else
324 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
325#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100326 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li71689772014-02-20 18:00:57 +0800327#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700328#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500329 }
330
Stefano Babicc67bee12010-02-05 15:11:27 +0100331 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500332
333 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530334 /*
335 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
336 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
337 * So, Number of SD Clock cycles for 0.25sec should be minimum
338 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500339 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530340 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500341 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530342 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500343 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530344 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500345 * => timeout + 13 = log2(mmc->clock/4) + 1
346 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lue978a312015-12-30 14:19:30 +0800347 *
348 * However, the MMC spec "It is strongly recommended for hosts to
349 * implement more than 500ms timeout value even if the card
350 * indicates the 250ms maximum busy length." Even the previous
351 * value of 300ms is known to be insufficient for some cards.
352 * So, we use
353 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530354 */
Yangbo Lue978a312015-12-30 14:19:30 +0800355 timeout = fls(mmc->clock/2);
Andy Fleming50586ef2008-10-30 16:47:16 -0500356 timeout -= 13;
357
358 if (timeout > 14)
359 timeout = 14;
360
361 if (timeout < 0)
362 timeout = 0;
363
Kumar Gala5103a032011-01-29 15:36:10 -0600364#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
365 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
366 timeout++;
367#endif
368
Haijun.Zhang1336e2d2014-03-18 17:04:23 +0800369#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
370 timeout = 0xE;
371#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100372 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500373
374 return 0;
375}
376
Eric Nelsone576bd92012-04-25 14:28:48 +0000377static void check_and_invalidate_dcache_range
378 (struct mmc_cmd *cmd,
379 struct mmc_data *data) {
Yangbo Lu8b064602015-03-20 19:28:31 -0700380 unsigned start = 0;
Yangbo Lucc634e22016-05-12 19:12:58 +0800381 unsigned end = 0;
Eric Nelsone576bd92012-04-25 14:28:48 +0000382 unsigned size = roundup(ARCH_DMA_MINALIGN,
383 data->blocks*data->blocksize);
Peng Faneec2d432018-01-10 13:20:40 +0800384#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234) || \
Peng Fancd357ad2018-11-20 10:19:25 +0000385 defined(CONFIG_IMX8) || defined(CONFIG_IMX8M)
Yangbo Lu8b064602015-03-20 19:28:31 -0700386 dma_addr_t addr;
387
388 addr = virt_to_phys((void *)(data->dest));
389 if (upper_32_bits(addr))
390 printf("Error found for upper 32 bits\n");
391 else
392 start = lower_32_bits(addr);
Yangbo Lucc634e22016-05-12 19:12:58 +0800393#else
394 start = (unsigned)data->dest;
Yangbo Lu8b064602015-03-20 19:28:31 -0700395#endif
Yangbo Lucc634e22016-05-12 19:12:58 +0800396 end = start + size;
Eric Nelsone576bd92012-04-25 14:28:48 +0000397 invalidate_dcache_range(start, end);
398}
Tom Rini10dc7772014-05-23 09:19:05 -0400399
Angelo Dureghello1f15cb82019-01-19 10:40:38 +0100400#ifdef CONFIG_MCF5441x
401/*
402 * Swaps 32-bit words to little-endian byte order.
403 */
404static inline void sd_swap_dma_buff(struct mmc_data *data)
405{
406 int i, size = data->blocksize >> 2;
407 u32 *buffer = (u32 *)data->dest;
408 u32 sw;
409
410 while (data->blocks--) {
411 for (i = 0; i < size; i++) {
412 sw = __sw32(*buffer);
413 *buffer++ = sw;
414 }
415 }
416}
417#endif
418
Andy Fleming50586ef2008-10-30 16:47:16 -0500419/*
420 * Sends a command out on the bus. Takes the mmc pointer,
421 * a command pointer, and an optional data pointer.
422 */
Simon Glass9586aa62017-07-29 11:35:18 -0600423static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
424 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500425{
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500426 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500427 uint xfertyp;
428 uint irqstat;
Peng Fan51313b42018-01-21 19:00:24 +0800429 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fan96f04072016-03-25 14:16:56 +0800430 struct fsl_esdhc *regs = priv->esdhc_regs;
Fabio Estevam29c2edb2018-11-19 10:31:53 -0200431 unsigned long start;
Andy Fleming50586ef2008-10-30 16:47:16 -0500432
Jerry Huangd621da02011-01-06 23:42:19 -0600433#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
434 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
435 return 0;
436#endif
437
Stefano Babicc67bee12010-02-05 15:11:27 +0100438 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500439
440 sync();
441
442 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100443 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
444 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
445 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500446
Stefano Babicc67bee12010-02-05 15:11:27 +0100447 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
448 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500449
450 /* Wait at least 8 SD clock cycles before the next command */
451 /*
452 * Note: This is way more than 8 cycles, but 1ms seems to
453 * resolve timing issues with some cards
454 */
455 udelay(1000);
456
457 /* Set up for a data transfer if we have one */
458 if (data) {
Simon Glass09b465f2017-07-29 11:35:17 -0600459 err = esdhc_setup_data(priv, mmc, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500460 if(err)
461 return err;
Peng Fan4683b222015-06-25 10:32:26 +0800462
463 if (data->flags & MMC_DATA_READ)
464 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500465 }
466
467 /* Figure out the transfer arguments */
468 xfertyp = esdhc_xfertyp(cmd, data);
469
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500470 /* Mask all irqs */
471 esdhc_write32(&regs->irqsigen, 0);
472
Andy Fleming50586ef2008-10-30 16:47:16 -0500473 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100474 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu46927082011-11-25 00:18:04 +0000475#if defined(CONFIG_FSL_USDHC)
476 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500477 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
478 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu46927082011-11-25 00:18:04 +0000479 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
480#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100481 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu46927082011-11-25 00:18:04 +0000482#endif
Dirk Behme7a5b8022012-03-26 03:13:05 +0000483
Peng Fan51313b42018-01-21 19:00:24 +0800484 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
485 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
486 flags = IRQSTAT_BRR;
487
Andy Fleming50586ef2008-10-30 16:47:16 -0500488 /* Wait for the command to complete */
Fabio Estevam29c2edb2018-11-19 10:31:53 -0200489 start = get_timer(0);
490 while (!(esdhc_read32(&regs->irqstat) & flags)) {
491 if (get_timer(start) > 1000) {
492 err = -ETIMEDOUT;
493 goto out;
494 }
495 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500496
Stefano Babicc67bee12010-02-05 15:11:27 +0100497 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500498
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500499 if (irqstat & CMD_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900500 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500501 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000502 }
503
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500504 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900505 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500506 goto out;
507 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500508
Otavio Salvadorf022d362015-02-17 10:42:43 -0200509 /* Switch voltage to 1.8V if CMD11 succeeded */
510 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
511 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
512
513 printf("Run CMD11 1.8V switch\n");
514 /* Sleep for 5 ms - max time for card to switch to 1.8V */
515 udelay(5000);
516 }
517
Dirk Behme7a5b8022012-03-26 03:13:05 +0000518 /* Workaround for ESDHC errata ENGcm03648 */
519 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800520 int timeout = 6000;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000521
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800522 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000523 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
524 PRSSTAT_DAT0)) {
525 udelay(100);
526 timeout--;
527 }
528
529 if (timeout <= 0) {
530 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900531 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500532 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000533 }
534 }
535
Andy Fleming50586ef2008-10-30 16:47:16 -0500536 /* Copy the response to the response buffer */
537 if (cmd->resp_type & MMC_RSP_136) {
538 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
539
Stefano Babicc67bee12010-02-05 15:11:27 +0100540 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
541 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
542 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
543 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530544 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
545 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
546 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
547 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500548 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100549 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500550
551 /* Wait until all of the blocks are transferred */
552 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530553#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass09b465f2017-07-29 11:35:17 -0600554 esdhc_pio_read_write(priv, data);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530555#else
Peng Fan51313b42018-01-21 19:00:24 +0800556 flags = DATA_COMPLETE;
557 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
558 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
559 flags = IRQSTAT_BRR;
560 }
561
Andy Fleming50586ef2008-10-30 16:47:16 -0500562 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100563 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500564
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500565 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900566 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500567 goto out;
568 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000569
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500570 if (irqstat & DATA_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900571 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500572 goto out;
573 }
Peng Fan51313b42018-01-21 19:00:24 +0800574 } while ((irqstat & flags) != flags);
Ye.Li71689772014-02-20 18:00:57 +0800575
Peng Fan4683b222015-06-25 10:32:26 +0800576 /*
577 * Need invalidate the dcache here again to avoid any
578 * cache-fill during the DMA operations such as the
579 * speculative pre-fetching etc.
580 */
Angelo Dureghello1f15cb82019-01-19 10:40:38 +0100581 if (data->flags & MMC_DATA_READ) {
Eric Nelson54899fc2013-04-03 12:31:56 +0000582 check_and_invalidate_dcache_range(cmd, data);
Angelo Dureghello1f15cb82019-01-19 10:40:38 +0100583#ifdef CONFIG_MCF5441x
584 sd_swap_dma_buff(data);
585#endif
586 }
Ye.Li71689772014-02-20 18:00:57 +0800587#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500588 }
589
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500590out:
591 /* Reset CMD and DATA portions on error */
592 if (err) {
593 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
594 SYSCTL_RSTC);
595 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
596 ;
597
598 if (data) {
599 esdhc_write32(&regs->sysctl,
600 esdhc_read32(&regs->sysctl) |
601 SYSCTL_RSTD);
602 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
603 ;
604 }
Otavio Salvadorf022d362015-02-17 10:42:43 -0200605
606 /* If this was CMD11, then notify that power cycle is needed */
607 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
608 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500609 }
610
Stefano Babicc67bee12010-02-05 15:11:27 +0100611 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500612
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500613 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500614}
615
Simon Glass09b465f2017-07-29 11:35:17 -0600616static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500617{
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100618 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200619 int div = 1;
620#ifdef ARCH_MXC
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100621#ifdef CONFIG_MX53
622 /* For i.MX53 eSDHCv3, SYSCTL.SDCLKFS may not be set to 0. */
623 int pre_div = (regs == (struct fsl_esdhc *)MMC_SDHC3_BASE_ADDR) ? 2 : 1;
624#else
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200625 int pre_div = 1;
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100626#endif
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200627#else
628 int pre_div = 2;
629#endif
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200630 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
Peng Fan96f04072016-03-25 14:16:56 +0800631 int sdhc_clk = priv->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500632 uint clk;
633
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200634 if (clock < mmc->cfg->f_min)
635 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100636
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200637 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
638 pre_div *= 2;
Andy Fleming50586ef2008-10-30 16:47:16 -0500639
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200640 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
641 div++;
Andy Fleming50586ef2008-10-30 16:47:16 -0500642
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200643 pre_div >>= 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500644 div -= 1;
645
646 clk = (pre_div << 8) | (div << 4);
647
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700648#ifdef CONFIG_FSL_USDHC
Ye Li84ecdf62016-06-15 10:53:01 +0800649 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700650#else
Kumar Galacc4d1222010-03-18 15:51:05 -0500651 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700652#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100653
654 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500655
656 udelay(10000);
657
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700658#ifdef CONFIG_FSL_USDHC
Ye Li84ecdf62016-06-15 10:53:01 +0800659 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700660#else
661 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
662#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100663
Peng Fan51313b42018-01-21 19:00:24 +0800664 priv->clock = clock;
Andy Fleming50586ef2008-10-30 16:47:16 -0500665}
666
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800667#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
Simon Glass09b465f2017-07-29 11:35:17 -0600668static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800669{
Peng Fan96f04072016-03-25 14:16:56 +0800670 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800671 u32 value;
672 u32 time_out;
673
674 value = esdhc_read32(&regs->sysctl);
675
676 if (enable)
677 value |= SYSCTL_CKEN;
678 else
679 value &= ~SYSCTL_CKEN;
680
681 esdhc_write32(&regs->sysctl, value);
682
683 time_out = 20;
684 value = PRSSTAT_SDSTB;
685 while (!(esdhc_read32(&regs->prsstat) & value)) {
686 if (time_out == 0) {
687 printf("fsl_esdhc: Internal clock never stabilised.\n");
688 break;
689 }
690 time_out--;
691 mdelay(1);
692 }
693}
694#endif
695
Peng Fan51313b42018-01-21 19:00:24 +0800696#ifdef MMC_SUPPORTS_TUNING
697static int esdhc_change_pinstate(struct udevice *dev)
698{
699 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
700 int ret;
701
702 switch (priv->mode) {
703 case UHS_SDR50:
704 case UHS_DDR50:
705 ret = pinctrl_select_state(dev, "state_100mhz");
706 break;
707 case UHS_SDR104:
708 case MMC_HS_200:
Peng Fanc76382f2018-08-10 14:07:55 +0800709 case MMC_HS_400:
Peng Fan51313b42018-01-21 19:00:24 +0800710 ret = pinctrl_select_state(dev, "state_200mhz");
711 break;
712 default:
713 ret = pinctrl_select_state(dev, "default");
714 break;
715 }
716
717 if (ret)
718 printf("%s %d error\n", __func__, priv->mode);
719
720 return ret;
721}
722
723static void esdhc_reset_tuning(struct mmc *mmc)
724{
725 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
726 struct fsl_esdhc *regs = priv->esdhc_regs;
727
728 if (priv->flags & ESDHC_FLAG_USDHC) {
729 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
730 esdhc_clrbits32(&regs->autoc12err,
731 MIX_CTRL_SMPCLK_SEL |
732 MIX_CTRL_EXE_TUNE);
733 }
734 }
735}
736
Peng Fanc76382f2018-08-10 14:07:55 +0800737static void esdhc_set_strobe_dll(struct mmc *mmc)
738{
739 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
740 struct fsl_esdhc *regs = priv->esdhc_regs;
741 u32 val;
742
743 if (priv->clock > ESDHC_STROBE_DLL_CLK_FREQ) {
744 writel(ESDHC_STROBE_DLL_CTRL_RESET, &regs->strobe_dllctrl);
745
746 /*
747 * enable strobe dll ctrl and adjust the delay target
748 * for the uSDHC loopback read clock
749 */
750 val = ESDHC_STROBE_DLL_CTRL_ENABLE |
751 (priv->strobe_dll_delay_target <<
752 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_SHIFT);
753 writel(val, &regs->strobe_dllctrl);
754 /* wait 1us to make sure strobe dll status register stable */
755 mdelay(1);
756 val = readl(&regs->strobe_dllstat);
757 if (!(val & ESDHC_STROBE_DLL_STS_REF_LOCK))
758 pr_warn("HS400 strobe DLL status REF not lock!\n");
759 if (!(val & ESDHC_STROBE_DLL_STS_SLV_LOCK))
760 pr_warn("HS400 strobe DLL status SLV not lock!\n");
761 }
762}
763
Peng Fan51313b42018-01-21 19:00:24 +0800764static int esdhc_set_timing(struct mmc *mmc)
765{
766 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
767 struct fsl_esdhc *regs = priv->esdhc_regs;
768 u32 mixctrl;
769
770 mixctrl = readl(&regs->mixctrl);
771 mixctrl &= ~(MIX_CTRL_DDREN | MIX_CTRL_HS400_EN);
772
773 switch (mmc->selected_mode) {
774 case MMC_LEGACY:
775 case SD_LEGACY:
776 esdhc_reset_tuning(mmc);
Peng Fanc76382f2018-08-10 14:07:55 +0800777 writel(mixctrl, &regs->mixctrl);
778 break;
779 case MMC_HS_400:
780 mixctrl |= MIX_CTRL_DDREN | MIX_CTRL_HS400_EN;
781 writel(mixctrl, &regs->mixctrl);
782 esdhc_set_strobe_dll(mmc);
Peng Fan51313b42018-01-21 19:00:24 +0800783 break;
784 case MMC_HS:
785 case MMC_HS_52:
786 case MMC_HS_200:
787 case SD_HS:
788 case UHS_SDR12:
789 case UHS_SDR25:
790 case UHS_SDR50:
791 case UHS_SDR104:
792 writel(mixctrl, &regs->mixctrl);
793 break;
794 case UHS_DDR50:
795 case MMC_DDR_52:
796 mixctrl |= MIX_CTRL_DDREN;
797 writel(mixctrl, &regs->mixctrl);
798 break;
799 default:
800 printf("Not supported %d\n", mmc->selected_mode);
801 return -EINVAL;
802 }
803
804 priv->mode = mmc->selected_mode;
805
806 return esdhc_change_pinstate(mmc->dev);
807}
808
809static int esdhc_set_voltage(struct mmc *mmc)
810{
811 struct fsl_esdhc_priv *priv = dev_get_priv(mmc->dev);
812 struct fsl_esdhc *regs = priv->esdhc_regs;
813 int ret;
814
815 priv->signal_voltage = mmc->signal_voltage;
816 switch (mmc->signal_voltage) {
817 case MMC_SIGNAL_VOLTAGE_330:
818 if (priv->vs18_enable)
819 return -EIO;
Abel Vesad76706c2019-02-01 16:40:11 +0000820#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan51313b42018-01-21 19:00:24 +0800821 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
822 ret = regulator_set_value(priv->vqmmc_dev, 3300000);
823 if (ret) {
824 printf("Setting to 3.3V error");
825 return -EIO;
826 }
827 /* Wait for 5ms */
828 mdelay(5);
829 }
830#endif
831
832 esdhc_clrbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
833 if (!(esdhc_read32(&regs->vendorspec) &
834 ESDHC_VENDORSPEC_VSELECT))
835 return 0;
836
837 return -EAGAIN;
838 case MMC_SIGNAL_VOLTAGE_180:
Abel Vesad76706c2019-02-01 16:40:11 +0000839#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan51313b42018-01-21 19:00:24 +0800840 if (!IS_ERR_OR_NULL(priv->vqmmc_dev)) {
841 ret = regulator_set_value(priv->vqmmc_dev, 1800000);
842 if (ret) {
843 printf("Setting to 1.8V error");
844 return -EIO;
845 }
846 }
847#endif
848 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
849 if (esdhc_read32(&regs->vendorspec) & ESDHC_VENDORSPEC_VSELECT)
850 return 0;
851
852 return -EAGAIN;
853 case MMC_SIGNAL_VOLTAGE_120:
854 return -ENOTSUPP;
855 default:
856 return 0;
857 }
858}
859
860static void esdhc_stop_tuning(struct mmc *mmc)
861{
862 struct mmc_cmd cmd;
863
864 cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
865 cmd.cmdarg = 0;
866 cmd.resp_type = MMC_RSP_R1b;
867
868 dm_mmc_send_cmd(mmc->dev, &cmd, NULL);
869}
870
871static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
872{
873 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
874 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
875 struct fsl_esdhc *regs = priv->esdhc_regs;
876 struct mmc *mmc = &plat->mmc;
877 u32 irqstaten = readl(&regs->irqstaten);
878 u32 irqsigen = readl(&regs->irqsigen);
879 int i, ret = -ETIMEDOUT;
880 u32 val, mixctrl;
881
882 /* clock tuning is not needed for upto 52MHz */
883 if (mmc->clock <= 52000000)
884 return 0;
885
886 /* This is readw/writew SDHCI_HOST_CONTROL2 when tuning */
887 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
888 val = readl(&regs->autoc12err);
889 mixctrl = readl(&regs->mixctrl);
890 val &= ~MIX_CTRL_SMPCLK_SEL;
891 mixctrl &= ~(MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN);
892
893 val |= MIX_CTRL_EXE_TUNE;
894 mixctrl |= MIX_CTRL_FBCLK_SEL | MIX_CTRL_AUTO_TUNE_EN;
895
896 writel(val, &regs->autoc12err);
897 writel(mixctrl, &regs->mixctrl);
898 }
899
900 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE); */
901 mixctrl = readl(&regs->mixctrl);
902 mixctrl = MIX_CTRL_DTDSEL_READ | (mixctrl & ~MIX_CTRL_SDHCI_MASK);
903 writel(mixctrl, &regs->mixctrl);
904
905 writel(IRQSTATEN_BRR, &regs->irqstaten);
906 writel(IRQSTATEN_BRR, &regs->irqsigen);
907
908 /*
909 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
910 * of loops reaches 40 times.
911 */
912 for (i = 0; i < MAX_TUNING_LOOP; i++) {
913 u32 ctrl;
914
915 if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200) {
916 if (mmc->bus_width == 8)
917 writel(0x7080, &regs->blkattr);
918 else if (mmc->bus_width == 4)
919 writel(0x7040, &regs->blkattr);
920 } else {
921 writel(0x7040, &regs->blkattr);
922 }
923
924 /* sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE) */
925 val = readl(&regs->mixctrl);
926 val = MIX_CTRL_DTDSEL_READ | (val & ~MIX_CTRL_SDHCI_MASK);
927 writel(val, &regs->mixctrl);
928
929 /* We are using STD tuning, no need to check return value */
930 mmc_send_tuning(mmc, opcode, NULL);
931
932 ctrl = readl(&regs->autoc12err);
933 if ((!(ctrl & MIX_CTRL_EXE_TUNE)) &&
934 (ctrl & MIX_CTRL_SMPCLK_SEL)) {
935 /*
936 * need to wait some time, make sure sd/mmc fininsh
937 * send out tuning data, otherwise, the sd/mmc can't
938 * response to any command when the card still out
939 * put the tuning data.
940 */
941 mdelay(1);
942 ret = 0;
943 break;
944 }
945
946 /* Add 1ms delay for SD and eMMC */
947 mdelay(1);
948 }
949
950 writel(irqstaten, &regs->irqstaten);
951 writel(irqsigen, &regs->irqsigen);
952
953 esdhc_stop_tuning(mmc);
954
955 return ret;
956}
957#endif
958
Simon Glass9586aa62017-07-29 11:35:18 -0600959static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500960{
Peng Fan96f04072016-03-25 14:16:56 +0800961 struct fsl_esdhc *regs = priv->esdhc_regs;
Peng Fan51313b42018-01-21 19:00:24 +0800962 int ret __maybe_unused;
Andy Fleming50586ef2008-10-30 16:47:16 -0500963
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800964#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
965 /* Select to use peripheral clock */
Simon Glass09b465f2017-07-29 11:35:17 -0600966 esdhc_clock_control(priv, false);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800967 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
Simon Glass09b465f2017-07-29 11:35:17 -0600968 esdhc_clock_control(priv, true);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800969#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500970 /* Set the clock speed */
Peng Fan51313b42018-01-21 19:00:24 +0800971 if (priv->clock != mmc->clock)
972 set_sysctl(priv, mmc, mmc->clock);
973
974#ifdef MMC_SUPPORTS_TUNING
975 if (mmc->clk_disable) {
976#ifdef CONFIG_FSL_USDHC
977 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
978#else
979 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
980#endif
981 } else {
982#ifdef CONFIG_FSL_USDHC
983 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
984 VENDORSPEC_CKEN);
985#else
986 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
987#endif
988 }
989
990 if (priv->mode != mmc->selected_mode) {
991 ret = esdhc_set_timing(mmc);
992 if (ret) {
993 printf("esdhc_set_timing error %d\n", ret);
994 return ret;
995 }
996 }
997
998 if (priv->signal_voltage != mmc->signal_voltage) {
999 ret = esdhc_set_voltage(mmc);
1000 if (ret) {
1001 printf("esdhc_set_voltage error %d\n", ret);
1002 return ret;
1003 }
1004 }
1005#endif
Andy Fleming50586ef2008-10-30 16:47:16 -05001006
1007 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +01001008 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -05001009
1010 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +01001011 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -05001012 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +01001013 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
1014
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +09001015 return 0;
Andy Fleming50586ef2008-10-30 16:47:16 -05001016}
1017
Simon Glass9586aa62017-07-29 11:35:18 -06001018static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -05001019{
Peng Fan96f04072016-03-25 14:16:56 +08001020 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass201e8282017-07-29 11:35:20 -06001021 ulong start;
Andy Fleming50586ef2008-10-30 16:47:16 -05001022
Stefano Babicc67bee12010-02-05 15:11:27 +01001023 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +02001024 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +01001025
1026 /* Wait until the controller is available */
Simon Glass201e8282017-07-29 11:35:20 -06001027 start = get_timer(0);
1028 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1029 if (get_timer(start) > 1000)
1030 return -ETIMEDOUT;
1031 }
Stefano Babicc67bee12010-02-05 15:11:27 +01001032
Peng Fanf53225c2016-06-15 10:53:00 +08001033#if defined(CONFIG_FSL_USDHC)
1034 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
1035 esdhc_write32(&regs->mmcboot, 0x0);
1036 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
1037 esdhc_write32(&regs->mixctrl, 0x0);
1038 esdhc_write32(&regs->clktunectrlstatus, 0x0);
1039
1040 /* Put VEND_SPEC to default value */
Peng Fandb359ef2018-01-02 16:51:22 +08001041 if (priv->vs18_enable)
1042 esdhc_write32(&regs->vendorspec, (VENDORSPEC_INIT |
1043 ESDHC_VENDORSPEC_VSELECT));
1044 else
1045 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
Peng Fanf53225c2016-06-15 10:53:00 +08001046
1047 /* Disable DLL_CTRL delay line */
1048 esdhc_write32(&regs->dllctrl, 0x0);
1049#endif
1050
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +00001051#ifndef ARCH_MXC
P.V.Suresh2c1764e2010-12-04 10:37:23 +05301052 /* Enable cache snooping */
Benoît Thébaudeau16e43f32012-08-13 07:28:16 +00001053 esdhc_write32(&regs->scr, 0x00000040);
1054#endif
P.V.Suresh2c1764e2010-12-04 10:37:23 +05301055
Eric Nelsonf0b5f232015-12-04 12:32:48 -07001056#ifndef CONFIG_FSL_USDHC
Dirk Behmea61da722013-07-15 15:44:29 +02001057 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Ye Li84ecdf62016-06-15 10:53:01 +08001058#else
1059 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -07001060#endif
Andy Fleming50586ef2008-10-30 16:47:16 -05001061
1062 /* Set the initial clock speed */
Jaehoon Chung65117182018-01-26 19:25:29 +09001063 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
Andy Fleming50586ef2008-10-30 16:47:16 -05001064
1065 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +01001066 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -05001067
Angelo Dureghello1f15cb82019-01-19 10:40:38 +01001068#ifdef CONFIG_MCF5441x
1069 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1070#else
Andy Fleming50586ef2008-10-30 16:47:16 -05001071 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +01001072 esdhc_write32(&regs->proctl, PROCTL_INIT);
Angelo Dureghello1f15cb82019-01-19 10:40:38 +01001073#endif
Andy Fleming50586ef2008-10-30 16:47:16 -05001074
Stefano Babicc67bee12010-02-05 15:11:27 +01001075 /* Set timout to the maximum value */
1076 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -05001077
Thierry Redingd48d2e22012-01-02 01:15:38 +00001078 return 0;
1079}
Andy Fleming50586ef2008-10-30 16:47:16 -05001080
Simon Glass9586aa62017-07-29 11:35:18 -06001081static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Redingd48d2e22012-01-02 01:15:38 +00001082{
Peng Fan96f04072016-03-25 14:16:56 +08001083 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Redingd48d2e22012-01-02 01:15:38 +00001084 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +01001085
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +08001086#ifdef CONFIG_ESDHC_DETECT_QUIRK
1087 if (CONFIG_ESDHC_DETECT_QUIRK)
1088 return 1;
1089#endif
Peng Fan96f04072016-03-25 14:16:56 +08001090
Simon Glass653282b2017-07-29 11:35:24 -06001091#if CONFIG_IS_ENABLED(DM_MMC)
Peng Fan96f04072016-03-25 14:16:56 +08001092 if (priv->non_removable)
1093 return 1;
Yangbo Lufc8048a2016-12-07 11:54:30 +08001094#ifdef CONFIG_DM_GPIO
Peng Fan96f04072016-03-25 14:16:56 +08001095 if (dm_gpio_is_valid(&priv->cd_gpio))
1096 return dm_gpio_get_value(&priv->cd_gpio);
1097#endif
Yangbo Lufc8048a2016-12-07 11:54:30 +08001098#endif
Peng Fan96f04072016-03-25 14:16:56 +08001099
Thierry Redingd48d2e22012-01-02 01:15:38 +00001100 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
1101 udelay(1000);
1102
1103 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -05001104}
1105
Simon Glass446e0772017-07-29 11:35:19 -06001106static int esdhc_reset(struct fsl_esdhc *regs)
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001107{
Simon Glass446e0772017-07-29 11:35:19 -06001108 ulong start;
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001109
1110 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +02001111 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001112
1113 /* hardware clears the bit when it is done */
Simon Glass446e0772017-07-29 11:35:19 -06001114 start = get_timer(0);
1115 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
1116 if (get_timer(start) > 100) {
1117 printf("MMC/SD: Reset never completed.\n");
1118 return -ETIMEDOUT;
1119 }
1120 }
1121
1122 return 0;
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001123}
1124
Simon Glasse7881d82017-07-29 11:35:31 -06001125#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass9586aa62017-07-29 11:35:18 -06001126static int esdhc_getcd(struct mmc *mmc)
1127{
1128 struct fsl_esdhc_priv *priv = mmc->priv;
1129
1130 return esdhc_getcd_common(priv);
1131}
1132
1133static int esdhc_init(struct mmc *mmc)
1134{
1135 struct fsl_esdhc_priv *priv = mmc->priv;
1136
1137 return esdhc_init_common(priv, mmc);
1138}
1139
1140static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
1141 struct mmc_data *data)
1142{
1143 struct fsl_esdhc_priv *priv = mmc->priv;
1144
1145 return esdhc_send_cmd_common(priv, mmc, cmd, data);
1146}
1147
1148static int esdhc_set_ios(struct mmc *mmc)
1149{
1150 struct fsl_esdhc_priv *priv = mmc->priv;
1151
1152 return esdhc_set_ios_common(priv, mmc);
1153}
1154
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001155static const struct mmc_ops esdhc_ops = {
Simon Glass9586aa62017-07-29 11:35:18 -06001156 .getcd = esdhc_getcd,
1157 .init = esdhc_init,
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001158 .send_cmd = esdhc_send_cmd,
1159 .set_ios = esdhc_set_ios,
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001160};
Simon Glass653282b2017-07-29 11:35:24 -06001161#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +02001162
Simon Glasse88e1d92017-07-29 11:35:21 -06001163static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
1164 struct fsl_esdhc_plat *plat)
Andy Fleming50586ef2008-10-30 16:47:16 -05001165{
Simon Glasse88e1d92017-07-29 11:35:21 -06001166 struct mmc_config *cfg;
Stefano Babicc67bee12010-02-05 15:11:27 +01001167 struct fsl_esdhc *regs;
Li Yang030955c2010-11-25 17:06:09 +00001168 u32 caps, voltage_caps;
Simon Glass446e0772017-07-29 11:35:19 -06001169 int ret;
Andy Fleming50586ef2008-10-30 16:47:16 -05001170
Peng Fan96f04072016-03-25 14:16:56 +08001171 if (!priv)
1172 return -EINVAL;
Stefano Babicc67bee12010-02-05 15:11:27 +01001173
Peng Fan96f04072016-03-25 14:16:56 +08001174 regs = priv->esdhc_regs;
Stefano Babicc67bee12010-02-05 15:11:27 +01001175
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001176 /* First reset the eSDHC controller */
Simon Glass446e0772017-07-29 11:35:19 -06001177 ret = esdhc_reset(regs);
1178 if (ret)
1179 return ret;
Jerry Huang48bb3bb2010-03-18 15:57:06 -05001180
Angelo Dureghello1f15cb82019-01-19 10:40:38 +01001181#ifdef CONFIG_MCF5441x
1182 /* ColdFire, using SDHC_DATA[3] for card detection */
1183 esdhc_write32(&regs->proctl, PROCTL_INIT | PROCTL_D3CD);
1184#endif
1185
Eric Nelsonf0b5f232015-12-04 12:32:48 -07001186#ifndef CONFIG_FSL_USDHC
Jerry Huang975324a2012-05-17 23:57:02 +00001187 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
1188 | SYSCTL_IPGEN | SYSCTL_CKEN);
Peng Fan51313b42018-01-21 19:00:24 +08001189 /* Clearing tuning bits in case ROM has set it already */
1190 esdhc_write32(&regs->mixctrl, 0);
1191 esdhc_write32(&regs->autoc12err, 0);
1192 esdhc_write32(&regs->clktunectrlstatus, 0);
Ye Li84ecdf62016-06-15 10:53:01 +08001193#else
1194 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
1195 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Eric Nelsonf0b5f232015-12-04 12:32:48 -07001196#endif
Jerry Huang975324a2012-05-17 23:57:02 +00001197
Peng Fan32a91792017-06-12 17:50:53 +08001198 if (priv->vs18_enable)
1199 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
1200
Ye.Lia3d6e382014-11-04 15:35:49 +08001201 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Simon Glasse88e1d92017-07-29 11:35:21 -06001202 cfg = &plat->cfg;
Simon Glass653282b2017-07-29 11:35:24 -06001203#ifndef CONFIG_DM_MMC
Simon Glasse88e1d92017-07-29 11:35:21 -06001204 memset(cfg, '\0', sizeof(*cfg));
Simon Glass653282b2017-07-29 11:35:24 -06001205#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001206
Li Yang030955c2010-11-25 17:06:09 +00001207 voltage_caps = 0;
Wang Huan19060bd2014-09-05 13:52:40 +08001208 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -06001209
Angelo Dureghello1f15cb82019-01-19 10:40:38 +01001210#ifdef CONFIG_MCF5441x
1211 /*
1212 * MCF5441x RM declares in more points that sdhc clock speed must
1213 * never exceed 25 Mhz. From this, the HS bit needs to be disabled
1214 * from host capabilities.
1215 */
1216 caps &= ~ESDHC_HOSTCAPBLT_HSS;
1217#endif
1218
Roy Zang3b4456e2011-01-07 00:06:47 -06001219#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
1220 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
1221 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
1222#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +08001223
1224/* T4240 host controller capabilities register should have VS33 bit */
1225#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
1226 caps = caps | ESDHC_HOSTCAPBLT_VS33;
1227#endif
1228
Andy Fleming50586ef2008-10-30 16:47:16 -05001229 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +00001230 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -05001231 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +00001232 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -05001233 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +00001234 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
1235
Simon Glasse88e1d92017-07-29 11:35:21 -06001236 cfg->name = "FSL_SDHC";
Simon Glasse7881d82017-07-29 11:35:31 -06001237#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glasse88e1d92017-07-29 11:35:21 -06001238 cfg->ops = &esdhc_ops;
Simon Glass653282b2017-07-29 11:35:24 -06001239#endif
Li Yang030955c2010-11-25 17:06:09 +00001240#ifdef CONFIG_SYS_SD_VOLTAGE
Simon Glasse88e1d92017-07-29 11:35:21 -06001241 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +00001242#else
Simon Glasse88e1d92017-07-29 11:35:21 -06001243 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +00001244#endif
Simon Glasse88e1d92017-07-29 11:35:21 -06001245 if ((cfg->voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +00001246 printf("voltage not supported by controller\n");
1247 return -1;
1248 }
Andy Fleming50586ef2008-10-30 16:47:16 -05001249
Peng Fan96f04072016-03-25 14:16:56 +08001250 if (priv->bus_width == 8)
Simon Glasse88e1d92017-07-29 11:35:21 -06001251 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +08001252 else if (priv->bus_width == 4)
Simon Glasse88e1d92017-07-29 11:35:21 -06001253 cfg->host_caps = MMC_MODE_4BIT;
Peng Fan96f04072016-03-25 14:16:56 +08001254
Simon Glasse88e1d92017-07-29 11:35:21 -06001255 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -05001256#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Simon Glasse88e1d92017-07-29 11:35:21 -06001257 cfg->host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -05001258#endif
Andy Fleming50586ef2008-10-30 16:47:16 -05001259
Peng Fan96f04072016-03-25 14:16:56 +08001260 if (priv->bus_width > 0) {
1261 if (priv->bus_width < 8)
Simon Glasse88e1d92017-07-29 11:35:21 -06001262 cfg->host_caps &= ~MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +08001263 if (priv->bus_width < 4)
Simon Glasse88e1d92017-07-29 11:35:21 -06001264 cfg->host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +00001265 }
1266
Andy Fleming50586ef2008-10-30 16:47:16 -05001267 if (caps & ESDHC_HOSTCAPBLT_HSS)
Simon Glasse88e1d92017-07-29 11:35:21 -06001268 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -05001269
Haijun.Zhangd47e3d22014-01-10 13:52:18 +08001270#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
1271 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Simon Glasse88e1d92017-07-29 11:35:21 -06001272 cfg->host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +08001273#endif
1274
Peng Fan51313b42018-01-21 19:00:24 +08001275 cfg->host_caps |= priv->caps;
1276
Simon Glasse88e1d92017-07-29 11:35:21 -06001277 cfg->f_min = 400000;
Peng Fan51313b42018-01-21 19:00:24 +08001278 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Andy Fleming50586ef2008-10-30 16:47:16 -05001279
Simon Glasse88e1d92017-07-29 11:35:21 -06001280 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +02001281
Peng Fan51313b42018-01-21 19:00:24 +08001282 writel(0, &regs->dllctrl);
1283 if (priv->flags & ESDHC_FLAG_USDHC) {
1284 if (priv->flags & ESDHC_FLAG_STD_TUNING) {
1285 u32 val = readl(&regs->tuning_ctrl);
1286
1287 val |= ESDHC_STD_TUNING_EN;
1288 val &= ~ESDHC_TUNING_START_TAP_MASK;
1289 val |= priv->tuning_start_tap;
1290 val &= ~ESDHC_TUNING_STEP_MASK;
1291 val |= (priv->tuning_step) << ESDHC_TUNING_STEP_SHIFT;
1292 writel(val, &regs->tuning_ctrl);
1293 }
1294 }
1295
Peng Fan96f04072016-03-25 14:16:56 +08001296 return 0;
1297}
1298
Simon Glass52489302017-07-29 11:35:28 -06001299#if !CONFIG_IS_ENABLED(DM_MMC)
Jagan Teki2e87c442017-05-12 17:18:20 +05301300static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
1301 struct fsl_esdhc_priv *priv)
1302{
1303 if (!cfg || !priv)
1304 return -EINVAL;
1305
1306 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
1307 priv->bus_width = cfg->max_bus_width;
1308 priv->sdhc_clk = cfg->sdhc_clk;
1309 priv->wp_enable = cfg->wp_enable;
Peng Fan32a91792017-06-12 17:50:53 +08001310 priv->vs18_enable = cfg->vs18_enable;
Jagan Teki2e87c442017-05-12 17:18:20 +05301311
1312 return 0;
1313};
1314
Peng Fan96f04072016-03-25 14:16:56 +08001315int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
1316{
Simon Glasse88e1d92017-07-29 11:35:21 -06001317 struct fsl_esdhc_plat *plat;
Peng Fan96f04072016-03-25 14:16:56 +08001318 struct fsl_esdhc_priv *priv;
Simon Glassd6eb25e2017-07-29 11:35:22 -06001319 struct mmc *mmc;
Peng Fan96f04072016-03-25 14:16:56 +08001320 int ret;
1321
1322 if (!cfg)
1323 return -EINVAL;
1324
1325 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
1326 if (!priv)
1327 return -ENOMEM;
Simon Glasse88e1d92017-07-29 11:35:21 -06001328 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
1329 if (!plat) {
1330 free(priv);
1331 return -ENOMEM;
1332 }
Peng Fan96f04072016-03-25 14:16:56 +08001333
1334 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
1335 if (ret) {
1336 debug("%s xlate failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -06001337 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +08001338 free(priv);
1339 return ret;
1340 }
1341
Simon Glasse88e1d92017-07-29 11:35:21 -06001342 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +08001343 if (ret) {
1344 debug("%s init failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -06001345 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +08001346 free(priv);
1347 return ret;
1348 }
1349
Simon Glassd6eb25e2017-07-29 11:35:22 -06001350 mmc = mmc_create(&plat->cfg, priv);
1351 if (!mmc)
1352 return -EIO;
1353
1354 priv->mmc = mmc;
1355
Andy Fleming50586ef2008-10-30 16:47:16 -05001356 return 0;
1357}
1358
1359int fsl_esdhc_mmc_init(bd_t *bis)
1360{
Stefano Babicc67bee12010-02-05 15:11:27 +01001361 struct fsl_esdhc_cfg *cfg;
1362
Fabio Estevam88227a12012-12-27 08:51:08 +00001363 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +01001364 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +00001365 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +01001366 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -05001367}
Jagan Teki2e87c442017-05-12 17:18:20 +05301368#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001369
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001370#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1371void mmc_adapter_card_type_ident(void)
1372{
1373 u8 card_id;
1374 u8 value;
1375
1376 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
1377 gd->arch.sdhc_adapter = card_id;
1378
1379 switch (card_id) {
1380 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lucdc69552015-09-17 10:27:12 +08001381 value = QIXIS_READ(brdcfg[5]);
1382 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
1383 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001384 break;
1385 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Lubf50be82015-09-17 10:27:48 +08001386 value = QIXIS_READ(pwr_ctl[1]);
1387 value |= QIXIS_EVDD_BY_SDHC_VS;
1388 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001389 break;
1390 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
1391 value = QIXIS_READ(brdcfg[5]);
1392 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
1393 QIXIS_WRITE(brdcfg[5], value);
1394 break;
1395 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
1396 break;
1397 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
1398 break;
1399 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
1400 break;
1401 case QIXIS_ESDHC_NO_ADAPTER:
1402 break;
1403 default:
1404 break;
1405 }
1406}
1407#endif
1408
Stefano Babicc67bee12010-02-05 15:11:27 +01001409#ifdef CONFIG_OF_LIBFDT
Yangbo Lufce1e162017-01-17 10:43:54 +08001410__weak int esdhc_status_fixup(void *blob, const char *compat)
1411{
1412#ifdef CONFIG_FSL_ESDHC_PIN_MUX
1413 if (!hwconfig("esdhc")) {
1414 do_fixup_by_compat(blob, compat, "status", "disabled",
1415 sizeof("disabled"), 1);
1416 return 1;
1417 }
1418#endif
Yangbo Lufce1e162017-01-17 10:43:54 +08001419 return 0;
1420}
1421
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001422void fdt_fixup_esdhc(void *blob, bd_t *bd)
1423{
1424 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001425
Yangbo Lufce1e162017-01-17 10:43:54 +08001426 if (esdhc_status_fixup(blob, compat))
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +08001427 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001428
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +08001429#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
1430 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
1431 gd->arch.sdhc_clk, 1);
1432#else
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001433 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +00001434 gd->arch.sdhc_clk, 1);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +08001435#endif
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +08001436#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
1437 do_fixup_by_compat_u32(blob, compat, "adapter-type",
1438 (u32)(gd->arch.sdhc_adapter), 1);
1439#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +04001440}
Stefano Babicc67bee12010-02-05 15:11:27 +01001441#endif
Peng Fan96f04072016-03-25 14:16:56 +08001442
Simon Glass653282b2017-07-29 11:35:24 -06001443#if CONFIG_IS_ENABLED(DM_MMC)
Yinbo Zhub512d072019-04-11 11:01:46 +00001444#ifndef CONFIG_PPC
Peng Fan96f04072016-03-25 14:16:56 +08001445#include <asm/arch/clock.h>
Yinbo Zhub512d072019-04-11 11:01:46 +00001446#endif
Peng Fanb60f1452017-02-22 16:21:55 +08001447__weak void init_clk_usdhc(u32 index)
1448{
1449}
1450
Peng Fan96f04072016-03-25 14:16:56 +08001451static int fsl_esdhc_probe(struct udevice *dev)
1452{
1453 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glasse88e1d92017-07-29 11:35:21 -06001454 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fan96f04072016-03-25 14:16:56 +08001455 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Peng Fan51313b42018-01-21 19:00:24 +08001456 const void *fdt = gd->fdt_blob;
1457 int node = dev_of_offset(dev);
1458 struct esdhc_soc_data *data =
1459 (struct esdhc_soc_data *)dev_get_driver_data(dev);
Abel Vesad76706c2019-02-01 16:40:11 +00001460#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan4483b7e2017-06-12 17:50:54 +08001461 struct udevice *vqmmc_dev;
York Sun9bb272e2017-08-08 15:45:13 -07001462#endif
Peng Fan96f04072016-03-25 14:16:56 +08001463 fdt_addr_t addr;
1464 unsigned int val;
Simon Glass653282b2017-07-29 11:35:24 -06001465 struct mmc *mmc;
Yangbo Lu66fa0352019-05-23 11:05:46 +08001466#if !CONFIG_IS_ENABLED(BLK)
1467 struct blk_desc *bdesc;
1468#endif
Peng Fan96f04072016-03-25 14:16:56 +08001469 int ret;
1470
Simon Glass4aac33f2017-07-29 11:35:23 -06001471 addr = dev_read_addr(dev);
Peng Fan96f04072016-03-25 14:16:56 +08001472 if (addr == FDT_ADDR_T_NONE)
1473 return -EINVAL;
Yinbo Zhub69e1d02019-04-11 11:01:50 +00001474#ifdef CONFIG_PPC
1475 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
1476#else
Peng Fan96f04072016-03-25 14:16:56 +08001477 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yinbo Zhub69e1d02019-04-11 11:01:50 +00001478#endif
Peng Fan96f04072016-03-25 14:16:56 +08001479 priv->dev = dev;
Peng Fan51313b42018-01-21 19:00:24 +08001480 priv->mode = -1;
1481 if (data) {
1482 priv->flags = data->flags;
1483 priv->caps = data->caps;
1484 }
Peng Fan96f04072016-03-25 14:16:56 +08001485
Simon Glass4aac33f2017-07-29 11:35:23 -06001486 val = dev_read_u32_default(dev, "bus-width", -1);
Peng Fan96f04072016-03-25 14:16:56 +08001487 if (val == 8)
1488 priv->bus_width = 8;
1489 else if (val == 4)
1490 priv->bus_width = 4;
1491 else
1492 priv->bus_width = 1;
1493
Peng Fan51313b42018-01-21 19:00:24 +08001494 val = fdtdec_get_int(fdt, node, "fsl,tuning-step", 1);
1495 priv->tuning_step = val;
1496 val = fdtdec_get_int(fdt, node, "fsl,tuning-start-tap",
1497 ESDHC_TUNING_START_TAP_DEFAULT);
1498 priv->tuning_start_tap = val;
1499 val = fdtdec_get_int(fdt, node, "fsl,strobe-dll-delay-target",
1500 ESDHC_STROBE_DLL_CTRL_SLV_DLY_TARGET_DEFAULT);
1501 priv->strobe_dll_delay_target = val;
1502
Simon Glass4aac33f2017-07-29 11:35:23 -06001503 if (dev_read_bool(dev, "non-removable")) {
Peng Fan96f04072016-03-25 14:16:56 +08001504 priv->non_removable = 1;
1505 } else {
1506 priv->non_removable = 0;
Yangbo Lufc8048a2016-12-07 11:54:30 +08001507#ifdef CONFIG_DM_GPIO
Simon Glass4aac33f2017-07-29 11:35:23 -06001508 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
1509 GPIOD_IS_IN);
Yangbo Lufc8048a2016-12-07 11:54:30 +08001510#endif
Peng Fan96f04072016-03-25 14:16:56 +08001511 }
1512
Ye Lida8e1f32019-01-07 09:10:27 +00001513 if (dev_read_prop(dev, "fsl,wp-controller", NULL)) {
1514 priv->wp_enable = 1;
1515 } else {
Peng Fan14831512016-06-15 10:53:02 +08001516 priv->wp_enable = 0;
Ye Lida8e1f32019-01-07 09:10:27 +00001517#ifdef CONFIG_DM_GPIO
1518 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
1519 GPIOD_IS_IN);
Yangbo Lufc8048a2016-12-07 11:54:30 +08001520#endif
Ye Lida8e1f32019-01-07 09:10:27 +00001521 }
Peng Fan4483b7e2017-06-12 17:50:54 +08001522
1523 priv->vs18_enable = 0;
1524
Abel Vesad76706c2019-02-01 16:40:11 +00001525#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan4483b7e2017-06-12 17:50:54 +08001526 /*
1527 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1528 * otherwise, emmc will work abnormally.
1529 */
1530 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1531 if (ret) {
1532 dev_dbg(dev, "no vqmmc-supply\n");
1533 } else {
1534 ret = regulator_set_enable(vqmmc_dev, true);
1535 if (ret) {
1536 dev_err(dev, "fail to enable vqmmc-supply\n");
1537 return ret;
1538 }
1539
1540 if (regulator_get_value(vqmmc_dev) == 1800000)
1541 priv->vs18_enable = 1;
1542 }
1543#endif
1544
Peng Fan51313b42018-01-21 19:00:24 +08001545 if (fdt_get_property(fdt, node, "no-1-8-v", NULL))
Peng Fanc76382f2018-08-10 14:07:55 +08001546 priv->caps &= ~(UHS_CAPS | MMC_MODE_HS200 | MMC_MODE_HS400);
Peng Fan51313b42018-01-21 19:00:24 +08001547
Peng Fan96f04072016-03-25 14:16:56 +08001548 /*
1549 * TODO:
1550 * Because lack of clk driver, if SDHC clk is not enabled,
1551 * need to enable it first before this driver is invoked.
1552 *
1553 * we use MXC_ESDHC_CLK to get clk freq.
1554 * If one would like to make this function work,
1555 * the aliases should be provided in dts as this:
1556 *
1557 * aliases {
1558 * mmc0 = &usdhc1;
1559 * mmc1 = &usdhc2;
1560 * mmc2 = &usdhc3;
1561 * mmc3 = &usdhc4;
1562 * };
1563 * Then if your board only supports mmc2 and mmc3, but we can
1564 * correctly get the seq as 2 and 3, then let mxc_get_clock
1565 * work as expected.
1566 */
Peng Fanb60f1452017-02-22 16:21:55 +08001567
1568 init_clk_usdhc(dev->seq);
1569
Peng Fan3cb14502018-10-18 14:28:35 +02001570 if (IS_ENABLED(CONFIG_CLK)) {
1571 /* Assigned clock already set clock */
1572 ret = clk_get_by_name(dev, "per", &priv->per_clk);
1573 if (ret) {
1574 printf("Failed to get per_clk\n");
1575 return ret;
1576 }
1577 ret = clk_enable(&priv->per_clk);
1578 if (ret) {
1579 printf("Failed to enable per_clk\n");
1580 return ret;
1581 }
1582
1583 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1584 } else {
Yinbo Zhub512d072019-04-11 11:01:46 +00001585#ifndef CONFIG_PPC
Peng Fan3cb14502018-10-18 14:28:35 +02001586 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
Yinbo Zhub512d072019-04-11 11:01:46 +00001587#else
1588 priv->sdhc_clk = gd->arch.sdhc_clk;
1589#endif
Peng Fan3cb14502018-10-18 14:28:35 +02001590 if (priv->sdhc_clk <= 0) {
1591 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1592 return -EINVAL;
1593 }
Peng Fan96f04072016-03-25 14:16:56 +08001594 }
1595
Simon Glasse88e1d92017-07-29 11:35:21 -06001596 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +08001597 if (ret) {
1598 dev_err(dev, "fsl_esdhc_init failure\n");
1599 return ret;
1600 }
1601
Simon Glass653282b2017-07-29 11:35:24 -06001602 mmc = &plat->mmc;
1603 mmc->cfg = &plat->cfg;
1604 mmc->dev = dev;
Yangbo Lu66fa0352019-05-23 11:05:46 +08001605#if !CONFIG_IS_ENABLED(BLK)
1606 mmc->priv = priv;
1607
1608 /* Setup dsr related values */
1609 mmc->dsr_imp = 0;
1610 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1611 /* Setup the universal parts of the block interface just once */
1612 bdesc = mmc_get_blk_desc(mmc);
1613 bdesc->if_type = IF_TYPE_MMC;
1614 bdesc->removable = 1;
1615 bdesc->devnum = mmc_get_next_devnum();
1616 bdesc->block_read = mmc_bread;
1617 bdesc->block_write = mmc_bwrite;
1618 bdesc->block_erase = mmc_berase;
1619
1620 /* setup initial part type */
1621 bdesc->part_type = mmc->cfg->part_type;
1622 mmc_list_add(mmc);
1623#endif
1624
Simon Glass653282b2017-07-29 11:35:24 -06001625 upriv->mmc = mmc;
Peng Fan96f04072016-03-25 14:16:56 +08001626
Simon Glass653282b2017-07-29 11:35:24 -06001627 return esdhc_init_common(priv, mmc);
Peng Fan96f04072016-03-25 14:16:56 +08001628}
1629
Simon Glasse7881d82017-07-29 11:35:31 -06001630#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass653282b2017-07-29 11:35:24 -06001631static int fsl_esdhc_get_cd(struct udevice *dev)
1632{
1633 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1634
Simon Glass653282b2017-07-29 11:35:24 -06001635 return esdhc_getcd_common(priv);
1636}
1637
1638static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1639 struct mmc_data *data)
1640{
1641 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1642 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1643
1644 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1645}
1646
1647static int fsl_esdhc_set_ios(struct udevice *dev)
1648{
1649 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1650 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1651
1652 return esdhc_set_ios_common(priv, &plat->mmc);
1653}
1654
1655static const struct dm_mmc_ops fsl_esdhc_ops = {
1656 .get_cd = fsl_esdhc_get_cd,
1657 .send_cmd = fsl_esdhc_send_cmd,
1658 .set_ios = fsl_esdhc_set_ios,
Peng Fan51313b42018-01-21 19:00:24 +08001659#ifdef MMC_SUPPORTS_TUNING
1660 .execute_tuning = fsl_esdhc_execute_tuning,
1661#endif
Simon Glass653282b2017-07-29 11:35:24 -06001662};
1663#endif
1664
Peng Fan51313b42018-01-21 19:00:24 +08001665static struct esdhc_soc_data usdhc_imx7d_data = {
1666 .flags = ESDHC_FLAG_USDHC | ESDHC_FLAG_STD_TUNING
1667 | ESDHC_FLAG_HAVE_CAP1 | ESDHC_FLAG_HS200
1668 | ESDHC_FLAG_HS400,
1669 .caps = UHS_CAPS | MMC_MODE_HS200 | MMC_MODE_DDR_52MHz |
1670 MMC_MODE_HS_52MHz | MMC_MODE_HS,
1671};
1672
Peng Fan96f04072016-03-25 14:16:56 +08001673static const struct udevice_id fsl_esdhc_ids[] = {
Patrick Bruenn791c88d2019-01-03 07:54:32 +01001674 { .compatible = "fsl,imx53-esdhc", },
Peng Fan96f04072016-03-25 14:16:56 +08001675 { .compatible = "fsl,imx6ul-usdhc", },
1676 { .compatible = "fsl,imx6sx-usdhc", },
1677 { .compatible = "fsl,imx6sl-usdhc", },
1678 { .compatible = "fsl,imx6q-usdhc", },
Peng Fan51313b42018-01-21 19:00:24 +08001679 { .compatible = "fsl,imx7d-usdhc", .data = (ulong)&usdhc_imx7d_data,},
Peng Fanb60f1452017-02-22 16:21:55 +08001680 { .compatible = "fsl,imx7ulp-usdhc", },
Yangbo Lua6473f82016-12-07 11:54:31 +08001681 { .compatible = "fsl,esdhc", },
Peng Fan96f04072016-03-25 14:16:56 +08001682 { /* sentinel */ }
1683};
1684
Simon Glass653282b2017-07-29 11:35:24 -06001685#if CONFIG_IS_ENABLED(BLK)
1686static int fsl_esdhc_bind(struct udevice *dev)
1687{
1688 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1689
1690 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1691}
1692#endif
1693
Peng Fan96f04072016-03-25 14:16:56 +08001694U_BOOT_DRIVER(fsl_esdhc) = {
1695 .name = "fsl-esdhc-mmc",
1696 .id = UCLASS_MMC,
1697 .of_match = fsl_esdhc_ids,
Simon Glass653282b2017-07-29 11:35:24 -06001698 .ops = &fsl_esdhc_ops,
Simon Glass653282b2017-07-29 11:35:24 -06001699#if CONFIG_IS_ENABLED(BLK)
1700 .bind = fsl_esdhc_bind,
1701#endif
Peng Fan96f04072016-03-25 14:16:56 +08001702 .probe = fsl_esdhc_probe,
Simon Glasse88e1d92017-07-29 11:35:21 -06001703 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fan96f04072016-03-25 14:16:56 +08001704 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1705};
1706#endif