Skip to content

Commit 52807c1

Browse files
author
Jiang Jiang Jian
committed
Merge branch 'fix/fix_p4_usb_phy_bad_suspend_on_lslp_v5.4' into 'release/v5.4'
fix(esp_hw_support): fix p4 OTG phy bad suspend cause high power consumption on sleep (v5.4) See merge request espressif/esp-idf!36365
2 parents 206b3a2 + 9b7ce54 commit 52807c1

File tree

7 files changed

+107
-3
lines changed

7 files changed

+107
-3
lines changed

components/esp_hw_support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ if(NOT non_os_build)
3131
"sleep_modem.c"
3232
"sleep_modes.c"
3333
"sleep_console.c"
34+
"sleep_usb.c"
3435
"sleep_gpio.c"
3536
"sleep_event.c"
3637
"regi2c_ctrl.c"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
#include <stdint.h>
9+
#include "sdkconfig.h"
10+
#include "soc/soc_caps.h"
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
#if SOC_USB_OTG_SUPPORTED && SOC_PM_SUPPORT_CNNT_PD
17+
/**
18+
* @brief Backup usb OTG phy bus_clock / stoppclk configuration and
19+
* before light sleep to avoid current leakage
20+
*/
21+
void sleep_usb_otg_phy_backup_and_disable(void);
22+
23+
/**
24+
* @brief Restore initial usb OTG phy configuration when wakeup from light sleep
25+
*/
26+
void sleep_usb_otg_phy_restore(void);
27+
#endif
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif

components/esp_hw_support/linker.lf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ entries:
2626
pmu_param (noflash)
2727
if SOC_USB_SERIAL_JTAG_SUPPORTED = y:
2828
sleep_console (noflash)
29+
if SOC_USB_OTG_SUPPORTED && SOC_PM_SUPPORT_CNNT_PD = y:
30+
sleep_usb (noflash)
2931
if IDF_TARGET_ESP32 = y || IDF_TARGET_ESP32S2 = y:
3032
rtc_wdt (noflash_text)
3133
if PERIPH_CTRL_FUNC_IN_IRAM = y:

components/esp_hw_support/sleep_modes.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -76,6 +76,7 @@
7676
#include "esp_private/sleep_console.h"
7777
#include "esp_private/sleep_cpu.h"
7878
#include "esp_private/sleep_modem.h"
79+
#include "esp_private/sleep_usb.h"
7980
#include "esp_private/esp_clk.h"
8081
#include "esp_private/esp_task_wdt.h"
8182
#include "esp_private/sar_periph_ctrl.h"
@@ -669,6 +670,11 @@ FORCE_INLINE_ATTR void misc_modules_sleep_prepare(uint32_t pd_flags, bool deep_s
669670
// Only avoid USJ pad leakage here, USB OTG pad leakage is prevented through USB Host driver.
670671
sleep_console_usj_pad_backup_and_disable();
671672
#endif
673+
#if SOC_USB_OTG_SUPPORTED && SOC_PM_SUPPORT_CNNT_PD
674+
if (!(pd_flags & PMU_SLEEP_PD_CNNT)) {
675+
sleep_usb_otg_phy_backup_and_disable();
676+
}
677+
#endif
672678
#if CONFIG_MAC_BB_PD
673679
mac_bb_power_down_cb_execute();
674680
#endif
@@ -720,6 +726,11 @@ FORCE_INLINE_ATTR void misc_modules_wake_prepare(uint32_t pd_flags)
720726
#if SOC_USB_SERIAL_JTAG_SUPPORTED && !SOC_USB_SERIAL_JTAG_SUPPORT_LIGHT_SLEEP
721727
sleep_console_usj_pad_restore();
722728
#endif
729+
#if SOC_USB_OTG_SUPPORTED && SOC_PM_SUPPORT_CNNT_PD
730+
if (!(pd_flags & PMU_SLEEP_PD_CNNT)) {
731+
sleep_usb_otg_phy_restore();
732+
}
733+
#endif
723734
#if !CONFIG_IDF_TARGET_ESP32C61 // TODO: IDF-9304
724735
sar_periph_ctrl_power_enable();
725736
#endif
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdbool.h>
8+
#include "soc/soc_caps.h"
9+
#include "esp_private/sleep_usb.h"
10+
#include "esp_attr.h"
11+
12+
#if SOC_USB_OTG_SUPPORTED && SOC_PM_SUPPORT_CNNT_PD
13+
#include "hal/usb_utmi_ll.h"
14+
#include "hal/usb_dwc_ll.h"
15+
16+
static bool s_usb_utmi_bus_clock_state, s_usb_utmi_stoppclk_state;
17+
18+
void sleep_usb_otg_phy_backup_and_disable(void)
19+
{
20+
s_usb_utmi_bus_clock_state = _usb_utmi_ll_bus_clock_is_enabled();
21+
if (!s_usb_utmi_bus_clock_state) {
22+
_usb_utmi_ll_enable_bus_clock(true);
23+
}
24+
s_usb_utmi_stoppclk_state = usb_dwc_ll_get_stoppclk_st(&USB_DWC_HS);
25+
usb_dwc_ll_set_stoppclk(&USB_DWC_HS, true);
26+
}
27+
28+
void sleep_usb_otg_phy_restore(void)
29+
{
30+
_usb_utmi_ll_enable_bus_clock(true);
31+
usb_dwc_ll_set_stoppclk(&USB_DWC_HS, s_usb_utmi_stoppclk_state);
32+
if (!s_usb_utmi_bus_clock_state) {
33+
_usb_utmi_ll_enable_bus_clock(false);
34+
}
35+
}
36+
#endif

components/hal/esp32p4/include/hal/usb_dwc_ll.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/*
2-
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2020-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

77
#pragma once
88

9+
#include <stdlib.h>
910
#include <stdint.h>
1011
#include <stdbool.h>
12+
#include "esp_attr.h"
1113
#include "soc/usb_dwc_struct.h"
1214
#include "hal/usb_dwc_types.h"
1315
#include "hal/misc.h"
@@ -987,6 +989,17 @@ static inline void usb_dwc_ll_qtd_get_status(usb_dwc_ll_dma_qtd_t *qtd, int *rem
987989
qtd->buffer_status_val = 0;
988990
}
989991

992+
// ---------------------------- Power and Clock Gating Register --------------------------------
993+
FORCE_INLINE_ATTR void usb_dwc_ll_set_stoppclk(usb_dwc_dev_t *hw, bool stop)
994+
{
995+
hw->pcgcctl_reg.stoppclk = stop;
996+
}
997+
998+
FORCE_INLINE_ATTR bool usb_dwc_ll_get_stoppclk_st(usb_dwc_dev_t *hw)
999+
{
1000+
return hw->pcgcctl_reg.stoppclk;
1001+
}
1002+
9901003
#ifdef __cplusplus
9911004
}
9921005
#endif

components/hal/esp32p4/include/hal/usb_utmi_ll.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -50,6 +50,16 @@ FORCE_INLINE_ATTR void _usb_utmi_ll_enable_bus_clock(bool clk_en)
5050
// HP_SYS_CLKRST.soc_clk_ctrlx and LP_AON_CLKRST.hp_usb_clkrst_ctrlx are shared registers, so this function must be used in an atomic way
5151
#define usb_utmi_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; _usb_utmi_ll_enable_bus_clock(__VA_ARGS__)
5252

53+
/**
54+
* Get the enable status of the USB UTMI PHY bus clock
55+
*
56+
* @return Return true if USB UTMI PHY bus clock is enabled
57+
*/
58+
FORCE_INLINE_ATTR bool _usb_utmi_ll_bus_clock_is_enabled(void)
59+
{
60+
return (HP_SYS_CLKRST.soc_clk_ctrl1.reg_usb_otg20_sys_clk_en && LP_AON_CLKRST.hp_usb_clkrst_ctrl1.usb_otg20_phyref_clk_en);
61+
}
62+
5363
/**
5464
* @brief Reset the USB UTMI PHY and USB_DWC_HS controller
5565
*/

0 commit comments

Comments
 (0)