blob: 1e7d606cd819c0c04bab04f2ddd66fc02b76b58b [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>
Simon Glass1eb69ae2019-11-14 12:57:39 -070015#include <cpu_func.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
Andy Fleming50586ef2008-10-30 16:47:16 -050026DECLARE_GLOBAL_DATA_PTR;
27
28struct fsl_esdhc {
Haijun.Zhang511948b2013-10-30 11:37:55 +080029 uint dsaddr; /* SDMA system address register */
30 uint blkattr; /* Block attributes register */
31 uint cmdarg; /* Command argument register */
32 uint xfertyp; /* Transfer type register */
33 uint cmdrsp0; /* Command response 0 register */
34 uint cmdrsp1; /* Command response 1 register */
35 uint cmdrsp2; /* Command response 2 register */
36 uint cmdrsp3; /* Command response 3 register */
37 uint datport; /* Buffer data port register */
38 uint prsstat; /* Present state register */
39 uint proctl; /* Protocol control register */
40 uint sysctl; /* System Control Register */
41 uint irqstat; /* Interrupt status register */
42 uint irqstaten; /* Interrupt status enable register */
43 uint irqsigen; /* Interrupt signal enable register */
44 uint autoc12err; /* Auto CMD error status register */
45 uint hostcapblt; /* Host controller capabilities register */
46 uint wml; /* Watermark level register */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080047 char reserved1[8]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080048 uint fevt; /* Force event register */
49 uint admaes; /* ADMA error status register */
50 uint adsaddr; /* ADMA system address register */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080051 char reserved2[160];
Haijun.Zhang511948b2013-10-30 11:37:55 +080052 uint hostver; /* Host controller version register */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080053 char reserved3[4]; /* reserved */
Peng Fan59d37822018-01-21 19:00:22 +080054 uint dmaerraddr; /* DMA error address register */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080055 char reserved4[4]; /* reserved */
Peng Fan59d37822018-01-21 19:00:22 +080056 uint dmaerrattr; /* DMA error attribute register */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080057 char reserved5[4]; /* reserved */
Haijun.Zhang511948b2013-10-30 11:37:55 +080058 uint hostcapblt2; /* Host controller capabilities register 2 */
Yangbo Lu4d8ff422019-06-21 11:42:29 +080059 char reserved6[756]; /* reserved */
60 uint esdhcctl; /* eSDHC control register */
Andy Fleming50586ef2008-10-30 16:47:16 -050061};
62
Simon Glasse88e1d92017-07-29 11:35:21 -060063struct fsl_esdhc_plat {
64 struct mmc_config cfg;
65 struct mmc mmc;
66};
67
Peng Fan96f04072016-03-25 14:16:56 +080068/**
69 * struct fsl_esdhc_priv
70 *
71 * @esdhc_regs: registers of the sdhc controller
72 * @sdhc_clk: Current clk of the sdhc controller
73 * @bus_width: bus width, 1bit, 4bit or 8bit
74 * @cfg: mmc config
75 * @mmc: mmc
76 * Following is used when Driver Model is enabled for MMC
77 * @dev: pointer for the device
Peng Fan96f04072016-03-25 14:16:56 +080078 * @cd_gpio: gpio for card detection
Peng Fan14831512016-06-15 10:53:02 +080079 * @wp_gpio: gpio for write protection
Peng Fan96f04072016-03-25 14:16:56 +080080 */
81struct fsl_esdhc_priv {
82 struct fsl_esdhc *esdhc_regs;
83 unsigned int sdhc_clk;
Peng Fan51313b42018-01-21 19:00:24 +080084 unsigned int clock;
Yangbo Lu41dec2f2019-10-21 18:09:07 +080085#if !CONFIG_IS_ENABLED(DM_MMC)
Peng Fan96f04072016-03-25 14:16:56 +080086 struct mmc *mmc;
Simon Glass653282b2017-07-29 11:35:24 -060087#endif
Peng Fan96f04072016-03-25 14:16:56 +080088 struct udevice *dev;
Peng Fan96f04072016-03-25 14:16:56 +080089};
90
Andy Fleming50586ef2008-10-30 16:47:16 -050091/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipseafa90a2012-10-29 13:34:44 +000092static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -050093{
94 uint xfertyp = 0;
95
96 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +053097 xfertyp |= XFERTYP_DPSEL;
98#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
99 xfertyp |= XFERTYP_DMAEN;
100#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500101 if (data->blocks > 1) {
102 xfertyp |= XFERTYP_MSBSEL;
103 xfertyp |= XFERTYP_BCEN;
Jerry Huangd621da02011-01-06 23:42:19 -0600104#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
105 xfertyp |= XFERTYP_AC12EN;
106#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500107 }
108
109 if (data->flags & MMC_DATA_READ)
110 xfertyp |= XFERTYP_DTDSEL;
111 }
112
113 if (cmd->resp_type & MMC_RSP_CRC)
114 xfertyp |= XFERTYP_CCCEN;
115 if (cmd->resp_type & MMC_RSP_OPCODE)
116 xfertyp |= XFERTYP_CICEN;
117 if (cmd->resp_type & MMC_RSP_136)
118 xfertyp |= XFERTYP_RSPTYP_136;
119 else if (cmd->resp_type & MMC_RSP_BUSY)
120 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
121 else if (cmd->resp_type & MMC_RSP_PRESENT)
122 xfertyp |= XFERTYP_RSPTYP_48;
123
Jason Liu4571de32011-03-22 01:32:31 +0000124 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
125 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lu25503442016-01-21 17:33:19 +0800126
Andy Fleming50586ef2008-10-30 16:47:16 -0500127 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
128}
129
Dipen Dudhat77c14582009-10-05 15:41:58 +0530130#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
131/*
132 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
133 */
Simon Glass09b465f2017-07-29 11:35:17 -0600134static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv,
135 struct mmc_data *data)
Dipen Dudhat77c14582009-10-05 15:41:58 +0530136{
Peng Fan96f04072016-03-25 14:16:56 +0800137 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530138 uint blocks;
139 char *buffer;
140 uint databuf;
141 uint size;
142 uint irqstat;
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100143 ulong start;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530144
145 if (data->flags & MMC_DATA_READ) {
146 blocks = data->blocks;
147 buffer = data->dest;
148 while (blocks) {
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100149 start = get_timer(0);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530150 size = data->blocksize;
151 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100152 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)) {
153 if (get_timer(start) > PIO_TIMEOUT) {
154 printf("\nData Read Failed in PIO Mode.");
155 return;
156 }
Dipen Dudhat77c14582009-10-05 15:41:58 +0530157 }
158 while (size && (!(irqstat & IRQSTAT_TC))) {
159 udelay(100); /* Wait before last byte transfer complete */
160 irqstat = esdhc_read32(&regs->irqstat);
161 databuf = in_le32(&regs->datport);
162 *((uint *)buffer) = databuf;
163 buffer += 4;
164 size -= 4;
165 }
166 blocks--;
167 }
168 } else {
169 blocks = data->blocks;
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200170 buffer = (char *)data->src;
Dipen Dudhat77c14582009-10-05 15:41:58 +0530171 while (blocks) {
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100172 start = get_timer(0);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530173 size = data->blocksize;
174 irqstat = esdhc_read32(&regs->irqstat);
Benoît Thébaudeaubcfb3652017-10-29 22:08:58 +0100175 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)) {
176 if (get_timer(start) > PIO_TIMEOUT) {
177 printf("\nData Write Failed in PIO Mode.");
178 return;
179 }
Dipen Dudhat77c14582009-10-05 15:41:58 +0530180 }
181 while (size && (!(irqstat & IRQSTAT_TC))) {
182 udelay(100); /* Wait before last byte transfer complete */
183 databuf = *((uint *)buffer);
184 buffer += 4;
185 size -= 4;
186 irqstat = esdhc_read32(&regs->irqstat);
187 out_le32(&regs->datport, databuf);
188 }
189 blocks--;
190 }
191 }
192}
193#endif
194
Simon Glass09b465f2017-07-29 11:35:17 -0600195static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc,
196 struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500197{
Andy Fleming50586ef2008-10-30 16:47:16 -0500198 int timeout;
Peng Fan96f04072016-03-25 14:16:56 +0800199 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800200#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lu8b064602015-03-20 19:28:31 -0700201 dma_addr_t addr;
202#endif
Wolfgang Denk7b43db92010-05-09 23:52:59 +0200203 uint wml_value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500204
205 wml_value = data->blocksize/4;
206
207 if (data->flags & MMC_DATA_READ) {
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530208 if (wml_value > WML_RD_WML_MAX)
209 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500210
Roy Zangab467c52010-02-09 18:23:33 +0800211 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li71689772014-02-20 18:00:57 +0800212#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800213#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lu8b064602015-03-20 19:28:31 -0700214 addr = virt_to_phys((void *)(data->dest));
215 if (upper_32_bits(addr))
216 printf("Error found for upper 32 bits\n");
217 else
218 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
219#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100220 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li71689772014-02-20 18:00:57 +0800221#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700222#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500223 } else {
Ye.Li71689772014-02-20 18:00:57 +0800224#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelsone576bd92012-04-25 14:28:48 +0000225 flush_dcache_range((ulong)data->src,
226 (ulong)data->src+data->blocks
227 *data->blocksize);
Ye.Li71689772014-02-20 18:00:57 +0800228#endif
Priyanka Jain32c8cfb2011-02-09 09:24:10 +0530229 if (wml_value > WML_WR_WML_MAX)
230 wml_value = WML_WR_WML_MAX_VAL;
Yangbo Lu0cc127c2019-10-31 18:54:25 +0800231
232 if (!(esdhc_read32(&regs->prsstat) & PRSSTAT_WPSPL)) {
233 printf("Can not write to locked SD card.\n");
234 return -EINVAL;
Andy Fleming50586ef2008-10-30 16:47:16 -0500235 }
Roy Zangab467c52010-02-09 18:23:33 +0800236
237 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
238 wml_value << 16);
Ye.Li71689772014-02-20 18:00:57 +0800239#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800240#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lu8b064602015-03-20 19:28:31 -0700241 addr = virt_to_phys((void *)(data->src));
242 if (upper_32_bits(addr))
243 printf("Error found for upper 32 bits\n");
244 else
245 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
246#else
Stefano Babicc67bee12010-02-05 15:11:27 +0100247 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li71689772014-02-20 18:00:57 +0800248#endif
Yangbo Lu8b064602015-03-20 19:28:31 -0700249#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500250 }
251
Stefano Babicc67bee12010-02-05 15:11:27 +0100252 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleming50586ef2008-10-30 16:47:16 -0500253
254 /* Calculate the timeout period for data transactions */
Priyanka Jainb71ea332011-03-03 09:18:56 +0530255 /*
256 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
257 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
258 * So, Number of SD Clock cycles for 0.25sec should be minimum
259 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500260 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainb71ea332011-03-03 09:18:56 +0530261 * As 1) >= 2)
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500262 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainb71ea332011-03-03 09:18:56 +0530263 * Taking log2 both the sides
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500264 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530265 * Rounding up to next power of 2
Andrew Gabbasovfb823982014-03-24 02:40:41 -0500266 * => timeout + 13 = log2(mmc->clock/4) + 1
267 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lue978a312015-12-30 14:19:30 +0800268 *
269 * However, the MMC spec "It is strongly recommended for hosts to
270 * implement more than 500ms timeout value even if the card
271 * indicates the 250ms maximum busy length." Even the previous
272 * value of 300ms is known to be insufficient for some cards.
273 * So, we use
274 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainb71ea332011-03-03 09:18:56 +0530275 */
Yangbo Lue978a312015-12-30 14:19:30 +0800276 timeout = fls(mmc->clock/2);
Andy Fleming50586ef2008-10-30 16:47:16 -0500277 timeout -= 13;
278
279 if (timeout > 14)
280 timeout = 14;
281
282 if (timeout < 0)
283 timeout = 0;
284
Kumar Gala5103a032011-01-29 15:36:10 -0600285#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
286 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
287 timeout++;
288#endif
289
Haijun.Zhang1336e2d2014-03-18 17:04:23 +0800290#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
291 timeout = 0xE;
292#endif
Stefano Babicc67bee12010-02-05 15:11:27 +0100293 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500294
295 return 0;
296}
297
Eric Nelsone576bd92012-04-25 14:28:48 +0000298static void check_and_invalidate_dcache_range
299 (struct mmc_cmd *cmd,
300 struct mmc_data *data) {
Yangbo Lu8b064602015-03-20 19:28:31 -0700301 unsigned start = 0;
Yangbo Lucc634e22016-05-12 19:12:58 +0800302 unsigned end = 0;
Eric Nelsone576bd92012-04-25 14:28:48 +0000303 unsigned size = roundup(ARCH_DMA_MINALIGN,
304 data->blocks*data->blocksize);
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800305#if defined(CONFIG_FSL_LAYERSCAPE)
Yangbo Lu8b064602015-03-20 19:28:31 -0700306 dma_addr_t addr;
307
308 addr = virt_to_phys((void *)(data->dest));
309 if (upper_32_bits(addr))
310 printf("Error found for upper 32 bits\n");
311 else
312 start = lower_32_bits(addr);
Yangbo Lucc634e22016-05-12 19:12:58 +0800313#else
314 start = (unsigned)data->dest;
Yangbo Lu8b064602015-03-20 19:28:31 -0700315#endif
Yangbo Lucc634e22016-05-12 19:12:58 +0800316 end = start + size;
Eric Nelsone576bd92012-04-25 14:28:48 +0000317 invalidate_dcache_range(start, end);
318}
Tom Rini10dc7772014-05-23 09:19:05 -0400319
Andy Fleming50586ef2008-10-30 16:47:16 -0500320/*
321 * Sends a command out on the bus. Takes the mmc pointer,
322 * a command pointer, and an optional data pointer.
323 */
Simon Glass9586aa62017-07-29 11:35:18 -0600324static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc,
325 struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleming50586ef2008-10-30 16:47:16 -0500326{
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500327 int err = 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500328 uint xfertyp;
329 uint irqstat;
Peng Fan51313b42018-01-21 19:00:24 +0800330 u32 flags = IRQSTAT_CC | IRQSTAT_CTOE;
Peng Fan96f04072016-03-25 14:16:56 +0800331 struct fsl_esdhc *regs = priv->esdhc_regs;
Fabio Estevam29c2edb2018-11-19 10:31:53 -0200332 unsigned long start;
Andy Fleming50586ef2008-10-30 16:47:16 -0500333
Jerry Huangd621da02011-01-06 23:42:19 -0600334#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
335 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
336 return 0;
337#endif
338
Stefano Babicc67bee12010-02-05 15:11:27 +0100339 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500340
341 sync();
342
343 /* Wait for the bus to be idle */
Stefano Babicc67bee12010-02-05 15:11:27 +0100344 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
345 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
346 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500347
Stefano Babicc67bee12010-02-05 15:11:27 +0100348 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
349 ;
Andy Fleming50586ef2008-10-30 16:47:16 -0500350
351 /* Wait at least 8 SD clock cycles before the next command */
352 /*
353 * Note: This is way more than 8 cycles, but 1ms seems to
354 * resolve timing issues with some cards
355 */
356 udelay(1000);
357
358 /* Set up for a data transfer if we have one */
359 if (data) {
Simon Glass09b465f2017-07-29 11:35:17 -0600360 err = esdhc_setup_data(priv, mmc, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500361 if(err)
362 return err;
Peng Fan4683b222015-06-25 10:32:26 +0800363
364 if (data->flags & MMC_DATA_READ)
365 check_and_invalidate_dcache_range(cmd, data);
Andy Fleming50586ef2008-10-30 16:47:16 -0500366 }
367
368 /* Figure out the transfer arguments */
369 xfertyp = esdhc_xfertyp(cmd, data);
370
Andrew Gabbasov01b77352013-06-11 10:34:22 -0500371 /* Mask all irqs */
372 esdhc_write32(&regs->irqsigen, 0);
373
Andy Fleming50586ef2008-10-30 16:47:16 -0500374 /* Send the command */
Stefano Babicc67bee12010-02-05 15:11:27 +0100375 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
376 esdhc_write32(&regs->xfertyp, xfertyp);
Dirk Behme7a5b8022012-03-26 03:13:05 +0000377
Andy Fleming50586ef2008-10-30 16:47:16 -0500378 /* Wait for the command to complete */
Fabio Estevam29c2edb2018-11-19 10:31:53 -0200379 start = get_timer(0);
380 while (!(esdhc_read32(&regs->irqstat) & flags)) {
381 if (get_timer(start) > 1000) {
382 err = -ETIMEDOUT;
383 goto out;
384 }
385 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500386
Stefano Babicc67bee12010-02-05 15:11:27 +0100387 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500388
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500389 if (irqstat & CMD_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900390 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500391 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000392 }
393
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500394 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900395 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500396 goto out;
397 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500398
Dirk Behme7a5b8022012-03-26 03:13:05 +0000399 /* Workaround for ESDHC errata ENGcm03648 */
400 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800401 int timeout = 6000;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000402
Yangbo Lu253d5bd2015-04-15 10:13:12 +0800403 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behme7a5b8022012-03-26 03:13:05 +0000404 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
405 PRSSTAT_DAT0)) {
406 udelay(100);
407 timeout--;
408 }
409
410 if (timeout <= 0) {
411 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900412 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500413 goto out;
Dirk Behme7a5b8022012-03-26 03:13:05 +0000414 }
415 }
416
Andy Fleming50586ef2008-10-30 16:47:16 -0500417 /* Copy the response to the response buffer */
418 if (cmd->resp_type & MMC_RSP_136) {
419 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
420
Stefano Babicc67bee12010-02-05 15:11:27 +0100421 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
422 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
423 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
424 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincent998be3d2009-04-05 13:30:56 +0530425 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
426 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
427 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
428 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500429 } else
Stefano Babicc67bee12010-02-05 15:11:27 +0100430 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleming50586ef2008-10-30 16:47:16 -0500431
432 /* Wait until all of the blocks are transferred */
433 if (data) {
Dipen Dudhat77c14582009-10-05 15:41:58 +0530434#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
Simon Glass09b465f2017-07-29 11:35:17 -0600435 esdhc_pio_read_write(priv, data);
Dipen Dudhat77c14582009-10-05 15:41:58 +0530436#else
Andy Fleming50586ef2008-10-30 16:47:16 -0500437 do {
Stefano Babicc67bee12010-02-05 15:11:27 +0100438 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleming50586ef2008-10-30 16:47:16 -0500439
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500440 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900441 err = -ETIMEDOUT;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500442 goto out;
443 }
Frans Meulenbroeks63fb5a72010-07-31 04:45:18 +0000444
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500445 if (irqstat & DATA_ERR) {
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900446 err = -ECOMM;
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500447 goto out;
448 }
Yinbo Zhu6f883e52019-07-16 15:09:11 +0800449 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li71689772014-02-20 18:00:57 +0800450
Peng Fan4683b222015-06-25 10:32:26 +0800451 /*
452 * Need invalidate the dcache here again to avoid any
453 * cache-fill during the DMA operations such as the
454 * speculative pre-fetching etc.
455 */
Angelo Dureghello1f15cb82019-01-19 10:40:38 +0100456 if (data->flags & MMC_DATA_READ) {
Eric Nelson54899fc2013-04-03 12:31:56 +0000457 check_and_invalidate_dcache_range(cmd, data);
Angelo Dureghello1f15cb82019-01-19 10:40:38 +0100458 }
Ye.Li71689772014-02-20 18:00:57 +0800459#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500460 }
461
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500462out:
463 /* Reset CMD and DATA portions on error */
464 if (err) {
465 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
466 SYSCTL_RSTC);
467 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
468 ;
469
470 if (data) {
471 esdhc_write32(&regs->sysctl,
472 esdhc_read32(&regs->sysctl) |
473 SYSCTL_RSTD);
474 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
475 ;
476 }
477 }
478
Stefano Babicc67bee12010-02-05 15:11:27 +0100479 esdhc_write32(&regs->irqstat, -1);
Andy Fleming50586ef2008-10-30 16:47:16 -0500480
Andrew Gabbasov8a573022014-03-24 02:41:06 -0500481 return err;
Andy Fleming50586ef2008-10-30 16:47:16 -0500482}
483
Simon Glass09b465f2017-07-29 11:35:17 -0600484static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock)
Andy Fleming50586ef2008-10-30 16:47:16 -0500485{
Benoît Thébaudeaub9b4f142018-01-16 22:44:18 +0100486 struct fsl_esdhc *regs = priv->esdhc_regs;
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200487 int div = 1;
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200488 int pre_div = 2;
Yinbo Zhu6f883e52019-07-16 15:09:11 +0800489 unsigned int sdhc_clk = priv->sdhc_clk;
490 u32 time_out;
491 u32 value;
Andy Fleming50586ef2008-10-30 16:47:16 -0500492 uint clk;
493
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200494 if (clock < mmc->cfg->f_min)
495 clock = mmc->cfg->f_min;
Stefano Babicc67bee12010-02-05 15:11:27 +0100496
Yangbo Lu5d336d12019-10-21 18:09:09 +0800497 while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256)
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200498 pre_div *= 2;
Andy Fleming50586ef2008-10-30 16:47:16 -0500499
Yangbo Lu5d336d12019-10-21 18:09:09 +0800500 while (sdhc_clk / (div * pre_div) > clock && div < 16)
Lukasz Majewskib6a04272019-05-07 17:47:28 +0200501 div++;
Andy Fleming50586ef2008-10-30 16:47:16 -0500502
Benoît Thébaudeau4f425282017-05-03 11:59:03 +0200503 pre_div >>= 1;
Andy Fleming50586ef2008-10-30 16:47:16 -0500504 div -= 1;
505
506 clk = (pre_div << 8) | (div << 4);
507
Kumar Galacc4d1222010-03-18 15:51:05 -0500508 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Stefano Babicc67bee12010-02-05 15:11:27 +0100509
510 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleming50586ef2008-10-30 16:47:16 -0500511
Yinbo Zhu6f883e52019-07-16 15:09:11 +0800512 time_out = 20;
513 value = PRSSTAT_SDSTB;
514 while (!(esdhc_read32(&regs->prsstat) & value)) {
515 if (time_out == 0) {
516 printf("fsl_esdhc: Internal clock never stabilised.\n");
517 break;
518 }
519 time_out--;
520 mdelay(1);
521 }
Andy Fleming50586ef2008-10-30 16:47:16 -0500522
Eric Nelsonf0b5f232015-12-04 12:32:48 -0700523 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
Andy Fleming50586ef2008-10-30 16:47:16 -0500524}
525
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800526#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
Simon Glass09b465f2017-07-29 11:35:17 -0600527static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable)
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800528{
Peng Fan96f04072016-03-25 14:16:56 +0800529 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800530 u32 value;
531 u32 time_out;
532
533 value = esdhc_read32(&regs->sysctl);
534
535 if (enable)
536 value |= SYSCTL_CKEN;
537 else
538 value &= ~SYSCTL_CKEN;
539
540 esdhc_write32(&regs->sysctl, value);
541
542 time_out = 20;
543 value = PRSSTAT_SDSTB;
544 while (!(esdhc_read32(&regs->prsstat) & value)) {
545 if (time_out == 0) {
546 printf("fsl_esdhc: Internal clock never stabilised.\n");
547 break;
548 }
549 time_out--;
550 mdelay(1);
551 }
552}
553#endif
554
Simon Glass9586aa62017-07-29 11:35:18 -0600555static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500556{
Peng Fan96f04072016-03-25 14:16:56 +0800557 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleming50586ef2008-10-30 16:47:16 -0500558
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800559#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
560 /* Select to use peripheral clock */
Simon Glass09b465f2017-07-29 11:35:17 -0600561 esdhc_clock_control(priv, false);
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800562 esdhc_setbits32(&regs->esdhcctl, ESDHCCTL_PCS);
Simon Glass09b465f2017-07-29 11:35:17 -0600563 esdhc_clock_control(priv, true);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800564#endif
Andy Fleming50586ef2008-10-30 16:47:16 -0500565 /* Set the clock speed */
Peng Fan51313b42018-01-21 19:00:24 +0800566 if (priv->clock != mmc->clock)
567 set_sysctl(priv, mmc, mmc->clock);
568
Andy Fleming50586ef2008-10-30 16:47:16 -0500569 /* Set the bus width */
Stefano Babicc67bee12010-02-05 15:11:27 +0100570 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleming50586ef2008-10-30 16:47:16 -0500571
572 if (mmc->bus_width == 4)
Stefano Babicc67bee12010-02-05 15:11:27 +0100573 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleming50586ef2008-10-30 16:47:16 -0500574 else if (mmc->bus_width == 8)
Stefano Babicc67bee12010-02-05 15:11:27 +0100575 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
576
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900577 return 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500578}
579
Simon Glass9586aa62017-07-29 11:35:18 -0600580static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc)
Andy Fleming50586ef2008-10-30 16:47:16 -0500581{
Peng Fan96f04072016-03-25 14:16:56 +0800582 struct fsl_esdhc *regs = priv->esdhc_regs;
Simon Glass201e8282017-07-29 11:35:20 -0600583 ulong start;
Andy Fleming50586ef2008-10-30 16:47:16 -0500584
Stefano Babicc67bee12010-02-05 15:11:27 +0100585 /* Reset the entire host controller */
Dirk Behmea61da722013-07-15 15:44:29 +0200586 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicc67bee12010-02-05 15:11:27 +0100587
588 /* Wait until the controller is available */
Simon Glass201e8282017-07-29 11:35:20 -0600589 start = get_timer(0);
590 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA)) {
591 if (get_timer(start) > 1000)
592 return -ETIMEDOUT;
593 }
Stefano Babicc67bee12010-02-05 15:11:27 +0100594
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530595 /* Enable cache snooping */
Yangbo Lu4d8ff422019-06-21 11:42:29 +0800596 esdhc_write32(&regs->esdhcctl, 0x00000040);
P.V.Suresh2c1764e2010-12-04 10:37:23 +0530597
Dirk Behmea61da722013-07-15 15:44:29 +0200598 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Andy Fleming50586ef2008-10-30 16:47:16 -0500599
600 /* Set the initial clock speed */
Jaehoon Chung65117182018-01-26 19:25:29 +0900601 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
Andy Fleming50586ef2008-10-30 16:47:16 -0500602
603 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicc67bee12010-02-05 15:11:27 +0100604 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleming50586ef2008-10-30 16:47:16 -0500605
606 /* Put the PROCTL reg back to the default */
Stefano Babicc67bee12010-02-05 15:11:27 +0100607 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleming50586ef2008-10-30 16:47:16 -0500608
Stefano Babicc67bee12010-02-05 15:11:27 +0100609 /* Set timout to the maximum value */
610 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleming50586ef2008-10-30 16:47:16 -0500611
Thierry Redingd48d2e22012-01-02 01:15:38 +0000612 return 0;
613}
Andy Fleming50586ef2008-10-30 16:47:16 -0500614
Simon Glass9586aa62017-07-29 11:35:18 -0600615static int esdhc_getcd_common(struct fsl_esdhc_priv *priv)
Thierry Redingd48d2e22012-01-02 01:15:38 +0000616{
Peng Fan96f04072016-03-25 14:16:56 +0800617 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Redingd48d2e22012-01-02 01:15:38 +0000618 int timeout = 1000;
Stefano Babicc67bee12010-02-05 15:11:27 +0100619
Haijun.Zhangf7e27cc2014-01-10 13:52:17 +0800620#ifdef CONFIG_ESDHC_DETECT_QUIRK
621 if (CONFIG_ESDHC_DETECT_QUIRK)
622 return 1;
623#endif
Thierry Redingd48d2e22012-01-02 01:15:38 +0000624 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
625 udelay(1000);
626
627 return timeout > 0;
Andy Fleming50586ef2008-10-30 16:47:16 -0500628}
629
Yangbo Lu57059732019-10-31 18:54:23 +0800630static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv,
631 struct mmc_config *cfg)
Andy Fleming50586ef2008-10-30 16:47:16 -0500632{
Yangbo Lu57059732019-10-31 18:54:23 +0800633 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu5b05fc02019-10-31 18:54:21 +0800634 u32 caps;
Andy Fleming50586ef2008-10-30 16:47:16 -0500635
Wang Huan19060bd2014-09-05 13:52:40 +0800636 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang3b4456e2011-01-07 00:06:47 -0600637#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
Yangbo Lu5b05fc02019-10-31 18:54:21 +0800638 caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30);
Roy Zang3b4456e2011-01-07 00:06:47 -0600639#endif
Haijun.Zhangef38f3f2013-10-31 09:38:19 +0800640#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
Yangbo Lu5b05fc02019-10-31 18:54:21 +0800641 caps |= HOSTCAPBLT_VS33;
Haijun.Zhangef38f3f2013-10-31 09:38:19 +0800642#endif
Yangbo Lu5b05fc02019-10-31 18:54:21 +0800643 if (caps & HOSTCAPBLT_VS18)
644 cfg->voltages |= MMC_VDD_165_195;
645 if (caps & HOSTCAPBLT_VS30)
646 cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31;
647 if (caps & HOSTCAPBLT_VS33)
648 cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yang030955c2010-11-25 17:06:09 +0000649
Simon Glasse88e1d92017-07-29 11:35:21 -0600650 cfg->name = "FSL_SDHC";
Abbas Razaaad46592013-03-25 09:13:34 +0000651
Yangbo Lu5b05fc02019-10-31 18:54:21 +0800652 if (caps & HOSTCAPBLT_HSS)
Simon Glasse88e1d92017-07-29 11:35:21 -0600653 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleming50586ef2008-10-30 16:47:16 -0500654
Simon Glasse88e1d92017-07-29 11:35:21 -0600655 cfg->f_min = 400000;
Peng Fan51313b42018-01-21 19:00:24 +0800656 cfg->f_max = min(priv->sdhc_clk, (u32)200000000);
Simon Glasse88e1d92017-07-29 11:35:21 -0600657 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Peng Fan96f04072016-03-25 14:16:56 +0800658}
659
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800660#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
661void mmc_adapter_card_type_ident(void)
662{
663 u8 card_id;
664 u8 value;
665
666 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
667 gd->arch.sdhc_adapter = card_id;
668
669 switch (card_id) {
670 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lucdc69552015-09-17 10:27:12 +0800671 value = QIXIS_READ(brdcfg[5]);
672 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
673 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800674 break;
675 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Lubf50be82015-09-17 10:27:48 +0800676 value = QIXIS_READ(pwr_ctl[1]);
677 value |= QIXIS_EVDD_BY_SDHC_VS;
678 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800679 break;
680 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
681 value = QIXIS_READ(brdcfg[5]);
682 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
683 QIXIS_WRITE(brdcfg[5], value);
684 break;
685 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
686 break;
687 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
688 break;
689 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
690 break;
691 case QIXIS_ESDHC_NO_ADAPTER:
692 break;
693 default:
694 break;
695 }
696}
697#endif
698
Stefano Babicc67bee12010-02-05 15:11:27 +0100699#ifdef CONFIG_OF_LIBFDT
Yangbo Lufce1e162017-01-17 10:43:54 +0800700__weak int esdhc_status_fixup(void *blob, const char *compat)
701{
702#ifdef CONFIG_FSL_ESDHC_PIN_MUX
703 if (!hwconfig("esdhc")) {
704 do_fixup_by_compat(blob, compat, "status", "disabled",
705 sizeof("disabled"), 1);
706 return 1;
707 }
708#endif
Yangbo Lufce1e162017-01-17 10:43:54 +0800709 return 0;
710}
711
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400712void fdt_fixup_esdhc(void *blob, bd_t *bd)
713{
714 const char *compat = "fsl,esdhc";
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400715
Yangbo Lufce1e162017-01-17 10:43:54 +0800716 if (esdhc_status_fixup(blob, compat))
Chenhui Zhaoa6da8b82011-01-04 17:23:05 +0800717 return;
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400718
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800719#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
720 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
721 gd->arch.sdhc_clk, 1);
722#else
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400723 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glasse9adeca2012-12-13 20:49:05 +0000724 gd->arch.sdhc_clk, 1);
Yangbo Lu2d9ca2c2015-04-22 13:57:40 +0800725#endif
Yangbo Lu5a8dbdc2015-04-22 13:57:00 +0800726#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
727 do_fixup_by_compat_u32(blob, compat, "adapter-type",
728 (u32)(gd->arch.sdhc_adapter), 1);
729#endif
Anton Vorontsovb33433a2009-06-10 00:25:29 +0400730}
Stefano Babicc67bee12010-02-05 15:11:27 +0100731#endif
Peng Fan96f04072016-03-25 14:16:56 +0800732
Yangbo Lu61870472019-10-31 18:54:26 +0800733#if !CONFIG_IS_ENABLED(DM_MMC)
734static int esdhc_getcd(struct mmc *mmc)
735{
736 struct fsl_esdhc_priv *priv = mmc->priv;
737
738 return esdhc_getcd_common(priv);
739}
740
741static int esdhc_init(struct mmc *mmc)
742{
743 struct fsl_esdhc_priv *priv = mmc->priv;
744
745 return esdhc_init_common(priv, mmc);
746}
747
748static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
749 struct mmc_data *data)
750{
751 struct fsl_esdhc_priv *priv = mmc->priv;
752
753 return esdhc_send_cmd_common(priv, mmc, cmd, data);
754}
755
756static int esdhc_set_ios(struct mmc *mmc)
757{
758 struct fsl_esdhc_priv *priv = mmc->priv;
759
760 return esdhc_set_ios_common(priv, mmc);
761}
762
763static const struct mmc_ops esdhc_ops = {
764 .getcd = esdhc_getcd,
765 .init = esdhc_init,
766 .send_cmd = esdhc_send_cmd,
767 .set_ios = esdhc_set_ios,
768};
769
770int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
771{
772 struct fsl_esdhc_plat *plat;
773 struct fsl_esdhc_priv *priv;
774 struct mmc_config *mmc_cfg;
775 struct mmc *mmc;
776
777 if (!cfg)
778 return -EINVAL;
779
780 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
781 if (!priv)
782 return -ENOMEM;
783 plat = calloc(sizeof(struct fsl_esdhc_plat), 1);
784 if (!plat) {
785 free(priv);
786 return -ENOMEM;
787 }
788
789 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
790 priv->sdhc_clk = cfg->sdhc_clk;
791
792 mmc_cfg = &plat->cfg;
793
794 if (cfg->max_bus_width == 8) {
795 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
796 MMC_MODE_8BIT;
797 } else if (cfg->max_bus_width == 4) {
798 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT;
799 } else if (cfg->max_bus_width == 1) {
800 mmc_cfg->host_caps |= MMC_MODE_1BIT;
801 } else {
802 mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT |
803 MMC_MODE_8BIT;
804 printf("No max bus width provided. Assume 8-bit supported.\n");
805 }
806
807#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
808 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
809 mmc_cfg->host_caps &= ~MMC_MODE_8BIT;
810#endif
811 mmc_cfg->ops = &esdhc_ops;
812
813 fsl_esdhc_get_cfg_common(priv, mmc_cfg);
814
815 mmc = mmc_create(mmc_cfg, priv);
816 if (!mmc)
817 return -EIO;
818
819 priv->mmc = mmc;
820 return 0;
821}
822
823int fsl_esdhc_mmc_init(bd_t *bis)
824{
825 struct fsl_esdhc_cfg *cfg;
826
827 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
828 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
829 cfg->sdhc_clk = gd->arch.sdhc_clk;
830 return fsl_esdhc_initialize(bis, cfg);
831}
832#else /* DM_MMC */
Peng Fan96f04072016-03-25 14:16:56 +0800833static int fsl_esdhc_probe(struct udevice *dev)
834{
835 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glasse88e1d92017-07-29 11:35:21 -0600836 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Peng Fan96f04072016-03-25 14:16:56 +0800837 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
Peng Fan96f04072016-03-25 14:16:56 +0800838 fdt_addr_t addr;
Simon Glass653282b2017-07-29 11:35:24 -0600839 struct mmc *mmc;
Peng Fan96f04072016-03-25 14:16:56 +0800840
Simon Glass4aac33f2017-07-29 11:35:23 -0600841 addr = dev_read_addr(dev);
Peng Fan96f04072016-03-25 14:16:56 +0800842 if (addr == FDT_ADDR_T_NONE)
843 return -EINVAL;
Yinbo Zhub69e1d02019-04-11 11:01:50 +0000844#ifdef CONFIG_PPC
845 priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr);
846#else
Peng Fan96f04072016-03-25 14:16:56 +0800847 priv->esdhc_regs = (struct fsl_esdhc *)addr;
Yinbo Zhub69e1d02019-04-11 11:01:50 +0000848#endif
Peng Fan96f04072016-03-25 14:16:56 +0800849 priv->dev = dev;
850
Yangbo Lu5e81cbf2019-11-12 19:28:36 +0800851 priv->sdhc_clk = gd->arch.sdhc_clk;
852 if (priv->sdhc_clk <= 0) {
853 dev_err(dev, "Unable to get clk for %s\n", dev->name);
854 return -EINVAL;
Peng Fan96f04072016-03-25 14:16:56 +0800855 }
856
Yangbo Lu57059732019-10-31 18:54:23 +0800857 fsl_esdhc_get_cfg_common(priv, &plat->cfg);
Peng Fan96f04072016-03-25 14:16:56 +0800858
Yinbo Zhu6f883e52019-07-16 15:09:11 +0800859 mmc_of_parse(dev, &plat->cfg);
860
Simon Glass653282b2017-07-29 11:35:24 -0600861 mmc = &plat->mmc;
862 mmc->cfg = &plat->cfg;
863 mmc->dev = dev;
Yangbo Lu66fa0352019-05-23 11:05:46 +0800864
Simon Glass653282b2017-07-29 11:35:24 -0600865 upriv->mmc = mmc;
Peng Fan96f04072016-03-25 14:16:56 +0800866
Simon Glass653282b2017-07-29 11:35:24 -0600867 return esdhc_init_common(priv, mmc);
Peng Fan96f04072016-03-25 14:16:56 +0800868}
869
Simon Glass653282b2017-07-29 11:35:24 -0600870static int fsl_esdhc_get_cd(struct udevice *dev)
871{
Yangbo Lu08197cb2019-10-31 18:54:24 +0800872 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
Simon Glass653282b2017-07-29 11:35:24 -0600873 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
874
Yangbo Lu08197cb2019-10-31 18:54:24 +0800875 if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE)
876 return 1;
877
Simon Glass653282b2017-07-29 11:35:24 -0600878 return esdhc_getcd_common(priv);
879}
880
881static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
882 struct mmc_data *data)
883{
884 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
885 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
886
887 return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data);
888}
889
890static int fsl_esdhc_set_ios(struct udevice *dev)
891{
892 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
893 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
894
895 return esdhc_set_ios_common(priv, &plat->mmc);
896}
897
898static const struct dm_mmc_ops fsl_esdhc_ops = {
899 .get_cd = fsl_esdhc_get_cd,
900 .send_cmd = fsl_esdhc_send_cmd,
901 .set_ios = fsl_esdhc_set_ios,
Yinbo Zhu6f883e52019-07-16 15:09:11 +0800902#ifdef MMC_SUPPORTS_TUNING
903 .execute_tuning = fsl_esdhc_execute_tuning,
904#endif
Simon Glass653282b2017-07-29 11:35:24 -0600905};
Simon Glass653282b2017-07-29 11:35:24 -0600906
Peng Fan96f04072016-03-25 14:16:56 +0800907static const struct udevice_id fsl_esdhc_ids[] = {
Yangbo Lua6473f82016-12-07 11:54:31 +0800908 { .compatible = "fsl,esdhc", },
Peng Fan96f04072016-03-25 14:16:56 +0800909 { /* sentinel */ }
910};
911
Simon Glass653282b2017-07-29 11:35:24 -0600912static int fsl_esdhc_bind(struct udevice *dev)
913{
914 struct fsl_esdhc_plat *plat = dev_get_platdata(dev);
915
916 return mmc_bind(dev, &plat->mmc, &plat->cfg);
917}
Simon Glass653282b2017-07-29 11:35:24 -0600918
Peng Fan96f04072016-03-25 14:16:56 +0800919U_BOOT_DRIVER(fsl_esdhc) = {
920 .name = "fsl-esdhc-mmc",
921 .id = UCLASS_MMC,
922 .of_match = fsl_esdhc_ids,
Simon Glass653282b2017-07-29 11:35:24 -0600923 .ops = &fsl_esdhc_ops,
Simon Glass653282b2017-07-29 11:35:24 -0600924 .bind = fsl_esdhc_bind,
Peng Fan96f04072016-03-25 14:16:56 +0800925 .probe = fsl_esdhc_probe,
Simon Glasse88e1d92017-07-29 11:35:21 -0600926 .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat),
Peng Fan96f04072016-03-25 14:16:56 +0800927 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
928};
929#endif