1b7d950b9SSerge Semin // SPDX-License-Identifier: GPL-2.0-only
2b7d950b9SSerge Semin /*
3b7d950b9SSerge Semin * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
4b7d950b9SSerge Semin *
5b7d950b9SSerge Semin * Authors:
6b7d950b9SSerge Semin * Serge Semin <Sergey.Semin@baikalelectronics.ru>
7b7d950b9SSerge Semin * Dmitry Dunaev <dmitry.dunaev@baikalelectronics.ru>
8b7d950b9SSerge Semin *
9b7d950b9SSerge Semin * Baikal-T1 CCU PLL clocks driver
10b7d950b9SSerge Semin */
11b7d950b9SSerge Semin
12b7d950b9SSerge Semin #define pr_fmt(fmt) "bt1-ccu-pll: " fmt
13b7d950b9SSerge Semin
14b7d950b9SSerge Semin #include <linux/kernel.h>
15*c4e05443SSerge Semin #include <linux/platform_device.h>
16b7d950b9SSerge Semin #include <linux/printk.h>
17b7d950b9SSerge Semin #include <linux/slab.h>
18b7d950b9SSerge Semin #include <linux/clk-provider.h>
19b7d950b9SSerge Semin #include <linux/mfd/syscon.h>
20b7d950b9SSerge Semin #include <linux/of.h>
21b7d950b9SSerge Semin #include <linux/of_address.h>
22b7d950b9SSerge Semin #include <linux/ioport.h>
23b7d950b9SSerge Semin #include <linux/regmap.h>
24b7d950b9SSerge Semin
25b7d950b9SSerge Semin #include <dt-bindings/clock/bt1-ccu.h>
26b7d950b9SSerge Semin
27b7d950b9SSerge Semin #include "ccu-pll.h"
28b7d950b9SSerge Semin
29b7d950b9SSerge Semin #define CCU_CPU_PLL_BASE 0x000
30b7d950b9SSerge Semin #define CCU_SATA_PLL_BASE 0x008
31b7d950b9SSerge Semin #define CCU_DDR_PLL_BASE 0x010
32b7d950b9SSerge Semin #define CCU_PCIE_PLL_BASE 0x018
33b7d950b9SSerge Semin #define CCU_ETH_PLL_BASE 0x020
34b7d950b9SSerge Semin
35*c4e05443SSerge Semin #define CCU_PLL_INFO(_id, _name, _pname, _base, _flags, _features) \
36b7d950b9SSerge Semin { \
37b7d950b9SSerge Semin .id = _id, \
38b7d950b9SSerge Semin .name = _name, \
39b7d950b9SSerge Semin .parent_name = _pname, \
40b7d950b9SSerge Semin .base = _base, \
41*c4e05443SSerge Semin .flags = _flags, \
42*c4e05443SSerge Semin .features = _features, \
43b7d950b9SSerge Semin }
44b7d950b9SSerge Semin
45b7d950b9SSerge Semin #define CCU_PLL_NUM ARRAY_SIZE(pll_info)
46b7d950b9SSerge Semin
47b7d950b9SSerge Semin struct ccu_pll_info {
48b7d950b9SSerge Semin unsigned int id;
49b7d950b9SSerge Semin const char *name;
50b7d950b9SSerge Semin const char *parent_name;
51b7d950b9SSerge Semin unsigned int base;
52b7d950b9SSerge Semin unsigned long flags;
53*c4e05443SSerge Semin unsigned long features;
54b7d950b9SSerge Semin };
55b7d950b9SSerge Semin
56b7d950b9SSerge Semin /*
579ba9ad8fSSerge Semin * Alas we have to mark all PLLs as critical. CPU and DDR PLLs are sources of
589ba9ad8fSSerge Semin * CPU cores and DDR controller reference clocks, due to which they obviously
599ba9ad8fSSerge Semin * shouldn't be ever gated. SATA and PCIe PLLs are the parents of APB-bus and
609ba9ad8fSSerge Semin * DDR controller AXI-bus clocks. If they are gated the system will be
619ba9ad8fSSerge Semin * unusable. Moreover disabling SATA and Ethernet PLLs causes automatic reset
629ba9ad8fSSerge Semin * of the corresponding subsystems. So until we aren't ready to re-initialize
639ba9ad8fSSerge Semin * all the devices consuming those PLLs, they will be marked as critical too.
64b7d950b9SSerge Semin */
65b7d950b9SSerge Semin static const struct ccu_pll_info pll_info[] = {
66b7d950b9SSerge Semin CCU_PLL_INFO(CCU_CPU_PLL, "cpu_pll", "ref_clk", CCU_CPU_PLL_BASE,
67*c4e05443SSerge Semin CLK_IS_CRITICAL, CCU_PLL_BASIC),
68b7d950b9SSerge Semin CCU_PLL_INFO(CCU_SATA_PLL, "sata_pll", "ref_clk", CCU_SATA_PLL_BASE,
69*c4e05443SSerge Semin CLK_IS_CRITICAL | CLK_SET_RATE_GATE, 0),
70b7d950b9SSerge Semin CCU_PLL_INFO(CCU_DDR_PLL, "ddr_pll", "ref_clk", CCU_DDR_PLL_BASE,
71*c4e05443SSerge Semin CLK_IS_CRITICAL | CLK_SET_RATE_GATE, 0),
72b7d950b9SSerge Semin CCU_PLL_INFO(CCU_PCIE_PLL, "pcie_pll", "ref_clk", CCU_PCIE_PLL_BASE,
73*c4e05443SSerge Semin CLK_IS_CRITICAL, CCU_PLL_BASIC),
74b7d950b9SSerge Semin CCU_PLL_INFO(CCU_ETH_PLL, "eth_pll", "ref_clk", CCU_ETH_PLL_BASE,
75*c4e05443SSerge Semin CLK_IS_CRITICAL | CLK_SET_RATE_GATE, 0)
76b7d950b9SSerge Semin };
77b7d950b9SSerge Semin
78b7d950b9SSerge Semin struct ccu_pll_data {
79b7d950b9SSerge Semin struct device_node *np;
80b7d950b9SSerge Semin struct regmap *sys_regs;
81b7d950b9SSerge Semin struct ccu_pll *plls[CCU_PLL_NUM];
82b7d950b9SSerge Semin };
83b7d950b9SSerge Semin
84*c4e05443SSerge Semin static struct ccu_pll_data *pll_data;
85*c4e05443SSerge Semin
ccu_pll_find_desc(struct ccu_pll_data * data,unsigned int clk_id)86b7d950b9SSerge Semin static struct ccu_pll *ccu_pll_find_desc(struct ccu_pll_data *data,
87b7d950b9SSerge Semin unsigned int clk_id)
88b7d950b9SSerge Semin {
89b7d950b9SSerge Semin int idx;
90b7d950b9SSerge Semin
91b7d950b9SSerge Semin for (idx = 0; idx < CCU_PLL_NUM; ++idx) {
92*c4e05443SSerge Semin if (pll_info[idx].id == clk_id)
93*c4e05443SSerge Semin return data->plls[idx];
94b7d950b9SSerge Semin }
95b7d950b9SSerge Semin
96b7d950b9SSerge Semin return ERR_PTR(-EINVAL);
97b7d950b9SSerge Semin }
98b7d950b9SSerge Semin
ccu_pll_create_data(struct device_node * np)99b7d950b9SSerge Semin static struct ccu_pll_data *ccu_pll_create_data(struct device_node *np)
100b7d950b9SSerge Semin {
101b7d950b9SSerge Semin struct ccu_pll_data *data;
102b7d950b9SSerge Semin
103b7d950b9SSerge Semin data = kzalloc(sizeof(*data), GFP_KERNEL);
104b7d950b9SSerge Semin if (!data)
105b7d950b9SSerge Semin return ERR_PTR(-ENOMEM);
106b7d950b9SSerge Semin
107b7d950b9SSerge Semin data->np = np;
108b7d950b9SSerge Semin
109b7d950b9SSerge Semin return data;
110b7d950b9SSerge Semin }
111b7d950b9SSerge Semin
ccu_pll_free_data(struct ccu_pll_data * data)112b7d950b9SSerge Semin static void ccu_pll_free_data(struct ccu_pll_data *data)
113b7d950b9SSerge Semin {
114b7d950b9SSerge Semin kfree(data);
115b7d950b9SSerge Semin }
116b7d950b9SSerge Semin
ccu_pll_find_sys_regs(struct ccu_pll_data * data)117b7d950b9SSerge Semin static int ccu_pll_find_sys_regs(struct ccu_pll_data *data)
118b7d950b9SSerge Semin {
119b7d950b9SSerge Semin data->sys_regs = syscon_node_to_regmap(data->np->parent);
120b7d950b9SSerge Semin if (IS_ERR(data->sys_regs)) {
121b7d950b9SSerge Semin pr_err("Failed to find syscon regs for '%s'\n",
122b7d950b9SSerge Semin of_node_full_name(data->np));
123b7d950b9SSerge Semin return PTR_ERR(data->sys_regs);
124b7d950b9SSerge Semin }
125b7d950b9SSerge Semin
126b7d950b9SSerge Semin return 0;
127b7d950b9SSerge Semin }
128b7d950b9SSerge Semin
ccu_pll_of_clk_hw_get(struct of_phandle_args * clkspec,void * priv)129b7d950b9SSerge Semin static struct clk_hw *ccu_pll_of_clk_hw_get(struct of_phandle_args *clkspec,
130b7d950b9SSerge Semin void *priv)
131b7d950b9SSerge Semin {
132b7d950b9SSerge Semin struct ccu_pll_data *data = priv;
133b7d950b9SSerge Semin struct ccu_pll *pll;
134b7d950b9SSerge Semin unsigned int clk_id;
135b7d950b9SSerge Semin
136b7d950b9SSerge Semin clk_id = clkspec->args[0];
137b7d950b9SSerge Semin pll = ccu_pll_find_desc(data, clk_id);
138b7d950b9SSerge Semin if (IS_ERR(pll)) {
139*c4e05443SSerge Semin if (pll != ERR_PTR(-EPROBE_DEFER))
140b7d950b9SSerge Semin pr_info("Invalid PLL clock ID %d specified\n", clk_id);
141*c4e05443SSerge Semin
142b7d950b9SSerge Semin return ERR_CAST(pll);
143b7d950b9SSerge Semin }
144b7d950b9SSerge Semin
145b7d950b9SSerge Semin return ccu_pll_get_clk_hw(pll);
146b7d950b9SSerge Semin }
147b7d950b9SSerge Semin
ccu_pll_clk_register(struct ccu_pll_data * data,bool defer)148*c4e05443SSerge Semin static int ccu_pll_clk_register(struct ccu_pll_data *data, bool defer)
149b7d950b9SSerge Semin {
150b7d950b9SSerge Semin int idx, ret;
151b7d950b9SSerge Semin
152b7d950b9SSerge Semin for (idx = 0; idx < CCU_PLL_NUM; ++idx) {
153b7d950b9SSerge Semin const struct ccu_pll_info *info = &pll_info[idx];
154b7d950b9SSerge Semin struct ccu_pll_init_data init = {0};
155b7d950b9SSerge Semin
156*c4e05443SSerge Semin /* Defer non-basic PLLs allocation for the probe stage */
157*c4e05443SSerge Semin if (!!(info->features & CCU_PLL_BASIC) ^ defer) {
158*c4e05443SSerge Semin if (!data->plls[idx])
159*c4e05443SSerge Semin data->plls[idx] = ERR_PTR(-EPROBE_DEFER);
160*c4e05443SSerge Semin
161*c4e05443SSerge Semin continue;
162*c4e05443SSerge Semin }
163*c4e05443SSerge Semin
164b7d950b9SSerge Semin init.id = info->id;
165b7d950b9SSerge Semin init.name = info->name;
166b7d950b9SSerge Semin init.parent_name = info->parent_name;
167b7d950b9SSerge Semin init.base = info->base;
168b7d950b9SSerge Semin init.sys_regs = data->sys_regs;
169b7d950b9SSerge Semin init.np = data->np;
170b7d950b9SSerge Semin init.flags = info->flags;
171*c4e05443SSerge Semin init.features = info->features;
172b7d950b9SSerge Semin
173b7d950b9SSerge Semin data->plls[idx] = ccu_pll_hw_register(&init);
174b7d950b9SSerge Semin if (IS_ERR(data->plls[idx])) {
175b7d950b9SSerge Semin ret = PTR_ERR(data->plls[idx]);
176b7d950b9SSerge Semin pr_err("Couldn't register PLL hw '%s'\n",
177b7d950b9SSerge Semin init.name);
178b7d950b9SSerge Semin goto err_hw_unregister;
179b7d950b9SSerge Semin }
180b7d950b9SSerge Semin }
181b7d950b9SSerge Semin
182*c4e05443SSerge Semin return 0;
183*c4e05443SSerge Semin
184*c4e05443SSerge Semin err_hw_unregister:
185*c4e05443SSerge Semin for (--idx; idx >= 0; --idx) {
186*c4e05443SSerge Semin if (!!(pll_info[idx].features & CCU_PLL_BASIC) ^ defer)
187*c4e05443SSerge Semin continue;
188*c4e05443SSerge Semin
189*c4e05443SSerge Semin ccu_pll_hw_unregister(data->plls[idx]);
190*c4e05443SSerge Semin }
191*c4e05443SSerge Semin
192*c4e05443SSerge Semin return ret;
193*c4e05443SSerge Semin }
194*c4e05443SSerge Semin
ccu_pll_clk_unregister(struct ccu_pll_data * data,bool defer)195*c4e05443SSerge Semin static void ccu_pll_clk_unregister(struct ccu_pll_data *data, bool defer)
196*c4e05443SSerge Semin {
197*c4e05443SSerge Semin int idx;
198*c4e05443SSerge Semin
199*c4e05443SSerge Semin /* Uninstall only the clocks registered on the specfied stage */
200*c4e05443SSerge Semin for (idx = 0; idx < CCU_PLL_NUM; ++idx) {
201*c4e05443SSerge Semin if (!!(pll_info[idx].features & CCU_PLL_BASIC) ^ defer)
202*c4e05443SSerge Semin continue;
203*c4e05443SSerge Semin
204*c4e05443SSerge Semin ccu_pll_hw_unregister(data->plls[idx]);
205*c4e05443SSerge Semin }
206*c4e05443SSerge Semin }
207*c4e05443SSerge Semin
ccu_pll_of_register(struct ccu_pll_data * data)208*c4e05443SSerge Semin static int ccu_pll_of_register(struct ccu_pll_data *data)
209*c4e05443SSerge Semin {
210*c4e05443SSerge Semin int ret;
211*c4e05443SSerge Semin
212b7d950b9SSerge Semin ret = of_clk_add_hw_provider(data->np, ccu_pll_of_clk_hw_get, data);
213b7d950b9SSerge Semin if (ret) {
214b7d950b9SSerge Semin pr_err("Couldn't register PLL provider of '%s'\n",
215b7d950b9SSerge Semin of_node_full_name(data->np));
216b7d950b9SSerge Semin }
217b7d950b9SSerge Semin
218b7d950b9SSerge Semin return ret;
219b7d950b9SSerge Semin }
220b7d950b9SSerge Semin
ccu_pll_probe(struct platform_device * pdev)221*c4e05443SSerge Semin static int ccu_pll_probe(struct platform_device *pdev)
222*c4e05443SSerge Semin {
223*c4e05443SSerge Semin struct ccu_pll_data *data = pll_data;
224*c4e05443SSerge Semin
225*c4e05443SSerge Semin if (!data)
226*c4e05443SSerge Semin return -EINVAL;
227*c4e05443SSerge Semin
228*c4e05443SSerge Semin return ccu_pll_clk_register(data, false);
229*c4e05443SSerge Semin }
230*c4e05443SSerge Semin
231*c4e05443SSerge Semin static const struct of_device_id ccu_pll_of_match[] = {
232*c4e05443SSerge Semin { .compatible = "baikal,bt1-ccu-pll" },
233*c4e05443SSerge Semin { }
234*c4e05443SSerge Semin };
235*c4e05443SSerge Semin
236*c4e05443SSerge Semin static struct platform_driver ccu_pll_driver = {
237*c4e05443SSerge Semin .probe = ccu_pll_probe,
238*c4e05443SSerge Semin .driver = {
239*c4e05443SSerge Semin .name = "clk-ccu-pll",
240*c4e05443SSerge Semin .of_match_table = ccu_pll_of_match,
241*c4e05443SSerge Semin .suppress_bind_attrs = true,
242*c4e05443SSerge Semin },
243*c4e05443SSerge Semin };
244*c4e05443SSerge Semin builtin_platform_driver(ccu_pll_driver);
245*c4e05443SSerge Semin
ccu_pll_init(struct device_node * np)246b7d950b9SSerge Semin static __init void ccu_pll_init(struct device_node *np)
247b7d950b9SSerge Semin {
248b7d950b9SSerge Semin struct ccu_pll_data *data;
249b7d950b9SSerge Semin int ret;
250b7d950b9SSerge Semin
251b7d950b9SSerge Semin data = ccu_pll_create_data(np);
252b7d950b9SSerge Semin if (IS_ERR(data))
253b7d950b9SSerge Semin return;
254b7d950b9SSerge Semin
255b7d950b9SSerge Semin ret = ccu_pll_find_sys_regs(data);
256b7d950b9SSerge Semin if (ret)
257b7d950b9SSerge Semin goto err_free_data;
258b7d950b9SSerge Semin
259*c4e05443SSerge Semin ret = ccu_pll_clk_register(data, true);
260b7d950b9SSerge Semin if (ret)
261b7d950b9SSerge Semin goto err_free_data;
262b7d950b9SSerge Semin
263*c4e05443SSerge Semin ret = ccu_pll_of_register(data);
264*c4e05443SSerge Semin if (ret)
265*c4e05443SSerge Semin goto err_clk_unregister;
266*c4e05443SSerge Semin
267*c4e05443SSerge Semin pll_data = data;
268*c4e05443SSerge Semin
269b7d950b9SSerge Semin return;
270b7d950b9SSerge Semin
271*c4e05443SSerge Semin err_clk_unregister:
272*c4e05443SSerge Semin ccu_pll_clk_unregister(data, true);
273*c4e05443SSerge Semin
274b7d950b9SSerge Semin err_free_data:
275b7d950b9SSerge Semin ccu_pll_free_data(data);
276b7d950b9SSerge Semin }
277*c4e05443SSerge Semin CLK_OF_DECLARE_DRIVER(ccu_pll, "baikal,bt1-ccu-pll", ccu_pll_init);
278