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