|
11 | 11 | #include <sys/util.h> |
12 | 12 | #include <drivers/clock_control/gd32_clock_control.h> |
13 | 13 | #include <gd32e10x_rcu.h> |
| 14 | +#include <gd32e10x.h> |
| 15 | + |
14 | 16 |
|
15 | 17 | #define RCC_BASE DT_REG_ADDR_BY_IDX(DT_NODELABEL(rcc), 0) |
16 | 18 | #undef RCU_REG_VAL |
17 | 19 | #define RCU_REG_VAL(periph) (REG32(RCC_BASE | periph )) |
18 | 20 |
|
19 | | -// todo: work here later |
| 21 | +static inline int gd32_clock_reset() |
| 22 | +{ |
| 23 | + /* reset the RCU clock configuration to the default reset state */ |
| 24 | + /* Set IRC8MEN bit */ |
| 25 | + RCU_CTL |= RCU_CTL_IRC8MEN; |
| 26 | + |
| 27 | + /* Reset CFG0 and CFG1 registers */ |
| 28 | + RCU_CFG0 = 0x00000000U; |
| 29 | + RCU_CFG1 = 0x00000000U; |
| 30 | + |
| 31 | + /* Reset HXTALEN, CKMEN, PLLEN, PLL1EN and PLL2EN bits */ |
| 32 | + RCU_CTL &= ~(RCU_CTL_PLLEN |RCU_CTL_PLL1EN | RCU_CTL_PLL2EN | RCU_CTL_CKMEN | RCU_CTL_HXTALEN); |
| 33 | + /* disable all interrupts */ |
| 34 | + RCU_INT = 0x00ff0000U; |
| 35 | + |
| 36 | + /* reset HXTALBPS bit */ |
| 37 | + RCU_CTL &= ~(RCU_CTL_HXTALBPS); |
| 38 | + |
| 39 | + return 0; |
| 40 | +} |
| 41 | + |
| 42 | +static inline void system_clock_120m_hxtal(void) |
| 43 | +{ |
| 44 | + uint32_t timeout = 0U; |
| 45 | + uint32_t stab_flag = 0U; |
| 46 | + |
| 47 | + /* enable HXTAL */ |
| 48 | + RCU_CTL |= RCU_CTL_HXTALEN; |
| 49 | + |
| 50 | + /* wait until HXTAL is stable or the startup time is longer than HXTAL_STARTUP_TIMEOUT */ |
| 51 | + do{ |
| 52 | + timeout++; |
| 53 | + stab_flag = (RCU_CTL & RCU_CTL_HXTALSTB); |
| 54 | + }while((0U == stab_flag) && (HXTAL_STARTUP_TIMEOUT != timeout)); |
| 55 | + |
| 56 | + /* if fail */ |
| 57 | + if(0U == (RCU_CTL & RCU_CTL_HXTALSTB)){ |
| 58 | + while(1){ |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + FMC_WS = (FMC_WS & (~FMC_WS_WSCNT)) | FMC_WAIT_STATE_3; |
| 63 | + |
| 64 | + /* HXTAL is stable */ |
| 65 | + /* AHB = SYSCLK */ |
| 66 | + RCU_CFG0 |= RCU_AHB_CKSYS_DIV1; |
| 67 | + /* APB2 = AHB/1 */ |
| 68 | + RCU_CFG0 |= RCU_APB2_CKAHB_DIV1; |
| 69 | + /* APB1 = AHB/2 */ |
| 70 | + RCU_CFG0 |= RCU_APB1_CKAHB_DIV2; |
| 71 | + |
| 72 | + /* CK_PLL = (CK_PREDIV0) * 30 = 120 MHz */ |
| 73 | + RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLMF_4); |
| 74 | + RCU_CFG0 |= (RCU_PLLSRC_HXTAL_IRC48M | RCU_PLL_MUL30); |
| 75 | + |
| 76 | + RCU_CFG1 &= ~(RCU_CFG1_PLLPRESEL | RCU_CFG1_PREDV0SEL | RCU_CFG1_PLL1MF | RCU_CFG1_PREDV1 | RCU_CFG1_PREDV0); |
| 77 | +#ifdef HXTAL_VALUE_8M |
| 78 | + /* CK_PREDIV0 = (CK_HXTAL)/2 *10 /10 = 4 MHz */ |
| 79 | + RCU_CFG1 |= (RCU_PLLPRESRC_HXTAL | RCU_PREDV0SRC_CKPLL1 | RCU_PLL1_MUL10 | RCU_PREDV1_DIV2 | RCU_PREDV0_DIV10); |
| 80 | +#elif defined (HXTAL_VALUE_25M) |
| 81 | + /* CK_PREDIV0 = (CK_HXTAL)/5 *8/10 = 4 MHz */ |
| 82 | + RCU_CFG1 |= (RCU_PLLPRESRC_HXTAL | RCU_PREDV0SRC_CKPLL1 | RCU_PLL1_MUL8 | RCU_PREDV1_DIV5 | RCU_PREDV0_DIV10); |
| 83 | +#endif |
| 84 | + |
| 85 | + /* enable PLL1 */ |
| 86 | + RCU_CTL |= RCU_CTL_PLL1EN; |
| 87 | + /* wait till PLL1 is ready */ |
| 88 | + while((RCU_CTL & RCU_CTL_PLL1STB) == 0U){ |
| 89 | + } |
| 90 | + |
| 91 | + /* enable PLL */ |
| 92 | + RCU_CTL |= RCU_CTL_PLLEN; |
| 93 | + |
| 94 | + /* wait until PLL is stable */ |
| 95 | + while(0U == (RCU_CTL & RCU_CTL_PLLSTB)){ |
| 96 | + } |
| 97 | + |
| 98 | + /* select PLL as system clock */ |
| 99 | + RCU_CFG0 &= ~RCU_CFG0_SCS; |
| 100 | + RCU_CFG0 |= RCU_CKSYSSRC_PLL; |
| 101 | + |
| 102 | + /* wait until PLL is selected as system clock */ |
| 103 | + while(0U == (RCU_CFG0 & RCU_SCSS_PLL)){ |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +static inline int gd32_clock_init() |
| 108 | +{ |
| 109 | + system_clock_120m_hxtal(); |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * @brief Initialize clocks for the gd32 |
| 116 | + * |
| 117 | + * This routine is called to enable and configure the clocks and PLL |
| 118 | + * of the soc on the board. It depends on the board definition. |
| 119 | + * This function is called on the startup and also to restore the config |
| 120 | + * when exiting for low power mode. |
| 121 | + * |
| 122 | + * @param dev clock device struct |
| 123 | + * |
| 124 | + * @return 0 |
| 125 | + */ |
20 | 126 | int gd32_clock_control_init(const struct device *dev) |
21 | 127 | { |
| 128 | + gd32_clock_reset(); |
| 129 | + gd32_clock_init(); |
| 130 | + |
22 | 131 | return 0; |
23 | 132 | } |
24 | 133 |
|
|
0 commit comments