blob: 270f2fbdf0ccb6a65807e6304456c8748eaaea48 [file] [log] [blame]
Dario Binacchiea45b8f2020-12-30 00:06:35 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * TI divider clock support
4 *
5 * Copyright (C) 2020 Dario Binacchi <dariobin@libero.it>
6 *
7 * Loosely based on Linux kernel drivers/clk/ti/divider.c
8 */
9
10#include <common.h>
11#include <clk.h>
12#include <clk-uclass.h>
13#include <div64.h>
14#include <dm.h>
15#include <dm/device_compat.h>
16#include <asm/io.h>
17#include <linux/clk-provider.h>
18#include <linux/kernel.h>
19#include <linux/log2.h>
20#include "clk.h"
21
22/*
23 * The reverse of DIV_ROUND_UP: The maximum number which
24 * divided by m is r
25 */
26#define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
27
28struct clk_ti_divider_priv {
29 struct clk parent;
30 fdt_addr_t reg;
31 const struct clk_div_table *table;
32 u8 shift;
33 u8 flags;
34 u8 div_flags;
35 s8 latch;
36 u16 min;
37 u16 max;
38 u16 mask;
39};
40
41static unsigned int _get_div(const struct clk_div_table *table, ulong flags,
42 unsigned int val)
43{
44 if (flags & CLK_DIVIDER_ONE_BASED)
45 return val;
46
47 if (flags & CLK_DIVIDER_POWER_OF_TWO)
48 return 1 << val;
49
50 if (table)
51 return clk_divider_get_table_div(table, val);
52
53 return val + 1;
54}
55
56static unsigned int _get_val(const struct clk_div_table *table, ulong flags,
57 unsigned int div)
58{
59 if (flags & CLK_DIVIDER_ONE_BASED)
60 return div;
61
62 if (flags & CLK_DIVIDER_POWER_OF_TWO)
63 return __ffs(div);
64
65 if (table)
66 return clk_divider_get_table_val(table, div);
67
68 return div - 1;
69}
70
71static int _div_round_up(const struct clk_div_table *table, ulong parent_rate,
72 ulong rate)
73{
74 const struct clk_div_table *clkt;
75 int up = INT_MAX;
76 int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
77
78 for (clkt = table; clkt->div; clkt++) {
79 if (clkt->div == div)
80 return clkt->div;
81 else if (clkt->div < div)
82 continue;
83
84 if ((clkt->div - div) < (up - div))
85 up = clkt->div;
86 }
87
88 return up;
89}
90
91static int _div_round(const struct clk_div_table *table, ulong parent_rate,
92 ulong rate)
93{
94 if (table)
95 return _div_round_up(table, parent_rate, rate);
96
97 return DIV_ROUND_UP(parent_rate, rate);
98}
99
100static int clk_ti_divider_best_div(struct clk *clk, ulong rate,
101 ulong *best_parent_rate)
102{
103 struct clk_ti_divider_priv *priv = dev_get_priv(clk->dev);
104 ulong parent_rate, parent_round_rate, max_div;
105 ulong best_rate, r;
106 int i, best_div = 0;
107
108 parent_rate = clk_get_rate(&priv->parent);
109 if (IS_ERR_VALUE(parent_rate))
110 return parent_rate;
111
112 if (!rate)
113 rate = 1;
114
115 if (!(clk->flags & CLK_SET_RATE_PARENT)) {
116 best_div = _div_round(priv->table, parent_rate, rate);
117 if (best_div == 0)
118 best_div = 1;
119
120 if (best_div > priv->max)
121 best_div = priv->max;
122
123 *best_parent_rate = parent_rate;
124 return best_div;
125 }
126
127 max_div = min(ULONG_MAX / rate, (ulong)priv->max);
128 for (best_rate = 0, i = 1; i <= max_div; i++) {
129 if (!clk_divider_is_valid_div(priv->table, priv->div_flags, i))
130 continue;
131
132 /*
133 * It's the most ideal case if the requested rate can be
134 * divided from parent clock without needing to change
135 * parent rate, so return the divider immediately.
136 */
137 if ((rate * i) == parent_rate) {
138 *best_parent_rate = parent_rate;
139 dev_dbg(clk->dev, "rate=%ld, best_rate=%ld, div=%d\n",
140 rate, rate, i);
141 return i;
142 }
143
144 parent_round_rate = clk_round_rate(&priv->parent,
145 MULT_ROUND_UP(rate, i));
146 if (IS_ERR_VALUE(parent_round_rate))
147 continue;
148
149 r = DIV_ROUND_UP(parent_round_rate, i);
150 if (r <= rate && r > best_rate) {
151 best_div = i;
152 best_rate = r;
153 *best_parent_rate = parent_round_rate;
154 if (best_rate == rate)
155 break;
156 }
157 }
158
159 if (best_div == 0) {
160 best_div = priv->max;
161 parent_round_rate = clk_round_rate(&priv->parent, 1);
162 if (IS_ERR_VALUE(parent_round_rate))
163 return parent_round_rate;
164 }
165
166 dev_dbg(clk->dev, "rate=%ld, best_rate=%ld, div=%d\n", rate, best_rate,
167 best_div);
168
169 return best_div;
170}
171
172static ulong clk_ti_divider_round_rate(struct clk *clk, ulong rate)
173{
174 ulong parent_rate;
175 int div;
176
177 div = clk_ti_divider_best_div(clk, rate, &parent_rate);
178 if (div < 0)
179 return div;
180
181 return DIV_ROUND_UP(parent_rate, div);
182}
183
184static ulong clk_ti_divider_set_rate(struct clk *clk, ulong rate)
185{
186 struct clk_ti_divider_priv *priv = dev_get_priv(clk->dev);
187 ulong parent_rate;
188 int div;
189 u32 val, v;
190
191 div = clk_ti_divider_best_div(clk, rate, &parent_rate);
192 if (div < 0)
193 return div;
194
195 if (clk->flags & CLK_SET_RATE_PARENT) {
196 parent_rate = clk_set_rate(&priv->parent, parent_rate);
197 if (IS_ERR_VALUE(parent_rate))
198 return parent_rate;
199 }
200
201 val = _get_val(priv->table, priv->div_flags, div);
202
203 v = readl(priv->reg);
204 v &= ~(priv->mask << priv->shift);
205 v |= val << priv->shift;
206 writel(v, priv->reg);
207 clk_ti_latch(priv->reg, priv->latch);
208
209 return clk_get_rate(clk);
210}
211
212static ulong clk_ti_divider_get_rate(struct clk *clk)
213{
214 struct clk_ti_divider_priv *priv = dev_get_priv(clk->dev);
215 ulong rate, parent_rate;
216 unsigned int div;
217 u32 v;
218
219 parent_rate = clk_get_rate(&priv->parent);
220 if (IS_ERR_VALUE(parent_rate))
221 return parent_rate;
222
223 v = readl(priv->reg) >> priv->shift;
224 v &= priv->mask;
225
226 div = _get_div(priv->table, priv->div_flags, v);
227 if (!div) {
228 if (!(priv->div_flags & CLK_DIVIDER_ALLOW_ZERO))
229 dev_warn(clk->dev,
230 "zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n");
231 return parent_rate;
232 }
233
234 rate = DIV_ROUND_UP(parent_rate, div);
235 dev_dbg(clk->dev, "rate=%ld\n", rate);
236 return rate;
237}
238
239static int clk_ti_divider_request(struct clk *clk)
240{
241 struct clk_ti_divider_priv *priv = dev_get_priv(clk->dev);
242
243 clk->flags = priv->flags;
244 return 0;
245}
246
247const struct clk_ops clk_ti_divider_ops = {
248 .request = clk_ti_divider_request,
249 .round_rate = clk_ti_divider_round_rate,
250 .get_rate = clk_ti_divider_get_rate,
251 .set_rate = clk_ti_divider_set_rate
252};
253
254static int clk_ti_divider_remove(struct udevice *dev)
255{
256 struct clk_ti_divider_priv *priv = dev_get_priv(dev);
257 int err;
258
259 err = clk_release_all(&priv->parent, 1);
260 if (err) {
261 dev_err(dev, "failed to release parent clock\n");
262 return err;
263 }
264
265 return 0;
266}
267
268static int clk_ti_divider_probe(struct udevice *dev)
269{
270 struct clk_ti_divider_priv *priv = dev_get_priv(dev);
271 int err;
272
273 err = clk_get_by_index(dev, 0, &priv->parent);
274 if (err) {
275 dev_err(dev, "failed to get parent clock\n");
276 return err;
277 }
278
279 return 0;
280}
281
282static int clk_ti_divider_of_to_plat(struct udevice *dev)
283{
284 struct clk_ti_divider_priv *priv = dev_get_priv(dev);
285 struct clk_div_table *table = NULL;
286 u32 val, valid_div;
287 u32 min_div = 0;
288 u32 max_val, max_div = 0;
289 u16 mask;
290 int i, div_num;
291
292 priv->reg = dev_read_addr(dev);
293 dev_dbg(dev, "reg=0x%08lx\n", priv->reg);
294 priv->shift = dev_read_u32_default(dev, "ti,bit-shift", 0);
295 priv->latch = dev_read_s32_default(dev, "ti,latch-bit", -EINVAL);
296 if (dev_read_bool(dev, "ti,index-starts-at-one"))
297 priv->div_flags |= CLK_DIVIDER_ONE_BASED;
298
299 if (dev_read_bool(dev, "ti,index-power-of-two"))
300 priv->div_flags |= CLK_DIVIDER_POWER_OF_TWO;
301
302 if (dev_read_bool(dev, "ti,set-rate-parent"))
303 priv->flags |= CLK_SET_RATE_PARENT;
304
305 if (dev_read_prop(dev, "ti,dividers", &div_num)) {
306 div_num /= sizeof(u32);
307
308 /* Determine required size for divider table */
309 for (i = 0, valid_div = 0; i < div_num; i++) {
310 dev_read_u32_index(dev, "ti,dividers", i, &val);
311 if (val)
312 valid_div++;
313 }
314
315 if (!valid_div) {
316 dev_err(dev, "no valid dividers\n");
317 return -EINVAL;
318 }
319
320 table = calloc(valid_div + 1, sizeof(*table));
321 if (!table)
322 return -ENOMEM;
323
324 for (i = 0, valid_div = 0; i < div_num; i++) {
325 dev_read_u32_index(dev, "ti,dividers", i, &val);
326 if (!val)
327 continue;
328
329 table[valid_div].div = val;
330 table[valid_div].val = i;
331 valid_div++;
332 if (val > max_div)
333 max_div = val;
334
335 if (!min_div || val < min_div)
336 min_div = val;
337 }
338
339 max_val = max_div;
340 } else {
341 /* Divider table not provided, determine min/max divs */
342 min_div = dev_read_u32_default(dev, "ti,min-div", 1);
343 if (dev_read_u32(dev, "ti,max-div", &max_div)) {
344 dev_err(dev, "missing 'max-div' property\n");
345 return -EFAULT;
346 }
347
348 max_val = max_div;
349 if (!(priv->div_flags & CLK_DIVIDER_ONE_BASED) &&
350 !(priv->div_flags & CLK_DIVIDER_POWER_OF_TWO))
351 max_val--;
352 }
353
354 priv->table = table;
355 priv->min = min_div;
356 priv->max = max_div;
357
358 if (priv->div_flags & CLK_DIVIDER_POWER_OF_TWO)
359 mask = fls(max_val) - 1;
360 else
361 mask = max_val;
362
363 priv->mask = (1 << fls(mask)) - 1;
364 return 0;
365}
366
367static const struct udevice_id clk_ti_divider_of_match[] = {
368 {.compatible = "ti,divider-clock"},
369 {}
370};
371
372U_BOOT_DRIVER(clk_ti_divider) = {
373 .name = "ti_divider_clock",
374 .id = UCLASS_CLK,
375 .of_match = clk_ti_divider_of_match,
Dario Binacchib0db69b2021-01-15 09:10:26 +0100376 .of_to_plat = clk_ti_divider_of_to_plat,
Dario Binacchiea45b8f2020-12-30 00:06:35 +0100377 .probe = clk_ti_divider_probe,
378 .remove = clk_ti_divider_remove,
379 .priv_auto = sizeof(struct clk_ti_divider_priv),
380 .ops = &clk_ti_divider_ops,
381};