2 * Copyright 2013 Emilio López
4 * Emilio López <emilio@elopez.com.ar>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
19 #include <linux/clkdev.h>
21 #include <linux/of_address.h>
22 #include <linux/reset-controller.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25 #include <linux/log2.h>
27 #include "clk-factors.h"
29 static DEFINE_SPINLOCK(clk_lock
);
32 * sun6i_a31_ahb1_clk_setup() - Setup function for a31 ahb1 composite clk
35 #define SUN6I_AHB1_MAX_PARENTS 4
36 #define SUN6I_AHB1_MUX_PARENT_PLL6 3
37 #define SUN6I_AHB1_MUX_SHIFT 12
38 /* un-shifted mask is what mux_clk expects */
39 #define SUN6I_AHB1_MUX_MASK 0x3
40 #define SUN6I_AHB1_MUX_GET_PARENT(reg) ((reg >> SUN6I_AHB1_MUX_SHIFT) & \
43 #define SUN6I_AHB1_DIV_SHIFT 4
44 #define SUN6I_AHB1_DIV_MASK (0x3 << SUN6I_AHB1_DIV_SHIFT)
45 #define SUN6I_AHB1_DIV_GET(reg) ((reg & SUN6I_AHB1_DIV_MASK) >> \
47 #define SUN6I_AHB1_DIV_SET(reg, div) ((reg & ~SUN6I_AHB1_DIV_MASK) | \
48 (div << SUN6I_AHB1_DIV_SHIFT))
49 #define SUN6I_AHB1_PLL6_DIV_SHIFT 6
50 #define SUN6I_AHB1_PLL6_DIV_MASK (0x3 << SUN6I_AHB1_PLL6_DIV_SHIFT)
51 #define SUN6I_AHB1_PLL6_DIV_GET(reg) ((reg & SUN6I_AHB1_PLL6_DIV_MASK) >> \
52 SUN6I_AHB1_PLL6_DIV_SHIFT)
53 #define SUN6I_AHB1_PLL6_DIV_SET(reg, div) ((reg & ~SUN6I_AHB1_PLL6_DIV_MASK) | \
54 (div << SUN6I_AHB1_PLL6_DIV_SHIFT))
56 struct sun6i_ahb1_clk
{
61 #define to_sun6i_ahb1_clk(_hw) container_of(_hw, struct sun6i_ahb1_clk, hw)
63 static unsigned long sun6i_ahb1_clk_recalc_rate(struct clk_hw
*hw
,
64 unsigned long parent_rate
)
66 struct sun6i_ahb1_clk
*ahb1
= to_sun6i_ahb1_clk(hw
);
70 /* Fetch the register value */
71 reg
= readl(ahb1
->reg
);
73 /* apply pre-divider first if parent is pll6 */
74 if (SUN6I_AHB1_MUX_GET_PARENT(reg
) == SUN6I_AHB1_MUX_PARENT_PLL6
)
75 parent_rate
/= SUN6I_AHB1_PLL6_DIV_GET(reg
) + 1;
78 rate
= parent_rate
>> SUN6I_AHB1_DIV_GET(reg
);
83 static long sun6i_ahb1_clk_round(unsigned long rate
, u8
*divp
, u8
*pre_divp
,
84 u8 parent
, unsigned long parent_rate
)
86 u8 div
, calcp
, calcm
= 1;
89 * clock can only divide, so we will never be able to achieve
90 * frequencies higher than the parent frequency
92 if (parent_rate
&& rate
> parent_rate
)
95 div
= DIV_ROUND_UP(parent_rate
, rate
);
97 /* calculate pre-divider if parent is pll6 */
98 if (parent
== SUN6I_AHB1_MUX_PARENT_PLL6
) {
101 else if (div
/ 2 < 4)
103 else if (div
/ 4 < 4)
108 calcm
= DIV_ROUND_UP(div
, 1 << calcp
);
110 calcp
= __roundup_pow_of_two(div
);
111 calcp
= calcp
> 3 ? 3 : calcp
;
114 /* we were asked to pass back divider values */
117 *pre_divp
= calcm
- 1;
120 return (parent_rate
/ calcm
) >> calcp
;
123 static int sun6i_ahb1_clk_determine_rate(struct clk_hw
*hw
,
124 struct clk_rate_request
*req
)
126 struct clk_hw
*parent
, *best_parent
= NULL
;
128 unsigned long parent_rate
, best
= 0, child_rate
, best_child_rate
= 0;
130 /* find the parent that can help provide the fastest rate <= rate */
131 num_parents
= clk_hw_get_num_parents(hw
);
132 for (i
= 0; i
< num_parents
; i
++) {
133 parent
= clk_hw_get_parent_by_index(hw
, i
);
136 if (clk_hw_get_flags(hw
) & CLK_SET_RATE_PARENT
)
137 parent_rate
= clk_hw_round_rate(parent
, req
->rate
);
139 parent_rate
= clk_hw_get_rate(parent
);
141 child_rate
= sun6i_ahb1_clk_round(req
->rate
, NULL
, NULL
, i
,
144 if (child_rate
<= req
->rate
&& child_rate
> best_child_rate
) {
145 best_parent
= parent
;
147 best_child_rate
= child_rate
;
154 req
->best_parent_hw
= best_parent
;
155 req
->best_parent_rate
= best
;
156 req
->rate
= best_child_rate
;
161 static int sun6i_ahb1_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
162 unsigned long parent_rate
)
164 struct sun6i_ahb1_clk
*ahb1
= to_sun6i_ahb1_clk(hw
);
166 u8 div
, pre_div
, parent
;
169 spin_lock_irqsave(&clk_lock
, flags
);
171 reg
= readl(ahb1
->reg
);
173 /* need to know which parent is used to apply pre-divider */
174 parent
= SUN6I_AHB1_MUX_GET_PARENT(reg
);
175 sun6i_ahb1_clk_round(rate
, &div
, &pre_div
, parent
, parent_rate
);
177 reg
= SUN6I_AHB1_DIV_SET(reg
, div
);
178 reg
= SUN6I_AHB1_PLL6_DIV_SET(reg
, pre_div
);
179 writel(reg
, ahb1
->reg
);
181 spin_unlock_irqrestore(&clk_lock
, flags
);
186 static const struct clk_ops sun6i_ahb1_clk_ops
= {
187 .determine_rate
= sun6i_ahb1_clk_determine_rate
,
188 .recalc_rate
= sun6i_ahb1_clk_recalc_rate
,
189 .set_rate
= sun6i_ahb1_clk_set_rate
,
192 static void __init
sun6i_ahb1_clk_setup(struct device_node
*node
)
195 struct sun6i_ahb1_clk
*ahb1
;
197 const char *clk_name
= node
->name
;
198 const char *parents
[SUN6I_AHB1_MAX_PARENTS
];
202 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
206 /* we have a mux, we will have >1 parents */
207 i
= of_clk_parent_fill(node
, parents
, SUN6I_AHB1_MAX_PARENTS
);
208 of_property_read_string(node
, "clock-output-names", &clk_name
);
210 ahb1
= kzalloc(sizeof(struct sun6i_ahb1_clk
), GFP_KERNEL
);
214 mux
= kzalloc(sizeof(struct clk_mux
), GFP_KERNEL
);
220 /* set up clock properties */
222 mux
->shift
= SUN6I_AHB1_MUX_SHIFT
;
223 mux
->mask
= SUN6I_AHB1_MUX_MASK
;
224 mux
->lock
= &clk_lock
;
227 clk
= clk_register_composite(NULL
, clk_name
, parents
, i
,
228 &mux
->hw
, &clk_mux_ops
,
229 &ahb1
->hw
, &sun6i_ahb1_clk_ops
,
233 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
234 clk_register_clkdev(clk
, clk_name
, NULL
);
237 CLK_OF_DECLARE(sun6i_a31_ahb1
, "allwinner,sun6i-a31-ahb1-clk", sun6i_ahb1_clk_setup
);
239 /* Maximum number of parents our clocks have */
240 #define SUNXI_MAX_PARENTS 5
243 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
244 * PLL1 rate is calculated as follows
245 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
246 * parent_rate is always 24Mhz
249 static void sun4i_get_pll1_factors(u32
*freq
, u32 parent_rate
,
250 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
254 /* Normalize value to a 6M multiple */
255 div
= *freq
/ 6000000;
256 *freq
= 6000000 * div
;
258 /* we were called to round the frequency, we can now return */
262 /* m is always zero for pll1 */
265 /* k is 1 only on these cases */
266 if (*freq
>= 768000000 || *freq
== 42000000 || *freq
== 54000000)
271 /* p will be 3 for divs under 10 */
275 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
276 else if (div
< 20 || (div
< 32 && (div
& 1)))
279 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
280 * of divs between 40-62 */
281 else if (div
< 40 || (div
< 64 && (div
& 2)))
284 /* any other entries have p = 0 */
288 /* calculate a suitable n based on k and p */
295 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
296 * PLL1 rate is calculated as follows
297 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
298 * parent_rate should always be 24MHz
300 static void sun6i_a31_get_pll1_factors(u32
*freq
, u32 parent_rate
,
301 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
304 * We can operate only on MHz, this will make our life easier
307 u32 freq_mhz
= *freq
/ 1000000;
308 u32 parent_freq_mhz
= parent_rate
/ 1000000;
311 * Round down the frequency to the closest multiple of either
314 u32 round_freq_6
= round_down(freq_mhz
, 6);
315 u32 round_freq_16
= round_down(freq_mhz
, 16);
317 if (round_freq_6
> round_freq_16
)
318 freq_mhz
= round_freq_6
;
320 freq_mhz
= round_freq_16
;
322 *freq
= freq_mhz
* 1000000;
325 * If the factors pointer are null, we were just called to
326 * round down the frequency.
332 /* If the frequency is a multiple of 32 MHz, k is always 3 */
333 if (!(freq_mhz
% 32))
335 /* If the frequency is a multiple of 9 MHz, k is always 2 */
336 else if (!(freq_mhz
% 9))
338 /* If the frequency is a multiple of 8 MHz, k is always 1 */
339 else if (!(freq_mhz
% 8))
341 /* Otherwise, we don't use the k factor */
346 * If the frequency is a multiple of 2 but not a multiple of
347 * 3, m is 3. This is the first time we use 6 here, yet we
348 * will use it on several other places.
349 * We use this number because it's the lowest frequency we can
350 * generate (with n = 0, k = 0, m = 3), so every other frequency
351 * somehow relates to this frequency.
353 if ((freq_mhz
% 6) == 2 || (freq_mhz
% 6) == 4)
356 * If the frequency is a multiple of 6MHz, but the factor is
359 else if ((freq_mhz
/ 6) & 1)
361 /* Otherwise, we end up with m = 1 */
365 /* Calculate n thanks to the above factors we already got */
366 *n
= freq_mhz
* (*m
+ 1) / ((*k
+ 1) * parent_freq_mhz
) - 1;
369 * If n end up being outbound, and that we can still decrease
372 if ((*n
+ 1) > 31 && (*m
+ 1) > 1) {
373 *n
= (*n
+ 1) / 2 - 1;
374 *m
= (*m
+ 1) / 2 - 1;
379 * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
380 * PLL1 rate is calculated as follows
381 * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
382 * parent_rate is always 24Mhz
385 static void sun8i_a23_get_pll1_factors(u32
*freq
, u32 parent_rate
,
386 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
390 /* Normalize value to a 6M multiple */
391 div
= *freq
/ 6000000;
392 *freq
= 6000000 * div
;
394 /* we were called to round the frequency, we can now return */
398 /* m is always zero for pll1 */
401 /* k is 1 only on these cases */
402 if (*freq
>= 768000000 || *freq
== 42000000 || *freq
== 54000000)
407 /* p will be 2 for divs under 20 and odd divs under 32 */
408 if (div
< 20 || (div
< 32 && (div
& 1)))
411 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
412 * of divs between 40-62 */
413 else if (div
< 40 || (div
< 64 && (div
& 2)))
416 /* any other entries have p = 0 */
420 /* calculate a suitable n based on k and p */
427 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
428 * PLL5 rate is calculated as follows
429 * rate = parent_rate * n * (k + 1)
430 * parent_rate is always 24Mhz
433 static void sun4i_get_pll5_factors(u32
*freq
, u32 parent_rate
,
434 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
438 /* Normalize value to a parent_rate multiple (24M) */
439 div
= *freq
/ parent_rate
;
440 *freq
= parent_rate
* div
;
442 /* we were called to round the frequency, we can now return */
448 else if (div
/ 2 < 31)
450 else if (div
/ 3 < 31)
455 *n
= DIV_ROUND_UP(div
, (*k
+1));
459 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6x2
460 * PLL6x2 rate is calculated as follows
461 * rate = parent_rate * (n + 1) * (k + 1)
462 * parent_rate is always 24Mhz
465 static void sun6i_a31_get_pll6_factors(u32
*freq
, u32 parent_rate
,
466 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
470 /* Normalize value to a parent_rate multiple (24M) */
471 div
= *freq
/ parent_rate
;
472 *freq
= parent_rate
* div
;
474 /* we were called to round the frequency, we can now return */
482 *n
= DIV_ROUND_UP(div
, (*k
+1)) - 1;
486 * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB
487 * AHB rate is calculated as follows
488 * rate = parent_rate >> p
491 static void sun5i_a13_get_ahb_factors(u32
*freq
, u32 parent_rate
,
492 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
497 if (parent_rate
< *freq
)
501 * user manual says valid speed is 8k ~ 276M, but tests show it
502 * can work at speeds up to 300M, just after reparenting to pll6
506 if (*freq
> 300000000)
509 div
= order_base_2(DIV_ROUND_UP(parent_rate
, *freq
));
515 *freq
= parent_rate
>> div
;
517 /* we were called to round the frequency, we can now return */
525 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
526 * APB1 rate is calculated as follows
527 * rate = (parent_rate >> p) / (m + 1);
530 static void sun4i_get_apb1_factors(u32
*freq
, u32 parent_rate
,
531 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
535 if (parent_rate
< *freq
)
538 parent_rate
= DIV_ROUND_UP(parent_rate
, *freq
);
541 if (parent_rate
> 32)
544 if (parent_rate
<= 4)
546 else if (parent_rate
<= 8)
548 else if (parent_rate
<= 16)
553 calcm
= (parent_rate
>> calcp
) - 1;
555 *freq
= (parent_rate
>> calcp
) / (calcm
+ 1);
557 /* we were called to round the frequency, we can now return */
569 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
570 * CLK_OUT rate is calculated as follows
571 * rate = (parent_rate >> p) / (m + 1);
574 static void sun7i_a20_get_out_factors(u32
*freq
, u32 parent_rate
,
575 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
577 u8 div
, calcm
, calcp
;
579 /* These clocks can only divide, so we will never be able to achieve
580 * frequencies higher than the parent frequency */
581 if (*freq
> parent_rate
)
584 div
= DIV_ROUND_UP(parent_rate
, *freq
);
588 else if (div
/ 2 < 32)
590 else if (div
/ 4 < 32)
595 calcm
= DIV_ROUND_UP(div
, 1 << calcp
);
597 *freq
= (parent_rate
>> calcp
) / calcm
;
599 /* we were called to round the frequency, we can now return */
608 * sunxi_factors_clk_setup() - Setup function for factor clocks
611 static struct clk_factors_config sun4i_pll1_config
= {
622 static struct clk_factors_config sun6i_a31_pll1_config
= {
632 static struct clk_factors_config sun8i_a23_pll1_config
= {
644 static struct clk_factors_config sun4i_pll5_config
= {
651 static struct clk_factors_config sun6i_a31_pll6_config
= {
659 static struct clk_factors_config sun5i_a13_ahb_config
= {
664 static struct clk_factors_config sun4i_apb1_config
= {
671 /* user manual says "n" but it's really "p" */
672 static struct clk_factors_config sun7i_a20_out_config
= {
679 static const struct factors_data sun4i_pll1_data __initconst
= {
681 .table
= &sun4i_pll1_config
,
682 .getter
= sun4i_get_pll1_factors
,
685 static const struct factors_data sun6i_a31_pll1_data __initconst
= {
687 .table
= &sun6i_a31_pll1_config
,
688 .getter
= sun6i_a31_get_pll1_factors
,
691 static const struct factors_data sun8i_a23_pll1_data __initconst
= {
693 .table
= &sun8i_a23_pll1_config
,
694 .getter
= sun8i_a23_get_pll1_factors
,
697 static const struct factors_data sun7i_a20_pll4_data __initconst
= {
699 .table
= &sun4i_pll5_config
,
700 .getter
= sun4i_get_pll5_factors
,
703 static const struct factors_data sun4i_pll5_data __initconst
= {
705 .table
= &sun4i_pll5_config
,
706 .getter
= sun4i_get_pll5_factors
,
710 static const struct factors_data sun4i_pll6_data __initconst
= {
712 .table
= &sun4i_pll5_config
,
713 .getter
= sun4i_get_pll5_factors
,
717 static const struct factors_data sun6i_a31_pll6_data __initconst
= {
719 .table
= &sun6i_a31_pll6_config
,
720 .getter
= sun6i_a31_get_pll6_factors
,
724 static const struct factors_data sun5i_a13_ahb_data __initconst
= {
726 .muxmask
= BIT(1) | BIT(0),
727 .table
= &sun5i_a13_ahb_config
,
728 .getter
= sun5i_a13_get_ahb_factors
,
731 static const struct factors_data sun4i_apb1_data __initconst
= {
733 .muxmask
= BIT(1) | BIT(0),
734 .table
= &sun4i_apb1_config
,
735 .getter
= sun4i_get_apb1_factors
,
738 static const struct factors_data sun7i_a20_out_data __initconst
= {
741 .muxmask
= BIT(1) | BIT(0),
742 .table
= &sun7i_a20_out_config
,
743 .getter
= sun7i_a20_get_out_factors
,
746 static struct clk
* __init
sunxi_factors_clk_setup(struct device_node
*node
,
747 const struct factors_data
*data
)
751 reg
= of_iomap(node
, 0);
753 pr_err("Could not get registers for factors-clk: %s\n",
758 return sunxi_factors_register(node
, data
, &clk_lock
, reg
);
764 * sunxi_mux_clk_setup() - Setup function for muxes
767 #define SUNXI_MUX_GATE_WIDTH 2
773 static const struct mux_data sun4i_cpu_mux_data __initconst
= {
777 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst
= {
781 static void __init
sunxi_mux_clk_setup(struct device_node
*node
,
782 struct mux_data
*data
)
785 const char *clk_name
= node
->name
;
786 const char *parents
[SUNXI_MAX_PARENTS
];
790 reg
= of_iomap(node
, 0);
792 i
= of_clk_parent_fill(node
, parents
, SUNXI_MAX_PARENTS
);
793 of_property_read_string(node
, "clock-output-names", &clk_name
);
795 clk
= clk_register_mux(NULL
, clk_name
, parents
, i
,
796 CLK_SET_RATE_PARENT
, reg
,
797 data
->shift
, SUNXI_MUX_GATE_WIDTH
,
801 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
802 clk_register_clkdev(clk
, clk_name
, NULL
);
809 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
816 const struct clk_div_table
*table
;
819 static const struct div_data sun4i_axi_data __initconst
= {
825 static const struct clk_div_table sun8i_a23_axi_table
[] __initconst
= {
826 { .val
= 0, .div
= 1 },
827 { .val
= 1, .div
= 2 },
828 { .val
= 2, .div
= 3 },
829 { .val
= 3, .div
= 4 },
830 { .val
= 4, .div
= 4 },
831 { .val
= 5, .div
= 4 },
832 { .val
= 6, .div
= 4 },
833 { .val
= 7, .div
= 4 },
837 static const struct div_data sun8i_a23_axi_data __initconst
= {
839 .table
= sun8i_a23_axi_table
,
842 static const struct div_data sun4i_ahb_data __initconst
= {
848 static const struct clk_div_table sun4i_apb0_table
[] __initconst
= {
849 { .val
= 0, .div
= 2 },
850 { .val
= 1, .div
= 2 },
851 { .val
= 2, .div
= 4 },
852 { .val
= 3, .div
= 8 },
856 static const struct div_data sun4i_apb0_data __initconst
= {
860 .table
= sun4i_apb0_table
,
863 static void __init
sunxi_divider_clk_setup(struct device_node
*node
,
864 struct div_data
*data
)
867 const char *clk_name
= node
->name
;
868 const char *clk_parent
;
871 reg
= of_iomap(node
, 0);
873 clk_parent
= of_clk_get_parent_name(node
, 0);
875 of_property_read_string(node
, "clock-output-names", &clk_name
);
877 clk
= clk_register_divider_table(NULL
, clk_name
, clk_parent
, 0,
878 reg
, data
->shift
, data
->width
,
879 data
->pow
? CLK_DIVIDER_POWER_OF_TWO
: 0,
880 data
->table
, &clk_lock
);
882 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
883 clk_register_clkdev(clk
, clk_name
, NULL
);
890 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
893 #define SUNXI_GATES_MAX_SIZE 64
896 DECLARE_BITMAP(mask
, SUNXI_GATES_MAX_SIZE
);
900 * sunxi_divs_clk_setup() helper data
903 #define SUNXI_DIVS_MAX_QTY 4
904 #define SUNXI_DIVISOR_WIDTH 2
907 const struct factors_data
*factors
; /* data for the factor clock */
908 int ndivs
; /* number of outputs */
910 * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
911 * self or base factor clock refers to the output from the pll
912 * itself. The remaining refer to fixed or configurable divider
916 u8 self
; /* is it the base factor clock? (only one) */
917 u8 fixed
; /* is it a fixed divisor? if not... */
918 struct clk_div_table
*table
; /* is it a table based divisor? */
919 u8 shift
; /* otherwise it's a normal divisor with this shift */
920 u8 pow
; /* is it power-of-two based? */
921 u8 gate
; /* is it independently gateable? */
922 } div
[SUNXI_DIVS_MAX_QTY
];
925 static struct clk_div_table pll6_sata_tbl
[] = {
926 { .val
= 0, .div
= 6, },
927 { .val
= 1, .div
= 12, },
928 { .val
= 2, .div
= 18, },
929 { .val
= 3, .div
= 24, },
933 static const struct divs_data pll5_divs_data __initconst
= {
934 .factors
= &sun4i_pll5_data
,
937 { .shift
= 0, .pow
= 0, }, /* M, DDR */
938 { .shift
= 16, .pow
= 1, }, /* P, other */
939 /* No output for the base factor clock */
943 static const struct divs_data pll6_divs_data __initconst
= {
944 .factors
= &sun4i_pll6_data
,
947 { .shift
= 0, .table
= pll6_sata_tbl
, .gate
= 14 }, /* M, SATA */
948 { .fixed
= 2 }, /* P, other */
949 { .self
= 1 }, /* base factor clock, 2x */
950 { .fixed
= 4 }, /* pll6 / 4, used as ahb input */
954 static const struct divs_data sun6i_a31_pll6_divs_data __initconst
= {
955 .factors
= &sun6i_a31_pll6_data
,
958 { .fixed
= 2 }, /* normal output */
959 { .self
= 1 }, /* base factor clock, 2x */
964 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
966 * These clocks look something like this
967 * ________________________
968 * | ___divisor 1---|----> to consumer
969 * parent >--| pll___/___divisor 2---|----> to consumer
970 * | \_______________|____> to consumer
971 * |________________________|
974 static void __init
sunxi_divs_clk_setup(struct device_node
*node
,
975 struct divs_data
*data
)
977 struct clk_onecell_data
*clk_data
;
979 const char *clk_name
;
980 struct clk
**clks
, *pclk
;
981 struct clk_hw
*gate_hw
, *rate_hw
;
982 const struct clk_ops
*rate_ops
;
983 struct clk_gate
*gate
= NULL
;
984 struct clk_fixed_factor
*fix_factor
;
985 struct clk_divider
*divider
;
987 int ndivs
= SUNXI_DIVS_MAX_QTY
, i
= 0;
990 /* if number of children known, use it */
994 /* Set up factor clock that we will be dividing */
995 pclk
= sunxi_factors_clk_setup(node
, data
->factors
);
996 parent
= __clk_get_name(pclk
);
998 reg
= of_iomap(node
, 0);
1000 clk_data
= kmalloc(sizeof(struct clk_onecell_data
), GFP_KERNEL
);
1004 clks
= kcalloc(ndivs
, sizeof(*clks
), GFP_KERNEL
);
1008 clk_data
->clks
= clks
;
1010 /* It's not a good idea to have automatic reparenting changing
1012 clkflags
= !strcmp("pll5", parent
) ? 0 : CLK_SET_RATE_PARENT
;
1014 for (i
= 0; i
< ndivs
; i
++) {
1015 if (of_property_read_string_index(node
, "clock-output-names",
1019 /* If this is the base factor clock, only update clks */
1020 if (data
->div
[i
].self
) {
1021 clk_data
->clks
[i
] = pclk
;
1029 /* If this leaf clock can be gated, create a gate */
1030 if (data
->div
[i
].gate
) {
1031 gate
= kzalloc(sizeof(*gate
), GFP_KERNEL
);
1036 gate
->bit_idx
= data
->div
[i
].gate
;
1037 gate
->lock
= &clk_lock
;
1039 gate_hw
= &gate
->hw
;
1042 /* Leaves can be fixed or configurable divisors */
1043 if (data
->div
[i
].fixed
) {
1044 fix_factor
= kzalloc(sizeof(*fix_factor
), GFP_KERNEL
);
1048 fix_factor
->mult
= 1;
1049 fix_factor
->div
= data
->div
[i
].fixed
;
1051 rate_hw
= &fix_factor
->hw
;
1052 rate_ops
= &clk_fixed_factor_ops
;
1054 divider
= kzalloc(sizeof(*divider
), GFP_KERNEL
);
1058 flags
= data
->div
[i
].pow
? CLK_DIVIDER_POWER_OF_TWO
: 0;
1061 divider
->shift
= data
->div
[i
].shift
;
1062 divider
->width
= SUNXI_DIVISOR_WIDTH
;
1063 divider
->flags
= flags
;
1064 divider
->lock
= &clk_lock
;
1065 divider
->table
= data
->div
[i
].table
;
1067 rate_hw
= ÷r
->hw
;
1068 rate_ops
= &clk_divider_ops
;
1071 /* Wrap the (potential) gate and the divisor on a composite
1072 * clock to unify them */
1073 clks
[i
] = clk_register_composite(NULL
, clk_name
, &parent
, 1,
1076 gate_hw
, &clk_gate_ops
,
1079 WARN_ON(IS_ERR(clk_data
->clks
[i
]));
1080 clk_register_clkdev(clks
[i
], clk_name
, NULL
);
1083 /* Adjust to the real max */
1084 clk_data
->clk_num
= i
;
1086 of_clk_add_provider(node
, of_clk_src_onecell_get
, clk_data
);
1100 /* Matches for factors clocks */
1101 static const struct of_device_id clk_factors_match
[] __initconst
= {
1102 {.compatible
= "allwinner,sun4i-a10-pll1-clk", .data
= &sun4i_pll1_data
,},
1103 {.compatible
= "allwinner,sun6i-a31-pll1-clk", .data
= &sun6i_a31_pll1_data
,},
1104 {.compatible
= "allwinner,sun8i-a23-pll1-clk", .data
= &sun8i_a23_pll1_data
,},
1105 {.compatible
= "allwinner,sun7i-a20-pll4-clk", .data
= &sun7i_a20_pll4_data
,},
1106 {.compatible
= "allwinner,sun5i-a13-ahb-clk", .data
= &sun5i_a13_ahb_data
,},
1107 {.compatible
= "allwinner,sun4i-a10-apb1-clk", .data
= &sun4i_apb1_data
,},
1108 {.compatible
= "allwinner,sun7i-a20-out-clk", .data
= &sun7i_a20_out_data
,},
1112 /* Matches for divider clocks */
1113 static const struct of_device_id clk_div_match
[] __initconst
= {
1114 {.compatible
= "allwinner,sun4i-a10-axi-clk", .data
= &sun4i_axi_data
,},
1115 {.compatible
= "allwinner,sun8i-a23-axi-clk", .data
= &sun8i_a23_axi_data
,},
1116 {.compatible
= "allwinner,sun4i-a10-ahb-clk", .data
= &sun4i_ahb_data
,},
1117 {.compatible
= "allwinner,sun4i-a10-apb0-clk", .data
= &sun4i_apb0_data
,},
1121 /* Matches for divided outputs */
1122 static const struct of_device_id clk_divs_match
[] __initconst
= {
1123 {.compatible
= "allwinner,sun4i-a10-pll5-clk", .data
= &pll5_divs_data
,},
1124 {.compatible
= "allwinner,sun4i-a10-pll6-clk", .data
= &pll6_divs_data
,},
1125 {.compatible
= "allwinner,sun6i-a31-pll6-clk", .data
= &sun6i_a31_pll6_divs_data
,},
1129 /* Matches for mux clocks */
1130 static const struct of_device_id clk_mux_match
[] __initconst
= {
1131 {.compatible
= "allwinner,sun4i-a10-cpu-clk", .data
= &sun4i_cpu_mux_data
,},
1132 {.compatible
= "allwinner,sun6i-a31-ahb1-mux-clk", .data
= &sun6i_a31_ahb1_mux_data
,},
1137 static void __init
of_sunxi_table_clock_setup(const struct of_device_id
*clk_match
,
1140 struct device_node
*np
;
1141 const struct div_data
*data
;
1142 const struct of_device_id
*match
;
1143 void (*setup_function
)(struct device_node
*, const void *) = function
;
1145 for_each_matching_node_and_match(np
, clk_match
, &match
) {
1147 setup_function(np
, data
);
1151 static void __init
sunxi_init_clocks(const char *clocks
[], int nclocks
)
1155 /* Register divided output clocks */
1156 of_sunxi_table_clock_setup(clk_divs_match
, sunxi_divs_clk_setup
);
1158 /* Register factor clocks */
1159 of_sunxi_table_clock_setup(clk_factors_match
, sunxi_factors_clk_setup
);
1161 /* Register divider clocks */
1162 of_sunxi_table_clock_setup(clk_div_match
, sunxi_divider_clk_setup
);
1164 /* Register mux clocks */
1165 of_sunxi_table_clock_setup(clk_mux_match
, sunxi_mux_clk_setup
);
1167 /* Protect the clocks that needs to stay on */
1168 for (i
= 0; i
< nclocks
; i
++) {
1169 struct clk
*clk
= clk_get(NULL
, clocks
[i
]);
1172 clk_prepare_enable(clk
);
1176 static const char *sun4i_a10_critical_clocks
[] __initdata
= {
1180 static void __init
sun4i_a10_init_clocks(struct device_node
*node
)
1182 sunxi_init_clocks(sun4i_a10_critical_clocks
,
1183 ARRAY_SIZE(sun4i_a10_critical_clocks
));
1185 CLK_OF_DECLARE(sun4i_a10_clk_init
, "allwinner,sun4i-a10", sun4i_a10_init_clocks
);
1187 static const char *sun5i_critical_clocks
[] __initdata
= {
1192 static void __init
sun5i_init_clocks(struct device_node
*node
)
1194 sunxi_init_clocks(sun5i_critical_clocks
,
1195 ARRAY_SIZE(sun5i_critical_clocks
));
1197 CLK_OF_DECLARE(sun5i_a10s_clk_init
, "allwinner,sun5i-a10s", sun5i_init_clocks
);
1198 CLK_OF_DECLARE(sun5i_a13_clk_init
, "allwinner,sun5i-a13", sun5i_init_clocks
);
1199 CLK_OF_DECLARE(sun5i_r8_clk_init
, "allwinner,sun5i-r8", sun5i_init_clocks
);
1200 CLK_OF_DECLARE(sun7i_a20_clk_init
, "allwinner,sun7i-a20", sun5i_init_clocks
);
1202 static const char *sun6i_critical_clocks
[] __initdata
= {
1206 static void __init
sun6i_init_clocks(struct device_node
*node
)
1208 sunxi_init_clocks(sun6i_critical_clocks
,
1209 ARRAY_SIZE(sun6i_critical_clocks
));
1211 CLK_OF_DECLARE(sun6i_a31_clk_init
, "allwinner,sun6i-a31", sun6i_init_clocks
);
1212 CLK_OF_DECLARE(sun6i_a31s_clk_init
, "allwinner,sun6i-a31s", sun6i_init_clocks
);
1213 CLK_OF_DECLARE(sun8i_a23_clk_init
, "allwinner,sun8i-a23", sun6i_init_clocks
);
1214 CLK_OF_DECLARE(sun8i_a33_clk_init
, "allwinner,sun8i-a33", sun6i_init_clocks
);
1216 static void __init
sun9i_init_clocks(struct device_node
*node
)
1218 sunxi_init_clocks(NULL
, 0);
1220 CLK_OF_DECLARE(sun9i_a80_clk_init
, "allwinner,sun9i-a80", sun9i_init_clocks
);