blob: e921894e7a207131f4ecdb058e93b87adb2d4c54 [file] [log] [blame]
Tero Kristob4a72a92021-06-11 11:45:14 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments K3 clock driver
4 *
5 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
6 * Tero Kristo <t-kristo@ti.com>
7 */
8
9#include <common.h>
10#include <dm.h>
11#include <errno.h>
12#include <soc.h>
13#include <clk-uclass.h>
14#include "k3-clk.h"
15
16#define PLL_MIN_FREQ 800000000
17#define PLL_MAX_FREQ 3200000000UL
18#define PLL_MAX_DIV 127
19
20/**
21 * struct clk_map - mapping from dev/clk id tuples towards physical clocks
22 * @dev_id: device ID for the clock
23 * @clk_id: clock ID for the clock
24 * @clk: pointer to the registered clock entry for the mapping
25 */
26struct clk_map {
27 u16 dev_id;
28 u32 clk_id;
29 struct clk *clk;
30};
31
32/**
33 * struct ti_clk_data - clock controller information structure
34 * @map: mapping from dev/clk id tuples to physical clock entries
35 * @size: number of entries in the map
36 */
37struct ti_clk_data {
38 struct clk_map *map;
39 int size;
40};
41
42static ulong osc_freq;
43
44static void clk_add_map(struct ti_clk_data *data, struct clk *clk,
45 u32 dev_id, u32 clk_id)
46{
47 struct clk_map *map;
48
49 debug("%s: added clk=%p, data=%p, dev=%d, clk=%d\n", __func__,
50 clk, data, dev_id, clk_id);
51 if (!clk)
52 return;
53
54 map = data->map + data->size++;
55
56 map->dev_id = dev_id;
57 map->clk_id = clk_id;
58 map->clk = clk;
59}
60
61static const struct soc_attr ti_k3_soc_clk_data[] = {
62#if IS_ENABLED(CONFIG_SOC_K3_J721E)
63 {
64 .family = "J721E",
65 .data = &j721e_clk_platdata,
66 },
67 {
68 .family = "J7200",
69 .data = &j7200_clk_platdata,
70 },
71#endif
72 { /* sentinel */ }
73};
74
75static int ti_clk_probe(struct udevice *dev)
76{
77 struct ti_clk_data *data = dev_get_priv(dev);
78 struct clk *clk;
79 const char *name;
80 const struct clk_data *ti_clk_data;
81 int i, j;
82 const struct soc_attr *soc_match_data;
83 const struct ti_k3_clk_platdata *pdata;
84
85 debug("%s(dev=%p)\n", __func__, dev);
86
87 soc_match_data = soc_device_match(ti_k3_soc_clk_data);
88 if (!soc_match_data)
89 return -ENODEV;
90
91 pdata = (const struct ti_k3_clk_platdata *)soc_match_data->data;
92
93 data->map = kcalloc(pdata->soc_dev_clk_data_cnt, sizeof(*data->map),
94 GFP_KERNEL);
95 data->size = 0;
96
97 for (i = 0; i < pdata->clk_list_cnt; i++) {
98 ti_clk_data = &pdata->clk_list[i];
99
100 switch (ti_clk_data->type) {
101 case CLK_TYPE_FIXED_RATE:
102 name = ti_clk_data->clk.fixed_rate.name;
103 clk = clk_register_fixed_rate(NULL,
104 name,
105 ti_clk_data->clk.fixed_rate.rate);
106 break;
107 case CLK_TYPE_DIV:
108 name = ti_clk_data->clk.div.name;
109 clk = clk_register_divider(NULL, name,
110 ti_clk_data->clk.div.parent,
111 ti_clk_data->clk.div.flags,
112 map_physmem(ti_clk_data->clk.div.reg, 0, MAP_NOCACHE),
113 ti_clk_data->clk.div.shift,
114 ti_clk_data->clk.div.width,
115 0);
116 break;
117 case CLK_TYPE_MUX:
118 name = ti_clk_data->clk.mux.name;
119 clk = clk_register_mux(NULL, name,
120 ti_clk_data->clk.mux.parents,
121 ti_clk_data->clk.mux.num_parents,
122 ti_clk_data->clk.mux.flags,
123 map_physmem(ti_clk_data->clk.mux.reg, 0, MAP_NOCACHE),
124 ti_clk_data->clk.mux.shift,
125 ti_clk_data->clk.mux.width,
126 0);
127 break;
128 case CLK_TYPE_PLL:
129 name = ti_clk_data->clk.pll.name;
130 clk = clk_register_ti_pll(name,
131 ti_clk_data->clk.pll.parent,
132 map_physmem(ti_clk_data->clk.pll.reg, 0, MAP_NOCACHE));
133
134 if (!osc_freq)
135 osc_freq = clk_get_rate(clk_get_parent(clk));
136 break;
137 default:
138 name = NULL;
139 clk = NULL;
140 printf("WARNING: %s has encountered unknown clk type %d\n",
141 __func__, ti_clk_data->type);
142 }
143
144 if (clk && ti_clk_data->default_freq)
145 clk_set_rate(clk, ti_clk_data->default_freq);
146
147 if (clk && name) {
148 for (j = 0; j < pdata->soc_dev_clk_data_cnt; j++) {
149 if (!strcmp(name, pdata->soc_dev_clk_data[j].clk_name)) {
150 clk_add_map(data, clk, pdata->soc_dev_clk_data[j].dev_id,
151 pdata->soc_dev_clk_data[j].clk_id);
152 }
153 }
154 }
155 }
156
157 return 0;
158}
159
160static int _clk_cmp(u32 dev_id, u32 clk_id, const struct clk_map *map)
161{
162 if (map->dev_id == dev_id && map->clk_id == clk_id)
163 return 0;
164 if (map->dev_id > dev_id ||
165 (map->dev_id == dev_id && map->clk_id > clk_id))
166 return -1;
167 return 1;
168}
169
170static int bsearch(u32 dev_id, u32 clk_id, struct clk_map *map, int num)
171{
172 int result;
173 int idx;
174
175 for (idx = 0; idx < num; idx++) {
176 result = _clk_cmp(dev_id, clk_id, &map[idx]);
177
178 if (result == 0)
179 return idx;
180 }
181
182 return -ENOENT;
183}
184
185static int ti_clk_of_xlate(struct clk *clk,
186 struct ofnode_phandle_args *args)
187{
188 struct ti_clk_data *data = dev_get_priv(clk->dev);
189 int idx;
190
191 debug("%s(clk=%p, args_count=%d [0]=%d [1]=%d)\n", __func__, clk,
192 args->args_count, args->args[0], args->args[1]);
193
194 if (args->args_count != 2) {
195 debug("Invalid args_count: %d\n", args->args_count);
196 return -EINVAL;
197 }
198
199 if (!data->size)
200 return -EPROBE_DEFER;
201
202 idx = bsearch(args->args[0], args->args[1], data->map, data->size);
203 if (idx < 0)
204 return idx;
205
206 clk->id = idx;
207
208 return 0;
209}
210
211static ulong ti_clk_get_rate(struct clk *clk)
212{
213 struct ti_clk_data *data = dev_get_priv(clk->dev);
214 struct clk *clkp = data->map[clk->id].clk;
215
216 return clk_get_rate(clkp);
217}
218
219static ulong ti_clk_set_rate(struct clk *clk, ulong rate)
220{
221 struct ti_clk_data *data = dev_get_priv(clk->dev);
222 struct clk *clkp = data->map[clk->id].clk;
223 int div = 1;
224 ulong child_rate;
225 const struct clk_ops *ops;
226 ulong new_rate, rem;
227 ulong diff, new_diff;
228
229 /*
230 * We must propagate rate change to parent if current clock type
231 * does not allow setting it.
232 */
233 while (clkp) {
234 ops = clkp->dev->driver->ops;
235 if (ops->set_rate)
236 break;
237
238 /*
239 * Store child rate so we can calculate the clock rate
240 * that must be passed to parent
241 */
242 child_rate = clk_get_rate(clkp);
243 clkp = clk_get_parent(clkp);
244 if (clkp) {
245 debug("%s: propagating rate change to parent %s, rate=%u.\n",
246 __func__, clkp->dev->name, (u32)rate / div);
247 div *= clk_get_rate(clkp) / child_rate;
248 }
249 }
250
251 if (!clkp)
252 return -ENOSYS;
253
254 child_rate = clk_get_rate(clkp);
255
256 new_rate = clk_set_rate(clkp, rate / div);
257
258 diff = abs(new_rate - rate / div);
259
260 debug("%s: clk=%s, div=%d, rate=%u, new_rate=%u, diff=%u\n", __func__,
261 clkp->dev->name, div, (u32)rate, (u32)new_rate, (u32)diff);
262
263 /*
264 * If the new rate differs by 50% of the target,
265 * modify parent. This handles typical cases where we have a hsdiv
266 * following directly a PLL
267 */
268
269 if (diff > rate / div / 2) {
270 ulong pll_tgt;
271 int pll_div = 0;
272
273 clk = clkp;
274
275 debug("%s: propagating rate change to parent, rate=%u.\n",
276 __func__, (u32)rate / div);
277
278 clkp = clk_get_parent(clkp);
279
280 if (rate > osc_freq) {
281 if (rate > PLL_MAX_FREQ / 2 && rate < PLL_MAX_FREQ) {
282 pll_tgt = rate;
283 pll_div = 1;
284 } else {
285 for (pll_div = 2; pll_div < PLL_MAX_DIV; pll_div++) {
286 pll_tgt = rate / div * pll_div;
287 if (pll_tgt >= PLL_MIN_FREQ && pll_tgt <= PLL_MAX_FREQ)
288 break;
289 }
290 }
291 } else {
292 pll_tgt = osc_freq;
293 pll_div = rate / div / osc_freq;
294 }
295
296 debug("%s: pll_tgt=%u, rate=%u, div=%u\n", __func__,
297 (u32)pll_tgt, (u32)rate, pll_div);
298
299 clk_set_rate(clkp, pll_tgt);
300
301 return clk_set_rate(clk, rate / div) * div;
302 }
303
304 /*
305 * If the new rate differs by at least 5% of the target,
306 * we must check for rounding error in a divider, so try
307 * set rate with rate + (parent_freq % rate).
308 */
309
310 if (diff > rate / div / 20) {
311 u64 parent_freq = clk_get_parent_rate(clkp);
312
313 rem = parent_freq % rate;
314 new_rate = clk_set_rate(clkp, (rate / div) + rem);
315 new_diff = abs(new_rate - rate / div);
316
317 if (new_diff > diff) {
318 new_rate = clk_set_rate(clkp, rate / div);
319 } else {
320 debug("%s: Using better rate %lu that gives diff %lu\n",
321 __func__, new_rate, new_diff);
322 }
323 }
324
325 return new_rate;
326}
327
328static int ti_clk_set_parent(struct clk *clk, struct clk *parent)
329{
330 struct ti_clk_data *data = dev_get_priv(clk->dev);
331 struct clk *clkp = data->map[clk->id].clk;
332 struct clk *parentp = data->map[parent->id].clk;
333
334 return clk_set_parent(clkp, parentp);
335}
336
337static int ti_clk_enable(struct clk *clk)
338{
339 struct ti_clk_data *data = dev_get_priv(clk->dev);
340 struct clk *clkp = data->map[clk->id].clk;
341
342 return clk_enable(clkp);
343}
344
345static int ti_clk_disable(struct clk *clk)
346{
347 struct ti_clk_data *data = dev_get_priv(clk->dev);
348 struct clk *clkp = data->map[clk->id].clk;
349
350 return clk_disable(clkp);
351}
352
353static const struct udevice_id ti_clk_of_match[] = {
354 { .compatible = "ti,k2g-sci-clk" },
355 { /* sentinel */ },
356};
357
358static const struct clk_ops ti_clk_ops = {
359 .of_xlate = ti_clk_of_xlate,
360 .set_rate = ti_clk_set_rate,
361 .get_rate = ti_clk_get_rate,
362 .enable = ti_clk_enable,
363 .disable = ti_clk_disable,
364 .set_parent = ti_clk_set_parent,
365};
366
367U_BOOT_DRIVER(ti_clk) = {
368 .name = "ti-clk",
369 .id = UCLASS_CLK,
370 .of_match = ti_clk_of_match,
371 .probe = ti_clk_probe,
372 .priv_auto = sizeof(struct ti_clk_data),
373 .ops = &ti_clk_ops,
374};