blob: 3abd2d30aff89cb821645e5d79884f2cb0f85202 [file] [log] [blame]
Andy Fleminge52ffb82008-10-30 16:47:16 -05001/*
Jerry Huanged413672011-01-06 23:42:19 -06002 * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc
Andy Fleminge52ffb82008-10-30 16:47:16 -05003 * Andy Fleming
4 *
5 * Based vaguely on the pxa mmc code:
6 * (C) Copyright 2003
7 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
8 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02009 * SPDX-License-Identifier: GPL-2.0+
Andy Fleminge52ffb82008-10-30 16:47:16 -050010 */
11
12#include <config.h>
13#include <common.h>
14#include <command.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090015#include <errno.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040016#include <hwconfig.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050017#include <mmc.h>
18#include <part.h>
Peng Fan5eb8b432017-06-12 17:50:54 +080019#include <power/regulator.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050020#include <malloc.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050021#include <fsl_esdhc.h>
Anton Vorontsovf751a3c2009-06-10 00:25:29 +040022#include <fdt_support.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050023#include <asm/io.h>
Peng Fana4d36f72016-03-25 14:16:56 +080024#include <dm.h>
25#include <asm-generic/gpio.h>
Andy Fleminge52ffb82008-10-30 16:47:16 -050026
Andy Fleminge52ffb82008-10-30 16:47:16 -050027DECLARE_GLOBAL_DATA_PTR;
28
Ye.Li3d46c312014-11-04 15:35:49 +080029#define SDHCI_IRQ_EN_BITS (IRQSTATEN_CC | IRQSTATEN_TC | \
30 IRQSTATEN_CINT | \
31 IRQSTATEN_CTOE | IRQSTATEN_CCE | IRQSTATEN_CEBE | \
32 IRQSTATEN_CIE | IRQSTATEN_DTOE | IRQSTATEN_DCE | \
33 IRQSTATEN_DEBE | IRQSTATEN_BRR | IRQSTATEN_BWR | \
34 IRQSTATEN_DINT)
35
Andy Fleminge52ffb82008-10-30 16:47:16 -050036struct fsl_esdhc {
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080037 uint dsaddr; /* SDMA system address register */
38 uint blkattr; /* Block attributes register */
39 uint cmdarg; /* Command argument register */
40 uint xfertyp; /* Transfer type register */
41 uint cmdrsp0; /* Command response 0 register */
42 uint cmdrsp1; /* Command response 1 register */
43 uint cmdrsp2; /* Command response 2 register */
44 uint cmdrsp3; /* Command response 3 register */
45 uint datport; /* Buffer data port register */
46 uint prsstat; /* Present state register */
47 uint proctl; /* Protocol control register */
48 uint sysctl; /* System Control Register */
49 uint irqstat; /* Interrupt status register */
50 uint irqstaten; /* Interrupt status enable register */
51 uint irqsigen; /* Interrupt signal enable register */
52 uint autoc12err; /* Auto CMD error status register */
53 uint hostcapblt; /* Host controller capabilities register */
54 uint wml; /* Watermark level register */
55 uint mixctrl; /* For USDHC */
56 char reserved1[4]; /* reserved */
57 uint fevt; /* Force event register */
58 uint admaes; /* ADMA error status register */
59 uint adsaddr; /* ADMA system address register */
Peng Fana6eadd52016-06-15 10:53:00 +080060 char reserved2[4];
61 uint dllctrl;
62 uint dllstat;
63 uint clktunectrlstatus;
64 char reserved3[84];
65 uint vendorspec;
66 uint mmcboot;
67 uint vendorspec2;
68 char reserved4[48];
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080069 uint hostver; /* Host controller version register */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080070 char reserved5[4]; /* reserved */
Peng Fana6eadd52016-06-15 10:53:00 +080071 uint dmaerraddr; /* DMA error address register */
Otavio Salvadorfad3e062015-02-17 10:42:43 -020072 char reserved6[4]; /* reserved */
Peng Fana6eadd52016-06-15 10:53:00 +080073 uint dmaerrattr; /* DMA error attribute register */
74 char reserved7[4]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080075 uint hostcapblt2; /* Host controller capabilities register 2 */
Peng Fana6eadd52016-06-15 10:53:00 +080076 char reserved8[8]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080077 uint tcr; /* Tuning control register */
Peng Fana6eadd52016-06-15 10:53:00 +080078 char reserved9[28]; /* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080079 uint sddirctl; /* SD direction control register */
Peng Fana6eadd52016-06-15 10:53:00 +080080 char reserved10[712];/* reserved */
Haijun.Zhangd49eb9e2013-10-30 11:37:55 +080081 uint scr; /* eSDHC control register */
Andy Fleminge52ffb82008-10-30 16:47:16 -050082};
83
Peng Fana4d36f72016-03-25 14:16:56 +080084/**
85 * struct fsl_esdhc_priv
86 *
87 * @esdhc_regs: registers of the sdhc controller
88 * @sdhc_clk: Current clk of the sdhc controller
89 * @bus_width: bus width, 1bit, 4bit or 8bit
90 * @cfg: mmc config
91 * @mmc: mmc
92 * Following is used when Driver Model is enabled for MMC
93 * @dev: pointer for the device
94 * @non_removable: 0: removable; 1: non-removable
Peng Fan01eb1c42016-06-15 10:53:02 +080095 * @wp_enable: 1: enable checking wp; 0: no check
Peng Fanaee78582017-06-12 17:50:53 +080096 * @vs18_enable: 1: use 1.8V voltage; 0: use 3.3V
Peng Fana4d36f72016-03-25 14:16:56 +080097 * @cd_gpio: gpio for card detection
Peng Fan01eb1c42016-06-15 10:53:02 +080098 * @wp_gpio: gpio for write protection
Peng Fana4d36f72016-03-25 14:16:56 +080099 */
100struct fsl_esdhc_priv {
101 struct fsl_esdhc *esdhc_regs;
102 unsigned int sdhc_clk;
103 unsigned int bus_width;
104 struct mmc_config cfg;
105 struct mmc *mmc;
106 struct udevice *dev;
107 int non_removable;
Peng Fan01eb1c42016-06-15 10:53:02 +0800108 int wp_enable;
Peng Fanaee78582017-06-12 17:50:53 +0800109 int vs18_enable;
Yangbo Lub99647c2016-12-07 11:54:30 +0800110#ifdef CONFIG_DM_GPIO
Peng Fana4d36f72016-03-25 14:16:56 +0800111 struct gpio_desc cd_gpio;
Peng Fan01eb1c42016-06-15 10:53:02 +0800112 struct gpio_desc wp_gpio;
Yangbo Lub99647c2016-12-07 11:54:30 +0800113#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800114};
115
Andy Fleminge52ffb82008-10-30 16:47:16 -0500116/* Return the XFERTYP flags for a given command and data packet */
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000117static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500118{
119 uint xfertyp = 0;
120
121 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530122 xfertyp |= XFERTYP_DPSEL;
123#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
124 xfertyp |= XFERTYP_DMAEN;
125#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500126 if (data->blocks > 1) {
127 xfertyp |= XFERTYP_MSBSEL;
128 xfertyp |= XFERTYP_BCEN;
Jerry Huanged413672011-01-06 23:42:19 -0600129#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
130 xfertyp |= XFERTYP_AC12EN;
131#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500132 }
133
134 if (data->flags & MMC_DATA_READ)
135 xfertyp |= XFERTYP_DTDSEL;
136 }
137
138 if (cmd->resp_type & MMC_RSP_CRC)
139 xfertyp |= XFERTYP_CCCEN;
140 if (cmd->resp_type & MMC_RSP_OPCODE)
141 xfertyp |= XFERTYP_CICEN;
142 if (cmd->resp_type & MMC_RSP_136)
143 xfertyp |= XFERTYP_RSPTYP_136;
144 else if (cmd->resp_type & MMC_RSP_BUSY)
145 xfertyp |= XFERTYP_RSPTYP_48_BUSY;
146 else if (cmd->resp_type & MMC_RSP_PRESENT)
147 xfertyp |= XFERTYP_RSPTYP_48;
148
Jason Liubef0ff02011-03-22 01:32:31 +0000149 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
150 xfertyp |= XFERTYP_CMDTYP_ABORT;
Yangbo Lub73a3d62016-01-21 17:33:19 +0800151
Andy Fleminge52ffb82008-10-30 16:47:16 -0500152 return XFERTYP_CMD(cmd->cmdidx) | xfertyp;
153}
154
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530155#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
156/*
157 * PIO Read/Write Mode reduce the performace as DMA is not used in this mode.
158 */
Wolfgang Denka40545c2010-05-09 23:52:59 +0200159static void
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530160esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data)
161{
Peng Fana4d36f72016-03-25 14:16:56 +0800162 struct fsl_esdhc_priv *priv = mmc->priv;
163 struct fsl_esdhc *regs = priv->esdhc_regs;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530164 uint blocks;
165 char *buffer;
166 uint databuf;
167 uint size;
168 uint irqstat;
169 uint timeout;
170
171 if (data->flags & MMC_DATA_READ) {
172 blocks = data->blocks;
173 buffer = data->dest;
174 while (blocks) {
175 timeout = PIO_TIMEOUT;
176 size = data->blocksize;
177 irqstat = esdhc_read32(&regs->irqstat);
178 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BREN)
179 && --timeout);
180 if (timeout <= 0) {
181 printf("\nData Read Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200182 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530183 }
184 while (size && (!(irqstat & IRQSTAT_TC))) {
185 udelay(100); /* Wait before last byte transfer complete */
186 irqstat = esdhc_read32(&regs->irqstat);
187 databuf = in_le32(&regs->datport);
188 *((uint *)buffer) = databuf;
189 buffer += 4;
190 size -= 4;
191 }
192 blocks--;
193 }
194 } else {
195 blocks = data->blocks;
Wolfgang Denka40545c2010-05-09 23:52:59 +0200196 buffer = (char *)data->src;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530197 while (blocks) {
198 timeout = PIO_TIMEOUT;
199 size = data->blocksize;
200 irqstat = esdhc_read32(&regs->irqstat);
201 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_BWEN)
202 && --timeout);
203 if (timeout <= 0) {
204 printf("\nData Write Failed in PIO Mode.");
Wolfgang Denka40545c2010-05-09 23:52:59 +0200205 return;
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530206 }
207 while (size && (!(irqstat & IRQSTAT_TC))) {
208 udelay(100); /* Wait before last byte transfer complete */
209 databuf = *((uint *)buffer);
210 buffer += 4;
211 size -= 4;
212 irqstat = esdhc_read32(&regs->irqstat);
213 out_le32(&regs->datport, databuf);
214 }
215 blocks--;
216 }
217 }
218}
219#endif
220
Andy Fleminge52ffb82008-10-30 16:47:16 -0500221static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data)
222{
Andy Fleminge52ffb82008-10-30 16:47:16 -0500223 int timeout;
Peng Fana4d36f72016-03-25 14:16:56 +0800224 struct fsl_esdhc_priv *priv = mmc->priv;
225 struct fsl_esdhc *regs = priv->esdhc_regs;
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300226#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700227 dma_addr_t addr;
228#endif
Wolfgang Denka40545c2010-05-09 23:52:59 +0200229 uint wml_value;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500230
231 wml_value = data->blocksize/4;
232
233 if (data->flags & MMC_DATA_READ) {
Priyanka Jain02449632011-02-09 09:24:10 +0530234 if (wml_value > WML_RD_WML_MAX)
235 wml_value = WML_RD_WML_MAX_VAL;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500236
Roy Zange5853af2010-02-09 18:23:33 +0800237 esdhc_clrsetbits32(&regs->wml, WML_RD_WML_MASK, wml_value);
Ye.Li33a56b12014-02-20 18:00:57 +0800238#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300239#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700240 addr = virt_to_phys((void *)(data->dest));
241 if (upper_32_bits(addr))
242 printf("Error found for upper 32 bits\n");
243 else
244 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
245#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100246 esdhc_write32(&regs->dsaddr, (u32)data->dest);
Ye.Li33a56b12014-02-20 18:00:57 +0800247#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700248#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500249 } else {
Ye.Li33a56b12014-02-20 18:00:57 +0800250#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eric Nelson30e9cad2012-04-25 14:28:48 +0000251 flush_dcache_range((ulong)data->src,
252 (ulong)data->src+data->blocks
253 *data->blocksize);
Ye.Li33a56b12014-02-20 18:00:57 +0800254#endif
Priyanka Jain02449632011-02-09 09:24:10 +0530255 if (wml_value > WML_WR_WML_MAX)
256 wml_value = WML_WR_WML_MAX_VAL;
Peng Fan01eb1c42016-06-15 10:53:02 +0800257 if (priv->wp_enable) {
258 if ((esdhc_read32(&regs->prsstat) &
259 PRSSTAT_WPSPL) == 0) {
260 printf("\nThe SD card is locked. Can not write to a locked card.\n\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900261 return -ETIMEDOUT;
Peng Fan01eb1c42016-06-15 10:53:02 +0800262 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500263 }
Roy Zange5853af2010-02-09 18:23:33 +0800264
265 esdhc_clrsetbits32(&regs->wml, WML_WR_WML_MASK,
266 wml_value << 16);
Ye.Li33a56b12014-02-20 18:00:57 +0800267#ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300268#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700269 addr = virt_to_phys((void *)(data->src));
270 if (upper_32_bits(addr))
271 printf("Error found for upper 32 bits\n");
272 else
273 esdhc_write32(&regs->dsaddr, lower_32_bits(addr));
274#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100275 esdhc_write32(&regs->dsaddr, (u32)data->src);
Ye.Li33a56b12014-02-20 18:00:57 +0800276#endif
Yangbo Lud0e295d2015-03-20 19:28:31 -0700277#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500278 }
279
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100280 esdhc_write32(&regs->blkattr, data->blocks << 16 | data->blocksize);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500281
282 /* Calculate the timeout period for data transactions */
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530283 /*
284 * 1)Timeout period = (2^(timeout+13)) SD Clock cycles
285 * 2)Timeout period should be minimum 0.250sec as per SD Card spec
286 * So, Number of SD Clock cycles for 0.25sec should be minimum
287 * (SD Clock/sec * 0.25 sec) SD Clock cycles
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500288 * = (mmc->clock * 1/4) SD Clock cycles
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530289 * As 1) >= 2)
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500290 * => (2^(timeout+13)) >= mmc->clock * 1/4
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530291 * Taking log2 both the sides
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500292 * => timeout + 13 >= log2(mmc->clock/4)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530293 * Rounding up to next power of 2
Andrew Gabbasovd5b48662014-03-24 02:40:41 -0500294 * => timeout + 13 = log2(mmc->clock/4) + 1
295 * => timeout + 13 = fls(mmc->clock/4)
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800296 *
297 * However, the MMC spec "It is strongly recommended for hosts to
298 * implement more than 500ms timeout value even if the card
299 * indicates the 250ms maximum busy length." Even the previous
300 * value of 300ms is known to be insufficient for some cards.
301 * So, we use
302 * => timeout + 13 = fls(mmc->clock/2)
Priyanka Jainc51b40d2011-03-03 09:18:56 +0530303 */
Yangbo Lu9d7f3212015-12-30 14:19:30 +0800304 timeout = fls(mmc->clock/2);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500305 timeout -= 13;
306
307 if (timeout > 14)
308 timeout = 14;
309
310 if (timeout < 0)
311 timeout = 0;
312
Kumar Gala9a878d52011-01-29 15:36:10 -0600313#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001
314 if ((timeout == 4) || (timeout == 8) || (timeout == 12))
315 timeout++;
316#endif
317
Haijun.Zhangedeb83a2014-03-18 17:04:23 +0800318#ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE
319 timeout = 0xE;
320#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100321 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500322
323 return 0;
324}
325
Eric Nelson30e9cad2012-04-25 14:28:48 +0000326static void check_and_invalidate_dcache_range
327 (struct mmc_cmd *cmd,
328 struct mmc_data *data) {
Yangbo Lud0e295d2015-03-20 19:28:31 -0700329 unsigned start = 0;
Yangbo Lue7702c62016-05-12 19:12:58 +0800330 unsigned end = 0;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000331 unsigned size = roundup(ARCH_DMA_MINALIGN,
332 data->blocks*data->blocksize);
Eddy Petrișor5178dc12016-06-05 03:43:00 +0300333#if defined(CONFIG_FSL_LAYERSCAPE) || defined(CONFIG_S32V234)
Yangbo Lud0e295d2015-03-20 19:28:31 -0700334 dma_addr_t addr;
335
336 addr = virt_to_phys((void *)(data->dest));
337 if (upper_32_bits(addr))
338 printf("Error found for upper 32 bits\n");
339 else
340 start = lower_32_bits(addr);
Yangbo Lue7702c62016-05-12 19:12:58 +0800341#else
342 start = (unsigned)data->dest;
Yangbo Lud0e295d2015-03-20 19:28:31 -0700343#endif
Yangbo Lue7702c62016-05-12 19:12:58 +0800344 end = start + size;
Eric Nelson30e9cad2012-04-25 14:28:48 +0000345 invalidate_dcache_range(start, end);
346}
Tom Rini239dd252014-05-23 09:19:05 -0400347
Andy Fleminge52ffb82008-10-30 16:47:16 -0500348/*
349 * Sends a command out on the bus. Takes the mmc pointer,
350 * a command pointer, and an optional data pointer.
351 */
352static int
353esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
354{
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500355 int err = 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500356 uint xfertyp;
357 uint irqstat;
Peng Fana4d36f72016-03-25 14:16:56 +0800358 struct fsl_esdhc_priv *priv = mmc->priv;
359 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500360
Jerry Huanged413672011-01-06 23:42:19 -0600361#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111
362 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
363 return 0;
364#endif
365
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100366 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500367
368 sync();
369
370 /* Wait for the bus to be idle */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100371 while ((esdhc_read32(&regs->prsstat) & PRSSTAT_CICHB) ||
372 (esdhc_read32(&regs->prsstat) & PRSSTAT_CIDHB))
373 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500374
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100375 while (esdhc_read32(&regs->prsstat) & PRSSTAT_DLA)
376 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500377
378 /* Wait at least 8 SD clock cycles before the next command */
379 /*
380 * Note: This is way more than 8 cycles, but 1ms seems to
381 * resolve timing issues with some cards
382 */
383 udelay(1000);
384
385 /* Set up for a data transfer if we have one */
386 if (data) {
Andy Fleminge52ffb82008-10-30 16:47:16 -0500387 err = esdhc_setup_data(mmc, data);
388 if(err)
389 return err;
Peng Fan9cb5e992015-06-25 10:32:26 +0800390
391 if (data->flags & MMC_DATA_READ)
392 check_and_invalidate_dcache_range(cmd, data);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500393 }
394
395 /* Figure out the transfer arguments */
396 xfertyp = esdhc_xfertyp(cmd, data);
397
Andrew Gabbasov4816b7a2013-06-11 10:34:22 -0500398 /* Mask all irqs */
399 esdhc_write32(&regs->irqsigen, 0);
400
Andy Fleminge52ffb82008-10-30 16:47:16 -0500401 /* Send the command */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100402 esdhc_write32(&regs->cmdarg, cmd->cmdarg);
Jason Liu9919d642011-11-25 00:18:04 +0000403#if defined(CONFIG_FSL_USDHC)
404 esdhc_write32(&regs->mixctrl,
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500405 (esdhc_read32(&regs->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)
406 | (mmc->ddr_mode ? XFERTYP_DDREN : 0));
Jason Liu9919d642011-11-25 00:18:04 +0000407 esdhc_write32(&regs->xfertyp, xfertyp & 0xFFFF0000);
408#else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100409 esdhc_write32(&regs->xfertyp, xfertyp);
Jason Liu9919d642011-11-25 00:18:04 +0000410#endif
Dirk Behmed8552d62012-03-26 03:13:05 +0000411
Andy Fleminge52ffb82008-10-30 16:47:16 -0500412 /* Wait for the command to complete */
Dirk Behmed8552d62012-03-26 03:13:05 +0000413 while (!(esdhc_read32(&regs->irqstat) & (IRQSTAT_CC | IRQSTAT_CTOE)))
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100414 ;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500415
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100416 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500417
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500418 if (irqstat & CMD_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900419 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500420 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000421 }
422
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500423 if (irqstat & IRQSTAT_CTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900424 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500425 goto out;
426 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500427
Otavio Salvadorfad3e062015-02-17 10:42:43 -0200428 /* Switch voltage to 1.8V if CMD11 succeeded */
429 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V) {
430 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
431
432 printf("Run CMD11 1.8V switch\n");
433 /* Sleep for 5 ms - max time for card to switch to 1.8V */
434 udelay(5000);
435 }
436
Dirk Behmed8552d62012-03-26 03:13:05 +0000437 /* Workaround for ESDHC errata ENGcm03648 */
438 if (!data && (cmd->resp_type & MMC_RSP_BUSY)) {
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800439 int timeout = 6000;
Dirk Behmed8552d62012-03-26 03:13:05 +0000440
Yangbo Lu3ffa8512015-04-15 10:13:12 +0800441 /* Poll on DATA0 line for cmd with busy signal for 600 ms */
Dirk Behmed8552d62012-03-26 03:13:05 +0000442 while (timeout > 0 && !(esdhc_read32(&regs->prsstat) &
443 PRSSTAT_DAT0)) {
444 udelay(100);
445 timeout--;
446 }
447
448 if (timeout <= 0) {
449 printf("Timeout waiting for DAT0 to go high!\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900450 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500451 goto out;
Dirk Behmed8552d62012-03-26 03:13:05 +0000452 }
453 }
454
Andy Fleminge52ffb82008-10-30 16:47:16 -0500455 /* Copy the response to the response buffer */
456 if (cmd->resp_type & MMC_RSP_136) {
457 u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0;
458
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100459 cmdrsp3 = esdhc_read32(&regs->cmdrsp3);
460 cmdrsp2 = esdhc_read32(&regs->cmdrsp2);
461 cmdrsp1 = esdhc_read32(&regs->cmdrsp1);
462 cmdrsp0 = esdhc_read32(&regs->cmdrsp0);
Rabin Vincentb6eed942009-04-05 13:30:56 +0530463 cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24);
464 cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24);
465 cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24);
466 cmd->response[3] = (cmdrsp0 << 8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500467 } else
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100468 cmd->response[0] = esdhc_read32(&regs->cmdrsp0);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500469
470 /* Wait until all of the blocks are transferred */
471 if (data) {
Dipen Dudhat5c72f352009-10-05 15:41:58 +0530472#ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO
473 esdhc_pio_read_write(mmc, data);
474#else
Andy Fleminge52ffb82008-10-30 16:47:16 -0500475 do {
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100476 irqstat = esdhc_read32(&regs->irqstat);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500477
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500478 if (irqstat & IRQSTAT_DTOE) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900479 err = -ETIMEDOUT;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500480 goto out;
481 }
Frans Meulenbroeks010ba982010-07-31 04:45:18 +0000482
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500483 if (irqstat & DATA_ERR) {
Jaehoon Chung7825d202016-07-19 16:33:36 +0900484 err = -ECOMM;
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500485 goto out;
486 }
Andrew Gabbasov4a929622013-04-07 23:06:08 +0000487 } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE);
Ye.Li33a56b12014-02-20 18:00:57 +0800488
Peng Fan9cb5e992015-06-25 10:32:26 +0800489 /*
490 * Need invalidate the dcache here again to avoid any
491 * cache-fill during the DMA operations such as the
492 * speculative pre-fetching etc.
493 */
Eric Nelson70e68692013-04-03 12:31:56 +0000494 if (data->flags & MMC_DATA_READ)
495 check_and_invalidate_dcache_range(cmd, data);
Ye.Li33a56b12014-02-20 18:00:57 +0800496#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500497 }
498
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500499out:
500 /* Reset CMD and DATA portions on error */
501 if (err) {
502 esdhc_write32(&regs->sysctl, esdhc_read32(&regs->sysctl) |
503 SYSCTL_RSTC);
504 while (esdhc_read32(&regs->sysctl) & SYSCTL_RSTC)
505 ;
506
507 if (data) {
508 esdhc_write32(&regs->sysctl,
509 esdhc_read32(&regs->sysctl) |
510 SYSCTL_RSTD);
511 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTD))
512 ;
513 }
Otavio Salvadorfad3e062015-02-17 10:42:43 -0200514
515 /* If this was CMD11, then notify that power cycle is needed */
516 if (cmd->cmdidx == SD_CMD_SWITCH_UHS18V)
517 printf("CMD11 to switch to 1.8V mode failed, card requires power cycle.\n");
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500518 }
519
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100520 esdhc_write32(&regs->irqstat, -1);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500521
Andrew Gabbasova04a6e02014-03-24 02:41:06 -0500522 return err;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500523}
524
Kim Phillipsf9e0b602012-10-29 13:34:44 +0000525static void set_sysctl(struct mmc *mmc, uint clock)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500526{
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200527 int div = 1;
528#ifdef ARCH_MXC
529 int pre_div = 1;
530#else
531 int pre_div = 2;
532#endif
533 int ddr_pre_div = mmc->ddr_mode ? 2 : 1;
Peng Fana4d36f72016-03-25 14:16:56 +0800534 struct fsl_esdhc_priv *priv = mmc->priv;
535 struct fsl_esdhc *regs = priv->esdhc_regs;
536 int sdhc_clk = priv->sdhc_clk;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500537 uint clk;
538
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200539 if (clock < mmc->cfg->f_min)
540 clock = mmc->cfg->f_min;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100541
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200542 while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256)
543 pre_div *= 2;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500544
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200545 while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16)
546 div++;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500547
Benoît Thébaudeaue16e9222017-05-03 11:59:03 +0200548 pre_div >>= 1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500549 div -= 1;
550
551 clk = (pre_div << 8) | (div << 4);
552
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700553#ifdef CONFIG_FSL_USDHC
Ye Li5a24f292016-06-15 10:53:01 +0800554 esdhc_clrbits32(&regs->vendorspec, VENDORSPEC_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700555#else
Kumar Gala09876a32010-03-18 15:51:05 -0500556 esdhc_clrbits32(&regs->sysctl, SYSCTL_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700557#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100558
559 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_CLOCK_MASK, clk);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500560
561 udelay(10000);
562
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700563#ifdef CONFIG_FSL_USDHC
Ye Li5a24f292016-06-15 10:53:01 +0800564 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN | VENDORSPEC_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700565#else
566 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_CKEN);
567#endif
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100568
Andy Fleminge52ffb82008-10-30 16:47:16 -0500569}
570
Yangbo Lu163beec2015-04-22 13:57:40 +0800571#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
572static void esdhc_clock_control(struct mmc *mmc, bool enable)
573{
Peng Fana4d36f72016-03-25 14:16:56 +0800574 struct fsl_esdhc_priv *priv = mmc->priv;
575 struct fsl_esdhc *regs = priv->esdhc_regs;
Yangbo Lu163beec2015-04-22 13:57:40 +0800576 u32 value;
577 u32 time_out;
578
579 value = esdhc_read32(&regs->sysctl);
580
581 if (enable)
582 value |= SYSCTL_CKEN;
583 else
584 value &= ~SYSCTL_CKEN;
585
586 esdhc_write32(&regs->sysctl, value);
587
588 time_out = 20;
589 value = PRSSTAT_SDSTB;
590 while (!(esdhc_read32(&regs->prsstat) & value)) {
591 if (time_out == 0) {
592 printf("fsl_esdhc: Internal clock never stabilised.\n");
593 break;
594 }
595 time_out--;
596 mdelay(1);
597 }
598}
599#endif
600
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900601static int esdhc_set_ios(struct mmc *mmc)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500602{
Peng Fana4d36f72016-03-25 14:16:56 +0800603 struct fsl_esdhc_priv *priv = mmc->priv;
604 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500605
Yangbo Lu163beec2015-04-22 13:57:40 +0800606#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
607 /* Select to use peripheral clock */
608 esdhc_clock_control(mmc, false);
609 esdhc_setbits32(&regs->scr, ESDHCCTL_PCS);
610 esdhc_clock_control(mmc, true);
611#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500612 /* Set the clock speed */
613 set_sysctl(mmc, mmc->clock);
614
615 /* Set the bus width */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100616 esdhc_clrbits32(&regs->proctl, PROCTL_DTW_4 | PROCTL_DTW_8);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500617
618 if (mmc->bus_width == 4)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100619 esdhc_setbits32(&regs->proctl, PROCTL_DTW_4);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500620 else if (mmc->bus_width == 8)
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100621 esdhc_setbits32(&regs->proctl, PROCTL_DTW_8);
622
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900623 return 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500624}
625
626static int esdhc_init(struct mmc *mmc)
627{
Peng Fana4d36f72016-03-25 14:16:56 +0800628 struct fsl_esdhc_priv *priv = mmc->priv;
629 struct fsl_esdhc *regs = priv->esdhc_regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500630 int timeout = 1000;
631
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100632 /* Reset the entire host controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200633 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100634
635 /* Wait until the controller is available */
636 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
637 udelay(1000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500638
Peng Fana6eadd52016-06-15 10:53:00 +0800639#if defined(CONFIG_FSL_USDHC)
640 /* RSTA doesn't reset MMC_BOOT register, so manually reset it */
641 esdhc_write32(&regs->mmcboot, 0x0);
642 /* Reset MIX_CTRL and CLK_TUNE_CTRL_STATUS regs to 0 */
643 esdhc_write32(&regs->mixctrl, 0x0);
644 esdhc_write32(&regs->clktunectrlstatus, 0x0);
645
646 /* Put VEND_SPEC to default value */
647 esdhc_write32(&regs->vendorspec, VENDORSPEC_INIT);
648
649 /* Disable DLL_CTRL delay line */
650 esdhc_write32(&regs->dllctrl, 0x0);
651#endif
652
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000653#ifndef ARCH_MXC
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530654 /* Enable cache snooping */
Benoît Thébaudeauc08d11c2012-08-13 07:28:16 +0000655 esdhc_write32(&regs->scr, 0x00000040);
656#endif
P.V.Suresh7b1868b2010-12-04 10:37:23 +0530657
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700658#ifndef CONFIG_FSL_USDHC
Dirk Behmedbe67252013-07-15 15:44:29 +0200659 esdhc_setbits32(&regs->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN);
Ye Li5a24f292016-06-15 10:53:01 +0800660#else
661 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_HCKEN | VENDORSPEC_IPGEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700662#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500663
664 /* Set the initial clock speed */
Jerry Huang0caea1a2010-11-25 17:06:07 +0000665 mmc_set_clock(mmc, 400000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500666
667 /* Disable the BRR and BWR bits in IRQSTAT */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100668 esdhc_clrbits32(&regs->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500669
670 /* Put the PROCTL reg back to the default */
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100671 esdhc_write32(&regs->proctl, PROCTL_INIT);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500672
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100673 /* Set timout to the maximum value */
674 esdhc_clrsetbits32(&regs->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500675
Peng Fanaee78582017-06-12 17:50:53 +0800676 if (priv->vs18_enable)
677 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
678
Thierry Reding8cee4c982012-01-02 01:15:38 +0000679 return 0;
680}
681
682static int esdhc_getcd(struct mmc *mmc)
683{
Peng Fana4d36f72016-03-25 14:16:56 +0800684 struct fsl_esdhc_priv *priv = mmc->priv;
685 struct fsl_esdhc *regs = priv->esdhc_regs;
Thierry Reding8cee4c982012-01-02 01:15:38 +0000686 int timeout = 1000;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500687
Haijun.Zhang05f58542014-01-10 13:52:17 +0800688#ifdef CONFIG_ESDHC_DETECT_QUIRK
689 if (CONFIG_ESDHC_DETECT_QUIRK)
690 return 1;
691#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800692
693#ifdef CONFIG_DM_MMC
694 if (priv->non_removable)
695 return 1;
Yangbo Lub99647c2016-12-07 11:54:30 +0800696#ifdef CONFIG_DM_GPIO
Peng Fana4d36f72016-03-25 14:16:56 +0800697 if (dm_gpio_is_valid(&priv->cd_gpio))
698 return dm_gpio_get_value(&priv->cd_gpio);
699#endif
Yangbo Lub99647c2016-12-07 11:54:30 +0800700#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800701
Thierry Reding8cee4c982012-01-02 01:15:38 +0000702 while (!(esdhc_read32(&regs->prsstat) & PRSSTAT_CINS) && --timeout)
703 udelay(1000);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100704
Thierry Reding8cee4c982012-01-02 01:15:38 +0000705 return timeout > 0;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500706}
707
Jerry Huangb7ef7562010-03-18 15:57:06 -0500708static void esdhc_reset(struct fsl_esdhc *regs)
709{
710 unsigned long timeout = 100; /* wait max 100 ms */
711
712 /* reset the controller */
Dirk Behmedbe67252013-07-15 15:44:29 +0200713 esdhc_setbits32(&regs->sysctl, SYSCTL_RSTA);
Jerry Huangb7ef7562010-03-18 15:57:06 -0500714
715 /* hardware clears the bit when it is done */
716 while ((esdhc_read32(&regs->sysctl) & SYSCTL_RSTA) && --timeout)
717 udelay(1000);
718 if (!timeout)
719 printf("MMC/SD: Reset never completed.\n");
720}
721
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200722static const struct mmc_ops esdhc_ops = {
723 .send_cmd = esdhc_send_cmd,
724 .set_ios = esdhc_set_ios,
725 .init = esdhc_init,
726 .getcd = esdhc_getcd,
727};
728
Peng Fana4d36f72016-03-25 14:16:56 +0800729static int fsl_esdhc_init(struct fsl_esdhc_priv *priv)
Andy Fleminge52ffb82008-10-30 16:47:16 -0500730{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100731 struct fsl_esdhc *regs;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500732 struct mmc *mmc;
Li Yangd4933f22010-11-25 17:06:09 +0000733 u32 caps, voltage_caps;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500734
Peng Fana4d36f72016-03-25 14:16:56 +0800735 if (!priv)
736 return -EINVAL;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100737
Peng Fana4d36f72016-03-25 14:16:56 +0800738 regs = priv->esdhc_regs;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100739
Jerry Huangb7ef7562010-03-18 15:57:06 -0500740 /* First reset the eSDHC controller */
741 esdhc_reset(regs);
742
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700743#ifndef CONFIG_FSL_USDHC
Jerry Huang4e3bfa02012-05-17 23:57:02 +0000744 esdhc_setbits32(&regs->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN
745 | SYSCTL_IPGEN | SYSCTL_CKEN);
Ye Li5a24f292016-06-15 10:53:01 +0800746#else
747 esdhc_setbits32(&regs->vendorspec, VENDORSPEC_PEREN |
748 VENDORSPEC_HCKEN | VENDORSPEC_IPGEN | VENDORSPEC_CKEN);
Eric Nelsonc8e615c2015-12-04 12:32:48 -0700749#endif
Jerry Huang4e3bfa02012-05-17 23:57:02 +0000750
Peng Fanaee78582017-06-12 17:50:53 +0800751 if (priv->vs18_enable)
752 esdhc_setbits32(&regs->vendorspec, ESDHC_VENDORSPEC_VSELECT);
753
Ye.Li3d46c312014-11-04 15:35:49 +0800754 writel(SDHCI_IRQ_EN_BITS, &regs->irqstaten);
Peng Fana4d36f72016-03-25 14:16:56 +0800755 memset(&priv->cfg, 0, sizeof(priv->cfg));
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200756
Li Yangd4933f22010-11-25 17:06:09 +0000757 voltage_caps = 0;
Wang Huanc9292132014-09-05 13:52:40 +0800758 caps = esdhc_read32(&regs->hostcapblt);
Roy Zang39356612011-01-07 00:06:47 -0600759
760#ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135
761 caps = caps & ~(ESDHC_HOSTCAPBLT_SRS |
762 ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30);
763#endif
Haijun.Zhang8a065e92013-10-31 09:38:19 +0800764
765/* T4240 host controller capabilities register should have VS33 bit */
766#ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33
767 caps = caps | ESDHC_HOSTCAPBLT_VS33;
768#endif
769
Andy Fleminge52ffb82008-10-30 16:47:16 -0500770 if (caps & ESDHC_HOSTCAPBLT_VS18)
Li Yangd4933f22010-11-25 17:06:09 +0000771 voltage_caps |= MMC_VDD_165_195;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500772 if (caps & ESDHC_HOSTCAPBLT_VS30)
Li Yangd4933f22010-11-25 17:06:09 +0000773 voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500774 if (caps & ESDHC_HOSTCAPBLT_VS33)
Li Yangd4933f22010-11-25 17:06:09 +0000775 voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34;
776
Peng Fana4d36f72016-03-25 14:16:56 +0800777 priv->cfg.name = "FSL_SDHC";
778 priv->cfg.ops = &esdhc_ops;
Li Yangd4933f22010-11-25 17:06:09 +0000779#ifdef CONFIG_SYS_SD_VOLTAGE
Peng Fana4d36f72016-03-25 14:16:56 +0800780 priv->cfg.voltages = CONFIG_SYS_SD_VOLTAGE;
Li Yangd4933f22010-11-25 17:06:09 +0000781#else
Peng Fana4d36f72016-03-25 14:16:56 +0800782 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Li Yangd4933f22010-11-25 17:06:09 +0000783#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800784 if ((priv->cfg.voltages & voltage_caps) == 0) {
Li Yangd4933f22010-11-25 17:06:09 +0000785 printf("voltage not supported by controller\n");
786 return -1;
787 }
Andy Fleminge52ffb82008-10-30 16:47:16 -0500788
Peng Fana4d36f72016-03-25 14:16:56 +0800789 if (priv->bus_width == 8)
790 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
791 else if (priv->bus_width == 4)
792 priv->cfg.host_caps = MMC_MODE_4BIT;
793
794 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500795#ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE
Peng Fana4d36f72016-03-25 14:16:56 +0800796 priv->cfg.host_caps |= MMC_MODE_DDR_52MHz;
Volodymyr Riazantsevd251e112015-01-20 10:16:44 -0500797#endif
Andy Fleminge52ffb82008-10-30 16:47:16 -0500798
Peng Fana4d36f72016-03-25 14:16:56 +0800799 if (priv->bus_width > 0) {
800 if (priv->bus_width < 8)
801 priv->cfg.host_caps &= ~MMC_MODE_8BIT;
802 if (priv->bus_width < 4)
803 priv->cfg.host_caps &= ~MMC_MODE_4BIT;
Abbas Razae6bf9772013-03-25 09:13:34 +0000804 }
805
Andy Fleminge52ffb82008-10-30 16:47:16 -0500806 if (caps & ESDHC_HOSTCAPBLT_HSS)
Peng Fana4d36f72016-03-25 14:16:56 +0800807 priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500808
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800809#ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK
810 if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK)
Peng Fana4d36f72016-03-25 14:16:56 +0800811 priv->cfg.host_caps &= ~MMC_MODE_8BIT;
Haijun.Zhangf0fe8ad2014-01-10 13:52:18 +0800812#endif
813
Peng Fana4d36f72016-03-25 14:16:56 +0800814 priv->cfg.f_min = 400000;
815 priv->cfg.f_max = min(priv->sdhc_clk, (u32)52000000);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500816
Peng Fana4d36f72016-03-25 14:16:56 +0800817 priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200818
Peng Fana4d36f72016-03-25 14:16:56 +0800819 mmc = mmc_create(&priv->cfg, priv);
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200820 if (mmc == NULL)
821 return -1;
Andy Fleminge52ffb82008-10-30 16:47:16 -0500822
Peng Fana4d36f72016-03-25 14:16:56 +0800823 priv->mmc = mmc;
824
825 return 0;
826}
827
Jagan Teki3c2cc6d2017-05-12 17:18:20 +0530828#ifndef CONFIG_DM_MMC
829static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg,
830 struct fsl_esdhc_priv *priv)
831{
832 if (!cfg || !priv)
833 return -EINVAL;
834
835 priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base);
836 priv->bus_width = cfg->max_bus_width;
837 priv->sdhc_clk = cfg->sdhc_clk;
838 priv->wp_enable = cfg->wp_enable;
Peng Fanaee78582017-06-12 17:50:53 +0800839 priv->vs18_enable = cfg->vs18_enable;
Jagan Teki3c2cc6d2017-05-12 17:18:20 +0530840
841 return 0;
842};
843
Peng Fana4d36f72016-03-25 14:16:56 +0800844int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg)
845{
846 struct fsl_esdhc_priv *priv;
847 int ret;
848
849 if (!cfg)
850 return -EINVAL;
851
852 priv = calloc(sizeof(struct fsl_esdhc_priv), 1);
853 if (!priv)
854 return -ENOMEM;
855
856 ret = fsl_esdhc_cfg_to_priv(cfg, priv);
857 if (ret) {
858 debug("%s xlate failure\n", __func__);
859 free(priv);
860 return ret;
861 }
862
863 ret = fsl_esdhc_init(priv);
864 if (ret) {
865 debug("%s init failure\n", __func__);
866 free(priv);
867 return ret;
868 }
869
Andy Fleminge52ffb82008-10-30 16:47:16 -0500870 return 0;
871}
872
873int fsl_esdhc_mmc_init(bd_t *bis)
874{
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100875 struct fsl_esdhc_cfg *cfg;
876
Fabio Estevam6592a992012-12-27 08:51:08 +0000877 cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1);
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100878 cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR;
Simon Glass9e247d12012-12-13 20:49:05 +0000879 cfg->sdhc_clk = gd->arch.sdhc_clk;
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100880 return fsl_esdhc_initialize(bis, cfg);
Andy Fleminge52ffb82008-10-30 16:47:16 -0500881}
Jagan Teki3c2cc6d2017-05-12 17:18:20 +0530882#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400883
Yangbo Lub124f8a2015-04-22 13:57:00 +0800884#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
885void mmc_adapter_card_type_ident(void)
886{
887 u8 card_id;
888 u8 value;
889
890 card_id = QIXIS_READ(present) & QIXIS_SDID_MASK;
891 gd->arch.sdhc_adapter = card_id;
892
893 switch (card_id) {
894 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45:
Yangbo Lu81eacd62015-09-17 10:27:12 +0800895 value = QIXIS_READ(brdcfg[5]);
896 value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7);
897 QIXIS_WRITE(brdcfg[5], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +0800898 break;
899 case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY:
Yangbo Luc6799ce2015-09-17 10:27:48 +0800900 value = QIXIS_READ(pwr_ctl[1]);
901 value |= QIXIS_EVDD_BY_SDHC_VS;
902 QIXIS_WRITE(pwr_ctl[1], value);
Yangbo Lub124f8a2015-04-22 13:57:00 +0800903 break;
904 case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44:
905 value = QIXIS_READ(brdcfg[5]);
906 value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT);
907 QIXIS_WRITE(brdcfg[5], value);
908 break;
909 case QIXIS_ESDHC_ADAPTER_TYPE_RSV:
910 break;
911 case QIXIS_ESDHC_ADAPTER_TYPE_MMC:
912 break;
913 case QIXIS_ESDHC_ADAPTER_TYPE_SD:
914 break;
915 case QIXIS_ESDHC_NO_ADAPTER:
916 break;
917 default:
918 break;
919 }
920}
921#endif
922
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100923#ifdef CONFIG_OF_LIBFDT
Yangbo Lud84139c2017-01-17 10:43:54 +0800924__weak int esdhc_status_fixup(void *blob, const char *compat)
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400925{
Chenhui Zhao025eab02011-01-04 17:23:05 +0800926#ifdef CONFIG_FSL_ESDHC_PIN_MUX
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400927 if (!hwconfig("esdhc")) {
Chenhui Zhao025eab02011-01-04 17:23:05 +0800928 do_fixup_by_compat(blob, compat, "status", "disabled",
Yangbo Lud84139c2017-01-17 10:43:54 +0800929 sizeof("disabled"), 1);
930 return 1;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400931 }
Chenhui Zhao025eab02011-01-04 17:23:05 +0800932#endif
Yangbo Lud84139c2017-01-17 10:43:54 +0800933 do_fixup_by_compat(blob, compat, "status", "okay",
934 sizeof("okay"), 1);
935 return 0;
936}
937
938void fdt_fixup_esdhc(void *blob, bd_t *bd)
939{
940 const char *compat = "fsl,esdhc";
941
942 if (esdhc_status_fixup(blob, compat))
943 return;
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400944
Yangbo Lu163beec2015-04-22 13:57:40 +0800945#ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK
946 do_fixup_by_compat_u32(blob, compat, "peripheral-frequency",
947 gd->arch.sdhc_clk, 1);
948#else
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400949 do_fixup_by_compat_u32(blob, compat, "clock-frequency",
Simon Glass9e247d12012-12-13 20:49:05 +0000950 gd->arch.sdhc_clk, 1);
Yangbo Lu163beec2015-04-22 13:57:40 +0800951#endif
Yangbo Lub124f8a2015-04-22 13:57:00 +0800952#ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
953 do_fixup_by_compat_u32(blob, compat, "adapter-type",
954 (u32)(gd->arch.sdhc_adapter), 1);
955#endif
Anton Vorontsovf751a3c2009-06-10 00:25:29 +0400956}
Stefano Babicff7a5ca2010-02-05 15:11:27 +0100957#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800958
959#ifdef CONFIG_DM_MMC
960#include <asm/arch/clock.h>
Peng Fanaf6dbc02017-02-22 16:21:55 +0800961__weak void init_clk_usdhc(u32 index)
962{
963}
964
Peng Fana4d36f72016-03-25 14:16:56 +0800965static int fsl_esdhc_probe(struct udevice *dev)
966{
967 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
968 struct fsl_esdhc_priv *priv = dev_get_priv(dev);
969 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700970 int node = dev_of_offset(dev);
Peng Fan5eb8b432017-06-12 17:50:54 +0800971 struct udevice *vqmmc_dev;
Peng Fana4d36f72016-03-25 14:16:56 +0800972 fdt_addr_t addr;
973 unsigned int val;
974 int ret;
975
Simon Glassba1dea42017-05-17 17:18:05 -0600976 addr = devfdt_get_addr(dev);
Peng Fana4d36f72016-03-25 14:16:56 +0800977 if (addr == FDT_ADDR_T_NONE)
978 return -EINVAL;
979
980 priv->esdhc_regs = (struct fsl_esdhc *)addr;
981 priv->dev = dev;
982
983 val = fdtdec_get_int(fdt, node, "bus-width", -1);
984 if (val == 8)
985 priv->bus_width = 8;
986 else if (val == 4)
987 priv->bus_width = 4;
988 else
989 priv->bus_width = 1;
990
991 if (fdt_get_property(fdt, node, "non-removable", NULL)) {
992 priv->non_removable = 1;
993 } else {
994 priv->non_removable = 0;
Yangbo Lub99647c2016-12-07 11:54:30 +0800995#ifdef CONFIG_DM_GPIO
Simon Glass1d9af1f2017-05-30 21:47:09 -0600996 gpio_request_by_name_nodev(offset_to_ofnode(node), "cd-gpios",
997 0, &priv->cd_gpio, GPIOD_IS_IN);
Yangbo Lub99647c2016-12-07 11:54:30 +0800998#endif
Peng Fana4d36f72016-03-25 14:16:56 +0800999 }
1000
Peng Fan01eb1c42016-06-15 10:53:02 +08001001 priv->wp_enable = 1;
1002
Yangbo Lub99647c2016-12-07 11:54:30 +08001003#ifdef CONFIG_DM_GPIO
Simon Glass1d9af1f2017-05-30 21:47:09 -06001004 ret = gpio_request_by_name_nodev(offset_to_ofnode(node), "wp-gpios", 0,
Peng Fan01eb1c42016-06-15 10:53:02 +08001005 &priv->wp_gpio, GPIOD_IS_IN);
1006 if (ret)
1007 priv->wp_enable = 0;
Yangbo Lub99647c2016-12-07 11:54:30 +08001008#endif
Peng Fan5eb8b432017-06-12 17:50:54 +08001009
1010 priv->vs18_enable = 0;
1011
1012#ifdef CONFIG_DM_REGULATOR
1013 /*
1014 * If emmc I/O has a fixed voltage at 1.8V, this must be provided,
1015 * otherwise, emmc will work abnormally.
1016 */
1017 ret = device_get_supply_regulator(dev, "vqmmc-supply", &vqmmc_dev);
1018 if (ret) {
1019 dev_dbg(dev, "no vqmmc-supply\n");
1020 } else {
1021 ret = regulator_set_enable(vqmmc_dev, true);
1022 if (ret) {
1023 dev_err(dev, "fail to enable vqmmc-supply\n");
1024 return ret;
1025 }
1026
1027 if (regulator_get_value(vqmmc_dev) == 1800000)
1028 priv->vs18_enable = 1;
1029 }
1030#endif
1031
Peng Fana4d36f72016-03-25 14:16:56 +08001032 /*
1033 * TODO:
1034 * Because lack of clk driver, if SDHC clk is not enabled,
1035 * need to enable it first before this driver is invoked.
1036 *
1037 * we use MXC_ESDHC_CLK to get clk freq.
1038 * If one would like to make this function work,
1039 * the aliases should be provided in dts as this:
1040 *
1041 * aliases {
1042 * mmc0 = &usdhc1;
1043 * mmc1 = &usdhc2;
1044 * mmc2 = &usdhc3;
1045 * mmc3 = &usdhc4;
1046 * };
1047 * Then if your board only supports mmc2 and mmc3, but we can
1048 * correctly get the seq as 2 and 3, then let mxc_get_clock
1049 * work as expected.
1050 */
Peng Fanaf6dbc02017-02-22 16:21:55 +08001051
1052 init_clk_usdhc(dev->seq);
1053
Peng Fana4d36f72016-03-25 14:16:56 +08001054 priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq);
1055 if (priv->sdhc_clk <= 0) {
1056 dev_err(dev, "Unable to get clk for %s\n", dev->name);
1057 return -EINVAL;
1058 }
1059
1060 ret = fsl_esdhc_init(priv);
1061 if (ret) {
1062 dev_err(dev, "fsl_esdhc_init failure\n");
1063 return ret;
1064 }
1065
1066 upriv->mmc = priv->mmc;
Peng Fand0a0c1d2016-08-11 14:02:56 +08001067 priv->mmc->dev = dev;
Peng Fana4d36f72016-03-25 14:16:56 +08001068
1069 return 0;
1070}
1071
1072static const struct udevice_id fsl_esdhc_ids[] = {
1073 { .compatible = "fsl,imx6ul-usdhc", },
1074 { .compatible = "fsl,imx6sx-usdhc", },
1075 { .compatible = "fsl,imx6sl-usdhc", },
1076 { .compatible = "fsl,imx6q-usdhc", },
1077 { .compatible = "fsl,imx7d-usdhc", },
Peng Fanaf6dbc02017-02-22 16:21:55 +08001078 { .compatible = "fsl,imx7ulp-usdhc", },
Yangbo Lu2a99b602016-12-07 11:54:31 +08001079 { .compatible = "fsl,esdhc", },
Peng Fana4d36f72016-03-25 14:16:56 +08001080 { /* sentinel */ }
1081};
1082
1083U_BOOT_DRIVER(fsl_esdhc) = {
1084 .name = "fsl-esdhc-mmc",
1085 .id = UCLASS_MMC,
1086 .of_match = fsl_esdhc_ids,
1087 .probe = fsl_esdhc_probe,
1088 .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv),
1089};
1090#endif