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-provider.h>
18 #include <linux/clkdev.h>
20 #include <linux/of_address.h>
21 #include <linux/reset-controller.h>
22 #include <linux/spinlock.h>
23 #include <linux/log2.h>
25 #include "clk-factors.h"
27 static DEFINE_SPINLOCK(clk_lock
);
30 * sun6i_a31_ahb1_clk_setup() - Setup function for a31 ahb1 composite clk
33 #define SUN6I_AHB1_MAX_PARENTS 4
34 #define SUN6I_AHB1_MUX_PARENT_PLL6 3
35 #define SUN6I_AHB1_MUX_SHIFT 12
36 /* un-shifted mask is what mux_clk expects */
37 #define SUN6I_AHB1_MUX_MASK 0x3
38 #define SUN6I_AHB1_MUX_GET_PARENT(reg) ((reg >> SUN6I_AHB1_MUX_SHIFT) & \
41 #define SUN6I_AHB1_DIV_SHIFT 4
42 #define SUN6I_AHB1_DIV_MASK (0x3 << SUN6I_AHB1_DIV_SHIFT)
43 #define SUN6I_AHB1_DIV_GET(reg) ((reg & SUN6I_AHB1_DIV_MASK) >> \
45 #define SUN6I_AHB1_DIV_SET(reg, div) ((reg & ~SUN6I_AHB1_DIV_MASK) | \
46 (div << SUN6I_AHB1_DIV_SHIFT))
47 #define SUN6I_AHB1_PLL6_DIV_SHIFT 6
48 #define SUN6I_AHB1_PLL6_DIV_MASK (0x3 << SUN6I_AHB1_PLL6_DIV_SHIFT)
49 #define SUN6I_AHB1_PLL6_DIV_GET(reg) ((reg & SUN6I_AHB1_PLL6_DIV_MASK) >> \
50 SUN6I_AHB1_PLL6_DIV_SHIFT)
51 #define SUN6I_AHB1_PLL6_DIV_SET(reg, div) ((reg & ~SUN6I_AHB1_PLL6_DIV_MASK) | \
52 (div << SUN6I_AHB1_PLL6_DIV_SHIFT))
54 struct sun6i_ahb1_clk
{
59 #define to_sun6i_ahb1_clk(_hw) container_of(_hw, struct sun6i_ahb1_clk, hw)
61 static unsigned long sun6i_ahb1_clk_recalc_rate(struct clk_hw
*hw
,
62 unsigned long parent_rate
)
64 struct sun6i_ahb1_clk
*ahb1
= to_sun6i_ahb1_clk(hw
);
68 /* Fetch the register value */
69 reg
= readl(ahb1
->reg
);
71 /* apply pre-divider first if parent is pll6 */
72 if (SUN6I_AHB1_MUX_GET_PARENT(reg
) == SUN6I_AHB1_MUX_PARENT_PLL6
)
73 parent_rate
/= SUN6I_AHB1_PLL6_DIV_GET(reg
) + 1;
76 rate
= parent_rate
>> SUN6I_AHB1_DIV_GET(reg
);
81 static long sun6i_ahb1_clk_round(unsigned long rate
, u8
*divp
, u8
*pre_divp
,
82 u8 parent
, unsigned long parent_rate
)
84 u8 div
, calcp
, calcm
= 1;
87 * clock can only divide, so we will never be able to achieve
88 * frequencies higher than the parent frequency
90 if (parent_rate
&& rate
> parent_rate
)
93 div
= DIV_ROUND_UP(parent_rate
, rate
);
95 /* calculate pre-divider if parent is pll6 */
96 if (parent
== SUN6I_AHB1_MUX_PARENT_PLL6
) {
101 else if (div
/ 4 < 4)
106 calcm
= DIV_ROUND_UP(div
, 1 << calcp
);
108 calcp
= __roundup_pow_of_two(div
);
109 calcp
= calcp
> 3 ? 3 : calcp
;
112 /* we were asked to pass back divider values */
115 *pre_divp
= calcm
- 1;
118 return (parent_rate
/ calcm
) >> calcp
;
121 static int sun6i_ahb1_clk_determine_rate(struct clk_hw
*hw
,
122 struct clk_rate_request
*req
)
124 struct clk
*clk
= hw
->clk
, *parent
, *best_parent
= NULL
;
126 unsigned long parent_rate
, best
= 0, child_rate
, best_child_rate
= 0;
128 /* find the parent that can help provide the fastest rate <= rate */
129 num_parents
= __clk_get_num_parents(clk
);
130 for (i
= 0; i
< num_parents
; i
++) {
131 parent
= clk_get_parent_by_index(clk
, i
);
134 if (__clk_get_flags(clk
) & CLK_SET_RATE_PARENT
)
135 parent_rate
= __clk_round_rate(parent
, req
->rate
);
137 parent_rate
= __clk_get_rate(parent
);
139 child_rate
= sun6i_ahb1_clk_round(req
->rate
, NULL
, NULL
, i
,
142 if (child_rate
<= req
->rate
&& child_rate
> best_child_rate
) {
143 best_parent
= parent
;
145 best_child_rate
= child_rate
;
152 req
->best_parent_hw
= __clk_get_hw(best_parent
);
153 req
->best_parent_rate
= best
;
154 req
->rate
= best_child_rate
;
159 static int sun6i_ahb1_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
160 unsigned long parent_rate
)
162 struct sun6i_ahb1_clk
*ahb1
= to_sun6i_ahb1_clk(hw
);
164 u8 div
, pre_div
, parent
;
167 spin_lock_irqsave(&clk_lock
, flags
);
169 reg
= readl(ahb1
->reg
);
171 /* need to know which parent is used to apply pre-divider */
172 parent
= SUN6I_AHB1_MUX_GET_PARENT(reg
);
173 sun6i_ahb1_clk_round(rate
, &div
, &pre_div
, parent
, parent_rate
);
175 reg
= SUN6I_AHB1_DIV_SET(reg
, div
);
176 reg
= SUN6I_AHB1_PLL6_DIV_SET(reg
, pre_div
);
177 writel(reg
, ahb1
->reg
);
179 spin_unlock_irqrestore(&clk_lock
, flags
);
184 static const struct clk_ops sun6i_ahb1_clk_ops
= {
185 .determine_rate
= sun6i_ahb1_clk_determine_rate
,
186 .recalc_rate
= sun6i_ahb1_clk_recalc_rate
,
187 .set_rate
= sun6i_ahb1_clk_set_rate
,
190 static void __init
sun6i_ahb1_clk_setup(struct device_node
*node
)
193 struct sun6i_ahb1_clk
*ahb1
;
195 const char *clk_name
= node
->name
;
196 const char *parents
[SUN6I_AHB1_MAX_PARENTS
];
200 reg
= of_io_request_and_map(node
, 0, of_node_full_name(node
));
204 /* we have a mux, we will have >1 parents */
205 i
= of_clk_parent_fill(node
, parents
, SUN6I_AHB1_MAX_PARENTS
);
206 of_property_read_string(node
, "clock-output-names", &clk_name
);
208 ahb1
= kzalloc(sizeof(struct sun6i_ahb1_clk
), GFP_KERNEL
);
212 mux
= kzalloc(sizeof(struct clk_mux
), GFP_KERNEL
);
218 /* set up clock properties */
220 mux
->shift
= SUN6I_AHB1_MUX_SHIFT
;
221 mux
->mask
= SUN6I_AHB1_MUX_MASK
;
222 mux
->lock
= &clk_lock
;
225 clk
= clk_register_composite(NULL
, clk_name
, parents
, i
,
226 &mux
->hw
, &clk_mux_ops
,
227 &ahb1
->hw
, &sun6i_ahb1_clk_ops
,
231 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
232 clk_register_clkdev(clk
, clk_name
, NULL
);
235 CLK_OF_DECLARE(sun6i_a31_ahb1
, "allwinner,sun6i-a31-ahb1-clk", sun6i_ahb1_clk_setup
);
237 /* Maximum number of parents our clocks have */
238 #define SUNXI_MAX_PARENTS 5
241 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
242 * PLL1 rate is calculated as follows
243 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
244 * parent_rate is always 24Mhz
247 static void sun4i_get_pll1_factors(u32
*freq
, u32 parent_rate
,
248 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
252 /* Normalize value to a 6M multiple */
253 div
= *freq
/ 6000000;
254 *freq
= 6000000 * div
;
256 /* we were called to round the frequency, we can now return */
260 /* m is always zero for pll1 */
263 /* k is 1 only on these cases */
264 if (*freq
>= 768000000 || *freq
== 42000000 || *freq
== 54000000)
269 /* p will be 3 for divs under 10 */
273 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
274 else if (div
< 20 || (div
< 32 && (div
& 1)))
277 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
278 * of divs between 40-62 */
279 else if (div
< 40 || (div
< 64 && (div
& 2)))
282 /* any other entries have p = 0 */
286 /* calculate a suitable n based on k and p */
293 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
294 * PLL1 rate is calculated as follows
295 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
296 * parent_rate should always be 24MHz
298 static void sun6i_a31_get_pll1_factors(u32
*freq
, u32 parent_rate
,
299 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
302 * We can operate only on MHz, this will make our life easier
305 u32 freq_mhz
= *freq
/ 1000000;
306 u32 parent_freq_mhz
= parent_rate
/ 1000000;
309 * Round down the frequency to the closest multiple of either
312 u32 round_freq_6
= round_down(freq_mhz
, 6);
313 u32 round_freq_16
= round_down(freq_mhz
, 16);
315 if (round_freq_6
> round_freq_16
)
316 freq_mhz
= round_freq_6
;
318 freq_mhz
= round_freq_16
;
320 *freq
= freq_mhz
* 1000000;
323 * If the factors pointer are null, we were just called to
324 * round down the frequency.
330 /* If the frequency is a multiple of 32 MHz, k is always 3 */
331 if (!(freq_mhz
% 32))
333 /* If the frequency is a multiple of 9 MHz, k is always 2 */
334 else if (!(freq_mhz
% 9))
336 /* If the frequency is a multiple of 8 MHz, k is always 1 */
337 else if (!(freq_mhz
% 8))
339 /* Otherwise, we don't use the k factor */
344 * If the frequency is a multiple of 2 but not a multiple of
345 * 3, m is 3. This is the first time we use 6 here, yet we
346 * will use it on several other places.
347 * We use this number because it's the lowest frequency we can
348 * generate (with n = 0, k = 0, m = 3), so every other frequency
349 * somehow relates to this frequency.
351 if ((freq_mhz
% 6) == 2 || (freq_mhz
% 6) == 4)
354 * If the frequency is a multiple of 6MHz, but the factor is
357 else if ((freq_mhz
/ 6) & 1)
359 /* Otherwise, we end up with m = 1 */
363 /* Calculate n thanks to the above factors we already got */
364 *n
= freq_mhz
* (*m
+ 1) / ((*k
+ 1) * parent_freq_mhz
) - 1;
367 * If n end up being outbound, and that we can still decrease
370 if ((*n
+ 1) > 31 && (*m
+ 1) > 1) {
371 *n
= (*n
+ 1) / 2 - 1;
372 *m
= (*m
+ 1) / 2 - 1;
377 * sun8i_a23_get_pll1_factors() - calculates n, k, m, p factors for PLL1
378 * PLL1 rate is calculated as follows
379 * rate = (parent_rate * (n + 1) * (k + 1) >> p) / (m + 1);
380 * parent_rate is always 24Mhz
383 static void sun8i_a23_get_pll1_factors(u32
*freq
, u32 parent_rate
,
384 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
388 /* Normalize value to a 6M multiple */
389 div
= *freq
/ 6000000;
390 *freq
= 6000000 * div
;
392 /* we were called to round the frequency, we can now return */
396 /* m is always zero for pll1 */
399 /* k is 1 only on these cases */
400 if (*freq
>= 768000000 || *freq
== 42000000 || *freq
== 54000000)
405 /* p will be 2 for divs under 20 and odd divs under 32 */
406 if (div
< 20 || (div
< 32 && (div
& 1)))
409 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
410 * of divs between 40-62 */
411 else if (div
< 40 || (div
< 64 && (div
& 2)))
414 /* any other entries have p = 0 */
418 /* calculate a suitable n based on k and p */
425 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
426 * PLL5 rate is calculated as follows
427 * rate = parent_rate * n * (k + 1)
428 * parent_rate is always 24Mhz
431 static void sun4i_get_pll5_factors(u32
*freq
, u32 parent_rate
,
432 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
436 /* Normalize value to a parent_rate multiple (24M) */
437 div
= *freq
/ parent_rate
;
438 *freq
= parent_rate
* div
;
440 /* we were called to round the frequency, we can now return */
446 else if (div
/ 2 < 31)
448 else if (div
/ 3 < 31)
453 *n
= DIV_ROUND_UP(div
, (*k
+1));
457 * sun6i_a31_get_pll6_factors() - calculates n, k factors for A31 PLL6x2
458 * PLL6x2 rate is calculated as follows
459 * rate = parent_rate * (n + 1) * (k + 1)
460 * parent_rate is always 24Mhz
463 static void sun6i_a31_get_pll6_factors(u32
*freq
, u32 parent_rate
,
464 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
468 /* Normalize value to a parent_rate multiple (24M) */
469 div
= *freq
/ parent_rate
;
470 *freq
= parent_rate
* div
;
472 /* we were called to round the frequency, we can now return */
480 *n
= DIV_ROUND_UP(div
, (*k
+1)) - 1;
484 * sun5i_a13_get_ahb_factors() - calculates m, p factors for AHB
485 * AHB rate is calculated as follows
486 * rate = parent_rate >> p
489 static void sun5i_a13_get_ahb_factors(u32
*freq
, u32 parent_rate
,
490 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
495 if (parent_rate
< *freq
)
499 * user manual says valid speed is 8k ~ 276M, but tests show it
500 * can work at speeds up to 300M, just after reparenting to pll6
504 if (*freq
> 300000000)
507 div
= order_base_2(DIV_ROUND_UP(parent_rate
, *freq
));
513 *freq
= parent_rate
>> div
;
515 /* we were called to round the frequency, we can now return */
523 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
524 * APB1 rate is calculated as follows
525 * rate = (parent_rate >> p) / (m + 1);
528 static void sun4i_get_apb1_factors(u32
*freq
, u32 parent_rate
,
529 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
533 if (parent_rate
< *freq
)
536 parent_rate
= DIV_ROUND_UP(parent_rate
, *freq
);
539 if (parent_rate
> 32)
542 if (parent_rate
<= 4)
544 else if (parent_rate
<= 8)
546 else if (parent_rate
<= 16)
551 calcm
= (parent_rate
>> calcp
) - 1;
553 *freq
= (parent_rate
>> calcp
) / (calcm
+ 1);
555 /* we were called to round the frequency, we can now return */
567 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
568 * CLK_OUT rate is calculated as follows
569 * rate = (parent_rate >> p) / (m + 1);
572 static void sun7i_a20_get_out_factors(u32
*freq
, u32 parent_rate
,
573 u8
*n
, u8
*k
, u8
*m
, u8
*p
)
575 u8 div
, calcm
, calcp
;
577 /* These clocks can only divide, so we will never be able to achieve
578 * frequencies higher than the parent frequency */
579 if (*freq
> parent_rate
)
582 div
= DIV_ROUND_UP(parent_rate
, *freq
);
586 else if (div
/ 2 < 32)
588 else if (div
/ 4 < 32)
593 calcm
= DIV_ROUND_UP(div
, 1 << calcp
);
595 *freq
= (parent_rate
>> calcp
) / calcm
;
597 /* we were called to round the frequency, we can now return */
606 * sunxi_factors_clk_setup() - Setup function for factor clocks
609 static struct clk_factors_config sun4i_pll1_config
= {
620 static struct clk_factors_config sun6i_a31_pll1_config
= {
630 static struct clk_factors_config sun8i_a23_pll1_config
= {
642 static struct clk_factors_config sun4i_pll5_config
= {
649 static struct clk_factors_config sun6i_a31_pll6_config
= {
657 static struct clk_factors_config sun5i_a13_ahb_config
= {
662 static struct clk_factors_config sun4i_apb1_config
= {
669 /* user manual says "n" but it's really "p" */
670 static struct clk_factors_config sun7i_a20_out_config
= {
677 static const struct factors_data sun4i_pll1_data __initconst
= {
679 .table
= &sun4i_pll1_config
,
680 .getter
= sun4i_get_pll1_factors
,
683 static const struct factors_data sun6i_a31_pll1_data __initconst
= {
685 .table
= &sun6i_a31_pll1_config
,
686 .getter
= sun6i_a31_get_pll1_factors
,
689 static const struct factors_data sun8i_a23_pll1_data __initconst
= {
691 .table
= &sun8i_a23_pll1_config
,
692 .getter
= sun8i_a23_get_pll1_factors
,
695 static const struct factors_data sun7i_a20_pll4_data __initconst
= {
697 .table
= &sun4i_pll5_config
,
698 .getter
= sun4i_get_pll5_factors
,
701 static const struct factors_data sun4i_pll5_data __initconst
= {
703 .table
= &sun4i_pll5_config
,
704 .getter
= sun4i_get_pll5_factors
,
708 static const struct factors_data sun4i_pll6_data __initconst
= {
710 .table
= &sun4i_pll5_config
,
711 .getter
= sun4i_get_pll5_factors
,
715 static const struct factors_data sun6i_a31_pll6_data __initconst
= {
717 .table
= &sun6i_a31_pll6_config
,
718 .getter
= sun6i_a31_get_pll6_factors
,
722 static const struct factors_data sun5i_a13_ahb_data __initconst
= {
724 .muxmask
= BIT(1) | BIT(0),
725 .table
= &sun5i_a13_ahb_config
,
726 .getter
= sun5i_a13_get_ahb_factors
,
729 static const struct factors_data sun4i_apb1_data __initconst
= {
731 .muxmask
= BIT(1) | BIT(0),
732 .table
= &sun4i_apb1_config
,
733 .getter
= sun4i_get_apb1_factors
,
736 static const struct factors_data sun7i_a20_out_data __initconst
= {
739 .muxmask
= BIT(1) | BIT(0),
740 .table
= &sun7i_a20_out_config
,
741 .getter
= sun7i_a20_get_out_factors
,
744 static struct clk
* __init
sunxi_factors_clk_setup(struct device_node
*node
,
745 const struct factors_data
*data
)
749 reg
= of_iomap(node
, 0);
751 pr_err("Could not get registers for factors-clk: %s\n",
756 return sunxi_factors_register(node
, data
, &clk_lock
, reg
);
762 * sunxi_mux_clk_setup() - Setup function for muxes
765 #define SUNXI_MUX_GATE_WIDTH 2
771 static const struct mux_data sun4i_cpu_mux_data __initconst
= {
775 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst
= {
779 static void __init
sunxi_mux_clk_setup(struct device_node
*node
,
780 struct mux_data
*data
)
783 const char *clk_name
= node
->name
;
784 const char *parents
[SUNXI_MAX_PARENTS
];
788 reg
= of_iomap(node
, 0);
790 i
= of_clk_parent_fill(node
, parents
, SUNXI_MAX_PARENTS
);
791 of_property_read_string(node
, "clock-output-names", &clk_name
);
793 clk
= clk_register_mux(NULL
, clk_name
, parents
, i
,
794 CLK_SET_RATE_PARENT
, reg
,
795 data
->shift
, SUNXI_MUX_GATE_WIDTH
,
799 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
800 clk_register_clkdev(clk
, clk_name
, NULL
);
807 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
814 const struct clk_div_table
*table
;
817 static const struct div_data sun4i_axi_data __initconst
= {
823 static const struct clk_div_table sun8i_a23_axi_table
[] __initconst
= {
824 { .val
= 0, .div
= 1 },
825 { .val
= 1, .div
= 2 },
826 { .val
= 2, .div
= 3 },
827 { .val
= 3, .div
= 4 },
828 { .val
= 4, .div
= 4 },
829 { .val
= 5, .div
= 4 },
830 { .val
= 6, .div
= 4 },
831 { .val
= 7, .div
= 4 },
835 static const struct div_data sun8i_a23_axi_data __initconst
= {
837 .table
= sun8i_a23_axi_table
,
840 static const struct div_data sun4i_ahb_data __initconst
= {
846 static const struct clk_div_table sun4i_apb0_table
[] __initconst
= {
847 { .val
= 0, .div
= 2 },
848 { .val
= 1, .div
= 2 },
849 { .val
= 2, .div
= 4 },
850 { .val
= 3, .div
= 8 },
854 static const struct div_data sun4i_apb0_data __initconst
= {
858 .table
= sun4i_apb0_table
,
861 static void __init
sunxi_divider_clk_setup(struct device_node
*node
,
862 struct div_data
*data
)
865 const char *clk_name
= node
->name
;
866 const char *clk_parent
;
869 reg
= of_iomap(node
, 0);
871 clk_parent
= of_clk_get_parent_name(node
, 0);
873 of_property_read_string(node
, "clock-output-names", &clk_name
);
875 clk
= clk_register_divider_table(NULL
, clk_name
, clk_parent
, 0,
876 reg
, data
->shift
, data
->width
,
877 data
->pow
? CLK_DIVIDER_POWER_OF_TWO
: 0,
878 data
->table
, &clk_lock
);
880 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
881 clk_register_clkdev(clk
, clk_name
, NULL
);
888 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
891 #define SUNXI_GATES_MAX_SIZE 64
894 DECLARE_BITMAP(mask
, SUNXI_GATES_MAX_SIZE
);
897 static const struct gates_data sun4i_axi_gates_data __initconst
= {
901 static const struct gates_data sun4i_ahb_gates_data __initconst
= {
902 .mask
= {0x7F77FFF, 0x14FB3F},
905 static const struct gates_data sun5i_a10s_ahb_gates_data __initconst
= {
906 .mask
= {0x147667e7, 0x185915},
909 static const struct gates_data sun5i_a13_ahb_gates_data __initconst
= {
910 .mask
= {0x107067e7, 0x185111},
913 static const struct gates_data sun6i_a31_ahb1_gates_data __initconst
= {
914 .mask
= {0xEDFE7F62, 0x794F931},
917 static const struct gates_data sun7i_a20_ahb_gates_data __initconst
= {
918 .mask
= { 0x12f77fff, 0x16ff3f },
921 static const struct gates_data sun8i_a23_ahb1_gates_data __initconst
= {
922 .mask
= {0x25386742, 0x2505111},
925 static const struct gates_data sun9i_a80_ahb0_gates_data __initconst
= {
929 static const struct gates_data sun9i_a80_ahb1_gates_data __initconst
= {
933 static const struct gates_data sun9i_a80_ahb2_gates_data __initconst
= {
937 static const struct gates_data sun4i_apb0_gates_data __initconst
= {
941 static const struct gates_data sun5i_a10s_apb0_gates_data __initconst
= {
945 static const struct gates_data sun5i_a13_apb0_gates_data __initconst
= {
949 static const struct gates_data sun7i_a20_apb0_gates_data __initconst
= {
953 static const struct gates_data sun9i_a80_apb0_gates_data __initconst
= {
957 static const struct gates_data sun4i_apb1_gates_data __initconst
= {
961 static const struct gates_data sun5i_a10s_apb1_gates_data __initconst
= {
965 static const struct gates_data sun5i_a13_apb1_gates_data __initconst
= {
969 static const struct gates_data sun6i_a31_apb1_gates_data __initconst
= {
973 static const struct gates_data sun8i_a23_apb1_gates_data __initconst
= {
977 static const struct gates_data sun6i_a31_apb2_gates_data __initconst
= {
981 static const struct gates_data sun7i_a20_apb1_gates_data __initconst
= {
982 .mask
= { 0xff80ff },
985 static const struct gates_data sun9i_a80_apb1_gates_data __initconst
= {
989 static const struct gates_data sun8i_a23_apb2_gates_data __initconst
= {
993 static void __init
sunxi_gates_clk_setup(struct device_node
*node
,
994 struct gates_data
*data
)
996 struct clk_onecell_data
*clk_data
;
997 const char *clk_parent
;
998 const char *clk_name
;
1004 reg
= of_iomap(node
, 0);
1006 clk_parent
= of_clk_get_parent_name(node
, 0);
1008 /* Worst-case size approximation and memory allocation */
1009 qty
= find_last_bit(data
->mask
, SUNXI_GATES_MAX_SIZE
);
1010 clk_data
= kmalloc(sizeof(struct clk_onecell_data
), GFP_KERNEL
);
1013 clk_data
->clks
= kzalloc((qty
+1) * sizeof(struct clk
*), GFP_KERNEL
);
1014 if (!clk_data
->clks
) {
1019 for_each_set_bit(i
, data
->mask
, SUNXI_GATES_MAX_SIZE
) {
1020 of_property_read_string_index(node
, "clock-output-names",
1023 clk_data
->clks
[i
] = clk_register_gate(NULL
, clk_name
,
1025 reg
+ 4 * (i
/32), i
% 32,
1027 WARN_ON(IS_ERR(clk_data
->clks
[i
]));
1028 clk_register_clkdev(clk_data
->clks
[i
], clk_name
, NULL
);
1033 /* Adjust to the real max */
1034 clk_data
->clk_num
= i
;
1036 of_clk_add_provider(node
, of_clk_src_onecell_get
, clk_data
);
1042 * sunxi_divs_clk_setup() helper data
1045 #define SUNXI_DIVS_MAX_QTY 4
1046 #define SUNXI_DIVISOR_WIDTH 2
1049 const struct factors_data
*factors
; /* data for the factor clock */
1050 int ndivs
; /* number of outputs */
1052 * List of outputs. Refer to the diagram for sunxi_divs_clk_setup():
1053 * self or base factor clock refers to the output from the pll
1054 * itself. The remaining refer to fixed or configurable divider
1058 u8 self
; /* is it the base factor clock? (only one) */
1059 u8 fixed
; /* is it a fixed divisor? if not... */
1060 struct clk_div_table
*table
; /* is it a table based divisor? */
1061 u8 shift
; /* otherwise it's a normal divisor with this shift */
1062 u8 pow
; /* is it power-of-two based? */
1063 u8 gate
; /* is it independently gateable? */
1064 } div
[SUNXI_DIVS_MAX_QTY
];
1067 static struct clk_div_table pll6_sata_tbl
[] = {
1068 { .val
= 0, .div
= 6, },
1069 { .val
= 1, .div
= 12, },
1070 { .val
= 2, .div
= 18, },
1071 { .val
= 3, .div
= 24, },
1075 static const struct divs_data pll5_divs_data __initconst
= {
1076 .factors
= &sun4i_pll5_data
,
1079 { .shift
= 0, .pow
= 0, }, /* M, DDR */
1080 { .shift
= 16, .pow
= 1, }, /* P, other */
1081 /* No output for the base factor clock */
1085 static const struct divs_data pll6_divs_data __initconst
= {
1086 .factors
= &sun4i_pll6_data
,
1089 { .shift
= 0, .table
= pll6_sata_tbl
, .gate
= 14 }, /* M, SATA */
1090 { .fixed
= 2 }, /* P, other */
1091 { .self
= 1 }, /* base factor clock, 2x */
1092 { .fixed
= 4 }, /* pll6 / 4, used as ahb input */
1096 static const struct divs_data sun6i_a31_pll6_divs_data __initconst
= {
1097 .factors
= &sun6i_a31_pll6_data
,
1100 { .fixed
= 2 }, /* normal output */
1101 { .self
= 1 }, /* base factor clock, 2x */
1106 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
1108 * These clocks look something like this
1109 * ________________________
1110 * | ___divisor 1---|----> to consumer
1111 * parent >--| pll___/___divisor 2---|----> to consumer
1112 * | \_______________|____> to consumer
1113 * |________________________|
1116 static void __init
sunxi_divs_clk_setup(struct device_node
*node
,
1117 struct divs_data
*data
)
1119 struct clk_onecell_data
*clk_data
;
1121 const char *clk_name
;
1122 struct clk
**clks
, *pclk
;
1123 struct clk_hw
*gate_hw
, *rate_hw
;
1124 const struct clk_ops
*rate_ops
;
1125 struct clk_gate
*gate
= NULL
;
1126 struct clk_fixed_factor
*fix_factor
;
1127 struct clk_divider
*divider
;
1129 int ndivs
= SUNXI_DIVS_MAX_QTY
, i
= 0;
1130 int flags
, clkflags
;
1132 /* if number of children known, use it */
1134 ndivs
= data
->ndivs
;
1136 /* Set up factor clock that we will be dividing */
1137 pclk
= sunxi_factors_clk_setup(node
, data
->factors
);
1138 parent
= __clk_get_name(pclk
);
1140 reg
= of_iomap(node
, 0);
1142 clk_data
= kmalloc(sizeof(struct clk_onecell_data
), GFP_KERNEL
);
1146 clks
= kcalloc(ndivs
, sizeof(*clks
), GFP_KERNEL
);
1150 clk_data
->clks
= clks
;
1152 /* It's not a good idea to have automatic reparenting changing
1154 clkflags
= !strcmp("pll5", parent
) ? 0 : CLK_SET_RATE_PARENT
;
1156 for (i
= 0; i
< ndivs
; i
++) {
1157 if (of_property_read_string_index(node
, "clock-output-names",
1161 /* If this is the base factor clock, only update clks */
1162 if (data
->div
[i
].self
) {
1163 clk_data
->clks
[i
] = pclk
;
1171 /* If this leaf clock can be gated, create a gate */
1172 if (data
->div
[i
].gate
) {
1173 gate
= kzalloc(sizeof(*gate
), GFP_KERNEL
);
1178 gate
->bit_idx
= data
->div
[i
].gate
;
1179 gate
->lock
= &clk_lock
;
1181 gate_hw
= &gate
->hw
;
1184 /* Leaves can be fixed or configurable divisors */
1185 if (data
->div
[i
].fixed
) {
1186 fix_factor
= kzalloc(sizeof(*fix_factor
), GFP_KERNEL
);
1190 fix_factor
->mult
= 1;
1191 fix_factor
->div
= data
->div
[i
].fixed
;
1193 rate_hw
= &fix_factor
->hw
;
1194 rate_ops
= &clk_fixed_factor_ops
;
1196 divider
= kzalloc(sizeof(*divider
), GFP_KERNEL
);
1200 flags
= data
->div
[i
].pow
? CLK_DIVIDER_POWER_OF_TWO
: 0;
1203 divider
->shift
= data
->div
[i
].shift
;
1204 divider
->width
= SUNXI_DIVISOR_WIDTH
;
1205 divider
->flags
= flags
;
1206 divider
->lock
= &clk_lock
;
1207 divider
->table
= data
->div
[i
].table
;
1209 rate_hw
= ÷r
->hw
;
1210 rate_ops
= &clk_divider_ops
;
1213 /* Wrap the (potential) gate and the divisor on a composite
1214 * clock to unify them */
1215 clks
[i
] = clk_register_composite(NULL
, clk_name
, &parent
, 1,
1218 gate_hw
, &clk_gate_ops
,
1221 WARN_ON(IS_ERR(clk_data
->clks
[i
]));
1222 clk_register_clkdev(clks
[i
], clk_name
, NULL
);
1225 /* Adjust to the real max */
1226 clk_data
->clk_num
= i
;
1228 of_clk_add_provider(node
, of_clk_src_onecell_get
, clk_data
);
1242 /* Matches for factors clocks */
1243 static const struct of_device_id clk_factors_match
[] __initconst
= {
1244 {.compatible
= "allwinner,sun4i-a10-pll1-clk", .data
= &sun4i_pll1_data
,},
1245 {.compatible
= "allwinner,sun6i-a31-pll1-clk", .data
= &sun6i_a31_pll1_data
,},
1246 {.compatible
= "allwinner,sun8i-a23-pll1-clk", .data
= &sun8i_a23_pll1_data
,},
1247 {.compatible
= "allwinner,sun7i-a20-pll4-clk", .data
= &sun7i_a20_pll4_data
,},
1248 {.compatible
= "allwinner,sun5i-a13-ahb-clk", .data
= &sun5i_a13_ahb_data
,},
1249 {.compatible
= "allwinner,sun4i-a10-apb1-clk", .data
= &sun4i_apb1_data
,},
1250 {.compatible
= "allwinner,sun7i-a20-out-clk", .data
= &sun7i_a20_out_data
,},
1254 /* Matches for divider clocks */
1255 static const struct of_device_id clk_div_match
[] __initconst
= {
1256 {.compatible
= "allwinner,sun4i-a10-axi-clk", .data
= &sun4i_axi_data
,},
1257 {.compatible
= "allwinner,sun8i-a23-axi-clk", .data
= &sun8i_a23_axi_data
,},
1258 {.compatible
= "allwinner,sun4i-a10-ahb-clk", .data
= &sun4i_ahb_data
,},
1259 {.compatible
= "allwinner,sun4i-a10-apb0-clk", .data
= &sun4i_apb0_data
,},
1263 /* Matches for divided outputs */
1264 static const struct of_device_id clk_divs_match
[] __initconst
= {
1265 {.compatible
= "allwinner,sun4i-a10-pll5-clk", .data
= &pll5_divs_data
,},
1266 {.compatible
= "allwinner,sun4i-a10-pll6-clk", .data
= &pll6_divs_data
,},
1267 {.compatible
= "allwinner,sun6i-a31-pll6-clk", .data
= &sun6i_a31_pll6_divs_data
,},
1271 /* Matches for mux clocks */
1272 static const struct of_device_id clk_mux_match
[] __initconst
= {
1273 {.compatible
= "allwinner,sun4i-a10-cpu-clk", .data
= &sun4i_cpu_mux_data
,},
1274 {.compatible
= "allwinner,sun6i-a31-ahb1-mux-clk", .data
= &sun6i_a31_ahb1_mux_data
,},
1278 /* Matches for gate clocks */
1279 static const struct of_device_id clk_gates_match
[] __initconst
= {
1280 {.compatible
= "allwinner,sun4i-a10-axi-gates-clk", .data
= &sun4i_axi_gates_data
,},
1281 {.compatible
= "allwinner,sun4i-a10-ahb-gates-clk", .data
= &sun4i_ahb_gates_data
,},
1282 {.compatible
= "allwinner,sun5i-a10s-ahb-gates-clk", .data
= &sun5i_a10s_ahb_gates_data
,},
1283 {.compatible
= "allwinner,sun5i-a13-ahb-gates-clk", .data
= &sun5i_a13_ahb_gates_data
,},
1284 {.compatible
= "allwinner,sun6i-a31-ahb1-gates-clk", .data
= &sun6i_a31_ahb1_gates_data
,},
1285 {.compatible
= "allwinner,sun7i-a20-ahb-gates-clk", .data
= &sun7i_a20_ahb_gates_data
,},
1286 {.compatible
= "allwinner,sun8i-a23-ahb1-gates-clk", .data
= &sun8i_a23_ahb1_gates_data
,},
1287 {.compatible
= "allwinner,sun9i-a80-ahb0-gates-clk", .data
= &sun9i_a80_ahb0_gates_data
,},
1288 {.compatible
= "allwinner,sun9i-a80-ahb1-gates-clk", .data
= &sun9i_a80_ahb1_gates_data
,},
1289 {.compatible
= "allwinner,sun9i-a80-ahb2-gates-clk", .data
= &sun9i_a80_ahb2_gates_data
,},
1290 {.compatible
= "allwinner,sun4i-a10-apb0-gates-clk", .data
= &sun4i_apb0_gates_data
,},
1291 {.compatible
= "allwinner,sun5i-a10s-apb0-gates-clk", .data
= &sun5i_a10s_apb0_gates_data
,},
1292 {.compatible
= "allwinner,sun5i-a13-apb0-gates-clk", .data
= &sun5i_a13_apb0_gates_data
,},
1293 {.compatible
= "allwinner,sun7i-a20-apb0-gates-clk", .data
= &sun7i_a20_apb0_gates_data
,},
1294 {.compatible
= "allwinner,sun9i-a80-apb0-gates-clk", .data
= &sun9i_a80_apb0_gates_data
,},
1295 {.compatible
= "allwinner,sun4i-a10-apb1-gates-clk", .data
= &sun4i_apb1_gates_data
,},
1296 {.compatible
= "allwinner,sun5i-a10s-apb1-gates-clk", .data
= &sun5i_a10s_apb1_gates_data
,},
1297 {.compatible
= "allwinner,sun5i-a13-apb1-gates-clk", .data
= &sun5i_a13_apb1_gates_data
,},
1298 {.compatible
= "allwinner,sun6i-a31-apb1-gates-clk", .data
= &sun6i_a31_apb1_gates_data
,},
1299 {.compatible
= "allwinner,sun7i-a20-apb1-gates-clk", .data
= &sun7i_a20_apb1_gates_data
,},
1300 {.compatible
= "allwinner,sun8i-a23-apb1-gates-clk", .data
= &sun8i_a23_apb1_gates_data
,},
1301 {.compatible
= "allwinner,sun9i-a80-apb1-gates-clk", .data
= &sun9i_a80_apb1_gates_data
,},
1302 {.compatible
= "allwinner,sun6i-a31-apb2-gates-clk", .data
= &sun6i_a31_apb2_gates_data
,},
1303 {.compatible
= "allwinner,sun8i-a23-apb2-gates-clk", .data
= &sun8i_a23_apb2_gates_data
,},
1307 static void __init
of_sunxi_table_clock_setup(const struct of_device_id
*clk_match
,
1310 struct device_node
*np
;
1311 const struct div_data
*data
;
1312 const struct of_device_id
*match
;
1313 void (*setup_function
)(struct device_node
*, const void *) = function
;
1315 for_each_matching_node_and_match(np
, clk_match
, &match
) {
1317 setup_function(np
, data
);
1321 static void __init
sunxi_init_clocks(const char *clocks
[], int nclocks
)
1325 /* Register divided output clocks */
1326 of_sunxi_table_clock_setup(clk_divs_match
, sunxi_divs_clk_setup
);
1328 /* Register factor clocks */
1329 of_sunxi_table_clock_setup(clk_factors_match
, sunxi_factors_clk_setup
);
1331 /* Register divider clocks */
1332 of_sunxi_table_clock_setup(clk_div_match
, sunxi_divider_clk_setup
);
1334 /* Register mux clocks */
1335 of_sunxi_table_clock_setup(clk_mux_match
, sunxi_mux_clk_setup
);
1337 /* Register gate clocks */
1338 of_sunxi_table_clock_setup(clk_gates_match
, sunxi_gates_clk_setup
);
1340 /* Protect the clocks that needs to stay on */
1341 for (i
= 0; i
< nclocks
; i
++) {
1342 struct clk
*clk
= clk_get(NULL
, clocks
[i
]);
1345 clk_prepare_enable(clk
);
1349 static const char *sun4i_a10_critical_clocks
[] __initdata
= {
1354 static void __init
sun4i_a10_init_clocks(struct device_node
*node
)
1356 sunxi_init_clocks(sun4i_a10_critical_clocks
,
1357 ARRAY_SIZE(sun4i_a10_critical_clocks
));
1359 CLK_OF_DECLARE(sun4i_a10_clk_init
, "allwinner,sun4i-a10", sun4i_a10_init_clocks
);
1361 static const char *sun5i_critical_clocks
[] __initdata
= {
1367 static void __init
sun5i_init_clocks(struct device_node
*node
)
1369 sunxi_init_clocks(sun5i_critical_clocks
,
1370 ARRAY_SIZE(sun5i_critical_clocks
));
1372 CLK_OF_DECLARE(sun5i_a10s_clk_init
, "allwinner,sun5i-a10s", sun5i_init_clocks
);
1373 CLK_OF_DECLARE(sun5i_a13_clk_init
, "allwinner,sun5i-a13", sun5i_init_clocks
);
1374 CLK_OF_DECLARE(sun7i_a20_clk_init
, "allwinner,sun7i-a20", sun5i_init_clocks
);
1376 static const char *sun6i_critical_clocks
[] __initdata
= {
1380 static void __init
sun6i_init_clocks(struct device_node
*node
)
1382 sunxi_init_clocks(sun6i_critical_clocks
,
1383 ARRAY_SIZE(sun6i_critical_clocks
));
1385 CLK_OF_DECLARE(sun6i_a31_clk_init
, "allwinner,sun6i-a31", sun6i_init_clocks
);
1386 CLK_OF_DECLARE(sun6i_a31s_clk_init
, "allwinner,sun6i-a31s", sun6i_init_clocks
);
1387 CLK_OF_DECLARE(sun8i_a23_clk_init
, "allwinner,sun8i-a23", sun6i_init_clocks
);
1389 static void __init
sun9i_init_clocks(struct device_node
*node
)
1391 sunxi_init_clocks(NULL
, 0);
1393 CLK_OF_DECLARE(sun9i_a80_clk_init
, "allwinner,sun9i-a80", sun9i_init_clocks
);