clk: k210: Rewrite K210 clock without CCF
Subsystems (5)
v3 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 because each funcion is implemented in one place.
* 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.
text data bss dec hex filename
292485 32672 12624 337781 52775 before/u-boot 283125 29856 12624 325605 4f7e5 after/u-boot
This series depends on
https://patchwork.ozlabs.org/project/uboot/list/?series=238211
Changes in v3:
- Always define clk_defaults_stage, even if clk_set_defaults is a dummy
- Fix inverted condition for setting defaults
- Fix val not being set for K210_DIV_POWER
- Add CLK_K210_SET_RATE to defconfig so these changes apply
Changes in v2:
- Convert stage to enum
- Only force probe clocks pre-reloc
- Rebase on u-boot/master
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 | 2 +- drivers/clk/Kconfig | 14 +- drivers/clk/Makefile | 2 +- drivers/clk/clk-uclass.c | 27 +- drivers/clk/clk_kendryte.c | 1320 +++++++++++++++++++++++ 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 ---------- drivers/clk/rockchip/clk_rk3308.c | 2 +- drivers/core/device.c | 2 +- drivers/net/gmac_rockchip.c | 2 +- include/clk.h | 30 +- include/dt-bindings/clock/k210-sysctl.h | 94 +- include/kendryte/bypass.h | 31 - include/kendryte/clk.h | 35 - include/kendryte/pll.h | 34 - 24 files changed, 1437 insertions(+), 1711 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
PATCH v3 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, extend the stage parameter to allow force setting defaults, even if they would be otherwise postponed 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.
---
Changes in v3:
- Always define clk_defaults_stage, even if clk_set_defaults is a dummy
Changes in v2:
- Convert stage to enum
drivers/clk/clk-uclass.c | 27 +++++++++++++++++---------- drivers/clk/rockchip/clk_rk3308.c | 2 +- drivers/core/device.c | 2 +- drivers/net/gmac_rockchip.c | 2 +- include/clk.h | 30 ++++++++++++++++++++++++++---- 5 files changed, 46 insertions(+), 17 deletions(-)
drivers/clk/clk-uclass.c +17 -10
@@ -207,7 +207,8 @@ static struct clk *clk_set_default_get_by_id(struct clk *clk)return c; } -static int clk_set_default_parents (struct udevice *dev,int stage) +static int clk_set_default_parents (struct udevice *dev, +enum clk_defaults_stage stage) {struct clk clk, parent_clk, *c, *p;int index;
@@ -251,10 +252,10 @@ static int clk_set_default_parents(struct udevice *dev, int stage)* It cannot be done right now but need to wait after the * device is probed */ -if (stage ==0 && clk.dev == dev) +if (stage ==CLK_DEFAULTS_PRE && clk.dev == dev)continue ; -if (stage >0 && clk.dev != dev) +if (stage !=CLK_DEFAULTS_PRE && clk.dev != dev)/* do not setup twice the parent clocks */ continue ;
@@ -280,7 +281,8 @@ static int clk_set_default_parents(struct udevice *dev, int stage)return 0 ; } -static int clk_set_default_rates (struct udevice *dev,int stage) +static int clk_set_default_rates (struct udevice *dev, +enum clk_defaults_stage stage) {struct clk clk, *c;int index;
@@ -320,10 +322,10 @@ static int clk_set_default_rates(struct udevice *dev, int stage)* It cannot be done right now but need to wait after the * device is probed */ -if (stage ==0 && clk.dev == dev) +if (stage ==CLK_DEFAULTS_PRE && clk.dev == dev)continue ; -if (stage >0 && clk.dev != dev) +if (stage !=CLK_DEFAULTS_PRE && clk.dev != dev)/* do not setup twice the parent clocks */ continue ;
@@ -346,16 +348,21 @@ fail:return ret; } -int clk_set_defaults (struct udevice *dev,int stage) +int clk_set_defaults (struct udevice *dev,enum clk_defaults_stage stage) {int ret;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 !=CLK_DEFAULTS_POST_FORCE ) +return 0 ;debug ("%s(%s)\n" , __func__,dev_read_name (dev));
@@ -805,7 +812,7 @@ int clk_uclass_post_probe(struct udevice *dev)* where the DT is used to setup default parents and rates * using assigned-clocks */ -clk_set_defaults (dev,1 ); +clk_set_defaults (dev,CLK_DEFAULTS_POST );return 0 ; }
drivers/clk/rockchip/clk_rk3308.c +1 -1
@@ -1014,7 +1014,7 @@ static int rk3308_clk_probe(struct udevice *dev)rk3308_clk_init (dev);/* Process 'assigned-{clocks/clock-parents/clock-rates}' properties */ - ret =clk_set_defaults (dev,1 ); + ret =clk_set_defaults (dev,CLK_DEFAULTS_POST );if (ret)debug ("%s clk_set_defaults failed %d\n" , __func__, ret);
drivers/core/device.c +1 -1
@@ -561,7 +561,7 @@ int device_probe(struct udevice *dev)* Process 'assigned-{clocks/clock-parents/clock-rates}' * properties */ - ret =clk_set_defaults (dev,0 ); + ret =clk_set_defaults (dev,CLK_DEFAULTS_PRE );if (ret) goto fail; }
drivers/net/gmac_rockchip.c +1 -1
@@ -565,7 +565,7 @@ static int gmac_rockchip_probe(struct udevice *dev)ulong rate;int ret; - ret =clk_set_defaults (dev,0 ); + ret =clk_set_defaults (dev,CLK_DEFAULTS_PRE );if (ret)debug ("%s clk_set_defaults failed %d\n" , __func__, ret);
include/clk.h +26 -4
@@ -277,19 +277,41 @@ static inline int clk_release_all(struct clk *clk, int count) }#endif +/** +* enum clk_defaults_stage - What stage clk_set_defaults() is called at +* @CLK_DEFAULTS_PRE: Called before probe. Setting of defaults for clocks owned +* by this clock driver will be defered until after probing. +* @CLK_DEFAULTS_POST: Called after probe. Only defaults for clocks owned by +* this clock driver will be set. +* @CLK_DEFAULTS_POST_FORCE: Called after probe, and always set defaults, even +* before relocation. Usually, defaults are not set +* pre-relocation to avoid setting them twice (when +* the device is probed again post-relocation). This +* may incur a performance cost as device tree +* properties must be parsed for a second time. +* However, when not using SPL, pre-relocation may be +* the only time we can set defaults for some clocks +* (such as those used for the RAM we will relocate +* into). +*/ +enum clk_defaults_stage { +CLK_DEFAULTS_PRE =0 , +CLK_DEFAULTS_POST =1 , +CLK_DEFAULTS_POST_FORCE , +}; +#if (CONFIG_IS_ENABLED (OF_CONTROL ) && !CONFIG_IS_ENABLED (OF_PLATDATA )) && \CONFIG_IS_ENABLED (CLK ) +/** * clk_set_defaults - Process 'assigned-{clocks/clock-parents/clock-rates}' * properties to configure clocks * * @dev: A device to process (the ofnode associated with this device * 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 +* @stage: The stage of the probing process this function is called during. */ -int clk_set_defaults (struct udevice *dev,int stage); +int clk_set_defaults (struct udevice *dev,enum clk_defaults_stage stage);#else static inline int clk_set_defaults (struct udevice *dev,int stage) {
Signature · 1 line
2.31.0
PATCH v3 2/11 clk: k210: Rewrite to remove CCF
In reply to Sean AndersonThis is effectively a complete rewrite to remove all dependency on CCF. The code is now smaller, and so is the binary. 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 :)
---
(no changes since v1)
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
@@ -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
@@ -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
Re: [PATCH v3 02/11] clk: k210: Rewrite to remove CCF
Quoted 24 lines · Sean Anderson
This is effectively a complete rewrite to remove all dependency on CCF. The code is now smaller, and so is the binary. 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 :)
Signed-off-by Sean Anderson <[email protected]>---
Quoted 8 lines
(no changes since v1)
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(-)
PATCH v3 3/11 clk: k210: Move pll into the rest of the driver
In reply to Sean AndersonNow 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.
---
(no changes since v1)
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
@@ -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
@@ -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
@@ -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
Re: [PATCH v3 03/11] clk: k210: Move pll into the rest of the driver
Quoted 10 lines · 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.
Signed-off-by Sean Anderson <[email protected]>---
(no changes since v1)
Quoted 8 lines
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
PATCH v3 4/11 clk: k210: Implement soc_clk_dump
In reply to Sean AndersonSince we are no longer using CCF we cannot use the default soc_clk_dump. Instead, implement our own.
---
(no changes since v1)
drivers/clk/kendryte/clk.c | 68 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-)
drivers/clk/kendryte/clk.c +66 -2
@@ -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
Re: [PATCH v3 04/11] clk: k210: Implement soc_clk_dump
Quoted 10 lines · Sean Anderson
Since we are no longer using CCF we cannot use the default
soc_clk_dump. Instead, implement our own.Signed-off-by Sean Anderson <[email protected]>---
(no changes since v1)
drivers/clk/kendryte/clk.c | 68 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-)
PATCH v3 5/11 clk: k210: Re-add support for setting rate
In reply to Sean AndersonThis 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).
---
Changes in v3:
- Fix inverted condition for setting defaults
- Fix val not being set for K210_DIV_POWER
Changes in v2:
- Only force probe clocks pre-reloc
drivers/clk/kendryte/clk.c | 89 +++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 5 deletions(-)
drivers/clk/kendryte/clk.c +84 -5
@@ -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 ; + + val =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,13 @@ 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. +*/ +if (!(gd->flags &GD_FLG_RELOC )) +clk_set_defaults (dev,CLK_DEFAULTS_POST_FORCE ); +return 0 ; }
Signature · 1 line
2.31.0
Re: [PATCH v3 05/11] clk: k210: Re-add support for setting rate
Quoted 20 lines · 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).Signed-off-by Sean Anderson <[email protected]>---
Changes in v3:
- Fix inverted condition for setting defaults
- Fix val not being set forK210_DIV_POWER
Quoted 5 lines
Changes in v2:
- Only force probe clocks pre-relocdrivers/clk/kendryte/clk.c | 89 +++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 5 deletions(-)
PATCH v3 6/11 clk: k210: Don't set PLL rates if we are already at the correct rate
In reply to Sean AndersonThis speeds up boot by preventing multiple reconfigurations of the PLLs.
---
(no changes since v1)
drivers/clk/kendryte/clk.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/clk/kendryte/clk.c +8 -7
@@ -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 at 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
Re: [PATCH v3 06/11] clk: k210: Don't set PLL rates if we are already at the correct rate
Quoted 9 lines · Sean Anderson
This speeds up boot by preventing multiple reconfigurations of the PLLs.
Signed-off-by Sean Anderson <[email protected]>---
(no changes since v1)
drivers/clk/kendryte/clk.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
PATCH v3 7/11 clk: k210: Remove bypass driver
In reply to Sean AndersonThis driver no longer serves a purpose now that we have moved away from CCF. Drop it.
---
(no changes since v1)
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
@@ -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
@@ -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
Re: [PATCH v3 07/11] clk: k210: Remove bypass driver
Quoted 6 lines · Sean Anderson
This driver no longer serves a purpose now that we have moved away from CCF. Drop it.
Signed-off-by Sean Anderson <[email protected]>---
Quoted 9 lines
(no changes since v1)
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
PATCH v3 8/11 clk: k210: Move k210 clock out of its own subdirectory
In reply to Sean AndersonNow 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.
---
(no changes since v1)
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/MakefileMAINTAINERS +2 -2
@@ -999,8 +999,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
@@ -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
@@ -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
drivers/clk/kendryte/Kconfig deleted +0 -12
@@ -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
@@ -1 +0,0 @@ -obj-y += clk.o
Signature · 1 line
2.31.0
Re: [PATCH v3 08/11] clk: k210: Move k210 clock out of its own subdirectory
Quoted 11 lines · 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
MAINTAINERSfor kendryte pinctrl is also fixed since it has always been wrong.Signed-off-by Sean Anderson <[email protected]>---
(no changes since v1)
Quoted 10 lines
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/MakefilePATCH v3 9/11 k210: dts: Set PLL1 to the same rate as PLL0
In reply to Sean AndersonLinux has had some stability issues when using AISRAM with a different frequency from SRAM. Mirror their change here now that we relocate into AISRAM.
---
Changes in v3:
- Add CLK_K210_SET_RATE to defconfig so these changes apply
arch/riscv/dts/k210.dtsi | 2 ++ configs/sipeed_maix_bitm_defconfig | 1 + 2 files changed, 3 insertions(+)
arch/riscv/dts/k210.dtsi +2 -0
@@ -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; };
configs/sipeed_maix_bitm_defconfig +1 -0
@@ -13,6 +13,7 @@ CONFIG_HUSH_PARSER=y CONFIG_MTDIDS_DEFAULT="nor0=spi3:0" CONFIG_MTDPARTS_DEFAULT="nor0:1M(u-boot),0x1000@0xfff000(env)" # CONFIG_NET is not set +CONFIG_CLK_K210_SET_RATE=y # CONFIG_INPUT is not set CONFIG_SF_DEFAULT_BUS=3 # CONFIG_DM_ETH is not set
Signature · 1 line
2.31.0
Re: [PATCH v3 09/11] k210: dts: Set PLL1 to the same rate as PLL0
Quoted 5 lines · 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.
Signed-off-by Sean Anderson <[email protected]>
Quoted 8 lines
---
Changes in v3:
- AddCLK_K210_SET_RATEto defconfig so these changes applyarch/riscv/dts/k210.dtsi | 2 ++ configs/sipeed_maix_bitm_defconfig | 1 + 2 files changed, 3 insertions(+)
PATCH v3 10/11 k210: Don't imply CCF
In reply to Sean AndersonNow 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.
---
(no changes since v1)
board/sipeed/maix/Kconfig | 2 -- configs/sipeed_maix_bitm_defconfig | 1 - 2 files changed, 3 deletions(-)
board/sipeed/maix/Kconfig +0 -2
@@ -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
@@ -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
Re: [PATCH v3 10/11] k210: Don't imply CCF
Quoted 4 lines · 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.
Quoted 9 lines
Signed-off-by Sean Anderson <[email protected]>---
(no changes since v1)
board/sipeed/maix/Kconfig | 2 -- configs/sipeed_maix_bitm_defconfig | 1 - 2 files changed, 3 deletions(-)
PATCH v3 11/11 test: Add K210 PLL tests to sandbox defconfigs
In reply to Sean AndersonThis adds the unit test for the K210 PLL to the sandbox defconfigs.
---
(no changes since v1)
configs/sandbox64_defconfig | 2 ++ configs/sandbox_defconfig | 2 ++ configs/sandbox_flattree_defconfig | 2 ++ 3 files changed, 6 insertions(+)
configs/sandbox64_defconfig +2 -0
@@ -107,6 +107,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
@@ -130,6 +130,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
@@ -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
Quoted 31 lines · On Fri, 11 Jun 2021 00:16:06 -0400 Sean Anderson <[email protected]> wrote:
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
_halfclocks 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 because each funcion is implemented in one place.
* 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 variousregister()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.
Quoted 4 lines
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.
You mean for U-Boot or for Linux ?
Quoted 7 lines
Overall there is a total savings of 12k from this series.
text data bss dec
hex filename 292485 32672 12624
337781 52775 before/u-boot 283125
29856 12624 325605 4f7e5 after/u-boot
I'm not going to defend CCF to the last breath, I know their issues.
However, the goal for CCF was to have:
1. Ported code from Linux (with some code simplification)
2. Get the standard approach to the clock subsystem.
I'm just wondering if we (as a community) want to have such diversity - I mean each architecture would have different clock driver (under the device model)
As fair as I can see - the K210 already has support for CCF in Linux:drivers/clk/clk-k210.c
Reading the above comment - it looks like you couldn't simplyfy this Linux driver to be smaller and fit into U-Boot?
Sean, do you think that other archs can benefit from your work?
Quoted 53 lines
This series depends on
https://patchwork.ozlabs.org/project/uboot/list/?series=238211Changes in v3:
- Always defineclk_defaults_stage, even ifclk_set_defaultsis a dummy
- Fix inverted condition for setting defaults
- Fix val not being set forK210_DIV_POWER
- AddCLK_K210_SET_RATEto defconfig so these changes applyChanges in v2:
- Convert stage to enum
- Only force probe clocks pre-reloc
- Rebase on u-boot/masterSean 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: Implementsoc_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 defconfigsMAINTAINERS | 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 | 2 +- drivers/clk/Kconfig | 14 +- drivers/clk/Makefile | 2 +- drivers/clk/clk-uclass.c | 27 +- drivers/clk/clk_kendryte.c | 1320 +++++++++++++++++++++++ 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 ---------- drivers/clk/rockchip/clk_rk3308.c | 2 +- drivers/core/device.c | 2 +- drivers/net/gmac_rockchip.c | 2 +- include/clk.h | 30 +- include/dt-bindings/clock/k210-sysctl.h | 94 +- include/kendryte/bypass.h | 31 - include/kendryte/clk.h | 35 - include/kendryte/pll.h | 34 - 24 files changed, 1437 insertions(+), 1711 deletions(-)
Quoted 9 lines
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
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: [email protected]
Quoted 34 lines · Lukasz Majewski
Quoted 31 lines · On Fri, 11 Jun 2021 00:16:06 -0400 Sean Anderson <[email protected]> wrote:
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
_halfclocks 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 because each funcion is implemented in one place.
* 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 variousregister()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.
Quoted 6 lines
Quoted 4 lines
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.
You mean for U-Boot or for Linux ?
For U-Boot. I wasn't aware that the CCF was mainly designed for porting drivers from Linux.
Quoted 11 lines
Quoted 8 lines
Overall there is a total savings of 12k from this series.
text data bss dec
hex filename 292485 32672 12624
337781 52775 before/u-boot 283125
29856 12624 325605 4f7e5 after/u-bootI'm not going to defend CCF to the last breath, I know their issues.
Quoted 8 lines
However, the goal for CCF was to have:
1. Ported code from Linux (with some code simplification)
2. Get the standard approach to the clock subsystem.I'm just wondering if we (as a community) want to have such diversity - I mean each architecture would have different clock driver (under the device model)
If there is already a CCF driver for that arch for Linux, then I think reusing it is OK. However, I think it generally tends toward larger drivers, larger runtime memory usage, and awkward design. So if you are writing a new driver for U-Boot, I think it is better to not use CCF drivers.
Quoted 3 lines
As fair as I can see - the K210 already has support for CCF in Linux:
drivers/clk/clk-k210.c
Yes, but it does not really "use" the CCF. For example, things like composite clocks are absent; all calculations of rate are internal to the driver. In fact, I took inspiration from Damien's handling of this driver in Linux.
Quoted 2 lines
Reading the above comment - it looks like you couldn't simplyfy this Linux driver to be smaller and fit into U-Boot?
Well, this driver takes major inspiration from the Linux driver, which was itself based on this code. So in a sense, this is already a simplification of the Linux driver.
Quoted 2 lines
Sean, do you think that other archs can benefit from your work?
Hm, I don't know. I don't think there is must code which could be reused. Some of the major savings in disabling CCF were from disabling imx-specific clocks which are always enabled, even though they are not designed to be used outside of that arch.
In terms of general improvements, I think that get_parent() should be part of clk_ops. That would allow me to use the existing clock dump code. Additionally, I think that enable_count has no business living in struct clk, as that struct is a "thick pointer" and multiple ones can exist for the same clock.
--Sean
Quoted 76 lines
Quoted 54 lines
This series depends on
https://patchwork.ozlabs.org/project/uboot/list/?series=238211Changes in v3:
- Always defineclk_defaults_stage, even ifclk_set_defaultsis a dummy
- Fix inverted condition for setting defaults
- Fix val not being set forK210_DIV_POWER
- AddCLK_K210_SET_RATEto defconfig so these changes applyChanges in v2:
- Convert stage to enum
- Only force probe clocks pre-reloc
- Rebase on u-boot/masterSean 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: Implementsoc_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 defconfigsMAINTAINERS | 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 | 2 +- drivers/clk/Kconfig | 14 +- drivers/clk/Makefile | 2 +- drivers/clk/clk-uclass.c | 27 +- drivers/clk/clk_kendryte.c | 1320 +++++++++++++++++++++++ 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 ---------- drivers/clk/rockchip/clk_rk3308.c | 2 +- drivers/core/device.c | 2 +- drivers/net/gmac_rockchip.c | 2 +- include/clk.h | 30 +- include/dt-bindings/clock/k210-sysctl.h | 94 +- include/kendryte/bypass.h | 31 - include/kendryte/clk.h | 35 - include/kendryte/pll.h | 34 - 24 files changed, 1437 insertions(+), 1711 deletions(-)Quoted 9 lines
create mode 100644
drivers/clk/clk_kendryte.cdelete mode 100644drivers/clk/kendryte/Kconfigdelete mode 100644drivers/clk/kendryte/Makefiledelete mode 100644drivers/clk/kendryte/bypass.cdelete mode 100644drivers/clk/kendryte/clk.cdelete mode 100644drivers/clk/kendryte/pll.cdelete mode 100644include/kendryte/bypass.hdelete mode 100644include/kendryte/clk.hBest 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: [email protected]
Quoted 72 lines · Sean Anderson
Quoted 34 lines · Lukasz Majewski
Quoted 31 lines · On Fri, 11 Jun 2021 00:16:06 -0400 Sean Anderson <[email protected]> wrote:
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
_halfclocks 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 because each funcion is implemented in one place.
* 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 variousregister()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.Quoted 6 lines
Quoted 4 lines
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.
You mean for U-Boot or for Linux ?
For U-Boot. I wasn't aware that the CCF was mainly designed for porting drivers from Linux.
Quoted 11 lines
Quoted 8 lines
Overall there is a total savings of 12k from this series.
text data bss dec
hex filename 292485 32672 12624
337781 52775 before/u-boot 283125
29856 12624 325605 4f7e5 after/u-bootI'm not going to defend CCF to the last breath, I know their issues.
Quoted 8 lines
However, the goal for CCF was to have:
1. Ported code from Linux (with some code simplification)
2. Get the standard approach to the clock subsystem.I'm just wondering if we (as a community) want to have such diversity - I mean each architecture would have different clock driver (under the device model)
If there is already a CCF driver for that arch for Linux, then I think reusing it is OK. However, I think it generally tends toward larger drivers, larger runtime memory usage, and awkward design. So if you are writing a new driver for U-Boot, I think it is better to not use CCF drivers.
Quoted 1 line
Quoted 7 lines
Quoted 2 lines
As fair as I can see - the K210 already has support for CCF in Linux:
drivers/clk/clk-k210.cYes, but it does not really "use" the CCF. For example, things like composite clocks are absent; all calculations of rate are internal to the driver. In fact, I took inspiration from Damien's handling of this driver in Linux.
Correct. The initial development versions of the driver were inspired (hmmm... copied) from Sean CCF based driver, but the composite clocks and some others were a real pain due to the weird divisors that the K210 clocks use. In the end, I decided to not use Linux CCF, or rather, to limit its use to simple clocks only and to implement internally all divisors for the clocks. This ended up simplifying the code a lot. That is what went upstream.
Quoted 102 lines
Quoted 3 lines
Reading the above comment - it looks like you couldn't simplyfy this Linux driver to be smaller and fit into U-Boot?
Well, this driver takes major inspiration from the Linux driver, which was itself based on this code. So in a sense, this is already a simplification of the Linux driver.
Quoted 2 lines
Sean, do you think that other archs can benefit from your work?
Hm, I don't know. I don't think there is must code which could be reused. Some of the major savings in disabling CCF were from disabling imx-specific clocks which are always enabled, even though they are not designed to be used outside of that arch.
In terms of general improvements, I think that
get_parent()should be part ofclk_ops. That would allow me to use the existing clock dump code. Additionally, I think thatenable_counthas no business living in struct clk, as that struct is a "thick pointer" and multiple ones can exist for the same clock.--Sean
Quoted 76 lines
Quoted 54 lines
This series depends on
https://patchwork.ozlabs.org/project/uboot/list/?series=238211Changes in v3:
- Always defineclk_defaults_stage, even ifclk_set_defaultsis a dummy
- Fix inverted condition for setting defaults
- Fix val not being set forK210_DIV_POWER
- AddCLK_K210_SET_RATEto defconfig so these changes applyChanges in v2:
- Convert stage to enum
- Only force probe clocks pre-reloc
- Rebase on u-boot/masterSean 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: Implementsoc_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 defconfigsMAINTAINERS | 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 | 2 +- drivers/clk/Kconfig | 14 +- drivers/clk/Makefile | 2 +- drivers/clk/clk-uclass.c | 27 +- drivers/clk/clk_kendryte.c | 1320 +++++++++++++++++++++++ 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 ---------- drivers/clk/rockchip/clk_rk3308.c | 2 +- drivers/core/device.c | 2 +- drivers/net/gmac_rockchip.c | 2 +- include/clk.h | 30 +- include/dt-bindings/clock/k210-sysctl.h | 94 +- include/kendryte/bypass.h | 31 - include/kendryte/clk.h | 35 - include/kendryte/pll.h | 34 - 24 files changed, 1437 insertions(+), 1711 deletions(-)Quoted 9 lines
create mode 100644
drivers/clk/clk_kendryte.cdelete mode 100644drivers/clk/kendryte/Kconfigdelete mode 100644drivers/clk/kendryte/Makefiledelete mode 100644drivers/clk/kendryte/bypass.cdelete mode 100644drivers/clk/kendryte/clk.cdelete mode 100644drivers/clk/kendryte/pll.cdelete mode 100644include/kendryte/bypass.hdelete mode 100644include/kendryte/clk.hBest 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: [email protected]
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 91 lines · Sean Anderson
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
_halfclocks 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 because each funcion is implemented in one place.
* 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 variousregister()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.
text data bss dec hex filename292485 32672 12624 337781 52775 before/u-boot 283125 29856 12624 325605 4f7e5 after/u-bootThis series depends on
https://patchwork.ozlabs.org/project/uboot/list/?series=238211Changes in v3:
- Always defineclk_defaults_stage, even ifclk_set_defaultsis a dummy
- Fix inverted condition for setting defaults
- Fix val not being set forK210_DIV_POWER
- AddCLK_K210_SET_RATEto defconfig so these changes applyChanges in v2:
- Convert stage to enum
- Only force probe clocks pre-reloc
- Rebase on u-boot/masterSean 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: Implementsoc_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 defconfigsMAINTAINERS | 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 | 2 +- drivers/clk/Kconfig | 14 +- drivers/clk/Makefile | 2 +- drivers/clk/clk-uclass.c | 27 +- drivers/clk/clk_kendryte.c | 1320 +++++++++++++++++++++++ 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 ---------- drivers/clk/rockchip/clk_rk3308.c | 2 +- drivers/core/device.c | 2 +- drivers/net/gmac_rockchip.c | 2 +- include/clk.h | 30 +- include/dt-bindings/clock/k210-sysctl.h | 94 +- include/kendryte/bypass.h | 31 - include/kendryte/clk.h | 35 - include/kendryte/pll.h | 34 - 24 files changed, 1437 insertions(+), 1711 deletions(-)
Quoted 9 lines
create mode 100644
drivers/clk/clk_kendryte.cdelete mode 100644drivers/clk/kendryte/Kconfigdelete mode 100644drivers/clk/kendryte/Makefiledelete mode 100644drivers/clk/kendryte/bypass.cdelete mode 100644drivers/clk/kendryte/clk.cdelete mode 100644drivers/clk/kendryte/pll.cdelete mode 100644include/kendryte/bypass.hdelete mode 100644include/kendryte/clk.h
passing CI: https://dev.azure.com/seanga2/u-boot/_build/results?buildId=70&view=results