blob: 4e04c101dc9202f9b4df9782e94572c026c5d30d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleminge52ffb82008-10-30 16:47:16 -05002/*
Jerry Huanged413672011-01-06 23:42:19 -06003 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Yangbo Lu8abc0432020-05-19 11:06:43 +08004 * Copyright 2019-2020 NXP
Andy Fleminge52ffb82008-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 Fleminge52ffb82008-10-30 16:47:16 -050010 */
11
12#include <config.h>
13#include <common.h>
14#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -070015#include <cpu_func.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090016#include <errno.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040017#include <hwconfig.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050018#include <mmc.h>
19#include <part.h>
20#include <malloc.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050021#include <fsl_esdhc.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040022#include <fdt_support.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <asm/cache.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050024#include <asm/io.h>
Peng Fana4d36f72016-03-25 14:16:56 +080025#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070026#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060027#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060028#include <linux/delay.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050029
Andy Fleminge52ffb82008-10-30 16:47:16 -050030DECLARE_GLOBAL_DATA_PTR;
31
32struct fsl_esdhc {
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080033 uint dsaddr; /* SDMA system address register */
34 uint blkattr; /* Block attributes register */
35 uint cmdarg; /* Command argument register */
36 uint xfertyp; /* Transfer type register */
37 uint cmdrsp0; /* Command response 0 register */
38 uint cmdrsp1; /* Command response 1 register */
39 uint cmdrsp2; /* Command response 2 register */
40 uint cmdrsp3; /* Command response 3 register */
41 uint datport; /* Buffer data port register */
42 uint prsstat; /* Present state register */
43 uint proctl; /* Protocol control register */
44 uint sysctl; /* System Control Register */
45 uint irqstat; /* Interrupt status register */
46 uint irqstaten; /* Interrupt status enable register */
47 uint irqsigen; /* Interrupt signal enable register */
48 uint autoc12err; /* Auto CMD error status register */
49 uint hostcapblt; /* Host controller capabilities register */
50 uint wml; /* Watermark level register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080051 char reserved1[8]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080052 uint fevt; /* Force event register */
53 uint admaes; /* ADMA error status register */
54 uint adsaddr; /* ADMA system address register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080055 char reserved2[160];
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080056 uint hostver; /* Host controller version register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080057 char reserved3[4]; /* reserved */
Peng Fanb9b42362018-01-21 19:00:22 +080058 uint dmaerraddr; /* DMA error address register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080059 char reserved4[4]; /* reserved */
Peng Fanb9b42362018-01-21 19:00:22 +080060 uint dmaerrattr; /* DMA error attribute register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080061 char reserved5[4]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080062 uint hostcapblt2; /* Host controller capabilities register 2 */
Yangbo Lu73da9c82020-09-01 16:58:01 +080063 char reserved6[8]; /* reserved */
64 uint tbctl; /* Tuning block control register */
65 char reserved7[744]; /* reserved */
Yangbo Lu62b56b32019-06-21 11:42:29 +080066 uint esdhcctl; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050067};
68
Simon Glassfa02ca52017-07-29 11:35:21 -060069struct fsl_esdhc_plat {
70 struct mmc_config cfg;
71 struct mmc mmc;
72};
73
Peng Fana4d36f72016-03-25 14:16:56 +080074/**
75 * struct fsl_esdhc_priv
76 *
77 * @esdhc_regs: registers of the sdhc controller
78 * @sdhc_clk: Current clk of the sdhc controller
79 * @bus_width: bus width, 1bit, 4bit or 8bit
80 * @cfg: mmc config
81 * @mmc: mmc
82 * Following is used when Driver Model is enabled for MMC
83 * @dev: pointer for the device
Peng Fana4d36f72016-03-25 14:16:56 +080084 * @cd_gpio: gpio for card detection
Peng Fan01eb1c42016-06-15 10:53:02 +080085 * @wp_gpio: gpio for write protection
Peng Fana4d36f72016-03-25 14:16:56 +080086 */
87struct fsl_esdhc_priv {
88 struct fsl_esdhc *esdhc_regs;
89 unsigned int sdhc_clk;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +080090 bool is_sdhc_per_clk;
Peng Fanc4142702018-01-21 19:00:24 +080091 unsigned int clock;
Yangbo Lu77f26322019-10-21 18:09:07 +080092#if !CONFIG_IS_ENABLED(DM_MMC)
Peng Fana4d36f72016-03-25 14:16:56 +080093 struct mmc *mmc;
Simon Glass407025d2017-07-29 11:35:24 -060094#endif
Peng Fana4d36f72016-03-25 14:16:56 +080095 struct udevice *dev;
Peng Fana4d36f72016-03-25 14:16:56 +080096};
97
Andy Fleminge52ffb82008-10-30 16:47:16 -050098/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +000099static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500100{
101 uint xfertyp = 0;
102
103 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530104 xfertyp |= XFERTYP_DPSEL;
105#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu73da9c82020-09-01 16:58:01 +0800106 if (cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK &&
107 cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200)
108 xfertyp |= XFERTYP_DMAEN;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530109#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500110 if (data->blocks > 1) {
111 xfertyp |= XFERTYP_MSBSEL;
112 xfertyp |= XFERTYP_BCEN;
Jerry Huanged413672011-01-06 23:42:19 -0600113#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
114 xfertyp |= XFERTYP_AC12EN;
115#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500116 }
117
118 if (data->flags & MMC_DATA_READ)
119 xfertyp |= XFERTYP_DTDSEL;
120 }
121
122 if (cmd->resp_type & MMC_RSP_CRC)
123 xfertyp |= XFERTYP_CCCEN;
124 if (cmd->resp_type & MMC_RSP_OPCODE)
125 xfertyp |= XFERTYP_CICEN;
126 if (cmd->resp_type & MMC_RSP_136)
127 xfertyp |= XFERTYP_RSPTYP_136;
128 else if (cmd->resp_type & MMC_RSP_BUSY)
129 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
130 else if (cmd->resp_type & MMC_RSP_PRESENT)
131 xfertyp |= XFERTYP_RSPTYP_48;
132
Jason Liubef0ff02011-03-22 01:32:31 +0000133 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
134 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lub73a3d62016-01-21 17:33:19 +0800135
Andy Fleminge52ffb82008-10-30 16:47:16 -0500136 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
137}
138
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530139#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
140/*
141 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
142 */
Simon Glass1d177d42017-07-29 11:35:17 -0600143static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
144 struct mmc_data *data)
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530145{
Peng Fana4d36f72016-03-25 14:16:56 +0800146 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530147 uint blocks;
148 char *buffer;
149 uint databuf;
150 uint size;
151 uint irqstat;
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100152 ulong start;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530153
154 if (data->flags & MMC_DATA_READ) {
155 blocks = data->blocks;
156 buffer = data->dest;
157 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100158 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530159 size = data->blocksize;
160 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100161 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
162 if (get_timer(start) > PIO_TIMEOUT) {
163 printf("\nData Read Failed in PIO Mode.");
164 return;
165 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530166 }
167 while (size && (!(irqstat & IRQSTAT_TC))) {
168 udelay(100); /* Wait before last byte transfer complete */
169 irqstat = esdhc_read32(&regs->irqstat);
170 databuf = in_le32(&regs->datport);
171 *((uint *)buffer) = databuf;
172 buffer += 4;
173 size -= 4;
174 }
175 blocks--;
176 }
177 } else {
178 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200179 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530180 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100181 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530182 size = data->blocksize;
183 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100184 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
185 if (get_timer(start) > PIO_TIMEOUT) {
186 printf("\nData Write Failed in PIO Mode.");
187 return;
188 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530189 }
190 while (size && (!(irqstat & IRQSTAT_TC))) {
191 udelay(100); /* Wait before last byte transfer complete */
192 databuf = *((uint *)buffer);
193 buffer += 4;
194 size -= 4;
195 irqstat = esdhc_read32(&regs->irqstat);
196 out_le32(&regs->datport, databuf);
197 }
198 blocks--;
199 }
200 }
201}
202#endif
203
Simon Glass1d177d42017-07-29 11:35:17 -0600204static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
205 struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500206{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500207 int timeout;
Peng Fana4d36f72016-03-25 14:16:56 +0800208 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu62b56b32019-06-21 11:42:29 +0800209#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700210 dma_addr_t addr;
211#endif
Wolfgang Denka40545c2010-05-09 23:52:59 +0200212 uint wml_value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500213
214 wml_value = data->blocksize/4;
215
216 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530217 if (wml_value > WML_RD_WML_MAX)
218 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500219
Roy Zange5853af2010-02-09 18:23:33 +0800220 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li33a56b12014-02-20 18:00:57 +0800221#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu62b56b32019-06-21 11:42:29 +0800222#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700223 addr = virt_to_phys((void *)(data->dest));
224 if (upper_32_bits(addr))
225 printf("Error found for upper 32 bits\n");
226 else
227 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
228#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100229 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li33a56b12014-02-20 18:00:57 +0800230#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700231#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500232 } else {
Ye.Li33a56b12014-02-20 18:00:57 +0800233#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000234 flush_dcache_range((ulong)data->src,
235 (ulong)data->src+data->blocks
236 *data->blocksize);
Ye.Li33a56b12014-02-20 18:00:57 +0800237#endif
Priyanka Jain02449632011-02-09 09:24:10 +0530238 if (wml_value > WML_WR_WML_MAX)
239 wml_value = WML_WR_WML_MAX_VAL;
Yangbo Luf3bcc832019-10-31 18:54:25 +0800240
241 if (!(esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL)) {
242 printf("Can not write to locked SD card.\n");
243 return -EINVAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500244 }
Roy Zange5853af2010-02-09 18:23:33 +0800245
246 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
247 wml_value << 16);
Ye.Li33a56b12014-02-20 18:00:57 +0800248#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu62b56b32019-06-21 11:42:29 +0800249#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700250 addr = virt_to_phys((void *)(data->src));
251 if (upper_32_bits(addr))
252 printf("Error found for upper 32 bits\n");
253 else
254 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
255#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100256 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li33a56b12014-02-20 18:00:57 +0800257#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700258#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500259 }
260
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100261 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500262
263 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530264 /*
265 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
266 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
267 * So, Number of SD Clock cycles for 0.25sec should be minimum
268 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500269 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530270 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500271 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530272 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500273 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530274 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500275 * => timeout + 13 = log2(mmc->clock/4) + 1
276 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800277 *
278 * However, the MMC spec "It is strongly recommended for hosts to
279 * implement more than 500ms timeout value even if the card
280 * indicates the 250ms maximum busy length." Even the previous
281 * value of 300ms is known to be insufficient for some cards.
282 * So, we use
283 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530284 */
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800285 timeout = fls(mmc->clock/2);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500286 timeout -= 13;
287
288 if (timeout > 14)
289 timeout = 14;
290
291 if (timeout < 0)
292 timeout = 0;
293
Kumar Gala9a878d52011-01-29 15:36:10 -0600294#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
295 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
296 timeout++;
297#endif
298
Haijun.Zhangedeb83a2014-03-18 17:04:23 +0800299#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
300 timeout = 0xE;
301#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100302 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500303
304 return 0;
305}
306
Eric Nelson30e9cad2012-04-25 14:28:48 +0000307static void check_and_invalidate_dcache_range
308 (struct mmc_cmd *cmd,
309 struct mmc_data *data) {
Yangbo Lud0e295d2015-03-20 19:28:31 -0700310 unsigned start = 0;
Yangbo Lue7702c62016-05-12 19:12:58 +0800311 unsigned end = 0;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000312 unsigned size = roundup(ARCH_DMA_MINALIGN,
313 data->blocks*data->blocksize);
Yangbo Lu62b56b32019-06-21 11:42:29 +0800314#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700315 dma_addr_t addr;
316
317 addr = virt_to_phys((void *)(data->dest));
318 if (upper_32_bits(addr))
319 printf("Error found for upper 32 bits\n");
320 else
321 start = lower_32_bits(addr);
Yangbo Lue7702c62016-05-12 19:12:58 +0800322#else
323 start = (unsigned)data->dest;
Yangbo Lud0e295d2015-03-20 19:28:31 -0700324#endif
Yangbo Lue7702c62016-05-12 19:12:58 +0800325 end = start + size;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000326 invalidate_dcache_range(start, end);
327}
Angelo Dureghello520a6692019-01-19 10:40:38 +0100328
Andy Fleminge52ffb82008-10-30 16:47:16 -0500329/*
330 * Sends a command out on the bus. Takes the mmc pointer,
331 * a command pointer, and an optional data pointer.
332 */
Simon Glass6aa55dc2017-07-29 11:35:18 -0600333static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
334 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500335{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500336 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500337 uint xfertyp;
338 uint irqstat;
Peng Fanc4142702018-01-21 19:00:24 +0800339 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fana4d36f72016-03-25 14:16:56 +0800340 struct fsl_esdhc *regs = priv->esdhc_regs;
Fabio Estevam7300ef52018-11-19 10:31:53 -0200341 unsigned long start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500342
Jerry Huanged413672011-01-06 23:42:19 -0600343#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
344 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
345 return 0;
346#endif
347
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100348 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500349
350 sync();
351
352 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100353 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
354 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
355 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500356
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100357 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
358 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500359
360 /* Wait at least 8 SD clock cycles before the next command */
361 /*
362 * Note: This is way more than 8 cycles, but 1ms seems to
363 * resolve timing issues with some cards
364 */
365 udelay(1000);
366
367 /* Set up for a data transfer if we have one */
368 if (data) {
Simon Glass1d177d42017-07-29 11:35:17 -0600369 err = esdhc_setup_data(priv, mmc, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500370 if(err)
371 return err;
Peng Fan9cb5e992015-06-25 10:32:26 +0800372
373 if (data->flags & MMC_DATA_READ)
374 check_and_invalidate_dcache_range(cmd, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500375 }
376
377 /* Figure out the transfer arguments */
378 xfertyp = esdhc_xfertyp(cmd, data);
379
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500380 /* Mask all irqs */
381 esdhc_write32(&regs->irqsigen, 0);
382
Andy Fleminge52ffb82008-10-30 16:47:16 -0500383 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100384 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
385 esdhc_write32(&regs->xfertyp, xfertyp);
Dirk Behmed8552d62012-03-26 03:13:05 +0000386
Yangbo Lu73da9c82020-09-01 16:58:01 +0800387 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
388 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
389 flags = IRQSTAT_BRR;
390
Andy Fleminge52ffb82008-10-30 16:47:16 -0500391 /* Wait for the command to complete */
Fabio Estevam7300ef52018-11-19 10:31:53 -0200392 start = get_timer(0);
393 while (!(esdhc_read32(&regs->irqstat) & flags)) {
394 if (get_timer(start) > 1000) {
395 err = -ETIMEDOUT;
396 goto out;
397 }
398 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500399
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100400 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500401
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500402 if (irqstat & CMD_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900403 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500404 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000405 }
406
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500407 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900408 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500409 goto out;
410 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500411
Dirk Behmed8552d62012-03-26 03:13:05 +0000412 /* Workaround for ESDHC errata ENGcm03648 */
413 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800414 int timeout = 6000;
Dirk Behmed8552d62012-03-26 03:13:05 +0000415
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800416 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behmed8552d62012-03-26 03:13:05 +0000417 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
418 PRSSTAT_DAT0)) {
419 udelay(100);
420 timeout--;
421 }
422
423 if (timeout <= 0) {
424 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900425 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500426 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000427 }
428 }
429
Andy Fleminge52ffb82008-10-30 16:47:16 -0500430 /* Copy the response to the response buffer */
431 if (cmd->resp_type & MMC_RSP_136) {
432 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
433
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100434 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
435 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
436 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
437 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530438 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
439 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
440 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
441 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500442 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100443 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500444
445 /* Wait until all of the blocks are transferred */
446 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530447#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass1d177d42017-07-29 11:35:17 -0600448 esdhc_pio_read_write(priv, data);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530449#else
Yangbo Lu73da9c82020-09-01 16:58:01 +0800450 flags = DATA_COMPLETE;
451 if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK ||
452 cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)
453 flags = IRQSTAT_BRR;
454
Andy Fleminge52ffb82008-10-30 16:47:16 -0500455 do {
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100456 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500457
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500458 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900459 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500460 goto out;
461 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000462
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500463 if (irqstat & DATA_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900464 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500465 goto out;
466 }
Yangbo Lu73da9c82020-09-01 16:58:01 +0800467 } while ((irqstat & flags) != flags);
Ye.Li33a56b12014-02-20 18:00:57 +0800468
Peng Fan9cb5e992015-06-25 10:32:26 +0800469 /*
470 * Need invalidate the dcache here again to avoid any
471 * cache-fill during the DMA operations such as the
472 * speculative pre-fetching etc.
473 */
Angelo Dureghello520a6692019-01-19 10:40:38 +0100474 if (data->flags & MMC_DATA_READ) {
Eric Nelson70e68692013-04-03 12:31:56 +0000475 check_and_invalidate_dcache_range(cmd, data);
Angelo Dureghello520a6692019-01-19 10:40:38 +0100476 }
Ye.Li33a56b12014-02-20 18:00:57 +0800477#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500478 }
479
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500480out:
481 /* Reset CMD and DATA portions on error */
482 if (err) {
483 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
484 SYSCTL_RSTC);
485 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
486 ;
487
488 if (data) {
489 esdhc_write32(&regs->sysctl,
490 esdhc_read32(&regs->sysctl) |
491 SYSCTL_RSTD);
492 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
493 ;
494 }
495 }
496
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100497 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500498
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500499 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500500}
501
Simon Glass1d177d42017-07-29 11:35:17 -0600502static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500503{
Benoît Thébaudeau22464e02018-01-16 22:44:18 +0100504 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200505 int div = 1;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200506 int pre_div = 2;
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800507 unsigned int sdhc_clk = priv->sdhc_clk;
508 u32 time_out;
509 u32 value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500510 uint clk;
511
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200512 if (clock < mmc->cfg->f_min)
513 clock = mmc->cfg->f_min;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100514
Yangbo Lu4ee9b862019-10-21 18:09:09 +0800515 while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256)
Lukasz Majewski2a521832019-05-07 17:47:28 +0200516 pre_div *= 2;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500517
Yangbo Lu4ee9b862019-10-21 18:09:09 +0800518 while (sdhc_clk / (div * pre_div) > clock && div < 16)
Lukasz Majewski2a521832019-05-07 17:47:28 +0200519 div++;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500520
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200521 pre_div >>= 1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500522 div -= 1;
523
524 clk = (pre_div << 8) | (div << 4);
525
Kumar Gala09876a32010-03-18 15:51:05 -0500526 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100527
528 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500529
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800530 time_out = 20;
531 value = PRSSTAT_SDSTB;
532 while (!(esdhc_read32(&regs->prsstat) & value)) {
533 if (time_out == 0) {
534 printf("fsl_esdhc: Internal clock never stabilised.\n");
535 break;
536 }
537 time_out--;
538 mdelay(1);
539 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500540
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700541 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500542}
543
Simon Glass1d177d42017-07-29 11:35:17 -0600544static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu163beec2015-04-22 13:57:40 +0800545{
Peng Fana4d36f72016-03-25 14:16:56 +0800546 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu163beec2015-04-22 13:57:40 +0800547 u32 value;
548 u32 time_out;
549
550 value = esdhc_read32(&regs->sysctl);
551
552 if (enable)
553 value |= SYSCTL_CKEN;
554 else
555 value &= ~SYSCTL_CKEN;
556
557 esdhc_write32(&regs->sysctl, value);
558
559 time_out = 20;
560 value = PRSSTAT_SDSTB;
561 while (!(esdhc_read32(&regs->prsstat) & value)) {
562 if (time_out == 0) {
563 printf("fsl_esdhc: Internal clock never stabilised.\n");
564 break;
565 }
566 time_out--;
567 mdelay(1);
568 }
Peng Fanc4142702018-01-21 19:00:24 +0800569}
Yangbo Lu163beec2015-04-22 13:57:40 +0800570
Yangbo Lu73da9c82020-09-01 16:58:01 +0800571static void esdhc_set_timing(struct fsl_esdhc_priv *priv, enum bus_mode mode)
572{
573 struct fsl_esdhc *regs = priv->esdhc_regs;
574
575 esdhc_clock_control(priv, false);
576
577 if (mode == MMC_HS_200)
578 esdhc_clrsetbits32(&regs->autoc12err, UHSM_MASK,
579 UHSM_SDR104_HS200);
580
581 esdhc_clock_control(priv, true);
582}
583
Simon Glass6aa55dc2017-07-29 11:35:18 -0600584static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500585{
Peng Fana4d36f72016-03-25 14:16:56 +0800586 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500587
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800588 if (priv->is_sdhc_per_clk) {
589 /* Select to use peripheral clock */
590 esdhc_clock_control(priv, false);
591 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_PCS);
592 esdhc_clock_control(priv, true);
593 }
594
Andy Fleminge52ffb82008-10-30 16:47:16 -0500595 /* Set the clock speed */
Peng Fanc4142702018-01-21 19:00:24 +0800596 if (priv->clock != mmc->clock)
597 set_sysctl(priv, mmc, mmc->clock);
598
Yangbo Lu73da9c82020-09-01 16:58:01 +0800599 /* Set timing */
600 esdhc_set_timing(priv, mmc->selected_mode);
601
Andy Fleminge52ffb82008-10-30 16:47:16 -0500602 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100603 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500604
605 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100606 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500607 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100608 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
609
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900610 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500611}
612
Rasmus Villemoesa6d1f1a2020-01-30 12:06:45 +0000613static void esdhc_enable_cache_snooping(struct fsl_esdhc *regs)
614{
615#ifdef CONFIG_ARCH_MPC830X
616 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
617 sysconf83xx_t *sysconf = &immr->sysconf;
618
619 setbits_be32(&sysconf->sdhccr, 0x02000000);
620#else
621 esdhc_write32(&regs->esdhcctl, 0x00000040);
622#endif
623}
624
Simon Glass6aa55dc2017-07-29 11:35:18 -0600625static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500626{
Peng Fana4d36f72016-03-25 14:16:56 +0800627 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass0c3ef222017-07-29 11:35:20 -0600628 ulong start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500629
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100630 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200631 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100632
633 /* Wait until the controller is available */
Simon Glass0c3ef222017-07-29 11:35:20 -0600634 start = get_timer(0);
635 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
636 if (get_timer(start) > 1000)
637 return -ETIMEDOUT;
638 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500639
Rasmus Villemoesa6d1f1a2020-01-30 12:06:45 +0000640 esdhc_enable_cache_snooping(regs);
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530641
Dirk Behmedbe67252013-07-15 15:44:29 +0200642 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500643
644 /* Set the initial clock speed */
Jaehoon Chung239cb2f2018-01-26 19:25:29 +0900645 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500646
647 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100648 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500649
650 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100651 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500652
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100653 /* Set timout to the maximum value */
654 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500655
Thierry Reding8cee4c982012-01-02 01:15:38 +0000656 return 0;
657}
658
Simon Glass6aa55dc2017-07-29 11:35:18 -0600659static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Reding8cee4c982012-01-02 01:15:38 +0000660{
Peng Fana4d36f72016-03-25 14:16:56 +0800661 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500662
Haijun.Zhang05f58542014-01-10 13:52:17 +0800663#ifdef CONFIG_ESDHC_DETECT_QUIRK
664 if (CONFIG_ESDHC_DETECT_QUIRK)
665 return 1;
666#endif
Yangbo Lu8abc0432020-05-19 11:06:43 +0800667 if (esdhc_read32(&regs->prsstat) & PRSSTAT_CINS)
668 return 1;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100669
Yangbo Lu8abc0432020-05-19 11:06:43 +0800670 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500671}
672
Yangbo Lub64dc8d2019-10-31 18:54:23 +0800673static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv,
674 struct mmc_config *cfg)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500675{
Yangbo Lub64dc8d2019-10-31 18:54:23 +0800676 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu63267b42019-10-31 18:54:21 +0800677 u32 caps;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500678
Wang Huanc9292132014-09-05 13:52:40 +0800679 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang39356612011-01-07 00:06:47 -0600680#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
Yangbo Lu63267b42019-10-31 18:54:21 +0800681 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
Roy Zang39356612011-01-07 00:06:47 -0600682#endif
Haijun.Zhang8a065e92013-10-31 09:38:19 +0800683#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
Yangbo Lu63267b42019-10-31 18:54:21 +0800684 caps |= HOSTCAPBLT_VS33;
Haijun.Zhang8a065e92013-10-31 09:38:19 +0800685#endif
Yangbo Lu63267b42019-10-31 18:54:21 +0800686 if (caps & HOSTCAPBLT_VS18)
687 cfg->voltages |= MMC_VDD_165_195;
688 if (caps & HOSTCAPBLT_VS30)
689 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
690 if (caps & HOSTCAPBLT_VS33)
691 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +0000692
Simon Glassfa02ca52017-07-29 11:35:21 -0600693 cfg->name = "FSL_SDHC";
Abbas Razae6bf9772013-03-25 09:13:34 +0000694
Yangbo Lu63267b42019-10-31 18:54:21 +0800695 if (caps & HOSTCAPBLT_HSS)
Simon Glassfa02ca52017-07-29 11:35:21 -0600696 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500697
Simon Glassfa02ca52017-07-29 11:35:21 -0600698 cfg->f_min = 400000;
Peng Fanc4142702018-01-21 19:00:24 +0800699 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Simon Glassfa02ca52017-07-29 11:35:21 -0600700 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Peng Fana4d36f72016-03-25 14:16:56 +0800701}
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400702
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100703#ifdef CONFIG_OF_LIBFDT
Yangbo Lud84139c2017-01-17 10:43:54 +0800704__weak int esdhc_status_fixup(void *blob, const char *compat)
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400705{
Chenhui Zhao025eab02011-01-04 17:23:05 +0800706#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400707 if (!hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800708 do_fixup_by_compat(blob, compat, "status", "disabled",
Yangbo Lud84139c2017-01-17 10:43:54 +0800709 sizeof("disabled"), 1);
710 return 1;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400711 }
Chenhui Zhao025eab02011-01-04 17:23:05 +0800712#endif
Yangbo Lud84139c2017-01-17 10:43:54 +0800713 return 0;
714}
715
Yangbo Luce884022020-05-19 11:06:44 +0800716#ifdef CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND
717static int fsl_esdhc_get_cd(struct udevice *dev);
718
719static void esdhc_disable_for_no_card(void *blob)
720{
721 struct udevice *dev;
722
723 for (uclass_first_device(UCLASS_MMC, &dev);
724 dev;
725 uclass_next_device(&dev)) {
726 char esdhc_path[50];
727
728 if (fsl_esdhc_get_cd(dev))
729 continue;
730
731 snprintf(esdhc_path, sizeof(esdhc_path), "/soc/esdhc@%lx",
732 (unsigned long)dev_read_addr(dev));
733 do_fixup_by_path(blob, esdhc_path, "status", "disabled",
734 sizeof("disabled"), 1);
735 }
736}
737#endif
738
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900739void fdt_fixup_esdhc(void *blob, struct bd_info *bd)
Yangbo Lud84139c2017-01-17 10:43:54 +0800740{
741 const char *compat = "fsl,esdhc";
742
743 if (esdhc_status_fixup(blob, compat))
744 return;
Yangbo Luce884022020-05-19 11:06:44 +0800745#ifdef CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND
746 esdhc_disable_for_no_card(blob);
747#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400748 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000749 gd->arch.sdhc_clk, 1);
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400750}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100751#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800752
Yangbo Lu4fc93332019-10-31 18:54:26 +0800753#if !CONFIG_IS_ENABLED(DM_MMC)
754static int esdhc_getcd(struct mmc *mmc)
755{
756 struct fsl_esdhc_priv *priv = mmc->priv;
757
758 return esdhc_getcd_common(priv);
759}
760
761static int esdhc_init(struct mmc *mmc)
762{
763 struct fsl_esdhc_priv *priv = mmc->priv;
764
765 return esdhc_init_common(priv, mmc);
766}
767
768static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
769 struct mmc_data *data)
770{
771 struct fsl_esdhc_priv *priv = mmc->priv;
772
773 return esdhc_send_cmd_common(priv, mmc, cmd, data);
774}
775
776static int esdhc_set_ios(struct mmc *mmc)
777{
778 struct fsl_esdhc_priv *priv = mmc->priv;
779
780 return esdhc_set_ios_common(priv, mmc);
781}
782
783static const struct mmc_ops esdhc_ops = {
784 .getcd = esdhc_getcd,
785 .init = esdhc_init,
786 .send_cmd = esdhc_send_cmd,
787 .set_ios = esdhc_set_ios,
788};
789
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900790int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg)
Yangbo Lu4fc93332019-10-31 18:54:26 +0800791{
792 struct fsl_esdhc_plat *plat;
793 struct fsl_esdhc_priv *priv;
794 struct mmc_config *mmc_cfg;
795 struct mmc *mmc;
796
797 if (!cfg)
798 return -EINVAL;
799
800 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
801 if (!priv)
802 return -ENOMEM;
803 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
804 if (!plat) {
805 free(priv);
806 return -ENOMEM;
807 }
808
809 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
810 priv->sdhc_clk = cfg->sdhc_clk;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800811 if (gd->arch.sdhc_per_clk)
812 priv->is_sdhc_per_clk = true;
Yangbo Lu4fc93332019-10-31 18:54:26 +0800813
814 mmc_cfg = &plat->cfg;
815
816 if (cfg->max_bus_width == 8) {
817 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
818 MMC_MODE_8BIT;
819 } else if (cfg->max_bus_width == 4) {
820 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT;
821 } else if (cfg->max_bus_width == 1) {
822 mmc_cfg->host_caps |= MMC_MODE_1BIT;
823 } else {
824 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
825 MMC_MODE_8BIT;
826 printf("No max bus width provided. Assume 8-bit supported.\n");
827 }
828
829#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
830 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
831 mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
832#endif
833 mmc_cfg->ops = &esdhc_ops;
834
835 fsl_esdhc_get_cfg_common(priv, mmc_cfg);
836
837 mmc = mmc_create(mmc_cfg, priv);
838 if (!mmc)
839 return -EIO;
840
841 priv->mmc = mmc;
842 return 0;
843}
844
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900845int fsl_esdhc_mmc_init(struct bd_info *bis)
Yangbo Lu4fc93332019-10-31 18:54:26 +0800846{
847 struct fsl_esdhc_cfg *cfg;
848
849 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
850 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800851 /* Prefer peripheral clock which provides higher frequency. */
852 if (gd->arch.sdhc_per_clk)
853 cfg->sdhc_clk = gd->arch.sdhc_per_clk;
854 else
855 cfg->sdhc_clk = gd->arch.sdhc_clk;
Yangbo Lu4fc93332019-10-31 18:54:26 +0800856 return fsl_esdhc_initialize(bis, cfg);
857}
858#else /* DM_MMC */
Peng Fana4d36f72016-03-25 14:16:56 +0800859static int fsl_esdhc_probe(struct udevice *dev)
860{
861 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa02ca52017-07-29 11:35:21 -0600862 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800863 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800864 fdt_addr_t addr;
Simon Glass407025d2017-07-29 11:35:24 -0600865 struct mmc *mmc;
Yangbo Luce884022020-05-19 11:06:44 +0800866 int ret;
Peng Fana4d36f72016-03-25 14:16:56 +0800867
Simon Glass80e9df42017-07-29 11:35:23 -0600868 addr = dev_read_addr(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800869 if (addr == FDT_ADDR_T_NONE)
870 return -EINVAL;
Yinbo Zhu583d5e92019-04-11 11:01:50 +0000871#ifdef CONFIG_PPC
872 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
873#else
Peng Fana4d36f72016-03-25 14:16:56 +0800874 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yinbo Zhu583d5e92019-04-11 11:01:50 +0000875#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800876 priv->dev = dev;
877
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800878 if (gd->arch.sdhc_per_clk) {
879 priv->sdhc_clk = gd->arch.sdhc_per_clk;
880 priv->is_sdhc_per_clk = true;
881 } else {
882 priv->sdhc_clk = gd->arch.sdhc_clk;
883 }
884
Yangbo Lub8626e42019-11-12 19:28:36 +0800885 if (priv->sdhc_clk <= 0) {
886 dev_err(dev, "Unable to get clk for %s\n", dev->name);
887 return -EINVAL;
Peng Fana4d36f72016-03-25 14:16:56 +0800888 }
889
Yangbo Lub64dc8d2019-10-31 18:54:23 +0800890 fsl_esdhc_get_cfg_common(priv, &plat->cfg);
Peng Fana4d36f72016-03-25 14:16:56 +0800891
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800892 mmc_of_parse(dev, &plat->cfg);
893
Simon Glass407025d2017-07-29 11:35:24 -0600894 mmc = &plat->mmc;
895 mmc->cfg = &plat->cfg;
896 mmc->dev = dev;
Yangbo Lu4cc119b2019-05-23 11:05:46 +0800897
Simon Glass407025d2017-07-29 11:35:24 -0600898 upriv->mmc = mmc;
Peng Fana4d36f72016-03-25 14:16:56 +0800899
Yangbo Luce884022020-05-19 11:06:44 +0800900 ret = esdhc_init_common(priv, mmc);
901 if (ret)
902 return ret;
903
904#ifdef CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND
905 if (!fsl_esdhc_get_cd(dev))
906 esdhc_setbits32(&priv->esdhc_regs->proctl, PROCTL_VOLT_SEL);
907#endif
908 return 0;
Peng Fana4d36f72016-03-25 14:16:56 +0800909}
910
Simon Glass407025d2017-07-29 11:35:24 -0600911static int fsl_esdhc_get_cd(struct udevice *dev)
912{
Yangbo Lu9fed28d2019-10-31 18:54:24 +0800913 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Simon Glass407025d2017-07-29 11:35:24 -0600914 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
915
Yangbo Lu9fed28d2019-10-31 18:54:24 +0800916 if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
917 return 1;
918
Simon Glass407025d2017-07-29 11:35:24 -0600919 return esdhc_getcd_common(priv);
920}
921
922static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
923 struct mmc_data *data)
924{
925 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
926 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
927
928 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
929}
930
931static int fsl_esdhc_set_ios(struct udevice *dev)
932{
933 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
934 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
935
936 return esdhc_set_ios_common(priv, &plat->mmc);
937}
938
Yangbo Lu76c74692020-09-01 16:58:00 +0800939static int fsl_esdhc_reinit(struct udevice *dev)
940{
941 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
942 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
943
944 return esdhc_init_common(priv, &plat->mmc);
945}
946
Yangbo Lu73da9c82020-09-01 16:58:01 +0800947#ifdef MMC_SUPPORTS_TUNING
948static void esdhc_flush_async_fifo(struct fsl_esdhc_priv *priv)
949{
950 struct fsl_esdhc *regs = priv->esdhc_regs;
951 u32 time_out;
952
953 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_FAF);
954
955 time_out = 20;
956 while (esdhc_read32(&regs->esdhcctl) & ESDHCCTL_FAF) {
957 if (time_out == 0) {
958 printf("fsl_esdhc: Flush asynchronous FIFO timeout.\n");
959 break;
960 }
961 time_out--;
962 mdelay(1);
963 }
964}
965
966static void esdhc_tuning_block_enable(struct fsl_esdhc_priv *priv,
967 bool en)
968{
969 struct fsl_esdhc *regs = priv->esdhc_regs;
970
971 esdhc_clock_control(priv, false);
972 esdhc_flush_async_fifo(priv);
973 if (en)
974 esdhc_setbits32(&regs->tbctl, TBCTL_TB_EN);
975 else
976 esdhc_clrbits32(&regs->tbctl, TBCTL_TB_EN);
977 esdhc_clock_control(priv, true);
978}
979
980static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode)
981{
982 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
983 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
984 struct fsl_esdhc *regs = priv->esdhc_regs;
985 u32 val, irqstaten;
986 int i;
987
988 esdhc_tuning_block_enable(priv, true);
989 esdhc_setbits32(&regs->autoc12err, EXECUTE_TUNING);
990
991 irqstaten = esdhc_read32(&regs->irqstaten);
992 esdhc_write32(&regs->irqstaten, IRQSTATEN_BRR);
993
994 for (i = 0; i < MAX_TUNING_LOOP; i++) {
995 mmc_send_tuning(&plat->mmc, opcode, NULL);
996 mdelay(1);
997
998 val = esdhc_read32(&regs->autoc12err);
999 if (!(val & EXECUTE_TUNING)) {
1000 if (val & SMPCLKSEL)
1001 break;
1002 }
1003 }
1004
1005 esdhc_write32(&regs->irqstaten, irqstaten);
1006
1007 if (i != MAX_TUNING_LOOP)
1008 return 0;
1009
1010 printf("fsl_esdhc: tuning failed!\n");
1011 esdhc_clrbits32(&regs->autoc12err, SMPCLKSEL);
1012 esdhc_clrbits32(&regs->autoc12err, EXECUTE_TUNING);
1013 esdhc_tuning_block_enable(priv, false);
1014 return -ETIMEDOUT;
1015}
1016#endif
1017
Simon Glass407025d2017-07-29 11:35:24 -06001018static const struct dm_mmc_ops fsl_esdhc_ops = {
1019 .get_cd = fsl_esdhc_get_cd,
1020 .send_cmd = fsl_esdhc_send_cmd,
1021 .set_ios = fsl_esdhc_set_ios,
Yinbo Zhu101d3ef2019-07-16 15:09:11 +08001022#ifdef MMC_SUPPORTS_TUNING
1023 .execute_tuning = fsl_esdhc_execute_tuning,
1024#endif
Yangbo Lu76c74692020-09-01 16:58:00 +08001025 .reinit = fsl_esdhc_reinit,
Simon Glass407025d2017-07-29 11:35:24 -06001026};
Simon Glass407025d2017-07-29 11:35:24 -06001027
Peng Fana4d36f72016-03-25 14:16:56 +08001028static const struct udevice_id fsl_esdhc_ids[] = {
Yangbo Lu2a99b602016-12-07 11:54:31 +08001029 { .compatible = "fsl,esdhc", },
Peng Fana4d36f72016-03-25 14:16:56 +08001030 { /* sentinel */ }
1031};
1032
Simon Glass407025d2017-07-29 11:35:24 -06001033static int fsl_esdhc_bind(struct udevice *dev)
1034{
1035 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1036
1037 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1038}
Simon Glass407025d2017-07-29 11:35:24 -06001039
Peng Fana4d36f72016-03-25 14:16:56 +08001040U_BOOT_DRIVER(fsl_esdhc) = {
1041 .name = "fsl-esdhc-mmc",
1042 .id = UCLASS_MMC,
1043 .of_match = fsl_esdhc_ids,
Simon Glass407025d2017-07-29 11:35:24 -06001044 .ops = &fsl_esdhc_ops,
Simon Glass407025d2017-07-29 11:35:24 -06001045 .bind = fsl_esdhc_bind,
Peng Fana4d36f72016-03-25 14:16:56 +08001046 .probe = fsl_esdhc_probe,
Simon Glassfa02ca52017-07-29 11:35:21 -06001047 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fana4d36f72016-03-25 14:16:56 +08001048 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1049};
1050#endif