1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
5 * This driver supports the SCCG plls found in the imx8m SOCs
7 * Documentation for this SCCG pll can be found at:
8 * https://www.nxp.com/docs/en/reference-manual/IMX8MDQLQRM.pdf#page=834
11 #include <linux/clk-provider.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
15 #include <linux/iopoll.h>
16 #include <linux/slab.h>
17 #include <linux/bitfield.h>
26 #define PLL_DIVF1_MASK GENMASK(18, 13)
27 #define PLL_DIVF2_MASK GENMASK(12, 7)
28 #define PLL_DIVR1_MASK GENMASK(27, 25)
29 #define PLL_DIVR2_MASK GENMASK(24, 19)
30 #define PLL_DIVQ_MASK GENMASK(6, 1)
31 #define PLL_REF_MASK GENMASK(2, 0)
33 #define PLL_LOCK_MASK BIT(31)
34 #define PLL_PD_MASK BIT(7)
36 /* These are the specification limits for the SSCG PLL */
37 #define PLL_REF_MIN_FREQ 25000000UL
38 #define PLL_REF_MAX_FREQ 235000000UL
40 #define PLL_STAGE1_MIN_FREQ 1600000000UL
41 #define PLL_STAGE1_MAX_FREQ 2400000000UL
43 #define PLL_STAGE1_REF_MIN_FREQ 25000000UL
44 #define PLL_STAGE1_REF_MAX_FREQ 54000000UL
46 #define PLL_STAGE2_MIN_FREQ 1200000000UL
47 #define PLL_STAGE2_MAX_FREQ 2400000000UL
49 #define PLL_STAGE2_REF_MIN_FREQ 54000000UL
50 #define PLL_STAGE2_REF_MAX_FREQ 75000000UL
52 #define PLL_OUT_MIN_FREQ 20000000UL
53 #define PLL_OUT_MAX_FREQ 1200000000UL
55 #define PLL_DIVR1_MAX 7
56 #define PLL_DIVR2_MAX 63
57 #define PLL_DIVF1_MAX 63
58 #define PLL_DIVF2_MAX 63
59 #define PLL_DIVQ_MAX 63
61 #define PLL_BYPASS_NONE 0x0
62 #define PLL_BYPASS1 0x2
63 #define PLL_BYPASS2 0x1
65 #define SSCG_PLL_BYPASS1_MASK BIT(5)
66 #define SSCG_PLL_BYPASS2_MASK BIT(4)
67 #define SSCG_PLL_BYPASS_MASK GENMASK(5, 4)
69 #define PLL_SCCG_LOCK_TIMEOUT 70
71 struct clk_sscg_pll_setup
{
82 uint64_t fout_request
;
88 const struct clk_ops ops
;
90 struct clk_sscg_pll_setup setup
;
96 #define to_clk_sscg_pll(_hw) container_of(_hw, struct clk_sscg_pll, hw)
98 static int clk_sscg_pll_wait_lock(struct clk_sscg_pll
*pll
)
102 val
= readl_relaxed(pll
->base
+ PLL_CFG0
);
104 /* don't wait for lock if all plls are bypassed */
105 if (!(val
& SSCG_PLL_BYPASS2_MASK
))
106 return readl_poll_timeout(pll
->base
, val
, val
& PLL_LOCK_MASK
,
107 0, PLL_SCCG_LOCK_TIMEOUT
);
112 static int clk_sscg_pll2_check_match(struct clk_sscg_pll_setup
*setup
,
113 struct clk_sscg_pll_setup
*temp_setup
)
115 int new_diff
= temp_setup
->fout
- temp_setup
->fout_request
;
116 int diff
= temp_setup
->fout_error
;
118 if (abs(diff
) > abs(new_diff
)) {
119 temp_setup
->fout_error
= new_diff
;
120 memcpy(setup
, temp_setup
, sizeof(struct clk_sscg_pll_setup
));
122 if (temp_setup
->fout_request
== temp_setup
->fout
)
128 static int clk_sscg_divq_lookup(struct clk_sscg_pll_setup
*setup
,
129 struct clk_sscg_pll_setup
*temp_setup
)
133 for (temp_setup
->divq
= 0; temp_setup
->divq
<= PLL_DIVQ_MAX
;
134 temp_setup
->divq
++) {
135 temp_setup
->vco2
= temp_setup
->vco1
;
136 do_div(temp_setup
->vco2
, temp_setup
->divr2
+ 1);
137 temp_setup
->vco2
*= 2;
138 temp_setup
->vco2
*= temp_setup
->divf2
+ 1;
139 if (temp_setup
->vco2
>= PLL_STAGE2_MIN_FREQ
&&
140 temp_setup
->vco2
<= PLL_STAGE2_MAX_FREQ
) {
141 temp_setup
->fout
= temp_setup
->vco2
;
142 do_div(temp_setup
->fout
, 2 * (temp_setup
->divq
+ 1));
144 ret
= clk_sscg_pll2_check_match(setup
, temp_setup
);
146 temp_setup
->bypass
= PLL_BYPASS1
;
155 static int clk_sscg_divf2_lookup(struct clk_sscg_pll_setup
*setup
,
156 struct clk_sscg_pll_setup
*temp_setup
)
160 for (temp_setup
->divf2
= 0; temp_setup
->divf2
<= PLL_DIVF2_MAX
;
161 temp_setup
->divf2
++) {
162 ret
= clk_sscg_divq_lookup(setup
, temp_setup
);
170 static int clk_sscg_divr2_lookup(struct clk_sscg_pll_setup
*setup
,
171 struct clk_sscg_pll_setup
*temp_setup
)
175 for (temp_setup
->divr2
= 0; temp_setup
->divr2
<= PLL_DIVR2_MAX
;
176 temp_setup
->divr2
++) {
177 temp_setup
->ref_div2
= temp_setup
->vco1
;
178 do_div(temp_setup
->ref_div2
, temp_setup
->divr2
+ 1);
179 if (temp_setup
->ref_div2
>= PLL_STAGE2_REF_MIN_FREQ
&&
180 temp_setup
->ref_div2
<= PLL_STAGE2_REF_MAX_FREQ
) {
181 ret
= clk_sscg_divf2_lookup(setup
, temp_setup
);
190 static int clk_sscg_pll2_find_setup(struct clk_sscg_pll_setup
*setup
,
191 struct clk_sscg_pll_setup
*temp_setup
,
196 if (ref
< PLL_STAGE1_MIN_FREQ
|| ref
> PLL_STAGE1_MAX_FREQ
)
199 temp_setup
->vco1
= ref
;
201 ret
= clk_sscg_divr2_lookup(setup
, temp_setup
);
205 static int clk_sscg_divf1_lookup(struct clk_sscg_pll_setup
*setup
,
206 struct clk_sscg_pll_setup
*temp_setup
)
210 for (temp_setup
->divf1
= 0; temp_setup
->divf1
<= PLL_DIVF1_MAX
;
211 temp_setup
->divf1
++) {
212 uint64_t vco1
= temp_setup
->ref
;
214 do_div(vco1
, temp_setup
->divr1
+ 1);
216 vco1
*= temp_setup
->divf1
+ 1;
218 ret
= clk_sscg_pll2_find_setup(setup
, temp_setup
, vco1
);
220 temp_setup
->bypass
= PLL_BYPASS_NONE
;
228 static int clk_sscg_divr1_lookup(struct clk_sscg_pll_setup
*setup
,
229 struct clk_sscg_pll_setup
*temp_setup
)
233 for (temp_setup
->divr1
= 0; temp_setup
->divr1
<= PLL_DIVR1_MAX
;
234 temp_setup
->divr1
++) {
235 temp_setup
->ref_div1
= temp_setup
->ref
;
236 do_div(temp_setup
->ref_div1
, temp_setup
->divr1
+ 1);
237 if (temp_setup
->ref_div1
>= PLL_STAGE1_REF_MIN_FREQ
&&
238 temp_setup
->ref_div1
<= PLL_STAGE1_REF_MAX_FREQ
) {
239 ret
= clk_sscg_divf1_lookup(setup
, temp_setup
);
248 static int clk_sscg_pll1_find_setup(struct clk_sscg_pll_setup
*setup
,
249 struct clk_sscg_pll_setup
*temp_setup
,
254 if (ref
< PLL_REF_MIN_FREQ
|| ref
> PLL_REF_MAX_FREQ
)
257 temp_setup
->ref
= ref
;
259 ret
= clk_sscg_divr1_lookup(setup
, temp_setup
);
264 static int clk_sscg_pll_find_setup(struct clk_sscg_pll_setup
*setup
,
266 uint64_t rate
, int try_bypass
)
268 struct clk_sscg_pll_setup temp_setup
;
271 memset(&temp_setup
, 0, sizeof(struct clk_sscg_pll_setup
));
272 memset(setup
, 0, sizeof(struct clk_sscg_pll_setup
));
274 temp_setup
.fout_error
= PLL_OUT_MAX_FREQ
;
275 temp_setup
.fout_request
= rate
;
277 switch (try_bypass
) {
280 setup
->bypass
= PLL_BYPASS2
;
286 ret
= clk_sscg_pll2_find_setup(setup
, &temp_setup
, prate
);
288 case PLL_BYPASS_NONE
:
289 ret
= clk_sscg_pll1_find_setup(setup
, &temp_setup
, prate
);
296 static int clk_sscg_pll_is_prepared(struct clk_hw
*hw
)
298 struct clk_sscg_pll
*pll
= to_clk_sscg_pll(hw
);
300 u32 val
= readl_relaxed(pll
->base
+ PLL_CFG0
);
302 return (val
& PLL_PD_MASK
) ? 0 : 1;
305 static int clk_sscg_pll_prepare(struct clk_hw
*hw
)
307 struct clk_sscg_pll
*pll
= to_clk_sscg_pll(hw
);
310 val
= readl_relaxed(pll
->base
+ PLL_CFG0
);
312 writel_relaxed(val
, pll
->base
+ PLL_CFG0
);
314 return clk_sscg_pll_wait_lock(pll
);
317 static void clk_sscg_pll_unprepare(struct clk_hw
*hw
)
319 struct clk_sscg_pll
*pll
= to_clk_sscg_pll(hw
);
322 val
= readl_relaxed(pll
->base
+ PLL_CFG0
);
324 writel_relaxed(val
, pll
->base
+ PLL_CFG0
);
327 static unsigned long clk_sscg_pll_recalc_rate(struct clk_hw
*hw
,
328 unsigned long parent_rate
)
330 struct clk_sscg_pll
*pll
= to_clk_sscg_pll(hw
);
331 u32 val
, divr1
, divf1
, divr2
, divf2
, divq
;
334 val
= readl_relaxed(pll
->base
+ PLL_CFG2
);
335 divr1
= FIELD_GET(PLL_DIVR1_MASK
, val
);
336 divr2
= FIELD_GET(PLL_DIVR2_MASK
, val
);
337 divf1
= FIELD_GET(PLL_DIVF1_MASK
, val
);
338 divf2
= FIELD_GET(PLL_DIVF2_MASK
, val
);
339 divq
= FIELD_GET(PLL_DIVQ_MASK
, val
);
341 temp64
= parent_rate
;
343 val
= readl(pll
->base
+ PLL_CFG0
);
344 if (val
& SSCG_PLL_BYPASS2_MASK
) {
345 temp64
= parent_rate
;
346 } else if (val
& SSCG_PLL_BYPASS1_MASK
) {
348 do_div(temp64
, (divr2
+ 1) * (divq
+ 1));
351 temp64
*= (divf1
+ 1) * (divf2
+ 1);
352 do_div(temp64
, (divr1
+ 1) * (divr2
+ 1) * (divq
+ 1));
358 static int clk_sscg_pll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
359 unsigned long parent_rate
)
361 struct clk_sscg_pll
*pll
= to_clk_sscg_pll(hw
);
362 struct clk_sscg_pll_setup
*setup
= &pll
->setup
;
365 /* set bypass here too since the parent might be the same */
366 val
= readl(pll
->base
+ PLL_CFG0
);
367 val
&= ~SSCG_PLL_BYPASS_MASK
;
368 val
|= FIELD_PREP(SSCG_PLL_BYPASS_MASK
, setup
->bypass
);
369 writel(val
, pll
->base
+ PLL_CFG0
);
371 val
= readl_relaxed(pll
->base
+ PLL_CFG2
);
372 val
&= ~(PLL_DIVF1_MASK
| PLL_DIVF2_MASK
);
373 val
&= ~(PLL_DIVR1_MASK
| PLL_DIVR2_MASK
| PLL_DIVQ_MASK
);
374 val
|= FIELD_PREP(PLL_DIVF1_MASK
, setup
->divf1
);
375 val
|= FIELD_PREP(PLL_DIVF2_MASK
, setup
->divf2
);
376 val
|= FIELD_PREP(PLL_DIVR1_MASK
, setup
->divr1
);
377 val
|= FIELD_PREP(PLL_DIVR2_MASK
, setup
->divr2
);
378 val
|= FIELD_PREP(PLL_DIVQ_MASK
, setup
->divq
);
379 writel_relaxed(val
, pll
->base
+ PLL_CFG2
);
381 return clk_sscg_pll_wait_lock(pll
);
384 static u8
clk_sscg_pll_get_parent(struct clk_hw
*hw
)
386 struct clk_sscg_pll
*pll
= to_clk_sscg_pll(hw
);
388 u8 ret
= pll
->parent
;
390 val
= readl(pll
->base
+ PLL_CFG0
);
391 if (val
& SSCG_PLL_BYPASS2_MASK
)
393 else if (val
& SSCG_PLL_BYPASS1_MASK
)
398 static int clk_sscg_pll_set_parent(struct clk_hw
*hw
, u8 index
)
400 struct clk_sscg_pll
*pll
= to_clk_sscg_pll(hw
);
403 val
= readl(pll
->base
+ PLL_CFG0
);
404 val
&= ~SSCG_PLL_BYPASS_MASK
;
405 val
|= FIELD_PREP(SSCG_PLL_BYPASS_MASK
, pll
->setup
.bypass
);
406 writel(val
, pll
->base
+ PLL_CFG0
);
408 return clk_sscg_pll_wait_lock(pll
);
411 static int __clk_sscg_pll_determine_rate(struct clk_hw
*hw
,
412 struct clk_rate_request
*req
,
418 struct clk_sscg_pll
*pll
= to_clk_sscg_pll(hw
);
419 struct clk_sscg_pll_setup
*setup
= &pll
->setup
;
420 struct clk_hw
*parent_hw
= NULL
;
421 int bypass_parent_index
;
429 bypass_parent_index
= pll
->bypass2
;
432 bypass_parent_index
= pll
->bypass1
;
435 bypass_parent_index
= pll
->parent
;
439 parent_hw
= clk_hw_get_parent_by_index(hw
, bypass_parent_index
);
440 ret
= __clk_determine_rate(parent_hw
, req
);
442 ret
= clk_sscg_pll_find_setup(setup
, req
->rate
,
446 req
->best_parent_hw
= parent_hw
;
447 req
->best_parent_rate
= req
->rate
;
448 req
->rate
= setup
->fout
;
453 static int clk_sscg_pll_determine_rate(struct clk_hw
*hw
,
454 struct clk_rate_request
*req
)
456 struct clk_sscg_pll
*pll
= to_clk_sscg_pll(hw
);
457 struct clk_sscg_pll_setup
*setup
= &pll
->setup
;
458 uint64_t rate
= req
->rate
;
459 uint64_t min
= req
->min_rate
;
460 uint64_t max
= req
->max_rate
;
463 if (rate
< PLL_OUT_MIN_FREQ
|| rate
> PLL_OUT_MAX_FREQ
)
466 ret
= __clk_sscg_pll_determine_rate(hw
, req
, req
->rate
, req
->rate
,
471 ret
= __clk_sscg_pll_determine_rate(hw
, req
, PLL_STAGE1_REF_MIN_FREQ
,
472 PLL_STAGE1_REF_MAX_FREQ
, rate
,
477 ret
= __clk_sscg_pll_determine_rate(hw
, req
, PLL_REF_MIN_FREQ
,
478 PLL_REF_MAX_FREQ
, rate
,
483 if (setup
->fout
>= min
&& setup
->fout
<= max
)
489 static const struct clk_ops clk_sscg_pll_ops
= {
490 .prepare
= clk_sscg_pll_prepare
,
491 .unprepare
= clk_sscg_pll_unprepare
,
492 .is_prepared
= clk_sscg_pll_is_prepared
,
493 .recalc_rate
= clk_sscg_pll_recalc_rate
,
494 .set_rate
= clk_sscg_pll_set_rate
,
495 .set_parent
= clk_sscg_pll_set_parent
,
496 .get_parent
= clk_sscg_pll_get_parent
,
497 .determine_rate
= clk_sscg_pll_determine_rate
,
500 struct clk_hw
*imx_clk_hw_sscg_pll(const char *name
,
501 const char * const *parent_names
,
503 u8 parent
, u8 bypass1
, u8 bypass2
,
507 struct clk_sscg_pll
*pll
;
508 struct clk_init_data init
;
512 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
514 return ERR_PTR(-ENOMEM
);
516 pll
->parent
= parent
;
517 pll
->bypass1
= bypass1
;
518 pll
->bypass2
= bypass2
;
522 init
.ops
= &clk_sscg_pll_ops
;
525 init
.parent_names
= parent_names
;
526 init
.num_parents
= num_parents
;
529 pll
->hw
.init
= &init
;
533 ret
= clk_hw_register(NULL
, hw
);
541 EXPORT_SYMBOL_GPL(imx_clk_hw_sscg_pll
);