1 /* 2 * Copyright (C) 2022-2024, STMicroelectronics - All Rights Reserved 3 * 4 * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause 5 */ 6 7 #ifndef CLK_STM32_CORE_H 8 #define CLK_STM32_CORE_H 9 10 struct mux_cfg { 11 uint16_t offset; 12 uint8_t shift; 13 uint8_t width; 14 uint8_t bitrdy; 15 }; 16 17 struct gate_cfg { 18 uint16_t offset; 19 uint8_t bit_idx; 20 uint8_t set_clr; 21 }; 22 23 struct clk_div_table { 24 uint16_t val; 25 uint16_t div; 26 }; 27 28 struct div_cfg { 29 const struct clk_div_table *table; 30 uint16_t offset; 31 uint8_t shift; 32 uint8_t width; 33 uint8_t flags; 34 uint8_t bitrdy; 35 }; 36 37 struct parent_cfg { 38 const uint16_t *id_parents; 39 struct mux_cfg *mux; 40 uint8_t num_parents; 41 }; 42 43 struct stm32_clk_priv; 44 45 struct stm32_clk_ops { 46 unsigned long (*recalc_rate)(struct stm32_clk_priv *priv, int id, unsigned long rate); 47 int (*get_parent)(struct stm32_clk_priv *priv, int id); 48 int (*set_rate)(struct stm32_clk_priv *priv, int id, unsigned long rate, 49 unsigned long prate); 50 int (*enable)(struct stm32_clk_priv *priv, int id); 51 void (*disable)(struct stm32_clk_priv *priv, int id); 52 bool (*is_enabled)(struct stm32_clk_priv *priv, int id); 53 void (*init)(struct stm32_clk_priv *priv, int id); 54 }; 55 56 struct clk_stm32 { 57 uint16_t binding; 58 uint16_t parent; 59 uint8_t ops; 60 uint8_t flags; 61 void *clock_cfg; 62 }; 63 64 struct stm32_clk_priv { 65 uintptr_t base; 66 const uint32_t num; 67 const struct clk_stm32 *clks; 68 const struct parent_cfg *parents; 69 const uint32_t nb_parents; 70 const struct gate_cfg *gates; 71 const uint32_t nb_gates; 72 const struct div_cfg *div; 73 const uint32_t nb_div; 74 struct clk_oscillator_data *osci_data; 75 const uint32_t nb_osci_data; 76 uint8_t *gate_refcounts; 77 void *pdata; 78 const struct stm32_clk_ops **ops_array; 79 }; 80 81 struct stm32_clk_bypass { 82 uint16_t offset; 83 uint8_t bit_byp; 84 uint8_t bit_digbyp; 85 }; 86 87 struct stm32_clk_css { 88 uint16_t offset; 89 uint8_t bit_css; 90 }; 91 92 struct stm32_clk_drive { 93 uint16_t offset; 94 uint8_t drv_shift; 95 uint8_t drv_width; 96 uint8_t drv_default; 97 }; 98 99 struct clk_oscillator_data { 100 const char *name; 101 struct stm32_clk_bypass *bypass; 102 struct stm32_clk_css *css; 103 struct stm32_clk_drive *drive; 104 unsigned long frequency; 105 uint16_t id_clk; 106 uint16_t gate_id; 107 uint16_t gate_rdy_id; 108 109 }; 110 111 struct clk_gate_cfg { 112 uint32_t offset; 113 uint8_t bit_idx; 114 }; 115 116 /* CLOCK FLAGS */ 117 #define CLK_IS_CRITICAL BIT(0) 118 #define CLK_IGNORE_UNUSED BIT(1) 119 #define CLK_SET_RATE_PARENT BIT(2) 120 121 #define CLK_DIVIDER_ONE_BASED BIT(0) 122 #define CLK_DIVIDER_POWER_OF_TWO BIT(1) 123 #define CLK_DIVIDER_ALLOW_ZERO BIT(2) 124 #define CLK_DIVIDER_HIWORD_MASK BIT(3) 125 #define CLK_DIVIDER_ROUND_CLOSEST BIT(4) 126 #define CLK_DIVIDER_READ_ONLY BIT(5) 127 #define CLK_DIVIDER_MAX_AT_ZERO BIT(6) 128 #define CLK_DIVIDER_BIG_ENDIAN BIT(7) 129 130 #define MUX_MAX_PARENTS U(0x8000) 131 #define MUX_PARENT_MASK GENMASK(14, 0) 132 #define MUX_FLAG U(0x8000) 133 #define MUX(mux) ((mux) | MUX_FLAG) 134 135 #define NO_GATE 0 136 #define _NO_ID UINT16_MAX 137 #define CLK_IS_ROOT UINT16_MAX 138 #define MUX_NO_BIT_RDY UINT8_MAX 139 #define DIV_NO_BIT_RDY UINT8_MAX 140 141 #define MASK_WIDTH_SHIFT(_width, _shift) \ 142 GENMASK(((_width) + (_shift) - 1U), (_shift)) 143 144 void clk_stm32_rcc_regs_lock(void); 145 void clk_stm32_rcc_regs_unlock(void); 146 147 int clk_stm32_init(struct stm32_clk_priv *priv, uintptr_t base); 148 void clk_stm32_enable_critical_clocks(void); 149 150 struct stm32_clk_priv *clk_stm32_get_priv(void); 151 152 int clk_get_index(struct stm32_clk_priv *priv, unsigned long binding_id); 153 const struct clk_stm32 *_clk_get(struct stm32_clk_priv *priv, int id); 154 155 void clk_oscillator_set_bypass(struct stm32_clk_priv *priv, int id, bool digbyp, bool bypass); 156 void clk_oscillator_set_drive(struct stm32_clk_priv *priv, int id, uint8_t lsedrv); 157 void clk_oscillator_set_css(struct stm32_clk_priv *priv, int id, bool css); 158 159 int _clk_stm32_gate_wait_ready(struct stm32_clk_priv *priv, uint16_t gate_id, bool ready_on); 160 161 int clk_oscillator_wait_ready(struct stm32_clk_priv *priv, int id, bool ready_on); 162 int clk_oscillator_wait_ready_on(struct stm32_clk_priv *priv, int id); 163 int clk_oscillator_wait_ready_off(struct stm32_clk_priv *priv, int id); 164 165 int clk_stm32_get_counter(unsigned long binding_id); 166 167 void _clk_stm32_gate_disable(struct stm32_clk_priv *priv, uint16_t gate_id); 168 int _clk_stm32_gate_enable(struct stm32_clk_priv *priv, uint16_t gate_id); 169 170 int _clk_stm32_set_parent(struct stm32_clk_priv *priv, int id, int src_id); 171 int _clk_stm32_set_parent_by_index(struct stm32_clk_priv *priv, int clk, int sel); 172 173 int _clk_stm32_get_parent(struct stm32_clk_priv *priv, int id); 174 int _clk_stm32_get_parent_by_index(struct stm32_clk_priv *priv, int clk_id, int idx); 175 int _clk_stm32_get_parent_index(struct stm32_clk_priv *priv, int clk_id); 176 177 unsigned long _clk_stm32_get_rate(struct stm32_clk_priv *priv, int id); 178 unsigned long _clk_stm32_get_parent_rate(struct stm32_clk_priv *priv, int id); 179 180 bool _stm32_clk_is_flags(struct stm32_clk_priv *priv, int id, uint8_t flag); 181 182 int _clk_stm32_enable(struct stm32_clk_priv *priv, int id); 183 void _clk_stm32_disable(struct stm32_clk_priv *priv, int id); 184 185 int clk_stm32_enable_call_ops(struct stm32_clk_priv *priv, uint16_t id); 186 void clk_stm32_disable_call_ops(struct stm32_clk_priv *priv, uint16_t id); 187 188 bool _clk_stm32_is_enabled(struct stm32_clk_priv *priv, int id); 189 190 int _clk_stm32_divider_set_rate(struct stm32_clk_priv *priv, int div_id, 191 unsigned long rate, unsigned long parent_rate); 192 193 int clk_stm32_divider_set_rate(struct stm32_clk_priv *priv, int id, unsigned long rate, 194 unsigned long prate); 195 196 unsigned long _clk_stm32_divider_recalc(struct stm32_clk_priv *priv, 197 int div_id, 198 unsigned long prate); 199 200 unsigned long clk_stm32_divider_recalc(struct stm32_clk_priv *priv, int idx, 201 unsigned long prate); 202 203 int clk_stm32_gate_enable(struct stm32_clk_priv *priv, int idx); 204 void clk_stm32_gate_disable(struct stm32_clk_priv *priv, int idx); 205 206 bool _clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int gate_id); 207 bool clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int idx); 208 209 uint32_t clk_stm32_div_get_value(struct stm32_clk_priv *priv, int div_id); 210 int clk_stm32_set_div(struct stm32_clk_priv *priv, uint32_t div_id, uint32_t value); 211 int clk_mux_set_parent(struct stm32_clk_priv *priv, uint16_t pid, uint8_t sel); 212 int clk_mux_get_parent(struct stm32_clk_priv *priv, uint32_t mux_id); 213 214 int stm32_clk_parse_fdt_by_name(void *fdt, int node, const char *name, uint32_t *tab, uint32_t *nb); 215 216 #ifdef CFG_STM32_CLK_DEBUG 217 void clk_stm32_display_clock_info(void); 218 #endif 219 220 struct clk_stm32_div_cfg { 221 uint8_t id; 222 }; 223 224 #define STM32_DIV(idx, _binding, _parent, _flags, _div_id) \ 225 [(idx)] = (struct clk_stm32){ \ 226 .binding = (_binding),\ 227 .parent = (_parent),\ 228 .flags = (_flags),\ 229 .clock_cfg = &(struct clk_stm32_div_cfg){\ 230 .id = (_div_id),\ 231 },\ 232 .ops = STM32_DIVIDER_OPS,\ 233 } 234 235 struct clk_stm32_gate_cfg { 236 uint8_t id; 237 }; 238 239 #define STM32_GATE(idx, _binding, _parent, _flags, _gate_id) \ 240 [(idx)] = (struct clk_stm32){ \ 241 .binding = (_binding),\ 242 .parent = (_parent),\ 243 .flags = (_flags),\ 244 .clock_cfg = &(struct clk_stm32_gate_cfg){\ 245 .id = (_gate_id),\ 246 },\ 247 .ops = STM32_GATE_OPS,\ 248 } 249 250 struct fixed_factor_cfg { 251 uint8_t mult; 252 uint8_t div; 253 }; 254 255 unsigned long fixed_factor_recalc_rate(struct stm32_clk_priv *priv, 256 int _idx, unsigned long prate); 257 258 #define FIXED_FACTOR(idx, _idx, _parent, _mult, _div) \ 259 [(idx)] = (struct clk_stm32){ \ 260 .binding = (_idx),\ 261 .parent = (_parent),\ 262 .clock_cfg = &(struct fixed_factor_cfg){\ 263 .mult = (_mult),\ 264 .div = (_div),\ 265 },\ 266 .ops = FIXED_FACTOR_OPS,\ 267 } 268 269 #define GATE(idx, _binding, _parent, _flags, _offset, _bit_idx) \ 270 [(idx)] = (struct clk_stm32){ \ 271 .binding = (_binding),\ 272 .parent = (_parent),\ 273 .flags = (_flags),\ 274 .clock_cfg = &(struct clk_gate_cfg){\ 275 .offset = (_offset),\ 276 .bit_idx = (_bit_idx),\ 277 },\ 278 .ops = GATE_OPS,\ 279 } 280 281 #define STM32_MUX(idx, _binding, _mux_id, _flags) \ 282 [(idx)] = (struct clk_stm32){ \ 283 .binding = (_binding),\ 284 .parent = (MUX(_mux_id)),\ 285 .flags = (_flags),\ 286 .clock_cfg = NULL,\ 287 .ops = STM32_MUX_OPS\ 288 } 289 290 struct clk_timer_cfg { 291 uint32_t apbdiv; 292 uint32_t timpre; 293 }; 294 295 #define CK_TIMER(idx, _idx, _parent, _flags, _apbdiv, _timpre) \ 296 [(idx)] = (struct clk_stm32){ \ 297 .binding = (_idx),\ 298 .parent = (_parent),\ 299 .flags = (CLK_SET_RATE_PARENT | (_flags)),\ 300 .clock_cfg = &(struct clk_timer_cfg){\ 301 .apbdiv = (_apbdiv),\ 302 .timpre = (_timpre),\ 303 },\ 304 .ops = STM32_TIMER_OPS,\ 305 } 306 307 struct clk_stm32_fixed_rate_cfg { 308 unsigned long rate; 309 }; 310 311 #define CLK_FIXED_RATE(idx, _binding, _rate) \ 312 [(idx)] = (struct clk_stm32){ \ 313 .binding = (_binding),\ 314 .parent = (CLK_IS_ROOT),\ 315 .clock_cfg = &(struct clk_stm32_fixed_rate_cfg){\ 316 .rate = (_rate),\ 317 },\ 318 .ops = STM32_FIXED_RATE_OPS,\ 319 } 320 321 #define BYPASS(_offset, _bit_byp, _bit_digbyp) &(struct stm32_clk_bypass){\ 322 .offset = (_offset),\ 323 .bit_byp = (_bit_byp),\ 324 .bit_digbyp = (_bit_digbyp),\ 325 } 326 327 #define CSS(_offset, _bit_css) &(struct stm32_clk_css){\ 328 .offset = (_offset),\ 329 .bit_css = (_bit_css),\ 330 } 331 332 #define DRIVE(_offset, _shift, _width, _default) &(struct stm32_clk_drive){\ 333 .offset = (_offset),\ 334 .drv_shift = (_shift),\ 335 .drv_width = (_width),\ 336 .drv_default = (_default),\ 337 } 338 339 #define OSCILLATOR(idx_osc, _id, _name, _gate_id, _gate_rdy_id, _bypass, _css, _drive) \ 340 [(idx_osc)] = (struct clk_oscillator_data){\ 341 .name = (_name),\ 342 .id_clk = (_id),\ 343 .gate_id = (_gate_id),\ 344 .gate_rdy_id = (_gate_rdy_id),\ 345 .bypass = (_bypass),\ 346 .css = (_css),\ 347 .drive = (_drive),\ 348 } 349 350 struct clk_oscillator_data *clk_oscillator_get_data(struct stm32_clk_priv *priv, int id); 351 352 void clk_stm32_osc_init(struct stm32_clk_priv *priv, int id); 353 bool clk_stm32_osc_gate_is_enabled(struct stm32_clk_priv *priv, int id); 354 int clk_stm32_osc_gate_enable(struct stm32_clk_priv *priv, int id); 355 void clk_stm32_osc_gate_disable(struct stm32_clk_priv *priv, int id); 356 357 struct stm32_osc_cfg { 358 uint8_t osc_id; 359 }; 360 361 #define CLK_OSC(idx, _idx, _parent, _osc_id) \ 362 [(idx)] = (struct clk_stm32){ \ 363 .binding = (_idx),\ 364 .parent = (_parent),\ 365 .flags = CLK_IS_CRITICAL,\ 366 .clock_cfg = &(struct stm32_osc_cfg){\ 367 .osc_id = (_osc_id),\ 368 },\ 369 .ops = STM32_OSC_OPS,\ 370 } 371 372 #define CLK_OSC_FIXED(idx, _idx, _parent, _osc_id) \ 373 [(idx)] = (struct clk_stm32){ \ 374 .binding = (_idx),\ 375 .parent = (_parent),\ 376 .flags = CLK_IS_CRITICAL,\ 377 .clock_cfg = &(struct stm32_osc_cfg){\ 378 .osc_id = (_osc_id),\ 379 },\ 380 .ops = STM32_OSC_NOGATE_OPS,\ 381 } 382 383 extern const struct stm32_clk_ops clk_mux_ops; 384 extern const struct stm32_clk_ops clk_stm32_divider_ops; 385 extern const struct stm32_clk_ops clk_stm32_gate_ops; 386 extern const struct stm32_clk_ops clk_fixed_factor_ops; 387 extern const struct stm32_clk_ops clk_gate_ops; 388 extern const struct stm32_clk_ops clk_timer_ops; 389 extern const struct stm32_clk_ops clk_stm32_fixed_rate_ops; 390 extern const struct stm32_clk_ops clk_stm32_osc_ops; 391 extern const struct stm32_clk_ops clk_stm32_osc_nogate_ops; 392 393 enum { 394 NO_OPS, 395 FIXED_FACTOR_OPS, 396 GATE_OPS, 397 STM32_MUX_OPS, 398 STM32_DIVIDER_OPS, 399 STM32_GATE_OPS, 400 STM32_TIMER_OPS, 401 STM32_FIXED_RATE_OPS, 402 STM32_OSC_OPS, 403 STM32_OSC_NOGATE_OPS, 404 405 STM32_LAST_OPS 406 }; 407 408 #endif /* CLK_STM32_CORE_H */ 409