Skip to content

Commit 34f249a

Browse files
committed
feat(esp_hw_support): support esp_perip_clk_init for esp32c5
1 parent 904d952 commit 34f249a

File tree

14 files changed

+402
-110
lines changed

14 files changed

+402
-110
lines changed

components/esp_hw_support/port/esp32c5/esp_clk_tree.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -11,6 +11,7 @@
1111
#include "soc/rtc.h"
1212
#include "hal/clk_tree_hal.h"
1313
#include "hal/clk_tree_ll.h"
14+
#include "hal/clk_gate_ll.h"
1415
#include "esp_private/esp_clk_tree_common.h"
1516

1617
static const char *TAG = "esp_clk_tree";
@@ -69,6 +70,38 @@ uint32_t *freq_value)
6970

7071
esp_err_t esp_clk_tree_enable_src(soc_module_clk_t clk_src, bool enable)
7172
{
72-
(void)clk_src; (void)enable;
73+
PERIPH_RCC_ATOMIC() {
74+
switch (clk_src) {
75+
case SOC_MOD_CLK_PLL_F12M:
76+
clk_gate_ll_ref_12m_clk_en(enable);
77+
break;
78+
case SOC_MOD_CLK_PLL_F20M:
79+
clk_gate_ll_ref_20m_clk_en(enable);
80+
break;
81+
case SOC_MOD_CLK_PLL_F40M:
82+
clk_gate_ll_ref_40m_clk_en(enable);
83+
break;
84+
case SOC_MOD_CLK_PLL_F48M:
85+
clk_gate_ll_ref_48m_clk_en(enable);
86+
break;
87+
case SOC_MOD_CLK_PLL_F60M:
88+
clk_gate_ll_ref_60m_clk_en(enable);
89+
break;
90+
case SOC_MOD_CLK_PLL_F80M:
91+
clk_gate_ll_ref_80m_clk_en(enable);
92+
break;
93+
case SOC_MOD_CLK_PLL_F120M:
94+
clk_gate_ll_ref_120m_clk_en(enable);
95+
break;
96+
case SOC_MOD_CLK_PLL_F160M:
97+
clk_gate_ll_ref_160m_clk_en(enable);
98+
break;
99+
case SOC_MOD_CLK_PLL_F240M:
100+
clk_gate_ll_ref_240m_clk_en(enable);
101+
break;
102+
default:
103+
break;
104+
}
105+
}
73106
return ESP_OK;
74107
}

components/esp_hw_support/port/esp32c5/io_mux.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "sdkconfig.h"
88
#include "freertos/FreeRTOS.h"
9+
#include "esp_private/esp_clk_tree_common.h"
910
#include "esp_private/io_mux.h"
1011
#include "esp_private/periph_ctrl.h"
1112
#include "hal/gpio_ll.h"
@@ -40,6 +41,7 @@ esp_err_t io_mux_set_clock_source(soc_module_clk_t clk_src)
4041
return ESP_ERR_INVALID_STATE;
4142
}
4243

44+
esp_clk_tree_enable_src((soc_module_clk_t)clk_src, true);
4345
gpio_ll_iomux_set_clk_src(clk_src);
4446

4547
return ESP_OK;

components/esp_hw_support/port/esp32c5/rtc_time.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
6464
rtc_dig_clk8m_enable();
6565
}
6666
}
67-
67+
clk_ll_enable_timergroup_rtc_calibration_clock(true);
6868
/* There may be another calibration process already running during we call this function,
6969
* so we should wait the last process is done.
7070
*/
@@ -123,6 +123,7 @@ static uint32_t rtc_clk_cal_internal(rtc_cal_sel_t cal_clk, uint32_t slowclk_cyc
123123
}
124124
CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
125125

126+
clk_ll_enable_timergroup_rtc_calibration_clock(false);
126127
/* if dig_32k_xtal was originally off and enabled due to calibration, then set back to off state */
127128
if (cal_clk == RTC_CAL_32K_XTAL && !dig_32k_xtal_enabled) {
128129
clk_ll_xtal32k_digi_disable();

components/esp_system/port/soc/esp32c5/clk.c

Lines changed: 133 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,38 @@
2727
#if SOC_MODEM_CLOCK_SUPPORTED
2828
#include "hal/modem_lpcon_ll.h"
2929
#endif
30+
#include "hal/adc_ll.h"
31+
#include "hal/aes_ll.h"
32+
#include "hal/assist_debug_ll.h"
33+
#include "hal/apm_ll.h"
34+
#include "hal/clk_gate_ll.h"
35+
#include "hal/clk_tree_ll.h"
36+
#include "hal/ds_ll.h"
37+
#include "hal/ecc_ll.h"
38+
#include "hal/etm_ll.h"
39+
#include "hal/gdma_ll.h"
40+
#include "hal/hmac_ll.h"
41+
#include "hal/i2c_ll.h"
42+
#include "hal/i2s_ll.h"
43+
#include "hal/ledc_ll.h"
44+
#include "hal/lp_core_ll.h"
45+
#include "hal/lp_clkrst_ll.h"
46+
#include "hal/mcpwm_ll.h"
47+
#include "hal/mpi_ll.h"
48+
#include "hal/mspi_ll.h"
49+
#include "hal/parlio_ll.h"
50+
#include "hal/pau_ll.h"
51+
#include "hal/pcnt_ll.h"
52+
#include "hal/rmt_ll.h"
53+
#include "hal/rtc_io_ll.h"
54+
#include "hal/sha_ll.h"
55+
#include "hal/spi_ll.h"
56+
#include "hal/temperature_sensor_ll.h"
57+
#include "hal/timer_ll.h"
58+
#include "hal/twaifd_ll.h"
59+
#include "hal/uart_ll.h"
60+
#include "hal/uhci_ll.h"
61+
#include "hal/usb_serial_jtag_ll.h"
3062
#include "esp_private/esp_sleep_internal.h"
3163
#include "esp_private/esp_modem_clock.h"
3264
#include "esp_private/periph_ctrl.h"
@@ -200,23 +232,19 @@ void rtc_clk_select_rtc_slow_clk(void)
200232
*/
201233
__attribute__((weak)) void esp_perip_clk_init(void)
202234
{
203-
// TODO: [ESP32C5] IDF-8844
204-
#if SOC_MODEM_CLOCK_SUPPORTED
205-
// modem_clock_domain_pmu_state_icg_map_init();
206235
/* During system initialization, the low-power clock source of the modem
207236
* (WiFi, BLE or Coexist) follows the configuration of the slow clock source
208237
* of the system. If the WiFi, BLE or Coexist module needs a higher
209238
* precision sleep clock (for example, the BLE needs to use the main XTAL
210239
* oscillator (40 MHz) to provide the clock during the sleep process in some
211240
* scenarios), the module needs to switch to the required clock source by
212-
* itself. */ //TODO - WIFI-5233
241+
* itself. */
213242
soc_rtc_slow_clk_src_t rtc_slow_clk_src = rtc_clk_slow_src_get();
214243
modem_clock_lpclk_src_t modem_lpclk_src = (modem_clock_lpclk_src_t)(
215244
(rtc_slow_clk_src == SOC_RTC_SLOW_CLK_SRC_XTAL32K) ? MODEM_CLOCK_LPCLK_SRC_XTAL32K
216245
: (rtc_slow_clk_src == SOC_RTC_SLOW_CLK_SRC_OSC_SLOW) ? MODEM_CLOCK_LPCLK_SRC_EXT32K
217246
: MODEM_CLOCK_LPCLK_SRC_RC_SLOW);
218247
modem_clock_select_lp_clock_source(PERIPH_WIFI_MODULE, modem_lpclk_src, 0);
219-
#endif
220248

221249
/* On ESP32-C5 ECO1, clearing BIT(31) of PCR_FPGA_DEBUG_REG is used to fix
222250
* the issue where the modem module fails to transmit and receive packets
@@ -225,102 +253,112 @@ __attribute__((weak)) void esp_perip_clk_init(void)
225253
* to IDF-11064. */
226254
clk_ll_soc_root_clk_auto_gating_bypass(true);
227255

228-
ESP_EARLY_LOGW(TAG, "esp_perip_clk_init() has not been implemented yet");
229-
#if 0 // TODO: [ESP32C5] IDF-8844
230-
uint32_t common_perip_clk, hwcrypto_perip_clk, wifi_bt_sdio_clk = 0;
231-
uint32_t common_perip_clk1 = 0;
232-
233256
soc_reset_reason_t rst_reason = esp_rom_get_reset_reason(0);
234-
235-
/* For reason that only reset CPU, do not disable the clocks
236-
* that have been enabled before reset.
237-
*/
238-
if (rst_reason == RESET_REASON_CPU0_MWDT0 || rst_reason == RESET_REASON_CPU0_SW ||
239-
rst_reason == RESET_REASON_CPU0_RTC_WDT || rst_reason == RESET_REASON_CPU0_MWDT1) {
240-
common_perip_clk = ~READ_PERI_REG(SYSTEM_PERIP_CLK_EN0_REG);
241-
hwcrypto_perip_clk = ~READ_PERI_REG(SYSTEM_PERIP_CLK_EN1_REG);
242-
wifi_bt_sdio_clk = ~READ_PERI_REG(SYSTEM_WIFI_CLK_EN_REG);
243-
} else {
244-
common_perip_clk = SYSTEM_WDG_CLK_EN |
245-
SYSTEM_I2S0_CLK_EN |
257+
if ((rst_reason != RESET_REASON_CPU0_MWDT0) && (rst_reason != RESET_REASON_CPU0_MWDT1) \
258+
&& (rst_reason != RESET_REASON_CPU0_SW) && (rst_reason != RESET_REASON_CPU0_RTC_WDT) \
259+
&& (rst_reason != RESET_REASON_CPU0_JTAG) && (rst_reason != RESET_REASON_CPU0_LOCKUP)) {
246260
#if CONFIG_ESP_CONSOLE_UART_NUM != 0
247-
SYSTEM_UART_CLK_EN |
261+
uart_ll_enable_bus_clock(UART_NUM_0, false);
262+
uart_ll_sclk_disable(&UART0);
263+
#elif CONFIG_ESP_CONSOLE_UART_NUM != 1
264+
uart_ll_sclk_disable(&UART1);
265+
uart_ll_enable_bus_clock(UART_NUM_1, false);
248266
#endif
249-
#if CONFIG_ESP_CONSOLE_UART_NUM != 1
250-
SYSTEM_UART1_CLK_EN |
267+
i2c_ll_enable_bus_clock(0, false);
268+
i2c_ll_enable_controller_clock(&I2C0, false);
269+
rmt_ll_enable_bus_clock(0, false);
270+
rmt_ll_enable_group_clock(0, false);
271+
ledc_ll_enable_clock(&LEDC, false);
272+
ledc_ll_enable_bus_clock(false);
273+
clk_ll_enable_timergroup_rtc_calibration_clock(false);
274+
timer_ll_enable_clock(0, 0, false);
275+
timer_ll_enable_clock(1, 0, false);
276+
_timer_ll_enable_bus_clock(0, false);
277+
_timer_ll_enable_bus_clock(1, false);
278+
twaifd_ll_enable_clock(0, false);
279+
twaifd_ll_enable_bus_clock(0, false);
280+
twaifd_ll_enable_clock(1, false);
281+
twaifd_ll_enable_bus_clock(1, false);
282+
i2s_ll_enable_bus_clock(0, false);
283+
i2s_ll_tx_disable_clock(&I2S0);
284+
i2s_ll_rx_disable_clock(&I2S0);
285+
adc_ll_enable_bus_clock(false);
286+
pcnt_ll_enable_bus_clock(0, false);
287+
etm_ll_enable_bus_clock(0, false);
288+
mcpwm_ll_enable_bus_clock(0, false);
289+
mcpwm_ll_group_enable_clock(0, false);
290+
parlio_ll_rx_enable_clock(&PARL_IO, false);
291+
parlio_ll_tx_enable_clock(&PARL_IO, false);
292+
parlio_ll_enable_bus_clock(0, false);
293+
ahb_dma_ll_force_enable_reg_clock(&AHB_DMA, false);
294+
_gdma_ll_enable_bus_clock(0, false);
295+
#if CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
296+
mspi_timing_ll_enable_core_clock(0, false);
251297
#endif
252-
SYSTEM_SPI2_CLK_EN |
253-
SYSTEM_I2C_EXT0_CLK_EN |
254-
SYSTEM_UHCI0_CLK_EN |
255-
SYSTEM_RMT_CLK_EN |
256-
SYSTEM_LEDC_CLK_EN |
257-
SYSTEM_TIMERGROUP1_CLK_EN |
258-
SYSTEM_SPI3_CLK_EN |
259-
SYSTEM_SPI4_CLK_EN |
260-
SYSTEM_TWAI_CLK_EN |
261-
SYSTEM_I2S0_CLK_EN |
262-
SYSTEM_SPI2_DMA_CLK_EN |
263-
SYSTEM_SPI3_DMA_CLK_EN;
264-
265-
common_perip_clk1 = 0;
266-
hwcrypto_perip_clk = SYSTEM_CRYPTO_AES_CLK_EN |
267-
SYSTEM_CRYPTO_SHA_CLK_EN |
268-
SYSTEM_CRYPTO_RSA_CLK_EN;
269-
wifi_bt_sdio_clk = SYSTEM_WIFI_CLK_WIFI_EN |
270-
SYSTEM_WIFI_CLK_BT_EN_M |
271-
SYSTEM_WIFI_CLK_UNUSED_BIT5 |
272-
SYSTEM_WIFI_CLK_UNUSED_BIT12;
273-
}
274-
275-
//Reset the communication peripherals like I2C, SPI, UART, I2S and bring them to known state.
276-
common_perip_clk |= SYSTEM_I2S0_CLK_EN |
277-
#if CONFIG_ESP_CONSOLE_UART_NUM != 0
278-
SYSTEM_UART_CLK_EN |
298+
spi_ll_enable_bus_clock(SPI2_HOST, false);
299+
temperature_sensor_ll_bus_clk_enable(false);
300+
pau_ll_enable_bus_clock(false);
301+
#if !CONFIG_ESP_SYSTEM_HW_PC_RECORD
302+
/* Disable ASSIST Debug module clock if PC recoreding function is not used,
303+
* if stack guard function needs it, it will be re-enabled at esp_hw_stack_guard_init */
304+
assist_debug_ll_enable_bus_clock(false);
279305
#endif
280-
#if CONFIG_ESP_CONSOLE_UART_NUM != 1
281-
SYSTEM_UART1_CLK_EN |
306+
mpi_ll_enable_bus_clock(false);
307+
aes_ll_enable_bus_clock(false);
308+
sha_ll_enable_bus_clock(false);
309+
ecc_ll_enable_bus_clock(false);
310+
hmac_ll_enable_bus_clock(false);
311+
ds_ll_enable_bus_clock(false);
312+
apm_tee_ll_clk_gating_enable(false);
313+
uhci_ll_enable_bus_clock(0, false);
314+
315+
// TODO: Replace with hal implementation
316+
REG_CLR_BIT(HP_APM_CLOCK_GATE_REG, HP_APM_CLK_EN);
317+
REG_CLR_BIT(LP_TEE_CLOCK_GATE_REG, LP_TEE_CLK_EN);
318+
REG_CLR_BIT(PCR_TRACE_CONF_REG, PCR_TRACE_CLK_EN);
319+
REG_CLR_BIT(PCR_TCM_MEM_MONITOR_CONF_REG, PCR_TCM_MEM_MONITOR_CLK_EN);
320+
REG_CLR_BIT(PCR_PSRAM_MEM_MONITOR_CONF_REG, PCR_PSRAM_MEM_MONITOR_CLK_EN);
321+
REG_CLR_BIT(PCR_PVT_MONITOR_CONF_REG, PCR_PVT_MONITOR_CLK_EN);
322+
REG_CLR_BIT(PCR_PVT_MONITOR_FUNC_CLK_CONF_REG, PCR_PVT_MONITOR_FUNC_CLK_EN);
323+
WRITE_PERI_REG(PCR_CTRL_CLK_OUT_EN_REG, 0);
324+
325+
#if !CONFIG_USJ_ENABLE_USB_SERIAL_JTAG && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED
326+
// Disable USB-Serial-JTAG clock and it's pad if not used
327+
usb_serial_jtag_ll_phy_enable_pad(false);
328+
usb_serial_jtag_ll_enable_bus_clock(false);
329+
usb_serial_jtag_ll_enable_mem_clock(false);
330+
usb_serial_jtag_ll_set_mem_pd(true);
282331
#endif
283-
SYSTEM_SPI2_CLK_EN |
284-
SYSTEM_I2C_EXT0_CLK_EN |
285-
SYSTEM_UHCI0_CLK_EN |
286-
SYSTEM_RMT_CLK_EN |
287-
SYSTEM_UHCI1_CLK_EN |
288-
SYSTEM_SPI3_CLK_EN |
289-
SYSTEM_SPI4_CLK_EN |
290-
SYSTEM_I2C_EXT1_CLK_EN |
291-
SYSTEM_I2S0_CLK_EN |
292-
SYSTEM_SPI2_DMA_CLK_EN |
293-
SYSTEM_SPI3_DMA_CLK_EN;
294-
common_perip_clk1 = 0;
295332

296-
/* Change I2S clock to audio PLL first. Because if I2S uses 160MHz clock,
297-
* the current is not reduced when disable I2S clock.
298-
*/
299-
// TOCK(check replacement)
300-
// REG_SET_FIELD(I2S_CLKM_CONF_REG(0), I2S_CLK_SEL, I2S_CLK_AUDIO_PLL);
301-
// REG_SET_FIELD(I2S_CLKM_CONF_REG(1), I2S_CLK_SEL, I2S_CLK_AUDIO_PLL);
302-
303-
/* Disable some peripheral clocks. */
304-
CLEAR_PERI_REG_MASK(SYSTEM_PERIP_CLK_EN0_REG, common_perip_clk);
305-
SET_PERI_REG_MASK(SYSTEM_PERIP_RST_EN0_REG, common_perip_clk);
306-
307-
CLEAR_PERI_REG_MASK(SYSTEM_PERIP_CLK_EN1_REG, common_perip_clk1);
308-
SET_PERI_REG_MASK(SYSTEM_PERIP_RST_EN1_REG, common_perip_clk1);
309-
310-
/* Disable hardware crypto clocks. */
311-
CLEAR_PERI_REG_MASK(SYSTEM_PERIP_CLK_EN1_REG, hwcrypto_perip_clk);
312-
SET_PERI_REG_MASK(SYSTEM_PERIP_RST_EN1_REG, hwcrypto_perip_clk);
313-
314-
/* Disable WiFi/BT/SDIO clocks. */
315-
CLEAR_PERI_REG_MASK(SYSTEM_WIFI_CLK_EN_REG, wifi_bt_sdio_clk);
316-
SET_PERI_REG_MASK(SYSTEM_WIFI_CLK_EN_REG, SYSTEM_WIFI_CLK_EN);
317-
318-
/* Set WiFi light sleep clock source to RTC slow clock */
319-
REG_SET_FIELD(SYSTEM_BT_LPCK_DIV_INT_REG, SYSTEM_BT_LPCK_DIV_NUM, 0);
320-
CLEAR_PERI_REG_MASK(SYSTEM_BT_LPCK_DIV_FRAC_REG, SYSTEM_LPCLK_SEL_8M);
321-
SET_PERI_REG_MASK(SYSTEM_BT_LPCK_DIV_FRAC_REG, SYSTEM_LPCLK_SEL_RTC_SLOW);
333+
if (clk_ll_cpu_get_src() != SOC_CPU_CLK_SRC_PLL_F240M) {
334+
_clk_gate_ll_ref_240m_clk_en(false);
335+
}
336+
if (clk_ll_cpu_get_src() != SOC_CPU_CLK_SRC_PLL_F160M) {
337+
_clk_gate_ll_ref_160m_clk_en(false);
338+
}
339+
_clk_gate_ll_ref_120m_clk_en(false);
340+
_clk_gate_ll_ref_80m_clk_en(false);
341+
_clk_gate_ll_ref_60m_clk_en(false);
342+
_clk_gate_ll_ref_40m_clk_en(false);
343+
_clk_gate_ll_ref_20m_clk_en(false);
344+
_clk_gate_ll_ref_12m_clk_en(false);
345+
}
322346

323-
/* Enable RNG clock. */
324-
periph_module_enable(PERIPH_RNG_MODULE);
325-
#endif
347+
if ((rst_reason == RESET_REASON_CHIP_POWER_ON) || (rst_reason == RESET_REASON_CHIP_BROWN_OUT) \
348+
|| (rst_reason == RESET_REASON_SYS_RTC_WDT) || (rst_reason == RESET_REASON_SYS_SUPER_WDT) \
349+
|| (rst_reason == RESET_REASON_CORE_PWR_GLITCH)) {
350+
_lp_i2c_ll_enable_bus_clock(0, false);
351+
lp_uart_ll_sclk_disable(0);
352+
_lp_uart_ll_enable_bus_clock(0, false);
353+
_lp_core_ll_enable_bus_clock(false);
354+
_rtcio_ll_enable_io_clock(false);
355+
_lp_clkrst_ll_enable_rng_clock(false);
356+
_lp_clkrst_ll_enable_otp_dbg_clock(false);
357+
_lp_clkrst_ll_enable_lp_ana_i2c_clock(false);
358+
_lp_clkrst_ll_enable_lp_ext_i2c_clock(false);
359+
360+
REG_CLR_BIT(LP_APM_CLOCK_GATE_REG, LP_APM_CLK_EN);
361+
REG_CLR_BIT(LP_APM0_CLOCK_GATE_REG, LP_APM0_CLK_EN);
362+
WRITE_PERI_REG(LP_CLKRST_LP_CLK_PO_EN_REG, 0);
363+
}
326364
}

components/esp_system/port/soc/esp32c6/clk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ __attribute__((weak)) void esp_perip_clk_init(void)
224224
* precision sleep clock (for example, the BLE needs to use the main XTAL
225225
* oscillator (40 MHz) to provide the clock during the sleep process in some
226226
* scenarios), the module needs to switch to the required clock source by
227-
* itself. */ //TODO - WIFI-5233
227+
* itself. */
228228
soc_rtc_slow_clk_src_t rtc_slow_clk_src = rtc_clk_slow_src_get();
229229
modem_clock_lpclk_src_t modem_lpclk_src = (modem_clock_lpclk_src_t)(\
230230
(rtc_slow_clk_src == SOC_RTC_SLOW_CLK_SRC_RC_SLOW) ? MODEM_CLOCK_LPCLK_SRC_RC_SLOW \

components/esp_system/port/soc/esp32c61/clk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ __attribute__((weak)) void esp_perip_clk_init(void)
188188
* precision sleep clock (for example, the BLE needs to use the main XTAL
189189
* oscillator (40 MHz) to provide the clock during the sleep process in some
190190
* scenarios), the module needs to switch to the required clock source by
191-
* itself. */ //TODO - WIFI-5233
191+
* itself. */
192192
soc_rtc_slow_clk_src_t rtc_slow_clk_src = rtc_clk_slow_src_get();
193193
modem_clock_lpclk_src_t modem_lpclk_src = (modem_clock_lpclk_src_t)(
194194
(rtc_slow_clk_src == SOC_RTC_SLOW_CLK_SRC_RC_SLOW) ? MODEM_CLOCK_LPCLK_SRC_RC_SLOW

0 commit comments

Comments
 (0)