|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 STMicroelectronics |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <soc.h> |
| 8 | + |
| 9 | +#include <stm32_ll_bus.h> |
| 10 | +#include <stm32_ll_pwr.h> |
| 11 | +#include <stm32_ll_rcc.h> |
| 12 | +#include <stm32_ll_system.h> |
| 13 | +#include <stm32_ll_utils.h> |
| 14 | + |
| 15 | +#include <zephyr/arch/cpu.h> |
| 16 | +#include <zephyr/drivers/clock_control.h> |
| 17 | +#include <zephyr/drivers/clock_control/stm32_clock_control.h> |
| 18 | +#include <zephyr/sys/util.h> |
| 19 | + |
| 20 | +static int stm32_clock_control_on(const struct device *dev, clock_control_subsys_t sub_system) |
| 21 | +{ |
| 22 | + struct stm32_pclken *pclken = (struct stm32_pclken *)sub_system; |
| 23 | + volatile int temp; |
| 24 | + |
| 25 | + ARG_UNUSED(dev); |
| 26 | + |
| 27 | + if (!IN_RANGE(pclken->bus, STM32_PERIPH_BUS_MIN, STM32_PERIPH_BUS_MAX)) { |
| 28 | + /* Attempt to toggle a wrong periph clock bit */ |
| 29 | + return -ENOTSUP; |
| 30 | + } |
| 31 | + |
| 32 | + sys_set_bits(DT_REG_ADDR(DT_NODELABEL(rcc)) + pclken->bus, pclken->enr); |
| 33 | + /* Ensure that the write operation is completed */ |
| 34 | + temp = sys_read32(DT_REG_ADDR(DT_NODELABEL(rcc)) + pclken->bus); |
| 35 | + UNUSED(temp); |
| 36 | + |
| 37 | + return 0; |
| 38 | +} |
| 39 | + |
| 40 | +static int stm32_clock_control_off(const struct device *dev, clock_control_subsys_t sub_system) |
| 41 | +{ |
| 42 | + struct stm32_pclken *pclken = (struct stm32_pclken *)sub_system; |
| 43 | + volatile int temp; |
| 44 | + |
| 45 | + ARG_UNUSED(dev); |
| 46 | + |
| 47 | + if (!IN_RANGE(pclken->bus, STM32_PERIPH_BUS_MIN, STM32_PERIPH_BUS_MAX)) { |
| 48 | + /* Attempt to toggle a wrong periph clock bit */ |
| 49 | + return -ENOTSUP; |
| 50 | + } |
| 51 | + |
| 52 | + sys_clear_bits(DT_REG_ADDR(DT_NODELABEL(rcc)) + pclken->bus, pclken->enr); |
| 53 | + /* Ensure that the write operation is completed */ |
| 54 | + temp = sys_read32(DT_REG_ADDR(DT_NODELABEL(rcc)) + pclken->bus); |
| 55 | + UNUSED(temp); |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +static int stm32_clock_control_get_subsys_rate(const struct device *dev, |
| 61 | + clock_control_subsys_t sub_system, uint32_t *rate) |
| 62 | +{ |
| 63 | + struct stm32_pclken *pclken = (struct stm32_pclken *)sub_system; |
| 64 | + |
| 65 | + ARG_UNUSED(dev); |
| 66 | + |
| 67 | + switch (pclken->bus) { |
| 68 | + case STM32_CLOCK_BUS_APB1: |
| 69 | + switch (pclken->enr) { |
| 70 | + case LL_APB1_GRP1_PERIPH_UART4: |
| 71 | + *rate = LL_RCC_GetUARTClockFreq(LL_RCC_UART4_CLKSOURCE); |
| 72 | + break; |
| 73 | + default: |
| 74 | + return -ENOTSUP; |
| 75 | + } |
| 76 | + break; |
| 77 | + default: |
| 78 | + return -ENOTSUP; |
| 79 | + } |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +static const struct clock_control_driver_api stm32_clock_control_api = { |
| 84 | + .on = stm32_clock_control_on, |
| 85 | + .off = stm32_clock_control_off, |
| 86 | + .get_rate = stm32_clock_control_get_subsys_rate, |
| 87 | +}; |
| 88 | + |
| 89 | +static void set_up_fixed_clock_sources(void) |
| 90 | +{ |
| 91 | + if (IS_ENABLED(STM32_HSE_ENABLED)) { |
| 92 | + /* Enable HSE */ |
| 93 | + LL_RCC_HSE_Enable(); |
| 94 | + while (LL_RCC_HSE_IsReady() != 1) { |
| 95 | + /* Wait for HSE ready */ |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (IS_ENABLED(STM32_HSI_ENABLED)) { |
| 100 | + /* Enable HSI if not enabled */ |
| 101 | + if (LL_RCC_HSI_IsReady() != 1) { |
| 102 | + /* Enable HSI */ |
| 103 | + LL_RCC_HSI_Enable(); |
| 104 | + while (LL_RCC_HSI_IsReady() != 1) { |
| 105 | + /* Wait for HSI ready */ |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +static int stm32_clock_control_init(const struct device *dev) |
| 112 | +{ |
| 113 | + ARG_UNUSED(dev); |
| 114 | + |
| 115 | + set_up_fixed_clock_sources(); |
| 116 | + |
| 117 | +#if STM32_SYSCLK_SRC_HSE |
| 118 | + |
| 119 | + LL_RCC_SetMPUClkSource(LL_RCC_MPU_CLKSOURCE_HSE); |
| 120 | + while (LL_RCC_GetMPUClkSource() != LL_RCC_MPU_CLKSOURCE_HSE) { |
| 121 | + } |
| 122 | + |
| 123 | +#elif STM32_SYSCLK_SRC_HSI |
| 124 | + |
| 125 | + LL_RCC_SetMPUClkSource(LL_RCC_MPU_CLKSOURCE_HSI); |
| 126 | + while (LL_RCC_GetMPUClkSource() != LL_RCC_MPU_CLKSOURCE_HSI) { |
| 127 | + } |
| 128 | + |
| 129 | +#elif STM32_SYSCLK_SRC_PLL |
| 130 | + |
| 131 | + BUILD_ASSERT(IS_ENABLED(STM32_HSE_ENABLED), |
| 132 | + "STM32MP13 PLL requires HSE to be enabled!"); |
| 133 | + |
| 134 | + /* The default system clock source is HSI, but the bootloader may have switched it. */ |
| 135 | + /* Switch back to HSE for clock setup as PLL1 configuration must not be modified */ |
| 136 | + /* while active.*/ |
| 137 | + |
| 138 | + LL_RCC_SetMPUClkSource(LL_RCC_MPU_CLKSOURCE_HSE); |
| 139 | + while ((READ_BIT(RCC->MPCKSELR, RCC_MPCKSELR_MPUSRCRDY) != RCC_MPCKSELR_MPUSRCRDY)) { |
| 140 | + } |
| 141 | + |
| 142 | + CLEAR_BIT(RCC->PLL1CR, RCC_PLL1CR_DIVPEN); |
| 143 | + while (READ_BIT(RCC->PLL1CR, RCC_PLL1CR_DIVPEN) == RCC_PLL1CR_DIVPEN) { |
| 144 | + }; |
| 145 | + |
| 146 | + CLEAR_BIT(RCC->PLL1CR, RCC_PLL1CR_DIVQEN); |
| 147 | + while (READ_BIT(RCC->PLL1CR, RCC_PLL1CR_DIVQEN) == RCC_PLL1CR_DIVQEN) { |
| 148 | + }; |
| 149 | + |
| 150 | + CLEAR_BIT(RCC->PLL1CR, RCC_PLL1CR_DIVREN); |
| 151 | + while (READ_BIT(RCC->PLL1CR, RCC_PLL1CR_DIVREN) == RCC_PLL1CR_DIVREN) { |
| 152 | + }; |
| 153 | + |
| 154 | + uint32_t pll1_n = DT_PROP(DT_NODELABEL(pll1), mul_n); |
| 155 | + uint32_t pll1_m = DT_PROP(DT_NODELABEL(pll1), div_m); |
| 156 | + uint32_t pll1_p = DT_PROP(DT_NODELABEL(pll1), div_p); |
| 157 | + uint32_t pll1_v = DT_PROP(DT_NODELABEL(pll1), frac_v); |
| 158 | + |
| 159 | + LL_RCC_PLL1_SetN(pll1_n); |
| 160 | + while (LL_RCC_PLL1_GetN() != pll1_n) { |
| 161 | + } |
| 162 | + LL_RCC_PLL1_SetM(pll1_m); |
| 163 | + while (LL_RCC_PLL1_GetM() != pll1_m) { |
| 164 | + } |
| 165 | + LL_RCC_PLL1_SetP(pll1_p); |
| 166 | + while (LL_RCC_PLL1_GetP() != pll1_p) { |
| 167 | + } |
| 168 | + LL_RCC_PLL1_SetFRACV(pll1_v); |
| 169 | + while (LL_RCC_PLL1_GetFRACV() != pll1_v) { |
| 170 | + } |
| 171 | + |
| 172 | + LL_RCC_PLL1_Enable(); |
| 173 | + while (LL_RCC_PLL1_IsReady() != 1) { |
| 174 | + } |
| 175 | + |
| 176 | + SET_BIT(RCC->PLL1CR, RCC_PLL1CR_DIVPEN); |
| 177 | + while (READ_BIT(RCC->PLL1CR, RCC_PLL1CR_DIVPEN) != RCC_PLL1CR_DIVPEN) { |
| 178 | + }; |
| 179 | + |
| 180 | + LL_RCC_SetMPUClkSource(LL_RCC_MPU_CLKSOURCE_PLL1); |
| 181 | + while (LL_RCC_GetMPUClkSource() != LL_RCC_MPU_CLKSOURCE_PLL1) { |
| 182 | + } |
| 183 | + |
| 184 | +#endif |
| 185 | + |
| 186 | + return 0; |
| 187 | +} |
| 188 | + |
| 189 | +/** |
| 190 | + * @brief RCC device, note that priority is intentionally set to 1 so |
| 191 | + * that the device init runs just after SOC init |
| 192 | + */ |
| 193 | +DEVICE_DT_DEFINE(DT_NODELABEL(rcc), |
| 194 | + stm32_clock_control_init, |
| 195 | + NULL, |
| 196 | + NULL, NULL, |
| 197 | + PRE_KERNEL_1, |
| 198 | + CONFIG_CLOCK_CONTROL_INIT_PRIORITY, |
| 199 | + &stm32_clock_control_api); |
0 commit comments