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>
24 /* FAPLL Control Register PLL_CTRL */
25 #define FAPLL_MAIN_MULT_N_SHIFT 16
26 #define FAPLL_MAIN_DIV_P_SHIFT 8
27 #define FAPLL_MAIN_LOCK BIT(7)
28 #define FAPLL_MAIN_PLLEN BIT(3)
29 #define FAPLL_MAIN_BP BIT(2)
30 #define FAPLL_MAIN_LOC_CTL BIT(0)
32 #define FAPLL_MAIN_MAX_MULT_N 0xffff
33 #define FAPLL_MAIN_MAX_DIV_P 0xff
34 #define FAPLL_MAIN_CLEAR_MASK \
35 ((FAPLL_MAIN_MAX_MULT_N << FAPLL_MAIN_MULT_N_SHIFT) | \
36 (FAPLL_MAIN_DIV_P_SHIFT << FAPLL_MAIN_DIV_P_SHIFT) | \
39 /* FAPLL powerdown register PWD */
40 #define FAPLL_PWD_OFFSET 4
42 #define MAX_FAPLL_OUTPUTS 7
43 #define FAPLL_MAX_RETRIES 1000
45 #define to_fapll(_hw) container_of(_hw, struct fapll_data, hw)
46 #define to_synth(_hw) container_of(_hw, struct fapll_synth, hw)
48 /* The bypass bit is inverted on the ddr_pll.. */
49 #define fapll_is_ddr_pll(va) (((u32)(va) & 0xffff) == 0x0440)
52 * The audio_pll_clk1 input is hard wired to the 27MHz bypass clock,
53 * and the audio_pll_clk1 synthesizer is hardwared to 32KiHz output.
55 #define is_ddr_pll_clk1(va) (((u32)(va) & 0xffff) == 0x044c)
56 #define is_audio_pll_clk1(va) (((u32)(va) & 0xffff) == 0x04a8)
58 /* Synthesizer divider register */
59 #define SYNTH_LDMDIV1 BIT(8)
61 /* Synthesizer frequency register */
62 #define SYNTH_LDFREQ BIT(31)
64 #define SYNTH_PHASE_K 8
65 #define SYNTH_MAX_INT_DIV 0xf
66 #define SYNTH_MAX_DIV_M 0xff
73 struct clk *clk_bypass;
74 struct clk_onecell_data outputs;
75 bool bypass_bit_inverted;
80 struct fapll_data *fd;
88 static bool ti_fapll_clock_is_bypass(struct fapll_data *fd)
90 u32 v = readl_relaxed(fd->base);
92 if (fd->bypass_bit_inverted)
93 return !(v & FAPLL_MAIN_BP);
95 return !!(v & FAPLL_MAIN_BP);
98 static void ti_fapll_set_bypass(struct fapll_data *fd)
100 u32 v = readl_relaxed(fd->base);
102 if (fd->bypass_bit_inverted)
106 writel_relaxed(v, fd->base);
109 static void ti_fapll_clear_bypass(struct fapll_data *fd)
111 u32 v = readl_relaxed(fd->base);
113 if (fd->bypass_bit_inverted)
117 writel_relaxed(v, fd->base);
120 static int ti_fapll_wait_lock(struct fapll_data *fd)
122 int retries = FAPLL_MAX_RETRIES;
125 while ((v = readl_relaxed(fd->base))) {
126 if (v & FAPLL_MAIN_LOCK)
135 pr_err("%s failed to lock\n", fd->name);
140 static int ti_fapll_enable(struct clk_hw *hw)
142 struct fapll_data *fd = to_fapll(hw);
143 u32 v = readl_relaxed(fd->base);
145 v |= FAPLL_MAIN_PLLEN;
146 writel_relaxed(v, fd->base);
147 ti_fapll_wait_lock(fd);
152 static void ti_fapll_disable(struct clk_hw *hw)
154 struct fapll_data *fd = to_fapll(hw);
155 u32 v = readl_relaxed(fd->base);
157 v &= ~FAPLL_MAIN_PLLEN;
158 writel_relaxed(v, fd->base);
161 static int ti_fapll_is_enabled(struct clk_hw *hw)
163 struct fapll_data *fd = to_fapll(hw);
164 u32 v = readl_relaxed(fd->base);
166 return v & FAPLL_MAIN_PLLEN;
169 static unsigned long ti_fapll_recalc_rate(struct clk_hw *hw,
170 unsigned long parent_rate)
172 struct fapll_data *fd = to_fapll(hw);
173 u32 fapll_n, fapll_p, v;
176 if (ti_fapll_clock_is_bypass(fd))
181 /* PLL pre-divider is P and multiplier is N */
182 v = readl_relaxed(fd->base);
183 fapll_p = (v >> 8) & 0xff;
185 do_div(rate, fapll_p);
193 static u8 ti_fapll_get_parent(struct clk_hw *hw)
195 struct fapll_data *fd = to_fapll(hw);
197 if (ti_fapll_clock_is_bypass(fd))
203 static int ti_fapll_set_div_mult(unsigned long rate,
204 unsigned long parent_rate,
205 u32 *pre_div_p, u32 *mult_n)
208 * So far no luck getting decent clock with PLL divider,
209 * PLL does not seem to lock and the signal does not look
210 * right. It seems the divider can only be used together
211 * with the multiplier?
213 if (rate < parent_rate) {
214 pr_warn("FAPLL main divider rates unsupported\n");
218 *mult_n = rate / parent_rate;
219 if (*mult_n > FAPLL_MAIN_MAX_MULT_N)
226 static long ti_fapll_round_rate(struct clk_hw *hw, unsigned long rate,
227 unsigned long *parent_rate)
229 u32 pre_div_p, mult_n;
235 error = ti_fapll_set_div_mult(rate, *parent_rate,
236 &pre_div_p, &mult_n);
240 rate = *parent_rate / pre_div_p;
246 static int ti_fapll_set_rate(struct clk_hw *hw, unsigned long rate,
247 unsigned long parent_rate)
249 struct fapll_data *fd = to_fapll(hw);
250 u32 pre_div_p, mult_n, v;
256 error = ti_fapll_set_div_mult(rate, parent_rate,
257 &pre_div_p, &mult_n);
261 ti_fapll_set_bypass(fd);
262 v = readl_relaxed(fd->base);
263 v &= ~FAPLL_MAIN_CLEAR_MASK;
264 v |= pre_div_p << FAPLL_MAIN_DIV_P_SHIFT;
265 v |= mult_n << FAPLL_MAIN_MULT_N_SHIFT;
266 writel_relaxed(v, fd->base);
267 if (ti_fapll_is_enabled(hw))
268 ti_fapll_wait_lock(fd);
269 ti_fapll_clear_bypass(fd);
274 static const struct clk_ops ti_fapll_ops = {
275 .enable = ti_fapll_enable,
276 .disable = ti_fapll_disable,
277 .is_enabled = ti_fapll_is_enabled,
278 .recalc_rate = ti_fapll_recalc_rate,
279 .get_parent = ti_fapll_get_parent,
280 .round_rate = ti_fapll_round_rate,
281 .set_rate = ti_fapll_set_rate,
284 static int ti_fapll_synth_enable(struct clk_hw *hw)
286 struct fapll_synth *synth = to_synth(hw);
287 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
289 v &= ~(1 << synth->index);
290 writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
295 static void ti_fapll_synth_disable(struct clk_hw *hw)
297 struct fapll_synth *synth = to_synth(hw);
298 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
300 v |= 1 << synth->index;
301 writel_relaxed(v, synth->fd->base + FAPLL_PWD_OFFSET);
304 static int ti_fapll_synth_is_enabled(struct clk_hw *hw)
306 struct fapll_synth *synth = to_synth(hw);
307 u32 v = readl_relaxed(synth->fd->base + FAPLL_PWD_OFFSET);
309 return !(v & (1 << synth->index));
313 * See dm816x TRM chapter 1.10.3 Flying Adder PLL fore more info
315 static unsigned long ti_fapll_synth_recalc_rate(struct clk_hw *hw,
316 unsigned long parent_rate)
318 struct fapll_synth *synth = to_synth(hw);
322 /* The audio_pll_clk1 is hardwired to produce 32.768KiHz clock */
327 * PLL in bypass sets the synths in bypass mode too. The PLL rate
328 * can be also be set to 27MHz, so we can't use parent_rate to
329 * check for bypass mode.
331 if (ti_fapll_clock_is_bypass(synth->fd))
337 * Synth frequency integer and fractional divider.
338 * Note that the phase output K is 8, so the result needs
339 * to be multiplied by SYNTH_PHASE_K.
342 u32 v, synth_int_div, synth_frac_div, synth_div_freq;
344 v = readl_relaxed(synth->freq);
345 synth_int_div = (v >> 24) & 0xf;
346 synth_frac_div = v & 0xffffff;
347 synth_div_freq = (synth_int_div * 10000000) + synth_frac_div;
349 do_div(rate, synth_div_freq);
350 rate *= SYNTH_PHASE_K;
353 /* Synth post-divider M */
354 synth_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
356 return DIV_ROUND_UP_ULL(rate, synth_div_m);
359 static unsigned long ti_fapll_synth_get_frac_rate(struct clk_hw *hw,
360 unsigned long parent_rate)
362 struct fapll_synth *synth = to_synth(hw);
363 unsigned long current_rate, frac_rate;
366 current_rate = ti_fapll_synth_recalc_rate(hw, parent_rate);
367 post_div_m = readl_relaxed(synth->div) & SYNTH_MAX_DIV_M;
368 frac_rate = current_rate * post_div_m;
373 static u32 ti_fapll_synth_set_frac_rate(struct fapll_synth *synth,
375 unsigned long parent_rate)
377 u32 post_div_m, synth_int_div = 0, synth_frac_div = 0, v;
379 post_div_m = DIV_ROUND_UP_ULL((u64)parent_rate * SYNTH_PHASE_K, rate);
380 post_div_m = post_div_m / SYNTH_MAX_INT_DIV;
381 if (post_div_m > SYNTH_MAX_DIV_M)
386 for (; post_div_m < SYNTH_MAX_DIV_M; post_div_m++) {
387 synth_int_div = DIV_ROUND_UP_ULL((u64)parent_rate *
391 synth_frac_div = synth_int_div % 10000000;
392 synth_int_div /= 10000000;
394 if (synth_int_div <= SYNTH_MAX_INT_DIV)
398 if (synth_int_div > SYNTH_MAX_INT_DIV)
401 v = readl_relaxed(synth->freq);
403 v |= (synth_int_div & SYNTH_MAX_INT_DIV) << 24;
404 v |= (synth_frac_div & 0xffffff);
406 writel_relaxed(v, synth->freq);
411 static long ti_fapll_synth_round_rate(struct clk_hw *hw, unsigned long rate,
412 unsigned long *parent_rate)
414 struct fapll_synth *synth = to_synth(hw);
415 struct fapll_data *fd = synth->fd;
418 if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
421 /* Only post divider m available with no fractional divider? */
423 unsigned long frac_rate;
424 u32 synth_post_div_m;
426 frac_rate = ti_fapll_synth_get_frac_rate(hw, *parent_rate);
427 synth_post_div_m = DIV_ROUND_UP(frac_rate, rate);
428 r = DIV_ROUND_UP(frac_rate, synth_post_div_m);
432 r = *parent_rate * SYNTH_PHASE_K;
436 r = DIV_ROUND_UP_ULL(r, SYNTH_MAX_INT_DIV * SYNTH_MAX_DIV_M);
445 static int ti_fapll_synth_set_rate(struct clk_hw *hw, unsigned long rate,
446 unsigned long parent_rate)
448 struct fapll_synth *synth = to_synth(hw);
449 struct fapll_data *fd = synth->fd;
450 unsigned long frac_rate, post_rate = 0;
451 u32 post_div_m = 0, v;
453 if (ti_fapll_clock_is_bypass(fd) || !synth->div || !rate)
456 /* Produce the rate with just post divider M? */
457 frac_rate = ti_fapll_synth_get_frac_rate(hw, parent_rate);
458 if (frac_rate < rate) {
462 post_div_m = DIV_ROUND_UP(frac_rate, rate);
463 if (post_div_m && (post_div_m <= SYNTH_MAX_DIV_M))
464 post_rate = DIV_ROUND_UP(frac_rate, post_div_m);
465 if (!synth->freq && !post_rate)
469 /* Need to recalculate the fractional divider? */
470 if ((post_rate != rate) && synth->freq)
471 post_div_m = ti_fapll_synth_set_frac_rate(synth,
475 v = readl_relaxed(synth->div);
476 v &= ~SYNTH_MAX_DIV_M;
479 writel_relaxed(v, synth->div);
484 static const struct clk_ops ti_fapll_synt_ops = {
485 .enable = ti_fapll_synth_enable,
486 .disable = ti_fapll_synth_disable,
487 .is_enabled = ti_fapll_synth_is_enabled,
488 .recalc_rate = ti_fapll_synth_recalc_rate,
489 .round_rate = ti_fapll_synth_round_rate,
490 .set_rate = ti_fapll_synth_set_rate,
493 static struct clk * __init ti_fapll_synth_setup(struct fapll_data *fd,
501 struct clk_init_data *init;
502 struct fapll_synth *synth;
503 struct clk *clk = ERR_PTR(-ENOMEM);
505 init = kzalloc(sizeof(*init), GFP_KERNEL);
507 return ERR_PTR(-ENOMEM);
509 init->ops = &ti_fapll_synt_ops;
511 init->parent_names = &parent;
512 init->num_parents = 1;
514 synth = kzalloc(sizeof(*synth), GFP_KERNEL);
519 synth->index = index;
523 synth->hw.init = init;
524 synth->clk_pll = pll_clk;
526 clk = clk_register(NULL, &synth->hw);
528 pr_err("failed to register clock\n");
541 static void __init ti_fapll_setup(struct device_node *node)
543 struct fapll_data *fd;
544 struct clk_init_data *init = NULL;
545 const char *parent_name[2];
550 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
554 fd->outputs.clks = kzalloc(sizeof(struct clk *) *
555 MAX_FAPLL_OUTPUTS + 1,
557 if (!fd->outputs.clks)
560 init = kzalloc(sizeof(*init), GFP_KERNEL);
564 init->ops = &ti_fapll_ops;
565 name = ti_dt_clk_name(node);
568 init->num_parents = of_clk_get_parent_count(node);
569 if (init->num_parents != 2) {
570 pr_err("%pOFn must have two parents\n", node);
574 of_clk_parent_fill(node, parent_name, 2);
575 init->parent_names = parent_name;
577 fd->clk_ref = of_clk_get(node, 0);
578 if (IS_ERR(fd->clk_ref)) {
579 pr_err("%pOFn could not get clk_ref\n", node);
583 fd->clk_bypass = of_clk_get(node, 1);
584 if (IS_ERR(fd->clk_bypass)) {
585 pr_err("%pOFn could not get clk_bypass\n", node);
589 fd->base = of_iomap(node, 0);
591 pr_err("%pOFn could not get IO base\n", node);
595 if (fapll_is_ddr_pll(fd->base))
596 fd->bypass_bit_inverted = true;
601 /* Register the parent PLL */
602 pll_clk = clk_register(NULL, &fd->hw);
606 fd->outputs.clks[0] = pll_clk;
607 fd->outputs.clk_num++;
610 * Set up the child synthesizers starting at index 1 as the
611 * PLL output is at index 0. We need to check the clock-indices
612 * for numbering in case there are holes in the synth mapping,
613 * and then probe the synth register to see if it has a FREQ
614 * register available.
616 for (i = 0; i < MAX_FAPLL_OUTPUTS; i++) {
617 const char *output_name;
618 void __iomem *freq, *div;
619 struct clk *synth_clk;
623 if (of_property_read_string_index(node, "clock-output-names",
627 if (of_property_read_u32_index(node, "clock-indices", i,
631 freq = fd->base + (output_instance * 8);
634 /* Check for hardwired audio_pll_clk1 */
635 if (is_audio_pll_clk1(freq)) {
639 /* Does the synthesizer have a FREQ register? */
640 v = readl_relaxed(freq);
644 synth_clk = ti_fapll_synth_setup(fd, freq, div, output_instance,
645 output_name, name, pll_clk);
646 if (IS_ERR(synth_clk))
649 fd->outputs.clks[output_instance] = synth_clk;
650 fd->outputs.clk_num++;
652 clk_register_clkdev(synth_clk, output_name, NULL);
655 /* Register the child synthesizers as the FAPLL outputs */
656 of_clk_add_provider(node, of_clk_src_onecell_get, &fd->outputs);
657 /* Add clock alias for the outputs */
667 clk_put(fd->clk_bypass);
669 clk_put(fd->clk_ref);
670 kfree(fd->outputs.clks);
675 CLK_OF_DECLARE(ti_fapll_clock, "ti,dm816-fapll-clock", ti_fapll_setup);