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