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_MG 5
68 #define CP110_GATE_MG_CORE 6
69 #define CP110_GATE_XOR1 7
70 #define CP110_GATE_XOR0 8
71 #define CP110_GATE_GOP_DP 9
72 #define CP110_GATE_PCIE_X1_0 11
73 #define CP110_GATE_PCIE_X1_1 12
74 #define CP110_GATE_PCIE_X4 13
75 #define CP110_GATE_PCIE_XOR 14
76 #define CP110_GATE_SATA 15
77 #define CP110_GATE_SATA_USB 16
78 #define CP110_GATE_MAIN 17
79 #define CP110_GATE_SDMMC_GOP 18
80 #define CP110_GATE_SLOW_IO 21
81 #define CP110_GATE_USB3H0 22
82 #define CP110_GATE_USB3H1 23
83 #define CP110_GATE_USB3DEV 24
84 #define CP110_GATE_EIP150 25
85 #define CP110_GATE_EIP197 26
87 struct cp110_gate_clk
{
89 struct regmap
*regmap
;
93 #define to_cp110_gate_clk(hw) container_of(hw, struct cp110_gate_clk, hw)
95 static int cp110_gate_enable(struct clk_hw
*hw
)
97 struct cp110_gate_clk
*gate
= to_cp110_gate_clk(hw
);
99 regmap_update_bits(gate
->regmap
, CP110_PM_CLOCK_GATING_REG
,
100 BIT(gate
->bit_idx
), BIT(gate
->bit_idx
));
105 static void cp110_gate_disable(struct clk_hw
*hw
)
107 struct cp110_gate_clk
*gate
= to_cp110_gate_clk(hw
);
109 regmap_update_bits(gate
->regmap
, CP110_PM_CLOCK_GATING_REG
,
110 BIT(gate
->bit_idx
), 0);
113 static int cp110_gate_is_enabled(struct clk_hw
*hw
)
115 struct cp110_gate_clk
*gate
= to_cp110_gate_clk(hw
);
118 regmap_read(gate
->regmap
, CP110_PM_CLOCK_GATING_REG
, &val
);
120 return val
& BIT(gate
->bit_idx
);
123 static const struct clk_ops cp110_gate_ops
= {
124 .enable
= cp110_gate_enable
,
125 .disable
= cp110_gate_disable
,
126 .is_enabled
= cp110_gate_is_enabled
,
129 static struct clk_hw
*cp110_register_gate(const char *name
,
130 const char *parent_name
,
131 struct regmap
*regmap
, u8 bit_idx
)
133 struct cp110_gate_clk
*gate
;
135 struct clk_init_data init
;
138 gate
= kzalloc(sizeof(*gate
), GFP_KERNEL
);
140 return ERR_PTR(-ENOMEM
);
142 memset(&init
, 0, sizeof(init
));
145 init
.ops
= &cp110_gate_ops
;
146 init
.parent_names
= &parent_name
;
147 init
.num_parents
= 1;
149 gate
->regmap
= regmap
;
150 gate
->bit_idx
= bit_idx
;
151 gate
->hw
.init
= &init
;
154 ret
= clk_hw_register(NULL
, hw
);
163 static void cp110_unregister_gate(struct clk_hw
*hw
)
165 clk_hw_unregister(hw
);
166 kfree(to_cp110_gate_clk(hw
));
169 static struct clk_hw
*cp110_of_clk_get(struct of_phandle_args
*clkspec
,
172 struct clk_hw_onecell_data
*clk_data
= data
;
173 unsigned int type
= clkspec
->args
[0];
174 unsigned int idx
= clkspec
->args
[1];
176 if (type
== CP110_CLK_TYPE_CORE
) {
177 if (idx
> CP110_MAX_CORE_CLOCKS
)
178 return ERR_PTR(-EINVAL
);
179 return clk_data
->hws
[idx
];
180 } else if (type
== CP110_CLK_TYPE_GATABLE
) {
181 if (idx
> CP110_MAX_GATABLE_CLOCKS
)
182 return ERR_PTR(-EINVAL
);
183 return clk_data
->hws
[CP110_MAX_CORE_CLOCKS
+ idx
];
186 return ERR_PTR(-EINVAL
);
189 static int cp110_syscon_clk_probe(struct platform_device
*pdev
)
191 struct regmap
*regmap
;
192 struct device_node
*np
= pdev
->dev
.of_node
;
193 const char *ppv2_name
, *apll_name
, *core_name
, *eip_name
, *nand_name
;
194 struct clk_hw_onecell_data
*cp110_clk_data
;
195 struct clk_hw
*hw
, **cp110_clks
;
199 regmap
= syscon_node_to_regmap(np
);
201 return PTR_ERR(regmap
);
203 ret
= regmap_read(regmap
, CP110_NAND_FLASH_CLK_CTRL_REG
,
208 cp110_clk_data
= devm_kzalloc(&pdev
->dev
, sizeof(*cp110_clk_data
) +
209 sizeof(struct clk_hw
*) * CP110_CLK_NUM
,
214 cp110_clks
= cp110_clk_data
->hws
;
215 cp110_clk_data
->num
= CP110_CLK_NUM
;
217 /* Register the APLL which is the root of the hw tree */
218 of_property_read_string_index(np
, "core-clock-output-names",
219 CP110_CORE_APLL
, &apll_name
);
220 hw
= clk_hw_register_fixed_rate(NULL
, apll_name
, NULL
, 0,
227 cp110_clks
[CP110_CORE_APLL
] = hw
;
230 of_property_read_string_index(np
, "core-clock-output-names",
231 CP110_CORE_PPV2
, &ppv2_name
);
232 hw
= clk_hw_register_fixed_factor(NULL
, ppv2_name
, apll_name
, 0, 1, 3);
238 cp110_clks
[CP110_CORE_PPV2
] = hw
;
240 /* EIP clock is APLL/2 */
241 of_property_read_string_index(np
, "core-clock-output-names",
242 CP110_CORE_EIP
, &eip_name
);
243 hw
= clk_hw_register_fixed_factor(NULL
, eip_name
, apll_name
, 0, 1, 2);
249 cp110_clks
[CP110_CORE_EIP
] = hw
;
251 /* Core clock is EIP/2 */
252 of_property_read_string_index(np
, "core-clock-output-names",
253 CP110_CORE_CORE
, &core_name
);
254 hw
= clk_hw_register_fixed_factor(NULL
, core_name
, eip_name
, 0, 1, 2);
260 cp110_clks
[CP110_CORE_CORE
] = hw
;
262 /* NAND can be either APLL/2.5 or core clock */
263 of_property_read_string_index(np
, "core-clock-output-names",
264 CP110_CORE_NAND
, &nand_name
);
265 if (nand_clk_ctrl
& NF_CLOCK_SEL_400_MASK
)
266 hw
= clk_hw_register_fixed_factor(NULL
, nand_name
,
269 hw
= clk_hw_register_fixed_factor(NULL
, nand_name
,
276 cp110_clks
[CP110_CORE_NAND
] = hw
;
278 for (i
= 0; i
< CP110_MAX_GATABLE_CLOCKS
; i
++) {
279 const char *parent
, *name
;
282 ret
= of_property_read_string_index(np
,
283 "gate-clock-output-names",
285 /* Reached the end of the list? */
289 if (!strcmp(name
, "none"))
293 case CP110_GATE_AUDIO
:
294 case CP110_GATE_COMM_UNIT
:
295 case CP110_GATE_EIP150
:
296 case CP110_GATE_EIP197
:
297 case CP110_GATE_SLOW_IO
:
298 of_property_read_string_index(np
,
299 "gate-clock-output-names",
300 CP110_GATE_MAIN
, &parent
);
303 of_property_read_string_index(np
,
304 "gate-clock-output-names",
305 CP110_GATE_MG_CORE
, &parent
);
307 case CP110_GATE_NAND
:
310 case CP110_GATE_PPV2
:
313 case CP110_GATE_SDIO
:
314 case CP110_GATE_GOP_DP
:
315 of_property_read_string_index(np
,
316 "gate-clock-output-names",
317 CP110_GATE_SDMMC_GOP
, &parent
);
319 case CP110_GATE_XOR1
:
320 case CP110_GATE_XOR0
:
321 case CP110_GATE_PCIE_X1_0
:
322 case CP110_GATE_PCIE_X1_1
:
323 case CP110_GATE_PCIE_X4
:
324 of_property_read_string_index(np
,
325 "gate-clock-output-names",
326 CP110_GATE_PCIE_XOR
, &parent
);
328 case CP110_GATE_SATA
:
329 case CP110_GATE_USB3H0
:
330 case CP110_GATE_USB3H1
:
331 case CP110_GATE_USB3DEV
:
332 of_property_read_string_index(np
,
333 "gate-clock-output-names",
334 CP110_GATE_SATA_USB
, &parent
);
341 hw
= cp110_register_gate(name
, parent
, regmap
, i
);
347 cp110_clks
[CP110_MAX_CORE_CLOCKS
+ i
] = hw
;
350 ret
= of_clk_add_hw_provider(np
, cp110_of_clk_get
, cp110_clk_data
);
354 platform_set_drvdata(pdev
, cp110_clks
);
360 for (i
= 0; i
< CP110_MAX_GATABLE_CLOCKS
; i
++) {
361 hw
= cp110_clks
[CP110_MAX_CORE_CLOCKS
+ i
];
364 cp110_unregister_gate(hw
);
367 clk_hw_unregister_fixed_factor(cp110_clks
[CP110_CORE_NAND
]);
369 clk_hw_unregister_fixed_factor(cp110_clks
[CP110_CORE_CORE
]);
371 clk_hw_unregister_fixed_factor(cp110_clks
[CP110_CORE_EIP
]);
373 clk_hw_unregister_fixed_factor(cp110_clks
[CP110_CORE_PPV2
]);
375 clk_hw_unregister_fixed_rate(cp110_clks
[CP110_CORE_APLL
]);
380 static const struct of_device_id cp110_syscon_of_match
[] = {
381 { .compatible
= "marvell,cp110-system-controller0", },
385 static struct platform_driver cp110_syscon_driver
= {
386 .probe
= cp110_syscon_clk_probe
,
388 .name
= "marvell-cp110-system-controller0",
389 .of_match_table
= cp110_syscon_of_match
,
390 .suppress_bind_attrs
= true,
393 builtin_platform_driver(cp110_syscon_driver
);