[PATCH v5 1/6] clk: correct clk_div_mask() return value for width == 32
Junhui Liu
junhui.liu at pigmoral.tech
Thu May 14 02:27:17 PDT 2026
The macro clk_div_mask() currently wraps to zero when width is 32 due to
1 << 32 being undefined behavior. This leads to incorrect mask generation
and prevents correct retrieval of register field values for 32-bit-wide
dividers.
Although it is unlikely to exhaust all U32_MAX div, some clock IPs may rely
on a 32-bit val entry in their div_table to match a div, so providing a
full 32-bit mask is necessary.
Fix this by using the standard GENMASK() macro. This safely resolves the
undefined behavior on both 32-bit and 64-bit architectures, while also
benefiting from the built-in compile-time type and bounds checking
provided by the GENMASK() macro.
Cc: Troy Mitchell <troy.mitchell at linux.spacemit.com>
Cc: Brian Masney <bmasney at redhat.com>
Signed-off-by: Junhui Liu <junhui.liu at pigmoral.tech>
---
Hi Troy and Brian, I dropped your Reviewed-by tags in this version
because the implementation has changed (to use GENMASK()) and requires
re-evaluation IMO.
---
include/linux/clk-provider.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index b01a38fef8cf..b986cc054d82 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -6,6 +6,7 @@
#ifndef __LINUX_CLK_PROVIDER_H
#define __LINUX_CLK_PROVIDER_H
+#include <linux/bits.h>
#include <linux/of.h>
#include <linux/of_clk.h>
@@ -714,7 +715,7 @@ struct clk_divider {
spinlock_t *lock;
};
-#define clk_div_mask(width) ((1 << (width)) - 1)
+#define clk_div_mask(width) GENMASK((width) - 1, 0)
#define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
#define CLK_DIVIDER_ONE_BASED BIT(0)
--
2.54.0
More information about the linux-riscv
mailing list