git: 604e2b5c2adb - main - mmc: Fix clock handling in mmc-pwrseq-simple set_power()

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Thu, 05 Nov 2026 14:51:54 UTC
The branch main has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=604e2b5c2adb6a3961c68f3061840e7eafea67d4

commit 604e2b5c2adb6a3961c68f3061840e7eafea67d4
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-10-22 06:33:55 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-05 14:51:34 +0000

    mmc: Fix clock handling in mmc-pwrseq-simple set_power()
    
    set_power() enabled the ext_clock on power-on and called clk_stop() on
    power-off.  This was wrong in three ways:
    
    - clk_stop() is not the counterpart of clk_enable() (it KASSERTs
      enable_cnt == 0), so it panics once the clock has been enabled.
    - set_power() is driven by the MMC power_mode IOS and is called
      repeatedly for the same state during probing and tuning, so the
      enable/disable refcount over- or underflows and panics.
    - The ext_clock may be a shared PMIC output (e.g. rk808-clkout2 on the
      RockPro64, also feeding the companion Bluetooth controller), so gating
      it here disturbs the other consumers.
    
    The external clock is a free-running low-power oscillator the device
    needs for its whole lifetime, so enable it once at attach and leave it
    running.  Only act on real power-mode transitions in set_power() and
    limit it to driving the reset line, which is what actually sequences the
    device.
    
    Tested on a RockPro64 (Broadcom BCM4359 SDIO Wi-Fi): the SDIO function
    now enumerates and the module is reachable from userspace.
    
    MFC after:      1 week
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D61023
---
 sys/dev/mmc/mmc_pwrseq.c | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

diff --git a/sys/dev/mmc/mmc_pwrseq.c b/sys/dev/mmc/mmc_pwrseq.c
index 46bfab41aa6c..06a760f8f963 100644
--- a/sys/dev/mmc/mmc_pwrseq.c
+++ b/sys/dev/mmc/mmc_pwrseq.c
@@ -58,6 +58,7 @@ struct mmc_pwrseq_softc {
 
 	uint32_t		post_power_on_delay_ms;
 	uint32_t		power_off_delay_us;
+	bool			powered;
 };
 
 static int
@@ -99,6 +100,23 @@ mmc_pwrseq_attach(device_t dev)
 				    "Node have a clocks property but no clocks named \"ext_clock\"\n");
 				return (ENXIO);
 			}
+			/*
+			 * The external clock is a free-running low-power
+			 * oscillator (typically a PMIC 32kHz output) that the
+			 * attached device needs for its whole lifetime.  On
+			 * several boards it is also shared with other users of
+			 * the same module (e.g. the companion Bluetooth
+			 * controller), so gating it from set_power() disturbs
+			 * the other consumers.  Enable it once here and leave
+			 * it running rather than toggling it on every MMC
+			 * power_mode transition.
+			 */
+			rv = clk_enable(sc->ext_clock);
+			if (rv != 0) {
+				device_printf(dev,
+				    "Cannot enable \"ext_clock\"\n");
+				return (ENXIO);
+			}
 		}
 		OF_getencprop(node, "post-power-on-delay-ms", &sc->post_power_on_delay_ms, sizeof(uint32_t));
 		OF_getencprop(node, "power-off-delay-us", &sc->power_off_delay_us, sizeof(uint32_t));
@@ -133,13 +151,17 @@ mmv_pwrseq_set_power(device_t dev, bool power_on)
 
 	sc = device_get_softc(dev);
 
-	if (power_on) {
-		if (sc->ext_clock) {
-			rv = clk_enable(sc->ext_clock);
-			if (rv != 0)
-				return (rv);
-		}
+	/*
+	 * set_power() is driven from the MMC power_mode IOS and can be
+	 * called repeatedly for the same state during probing and tuning.
+	 * Only act on real transitions.  The external clock is handled once
+	 * at attach (see mmc_pwrseq_attach()); here we only drive the reset
+	 * line, which is what actually sequences the device's power.
+	 */
+	if (power_on == sc->powered)
+		return (0);
 
+	if (power_on) {
 		if (sc->reset_gpio) {
 			rv = gpio_pin_set_active(sc->reset_gpio, false);
 			if (rv != 0)
@@ -155,15 +177,11 @@ mmv_pwrseq_set_power(device_t dev, bool power_on)
 				return (rv);
 		}
 
-		if (sc->ext_clock) {
-			rv = clk_stop(sc->ext_clock);
-			if (rv != 0)
-				return (rv);
-		}
 		if (sc->power_off_delay_us)
 			DELAY(sc->power_off_delay_us);
 	}
 
+	sc->powered = power_on;
 	return (0);
 }