blob: cd357ea4116cc23a5ead341384e507ff480f4adc [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 Lu4cc119b2019-05-23 11:05:46 +08004 * Copyright 2019 NXP Semiconductors
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>
Peng Fan4c286b72018-10-18 14:28:35 +020015#include <clk.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>
Andy Fleminge52ffb82008-10-30 16:47:16 -050023#include <asm/io.h>
Peng Fana4d36f72016-03-25 14:16:56 +080024#include <dm.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050025
Andy Fleminge52ffb82008-10-30 16:47:16 -050026DECLARE_GLOBAL_DATA_PTR;
27
Ye.Li3d46c312014-11-04 15:35:49 +080028#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
29 IRQSTATEN_CINT | \
30 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
31 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
32 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
33 IRQSTATEN_DINT)
34
Andy Fleminge52ffb82008-10-30 16:47:16 -050035struct fsl_esdhc {
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080036 uint dsaddr; /* SDMA system address register */
37 uint blkattr; /* Block attributes register */
38 uint cmdarg; /* Command argument register */
39 uint xfertyp; /* Transfer type register */
40 uint cmdrsp0; /* Command response 0 register */
41 uint cmdrsp1; /* Command response 1 register */
42 uint cmdrsp2; /* Command response 2 register */
43 uint cmdrsp3; /* Command response 3 register */
44 uint datport; /* Buffer data port register */
45 uint prsstat; /* Present state register */
46 uint proctl; /* Protocol control register */
47 uint sysctl; /* System Control Register */
48 uint irqstat; /* Interrupt status register */
49 uint irqstaten; /* Interrupt status enable register */
50 uint irqsigen; /* Interrupt signal enable register */
51 uint autoc12err; /* Auto CMD error status register */
52 uint hostcapblt; /* Host controller capabilities register */
53 uint wml; /* Watermark level register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080054 char reserved1[8]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080055 uint fevt; /* Force event register */
56 uint admaes; /* ADMA error status register */
57 uint adsaddr; /* ADMA system address register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080058 char reserved2[160];
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080059 uint hostver; /* Host controller version register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080060 char reserved3[4]; /* reserved */
Peng Fanb9b42362018-01-21 19:00:22 +080061 uint dmaerraddr; /* DMA error address register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080062 char reserved4[4]; /* reserved */
Peng Fanb9b42362018-01-21 19:00:22 +080063 uint dmaerrattr; /* DMA error attribute register */
Yangbo Lu62b56b32019-06-21 11:42:29 +080064 char reserved5[4]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080065 uint hostcapblt2; /* Host controller capabilities register 2 */
Yangbo Lu62b56b32019-06-21 11:42:29 +080066 char reserved6[756]; /* reserved */
67 uint esdhcctl; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050068};
69
Simon Glassfa02ca52017-07-29 11:35:21 -060070struct fsl_esdhc_plat {
71 struct mmc_config cfg;
72 struct mmc mmc;
73};
74
Peng Fana4d36f72016-03-25 14:16:56 +080075/**
76 * struct fsl_esdhc_priv
77 *
78 * @esdhc_regs: registers of the sdhc controller
79 * @sdhc_clk: Current clk of the sdhc controller
80 * @bus_width: bus width, 1bit, 4bit or 8bit
81 * @cfg: mmc config
82 * @mmc: mmc
83 * Following is used when Driver Model is enabled for MMC
84 * @dev: pointer for the device
85 * @non_removable: 0: removable; 1: non-removable
Peng Fan01eb1c42016-06-15 10:53:02 +080086 * @wp_enable: 1: enable checking wp; 0: no check
Peng Fana4d36f72016-03-25 14:16:56 +080087 * @cd_gpio: gpio for card detection
Peng Fan01eb1c42016-06-15 10:53:02 +080088 * @wp_gpio: gpio for write protection
Peng Fana4d36f72016-03-25 14:16:56 +080089 */
90struct fsl_esdhc_priv {
91 struct fsl_esdhc *esdhc_regs;
92 unsigned int sdhc_clk;
Peng Fan4c286b72018-10-18 14:28:35 +020093 struct clk per_clk;
Peng Fanc4142702018-01-21 19:00:24 +080094 unsigned int clock;
Peng Fana4d36f72016-03-25 14:16:56 +080095 unsigned int bus_width;
Yangbo Lu77f26322019-10-21 18:09:07 +080096#if !CONFIG_IS_ENABLED(DM_MMC)
Peng Fana4d36f72016-03-25 14:16:56 +080097 struct mmc *mmc;
Simon Glass407025d2017-07-29 11:35:24 -060098#endif
Peng Fana4d36f72016-03-25 14:16:56 +080099 struct udevice *dev;
100 int non_removable;
Peng Fan01eb1c42016-06-15 10:53:02 +0800101 int wp_enable;
Peng Fana4d36f72016-03-25 14:16:56 +0800102};
103
Andy Fleminge52ffb82008-10-30 16:47:16 -0500104/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000105static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500106{
107 uint xfertyp = 0;
108
109 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530110 xfertyp |= XFERTYP_DPSEL;
111#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
112 xfertyp |= XFERTYP_DMAEN;
113#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500114 if (data->blocks > 1) {
115 xfertyp |= XFERTYP_MSBSEL;
116 xfertyp |= XFERTYP_BCEN;
Jerry Huanged413672011-01-06 23:42:19 -0600117#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
118 xfertyp |= XFERTYP_AC12EN;
119#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500120 }
121
122 if (data->flags & MMC_DATA_READ)
123 xfertyp |= XFERTYP_DTDSEL;
124 }
125
126 if (cmd->resp_type & MMC_RSP_CRC)
127 xfertyp |= XFERTYP_CCCEN;
128 if (cmd->resp_type & MMC_RSP_OPCODE)
129 xfertyp |= XFERTYP_CICEN;
130 if (cmd->resp_type & MMC_RSP_136)
131 xfertyp |= XFERTYP_RSPTYP_136;
132 else if (cmd->resp_type & MMC_RSP_BUSY)
133 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
134 else if (cmd->resp_type & MMC_RSP_PRESENT)
135 xfertyp |= XFERTYP_RSPTYP_48;
136
Jason Liubef0ff02011-03-22 01:32:31 +0000137 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
138 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lub73a3d62016-01-21 17:33:19 +0800139
Andy Fleminge52ffb82008-10-30 16:47:16 -0500140 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
141}
142
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530143#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
144/*
145 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
146 */
Simon Glass1d177d42017-07-29 11:35:17 -0600147static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
148 struct mmc_data *data)
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530149{
Peng Fana4d36f72016-03-25 14:16:56 +0800150 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530151 uint blocks;
152 char *buffer;
153 uint databuf;
154 uint size;
155 uint irqstat;
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100156 ulong start;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530157
158 if (data->flags & MMC_DATA_READ) {
159 blocks = data->blocks;
160 buffer = data->dest;
161 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100162 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530163 size = data->blocksize;
164 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100165 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
166 if (get_timer(start) > PIO_TIMEOUT) {
167 printf("\nData Read Failed in PIO Mode.");
168 return;
169 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530170 }
171 while (size && (!(irqstat & IRQSTAT_TC))) {
172 udelay(100); /* Wait before last byte transfer complete */
173 irqstat = esdhc_read32(&regs->irqstat);
174 databuf = in_le32(&regs->datport);
175 *((uint *)buffer) = databuf;
176 buffer += 4;
177 size -= 4;
178 }
179 blocks--;
180 }
181 } else {
182 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200183 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530184 while (blocks) {
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100185 start = get_timer(0);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530186 size = data->blocksize;
187 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeau2a7b6f52017-10-29 22:08:58 +0100188 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
189 if (get_timer(start) > PIO_TIMEOUT) {
190 printf("\nData Write Failed in PIO Mode.");
191 return;
192 }
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530193 }
194 while (size && (!(irqstat & IRQSTAT_TC))) {
195 udelay(100); /* Wait before last byte transfer complete */
196 databuf = *((uint *)buffer);
197 buffer += 4;
198 size -= 4;
199 irqstat = esdhc_read32(&regs->irqstat);
200 out_le32(&regs->datport, databuf);
201 }
202 blocks--;
203 }
204 }
205}
206#endif
207
Simon Glass1d177d42017-07-29 11:35:17 -0600208static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
209 struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500210{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500211 int timeout;
Peng Fana4d36f72016-03-25 14:16:56 +0800212 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu62b56b32019-06-21 11:42:29 +0800213#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700214 dma_addr_t addr;
215#endif
Wolfgang Denka40545c2010-05-09 23:52:59 +0200216 uint wml_value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500217
218 wml_value = data->blocksize/4;
219
220 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530221 if (wml_value > WML_RD_WML_MAX)
222 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500223
Roy Zange5853af2010-02-09 18:23:33 +0800224 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li33a56b12014-02-20 18:00:57 +0800225#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu62b56b32019-06-21 11:42:29 +0800226#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700227 addr = virt_to_phys((void *)(data->dest));
228 if (upper_32_bits(addr))
229 printf("Error found for upper 32 bits\n");
230 else
231 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
232#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100233 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li33a56b12014-02-20 18:00:57 +0800234#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700235#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500236 } else {
Ye.Li33a56b12014-02-20 18:00:57 +0800237#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000238 flush_dcache_range((ulong)data->src,
239 (ulong)data->src+data->blocks
240 *data->blocksize);
Ye.Li33a56b12014-02-20 18:00:57 +0800241#endif
Priyanka Jain02449632011-02-09 09:24:10 +0530242 if (wml_value > WML_WR_WML_MAX)
243 wml_value = WML_WR_WML_MAX_VAL;
Peng Fan01eb1c42016-06-15 10:53:02 +0800244 if (priv->wp_enable) {
245 if ((esdhc_read32(&regs->prsstat) &
246 PRSSTAT_WPSPL) == 0) {
247 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900248 return -ETIMEDOUT;
Peng Fan01eb1c42016-06-15 10:53:02 +0800249 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500250 }
Roy Zange5853af2010-02-09 18:23:33 +0800251
252 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
253 wml_value << 16);
Ye.Li33a56b12014-02-20 18:00:57 +0800254#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu62b56b32019-06-21 11:42:29 +0800255#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700256 addr = virt_to_phys((void *)(data->src));
257 if (upper_32_bits(addr))
258 printf("Error found for upper 32 bits\n");
259 else
260 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
261#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100262 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li33a56b12014-02-20 18:00:57 +0800263#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700264#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500265 }
266
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100267 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500268
269 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530270 /*
271 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
272 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
273 * So, Number of SD Clock cycles for 0.25sec should be minimum
274 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500275 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530276 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500277 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530278 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500279 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530280 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500281 * => timeout + 13 = log2(mmc->clock/4) + 1
282 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800283 *
284 * However, the MMC spec "It is strongly recommended for hosts to
285 * implement more than 500ms timeout value even if the card
286 * indicates the 250ms maximum busy length." Even the previous
287 * value of 300ms is known to be insufficient for some cards.
288 * So, we use
289 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530290 */
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800291 timeout = fls(mmc->clock/2);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500292 timeout -= 13;
293
294 if (timeout > 14)
295 timeout = 14;
296
297 if (timeout < 0)
298 timeout = 0;
299
Kumar Gala9a878d52011-01-29 15:36:10 -0600300#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
301 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
302 timeout++;
303#endif
304
Haijun.Zhangedeb83a2014-03-18 17:04:23 +0800305#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
306 timeout = 0xE;
307#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100308 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500309
310 return 0;
311}
312
Eric Nelson30e9cad2012-04-25 14:28:48 +0000313static void check_and_invalidate_dcache_range
314 (struct mmc_cmd *cmd,
315 struct mmc_data *data) {
Yangbo Lud0e295d2015-03-20 19:28:31 -0700316 unsigned start = 0;
Yangbo Lue7702c62016-05-12 19:12:58 +0800317 unsigned end = 0;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000318 unsigned size = roundup(ARCH_DMA_MINALIGN,
319 data->blocks*data->blocksize);
Yangbo Lu62b56b32019-06-21 11:42:29 +0800320#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700321 dma_addr_t addr;
322
323 addr = virt_to_phys((void *)(data->dest));
324 if (upper_32_bits(addr))
325 printf("Error found for upper 32 bits\n");
326 else
327 start = lower_32_bits(addr);
Yangbo Lue7702c62016-05-12 19:12:58 +0800328#else
329 start = (unsigned)data->dest;
Yangbo Lud0e295d2015-03-20 19:28:31 -0700330#endif
Yangbo Lue7702c62016-05-12 19:12:58 +0800331 end = start + size;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000332 invalidate_dcache_range(start, end);
333}
Angelo Dureghello520a6692019-01-19 10:40:38 +0100334
Andy Fleminge52ffb82008-10-30 16:47:16 -0500335/*
336 * Sends a command out on the bus. Takes the mmc pointer,
337 * a command pointer, and an optional data pointer.
338 */
Simon Glass6aa55dc2017-07-29 11:35:18 -0600339static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
340 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500341{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500342 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500343 uint xfertyp;
344 uint irqstat;
Peng Fanc4142702018-01-21 19:00:24 +0800345 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fana4d36f72016-03-25 14:16:56 +0800346 struct fsl_esdhc *regs = priv->esdhc_regs;
Fabio Estevam7300ef52018-11-19 10:31:53 -0200347 unsigned long start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500348
Jerry Huanged413672011-01-06 23:42:19 -0600349#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
350 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
351 return 0;
352#endif
353
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100354 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500355
356 sync();
357
358 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100359 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
360 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
361 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500362
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100363 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
364 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500365
366 /* Wait at least 8 SD clock cycles before the next command */
367 /*
368 * Note: This is way more than 8 cycles, but 1ms seems to
369 * resolve timing issues with some cards
370 */
371 udelay(1000);
372
373 /* Set up for a data transfer if we have one */
374 if (data) {
Simon Glass1d177d42017-07-29 11:35:17 -0600375 err = esdhc_setup_data(priv, mmc, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500376 if(err)
377 return err;
Peng Fan9cb5e992015-06-25 10:32:26 +0800378
379 if (data->flags & MMC_DATA_READ)
380 check_and_invalidate_dcache_range(cmd, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500381 }
382
383 /* Figure out the transfer arguments */
384 xfertyp = esdhc_xfertyp(cmd, data);
385
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500386 /* Mask all irqs */
387 esdhc_write32(&regs->irqsigen, 0);
388
Andy Fleminge52ffb82008-10-30 16:47:16 -0500389 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100390 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
391 esdhc_write32(&regs->xfertyp, xfertyp);
Dirk Behmed8552d62012-03-26 03:13:05 +0000392
Andy Fleminge52ffb82008-10-30 16:47:16 -0500393 /* Wait for the command to complete */
Fabio Estevam7300ef52018-11-19 10:31:53 -0200394 start = get_timer(0);
395 while (!(esdhc_read32(&regs->irqstat) & flags)) {
396 if (get_timer(start) > 1000) {
397 err = -ETIMEDOUT;
398 goto out;
399 }
400 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500401
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100402 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500403
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500404 if (irqstat & CMD_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900405 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500406 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000407 }
408
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500409 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900410 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500411 goto out;
412 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500413
Dirk Behmed8552d62012-03-26 03:13:05 +0000414 /* Workaround for ESDHC errata ENGcm03648 */
415 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800416 int timeout = 6000;
Dirk Behmed8552d62012-03-26 03:13:05 +0000417
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800418 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behmed8552d62012-03-26 03:13:05 +0000419 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
420 PRSSTAT_DAT0)) {
421 udelay(100);
422 timeout--;
423 }
424
425 if (timeout <= 0) {
426 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900427 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500428 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000429 }
430 }
431
Andy Fleminge52ffb82008-10-30 16:47:16 -0500432 /* Copy the response to the response buffer */
433 if (cmd->resp_type & MMC_RSP_136) {
434 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
435
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100436 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
437 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
438 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
439 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530440 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
441 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
442 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
443 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500444 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100445 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500446
447 /* Wait until all of the blocks are transferred */
448 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530449#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass1d177d42017-07-29 11:35:17 -0600450 esdhc_pio_read_write(priv, data);
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530451#else
Andy Fleminge52ffb82008-10-30 16:47:16 -0500452 do {
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100453 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500454
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500455 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900456 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500457 goto out;
458 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000459
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500460 if (irqstat & DATA_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900461 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500462 goto out;
463 }
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800464 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li33a56b12014-02-20 18:00:57 +0800465
Peng Fan9cb5e992015-06-25 10:32:26 +0800466 /*
467 * Need invalidate the dcache here again to avoid any
468 * cache-fill during the DMA operations such as the
469 * speculative pre-fetching etc.
470 */
Angelo Dureghello520a6692019-01-19 10:40:38 +0100471 if (data->flags & MMC_DATA_READ) {
Eric Nelson70e68692013-04-03 12:31:56 +0000472 check_and_invalidate_dcache_range(cmd, data);
Angelo Dureghello520a6692019-01-19 10:40:38 +0100473 }
Ye.Li33a56b12014-02-20 18:00:57 +0800474#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500475 }
476
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500477out:
478 /* Reset CMD and DATA portions on error */
479 if (err) {
480 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
481 SYSCTL_RSTC);
482 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
483 ;
484
485 if (data) {
486 esdhc_write32(&regs->sysctl,
487 esdhc_read32(&regs->sysctl) |
488 SYSCTL_RSTD);
489 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
490 ;
491 }
492 }
493
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100494 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500495
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500496 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500497}
498
Simon Glass1d177d42017-07-29 11:35:17 -0600499static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500500{
Benoît Thébaudeau22464e02018-01-16 22:44:18 +0100501 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200502 int div = 1;
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200503 int pre_div = 2;
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800504 unsigned int sdhc_clk = priv->sdhc_clk;
505 u32 time_out;
506 u32 value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500507 uint clk;
508
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200509 if (clock < mmc->cfg->f_min)
510 clock = mmc->cfg->f_min;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100511
Yangbo Lu4ee9b862019-10-21 18:09:09 +0800512 while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256)
Lukasz Majewski2a521832019-05-07 17:47:28 +0200513 pre_div *= 2;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500514
Yangbo Lu4ee9b862019-10-21 18:09:09 +0800515 while (sdhc_clk / (div * pre_div) > clock && div < 16)
Lukasz Majewski2a521832019-05-07 17:47:28 +0200516 div++;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500517
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200518 pre_div >>= 1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500519 div -= 1;
520
521 clk = (pre_div << 8) | (div << 4);
522
Kumar Gala09876a32010-03-18 15:51:05 -0500523 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100524
525 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500526
Yinbo Zhu101d3ef2019-07-16 15:09:11 +0800527 time_out = 20;
528 value = PRSSTAT_SDSTB;
529 while (!(esdhc_read32(&regs->prsstat) & value)) {
530 if (time_out == 0) {
531 printf("fsl_esdhc: Internal clock never stabilised.\n");
532 break;
533 }
534 time_out--;
535 mdelay(1);
536 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500537
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700538 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500539}
540
Yangbo Lu163beec2015-04-22 13:57:40 +0800541#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
Simon Glass1d177d42017-07-29 11:35:17 -0600542static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu163beec2015-04-22 13:57:40 +0800543{
Peng Fana4d36f72016-03-25 14:16:56 +0800544 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu163beec2015-04-22 13:57:40 +0800545 u32 value;
546 u32 time_out;
547
548 value = esdhc_read32(&regs->sysctl);
549
550 if (enable)
551 value |= SYSCTL_CKEN;
552 else
553 value &= ~SYSCTL_CKEN;
554
555 esdhc_write32(&regs->sysctl, value);
556
557 time_out = 20;
558 value = PRSSTAT_SDSTB;
559 while (!(esdhc_read32(&regs->prsstat) & value)) {
560 if (time_out == 0) {
561 printf("fsl_esdhc: Internal clock never stabilised.\n");
562 break;
563 }
564 time_out--;
565 mdelay(1);
566 }
Peng Fanc4142702018-01-21 19:00:24 +0800567}
568#endif
Yangbo Lu163beec2015-04-22 13:57:40 +0800569
Simon Glass6aa55dc2017-07-29 11:35:18 -0600570static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500571{
Peng Fana4d36f72016-03-25 14:16:56 +0800572 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500573
Yangbo Lu163beec2015-04-22 13:57:40 +0800574#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
575 /* Select to use peripheral clock */
Simon Glass1d177d42017-07-29 11:35:17 -0600576 esdhc_clock_control(priv, false);
Yangbo Lu62b56b32019-06-21 11:42:29 +0800577 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_PCS);
Simon Glass1d177d42017-07-29 11:35:17 -0600578 esdhc_clock_control(priv, true);
Yangbo Lu163beec2015-04-22 13:57:40 +0800579#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500580 /* Set the clock speed */
Peng Fanc4142702018-01-21 19:00:24 +0800581 if (priv->clock != mmc->clock)
582 set_sysctl(priv, mmc, mmc->clock);
583
Andy Fleminge52ffb82008-10-30 16:47:16 -0500584 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100585 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500586
587 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100588 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500589 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100590 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
591
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900592 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500593}
594
Simon Glass6aa55dc2017-07-29 11:35:18 -0600595static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500596{
Peng Fana4d36f72016-03-25 14:16:56 +0800597 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass0c3ef222017-07-29 11:35:20 -0600598 ulong start;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500599
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100600 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200601 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100602
603 /* Wait until the controller is available */
Simon Glass0c3ef222017-07-29 11:35:20 -0600604 start = get_timer(0);
605 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
606 if (get_timer(start) > 1000)
607 return -ETIMEDOUT;
608 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500609
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530610 /* Enable cache snooping */
Yangbo Lu62b56b32019-06-21 11:42:29 +0800611 esdhc_write32(&regs->esdhcctl, 0x00000040);
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;
Thierry Reding8cee4c982012-01-02 01:15:38 +0000633 int timeout = 1000;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500634
Haijun.Zhang05f58542014-01-10 13:52:17 +0800635#ifdef CONFIG_ESDHC_DETECT_QUIRK
636 if (CONFIG_ESDHC_DETECT_QUIRK)
637 return 1;
638#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800639
Simon Glass407025d2017-07-29 11:35:24 -0600640#if CONFIG_IS_ENABLED(DM_MMC)
Peng Fana4d36f72016-03-25 14:16:56 +0800641 if (priv->non_removable)
642 return 1;
Yangbo Lub99647c2016-12-07 11:54:30 +0800643#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800644
Thierry Reding8cee4c982012-01-02 01:15:38 +0000645 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
646 udelay(1000);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100647
Thierry Reding8cee4c982012-01-02 01:15:38 +0000648 return timeout > 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500649}
650
Simon Glass81357b52017-07-29 11:35:19 -0600651static int esdhc_reset(struct fsl_esdhc *regs)
Jerry Huangb7ef7562010-03-18 15:57:06 -0500652{
Simon Glass81357b52017-07-29 11:35:19 -0600653 ulong start;
Jerry Huangb7ef7562010-03-18 15:57:06 -0500654
655 /* reset the controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200656 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huangb7ef7562010-03-18 15:57:06 -0500657
658 /* hardware clears the bit when it is done */
Simon Glass81357b52017-07-29 11:35:19 -0600659 start = get_timer(0);
660 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
661 if (get_timer(start) > 100) {
662 printf("MMC/SD: Reset never completed.\n");
663 return -ETIMEDOUT;
664 }
665 }
666
667 return 0;
Jerry Huangb7ef7562010-03-18 15:57:06 -0500668}
669
Simon Glasseba48f92017-07-29 11:35:31 -0600670#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass6aa55dc2017-07-29 11:35:18 -0600671static int esdhc_getcd(struct mmc *mmc)
672{
673 struct fsl_esdhc_priv *priv = mmc->priv;
674
675 return esdhc_getcd_common(priv);
676}
677
678static int esdhc_init(struct mmc *mmc)
679{
680 struct fsl_esdhc_priv *priv = mmc->priv;
681
682 return esdhc_init_common(priv, mmc);
683}
684
685static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
686 struct mmc_data *data)
687{
688 struct fsl_esdhc_priv *priv = mmc->priv;
689
690 return esdhc_send_cmd_common(priv, mmc, cmd, data);
691}
692
693static int esdhc_set_ios(struct mmc *mmc)
694{
695 struct fsl_esdhc_priv *priv = mmc->priv;
696
697 return esdhc_set_ios_common(priv, mmc);
698}
699
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200700static const struct mmc_ops esdhc_ops = {
Simon Glass6aa55dc2017-07-29 11:35:18 -0600701 .getcd = esdhc_getcd,
702 .init = esdhc_init,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200703 .send_cmd = esdhc_send_cmd,
704 .set_ios = esdhc_set_ios,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200705};
Simon Glass407025d2017-07-29 11:35:24 -0600706#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200707
Simon Glassfa02ca52017-07-29 11:35:21 -0600708static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
709 struct fsl_esdhc_plat *plat)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500710{
Simon Glassfa02ca52017-07-29 11:35:21 -0600711 struct mmc_config *cfg;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100712 struct fsl_esdhc *regs;
Li Yangd4933f22010-11-25 17:06:09 +0000713 u32 caps, voltage_caps;
Simon Glass81357b52017-07-29 11:35:19 -0600714 int ret;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500715
Peng Fana4d36f72016-03-25 14:16:56 +0800716 if (!priv)
717 return -EINVAL;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100718
Peng Fana4d36f72016-03-25 14:16:56 +0800719 regs = priv->esdhc_regs;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100720
Jerry Huangb7ef7562010-03-18 15:57:06 -0500721 /* First reset the eSDHC controller */
Simon Glass81357b52017-07-29 11:35:19 -0600722 ret = esdhc_reset(regs);
723 if (ret)
724 return ret;
Jerry Huangb7ef7562010-03-18 15:57:06 -0500725
Yangbo Lu62b56b32019-06-21 11:42:29 +0800726 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
727 SYSCTL_IPGEN | SYSCTL_CKEN);
Peng Fanaee78582017-06-12 17:50:53 +0800728
Ye.Li3d46c312014-11-04 15:35:49 +0800729 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Simon Glassfa02ca52017-07-29 11:35:21 -0600730 cfg = &plat->cfg;
Simon Glass407025d2017-07-29 11:35:24 -0600731#ifndef CONFIG_DM_MMC
Simon Glassfa02ca52017-07-29 11:35:21 -0600732 memset(cfg, '\0', sizeof(*cfg));
Simon Glass407025d2017-07-29 11:35:24 -0600733#endif
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200734
Li Yangd4933f22010-11-25 17:06:09 +0000735 voltage_caps = 0;
Wang Huanc9292132014-09-05 13:52:40 +0800736 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang39356612011-01-07 00:06:47 -0600737
738#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
739 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
740 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
741#endif
Haijun.Zhang8a065e92013-10-31 09:38:19 +0800742
743/* T4240 host controller capabilities register should have VS33 bit */
744#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
745 caps = caps | ESDHC_HOSTCAPBLT_VS33;
746#endif
747
Andy Fleminge52ffb82008-10-30 16:47:16 -0500748 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yangd4933f22010-11-25 17:06:09 +0000749 voltage_caps |= MMC_VDD_165_195;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500750 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yangd4933f22010-11-25 17:06:09 +0000751 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500752 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yangd4933f22010-11-25 17:06:09 +0000753 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
754
Simon Glassfa02ca52017-07-29 11:35:21 -0600755 cfg->name = "FSL_SDHC";
Simon Glasseba48f92017-07-29 11:35:31 -0600756#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glassfa02ca52017-07-29 11:35:21 -0600757 cfg->ops = &esdhc_ops;
Simon Glass407025d2017-07-29 11:35:24 -0600758#endif
Li Yangd4933f22010-11-25 17:06:09 +0000759#ifdef CONFIG_SYS_SD_VOLTAGE
Simon Glassfa02ca52017-07-29 11:35:21 -0600760 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yangd4933f22010-11-25 17:06:09 +0000761#else
Simon Glassfa02ca52017-07-29 11:35:21 -0600762 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +0000763#endif
Simon Glassfa02ca52017-07-29 11:35:21 -0600764 if ((cfg->voltages & voltage_caps) == 0) {
Li Yangd4933f22010-11-25 17:06:09 +0000765 printf("voltage not supported by controller\n");
766 return -1;
767 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500768
Peng Fana4d36f72016-03-25 14:16:56 +0800769 if (priv->bus_width == 8)
Simon Glassfa02ca52017-07-29 11:35:21 -0600770 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Peng Fana4d36f72016-03-25 14:16:56 +0800771 else if (priv->bus_width == 4)
Simon Glassfa02ca52017-07-29 11:35:21 -0600772 cfg->host_caps = MMC_MODE_4BIT;
Peng Fana4d36f72016-03-25 14:16:56 +0800773
Simon Glassfa02ca52017-07-29 11:35:21 -0600774 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500775
Peng Fana4d36f72016-03-25 14:16:56 +0800776 if (priv->bus_width > 0) {
777 if (priv->bus_width < 8)
Simon Glassfa02ca52017-07-29 11:35:21 -0600778 cfg->host_caps &= ~MMC_MODE_8BIT;
Peng Fana4d36f72016-03-25 14:16:56 +0800779 if (priv->bus_width < 4)
Simon Glassfa02ca52017-07-29 11:35:21 -0600780 cfg->host_caps &= ~MMC_MODE_4BIT;
Abbas Razae6bf9772013-03-25 09:13:34 +0000781 }
782
Andy Fleminge52ffb82008-10-30 16:47:16 -0500783 if (caps & ESDHC_HOSTCAPBLT_HSS)
Simon Glassfa02ca52017-07-29 11:35:21 -0600784 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500785
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800786#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
787 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Simon Glassfa02ca52017-07-29 11:35:21 -0600788 cfg->host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800789#endif
790
Simon Glassfa02ca52017-07-29 11:35:21 -0600791 cfg->f_min = 400000;
Peng Fanc4142702018-01-21 19:00:24 +0800792 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500793
Simon Glassfa02ca52017-07-29 11:35:21 -0600794 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200795
Peng Fana4d36f72016-03-25 14:16:56 +0800796 return 0;
797}
798
Simon Glassb9876e22017-07-29 11:35:28 -0600799#if !CONFIG_IS_ENABLED(DM_MMC)
Jagan Teki3c2cc6d2017-05-12 17:18:20 +0530800static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
801 struct fsl_esdhc_priv *priv)
802{
803 if (!cfg || !priv)
804 return -EINVAL;
805
806 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
807 priv->bus_width = cfg->max_bus_width;
808 priv->sdhc_clk = cfg->sdhc_clk;
809 priv->wp_enable = cfg->wp_enable;
810
811 return 0;
812};
813
Peng Fana4d36f72016-03-25 14:16:56 +0800814int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
815{
Simon Glassfa02ca52017-07-29 11:35:21 -0600816 struct fsl_esdhc_plat *plat;
Peng Fana4d36f72016-03-25 14:16:56 +0800817 struct fsl_esdhc_priv *priv;
Simon Glass5ee39802017-07-29 11:35:22 -0600818 struct mmc *mmc;
Peng Fana4d36f72016-03-25 14:16:56 +0800819 int ret;
820
821 if (!cfg)
822 return -EINVAL;
823
824 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
825 if (!priv)
826 return -ENOMEM;
Simon Glassfa02ca52017-07-29 11:35:21 -0600827 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
828 if (!plat) {
829 free(priv);
830 return -ENOMEM;
831 }
Peng Fana4d36f72016-03-25 14:16:56 +0800832
833 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
834 if (ret) {
835 debug("%s xlate failure\n", __func__);
Simon Glassfa02ca52017-07-29 11:35:21 -0600836 free(plat);
Peng Fana4d36f72016-03-25 14:16:56 +0800837 free(priv);
838 return ret;
839 }
840
Simon Glassfa02ca52017-07-29 11:35:21 -0600841 ret = fsl_esdhc_init(priv, plat);
Peng Fana4d36f72016-03-25 14:16:56 +0800842 if (ret) {
843 debug("%s init failure\n", __func__);
Simon Glassfa02ca52017-07-29 11:35:21 -0600844 free(plat);
Peng Fana4d36f72016-03-25 14:16:56 +0800845 free(priv);
846 return ret;
847 }
848
Simon Glass5ee39802017-07-29 11:35:22 -0600849 mmc = mmc_create(&plat->cfg, priv);
850 if (!mmc)
851 return -EIO;
852
853 priv->mmc = mmc;
854
Andy Fleminge52ffb82008-10-30 16:47:16 -0500855 return 0;
856}
857
858int fsl_esdhc_mmc_init(bd_t *bis)
859{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100860 struct fsl_esdhc_cfg *cfg;
861
Fabio Estevam6592a992012-12-27 08:51:08 +0000862 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100863 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glass9e247d12012-12-13 20:49:05 +0000864 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100865 return fsl_esdhc_initialize(bis, cfg);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500866}
Jagan Teki3c2cc6d2017-05-12 17:18:20 +0530867#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400868
Yangbo Lub124f8a2015-04-22 13:57:00 +0800869#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
870void mmc_adapter_card_type_ident(void)
871{
872 u8 card_id;
873 u8 value;
874
875 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
876 gd->arch.sdhc_adapter = card_id;
877
878 switch (card_id) {
879 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lu81eacd62015-09-17 10:27:12 +0800880 value = QIXIS_READ(brdcfg[5]);
881 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
882 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +0800883 break;
884 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Luc6799ce2015-09-17 10:27:48 +0800885 value = QIXIS_READ(pwr_ctl[1]);
886 value |= QIXIS_EVDD_BY_SDHC_VS;
887 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +0800888 break;
889 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
890 value = QIXIS_READ(brdcfg[5]);
891 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
892 QIXIS_WRITE(brdcfg[5], value);
893 break;
894 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
895 break;
896 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
897 break;
898 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
899 break;
900 case QIXIS_ESDHC_NO_ADAPTER:
901 break;
902 default:
903 break;
904 }
905}
906#endif
907
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100908#ifdef CONFIG_OF_LIBFDT
Yangbo Lud84139c2017-01-17 10:43:54 +0800909__weak int esdhc_status_fixup(void *blob, const char *compat)
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400910{
Chenhui Zhao025eab02011-01-04 17:23:05 +0800911#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400912 if (!hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800913 do_fixup_by_compat(blob, compat, "status", "disabled",
Yangbo Lud84139c2017-01-17 10:43:54 +0800914 sizeof("disabled"), 1);
915 return 1;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400916 }
Chenhui Zhao025eab02011-01-04 17:23:05 +0800917#endif
Yangbo Lud84139c2017-01-17 10:43:54 +0800918 return 0;
919}
920
921void fdt_fixup_esdhc(void *blob, bd_t *bd)
922{
923 const char *compat = "fsl,esdhc";
924
925 if (esdhc_status_fixup(blob, compat))
926 return;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400927
Yangbo Lu163beec2015-04-22 13:57:40 +0800928#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
929 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
930 gd->arch.sdhc_clk, 1);
931#else
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400932 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000933 gd->arch.sdhc_clk, 1);
Yangbo Lu163beec2015-04-22 13:57:40 +0800934#endif
Yangbo Lub124f8a2015-04-22 13:57:00 +0800935#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
936 do_fixup_by_compat_u32(blob, compat, "adapter-type",
937 (u32)(gd->arch.sdhc_adapter), 1);
938#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400939}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100940#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800941
Simon Glass407025d2017-07-29 11:35:24 -0600942#if CONFIG_IS_ENABLED(DM_MMC)
Yinbo Zhu4bc86012019-04-11 11:01:46 +0000943#ifndef CONFIG_PPC
Peng Fana4d36f72016-03-25 14:16:56 +0800944#include <asm/arch/clock.h>
Yinbo Zhu4bc86012019-04-11 11:01:46 +0000945#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800946static int fsl_esdhc_probe(struct udevice *dev)
947{
948 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa02ca52017-07-29 11:35:21 -0600949 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800950 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800951 fdt_addr_t addr;
952 unsigned int val;
Simon Glass407025d2017-07-29 11:35:24 -0600953 struct mmc *mmc;
Peng Fana4d36f72016-03-25 14:16:56 +0800954 int ret;
955
Simon Glass80e9df42017-07-29 11:35:23 -0600956 addr = dev_read_addr(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800957 if (addr == FDT_ADDR_T_NONE)
958 return -EINVAL;
Yinbo Zhu583d5e92019-04-11 11:01:50 +0000959#ifdef CONFIG_PPC
960 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
961#else
Peng Fana4d36f72016-03-25 14:16:56 +0800962 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yinbo Zhu583d5e92019-04-11 11:01:50 +0000963#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800964 priv->dev = dev;
965
Simon Glass80e9df42017-07-29 11:35:23 -0600966 val = dev_read_u32_default(dev, "bus-width", -1);
Peng Fana4d36f72016-03-25 14:16:56 +0800967 if (val == 8)
968 priv->bus_width = 8;
969 else if (val == 4)
970 priv->bus_width = 4;
971 else
972 priv->bus_width = 1;
973
Simon Glass80e9df42017-07-29 11:35:23 -0600974 if (dev_read_bool(dev, "non-removable")) {
Peng Fana4d36f72016-03-25 14:16:56 +0800975 priv->non_removable = 1;
976 } else {
977 priv->non_removable = 0;
Peng Fan5eb8b432017-06-12 17:50:54 +0800978 }
Peng Fan5eb8b432017-06-12 17:50:54 +0800979
Yangbo Lu62b56b32019-06-21 11:42:29 +0800980 priv->wp_enable = 1;
Peng Fanaf6dbc02017-02-22 16:21:55 +0800981
Peng Fan4c286b72018-10-18 14:28:35 +0200982 if (IS_ENABLED(CONFIG_CLK)) {
983 /* Assigned clock already set clock */
984 ret = clk_get_by_name(dev, "per", &priv->per_clk);
985 if (ret) {
986 printf("Failed to get per_clk\n");
987 return ret;
988 }
989 ret = clk_enable(&priv->per_clk);
990 if (ret) {
991 printf("Failed to enable per_clk\n");
992 return ret;
993 }
994
995 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
996 } else {
Yinbo Zhu4bc86012019-04-11 11:01:46 +0000997#ifndef CONFIG_PPC
Peng Fan4c286b72018-10-18 14:28:35 +0200998 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
Yinbo Zhu4bc86012019-04-11 11:01:46 +0000999#else
1000 priv->sdhc_clk = gd->arch.sdhc_clk;
1001#endif
Peng Fan4c286b72018-10-18 14:28:35 +02001002 if (priv->sdhc_clk <= 0) {
1003 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1004 return -EINVAL;
1005 }
Peng Fana4d36f72016-03-25 14:16:56 +08001006 }
1007
Simon Glassfa02ca52017-07-29 11:35:21 -06001008 ret = fsl_esdhc_init(priv, plat);
Peng Fana4d36f72016-03-25 14:16:56 +08001009 if (ret) {
1010 dev_err(dev, "fsl_esdhc_init failure\n");
1011 return ret;
1012 }
1013
Yinbo Zhu101d3ef2019-07-16 15:09:11 +08001014 mmc_of_parse(dev, &plat->cfg);
1015
Simon Glass407025d2017-07-29 11:35:24 -06001016 mmc = &plat->mmc;
1017 mmc->cfg = &plat->cfg;
1018 mmc->dev = dev;
Yangbo Lu4cc119b2019-05-23 11:05:46 +08001019
Simon Glass407025d2017-07-29 11:35:24 -06001020 upriv->mmc = mmc;
Peng Fana4d36f72016-03-25 14:16:56 +08001021
Simon Glass407025d2017-07-29 11:35:24 -06001022 return esdhc_init_common(priv, mmc);
Peng Fana4d36f72016-03-25 14:16:56 +08001023}
1024
Simon Glass407025d2017-07-29 11:35:24 -06001025static int fsl_esdhc_get_cd(struct udevice *dev)
1026{
1027 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1028
Simon Glass407025d2017-07-29 11:35:24 -06001029 return esdhc_getcd_common(priv);
1030}
1031
1032static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1033 struct mmc_data *data)
1034{
1035 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1036 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1037
1038 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1039}
1040
1041static int fsl_esdhc_set_ios(struct udevice *dev)
1042{
1043 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1044 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1045
1046 return esdhc_set_ios_common(priv, &plat->mmc);
1047}
1048
1049static const struct dm_mmc_ops fsl_esdhc_ops = {
1050 .get_cd = fsl_esdhc_get_cd,
1051 .send_cmd = fsl_esdhc_send_cmd,
1052 .set_ios = fsl_esdhc_set_ios,
Yinbo Zhu101d3ef2019-07-16 15:09:11 +08001053#ifdef MMC_SUPPORTS_TUNING
1054 .execute_tuning = fsl_esdhc_execute_tuning,
1055#endif
Simon Glass407025d2017-07-29 11:35:24 -06001056};
Simon Glass407025d2017-07-29 11:35:24 -06001057
Peng Fana4d36f72016-03-25 14:16:56 +08001058static const struct udevice_id fsl_esdhc_ids[] = {
Yangbo Lu2a99b602016-12-07 11:54:31 +08001059 { .compatible = "fsl,esdhc", },
Peng Fana4d36f72016-03-25 14:16:56 +08001060 { /* sentinel */ }
1061};
1062
Simon Glass407025d2017-07-29 11:35:24 -06001063static int fsl_esdhc_bind(struct udevice *dev)
1064{
1065 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1066
1067 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1068}
Simon Glass407025d2017-07-29 11:35:24 -06001069
Peng Fana4d36f72016-03-25 14:16:56 +08001070U_BOOT_DRIVER(fsl_esdhc) = {
1071 .name = "fsl-esdhc-mmc",
1072 .id = UCLASS_MMC,
1073 .of_match = fsl_esdhc_ids,
Simon Glass407025d2017-07-29 11:35:24 -06001074 .ops = &fsl_esdhc_ops,
Simon Glass407025d2017-07-29 11:35:24 -06001075 .bind = fsl_esdhc_bind,
Peng Fana4d36f72016-03-25 14:16:56 +08001076 .probe = fsl_esdhc_probe,
Simon Glassfa02ca52017-07-29 11:35:21 -06001077 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fana4d36f72016-03-25 14:16:56 +08001078 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1079};
1080#endif