x86/intel_rdt: Fix incorrect returned value when creating rdgroup sub-directory in...
[cris-mirror.git] / drivers / clk / mvebu / cp110-system-controller.c
blobca9a0a53617471f3e92154a4657dee9ef88988e8
1 /*
2 * Marvell Armada CP110 System Controller
4 * Copyright (C) 2016 Marvell
6 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
14 * CP110 has 6 core clocks:
16 * - APLL (1 Ghz)
17 * - PPv2 core (1/3 APLL)
18 * - EIP (1/2 APLL)
19 * - Core (1/2 EIP)
20 * - SDIO (2/5 APLL)
22 * - NAND clock, which is either:
23 * - Equal to SDIO clock
24 * - 2/5 APLL
26 * CP110 has 32 gatable clocks, for the various peripherals in the
27 * IP. They have fairly complicated parent/child relationships.
30 #define pr_fmt(fmt) "cp110-system-controller: " fmt
32 #include <linux/clk-provider.h>
33 #include <linux/mfd/syscon.h>
34 #include <linux/init.h>
35 #include <linux/of.h>
36 #include <linux/of_address.h>
37 #include <linux/platform_device.h>
38 #include <linux/regmap.h>
39 #include <linux/slab.h>
41 #define CP110_PM_CLOCK_GATING_REG 0x220
42 #define CP110_NAND_FLASH_CLK_CTRL_REG 0x700
43 #define NF_CLOCK_SEL_400_MASK BIT(0)
45 enum {
46 CP110_CLK_TYPE_CORE,
47 CP110_CLK_TYPE_GATABLE,
50 #define CP110_MAX_CORE_CLOCKS 6
51 #define CP110_MAX_GATABLE_CLOCKS 32
53 #define CP110_CLK_NUM \
54 (CP110_MAX_CORE_CLOCKS + CP110_MAX_GATABLE_CLOCKS)
56 #define CP110_CORE_APLL 0
57 #define CP110_CORE_PPV2 1
58 #define CP110_CORE_EIP 2
59 #define CP110_CORE_CORE 3
60 #define CP110_CORE_NAND 4
61 #define CP110_CORE_SDIO 5
63 /* A number of gatable clocks need special handling */
64 #define CP110_GATE_AUDIO 0
65 #define CP110_GATE_COMM_UNIT 1
66 #define CP110_GATE_NAND 2
67 #define CP110_GATE_PPV2 3
68 #define CP110_GATE_SDIO 4
69 #define CP110_GATE_MG 5
70 #define CP110_GATE_MG_CORE 6
71 #define CP110_GATE_XOR1 7
72 #define CP110_GATE_XOR0 8
73 #define CP110_GATE_GOP_DP 9
74 #define CP110_GATE_PCIE_X1_0 11
75 #define CP110_GATE_PCIE_X1_1 12
76 #define CP110_GATE_PCIE_X4 13
77 #define CP110_GATE_PCIE_XOR 14
78 #define CP110_GATE_SATA 15
79 #define CP110_GATE_SATA_USB 16
80 #define CP110_GATE_MAIN 17
81 #define CP110_GATE_SDMMC_GOP 18
82 #define CP110_GATE_SLOW_IO 21
83 #define CP110_GATE_USB3H0 22
84 #define CP110_GATE_USB3H1 23
85 #define CP110_GATE_USB3DEV 24
86 #define CP110_GATE_EIP150 25
87 #define CP110_GATE_EIP197 26
89 static const char * const gate_base_names[] = {
90 [CP110_GATE_AUDIO] = "audio",
91 [CP110_GATE_COMM_UNIT] = "communit",
92 [CP110_GATE_NAND] = "nand",
93 [CP110_GATE_PPV2] = "ppv2",
94 [CP110_GATE_SDIO] = "sdio",
95 [CP110_GATE_MG] = "mg-domain",
96 [CP110_GATE_MG_CORE] = "mg-core",
97 [CP110_GATE_XOR1] = "xor1",
98 [CP110_GATE_XOR0] = "xor0",
99 [CP110_GATE_GOP_DP] = "gop-dp",
100 [CP110_GATE_PCIE_X1_0] = "pcie_x10",
101 [CP110_GATE_PCIE_X1_1] = "pcie_x11",
102 [CP110_GATE_PCIE_X4] = "pcie_x4",
103 [CP110_GATE_PCIE_XOR] = "pcie-xor",
104 [CP110_GATE_SATA] = "sata",
105 [CP110_GATE_SATA_USB] = "sata-usb",
106 [CP110_GATE_MAIN] = "main",
107 [CP110_GATE_SDMMC_GOP] = "sd-mmc-gop",
108 [CP110_GATE_SLOW_IO] = "slow-io",
109 [CP110_GATE_USB3H0] = "usb3h0",
110 [CP110_GATE_USB3H1] = "usb3h1",
111 [CP110_GATE_USB3DEV] = "usb3dev",
112 [CP110_GATE_EIP150] = "eip150",
113 [CP110_GATE_EIP197] = "eip197"
116 struct cp110_gate_clk {
117 struct clk_hw hw;
118 struct regmap *regmap;
119 u8 bit_idx;
122 #define to_cp110_gate_clk(hw) container_of(hw, struct cp110_gate_clk, hw)
124 static int cp110_gate_enable(struct clk_hw *hw)
126 struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
128 regmap_update_bits(gate->regmap, CP110_PM_CLOCK_GATING_REG,
129 BIT(gate->bit_idx), BIT(gate->bit_idx));
131 return 0;
134 static void cp110_gate_disable(struct clk_hw *hw)
136 struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
138 regmap_update_bits(gate->regmap, CP110_PM_CLOCK_GATING_REG,
139 BIT(gate->bit_idx), 0);
142 static int cp110_gate_is_enabled(struct clk_hw *hw)
144 struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
145 u32 val;
147 regmap_read(gate->regmap, CP110_PM_CLOCK_GATING_REG, &val);
149 return val & BIT(gate->bit_idx);
152 static const struct clk_ops cp110_gate_ops = {
153 .enable = cp110_gate_enable,
154 .disable = cp110_gate_disable,
155 .is_enabled = cp110_gate_is_enabled,
158 static struct clk_hw *cp110_register_gate(const char *name,
159 const char *parent_name,
160 struct regmap *regmap, u8 bit_idx)
162 struct cp110_gate_clk *gate;
163 struct clk_hw *hw;
164 struct clk_init_data init;
165 int ret;
167 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
168 if (!gate)
169 return ERR_PTR(-ENOMEM);
171 memset(&init, 0, sizeof(init));
173 init.name = name;
174 init.ops = &cp110_gate_ops;
175 init.parent_names = &parent_name;
176 init.num_parents = 1;
178 gate->regmap = regmap;
179 gate->bit_idx = bit_idx;
180 gate->hw.init = &init;
182 hw = &gate->hw;
183 ret = clk_hw_register(NULL, hw);
184 if (ret) {
185 kfree(gate);
186 hw = ERR_PTR(ret);
189 return hw;
192 static void cp110_unregister_gate(struct clk_hw *hw)
194 clk_hw_unregister(hw);
195 kfree(to_cp110_gate_clk(hw));
198 static struct clk_hw *cp110_of_clk_get(struct of_phandle_args *clkspec,
199 void *data)
201 struct clk_hw_onecell_data *clk_data = data;
202 unsigned int type = clkspec->args[0];
203 unsigned int idx = clkspec->args[1];
205 if (type == CP110_CLK_TYPE_CORE) {
206 if (idx > CP110_MAX_CORE_CLOCKS)
207 return ERR_PTR(-EINVAL);
208 return clk_data->hws[idx];
209 } else if (type == CP110_CLK_TYPE_GATABLE) {
210 if (idx > CP110_MAX_GATABLE_CLOCKS)
211 return ERR_PTR(-EINVAL);
212 return clk_data->hws[CP110_MAX_CORE_CLOCKS + idx];
215 return ERR_PTR(-EINVAL);
218 static char *cp110_unique_name(struct device *dev, struct device_node *np,
219 const char *name)
221 const __be32 *reg;
222 u64 addr;
224 /* Do not create a name if there is no clock */
225 if (!name)
226 return NULL;
228 reg = of_get_property(np, "reg", NULL);
229 addr = of_translate_address(np, reg);
230 return devm_kasprintf(dev, GFP_KERNEL, "%llx-%s",
231 (unsigned long long)addr, name);
234 static int cp110_syscon_common_probe(struct platform_device *pdev,
235 struct device_node *syscon_node)
237 struct regmap *regmap;
238 struct device *dev = &pdev->dev;
239 struct device_node *np = dev->of_node;
240 const char *ppv2_name, *apll_name, *core_name, *eip_name, *nand_name,
241 *sdio_name;
242 struct clk_hw_onecell_data *cp110_clk_data;
243 struct clk_hw *hw, **cp110_clks;
244 u32 nand_clk_ctrl;
245 int i, ret;
246 char *gate_name[ARRAY_SIZE(gate_base_names)];
248 regmap = syscon_node_to_regmap(syscon_node);
249 if (IS_ERR(regmap))
250 return PTR_ERR(regmap);
252 ret = regmap_read(regmap, CP110_NAND_FLASH_CLK_CTRL_REG,
253 &nand_clk_ctrl);
254 if (ret)
255 return ret;
257 cp110_clk_data = devm_kzalloc(dev, sizeof(*cp110_clk_data) +
258 sizeof(struct clk_hw *) * CP110_CLK_NUM,
259 GFP_KERNEL);
260 if (!cp110_clk_data)
261 return -ENOMEM;
263 cp110_clks = cp110_clk_data->hws;
264 cp110_clk_data->num = CP110_CLK_NUM;
266 /* Register the APLL which is the root of the hw tree */
267 apll_name = cp110_unique_name(dev, syscon_node, "apll");
268 hw = clk_hw_register_fixed_rate(NULL, apll_name, NULL, 0,
269 1000 * 1000 * 1000);
270 if (IS_ERR(hw)) {
271 ret = PTR_ERR(hw);
272 goto fail_apll;
275 cp110_clks[CP110_CORE_APLL] = hw;
277 /* PPv2 is APLL/3 */
278 ppv2_name = cp110_unique_name(dev, syscon_node, "ppv2-core");
279 hw = clk_hw_register_fixed_factor(NULL, ppv2_name, apll_name, 0, 1, 3);
280 if (IS_ERR(hw)) {
281 ret = PTR_ERR(hw);
282 goto fail_ppv2;
285 cp110_clks[CP110_CORE_PPV2] = hw;
287 /* EIP clock is APLL/2 */
288 eip_name = cp110_unique_name(dev, syscon_node, "eip");
289 hw = clk_hw_register_fixed_factor(NULL, eip_name, apll_name, 0, 1, 2);
290 if (IS_ERR(hw)) {
291 ret = PTR_ERR(hw);
292 goto fail_eip;
295 cp110_clks[CP110_CORE_EIP] = hw;
297 /* Core clock is EIP/2 */
298 core_name = cp110_unique_name(dev, syscon_node, "core");
299 hw = clk_hw_register_fixed_factor(NULL, core_name, eip_name, 0, 1, 2);
300 if (IS_ERR(hw)) {
301 ret = PTR_ERR(hw);
302 goto fail_core;
305 cp110_clks[CP110_CORE_CORE] = hw;
306 /* NAND can be either APLL/2.5 or core clock */
307 nand_name = cp110_unique_name(dev, syscon_node, "nand-core");
308 if (nand_clk_ctrl & NF_CLOCK_SEL_400_MASK)
309 hw = clk_hw_register_fixed_factor(NULL, nand_name,
310 apll_name, 0, 2, 5);
311 else
312 hw = clk_hw_register_fixed_factor(NULL, nand_name,
313 core_name, 0, 1, 1);
314 if (IS_ERR(hw)) {
315 ret = PTR_ERR(hw);
316 goto fail_nand;
319 cp110_clks[CP110_CORE_NAND] = hw;
321 /* SDIO clock is APLL/2.5 */
322 sdio_name = cp110_unique_name(dev, syscon_node, "sdio-core");
323 hw = clk_hw_register_fixed_factor(NULL, sdio_name,
324 apll_name, 0, 2, 5);
325 if (IS_ERR(hw)) {
326 ret = PTR_ERR(hw);
327 goto fail_sdio;
330 cp110_clks[CP110_CORE_SDIO] = hw;
332 /* create the unique name for all the gate clocks */
333 for (i = 0; i < ARRAY_SIZE(gate_base_names); i++)
334 gate_name[i] = cp110_unique_name(dev, syscon_node,
335 gate_base_names[i]);
337 for (i = 0; i < ARRAY_SIZE(gate_base_names); i++) {
338 const char *parent;
340 if (gate_name[i] == NULL)
341 continue;
343 switch (i) {
344 case CP110_GATE_AUDIO:
345 case CP110_GATE_COMM_UNIT:
346 case CP110_GATE_EIP150:
347 case CP110_GATE_EIP197:
348 case CP110_GATE_SLOW_IO:
349 parent = gate_name[CP110_GATE_MAIN];
350 break;
351 case CP110_GATE_MG:
352 parent = gate_name[CP110_GATE_MG_CORE];
353 break;
354 case CP110_GATE_NAND:
355 parent = nand_name;
356 break;
357 case CP110_GATE_PPV2:
358 parent = ppv2_name;
359 break;
360 case CP110_GATE_SDIO:
361 parent = sdio_name;
362 break;
363 case CP110_GATE_GOP_DP:
364 parent = gate_name[CP110_GATE_SDMMC_GOP];
365 break;
366 case CP110_GATE_XOR1:
367 case CP110_GATE_XOR0:
368 case CP110_GATE_PCIE_X1_0:
369 case CP110_GATE_PCIE_X1_1:
370 case CP110_GATE_PCIE_X4:
371 parent = gate_name[CP110_GATE_PCIE_XOR];
372 break;
373 case CP110_GATE_SATA:
374 case CP110_GATE_USB3H0:
375 case CP110_GATE_USB3H1:
376 case CP110_GATE_USB3DEV:
377 parent = gate_name[CP110_GATE_SATA_USB];
378 break;
379 default:
380 parent = core_name;
381 break;
383 hw = cp110_register_gate(gate_name[i], parent, regmap, i);
385 if (IS_ERR(hw)) {
386 ret = PTR_ERR(hw);
387 goto fail_gate;
390 cp110_clks[CP110_MAX_CORE_CLOCKS + i] = hw;
393 ret = of_clk_add_hw_provider(np, cp110_of_clk_get, cp110_clk_data);
394 if (ret)
395 goto fail_clk_add;
397 platform_set_drvdata(pdev, cp110_clks);
399 return 0;
401 fail_clk_add:
402 fail_gate:
403 for (i = 0; i < CP110_MAX_GATABLE_CLOCKS; i++) {
404 hw = cp110_clks[CP110_MAX_CORE_CLOCKS + i];
406 if (hw)
407 cp110_unregister_gate(hw);
410 clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_SDIO]);
411 fail_sdio:
412 clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_NAND]);
413 fail_nand:
414 clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_CORE]);
415 fail_core:
416 clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_EIP]);
417 fail_eip:
418 clk_hw_unregister_fixed_factor(cp110_clks[CP110_CORE_PPV2]);
419 fail_ppv2:
420 clk_hw_unregister_fixed_rate(cp110_clks[CP110_CORE_APLL]);
421 fail_apll:
422 return ret;
425 static int cp110_syscon_legacy_clk_probe(struct platform_device *pdev)
427 dev_warn(&pdev->dev, FW_WARN "Using legacy device tree binding\n");
428 dev_warn(&pdev->dev, FW_WARN "Update your device tree:\n");
429 dev_warn(&pdev->dev, FW_WARN
430 "This binding won't be supported in future kernels\n");
432 return cp110_syscon_common_probe(pdev, pdev->dev.of_node);
435 static int cp110_clk_probe(struct platform_device *pdev)
437 return cp110_syscon_common_probe(pdev, pdev->dev.of_node->parent);
440 static const struct of_device_id cp110_syscon_legacy_of_match[] = {
441 { .compatible = "marvell,cp110-system-controller0", },
445 static struct platform_driver cp110_syscon_legacy_driver = {
446 .probe = cp110_syscon_legacy_clk_probe,
447 .driver = {
448 .name = "marvell-cp110-system-controller0",
449 .of_match_table = cp110_syscon_legacy_of_match,
450 .suppress_bind_attrs = true,
453 builtin_platform_driver(cp110_syscon_legacy_driver);
455 static const struct of_device_id cp110_clock_of_match[] = {
456 { .compatible = "marvell,cp110-clock", },
460 static struct platform_driver cp110_clock_driver = {
461 .probe = cp110_clk_probe,
462 .driver = {
463 .name = "marvell-cp110-clock",
464 .of_match_table = cp110_clock_of_match,
465 .suppress_bind_attrs = true,
468 builtin_platform_driver(cp110_clock_driver);