1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2014 Marvell Technology Group Ltd.
5 * Alexandre Belloni <alexandre.belloni@free-electrons.com>
6 * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
10 #include <linux/clk-provider.h>
12 #include <linux/kernel.h>
14 #include <linux/of_address.h>
15 #include <linux/slab.h>
17 #include <dt-bindings/clock/berlin2q.h>
19 #include "berlin2-div.h"
20 #include "berlin2-pll.h"
23 #define REG_PINMUX0 0x0018
24 #define REG_PINMUX5 0x002c
25 #define REG_SYSPLLCTL0 0x0030
26 #define REG_SYSPLLCTL4 0x0040
27 #define REG_CLKENABLE 0x00e8
28 #define REG_CLKSELECT0 0x00ec
29 #define REG_CLKSELECT1 0x00f0
30 #define REG_CLKSELECT2 0x00f4
31 #define REG_CLKSWITCH0 0x00f8
32 #define REG_CLKSWITCH1 0x00fc
33 #define REG_SW_GENERIC0 0x0110
34 #define REG_SW_GENERIC3 0x011c
35 #define REG_SDIO0XIN_CLKCTL 0x0158
36 #define REG_SDIO1XIN_CLKCTL 0x015c
39 static struct clk_hw_onecell_data *clk_data;
40 static DEFINE_SPINLOCK(lock);
41 static void __iomem *gbase;
42 static void __iomem *cpupll_base;
47 AVPLL_B1, AVPLL_B2, AVPLL_B3, AVPLL_B4,
48 AVPLL_B5, AVPLL_B6, AVPLL_B7, AVPLL_B8,
51 static const char *clk_names[] = {
55 [AVPLL_B1] = "avpll_b1",
56 [AVPLL_B2] = "avpll_b2",
57 [AVPLL_B3] = "avpll_b3",
58 [AVPLL_B4] = "avpll_b4",
59 [AVPLL_B5] = "avpll_b5",
60 [AVPLL_B6] = "avpll_b6",
61 [AVPLL_B7] = "avpll_b7",
62 [AVPLL_B8] = "avpll_b8",
65 static const struct berlin2_pll_map bg2q_pll_map __initconst = {
66 .vcodiv = {1, 0, 2, 0, 3, 4, 0, 6, 8},
73 static const u8 default_parent_ids[] = {
74 SYSPLL, AVPLL_B4, AVPLL_B5, AVPLL_B6, AVPLL_B7, SYSPLL
77 static const struct berlin2_div_data bg2q_divs[] __initconst = {
80 .parent_ids = default_parent_ids,
81 .num_parents = ARRAY_SIZE(default_parent_ids),
83 BERLIN2_DIV_GATE(REG_CLKENABLE, 0),
84 BERLIN2_PLL_SELECT(REG_CLKSELECT0, 0),
85 BERLIN2_DIV_SELECT(REG_CLKSELECT0, 3),
86 BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 3),
87 BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 4),
88 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 5),
90 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
91 .flags = CLK_IGNORE_UNUSED,
95 .parent_ids = default_parent_ids,
96 .num_parents = ARRAY_SIZE(default_parent_ids),
98 BERLIN2_DIV_GATE(REG_CLKENABLE, 17),
99 BERLIN2_PLL_SELECT(REG_CLKSELECT0, 6),
100 BERLIN2_DIV_SELECT(REG_CLKSELECT0, 9),
101 BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 6),
102 BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 7),
103 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 8),
105 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
110 .parent_ids = default_parent_ids,
111 .num_parents = ARRAY_SIZE(default_parent_ids),
113 BERLIN2_DIV_GATE(REG_CLKENABLE, 1),
114 BERLIN2_PLL_SELECT(REG_CLKSELECT0, 12),
115 BERLIN2_DIV_SELECT(REG_CLKSELECT0, 15),
116 BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 9),
117 BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 10),
118 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 11),
120 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
125 .parent_ids = default_parent_ids,
126 .num_parents = ARRAY_SIZE(default_parent_ids),
128 BERLIN2_DIV_GATE(REG_CLKENABLE, 4),
129 BERLIN2_PLL_SELECT(REG_CLKSELECT0, 18),
130 BERLIN2_DIV_SELECT(REG_CLKSELECT0, 21),
131 BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 12),
132 BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 13),
133 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 14),
135 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
140 .parent_ids = default_parent_ids,
141 .num_parents = ARRAY_SIZE(default_parent_ids),
143 BERLIN2_DIV_GATE(REG_CLKENABLE, 6),
144 BERLIN2_PLL_SELECT(REG_CLKSELECT0, 24),
145 BERLIN2_DIV_SELECT(REG_CLKSELECT0, 27),
146 BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 15),
147 BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 16),
148 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 17),
150 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
155 .parent_ids = default_parent_ids,
156 .num_parents = ARRAY_SIZE(default_parent_ids),
158 BERLIN2_DIV_GATE(REG_CLKENABLE, 7),
159 BERLIN2_PLL_SELECT(REG_CLKSELECT1, 0),
160 BERLIN2_DIV_SELECT(REG_CLKSELECT1, 3),
161 BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 18),
162 BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 19),
163 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 20),
165 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
166 .flags = CLK_IGNORE_UNUSED,
170 .parent_ids = default_parent_ids,
171 .num_parents = ARRAY_SIZE(default_parent_ids),
173 BERLIN2_DIV_GATE(REG_CLKENABLE, 2),
174 BERLIN2_PLL_SELECT(REG_CLKSELECT1, 6),
175 BERLIN2_DIV_SELECT(REG_CLKSELECT1, 9),
176 BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 21),
177 BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 22),
178 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 23),
180 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
185 .parent_ids = default_parent_ids,
186 .num_parents = ARRAY_SIZE(default_parent_ids),
188 BERLIN2_DIV_GATE(REG_CLKENABLE, 3),
189 BERLIN2_PLL_SELECT(REG_CLKSELECT1, 12),
190 BERLIN2_DIV_SELECT(REG_CLKSELECT1, 15),
191 BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 24),
192 BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 25),
193 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 26),
195 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
200 .parent_ids = default_parent_ids,
201 .num_parents = ARRAY_SIZE(default_parent_ids),
203 BERLIN2_DIV_GATE(REG_CLKENABLE, 19),
204 BERLIN2_PLL_SELECT(REG_CLKSELECT1, 18),
205 BERLIN2_DIV_SELECT(REG_CLKSELECT1, 21),
206 BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 27),
207 BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 28),
208 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH0, 29),
210 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
215 .parent_ids = default_parent_ids,
216 .num_parents = ARRAY_SIZE(default_parent_ids),
218 BERLIN2_DIV_GATE(REG_CLKENABLE, 21),
219 BERLIN2_PLL_SELECT(REG_CLKSELECT1, 24),
220 BERLIN2_DIV_SELECT(REG_CLKSELECT1, 27),
221 BERLIN2_PLL_SWITCH(REG_CLKSWITCH0, 30),
222 BERLIN2_DIV_SWITCH(REG_CLKSWITCH0, 31),
223 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH1, 0),
225 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
230 .parent_ids = default_parent_ids,
231 .num_parents = ARRAY_SIZE(default_parent_ids),
233 BERLIN2_DIV_GATE(REG_CLKENABLE, 20),
234 BERLIN2_PLL_SELECT(REG_CLKSELECT2, 0),
235 BERLIN2_DIV_SELECT(REG_CLKSELECT2, 3),
236 BERLIN2_PLL_SWITCH(REG_CLKSWITCH1, 1),
237 BERLIN2_DIV_SWITCH(REG_CLKSWITCH1, 2),
238 BERLIN2_DIV_D3SWITCH(REG_CLKSWITCH1, 3),
240 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
245 .parent_ids = default_parent_ids,
246 .num_parents = ARRAY_SIZE(default_parent_ids),
248 BERLIN2_SINGLE_DIV(REG_SDIO0XIN_CLKCTL),
250 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
255 .parent_ids = default_parent_ids,
256 .num_parents = ARRAY_SIZE(default_parent_ids),
258 BERLIN2_SINGLE_DIV(REG_SDIO1XIN_CLKCTL),
260 .div_flags = BERLIN2_DIV_HAS_GATE | BERLIN2_DIV_HAS_MUX,
265 static const struct berlin2_gate_data bg2q_gates[] __initconst = {
266 { "gfx2daxi", "perif", 5 },
267 { "geth0", "perif", 8 },
268 { "sata", "perif", 9 },
269 { "ahbapb", "perif", 10, CLK_IGNORE_UNUSED },
270 { "usb0", "perif", 11 },
271 { "usb1", "perif", 12 },
272 { "usb2", "perif", 13 },
273 { "usb3", "perif", 14 },
274 { "pbridge", "perif", 15, CLK_IGNORE_UNUSED },
275 { "sdio", "perif", 16 },
276 { "nfc", "perif", 18 },
277 { "pcie", "perif", 22 },
280 static void __init berlin2q_clock_setup(struct device_node *np)
282 struct device_node *parent_np = of_get_parent(np);
283 const char *parent_names[9];
288 clk_data = kzalloc(struct_size(clk_data, hws, MAX_CLKS), GFP_KERNEL);
290 of_node_put(parent_np);
293 clk_data->num = MAX_CLKS;
296 gbase = of_iomap(parent_np, 0);
298 of_node_put(parent_np);
299 pr_err("%pOF: Unable to map global base\n", np);
303 /* BG2Q CPU PLL is not part of global registers */
304 cpupll_base = of_iomap(parent_np, 1);
305 of_node_put(parent_np);
307 pr_err("%pOF: Unable to map cpupll base\n", np);
312 /* overwrite default clock names with DT provided ones */
313 clk = of_clk_get_by_name(np, clk_names[REFCLK]);
315 clk_names[REFCLK] = __clk_get_name(clk);
319 /* simple register PLLs */
320 ret = berlin2_pll_register(&bg2q_pll_map, gbase + REG_SYSPLLCTL0,
321 clk_names[SYSPLL], clk_names[REFCLK], 0);
325 ret = berlin2_pll_register(&bg2q_pll_map, cpupll_base,
326 clk_names[CPUPLL], clk_names[REFCLK], 0);
330 /* TODO: add BG2Q AVPLL */
333 * TODO: add reference clock bypass switches:
334 * memPLLSWBypass, cpuPLLSWBypass, and sysPLLSWBypass
337 /* clock divider cells */
338 for (n = 0; n < ARRAY_SIZE(bg2q_divs); n++) {
339 const struct berlin2_div_data *dd = &bg2q_divs[n];
342 for (k = 0; k < dd->num_parents; k++)
343 parent_names[k] = clk_names[dd->parent_ids[k]];
345 hws[CLKID_SYS + n] = berlin2_div_register(&dd->map, gbase,
346 dd->name, dd->div_flags, parent_names,
347 dd->num_parents, dd->flags, &lock);
350 /* clock gate cells */
351 for (n = 0; n < ARRAY_SIZE(bg2q_gates); n++) {
352 const struct berlin2_gate_data *gd = &bg2q_gates[n];
354 hws[CLKID_GFX2DAXI + n] = clk_hw_register_gate(NULL, gd->name,
355 gd->parent_name, gd->flags, gbase + REG_CLKENABLE,
356 gd->bit_idx, 0, &lock);
359 /* cpuclk divider is fixed to 1 */
361 clk_hw_register_fixed_factor(NULL, "cpu", clk_names[CPUPLL],
363 /* twdclk is derived from cpu/3 */
365 clk_hw_register_fixed_factor(NULL, "twd", "cpu", 0, 1, 3);
367 /* check for errors on leaf clocks */
368 for (n = 0; n < MAX_CLKS; n++) {
372 pr_err("%pOF: Unable to register leaf clock %d\n", np, n);
376 /* register clk-provider */
377 of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data);
382 iounmap(cpupll_base);
385 CLK_OF_DECLARE(berlin2q_clk, "marvell,berlin2q-clk",
386 berlin2q_clock_setup);