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