18
18
19
19
#define WCH_RCC_CLOCK_ID_OFFSET (id ) (((id) >> 5) & 0xFF)
20
20
#define WCH_RCC_CLOCK_ID_BIT (id ) ((id) & 0x1F)
21
+ #define WCH_RCC_SYSCLK DT_PROP(DT_NODELABEL(cpu0), clock_frequency)
21
22
22
23
#if DT_NODE_HAS_COMPAT (DT_INST_CLOCKS_CTLR (0 ), wch_ch32v00x_pll_clock ) || \
23
24
DT_NODE_HAS_COMPAT (DT_INST_CLOCKS_CTLR (0 ), wch_ch32v20x_30x_pll_clock )
@@ -78,6 +79,33 @@ static int clock_control_wch_rcc_get_rate(const struct device *dev, clock_contro
78
79
return 0 ;
79
80
}
80
81
82
+ static void clock_control_wch_rcc_setup_flash (void )
83
+ {
84
+ #if defined(FLASH_ACTLR_LATENCY )
85
+ uint32_t latency ;
86
+
87
+ #if defined(CONFIG_SOC_CH32V003 )
88
+ if (WCH_RCC_SYSCLK <= 24000000 ) {
89
+ latency = FLASH_ACTLR_LATENCY_0 ;
90
+ } else {
91
+ latency = FLASH_ACTLR_LATENCY_1 ;
92
+ }
93
+ #elif defined(CONFIG_SOC_SERIES_CH32V00X )
94
+ if (WCH_RCC_SYSCLK <= 15000000 ) {
95
+ latency = FLASH_ACTLR_LATENCY_0 ;
96
+ } else if (WCH_RCC_SYSCLK <= 24000000 ) {
97
+ latency = FLASH_ACTLR_LATENCY_1 ;
98
+ } else {
99
+ latency = FLASH_ACTLR_LATENCY_2 ;
100
+ }
101
+ FLASH -> ACTLR = (FLASH -> ACTLR & ~FLASH_ACTLR_LATENCY ) | latency ;
102
+ #else
103
+ #error Unrecognised SOC family
104
+ #endif
105
+ FLASH -> ACTLR = (FLASH -> ACTLR & ~FLASH_ACTLR_LATENCY ) | latency ;
106
+ #endif
107
+ }
108
+
81
109
static DEVICE_API (clock_control , clock_control_wch_rcc_api ) = {
82
110
.on = clock_control_wch_rcc_on ,
83
111
.get_rate = clock_control_wch_rcc_get_rate ,
@@ -87,6 +115,8 @@ static int clock_control_wch_rcc_init(const struct device *dev)
87
115
{
88
116
const struct clock_control_wch_rcc_config * config = dev -> config ;
89
117
118
+ clock_control_wch_rcc_setup_flash ();
119
+
90
120
if (IS_ENABLED (CONFIG_DT_HAS_WCH_CH32V00X_PLL_CLOCK_ENABLED ) ||
91
121
IS_ENABLED (CONFIG_DT_HAS_WCH_CH32V20X_30X_PLL_CLOCK_ENABLED )) {
92
122
/* Disable the PLL before potentially changing the input clocks. */
@@ -145,10 +175,6 @@ static int clock_control_wch_rcc_init(const struct device *dev)
145
175
RCC -> INTR = RCC_CSSC | RCC_PLLRDYC | RCC_HSERDYC | RCC_LSIRDYC ;
146
176
/* HCLK = SYSCLK = APB1 */
147
177
RCC -> CFGR0 = (RCC -> CFGR0 & ~RCC_HPRE ) | RCC_HPRE_DIV1 ;
148
- #if defined(CONFIG_SOC_CH32V003 )
149
- /* Set the Flash to 0 wait state */
150
- FLASH -> ACTLR = (FLASH -> ACTLR & ~FLASH_ACTLR_LATENCY ) | FLASH_ACTLR_LATENCY_1 ;
151
- #endif
152
178
153
179
return 0 ;
154
180
}
0 commit comments