1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ee38b269SMaxime Ripard /*
3ee38b269SMaxime Ripard * Copyright 2015 Maxime Ripard
4ee38b269SMaxime Ripard *
5ee38b269SMaxime Ripard * Maxime Ripard <maxime.ripard@free-electrons.com>
6ee38b269SMaxime Ripard */
7ee38b269SMaxime Ripard
8ee38b269SMaxime Ripard #include <linux/clk.h>
9ee38b269SMaxime Ripard #include <linux/clk-provider.h>
1062e59c4eSStephen Boyd #include <linux/io.h>
11ee38b269SMaxime Ripard #include <linux/of.h>
12ee38b269SMaxime Ripard #include <linux/of_address.h>
13ee38b269SMaxime Ripard #include <linux/slab.h>
14ee38b269SMaxime Ripard #include <linux/spinlock.h>
15ee38b269SMaxime Ripard
16ee38b269SMaxime Ripard static DEFINE_SPINLOCK(gates_lock);
17ee38b269SMaxime Ripard
sunxi_simple_gates_setup(struct device_node * node,const int protected[],int nprotected)18ee38b269SMaxime Ripard static void __init sunxi_simple_gates_setup(struct device_node *node,
19ee38b269SMaxime Ripard const int protected[],
20ee38b269SMaxime Ripard int nprotected)
21ee38b269SMaxime Ripard {
22ee38b269SMaxime Ripard struct clk_onecell_data *clk_data;
23ee38b269SMaxime Ripard const char *clk_parent, *clk_name;
24ee38b269SMaxime Ripard struct property *prop;
25ee38b269SMaxime Ripard struct resource res;
26ee38b269SMaxime Ripard void __iomem *clk_reg;
27ee38b269SMaxime Ripard void __iomem *reg;
28ee38b269SMaxime Ripard const __be32 *p;
29ee38b269SMaxime Ripard int number, i = 0, j;
30ee38b269SMaxime Ripard u8 clk_bit;
31ee38b269SMaxime Ripard u32 index;
32ee38b269SMaxime Ripard
33ee38b269SMaxime Ripard reg = of_io_request_and_map(node, 0, of_node_full_name(node));
34ee38b269SMaxime Ripard if (IS_ERR(reg))
35ee38b269SMaxime Ripard return;
36ee38b269SMaxime Ripard
37ee38b269SMaxime Ripard clk_parent = of_clk_get_parent_name(node, 0);
38ee38b269SMaxime Ripard
39ee38b269SMaxime Ripard clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
40ee38b269SMaxime Ripard if (!clk_data)
41ee38b269SMaxime Ripard goto err_unmap;
42ee38b269SMaxime Ripard
43ee38b269SMaxime Ripard number = of_property_count_u32_elems(node, "clock-indices");
44ee38b269SMaxime Ripard of_property_read_u32_index(node, "clock-indices", number - 1, &number);
45ee38b269SMaxime Ripard
46ee38b269SMaxime Ripard clk_data->clks = kcalloc(number + 1, sizeof(struct clk *), GFP_KERNEL);
47ee38b269SMaxime Ripard if (!clk_data->clks)
48ee38b269SMaxime Ripard goto err_free_data;
49ee38b269SMaxime Ripard
50ee38b269SMaxime Ripard of_property_for_each_u32(node, "clock-indices", prop, p, index) {
51ee38b269SMaxime Ripard of_property_read_string_index(node, "clock-output-names",
52ee38b269SMaxime Ripard i, &clk_name);
53ee38b269SMaxime Ripard
54ee38b269SMaxime Ripard clk_reg = reg + 4 * (index / 32);
55ee38b269SMaxime Ripard clk_bit = index % 32;
56ee38b269SMaxime Ripard
57ee38b269SMaxime Ripard clk_data->clks[index] = clk_register_gate(NULL, clk_name,
58ee38b269SMaxime Ripard clk_parent, 0,
59ee38b269SMaxime Ripard clk_reg,
60ee38b269SMaxime Ripard clk_bit,
61ee38b269SMaxime Ripard 0, &gates_lock);
62ee38b269SMaxime Ripard i++;
63ee38b269SMaxime Ripard
64ee38b269SMaxime Ripard if (IS_ERR(clk_data->clks[index])) {
65ee38b269SMaxime Ripard WARN_ON(true);
66ee38b269SMaxime Ripard continue;
67ee38b269SMaxime Ripard }
68ee38b269SMaxime Ripard
69ee38b269SMaxime Ripard for (j = 0; j < nprotected; j++)
70ee38b269SMaxime Ripard if (protected[j] == index)
71ee38b269SMaxime Ripard clk_prepare_enable(clk_data->clks[index]);
72ee38b269SMaxime Ripard
73ee38b269SMaxime Ripard }
74ee38b269SMaxime Ripard
75ee38b269SMaxime Ripard clk_data->clk_num = number + 1;
76ee38b269SMaxime Ripard of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
77ee38b269SMaxime Ripard
78ee38b269SMaxime Ripard return;
79ee38b269SMaxime Ripard
80ee38b269SMaxime Ripard err_free_data:
81ee38b269SMaxime Ripard kfree(clk_data);
82ee38b269SMaxime Ripard err_unmap:
83ee38b269SMaxime Ripard iounmap(reg);
84ee38b269SMaxime Ripard of_address_to_resource(node, 0, &res);
85ee38b269SMaxime Ripard release_mem_region(res.start, resource_size(&res));
86ee38b269SMaxime Ripard }
87ee38b269SMaxime Ripard
sunxi_simple_gates_init(struct device_node * node)88ee38b269SMaxime Ripard static void __init sunxi_simple_gates_init(struct device_node *node)
89ee38b269SMaxime Ripard {
90ee38b269SMaxime Ripard sunxi_simple_gates_setup(node, NULL, 0);
91ee38b269SMaxime Ripard }
92ee38b269SMaxime Ripard
936e17b418SKrzysztof Adamski CLK_OF_DECLARE(sun4i_a10_gates, "allwinner,sun4i-a10-gates-clk",
946e17b418SKrzysztof Adamski sunxi_simple_gates_init);
95ee38b269SMaxime Ripard CLK_OF_DECLARE(sun4i_a10_apb0, "allwinner,sun4i-a10-apb0-gates-clk",
96ee38b269SMaxime Ripard sunxi_simple_gates_init);
97ee38b269SMaxime Ripard CLK_OF_DECLARE(sun4i_a10_apb1, "allwinner,sun4i-a10-apb1-gates-clk",
98ee38b269SMaxime Ripard sunxi_simple_gates_init);
99ee38b269SMaxime Ripard CLK_OF_DECLARE(sun4i_a10_axi, "allwinner,sun4i-a10-axi-gates-clk",
100ee38b269SMaxime Ripard sunxi_simple_gates_init);
101ee38b269SMaxime Ripard CLK_OF_DECLARE(sun5i_a10s_apb0, "allwinner,sun5i-a10s-apb0-gates-clk",
102ee38b269SMaxime Ripard sunxi_simple_gates_init);
103ee38b269SMaxime Ripard CLK_OF_DECLARE(sun5i_a10s_apb1, "allwinner,sun5i-a10s-apb1-gates-clk",
104ee38b269SMaxime Ripard sunxi_simple_gates_init);
105ee38b269SMaxime Ripard CLK_OF_DECLARE(sun5i_a13_apb0, "allwinner,sun5i-a13-apb0-gates-clk",
106ee38b269SMaxime Ripard sunxi_simple_gates_init);
107ee38b269SMaxime Ripard CLK_OF_DECLARE(sun5i_a13_apb1, "allwinner,sun5i-a13-apb1-gates-clk",
108ee38b269SMaxime Ripard sunxi_simple_gates_init);
109ee38b269SMaxime Ripard CLK_OF_DECLARE(sun6i_a31_ahb1, "allwinner,sun6i-a31-ahb1-gates-clk",
110ee38b269SMaxime Ripard sunxi_simple_gates_init);
111ee38b269SMaxime Ripard CLK_OF_DECLARE(sun6i_a31_apb1, "allwinner,sun6i-a31-apb1-gates-clk",
112ee38b269SMaxime Ripard sunxi_simple_gates_init);
113ee38b269SMaxime Ripard CLK_OF_DECLARE(sun6i_a31_apb2, "allwinner,sun6i-a31-apb2-gates-clk",
114ee38b269SMaxime Ripard sunxi_simple_gates_init);
115ee38b269SMaxime Ripard CLK_OF_DECLARE(sun7i_a20_apb0, "allwinner,sun7i-a20-apb0-gates-clk",
116ee38b269SMaxime Ripard sunxi_simple_gates_init);
117ee38b269SMaxime Ripard CLK_OF_DECLARE(sun7i_a20_apb1, "allwinner,sun7i-a20-apb1-gates-clk",
118ee38b269SMaxime Ripard sunxi_simple_gates_init);
119ee38b269SMaxime Ripard CLK_OF_DECLARE(sun8i_a23_ahb1, "allwinner,sun8i-a23-ahb1-gates-clk",
120ee38b269SMaxime Ripard sunxi_simple_gates_init);
121ee38b269SMaxime Ripard CLK_OF_DECLARE(sun8i_a23_apb1, "allwinner,sun8i-a23-apb1-gates-clk",
122ee38b269SMaxime Ripard sunxi_simple_gates_init);
123ee38b269SMaxime Ripard CLK_OF_DECLARE(sun8i_a23_apb2, "allwinner,sun8i-a23-apb2-gates-clk",
124ee38b269SMaxime Ripard sunxi_simple_gates_init);
1257d6ddad6SMaxime Ripard CLK_OF_DECLARE(sun8i_a33_ahb1, "allwinner,sun8i-a33-ahb1-gates-clk",
1267d6ddad6SMaxime Ripard sunxi_simple_gates_init);
1272d6f5f0cSVishnu Patekar CLK_OF_DECLARE(sun8i_a83t_apb0, "allwinner,sun8i-a83t-apb0-gates-clk",
1282d6f5f0cSVishnu Patekar sunxi_simple_gates_init);
129ee38b269SMaxime Ripard CLK_OF_DECLARE(sun9i_a80_ahb0, "allwinner,sun9i-a80-ahb0-gates-clk",
130ee38b269SMaxime Ripard sunxi_simple_gates_init);
131ee38b269SMaxime Ripard CLK_OF_DECLARE(sun9i_a80_ahb1, "allwinner,sun9i-a80-ahb1-gates-clk",
132ee38b269SMaxime Ripard sunxi_simple_gates_init);
133ee38b269SMaxime Ripard CLK_OF_DECLARE(sun9i_a80_ahb2, "allwinner,sun9i-a80-ahb2-gates-clk",
134ee38b269SMaxime Ripard sunxi_simple_gates_init);
135ee38b269SMaxime Ripard CLK_OF_DECLARE(sun9i_a80_apb0, "allwinner,sun9i-a80-apb0-gates-clk",
136ee38b269SMaxime Ripard sunxi_simple_gates_init);
137ee38b269SMaxime Ripard CLK_OF_DECLARE(sun9i_a80_apb1, "allwinner,sun9i-a80-apb1-gates-clk",
138ee38b269SMaxime Ripard sunxi_simple_gates_init);
139bfcba2edSChen-Yu Tsai CLK_OF_DECLARE(sun9i_a80_apbs, "allwinner,sun9i-a80-apbs-gates-clk",
140bfcba2edSChen-Yu Tsai sunxi_simple_gates_init);
141ee38b269SMaxime Ripard
142ee38b269SMaxime Ripard static const int sun4i_a10_ahb_critical_clocks[] __initconst = {
143ee38b269SMaxime Ripard 14, /* ahb_sdram */
144ee38b269SMaxime Ripard };
145ee38b269SMaxime Ripard
sun4i_a10_ahb_init(struct device_node * node)146ee38b269SMaxime Ripard static void __init sun4i_a10_ahb_init(struct device_node *node)
147ee38b269SMaxime Ripard {
148ee38b269SMaxime Ripard sunxi_simple_gates_setup(node, sun4i_a10_ahb_critical_clocks,
149ee38b269SMaxime Ripard ARRAY_SIZE(sun4i_a10_ahb_critical_clocks));
150ee38b269SMaxime Ripard }
151ee38b269SMaxime Ripard CLK_OF_DECLARE(sun4i_a10_ahb, "allwinner,sun4i-a10-ahb-gates-clk",
152ee38b269SMaxime Ripard sun4i_a10_ahb_init);
153ee38b269SMaxime Ripard CLK_OF_DECLARE(sun5i_a10s_ahb, "allwinner,sun5i-a10s-ahb-gates-clk",
154ee38b269SMaxime Ripard sun4i_a10_ahb_init);
155ee38b269SMaxime Ripard CLK_OF_DECLARE(sun5i_a13_ahb, "allwinner,sun5i-a13-ahb-gates-clk",
156ee38b269SMaxime Ripard sun4i_a10_ahb_init);
157ee38b269SMaxime Ripard CLK_OF_DECLARE(sun7i_a20_ahb, "allwinner,sun7i-a20-ahb-gates-clk",
158ee38b269SMaxime Ripard sun4i_a10_ahb_init);
1596d3a47c2SChen-Yu Tsai
1606d3a47c2SChen-Yu Tsai static const int sun4i_a10_dram_critical_clocks[] __initconst = {
1616d3a47c2SChen-Yu Tsai 15, /* dram_output */
1626d3a47c2SChen-Yu Tsai };
1636d3a47c2SChen-Yu Tsai
sun4i_a10_dram_init(struct device_node * node)1646d3a47c2SChen-Yu Tsai static void __init sun4i_a10_dram_init(struct device_node *node)
1656d3a47c2SChen-Yu Tsai {
1666d3a47c2SChen-Yu Tsai sunxi_simple_gates_setup(node, sun4i_a10_dram_critical_clocks,
1676d3a47c2SChen-Yu Tsai ARRAY_SIZE(sun4i_a10_dram_critical_clocks));
1686d3a47c2SChen-Yu Tsai }
1696d3a47c2SChen-Yu Tsai CLK_OF_DECLARE(sun4i_a10_dram, "allwinner,sun4i-a10-dram-gates-clk",
1706d3a47c2SChen-Yu Tsai sun4i_a10_dram_init);
171