Skip to contentSkip to conversation

clk: samsung: exynos850: Register clocks early

PATCH Superseded Sam Protsenko 4 participants 6 messages Oct 25, 2021 – Nov 21, 2021

Subsystems (4)
lore
Share & tools
lore

Sam Protsenko

author maintainer
#0 lore raw

PATCH v2 1/1 clk: samsung: exynos850: Register clocks early

Some clocks must be registered before init calls. For example MCT clock (from CMU_PERI) is needed for MCT timer driver, which is registered with TIMER_OF_DECLARE(). By the time we get to core_initcall() used for clk-exynos850 platform driver init, it's already too late. Inability to get "mct" clock in MCT driver leads to kernel panic, as functions registered with *_OF_DECLARE() can't do deferred calls. MCT timer driver can't be fixed either, as it's acting as a clock source and it's essential to register it in start_kernel() -> time_init().

Let's register CMU_PERI clocks early, using CLK_OF_DECLARE_DRIVER(), and do all stuff relying on "struct dev" object (like runtime PM and enabling bus clock) later in platform driver probe. Basically CLK_OF_DECLARE_DRIVER() matches CMU compatible, but clears OF_POPULATED flag, which allows the same device to be matched again later.

Similar issue was discussed at [1] and addressed in commit 1f7db7bbf031 ("clk: renesas: cpg-mssr: Add early clock support"), as well as in drivers/clk/mediatek/clk-mt2712.c.

[1] https://patchwork.kernel.org/project/linux-renesas-soc/patch/[email protected]/

---
Changes in v2:
- Register only CMU_PERI clocks early

Notes:
- This patch should be applied on top of CMU_APM series (clk: samsung: exynos850: Implement CMU_APM domain)

 drivers/clk/samsung/clk-exynos850.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/clk/samsung/clk-exynos850.c +15 -2
index 95e373d17b42..ecffa5c7a081 100644
@@ -753,6 +753,15 @@ static const struct samsung_cmu_info peri_cmu_info __initconst = {
 	.clk_name		= "dout_peri_bus",
 };
 
+static void __init exynos850_cmu_peri_init(struct device_node *np)
+{
+	exynos850_init_clocks(np, peri_clk_regs, ARRAY_SIZE(peri_clk_regs));
+	samsung_cmu_register_one(np, &peri_cmu_info);
+}
+
+CLK_OF_DECLARE_DRIVER(exynos850_cmu_peri, "samsung,exynos850-cmu-peri",
+		      exynos850_cmu_peri_init);
+
 /* ---- CMU_CORE ------------------------------------------------------------ */
 
 /* Register Offset definitions for CMU_CORE (0x12000000) */
@@ -920,8 +929,12 @@ static int __init exynos850_cmu_probe(struct platform_device *pdev)
 	struct device_node *np = dev->of_node;
 
 	info = of_device_get_match_data(dev);
-	exynos850_init_clocks(np, info->clk_regs, info->nr_clk_regs);
-	samsung_cmu_register_one(np, info);
+
+	/* Early clocks are already registered using CLK_OF_DECLARE_DRIVER() */
+	if (info != &peri_cmu_info) {
+		exynos850_init_clocks(np, info->clk_regs, info->nr_clk_regs);
+		samsung_cmu_register_one(np, info);
+	}
 
 	/* Keep bus clock running, so it's possible to access CMU registers */
 	if (info->clk_name) {
Signature · 1 line
2.30.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 26 lines · Sam Protsenko

Some clocks must be registered before init calls. For example MCT clock (from CMU_PERI) is needed for MCT timer driver, which is registered with TIMER_OF_DECLARE(). By the time we get to core_initcall() used for clk-exynos850 platform driver init, it's already too late. Inability to get "mct" clock in MCT driver leads to kernel panic, as functions registered with *_OF_DECLARE() can't do deferred calls. MCT timer driver can't be fixed either, as it's acting as a clock source and it's essential to register it in start_kernel() -> time_init().

Let's register CMU_PERI clocks early, using CLK_OF_DECLARE_DRIVER(), and do all stuff relying on "struct dev" object (like runtime PM and enabling bus clock) later in platform driver probe. Basically CLK_OF_DECLARE_DRIVER() matches CMU compatible, but clears OF_POPULATED flag, which allows the same device to be matched again later.

Similar issue was discussed at [1] and addressed in commit 1f7db7bbf031 ("clk: renesas: cpg-mssr: Add early clock support"), as well as in drivers/clk/mediatek/clk-mt2712.c.

[1] https://patchwork.kernel.org/project/linux-renesas-soc/patch/[email protected]/

---
Changes in v2:
- Register only CMU_PERI clocks early

Quoted 7 lines

Notes:
- This patch should be applied on top of CMU_APM series (clk: samsung: exynos850: Implement CMU_APM domain)

 drivers/clk/samsung/clk-exynos850.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

Best regards,
Krzysztof

#2 lore raw
In reply to Sam Protsenko
Quoted 42 lines · Sam Protsenko

Some clocks must be registered before init calls. For example MCT clock (from CMU_PERI) is needed for MCT timer driver, which is registered with TIMER_OF_DECLARE(). By the time we get to core_initcall() used for clk-exynos850 platform driver init, it's already too late. Inability to get "mct" clock in MCT driver leads to kernel panic, as functions registered with *_OF_DECLARE() can't do deferred calls. MCT timer driver can't be fixed either, as it's acting as a clock source and it's essential to register it in start_kernel() -> time_init().

Let's register CMU_PERI clocks early, using CLK_OF_DECLARE_DRIVER(), and do all stuff relying on "struct dev" object (like runtime PM and enabling bus clock) later in platform driver probe. Basically CLK_OF_DECLARE_DRIVER() matches CMU compatible, but clears OF_POPULATED flag, which allows the same device to be matched again later.

 drivers/clk/samsung/clk-exynos850.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/clk/samsung/clk-exynos850.c +9 -0
index 95e373d17b42..ecffa5c7a081 100644
@@ -753,6 +753,15 @@ static const struct samsung_cmu_info peri_cmu_info __initconst = {
 	.clk_name		= "dout_peri_bus",
 };
 
+static void __init exynos850_cmu_peri_init(struct device_node *np)
+{
+	exynos850_init_clocks(np, peri_clk_regs, ARRAY_SIZE(peri_clk_regs));
+	samsung_cmu_register_one(np, &peri_cmu_info);
+}
+
+CLK_OF_DECLARE_DRIVER(exynos850_cmu_peri, "samsung,exynos850-cmu-peri",
+		      exynos850_cmu_peri_init);
+
 /* ---- CMU_CORE ------------------------------------------------------------ */
 
 /* Register Offset definitions for CMU_CORE (0x12000000) */
@@ -920,8 +929,12 @@ static int __init exynos850_cmu_probe(struct platform_device *pdev)
 	struct device_node *np = dev->of_node;

drivers/clk/samsung/clk-exynos850.c static int __init exynos850_cmu_probe(struct platform_device *pdev) · line 937

Quoted 10 lines
drivers/clk/samsung/clk-exynos850.c +6 -2
 
 	info = of_device_get_match_data(dev);
-	exynos850_init_clocks(np, info->clk_regs, info->nr_clk_regs);
-	samsung_cmu_register_one(np, info);
+
+	/* Early clocks are already registered using CLK_OF_DECLARE_DRIVER() */
+	if (info != &peri_cmu_info) {
+		exynos850_init_clocks(np, info->clk_regs, info->nr_clk_regs);
+		samsung_cmu_register_one(np, info);
+	}

Don't you also need to register early CMU_TOP, which provides clocks for CMU_PERI? I'm afraid it might not work properly when you register CMU_PERI clocks early and only later in probe() you enable parent clock required for the already registered clocks to be usable.
How about registering also CMU_TOP early and enabling parent clock also in OF_CLK_DECLARE init callback, i.e. using either OF_CLK_DECLARE or platform driver for a CMU?

Signature · 2 lines
Regards,
Sylwester
author maintainer
#3 lore raw
Quoted 45 lines · Sylwester Nawrocki
Quoted 43 lines · Sam Protsenko

Some clocks must be registered before init calls. For example MCT clock (from CMU_PERI) is needed for MCT timer driver, which is registered with TIMER_OF_DECLARE(). By the time we get to core_initcall() used for clk-exynos850 platform driver init, it's already too late. Inability to get "mct" clock in MCT driver leads to kernel panic, as functions registered with *_OF_DECLARE() can't do deferred calls. MCT timer driver can't be fixed either, as it's acting as a clock source and it's essential to register it in start_kernel() -> time_init().

Let's register CMU_PERI clocks early, using CLK_OF_DECLARE_DRIVER(), and do all stuff relying on "struct dev" object (like runtime PM and enabling bus clock) later in platform driver probe. Basically CLK_OF_DECLARE_DRIVER() matches CMU compatible, but clears OF_POPULATED flag, which allows the same device to be matched again later.

 drivers/clk/samsung/clk-exynos850.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/clk/samsung/clk-exynos850.c +9 -0
index 95e373d17b42..ecffa5c7a081 100644
@@ -753,6 +753,15 @@ static const struct samsung_cmu_info peri_cmu_info __initconst = {
      .clk_name               = "dout_peri_bus",
 };
 
+static void __init exynos850_cmu_peri_init(struct device_node *np)
+{
+     exynos850_init_clocks(np, peri_clk_regs, ARRAY_SIZE(peri_clk_regs));
+     samsung_cmu_register_one(np, &peri_cmu_info);
+}
+
+CLK_OF_DECLARE_DRIVER(exynos850_cmu_peri, "samsung,exynos850-cmu-peri",
+                   exynos850_cmu_peri_init);
+
 /* ---- CMU_CORE ------------------------------------------------------------ */
 
 /* Register Offset definitions for CMU_CORE (0x12000000) */
@@ -920,8 +929,12 @@ static int __init exynos850_cmu_probe(struct platform_device *pdev)
      struct device_node *np = dev->of_node;
 
Quoted 14 lines

drivers/clk/samsung/clk-exynos850.c static int __init exynos850_cmu_probe(struct platform_device *pdev) · line 937

Quoted 9 lines
drivers/clk/samsung/clk-exynos850.c +6 -2
      info = of_device_get_match_data(dev);
-     exynos850_init_clocks(np, info->clk_regs, info->nr_clk_regs);
-     samsung_cmu_register_one(np, info);
+
+     /* Early clocks are already registered using CLK_OF_DECLARE_DRIVER() */
+     if (info != &peri_cmu_info) {
+             exynos850_init_clocks(np, info->clk_regs, info->nr_clk_regs);
+             samsung_cmu_register_one(np, info);
+     }

Don't you also need to register early CMU_TOP, which provides clocks for CMU_PERI? I'm afraid it might not work properly when you register CMU_PERI clocks early and only later in probe() you enable parent clock required for the already registered clocks to be usable.

Good point, I'll do that in v2. Not sure how I missed that dependency point, but thank you for noticing that. Guess it only works for me because clocks are already enabled in bootloader, and I'm using "clk_ignore_unused" param for now.

Quoted 4 lines

How about registering also CMU_TOP early and enabling parent clock also in OF_CLK_DECLARE init callback, i.e. using either OF_CLK_DECLARE or platform driver for a CMU?

If you mean doing clk_prepare_enable() for "dout_peri_bus" clock in exynos850_cmu_peri_init(), I don't think it's possible. clk_get() needs "struct device *dev", and we only have that in platform driver probe. Trying to pass dev=NULL won't work, so that's why I'm enabling parent clocks in platform driver probe.

I'm going to submit new patch series soon. It'll include all my recent patches, addressing all your comments. We can continue discussion there, in case I misunderstood you and those patches are still not correct.

Thanks!

Quoted 3 lines

--
Regards,
Sylwester

#4 lore raw
Quoted 26 lines · Sam Protsenko
Quoted 16 lines

drivers/clk/samsung/clk-exynos850.c static int __init exynos850_cmu_probe(struct platform_device *pdev) · line 937

Quoted 12 lines
drivers/clk/samsung/clk-exynos850.c +6 -2
@@ -920,8 +929,12 @@ static int __init exynos850_cmu_probe(struct platform_device *pdev)
       struct device_node *np = dev->of_node;
 
       info = of_device_get_match_data(dev);
-     exynos850_init_clocks(np, info->clk_regs, info->nr_clk_regs);
-     samsung_cmu_register_one(np, info);
+
+     /* Early clocks are already registered using CLK_OF_DECLARE_DRIVER() */
+     if (info != &peri_cmu_info) {
+             exynos850_init_clocks(np, info->clk_regs, info->nr_clk_regs);
+             samsung_cmu_register_one(np, info);
+     }

Don't you also need to register early CMU_TOP, which provides clocks for CMU_PERI? I'm afraid it might not work properly when you register CMU_PERI clocks early and only later in probe() you enable parent clock required for the already registered clocks to be usable.

Good point, I'll do that in v2. Not sure how I missed that dependency point, but thank you for noticing that. Guess it only works for me because clocks are already enabled in bootloader, and I'm using "clk_ignore_unused" param for now.

Quoted 4 lines

How about registering also CMU_TOP early and enabling parent clock also in OF_CLK_DECLARE init callback, i.e. using either OF_CLK_DECLARE or platform driver for a CMU?

Quoted 5 lines

If you mean doing clk_prepare_enable() for "dout_peri_bus" clock in exynos850_cmu_peri_init(), I don't think it's possible. clk_get() needs "struct device *dev", and we only have that in platform driver probe. Trying to pass dev=NULL won't work, so that's why I'm enabling parent clocks in platform driver probe.

Sorry, I didn't notice it earlier, actually CMU_TOP is already being initialized with OF_CLK_DECLARE.

You could use of_clk_get() to get the clock, the consumer clock indexes are fixed and defined in the DT binding. There is also of_clk_get_by_name() which works similarly to clk_get().

author maintainer
#5 lore raw
Quoted 28 lines · Sylwester Nawrocki
Quoted 26 lines · Sam Protsenko
Quoted 16 lines

drivers/clk/samsung/clk-exynos850.c static int __init exynos850_cmu_probe(struct platform_device *pdev) · line 937

Quoted 12 lines
drivers/clk/samsung/clk-exynos850.c +6 -2
@@ -920,8 +929,12 @@ static int __init exynos850_cmu_probe(struct platform_device *pdev)
       struct device_node *np = dev->of_node;
 
       info = of_device_get_match_data(dev);
-     exynos850_init_clocks(np, info->clk_regs, info->nr_clk_regs);
-     samsung_cmu_register_one(np, info);
+
+     /* Early clocks are already registered using CLK_OF_DECLARE_DRIVER() */
+     if (info != &peri_cmu_info) {
+             exynos850_init_clocks(np, info->clk_regs, info->nr_clk_regs);
+             samsung_cmu_register_one(np, info);
+     }

Don't you also need to register early CMU_TOP, which provides clocks for CMU_PERI? I'm afraid it might not work properly when you register CMU_PERI clocks early and only later in probe() you enable parent clock required for the already registered clocks to be usable.

Good point, I'll do that in v2. Not sure how I missed that dependency point, but thank you for noticing that. Guess it only works for me because clocks are already enabled in bootloader, and I'm using "clk_ignore_unused" param for now.

Quoted 4 lines

How about registering also CMU_TOP early and enabling parent clock also in OF_CLK_DECLARE init callback, i.e. using either OF_CLK_DECLARE or platform driver for a CMU?

Quoted 9 lines
Quoted 5 lines

If you mean doing clk_prepare_enable() for "dout_peri_bus" clock in exynos850_cmu_peri_init(), I don't think it's possible. clk_get() needs "struct device *dev", and we only have that in platform driver probe. Trying to pass dev=NULL won't work, so that's why I'm enabling parent clocks in platform driver probe.

Sorry, I didn't notice it earlier, actually CMU_TOP is already being initialized with OF_CLK_DECLARE.

No worries, I forgot about that too, as you can see :-D But yeah, I'll keep that as is then. So I'll only make parent clock enabled early.

Quoted 4 lines

You could use of_clk_get() to get the clock, the consumer clock indexes are fixed and defined in the DT binding. There is also of_clk_get_by_name() which works similarly to clk_get().

Thanks for the suggestion! I'll go with of_clk_get_by_name(), it's more natural as we already have parent clock name stored, so it can be used in more general way.

This patch is superseded by next series (going to submit that soon):
[PATCH 0/6] clk: samsung: exynos850: Clock driver improvements

Quoted 1 line