diff --git a/drivers/disk/sdmmc_stm32.c b/drivers/disk/sdmmc_stm32.c index 0fc94d1e2e7d5..38ae227de6117 100644 --- a/drivers/disk/sdmmc_stm32.c +++ b/drivers/disk/sdmmc_stm32.c @@ -300,7 +300,9 @@ static int stm32_sdmmc_dma_init(struct stm32_sdmmc_priv *priv) return err; } __HAL_LINKDMA(&priv->hsd, hdmatx, priv->dma_tx_handle); - HAL_DMA_Init(&priv->dma_tx_handle); + if (HAL_DMA_Init(&priv->dma_tx_handle) != HAL_OK) { + return -EIO; + } err = stm32_sdmmc_configure_dma(&priv->dma_rx_handle, &priv->dma_rx); if (err) { @@ -308,7 +310,9 @@ static int stm32_sdmmc_dma_init(struct stm32_sdmmc_priv *priv) return err; } __HAL_LINKDMA(&priv->hsd, hdmarx, priv->dma_rx_handle); - HAL_DMA_Init(&priv->dma_rx_handle); + if (HAL_DMA_Init(&priv->dma_rx_handle) != HAL_OK) { + return -EIO; + } #endif /* STM32_SDMMC_USE_DMA_SHARED */ return err; @@ -332,14 +336,17 @@ static int stm32_sdmmc_dma_deinit(struct stm32_sdmmc_priv *priv) #else struct sdmmc_dma_stream *dma_tx = &priv->dma_tx; struct sdmmc_dma_stream *dma_rx = &priv->dma_rx; + HAL_StatusTypeDef __maybe_unused hal_ret; ret = dma_stop(dma_tx->dev, dma_tx->channel); __ASSERT(ret == 0, "TX DMA channel index corrupted"); - HAL_DMA_DeInit(&priv->dma_tx_handle); + hal_ret = HAL_DMA_DeInit(&priv->dma_tx_handle); + __ASSERT_NO_MSG(hal_ret == HAL_OK); ret = dma_stop(dma_rx->dev, dma_rx->channel); __ASSERT(ret == 0, "RX DMA channel index corrupted"); - HAL_DMA_DeInit(&priv->dma_rx_handle); + hal_ret = HAL_DMA_DeInit(&priv->dma_rx_handle); + __ASSERT_NO_MSG(hal_ret == HAL_OK); #endif return 0; } @@ -361,6 +368,7 @@ static int stm32_sdmmc_access_init(struct disk_info *disk) { const struct device *dev = disk->dev; struct stm32_sdmmc_priv *priv = dev->data; + HAL_StatusTypeDef hal_ret; int err; err = stm32_sdmmc_pwr_on(priv); @@ -412,11 +420,11 @@ static int stm32_sdmmc_access_init(struct disk_info *disk) } #ifdef CONFIG_SDMMC_STM32_EMMC - err = HAL_MMC_Init(&priv->hsd); + hal_ret = HAL_MMC_Init(&priv->hsd); #else - err = HAL_SD_Init(&priv->hsd); + hal_ret = HAL_SD_Init(&priv->hsd); #endif - if (err != HAL_OK) { + if (hal_ret != HAL_OK) { LOG_ERR("failed to init stm32_sdmmc (ErrorCode 0x%X)", priv->hsd.ErrorCode); err = -EIO; goto error; @@ -424,8 +432,8 @@ static int stm32_sdmmc_access_init(struct disk_info *disk) if (SDMMC_BUS_WIDTH != SDMMC_BUS_WIDE_1B) { priv->hsd.Init.BusWide = SDMMC_BUS_WIDTH; - err = HAL_SD_ConfigWideBusOperation(&priv->hsd, priv->hsd.Init.BusWide); - if (err != HAL_OK) { + hal_ret = HAL_SD_ConfigWideBusOperation(&priv->hsd, priv->hsd.Init.BusWide); + if (hal_ret != HAL_OK) { LOG_ERR("failed to configure wide bus operation (ErrorCode 0x%X)", priv->hsd.ErrorCode); err = -EIO; @@ -449,9 +457,11 @@ static int stm32_sdmmc_access_init(struct disk_info *disk) static int stm32_sdmmc_access_deinit(struct stm32_sdmmc_priv *priv) { - int err = 0; + HAL_StatusTypeDef hal_ret; #if STM32_SDMMC_USE_DMA + int err; + err = stm32_sdmmc_dma_deinit(priv); if (err) { LOG_ERR("DMA deinit failed"); @@ -460,14 +470,14 @@ static int stm32_sdmmc_access_deinit(struct stm32_sdmmc_priv *priv) #endif #if defined(CONFIG_SDMMC_STM32_EMMC) - err = HAL_MMC_DeInit(&priv->hsd); + hal_ret = HAL_MMC_DeInit(&priv->hsd); #else - err = HAL_SD_DeInit(&priv->hsd); + hal_ret = HAL_SD_DeInit(&priv->hsd); stm32_sdmmc_clock_disable(priv); #endif - if (err != HAL_OK) { + if (hal_ret != HAL_OK) { LOG_ERR("failed to deinit stm32_sdmmc (ErrorCode 0x%X)", priv->hsd.ErrorCode); - return err; + return -EIO; } #if !defined(CONFIG_SDMMC_STM32_EMMC) @@ -499,23 +509,32 @@ static int stm32_sdmmc_is_card_in_transfer(HandleTypeDef *hsd) static int stm32_sdmmc_read_blocks(HandleTypeDef *hsd, uint8_t *data_buf, uint32_t start_sector, uint32_t num_sector) { + HAL_StatusTypeDef hal_ret; + #if STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) #ifdef CONFIG_SDMMC_STM32_EMMC - return HAL_MMC_ReadBlocks_DMA(hsd, data_buf, start_sector, num_sector); + hal_ret = HAL_MMC_ReadBlocks_DMA(hsd, data_buf, start_sector, num_sector); #else - return HAL_SD_ReadBlocks_DMA(hsd, data_buf, start_sector, num_sector); + hal_ret = HAL_SD_ReadBlocks_DMA(hsd, data_buf, start_sector, num_sector); #endif -#else +#else /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */ #ifdef CONFIG_SDMMC_STM32_EMMC - return HAL_MMC_ReadBlocks_IT(hsd, data_buf, start_sector, num_sector); + hal_ret = HAL_MMC_ReadBlocks_IT(hsd, data_buf, start_sector, num_sector); #else - return HAL_SD_ReadBlocks_IT(hsd, data_buf, start_sector, num_sector); + hal_ret = HAL_SD_ReadBlocks_IT(hsd, data_buf, start_sector, num_sector); #endif -#endif +#endif /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */ + + if (hal_ret != HAL_OK) { + LOG_ERR("sd read block failed %d", hal_ret); + return -EIO; + } + + return 0; } static int stm32_sdmmc_access_read(struct disk_info *disk, uint8_t *data_buf, @@ -537,16 +556,17 @@ static int stm32_sdmmc_access_read(struct disk_info *disk, uint8_t *data_buf, #endif err = stm32_sdmmc_read_blocks(&priv->hsd, data_buf, start_sector, num_sector); - if (err != HAL_OK) { - LOG_ERR("sd read block failed %d", err); - err = -EIO; + if (err != 0) { goto end; } k_sem_take(&priv->sync, K_FOREVER); #if STM32_SDMMC_USE_DMA_SHARED - HAL_DMA_DeInit(&priv->dma_txrx_handle); + if (HAL_DMA_DeInit(&priv->dma_txrx_handle) != HAL_OK) { + err = -EIO; + goto end; + } #endif if (priv->status != DISK_STATUS_OK) { @@ -567,23 +587,32 @@ static int stm32_sdmmc_write_blocks(HandleTypeDef *hsd, uint8_t *data_buf, uint32_t start_sector, uint32_t num_sector) { + HAL_StatusTypeDef hal_ret; + #if STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) #ifdef CONFIG_SDMMC_STM32_EMMC - return HAL_MMC_WriteBlocks_DMA(hsd, data_buf, start_sector, num_sector); + hal_ret = HAL_MMC_WriteBlocks_DMA(hsd, data_buf, start_sector, num_sector); #else - return HAL_SD_WriteBlocks_DMA(hsd, data_buf, start_sector, num_sector); + hal_ret = HAL_SD_WriteBlocks_DMA(hsd, data_buf, start_sector, num_sector); #endif -#else +#else /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */ #ifdef CONFIG_SDMMC_STM32_EMMC - return HAL_MMC_WriteBlocks_IT(hsd, data_buf, start_sector, num_sector); + hal_ret = HAL_MMC_WriteBlocks_IT(hsd, data_buf, start_sector, num_sector); #else - return HAL_SD_WriteBlocks_IT(hsd, data_buf, start_sector, num_sector); + hal_ret = HAL_SD_WriteBlocks_IT(hsd, data_buf, start_sector, num_sector); #endif -#endif +#endif /* STM32_SDMMC_USE_DMA || IS_ENABLED(DT_PROP(DT_DRV_INST(0), idma)) */ + + if (hal_ret != HAL_OK) { + LOG_ERR("sd write block failed %d", hal_ret); + return -EIO; + } + + return 0; } static int stm32_sdmmc_access_write(struct disk_info *disk, @@ -606,16 +635,18 @@ static int stm32_sdmmc_access_write(struct disk_info *disk, #endif err = stm32_sdmmc_write_blocks(&priv->hsd, (uint8_t *)data_buf, start_sector, num_sector); - if (err != HAL_OK) { - LOG_ERR("sd write block failed %d", err); - err = -EIO; + if (err != 0) { goto end; } k_sem_take(&priv->sync, K_FOREVER); #if STM32_SDMMC_USE_DMA_SHARED - HAL_DMA_DeInit(&priv->dma_txrx_handle); + if (HAL_DMA_DeInit(&priv->dma_txrx_handle) != HAL_OK) { + LOG_ERR("DMA deinit error"); + err = -EIO; + goto end; + } #endif if (priv->status != DISK_STATUS_OK) { @@ -635,9 +666,9 @@ static int stm32_sdmmc_access_write(struct disk_info *disk, static int stm32_sdmmc_get_card_info(HandleTypeDef *hsd, CardInfoTypeDef *info) { #ifdef CONFIG_SDMMC_STM32_EMMC - return HAL_MMC_GetCardInfo(hsd, info); + return (HAL_MMC_GetCardInfo(hsd, info) == HAL_OK) ? 0 : -EIO; #else - return HAL_SD_GetCardInfo(hsd, info); + return (HAL_SD_GetCardInfo(hsd, info) == HAL_OK) ? 0 : -EIO; #endif } @@ -652,15 +683,15 @@ static int stm32_sdmmc_access_ioctl(struct disk_info *disk, uint8_t cmd, switch (cmd) { case DISK_IOCTL_GET_SECTOR_COUNT: err = stm32_sdmmc_get_card_info(&priv->hsd, &info); - if (err != HAL_OK) { - return -EIO; + if (err != 0) { + return err; } *(uint32_t *)buff = info.LogBlockNbr; break; case DISK_IOCTL_GET_SECTOR_SIZE: err = stm32_sdmmc_get_card_info(&priv->hsd, &info); - if (err != HAL_OK) { - return -EIO; + if (err != 0) { + return err; } *(uint32_t *)buff = info.LogBlockSize; break;