Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 1 | /* |
Gabriel Fernandez | 42b3265 | 2022-07-08 14:42:19 +0200 | [diff] [blame] | 2 | * Copyright (C) 2022-2024, STMicroelectronics - All Rights Reserved |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <errno.h> |
| 9 | |
| 10 | #include "clk-stm32-core.h" |
| 11 | #include <common/debug.h> |
| 12 | #include <common/fdt_wrappers.h> |
| 13 | #include <drivers/clk.h> |
| 14 | #include <drivers/delay_timer.h> |
| 15 | #include <drivers/st/stm32mp_clkfunc.h> |
| 16 | #include <lib/mmio.h> |
| 17 | #include <lib/spinlock.h> |
| 18 | |
| 19 | static struct spinlock reg_lock; |
| 20 | static struct spinlock refcount_lock; |
| 21 | |
| 22 | static struct stm32_clk_priv *stm32_clock_data; |
| 23 | |
| 24 | const struct stm32_clk_ops clk_mux_ops; |
| 25 | |
| 26 | struct stm32_clk_priv *clk_stm32_get_priv(void) |
| 27 | { |
| 28 | return stm32_clock_data; |
| 29 | } |
| 30 | |
Yann Gautier | 0d9413e | 2023-10-25 17:27:13 +0200 | [diff] [blame] | 31 | static void _clk_lock(struct spinlock *lock) |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 32 | { |
| 33 | if (stm32mp_lock_available()) { |
| 34 | /* Assume interrupts are masked */ |
| 35 | spin_lock(lock); |
| 36 | } |
| 37 | } |
| 38 | |
Yann Gautier | 0d9413e | 2023-10-25 17:27:13 +0200 | [diff] [blame] | 39 | static void _clk_unlock(struct spinlock *lock) |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 40 | { |
| 41 | if (stm32mp_lock_available()) { |
| 42 | spin_unlock(lock); |
| 43 | } |
| 44 | } |
| 45 | |
Yann Gautier | 0d9413e | 2023-10-25 17:27:13 +0200 | [diff] [blame] | 46 | void clk_stm32_rcc_regs_lock(void) |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 47 | { |
Yann Gautier | 0d9413e | 2023-10-25 17:27:13 +0200 | [diff] [blame] | 48 | _clk_lock(®_lock); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Yann Gautier | 0d9413e | 2023-10-25 17:27:13 +0200 | [diff] [blame] | 51 | void clk_stm32_rcc_regs_unlock(void) |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 52 | { |
Yann Gautier | 0d9413e | 2023-10-25 17:27:13 +0200 | [diff] [blame] | 53 | _clk_unlock(®_lock); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | #define TIMEOUT_US_1S U(1000000) |
| 57 | #define OSCRDY_TIMEOUT TIMEOUT_US_1S |
| 58 | |
| 59 | struct clk_oscillator_data *clk_oscillator_get_data(struct stm32_clk_priv *priv, int id) |
| 60 | { |
| 61 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 62 | struct stm32_osc_cfg *osc_cfg = clk->clock_cfg; |
| 63 | int osc_id = osc_cfg->osc_id; |
| 64 | |
| 65 | return &priv->osci_data[osc_id]; |
| 66 | } |
| 67 | |
| 68 | void clk_oscillator_set_bypass(struct stm32_clk_priv *priv, int id, bool digbyp, bool bypass) |
| 69 | { |
| 70 | struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); |
| 71 | |
| 72 | struct stm32_clk_bypass *bypass_data = osc_data->bypass; |
| 73 | uintptr_t address; |
| 74 | |
| 75 | if (bypass_data == NULL) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | address = priv->base + bypass_data->offset; |
| 80 | |
| 81 | if (digbyp) { |
| 82 | mmio_setbits_32(address, BIT(bypass_data->bit_digbyp)); |
| 83 | } |
| 84 | |
| 85 | if (bypass || digbyp) { |
| 86 | mmio_setbits_32(address, BIT(bypass_data->bit_byp)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void clk_oscillator_set_css(struct stm32_clk_priv *priv, int id, bool css) |
| 91 | { |
| 92 | struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); |
| 93 | |
| 94 | struct stm32_clk_css *css_data = osc_data->css; |
| 95 | uintptr_t address; |
| 96 | |
| 97 | if (css_data == NULL) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | address = priv->base + css_data->offset; |
| 102 | |
| 103 | if (css) { |
| 104 | mmio_setbits_32(address, BIT(css_data->bit_css)); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void clk_oscillator_set_drive(struct stm32_clk_priv *priv, int id, uint8_t lsedrv) |
| 109 | { |
| 110 | struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); |
| 111 | |
| 112 | struct stm32_clk_drive *drive_data = osc_data->drive; |
| 113 | uintptr_t address; |
| 114 | uint32_t mask; |
| 115 | uint32_t value; |
| 116 | |
| 117 | if (drive_data == NULL) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | address = priv->base + drive_data->offset; |
| 122 | |
| 123 | mask = (BIT(drive_data->drv_width) - 1U) << drive_data->drv_shift; |
| 124 | |
| 125 | /* |
| 126 | * Warning: not recommended to switch directly from "high drive" |
| 127 | * to "medium low drive", and vice-versa. |
| 128 | */ |
| 129 | value = (mmio_read_32(address) & mask) >> drive_data->drv_shift; |
| 130 | |
| 131 | while (value != lsedrv) { |
| 132 | if (value > lsedrv) { |
| 133 | value--; |
| 134 | } else { |
| 135 | value++; |
| 136 | } |
| 137 | |
| 138 | mmio_clrsetbits_32(address, mask, value << drive_data->drv_shift); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | int clk_oscillator_wait_ready(struct stm32_clk_priv *priv, int id, bool ready_on) |
| 143 | { |
| 144 | struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); |
| 145 | |
Yann Gautier | 8d13723 | 2022-06-21 15:12:27 +0200 | [diff] [blame] | 146 | return _clk_stm32_gate_wait_ready(priv, osc_data->gate_rdy_id, ready_on); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int clk_oscillator_wait_ready_on(struct stm32_clk_priv *priv, int id) |
| 150 | { |
| 151 | return clk_oscillator_wait_ready(priv, id, true); |
| 152 | } |
| 153 | |
| 154 | int clk_oscillator_wait_ready_off(struct stm32_clk_priv *priv, int id) |
| 155 | { |
| 156 | return clk_oscillator_wait_ready(priv, id, false); |
| 157 | } |
| 158 | |
| 159 | static int clk_gate_enable(struct stm32_clk_priv *priv, int id) |
| 160 | { |
| 161 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 162 | struct clk_gate_cfg *cfg = clk->clock_cfg; |
| 163 | |
| 164 | mmio_setbits_32(priv->base + cfg->offset, BIT(cfg->bit_idx)); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static void clk_gate_disable(struct stm32_clk_priv *priv, int id) |
| 170 | { |
| 171 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 172 | struct clk_gate_cfg *cfg = clk->clock_cfg; |
| 173 | |
| 174 | mmio_clrbits_32(priv->base + cfg->offset, BIT(cfg->bit_idx)); |
| 175 | } |
| 176 | |
| 177 | static bool clk_gate_is_enabled(struct stm32_clk_priv *priv, int id) |
| 178 | { |
| 179 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 180 | struct clk_gate_cfg *cfg = clk->clock_cfg; |
| 181 | |
| 182 | return ((mmio_read_32(priv->base + cfg->offset) & BIT(cfg->bit_idx)) != 0U); |
| 183 | } |
| 184 | |
| 185 | const struct stm32_clk_ops clk_gate_ops = { |
| 186 | .enable = clk_gate_enable, |
| 187 | .disable = clk_gate_disable, |
| 188 | .is_enabled = clk_gate_is_enabled, |
| 189 | }; |
| 190 | |
| 191 | void _clk_stm32_gate_disable(struct stm32_clk_priv *priv, uint16_t gate_id) |
| 192 | { |
| 193 | const struct gate_cfg *gate = &priv->gates[gate_id]; |
| 194 | uintptr_t addr = priv->base + gate->offset; |
| 195 | |
| 196 | if (gate->set_clr != 0U) { |
| 197 | mmio_write_32(addr + RCC_MP_ENCLRR_OFFSET, BIT(gate->bit_idx)); |
| 198 | } else { |
| 199 | mmio_clrbits_32(addr, BIT(gate->bit_idx)); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | int _clk_stm32_gate_enable(struct stm32_clk_priv *priv, uint16_t gate_id) |
| 204 | { |
| 205 | const struct gate_cfg *gate = &priv->gates[gate_id]; |
| 206 | uintptr_t addr = priv->base + gate->offset; |
| 207 | |
| 208 | if (gate->set_clr != 0U) { |
| 209 | mmio_write_32(addr, BIT(gate->bit_idx)); |
| 210 | |
| 211 | } else { |
| 212 | mmio_setbits_32(addr, BIT(gate->bit_idx)); |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 218 | const struct clk_stm32 *_clk_get(struct stm32_clk_priv *priv, int id) |
| 219 | { |
| 220 | if ((unsigned int)id < priv->num) { |
| 221 | return &priv->clks[id]; |
| 222 | } |
| 223 | |
| 224 | return NULL; |
| 225 | } |
| 226 | |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 227 | static const struct stm32_clk_ops *_clk_get_ops(struct stm32_clk_priv *priv, int id) |
| 228 | { |
| 229 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 230 | |
| 231 | assert(clk->ops != NO_OPS); |
| 232 | |
| 233 | return priv->ops_array[clk->ops]; |
| 234 | } |
| 235 | |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 236 | #define clk_div_mask(_width) GENMASK(((_width) - 1U), 0U) |
| 237 | |
| 238 | static unsigned int _get_table_div(const struct clk_div_table *table, |
| 239 | unsigned int val) |
| 240 | { |
| 241 | const struct clk_div_table *clkt; |
| 242 | |
| 243 | for (clkt = table; clkt->div; clkt++) { |
| 244 | if (clkt->val == val) { |
| 245 | return clkt->div; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static unsigned int _get_div(const struct clk_div_table *table, |
| 253 | unsigned int val, unsigned long flags, |
| 254 | uint8_t width) |
| 255 | { |
| 256 | if ((flags & CLK_DIVIDER_ONE_BASED) != 0UL) { |
| 257 | return val; |
| 258 | } |
| 259 | |
| 260 | if ((flags & CLK_DIVIDER_POWER_OF_TWO) != 0UL) { |
| 261 | return BIT(val); |
| 262 | } |
| 263 | |
| 264 | if ((flags & CLK_DIVIDER_MAX_AT_ZERO) != 0UL) { |
| 265 | return (val != 0U) ? val : BIT(width); |
| 266 | } |
| 267 | |
| 268 | if (table != NULL) { |
| 269 | return _get_table_div(table, val); |
| 270 | } |
| 271 | |
| 272 | return val + 1U; |
| 273 | } |
| 274 | |
| 275 | #define TIMEOUT_US_200MS U(200000) |
| 276 | #define CLKSRC_TIMEOUT TIMEOUT_US_200MS |
| 277 | |
| 278 | int clk_mux_set_parent(struct stm32_clk_priv *priv, uint16_t pid, uint8_t sel) |
| 279 | { |
| 280 | const struct parent_cfg *parents = &priv->parents[pid & MUX_PARENT_MASK]; |
| 281 | const struct mux_cfg *mux = parents->mux; |
| 282 | uintptr_t address = priv->base + mux->offset; |
| 283 | uint32_t mask; |
| 284 | uint64_t timeout; |
| 285 | |
| 286 | mask = MASK_WIDTH_SHIFT(mux->width, mux->shift); |
| 287 | |
| 288 | mmio_clrsetbits_32(address, mask, (sel << mux->shift) & mask); |
| 289 | |
| 290 | if (mux->bitrdy == MUX_NO_BIT_RDY) { |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | timeout = timeout_init_us(CLKSRC_TIMEOUT); |
| 295 | |
| 296 | mask = BIT(mux->bitrdy); |
| 297 | |
| 298 | while ((mmio_read_32(address) & mask) == 0U) { |
| 299 | if (timeout_elapsed(timeout)) { |
| 300 | return -ETIMEDOUT; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | int _clk_stm32_set_parent(struct stm32_clk_priv *priv, int clk, int clkp) |
| 308 | { |
| 309 | const struct parent_cfg *parents; |
| 310 | uint16_t pid; |
| 311 | uint8_t sel; |
| 312 | int old_parent; |
| 313 | |
| 314 | pid = priv->clks[clk].parent; |
| 315 | |
| 316 | if ((pid == CLK_IS_ROOT) || (pid < MUX_MAX_PARENTS)) { |
| 317 | return -EINVAL; |
| 318 | } |
| 319 | |
| 320 | old_parent = _clk_stm32_get_parent(priv, clk); |
Yann Gautier | 972221c | 2022-03-29 09:51:21 +0200 | [diff] [blame] | 321 | if (old_parent < 0) { |
| 322 | return old_parent; |
| 323 | } |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 324 | if (old_parent == clkp) { |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | parents = &priv->parents[pid & MUX_PARENT_MASK]; |
| 329 | |
| 330 | for (sel = 0; sel < parents->num_parents; sel++) { |
| 331 | if (parents->id_parents[sel] == (uint16_t)clkp) { |
| 332 | bool clk_was_enabled = _clk_stm32_is_enabled(priv, clk); |
| 333 | int err = 0; |
| 334 | |
| 335 | /* Enable the parents (for glitch free mux) */ |
| 336 | _clk_stm32_enable(priv, clkp); |
| 337 | _clk_stm32_enable(priv, old_parent); |
| 338 | |
| 339 | err = clk_mux_set_parent(priv, pid, sel); |
| 340 | |
| 341 | _clk_stm32_disable(priv, old_parent); |
| 342 | |
| 343 | if (clk_was_enabled) { |
| 344 | _clk_stm32_disable(priv, old_parent); |
| 345 | } else { |
| 346 | _clk_stm32_disable(priv, clkp); |
| 347 | } |
| 348 | |
| 349 | return err; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | return -EINVAL; |
| 354 | } |
| 355 | |
| 356 | int clk_mux_get_parent(struct stm32_clk_priv *priv, uint32_t mux_id) |
| 357 | { |
| 358 | const struct parent_cfg *parent; |
| 359 | const struct mux_cfg *mux; |
| 360 | uint32_t mask; |
| 361 | |
| 362 | if (mux_id >= priv->nb_parents) { |
| 363 | panic(); |
| 364 | } |
| 365 | |
| 366 | parent = &priv->parents[mux_id]; |
| 367 | mux = parent->mux; |
| 368 | |
| 369 | mask = MASK_WIDTH_SHIFT(mux->width, mux->shift); |
| 370 | |
| 371 | return (mmio_read_32(priv->base + mux->offset) & mask) >> mux->shift; |
| 372 | } |
| 373 | |
| 374 | int _clk_stm32_set_parent_by_index(struct stm32_clk_priv *priv, int clk, int sel) |
| 375 | { |
| 376 | uint16_t pid; |
| 377 | |
| 378 | pid = priv->clks[clk].parent; |
| 379 | |
| 380 | if ((pid == CLK_IS_ROOT) || (pid < MUX_MAX_PARENTS)) { |
| 381 | return -EINVAL; |
| 382 | } |
| 383 | |
| 384 | return clk_mux_set_parent(priv, pid, sel); |
| 385 | } |
| 386 | |
| 387 | int _clk_stm32_get_parent(struct stm32_clk_priv *priv, int clk_id) |
| 388 | { |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 389 | const struct stm32_clk_ops *ops = _clk_get_ops(priv, clk_id); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 390 | const struct parent_cfg *parent; |
| 391 | uint16_t mux_id; |
| 392 | int sel; |
| 393 | |
| 394 | mux_id = priv->clks[clk_id].parent; |
| 395 | if (mux_id == CLK_IS_ROOT) { |
| 396 | return CLK_IS_ROOT; |
| 397 | } |
| 398 | |
| 399 | if (mux_id < MUX_MAX_PARENTS) { |
| 400 | return mux_id & MUX_PARENT_MASK; |
| 401 | } |
| 402 | |
| 403 | mux_id &= MUX_PARENT_MASK; |
| 404 | parent = &priv->parents[mux_id]; |
| 405 | |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 406 | if (ops->get_parent != NULL) { |
| 407 | sel = ops->get_parent(priv, clk_id); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 408 | } else { |
| 409 | sel = clk_mux_get_parent(priv, mux_id); |
| 410 | } |
| 411 | |
Yann Gautier | 972221c | 2022-03-29 09:51:21 +0200 | [diff] [blame] | 412 | if ((sel >= 0) && (sel < parent->num_parents)) { |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 413 | return parent->id_parents[sel]; |
| 414 | } |
| 415 | |
| 416 | return -EINVAL; |
| 417 | } |
| 418 | |
| 419 | int _clk_stm32_get_parent_index(struct stm32_clk_priv *priv, int clk_id) |
| 420 | { |
| 421 | uint16_t mux_id; |
| 422 | |
| 423 | mux_id = priv->clks[clk_id].parent; |
| 424 | if (mux_id == CLK_IS_ROOT) { |
| 425 | return CLK_IS_ROOT; |
| 426 | } |
| 427 | |
| 428 | if (mux_id < MUX_MAX_PARENTS) { |
| 429 | return mux_id & MUX_PARENT_MASK; |
| 430 | } |
| 431 | |
| 432 | mux_id &= MUX_PARENT_MASK; |
| 433 | |
| 434 | return clk_mux_get_parent(priv, mux_id); |
| 435 | } |
| 436 | |
| 437 | int _clk_stm32_get_parent_by_index(struct stm32_clk_priv *priv, int clk_id, int idx) |
| 438 | { |
| 439 | const struct parent_cfg *parent; |
| 440 | uint16_t mux_id; |
| 441 | |
| 442 | mux_id = priv->clks[clk_id].parent; |
| 443 | if (mux_id == CLK_IS_ROOT) { |
| 444 | return CLK_IS_ROOT; |
| 445 | } |
| 446 | |
| 447 | if (mux_id < MUX_MAX_PARENTS) { |
| 448 | return mux_id & MUX_PARENT_MASK; |
| 449 | } |
| 450 | |
| 451 | mux_id &= MUX_PARENT_MASK; |
| 452 | parent = &priv->parents[mux_id]; |
| 453 | |
| 454 | if (idx < parent->num_parents) { |
| 455 | return parent->id_parents[idx]; |
| 456 | } |
| 457 | |
| 458 | return -EINVAL; |
| 459 | } |
| 460 | |
| 461 | int clk_get_index(struct stm32_clk_priv *priv, unsigned long binding_id) |
| 462 | { |
| 463 | unsigned int i; |
| 464 | |
| 465 | for (i = 0U; i < priv->num; i++) { |
| 466 | if (binding_id == priv->clks[i].binding) { |
| 467 | return (int)i; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | |
| 474 | unsigned long _clk_stm32_get_rate(struct stm32_clk_priv *priv, int id) |
| 475 | { |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 476 | const struct stm32_clk_ops *ops = _clk_get_ops(priv, id); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 477 | int parent; |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 478 | |
| 479 | if ((unsigned int)id >= priv->num) { |
Yann Gautier | 21d117a | 2022-11-25 10:42:52 +0100 | [diff] [blame] | 480 | return 0UL; |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | parent = _clk_stm32_get_parent(priv, id); |
Yann Gautier | 972221c | 2022-03-29 09:51:21 +0200 | [diff] [blame] | 484 | if (parent < 0) { |
| 485 | return 0UL; |
| 486 | } |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 487 | |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 488 | if (ops->recalc_rate != NULL) { |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 489 | unsigned long prate = 0UL; |
| 490 | |
| 491 | if (parent != CLK_IS_ROOT) { |
| 492 | prate = _clk_stm32_get_rate(priv, parent); |
| 493 | } |
| 494 | |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 495 | return ops->recalc_rate(priv, id, prate); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Yann Gautier | 21d117a | 2022-11-25 10:42:52 +0100 | [diff] [blame] | 498 | if (parent == CLK_IS_ROOT) { |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 499 | panic(); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 500 | } |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 501 | |
Yann Gautier | 21d117a | 2022-11-25 10:42:52 +0100 | [diff] [blame] | 502 | return _clk_stm32_get_rate(priv, parent); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | unsigned long _clk_stm32_get_parent_rate(struct stm32_clk_priv *priv, int id) |
| 506 | { |
| 507 | int parent_id = _clk_stm32_get_parent(priv, id); |
| 508 | |
Yann Gautier | 972221c | 2022-03-29 09:51:21 +0200 | [diff] [blame] | 509 | if (parent_id < 0) { |
| 510 | return 0UL; |
| 511 | } |
| 512 | |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 513 | return _clk_stm32_get_rate(priv, parent_id); |
| 514 | } |
| 515 | |
| 516 | static uint8_t _stm32_clk_get_flags(struct stm32_clk_priv *priv, int id) |
| 517 | { |
| 518 | return priv->clks[id].flags; |
| 519 | } |
| 520 | |
| 521 | bool _stm32_clk_is_flags(struct stm32_clk_priv *priv, int id, uint8_t flag) |
| 522 | { |
Yann Gautier | 51dad68 | 2022-11-28 14:56:58 +0100 | [diff] [blame] | 523 | if ((_stm32_clk_get_flags(priv, id) & flag) != 0U) { |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 524 | return true; |
| 525 | } |
| 526 | |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | int clk_stm32_enable_call_ops(struct stm32_clk_priv *priv, uint16_t id) |
| 531 | { |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 532 | const struct stm32_clk_ops *ops = _clk_get_ops(priv, id); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 533 | |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 534 | if (ops->enable != NULL) { |
| 535 | ops->enable(priv, id); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static int _clk_stm32_enable_core(struct stm32_clk_priv *priv, int id) |
| 542 | { |
| 543 | int parent; |
| 544 | int ret = 0; |
| 545 | |
| 546 | if (priv->gate_refcounts[id] == 0U) { |
| 547 | parent = _clk_stm32_get_parent(priv, id); |
Yann Gautier | 972221c | 2022-03-29 09:51:21 +0200 | [diff] [blame] | 548 | if (parent < 0) { |
| 549 | return parent; |
| 550 | } |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 551 | if (parent != CLK_IS_ROOT) { |
| 552 | ret = _clk_stm32_enable_core(priv, parent); |
Yann Gautier | 51dad68 | 2022-11-28 14:56:58 +0100 | [diff] [blame] | 553 | if (ret != 0) { |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 554 | return ret; |
| 555 | } |
| 556 | } |
| 557 | clk_stm32_enable_call_ops(priv, id); |
| 558 | } |
| 559 | |
| 560 | priv->gate_refcounts[id]++; |
| 561 | |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 562 | if (priv->gate_refcounts[id] == UINT8_MAX) { |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 563 | ERROR("%s: %d max enable count !", __func__, id); |
| 564 | panic(); |
| 565 | } |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | int _clk_stm32_enable(struct stm32_clk_priv *priv, int id) |
| 571 | { |
| 572 | int ret; |
| 573 | |
Yann Gautier | 0d9413e | 2023-10-25 17:27:13 +0200 | [diff] [blame] | 574 | _clk_lock(&refcount_lock); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 575 | ret = _clk_stm32_enable_core(priv, id); |
Yann Gautier | 0d9413e | 2023-10-25 17:27:13 +0200 | [diff] [blame] | 576 | _clk_unlock(&refcount_lock); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 577 | |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | void clk_stm32_disable_call_ops(struct stm32_clk_priv *priv, uint16_t id) |
| 582 | { |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 583 | const struct stm32_clk_ops *ops = _clk_get_ops(priv, id); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 584 | |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 585 | if (ops->disable != NULL) { |
| 586 | ops->disable(priv, id); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | static void _clk_stm32_disable_core(struct stm32_clk_priv *priv, int id) |
| 591 | { |
| 592 | int parent; |
| 593 | |
| 594 | if ((priv->gate_refcounts[id] == 1U) && _stm32_clk_is_flags(priv, id, CLK_IS_CRITICAL)) { |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | if (priv->gate_refcounts[id] == 0U) { |
| 599 | /* case of clock ignore unused */ |
| 600 | if (_clk_stm32_is_enabled(priv, id)) { |
| 601 | clk_stm32_disable_call_ops(priv, id); |
| 602 | return; |
| 603 | } |
| 604 | VERBOSE("%s: %d already disabled !\n\n", __func__, id); |
| 605 | return; |
| 606 | } |
| 607 | |
| 608 | if (--priv->gate_refcounts[id] > 0U) { |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | clk_stm32_disable_call_ops(priv, id); |
| 613 | |
| 614 | parent = _clk_stm32_get_parent(priv, id); |
Yann Gautier | 972221c | 2022-03-29 09:51:21 +0200 | [diff] [blame] | 615 | if ((parent >= 0) && (parent != CLK_IS_ROOT)) { |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 616 | _clk_stm32_disable_core(priv, parent); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | void _clk_stm32_disable(struct stm32_clk_priv *priv, int id) |
| 621 | { |
Yann Gautier | 0d9413e | 2023-10-25 17:27:13 +0200 | [diff] [blame] | 622 | _clk_lock(&refcount_lock); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 623 | |
| 624 | _clk_stm32_disable_core(priv, id); |
| 625 | |
Yann Gautier | 0d9413e | 2023-10-25 17:27:13 +0200 | [diff] [blame] | 626 | _clk_unlock(&refcount_lock); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | bool _clk_stm32_is_enabled(struct stm32_clk_priv *priv, int id) |
| 630 | { |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 631 | const struct stm32_clk_ops *ops = _clk_get_ops(priv, id); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 632 | |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 633 | if (ops->is_enabled != NULL) { |
| 634 | return ops->is_enabled(priv, id); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | return priv->gate_refcounts[id]; |
| 638 | } |
| 639 | |
| 640 | static int clk_stm32_enable(unsigned long binding_id) |
| 641 | { |
| 642 | struct stm32_clk_priv *priv = clk_stm32_get_priv(); |
| 643 | int id; |
| 644 | |
| 645 | id = clk_get_index(priv, binding_id); |
| 646 | if (id == -EINVAL) { |
| 647 | return id; |
| 648 | } |
| 649 | |
| 650 | return _clk_stm32_enable(priv, id); |
| 651 | } |
| 652 | |
| 653 | static void clk_stm32_disable(unsigned long binding_id) |
| 654 | { |
| 655 | struct stm32_clk_priv *priv = clk_stm32_get_priv(); |
| 656 | int id; |
| 657 | |
| 658 | id = clk_get_index(priv, binding_id); |
| 659 | if (id != -EINVAL) { |
| 660 | _clk_stm32_disable(priv, id); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | static bool clk_stm32_is_enabled(unsigned long binding_id) |
| 665 | { |
| 666 | struct stm32_clk_priv *priv = clk_stm32_get_priv(); |
| 667 | int id; |
| 668 | |
| 669 | id = clk_get_index(priv, binding_id); |
| 670 | if (id == -EINVAL) { |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | return _clk_stm32_is_enabled(priv, id); |
| 675 | } |
| 676 | |
| 677 | static unsigned long clk_stm32_get_rate(unsigned long binding_id) |
| 678 | { |
| 679 | struct stm32_clk_priv *priv = clk_stm32_get_priv(); |
| 680 | int id; |
| 681 | |
| 682 | id = clk_get_index(priv, binding_id); |
| 683 | if (id == -EINVAL) { |
| 684 | return 0UL; |
| 685 | } |
| 686 | |
| 687 | return _clk_stm32_get_rate(priv, id); |
| 688 | } |
| 689 | |
| 690 | static int clk_stm32_get_parent(unsigned long binding_id) |
| 691 | { |
| 692 | struct stm32_clk_priv *priv = clk_stm32_get_priv(); |
| 693 | int id; |
| 694 | |
| 695 | id = clk_get_index(priv, binding_id); |
| 696 | if (id == -EINVAL) { |
| 697 | return id; |
| 698 | } |
| 699 | |
| 700 | return _clk_stm32_get_parent(priv, id); |
| 701 | } |
| 702 | |
| 703 | static const struct clk_ops stm32mp_clk_ops = { |
| 704 | .enable = clk_stm32_enable, |
| 705 | .disable = clk_stm32_disable, |
| 706 | .is_enabled = clk_stm32_is_enabled, |
| 707 | .get_rate = clk_stm32_get_rate, |
| 708 | .get_parent = clk_stm32_get_parent, |
| 709 | }; |
| 710 | |
| 711 | void clk_stm32_enable_critical_clocks(void) |
| 712 | { |
| 713 | struct stm32_clk_priv *priv = clk_stm32_get_priv(); |
| 714 | unsigned int i; |
| 715 | |
| 716 | for (i = 0U; i < priv->num; i++) { |
| 717 | if (_stm32_clk_is_flags(priv, i, CLK_IS_CRITICAL)) { |
| 718 | _clk_stm32_enable(priv, i); |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | static void stm32_clk_register(void) |
| 724 | { |
| 725 | clk_register(&stm32mp_clk_ops); |
| 726 | } |
| 727 | |
| 728 | uint32_t clk_stm32_div_get_value(struct stm32_clk_priv *priv, int div_id) |
| 729 | { |
| 730 | const struct div_cfg *divider = &priv->div[div_id]; |
| 731 | uint32_t val = 0; |
| 732 | |
| 733 | val = mmio_read_32(priv->base + divider->offset) >> divider->shift; |
| 734 | val &= clk_div_mask(divider->width); |
| 735 | |
| 736 | return val; |
| 737 | } |
| 738 | |
| 739 | unsigned long _clk_stm32_divider_recalc(struct stm32_clk_priv *priv, |
| 740 | int div_id, |
| 741 | unsigned long prate) |
| 742 | { |
| 743 | const struct div_cfg *divider = &priv->div[div_id]; |
| 744 | uint32_t val = clk_stm32_div_get_value(priv, div_id); |
| 745 | unsigned int div = 0U; |
| 746 | |
| 747 | div = _get_div(divider->table, val, divider->flags, divider->width); |
| 748 | if (div == 0U) { |
| 749 | return prate; |
| 750 | } |
| 751 | |
| 752 | return div_round_up((uint64_t)prate, div); |
| 753 | } |
| 754 | |
| 755 | unsigned long clk_stm32_divider_recalc(struct stm32_clk_priv *priv, int id, |
| 756 | unsigned long prate) |
| 757 | { |
| 758 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 759 | struct clk_stm32_div_cfg *div_cfg = clk->clock_cfg; |
| 760 | |
| 761 | return _clk_stm32_divider_recalc(priv, div_cfg->id, prate); |
| 762 | } |
| 763 | |
| 764 | const struct stm32_clk_ops clk_stm32_divider_ops = { |
| 765 | .recalc_rate = clk_stm32_divider_recalc, |
| 766 | }; |
| 767 | |
| 768 | int clk_stm32_set_div(struct stm32_clk_priv *priv, uint32_t div_id, uint32_t value) |
| 769 | { |
| 770 | const struct div_cfg *divider; |
| 771 | uintptr_t address; |
| 772 | uint64_t timeout; |
| 773 | uint32_t mask; |
| 774 | |
| 775 | if (div_id >= priv->nb_div) { |
| 776 | panic(); |
| 777 | } |
| 778 | |
| 779 | divider = &priv->div[div_id]; |
| 780 | address = priv->base + divider->offset; |
| 781 | |
| 782 | mask = MASK_WIDTH_SHIFT(divider->width, divider->shift); |
| 783 | mmio_clrsetbits_32(address, mask, (value << divider->shift) & mask); |
| 784 | |
| 785 | if (divider->bitrdy == DIV_NO_BIT_RDY) { |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | timeout = timeout_init_us(CLKSRC_TIMEOUT); |
| 790 | mask = BIT(divider->bitrdy); |
| 791 | |
| 792 | while ((mmio_read_32(address) & mask) == 0U) { |
| 793 | if (timeout_elapsed(timeout)) { |
| 794 | return -ETIMEDOUT; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | int _clk_stm32_gate_wait_ready(struct stm32_clk_priv *priv, uint16_t gate_id, |
| 802 | bool ready_on) |
| 803 | { |
| 804 | const struct gate_cfg *gate = &priv->gates[gate_id]; |
| 805 | uintptr_t address = priv->base + gate->offset; |
| 806 | uint32_t mask_rdy = BIT(gate->bit_idx); |
| 807 | uint64_t timeout; |
| 808 | uint32_t mask_test; |
| 809 | |
| 810 | if (ready_on) { |
| 811 | mask_test = BIT(gate->bit_idx); |
| 812 | } else { |
| 813 | mask_test = 0U; |
| 814 | } |
| 815 | |
| 816 | timeout = timeout_init_us(OSCRDY_TIMEOUT); |
| 817 | |
| 818 | while ((mmio_read_32(address) & mask_rdy) != mask_test) { |
| 819 | if (timeout_elapsed(timeout)) { |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | |
Yann Gautier | bd513f5 | 2022-06-21 14:34:13 +0200 | [diff] [blame] | 824 | if ((mmio_read_32(address) & mask_rdy) != mask_test) { |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 825 | return -ETIMEDOUT; |
Yann Gautier | bd513f5 | 2022-06-21 14:34:13 +0200 | [diff] [blame] | 826 | } |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | int clk_stm32_gate_enable(struct stm32_clk_priv *priv, int id) |
| 832 | { |
| 833 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 834 | struct clk_stm32_gate_cfg *cfg = clk->clock_cfg; |
| 835 | const struct gate_cfg *gate = &priv->gates[cfg->id]; |
| 836 | uintptr_t addr = priv->base + gate->offset; |
| 837 | |
| 838 | if (gate->set_clr != 0U) { |
| 839 | mmio_write_32(addr, BIT(gate->bit_idx)); |
| 840 | |
| 841 | } else { |
| 842 | mmio_setbits_32(addr, BIT(gate->bit_idx)); |
| 843 | } |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | void clk_stm32_gate_disable(struct stm32_clk_priv *priv, int id) |
| 849 | { |
| 850 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 851 | struct clk_stm32_gate_cfg *cfg = clk->clock_cfg; |
| 852 | const struct gate_cfg *gate = &priv->gates[cfg->id]; |
| 853 | uintptr_t addr = priv->base + gate->offset; |
| 854 | |
| 855 | if (gate->set_clr != 0U) { |
| 856 | mmio_write_32(addr + RCC_MP_ENCLRR_OFFSET, BIT(gate->bit_idx)); |
| 857 | } else { |
| 858 | mmio_clrbits_32(addr, BIT(gate->bit_idx)); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | bool _clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int gate_id) |
| 863 | { |
| 864 | const struct gate_cfg *gate; |
| 865 | uint32_t addr; |
| 866 | |
| 867 | gate = &priv->gates[gate_id]; |
| 868 | addr = priv->base + gate->offset; |
| 869 | |
| 870 | return ((mmio_read_32(addr) & BIT(gate->bit_idx)) != 0U); |
| 871 | } |
| 872 | |
| 873 | bool clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int id) |
| 874 | { |
| 875 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 876 | struct clk_stm32_gate_cfg *cfg = clk->clock_cfg; |
| 877 | |
| 878 | return _clk_stm32_gate_is_enabled(priv, cfg->id); |
| 879 | } |
| 880 | |
| 881 | const struct stm32_clk_ops clk_stm32_gate_ops = { |
| 882 | .enable = clk_stm32_gate_enable, |
| 883 | .disable = clk_stm32_gate_disable, |
| 884 | .is_enabled = clk_stm32_gate_is_enabled, |
| 885 | }; |
| 886 | |
| 887 | const struct stm32_clk_ops clk_fixed_factor_ops = { |
| 888 | .recalc_rate = fixed_factor_recalc_rate, |
| 889 | }; |
| 890 | |
| 891 | unsigned long fixed_factor_recalc_rate(struct stm32_clk_priv *priv, |
| 892 | int id, unsigned long prate) |
| 893 | { |
| 894 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 895 | const struct fixed_factor_cfg *cfg = clk->clock_cfg; |
| 896 | unsigned long long rate; |
| 897 | |
| 898 | rate = (unsigned long long)prate * cfg->mult; |
| 899 | |
| 900 | if (cfg->div == 0U) { |
| 901 | ERROR("division by zero\n"); |
| 902 | panic(); |
| 903 | } |
| 904 | |
| 905 | return (unsigned long)(rate / cfg->div); |
| 906 | }; |
| 907 | |
| 908 | #define APB_DIV_MASK GENMASK(2, 0) |
| 909 | #define TIM_PRE_MASK BIT(0) |
| 910 | |
| 911 | static unsigned long timer_recalc_rate(struct stm32_clk_priv *priv, |
| 912 | int id, unsigned long prate) |
| 913 | { |
| 914 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 915 | const struct clk_timer_cfg *cfg = clk->clock_cfg; |
| 916 | uint32_t prescaler, timpre; |
| 917 | uintptr_t rcc_base = priv->base; |
| 918 | |
| 919 | prescaler = mmio_read_32(rcc_base + cfg->apbdiv) & |
| 920 | APB_DIV_MASK; |
| 921 | |
| 922 | timpre = mmio_read_32(rcc_base + cfg->timpre) & |
| 923 | TIM_PRE_MASK; |
| 924 | |
| 925 | if (prescaler == 0U) { |
| 926 | return prate; |
| 927 | } |
| 928 | |
| 929 | return prate * (timpre + 1U) * 2U; |
| 930 | }; |
| 931 | |
| 932 | const struct stm32_clk_ops clk_timer_ops = { |
| 933 | .recalc_rate = timer_recalc_rate, |
| 934 | }; |
| 935 | |
| 936 | static unsigned long clk_fixed_rate_recalc(struct stm32_clk_priv *priv, int id, |
| 937 | unsigned long prate) |
| 938 | { |
| 939 | const struct clk_stm32 *clk = _clk_get(priv, id); |
| 940 | struct clk_stm32_fixed_rate_cfg *cfg = clk->clock_cfg; |
| 941 | |
| 942 | return cfg->rate; |
| 943 | } |
| 944 | |
| 945 | const struct stm32_clk_ops clk_stm32_fixed_rate_ops = { |
| 946 | .recalc_rate = clk_fixed_rate_recalc, |
| 947 | }; |
| 948 | |
| 949 | static unsigned long clk_stm32_osc_recalc_rate(struct stm32_clk_priv *priv, |
| 950 | int id, unsigned long prate) |
| 951 | { |
| 952 | struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); |
| 953 | |
| 954 | return osc_data->frequency; |
| 955 | }; |
| 956 | |
| 957 | bool clk_stm32_osc_gate_is_enabled(struct stm32_clk_priv *priv, int id) |
| 958 | { |
| 959 | struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); |
| 960 | |
| 961 | return _clk_stm32_gate_is_enabled(priv, osc_data->gate_id); |
| 962 | |
| 963 | } |
| 964 | |
| 965 | int clk_stm32_osc_gate_enable(struct stm32_clk_priv *priv, int id) |
| 966 | { |
| 967 | struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); |
| 968 | |
Gabriel Fernandez | 42b3265 | 2022-07-08 14:42:19 +0200 | [diff] [blame] | 969 | if (osc_data->frequency == 0UL) { |
| 970 | return 0; |
| 971 | } |
| 972 | |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 973 | _clk_stm32_gate_enable(priv, osc_data->gate_id); |
| 974 | |
| 975 | if (_clk_stm32_gate_wait_ready(priv, osc_data->gate_rdy_id, true) != 0U) { |
| 976 | ERROR("%s: %s (%d)\n", __func__, osc_data->name, __LINE__); |
| 977 | panic(); |
| 978 | } |
| 979 | |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | void clk_stm32_osc_gate_disable(struct stm32_clk_priv *priv, int id) |
| 984 | { |
| 985 | struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); |
| 986 | |
Gabriel Fernandez | 42b3265 | 2022-07-08 14:42:19 +0200 | [diff] [blame] | 987 | if (osc_data->frequency == 0UL) { |
| 988 | return; |
| 989 | } |
| 990 | |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 991 | _clk_stm32_gate_disable(priv, osc_data->gate_id); |
| 992 | |
| 993 | if (_clk_stm32_gate_wait_ready(priv, osc_data->gate_rdy_id, false) != 0U) { |
| 994 | ERROR("%s: %s (%d)\n", __func__, osc_data->name, __LINE__); |
| 995 | panic(); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | static unsigned long clk_stm32_get_dt_oscillator_frequency(const char *name) |
| 1000 | { |
| 1001 | void *fdt = NULL; |
| 1002 | int node = 0; |
| 1003 | int subnode = 0; |
| 1004 | |
| 1005 | if (fdt_get_address(&fdt) == 0) { |
| 1006 | panic(); |
| 1007 | } |
| 1008 | |
| 1009 | node = fdt_path_offset(fdt, "/clocks"); |
| 1010 | if (node < 0) { |
| 1011 | return 0UL; |
| 1012 | } |
| 1013 | |
| 1014 | fdt_for_each_subnode(subnode, fdt, node) { |
| 1015 | const char *cchar = NULL; |
| 1016 | const fdt32_t *cuint = NULL; |
| 1017 | int ret = 0; |
| 1018 | |
| 1019 | cchar = fdt_get_name(fdt, subnode, &ret); |
| 1020 | if (cchar == NULL) { |
| 1021 | continue; |
| 1022 | } |
| 1023 | |
| 1024 | if (strncmp(cchar, name, (size_t)ret) || |
| 1025 | fdt_get_status(subnode) == DT_DISABLED) { |
| 1026 | continue; |
| 1027 | } |
| 1028 | |
| 1029 | cuint = fdt_getprop(fdt, subnode, "clock-frequency", &ret); |
| 1030 | if (cuint == NULL) { |
| 1031 | return 0UL; |
| 1032 | } |
| 1033 | |
| 1034 | return fdt32_to_cpu(*cuint); |
| 1035 | } |
| 1036 | |
| 1037 | return 0UL; |
| 1038 | } |
| 1039 | |
| 1040 | void clk_stm32_osc_init(struct stm32_clk_priv *priv, int id) |
| 1041 | { |
| 1042 | struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); |
| 1043 | const char *name = osc_data->name; |
| 1044 | |
| 1045 | osc_data->frequency = clk_stm32_get_dt_oscillator_frequency(name); |
| 1046 | } |
| 1047 | |
| 1048 | const struct stm32_clk_ops clk_stm32_osc_ops = { |
| 1049 | .recalc_rate = clk_stm32_osc_recalc_rate, |
| 1050 | .is_enabled = clk_stm32_osc_gate_is_enabled, |
| 1051 | .enable = clk_stm32_osc_gate_enable, |
| 1052 | .disable = clk_stm32_osc_gate_disable, |
| 1053 | .init = clk_stm32_osc_init, |
| 1054 | }; |
| 1055 | |
| 1056 | const struct stm32_clk_ops clk_stm32_osc_nogate_ops = { |
| 1057 | .recalc_rate = clk_stm32_osc_recalc_rate, |
| 1058 | .init = clk_stm32_osc_init, |
| 1059 | }; |
| 1060 | |
| 1061 | int stm32_clk_parse_fdt_by_name(void *fdt, int node, const char *name, uint32_t *tab, uint32_t *nb) |
| 1062 | { |
| 1063 | const fdt32_t *cell; |
| 1064 | int len = 0; |
| 1065 | uint32_t i; |
| 1066 | |
| 1067 | cell = fdt_getprop(fdt, node, name, &len); |
Yann Gautier | d56fff3 | 2022-04-05 15:16:28 +0200 | [diff] [blame] | 1068 | if (cell == NULL) { |
| 1069 | *nb = 0U; |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
| 1073 | for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) { |
| 1074 | uint32_t val = fdt32_to_cpu(cell[i]); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 1075 | |
Yann Gautier | d56fff3 | 2022-04-05 15:16:28 +0200 | [diff] [blame] | 1076 | tab[i] = val; |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | *nb = (uint32_t)len / sizeof(uint32_t); |
| 1080 | |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | int clk_stm32_init(struct stm32_clk_priv *priv, uintptr_t base) |
| 1085 | { |
| 1086 | unsigned int i; |
| 1087 | |
| 1088 | stm32_clock_data = priv; |
| 1089 | |
| 1090 | priv->base = base; |
| 1091 | |
| 1092 | for (i = 0U; i < priv->num; i++) { |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 1093 | const struct stm32_clk_ops *ops = _clk_get_ops(priv, i); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 1094 | |
Gabriel Fernandez | 56fd523 | 2023-08-21 13:31:47 +0200 | [diff] [blame] | 1095 | if (ops->init != NULL) { |
| 1096 | ops->init(priv, i); |
Gabriel Fernandez | 1308d75 | 2020-03-11 11:30:34 +0100 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | stm32_clk_register(); |
| 1101 | |
| 1102 | return 0; |
| 1103 | } |