|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 STMicroelectronics |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <soc.h> |
| 8 | +#include <stm32_ll_bus.h> |
| 9 | +#include <stm32_ll_pwr.h> |
| 10 | +#include <stm32_ll_rcc.h> |
| 11 | +#include <stm32_ll_system.h> |
| 12 | +#include <string.h> |
| 13 | +#include <zephyr/kernel.h> |
| 14 | +#include <zephyr/sys/util.h> |
| 15 | + |
| 16 | +#include <zephyr/logging/log.h> |
| 17 | +LOG_MODULE_REGISTER(stm32_usb_pwr, CONFIG_STM32_USB_COMMON_LOG_LEVEL); |
| 18 | + |
| 19 | +static bool pwr_enabled = false; |
| 20 | + |
| 21 | +int stm32_usb_pwr_enable(void) { |
| 22 | + /* |
| 23 | + * Keep track of whether this has already been called |
| 24 | + * to avoid repeating the sequence. The bookkeeping is |
| 25 | + * done here to simplify the USB drivers, which can |
| 26 | + * call this function as part of the instance init |
| 27 | + * function without any prior check. |
| 28 | + * |
| 29 | + * NOTE: no mutex mechanism is implemented as this |
| 30 | + * function is expected to be called during device |
| 31 | + * initialization (at boot), which already ensures |
| 32 | + * only one device at a time is initialized. |
| 33 | + */ |
| 34 | + if (pwr_enabled) { |
| 35 | + return 0; |
| 36 | + } else { |
| 37 | + pwr_enabled = true; |
| 38 | + } |
| 39 | + |
| 40 | +#if defined(CONFIG_SOC_SERIES_STM32H7X) |
| 41 | + LL_PWR_EnableUSBVoltageDetector(); |
| 42 | + |
| 43 | + /* Per AN2606: USBREGEN not supported when running in FS mode. */ |
| 44 | + LL_PWR_DisableUSBReg(); |
| 45 | + while (!LL_PWR_IsActiveFlag_USB()) { |
| 46 | + LOG_INF("PWR not active yet"); |
| 47 | + k_msleep(100); |
| 48 | + } |
| 49 | +#elif defined(CONFIG_SOC_SERIES_STM32U5X) |
| 50 | + /* Sequence to enable the power of the OTG HS on a stm32U5 serie : Enable VDDUSB */ |
| 51 | + __ASSERT_NO_MSG(LL_AHB3_GRP1_IsEnabledClock(LL_AHB3_GRP1_PERIPH_PWR)); |
| 52 | + |
| 53 | + /* Check that power range is 1 or 2 */ |
| 54 | + if (LL_PWR_GetRegulVoltageScaling() < LL_PWR_REGU_VOLTAGE_SCALE2) { |
| 55 | + LOG_ERR("Wrong Power range to use USB OTG HS"); |
| 56 | + return -EIO; |
| 57 | + } |
| 58 | + |
| 59 | + LL_PWR_EnableVddUSB(); |
| 60 | + |
| 61 | + #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs) |
| 62 | + /* Configure VOSR register of USB HSTransceiverSupply(); */ |
| 63 | + LL_PWR_EnableUSBPowerSupply(); |
| 64 | + LL_PWR_EnableUSBEPODBooster(); |
| 65 | + while (LL_PWR_IsActiveFlag_USBBOOST() != 1) { |
| 66 | + /* Wait for USB EPOD BOOST ready */ |
| 67 | + } |
| 68 | + #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs) */ |
| 69 | +#elif defined(CONFIG_SOC_SERIES_STM32N6X) |
| 70 | + /* Enable Vdd33USB voltage monitoring */ |
| 71 | + LL_PWR_EnableVddUSBMonitoring(); |
| 72 | + while (!LL_PWR_IsActiveFlag_USB33RDY()) { |
| 73 | + /* Wait for Vdd33USB ready */ |
| 74 | + } |
| 75 | + |
| 76 | + /* Enable VDDUSB */ |
| 77 | + LL_PWR_EnableVddUSB(); |
| 78 | +#elif defined(CONFIG_SOC_SERIES_STM32WBAX) |
| 79 | + /* Remove VDDUSB power isolation */ |
| 80 | + LL_PWR_EnableVddUSB(); |
| 81 | + |
| 82 | + /* Make sure that voltage scaling is Range 1 */ |
| 83 | + __ASSERT_NO_MSG(LL_PWR_GetRegulCurrentVOS() == LL_PWR_REGU_VOLTAGE_SCALE1); |
| 84 | + |
| 85 | + /* Enable VDD11USB */ |
| 86 | + LL_PWR_EnableVdd11USB(); |
| 87 | + |
| 88 | + /* Enable USB OTG internal power */ |
| 89 | + LL_PWR_EnableUSBPWR(); |
| 90 | + |
| 91 | + while (!LL_PWR_IsActiveFlag_VDD11USBRDY()) { |
| 92 | + /* Wait for VDD11USB supply to be ready */ |
| 93 | + } |
| 94 | + |
| 95 | + /* Enable USB OTG booster */ |
| 96 | + LL_PWR_EnableUSBBooster(); |
| 97 | + |
| 98 | + while (!LL_PWR_IsActiveFlag_USBBOOSTRDY()) { |
| 99 | + /* Wait for USB OTG booster to be ready */ |
| 100 | + } |
| 101 | +#elif defined(PWR_USBSCR_USB33SV) || defined(PWR_SVMCR_USV) |
| 102 | + /* |
| 103 | + * VDDUSB independent USB supply (PWR clock is on) |
| 104 | + * with LL_PWR_EnableVDDUSB function (higher case) |
| 105 | + */ |
| 106 | + LL_PWR_EnableVDDUSB(); |
| 107 | +#elif defined(PWR_CR2_USV) |
| 108 | + /*cd |
| 109 | + * Required for at least STM32L4 devices as they electrically |
| 110 | + * isolate USB features from VDDUSB. It must be enabled before |
| 111 | + * USB can function. Refer to section 5.1.3 in DM00083560 or |
| 112 | + * DM00310109. |
| 113 | + */ |
| 114 | + LL_PWR_EnableVddUSB(); |
| 115 | +#endif |
| 116 | + return 0; |
| 117 | +} |
0 commit comments