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 unsigned long rate = 0UL; 470 471 if ((unsigned int)id >= priv->num) { 472 return rate; 473 } 474 475 parent = _clk_stm32_get_parent(priv, id); 476 if (parent < 0) { 477 return 0UL; 478 } 479 480 if (clk->ops->recalc_rate != NULL) { 481 unsigned long prate = 0UL; 482 483 if (parent != CLK_IS_ROOT) { 484 prate = _clk_stm32_get_rate(priv, parent); 485 } 486 487 rate = clk->ops->recalc_rate(priv, id, prate); 488 489 return rate; 490 } 491 492 switch (parent) { 493 case CLK_IS_ROOT: 494 panic(); 495 496 default: 497 rate = _clk_stm32_get_rate(priv, parent); 498 break; 499 } 500 return rate; 501 502 } 503 504 unsigned long _clk_stm32_get_parent_rate(struct stm32_clk_priv *priv, int id) 505 { 506 int parent_id = _clk_stm32_get_parent(priv, id); 507 508 if (parent_id < 0) { 509 return 0UL; 510 } 511 512 return _clk_stm32_get_rate(priv, parent_id); 513 } 514 515 static uint8_t _stm32_clk_get_flags(struct stm32_clk_priv *priv, int id) 516 { 517 return priv->clks[id].flags; 518 } 519 520 bool _stm32_clk_is_flags(struct stm32_clk_priv *priv, int id, uint8_t flag) 521 { 522 if (_stm32_clk_get_flags(priv, id) & flag) { 523 return true; 524 } 525 526 return false; 527 } 528 529 int clk_stm32_enable_call_ops(struct stm32_clk_priv *priv, uint16_t id) 530 { 531 const struct clk_stm32 *clk = _clk_get(priv, id); 532 533 if (clk->ops->enable != NULL) { 534 clk->ops->enable(priv, id); 535 } 536 537 return 0; 538 } 539 540 static int _clk_stm32_enable_core(struct stm32_clk_priv *priv, int id) 541 { 542 int parent; 543 int ret = 0; 544 545 if (priv->gate_refcounts[id] == 0U) { 546 parent = _clk_stm32_get_parent(priv, id); 547 if (parent < 0) { 548 return parent; 549 } 550 if (parent != CLK_IS_ROOT) { 551 ret = _clk_stm32_enable_core(priv, parent); 552 if (ret) { 553 return ret; 554 } 555 } 556 clk_stm32_enable_call_ops(priv, id); 557 } 558 559 priv->gate_refcounts[id]++; 560 561 if (priv->gate_refcounts[id] == UINT_MAX) { 562 ERROR("%s: %d max enable count !", __func__, id); 563 panic(); 564 } 565 566 return 0; 567 } 568 569 int _clk_stm32_enable(struct stm32_clk_priv *priv, int id) 570 { 571 int ret; 572 573 stm32mp1_clk_lock(&refcount_lock); 574 ret = _clk_stm32_enable_core(priv, id); 575 stm32mp1_clk_unlock(&refcount_lock); 576 577 return ret; 578 } 579 580 void clk_stm32_disable_call_ops(struct stm32_clk_priv *priv, uint16_t id) 581 { 582 const struct clk_stm32 *clk = _clk_get(priv, id); 583 584 if (clk->ops->disable != NULL) { 585 clk->ops->disable(priv, id); 586 } 587 } 588 589 static void _clk_stm32_disable_core(struct stm32_clk_priv *priv, int id) 590 { 591 int parent; 592 593 if ((priv->gate_refcounts[id] == 1U) && _stm32_clk_is_flags(priv, id, CLK_IS_CRITICAL)) { 594 return; 595 } 596 597 if (priv->gate_refcounts[id] == 0U) { 598 /* case of clock ignore unused */ 599 if (_clk_stm32_is_enabled(priv, id)) { 600 clk_stm32_disable_call_ops(priv, id); 601 return; 602 } 603 VERBOSE("%s: %d already disabled !\n\n", __func__, id); 604 return; 605 } 606 607 if (--priv->gate_refcounts[id] > 0U) { 608 return; 609 } 610 611 clk_stm32_disable_call_ops(priv, id); 612 613 parent = _clk_stm32_get_parent(priv, id); 614 if ((parent >= 0) && (parent != CLK_IS_ROOT)) { 615 _clk_stm32_disable_core(priv, parent); 616 } 617 } 618 619 void _clk_stm32_disable(struct stm32_clk_priv *priv, int id) 620 { 621 stm32mp1_clk_lock(&refcount_lock); 622 623 _clk_stm32_disable_core(priv, id); 624 625 stm32mp1_clk_unlock(&refcount_lock); 626 } 627 628 bool _clk_stm32_is_enabled(struct stm32_clk_priv *priv, int id) 629 { 630 const struct clk_stm32 *clk = _clk_get(priv, id); 631 632 if (clk->ops->is_enabled != NULL) { 633 return clk->ops->is_enabled(priv, id); 634 } 635 636 return priv->gate_refcounts[id]; 637 } 638 639 static int clk_stm32_enable(unsigned long binding_id) 640 { 641 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 642 int id; 643 644 id = clk_get_index(priv, binding_id); 645 if (id == -EINVAL) { 646 return id; 647 } 648 649 return _clk_stm32_enable(priv, id); 650 } 651 652 static void clk_stm32_disable(unsigned long binding_id) 653 { 654 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 655 int id; 656 657 id = clk_get_index(priv, binding_id); 658 if (id != -EINVAL) { 659 _clk_stm32_disable(priv, id); 660 } 661 } 662 663 static bool clk_stm32_is_enabled(unsigned long binding_id) 664 { 665 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 666 int id; 667 668 id = clk_get_index(priv, binding_id); 669 if (id == -EINVAL) { 670 return false; 671 } 672 673 return _clk_stm32_is_enabled(priv, id); 674 } 675 676 static unsigned long clk_stm32_get_rate(unsigned long binding_id) 677 { 678 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 679 int id; 680 681 id = clk_get_index(priv, binding_id); 682 if (id == -EINVAL) { 683 return 0UL; 684 } 685 686 return _clk_stm32_get_rate(priv, id); 687 } 688 689 static int clk_stm32_get_parent(unsigned long binding_id) 690 { 691 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 692 int id; 693 694 id = clk_get_index(priv, binding_id); 695 if (id == -EINVAL) { 696 return id; 697 } 698 699 return _clk_stm32_get_parent(priv, id); 700 } 701 702 static const struct clk_ops stm32mp_clk_ops = { 703 .enable = clk_stm32_enable, 704 .disable = clk_stm32_disable, 705 .is_enabled = clk_stm32_is_enabled, 706 .get_rate = clk_stm32_get_rate, 707 .get_parent = clk_stm32_get_parent, 708 }; 709 710 void clk_stm32_enable_critical_clocks(void) 711 { 712 struct stm32_clk_priv *priv = clk_stm32_get_priv(); 713 unsigned int i; 714 715 for (i = 0U; i < priv->num; i++) { 716 if (_stm32_clk_is_flags(priv, i, CLK_IS_CRITICAL)) { 717 _clk_stm32_enable(priv, i); 718 } 719 } 720 } 721 722 static void stm32_clk_register(void) 723 { 724 clk_register(&stm32mp_clk_ops); 725 } 726 727 uint32_t clk_stm32_div_get_value(struct stm32_clk_priv *priv, int div_id) 728 { 729 const struct div_cfg *divider = &priv->div[div_id]; 730 uint32_t val = 0; 731 732 val = mmio_read_32(priv->base + divider->offset) >> divider->shift; 733 val &= clk_div_mask(divider->width); 734 735 return val; 736 } 737 738 unsigned long _clk_stm32_divider_recalc(struct stm32_clk_priv *priv, 739 int div_id, 740 unsigned long prate) 741 { 742 const struct div_cfg *divider = &priv->div[div_id]; 743 uint32_t val = clk_stm32_div_get_value(priv, div_id); 744 unsigned int div = 0U; 745 746 div = _get_div(divider->table, val, divider->flags, divider->width); 747 if (div == 0U) { 748 return prate; 749 } 750 751 return div_round_up((uint64_t)prate, div); 752 } 753 754 unsigned long clk_stm32_divider_recalc(struct stm32_clk_priv *priv, int id, 755 unsigned long prate) 756 { 757 const struct clk_stm32 *clk = _clk_get(priv, id); 758 struct clk_stm32_div_cfg *div_cfg = clk->clock_cfg; 759 760 return _clk_stm32_divider_recalc(priv, div_cfg->id, prate); 761 } 762 763 const struct stm32_clk_ops clk_stm32_divider_ops = { 764 .recalc_rate = clk_stm32_divider_recalc, 765 }; 766 767 int clk_stm32_set_div(struct stm32_clk_priv *priv, uint32_t div_id, uint32_t value) 768 { 769 const struct div_cfg *divider; 770 uintptr_t address; 771 uint64_t timeout; 772 uint32_t mask; 773 774 if (div_id >= priv->nb_div) { 775 panic(); 776 } 777 778 divider = &priv->div[div_id]; 779 address = priv->base + divider->offset; 780 781 mask = MASK_WIDTH_SHIFT(divider->width, divider->shift); 782 mmio_clrsetbits_32(address, mask, (value << divider->shift) & mask); 783 784 if (divider->bitrdy == DIV_NO_BIT_RDY) { 785 return 0; 786 } 787 788 timeout = timeout_init_us(CLKSRC_TIMEOUT); 789 mask = BIT(divider->bitrdy); 790 791 while ((mmio_read_32(address) & mask) == 0U) { 792 if (timeout_elapsed(timeout)) { 793 return -ETIMEDOUT; 794 } 795 } 796 797 return 0; 798 } 799 800 int _clk_stm32_gate_wait_ready(struct stm32_clk_priv *priv, uint16_t gate_id, 801 bool ready_on) 802 { 803 const struct gate_cfg *gate = &priv->gates[gate_id]; 804 uintptr_t address = priv->base + gate->offset; 805 uint32_t mask_rdy = BIT(gate->bit_idx); 806 uint64_t timeout; 807 uint32_t mask_test; 808 809 if (ready_on) { 810 mask_test = BIT(gate->bit_idx); 811 } else { 812 mask_test = 0U; 813 } 814 815 timeout = timeout_init_us(OSCRDY_TIMEOUT); 816 817 while ((mmio_read_32(address) & mask_rdy) != mask_test) { 818 if (timeout_elapsed(timeout)) { 819 break; 820 } 821 } 822 823 if ((mmio_read_32(address) & mask_rdy) != mask_test) { 824 return -ETIMEDOUT; 825 } 826 827 return 0; 828 } 829 830 int clk_stm32_gate_enable(struct stm32_clk_priv *priv, int id) 831 { 832 const struct clk_stm32 *clk = _clk_get(priv, id); 833 struct clk_stm32_gate_cfg *cfg = clk->clock_cfg; 834 const struct gate_cfg *gate = &priv->gates[cfg->id]; 835 uintptr_t addr = priv->base + gate->offset; 836 837 if (gate->set_clr != 0U) { 838 mmio_write_32(addr, BIT(gate->bit_idx)); 839 840 } else { 841 mmio_setbits_32(addr, BIT(gate->bit_idx)); 842 } 843 844 return 0; 845 } 846 847 void clk_stm32_gate_disable(struct stm32_clk_priv *priv, int id) 848 { 849 const struct clk_stm32 *clk = _clk_get(priv, id); 850 struct clk_stm32_gate_cfg *cfg = clk->clock_cfg; 851 const struct gate_cfg *gate = &priv->gates[cfg->id]; 852 uintptr_t addr = priv->base + gate->offset; 853 854 if (gate->set_clr != 0U) { 855 mmio_write_32(addr + RCC_MP_ENCLRR_OFFSET, BIT(gate->bit_idx)); 856 } else { 857 mmio_clrbits_32(addr, BIT(gate->bit_idx)); 858 } 859 } 860 861 bool _clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int gate_id) 862 { 863 const struct gate_cfg *gate; 864 uint32_t addr; 865 866 gate = &priv->gates[gate_id]; 867 addr = priv->base + gate->offset; 868 869 return ((mmio_read_32(addr) & BIT(gate->bit_idx)) != 0U); 870 } 871 872 bool clk_stm32_gate_is_enabled(struct stm32_clk_priv *priv, int id) 873 { 874 const struct clk_stm32 *clk = _clk_get(priv, id); 875 struct clk_stm32_gate_cfg *cfg = clk->clock_cfg; 876 877 return _clk_stm32_gate_is_enabled(priv, cfg->id); 878 } 879 880 const struct stm32_clk_ops clk_stm32_gate_ops = { 881 .enable = clk_stm32_gate_enable, 882 .disable = clk_stm32_gate_disable, 883 .is_enabled = clk_stm32_gate_is_enabled, 884 }; 885 886 const struct stm32_clk_ops clk_fixed_factor_ops = { 887 .recalc_rate = fixed_factor_recalc_rate, 888 }; 889 890 unsigned long fixed_factor_recalc_rate(struct stm32_clk_priv *priv, 891 int id, unsigned long prate) 892 { 893 const struct clk_stm32 *clk = _clk_get(priv, id); 894 const struct fixed_factor_cfg *cfg = clk->clock_cfg; 895 unsigned long long rate; 896 897 rate = (unsigned long long)prate * cfg->mult; 898 899 if (cfg->div == 0U) { 900 ERROR("division by zero\n"); 901 panic(); 902 } 903 904 return (unsigned long)(rate / cfg->div); 905 }; 906 907 #define APB_DIV_MASK GENMASK(2, 0) 908 #define TIM_PRE_MASK BIT(0) 909 910 static unsigned long timer_recalc_rate(struct stm32_clk_priv *priv, 911 int id, unsigned long prate) 912 { 913 const struct clk_stm32 *clk = _clk_get(priv, id); 914 const struct clk_timer_cfg *cfg = clk->clock_cfg; 915 uint32_t prescaler, timpre; 916 uintptr_t rcc_base = priv->base; 917 918 prescaler = mmio_read_32(rcc_base + cfg->apbdiv) & 919 APB_DIV_MASK; 920 921 timpre = mmio_read_32(rcc_base + cfg->timpre) & 922 TIM_PRE_MASK; 923 924 if (prescaler == 0U) { 925 return prate; 926 } 927 928 return prate * (timpre + 1U) * 2U; 929 }; 930 931 const struct stm32_clk_ops clk_timer_ops = { 932 .recalc_rate = timer_recalc_rate, 933 }; 934 935 static unsigned long clk_fixed_rate_recalc(struct stm32_clk_priv *priv, int id, 936 unsigned long prate) 937 { 938 const struct clk_stm32 *clk = _clk_get(priv, id); 939 struct clk_stm32_fixed_rate_cfg *cfg = clk->clock_cfg; 940 941 return cfg->rate; 942 } 943 944 const struct stm32_clk_ops clk_stm32_fixed_rate_ops = { 945 .recalc_rate = clk_fixed_rate_recalc, 946 }; 947 948 static unsigned long clk_stm32_osc_recalc_rate(struct stm32_clk_priv *priv, 949 int id, unsigned long prate) 950 { 951 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); 952 953 return osc_data->frequency; 954 }; 955 956 bool clk_stm32_osc_gate_is_enabled(struct stm32_clk_priv *priv, int id) 957 { 958 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); 959 960 return _clk_stm32_gate_is_enabled(priv, osc_data->gate_id); 961 962 } 963 964 int clk_stm32_osc_gate_enable(struct stm32_clk_priv *priv, int id) 965 { 966 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); 967 968 _clk_stm32_gate_enable(priv, osc_data->gate_id); 969 970 if (_clk_stm32_gate_wait_ready(priv, osc_data->gate_rdy_id, true) != 0U) { 971 ERROR("%s: %s (%d)\n", __func__, osc_data->name, __LINE__); 972 panic(); 973 } 974 975 return 0; 976 } 977 978 void clk_stm32_osc_gate_disable(struct stm32_clk_priv *priv, int id) 979 { 980 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); 981 982 _clk_stm32_gate_disable(priv, osc_data->gate_id); 983 984 if (_clk_stm32_gate_wait_ready(priv, osc_data->gate_rdy_id, false) != 0U) { 985 ERROR("%s: %s (%d)\n", __func__, osc_data->name, __LINE__); 986 panic(); 987 } 988 } 989 990 static unsigned long clk_stm32_get_dt_oscillator_frequency(const char *name) 991 { 992 void *fdt = NULL; 993 int node = 0; 994 int subnode = 0; 995 996 if (fdt_get_address(&fdt) == 0) { 997 panic(); 998 } 999 1000 node = fdt_path_offset(fdt, "/clocks"); 1001 if (node < 0) { 1002 return 0UL; 1003 } 1004 1005 fdt_for_each_subnode(subnode, fdt, node) { 1006 const char *cchar = NULL; 1007 const fdt32_t *cuint = NULL; 1008 int ret = 0; 1009 1010 cchar = fdt_get_name(fdt, subnode, &ret); 1011 if (cchar == NULL) { 1012 continue; 1013 } 1014 1015 if (strncmp(cchar, name, (size_t)ret) || 1016 fdt_get_status(subnode) == DT_DISABLED) { 1017 continue; 1018 } 1019 1020 cuint = fdt_getprop(fdt, subnode, "clock-frequency", &ret); 1021 if (cuint == NULL) { 1022 return 0UL; 1023 } 1024 1025 return fdt32_to_cpu(*cuint); 1026 } 1027 1028 return 0UL; 1029 } 1030 1031 void clk_stm32_osc_init(struct stm32_clk_priv *priv, int id) 1032 { 1033 struct clk_oscillator_data *osc_data = clk_oscillator_get_data(priv, id); 1034 const char *name = osc_data->name; 1035 1036 osc_data->frequency = clk_stm32_get_dt_oscillator_frequency(name); 1037 } 1038 1039 const struct stm32_clk_ops clk_stm32_osc_ops = { 1040 .recalc_rate = clk_stm32_osc_recalc_rate, 1041 .is_enabled = clk_stm32_osc_gate_is_enabled, 1042 .enable = clk_stm32_osc_gate_enable, 1043 .disable = clk_stm32_osc_gate_disable, 1044 .init = clk_stm32_osc_init, 1045 }; 1046 1047 const struct stm32_clk_ops clk_stm32_osc_nogate_ops = { 1048 .recalc_rate = clk_stm32_osc_recalc_rate, 1049 .init = clk_stm32_osc_init, 1050 }; 1051 1052 int stm32_clk_parse_fdt_by_name(void *fdt, int node, const char *name, uint32_t *tab, uint32_t *nb) 1053 { 1054 const fdt32_t *cell; 1055 int len = 0; 1056 uint32_t i; 1057 1058 cell = fdt_getprop(fdt, node, name, &len); 1059 if (cell == NULL) { 1060 *nb = 0U; 1061 return 0; 1062 } 1063 1064 for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) { 1065 uint32_t val = fdt32_to_cpu(cell[i]); 1066 1067 tab[i] = val; 1068 } 1069 1070 *nb = (uint32_t)len / sizeof(uint32_t); 1071 1072 return 0; 1073 } 1074 1075 int clk_stm32_init(struct stm32_clk_priv *priv, uintptr_t base) 1076 { 1077 unsigned int i; 1078 1079 stm32_clock_data = priv; 1080 1081 priv->base = base; 1082 1083 for (i = 0U; i < priv->num; i++) { 1084 const struct clk_stm32 *clk = _clk_get(priv, i); 1085 1086 assert(clk->ops != NULL); 1087 1088 if (clk->ops->init != NULL) { 1089 clk->ops->init(priv, i); 1090 } 1091 } 1092 1093 stm32_clk_register(); 1094 1095 return 0; 1096 } 1097