blob: a4b923ab99f2d1719596aa9d5badb01d3457eb8f [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 Lu62b56b32019-06-21 11:42:29 +080063 char reserved6[756]; /* reserved */
64 uint esdhcctl; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050065};
66
Simon Glassfa02ca52017-07-29 11:35:21 -060067struct fsl_esdhc_plat {
68 struct mmc_config cfg;
69 struct mmc mmc;
70};
71
Peng Fana4d36f72016-03-25 14:16:56 +080072/**
73 * struct fsl_esdhc_priv
74 *
75 * @esdhc_regs: registers of the sdhc controller
76 * @sdhc_clk: Current clk of the sdhc controller
77 * @bus_width: bus width, 1bit, 4bit or 8bit
78 * @cfg: mmc config
79 * @mmc: mmc
80 * Following is used when Driver Model is enabled for MMC
81 * @dev: pointer for the device
Peng Fana4d36f72016-03-25 14:16:56 +080082 * @cd_gpio: gpio for card detection
Peng Fan01eb1c42016-06-15 10:53:02 +080083 * @wp_gpio: gpio for write protection
Peng Fana4d36f72016-03-25 14:16:56 +080084 */
85struct fsl_esdhc_priv {
86 struct fsl_esdhc *esdhc_regs;
87 unsigned int sdhc_clk;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +080088 bool is_sdhc_per_clk;
Peng Fanc4142702018-01-21 19:00:24 +080089 unsigned int clock;
Yangbo Lu77f26322019-10-21 18:09:07 +080090#if !CONFIG_IS_ENABLED(DM_MMC)
Peng Fana4d36f72016-03-25 14:16:56 +080091 struct mmc *mmc;
Simon Glass407025d2017-07-29 11:35:24 -060092#endif
Peng Fana4d36f72016-03-25 14:16:56 +080093 struct udevice *dev;
Peng Fana4d36f72016-03-25 14:16:56 +080094};
95
Andy Fleminge52ffb82008-10-30 16:47:16 -050096/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +000097static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -050098{
99 uint xfertyp = 0;
100
101 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530102 xfertyp |= XFERTYP_DPSEL;
103#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
104 xfertyp |= XFERTYP_DMAEN;
105#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500106 if (data->blocks > 1) {
107 xfertyp |= XFERTYP_MSBSEL;
108 xfertyp |= XFERTYP_BCEN;
Jerry Huanged413672011-01-06 23:42:19 -0600109#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
110 xfertyp |= XFERTYP_AC12EN;
111#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500112 }
113
114 if (data->flags & MMC_DATA_READ)
115 xfertyp |= XFERTYP_DTDSEL;
116 }
117
118 if (cmd->resp_type & MMC_RSP_CRC)
119 xfertyp |= XFERTYP_CCCEN;
120 if (cmd->resp_type & MMC_RSP_OPCODE)
121 xfertyp |= XFERTYP_CICEN;
122 if (cmd->resp_type & MMC_RSP_136)
123 xfertyp |= XFERTYP_RSPTYP_136;
124 else if (cmd->resp_type & MMC_RSP_BUSY)
125 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
126 else if (cmd->resp_type & MMC_RSP_PRESENT)
127 xfertyp |= XFERTYP_RSPTYP_48;
128
Jason Liubef0ff02011-03-22 01:32:31 +0000129 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
130 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lub73a3d62016-01-21 17:33:19 +0800131
Andy Fleminge52ffb82008-10-30 16:47:16 -0500132 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
133}
134
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530135#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
136/*
137 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
138 */
Simon Glass1d177d42017-07-29 11:35:17 -0600139static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
140 struct mmc_data *data)
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530141{
Peng Fana4d36f72016-03-25 14:16:56 +0800142 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530143 uint blocks;
144 char *buffer;
145 uint databuf;
146 uint size;
147 uint irqstat;
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100148 ulong start;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530149
150 if (data->flags & MMC_DATA_READ) {
151 blocks = data->blocks;
152 buffer = data->dest;
153 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100154 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530155 size = data->blocksize;
156 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100157 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
158 if (get_timer(start) > PIO_TIMEOUT) {
159 printf("\nData Read Failed in PIO Mode.");
160 return;
161 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530162 }
163 while (size && (!(irqstat & IRQSTAT_TC))) {
164 udelay(100); /* Wait before last byte transfer complete */
165 irqstat = esdhc_read32(&regs->irqstat);
166 databuf = in_le32(&regs->datport);
167 *((uint *)buffer) = databuf;
168 buffer += 4;
169 size -= 4;
170 }
171 blocks--;
172 }
173 } else {
174 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200175 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530176 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100177 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530178 size = data->blocksize;
179 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100180 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
181 if (get_timer(start) > PIO_TIMEOUT) {
182 printf("\nData Write Failed in PIO Mode.");
183 return;
184 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530185 }
186 while (size && (!(irqstat & IRQSTAT_TC))) {
187 udelay(100); /* Wait before last byte transfer complete */
188 databuf = *((uint *)buffer);
189 buffer += 4;
190 size -= 4;
191 irqstat = esdhc_read32(&regs->irqstat);
192 out_le32(&regs->datport, databuf);
193 }
194 blocks--;
195 }
196 }
197}
198#endif
199
Simon Glass1d177d42017-07-29 11:35:17 -0600200static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
201 struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500202{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500203 int timeout;
Peng Fana4d36f72016-03-25 14:16:56 +0800204 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu62b56b32019-06-21 11:42:29 +0800205#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700206 dma_addr_t addr;
207#endif
Wolfgang Denka40545c2010-05-09 23:52:59 +0200208 uint wml_value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500209
210 wml_value = data->blocksize/4;
211
212 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530213 if (wml_value > WML_RD_WML_MAX)
214 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500215
Roy Zange5853af2010-02-09 18:23:33 +0800216 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li33a56b12014-02-20 18:00:57 +0800217#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu62b56b32019-06-21 11:42:29 +0800218#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700219 addr = virt_to_phys((void *)(data->dest));
220 if (upper_32_bits(addr))
221 printf("Error found for upper 32 bits\n");
222 else
223 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
224#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100225 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li33a56b12014-02-20 18:00:57 +0800226#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700227#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500228 } else {
Ye.Li33a56b12014-02-20 18:00:57 +0800229#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000230 flush_dcache_range((ulong)data->src,
231 (ulong)data->src+data->blocks
232 *data->blocksize);
Ye.Li33a56b12014-02-20 18:00:57 +0800233#endif
Priyanka Jain02449632011-02-09 09:24:10 +0530234 if (wml_value > WML_WR_WML_MAX)
235 wml_value = WML_WR_WML_MAX_VAL;
Yangbo Luf3bcc832019-10-31 18:54:25 +0800236
237 if (!(esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL)) {
238 printf("Can not write to locked SD card.\n");
239 return -EINVAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500240 }
Roy Zange5853af2010-02-09 18:23:33 +0800241
242 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
243 wml_value << 16);
Ye.Li33a56b12014-02-20 18:00:57 +0800244#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu62b56b32019-06-21 11:42:29 +0800245#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700246 addr = virt_to_phys((void *)(data->src));
247 if (upper_32_bits(addr))
248 printf("Error found for upper 32 bits\n");
249 else
250 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
251#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100252 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li33a56b12014-02-20 18:00:57 +0800253#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700254#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500255 }
256
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100257 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500258
259 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530260 /*
261 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
262 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
263 * So, Number of SD Clock cycles for 0.25sec should be minimum
264 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500265 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530266 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500267 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530268 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500269 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530270 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500271 * => timeout + 13 = log2(mmc->clock/4) + 1
272 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800273 *
274 * However, the MMC spec "It is strongly recommended for hosts to
275 * implement more than 500ms timeout value even if the card
276 * indicates the 250ms maximum busy length." Even the previous
277 * value of 300ms is known to be insufficient for some cards.
278 * So, we use
279 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530280 */
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800281 timeout = fls(mmc->clock/2);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500282 timeout -= 13;
283
284 if (timeout > 14)
285 timeout = 14;
286
287 if (timeout < 0)
288 timeout = 0;
289
Kumar Gala9a878d52011-01-29 15:36:10 -0600290#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
291 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
292 timeout++;
293#endif
294
Haijun.Zhangedeb83a2014-03-18 17:04:23 +0800295#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
296 timeout = 0xE;
297#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100298 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500299
300 return 0;
301}
302
Eric Nelson30e9cad2012-04-25 14:28:48 +0000303static void check_and_invalidate_dcache_range
304 (struct mmc_cmd *cmd,
305 struct mmc_data *data) {
Yangbo Lud0e295d2015-03-20 19:28:31 -0700306 unsigned start = 0;
Yangbo Lue7702c62016-05-12 19:12:58 +0800307 unsigned end = 0;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000308 unsigned size = roundup(ARCH_DMA_MINALIGN,
309 data->blocks*data->blocksize);
Yangbo Lu62b56b32019-06-21 11:42:29 +0800310#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700311 dma_addr_t addr;
312
313 addr = virt_to_phys((void *)(data->dest));
314 if (upper_32_bits(addr))
315 printf("Error found for upper 32 bits\n");
316 else
317 start = lower_32_bits(addr);
Yangbo Lue7702c62016-05-12 19:12:58 +0800318#else
319 start = (unsigned)data->dest;
Yangbo Lud0e295d2015-03-20 19:28:31 -0700320#endif
Yangbo Lue7702c62016-05-12 19:12:58 +0800321 end = start + size;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000322 invalidate_dcache_range(start, end);
323}
Angelo Dureghello520a6692019-01-19 10:40:38 +0100324
Andy Fleminge52ffb82008-10-30 16:47:16 -0500325/*
326 * Sends a command out on the bus. Takes the mmc pointer,
327 * a command pointer, and an optional data pointer.
328 */
Simon Glass6aa55dc2017-07-29 11:35:18 -0600329static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
330 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500331{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500332 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500333 uint xfertyp;
334 uint irqstat;
Peng Fanc4142702018-01-21 19:00:24 +0800335 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fana4d36f72016-03-25 14:16:56 +0800336 struct fsl_esdhc *regs = priv->esdhc_regs;
Fabio Estevam7300ef52018-11-19 10:31:53 -0200337 unsigned long start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500338
Jerry Huanged413672011-01-06 23:42:19 -0600339#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
340 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
341 return 0;
342#endif
343
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100344 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500345
346 sync();
347
348 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100349 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
350 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
351 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500352
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100353 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
354 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500355
356 /* Wait at least 8 SD clock cycles before the next command */
357 /*
358 * Note: This is way more than 8 cycles, but 1ms seems to
359 * resolve timing issues with some cards
360 */
361 udelay(1000);
362
363 /* Set up for a data transfer if we have one */
364 if (data) {
Simon Glass1d177d42017-07-29 11:35:17 -0600365 err = esdhc_setup_data(priv, mmc, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500366 if(err)
367 return err;
Peng Fan9cb5e992015-06-25 10:32:26 +0800368
369 if (data->flags & MMC_DATA_READ)
370 check_and_invalidate_dcache_range(cmd, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500371 }
372
373 /* Figure out the transfer arguments */
374 xfertyp = esdhc_xfertyp(cmd, data);
375
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500376 /* Mask all irqs */
377 esdhc_write32(&regs->irqsigen, 0);
378
Andy Fleminge52ffb82008-10-30 16:47:16 -0500379 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100380 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
381 esdhc_write32(&regs->xfertyp, xfertyp);
Dirk Behmed8552d62012-03-26 03:13:05 +0000382
Andy Fleminge52ffb82008-10-30 16:47:16 -0500383 /* Wait for the command to complete */
Fabio Estevam7300ef52018-11-19 10:31:53 -0200384 start = get_timer(0);
385 while (!(esdhc_read32(&regs->irqstat) & flags)) {
386 if (get_timer(start) > 1000) {
387 err = -ETIMEDOUT;
388 goto out;
389 }
390 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500391
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100392 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500393
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500394 if (irqstat & CMD_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900395 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500396 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000397 }
398
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500399 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900400 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500401 goto out;
402 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500403
Dirk Behmed8552d62012-03-26 03:13:05 +0000404 /* Workaround for ESDHC errata ENGcm03648 */
405 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800406 int timeout = 6000;
Dirk Behmed8552d62012-03-26 03:13:05 +0000407
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800408 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behmed8552d62012-03-26 03:13:05 +0000409 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
410 PRSSTAT_DAT0)) {
411 udelay(100);
412 timeout--;
413 }
414
415 if (timeout <= 0) {
416 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900417 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500418 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000419 }
420 }
421
Andy Fleminge52ffb82008-10-30 16:47:16 -0500422 /* Copy the response to the response buffer */
423 if (cmd->resp_type & MMC_RSP_136) {
424 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
425
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100426 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
427 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
428 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
429 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530430 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
431 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
432 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
433 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500434 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100435 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500436
437 /* Wait until all of the blocks are transferred */
438 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530439#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass1d177d42017-07-29 11:35:17 -0600440 esdhc_pio_read_write(priv, data);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530441#else
Andy Fleminge52ffb82008-10-30 16:47:16 -0500442 do {
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100443 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500444
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500445 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900446 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500447 goto out;
448 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000449
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500450 if (irqstat & DATA_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900451 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500452 goto out;
453 }
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800454 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li33a56b12014-02-20 18:00:57 +0800455
Peng Fan9cb5e992015-06-25 10:32:26 +0800456 /*
457 * Need invalidate the dcache here again to avoid any
458 * cache-fill during the DMA operations such as the
459 * speculative pre-fetching etc.
460 */
Angelo Dureghello520a6692019-01-19 10:40:38 +0100461 if (data->flags & MMC_DATA_READ) {
Eric Nelson70e68692013-04-03 12:31:56 +0000462 check_and_invalidate_dcache_range(cmd, data);
Angelo Dureghello520a6692019-01-19 10:40:38 +0100463 }
Ye.Li33a56b12014-02-20 18:00:57 +0800464#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500465 }
466
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500467out:
468 /* Reset CMD and DATA portions on error */
469 if (err) {
470 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
471 SYSCTL_RSTC);
472 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
473 ;
474
475 if (data) {
476 esdhc_write32(&regs->sysctl,
477 esdhc_read32(&regs->sysctl) |
478 SYSCTL_RSTD);
479 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
480 ;
481 }
482 }
483
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100484 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500485
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500486 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500487}
488
Simon Glass1d177d42017-07-29 11:35:17 -0600489static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500490{
Benoît Thébaudeau22464e02018-01-16 22:44:18 +0100491 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200492 int div = 1;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200493 int pre_div = 2;
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800494 unsigned int sdhc_clk = priv->sdhc_clk;
495 u32 time_out;
496 u32 value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500497 uint clk;
498
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200499 if (clock < mmc->cfg->f_min)
500 clock = mmc->cfg->f_min;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100501
Yangbo Lu4ee9b862019-10-21 18:09:09 +0800502 while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256)
Lukasz Majewski2a521832019-05-07 17:47:28 +0200503 pre_div *= 2;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500504
Yangbo Lu4ee9b862019-10-21 18:09:09 +0800505 while (sdhc_clk / (div * pre_div) > clock && div < 16)
Lukasz Majewski2a521832019-05-07 17:47:28 +0200506 div++;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500507
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200508 pre_div >>= 1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500509 div -= 1;
510
511 clk = (pre_div << 8) | (div << 4);
512
Kumar Gala09876a32010-03-18 15:51:05 -0500513 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100514
515 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500516
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800517 time_out = 20;
518 value = PRSSTAT_SDSTB;
519 while (!(esdhc_read32(&regs->prsstat) & value)) {
520 if (time_out == 0) {
521 printf("fsl_esdhc: Internal clock never stabilised.\n");
522 break;
523 }
524 time_out--;
525 mdelay(1);
526 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500527
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700528 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500529}
530
Simon Glass1d177d42017-07-29 11:35:17 -0600531static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu163beec2015-04-22 13:57:40 +0800532{
Peng Fana4d36f72016-03-25 14:16:56 +0800533 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu163beec2015-04-22 13:57:40 +0800534 u32 value;
535 u32 time_out;
536
537 value = esdhc_read32(&regs->sysctl);
538
539 if (enable)
540 value |= SYSCTL_CKEN;
541 else
542 value &= ~SYSCTL_CKEN;
543
544 esdhc_write32(&regs->sysctl, value);
545
546 time_out = 20;
547 value = PRSSTAT_SDSTB;
548 while (!(esdhc_read32(&regs->prsstat) & value)) {
549 if (time_out == 0) {
550 printf("fsl_esdhc: Internal clock never stabilised.\n");
551 break;
552 }
553 time_out--;
554 mdelay(1);
555 }
Peng Fanc4142702018-01-21 19:00:24 +0800556}
Yangbo Lu163beec2015-04-22 13:57:40 +0800557
Simon Glass6aa55dc2017-07-29 11:35:18 -0600558static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500559{
Peng Fana4d36f72016-03-25 14:16:56 +0800560 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500561
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800562 if (priv->is_sdhc_per_clk) {
563 /* Select to use peripheral clock */
564 esdhc_clock_control(priv, false);
565 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_PCS);
566 esdhc_clock_control(priv, true);
567 }
568
Andy Fleminge52ffb82008-10-30 16:47:16 -0500569 /* Set the clock speed */
Peng Fanc4142702018-01-21 19:00:24 +0800570 if (priv->clock != mmc->clock)
571 set_sysctl(priv, mmc, mmc->clock);
572
Andy Fleminge52ffb82008-10-30 16:47:16 -0500573 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100574 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500575
576 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100577 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500578 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100579 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
580
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900581 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500582}
583
Rasmus Villemoesa6d1f1a2020-01-30 12:06:45 +0000584static void esdhc_enable_cache_snooping(struct fsl_esdhc *regs)
585{
586#ifdef CONFIG_ARCH_MPC830X
587 immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
588 sysconf83xx_t *sysconf = &immr->sysconf;
589
590 setbits_be32(&sysconf->sdhccr, 0x02000000);
591#else
592 esdhc_write32(&regs->esdhcctl, 0x00000040);
593#endif
594}
595
Simon Glass6aa55dc2017-07-29 11:35:18 -0600596static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500597{
Peng Fana4d36f72016-03-25 14:16:56 +0800598 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass0c3ef222017-07-29 11:35:20 -0600599 ulong start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500600
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100601 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200602 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100603
604 /* Wait until the controller is available */
Simon Glass0c3ef222017-07-29 11:35:20 -0600605 start = get_timer(0);
606 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
607 if (get_timer(start) > 1000)
608 return -ETIMEDOUT;
609 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500610
Rasmus Villemoesa6d1f1a2020-01-30 12:06:45 +0000611 esdhc_enable_cache_snooping(regs);
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530612
Dirk Behmedbe67252013-07-15 15:44:29 +0200613 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500614
615 /* Set the initial clock speed */
Jaehoon Chung239cb2f2018-01-26 19:25:29 +0900616 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500617
618 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100619 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500620
621 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100622 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500623
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100624 /* Set timout to the maximum value */
625 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500626
Thierry Reding8cee4c982012-01-02 01:15:38 +0000627 return 0;
628}
629
Simon Glass6aa55dc2017-07-29 11:35:18 -0600630static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Reding8cee4c982012-01-02 01:15:38 +0000631{
Peng Fana4d36f72016-03-25 14:16:56 +0800632 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500633
Haijun.Zhang05f58542014-01-10 13:52:17 +0800634#ifdef CONFIG_ESDHC_DETECT_QUIRK
635 if (CONFIG_ESDHC_DETECT_QUIRK)
636 return 1;
637#endif
Yangbo Lu8abc0432020-05-19 11:06:43 +0800638 if (esdhc_read32(&regs->prsstat) & PRSSTAT_CINS)
639 return 1;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100640
Yangbo Lu8abc0432020-05-19 11:06:43 +0800641 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500642}
643
Yangbo Lub64dc8d2019-10-31 18:54:23 +0800644static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv,
645 struct mmc_config *cfg)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500646{
Yangbo Lub64dc8d2019-10-31 18:54:23 +0800647 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu63267b42019-10-31 18:54:21 +0800648 u32 caps;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500649
Wang Huanc9292132014-09-05 13:52:40 +0800650 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang39356612011-01-07 00:06:47 -0600651#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
Yangbo Lu63267b42019-10-31 18:54:21 +0800652 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
Roy Zang39356612011-01-07 00:06:47 -0600653#endif
Haijun.Zhang8a065e92013-10-31 09:38:19 +0800654#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
Yangbo Lu63267b42019-10-31 18:54:21 +0800655 caps |= HOSTCAPBLT_VS33;
Haijun.Zhang8a065e92013-10-31 09:38:19 +0800656#endif
Yangbo Lu63267b42019-10-31 18:54:21 +0800657 if (caps & HOSTCAPBLT_VS18)
658 cfg->voltages |= MMC_VDD_165_195;
659 if (caps & HOSTCAPBLT_VS30)
660 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
661 if (caps & HOSTCAPBLT_VS33)
662 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +0000663
Simon Glassfa02ca52017-07-29 11:35:21 -0600664 cfg->name = "FSL_SDHC";
Abbas Razae6bf9772013-03-25 09:13:34 +0000665
Yangbo Lu63267b42019-10-31 18:54:21 +0800666 if (caps & HOSTCAPBLT_HSS)
Simon Glassfa02ca52017-07-29 11:35:21 -0600667 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500668
Simon Glassfa02ca52017-07-29 11:35:21 -0600669 cfg->f_min = 400000;
Peng Fanc4142702018-01-21 19:00:24 +0800670 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Simon Glassfa02ca52017-07-29 11:35:21 -0600671 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Peng Fana4d36f72016-03-25 14:16:56 +0800672}
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400673
Yangbo Lub124f8a2015-04-22 13:57:00 +0800674#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
675void mmc_adapter_card_type_ident(void)
676{
677 u8 card_id;
678 u8 value;
679
680 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
681 gd->arch.sdhc_adapter = card_id;
682
683 switch (card_id) {
684 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lu81eacd62015-09-17 10:27:12 +0800685 value = QIXIS_READ(brdcfg[5]);
686 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
687 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +0800688 break;
689 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Luc6799ce2015-09-17 10:27:48 +0800690 value = QIXIS_READ(pwr_ctl[1]);
691 value |= QIXIS_EVDD_BY_SDHC_VS;
692 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +0800693 break;
694 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
695 value = QIXIS_READ(brdcfg[5]);
696 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
697 QIXIS_WRITE(brdcfg[5], value);
698 break;
699 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
700 break;
701 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
702 break;
703 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
704 break;
705 case QIXIS_ESDHC_NO_ADAPTER:
706 break;
707 default:
708 break;
709 }
710}
711#endif
712
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100713#ifdef CONFIG_OF_LIBFDT
Yangbo Lud84139c2017-01-17 10:43:54 +0800714__weak int esdhc_status_fixup(void *blob, const char *compat)
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400715{
Chenhui Zhao025eab02011-01-04 17:23:05 +0800716#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400717 if (!hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800718 do_fixup_by_compat(blob, compat, "status", "disabled",
Yangbo Lud84139c2017-01-17 10:43:54 +0800719 sizeof("disabled"), 1);
720 return 1;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400721 }
Chenhui Zhao025eab02011-01-04 17:23:05 +0800722#endif
Yangbo Lud84139c2017-01-17 10:43:54 +0800723 return 0;
724}
725
Yangbo Luce884022020-05-19 11:06:44 +0800726#ifdef CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND
727static int fsl_esdhc_get_cd(struct udevice *dev);
728
729static void esdhc_disable_for_no_card(void *blob)
730{
731 struct udevice *dev;
732
733 for (uclass_first_device(UCLASS_MMC, &dev);
734 dev;
735 uclass_next_device(&dev)) {
736 char esdhc_path[50];
737
738 if (fsl_esdhc_get_cd(dev))
739 continue;
740
741 snprintf(esdhc_path, sizeof(esdhc_path), "/soc/esdhc@%lx",
742 (unsigned long)dev_read_addr(dev));
743 do_fixup_by_path(blob, esdhc_path, "status", "disabled",
744 sizeof("disabled"), 1);
745 }
746}
747#endif
748
Yangbo Lud84139c2017-01-17 10:43:54 +0800749void fdt_fixup_esdhc(void *blob, bd_t *bd)
750{
751 const char *compat = "fsl,esdhc";
752
753 if (esdhc_status_fixup(blob, compat))
754 return;
Yangbo Luce884022020-05-19 11:06:44 +0800755#ifdef CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND
756 esdhc_disable_for_no_card(blob);
757#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400758 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000759 gd->arch.sdhc_clk, 1);
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400760}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100761#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800762
Yangbo Lu4fc93332019-10-31 18:54:26 +0800763#if !CONFIG_IS_ENABLED(DM_MMC)
764static int esdhc_getcd(struct mmc *mmc)
765{
766 struct fsl_esdhc_priv *priv = mmc->priv;
767
768 return esdhc_getcd_common(priv);
769}
770
771static int esdhc_init(struct mmc *mmc)
772{
773 struct fsl_esdhc_priv *priv = mmc->priv;
774
775 return esdhc_init_common(priv, mmc);
776}
777
778static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
779 struct mmc_data *data)
780{
781 struct fsl_esdhc_priv *priv = mmc->priv;
782
783 return esdhc_send_cmd_common(priv, mmc, cmd, data);
784}
785
786static int esdhc_set_ios(struct mmc *mmc)
787{
788 struct fsl_esdhc_priv *priv = mmc->priv;
789
790 return esdhc_set_ios_common(priv, mmc);
791}
792
793static const struct mmc_ops esdhc_ops = {
794 .getcd = esdhc_getcd,
795 .init = esdhc_init,
796 .send_cmd = esdhc_send_cmd,
797 .set_ios = esdhc_set_ios,
798};
799
800int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
801{
802 struct fsl_esdhc_plat *plat;
803 struct fsl_esdhc_priv *priv;
804 struct mmc_config *mmc_cfg;
805 struct mmc *mmc;
806
807 if (!cfg)
808 return -EINVAL;
809
810 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
811 if (!priv)
812 return -ENOMEM;
813 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
814 if (!plat) {
815 free(priv);
816 return -ENOMEM;
817 }
818
819 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
820 priv->sdhc_clk = cfg->sdhc_clk;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800821 if (gd->arch.sdhc_per_clk)
822 priv->is_sdhc_per_clk = true;
Yangbo Lu4fc93332019-10-31 18:54:26 +0800823
824 mmc_cfg = &plat->cfg;
825
826 if (cfg->max_bus_width == 8) {
827 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
828 MMC_MODE_8BIT;
829 } else if (cfg->max_bus_width == 4) {
830 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT;
831 } else if (cfg->max_bus_width == 1) {
832 mmc_cfg->host_caps |= MMC_MODE_1BIT;
833 } else {
834 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
835 MMC_MODE_8BIT;
836 printf("No max bus width provided. Assume 8-bit supported.\n");
837 }
838
839#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
840 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
841 mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
842#endif
843 mmc_cfg->ops = &esdhc_ops;
844
845 fsl_esdhc_get_cfg_common(priv, mmc_cfg);
846
847 mmc = mmc_create(mmc_cfg, priv);
848 if (!mmc)
849 return -EIO;
850
851 priv->mmc = mmc;
852 return 0;
853}
854
855int fsl_esdhc_mmc_init(bd_t *bis)
856{
857 struct fsl_esdhc_cfg *cfg;
858
859 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
860 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800861 /* Prefer peripheral clock which provides higher frequency. */
862 if (gd->arch.sdhc_per_clk)
863 cfg->sdhc_clk = gd->arch.sdhc_per_clk;
864 else
865 cfg->sdhc_clk = gd->arch.sdhc_clk;
Yangbo Lu4fc93332019-10-31 18:54:26 +0800866 return fsl_esdhc_initialize(bis, cfg);
867}
868#else /* DM_MMC */
Peng Fana4d36f72016-03-25 14:16:56 +0800869static int fsl_esdhc_probe(struct udevice *dev)
870{
871 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa02ca52017-07-29 11:35:21 -0600872 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800873 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800874 fdt_addr_t addr;
Simon Glass407025d2017-07-29 11:35:24 -0600875 struct mmc *mmc;
Yangbo Luce884022020-05-19 11:06:44 +0800876 int ret;
Peng Fana4d36f72016-03-25 14:16:56 +0800877
Simon Glass80e9df42017-07-29 11:35:23 -0600878 addr = dev_read_addr(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800879 if (addr == FDT_ADDR_T_NONE)
880 return -EINVAL;
Yinbo Zhu583d5e92019-04-11 11:01:50 +0000881#ifdef CONFIG_PPC
882 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
883#else
Peng Fana4d36f72016-03-25 14:16:56 +0800884 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yinbo Zhu583d5e92019-04-11 11:01:50 +0000885#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800886 priv->dev = dev;
887
Yangbo Lu1ca7a9f2019-12-19 18:59:30 +0800888 if (gd->arch.sdhc_per_clk) {
889 priv->sdhc_clk = gd->arch.sdhc_per_clk;
890 priv->is_sdhc_per_clk = true;
891 } else {
892 priv->sdhc_clk = gd->arch.sdhc_clk;
893 }
894
Yangbo Lub8626e42019-11-12 19:28:36 +0800895 if (priv->sdhc_clk <= 0) {
896 dev_err(dev, "Unable to get clk for %s\n", dev->name);
897 return -EINVAL;
Peng Fana4d36f72016-03-25 14:16:56 +0800898 }
899
Yangbo Lub64dc8d2019-10-31 18:54:23 +0800900 fsl_esdhc_get_cfg_common(priv, &plat->cfg);
Peng Fana4d36f72016-03-25 14:16:56 +0800901
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800902 mmc_of_parse(dev, &plat->cfg);
903
Simon Glass407025d2017-07-29 11:35:24 -0600904 mmc = &plat->mmc;
905 mmc->cfg = &plat->cfg;
906 mmc->dev = dev;
Yangbo Lu4cc119b2019-05-23 11:05:46 +0800907
Simon Glass407025d2017-07-29 11:35:24 -0600908 upriv->mmc = mmc;
Peng Fana4d36f72016-03-25 14:16:56 +0800909
Yangbo Luce884022020-05-19 11:06:44 +0800910 ret = esdhc_init_common(priv, mmc);
911 if (ret)
912 return ret;
913
914#ifdef CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND
915 if (!fsl_esdhc_get_cd(dev))
916 esdhc_setbits32(&priv->esdhc_regs->proctl, PROCTL_VOLT_SEL);
917#endif
918 return 0;
Peng Fana4d36f72016-03-25 14:16:56 +0800919}
920
Simon Glass407025d2017-07-29 11:35:24 -0600921static int fsl_esdhc_get_cd(struct udevice *dev)
922{
Yangbo Lu9fed28d2019-10-31 18:54:24 +0800923 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Simon Glass407025d2017-07-29 11:35:24 -0600924 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
925
Yangbo Lu9fed28d2019-10-31 18:54:24 +0800926 if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
927 return 1;
928
Simon Glass407025d2017-07-29 11:35:24 -0600929 return esdhc_getcd_common(priv);
930}
931
932static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
933 struct mmc_data *data)
934{
935 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
936 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
937
938 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
939}
940
941static int fsl_esdhc_set_ios(struct udevice *dev)
942{
943 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
944 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
945
946 return esdhc_set_ios_common(priv, &plat->mmc);
947}
948
949static const struct dm_mmc_ops fsl_esdhc_ops = {
950 .get_cd = fsl_esdhc_get_cd,
951 .send_cmd = fsl_esdhc_send_cmd,
952 .set_ios = fsl_esdhc_set_ios,
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800953#ifdef MMC_SUPPORTS_TUNING
954 .execute_tuning = fsl_esdhc_execute_tuning,
955#endif
Simon Glass407025d2017-07-29 11:35:24 -0600956};
Simon Glass407025d2017-07-29 11:35:24 -0600957
Peng Fana4d36f72016-03-25 14:16:56 +0800958static const struct udevice_id fsl_esdhc_ids[] = {
Yangbo Lu2a99b602016-12-07 11:54:31 +0800959 { .compatible = "fsl,esdhc", },
Peng Fana4d36f72016-03-25 14:16:56 +0800960 { /* sentinel */ }
961};
962
Simon Glass407025d2017-07-29 11:35:24 -0600963static int fsl_esdhc_bind(struct udevice *dev)
964{
965 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
966
967 return mmc_bind(dev, &plat->mmc, &plat->cfg);
968}
Simon Glass407025d2017-07-29 11:35:24 -0600969
Peng Fana4d36f72016-03-25 14:16:56 +0800970U_BOOT_DRIVER(fsl_esdhc) = {
971 .name = "fsl-esdhc-mmc",
972 .id = UCLASS_MMC,
973 .of_match = fsl_esdhc_ids,
Simon Glass407025d2017-07-29 11:35:24 -0600974 .ops = &fsl_esdhc_ops,
Simon Glass407025d2017-07-29 11:35:24 -0600975 .bind = fsl_esdhc_bind,
Peng Fana4d36f72016-03-25 14:16:56 +0800976 .probe = fsl_esdhc_probe,
Simon Glassfa02ca52017-07-29 11:35:21 -0600977 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fana4d36f72016-03-25 14:16:56 +0800978 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
979};
980#endif