developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * MediaTek common clock driver |
| 4 | * |
| 5 | * Copyright (C) 2018 MediaTek Inc. |
| 6 | * Author: Ryder Lee <ryder.lee@mediatek.com> |
| 7 | */ |
| 8 | |
Tom Rini | abb9a04 | 2024-05-18 20:20:43 -0600 | [diff] [blame] | 9 | #include <common.h> |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 10 | #include <clk-uclass.h> |
| 11 | #include <div64.h> |
| 12 | #include <dm.h> |
| 13 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 14 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 16 | |
| 17 | #include "clk-mtk.h" |
| 18 | |
| 19 | #define REG_CON0 0 |
| 20 | #define REG_CON1 4 |
| 21 | |
| 22 | #define CON0_BASE_EN BIT(0) |
| 23 | #define CON0_PWR_ON BIT(0) |
| 24 | #define CON0_ISO_EN BIT(1) |
| 25 | #define CON1_PCW_CHG BIT(31) |
| 26 | |
| 27 | #define POSTDIV_MASK 0x7 |
| 28 | #define INTEGER_BITS 7 |
| 29 | |
| 30 | /* scpsys clock off control */ |
| 31 | #define CLK_SCP_CFG0 0x200 |
| 32 | #define CLK_SCP_CFG1 0x204 |
| 33 | #define SCP_ARMCK_OFF_EN GENMASK(9, 0) |
| 34 | #define SCP_AXICK_DCM_DIS_EN BIT(0) |
| 35 | #define SCP_AXICK_26M_SEL_EN BIT(4) |
| 36 | |
| 37 | /* shared functions */ |
| 38 | |
| 39 | /* |
| 40 | * In case the rate change propagation to parent clocks is undesirable, |
| 41 | * this function is recursively called to find the parent to calculate |
| 42 | * the accurate frequency. |
| 43 | */ |
developer | 65da8e7 | 2020-01-10 16:30:30 +0800 | [diff] [blame] | 44 | static ulong mtk_clk_find_parent_rate(struct clk *clk, int id, |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 45 | struct udevice *pdev) |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 46 | { |
| 47 | struct clk parent = { .id = id, }; |
| 48 | |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 49 | if (pdev) |
| 50 | parent.dev = pdev; |
| 51 | else |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 52 | parent.dev = clk->dev; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 53 | |
| 54 | return clk_get_rate(&parent); |
| 55 | } |
| 56 | |
| 57 | static int mtk_clk_mux_set_parent(void __iomem *base, u32 parent, |
| 58 | const struct mtk_composite *mux) |
| 59 | { |
| 60 | u32 val, index = 0; |
| 61 | |
| 62 | while (mux->parent[index] != parent) |
| 63 | if (++index == mux->num_parents) |
| 64 | return -EINVAL; |
| 65 | |
developer | ba560c7 | 2019-12-31 11:29:21 +0800 | [diff] [blame] | 66 | if (mux->flags & CLK_MUX_SETCLR_UPD) { |
| 67 | val = (mux->mux_mask << mux->mux_shift); |
| 68 | writel(val, base + mux->mux_clr_reg); |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 69 | |
developer | ba560c7 | 2019-12-31 11:29:21 +0800 | [diff] [blame] | 70 | val = (index << mux->mux_shift); |
| 71 | writel(val, base + mux->mux_set_reg); |
| 72 | |
| 73 | if (mux->upd_shift >= 0) |
| 74 | writel(BIT(mux->upd_shift), base + mux->upd_reg); |
| 75 | } else { |
| 76 | /* switch mux to a select parent */ |
| 77 | val = readl(base + mux->mux_reg); |
| 78 | val &= ~(mux->mux_mask << mux->mux_shift); |
| 79 | |
| 80 | val |= index << mux->mux_shift; |
| 81 | writel(val, base + mux->mux_reg); |
| 82 | } |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /* apmixedsys functions */ |
| 88 | |
| 89 | static unsigned long __mtk_pll_recalc_rate(const struct mtk_pll_data *pll, |
| 90 | u32 fin, u32 pcw, int postdiv) |
| 91 | { |
| 92 | int pcwbits = pll->pcwbits; |
| 93 | int pcwfbits; |
developer | 0b5e5f1 | 2019-12-31 11:29:22 +0800 | [diff] [blame] | 94 | int ibits; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 95 | u64 vco; |
| 96 | u8 c = 0; |
| 97 | |
| 98 | /* The fractional part of the PLL divider. */ |
developer | 0b5e5f1 | 2019-12-31 11:29:22 +0800 | [diff] [blame] | 99 | ibits = pll->pcwibits ? pll->pcwibits : INTEGER_BITS; |
| 100 | pcwfbits = pcwbits > ibits ? pcwbits - ibits : 0; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 101 | |
| 102 | vco = (u64)fin * pcw; |
| 103 | |
| 104 | if (pcwfbits && (vco & GENMASK(pcwfbits - 1, 0))) |
| 105 | c = 1; |
| 106 | |
| 107 | vco >>= pcwfbits; |
| 108 | |
| 109 | if (c) |
| 110 | vco++; |
| 111 | |
| 112 | return ((unsigned long)vco + postdiv - 1) / postdiv; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * MediaTek PLLs are configured through their pcw value. The pcw value |
| 117 | * describes a divider in the PLL feedback loop which consists of 7 bits |
| 118 | * for the integer part and the remaining bits (if present) for the |
| 119 | * fractional part. Also they have a 3 bit power-of-two post divider. |
| 120 | */ |
| 121 | static void mtk_pll_set_rate_regs(struct clk *clk, u32 pcw, int postdiv) |
| 122 | { |
| 123 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 124 | const struct mtk_pll_data *pll = &priv->tree->plls[clk->id]; |
developer | 0b5e5f1 | 2019-12-31 11:29:22 +0800 | [diff] [blame] | 125 | u32 val, chg; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 126 | |
| 127 | /* set postdiv */ |
| 128 | val = readl(priv->base + pll->pd_reg); |
| 129 | val &= ~(POSTDIV_MASK << pll->pd_shift); |
| 130 | val |= (ffs(postdiv) - 1) << pll->pd_shift; |
| 131 | |
| 132 | /* postdiv and pcw need to set at the same time if on same register */ |
| 133 | if (pll->pd_reg != pll->pcw_reg) { |
| 134 | writel(val, priv->base + pll->pd_reg); |
| 135 | val = readl(priv->base + pll->pcw_reg); |
| 136 | } |
| 137 | |
| 138 | /* set pcw */ |
| 139 | val &= ~GENMASK(pll->pcw_shift + pll->pcwbits - 1, pll->pcw_shift); |
| 140 | val |= pcw << pll->pcw_shift; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 141 | |
developer | 0b5e5f1 | 2019-12-31 11:29:22 +0800 | [diff] [blame] | 142 | if (pll->pcw_chg_reg) { |
| 143 | chg = readl(priv->base + pll->pcw_chg_reg); |
| 144 | chg |= CON1_PCW_CHG; |
| 145 | writel(val, priv->base + pll->pcw_reg); |
| 146 | writel(chg, priv->base + pll->pcw_chg_reg); |
| 147 | } else { |
| 148 | val |= CON1_PCW_CHG; |
| 149 | writel(val, priv->base + pll->pcw_reg); |
| 150 | } |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 151 | |
| 152 | udelay(20); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * mtk_pll_calc_values - calculate good values for a given input frequency. |
| 157 | * @clk: The clk |
| 158 | * @pcw: The pcw value (output) |
| 159 | * @postdiv: The post divider (output) |
| 160 | * @freq: The desired target frequency |
| 161 | */ |
| 162 | static void mtk_pll_calc_values(struct clk *clk, u32 *pcw, u32 *postdiv, |
| 163 | u32 freq) |
| 164 | { |
| 165 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 166 | const struct mtk_pll_data *pll = &priv->tree->plls[clk->id]; |
developer | 0b5e5f1 | 2019-12-31 11:29:22 +0800 | [diff] [blame] | 167 | unsigned long fmin = pll->fmin ? pll->fmin : 1000 * MHZ; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 168 | u64 _pcw; |
developer | 0b5e5f1 | 2019-12-31 11:29:22 +0800 | [diff] [blame] | 169 | int ibits; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 170 | u32 val; |
| 171 | |
| 172 | if (freq > pll->fmax) |
| 173 | freq = pll->fmax; |
| 174 | |
| 175 | for (val = 0; val < 5; val++) { |
| 176 | *postdiv = 1 << val; |
| 177 | if ((u64)freq * *postdiv >= fmin) |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | /* _pcw = freq * postdiv / xtal_rate * 2^pcwfbits */ |
developer | 0b5e5f1 | 2019-12-31 11:29:22 +0800 | [diff] [blame] | 182 | ibits = pll->pcwibits ? pll->pcwibits : INTEGER_BITS; |
| 183 | _pcw = ((u64)freq << val) << (pll->pcwbits - ibits); |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 184 | do_div(_pcw, priv->tree->xtal2_rate); |
| 185 | |
| 186 | *pcw = (u32)_pcw; |
| 187 | } |
| 188 | |
| 189 | static ulong mtk_apmixedsys_set_rate(struct clk *clk, ulong rate) |
| 190 | { |
| 191 | u32 pcw = 0; |
| 192 | u32 postdiv; |
| 193 | |
| 194 | mtk_pll_calc_values(clk, &pcw, &postdiv, rate); |
| 195 | mtk_pll_set_rate_regs(clk, pcw, postdiv); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static ulong mtk_apmixedsys_get_rate(struct clk *clk) |
| 201 | { |
| 202 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 203 | const struct mtk_pll_data *pll = &priv->tree->plls[clk->id]; |
| 204 | u32 postdiv; |
| 205 | u32 pcw; |
| 206 | |
| 207 | postdiv = (readl(priv->base + pll->pd_reg) >> pll->pd_shift) & |
| 208 | POSTDIV_MASK; |
| 209 | postdiv = 1 << postdiv; |
| 210 | |
| 211 | pcw = readl(priv->base + pll->pcw_reg) >> pll->pcw_shift; |
| 212 | pcw &= GENMASK(pll->pcwbits - 1, 0); |
| 213 | |
| 214 | return __mtk_pll_recalc_rate(pll, priv->tree->xtal2_rate, |
| 215 | pcw, postdiv); |
| 216 | } |
| 217 | |
| 218 | static int mtk_apmixedsys_enable(struct clk *clk) |
| 219 | { |
| 220 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 221 | const struct mtk_pll_data *pll = &priv->tree->plls[clk->id]; |
| 222 | u32 r; |
| 223 | |
| 224 | r = readl(priv->base + pll->pwr_reg) | CON0_PWR_ON; |
| 225 | writel(r, priv->base + pll->pwr_reg); |
| 226 | udelay(1); |
| 227 | |
| 228 | r = readl(priv->base + pll->pwr_reg) & ~CON0_ISO_EN; |
| 229 | writel(r, priv->base + pll->pwr_reg); |
| 230 | udelay(1); |
| 231 | |
| 232 | r = readl(priv->base + pll->reg + REG_CON0); |
| 233 | r |= pll->en_mask; |
| 234 | writel(r, priv->base + pll->reg + REG_CON0); |
| 235 | |
| 236 | udelay(20); |
| 237 | |
| 238 | if (pll->flags & HAVE_RST_BAR) { |
| 239 | r = readl(priv->base + pll->reg + REG_CON0); |
| 240 | r |= pll->rst_bar_mask; |
| 241 | writel(r, priv->base + pll->reg + REG_CON0); |
| 242 | } |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static int mtk_apmixedsys_disable(struct clk *clk) |
| 248 | { |
| 249 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 250 | const struct mtk_pll_data *pll = &priv->tree->plls[clk->id]; |
| 251 | u32 r; |
| 252 | |
| 253 | if (pll->flags & HAVE_RST_BAR) { |
| 254 | r = readl(priv->base + pll->reg + REG_CON0); |
| 255 | r &= ~pll->rst_bar_mask; |
| 256 | writel(r, priv->base + pll->reg + REG_CON0); |
| 257 | } |
| 258 | |
| 259 | r = readl(priv->base + pll->reg + REG_CON0); |
| 260 | r &= ~CON0_BASE_EN; |
| 261 | writel(r, priv->base + pll->reg + REG_CON0); |
| 262 | |
| 263 | r = readl(priv->base + pll->pwr_reg) | CON0_ISO_EN; |
| 264 | writel(r, priv->base + pll->pwr_reg); |
| 265 | |
| 266 | r = readl(priv->base + pll->pwr_reg) & ~CON0_PWR_ON; |
| 267 | writel(r, priv->base + pll->pwr_reg); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | /* topckgen functions */ |
| 273 | |
| 274 | static ulong mtk_factor_recalc_rate(const struct mtk_fixed_factor *fdiv, |
| 275 | ulong parent_rate) |
| 276 | { |
| 277 | u64 rate = parent_rate * fdiv->mult; |
| 278 | |
| 279 | do_div(rate, fdiv->div); |
| 280 | |
| 281 | return rate; |
| 282 | } |
| 283 | |
developer | 65da8e7 | 2020-01-10 16:30:30 +0800 | [diff] [blame] | 284 | static ulong mtk_topckgen_get_factor_rate(struct clk *clk, u32 off) |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 285 | { |
| 286 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 287 | const struct mtk_fixed_factor *fdiv = &priv->tree->fdivs[off]; |
| 288 | ulong rate; |
| 289 | |
| 290 | switch (fdiv->flags & CLK_PARENT_MASK) { |
| 291 | case CLK_PARENT_APMIXED: |
| 292 | rate = mtk_clk_find_parent_rate(clk, fdiv->parent, |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 293 | priv->parent); |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 294 | break; |
| 295 | case CLK_PARENT_TOPCKGEN: |
| 296 | rate = mtk_clk_find_parent_rate(clk, fdiv->parent, NULL); |
| 297 | break; |
| 298 | |
developer | f724f11 | 2022-09-09 20:00:07 +0800 | [diff] [blame] | 299 | case CLK_PARENT_XTAL: |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 300 | default: |
| 301 | rate = priv->tree->xtal_rate; |
| 302 | } |
| 303 | |
| 304 | return mtk_factor_recalc_rate(fdiv, rate); |
| 305 | } |
| 306 | |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 307 | static ulong mtk_infrasys_get_factor_rate(struct clk *clk, u32 off) |
| 308 | { |
| 309 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 310 | const struct mtk_fixed_factor *fdiv = &priv->tree->fdivs[off]; |
| 311 | ulong rate; |
| 312 | |
| 313 | switch (fdiv->flags & CLK_PARENT_MASK) { |
| 314 | case CLK_PARENT_TOPCKGEN: |
| 315 | rate = mtk_clk_find_parent_rate(clk, fdiv->parent, |
| 316 | priv->parent); |
| 317 | break; |
developer | f724f11 | 2022-09-09 20:00:07 +0800 | [diff] [blame] | 318 | case CLK_PARENT_XTAL: |
| 319 | rate = priv->tree->xtal_rate; |
| 320 | break; |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 321 | default: |
| 322 | rate = mtk_clk_find_parent_rate(clk, fdiv->parent, NULL); |
| 323 | } |
| 324 | |
| 325 | return mtk_factor_recalc_rate(fdiv, rate); |
| 326 | } |
| 327 | |
developer | 65da8e7 | 2020-01-10 16:30:30 +0800 | [diff] [blame] | 328 | static ulong mtk_topckgen_get_mux_rate(struct clk *clk, u32 off) |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 329 | { |
| 330 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 331 | const struct mtk_composite *mux = &priv->tree->muxes[off]; |
| 332 | u32 index; |
| 333 | |
| 334 | index = readl(priv->base + mux->mux_reg); |
| 335 | index &= mux->mux_mask << mux->mux_shift; |
| 336 | index = index >> mux->mux_shift; |
| 337 | |
developer | 2dc4caa | 2022-09-09 19:59:59 +0800 | [diff] [blame] | 338 | if (mux->parent[index] > 0 || |
| 339 | (mux->parent[index] == CLK_XTAL && |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 340 | priv->tree->flags & CLK_BYPASS_XTAL)) { |
| 341 | switch (mux->flags & CLK_PARENT_MASK) { |
| 342 | case CLK_PARENT_APMIXED: |
| 343 | return mtk_clk_find_parent_rate(clk, mux->parent[index], |
| 344 | priv->parent); |
| 345 | break; |
| 346 | default: |
| 347 | return mtk_clk_find_parent_rate(clk, mux->parent[index], |
| 348 | NULL); |
| 349 | break; |
| 350 | } |
| 351 | } |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 352 | |
| 353 | return priv->tree->xtal_rate; |
| 354 | } |
| 355 | |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 356 | static ulong mtk_infrasys_get_mux_rate(struct clk *clk, u32 off) |
| 357 | { |
| 358 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 359 | const struct mtk_composite *mux = &priv->tree->muxes[off]; |
| 360 | u32 index; |
| 361 | |
| 362 | index = readl(priv->base + mux->mux_reg); |
| 363 | index &= mux->mux_mask << mux->mux_shift; |
| 364 | index = index >> mux->mux_shift; |
| 365 | |
| 366 | if (mux->parent[index] > 0 || |
| 367 | (mux->parent[index] == CLK_XTAL && |
| 368 | priv->tree->flags & CLK_BYPASS_XTAL)) { |
| 369 | switch (mux->flags & CLK_PARENT_MASK) { |
| 370 | case CLK_PARENT_TOPCKGEN: |
| 371 | return mtk_clk_find_parent_rate(clk, mux->parent[index], |
| 372 | priv->parent); |
| 373 | break; |
| 374 | default: |
| 375 | return mtk_clk_find_parent_rate(clk, mux->parent[index], |
| 376 | NULL); |
| 377 | break; |
| 378 | } |
| 379 | } |
Christian Marangi | 358415a | 2024-06-28 19:40:47 +0200 | [diff] [blame] | 380 | return priv->tree->xtal_rate; |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 381 | } |
| 382 | |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 383 | static ulong mtk_topckgen_get_rate(struct clk *clk) |
| 384 | { |
| 385 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 386 | |
| 387 | if (clk->id < priv->tree->fdivs_offs) |
| 388 | return priv->tree->fclks[clk->id].rate; |
| 389 | else if (clk->id < priv->tree->muxes_offs) |
| 390 | return mtk_topckgen_get_factor_rate(clk, clk->id - |
| 391 | priv->tree->fdivs_offs); |
| 392 | else |
| 393 | return mtk_topckgen_get_mux_rate(clk, clk->id - |
| 394 | priv->tree->muxes_offs); |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static ulong mtk_infrasys_get_rate(struct clk *clk) |
| 398 | { |
| 399 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 400 | |
| 401 | ulong rate; |
| 402 | |
| 403 | if (clk->id < priv->tree->fdivs_offs) { |
| 404 | rate = priv->tree->fclks[clk->id].rate; |
| 405 | } else if (clk->id < priv->tree->muxes_offs) { |
| 406 | rate = mtk_infrasys_get_factor_rate(clk, clk->id - |
| 407 | priv->tree->fdivs_offs); |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 408 | /* No gates defined or ID is a MUX */ |
| 409 | } else if (!priv->tree->gates || clk->id < priv->tree->gates_offs) { |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 410 | rate = mtk_infrasys_get_mux_rate(clk, clk->id - |
| 411 | priv->tree->muxes_offs); |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 412 | /* Only valid with muxes + gates implementation */ |
| 413 | } else { |
| 414 | struct udevice *parent = NULL; |
| 415 | const struct mtk_gate *gate; |
| 416 | |
| 417 | gate = &priv->tree->gates[clk->id - priv->tree->gates_offs]; |
| 418 | if (gate->flags & CLK_PARENT_TOPCKGEN) |
| 419 | parent = priv->parent; |
| 420 | /* |
| 421 | * Assume xtal_rate to be declared if some gates have |
| 422 | * XTAL as parent |
| 423 | */ |
| 424 | else if (gate->flags & CLK_PARENT_XTAL) |
| 425 | return priv->tree->xtal_rate; |
| 426 | |
| 427 | rate = mtk_clk_find_parent_rate(clk, gate->parent, parent); |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | return rate; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 431 | } |
| 432 | |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 433 | static int mtk_clk_mux_enable(struct clk *clk) |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 434 | { |
| 435 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 436 | const struct mtk_composite *mux; |
| 437 | u32 val; |
| 438 | |
| 439 | if (clk->id < priv->tree->muxes_offs) |
| 440 | return 0; |
| 441 | |
| 442 | mux = &priv->tree->muxes[clk->id - priv->tree->muxes_offs]; |
| 443 | if (mux->gate_shift < 0) |
| 444 | return 0; |
| 445 | |
| 446 | /* enable clock gate */ |
developer | ba560c7 | 2019-12-31 11:29:21 +0800 | [diff] [blame] | 447 | if (mux->flags & CLK_MUX_SETCLR_UPD) { |
| 448 | val = BIT(mux->gate_shift); |
| 449 | writel(val, priv->base + mux->mux_clr_reg); |
| 450 | } else { |
| 451 | val = readl(priv->base + mux->gate_reg); |
| 452 | val &= ~BIT(mux->gate_shift); |
| 453 | writel(val, priv->base + mux->gate_reg); |
| 454 | } |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 455 | |
| 456 | if (mux->flags & CLK_DOMAIN_SCPSYS) { |
| 457 | /* enable scpsys clock off control */ |
| 458 | writel(SCP_ARMCK_OFF_EN, priv->base + CLK_SCP_CFG0); |
| 459 | writel(SCP_AXICK_DCM_DIS_EN | SCP_AXICK_26M_SEL_EN, |
| 460 | priv->base + CLK_SCP_CFG1); |
| 461 | } |
| 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 466 | static int mtk_clk_mux_disable(struct clk *clk) |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 467 | { |
| 468 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 469 | const struct mtk_composite *mux; |
| 470 | u32 val; |
| 471 | |
| 472 | if (clk->id < priv->tree->muxes_offs) |
| 473 | return 0; |
| 474 | |
| 475 | mux = &priv->tree->muxes[clk->id - priv->tree->muxes_offs]; |
| 476 | if (mux->gate_shift < 0) |
| 477 | return 0; |
| 478 | |
| 479 | /* disable clock gate */ |
developer | ba560c7 | 2019-12-31 11:29:21 +0800 | [diff] [blame] | 480 | if (mux->flags & CLK_MUX_SETCLR_UPD) { |
| 481 | val = BIT(mux->gate_shift); |
| 482 | writel(val, priv->base + mux->mux_set_reg); |
| 483 | } else { |
| 484 | val = readl(priv->base + mux->gate_reg); |
| 485 | val |= BIT(mux->gate_shift); |
| 486 | writel(val, priv->base + mux->gate_reg); |
| 487 | } |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 492 | static int mtk_common_clk_set_parent(struct clk *clk, struct clk *parent) |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 493 | { |
| 494 | struct mtk_clk_priv *priv = dev_get_priv(clk->dev); |
| 495 | |
| 496 | if (clk->id < priv->tree->muxes_offs) |
| 497 | return 0; |
| 498 | |
| 499 | return mtk_clk_mux_set_parent(priv->base, parent->id, |
| 500 | &priv->tree->muxes[clk->id - priv->tree->muxes_offs]); |
| 501 | } |
| 502 | |
| 503 | /* CG functions */ |
| 504 | |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 505 | static int mtk_gate_enable(void __iomem *base, const struct mtk_gate *gate) |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 506 | { |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 507 | u32 bit = BIT(gate->shift); |
| 508 | |
| 509 | switch (gate->flags & CLK_GATE_MASK) { |
| 510 | case CLK_GATE_SETCLR: |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 511 | writel(bit, base + gate->regs->clr_ofs); |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 512 | break; |
Fabien Parent | 69463e5 | 2019-03-24 16:46:35 +0100 | [diff] [blame] | 513 | case CLK_GATE_SETCLR_INV: |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 514 | writel(bit, base + gate->regs->set_ofs); |
Fabien Parent | 69463e5 | 2019-03-24 16:46:35 +0100 | [diff] [blame] | 515 | break; |
| 516 | case CLK_GATE_NO_SETCLR: |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 517 | clrsetbits_le32(base + gate->regs->sta_ofs, bit, 0); |
Fabien Parent | 69463e5 | 2019-03-24 16:46:35 +0100 | [diff] [blame] | 518 | break; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 519 | case CLK_GATE_NO_SETCLR_INV: |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 520 | clrsetbits_le32(base + gate->regs->sta_ofs, bit, bit); |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 521 | break; |
| 522 | |
| 523 | default: |
| 524 | return -EINVAL; |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 530 | static int mtk_clk_gate_enable(struct clk *clk) |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 531 | { |
| 532 | struct mtk_cg_priv *priv = dev_get_priv(clk->dev); |
| 533 | const struct mtk_gate *gate = &priv->gates[clk->id]; |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 534 | |
| 535 | return mtk_gate_enable(priv->base, gate); |
| 536 | } |
| 537 | |
| 538 | static int mtk_clk_infrasys_enable(struct clk *clk) |
| 539 | { |
| 540 | struct mtk_cg_priv *priv = dev_get_priv(clk->dev); |
| 541 | const struct mtk_gate *gate; |
| 542 | |
| 543 | /* MUX handling */ |
| 544 | if (!priv->tree->gates || clk->id < priv->tree->gates_offs) |
| 545 | return mtk_clk_mux_enable(clk); |
| 546 | |
| 547 | gate = &priv->tree->gates[clk->id - priv->tree->gates_offs]; |
| 548 | return mtk_gate_enable(priv->base, gate); |
| 549 | } |
| 550 | |
| 551 | static int mtk_gate_disable(void __iomem *base, const struct mtk_gate *gate) |
| 552 | { |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 553 | u32 bit = BIT(gate->shift); |
| 554 | |
| 555 | switch (gate->flags & CLK_GATE_MASK) { |
| 556 | case CLK_GATE_SETCLR: |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 557 | writel(bit, base + gate->regs->set_ofs); |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 558 | break; |
Fabien Parent | 69463e5 | 2019-03-24 16:46:35 +0100 | [diff] [blame] | 559 | case CLK_GATE_SETCLR_INV: |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 560 | writel(bit, base + gate->regs->clr_ofs); |
Fabien Parent | 69463e5 | 2019-03-24 16:46:35 +0100 | [diff] [blame] | 561 | break; |
| 562 | case CLK_GATE_NO_SETCLR: |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 563 | clrsetbits_le32(base + gate->regs->sta_ofs, bit, bit); |
Fabien Parent | 69463e5 | 2019-03-24 16:46:35 +0100 | [diff] [blame] | 564 | break; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 565 | case CLK_GATE_NO_SETCLR_INV: |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 566 | clrsetbits_le32(base + gate->regs->sta_ofs, bit, 0); |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 567 | break; |
| 568 | |
| 569 | default: |
| 570 | return -EINVAL; |
| 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 576 | static int mtk_clk_gate_disable(struct clk *clk) |
| 577 | { |
| 578 | struct mtk_cg_priv *priv = dev_get_priv(clk->dev); |
| 579 | const struct mtk_gate *gate = &priv->gates[clk->id]; |
| 580 | |
| 581 | return mtk_gate_disable(priv->base, gate); |
| 582 | } |
| 583 | |
| 584 | static int mtk_clk_infrasys_disable(struct clk *clk) |
| 585 | { |
| 586 | struct mtk_cg_priv *priv = dev_get_priv(clk->dev); |
| 587 | const struct mtk_gate *gate; |
| 588 | |
| 589 | /* MUX handling */ |
| 590 | if (!priv->tree->gates || clk->id < priv->tree->gates_offs) |
| 591 | return mtk_clk_mux_disable(clk); |
| 592 | |
| 593 | gate = &priv->tree->gates[clk->id - priv->tree->gates_offs]; |
| 594 | return mtk_gate_disable(priv->base, gate); |
| 595 | } |
| 596 | |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 597 | static ulong mtk_clk_gate_get_rate(struct clk *clk) |
| 598 | { |
| 599 | struct mtk_cg_priv *priv = dev_get_priv(clk->dev); |
| 600 | const struct mtk_gate *gate = &priv->gates[clk->id]; |
| 601 | |
Christian Marangi | 16f5f3f | 2024-06-28 19:40:46 +0200 | [diff] [blame] | 602 | /* |
| 603 | * Assume xtal_rate to be declared if some gates have |
| 604 | * XTAL as parent |
| 605 | */ |
| 606 | if (gate->flags & CLK_PARENT_XTAL) |
| 607 | return priv->tree->xtal_rate; |
| 608 | |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 609 | return mtk_clk_find_parent_rate(clk, gate->parent, priv->parent); |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | const struct clk_ops mtk_clk_apmixedsys_ops = { |
| 613 | .enable = mtk_apmixedsys_enable, |
| 614 | .disable = mtk_apmixedsys_disable, |
| 615 | .set_rate = mtk_apmixedsys_set_rate, |
| 616 | .get_rate = mtk_apmixedsys_get_rate, |
| 617 | }; |
| 618 | |
| 619 | const struct clk_ops mtk_clk_topckgen_ops = { |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 620 | .enable = mtk_clk_mux_enable, |
| 621 | .disable = mtk_clk_mux_disable, |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 622 | .get_rate = mtk_topckgen_get_rate, |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 623 | .set_parent = mtk_common_clk_set_parent, |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 624 | }; |
| 625 | |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 626 | const struct clk_ops mtk_clk_infrasys_ops = { |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame^] | 627 | .enable = mtk_clk_infrasys_enable, |
| 628 | .disable = mtk_clk_infrasys_disable, |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 629 | .get_rate = mtk_infrasys_get_rate, |
| 630 | .set_parent = mtk_common_clk_set_parent, |
| 631 | }; |
| 632 | |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 633 | const struct clk_ops mtk_clk_gate_ops = { |
| 634 | .enable = mtk_clk_gate_enable, |
| 635 | .disable = mtk_clk_gate_disable, |
| 636 | .get_rate = mtk_clk_gate_get_rate, |
| 637 | }; |
| 638 | |
| 639 | int mtk_common_clk_init(struct udevice *dev, |
| 640 | const struct mtk_clk_tree *tree) |
| 641 | { |
| 642 | struct mtk_clk_priv *priv = dev_get_priv(dev); |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 643 | struct udevice *parent; |
| 644 | int ret; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 645 | |
| 646 | priv->base = dev_read_addr_ptr(dev); |
| 647 | if (!priv->base) |
| 648 | return -ENOENT; |
| 649 | |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 650 | ret = uclass_get_device_by_phandle(UCLASS_CLK, dev, "clock-parent", &parent); |
| 651 | if (ret || !parent) { |
| 652 | ret = uclass_get_device_by_driver(UCLASS_CLK, |
| 653 | DM_DRIVER_GET(mtk_clk_apmixedsys), &parent); |
| 654 | if (ret || !parent) |
| 655 | return -ENOENT; |
| 656 | } |
| 657 | |
| 658 | priv->parent = parent; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 659 | priv->tree = tree; |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | int mtk_common_clk_gate_init(struct udevice *dev, |
| 665 | const struct mtk_clk_tree *tree, |
| 666 | const struct mtk_gate *gates) |
| 667 | { |
| 668 | struct mtk_cg_priv *priv = dev_get_priv(dev); |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 669 | struct udevice *parent; |
| 670 | int ret; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 671 | |
| 672 | priv->base = dev_read_addr_ptr(dev); |
| 673 | if (!priv->base) |
| 674 | return -ENOENT; |
| 675 | |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 676 | ret = uclass_get_device_by_phandle(UCLASS_CLK, dev, "clock-parent", &parent); |
| 677 | if (ret || !parent) { |
| 678 | ret = uclass_get_device_by_driver(UCLASS_CLK, |
| 679 | DM_DRIVER_GET(mtk_clk_topckgen), &parent); |
| 680 | if (ret || !parent) |
| 681 | return -ENOENT; |
| 682 | } |
| 683 | |
| 684 | priv->parent = parent; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 685 | priv->tree = tree; |
| 686 | priv->gates = gates; |
| 687 | |
| 688 | return 0; |
| 689 | } |