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/iopoll.h>
14 #include <linux/slab.h>
15 #include <linux/bitfield.h>
24 #define PLL_DIVF1_MASK GENMASK(18, 13)
25 #define PLL_DIVF2_MASK GENMASK(12, 7)
26 #define PLL_DIVR1_MASK GENMASK(27, 25)
27 #define PLL_DIVR2_MASK GENMASK(24, 19)
28 #define PLL_DIVQ_MASK GENMASK(6, 1)
29 #define PLL_REF_MASK GENMASK(2, 0)
31 #define PLL_LOCK_MASK BIT(31)
32 #define PLL_PD_MASK BIT(7)
34 /* These are the specification limits for the SSCG PLL */
35 #define PLL_REF_MIN_FREQ 25000000UL
36 #define PLL_REF_MAX_FREQ 235000000UL
38 #define PLL_STAGE1_MIN_FREQ 1600000000UL
39 #define PLL_STAGE1_MAX_FREQ 2400000000UL
41 #define PLL_STAGE1_REF_MIN_FREQ 25000000UL
42 #define PLL_STAGE1_REF_MAX_FREQ 54000000UL
44 #define PLL_STAGE2_MIN_FREQ 1200000000UL
45 #define PLL_STAGE2_MAX_FREQ 2400000000UL
47 #define PLL_STAGE2_REF_MIN_FREQ 54000000UL
48 #define PLL_STAGE2_REF_MAX_FREQ 75000000UL
50 #define PLL_OUT_MIN_FREQ 20000000UL
51 #define PLL_OUT_MAX_FREQ 1200000000UL
53 #define PLL_DIVR1_MAX 7
54 #define PLL_DIVR2_MAX 63
55 #define PLL_DIVF1_MAX 63
56 #define PLL_DIVF2_MAX 63
57 #define PLL_DIVQ_MAX 63
59 #define PLL_BYPASS_NONE 0x0
60 #define PLL_BYPASS1 0x2
61 #define PLL_BYPASS2 0x1
63 #define SSCG_PLL_BYPASS1_MASK BIT(5)
64 #define SSCG_PLL_BYPASS2_MASK BIT(4)
65 #define SSCG_PLL_BYPASS_MASK GENMASK(5, 4)
67 #define PLL_SCCG_LOCK_TIMEOUT 70
69 struct clk_sccg_pll_setup {
81 uint64_t fout_request;
87 const struct clk_ops ops;
91 struct clk_sccg_pll_setup setup;
98 #define to_clk_sccg_pll(_hw) container_of(_hw, struct clk_sccg_pll, hw)
100 static int clk_sccg_pll_wait_lock(struct clk_sccg_pll *pll)
104 val = readl_relaxed(pll->base + PLL_CFG0);
106 /* don't wait for lock if all plls are bypassed */
107 if (!(val & SSCG_PLL_BYPASS2_MASK))
108 return readl_poll_timeout(pll->base, val, val & PLL_LOCK_MASK,
109 0, PLL_SCCG_LOCK_TIMEOUT);
114 static int clk_sccg_pll2_check_match(struct clk_sccg_pll_setup *setup,
115 struct clk_sccg_pll_setup *temp_setup)
117 int new_diff = temp_setup->fout - temp_setup->fout_request;
118 int diff = temp_setup->fout_error;
120 if (abs(diff) > abs(new_diff)) {
121 temp_setup->fout_error = new_diff;
122 memcpy(setup, temp_setup, sizeof(struct clk_sccg_pll_setup));
124 if (temp_setup->fout_request == temp_setup->fout)
130 static int clk_sccg_divq_lookup(struct clk_sccg_pll_setup *setup,
131 struct clk_sccg_pll_setup *temp_setup)
135 for (temp_setup->divq = 0; temp_setup->divq <= PLL_DIVQ_MAX;
136 temp_setup->divq++) {
137 temp_setup->vco2 = temp_setup->vco1;
138 do_div(temp_setup->vco2, temp_setup->divr2 + 1);
139 temp_setup->vco2 *= 2;
140 temp_setup->vco2 *= temp_setup->divf2 + 1;
141 if (temp_setup->vco2 >= PLL_STAGE2_MIN_FREQ &&
142 temp_setup->vco2 <= PLL_STAGE2_MAX_FREQ) {
143 temp_setup->fout = temp_setup->vco2;
144 do_div(temp_setup->fout, 2 * (temp_setup->divq + 1));
146 ret = clk_sccg_pll2_check_match(setup, temp_setup);
148 temp_setup->bypass = PLL_BYPASS1;
157 static int clk_sccg_divf2_lookup(struct clk_sccg_pll_setup *setup,
158 struct clk_sccg_pll_setup *temp_setup)
162 for (temp_setup->divf2 = 0; temp_setup->divf2 <= PLL_DIVF2_MAX;
163 temp_setup->divf2++) {
164 ret = clk_sccg_divq_lookup(setup, temp_setup);
172 static int clk_sccg_divr2_lookup(struct clk_sccg_pll_setup *setup,
173 struct clk_sccg_pll_setup *temp_setup)
177 for (temp_setup->divr2 = 0; temp_setup->divr2 <= PLL_DIVR2_MAX;
178 temp_setup->divr2++) {
179 temp_setup->ref_div2 = temp_setup->vco1;
180 do_div(temp_setup->ref_div2, temp_setup->divr2 + 1);
181 if (temp_setup->ref_div2 >= PLL_STAGE2_REF_MIN_FREQ &&
182 temp_setup->ref_div2 <= PLL_STAGE2_REF_MAX_FREQ) {
183 ret = clk_sccg_divf2_lookup(setup, temp_setup);
192 static int clk_sccg_pll2_find_setup(struct clk_sccg_pll_setup *setup,
193 struct clk_sccg_pll_setup *temp_setup,
199 if (ref < PLL_STAGE1_MIN_FREQ || ref > PLL_STAGE1_MAX_FREQ)
202 temp_setup->vco1 = ref;
204 ret = clk_sccg_divr2_lookup(setup, temp_setup);
208 static int clk_sccg_divf1_lookup(struct clk_sccg_pll_setup *setup,
209 struct clk_sccg_pll_setup *temp_setup)
213 for (temp_setup->divf1 = 0; temp_setup->divf1 <= PLL_DIVF1_MAX;
214 temp_setup->divf1++) {
215 uint64_t vco1 = temp_setup->ref;
217 do_div(vco1, temp_setup->divr1 + 1);
219 vco1 *= temp_setup->divf1 + 1;
221 ret = clk_sccg_pll2_find_setup(setup, temp_setup, vco1);
223 temp_setup->bypass = PLL_BYPASS_NONE;
231 static int clk_sccg_divr1_lookup(struct clk_sccg_pll_setup *setup,
232 struct clk_sccg_pll_setup *temp_setup)
236 for (temp_setup->divr1 = 0; temp_setup->divr1 <= PLL_DIVR1_MAX;
237 temp_setup->divr1++) {
238 temp_setup->ref_div1 = temp_setup->ref;
239 do_div(temp_setup->ref_div1, temp_setup->divr1 + 1);
240 if (temp_setup->ref_div1 >= PLL_STAGE1_REF_MIN_FREQ &&
241 temp_setup->ref_div1 <= PLL_STAGE1_REF_MAX_FREQ) {
242 ret = clk_sccg_divf1_lookup(setup, temp_setup);
251 static int clk_sccg_pll1_find_setup(struct clk_sccg_pll_setup *setup,
252 struct clk_sccg_pll_setup *temp_setup,
258 if (ref < PLL_REF_MIN_FREQ || ref > PLL_REF_MAX_FREQ)
261 temp_setup->ref = ref;
263 ret = clk_sccg_divr1_lookup(setup, temp_setup);
268 static int clk_sccg_pll_find_setup(struct clk_sccg_pll_setup *setup,
270 uint64_t rate, int try_bypass)
272 struct clk_sccg_pll_setup temp_setup;
275 memset(&temp_setup, 0, sizeof(struct clk_sccg_pll_setup));
276 memset(setup, 0, sizeof(struct clk_sccg_pll_setup));
278 temp_setup.fout_error = PLL_OUT_MAX_FREQ;
279 temp_setup.fout_request = rate;
281 switch (try_bypass) {
285 setup->bypass = PLL_BYPASS2;
292 ret = clk_sccg_pll2_find_setup(setup, &temp_setup, prate);
295 case PLL_BYPASS_NONE:
296 ret = clk_sccg_pll1_find_setup(setup, &temp_setup, prate);
304 static int clk_sccg_pll_is_prepared(struct clk_hw *hw)
306 struct clk_sccg_pll *pll = to_clk_sccg_pll(hw);
308 u32 val = readl_relaxed(pll->base + PLL_CFG0);
310 return (val & PLL_PD_MASK) ? 0 : 1;
313 static int clk_sccg_pll_prepare(struct clk_hw *hw)
315 struct clk_sccg_pll *pll = to_clk_sccg_pll(hw);
318 val = readl_relaxed(pll->base + PLL_CFG0);
320 writel_relaxed(val, pll->base + PLL_CFG0);
322 return clk_sccg_pll_wait_lock(pll);
325 static void clk_sccg_pll_unprepare(struct clk_hw *hw)
327 struct clk_sccg_pll *pll = to_clk_sccg_pll(hw);
330 val = readl_relaxed(pll->base + PLL_CFG0);
332 writel_relaxed(val, pll->base + PLL_CFG0);
335 static unsigned long clk_sccg_pll_recalc_rate(struct clk_hw *hw,
336 unsigned long parent_rate)
338 struct clk_sccg_pll *pll = to_clk_sccg_pll(hw);
339 u32 val, divr1, divf1, divr2, divf2, divq;
342 val = readl_relaxed(pll->base + PLL_CFG2);
343 divr1 = FIELD_GET(PLL_DIVR1_MASK, val);
344 divr2 = FIELD_GET(PLL_DIVR2_MASK, val);
345 divf1 = FIELD_GET(PLL_DIVF1_MASK, val);
346 divf2 = FIELD_GET(PLL_DIVF2_MASK, val);
347 divq = FIELD_GET(PLL_DIVQ_MASK, val);
349 temp64 = parent_rate;
351 val = clk_readl(pll->base + PLL_CFG0);
352 if (val & SSCG_PLL_BYPASS2_MASK) {
353 temp64 = parent_rate;
354 } else if (val & SSCG_PLL_BYPASS1_MASK) {
356 do_div(temp64, (divr2 + 1) * (divq + 1));
359 temp64 *= (divf1 + 1) * (divf2 + 1);
360 do_div(temp64, (divr1 + 1) * (divr2 + 1) * (divq + 1));
366 static int clk_sccg_pll_set_rate(struct clk_hw *hw, unsigned long rate,
367 unsigned long parent_rate)
369 struct clk_sccg_pll *pll = to_clk_sccg_pll(hw);
370 struct clk_sccg_pll_setup *setup = &pll->setup;
373 /* set bypass here too since the parent might be the same */
374 val = clk_readl(pll->base + PLL_CFG0);
375 val &= ~SSCG_PLL_BYPASS_MASK;
376 val |= FIELD_PREP(SSCG_PLL_BYPASS_MASK, setup->bypass);
377 clk_writel(val, pll->base + PLL_CFG0);
379 val = readl_relaxed(pll->base + PLL_CFG2);
380 val &= ~(PLL_DIVF1_MASK | PLL_DIVF2_MASK);
381 val &= ~(PLL_DIVR1_MASK | PLL_DIVR2_MASK | PLL_DIVQ_MASK);
382 val |= FIELD_PREP(PLL_DIVF1_MASK, setup->divf1);
383 val |= FIELD_PREP(PLL_DIVF2_MASK, setup->divf2);
384 val |= FIELD_PREP(PLL_DIVR1_MASK, setup->divr1);
385 val |= FIELD_PREP(PLL_DIVR2_MASK, setup->divr2);
386 val |= FIELD_PREP(PLL_DIVQ_MASK, setup->divq);
387 writel_relaxed(val, pll->base + PLL_CFG2);
389 return clk_sccg_pll_wait_lock(pll);
392 static u8 clk_sccg_pll_get_parent(struct clk_hw *hw)
394 struct clk_sccg_pll *pll = to_clk_sccg_pll(hw);
396 u8 ret = pll->parent;
398 val = clk_readl(pll->base + PLL_CFG0);
399 if (val & SSCG_PLL_BYPASS2_MASK)
401 else if (val & SSCG_PLL_BYPASS1_MASK)
406 static int clk_sccg_pll_set_parent(struct clk_hw *hw, u8 index)
408 struct clk_sccg_pll *pll = to_clk_sccg_pll(hw);
411 val = clk_readl(pll->base + PLL_CFG0);
412 val &= ~SSCG_PLL_BYPASS_MASK;
413 val |= FIELD_PREP(SSCG_PLL_BYPASS_MASK, pll->setup.bypass);
414 clk_writel(val, pll->base + PLL_CFG0);
416 return clk_sccg_pll_wait_lock(pll);
419 static int __clk_sccg_pll_determine_rate(struct clk_hw *hw,
420 struct clk_rate_request *req,
426 struct clk_sccg_pll *pll = to_clk_sccg_pll(hw);
427 struct clk_sccg_pll_setup *setup = &pll->setup;
428 struct clk_hw *parent_hw = NULL;
429 int bypass_parent_index;
437 bypass_parent_index = pll->bypass2;
440 bypass_parent_index = pll->bypass1;
443 bypass_parent_index = pll->parent;
447 parent_hw = clk_hw_get_parent_by_index(hw, bypass_parent_index);
448 ret = __clk_determine_rate(parent_hw, req);
450 ret = clk_sccg_pll_find_setup(setup, req->rate,
454 req->best_parent_hw = parent_hw;
455 req->best_parent_rate = req->rate;
456 req->rate = setup->fout;
461 static int clk_sccg_pll_determine_rate(struct clk_hw *hw,
462 struct clk_rate_request *req)
464 struct clk_sccg_pll *pll = to_clk_sccg_pll(hw);
465 struct clk_sccg_pll_setup *setup = &pll->setup;
466 uint64_t rate = req->rate;
467 uint64_t min = req->min_rate;
468 uint64_t max = req->max_rate;
471 if (rate < PLL_OUT_MIN_FREQ || rate > PLL_OUT_MAX_FREQ)
474 ret = __clk_sccg_pll_determine_rate(hw, req, req->rate, req->rate,
479 ret = __clk_sccg_pll_determine_rate(hw, req, PLL_STAGE1_REF_MIN_FREQ,
480 PLL_STAGE1_REF_MAX_FREQ, rate,
485 ret = __clk_sccg_pll_determine_rate(hw, req, PLL_REF_MIN_FREQ,
486 PLL_REF_MAX_FREQ, rate,
491 if (setup->fout >= min && setup->fout <= max)
497 static const struct clk_ops clk_sccg_pll_ops = {
498 .prepare = clk_sccg_pll_prepare,
499 .unprepare = clk_sccg_pll_unprepare,
500 .is_prepared = clk_sccg_pll_is_prepared,
501 .recalc_rate = clk_sccg_pll_recalc_rate,
502 .set_rate = clk_sccg_pll_set_rate,
503 .set_parent = clk_sccg_pll_set_parent,
504 .get_parent = clk_sccg_pll_get_parent,
505 .determine_rate = clk_sccg_pll_determine_rate,
508 struct clk *imx_clk_sccg_pll(const char *name,
509 const char * const *parent_names,
511 u8 parent, u8 bypass1, u8 bypass2,
515 struct clk_sccg_pll *pll;
516 struct clk_init_data init;
520 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
522 return ERR_PTR(-ENOMEM);
524 pll->parent = parent;
525 pll->bypass1 = bypass1;
526 pll->bypass2 = bypass2;
530 init.ops = &clk_sccg_pll_ops;
533 init.parent_names = parent_names;
534 init.num_parents = num_parents;
537 pll->hw.init = &init;
541 ret = clk_hw_register(NULL, hw);