Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 | // SPDX-License-Identifier: GPL-2.0 /* * MediaTek common clock driver * * Copyright (C) 2018 MediaTek Inc. * Author: Ryder Lee <ryder.lee@mediatek.com> */ #include <clk-uclass.h> #include <div64.h> #include <dm.h> #include <asm/io.h> #include <linux/bitops.h> #include <linux/delay.h> #include "clk-mtk.h" #define REG_CON0 0 #define REG_CON1 4 #define CON0_BASE_EN BIT(0) #define CON0_PWR_ON BIT(0) #define CON0_ISO_EN BIT(1) #define CON1_PCW_CHG BIT(31) #define POSTDIV_MASK 0x7 #define INTEGER_BITS 7 /* scpsys clock off control */ #define CLK_SCP_CFG0 0x200 #define CLK_SCP_CFG1 0x204 #define SCP_ARMCK_OFF_EN GENMASK(9, 0) #define SCP_AXICK_DCM_DIS_EN BIT(0) #define SCP_AXICK_26M_SEL_EN BIT(4) /* shared functions */ static int mtk_clk_get_id(struct clk *clk) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); int id = clk->id; /* Remap the clk ID to the one expected by driver */ if (priv->tree->id_offs_map) id = priv->tree->id_offs_map[id]; return id; } static int mtk_dummy_enable(struct clk *clk) { return 0; } static int mtk_gate_enable(void __iomem *base, const struct mtk_gate *gate) { u32 bit = BIT(gate->shift); switch (gate->flags & CLK_GATE_MASK) { case CLK_GATE_SETCLR: writel(bit, base + gate->regs->clr_ofs); break; case CLK_GATE_SETCLR_INV: writel(bit, base + gate->regs->set_ofs); break; case CLK_GATE_NO_SETCLR: clrsetbits_le32(base + gate->regs->sta_ofs, bit, 0); break; case CLK_GATE_NO_SETCLR_INV: clrsetbits_le32(base + gate->regs->sta_ofs, bit, bit); break; default: return -EINVAL; } return 0; } static int mtk_gate_disable(void __iomem *base, const struct mtk_gate *gate) { u32 bit = BIT(gate->shift); switch (gate->flags & CLK_GATE_MASK) { case CLK_GATE_SETCLR: writel(bit, base + gate->regs->set_ofs); break; case CLK_GATE_SETCLR_INV: writel(bit, base + gate->regs->clr_ofs); break; case CLK_GATE_NO_SETCLR: clrsetbits_le32(base + gate->regs->sta_ofs, bit, bit); break; case CLK_GATE_NO_SETCLR_INV: clrsetbits_le32(base + gate->regs->sta_ofs, bit, 0); break; default: return -EINVAL; } return 0; } /* * In case the rate change propagation to parent clocks is undesirable, * this function is recursively called to find the parent to calculate * the accurate frequency. */ static ulong mtk_clk_find_parent_rate(struct clk *clk, int id, struct udevice *pdev) { struct clk parent = { .id = id, }; if (pdev) parent.dev = pdev; else parent.dev = clk->dev; return clk_get_rate(&parent); } static int mtk_clk_mux_set_parent(void __iomem *base, u32 parent, u32 parent_type, const struct mtk_composite *mux) { u32 val, index = 0; if (mux->flags & CLK_PARENT_MIXED) { /* * Assume parent_type in clk_tree to be always set with * CLK_PARENT_MIXED implementation. If it's not, assume * not parent clk ID clash is possible. */ while (mux->parent_flags[index].id != parent || (parent_type && (mux->parent_flags[index].flags & CLK_PARENT_MASK) != parent_type)) if (++index == mux->num_parents) return -EINVAL; } else { while (mux->parent[index] != parent) if (++index == mux->num_parents) return -EINVAL; } if (mux->flags & CLK_MUX_SETCLR_UPD) { val = (mux->mux_mask << mux->mux_shift); writel(val, base + mux->mux_clr_reg); val = (index << mux->mux_shift); writel(val, base + mux->mux_set_reg); if (mux->upd_shift >= 0) writel(BIT(mux->upd_shift), base + mux->upd_reg); } else { /* switch mux to a select parent */ val = readl(base + mux->mux_reg); val &= ~(mux->mux_mask << mux->mux_shift); val |= index << mux->mux_shift; writel(val, base + mux->mux_reg); } return 0; } /* apmixedsys functions */ static unsigned long __mtk_pll_recalc_rate(const struct mtk_pll_data *pll, u32 fin, u32 pcw, int postdiv) { int pcwbits = pll->pcwbits; int pcwfbits; int ibits; u64 vco; u8 c = 0; /* The fractional part of the PLL divider. */ ibits = pll->pcwibits ? pll->pcwibits : INTEGER_BITS; pcwfbits = pcwbits > ibits ? pcwbits - ibits : 0; vco = (u64)fin * pcw; if (pcwfbits && (vco & GENMASK(pcwfbits - 1, 0))) c = 1; vco >>= pcwfbits; if (c) vco++; return ((unsigned long)vco + postdiv - 1) / postdiv; } /** * MediaTek PLLs are configured through their pcw value. The pcw value * describes a divider in the PLL feedback loop which consists of 7 bits * for the integer part and the remaining bits (if present) for the * fractional part. Also they have a 3 bit power-of-two post divider. */ static void mtk_pll_set_rate_regs(struct mtk_clk_priv *priv, u32 id, u32 pcw, int postdiv) { const struct mtk_pll_data *pll; u32 val, chg; pll = &priv->tree->plls[id]; /* set postdiv */ val = readl(priv->base + pll->pd_reg); val &= ~(POSTDIV_MASK << pll->pd_shift); val |= (ffs(postdiv) - 1) << pll->pd_shift; /* postdiv and pcw need to set at the same time if on same register */ if (pll->pd_reg != pll->pcw_reg) { writel(val, priv->base + pll->pd_reg); val = readl(priv->base + pll->pcw_reg); } /* set pcw */ val &= ~GENMASK(pll->pcw_shift + pll->pcwbits - 1, pll->pcw_shift); val |= pcw << pll->pcw_shift; if (pll->pcw_chg_reg) { chg = readl(priv->base + pll->pcw_chg_reg); chg |= CON1_PCW_CHG; writel(val, priv->base + pll->pcw_reg); writel(chg, priv->base + pll->pcw_chg_reg); } else { val |= CON1_PCW_CHG; writel(val, priv->base + pll->pcw_reg); } udelay(20); } /** * mtk_pll_calc_values - calculate good values for a given input frequency. * @priv: The mtk priv struct * @id: The clk id * @pcw: The pcw value (output) * @postdiv: The post divider (output) * @freq: The desired target frequency */ static void mtk_pll_calc_values(struct mtk_clk_priv *priv, u32 id, u32 *pcw, u32 *postdiv, u32 freq) { const struct mtk_pll_data *pll; unsigned long fmin; u64 _pcw; int ibits; u32 val; pll = &priv->tree->plls[id]; fmin = pll->fmin ? pll->fmin : 1000 * MHZ; if (freq > pll->fmax) freq = pll->fmax; for (val = 0; val < 5; val++) { *postdiv = 1 << val; if ((u64)freq * *postdiv >= fmin) break; } /* _pcw = freq * postdiv / xtal_rate * 2^pcwfbits */ ibits = pll->pcwibits ? pll->pcwibits : INTEGER_BITS; _pcw = ((u64)freq << val) << (pll->pcwbits - ibits); do_div(_pcw, priv->tree->xtal2_rate); *pcw = (u32)_pcw; } static ulong mtk_apmixedsys_set_rate(struct clk *clk, ulong rate) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); int id = mtk_clk_get_id(clk); u32 pcw = 0; u32 postdiv; if (priv->tree->gates && id >= priv->tree->gates_offs) return -EINVAL; mtk_pll_calc_values(priv, id, &pcw, &postdiv, rate); mtk_pll_set_rate_regs(priv, id, pcw, postdiv); return 0; } static ulong mtk_apmixedsys_get_rate(struct clk *clk) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); const struct mtk_pll_data *pll; int id = mtk_clk_get_id(clk); const struct mtk_gate *gate; u32 postdiv; u32 pcw; /* GATE handling */ if (priv->tree->gates && id >= priv->tree->gates_offs) { gate = &priv->tree->gates[id - priv->tree->gates_offs]; return mtk_clk_find_parent_rate(clk, gate->parent, NULL); } pll = &priv->tree->plls[id]; postdiv = (readl(priv->base + pll->pd_reg) >> pll->pd_shift) & POSTDIV_MASK; postdiv = 1 << postdiv; pcw = readl(priv->base + pll->pcw_reg) >> pll->pcw_shift; pcw &= GENMASK(pll->pcwbits - 1, 0); return __mtk_pll_recalc_rate(pll, priv->tree->xtal2_rate, pcw, postdiv); } static int mtk_apmixedsys_enable(struct clk *clk) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); const struct mtk_pll_data *pll; int id = mtk_clk_get_id(clk); const struct mtk_gate *gate; u32 r; /* GATE handling */ if (priv->tree->gates && id >= priv->tree->gates_offs) { gate = &priv->tree->gates[id - priv->tree->gates_offs]; return mtk_gate_enable(priv->base, gate); } pll = &priv->tree->plls[id]; r = readl(priv->base + pll->pwr_reg) | CON0_PWR_ON; writel(r, priv->base + pll->pwr_reg); udelay(1); r = readl(priv->base + pll->pwr_reg) & ~CON0_ISO_EN; writel(r, priv->base + pll->pwr_reg); udelay(1); r = readl(priv->base + pll->reg + REG_CON0); r |= pll->en_mask; writel(r, priv->base + pll->reg + REG_CON0); udelay(20); if (pll->flags & HAVE_RST_BAR) { r = readl(priv->base + pll->reg + REG_CON0); r |= pll->rst_bar_mask; writel(r, priv->base + pll->reg + REG_CON0); } return 0; } static int mtk_apmixedsys_disable(struct clk *clk) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); const struct mtk_pll_data *pll; int id = mtk_clk_get_id(clk); const struct mtk_gate *gate; u32 r; /* GATE handling */ if (priv->tree->gates && id >= priv->tree->gates_offs) { gate = &priv->tree->gates[id - priv->tree->gates_offs]; return mtk_gate_disable(priv->base, gate); } pll = &priv->tree->plls[id]; if (pll->flags & HAVE_RST_BAR) { r = readl(priv->base + pll->reg + REG_CON0); r &= ~pll->rst_bar_mask; writel(r, priv->base + pll->reg + REG_CON0); } r = readl(priv->base + pll->reg + REG_CON0); r &= ~CON0_BASE_EN; writel(r, priv->base + pll->reg + REG_CON0); r = readl(priv->base + pll->pwr_reg) | CON0_ISO_EN; writel(r, priv->base + pll->pwr_reg); r = readl(priv->base + pll->pwr_reg) & ~CON0_PWR_ON; writel(r, priv->base + pll->pwr_reg); return 0; } /* topckgen functions */ static ulong mtk_factor_recalc_rate(const struct mtk_fixed_factor *fdiv, ulong parent_rate) { u64 rate = parent_rate * fdiv->mult; do_div(rate, fdiv->div); return rate; } static ulong mtk_topckgen_get_factor_rate(struct clk *clk, u32 off) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); const struct mtk_fixed_factor *fdiv = &priv->tree->fdivs[off]; ulong rate; switch (fdiv->flags & CLK_PARENT_MASK) { case CLK_PARENT_APMIXED: rate = mtk_clk_find_parent_rate(clk, fdiv->parent, priv->parent); break; case CLK_PARENT_TOPCKGEN: rate = mtk_clk_find_parent_rate(clk, fdiv->parent, NULL); break; case CLK_PARENT_XTAL: default: rate = priv->tree->xtal_rate; } return mtk_factor_recalc_rate(fdiv, rate); } static ulong mtk_infrasys_get_factor_rate(struct clk *clk, u32 off) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); const struct mtk_fixed_factor *fdiv = &priv->tree->fdivs[off]; ulong rate; switch (fdiv->flags & CLK_PARENT_MASK) { case CLK_PARENT_TOPCKGEN: rate = mtk_clk_find_parent_rate(clk, fdiv->parent, priv->parent); break; case CLK_PARENT_XTAL: rate = priv->tree->xtal_rate; break; default: rate = mtk_clk_find_parent_rate(clk, fdiv->parent, NULL); } return mtk_factor_recalc_rate(fdiv, rate); } static ulong mtk_topckgen_find_parent_rate(struct mtk_clk_priv *priv, struct clk *clk, const int parent, u16 flags) { switch (flags & CLK_PARENT_MASK) { case CLK_PARENT_XTAL: return priv->tree->xtal_rate; case CLK_PARENT_APMIXED: return mtk_clk_find_parent_rate(clk, parent, priv->parent); default: return mtk_clk_find_parent_rate(clk, parent, NULL); } } static ulong mtk_topckgen_get_mux_rate(struct clk *clk, u32 off) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); const struct mtk_composite *mux = &priv->tree->muxes[off]; u32 index; index = readl(priv->base + mux->mux_reg); index &= mux->mux_mask << mux->mux_shift; index = index >> mux->mux_shift; /* * Parents can be either from APMIXED or TOPCKGEN, * inspect the mtk_parent struct to check the source */ if (mux->flags & CLK_PARENT_MIXED) { const struct mtk_parent *parent = &mux->parent_flags[index]; return mtk_topckgen_find_parent_rate(priv, clk, parent->id, parent->flags); } if (mux->parent[index] == CLK_XTAL && !(priv->tree->flags & CLK_BYPASS_XTAL)) return priv->tree->xtal_rate; return mtk_topckgen_find_parent_rate(priv, clk, mux->parent[index], mux->flags); } static ulong mtk_find_parent_rate(struct mtk_clk_priv *priv, struct clk *clk, const int parent, u16 flags) { switch (flags & CLK_PARENT_MASK) { case CLK_PARENT_XTAL: return priv->tree->xtal_rate; /* Assume the second level parent is always APMIXED */ case CLK_PARENT_APMIXED: priv = dev_get_priv(priv->parent); fallthrough; case CLK_PARENT_TOPCKGEN: return mtk_clk_find_parent_rate(clk, parent, priv->parent); default: return mtk_clk_find_parent_rate(clk, parent, NULL); } } static ulong mtk_infrasys_get_mux_rate(struct clk *clk, u32 off) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); const struct mtk_composite *mux = &priv->tree->muxes[off]; u32 index; index = readl(priv->base + mux->mux_reg); index &= mux->mux_mask << mux->mux_shift; index = index >> mux->mux_shift; /* * Parents can be either from TOPCKGEN or INFRACFG, * inspect the mtk_parent struct to check the source */ if (mux->flags & CLK_PARENT_MIXED) { const struct mtk_parent *parent = &mux->parent_flags[index]; return mtk_find_parent_rate(priv, clk, parent->id, parent->flags); } if (mux->parent[index] == CLK_XTAL && !(priv->tree->flags & CLK_BYPASS_XTAL)) return priv->tree->xtal_rate; return mtk_find_parent_rate(priv, clk, mux->parent[index], mux->flags); } static ulong mtk_topckgen_get_rate(struct clk *clk) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); int id = mtk_clk_get_id(clk); if (id < priv->tree->fdivs_offs) return priv->tree->fclks[id].rate; else if (id < priv->tree->muxes_offs) return mtk_topckgen_get_factor_rate(clk, id - priv->tree->fdivs_offs); else return mtk_topckgen_get_mux_rate(clk, id - priv->tree->muxes_offs); } static ulong mtk_infrasys_get_rate(struct clk *clk) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); int id = mtk_clk_get_id(clk); ulong rate; if (id < priv->tree->fdivs_offs) { rate = priv->tree->fclks[id].rate; } else if (id < priv->tree->muxes_offs) { rate = mtk_infrasys_get_factor_rate(clk, id - priv->tree->fdivs_offs); /* No gates defined or ID is a MUX */ } else if (!priv->tree->gates || id < priv->tree->gates_offs) { rate = mtk_infrasys_get_mux_rate(clk, id - priv->tree->muxes_offs); /* Only valid with muxes + gates implementation */ } else { struct udevice *parent = NULL; const struct mtk_gate *gate; gate = &priv->tree->gates[id - priv->tree->gates_offs]; if (gate->flags & CLK_PARENT_TOPCKGEN) parent = priv->parent; /* * Assume xtal_rate to be declared if some gates have * XTAL as parent */ else if (gate->flags & CLK_PARENT_XTAL) return priv->tree->xtal_rate; rate = mtk_clk_find_parent_rate(clk, gate->parent, parent); } return rate; } static int mtk_clk_mux_enable(struct clk *clk) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); const struct mtk_composite *mux; int id = mtk_clk_get_id(clk); u32 val; if (id < priv->tree->muxes_offs) return 0; mux = &priv->tree->muxes[id - priv->tree->muxes_offs]; if (mux->gate_shift < 0) return 0; /* enable clock gate */ if (mux->flags & CLK_MUX_SETCLR_UPD) { val = BIT(mux->gate_shift); writel(val, priv->base + mux->mux_clr_reg); } else { val = readl(priv->base + mux->gate_reg); val &= ~BIT(mux->gate_shift); writel(val, priv->base + mux->gate_reg); } if (mux->flags & CLK_DOMAIN_SCPSYS) { /* enable scpsys clock off control */ writel(SCP_ARMCK_OFF_EN, priv->base + CLK_SCP_CFG0); writel(SCP_AXICK_DCM_DIS_EN | SCP_AXICK_26M_SEL_EN, priv->base + CLK_SCP_CFG1); } return 0; } static int mtk_clk_mux_disable(struct clk *clk) { struct mtk_clk_priv *priv = dev_get_priv(clk->dev); const struct mtk_composite *mux; int id = mtk_clk_get_id(clk); u32 val; if (id < priv->tree->muxes_offs) return 0; mux = &priv->tree->muxes[id - priv->tree->muxes_offs]; if (mux->gate_shift < 0) return 0; /* disable clock gate */ if (mux->flags & CLK_MUX_SETCLR_UPD) { val = BIT(mux->gate_shift); writel(val, priv->base + mux->mux_set_reg); } else { val = readl(priv->base + mux->gate_reg); val |= BIT(mux->gate_shift); writel(val, priv->base + mux->gate_reg); } return 0; } static int mtk_common_clk_set_parent(struct clk *clk, struct clk *parent) { struct mtk_clk_priv *parent_priv = dev_get_priv(parent->dev); struct mtk_clk_priv *priv = dev_get_priv(clk->dev); int id = mtk_clk_get_id(clk); u32 parent_type; if (id < priv->tree->muxes_offs) return 0; if (!parent_priv) return 0; parent_type = parent_priv->tree->flags & CLK_PARENT_MASK; return mtk_clk_mux_set_parent(priv->base, parent->id, parent_type, &priv->tree->muxes[id - priv->tree->muxes_offs]); } /* CG functions */ static int mtk_clk_gate_enable(struct clk *clk) { struct mtk_cg_priv *priv = dev_get_priv(clk->dev); int id = mtk_clk_get_id(clk); const struct mtk_gate *gate; if (id < priv->tree->gates_offs) return -EINVAL; gate = &priv->gates[id - priv->tree->gates_offs]; return mtk_gate_enable(priv->base, gate); } static int mtk_clk_infrasys_enable(struct clk *clk) { struct mtk_cg_priv *priv = dev_get_priv(clk->dev); int id = mtk_clk_get_id(clk); const struct mtk_gate *gate; /* MUX handling */ if (!priv->tree->gates || id < priv->tree->gates_offs) return mtk_clk_mux_enable(clk); gate = &priv->tree->gates[id - priv->tree->gates_offs]; return mtk_gate_enable(priv->base, gate); } static int mtk_clk_gate_disable(struct clk *clk) { struct mtk_cg_priv *priv = dev_get_priv(clk->dev); int id = mtk_clk_get_id(clk); const struct mtk_gate *gate; if (id < priv->tree->gates_offs) return -EINVAL; gate = &priv->gates[id - priv->tree->gates_offs]; return mtk_gate_disable(priv->base, gate); } static int mtk_clk_infrasys_disable(struct clk *clk) { struct mtk_cg_priv *priv = dev_get_priv(clk->dev); int id = mtk_clk_get_id(clk); const struct mtk_gate *gate; /* MUX handling */ if (!priv->tree->gates || id < priv->tree->gates_offs) return mtk_clk_mux_disable(clk); gate = &priv->tree->gates[id - priv->tree->gates_offs]; return mtk_gate_disable(priv->base, gate); } static ulong mtk_clk_gate_get_rate(struct clk *clk) { struct mtk_cg_priv *priv = dev_get_priv(clk->dev); struct udevice *parent = priv->parent; int id = mtk_clk_get_id(clk); const struct mtk_gate *gate; if (id < priv->tree->gates_offs) return -EINVAL; gate = &priv->gates[id - priv->tree->gates_offs]; /* * With requesting a TOPCKGEN parent, make sure the dev parent * is actually topckgen. This might not be the case for an * infracfg-ao implementation where: * parent = infracfg * parent->parent = topckgen */ if (gate->flags & CLK_PARENT_TOPCKGEN && parent->driver != DM_DRIVER_GET(mtk_clk_topckgen)) { priv = dev_get_priv(parent); parent = priv->parent; /* * Assume xtal_rate to be declared if some gates have * XTAL as parent */ } else if (gate->flags & CLK_PARENT_XTAL) { return priv->tree->xtal_rate; } return mtk_clk_find_parent_rate(clk, gate->parent, parent); } const struct clk_ops mtk_clk_apmixedsys_ops = { .enable = mtk_apmixedsys_enable, .disable = mtk_apmixedsys_disable, .set_rate = mtk_apmixedsys_set_rate, .get_rate = mtk_apmixedsys_get_rate, }; const struct clk_ops mtk_clk_fixed_pll_ops = { .enable = mtk_dummy_enable, .disable = mtk_dummy_enable, .get_rate = mtk_topckgen_get_rate, }; const struct clk_ops mtk_clk_topckgen_ops = { .enable = mtk_clk_mux_enable, .disable = mtk_clk_mux_disable, .get_rate = mtk_topckgen_get_rate, .set_parent = mtk_common_clk_set_parent, }; const struct clk_ops mtk_clk_infrasys_ops = { .enable = mtk_clk_infrasys_enable, .disable = mtk_clk_infrasys_disable, .get_rate = mtk_infrasys_get_rate, .set_parent = mtk_common_clk_set_parent, }; const struct clk_ops mtk_clk_gate_ops = { .enable = mtk_clk_gate_enable, .disable = mtk_clk_gate_disable, .get_rate = mtk_clk_gate_get_rate, }; static int mtk_common_clk_init_drv(struct udevice *dev, const struct mtk_clk_tree *tree, const struct driver *drv) { struct mtk_clk_priv *priv = dev_get_priv(dev); struct udevice *parent; int ret; priv->base = dev_read_addr_ptr(dev); if (!priv->base) return -ENOENT; ret = uclass_get_device_by_phandle(UCLASS_CLK, dev, "clock-parent", &parent); if (ret || !parent) { ret = uclass_get_device_by_driver(UCLASS_CLK, drv, &parent); if (ret || !parent) return -ENOENT; } priv->parent = parent; priv->tree = tree; return 0; } int mtk_common_clk_init(struct udevice *dev, const struct mtk_clk_tree *tree) { return mtk_common_clk_init_drv(dev, tree, DM_DRIVER_GET(mtk_clk_apmixedsys)); } int mtk_common_clk_infrasys_init(struct udevice *dev, const struct mtk_clk_tree *tree) { return mtk_common_clk_init_drv(dev, tree, DM_DRIVER_GET(mtk_clk_topckgen)); } int mtk_common_clk_gate_init(struct udevice *dev, const struct mtk_clk_tree *tree, const struct mtk_gate *gates) { struct mtk_cg_priv *priv = dev_get_priv(dev); struct udevice *parent; int ret; priv->base = dev_read_addr_ptr(dev); if (!priv->base) return -ENOENT; ret = uclass_get_device_by_phandle(UCLASS_CLK, dev, "clock-parent", &parent); if (ret || !parent) { ret = uclass_get_device_by_driver(UCLASS_CLK, DM_DRIVER_GET(mtk_clk_topckgen), &parent); if (ret || !parent) return -ENOENT; } priv->parent = parent; priv->tree = tree; priv->gates = gates; return 0; } |