Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / clk / imx / clk-pll14xx.c
blob2b5ed86b9dbbbdf19a57119798a8a75fab258354
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2017-2018 NXP.
4 */
6 #include <linux/bits.h>
7 #include <linux/clk-provider.h>
8 #include <linux/err.h>
9 #include <linux/export.h>
10 #include <linux/io.h>
11 #include <linux/iopoll.h>
12 #include <linux/slab.h>
13 #include <linux/jiffies.h>
15 #include "clk.h"
17 #define GNRL_CTL 0x0
18 #define DIV_CTL 0x4
19 #define LOCK_STATUS BIT(31)
20 #define LOCK_SEL_MASK BIT(29)
21 #define CLKE_MASK BIT(11)
22 #define RST_MASK BIT(9)
23 #define BYPASS_MASK BIT(4)
24 #define MDIV_SHIFT 12
25 #define MDIV_MASK GENMASK(21, 12)
26 #define PDIV_SHIFT 4
27 #define PDIV_MASK GENMASK(9, 4)
28 #define SDIV_SHIFT 0
29 #define SDIV_MASK GENMASK(2, 0)
30 #define KDIV_SHIFT 0
31 #define KDIV_MASK GENMASK(15, 0)
33 #define LOCK_TIMEOUT_US 10000
35 struct clk_pll14xx {
36 struct clk_hw hw;
37 void __iomem *base;
38 enum imx_pll14xx_type type;
39 const struct imx_pll14xx_rate_table *rate_table;
40 int rate_count;
43 #define to_clk_pll14xx(_hw) container_of(_hw, struct clk_pll14xx, hw)
45 static const struct imx_pll14xx_rate_table imx_pll1416x_tbl[] = {
46 PLL_1416X_RATE(1800000000U, 225, 3, 0),
47 PLL_1416X_RATE(1600000000U, 200, 3, 0),
48 PLL_1416X_RATE(1500000000U, 375, 3, 1),
49 PLL_1416X_RATE(1400000000U, 350, 3, 1),
50 PLL_1416X_RATE(1200000000U, 300, 3, 1),
51 PLL_1416X_RATE(1000000000U, 250, 3, 1),
52 PLL_1416X_RATE(800000000U, 200, 3, 1),
53 PLL_1416X_RATE(750000000U, 250, 2, 2),
54 PLL_1416X_RATE(700000000U, 350, 3, 2),
55 PLL_1416X_RATE(600000000U, 300, 3, 2),
58 static const struct imx_pll14xx_rate_table imx_pll1443x_tbl[] = {
59 PLL_1443X_RATE(1039500000U, 173, 2, 1, 16384),
60 PLL_1443X_RATE(650000000U, 325, 3, 2, 0),
61 PLL_1443X_RATE(594000000U, 198, 2, 2, 0),
62 PLL_1443X_RATE(519750000U, 173, 2, 2, 16384),
63 PLL_1443X_RATE(393216000U, 262, 2, 3, 9437),
64 PLL_1443X_RATE(361267200U, 361, 3, 3, 17511),
67 struct imx_pll14xx_clk imx_1443x_pll = {
68 .type = PLL_1443X,
69 .rate_table = imx_pll1443x_tbl,
70 .rate_count = ARRAY_SIZE(imx_pll1443x_tbl),
72 EXPORT_SYMBOL_GPL(imx_1443x_pll);
74 struct imx_pll14xx_clk imx_1443x_dram_pll = {
75 .type = PLL_1443X,
76 .rate_table = imx_pll1443x_tbl,
77 .rate_count = ARRAY_SIZE(imx_pll1443x_tbl),
78 .flags = CLK_GET_RATE_NOCACHE,
80 EXPORT_SYMBOL_GPL(imx_1443x_dram_pll);
82 struct imx_pll14xx_clk imx_1416x_pll = {
83 .type = PLL_1416X,
84 .rate_table = imx_pll1416x_tbl,
85 .rate_count = ARRAY_SIZE(imx_pll1416x_tbl),
87 EXPORT_SYMBOL_GPL(imx_1416x_pll);
89 static const struct imx_pll14xx_rate_table *imx_get_pll_settings(
90 struct clk_pll14xx *pll, unsigned long rate)
92 const struct imx_pll14xx_rate_table *rate_table = pll->rate_table;
93 int i;
95 for (i = 0; i < pll->rate_count; i++)
96 if (rate == rate_table[i].rate)
97 return &rate_table[i];
99 return NULL;
102 static long clk_pll14xx_round_rate(struct clk_hw *hw, unsigned long rate,
103 unsigned long *prate)
105 struct clk_pll14xx *pll = to_clk_pll14xx(hw);
106 const struct imx_pll14xx_rate_table *rate_table = pll->rate_table;
107 int i;
109 /* Assumming rate_table is in descending order */
110 for (i = 0; i < pll->rate_count; i++)
111 if (rate >= rate_table[i].rate)
112 return rate_table[i].rate;
114 /* return minimum supported value */
115 return rate_table[i - 1].rate;
118 static unsigned long clk_pll1416x_recalc_rate(struct clk_hw *hw,
119 unsigned long parent_rate)
121 struct clk_pll14xx *pll = to_clk_pll14xx(hw);
122 u32 mdiv, pdiv, sdiv, pll_div;
123 u64 fvco = parent_rate;
125 pll_div = readl_relaxed(pll->base + 4);
126 mdiv = (pll_div & MDIV_MASK) >> MDIV_SHIFT;
127 pdiv = (pll_div & PDIV_MASK) >> PDIV_SHIFT;
128 sdiv = (pll_div & SDIV_MASK) >> SDIV_SHIFT;
130 fvco *= mdiv;
131 do_div(fvco, pdiv << sdiv);
133 return fvco;
136 static unsigned long clk_pll1443x_recalc_rate(struct clk_hw *hw,
137 unsigned long parent_rate)
139 struct clk_pll14xx *pll = to_clk_pll14xx(hw);
140 u32 mdiv, pdiv, sdiv, pll_div_ctl0, pll_div_ctl1;
141 short int kdiv;
142 u64 fvco = parent_rate;
144 pll_div_ctl0 = readl_relaxed(pll->base + 4);
145 pll_div_ctl1 = readl_relaxed(pll->base + 8);
146 mdiv = (pll_div_ctl0 & MDIV_MASK) >> MDIV_SHIFT;
147 pdiv = (pll_div_ctl0 & PDIV_MASK) >> PDIV_SHIFT;
148 sdiv = (pll_div_ctl0 & SDIV_MASK) >> SDIV_SHIFT;
149 kdiv = pll_div_ctl1 & KDIV_MASK;
151 /* fvco = (m * 65536 + k) * Fin / (p * 65536) */
152 fvco *= (mdiv * 65536 + kdiv);
153 pdiv *= 65536;
155 do_div(fvco, pdiv << sdiv);
157 return fvco;
160 static inline bool clk_pll14xx_mp_change(const struct imx_pll14xx_rate_table *rate,
161 u32 pll_div)
163 u32 old_mdiv, old_pdiv;
165 old_mdiv = (pll_div & MDIV_MASK) >> MDIV_SHIFT;
166 old_pdiv = (pll_div & PDIV_MASK) >> PDIV_SHIFT;
168 return rate->mdiv != old_mdiv || rate->pdiv != old_pdiv;
171 static int clk_pll14xx_wait_lock(struct clk_pll14xx *pll)
173 u32 val;
175 return readl_poll_timeout(pll->base, val, val & LOCK_STATUS, 0,
176 LOCK_TIMEOUT_US);
179 static int clk_pll1416x_set_rate(struct clk_hw *hw, unsigned long drate,
180 unsigned long prate)
182 struct clk_pll14xx *pll = to_clk_pll14xx(hw);
183 const struct imx_pll14xx_rate_table *rate;
184 u32 tmp, div_val;
185 int ret;
187 rate = imx_get_pll_settings(pll, drate);
188 if (!rate) {
189 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
190 drate, clk_hw_get_name(hw));
191 return -EINVAL;
194 tmp = readl_relaxed(pll->base + 4);
196 if (!clk_pll14xx_mp_change(rate, tmp)) {
197 tmp &= ~(SDIV_MASK) << SDIV_SHIFT;
198 tmp |= rate->sdiv << SDIV_SHIFT;
199 writel_relaxed(tmp, pll->base + 4);
201 return 0;
204 /* Bypass clock and set lock to pll output lock */
205 tmp = readl_relaxed(pll->base);
206 tmp |= LOCK_SEL_MASK;
207 writel_relaxed(tmp, pll->base);
209 /* Enable RST */
210 tmp &= ~RST_MASK;
211 writel_relaxed(tmp, pll->base);
213 /* Enable BYPASS */
214 tmp |= BYPASS_MASK;
215 writel(tmp, pll->base);
217 div_val = (rate->mdiv << MDIV_SHIFT) | (rate->pdiv << PDIV_SHIFT) |
218 (rate->sdiv << SDIV_SHIFT);
219 writel_relaxed(div_val, pll->base + 0x4);
222 * According to SPEC, t3 - t2 need to be greater than
223 * 1us and 1/FREF, respectively.
224 * FREF is FIN / Prediv, the prediv is [1, 63], so choose
225 * 3us.
227 udelay(3);
229 /* Disable RST */
230 tmp |= RST_MASK;
231 writel_relaxed(tmp, pll->base);
233 /* Wait Lock */
234 ret = clk_pll14xx_wait_lock(pll);
235 if (ret)
236 return ret;
238 /* Bypass */
239 tmp &= ~BYPASS_MASK;
240 writel_relaxed(tmp, pll->base);
242 return 0;
245 static int clk_pll1443x_set_rate(struct clk_hw *hw, unsigned long drate,
246 unsigned long prate)
248 struct clk_pll14xx *pll = to_clk_pll14xx(hw);
249 const struct imx_pll14xx_rate_table *rate;
250 u32 tmp, div_val;
251 int ret;
253 rate = imx_get_pll_settings(pll, drate);
254 if (!rate) {
255 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
256 drate, clk_hw_get_name(hw));
257 return -EINVAL;
260 tmp = readl_relaxed(pll->base + 4);
262 if (!clk_pll14xx_mp_change(rate, tmp)) {
263 tmp &= ~(SDIV_MASK) << SDIV_SHIFT;
264 tmp |= rate->sdiv << SDIV_SHIFT;
265 writel_relaxed(tmp, pll->base + 4);
267 tmp = rate->kdiv << KDIV_SHIFT;
268 writel_relaxed(tmp, pll->base + 8);
270 return 0;
273 /* Enable RST */
274 tmp = readl_relaxed(pll->base);
275 tmp &= ~RST_MASK;
276 writel_relaxed(tmp, pll->base);
278 /* Enable BYPASS */
279 tmp |= BYPASS_MASK;
280 writel_relaxed(tmp, pll->base);
282 div_val = (rate->mdiv << MDIV_SHIFT) | (rate->pdiv << PDIV_SHIFT) |
283 (rate->sdiv << SDIV_SHIFT);
284 writel_relaxed(div_val, pll->base + 0x4);
285 writel_relaxed(rate->kdiv << KDIV_SHIFT, pll->base + 0x8);
288 * According to SPEC, t3 - t2 need to be greater than
289 * 1us and 1/FREF, respectively.
290 * FREF is FIN / Prediv, the prediv is [1, 63], so choose
291 * 3us.
293 udelay(3);
295 /* Disable RST */
296 tmp |= RST_MASK;
297 writel_relaxed(tmp, pll->base);
299 /* Wait Lock*/
300 ret = clk_pll14xx_wait_lock(pll);
301 if (ret)
302 return ret;
304 /* Bypass */
305 tmp &= ~BYPASS_MASK;
306 writel_relaxed(tmp, pll->base);
308 return 0;
311 static int clk_pll14xx_prepare(struct clk_hw *hw)
313 struct clk_pll14xx *pll = to_clk_pll14xx(hw);
314 u32 val;
315 int ret;
318 * RESETB = 1 from 0, PLL starts its normal
319 * operation after lock time
321 val = readl_relaxed(pll->base + GNRL_CTL);
322 if (val & RST_MASK)
323 return 0;
324 val |= BYPASS_MASK;
325 writel_relaxed(val, pll->base + GNRL_CTL);
326 val |= RST_MASK;
327 writel_relaxed(val, pll->base + GNRL_CTL);
329 ret = clk_pll14xx_wait_lock(pll);
330 if (ret)
331 return ret;
333 val &= ~BYPASS_MASK;
334 writel_relaxed(val, pll->base + GNRL_CTL);
336 return 0;
339 static int clk_pll14xx_is_prepared(struct clk_hw *hw)
341 struct clk_pll14xx *pll = to_clk_pll14xx(hw);
342 u32 val;
344 val = readl_relaxed(pll->base + GNRL_CTL);
346 return (val & RST_MASK) ? 1 : 0;
349 static void clk_pll14xx_unprepare(struct clk_hw *hw)
351 struct clk_pll14xx *pll = to_clk_pll14xx(hw);
352 u32 val;
355 * Set RST to 0, power down mode is enabled and
356 * every digital block is reset
358 val = readl_relaxed(pll->base + GNRL_CTL);
359 val &= ~RST_MASK;
360 writel_relaxed(val, pll->base + GNRL_CTL);
363 static const struct clk_ops clk_pll1416x_ops = {
364 .prepare = clk_pll14xx_prepare,
365 .unprepare = clk_pll14xx_unprepare,
366 .is_prepared = clk_pll14xx_is_prepared,
367 .recalc_rate = clk_pll1416x_recalc_rate,
368 .round_rate = clk_pll14xx_round_rate,
369 .set_rate = clk_pll1416x_set_rate,
372 static const struct clk_ops clk_pll1416x_min_ops = {
373 .recalc_rate = clk_pll1416x_recalc_rate,
376 static const struct clk_ops clk_pll1443x_ops = {
377 .prepare = clk_pll14xx_prepare,
378 .unprepare = clk_pll14xx_unprepare,
379 .is_prepared = clk_pll14xx_is_prepared,
380 .recalc_rate = clk_pll1443x_recalc_rate,
381 .round_rate = clk_pll14xx_round_rate,
382 .set_rate = clk_pll1443x_set_rate,
385 struct clk_hw *imx_dev_clk_hw_pll14xx(struct device *dev, const char *name,
386 const char *parent_name, void __iomem *base,
387 const struct imx_pll14xx_clk *pll_clk)
389 struct clk_pll14xx *pll;
390 struct clk_hw *hw;
391 struct clk_init_data init;
392 int ret;
393 u32 val;
395 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
396 if (!pll)
397 return ERR_PTR(-ENOMEM);
399 init.name = name;
400 init.flags = pll_clk->flags;
401 init.parent_names = &parent_name;
402 init.num_parents = 1;
404 switch (pll_clk->type) {
405 case PLL_1416X:
406 if (!pll_clk->rate_table)
407 init.ops = &clk_pll1416x_min_ops;
408 else
409 init.ops = &clk_pll1416x_ops;
410 break;
411 case PLL_1443X:
412 init.ops = &clk_pll1443x_ops;
413 break;
414 default:
415 pr_err("%s: Unknown pll type for pll clk %s\n",
416 __func__, name);
417 kfree(pll);
418 return ERR_PTR(-EINVAL);
421 pll->base = base;
422 pll->hw.init = &init;
423 pll->type = pll_clk->type;
424 pll->rate_table = pll_clk->rate_table;
425 pll->rate_count = pll_clk->rate_count;
427 val = readl_relaxed(pll->base + GNRL_CTL);
428 val &= ~BYPASS_MASK;
429 writel_relaxed(val, pll->base + GNRL_CTL);
431 hw = &pll->hw;
433 ret = clk_hw_register(dev, hw);
434 if (ret) {
435 pr_err("%s: failed to register pll %s %d\n",
436 __func__, name, ret);
437 kfree(pll);
438 return ERR_PTR(ret);
441 return hw;
443 EXPORT_SYMBOL_GPL(imx_dev_clk_hw_pll14xx);