Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 2 | /* |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 3 | * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc |
Yangbo Lu | 4cc119b | 2019-05-23 11:05:46 +0800 | [diff] [blame] | 4 | * Copyright 2019 NXP Semiconductors |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 5 | * 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 Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <config.h> |
| 13 | #include <common.h> |
| 14 | #include <command.h> |
Peng Fan | 4c286b7 | 2018-10-18 14:28:35 +0200 | [diff] [blame] | 15 | #include <clk.h> |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 16 | #include <errno.h> |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 17 | #include <hwconfig.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 18 | #include <mmc.h> |
| 19 | #include <part.h> |
| 20 | #include <malloc.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 21 | #include <fsl_esdhc.h> |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 22 | #include <fdt_support.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 23 | #include <asm/io.h> |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 24 | #include <dm.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 25 | |
Yangbo Lu | 4cc119b | 2019-05-23 11:05:46 +0800 | [diff] [blame] | 26 | #if !CONFIG_IS_ENABLED(BLK) |
| 27 | #include "mmc_private.h" |
| 28 | #endif |
| 29 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Ye.Li | 3d46c31 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 32 | #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 Lu | 4cc119b | 2019-05-23 11:05:46 +0800 | [diff] [blame] | 38 | #define ESDHC_DRIVER_STAGE_VALUE 0xffffffff |
Ye.Li | 3d46c31 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 39 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 40 | struct fsl_esdhc { |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 41 | 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 Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 59 | char reserved1[8]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 60 | uint fevt; /* Force event register */ |
| 61 | uint admaes; /* ADMA error status register */ |
| 62 | uint adsaddr; /* ADMA system address register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 63 | char reserved2[160]; |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 64 | uint hostver; /* Host controller version register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 65 | char reserved3[4]; /* reserved */ |
Peng Fan | b9b4236 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 66 | uint dmaerraddr; /* DMA error address register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 67 | char reserved4[4]; /* reserved */ |
Peng Fan | b9b4236 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 68 | uint dmaerrattr; /* DMA error attribute register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 69 | char reserved5[4]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 70 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 71 | char reserved6[756]; /* reserved */ |
| 72 | uint esdhcctl; /* eSDHC control register */ |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 73 | }; |
| 74 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 75 | struct fsl_esdhc_plat { |
| 76 | struct mmc_config cfg; |
| 77 | struct mmc mmc; |
| 78 | }; |
| 79 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 80 | /** |
| 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 Fan | 01eb1c4 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 91 | * @wp_enable: 1: enable checking wp; 0: no check |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 92 | * @cd_gpio: gpio for card detection |
Peng Fan | 01eb1c4 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 93 | * @wp_gpio: gpio for write protection |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 94 | */ |
| 95 | struct fsl_esdhc_priv { |
| 96 | struct fsl_esdhc *esdhc_regs; |
| 97 | unsigned int sdhc_clk; |
Peng Fan | 4c286b7 | 2018-10-18 14:28:35 +0200 | [diff] [blame] | 98 | struct clk per_clk; |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 99 | unsigned int clock; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 100 | unsigned int bus_width; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 101 | #if !CONFIG_IS_ENABLED(BLK) |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 102 | struct mmc *mmc; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 103 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 104 | struct udevice *dev; |
| 105 | int non_removable; |
Peng Fan | 01eb1c4 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 106 | int wp_enable; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 107 | }; |
| 108 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 109 | /* Return the XFERTYP flags for a given command and data packet */ |
Kim Phillips | f9e0b60 | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 110 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 111 | { |
| 112 | uint xfertyp = 0; |
| 113 | |
| 114 | if (data) { |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 115 | xfertyp |= XFERTYP_DPSEL; |
| 116 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 117 | xfertyp |= XFERTYP_DMAEN; |
| 118 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 119 | if (data->blocks > 1) { |
| 120 | xfertyp |= XFERTYP_MSBSEL; |
| 121 | xfertyp |= XFERTYP_BCEN; |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 122 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 123 | xfertyp |= XFERTYP_AC12EN; |
| 124 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 125 | } |
| 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 Liu | bef0ff0 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 142 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 143 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
Yangbo Lu | b73a3d6 | 2016-01-21 17:33:19 +0800 | [diff] [blame] | 144 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 145 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 146 | } |
| 147 | |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 148 | #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 Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 152 | static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv, |
| 153 | struct mmc_data *data) |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 154 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 155 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 156 | uint blocks; |
| 157 | char *buffer; |
| 158 | uint databuf; |
| 159 | uint size; |
| 160 | uint irqstat; |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 161 | ulong start; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 162 | |
| 163 | if (data->flags & MMC_DATA_READ) { |
| 164 | blocks = data->blocks; |
| 165 | buffer = data->dest; |
| 166 | while (blocks) { |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 167 | start = get_timer(0); |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 168 | size = data->blocksize; |
| 169 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 170 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) { |
| 171 | if (get_timer(start) > PIO_TIMEOUT) { |
| 172 | printf("\nData Read Failed in PIO Mode."); |
| 173 | return; |
| 174 | } |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 175 | } |
| 176 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 177 | udelay(100); /* Wait before last byte transfer complete */ |
| 178 | irqstat = esdhc_read32(®s->irqstat); |
| 179 | databuf = in_le32(®s->datport); |
| 180 | *((uint *)buffer) = databuf; |
| 181 | buffer += 4; |
| 182 | size -= 4; |
| 183 | } |
| 184 | blocks--; |
| 185 | } |
| 186 | } else { |
| 187 | blocks = data->blocks; |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 188 | buffer = (char *)data->src; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 189 | while (blocks) { |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 190 | start = get_timer(0); |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 191 | size = data->blocksize; |
| 192 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 193 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) { |
| 194 | if (get_timer(start) > PIO_TIMEOUT) { |
| 195 | printf("\nData Write Failed in PIO Mode."); |
| 196 | return; |
| 197 | } |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 198 | } |
| 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(®s->irqstat); |
| 205 | out_le32(®s->datport, databuf); |
| 206 | } |
| 207 | blocks--; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | #endif |
| 212 | |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 213 | static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 214 | struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 215 | { |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 216 | int timeout; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 217 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 218 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 219 | dma_addr_t addr; |
| 220 | #endif |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 221 | uint wml_value; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 222 | |
| 223 | wml_value = data->blocksize/4; |
| 224 | |
| 225 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 0244963 | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 226 | if (wml_value > WML_RD_WML_MAX) |
| 227 | wml_value = WML_RD_WML_MAX_VAL; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 228 | |
Roy Zang | e5853af | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 229 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 230 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 231 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 232 | 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(®s->dsaddr, lower_32_bits(addr)); |
| 237 | #else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 238 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 239 | #endif |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 240 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 241 | } else { |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 242 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 243 | flush_dcache_range((ulong)data->src, |
| 244 | (ulong)data->src+data->blocks |
| 245 | *data->blocksize); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 246 | #endif |
Priyanka Jain | 0244963 | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 247 | if (wml_value > WML_WR_WML_MAX) |
| 248 | wml_value = WML_WR_WML_MAX_VAL; |
Peng Fan | 01eb1c4 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 249 | if (priv->wp_enable) { |
| 250 | if ((esdhc_read32(®s->prsstat) & |
| 251 | PRSSTAT_WPSPL) == 0) { |
| 252 | printf("\nThe SD card is locked. Can not write to a locked card.\n\n"); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 253 | return -ETIMEDOUT; |
Peng Fan | 01eb1c4 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 254 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 255 | } |
Roy Zang | e5853af | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 256 | |
| 257 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 258 | wml_value << 16); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 259 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 260 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 261 | 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(®s->dsaddr, lower_32_bits(addr)); |
| 266 | #else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 267 | esdhc_write32(®s->dsaddr, (u32)data->src); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 268 | #endif |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 269 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 270 | } |
| 271 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 272 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 273 | |
| 274 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 275 | /* |
| 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 Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 280 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 281 | * As 1) >= 2) |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 282 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 283 | * Taking log2 both the sides |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 284 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 285 | * Rounding up to next power of 2 |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 286 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 287 | * => timeout + 13 = fls(mmc->clock/4) |
Yangbo Lu | 9d7f321 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 288 | * |
| 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 Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 295 | */ |
Yangbo Lu | 9d7f321 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 296 | timeout = fls(mmc->clock/2); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 297 | timeout -= 13; |
| 298 | |
| 299 | if (timeout > 14) |
| 300 | timeout = 14; |
| 301 | |
| 302 | if (timeout < 0) |
| 303 | timeout = 0; |
| 304 | |
Kumar Gala | 9a878d5 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 305 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 306 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 307 | timeout++; |
| 308 | #endif |
| 309 | |
Haijun.Zhang | edeb83a | 2014-03-18 17:04:23 +0800 | [diff] [blame] | 310 | #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE |
| 311 | timeout = 0xE; |
| 312 | #endif |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 313 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 318 | static void check_and_invalidate_dcache_range |
| 319 | (struct mmc_cmd *cmd, |
| 320 | struct mmc_data *data) { |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 321 | unsigned start = 0; |
Yangbo Lu | e7702c6 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 322 | unsigned end = 0; |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 323 | unsigned size = roundup(ARCH_DMA_MINALIGN, |
| 324 | data->blocks*data->blocksize); |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 325 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 326 | 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 Lu | e7702c6 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 333 | #else |
| 334 | start = (unsigned)data->dest; |
Yangbo Lu | d0e295d | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 335 | #endif |
Yangbo Lu | e7702c6 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 336 | end = start + size; |
Eric Nelson | 30e9cad | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 337 | invalidate_dcache_range(start, end); |
| 338 | } |
Angelo Dureghello | 520a669 | 2019-01-19 10:40:38 +0100 | [diff] [blame] | 339 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 340 | /* |
| 341 | * Sends a command out on the bus. Takes the mmc pointer, |
| 342 | * a command pointer, and an optional data pointer. |
| 343 | */ |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 344 | static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 345 | struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 346 | { |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 347 | int err = 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 348 | uint xfertyp; |
| 349 | uint irqstat; |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 350 | u32 flags = IRQSTAT_CC | IRQSTAT_CTOE; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 351 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Fabio Estevam | 7300ef5 | 2018-11-19 10:31:53 -0200 | [diff] [blame] | 352 | unsigned long start; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 353 | |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 354 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 355 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 356 | return 0; |
| 357 | #endif |
| 358 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 359 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 360 | |
| 361 | sync(); |
| 362 | |
| 363 | /* Wait for the bus to be idle */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 364 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 365 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 366 | ; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 367 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 368 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 369 | ; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 370 | |
| 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 Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 380 | err = esdhc_setup_data(priv, mmc, data); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 381 | if(err) |
| 382 | return err; |
Peng Fan | 9cb5e99 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 383 | |
| 384 | if (data->flags & MMC_DATA_READ) |
| 385 | check_and_invalidate_dcache_range(cmd, data); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /* Figure out the transfer arguments */ |
| 389 | xfertyp = esdhc_xfertyp(cmd, data); |
| 390 | |
Andrew Gabbasov | 4816b7a | 2013-06-11 10:34:22 -0500 | [diff] [blame] | 391 | /* Mask all irqs */ |
| 392 | esdhc_write32(®s->irqsigen, 0); |
| 393 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 394 | /* Send the command */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 395 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
| 396 | esdhc_write32(®s->xfertyp, xfertyp); |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 397 | |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 398 | if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) || |
| 399 | (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) |
| 400 | flags = IRQSTAT_BRR; |
| 401 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 402 | /* Wait for the command to complete */ |
Fabio Estevam | 7300ef5 | 2018-11-19 10:31:53 -0200 | [diff] [blame] | 403 | start = get_timer(0); |
| 404 | while (!(esdhc_read32(®s->irqstat) & flags)) { |
| 405 | if (get_timer(start) > 1000) { |
| 406 | err = -ETIMEDOUT; |
| 407 | goto out; |
| 408 | } |
| 409 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 410 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 411 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 412 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 413 | if (irqstat & CMD_ERR) { |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 414 | err = -ECOMM; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 415 | goto out; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 418 | if (irqstat & IRQSTAT_CTOE) { |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 419 | err = -ETIMEDOUT; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 420 | goto out; |
| 421 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 422 | |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 423 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 424 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
Yangbo Lu | 3ffa851 | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 425 | int timeout = 6000; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 426 | |
Yangbo Lu | 3ffa851 | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 427 | /* Poll on DATA0 line for cmd with busy signal for 600 ms */ |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 428 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 429 | PRSSTAT_DAT0)) { |
| 430 | udelay(100); |
| 431 | timeout--; |
| 432 | } |
| 433 | |
| 434 | if (timeout <= 0) { |
| 435 | printf("Timeout waiting for DAT0 to go high!\n"); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 436 | err = -ETIMEDOUT; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 437 | goto out; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 441 | /* Copy the response to the response buffer */ |
| 442 | if (cmd->resp_type & MMC_RSP_136) { |
| 443 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 444 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 445 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 446 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 447 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 448 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 449 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 450 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 451 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 452 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 453 | } else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 454 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 455 | |
| 456 | /* Wait until all of the blocks are transferred */ |
| 457 | if (data) { |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 458 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 459 | esdhc_pio_read_write(priv, data); |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 460 | #else |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 461 | flags = DATA_COMPLETE; |
| 462 | if ((cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK) || |
| 463 | (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200)) { |
| 464 | flags = IRQSTAT_BRR; |
| 465 | } |
| 466 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 467 | do { |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 468 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 469 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 470 | if (irqstat & IRQSTAT_DTOE) { |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 471 | err = -ETIMEDOUT; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 472 | goto out; |
| 473 | } |
Frans Meulenbroeks | 010ba98 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 474 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 475 | if (irqstat & DATA_ERR) { |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 476 | err = -ECOMM; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 477 | goto out; |
| 478 | } |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 479 | } while ((irqstat & flags) != flags); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 480 | |
Peng Fan | 9cb5e99 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 481 | /* |
| 482 | * Need invalidate the dcache here again to avoid any |
| 483 | * cache-fill during the DMA operations such as the |
| 484 | * speculative pre-fetching etc. |
| 485 | */ |
Angelo Dureghello | 520a669 | 2019-01-19 10:40:38 +0100 | [diff] [blame] | 486 | if (data->flags & MMC_DATA_READ) { |
Eric Nelson | 70e6869 | 2013-04-03 12:31:56 +0000 | [diff] [blame] | 487 | check_and_invalidate_dcache_range(cmd, data); |
Angelo Dureghello | 520a669 | 2019-01-19 10:40:38 +0100 | [diff] [blame] | 488 | } |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 489 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 490 | } |
| 491 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 492 | out: |
| 493 | /* Reset CMD and DATA portions on error */ |
| 494 | if (err) { |
| 495 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 496 | SYSCTL_RSTC); |
| 497 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 498 | ; |
| 499 | |
| 500 | if (data) { |
| 501 | esdhc_write32(®s->sysctl, |
| 502 | esdhc_read32(®s->sysctl) | |
| 503 | SYSCTL_RSTD); |
| 504 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 505 | ; |
| 506 | } |
| 507 | } |
| 508 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 509 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 510 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 511 | return err; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 512 | } |
| 513 | |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 514 | static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 515 | { |
Benoît Thébaudeau | 22464e0 | 2018-01-16 22:44:18 +0100 | [diff] [blame] | 516 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Benoît Thébaudeau | e16e922 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 517 | int div = 1; |
Benoît Thébaudeau | e16e922 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 518 | int pre_div = 2; |
Lukasz Majewski | 2a52183 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 519 | int ddr_pre_div = mmc->ddr_mode ? 2 : 1; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 520 | int sdhc_clk = priv->sdhc_clk; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 521 | uint clk; |
| 522 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 523 | if (clock < mmc->cfg->f_min) |
| 524 | clock = mmc->cfg->f_min; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 525 | |
Lukasz Majewski | 2a52183 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 526 | while (sdhc_clk / (16 * pre_div * ddr_pre_div) > clock && pre_div < 256) |
| 527 | pre_div *= 2; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 528 | |
Lukasz Majewski | 2a52183 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 529 | while (sdhc_clk / (div * pre_div * ddr_pre_div) > clock && div < 16) |
| 530 | div++; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 531 | |
Benoît Thébaudeau | e16e922 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 532 | pre_div >>= 1; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 533 | div -= 1; |
| 534 | |
| 535 | clk = (pre_div << 8) | (div << 4); |
| 536 | |
Kumar Gala | 09876a3 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 537 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 538 | |
| 539 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 540 | |
| 541 | udelay(10000); |
| 542 | |
Eric Nelson | c8e615c | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 543 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 544 | |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 545 | priv->clock = clock; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 546 | } |
| 547 | |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 548 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 549 | static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable) |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 550 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 551 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 552 | u32 value; |
| 553 | u32 time_out; |
| 554 | |
| 555 | value = esdhc_read32(®s->sysctl); |
| 556 | |
| 557 | if (enable) |
| 558 | value |= SYSCTL_CKEN; |
| 559 | else |
| 560 | value &= ~SYSCTL_CKEN; |
| 561 | |
| 562 | esdhc_write32(®s->sysctl, value); |
| 563 | |
| 564 | time_out = 20; |
| 565 | value = PRSSTAT_SDSTB; |
| 566 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 567 | if (time_out == 0) { |
| 568 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 569 | break; |
| 570 | } |
| 571 | time_out--; |
| 572 | mdelay(1); |
| 573 | } |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 574 | } |
| 575 | #endif |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 576 | |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 577 | static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 578 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 579 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 580 | |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 581 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 582 | /* Select to use peripheral clock */ |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 583 | esdhc_clock_control(priv, false); |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 584 | esdhc_setbits32(®s->esdhcctl, ESDHCCTL_PCS); |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 585 | esdhc_clock_control(priv, true); |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 586 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 587 | /* Set the clock speed */ |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 588 | if (priv->clock != mmc->clock) |
| 589 | set_sysctl(priv, mmc, mmc->clock); |
| 590 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 591 | /* Set the bus width */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 592 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 593 | |
| 594 | if (mmc->bus_width == 4) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 595 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 596 | else if (mmc->bus_width == 8) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 597 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 598 | |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 599 | return 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 600 | } |
| 601 | |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 602 | static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 603 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 604 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Simon Glass | 0c3ef22 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 605 | ulong start; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 606 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 607 | /* Reset the entire host controller */ |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 608 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 609 | |
| 610 | /* Wait until the controller is available */ |
Simon Glass | 0c3ef22 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 611 | start = get_timer(0); |
| 612 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 613 | if (get_timer(start) > 1000) |
| 614 | return -ETIMEDOUT; |
| 615 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 616 | |
P.V.Suresh | 7b1868b | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 617 | /* Enable cache snooping */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 618 | esdhc_write32(®s->esdhcctl, 0x00000040); |
P.V.Suresh | 7b1868b | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 619 | |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 620 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 621 | |
| 622 | /* Set the initial clock speed */ |
Jaehoon Chung | 239cb2f | 2018-01-26 19:25:29 +0900 | [diff] [blame] | 623 | mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 624 | |
| 625 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 626 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 627 | |
| 628 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 629 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 630 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 631 | /* Set timout to the maximum value */ |
| 632 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 633 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 637 | static int esdhc_getcd_common(struct fsl_esdhc_priv *priv) |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 638 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 639 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 640 | int timeout = 1000; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 641 | |
Haijun.Zhang | 05f5854 | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 642 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 643 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 644 | return 1; |
| 645 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 646 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 647 | #if CONFIG_IS_ENABLED(DM_MMC) |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 648 | if (priv->non_removable) |
| 649 | return 1; |
Yangbo Lu | b99647c | 2016-12-07 11:54:30 +0800 | [diff] [blame] | 650 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 651 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 652 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 653 | udelay(1000); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 654 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 655 | return timeout > 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 656 | } |
| 657 | |
Simon Glass | 81357b5 | 2017-07-29 11:35:19 -0600 | [diff] [blame] | 658 | static int esdhc_reset(struct fsl_esdhc *regs) |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 659 | { |
Simon Glass | 81357b5 | 2017-07-29 11:35:19 -0600 | [diff] [blame] | 660 | ulong start; |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 661 | |
| 662 | /* reset the controller */ |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 663 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 664 | |
| 665 | /* hardware clears the bit when it is done */ |
Simon Glass | 81357b5 | 2017-07-29 11:35:19 -0600 | [diff] [blame] | 666 | start = get_timer(0); |
| 667 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 668 | if (get_timer(start) > 100) { |
| 669 | printf("MMC/SD: Reset never completed.\n"); |
| 670 | return -ETIMEDOUT; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | return 0; |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 675 | } |
| 676 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 677 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 678 | static int esdhc_getcd(struct mmc *mmc) |
| 679 | { |
| 680 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 681 | |
| 682 | return esdhc_getcd_common(priv); |
| 683 | } |
| 684 | |
| 685 | static int esdhc_init(struct mmc *mmc) |
| 686 | { |
| 687 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 688 | |
| 689 | return esdhc_init_common(priv, mmc); |
| 690 | } |
| 691 | |
| 692 | static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 693 | struct mmc_data *data) |
| 694 | { |
| 695 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 696 | |
| 697 | return esdhc_send_cmd_common(priv, mmc, cmd, data); |
| 698 | } |
| 699 | |
| 700 | static int esdhc_set_ios(struct mmc *mmc) |
| 701 | { |
| 702 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 703 | |
| 704 | return esdhc_set_ios_common(priv, mmc); |
| 705 | } |
| 706 | |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 707 | static const struct mmc_ops esdhc_ops = { |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 708 | .getcd = esdhc_getcd, |
| 709 | .init = esdhc_init, |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 710 | .send_cmd = esdhc_send_cmd, |
| 711 | .set_ios = esdhc_set_ios, |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 712 | }; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 713 | #endif |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 714 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 715 | static int fsl_esdhc_init(struct fsl_esdhc_priv *priv, |
| 716 | struct fsl_esdhc_plat *plat) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 717 | { |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 718 | struct mmc_config *cfg; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 719 | struct fsl_esdhc *regs; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 720 | u32 caps, voltage_caps; |
Simon Glass | 81357b5 | 2017-07-29 11:35:19 -0600 | [diff] [blame] | 721 | int ret; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 722 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 723 | if (!priv) |
| 724 | return -EINVAL; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 725 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 726 | regs = priv->esdhc_regs; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 727 | |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 728 | /* First reset the eSDHC controller */ |
Simon Glass | 81357b5 | 2017-07-29 11:35:19 -0600 | [diff] [blame] | 729 | ret = esdhc_reset(regs); |
| 730 | if (ret) |
| 731 | return ret; |
Jerry Huang | b7ef756 | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 732 | |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 733 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_HCKEN | |
| 734 | SYSCTL_IPGEN | SYSCTL_CKEN); |
Peng Fan | aee7858 | 2017-06-12 17:50:53 +0800 | [diff] [blame] | 735 | |
Ye.Li | 3d46c31 | 2014-11-04 15:35:49 +0800 | [diff] [blame] | 736 | writel(SDHCI_IRQ_EN_BITS, ®s->irqstaten); |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 737 | cfg = &plat->cfg; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 738 | #ifndef CONFIG_DM_MMC |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 739 | memset(cfg, '\0', sizeof(*cfg)); |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 740 | #endif |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 741 | |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 742 | voltage_caps = 0; |
Wang Huan | c929213 | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 743 | caps = esdhc_read32(®s->hostcapblt); |
Roy Zang | 3935661 | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 744 | |
| 745 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
| 746 | caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | |
| 747 | ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); |
| 748 | #endif |
Haijun.Zhang | 8a065e9 | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 749 | |
| 750 | /* T4240 host controller capabilities register should have VS33 bit */ |
| 751 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
| 752 | caps = caps | ESDHC_HOSTCAPBLT_VS33; |
| 753 | #endif |
| 754 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 755 | if (caps & ESDHC_HOSTCAPBLT_VS18) |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 756 | voltage_caps |= MMC_VDD_165_195; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 757 | if (caps & ESDHC_HOSTCAPBLT_VS30) |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 758 | voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 759 | if (caps & ESDHC_HOSTCAPBLT_VS33) |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 760 | voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; |
| 761 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 762 | cfg->name = "FSL_SDHC"; |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 763 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 764 | cfg->ops = &esdhc_ops; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 765 | #endif |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 766 | #ifdef CONFIG_SYS_SD_VOLTAGE |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 767 | cfg->voltages = CONFIG_SYS_SD_VOLTAGE; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 768 | #else |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 769 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 770 | #endif |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 771 | if ((cfg->voltages & voltage_caps) == 0) { |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 772 | printf("voltage not supported by controller\n"); |
| 773 | return -1; |
| 774 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 775 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 776 | if (priv->bus_width == 8) |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 777 | cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 778 | else if (priv->bus_width == 4) |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 779 | cfg->host_caps = MMC_MODE_4BIT; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 780 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 781 | cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
Volodymyr Riazantsev | d251e11 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 782 | #ifdef CONFIG_SYS_FSL_ESDHC_HAS_DDR_MODE |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 783 | cfg->host_caps |= MMC_MODE_DDR_52MHz; |
Volodymyr Riazantsev | d251e11 | 2015-01-20 10:16:44 -0500 | [diff] [blame] | 784 | #endif |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 785 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 786 | if (priv->bus_width > 0) { |
| 787 | if (priv->bus_width < 8) |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 788 | cfg->host_caps &= ~MMC_MODE_8BIT; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 789 | if (priv->bus_width < 4) |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 790 | cfg->host_caps &= ~MMC_MODE_4BIT; |
Abbas Raza | e6bf977 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 793 | if (caps & ESDHC_HOSTCAPBLT_HSS) |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 794 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 795 | |
Haijun.Zhang | f0fe8ad | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 796 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 797 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 798 | cfg->host_caps &= ~MMC_MODE_8BIT; |
Haijun.Zhang | f0fe8ad | 2014-01-10 13:52:18 +0800 | [diff] [blame] | 799 | #endif |
| 800 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 801 | cfg->f_min = 400000; |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 802 | cfg->f_max = min(priv->sdhc_clk, (u32)200000000); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 803 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 804 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 805 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 806 | return 0; |
| 807 | } |
| 808 | |
Simon Glass | b9876e2 | 2017-07-29 11:35:28 -0600 | [diff] [blame] | 809 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Jagan Teki | 3c2cc6d | 2017-05-12 17:18:20 +0530 | [diff] [blame] | 810 | static int fsl_esdhc_cfg_to_priv(struct fsl_esdhc_cfg *cfg, |
| 811 | struct fsl_esdhc_priv *priv) |
| 812 | { |
| 813 | if (!cfg || !priv) |
| 814 | return -EINVAL; |
| 815 | |
| 816 | priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); |
| 817 | priv->bus_width = cfg->max_bus_width; |
| 818 | priv->sdhc_clk = cfg->sdhc_clk; |
| 819 | priv->wp_enable = cfg->wp_enable; |
| 820 | |
| 821 | return 0; |
| 822 | }; |
| 823 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 824 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
| 825 | { |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 826 | struct fsl_esdhc_plat *plat; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 827 | struct fsl_esdhc_priv *priv; |
Simon Glass | 5ee3980 | 2017-07-29 11:35:22 -0600 | [diff] [blame] | 828 | struct mmc *mmc; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 829 | int ret; |
| 830 | |
| 831 | if (!cfg) |
| 832 | return -EINVAL; |
| 833 | |
| 834 | priv = calloc(sizeof(struct fsl_esdhc_priv), 1); |
| 835 | if (!priv) |
| 836 | return -ENOMEM; |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 837 | plat = calloc(sizeof(struct fsl_esdhc_plat), 1); |
| 838 | if (!plat) { |
| 839 | free(priv); |
| 840 | return -ENOMEM; |
| 841 | } |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 842 | |
| 843 | ret = fsl_esdhc_cfg_to_priv(cfg, priv); |
| 844 | if (ret) { |
| 845 | debug("%s xlate failure\n", __func__); |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 846 | free(plat); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 847 | free(priv); |
| 848 | return ret; |
| 849 | } |
| 850 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 851 | ret = fsl_esdhc_init(priv, plat); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 852 | if (ret) { |
| 853 | debug("%s init failure\n", __func__); |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 854 | free(plat); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 855 | free(priv); |
| 856 | return ret; |
| 857 | } |
| 858 | |
Simon Glass | 5ee3980 | 2017-07-29 11:35:22 -0600 | [diff] [blame] | 859 | mmc = mmc_create(&plat->cfg, priv); |
| 860 | if (!mmc) |
| 861 | return -EIO; |
| 862 | |
| 863 | priv->mmc = mmc; |
| 864 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 865 | return 0; |
| 866 | } |
| 867 | |
| 868 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 869 | { |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 870 | struct fsl_esdhc_cfg *cfg; |
| 871 | |
Fabio Estevam | 6592a99 | 2012-12-27 08:51:08 +0000 | [diff] [blame] | 872 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 873 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
Simon Glass | 9e247d1 | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 874 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 875 | return fsl_esdhc_initialize(bis, cfg); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 876 | } |
Jagan Teki | 3c2cc6d | 2017-05-12 17:18:20 +0530 | [diff] [blame] | 877 | #endif |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 878 | |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 879 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 880 | void mmc_adapter_card_type_ident(void) |
| 881 | { |
| 882 | u8 card_id; |
| 883 | u8 value; |
| 884 | |
| 885 | card_id = QIXIS_READ(present) & QIXIS_SDID_MASK; |
| 886 | gd->arch.sdhc_adapter = card_id; |
| 887 | |
| 888 | switch (card_id) { |
| 889 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45: |
Yangbo Lu | 81eacd6 | 2015-09-17 10:27:12 +0800 | [diff] [blame] | 890 | value = QIXIS_READ(brdcfg[5]); |
| 891 | value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7); |
| 892 | QIXIS_WRITE(brdcfg[5], value); |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 893 | break; |
| 894 | case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY: |
Yangbo Lu | c6799ce | 2015-09-17 10:27:48 +0800 | [diff] [blame] | 895 | value = QIXIS_READ(pwr_ctl[1]); |
| 896 | value |= QIXIS_EVDD_BY_SDHC_VS; |
| 897 | QIXIS_WRITE(pwr_ctl[1], value); |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 898 | break; |
| 899 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44: |
| 900 | value = QIXIS_READ(brdcfg[5]); |
| 901 | value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT); |
| 902 | QIXIS_WRITE(brdcfg[5], value); |
| 903 | break; |
| 904 | case QIXIS_ESDHC_ADAPTER_TYPE_RSV: |
| 905 | break; |
| 906 | case QIXIS_ESDHC_ADAPTER_TYPE_MMC: |
| 907 | break; |
| 908 | case QIXIS_ESDHC_ADAPTER_TYPE_SD: |
| 909 | break; |
| 910 | case QIXIS_ESDHC_NO_ADAPTER: |
| 911 | break; |
| 912 | default: |
| 913 | break; |
| 914 | } |
| 915 | } |
| 916 | #endif |
| 917 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 918 | #ifdef CONFIG_OF_LIBFDT |
Yangbo Lu | d84139c | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 919 | __weak int esdhc_status_fixup(void *blob, const char *compat) |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 920 | { |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 921 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 922 | if (!hwconfig("esdhc")) { |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 923 | do_fixup_by_compat(blob, compat, "status", "disabled", |
Yangbo Lu | d84139c | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 924 | sizeof("disabled"), 1); |
| 925 | return 1; |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 926 | } |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 927 | #endif |
Yangbo Lu | d84139c | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 932 | { |
| 933 | const char *compat = "fsl,esdhc"; |
| 934 | |
| 935 | if (esdhc_status_fixup(blob, compat)) |
| 936 | return; |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 937 | |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 938 | #ifdef CONFIG_FSL_ESDHC_USE_PERIPHERAL_CLK |
| 939 | do_fixup_by_compat_u32(blob, compat, "peripheral-frequency", |
| 940 | gd->arch.sdhc_clk, 1); |
| 941 | #else |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 942 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | 9e247d1 | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 943 | gd->arch.sdhc_clk, 1); |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 944 | #endif |
Yangbo Lu | b124f8a | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 945 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 946 | do_fixup_by_compat_u32(blob, compat, "adapter-type", |
| 947 | (u32)(gd->arch.sdhc_adapter), 1); |
| 948 | #endif |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 949 | } |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 950 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 951 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 952 | #if CONFIG_IS_ENABLED(DM_MMC) |
Yinbo Zhu | 4bc8601 | 2019-04-11 11:01:46 +0000 | [diff] [blame] | 953 | #ifndef CONFIG_PPC |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 954 | #include <asm/arch/clock.h> |
Yinbo Zhu | 4bc8601 | 2019-04-11 11:01:46 +0000 | [diff] [blame] | 955 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 956 | static int fsl_esdhc_probe(struct udevice *dev) |
| 957 | { |
| 958 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 959 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 960 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 961 | fdt_addr_t addr; |
| 962 | unsigned int val; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 963 | struct mmc *mmc; |
Yangbo Lu | 4cc119b | 2019-05-23 11:05:46 +0800 | [diff] [blame] | 964 | #if !CONFIG_IS_ENABLED(BLK) |
| 965 | struct blk_desc *bdesc; |
| 966 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 967 | int ret; |
| 968 | |
Simon Glass | 80e9df4 | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 969 | addr = dev_read_addr(dev); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 970 | if (addr == FDT_ADDR_T_NONE) |
| 971 | return -EINVAL; |
Yinbo Zhu | 583d5e9 | 2019-04-11 11:01:50 +0000 | [diff] [blame] | 972 | #ifdef CONFIG_PPC |
| 973 | priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr); |
| 974 | #else |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 975 | priv->esdhc_regs = (struct fsl_esdhc *)addr; |
Yinbo Zhu | 583d5e9 | 2019-04-11 11:01:50 +0000 | [diff] [blame] | 976 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 977 | priv->dev = dev; |
| 978 | |
Simon Glass | 80e9df4 | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 979 | val = dev_read_u32_default(dev, "bus-width", -1); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 980 | if (val == 8) |
| 981 | priv->bus_width = 8; |
| 982 | else if (val == 4) |
| 983 | priv->bus_width = 4; |
| 984 | else |
| 985 | priv->bus_width = 1; |
| 986 | |
Simon Glass | 80e9df4 | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 987 | if (dev_read_bool(dev, "non-removable")) { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 988 | priv->non_removable = 1; |
| 989 | } else { |
| 990 | priv->non_removable = 0; |
Peng Fan | 5eb8b43 | 2017-06-12 17:50:54 +0800 | [diff] [blame] | 991 | } |
Peng Fan | 5eb8b43 | 2017-06-12 17:50:54 +0800 | [diff] [blame] | 992 | |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 993 | priv->wp_enable = 1; |
Peng Fan | af6dbc0 | 2017-02-22 16:21:55 +0800 | [diff] [blame] | 994 | |
Peng Fan | 4c286b7 | 2018-10-18 14:28:35 +0200 | [diff] [blame] | 995 | if (IS_ENABLED(CONFIG_CLK)) { |
| 996 | /* Assigned clock already set clock */ |
| 997 | ret = clk_get_by_name(dev, "per", &priv->per_clk); |
| 998 | if (ret) { |
| 999 | printf("Failed to get per_clk\n"); |
| 1000 | return ret; |
| 1001 | } |
| 1002 | ret = clk_enable(&priv->per_clk); |
| 1003 | if (ret) { |
| 1004 | printf("Failed to enable per_clk\n"); |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | priv->sdhc_clk = clk_get_rate(&priv->per_clk); |
| 1009 | } else { |
Yinbo Zhu | 4bc8601 | 2019-04-11 11:01:46 +0000 | [diff] [blame] | 1010 | #ifndef CONFIG_PPC |
Peng Fan | 4c286b7 | 2018-10-18 14:28:35 +0200 | [diff] [blame] | 1011 | priv->sdhc_clk = mxc_get_clock(MXC_ESDHC_CLK + dev->seq); |
Yinbo Zhu | 4bc8601 | 2019-04-11 11:01:46 +0000 | [diff] [blame] | 1012 | #else |
| 1013 | priv->sdhc_clk = gd->arch.sdhc_clk; |
| 1014 | #endif |
Peng Fan | 4c286b7 | 2018-10-18 14:28:35 +0200 | [diff] [blame] | 1015 | if (priv->sdhc_clk <= 0) { |
| 1016 | dev_err(dev, "Unable to get clk for %s\n", dev->name); |
| 1017 | return -EINVAL; |
| 1018 | } |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1019 | } |
| 1020 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1021 | ret = fsl_esdhc_init(priv, plat); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1022 | if (ret) { |
| 1023 | dev_err(dev, "fsl_esdhc_init failure\n"); |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1027 | mmc = &plat->mmc; |
| 1028 | mmc->cfg = &plat->cfg; |
| 1029 | mmc->dev = dev; |
Yangbo Lu | 4cc119b | 2019-05-23 11:05:46 +0800 | [diff] [blame] | 1030 | #if !CONFIG_IS_ENABLED(BLK) |
| 1031 | mmc->priv = priv; |
| 1032 | |
| 1033 | /* Setup dsr related values */ |
| 1034 | mmc->dsr_imp = 0; |
| 1035 | mmc->dsr = ESDHC_DRIVER_STAGE_VALUE; |
| 1036 | /* Setup the universal parts of the block interface just once */ |
| 1037 | bdesc = mmc_get_blk_desc(mmc); |
| 1038 | bdesc->if_type = IF_TYPE_MMC; |
| 1039 | bdesc->removable = 1; |
| 1040 | bdesc->devnum = mmc_get_next_devnum(); |
| 1041 | bdesc->block_read = mmc_bread; |
| 1042 | bdesc->block_write = mmc_bwrite; |
| 1043 | bdesc->block_erase = mmc_berase; |
| 1044 | |
| 1045 | /* setup initial part type */ |
| 1046 | bdesc->part_type = mmc->cfg->part_type; |
| 1047 | mmc_list_add(mmc); |
| 1048 | #endif |
| 1049 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1050 | upriv->mmc = mmc; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1051 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1052 | return esdhc_init_common(priv, mmc); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1053 | } |
| 1054 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 1055 | #if CONFIG_IS_ENABLED(DM_MMC) |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1056 | static int fsl_esdhc_get_cd(struct udevice *dev) |
| 1057 | { |
| 1058 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1059 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1060 | return esdhc_getcd_common(priv); |
| 1061 | } |
| 1062 | |
| 1063 | static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 1064 | struct mmc_data *data) |
| 1065 | { |
| 1066 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1067 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1068 | |
| 1069 | return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data); |
| 1070 | } |
| 1071 | |
| 1072 | static int fsl_esdhc_set_ios(struct udevice *dev) |
| 1073 | { |
| 1074 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1075 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1076 | |
| 1077 | return esdhc_set_ios_common(priv, &plat->mmc); |
| 1078 | } |
| 1079 | |
| 1080 | static const struct dm_mmc_ops fsl_esdhc_ops = { |
| 1081 | .get_cd = fsl_esdhc_get_cd, |
| 1082 | .send_cmd = fsl_esdhc_send_cmd, |
| 1083 | .set_ios = fsl_esdhc_set_ios, |
| 1084 | }; |
| 1085 | #endif |
| 1086 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1087 | static const struct udevice_id fsl_esdhc_ids[] = { |
Yangbo Lu | 2a99b60 | 2016-12-07 11:54:31 +0800 | [diff] [blame] | 1088 | { .compatible = "fsl,esdhc", }, |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1089 | { /* sentinel */ } |
| 1090 | }; |
| 1091 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1092 | #if CONFIG_IS_ENABLED(BLK) |
| 1093 | static int fsl_esdhc_bind(struct udevice *dev) |
| 1094 | { |
| 1095 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 1096 | |
| 1097 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 1098 | } |
| 1099 | #endif |
| 1100 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1101 | U_BOOT_DRIVER(fsl_esdhc) = { |
| 1102 | .name = "fsl-esdhc-mmc", |
| 1103 | .id = UCLASS_MMC, |
| 1104 | .of_match = fsl_esdhc_ids, |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1105 | .ops = &fsl_esdhc_ops, |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1106 | #if CONFIG_IS_ENABLED(BLK) |
| 1107 | .bind = fsl_esdhc_bind, |
| 1108 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1109 | .probe = fsl_esdhc_probe, |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 1110 | .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat), |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1111 | .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv), |
| 1112 | }; |
| 1113 | #endif |