Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1 | /* |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 2 | * Copyright (C) 2018-2022, STMicroelectronics - All Rights Reserved |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 5 | */ |
| 6 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 7 | #include <assert.h> |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 9 | #include <stdint.h> |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 10 | #include <stdio.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <arch.h> |
| 13 | #include <arch_helpers.h> |
| 14 | #include <common/debug.h> |
Andre Przywara | cc99f3f | 2020-03-26 12:51:21 +0000 | [diff] [blame] | 15 | #include <common/fdt_wrappers.h> |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 16 | #include <drivers/clk.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 17 | #include <drivers/delay_timer.h> |
Yann Gautier | 4d42947 | 2019-02-14 11:15:20 +0100 | [diff] [blame] | 18 | #include <drivers/st/stm32mp_clkfunc.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 19 | #include <drivers/st/stm32mp1_clk.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 20 | #include <drivers/st/stm32mp1_rcc.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 21 | #include <dt-bindings/clock/stm32mp1-clksrc.h> |
| 22 | #include <lib/mmio.h> |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 23 | #include <lib/spinlock.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 24 | #include <lib/utils_def.h> |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 25 | #include <libfdt.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 26 | #include <plat/common/platform.h> |
| 27 | |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 28 | #include <platform_def.h> |
| 29 | |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 30 | #define MAX_HSI_HZ 64000000 |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 31 | #define USB_PHY_48_MHZ 48000000 |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 32 | |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 33 | #define TIMEOUT_US_200MS U(200000) |
| 34 | #define TIMEOUT_US_1S U(1000000) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 35 | |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 36 | #define PLLRDY_TIMEOUT TIMEOUT_US_200MS |
| 37 | #define CLKSRC_TIMEOUT TIMEOUT_US_200MS |
| 38 | #define CLKDIV_TIMEOUT TIMEOUT_US_200MS |
| 39 | #define HSIDIV_TIMEOUT TIMEOUT_US_200MS |
| 40 | #define OSCRDY_TIMEOUT TIMEOUT_US_1S |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 41 | |
Yann Gautier | 5f2e874 | 2019-05-17 15:57:56 +0200 | [diff] [blame] | 42 | const char *stm32mp_osc_node_label[NB_OSC] = { |
| 43 | [_LSI] = "clk-lsi", |
| 44 | [_LSE] = "clk-lse", |
| 45 | [_HSI] = "clk-hsi", |
| 46 | [_HSE] = "clk-hse", |
| 47 | [_CSI] = "clk-csi", |
| 48 | [_I2S_CKIN] = "i2s_ckin", |
| 49 | }; |
| 50 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 51 | enum stm32mp1_parent_id { |
| 52 | /* Oscillators are defined in enum stm32mp_osc_id */ |
| 53 | |
| 54 | /* Other parent source */ |
| 55 | _HSI_KER = NB_OSC, |
| 56 | _HSE_KER, |
| 57 | _HSE_KER_DIV2, |
Gabriel Fernandez | 4e3a51a | 2021-07-27 15:39:16 +0200 | [diff] [blame] | 58 | _HSE_RTC, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 59 | _CSI_KER, |
| 60 | _PLL1_P, |
| 61 | _PLL1_Q, |
| 62 | _PLL1_R, |
| 63 | _PLL2_P, |
| 64 | _PLL2_Q, |
| 65 | _PLL2_R, |
| 66 | _PLL3_P, |
| 67 | _PLL3_Q, |
| 68 | _PLL3_R, |
| 69 | _PLL4_P, |
| 70 | _PLL4_Q, |
| 71 | _PLL4_R, |
| 72 | _ACLK, |
| 73 | _PCLK1, |
| 74 | _PCLK2, |
| 75 | _PCLK3, |
| 76 | _PCLK4, |
| 77 | _PCLK5, |
| 78 | _HCLK6, |
| 79 | _HCLK2, |
| 80 | _CK_PER, |
| 81 | _CK_MPU, |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 82 | _CK_MCU, |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 83 | _USB_PHY_48, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 84 | _PARENT_NB, |
| 85 | _UNKNOWN_ID = 0xff, |
| 86 | }; |
| 87 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 88 | /* Lists only the parent clock we are interested in */ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 89 | enum stm32mp1_parent_sel { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 90 | _I2C12_SEL, |
| 91 | _I2C35_SEL, |
| 92 | _STGEN_SEL, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 93 | _I2C46_SEL, |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 94 | _SPI6_SEL, |
Yann Gautier | 9d8bbcd | 2019-05-07 18:49:33 +0200 | [diff] [blame] | 95 | _UART1_SEL, |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 96 | _RNG1_SEL, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 97 | _UART6_SEL, |
| 98 | _UART24_SEL, |
| 99 | _UART35_SEL, |
| 100 | _UART78_SEL, |
| 101 | _SDMMC12_SEL, |
| 102 | _SDMMC3_SEL, |
| 103 | _QSPI_SEL, |
| 104 | _FMC_SEL, |
Yann Gautier | 9d8bbcd | 2019-05-07 18:49:33 +0200 | [diff] [blame] | 105 | _AXIS_SEL, |
| 106 | _MCUS_SEL, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 107 | _USBPHY_SEL, |
| 108 | _USBO_SEL, |
Etienne Carriere | 0413261 | 2019-12-08 08:20:12 +0100 | [diff] [blame] | 109 | _MPU_SEL, |
Yann Gautier | faa9bcf | 2021-08-31 18:23:13 +0200 | [diff] [blame] | 110 | _CKPER_SEL, |
Etienne Carriere | bccc7d0 | 2019-12-08 08:22:31 +0100 | [diff] [blame] | 111 | _RTC_SEL, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 112 | _PARENT_SEL_NB, |
| 113 | _UNKNOWN_SEL = 0xff, |
| 114 | }; |
| 115 | |
Etienne Carriere | 0413261 | 2019-12-08 08:20:12 +0100 | [diff] [blame] | 116 | /* State the parent clock ID straight related to a clock */ |
| 117 | static const uint8_t parent_id_clock_id[_PARENT_NB] = { |
| 118 | [_HSE] = CK_HSE, |
| 119 | [_HSI] = CK_HSI, |
| 120 | [_CSI] = CK_CSI, |
| 121 | [_LSE] = CK_LSE, |
| 122 | [_LSI] = CK_LSI, |
| 123 | [_I2S_CKIN] = _UNKNOWN_ID, |
| 124 | [_USB_PHY_48] = _UNKNOWN_ID, |
| 125 | [_HSI_KER] = CK_HSI, |
| 126 | [_HSE_KER] = CK_HSE, |
| 127 | [_HSE_KER_DIV2] = CK_HSE_DIV2, |
Gabriel Fernandez | 4e3a51a | 2021-07-27 15:39:16 +0200 | [diff] [blame] | 128 | [_HSE_RTC] = _UNKNOWN_ID, |
Etienne Carriere | 0413261 | 2019-12-08 08:20:12 +0100 | [diff] [blame] | 129 | [_CSI_KER] = CK_CSI, |
| 130 | [_PLL1_P] = PLL1_P, |
| 131 | [_PLL1_Q] = PLL1_Q, |
| 132 | [_PLL1_R] = PLL1_R, |
| 133 | [_PLL2_P] = PLL2_P, |
| 134 | [_PLL2_Q] = PLL2_Q, |
| 135 | [_PLL2_R] = PLL2_R, |
| 136 | [_PLL3_P] = PLL3_P, |
| 137 | [_PLL3_Q] = PLL3_Q, |
| 138 | [_PLL3_R] = PLL3_R, |
| 139 | [_PLL4_P] = PLL4_P, |
| 140 | [_PLL4_Q] = PLL4_Q, |
| 141 | [_PLL4_R] = PLL4_R, |
| 142 | [_ACLK] = CK_AXI, |
| 143 | [_PCLK1] = CK_AXI, |
| 144 | [_PCLK2] = CK_AXI, |
| 145 | [_PCLK3] = CK_AXI, |
| 146 | [_PCLK4] = CK_AXI, |
| 147 | [_PCLK5] = CK_AXI, |
| 148 | [_CK_PER] = CK_PER, |
| 149 | [_CK_MPU] = CK_MPU, |
| 150 | [_CK_MCU] = CK_MCU, |
| 151 | }; |
| 152 | |
| 153 | static unsigned int clock_id2parent_id(unsigned long id) |
| 154 | { |
| 155 | unsigned int n; |
| 156 | |
| 157 | for (n = 0U; n < ARRAY_SIZE(parent_id_clock_id); n++) { |
| 158 | if (parent_id_clock_id[n] == id) { |
| 159 | return n; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return _UNKNOWN_ID; |
| 164 | } |
| 165 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 166 | enum stm32mp1_pll_id { |
| 167 | _PLL1, |
| 168 | _PLL2, |
| 169 | _PLL3, |
| 170 | _PLL4, |
| 171 | _PLL_NB |
| 172 | }; |
| 173 | |
| 174 | enum stm32mp1_div_id { |
| 175 | _DIV_P, |
| 176 | _DIV_Q, |
| 177 | _DIV_R, |
| 178 | _DIV_NB, |
| 179 | }; |
| 180 | |
| 181 | enum stm32mp1_clksrc_id { |
| 182 | CLKSRC_MPU, |
| 183 | CLKSRC_AXI, |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 184 | CLKSRC_MCU, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 185 | CLKSRC_PLL12, |
| 186 | CLKSRC_PLL3, |
| 187 | CLKSRC_PLL4, |
| 188 | CLKSRC_RTC, |
| 189 | CLKSRC_MCO1, |
| 190 | CLKSRC_MCO2, |
| 191 | CLKSRC_NB |
| 192 | }; |
| 193 | |
| 194 | enum stm32mp1_clkdiv_id { |
| 195 | CLKDIV_MPU, |
| 196 | CLKDIV_AXI, |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 197 | CLKDIV_MCU, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 198 | CLKDIV_APB1, |
| 199 | CLKDIV_APB2, |
| 200 | CLKDIV_APB3, |
| 201 | CLKDIV_APB4, |
| 202 | CLKDIV_APB5, |
| 203 | CLKDIV_RTC, |
| 204 | CLKDIV_MCO1, |
| 205 | CLKDIV_MCO2, |
| 206 | CLKDIV_NB |
| 207 | }; |
| 208 | |
| 209 | enum stm32mp1_pllcfg { |
| 210 | PLLCFG_M, |
| 211 | PLLCFG_N, |
| 212 | PLLCFG_P, |
| 213 | PLLCFG_Q, |
| 214 | PLLCFG_R, |
| 215 | PLLCFG_O, |
| 216 | PLLCFG_NB |
| 217 | }; |
| 218 | |
| 219 | enum stm32mp1_pllcsg { |
| 220 | PLLCSG_MOD_PER, |
| 221 | PLLCSG_INC_STEP, |
| 222 | PLLCSG_SSCG_MODE, |
| 223 | PLLCSG_NB |
| 224 | }; |
| 225 | |
| 226 | enum stm32mp1_plltype { |
| 227 | PLL_800, |
| 228 | PLL_1600, |
| 229 | PLL_TYPE_NB |
| 230 | }; |
| 231 | |
| 232 | struct stm32mp1_pll { |
| 233 | uint8_t refclk_min; |
| 234 | uint8_t refclk_max; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | struct stm32mp1_clk_gate { |
| 238 | uint16_t offset; |
| 239 | uint8_t bit; |
| 240 | uint8_t index; |
| 241 | uint8_t set_clr; |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 242 | uint8_t secure; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 243 | uint8_t sel; /* Relates to enum stm32mp1_parent_sel */ |
| 244 | uint8_t fixed; /* Relates to enum stm32mp1_parent_id */ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | struct stm32mp1_clk_sel { |
| 248 | uint16_t offset; |
| 249 | uint8_t src; |
| 250 | uint8_t msk; |
| 251 | uint8_t nb_parent; |
| 252 | const uint8_t *parent; |
| 253 | }; |
| 254 | |
| 255 | #define REFCLK_SIZE 4 |
| 256 | struct stm32mp1_clk_pll { |
| 257 | enum stm32mp1_plltype plltype; |
| 258 | uint16_t rckxselr; |
| 259 | uint16_t pllxcfgr1; |
| 260 | uint16_t pllxcfgr2; |
| 261 | uint16_t pllxfracr; |
| 262 | uint16_t pllxcr; |
| 263 | uint16_t pllxcsgr; |
| 264 | enum stm32mp_osc_id refclk[REFCLK_SIZE]; |
| 265 | }; |
| 266 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 267 | /* Clocks with selectable source and non set/clr register access */ |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 268 | #define _CLK_SELEC(sec, off, b, idx, s) \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 269 | { \ |
| 270 | .offset = (off), \ |
| 271 | .bit = (b), \ |
| 272 | .index = (idx), \ |
| 273 | .set_clr = 0, \ |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 274 | .secure = (sec), \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 275 | .sel = (s), \ |
| 276 | .fixed = _UNKNOWN_ID, \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 279 | /* Clocks with fixed source and non set/clr register access */ |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 280 | #define _CLK_FIXED(sec, off, b, idx, f) \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 281 | { \ |
| 282 | .offset = (off), \ |
| 283 | .bit = (b), \ |
| 284 | .index = (idx), \ |
| 285 | .set_clr = 0, \ |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 286 | .secure = (sec), \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 287 | .sel = _UNKNOWN_SEL, \ |
| 288 | .fixed = (f), \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 291 | /* Clocks with selectable source and set/clr register access */ |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 292 | #define _CLK_SC_SELEC(sec, off, b, idx, s) \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 293 | { \ |
| 294 | .offset = (off), \ |
| 295 | .bit = (b), \ |
| 296 | .index = (idx), \ |
| 297 | .set_clr = 1, \ |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 298 | .secure = (sec), \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 299 | .sel = (s), \ |
| 300 | .fixed = _UNKNOWN_ID, \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 303 | /* Clocks with fixed source and set/clr register access */ |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 304 | #define _CLK_SC_FIXED(sec, off, b, idx, f) \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 305 | { \ |
| 306 | .offset = (off), \ |
| 307 | .bit = (b), \ |
| 308 | .index = (idx), \ |
| 309 | .set_clr = 1, \ |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 310 | .secure = (sec), \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 311 | .sel = _UNKNOWN_SEL, \ |
| 312 | .fixed = (f), \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Yann Gautier | 9d8bbcd | 2019-05-07 18:49:33 +0200 | [diff] [blame] | 315 | #define _CLK_PARENT_SEL(_label, _rcc_selr, _parents) \ |
| 316 | [_ ## _label ## _SEL] = { \ |
| 317 | .offset = _rcc_selr, \ |
| 318 | .src = _rcc_selr ## _ ## _label ## SRC_SHIFT, \ |
Etienne Carriere | c164ce2 | 2019-12-08 08:20:40 +0100 | [diff] [blame] | 319 | .msk = (_rcc_selr ## _ ## _label ## SRC_MASK) >> \ |
| 320 | (_rcc_selr ## _ ## _label ## SRC_SHIFT), \ |
Yann Gautier | 9d8bbcd | 2019-05-07 18:49:33 +0200 | [diff] [blame] | 321 | .parent = (_parents), \ |
| 322 | .nb_parent = ARRAY_SIZE(_parents) \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 325 | #define _CLK_PLL(idx, type, off1, off2, off3, \ |
| 326 | off4, off5, off6, \ |
| 327 | p1, p2, p3, p4) \ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 328 | [(idx)] = { \ |
| 329 | .plltype = (type), \ |
| 330 | .rckxselr = (off1), \ |
| 331 | .pllxcfgr1 = (off2), \ |
| 332 | .pllxcfgr2 = (off3), \ |
| 333 | .pllxfracr = (off4), \ |
| 334 | .pllxcr = (off5), \ |
| 335 | .pllxcsgr = (off6), \ |
| 336 | .refclk[0] = (p1), \ |
| 337 | .refclk[1] = (p2), \ |
| 338 | .refclk[2] = (p3), \ |
| 339 | .refclk[3] = (p4), \ |
| 340 | } |
| 341 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 342 | #define NB_GATES ARRAY_SIZE(stm32mp1_clk_gate) |
| 343 | |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 344 | #define SEC 1 |
| 345 | #define N_S 0 |
| 346 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 347 | static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = { |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 348 | _CLK_FIXED(SEC, RCC_DDRITFCR, 0, DDRC1, _ACLK), |
| 349 | _CLK_FIXED(SEC, RCC_DDRITFCR, 1, DDRC1LP, _ACLK), |
| 350 | _CLK_FIXED(SEC, RCC_DDRITFCR, 2, DDRC2, _ACLK), |
| 351 | _CLK_FIXED(SEC, RCC_DDRITFCR, 3, DDRC2LP, _ACLK), |
| 352 | _CLK_FIXED(SEC, RCC_DDRITFCR, 4, DDRPHYC, _PLL2_R), |
| 353 | _CLK_FIXED(SEC, RCC_DDRITFCR, 5, DDRPHYCLP, _PLL2_R), |
| 354 | _CLK_FIXED(SEC, RCC_DDRITFCR, 6, DDRCAPB, _PCLK4), |
| 355 | _CLK_FIXED(SEC, RCC_DDRITFCR, 7, DDRCAPBLP, _PCLK4), |
| 356 | _CLK_FIXED(SEC, RCC_DDRITFCR, 8, AXIDCG, _ACLK), |
| 357 | _CLK_FIXED(SEC, RCC_DDRITFCR, 9, DDRPHYCAPB, _PCLK4), |
| 358 | _CLK_FIXED(SEC, RCC_DDRITFCR, 10, DDRPHYCAPBLP, _PCLK4), |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 359 | |
Yann Gautier | 5672967 | 2020-01-17 11:59:28 +0100 | [diff] [blame] | 360 | #if defined(IMAGE_BL32) |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 361 | _CLK_SC_FIXED(N_S, RCC_MP_APB1ENSETR, 6, TIM12_K, _PCLK1), |
Yann Gautier | 5672967 | 2020-01-17 11:59:28 +0100 | [diff] [blame] | 362 | #endif |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 363 | _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 14, USART2_K, _UART24_SEL), |
| 364 | _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 15, USART3_K, _UART35_SEL), |
| 365 | _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 16, UART4_K, _UART24_SEL), |
| 366 | _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 17, UART5_K, _UART35_SEL), |
| 367 | _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 18, UART7_K, _UART78_SEL), |
| 368 | _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 19, UART8_K, _UART78_SEL), |
| 369 | _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 21, I2C1_K, _I2C12_SEL), |
| 370 | _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 22, I2C2_K, _I2C12_SEL), |
| 371 | _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 23, I2C3_K, _I2C35_SEL), |
| 372 | _CLK_SC_SELEC(N_S, RCC_MP_APB1ENSETR, 24, I2C5_K, _I2C35_SEL), |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 373 | |
Yann Gautier | 5672967 | 2020-01-17 11:59:28 +0100 | [diff] [blame] | 374 | #if defined(IMAGE_BL32) |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 375 | _CLK_SC_FIXED(N_S, RCC_MP_APB2ENSETR, 2, TIM15_K, _PCLK2), |
Yann Gautier | 5672967 | 2020-01-17 11:59:28 +0100 | [diff] [blame] | 376 | #endif |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 377 | _CLK_SC_SELEC(N_S, RCC_MP_APB2ENSETR, 13, USART6_K, _UART6_SEL), |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 378 | |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 379 | _CLK_SC_FIXED(N_S, RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_ID), |
Yann Gautier | 3edc7c3 | 2019-05-20 19:17:08 +0200 | [diff] [blame] | 380 | |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 381 | _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL), |
| 382 | _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL), |
| 383 | _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 16, USBPHY_K, _USBPHY_SEL), |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 384 | |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 385 | _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 0, SPI6_K, _SPI6_SEL), |
| 386 | _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 2, I2C4_K, _I2C46_SEL), |
| 387 | _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 3, I2C6_K, _I2C46_SEL), |
| 388 | _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 4, USART1_K, _UART1_SEL), |
| 389 | _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 8, RTCAPB, _PCLK5), |
| 390 | _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 11, TZC1, _PCLK5), |
| 391 | _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 12, TZC2, _PCLK5), |
| 392 | _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 13, TZPC, _PCLK5), |
| 393 | _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 15, IWDG1, _PCLK5), |
| 394 | _CLK_SC_FIXED(SEC, RCC_MP_APB5ENSETR, 16, BSEC, _PCLK5), |
| 395 | _CLK_SC_SELEC(SEC, RCC_MP_APB5ENSETR, 20, STGEN_K, _STGEN_SEL), |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 396 | |
Yann Gautier | 5672967 | 2020-01-17 11:59:28 +0100 | [diff] [blame] | 397 | #if defined(IMAGE_BL32) |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 398 | _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL), |
| 399 | _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL), |
Yann Gautier | 5672967 | 2020-01-17 11:59:28 +0100 | [diff] [blame] | 400 | #endif |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 401 | |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 402 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 0, GPIOA, _UNKNOWN_SEL), |
| 403 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 1, GPIOB, _UNKNOWN_SEL), |
| 404 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 2, GPIOC, _UNKNOWN_SEL), |
| 405 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 3, GPIOD, _UNKNOWN_SEL), |
| 406 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 4, GPIOE, _UNKNOWN_SEL), |
| 407 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 5, GPIOF, _UNKNOWN_SEL), |
| 408 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 6, GPIOG, _UNKNOWN_SEL), |
| 409 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 7, GPIOH, _UNKNOWN_SEL), |
| 410 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 8, GPIOI, _UNKNOWN_SEL), |
| 411 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 9, GPIOJ, _UNKNOWN_SEL), |
| 412 | _CLK_SC_SELEC(N_S, RCC_MP_AHB4ENSETR, 10, GPIOK, _UNKNOWN_SEL), |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 413 | |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 414 | _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 0, GPIOZ, _PCLK5), |
| 415 | _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 4, CRYP1, _PCLK5), |
| 416 | _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 5, HASH1, _PCLK5), |
| 417 | _CLK_SC_SELEC(SEC, RCC_MP_AHB5ENSETR, 6, RNG1_K, _RNG1_SEL), |
| 418 | _CLK_SC_FIXED(SEC, RCC_MP_AHB5ENSETR, 8, BKPSRAM, _PCLK5), |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 419 | |
Yann Gautier | 5672967 | 2020-01-17 11:59:28 +0100 | [diff] [blame] | 420 | #if defined(IMAGE_BL2) |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 421 | _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 12, FMC_K, _FMC_SEL), |
| 422 | _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 14, QSPI_K, _QSPI_SEL), |
Yann Gautier | 5672967 | 2020-01-17 11:59:28 +0100 | [diff] [blame] | 423 | #endif |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 424 | _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL), |
| 425 | _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL), |
Yann Gautier | 5672967 | 2020-01-17 11:59:28 +0100 | [diff] [blame] | 426 | #if defined(IMAGE_BL32) |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 427 | _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL), |
Yann Gautier | 5672967 | 2020-01-17 11:59:28 +0100 | [diff] [blame] | 428 | #endif |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 429 | |
Yann Gautier | b2edbc3 | 2021-10-27 18:16:59 +0200 | [diff] [blame] | 430 | _CLK_SELEC(SEC, RCC_BDCR, 20, RTC, _RTC_SEL), |
| 431 | _CLK_SELEC(N_S, RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL), |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 432 | }; |
| 433 | |
| 434 | static const uint8_t i2c12_parents[] = { |
| 435 | _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER |
| 436 | }; |
| 437 | |
| 438 | static const uint8_t i2c35_parents[] = { |
| 439 | _PCLK1, _PLL4_R, _HSI_KER, _CSI_KER |
| 440 | }; |
| 441 | |
| 442 | static const uint8_t stgen_parents[] = { |
| 443 | _HSI_KER, _HSE_KER |
| 444 | }; |
| 445 | |
| 446 | static const uint8_t i2c46_parents[] = { |
| 447 | _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER |
| 448 | }; |
| 449 | |
| 450 | static const uint8_t spi6_parents[] = { |
| 451 | _PCLK5, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER, _PLL3_Q |
| 452 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 453 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 454 | static const uint8_t usart1_parents[] = { |
| 455 | _PCLK5, _PLL3_Q, _HSI_KER, _CSI_KER, _PLL4_Q, _HSE_KER |
| 456 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 457 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 458 | static const uint8_t rng1_parents[] = { |
| 459 | _CSI, _PLL4_R, _LSE, _LSI |
| 460 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 461 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 462 | static const uint8_t uart6_parents[] = { |
| 463 | _PCLK2, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER |
| 464 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 465 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 466 | static const uint8_t uart234578_parents[] = { |
| 467 | _PCLK1, _PLL4_Q, _HSI_KER, _CSI_KER, _HSE_KER |
| 468 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 469 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 470 | static const uint8_t sdmmc12_parents[] = { |
| 471 | _HCLK6, _PLL3_R, _PLL4_P, _HSI_KER |
| 472 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 473 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 474 | static const uint8_t sdmmc3_parents[] = { |
| 475 | _HCLK2, _PLL3_R, _PLL4_P, _HSI_KER |
| 476 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 477 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 478 | static const uint8_t qspi_parents[] = { |
| 479 | _ACLK, _PLL3_R, _PLL4_P, _CK_PER |
| 480 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 481 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 482 | static const uint8_t fmc_parents[] = { |
| 483 | _ACLK, _PLL3_R, _PLL4_P, _CK_PER |
| 484 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 485 | |
Etienne Carriere | 40c28e8 | 2019-12-19 10:03:23 +0100 | [diff] [blame] | 486 | static const uint8_t axiss_parents[] = { |
| 487 | _HSI, _HSE, _PLL2_P |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 488 | }; |
| 489 | |
Etienne Carriere | 40c28e8 | 2019-12-19 10:03:23 +0100 | [diff] [blame] | 490 | static const uint8_t mcuss_parents[] = { |
| 491 | _HSI, _HSE, _CSI, _PLL3_P |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 492 | }; |
| 493 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 494 | static const uint8_t usbphy_parents[] = { |
| 495 | _HSE_KER, _PLL4_R, _HSE_KER_DIV2 |
| 496 | }; |
| 497 | |
| 498 | static const uint8_t usbo_parents[] = { |
| 499 | _PLL4_R, _USB_PHY_48 |
| 500 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 501 | |
Etienne Carriere | 0413261 | 2019-12-08 08:20:12 +0100 | [diff] [blame] | 502 | static const uint8_t mpu_parents[] = { |
| 503 | _HSI, _HSE, _PLL1_P, _PLL1_P /* specific div */ |
| 504 | }; |
| 505 | |
| 506 | static const uint8_t per_parents[] = { |
| 507 | _HSI, _HSE, _CSI, |
| 508 | }; |
| 509 | |
Etienne Carriere | bccc7d0 | 2019-12-08 08:22:31 +0100 | [diff] [blame] | 510 | static const uint8_t rtc_parents[] = { |
Gabriel Fernandez | 4e3a51a | 2021-07-27 15:39:16 +0200 | [diff] [blame] | 511 | _UNKNOWN_ID, _LSE, _LSI, _HSE_RTC |
Etienne Carriere | bccc7d0 | 2019-12-08 08:22:31 +0100 | [diff] [blame] | 512 | }; |
| 513 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 514 | static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = { |
Yann Gautier | 9d8bbcd | 2019-05-07 18:49:33 +0200 | [diff] [blame] | 515 | _CLK_PARENT_SEL(I2C12, RCC_I2C12CKSELR, i2c12_parents), |
| 516 | _CLK_PARENT_SEL(I2C35, RCC_I2C35CKSELR, i2c35_parents), |
| 517 | _CLK_PARENT_SEL(STGEN, RCC_STGENCKSELR, stgen_parents), |
| 518 | _CLK_PARENT_SEL(I2C46, RCC_I2C46CKSELR, i2c46_parents), |
| 519 | _CLK_PARENT_SEL(SPI6, RCC_SPI6CKSELR, spi6_parents), |
| 520 | _CLK_PARENT_SEL(UART1, RCC_UART1CKSELR, usart1_parents), |
| 521 | _CLK_PARENT_SEL(RNG1, RCC_RNG1CKSELR, rng1_parents), |
Etienne Carriere | 0413261 | 2019-12-08 08:20:12 +0100 | [diff] [blame] | 522 | _CLK_PARENT_SEL(MPU, RCC_MPCKSELR, mpu_parents), |
Yann Gautier | faa9bcf | 2021-08-31 18:23:13 +0200 | [diff] [blame] | 523 | _CLK_PARENT_SEL(CKPER, RCC_CPERCKSELR, per_parents), |
Etienne Carriere | bccc7d0 | 2019-12-08 08:22:31 +0100 | [diff] [blame] | 524 | _CLK_PARENT_SEL(RTC, RCC_BDCR, rtc_parents), |
Yann Gautier | 9d8bbcd | 2019-05-07 18:49:33 +0200 | [diff] [blame] | 525 | _CLK_PARENT_SEL(UART6, RCC_UART6CKSELR, uart6_parents), |
| 526 | _CLK_PARENT_SEL(UART24, RCC_UART24CKSELR, uart234578_parents), |
| 527 | _CLK_PARENT_SEL(UART35, RCC_UART35CKSELR, uart234578_parents), |
| 528 | _CLK_PARENT_SEL(UART78, RCC_UART78CKSELR, uart234578_parents), |
| 529 | _CLK_PARENT_SEL(SDMMC12, RCC_SDMMC12CKSELR, sdmmc12_parents), |
| 530 | _CLK_PARENT_SEL(SDMMC3, RCC_SDMMC3CKSELR, sdmmc3_parents), |
| 531 | _CLK_PARENT_SEL(QSPI, RCC_QSPICKSELR, qspi_parents), |
| 532 | _CLK_PARENT_SEL(FMC, RCC_FMCCKSELR, fmc_parents), |
Etienne Carriere | 40c28e8 | 2019-12-19 10:03:23 +0100 | [diff] [blame] | 533 | _CLK_PARENT_SEL(AXIS, RCC_ASSCKSELR, axiss_parents), |
| 534 | _CLK_PARENT_SEL(MCUS, RCC_MSSCKSELR, mcuss_parents), |
Yann Gautier | 9d8bbcd | 2019-05-07 18:49:33 +0200 | [diff] [blame] | 535 | _CLK_PARENT_SEL(USBPHY, RCC_USBCKSELR, usbphy_parents), |
| 536 | _CLK_PARENT_SEL(USBO, RCC_USBCKSELR, usbo_parents), |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 537 | }; |
| 538 | |
| 539 | /* Define characteristic of PLL according type */ |
| 540 | #define DIVN_MIN 24 |
| 541 | static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = { |
| 542 | [PLL_800] = { |
| 543 | .refclk_min = 4, |
| 544 | .refclk_max = 16, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 545 | }, |
| 546 | [PLL_1600] = { |
| 547 | .refclk_min = 8, |
| 548 | .refclk_max = 16, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 549 | }, |
| 550 | }; |
| 551 | |
| 552 | /* PLLNCFGR2 register divider by output */ |
| 553 | static const uint8_t pllncfgr2[_DIV_NB] = { |
| 554 | [_DIV_P] = RCC_PLLNCFGR2_DIVP_SHIFT, |
| 555 | [_DIV_Q] = RCC_PLLNCFGR2_DIVQ_SHIFT, |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 556 | [_DIV_R] = RCC_PLLNCFGR2_DIVR_SHIFT, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 560 | _CLK_PLL(_PLL1, PLL_1600, |
| 561 | RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2, |
| 562 | RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR, |
| 563 | _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID), |
| 564 | _CLK_PLL(_PLL2, PLL_1600, |
| 565 | RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2, |
| 566 | RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR, |
| 567 | _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID), |
| 568 | _CLK_PLL(_PLL3, PLL_800, |
| 569 | RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2, |
| 570 | RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR, |
| 571 | _HSI, _HSE, _CSI, _UNKNOWN_OSC_ID), |
| 572 | _CLK_PLL(_PLL4, PLL_800, |
| 573 | RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2, |
| 574 | RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR, |
| 575 | _HSI, _HSE, _CSI, _I2S_CKIN), |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | /* Prescaler table lookups for clock computation */ |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 579 | /* div = /1 /2 /4 /8 / 16 /64 /128 /512 */ |
| 580 | static const uint8_t stm32mp1_mcu_div[16] = { |
| 581 | 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9 |
| 582 | }; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 583 | |
| 584 | /* div = /1 /2 /4 /8 /16 : same divider for PMU and APBX */ |
| 585 | #define stm32mp1_mpu_div stm32mp1_mpu_apbx_div |
| 586 | #define stm32mp1_apbx_div stm32mp1_mpu_apbx_div |
| 587 | static const uint8_t stm32mp1_mpu_apbx_div[8] = { |
| 588 | 0, 1, 2, 3, 4, 4, 4, 4 |
| 589 | }; |
| 590 | |
| 591 | /* div = /1 /2 /3 /4 */ |
| 592 | static const uint8_t stm32mp1_axi_div[8] = { |
| 593 | 1, 2, 3, 4, 4, 4, 4, 4 |
| 594 | }; |
| 595 | |
Etienne Carriere | 1368ada | 2020-05-13 11:49:49 +0200 | [diff] [blame] | 596 | static const char * const stm32mp1_clk_parent_name[_PARENT_NB] __unused = { |
| 597 | [_HSI] = "HSI", |
| 598 | [_HSE] = "HSE", |
| 599 | [_CSI] = "CSI", |
| 600 | [_LSI] = "LSI", |
| 601 | [_LSE] = "LSE", |
| 602 | [_I2S_CKIN] = "I2S_CKIN", |
| 603 | [_HSI_KER] = "HSI_KER", |
| 604 | [_HSE_KER] = "HSE_KER", |
| 605 | [_HSE_KER_DIV2] = "HSE_KER_DIV2", |
Gabriel Fernandez | 4e3a51a | 2021-07-27 15:39:16 +0200 | [diff] [blame] | 606 | [_HSE_RTC] = "HSE_RTC", |
Etienne Carriere | 1368ada | 2020-05-13 11:49:49 +0200 | [diff] [blame] | 607 | [_CSI_KER] = "CSI_KER", |
| 608 | [_PLL1_P] = "PLL1_P", |
| 609 | [_PLL1_Q] = "PLL1_Q", |
| 610 | [_PLL1_R] = "PLL1_R", |
| 611 | [_PLL2_P] = "PLL2_P", |
| 612 | [_PLL2_Q] = "PLL2_Q", |
| 613 | [_PLL2_R] = "PLL2_R", |
| 614 | [_PLL3_P] = "PLL3_P", |
| 615 | [_PLL3_Q] = "PLL3_Q", |
| 616 | [_PLL3_R] = "PLL3_R", |
| 617 | [_PLL4_P] = "PLL4_P", |
| 618 | [_PLL4_Q] = "PLL4_Q", |
| 619 | [_PLL4_R] = "PLL4_R", |
| 620 | [_ACLK] = "ACLK", |
| 621 | [_PCLK1] = "PCLK1", |
| 622 | [_PCLK2] = "PCLK2", |
| 623 | [_PCLK3] = "PCLK3", |
| 624 | [_PCLK4] = "PCLK4", |
| 625 | [_PCLK5] = "PCLK5", |
| 626 | [_HCLK6] = "KCLK6", |
| 627 | [_HCLK2] = "HCLK2", |
| 628 | [_CK_PER] = "CK_PER", |
| 629 | [_CK_MPU] = "CK_MPU", |
| 630 | [_CK_MCU] = "CK_MCU", |
| 631 | [_USB_PHY_48] = "USB_PHY_48", |
| 632 | }; |
| 633 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 634 | /* RCC clock device driver private */ |
| 635 | static unsigned long stm32mp1_osc[NB_OSC]; |
| 636 | static struct spinlock reg_lock; |
| 637 | static unsigned int gate_refcounts[NB_GATES]; |
| 638 | static struct spinlock refcount_lock; |
| 639 | |
| 640 | static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx) |
| 641 | { |
| 642 | return &stm32mp1_clk_gate[idx]; |
| 643 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 644 | |
Yann Gautier | af1e8f4 | 2021-10-27 18:21:11 +0200 | [diff] [blame] | 645 | #if defined(IMAGE_BL32) |
| 646 | static bool gate_is_non_secure(const struct stm32mp1_clk_gate *gate) |
| 647 | { |
| 648 | return gate->secure == N_S; |
| 649 | } |
| 650 | #endif |
| 651 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 652 | static const struct stm32mp1_clk_sel *clk_sel_ref(unsigned int idx) |
| 653 | { |
| 654 | return &stm32mp1_clk_sel[idx]; |
| 655 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 656 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 657 | static const struct stm32mp1_clk_pll *pll_ref(unsigned int idx) |
| 658 | { |
| 659 | return &stm32mp1_clk_pll[idx]; |
| 660 | } |
| 661 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 662 | static void stm32mp1_clk_lock(struct spinlock *lock) |
| 663 | { |
Yann Gautier | f540a59 | 2019-05-22 19:13:51 +0200 | [diff] [blame] | 664 | if (stm32mp_lock_available()) { |
| 665 | /* Assume interrupts are masked */ |
| 666 | spin_lock(lock); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 667 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | static void stm32mp1_clk_unlock(struct spinlock *lock) |
| 671 | { |
Yann Gautier | f540a59 | 2019-05-22 19:13:51 +0200 | [diff] [blame] | 672 | if (stm32mp_lock_available()) { |
| 673 | spin_unlock(lock); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 674 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | bool stm32mp1_rcc_is_secure(void) |
| 678 | { |
| 679 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Etienne Carriere | 5e68f6b | 2020-02-05 10:03:27 +0100 | [diff] [blame] | 680 | uint32_t mask = RCC_TZCR_TZEN; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 681 | |
Etienne Carriere | 5e68f6b | 2020-02-05 10:03:27 +0100 | [diff] [blame] | 682 | return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 685 | bool stm32mp1_rcc_is_mckprot(void) |
| 686 | { |
| 687 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Etienne Carriere | 5e68f6b | 2020-02-05 10:03:27 +0100 | [diff] [blame] | 688 | uint32_t mask = RCC_TZCR_TZEN | RCC_TZCR_MCKPROT; |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 689 | |
Etienne Carriere | 5e68f6b | 2020-02-05 10:03:27 +0100 | [diff] [blame] | 690 | return (mmio_read_32(rcc_base + RCC_TZCR) & mask) == mask; |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 693 | void stm32mp1_clk_rcc_regs_lock(void) |
| 694 | { |
| 695 | stm32mp1_clk_lock(®_lock); |
| 696 | } |
| 697 | |
| 698 | void stm32mp1_clk_rcc_regs_unlock(void) |
| 699 | { |
| 700 | stm32mp1_clk_unlock(®_lock); |
| 701 | } |
| 702 | |
| 703 | static unsigned long stm32mp1_clk_get_fixed(enum stm32mp_osc_id idx) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 704 | { |
| 705 | if (idx >= NB_OSC) { |
| 706 | return 0; |
| 707 | } |
| 708 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 709 | return stm32mp1_osc[idx]; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 712 | static int stm32mp1_clk_get_gated_id(unsigned long id) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 713 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 714 | unsigned int i; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 715 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 716 | for (i = 0U; i < NB_GATES; i++) { |
| 717 | if (gate_ref(i)->index == id) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 718 | return i; |
| 719 | } |
| 720 | } |
| 721 | |
Yann Gautier | c934381 | 2021-09-07 09:05:44 +0200 | [diff] [blame] | 722 | ERROR("%s: clk id %lu not found\n", __func__, id); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 723 | |
| 724 | return -EINVAL; |
| 725 | } |
| 726 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 727 | static enum stm32mp1_parent_sel stm32mp1_clk_get_sel(int i) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 728 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 729 | return (enum stm32mp1_parent_sel)(gate_ref(i)->sel); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 730 | } |
| 731 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 732 | static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 733 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 734 | return (enum stm32mp1_parent_id)(gate_ref(i)->fixed); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 735 | } |
| 736 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 737 | static int stm32mp1_clk_get_parent(unsigned long id) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 738 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 739 | const struct stm32mp1_clk_sel *sel; |
Etienne Carriere | 0413261 | 2019-12-08 08:20:12 +0100 | [diff] [blame] | 740 | uint32_t p_sel; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 741 | int i; |
| 742 | enum stm32mp1_parent_id p; |
| 743 | enum stm32mp1_parent_sel s; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 744 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 745 | |
Etienne Carriere | 0413261 | 2019-12-08 08:20:12 +0100 | [diff] [blame] | 746 | /* Few non gateable clock have a static parent ID, find them */ |
| 747 | i = (int)clock_id2parent_id(id); |
| 748 | if (i != _UNKNOWN_ID) { |
| 749 | return i; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 750 | } |
| 751 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 752 | i = stm32mp1_clk_get_gated_id(id); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 753 | if (i < 0) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 754 | panic(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 755 | } |
| 756 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 757 | p = stm32mp1_clk_get_fixed_parent(i); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 758 | if (p < _PARENT_NB) { |
| 759 | return (int)p; |
| 760 | } |
| 761 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 762 | s = stm32mp1_clk_get_sel(i); |
| 763 | if (s == _UNKNOWN_SEL) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 764 | return -EINVAL; |
| 765 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 766 | if (s >= _PARENT_SEL_NB) { |
| 767 | panic(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 770 | sel = clk_sel_ref(s); |
Etienne Carriere | c164ce2 | 2019-12-08 08:20:40 +0100 | [diff] [blame] | 771 | p_sel = (mmio_read_32(rcc_base + sel->offset) & |
| 772 | (sel->msk << sel->src)) >> sel->src; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 773 | if (p_sel < sel->nb_parent) { |
| 774 | return (int)sel->parent[p_sel]; |
| 775 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 776 | |
| 777 | return -EINVAL; |
| 778 | } |
| 779 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 780 | static unsigned long stm32mp1_pll_get_fref(const struct stm32mp1_clk_pll *pll) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 781 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 782 | uint32_t selr = mmio_read_32(stm32mp_rcc_base() + pll->rckxselr); |
| 783 | uint32_t src = selr & RCC_SELR_REFCLK_SRC_MASK; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 784 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 785 | return stm32mp1_clk_get_fixed(pll->refclk[src]); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* |
| 789 | * pll_get_fvco() : return the VCO or (VCO / 2) frequency for the requested PLL |
| 790 | * - PLL1 & PLL2 => return VCO / 2 with Fpll_y_ck = FVCO / 2 * (DIVy + 1) |
| 791 | * - PLL3 & PLL4 => return VCO with Fpll_y_ck = FVCO / (DIVy + 1) |
| 792 | * => in all cases Fpll_y_ck = pll_get_fvco() / (DIVy + 1) |
| 793 | */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 794 | static unsigned long stm32mp1_pll_get_fvco(const struct stm32mp1_clk_pll *pll) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 795 | { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 796 | unsigned long refclk, fvco; |
| 797 | uint32_t cfgr1, fracr, divm, divn; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 798 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 799 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 800 | cfgr1 = mmio_read_32(rcc_base + pll->pllxcfgr1); |
| 801 | fracr = mmio_read_32(rcc_base + pll->pllxfracr); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 802 | |
| 803 | divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT; |
| 804 | divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK; |
| 805 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 806 | refclk = stm32mp1_pll_get_fref(pll); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 807 | |
| 808 | /* |
| 809 | * With FRACV : |
| 810 | * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1) |
| 811 | * Without FRACV |
| 812 | * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1) |
| 813 | */ |
| 814 | if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 815 | uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >> |
| 816 | RCC_PLLNFRACR_FRACV_SHIFT; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 817 | unsigned long long numerator, denominator; |
| 818 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 819 | numerator = (((unsigned long long)divn + 1U) << 13) + fracv; |
| 820 | numerator = refclk * numerator; |
| 821 | denominator = ((unsigned long long)divm + 1U) << 13; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 822 | fvco = (unsigned long)(numerator / denominator); |
| 823 | } else { |
| 824 | fvco = (unsigned long)(refclk * (divn + 1U) / (divm + 1U)); |
| 825 | } |
| 826 | |
| 827 | return fvco; |
| 828 | } |
| 829 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 830 | static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 831 | enum stm32mp1_div_id div_id) |
| 832 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 833 | const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 834 | unsigned long dfout; |
| 835 | uint32_t cfgr2, divy; |
| 836 | |
| 837 | if (div_id >= _DIV_NB) { |
| 838 | return 0; |
| 839 | } |
| 840 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 841 | cfgr2 = mmio_read_32(stm32mp_rcc_base() + pll->pllxcfgr2); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 842 | divy = (cfgr2 >> pllncfgr2[div_id]) & RCC_PLLNCFGR2_DIVX_MASK; |
| 843 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 844 | dfout = stm32mp1_pll_get_fvco(pll) / (divy + 1U); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 845 | |
| 846 | return dfout; |
| 847 | } |
| 848 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 849 | static unsigned long get_clock_rate(int p) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 850 | { |
| 851 | uint32_t reg, clkdiv; |
| 852 | unsigned long clock = 0; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 853 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 854 | |
| 855 | switch (p) { |
| 856 | case _CK_MPU: |
| 857 | /* MPU sub system */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 858 | reg = mmio_read_32(rcc_base + RCC_MPCKSELR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 859 | switch (reg & RCC_SELR_SRC_MASK) { |
| 860 | case RCC_MPCKSELR_HSI: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 861 | clock = stm32mp1_clk_get_fixed(_HSI); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 862 | break; |
| 863 | case RCC_MPCKSELR_HSE: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 864 | clock = stm32mp1_clk_get_fixed(_HSE); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 865 | break; |
| 866 | case RCC_MPCKSELR_PLL: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 867 | clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 868 | break; |
| 869 | case RCC_MPCKSELR_PLL_MPUDIV: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 870 | clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 871 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 872 | reg = mmio_read_32(rcc_base + RCC_MPCKDIVR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 873 | clkdiv = reg & RCC_MPUDIV_MASK; |
Gabriel Fernandez | 4d19874 | 2020-02-28 09:09:06 +0100 | [diff] [blame] | 874 | clock >>= stm32mp1_mpu_div[clkdiv]; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 875 | break; |
| 876 | default: |
| 877 | break; |
| 878 | } |
| 879 | break; |
| 880 | /* AXI sub system */ |
| 881 | case _ACLK: |
| 882 | case _HCLK2: |
| 883 | case _HCLK6: |
| 884 | case _PCLK4: |
| 885 | case _PCLK5: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 886 | reg = mmio_read_32(rcc_base + RCC_ASSCKSELR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 887 | switch (reg & RCC_SELR_SRC_MASK) { |
| 888 | case RCC_ASSCKSELR_HSI: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 889 | clock = stm32mp1_clk_get_fixed(_HSI); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 890 | break; |
| 891 | case RCC_ASSCKSELR_HSE: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 892 | clock = stm32mp1_clk_get_fixed(_HSE); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 893 | break; |
| 894 | case RCC_ASSCKSELR_PLL: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 895 | clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 896 | break; |
| 897 | default: |
| 898 | break; |
| 899 | } |
| 900 | |
| 901 | /* System clock divider */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 902 | reg = mmio_read_32(rcc_base + RCC_AXIDIVR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 903 | clock /= stm32mp1_axi_div[reg & RCC_AXIDIV_MASK]; |
| 904 | |
| 905 | switch (p) { |
| 906 | case _PCLK4: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 907 | reg = mmio_read_32(rcc_base + RCC_APB4DIVR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 908 | clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK]; |
| 909 | break; |
| 910 | case _PCLK5: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 911 | reg = mmio_read_32(rcc_base + RCC_APB5DIVR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 912 | clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK]; |
| 913 | break; |
| 914 | default: |
| 915 | break; |
| 916 | } |
| 917 | break; |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 918 | /* MCU sub system */ |
| 919 | case _CK_MCU: |
| 920 | case _PCLK1: |
| 921 | case _PCLK2: |
| 922 | case _PCLK3: |
| 923 | reg = mmio_read_32(rcc_base + RCC_MSSCKSELR); |
| 924 | switch (reg & RCC_SELR_SRC_MASK) { |
| 925 | case RCC_MSSCKSELR_HSI: |
| 926 | clock = stm32mp1_clk_get_fixed(_HSI); |
| 927 | break; |
| 928 | case RCC_MSSCKSELR_HSE: |
| 929 | clock = stm32mp1_clk_get_fixed(_HSE); |
| 930 | break; |
| 931 | case RCC_MSSCKSELR_CSI: |
| 932 | clock = stm32mp1_clk_get_fixed(_CSI); |
| 933 | break; |
| 934 | case RCC_MSSCKSELR_PLL: |
| 935 | clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P); |
| 936 | break; |
| 937 | default: |
| 938 | break; |
| 939 | } |
| 940 | |
| 941 | /* MCU clock divider */ |
| 942 | reg = mmio_read_32(rcc_base + RCC_MCUDIVR); |
| 943 | clock >>= stm32mp1_mcu_div[reg & RCC_MCUDIV_MASK]; |
| 944 | |
| 945 | switch (p) { |
| 946 | case _PCLK1: |
| 947 | reg = mmio_read_32(rcc_base + RCC_APB1DIVR); |
| 948 | clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK]; |
| 949 | break; |
| 950 | case _PCLK2: |
| 951 | reg = mmio_read_32(rcc_base + RCC_APB2DIVR); |
| 952 | clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK]; |
| 953 | break; |
| 954 | case _PCLK3: |
| 955 | reg = mmio_read_32(rcc_base + RCC_APB3DIVR); |
| 956 | clock >>= stm32mp1_apbx_div[reg & RCC_APBXDIV_MASK]; |
| 957 | break; |
| 958 | case _CK_MCU: |
| 959 | default: |
| 960 | break; |
| 961 | } |
| 962 | break; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 963 | case _CK_PER: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 964 | reg = mmio_read_32(rcc_base + RCC_CPERCKSELR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 965 | switch (reg & RCC_SELR_SRC_MASK) { |
| 966 | case RCC_CPERCKSELR_HSI: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 967 | clock = stm32mp1_clk_get_fixed(_HSI); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 968 | break; |
| 969 | case RCC_CPERCKSELR_HSE: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 970 | clock = stm32mp1_clk_get_fixed(_HSE); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 971 | break; |
| 972 | case RCC_CPERCKSELR_CSI: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 973 | clock = stm32mp1_clk_get_fixed(_CSI); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 974 | break; |
| 975 | default: |
| 976 | break; |
| 977 | } |
| 978 | break; |
| 979 | case _HSI: |
| 980 | case _HSI_KER: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 981 | clock = stm32mp1_clk_get_fixed(_HSI); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 982 | break; |
| 983 | case _CSI: |
| 984 | case _CSI_KER: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 985 | clock = stm32mp1_clk_get_fixed(_CSI); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 986 | break; |
| 987 | case _HSE: |
| 988 | case _HSE_KER: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 989 | clock = stm32mp1_clk_get_fixed(_HSE); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 990 | break; |
| 991 | case _HSE_KER_DIV2: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 992 | clock = stm32mp1_clk_get_fixed(_HSE) >> 1; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 993 | break; |
Gabriel Fernandez | 4e3a51a | 2021-07-27 15:39:16 +0200 | [diff] [blame] | 994 | case _HSE_RTC: |
| 995 | clock = stm32mp1_clk_get_fixed(_HSE); |
| 996 | clock /= (mmio_read_32(rcc_base + RCC_RTCDIVR) & RCC_DIVR_DIV_MASK) + 1U; |
| 997 | break; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 998 | case _LSI: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 999 | clock = stm32mp1_clk_get_fixed(_LSI); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1000 | break; |
| 1001 | case _LSE: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1002 | clock = stm32mp1_clk_get_fixed(_LSE); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1003 | break; |
| 1004 | /* PLL */ |
| 1005 | case _PLL1_P: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1006 | clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1007 | break; |
| 1008 | case _PLL1_Q: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1009 | clock = stm32mp1_read_pll_freq(_PLL1, _DIV_Q); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1010 | break; |
| 1011 | case _PLL1_R: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1012 | clock = stm32mp1_read_pll_freq(_PLL1, _DIV_R); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1013 | break; |
| 1014 | case _PLL2_P: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1015 | clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1016 | break; |
| 1017 | case _PLL2_Q: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1018 | clock = stm32mp1_read_pll_freq(_PLL2, _DIV_Q); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1019 | break; |
| 1020 | case _PLL2_R: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1021 | clock = stm32mp1_read_pll_freq(_PLL2, _DIV_R); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1022 | break; |
| 1023 | case _PLL3_P: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1024 | clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1025 | break; |
| 1026 | case _PLL3_Q: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1027 | clock = stm32mp1_read_pll_freq(_PLL3, _DIV_Q); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1028 | break; |
| 1029 | case _PLL3_R: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1030 | clock = stm32mp1_read_pll_freq(_PLL3, _DIV_R); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1031 | break; |
| 1032 | case _PLL4_P: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1033 | clock = stm32mp1_read_pll_freq(_PLL4, _DIV_P); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1034 | break; |
| 1035 | case _PLL4_Q: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1036 | clock = stm32mp1_read_pll_freq(_PLL4, _DIV_Q); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1037 | break; |
| 1038 | case _PLL4_R: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1039 | clock = stm32mp1_read_pll_freq(_PLL4, _DIV_R); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1040 | break; |
| 1041 | /* Other */ |
| 1042 | case _USB_PHY_48: |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1043 | clock = USB_PHY_48_MHZ; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1044 | break; |
| 1045 | default: |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | return clock; |
| 1050 | } |
| 1051 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1052 | static void __clk_enable(struct stm32mp1_clk_gate const *gate) |
| 1053 | { |
| 1054 | uintptr_t rcc_base = stm32mp_rcc_base(); |
| 1055 | |
Etienne Carriere | 8a66889 | 2019-12-08 08:21:08 +0100 | [diff] [blame] | 1056 | VERBOSE("Enable clock %u\n", gate->index); |
| 1057 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1058 | if (gate->set_clr != 0U) { |
| 1059 | mmio_write_32(rcc_base + gate->offset, BIT(gate->bit)); |
| 1060 | } else { |
| 1061 | mmio_setbits_32(rcc_base + gate->offset, BIT(gate->bit)); |
| 1062 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | static void __clk_disable(struct stm32mp1_clk_gate const *gate) |
| 1066 | { |
| 1067 | uintptr_t rcc_base = stm32mp_rcc_base(); |
| 1068 | |
Etienne Carriere | 8a66889 | 2019-12-08 08:21:08 +0100 | [diff] [blame] | 1069 | VERBOSE("Disable clock %u\n", gate->index); |
| 1070 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1071 | if (gate->set_clr != 0U) { |
| 1072 | mmio_write_32(rcc_base + gate->offset + RCC_MP_ENCLRR_OFFSET, |
| 1073 | BIT(gate->bit)); |
| 1074 | } else { |
| 1075 | mmio_clrbits_32(rcc_base + gate->offset, BIT(gate->bit)); |
| 1076 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | static bool __clk_is_enabled(struct stm32mp1_clk_gate const *gate) |
| 1080 | { |
| 1081 | uintptr_t rcc_base = stm32mp_rcc_base(); |
| 1082 | |
| 1083 | return mmio_read_32(rcc_base + gate->offset) & BIT(gate->bit); |
| 1084 | } |
| 1085 | |
Etienne Carriere | 481aa00 | 2019-12-08 08:21:44 +0100 | [diff] [blame] | 1086 | /* Oscillators and PLLs are not gated at runtime */ |
| 1087 | static bool clock_is_always_on(unsigned long id) |
| 1088 | { |
| 1089 | switch (id) { |
| 1090 | case CK_HSE: |
| 1091 | case CK_CSI: |
| 1092 | case CK_LSI: |
| 1093 | case CK_LSE: |
| 1094 | case CK_HSI: |
| 1095 | case CK_HSE_DIV2: |
| 1096 | case PLL1_Q: |
| 1097 | case PLL1_R: |
| 1098 | case PLL2_P: |
| 1099 | case PLL2_Q: |
| 1100 | case PLL2_R: |
| 1101 | case PLL3_P: |
| 1102 | case PLL3_Q: |
| 1103 | case PLL3_R: |
Yann Gautier | b39a152 | 2020-09-16 16:41:55 +0200 | [diff] [blame] | 1104 | case CK_AXI: |
| 1105 | case CK_MPU: |
| 1106 | case CK_MCU: |
HE Shushan | c47c816 | 2021-07-12 23:04:10 +0200 | [diff] [blame] | 1107 | case RTC: |
Etienne Carriere | 481aa00 | 2019-12-08 08:21:44 +0100 | [diff] [blame] | 1108 | return true; |
| 1109 | default: |
| 1110 | return false; |
| 1111 | } |
| 1112 | } |
| 1113 | |
Yann Gautier | ad730b5 | 2022-01-19 13:57:49 +0100 | [diff] [blame] | 1114 | static void __stm32mp1_clk_enable(unsigned long id, bool with_refcnt) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1115 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1116 | const struct stm32mp1_clk_gate *gate; |
Etienne Carriere | 481aa00 | 2019-12-08 08:21:44 +0100 | [diff] [blame] | 1117 | int i; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1118 | |
Etienne Carriere | 481aa00 | 2019-12-08 08:21:44 +0100 | [diff] [blame] | 1119 | if (clock_is_always_on(id)) { |
| 1120 | return; |
| 1121 | } |
| 1122 | |
| 1123 | i = stm32mp1_clk_get_gated_id(id); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1124 | if (i < 0) { |
Yann Gautier | c934381 | 2021-09-07 09:05:44 +0200 | [diff] [blame] | 1125 | ERROR("Clock %lu can't be enabled\n", id); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1126 | panic(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1129 | gate = gate_ref(i); |
Yann Gautier | ad730b5 | 2022-01-19 13:57:49 +0100 | [diff] [blame] | 1130 | |
| 1131 | if (!with_refcnt) { |
| 1132 | __clk_enable(gate); |
| 1133 | return; |
| 1134 | } |
Yann Gautier | af1e8f4 | 2021-10-27 18:21:11 +0200 | [diff] [blame] | 1135 | |
| 1136 | #if defined(IMAGE_BL32) |
| 1137 | if (gate_is_non_secure(gate)) { |
| 1138 | /* Enable non-secure clock w/o any refcounting */ |
| 1139 | __clk_enable(gate); |
| 1140 | return; |
| 1141 | } |
| 1142 | #endif |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1143 | |
| 1144 | stm32mp1_clk_lock(&refcount_lock); |
| 1145 | |
Yann Gautier | ad730b5 | 2022-01-19 13:57:49 +0100 | [diff] [blame] | 1146 | if (gate_refcounts[i] == 0U) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1147 | __clk_enable(gate); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1148 | } |
| 1149 | |
Yann Gautier | ad730b5 | 2022-01-19 13:57:49 +0100 | [diff] [blame] | 1150 | gate_refcounts[i]++; |
| 1151 | if (gate_refcounts[i] == UINT_MAX) { |
| 1152 | ERROR("Clock %lu refcount reached max value\n", id); |
| 1153 | panic(); |
| 1154 | } |
| 1155 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1156 | stm32mp1_clk_unlock(&refcount_lock); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1157 | } |
| 1158 | |
Yann Gautier | ad730b5 | 2022-01-19 13:57:49 +0100 | [diff] [blame] | 1159 | static void __stm32mp1_clk_disable(unsigned long id, bool with_refcnt) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1160 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1161 | const struct stm32mp1_clk_gate *gate; |
Etienne Carriere | 481aa00 | 2019-12-08 08:21:44 +0100 | [diff] [blame] | 1162 | int i; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1163 | |
Etienne Carriere | 481aa00 | 2019-12-08 08:21:44 +0100 | [diff] [blame] | 1164 | if (clock_is_always_on(id)) { |
| 1165 | return; |
| 1166 | } |
| 1167 | |
| 1168 | i = stm32mp1_clk_get_gated_id(id); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1169 | if (i < 0) { |
Yann Gautier | c934381 | 2021-09-07 09:05:44 +0200 | [diff] [blame] | 1170 | ERROR("Clock %lu can't be disabled\n", id); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1171 | panic(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1174 | gate = gate_ref(i); |
Yann Gautier | ad730b5 | 2022-01-19 13:57:49 +0100 | [diff] [blame] | 1175 | |
| 1176 | if (!with_refcnt) { |
| 1177 | __clk_disable(gate); |
| 1178 | return; |
| 1179 | } |
Yann Gautier | af1e8f4 | 2021-10-27 18:21:11 +0200 | [diff] [blame] | 1180 | |
| 1181 | #if defined(IMAGE_BL32) |
| 1182 | if (gate_is_non_secure(gate)) { |
| 1183 | /* Don't disable non-secure clocks */ |
| 1184 | return; |
| 1185 | } |
| 1186 | #endif |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1187 | |
| 1188 | stm32mp1_clk_lock(&refcount_lock); |
| 1189 | |
Yann Gautier | ad730b5 | 2022-01-19 13:57:49 +0100 | [diff] [blame] | 1190 | if (gate_refcounts[i] == 0U) { |
| 1191 | ERROR("Clock %lu refcount reached 0\n", id); |
| 1192 | panic(); |
| 1193 | } |
| 1194 | gate_refcounts[i]--; |
| 1195 | |
| 1196 | if (gate_refcounts[i] == 0U) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1197 | __clk_disable(gate); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1198 | } |
| 1199 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1200 | stm32mp1_clk_unlock(&refcount_lock); |
| 1201 | } |
| 1202 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 1203 | static int stm32mp_clk_enable(unsigned long id) |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1204 | { |
| 1205 | __stm32mp1_clk_enable(id, true); |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 1206 | |
| 1207 | return 0; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 1210 | static void stm32mp_clk_disable(unsigned long id) |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1211 | { |
| 1212 | __stm32mp1_clk_disable(id, true); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1213 | } |
| 1214 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 1215 | static bool stm32mp_clk_is_enabled(unsigned long id) |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1216 | { |
Etienne Carriere | 481aa00 | 2019-12-08 08:21:44 +0100 | [diff] [blame] | 1217 | int i; |
| 1218 | |
| 1219 | if (clock_is_always_on(id)) { |
| 1220 | return true; |
| 1221 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1222 | |
Etienne Carriere | 481aa00 | 2019-12-08 08:21:44 +0100 | [diff] [blame] | 1223 | i = stm32mp1_clk_get_gated_id(id); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1224 | if (i < 0) { |
| 1225 | panic(); |
| 1226 | } |
| 1227 | |
| 1228 | return __clk_is_enabled(gate_ref(i)); |
| 1229 | } |
| 1230 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 1231 | static unsigned long stm32mp_clk_get_rate(unsigned long id) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1232 | { |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 1233 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1234 | int p = stm32mp1_clk_get_parent(id); |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 1235 | uint32_t prescaler, timpre; |
| 1236 | unsigned long parent_rate; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1237 | |
| 1238 | if (p < 0) { |
| 1239 | return 0; |
| 1240 | } |
| 1241 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 1242 | parent_rate = get_clock_rate(p); |
| 1243 | |
| 1244 | switch (id) { |
| 1245 | case TIM2_K: |
| 1246 | case TIM3_K: |
| 1247 | case TIM4_K: |
| 1248 | case TIM5_K: |
| 1249 | case TIM6_K: |
| 1250 | case TIM7_K: |
| 1251 | case TIM12_K: |
| 1252 | case TIM13_K: |
| 1253 | case TIM14_K: |
| 1254 | prescaler = mmio_read_32(rcc_base + RCC_APB1DIVR) & |
| 1255 | RCC_APBXDIV_MASK; |
| 1256 | timpre = mmio_read_32(rcc_base + RCC_TIMG1PRER) & |
| 1257 | RCC_TIMGXPRER_TIMGXPRE; |
| 1258 | break; |
| 1259 | |
| 1260 | case TIM1_K: |
| 1261 | case TIM8_K: |
| 1262 | case TIM15_K: |
| 1263 | case TIM16_K: |
| 1264 | case TIM17_K: |
| 1265 | prescaler = mmio_read_32(rcc_base + RCC_APB2DIVR) & |
| 1266 | RCC_APBXDIV_MASK; |
| 1267 | timpre = mmio_read_32(rcc_base + RCC_TIMG2PRER) & |
| 1268 | RCC_TIMGXPRER_TIMGXPRE; |
| 1269 | break; |
| 1270 | |
| 1271 | default: |
| 1272 | return parent_rate; |
| 1273 | } |
| 1274 | |
| 1275 | if (prescaler == 0U) { |
| 1276 | return parent_rate; |
| 1277 | } |
| 1278 | |
| 1279 | return parent_rate * (timpre + 1U) * 2U; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1280 | } |
| 1281 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1282 | static void stm32mp1_ls_osc_set(bool enable, uint32_t offset, uint32_t mask_on) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1283 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1284 | uintptr_t address = stm32mp_rcc_base() + offset; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1285 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1286 | if (enable) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1287 | mmio_setbits_32(address, mask_on); |
| 1288 | } else { |
| 1289 | mmio_clrbits_32(address, mask_on); |
| 1290 | } |
| 1291 | } |
| 1292 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1293 | static void stm32mp1_hs_ocs_set(bool enable, uint32_t mask_on) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1294 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1295 | uint32_t offset = enable ? RCC_OCENSETR : RCC_OCENCLRR; |
| 1296 | uintptr_t address = stm32mp_rcc_base() + offset; |
| 1297 | |
| 1298 | mmio_write_32(address, mask_on); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1301 | static int stm32mp1_osc_wait(bool enable, uint32_t offset, uint32_t mask_rdy) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1302 | { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1303 | uint64_t timeout; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1304 | uint32_t mask_test; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1305 | uintptr_t address = stm32mp_rcc_base() + offset; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1306 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1307 | if (enable) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1308 | mask_test = mask_rdy; |
| 1309 | } else { |
| 1310 | mask_test = 0; |
| 1311 | } |
| 1312 | |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1313 | timeout = timeout_init_us(OSCRDY_TIMEOUT); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1314 | while ((mmio_read_32(address) & mask_rdy) != mask_test) { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1315 | if (timeout_elapsed(timeout)) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1316 | ERROR("OSC %x @ %lx timeout for enable=%d : 0x%x\n", |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1317 | mask_rdy, address, enable, mmio_read_32(address)); |
| 1318 | return -ETIMEDOUT; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1325 | static void stm32mp1_lse_enable(bool bypass, bool digbyp, uint32_t lsedrv) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1326 | { |
| 1327 | uint32_t value; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1328 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1329 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1330 | if (digbyp) { |
| 1331 | mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_DIGBYP); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1334 | if (bypass || digbyp) { |
| 1335 | mmio_setbits_32(rcc_base + RCC_BDCR, RCC_BDCR_LSEBYP); |
| 1336 | } |
| 1337 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1338 | /* |
| 1339 | * Warning: not recommended to switch directly from "high drive" |
| 1340 | * to "medium low drive", and vice-versa. |
| 1341 | */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1342 | value = (mmio_read_32(rcc_base + RCC_BDCR) & RCC_BDCR_LSEDRV_MASK) >> |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1343 | RCC_BDCR_LSEDRV_SHIFT; |
| 1344 | |
| 1345 | while (value != lsedrv) { |
| 1346 | if (value > lsedrv) { |
| 1347 | value--; |
| 1348 | } else { |
| 1349 | value++; |
| 1350 | } |
| 1351 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1352 | mmio_clrsetbits_32(rcc_base + RCC_BDCR, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1353 | RCC_BDCR_LSEDRV_MASK, |
| 1354 | value << RCC_BDCR_LSEDRV_SHIFT); |
| 1355 | } |
| 1356 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1357 | stm32mp1_ls_osc_set(true, RCC_BDCR, RCC_BDCR_LSEON); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1358 | } |
| 1359 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1360 | static void stm32mp1_lse_wait(void) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1361 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1362 | if (stm32mp1_osc_wait(true, RCC_BDCR, RCC_BDCR_LSERDY) != 0) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1363 | VERBOSE("%s: failed\n", __func__); |
| 1364 | } |
| 1365 | } |
| 1366 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1367 | static void stm32mp1_lsi_set(bool enable) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1368 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1369 | stm32mp1_ls_osc_set(enable, RCC_RDLSICR, RCC_RDLSICR_LSION); |
| 1370 | |
| 1371 | if (stm32mp1_osc_wait(enable, RCC_RDLSICR, RCC_RDLSICR_LSIRDY) != 0) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1372 | VERBOSE("%s: failed\n", __func__); |
| 1373 | } |
| 1374 | } |
| 1375 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1376 | static void stm32mp1_hse_enable(bool bypass, bool digbyp, bool css) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1377 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1378 | uintptr_t rcc_base = stm32mp_rcc_base(); |
| 1379 | |
| 1380 | if (digbyp) { |
| 1381 | mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_DIGBYP); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1384 | if (bypass || digbyp) { |
| 1385 | mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSEBYP); |
| 1386 | } |
| 1387 | |
| 1388 | stm32mp1_hs_ocs_set(true, RCC_OCENR_HSEON); |
| 1389 | if (stm32mp1_osc_wait(true, RCC_OCRDYR, RCC_OCRDYR_HSERDY) != 0) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1390 | VERBOSE("%s: failed\n", __func__); |
| 1391 | } |
| 1392 | |
| 1393 | if (css) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1394 | mmio_write_32(rcc_base + RCC_OCENSETR, RCC_OCENR_HSECSSON); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1395 | } |
Lionel Debieve | d78bd85 | 2019-07-02 18:03:34 +0200 | [diff] [blame] | 1396 | |
| 1397 | #if STM32MP_UART_PROGRAMMER || STM32MP_USB_PROGRAMMER |
| 1398 | if ((mmio_read_32(rcc_base + RCC_OCENSETR) & RCC_OCENR_HSEBYP) && |
| 1399 | (!(digbyp || bypass))) { |
| 1400 | panic(); |
| 1401 | } |
| 1402 | #endif |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1403 | } |
| 1404 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1405 | static void stm32mp1_csi_set(bool enable) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1406 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1407 | stm32mp1_hs_ocs_set(enable, RCC_OCENR_CSION); |
| 1408 | if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_CSIRDY) != 0) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1409 | VERBOSE("%s: failed\n", __func__); |
| 1410 | } |
| 1411 | } |
| 1412 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1413 | static void stm32mp1_hsi_set(bool enable) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1414 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1415 | stm32mp1_hs_ocs_set(enable, RCC_OCENR_HSION); |
| 1416 | if (stm32mp1_osc_wait(enable, RCC_OCRDYR, RCC_OCRDYR_HSIRDY) != 0) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1417 | VERBOSE("%s: failed\n", __func__); |
| 1418 | } |
| 1419 | } |
| 1420 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1421 | static int stm32mp1_set_hsidiv(uint8_t hsidiv) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1422 | { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1423 | uint64_t timeout; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1424 | uintptr_t rcc_base = stm32mp_rcc_base(); |
| 1425 | uintptr_t address = rcc_base + RCC_OCRDYR; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1426 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1427 | mmio_clrsetbits_32(rcc_base + RCC_HSICFGR, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1428 | RCC_HSICFGR_HSIDIV_MASK, |
| 1429 | RCC_HSICFGR_HSIDIV_MASK & (uint32_t)hsidiv); |
| 1430 | |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1431 | timeout = timeout_init_us(HSIDIV_TIMEOUT); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1432 | while ((mmio_read_32(address) & RCC_OCRDYR_HSIDIVRDY) == 0U) { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1433 | if (timeout_elapsed(timeout)) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1434 | ERROR("HSIDIV failed @ 0x%lx: 0x%x\n", |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1435 | address, mmio_read_32(address)); |
| 1436 | return -ETIMEDOUT; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1443 | static int stm32mp1_hsidiv(unsigned long hsifreq) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1444 | { |
| 1445 | uint8_t hsidiv; |
| 1446 | uint32_t hsidivfreq = MAX_HSI_HZ; |
| 1447 | |
| 1448 | for (hsidiv = 0; hsidiv < 4U; hsidiv++) { |
| 1449 | if (hsidivfreq == hsifreq) { |
| 1450 | break; |
| 1451 | } |
| 1452 | |
| 1453 | hsidivfreq /= 2U; |
| 1454 | } |
| 1455 | |
| 1456 | if (hsidiv == 4U) { |
| 1457 | ERROR("Invalid clk-hsi frequency\n"); |
| 1458 | return -1; |
| 1459 | } |
| 1460 | |
| 1461 | if (hsidiv != 0U) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1462 | return stm32mp1_set_hsidiv(hsidiv); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | return 0; |
| 1466 | } |
| 1467 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1468 | static bool stm32mp1_check_pll_conf(enum stm32mp1_pll_id pll_id, |
| 1469 | unsigned int clksrc, |
| 1470 | uint32_t *pllcfg, int plloff) |
| 1471 | { |
| 1472 | const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); |
| 1473 | uintptr_t rcc_base = stm32mp_rcc_base(); |
| 1474 | uintptr_t pllxcr = rcc_base + pll->pllxcr; |
| 1475 | enum stm32mp1_plltype type = pll->plltype; |
| 1476 | uintptr_t clksrc_address = rcc_base + (clksrc >> 4); |
| 1477 | unsigned long refclk; |
| 1478 | uint32_t ifrge = 0U; |
Andre Przywara | 2d5690c | 2020-03-26 11:50:33 +0000 | [diff] [blame] | 1479 | uint32_t src, value, fracv = 0; |
| 1480 | void *fdt; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1481 | |
| 1482 | /* Check PLL output */ |
| 1483 | if (mmio_read_32(pllxcr) != RCC_PLLNCR_PLLON) { |
| 1484 | return false; |
| 1485 | } |
| 1486 | |
| 1487 | /* Check current clksrc */ |
| 1488 | src = mmio_read_32(clksrc_address) & RCC_SELR_SRC_MASK; |
| 1489 | if (src != (clksrc & RCC_SELR_SRC_MASK)) { |
| 1490 | return false; |
| 1491 | } |
| 1492 | |
| 1493 | /* Check Div */ |
| 1494 | src = mmio_read_32(rcc_base + pll->rckxselr) & RCC_SELR_REFCLK_SRC_MASK; |
| 1495 | |
| 1496 | refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) / |
| 1497 | (pllcfg[PLLCFG_M] + 1U); |
| 1498 | |
| 1499 | if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) || |
| 1500 | (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) { |
| 1501 | return false; |
| 1502 | } |
| 1503 | |
| 1504 | if ((type == PLL_800) && (refclk >= 8000000U)) { |
| 1505 | ifrge = 1U; |
| 1506 | } |
| 1507 | |
| 1508 | value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) & |
| 1509 | RCC_PLLNCFGR1_DIVN_MASK; |
| 1510 | value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) & |
| 1511 | RCC_PLLNCFGR1_DIVM_MASK; |
| 1512 | value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) & |
| 1513 | RCC_PLLNCFGR1_IFRGE_MASK; |
| 1514 | if (mmio_read_32(rcc_base + pll->pllxcfgr1) != value) { |
| 1515 | return false; |
| 1516 | } |
| 1517 | |
| 1518 | /* Fractional configuration */ |
Andre Przywara | 2d5690c | 2020-03-26 11:50:33 +0000 | [diff] [blame] | 1519 | if (fdt_get_address(&fdt) == 1) { |
| 1520 | fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0); |
| 1521 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1522 | |
| 1523 | value = fracv << RCC_PLLNFRACR_FRACV_SHIFT; |
| 1524 | value |= RCC_PLLNFRACR_FRACLE; |
| 1525 | if (mmio_read_32(rcc_base + pll->pllxfracr) != value) { |
| 1526 | return false; |
| 1527 | } |
| 1528 | |
| 1529 | /* Output config */ |
| 1530 | value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) & |
| 1531 | RCC_PLLNCFGR2_DIVP_MASK; |
| 1532 | value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) & |
| 1533 | RCC_PLLNCFGR2_DIVQ_MASK; |
| 1534 | value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) & |
| 1535 | RCC_PLLNCFGR2_DIVR_MASK; |
| 1536 | if (mmio_read_32(rcc_base + pll->pllxcfgr2) != value) { |
| 1537 | return false; |
| 1538 | } |
| 1539 | |
| 1540 | return true; |
| 1541 | } |
| 1542 | |
| 1543 | static void stm32mp1_pll_start(enum stm32mp1_pll_id pll_id) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1544 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1545 | const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); |
| 1546 | uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1547 | |
Yann Gautier | d0dcbaa | 2019-06-04 15:55:37 +0200 | [diff] [blame] | 1548 | /* Preserve RCC_PLLNCR_SSCG_CTRL value */ |
| 1549 | mmio_clrsetbits_32(pllxcr, |
| 1550 | RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | |
| 1551 | RCC_PLLNCR_DIVREN, |
| 1552 | RCC_PLLNCR_PLLON); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1553 | } |
| 1554 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1555 | static int stm32mp1_pll_output(enum stm32mp1_pll_id pll_id, uint32_t output) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1556 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1557 | const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); |
| 1558 | uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr; |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1559 | uint64_t timeout = timeout_init_us(PLLRDY_TIMEOUT); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1560 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1561 | /* Wait PLL lock */ |
| 1562 | while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) == 0U) { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1563 | if (timeout_elapsed(timeout)) { |
Yann Gautier | 4ce04cc | 2022-02-28 11:34:05 +0100 | [diff] [blame] | 1564 | ERROR("PLL%u start failed @ 0x%lx: 0x%x\n", |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1565 | pll_id, pllxcr, mmio_read_32(pllxcr)); |
| 1566 | return -ETIMEDOUT; |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | /* Start the requested output */ |
| 1571 | mmio_setbits_32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT); |
| 1572 | |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1576 | static int stm32mp1_pll_stop(enum stm32mp1_pll_id pll_id) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1577 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1578 | const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); |
| 1579 | uintptr_t pllxcr = stm32mp_rcc_base() + pll->pllxcr; |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1580 | uint64_t timeout; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1581 | |
| 1582 | /* Stop all output */ |
| 1583 | mmio_clrbits_32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | |
| 1584 | RCC_PLLNCR_DIVREN); |
| 1585 | |
| 1586 | /* Stop PLL */ |
| 1587 | mmio_clrbits_32(pllxcr, RCC_PLLNCR_PLLON); |
| 1588 | |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1589 | timeout = timeout_init_us(PLLRDY_TIMEOUT); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1590 | /* Wait PLL stopped */ |
| 1591 | while ((mmio_read_32(pllxcr) & RCC_PLLNCR_PLLRDY) != 0U) { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1592 | if (timeout_elapsed(timeout)) { |
Yann Gautier | 4ce04cc | 2022-02-28 11:34:05 +0100 | [diff] [blame] | 1593 | ERROR("PLL%u stop failed @ 0x%lx: 0x%x\n", |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1594 | pll_id, pllxcr, mmio_read_32(pllxcr)); |
| 1595 | return -ETIMEDOUT; |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1602 | static void stm32mp1_pll_config_output(enum stm32mp1_pll_id pll_id, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1603 | uint32_t *pllcfg) |
| 1604 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1605 | const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); |
| 1606 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1607 | uint32_t value; |
| 1608 | |
| 1609 | value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) & |
| 1610 | RCC_PLLNCFGR2_DIVP_MASK; |
| 1611 | value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) & |
| 1612 | RCC_PLLNCFGR2_DIVQ_MASK; |
| 1613 | value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) & |
| 1614 | RCC_PLLNCFGR2_DIVR_MASK; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1615 | mmio_write_32(rcc_base + pll->pllxcfgr2, value); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1616 | } |
| 1617 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1618 | static int stm32mp1_pll_config(enum stm32mp1_pll_id pll_id, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1619 | uint32_t *pllcfg, uint32_t fracv) |
| 1620 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1621 | const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); |
| 1622 | uintptr_t rcc_base = stm32mp_rcc_base(); |
| 1623 | enum stm32mp1_plltype type = pll->plltype; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1624 | unsigned long refclk; |
| 1625 | uint32_t ifrge = 0; |
| 1626 | uint32_t src, value; |
| 1627 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1628 | src = mmio_read_32(rcc_base + pll->rckxselr) & |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1629 | RCC_SELR_REFCLK_SRC_MASK; |
| 1630 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1631 | refclk = stm32mp1_clk_get_fixed(pll->refclk[src]) / |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1632 | (pllcfg[PLLCFG_M] + 1U); |
| 1633 | |
| 1634 | if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) || |
| 1635 | (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) { |
| 1636 | return -EINVAL; |
| 1637 | } |
| 1638 | |
| 1639 | if ((type == PLL_800) && (refclk >= 8000000U)) { |
| 1640 | ifrge = 1U; |
| 1641 | } |
| 1642 | |
| 1643 | value = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) & |
| 1644 | RCC_PLLNCFGR1_DIVN_MASK; |
| 1645 | value |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) & |
| 1646 | RCC_PLLNCFGR1_DIVM_MASK; |
| 1647 | value |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) & |
| 1648 | RCC_PLLNCFGR1_IFRGE_MASK; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1649 | mmio_write_32(rcc_base + pll->pllxcfgr1, value); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1650 | |
| 1651 | /* Fractional configuration */ |
| 1652 | value = 0; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1653 | mmio_write_32(rcc_base + pll->pllxfracr, value); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1654 | |
| 1655 | value = fracv << RCC_PLLNFRACR_FRACV_SHIFT; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1656 | mmio_write_32(rcc_base + pll->pllxfracr, value); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1657 | |
| 1658 | value |= RCC_PLLNFRACR_FRACLE; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1659 | mmio_write_32(rcc_base + pll->pllxfracr, value); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1660 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1661 | stm32mp1_pll_config_output(pll_id, pllcfg); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1662 | |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1666 | static void stm32mp1_pll_csg(enum stm32mp1_pll_id pll_id, uint32_t *csg) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1667 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1668 | const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1669 | uint32_t pllxcsg = 0; |
| 1670 | |
| 1671 | pllxcsg |= (csg[PLLCSG_MOD_PER] << RCC_PLLNCSGR_MOD_PER_SHIFT) & |
| 1672 | RCC_PLLNCSGR_MOD_PER_MASK; |
| 1673 | |
| 1674 | pllxcsg |= (csg[PLLCSG_INC_STEP] << RCC_PLLNCSGR_INC_STEP_SHIFT) & |
| 1675 | RCC_PLLNCSGR_INC_STEP_MASK; |
| 1676 | |
| 1677 | pllxcsg |= (csg[PLLCSG_SSCG_MODE] << RCC_PLLNCSGR_SSCG_MODE_SHIFT) & |
| 1678 | RCC_PLLNCSGR_SSCG_MODE_MASK; |
| 1679 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1680 | mmio_write_32(stm32mp_rcc_base() + pll->pllxcsgr, pllxcsg); |
Yann Gautier | d0dcbaa | 2019-06-04 15:55:37 +0200 | [diff] [blame] | 1681 | |
| 1682 | mmio_setbits_32(stm32mp_rcc_base() + pll->pllxcr, |
| 1683 | RCC_PLLNCR_SSCG_CTRL); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1684 | } |
| 1685 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1686 | static int stm32mp1_set_clksrc(unsigned int clksrc) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1687 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1688 | uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4); |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1689 | uint64_t timeout; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1690 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1691 | mmio_clrsetbits_32(clksrc_address, RCC_SELR_SRC_MASK, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1692 | clksrc & RCC_SELR_SRC_MASK); |
| 1693 | |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1694 | timeout = timeout_init_us(CLKSRC_TIMEOUT); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1695 | while ((mmio_read_32(clksrc_address) & RCC_SELR_SRCRDY) == 0U) { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1696 | if (timeout_elapsed(timeout)) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1697 | ERROR("CLKSRC %x start failed @ 0x%lx: 0x%x\n", clksrc, |
| 1698 | clksrc_address, mmio_read_32(clksrc_address)); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1699 | return -ETIMEDOUT; |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1706 | static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1707 | { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1708 | uint64_t timeout; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1709 | |
| 1710 | mmio_clrsetbits_32(address, RCC_DIVR_DIV_MASK, |
| 1711 | clkdiv & RCC_DIVR_DIV_MASK); |
| 1712 | |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1713 | timeout = timeout_init_us(CLKDIV_TIMEOUT); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1714 | while ((mmio_read_32(address) & RCC_DIVR_DIVRDY) == 0U) { |
Yann Gautier | 2299d57 | 2019-02-14 11:14:39 +0100 | [diff] [blame] | 1715 | if (timeout_elapsed(timeout)) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1716 | ERROR("CLKDIV %x start failed @ 0x%lx: 0x%x\n", |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1717 | clkdiv, address, mmio_read_32(address)); |
| 1718 | return -ETIMEDOUT; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1725 | static void stm32mp1_mco_csg(uint32_t clksrc, uint32_t clkdiv) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1726 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1727 | uintptr_t clksrc_address = stm32mp_rcc_base() + (clksrc >> 4); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1728 | |
| 1729 | /* |
| 1730 | * Binding clksrc : |
| 1731 | * bit15-4 offset |
| 1732 | * bit3: disable |
| 1733 | * bit2-0: MCOSEL[2:0] |
| 1734 | */ |
| 1735 | if ((clksrc & 0x8U) != 0U) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1736 | mmio_clrbits_32(clksrc_address, RCC_MCOCFG_MCOON); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1737 | } else { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1738 | mmio_clrsetbits_32(clksrc_address, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1739 | RCC_MCOCFG_MCOSRC_MASK, |
| 1740 | clksrc & RCC_MCOCFG_MCOSRC_MASK); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1741 | mmio_clrsetbits_32(clksrc_address, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1742 | RCC_MCOCFG_MCODIV_MASK, |
| 1743 | clkdiv << RCC_MCOCFG_MCODIV_SHIFT); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1744 | mmio_setbits_32(clksrc_address, RCC_MCOCFG_MCOON); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1745 | } |
| 1746 | } |
| 1747 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1748 | static void stm32mp1_set_rtcsrc(unsigned int clksrc, bool lse_css) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1749 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1750 | uintptr_t address = stm32mp_rcc_base() + RCC_BDCR; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1751 | |
| 1752 | if (((mmio_read_32(address) & RCC_BDCR_RTCCKEN) == 0U) || |
| 1753 | (clksrc != (uint32_t)CLK_RTC_DISABLED)) { |
| 1754 | mmio_clrsetbits_32(address, |
| 1755 | RCC_BDCR_RTCSRC_MASK, |
Yann Gautier | 74aa83a | 2021-04-06 13:41:19 +0200 | [diff] [blame] | 1756 | (clksrc & RCC_SELR_SRC_MASK) << RCC_BDCR_RTCSRC_SHIFT); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1757 | |
| 1758 | mmio_setbits_32(address, RCC_BDCR_RTCCKEN); |
| 1759 | } |
| 1760 | |
| 1761 | if (lse_css) { |
| 1762 | mmio_setbits_32(address, RCC_BDCR_LSECSSON); |
| 1763 | } |
| 1764 | } |
| 1765 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1766 | static void stm32mp1_pkcs_config(uint32_t pkcs) |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1767 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1768 | uintptr_t address = stm32mp_rcc_base() + ((pkcs >> 4) & 0xFFFU); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1769 | uint32_t value = pkcs & 0xFU; |
| 1770 | uint32_t mask = 0xFU; |
| 1771 | |
| 1772 | if ((pkcs & BIT(31)) != 0U) { |
| 1773 | mask <<= 4; |
| 1774 | value <<= 4; |
| 1775 | } |
| 1776 | |
| 1777 | mmio_clrsetbits_32(address, mask, value); |
| 1778 | } |
| 1779 | |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 1780 | static int clk_get_pll_settings_from_dt(int plloff, unsigned int *pllcfg, |
| 1781 | uint32_t *fracv, uint32_t *csg, |
| 1782 | bool *csg_set) |
| 1783 | { |
| 1784 | void *fdt; |
| 1785 | int ret; |
| 1786 | |
| 1787 | if (fdt_get_address(&fdt) == 0) { |
| 1788 | return -FDT_ERR_NOTFOUND; |
| 1789 | } |
| 1790 | |
| 1791 | ret = fdt_read_uint32_array(fdt, plloff, "cfg", (uint32_t)PLLCFG_NB, |
| 1792 | pllcfg); |
| 1793 | if (ret < 0) { |
| 1794 | return -FDT_ERR_NOTFOUND; |
| 1795 | } |
| 1796 | |
| 1797 | *fracv = fdt_read_uint32_default(fdt, plloff, "frac", 0); |
| 1798 | |
| 1799 | ret = fdt_read_uint32_array(fdt, plloff, "csg", (uint32_t)PLLCSG_NB, |
| 1800 | csg); |
| 1801 | |
| 1802 | *csg_set = (ret == 0); |
| 1803 | |
| 1804 | if (ret == -FDT_ERR_NOTFOUND) { |
| 1805 | ret = 0; |
| 1806 | } |
| 1807 | |
| 1808 | return ret; |
| 1809 | } |
| 1810 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1811 | int stm32mp1_clk_init(void) |
| 1812 | { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1813 | uintptr_t rcc_base = stm32mp_rcc_base(); |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 1814 | uint32_t pllfracv[_PLL_NB]; |
| 1815 | uint32_t pllcsg[_PLL_NB][PLLCSG_NB]; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1816 | unsigned int clksrc[CLKSRC_NB]; |
| 1817 | unsigned int clkdiv[CLKDIV_NB]; |
| 1818 | unsigned int pllcfg[_PLL_NB][PLLCFG_NB]; |
| 1819 | int plloff[_PLL_NB]; |
| 1820 | int ret, len; |
| 1821 | enum stm32mp1_pll_id i; |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 1822 | bool pllcsg_set[_PLL_NB]; |
| 1823 | bool pllcfg_valid[_PLL_NB]; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1824 | bool lse_css = false; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1825 | bool pll3_preserve = false; |
| 1826 | bool pll4_preserve = false; |
| 1827 | bool pll4_bootrom = false; |
Yann Gautier | f9af3bc | 2018-11-09 15:57:18 +0100 | [diff] [blame] | 1828 | const fdt32_t *pkcs_cell; |
Andre Przywara | cc99f3f | 2020-03-26 12:51:21 +0000 | [diff] [blame] | 1829 | void *fdt; |
Patrick Delaunay | 64e1b2c | 2020-09-04 17:39:12 +0200 | [diff] [blame] | 1830 | int stgen_p = stm32mp1_clk_get_parent(STGEN_K); |
| 1831 | int usbphy_p = stm32mp1_clk_get_parent(USBPHY_K); |
Andre Przywara | cc99f3f | 2020-03-26 12:51:21 +0000 | [diff] [blame] | 1832 | |
| 1833 | if (fdt_get_address(&fdt) == 0) { |
Yann Gautier | 360e0e9 | 2020-09-16 16:40:34 +0200 | [diff] [blame] | 1834 | return -FDT_ERR_NOTFOUND; |
Andre Przywara | cc99f3f | 2020-03-26 12:51:21 +0000 | [diff] [blame] | 1835 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1836 | |
Andre Przywara | cc99f3f | 2020-03-26 12:51:21 +0000 | [diff] [blame] | 1837 | ret = fdt_rcc_read_uint32_array("st,clksrc", (uint32_t)CLKSRC_NB, |
| 1838 | clksrc); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1839 | if (ret < 0) { |
| 1840 | return -FDT_ERR_NOTFOUND; |
| 1841 | } |
| 1842 | |
Andre Przywara | cc99f3f | 2020-03-26 12:51:21 +0000 | [diff] [blame] | 1843 | ret = fdt_rcc_read_uint32_array("st,clkdiv", (uint32_t)CLKDIV_NB, |
| 1844 | clkdiv); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1845 | if (ret < 0) { |
| 1846 | return -FDT_ERR_NOTFOUND; |
| 1847 | } |
| 1848 | |
| 1849 | for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) { |
| 1850 | char name[12]; |
| 1851 | |
Yann Gautier | 4ce04cc | 2022-02-28 11:34:05 +0100 | [diff] [blame] | 1852 | snprintf(name, sizeof(name), "st,pll@%u", i); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1853 | plloff[i] = fdt_rcc_subnode_offset(name); |
| 1854 | |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 1855 | pllcfg_valid[i] = fdt_check_node(plloff[i]); |
| 1856 | if (!pllcfg_valid[i]) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1857 | continue; |
| 1858 | } |
| 1859 | |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 1860 | ret = clk_get_pll_settings_from_dt(plloff[i], pllcfg[i], |
| 1861 | &pllfracv[i], pllcsg[i], |
| 1862 | &pllcsg_set[i]); |
| 1863 | if (ret != 0) { |
| 1864 | return ret; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1868 | stm32mp1_mco_csg(clksrc[CLKSRC_MCO1], clkdiv[CLKDIV_MCO1]); |
| 1869 | stm32mp1_mco_csg(clksrc[CLKSRC_MCO2], clkdiv[CLKDIV_MCO2]); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1870 | |
| 1871 | /* |
| 1872 | * Switch ON oscillator found in device-tree. |
| 1873 | * Note: HSI already ON after BootROM stage. |
| 1874 | */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1875 | if (stm32mp1_osc[_LSI] != 0U) { |
| 1876 | stm32mp1_lsi_set(true); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1877 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1878 | if (stm32mp1_osc[_LSE] != 0U) { |
Gabriel Fernandez | 5177ea2 | 2020-05-15 08:00:03 +0200 | [diff] [blame] | 1879 | const char *name = stm32mp_osc_node_label[_LSE]; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1880 | bool bypass, digbyp; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1881 | uint32_t lsedrv; |
| 1882 | |
Gabriel Fernandez | 5177ea2 | 2020-05-15 08:00:03 +0200 | [diff] [blame] | 1883 | bypass = fdt_clk_read_bool(name, "st,bypass"); |
| 1884 | digbyp = fdt_clk_read_bool(name, "st,digbypass"); |
| 1885 | lse_css = fdt_clk_read_bool(name, "st,css"); |
| 1886 | lsedrv = fdt_clk_read_uint32_default(name, "st,drive", |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1887 | LSEDRV_MEDIUM_HIGH); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1888 | stm32mp1_lse_enable(bypass, digbyp, lsedrv); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1889 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1890 | if (stm32mp1_osc[_HSE] != 0U) { |
Gabriel Fernandez | 5177ea2 | 2020-05-15 08:00:03 +0200 | [diff] [blame] | 1891 | const char *name = stm32mp_osc_node_label[_HSE]; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1892 | bool bypass, digbyp, css; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1893 | |
Gabriel Fernandez | 5177ea2 | 2020-05-15 08:00:03 +0200 | [diff] [blame] | 1894 | bypass = fdt_clk_read_bool(name, "st,bypass"); |
| 1895 | digbyp = fdt_clk_read_bool(name, "st,digbypass"); |
| 1896 | css = fdt_clk_read_bool(name, "st,css"); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1897 | stm32mp1_hse_enable(bypass, digbyp, css); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1898 | } |
| 1899 | /* |
| 1900 | * CSI is mandatory for automatic I/O compensation (SYSCFG_CMPCR) |
| 1901 | * => switch on CSI even if node is not present in device tree |
| 1902 | */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1903 | stm32mp1_csi_set(true); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1904 | |
| 1905 | /* Come back to HSI */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1906 | ret = stm32mp1_set_clksrc(CLK_MPU_HSI); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1907 | if (ret != 0) { |
| 1908 | return ret; |
| 1909 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1910 | ret = stm32mp1_set_clksrc(CLK_AXI_HSI); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1911 | if (ret != 0) { |
| 1912 | return ret; |
| 1913 | } |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 1914 | ret = stm32mp1_set_clksrc(CLK_MCU_HSI); |
| 1915 | if (ret != 0) { |
| 1916 | return ret; |
| 1917 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1918 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1919 | if ((mmio_read_32(rcc_base + RCC_MP_RSTSCLRR) & |
| 1920 | RCC_MP_RSTSCLRR_MPUP0RSTF) != 0) { |
Yann Gautier | fffc76e | 2022-03-04 11:08:47 +0100 | [diff] [blame] | 1921 | if (pllcfg_valid[_PLL3]) { |
| 1922 | pll3_preserve = |
| 1923 | stm32mp1_check_pll_conf(_PLL3, |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1924 | clksrc[CLKSRC_PLL3], |
| 1925 | pllcfg[_PLL3], |
| 1926 | plloff[_PLL3]); |
Yann Gautier | fffc76e | 2022-03-04 11:08:47 +0100 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | if (pllcfg_valid[_PLL4]) { |
| 1930 | pll4_preserve = |
| 1931 | stm32mp1_check_pll_conf(_PLL4, |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1932 | clksrc[CLKSRC_PLL4], |
| 1933 | pllcfg[_PLL4], |
| 1934 | plloff[_PLL4]); |
Yann Gautier | fffc76e | 2022-03-04 11:08:47 +0100 | [diff] [blame] | 1935 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1936 | } |
Patrick Delaunay | 64e1b2c | 2020-09-04 17:39:12 +0200 | [diff] [blame] | 1937 | /* Don't initialize PLL4, when used by BOOTROM */ |
| 1938 | if ((stm32mp_get_boot_itf_selected() == |
| 1939 | BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_USB) && |
| 1940 | ((stgen_p == (int)_PLL4_R) || (usbphy_p == (int)_PLL4_R))) { |
| 1941 | pll4_bootrom = true; |
| 1942 | pll4_preserve = true; |
| 1943 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1944 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1945 | for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1946 | if (((i == _PLL3) && pll3_preserve) || |
| 1947 | ((i == _PLL4) && pll4_preserve)) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1948 | continue; |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1949 | } |
| 1950 | |
| 1951 | ret = stm32mp1_pll_stop(i); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1952 | if (ret != 0) { |
| 1953 | return ret; |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | /* Configure HSIDIV */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1958 | if (stm32mp1_osc[_HSI] != 0U) { |
| 1959 | ret = stm32mp1_hsidiv(stm32mp1_osc[_HSI]); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1960 | if (ret != 0) { |
| 1961 | return ret; |
| 1962 | } |
Lionel Debieve | deef969 | 2019-12-04 21:50:19 +0100 | [diff] [blame] | 1963 | |
| 1964 | stm32mp_stgen_config(stm32mp_clk_get_rate(STGEN_K)); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | /* Select DIV */ |
| 1968 | /* No ready bit when MPUSRC != CLK_MPU_PLL1P_DIV, MPUDIV is disabled */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1969 | mmio_write_32(rcc_base + RCC_MPCKDIVR, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1970 | clkdiv[CLKDIV_MPU] & RCC_DIVR_DIV_MASK); |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1971 | ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_AXI], rcc_base + RCC_AXIDIVR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1972 | if (ret != 0) { |
| 1973 | return ret; |
| 1974 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1975 | ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB4], rcc_base + RCC_APB4DIVR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1976 | if (ret != 0) { |
| 1977 | return ret; |
| 1978 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1979 | ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB5], rcc_base + RCC_APB5DIVR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1980 | if (ret != 0) { |
| 1981 | return ret; |
| 1982 | } |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 1983 | ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_MCU], rcc_base + RCC_MCUDIVR); |
| 1984 | if (ret != 0) { |
| 1985 | return ret; |
| 1986 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1987 | ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB1], rcc_base + RCC_APB1DIVR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1988 | if (ret != 0) { |
| 1989 | return ret; |
| 1990 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1991 | ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB2], rcc_base + RCC_APB2DIVR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1992 | if (ret != 0) { |
| 1993 | return ret; |
| 1994 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 1995 | ret = stm32mp1_set_clkdiv(clkdiv[CLKDIV_APB3], rcc_base + RCC_APB3DIVR); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 1996 | if (ret != 0) { |
| 1997 | return ret; |
| 1998 | } |
| 1999 | |
| 2000 | /* No ready bit for RTC */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2001 | mmio_write_32(rcc_base + RCC_RTCDIVR, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2002 | clkdiv[CLKDIV_RTC] & RCC_DIVR_DIV_MASK); |
| 2003 | |
| 2004 | /* Configure PLLs source */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2005 | ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL12]); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2006 | if (ret != 0) { |
| 2007 | return ret; |
| 2008 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2009 | |
| 2010 | if (!pll3_preserve) { |
| 2011 | ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL3]); |
| 2012 | if (ret != 0) { |
| 2013 | return ret; |
| 2014 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2015 | } |
| 2016 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2017 | if (!pll4_preserve) { |
| 2018 | ret = stm32mp1_set_clksrc(clksrc[CLKSRC_PLL4]); |
| 2019 | if (ret != 0) { |
| 2020 | return ret; |
| 2021 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /* Configure and start PLLs */ |
| 2025 | for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2026 | if (((i == _PLL3) && pll3_preserve) || |
| 2027 | ((i == _PLL4) && pll4_preserve && !pll4_bootrom)) { |
| 2028 | continue; |
| 2029 | } |
| 2030 | |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 2031 | if (!pllcfg_valid[i]) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2032 | continue; |
| 2033 | } |
| 2034 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2035 | if ((i == _PLL4) && pll4_bootrom) { |
| 2036 | /* Set output divider if not done by the Bootrom */ |
| 2037 | stm32mp1_pll_config_output(i, pllcfg[i]); |
| 2038 | continue; |
| 2039 | } |
| 2040 | |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 2041 | ret = stm32mp1_pll_config(i, pllcfg[i], pllfracv[i]); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2042 | if (ret != 0) { |
| 2043 | return ret; |
| 2044 | } |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 2045 | |
| 2046 | if (pllcsg_set[i]) { |
| 2047 | stm32mp1_pll_csg(i, pllcsg[i]); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2048 | } |
| 2049 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2050 | stm32mp1_pll_start(i); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2051 | } |
Elyes Haouas | 2be03c0 | 2023-02-13 09:14:48 +0100 | [diff] [blame] | 2052 | /* Wait and start PLLs output when ready */ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2053 | for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) { |
Nicolas Le Bayon | dab197a | 2019-11-13 11:46:31 +0100 | [diff] [blame] | 2054 | if (!pllcfg_valid[i]) { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2055 | continue; |
| 2056 | } |
| 2057 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2058 | ret = stm32mp1_pll_output(i, pllcfg[i][PLLCFG_O]); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2059 | if (ret != 0) { |
| 2060 | return ret; |
| 2061 | } |
| 2062 | } |
| 2063 | /* Wait LSE ready before to use it */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2064 | if (stm32mp1_osc[_LSE] != 0U) { |
| 2065 | stm32mp1_lse_wait(); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | /* Configure with expected clock source */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2069 | ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MPU]); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2070 | if (ret != 0) { |
| 2071 | return ret; |
| 2072 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2073 | ret = stm32mp1_set_clksrc(clksrc[CLKSRC_AXI]); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2074 | if (ret != 0) { |
| 2075 | return ret; |
| 2076 | } |
Yann Gautier | ed34232 | 2019-02-15 17:33:27 +0100 | [diff] [blame] | 2077 | ret = stm32mp1_set_clksrc(clksrc[CLKSRC_MCU]); |
| 2078 | if (ret != 0) { |
| 2079 | return ret; |
| 2080 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2081 | stm32mp1_set_rtcsrc(clksrc[CLKSRC_RTC], lse_css); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2082 | |
| 2083 | /* Configure PKCK */ |
| 2084 | pkcs_cell = fdt_rcc_read_prop("st,pkcs", &len); |
| 2085 | if (pkcs_cell != NULL) { |
| 2086 | bool ckper_disabled = false; |
| 2087 | uint32_t j; |
Patrick Delaunay | 64e1b2c | 2020-09-04 17:39:12 +0200 | [diff] [blame] | 2088 | uint32_t usbreg_bootrom = 0U; |
| 2089 | |
| 2090 | if (pll4_bootrom) { |
| 2091 | usbreg_bootrom = mmio_read_32(rcc_base + RCC_USBCKSELR); |
| 2092 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2093 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2094 | for (j = 0; j < ((uint32_t)len / sizeof(uint32_t)); j++) { |
Yann Gautier | f9af3bc | 2018-11-09 15:57:18 +0100 | [diff] [blame] | 2095 | uint32_t pkcs = fdt32_to_cpu(pkcs_cell[j]); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2096 | |
| 2097 | if (pkcs == (uint32_t)CLK_CKPER_DISABLED) { |
| 2098 | ckper_disabled = true; |
| 2099 | continue; |
| 2100 | } |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2101 | stm32mp1_pkcs_config(pkcs); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | /* |
| 2105 | * CKPER is source for some peripheral clocks |
| 2106 | * (FMC-NAND / QPSI-NOR) and switching source is allowed |
| 2107 | * only if previous clock is still ON |
| 2108 | * => deactivated CKPER only after switching clock |
| 2109 | */ |
| 2110 | if (ckper_disabled) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2111 | stm32mp1_pkcs_config(CLK_CKPER_DISABLED); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2112 | } |
Patrick Delaunay | 64e1b2c | 2020-09-04 17:39:12 +0200 | [diff] [blame] | 2113 | |
| 2114 | if (pll4_bootrom) { |
| 2115 | uint32_t usbreg_value, usbreg_mask; |
| 2116 | const struct stm32mp1_clk_sel *sel; |
| 2117 | |
| 2118 | sel = clk_sel_ref(_USBPHY_SEL); |
| 2119 | usbreg_mask = (uint32_t)sel->msk << sel->src; |
| 2120 | sel = clk_sel_ref(_USBO_SEL); |
| 2121 | usbreg_mask |= (uint32_t)sel->msk << sel->src; |
| 2122 | |
| 2123 | usbreg_value = mmio_read_32(rcc_base + RCC_USBCKSELR) & |
| 2124 | usbreg_mask; |
| 2125 | usbreg_bootrom &= usbreg_mask; |
| 2126 | if (usbreg_bootrom != usbreg_value) { |
| 2127 | VERBOSE("forbidden new USB clk path\n"); |
| 2128 | VERBOSE("vs bootrom on USB boot\n"); |
| 2129 | return -FDT_ERR_BADVALUE; |
| 2130 | } |
| 2131 | } |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | /* Switch OFF HSI if not found in device-tree */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2135 | if (stm32mp1_osc[_HSI] == 0U) { |
| 2136 | stm32mp1_hsi_set(false); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2137 | } |
Lionel Debieve | deef969 | 2019-12-04 21:50:19 +0100 | [diff] [blame] | 2138 | |
| 2139 | stm32mp_stgen_config(stm32mp_clk_get_rate(STGEN_K)); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2140 | |
| 2141 | /* Software Self-Refresh mode (SSR) during DDR initilialization */ |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2142 | mmio_clrsetbits_32(rcc_base + RCC_DDRITFCR, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2143 | RCC_DDRITFCR_DDRCKMOD_MASK, |
| 2144 | RCC_DDRITFCR_DDRCKMOD_SSR << |
| 2145 | RCC_DDRITFCR_DDRCKMOD_SHIFT); |
| 2146 | |
| 2147 | return 0; |
| 2148 | } |
| 2149 | |
| 2150 | static void stm32mp1_osc_clk_init(const char *name, |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2151 | enum stm32mp_osc_id index) |
| 2152 | { |
| 2153 | uint32_t frequency; |
| 2154 | |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2155 | if (fdt_osc_read_freq(name, &frequency) == 0) { |
| 2156 | stm32mp1_osc[index] = frequency; |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2157 | } |
| 2158 | } |
| 2159 | |
| 2160 | static void stm32mp1_osc_init(void) |
| 2161 | { |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2162 | enum stm32mp_osc_id i; |
| 2163 | |
| 2164 | for (i = (enum stm32mp_osc_id)0 ; i < NB_OSC; i++) { |
Yann Gautier | e4a3c35 | 2019-02-14 10:53:33 +0100 | [diff] [blame] | 2165 | stm32mp1_osc_clk_init(stm32mp_osc_node_label[i], i); |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2166 | } |
| 2167 | } |
Etienne Carriere | 1368ada | 2020-05-13 11:49:49 +0200 | [diff] [blame] | 2168 | |
| 2169 | #ifdef STM32MP_SHARED_RESOURCES |
| 2170 | /* |
| 2171 | * Get the parent ID of the target parent clock, for tagging as secure |
| 2172 | * shared clock dependencies. |
| 2173 | */ |
| 2174 | static int get_parent_id_parent(unsigned int parent_id) |
| 2175 | { |
| 2176 | enum stm32mp1_parent_sel s = _UNKNOWN_SEL; |
| 2177 | enum stm32mp1_pll_id pll_id; |
| 2178 | uint32_t p_sel; |
| 2179 | uintptr_t rcc_base = stm32mp_rcc_base(); |
| 2180 | |
| 2181 | switch (parent_id) { |
| 2182 | case _ACLK: |
| 2183 | case _PCLK4: |
| 2184 | case _PCLK5: |
| 2185 | s = _AXIS_SEL; |
| 2186 | break; |
| 2187 | case _PLL1_P: |
| 2188 | case _PLL1_Q: |
| 2189 | case _PLL1_R: |
| 2190 | pll_id = _PLL1; |
| 2191 | break; |
| 2192 | case _PLL2_P: |
| 2193 | case _PLL2_Q: |
| 2194 | case _PLL2_R: |
| 2195 | pll_id = _PLL2; |
| 2196 | break; |
| 2197 | case _PLL3_P: |
| 2198 | case _PLL3_Q: |
| 2199 | case _PLL3_R: |
| 2200 | pll_id = _PLL3; |
| 2201 | break; |
| 2202 | case _PLL4_P: |
| 2203 | case _PLL4_Q: |
| 2204 | case _PLL4_R: |
| 2205 | pll_id = _PLL4; |
| 2206 | break; |
| 2207 | case _PCLK1: |
| 2208 | case _PCLK2: |
| 2209 | case _HCLK2: |
| 2210 | case _HCLK6: |
| 2211 | case _CK_PER: |
| 2212 | case _CK_MPU: |
| 2213 | case _CK_MCU: |
| 2214 | case _USB_PHY_48: |
| 2215 | /* We do not expect to access these */ |
| 2216 | panic(); |
| 2217 | break; |
| 2218 | default: |
| 2219 | /* Other parents have no parent */ |
| 2220 | return -1; |
| 2221 | } |
| 2222 | |
| 2223 | if (s != _UNKNOWN_SEL) { |
| 2224 | const struct stm32mp1_clk_sel *sel = clk_sel_ref(s); |
| 2225 | |
| 2226 | p_sel = (mmio_read_32(rcc_base + sel->offset) >> sel->src) & |
| 2227 | sel->msk; |
| 2228 | |
| 2229 | if (p_sel < sel->nb_parent) { |
| 2230 | return (int)sel->parent[p_sel]; |
| 2231 | } |
| 2232 | } else { |
| 2233 | const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); |
| 2234 | |
| 2235 | p_sel = mmio_read_32(rcc_base + pll->rckxselr) & |
| 2236 | RCC_SELR_REFCLK_SRC_MASK; |
| 2237 | |
| 2238 | if (pll->refclk[p_sel] != _UNKNOWN_OSC_ID) { |
| 2239 | return (int)pll->refclk[p_sel]; |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | VERBOSE("No parent selected for %s\n", |
| 2244 | stm32mp1_clk_parent_name[parent_id]); |
| 2245 | |
| 2246 | return -1; |
| 2247 | } |
| 2248 | |
| 2249 | static void secure_parent_clocks(unsigned long parent_id) |
| 2250 | { |
| 2251 | int grandparent_id; |
| 2252 | |
| 2253 | switch (parent_id) { |
| 2254 | case _PLL3_P: |
| 2255 | case _PLL3_Q: |
| 2256 | case _PLL3_R: |
| 2257 | stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3); |
| 2258 | break; |
| 2259 | |
| 2260 | /* These clocks are always secure when RCC is secure */ |
| 2261 | case _ACLK: |
| 2262 | case _HCLK2: |
| 2263 | case _HCLK6: |
| 2264 | case _PCLK4: |
| 2265 | case _PCLK5: |
| 2266 | case _PLL1_P: |
| 2267 | case _PLL1_Q: |
| 2268 | case _PLL1_R: |
| 2269 | case _PLL2_P: |
| 2270 | case _PLL2_Q: |
| 2271 | case _PLL2_R: |
| 2272 | case _HSI: |
| 2273 | case _HSI_KER: |
| 2274 | case _LSI: |
| 2275 | case _CSI: |
| 2276 | case _CSI_KER: |
| 2277 | case _HSE: |
| 2278 | case _HSE_KER: |
| 2279 | case _HSE_KER_DIV2: |
Gabriel Fernandez | 4e3a51a | 2021-07-27 15:39:16 +0200 | [diff] [blame] | 2280 | case _HSE_RTC: |
Etienne Carriere | 1368ada | 2020-05-13 11:49:49 +0200 | [diff] [blame] | 2281 | case _LSE: |
| 2282 | break; |
| 2283 | |
| 2284 | default: |
| 2285 | VERBOSE("Cannot secure parent clock %s\n", |
| 2286 | stm32mp1_clk_parent_name[parent_id]); |
| 2287 | panic(); |
| 2288 | } |
| 2289 | |
| 2290 | grandparent_id = get_parent_id_parent(parent_id); |
| 2291 | if (grandparent_id >= 0) { |
| 2292 | secure_parent_clocks(grandparent_id); |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | void stm32mp1_register_clock_parents_secure(unsigned long clock_id) |
| 2297 | { |
| 2298 | int parent_id; |
| 2299 | |
| 2300 | if (!stm32mp1_rcc_is_secure()) { |
| 2301 | return; |
| 2302 | } |
| 2303 | |
| 2304 | switch (clock_id) { |
| 2305 | case PLL1: |
| 2306 | case PLL2: |
| 2307 | /* PLL1/PLL2 are always secure: nothing to do */ |
| 2308 | break; |
| 2309 | case PLL3: |
| 2310 | stm32mp_register_secure_periph(STM32MP1_SHRES_PLL3); |
| 2311 | break; |
| 2312 | case PLL4: |
| 2313 | ERROR("PLL4 cannot be secured\n"); |
| 2314 | panic(); |
| 2315 | break; |
| 2316 | default: |
| 2317 | /* Others are expected gateable clock */ |
| 2318 | parent_id = stm32mp1_clk_get_parent(clock_id); |
| 2319 | if (parent_id < 0) { |
| 2320 | INFO("No parent found for clock %lu\n", clock_id); |
| 2321 | } else { |
| 2322 | secure_parent_clocks(parent_id); |
| 2323 | } |
| 2324 | break; |
| 2325 | } |
| 2326 | } |
| 2327 | #endif /* STM32MP_SHARED_RESOURCES */ |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2328 | |
Yann Gautier | c7f9e96 | 2019-05-20 14:39:26 +0200 | [diff] [blame] | 2329 | static void sync_earlyboot_clocks_state(void) |
| 2330 | { |
Etienne Carriere | 2a756c2 | 2019-12-08 08:23:35 +0100 | [diff] [blame] | 2331 | unsigned int idx; |
| 2332 | const unsigned long secure_enable[] = { |
| 2333 | AXIDCG, |
| 2334 | BSEC, |
| 2335 | DDRC1, DDRC1LP, |
| 2336 | DDRC2, DDRC2LP, |
| 2337 | DDRCAPB, DDRPHYCAPB, DDRPHYCAPBLP, |
| 2338 | DDRPHYC, DDRPHYCLP, |
Lionel Debieve | cfa88cc | 2019-09-02 18:15:45 +0200 | [diff] [blame] | 2339 | RTCAPB, |
Etienne Carriere | 2a756c2 | 2019-12-08 08:23:35 +0100 | [diff] [blame] | 2340 | TZC1, TZC2, |
| 2341 | TZPC, |
| 2342 | STGEN_K, |
| 2343 | }; |
| 2344 | |
| 2345 | for (idx = 0U; idx < ARRAY_SIZE(secure_enable); idx++) { |
| 2346 | stm32mp_clk_enable(secure_enable[idx]); |
| 2347 | } |
Yann Gautier | c7f9e96 | 2019-05-20 14:39:26 +0200 | [diff] [blame] | 2348 | } |
| 2349 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 2350 | static const struct clk_ops stm32mp_clk_ops = { |
| 2351 | .enable = stm32mp_clk_enable, |
| 2352 | .disable = stm32mp_clk_disable, |
| 2353 | .is_enabled = stm32mp_clk_is_enabled, |
| 2354 | .get_rate = stm32mp_clk_get_rate, |
| 2355 | .get_parent = stm32mp1_clk_get_parent, |
| 2356 | }; |
| 2357 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2358 | int stm32mp1_clk_probe(void) |
| 2359 | { |
Lionel Debieve | 3c0fbfe | 2020-12-15 10:35:59 +0100 | [diff] [blame] | 2360 | #if defined(IMAGE_BL32) |
| 2361 | if (!fdt_get_rcc_secure_state()) { |
| 2362 | mmio_write_32(stm32mp_rcc_base() + RCC_TZCR, 0U); |
| 2363 | } |
| 2364 | #endif |
| 2365 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2366 | stm32mp1_osc_init(); |
| 2367 | |
Yann Gautier | c7f9e96 | 2019-05-20 14:39:26 +0200 | [diff] [blame] | 2368 | sync_earlyboot_clocks_state(); |
| 2369 | |
Yann Gautier | a205a5c | 2021-08-30 15:06:54 +0200 | [diff] [blame] | 2370 | clk_register(&stm32mp_clk_ops); |
| 2371 | |
Yann Gautier | 9aea69e | 2018-07-24 17:13:36 +0200 | [diff] [blame] | 2372 | return 0; |
| 2373 | } |