Skip to content

Commit 76de1af

Browse files
committed
drivers: sdmmc_stm32: Fix bus width initialization sequence
Fix SDMMC initialization by starting with 1-bit bus mode and properly configuring wide bus operation after HAL initialization. The SDMMC protocol requires initialization to start in 1-bit mode before switching to wider bus widths. Previously, the driver attempted to initialize directly with the target bus width, which could cause later read/write failures. Changes: - Initialize with SDMMC_BUS_WIDE_1B instead of target bus width - Add HAL_SD_ConfigWideBusOperation() call if needed after successful init - Add error logging for wide bus configuration failures Fixes potential SDMMC read/write failures issues on STM32 platforms. Signed-off-by: Shan Pen <[email protected]>
1 parent 5945a3f commit 76de1af

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

drivers/disk/sdmmc_stm32.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ LOG_MODULE_REGISTER(stm32_sdmmc, CONFIG_SDMMC_LOG_LEVEL);
4747
#define SDMMC_BUS_WIDE_8B SDIO_BUS_WIDE_8B
4848
#endif
4949

50+
#if DT_INST_PROP(0, bus_width) == 1
51+
#define SDMMC_BUS_WIDTH SDMMC_BUS_WIDE_1B
52+
#elif DT_INST_PROP(0, bus_width) == 4
53+
#define SDMMC_BUS_WIDTH SDMMC_BUS_WIDE_4B
54+
#elif DT_INST_PROP(0, bus_width) == 8
55+
#define SDMMC_BUS_WIDTH SDMMC_BUS_WIDE_8B
56+
#endif /* DT_INST_PROP(0, bus_width) */
57+
5058
typedef void (*irq_config_func_t)(const struct device *dev);
5159

5260
#if STM32_SDMMC_USE_DMA
@@ -365,7 +373,15 @@ static int stm32_sdmmc_access_init(struct disk_info *disk)
365373
LOG_ERR("failed to init stm32_sdmmc (ErrorCode 0x%X)", priv->hsd.ErrorCode);
366374
return -EIO;
367375
}
368-
376+
if (SDMMC_BUS_WIDTH != SDMMC_BUS_WIDE_1B) {
377+
priv->hsd.Init.BusWide = SDMMC_BUS_WIDTH;
378+
err = HAL_SD_ConfigWideBusOperation(&priv->hsd, priv->hsd.Init.BusWide);
379+
if (err != HAL_OK) {
380+
LOG_ERR("failed to configure wide bus operation(ErrorCode 0x%X)",
381+
priv->hsd.ErrorCode);
382+
return -EIO;
383+
}
384+
}
369385
#ifdef CONFIG_SDMMC_STM32_HWFC
370386
stm32_sdmmc_fc_enable(priv);
371387
#endif
@@ -866,14 +882,6 @@ static void stm32_sdmmc_irq_config_func(const struct device *dev)
866882
irq_enable(DT_INST_IRQN(0));
867883
}
868884

869-
#if DT_INST_PROP(0, bus_width) == 1
870-
#define SDMMC_BUS_WIDTH SDMMC_BUS_WIDE_1B
871-
#elif DT_INST_PROP(0, bus_width) == 4
872-
#define SDMMC_BUS_WIDTH SDMMC_BUS_WIDE_4B
873-
#elif DT_INST_PROP(0, bus_width) == 8
874-
#define SDMMC_BUS_WIDTH SDMMC_BUS_WIDE_8B
875-
#endif /* DT_INST_PROP(0, bus_width) */
876-
877885
static struct stm32_pclken pclken_sdmmc[] = STM32_DT_INST_CLOCKS(0);
878886

879887
static struct stm32_sdmmc_priv stm32_sdmmc_priv_1 = {
@@ -887,7 +895,7 @@ static struct stm32_sdmmc_priv stm32_sdmmc_priv_1 = {
887895
: SDMMC_CLOCK_BYPASS_DISABLE,
888896
#endif
889897
.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE,
890-
.Init.BusWide = SDMMC_BUS_WIDTH,
898+
.Init.BusWide = SDMMC_BUS_WIDE_1B,
891899
.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE,
892900
.Init.ClockDiv = DT_INST_PROP_OR(0, clk_div, 0),
893901
},

0 commit comments

Comments
 (0)