blob: 59fe76e4727590292ff7cbf88986afd943a6ddc6 [file] [log] [blame]
Thomas Petazzonid3da3ea2016-04-14 17:33:33 +02001/*
2 * Marvell Armada CP110 System Controller
3 *
4 * Copyright (C) 2016 Marvell
5 *
6 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
7 *
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.
11 */
12
13/*
14 * CP110 has 5 core clocks:
15 *
16 * - APLL (1 Ghz)
17 * - PPv2 core (1/3 APLL)
18 * - EIP (1/2 APLL)
19 * - Core (1/2 EIP)
20 *
21 * - NAND clock, which is either:
22 * - Equal to the core clock
23 * - 2/5 APLL
24 *
25 * CP110 has 32 gatable clocks, for the various peripherals in the
26 * IP. They have fairly complicated parent/child relationships.
27 */
28
29#define pr_fmt(fmt) "cp110-system-controller: " fmt
30
31#include <linux/clk-provider.h>
32#include <linux/mfd/syscon.h>
33#include <linux/module.h>
34#include <linux/of.h>
35#include <linux/of_address.h>
36#include <linux/platform_device.h>
37#include <linux/regmap.h>
38#include <linux/slab.h>
39
40#define CP110_PM_CLOCK_GATING_REG 0x220
41#define CP110_NAND_FLASH_CLK_CTRL_REG 0x700
42#define NF_CLOCK_SEL_400_MASK BIT(0)
43
44enum {
45 CP110_CLK_TYPE_CORE,
46 CP110_CLK_TYPE_GATABLE,
47};
48
49#define CP110_MAX_CORE_CLOCKS 5
50#define CP110_MAX_GATABLE_CLOCKS 32
51
52#define CP110_CLK_NUM \
53 (CP110_MAX_CORE_CLOCKS + CP110_MAX_GATABLE_CLOCKS)
54
55#define CP110_CORE_APLL 0
56#define CP110_CORE_PPV2 1
57#define CP110_CORE_EIP 2
58#define CP110_CORE_CORE 3
59#define CP110_CORE_NAND 4
60
61/* A number of gatable clocks need special handling */
62#define CP110_GATE_AUDIO 0
63#define CP110_GATE_COMM_UNIT 1
64#define CP110_GATE_NAND 2
65#define CP110_GATE_PPV2 3
66#define CP110_GATE_SDIO 4
67#define CP110_GATE_XOR1 7
68#define CP110_GATE_XOR0 8
69#define CP110_GATE_PCIE_X1_0 11
70#define CP110_GATE_PCIE_X1_1 12
71#define CP110_GATE_PCIE_X4 13
72#define CP110_GATE_PCIE_XOR 14
73#define CP110_GATE_SATA 15
74#define CP110_GATE_SATA_USB 16
75#define CP110_GATE_MAIN 17
76#define CP110_GATE_SDMMC 18
77#define CP110_GATE_SLOW_IO 21
78#define CP110_GATE_USB3H0 22
79#define CP110_GATE_USB3H1 23
80#define CP110_GATE_USB3DEV 24
81#define CP110_GATE_EIP150 25
82#define CP110_GATE_EIP197 26
83
84static struct clk *cp110_clks[CP110_CLK_NUM];
85
86static struct clk_onecell_data cp110_clk_data = {
87 .clks = cp110_clks,
88 .clk_num = CP110_CLK_NUM,
89};
90
91struct cp110_gate_clk {
92 struct clk_hw hw;
93 struct regmap *regmap;
94 u8 bit_idx;
95};
96
97#define to_cp110_gate_clk(clk) container_of(clk, struct cp110_gate_clk, hw)
98
99static int cp110_gate_enable(struct clk_hw *hw)
100{
101 struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
102
103 regmap_update_bits(gate->regmap, CP110_PM_CLOCK_GATING_REG,
104 BIT(gate->bit_idx), BIT(gate->bit_idx));
105
106 return 0;
107}
108
109static void cp110_gate_disable(struct clk_hw *hw)
110{
111 struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
112
113 regmap_update_bits(gate->regmap, CP110_PM_CLOCK_GATING_REG,
114 BIT(gate->bit_idx), 0);
115}
116
117static int cp110_gate_is_enabled(struct clk_hw *hw)
118{
119 struct cp110_gate_clk *gate = to_cp110_gate_clk(hw);
120 u32 val;
121
122 regmap_read(gate->regmap, CP110_PM_CLOCK_GATING_REG, &val);
123
124 return val & BIT(gate->bit_idx);
125}
126
127static const struct clk_ops cp110_gate_ops = {
128 .enable = cp110_gate_enable,
129 .disable = cp110_gate_disable,
130 .is_enabled = cp110_gate_is_enabled,
131};
132
133static struct clk *cp110_register_gate(const char *name,
134 const char *parent_name,
135 struct regmap *regmap, u8 bit_idx)
136{
137 struct cp110_gate_clk *gate;
138 struct clk *clk;
139 struct clk_init_data init;
140
141 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
142 if (!gate)
143 return ERR_PTR(-ENOMEM);
144
Marcin Wojtasad715b22016-09-21 11:05:57 +0200145 memset(&init, 0, sizeof(init));
146
Thomas Petazzonid3da3ea2016-04-14 17:33:33 +0200147 init.name = name;
148 init.ops = &cp110_gate_ops;
149 init.parent_names = &parent_name;
150 init.num_parents = 1;
151
152 gate->regmap = regmap;
153 gate->bit_idx = bit_idx;
154 gate->hw.init = &init;
155
156 clk = clk_register(NULL, &gate->hw);
157 if (IS_ERR(clk))
158 kfree(gate);
159
160 return clk;
161}
162
163static void cp110_unregister_gate(struct clk *clk)
164{
165 struct clk_hw *hw;
166
167 hw = __clk_get_hw(clk);
168 if (!hw)
169 return;
170
171 clk_unregister(clk);
172 kfree(to_cp110_gate_clk(hw));
173}
174
175static struct clk *cp110_of_clk_get(struct of_phandle_args *clkspec, void *data)
176{
177 struct clk_onecell_data *clk_data = data;
178 unsigned int type = clkspec->args[0];
179 unsigned int idx = clkspec->args[1];
180
181 if (type == CP110_CLK_TYPE_CORE) {
182 if (idx > CP110_MAX_CORE_CLOCKS)
183 return ERR_PTR(-EINVAL);
184 return clk_data->clks[idx];
185 } else if (type == CP110_CLK_TYPE_GATABLE) {
186 if (idx > CP110_MAX_GATABLE_CLOCKS)
187 return ERR_PTR(-EINVAL);
188 return clk_data->clks[CP110_MAX_CORE_CLOCKS + idx];
189 }
190
191 return ERR_PTR(-EINVAL);
192}
193
194static int cp110_syscon_clk_probe(struct platform_device *pdev)
195{
196 struct regmap *regmap;
197 struct device_node *np = pdev->dev.of_node;
198 const char *ppv2_name, *apll_name, *core_name, *eip_name, *nand_name;
199 struct clk *clk;
200 u32 nand_clk_ctrl;
201 int i, ret;
202
203 regmap = syscon_node_to_regmap(np);
204 if (IS_ERR(regmap))
205 return PTR_ERR(regmap);
206
207 ret = regmap_read(regmap, CP110_NAND_FLASH_CLK_CTRL_REG,
208 &nand_clk_ctrl);
209 if (ret)
210 return ret;
211
212 /* Register the APLL which is the root of the clk tree */
213 of_property_read_string_index(np, "core-clock-output-names",
214 CP110_CORE_APLL, &apll_name);
215 clk = clk_register_fixed_rate(NULL, apll_name, NULL, 0,
216 1000 * 1000 * 1000);
217 if (IS_ERR(clk)) {
218 ret = PTR_ERR(clk);
219 goto fail0;
220 }
221
222 cp110_clks[CP110_CORE_APLL] = clk;
223
224 /* PPv2 is APLL/3 */
225 of_property_read_string_index(np, "core-clock-output-names",
226 CP110_CORE_PPV2, &ppv2_name);
227 clk = clk_register_fixed_factor(NULL, ppv2_name, apll_name, 0, 1, 3);
228 if (IS_ERR(clk)) {
229 ret = PTR_ERR(clk);
230 goto fail1;
231 }
232
233 cp110_clks[CP110_CORE_PPV2] = clk;
234
235 /* EIP clock is APLL/2 */
236 of_property_read_string_index(np, "core-clock-output-names",
237 CP110_CORE_EIP, &eip_name);
238 clk = clk_register_fixed_factor(NULL, eip_name, apll_name, 0, 1, 2);
239 if (IS_ERR(clk)) {
240 ret = PTR_ERR(clk);
241 goto fail2;
242 }
243
244 cp110_clks[CP110_CORE_EIP] = clk;
245
246 /* Core clock is EIP/2 */
247 of_property_read_string_index(np, "core-clock-output-names",
248 CP110_CORE_CORE, &core_name);
249 clk = clk_register_fixed_factor(NULL, core_name, eip_name, 0, 1, 2);
250 if (IS_ERR(clk)) {
251 ret = PTR_ERR(clk);
252 goto fail3;
253 }
254
255 cp110_clks[CP110_CORE_CORE] = clk;
256
257 /* NAND can be either APLL/2.5 or core clock */
258 of_property_read_string_index(np, "core-clock-output-names",
259 CP110_CORE_NAND, &nand_name);
260 if (nand_clk_ctrl & NF_CLOCK_SEL_400_MASK)
261 clk = clk_register_fixed_factor(NULL, nand_name,
262 apll_name, 0, 2, 5);
263 else
264 clk = clk_register_fixed_factor(NULL, nand_name,
265 core_name, 0, 1, 1);
266 if (IS_ERR(clk)) {
267 ret = PTR_ERR(clk);
268 goto fail4;
269 }
270
271 cp110_clks[CP110_CORE_NAND] = clk;
272
273 for (i = 0; i < CP110_MAX_GATABLE_CLOCKS; i++) {
274 const char *parent, *name;
275 int ret;
276
277 ret = of_property_read_string_index(np,
278 "gate-clock-output-names",
279 i, &name);
280 /* Reached the end of the list? */
281 if (ret < 0)
282 break;
283
284 if (!strcmp(name, "none"))
285 continue;
286
287 switch (i) {
288 case CP110_GATE_AUDIO:
289 case CP110_GATE_COMM_UNIT:
290 case CP110_GATE_EIP150:
291 case CP110_GATE_EIP197:
292 case CP110_GATE_SLOW_IO:
293 of_property_read_string_index(np,
294 "gate-clock-output-names",
295 CP110_GATE_MAIN, &parent);
296 break;
297 case CP110_GATE_NAND:
298 parent = nand_name;
299 break;
300 case CP110_GATE_PPV2:
301 parent = ppv2_name;
302 break;
303 case CP110_GATE_SDIO:
304 of_property_read_string_index(np,
305 "gate-clock-output-names",
306 CP110_GATE_SDMMC, &parent);
307 break;
308 case CP110_GATE_XOR1:
309 case CP110_GATE_XOR0:
310 case CP110_GATE_PCIE_X1_0:
311 case CP110_GATE_PCIE_X1_1:
312 case CP110_GATE_PCIE_X4:
313 of_property_read_string_index(np,
314 "gate-clock-output-names",
315 CP110_GATE_PCIE_XOR, &parent);
316 break;
317 case CP110_GATE_SATA:
318 case CP110_GATE_USB3H0:
319 case CP110_GATE_USB3H1:
320 case CP110_GATE_USB3DEV:
321 of_property_read_string_index(np,
322 "gate-clock-output-names",
323 CP110_GATE_SATA_USB, &parent);
324 break;
325 default:
326 parent = core_name;
327 break;
328 }
329
330 clk = cp110_register_gate(name, parent, regmap, i);
331 if (IS_ERR(clk)) {
332 ret = PTR_ERR(clk);
333 goto fail_gate;
334 }
335
336 cp110_clks[CP110_MAX_CORE_CLOCKS + i] = clk;
337 }
338
339 ret = of_clk_add_provider(np, cp110_of_clk_get, &cp110_clk_data);
340 if (ret)
341 goto fail_clk_add;
342
343 return 0;
344
345fail_clk_add:
346fail_gate:
347 for (i = 0; i < CP110_MAX_GATABLE_CLOCKS; i++) {
348 clk = cp110_clks[CP110_MAX_CORE_CLOCKS + i];
349
350 if (clk)
351 cp110_unregister_gate(clk);
352 }
353
354 clk_unregister_fixed_factor(cp110_clks[CP110_CORE_NAND]);
355fail4:
356 clk_unregister_fixed_factor(cp110_clks[CP110_CORE_CORE]);
357fail3:
358 clk_unregister_fixed_factor(cp110_clks[CP110_CORE_EIP]);
359fail2:
360 clk_unregister_fixed_factor(cp110_clks[CP110_CORE_PPV2]);
361fail1:
362 clk_unregister_fixed_rate(cp110_clks[CP110_CORE_APLL]);
363fail0:
364 return ret;
365}
366
367static int cp110_syscon_clk_remove(struct platform_device *pdev)
368{
369 int i;
370
371 of_clk_del_provider(pdev->dev.of_node);
372
373 for (i = 0; i < CP110_MAX_GATABLE_CLOCKS; i++) {
374 struct clk *clk = cp110_clks[CP110_MAX_CORE_CLOCKS + i];
375
376 if (clk)
377 cp110_unregister_gate(clk);
378 }
379
380 clk_unregister_fixed_factor(cp110_clks[CP110_CORE_NAND]);
381 clk_unregister_fixed_factor(cp110_clks[CP110_CORE_CORE]);
382 clk_unregister_fixed_factor(cp110_clks[CP110_CORE_EIP]);
383 clk_unregister_fixed_factor(cp110_clks[CP110_CORE_PPV2]);
384 clk_unregister_fixed_rate(cp110_clks[CP110_CORE_APLL]);
385
386 return 0;
387}
388
389static const struct of_device_id cp110_syscon_of_match[] = {
390 { .compatible = "marvell,cp110-system-controller0", },
391 { }
392};
393MODULE_DEVICE_TABLE(of, armada8k_pcie_of_match);
394
395static struct platform_driver cp110_syscon_driver = {
396 .probe = cp110_syscon_clk_probe,
397 .remove = cp110_syscon_clk_remove,
398 .driver = {
399 .name = "marvell-cp110-system-controller0",
400 .of_match_table = cp110_syscon_of_match,
401 },
402};
403
404module_platform_driver(cp110_syscon_driver);
405
406MODULE_DESCRIPTION("Marvell CP110 System Controller 0 driver");
407MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
408MODULE_LICENSE("GPL");