1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021 Linaro Ltd.
4 * Copyright (C) 2021 Dávid Virág <virag.david003@gmail.com>
5 * Author: Sam Protsenko <semen.protsenko@linaro.org>
6 * Author: Dávid Virág <virag.david003@gmail.com>
8 * This file contains shared functions used by some arm64 Exynos SoCs,
9 * such as Exynos7885 or Exynos850 to register and init CMUs.
11 #include <linux/clk.h>
12 #include <linux/of_address.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
18 #include "clk-exynos-arm64.h"
20 /* PLL register bits */
21 #define PLL_CON1_MANUAL BIT(1)
23 /* Gate register bits */
24 #define GATE_MANUAL BIT(20)
25 #define GATE_ENABLE_HWACG BIT(28)
27 /* PLL_CONx_PLL register offsets range */
28 #define PLL_CON_OFF_START 0x100
29 #define PLL_CON_OFF_END 0x600
31 /* Gate register offsets range */
32 #define GATE_OFF_START 0x2000
33 #define GATE_OFF_END 0x2fff
35 struct exynos_arm64_cmu_data
{
36 struct samsung_clk_reg_dump
*clk_save
;
37 unsigned int nr_clk_save
;
38 const struct samsung_clk_reg_dump
*clk_suspend
;
39 unsigned int nr_clk_suspend
;
45 struct samsung_clk_provider
*ctx
;
48 /* Check if the register offset is a GATE register */
49 static bool is_gate_reg(unsigned long off
)
51 return off
>= GATE_OFF_START
&& off
<= GATE_OFF_END
;
54 /* Check if the register offset is a PLL_CONx register */
55 static bool is_pll_conx_reg(unsigned long off
)
57 return off
>= PLL_CON_OFF_START
&& off
<= PLL_CON_OFF_END
;
60 /* Check if the register offset is a PLL_CON1 register */
61 static bool is_pll_con1_reg(unsigned long off
)
63 return is_pll_conx_reg(off
) && (off
& 0xf) == 0x4 && !(off
& 0x10);
67 * exynos_arm64_init_clocks - Set clocks initial configuration
68 * @np: CMU device tree node with "reg" property (CMU addr)
71 * Set manual control mode for all gate and PLL clocks.
73 static void __init
exynos_arm64_init_clocks(struct device_node
*np
,
74 const struct samsung_cmu_info
*cmu
)
76 const unsigned long *reg_offs
= cmu
->clk_regs
;
77 size_t reg_offs_len
= cmu
->nr_clk_regs
;
78 void __iomem
*reg_base
;
81 reg_base
= of_iomap(np
, 0);
83 panic("%s: failed to map registers\n", __func__
);
85 for (i
= 0; i
< reg_offs_len
; ++i
) {
86 void __iomem
*reg
= reg_base
+ reg_offs
[i
];
89 if (cmu
->manual_plls
&& is_pll_con1_reg(reg_offs
[i
])) {
90 writel(PLL_CON1_MANUAL
, reg
);
91 } else if (is_gate_reg(reg_offs
[i
])) {
94 val
&= ~GATE_ENABLE_HWACG
;
103 * exynos_arm64_enable_bus_clk - Enable parent clock of specified CMU
105 * @dev: Device object; may be NULL if this function is not being
106 * called from platform driver probe function
107 * @np: CMU device tree node
110 * Keep CMU parent clock running (needed for CMU registers access).
112 * Return: 0 on success or a negative error code on failure.
114 static int __init
exynos_arm64_enable_bus_clk(struct device
*dev
,
115 struct device_node
*np
, const struct samsung_cmu_info
*cmu
)
117 struct clk
*parent_clk
;
123 struct exynos_arm64_cmu_data
*data
;
125 parent_clk
= clk_get(dev
, cmu
->clk_name
);
126 data
= dev_get_drvdata(dev
);
128 data
->clk
= parent_clk
;
130 parent_clk
= of_clk_get_by_name(np
, cmu
->clk_name
);
133 if (IS_ERR(parent_clk
))
134 return PTR_ERR(parent_clk
);
136 return clk_prepare_enable(parent_clk
);
139 static int __init
exynos_arm64_cmu_prepare_pm(struct device
*dev
,
140 const struct samsung_cmu_info
*cmu
)
142 struct exynos_arm64_cmu_data
*data
= dev_get_drvdata(dev
);
145 data
->clk_save
= samsung_clk_alloc_reg_dump(cmu
->clk_regs
,
150 data
->nr_clk_save
= cmu
->nr_clk_regs
;
151 data
->clk_suspend
= cmu
->suspend_regs
;
152 data
->nr_clk_suspend
= cmu
->nr_suspend_regs
;
153 data
->nr_pclks
= of_clk_get_parent_count(dev
->of_node
);
157 data
->pclks
= devm_kcalloc(dev
, sizeof(struct clk
*), data
->nr_pclks
,
160 kfree(data
->clk_save
);
164 for (i
= 0; i
< data
->nr_pclks
; i
++) {
165 struct clk
*clk
= of_clk_get(dev
->of_node
, i
);
168 kfree(data
->clk_save
);
170 clk_put(data
->pclks
[i
]);
173 data
->pclks
[i
] = clk
;
180 * exynos_arm64_register_cmu - Register specified Exynos CMU domain
181 * @dev: Device object; may be NULL if this function is not being
182 * called from platform driver probe function
183 * @np: CMU device tree node
186 * Register specified CMU domain, which includes next steps:
188 * 1. Enable parent clock of @cmu CMU
189 * 2. Set initial registers configuration for @cmu CMU clocks
190 * 3. Register @cmu CMU clocks using Samsung clock framework API
192 void __init
exynos_arm64_register_cmu(struct device
*dev
,
193 struct device_node
*np
, const struct samsung_cmu_info
*cmu
)
198 * Try to boot even if the parent clock enablement fails, as it might be
199 * already enabled by bootloader.
201 err
= exynos_arm64_enable_bus_clk(dev
, np
, cmu
);
203 pr_err("%s: could not enable bus clock %s; err = %d\n",
204 __func__
, cmu
->clk_name
, err
);
206 exynos_arm64_init_clocks(np
, cmu
);
207 samsung_cmu_register_one(np
, cmu
);
211 * exynos_arm64_register_cmu_pm - Register Exynos CMU domain with PM support
213 * @pdev: Platform device object
214 * @set_manual: If true, set gate clocks to manual mode
216 * It's a version of exynos_arm64_register_cmu() with PM support. Should be
217 * called from probe function of platform driver.
219 * Return: 0 on success, or negative error code on error.
221 int __init
exynos_arm64_register_cmu_pm(struct platform_device
*pdev
,
224 const struct samsung_cmu_info
*cmu
;
225 struct device
*dev
= &pdev
->dev
;
226 struct device_node
*np
= dev
->of_node
;
227 struct exynos_arm64_cmu_data
*data
;
228 void __iomem
*reg_base
;
231 cmu
= of_device_get_match_data(dev
);
233 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
237 platform_set_drvdata(pdev
, data
);
239 ret
= exynos_arm64_cmu_prepare_pm(dev
, cmu
);
244 * Try to boot even if the parent clock enablement fails, as it might be
245 * already enabled by bootloader.
247 ret
= exynos_arm64_enable_bus_clk(dev
, NULL
, cmu
);
249 dev_err(dev
, "%s: could not enable bus clock %s; err = %d\n",
250 __func__
, cmu
->clk_name
, ret
);
253 exynos_arm64_init_clocks(np
, cmu
);
255 reg_base
= devm_platform_ioremap_resource(pdev
, 0);
256 if (IS_ERR(reg_base
))
257 return PTR_ERR(reg_base
);
259 data
->ctx
= samsung_clk_init(dev
, reg_base
, cmu
->nr_clk_ids
);
262 * Enable runtime PM here to allow the clock core using runtime PM
263 * for the registered clocks. Additionally, we increase the runtime
264 * PM usage count before registering the clocks, to prevent the
265 * clock core from runtime suspending the device.
267 pm_runtime_get_noresume(dev
);
268 pm_runtime_set_active(dev
);
269 pm_runtime_enable(dev
);
271 samsung_cmu_register_clocks(data
->ctx
, cmu
);
272 samsung_clk_of_add_provider(dev
->of_node
, data
->ctx
);
273 pm_runtime_put_sync(dev
);
278 int exynos_arm64_cmu_suspend(struct device
*dev
)
280 struct exynos_arm64_cmu_data
*data
= dev_get_drvdata(dev
);
283 samsung_clk_save(data
->ctx
->reg_base
, data
->clk_save
,
286 for (i
= 0; i
< data
->nr_pclks
; i
++)
287 clk_prepare_enable(data
->pclks
[i
]);
289 /* For suspend some registers have to be set to certain values */
290 samsung_clk_restore(data
->ctx
->reg_base
, data
->clk_suspend
,
291 data
->nr_clk_suspend
);
293 for (i
= 0; i
< data
->nr_pclks
; i
++)
294 clk_disable_unprepare(data
->pclks
[i
]);
296 clk_disable_unprepare(data
->clk
);
301 int exynos_arm64_cmu_resume(struct device
*dev
)
303 struct exynos_arm64_cmu_data
*data
= dev_get_drvdata(dev
);
306 clk_prepare_enable(data
->clk
);
308 for (i
= 0; i
< data
->nr_pclks
; i
++)
309 clk_prepare_enable(data
->pclks
[i
]);
311 samsung_clk_restore(data
->ctx
->reg_base
, data
->clk_save
,
314 for (i
= 0; i
< data
->nr_pclks
; i
++)
315 clk_disable_unprepare(data
->pclks
[i
]);