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>
16 #include <linux/math64.h>
18 #include <linux/of_address.h>
19 #include <linux/clk/ti.h>
21 /* FAPLL Control Register PLL_CTRL */
22 #define FAPLL_MAIN_MULT_N_SHIFT 16
23 #define FAPLL_MAIN_DIV_P_SHIFT 8
24 #define FAPLL_MAIN_LOCK BIT(7)
25 #define FAPLL_MAIN_PLLEN BIT(3)
26 #define FAPLL_MAIN_BP BIT(2)
27 #define FAPLL_MAIN_LOC_CTL BIT(0)
29 #define FAPLL_MAIN_MAX_MULT_N 0xffff
30 #define FAPLL_MAIN_MAX_DIV_P 0xff
31 #define FAPLL_MAIN_CLEAR_MASK \
32 ((FAPLL_MAIN_MAX_MULT_N << FAPLL_MAIN_MULT_N_SHIFT) | \
33 (FAPLL_MAIN_DIV_P_SHIFT << FAPLL_MAIN_DIV_P_SHIFT) | \
36 /* FAPLL powerdown register PWD */
37 #define FAPLL_PWD_OFFSET 4
39 #define MAX_FAPLL_OUTPUTS 7
40 #define FAPLL_MAX_RETRIES 1000
42 #define to_fapll(_hw) container_of(_hw, struct fapll_data, hw)
43 #define to_synth(_hw) container_of(_hw, struct fapll_synth, hw)
45 /* The bypass bit is inverted on the ddr_pll.. */
46 #define fapll_is_ddr_pll(va) (((u32)(va) & 0xffff) == 0x0440)
49 * The audio_pll_clk1 input is hard wired to the 27MHz bypass clock,
50 * and the audio_pll_clk1 synthesizer is hardwared to 32KiHz output.
52 #define is_ddr_pll_clk1(va) (((u32)(va) & 0xffff) == 0x044c)
53 #define is_audio_pll_clk1(va) (((u32)(va) & 0xffff) == 0x04a8)
55 /* Synthesizer divider register */
56 #define SYNTH_LDMDIV1 BIT(8)
58 /* Synthesizer frequency register */
59 #define SYNTH_LDFREQ BIT(31)
61 #define SYNTH_PHASE_K 8
62 #define SYNTH_MAX_INT_DIV 0xf
63 #define SYNTH_MAX_DIV_M 0xff
70 struct clk *clk_bypass;
71 struct clk_onecell_data outputs;
72 bool bypass_bit_inverted;
77 struct fapll_data *fd;
85 static bool ti_fapll_clock_is_bypass(struct fapll_data *fd)
87 u32 v = readl_relaxed(fd->base);
89 if (fd->bypass_bit_inverted)
90 return !(v & FAPLL_MAIN_BP);
92 return !!(v & FAPLL_MAIN_BP);
95 static void ti_fapll_set_bypass(struct fapll_data *fd)
97 u32 v = readl_relaxed(fd->base);
99 if (fd->bypass_bit_inverted)
103 writel_relaxed(v, fd->base);
106 static void ti_fapll_clear_bypass(struct fapll_data *fd)
108 u32 v = readl_relaxed(fd->base);
110 if (fd->bypass_bit_inverted)
114 writel_relaxed(v, fd->base);
117 static int ti_fapll_wait_lock(struct fapll_data *fd)
119 int retries = FAPLL_MAX_RETRIES;
122 while ((v = readl_relaxed(fd->base))) {
123 if (v & FAPLL_MAIN_LOCK)
132 pr_err("%s failed to lock\n", fd->name);
137 static int ti_fapll_enable(struct clk_hw *hw)
139 struct fapll_data *fd = to_fapll(hw);
140 u32 v = readl_relaxed(fd->base);
142 v |= FAPLL_MAIN_PLLEN;
143 writel_relaxed(v, fd->base);
144 ti_fapll_wait_lock(fd);
149 static void ti_fapll_disable(struct clk_hw *hw)
151 struct fapll_data *fd = to_fapll(hw);
152 u32 v = readl_relaxed(fd->base);
154 v &= ~FAPLL_MAIN_PLLEN;
155 writel_relaxed(v, fd->base);
158 static int ti_fapll_is_enabled(struct clk_hw *hw)
160 struct fapll_data *fd = to_fapll(hw);
161 u32 v = readl_relaxed(fd->base);
163 return v & FAPLL_MAIN_PLLEN;
166 static unsigned long ti_fapll_recalc_rate(struct clk_hw *hw,
167 unsigned long parent_rate)
169 struct fapll_data *fd = to_fapll(hw);
170 u32 fapll_n, fapll_p, v;
173 if (ti_fapll_clock_is_bypass(fd))
178 /* PLL pre-divider is P and multiplier is N */
179 v = readl_relaxed(fd->base);
180 fapll_p = (v >> 8) & 0xff;
182 do_div(rate, fapll_p);
190 static u8 ti_fapll_get_parent(struct clk_hw *hw)
192 struct fapll_data *fd = to_fapll(hw);
194 if (ti_fapll_clock_is_bypass(fd))
200 static int ti_fapll_set_div_mult(unsigned long rate,
201 unsigned long parent_rate,
202 u32 *pre_div_p, u32 *mult_n)
205 * So far no luck getting decent clock with PLL divider,
206 * PLL does not seem to lock and the signal does not look
207 * right. It seems the divider can only be used together
208 * with the multiplier?
210 if (rate < parent_rate) {
211 pr_warn("FAPLL main divider rates unsupported\n");
215 *mult_n = rate / parent_rate;
216 if (*mult_n > FAPLL_MAIN_MAX_MULT_N)
223 static long ti_fapll_round_rate(struct clk_hw *hw, unsigned long rate,
224 unsigned long *parent_rate)
226 u32 pre_div_p, mult_n;
232 error = ti_fapll_set_div_mult(rate, *parent_rate,
233 &pre_div_p, &mult_n);
237 rate = *parent_rate / pre_div_p;
243 static int ti_fapll_set_rate(struct clk_hw *hw, unsigned long rate,
244 unsigned long parent_rate)
246 struct fapll_data *fd = to_fapll(hw);
247 u32 pre_div_p, mult_n, v;
253 error = ti_fapll_set_div_mult(rate, parent_rate,
254 &pre_div_p, &mult_n);
258 ti_fapll_set_bypass(fd);
259 v = readl_relaxed(fd->base);
260 v &= ~FAPLL_MAIN_CLEAR_MASK;
261 v |= pre_div_p << FAPLL_MAIN_DIV_P_SHIFT;
262 v |= mult_n << FAPLL_MAIN_MULT_N_SHIFT;
263 writel_relaxed(v, fd->base);
264 if (ti_fapll_is_enabled(hw))
265 ti_fapll_wait_lock(fd);
266 ti_fapll_clear_bypass(fd);
271 static const struct clk_ops ti_fapll_ops = {
272 .enable = ti_fapll_enable,
273 .disable = ti_fapll_disable,
274 .is_enabled = ti_fapll_is_enabled,
275 .recalc_rate = ti_fapll_recalc_rate,
276 .get_parent = ti_fapll_get_parent,
277 .round_rate = ti_fapll_round_rate,
278 .set_rate = ti_fapll_set_rate,
281 static int ti_fapll_synth_enable(struct clk_hw *hw)
283 struct fapll_synth *synth = to_synth(hw);
284 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
286 v &= ~(1 << synth->index);
287 writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
292 static void ti_fapll_synth_disable(struct clk_hw *hw)
294 struct fapll_synth *synth = to_synth(hw);
295 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
297 v |= 1 << synth->index;
298 writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
301 static int ti_fapll_synth_is_enabled(struct clk_hw *hw)
303 struct fapll_synth *synth = to_synth(hw);
304 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
306 return !(v & (1 << synth->index));
310 * See dm816x TRM chapter 1.10.3 Flying Adder PLL fore more info
312 static unsigned long ti_fapll_synth_recalc_rate(struct clk_hw *hw,
313 unsigned long parent_rate)
315 struct fapll_synth *synth = to_synth(hw);
319 /* The audio_pll_clk1 is hardwired to produce 32.768KiHz clock */
324 * PLL in bypass sets the synths in bypass mode too. The PLL rate
325 * can be also be set to 27MHz, so we can't use parent_rate to
326 * check for bypass mode.
328 if (ti_fapll_clock_is_bypass(synth->fd))
334 * Synth frequency integer and fractional divider.
335 * Note that the phase output K is 8, so the result needs
336 * to be multiplied by SYNTH_PHASE_K.
339 u32 v, synth_int_div, synth_frac_div, synth_div_freq;
341 v = readl_relaxed(synth->freq);
342 synth_int_div = (v >> 24) & 0xf;
343 synth_frac_div = v & 0xffffff;
344 synth_div_freq = (synth_int_div * 10000000) + synth_frac_div;
346 do_div(rate, synth_div_freq);
347 rate *= SYNTH_PHASE_K;
350 /* Synth post-divider M */
351 synth_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
353 return DIV_ROUND_UP_ULL(rate, synth_div_m);
356 static unsigned long ti_fapll_synth_get_frac_rate(struct clk_hw *hw,
357 unsigned long parent_rate)
359 struct fapll_synth *synth = to_synth(hw);
360 unsigned long current_rate, frac_rate;
363 current_rate = ti_fapll_synth_recalc_rate(hw, parent_rate);
364 post_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
365 frac_rate = current_rate * post_div_m;
370 static u32 ti_fapll_synth_set_frac_rate(struct fapll_synth *synth,
372 unsigned long parent_rate)
374 u32 post_div_m, synth_int_div = 0, synth_frac_div = 0, v;
376 post_div_m = DIV_ROUND_UP_ULL((u64)parent_rate * SYNTH_PHASE_K, rate);
377 post_div_m = post_div_m / SYNTH_MAX_INT_DIV;
378 if (post_div_m > SYNTH_MAX_DIV_M)
383 for (; post_div_m < SYNTH_MAX_DIV_M; post_div_m++) {
384 synth_int_div = DIV_ROUND_UP_ULL((u64)parent_rate *
388 synth_frac_div = synth_int_div % 10000000;
389 synth_int_div /= 10000000;
391 if (synth_int_div <= SYNTH_MAX_INT_DIV)
395 if (synth_int_div > SYNTH_MAX_INT_DIV)
398 v = readl_relaxed(synth->freq);
400 v |= (synth_int_div & SYNTH_MAX_INT_DIV) << 24;
401 v |= (synth_frac_div & 0xffffff);
403 writel_relaxed(v, synth->freq);
408 static long ti_fapll_synth_round_rate(struct clk_hw *hw, unsigned long rate,
409 unsigned long *parent_rate)
411 struct fapll_synth *synth = to_synth(hw);
412 struct fapll_data *fd = synth->fd;
415 if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
418 /* Only post divider m available with no fractional divider? */
420 unsigned long frac_rate;
421 u32 synth_post_div_m;
423 frac_rate = ti_fapll_synth_get_frac_rate(hw, *parent_rate);
424 synth_post_div_m = DIV_ROUND_UP(frac_rate, rate);
425 r = DIV_ROUND_UP(frac_rate, synth_post_div_m);
429 r = *parent_rate * SYNTH_PHASE_K;
433 r = DIV_ROUND_UP_ULL(r, SYNTH_MAX_INT_DIV * SYNTH_MAX_DIV_M);
442 static int ti_fapll_synth_set_rate(struct clk_hw *hw, unsigned long rate,
443 unsigned long parent_rate)
445 struct fapll_synth *synth = to_synth(hw);
446 struct fapll_data *fd = synth->fd;
447 unsigned long frac_rate, post_rate = 0;
448 u32 post_div_m = 0, v;
450 if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
453 /* Produce the rate with just post divider M? */
454 frac_rate = ti_fapll_synth_get_frac_rate(hw, parent_rate);
455 if (frac_rate < rate) {
459 post_div_m = DIV_ROUND_UP(frac_rate, rate);
460 if (post_div_m && (post_div_m <= SYNTH_MAX_DIV_M))
461 post_rate = DIV_ROUND_UP(frac_rate, post_div_m);
462 if (!synth->freq && !post_rate)
466 /* Need to recalculate the fractional divider? */
467 if ((post_rate != rate) && synth->freq)
468 post_div_m = ti_fapll_synth_set_frac_rate(synth,
472 v = readl_relaxed(synth->div);
473 v &= ~SYNTH_MAX_DIV_M;
476 writel_relaxed(v, synth->div);
481 static const struct clk_ops ti_fapll_synt_ops = {
482 .enable = ti_fapll_synth_enable,
483 .disable = ti_fapll_synth_disable,
484 .is_enabled = ti_fapll_synth_is_enabled,
485 .recalc_rate = ti_fapll_synth_recalc_rate,
486 .round_rate = ti_fapll_synth_round_rate,
487 .set_rate = ti_fapll_synth_set_rate,
490 static struct clk * __init ti_fapll_synth_setup(struct fapll_data *fd,
498 struct clk_init_data *init;
499 struct fapll_synth *synth;
501 init = kzalloc(sizeof(*init), GFP_KERNEL);
503 return ERR_PTR(-ENOMEM);
505 init->ops = &ti_fapll_synt_ops;
507 init->parent_names = &parent;
508 init->num_parents = 1;
510 synth = kzalloc(sizeof(*synth), GFP_KERNEL);
515 synth->index = index;
519 synth->hw.init = init;
520 synth->clk_pll = pll_clk;
522 return clk_register(NULL, &synth->hw);
528 return ERR_PTR(-ENOMEM);
531 static void __init ti_fapll_setup(struct device_node *node)
533 struct fapll_data *fd;
534 struct clk_init_data *init = NULL;
535 const char *parent_name[2];
539 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
543 fd->outputs.clks = kzalloc(sizeof(struct clk *) *
544 MAX_FAPLL_OUTPUTS + 1,
546 if (!fd->outputs.clks)
549 init = kzalloc(sizeof(*init), GFP_KERNEL);
553 init->ops = &ti_fapll_ops;
554 init->name = node->name;
556 init->num_parents = of_clk_get_parent_count(node);
557 if (init->num_parents != 2) {
558 pr_err("%pOFn must have two parents\n", node);
562 of_clk_parent_fill(node, parent_name, 2);
563 init->parent_names = parent_name;
565 fd->clk_ref = of_clk_get(node, 0);
566 if (IS_ERR(fd->clk_ref)) {
567 pr_err("%pOFn could not get clk_ref\n", node);
571 fd->clk_bypass = of_clk_get(node, 1);
572 if (IS_ERR(fd->clk_bypass)) {
573 pr_err("%pOFn could not get clk_bypass\n", node);
577 fd->base = of_iomap(node, 0);
579 pr_err("%pOFn could not get IO base\n", node);
583 if (fapll_is_ddr_pll(fd->base))
584 fd->bypass_bit_inverted = true;
586 fd->name = node->name;
589 /* Register the parent PLL */
590 pll_clk = clk_register(NULL, &fd->hw);
594 fd->outputs.clks[0] = pll_clk;
595 fd->outputs.clk_num++;
598 * Set up the child synthesizers starting at index 1 as the
599 * PLL output is at index 0. We need to check the clock-indices
600 * for numbering in case there are holes in the synth mapping,
601 * and then probe the synth register to see if it has a FREQ
602 * register available.
604 for (i = 0; i < MAX_FAPLL_OUTPUTS; i++) {
605 const char *output_name;
606 void __iomem *freq, *div;
607 struct clk *synth_clk;
611 if (of_property_read_string_index(node, "clock-output-names",
615 if (of_property_read_u32_index(node, "clock-indices", i,
619 freq = fd->base + (output_instance * 8);
622 /* Check for hardwired audio_pll_clk1 */
623 if (is_audio_pll_clk1(freq)) {
627 /* Does the synthesizer have a FREQ register? */
628 v = readl_relaxed(freq);
632 synth_clk = ti_fapll_synth_setup(fd, freq, div, output_instance,
633 output_name, node->name,
635 if (IS_ERR(synth_clk))
638 fd->outputs.clks[output_instance] = synth_clk;
639 fd->outputs.clk_num++;
641 clk_register_clkdev(synth_clk, output_name, NULL);
644 /* Register the child synthesizers as the FAPLL outputs */
645 of_clk_add_provider(node, of_clk_src_onecell_get, &fd->outputs);
646 /* Add clock alias for the outputs */
656 clk_put(fd->clk_bypass);
658 clk_put(fd->clk_ref);
659 kfree(fd->outputs.clks);
664 CLK_OF_DECLARE(ti_fapll_clock, "ti,dm816-fapll-clock", ti_fapll_setup);