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>
23 #include "clk-factors.h"
25 static DEFINE_SPINLOCK(clk_lock);
27 /* Maximum number of parents our clocks have */
28 #define SUNXI_MAX_PARENTS 5
31 * sun4i_osc_clk_setup() - Setup function for gatable oscillator
34 #define SUNXI_OSC24M_GATE 0
36 static void __init sun4i_osc_clk_setup(struct device_node *node)
39 struct clk_fixed_rate *fixed;
40 struct clk_gate *gate;
41 const char *clk_name = node->name;
44 if (of_property_read_u32(node, "clock-frequency", &rate))
47 /* allocate fixed-rate and gate clock structs */
48 fixed = kzalloc(sizeof(struct clk_fixed_rate), GFP_KERNEL);
51 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
55 of_property_read_string(node, "clock-output-names", &clk_name);
57 /* set up gate and fixed rate properties */
58 gate->reg = of_iomap(node, 0);
59 gate->bit_idx = SUNXI_OSC24M_GATE;
60 gate->lock = &clk_lock;
61 fixed->fixed_rate = rate;
63 clk = clk_register_composite(NULL, clk_name,
66 &fixed->hw, &clk_fixed_rate_ops,
67 &gate->hw, &clk_gate_ops,
73 of_clk_add_provider(node, of_clk_src_simple_get, clk);
74 clk_register_clkdev(clk, clk_name, NULL);
83 CLK_OF_DECLARE(sun4i_osc, "allwinner,sun4i-osc-clk", sun4i_osc_clk_setup);
88 * sun4i_get_pll1_factors() - calculates n, k, m, p factors for PLL1
89 * PLL1 rate is calculated as follows
90 * rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
91 * parent_rate is always 24Mhz
94 static void sun4i_get_pll1_factors(u32 *freq, u32 parent_rate,
95 u8 *n, u8 *k, u8 *m, u8 *p)
99 /* Normalize value to a 6M multiple */
100 div = *freq / 6000000;
101 *freq = 6000000 * div;
103 /* we were called to round the frequency, we can now return */
107 /* m is always zero for pll1 */
110 /* k is 1 only on these cases */
111 if (*freq >= 768000000 || *freq == 42000000 || *freq == 54000000)
116 /* p will be 3 for divs under 10 */
120 /* p will be 2 for divs between 10 - 20 and odd divs under 32 */
121 else if (div < 20 || (div < 32 && (div & 1)))
124 /* p will be 1 for even divs under 32, divs under 40 and odd pairs
125 * of divs between 40-62 */
126 else if (div < 40 || (div < 64 && (div & 2)))
129 /* any other entries have p = 0 */
133 /* calculate a suitable n based on k and p */
140 * sun6i_a31_get_pll1_factors() - calculates n, k and m factors for PLL1
141 * PLL1 rate is calculated as follows
142 * rate = parent_rate * (n + 1) * (k + 1) / (m + 1);
143 * parent_rate should always be 24MHz
145 static void sun6i_a31_get_pll1_factors(u32 *freq, u32 parent_rate,
146 u8 *n, u8 *k, u8 *m, u8 *p)
149 * We can operate only on MHz, this will make our life easier
152 u32 freq_mhz = *freq / 1000000;
153 u32 parent_freq_mhz = parent_rate / 1000000;
156 * Round down the frequency to the closest multiple of either
159 u32 round_freq_6 = round_down(freq_mhz, 6);
160 u32 round_freq_16 = round_down(freq_mhz, 16);
162 if (round_freq_6 > round_freq_16)
163 freq_mhz = round_freq_6;
165 freq_mhz = round_freq_16;
167 *freq = freq_mhz * 1000000;
170 * If the factors pointer are null, we were just called to
171 * round down the frequency.
177 /* If the frequency is a multiple of 32 MHz, k is always 3 */
178 if (!(freq_mhz % 32))
180 /* If the frequency is a multiple of 9 MHz, k is always 2 */
181 else if (!(freq_mhz % 9))
183 /* If the frequency is a multiple of 8 MHz, k is always 1 */
184 else if (!(freq_mhz % 8))
186 /* Otherwise, we don't use the k factor */
191 * If the frequency is a multiple of 2 but not a multiple of
192 * 3, m is 3. This is the first time we use 6 here, yet we
193 * will use it on several other places.
194 * We use this number because it's the lowest frequency we can
195 * generate (with n = 0, k = 0, m = 3), so every other frequency
196 * somehow relates to this frequency.
198 if ((freq_mhz % 6) == 2 || (freq_mhz % 6) == 4)
201 * If the frequency is a multiple of 6MHz, but the factor is
204 else if ((freq_mhz / 6) & 1)
206 /* Otherwise, we end up with m = 1 */
210 /* Calculate n thanks to the above factors we already got */
211 *n = freq_mhz * (*m + 1) / ((*k + 1) * parent_freq_mhz) - 1;
214 * If n end up being outbound, and that we can still decrease
217 if ((*n + 1) > 31 && (*m + 1) > 1) {
218 *n = (*n + 1) / 2 - 1;
219 *m = (*m + 1) / 2 - 1;
224 * sun4i_get_pll5_factors() - calculates n, k factors for PLL5
225 * PLL5 rate is calculated as follows
226 * rate = parent_rate * n * (k + 1)
227 * parent_rate is always 24Mhz
230 static void sun4i_get_pll5_factors(u32 *freq, u32 parent_rate,
231 u8 *n, u8 *k, u8 *m, u8 *p)
235 /* Normalize value to a parent_rate multiple (24M) */
236 div = *freq / parent_rate;
237 *freq = parent_rate * div;
239 /* we were called to round the frequency, we can now return */
245 else if (div / 2 < 31)
247 else if (div / 3 < 31)
252 *n = DIV_ROUND_UP(div, (*k+1));
258 * sun4i_get_apb1_factors() - calculates m, p factors for APB1
259 * APB1 rate is calculated as follows
260 * rate = (parent_rate >> p) / (m + 1);
263 static void sun4i_get_apb1_factors(u32 *freq, u32 parent_rate,
264 u8 *n, u8 *k, u8 *m, u8 *p)
268 if (parent_rate < *freq)
271 parent_rate = (parent_rate + (*freq - 1)) / *freq;
274 if (parent_rate > 32)
277 if (parent_rate <= 4)
279 else if (parent_rate <= 8)
281 else if (parent_rate <= 16)
286 calcm = (parent_rate >> calcp) - 1;
288 *freq = (parent_rate >> calcp) / (calcm + 1);
290 /* we were called to round the frequency, we can now return */
301 * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
302 * MMC rate is calculated as follows
303 * rate = (parent_rate >> p) / (m + 1);
306 static void sun4i_get_mod0_factors(u32 *freq, u32 parent_rate,
307 u8 *n, u8 *k, u8 *m, u8 *p)
309 u8 div, calcm, calcp;
311 /* These clocks can only divide, so we will never be able to achieve
312 * frequencies higher than the parent frequency */
313 if (*freq > parent_rate)
316 div = parent_rate / *freq;
320 else if (div / 2 < 16)
322 else if (div / 4 < 16)
327 calcm = DIV_ROUND_UP(div, 1 << calcp);
329 *freq = (parent_rate >> calcp) / calcm;
331 /* we were called to round the frequency, we can now return */
342 * sun7i_a20_get_out_factors() - calculates m, p factors for CLK_OUT_A/B
343 * CLK_OUT rate is calculated as follows
344 * rate = (parent_rate >> p) / (m + 1);
347 static void sun7i_a20_get_out_factors(u32 *freq, u32 parent_rate,
348 u8 *n, u8 *k, u8 *m, u8 *p)
350 u8 div, calcm, calcp;
352 /* These clocks can only divide, so we will never be able to achieve
353 * frequencies higher than the parent frequency */
354 if (*freq > parent_rate)
357 div = parent_rate / *freq;
361 else if (div / 2 < 32)
363 else if (div / 4 < 32)
368 calcm = DIV_ROUND_UP(div, 1 << calcp);
370 *freq = (parent_rate >> calcp) / calcm;
372 /* we were called to round the frequency, we can now return */
383 * sunxi_factors_clk_setup() - Setup function for factor clocks
386 #define SUNXI_FACTORS_MUX_MASK 0x3
388 struct factors_data {
391 struct clk_factors_config *table;
392 void (*getter) (u32 *rate, u32 parent_rate, u8 *n, u8 *k, u8 *m, u8 *p);
396 static struct clk_factors_config sun4i_pll1_config = {
407 static struct clk_factors_config sun6i_a31_pll1_config = {
416 static struct clk_factors_config sun4i_pll5_config = {
423 static struct clk_factors_config sun4i_apb1_config = {
430 /* user manual says "n" but it's really "p" */
431 static struct clk_factors_config sun4i_mod0_config = {
438 /* user manual says "n" but it's really "p" */
439 static struct clk_factors_config sun7i_a20_out_config = {
446 static const struct factors_data sun4i_pll1_data __initconst = {
448 .table = &sun4i_pll1_config,
449 .getter = sun4i_get_pll1_factors,
452 static const struct factors_data sun6i_a31_pll1_data __initconst = {
454 .table = &sun6i_a31_pll1_config,
455 .getter = sun6i_a31_get_pll1_factors,
458 static const struct factors_data sun4i_pll5_data __initconst = {
460 .table = &sun4i_pll5_config,
461 .getter = sun4i_get_pll5_factors,
465 static const struct factors_data sun4i_pll6_data __initconst = {
467 .table = &sun4i_pll5_config,
468 .getter = sun4i_get_pll5_factors,
472 static const struct factors_data sun4i_apb1_data __initconst = {
473 .table = &sun4i_apb1_config,
474 .getter = sun4i_get_apb1_factors,
477 static const struct factors_data sun4i_mod0_data __initconst = {
480 .table = &sun4i_mod0_config,
481 .getter = sun4i_get_mod0_factors,
484 static const struct factors_data sun7i_a20_out_data __initconst = {
487 .table = &sun7i_a20_out_config,
488 .getter = sun7i_a20_get_out_factors,
491 static struct clk * __init sunxi_factors_clk_setup(struct device_node *node,
492 const struct factors_data *data)
495 struct clk_factors *factors;
496 struct clk_gate *gate = NULL;
497 struct clk_mux *mux = NULL;
498 struct clk_hw *gate_hw = NULL;
499 struct clk_hw *mux_hw = NULL;
500 const char *clk_name = node->name;
501 const char *parents[SUNXI_MAX_PARENTS];
505 reg = of_iomap(node, 0);
507 /* if we have a mux, we will have >1 parents */
508 while (i < SUNXI_MAX_PARENTS &&
509 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
513 * some factor clocks, such as pll5 and pll6, may have multiple
514 * outputs, and have their name designated in factors_data
517 clk_name = data->name;
519 of_property_read_string(node, "clock-output-names", &clk_name);
521 factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
525 /* Add a gate if this factor clock can be gated */
527 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
533 /* set up gate properties */
535 gate->bit_idx = data->enable;
536 gate->lock = &clk_lock;
540 /* Add a mux if this factor clock can be muxed */
542 mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
549 /* set up gate properties */
551 mux->shift = data->mux;
552 mux->mask = SUNXI_FACTORS_MUX_MASK;
553 mux->lock = &clk_lock;
557 /* set up factors properties */
559 factors->config = data->table;
560 factors->get_factors = data->getter;
561 factors->lock = &clk_lock;
563 clk = clk_register_composite(NULL, clk_name,
565 mux_hw, &clk_mux_ops,
566 &factors->hw, &clk_factors_ops,
567 gate_hw, &clk_gate_ops, 0);
570 of_clk_add_provider(node, of_clk_src_simple_get, clk);
571 clk_register_clkdev(clk, clk_name, NULL);
580 * sunxi_mux_clk_setup() - Setup function for muxes
583 #define SUNXI_MUX_GATE_WIDTH 2
589 static const struct mux_data sun4i_cpu_mux_data __initconst = {
593 static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
597 static const struct mux_data sun4i_apb1_mux_data __initconst = {
601 static void __init sunxi_mux_clk_setup(struct device_node *node,
602 struct mux_data *data)
605 const char *clk_name = node->name;
606 const char *parents[SUNXI_MAX_PARENTS];
610 reg = of_iomap(node, 0);
612 while (i < SUNXI_MAX_PARENTS &&
613 (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
616 of_property_read_string(node, "clock-output-names", &clk_name);
618 clk = clk_register_mux(NULL, clk_name, parents, i,
619 CLK_SET_RATE_NO_REPARENT, reg,
620 data->shift, SUNXI_MUX_GATE_WIDTH,
624 of_clk_add_provider(node, of_clk_src_simple_get, clk);
625 clk_register_clkdev(clk, clk_name, NULL);
632 * sunxi_divider_clk_setup() - Setup function for simple divider clocks
641 static const struct div_data sun4i_axi_data __initconst = {
647 static const struct div_data sun4i_ahb_data __initconst = {
653 static const struct div_data sun4i_apb0_data __initconst = {
659 static const struct div_data sun6i_a31_apb2_div_data __initconst = {
665 static void __init sunxi_divider_clk_setup(struct device_node *node,
666 struct div_data *data)
669 const char *clk_name = node->name;
670 const char *clk_parent;
673 reg = of_iomap(node, 0);
675 clk_parent = of_clk_get_parent_name(node, 0);
677 of_property_read_string(node, "clock-output-names", &clk_name);
679 clk = clk_register_divider(NULL, clk_name, clk_parent, 0,
680 reg, data->shift, data->width,
681 data->pow ? CLK_DIVIDER_POWER_OF_TWO : 0,
684 of_clk_add_provider(node, of_clk_src_simple_get, clk);
685 clk_register_clkdev(clk, clk_name, NULL);
692 * sunxi_gates_reset... - reset bits in leaf gate clk registers handling
695 struct gates_reset_data {
698 struct reset_controller_dev rcdev;
701 static int sunxi_gates_reset_assert(struct reset_controller_dev *rcdev,
704 struct gates_reset_data *data = container_of(rcdev,
705 struct gates_reset_data,
710 spin_lock_irqsave(data->lock, flags);
712 reg = readl(data->reg);
713 writel(reg & ~BIT(id), data->reg);
715 spin_unlock_irqrestore(data->lock, flags);
720 static int sunxi_gates_reset_deassert(struct reset_controller_dev *rcdev,
723 struct gates_reset_data *data = container_of(rcdev,
724 struct gates_reset_data,
729 spin_lock_irqsave(data->lock, flags);
731 reg = readl(data->reg);
732 writel(reg | BIT(id), data->reg);
734 spin_unlock_irqrestore(data->lock, flags);
739 static struct reset_control_ops sunxi_gates_reset_ops = {
740 .assert = sunxi_gates_reset_assert,
741 .deassert = sunxi_gates_reset_deassert,
745 * sunxi_gates_clk_setup() - Setup function for leaf gates on clocks
748 #define SUNXI_GATES_MAX_SIZE 64
751 DECLARE_BITMAP(mask, SUNXI_GATES_MAX_SIZE);
755 static const struct gates_data sun4i_axi_gates_data __initconst = {
759 static const struct gates_data sun4i_ahb_gates_data __initconst = {
760 .mask = {0x7F77FFF, 0x14FB3F},
763 static const struct gates_data sun5i_a10s_ahb_gates_data __initconst = {
764 .mask = {0x147667e7, 0x185915},
767 static const struct gates_data sun5i_a13_ahb_gates_data __initconst = {
768 .mask = {0x107067e7, 0x185111},
771 static const struct gates_data sun6i_a31_ahb1_gates_data __initconst = {
772 .mask = {0xEDFE7F62, 0x794F931},
775 static const struct gates_data sun7i_a20_ahb_gates_data __initconst = {
776 .mask = { 0x12f77fff, 0x16ff3f },
779 static const struct gates_data sun4i_apb0_gates_data __initconst = {
783 static const struct gates_data sun5i_a10s_apb0_gates_data __initconst = {
787 static const struct gates_data sun5i_a13_apb0_gates_data __initconst = {
791 static const struct gates_data sun7i_a20_apb0_gates_data __initconst = {
795 static const struct gates_data sun4i_apb1_gates_data __initconst = {
799 static const struct gates_data sun5i_a10s_apb1_gates_data __initconst = {
803 static const struct gates_data sun5i_a13_apb1_gates_data __initconst = {
807 static const struct gates_data sun6i_a31_apb1_gates_data __initconst = {
811 static const struct gates_data sun6i_a31_apb2_gates_data __initconst = {
815 static const struct gates_data sun7i_a20_apb1_gates_data __initconst = {
816 .mask = { 0xff80ff },
819 static const struct gates_data sun4i_a10_usb_gates_data __initconst = {
824 static const struct gates_data sun5i_a13_usb_gates_data __initconst = {
829 static void __init sunxi_gates_clk_setup(struct device_node *node,
830 struct gates_data *data)
832 struct clk_onecell_data *clk_data;
833 struct gates_reset_data *reset_data;
834 const char *clk_parent;
835 const char *clk_name;
842 reg = of_iomap(node, 0);
844 clk_parent = of_clk_get_parent_name(node, 0);
846 /* Worst-case size approximation and memory allocation */
847 qty = find_last_bit(data->mask, SUNXI_GATES_MAX_SIZE);
848 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
851 clk_data->clks = kzalloc((qty+1) * sizeof(struct clk *), GFP_KERNEL);
852 if (!clk_data->clks) {
857 for_each_set_bit(i, data->mask, SUNXI_GATES_MAX_SIZE) {
858 of_property_read_string_index(node, "clock-output-names",
861 /* No driver claims this clock, but it should remain gated */
862 ignore = !strcmp("ahb_sdram", clk_name) ? CLK_IGNORE_UNUSED : 0;
864 clk_data->clks[i] = clk_register_gate(NULL, clk_name,
866 reg + 4 * (i/32), i % 32,
868 WARN_ON(IS_ERR(clk_data->clks[i]));
873 /* Adjust to the real max */
874 clk_data->clk_num = i;
876 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
878 /* Register a reset controler for gates with reset bits */
879 if (data->reset_mask == 0)
882 reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
886 reset_data->reg = reg;
887 reset_data->lock = &clk_lock;
888 reset_data->rcdev.nr_resets = __fls(data->reset_mask) + 1;
889 reset_data->rcdev.ops = &sunxi_gates_reset_ops;
890 reset_data->rcdev.of_node = node;
891 reset_controller_register(&reset_data->rcdev);
897 * sunxi_divs_clk_setup() helper data
900 #define SUNXI_DIVS_MAX_QTY 2
901 #define SUNXI_DIVISOR_WIDTH 2
904 const struct factors_data *factors; /* data for the factor clock */
906 u8 fixed; /* is it a fixed divisor? if not... */
907 struct clk_div_table *table; /* is it a table based divisor? */
908 u8 shift; /* otherwise it's a normal divisor with this shift */
909 u8 pow; /* is it power-of-two based? */
910 u8 gate; /* is it independently gateable? */
911 } div[SUNXI_DIVS_MAX_QTY];
914 static struct clk_div_table pll6_sata_tbl[] = {
915 { .val = 0, .div = 6, },
916 { .val = 1, .div = 12, },
917 { .val = 2, .div = 18, },
918 { .val = 3, .div = 24, },
922 static const struct divs_data pll5_divs_data __initconst = {
923 .factors = &sun4i_pll5_data,
925 { .shift = 0, .pow = 0, }, /* M, DDR */
926 { .shift = 16, .pow = 1, }, /* P, other */
930 static const struct divs_data pll6_divs_data __initconst = {
931 .factors = &sun4i_pll6_data,
933 { .shift = 0, .table = pll6_sata_tbl, .gate = 14 }, /* M, SATA */
934 { .fixed = 2 }, /* P, other */
939 * sunxi_divs_clk_setup() - Setup function for leaf divisors on clocks
941 * These clocks look something like this
942 * ________________________
943 * | ___divisor 1---|----> to consumer
944 * parent >--| pll___/___divisor 2---|----> to consumer
945 * | \_______________|____> to consumer
946 * |________________________|
949 static void __init sunxi_divs_clk_setup(struct device_node *node,
950 struct divs_data *data)
952 struct clk_onecell_data *clk_data;
954 const char *clk_name;
955 struct clk **clks, *pclk;
956 struct clk_hw *gate_hw, *rate_hw;
957 const struct clk_ops *rate_ops;
958 struct clk_gate *gate = NULL;
959 struct clk_fixed_factor *fix_factor;
960 struct clk_divider *divider;
965 /* Set up factor clock that we will be dividing */
966 pclk = sunxi_factors_clk_setup(node, data->factors);
967 parent = __clk_get_name(pclk);
969 reg = of_iomap(node, 0);
971 clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
975 clks = kzalloc((SUNXI_DIVS_MAX_QTY+1) * sizeof(*clks), GFP_KERNEL);
979 clk_data->clks = clks;
981 /* It's not a good idea to have automatic reparenting changing
983 clkflags = !strcmp("pll5", parent) ? 0 : CLK_SET_RATE_PARENT;
985 for (i = 0; i < SUNXI_DIVS_MAX_QTY; i++) {
986 if (of_property_read_string_index(node, "clock-output-names",
994 /* If this leaf clock can be gated, create a gate */
995 if (data->div[i].gate) {
996 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
1001 gate->bit_idx = data->div[i].gate;
1002 gate->lock = &clk_lock;
1004 gate_hw = &gate->hw;
1007 /* Leaves can be fixed or configurable divisors */
1008 if (data->div[i].fixed) {
1009 fix_factor = kzalloc(sizeof(*fix_factor), GFP_KERNEL);
1013 fix_factor->mult = 1;
1014 fix_factor->div = data->div[i].fixed;
1016 rate_hw = &fix_factor->hw;
1017 rate_ops = &clk_fixed_factor_ops;
1019 divider = kzalloc(sizeof(*divider), GFP_KERNEL);
1023 flags = data->div[i].pow ? CLK_DIVIDER_POWER_OF_TWO : 0;
1026 divider->shift = data->div[i].shift;
1027 divider->width = SUNXI_DIVISOR_WIDTH;
1028 divider->flags = flags;
1029 divider->lock = &clk_lock;
1030 divider->table = data->div[i].table;
1032 rate_hw = ÷r->hw;
1033 rate_ops = &clk_divider_ops;
1036 /* Wrap the (potential) gate and the divisor on a composite
1037 * clock to unify them */
1038 clks[i] = clk_register_composite(NULL, clk_name, &parent, 1,
1041 gate_hw, &clk_gate_ops,
1044 WARN_ON(IS_ERR(clk_data->clks[i]));
1045 clk_register_clkdev(clks[i], clk_name, NULL);
1048 /* The last clock available on the getter is the parent */
1051 /* Adjust to the real max */
1052 clk_data->clk_num = i;
1054 of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
1068 /* Matches for factors clocks */
1069 static const struct of_device_id clk_factors_match[] __initconst = {
1070 {.compatible = "allwinner,sun4i-pll1-clk", .data = &sun4i_pll1_data,},
1071 {.compatible = "allwinner,sun6i-a31-pll1-clk", .data = &sun6i_a31_pll1_data,},
1072 {.compatible = "allwinner,sun4i-apb1-clk", .data = &sun4i_apb1_data,},
1073 {.compatible = "allwinner,sun4i-mod0-clk", .data = &sun4i_mod0_data,},
1074 {.compatible = "allwinner,sun7i-a20-out-clk", .data = &sun7i_a20_out_data,},
1078 /* Matches for divider clocks */
1079 static const struct of_device_id clk_div_match[] __initconst = {
1080 {.compatible = "allwinner,sun4i-axi-clk", .data = &sun4i_axi_data,},
1081 {.compatible = "allwinner,sun4i-ahb-clk", .data = &sun4i_ahb_data,},
1082 {.compatible = "allwinner,sun4i-apb0-clk", .data = &sun4i_apb0_data,},
1083 {.compatible = "allwinner,sun6i-a31-apb2-div-clk", .data = &sun6i_a31_apb2_div_data,},
1087 /* Matches for divided outputs */
1088 static const struct of_device_id clk_divs_match[] __initconst = {
1089 {.compatible = "allwinner,sun4i-pll5-clk", .data = &pll5_divs_data,},
1090 {.compatible = "allwinner,sun4i-pll6-clk", .data = &pll6_divs_data,},
1094 /* Matches for mux clocks */
1095 static const struct of_device_id clk_mux_match[] __initconst = {
1096 {.compatible = "allwinner,sun4i-cpu-clk", .data = &sun4i_cpu_mux_data,},
1097 {.compatible = "allwinner,sun4i-apb1-mux-clk", .data = &sun4i_apb1_mux_data,},
1098 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
1102 /* Matches for gate clocks */
1103 static const struct of_device_id clk_gates_match[] __initconst = {
1104 {.compatible = "allwinner,sun4i-axi-gates-clk", .data = &sun4i_axi_gates_data,},
1105 {.compatible = "allwinner,sun4i-ahb-gates-clk", .data = &sun4i_ahb_gates_data,},
1106 {.compatible = "allwinner,sun5i-a10s-ahb-gates-clk", .data = &sun5i_a10s_ahb_gates_data,},
1107 {.compatible = "allwinner,sun5i-a13-ahb-gates-clk", .data = &sun5i_a13_ahb_gates_data,},
1108 {.compatible = "allwinner,sun6i-a31-ahb1-gates-clk", .data = &sun6i_a31_ahb1_gates_data,},
1109 {.compatible = "allwinner,sun7i-a20-ahb-gates-clk", .data = &sun7i_a20_ahb_gates_data,},
1110 {.compatible = "allwinner,sun4i-apb0-gates-clk", .data = &sun4i_apb0_gates_data,},
1111 {.compatible = "allwinner,sun5i-a10s-apb0-gates-clk", .data = &sun5i_a10s_apb0_gates_data,},
1112 {.compatible = "allwinner,sun5i-a13-apb0-gates-clk", .data = &sun5i_a13_apb0_gates_data,},
1113 {.compatible = "allwinner,sun7i-a20-apb0-gates-clk", .data = &sun7i_a20_apb0_gates_data,},
1114 {.compatible = "allwinner,sun4i-apb1-gates-clk", .data = &sun4i_apb1_gates_data,},
1115 {.compatible = "allwinner,sun5i-a10s-apb1-gates-clk", .data = &sun5i_a10s_apb1_gates_data,},
1116 {.compatible = "allwinner,sun5i-a13-apb1-gates-clk", .data = &sun5i_a13_apb1_gates_data,},
1117 {.compatible = "allwinner,sun6i-a31-apb1-gates-clk", .data = &sun6i_a31_apb1_gates_data,},
1118 {.compatible = "allwinner,sun7i-a20-apb1-gates-clk", .data = &sun7i_a20_apb1_gates_data,},
1119 {.compatible = "allwinner,sun6i-a31-apb2-gates-clk", .data = &sun6i_a31_apb2_gates_data,},
1120 {.compatible = "allwinner,sun4i-a10-usb-clk", .data = &sun4i_a10_usb_gates_data,},
1121 {.compatible = "allwinner,sun5i-a13-usb-clk", .data = &sun5i_a13_usb_gates_data,},
1125 static void __init of_sunxi_table_clock_setup(const struct of_device_id *clk_match,
1128 struct device_node *np;
1129 const struct div_data *data;
1130 const struct of_device_id *match;
1131 void (*setup_function)(struct device_node *, const void *) = function;
1133 for_each_matching_node(np, clk_match) {
1134 match = of_match_node(clk_match, np);
1136 setup_function(np, data);
1141 * System clock protection
1143 * By enabling these critical clocks, we prevent their accidental gating
1146 static void __init sunxi_clock_protect(void)
1150 /* memory bus clock - sun5i+ */
1151 clk = clk_get(NULL, "mbus");
1153 clk_prepare_enable(clk);
1157 /* DDR clock - sun4i+ */
1158 clk = clk_get(NULL, "pll5_ddr");
1160 clk_prepare_enable(clk);
1165 static void __init sunxi_init_clocks(void)
1167 /* Register factor clocks */
1168 of_sunxi_table_clock_setup(clk_factors_match, sunxi_factors_clk_setup);
1170 /* Register divider clocks */
1171 of_sunxi_table_clock_setup(clk_div_match, sunxi_divider_clk_setup);
1173 /* Register divided output clocks */
1174 of_sunxi_table_clock_setup(clk_divs_match, sunxi_divs_clk_setup);
1176 /* Register mux clocks */
1177 of_sunxi_table_clock_setup(clk_mux_match, sunxi_mux_clk_setup);
1179 /* Register gate clocks */
1180 of_sunxi_table_clock_setup(clk_gates_match, sunxi_gates_clk_setup);
1182 /* Enable core system clocks */
1183 sunxi_clock_protect();
1185 CLK_OF_DECLARE(sun4i_a10_clk_init, "allwinner,sun4i-a10", sunxi_init_clocks);
1186 CLK_OF_DECLARE(sun5i_a10s_clk_init, "allwinner,sun5i-a10s", sunxi_init_clocks);
1187 CLK_OF_DECLARE(sun5i_a13_clk_init, "allwinner,sun5i-a13", sunxi_init_clocks);
1188 CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sunxi_init_clocks);
1189 CLK_OF_DECLARE(sun7i_a20_clk_init, "allwinner,sun7i-a20", sunxi_init_clocks);