blob: 07318472a7e677d39aaf994b4d47f3b668310237 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Fleming50586ef2008-10-30 16:47:16 -05002/*
Jerry Huangd621da02011-01-06 23:42:19 -06003 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Yangbo Lu66fa0352019-05-23 11:05:46 +08004 * Copyright 2019 NXP Semiconductors
Andy Fleming50586ef2008-10-30 16:47:16 -05005 * Andy Fleming
6 *
7 * Based vaguely on the pxa mmc code:
8 * (C) Copyright 2003
9 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
Andy Fleming50586ef2008-10-30 16:47:16 -050010 */
11
12#include <config.h>
13#include <common.h>
14#include <command.h>
Peng Fan3cb14502018-10-18 14:28:35 +020015#include <clk.h>
Jaehoon Chung915ffa52016-07-19 16:33:36 +090016#include <errno.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040017#include <hwconfig.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050018#include <mmc.h>
19#include <part.h>
20#include <malloc.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050021#include <fsl_esdhc.h>
Anton Vorontsovb33433a2009-06-10 00:25:29 +040022#include <fdt_support.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050023#include <asm/io.h>
Peng Fan96f04072016-03-25 14:16:56 +080024#include <dm.h>
Andy Fleming50586ef2008-10-30 16:47:16 -050025
Yangbo Lu66fa0352019-05-23 11:05:46 +080026#if !CONFIG_IS_ENABLED(BLK)
27#include "mmc_private.h"
28#endif
29
Andy Fleming50586ef2008-10-30 16:47:16 -050030DECLARE_GLOBAL_DATA_PTR;
31
Ye.Lia3d6e382014-11-04 15:35:49 +080032#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
33 IRQSTATEN_CINT | \
34 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
35 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
36 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
37 IRQSTATEN_DINT)
Yangbo Lu66fa0352019-05-23 11:05:46 +080038#define ESDHC_DRIVER_STAGE_VALUE 0xffffffff
Ye.Lia3d6e382014-11-04 15:35:49 +080039
Andy Fleming50586ef2008-10-30 16:47:16 -050040struct fsl_esdhc {
Haijun.Zhang511948b2013-10-30 11:37:55 +080041 uint dsaddr; /* SDMA system address register */
42 uint blkattr; /* Block attributes register */
43 uint cmdarg; /* Command argument register */
44 uint xfertyp; /* Transfer type register */
45 uint cmdrsp0; /* Command response 0 register */
46 uint cmdrsp1; /* Command response 1 register */
47 uint cmdrsp2; /* Command response 2 register */
48 uint cmdrsp3; /* Command response 3 register */
49 uint datport; /* Buffer data port register */
50 uint prsstat; /* Present state register */
51 uint proctl; /* Protocol control register */
52 uint sysctl; /* System Control Register */
53 uint irqstat; /* Interrupt status register */
54 uint irqstaten; /* Interrupt status enable register */
55 uint irqsigen; /* Interrupt signal enable register */
56 uint autoc12err; /* Auto CMD error status register */
57 uint hostcapblt; /* Host controller capabilities register */
58 uint wml; /* Watermark level register */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080059 char reserved1[8]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080060 uint fevt; /* Force event register */
61 uint admaes; /* ADMA error status register */
62 uint adsaddr; /* ADMA system address register */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080063 char reserved2[160];
Haijun.Zhang511948b2013-10-30 11:37:55 +080064 uint hostver; /* Host controller version register */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080065 char reserved3[4]; /* reserved */
Peng Fan59d37822018-01-21 19:00:22 +080066 uint dmaerraddr; /* DMA error address register */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080067 char reserved4[4]; /* reserved */
Peng Fan59d37822018-01-21 19:00:22 +080068 uint dmaerrattr; /* DMA error attribute register */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080069 char reserved5[4]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080070 uint hostcapblt2; /* Host controller capabilities register 2 */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080071 char reserved6[756]; /* reserved */
72 uint esdhcctl; /* eSDHC control register */
Andy Fleming50586ef2008-10-30 16:47:16 -050073};
74
Simon Glasse88e1d92017-07-29 11:35:21 -060075struct fsl_esdhc_plat {
76 struct mmc_config cfg;
77 struct mmc mmc;
78};
79
Peng Fan96f04072016-03-25 14:16:56 +080080/**
81 * struct fsl_esdhc_priv
82 *
83 * @esdhc_regs: registers of the sdhc controller
84 * @sdhc_clk: Current clk of the sdhc controller
85 * @bus_width: bus width, 1bit, 4bit or 8bit
86 * @cfg: mmc config
87 * @mmc: mmc
88 * Following is used when Driver Model is enabled for MMC
89 * @dev: pointer for the device
90 * @non_removable: 0: removable; 1: non-removable
Peng Fan14831512016-06-15 10:53:02 +080091 * @wp_enable: 1: enable checking wp; 0: no check
Peng Fan96f04072016-03-25 14:16:56 +080092 * @cd_gpio: gpio for card detection
Peng Fan14831512016-06-15 10:53:02 +080093 * @wp_gpio: gpio for write protection
Peng Fan96f04072016-03-25 14:16:56 +080094 */
95struct fsl_esdhc_priv {
96 struct fsl_esdhc *esdhc_regs;
97 unsigned int sdhc_clk;
Peng Fan3cb14502018-10-18 14:28:35 +020098 struct clk per_clk;
Peng Fan51313b42018-01-21 19:00:24 +080099 unsigned int clock;
Peng Fan96f04072016-03-25 14:16:56 +0800100 unsigned int bus_width;
Simon Glass653282b2017-07-29 11:35:24 -0600101#if !CONFIG_IS_ENABLED(BLK)
Peng Fan96f04072016-03-25 14:16:56 +0800102 struct mmc *mmc;
Simon Glass653282b2017-07-29 11:35:24 -0600103#endif
Peng Fan96f04072016-03-25 14:16:56 +0800104 struct udevice *dev;
105 int non_removable;
Peng Fan14831512016-06-15 10:53:02 +0800106 int wp_enable;
Peng Fan96f04072016-03-25 14:16:56 +0800107};
108
Andy Fleming50586ef2008-10-30 16:47:16 -0500109/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +0000110static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500111{
112 uint xfertyp = 0;
113
114 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530115 xfertyp |= XFERTYP_DPSEL;
116#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
117 xfertyp |= XFERTYP_DMAEN;
118#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500119 if (data->blocks > 1) {
120 xfertyp |= XFERTYP_MSBSEL;
121 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -0600122#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
123 xfertyp |= XFERTYP_AC12EN;
124#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500125 }
126
127 if (data->flags & MMC_DATA_READ)
128 xfertyp |= XFERTYP_DTDSEL;
129 }
130
131 if (cmd->resp_type & MMC_RSP_CRC)
132 xfertyp |= XFERTYP_CCCEN;
133 if (cmd->resp_type & MMC_RSP_OPCODE)
134 xfertyp |= XFERTYP_CICEN;
135 if (cmd->resp_type & MMC_RSP_136)
136 xfertyp |= XFERTYP_RSPTYP_136;
137 else if (cmd->resp_type & MMC_RSP_BUSY)
138 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
139 else if (cmd->resp_type & MMC_RSP_PRESENT)
140 xfertyp |= XFERTYP_RSPTYP_48;
141
Jason Liu4571de32011-03-22 01:32:31 +0000142 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
143 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lu25503442016-01-21 17:33:19 +0800144
Andy Fleming50586ef2008-10-30 16:47:16 -0500145 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
146}
147
Dipen Dudhat77c14582009-10-05 15:41:58 +0530148#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
149/*
150 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
151 */
Simon Glass09b465f2017-07-29 11:35:17 -0600152static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
153 struct mmc_data *data)
Dipen Dudhat77c14582009-10-05 15:41:58 +0530154{
Peng Fan96f04072016-03-25 14:16:56 +0800155 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530156 uint blocks;
157 char *buffer;
158 uint databuf;
159 uint size;
160 uint irqstat;
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100161 ulong start;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530162
163 if (data->flags & MMC_DATA_READ) {
164 blocks = data->blocks;
165 buffer = data->dest;
166 while (blocks) {
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100167 start = get_timer(0);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530168 size = data->blocksize;
169 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100170 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
171 if (get_timer(start) > PIO_TIMEOUT) {
172 printf("\nData Read Failed in PIO Mode.");
173 return;
174 }
Dipen Dudhat77c14582009-10-05 15:41:58 +0530175 }
176 while (size && (!(irqstat & IRQSTAT_TC))) {
177 udelay(100); /* Wait before last byte transfer complete */
178 irqstat = esdhc_read32(&regs->irqstat);
179 databuf = in_le32(&regs->datport);
180 *((uint *)buffer) = databuf;
181 buffer += 4;
182 size -= 4;
183 }
184 blocks--;
185 }
186 } else {
187 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200188 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530189 while (blocks) {
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100190 start = get_timer(0);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530191 size = data->blocksize;
192 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100193 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
194 if (get_timer(start) > PIO_TIMEOUT) {
195 printf("\nData Write Failed in PIO Mode.");
196 return;
197 }
Dipen Dudhat77c14582009-10-05 15:41:58 +0530198 }
199 while (size && (!(irqstat & IRQSTAT_TC))) {
200 udelay(100); /* Wait before last byte transfer complete */
201 databuf = *((uint *)buffer);
202 buffer += 4;
203 size -= 4;
204 irqstat = esdhc_read32(&regs->irqstat);
205 out_le32(&regs->datport, databuf);
206 }
207 blocks--;
208 }
209 }
210}
211#endif
212
Simon Glass09b465f2017-07-29 11:35:17 -0600213static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
214 struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500215{
Andy Fleming50586ef2008-10-30 16:47:16 -0500216 int timeout;
Peng Fan96f04072016-03-25 14:16:56 +0800217 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800218#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lu8b064602015-03-20 19:28:31 -0700219 dma_addr_t addr;
220#endif
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200221 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500222
223 wml_value = data->blocksize/4;
224
225 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530226 if (wml_value > WML_RD_WML_MAX)
227 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500228
Roy Zangab467c52010-02-09 18:23:33 +0800229 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li71689772014-02-20 18:00:57 +0800230#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800231#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lu8b064602015-03-20 19:28:31 -0700232 addr = virt_to_phys((void *)(data->dest));
233 if (upper_32_bits(addr))
234 printf("Error found for upper 32 bits\n");
235 else
236 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
237#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100238 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li71689772014-02-20 18:00:57 +0800239#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700240#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500241 } else {
Ye.Li71689772014-02-20 18:00:57 +0800242#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-04-25 14:28:48 +0000243 flush_dcache_range((ulong)data->src,
244 (ulong)data->src+data->blocks
245 *data->blocksize);
Ye.Li71689772014-02-20 18:00:57 +0800246#endif
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530247 if (wml_value > WML_WR_WML_MAX)
248 wml_value = WML_WR_WML_MAX_VAL;
Peng Fan14831512016-06-15 10:53:02 +0800249 if (priv->wp_enable) {
250 if ((esdhc_read32(&regs->prsstat) &
251 PRSSTAT_WPSPL) == 0) {
252 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900253 return -ETIMEDOUT;
Peng Fan14831512016-06-15 10:53:02 +0800254 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500255 }
Roy Zangab467c52010-02-09 18:23:33 +0800256
257 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
258 wml_value << 16);
Ye.Li71689772014-02-20 18:00:57 +0800259#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800260#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lu8b064602015-03-20 19:28:31 -0700261 addr = virt_to_phys((void *)(data->src));
262 if (upper_32_bits(addr))
263 printf("Error found for upper 32 bits\n");
264 else
265 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
266#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100267 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li71689772014-02-20 18:00:57 +0800268#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700269#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500270 }
271
Stefano Babicc67bee12010-02-05 15:11:27 +0100272 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500273
274 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530275 /*
276 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
277 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
278 * So, Number of SD Clock cycles for 0.25sec should be minimum
279 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500280 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530281 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500282 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530283 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500284 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530285 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500286 * => timeout + 13 = log2(mmc->clock/4) + 1
287 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lue978a312015-12-30 14:19:30 +0800288 *
289 * However, the MMC spec "It is strongly recommended for hosts to
290 * implement more than 500ms timeout value even if the card
291 * indicates the 250ms maximum busy length." Even the previous
292 * value of 300ms is known to be insufficient for some cards.
293 * So, we use
294 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530295 */
Yangbo Lue978a312015-12-30 14:19:30 +0800296 timeout = fls(mmc->clock/2);
Andy Fleming50586ef2008-10-30 16:47:16 -0500297 timeout -= 13;
298
299 if (timeout > 14)
300 timeout = 14;
301
302 if (timeout < 0)
303 timeout = 0;
304
Kumar Gala5103a032011-01-29 15:36:10 -0600305#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
306 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
307 timeout++;
308#endif
309
Haijun.Zhang1336e2d2014-03-18 17:04:23 +0800310#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
311 timeout = 0xE;
312#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100313 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500314
315 return 0;
316}
317
Eric Nelsone576bd92012-04-25 14:28:48 +0000318static void check_and_invalidate_dcache_range
319 (struct mmc_cmd *cmd,
320 struct mmc_data *data) {
Yangbo Lu8b064602015-03-20 19:28:31 -0700321 unsigned start = 0;
Yangbo Lucc634e22016-05-12 19:12:58 +0800322 unsigned end = 0;
Eric Nelsone576bd92012-04-25 14:28:48 +0000323 unsigned size = roundup(ARCH_DMA_MINALIGN,
324 data->blocks*data->blocksize);
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800325#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lu8b064602015-03-20 19:28:31 -0700326 dma_addr_t addr;
327
328 addr = virt_to_phys((void *)(data->dest));
329 if (upper_32_bits(addr))
330 printf("Error found for upper 32 bits\n");
331 else
332 start = lower_32_bits(addr);
Yangbo Lucc634e22016-05-12 19:12:58 +0800333#else
334 start = (unsigned)data->dest;
Yangbo Lu8b064602015-03-20 19:28:31 -0700335#endif
Yangbo Lucc634e22016-05-12 19:12:58 +0800336 end = start + size;
Eric Nelsone576bd92012-04-25 14:28:48 +0000337 invalidate_dcache_range(start, end);
338}
Tom Rini10dc7772014-05-23 09:19:05 -0400339
Andy Fleming50586ef2008-10-30 16:47:16 -0500340/*
341 * Sends a command out on the bus. Takes the mmc pointer,
342 * a command pointer, and an optional data pointer.
343 */
Simon Glass9586aa62017-07-29 11:35:18 -0600344static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
345 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500346{
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500347 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500348 uint xfertyp;
349 uint irqstat;
Peng Fan51313b42018-01-21 19:00:24 +0800350 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fan96f04072016-03-25 14:16:56 +0800351 struct fsl_esdhc *regs = priv->esdhc_regs;
Fabio Estevam29c2edb2018-11-19 10:31:53 -0200352 unsigned long start;
Andy Fleming50586ef2008-10-30 16:47:16 -0500353
Jerry Huangd621da02011-01-06 23:42:19 -0600354#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
355 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
356 return 0;
357#endif
358
Stefano Babicc67bee12010-02-05 15:11:27 +0100359 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500360
361 sync();
362
363 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100364 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
365 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
366 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500367
Stefano Babicc67bee12010-02-05 15:11:27 +0100368 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
369 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500370
371 /* Wait at least 8 SD clock cycles before the next command */
372 /*
373 * Note: This is way more than 8 cycles, but 1ms seems to
374 * resolve timing issues with some cards
375 */
376 udelay(1000);
377
378 /* Set up for a data transfer if we have one */
379 if (data) {
Simon Glass09b465f2017-07-29 11:35:17 -0600380 err = esdhc_setup_data(priv, mmc, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500381 if(err)
382 return err;
Peng Fan4683b222015-06-25 10:32:26 +0800383
384 if (data->flags & MMC_DATA_READ)
385 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500386 }
387
388 /* Figure out the transfer arguments */
389 xfertyp = esdhc_xfertyp(cmd, data);
390
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500391 /* Mask all irqs */
392 esdhc_write32(&regs->irqsigen, 0);
393
Andy Fleming50586ef2008-10-30 16:47:16 -0500394 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100395 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
396 esdhc_write32(&regs->xfertyp, xfertyp);
Dirk Behme7a5b8022012-03-26 03:13:05 +0000397
Peng Fan51313b42018-01-21 19:00:24 +0800398 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
399 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))
400 flags = IRQSTAT_BRR;
401
Andy Fleming50586ef2008-10-30 16:47:16 -0500402 /* Wait for the command to complete */
Fabio Estevam29c2edb2018-11-19 10:31:53 -0200403 start = get_timer(0);
404 while (!(esdhc_read32(&regs->irqstat) & flags)) {
405 if (get_timer(start) > 1000) {
406 err = -ETIMEDOUT;
407 goto out;
408 }
409 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500410
Stefano Babicc67bee12010-02-05 15:11:27 +0100411 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500412
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500413 if (irqstat & CMD_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900414 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500415 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000416 }
417
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500418 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900419 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500420 goto out;
421 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500422
Dirk Behme7a5b8022012-03-26 03:13:05 +0000423 /* Workaround for ESDHC errata ENGcm03648 */
424 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800425 int timeout = 6000;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000426
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800427 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000428 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
429 PRSSTAT_DAT0)) {
430 udelay(100);
431 timeout--;
432 }
433
434 if (timeout <= 0) {
435 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900436 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500437 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000438 }
439 }
440
Andy Fleming50586ef2008-10-30 16:47:16 -0500441 /* Copy the response to the response buffer */
442 if (cmd->resp_type & MMC_RSP_136) {
443 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
444
Stefano Babicc67bee12010-02-05 15:11:27 +0100445 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
446 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
447 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
448 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530449 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
450 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
451 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
452 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500453 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100454 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500455
456 /* Wait until all of the blocks are transferred */
457 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530458#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass09b465f2017-07-29 11:35:17 -0600459 esdhc_pio_read_write(priv, data);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530460#else
Peng Fan51313b42018-01-21 19:00:24 +0800461 flags = DATA_COMPLETE;
462 if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) ||
463 (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) {
464 flags = IRQSTAT_BRR;
465 }
466
Andy Fleming50586ef2008-10-30 16:47:16 -0500467 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100468 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500469
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500470 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900471 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500472 goto out;
473 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000474
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500475 if (irqstat & DATA_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900476 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500477 goto out;
478 }
Peng Fan51313b42018-01-21 19:00:24 +0800479 } while ((irqstat & flags) != flags);
Ye.Li71689772014-02-20 18:00:57 +0800480
Peng Fan4683b222015-06-25 10:32:26 +0800481 /*
482 * Need invalidate the dcache here again to avoid any
483 * cache-fill during the DMA operations such as the
484 * speculative pre-fetching etc.
485 */
Angelo Dureghello1f15cb82019-01-19 10:40:38 +0100486 if (data->flags & MMC_DATA_READ) {
Eric Nelson54899fc2013-04-03 12:31:56 +0000487 check_and_invalidate_dcache_range(cmd, data);
Angelo Dureghello1f15cb82019-01-19 10:40:38 +0100488 }
Ye.Li71689772014-02-20 18:00:57 +0800489#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500490 }
491
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500492out:
493 /* Reset CMD and DATA portions on error */
494 if (err) {
495 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
496 SYSCTL_RSTC);
497 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
498 ;
499
500 if (data) {
501 esdhc_write32(&regs->sysctl,
502 esdhc_read32(&regs->sysctl) |
503 SYSCTL_RSTD);
504 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
505 ;
506 }
507 }
508
Stefano Babicc67bee12010-02-05 15:11:27 +0100509 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500510
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500511 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500512}
513
Simon Glass09b465f2017-07-29 11:35:17 -0600514static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500515{
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100516 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200517 int div = 1;
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200518 int pre_div = 2;
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200519 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
Peng Fan96f04072016-03-25 14:16:56 +0800520 int sdhc_clk = priv->sdhc_clk;
Andy Fleming50586ef2008-10-30 16:47:16 -0500521 uint clk;
522
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200523 if (clock < mmc->cfg->f_min)
524 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100525
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200526 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
527 pre_div *= 2;
Andy Fleming50586ef2008-10-30 16:47:16 -0500528
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200529 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
530 div++;
Andy Fleming50586ef2008-10-30 16:47:16 -0500531
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200532 pre_div >>= 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500533 div -= 1;
534
535 clk = (pre_div << 8) | (div << 4);
536
Kumar Galacc4d1222010-03-18 15:51:05 -0500537 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicc67bee12010-02-05 15:11:27 +0100538
539 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500540
541 udelay(10000);
542
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700543 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Stefano Babicc67bee12010-02-05 15:11:27 +0100544
Peng Fan51313b42018-01-21 19:00:24 +0800545 priv->clock = clock;
Andy Fleming50586ef2008-10-30 16:47:16 -0500546}
547
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800548#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
Simon Glass09b465f2017-07-29 11:35:17 -0600549static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800550{
Peng Fan96f04072016-03-25 14:16:56 +0800551 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800552 u32 value;
553 u32 time_out;
554
555 value = esdhc_read32(&regs->sysctl);
556
557 if (enable)
558 value |= SYSCTL_CKEN;
559 else
560 value &= ~SYSCTL_CKEN;
561
562 esdhc_write32(&regs->sysctl, value);
563
564 time_out = 20;
565 value = PRSSTAT_SDSTB;
566 while (!(esdhc_read32(&regs->prsstat) & value)) {
567 if (time_out == 0) {
568 printf("fsl_esdhc: Internal clock never stabilised.\n");
569 break;
570 }
571 time_out--;
572 mdelay(1);
573 }
574}
575#endif
576
Simon Glass9586aa62017-07-29 11:35:18 -0600577static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500578{
Peng Fan96f04072016-03-25 14:16:56 +0800579 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500580
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800581#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
582 /* Select to use peripheral clock */
Simon Glass09b465f2017-07-29 11:35:17 -0600583 esdhc_clock_control(priv, false);
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800584 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_PCS);
Simon Glass09b465f2017-07-29 11:35:17 -0600585 esdhc_clock_control(priv, true);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800586#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500587 /* Set the clock speed */
Peng Fan51313b42018-01-21 19:00:24 +0800588 if (priv->clock != mmc->clock)
589 set_sysctl(priv, mmc, mmc->clock);
590
Andy Fleming50586ef2008-10-30 16:47:16 -0500591 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100592 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500593
594 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100595 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500596 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100597 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
598
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900599 return 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500600}
601
Simon Glass9586aa62017-07-29 11:35:18 -0600602static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500603{
Peng Fan96f04072016-03-25 14:16:56 +0800604 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass201e8282017-07-29 11:35:20 -0600605 ulong start;
Andy Fleming50586ef2008-10-30 16:47:16 -0500606
Stefano Babicc67bee12010-02-05 15:11:27 +0100607 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200608 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100609
610 /* Wait until the controller is available */
Simon Glass201e8282017-07-29 11:35:20 -0600611 start = get_timer(0);
612 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
613 if (get_timer(start) > 1000)
614 return -ETIMEDOUT;
615 }
Stefano Babicc67bee12010-02-05 15:11:27 +0100616
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530617 /* Enable cache snooping */
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800618 esdhc_write32(&regs->esdhcctl, 0x00000040);
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530619
Dirk Behmea61da722013-07-15 15:44:29 +0200620 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleming50586ef2008-10-30 16:47:16 -0500621
622 /* Set the initial clock speed */
Jaehoon Chung65117182018-01-26 19:25:29 +0900623 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
Andy Fleming50586ef2008-10-30 16:47:16 -0500624
625 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100626 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500627
628 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100629 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500630
Stefano Babicc67bee12010-02-05 15:11:27 +0100631 /* Set timout to the maximum value */
632 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500633
Thierry Redingd48d2e22012-01-02 01:15:38 +0000634 return 0;
635}
Andy Fleming50586ef2008-10-30 16:47:16 -0500636
Simon Glass9586aa62017-07-29 11:35:18 -0600637static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Redingd48d2e22012-01-02 01:15:38 +0000638{
Peng Fan96f04072016-03-25 14:16:56 +0800639 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Redingd48d2e22012-01-02 01:15:38 +0000640 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100641
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +0800642#ifdef CONFIG_ESDHC_DETECT_QUIRK
643 if (CONFIG_ESDHC_DETECT_QUIRK)
644 return 1;
645#endif
Peng Fan96f04072016-03-25 14:16:56 +0800646
Simon Glass653282b2017-07-29 11:35:24 -0600647#if CONFIG_IS_ENABLED(DM_MMC)
Peng Fan96f04072016-03-25 14:16:56 +0800648 if (priv->non_removable)
649 return 1;
Yangbo Lufc8048a2016-12-07 11:54:30 +0800650#endif
Peng Fan96f04072016-03-25 14:16:56 +0800651
Thierry Redingd48d2e22012-01-02 01:15:38 +0000652 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
653 udelay(1000);
654
655 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500656}
657
Simon Glass446e0772017-07-29 11:35:19 -0600658static int esdhc_reset(struct fsl_esdhc *regs)
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500659{
Simon Glass446e0772017-07-29 11:35:19 -0600660 ulong start;
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500661
662 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200663 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500664
665 /* hardware clears the bit when it is done */
Simon Glass446e0772017-07-29 11:35:19 -0600666 start = get_timer(0);
667 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
668 if (get_timer(start) > 100) {
669 printf("MMC/SD: Reset never completed.\n");
670 return -ETIMEDOUT;
671 }
672 }
673
674 return 0;
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500675}
676
Simon Glasse7881d82017-07-29 11:35:31 -0600677#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass9586aa62017-07-29 11:35:18 -0600678static int esdhc_getcd(struct mmc *mmc)
679{
680 struct fsl_esdhc_priv *priv = mmc->priv;
681
682 return esdhc_getcd_common(priv);
683}
684
685static int esdhc_init(struct mmc *mmc)
686{
687 struct fsl_esdhc_priv *priv = mmc->priv;
688
689 return esdhc_init_common(priv, mmc);
690}
691
692static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
693 struct mmc_data *data)
694{
695 struct fsl_esdhc_priv *priv = mmc->priv;
696
697 return esdhc_send_cmd_common(priv, mmc, cmd, data);
698}
699
700static int esdhc_set_ios(struct mmc *mmc)
701{
702 struct fsl_esdhc_priv *priv = mmc->priv;
703
704 return esdhc_set_ios_common(priv, mmc);
705}
706
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200707static const struct mmc_ops esdhc_ops = {
Simon Glass9586aa62017-07-29 11:35:18 -0600708 .getcd = esdhc_getcd,
709 .init = esdhc_init,
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200710 .send_cmd = esdhc_send_cmd,
711 .set_ios = esdhc_set_ios,
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200712};
Simon Glass653282b2017-07-29 11:35:24 -0600713#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200714
Simon Glasse88e1d92017-07-29 11:35:21 -0600715static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
716 struct fsl_esdhc_plat *plat)
Andy Fleming50586ef2008-10-30 16:47:16 -0500717{
Simon Glasse88e1d92017-07-29 11:35:21 -0600718 struct mmc_config *cfg;
Stefano Babicc67bee12010-02-05 15:11:27 +0100719 struct fsl_esdhc *regs;
Li Yang030955c2010-11-25 17:06:09 +0000720 u32 caps, voltage_caps;
Simon Glass446e0772017-07-29 11:35:19 -0600721 int ret;
Andy Fleming50586ef2008-10-30 16:47:16 -0500722
Peng Fan96f04072016-03-25 14:16:56 +0800723 if (!priv)
724 return -EINVAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100725
Peng Fan96f04072016-03-25 14:16:56 +0800726 regs = priv->esdhc_regs;
Stefano Babicc67bee12010-02-05 15:11:27 +0100727
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500728 /* First reset the eSDHC controller */
Simon Glass446e0772017-07-29 11:35:19 -0600729 ret = esdhc_reset(regs);
730 if (ret)
731 return ret;
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500732
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800733 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
734 SYSCTL_IPGEN | SYSCTL_CKEN);
Peng Fan32a91792017-06-12 17:50:53 +0800735
Ye.Lia3d6e382014-11-04 15:35:49 +0800736 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Simon Glasse88e1d92017-07-29 11:35:21 -0600737 cfg = &plat->cfg;
Simon Glass653282b2017-07-29 11:35:24 -0600738#ifndef CONFIG_DM_MMC
Simon Glasse88e1d92017-07-29 11:35:21 -0600739 memset(cfg, '\0', sizeof(*cfg));
Simon Glass653282b2017-07-29 11:35:24 -0600740#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200741
Li Yang030955c2010-11-25 17:06:09 +0000742 voltage_caps = 0;
Wang Huan19060bd2014-09-05 13:52:40 +0800743 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -0600744
745#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
746 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
747 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
748#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +0800749
750/* T4240 host controller capabilities register should have VS33 bit */
751#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
752 caps = caps | ESDHC_HOSTCAPBLT_VS33;
753#endif
754
Andy Fleming50586ef2008-10-30 16:47:16 -0500755 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +0000756 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -0500757 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +0000758 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -0500759 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +0000760 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
761
Simon Glasse88e1d92017-07-29 11:35:21 -0600762 cfg->name = "FSL_SDHC";
Simon Glasse7881d82017-07-29 11:35:31 -0600763#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glasse88e1d92017-07-29 11:35:21 -0600764 cfg->ops = &esdhc_ops;
Simon Glass653282b2017-07-29 11:35:24 -0600765#endif
Li Yang030955c2010-11-25 17:06:09 +0000766#ifdef CONFIG_SYS_SD_VOLTAGE
Simon Glasse88e1d92017-07-29 11:35:21 -0600767 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +0000768#else
Simon Glasse88e1d92017-07-29 11:35:21 -0600769 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +0000770#endif
Simon Glasse88e1d92017-07-29 11:35:21 -0600771 if ((cfg->voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +0000772 printf("voltage not supported by controller\n");
773 return -1;
774 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500775
Peng Fan96f04072016-03-25 14:16:56 +0800776 if (priv->bus_width == 8)
Simon Glasse88e1d92017-07-29 11:35:21 -0600777 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +0800778 else if (priv->bus_width == 4)
Simon Glasse88e1d92017-07-29 11:35:21 -0600779 cfg->host_caps = MMC_MODE_4BIT;
Peng Fan96f04072016-03-25 14:16:56 +0800780
Simon Glasse88e1d92017-07-29 11:35:21 -0600781 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500782#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Simon Glasse88e1d92017-07-29 11:35:21 -0600783 cfg->host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500784#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500785
Peng Fan96f04072016-03-25 14:16:56 +0800786 if (priv->bus_width > 0) {
787 if (priv->bus_width < 8)
Simon Glasse88e1d92017-07-29 11:35:21 -0600788 cfg->host_caps &= ~MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +0800789 if (priv->bus_width < 4)
Simon Glasse88e1d92017-07-29 11:35:21 -0600790 cfg->host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000791 }
792
Andy Fleming50586ef2008-10-30 16:47:16 -0500793 if (caps & ESDHC_HOSTCAPBLT_HSS)
Simon Glasse88e1d92017-07-29 11:35:21 -0600794 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -0500795
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800796#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
797 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Simon Glasse88e1d92017-07-29 11:35:21 -0600798 cfg->host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800799#endif
800
Simon Glasse88e1d92017-07-29 11:35:21 -0600801 cfg->f_min = 400000;
Peng Fan51313b42018-01-21 19:00:24 +0800802 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500803
Simon Glasse88e1d92017-07-29 11:35:21 -0600804 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200805
Peng Fan96f04072016-03-25 14:16:56 +0800806 return 0;
807}
808
Simon Glass52489302017-07-29 11:35:28 -0600809#if !CONFIG_IS_ENABLED(DM_MMC)
Jagan Teki2e87c442017-05-12 17:18:20 +0530810static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
811 struct fsl_esdhc_priv *priv)
812{
813 if (!cfg || !priv)
814 return -EINVAL;
815
816 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
817 priv->bus_width = cfg->max_bus_width;
818 priv->sdhc_clk = cfg->sdhc_clk;
819 priv->wp_enable = cfg->wp_enable;
820
821 return 0;
822};
823
Peng Fan96f04072016-03-25 14:16:56 +0800824int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
825{
Simon Glasse88e1d92017-07-29 11:35:21 -0600826 struct fsl_esdhc_plat *plat;
Peng Fan96f04072016-03-25 14:16:56 +0800827 struct fsl_esdhc_priv *priv;
Simon Glassd6eb25e2017-07-29 11:35:22 -0600828 struct mmc *mmc;
Peng Fan96f04072016-03-25 14:16:56 +0800829 int ret;
830
831 if (!cfg)
832 return -EINVAL;
833
834 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
835 if (!priv)
836 return -ENOMEM;
Simon Glasse88e1d92017-07-29 11:35:21 -0600837 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
838 if (!plat) {
839 free(priv);
840 return -ENOMEM;
841 }
Peng Fan96f04072016-03-25 14:16:56 +0800842
843 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
844 if (ret) {
845 debug("%s xlate failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -0600846 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +0800847 free(priv);
848 return ret;
849 }
850
Simon Glasse88e1d92017-07-29 11:35:21 -0600851 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +0800852 if (ret) {
853 debug("%s init failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -0600854 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +0800855 free(priv);
856 return ret;
857 }
858
Simon Glassd6eb25e2017-07-29 11:35:22 -0600859 mmc = mmc_create(&plat->cfg, priv);
860 if (!mmc)
861 return -EIO;
862
863 priv->mmc = mmc;
864
Andy Fleming50586ef2008-10-30 16:47:16 -0500865 return 0;
866}
867
868int fsl_esdhc_mmc_init(bd_t *bis)
869{
Stefano Babicc67bee12010-02-05 15:11:27 +0100870 struct fsl_esdhc_cfg *cfg;
871
Fabio Estevam88227a12012-12-27 08:51:08 +0000872 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +0100873 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +0000874 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +0100875 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500876}
Jagan Teki2e87c442017-05-12 17:18:20 +0530877#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400878
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800879#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
880void mmc_adapter_card_type_ident(void)
881{
882 u8 card_id;
883 u8 value;
884
885 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
886 gd->arch.sdhc_adapter = card_id;
887
888 switch (card_id) {
889 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lucdc69552015-09-17 10:27:12 +0800890 value = QIXIS_READ(brdcfg[5]);
891 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
892 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800893 break;
894 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Lubf50be82015-09-17 10:27:48 +0800895 value = QIXIS_READ(pwr_ctl[1]);
896 value |= QIXIS_EVDD_BY_SDHC_VS;
897 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800898 break;
899 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
900 value = QIXIS_READ(brdcfg[5]);
901 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
902 QIXIS_WRITE(brdcfg[5], value);
903 break;
904 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
905 break;
906 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
907 break;
908 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
909 break;
910 case QIXIS_ESDHC_NO_ADAPTER:
911 break;
912 default:
913 break;
914 }
915}
916#endif
917
Stefano Babicc67bee12010-02-05 15:11:27 +0100918#ifdef CONFIG_OF_LIBFDT
Yangbo Lufce1e162017-01-17 10:43:54 +0800919__weak int esdhc_status_fixup(void *blob, const char *compat)
920{
921#ifdef CONFIG_FSL_ESDHC_PIN_MUX
922 if (!hwconfig("esdhc")) {
923 do_fixup_by_compat(blob, compat, "status", "disabled",
924 sizeof("disabled"), 1);
925 return 1;
926 }
927#endif
Yangbo Lufce1e162017-01-17 10:43:54 +0800928 return 0;
929}
930
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400931void fdt_fixup_esdhc(void *blob, bd_t *bd)
932{
933 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400934
Yangbo Lufce1e162017-01-17 10:43:54 +0800935 if (esdhc_status_fixup(blob, compat))
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800936 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400937
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800938#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
939 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
940 gd->arch.sdhc_clk, 1);
941#else
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400942 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000943 gd->arch.sdhc_clk, 1);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800944#endif
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800945#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
946 do_fixup_by_compat_u32(blob, compat, "adapter-type",
947 (u32)(gd->arch.sdhc_adapter), 1);
948#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400949}
Stefano Babicc67bee12010-02-05 15:11:27 +0100950#endif
Peng Fan96f04072016-03-25 14:16:56 +0800951
Simon Glass653282b2017-07-29 11:35:24 -0600952#if CONFIG_IS_ENABLED(DM_MMC)
Yinbo Zhub512d072019-04-11 11:01:46 +0000953#ifndef CONFIG_PPC
Peng Fan96f04072016-03-25 14:16:56 +0800954#include <asm/arch/clock.h>
Yinbo Zhub512d072019-04-11 11:01:46 +0000955#endif
Peng Fan96f04072016-03-25 14:16:56 +0800956static int fsl_esdhc_probe(struct udevice *dev)
957{
958 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glasse88e1d92017-07-29 11:35:21 -0600959 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fan96f04072016-03-25 14:16:56 +0800960 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Peng Fan96f04072016-03-25 14:16:56 +0800961 fdt_addr_t addr;
962 unsigned int val;
Simon Glass653282b2017-07-29 11:35:24 -0600963 struct mmc *mmc;
Yangbo Lu66fa0352019-05-23 11:05:46 +0800964#if !CONFIG_IS_ENABLED(BLK)
965 struct blk_desc *bdesc;
966#endif
Peng Fan96f04072016-03-25 14:16:56 +0800967 int ret;
968
Simon Glass4aac33f2017-07-29 11:35:23 -0600969 addr = dev_read_addr(dev);
Peng Fan96f04072016-03-25 14:16:56 +0800970 if (addr == FDT_ADDR_T_NONE)
971 return -EINVAL;
Yinbo Zhub69e1d02019-04-11 11:01:50 +0000972#ifdef CONFIG_PPC
973 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
974#else
Peng Fan96f04072016-03-25 14:16:56 +0800975 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yinbo Zhub69e1d02019-04-11 11:01:50 +0000976#endif
Peng Fan96f04072016-03-25 14:16:56 +0800977 priv->dev = dev;
978
Simon Glass4aac33f2017-07-29 11:35:23 -0600979 val = dev_read_u32_default(dev, "bus-width", -1);
Peng Fan96f04072016-03-25 14:16:56 +0800980 if (val == 8)
981 priv->bus_width = 8;
982 else if (val == 4)
983 priv->bus_width = 4;
984 else
985 priv->bus_width = 1;
986
Simon Glass4aac33f2017-07-29 11:35:23 -0600987 if (dev_read_bool(dev, "non-removable")) {
Peng Fan96f04072016-03-25 14:16:56 +0800988 priv->non_removable = 1;
989 } else {
990 priv->non_removable = 0;
Peng Fan96f04072016-03-25 14:16:56 +0800991 }
992
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800993 priv->wp_enable = 1;
Peng Fanb60f1452017-02-22 16:21:55 +0800994
Peng Fan3cb14502018-10-18 14:28:35 +0200995 if (IS_ENABLED(CONFIG_CLK)) {
996 /* Assigned clock already set clock */
997 ret = clk_get_by_name(dev, "per", &priv->per_clk);
998 if (ret) {
999 printf("Failed to get per_clk\n");
1000 return ret;
1001 }
1002 ret = clk_enable(&priv->per_clk);
1003 if (ret) {
1004 printf("Failed to enable per_clk\n");
1005 return ret;
1006 }
1007
1008 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1009 } else {
Yinbo Zhub512d072019-04-11 11:01:46 +00001010#ifndef CONFIG_PPC
Peng Fan3cb14502018-10-18 14:28:35 +02001011 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
Yinbo Zhub512d072019-04-11 11:01:46 +00001012#else
1013 priv->sdhc_clk = gd->arch.sdhc_clk;
1014#endif
Peng Fan3cb14502018-10-18 14:28:35 +02001015 if (priv->sdhc_clk <= 0) {
1016 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1017 return -EINVAL;
1018 }
Peng Fan96f04072016-03-25 14:16:56 +08001019 }
1020
Simon Glasse88e1d92017-07-29 11:35:21 -06001021 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +08001022 if (ret) {
1023 dev_err(dev, "fsl_esdhc_init failure\n");
1024 return ret;
1025 }
1026
Simon Glass653282b2017-07-29 11:35:24 -06001027 mmc = &plat->mmc;
1028 mmc->cfg = &plat->cfg;
1029 mmc->dev = dev;
Yangbo Lu66fa0352019-05-23 11:05:46 +08001030#if !CONFIG_IS_ENABLED(BLK)
1031 mmc->priv = priv;
1032
1033 /* Setup dsr related values */
1034 mmc->dsr_imp = 0;
1035 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1036 /* Setup the universal parts of the block interface just once */
1037 bdesc = mmc_get_blk_desc(mmc);
1038 bdesc->if_type = IF_TYPE_MMC;
1039 bdesc->removable = 1;
1040 bdesc->devnum = mmc_get_next_devnum();
1041 bdesc->block_read = mmc_bread;
1042 bdesc->block_write = mmc_bwrite;
1043 bdesc->block_erase = mmc_berase;
1044
1045 /* setup initial part type */
1046 bdesc->part_type = mmc->cfg->part_type;
1047 mmc_list_add(mmc);
1048#endif
1049
Simon Glass653282b2017-07-29 11:35:24 -06001050 upriv->mmc = mmc;
Peng Fan96f04072016-03-25 14:16:56 +08001051
Simon Glass653282b2017-07-29 11:35:24 -06001052 return esdhc_init_common(priv, mmc);
Peng Fan96f04072016-03-25 14:16:56 +08001053}
1054
Simon Glasse7881d82017-07-29 11:35:31 -06001055#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass653282b2017-07-29 11:35:24 -06001056static int fsl_esdhc_get_cd(struct udevice *dev)
1057{
1058 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1059
Simon Glass653282b2017-07-29 11:35:24 -06001060 return esdhc_getcd_common(priv);
1061}
1062
1063static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1064 struct mmc_data *data)
1065{
1066 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1067 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1068
1069 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1070}
1071
1072static int fsl_esdhc_set_ios(struct udevice *dev)
1073{
1074 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1075 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1076
1077 return esdhc_set_ios_common(priv, &plat->mmc);
1078}
1079
1080static const struct dm_mmc_ops fsl_esdhc_ops = {
1081 .get_cd = fsl_esdhc_get_cd,
1082 .send_cmd = fsl_esdhc_send_cmd,
1083 .set_ios = fsl_esdhc_set_ios,
1084};
1085#endif
1086
Peng Fan96f04072016-03-25 14:16:56 +08001087static const struct udevice_id fsl_esdhc_ids[] = {
Yangbo Lua6473f82016-12-07 11:54:31 +08001088 { .compatible = "fsl,esdhc", },
Peng Fan96f04072016-03-25 14:16:56 +08001089 { /* sentinel */ }
1090};
1091
Simon Glass653282b2017-07-29 11:35:24 -06001092#if CONFIG_IS_ENABLED(BLK)
1093static int fsl_esdhc_bind(struct udevice *dev)
1094{
1095 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1096
1097 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1098}
1099#endif
1100
Peng Fan96f04072016-03-25 14:16:56 +08001101U_BOOT_DRIVER(fsl_esdhc) = {
1102 .name = "fsl-esdhc-mmc",
1103 .id = UCLASS_MMC,
1104 .of_match = fsl_esdhc_ids,
Simon Glass653282b2017-07-29 11:35:24 -06001105 .ops = &fsl_esdhc_ops,
Simon Glass653282b2017-07-29 11:35:24 -06001106#if CONFIG_IS_ENABLED(BLK)
1107 .bind = fsl_esdhc_bind,
1108#endif
Peng Fan96f04072016-03-25 14:16:56 +08001109 .probe = fsl_esdhc_probe,
Simon Glasse88e1d92017-07-29 11:35:21 -06001110 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fan96f04072016-03-25 14:16:56 +08001111 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1112};
1113#endif