1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
6 * Serge Semin <Sergey.Semin@baikalelectronics.ru>
7 * Dmitry Dunaev <dmitry.dunaev@baikalelectronics.ru>
9 * Baikal-T1 CCU PLL clocks driver
12 #define pr_fmt(fmt) "bt1-ccu-pll: " fmt
14 #include <linux/kernel.h>
15 #include <linux/printk.h>
16 #include <linux/slab.h>
17 #include <linux/clk-provider.h>
18 #include <linux/mfd/syscon.h>
20 #include <linux/of_address.h>
21 #include <linux/ioport.h>
22 #include <linux/regmap.h>
24 #include <dt-bindings/clock/bt1-ccu.h>
28 #define CCU_CPU_PLL_BASE 0x000
29 #define CCU_SATA_PLL_BASE 0x008
30 #define CCU_DDR_PLL_BASE 0x010
31 #define CCU_PCIE_PLL_BASE 0x018
32 #define CCU_ETH_PLL_BASE 0x020
34 #define CCU_PLL_INFO(_id, _name, _pname, _base, _flags) \
38 .parent_name = _pname, \
43 #define CCU_PLL_NUM ARRAY_SIZE(pll_info)
48 const char *parent_name
;
54 * Alas we have to mark all PLLs as critical. CPU and DDR PLLs are sources of
55 * CPU cores and DDR controller reference clocks, due to which they obviously
56 * shouldn't be ever gated. SATA and PCIe PLLs are the parents of APB-bus and
57 * DDR controller AXI-bus clocks. If they are gated the system will be
58 * unusable. Moreover disabling SATA and Ethernet PLLs causes automatic reset
59 * of the corresponding subsystems. So until we aren't ready to re-initialize
60 * all the devices consuming those PLLs, they will be marked as critical too.
62 static const struct ccu_pll_info pll_info
[] = {
63 CCU_PLL_INFO(CCU_CPU_PLL
, "cpu_pll", "ref_clk", CCU_CPU_PLL_BASE
,
65 CCU_PLL_INFO(CCU_SATA_PLL
, "sata_pll", "ref_clk", CCU_SATA_PLL_BASE
,
66 CLK_IS_CRITICAL
| CLK_SET_RATE_GATE
),
67 CCU_PLL_INFO(CCU_DDR_PLL
, "ddr_pll", "ref_clk", CCU_DDR_PLL_BASE
,
68 CLK_IS_CRITICAL
| CLK_SET_RATE_GATE
),
69 CCU_PLL_INFO(CCU_PCIE_PLL
, "pcie_pll", "ref_clk", CCU_PCIE_PLL_BASE
,
71 CCU_PLL_INFO(CCU_ETH_PLL
, "eth_pll", "ref_clk", CCU_ETH_PLL_BASE
,
72 CLK_IS_CRITICAL
| CLK_SET_RATE_GATE
)
76 struct device_node
*np
;
77 struct regmap
*sys_regs
;
78 struct ccu_pll
*plls
[CCU_PLL_NUM
];
81 static struct ccu_pll
*ccu_pll_find_desc(struct ccu_pll_data
*data
,
87 for (idx
= 0; idx
< CCU_PLL_NUM
; ++idx
) {
88 pll
= data
->plls
[idx
];
89 if (pll
&& pll
->id
== clk_id
)
93 return ERR_PTR(-EINVAL
);
96 static struct ccu_pll_data
*ccu_pll_create_data(struct device_node
*np
)
98 struct ccu_pll_data
*data
;
100 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
102 return ERR_PTR(-ENOMEM
);
109 static void ccu_pll_free_data(struct ccu_pll_data
*data
)
114 static int ccu_pll_find_sys_regs(struct ccu_pll_data
*data
)
116 data
->sys_regs
= syscon_node_to_regmap(data
->np
->parent
);
117 if (IS_ERR(data
->sys_regs
)) {
118 pr_err("Failed to find syscon regs for '%s'\n",
119 of_node_full_name(data
->np
));
120 return PTR_ERR(data
->sys_regs
);
126 static struct clk_hw
*ccu_pll_of_clk_hw_get(struct of_phandle_args
*clkspec
,
129 struct ccu_pll_data
*data
= priv
;
133 clk_id
= clkspec
->args
[0];
134 pll
= ccu_pll_find_desc(data
, clk_id
);
136 pr_info("Invalid PLL clock ID %d specified\n", clk_id
);
137 return ERR_CAST(pll
);
140 return ccu_pll_get_clk_hw(pll
);
143 static int ccu_pll_clk_register(struct ccu_pll_data
*data
)
147 for (idx
= 0; idx
< CCU_PLL_NUM
; ++idx
) {
148 const struct ccu_pll_info
*info
= &pll_info
[idx
];
149 struct ccu_pll_init_data init
= {0};
152 init
.name
= info
->name
;
153 init
.parent_name
= info
->parent_name
;
154 init
.base
= info
->base
;
155 init
.sys_regs
= data
->sys_regs
;
157 init
.flags
= info
->flags
;
159 data
->plls
[idx
] = ccu_pll_hw_register(&init
);
160 if (IS_ERR(data
->plls
[idx
])) {
161 ret
= PTR_ERR(data
->plls
[idx
]);
162 pr_err("Couldn't register PLL hw '%s'\n",
164 goto err_hw_unregister
;
168 ret
= of_clk_add_hw_provider(data
->np
, ccu_pll_of_clk_hw_get
, data
);
170 pr_err("Couldn't register PLL provider of '%s'\n",
171 of_node_full_name(data
->np
));
172 goto err_hw_unregister
;
178 for (--idx
; idx
>= 0; --idx
)
179 ccu_pll_hw_unregister(data
->plls
[idx
]);
184 static __init
void ccu_pll_init(struct device_node
*np
)
186 struct ccu_pll_data
*data
;
189 data
= ccu_pll_create_data(np
);
193 ret
= ccu_pll_find_sys_regs(data
);
197 ret
= ccu_pll_clk_register(data
);
204 ccu_pll_free_data(data
);
206 CLK_OF_DECLARE(ccu_pll
, "baikal,bt1-ccu-pll", ccu_pll_init
);