Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_lightsleep_current_leakage_on_usj_pad_v5.2' …
Browse files Browse the repository at this point in the history
…into 'release/v5.2'

fix(esp_hw_support): fix lightsleep current leakage on usb pad (backport v5.2)

See merge request espressif/esp-idf!27199
  • Loading branch information
jack0c committed Nov 17, 2023
2 parents 7f0f299 + b7f1aa5 commit 1cdca25
Show file tree
Hide file tree
Showing 15 changed files with 438 additions and 14 deletions.
1 change: 1 addition & 0 deletions components/bootloader_support/src/bootloader_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ void bootloader_console_init(void)
esp_rom_uart_set_as_console(ESP_ROM_USB_OTG_NUM);
esp_rom_install_channel_putc(1, bootloader_console_write_char_usb);
#if SOC_USB_SERIAL_JTAG_SUPPORTED
usb_phy_ll_usb_wrap_pad_enable(&USB_WRAP, true);
usb_phy_ll_int_otg_enable(&USB_WRAP);
#endif
}
Expand Down
14 changes: 14 additions & 0 deletions components/driver/usb_serial_jtag/usb_serial_jtag.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
#include "esp_intr_alloc.h"
#include "driver/usb_serial_jtag.h"
#include "soc/periph_defs.h"
#include "soc/soc_caps.h"
#include "esp_check.h"
#include "esp_private/periph_ctrl.h"

#if !SOC_RCC_IS_INDEPENDENT
#define USJ_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
#else
#define USJ_RCC_ATOMIC()
#endif

// The hardware buffer max size is 64
#define USB_SER_JTAG_ENDP_SIZE (64)
Expand Down Expand Up @@ -151,6 +159,11 @@ esp_err_t usb_serial_jtag_driver_install(usb_serial_jtag_driver_config_t *usb_se
goto _exit;
}

// Enable USB-Serial-JTAG peripheral module clock
USJ_RCC_ATOMIC() {
usb_serial_jtag_ll_enable_bus_clock(true);
}

// Configure PHY
usb_phy_ll_int_jtag_enable(&USB_SERIAL_JTAG);

Expand Down Expand Up @@ -214,6 +227,7 @@ esp_err_t usb_serial_jtag_driver_uninstall(void)
return ESP_OK;
}

/* Not disable the module clock and usb_pad_enable here since the USJ stdout might still depends on it. */
//Disable tx/rx interrupt.
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
esp_intr_free(p_usb_serial_jtag_obj->intr_handle);
Expand Down
1 change: 1 addition & 0 deletions components/esp_hw_support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ if(NOT BOOTLOADER_BUILD)
"revision.c"
"rtc_module.c"
"sleep_modes.c"
"sleep_console.c"
"sleep_gpio.c"
"sleep_event.c"
"sleep_modem.c"
Expand Down
35 changes: 35 additions & 0 deletions components/esp_hw_support/include/esp_private/sleep_console.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once
#include <stdint.h>
#include "sdkconfig.h"

#ifdef __cplusplus
extern "C" {
#endif

#if SOC_USB_SERIAL_JTAG_SUPPORTED
typedef struct {
bool usj_clock_enabled;
bool usj_pad_enabled;
} sleep_console_usj_enable_state_t;

/**
* @brief Disable usb-serial-jtag pad during light sleep to avoid current leakage and
* backup the enable state before light sleep
*/
void sleep_console_usj_pad_backup_and_disable(void);

/**
* @brief Restore initial usb-serial-jtag pad enable state when wakeup from light sleep
*/
void sleep_console_usj_pad_restore(void);
#endif

#ifdef __cplusplus
}
#endif
2 changes: 2 additions & 0 deletions components/esp_hw_support/linker.lf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ entries:
rtc_time (noflash_text)
if SOC_PMU_SUPPORTED = y:
pmu_sleep (noflash)
if SOC_USB_SERIAL_JTAG_SUPPORTED = y:
sleep_console (noflash)
if IDF_TARGET_ESP32 = y || IDF_TARGET_ESP32S2 = y:
rtc_wdt (noflash_text)
if PERIPH_CTRL_FUNC_IN_IRAM = y:
Expand Down
48 changes: 48 additions & 0 deletions components/esp_hw_support/sleep_console.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdbool.h>
#include "soc/soc_caps.h"
#include "esp_private/sleep_console.h"
#include "esp_attr.h"

#if SOC_USB_SERIAL_JTAG_SUPPORTED
#include "hal/usb_serial_jtag_ll.h"

static sleep_console_usj_enable_state_t s_usj_state = {0};

void sleep_console_usj_pad_backup_and_disable(void)
{
// This function can be called in sleep process only, and sleep process code
// is in critical region and thread safe already, so to avoid build errors/warnings
// declare __DECLARE_RCC_ATOMIC_ENV here.
int __DECLARE_RCC_ATOMIC_ENV __attribute__ ((unused));

s_usj_state.usj_clock_enabled = usb_serial_jtag_ll_module_is_enabled();
if (!s_usj_state.usj_clock_enabled) {
// Enable USJ clock and clear reset
usb_serial_jtag_ll_enable_bus_clock(true);
usb_serial_jtag_ll_reset_register();
}
s_usj_state.usj_pad_enabled = usb_serial_jtag_ll_pad_backup_and_disable();
// Disable USJ clock
usb_serial_jtag_ll_enable_bus_clock(false);
}

void sleep_console_usj_pad_restore(void)
{
// This function can be called in sleep process only, and sleep process code
// is in critical region and thread safe already, so to avoid build errors/warnings
// declare __DECLARE_RCC_ATOMIC_ENV here.
int __DECLARE_RCC_ATOMIC_ENV __attribute__ ((unused));

usb_serial_jtag_ll_enable_bus_clock(true);
usb_serial_jtag_ll_enable_pad(s_usj_state.usj_pad_enabled);
if (!s_usj_state.usj_clock_enabled) {
usb_serial_jtag_ll_enable_bus_clock(false);
}
}
#endif
8 changes: 8 additions & 0 deletions components/esp_hw_support/sleep_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include "esp_rom_uart.h"
#include "esp_rom_sys.h"
#include "esp_private/brownout.h"
#include "esp_private/sleep_console.h"
#include "esp_private/sleep_cpu.h"
#include "esp_private/sleep_modem.h"
#include "esp_private/esp_clk.h"
Expand Down Expand Up @@ -538,6 +539,10 @@ FORCE_INLINE_ATTR void misc_modules_sleep_prepare(bool deep_sleep)
}
}
} else {
#if SOC_USB_SERIAL_JTAG_SUPPORTED && !SOC_USB_SERIAL_JTAG_SUPPORT_LIGHT_SLEEP
// Only avoid USJ pad leakage here, USB OTG pad leakage is prevented through USB Host driver.
sleep_console_usj_pad_backup_and_disable();
#endif
#if CONFIG_MAC_BB_PD
mac_bb_power_down_cb_execute();
#endif
Expand Down Expand Up @@ -566,6 +571,9 @@ FORCE_INLINE_ATTR void misc_modules_sleep_prepare(bool deep_sleep)
*/
FORCE_INLINE_ATTR void misc_modules_wake_prepare(void)
{
#if SOC_USB_SERIAL_JTAG_SUPPORTED && !SOC_USB_SERIAL_JTAG_SUPPORT_LIGHT_SLEEP
sleep_console_usj_pad_restore();
#endif
#if SOC_PM_RETENTION_HAS_REGDMA_POWER_BUG
sleep_retention_do_system_retention(false);
#endif
Expand Down
64 changes: 63 additions & 1 deletion components/hal/esp32c3/include/hal/usb_serial_jtag_ll.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

// The LL layer of the USB-serial-jtag controller

#pragma once
#include <stdbool.h>
#include "esp_attr.h"
#include "soc/usb_serial_jtag_reg.h"
#include "soc/usb_serial_jtag_struct.h"
#include "soc/system_struct.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -168,6 +171,65 @@ static inline void usb_serial_jtag_ll_txfifo_flush(void)
USB_SERIAL_JTAG.ep1_conf.wr_done=1;
}

/**
* @brief Disable usb serial jtag pad during light sleep to avoid current leakage
*
* @return Initial configuration of usb serial jtag pad enable before light sleep
*/
FORCE_INLINE_ATTR bool usb_serial_jtag_ll_pad_backup_and_disable(void)
{
bool pad_enabled = USB_SERIAL_JTAG.conf0.usb_pad_enable;

// Disable USB pad function
USB_SERIAL_JTAG.conf0.usb_pad_enable = 0;

return pad_enabled;
}

/**
* @brief Enable the internal USJ PHY control to D+/D- pad
*
* @param enable_pad Enable the USJ PHY control to D+/D- pad
*/
FORCE_INLINE_ATTR void usb_serial_jtag_ll_enable_pad(bool enable_pad)
{
USB_SERIAL_JTAG.conf0.usb_pad_enable = enable_pad;
}

/**
* @brief Enable the bus clock for USB Serial_JTAG module
* @param clk_en True if enable the clock of USB Serial_JTAG module
*/
FORCE_INLINE_ATTR void usb_serial_jtag_ll_enable_bus_clock(bool clk_en)
{
SYSTEM.perip_clk_en0.reg_usb_device_clk_en = clk_en;
}

// SYSTEM.perip_clk_enx are shared registers, so this function must be used in an atomic way
#define usb_serial_jtag_ll_enable_bus_clock(...) (void)__DECLARE_RCC_ATOMIC_ENV; usb_serial_jtag_ll_enable_bus_clock(__VA_ARGS__)

/**
* @brief Reset the usb serial jtag module
*/
FORCE_INLINE_ATTR void usb_serial_jtag_ll_reset_register(void)
{
SYSTEM.perip_rst_en0.reg_usb_device_rst = 1;
SYSTEM.perip_rst_en0.reg_usb_device_rst = 0;
}

// SYSTEM.perip_clk_enx are shared registers, so this function must be used in an atomic way
#define usb_serial_jtag_ll_reset_register(...) (void)__DECLARE_RCC_ATOMIC_ENV; usb_serial_jtag_ll_reset_register(__VA_ARGS__)

/**
* Get the enable status USB Serial_JTAG module
*
* @return Return true if USB Serial_JTAG module is enabled
*/
FORCE_INLINE_ATTR bool usb_serial_jtag_ll_module_is_enabled(void)
{
return (SYSTEM.perip_clk_en0.reg_usb_device_clk_en && !SYSTEM.perip_rst_en0.reg_usb_device_rst);
}


#ifdef __cplusplus
}
Expand Down
56 changes: 56 additions & 0 deletions components/hal/esp32c6/include/hal/usb_serial_jtag_ll.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// The LL layer of the USB-serial-jtag controller

#pragma once
#include <stdbool.h>
#include "esp_attr.h"
#include "soc/pcr_struct.h"
#include "soc/usb_serial_jtag_reg.h"
#include "soc/usb_serial_jtag_struct.h"

Expand Down Expand Up @@ -169,6 +172,59 @@ static inline void usb_serial_jtag_ll_txfifo_flush(void)
}


/**
* @brief Disable usb serial jtag pad during light sleep to avoid current leakage
*
* @return Initial configuration of usb serial jtag pad enable before light sleep
*/
FORCE_INLINE_ATTR bool usb_serial_jtag_ll_pad_backup_and_disable(void)
{
bool pad_enabled = USB_SERIAL_JTAG.conf0.usb_pad_enable;

// Disable USB pad function
USB_SERIAL_JTAG.conf0.usb_pad_enable = 0;

return pad_enabled;
}

/**
* @brief Enable the internal USJ PHY control to D+/D- pad
*
* @param enable_pad Enable the USJ PHY control to D+/D- pad
*/
FORCE_INLINE_ATTR void usb_serial_jtag_ll_enable_pad(bool enable_pad)
{
USB_SERIAL_JTAG.conf0.usb_pad_enable = enable_pad;
}

/**
* @brief Enable the bus clock for USB Serial_JTAG module
* @param clk_en True if enable the clock of USB Serial_JTAG module
*/
FORCE_INLINE_ATTR void usb_serial_jtag_ll_enable_bus_clock(bool clk_en)
{
PCR.usb_device_conf.usb_device_clk_en = clk_en;
}

/**
* @brief Reset the usb serial jtag module
*/
FORCE_INLINE_ATTR void usb_serial_jtag_ll_reset_register(void)
{
PCR.usb_device_conf.usb_device_rst_en = 1;
PCR.usb_device_conf.usb_device_rst_en = 0;
}

/**
* Get the enable status USB Serial_JTAG module
*
* @return Return true if USB Serial_JTAG module is enabled
*/
FORCE_INLINE_ATTR bool usb_serial_jtag_ll_module_is_enabled(void)
{
return (PCR.usb_device_conf.usb_device_clk_en && !PCR.usb_device_conf.usb_device_rst_en);
}

#ifdef __cplusplus
}
#endif
55 changes: 55 additions & 0 deletions components/hal/esp32h2/include/hal/usb_serial_jtag_ll.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// The LL layer of the USB-serial-jtag controller

#pragma once
#include <stdbool.h>
#include "esp_attr.h"
#include "soc/pcr_struct.h"
#include "soc/usb_serial_jtag_reg.h"
#include "soc/usb_serial_jtag_struct.h"

Expand Down Expand Up @@ -168,6 +171,58 @@ static inline void usb_serial_jtag_ll_txfifo_flush(void)
USB_SERIAL_JTAG.ep1_conf.wr_done=1;
}

/**
* @brief Disable usb serial jtag pad during light sleep to avoid current leakage
*
* @return Initial configuration of usb serial jtag pad enable before light sleep
*/
FORCE_INLINE_ATTR bool usb_serial_jtag_ll_pad_backup_and_disable(void)
{
bool pad_enabled = USB_SERIAL_JTAG.conf0.usb_pad_enable;

// Disable USB pad function
USB_SERIAL_JTAG.conf0.usb_pad_enable = 0;

return pad_enabled;
}

/**
* @brief Enable the internal USJ PHY control to D+/D- pad
*
* @param enable_pad Enable the USJ PHY control to D+/D- pad
*/
FORCE_INLINE_ATTR void usb_serial_jtag_ll_enable_pad(bool enable_pad)
{
USB_SERIAL_JTAG.conf0.usb_pad_enable = enable_pad;
}

/**
* @brief Enable the bus clock for USB Serial_JTAG module
* @param clk_en True if enable the clock of USB Serial_JTAG module
*/
FORCE_INLINE_ATTR void usb_serial_jtag_ll_enable_bus_clock(bool clk_en)
{
PCR.usb_device_conf.usb_device_clk_en = clk_en;
}

/**
* @brief Reset the usb serial jtag module
*/
FORCE_INLINE_ATTR void usb_serial_jtag_ll_reset_register(void)
{
PCR.usb_device_conf.usb_device_rst_en = 1;
PCR.usb_device_conf.usb_device_rst_en = 0;
}

/**
* Get the enable status USB Serial_JTAG module
*
* @return Return true if USB Serial_JTAG module is enabled
*/
FORCE_INLINE_ATTR bool usb_serial_jtag_ll_module_is_enabled(void)
{
return (PCR.usb_device_conf.usb_device_clk_en && !PCR.usb_device_conf.usb_device_rst_en);
}

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 1cdca25

Please sign in to comment.