sunxi: initial 4.4 support
[openwrt/staging/yousong.git] / target / linux / sunxi / patches-4.4 / 103-clk-sunxi-add-h3-clksupport.patch
1 From ab6e23a4e388f5f2696b8e92c350f845142da118 Mon Sep 17 00:00:00 2001
2 From: Jens Kuske <jenskuske@gmail.com>
3 Date: Fri, 4 Dec 2015 22:24:40 +0100
4 Subject: [PATCH] clk: sunxi: Add H3 clocks support
5
6 The H3 clock control unit is similar to the those of other sun8i family
7 members like the A23.
8
9 It adds a new bus gates clock similar to the simple gates, but with a
10 different parent clock for each single gate.
11 Some of the gates use the new AHB2 clock as parent, whose clock source
12 is muxable between AHB1 and PLL6/2. The documentation isn't totally clear
13 about which devices belong to AHB2 now, especially USB EHIC/OHIC, so it
14 is mostly based on Allwinner kernel source code.
15
16 Signed-off-by: Jens Kuske <jenskuske@gmail.com>
17 Acked-by: Rob Herring <robh@kernel.org>
18 Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
19 ---
20 Documentation/devicetree/bindings/clock/sunxi.txt | 2 +
21 drivers/clk/sunxi/Makefile | 1 +
22 drivers/clk/sunxi/clk-sun8i-bus-gates.c | 112 ++++++++++++++++++++++
23 drivers/clk/sunxi/clk-sunxi.c | 6 ++
24 4 files changed, 121 insertions(+)
25 create mode 100644 drivers/clk/sunxi/clk-sun8i-bus-gates.c
26
27 diff --git a/Documentation/devicetree/bindings/clock/sunxi.txt b/Documentation/devicetree/bindings/clock/sunxi.txt
28 index ef0b452..014eab8 100644
29 diff --git a/drivers/clk/sunxi/Makefile b/drivers/clk/sunxi/Makefile
30 index 103efab..abf4916 100644
31 --- a/drivers/clk/sunxi/Makefile
32 +++ b/drivers/clk/sunxi/Makefile
33 @@ -10,6 +10,7 @@ obj-y += clk-a10-pll2.o
34 obj-y += clk-a20-gmac.o
35 obj-y += clk-mod0.o
36 obj-y += clk-simple-gates.o
37 +obj-y += clk-sun8i-bus-gates.o
38 obj-y += clk-sun8i-mbus.o
39 obj-y += clk-sun9i-core.o
40 obj-y += clk-sun9i-mmc.o
41 diff --git a/drivers/clk/sunxi/clk-sun8i-bus-gates.c b/drivers/clk/sunxi/clk-sun8i-bus-gates.c
42 new file mode 100644
43 index 0000000..7ab60c5
44 --- /dev/null
45 +++ b/drivers/clk/sunxi/clk-sun8i-bus-gates.c
46 @@ -0,0 +1,112 @@
47 +/*
48 + * Copyright (C) 2015 Jens Kuske <jenskuske@gmail.com>
49 + *
50 + * Based on clk-simple-gates.c, which is:
51 + * Copyright 2015 Maxime Ripard
52 + *
53 + * Maxime Ripard <maxime.ripard@free-electrons.com>
54 + *
55 + * This program is free software; you can redistribute it and/or modify
56 + * it under the terms of the GNU General Public License as published by
57 + * the Free Software Foundation; either version 2 of the License, or
58 + * (at your option) any later version.
59 + *
60 + * This program is distributed in the hope that it will be useful,
61 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
62 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63 + * GNU General Public License for more details.
64 + */
65 +
66 +#include <linux/clk.h>
67 +#include <linux/clk-provider.h>
68 +#include <linux/of.h>
69 +#include <linux/of_address.h>
70 +#include <linux/slab.h>
71 +#include <linux/spinlock.h>
72 +
73 +static DEFINE_SPINLOCK(gates_lock);
74 +
75 +static void __init sun8i_h3_bus_gates_init(struct device_node *node)
76 +{
77 + static const char * const names[] = { "ahb1", "ahb2", "apb1", "apb2" };
78 + enum { AHB1, AHB2, APB1, APB2, PARENT_MAX } clk_parent;
79 + const char *parents[PARENT_MAX];
80 + struct clk_onecell_data *clk_data;
81 + const char *clk_name;
82 + struct property *prop;
83 + struct resource res;
84 + void __iomem *clk_reg;
85 + void __iomem *reg;
86 + const __be32 *p;
87 + int number, i;
88 + u8 clk_bit;
89 + u32 index;
90 +
91 + reg = of_io_request_and_map(node, 0, of_node_full_name(node));
92 + if (IS_ERR(reg))
93 + return;
94 +
95 + for (i = 0; i < ARRAY_SIZE(names); i++) {
96 + index = of_property_match_string(node, "clock-names",
97 + names[i]);
98 + if (index < 0)
99 + return;
100 +
101 + parents[i] = of_clk_get_parent_name(node, index);
102 + }
103 +
104 + clk_data = kmalloc(sizeof(struct clk_onecell_data), GFP_KERNEL);
105 + if (!clk_data)
106 + goto err_unmap;
107 +
108 + number = of_property_count_u32_elems(node, "clock-indices");
109 + of_property_read_u32_index(node, "clock-indices", number - 1, &number);
110 +
111 + clk_data->clks = kcalloc(number + 1, sizeof(struct clk *), GFP_KERNEL);
112 + if (!clk_data->clks)
113 + goto err_free_data;
114 +
115 + i = 0;
116 + of_property_for_each_u32(node, "clock-indices", prop, p, index) {
117 + of_property_read_string_index(node, "clock-output-names",
118 + i, &clk_name);
119 +
120 + if (index == 17 || (index >= 29 && index <= 31))
121 + clk_parent = AHB2;
122 + else if (index <= 63 || index >= 128)
123 + clk_parent = AHB1;
124 + else if (index >= 64 && index <= 95)
125 + clk_parent = APB1;
126 + else if (index >= 96 && index <= 127)
127 + clk_parent = APB2;
128 +
129 + clk_reg = reg + 4 * (index / 32);
130 + clk_bit = index % 32;
131 +
132 + clk_data->clks[index] = clk_register_gate(NULL, clk_name,
133 + parents[clk_parent],
134 + 0, clk_reg, clk_bit,
135 + 0, &gates_lock);
136 + i++;
137 +
138 + if (IS_ERR(clk_data->clks[index])) {
139 + WARN_ON(true);
140 + continue;
141 + }
142 + }
143 +
144 + clk_data->clk_num = number + 1;
145 + of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
146 +
147 + return;
148 +
149 +err_free_data:
150 + kfree(clk_data);
151 +err_unmap:
152 + iounmap(reg);
153 + of_address_to_resource(node, 0, &res);
154 + release_mem_region(res.start, resource_size(&res));
155 +}
156 +
157 +CLK_OF_DECLARE(sun8i_h3_bus_gates, "allwinner,sun8i-h3-bus-gates-clk",
158 + sun8i_h3_bus_gates_init);
159 diff --git a/drivers/clk/sunxi/clk-sunxi.c b/drivers/clk/sunxi/clk-sunxi.c
160 index 9c79af0c..5ba2188 100644
161 --- a/drivers/clk/sunxi/clk-sunxi.c
162 +++ b/drivers/clk/sunxi/clk-sunxi.c
163 @@ -778,6 +778,10 @@ static const struct mux_data sun6i_a31_ahb1_mux_data __initconst = {
164 .shift = 12,
165 };
166
167 +static const struct mux_data sun8i_h3_ahb2_mux_data __initconst = {
168 + .shift = 0,
169 +};
170 +
171 static void __init sunxi_mux_clk_setup(struct device_node *node,
172 struct mux_data *data)
173 {
174 @@ -1130,6 +1134,7 @@ static const struct of_device_id clk_divs_match[] __initconst = {
175 static const struct of_device_id clk_mux_match[] __initconst = {
176 {.compatible = "allwinner,sun4i-a10-cpu-clk", .data = &sun4i_cpu_mux_data,},
177 {.compatible = "allwinner,sun6i-a31-ahb1-mux-clk", .data = &sun6i_a31_ahb1_mux_data,},
178 + {.compatible = "allwinner,sun8i-h3-ahb2-clk", .data = &sun8i_h3_ahb2_mux_data,},
179 {}
180 };
181
182 @@ -1212,6 +1217,7 @@ CLK_OF_DECLARE(sun6i_a31_clk_init, "allwinner,sun6i-a31", sun6i_init_clocks);
183 CLK_OF_DECLARE(sun6i_a31s_clk_init, "allwinner,sun6i-a31s", sun6i_init_clocks);
184 CLK_OF_DECLARE(sun8i_a23_clk_init, "allwinner,sun8i-a23", sun6i_init_clocks);
185 CLK_OF_DECLARE(sun8i_a33_clk_init, "allwinner,sun8i-a33", sun6i_init_clocks);
186 +CLK_OF_DECLARE(sun8i_h3_clk_init, "allwinner,sun8i-h3", sun6i_init_clocks);
187
188 static void __init sun9i_init_clocks(struct device_node *node)
189 {