3232#define DT_CPU_COMPAT espressif_riscv
3333#include <zephyr/dt-bindings/clock/esp32c3_clock.h>
3434#include <esp32c3/rom/rtc.h>
35+ #elif CONFIG_SOC_SERIES_ESP32C6
36+ #define DT_CPU_COMPAT espressif_riscv
37+ #include <zephyr/dt-bindings/clock/esp32c6_clock.h>
38+ #include <soc/lp_clkrst_reg.h>
39+ #include <soc/regi2c_dig_reg.h>
40+ #include <regi2c_ctrl.h>
41+ #include <esp32c6/rom/rtc.h>
42+ #include <soc/dport_access.h>
43+ #include <hal/clk_tree_ll.h>
3544#endif /* CONFIG_SOC_SERIES_ESP32xx */
3645
3746#include <zephyr/drivers/clock_control.h>
@@ -68,6 +77,49 @@ static bool reset_reason_is_cpu_reset(void)
6877 return false;
6978}
7079
80+ #if defined(CONFIG_SOC_SERIES_ESP32C6 )
81+ static void esp32_clock_perip_init (void )
82+ {
83+ soc_reset_reason_t rst_reason = esp_rom_get_reset_reason (0 );
84+
85+ if ((rst_reason != RESET_REASON_CPU0_MWDT0 ) && (rst_reason != RESET_REASON_CPU0_MWDT1 ) &&
86+ (rst_reason != RESET_REASON_CPU0_SW ) && (rst_reason != RESET_REASON_CPU0_RTC_WDT )) {
87+
88+ periph_ll_disable_clk_set_rst (PERIPH_UART1_MODULE );
89+ periph_ll_disable_clk_set_rst (PERIPH_I2C0_MODULE );
90+ periph_ll_disable_clk_set_rst (PERIPH_RMT_MODULE );
91+ periph_ll_disable_clk_set_rst (PERIPH_LEDC_MODULE );
92+ periph_ll_disable_clk_set_rst (PERIPH_TIMG1_MODULE );
93+ periph_ll_disable_clk_set_rst (PERIPH_TWAI0_MODULE );
94+ periph_ll_disable_clk_set_rst (PERIPH_TWAI1_MODULE );
95+ periph_ll_disable_clk_set_rst (PERIPH_I2S1_MODULE );
96+ periph_ll_disable_clk_set_rst (PERIPH_PCNT_MODULE );
97+ periph_ll_disable_clk_set_rst (PERIPH_ETM_MODULE );
98+ periph_ll_disable_clk_set_rst (PERIPH_MCPWM0_MODULE );
99+ periph_ll_disable_clk_set_rst (PERIPH_PARLIO_MODULE );
100+ periph_ll_disable_clk_set_rst (PERIPH_GDMA_MODULE );
101+ periph_ll_disable_clk_set_rst (PERIPH_SPI2_MODULE );
102+ periph_ll_disable_clk_set_rst (PERIPH_TEMPSENSOR_MODULE );
103+ periph_ll_disable_clk_set_rst (PERIPH_UHCI0_MODULE );
104+ periph_ll_disable_clk_set_rst (PERIPH_SARADC_MODULE );
105+ periph_ll_disable_clk_set_rst (PERIPH_SDIO_SLAVE_MODULE );
106+ periph_ll_disable_clk_set_rst (PERIPH_RSA_MODULE );
107+ periph_ll_disable_clk_set_rst (PERIPH_AES_MODULE );
108+ periph_ll_disable_clk_set_rst (PERIPH_SHA_MODULE );
109+ periph_ll_disable_clk_set_rst (PERIPH_ECC_MODULE );
110+ periph_ll_disable_clk_set_rst (PERIPH_HMAC_MODULE );
111+ periph_ll_disable_clk_set_rst (PERIPH_DS_MODULE );
112+
113+ REG_CLR_BIT (PCR_CTRL_TICK_CONF_REG , PCR_TICK_ENABLE );
114+ REG_CLR_BIT (PCR_TRACE_CONF_REG , PCR_TRACE_CLK_EN );
115+ REG_CLR_BIT (PCR_RETENTION_CONF_REG , PCR_RETENTION_CLK_EN );
116+ REG_CLR_BIT (PCR_MEM_MONITOR_CONF_REG , PCR_MEM_MONITOR_CLK_EN );
117+ REG_CLR_BIT (PCR_PVT_MONITOR_CONF_REG , PCR_PVT_MONITOR_CLK_EN );
118+ REG_CLR_BIT (PCR_PVT_MONITOR_FUNC_CLK_CONF_REG , PCR_PVT_MONITOR_FUNC_CLK_EN );
119+ WRITE_PERI_REG (PCR_CTRL_CLK_OUT_EN_REG , 0 );
120+ }
121+ }
122+ #else
71123static void esp32_clock_perip_init (void )
72124{
73125 uint32_t common_perip_clk ;
@@ -330,6 +382,7 @@ static void esp32_clock_perip_init(void)
330382 periph_module_enable (PERIPH_TIMG0_MODULE );
331383#endif
332384}
385+ #endif
333386
334387static enum clock_control_status clock_control_esp32_get_status (const struct device * dev ,
335388 clock_control_subsys_t sys )
@@ -389,7 +442,11 @@ static int clock_control_esp32_get_rate(const struct device *dev, clock_control_
389442
390443static int esp32_select_rtc_slow_clk (uint8_t slow_clk )
391444{
445+ #if !defined(CONFIG_SOC_SERIES_ESP32C6 )
392446 soc_rtc_slow_clk_src_t rtc_slow_clk_src = slow_clk & RTC_CNTL_ANA_CLK_RTC_SEL_V ;
447+ #else
448+ soc_rtc_slow_clk_src_t rtc_slow_clk_src = slow_clk ;
449+ #endif
393450 uint32_t cal_val = 0 ;
394451 /* number of times to repeat 32k XTAL calibration
395452 * before giving up and switching to the internal RC
@@ -424,9 +481,15 @@ static int esp32_select_rtc_slow_clk(uint8_t slow_clk)
424481 return - ENODEV ;
425482 }
426483 }
484+ #if defined(CONFIG_SOC_SERIES_ESP32C6 )
485+ } else if (rtc_slow_clk_src == SOC_RTC_SLOW_CLK_SRC_RC32K ) {
486+ rtc_clk_rc32k_enable (true);
487+ }
488+ #else
427489 } else if (rtc_slow_clk_src == SOC_RTC_SLOW_CLK_SRC_RC_FAST_D256 ) {
428490 rtc_clk_8m_enable (true, true);
429491 }
492+ #endif
430493 rtc_clk_slow_src_set (rtc_slow_clk_src );
431494
432495 if (CONFIG_RTC_CLK_CAL_CYCLES > 0 ) {
@@ -458,30 +521,53 @@ static int esp32_cpu_clock_configure(const struct esp32_cpu_clock_config *cpu_cf
458521
459522 esp_rom_uart_tx_wait_idle (ESP_CONSOLE_UART_NUM );
460523
524+ #if defined(CONFIG_SOC_SERIES_ESP32C6 )
525+ rtc_clk_modem_clock_domain_active_state_icg_map_preinit ();
526+
527+ REG_SET_FIELD (LP_CLKRST_FOSC_CNTL_REG , LP_CLKRST_FOSC_DFREQ , rtc_clk_cfg .clk_8m_dfreq );
528+ REGI2C_WRITE_MASK (I2C_DIG_REG , I2C_DIG_REG_SCK_DCAP , rtc_clk_cfg .slow_clk_dcap );
529+ REG_SET_FIELD (LP_CLKRST_RC32K_CNTL_REG , LP_CLKRST_RC32K_DFREQ , rtc_clk_cfg .rc32k_dfreq );
530+ #else
461531 REG_SET_FIELD (RTC_CNTL_REG , RTC_CNTL_SCK_DCAP , rtc_clk_cfg .slow_clk_dcap );
462532 REG_SET_FIELD (RTC_CNTL_CLK_CONF_REG , RTC_CNTL_CK8M_DFREQ , rtc_clk_cfg .clk_8m_dfreq );
533+ #endif
463534
464- #if !defined(CONFIG_SOC_SERIES_ESP32 )
535+ #if defined(CONFIG_SOC_SERIES_ESP32 )
536+ REG_SET_FIELD (RTC_CNTL_CLK_CONF_REG , RTC_CNTL_CK8M_DIV_SEL , rtc_clk_cfg .clk_8m_div - 1 );
537+ #elif defined(CONFIG_SOC_SERIES_ESP32C6 )
538+ clk_ll_rc_fast_tick_conf ();
539+ #else
465540 /* Configure 150k clock division */
466541 rtc_clk_divider_set (rtc_clk_cfg .clk_rtc_clk_div );
467542
468543 /* Configure 8M clock division */
469544 rtc_clk_8m_divider_set (rtc_clk_cfg .clk_8m_clk_div );
470- #else
471- REG_SET_FIELD (RTC_CNTL_CLK_CONF_REG , RTC_CNTL_CK8M_DIV_SEL , rtc_clk_cfg .clk_8m_div - 1 );
472545#endif
546+
547+ #if !defined(CONFIG_SOC_SERIES_ESP32C6 )
473548 /* Reset (disable) i2c internal bus for all regi2c registers */
474549 regi2c_ctrl_ll_i2c_reset ();
475550 /* Enable the internal bus used to configure BBPLL */
476551 regi2c_ctrl_ll_i2c_bbpll_enable ();
552+ #endif
553+
477554#if defined(CONFIG_SOC_SERIES_ESP32S2 ) || defined(CONFIG_SOC_SERIES_ESP32 )
478555 regi2c_ctrl_ll_i2c_apll_enable ();
479556#endif
480557
481558#if !defined(CONFIG_SOC_SERIES_ESP32S2 )
482559 rtc_clk_xtal_freq_update (rtc_clk_cfg .xtal_freq );
483560#endif
561+ #if defined(CONFIG_SOC_SERIES_ESP32C6 )
562+ /* On ESP32C6, MSPI source clock's default HS divider leads to 120MHz,
563+ * which is unusable before calibration. Therefore, before switching
564+ * SOC_ROOT_CLK to HS, we need to set MSPI source clock HS divider
565+ * to make it run at 80MHz after the switch. PLL = 480MHz, so divider is 6.
566+ */
567+ clk_ll_mspi_fast_set_hs_divider (6 );
568+ #else
484569 rtc_clk_apb_freq_update (rtc_clk_cfg .xtal_freq * MHZ (1 ));
570+ #endif
485571
486572 /* Set CPU frequency */
487573 rtc_clk_cpu_freq_get_config (& old_config );
@@ -498,6 +584,7 @@ static int esp32_cpu_clock_configure(const struct esp32_cpu_clock_config *cpu_cf
498584 esp_cpu_set_cycle_count ((uint64_t )esp_cpu_get_cycle_count () * rtc_clk_cfg .cpu_freq_mhz /
499585 old_config .freq_mhz );
500586
587+ #if !defined(CONFIG_SOC_SERIES_ESP32C6 )
501588#if ESP_ROM_UART_CLK_IS_XTAL
502589 uart_clock_src_hz = (uint32_t )rtc_clk_xtal_freq_get () * MHZ (1 );
503590#else
@@ -507,6 +594,7 @@ static int esp32_cpu_clock_configure(const struct esp32_cpu_clock_config *cpu_cf
507594#if !defined(ESP_CONSOLE_UART_NONE )
508595 esp_rom_uart_set_clock_baudrate (ESP_CONSOLE_UART_NUM , uart_clock_src_hz ,
509596 ESP_CONSOLE_UART_BAUDRATE );
597+ #endif
510598#endif
511599 return 0 ;
512600}
@@ -546,10 +634,10 @@ static int clock_control_esp32_configure(const struct device *dev, clock_control
546634static int clock_control_esp32_init (const struct device * dev )
547635{
548636 const struct esp32_clock_config * cfg = dev -> config ;
549- struct esp32_clock_data * data = dev -> data ;
637+ bool ret ;
638+ #if !defined(CONFIG_SOC_SERIES_ESP32C6 )
550639 soc_reset_reason_t rst_reas ;
551640 rtc_config_t rtc_cfg = RTC_CONFIG_DEFAULT ();
552- bool ret ;
553641
554642 rst_reas = esp_rom_get_reset_reason (0 );
555643#if !defined(CONFIG_SOC_SERIES_ESP32 )
@@ -562,6 +650,7 @@ static int clock_control_esp32_init(const struct device *dev)
562650 }
563651#endif
564652 rtc_init (rtc_cfg );
653+ #endif
565654
566655 ret = esp32_cpu_clock_configure (& cfg -> cpu );
567656 if (ret ) {
0 commit comments