1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014 Google, Inc.
6 #define pr_fmt(fmt) "%s: " fmt, __func__
8 #include <linux/clk-provider.h>
10 #include <linux/kernel.h>
11 #include <linux/printk.h>
12 #include <linux/slab.h>
16 #define PLL_STATUS 0x0
17 #define PLL_STATUS_LOCK BIT(0)
20 #define PLL_CTRL1_REFDIV_SHIFT 0
21 #define PLL_CTRL1_REFDIV_MASK 0x3f
22 #define PLL_CTRL1_FBDIV_SHIFT 6
23 #define PLL_CTRL1_FBDIV_MASK 0xfff
24 #define PLL_INT_CTRL1_POSTDIV1_SHIFT 18
25 #define PLL_INT_CTRL1_POSTDIV1_MASK 0x7
26 #define PLL_INT_CTRL1_POSTDIV2_SHIFT 21
27 #define PLL_INT_CTRL1_POSTDIV2_MASK 0x7
28 #define PLL_INT_CTRL1_PD BIT(24)
29 #define PLL_INT_CTRL1_DSMPD BIT(25)
30 #define PLL_INT_CTRL1_FOUTPOSTDIVPD BIT(26)
31 #define PLL_INT_CTRL1_FOUTVCOPD BIT(27)
34 #define PLL_FRAC_CTRL2_FRAC_SHIFT 0
35 #define PLL_FRAC_CTRL2_FRAC_MASK 0xffffff
36 #define PLL_FRAC_CTRL2_POSTDIV1_SHIFT 24
37 #define PLL_FRAC_CTRL2_POSTDIV1_MASK 0x7
38 #define PLL_FRAC_CTRL2_POSTDIV2_SHIFT 27
39 #define PLL_FRAC_CTRL2_POSTDIV2_MASK 0x7
40 #define PLL_INT_CTRL2_BYPASS BIT(28)
43 #define PLL_FRAC_CTRL3_PD BIT(0)
44 #define PLL_FRAC_CTRL3_DACPD BIT(1)
45 #define PLL_FRAC_CTRL3_DSMPD BIT(2)
46 #define PLL_FRAC_CTRL3_FOUTPOSTDIVPD BIT(3)
47 #define PLL_FRAC_CTRL3_FOUT4PHASEPD BIT(4)
48 #define PLL_FRAC_CTRL3_FOUTVCOPD BIT(5)
50 #define PLL_CTRL4 0x10
51 #define PLL_FRAC_CTRL4_BYPASS BIT(28)
53 #define MIN_PFD 9600000UL
54 #define MIN_VCO_LA 400000000UL
55 #define MAX_VCO_LA 1600000000UL
56 #define MIN_VCO_FRAC_INT 600000000UL
57 #define MAX_VCO_FRAC_INT 1600000000UL
58 #define MIN_VCO_FRAC_FRAC 600000000UL
59 #define MAX_VCO_FRAC_FRAC 2400000000UL
60 #define MIN_OUTPUT_LA 8000000UL
61 #define MAX_OUTPUT_LA 1600000000UL
62 #define MIN_OUTPUT_FRAC 12000000UL
63 #define MAX_OUTPUT_FRAC 1600000000UL
65 /* Fractional PLL operating modes */
71 struct pistachio_clk_pll
{
74 struct pistachio_pll_rate_table
*rates
;
75 unsigned int nr_rates
;
78 static inline u32
pll_readl(struct pistachio_clk_pll
*pll
, u32 reg
)
80 return readl(pll
->base
+ reg
);
83 static inline void pll_writel(struct pistachio_clk_pll
*pll
, u32 val
, u32 reg
)
85 writel(val
, pll
->base
+ reg
);
88 static inline void pll_lock(struct pistachio_clk_pll
*pll
)
90 while (!(pll_readl(pll
, PLL_STATUS
) & PLL_STATUS_LOCK
))
94 static inline u64
do_div_round_closest(u64 dividend
, u64 divisor
)
96 dividend
+= divisor
/ 2;
97 return div64_u64(dividend
, divisor
);
100 static inline struct pistachio_clk_pll
*to_pistachio_pll(struct clk_hw
*hw
)
102 return container_of(hw
, struct pistachio_clk_pll
, hw
);
105 static inline enum pll_mode
pll_frac_get_mode(struct clk_hw
*hw
)
107 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
110 val
= pll_readl(pll
, PLL_CTRL3
) & PLL_FRAC_CTRL3_DSMPD
;
111 return val
? PLL_MODE_INT
: PLL_MODE_FRAC
;
114 static inline void pll_frac_set_mode(struct clk_hw
*hw
, enum pll_mode mode
)
116 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
119 val
= pll_readl(pll
, PLL_CTRL3
);
120 if (mode
== PLL_MODE_INT
)
121 val
|= PLL_FRAC_CTRL3_DSMPD
| PLL_FRAC_CTRL3_DACPD
;
123 val
&= ~(PLL_FRAC_CTRL3_DSMPD
| PLL_FRAC_CTRL3_DACPD
);
125 pll_writel(pll
, val
, PLL_CTRL3
);
128 static struct pistachio_pll_rate_table
*
129 pll_get_params(struct pistachio_clk_pll
*pll
, unsigned long fref
,
134 for (i
= 0; i
< pll
->nr_rates
; i
++) {
135 if (pll
->rates
[i
].fref
== fref
&& pll
->rates
[i
].fout
== fout
)
136 return &pll
->rates
[i
];
142 static long pll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
143 unsigned long *parent_rate
)
145 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
148 for (i
= 0; i
< pll
->nr_rates
; i
++) {
149 if (i
> 0 && pll
->rates
[i
].fref
== *parent_rate
&&
150 pll
->rates
[i
].fout
<= rate
)
151 return pll
->rates
[i
- 1].fout
;
154 return pll
->rates
[0].fout
;
157 static int pll_gf40lp_frac_enable(struct clk_hw
*hw
)
159 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
162 val
= pll_readl(pll
, PLL_CTRL3
);
163 val
&= ~(PLL_FRAC_CTRL3_PD
| PLL_FRAC_CTRL3_FOUTPOSTDIVPD
|
164 PLL_FRAC_CTRL3_FOUT4PHASEPD
| PLL_FRAC_CTRL3_FOUTVCOPD
);
165 pll_writel(pll
, val
, PLL_CTRL3
);
167 val
= pll_readl(pll
, PLL_CTRL4
);
168 val
&= ~PLL_FRAC_CTRL4_BYPASS
;
169 pll_writel(pll
, val
, PLL_CTRL4
);
176 static void pll_gf40lp_frac_disable(struct clk_hw
*hw
)
178 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
181 val
= pll_readl(pll
, PLL_CTRL3
);
182 val
|= PLL_FRAC_CTRL3_PD
;
183 pll_writel(pll
, val
, PLL_CTRL3
);
186 static int pll_gf40lp_frac_is_enabled(struct clk_hw
*hw
)
188 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
190 return !(pll_readl(pll
, PLL_CTRL3
) & PLL_FRAC_CTRL3_PD
);
193 static int pll_gf40lp_frac_set_rate(struct clk_hw
*hw
, unsigned long rate
,
194 unsigned long parent_rate
)
196 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
197 struct pistachio_pll_rate_table
*params
;
198 int enabled
= pll_gf40lp_frac_is_enabled(hw
);
199 u64 val
, vco
, old_postdiv1
, old_postdiv2
;
200 const char *name
= clk_hw_get_name(hw
);
202 if (rate
< MIN_OUTPUT_FRAC
|| rate
> MAX_OUTPUT_FRAC
)
205 params
= pll_get_params(pll
, parent_rate
, rate
);
206 if (!params
|| !params
->refdiv
)
211 vco
*= (params
->fbdiv
<< 24) + params
->frac
;
212 vco
= div64_u64(vco
, params
->refdiv
<< 24);
214 if (vco
< MIN_VCO_FRAC_FRAC
|| vco
> MAX_VCO_FRAC_FRAC
)
215 pr_warn("%s: VCO %llu is out of range %lu..%lu\n", name
, vco
,
216 MIN_VCO_FRAC_FRAC
, MAX_VCO_FRAC_FRAC
);
218 val
= div64_u64(params
->fref
, params
->refdiv
);
220 pr_warn("%s: PFD %llu is too low (min %lu)\n",
223 pr_warn("%s: PFD %llu is too high (max %llu)\n",
224 name
, val
, vco
/ 16);
226 val
= pll_readl(pll
, PLL_CTRL1
);
227 val
&= ~((PLL_CTRL1_REFDIV_MASK
<< PLL_CTRL1_REFDIV_SHIFT
) |
228 (PLL_CTRL1_FBDIV_MASK
<< PLL_CTRL1_FBDIV_SHIFT
));
229 val
|= (params
->refdiv
<< PLL_CTRL1_REFDIV_SHIFT
) |
230 (params
->fbdiv
<< PLL_CTRL1_FBDIV_SHIFT
);
231 pll_writel(pll
, val
, PLL_CTRL1
);
233 val
= pll_readl(pll
, PLL_CTRL2
);
235 old_postdiv1
= (val
>> PLL_FRAC_CTRL2_POSTDIV1_SHIFT
) &
236 PLL_FRAC_CTRL2_POSTDIV1_MASK
;
237 old_postdiv2
= (val
>> PLL_FRAC_CTRL2_POSTDIV2_SHIFT
) &
238 PLL_FRAC_CTRL2_POSTDIV2_MASK
;
240 (params
->postdiv1
!= old_postdiv1
||
241 params
->postdiv2
!= old_postdiv2
))
242 pr_warn("%s: changing postdiv while PLL is enabled\n", name
);
244 if (params
->postdiv2
> params
->postdiv1
)
245 pr_warn("%s: postdiv2 should not exceed postdiv1\n", name
);
247 val
&= ~((PLL_FRAC_CTRL2_FRAC_MASK
<< PLL_FRAC_CTRL2_FRAC_SHIFT
) |
248 (PLL_FRAC_CTRL2_POSTDIV1_MASK
<<
249 PLL_FRAC_CTRL2_POSTDIV1_SHIFT
) |
250 (PLL_FRAC_CTRL2_POSTDIV2_MASK
<<
251 PLL_FRAC_CTRL2_POSTDIV2_SHIFT
));
252 val
|= (params
->frac
<< PLL_FRAC_CTRL2_FRAC_SHIFT
) |
253 (params
->postdiv1
<< PLL_FRAC_CTRL2_POSTDIV1_SHIFT
) |
254 (params
->postdiv2
<< PLL_FRAC_CTRL2_POSTDIV2_SHIFT
);
255 pll_writel(pll
, val
, PLL_CTRL2
);
257 /* set operating mode */
259 pll_frac_set_mode(hw
, PLL_MODE_FRAC
);
261 pll_frac_set_mode(hw
, PLL_MODE_INT
);
269 static unsigned long pll_gf40lp_frac_recalc_rate(struct clk_hw
*hw
,
270 unsigned long parent_rate
)
272 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
273 u64 val
, prediv
, fbdiv
, frac
, postdiv1
, postdiv2
, rate
;
275 val
= pll_readl(pll
, PLL_CTRL1
);
276 prediv
= (val
>> PLL_CTRL1_REFDIV_SHIFT
) & PLL_CTRL1_REFDIV_MASK
;
277 fbdiv
= (val
>> PLL_CTRL1_FBDIV_SHIFT
) & PLL_CTRL1_FBDIV_MASK
;
279 val
= pll_readl(pll
, PLL_CTRL2
);
280 postdiv1
= (val
>> PLL_FRAC_CTRL2_POSTDIV1_SHIFT
) &
281 PLL_FRAC_CTRL2_POSTDIV1_MASK
;
282 postdiv2
= (val
>> PLL_FRAC_CTRL2_POSTDIV2_SHIFT
) &
283 PLL_FRAC_CTRL2_POSTDIV2_MASK
;
284 frac
= (val
>> PLL_FRAC_CTRL2_FRAC_SHIFT
) & PLL_FRAC_CTRL2_FRAC_MASK
;
286 /* get operating mode (int/frac) and calculate rate accordingly */
288 if (pll_frac_get_mode(hw
) == PLL_MODE_FRAC
)
289 rate
*= (fbdiv
<< 24) + frac
;
291 rate
*= (fbdiv
<< 24);
293 rate
= do_div_round_closest(rate
, (prediv
* postdiv1
* postdiv2
) << 24);
298 static const struct clk_ops pll_gf40lp_frac_ops
= {
299 .enable
= pll_gf40lp_frac_enable
,
300 .disable
= pll_gf40lp_frac_disable
,
301 .is_enabled
= pll_gf40lp_frac_is_enabled
,
302 .recalc_rate
= pll_gf40lp_frac_recalc_rate
,
303 .round_rate
= pll_round_rate
,
304 .set_rate
= pll_gf40lp_frac_set_rate
,
307 static const struct clk_ops pll_gf40lp_frac_fixed_ops
= {
308 .enable
= pll_gf40lp_frac_enable
,
309 .disable
= pll_gf40lp_frac_disable
,
310 .is_enabled
= pll_gf40lp_frac_is_enabled
,
311 .recalc_rate
= pll_gf40lp_frac_recalc_rate
,
314 static int pll_gf40lp_laint_enable(struct clk_hw
*hw
)
316 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
319 val
= pll_readl(pll
, PLL_CTRL1
);
320 val
&= ~(PLL_INT_CTRL1_PD
|
321 PLL_INT_CTRL1_FOUTPOSTDIVPD
| PLL_INT_CTRL1_FOUTVCOPD
);
322 pll_writel(pll
, val
, PLL_CTRL1
);
324 val
= pll_readl(pll
, PLL_CTRL2
);
325 val
&= ~PLL_INT_CTRL2_BYPASS
;
326 pll_writel(pll
, val
, PLL_CTRL2
);
333 static void pll_gf40lp_laint_disable(struct clk_hw
*hw
)
335 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
338 val
= pll_readl(pll
, PLL_CTRL1
);
339 val
|= PLL_INT_CTRL1_PD
;
340 pll_writel(pll
, val
, PLL_CTRL1
);
343 static int pll_gf40lp_laint_is_enabled(struct clk_hw
*hw
)
345 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
347 return !(pll_readl(pll
, PLL_CTRL1
) & PLL_INT_CTRL1_PD
);
350 static int pll_gf40lp_laint_set_rate(struct clk_hw
*hw
, unsigned long rate
,
351 unsigned long parent_rate
)
353 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
354 struct pistachio_pll_rate_table
*params
;
355 int enabled
= pll_gf40lp_laint_is_enabled(hw
);
356 u32 val
, vco
, old_postdiv1
, old_postdiv2
;
357 const char *name
= clk_hw_get_name(hw
);
359 if (rate
< MIN_OUTPUT_LA
|| rate
> MAX_OUTPUT_LA
)
362 params
= pll_get_params(pll
, parent_rate
, rate
);
363 if (!params
|| !params
->refdiv
)
366 vco
= div_u64(params
->fref
* params
->fbdiv
, params
->refdiv
);
367 if (vco
< MIN_VCO_LA
|| vco
> MAX_VCO_LA
)
368 pr_warn("%s: VCO %u is out of range %lu..%lu\n", name
, vco
,
369 MIN_VCO_LA
, MAX_VCO_LA
);
371 val
= div_u64(params
->fref
, params
->refdiv
);
373 pr_warn("%s: PFD %u is too low (min %lu)\n",
376 pr_warn("%s: PFD %u is too high (max %u)\n",
377 name
, val
, vco
/ 16);
379 val
= pll_readl(pll
, PLL_CTRL1
);
381 old_postdiv1
= (val
>> PLL_INT_CTRL1_POSTDIV1_SHIFT
) &
382 PLL_INT_CTRL1_POSTDIV1_MASK
;
383 old_postdiv2
= (val
>> PLL_INT_CTRL1_POSTDIV2_SHIFT
) &
384 PLL_INT_CTRL1_POSTDIV2_MASK
;
386 (params
->postdiv1
!= old_postdiv1
||
387 params
->postdiv2
!= old_postdiv2
))
388 pr_warn("%s: changing postdiv while PLL is enabled\n", name
);
390 if (params
->postdiv2
> params
->postdiv1
)
391 pr_warn("%s: postdiv2 should not exceed postdiv1\n", name
);
393 val
&= ~((PLL_CTRL1_REFDIV_MASK
<< PLL_CTRL1_REFDIV_SHIFT
) |
394 (PLL_CTRL1_FBDIV_MASK
<< PLL_CTRL1_FBDIV_SHIFT
) |
395 (PLL_INT_CTRL1_POSTDIV1_MASK
<< PLL_INT_CTRL1_POSTDIV1_SHIFT
) |
396 (PLL_INT_CTRL1_POSTDIV2_MASK
<< PLL_INT_CTRL1_POSTDIV2_SHIFT
));
397 val
|= (params
->refdiv
<< PLL_CTRL1_REFDIV_SHIFT
) |
398 (params
->fbdiv
<< PLL_CTRL1_FBDIV_SHIFT
) |
399 (params
->postdiv1
<< PLL_INT_CTRL1_POSTDIV1_SHIFT
) |
400 (params
->postdiv2
<< PLL_INT_CTRL1_POSTDIV2_SHIFT
);
401 pll_writel(pll
, val
, PLL_CTRL1
);
409 static unsigned long pll_gf40lp_laint_recalc_rate(struct clk_hw
*hw
,
410 unsigned long parent_rate
)
412 struct pistachio_clk_pll
*pll
= to_pistachio_pll(hw
);
413 u32 val
, prediv
, fbdiv
, postdiv1
, postdiv2
;
414 u64 rate
= parent_rate
;
416 val
= pll_readl(pll
, PLL_CTRL1
);
417 prediv
= (val
>> PLL_CTRL1_REFDIV_SHIFT
) & PLL_CTRL1_REFDIV_MASK
;
418 fbdiv
= (val
>> PLL_CTRL1_FBDIV_SHIFT
) & PLL_CTRL1_FBDIV_MASK
;
419 postdiv1
= (val
>> PLL_INT_CTRL1_POSTDIV1_SHIFT
) &
420 PLL_INT_CTRL1_POSTDIV1_MASK
;
421 postdiv2
= (val
>> PLL_INT_CTRL1_POSTDIV2_SHIFT
) &
422 PLL_INT_CTRL1_POSTDIV2_MASK
;
425 rate
= do_div_round_closest(rate
, prediv
* postdiv1
* postdiv2
);
430 static const struct clk_ops pll_gf40lp_laint_ops
= {
431 .enable
= pll_gf40lp_laint_enable
,
432 .disable
= pll_gf40lp_laint_disable
,
433 .is_enabled
= pll_gf40lp_laint_is_enabled
,
434 .recalc_rate
= pll_gf40lp_laint_recalc_rate
,
435 .round_rate
= pll_round_rate
,
436 .set_rate
= pll_gf40lp_laint_set_rate
,
439 static const struct clk_ops pll_gf40lp_laint_fixed_ops
= {
440 .enable
= pll_gf40lp_laint_enable
,
441 .disable
= pll_gf40lp_laint_disable
,
442 .is_enabled
= pll_gf40lp_laint_is_enabled
,
443 .recalc_rate
= pll_gf40lp_laint_recalc_rate
,
446 static struct clk
*pll_register(const char *name
, const char *parent_name
,
447 unsigned long flags
, void __iomem
*base
,
448 enum pistachio_pll_type type
,
449 struct pistachio_pll_rate_table
*rates
,
450 unsigned int nr_rates
)
452 struct pistachio_clk_pll
*pll
;
453 struct clk_init_data init
;
456 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
458 return ERR_PTR(-ENOMEM
);
461 init
.flags
= flags
| CLK_GET_RATE_NOCACHE
;
462 init
.parent_names
= &parent_name
;
463 init
.num_parents
= 1;
466 case PLL_GF40LP_FRAC
:
468 init
.ops
= &pll_gf40lp_frac_ops
;
470 init
.ops
= &pll_gf40lp_frac_fixed_ops
;
472 case PLL_GF40LP_LAINT
:
474 init
.ops
= &pll_gf40lp_laint_ops
;
476 init
.ops
= &pll_gf40lp_laint_fixed_ops
;
479 pr_err("Unrecognized PLL type %u\n", type
);
481 return ERR_PTR(-EINVAL
);
484 pll
->hw
.init
= &init
;
487 pll
->nr_rates
= nr_rates
;
489 clk
= clk_register(NULL
, &pll
->hw
);
496 void pistachio_clk_register_pll(struct pistachio_clk_provider
*p
,
497 struct pistachio_pll
*pll
,
503 for (i
= 0; i
< num
; i
++) {
504 clk
= pll_register(pll
[i
].name
, pll
[i
].parent
,
505 0, p
->base
+ pll
[i
].reg_base
,
506 pll
[i
].type
, pll
[i
].rates
,
508 p
->clk_data
.clks
[pll
[i
].id
] = clk
;