1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019 DENX Software Engineering
12 #define LOG_CATEGORY UCLASS_CLK
16 #include <clk-uclass.h>
18 #include <dm/device.h>
19 #include <dm/devres.h>
20 #include <dm/uclass.h>
22 #include <dm/device_compat.h>
23 #include <dm/device-internal.h>
24 #include <linux/bug.h>
25 #include <linux/clk-provider.h>
26 #include <linux/err.h>
27 #include <linux/log2.h>
30 #include <linux/printk.h>
33 #define UBOOT_DM_CLK_CCF_DIVIDER "ccf_clk_divider"
35 unsigned int clk_divider_get_table_div(const struct clk_div_table *table,
38 const struct clk_div_table *clkt;
40 for (clkt = table; clkt->div; clkt++)
46 static unsigned int _get_div(const struct clk_div_table *table,
47 unsigned int val, unsigned long flags, u8 width)
49 if (flags & CLK_DIVIDER_ONE_BASED)
51 if (flags & CLK_DIVIDER_POWER_OF_TWO)
53 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
54 return val ? val : clk_div_mask(width) + 1;
56 return clk_divider_get_table_div(table, val);
60 unsigned long divider_recalc_rate(struct clk *hw, unsigned long parent_rate,
62 const struct clk_div_table *table,
63 unsigned long flags, unsigned long width)
67 div = _get_div(table, val, flags, width);
69 WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
70 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
75 return DIV_ROUND_UP_ULL((u64)parent_rate, div);
78 static ulong clk_divider_recalc_rate(struct clk *clk)
80 struct clk_divider *divider = to_clk_divider(clk);
81 unsigned long parent_rate = clk_get_parent_rate(clk);
84 #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
85 val = divider->io_divider_val;
87 val = readl(divider->reg);
89 val >>= divider->shift;
90 val &= clk_div_mask(divider->width);
92 return divider_recalc_rate(clk, parent_rate, val, divider->table,
93 divider->flags, divider->width);
96 bool clk_divider_is_valid_table_div(const struct clk_div_table *table,
99 const struct clk_div_table *clkt;
101 for (clkt = table; clkt->div; clkt++)
102 if (clkt->div == div)
107 bool clk_divider_is_valid_div(const struct clk_div_table *table,
108 unsigned int div, unsigned long flags)
110 if (flags & CLK_DIVIDER_POWER_OF_TWO)
111 return is_power_of_2(div);
113 return clk_divider_is_valid_table_div(table, div);
117 unsigned int clk_divider_get_table_val(const struct clk_div_table *table,
120 const struct clk_div_table *clkt;
122 for (clkt = table; clkt->div; clkt++)
123 if (clkt->div == div)
128 static unsigned int _get_val(const struct clk_div_table *table,
129 unsigned int div, unsigned long flags, u8 width)
131 if (flags & CLK_DIVIDER_ONE_BASED)
133 if (flags & CLK_DIVIDER_POWER_OF_TWO)
135 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
136 return (div == clk_div_mask(width) + 1) ? 0 : div;
138 return clk_divider_get_table_val(table, div);
141 int divider_get_val(unsigned long rate, unsigned long parent_rate,
142 const struct clk_div_table *table, u8 width,
145 unsigned int div, value;
147 div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
149 if (!clk_divider_is_valid_div(table, div, flags))
152 value = _get_val(table, div, flags, width);
154 return min_t(unsigned int, value, clk_div_mask(width));
157 static ulong clk_divider_set_rate(struct clk *clk, unsigned long rate)
159 struct clk_divider *divider = to_clk_divider(clk);
160 unsigned long parent_rate = clk_get_parent_rate(clk);
164 value = divider_get_val(rate, parent_rate, divider->table,
165 divider->width, divider->flags);
169 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
170 val = clk_div_mask(divider->width) << (divider->shift + 16);
172 val = readl(divider->reg);
173 val &= ~(clk_div_mask(divider->width) << divider->shift);
175 val |= (u32)value << divider->shift;
176 writel(val, divider->reg);
178 return clk_get_rate(clk);
181 const struct clk_ops clk_divider_ops = {
182 .get_rate = clk_divider_recalc_rate,
183 .set_rate = clk_divider_set_rate,
186 static struct clk *_register_divider(struct device *dev, const char *name,
187 const char *parent_name, unsigned long flags,
188 void __iomem *reg, u8 shift, u8 width,
189 u8 clk_divider_flags, const struct clk_div_table *table)
191 struct clk_divider *div;
195 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
196 if (width + shift > 16) {
197 dev_warn(dev, "divider value exceeds LOWORD field\n");
198 return ERR_PTR(-EINVAL);
202 /* allocate the divider */
203 div = kzalloc(sizeof(*div), GFP_KERNEL);
205 return ERR_PTR(-ENOMEM);
207 /* struct clk_divider assignments */
211 div->flags = clk_divider_flags;
213 #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
214 div->io_divider_val = *(u32 *)reg;
217 /* register the clock */
221 ret = clk_register(clk, UBOOT_DM_CLK_CCF_DIVIDER, name, parent_name);
230 struct clk *clk_register_divider(struct device *dev, const char *name,
231 const char *parent_name, unsigned long flags,
232 void __iomem *reg, u8 shift, u8 width,
233 u8 clk_divider_flags)
237 clk = _register_divider(dev, name, parent_name, flags, reg, shift,
238 width, clk_divider_flags, NULL);
240 return ERR_CAST(clk);
244 U_BOOT_DRIVER(ccf_clk_divider) = {
245 .name = UBOOT_DM_CLK_CCF_DIVIDER,
247 .ops = &clk_divider_ops,
248 .flags = DM_FLAG_PRE_RELOC,