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 5 core clocks:
17 * - PPv2 core (1/3 APLL)
21 * - NAND clock, which is either:
22 * - Equal to the core clock
25 * CP110 has 32 gatable clocks, for the various peripherals in the
26 * IP. They have fairly complicated parent/child relationships.
29 #define pr_fmt(fmt) "cp110-system-controller: " fmt
31 #include <linux/clk-provider.h>
32 #include <linux/mfd/syscon.h>
33 #include <linux/init.h>
35 #include <linux/of_address.h>
36 #include <linux/platform_device.h>
37 #include <linux/regmap.h>
38 #include <linux/slab.h>
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)
46 CP110_CLK_TYPE_GATABLE
,
49 #define CP110_MAX_CORE_CLOCKS 5
50 #define CP110_MAX_GATABLE_CLOCKS 32
52 #define CP110_CLK_NUM \
53 (CP110_MAX_CORE_CLOCKS + CP110_MAX_GATABLE_CLOCKS)
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
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
84 struct cp110_gate_clk
{
86 struct regmap
*regmap
;
90 #define to_cp110_gate_clk(hw) container_of(hw, struct cp110_gate_clk, hw)
92 static int cp110_gate_enable(struct clk_hw
*hw
)
94 struct cp110_gate_clk
*gate
= to_cp110_gate_clk(hw
);
96 regmap_update_bits(gate
->regmap
, CP110_PM_CLOCK_GATING_REG
,
97 BIT(gate
->bit_idx
), BIT(gate
->bit_idx
));
102 static void cp110_gate_disable(struct clk_hw
*hw
)
104 struct cp110_gate_clk
*gate
= to_cp110_gate_clk(hw
);
106 regmap_update_bits(gate
->regmap
, CP110_PM_CLOCK_GATING_REG
,
107 BIT(gate
->bit_idx
), 0);
110 static int cp110_gate_is_enabled(struct clk_hw
*hw
)
112 struct cp110_gate_clk
*gate
= to_cp110_gate_clk(hw
);
115 regmap_read(gate
->regmap
, CP110_PM_CLOCK_GATING_REG
, &val
);
117 return val
& BIT(gate
->bit_idx
);
120 static const struct clk_ops cp110_gate_ops
= {
121 .enable
= cp110_gate_enable
,
122 .disable
= cp110_gate_disable
,
123 .is_enabled
= cp110_gate_is_enabled
,
126 static struct clk_hw
*cp110_register_gate(const char *name
,
127 const char *parent_name
,
128 struct regmap
*regmap
, u8 bit_idx
)
130 struct cp110_gate_clk
*gate
;
132 struct clk_init_data init
;
135 gate
= kzalloc(sizeof(*gate
), GFP_KERNEL
);
137 return ERR_PTR(-ENOMEM
);
139 memset(&init
, 0, sizeof(init
));
142 init
.ops
= &cp110_gate_ops
;
143 init
.parent_names
= &parent_name
;
144 init
.num_parents
= 1;
146 gate
->regmap
= regmap
;
147 gate
->bit_idx
= bit_idx
;
148 gate
->hw
.init
= &init
;
151 ret
= clk_hw_register(NULL
, hw
);
160 static void cp110_unregister_gate(struct clk_hw
*hw
)
162 clk_hw_unregister(hw
);
163 kfree(to_cp110_gate_clk(hw
));
166 static struct clk_hw
*cp110_of_clk_get(struct of_phandle_args
*clkspec
,
169 struct clk_hw_onecell_data
*clk_data
= data
;
170 unsigned int type
= clkspec
->args
[0];
171 unsigned int idx
= clkspec
->args
[1];
173 if (type
== CP110_CLK_TYPE_CORE
) {
174 if (idx
> CP110_MAX_CORE_CLOCKS
)
175 return ERR_PTR(-EINVAL
);
176 return clk_data
->hws
[idx
];
177 } else if (type
== CP110_CLK_TYPE_GATABLE
) {
178 if (idx
> CP110_MAX_GATABLE_CLOCKS
)
179 return ERR_PTR(-EINVAL
);
180 return clk_data
->hws
[CP110_MAX_CORE_CLOCKS
+ idx
];
183 return ERR_PTR(-EINVAL
);
186 static int cp110_syscon_clk_probe(struct platform_device
*pdev
)
188 struct regmap
*regmap
;
189 struct device_node
*np
= pdev
->dev
.of_node
;
190 const char *ppv2_name
, *apll_name
, *core_name
, *eip_name
, *nand_name
;
191 struct clk_hw_onecell_data
*cp110_clk_data
;
192 struct clk_hw
*hw
, **cp110_clks
;
196 regmap
= syscon_node_to_regmap(np
);
198 return PTR_ERR(regmap
);
200 ret
= regmap_read(regmap
, CP110_NAND_FLASH_CLK_CTRL_REG
,
205 cp110_clk_data
= devm_kzalloc(&pdev
->dev
, sizeof(*cp110_clk_data
) +
206 sizeof(struct clk_hw
*) * CP110_CLK_NUM
,
211 cp110_clks
= cp110_clk_data
->hws
;
212 cp110_clk_data
->num
= CP110_CLK_NUM
;
214 /* Register the APLL which is the root of the hw tree */
215 of_property_read_string_index(np
, "core-clock-output-names",
216 CP110_CORE_APLL
, &apll_name
);
217 hw
= clk_hw_register_fixed_rate(NULL
, apll_name
, NULL
, 0,
224 cp110_clks
[CP110_CORE_APLL
] = hw
;
227 of_property_read_string_index(np
, "core-clock-output-names",
228 CP110_CORE_PPV2
, &ppv2_name
);
229 hw
= clk_hw_register_fixed_factor(NULL
, ppv2_name
, apll_name
, 0, 1, 3);
235 cp110_clks
[CP110_CORE_PPV2
] = hw
;
237 /* EIP clock is APLL/2 */
238 of_property_read_string_index(np
, "core-clock-output-names",
239 CP110_CORE_EIP
, &eip_name
);
240 hw
= clk_hw_register_fixed_factor(NULL
, eip_name
, apll_name
, 0, 1, 2);
246 cp110_clks
[CP110_CORE_EIP
] = hw
;
248 /* Core clock is EIP/2 */
249 of_property_read_string_index(np
, "core-clock-output-names",
250 CP110_CORE_CORE
, &core_name
);
251 hw
= clk_hw_register_fixed_factor(NULL
, core_name
, eip_name
, 0, 1, 2);
257 cp110_clks
[CP110_CORE_CORE
] = hw
;
259 /* NAND can be either APLL/2.5 or core clock */
260 of_property_read_string_index(np
, "core-clock-output-names",
261 CP110_CORE_NAND
, &nand_name
);
262 if (nand_clk_ctrl
& NF_CLOCK_SEL_400_MASK
)
263 hw
= clk_hw_register_fixed_factor(NULL
, nand_name
,
266 hw
= clk_hw_register_fixed_factor(NULL
, nand_name
,
273 cp110_clks
[CP110_CORE_NAND
] = hw
;
275 for (i
= 0; i
< CP110_MAX_GATABLE_CLOCKS
; i
++) {
276 const char *parent
, *name
;
279 ret
= of_property_read_string_index(np
,
280 "gate-clock-output-names",
282 /* Reached the end of the list? */
286 if (!strcmp(name
, "none"))
290 case CP110_GATE_AUDIO
:
291 case CP110_GATE_COMM_UNIT
:
292 case CP110_GATE_EIP150
:
293 case CP110_GATE_EIP197
:
294 case CP110_GATE_SLOW_IO
:
295 of_property_read_string_index(np
,
296 "gate-clock-output-names",
297 CP110_GATE_MAIN
, &parent
);
299 case CP110_GATE_NAND
:
302 case CP110_GATE_PPV2
:
305 case CP110_GATE_SDIO
:
306 of_property_read_string_index(np
,
307 "gate-clock-output-names",
308 CP110_GATE_SDMMC
, &parent
);
310 case CP110_GATE_XOR1
:
311 case CP110_GATE_XOR0
:
312 case CP110_GATE_PCIE_X1_0
:
313 case CP110_GATE_PCIE_X1_1
:
314 case CP110_GATE_PCIE_X4
:
315 of_property_read_string_index(np
,
316 "gate-clock-output-names",
317 CP110_GATE_PCIE_XOR
, &parent
);
319 case CP110_GATE_SATA
:
320 case CP110_GATE_USB3H0
:
321 case CP110_GATE_USB3H1
:
322 case CP110_GATE_USB3DEV
:
323 of_property_read_string_index(np
,
324 "gate-clock-output-names",
325 CP110_GATE_SATA_USB
, &parent
);
332 hw
= cp110_register_gate(name
, parent
, regmap
, i
);
338 cp110_clks
[CP110_MAX_CORE_CLOCKS
+ i
] = hw
;
341 ret
= of_clk_add_hw_provider(np
, cp110_of_clk_get
, cp110_clk_data
);
345 platform_set_drvdata(pdev
, cp110_clks
);
351 for (i
= 0; i
< CP110_MAX_GATABLE_CLOCKS
; i
++) {
352 hw
= cp110_clks
[CP110_MAX_CORE_CLOCKS
+ i
];
355 cp110_unregister_gate(hw
);
358 clk_hw_unregister_fixed_factor(cp110_clks
[CP110_CORE_NAND
]);
360 clk_hw_unregister_fixed_factor(cp110_clks
[CP110_CORE_CORE
]);
362 clk_hw_unregister_fixed_factor(cp110_clks
[CP110_CORE_EIP
]);
364 clk_hw_unregister_fixed_factor(cp110_clks
[CP110_CORE_PPV2
]);
366 clk_hw_unregister_fixed_rate(cp110_clks
[CP110_CORE_APLL
]);
371 static const struct of_device_id cp110_syscon_of_match
[] = {
372 { .compatible
= "marvell,cp110-system-controller0", },
376 static struct platform_driver cp110_syscon_driver
= {
377 .probe
= cp110_syscon_clk_probe
,
379 .name
= "marvell-cp110-system-controller0",
380 .of_match_table
= cp110_syscon_of_match
,
381 .suppress_bind_attrs
= true,
384 builtin_platform_driver(cp110_syscon_driver
);