blob: 986e73542d83c5898627f0efb1e04643665b5dc0 [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
Tom Riniabb9a042024-05-18 20:20:43 -06009#include <common.h>
developer2186c982018-11-15 10:07:54 +080010#include <clk-uclass.h>
11#include <div64.h>
12#include <dm.h>
13#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
developer2186c982018-11-15 10:07:54 +080016
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 */
developer65da8e72020-01-10 16:30:30 +080044static ulong mtk_clk_find_parent_rate(struct clk *clk, int id,
developerfd47f762022-09-09 20:00:01 +080045 struct udevice *pdev)
developer2186c982018-11-15 10:07:54 +080046{
47 struct clk parent = { .id = id, };
48
developerfd47f762022-09-09 20:00:01 +080049 if (pdev)
50 parent.dev = pdev;
51 else
developer2186c982018-11-15 10:07:54 +080052 parent.dev = clk->dev;
developer2186c982018-11-15 10:07:54 +080053
54 return clk_get_rate(&parent);
55}
56
57static 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
developerba560c72019-12-31 11:29:21 +080066 if (mux->flags & CLK_MUX_SETCLR_UPD) {
67 val = (mux->mux_mask << mux->mux_shift);
68 writel(val, base + mux->mux_clr_reg);
developer2186c982018-11-15 10:07:54 +080069
developerba560c72019-12-31 11:29:21 +080070 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 }
developer2186c982018-11-15 10:07:54 +080083
84 return 0;
85}
86
87/* apmixedsys functions */
88
89static 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;
developer0b5e5f12019-12-31 11:29:22 +080094 int ibits;
developer2186c982018-11-15 10:07:54 +080095 u64 vco;
96 u8 c = 0;
97
98 /* The fractional part of the PLL divider. */
developer0b5e5f12019-12-31 11:29:22 +080099 ibits = pll->pcwibits ? pll->pcwibits : INTEGER_BITS;
100 pcwfbits = pcwbits > ibits ? pcwbits - ibits : 0;
developer2186c982018-11-15 10:07:54 +0800101
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 */
121static 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];
developer0b5e5f12019-12-31 11:29:22 +0800125 u32 val, chg;
developer2186c982018-11-15 10:07:54 +0800126
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;
developer2186c982018-11-15 10:07:54 +0800141
developer0b5e5f12019-12-31 11:29:22 +0800142 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 }
developer2186c982018-11-15 10:07:54 +0800151
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 */
162static 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];
developer0b5e5f12019-12-31 11:29:22 +0800167 unsigned long fmin = pll->fmin ? pll->fmin : 1000 * MHZ;
developer2186c982018-11-15 10:07:54 +0800168 u64 _pcw;
developer0b5e5f12019-12-31 11:29:22 +0800169 int ibits;
developer2186c982018-11-15 10:07:54 +0800170 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 */
developer0b5e5f12019-12-31 11:29:22 +0800182 ibits = pll->pcwibits ? pll->pcwibits : INTEGER_BITS;
183 _pcw = ((u64)freq << val) << (pll->pcwbits - ibits);
developer2186c982018-11-15 10:07:54 +0800184 do_div(_pcw, priv->tree->xtal2_rate);
185
186 *pcw = (u32)_pcw;
187}
188
189static 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
200static 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
218static 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
247static 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
274static 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
developer65da8e72020-01-10 16:30:30 +0800284static ulong mtk_topckgen_get_factor_rate(struct clk *clk, u32 off)
developer2186c982018-11-15 10:07:54 +0800285{
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,
developerfd47f762022-09-09 20:00:01 +0800293 priv->parent);
developer2186c982018-11-15 10:07:54 +0800294 break;
295 case CLK_PARENT_TOPCKGEN:
296 rate = mtk_clk_find_parent_rate(clk, fdiv->parent, NULL);
297 break;
298
developerf724f112022-09-09 20:00:07 +0800299 case CLK_PARENT_XTAL:
developer2186c982018-11-15 10:07:54 +0800300 default:
301 rate = priv->tree->xtal_rate;
302 }
303
304 return mtk_factor_recalc_rate(fdiv, rate);
305}
306
developerad5b0752022-09-09 20:00:04 +0800307static 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;
developerf724f112022-09-09 20:00:07 +0800318 case CLK_PARENT_XTAL:
319 rate = priv->tree->xtal_rate;
320 break;
developerad5b0752022-09-09 20:00:04 +0800321 default:
322 rate = mtk_clk_find_parent_rate(clk, fdiv->parent, NULL);
323 }
324
325 return mtk_factor_recalc_rate(fdiv, rate);
326}
327
developer65da8e72020-01-10 16:30:30 +0800328static ulong mtk_topckgen_get_mux_rate(struct clk *clk, u32 off)
developer2186c982018-11-15 10:07:54 +0800329{
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
developer2dc4caa2022-09-09 19:59:59 +0800338 if (mux->parent[index] > 0 ||
339 (mux->parent[index] == CLK_XTAL &&
developerfd47f762022-09-09 20:00:01 +0800340 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 }
developer2186c982018-11-15 10:07:54 +0800352
353 return priv->tree->xtal_rate;
354}
355
developerad5b0752022-09-09 20:00:04 +0800356static 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 Marangi358415a2024-06-28 19:40:47 +0200380 return priv->tree->xtal_rate;
developerad5b0752022-09-09 20:00:04 +0800381}
382
developer2186c982018-11-15 10:07:54 +0800383static 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);
developerad5b0752022-09-09 20:00:04 +0800395}
396
397static 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 Marangibaa244c2024-06-28 19:40:48 +0200408 /* No gates defined or ID is a MUX */
409 } else if (!priv->tree->gates || clk->id < priv->tree->gates_offs) {
developerad5b0752022-09-09 20:00:04 +0800410 rate = mtk_infrasys_get_mux_rate(clk, clk->id -
411 priv->tree->muxes_offs);
Christian Marangibaa244c2024-06-28 19:40:48 +0200412 /* 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);
developerad5b0752022-09-09 20:00:04 +0800428 }
429
430 return rate;
developer2186c982018-11-15 10:07:54 +0800431}
432
developerfd47f762022-09-09 20:00:01 +0800433static int mtk_clk_mux_enable(struct clk *clk)
developer2186c982018-11-15 10:07:54 +0800434{
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 */
developerba560c72019-12-31 11:29:21 +0800447 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 }
developer2186c982018-11-15 10:07:54 +0800455
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
developerfd47f762022-09-09 20:00:01 +0800466static int mtk_clk_mux_disable(struct clk *clk)
developer2186c982018-11-15 10:07:54 +0800467{
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 */
developerba560c72019-12-31 11:29:21 +0800480 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 }
developer2186c982018-11-15 10:07:54 +0800488
489 return 0;
490}
491
developerfd47f762022-09-09 20:00:01 +0800492static int mtk_common_clk_set_parent(struct clk *clk, struct clk *parent)
developer2186c982018-11-15 10:07:54 +0800493{
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 Marangibaa244c2024-06-28 19:40:48 +0200505static int mtk_gate_enable(void __iomem *base, const struct mtk_gate *gate)
developer2186c982018-11-15 10:07:54 +0800506{
developer2186c982018-11-15 10:07:54 +0800507 u32 bit = BIT(gate->shift);
508
509 switch (gate->flags & CLK_GATE_MASK) {
510 case CLK_GATE_SETCLR:
Christian Marangibaa244c2024-06-28 19:40:48 +0200511 writel(bit, base + gate->regs->clr_ofs);
developer2186c982018-11-15 10:07:54 +0800512 break;
Fabien Parent69463e52019-03-24 16:46:35 +0100513 case CLK_GATE_SETCLR_INV:
Christian Marangibaa244c2024-06-28 19:40:48 +0200514 writel(bit, base + gate->regs->set_ofs);
Fabien Parent69463e52019-03-24 16:46:35 +0100515 break;
516 case CLK_GATE_NO_SETCLR:
Christian Marangibaa244c2024-06-28 19:40:48 +0200517 clrsetbits_le32(base + gate->regs->sta_ofs, bit, 0);
Fabien Parent69463e52019-03-24 16:46:35 +0100518 break;
developer2186c982018-11-15 10:07:54 +0800519 case CLK_GATE_NO_SETCLR_INV:
Christian Marangibaa244c2024-06-28 19:40:48 +0200520 clrsetbits_le32(base + gate->regs->sta_ofs, bit, bit);
developer2186c982018-11-15 10:07:54 +0800521 break;
522
523 default:
524 return -EINVAL;
525 }
526
527 return 0;
528}
529
Christian Marangibaa244c2024-06-28 19:40:48 +0200530static int mtk_clk_gate_enable(struct clk *clk)
developer2186c982018-11-15 10:07:54 +0800531{
532 struct mtk_cg_priv *priv = dev_get_priv(clk->dev);
533 const struct mtk_gate *gate = &priv->gates[clk->id];
Christian Marangibaa244c2024-06-28 19:40:48 +0200534
535 return mtk_gate_enable(priv->base, gate);
536}
537
538static 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
551static int mtk_gate_disable(void __iomem *base, const struct mtk_gate *gate)
552{
developer2186c982018-11-15 10:07:54 +0800553 u32 bit = BIT(gate->shift);
554
555 switch (gate->flags & CLK_GATE_MASK) {
556 case CLK_GATE_SETCLR:
Christian Marangibaa244c2024-06-28 19:40:48 +0200557 writel(bit, base + gate->regs->set_ofs);
developer2186c982018-11-15 10:07:54 +0800558 break;
Fabien Parent69463e52019-03-24 16:46:35 +0100559 case CLK_GATE_SETCLR_INV:
Christian Marangibaa244c2024-06-28 19:40:48 +0200560 writel(bit, base + gate->regs->clr_ofs);
Fabien Parent69463e52019-03-24 16:46:35 +0100561 break;
562 case CLK_GATE_NO_SETCLR:
Christian Marangibaa244c2024-06-28 19:40:48 +0200563 clrsetbits_le32(base + gate->regs->sta_ofs, bit, bit);
Fabien Parent69463e52019-03-24 16:46:35 +0100564 break;
developer2186c982018-11-15 10:07:54 +0800565 case CLK_GATE_NO_SETCLR_INV:
Christian Marangibaa244c2024-06-28 19:40:48 +0200566 clrsetbits_le32(base + gate->regs->sta_ofs, bit, 0);
developer2186c982018-11-15 10:07:54 +0800567 break;
568
569 default:
570 return -EINVAL;
571 }
572
573 return 0;
574}
575
Christian Marangibaa244c2024-06-28 19:40:48 +0200576static 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
584static 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
developer2186c982018-11-15 10:07:54 +0800597static 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 Marangi16f5f3f2024-06-28 19:40:46 +0200602 /*
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
developerfd47f762022-09-09 20:00:01 +0800609 return mtk_clk_find_parent_rate(clk, gate->parent, priv->parent);
developer2186c982018-11-15 10:07:54 +0800610}
611
612const 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
619const struct clk_ops mtk_clk_topckgen_ops = {
developerfd47f762022-09-09 20:00:01 +0800620 .enable = mtk_clk_mux_enable,
621 .disable = mtk_clk_mux_disable,
developer2186c982018-11-15 10:07:54 +0800622 .get_rate = mtk_topckgen_get_rate,
developerfd47f762022-09-09 20:00:01 +0800623 .set_parent = mtk_common_clk_set_parent,
developer2186c982018-11-15 10:07:54 +0800624};
625
developerad5b0752022-09-09 20:00:04 +0800626const struct clk_ops mtk_clk_infrasys_ops = {
Christian Marangibaa244c2024-06-28 19:40:48 +0200627 .enable = mtk_clk_infrasys_enable,
628 .disable = mtk_clk_infrasys_disable,
developerad5b0752022-09-09 20:00:04 +0800629 .get_rate = mtk_infrasys_get_rate,
630 .set_parent = mtk_common_clk_set_parent,
631};
632
developer2186c982018-11-15 10:07:54 +0800633const 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
639int 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);
developerfd47f762022-09-09 20:00:01 +0800643 struct udevice *parent;
644 int ret;
developer2186c982018-11-15 10:07:54 +0800645
646 priv->base = dev_read_addr_ptr(dev);
647 if (!priv->base)
648 return -ENOENT;
649
developerfd47f762022-09-09 20:00:01 +0800650 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;
developer2186c982018-11-15 10:07:54 +0800659 priv->tree = tree;
660
661 return 0;
662}
663
664int 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);
developerfd47f762022-09-09 20:00:01 +0800669 struct udevice *parent;
670 int ret;
developer2186c982018-11-15 10:07:54 +0800671
672 priv->base = dev_read_addr_ptr(dev);
673 if (!priv->base)
674 return -ENOENT;
675
developerfd47f762022-09-09 20:00:01 +0800676 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;
developer2186c982018-11-15 10:07:54 +0800685 priv->tree = tree;
686 priv->gates = gates;
687
688 return 0;
689}