Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 2 | /* |
Patrice Chotard | 789ee0e | 2017-10-23 09:53:58 +0200 | [diff] [blame] | 3 | * Copyright (C) 2017, STMicroelectronics - All Rights Reserved |
Patrice Chotard | 5d9950d | 2020-12-02 18:47:30 +0100 | [diff] [blame] | 4 | * Author(s): Patrice Chotard, <patrice.chotard@foss.st.com> for STMicroelectronics. |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_MMC |
| 8 | |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <clk.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <asm/bitops.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 17 | #include <asm/cache.h> |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 18 | #include <dm/device_compat.h> |
Marek Vasut | 98a1205 | 2021-11-13 03:29:43 +0100 | [diff] [blame] | 19 | #include <dm/pinctrl.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 20 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 22 | #include <linux/libfdt.h> |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 23 | #include <mmc.h> |
| 24 | #include <reset.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/gpio.h> |
| 27 | #include <linux/iopoll.h> |
Christophe Kerello | e0452e8 | 2019-07-30 19:16:45 +0200 | [diff] [blame] | 28 | #include <watchdog.h> |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 29 | |
| 30 | struct stm32_sdmmc2_plat { |
| 31 | struct mmc_config cfg; |
| 32 | struct mmc mmc; |
| 33 | }; |
| 34 | |
| 35 | struct stm32_sdmmc2_priv { |
| 36 | fdt_addr_t base; |
| 37 | struct clk clk; |
| 38 | struct reset_ctl reset_ctl; |
| 39 | struct gpio_desc cd_gpio; |
| 40 | u32 clk_reg_msk; |
| 41 | u32 pwr_reg_msk; |
| 42 | }; |
| 43 | |
| 44 | struct stm32_sdmmc2_ctx { |
| 45 | u32 cache_start; |
| 46 | u32 cache_end; |
| 47 | u32 data_length; |
| 48 | bool dpsm_abort; |
| 49 | }; |
| 50 | |
| 51 | /* SDMMC REGISTERS OFFSET */ |
| 52 | #define SDMMC_POWER 0x00 /* SDMMC power control */ |
| 53 | #define SDMMC_CLKCR 0x04 /* SDMMC clock control */ |
| 54 | #define SDMMC_ARG 0x08 /* SDMMC argument */ |
| 55 | #define SDMMC_CMD 0x0C /* SDMMC command */ |
| 56 | #define SDMMC_RESP1 0x14 /* SDMMC response 1 */ |
| 57 | #define SDMMC_RESP2 0x18 /* SDMMC response 2 */ |
| 58 | #define SDMMC_RESP3 0x1C /* SDMMC response 3 */ |
| 59 | #define SDMMC_RESP4 0x20 /* SDMMC response 4 */ |
| 60 | #define SDMMC_DTIMER 0x24 /* SDMMC data timer */ |
| 61 | #define SDMMC_DLEN 0x28 /* SDMMC data length */ |
| 62 | #define SDMMC_DCTRL 0x2C /* SDMMC data control */ |
| 63 | #define SDMMC_DCOUNT 0x30 /* SDMMC data counter */ |
| 64 | #define SDMMC_STA 0x34 /* SDMMC status */ |
| 65 | #define SDMMC_ICR 0x38 /* SDMMC interrupt clear */ |
| 66 | #define SDMMC_MASK 0x3C /* SDMMC mask */ |
| 67 | #define SDMMC_IDMACTRL 0x50 /* SDMMC DMA control */ |
| 68 | #define SDMMC_IDMABASE0 0x58 /* SDMMC DMA buffer 0 base address */ |
| 69 | |
| 70 | /* SDMMC_POWER register */ |
Patrick Delaunay | 6935108 | 2018-06-27 10:15:33 +0200 | [diff] [blame] | 71 | #define SDMMC_POWER_PWRCTRL_MASK GENMASK(1, 0) |
| 72 | #define SDMMC_POWER_PWRCTRL_OFF 0 |
| 73 | #define SDMMC_POWER_PWRCTRL_CYCLE 2 |
| 74 | #define SDMMC_POWER_PWRCTRL_ON 3 |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 75 | #define SDMMC_POWER_VSWITCH BIT(2) |
| 76 | #define SDMMC_POWER_VSWITCHEN BIT(3) |
| 77 | #define SDMMC_POWER_DIRPOL BIT(4) |
| 78 | |
| 79 | /* SDMMC_CLKCR register */ |
| 80 | #define SDMMC_CLKCR_CLKDIV GENMASK(9, 0) |
| 81 | #define SDMMC_CLKCR_CLKDIV_MAX SDMMC_CLKCR_CLKDIV |
| 82 | #define SDMMC_CLKCR_PWRSAV BIT(12) |
| 83 | #define SDMMC_CLKCR_WIDBUS_4 BIT(14) |
| 84 | #define SDMMC_CLKCR_WIDBUS_8 BIT(15) |
| 85 | #define SDMMC_CLKCR_NEGEDGE BIT(16) |
| 86 | #define SDMMC_CLKCR_HWFC_EN BIT(17) |
| 87 | #define SDMMC_CLKCR_DDR BIT(18) |
| 88 | #define SDMMC_CLKCR_BUSSPEED BIT(19) |
Patrick Delaunay | a36da36 | 2018-02-07 17:19:59 +0100 | [diff] [blame] | 89 | #define SDMMC_CLKCR_SELCLKRX_MASK GENMASK(21, 20) |
| 90 | #define SDMMC_CLKCR_SELCLKRX_CK 0 |
| 91 | #define SDMMC_CLKCR_SELCLKRX_CKIN BIT(20) |
| 92 | #define SDMMC_CLKCR_SELCLKRX_FBCK BIT(21) |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 93 | |
| 94 | /* SDMMC_CMD register */ |
| 95 | #define SDMMC_CMD_CMDINDEX GENMASK(5, 0) |
| 96 | #define SDMMC_CMD_CMDTRANS BIT(6) |
| 97 | #define SDMMC_CMD_CMDSTOP BIT(7) |
| 98 | #define SDMMC_CMD_WAITRESP GENMASK(9, 8) |
| 99 | #define SDMMC_CMD_WAITRESP_0 BIT(8) |
| 100 | #define SDMMC_CMD_WAITRESP_1 BIT(9) |
| 101 | #define SDMMC_CMD_WAITINT BIT(10) |
| 102 | #define SDMMC_CMD_WAITPEND BIT(11) |
| 103 | #define SDMMC_CMD_CPSMEN BIT(12) |
| 104 | #define SDMMC_CMD_DTHOLD BIT(13) |
| 105 | #define SDMMC_CMD_BOOTMODE BIT(14) |
| 106 | #define SDMMC_CMD_BOOTEN BIT(15) |
| 107 | #define SDMMC_CMD_CMDSUSPEND BIT(16) |
| 108 | |
| 109 | /* SDMMC_DCTRL register */ |
| 110 | #define SDMMC_DCTRL_DTEN BIT(0) |
| 111 | #define SDMMC_DCTRL_DTDIR BIT(1) |
| 112 | #define SDMMC_DCTRL_DTMODE GENMASK(3, 2) |
| 113 | #define SDMMC_DCTRL_DBLOCKSIZE GENMASK(7, 4) |
| 114 | #define SDMMC_DCTRL_DBLOCKSIZE_SHIFT 4 |
| 115 | #define SDMMC_DCTRL_RWSTART BIT(8) |
| 116 | #define SDMMC_DCTRL_RWSTOP BIT(9) |
| 117 | #define SDMMC_DCTRL_RWMOD BIT(10) |
| 118 | #define SDMMC_DCTRL_SDMMCEN BIT(11) |
| 119 | #define SDMMC_DCTRL_BOOTACKEN BIT(12) |
| 120 | #define SDMMC_DCTRL_FIFORST BIT(13) |
| 121 | |
| 122 | /* SDMMC_STA register */ |
| 123 | #define SDMMC_STA_CCRCFAIL BIT(0) |
| 124 | #define SDMMC_STA_DCRCFAIL BIT(1) |
| 125 | #define SDMMC_STA_CTIMEOUT BIT(2) |
| 126 | #define SDMMC_STA_DTIMEOUT BIT(3) |
| 127 | #define SDMMC_STA_TXUNDERR BIT(4) |
| 128 | #define SDMMC_STA_RXOVERR BIT(5) |
| 129 | #define SDMMC_STA_CMDREND BIT(6) |
| 130 | #define SDMMC_STA_CMDSENT BIT(7) |
| 131 | #define SDMMC_STA_DATAEND BIT(8) |
| 132 | #define SDMMC_STA_DHOLD BIT(9) |
| 133 | #define SDMMC_STA_DBCKEND BIT(10) |
| 134 | #define SDMMC_STA_DABORT BIT(11) |
| 135 | #define SDMMC_STA_DPSMACT BIT(12) |
| 136 | #define SDMMC_STA_CPSMACT BIT(13) |
| 137 | #define SDMMC_STA_TXFIFOHE BIT(14) |
| 138 | #define SDMMC_STA_RXFIFOHF BIT(15) |
| 139 | #define SDMMC_STA_TXFIFOF BIT(16) |
| 140 | #define SDMMC_STA_RXFIFOF BIT(17) |
| 141 | #define SDMMC_STA_TXFIFOE BIT(18) |
| 142 | #define SDMMC_STA_RXFIFOE BIT(19) |
| 143 | #define SDMMC_STA_BUSYD0 BIT(20) |
| 144 | #define SDMMC_STA_BUSYD0END BIT(21) |
| 145 | #define SDMMC_STA_SDMMCIT BIT(22) |
| 146 | #define SDMMC_STA_ACKFAIL BIT(23) |
| 147 | #define SDMMC_STA_ACKTIMEOUT BIT(24) |
| 148 | #define SDMMC_STA_VSWEND BIT(25) |
| 149 | #define SDMMC_STA_CKSTOP BIT(26) |
| 150 | #define SDMMC_STA_IDMATE BIT(27) |
| 151 | #define SDMMC_STA_IDMABTC BIT(28) |
| 152 | |
| 153 | /* SDMMC_ICR register */ |
| 154 | #define SDMMC_ICR_CCRCFAILC BIT(0) |
| 155 | #define SDMMC_ICR_DCRCFAILC BIT(1) |
| 156 | #define SDMMC_ICR_CTIMEOUTC BIT(2) |
| 157 | #define SDMMC_ICR_DTIMEOUTC BIT(3) |
| 158 | #define SDMMC_ICR_TXUNDERRC BIT(4) |
| 159 | #define SDMMC_ICR_RXOVERRC BIT(5) |
| 160 | #define SDMMC_ICR_CMDRENDC BIT(6) |
| 161 | #define SDMMC_ICR_CMDSENTC BIT(7) |
| 162 | #define SDMMC_ICR_DATAENDC BIT(8) |
| 163 | #define SDMMC_ICR_DHOLDC BIT(9) |
| 164 | #define SDMMC_ICR_DBCKENDC BIT(10) |
| 165 | #define SDMMC_ICR_DABORTC BIT(11) |
| 166 | #define SDMMC_ICR_BUSYD0ENDC BIT(21) |
| 167 | #define SDMMC_ICR_SDMMCITC BIT(22) |
| 168 | #define SDMMC_ICR_ACKFAILC BIT(23) |
| 169 | #define SDMMC_ICR_ACKTIMEOUTC BIT(24) |
| 170 | #define SDMMC_ICR_VSWENDC BIT(25) |
| 171 | #define SDMMC_ICR_CKSTOPC BIT(26) |
| 172 | #define SDMMC_ICR_IDMATEC BIT(27) |
| 173 | #define SDMMC_ICR_IDMABTCC BIT(28) |
| 174 | #define SDMMC_ICR_STATIC_FLAGS ((GENMASK(28, 21)) | (GENMASK(11, 0))) |
| 175 | |
| 176 | /* SDMMC_MASK register */ |
| 177 | #define SDMMC_MASK_CCRCFAILIE BIT(0) |
| 178 | #define SDMMC_MASK_DCRCFAILIE BIT(1) |
| 179 | #define SDMMC_MASK_CTIMEOUTIE BIT(2) |
| 180 | #define SDMMC_MASK_DTIMEOUTIE BIT(3) |
| 181 | #define SDMMC_MASK_TXUNDERRIE BIT(4) |
| 182 | #define SDMMC_MASK_RXOVERRIE BIT(5) |
| 183 | #define SDMMC_MASK_CMDRENDIE BIT(6) |
| 184 | #define SDMMC_MASK_CMDSENTIE BIT(7) |
| 185 | #define SDMMC_MASK_DATAENDIE BIT(8) |
| 186 | #define SDMMC_MASK_DHOLDIE BIT(9) |
| 187 | #define SDMMC_MASK_DBCKENDIE BIT(10) |
| 188 | #define SDMMC_MASK_DABORTIE BIT(11) |
| 189 | #define SDMMC_MASK_TXFIFOHEIE BIT(14) |
| 190 | #define SDMMC_MASK_RXFIFOHFIE BIT(15) |
| 191 | #define SDMMC_MASK_RXFIFOFIE BIT(17) |
| 192 | #define SDMMC_MASK_TXFIFOEIE BIT(18) |
| 193 | #define SDMMC_MASK_BUSYD0ENDIE BIT(21) |
| 194 | #define SDMMC_MASK_SDMMCITIE BIT(22) |
| 195 | #define SDMMC_MASK_ACKFAILIE BIT(23) |
| 196 | #define SDMMC_MASK_ACKTIMEOUTIE BIT(24) |
| 197 | #define SDMMC_MASK_VSWENDIE BIT(25) |
| 198 | #define SDMMC_MASK_CKSTOPIE BIT(26) |
| 199 | #define SDMMC_MASK_IDMABTCIE BIT(28) |
| 200 | |
| 201 | /* SDMMC_IDMACTRL register */ |
| 202 | #define SDMMC_IDMACTRL_IDMAEN BIT(0) |
| 203 | |
| 204 | #define SDMMC_CMD_TIMEOUT 0xFFFFFFFF |
Patrice Chotard | bd16b4c | 2019-07-22 11:41:10 +0200 | [diff] [blame] | 205 | #define SDMMC_BUSYD0END_TIMEOUT_US 2000000 |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 206 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 207 | static void stm32_sdmmc2_start_data(struct udevice *dev, |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 208 | struct mmc_data *data, |
| 209 | struct stm32_sdmmc2_ctx *ctx) |
| 210 | { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 211 | struct stm32_sdmmc2_priv *priv = dev_get_priv(dev); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 212 | u32 data_ctrl, idmabase0; |
| 213 | |
| 214 | /* Configure the SDMMC DPSM (Data Path State Machine) */ |
| 215 | data_ctrl = (__ilog2(data->blocksize) << |
| 216 | SDMMC_DCTRL_DBLOCKSIZE_SHIFT) & |
| 217 | SDMMC_DCTRL_DBLOCKSIZE; |
| 218 | |
| 219 | if (data->flags & MMC_DATA_READ) { |
| 220 | data_ctrl |= SDMMC_DCTRL_DTDIR; |
| 221 | idmabase0 = (u32)data->dest; |
| 222 | } else { |
| 223 | idmabase0 = (u32)data->src; |
| 224 | } |
| 225 | |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 226 | /* Set the SDMMC DataLength value */ |
| 227 | writel(ctx->data_length, priv->base + SDMMC_DLEN); |
| 228 | |
| 229 | /* Write to SDMMC DCTRL */ |
| 230 | writel(data_ctrl, priv->base + SDMMC_DCTRL); |
| 231 | |
| 232 | /* Cache align */ |
| 233 | ctx->cache_start = rounddown(idmabase0, ARCH_DMA_MINALIGN); |
| 234 | ctx->cache_end = roundup(idmabase0 + ctx->data_length, |
| 235 | ARCH_DMA_MINALIGN); |
| 236 | |
| 237 | /* |
| 238 | * Flush data cache before DMA start (clean and invalidate) |
| 239 | * Clean also needed for read |
| 240 | * Avoid issue on buffer not cached-aligned |
| 241 | */ |
| 242 | flush_dcache_range(ctx->cache_start, ctx->cache_end); |
| 243 | |
| 244 | /* Enable internal DMA */ |
| 245 | writel(idmabase0, priv->base + SDMMC_IDMABASE0); |
| 246 | writel(SDMMC_IDMACTRL_IDMAEN, priv->base + SDMMC_IDMACTRL); |
| 247 | } |
| 248 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 249 | static void stm32_sdmmc2_start_cmd(struct udevice *dev, |
Christophe Kerello | 8a0b523 | 2018-12-06 15:58:10 +0100 | [diff] [blame] | 250 | struct mmc_cmd *cmd, u32 cmd_param, |
| 251 | struct stm32_sdmmc2_ctx *ctx) |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 252 | { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 253 | struct stm32_sdmmc2_priv *priv = dev_get_priv(dev); |
Christophe Kerello | 8a0b523 | 2018-12-06 15:58:10 +0100 | [diff] [blame] | 254 | u32 timeout = 0; |
| 255 | |
Patrice Chotard | 85b9f63 | 2018-05-17 16:53:57 +0200 | [diff] [blame] | 256 | if (readl(priv->base + SDMMC_CMD) & SDMMC_CMD_CPSMEN) |
| 257 | writel(0, priv->base + SDMMC_CMD); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 258 | |
| 259 | cmd_param |= cmd->cmdidx | SDMMC_CMD_CPSMEN; |
| 260 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 261 | if (cmd->resp_type & MMC_RSP_136) |
| 262 | cmd_param |= SDMMC_CMD_WAITRESP; |
| 263 | else if (cmd->resp_type & MMC_RSP_CRC) |
| 264 | cmd_param |= SDMMC_CMD_WAITRESP_0; |
| 265 | else |
| 266 | cmd_param |= SDMMC_CMD_WAITRESP_1; |
| 267 | } |
| 268 | |
Christophe Kerello | 8a0b523 | 2018-12-06 15:58:10 +0100 | [diff] [blame] | 269 | /* |
| 270 | * SDMMC_DTIME must be set in two case: |
| 271 | * - on data transfert. |
| 272 | * - on busy request. |
| 273 | * If not done or too short, the dtimeout flag occurs and DPSM stays |
| 274 | * enabled/busy and waits for abort (stop transmission cmd). |
| 275 | * Next data command is not possible whereas DPSM is activated. |
| 276 | */ |
| 277 | if (ctx->data_length) { |
| 278 | timeout = SDMMC_CMD_TIMEOUT; |
| 279 | } else { |
| 280 | writel(0, priv->base + SDMMC_DCTRL); |
| 281 | |
| 282 | if (cmd->resp_type & MMC_RSP_BUSY) |
| 283 | timeout = SDMMC_CMD_TIMEOUT; |
| 284 | } |
| 285 | |
| 286 | /* Set the SDMMC Data TimeOut value */ |
| 287 | writel(timeout, priv->base + SDMMC_DTIMER); |
| 288 | |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 289 | /* Clear flags */ |
| 290 | writel(SDMMC_ICR_STATIC_FLAGS, priv->base + SDMMC_ICR); |
| 291 | |
| 292 | /* Set SDMMC argument value */ |
| 293 | writel(cmd->cmdarg, priv->base + SDMMC_ARG); |
| 294 | |
| 295 | /* Set SDMMC command parameters */ |
| 296 | writel(cmd_param, priv->base + SDMMC_CMD); |
| 297 | } |
| 298 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 299 | static int stm32_sdmmc2_end_cmd(struct udevice *dev, |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 300 | struct mmc_cmd *cmd, |
| 301 | struct stm32_sdmmc2_ctx *ctx) |
| 302 | { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 303 | struct stm32_sdmmc2_priv *priv = dev_get_priv(dev); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 304 | u32 mask = SDMMC_STA_CTIMEOUT; |
| 305 | u32 status; |
| 306 | int ret; |
| 307 | |
| 308 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 309 | mask |= SDMMC_STA_CMDREND; |
| 310 | if (cmd->resp_type & MMC_RSP_CRC) |
| 311 | mask |= SDMMC_STA_CCRCFAIL; |
| 312 | } else { |
| 313 | mask |= SDMMC_STA_CMDSENT; |
| 314 | } |
| 315 | |
| 316 | /* Polling status register */ |
| 317 | ret = readl_poll_timeout(priv->base + SDMMC_STA, status, status & mask, |
Christophe Kerello | c9ab62c | 2017-10-09 17:02:28 +0200 | [diff] [blame] | 318 | 10000); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 319 | |
| 320 | if (ret < 0) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 321 | dev_dbg(dev, "timeout reading SDMMC_STA register\n"); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 322 | ctx->dpsm_abort = true; |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | /* Check status */ |
| 327 | if (status & SDMMC_STA_CTIMEOUT) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 328 | dev_dbg(dev, "error SDMMC_STA_CTIMEOUT (0x%x) for cmd %d\n", |
| 329 | status, cmd->cmdidx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 330 | ctx->dpsm_abort = true; |
| 331 | return -ETIMEDOUT; |
| 332 | } |
| 333 | |
| 334 | if (status & SDMMC_STA_CCRCFAIL && cmd->resp_type & MMC_RSP_CRC) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 335 | dev_dbg(dev, "error SDMMC_STA_CCRCFAIL (0x%x) for cmd %d\n", |
| 336 | status, cmd->cmdidx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 337 | ctx->dpsm_abort = true; |
| 338 | return -EILSEQ; |
| 339 | } |
| 340 | |
| 341 | if (status & SDMMC_STA_CMDREND && cmd->resp_type & MMC_RSP_PRESENT) { |
| 342 | cmd->response[0] = readl(priv->base + SDMMC_RESP1); |
| 343 | if (cmd->resp_type & MMC_RSP_136) { |
| 344 | cmd->response[1] = readl(priv->base + SDMMC_RESP2); |
| 345 | cmd->response[2] = readl(priv->base + SDMMC_RESP3); |
| 346 | cmd->response[3] = readl(priv->base + SDMMC_RESP4); |
| 347 | } |
Christophe Kerello | 8a0b523 | 2018-12-06 15:58:10 +0100 | [diff] [blame] | 348 | |
| 349 | /* Wait for BUSYD0END flag if busy status is detected */ |
| 350 | if (cmd->resp_type & MMC_RSP_BUSY && |
| 351 | status & SDMMC_STA_BUSYD0) { |
| 352 | mask = SDMMC_STA_DTIMEOUT | SDMMC_STA_BUSYD0END; |
| 353 | |
| 354 | /* Polling status register */ |
| 355 | ret = readl_poll_timeout(priv->base + SDMMC_STA, |
| 356 | status, status & mask, |
| 357 | SDMMC_BUSYD0END_TIMEOUT_US); |
| 358 | |
| 359 | if (ret < 0) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 360 | dev_dbg(dev, "timeout reading SDMMC_STA\n"); |
Christophe Kerello | 8a0b523 | 2018-12-06 15:58:10 +0100 | [diff] [blame] | 361 | ctx->dpsm_abort = true; |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | if (status & SDMMC_STA_DTIMEOUT) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 366 | dev_dbg(dev, |
| 367 | "error SDMMC_STA_DTIMEOUT (0x%x)\n", |
| 368 | status); |
Christophe Kerello | 8a0b523 | 2018-12-06 15:58:10 +0100 | [diff] [blame] | 369 | ctx->dpsm_abort = true; |
| 370 | return -ETIMEDOUT; |
| 371 | } |
| 372 | } |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 378 | static int stm32_sdmmc2_end_data(struct udevice *dev, |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 379 | struct mmc_cmd *cmd, |
| 380 | struct mmc_data *data, |
| 381 | struct stm32_sdmmc2_ctx *ctx) |
| 382 | { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 383 | struct stm32_sdmmc2_priv *priv = dev_get_priv(dev); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 384 | u32 mask = SDMMC_STA_DCRCFAIL | SDMMC_STA_DTIMEOUT | |
| 385 | SDMMC_STA_IDMATE | SDMMC_STA_DATAEND; |
| 386 | u32 status; |
| 387 | |
| 388 | if (data->flags & MMC_DATA_READ) |
| 389 | mask |= SDMMC_STA_RXOVERR; |
| 390 | else |
| 391 | mask |= SDMMC_STA_TXUNDERR; |
| 392 | |
| 393 | status = readl(priv->base + SDMMC_STA); |
| 394 | while (!(status & mask)) |
| 395 | status = readl(priv->base + SDMMC_STA); |
| 396 | |
| 397 | /* |
| 398 | * Need invalidate the dcache again to avoid any |
| 399 | * cache-refill during the DMA operations (pre-fetching) |
| 400 | */ |
| 401 | if (data->flags & MMC_DATA_READ) |
| 402 | invalidate_dcache_range(ctx->cache_start, ctx->cache_end); |
| 403 | |
| 404 | if (status & SDMMC_STA_DCRCFAIL) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 405 | dev_dbg(dev, "error SDMMC_STA_DCRCFAIL (0x%x) for cmd %d\n", |
| 406 | status, cmd->cmdidx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 407 | if (readl(priv->base + SDMMC_DCOUNT)) |
| 408 | ctx->dpsm_abort = true; |
| 409 | return -EILSEQ; |
| 410 | } |
| 411 | |
| 412 | if (status & SDMMC_STA_DTIMEOUT) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 413 | dev_dbg(dev, "error SDMMC_STA_DTIMEOUT (0x%x) for cmd %d\n", |
| 414 | status, cmd->cmdidx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 415 | ctx->dpsm_abort = true; |
| 416 | return -ETIMEDOUT; |
| 417 | } |
| 418 | |
| 419 | if (status & SDMMC_STA_TXUNDERR) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 420 | dev_dbg(dev, "error SDMMC_STA_TXUNDERR (0x%x) for cmd %d\n", |
| 421 | status, cmd->cmdidx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 422 | ctx->dpsm_abort = true; |
| 423 | return -EIO; |
| 424 | } |
| 425 | |
| 426 | if (status & SDMMC_STA_RXOVERR) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 427 | dev_dbg(dev, "error SDMMC_STA_RXOVERR (0x%x) for cmd %d\n", |
| 428 | status, cmd->cmdidx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 429 | ctx->dpsm_abort = true; |
| 430 | return -EIO; |
| 431 | } |
| 432 | |
| 433 | if (status & SDMMC_STA_IDMATE) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 434 | dev_dbg(dev, "error SDMMC_STA_IDMATE (0x%x) for cmd %d\n", |
| 435 | status, cmd->cmdidx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 436 | ctx->dpsm_abort = true; |
| 437 | return -EIO; |
| 438 | } |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int stm32_sdmmc2_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 444 | struct mmc_data *data) |
| 445 | { |
| 446 | struct stm32_sdmmc2_priv *priv = dev_get_priv(dev); |
| 447 | struct stm32_sdmmc2_ctx ctx; |
| 448 | u32 cmdat = data ? SDMMC_CMD_CMDTRANS : 0; |
| 449 | int ret, retry = 3; |
| 450 | |
Christophe Kerello | e0452e8 | 2019-07-30 19:16:45 +0200 | [diff] [blame] | 451 | WATCHDOG_RESET(); |
| 452 | |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 453 | retry_cmd: |
| 454 | ctx.data_length = 0; |
| 455 | ctx.dpsm_abort = false; |
| 456 | |
| 457 | if (data) { |
| 458 | ctx.data_length = data->blocks * data->blocksize; |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 459 | stm32_sdmmc2_start_data(dev, data, &ctx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 462 | stm32_sdmmc2_start_cmd(dev, cmd, cmdat, &ctx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 463 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 464 | dev_dbg(dev, "send cmd %d data: 0x%x @ 0x%x\n", |
| 465 | cmd->cmdidx, data ? ctx.data_length : 0, (unsigned int)data); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 466 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 467 | ret = stm32_sdmmc2_end_cmd(dev, cmd, &ctx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 468 | |
| 469 | if (data && !ret) |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 470 | ret = stm32_sdmmc2_end_data(dev, cmd, data, &ctx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 471 | |
| 472 | /* Clear flags */ |
| 473 | writel(SDMMC_ICR_STATIC_FLAGS, priv->base + SDMMC_ICR); |
| 474 | if (data) |
| 475 | writel(0x0, priv->base + SDMMC_IDMACTRL); |
| 476 | |
| 477 | /* |
| 478 | * To stop Data Path State Machine, a stop_transmission command |
| 479 | * shall be send on cmd or data errors. |
| 480 | */ |
| 481 | if (ctx.dpsm_abort && (cmd->cmdidx != MMC_CMD_STOP_TRANSMISSION)) { |
| 482 | struct mmc_cmd stop_cmd; |
| 483 | |
| 484 | stop_cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION; |
| 485 | stop_cmd.cmdarg = 0; |
| 486 | stop_cmd.resp_type = MMC_RSP_R1b; |
| 487 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 488 | dev_dbg(dev, "send STOP command to abort dpsm treatments\n"); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 489 | |
Christophe Kerello | 8a0b523 | 2018-12-06 15:58:10 +0100 | [diff] [blame] | 490 | ctx.data_length = 0; |
| 491 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 492 | stm32_sdmmc2_start_cmd(dev, &stop_cmd, |
Christophe Kerello | 8a0b523 | 2018-12-06 15:58:10 +0100 | [diff] [blame] | 493 | SDMMC_CMD_CMDSTOP, &ctx); |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 494 | stm32_sdmmc2_end_cmd(dev, &stop_cmd, &ctx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 495 | |
| 496 | writel(SDMMC_ICR_STATIC_FLAGS, priv->base + SDMMC_ICR); |
| 497 | } |
| 498 | |
| 499 | if ((ret != -ETIMEDOUT) && (ret != 0) && retry) { |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 500 | dev_err(dev, "cmd %d failed, retrying ...\n", cmd->cmdidx); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 501 | retry--; |
| 502 | goto retry_cmd; |
| 503 | } |
| 504 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 505 | dev_dbg(dev, "end for CMD %d, ret = %d\n", cmd->cmdidx, ret); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 506 | |
| 507 | return ret; |
| 508 | } |
| 509 | |
Patrick Delaunay | 6935108 | 2018-06-27 10:15:33 +0200 | [diff] [blame] | 510 | /* |
| 511 | * Reset the SDMMC with the RCC.SDMMCxRST register bit. |
| 512 | * This will reset the SDMMC to the reset state and the CPSM and DPSM |
| 513 | * to the Idle state. SDMMC is disabled, Signals Hiz. |
| 514 | */ |
| 515 | static void stm32_sdmmc2_reset(struct stm32_sdmmc2_priv *priv) |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 516 | { |
| 517 | /* Reset */ |
| 518 | reset_assert(&priv->reset_ctl); |
| 519 | udelay(2); |
| 520 | reset_deassert(&priv->reset_ctl); |
| 521 | |
Patrick Delaunay | 6935108 | 2018-06-27 10:15:33 +0200 | [diff] [blame] | 522 | /* init the needed SDMMC register after reset */ |
| 523 | writel(priv->pwr_reg_msk, priv->base + SDMMC_POWER); |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Set the SDMMC in power-cycle state. |
| 528 | * This will make that the SDMMC_D[7:0], |
| 529 | * SDMMC_CMD and SDMMC_CK are driven low, to prevent the card from being |
| 530 | * supplied through the signal lines. |
| 531 | */ |
| 532 | static void stm32_sdmmc2_pwrcycle(struct stm32_sdmmc2_priv *priv) |
| 533 | { |
| 534 | if ((readl(priv->base + SDMMC_POWER) & SDMMC_POWER_PWRCTRL_MASK) == |
| 535 | SDMMC_POWER_PWRCTRL_CYCLE) |
| 536 | return; |
| 537 | |
| 538 | stm32_sdmmc2_reset(priv); |
Patrick Delaunay | 6935108 | 2018-06-27 10:15:33 +0200 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /* |
| 542 | * set the SDMMC state Power-on: the card is clocked |
| 543 | * manage the SDMMC state control: |
| 544 | * Reset => Power-Cycle => Power-Off => Power |
| 545 | * PWRCTRL=10 PWCTRL=00 PWCTRL=11 |
| 546 | */ |
| 547 | static void stm32_sdmmc2_pwron(struct stm32_sdmmc2_priv *priv) |
| 548 | { |
| 549 | u32 pwrctrl = |
| 550 | readl(priv->base + SDMMC_POWER) & SDMMC_POWER_PWRCTRL_MASK; |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 551 | |
Patrick Delaunay | 6935108 | 2018-06-27 10:15:33 +0200 | [diff] [blame] | 552 | if (pwrctrl == SDMMC_POWER_PWRCTRL_ON) |
| 553 | return; |
| 554 | |
| 555 | /* warning: same PWRCTRL value after reset and for power-off state |
| 556 | * it is the reset state here = the only managed by the driver |
| 557 | */ |
| 558 | if (pwrctrl == SDMMC_POWER_PWRCTRL_OFF) { |
| 559 | writel(SDMMC_POWER_PWRCTRL_CYCLE | priv->pwr_reg_msk, |
| 560 | priv->base + SDMMC_POWER); |
| 561 | } |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 562 | |
| 563 | /* |
Patrick Delaunay | 6935108 | 2018-06-27 10:15:33 +0200 | [diff] [blame] | 564 | * the remaining case is SDMMC_POWER_PWRCTRL_CYCLE |
| 565 | * switch to Power-Off state: SDMCC disable, signals drive 1 |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 566 | */ |
Patrick Delaunay | 6935108 | 2018-06-27 10:15:33 +0200 | [diff] [blame] | 567 | writel(SDMMC_POWER_PWRCTRL_OFF | priv->pwr_reg_msk, |
| 568 | priv->base + SDMMC_POWER); |
| 569 | |
| 570 | /* After the 1ms delay set the SDMMC to power-on */ |
| 571 | mdelay(1); |
| 572 | writel(SDMMC_POWER_PWRCTRL_ON | priv->pwr_reg_msk, |
| 573 | priv->base + SDMMC_POWER); |
| 574 | |
| 575 | /* during the first 74 SDMMC_CK cycles the SDMMC is still disabled. */ |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | #define IS_RISING_EDGE(reg) (reg & SDMMC_CLKCR_NEGEDGE ? 0 : 1) |
| 579 | static int stm32_sdmmc2_set_ios(struct udevice *dev) |
| 580 | { |
| 581 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 582 | struct stm32_sdmmc2_priv *priv = dev_get_priv(dev); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 583 | u32 desired = mmc->clock; |
| 584 | u32 sys_clock = clk_get_rate(&priv->clk); |
| 585 | u32 clk = 0; |
| 586 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 587 | dev_dbg(dev, "bus_with = %d, clock = %d\n", |
| 588 | mmc->bus_width, mmc->clock); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 589 | |
Patrick Delaunay | 6935108 | 2018-06-27 10:15:33 +0200 | [diff] [blame] | 590 | if (mmc->clk_disable) |
| 591 | stm32_sdmmc2_pwrcycle(priv); |
| 592 | else |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 593 | stm32_sdmmc2_pwron(priv); |
| 594 | |
| 595 | /* |
| 596 | * clk_div = 0 => command and data generated on SDMMCCLK falling edge |
| 597 | * clk_div > 0 and NEGEDGE = 0 => command and data generated on |
| 598 | * SDMMCCLK rising edge |
| 599 | * clk_div > 0 and NEGEDGE = 1 => command and data generated on |
| 600 | * SDMMCCLK falling edge |
| 601 | */ |
| 602 | if (desired && ((sys_clock > desired) || |
| 603 | IS_RISING_EDGE(priv->clk_reg_msk))) { |
| 604 | clk = DIV_ROUND_UP(sys_clock, 2 * desired); |
| 605 | if (clk > SDMMC_CLKCR_CLKDIV_MAX) |
| 606 | clk = SDMMC_CLKCR_CLKDIV_MAX; |
| 607 | } |
| 608 | |
| 609 | if (mmc->bus_width == 4) |
| 610 | clk |= SDMMC_CLKCR_WIDBUS_4; |
| 611 | if (mmc->bus_width == 8) |
| 612 | clk |= SDMMC_CLKCR_WIDBUS_8; |
| 613 | |
Patrick Delaunay | 6eae43c | 2018-02-07 17:19:58 +0100 | [diff] [blame] | 614 | writel(clk | priv->clk_reg_msk | SDMMC_CLKCR_HWFC_EN, |
| 615 | priv->base + SDMMC_CLKCR); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static int stm32_sdmmc2_getcd(struct udevice *dev) |
| 621 | { |
| 622 | struct stm32_sdmmc2_priv *priv = dev_get_priv(dev); |
| 623 | |
Patrick Delaunay | 758de43 | 2020-11-06 19:01:37 +0100 | [diff] [blame] | 624 | dev_dbg(dev, "%s called\n", __func__); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 625 | |
| 626 | if (dm_gpio_is_valid(&priv->cd_gpio)) |
| 627 | return dm_gpio_get_value(&priv->cd_gpio); |
| 628 | |
| 629 | return 1; |
| 630 | } |
| 631 | |
Yann Gautier | ae3cd2a | 2019-09-19 17:56:13 +0200 | [diff] [blame] | 632 | static int stm32_sdmmc2_host_power_cycle(struct udevice *dev) |
| 633 | { |
| 634 | struct stm32_sdmmc2_priv *priv = dev_get_priv(dev); |
| 635 | |
| 636 | writel(SDMMC_POWER_PWRCTRL_CYCLE | priv->pwr_reg_msk, |
| 637 | priv->base + SDMMC_POWER); |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 642 | static const struct dm_mmc_ops stm32_sdmmc2_ops = { |
| 643 | .send_cmd = stm32_sdmmc2_send_cmd, |
| 644 | .set_ios = stm32_sdmmc2_set_ios, |
| 645 | .get_cd = stm32_sdmmc2_getcd, |
Yann Gautier | ae3cd2a | 2019-09-19 17:56:13 +0200 | [diff] [blame] | 646 | .host_power_cycle = stm32_sdmmc2_host_power_cycle, |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 647 | }; |
| 648 | |
Marek Vasut | 98a1205 | 2021-11-13 03:29:43 +0100 | [diff] [blame] | 649 | static int stm32_sdmmc2_probe_level_translator(struct udevice *dev) |
| 650 | { |
| 651 | struct stm32_sdmmc2_priv *priv = dev_get_priv(dev); |
| 652 | struct gpio_desc cmd_gpio; |
| 653 | struct gpio_desc ck_gpio; |
| 654 | struct gpio_desc ckin_gpio; |
| 655 | int clk_hi, clk_lo, ret; |
| 656 | |
| 657 | /* |
| 658 | * Assume the level translator is present if st,use-ckin is set. |
| 659 | * This is to cater for DTs which do not implement this test. |
| 660 | */ |
| 661 | priv->clk_reg_msk |= SDMMC_CLKCR_SELCLKRX_CKIN; |
| 662 | |
| 663 | ret = gpio_request_by_name(dev, "st,cmd-gpios", 0, &cmd_gpio, |
| 664 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
| 665 | if (ret) |
| 666 | goto exit_cmd; |
| 667 | |
| 668 | ret = gpio_request_by_name(dev, "st,ck-gpios", 0, &ck_gpio, |
| 669 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); |
| 670 | if (ret) |
| 671 | goto exit_ck; |
| 672 | |
| 673 | ret = gpio_request_by_name(dev, "st,ckin-gpios", 0, &ckin_gpio, |
| 674 | GPIOD_IS_IN); |
| 675 | if (ret) |
| 676 | goto exit_ckin; |
| 677 | |
| 678 | /* All GPIOs are valid, test whether level translator works */ |
| 679 | |
| 680 | /* Sample CKIN */ |
| 681 | clk_hi = !!dm_gpio_get_value(&ckin_gpio); |
| 682 | |
| 683 | /* Set CK low */ |
| 684 | dm_gpio_set_value(&ck_gpio, 0); |
| 685 | |
| 686 | /* Sample CKIN */ |
| 687 | clk_lo = !!dm_gpio_get_value(&ckin_gpio); |
| 688 | |
| 689 | /* Tristate all */ |
| 690 | dm_gpio_set_dir_flags(&cmd_gpio, GPIOD_IS_IN); |
| 691 | dm_gpio_set_dir_flags(&ck_gpio, GPIOD_IS_IN); |
| 692 | |
| 693 | /* Level translator is present if CK signal is propagated to CKIN */ |
| 694 | if (!clk_hi || clk_lo) |
| 695 | priv->clk_reg_msk &= ~SDMMC_CLKCR_SELCLKRX_CKIN; |
| 696 | |
| 697 | dm_gpio_free(dev, &ckin_gpio); |
| 698 | |
| 699 | exit_ckin: |
| 700 | dm_gpio_free(dev, &ck_gpio); |
| 701 | exit_ck: |
| 702 | dm_gpio_free(dev, &cmd_gpio); |
| 703 | exit_cmd: |
| 704 | pinctrl_select_state(dev, "default"); |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 709 | static int stm32_sdmmc2_probe(struct udevice *dev) |
| 710 | { |
| 711 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 712 | struct stm32_sdmmc2_plat *plat = dev_get_plat(dev); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 713 | struct stm32_sdmmc2_priv *priv = dev_get_priv(dev); |
| 714 | struct mmc_config *cfg = &plat->cfg; |
| 715 | int ret; |
| 716 | |
| 717 | priv->base = dev_read_addr(dev); |
| 718 | if (priv->base == FDT_ADDR_T_NONE) |
| 719 | return -EINVAL; |
| 720 | |
Patrick Delaunay | 920e89f | 2018-11-16 10:25:54 +0100 | [diff] [blame] | 721 | if (dev_read_bool(dev, "st,neg-edge")) |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 722 | priv->clk_reg_msk |= SDMMC_CLKCR_NEGEDGE; |
Patrick Delaunay | 920e89f | 2018-11-16 10:25:54 +0100 | [diff] [blame] | 723 | if (dev_read_bool(dev, "st,sig-dir")) |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 724 | priv->pwr_reg_msk |= SDMMC_POWER_DIRPOL; |
Patrick Delaunay | 920e89f | 2018-11-16 10:25:54 +0100 | [diff] [blame] | 725 | if (dev_read_bool(dev, "st,use-ckin")) |
Marek Vasut | 98a1205 | 2021-11-13 03:29:43 +0100 | [diff] [blame] | 726 | stm32_sdmmc2_probe_level_translator(dev); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 727 | |
| 728 | ret = clk_get_by_index(dev, 0, &priv->clk); |
| 729 | if (ret) |
| 730 | return ret; |
| 731 | |
| 732 | ret = clk_enable(&priv->clk); |
| 733 | if (ret) |
| 734 | goto clk_free; |
| 735 | |
| 736 | ret = reset_get_by_index(dev, 0, &priv->reset_ctl); |
| 737 | if (ret) |
| 738 | goto clk_disable; |
| 739 | |
| 740 | gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, |
| 741 | GPIOD_IS_IN); |
| 742 | |
| 743 | cfg->f_min = 400000; |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 744 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195; |
| 745 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Patrick Delaunay | c3d0780 | 2020-04-30 09:52:13 +0200 | [diff] [blame] | 746 | cfg->name = "STM32 SD/MMC"; |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 747 | |
| 748 | cfg->host_caps = 0; |
Alexandru Gagniuc | 643e31b | 2020-09-09 16:54:02 -0500 | [diff] [blame] | 749 | cfg->f_max = 52000000; |
| 750 | mmc_of_parse(dev, cfg); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 751 | |
| 752 | upriv->mmc = &plat->mmc; |
| 753 | |
Patrick Delaunay | 6935108 | 2018-06-27 10:15:33 +0200 | [diff] [blame] | 754 | /* SDMMC init */ |
| 755 | stm32_sdmmc2_reset(priv); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 756 | return 0; |
| 757 | |
| 758 | clk_disable: |
| 759 | clk_disable(&priv->clk); |
| 760 | clk_free: |
| 761 | clk_free(&priv->clk); |
| 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
Patrick Delaunay | 2961b21 | 2019-06-21 15:26:42 +0200 | [diff] [blame] | 766 | static int stm32_sdmmc_bind(struct udevice *dev) |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 767 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 768 | struct stm32_sdmmc2_plat *plat = dev_get_plat(dev); |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 769 | |
| 770 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 771 | } |
| 772 | |
| 773 | static const struct udevice_id stm32_sdmmc2_ids[] = { |
| 774 | { .compatible = "st,stm32-sdmmc2" }, |
| 775 | { } |
| 776 | }; |
| 777 | |
| 778 | U_BOOT_DRIVER(stm32_sdmmc2) = { |
| 779 | .name = "stm32_sdmmc2", |
| 780 | .id = UCLASS_MMC, |
| 781 | .of_match = stm32_sdmmc2_ids, |
| 782 | .ops = &stm32_sdmmc2_ops, |
| 783 | .probe = stm32_sdmmc2_probe, |
| 784 | .bind = stm32_sdmmc_bind, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 785 | .priv_auto = sizeof(struct stm32_sdmmc2_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 786 | .plat_auto = sizeof(struct stm32_sdmmc2_plat), |
Patrice Chotard | ae19b81 | 2017-09-04 17:56:22 +0200 | [diff] [blame] | 787 | }; |