blob: ba925fa3c4805edbfe1d395735275bdb498fe51d [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 *
Suman Annacfd50df2021-09-07 17:16:58 -05005 * Copyright (C) 2020-2021 Texas Instruments Incorporated - http://www.ti.com/
Tero Kristob4a72a92021-06-11 11:45:14 +03006 * 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 },
David Huang55bdc202022-01-25 20:56:33 +053071#elif CONFIG_SOC_K3_J721S2
72 {
73 .family = "J721S2",
74 .data = &j721s2_clk_platdata,
75 },
Tero Kristob4a72a92021-06-11 11:45:14 +030076#endif
Suman Anna4b8903a2022-05-25 13:38:43 +053077#ifdef CONFIG_SOC_K3_AM625
78 {
79 .family = "AM62X",
80 .data = &am62x_clk_platdata,
81 },
82#endif
Bryan Brattlofb6cbcd62022-11-03 19:13:56 -050083#ifdef CONFIG_SOC_K3_AM62A7
84 {
85 .family = "AM62AX",
86 .data = &am62ax_clk_platdata,
87 },
88#endif
Tero Kristob4a72a92021-06-11 11:45:14 +030089 { /* sentinel */ }
90};
91
92static int ti_clk_probe(struct udevice *dev)
93{
94 struct ti_clk_data *data = dev_get_priv(dev);
95 struct clk *clk;
96 const char *name;
97 const struct clk_data *ti_clk_data;
98 int i, j;
99 const struct soc_attr *soc_match_data;
100 const struct ti_k3_clk_platdata *pdata;
101
102 debug("%s(dev=%p)\n", __func__, dev);
103
104 soc_match_data = soc_device_match(ti_k3_soc_clk_data);
105 if (!soc_match_data)
106 return -ENODEV;
107
108 pdata = (const struct ti_k3_clk_platdata *)soc_match_data->data;
109
110 data->map = kcalloc(pdata->soc_dev_clk_data_cnt, sizeof(*data->map),
111 GFP_KERNEL);
112 data->size = 0;
113
114 for (i = 0; i < pdata->clk_list_cnt; i++) {
115 ti_clk_data = &pdata->clk_list[i];
116
117 switch (ti_clk_data->type) {
118 case CLK_TYPE_FIXED_RATE:
119 name = ti_clk_data->clk.fixed_rate.name;
120 clk = clk_register_fixed_rate(NULL,
121 name,
122 ti_clk_data->clk.fixed_rate.rate);
123 break;
124 case CLK_TYPE_DIV:
125 name = ti_clk_data->clk.div.name;
126 clk = clk_register_divider(NULL, name,
127 ti_clk_data->clk.div.parent,
128 ti_clk_data->clk.div.flags,
129 map_physmem(ti_clk_data->clk.div.reg, 0, MAP_NOCACHE),
130 ti_clk_data->clk.div.shift,
131 ti_clk_data->clk.div.width,
Suman Annacfd50df2021-09-07 17:16:58 -0500132 ti_clk_data->clk.div.div_flags);
Tero Kristob4a72a92021-06-11 11:45:14 +0300133 break;
134 case CLK_TYPE_MUX:
135 name = ti_clk_data->clk.mux.name;
136 clk = clk_register_mux(NULL, name,
137 ti_clk_data->clk.mux.parents,
138 ti_clk_data->clk.mux.num_parents,
139 ti_clk_data->clk.mux.flags,
140 map_physmem(ti_clk_data->clk.mux.reg, 0, MAP_NOCACHE),
141 ti_clk_data->clk.mux.shift,
142 ti_clk_data->clk.mux.width,
143 0);
144 break;
145 case CLK_TYPE_PLL:
146 name = ti_clk_data->clk.pll.name;
147 clk = clk_register_ti_pll(name,
148 ti_clk_data->clk.pll.parent,
149 map_physmem(ti_clk_data->clk.pll.reg, 0, MAP_NOCACHE));
150
151 if (!osc_freq)
152 osc_freq = clk_get_rate(clk_get_parent(clk));
153 break;
154 default:
155 name = NULL;
156 clk = NULL;
157 printf("WARNING: %s has encountered unknown clk type %d\n",
158 __func__, ti_clk_data->type);
159 }
160
161 if (clk && ti_clk_data->default_freq)
162 clk_set_rate(clk, ti_clk_data->default_freq);
163
164 if (clk && name) {
165 for (j = 0; j < pdata->soc_dev_clk_data_cnt; j++) {
166 if (!strcmp(name, pdata->soc_dev_clk_data[j].clk_name)) {
167 clk_add_map(data, clk, pdata->soc_dev_clk_data[j].dev_id,
168 pdata->soc_dev_clk_data[j].clk_id);
169 }
170 }
171 }
172 }
173
174 return 0;
175}
176
177static int _clk_cmp(u32 dev_id, u32 clk_id, const struct clk_map *map)
178{
179 if (map->dev_id == dev_id && map->clk_id == clk_id)
180 return 0;
181 if (map->dev_id > dev_id ||
182 (map->dev_id == dev_id && map->clk_id > clk_id))
183 return -1;
184 return 1;
185}
186
187static int bsearch(u32 dev_id, u32 clk_id, struct clk_map *map, int num)
188{
189 int result;
190 int idx;
191
192 for (idx = 0; idx < num; idx++) {
193 result = _clk_cmp(dev_id, clk_id, &map[idx]);
194
195 if (result == 0)
196 return idx;
197 }
198
199 return -ENOENT;
200}
201
202static int ti_clk_of_xlate(struct clk *clk,
203 struct ofnode_phandle_args *args)
204{
205 struct ti_clk_data *data = dev_get_priv(clk->dev);
206 int idx;
207
208 debug("%s(clk=%p, args_count=%d [0]=%d [1]=%d)\n", __func__, clk,
209 args->args_count, args->args[0], args->args[1]);
210
211 if (args->args_count != 2) {
212 debug("Invalid args_count: %d\n", args->args_count);
213 return -EINVAL;
214 }
215
216 if (!data->size)
217 return -EPROBE_DEFER;
218
219 idx = bsearch(args->args[0], args->args[1], data->map, data->size);
220 if (idx < 0)
221 return idx;
222
223 clk->id = idx;
224
225 return 0;
226}
227
228static ulong ti_clk_get_rate(struct clk *clk)
229{
230 struct ti_clk_data *data = dev_get_priv(clk->dev);
231 struct clk *clkp = data->map[clk->id].clk;
232
233 return clk_get_rate(clkp);
234}
235
236static ulong ti_clk_set_rate(struct clk *clk, ulong rate)
237{
238 struct ti_clk_data *data = dev_get_priv(clk->dev);
239 struct clk *clkp = data->map[clk->id].clk;
240 int div = 1;
241 ulong child_rate;
242 const struct clk_ops *ops;
243 ulong new_rate, rem;
244 ulong diff, new_diff;
245
246 /*
247 * We must propagate rate change to parent if current clock type
248 * does not allow setting it.
249 */
250 while (clkp) {
251 ops = clkp->dev->driver->ops;
252 if (ops->set_rate)
253 break;
254
255 /*
256 * Store child rate so we can calculate the clock rate
257 * that must be passed to parent
258 */
259 child_rate = clk_get_rate(clkp);
260 clkp = clk_get_parent(clkp);
261 if (clkp) {
262 debug("%s: propagating rate change to parent %s, rate=%u.\n",
263 __func__, clkp->dev->name, (u32)rate / div);
264 div *= clk_get_rate(clkp) / child_rate;
265 }
266 }
267
268 if (!clkp)
269 return -ENOSYS;
270
271 child_rate = clk_get_rate(clkp);
272
273 new_rate = clk_set_rate(clkp, rate / div);
274
275 diff = abs(new_rate - rate / div);
276
277 debug("%s: clk=%s, div=%d, rate=%u, new_rate=%u, diff=%u\n", __func__,
278 clkp->dev->name, div, (u32)rate, (u32)new_rate, (u32)diff);
279
280 /*
281 * If the new rate differs by 50% of the target,
282 * modify parent. This handles typical cases where we have a hsdiv
283 * following directly a PLL
284 */
285
286 if (diff > rate / div / 2) {
287 ulong pll_tgt;
288 int pll_div = 0;
289
290 clk = clkp;
291
292 debug("%s: propagating rate change to parent, rate=%u.\n",
293 __func__, (u32)rate / div);
294
295 clkp = clk_get_parent(clkp);
296
297 if (rate > osc_freq) {
298 if (rate > PLL_MAX_FREQ / 2 && rate < PLL_MAX_FREQ) {
299 pll_tgt = rate;
300 pll_div = 1;
301 } else {
302 for (pll_div = 2; pll_div < PLL_MAX_DIV; pll_div++) {
303 pll_tgt = rate / div * pll_div;
304 if (pll_tgt >= PLL_MIN_FREQ && pll_tgt <= PLL_MAX_FREQ)
305 break;
306 }
307 }
308 } else {
309 pll_tgt = osc_freq;
310 pll_div = rate / div / osc_freq;
311 }
312
313 debug("%s: pll_tgt=%u, rate=%u, div=%u\n", __func__,
314 (u32)pll_tgt, (u32)rate, pll_div);
315
316 clk_set_rate(clkp, pll_tgt);
317
318 return clk_set_rate(clk, rate / div) * div;
319 }
320
321 /*
322 * If the new rate differs by at least 5% of the target,
323 * we must check for rounding error in a divider, so try
324 * set rate with rate + (parent_freq % rate).
325 */
326
327 if (diff > rate / div / 20) {
328 u64 parent_freq = clk_get_parent_rate(clkp);
329
330 rem = parent_freq % rate;
331 new_rate = clk_set_rate(clkp, (rate / div) + rem);
332 new_diff = abs(new_rate - rate / div);
333
334 if (new_diff > diff) {
335 new_rate = clk_set_rate(clkp, rate / div);
336 } else {
337 debug("%s: Using better rate %lu that gives diff %lu\n",
338 __func__, new_rate, new_diff);
339 }
340 }
341
342 return new_rate;
343}
344
345static int ti_clk_set_parent(struct clk *clk, struct clk *parent)
346{
347 struct ti_clk_data *data = dev_get_priv(clk->dev);
348 struct clk *clkp = data->map[clk->id].clk;
349 struct clk *parentp = data->map[parent->id].clk;
350
351 return clk_set_parent(clkp, parentp);
352}
353
354static int ti_clk_enable(struct clk *clk)
355{
356 struct ti_clk_data *data = dev_get_priv(clk->dev);
357 struct clk *clkp = data->map[clk->id].clk;
358
359 return clk_enable(clkp);
360}
361
362static int ti_clk_disable(struct clk *clk)
363{
364 struct ti_clk_data *data = dev_get_priv(clk->dev);
365 struct clk *clkp = data->map[clk->id].clk;
366
367 return clk_disable(clkp);
368}
369
370static const struct udevice_id ti_clk_of_match[] = {
371 { .compatible = "ti,k2g-sci-clk" },
372 { /* sentinel */ },
373};
374
375static const struct clk_ops ti_clk_ops = {
376 .of_xlate = ti_clk_of_xlate,
377 .set_rate = ti_clk_set_rate,
378 .get_rate = ti_clk_get_rate,
379 .enable = ti_clk_enable,
380 .disable = ti_clk_disable,
381 .set_parent = ti_clk_set_parent,
382};
383
384U_BOOT_DRIVER(ti_clk) = {
385 .name = "ti-clk",
386 .id = UCLASS_CLK,
387 .of_match = ti_clk_of_match,
388 .probe = ti_clk_probe,
389 .priv_auto = sizeof(struct ti_clk_data),
390 .ops = &ti_clk_ops,
391};