blob: 28d2312ef75545aee138c7d9e3d74834245fc43f [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
Andy Fleming50586ef2008-10-30 16:47:16 -0500398 /* Wait for the command to complete */
Fabio Estevam29c2edb2018-11-19 10:31:53 -0200399 start = get_timer(0);
400 while (!(esdhc_read32(&regs->irqstat) & flags)) {
401 if (get_timer(start) > 1000) {
402 err = -ETIMEDOUT;
403 goto out;
404 }
405 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500406
Stefano Babicc67bee12010-02-05 15:11:27 +0100407 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500408
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500409 if (irqstat & CMD_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900410 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500411 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000412 }
413
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500414 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900415 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500416 goto out;
417 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500418
Dirk Behme7a5b8022012-03-26 03:13:05 +0000419 /* Workaround for ESDHC errata ENGcm03648 */
420 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800421 int timeout = 6000;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000422
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800423 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000424 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
425 PRSSTAT_DAT0)) {
426 udelay(100);
427 timeout--;
428 }
429
430 if (timeout <= 0) {
431 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900432 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500433 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000434 }
435 }
436
Andy Fleming50586ef2008-10-30 16:47:16 -0500437 /* Copy the response to the response buffer */
438 if (cmd->resp_type & MMC_RSP_136) {
439 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
440
Stefano Babicc67bee12010-02-05 15:11:27 +0100441 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
442 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
443 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
444 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530445 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
446 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
447 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
448 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500449 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100450 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500451
452 /* Wait until all of the blocks are transferred */
453 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530454#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass09b465f2017-07-29 11:35:17 -0600455 esdhc_pio_read_write(priv, data);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530456#else
Andy Fleming50586ef2008-10-30 16:47:16 -0500457 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100458 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500459
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500460 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900461 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500462 goto out;
463 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000464
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500465 if (irqstat & DATA_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900466 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500467 goto out;
468 }
Yinbo Zhu6f883e52019-07-16 15:09:11 +0800469 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li71689772014-02-20 18:00:57 +0800470
Peng Fan4683b222015-06-25 10:32:26 +0800471 /*
472 * Need invalidate the dcache here again to avoid any
473 * cache-fill during the DMA operations such as the
474 * speculative pre-fetching etc.
475 */
Angelo Dureghello1f15cb82019-01-19 10:40:38 +0100476 if (data->flags & MMC_DATA_READ) {
Eric Nelson54899fc2013-04-03 12:31:56 +0000477 check_and_invalidate_dcache_range(cmd, data);
Angelo Dureghello1f15cb82019-01-19 10:40:38 +0100478 }
Ye.Li71689772014-02-20 18:00:57 +0800479#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500480 }
481
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500482out:
483 /* Reset CMD and DATA portions on error */
484 if (err) {
485 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
486 SYSCTL_RSTC);
487 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
488 ;
489
490 if (data) {
491 esdhc_write32(&regs->sysctl,
492 esdhc_read32(&regs->sysctl) |
493 SYSCTL_RSTD);
494 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
495 ;
496 }
497 }
498
Stefano Babicc67bee12010-02-05 15:11:27 +0100499 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500500
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500501 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500502}
503
Simon Glass09b465f2017-07-29 11:35:17 -0600504static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500505{
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100506 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200507 int div = 1;
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200508 int pre_div = 2;
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200509 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
Yinbo Zhu6f883e52019-07-16 15:09:11 +0800510 unsigned int sdhc_clk = priv->sdhc_clk;
511 u32 time_out;
512 u32 value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500513 uint clk;
514
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200515 if (clock < mmc->cfg->f_min)
516 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100517
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200518 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
519 pre_div *= 2;
Andy Fleming50586ef2008-10-30 16:47:16 -0500520
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200521 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
522 div++;
Andy Fleming50586ef2008-10-30 16:47:16 -0500523
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200524 pre_div >>= 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500525 div -= 1;
526
527 clk = (pre_div << 8) | (div << 4);
528
Kumar Galacc4d1222010-03-18 15:51:05 -0500529 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicc67bee12010-02-05 15:11:27 +0100530
531 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500532
Yinbo Zhu6f883e52019-07-16 15:09:11 +0800533 time_out = 20;
534 value = PRSSTAT_SDSTB;
535 while (!(esdhc_read32(&regs->prsstat) & value)) {
536 if (time_out == 0) {
537 printf("fsl_esdhc: Internal clock never stabilised.\n");
538 break;
539 }
540 time_out--;
541 mdelay(1);
542 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500543
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700544 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Andy Fleming50586ef2008-10-30 16:47:16 -0500545}
546
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800547#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
Simon Glass09b465f2017-07-29 11:35:17 -0600548static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800549{
Peng Fan96f04072016-03-25 14:16:56 +0800550 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800551 u32 value;
552 u32 time_out;
553
554 value = esdhc_read32(&regs->sysctl);
555
556 if (enable)
557 value |= SYSCTL_CKEN;
558 else
559 value &= ~SYSCTL_CKEN;
560
561 esdhc_write32(&regs->sysctl, value);
562
563 time_out = 20;
564 value = PRSSTAT_SDSTB;
565 while (!(esdhc_read32(&regs->prsstat) & value)) {
566 if (time_out == 0) {
567 printf("fsl_esdhc: Internal clock never stabilised.\n");
568 break;
569 }
570 time_out--;
571 mdelay(1);
572 }
573}
574#endif
575
Simon Glass9586aa62017-07-29 11:35:18 -0600576static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500577{
Peng Fan96f04072016-03-25 14:16:56 +0800578 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500579
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800580#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
581 /* Select to use peripheral clock */
Simon Glass09b465f2017-07-29 11:35:17 -0600582 esdhc_clock_control(priv, false);
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800583 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_PCS);
Simon Glass09b465f2017-07-29 11:35:17 -0600584 esdhc_clock_control(priv, true);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800585#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500586 /* Set the clock speed */
Peng Fan51313b42018-01-21 19:00:24 +0800587 if (priv->clock != mmc->clock)
588 set_sysctl(priv, mmc, mmc->clock);
589
Andy Fleming50586ef2008-10-30 16:47:16 -0500590 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100591 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500592
593 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100594 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500595 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100596 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
597
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900598 return 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500599}
600
Simon Glass9586aa62017-07-29 11:35:18 -0600601static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500602{
Peng Fan96f04072016-03-25 14:16:56 +0800603 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass201e8282017-07-29 11:35:20 -0600604 ulong start;
Andy Fleming50586ef2008-10-30 16:47:16 -0500605
Stefano Babicc67bee12010-02-05 15:11:27 +0100606 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200607 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100608
609 /* Wait until the controller is available */
Simon Glass201e8282017-07-29 11:35:20 -0600610 start = get_timer(0);
611 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
612 if (get_timer(start) > 1000)
613 return -ETIMEDOUT;
614 }
Stefano Babicc67bee12010-02-05 15:11:27 +0100615
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530616 /* Enable cache snooping */
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800617 esdhc_write32(&regs->esdhcctl, 0x00000040);
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530618
Dirk Behmea61da722013-07-15 15:44:29 +0200619 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleming50586ef2008-10-30 16:47:16 -0500620
621 /* Set the initial clock speed */
Jaehoon Chung65117182018-01-26 19:25:29 +0900622 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
Andy Fleming50586ef2008-10-30 16:47:16 -0500623
624 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100625 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500626
627 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100628 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500629
Stefano Babicc67bee12010-02-05 15:11:27 +0100630 /* Set timout to the maximum value */
631 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500632
Thierry Redingd48d2e22012-01-02 01:15:38 +0000633 return 0;
634}
Andy Fleming50586ef2008-10-30 16:47:16 -0500635
Simon Glass9586aa62017-07-29 11:35:18 -0600636static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Redingd48d2e22012-01-02 01:15:38 +0000637{
Peng Fan96f04072016-03-25 14:16:56 +0800638 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Redingd48d2e22012-01-02 01:15:38 +0000639 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100640
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +0800641#ifdef CONFIG_ESDHC_DETECT_QUIRK
642 if (CONFIG_ESDHC_DETECT_QUIRK)
643 return 1;
644#endif
Peng Fan96f04072016-03-25 14:16:56 +0800645
Simon Glass653282b2017-07-29 11:35:24 -0600646#if CONFIG_IS_ENABLED(DM_MMC)
Peng Fan96f04072016-03-25 14:16:56 +0800647 if (priv->non_removable)
648 return 1;
Yangbo Lufc8048a2016-12-07 11:54:30 +0800649#endif
Peng Fan96f04072016-03-25 14:16:56 +0800650
Thierry Redingd48d2e22012-01-02 01:15:38 +0000651 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
652 udelay(1000);
653
654 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500655}
656
Simon Glass446e0772017-07-29 11:35:19 -0600657static int esdhc_reset(struct fsl_esdhc *regs)
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500658{
Simon Glass446e0772017-07-29 11:35:19 -0600659 ulong start;
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500660
661 /* reset the controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200662 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500663
664 /* hardware clears the bit when it is done */
Simon Glass446e0772017-07-29 11:35:19 -0600665 start = get_timer(0);
666 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
667 if (get_timer(start) > 100) {
668 printf("MMC/SD: Reset never completed.\n");
669 return -ETIMEDOUT;
670 }
671 }
672
673 return 0;
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500674}
675
Simon Glasse7881d82017-07-29 11:35:31 -0600676#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glass9586aa62017-07-29 11:35:18 -0600677static int esdhc_getcd(struct mmc *mmc)
678{
679 struct fsl_esdhc_priv *priv = mmc->priv;
680
681 return esdhc_getcd_common(priv);
682}
683
684static int esdhc_init(struct mmc *mmc)
685{
686 struct fsl_esdhc_priv *priv = mmc->priv;
687
688 return esdhc_init_common(priv, mmc);
689}
690
691static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
692 struct mmc_data *data)
693{
694 struct fsl_esdhc_priv *priv = mmc->priv;
695
696 return esdhc_send_cmd_common(priv, mmc, cmd, data);
697}
698
699static int esdhc_set_ios(struct mmc *mmc)
700{
701 struct fsl_esdhc_priv *priv = mmc->priv;
702
703 return esdhc_set_ios_common(priv, mmc);
704}
705
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200706static const struct mmc_ops esdhc_ops = {
Simon Glass9586aa62017-07-29 11:35:18 -0600707 .getcd = esdhc_getcd,
708 .init = esdhc_init,
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200709 .send_cmd = esdhc_send_cmd,
710 .set_ios = esdhc_set_ios,
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200711};
Simon Glass653282b2017-07-29 11:35:24 -0600712#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200713
Simon Glasse88e1d92017-07-29 11:35:21 -0600714static int fsl_esdhc_init(struct fsl_esdhc_priv *priv,
715 struct fsl_esdhc_plat *plat)
Andy Fleming50586ef2008-10-30 16:47:16 -0500716{
Simon Glasse88e1d92017-07-29 11:35:21 -0600717 struct mmc_config *cfg;
Stefano Babicc67bee12010-02-05 15:11:27 +0100718 struct fsl_esdhc *regs;
Li Yang030955c2010-11-25 17:06:09 +0000719 u32 caps, voltage_caps;
Simon Glass446e0772017-07-29 11:35:19 -0600720 int ret;
Andy Fleming50586ef2008-10-30 16:47:16 -0500721
Peng Fan96f04072016-03-25 14:16:56 +0800722 if (!priv)
723 return -EINVAL;
Stefano Babicc67bee12010-02-05 15:11:27 +0100724
Peng Fan96f04072016-03-25 14:16:56 +0800725 regs = priv->esdhc_regs;
Stefano Babicc67bee12010-02-05 15:11:27 +0100726
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500727 /* First reset the eSDHC controller */
Simon Glass446e0772017-07-29 11:35:19 -0600728 ret = esdhc_reset(regs);
729 if (ret)
730 return ret;
Jerry Huang48bb3bb2010-03-18 15:57:06 -0500731
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800732 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN |
733 SYSCTL_IPGEN | SYSCTL_CKEN);
Peng Fan32a91792017-06-12 17:50:53 +0800734
Ye.Lia3d6e382014-11-04 15:35:49 +0800735 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Simon Glasse88e1d92017-07-29 11:35:21 -0600736 cfg = &plat->cfg;
Simon Glass653282b2017-07-29 11:35:24 -0600737#ifndef CONFIG_DM_MMC
Simon Glasse88e1d92017-07-29 11:35:21 -0600738 memset(cfg, '\0', sizeof(*cfg));
Simon Glass653282b2017-07-29 11:35:24 -0600739#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200740
Li Yang030955c2010-11-25 17:06:09 +0000741 voltage_caps = 0;
Wang Huan19060bd2014-09-05 13:52:40 +0800742 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -0600743
744#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
745 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
746 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
747#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +0800748
749/* T4240 host controller capabilities register should have VS33 bit */
750#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
751 caps = caps | ESDHC_HOSTCAPBLT_VS33;
752#endif
753
Andy Fleming50586ef2008-10-30 16:47:16 -0500754 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yang030955c2010-11-25 17:06:09 +0000755 voltage_caps |= MMC_VDD_165_195;
Andy Fleming50586ef2008-10-30 16:47:16 -0500756 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yang030955c2010-11-25 17:06:09 +0000757 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleming50586ef2008-10-30 16:47:16 -0500758 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yang030955c2010-11-25 17:06:09 +0000759 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
760
Simon Glasse88e1d92017-07-29 11:35:21 -0600761 cfg->name = "FSL_SDHC";
Simon Glasse7881d82017-07-29 11:35:31 -0600762#if !CONFIG_IS_ENABLED(DM_MMC)
Simon Glasse88e1d92017-07-29 11:35:21 -0600763 cfg->ops = &esdhc_ops;
Simon Glass653282b2017-07-29 11:35:24 -0600764#endif
Li Yang030955c2010-11-25 17:06:09 +0000765#ifdef CONFIG_SYS_SD_VOLTAGE
Simon Glasse88e1d92017-07-29 11:35:21 -0600766 cfg->voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yang030955c2010-11-25 17:06:09 +0000767#else
Simon Glasse88e1d92017-07-29 11:35:21 -0600768 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +0000769#endif
Simon Glasse88e1d92017-07-29 11:35:21 -0600770 if ((cfg->voltages & voltage_caps) == 0) {
Li Yang030955c2010-11-25 17:06:09 +0000771 printf("voltage not supported by controller\n");
772 return -1;
773 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500774
Peng Fan96f04072016-03-25 14:16:56 +0800775 if (priv->bus_width == 8)
Simon Glasse88e1d92017-07-29 11:35:21 -0600776 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +0800777 else if (priv->bus_width == 4)
Simon Glasse88e1d92017-07-29 11:35:21 -0600778 cfg->host_caps = MMC_MODE_4BIT;
Peng Fan96f04072016-03-25 14:16:56 +0800779
Simon Glasse88e1d92017-07-29 11:35:21 -0600780 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500781#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Simon Glasse88e1d92017-07-29 11:35:21 -0600782 cfg->host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsev0e1bf612015-01-20 10:16:44 -0500783#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500784
Peng Fan96f04072016-03-25 14:16:56 +0800785 if (priv->bus_width > 0) {
786 if (priv->bus_width < 8)
Simon Glasse88e1d92017-07-29 11:35:21 -0600787 cfg->host_caps &= ~MMC_MODE_8BIT;
Peng Fan96f04072016-03-25 14:16:56 +0800788 if (priv->bus_width < 4)
Simon Glasse88e1d92017-07-29 11:35:21 -0600789 cfg->host_caps &= ~MMC_MODE_4BIT;
Abbas Razaaad46592013-03-25 09:13:34 +0000790 }
791
Andy Fleming50586ef2008-10-30 16:47:16 -0500792 if (caps & ESDHC_HOSTCAPBLT_HSS)
Simon Glasse88e1d92017-07-29 11:35:21 -0600793 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -0500794
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800795#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
796 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Simon Glasse88e1d92017-07-29 11:35:21 -0600797 cfg->host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangd47e3d22014-01-10 13:52:18 +0800798#endif
799
Simon Glasse88e1d92017-07-29 11:35:21 -0600800 cfg->f_min = 400000;
Peng Fan51313b42018-01-21 19:00:24 +0800801 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Andy Fleming50586ef2008-10-30 16:47:16 -0500802
Simon Glasse88e1d92017-07-29 11:35:21 -0600803 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200804
Peng Fan96f04072016-03-25 14:16:56 +0800805 return 0;
806}
807
Simon Glass52489302017-07-29 11:35:28 -0600808#if !CONFIG_IS_ENABLED(DM_MMC)
Jagan Teki2e87c442017-05-12 17:18:20 +0530809static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
810 struct fsl_esdhc_priv *priv)
811{
812 if (!cfg || !priv)
813 return -EINVAL;
814
815 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
816 priv->bus_width = cfg->max_bus_width;
817 priv->sdhc_clk = cfg->sdhc_clk;
818 priv->wp_enable = cfg->wp_enable;
819
820 return 0;
821};
822
Peng Fan96f04072016-03-25 14:16:56 +0800823int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
824{
Simon Glasse88e1d92017-07-29 11:35:21 -0600825 struct fsl_esdhc_plat *plat;
Peng Fan96f04072016-03-25 14:16:56 +0800826 struct fsl_esdhc_priv *priv;
Simon Glassd6eb25e2017-07-29 11:35:22 -0600827 struct mmc *mmc;
Peng Fan96f04072016-03-25 14:16:56 +0800828 int ret;
829
830 if (!cfg)
831 return -EINVAL;
832
833 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
834 if (!priv)
835 return -ENOMEM;
Simon Glasse88e1d92017-07-29 11:35:21 -0600836 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
837 if (!plat) {
838 free(priv);
839 return -ENOMEM;
840 }
Peng Fan96f04072016-03-25 14:16:56 +0800841
842 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
843 if (ret) {
844 debug("%s xlate failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -0600845 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +0800846 free(priv);
847 return ret;
848 }
849
Simon Glasse88e1d92017-07-29 11:35:21 -0600850 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +0800851 if (ret) {
852 debug("%s init failure\n", __func__);
Simon Glasse88e1d92017-07-29 11:35:21 -0600853 free(plat);
Peng Fan96f04072016-03-25 14:16:56 +0800854 free(priv);
855 return ret;
856 }
857
Simon Glassd6eb25e2017-07-29 11:35:22 -0600858 mmc = mmc_create(&plat->cfg, priv);
859 if (!mmc)
860 return -EIO;
861
862 priv->mmc = mmc;
863
Andy Fleming50586ef2008-10-30 16:47:16 -0500864 return 0;
865}
866
867int fsl_esdhc_mmc_init(bd_t *bis)
868{
Stefano Babicc67bee12010-02-05 15:11:27 +0100869 struct fsl_esdhc_cfg *cfg;
870
Fabio Estevam88227a12012-12-27 08:51:08 +0000871 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicc67bee12010-02-05 15:11:27 +0100872 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glasse9adeca2012-12-13 20:49:05 +0000873 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicc67bee12010-02-05 15:11:27 +0100874 return fsl_esdhc_initialize(bis, cfg);
Andy Fleming50586ef2008-10-30 16:47:16 -0500875}
Jagan Teki2e87c442017-05-12 17:18:20 +0530876#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400877
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800878#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
879void mmc_adapter_card_type_ident(void)
880{
881 u8 card_id;
882 u8 value;
883
884 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
885 gd->arch.sdhc_adapter = card_id;
886
887 switch (card_id) {
888 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lucdc69552015-09-17 10:27:12 +0800889 value = QIXIS_READ(brdcfg[5]);
890 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
891 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800892 break;
893 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Lubf50be82015-09-17 10:27:48 +0800894 value = QIXIS_READ(pwr_ctl[1]);
895 value |= QIXIS_EVDD_BY_SDHC_VS;
896 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800897 break;
898 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
899 value = QIXIS_READ(brdcfg[5]);
900 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
901 QIXIS_WRITE(brdcfg[5], value);
902 break;
903 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
904 break;
905 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
906 break;
907 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
908 break;
909 case QIXIS_ESDHC_NO_ADAPTER:
910 break;
911 default:
912 break;
913 }
914}
915#endif
916
Stefano Babicc67bee12010-02-05 15:11:27 +0100917#ifdef CONFIG_OF_LIBFDT
Yangbo Lufce1e162017-01-17 10:43:54 +0800918__weak int esdhc_status_fixup(void *blob, const char *compat)
919{
920#ifdef CONFIG_FSL_ESDHC_PIN_MUX
921 if (!hwconfig("esdhc")) {
922 do_fixup_by_compat(blob, compat, "status", "disabled",
923 sizeof("disabled"), 1);
924 return 1;
925 }
926#endif
Yangbo Lufce1e162017-01-17 10:43:54 +0800927 return 0;
928}
929
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400930void fdt_fixup_esdhc(void *blob, bd_t *bd)
931{
932 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400933
Yangbo Lufce1e162017-01-17 10:43:54 +0800934 if (esdhc_status_fixup(blob, compat))
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800935 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400936
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800937#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
938 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
939 gd->arch.sdhc_clk, 1);
940#else
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400941 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000942 gd->arch.sdhc_clk, 1);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800943#endif
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800944#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
945 do_fixup_by_compat_u32(blob, compat, "adapter-type",
946 (u32)(gd->arch.sdhc_adapter), 1);
947#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400948}
Stefano Babicc67bee12010-02-05 15:11:27 +0100949#endif
Peng Fan96f04072016-03-25 14:16:56 +0800950
Simon Glass653282b2017-07-29 11:35:24 -0600951#if CONFIG_IS_ENABLED(DM_MMC)
Yinbo Zhub512d072019-04-11 11:01:46 +0000952#ifndef CONFIG_PPC
Peng Fan96f04072016-03-25 14:16:56 +0800953#include <asm/arch/clock.h>
Yinbo Zhub512d072019-04-11 11:01:46 +0000954#endif
Peng Fan96f04072016-03-25 14:16:56 +0800955static int fsl_esdhc_probe(struct udevice *dev)
956{
957 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glasse88e1d92017-07-29 11:35:21 -0600958 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fan96f04072016-03-25 14:16:56 +0800959 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Peng Fan96f04072016-03-25 14:16:56 +0800960 fdt_addr_t addr;
961 unsigned int val;
Simon Glass653282b2017-07-29 11:35:24 -0600962 struct mmc *mmc;
Yangbo Lu66fa0352019-05-23 11:05:46 +0800963#if !CONFIG_IS_ENABLED(BLK)
964 struct blk_desc *bdesc;
965#endif
Peng Fan96f04072016-03-25 14:16:56 +0800966 int ret;
967
Simon Glass4aac33f2017-07-29 11:35:23 -0600968 addr = dev_read_addr(dev);
Peng Fan96f04072016-03-25 14:16:56 +0800969 if (addr == FDT_ADDR_T_NONE)
970 return -EINVAL;
Yinbo Zhub69e1d02019-04-11 11:01:50 +0000971#ifdef CONFIG_PPC
972 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
973#else
Peng Fan96f04072016-03-25 14:16:56 +0800974 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yinbo Zhub69e1d02019-04-11 11:01:50 +0000975#endif
Peng Fan96f04072016-03-25 14:16:56 +0800976 priv->dev = dev;
977
Simon Glass4aac33f2017-07-29 11:35:23 -0600978 val = dev_read_u32_default(dev, "bus-width", -1);
Peng Fan96f04072016-03-25 14:16:56 +0800979 if (val == 8)
980 priv->bus_width = 8;
981 else if (val == 4)
982 priv->bus_width = 4;
983 else
984 priv->bus_width = 1;
985
Simon Glass4aac33f2017-07-29 11:35:23 -0600986 if (dev_read_bool(dev, "non-removable")) {
Peng Fan96f04072016-03-25 14:16:56 +0800987 priv->non_removable = 1;
988 } else {
989 priv->non_removable = 0;
Peng Fan96f04072016-03-25 14:16:56 +0800990 }
991
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800992 priv->wp_enable = 1;
Peng Fanb60f1452017-02-22 16:21:55 +0800993
Peng Fan3cb14502018-10-18 14:28:35 +0200994 if (IS_ENABLED(CONFIG_CLK)) {
995 /* Assigned clock already set clock */
996 ret = clk_get_by_name(dev, "per", &priv->per_clk);
997 if (ret) {
998 printf("Failed to get per_clk\n");
999 return ret;
1000 }
1001 ret = clk_enable(&priv->per_clk);
1002 if (ret) {
1003 printf("Failed to enable per_clk\n");
1004 return ret;
1005 }
1006
1007 priv->sdhc_clk = clk_get_rate(&priv->per_clk);
1008 } else {
Yinbo Zhub512d072019-04-11 11:01:46 +00001009#ifndef CONFIG_PPC
Peng Fan3cb14502018-10-18 14:28:35 +02001010 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
Yinbo Zhub512d072019-04-11 11:01:46 +00001011#else
1012 priv->sdhc_clk = gd->arch.sdhc_clk;
1013#endif
Peng Fan3cb14502018-10-18 14:28:35 +02001014 if (priv->sdhc_clk <= 0) {
1015 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1016 return -EINVAL;
1017 }
Peng Fan96f04072016-03-25 14:16:56 +08001018 }
1019
Simon Glasse88e1d92017-07-29 11:35:21 -06001020 ret = fsl_esdhc_init(priv, plat);
Peng Fan96f04072016-03-25 14:16:56 +08001021 if (ret) {
1022 dev_err(dev, "fsl_esdhc_init failure\n");
1023 return ret;
1024 }
1025
Yinbo Zhu6f883e52019-07-16 15:09:11 +08001026 mmc_of_parse(dev, &plat->cfg);
1027
Simon Glass653282b2017-07-29 11:35:24 -06001028 mmc = &plat->mmc;
1029 mmc->cfg = &plat->cfg;
1030 mmc->dev = dev;
Yangbo Lu66fa0352019-05-23 11:05:46 +08001031#if !CONFIG_IS_ENABLED(BLK)
1032 mmc->priv = priv;
1033
1034 /* Setup dsr related values */
1035 mmc->dsr_imp = 0;
1036 mmc->dsr = ESDHC_DRIVER_STAGE_VALUE;
1037 /* Setup the universal parts of the block interface just once */
1038 bdesc = mmc_get_blk_desc(mmc);
1039 bdesc->if_type = IF_TYPE_MMC;
1040 bdesc->removable = 1;
1041 bdesc->devnum = mmc_get_next_devnum();
1042 bdesc->block_read = mmc_bread;
1043 bdesc->block_write = mmc_bwrite;
1044 bdesc->block_erase = mmc_berase;
1045
1046 /* setup initial part type */
1047 bdesc->part_type = mmc->cfg->part_type;
1048 mmc_list_add(mmc);
1049#endif
1050
Simon Glass653282b2017-07-29 11:35:24 -06001051 upriv->mmc = mmc;
Peng Fan96f04072016-03-25 14:16:56 +08001052
Simon Glass653282b2017-07-29 11:35:24 -06001053 return esdhc_init_common(priv, mmc);
Peng Fan96f04072016-03-25 14:16:56 +08001054}
1055
Simon Glasse7881d82017-07-29 11:35:31 -06001056#if CONFIG_IS_ENABLED(DM_MMC)
Simon Glass653282b2017-07-29 11:35:24 -06001057static int fsl_esdhc_get_cd(struct udevice *dev)
1058{
1059 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1060
Simon Glass653282b2017-07-29 11:35:24 -06001061 return esdhc_getcd_common(priv);
1062}
1063
1064static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
1065 struct mmc_data *data)
1066{
1067 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1068 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1069
1070 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
1071}
1072
1073static int fsl_esdhc_set_ios(struct udevice *dev)
1074{
1075 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1076 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
1077
1078 return esdhc_set_ios_common(priv, &plat->mmc);
1079}
1080
1081static const struct dm_mmc_ops fsl_esdhc_ops = {
1082 .get_cd = fsl_esdhc_get_cd,
1083 .send_cmd = fsl_esdhc_send_cmd,
1084 .set_ios = fsl_esdhc_set_ios,
Yinbo Zhu6f883e52019-07-16 15:09:11 +08001085#ifdef MMC_SUPPORTS_TUNING
1086 .execute_tuning = fsl_esdhc_execute_tuning,
1087#endif
Simon Glass653282b2017-07-29 11:35:24 -06001088};
1089#endif
1090
Peng Fan96f04072016-03-25 14:16:56 +08001091static const struct udevice_id fsl_esdhc_ids[] = {
Yangbo Lua6473f82016-12-07 11:54:31 +08001092 { .compatible = "fsl,esdhc", },
Peng Fan96f04072016-03-25 14:16:56 +08001093 { /* sentinel */ }
1094};
1095
Simon Glass653282b2017-07-29 11:35:24 -06001096#if CONFIG_IS_ENABLED(BLK)
1097static int fsl_esdhc_bind(struct udevice *dev)
1098{
1099 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
1100
1101 return mmc_bind(dev, &plat->mmc, &plat->cfg);
1102}
1103#endif
1104
Peng Fan96f04072016-03-25 14:16:56 +08001105U_BOOT_DRIVER(fsl_esdhc) = {
1106 .name = "fsl-esdhc-mmc",
1107 .id = UCLASS_MMC,
1108 .of_match = fsl_esdhc_ids,
Simon Glass653282b2017-07-29 11:35:24 -06001109 .ops = &fsl_esdhc_ops,
Simon Glass653282b2017-07-29 11:35:24 -06001110#if CONFIG_IS_ENABLED(BLK)
1111 .bind = fsl_esdhc_bind,
1112#endif
Peng Fan96f04072016-03-25 14:16:56 +08001113 .probe = fsl_esdhc_probe,
Simon Glasse88e1d92017-07-29 11:35:21 -06001114 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fan96f04072016-03-25 14:16:56 +08001115 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1116};
1117#endif