blob: 7e8e62feeeeb27ae701ffbffe730155a1837534d [file] [log] [blame]
Lukasz Majewski1d7993d2019-06-24 15:50:45 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2019 DENX Software Engineering
4 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5 *
6 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
7 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
8 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
9 *
10 */
11
Patrick Delaunay560e1e02021-11-19 15:12:07 +010012#define LOG_CATEGORY UCLASS_CLK
13
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020014#include <common.h>
15#include <asm/io.h>
16#include <malloc.h>
17#include <clk-uclass.h>
Patrick Delaunay560e1e02021-11-19 15:12:07 +010018#include <log.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020019#include <dm/device.h>
Simon Glass61b29b82020-02-03 07:36:15 -070020#include <dm/devres.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020021#include <dm/uclass.h>
22#include <dm/lists.h>
Patrick Delaunay560e1e02021-11-19 15:12:07 +010023#include <dm/device_compat.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020024#include <dm/device-internal.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060025#include <linux/bug.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020026#include <linux/clk-provider.h>
Simon Glass61b29b82020-02-03 07:36:15 -070027#include <linux/err.h>
Peng Fanfe69b032019-07-31 07:01:37 +000028#include <linux/log2.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020029#include <div64.h>
30#include <clk.h>
31#include "clk.h"
32
33#define UBOOT_DM_CLK_CCF_DIVIDER "ccf_clk_divider"
34
Dario Binacchi5688f3b2020-12-30 00:06:27 +010035unsigned int clk_divider_get_table_div(const struct clk_div_table *table,
36 unsigned int val)
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020037{
38 const struct clk_div_table *clkt;
39
40 for (clkt = table; clkt->div; clkt++)
41 if (clkt->val == val)
42 return clkt->div;
43 return 0;
44}
45
46static unsigned int _get_div(const struct clk_div_table *table,
47 unsigned int val, unsigned long flags, u8 width)
48{
49 if (flags & CLK_DIVIDER_ONE_BASED)
50 return val;
51 if (flags & CLK_DIVIDER_POWER_OF_TWO)
52 return 1 << val;
53 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
54 return val ? val : clk_div_mask(width) + 1;
55 if (table)
Dario Binacchi5688f3b2020-12-30 00:06:27 +010056 return clk_divider_get_table_div(table, val);
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020057 return val + 1;
58}
59
60unsigned long divider_recalc_rate(struct clk *hw, unsigned long parent_rate,
61 unsigned int val,
62 const struct clk_div_table *table,
63 unsigned long flags, unsigned long width)
64{
65 unsigned int div;
66
67 div = _get_div(table, val, flags, width);
68 if (!div) {
69 WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
70 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
71 clk_hw_get_name(hw));
72 return parent_rate;
73 }
74
75 return DIV_ROUND_UP_ULL((u64)parent_rate, div);
76}
77
78static ulong clk_divider_recalc_rate(struct clk *clk)
79{
Sean Anderson78ce0bd2020-06-24 06:41:06 -040080 struct clk_divider *divider = to_clk_divider(clk);
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020081 unsigned long parent_rate = clk_get_parent_rate(clk);
82 unsigned int val;
83
Lukasz Majewski6bb15d62019-06-24 15:50:48 +020084#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
85 val = divider->io_divider_val;
86#else
87 val = readl(divider->reg);
88#endif
89 val >>= divider->shift;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020090 val &= clk_div_mask(divider->width);
91
92 return divider_recalc_rate(clk, parent_rate, val, divider->table,
93 divider->flags, divider->width);
94}
95
Dario Binacchi5688f3b2020-12-30 00:06:27 +010096bool clk_divider_is_valid_table_div(const struct clk_div_table *table,
97 unsigned int div)
Peng Fanfe69b032019-07-31 07:01:37 +000098{
99 const struct clk_div_table *clkt;
100
101 for (clkt = table; clkt->div; clkt++)
102 if (clkt->div == div)
103 return true;
104 return false;
105}
106
Dario Binacchi5688f3b2020-12-30 00:06:27 +0100107bool clk_divider_is_valid_div(const struct clk_div_table *table,
108 unsigned int div, unsigned long flags)
Peng Fanfe69b032019-07-31 07:01:37 +0000109{
110 if (flags & CLK_DIVIDER_POWER_OF_TWO)
111 return is_power_of_2(div);
112 if (table)
Dario Binacchi5688f3b2020-12-30 00:06:27 +0100113 return clk_divider_is_valid_table_div(table, div);
Peng Fanfe69b032019-07-31 07:01:37 +0000114 return true;
115}
116
Dario Binacchi5688f3b2020-12-30 00:06:27 +0100117unsigned int clk_divider_get_table_val(const struct clk_div_table *table,
118 unsigned int div)
Peng Fanfe69b032019-07-31 07:01:37 +0000119{
120 const struct clk_div_table *clkt;
121
122 for (clkt = table; clkt->div; clkt++)
123 if (clkt->div == div)
124 return clkt->val;
125 return 0;
126}
127
128static unsigned int _get_val(const struct clk_div_table *table,
129 unsigned int div, unsigned long flags, u8 width)
130{
131 if (flags & CLK_DIVIDER_ONE_BASED)
132 return div;
133 if (flags & CLK_DIVIDER_POWER_OF_TWO)
134 return __ffs(div);
135 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
136 return (div == clk_div_mask(width) + 1) ? 0 : div;
137 if (table)
Dario Binacchi5688f3b2020-12-30 00:06:27 +0100138 return clk_divider_get_table_val(table, div);
Peng Fanfe69b032019-07-31 07:01:37 +0000139 return div - 1;
140}
141int divider_get_val(unsigned long rate, unsigned long parent_rate,
142 const struct clk_div_table *table, u8 width,
143 unsigned long flags)
144{
145 unsigned int div, value;
146
147 div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
148
Dario Binacchi5688f3b2020-12-30 00:06:27 +0100149 if (!clk_divider_is_valid_div(table, div, flags))
Peng Fanfe69b032019-07-31 07:01:37 +0000150 return -EINVAL;
151
152 value = _get_val(table, div, flags, width);
153
154 return min_t(unsigned int, value, clk_div_mask(width));
155}
156
157static ulong clk_divider_set_rate(struct clk *clk, unsigned long rate)
158{
Sean Anderson78ce0bd2020-06-24 06:41:06 -0400159 struct clk_divider *divider = to_clk_divider(clk);
Peng Fanfe69b032019-07-31 07:01:37 +0000160 unsigned long parent_rate = clk_get_parent_rate(clk);
161 int value;
162 u32 val;
163
164 value = divider_get_val(rate, parent_rate, divider->table,
165 divider->width, divider->flags);
166 if (value < 0)
167 return value;
168
169 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
170 val = clk_div_mask(divider->width) << (divider->shift + 16);
171 } else {
172 val = readl(divider->reg);
173 val &= ~(clk_div_mask(divider->width) << divider->shift);
174 }
175 val |= (u32)value << divider->shift;
176 writel(val, divider->reg);
177
178 return clk_get_rate(clk);
179}
180
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200181const struct clk_ops clk_divider_ops = {
182 .get_rate = clk_divider_recalc_rate,
Peng Fanfe69b032019-07-31 07:01:37 +0000183 .set_rate = clk_divider_set_rate,
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200184};
185
186static 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)
190{
191 struct clk_divider *div;
192 struct clk *clk;
193 int ret;
194
195 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
196 if (width + shift > 16) {
Patrick Delaunay560e1e02021-11-19 15:12:07 +0100197 dev_warn(dev, "divider value exceeds LOWORD field\n");
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200198 return ERR_PTR(-EINVAL);
199 }
200 }
201
202 /* allocate the divider */
203 div = kzalloc(sizeof(*div), GFP_KERNEL);
204 if (!div)
205 return ERR_PTR(-ENOMEM);
206
207 /* struct clk_divider assignments */
208 div->reg = reg;
209 div->shift = shift;
210 div->width = width;
211 div->flags = clk_divider_flags;
212 div->table = table;
Lukasz Majewski6bb15d62019-06-24 15:50:48 +0200213#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
214 div->io_divider_val = *(u32 *)reg;
215#endif
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200216
217 /* register the clock */
218 clk = &div->clk;
Dario Binacchi16bdc852020-04-13 14:36:27 +0200219 clk->flags = flags;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200220
221 ret = clk_register(clk, UBOOT_DM_CLK_CCF_DIVIDER, name, parent_name);
222 if (ret) {
223 kfree(div);
224 return ERR_PTR(ret);
225 }
226
227 return clk;
228}
229
230struct 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)
234{
235 struct clk *clk;
236
237 clk = _register_divider(dev, name, parent_name, flags, reg, shift,
238 width, clk_divider_flags, NULL);
239 if (IS_ERR(clk))
240 return ERR_CAST(clk);
241 return clk;
242}
243
244U_BOOT_DRIVER(ccf_clk_divider) = {
245 .name = UBOOT_DM_CLK_CCF_DIVIDER,
246 .id = UCLASS_CLK,
247 .ops = &clk_divider_ops,
248 .flags = DM_FLAG_PRE_RELOC,
249};