On standby exit, the bootrom may reuse the SDMMC instance used as boot device, leaving the IP in a state not known by Linux. STM32 variants do not set pwrreg_nopower, so MMCICLOCK, MMCIDATACTRL and MMCIPOWER are neither saved nor restored by mmci_save()/mmci_restore(): their content is expected to be retained by the hardware. Add a reset_on_resume variant flag. When set, and if the card is powered off and a reset line is available, reset the IP on system resume and restore the cached MMCICLOCK, MMCIDATACTRL and MMCIPOWER values. This is done from a .resume_noirq callback, so that mmci_irq() cannot run on a state left by the bootrom once device interrupts are re-enabled. It also runs before pm_runtime_force_resume(), which does not resume the device if it was already runtime suspended before system suspend. The clock is therefore enabled locally for the duration of the sequence. Clock and reset errors are reported to the PM core. Enable the flag for all STM32 SDMMC variants. Fixes: 46b723dd867d ("mmc: mmci: add stm32 sdmmc variant") Cc: stable@vger.kernel.org Signed-off-by: Christophe Kerello --- drivers/mmc/host/mmci.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ drivers/mmc/host/mmci.h | 4 ++++ 2 files changed, 62 insertions(+) diff --git a/drivers/mmc/host/mmci.c b/drivers/mmc/host/mmci.c index 5ecb4d0bdaa5..f5affe4fe1b2 100644 --- a/drivers/mmc/host/mmci.c +++ b/drivers/mmc/host/mmci.c @@ -278,6 +278,7 @@ static struct variant_data variant_stm32_sdmmc = { .busy_detect = true, .busy_detect_flag = MCI_STM32_BUSYD0, .busy_detect_mask = MCI_STM32_BUSYD0ENDMASK, + .reset_on_resume = true, .init = sdmmc_variant_init, }; @@ -307,6 +308,7 @@ static struct variant_data variant_stm32_sdmmcv2 = { .busy_detect = true, .busy_detect_flag = MCI_STM32_BUSYD0, .busy_detect_mask = MCI_STM32_BUSYD0ENDMASK, + .reset_on_resume = true, .init = sdmmc_variant_init, }; @@ -336,6 +338,7 @@ static struct variant_data variant_stm32_sdmmcv3 = { .busy_detect = true, .busy_detect_flag = MCI_STM32_BUSYD0, .busy_detect_mask = MCI_STM32_BUSYD0ENDMASK, + .reset_on_resume = true, .init = sdmmc_variant_init, }; @@ -2586,8 +2589,63 @@ static int mmci_runtime_resume(struct device *dev) return 0; } +static int mmci_reset_on_resume(struct mmci_host *host) +{ + struct device *dev = mmc_dev(host->mmc); + unsigned long flags; + int ret; + + if (!host->variant->reset_on_resume || !host->rst || + host->mmc->ios.power_mode != MMC_POWER_OFF) + return 0; + + ret = clk_prepare_enable(host->clk); + if (ret) { + dev_err(dev, "failed to enable clock: %d\n", ret); + return ret; + } + + ret = reset_control_assert(host->rst); + if (ret) { + dev_err(dev, "failed to assert reset: %d\n", ret); + goto out; + } + + udelay(2); + + ret = reset_control_deassert(host->rst); + if (ret) { + dev_err(dev, "failed to deassert reset: %d\n", ret); + goto out; + } + + spin_lock_irqsave(&host->lock, flags); + writel(host->clk_reg, host->base + MMCICLOCK); + writel(host->datactrl_reg, host->base + MMCIDATACTRL); + writel(host->pwr_reg, host->base + MMCIPOWER); + mmci_reg_delay(host); + spin_unlock_irqrestore(&host->lock, flags); + +out: + clk_disable_unprepare(host->clk); + + return ret; +} + +static int mmci_resume_noirq(struct device *dev) +{ + struct amba_device *adev = to_amba_device(dev); + struct mmc_host *mmc = amba_get_drvdata(adev); + + if (!mmc) + return 0; + + return mmci_reset_on_resume(mmc_priv(mmc)); +} + static const struct dev_pm_ops mmci_dev_pm_ops = { SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume) + NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, mmci_resume_noirq) RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL) }; diff --git a/drivers/mmc/host/mmci.h b/drivers/mmc/host/mmci.h index 4d3647f9ec06..3a053e40d113 100644 --- a/drivers/mmc/host/mmci.h +++ b/drivers/mmc/host/mmci.h @@ -334,6 +334,9 @@ enum mmci_busy_state { * @supports_sdio_irq: allow SD I/O card to interrupt the host * @stm32_idmabsize_mask: stm32 sdmmc idma buffer size. * @dma_flow_controller: use peripheral as flow controller for DMA. + * @reset_on_resume: on system resume, if the card is powered off, reset the + * IP and restore MMCICLOCK, MMCIDATACTRL and MMCIPOWER, as + * the IP may have been used by a bootloader or the bootrom. */ struct variant_data { unsigned int clkreg; @@ -382,6 +385,7 @@ struct variant_data { u32 stm32_idmabsize_mask; u32 stm32_idmabsize_align; bool dma_flow_controller; + bool reset_on_resume; void (*init)(struct mmci_host *host); }; --- base-commit: d2616724e01132376c78872b8324f38659af9f28 base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc.git next change-id: 20261113-fix-resume-reset-9bb5714fd505 Best regards, -- Christophe Kerello