zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.17k stars 6.23k forks source link

STM32 USB PHY not working with UCPD node disabled and Type-C plugged into host supporting USB PD #68577

Closed JPHutchins closed 4 months ago

JPHutchins commented 5 months ago

Describe the bug

STM32 devices, like L5, have a USB PHY and UCPD (USB power delivery) system. We have encountered an issue wherein disabling the UCPD node (and disabling the STM provided USBPD system or the Zephyr USBPD system) while leaving the USB PHY enabled causes the STM32 to not enumerate on some host devices supporting USB PD on their Type-C ports.

This is a critical issue for MCUBoot USB functionality because it adds a USB PD requirement to MCUBoot - a potentially large flash footprint for a bootloader.

To Reproduce

Put the Zephyr shell on USB CDC ACM, disable the ucpd node, and verify:

Expected behavior

The USB PHY should not depend on the presence of some USB PD system.

Impact

Prevents FW upgrades from within MCUBoot from newer laptops that have Type-C with PD. Users must use a Type-C -> Type-A cable or adapter to defeat the bug.

Fix

It is relatively simple to fix and this is a patch that I would propose on all STM32 systems that have USB PD.

diff --git a/soc/arm/st_stm32/stm32l5/soc.c b/soc/arm/st_stm32/stm32l5/soc.c
index 1db1bf0411..ac22e0e7a7 100644
--- a/soc/arm/st_stm32/stm32l5/soc.c
+++ b/soc/arm/st_stm32/stm32l5/soc.c
@@ -58,8 +58,10 @@ static int stm32l5_init(void)
    LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
    LL_PWR_SetRegulVoltageScaling(LL_PWR_REGU_VOLTAGE_SCALE0);

-   /* Disable USB Type-C dead battery pull-down behavior */
-   LL_PWR_DisableUCPDDeadBattery();
+   if (IS_ENABLED(CONFIG_DT_HAS_ST_STM32_UCPD_ENABLED) || !IS_ENABLED(CONFIG_USB_DEVICE_DRIVER)) {
+       /* Disable USB Type-C dead battery pull-down behavior */
+       LL_PWR_DisableUCPDDeadBattery();
+   }

    return 0;
 }

The logic is that the UCPDDeadBattery Mode (AKA Rd 5.1K pulldown) should ONLY BE DISABLED if:

The commit adding this, https://github.com/zephyrproject-rtos/zephyr/commit/9e30ab58ea50ee8467adde26909eced0c9f85d52, has this message:

soc: arm: stm32l5 with USB-C PD cannot use CC1 and CC2 pins by default
With this patch, the UCPD1 _CC1 and _CC2 pins
are disabling the USB Type-C and Power Delivery Dead Battery.

Signed-off-by: Francois Ramu <francois.ramu@st.com>

Which is pretty clear! Yet, I think that it is a mistake to call this function in the soc.c files without regard for the products requirements.

@FRASTM please take a look when you have a chance, thank you!

Cheers, JP

erwango commented 5 months ago
FRASTM commented 5 months ago

The same change should apply to