xref: /linux/drivers/clk/samsung/clk-exynos-arm64.c (revision 619b92b9c8fe5369503ae948ad4e0a9c195c2c4a)
1cfe238e4SDavid Virag // SPDX-License-Identifier: GPL-2.0-only
2cfe238e4SDavid Virag /*
3cfe238e4SDavid Virag  * Copyright (C) 2021 Linaro Ltd.
4cfe238e4SDavid Virag  * Copyright (C) 2021 Dávid Virág <virag.david003@gmail.com>
5cfe238e4SDavid Virag  * Author: Sam Protsenko <semen.protsenko@linaro.org>
6cfe238e4SDavid Virag  * Author: Dávid Virág <virag.david003@gmail.com>
7cfe238e4SDavid Virag  *
8cfe238e4SDavid Virag  * This file contains shared functions used by some arm64 Exynos SoCs,
9cfe238e4SDavid Virag  * such as Exynos7885 or Exynos850 to register and init CMUs.
10cfe238e4SDavid Virag  */
11cfe238e4SDavid Virag #include <linux/clk.h>
12cfe238e4SDavid Virag #include <linux/of_address.h>
13a96cbb14SRob Herring #include <linux/of.h>
14a96cbb14SRob Herring #include <linux/platform_device.h>
15f05dc202SSam Protsenko #include <linux/pm_runtime.h>
16f05dc202SSam Protsenko #include <linux/slab.h>
17cfe238e4SDavid Virag 
18cfe238e4SDavid Virag #include "clk-exynos-arm64.h"
19cfe238e4SDavid Virag 
20*7fa37084SSam Protsenko /* PLL register bits */
21*7fa37084SSam Protsenko #define PLL_CON1_MANUAL		BIT(1)
22*7fa37084SSam Protsenko 
23cfe238e4SDavid Virag /* Gate register bits */
24cfe238e4SDavid Virag #define GATE_MANUAL		BIT(20)
25cfe238e4SDavid Virag #define GATE_ENABLE_HWACG	BIT(28)
26cfe238e4SDavid Virag 
27*7fa37084SSam Protsenko /* PLL_CONx_PLL register offsets range */
28*7fa37084SSam Protsenko #define PLL_CON_OFF_START	0x100
29*7fa37084SSam Protsenko #define PLL_CON_OFF_END		0x600
30*7fa37084SSam Protsenko 
31cfe238e4SDavid Virag /* Gate register offsets range */
32cfe238e4SDavid Virag #define GATE_OFF_START		0x2000
33cfe238e4SDavid Virag #define GATE_OFF_END		0x2fff
34cfe238e4SDavid Virag 
35f05dc202SSam Protsenko struct exynos_arm64_cmu_data {
36f05dc202SSam Protsenko 	struct samsung_clk_reg_dump *clk_save;
37f05dc202SSam Protsenko 	unsigned int nr_clk_save;
38f05dc202SSam Protsenko 	const struct samsung_clk_reg_dump *clk_suspend;
39f05dc202SSam Protsenko 	unsigned int nr_clk_suspend;
40f05dc202SSam Protsenko 
41f05dc202SSam Protsenko 	struct clk *clk;
42f05dc202SSam Protsenko 	struct clk **pclks;
43f05dc202SSam Protsenko 	int nr_pclks;
44f05dc202SSam Protsenko 
45f05dc202SSam Protsenko 	struct samsung_clk_provider *ctx;
46f05dc202SSam Protsenko };
47f05dc202SSam Protsenko 
48*7fa37084SSam Protsenko /* Check if the register offset is a GATE register */
is_gate_reg(unsigned long off)49*7fa37084SSam Protsenko static bool is_gate_reg(unsigned long off)
50*7fa37084SSam Protsenko {
51*7fa37084SSam Protsenko 	return off >= GATE_OFF_START && off <= GATE_OFF_END;
52*7fa37084SSam Protsenko }
53*7fa37084SSam Protsenko 
54*7fa37084SSam Protsenko /* Check if the register offset is a PLL_CONx register */
is_pll_conx_reg(unsigned long off)55*7fa37084SSam Protsenko static bool is_pll_conx_reg(unsigned long off)
56*7fa37084SSam Protsenko {
57*7fa37084SSam Protsenko 	return off >= PLL_CON_OFF_START && off <= PLL_CON_OFF_END;
58*7fa37084SSam Protsenko }
59*7fa37084SSam Protsenko 
60*7fa37084SSam Protsenko /* Check if the register offset is a PLL_CON1 register */
is_pll_con1_reg(unsigned long off)61*7fa37084SSam Protsenko static bool is_pll_con1_reg(unsigned long off)
62*7fa37084SSam Protsenko {
63*7fa37084SSam Protsenko 	return is_pll_conx_reg(off) && (off & 0xf) == 0x4 && !(off & 0x10);
64*7fa37084SSam Protsenko }
65*7fa37084SSam Protsenko 
66cfe238e4SDavid Virag /**
67cfe238e4SDavid Virag  * exynos_arm64_init_clocks - Set clocks initial configuration
68cfe238e4SDavid Virag  * @np:		CMU device tree node with "reg" property (CMU addr)
69*7fa37084SSam Protsenko  * @cmu:	CMU data
70cfe238e4SDavid Virag  *
71*7fa37084SSam Protsenko  * Set manual control mode for all gate and PLL clocks.
72cfe238e4SDavid Virag  */
exynos_arm64_init_clocks(struct device_node * np,const struct samsung_cmu_info * cmu)73cfe238e4SDavid Virag static void __init exynos_arm64_init_clocks(struct device_node *np,
74*7fa37084SSam Protsenko 					    const struct samsung_cmu_info *cmu)
75cfe238e4SDavid Virag {
76*7fa37084SSam Protsenko 	const unsigned long *reg_offs = cmu->clk_regs;
77*7fa37084SSam Protsenko 	size_t reg_offs_len = cmu->nr_clk_regs;
78cfe238e4SDavid Virag 	void __iomem *reg_base;
79cfe238e4SDavid Virag 	size_t i;
80cfe238e4SDavid Virag 
81cfe238e4SDavid Virag 	reg_base = of_iomap(np, 0);
82cfe238e4SDavid Virag 	if (!reg_base)
83cfe238e4SDavid Virag 		panic("%s: failed to map registers\n", __func__);
84cfe238e4SDavid Virag 
85cfe238e4SDavid Virag 	for (i = 0; i < reg_offs_len; ++i) {
86cfe238e4SDavid Virag 		void __iomem *reg = reg_base + reg_offs[i];
87cfe238e4SDavid Virag 		u32 val;
88cfe238e4SDavid Virag 
89*7fa37084SSam Protsenko 		if (cmu->manual_plls && is_pll_con1_reg(reg_offs[i])) {
90*7fa37084SSam Protsenko 			writel(PLL_CON1_MANUAL, reg);
91*7fa37084SSam Protsenko 		} else if (is_gate_reg(reg_offs[i])) {
92cfe238e4SDavid Virag 			val = readl(reg);
93cfe238e4SDavid Virag 			val |= GATE_MANUAL;
94cfe238e4SDavid Virag 			val &= ~GATE_ENABLE_HWACG;
95cfe238e4SDavid Virag 			writel(val, reg);
96cfe238e4SDavid Virag 		}
97*7fa37084SSam Protsenko 	}
98cfe238e4SDavid Virag 
99cfe238e4SDavid Virag 	iounmap(reg_base);
100cfe238e4SDavid Virag }
101cfe238e4SDavid Virag 
102cfe238e4SDavid Virag /**
103454e8d29SSam Protsenko  * exynos_arm64_enable_bus_clk - Enable parent clock of specified CMU
104454e8d29SSam Protsenko  *
105454e8d29SSam Protsenko  * @dev:	Device object; may be NULL if this function is not being
106454e8d29SSam Protsenko  *		called from platform driver probe function
107454e8d29SSam Protsenko  * @np:		CMU device tree node
108454e8d29SSam Protsenko  * @cmu:	CMU data
109454e8d29SSam Protsenko  *
110454e8d29SSam Protsenko  * Keep CMU parent clock running (needed for CMU registers access).
111454e8d29SSam Protsenko  *
112454e8d29SSam Protsenko  * Return: 0 on success or a negative error code on failure.
113454e8d29SSam Protsenko  */
exynos_arm64_enable_bus_clk(struct device * dev,struct device_node * np,const struct samsung_cmu_info * cmu)114454e8d29SSam Protsenko static int __init exynos_arm64_enable_bus_clk(struct device *dev,
115454e8d29SSam Protsenko 		struct device_node *np, const struct samsung_cmu_info *cmu)
116454e8d29SSam Protsenko {
117454e8d29SSam Protsenko 	struct clk *parent_clk;
118454e8d29SSam Protsenko 
119454e8d29SSam Protsenko 	if (!cmu->clk_name)
120454e8d29SSam Protsenko 		return 0;
121454e8d29SSam Protsenko 
122f05dc202SSam Protsenko 	if (dev) {
123f05dc202SSam Protsenko 		struct exynos_arm64_cmu_data *data;
124f05dc202SSam Protsenko 
125454e8d29SSam Protsenko 		parent_clk = clk_get(dev, cmu->clk_name);
126f05dc202SSam Protsenko 		data = dev_get_drvdata(dev);
127f05dc202SSam Protsenko 		if (data)
128f05dc202SSam Protsenko 			data->clk = parent_clk;
129f05dc202SSam Protsenko 	} else {
130454e8d29SSam Protsenko 		parent_clk = of_clk_get_by_name(np, cmu->clk_name);
131f05dc202SSam Protsenko 	}
132454e8d29SSam Protsenko 
133454e8d29SSam Protsenko 	if (IS_ERR(parent_clk))
134454e8d29SSam Protsenko 		return PTR_ERR(parent_clk);
135454e8d29SSam Protsenko 
136454e8d29SSam Protsenko 	return clk_prepare_enable(parent_clk);
137454e8d29SSam Protsenko }
138454e8d29SSam Protsenko 
exynos_arm64_cmu_prepare_pm(struct device * dev,const struct samsung_cmu_info * cmu)139f05dc202SSam Protsenko static int __init exynos_arm64_cmu_prepare_pm(struct device *dev,
140f05dc202SSam Protsenko 		const struct samsung_cmu_info *cmu)
141f05dc202SSam Protsenko {
142f05dc202SSam Protsenko 	struct exynos_arm64_cmu_data *data = dev_get_drvdata(dev);
143f05dc202SSam Protsenko 	int i;
144f05dc202SSam Protsenko 
145f05dc202SSam Protsenko 	data->clk_save = samsung_clk_alloc_reg_dump(cmu->clk_regs,
146f05dc202SSam Protsenko 						    cmu->nr_clk_regs);
147f05dc202SSam Protsenko 	if (!data->clk_save)
148f05dc202SSam Protsenko 		return -ENOMEM;
149f05dc202SSam Protsenko 
150f05dc202SSam Protsenko 	data->nr_clk_save = cmu->nr_clk_regs;
151f05dc202SSam Protsenko 	data->clk_suspend = cmu->suspend_regs;
152f05dc202SSam Protsenko 	data->nr_clk_suspend = cmu->nr_suspend_regs;
153f05dc202SSam Protsenko 	data->nr_pclks = of_clk_get_parent_count(dev->of_node);
154f05dc202SSam Protsenko 	if (!data->nr_pclks)
155f05dc202SSam Protsenko 		return 0;
156f05dc202SSam Protsenko 
157f05dc202SSam Protsenko 	data->pclks = devm_kcalloc(dev, sizeof(struct clk *), data->nr_pclks,
158f05dc202SSam Protsenko 				   GFP_KERNEL);
159f05dc202SSam Protsenko 	if (!data->pclks) {
160f05dc202SSam Protsenko 		kfree(data->clk_save);
161f05dc202SSam Protsenko 		return -ENOMEM;
162f05dc202SSam Protsenko 	}
163f05dc202SSam Protsenko 
164f05dc202SSam Protsenko 	for (i = 0; i < data->nr_pclks; i++) {
165f05dc202SSam Protsenko 		struct clk *clk = of_clk_get(dev->of_node, i);
166f05dc202SSam Protsenko 
167f05dc202SSam Protsenko 		if (IS_ERR(clk)) {
168f05dc202SSam Protsenko 			kfree(data->clk_save);
169f05dc202SSam Protsenko 			while (--i >= 0)
170f05dc202SSam Protsenko 				clk_put(data->pclks[i]);
171f05dc202SSam Protsenko 			return PTR_ERR(clk);
172f05dc202SSam Protsenko 		}
173f05dc202SSam Protsenko 		data->pclks[i] = clk;
174f05dc202SSam Protsenko 	}
175f05dc202SSam Protsenko 
176f05dc202SSam Protsenko 	return 0;
177f05dc202SSam Protsenko }
178f05dc202SSam Protsenko 
179454e8d29SSam Protsenko /**
180cfe238e4SDavid Virag  * exynos_arm64_register_cmu - Register specified Exynos CMU domain
181cfe238e4SDavid Virag  * @dev:	Device object; may be NULL if this function is not being
182cfe238e4SDavid Virag  *		called from platform driver probe function
183cfe238e4SDavid Virag  * @np:		CMU device tree node
184cfe238e4SDavid Virag  * @cmu:	CMU data
185cfe238e4SDavid Virag  *
186cfe238e4SDavid Virag  * Register specified CMU domain, which includes next steps:
187cfe238e4SDavid Virag  *
188cfe238e4SDavid Virag  * 1. Enable parent clock of @cmu CMU
189cfe238e4SDavid Virag  * 2. Set initial registers configuration for @cmu CMU clocks
190cfe238e4SDavid Virag  * 3. Register @cmu CMU clocks using Samsung clock framework API
191cfe238e4SDavid Virag  */
exynos_arm64_register_cmu(struct device * dev,struct device_node * np,const struct samsung_cmu_info * cmu)192cfe238e4SDavid Virag void __init exynos_arm64_register_cmu(struct device *dev,
193cfe238e4SDavid Virag 		struct device_node *np, const struct samsung_cmu_info *cmu)
194cfe238e4SDavid Virag {
195454e8d29SSam Protsenko 	int err;
196cfe238e4SDavid Virag 
197454e8d29SSam Protsenko 	/*
198454e8d29SSam Protsenko 	 * Try to boot even if the parent clock enablement fails, as it might be
199454e8d29SSam Protsenko 	 * already enabled by bootloader.
200454e8d29SSam Protsenko 	 */
201454e8d29SSam Protsenko 	err = exynos_arm64_enable_bus_clk(dev, np, cmu);
202454e8d29SSam Protsenko 	if (err)
203454e8d29SSam Protsenko 		pr_err("%s: could not enable bus clock %s; err = %d\n",
204454e8d29SSam Protsenko 		       __func__, cmu->clk_name, err);
205cfe238e4SDavid Virag 
206*7fa37084SSam Protsenko 	exynos_arm64_init_clocks(np, cmu);
207cfe238e4SDavid Virag 	samsung_cmu_register_one(np, cmu);
208cfe238e4SDavid Virag }
209f05dc202SSam Protsenko 
210f05dc202SSam Protsenko /**
211f05dc202SSam Protsenko  * exynos_arm64_register_cmu_pm - Register Exynos CMU domain with PM support
212f05dc202SSam Protsenko  *
213f05dc202SSam Protsenko  * @pdev:	Platform device object
214f05dc202SSam Protsenko  * @set_manual:	If true, set gate clocks to manual mode
215f05dc202SSam Protsenko  *
216f05dc202SSam Protsenko  * It's a version of exynos_arm64_register_cmu() with PM support. Should be
217f05dc202SSam Protsenko  * called from probe function of platform driver.
218f05dc202SSam Protsenko  *
219f05dc202SSam Protsenko  * Return: 0 on success, or negative error code on error.
220f05dc202SSam Protsenko  */
exynos_arm64_register_cmu_pm(struct platform_device * pdev,bool set_manual)221f05dc202SSam Protsenko int __init exynos_arm64_register_cmu_pm(struct platform_device *pdev,
222f05dc202SSam Protsenko 					bool set_manual)
223f05dc202SSam Protsenko {
224f05dc202SSam Protsenko 	const struct samsung_cmu_info *cmu;
225f05dc202SSam Protsenko 	struct device *dev = &pdev->dev;
226f05dc202SSam Protsenko 	struct device_node *np = dev->of_node;
227f05dc202SSam Protsenko 	struct exynos_arm64_cmu_data *data;
228f05dc202SSam Protsenko 	void __iomem *reg_base;
229f05dc202SSam Protsenko 	int ret;
230f05dc202SSam Protsenko 
231f05dc202SSam Protsenko 	cmu = of_device_get_match_data(dev);
232f05dc202SSam Protsenko 
233f05dc202SSam Protsenko 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
234f05dc202SSam Protsenko 	if (!data)
235f05dc202SSam Protsenko 		return -ENOMEM;
236f05dc202SSam Protsenko 
237f05dc202SSam Protsenko 	platform_set_drvdata(pdev, data);
238f05dc202SSam Protsenko 
239f05dc202SSam Protsenko 	ret = exynos_arm64_cmu_prepare_pm(dev, cmu);
240f05dc202SSam Protsenko 	if (ret)
241f05dc202SSam Protsenko 		return ret;
242f05dc202SSam Protsenko 
243f05dc202SSam Protsenko 	/*
244f05dc202SSam Protsenko 	 * Try to boot even if the parent clock enablement fails, as it might be
245f05dc202SSam Protsenko 	 * already enabled by bootloader.
246f05dc202SSam Protsenko 	 */
247f05dc202SSam Protsenko 	ret = exynos_arm64_enable_bus_clk(dev, NULL, cmu);
248f05dc202SSam Protsenko 	if (ret)
249f05dc202SSam Protsenko 		dev_err(dev, "%s: could not enable bus clock %s; err = %d\n",
250f05dc202SSam Protsenko 		       __func__, cmu->clk_name, ret);
251f05dc202SSam Protsenko 
252f05dc202SSam Protsenko 	if (set_manual)
253*7fa37084SSam Protsenko 		exynos_arm64_init_clocks(np, cmu);
254f05dc202SSam Protsenko 
255f05dc202SSam Protsenko 	reg_base = devm_platform_ioremap_resource(pdev, 0);
256f05dc202SSam Protsenko 	if (IS_ERR(reg_base))
257f05dc202SSam Protsenko 		return PTR_ERR(reg_base);
258f05dc202SSam Protsenko 
259f05dc202SSam Protsenko 	data->ctx = samsung_clk_init(dev, reg_base, cmu->nr_clk_ids);
260f05dc202SSam Protsenko 
261f05dc202SSam Protsenko 	/*
262f05dc202SSam Protsenko 	 * Enable runtime PM here to allow the clock core using runtime PM
263f05dc202SSam Protsenko 	 * for the registered clocks. Additionally, we increase the runtime
264f05dc202SSam Protsenko 	 * PM usage count before registering the clocks, to prevent the
265f05dc202SSam Protsenko 	 * clock core from runtime suspending the device.
266f05dc202SSam Protsenko 	 */
267f05dc202SSam Protsenko 	pm_runtime_get_noresume(dev);
268f05dc202SSam Protsenko 	pm_runtime_set_active(dev);
269f05dc202SSam Protsenko 	pm_runtime_enable(dev);
270f05dc202SSam Protsenko 
271f05dc202SSam Protsenko 	samsung_cmu_register_clocks(data->ctx, cmu);
272f05dc202SSam Protsenko 	samsung_clk_of_add_provider(dev->of_node, data->ctx);
273f05dc202SSam Protsenko 	pm_runtime_put_sync(dev);
274f05dc202SSam Protsenko 
275f05dc202SSam Protsenko 	return 0;
276f05dc202SSam Protsenko }
277f05dc202SSam Protsenko 
exynos_arm64_cmu_suspend(struct device * dev)278f05dc202SSam Protsenko int exynos_arm64_cmu_suspend(struct device *dev)
279f05dc202SSam Protsenko {
280f05dc202SSam Protsenko 	struct exynos_arm64_cmu_data *data = dev_get_drvdata(dev);
281f05dc202SSam Protsenko 	int i;
282f05dc202SSam Protsenko 
283f05dc202SSam Protsenko 	samsung_clk_save(data->ctx->reg_base, data->clk_save,
284f05dc202SSam Protsenko 			 data->nr_clk_save);
285f05dc202SSam Protsenko 
286f05dc202SSam Protsenko 	for (i = 0; i < data->nr_pclks; i++)
287f05dc202SSam Protsenko 		clk_prepare_enable(data->pclks[i]);
288f05dc202SSam Protsenko 
289f05dc202SSam Protsenko 	/* For suspend some registers have to be set to certain values */
290f05dc202SSam Protsenko 	samsung_clk_restore(data->ctx->reg_base, data->clk_suspend,
291f05dc202SSam Protsenko 			    data->nr_clk_suspend);
292f05dc202SSam Protsenko 
293f05dc202SSam Protsenko 	for (i = 0; i < data->nr_pclks; i++)
294f05dc202SSam Protsenko 		clk_disable_unprepare(data->pclks[i]);
295f05dc202SSam Protsenko 
296f05dc202SSam Protsenko 	clk_disable_unprepare(data->clk);
297f05dc202SSam Protsenko 
298f05dc202SSam Protsenko 	return 0;
299f05dc202SSam Protsenko }
300f05dc202SSam Protsenko 
exynos_arm64_cmu_resume(struct device * dev)301f05dc202SSam Protsenko int exynos_arm64_cmu_resume(struct device *dev)
302f05dc202SSam Protsenko {
303f05dc202SSam Protsenko 	struct exynos_arm64_cmu_data *data = dev_get_drvdata(dev);
304f05dc202SSam Protsenko 	int i;
305f05dc202SSam Protsenko 
306f05dc202SSam Protsenko 	clk_prepare_enable(data->clk);
307f05dc202SSam Protsenko 
308f05dc202SSam Protsenko 	for (i = 0; i < data->nr_pclks; i++)
309f05dc202SSam Protsenko 		clk_prepare_enable(data->pclks[i]);
310f05dc202SSam Protsenko 
311f05dc202SSam Protsenko 	samsung_clk_restore(data->ctx->reg_base, data->clk_save,
312f05dc202SSam Protsenko 			    data->nr_clk_save);
313f05dc202SSam Protsenko 
314f05dc202SSam Protsenko 	for (i = 0; i < data->nr_pclks; i++)
315f05dc202SSam Protsenko 		clk_disable_unprepare(data->pclks[i]);
316f05dc202SSam Protsenko 
317f05dc202SSam Protsenko 	return 0;
318f05dc202SSam Protsenko }
319