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/slab.h>
11 #include <linux/jiffies.h>
12 #include <linux/err.h>
15 #define PLL_NUM_OFFSET 0x10
16 #define PLL_DENOM_OFFSET 0x20
17 #define PLL_IMX7_NUM_OFFSET 0x20
18 #define PLL_IMX7_DENOM_OFFSET 0x30
20 #define PLL_VF610_NUM_OFFSET 0x20
21 #define PLL_VF610_DENOM_OFFSET 0x30
23 #define BM_PLL_POWER (0x1 << 12)
24 #define BM_PLL_LOCK (0x1 << 31)
25 #define IMX7_ENET_PLL_POWER (0x1 << 5)
26 #define IMX7_DDR_PLL_POWER (0x1 << 20)
29 * struct clk_pllv3 - IMX PLL clock version 3
30 * @clk_hw: clock source
31 * @base: base address of PLL registers
32 * @power_bit: pll power bit mask
33 * @powerup_set: set power_bit to power up the PLL
34 * @div_mask: mask of divider bits
35 * @div_shift: shift of divider bits
37 * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
38 * is actually a multiplier, and always sits at bit 0.
47 unsigned long ref_clock
;
52 #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
54 static int clk_pllv3_wait_lock(struct clk_pllv3
*pll
)
56 unsigned long timeout
= jiffies
+ msecs_to_jiffies(10);
57 u32 val
= readl_relaxed(pll
->base
) & pll
->power_bit
;
59 /* No need to wait for lock when pll is not powered up */
60 if ((pll
->powerup_set
&& !val
) || (!pll
->powerup_set
&& val
))
63 /* Wait for PLL to lock */
65 if (readl_relaxed(pll
->base
) & BM_PLL_LOCK
)
67 if (time_after(jiffies
, timeout
))
69 usleep_range(50, 500);
72 return readl_relaxed(pll
->base
) & BM_PLL_LOCK
? 0 : -ETIMEDOUT
;
75 static int clk_pllv3_prepare(struct clk_hw
*hw
)
77 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
80 val
= readl_relaxed(pll
->base
);
82 val
|= pll
->power_bit
;
84 val
&= ~pll
->power_bit
;
85 writel_relaxed(val
, pll
->base
);
87 return clk_pllv3_wait_lock(pll
);
90 static void clk_pllv3_unprepare(struct clk_hw
*hw
)
92 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
95 val
= readl_relaxed(pll
->base
);
97 val
&= ~pll
->power_bit
;
99 val
|= pll
->power_bit
;
100 writel_relaxed(val
, pll
->base
);
103 static int clk_pllv3_is_prepared(struct clk_hw
*hw
)
105 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
107 if (readl_relaxed(pll
->base
) & BM_PLL_LOCK
)
113 static unsigned long clk_pllv3_recalc_rate(struct clk_hw
*hw
,
114 unsigned long parent_rate
)
116 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
117 u32 div
= (readl_relaxed(pll
->base
) >> pll
->div_shift
) & pll
->div_mask
;
119 return (div
== 1) ? parent_rate
* 22 : parent_rate
* 20;
122 static long clk_pllv3_round_rate(struct clk_hw
*hw
, unsigned long rate
,
123 unsigned long *prate
)
125 unsigned long parent_rate
= *prate
;
127 return (rate
>= parent_rate
* 22) ? parent_rate
* 22 :
131 static int clk_pllv3_set_rate(struct clk_hw
*hw
, unsigned long rate
,
132 unsigned long parent_rate
)
134 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
137 if (rate
== parent_rate
* 22)
139 else if (rate
== parent_rate
* 20)
144 val
= readl_relaxed(pll
->base
);
145 val
&= ~(pll
->div_mask
<< pll
->div_shift
);
146 val
|= (div
<< pll
->div_shift
);
147 writel_relaxed(val
, pll
->base
);
149 return clk_pllv3_wait_lock(pll
);
152 static const struct clk_ops clk_pllv3_ops
= {
153 .prepare
= clk_pllv3_prepare
,
154 .unprepare
= clk_pllv3_unprepare
,
155 .is_prepared
= clk_pllv3_is_prepared
,
156 .recalc_rate
= clk_pllv3_recalc_rate
,
157 .round_rate
= clk_pllv3_round_rate
,
158 .set_rate
= clk_pllv3_set_rate
,
161 static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw
*hw
,
162 unsigned long parent_rate
)
164 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
165 u32 div
= readl_relaxed(pll
->base
) & pll
->div_mask
;
167 return parent_rate
* div
/ 2;
170 static long clk_pllv3_sys_round_rate(struct clk_hw
*hw
, unsigned long rate
,
171 unsigned long *prate
)
173 unsigned long parent_rate
= *prate
;
174 unsigned long min_rate
= parent_rate
* 54 / 2;
175 unsigned long max_rate
= parent_rate
* 108 / 2;
180 else if (rate
< min_rate
)
182 div
= rate
* 2 / parent_rate
;
184 return parent_rate
* div
/ 2;
187 static int clk_pllv3_sys_set_rate(struct clk_hw
*hw
, unsigned long rate
,
188 unsigned long parent_rate
)
190 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
191 unsigned long min_rate
= parent_rate
* 54 / 2;
192 unsigned long max_rate
= parent_rate
* 108 / 2;
195 if (rate
< min_rate
|| rate
> max_rate
)
198 div
= rate
* 2 / parent_rate
;
199 val
= readl_relaxed(pll
->base
);
200 val
&= ~pll
->div_mask
;
202 writel_relaxed(val
, pll
->base
);
204 return clk_pllv3_wait_lock(pll
);
207 static const struct clk_ops clk_pllv3_sys_ops
= {
208 .prepare
= clk_pllv3_prepare
,
209 .unprepare
= clk_pllv3_unprepare
,
210 .is_prepared
= clk_pllv3_is_prepared
,
211 .recalc_rate
= clk_pllv3_sys_recalc_rate
,
212 .round_rate
= clk_pllv3_sys_round_rate
,
213 .set_rate
= clk_pllv3_sys_set_rate
,
216 static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw
*hw
,
217 unsigned long parent_rate
)
219 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
220 u32 mfn
= readl_relaxed(pll
->base
+ pll
->num_offset
);
221 u32 mfd
= readl_relaxed(pll
->base
+ pll
->denom_offset
);
222 u32 div
= readl_relaxed(pll
->base
) & pll
->div_mask
;
223 u64 temp64
= (u64
)parent_rate
;
228 return parent_rate
* div
+ (unsigned long)temp64
;
231 static long clk_pllv3_av_round_rate(struct clk_hw
*hw
, unsigned long rate
,
232 unsigned long *prate
)
234 unsigned long parent_rate
= *prate
;
235 unsigned long min_rate
= parent_rate
* 27;
236 unsigned long max_rate
= parent_rate
* 54;
238 u32 mfn
, mfd
= 1000000;
239 u32 max_mfd
= 0x3FFFFFFF;
244 else if (rate
< min_rate
)
247 if (parent_rate
<= max_mfd
)
250 div
= rate
/ parent_rate
;
251 temp64
= (u64
) (rate
- div
* parent_rate
);
253 do_div(temp64
, parent_rate
);
256 temp64
= (u64
)parent_rate
;
260 return parent_rate
* div
+ (unsigned long)temp64
;
263 static int clk_pllv3_av_set_rate(struct clk_hw
*hw
, unsigned long rate
,
264 unsigned long parent_rate
)
266 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
267 unsigned long min_rate
= parent_rate
* 27;
268 unsigned long max_rate
= parent_rate
* 54;
270 u32 mfn
, mfd
= 1000000;
271 u32 max_mfd
= 0x3FFFFFFF;
274 if (rate
< min_rate
|| rate
> max_rate
)
277 if (parent_rate
<= max_mfd
)
280 div
= rate
/ parent_rate
;
281 temp64
= (u64
) (rate
- div
* parent_rate
);
283 do_div(temp64
, parent_rate
);
286 val
= readl_relaxed(pll
->base
);
287 val
&= ~pll
->div_mask
;
289 writel_relaxed(val
, pll
->base
);
290 writel_relaxed(mfn
, pll
->base
+ pll
->num_offset
);
291 writel_relaxed(mfd
, pll
->base
+ pll
->denom_offset
);
293 return clk_pllv3_wait_lock(pll
);
296 static const struct clk_ops clk_pllv3_av_ops
= {
297 .prepare
= clk_pllv3_prepare
,
298 .unprepare
= clk_pllv3_unprepare
,
299 .is_prepared
= clk_pllv3_is_prepared
,
300 .recalc_rate
= clk_pllv3_av_recalc_rate
,
301 .round_rate
= clk_pllv3_av_round_rate
,
302 .set_rate
= clk_pllv3_av_set_rate
,
305 struct clk_pllv3_vf610_mf
{
306 u32 mfi
; /* integer part, can be 20 or 22 */
307 u32 mfn
; /* numerator, 30-bit value */
308 u32 mfd
; /* denominator, 30-bit value, must be less than mfn */
311 static unsigned long clk_pllv3_vf610_mf_to_rate(unsigned long parent_rate
,
312 struct clk_pllv3_vf610_mf mf
)
316 temp64
= parent_rate
;
318 do_div(temp64
, mf
.mfd
);
320 return (parent_rate
* mf
.mfi
) + temp64
;
323 static struct clk_pllv3_vf610_mf
clk_pllv3_vf610_rate_to_mf(
324 unsigned long parent_rate
, unsigned long rate
)
326 struct clk_pllv3_vf610_mf mf
;
329 mf
.mfi
= (rate
>= 22 * parent_rate
) ? 22 : 20;
330 mf
.mfd
= 0x3fffffff; /* use max supported value for best accuracy */
332 if (rate
<= parent_rate
* mf
.mfi
)
334 else if (rate
>= parent_rate
* (mf
.mfi
+ 1))
337 /* rate = parent_rate * (mfi + mfn/mfd) */
338 temp64
= rate
- parent_rate
* mf
.mfi
;
340 do_div(temp64
, parent_rate
);
347 static unsigned long clk_pllv3_vf610_recalc_rate(struct clk_hw
*hw
,
348 unsigned long parent_rate
)
350 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
351 struct clk_pllv3_vf610_mf mf
;
353 mf
.mfn
= readl_relaxed(pll
->base
+ pll
->num_offset
);
354 mf
.mfd
= readl_relaxed(pll
->base
+ pll
->denom_offset
);
355 mf
.mfi
= (readl_relaxed(pll
->base
) & pll
->div_mask
) ? 22 : 20;
357 return clk_pllv3_vf610_mf_to_rate(parent_rate
, mf
);
360 static long clk_pllv3_vf610_round_rate(struct clk_hw
*hw
, unsigned long rate
,
361 unsigned long *prate
)
363 struct clk_pllv3_vf610_mf mf
= clk_pllv3_vf610_rate_to_mf(*prate
, rate
);
365 return clk_pllv3_vf610_mf_to_rate(*prate
, mf
);
368 static int clk_pllv3_vf610_set_rate(struct clk_hw
*hw
, unsigned long rate
,
369 unsigned long parent_rate
)
371 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
372 struct clk_pllv3_vf610_mf mf
=
373 clk_pllv3_vf610_rate_to_mf(parent_rate
, rate
);
376 val
= readl_relaxed(pll
->base
);
378 val
&= ~pll
->div_mask
; /* clear bit for mfi=20 */
380 val
|= pll
->div_mask
; /* set bit for mfi=22 */
381 writel_relaxed(val
, pll
->base
);
383 writel_relaxed(mf
.mfn
, pll
->base
+ pll
->num_offset
);
384 writel_relaxed(mf
.mfd
, pll
->base
+ pll
->denom_offset
);
386 return clk_pllv3_wait_lock(pll
);
389 static const struct clk_ops clk_pllv3_vf610_ops
= {
390 .prepare
= clk_pllv3_prepare
,
391 .unprepare
= clk_pllv3_unprepare
,
392 .is_prepared
= clk_pllv3_is_prepared
,
393 .recalc_rate
= clk_pllv3_vf610_recalc_rate
,
394 .round_rate
= clk_pllv3_vf610_round_rate
,
395 .set_rate
= clk_pllv3_vf610_set_rate
,
398 static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw
*hw
,
399 unsigned long parent_rate
)
401 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
403 return pll
->ref_clock
;
406 static const struct clk_ops clk_pllv3_enet_ops
= {
407 .prepare
= clk_pllv3_prepare
,
408 .unprepare
= clk_pllv3_unprepare
,
409 .is_prepared
= clk_pllv3_is_prepared
,
410 .recalc_rate
= clk_pllv3_enet_recalc_rate
,
413 struct clk_hw
*imx_clk_hw_pllv3(enum imx_pllv3_type type
, const char *name
,
414 const char *parent_name
, void __iomem
*base
,
417 struct clk_pllv3
*pll
;
418 const struct clk_ops
*ops
;
420 struct clk_init_data init
;
423 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
425 return ERR_PTR(-ENOMEM
);
427 pll
->power_bit
= BM_PLL_POWER
;
428 pll
->num_offset
= PLL_NUM_OFFSET
;
429 pll
->denom_offset
= PLL_DENOM_OFFSET
;
433 ops
= &clk_pllv3_sys_ops
;
435 case IMX_PLLV3_SYS_VF610
:
436 ops
= &clk_pllv3_vf610_ops
;
437 pll
->num_offset
= PLL_VF610_NUM_OFFSET
;
438 pll
->denom_offset
= PLL_VF610_DENOM_OFFSET
;
440 case IMX_PLLV3_USB_VF610
:
444 ops
= &clk_pllv3_ops
;
445 pll
->powerup_set
= true;
447 case IMX_PLLV3_AV_IMX7
:
448 pll
->num_offset
= PLL_IMX7_NUM_OFFSET
;
449 pll
->denom_offset
= PLL_IMX7_DENOM_OFFSET
;
452 ops
= &clk_pllv3_av_ops
;
454 case IMX_PLLV3_ENET_IMX7
:
455 pll
->power_bit
= IMX7_ENET_PLL_POWER
;
456 pll
->ref_clock
= 1000000000;
457 ops
= &clk_pllv3_enet_ops
;
460 pll
->ref_clock
= 500000000;
461 ops
= &clk_pllv3_enet_ops
;
463 case IMX_PLLV3_DDR_IMX7
:
464 pll
->power_bit
= IMX7_DDR_PLL_POWER
;
465 pll
->num_offset
= PLL_IMX7_NUM_OFFSET
;
466 pll
->denom_offset
= PLL_IMX7_DENOM_OFFSET
;
467 ops
= &clk_pllv3_av_ops
;
470 ops
= &clk_pllv3_ops
;
473 pll
->div_mask
= div_mask
;
478 init
.parent_names
= &parent_name
;
479 init
.num_parents
= 1;
481 pll
->hw
.init
= &init
;
484 ret
= clk_hw_register(NULL
, hw
);