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>
19 #include <linux/clk/sunxi.h>
21 #include <linux/of_address.h>
23 #include "clk-factors.h"
25 static DEFINE_SPINLOCK(clk_lock);
28 * sun4i_osc_clk_setup() - Setup function for gatable oscillator
31 #define SUNXI_OSC24M_GATE 0
33 static void __init sun4i_osc_clk_setup(struct device_node *node)
36 struct clk_fixed_rate *fixed;
37 struct clk_gate *gate;
38 const char *clk_name = node->name;
41 if (of_property_read_u32(node, "clock-frequency", &rate))
44 /* allocate fixed-rate and gate clock structs */
45 fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
48 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
52 /* set up gate and fixed rate properties */
53 gate->reg = of_iomap(node, 0);
54 gate->bit_idx = SUNXI_OSC24M_GATE;
55 gate->lock = &clk_lock;
56 fixed->fixed_rate = rate;
58 clk = clk_register_composite(NULL, clk_name,
61 &fixed->hw, &clk_fixed_rate_ops,
62 &gate->hw, &clk_gate_ops,
68 of_clk_add_provider(node, of_clk_src_simple_get, clk);
69 clk_register_clkdev(clk, clk_name, NULL);
78 CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
83 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
84 * PLL1 rate is calculated as follows
85 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
86 * parent_rate is always 24Mhz
89 static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
90 u8 *n, u8 *k, u8 *m, u8 *p)
94 /* Normalize value to a 6M multiple */
95 div = *freq / 6000000;
96 *freq = 6000000 * div;
98 /* we were called to round the frequency, we can now return */
102 /* m is always zero for pll1 */
105 /* k is 1 only on these cases */
106 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
111 /* p will be 3 for divs under 10 */
115 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
116 else if (div < 20 || (div < 32 && (div & 1)))
119 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
120 * of divs between 40-62 */
121 else if (div < 40 || (div < 64 && (div & 2)))
124 /* any other entries have p = 0 */
128 /* calculate a suitable n based on k and p */
135 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
136 * PLL1 rate is calculated as follows
137 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
138 * parent_rate should always be 24MHz
140 static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
141 u8 *n, u8 *k, u8 *m, u8 *p)
144 * We can operate only on MHz, this will make our life easier
147 u32 freq_mhz = *freq / 1000000;
148 u32 parent_freq_mhz = parent_rate / 1000000;
151 * Round down the frequency to the closest multiple of either
154 u32 round_freq_6 = round_down(freq_mhz, 6);
155 u32 round_freq_16 = round_down(freq_mhz, 16);
157 if (round_freq_6 > round_freq_16)
158 freq_mhz = round_freq_6;
160 freq_mhz = round_freq_16;
162 *freq = freq_mhz * 1000000;
165 * If the factors pointer are null, we were just called to
166 * round down the frequency.
172 /* If the frequency is a multiple of 32 MHz, k is always 3 */
173 if (!(freq_mhz % 32))
175 /* If the frequency is a multiple of 9 MHz, k is always 2 */
176 else if (!(freq_mhz % 9))
178 /* If the frequency is a multiple of 8 MHz, k is always 1 */
179 else if (!(freq_mhz % 8))
181 /* Otherwise, we don't use the k factor */
186 * If the frequency is a multiple of 2 but not a multiple of
187 * 3, m is 3. This is the first time we use 6 here, yet we
188 * will use it on several other places.
189 * We use this number because it's the lowest frequency we can
190 * generate (with n = 0, k = 0, m = 3), so every other frequency
191 * somehow relates to this frequency.
193 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
196 * If the frequency is a multiple of 6MHz, but the factor is
199 else if ((freq_mhz / 6) & 1)
201 /* Otherwise, we end up with m = 1 */
205 /* Calculate n thanks to the above factors we already got */
206 *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
209 * If n end up being outbound, and that we can still decrease
212 if ((*n + 1) > 31 && (*m + 1) > 1) {
213 *n = (*n + 1) / 2 - 1;
214 *m = (*m + 1) / 2 - 1;
219 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
220 * APB1 rate is calculated as follows
221 * rate = (parent_rate >> p) / (m + 1);
224 static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
225 u8 *n, u8 *k, u8 *m, u8 *p)
229 if (parent_rate < *freq)
232 parent_rate = (parent_rate + (*freq - 1)) / *freq;
235 if (parent_rate > 32)
238 if (parent_rate <= 4)
240 else if (parent_rate <= 8)
242 else if (parent_rate <= 16)
247 calcm = (parent_rate >> calcp) - 1;
249 *freq = (parent_rate >> calcp) / (calcm + 1);
251 /* we were called to round the frequency, we can now return */
262 * sunxi_factors_clk_setup() - Setup function for factor clocks
265 struct factors_data {
266 struct clk_factors_config *table;
267 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
270 static struct clk_factors_config sun4i_pll1_config = {
281 static struct clk_factors_config sun6i_a31_pll1_config = {
290 static struct clk_factors_config sun4i_apb1_config = {
297 static const struct factors_data sun4i_pll1_data __initconst = {
298 .table = &sun4i_pll1_config,
299 .getter = sun4i_get_pll1_factors,
302 static const struct factors_data sun6i_a31_pll1_data __initconst = {
303 .table = &sun6i_a31_pll1_config,
304 .getter = sun6i_a31_get_pll1_factors,
307 static const struct factors_data sun4i_apb1_data __initconst = {
308 .table = &sun4i_apb1_config,
309 .getter = sun4i_get_apb1_factors,
312 static void __init sunxi_factors_clk_setup(struct device_node *node,
313 struct factors_data *data)
316 const char *clk_name = node->name;
320 reg = of_iomap(node, 0);
322 parent = of_clk_get_parent_name(node, 0);
324 clk = clk_register_factors(NULL, clk_name, parent, 0, reg,
325 data->table, data->getter, &clk_lock);
328 of_clk_add_provider(node, of_clk_src_simple_get, clk);
329 clk_register_clkdev(clk, clk_name, NULL);
336 * sunxi_mux_clk_setup() - Setup function for muxes
339 #define SUNXI_MUX_GATE_WIDTH 2
345 static const struct mux_data sun4i_cpu_mux_data __initconst = {
349 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
353 static const struct mux_data sun4i_apb1_mux_data __initconst = {
357 static void __init sunxi_mux_clk_setup(struct device_node *node,
358 struct mux_data *data)
361 const char *clk_name = node->name;
362 const char *parents[5];
366 reg = of_iomap(node, 0);
368 while (i < 5 && (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
371 clk = clk_register_mux(NULL, clk_name, parents, i,
372 CLK_SET_RATE_NO_REPARENT, reg,
373 data->shift, SUNXI_MUX_GATE_WIDTH,
377 of_clk_add_provider(node, of_clk_src_simple_get, clk);
378 clk_register_clkdev(clk, clk_name, NULL);
385 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
394 static const struct div_data sun4i_axi_data __initconst = {
400 static const struct div_data sun4i_ahb_data __initconst = {
406 static const struct div_data sun4i_apb0_data __initconst = {
412 static const struct div_data sun6i_a31_apb2_div_data __initconst = {
418 static void __init sunxi_divider_clk_setup(struct device_node *node,
419 struct div_data *data)
422 const char *clk_name = node->name;
423 const char *clk_parent;
426 reg = of_iomap(node, 0);
428 clk_parent = of_clk_get_parent_name(node, 0);
430 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
431 reg, data->shift, data->width,
432 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
435 of_clk_add_provider(node, of_clk_src_simple_get, clk);
436 clk_register_clkdev(clk, clk_name, NULL);
443 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
446 #define SUNXI_GATES_MAX_SIZE 64
449 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
452 static const struct gates_data sun4i_axi_gates_data __initconst = {
456 static const struct gates_data sun4i_ahb_gates_data __initconst = {
457 .mask = {0x7F77FFF, 0x14FB3F},
460 static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
461 .mask = {0x147667e7, 0x185915},
464 static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
465 .mask = {0x107067e7, 0x185111},
468 static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
469 .mask = {0xEDFE7F62, 0x794F931},
472 static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
473 .mask = { 0x12f77fff, 0x16ff3f },
476 static const struct gates_data sun4i_apb0_gates_data __initconst = {
480 static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
484 static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
488 static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
492 static const struct gates_data sun4i_apb1_gates_data __initconst = {
496 static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
500 static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
504 static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
508 static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
512 static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
513 .mask = { 0xff80ff },
516 static void __init sunxi_gates_clk_setup(struct device_node *node,
517 struct gates_data *data)
519 struct clk_onecell_data *clk_data;
520 const char *clk_parent;
521 const char *clk_name;
528 reg = of_iomap(node, 0);
530 clk_parent = of_clk_get_parent_name(node, 0);
532 /* Worst-case size approximation and memory allocation */
533 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
534 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
537 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
538 if (!clk_data->clks) {
543 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
544 of_property_read_string_index(node, "clock-output-names",
547 /* No driver claims this clock, but it should remain gated */
548 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
550 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
552 reg + 4 * (i/32), i % 32,
554 WARN_ON(IS_ERR(clk_data->clks[i]));
559 /* Adjust to the real max */
560 clk_data->clk_num = i;
562 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
565 /* Matches for factors clocks */
566 static const struct of_device_id clk_factors_match[] __initconst = {
567 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
568 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
569 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
573 /* Matches for divider clocks */
574 static const struct of_device_id clk_div_match[] __initconst = {
575 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
576 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
577 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
578 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
582 /* Matches for mux clocks */
583 static const struct of_device_id clk_mux_match[] __initconst = {
584 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
585 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
586 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
590 /* Matches for gate clocks */
591 static const struct of_device_id clk_gates_match[] __initconst = {
592 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
593 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
594 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
595 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
596 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
597 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
598 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
599 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
600 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
601 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
602 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
603 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
604 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
605 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
606 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
607 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
611 static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
614 struct device_node *np;
615 const struct div_data *data;
616 const struct of_device_id *match;
617 void (*setup_function)(struct device_node *, const void *) = function;
619 for_each_matching_node(np, clk_match) {
620 match = of_match_node(clk_match, np);
622 setup_function(np, data);
627 * System clock protection
629 * By enabling these critical clocks, we prevent their accidental gating
632 static void __init sunxi_clock_protect(void)
636 /* memory bus clock - sun5i+ */
637 clk = clk_get(NULL, "mbus");
639 clk_prepare_enable(clk);
643 /* DDR clock - sun4i+ */
644 clk = clk_get(NULL, "pll5_ddr");
646 clk_prepare_enable(clk);
651 void __init sunxi_init_clocks(void)
653 /* Register all the simple and basic clocks on DT */
656 /* Register factor clocks */
657 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
659 /* Register divider clocks */
660 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
662 /* Register mux clocks */
663 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
665 /* Register gate clocks */
666 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
668 /* Enable core system clocks */
669 sunxi_clock_protect();