1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2012 Freescale Semiconductor, Inc.
4 * Copyright 2012 Linaro Ltd.
7 #include <linux/clk-provider.h>
8 #include <linux/delay.h>
10 #include <linux/iopoll.h>
11 #include <linux/slab.h>
12 #include <linux/jiffies.h>
13 #include <linux/err.h>
16 #define PLL_NUM_OFFSET 0x10
17 #define PLL_DENOM_OFFSET 0x20
18 #define PLL_IMX7_NUM_OFFSET 0x20
19 #define PLL_IMX7_DENOM_OFFSET 0x30
21 #define PLL_VF610_NUM_OFFSET 0x20
22 #define PLL_VF610_DENOM_OFFSET 0x30
24 #define BM_PLL_POWER (0x1 << 12)
25 #define BM_PLL_LOCK (0x1 << 31)
26 #define IMX7_ENET_PLL_POWER (0x1 << 5)
27 #define IMX7_DDR_PLL_POWER (0x1 << 20)
29 #define PLL_LOCK_TIMEOUT 10000
32 * struct clk_pllv3 - IMX PLL clock version 3
34 * @base: base address of PLL registers
35 * @power_bit: pll power bit mask
36 * @powerup_set: set power_bit to power up the PLL
37 * @div_mask: mask of divider bits
38 * @div_shift: shift of divider bits
39 * @ref_clock: reference clock rate
40 * @num_offset: num register offset
41 * @denom_offset: denom register offset
43 * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
44 * is actually a multiplier, and always sits at bit 0.
53 unsigned long ref_clock
;
58 #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
60 static int clk_pllv3_wait_lock(struct clk_pllv3
*pll
)
62 u32 val
= readl_relaxed(pll
->base
) & pll
->power_bit
;
64 /* No need to wait for lock when pll is not powered up */
65 if ((pll
->powerup_set
&& !val
) || (!pll
->powerup_set
&& val
))
68 return readl_relaxed_poll_timeout(pll
->base
, val
, val
& BM_PLL_LOCK
,
69 500, PLL_LOCK_TIMEOUT
);
72 static int clk_pllv3_prepare(struct clk_hw
*hw
)
74 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
77 val
= readl_relaxed(pll
->base
);
79 val
|= pll
->power_bit
;
81 val
&= ~pll
->power_bit
;
82 writel_relaxed(val
, pll
->base
);
84 return clk_pllv3_wait_lock(pll
);
87 static void clk_pllv3_unprepare(struct clk_hw
*hw
)
89 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
92 val
= readl_relaxed(pll
->base
);
94 val
&= ~pll
->power_bit
;
96 val
|= pll
->power_bit
;
97 writel_relaxed(val
, pll
->base
);
100 static int clk_pllv3_is_prepared(struct clk_hw
*hw
)
102 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
104 if (readl_relaxed(pll
->base
) & BM_PLL_LOCK
)
110 static unsigned long clk_pllv3_recalc_rate(struct clk_hw
*hw
,
111 unsigned long parent_rate
)
113 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
114 u32 div
= (readl_relaxed(pll
->base
) >> pll
->div_shift
) & pll
->div_mask
;
116 return (div
== 1) ? parent_rate
* 22 : parent_rate
* 20;
119 static long clk_pllv3_round_rate(struct clk_hw
*hw
, unsigned long rate
,
120 unsigned long *prate
)
122 unsigned long parent_rate
= *prate
;
124 return (rate
>= parent_rate
* 22) ? parent_rate
* 22 :
128 static int clk_pllv3_set_rate(struct clk_hw
*hw
, unsigned long rate
,
129 unsigned long parent_rate
)
131 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
134 if (rate
== parent_rate
* 22)
136 else if (rate
== parent_rate
* 20)
141 val
= readl_relaxed(pll
->base
);
142 val
&= ~(pll
->div_mask
<< pll
->div_shift
);
143 val
|= (div
<< pll
->div_shift
);
144 writel_relaxed(val
, pll
->base
);
146 return clk_pllv3_wait_lock(pll
);
149 static const struct clk_ops clk_pllv3_ops
= {
150 .prepare
= clk_pllv3_prepare
,
151 .unprepare
= clk_pllv3_unprepare
,
152 .is_prepared
= clk_pllv3_is_prepared
,
153 .recalc_rate
= clk_pllv3_recalc_rate
,
154 .round_rate
= clk_pllv3_round_rate
,
155 .set_rate
= clk_pllv3_set_rate
,
158 static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw
*hw
,
159 unsigned long parent_rate
)
161 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
162 u32 div
= readl_relaxed(pll
->base
) & pll
->div_mask
;
164 return parent_rate
* div
/ 2;
167 static long clk_pllv3_sys_round_rate(struct clk_hw
*hw
, unsigned long rate
,
168 unsigned long *prate
)
170 unsigned long parent_rate
= *prate
;
171 unsigned long min_rate
= parent_rate
* 54 / 2;
172 unsigned long max_rate
= parent_rate
* 108 / 2;
177 else if (rate
< min_rate
)
179 div
= rate
* 2 / parent_rate
;
181 return parent_rate
* div
/ 2;
184 static int clk_pllv3_sys_set_rate(struct clk_hw
*hw
, unsigned long rate
,
185 unsigned long parent_rate
)
187 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
188 unsigned long min_rate
= parent_rate
* 54 / 2;
189 unsigned long max_rate
= parent_rate
* 108 / 2;
192 if (rate
< min_rate
|| rate
> max_rate
)
195 div
= rate
* 2 / parent_rate
;
196 val
= readl_relaxed(pll
->base
);
197 val
&= ~pll
->div_mask
;
199 writel_relaxed(val
, pll
->base
);
201 return clk_pllv3_wait_lock(pll
);
204 static const struct clk_ops clk_pllv3_sys_ops
= {
205 .prepare
= clk_pllv3_prepare
,
206 .unprepare
= clk_pllv3_unprepare
,
207 .is_prepared
= clk_pllv3_is_prepared
,
208 .recalc_rate
= clk_pllv3_sys_recalc_rate
,
209 .round_rate
= clk_pllv3_sys_round_rate
,
210 .set_rate
= clk_pllv3_sys_set_rate
,
213 static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw
*hw
,
214 unsigned long parent_rate
)
216 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
217 u32 mfn
= readl_relaxed(pll
->base
+ pll
->num_offset
);
218 u32 mfd
= readl_relaxed(pll
->base
+ pll
->denom_offset
);
219 u32 div
= readl_relaxed(pll
->base
) & pll
->div_mask
;
220 u64 temp64
= (u64
)parent_rate
;
225 return parent_rate
* div
+ (unsigned long)temp64
;
228 static long clk_pllv3_av_round_rate(struct clk_hw
*hw
, unsigned long rate
,
229 unsigned long *prate
)
231 unsigned long parent_rate
= *prate
;
232 unsigned long min_rate
= parent_rate
* 27;
233 unsigned long max_rate
= parent_rate
* 54;
235 u32 mfn
, mfd
= 1000000;
236 u32 max_mfd
= 0x3FFFFFFF;
241 else if (rate
< min_rate
)
244 if (parent_rate
<= max_mfd
)
247 div
= rate
/ parent_rate
;
248 temp64
= (u64
) (rate
- div
* parent_rate
);
250 do_div(temp64
, parent_rate
);
253 temp64
= (u64
)parent_rate
;
257 return parent_rate
* div
+ (unsigned long)temp64
;
260 static int clk_pllv3_av_set_rate(struct clk_hw
*hw
, unsigned long rate
,
261 unsigned long parent_rate
)
263 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
264 unsigned long min_rate
= parent_rate
* 27;
265 unsigned long max_rate
= parent_rate
* 54;
267 u32 mfn
, mfd
= 1000000;
268 u32 max_mfd
= 0x3FFFFFFF;
271 if (rate
< min_rate
|| rate
> max_rate
)
274 if (parent_rate
<= max_mfd
)
277 div
= rate
/ parent_rate
;
278 temp64
= (u64
) (rate
- div
* parent_rate
);
280 do_div(temp64
, parent_rate
);
283 val
= readl_relaxed(pll
->base
);
284 val
&= ~pll
->div_mask
;
286 writel_relaxed(val
, pll
->base
);
287 writel_relaxed(mfn
, pll
->base
+ pll
->num_offset
);
288 writel_relaxed(mfd
, pll
->base
+ pll
->denom_offset
);
290 return clk_pllv3_wait_lock(pll
);
293 static const struct clk_ops clk_pllv3_av_ops
= {
294 .prepare
= clk_pllv3_prepare
,
295 .unprepare
= clk_pllv3_unprepare
,
296 .is_prepared
= clk_pllv3_is_prepared
,
297 .recalc_rate
= clk_pllv3_av_recalc_rate
,
298 .round_rate
= clk_pllv3_av_round_rate
,
299 .set_rate
= clk_pllv3_av_set_rate
,
302 struct clk_pllv3_vf610_mf
{
303 u32 mfi
; /* integer part, can be 20 or 22 */
304 u32 mfn
; /* numerator, 30-bit value */
305 u32 mfd
; /* denominator, 30-bit value, must be less than mfn */
308 static unsigned long clk_pllv3_vf610_mf_to_rate(unsigned long parent_rate
,
309 struct clk_pllv3_vf610_mf mf
)
313 temp64
= parent_rate
;
315 do_div(temp64
, mf
.mfd
);
317 return (parent_rate
* mf
.mfi
) + temp64
;
320 static struct clk_pllv3_vf610_mf
clk_pllv3_vf610_rate_to_mf(
321 unsigned long parent_rate
, unsigned long rate
)
323 struct clk_pllv3_vf610_mf mf
;
326 mf
.mfi
= (rate
>= 22 * parent_rate
) ? 22 : 20;
327 mf
.mfd
= 0x3fffffff; /* use max supported value for best accuracy */
329 if (rate
<= parent_rate
* mf
.mfi
)
331 else if (rate
>= parent_rate
* (mf
.mfi
+ 1))
334 /* rate = parent_rate * (mfi + mfn/mfd) */
335 temp64
= rate
- parent_rate
* mf
.mfi
;
337 do_div(temp64
, parent_rate
);
344 static unsigned long clk_pllv3_vf610_recalc_rate(struct clk_hw
*hw
,
345 unsigned long parent_rate
)
347 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
348 struct clk_pllv3_vf610_mf mf
;
350 mf
.mfn
= readl_relaxed(pll
->base
+ pll
->num_offset
);
351 mf
.mfd
= readl_relaxed(pll
->base
+ pll
->denom_offset
);
352 mf
.mfi
= (readl_relaxed(pll
->base
) & pll
->div_mask
) ? 22 : 20;
354 return clk_pllv3_vf610_mf_to_rate(parent_rate
, mf
);
357 static long clk_pllv3_vf610_round_rate(struct clk_hw
*hw
, unsigned long rate
,
358 unsigned long *prate
)
360 struct clk_pllv3_vf610_mf mf
= clk_pllv3_vf610_rate_to_mf(*prate
, rate
);
362 return clk_pllv3_vf610_mf_to_rate(*prate
, mf
);
365 static int clk_pllv3_vf610_set_rate(struct clk_hw
*hw
, unsigned long rate
,
366 unsigned long parent_rate
)
368 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
369 struct clk_pllv3_vf610_mf mf
=
370 clk_pllv3_vf610_rate_to_mf(parent_rate
, rate
);
373 val
= readl_relaxed(pll
->base
);
375 val
&= ~pll
->div_mask
; /* clear bit for mfi=20 */
377 val
|= pll
->div_mask
; /* set bit for mfi=22 */
378 writel_relaxed(val
, pll
->base
);
380 writel_relaxed(mf
.mfn
, pll
->base
+ pll
->num_offset
);
381 writel_relaxed(mf
.mfd
, pll
->base
+ pll
->denom_offset
);
383 return clk_pllv3_wait_lock(pll
);
386 static const struct clk_ops clk_pllv3_vf610_ops
= {
387 .prepare
= clk_pllv3_prepare
,
388 .unprepare
= clk_pllv3_unprepare
,
389 .is_prepared
= clk_pllv3_is_prepared
,
390 .recalc_rate
= clk_pllv3_vf610_recalc_rate
,
391 .round_rate
= clk_pllv3_vf610_round_rate
,
392 .set_rate
= clk_pllv3_vf610_set_rate
,
395 static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw
*hw
,
396 unsigned long parent_rate
)
398 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
400 return pll
->ref_clock
;
403 static const struct clk_ops clk_pllv3_enet_ops
= {
404 .prepare
= clk_pllv3_prepare
,
405 .unprepare
= clk_pllv3_unprepare
,
406 .is_prepared
= clk_pllv3_is_prepared
,
407 .recalc_rate
= clk_pllv3_enet_recalc_rate
,
410 struct clk_hw
*imx_clk_hw_pllv3(enum imx_pllv3_type type
, const char *name
,
411 const char *parent_name
, void __iomem
*base
,
414 struct clk_pllv3
*pll
;
415 const struct clk_ops
*ops
;
417 struct clk_init_data init
;
420 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
422 return ERR_PTR(-ENOMEM
);
424 pll
->power_bit
= BM_PLL_POWER
;
425 pll
->num_offset
= PLL_NUM_OFFSET
;
426 pll
->denom_offset
= PLL_DENOM_OFFSET
;
430 ops
= &clk_pllv3_sys_ops
;
432 case IMX_PLLV3_SYS_VF610
:
433 ops
= &clk_pllv3_vf610_ops
;
434 pll
->num_offset
= PLL_VF610_NUM_OFFSET
;
435 pll
->denom_offset
= PLL_VF610_DENOM_OFFSET
;
437 case IMX_PLLV3_USB_VF610
:
441 ops
= &clk_pllv3_ops
;
442 pll
->powerup_set
= true;
444 case IMX_PLLV3_AV_IMX7
:
445 pll
->num_offset
= PLL_IMX7_NUM_OFFSET
;
446 pll
->denom_offset
= PLL_IMX7_DENOM_OFFSET
;
449 ops
= &clk_pllv3_av_ops
;
451 case IMX_PLLV3_ENET_IMX7
:
452 pll
->power_bit
= IMX7_ENET_PLL_POWER
;
453 pll
->ref_clock
= 1000000000;
454 ops
= &clk_pllv3_enet_ops
;
457 pll
->ref_clock
= 500000000;
458 ops
= &clk_pllv3_enet_ops
;
460 case IMX_PLLV3_DDR_IMX7
:
461 pll
->power_bit
= IMX7_DDR_PLL_POWER
;
462 pll
->num_offset
= PLL_IMX7_NUM_OFFSET
;
463 pll
->denom_offset
= PLL_IMX7_DENOM_OFFSET
;
464 ops
= &clk_pllv3_av_ops
;
467 ops
= &clk_pllv3_ops
;
470 pll
->div_mask
= div_mask
;
475 init
.parent_names
= &parent_name
;
476 init
.num_parents
= 1;
478 pll
->hw
.init
= &init
;
481 ret
= clk_hw_register(NULL
, hw
);