blob: 66683aeb2d72027cd3a2c2d36f7d199a13027411 [file] [log] [blame]
developer2186c982018-11-15 10:07:54 +08001// 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
developer2186c982018-11-15 10:07:54 +08009#include <clk-uclass.h>
10#include <div64.h>
11#include <dm.h>
12#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060013#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
developer2186c982018-11-15 10:07:54 +080015
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 Marangi29771ad2024-06-28 19:40:54 +020038static 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 Marangi475d00f2024-06-28 19:40:56 +020050static 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
75static 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
developer2186c982018-11-15 10:07:54 +0800100/*
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 */
developer65da8e72020-01-10 16:30:30 +0800105static ulong mtk_clk_find_parent_rate(struct clk *clk, int id,
developerfd47f762022-09-09 20:00:01 +0800106 struct udevice *pdev)
developer2186c982018-11-15 10:07:54 +0800107{
108 struct clk parent = { .id = id, };
109
developerfd47f762022-09-09 20:00:01 +0800110 if (pdev)
111 parent.dev = pdev;
112 else
developer2186c982018-11-15 10:07:54 +0800113 parent.dev = clk->dev;
developer2186c982018-11-15 10:07:54 +0800114
115 return clk_get_rate(&parent);
116}
117
118static int mtk_clk_mux_set_parent(void __iomem *base, u32 parent,
Christian Marangia4143eb2024-06-28 19:40:50 +0200119 u32 parent_type,
developer2186c982018-11-15 10:07:54 +0800120 const struct mtk_composite *mux)
121{
122 u32 val, index = 0;
123
Christian Marangia4143eb2024-06-28 19:40:50 +0200124 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 }
developer2186c982018-11-15 10:07:54 +0800140
developerba560c72019-12-31 11:29:21 +0800141 if (mux->flags & CLK_MUX_SETCLR_UPD) {
142 val = (mux->mux_mask << mux->mux_shift);
143 writel(val, base + mux->mux_clr_reg);
developer2186c982018-11-15 10:07:54 +0800144
developerba560c72019-12-31 11:29:21 +0800145 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 }
developer2186c982018-11-15 10:07:54 +0800158
159 return 0;
160}
161
162/* apmixedsys functions */
163
164static 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;
developer0b5e5f12019-12-31 11:29:22 +0800169 int ibits;
developer2186c982018-11-15 10:07:54 +0800170 u64 vco;
171 u8 c = 0;
172
173 /* The fractional part of the PLL divider. */
developer0b5e5f12019-12-31 11:29:22 +0800174 ibits = pll->pcwibits ? pll->pcwibits : INTEGER_BITS;
175 pcwfbits = pcwbits > ibits ? pcwbits - ibits : 0;
developer2186c982018-11-15 10:07:54 +0800176
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 Marangi475d00f2024-06-28 19:40:56 +0200196static void mtk_pll_set_rate_regs(struct mtk_clk_priv *priv, u32 id,
197 u32 pcw, int postdiv)
developer2186c982018-11-15 10:07:54 +0800198{
Christian Marangi29771ad2024-06-28 19:40:54 +0200199 const struct mtk_pll_data *pll;
developer0b5e5f12019-12-31 11:29:22 +0800200 u32 val, chg;
developer2186c982018-11-15 10:07:54 +0800201
Christian Marangi29771ad2024-06-28 19:40:54 +0200202 pll = &priv->tree->plls[id];
203
developer2186c982018-11-15 10:07:54 +0800204 /* 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;
developer2186c982018-11-15 10:07:54 +0800218
developer0b5e5f12019-12-31 11:29:22 +0800219 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 }
developer2186c982018-11-15 10:07:54 +0800228
229 udelay(20);
230}
231
232/**
233 * mtk_pll_calc_values - calculate good values for a given input frequency.
Christian Marangi475d00f2024-06-28 19:40:56 +0200234 * @priv: The mtk priv struct
235 * @id: The clk id
developer2186c982018-11-15 10:07:54 +0800236 * @pcw: The pcw value (output)
237 * @postdiv: The post divider (output)
238 * @freq: The desired target frequency
239 */
Christian Marangi475d00f2024-06-28 19:40:56 +0200240static void mtk_pll_calc_values(struct mtk_clk_priv *priv, u32 id,
241 u32 *pcw, u32 *postdiv, u32 freq)
developer2186c982018-11-15 10:07:54 +0800242{
Christian Marangi29771ad2024-06-28 19:40:54 +0200243 const struct mtk_pll_data *pll;
Christian Marangi29771ad2024-06-28 19:40:54 +0200244 unsigned long fmin;
developer2186c982018-11-15 10:07:54 +0800245 u64 _pcw;
developer0b5e5f12019-12-31 11:29:22 +0800246 int ibits;
developer2186c982018-11-15 10:07:54 +0800247 u32 val;
248
Christian Marangi29771ad2024-06-28 19:40:54 +0200249 pll = &priv->tree->plls[id];
250 fmin = pll->fmin ? pll->fmin : 1000 * MHZ;
251
developer2186c982018-11-15 10:07:54 +0800252 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 */
developer0b5e5f12019-12-31 11:29:22 +0800262 ibits = pll->pcwibits ? pll->pcwibits : INTEGER_BITS;
263 _pcw = ((u64)freq << val) << (pll->pcwbits - ibits);
developer2186c982018-11-15 10:07:54 +0800264 do_div(_pcw, priv->tree->xtal2_rate);
265
266 *pcw = (u32)_pcw;
267}
268
269static ulong mtk_apmixedsys_set_rate(struct clk *clk, ulong rate)
270{
Christian Marangi475d00f2024-06-28 19:40:56 +0200271 struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
272 int id = mtk_clk_get_id(clk);
developer2186c982018-11-15 10:07:54 +0800273 u32 pcw = 0;
274 u32 postdiv;
275
Christian Marangi475d00f2024-06-28 19:40:56 +0200276 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);
developer2186c982018-11-15 10:07:54 +0800281
282 return 0;
283}
284
285static ulong mtk_apmixedsys_get_rate(struct clk *clk)
286{
287 struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
Christian Marangi29771ad2024-06-28 19:40:54 +0200288 const struct mtk_pll_data *pll;
289 int id = mtk_clk_get_id(clk);
Christian Marangi475d00f2024-06-28 19:40:56 +0200290 const struct mtk_gate *gate;
developer2186c982018-11-15 10:07:54 +0800291 u32 postdiv;
292 u32 pcw;
293
Christian Marangi475d00f2024-06-28 19:40:56 +0200294 /* 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 Marangi29771ad2024-06-28 19:40:54 +0200300 pll = &priv->tree->plls[id];
301
developer2186c982018-11-15 10:07:54 +0800302 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
313static int mtk_apmixedsys_enable(struct clk *clk)
314{
315 struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
Christian Marangi29771ad2024-06-28 19:40:54 +0200316 const struct mtk_pll_data *pll;
317 int id = mtk_clk_get_id(clk);
Christian Marangi475d00f2024-06-28 19:40:56 +0200318 const struct mtk_gate *gate;
developer2186c982018-11-15 10:07:54 +0800319 u32 r;
320
Christian Marangi475d00f2024-06-28 19:40:56 +0200321 /* 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 Marangi29771ad2024-06-28 19:40:54 +0200327 pll = &priv->tree->plls[id];
328
developer2186c982018-11-15 10:07:54 +0800329 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
352static int mtk_apmixedsys_disable(struct clk *clk)
353{
354 struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
Christian Marangi29771ad2024-06-28 19:40:54 +0200355 const struct mtk_pll_data *pll;
356 int id = mtk_clk_get_id(clk);
Christian Marangi475d00f2024-06-28 19:40:56 +0200357 const struct mtk_gate *gate;
developer2186c982018-11-15 10:07:54 +0800358 u32 r;
359
Christian Marangi475d00f2024-06-28 19:40:56 +0200360 /* 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 Marangi29771ad2024-06-28 19:40:54 +0200366 pll = &priv->tree->plls[id];
367
developer2186c982018-11-15 10:07:54 +0800368 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
389static 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
developer65da8e72020-01-10 16:30:30 +0800399static ulong mtk_topckgen_get_factor_rate(struct clk *clk, u32 off)
developer2186c982018-11-15 10:07:54 +0800400{
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,
developerfd47f762022-09-09 20:00:01 +0800408 priv->parent);
developer2186c982018-11-15 10:07:54 +0800409 break;
410 case CLK_PARENT_TOPCKGEN:
411 rate = mtk_clk_find_parent_rate(clk, fdiv->parent, NULL);
412 break;
413
developerf724f112022-09-09 20:00:07 +0800414 case CLK_PARENT_XTAL:
developer2186c982018-11-15 10:07:54 +0800415 default:
416 rate = priv->tree->xtal_rate;
417 }
418
419 return mtk_factor_recalc_rate(fdiv, rate);
420}
421
developerad5b0752022-09-09 20:00:04 +0800422static 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;
developerf724f112022-09-09 20:00:07 +0800433 case CLK_PARENT_XTAL:
434 rate = priv->tree->xtal_rate;
435 break;
developerad5b0752022-09-09 20:00:04 +0800436 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 Marangi1bde1492024-06-28 19:40:51 +0200443static 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
developer65da8e72020-01-10 16:30:30 +0800456static ulong mtk_topckgen_get_mux_rate(struct clk *clk, u32 off)
developer2186c982018-11-15 10:07:54 +0800457{
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 Marangi1bde1492024-06-28 19:40:51 +0200466 /*
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);
developerfd47f762022-09-09 20:00:01 +0800475 }
developer2186c982018-11-15 10:07:54 +0800476
Christian Marangi1bde1492024-06-28 19:40:51 +0200477 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);
developer2186c982018-11-15 10:07:54 +0800483}
484
Christian Marangia4143eb2024-06-28 19:40:50 +0200485static 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 Marangi3c2ae7d2024-06-28 19:40:57 +0200491 /* Assume the second level parent is always APMIXED */
492 case CLK_PARENT_APMIXED:
493 priv = dev_get_priv(priv->parent);
494 fallthrough;
Christian Marangia4143eb2024-06-28 19:40:50 +0200495 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 }
developer2186c982018-11-15 10:07:54 +0800500}
501
developerad5b0752022-09-09 20:00:04 +0800502static 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 Marangia4143eb2024-06-28 19:40:50 +0200512 /*
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);
developerad5b0752022-09-09 20:00:04 +0800520 }
Christian Marangia4143eb2024-06-28 19:40:50 +0200521
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);
developerad5b0752022-09-09 20:00:04 +0800527}
528
developer2186c982018-11-15 10:07:54 +0800529static ulong mtk_topckgen_get_rate(struct clk *clk)
530{
531 struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
Christian Marangi29771ad2024-06-28 19:40:54 +0200532 int id = mtk_clk_get_id(clk);
developer2186c982018-11-15 10:07:54 +0800533
Christian Marangi29771ad2024-06-28 19:40:54 +0200534 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 -
developer2186c982018-11-15 10:07:54 +0800538 priv->tree->fdivs_offs);
539 else
Christian Marangi29771ad2024-06-28 19:40:54 +0200540 return mtk_topckgen_get_mux_rate(clk, id -
developer2186c982018-11-15 10:07:54 +0800541 priv->tree->muxes_offs);
developerad5b0752022-09-09 20:00:04 +0800542}
543
544static ulong mtk_infrasys_get_rate(struct clk *clk)
545{
546 struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
Christian Marangi29771ad2024-06-28 19:40:54 +0200547 int id = mtk_clk_get_id(clk);
developerad5b0752022-09-09 20:00:04 +0800548 ulong rate;
549
Christian Marangi29771ad2024-06-28 19:40:54 +0200550 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 -
developerad5b0752022-09-09 20:00:04 +0800554 priv->tree->fdivs_offs);
Christian Marangibaa244c2024-06-28 19:40:48 +0200555 /* No gates defined or ID is a MUX */
Christian Marangi29771ad2024-06-28 19:40:54 +0200556 } else if (!priv->tree->gates || id < priv->tree->gates_offs) {
557 rate = mtk_infrasys_get_mux_rate(clk, id -
developerad5b0752022-09-09 20:00:04 +0800558 priv->tree->muxes_offs);
Christian Marangibaa244c2024-06-28 19:40:48 +0200559 /* Only valid with muxes + gates implementation */
560 } else {
561 struct udevice *parent = NULL;
562 const struct mtk_gate *gate;
563
Christian Marangi29771ad2024-06-28 19:40:54 +0200564 gate = &priv->tree->gates[id - priv->tree->gates_offs];
Christian Marangibaa244c2024-06-28 19:40:48 +0200565 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);
developerad5b0752022-09-09 20:00:04 +0800575 }
576
577 return rate;
developer2186c982018-11-15 10:07:54 +0800578}
579
developerfd47f762022-09-09 20:00:01 +0800580static int mtk_clk_mux_enable(struct clk *clk)
developer2186c982018-11-15 10:07:54 +0800581{
582 struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
583 const struct mtk_composite *mux;
Christian Marangi29771ad2024-06-28 19:40:54 +0200584 int id = mtk_clk_get_id(clk);
developer2186c982018-11-15 10:07:54 +0800585 u32 val;
586
Christian Marangi29771ad2024-06-28 19:40:54 +0200587 if (id < priv->tree->muxes_offs)
developer2186c982018-11-15 10:07:54 +0800588 return 0;
589
Christian Marangi29771ad2024-06-28 19:40:54 +0200590 mux = &priv->tree->muxes[id - priv->tree->muxes_offs];
developer2186c982018-11-15 10:07:54 +0800591 if (mux->gate_shift < 0)
592 return 0;
593
594 /* enable clock gate */
developerba560c72019-12-31 11:29:21 +0800595 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 }
developer2186c982018-11-15 10:07:54 +0800603
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
developerfd47f762022-09-09 20:00:01 +0800614static int mtk_clk_mux_disable(struct clk *clk)
developer2186c982018-11-15 10:07:54 +0800615{
616 struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
617 const struct mtk_composite *mux;
Christian Marangi29771ad2024-06-28 19:40:54 +0200618 int id = mtk_clk_get_id(clk);
developer2186c982018-11-15 10:07:54 +0800619 u32 val;
620
Christian Marangi29771ad2024-06-28 19:40:54 +0200621 if (id < priv->tree->muxes_offs)
developer2186c982018-11-15 10:07:54 +0800622 return 0;
623
Christian Marangi29771ad2024-06-28 19:40:54 +0200624 mux = &priv->tree->muxes[id - priv->tree->muxes_offs];
developer2186c982018-11-15 10:07:54 +0800625 if (mux->gate_shift < 0)
626 return 0;
627
628 /* disable clock gate */
developerba560c72019-12-31 11:29:21 +0800629 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 }
developer2186c982018-11-15 10:07:54 +0800637
638 return 0;
639}
640
developerfd47f762022-09-09 20:00:01 +0800641static int mtk_common_clk_set_parent(struct clk *clk, struct clk *parent)
developer2186c982018-11-15 10:07:54 +0800642{
Christian Marangia4143eb2024-06-28 19:40:50 +0200643 struct mtk_clk_priv *parent_priv = dev_get_priv(parent->dev);
developer2186c982018-11-15 10:07:54 +0800644 struct mtk_clk_priv *priv = dev_get_priv(clk->dev);
Christian Marangi29771ad2024-06-28 19:40:54 +0200645 int id = mtk_clk_get_id(clk);
Christian Marangia4143eb2024-06-28 19:40:50 +0200646 u32 parent_type;
developer2186c982018-11-15 10:07:54 +0800647
Christian Marangi29771ad2024-06-28 19:40:54 +0200648 if (id < priv->tree->muxes_offs)
developer2186c982018-11-15 10:07:54 +0800649 return 0;
developer2186c982018-11-15 10:07:54 +0800650
Christian Marangia4143eb2024-06-28 19:40:50 +0200651 if (!parent_priv)
developer2186c982018-11-15 10:07:54 +0800652 return 0;
653
Christian Marangia4143eb2024-06-28 19:40:50 +0200654 parent_type = parent_priv->tree->flags & CLK_PARENT_MASK;
655 return mtk_clk_mux_set_parent(priv->base, parent->id, parent_type,
Christian Marangi29771ad2024-06-28 19:40:54 +0200656 &priv->tree->muxes[id - priv->tree->muxes_offs]);
developer2186c982018-11-15 10:07:54 +0800657}
658
659/* CG functions */
660
661static int mtk_clk_gate_enable(struct clk *clk)
662{
663 struct mtk_cg_priv *priv = dev_get_priv(clk->dev);
Christian Marangi29771ad2024-06-28 19:40:54 +0200664 int id = mtk_clk_get_id(clk);
Christian Marangi8f5b30c2024-06-28 19:40:49 +0200665 const struct mtk_gate *gate;
developer2186c982018-11-15 10:07:54 +0800666
Christian Marangi29771ad2024-06-28 19:40:54 +0200667 if (id < priv->tree->gates_offs)
developer2186c982018-11-15 10:07:54 +0800668 return -EINVAL;
developer2186c982018-11-15 10:07:54 +0800669
Christian Marangi29771ad2024-06-28 19:40:54 +0200670 gate = &priv->gates[id - priv->tree->gates_offs];
Christian Marangibaa244c2024-06-28 19:40:48 +0200671 return mtk_gate_enable(priv->base, gate);
developer2186c982018-11-15 10:07:54 +0800672}
673
Christian Marangibaa244c2024-06-28 19:40:48 +0200674static int mtk_clk_infrasys_enable(struct clk *clk)
developer2186c982018-11-15 10:07:54 +0800675{
676 struct mtk_cg_priv *priv = dev_get_priv(clk->dev);
Christian Marangi29771ad2024-06-28 19:40:54 +0200677 int id = mtk_clk_get_id(clk);
Christian Marangibaa244c2024-06-28 19:40:48 +0200678 const struct mtk_gate *gate;
developer2186c982018-11-15 10:07:54 +0800679
Christian Marangibaa244c2024-06-28 19:40:48 +0200680 /* MUX handling */
Christian Marangi29771ad2024-06-28 19:40:54 +0200681 if (!priv->tree->gates || id < priv->tree->gates_offs)
Christian Marangibaa244c2024-06-28 19:40:48 +0200682 return mtk_clk_mux_enable(clk);
developer2186c982018-11-15 10:07:54 +0800683
Christian Marangi29771ad2024-06-28 19:40:54 +0200684 gate = &priv->tree->gates[id - priv->tree->gates_offs];
Christian Marangibaa244c2024-06-28 19:40:48 +0200685 return mtk_gate_enable(priv->base, gate);
686}
687
Christian Marangibaa244c2024-06-28 19:40:48 +0200688static int mtk_clk_gate_disable(struct clk *clk)
689{
690 struct mtk_cg_priv *priv = dev_get_priv(clk->dev);
Christian Marangi29771ad2024-06-28 19:40:54 +0200691 int id = mtk_clk_get_id(clk);
Christian Marangi8f5b30c2024-06-28 19:40:49 +0200692 const struct mtk_gate *gate;
Christian Marangibaa244c2024-06-28 19:40:48 +0200693
Christian Marangi29771ad2024-06-28 19:40:54 +0200694 if (id < priv->tree->gates_offs)
developer2186c982018-11-15 10:07:54 +0800695 return -EINVAL;
developer2186c982018-11-15 10:07:54 +0800696
Christian Marangi29771ad2024-06-28 19:40:54 +0200697 gate = &priv->gates[id - priv->tree->gates_offs];
Christian Marangibaa244c2024-06-28 19:40:48 +0200698 return mtk_gate_disable(priv->base, gate);
699}
700
701static int mtk_clk_infrasys_disable(struct clk *clk)
702{
703 struct mtk_cg_priv *priv = dev_get_priv(clk->dev);
Christian Marangi29771ad2024-06-28 19:40:54 +0200704 int id = mtk_clk_get_id(clk);
Christian Marangibaa244c2024-06-28 19:40:48 +0200705 const struct mtk_gate *gate;
706
707 /* MUX handling */
Christian Marangi29771ad2024-06-28 19:40:54 +0200708 if (!priv->tree->gates || id < priv->tree->gates_offs)
Christian Marangibaa244c2024-06-28 19:40:48 +0200709 return mtk_clk_mux_disable(clk);
710
Christian Marangi29771ad2024-06-28 19:40:54 +0200711 gate = &priv->tree->gates[id - priv->tree->gates_offs];
Christian Marangibaa244c2024-06-28 19:40:48 +0200712 return mtk_gate_disable(priv->base, gate);
developer2186c982018-11-15 10:07:54 +0800713}
714
715static ulong mtk_clk_gate_get_rate(struct clk *clk)
716{
717 struct mtk_cg_priv *priv = dev_get_priv(clk->dev);
Christian Marangi56dc7cd2024-06-28 19:40:52 +0200718 struct udevice *parent = priv->parent;
Christian Marangi29771ad2024-06-28 19:40:54 +0200719 int id = mtk_clk_get_id(clk);
Christian Marangi8f5b30c2024-06-28 19:40:49 +0200720 const struct mtk_gate *gate;
721
Christian Marangi29771ad2024-06-28 19:40:54 +0200722 if (id < priv->tree->gates_offs)
Christian Marangi8f5b30c2024-06-28 19:40:49 +0200723 return -EINVAL;
developer2186c982018-11-15 10:07:54 +0800724
Christian Marangi29771ad2024-06-28 19:40:54 +0200725 gate = &priv->gates[id - priv->tree->gates_offs];
Christian Marangi16f5f3f2024-06-28 19:40:46 +0200726 /*
Christian Marangi56dc7cd2024-06-28 19:40:52 +0200727 * 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 Marangi16f5f3f2024-06-28 19:40:46 +0200738 * Assume xtal_rate to be declared if some gates have
739 * XTAL as parent
740 */
Christian Marangi56dc7cd2024-06-28 19:40:52 +0200741 } else if (gate->flags & CLK_PARENT_XTAL) {
Christian Marangi16f5f3f2024-06-28 19:40:46 +0200742 return priv->tree->xtal_rate;
Christian Marangi56dc7cd2024-06-28 19:40:52 +0200743 }
Christian Marangi16f5f3f2024-06-28 19:40:46 +0200744
Christian Marangi56dc7cd2024-06-28 19:40:52 +0200745 return mtk_clk_find_parent_rate(clk, gate->parent, parent);
developer2186c982018-11-15 10:07:54 +0800746}
747
748const 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
755const struct clk_ops mtk_clk_topckgen_ops = {
developerfd47f762022-09-09 20:00:01 +0800756 .enable = mtk_clk_mux_enable,
757 .disable = mtk_clk_mux_disable,
developer2186c982018-11-15 10:07:54 +0800758 .get_rate = mtk_topckgen_get_rate,
developerfd47f762022-09-09 20:00:01 +0800759 .set_parent = mtk_common_clk_set_parent,
developer2186c982018-11-15 10:07:54 +0800760};
761
developerad5b0752022-09-09 20:00:04 +0800762const struct clk_ops mtk_clk_infrasys_ops = {
Christian Marangibaa244c2024-06-28 19:40:48 +0200763 .enable = mtk_clk_infrasys_enable,
764 .disable = mtk_clk_infrasys_disable,
developerad5b0752022-09-09 20:00:04 +0800765 .get_rate = mtk_infrasys_get_rate,
766 .set_parent = mtk_common_clk_set_parent,
767};
768
developer2186c982018-11-15 10:07:54 +0800769const 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 Marangie03d0802024-06-28 19:40:53 +0200775static int mtk_common_clk_init_drv(struct udevice *dev,
776 const struct mtk_clk_tree *tree,
777 const struct driver *drv)
developer2186c982018-11-15 10:07:54 +0800778{
779 struct mtk_clk_priv *priv = dev_get_priv(dev);
developerfd47f762022-09-09 20:00:01 +0800780 struct udevice *parent;
781 int ret;
developer2186c982018-11-15 10:07:54 +0800782
783 priv->base = dev_read_addr_ptr(dev);
784 if (!priv->base)
785 return -ENOENT;
786
developerfd47f762022-09-09 20:00:01 +0800787 ret = uclass_get_device_by_phandle(UCLASS_CLK, dev, "clock-parent", &parent);
788 if (ret || !parent) {
Christian Marangie03d0802024-06-28 19:40:53 +0200789 ret = uclass_get_device_by_driver(UCLASS_CLK, drv, &parent);
developerfd47f762022-09-09 20:00:01 +0800790 if (ret || !parent)
791 return -ENOENT;
792 }
793
794 priv->parent = parent;
developer2186c982018-11-15 10:07:54 +0800795 priv->tree = tree;
796
797 return 0;
798}
799
Christian Marangie03d0802024-06-28 19:40:53 +0200800int 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
807int 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
developer2186c982018-11-15 10:07:54 +0800814int 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);
developerfd47f762022-09-09 20:00:01 +0800819 struct udevice *parent;
820 int ret;
developer2186c982018-11-15 10:07:54 +0800821
822 priv->base = dev_read_addr_ptr(dev);
823 if (!priv->base)
824 return -ENOENT;
825
developerfd47f762022-09-09 20:00:01 +0800826 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;
developer2186c982018-11-15 10:07:54 +0800835 priv->tree = tree;
836 priv->gates = gates;
837
838 return 0;
839}