Skip to contentSkip to conversation

clk: k210: Rewrite K210 clock without CCF

PATCH Superseded Sean Anderson 3 participants 15 messages Apr 12, 2021 – May 10, 2021

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover clk: k210: Rewrite K210 clock without CCF

This is something I've been meaning to do for a while but only just got around to. The CCF has been quite unwieldy in a few ways:

* It is very rigid, and there are not easy ways to hook into it without rewriting many things. See e.g. things like the bypass clock and all the _half clocks which were created because CCF didn't support the dividers used on the k210. While preparing this series, I encountered several edge cases which I had initially overlooked (or which were not supported in the initial release). These would have been very difficult to fix with CCF, but were much easier to address be
* There is a lot of magic going on under the curtains because of all the CCF code which lives in many different files. Some things live in drivers, but many things live in e.g. clk-uclass.c. So many things live in so many files and it can be very difficult to get a handle on what exactly happens. Complicating this is that there is a conflation of struct clk as a handle and struct clk as a device. In this regard, refcounting is completely broken. IMO we should just do away with refcounts and only disable clocks when explicitly asked for.
* It is very dependent on runtime initialization. Typically, everything is initialized by calling into various register() functions, usually with several wrappers to avoid specifying all the arguments. This balloons the runtime memory usage since there are so many devices created. It also makes it hard to debug, since if you do it the "typical" way it is easy to accidentally assign a clock to the wrong register.
* It inflates code size by pulling in not just some dead code (e.g. this driver does not use divider tables but they are in clk-divider anyway) but also pulling in numerous imx-specific clocks. This could be fixed, but I don't want to due to the other reasons listed.

I am very happy to have completely excised it from my driver. IMO there should be big warning signs on the CCF warning not to use it for new code. This would hopefully dissuade those like myself who had no idea that CCF was *not* in fact a good way to write a clock driver.

Overall there is a total savings of 12k from this series. Before:
text data bss dec hex filename
292485 32672 12624 337781 52775 u-boot
After:
text data bss dec hex filename
283125 29856 12624 325605 4f7e5 u-boot

This series depends on
https://patchwork.ozlabs.org/project/uboot/list/?series=238211

Sean Anderson (11):
clk: Allow force setting clock defaults before relocation
clk: k210: Rewrite to remove CCF
clk: k210: Move pll into the rest of the driver
clk: k210: Implement soc_clk_dump
clk: k210: Re-add support for setting rate
clk: k210: Don't set PLL rates if we are already at the correct rate
clk: k210: Remove bypass driver
clk: k210: Move k210 clock out of its own subdirectory
k210: dts: Set PLL1 to the same rate as PLL0
k210: Don't imply CCF
test: Add K210 PLL tests to sandbox defconfigs

 MAINTAINERS                             |    4 +-
 arch/riscv/dts/k210.dtsi                |    2 +
 board/sipeed/maix/Kconfig               |    2 -
 configs/sandbox64_defconfig             |    2 +
 configs/sandbox_defconfig               |    2 +
 configs/sandbox_flattree_defconfig      |    2 +
 configs/sipeed_maix_bitm_defconfig      |    1 -
 drivers/clk/Kconfig                     |   14 +-
 drivers/clk/Makefile                    |    2 +-
 drivers/clk/clk-uclass.c                |    9 +-
 drivers/clk/clk_kendryte.c              | 1319 +++++++++++++++++++++++
 drivers/clk/kendryte/Kconfig            |   12 -
 drivers/clk/kendryte/Makefile           |    1 -
 drivers/clk/kendryte/bypass.c           |  273 -----
 drivers/clk/kendryte/clk.c              |  668 ------------
 drivers/clk/kendryte/pll.c              |  585 ----------
 include/clk.h                           |    4 +-
 include/dt-bindings/clock/k210-sysctl.h |   94 +-
 include/kendryte/bypass.h               |   31 -
 include/kendryte/clk.h                  |   35 -
 include/kendryte/pll.h                  |   34 -
 21 files changed, 1399 insertions(+), 1697 deletions(-)
 create mode 100644 drivers/clk/clk_kendryte.c
 delete mode 100644 drivers/clk/kendryte/Kconfig
 delete mode 100644 drivers/clk/kendryte/Makefile
 delete mode 100644 drivers/clk/kendryte/bypass.c
 delete mode 100644 drivers/clk/kendryte/clk.c
 delete mode 100644 drivers/clk/kendryte/pll.c
 delete mode 100644 include/kendryte/bypass.h
 delete mode 100644 include/kendryte/clk.h
Signature · 1 line
2.31.0
#1 lore raw

PATCH 1/11 clk: Allow force setting clock defaults before relocation

Since 291da96b8e ("clk: Allow clock defaults to be set during re-reloc state for SPL only") it has been impossible to set clock defaults before relocation. This is annoying on boards without SPL, since there is no way to set clock defaults before U-Boot proper. In particular, the aisram rate must be changed before relocation on the K210, since U-Boot will hang if we try and change the rate while we are using aisram.

To get around this, (ab)use the stage parameter to force setting defaults, even if they would be otherwise posponed for later. A device tree property was decided against because of the concerns in the original commit thread about the overhead of repeatedly parsing the device tree.

---

 drivers/clk/clk-uclass.c | 9 +++++++--
 include/clk.h            | 4 +++-
 2 files changed, 10 insertions(+), 3 deletions(-)
drivers/clk/clk-uclass.c +7 -2
index 53e7be764d..cf8d35b04b 100644
@@ -353,9 +353,14 @@ int clk_set_defaults(struct udevice *dev, int stage)
 	if (!dev_has_ofnode(dev))
 		return 0;
 
-	/* If this not in SPL and pre-reloc state, don't take any action. */
+	/*
+	 * To avoid setting defaults twice, don't set them before relocation.
+	 * However, still set them for SPL. And still set them if explicitly
+	 * asked.
+	 */
 	if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
-		return 0;
+		if (stage <= 1)
+			return 0;
 
 	debug("%s(%s)\n", __func__, dev_read_name(dev));
 
include/clk.h +3 -1
index ca6b85fa6f..2021eb0506 100644
@@ -287,7 +287,9 @@ static inline int clk_release_all(struct clk *clk, int count)
  *              will be processed).
  * @stage:	A integer. 0 indicates that this is called before the device
  *		is probed. 1 indicates that this is called just after the
- *		device has been probed
+ *		device has been probed. 2 indicates that this is called after
+ *		the device has been probed, and that defaults should still be
+ *		set even if they would otherwise be ignored.
  */
 int clk_set_defaults(struct udevice *dev, int stage);
 #else
Signature · 1 line
2.31.0
#2 lore raw

PATCH 1/11 clk: Allow force setting clock defaults before relocation

Hi Sean,

Quoted 13 lines · Sean Anderson

Since 291da96b8e ("clk: Allow clock defaults to be set during re-reloc state for SPL only") it has been impossible to set clock defaults before relocation. This is annoying on boards without SPL, since there is no way to set clock defaults before U-Boot proper. In particular, the aisram rate must be changed before relocation on the K210, since U-Boot will hang if we try and change the rate while we are using aisram.

To get around this, (ab)use the stage parameter to force setting defaults, even if they would be otherwise posponed for later. A device tree property was decided against because of the concerns in the original commit thread about the overhead of repeatedly parsing the device tree.

Quoted 7 lines

---

 drivers/clk/clk-uclass.c | 9 +++++++--
 include/clk.h            | 4 +++-
 2 files changed, 10 insertions(+), 3 deletions(-)

But I think this should be an enum.

#3 lore raw

PATCH 1/11 clk: Allow force setting clock defaults before relocation

In reply to Sean Anderson

Hi Sean,

This patch series produces conflicts when applying. Could you please rebase them? Thanks!

Best regards,
Leo

Quoted 56 lines · Sean Anderson

Since 291da96b8e ("clk: Allow clock defaults to be set during re-reloc state for SPL only") it has been impossible to set clock defaults before relocation. This is annoying on boards without SPL, since there is no way to set clock defaults before U-Boot proper. In particular, the aisram rate must be changed before relocation on the K210, since U-Boot will hang if we try and change the rate while we are using aisram.

To get around this, (ab)use the stage parameter to force setting defaults, even if they would be otherwise posponed for later. A device tree property was decided against because of the concerns in the original commit thread about the overhead of repeatedly parsing the device tree.

---

 drivers/clk/clk-uclass.c | 9 +++++++--
 include/clk.h            | 4 +++-
 2 files changed, 10 insertions(+), 3 deletions(-)
drivers/clk/clk-uclass.c +7 -2
index 53e7be764d..cf8d35b04b 100644
@@ -353,9 +353,14 @@ int clk_set_defaults(struct udevice *dev, int stage)
 	if (!dev_has_ofnode(dev))
 		return 0;
 
-	/* If this not in SPL and pre-reloc state, don't take any action. */
+	/*
+	 * To avoid setting defaults twice, don't set them before relocation.
+	 * However, still set them for SPL. And still set them if explicitly
+	 * asked.
+	 */
 	if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
-		return 0;
+		if (stage <= 1)
+			return 0;
 
 	debug("%s(%s)\n", __func__, dev_read_name(dev));
 
include/clk.h +3 -1
index ca6b85fa6f..2021eb0506 100644
@@ -287,7 +287,9 @@ static inline int clk_release_all(struct clk *clk, int count)
  *              will be processed).
  * @stage:	A integer. 0 indicates that this is called before the device
  *		is probed. 1 indicates that this is called just after the
- *		device has been probed
+ *		device has been probed. 2 indicates that this is called after
+ *		the device has been probed, and that defaults should still be
+ *		set even if they would otherwise be ignored.
  */
 int clk_set_defaults(struct udevice *dev, int stage);
 #else
#4 lore raw

PATCH 2/11 clk: k210: Rewrite to remove CCF

In reply to Sean Anderson

This is effectively a complete rewrite to remove all dependency on CCF. The code is now smaller, and so is the binary (TODO: numbers). It also takes up less memory at runtime (since we don't have to create 40 udevices). In general, I am much happier with this driver as much of the complexity and late binding has been removed.

The k210_*_params structs which were previously used to initialize CCF clocks are now used as the complete configuration. Since we can write our own division logic, we can now do away with several "half" clocks which only existed to provide constant factors of two.

The clock IDs have been renumbered to remove unused clocks. This may not be the last time they are renumbered, since we have diverged with Linux. There are also still a few clocks left out which may need to be added back in.

In general, I have tried to leave out behavioral changes. However, there is a small bugfix regarding ACLK. According to the technical reference manual, its mux comes *after* its divider (which is present only for PLL0). This would have required yet another intermediate clock to fix with CCF, but with the new driver it is just 2 lines of code :)

---

 drivers/clk/kendryte/Kconfig            |   2 +-
 drivers/clk/kendryte/clk.c              | 848 +++++++++++-------------
 drivers/clk/kendryte/pll.c              | 114 ++--
 include/dt-bindings/clock/k210-sysctl.h |  94 ++-
 include/kendryte/pll.h                  |  26 +-
 5 files changed, 499 insertions(+), 585 deletions(-)
drivers/clk/kendryte/Kconfig +1 -1
index 073fca0781..0dc8e3f889 100644
@@ -1,6 +1,6 @@
 config CLK_K210
 	bool "Clock support for Kendryte K210"
-	depends on CLK && CLK_CCF && CLK_COMPOSITE_CCF
+	depends on CLK
 	help
 	  This enables support clock driver for Kendryte K210 platforms.
 
drivers/clk/kendryte/clk.c +381 -467

980 lines not on this page · raw message

drivers/clk/kendryte/pll.c +58 -56

209 lines not on this page · raw message

include/dt-bindings/clock/k210-sysctl.h +46 -48

104 lines not on this page · raw message

include/kendryte/pll.h +13 -13
index 95b8494f40..16fd5a5b68 100644
@@ -25,16 +25,6 @@
 #define K210_PLL_CLEAR_SLIP 2
 #define K210_PLL_TEST_OUT 3
 
-struct k210_pll {
-	struct clk clk;
-	void __iomem *reg; /* Base PLL register */
-	void __iomem *lock; /* Common PLL lock register */
-	u8 shift; /* Offset of bits in lock register */
-	u8 width; /* Width of lock bits to test against */
-};
-
-#define to_k210_pll(_clk) container_of(_clk, struct k210_pll, clk)
-
 struct k210_pll_config {
 	u8 r;
 	u8 f;
@@ -51,8 +41,18 @@ TEST_STATIC int k210_pll_calc_config(u32 rate, u32 rate_in,
 
 #endif
 
-extern const struct clk_ops k210_pll_ops;
+/**
+ * struct k210_clk_priv - K210 clock driver private data
+ * @base: The base address of the sysctl device
+ * @in0: The "in0" external oscillator
+ */
+struct k210_clk_priv {
+	void __iomem *base;
+	struct clk in0;
+};
 
-struct clk *k210_register_pll_struct(const char *name, const char *parent_name,
-				     struct k210_pll *pll);
+ulong k210_pll_set_rate(struct k210_clk_priv *priv, int id, ulong rate, ulong rate_in);
+ulong k210_pll_get_rate(struct k210_clk_priv *priv, int id, ulong rate_in);
+int k210_pll_enable(struct k210_clk_priv *priv, int id);
+int k210_pll_disable(struct k210_clk_priv *priv, int id);
 #endif /* K210_PLL_H */
Signature · 1 line
2.31.0
#5 lore raw

PATCH 3/11 clk: k210: Move pll into the rest of the driver

In reply to Sean Anderson

Now that there no separate PLL driver, we can no longer make the PLL functions static. By moving the PLL driver in with the rest of the clock code, we can make these functions static again. We still keep the pll header for unit testing, but it is pretty reduced.

---

 drivers/clk/kendryte/Makefile |   2 +-
 drivers/clk/kendryte/clk.c    | 606 +++++++++++++++++++++++++++++++++-
 drivers/clk/kendryte/pll.c    | 587 --------------------------------
 include/kendryte/clk.h        |  35 --
 include/kendryte/pll.h        |  34 --
 5 files changed, 601 insertions(+), 663 deletions(-)
 delete mode 100644 drivers/clk/kendryte/pll.c
 delete mode 100644 include/kendryte/clk.h
drivers/clk/kendryte/Makefile +1 -1
index 6fb68253ae..6710a1db72 100644
@@ -1 +1 @@
-obj-y += bypass.o clk.o pll.o
+obj-y += bypass.o clk.o
drivers/clk/kendryte/clk.c +600 -6

636 lines not on this page · raw message

drivers/clk/kendryte/pll.c deleted +0 -587

587 lines not on this page · raw message

include/kendryte/clk.h deleted +0 -35
deleted file mode 100644
index 9c6245d468..0000000000
@@ -1,35 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0+ */
-/*
- * Copyright (C) 2019-20 Sean Anderson <[email protected]>
- */
-
-#ifndef K210_CLK_H
-#define K210_CLK_H
-
-#define LOG_CATEGORY UCLASS_CLK
-#include <linux/types.h>
-#include <linux/clk-provider.h>
-
-static inline struct clk *k210_clk_gate(const char *name,
-					const char *parent_name,
-					void __iomem *reg, u8 bit_idx)
-{
-	return clk_register_gate(NULL, name, parent_name, 0, reg, bit_idx, 0,
-				 NULL);
-}
-
-static inline struct clk *k210_clk_half(const char *name,
-					const char *parent_name)
-{
-	return clk_register_fixed_factor(NULL, name, parent_name, 0, 1, 2);
-}
-
-static inline struct clk *k210_clk_div(const char *name,
-				       const char *parent_name,
-				       void __iomem *reg, u8 shift, u8 width)
-{
-	return clk_register_divider(NULL, name, parent_name, 0, reg, shift,
-				    width, 0);
-}
-
-#endif /* K210_CLK_H */
include/kendryte/pll.h +0 -34
index 16fd5a5b68..fd16a89cb2 100644
@@ -5,25 +5,7 @@
 #ifndef K210_PLL_H
 #define K210_PLL_H
 
-#include <clk.h>
 #include <test/export.h>
-#include <asm/io.h>
-
-#define K210_PLL_CLKR GENMASK(3, 0)
-#define K210_PLL_CLKF GENMASK(9, 4)
-#define K210_PLL_CLKOD GENMASK(13, 10) /* Output Divider */
-#define K210_PLL_BWADJ GENMASK(19, 14) /* BandWidth Adjust */
-#define K210_PLL_RESET BIT(20)
-#define K210_PLL_PWRD BIT(21) /* PoWeReD */
-#define K210_PLL_INTFB BIT(22) /* Internal FeedBack */
-#define K210_PLL_BYPASS BIT(23)
-#define K210_PLL_TEST BIT(24)
-#define K210_PLL_EN BIT(25)
-#define K210_PLL_TEST_EN BIT(26)
-
-#define K210_PLL_LOCK 0
-#define K210_PLL_CLEAR_SLIP 2
-#define K210_PLL_TEST_OUT 3
 
 struct k210_pll_config {
 	u8 r;
@@ -34,25 +16,9 @@ struct k210_pll_config {
 #ifdef CONFIG_UNIT_TEST
 TEST_STATIC int k210_pll_calc_config(u32 rate, u32 rate_in,
 				     struct k210_pll_config *best);
-
 #ifndef nop
 #define nop()
 #endif
 
 #endif
-
-/**
- * struct k210_clk_priv - K210 clock driver private data
- * @base: The base address of the sysctl device
- * @in0: The "in0" external oscillator
- */
-struct k210_clk_priv {
-	void __iomem *base;
-	struct clk in0;
-};
-
-ulong k210_pll_set_rate(struct k210_clk_priv *priv, int id, ulong rate, ulong rate_in);
-ulong k210_pll_get_rate(struct k210_clk_priv *priv, int id, ulong rate_in);
-int k210_pll_enable(struct k210_clk_priv *priv, int id);
-int k210_pll_disable(struct k210_clk_priv *priv, int id);
 #endif /* K210_PLL_H */
Signature · 1 line
2.31.0
#6 lore raw

PATCH 4/11 clk: k210: Implement soc_clk_dump

In reply to Sean Anderson

Since we are no longer using CCF we cannot use the default soc_clk_dump. Instead, implement our own.

---

 drivers/clk/kendryte/clk.c | 68 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 66 insertions(+), 2 deletions(-)
drivers/clk/kendryte/clk.c +66 -2
index de9db84361..203d5f741c 100644
@@ -925,14 +925,19 @@ static void k210_pll_waitfor_lock(struct k210_clk_priv *priv, int id)
 	}
 }
 
+static bool k210_pll_enabled(u32 reg)
+{
+	return (reg & K210_PLL_PWRD) && (reg & K210_PLL_EN) &&
+		!(reg & K210_PLL_RESET);
+}
+
 /* Adapted from sysctl_pll_enable */
 static int k210_pll_enable(struct k210_clk_priv *priv, int id)
 {
 	const struct k210_pll_params *pll = &k210_plls[id];
 	u32 reg = readl(priv->base + pll->off);
 
-	if ((reg & K210_PLL_PWRD) && (reg & K210_PLL_EN) &&
-	    !(reg & K210_PLL_RESET))
+	if (k210_pll_enabled(reg))
 		return 0;
 
 	reg |= K210_PLL_PWRD;
@@ -1174,3 +1179,62 @@ U_BOOT_DRIVER(k210_clk) = {
 	.probe = k210_clk_probe,
 	.priv_auto = sizeof(struct k210_clk_priv),
 };
+
+#if CONFIG_IS_ENABLED(CMD_CLK)
+static char show_enabled(struct k210_clk_priv *priv, int id)
+{
+	bool enabled;
+
+	if (k210_clks[id].flags & K210_CLKF_PLL) {
+		const struct k210_pll_params *pll =
+			&k210_plls[k210_clks[id].pll];
+
+		enabled = k210_pll_enabled(readl(priv->base + pll->off));
+	} else if (k210_clks[id].gate == K210_CLK_GATE_NONE) {
+		return '-';
+	} else {
+		const struct k210_gate_params *gate =
+			&k210_gates[k210_clks[id].gate];
+
+		enabled = k210_clk_readl(priv, gate->off, gate->bit_idx, 1);
+	}
+
+	return enabled ? 'y' : 'n';
+}
+
+static void show_clks(struct k210_clk_priv *priv, int id, int depth)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(k210_clks); i++) {
+		if (k210_clk_get_parent(priv, i) != id)
+			continue;
+
+		printf(" %-9lu %-7c %*s%s\n", do_k210_clk_get_rate(priv, i),
+		       show_enabled(priv, i), depth * 4, "",
+		       k210_clks[i].name);
+
+		show_clks(priv, i, depth + 1);
+	}
+}
+
+int soc_clk_dump(void)
+{
+	int ret;
+	struct udevice *dev;
+	struct k210_clk_priv *priv;
+
+	ret = uclass_get_device_by_driver(UCLASS_CLK, DM_DRIVER_GET(k210_clk),
+					  &dev);
+	if (ret)
+		return ret;
+	priv = dev_get_priv(dev);
+
+	puts(" Rate      Enabled Name\n");
+	puts("------------------------\n");
+	printf(" %-9lu %-7c %*s%s\n", clk_get_rate(&priv->in0), 'y', 0, "",
+	       priv->in0.dev->name);
+	show_clks(priv, K210_CLK_IN0, 1);
+	return 0;
+}
+#endif
Signature · 1 line
2.31.0
#7 lore raw

PATCH 5/11 clk: k210: Re-add support for setting rate

In reply to Sean Anderson

This adds support for setting clock rates, which was left out of the initial CCF expunging. There are several tricky bits here, mostly related to the PLLS:

* The PLL's bypass is broken. If the PLL is reconfigured, any child clocks will be stopped.
* PLL0 is the parent of ACLK which is the CPU and SRAM's clock. To prevent stopping the CPU while we configure PLL0's rate, ACLK is reparented to IN0 while PLL0 is disabled.
* PLL1 is the parent of the AISRAM clock. This clock cannot be reparented, so we instead just disallow changing PLL1's rate after relocation (when we are using the AISRAM).

---

 drivers/clk/kendryte/clk.c | 88 +++++++++++++++++++++++++++++++++++---
 1 file changed, 83 insertions(+), 5 deletions(-)
drivers/clk/kendryte/clk.c +83 -5
index 203d5f741c..cdea3d6f2b 100644
@@ -17,6 +17,8 @@
 #include <kendryte/pll.h>
 #include <linux/bitfield.h>
 
+DECLARE_GLOBAL_DATA_PTR;
+
 /**
  * struct k210_clk_priv - K210 clock driver private data
  * @base: The base address of the sysctl device
@@ -1059,11 +1061,6 @@ static ulong k210_clk_get_rate(struct clk *clk)
 	return do_k210_clk_get_rate(dev_get_priv(clk->dev), clk->id);
 }
 
-static ulong k210_clk_set_rate(struct clk *clk, unsigned long rate)
-{
-	return -ENOSYS;
-}
-
 static int do_k210_clk_set_parent(struct k210_clk_priv *priv, int id, int new)
 {
 	int i;
@@ -1089,6 +1086,81 @@ static int k210_clk_set_parent(struct clk *clk, struct clk *parent)
 				      parent->id);
 }
 
+static ulong k210_clk_set_rate(struct clk *clk, unsigned long rate)
+{
+	int parent, ret, err;
+	ulong rate_in, val;
+	const struct k210_div_params *div;
+	struct k210_clk_priv *priv = dev_get_priv(clk->dev);
+
+	if (clk->id == K210_CLK_IN0)
+		return clk_set_rate(&priv->in0, rate);
+
+	parent = k210_clk_get_parent(priv, clk->id);
+	rate_in = do_k210_clk_get_rate(priv, parent);
+
+	log_debug("id=%ld rate=%lu rate_in=%lu\n", clk->id, rate, rate_in);
+
+	if (clk->id == K210_CLK_PLL0) {
+		/* Bypass ACLK so the CPU keeps going */
+		ret = do_k210_clk_set_parent(priv, K210_CLK_ACLK, K210_CLK_IN0);
+		if (ret)
+			return ret;
+	} else if (clk->id == K210_CLK_PLL1 && gd->flags & GD_FLG_RELOC) {
+		/*
+		 * We can't bypass the AI clock like we can ACLK, and after
+		 * relocation we are using the AI ram.
+		 */
+		return -EPERM;
+	}
+
+	if (k210_clks[clk->id].flags & K210_CLKF_PLL) {
+		ret = k210_pll_set_rate(priv, k210_clks[clk->id].pll, rate,
+					rate_in);
+		if (!IS_ERR_VALUE(ret) && clk->id == K210_CLK_PLL0) {
+			/*
+			 * This may have the side effect of reparenting ACLK,
+			 * but I don't really want to keep track of what the old
+			 * parent was.
+			 */
+			err = do_k210_clk_set_parent(priv, K210_CLK_ACLK,
+						     K210_CLK_PLL0);
+			if (err)
+				return err;
+		}
+		return ret;
+	}
+
+	if (k210_clks[clk->id].div == K210_CLK_DIV_NONE)
+		return -ENOSYS;
+	div = &k210_divs[k210_clks[clk->id].div];
+
+	switch (div->type) {
+	case K210_DIV_ONE:
+		val = DIV_ROUND_CLOSEST_ULL((u64)rate_in, rate);
+		val = val ? val - 1 : 0;
+		break;
+	case K210_DIV_EVEN:
+		val = DIV_ROUND_CLOSEST_ULL((u64)rate_in, 2 * rate);
+		break;
+	case K210_DIV_POWER:
+		/* This is ACLK, which has no divider on IN0 */
+		if (parent == K210_CLK_IN0)
+			return -ENOSYS;
+
+		DIV_ROUND_CLOSEST_ULL((u64)rate_in, rate);
+		val = __ffs(val);
+		break;
+	default:
+		assert(false);
+		return -EINVAL;
+	};
+
+	val = val ? val - 1 : 0;
+	k210_clk_writel(priv, div->off, div->shift, div->width, val);
+	return do_k210_clk_get_rate(priv, clk->id);
+}
+
 static int k210_clk_endisable(struct k210_clk_priv *priv, int id, bool enable)
 {
 	int parent = k210_clk_get_parent(priv, id);
@@ -1163,6 +1235,12 @@ static int k210_clk_probe(struct udevice *dev)
 	if (ret)
 		return ret;
 
+	/*
+	 * Force setting defaults, even before relocation. This is so we can
+	 * set the clock rate for PLL1 before we relocate into aisram.
+	 */
+	clk_set_defaults(dev, 2);
+
 	return 0;
 }
 
Signature · 1 line
2.31.0
#8 lore raw

PATCH 6/11 clk: k210: Don't set PLL rates if we are already at the correct rate

In reply to Sean Anderson

This speeds up boot by preventing multiple reconfigurations of the PLLs.

---

 drivers/clk/kendryte/clk.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/clk/kendryte/clk.c +8 -7
index cdea3d6f2b..8e845db7eb 100644
@@ -847,21 +847,22 @@ static ulong k210_pll_set_rate(struct k210_clk_priv *priv, int id, ulong rate,
 	const struct k210_pll_params *pll = &k210_plls[id];
 	struct k210_pll_config config = {};
 	u32 reg;
+	ulong calc_rate;
 
 	if (rate_in < 0)
 		return rate_in;
 
-	log_debug("Calculating parameters with rate=%lu and rate_in=%lu\n",
-		  rate, rate_in);
 	err = k210_pll_calc_config(rate, rate_in, &config);
 	if (err)
 		return err;
 	log_debug("Got r=%u f=%u od=%u\n", config.r, config.f, config.od);
 
-	/*
-	 * Don't use clk_disable as it might not actually disable the pll due to
-	 * refcounting
-	 */
+	/* Don't bother setting the rate if we're already@that rate */
+	calc_rate = DIV_ROUND_DOWN_ULL(((u64)rate_in) * config.f,
+				       config.r * config.od);
+	if (calc_rate == k210_pll_get_rate(priv, id, rate))
+		return calc_rate;
+
 	k210_pll_disable(priv, id);
 
 	reg = readl(priv->base + pll->off);
@@ -875,7 +876,7 @@ static ulong k210_pll_set_rate(struct k210_clk_priv *priv, int id, ulong rate,
 	    |  FIELD_PREP(K210_PLL_BWADJ, config.f - 1);
 	writel(reg, priv->base + pll->off);
 
-	err = k210_pll_enable(priv, id);
+	k210_pll_enable(priv, id);
 
 	serial_setbrg();
 	return k210_pll_get_rate(priv, id, rate);
Signature · 1 line
2.31.0
#9 lore raw

PATCH 7/11 clk: k210: Remove bypass driver

In reply to Sean Anderson

This driver no longer serves a purpose now that we have moved away from CCF. Drop it.

---

 drivers/clk/kendryte/Makefile |   2 +-
 drivers/clk/kendryte/bypass.c | 273 ----------------------------------
 include/kendryte/bypass.h     |  31 ----
 3 files changed, 1 insertion(+), 305 deletions(-)
 delete mode 100644 drivers/clk/kendryte/bypass.c
 delete mode 100644 include/kendryte/bypass.h
drivers/clk/kendryte/Makefile +1 -1
index 6710a1db72..0303c0b99c 100644
@@ -1 +1 @@
-obj-y += bypass.o clk.o
+obj-y += clk.o
drivers/clk/kendryte/bypass.c deleted +0 -273

273 lines not on this page · raw message

include/kendryte/bypass.h deleted +0 -31
deleted file mode 100644
index ab85bbcbfc..0000000000
@@ -1,31 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0+ */
-/*
- * Copyright (C) 2020 Sean Anderson <[email protected]>
- */
-#ifndef K210_BYPASS_H
-#define K210_BYPASS_H
-
-struct clk;
-
-struct k210_bypass {
-	struct clk clk;
-	struct clk **children; /* Clocks to reparent */
-	struct clk **saved_parents; /* Parents saved over en-/dis-able */
-	struct clk *bypassee; /* Clock to bypass */
-	const struct clk_ops *bypassee_ops; /* Ops of the bypass clock */
-	struct clk *alt; /* Clock to set children to when bypassing */
-	size_t child_count;
-};
-
-#define to_k210_bypass(_clk) container_of(_clk, struct k210_bypass, clk)
-
-int k210_bypass_set_children(struct clk *clk, struct clk **children,
-			     size_t child_count);
-struct clk *k210_register_bypass_struct(const char *name,
-					const char *parent_name,
-					struct k210_bypass *bypass);
-struct clk *k210_register_bypass(const char *name, const char *parent_name,
-				 struct clk *bypassee,
-				 const struct clk_ops *bypassee_ops,
-				 struct clk *alt);
-#endif /* K210_BYPASS_H */
Signature · 1 line
2.31.0
#10 lore raw

PATCH 8/11 clk: k210: Move k210 clock out of its own subdirectory

In reply to Sean Anderson

Now that we have only one clock driver, we don't need to have our own subdirectory. Move the driver back with the rest of the clock drivers.

The MAINTAINERS for kendryte pinctrl is also fixed since it has always been wrong.

---

 MAINTAINERS                                    |  4 ++--
 drivers/clk/Kconfig                            | 14 +++++++++++++-
 drivers/clk/Makefile                           |  2 +-
 drivers/clk/{kendryte/clk.c => clk_kendryte.c} |  0
 drivers/clk/kendryte/Kconfig                   | 12 ------------
 drivers/clk/kendryte/Makefile                  |  1 -
 6 files changed, 16 insertions(+), 17 deletions(-)
 rename drivers/clk/{kendryte/clk.c => clk_kendryte.c} (100%)
 delete mode 100644 drivers/clk/kendryte/Kconfig
 delete mode 100644 drivers/clk/kendryte/Makefile
MAINTAINERS +2 -2
index c6dd9bf838..74a915a269 100644
@@ -995,8 +995,8 @@ M:	Sean Anderson <[email protected]>
 S:	Maintained
 F:	doc/device-tree-bindings/mfd/kendryte,k210-sysctl.txt
 F:	doc/device-tree-bindings/pinctrl/kendryte,k210-fpioa.txt
-F:	drivers/clk/kendryte/
-F:	drivers/pinctrl/kendryte/
+F:	drivers/clk/clk_kendryte.c
+F:	drivers/pinctrl/pinctrl-kendryte.c
 F:	include/kendryte/
 
 RNG
drivers/clk/Kconfig +13 -1
index 4aeaa0cd58..6c61e3019f 100644
@@ -159,11 +159,23 @@ config CLK_SCMI
 	  by a SCMI agent based on SCMI clock protocol communication
 	  with a SCMI server.
 
+config CLK_K210
+	bool "Clock support for Kendryte K210"
+	depends on CLK
+	help
+	  This enables support clock driver for Kendryte K210 platforms.
+
+config CLK_K210_SET_RATE
+	bool "Enable setting the Kendryte K210 PLL rate"
+	depends on CLK_K210
+	help
+	  Add functionality to calculate new rates for K210 PLLs. Enabling this
+	  feature adds around 1K to U-Boot's final size.
+
 source "drivers/clk/analogbits/Kconfig"
 source "drivers/clk/at91/Kconfig"
 source "drivers/clk/exynos/Kconfig"
 source "drivers/clk/imx/Kconfig"
-source "drivers/clk/kendryte/Kconfig"
 source "drivers/clk/meson/Kconfig"
 source "drivers/clk/microchip/Kconfig"
 source "drivers/clk/mvebu/Kconfig"
drivers/clk/Makefile +1 -1
index 645709b855..f06164bb49 100644
@@ -28,7 +28,7 @@ obj-$(CONFIG_CLK_BOSTON) += clk_boston.o
 obj-$(CONFIG_CLK_EXYNOS) += exynos/
 obj-$(CONFIG_$(SPL_TPL_)CLK_INTEL) += intel/
 obj-$(CONFIG_CLK_HSDK) += clk-hsdk-cgu.o
-obj-$(CONFIG_CLK_K210) += kendryte/
+obj-$(CONFIG_CLK_K210) += clk_kendryte.o
 obj-$(CONFIG_CLK_MPC83XX) += mpc83xx_clk.o
 obj-$(CONFIG_CLK_MPFS) += microchip/
 obj-$(CONFIG_CLK_OCTEON) += clk_octeon.o
drivers/clk/clk_kendryte.c renamed from drivers/clk/kendryte/clk.c +0 -0
similarity index 100%
rename from drivers/clk/kendryte/clk.c
rename to drivers/clk/clk_kendryte.c
drivers/clk/kendryte/Kconfig deleted +0 -12
deleted file mode 100644
index 0dc8e3f889..0000000000
@@ -1,12 +0,0 @@
-config CLK_K210
-	bool "Clock support for Kendryte K210"
-	depends on CLK
-	help
-	  This enables support clock driver for Kendryte K210 platforms.
-
-config CLK_K210_SET_RATE
-	bool "Enable setting the Kendryte K210 PLL rate"
-	depends on CLK_K210
-	help
-	  Add functionality to calculate new rates for K210 PLLs. Enabling this
-	  feature adds around 1K to U-Boot's final size.
drivers/clk/kendryte/Makefile deleted +0 -1
deleted file mode 100644
index 0303c0b99c..0000000000
@@ -1 +0,0 @@
-obj-y += clk.o
Signature · 1 line
2.31.0
#11 lore raw

PATCH 9/11 k210: dts: Set PLL1 to the same rate as PLL0

In reply to Sean Anderson

Linux has had some stability issues when using AISRAM with a different frequency from SRAM. Mirror their change here now that we relocate into AISRAM.

---

 arch/riscv/dts/k210.dtsi | 2 ++
 1 file changed, 2 insertions(+)
arch/riscv/dts/k210.dtsi +2 -0
index 2492af8038..8bcd3cebde 100644
@@ -501,6 +501,8 @@
 					#clock-cells = <1>;
 					compatible = "kendryte,k210-clk";
 					clocks = <&in0>;
+					assigned-clocks = <&sysclk K210_CLK_PLL1>;
+					assigned-clock-rates = <390000000>;
 					u-boot,dm-pre-reloc;
 				};
 
Signature · 1 line
2.31.0
#12 lore raw

PATCH 10/11 k210: Don't imply CCF

In reply to Sean Anderson

Now that the k210 clock driver does not depend on CCF, we should no longer imply it (and probably should not have in the first place). We can also reduce the pre-relocation malloc arena back to something sensible.

---

 board/sipeed/maix/Kconfig          | 2 --
 configs/sipeed_maix_bitm_defconfig | 1 -
 2 files changed, 3 deletions(-)
board/sipeed/maix/Kconfig +0 -2
index adf6abb572..b1d7a7ad93 100644
@@ -37,8 +37,6 @@ config BOARD_SPECIFIC_OPTIONS
 	imply SIFIVE_CLINT
 	imply POWER_DOMAIN
 	imply SIMPLE_PM_BUS
-	imply CLK_CCF
-	imply CLK_COMPOSITE_CCF
 	imply CLK_K210
 	imply DM_RESET
 	imply RESET_SYSCON
configs/sipeed_maix_bitm_defconfig +0 -1
index bd877cd055..4903476961 100644
@@ -1,5 +1,4 @@
 CONFIG_RISCV=y
-CONFIG_SYS_MALLOC_F_LEN=0x10000
 CONFIG_ENV_SIZE=0x1000
 CONFIG_ENV_OFFSET=0xfff000
 CONFIG_ENV_SECT_SIZE=0x1000
Signature · 1 line
2.31.0
#13 lore raw

PATCH 11/11 test: Add K210 PLL tests to sandbox defconfigs

In reply to Sean Anderson

This adds the unit test for the K210 PLL to the sandbox defconfigs.

---

 configs/sandbox64_defconfig        | 2 ++
 configs/sandbox_defconfig          | 2 ++
 configs/sandbox_flattree_defconfig | 2 ++
 3 files changed, 6 insertions(+)
configs/sandbox64_defconfig +2 -0
index 4648808d51..cbaf80a004 100644
@@ -106,6 +106,8 @@ CONFIG_AXI_SANDBOX=y
 CONFIG_BUTTON=y
 CONFIG_BUTTON_GPIO=y
 CONFIG_CLK=y
+CONFIG_CLK_K210=y
+CONFIG_CLK_K210_SET_RATE=y
 CONFIG_CPU=y
 CONFIG_DM_DEMO=y
 CONFIG_DM_DEMO_SIMPLE=y
configs/sandbox_defconfig +2 -0
index 5da8d1679e..c44843f0b8 100644
@@ -129,6 +129,8 @@ CONFIG_BUTTON_GPIO=y
 CONFIG_CLK=y
 CONFIG_CLK_COMPOSITE_CCF=y
 CONFIG_CLK_SCMI=y
+CONFIG_CLK_K210=y
+CONFIG_CLK_K210_SET_RATE=y
 CONFIG_SANDBOX_CLK_CCF=y
 CONFIG_CPU=y
 CONFIG_DM_DEMO=y
configs/sandbox_flattree_defconfig +2 -0
index b68f938cb3..5dce6b5523 100644
@@ -86,6 +86,8 @@ CONFIG_AXI=y
 CONFIG_AXI_SANDBOX=y
 CONFIG_CLK=y
 CONFIG_CLK_COMPOSITE_CCF=y
+CONFIG_CLK_K210=y
+CONFIG_CLK_K210_SET_RATE=y
 CONFIG_SANDBOX_CLK_CCF=y
 CONFIG_CPU=y
 CONFIG_DM_DEMO=y
Signature · 1 line
2.31.0
#14 lore raw

PATCH 11/11 test: Add K210 PLL tests to sandbox defconfigs

Quoted 10 lines · Sean Anderson

This adds the unit test for the K210 PLL to the sandbox defconfigs.

---

 configs/sandbox64_defconfig        | 2 ++
 configs/sandbox_defconfig          | 2 ++
 configs/sandbox_flattree_defconfig | 2 ++
 3 files changed, 6 insertions(+)