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> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 15 | #include <cpu_func.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> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 25 | #include <dm/device_compat.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 26 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
| 29 | struct fsl_esdhc { |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 30 | uint dsaddr; /* SDMA system address register */ |
| 31 | uint blkattr; /* Block attributes register */ |
| 32 | uint cmdarg; /* Command argument register */ |
| 33 | uint xfertyp; /* Transfer type register */ |
| 34 | uint cmdrsp0; /* Command response 0 register */ |
| 35 | uint cmdrsp1; /* Command response 1 register */ |
| 36 | uint cmdrsp2; /* Command response 2 register */ |
| 37 | uint cmdrsp3; /* Command response 3 register */ |
| 38 | uint datport; /* Buffer data port register */ |
| 39 | uint prsstat; /* Present state register */ |
| 40 | uint proctl; /* Protocol control register */ |
| 41 | uint sysctl; /* System Control Register */ |
| 42 | uint irqstat; /* Interrupt status register */ |
| 43 | uint irqstaten; /* Interrupt status enable register */ |
| 44 | uint irqsigen; /* Interrupt signal enable register */ |
| 45 | uint autoc12err; /* Auto CMD error status register */ |
| 46 | uint hostcapblt; /* Host controller capabilities register */ |
| 47 | uint wml; /* Watermark level register */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 48 | char reserved1[8]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 49 | uint fevt; /* Force event register */ |
| 50 | uint admaes; /* ADMA error status register */ |
| 51 | uint adsaddr; /* ADMA system address register */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 52 | char reserved2[160]; |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 53 | uint hostver; /* Host controller version register */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 54 | char reserved3[4]; /* reserved */ |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 55 | uint dmaerraddr; /* DMA error address register */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 56 | char reserved4[4]; /* reserved */ |
Peng Fan | 59d3782 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 57 | uint dmaerrattr; /* DMA error attribute register */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 58 | char reserved5[4]; /* reserved */ |
Haijun.Zhang | 511948b | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 59 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 60 | char reserved6[756]; /* reserved */ |
| 61 | uint esdhcctl; /* eSDHC control register */ |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 62 | }; |
| 63 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 64 | struct fsl_esdhc_plat { |
| 65 | struct mmc_config cfg; |
| 66 | struct mmc mmc; |
| 67 | }; |
| 68 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 69 | /** |
| 70 | * struct fsl_esdhc_priv |
| 71 | * |
| 72 | * @esdhc_regs: registers of the sdhc controller |
| 73 | * @sdhc_clk: Current clk of the sdhc controller |
| 74 | * @bus_width: bus width, 1bit, 4bit or 8bit |
| 75 | * @cfg: mmc config |
| 76 | * @mmc: mmc |
| 77 | * Following is used when Driver Model is enabled for MMC |
| 78 | * @dev: pointer for the device |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 79 | * @cd_gpio: gpio for card detection |
Peng Fan | 1483151 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 80 | * @wp_gpio: gpio for write protection |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 81 | */ |
| 82 | struct fsl_esdhc_priv { |
| 83 | struct fsl_esdhc *esdhc_regs; |
| 84 | unsigned int sdhc_clk; |
Yangbo Lu | f1bce08 | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 85 | bool is_sdhc_per_clk; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 86 | unsigned int clock; |
Yangbo Lu | 41dec2f | 2019-10-21 18:09:07 +0800 | [diff] [blame] | 87 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 88 | struct mmc *mmc; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 89 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 90 | struct udevice *dev; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 91 | }; |
| 92 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 93 | /* Return the XFERTYP flags for a given command and data packet */ |
Kim Phillips | eafa90a | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 94 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 95 | { |
| 96 | uint xfertyp = 0; |
| 97 | |
| 98 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 99 | xfertyp |= XFERTYP_DPSEL; |
| 100 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 101 | xfertyp |= XFERTYP_DMAEN; |
| 102 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 103 | if (data->blocks > 1) { |
| 104 | xfertyp |= XFERTYP_MSBSEL; |
| 105 | xfertyp |= XFERTYP_BCEN; |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 106 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 107 | xfertyp |= XFERTYP_AC12EN; |
| 108 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | if (data->flags & MMC_DATA_READ) |
| 112 | xfertyp |= XFERTYP_DTDSEL; |
| 113 | } |
| 114 | |
| 115 | if (cmd->resp_type & MMC_RSP_CRC) |
| 116 | xfertyp |= XFERTYP_CCCEN; |
| 117 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 118 | xfertyp |= XFERTYP_CICEN; |
| 119 | if (cmd->resp_type & MMC_RSP_136) |
| 120 | xfertyp |= XFERTYP_RSPTYP_136; |
| 121 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 122 | xfertyp |= XFERTYP_RSPTYP_48_BUSY; |
| 123 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 124 | xfertyp |= XFERTYP_RSPTYP_48; |
| 125 | |
Jason Liu | 4571de3 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 126 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 127 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
Yangbo Lu | 2550344 | 2016-01-21 17:33:19 +0800 | [diff] [blame] | 128 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 129 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 130 | } |
| 131 | |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 132 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 133 | /* |
| 134 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 135 | */ |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 136 | static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv, |
| 137 | struct mmc_data *data) |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 138 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 139 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 140 | uint blocks; |
| 141 | char *buffer; |
| 142 | uint databuf; |
| 143 | uint size; |
| 144 | uint irqstat; |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 145 | ulong start; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 146 | |
| 147 | if (data->flags & MMC_DATA_READ) { |
| 148 | blocks = data->blocks; |
| 149 | buffer = data->dest; |
| 150 | while (blocks) { |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 151 | start = get_timer(0); |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 152 | size = data->blocksize; |
| 153 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 154 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) { |
| 155 | if (get_timer(start) > PIO_TIMEOUT) { |
| 156 | printf("\nData Read Failed in PIO Mode."); |
| 157 | return; |
| 158 | } |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 159 | } |
| 160 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 161 | udelay(100); /* Wait before last byte transfer complete */ |
| 162 | irqstat = esdhc_read32(®s->irqstat); |
| 163 | databuf = in_le32(®s->datport); |
| 164 | *((uint *)buffer) = databuf; |
| 165 | buffer += 4; |
| 166 | size -= 4; |
| 167 | } |
| 168 | blocks--; |
| 169 | } |
| 170 | } else { |
| 171 | blocks = data->blocks; |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 172 | buffer = (char *)data->src; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 173 | while (blocks) { |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 174 | start = get_timer(0); |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 175 | size = data->blocksize; |
| 176 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | bcfb365 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 177 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) { |
| 178 | if (get_timer(start) > PIO_TIMEOUT) { |
| 179 | printf("\nData Write Failed in PIO Mode."); |
| 180 | return; |
| 181 | } |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 182 | } |
| 183 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 184 | udelay(100); /* Wait before last byte transfer complete */ |
| 185 | databuf = *((uint *)buffer); |
| 186 | buffer += 4; |
| 187 | size -= 4; |
| 188 | irqstat = esdhc_read32(®s->irqstat); |
| 189 | out_le32(®s->datport, databuf); |
| 190 | } |
| 191 | blocks--; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | #endif |
| 196 | |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 197 | static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 198 | struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 199 | { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 200 | int timeout; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 201 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 202 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 203 | dma_addr_t addr; |
| 204 | #endif |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 205 | uint wml_value; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 206 | |
| 207 | wml_value = data->blocksize/4; |
| 208 | |
| 209 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 210 | if (wml_value > WML_RD_WML_MAX) |
| 211 | wml_value = WML_RD_WML_MAX_VAL; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 212 | |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 213 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 214 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 215 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 216 | addr = virt_to_phys((void *)(data->dest)); |
| 217 | if (upper_32_bits(addr)) |
| 218 | printf("Error found for upper 32 bits\n"); |
| 219 | else |
| 220 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 221 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 222 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 223 | #endif |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 224 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 225 | } else { |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 226 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 227 | flush_dcache_range((ulong)data->src, |
| 228 | (ulong)data->src+data->blocks |
| 229 | *data->blocksize); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 230 | #endif |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 231 | if (wml_value > WML_WR_WML_MAX) |
| 232 | wml_value = WML_WR_WML_MAX_VAL; |
Yangbo Lu | 0cc127c | 2019-10-31 18:54:25 +0800 | [diff] [blame] | 233 | |
| 234 | if (!(esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL)) { |
| 235 | printf("Can not write to locked SD card.\n"); |
| 236 | return -EINVAL; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 237 | } |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 238 | |
| 239 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 240 | wml_value << 16); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 241 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 242 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 243 | addr = virt_to_phys((void *)(data->src)); |
| 244 | if (upper_32_bits(addr)) |
| 245 | printf("Error found for upper 32 bits\n"); |
| 246 | else |
| 247 | esdhc_write32(®s->dsaddr, lower_32_bits(addr)); |
| 248 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 249 | esdhc_write32(®s->dsaddr, (u32)data->src); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 250 | #endif |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 251 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 252 | } |
| 253 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 254 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 255 | |
| 256 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 257 | /* |
| 258 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 259 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 260 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 261 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 262 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 263 | * As 1) >= 2) |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 264 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 265 | * Taking log2 both the sides |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 266 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 267 | * Rounding up to next power of 2 |
Andrew Gabbasov | fb82398 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 268 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 269 | * => timeout + 13 = fls(mmc->clock/4) |
Yangbo Lu | e978a31 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 270 | * |
| 271 | * However, the MMC spec "It is strongly recommended for hosts to |
| 272 | * implement more than 500ms timeout value even if the card |
| 273 | * indicates the 250ms maximum busy length." Even the previous |
| 274 | * value of 300ms is known to be insufficient for some cards. |
| 275 | * So, we use |
| 276 | * => timeout + 13 = fls(mmc->clock/2) |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 277 | */ |
Yangbo Lu | e978a31 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 278 | timeout = fls(mmc->clock/2); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 279 | timeout -= 13; |
| 280 | |
| 281 | if (timeout > 14) |
| 282 | timeout = 14; |
| 283 | |
| 284 | if (timeout < 0) |
| 285 | timeout = 0; |
| 286 | |
Kumar Gala | 5103a03 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 287 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 288 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 289 | timeout++; |
| 290 | #endif |
| 291 | |
Haijun.Zhang | 1336e2d | 2014-03-18 17:04:23 +0800 | [diff] [blame] | 292 | #ifdef ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE |
| 293 | timeout = 0xE; |
| 294 | #endif |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 295 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 300 | static void check_and_invalidate_dcache_range |
| 301 | (struct mmc_cmd *cmd, |
| 302 | struct mmc_data *data) { |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 303 | unsigned start = 0; |
Yangbo Lu | cc634e2 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 304 | unsigned end = 0; |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 305 | unsigned size = roundup(ARCH_DMA_MINALIGN, |
| 306 | data->blocks*data->blocksize); |
Yangbo Lu | 4d8ff42 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 307 | #if defined(CONFIG_FSL_LAYERSCAPE) |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 308 | dma_addr_t addr; |
| 309 | |
| 310 | addr = virt_to_phys((void *)(data->dest)); |
| 311 | if (upper_32_bits(addr)) |
| 312 | printf("Error found for upper 32 bits\n"); |
| 313 | else |
| 314 | start = lower_32_bits(addr); |
Yangbo Lu | cc634e2 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 315 | #else |
| 316 | start = (unsigned)data->dest; |
Yangbo Lu | 8b06460 | 2015-03-20 19:28:31 -0700 | [diff] [blame] | 317 | #endif |
Yangbo Lu | cc634e2 | 2016-05-12 19:12:58 +0800 | [diff] [blame] | 318 | end = start + size; |
Eric Nelson | e576bd9 | 2012-04-25 14:28:48 +0000 | [diff] [blame] | 319 | invalidate_dcache_range(start, end); |
| 320 | } |
Tom Rini | 10dc777 | 2014-05-23 09:19:05 -0400 | [diff] [blame] | 321 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 322 | /* |
| 323 | * Sends a command out on the bus. Takes the mmc pointer, |
| 324 | * a command pointer, and an optional data pointer. |
| 325 | */ |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 326 | static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 327 | struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 328 | { |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 329 | int err = 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 330 | uint xfertyp; |
| 331 | uint irqstat; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 332 | u32 flags = IRQSTAT_CC | IRQSTAT_CTOE; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 333 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Fabio Estevam | 29c2edb | 2018-11-19 10:31:53 -0200 | [diff] [blame] | 334 | unsigned long start; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 335 | |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 336 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 337 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 338 | return 0; |
| 339 | #endif |
| 340 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 341 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 342 | |
| 343 | sync(); |
| 344 | |
| 345 | /* Wait for the bus to be idle */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 346 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 347 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 348 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 349 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 350 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 351 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 352 | |
| 353 | /* Wait at least 8 SD clock cycles before the next command */ |
| 354 | /* |
| 355 | * Note: This is way more than 8 cycles, but 1ms seems to |
| 356 | * resolve timing issues with some cards |
| 357 | */ |
| 358 | udelay(1000); |
| 359 | |
| 360 | /* Set up for a data transfer if we have one */ |
| 361 | if (data) { |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 362 | err = esdhc_setup_data(priv, mmc, data); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 363 | if(err) |
| 364 | return err; |
Peng Fan | 4683b22 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 365 | |
| 366 | if (data->flags & MMC_DATA_READ) |
| 367 | check_and_invalidate_dcache_range(cmd, data); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* Figure out the transfer arguments */ |
| 371 | xfertyp = esdhc_xfertyp(cmd, data); |
| 372 | |
Andrew Gabbasov | 01b7735 | 2013-06-11 10:34:22 -0500 | [diff] [blame] | 373 | /* Mask all irqs */ |
| 374 | esdhc_write32(®s->irqsigen, 0); |
| 375 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 376 | /* Send the command */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 377 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
| 378 | esdhc_write32(®s->xfertyp, xfertyp); |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 379 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 380 | /* Wait for the command to complete */ |
Fabio Estevam | 29c2edb | 2018-11-19 10:31:53 -0200 | [diff] [blame] | 381 | start = get_timer(0); |
| 382 | while (!(esdhc_read32(®s->irqstat) & flags)) { |
| 383 | if (get_timer(start) > 1000) { |
| 384 | err = -ETIMEDOUT; |
| 385 | goto out; |
| 386 | } |
| 387 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 388 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 389 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 390 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 391 | if (irqstat & CMD_ERR) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 392 | err = -ECOMM; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 393 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 396 | if (irqstat & IRQSTAT_CTOE) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 397 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 398 | goto out; |
| 399 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 400 | |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 401 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 402 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
Yangbo Lu | 253d5bd | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 403 | int timeout = 6000; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 404 | |
Yangbo Lu | 253d5bd | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 405 | /* Poll on DATA0 line for cmd with busy signal for 600 ms */ |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 406 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 407 | PRSSTAT_DAT0)) { |
| 408 | udelay(100); |
| 409 | timeout--; |
| 410 | } |
| 411 | |
| 412 | if (timeout <= 0) { |
| 413 | printf("Timeout waiting for DAT0 to go high!\n"); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 414 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 415 | goto out; |
Dirk Behme | 7a5b802 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 419 | /* Copy the response to the response buffer */ |
| 420 | if (cmd->resp_type & MMC_RSP_136) { |
| 421 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 422 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 423 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 424 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 425 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 426 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 427 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 428 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 429 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 430 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 431 | } else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 432 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 433 | |
| 434 | /* Wait until all of the blocks are transferred */ |
| 435 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 436 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 437 | esdhc_pio_read_write(priv, data); |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 438 | #else |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 439 | do { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 440 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 441 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 442 | if (irqstat & IRQSTAT_DTOE) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 443 | err = -ETIMEDOUT; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 444 | goto out; |
| 445 | } |
Frans Meulenbroeks | 63fb5a7 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 446 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 447 | if (irqstat & DATA_ERR) { |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 448 | err = -ECOMM; |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 449 | goto out; |
| 450 | } |
Yinbo Zhu | 6f883e5 | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 451 | } while ((irqstat & DATA_COMPLETE) != DATA_COMPLETE); |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 452 | |
Peng Fan | 4683b22 | 2015-06-25 10:32:26 +0800 | [diff] [blame] | 453 | /* |
| 454 | * Need invalidate the dcache here again to avoid any |
| 455 | * cache-fill during the DMA operations such as the |
| 456 | * speculative pre-fetching etc. |
| 457 | */ |
Angelo Dureghello | 1f15cb8 | 2019-01-19 10:40:38 +0100 | [diff] [blame] | 458 | if (data->flags & MMC_DATA_READ) { |
Eric Nelson | 54899fc | 2013-04-03 12:31:56 +0000 | [diff] [blame] | 459 | check_and_invalidate_dcache_range(cmd, data); |
Angelo Dureghello | 1f15cb8 | 2019-01-19 10:40:38 +0100 | [diff] [blame] | 460 | } |
Ye.Li | 7168977 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 461 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 462 | } |
| 463 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 464 | out: |
| 465 | /* Reset CMD and DATA portions on error */ |
| 466 | if (err) { |
| 467 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 468 | SYSCTL_RSTC); |
| 469 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 470 | ; |
| 471 | |
| 472 | if (data) { |
| 473 | esdhc_write32(®s->sysctl, |
| 474 | esdhc_read32(®s->sysctl) | |
| 475 | SYSCTL_RSTD); |
| 476 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 477 | ; |
| 478 | } |
| 479 | } |
| 480 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 481 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 482 | |
Andrew Gabbasov | 8a57302 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 483 | return err; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 486 | 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] | 487 | { |
Benoît Thébaudeau | b9b4f14 | 2018-01-16 22:44:18 +0100 | [diff] [blame] | 488 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 489 | int div = 1; |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 490 | int pre_div = 2; |
Yinbo Zhu | 6f883e5 | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 491 | unsigned int sdhc_clk = priv->sdhc_clk; |
| 492 | u32 time_out; |
| 493 | u32 value; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 494 | uint clk; |
| 495 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 496 | if (clock < mmc->cfg->f_min) |
| 497 | clock = mmc->cfg->f_min; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 498 | |
Yangbo Lu | 5d336d1 | 2019-10-21 18:09:09 +0800 | [diff] [blame] | 499 | while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256) |
Lukasz Majewski | b6a0427 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 500 | pre_div *= 2; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 501 | |
Yangbo Lu | 5d336d1 | 2019-10-21 18:09:09 +0800 | [diff] [blame] | 502 | while (sdhc_clk / (div * pre_div) > clock && div < 16) |
Lukasz Majewski | b6a0427 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 503 | div++; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 504 | |
Benoît Thébaudeau | 4f42528 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 505 | pre_div >>= 1; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 506 | div -= 1; |
| 507 | |
| 508 | clk = (pre_div << 8) | (div << 4); |
| 509 | |
Kumar Gala | cc4d122 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 510 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 511 | |
| 512 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 513 | |
Yinbo Zhu | 6f883e5 | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 514 | time_out = 20; |
| 515 | value = PRSSTAT_SDSTB; |
| 516 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 517 | if (time_out == 0) { |
| 518 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 519 | break; |
| 520 | } |
| 521 | time_out--; |
| 522 | mdelay(1); |
| 523 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 524 | |
Eric Nelson | f0b5f23 | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 525 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 526 | } |
| 527 | |
Simon Glass | 09b465f | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 528 | static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable) |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 529 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 530 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 531 | u32 value; |
| 532 | u32 time_out; |
| 533 | |
| 534 | value = esdhc_read32(®s->sysctl); |
| 535 | |
| 536 | if (enable) |
| 537 | value |= SYSCTL_CKEN; |
| 538 | else |
| 539 | value &= ~SYSCTL_CKEN; |
| 540 | |
| 541 | esdhc_write32(®s->sysctl, value); |
| 542 | |
| 543 | time_out = 20; |
| 544 | value = PRSSTAT_SDSTB; |
| 545 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 546 | if (time_out == 0) { |
| 547 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 548 | break; |
| 549 | } |
| 550 | time_out--; |
| 551 | mdelay(1); |
| 552 | } |
| 553 | } |
Yangbo Lu | 2d9ca2c | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 554 | |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 555 | 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] | 556 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 557 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 558 | |
Yangbo Lu | f1bce08 | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 559 | if (priv->is_sdhc_per_clk) { |
| 560 | /* Select to use peripheral clock */ |
| 561 | esdhc_clock_control(priv, false); |
| 562 | esdhc_setbits32(®s->esdhcctl, ESDHCCTL_PCS); |
| 563 | esdhc_clock_control(priv, true); |
| 564 | } |
| 565 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 566 | /* Set the clock speed */ |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 567 | if (priv->clock != mmc->clock) |
| 568 | set_sysctl(priv, mmc, mmc->clock); |
| 569 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 570 | /* Set the bus width */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 571 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 572 | |
| 573 | if (mmc->bus_width == 4) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 574 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 575 | else if (mmc->bus_width == 8) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 576 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 577 | |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 578 | return 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 579 | } |
| 580 | |
Rasmus Villemoes | ede2822 | 2020-01-30 12:06:45 +0000 | [diff] [blame] | 581 | static void esdhc_enable_cache_snooping(struct fsl_esdhc *regs) |
| 582 | { |
| 583 | #ifdef CONFIG_ARCH_MPC830X |
| 584 | immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
| 585 | sysconf83xx_t *sysconf = &immr->sysconf; |
| 586 | |
| 587 | setbits_be32(&sysconf->sdhccr, 0x02000000); |
| 588 | #else |
| 589 | esdhc_write32(®s->esdhcctl, 0x00000040); |
| 590 | #endif |
| 591 | } |
| 592 | |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 593 | 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] | 594 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 595 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Simon Glass | 201e828 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 596 | ulong start; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 597 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 598 | /* Reset the entire host controller */ |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 599 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 600 | |
| 601 | /* Wait until the controller is available */ |
Simon Glass | 201e828 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 602 | start = get_timer(0); |
| 603 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 604 | if (get_timer(start) > 1000) |
| 605 | return -ETIMEDOUT; |
| 606 | } |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 607 | |
Rasmus Villemoes | ede2822 | 2020-01-30 12:06:45 +0000 | [diff] [blame] | 608 | esdhc_enable_cache_snooping(regs); |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 609 | |
Dirk Behme | a61da72 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 610 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 611 | |
| 612 | /* Set the initial clock speed */ |
Jaehoon Chung | 6511718 | 2018-01-26 19:25:29 +0900 | [diff] [blame] | 613 | mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 614 | |
| 615 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 616 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 617 | |
| 618 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 619 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 620 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 621 | /* Set timout to the maximum value */ |
| 622 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 623 | |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 624 | return 0; |
| 625 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 626 | |
Simon Glass | 9586aa6 | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 627 | static int esdhc_getcd_common(struct fsl_esdhc_priv *priv) |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 628 | { |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 629 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 630 | int timeout = 1000; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 631 | |
Haijun.Zhang | f7e27cc | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 632 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
| 633 | if (CONFIG_ESDHC_DETECT_QUIRK) |
| 634 | return 1; |
| 635 | #endif |
Thierry Reding | d48d2e2 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 636 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && --timeout) |
| 637 | udelay(1000); |
| 638 | |
| 639 | return timeout > 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 640 | } |
| 641 | |
Yangbo Lu | 5705973 | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 642 | static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv, |
| 643 | struct mmc_config *cfg) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 644 | { |
Yangbo Lu | 5705973 | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 645 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 5b05fc0 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 646 | u32 caps; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 647 | |
Wang Huan | 19060bd | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 648 | caps = esdhc_read32(®s->hostcapblt); |
Roy Zang | 3b4456e | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 649 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
Yangbo Lu | 5b05fc0 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 650 | caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30); |
Roy Zang | 3b4456e | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 651 | #endif |
Haijun.Zhang | ef38f3f | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 652 | #ifdef CONFIG_SYS_FSL_MMC_HAS_CAPBLT_VS33 |
Yangbo Lu | 5b05fc0 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 653 | caps |= HOSTCAPBLT_VS33; |
Haijun.Zhang | ef38f3f | 2013-10-31 09:38:19 +0800 | [diff] [blame] | 654 | #endif |
Yangbo Lu | 5b05fc0 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 655 | if (caps & HOSTCAPBLT_VS18) |
| 656 | cfg->voltages |= MMC_VDD_165_195; |
| 657 | if (caps & HOSTCAPBLT_VS30) |
| 658 | cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31; |
| 659 | if (caps & HOSTCAPBLT_VS33) |
| 660 | cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 661 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 662 | cfg->name = "FSL_SDHC"; |
Abbas Raza | aad4659 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 663 | |
Yangbo Lu | 5b05fc0 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 664 | if (caps & HOSTCAPBLT_HSS) |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 665 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 666 | |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 667 | cfg->f_min = 400000; |
Peng Fan | 51313b4 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 668 | cfg->f_max = min(priv->sdhc_clk, (u32)200000000); |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 669 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 670 | } |
| 671 | |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 672 | #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT |
| 673 | void mmc_adapter_card_type_ident(void) |
| 674 | { |
| 675 | u8 card_id; |
| 676 | u8 value; |
| 677 | |
| 678 | card_id = QIXIS_READ(present) & QIXIS_SDID_MASK; |
| 679 | gd->arch.sdhc_adapter = card_id; |
| 680 | |
| 681 | switch (card_id) { |
| 682 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC45: |
Yangbo Lu | cdc6955 | 2015-09-17 10:27:12 +0800 | [diff] [blame] | 683 | value = QIXIS_READ(brdcfg[5]); |
| 684 | value |= (QIXIS_DAT4 | QIXIS_DAT5_6_7); |
| 685 | QIXIS_WRITE(brdcfg[5], value); |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 686 | break; |
| 687 | case QIXIS_ESDHC_ADAPTER_TYPE_SDMMC_LEGACY: |
Yangbo Lu | bf50be8 | 2015-09-17 10:27:48 +0800 | [diff] [blame] | 688 | value = QIXIS_READ(pwr_ctl[1]); |
| 689 | value |= QIXIS_EVDD_BY_SDHC_VS; |
| 690 | QIXIS_WRITE(pwr_ctl[1], value); |
Yangbo Lu | 5a8dbdc | 2015-04-22 13:57:00 +0800 | [diff] [blame] | 691 | break; |
| 692 | case QIXIS_ESDHC_ADAPTER_TYPE_EMMC44: |
| 693 | value = QIXIS_READ(brdcfg[5]); |
| 694 | value |= (QIXIS_SDCLKIN | QIXIS_SDCLKOUT); |
| 695 | QIXIS_WRITE(brdcfg[5], value); |
| 696 | break; |
| 697 | case QIXIS_ESDHC_ADAPTER_TYPE_RSV: |
| 698 | break; |
| 699 | case QIXIS_ESDHC_ADAPTER_TYPE_MMC: |
| 700 | break; |
| 701 | case QIXIS_ESDHC_ADAPTER_TYPE_SD: |
| 702 | break; |
| 703 | case QIXIS_ESDHC_NO_ADAPTER: |
| 704 | break; |
| 705 | default: |
| 706 | break; |
| 707 | } |
| 708 | } |
| 709 | #endif |
| 710 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 711 | #ifdef CONFIG_OF_LIBFDT |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 712 | __weak int esdhc_status_fixup(void *blob, const char *compat) |
| 713 | { |
| 714 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
| 715 | if (!hwconfig("esdhc")) { |
| 716 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 717 | sizeof("disabled"), 1); |
| 718 | return 1; |
| 719 | } |
| 720 | #endif |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 721 | return 0; |
| 722 | } |
| 723 | |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 724 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 725 | { |
| 726 | const char *compat = "fsl,esdhc"; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 727 | |
Yangbo Lu | fce1e16 | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 728 | if (esdhc_status_fixup(blob, compat)) |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 729 | return; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 730 | |
| 731 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | e9adeca | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 732 | gd->arch.sdhc_clk, 1); |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 733 | } |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 734 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 735 | |
Yangbo Lu | 6187047 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 736 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 737 | static int esdhc_getcd(struct mmc *mmc) |
| 738 | { |
| 739 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 740 | |
| 741 | return esdhc_getcd_common(priv); |
| 742 | } |
| 743 | |
| 744 | static int esdhc_init(struct mmc *mmc) |
| 745 | { |
| 746 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 747 | |
| 748 | return esdhc_init_common(priv, mmc); |
| 749 | } |
| 750 | |
| 751 | static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 752 | struct mmc_data *data) |
| 753 | { |
| 754 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 755 | |
| 756 | return esdhc_send_cmd_common(priv, mmc, cmd, data); |
| 757 | } |
| 758 | |
| 759 | static int esdhc_set_ios(struct mmc *mmc) |
| 760 | { |
| 761 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 762 | |
| 763 | return esdhc_set_ios_common(priv, mmc); |
| 764 | } |
| 765 | |
| 766 | static const struct mmc_ops esdhc_ops = { |
| 767 | .getcd = esdhc_getcd, |
| 768 | .init = esdhc_init, |
| 769 | .send_cmd = esdhc_send_cmd, |
| 770 | .set_ios = esdhc_set_ios, |
| 771 | }; |
| 772 | |
| 773 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
| 774 | { |
| 775 | struct fsl_esdhc_plat *plat; |
| 776 | struct fsl_esdhc_priv *priv; |
| 777 | struct mmc_config *mmc_cfg; |
| 778 | struct mmc *mmc; |
| 779 | |
| 780 | if (!cfg) |
| 781 | return -EINVAL; |
| 782 | |
| 783 | priv = calloc(sizeof(struct fsl_esdhc_priv), 1); |
| 784 | if (!priv) |
| 785 | return -ENOMEM; |
| 786 | plat = calloc(sizeof(struct fsl_esdhc_plat), 1); |
| 787 | if (!plat) { |
| 788 | free(priv); |
| 789 | return -ENOMEM; |
| 790 | } |
| 791 | |
| 792 | priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); |
| 793 | priv->sdhc_clk = cfg->sdhc_clk; |
Yangbo Lu | f1bce08 | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 794 | if (gd->arch.sdhc_per_clk) |
| 795 | priv->is_sdhc_per_clk = true; |
Yangbo Lu | 6187047 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 796 | |
| 797 | mmc_cfg = &plat->cfg; |
| 798 | |
| 799 | if (cfg->max_bus_width == 8) { |
| 800 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT | |
| 801 | MMC_MODE_8BIT; |
| 802 | } else if (cfg->max_bus_width == 4) { |
| 803 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT; |
| 804 | } else if (cfg->max_bus_width == 1) { |
| 805 | mmc_cfg->host_caps |= MMC_MODE_1BIT; |
| 806 | } else { |
| 807 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT | |
| 808 | MMC_MODE_8BIT; |
| 809 | printf("No max bus width provided. Assume 8-bit supported.\n"); |
| 810 | } |
| 811 | |
| 812 | #ifdef CONFIG_ESDHC_DETECT_8_BIT_QUIRK |
| 813 | if (CONFIG_ESDHC_DETECT_8_BIT_QUIRK) |
| 814 | mmc_cfg->host_caps &= ~MMC_MODE_8BIT; |
| 815 | #endif |
| 816 | mmc_cfg->ops = &esdhc_ops; |
| 817 | |
| 818 | fsl_esdhc_get_cfg_common(priv, mmc_cfg); |
| 819 | |
| 820 | mmc = mmc_create(mmc_cfg, priv); |
| 821 | if (!mmc) |
| 822 | return -EIO; |
| 823 | |
| 824 | priv->mmc = mmc; |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 829 | { |
| 830 | struct fsl_esdhc_cfg *cfg; |
| 831 | |
| 832 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
| 833 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
Yangbo Lu | f1bce08 | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 834 | /* Prefer peripheral clock which provides higher frequency. */ |
| 835 | if (gd->arch.sdhc_per_clk) |
| 836 | cfg->sdhc_clk = gd->arch.sdhc_per_clk; |
| 837 | else |
| 838 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Yangbo Lu | 6187047 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 839 | return fsl_esdhc_initialize(bis, cfg); |
| 840 | } |
| 841 | #else /* DM_MMC */ |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 842 | static int fsl_esdhc_probe(struct udevice *dev) |
| 843 | { |
| 844 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 845 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 846 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 847 | fdt_addr_t addr; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 848 | struct mmc *mmc; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 849 | |
Simon Glass | 4aac33f | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 850 | addr = dev_read_addr(dev); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 851 | if (addr == FDT_ADDR_T_NONE) |
| 852 | return -EINVAL; |
Yinbo Zhu | b69e1d0 | 2019-04-11 11:01:50 +0000 | [diff] [blame] | 853 | #ifdef CONFIG_PPC |
| 854 | priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr); |
| 855 | #else |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 856 | priv->esdhc_regs = (struct fsl_esdhc *)addr; |
Yinbo Zhu | b69e1d0 | 2019-04-11 11:01:50 +0000 | [diff] [blame] | 857 | #endif |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 858 | priv->dev = dev; |
| 859 | |
Yangbo Lu | f1bce08 | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 860 | if (gd->arch.sdhc_per_clk) { |
| 861 | priv->sdhc_clk = gd->arch.sdhc_per_clk; |
| 862 | priv->is_sdhc_per_clk = true; |
| 863 | } else { |
| 864 | priv->sdhc_clk = gd->arch.sdhc_clk; |
| 865 | } |
| 866 | |
Yangbo Lu | 5e81cbf | 2019-11-12 19:28:36 +0800 | [diff] [blame] | 867 | if (priv->sdhc_clk <= 0) { |
| 868 | dev_err(dev, "Unable to get clk for %s\n", dev->name); |
| 869 | return -EINVAL; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 870 | } |
| 871 | |
Yangbo Lu | 5705973 | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 872 | fsl_esdhc_get_cfg_common(priv, &plat->cfg); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 873 | |
Yinbo Zhu | 6f883e5 | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 874 | mmc_of_parse(dev, &plat->cfg); |
| 875 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 876 | mmc = &plat->mmc; |
| 877 | mmc->cfg = &plat->cfg; |
| 878 | mmc->dev = dev; |
Yangbo Lu | 66fa035 | 2019-05-23 11:05:46 +0800 | [diff] [blame] | 879 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 880 | upriv->mmc = mmc; |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 881 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 882 | return esdhc_init_common(priv, mmc); |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 883 | } |
| 884 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 885 | static int fsl_esdhc_get_cd(struct udevice *dev) |
| 886 | { |
Yangbo Lu | 08197cb | 2019-10-31 18:54:24 +0800 | [diff] [blame] | 887 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 888 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 889 | |
Yangbo Lu | 08197cb | 2019-10-31 18:54:24 +0800 | [diff] [blame] | 890 | if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE) |
| 891 | return 1; |
| 892 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 893 | return esdhc_getcd_common(priv); |
| 894 | } |
| 895 | |
| 896 | static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 897 | struct mmc_data *data) |
| 898 | { |
| 899 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 900 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 901 | |
| 902 | return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data); |
| 903 | } |
| 904 | |
| 905 | static int fsl_esdhc_set_ios(struct udevice *dev) |
| 906 | { |
| 907 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 908 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 909 | |
| 910 | return esdhc_set_ios_common(priv, &plat->mmc); |
| 911 | } |
| 912 | |
| 913 | static const struct dm_mmc_ops fsl_esdhc_ops = { |
| 914 | .get_cd = fsl_esdhc_get_cd, |
| 915 | .send_cmd = fsl_esdhc_send_cmd, |
| 916 | .set_ios = fsl_esdhc_set_ios, |
Yinbo Zhu | 6f883e5 | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 917 | #ifdef MMC_SUPPORTS_TUNING |
| 918 | .execute_tuning = fsl_esdhc_execute_tuning, |
| 919 | #endif |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 920 | }; |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 921 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 922 | static const struct udevice_id fsl_esdhc_ids[] = { |
Yangbo Lu | a6473f8 | 2016-12-07 11:54:31 +0800 | [diff] [blame] | 923 | { .compatible = "fsl,esdhc", }, |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 924 | { /* sentinel */ } |
| 925 | }; |
| 926 | |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 927 | static int fsl_esdhc_bind(struct udevice *dev) |
| 928 | { |
| 929 | struct fsl_esdhc_plat *plat = dev_get_platdata(dev); |
| 930 | |
| 931 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 932 | } |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 933 | |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 934 | U_BOOT_DRIVER(fsl_esdhc) = { |
| 935 | .name = "fsl-esdhc-mmc", |
| 936 | .id = UCLASS_MMC, |
| 937 | .of_match = fsl_esdhc_ids, |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 938 | .ops = &fsl_esdhc_ops, |
Simon Glass | 653282b | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 939 | .bind = fsl_esdhc_bind, |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 940 | .probe = fsl_esdhc_probe, |
Simon Glass | e88e1d9 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 941 | .platdata_auto_alloc_size = sizeof(struct fsl_esdhc_plat), |
Peng Fan | 96f0407 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 942 | .priv_auto_alloc_size = sizeof(struct fsl_esdhc_priv), |
| 943 | }; |
| 944 | #endif |