Skip to contentSkip to conversation

clk: introduce enable_cnt

PATCH New Peng Fan 2 participants 5 messages Aug 16, 2019 Common clk

lore
Share & tools
lore

Peng Fan

author
#0 lore raw

PATCH 1/3 clk: introduce enable_cnt

Introduce enable_cnt to track the clk enable/disable count when clk_enable/disable for CCF.
And Initialize enable_cnt to 0 when register the clk.

---
 drivers/clk/clk.c            | 1 +
 drivers/clk/clk_fixed_rate.c | 1 +
 include/clk.h                | 1 +
 3 files changed, 3 insertions(+)
drivers/clk/clk.c +1 -0
index 39b3087067..7effd410ee 100644
@@ -40,6 +40,7 @@ int clk_register(struct clk *clk, const char *drv_name,
 		return ret;
 	}
 
+	clk->enable_cnt = 0;
 	/* Store back pointer to clk from udevice */
 	clk->dev->uclass_priv = clk;
 
drivers/clk/clk_fixed_rate.c +1 -0
index 08cce0d79b..6b89fad46b 100644
@@ -27,6 +27,7 @@ static int clk_fixed_rate_ofdata_to_platdata(struct udevice *dev)
 	/* Make fixed rate clock accessible from higher level struct clk */
 	dev->uclass_priv = clk;
 	clk->dev = dev;
+	clk->enable_cnt = 0;
 
 	return 0;
 }
include/clk.h +1 -0
index 2ebc905e04..58e456898f 100644
@@ -61,6 +61,7 @@ struct clk {
 	struct udevice *dev;
 	long long rate;	/* in HZ */
 	u32 flags;
+	int enable_cnt;
 	/*
 	 * Written by of_xlate. In the future, we might add more fields here.
 	 */
Signature · 1 line
2.16.4
author
#1 lore raw

PATCH 2/3 clk: prograte clk enable/disable to parent

When enabling/disabling a clk, also need to enable/disable the clk's parent. Implement this in clk_enable/disable.

---
 drivers/clk/clk-uclass.c | 68 +++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 62 insertions(+), 6 deletions(-)
drivers/clk/clk-uclass.c +62 -6
index cee4d912b0..208c9b7906 100644
@@ -449,13 +449,41 @@ int clk_set_parent(struct clk *clk, struct clk *parent)
 int clk_enable(struct clk *clk)
 {
 	const struct clk_ops *ops = clk_dev_ops(clk->dev);
+	struct clk *clkp = NULL;
+	int ret;
 
 	debug("%s(clk=%p)\n", __func__, clk);
 
-	if (!ops->enable)
-		return -ENOSYS;
+	if (CONFIG_IS_ENABLED(CLK_CCF)) {
+		/* Take id 0 as a non-valid clk, such as dummy */
+		if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
+			if (clkp->dev->parent &&
+			    device_get_uclass_id(clkp->dev) == UCLASS_CLK) {
+				ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
+				if (ret) {
+					printf("Enable %s failed\n",
+					       clkp->dev->parent->name);
+					return ret;
+				}
+			}
+		}
 
-	return ops->enable(clk);
+		if (ops->enable) {
+			ret = ops->enable(clk);
+			if (ret) {
+				printf("Enable %s failed\n", clk->dev->name);
+				return ret;
+			}
+		}
+		if (clkp)
+			clkp->enable_cnt++;
+	} else {
+		if (!ops->enable)
+			return -ENOSYS;
+		return ops->enable(clk);
+	}
+
+	return 0;
 }
 
 int clk_enable_bulk(struct clk_bulk *bulk)
@@ -474,13 +502,41 @@ int clk_enable_bulk(struct clk_bulk *bulk)
 int clk_disable(struct clk *clk)
 {
 	const struct clk_ops *ops = clk_dev_ops(clk->dev);
+	struct clk *clkp;
+	int ret;
 
 	debug("%s(clk=%p)\n", __func__, clk);
 
-	if (!ops->disable)
-		return -ENOSYS;
+	if (CONFIG_IS_ENABLED(CLK_CCF)) {
+		if (ops->disable) {
+			ret = ops->disable(clk);
+			if (ret)
+				return ret;
+		}
+
+		if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
+			if (clkp->dev->parent &&
+			    device_get_uclass_id(clkp->dev) == UCLASS_CLK) {
+				ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent));
+				if (ret) {
+					printf("Disable %s failed\n",
+					       clkp->dev->parent->name);
+					return ret;
+				}
+			}
+		}
+
+		if (clkp)
+			clkp->enable_cnt--;
+	} else {
 
-	return ops->disable(clk);
+		if (!ops->disable)
+			return -ENOSYS;
+
+		return ops->disable(clk);
+	}
+
+	return 0;
 }
 
 int clk_disable_bulk(struct clk_bulk *bulk)
Signature · 1 line
2.16.4
author
#2 lore raw

PATCH 3/3 clk: support clk tree dump

In reply to Peng Fan

The previous code only dump the clk list. This patch is to support clk tree dump, and also dump the enable_cnt.

On i.MX8MM, Partial output:
u-boot=> clk dump
Rate Usecnt Name
------------------------------------------

 24000000             0        |-- clock-osc-24m
 24000000             0        |   |-- dram_pll_ref_sel
 750000000            0        |   |   `-- dram_pll
 750000000            0        |   |       `-- dram_pll_bypass
 750000000            0        |   |           `-- dram_pll_out
 24000000             0        |   |-- arm_pll_ref_sel
 1200000000           0        |   |   `-- arm_pll
 1200000000           0        |   |       `-- arm_pll_bypass
 1200000000           0        |   |           `-- arm_pll_out
 1200000000           0        |   |               `-- arm_a53_src
 1200000000           0        |   |                   `-- arm_a53_cg
 1200000000           0        |   |                       `-- arm_a53_div
 24000000             4        |   |-- sys_pll1_ref_sel
 800000000            4        |   |   `-- sys_pll1
 800000000            4        |   |       `-- sys_pll1_bypass
 800000000            4        |   |           `-- sys_pll1_out
 40000000             0        |   |               |-- sys_pll1_40m
---
 cmd/clk.c | 77 ++++++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 44 insertions(+), 33 deletions(-)
cmd/clk.c +44 -33
index 5402c87de7..4b1f3079ff 100644
@@ -7,50 +7,61 @@
 #include <clk.h>
 #if defined(CONFIG_DM) && defined(CONFIG_CLK)
 #include <dm.h>
+#include <dm/device.h>
+#include <dm/root.h>
 #include <dm/device-internal.h>
+#include <linux/clk-provider.h>
 #endif
 
-int __weak soc_clk_dump(void)
+static void show_clks(struct udevice *dev, int depth, int last_flag)
 {
-#if defined(CONFIG_DM) && defined(CONFIG_CLK)
-	struct udevice *dev;
-	struct uclass *uc;
-	struct clk clk;
-	int ret;
-	ulong rate;
-
-	/* Device addresses start at 1 */
-	ret = uclass_get(UCLASS_CLK, &uc);
-	if (ret)
-		return ret;
-
-	uclass_foreach_dev(dev, uc) {
-		memset(&clk, 0, sizeof(clk));
-		ret = device_probe(dev);
-		if (ret)
-			goto noclk;
+	int i, is_last;
+	struct udevice *child;
+	struct clk *clkp;
+	u32 rate;
+
+	clkp = dev_get_clk_ptr(dev);
+	if (device_get_uclass_id(dev) == UCLASS_CLK && clkp) {
+		rate = clk_get_rate(clkp);
+
+	printf(" %-12u  %8d        ", rate, clkp->enable_cnt);
+
+	for (i = depth; i >= 0; i--) {
+		is_last = (last_flag >> i) & 1;
+		if (i) {
+			if (is_last)
+				printf("    ");
+			else
+				printf("|   ");
+		} else {
+			if (is_last)
+				printf("`-- ");
+			else
+				printf("|-- ");
+		}
+	}
 
-		ret = clk_request(dev, &clk);
-		if (ret)
-			goto noclk;
+	printf("%s\n", dev->name);
+	}
 
-		rate = clk_get_rate(&clk);
-		clk_free(&clk);
+	list_for_each_entry(child, &dev->child_head, sibling_node) {
+		is_last = list_is_last(&child->sibling_node, &dev->child_head);
+		show_clks(child, depth + 1, (last_flag << 1) | is_last);
+	}
+}
 
-		if (rate == -ENODEV)
-			goto noclk;
+int __weak soc_clk_dump(void)
+{
+	struct udevice *root;
 
-		printf("%-30.30s : %lu Hz\n", dev->name, rate);
-		continue;
-	noclk:
-		printf("%-30.30s : ? Hz\n", dev->name);
+	root = dm_root();
+	if (root) {
+		printf(" Rate               Usecnt      Name\n");
+		printf("------------------------------------------\n");
+		show_clks(root, -1, 0);
 	}
 
 	return 0;
-#else
-	puts("Not implemented\n");
-	return 1;
-#endif
 }
 
 static int do_clk_dump(cmd_tbl_t *cmdtp, int flag, int argc,
Signature · 1 line
2.16.4
#3 lore raw

PATCH 1/3 clk: introduce enable_cnt

In reply to Peng Fan

Hi Peng,

Quoted 1 line

Introduce enable_cnt to track the clk enable/disable count

As fair as I remember there was no reference counters for Linux original CCF (to be precise - they are in devices, but not explicitly used in CCF).

As the commit message is very short I would like to explicitly ask what problem do you try to solve here (and if the same problem is available on Linux)? [*]

The whole parch series misses a few things:

- Detailed explanation and rationale for this code [*]

- If you introduce new variable/functionality in clk-uclass - there shall be a sandbox test code for it. This is missing in this series.

Note:

[*] - please write detailed commit messages / readme for your patches (this is the note for the whole NXP's u-boot developers' team).

Detailed commit message provides:

- Better understanding of the issue for reviewers from the outset

- It is a very good documentation which stays with the code (in git)

- After e.g. 2 years it is still possible to get the grasp of the problem (when bisecting/ fixing bugs) quickly.

Quoted 46 lines

when
clk_enable/disable for CCF.
And Initialize enable_cnt to 0 when register the clk.

---
 drivers/clk/clk.c            | 1 +
 drivers/clk/clk_fixed_rate.c | 1 +
 include/clk.h                | 1 +
 3 files changed, 3 insertions(+)
drivers/clk/clk.c +1 -0
index 39b3087067..7effd410ee 100644
@@ -40,6 +40,7 @@ int clk_register(struct clk *clk, const char
 *drv_name, return ret;
 	}
 
+	clk->enable_cnt = 0;
 	/* Store back pointer to clk from udevice */
 	clk->dev->uclass_priv = clk;
 
(quoted diff) +0 -0

b/drivers/clk/clk_fixed_rate.c index 08cce0d79b..6b89fad46b 100644

drivers/clk/clk_fixed_rate.c +0 -0
@@ -27,6 +27,7 @@ static int clk_fixed_rate_ofdata_to_platdata(struct

udevice *dev) /* Make fixed rate clock accessible from higher level struct clk */ dev->uclass_priv = clk;

drivers/clk/clk_fixed_rate.c +1 -0
 	clk->dev = dev;
+	clk->enable_cnt = 0;
 
 	return 0;
 }
include/clk.h +1 -0
index 2ebc905e04..58e456898f 100644
@@ -61,6 +61,7 @@ struct clk {
 	struct udevice *dev;
 	long long rate;	/* in HZ */
 	u32 flags;
+	int enable_cnt;
 	/*
 	 * Written by of_xlate. In the future, we might add more
Quoted 1 line

fields here. */

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20190816/82716981/attachment.sig>

author
#4 lore raw

PATCH 1/3 clk: introduce enable_cnt

Hi Lukasz,

Quoted 8 lines

Subject: Re: [PATCH 1/3] clk: introduce enable_cnt

Hi Peng,

Quoted 1 line

Introduce enable_cnt to track the clk enable/disable count

As fair as I remember there was no reference counters for Linux original CCF (to be precise - they are in devices, but not explicitly used in CCF).

There are prepare and enable count in Linux clk
https://elixir.bootlin.com/linux/latest/source/drivers/clk/clk.c#L1006

Quoted 4 lines

As the commit message is very short I would like to explicitly ask what problem do you try to solve here (and if the same problem is available on Linux)? [*]

On i.MX8MM, the clk tree might be
OSC->PLL->PLL GATE->CCM->CCGR GATE->Device

The current U-Boot implementation only handle CCGR GATE, It not able to handle PLL GATE unless enable the gate explicitly.

Quoted 14 lines

The whole parch series misses a few things:

- Detailed explanation and rationale for this code [*]

- If you introduce new variable/functionality in clk-uclass - there shall be a sandbox test code for it. This is missing in this series.

Note:

[*] - please write detailed commit messages / readme for your patches (this is the note for the whole NXP's u-boot developers' team).

Quoted 8 lines

Detailed commit message provides:

- Better understanding of the issue for reviewers from the outset

- It is a very good documentation which stays with the code (in git)

- After e.g. 2 years it is still possible to get the grasp of the problem (when bisecting/ fixing bugs) quickly.

Will fix in V2.

Thanks,
Peng.

Quoted 61 lines
Quoted 47 lines

when
clk_enable/disable for CCF.
And Initialize enable_cnt to 0 when register the clk.

---
 drivers/clk/clk.c            | 1 +
 drivers/clk/clk_fixed_rate.c | 1 +
 include/clk.h                | 1 +
 3 files changed, 3 insertions(+)
drivers/clk/clk.c index renamed from drivers/clk/clk.c +0 -0

39b3087067..7effd410ee 100644

drivers/clk/clk.c +1 -0
@@ -40,6 +40,7 @@ int clk_register(struct clk *clk, const char
 *drv_name, return ret;
 	}
 
+	clk->enable_cnt = 0;
 	/* Store back pointer to clk from udevice */
 	clk->dev->uclass_priv = clk;
 
(quoted diff) +0 -0

b/drivers/clk/clk_fixed_rate.c index 08cce0d79b..6b89fad46b 100644

drivers/clk/clk_fixed_rate.c +0 -0
@@ -27,6 +27,7 @@ static int clk_fixed_rate_ofdata_to_platdata(struct

udevice *dev) /* Make fixed rate clock accessible from higher level struct clk */ dev->uclass_priv = clk;

drivers/clk/clk_fixed_rate.c +1 -0
 	clk->dev = dev;
+	clk->enable_cnt = 0;
 
 	return 0;
 }
include/clk.h index renamed from include/clk.h +0 -0

2ebc905e04..58e456898f 100644

include/clk.h +1 -0
@@ -61,6 +61,7 @@ struct clk {
 	struct udevice *dev;
 	long long rate;	/* in HZ */
 	u32 flags;
+	int enable_cnt;
 	/*
 	 * Written by of_xlate. In the future, we might add more fields
Quoted 1 line

here. */

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email:
lukma at denx.de