Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 2 | /* |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 3 | * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc |
Yangbo Lu | e087cd6 | 2021-06-03 10:51:17 +0800 | [diff] [blame] | 4 | * Copyright 2019-2021 NXP |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 5 | * Andy Fleming |
| 6 | * |
| 7 | * Based vaguely on the pxa mmc code: |
| 8 | * (C) Copyright 2003 |
| 9 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <config.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 13 | #include <command.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 14 | #include <cpu_func.h> |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 15 | #include <errno.h> |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 16 | #include <hwconfig.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 17 | #include <mmc.h> |
| 18 | #include <part.h> |
| 19 | #include <malloc.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 20 | #include <fsl_esdhc.h> |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 21 | #include <fdt_support.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 22 | #include <asm/cache.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 23 | #include <asm/global_data.h> |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 24 | #include <asm/io.h> |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 25 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 26 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 27 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 28 | #include <linux/delay.h> |
Stephen Carlson | 1822a97 | 2021-08-17 12:46:40 -0700 | [diff] [blame] | 29 | #include <linux/iopoll.h> |
Michael Walle | c9bba2e | 2020-09-23 12:42:48 +0200 | [diff] [blame] | 30 | #include <linux/dma-mapping.h> |
Michael Walle | 081d401 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 31 | #include <sdhci.h> |
Tom Rini | bdd47f3 | 2022-06-16 14:04:38 -0400 | [diff] [blame] | 32 | #include "../../board/freescale/common/qixis.h" |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 33 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
| 36 | struct fsl_esdhc { |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 37 | uint dsaddr; /* SDMA system address register */ |
| 38 | uint blkattr; /* Block attributes register */ |
| 39 | uint cmdarg; /* Command argument register */ |
| 40 | uint xfertyp; /* Transfer type register */ |
| 41 | uint cmdrsp0; /* Command response 0 register */ |
| 42 | uint cmdrsp1; /* Command response 1 register */ |
| 43 | uint cmdrsp2; /* Command response 2 register */ |
| 44 | uint cmdrsp3; /* Command response 3 register */ |
| 45 | uint datport; /* Buffer data port register */ |
| 46 | uint prsstat; /* Present state register */ |
| 47 | uint proctl; /* Protocol control register */ |
| 48 | uint sysctl; /* System Control Register */ |
| 49 | uint irqstat; /* Interrupt status register */ |
| 50 | uint irqstaten; /* Interrupt status enable register */ |
| 51 | uint irqsigen; /* Interrupt signal enable register */ |
| 52 | uint autoc12err; /* Auto CMD error status register */ |
| 53 | uint hostcapblt; /* Host controller capabilities register */ |
| 54 | uint wml; /* Watermark level register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 55 | char reserved1[8]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 56 | uint fevt; /* Force event register */ |
| 57 | uint admaes; /* ADMA error status register */ |
Michael Walle | 081d401 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 58 | uint adsaddrl; /* ADMA system address low register */ |
| 59 | uint adsaddrh; /* ADMA system address high register */ |
| 60 | char reserved2[156]; |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 61 | uint hostver; /* Host controller version register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 62 | char reserved3[4]; /* reserved */ |
Peng Fan | b9b4236 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 63 | uint dmaerraddr; /* DMA error address register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 64 | char reserved4[4]; /* reserved */ |
Peng Fan | b9b4236 | 2018-01-21 19:00:22 +0800 | [diff] [blame] | 65 | uint dmaerrattr; /* DMA error attribute register */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 66 | char reserved5[4]; /* reserved */ |
Haijun.Zhang | d49eb9e | 2013-10-30 11:37:55 +0800 | [diff] [blame] | 67 | uint hostcapblt2; /* Host controller capabilities register 2 */ |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 68 | char reserved6[8]; /* reserved */ |
| 69 | uint tbctl; /* Tuning block control register */ |
Yangbo Lu | 8f9ace1 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 70 | char reserved7[32]; /* reserved */ |
| 71 | uint sdclkctl; /* SD clock control register */ |
| 72 | uint sdtimingctl; /* SD timing control register */ |
| 73 | char reserved8[20]; /* reserved */ |
| 74 | uint dllcfg0; /* DLL config 0 register */ |
Michael Walle | 7259dc5 | 2021-03-17 15:01:37 +0100 | [diff] [blame] | 75 | uint dllcfg1; /* DLL config 1 register */ |
| 76 | char reserved9[8]; /* reserved */ |
Yangbo Lu | 8fbe95b | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 77 | uint dllstat0; /* DLL status 0 register */ |
| 78 | char reserved10[664];/* reserved */ |
Yangbo Lu | 62b56b3 | 2019-06-21 11:42:29 +0800 | [diff] [blame] | 79 | uint esdhcctl; /* eSDHC control register */ |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 80 | }; |
| 81 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 82 | struct fsl_esdhc_plat { |
| 83 | struct mmc_config cfg; |
| 84 | struct mmc mmc; |
| 85 | }; |
| 86 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 87 | /** |
| 88 | * struct fsl_esdhc_priv |
| 89 | * |
| 90 | * @esdhc_regs: registers of the sdhc controller |
| 91 | * @sdhc_clk: Current clk of the sdhc controller |
| 92 | * @bus_width: bus width, 1bit, 4bit or 8bit |
| 93 | * @cfg: mmc config |
| 94 | * @mmc: mmc |
| 95 | * Following is used when Driver Model is enabled for MMC |
| 96 | * @dev: pointer for the device |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 97 | * @cd_gpio: gpio for card detection |
Peng Fan | 01eb1c4 | 2016-06-15 10:53:02 +0800 | [diff] [blame] | 98 | * @wp_gpio: gpio for write protection |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 99 | */ |
| 100 | struct fsl_esdhc_priv { |
| 101 | struct fsl_esdhc *esdhc_regs; |
| 102 | unsigned int sdhc_clk; |
Yangbo Lu | 1ca7a9f | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 103 | bool is_sdhc_per_clk; |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 104 | unsigned int clock; |
Yangbo Lu | 77f2632 | 2019-10-21 18:09:07 +0800 | [diff] [blame] | 105 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 106 | struct mmc *mmc; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 107 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 108 | struct udevice *dev; |
Michael Walle | 081d401 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 109 | struct sdhci_adma_desc *adma_desc_table; |
Michael Walle | c9bba2e | 2020-09-23 12:42:48 +0200 | [diff] [blame] | 110 | dma_addr_t dma_addr; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 111 | }; |
| 112 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 113 | /* Return the XFERTYP flags for a given command and data packet */ |
Kim Phillips | f9e0b60 | 2012-10-29 13:34:44 +0000 | [diff] [blame] | 114 | static uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 115 | { |
| 116 | uint xfertyp = 0; |
| 117 | |
| 118 | if (data) { |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 119 | xfertyp |= XFERTYP_DPSEL; |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 120 | if (!IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO) && |
| 121 | cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK && |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 122 | cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200) |
| 123 | xfertyp |= XFERTYP_DMAEN; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 124 | if (data->blocks > 1) { |
| 125 | xfertyp |= XFERTYP_MSBSEL; |
| 126 | xfertyp |= XFERTYP_BCEN; |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 127 | if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111)) |
| 128 | xfertyp |= XFERTYP_AC12EN; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (data->flags & MMC_DATA_READ) |
| 132 | xfertyp |= XFERTYP_DTDSEL; |
| 133 | } |
| 134 | |
| 135 | if (cmd->resp_type & MMC_RSP_CRC) |
| 136 | xfertyp |= XFERTYP_CCCEN; |
| 137 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 138 | xfertyp |= XFERTYP_CICEN; |
| 139 | if (cmd->resp_type & MMC_RSP_136) |
| 140 | xfertyp |= XFERTYP_RSPTYP_136; |
| 141 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 142 | xfertyp |= XFERTYP_RSPTYP_48_BUSY; |
| 143 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 144 | xfertyp |= XFERTYP_RSPTYP_48; |
| 145 | |
Jason Liu | bef0ff0 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 146 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 147 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
Yangbo Lu | b73a3d6 | 2016-01-21 17:33:19 +0800 | [diff] [blame] | 148 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 149 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 150 | } |
| 151 | |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 152 | /* |
| 153 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 154 | */ |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 155 | static void esdhc_pio_read_write(struct fsl_esdhc_priv *priv, |
| 156 | struct mmc_data *data) |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 157 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 158 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 159 | uint blocks; |
| 160 | char *buffer; |
| 161 | uint databuf; |
| 162 | uint size; |
| 163 | uint irqstat; |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 164 | ulong start; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 165 | |
| 166 | if (data->flags & MMC_DATA_READ) { |
| 167 | blocks = data->blocks; |
| 168 | buffer = data->dest; |
| 169 | while (blocks) { |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 170 | start = get_timer(0); |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 171 | size = data->blocksize; |
| 172 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 173 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN)) { |
| 174 | if (get_timer(start) > PIO_TIMEOUT) { |
| 175 | printf("\nData Read Failed in PIO Mode."); |
| 176 | return; |
| 177 | } |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 178 | } |
| 179 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 180 | udelay(100); /* Wait before last byte transfer complete */ |
| 181 | irqstat = esdhc_read32(®s->irqstat); |
| 182 | databuf = in_le32(®s->datport); |
| 183 | *((uint *)buffer) = databuf; |
| 184 | buffer += 4; |
| 185 | size -= 4; |
| 186 | } |
| 187 | blocks--; |
| 188 | } |
| 189 | } else { |
| 190 | blocks = data->blocks; |
Wolfgang Denk | a40545c | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 191 | buffer = (char *)data->src; |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 192 | while (blocks) { |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 193 | start = get_timer(0); |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 194 | size = data->blocksize; |
| 195 | irqstat = esdhc_read32(®s->irqstat); |
Benoît Thébaudeau | 2a7b6f5 | 2017-10-29 22:08:58 +0100 | [diff] [blame] | 196 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN)) { |
| 197 | if (get_timer(start) > PIO_TIMEOUT) { |
| 198 | printf("\nData Write Failed in PIO Mode."); |
| 199 | return; |
| 200 | } |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 201 | } |
| 202 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 203 | udelay(100); /* Wait before last byte transfer complete */ |
| 204 | databuf = *((uint *)buffer); |
| 205 | buffer += 4; |
| 206 | size -= 4; |
| 207 | irqstat = esdhc_read32(®s->irqstat); |
| 208 | out_le32(®s->datport, databuf); |
| 209 | } |
| 210 | blocks--; |
| 211 | } |
| 212 | } |
| 213 | } |
Dipen Dudhat | 5c72f35 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 214 | |
Michael Walle | bdd413f | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 215 | static void esdhc_setup_watermark_level(struct fsl_esdhc_priv *priv, |
| 216 | struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 217 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 218 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Michael Walle | bdd413f | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 219 | uint wml_value = data->blocksize / 4; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 220 | |
| 221 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 0244963 | 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 | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 224 | |
Roy Zang | e5853af | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 225 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 226 | } else { |
Priyanka Jain | 0244963 | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 227 | if (wml_value > WML_WR_WML_MAX) |
| 228 | wml_value = WML_WR_WML_MAX_VAL; |
Yangbo Lu | f3bcc83 | 2019-10-31 18:54:25 +0800 | [diff] [blame] | 229 | |
Roy Zang | e5853af | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 230 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
Michael Walle | bdd413f | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 231 | wml_value << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 232 | } |
Michael Walle | bdd413f | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 233 | } |
Michael Walle | bdd413f | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 234 | |
| 235 | static void esdhc_setup_dma(struct fsl_esdhc_priv *priv, struct mmc_data *data) |
| 236 | { |
| 237 | uint trans_bytes = data->blocksize * data->blocks; |
| 238 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Michael Walle | 081d401 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 239 | phys_addr_t adma_addr; |
Michael Walle | bdd413f | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 240 | void *buf; |
| 241 | |
| 242 | if (data->flags & MMC_DATA_WRITE) |
| 243 | buf = (void *)data->src; |
| 244 | else |
| 245 | buf = data->dest; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 246 | |
Michael Walle | bdd413f | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 247 | priv->dma_addr = dma_map_single(buf, trans_bytes, |
| 248 | mmc_get_dma_dir(data)); |
Michael Walle | 081d401 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 249 | |
| 250 | if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2) && |
| 251 | priv->adma_desc_table) { |
| 252 | debug("Using ADMA2\n"); |
| 253 | /* prefer ADMA2 if it is available */ |
Ian Roberts | 6853d89 | 2024-04-22 15:00:02 -0400 | [diff] [blame] | 254 | sdhci_prepare_adma_table(NULL, priv->adma_desc_table, data, |
Michael Walle | 081d401 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 255 | priv->dma_addr); |
| 256 | |
| 257 | adma_addr = virt_to_phys(priv->adma_desc_table); |
| 258 | esdhc_write32(®s->adsaddrl, lower_32_bits(adma_addr)); |
| 259 | if (IS_ENABLED(CONFIG_DMA_ADDR_T_64BIT)) |
| 260 | esdhc_write32(®s->adsaddrh, upper_32_bits(adma_addr)); |
| 261 | esdhc_clrsetbits32(®s->proctl, PROCTL_DMAS_MASK, |
| 262 | PROCTL_DMAS_ADMA2); |
| 263 | } else { |
| 264 | debug("Using SDMA\n"); |
| 265 | if (upper_32_bits(priv->dma_addr)) |
| 266 | printf("Cannot use 64 bit addresses with SDMA\n"); |
| 267 | esdhc_write32(®s->dsaddr, lower_32_bits(priv->dma_addr)); |
| 268 | esdhc_clrsetbits32(®s->proctl, PROCTL_DMAS_MASK, |
| 269 | PROCTL_DMAS_SDMA); |
| 270 | } |
| 271 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 272 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Michael Walle | bdd413f | 2020-09-23 12:42:49 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int esdhc_setup_data(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 276 | struct mmc_data *data) |
| 277 | { |
| 278 | int timeout; |
| 279 | bool is_write = data->flags & MMC_DATA_WRITE; |
| 280 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 281 | |
| 282 | if (is_write && !(esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL)) { |
| 283 | printf("Can not write to locked SD card.\n"); |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 287 | if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) |
| 288 | esdhc_setup_watermark_level(priv, data); |
| 289 | else |
| 290 | esdhc_setup_dma(priv, data); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 291 | |
| 292 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 293 | /* |
| 294 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 295 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 296 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 297 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 298 | * = (mmc->clock * 1/4) SD Clock cycles |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 299 | * As 1) >= 2) |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 300 | * => (2^(timeout+13)) >= mmc->clock * 1/4 |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 301 | * Taking log2 both the sides |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 302 | * => timeout + 13 >= log2(mmc->clock/4) |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 303 | * Rounding up to next power of 2 |
Andrew Gabbasov | d5b4866 | 2014-03-24 02:40:41 -0500 | [diff] [blame] | 304 | * => timeout + 13 = log2(mmc->clock/4) + 1 |
| 305 | * => timeout + 13 = fls(mmc->clock/4) |
Yangbo Lu | 9d7f321 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 306 | * |
| 307 | * However, the MMC spec "It is strongly recommended for hosts to |
| 308 | * implement more than 500ms timeout value even if the card |
| 309 | * indicates the 250ms maximum busy length." Even the previous |
| 310 | * value of 300ms is known to be insufficient for some cards. |
| 311 | * So, we use |
| 312 | * => timeout + 13 = fls(mmc->clock/2) |
Priyanka Jain | c51b40d | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 313 | */ |
Yangbo Lu | 9d7f321 | 2015-12-30 14:19:30 +0800 | [diff] [blame] | 314 | timeout = fls(mmc->clock/2); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 315 | timeout -= 13; |
| 316 | |
| 317 | if (timeout > 14) |
| 318 | timeout = 14; |
| 319 | |
| 320 | if (timeout < 0) |
| 321 | timeout = 0; |
| 322 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 323 | if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC_A001) && |
| 324 | (timeout == 4 || timeout == 8 || timeout == 12)) |
Kumar Gala | 9a878d5 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 325 | timeout++; |
Kumar Gala | 9a878d5 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 326 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 327 | if (IS_ENABLED(ESDHCI_QUIRK_BROKEN_TIMEOUT_VALUE)) |
| 328 | timeout = 0xE; |
| 329 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 330 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 335 | /* |
| 336 | * Sends a command out on the bus. Takes the mmc pointer, |
| 337 | * a command pointer, and an optional data pointer. |
| 338 | */ |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 339 | static int esdhc_send_cmd_common(struct fsl_esdhc_priv *priv, struct mmc *mmc, |
| 340 | struct mmc_cmd *cmd, struct mmc_data *data) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 341 | { |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 342 | int err = 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 343 | uint xfertyp; |
| 344 | uint irqstat; |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 345 | u32 flags = IRQSTAT_CC | IRQSTAT_CTOE; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 346 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Fabio Estevam | 7300ef5 | 2018-11-19 10:31:53 -0200 | [diff] [blame] | 347 | unsigned long start; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 348 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 349 | if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC111) && |
| 350 | cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 351 | return 0; |
Jerry Huang | ed41367 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 352 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 353 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 354 | |
| 355 | sync(); |
| 356 | |
| 357 | /* Wait for the bus to be idle */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 358 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 359 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 360 | ; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 361 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 362 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 363 | ; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 364 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 365 | /* Set up for a data transfer if we have one */ |
| 366 | if (data) { |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 367 | err = esdhc_setup_data(priv, mmc, data); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 368 | if(err) |
| 369 | return err; |
| 370 | } |
| 371 | |
| 372 | /* Figure out the transfer arguments */ |
| 373 | xfertyp = esdhc_xfertyp(cmd, data); |
| 374 | |
Andrew Gabbasov | 4816b7a | 2013-06-11 10:34:22 -0500 | [diff] [blame] | 375 | /* Mask all irqs */ |
| 376 | esdhc_write32(®s->irqsigen, 0); |
| 377 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 378 | /* Send the command */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 379 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
| 380 | esdhc_write32(®s->xfertyp, xfertyp); |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 381 | |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 382 | if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK || |
| 383 | cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) |
| 384 | flags = IRQSTAT_BRR; |
| 385 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 386 | /* Wait for the command to complete */ |
Fabio Estevam | 7300ef5 | 2018-11-19 10:31:53 -0200 | [diff] [blame] | 387 | start = get_timer(0); |
| 388 | while (!(esdhc_read32(®s->irqstat) & flags)) { |
| 389 | if (get_timer(start) > 1000) { |
| 390 | err = -ETIMEDOUT; |
| 391 | goto out; |
| 392 | } |
| 393 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 394 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 395 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 396 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 397 | if (irqstat & CMD_ERR) { |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 398 | err = -ECOMM; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 399 | goto out; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 402 | if (irqstat & IRQSTAT_CTOE) { |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 403 | err = -ETIMEDOUT; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 404 | goto out; |
| 405 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 406 | |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 407 | /* Workaround for ESDHC errata ENGcm03648 */ |
| 408 | if (!data && (cmd->resp_type & MMC_RSP_BUSY)) { |
Yangbo Lu | 3ffa851 | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 409 | int timeout = 6000; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 410 | |
Yangbo Lu | 3ffa851 | 2015-04-15 10:13:12 +0800 | [diff] [blame] | 411 | /* Poll on DATA0 line for cmd with busy signal for 600 ms */ |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 412 | while (timeout > 0 && !(esdhc_read32(®s->prsstat) & |
| 413 | PRSSTAT_DAT0)) { |
| 414 | udelay(100); |
| 415 | timeout--; |
| 416 | } |
| 417 | |
| 418 | if (timeout <= 0) { |
| 419 | printf("Timeout waiting for DAT0 to go high!\n"); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 420 | err = -ETIMEDOUT; |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 421 | goto out; |
Dirk Behme | d8552d6 | 2012-03-26 03:13:05 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 425 | /* Copy the response to the response buffer */ |
| 426 | if (cmd->resp_type & MMC_RSP_136) { |
| 427 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 428 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 429 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 430 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 431 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 432 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | b6eed94 | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 433 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 434 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 435 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 436 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 437 | } else |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 438 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 439 | |
| 440 | /* Wait until all of the blocks are transferred */ |
| 441 | if (data) { |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 442 | if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_USE_PIO)) { |
| 443 | esdhc_pio_read_write(priv, data); |
| 444 | } else { |
| 445 | flags = DATA_COMPLETE; |
| 446 | if (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK || |
| 447 | cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200) |
| 448 | flags = IRQSTAT_BRR; |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 449 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 450 | do { |
| 451 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 452 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 453 | if (irqstat & IRQSTAT_DTOE) { |
| 454 | err = -ETIMEDOUT; |
| 455 | goto out; |
| 456 | } |
Frans Meulenbroeks | 010ba98 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 457 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 458 | if (irqstat & DATA_ERR) { |
| 459 | err = -ECOMM; |
| 460 | goto out; |
| 461 | } |
| 462 | } while ((irqstat & flags) != flags); |
Ye.Li | 33a56b1 | 2014-02-20 18:00:57 +0800 | [diff] [blame] | 463 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 464 | /* |
| 465 | * Need invalidate the dcache here again to avoid any |
| 466 | * cache-fill during the DMA operations such as the |
| 467 | * speculative pre-fetching etc. |
| 468 | */ |
| 469 | dma_unmap_single(priv->dma_addr, |
| 470 | data->blocks * data->blocksize, |
| 471 | mmc_get_dma_dir(data)); |
| 472 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 473 | } |
| 474 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 475 | out: |
| 476 | /* Reset CMD and DATA portions on error */ |
| 477 | if (err) { |
| 478 | esdhc_write32(®s->sysctl, esdhc_read32(®s->sysctl) | |
| 479 | SYSCTL_RSTC); |
| 480 | while (esdhc_read32(®s->sysctl) & SYSCTL_RSTC) |
| 481 | ; |
| 482 | |
| 483 | if (data) { |
| 484 | esdhc_write32(®s->sysctl, |
| 485 | esdhc_read32(®s->sysctl) | |
| 486 | SYSCTL_RSTD); |
| 487 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTD)) |
| 488 | ; |
| 489 | } |
| 490 | } |
| 491 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 492 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 493 | |
Andrew Gabbasov | a04a6e0 | 2014-03-24 02:41:06 -0500 | [diff] [blame] | 494 | return err; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 495 | } |
| 496 | |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 497 | static void set_sysctl(struct fsl_esdhc_priv *priv, struct mmc *mmc, uint clock) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 498 | { |
Benoît Thébaudeau | 22464e0 | 2018-01-16 22:44:18 +0100 | [diff] [blame] | 499 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Benoît Thébaudeau | e16e922 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 500 | int div = 1; |
Benoît Thébaudeau | e16e922 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 501 | int pre_div = 2; |
Yinbo Zhu | 101d3ef | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 502 | unsigned int sdhc_clk = priv->sdhc_clk; |
| 503 | u32 time_out; |
| 504 | u32 value; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 505 | uint clk; |
Pali Rohár | 7ab6500 | 2022-04-29 20:27:34 +0200 | [diff] [blame] | 506 | u32 hostver; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 507 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 508 | if (clock < mmc->cfg->f_min) |
| 509 | clock = mmc->cfg->f_min; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 510 | |
Yangbo Lu | 4ee9b86 | 2019-10-21 18:09:09 +0800 | [diff] [blame] | 511 | while (sdhc_clk / (16 * pre_div) > clock && pre_div < 256) |
Lukasz Majewski | 2a52183 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 512 | pre_div *= 2; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 513 | |
Yangbo Lu | 4ee9b86 | 2019-10-21 18:09:09 +0800 | [diff] [blame] | 514 | while (sdhc_clk / (div * pre_div) > clock && div < 16) |
Lukasz Majewski | 2a52183 | 2019-05-07 17:47:28 +0200 | [diff] [blame] | 515 | div++; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 516 | |
Michael Walle | 148dc61 | 2021-03-17 15:01:36 +0100 | [diff] [blame] | 517 | if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) && |
| 518 | clock == 200000000 && mmc->selected_mode == MMC_HS_400) { |
| 519 | u32 div_ratio = pre_div * div; |
| 520 | |
| 521 | if (div_ratio <= 4) { |
| 522 | pre_div = 4; |
| 523 | div = 1; |
| 524 | } else if (div_ratio <= 8) { |
| 525 | pre_div = 4; |
| 526 | div = 2; |
| 527 | } else if (div_ratio <= 12) { |
| 528 | pre_div = 4; |
| 529 | div = 3; |
| 530 | } else { |
| 531 | printf("unsupported clock division.\n"); |
| 532 | } |
| 533 | } |
| 534 | |
Yangbo Lu | dd08eea | 2020-09-01 16:58:06 +0800 | [diff] [blame] | 535 | mmc->clock = sdhc_clk / pre_div / div; |
| 536 | priv->clock = mmc->clock; |
| 537 | |
Benoît Thébaudeau | e16e922 | 2017-05-03 11:59:03 +0200 | [diff] [blame] | 538 | pre_div >>= 1; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 539 | div -= 1; |
| 540 | |
| 541 | clk = (pre_div << 8) | (div << 4); |
| 542 | |
Kumar Gala | 09876a3 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 543 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 544 | |
| 545 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 546 | |
Pali Rohár | 7ab6500 | 2022-04-29 20:27:34 +0200 | [diff] [blame] | 547 | /* Only newer eSDHC controllers set PRSSTAT_SDSTB flag */ |
| 548 | hostver = esdhc_read32(&priv->esdhc_regs->hostver); |
| 549 | if (HOSTVER_VENDOR(hostver) <= VENDOR_V_22) { |
| 550 | udelay(10000); |
| 551 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
| 552 | return; |
| 553 | } |
| 554 | |
Yinbo Zhu | 101d3ef | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 555 | time_out = 20; |
| 556 | value = PRSSTAT_SDSTB; |
| 557 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 558 | if (time_out == 0) { |
| 559 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 560 | break; |
| 561 | } |
| 562 | time_out--; |
| 563 | mdelay(1); |
| 564 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 565 | |
Eric Nelson | c8e615c | 2015-12-04 12:32:48 -0700 | [diff] [blame] | 566 | esdhc_setbits32(®s->sysctl, SYSCTL_PEREN | SYSCTL_CKEN); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 567 | } |
| 568 | |
Simon Glass | 1d177d4 | 2017-07-29 11:35:17 -0600 | [diff] [blame] | 569 | static void esdhc_clock_control(struct fsl_esdhc_priv *priv, bool enable) |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 570 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 571 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 572 | u32 value; |
| 573 | u32 time_out; |
Pali Rohár | 7ab6500 | 2022-04-29 20:27:34 +0200 | [diff] [blame] | 574 | u32 hostver; |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 575 | |
| 576 | value = esdhc_read32(®s->sysctl); |
| 577 | |
| 578 | if (enable) |
| 579 | value |= SYSCTL_CKEN; |
| 580 | else |
| 581 | value &= ~SYSCTL_CKEN; |
| 582 | |
| 583 | esdhc_write32(®s->sysctl, value); |
| 584 | |
Pali Rohár | 7ab6500 | 2022-04-29 20:27:34 +0200 | [diff] [blame] | 585 | /* Only newer eSDHC controllers set PRSSTAT_SDSTB flag */ |
| 586 | hostver = esdhc_read32(&priv->esdhc_regs->hostver); |
| 587 | if (HOSTVER_VENDOR(hostver) <= VENDOR_V_22) { |
| 588 | udelay(10000); |
| 589 | return; |
| 590 | } |
| 591 | |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 592 | time_out = 20; |
| 593 | value = PRSSTAT_SDSTB; |
| 594 | while (!(esdhc_read32(®s->prsstat) & value)) { |
| 595 | if (time_out == 0) { |
| 596 | printf("fsl_esdhc: Internal clock never stabilised.\n"); |
| 597 | break; |
| 598 | } |
| 599 | time_out--; |
| 600 | mdelay(1); |
| 601 | } |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 602 | } |
Yangbo Lu | 163beec | 2015-04-22 13:57:40 +0800 | [diff] [blame] | 603 | |
Yangbo Lu | 8f9ace1 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 604 | static void esdhc_flush_async_fifo(struct fsl_esdhc_priv *priv) |
| 605 | { |
| 606 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 607 | u32 time_out; |
| 608 | |
| 609 | esdhc_setbits32(®s->esdhcctl, ESDHCCTL_FAF); |
| 610 | |
| 611 | time_out = 20; |
| 612 | while (esdhc_read32(®s->esdhcctl) & ESDHCCTL_FAF) { |
| 613 | if (time_out == 0) { |
| 614 | printf("fsl_esdhc: Flush asynchronous FIFO timeout.\n"); |
| 615 | break; |
| 616 | } |
| 617 | time_out--; |
| 618 | mdelay(1); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | static void esdhc_tuning_block_enable(struct fsl_esdhc_priv *priv, |
| 623 | bool en) |
| 624 | { |
| 625 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 626 | |
| 627 | esdhc_clock_control(priv, false); |
| 628 | esdhc_flush_async_fifo(priv); |
| 629 | if (en) |
| 630 | esdhc_setbits32(®s->tbctl, TBCTL_TB_EN); |
| 631 | else |
| 632 | esdhc_clrbits32(®s->tbctl, TBCTL_TB_EN); |
| 633 | esdhc_clock_control(priv, true); |
| 634 | } |
| 635 | |
| 636 | static void esdhc_exit_hs400(struct fsl_esdhc_priv *priv) |
| 637 | { |
| 638 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 639 | |
| 640 | esdhc_clrbits32(®s->sdtimingctl, FLW_CTL_BG); |
| 641 | esdhc_clrbits32(®s->sdclkctl, CMD_CLK_CTL); |
| 642 | |
| 643 | esdhc_clock_control(priv, false); |
| 644 | esdhc_clrbits32(®s->tbctl, HS400_MODE); |
| 645 | esdhc_clock_control(priv, true); |
| 646 | |
| 647 | esdhc_clrbits32(®s->dllcfg0, DLL_FREQ_SEL | DLL_ENABLE); |
| 648 | esdhc_clrbits32(®s->tbctl, HS400_WNDW_ADJUST); |
| 649 | |
| 650 | esdhc_tuning_block_enable(priv, false); |
| 651 | } |
| 652 | |
Yangbo Lu | 8fbe95b | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 653 | static int esdhc_set_timing(struct fsl_esdhc_priv *priv, enum bus_mode mode) |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 654 | { |
| 655 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 8fbe95b | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 656 | ulong start; |
| 657 | u32 val; |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 658 | |
Yangbo Lu | 8f9ace1 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 659 | /* Exit HS400 mode before setting any other mode */ |
| 660 | if (esdhc_read32(®s->tbctl) & HS400_MODE && |
| 661 | mode != MMC_HS_400) |
| 662 | esdhc_exit_hs400(priv); |
| 663 | |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 664 | esdhc_clock_control(priv, false); |
| 665 | |
| 666 | if (mode == MMC_HS_200) |
| 667 | esdhc_clrsetbits32(®s->autoc12err, UHSM_MASK, |
| 668 | UHSM_SDR104_HS200); |
Yangbo Lu | 8f9ace1 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 669 | if (mode == MMC_HS_400) { |
| 670 | esdhc_setbits32(®s->tbctl, HS400_MODE); |
| 671 | esdhc_setbits32(®s->sdclkctl, CMD_CLK_CTL); |
| 672 | esdhc_clock_control(priv, true); |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 673 | |
Yangbo Lu | 9ac60a4 | 2020-09-01 16:58:07 +0800 | [diff] [blame] | 674 | if (priv->clock == 200000000) |
| 675 | esdhc_setbits32(®s->dllcfg0, DLL_FREQ_SEL); |
| 676 | |
| 677 | esdhc_setbits32(®s->dllcfg0, DLL_ENABLE); |
Yangbo Lu | 8fbe95b | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 678 | |
| 679 | esdhc_setbits32(®s->dllcfg0, DLL_RESET); |
| 680 | udelay(1); |
| 681 | esdhc_clrbits32(®s->dllcfg0, DLL_RESET); |
| 682 | |
| 683 | start = get_timer(0); |
| 684 | val = DLL_STS_SLV_LOCK; |
| 685 | while (!(esdhc_read32(®s->dllstat0) & val)) { |
| 686 | if (get_timer(start) > 1000) { |
| 687 | printf("fsl_esdhc: delay chain lock timeout\n"); |
| 688 | return -ETIMEDOUT; |
| 689 | } |
| 690 | } |
| 691 | |
Yangbo Lu | 8f9ace1 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 692 | esdhc_setbits32(®s->tbctl, HS400_WNDW_ADJUST); |
| 693 | |
| 694 | esdhc_clock_control(priv, false); |
| 695 | esdhc_flush_async_fifo(priv); |
| 696 | } |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 697 | esdhc_clock_control(priv, true); |
Yangbo Lu | 8fbe95b | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 698 | return 0; |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 699 | } |
| 700 | |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 701 | static int esdhc_set_ios_common(struct fsl_esdhc_priv *priv, struct mmc *mmc) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 702 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 703 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 8fbe95b | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 704 | int ret; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 705 | |
Yangbo Lu | 1ca7a9f | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 706 | if (priv->is_sdhc_per_clk) { |
| 707 | /* Select to use peripheral clock */ |
| 708 | esdhc_clock_control(priv, false); |
| 709 | esdhc_setbits32(®s->esdhcctl, ESDHCCTL_PCS); |
| 710 | esdhc_clock_control(priv, true); |
| 711 | } |
| 712 | |
Yangbo Lu | 8f9ace1 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 713 | if (mmc->selected_mode == MMC_HS_400) |
| 714 | esdhc_tuning_block_enable(priv, true); |
| 715 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 716 | /* Set the clock speed */ |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 717 | if (priv->clock != mmc->clock) |
| 718 | set_sysctl(priv, mmc, mmc->clock); |
| 719 | |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 720 | /* Set timing */ |
Yangbo Lu | 8fbe95b | 2020-10-20 11:04:52 +0800 | [diff] [blame] | 721 | ret = esdhc_set_timing(priv, mmc->selected_mode); |
| 722 | if (ret) |
| 723 | return ret; |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 724 | |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 725 | /* Set the bus width */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 726 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 727 | |
| 728 | if (mmc->bus_width == 4) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 729 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 730 | else if (mmc->bus_width == 8) |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 731 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 732 | |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 733 | return 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 734 | } |
| 735 | |
Rasmus Villemoes | a6d1f1a | 2020-01-30 12:06:45 +0000 | [diff] [blame] | 736 | static void esdhc_enable_cache_snooping(struct fsl_esdhc *regs) |
| 737 | { |
| 738 | #ifdef CONFIG_ARCH_MPC830X |
| 739 | immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
| 740 | sysconf83xx_t *sysconf = &immr->sysconf; |
| 741 | |
| 742 | setbits_be32(&sysconf->sdhccr, 0x02000000); |
| 743 | #else |
Pali Rohár | 6c6cec3 | 2022-04-04 18:32:13 +0200 | [diff] [blame] | 744 | esdhc_write32(®s->esdhcctl, ESDHCCTL_SNOOP); |
Rasmus Villemoes | a6d1f1a | 2020-01-30 12:06:45 +0000 | [diff] [blame] | 745 | #endif |
| 746 | } |
| 747 | |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 748 | static int esdhc_init_common(struct fsl_esdhc_priv *priv, struct mmc *mmc) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 749 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 750 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Simon Glass | 0c3ef22 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 751 | ulong start; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 752 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 753 | /* Reset the entire host controller */ |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 754 | esdhc_setbits32(®s->sysctl, SYSCTL_RSTA); |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 755 | |
| 756 | /* Wait until the controller is available */ |
Simon Glass | 0c3ef22 | 2017-07-29 11:35:20 -0600 | [diff] [blame] | 757 | start = get_timer(0); |
| 758 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA)) { |
| 759 | if (get_timer(start) > 1000) |
| 760 | return -ETIMEDOUT; |
| 761 | } |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 762 | |
Yangbo Lu | 573859c | 2020-09-01 16:58:02 +0800 | [diff] [blame] | 763 | /* Clean TBCTL[TB_EN] which is not able to be reset by reset all */ |
| 764 | esdhc_clrbits32(®s->tbctl, TBCTL_TB_EN); |
| 765 | |
Rasmus Villemoes | a6d1f1a | 2020-01-30 12:06:45 +0000 | [diff] [blame] | 766 | esdhc_enable_cache_snooping(regs); |
P.V.Suresh | 7b1868b | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 767 | |
Dirk Behme | dbe6725 | 2013-07-15 15:44:29 +0200 | [diff] [blame] | 768 | esdhc_setbits32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 769 | |
| 770 | /* Set the initial clock speed */ |
Yangbo Lu | ee2708b | 2020-10-20 11:04:51 +0800 | [diff] [blame] | 771 | set_sysctl(priv, mmc, 400000); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 772 | |
| 773 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 774 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 775 | |
| 776 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 777 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 778 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 779 | /* Set timout to the maximum value */ |
| 780 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 781 | |
Michael Walle | 7259dc5 | 2021-03-17 15:01:37 +0100 | [diff] [blame] | 782 | if (IS_ENABLED(CONFIG_SYS_FSL_ESDHC_UNRELIABLE_PULSE_DETECTION_WORKAROUND)) |
| 783 | esdhc_clrbits32(®s->dllcfg1, DLL_PD_PULSE_STRETCH_SEL); |
| 784 | |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 785 | return 0; |
| 786 | } |
| 787 | |
Simon Glass | 6aa55dc | 2017-07-29 11:35:18 -0600 | [diff] [blame] | 788 | static int esdhc_getcd_common(struct fsl_esdhc_priv *priv) |
Thierry Reding | 8cee4c98 | 2012-01-02 01:15:38 +0000 | [diff] [blame] | 789 | { |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 790 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 791 | |
Haijun.Zhang | 05f5854 | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 792 | #ifdef CONFIG_ESDHC_DETECT_QUIRK |
Tom Rini | bdd47f3 | 2022-06-16 14:04:38 -0400 | [diff] [blame] | 793 | if (qixis_esdhc_detect_quirk()) |
Haijun.Zhang | 05f5854 | 2014-01-10 13:52:17 +0800 | [diff] [blame] | 794 | return 1; |
| 795 | #endif |
Yangbo Lu | 8abc043 | 2020-05-19 11:06:43 +0800 | [diff] [blame] | 796 | if (esdhc_read32(®s->prsstat) & PRSSTAT_CINS) |
| 797 | return 1; |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 798 | |
Yangbo Lu | 8abc043 | 2020-05-19 11:06:43 +0800 | [diff] [blame] | 799 | return 0; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 800 | } |
| 801 | |
Yangbo Lu | b64dc8d | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 802 | static void fsl_esdhc_get_cfg_common(struct fsl_esdhc_priv *priv, |
| 803 | struct mmc_config *cfg) |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 804 | { |
Yangbo Lu | b64dc8d | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 805 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Yangbo Lu | 63267b4 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 806 | u32 caps; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 807 | |
Wang Huan | c929213 | 2014-09-05 13:52:40 +0800 | [diff] [blame] | 808 | caps = esdhc_read32(®s->hostcapblt); |
Yangbo Lu | e087cd6 | 2021-06-03 10:51:17 +0800 | [diff] [blame] | 809 | |
| 810 | /* |
| 811 | * For eSDHC, power supply is through peripheral circuit. Some eSDHC |
| 812 | * versions have value 0 of the bit but that does not reflect the |
| 813 | * truth. 3.3V is common for SD/MMC, and is supported for all boards |
| 814 | * with eSDHC in current u-boot. So, make 3.3V is supported in |
| 815 | * default in code. CONFIG_FSL_ESDHC_VS33_NOT_SUPPORT can be enabled |
| 816 | * if future board does not support 3.3V. |
| 817 | */ |
| 818 | caps |= HOSTCAPBLT_VS33; |
| 819 | if (IS_ENABLED(CONFIG_FSL_ESDHC_VS33_NOT_SUPPORT)) |
| 820 | caps &= ~HOSTCAPBLT_VS33; |
| 821 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 822 | if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_ESDHC135)) |
| 823 | caps &= ~(HOSTCAPBLT_SRS | HOSTCAPBLT_VS18 | HOSTCAPBLT_VS30); |
Yangbo Lu | 63267b4 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 824 | if (caps & HOSTCAPBLT_VS18) |
| 825 | cfg->voltages |= MMC_VDD_165_195; |
| 826 | if (caps & HOSTCAPBLT_VS30) |
| 827 | cfg->voltages |= MMC_VDD_29_30 | MMC_VDD_30_31; |
| 828 | if (caps & HOSTCAPBLT_VS33) |
| 829 | cfg->voltages |= MMC_VDD_32_33 | MMC_VDD_33_34; |
Li Yang | d4933f2 | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 830 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 831 | cfg->name = "FSL_SDHC"; |
Abbas Raza | e6bf977 | 2013-03-25 09:13:34 +0000 | [diff] [blame] | 832 | |
Yangbo Lu | 63267b4 | 2019-10-31 18:54:21 +0800 | [diff] [blame] | 833 | if (caps & HOSTCAPBLT_HSS) |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 834 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Andy Fleming | e52ffb8 | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 835 | |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 836 | cfg->f_min = 400000; |
Peng Fan | c414270 | 2018-01-21 19:00:24 +0800 | [diff] [blame] | 837 | cfg->f_max = min(priv->sdhc_clk, (u32)200000000); |
Simon Glass | fa02ca5 | 2017-07-29 11:35:21 -0600 | [diff] [blame] | 838 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 839 | } |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 840 | |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 841 | #ifdef CONFIG_OF_LIBFDT |
Yangbo Lu | d84139c | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 842 | __weak int esdhc_status_fixup(void *blob, const char *compat) |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 843 | { |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 844 | if (IS_ENABLED(CONFIG_FSL_ESDHC_PIN_MUX) && !hwconfig("esdhc")) { |
Chenhui Zhao | 025eab0 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 845 | do_fixup_by_compat(blob, compat, "status", "disabled", |
Yangbo Lu | d84139c | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 846 | sizeof("disabled"), 1); |
| 847 | return 1; |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 848 | } |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 849 | |
Yangbo Lu | d84139c | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 850 | return 0; |
| 851 | } |
| 852 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 853 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 854 | static int fsl_esdhc_get_cd(struct udevice *dev); |
Yangbo Lu | ce88402 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 855 | static void esdhc_disable_for_no_card(void *blob) |
| 856 | { |
| 857 | struct udevice *dev; |
| 858 | |
| 859 | for (uclass_first_device(UCLASS_MMC, &dev); |
| 860 | dev; |
| 861 | uclass_next_device(&dev)) { |
| 862 | char esdhc_path[50]; |
| 863 | |
| 864 | if (fsl_esdhc_get_cd(dev)) |
| 865 | continue; |
| 866 | |
| 867 | snprintf(esdhc_path, sizeof(esdhc_path), "/soc/esdhc@%lx", |
| 868 | (unsigned long)dev_read_addr(dev)); |
| 869 | do_fixup_by_path(blob, esdhc_path, "status", "disabled", |
| 870 | sizeof("disabled"), 1); |
| 871 | } |
| 872 | } |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 873 | #else |
| 874 | static void esdhc_disable_for_no_card(void *blob) |
| 875 | { |
| 876 | } |
Yangbo Lu | ce88402 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 877 | #endif |
| 878 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 879 | void fdt_fixup_esdhc(void *blob, struct bd_info *bd) |
Yangbo Lu | d84139c | 2017-01-17 10:43:54 +0800 | [diff] [blame] | 880 | { |
| 881 | const char *compat = "fsl,esdhc"; |
| 882 | |
| 883 | if (esdhc_status_fixup(blob, compat)) |
| 884 | return; |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 885 | |
| 886 | if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND)) |
| 887 | esdhc_disable_for_no_card(blob); |
| 888 | |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 889 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
Simon Glass | 9e247d1 | 2012-12-13 20:49:05 +0000 | [diff] [blame] | 890 | gd->arch.sdhc_clk, 1); |
Anton Vorontsov | f751a3c | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 891 | } |
Stefano Babic | ff7a5ca | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 892 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 893 | |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 894 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 895 | static int esdhc_getcd(struct mmc *mmc) |
| 896 | { |
| 897 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 898 | |
| 899 | return esdhc_getcd_common(priv); |
| 900 | } |
| 901 | |
| 902 | static int esdhc_init(struct mmc *mmc) |
| 903 | { |
| 904 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 905 | |
| 906 | return esdhc_init_common(priv, mmc); |
| 907 | } |
| 908 | |
| 909 | static int esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 910 | struct mmc_data *data) |
| 911 | { |
| 912 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 913 | |
| 914 | return esdhc_send_cmd_common(priv, mmc, cmd, data); |
| 915 | } |
| 916 | |
| 917 | static int esdhc_set_ios(struct mmc *mmc) |
| 918 | { |
| 919 | struct fsl_esdhc_priv *priv = mmc->priv; |
| 920 | |
| 921 | return esdhc_set_ios_common(priv, mmc); |
| 922 | } |
| 923 | |
| 924 | static const struct mmc_ops esdhc_ops = { |
| 925 | .getcd = esdhc_getcd, |
| 926 | .init = esdhc_init, |
| 927 | .send_cmd = esdhc_send_cmd, |
| 928 | .set_ios = esdhc_set_ios, |
| 929 | }; |
| 930 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 931 | int fsl_esdhc_initialize(struct bd_info *bis, struct fsl_esdhc_cfg *cfg) |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 932 | { |
| 933 | struct fsl_esdhc_plat *plat; |
| 934 | struct fsl_esdhc_priv *priv; |
| 935 | struct mmc_config *mmc_cfg; |
| 936 | struct mmc *mmc; |
| 937 | |
| 938 | if (!cfg) |
| 939 | return -EINVAL; |
| 940 | |
| 941 | priv = calloc(sizeof(struct fsl_esdhc_priv), 1); |
| 942 | if (!priv) |
| 943 | return -ENOMEM; |
| 944 | plat = calloc(sizeof(struct fsl_esdhc_plat), 1); |
| 945 | if (!plat) { |
| 946 | free(priv); |
| 947 | return -ENOMEM; |
| 948 | } |
| 949 | |
| 950 | priv->esdhc_regs = (struct fsl_esdhc *)(unsigned long)(cfg->esdhc_base); |
| 951 | priv->sdhc_clk = cfg->sdhc_clk; |
Yangbo Lu | 1ca7a9f | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 952 | if (gd->arch.sdhc_per_clk) |
| 953 | priv->is_sdhc_per_clk = true; |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 954 | |
| 955 | mmc_cfg = &plat->cfg; |
| 956 | |
| 957 | if (cfg->max_bus_width == 8) { |
| 958 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT | |
| 959 | MMC_MODE_8BIT; |
| 960 | } else if (cfg->max_bus_width == 4) { |
| 961 | mmc_cfg->host_caps |= MMC_MODE_1BIT | MMC_MODE_4BIT; |
| 962 | } else if (cfg->max_bus_width == 1) { |
| 963 | mmc_cfg->host_caps |= MMC_MODE_1BIT; |
| 964 | } else { |
Pali Rohár | 39a43b0 | 2022-05-11 20:27:12 +0200 | [diff] [blame] | 965 | mmc_cfg->host_caps |= MMC_MODE_1BIT; |
| 966 | printf("No max bus width provided. Fallback to 1-bit mode.\n"); |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 967 | } |
| 968 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 969 | if (IS_ENABLED(CONFIG_ESDHC_DETECT_8_BIT_QUIRK)) |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 970 | mmc_cfg->host_caps &= ~MMC_MODE_8BIT; |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 971 | |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 972 | mmc_cfg->ops = &esdhc_ops; |
| 973 | |
| 974 | fsl_esdhc_get_cfg_common(priv, mmc_cfg); |
| 975 | |
| 976 | mmc = mmc_create(mmc_cfg, priv); |
| 977 | if (!mmc) |
| 978 | return -EIO; |
| 979 | |
| 980 | priv->mmc = mmc; |
| 981 | return 0; |
| 982 | } |
| 983 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 984 | int fsl_esdhc_mmc_init(struct bd_info *bis) |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 985 | { |
| 986 | struct fsl_esdhc_cfg *cfg; |
| 987 | |
| 988 | cfg = calloc(sizeof(struct fsl_esdhc_cfg), 1); |
Tom Rini | 376b88a | 2022-10-28 20:27:13 -0400 | [diff] [blame] | 989 | cfg->esdhc_base = CFG_SYS_FSL_ESDHC_ADDR; |
Pali Rohár | 3a672d6 | 2022-05-11 20:27:13 +0200 | [diff] [blame] | 990 | cfg->max_bus_width = CONFIG_SYS_FSL_ESDHC_DEFAULT_BUS_WIDTH; |
Yangbo Lu | 1ca7a9f | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 991 | /* Prefer peripheral clock which provides higher frequency. */ |
| 992 | if (gd->arch.sdhc_per_clk) |
| 993 | cfg->sdhc_clk = gd->arch.sdhc_per_clk; |
| 994 | else |
| 995 | cfg->sdhc_clk = gd->arch.sdhc_clk; |
Yangbo Lu | 4fc9333 | 2019-10-31 18:54:26 +0800 | [diff] [blame] | 996 | return fsl_esdhc_initialize(bis, cfg); |
| 997 | } |
| 998 | #else /* DM_MMC */ |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 999 | static int fsl_esdhc_probe(struct udevice *dev) |
| 1000 | { |
| 1001 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1002 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1003 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
Michael Walle | 081d401 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 1004 | u32 caps, hostver; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1005 | fdt_addr_t addr; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1006 | struct mmc *mmc; |
Yangbo Lu | ce88402 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 1007 | int ret; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1008 | |
Simon Glass | 80e9df4 | 2017-07-29 11:35:23 -0600 | [diff] [blame] | 1009 | addr = dev_read_addr(dev); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1010 | if (addr == FDT_ADDR_T_NONE) |
| 1011 | return -EINVAL; |
Yinbo Zhu | 583d5e9 | 2019-04-11 11:01:50 +0000 | [diff] [blame] | 1012 | #ifdef CONFIG_PPC |
| 1013 | priv->esdhc_regs = (struct fsl_esdhc *)lower_32_bits(addr); |
| 1014 | #else |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1015 | priv->esdhc_regs = (struct fsl_esdhc *)addr; |
Yinbo Zhu | 583d5e9 | 2019-04-11 11:01:50 +0000 | [diff] [blame] | 1016 | #endif |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1017 | priv->dev = dev; |
| 1018 | |
Michael Walle | 081d401 | 2020-10-12 10:07:14 +0200 | [diff] [blame] | 1019 | if (IS_ENABLED(CONFIG_FSL_ESDHC_SUPPORT_ADMA2)) { |
| 1020 | /* |
| 1021 | * Only newer eSDHC controllers can do ADMA2 if the ADMA flag |
| 1022 | * is set in the host capabilities register. |
| 1023 | */ |
| 1024 | caps = esdhc_read32(&priv->esdhc_regs->hostcapblt); |
| 1025 | hostver = esdhc_read32(&priv->esdhc_regs->hostver); |
| 1026 | if (caps & HOSTCAPBLT_DMAS && |
| 1027 | HOSTVER_VENDOR(hostver) > VENDOR_V_22) { |
| 1028 | priv->adma_desc_table = sdhci_adma_init(); |
| 1029 | if (!priv->adma_desc_table) |
| 1030 | debug("Could not allocate ADMA tables, falling back to SDMA\n"); |
| 1031 | } |
| 1032 | } |
| 1033 | |
Yangbo Lu | 1ca7a9f | 2019-12-19 18:59:30 +0800 | [diff] [blame] | 1034 | if (gd->arch.sdhc_per_clk) { |
| 1035 | priv->sdhc_clk = gd->arch.sdhc_per_clk; |
| 1036 | priv->is_sdhc_per_clk = true; |
| 1037 | } else { |
| 1038 | priv->sdhc_clk = gd->arch.sdhc_clk; |
| 1039 | } |
| 1040 | |
Yangbo Lu | b8626e4 | 2019-11-12 19:28:36 +0800 | [diff] [blame] | 1041 | if (priv->sdhc_clk <= 0) { |
| 1042 | dev_err(dev, "Unable to get clk for %s\n", dev->name); |
| 1043 | return -EINVAL; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Yangbo Lu | b64dc8d | 2019-10-31 18:54:23 +0800 | [diff] [blame] | 1046 | fsl_esdhc_get_cfg_common(priv, &plat->cfg); |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1047 | |
Yinbo Zhu | 101d3ef | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 1048 | mmc_of_parse(dev, &plat->cfg); |
| 1049 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1050 | mmc = &plat->mmc; |
| 1051 | mmc->cfg = &plat->cfg; |
| 1052 | mmc->dev = dev; |
Yangbo Lu | 4cc119b | 2019-05-23 11:05:46 +0800 | [diff] [blame] | 1053 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1054 | upriv->mmc = mmc; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1055 | |
Yangbo Lu | ce88402 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 1056 | ret = esdhc_init_common(priv, mmc); |
| 1057 | if (ret) |
| 1058 | return ret; |
| 1059 | |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 1060 | if (IS_ENABLED(CONFIG_FSL_ESDHC_33V_IO_RELIABILITY_WORKAROUND) && |
| 1061 | !fsl_esdhc_get_cd(dev)) |
Yangbo Lu | ce88402 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 1062 | esdhc_setbits32(&priv->esdhc_regs->proctl, PROCTL_VOLT_SEL); |
Michael Walle | bc9e13e | 2020-10-12 10:07:13 +0200 | [diff] [blame] | 1063 | |
Yangbo Lu | ce88402 | 2020-05-19 11:06:44 +0800 | [diff] [blame] | 1064 | return 0; |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1065 | } |
| 1066 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1067 | static int fsl_esdhc_get_cd(struct udevice *dev) |
| 1068 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1069 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1070 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1071 | |
Yangbo Lu | 9fed28d | 2019-10-31 18:54:24 +0800 | [diff] [blame] | 1072 | if (plat->cfg.host_caps & MMC_CAP_NONREMOVABLE) |
| 1073 | return 1; |
| 1074 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1075 | return esdhc_getcd_common(priv); |
| 1076 | } |
| 1077 | |
| 1078 | static int fsl_esdhc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 1079 | struct mmc_data *data) |
| 1080 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1081 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1082 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1083 | |
| 1084 | return esdhc_send_cmd_common(priv, &plat->mmc, cmd, data); |
| 1085 | } |
| 1086 | |
| 1087 | static int fsl_esdhc_set_ios(struct udevice *dev) |
| 1088 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1089 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1090 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1091 | |
| 1092 | return esdhc_set_ios_common(priv, &plat->mmc); |
| 1093 | } |
| 1094 | |
Yangbo Lu | 76c7469 | 2020-09-01 16:58:00 +0800 | [diff] [blame] | 1095 | static int fsl_esdhc_reinit(struct udevice *dev) |
| 1096 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1097 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Yangbo Lu | 76c7469 | 2020-09-01 16:58:00 +0800 | [diff] [blame] | 1098 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1099 | |
| 1100 | return esdhc_init_common(priv, &plat->mmc); |
| 1101 | } |
| 1102 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 1103 | #if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING) |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1104 | static int fsl_esdhc_execute_tuning(struct udevice *dev, uint32_t opcode) |
| 1105 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1106 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1107 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1108 | struct fsl_esdhc *regs = priv->esdhc_regs; |
Michael Walle | 148dc61 | 2021-03-17 15:01:36 +0100 | [diff] [blame] | 1109 | struct mmc *mmc = &plat->mmc; |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1110 | u32 val, irqstaten; |
| 1111 | int i; |
| 1112 | |
Michael Walle | 148dc61 | 2021-03-17 15:01:36 +0100 | [diff] [blame] | 1113 | if (IS_ENABLED(CONFIG_SYS_FSL_ERRATUM_A011334) && |
| 1114 | plat->mmc.hs400_tuning) |
| 1115 | set_sysctl(priv, mmc, mmc->clock); |
| 1116 | |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1117 | esdhc_tuning_block_enable(priv, true); |
| 1118 | esdhc_setbits32(®s->autoc12err, EXECUTE_TUNING); |
| 1119 | |
| 1120 | irqstaten = esdhc_read32(®s->irqstaten); |
| 1121 | esdhc_write32(®s->irqstaten, IRQSTATEN_BRR); |
| 1122 | |
| 1123 | for (i = 0; i < MAX_TUNING_LOOP; i++) { |
Marek Vasut | dad81fb | 2024-02-20 09:36:23 +0100 | [diff] [blame] | 1124 | mmc_send_tuning(mmc, opcode); |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1125 | mdelay(1); |
| 1126 | |
| 1127 | val = esdhc_read32(®s->autoc12err); |
| 1128 | if (!(val & EXECUTE_TUNING)) { |
| 1129 | if (val & SMPCLKSEL) |
| 1130 | break; |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | esdhc_write32(®s->irqstaten, irqstaten); |
| 1135 | |
Yangbo Lu | 8f9ace1 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 1136 | if (i != MAX_TUNING_LOOP) { |
| 1137 | if (plat->mmc.hs400_tuning) |
| 1138 | esdhc_setbits32(®s->sdtimingctl, FLW_CTL_BG); |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1139 | return 0; |
Yangbo Lu | 8f9ace1 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 1140 | } |
Yangbo Lu | 73da9c8 | 2020-09-01 16:58:01 +0800 | [diff] [blame] | 1141 | |
| 1142 | printf("fsl_esdhc: tuning failed!\n"); |
| 1143 | esdhc_clrbits32(®s->autoc12err, SMPCLKSEL); |
| 1144 | esdhc_clrbits32(®s->autoc12err, EXECUTE_TUNING); |
| 1145 | esdhc_tuning_block_enable(priv, false); |
| 1146 | return -ETIMEDOUT; |
| 1147 | } |
| 1148 | #endif |
| 1149 | |
Yangbo Lu | 8f9ace1 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 1150 | int fsl_esdhc_hs400_prepare_ddr(struct udevice *dev) |
| 1151 | { |
| 1152 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1153 | |
| 1154 | esdhc_tuning_block_enable(priv, false); |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
Stephen Carlson | 1822a97 | 2021-08-17 12:46:40 -0700 | [diff] [blame] | 1158 | static int fsl_esdhc_wait_dat0(struct udevice *dev, int state, |
| 1159 | int timeout_us) |
| 1160 | { |
| 1161 | int ret; |
| 1162 | u32 tmp; |
| 1163 | struct fsl_esdhc_priv *priv = dev_get_priv(dev); |
| 1164 | struct fsl_esdhc *regs = priv->esdhc_regs; |
| 1165 | |
| 1166 | ret = readx_poll_timeout(esdhc_read32, ®s->prsstat, tmp, |
| 1167 | !!(tmp & PRSSTAT_DAT0) == !!state, |
| 1168 | timeout_us); |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1172 | static const struct dm_mmc_ops fsl_esdhc_ops = { |
| 1173 | .get_cd = fsl_esdhc_get_cd, |
| 1174 | .send_cmd = fsl_esdhc_send_cmd, |
| 1175 | .set_ios = fsl_esdhc_set_ios, |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 1176 | #if CONFIG_IS_ENABLED(MMC_SUPPORTS_TUNING) |
Yinbo Zhu | 101d3ef | 2019-07-16 15:09:11 +0800 | [diff] [blame] | 1177 | .execute_tuning = fsl_esdhc_execute_tuning, |
| 1178 | #endif |
Yangbo Lu | 76c7469 | 2020-09-01 16:58:00 +0800 | [diff] [blame] | 1179 | .reinit = fsl_esdhc_reinit, |
Yangbo Lu | 8f9ace1 | 2020-09-01 16:58:05 +0800 | [diff] [blame] | 1180 | .hs400_prepare_ddr = fsl_esdhc_hs400_prepare_ddr, |
Stephen Carlson | 1822a97 | 2021-08-17 12:46:40 -0700 | [diff] [blame] | 1181 | .wait_dat0 = fsl_esdhc_wait_dat0, |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1182 | }; |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1183 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1184 | static const struct udevice_id fsl_esdhc_ids[] = { |
Yangbo Lu | 2a99b60 | 2016-12-07 11:54:31 +0800 | [diff] [blame] | 1185 | { .compatible = "fsl,esdhc", }, |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1186 | { /* sentinel */ } |
| 1187 | }; |
| 1188 | |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1189 | static int fsl_esdhc_bind(struct udevice *dev) |
| 1190 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1191 | struct fsl_esdhc_plat *plat = dev_get_plat(dev); |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1192 | |
| 1193 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 1194 | } |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1195 | |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1196 | U_BOOT_DRIVER(fsl_esdhc) = { |
| 1197 | .name = "fsl-esdhc-mmc", |
| 1198 | .id = UCLASS_MMC, |
| 1199 | .of_match = fsl_esdhc_ids, |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1200 | .ops = &fsl_esdhc_ops, |
Simon Glass | 407025d | 2017-07-29 11:35:24 -0600 | [diff] [blame] | 1201 | .bind = fsl_esdhc_bind, |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1202 | .probe = fsl_esdhc_probe, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1203 | .plat_auto = sizeof(struct fsl_esdhc_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1204 | .priv_auto = sizeof(struct fsl_esdhc_priv), |
Peng Fan | a4d36f7 | 2016-03-25 14:16:56 +0800 | [diff] [blame] | 1205 | }; |
| 1206 | #endif |