1 /* 2 * Copyright (C) 2022, STMicroelectronics - All Rights Reserved 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 31 static void stm32mp1_clk_lock(struct spinlock *lock) 32 { 33 if (stm32mp_lock_available()) { 34 /* Assume interrupts are masked */ 35 spin_lock(lock); 36 } 37 } 38 39 static void stm32mp1_clk_unlock(struct spinlock *lock) 40 { 41 if (stm32mp_lock_available()) { 42 spin_unlock(lock); 43 } 44 } 45 46 void stm32mp1_clk_rcc_regs_lock(void) 47 { 48 stm32mp1_clk_lock(®_lock); 49 } 50 51 void stm32mp1_clk_rcc_regs_unlock(void) 52 { 53 stm32mp1_clk_unlock(®_lock); 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 146 return _clk_stm32_gate_wait_ready(priv, osc_data->gate_rdy_id, ready_on); 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 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 227 #define clk_div_mask(_width) GENMASK(((_width) - 1U), 0U) 228 229 static unsigned int _get_table_div(const struct clk_div_table *table, 230 unsigned int val) 231 { 232 const struct clk_div_table *clkt; 233 234 for (clkt = table; clkt->div; clkt++) { 235 if (clkt->val == val) { 236 return clkt->div; 237 } 238 } 239 240 return 0; 241 } 242 243 static unsigned int _get_div(const struct clk_div_table *table, 244 unsigned int val, unsigned long flags, 245 uint8_t width) 246 { 247 if ((flags & CLK_DIVIDER_ONE_BASED) != 0UL) { 248 return val; 249 } 250 251 if ((flags & CLK_DIVIDER_POWER_OF_TWO) != 0UL) { 252 return BIT(val); 253 } 254 255 if ((flags & CLK_DIVIDER_MAX_AT_ZERO) != 0UL) { 256 return (val != 0U) ? val : BIT(width); 257 } 258 259 if (table != NULL) { 260 return _get_table_div(table, val); 261 } 262 263 return val + 1U; 264 } 265 266 #define TIMEOUT_US_200MS U(200000) 267 #define CLKSRC_TIMEOUT TIMEOUT_US_200MS 268 269 int clk_mux_set_parent(struct stm32_clk_priv *priv, uint16_t pid, uint8_t sel) 270 { 271 const struct parent_cfg *parents = &priv->parents[pid & MUX_PARENT_MASK]; 272 const struct mux_cfg *mux = parents->mux; 273 uintptr_t address = priv->base + mux->offset; 274 uint32_t mask; 275 uint64_t timeout; 276 277 mask = MASK_WIDTH_SHIFT(mux->width, mux->shift); 278 279 mmio_clrsetbits_32(address, mask, (sel << mux->shift) & mask); 280 281 if (mux->bitrdy == MUX_NO_BIT_RDY) { 282 return 0; 283 } 284 285 timeout = timeout_init_us(CLKSRC_TIMEOUT); 286 287 mask = BIT(mux->bitrdy); 288 289 while ((mmio_read_32(address) & mask) == 0U) { 290 if (timeout_elapsed(timeout)) { 291 return -ETIMEDOUT; 292 } 293 } 294 295 return 0; 296 } 297 298 int _clk_stm32_set_parent(struct stm32_clk_priv *priv, int clk, int clkp) 299 { 300 const struct parent_cfg *parents; 301 uint16_t pid; 302 uint8_t sel; 303 int old_parent; 304 305 pid = priv->clks[clk].parent; 306 307 if ((pid == CLK_IS_ROOT) || (pid < MUX_MAX_PARENTS)) { 308 return -EINVAL; 309 } 310 311 old_parent = _clk_stm32_get_parent(priv, clk); 312 if (old_parent < 0) { 313 return old_parent; 314 } 315 if (old_parent == clkp) { 316 return 0; 317 } 318 319 parents = &priv->parents[pid & MUX_PARENT_MASK]; 320 321 for (sel = 0; sel < parents->num_parents; sel++) { 322 if (parents->id_parents[sel] == (uint16_t)clkp) { 323 bool clk_was_enabled = _clk_stm32_is_enabled(priv, clk); 324 int err = 0; 325 326 /* Enable the parents (for glitch free mux) */ 327 _clk_stm32_enable(priv, clkp); 328 _clk_stm32_enable(priv, old_parent); 329 330 err = clk_mux_set_parent(priv, pid, sel); 331 332 _clk_stm32_disable(priv, old_parent); 333 334 if (clk_was_enabled) { 335 _clk_stm32_disable(priv, old_parent); 336 } else { 337 _clk_stm32_disable(priv, clkp); 338 } 339 340 return err; 341 } 342 } 343 344 return -EINVAL; 345 } 346 347 int clk_mux_get_parent(struct stm32_clk_priv *priv, uint32_t mux_id) 348 { 349 const struct parent_cfg *parent; 350 const struct mux_cfg *mux; 351 uint32_t mask; 352 353 if (mux_id >= priv->nb_parents) { 354 panic(); 355 } 356 357 parent = &priv->parents[mux_id]; 358 mux = parent->mux; 359 360 mask = MASK_WIDTH_SHIFT(mux->width, mux->shift); 361 362 return (mmio_read_32(priv->base + mux->offset) & mask) >> mux->shift; 363 } 364 365 int _clk_stm32_set_parent_by_index(struct stm32_clk_priv *priv, int clk, int sel) 366 { 367 uint16_t pid; 368 369 pid = priv->clks[clk].parent; 370 371 if ((pid == CLK_IS_ROOT) || (pid < MUX_MAX_PARENTS)) { 372 return -EINVAL; 373 } 374 375 return clk_mux_set_parent(priv, pid, sel); 376 } 377 378 int _clk_stm32_get_parent(struct stm32_clk_priv *priv, int clk_id) 379 { 380 const struct clk_stm32 *clk = _clk_get(priv, clk_id); 381 const struct parent_cfg *parent; 382 uint16_t mux_id; 383 int sel; 384 385 mux_id = priv->clks[clk_id].parent; 386 if (mux_id == CLK_IS_ROOT) { 387 return CLK_IS_ROOT; 388 } 389 390 if (mux_id < MUX_MAX_PARENTS) { 391 return mux_id & MUX_PARENT_MASK; 392 } 393 394 mux_id &= MUX_PARENT_MASK; 395 parent = &priv->parents[mux_id]; 396 397 if (clk->ops->get_parent != NULL) { 398 sel = clk->ops->get_parent(priv, clk_id); 399 } else { 400 sel = clk_mux_get_parent(priv, mux_id); 401 } 402 403 if ((sel >= 0) && (sel < parent->num_parents)) { 404 return parent->id_parents[sel]; 405 } 406 407 return -EINVAL; 408 } 409 410 int _clk_stm32_get_parent_index(struct stm32_clk_priv *priv, int clk_id) 411 { 412 uint16_t mux_id; 413 414 mux_id = priv->clks[clk_id].parent; 415 if (mux_id == CLK_IS_ROOT) { 416 return CLK_IS_ROOT; 417 } 418 419 if (mux_id < MUX_MAX_PARENTS) { 420 return mux_id & MUX_PARENT_MASK; 421 } 422 423 mux_id &= MUX_PARENT_MASK; 424 425 return clk_mux_get_parent(priv, mux_id); 426 } 427 428 int _clk_stm32_get_parent_by_index(struct stm32_clk_priv *priv, int clk_id, int idx) 429 { 430 const struct parent_cfg *parent; 431 uint16_t mux_id; 432 433 mux_id = priv->clks[clk_id].parent; 434 if (mux_id == CLK_IS_ROOT) { 435 return CLK_IS_ROOT; 436 } 437 438 if (mux_id < MUX_MAX_PARENTS) { 439 return mux_id & MUX_PARENT_MASK; 440 } 441 442 mux_id &= MUX_PARENT_MASK; 443 parent = &priv->parents[mux_id]; 444 445 if (idx < parent->num_parents) { 446 return parent->id_parents[idx]; 447 } 448 449 return -EINVAL; 450 } 451 452 int clk_get_index(struct stm32_clk_priv *priv, unsigned long binding_id) 453 { 454 unsigned int i; 455 456 for (i = 0U; i < priv->num; i++) { 457 if (binding_id == priv->clks[i].binding) { 458 return (int)i; 459 } 460 } 461 462 return -EINVAL; 463 } 464 465 unsigned long _clk_stm32_get_rate(struct stm32_clk_priv *priv, int id) 466 { 467 const struct clk_stm32 *clk = _clk_get(priv, id); 468 int parent; 469 470 if ((unsigned int)id >= priv->num) { 471 return 0UL; 472 } 473 474 parent = _clk_stm32_get_parent(priv, id); 475 if (parent < 0) { 476 return 0UL; 477 } 478 479 if (clk->ops->recalc_rate != NULL) { 480 unsigned long prate = 0UL; 481 482 if (parent != CLK_IS_ROOT) { 483 prate = _clk_stm32_get_rate(priv, parent); 484 } 485 486 return clk->ops->recalc_rate(priv, id, prate); 487 } 488 489 if (parent == CLK_IS_ROOT) { 490 panic(); 491 } 492 493 return _clk_stm32_get_rate(priv, parent); 494 } 495 496 unsigned long _clk_stm32_get_parent_rate(struct stm32_clk_priv *priv, int id) 497 { 498 int parent_id = _clk_stm32_get_parent(priv, id); 499 500 if (parent_id < 0) { 501 return 0UL; 502 } 503 504 return _clk_stm32_get_rate(priv, parent_id); 505 } 506 507 static uint8_t _stm32_clk_get_flags(struct stm32_clk_priv *priv, int id) 508 { 509 return priv->clks[id].flags; 510 } 511 512 bool _stm32_clk_is_flags(struct stm32_clk_priv *priv, int id, uint8_t flag) 513 { 514 if ((_stm32_clk_get_flags(priv, id) & flag) != 0U) { 515 return true; 516 } 517 518 return false; 519 } 520 521 int clk_stm32_enable_call_ops(struct stm32_clk_priv *priv, uint16_t id) 522 { 523 const struct clk_stm32 *clk = _clk_get(priv, id); 524 525 if (clk->ops->enable != NULL) { 526 clk->ops->enable(priv, id); 527 } 528 529 return 0; 530 } 531 532 static int _clk_stm32_enable_core(struct stm32_clk_priv *priv, int id) 533 { 534 int parent; 535 int ret = 0; 536 537 if (priv->gate_refcounts[id] == 0U) { 538 parent = _clk_stm32_get_parent(priv, id); 539 if (parent < 0) { 540 return parent; 541 } 542 if (parent != CLK_IS_ROOT) { 543 ret = _clk_stm32_enable_core(priv, parent); 544 if (ret != 0) { 545 return ret; 546 } 547 } 548 clk_stm32_enable_call_ops(priv, id); 549 } 550 551 priv->gate_refcounts[id]++; 552 553 if (priv->gate_refcounts[id] == UINT_MAX) { 554 ERROR("%s: %d max enable count !", __func__, id); 555 panic(); 556 } 557 558 return 0; 559 } 560 561 int _clk_stm32_enable(struct stm32_clk_priv *priv, int id) 562 { 563 int ret; 564 565 stm32mp1_clk_lock(&refcount_lock); 566 ret = _clk_stm32_enable_core(priv, id); 567 stm32mp1_clk_unlock(&refcount_lock); 568 569 return ret; 570 } 571 572 void clk_stm32_disable_call_ops(struct stm32_clk_priv *priv, uint16_t id) 573 { 574 const struct clk_stm32 *clk = _clk_get(priv, id); 575 576 if (clk->ops->disable != NULL) { 577 clk->ops->disable(priv, id); 578 } 579 } 580 581 static void _clk_stm32_disable_core(struct stm32_clk_priv *priv, int id) 582 { 583 int parent; 584 585 if ((priv->gate_refcounts[id] == 1U) && _stm32_clk_is_flags(priv, id, CLK_IS_CRITICAL)) { 586 return; 587 } 588 589 if (priv->gate_refcounts[id] == 0U) { 590 /* case of clock ignore unused */ 591 if (_clk_stm32_is_enabled(priv, id)) { 592 clk_stm32_disable_call_ops(priv, id); 593 return; 594 } 595 VERBOSE("%s: %d already disabled !\n\n", __func__, id); 596 return; 597 } 598 599 if (--priv->gate_refcounts[id] > 0U) { 600 return; 601 } 602 603 clk_stm32_disable_call_ops(priv, id); 604 605 parent = _clk_stm32_get_parent(priv, id); 606 if ((parent >= 0) && (parent != CLK_IS_ROOT)) { 607 _clk_stm32_disable_core(priv, parent); 608 } 609 } 610 611 void _clk_stm32_disable(struct stm32_clk_priv *priv, int id) 612 { 613 stm32mp1_clk_lock(&refcount_lock); 614 615 _clk_stm32_disable_core(priv, id); 616 617 stm32mp1_clk_unlock(&refcount_lock); 618 } 619 620 bool _clk_stm32_is_enabled(struct stm32_clk_priv *priv, int id) 621 { 622 const struct clk_stm32 *clk = _clk_get(priv, id); 623 624 if (clk->ops->is_enabled != NULL) { 625 return clk->ops->is_enabled(priv, id); 626 } 627 628 return priv->gate_refcounts[id]; 629 } 630 631 static int clk_stm32_enable(unsigned long binding_id) 632 { 633 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 634 int id; 635 636 id = clk_get_index(priv, binding_id); 637 if (id == -EINVAL) { 638 return id; 639 } 640 641 return _clk_stm32_enable(priv, id); 642 } 643 644 static void clk_stm32_disable(unsigned long binding_id) 645 { 646 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 647 int id; 648 649 id = clk_get_index(priv, binding_id); 650 if (id != -EINVAL) { 651 _clk_stm32_disable(priv, id); 652 } 653 } 654 655 static bool clk_stm32_is_enabled(unsigned long binding_id) 656 { 657 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 658 int id; 659 660 id = clk_get_index(priv, binding_id); 661 if (id == -EINVAL) { 662 return false; 663 } 664 665 return _clk_stm32_is_enabled(priv, id); 666 } 667 668 static unsigned long clk_stm32_get_rate(unsigned long binding_id) 669 { 670 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 671 int id; 672 673 id = clk_get_index(priv, binding_id); 674 if (id == -EINVAL) { 675 return 0UL; 676 } 677 678 return _clk_stm32_get_rate(priv, id); 679 } 680 681 static int clk_stm32_get_parent(unsigned long binding_id) 682 { 683 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 684 int id; 685 686 id = clk_get_index(priv, binding_id); 687 if (id == -EINVAL) { 688 return id; 689 } 690 691 return _clk_stm32_get_parent(priv, id); 692 } 693 694 static const struct clk_ops stm32mp_clk_ops = { 695 .enable = clk_stm32_enable, 696 .disable = clk_stm32_disable, 697 .is_enabled = clk_stm32_is_enabled, 698 .get_rate = clk_stm32_get_rate, 699 .get_parent = clk_stm32_get_parent, 700 }; 701 702 void clk_stm32_enable_critical_clocks(void) 703 { 704 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 705 unsigned int i; 706 707 for (i = 0U; i < priv->num; i++) { 708 if (_stm32_clk_is_flags(priv, i, CLK_IS_CRITICAL)) { 709 _clk_stm32_enable(priv, i); 710 } 711 } 712 } 713 714 static void stm32_clk_register(void) 715 { 716 clk_register(&stm32mp_clk_ops); 717 } 718 719 uint32_t clk_stm32_div_get_value(struct stm32_clk_priv *priv, int div_id) 720 { 721 const struct div_cfg *divider = &priv->div[div_id]; 722 uint32_t val = 0; 723 724 val = mmio_read_32(priv->base + divider->offset) >> divider->shift; 725 val &= clk_div_mask(divider->width); 726 727 return val; 728 } 729 730 unsigned long _clk_stm32_divider_recalc(struct stm32_clk_priv *priv, 731 int div_id, 732 unsigned long prate) 733 { 734 const struct div_cfg *divider = &priv->div[div_id]; 735 uint32_t val = clk_stm32_div_get_value(priv, div_id); 736 unsigned int div = 0U; 737 738 div = _get_div(divider->table, val, divider->flags, divider->width); 739 if (div == 0U) { 740 return prate; 741 } 742 743 return div_round_up((uint64_t)prate, div); 744 } 745 746 unsigned long clk_stm32_divider_recalc(struct stm32_clk_priv *priv, int id, 747 unsigned long prate) 748 { 749 const struct clk_stm32 *clk = _clk_get(priv, id); 750 struct clk_stm32_div_cfg *div_cfg = clk->clock_cfg; 751 752 return _clk_stm32_divider_recalc(priv, div_cfg->id, prate); 753 } 754 755 const struct stm32_clk_ops clk_stm32_divider_ops = { 756 .recalc_rate = clk_stm32_divider_recalc, 757 }; 758 759 int clk_stm32_set_div(struct stm32_clk_priv *priv, uint32_t div_id, uint32_t value) 760 { 761 const struct div_cfg *divider; 762 uintptr_t address; 763 uint64_t timeout; 764 uint32_t mask; 765 766 if (div_id >= priv->nb_div) { 767 panic(); 768 } 769 770 divider = &priv->div[div_id]; 771 address = priv->base + divider->offset; 772 773 mask = MASK_WIDTH_SHIFT(divider->width, divider->shift); 774 mmio_clrsetbits_32(address, mask, (value << divider->shift) & mask); 775 776 if (divider->bitrdy == DIV_NO_BIT_RDY) { 777 return 0; 778 } 779 780 timeout = timeout_init_us(CLKSRC_TIMEOUT); 781 mask = BIT(divider->bitrdy); 782 783 while ((mmio_read_32(address) & mask) == 0U) { 784 if (timeout_elapsed(timeout)) { 785 return -ETIMEDOUT; 786 } 787 } 788 789 return 0; 790 } 791 792 int _clk_stm32_gate_wait_ready(struct stm32_clk_priv *priv, uint16_t gate_id, 793 bool ready_on) 794 { 795 const struct gate_cfg *gate = &priv->gates[gate_id]; 796 uintptr_t address = priv->base + gate->offset; 797 uint32_t mask_rdy = BIT(gate->bit_idx); 798 uint64_t timeout; 799 uint32_t mask_test; 800 801 if (ready_on) { 802 mask_test = BIT(gate->bit_idx); 803 } else { 804 mask_test = 0U; 805 } 806 807 timeout = timeout_init_us(OSCRDY_TIMEOUT); 808 809 while ((mmio_read_32(address) & mask_rdy) != mask_test) { 810 if (timeout_elapsed(timeout)) { 811 break; 812 } 813 } 814 815 if ((mmio_read_32(address) & mask_rdy) != mask_test) { 816 return -ETIMEDOUT; 817 } 818 819 return 0; 820 } 821 822 int clk_stm32_gate_enable(struct stm32_clk_priv *priv, int id) 823 { 824 const struct clk_stm32 *clk = _clk_get(priv, id); 825 struct clk_stm32_gate_cfg *cfg = clk->clock_cfg; 826 const struct gate_cfg *gate = &priv->gates[cfg->id]; 827 uintptr_t addr = priv->base + gate->offset; 828 829 if (gate->set_clr != 0U) { 830 mmio_write_32(addr, BIT(gate->bit_idx)); 831 832 } else { 833 mmio_setbits_32(addr, BIT(gate->bit_idx)); 834 } 835 836 return 0; 837 } 838 839 void clk_stm32_gate_disable(struct stm32_clk_priv *priv, int id) 840 { 841 const struct clk_stm32 *clk = _clk_get(priv, id); 842 struct clk_stm32_gate_cfg *cfg = clk->clock_cfg; 843 const struct gate_cfg *gate = &priv->gates[cfg->id]; 844 uintptr_t addr = priv->base + gate->offset; 845 846 if (gate->set_clr != 0U) { 847 mmio_write_32(addr + RCC_MP_ENCLRR_OFFSET, BIT(gate->bit_idx)); 848 } else { 849 mmio_clrbits_32(addr, BIT(gate->bit_idx)); 850 } 851 } 852 853 bool _clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int gate_id) 854 { 855 const struct gate_cfg *gate; 856 uint32_t addr; 857 858 gate = &priv->gates[gate_id]; 859 addr = priv->base + gate->offset; 860 861 return ((mmio_read_32(addr) & BIT(gate->bit_idx)) != 0U); 862 } 863 864 bool clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int id) 865 { 866 const struct clk_stm32 *clk = _clk_get(priv, id); 867 struct clk_stm32_gate_cfg *cfg = clk->clock_cfg; 868 869 return _clk_stm32_gate_is_enabled(priv, cfg->id); 870 } 871 872 const struct stm32_clk_ops clk_stm32_gate_ops = { 873 .enable = clk_stm32_gate_enable, 874 .disable = clk_stm32_gate_disable, 875 .is_enabled = clk_stm32_gate_is_enabled, 876 }; 877 878 const struct stm32_clk_ops clk_fixed_factor_ops = { 879 .recalc_rate = fixed_factor_recalc_rate, 880 }; 881 882 unsigned long fixed_factor_recalc_rate(struct stm32_clk_priv *priv, 883 int id, unsigned long prate) 884 { 885 const struct clk_stm32 *clk = _clk_get(priv, id); 886 const struct fixed_factor_cfg *cfg = clk->clock_cfg; 887 unsigned long long rate; 888 889 rate = (unsigned long long)prate * cfg->mult; 890 891 if (cfg->div == 0U) { 892 ERROR("division by zero\n"); 893 panic(); 894 } 895 896 return (unsigned long)(rate / cfg->div); 897 }; 898 899 #define APB_DIV_MASK GENMASK(2, 0) 900 #define TIM_PRE_MASK BIT(0) 901 902 static unsigned long timer_recalc_rate(struct stm32_clk_priv *priv, 903 int id, unsigned long prate) 904 { 905 const struct clk_stm32 *clk = _clk_get(priv, id); 906 const struct clk_timer_cfg *cfg = clk->clock_cfg; 907 uint32_t prescaler, timpre; 908 uintptr_t rcc_base = priv->base; 909 910 prescaler = mmio_read_32(rcc_base + cfg->apbdiv) & 911 APB_DIV_MASK; 912 913 timpre = mmio_read_32(rcc_base + cfg->timpre) & 914 TIM_PRE_MASK; 915 916 if (prescaler == 0U) { 917 return prate; 918 } 919 920 return prate * (timpre + 1U) * 2U; 921 }; 922 923 const struct stm32_clk_ops clk_timer_ops = { 924 .recalc_rate = timer_recalc_rate, 925 }; 926 927 static unsigned long clk_fixed_rate_recalc(struct stm32_clk_priv *priv, int id, 928 unsigned long prate) 929 { 930 const struct clk_stm32 *clk = _clk_get(priv, id); 931 struct clk_stm32_fixed_rate_cfg *cfg = clk->clock_cfg; 932 933 return cfg->rate; 934 } 935 936 const struct stm32_clk_ops clk_stm32_fixed_rate_ops = { 937 .recalc_rate = clk_fixed_rate_recalc, 938 }; 939 940 static unsigned long clk_stm32_osc_recalc_rate(struct stm32_clk_priv *priv, 941 int id, unsigned long prate) 942 { 943 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); 944 945 return osc_data->frequency; 946 }; 947 948 bool clk_stm32_osc_gate_is_enabled(struct stm32_clk_priv *priv, int id) 949 { 950 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); 951 952 return _clk_stm32_gate_is_enabled(priv, osc_data->gate_id); 953 954 } 955 956 int clk_stm32_osc_gate_enable(struct stm32_clk_priv *priv, int id) 957 { 958 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); 959 960 _clk_stm32_gate_enable(priv, osc_data->gate_id); 961 962 if (_clk_stm32_gate_wait_ready(priv, osc_data->gate_rdy_id, true) != 0U) { 963 ERROR("%s: %s (%d)\n", __func__, osc_data->name, __LINE__); 964 panic(); 965 } 966 967 return 0; 968 } 969 970 void clk_stm32_osc_gate_disable(struct stm32_clk_priv *priv, int id) 971 { 972 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); 973 974 _clk_stm32_gate_disable(priv, osc_data->gate_id); 975 976 if (_clk_stm32_gate_wait_ready(priv, osc_data->gate_rdy_id, false) != 0U) { 977 ERROR("%s: %s (%d)\n", __func__, osc_data->name, __LINE__); 978 panic(); 979 } 980 } 981 982 static unsigned long clk_stm32_get_dt_oscillator_frequency(const char *name) 983 { 984 void *fdt = NULL; 985 int node = 0; 986 int subnode = 0; 987 988 if (fdt_get_address(&fdt) == 0) { 989 panic(); 990 } 991 992 node = fdt_path_offset(fdt, "/clocks"); 993 if (node < 0) { 994 return 0UL; 995 } 996 997 fdt_for_each_subnode(subnode, fdt, node) { 998 const char *cchar = NULL; 999 const fdt32_t *cuint = NULL; 1000 int ret = 0; 1001 1002 cchar = fdt_get_name(fdt, subnode, &ret); 1003 if (cchar == NULL) { 1004 continue; 1005 } 1006 1007 if (strncmp(cchar, name, (size_t)ret) || 1008 fdt_get_status(subnode) == DT_DISABLED) { 1009 continue; 1010 } 1011 1012 cuint = fdt_getprop(fdt, subnode, "clock-frequency", &ret); 1013 if (cuint == NULL) { 1014 return 0UL; 1015 } 1016 1017 return fdt32_to_cpu(*cuint); 1018 } 1019 1020 return 0UL; 1021 } 1022 1023 void clk_stm32_osc_init(struct stm32_clk_priv *priv, int id) 1024 { 1025 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); 1026 const char *name = osc_data->name; 1027 1028 osc_data->frequency = clk_stm32_get_dt_oscillator_frequency(name); 1029 } 1030 1031 const struct stm32_clk_ops clk_stm32_osc_ops = { 1032 .recalc_rate = clk_stm32_osc_recalc_rate, 1033 .is_enabled = clk_stm32_osc_gate_is_enabled, 1034 .enable = clk_stm32_osc_gate_enable, 1035 .disable = clk_stm32_osc_gate_disable, 1036 .init = clk_stm32_osc_init, 1037 }; 1038 1039 const struct stm32_clk_ops clk_stm32_osc_nogate_ops = { 1040 .recalc_rate = clk_stm32_osc_recalc_rate, 1041 .init = clk_stm32_osc_init, 1042 }; 1043 1044 int stm32_clk_parse_fdt_by_name(void *fdt, int node, const char *name, uint32_t *tab, uint32_t *nb) 1045 { 1046 const fdt32_t *cell; 1047 int len = 0; 1048 uint32_t i; 1049 1050 cell = fdt_getprop(fdt, node, name, &len); 1051 if (cell == NULL) { 1052 *nb = 0U; 1053 return 0; 1054 } 1055 1056 for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) { 1057 uint32_t val = fdt32_to_cpu(cell[i]); 1058 1059 tab[i] = val; 1060 } 1061 1062 *nb = (uint32_t)len / sizeof(uint32_t); 1063 1064 return 0; 1065 } 1066 1067 int clk_stm32_init(struct stm32_clk_priv *priv, uintptr_t base) 1068 { 1069 unsigned int i; 1070 1071 stm32_clock_data = priv; 1072 1073 priv->base = base; 1074 1075 for (i = 0U; i < priv->num; i++) { 1076 const struct clk_stm32 *clk = _clk_get(priv, i); 1077 1078 assert(clk->ops != NULL); 1079 1080 if (clk->ops->init != NULL) { 1081 clk->ops->init(priv, i); 1082 } 1083 } 1084 1085 stm32_clk_register(); 1086 1087 return 0; 1088 } 1089