This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 4b6968fa201b10c3ea35f25f1427b1016cde9add
Author: raiden00pl <[email protected]>
AuthorDate: Mon Nov 2 19:50:30 2026 +0100

    arch/arm/stm32: Move the STM32H7 SDMMC driver to common.
    
    The SDMMC v2 driver with the internal DMA becomes
    common/stm32/stm32_sdmmc_v2.c behind STM32_HAVE_IP_SDMMC_V2.  The memory
    reachable by the IDMA and the default kernel clock come from the family
    stm32_sdmmc_chip.h.  STM32H5 keeps its own driver for now.
    
    Assisted-by: Claude:claude-opus-5-5
    Signed-off-by: raiden00pl <[email protected]>
---
 Documentation/guides/chip-specific/stm32_ports.rst |   2 +-
 arch/arm/src/common/stm32/CMakeLists.txt           |   4 +
 arch/arm/src/common/stm32/Kconfig.have             |   5 +
 arch/arm/src/common/stm32/Kconfig.sdio             |   6 +-
 arch/arm/src/common/stm32/Make.defs                |   4 +
 .../stm32}/hardware/stm32_sdmmc.h                  |  23 +--
 .../stm32/hardware/stm32_sdmmc_v2.h}               |   8 +-
 .../src/{stm32h7 => common/stm32}/stm32_sdmmc.h    |  13 +-
 .../stm32/stm32_sdmmc_v2.c}                        | 193 ++++++++++-----------
 arch/arm/src/stm32h7/CMakeLists.txt                |   4 -
 arch/arm/src/stm32h7/Kconfig                       |   1 +
 arch/arm/src/stm32h7/Make.defs                     |   4 -
 arch/arm/src/stm32h7/stm32_sdmmc_chip.h            |  80 +++++++++
 13 files changed, 209 insertions(+), 138 deletions(-)

diff --git a/Documentation/guides/chip-specific/stm32_ports.rst 
b/Documentation/guides/chip-specific/stm32_ports.rst
index 33d9009a359..07fe1096eae 100644
--- a/Documentation/guides/chip-specific/stm32_ports.rst
+++ b/Documentation/guides/chip-specific/stm32_ports.rst
@@ -662,7 +662,7 @@ QUADSPI       to be done    
arch/arm/src/stm32h7/stm32_qspi.c
 RCC           to be done    arch/arm/src/stm32h7/stm32_rcc.c       
 RNG           v1            arch/arm/src/common/stm32/stm32_rng_v1.c
 RTC           to be done    arch/arm/src/stm32h7/stm32_rtc.c       
-SDMMC         to be done    arch/arm/src/stm32h7/stm32_sdmmc.c     
+SDMMC         v2            arch/arm/src/common/stm32/stm32_sdmmc_v2.c
 SPI/I2S       to be done    arch/arm/src/stm32h7/stm32_spi.c       
 TIM           v2, H7RS v3   arch/arm/src/common/stm32/stm32_tim.c  
 USART/LPUART  v4            arch/arm/src/stm32h7/stm32_serial.c    
diff --git a/arch/arm/src/common/stm32/CMakeLists.txt 
b/arch/arm/src/common/stm32/CMakeLists.txt
index 228fd525da7..5e15e027345 100644
--- a/arch/arm/src/common/stm32/CMakeLists.txt
+++ b/arch/arm/src/common/stm32/CMakeLists.txt
@@ -111,6 +111,10 @@ if(CONFIG_MDIO_BUS AND CONFIG_STM32_HAVE_IP_ETHMAC_V2)
   endif()
 endif()
 
+if(CONFIG_STM32_SDMMC AND CONFIG_STM32_HAVE_IP_SDMMC_V2)
+  list(APPEND SRCS stm32_sdmmc_v2.c)
+endif()
+
 if(CONFIG_STM32_COMMON_M33)
   list(APPEND SRCS stm32_allocateheap_m33_v1.c stm32_irq_m33_v1.c
        stm32_start_m33_v1.c)
diff --git a/arch/arm/src/common/stm32/Kconfig.have 
b/arch/arm/src/common/stm32/Kconfig.have
index fe1a36e7ffd..b2fe1c96eb2 100644
--- a/arch/arm/src/common/stm32/Kconfig.have
+++ b/arch/arm/src/common/stm32/Kconfig.have
@@ -808,6 +808,11 @@ config STM32_HAVE_SDMMC2
 config STM32_HAVE_IP_SDIO_M3M4_V1
        bool
 
+config STM32_HAVE_IP_SDMMC_V2
+       bool
+       ---help---
+               SDMMC with an internal DMA (IDMA) (STM32H7).
+
 # SECURITY capabilities
 
 config STM32_HAVE_FIREWALL
diff --git a/arch/arm/src/common/stm32/Kconfig.sdio 
b/arch/arm/src/common/stm32/Kconfig.sdio
index 0811d916ba3..f38fc8dd6fb 100644
--- a/arch/arm/src/common/stm32/Kconfig.sdio
+++ b/arch/arm/src/common/stm32/Kconfig.sdio
@@ -56,7 +56,7 @@ config STM32_SDIO_WIDTH_D1_ONLY
 
 config STM32_SDMMC_IDMA
        bool "Support IDMA data transfers"
-       depends on (ARCH_CHIP_STM32H7 || ARCH_CHIP_STM32H5) && STM32_SDMMC
+       depends on (STM32_HAVE_IP_SDMMC_V2 || ARCH_CHIP_STM32H5) && STM32_SDMMC
        default y
        select SDIO_DMA
        ---help---
@@ -67,8 +67,8 @@ config STM32_SDMMC_IDMA_BOUNCE_BUFFER
        depends on STM32_SDMMC_IDMA
        ---help---
                Use an internal, cache-line aligned buffer when a client buffer
-               cannot be accessed directly by the STM32H7 SDMMC IDMA or does 
not
-               meet its alignment requirements.  The internal buffer must be
+               cannot be accessed directly by the SDMMC IDMA or does not meet 
its
+               alignment requirements.  The internal buffer must be
                linked into IDMA-accessible memory, normally AXI SRAM.
 
 config STM32_SDMMC_IDMA_BOUNCE_BLOCKS
diff --git a/arch/arm/src/common/stm32/Make.defs 
b/arch/arm/src/common/stm32/Make.defs
index 665384ab5d1..2ffb0bf53fb 100644
--- a/arch/arm/src/common/stm32/Make.defs
+++ b/arch/arm/src/common/stm32/Make.defs
@@ -122,6 +122,10 @@ CHIP_CSRCS += stm32_mdio_v2.c
 endif
 endif
 
+ifeq ($(CONFIG_STM32_SDMMC)$(CONFIG_STM32_HAVE_IP_SDMMC_V2),yy)
+CHIP_CSRCS += stm32_sdmmc_v2.c
+endif
+
 ifeq ($(CONFIG_STM32_COMMON_M33),y)
 CHIP_CSRCS += stm32_allocateheap_m33_v1.c
 CHIP_CSRCS += stm32_irq_m33_v1.c
diff --git a/arch/arm/src/stm32h7/hardware/stm32_sdmmc.h 
b/arch/arm/src/common/stm32/hardware/stm32_sdmmc.h
similarity index 65%
rename from arch/arm/src/stm32h7/hardware/stm32_sdmmc.h
rename to arch/arm/src/common/stm32/hardware/stm32_sdmmc.h
index b5d9bd01944..71120ffb98c 100644
--- a/arch/arm/src/stm32h7/hardware/stm32_sdmmc.h
+++ b/arch/arm/src/common/stm32/hardware/stm32_sdmmc.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * arch/arm/src/stm32h7/hardware/stm32_sdmmc.h
+ * arch/arm/src/common/stm32/hardware/stm32_sdmmc.h
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,28 +20,19 @@
  *
  ****************************************************************************/
 
-#ifndef __ARCH_ARM_SRC_STM32H7_HARDWARE_STM32_SDMMC_H
-#define __ARCH_ARM_SRC_STM32H7_HARDWARE_STM32_SDMMC_H
+#ifndef __ARCH_ARM_SRC_COMMON_STM32_HARDWARE_STM32_SDMMC_H
+#define __ARCH_ARM_SRC_COMMON_STM32_HARDWARE_STM32_SDMMC_H
 
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
-#include "chip.h"
 
-#if defined(CONFIG_STM32_STM32H7X0XX)
-#  include "stm32h7x3xx_sdmmc.h"
-#elif defined(CONFIG_STM32_STM32H7X3XX)
-#  include "stm32h7x3xx_sdmmc.h"
-#elif defined(CONFIG_STM32_STM32H7B3XX)
-#  include "stm32h7x3xx_sdmmc.h"
-#elif defined(CONFIG_STM32_STM32H7X5XX)
-#  include "stm32h7x3xx_sdmmc.h"
-#elif defined(CONFIG_STM32_STM32H7X7XX)
-#  include "stm32h7x3xx_sdmmc.h"
+#if defined(CONFIG_STM32_HAVE_IP_SDMMC_V2)
+#  include "hardware/stm32_sdmmc_v2.h"
 #else
-#  error "Unsupported STM32 H7 part"
+#  error "Unsupported STM32 SDMMC"
 #endif
 
-#endif /* __ARCH_ARM_SRC_STM32H7_HARDWARE_STM32_SDMMC_H */
+#endif /* __ARCH_ARM_SRC_COMMON_STM32_HARDWARE_STM32_SDMMC_H */
diff --git a/arch/arm/src/stm32h7/hardware/stm32h7x3xx_sdmmc.h 
b/arch/arm/src/common/stm32/hardware/stm32_sdmmc_v2.h
similarity index 98%
rename from arch/arm/src/stm32h7/hardware/stm32h7x3xx_sdmmc.h
rename to arch/arm/src/common/stm32/hardware/stm32_sdmmc_v2.h
index 5d83e72adac..fe42f6f25ef 100644
--- a/arch/arm/src/stm32h7/hardware/stm32h7x3xx_sdmmc.h
+++ b/arch/arm/src/common/stm32/hardware/stm32_sdmmc_v2.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * arch/arm/src/stm32h7/hardware/stm32h7x3xx_sdmmc.h
+ * arch/arm/src/common/stm32/hardware/stm32_sdmmc_v2.h
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,8 +20,8 @@
  *
  ****************************************************************************/
 
-#ifndef __ARCH_ARM_SRC_STM32H7_HARDWARE_STM32H7X3XX_SDMMC_H
-#define __ARCH_ARM_SRC_STM32H7_HARDWARE_STM32H7X3XX_SDMMC_H
+#ifndef __ARCH_ARM_SRC_COMMON_STM32_HARDWARE_STM32_SDMMC_V2_H
+#define __ARCH_ARM_SRC_COMMON_STM32_HARDWARE_STM32_SDMMC_V2_H
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -245,4 +245,4 @@
 #define STM32_SDMMC_FIFOCNT_SHIFT             (0)
 #define STM32_SDMMC_FIFOCNT_MASK              (0x0ffffff << 
STM32_SDMMC_FIFOCNT_SHIFT)
 
-#endif /* __ARCH_ARM_SRC_STM32H7_HARDWARE_STM32H7X3XX_SDMMC_H */
+#endif /* __ARCH_ARM_SRC_COMMON_STM32_HARDWARE_STM32_SDMMC_V2_H */
diff --git a/arch/arm/src/stm32h7/stm32_sdmmc.h 
b/arch/arm/src/common/stm32/stm32_sdmmc.h
similarity index 93%
rename from arch/arm/src/stm32h7/stm32_sdmmc.h
rename to arch/arm/src/common/stm32/stm32_sdmmc.h
index 38aa648589f..da5fa17a9c9 100644
--- a/arch/arm/src/stm32h7/stm32_sdmmc.h
+++ b/arch/arm/src/common/stm32/stm32_sdmmc.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * arch/arm/src/stm32h7/stm32_sdmmc.h
+ * arch/arm/src/common/stm32/stm32_sdmmc.h
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,8 +20,8 @@
  *
  ****************************************************************************/
 
-#ifndef __ARCH_ARM_SRC_STM32H7_STM32_SDMMC_H
-#define __ARCH_ARM_SRC_STM32H7_STM32_SDMMC_H
+#ifndef __ARCH_ARM_SRC_COMMON_STM32_STM32_SDMMC_H
+#define __ARCH_ARM_SRC_COMMON_STM32_STM32_SDMMC_H
 
 /****************************************************************************
  * Included Files
@@ -32,7 +32,10 @@
 #include <stdbool.h>
 
 #include "chip.h"
-#include "hardware/stm32_sdmmc.h"
+
+#ifdef CONFIG_STM32_HAVE_IP_SDMMC_V2
+#  include "hardware/stm32_sdmmc.h"
+#endif
 
 /****************************************************************************
  * Public Function Prototypes
@@ -128,4 +131,4 @@ void sdio_set_sdio_card_isr(struct sdio_dev_s *dev,
 #endif
 
 #endif /* __ASSEMBLY__ */
-#endif /* __ARCH_ARM_SRC_STM32H7_STM32_SDMMC_H */
+#endif /* __ARCH_ARM_SRC_COMMON_STM32_STM32_SDMMC_H */
diff --git a/arch/arm/src/stm32h7/stm32_sdmmc.c 
b/arch/arm/src/common/stm32/stm32_sdmmc_v2.c
similarity index 96%
rename from arch/arm/src/stm32h7/stm32_sdmmc.c
rename to arch/arm/src/common/stm32/stm32_sdmmc_v2.c
index a258fa79102..61cc2f71edf 100644
--- a/arch/arm/src/stm32h7/stm32_sdmmc.c
+++ b/arch/arm/src/common/stm32/stm32_sdmmc_v2.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * arch/arm/src/stm32h7/stm32_sdmmc.c
+ * arch/arm/src/common/stm32/stm32_sdmmc_v2.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -50,11 +50,9 @@
 
 #include "chip.h"
 #include "arm_internal.h"
-#include "stm32_dtcm.h"
-#include "stm32_dma.h"
-#include "stm32_gpio.h"
-#include "stm32_rcc.h"
+#include "stm32.h"
 #include "stm32_sdmmc.h"
+#include "stm32_sdmmc_chip.h"
 
 #if defined(CONFIG_STM32_SDMMC1) || defined(CONFIG_STM32_SDMMC2)
 
@@ -86,11 +84,11 @@
  *
  * HW Issues when using IDMA
  *
- *    The DMA buffer must be located in a zone accessible via IDMA.
- * For SDMMC1, IDMA cannot access SRAM123 or SRAM4. Refer to ST AN5200.
+ *    The DMA buffer must be located in a zone accessible via IDMA.  The
+ * family stm32_sdmmc_chip.h gives these zones.
  * Buffer validity is checked when CONFIG_ARCH_HAVE_SDIO_PREFLIGHT is set.
  *
- * MDMA is only available on for SDMMC1 and Not supported at this time.
+ * The STM32H7 MDMA path for SDMMC1 is not supported at this time.
  *
  * Required system configuration options:
  *
@@ -138,20 +136,8 @@
 
 #if !defined(CONFIG_STM32_SDMMC_IDMA)
 #  warning "Large Non-DMA transfer may result in RX overrun failures"
-#else
-#  define AXISRAM_START  STM32_AXISRAM_BASE
-#  define AXISRAM_END    (AXISRAM_START + STM32_SRAM_SIZE)
-#  if defined(CONFIG_ARCH_CHIP_STM32H7_CORTEXM7) && \
-      !defined(CONFIG_STM32_STM32H72XXX_OR_STM32H73XXX) && \
-      (defined(CONFIG_STM32_STM32H7X0XX) || \
-       defined(CONFIG_STM32_STM32H7X3XX) || \
-       defined(CONFIG_STM32_STM32H7X5XX))
-#    define HAVE_SDMMC2_D2D3_IDMA 1
-#    define SRAM123_START STM32_SRAM123_BASE
-#    define SRAM123_END   (SRAM123_START + STM32_SRAM123_SIZE)
-#    define SRAM4_START   STM32_SRAM4_BASE
-#    define SRAM4_END     (SRAM4_START + STM32_SRAM4_SIZE)
-#  endif
+#elif !defined(STM32_SDMMC_IDMA_RAM_START) || 
!defined(STM32_SDMMC_IDMA_RAM_END)
+#  error "stm32_sdmmc_chip.h must define the IDMA accessible memory"
 #endif
 
 #if !defined(CONFIG_SCHED_WORKQUEUE) || !defined(CONFIG_SCHED_HPWORK)
@@ -183,20 +169,54 @@
 
 #define FIFO_SIZE_IN_BYTES        64
 
-/* Friendly Clock source & CLKCR bit re-definitions *************************/
-
-/* If not set in board use default pll1_q_ck clock is selected as
- * kernel peripheral clock (default after reset)
- */
+/* Data cache maintenance for ARMv7-M and ARMv8-M cores */
 
-#if !defined(STM32_RCC_D1CCIPR_SDMMCSEL)
-#  define STM32_RCC_D1CCIPR_SDMMCSEL  RCC_D1CCIPR_SDMMC_PLL1
+#if defined(CONFIG_ARMV7M_DCACHE)
+#  define SDMMC_HAVE_DCACHE 1
+#  if defined(CONFIG_ARMV7M_DCACHE_WRITETHROUGH)
+#    define SDMMC_DCACHE_WRITETHROUGH 1
+#  endif
+#elif defined(CONFIG_ARMV8M_DCACHE)
+#  define SDMMC_HAVE_DCACHE 1
+#  if defined(CONFIG_ARMV8M_DCACHE_WRITETHROUGH)
+#    define SDMMC_DCACHE_WRITETHROUGH 1
+#  endif
 #endif
 
-#if STM32_RCC_D1CCIPR_SDMMCSEL  == RCC_D1CCIPR_SDMMC_PLL1
-#  define STM32_SDMMC_CLK  STM32_PLL1Q_FREQUENCY
+#if defined(ARMV7M_DCACHE_LINESIZE)
+#  define SDMMC_DCACHE_LINESIZE ARMV7M_DCACHE_LINESIZE
+#elif defined(ARMV8M_DCACHE_LINESIZE)
+#  define SDMMC_DCACHE_LINESIZE ARMV8M_DCACHE_LINESIZE
 #else
-#  define STM32_SDMMC_CLK  STM32_PLL2R_FREQUENCY
+#  define SDMMC_DCACHE_LINESIZE 32
+#endif
+
+/* Peripheral reset bits */
+
+#if defined(RCC_AHB3RSTR_SDMMC1RST)
+#  define STM32_RCC_SDMMC1_RST_REG  STM32_RCC_AHB3RSTR
+#  define STM32_RCC_SDMMC1_RST      RCC_AHB3RSTR_SDMMC1RST
+#elif defined(RCC_AHB5RSTR_SDMMC1RST)
+#  define STM32_RCC_SDMMC1_RST_REG  STM32_RCC_AHB5RSTR
+#  define STM32_RCC_SDMMC1_RST      RCC_AHB5RSTR_SDMMC1RST
+#endif
+
+#if defined(RCC_AHB2RSTR_SDMMC2RST)
+#  define STM32_RCC_SDMMC2_RST_REG  STM32_RCC_AHB2RSTR
+#  define STM32_RCC_SDMMC2_RST      RCC_AHB2RSTR_SDMMC2RST
+#elif defined(RCC_AHB5RSTR_SDMMC2RST)
+#  define STM32_RCC_SDMMC2_RST_REG  STM32_RCC_AHB5RSTR
+#  define STM32_RCC_SDMMC2_RST      RCC_AHB5RSTR_SDMMC2RST
+#endif
+
+/* Friendly Clock source & CLKCR bit re-definitions *************************/
+
+/* The SDMMC kernel clock frequency comes from board.h or the family
+ * stm32_sdmmc_chip.h.
+ */
+
+#if !defined(STM32_SDMMC_CLK)
+#  error "board.h must provide STM32_SDMMC_CLK"
 #endif
 
 #define STM32_CLKCR_RISINGEDGE    (0)
@@ -371,6 +391,8 @@ struct stm32_dev_s
 
   uint32_t          base;
   int               nirq;
+  uint32_t          rstreg;           /* RCC reset register */
+  uint32_t          rstbit;           /* RCC reset bit */
 #if defined(CONFIG_MMCSD_SDIOWAIT_WRCOMPLETE)
   uint32_t          d0_gpio;
 #endif
@@ -420,7 +442,7 @@ struct stm32_dev_s
   struct work_s      cbfifo;          /* Monitor for Lame FIFO */
 #endif
   uint8_t            rxfifo[FIFO_SIZE_IN_BYTES] /* To offload with IDMA and 
support un-alinged buffers */
-                     aligned_data(ARMV7M_DCACHE_LINESIZE);
+                     aligned_data(SDMMC_DCACHE_LINESIZE);
   bool               unaligned_rx; /* read buffer is not cache-line or 32 bit 
aligned */
 #if defined(CONFIG_STM32_SDMMC_IDMA_BOUNCE_BUFFER)
   bool               bounce_rx;    /* Receive uses the IDMA bounce buffer */
@@ -595,7 +617,7 @@ static void stm32_default(struct stm32_dev_s *priv);
  ****************************************************************************/
 #if defined(CONFIG_STM32_SDMMC_IDMA) && defined(CONFIG_STM32_SDMMC1)
 static uint8_t g_sdmmc1_idmabuffer[SDMMC_IDMA_BUFFER_SIZE]
-               aligned_data(ARMV7M_DCACHE_LINESIZE);
+               aligned_data(SDMMC_DCACHE_LINESIZE);
 #endif
 
 #if defined(CONFIG_STM32_SDMMC1)
@@ -647,6 +669,8 @@ struct stm32_dev_s g_sdmmcdev1 =
   },
   .base               = STM32_SDMMC1_BASE,
   .nirq               = STM32_IRQ_SDMMC1,
+  .rstreg             = STM32_RCC_SDMMC1_RST_REG,
+  .rstbit             = STM32_RCC_SDMMC1_RST,
 #if defined(CONFIG_MMCSD_SDIOWAIT_WRCOMPLETE)
   .d0_gpio            = SDMMC1_SDIO_PULL(GPIO_SDMMC1_D0),
 #endif
@@ -662,7 +686,7 @@ struct stm32_dev_s g_sdmmcdev1 =
 
 #if defined(CONFIG_STM32_SDMMC_IDMA) && defined(CONFIG_STM32_SDMMC2)
 static uint8_t g_sdmmc2_idmabuffer[SDMMC_IDMA_BUFFER_SIZE]
-               aligned_data(ARMV7M_DCACHE_LINESIZE);
+               aligned_data(SDMMC_DCACHE_LINESIZE);
 #endif
 
 #if defined(CONFIG_STM32_SDMMC2)
@@ -714,6 +738,8 @@ struct stm32_dev_s g_sdmmcdev2 =
   },
   .base               = STM32_SDMMC2_BASE,
   .nirq               = STM32_IRQ_SDMMC2,
+  .rstreg             = STM32_RCC_SDMMC2_RST_REG,
+  .rstbit             = STM32_RCC_SDMMC2_RST,
 #if defined(CONFIG_MMCSD_SDIOWAIT_WRCOMPLETE)
   .d0_gpio            = SDMMC2_SDIO_PULL(GPIO_SDMMC2_D0),
 #endif
@@ -1280,29 +1306,10 @@ static void stm32_datastop(struct stm32_dev_s *priv)
       uint32_t mask = sdmmc_getreg32(priv, STM32_SDMMC_MASK_OFFSET);
       uint32_t dctrl = sdmmc_getreg32(priv, STM32_SDMMC_DCTRL_OFFSET) &
                        STM32_SDMMC_DCTRL_SDIOEN;
-      uint32_t regaddress = 0;
-      uint32_t rstbit = 0;
-
-#if defined(CONFIG_STM32_SDMMC1)
-      if (priv->base == STM32_SDMMC1_BASE)
-        {
-          regaddress = STM32_RCC_AHB3RSTR;
-          rstbit = RCC_AHB3RSTR_SDMMC1RST;
-        }
-
-#endif
-#if defined(CONFIG_STM32_SDMMC2)
-      if (priv->base == STM32_SDMMC2_BASE)
-        {
-          regaddress = STM32_RCC_AHB2RSTR;
-          rstbit = RCC_AHB2RSTR_SDMMC2RST;
-        }
-#endif
 
-      DEBUGASSERT(rstbit != 0);
-      modifyreg32(regaddress, 0, rstbit);
+      modifyreg32(priv->rstreg, 0, priv->rstbit);
       up_udelay(2);
-      modifyreg32(regaddress, rstbit, 0);
+      modifyreg32(priv->rstreg, priv->rstbit, 0);
 
       sdmmc_putreg32(priv, power, STM32_SDMMC_POWER_OFFSET);
       sdmmc_putreg32(priv, clkcr, STM32_SDMMC_CLKCR_OFFSET);
@@ -2075,33 +2082,15 @@ static void stm32_reset(struct sdio_dev_s *dev)
   struct stm32_dev_s *priv = (struct stm32_dev_s *)dev;
   irqstate_t flags;
   uint32_t regval;
-  uint32_t regaddress = 0;
-  uint32_t restval = 0;
 
   /* Disable clocking */
 
   flags = enter_critical_section();
 
-#if defined(CONFIG_STM32_SDMMC1)
-  if (priv->base == STM32_SDMMC1_BASE)
-    {
-      regaddress = STM32_RCC_AHB3RSTR;
-      restval    = RCC_AHB3RSTR_SDMMC1RST;
-    }
-#endif
-
-#if defined CONFIG_STM32_SDMMC2
-  if (priv->base == STM32_SDMMC2_BASE)
-    {
-      regaddress = STM32_RCC_AHB2RSTR;
-      restval    = RCC_AHB2RSTR_SDMMC2RST;
-    }
-#endif
-
-  regval = getreg32(regaddress);
-  putreg32(regval | restval, regaddress);
+  regval = getreg32(priv->rstreg);
+  putreg32(regval | priv->rstbit, priv->rstreg);
   nxsched_usleep(2);
-  putreg32(regval, regaddress);
+  putreg32(regval, priv->rstreg);
 
   stm32_setpwrctrl(priv, STM32_SDMMC_POWER_PWRCTRL_CYCLE);
   nxsched_usleep(1000);
@@ -3248,7 +3237,7 @@ errout_with_waitints:
         {
           DEBUGASSERT(priv->receivecnt <= SDMMC_IDMA_BUFFER_SIZE);
 
-#  if defined(CONFIG_ARMV7M_DCACHE)
+#  if defined(SDMMC_HAVE_DCACHE)
           up_invalidate_dcache((uintptr_t)priv->sdmmc_idmabuffer,
                                (uintptr_t)priv->sdmmc_idmabuffer +
                                priv->receivecnt);
@@ -3358,24 +3347,24 @@ static bool stm32_idma_accessible(struct stm32_dev_s 
*priv,
       return false;
     }
 
-  /* All STM32H7 SDMMC instances can reach D1 AXI SRAM. */
+  /* All SDMMC instances can reach AXI SRAM. */
 
-  if (start >= AXISRAM_START && end <= AXISRAM_END)
+  if (start >= STM32_SDMMC_IDMA_RAM_START &&
+      end <= STM32_SDMMC_IDMA_RAM_END)
     {
       return true;
     }
 
-  /* On the STM32H74x/H75x family, SDMMC2 can also reach the D2 SRAM1/2/3
-   * and D3 SRAM4 regions.  Other families remain conservatively limited to
-   * AXI SRAM until their bus matrix has an explicit allow-list.  Cortex-M4
-   * uses a private 0x100... alias for SRAM123, which must not be exposed to
-   * an IDMA bus master.
+  /* SDMMC2 may also reach the SRAM123 and SRAM4 regions given by
+   * stm32_sdmmc_chip.h.  Other memory must bounce or be rejected.
    */
 
-#if defined(CONFIG_STM32_SDMMC2) && defined(HAVE_SDMMC2_D2D3_IDMA)
+#if defined(CONFIG_STM32_SDMMC2) && defined(STM32_SDMMC2_IDMA_SRAM123_START)
   if (priv->base == STM32_SDMMC2_BASE &&
-      ((start >= SRAM123_START && end <= SRAM123_END) ||
-       (start >= SRAM4_START && end <= SRAM4_END)))
+      ((start >= STM32_SDMMC2_IDMA_SRAM123_START &&
+        end <= STM32_SDMMC2_IDMA_SRAM123_END) ||
+       (start >= STM32_SDMMC2_IDMA_SRAM4_START &&
+        end <= STM32_SDMMC2_IDMA_SRAM4_END)))
     {
       return true;
     }
@@ -3403,9 +3392,9 @@ static bool stm32_idma_bounce_required(struct stm32_dev_s 
*priv,
       return true;
     }
 
-#if defined(CONFIG_ARMV7M_DCACHE)
-  return (start & (ARMV7M_DCACHE_LINESIZE - 1)) != 0 ||
-         (buflen & (ARMV7M_DCACHE_LINESIZE - 1)) != 0;
+#if defined(SDMMC_HAVE_DCACHE)
+  return (start & (SDMMC_DCACHE_LINESIZE - 1)) != 0 ||
+         (buflen & (SDMMC_DCACHE_LINESIZE - 1)) != 0;
 #else
   return (start & 3) != 0;
 #endif
@@ -3464,17 +3453,17 @@ static int stm32_dmapreflight(struct sdio_dev_s *dev,
       return -EFAULT;
     }
 
-#if defined(CONFIG_ARMV7M_DCACHE) && 
!defined(CONFIG_ARMV7M_DCACHE_WRITETHROUGH)
+#if defined(SDMMC_HAVE_DCACHE) && !defined(SDMMC_DCACHE_WRITETHROUGH)
   /* buffer alignment is required for DMA transfers with dcache in buffered
    * mode (not write-through) because a) arch_invalidate_dcache could lose
    * buffered writes and b) arch_flush_dcache could corrupt adjacent memory
    * if the maddr and the mend+1, the next next address are not on
-   * ARMV7M_DCACHE_LINESIZE boundaries.
+   * SDMMC_DCACHE_LINESIZE boundaries.
    */
 
   if (buffer != priv->rxfifo &&
-      (((uintptr_t)buffer & (ARMV7M_DCACHE_LINESIZE - 1)) != 0 ||
-      ((uintptr_t)(buffer + buflen) & (ARMV7M_DCACHE_LINESIZE - 1)) != 0))
+      (((uintptr_t)buffer & (SDMMC_DCACHE_LINESIZE - 1)) != 0 ||
+      ((uintptr_t)(buffer + buflen) & (SDMMC_DCACHE_LINESIZE - 1)) != 0))
     {
       mcerr("dcache unaligned "
             "buffer:%p end:%p\n",
@@ -3527,7 +3516,7 @@ static int stm32_dmarecvsetup(struct sdio_dev_s *dev,
   priv->bounce_rx = stm32_idma_bounce_required(priv, buffer, buflen);
   priv->unaligned_rx = false;
 
-#  if defined(CONFIG_ARMV7M_DCACHE)
+#  if defined(SDMMC_HAVE_DCACHE)
   if (priv->bounce_rx)
     {
       up_invalidate_dcache((uintptr_t)priv->sdmmc_idmabuffer,
@@ -3539,9 +3528,9 @@ static int stm32_dmarecvsetup(struct sdio_dev_s *dev,
     }
 #  endif
 #else
-#  if defined(CONFIG_ARMV7M_DCACHE)
-  if (((uintptr_t)buffer & (ARMV7M_DCACHE_LINESIZE - 1)) != 0 ||
-      (buflen & (ARMV7M_DCACHE_LINESIZE - 1)) != 0)
+#  if defined(SDMMC_HAVE_DCACHE)
+  if (((uintptr_t)buffer & (SDMMC_DCACHE_LINESIZE - 1)) != 0 ||
+      (buflen & (SDMMC_DCACHE_LINESIZE - 1)) != 0)
     {
       /* Preserve the legacy single-block bounce path when the new
        * multi-block bounce buffer is disabled.
@@ -3669,10 +3658,12 @@ static int stm32_dmasendsetup(struct sdio_dev_s *dev,
 
   /* Flush cache to physical memory when not in DTCM memory */
 
-#if defined(CONFIG_ARMV7M_DCACHE) && \
-      !defined(CONFIG_ARMV7M_DCACHE_WRITETHROUGH)
+#if defined(SDMMC_HAVE_DCACHE) && \
+      !defined(SDMMC_DCACHE_WRITETHROUGH)
+#  if defined(DTCM_START)
   if ((uintptr_t)dma_buffer < DTCM_START ||
       (uintptr_t)dma_buffer + buflen > DTCM_END)
+#  endif
     {
       up_clean_dcache((uintptr_t)dma_buffer,
                       (uintptr_t)dma_buffer + buflen);
diff --git a/arch/arm/src/stm32h7/CMakeLists.txt 
b/arch/arm/src/stm32h7/CMakeLists.txt
index e7b16aade50..1d9dfffe670 100644
--- a/arch/arm/src/stm32h7/CMakeLists.txt
+++ b/arch/arm/src/stm32h7/CMakeLists.txt
@@ -127,10 +127,6 @@ if(CONFIG_SPI_SLAVE)
   list(APPEND SRCS stm32_spi_slave.c)
 endif()
 
-if(CONFIG_STM32_SDMMC)
-  list(APPEND SRCS stm32_sdmmc.c)
-endif()
-
 if(CONFIG_STM32_TIMX_CAP)
   list(APPEND SRCS stm32_capture.c)
 endif()
diff --git a/arch/arm/src/stm32h7/Kconfig b/arch/arm/src/stm32h7/Kconfig
index 8a0513522e2..069de5ac3ca 100644
--- a/arch/arm/src/stm32h7/Kconfig
+++ b/arch/arm/src/stm32h7/Kconfig
@@ -41,6 +41,7 @@ config STM32_H7_LEGACY
        select STM32_HAVE_IP_TIMERS_V2
        select STM32_HAVE_IP_WDG_V2
        select STM32_HAVE_IP_LPTIM_V1
+       select STM32_HAVE_IP_SDMMC_V2
        select STM32_HAVE_EXTI_SHARED_IRQ
        select STM32_HAVE_DMA1
        select STM32_HAVE_DMA2
diff --git a/arch/arm/src/stm32h7/Make.defs b/arch/arm/src/stm32h7/Make.defs
index bf373436288..119e3e1ad72 100644
--- a/arch/arm/src/stm32h7/Make.defs
+++ b/arch/arm/src/stm32h7/Make.defs
@@ -130,10 +130,6 @@ ifeq ($(CONFIG_SPI_SLAVE),y)
 CHIP_CSRCS += stm32_spi_slave.c
 endif
 
-ifeq ($(CONFIG_STM32_SDMMC),y)
-CHIP_CSRCS += stm32_sdmmc.c
-endif
-
 ifeq ($(CONFIG_STM32_TIMX_CAP),y)
 CHIP_CSRCS += stm32_capture.c
 endif
diff --git a/arch/arm/src/stm32h7/stm32_sdmmc_chip.h 
b/arch/arm/src/stm32h7/stm32_sdmmc_chip.h
new file mode 100644
index 00000000000..574774086c1
--- /dev/null
+++ b/arch/arm/src/stm32h7/stm32_sdmmc_chip.h
@@ -0,0 +1,80 @@
+/****************************************************************************
+ * arch/arm/src/stm32h7/stm32_sdmmc_chip.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __ARCH_ARM_SRC_STM32H7_STM32_SDMMC_CHIP_H
+#define __ARCH_ARM_SRC_STM32H7_STM32_SDMMC_CHIP_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <arch/board/board.h>
+
+#include "chip.h"
+#include "hardware/stm32_rcc.h"
+#include "stm32_dtcm.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Memory that the SDMMC IDMA can access.  Every SDMMC instance reaches the
+ * AXI SRAM.  On the STM32H74x/H75x, SDMMC2 also reaches the D2 SRAM1/2/3
+ * and the D3 SRAM4 (ST AN5200).  The Cortex-M4 uses a private alias for
+ * SRAM123 that must not be given to the IDMA.
+ */
+
+#define STM32_SDMMC_IDMA_RAM_START  STM32_AXISRAM_BASE
+#define STM32_SDMMC_IDMA_RAM_END    (STM32_AXISRAM_BASE + STM32_SRAM_SIZE)
+
+#if defined(CONFIG_ARCH_CHIP_STM32H7_CORTEXM7) && \
+    !defined(CONFIG_STM32_STM32H72XXX_OR_STM32H73XXX) && \
+    (defined(CONFIG_STM32_STM32H7X0XX) || \
+     defined(CONFIG_STM32_STM32H7X3XX) || \
+     defined(CONFIG_STM32_STM32H7X5XX))
+#  define STM32_SDMMC2_IDMA_SRAM123_START STM32_SRAM123_BASE
+#  define STM32_SDMMC2_IDMA_SRAM123_END   (STM32_SRAM123_BASE + \
+                                           STM32_SRAM123_SIZE)
+#  define STM32_SDMMC2_IDMA_SRAM4_START   STM32_SRAM4_BASE
+#  define STM32_SDMMC2_IDMA_SRAM4_END     (STM32_SRAM4_BASE + \
+                                           STM32_SRAM4_SIZE)
+#endif
+
+/* The SDMMC kernel clock frequency.  board.h may provide STM32_SDMMC_CLK or
+ * select the kernel clock with STM32_RCC_D1CCIPR_SDMMCSEL; pll1_q_ck is the
+ * default after reset.
+ */
+
+#if !defined(STM32_SDMMC_CLK)
+#  if !defined(STM32_RCC_D1CCIPR_SDMMCSEL)
+#    define STM32_RCC_D1CCIPR_SDMMCSEL  RCC_D1CCIPR_SDMMC_PLL1
+#  endif
+#  if STM32_RCC_D1CCIPR_SDMMCSEL  == RCC_D1CCIPR_SDMMC_PLL1
+#    define STM32_SDMMC_CLK  STM32_PLL1Q_FREQUENCY
+#  else
+#    define STM32_SDMMC_CLK  STM32_PLL2R_FREQUENCY
+#  endif
+#endif
+
+#endif /* __ARCH_ARM_SRC_STM32H7_STM32_SDMMC_CHIP_H */

Reply via email to