2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation version 2.
6 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
7 * kind, whether express or implied; without even the implied warranty
8 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
17 #include <linux/math64.h>
19 #include <linux/of_address.h>
20 #include <linux/clk/ti.h>
22 /* FAPLL Control Register PLL_CTRL */
23 #define FAPLL_MAIN_MULT_N_SHIFT 16
24 #define FAPLL_MAIN_DIV_P_SHIFT 8
25 #define FAPLL_MAIN_LOCK BIT(7)
26 #define FAPLL_MAIN_PLLEN BIT(3)
27 #define FAPLL_MAIN_BP BIT(2)
28 #define FAPLL_MAIN_LOC_CTL BIT(0)
30 #define FAPLL_MAIN_MAX_MULT_N 0xffff
31 #define FAPLL_MAIN_MAX_DIV_P 0xff
32 #define FAPLL_MAIN_CLEAR_MASK \
33 ((FAPLL_MAIN_MAX_MULT_N << FAPLL_MAIN_MULT_N_SHIFT) | \
34 (FAPLL_MAIN_DIV_P_SHIFT << FAPLL_MAIN_DIV_P_SHIFT) | \
37 /* FAPLL powerdown register PWD */
38 #define FAPLL_PWD_OFFSET 4
40 #define MAX_FAPLL_OUTPUTS 7
41 #define FAPLL_MAX_RETRIES 1000
43 #define to_fapll(_hw) container_of(_hw, struct fapll_data, hw)
44 #define to_synth(_hw) container_of(_hw, struct fapll_synth, hw)
46 /* The bypass bit is inverted on the ddr_pll.. */
47 #define fapll_is_ddr_pll(va) (((u32)(va) & 0xffff) == 0x0440)
50 * The audio_pll_clk1 input is hard wired to the 27MHz bypass clock,
51 * and the audio_pll_clk1 synthesizer is hardwared to 32KiHz output.
53 #define is_ddr_pll_clk1(va) (((u32)(va) & 0xffff) == 0x044c)
54 #define is_audio_pll_clk1(va) (((u32)(va) & 0xffff) == 0x04a8)
56 /* Synthesizer divider register */
57 #define SYNTH_LDMDIV1 BIT(8)
59 /* Synthesizer frequency register */
60 #define SYNTH_LDFREQ BIT(31)
62 #define SYNTH_PHASE_K 8
63 #define SYNTH_MAX_INT_DIV 0xf
64 #define SYNTH_MAX_DIV_M 0xff
71 struct clk
*clk_bypass
;
72 struct clk_onecell_data outputs
;
73 bool bypass_bit_inverted
;
78 struct fapll_data
*fd
;
86 static bool ti_fapll_clock_is_bypass(struct fapll_data
*fd
)
88 u32 v
= readl_relaxed(fd
->base
);
90 if (fd
->bypass_bit_inverted
)
91 return !(v
& FAPLL_MAIN_BP
);
93 return !!(v
& FAPLL_MAIN_BP
);
96 static void ti_fapll_set_bypass(struct fapll_data
*fd
)
98 u32 v
= readl_relaxed(fd
->base
);
100 if (fd
->bypass_bit_inverted
)
104 writel_relaxed(v
, fd
->base
);
107 static void ti_fapll_clear_bypass(struct fapll_data
*fd
)
109 u32 v
= readl_relaxed(fd
->base
);
111 if (fd
->bypass_bit_inverted
)
115 writel_relaxed(v
, fd
->base
);
118 static int ti_fapll_wait_lock(struct fapll_data
*fd
)
120 int retries
= FAPLL_MAX_RETRIES
;
123 while ((v
= readl_relaxed(fd
->base
))) {
124 if (v
& FAPLL_MAIN_LOCK
)
133 pr_err("%s failed to lock\n", fd
->name
);
138 static int ti_fapll_enable(struct clk_hw
*hw
)
140 struct fapll_data
*fd
= to_fapll(hw
);
141 u32 v
= readl_relaxed(fd
->base
);
143 v
|= FAPLL_MAIN_PLLEN
;
144 writel_relaxed(v
, fd
->base
);
145 ti_fapll_wait_lock(fd
);
150 static void ti_fapll_disable(struct clk_hw
*hw
)
152 struct fapll_data
*fd
= to_fapll(hw
);
153 u32 v
= readl_relaxed(fd
->base
);
155 v
&= ~FAPLL_MAIN_PLLEN
;
156 writel_relaxed(v
, fd
->base
);
159 static int ti_fapll_is_enabled(struct clk_hw
*hw
)
161 struct fapll_data
*fd
= to_fapll(hw
);
162 u32 v
= readl_relaxed(fd
->base
);
164 return v
& FAPLL_MAIN_PLLEN
;
167 static unsigned long ti_fapll_recalc_rate(struct clk_hw
*hw
,
168 unsigned long parent_rate
)
170 struct fapll_data
*fd
= to_fapll(hw
);
171 u32 fapll_n
, fapll_p
, v
;
174 if (ti_fapll_clock_is_bypass(fd
))
179 /* PLL pre-divider is P and multiplier is N */
180 v
= readl_relaxed(fd
->base
);
181 fapll_p
= (v
>> 8) & 0xff;
183 do_div(rate
, fapll_p
);
191 static u8
ti_fapll_get_parent(struct clk_hw
*hw
)
193 struct fapll_data
*fd
= to_fapll(hw
);
195 if (ti_fapll_clock_is_bypass(fd
))
201 static int ti_fapll_set_div_mult(unsigned long rate
,
202 unsigned long parent_rate
,
203 u32
*pre_div_p
, u32
*mult_n
)
206 * So far no luck getting decent clock with PLL divider,
207 * PLL does not seem to lock and the signal does not look
208 * right. It seems the divider can only be used together
209 * with the multiplier?
211 if (rate
< parent_rate
) {
212 pr_warn("FAPLL main divider rates unsupported\n");
216 *mult_n
= rate
/ parent_rate
;
217 if (*mult_n
> FAPLL_MAIN_MAX_MULT_N
)
224 static long ti_fapll_round_rate(struct clk_hw
*hw
, unsigned long rate
,
225 unsigned long *parent_rate
)
227 u32 pre_div_p
, mult_n
;
233 error
= ti_fapll_set_div_mult(rate
, *parent_rate
,
234 &pre_div_p
, &mult_n
);
238 rate
= *parent_rate
/ pre_div_p
;
244 static int ti_fapll_set_rate(struct clk_hw
*hw
, unsigned long rate
,
245 unsigned long parent_rate
)
247 struct fapll_data
*fd
= to_fapll(hw
);
248 u32 pre_div_p
, mult_n
, v
;
254 error
= ti_fapll_set_div_mult(rate
, parent_rate
,
255 &pre_div_p
, &mult_n
);
259 ti_fapll_set_bypass(fd
);
260 v
= readl_relaxed(fd
->base
);
261 v
&= ~FAPLL_MAIN_CLEAR_MASK
;
262 v
|= pre_div_p
<< FAPLL_MAIN_DIV_P_SHIFT
;
263 v
|= mult_n
<< FAPLL_MAIN_MULT_N_SHIFT
;
264 writel_relaxed(v
, fd
->base
);
265 if (ti_fapll_is_enabled(hw
))
266 ti_fapll_wait_lock(fd
);
267 ti_fapll_clear_bypass(fd
);
272 static const struct clk_ops ti_fapll_ops
= {
273 .enable
= ti_fapll_enable
,
274 .disable
= ti_fapll_disable
,
275 .is_enabled
= ti_fapll_is_enabled
,
276 .recalc_rate
= ti_fapll_recalc_rate
,
277 .get_parent
= ti_fapll_get_parent
,
278 .round_rate
= ti_fapll_round_rate
,
279 .set_rate
= ti_fapll_set_rate
,
282 static int ti_fapll_synth_enable(struct clk_hw
*hw
)
284 struct fapll_synth
*synth
= to_synth(hw
);
285 u32 v
= readl_relaxed(synth
->fd
->base
+ FAPLL_PWD_OFFSET
);
287 v
&= ~(1 << synth
->index
);
288 writel_relaxed(v
, synth
->fd
->base
+ FAPLL_PWD_OFFSET
);
293 static void ti_fapll_synth_disable(struct clk_hw
*hw
)
295 struct fapll_synth
*synth
= to_synth(hw
);
296 u32 v
= readl_relaxed(synth
->fd
->base
+ FAPLL_PWD_OFFSET
);
298 v
|= 1 << synth
->index
;
299 writel_relaxed(v
, synth
->fd
->base
+ FAPLL_PWD_OFFSET
);
302 static int ti_fapll_synth_is_enabled(struct clk_hw
*hw
)
304 struct fapll_synth
*synth
= to_synth(hw
);
305 u32 v
= readl_relaxed(synth
->fd
->base
+ FAPLL_PWD_OFFSET
);
307 return !(v
& (1 << synth
->index
));
311 * See dm816x TRM chapter 1.10.3 Flying Adder PLL fore more info
313 static unsigned long ti_fapll_synth_recalc_rate(struct clk_hw
*hw
,
314 unsigned long parent_rate
)
316 struct fapll_synth
*synth
= to_synth(hw
);
320 /* The audio_pll_clk1 is hardwired to produce 32.768KiHz clock */
325 * PLL in bypass sets the synths in bypass mode too. The PLL rate
326 * can be also be set to 27MHz, so we can't use parent_rate to
327 * check for bypass mode.
329 if (ti_fapll_clock_is_bypass(synth
->fd
))
335 * Synth frequency integer and fractional divider.
336 * Note that the phase output K is 8, so the result needs
337 * to be multiplied by SYNTH_PHASE_K.
340 u32 v
, synth_int_div
, synth_frac_div
, synth_div_freq
;
342 v
= readl_relaxed(synth
->freq
);
343 synth_int_div
= (v
>> 24) & 0xf;
344 synth_frac_div
= v
& 0xffffff;
345 synth_div_freq
= (synth_int_div
* 10000000) + synth_frac_div
;
347 do_div(rate
, synth_div_freq
);
348 rate
*= SYNTH_PHASE_K
;
351 /* Synth post-divider M */
352 synth_div_m
= readl_relaxed(synth
->div
) & SYNTH_MAX_DIV_M
;
354 return DIV_ROUND_UP_ULL(rate
, synth_div_m
);
357 static unsigned long ti_fapll_synth_get_frac_rate(struct clk_hw
*hw
,
358 unsigned long parent_rate
)
360 struct fapll_synth
*synth
= to_synth(hw
);
361 unsigned long current_rate
, frac_rate
;
364 current_rate
= ti_fapll_synth_recalc_rate(hw
, parent_rate
);
365 post_div_m
= readl_relaxed(synth
->div
) & SYNTH_MAX_DIV_M
;
366 frac_rate
= current_rate
* post_div_m
;
371 static u32
ti_fapll_synth_set_frac_rate(struct fapll_synth
*synth
,
373 unsigned long parent_rate
)
375 u32 post_div_m
, synth_int_div
= 0, synth_frac_div
= 0, v
;
377 post_div_m
= DIV_ROUND_UP_ULL((u64
)parent_rate
* SYNTH_PHASE_K
, rate
);
378 post_div_m
= post_div_m
/ SYNTH_MAX_INT_DIV
;
379 if (post_div_m
> SYNTH_MAX_DIV_M
)
384 for (; post_div_m
< SYNTH_MAX_DIV_M
; post_div_m
++) {
385 synth_int_div
= DIV_ROUND_UP_ULL((u64
)parent_rate
*
389 synth_frac_div
= synth_int_div
% 10000000;
390 synth_int_div
/= 10000000;
392 if (synth_int_div
<= SYNTH_MAX_INT_DIV
)
396 if (synth_int_div
> SYNTH_MAX_INT_DIV
)
399 v
= readl_relaxed(synth
->freq
);
401 v
|= (synth_int_div
& SYNTH_MAX_INT_DIV
) << 24;
402 v
|= (synth_frac_div
& 0xffffff);
404 writel_relaxed(v
, synth
->freq
);
409 static long ti_fapll_synth_round_rate(struct clk_hw
*hw
, unsigned long rate
,
410 unsigned long *parent_rate
)
412 struct fapll_synth
*synth
= to_synth(hw
);
413 struct fapll_data
*fd
= synth
->fd
;
416 if (ti_fapll_clock_is_bypass(fd
) || !synth
->div
|| !rate
)
419 /* Only post divider m available with no fractional divider? */
421 unsigned long frac_rate
;
422 u32 synth_post_div_m
;
424 frac_rate
= ti_fapll_synth_get_frac_rate(hw
, *parent_rate
);
425 synth_post_div_m
= DIV_ROUND_UP(frac_rate
, rate
);
426 r
= DIV_ROUND_UP(frac_rate
, synth_post_div_m
);
430 r
= *parent_rate
* SYNTH_PHASE_K
;
434 r
= DIV_ROUND_UP_ULL(r
, SYNTH_MAX_INT_DIV
* SYNTH_MAX_DIV_M
);
443 static int ti_fapll_synth_set_rate(struct clk_hw
*hw
, unsigned long rate
,
444 unsigned long parent_rate
)
446 struct fapll_synth
*synth
= to_synth(hw
);
447 struct fapll_data
*fd
= synth
->fd
;
448 unsigned long frac_rate
, post_rate
= 0;
449 u32 post_div_m
= 0, v
;
451 if (ti_fapll_clock_is_bypass(fd
) || !synth
->div
|| !rate
)
454 /* Produce the rate with just post divider M? */
455 frac_rate
= ti_fapll_synth_get_frac_rate(hw
, parent_rate
);
456 if (frac_rate
< rate
) {
460 post_div_m
= DIV_ROUND_UP(frac_rate
, rate
);
461 if (post_div_m
&& (post_div_m
<= SYNTH_MAX_DIV_M
))
462 post_rate
= DIV_ROUND_UP(frac_rate
, post_div_m
);
463 if (!synth
->freq
&& !post_rate
)
467 /* Need to recalculate the fractional divider? */
468 if ((post_rate
!= rate
) && synth
->freq
)
469 post_div_m
= ti_fapll_synth_set_frac_rate(synth
,
473 v
= readl_relaxed(synth
->div
);
474 v
&= ~SYNTH_MAX_DIV_M
;
477 writel_relaxed(v
, synth
->div
);
482 static const struct clk_ops ti_fapll_synt_ops
= {
483 .enable
= ti_fapll_synth_enable
,
484 .disable
= ti_fapll_synth_disable
,
485 .is_enabled
= ti_fapll_synth_is_enabled
,
486 .recalc_rate
= ti_fapll_synth_recalc_rate
,
487 .round_rate
= ti_fapll_synth_round_rate
,
488 .set_rate
= ti_fapll_synth_set_rate
,
491 static struct clk
* __init
ti_fapll_synth_setup(struct fapll_data
*fd
,
499 struct clk_init_data
*init
;
500 struct fapll_synth
*synth
;
501 struct clk
*clk
= ERR_PTR(-ENOMEM
);
503 init
= kzalloc(sizeof(*init
), GFP_KERNEL
);
505 return ERR_PTR(-ENOMEM
);
507 init
->ops
= &ti_fapll_synt_ops
;
509 init
->parent_names
= &parent
;
510 init
->num_parents
= 1;
512 synth
= kzalloc(sizeof(*synth
), GFP_KERNEL
);
517 synth
->index
= index
;
521 synth
->hw
.init
= init
;
522 synth
->clk_pll
= pll_clk
;
524 clk
= clk_register(NULL
, &synth
->hw
);
526 pr_err("failed to register clock\n");
539 static void __init
ti_fapll_setup(struct device_node
*node
)
541 struct fapll_data
*fd
;
542 struct clk_init_data
*init
= NULL
;
543 const char *parent_name
[2];
547 fd
= kzalloc(sizeof(*fd
), GFP_KERNEL
);
551 fd
->outputs
.clks
= kzalloc(sizeof(struct clk
*) *
552 MAX_FAPLL_OUTPUTS
+ 1,
554 if (!fd
->outputs
.clks
)
557 init
= kzalloc(sizeof(*init
), GFP_KERNEL
);
561 init
->ops
= &ti_fapll_ops
;
562 init
->name
= node
->name
;
564 init
->num_parents
= of_clk_get_parent_count(node
);
565 if (init
->num_parents
!= 2) {
566 pr_err("%pOFn must have two parents\n", node
);
570 of_clk_parent_fill(node
, parent_name
, 2);
571 init
->parent_names
= parent_name
;
573 fd
->clk_ref
= of_clk_get(node
, 0);
574 if (IS_ERR(fd
->clk_ref
)) {
575 pr_err("%pOFn could not get clk_ref\n", node
);
579 fd
->clk_bypass
= of_clk_get(node
, 1);
580 if (IS_ERR(fd
->clk_bypass
)) {
581 pr_err("%pOFn could not get clk_bypass\n", node
);
585 fd
->base
= of_iomap(node
, 0);
587 pr_err("%pOFn could not get IO base\n", node
);
591 if (fapll_is_ddr_pll(fd
->base
))
592 fd
->bypass_bit_inverted
= true;
594 fd
->name
= node
->name
;
597 /* Register the parent PLL */
598 pll_clk
= clk_register(NULL
, &fd
->hw
);
602 fd
->outputs
.clks
[0] = pll_clk
;
603 fd
->outputs
.clk_num
++;
606 * Set up the child synthesizers starting at index 1 as the
607 * PLL output is at index 0. We need to check the clock-indices
608 * for numbering in case there are holes in the synth mapping,
609 * and then probe the synth register to see if it has a FREQ
610 * register available.
612 for (i
= 0; i
< MAX_FAPLL_OUTPUTS
; i
++) {
613 const char *output_name
;
614 void __iomem
*freq
, *div
;
615 struct clk
*synth_clk
;
619 if (of_property_read_string_index(node
, "clock-output-names",
623 if (of_property_read_u32_index(node
, "clock-indices", i
,
627 freq
= fd
->base
+ (output_instance
* 8);
630 /* Check for hardwired audio_pll_clk1 */
631 if (is_audio_pll_clk1(freq
)) {
635 /* Does the synthesizer have a FREQ register? */
636 v
= readl_relaxed(freq
);
640 synth_clk
= ti_fapll_synth_setup(fd
, freq
, div
, output_instance
,
641 output_name
, node
->name
,
643 if (IS_ERR(synth_clk
))
646 fd
->outputs
.clks
[output_instance
] = synth_clk
;
647 fd
->outputs
.clk_num
++;
649 clk_register_clkdev(synth_clk
, output_name
, NULL
);
652 /* Register the child synthesizers as the FAPLL outputs */
653 of_clk_add_provider(node
, of_clk_src_onecell_get
, &fd
->outputs
);
654 /* Add clock alias for the outputs */
664 clk_put(fd
->clk_bypass
);
666 clk_put(fd
->clk_ref
);
667 kfree(fd
->outputs
.clks
);
672 CLK_OF_DECLARE(ti_fapll_clock
, "ti,dm816-fapll-clock", ti_fapll_setup
);