1ba7928d9SAbel Vesa // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2ba7928d9SAbel Vesa /*
3ba7928d9SAbel Vesa * Copyright 2018 NXP.
4ba7928d9SAbel Vesa *
5ba7928d9SAbel Vesa * This driver supports the SCCG plls found in the imx8m SOCs
6ba7928d9SAbel Vesa *
7ba7928d9SAbel Vesa * Documentation for this SCCG pll can be found at:
8ba7928d9SAbel Vesa * https://www.nxp.com/docs/en/reference-manual/IMX8MDQLQRM.pdf#page=834
9ba7928d9SAbel Vesa */
10ba7928d9SAbel Vesa
11ba7928d9SAbel Vesa #include <linux/clk-provider.h>
12ba7928d9SAbel Vesa #include <linux/err.h>
13*870ed5e2SAnson Huang #include <linux/export.h>
14ba7928d9SAbel Vesa #include <linux/io.h>
15ba7928d9SAbel Vesa #include <linux/iopoll.h>
16ba7928d9SAbel Vesa #include <linux/slab.h>
17ba7928d9SAbel Vesa #include <linux/bitfield.h>
18ba7928d9SAbel Vesa
19ba7928d9SAbel Vesa #include "clk.h"
20ba7928d9SAbel Vesa
21ba7928d9SAbel Vesa /* PLL CFGs */
22ba7928d9SAbel Vesa #define PLL_CFG0 0x0
23ba7928d9SAbel Vesa #define PLL_CFG1 0x4
24ba7928d9SAbel Vesa #define PLL_CFG2 0x8
25ba7928d9SAbel Vesa
26ba7928d9SAbel Vesa #define PLL_DIVF1_MASK GENMASK(18, 13)
27ba7928d9SAbel Vesa #define PLL_DIVF2_MASK GENMASK(12, 7)
28ba7928d9SAbel Vesa #define PLL_DIVR1_MASK GENMASK(27, 25)
29ba7928d9SAbel Vesa #define PLL_DIVR2_MASK GENMASK(24, 19)
30ba7928d9SAbel Vesa #define PLL_DIVQ_MASK GENMASK(6, 1)
31ba7928d9SAbel Vesa #define PLL_REF_MASK GENMASK(2, 0)
32ba7928d9SAbel Vesa
33ba7928d9SAbel Vesa #define PLL_LOCK_MASK BIT(31)
34ba7928d9SAbel Vesa #define PLL_PD_MASK BIT(7)
35ba7928d9SAbel Vesa
36ba7928d9SAbel Vesa /* These are the specification limits for the SSCG PLL */
37ba7928d9SAbel Vesa #define PLL_REF_MIN_FREQ 25000000UL
38ba7928d9SAbel Vesa #define PLL_REF_MAX_FREQ 235000000UL
39ba7928d9SAbel Vesa
40ba7928d9SAbel Vesa #define PLL_STAGE1_MIN_FREQ 1600000000UL
41ba7928d9SAbel Vesa #define PLL_STAGE1_MAX_FREQ 2400000000UL
42ba7928d9SAbel Vesa
43ba7928d9SAbel Vesa #define PLL_STAGE1_REF_MIN_FREQ 25000000UL
44ba7928d9SAbel Vesa #define PLL_STAGE1_REF_MAX_FREQ 54000000UL
45ba7928d9SAbel Vesa
46ba7928d9SAbel Vesa #define PLL_STAGE2_MIN_FREQ 1200000000UL
47ba7928d9SAbel Vesa #define PLL_STAGE2_MAX_FREQ 2400000000UL
48ba7928d9SAbel Vesa
49ba7928d9SAbel Vesa #define PLL_STAGE2_REF_MIN_FREQ 54000000UL
50ba7928d9SAbel Vesa #define PLL_STAGE2_REF_MAX_FREQ 75000000UL
51ba7928d9SAbel Vesa
52ba7928d9SAbel Vesa #define PLL_OUT_MIN_FREQ 20000000UL
53ba7928d9SAbel Vesa #define PLL_OUT_MAX_FREQ 1200000000UL
54ba7928d9SAbel Vesa
55ba7928d9SAbel Vesa #define PLL_DIVR1_MAX 7
56ba7928d9SAbel Vesa #define PLL_DIVR2_MAX 63
57ba7928d9SAbel Vesa #define PLL_DIVF1_MAX 63
58ba7928d9SAbel Vesa #define PLL_DIVF2_MAX 63
59ba7928d9SAbel Vesa #define PLL_DIVQ_MAX 63
60ba7928d9SAbel Vesa
61ba7928d9SAbel Vesa #define PLL_BYPASS_NONE 0x0
62ba7928d9SAbel Vesa #define PLL_BYPASS1 0x2
63ba7928d9SAbel Vesa #define PLL_BYPASS2 0x1
64ba7928d9SAbel Vesa
65ba7928d9SAbel Vesa #define SSCG_PLL_BYPASS1_MASK BIT(5)
66ba7928d9SAbel Vesa #define SSCG_PLL_BYPASS2_MASK BIT(4)
67ba7928d9SAbel Vesa #define SSCG_PLL_BYPASS_MASK GENMASK(5, 4)
68ba7928d9SAbel Vesa
69ba7928d9SAbel Vesa #define PLL_SCCG_LOCK_TIMEOUT 70
70ba7928d9SAbel Vesa
71ba7928d9SAbel Vesa struct clk_sscg_pll_setup {
72ba7928d9SAbel Vesa int divr1, divf1;
73ba7928d9SAbel Vesa int divr2, divf2;
74ba7928d9SAbel Vesa int divq;
75ba7928d9SAbel Vesa int bypass;
76ba7928d9SAbel Vesa uint64_t vco1;
77ba7928d9SAbel Vesa uint64_t vco2;
78ba7928d9SAbel Vesa uint64_t fout;
79ba7928d9SAbel Vesa uint64_t ref;
80ba7928d9SAbel Vesa uint64_t ref_div1;
81ba7928d9SAbel Vesa uint64_t ref_div2;
82ba7928d9SAbel Vesa uint64_t fout_request;
83ba7928d9SAbel Vesa int fout_error;
84ba7928d9SAbel Vesa };
85ba7928d9SAbel Vesa
86ba7928d9SAbel Vesa struct clk_sscg_pll {
87ba7928d9SAbel Vesa struct clk_hw hw;
88ba7928d9SAbel Vesa const struct clk_ops ops;
89ba7928d9SAbel Vesa void __iomem *base;
90ba7928d9SAbel Vesa struct clk_sscg_pll_setup setup;
91ba7928d9SAbel Vesa u8 parent;
92ba7928d9SAbel Vesa u8 bypass1;
93ba7928d9SAbel Vesa u8 bypass2;
94ba7928d9SAbel Vesa };
95ba7928d9SAbel Vesa
96ba7928d9SAbel Vesa #define to_clk_sscg_pll(_hw) container_of(_hw, struct clk_sscg_pll, hw)
97ba7928d9SAbel Vesa
clk_sscg_pll_wait_lock(struct clk_sscg_pll * pll)98ba7928d9SAbel Vesa static int clk_sscg_pll_wait_lock(struct clk_sscg_pll *pll)
99ba7928d9SAbel Vesa {
100ba7928d9SAbel Vesa u32 val;
101ba7928d9SAbel Vesa
102ba7928d9SAbel Vesa val = readl_relaxed(pll->base + PLL_CFG0);
103ba7928d9SAbel Vesa
104ba7928d9SAbel Vesa /* don't wait for lock if all plls are bypassed */
105ba7928d9SAbel Vesa if (!(val & SSCG_PLL_BYPASS2_MASK))
106ba7928d9SAbel Vesa return readl_poll_timeout(pll->base, val, val & PLL_LOCK_MASK,
107ba7928d9SAbel Vesa 0, PLL_SCCG_LOCK_TIMEOUT);
108ba7928d9SAbel Vesa
109ba7928d9SAbel Vesa return 0;
110ba7928d9SAbel Vesa }
111ba7928d9SAbel Vesa
clk_sscg_pll2_check_match(struct clk_sscg_pll_setup * setup,struct clk_sscg_pll_setup * temp_setup)112ba7928d9SAbel Vesa static int clk_sscg_pll2_check_match(struct clk_sscg_pll_setup *setup,
113ba7928d9SAbel Vesa struct clk_sscg_pll_setup *temp_setup)
114ba7928d9SAbel Vesa {
115ba7928d9SAbel Vesa int new_diff = temp_setup->fout - temp_setup->fout_request;
116ba7928d9SAbel Vesa int diff = temp_setup->fout_error;
117ba7928d9SAbel Vesa
118ba7928d9SAbel Vesa if (abs(diff) > abs(new_diff)) {
119ba7928d9SAbel Vesa temp_setup->fout_error = new_diff;
120ba7928d9SAbel Vesa memcpy(setup, temp_setup, sizeof(struct clk_sscg_pll_setup));
121ba7928d9SAbel Vesa
122ba7928d9SAbel Vesa if (temp_setup->fout_request == temp_setup->fout)
123ba7928d9SAbel Vesa return 0;
124ba7928d9SAbel Vesa }
125ba7928d9SAbel Vesa return -1;
126ba7928d9SAbel Vesa }
127ba7928d9SAbel Vesa
clk_sscg_divq_lookup(struct clk_sscg_pll_setup * setup,struct clk_sscg_pll_setup * temp_setup)128ba7928d9SAbel Vesa static int clk_sscg_divq_lookup(struct clk_sscg_pll_setup *setup,
129ba7928d9SAbel Vesa struct clk_sscg_pll_setup *temp_setup)
130ba7928d9SAbel Vesa {
131ba7928d9SAbel Vesa int ret = -EINVAL;
132ba7928d9SAbel Vesa
133ba7928d9SAbel Vesa for (temp_setup->divq = 0; temp_setup->divq <= PLL_DIVQ_MAX;
134ba7928d9SAbel Vesa temp_setup->divq++) {
135ba7928d9SAbel Vesa temp_setup->vco2 = temp_setup->vco1;
136ba7928d9SAbel Vesa do_div(temp_setup->vco2, temp_setup->divr2 + 1);
137ba7928d9SAbel Vesa temp_setup->vco2 *= 2;
138ba7928d9SAbel Vesa temp_setup->vco2 *= temp_setup->divf2 + 1;
139ba7928d9SAbel Vesa if (temp_setup->vco2 >= PLL_STAGE2_MIN_FREQ &&
140ba7928d9SAbel Vesa temp_setup->vco2 <= PLL_STAGE2_MAX_FREQ) {
141ba7928d9SAbel Vesa temp_setup->fout = temp_setup->vco2;
142ba7928d9SAbel Vesa do_div(temp_setup->fout, 2 * (temp_setup->divq + 1));
143ba7928d9SAbel Vesa
144ba7928d9SAbel Vesa ret = clk_sscg_pll2_check_match(setup, temp_setup);
145ba7928d9SAbel Vesa if (!ret) {
146ba7928d9SAbel Vesa temp_setup->bypass = PLL_BYPASS1;
147ba7928d9SAbel Vesa return ret;
148ba7928d9SAbel Vesa }
149ba7928d9SAbel Vesa }
150ba7928d9SAbel Vesa }
151ba7928d9SAbel Vesa
152ba7928d9SAbel Vesa return ret;
153ba7928d9SAbel Vesa }
154ba7928d9SAbel Vesa
clk_sscg_divf2_lookup(struct clk_sscg_pll_setup * setup,struct clk_sscg_pll_setup * temp_setup)155ba7928d9SAbel Vesa static int clk_sscg_divf2_lookup(struct clk_sscg_pll_setup *setup,
156ba7928d9SAbel Vesa struct clk_sscg_pll_setup *temp_setup)
157ba7928d9SAbel Vesa {
158ba7928d9SAbel Vesa int ret = -EINVAL;
159ba7928d9SAbel Vesa
160ba7928d9SAbel Vesa for (temp_setup->divf2 = 0; temp_setup->divf2 <= PLL_DIVF2_MAX;
161ba7928d9SAbel Vesa temp_setup->divf2++) {
162ba7928d9SAbel Vesa ret = clk_sscg_divq_lookup(setup, temp_setup);
163ba7928d9SAbel Vesa if (!ret)
164ba7928d9SAbel Vesa return ret;
165ba7928d9SAbel Vesa }
166ba7928d9SAbel Vesa
167ba7928d9SAbel Vesa return ret;
168ba7928d9SAbel Vesa }
169ba7928d9SAbel Vesa
clk_sscg_divr2_lookup(struct clk_sscg_pll_setup * setup,struct clk_sscg_pll_setup * temp_setup)170ba7928d9SAbel Vesa static int clk_sscg_divr2_lookup(struct clk_sscg_pll_setup *setup,
171ba7928d9SAbel Vesa struct clk_sscg_pll_setup *temp_setup)
172ba7928d9SAbel Vesa {
173ba7928d9SAbel Vesa int ret = -EINVAL;
174ba7928d9SAbel Vesa
175ba7928d9SAbel Vesa for (temp_setup->divr2 = 0; temp_setup->divr2 <= PLL_DIVR2_MAX;
176ba7928d9SAbel Vesa temp_setup->divr2++) {
177ba7928d9SAbel Vesa temp_setup->ref_div2 = temp_setup->vco1;
178ba7928d9SAbel Vesa do_div(temp_setup->ref_div2, temp_setup->divr2 + 1);
179ba7928d9SAbel Vesa if (temp_setup->ref_div2 >= PLL_STAGE2_REF_MIN_FREQ &&
180ba7928d9SAbel Vesa temp_setup->ref_div2 <= PLL_STAGE2_REF_MAX_FREQ) {
181ba7928d9SAbel Vesa ret = clk_sscg_divf2_lookup(setup, temp_setup);
182ba7928d9SAbel Vesa if (!ret)
183ba7928d9SAbel Vesa return ret;
184ba7928d9SAbel Vesa }
185ba7928d9SAbel Vesa }
186ba7928d9SAbel Vesa
187ba7928d9SAbel Vesa return ret;
188ba7928d9SAbel Vesa }
189ba7928d9SAbel Vesa
clk_sscg_pll2_find_setup(struct clk_sscg_pll_setup * setup,struct clk_sscg_pll_setup * temp_setup,uint64_t ref)190ba7928d9SAbel Vesa static int clk_sscg_pll2_find_setup(struct clk_sscg_pll_setup *setup,
191ba7928d9SAbel Vesa struct clk_sscg_pll_setup *temp_setup,
192ba7928d9SAbel Vesa uint64_t ref)
193ba7928d9SAbel Vesa {
194eeca5721SAnson Huang int ret;
195ba7928d9SAbel Vesa
196ba7928d9SAbel Vesa if (ref < PLL_STAGE1_MIN_FREQ || ref > PLL_STAGE1_MAX_FREQ)
197eeca5721SAnson Huang return -EINVAL;
198ba7928d9SAbel Vesa
199ba7928d9SAbel Vesa temp_setup->vco1 = ref;
200ba7928d9SAbel Vesa
201ba7928d9SAbel Vesa ret = clk_sscg_divr2_lookup(setup, temp_setup);
202ba7928d9SAbel Vesa return ret;
203ba7928d9SAbel Vesa }
204ba7928d9SAbel Vesa
clk_sscg_divf1_lookup(struct clk_sscg_pll_setup * setup,struct clk_sscg_pll_setup * temp_setup)205ba7928d9SAbel Vesa static int clk_sscg_divf1_lookup(struct clk_sscg_pll_setup *setup,
206ba7928d9SAbel Vesa struct clk_sscg_pll_setup *temp_setup)
207ba7928d9SAbel Vesa {
208ba7928d9SAbel Vesa int ret = -EINVAL;
209ba7928d9SAbel Vesa
210ba7928d9SAbel Vesa for (temp_setup->divf1 = 0; temp_setup->divf1 <= PLL_DIVF1_MAX;
211ba7928d9SAbel Vesa temp_setup->divf1++) {
212ba7928d9SAbel Vesa uint64_t vco1 = temp_setup->ref;
213ba7928d9SAbel Vesa
214ba7928d9SAbel Vesa do_div(vco1, temp_setup->divr1 + 1);
215ba7928d9SAbel Vesa vco1 *= 2;
216ba7928d9SAbel Vesa vco1 *= temp_setup->divf1 + 1;
217ba7928d9SAbel Vesa
218ba7928d9SAbel Vesa ret = clk_sscg_pll2_find_setup(setup, temp_setup, vco1);
219ba7928d9SAbel Vesa if (!ret) {
220ba7928d9SAbel Vesa temp_setup->bypass = PLL_BYPASS_NONE;
221ba7928d9SAbel Vesa return ret;
222ba7928d9SAbel Vesa }
223ba7928d9SAbel Vesa }
224ba7928d9SAbel Vesa
225ba7928d9SAbel Vesa return ret;
226ba7928d9SAbel Vesa }
227ba7928d9SAbel Vesa
clk_sscg_divr1_lookup(struct clk_sscg_pll_setup * setup,struct clk_sscg_pll_setup * temp_setup)228ba7928d9SAbel Vesa static int clk_sscg_divr1_lookup(struct clk_sscg_pll_setup *setup,
229ba7928d9SAbel Vesa struct clk_sscg_pll_setup *temp_setup)
230ba7928d9SAbel Vesa {
231ba7928d9SAbel Vesa int ret = -EINVAL;
232ba7928d9SAbel Vesa
233ba7928d9SAbel Vesa for (temp_setup->divr1 = 0; temp_setup->divr1 <= PLL_DIVR1_MAX;
234ba7928d9SAbel Vesa temp_setup->divr1++) {
235ba7928d9SAbel Vesa temp_setup->ref_div1 = temp_setup->ref;
236ba7928d9SAbel Vesa do_div(temp_setup->ref_div1, temp_setup->divr1 + 1);
237ba7928d9SAbel Vesa if (temp_setup->ref_div1 >= PLL_STAGE1_REF_MIN_FREQ &&
238ba7928d9SAbel Vesa temp_setup->ref_div1 <= PLL_STAGE1_REF_MAX_FREQ) {
239ba7928d9SAbel Vesa ret = clk_sscg_divf1_lookup(setup, temp_setup);
240ba7928d9SAbel Vesa if (!ret)
241ba7928d9SAbel Vesa return ret;
242ba7928d9SAbel Vesa }
243ba7928d9SAbel Vesa }
244ba7928d9SAbel Vesa
245ba7928d9SAbel Vesa return ret;
246ba7928d9SAbel Vesa }
247ba7928d9SAbel Vesa
clk_sscg_pll1_find_setup(struct clk_sscg_pll_setup * setup,struct clk_sscg_pll_setup * temp_setup,uint64_t ref)248ba7928d9SAbel Vesa static int clk_sscg_pll1_find_setup(struct clk_sscg_pll_setup *setup,
249ba7928d9SAbel Vesa struct clk_sscg_pll_setup *temp_setup,
250ba7928d9SAbel Vesa uint64_t ref)
251ba7928d9SAbel Vesa {
252eeca5721SAnson Huang int ret;
253ba7928d9SAbel Vesa
254ba7928d9SAbel Vesa if (ref < PLL_REF_MIN_FREQ || ref > PLL_REF_MAX_FREQ)
255eeca5721SAnson Huang return -EINVAL;
256ba7928d9SAbel Vesa
257ba7928d9SAbel Vesa temp_setup->ref = ref;
258ba7928d9SAbel Vesa
259ba7928d9SAbel Vesa ret = clk_sscg_divr1_lookup(setup, temp_setup);
260ba7928d9SAbel Vesa
261ba7928d9SAbel Vesa return ret;
262ba7928d9SAbel Vesa }
263ba7928d9SAbel Vesa
clk_sscg_pll_find_setup(struct clk_sscg_pll_setup * setup,uint64_t prate,uint64_t rate,int try_bypass)264ba7928d9SAbel Vesa static int clk_sscg_pll_find_setup(struct clk_sscg_pll_setup *setup,
265ba7928d9SAbel Vesa uint64_t prate,
266ba7928d9SAbel Vesa uint64_t rate, int try_bypass)
267ba7928d9SAbel Vesa {
268ba7928d9SAbel Vesa struct clk_sscg_pll_setup temp_setup;
269ba7928d9SAbel Vesa int ret = -EINVAL;
270ba7928d9SAbel Vesa
271ba7928d9SAbel Vesa memset(&temp_setup, 0, sizeof(struct clk_sscg_pll_setup));
272ba7928d9SAbel Vesa memset(setup, 0, sizeof(struct clk_sscg_pll_setup));
273ba7928d9SAbel Vesa
274ba7928d9SAbel Vesa temp_setup.fout_error = PLL_OUT_MAX_FREQ;
275ba7928d9SAbel Vesa temp_setup.fout_request = rate;
276ba7928d9SAbel Vesa
277ba7928d9SAbel Vesa switch (try_bypass) {
278ba7928d9SAbel Vesa case PLL_BYPASS2:
279ba7928d9SAbel Vesa if (prate == rate) {
280ba7928d9SAbel Vesa setup->bypass = PLL_BYPASS2;
281ba7928d9SAbel Vesa setup->fout = rate;
282ba7928d9SAbel Vesa ret = 0;
283ba7928d9SAbel Vesa }
284ba7928d9SAbel Vesa break;
285ba7928d9SAbel Vesa case PLL_BYPASS1:
286ba7928d9SAbel Vesa ret = clk_sscg_pll2_find_setup(setup, &temp_setup, prate);
287ba7928d9SAbel Vesa break;
288ba7928d9SAbel Vesa case PLL_BYPASS_NONE:
289ba7928d9SAbel Vesa ret = clk_sscg_pll1_find_setup(setup, &temp_setup, prate);
290ba7928d9SAbel Vesa break;
291ba7928d9SAbel Vesa }
292ba7928d9SAbel Vesa
293ba7928d9SAbel Vesa return ret;
294ba7928d9SAbel Vesa }
295ba7928d9SAbel Vesa
clk_sscg_pll_is_prepared(struct clk_hw * hw)296ba7928d9SAbel Vesa static int clk_sscg_pll_is_prepared(struct clk_hw *hw)
297ba7928d9SAbel Vesa {
298ba7928d9SAbel Vesa struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
299ba7928d9SAbel Vesa
300ba7928d9SAbel Vesa u32 val = readl_relaxed(pll->base + PLL_CFG0);
301ba7928d9SAbel Vesa
302ba7928d9SAbel Vesa return (val & PLL_PD_MASK) ? 0 : 1;
303ba7928d9SAbel Vesa }
304ba7928d9SAbel Vesa
clk_sscg_pll_prepare(struct clk_hw * hw)305ba7928d9SAbel Vesa static int clk_sscg_pll_prepare(struct clk_hw *hw)
306ba7928d9SAbel Vesa {
307ba7928d9SAbel Vesa struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
308ba7928d9SAbel Vesa u32 val;
309ba7928d9SAbel Vesa
310ba7928d9SAbel Vesa val = readl_relaxed(pll->base + PLL_CFG0);
311ba7928d9SAbel Vesa val &= ~PLL_PD_MASK;
312ba7928d9SAbel Vesa writel_relaxed(val, pll->base + PLL_CFG0);
313ba7928d9SAbel Vesa
314ba7928d9SAbel Vesa return clk_sscg_pll_wait_lock(pll);
315ba7928d9SAbel Vesa }
316ba7928d9SAbel Vesa
clk_sscg_pll_unprepare(struct clk_hw * hw)317ba7928d9SAbel Vesa static void clk_sscg_pll_unprepare(struct clk_hw *hw)
318ba7928d9SAbel Vesa {
319ba7928d9SAbel Vesa struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
320ba7928d9SAbel Vesa u32 val;
321ba7928d9SAbel Vesa
322ba7928d9SAbel Vesa val = readl_relaxed(pll->base + PLL_CFG0);
323ba7928d9SAbel Vesa val |= PLL_PD_MASK;
324ba7928d9SAbel Vesa writel_relaxed(val, pll->base + PLL_CFG0);
325ba7928d9SAbel Vesa }
326ba7928d9SAbel Vesa
clk_sscg_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)327ba7928d9SAbel Vesa static unsigned long clk_sscg_pll_recalc_rate(struct clk_hw *hw,
328ba7928d9SAbel Vesa unsigned long parent_rate)
329ba7928d9SAbel Vesa {
330ba7928d9SAbel Vesa struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
331ba7928d9SAbel Vesa u32 val, divr1, divf1, divr2, divf2, divq;
332ba7928d9SAbel Vesa u64 temp64;
333ba7928d9SAbel Vesa
334ba7928d9SAbel Vesa val = readl_relaxed(pll->base + PLL_CFG2);
335ba7928d9SAbel Vesa divr1 = FIELD_GET(PLL_DIVR1_MASK, val);
336ba7928d9SAbel Vesa divr2 = FIELD_GET(PLL_DIVR2_MASK, val);
337ba7928d9SAbel Vesa divf1 = FIELD_GET(PLL_DIVF1_MASK, val);
338ba7928d9SAbel Vesa divf2 = FIELD_GET(PLL_DIVF2_MASK, val);
339ba7928d9SAbel Vesa divq = FIELD_GET(PLL_DIVQ_MASK, val);
340ba7928d9SAbel Vesa
341ba7928d9SAbel Vesa temp64 = parent_rate;
342ba7928d9SAbel Vesa
343ba7928d9SAbel Vesa val = readl(pll->base + PLL_CFG0);
344ba7928d9SAbel Vesa if (val & SSCG_PLL_BYPASS2_MASK) {
345ba7928d9SAbel Vesa temp64 = parent_rate;
346ba7928d9SAbel Vesa } else if (val & SSCG_PLL_BYPASS1_MASK) {
347ba7928d9SAbel Vesa temp64 *= divf2;
348ba7928d9SAbel Vesa do_div(temp64, (divr2 + 1) * (divq + 1));
349ba7928d9SAbel Vesa } else {
350ba7928d9SAbel Vesa temp64 *= 2;
351ba7928d9SAbel Vesa temp64 *= (divf1 + 1) * (divf2 + 1);
352ba7928d9SAbel Vesa do_div(temp64, (divr1 + 1) * (divr2 + 1) * (divq + 1));
353ba7928d9SAbel Vesa }
354ba7928d9SAbel Vesa
355ba7928d9SAbel Vesa return temp64;
356ba7928d9SAbel Vesa }
357ba7928d9SAbel Vesa
clk_sscg_pll_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)358ba7928d9SAbel Vesa static int clk_sscg_pll_set_rate(struct clk_hw *hw, unsigned long rate,
359ba7928d9SAbel Vesa unsigned long parent_rate)
360ba7928d9SAbel Vesa {
361ba7928d9SAbel Vesa struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
362ba7928d9SAbel Vesa struct clk_sscg_pll_setup *setup = &pll->setup;
363ba7928d9SAbel Vesa u32 val;
364ba7928d9SAbel Vesa
365ba7928d9SAbel Vesa /* set bypass here too since the parent might be the same */
366ba7928d9SAbel Vesa val = readl(pll->base + PLL_CFG0);
367ba7928d9SAbel Vesa val &= ~SSCG_PLL_BYPASS_MASK;
368ba7928d9SAbel Vesa val |= FIELD_PREP(SSCG_PLL_BYPASS_MASK, setup->bypass);
369ba7928d9SAbel Vesa writel(val, pll->base + PLL_CFG0);
370ba7928d9SAbel Vesa
371ba7928d9SAbel Vesa val = readl_relaxed(pll->base + PLL_CFG2);
372ba7928d9SAbel Vesa val &= ~(PLL_DIVF1_MASK | PLL_DIVF2_MASK);
373ba7928d9SAbel Vesa val &= ~(PLL_DIVR1_MASK | PLL_DIVR2_MASK | PLL_DIVQ_MASK);
374ba7928d9SAbel Vesa val |= FIELD_PREP(PLL_DIVF1_MASK, setup->divf1);
375ba7928d9SAbel Vesa val |= FIELD_PREP(PLL_DIVF2_MASK, setup->divf2);
376ba7928d9SAbel Vesa val |= FIELD_PREP(PLL_DIVR1_MASK, setup->divr1);
377ba7928d9SAbel Vesa val |= FIELD_PREP(PLL_DIVR2_MASK, setup->divr2);
378ba7928d9SAbel Vesa val |= FIELD_PREP(PLL_DIVQ_MASK, setup->divq);
379ba7928d9SAbel Vesa writel_relaxed(val, pll->base + PLL_CFG2);
380ba7928d9SAbel Vesa
381ba7928d9SAbel Vesa return clk_sscg_pll_wait_lock(pll);
382ba7928d9SAbel Vesa }
383ba7928d9SAbel Vesa
clk_sscg_pll_get_parent(struct clk_hw * hw)384ba7928d9SAbel Vesa static u8 clk_sscg_pll_get_parent(struct clk_hw *hw)
385ba7928d9SAbel Vesa {
386ba7928d9SAbel Vesa struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
387ba7928d9SAbel Vesa u32 val;
388ba7928d9SAbel Vesa u8 ret = pll->parent;
389ba7928d9SAbel Vesa
390ba7928d9SAbel Vesa val = readl(pll->base + PLL_CFG0);
391ba7928d9SAbel Vesa if (val & SSCG_PLL_BYPASS2_MASK)
392ba7928d9SAbel Vesa ret = pll->bypass2;
393ba7928d9SAbel Vesa else if (val & SSCG_PLL_BYPASS1_MASK)
394ba7928d9SAbel Vesa ret = pll->bypass1;
395ba7928d9SAbel Vesa return ret;
396ba7928d9SAbel Vesa }
397ba7928d9SAbel Vesa
clk_sscg_pll_set_parent(struct clk_hw * hw,u8 index)398ba7928d9SAbel Vesa static int clk_sscg_pll_set_parent(struct clk_hw *hw, u8 index)
399ba7928d9SAbel Vesa {
400ba7928d9SAbel Vesa struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
401ba7928d9SAbel Vesa u32 val;
402ba7928d9SAbel Vesa
403ba7928d9SAbel Vesa val = readl(pll->base + PLL_CFG0);
404ba7928d9SAbel Vesa val &= ~SSCG_PLL_BYPASS_MASK;
405ba7928d9SAbel Vesa val |= FIELD_PREP(SSCG_PLL_BYPASS_MASK, pll->setup.bypass);
406ba7928d9SAbel Vesa writel(val, pll->base + PLL_CFG0);
407ba7928d9SAbel Vesa
408ba7928d9SAbel Vesa return clk_sscg_pll_wait_lock(pll);
409ba7928d9SAbel Vesa }
410ba7928d9SAbel Vesa
__clk_sscg_pll_determine_rate(struct clk_hw * hw,struct clk_rate_request * req,uint64_t min,uint64_t max,uint64_t rate,int bypass)411ba7928d9SAbel Vesa static int __clk_sscg_pll_determine_rate(struct clk_hw *hw,
412ba7928d9SAbel Vesa struct clk_rate_request *req,
413ba7928d9SAbel Vesa uint64_t min,
414ba7928d9SAbel Vesa uint64_t max,
415ba7928d9SAbel Vesa uint64_t rate,
416ba7928d9SAbel Vesa int bypass)
417ba7928d9SAbel Vesa {
418ba7928d9SAbel Vesa struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
419ba7928d9SAbel Vesa struct clk_sscg_pll_setup *setup = &pll->setup;
420ba7928d9SAbel Vesa struct clk_hw *parent_hw = NULL;
421ba7928d9SAbel Vesa int bypass_parent_index;
422eeca5721SAnson Huang int ret;
423ba7928d9SAbel Vesa
424ba7928d9SAbel Vesa req->max_rate = max;
425ba7928d9SAbel Vesa req->min_rate = min;
426ba7928d9SAbel Vesa
427ba7928d9SAbel Vesa switch (bypass) {
428ba7928d9SAbel Vesa case PLL_BYPASS2:
429ba7928d9SAbel Vesa bypass_parent_index = pll->bypass2;
430ba7928d9SAbel Vesa break;
431ba7928d9SAbel Vesa case PLL_BYPASS1:
432ba7928d9SAbel Vesa bypass_parent_index = pll->bypass1;
433ba7928d9SAbel Vesa break;
434ba7928d9SAbel Vesa default:
435ba7928d9SAbel Vesa bypass_parent_index = pll->parent;
436ba7928d9SAbel Vesa break;
437ba7928d9SAbel Vesa }
438ba7928d9SAbel Vesa
439ba7928d9SAbel Vesa parent_hw = clk_hw_get_parent_by_index(hw, bypass_parent_index);
440ba7928d9SAbel Vesa ret = __clk_determine_rate(parent_hw, req);
441ba7928d9SAbel Vesa if (!ret) {
442ba7928d9SAbel Vesa ret = clk_sscg_pll_find_setup(setup, req->rate,
443ba7928d9SAbel Vesa rate, bypass);
444ba7928d9SAbel Vesa }
445ba7928d9SAbel Vesa
446ba7928d9SAbel Vesa req->best_parent_hw = parent_hw;
447ba7928d9SAbel Vesa req->best_parent_rate = req->rate;
448ba7928d9SAbel Vesa req->rate = setup->fout;
449ba7928d9SAbel Vesa
450ba7928d9SAbel Vesa return ret;
451ba7928d9SAbel Vesa }
452ba7928d9SAbel Vesa
clk_sscg_pll_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)453ba7928d9SAbel Vesa static int clk_sscg_pll_determine_rate(struct clk_hw *hw,
454ba7928d9SAbel Vesa struct clk_rate_request *req)
455ba7928d9SAbel Vesa {
456ba7928d9SAbel Vesa struct clk_sscg_pll *pll = to_clk_sscg_pll(hw);
457ba7928d9SAbel Vesa struct clk_sscg_pll_setup *setup = &pll->setup;
458ba7928d9SAbel Vesa uint64_t rate = req->rate;
459ba7928d9SAbel Vesa uint64_t min = req->min_rate;
460ba7928d9SAbel Vesa uint64_t max = req->max_rate;
461eeca5721SAnson Huang int ret;
462ba7928d9SAbel Vesa
463ba7928d9SAbel Vesa if (rate < PLL_OUT_MIN_FREQ || rate > PLL_OUT_MAX_FREQ)
464eeca5721SAnson Huang return -EINVAL;
465ba7928d9SAbel Vesa
466ba7928d9SAbel Vesa ret = __clk_sscg_pll_determine_rate(hw, req, req->rate, req->rate,
467ba7928d9SAbel Vesa rate, PLL_BYPASS2);
468ba7928d9SAbel Vesa if (!ret)
469ba7928d9SAbel Vesa return ret;
470ba7928d9SAbel Vesa
471ba7928d9SAbel Vesa ret = __clk_sscg_pll_determine_rate(hw, req, PLL_STAGE1_REF_MIN_FREQ,
472ba7928d9SAbel Vesa PLL_STAGE1_REF_MAX_FREQ, rate,
473ba7928d9SAbel Vesa PLL_BYPASS1);
474ba7928d9SAbel Vesa if (!ret)
475ba7928d9SAbel Vesa return ret;
476ba7928d9SAbel Vesa
477ba7928d9SAbel Vesa ret = __clk_sscg_pll_determine_rate(hw, req, PLL_REF_MIN_FREQ,
478ba7928d9SAbel Vesa PLL_REF_MAX_FREQ, rate,
479ba7928d9SAbel Vesa PLL_BYPASS_NONE);
480ba7928d9SAbel Vesa if (!ret)
481ba7928d9SAbel Vesa return ret;
482ba7928d9SAbel Vesa
483ba7928d9SAbel Vesa if (setup->fout >= min && setup->fout <= max)
484ba7928d9SAbel Vesa ret = 0;
485ba7928d9SAbel Vesa
486ba7928d9SAbel Vesa return ret;
487ba7928d9SAbel Vesa }
488ba7928d9SAbel Vesa
489ba7928d9SAbel Vesa static const struct clk_ops clk_sscg_pll_ops = {
490ba7928d9SAbel Vesa .prepare = clk_sscg_pll_prepare,
491ba7928d9SAbel Vesa .unprepare = clk_sscg_pll_unprepare,
492ba7928d9SAbel Vesa .is_prepared = clk_sscg_pll_is_prepared,
493ba7928d9SAbel Vesa .recalc_rate = clk_sscg_pll_recalc_rate,
494ba7928d9SAbel Vesa .set_rate = clk_sscg_pll_set_rate,
495ba7928d9SAbel Vesa .set_parent = clk_sscg_pll_set_parent,
496ba7928d9SAbel Vesa .get_parent = clk_sscg_pll_get_parent,
497ba7928d9SAbel Vesa .determine_rate = clk_sscg_pll_determine_rate,
498ba7928d9SAbel Vesa };
499ba7928d9SAbel Vesa
imx_clk_hw_sscg_pll(const char * name,const char * const * parent_names,u8 num_parents,u8 parent,u8 bypass1,u8 bypass2,void __iomem * base,unsigned long flags)500179c1f7cSAbel Vesa struct clk_hw *imx_clk_hw_sscg_pll(const char *name,
501ba7928d9SAbel Vesa const char * const *parent_names,
502ba7928d9SAbel Vesa u8 num_parents,
503ba7928d9SAbel Vesa u8 parent, u8 bypass1, u8 bypass2,
504ba7928d9SAbel Vesa void __iomem *base,
505ba7928d9SAbel Vesa unsigned long flags)
506ba7928d9SAbel Vesa {
507ba7928d9SAbel Vesa struct clk_sscg_pll *pll;
508ba7928d9SAbel Vesa struct clk_init_data init;
509ba7928d9SAbel Vesa struct clk_hw *hw;
510ba7928d9SAbel Vesa int ret;
511ba7928d9SAbel Vesa
512ba7928d9SAbel Vesa pll = kzalloc(sizeof(*pll), GFP_KERNEL);
513ba7928d9SAbel Vesa if (!pll)
514ba7928d9SAbel Vesa return ERR_PTR(-ENOMEM);
515ba7928d9SAbel Vesa
516ba7928d9SAbel Vesa pll->parent = parent;
517ba7928d9SAbel Vesa pll->bypass1 = bypass1;
518ba7928d9SAbel Vesa pll->bypass2 = bypass2;
519ba7928d9SAbel Vesa
520ba7928d9SAbel Vesa pll->base = base;
521ba7928d9SAbel Vesa init.name = name;
522ba7928d9SAbel Vesa init.ops = &clk_sscg_pll_ops;
523ba7928d9SAbel Vesa
524ba7928d9SAbel Vesa init.flags = flags;
525ba7928d9SAbel Vesa init.parent_names = parent_names;
526ba7928d9SAbel Vesa init.num_parents = num_parents;
527ba7928d9SAbel Vesa
528ba7928d9SAbel Vesa pll->hw.init = &init;
529ba7928d9SAbel Vesa
530ba7928d9SAbel Vesa hw = &pll->hw;
531ba7928d9SAbel Vesa
532ba7928d9SAbel Vesa ret = clk_hw_register(NULL, hw);
533ba7928d9SAbel Vesa if (ret) {
534ba7928d9SAbel Vesa kfree(pll);
535ba7928d9SAbel Vesa return ERR_PTR(ret);
536ba7928d9SAbel Vesa }
537ba7928d9SAbel Vesa
538179c1f7cSAbel Vesa return hw;
539ba7928d9SAbel Vesa }
540*870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_clk_hw_sscg_pll);
541