[PATCH 6/6] mmc: mmci: stm32: handle address remapping on STM32MP21
From: Christophe Kerello
Date: Tue Dec 01 2026 - 11:22:49 EST
On STM32MP21, the DDR is mapped at 0x80000000 from the CPU point of view,
while the SDMMC can only issue 32-bit addresses. Only the first 2 GB of
the DDR can therefore be reached. The SYSCFG area dedicated to each SDMMC
instance provides an address remapping register (ARCR), which shifts the
view of the SDMMC so that the whole 4 GB of the DDR becomes reachable.
On st,stm32mp21-sdmmc2, the dma-ranges are used to find where the
beginning of the DDR is seen by the SDMMC. When the DMA address 0 is
translated to the beginning of the DDR, the remapping is enabled, the
IDMA then being programmed with the translated addresses. Otherwise,
when the DDR is seen at its CPU address, whether or not the DMA address
0 is covered by the dma-ranges, the remapping is disabled, in case a
previous kernel left it enabled, and only the first 2 GB of the DDR can
be reached. Any other translation can't be handled and fails the probe.
ARCR is accessed through the second "reg" range, already mapped for the
delay block registers. As the IDMA would otherwise access wrong memory
locations, whether the remapping has to be enabled or, if left enabled,
disabled, the probe fails on STM32MP21 when that range is missing, can't
be mapped or is too small to cover ARCR. The delay block remains
optional on the other SoCs.
ARCR is cleared on a system reset and when the SDMMC IP is reset. The
latter happens when powering off the SDMMC, from
mmci_sdmmc_set_pwrreg(), and from the error recovery path of
mmci_irq_thread(), which only restores MMCICLOCK, MMCIPOWER and
MMCIMASK0. The remapping is therefore enabled, if not already, when
starting each transfer rather than once at probe time. It is then left
enabled: as ARCR is cleared by the system reset, the ROM code and the
bootloaders find the remapping disabled. A kexec skips the system
reset, so the next kernel may inherit the remapping: it is then cleared
at probe time when not needed, as described above.
As ARCR and the IDMA registers belong to two different peripherals, the
relaxed accessors do not order the accesses to one against the other, hence
the read-back after the ARCR programming.
Signed-off-by: Christophe Kerello <christophe.kerello@xxxxxxxxxxx>
---
drivers/mmc/host/mmci_stm32_sdmmc.c | 95 +++++++++++++++++++++++++++++++++++--
1 file changed, 92 insertions(+), 3 deletions(-)
diff --git a/drivers/mmc/host/mmci_stm32_sdmmc.c b/drivers/mmc/host/mmci_stm32_sdmmc.c
index 322bfe0a0945..b1a7ef407745 100644
--- a/drivers/mmc/host/mmci_stm32_sdmmc.c
+++ b/drivers/mmc/host/mmci_stm32_sdmmc.c
@@ -47,6 +47,11 @@
#define DLYBSD_TIMEOUT_1S_IN_US 1000000
+#define SYSCFG_ARCR 0xc
+#define SYSCFG_ARCR_EN BIT(0)
+
+#define STM32MP21_DDR_BASE 0x80000000
+
struct sdmmc_lli_desc {
u32 idmalar;
u32 idmabase;
@@ -80,6 +85,7 @@ struct sdmmc_dlyb {
};
struct sdmmc_priv {
+ void __iomem *arcr_reg;
struct sdmmc_dlyb dlyb;
};
@@ -197,6 +203,24 @@ static void sdmmc_idma_unprep_data(struct mmci_host *host,
}
}
+static void sdmmc_idma_arcr_enable(struct mmci_host *host)
+{
+ struct sdmmc_priv *priv = host->variant_priv;
+ u32 arcr;
+
+ if (!priv || !priv->arcr_reg)
+ return;
+
+ arcr = readl_relaxed(priv->arcr_reg);
+ if (arcr & SYSCFG_ARCR_EN)
+ return;
+
+ writel_relaxed(arcr | SYSCFG_ARCR_EN, priv->arcr_reg);
+
+ /* Flush the ARCR write and order it against the IDMA programming */
+ readl(priv->arcr_reg);
+}
+
static int sdmmc_idma_setup(struct mmci_host *host)
{
struct sdmmc_idma *idma;
@@ -239,6 +263,7 @@ static int sdmmc_idma_start(struct mmci_host *host, unsigned int *datactrl)
int i;
host->dma_in_progress = true;
+ sdmmc_idma_arcr_enable(host);
if (!host->variant->dma_lli || data->sg_len == 1 ||
idma->use_bounce_buffer) {
@@ -745,21 +770,82 @@ static struct sdmmc_tuning_ops dlyb_tuning_mp25_ops = {
.set_cfg = sdmmc_dlyb_mp25_set_cfg,
};
+/* CPU address of a 32-bit DMA address, as translated by the dma-ranges */
+static u64 sdmmc_dma_to_cpu(struct device_node *np, u32 dma)
+{
+ __be32 addr[4] = { };
+ int na = of_n_addr_cells(np);
+
+ if (na < 1 || na > (int)ARRAY_SIZE(addr))
+ return OF_BAD_ADDR;
+
+ addr[na - 1] = cpu_to_be32(dma);
+
+ return of_translate_dma_address(np, addr);
+}
+
int sdmmc_variant_init(struct mmci_host *host)
{
struct device_node *np = host->mmc->parent->of_node;
+ struct device *dev = mmc_dev(host->mmc);
struct sdmmc_priv *priv;
struct sdmmc_dlyb *dlyb;
+ resource_size_t size;
void __iomem *base;
+ bool is_mp21, need_arcr = false;
host->ops = &sdmmc_variant_ops;
host->pwr_reg = readl_relaxed(host->base + MMCIPOWER);
- base = devm_of_iomap(mmc_dev(host->mmc), np, 1, NULL);
- if (IS_ERR(base))
+ is_mp21 = of_device_is_compatible(np, "st,stm32mp21-sdmmc2");
+ if (is_mp21) {
+ u64 cpu_0 = sdmmc_dma_to_cpu(np, 0);
+ u64 cpu_ddr = sdmmc_dma_to_cpu(np, STM32MP21_DDR_BASE);
+
+ /*
+ * ARCR is only needed when the beginning of the DDR is seen
+ * at the DMA address 0. Otherwise, the DDR must be seen at
+ * its CPU address, any other translation can't be handled
+ * by the IDMA.
+ */
+ if (cpu_0 == STM32MP21_DDR_BASE)
+ need_arcr = true;
+ else if (cpu_ddr != STM32MP21_DDR_BASE)
+ return dev_err_probe(dev, -EINVAL,
+ "unsupported dma-ranges (DMA 0 -> %#llx, DMA %#x -> %#llx)\n",
+ cpu_0, STM32MP21_DDR_BASE, cpu_ddr);
+ }
+
+ /*
+ * The delay block is optional, but ARCR must be accessible on
+ * STM32MP21, either to enable the remapping or to disable it.
+ */
+ if (is_mp21 && !of_get_address(np, 1, NULL, NULL))
+ return dev_err_probe(dev, -EINVAL,
+ "missing SYSCFG area for ARCR\n");
+
+ base = devm_of_iomap(dev, np, 1, &size);
+ if (IS_ERR(base)) {
+ if (is_mp21)
+ return dev_err_probe(dev, PTR_ERR(base),
+ "can't map SYSCFG area for ARCR\n");
+
return 0;
+ }
+
+ if (is_mp21 && size < SYSCFG_ARCR + sizeof(u32))
+ return dev_err_probe(dev, -EINVAL,
+ "SYSCFG area too small for ARCR\n");
- priv = devm_kzalloc(mmc_dev(host->mmc), sizeof(*priv), GFP_KERNEL);
+ /* The remapping may have been left enabled by a previous kernel */
+ if (is_mp21 && !need_arcr) {
+ u32 arcr = readl_relaxed(base + SYSCFG_ARCR);
+
+ writel_relaxed(arcr & ~SYSCFG_ARCR_EN, base + SYSCFG_ARCR);
+ readl(base + SYSCFG_ARCR);
+ }
+
+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
@@ -773,6 +859,9 @@ int sdmmc_variant_init(struct mmci_host *host)
dlyb->min_freq = 50000000;
}
+ if (need_arcr)
+ priv->arcr_reg = base + SYSCFG_ARCR;
+
host->variant_priv = priv;
host->mmc_ops->execute_tuning = sdmmc_execute_tuning;
--
2.43.0