blob: 734a71a8ef00c7d673dfe409a99191b91cd10774 [file] [log] [blame]
Tero Kristo82ceb0d2021-06-11 11:45:14 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments K3 clock driver
4 *
Nishanth Menoneaa39c62023-11-01 15:56:03 -05005 * Copyright (C) 2020-2021 Texas Instruments Incorporated - https://www.ti.com/
Tero Kristo82ceb0d2021-06-11 11:45:14 +03006 * Tero Kristo <t-kristo@ti.com>
7 */
8
Tero Kristo82ceb0d2021-06-11 11:45:14 +03009#include <dm.h>
10#include <errno.h>
11#include <soc.h>
12#include <clk-uclass.h>
Udit Kumarc648daa2023-09-21 22:30:38 +053013#include <k3-avs.h>
Tero Kristo82ceb0d2021-06-11 11:45:14 +030014#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[] = {
Jayesh Choudhary21b21772024-06-12 14:41:13 +053062#if IS_ENABLED(CONFIG_SOC_K3_AM625)
63 {
64 .family = "AM62X",
65 .data = &am62x_clk_platdata,
66 },
67#endif
68#if IS_ENABLED(CONFIG_SOC_K3_AM62A7)
69 {
70 .family = "AM62AX",
71 .data = &am62ax_clk_platdata,
72 },
73#endif
74#if IS_ENABLED(CONFIG_SOC_K3_AM62P5)
75 {
76 .family = "AM62PX",
77 .data = &am62px_clk_platdata,
78 },
79#endif
Tero Kristo82ceb0d2021-06-11 11:45:14 +030080#if IS_ENABLED(CONFIG_SOC_K3_J721E)
81 {
82 .family = "J721E",
83 .data = &j721e_clk_platdata,
84 },
85 {
86 .family = "J7200",
87 .data = &j7200_clk_platdata,
88 },
Jayesh Choudhary21b21772024-06-12 14:41:13 +053089#endif
90#if IS_ENABLED(CONFIG_SOC_K3_J721S2)
David Huange04854b2022-01-25 20:56:33 +053091 {
92 .family = "J721S2",
93 .data = &j721s2_clk_platdata,
94 },
Tero Kristo82ceb0d2021-06-11 11:45:14 +030095#endif
Jayesh Choudhary9332a6372024-06-12 14:41:16 +053096#if IS_ENABLED(CONFIG_SOC_K3_J722S)
97 {
98 .family = "J722S",
99 .data = &j722s_clk_platdata,
100 },
101#endif
Jayesh Choudhary21b21772024-06-12 14:41:13 +0530102#if IS_ENABLED(CONFIG_SOC_K3_J784S4)
Apurva Nandanb93ab922024-02-24 01:51:44 +0530103 {
104 .family = "J784S4",
105 .data = &j784s4_clk_platdata,
106 },
Manorit Chawdhryb5a384e2025-03-17 10:24:24 +0530107 {
108 .family = "J742S2",
109 .data = &j784s4_clk_platdata,
110 },
Apurva Nandanb93ab922024-02-24 01:51:44 +0530111#endif
Tero Kristo82ceb0d2021-06-11 11:45:14 +0300112 { /* sentinel */ }
113};
114
115static int ti_clk_probe(struct udevice *dev)
116{
117 struct ti_clk_data *data = dev_get_priv(dev);
118 struct clk *clk;
119 const char *name;
120 const struct clk_data *ti_clk_data;
121 int i, j;
122 const struct soc_attr *soc_match_data;
123 const struct ti_k3_clk_platdata *pdata;
124
125 debug("%s(dev=%p)\n", __func__, dev);
126
127 soc_match_data = soc_device_match(ti_k3_soc_clk_data);
128 if (!soc_match_data)
129 return -ENODEV;
130
131 pdata = (const struct ti_k3_clk_platdata *)soc_match_data->data;
132
133 data->map = kcalloc(pdata->soc_dev_clk_data_cnt, sizeof(*data->map),
134 GFP_KERNEL);
135 data->size = 0;
136
137 for (i = 0; i < pdata->clk_list_cnt; i++) {
138 ti_clk_data = &pdata->clk_list[i];
139
140 switch (ti_clk_data->type) {
141 case CLK_TYPE_FIXED_RATE:
142 name = ti_clk_data->clk.fixed_rate.name;
143 clk = clk_register_fixed_rate(NULL,
144 name,
145 ti_clk_data->clk.fixed_rate.rate);
146 break;
147 case CLK_TYPE_DIV:
148 name = ti_clk_data->clk.div.name;
149 clk = clk_register_divider(NULL, name,
150 ti_clk_data->clk.div.parent,
151 ti_clk_data->clk.div.flags,
152 map_physmem(ti_clk_data->clk.div.reg, 0, MAP_NOCACHE),
153 ti_clk_data->clk.div.shift,
154 ti_clk_data->clk.div.width,
Suman Annadb4c2dc2021-09-07 17:16:58 -0500155 ti_clk_data->clk.div.div_flags);
Tero Kristo82ceb0d2021-06-11 11:45:14 +0300156 break;
157 case CLK_TYPE_MUX:
158 name = ti_clk_data->clk.mux.name;
159 clk = clk_register_mux(NULL, name,
160 ti_clk_data->clk.mux.parents,
161 ti_clk_data->clk.mux.num_parents,
162 ti_clk_data->clk.mux.flags,
163 map_physmem(ti_clk_data->clk.mux.reg, 0, MAP_NOCACHE),
164 ti_clk_data->clk.mux.shift,
165 ti_clk_data->clk.mux.width,
166 0);
167 break;
168 case CLK_TYPE_PLL:
169 name = ti_clk_data->clk.pll.name;
170 clk = clk_register_ti_pll(name,
171 ti_clk_data->clk.pll.parent,
172 map_physmem(ti_clk_data->clk.pll.reg, 0, MAP_NOCACHE));
173
174 if (!osc_freq)
175 osc_freq = clk_get_rate(clk_get_parent(clk));
176 break;
177 default:
178 name = NULL;
179 clk = NULL;
180 printf("WARNING: %s has encountered unknown clk type %d\n",
181 __func__, ti_clk_data->type);
182 }
183
184 if (clk && ti_clk_data->default_freq)
185 clk_set_rate(clk, ti_clk_data->default_freq);
186
187 if (clk && name) {
188 for (j = 0; j < pdata->soc_dev_clk_data_cnt; j++) {
189 if (!strcmp(name, pdata->soc_dev_clk_data[j].clk_name)) {
190 clk_add_map(data, clk, pdata->soc_dev_clk_data[j].dev_id,
191 pdata->soc_dev_clk_data[j].clk_id);
192 }
193 }
194 }
195 }
196
197 return 0;
198}
199
200static int _clk_cmp(u32 dev_id, u32 clk_id, const struct clk_map *map)
201{
202 if (map->dev_id == dev_id && map->clk_id == clk_id)
203 return 0;
204 if (map->dev_id > dev_id ||
205 (map->dev_id == dev_id && map->clk_id > clk_id))
206 return -1;
207 return 1;
208}
209
210static int bsearch(u32 dev_id, u32 clk_id, struct clk_map *map, int num)
211{
212 int result;
213 int idx;
214
215 for (idx = 0; idx < num; idx++) {
216 result = _clk_cmp(dev_id, clk_id, &map[idx]);
217
218 if (result == 0)
219 return idx;
220 }
221
222 return -ENOENT;
223}
224
225static int ti_clk_of_xlate(struct clk *clk,
226 struct ofnode_phandle_args *args)
227{
228 struct ti_clk_data *data = dev_get_priv(clk->dev);
229 int idx;
230
231 debug("%s(clk=%p, args_count=%d [0]=%d [1]=%d)\n", __func__, clk,
232 args->args_count, args->args[0], args->args[1]);
233
234 if (args->args_count != 2) {
235 debug("Invalid args_count: %d\n", args->args_count);
236 return -EINVAL;
237 }
238
239 if (!data->size)
240 return -EPROBE_DEFER;
241
242 idx = bsearch(args->args[0], args->args[1], data->map, data->size);
243 if (idx < 0)
244 return idx;
245
246 clk->id = idx;
247
248 return 0;
249}
250
251static ulong ti_clk_get_rate(struct clk *clk)
252{
253 struct ti_clk_data *data = dev_get_priv(clk->dev);
254 struct clk *clkp = data->map[clk->id].clk;
255
256 return clk_get_rate(clkp);
257}
258
259static ulong ti_clk_set_rate(struct clk *clk, ulong rate)
260{
261 struct ti_clk_data *data = dev_get_priv(clk->dev);
262 struct clk *clkp = data->map[clk->id].clk;
263 int div = 1;
264 ulong child_rate;
265 const struct clk_ops *ops;
266 ulong new_rate, rem;
267 ulong diff, new_diff;
Udit Kumarc648daa2023-09-21 22:30:38 +0530268 int freq_scale_up = rate >= ti_clk_get_rate(clk) ? 1 : 0;
Tero Kristo82ceb0d2021-06-11 11:45:14 +0300269
Udit Kumarc648daa2023-09-21 22:30:38 +0530270 if (IS_ENABLED(CONFIG_K3_AVS0) && freq_scale_up)
271 k3_avs_notify_freq(data->map[clk->id].dev_id,
272 data->map[clk->id].clk_id, rate);
Tero Kristo82ceb0d2021-06-11 11:45:14 +0300273 /*
274 * We must propagate rate change to parent if current clock type
275 * does not allow setting it.
276 */
277 while (clkp) {
278 ops = clkp->dev->driver->ops;
279 if (ops->set_rate)
280 break;
281
282 /*
283 * Store child rate so we can calculate the clock rate
284 * that must be passed to parent
285 */
286 child_rate = clk_get_rate(clkp);
287 clkp = clk_get_parent(clkp);
288 if (clkp) {
289 debug("%s: propagating rate change to parent %s, rate=%u.\n",
290 __func__, clkp->dev->name, (u32)rate / div);
291 div *= clk_get_rate(clkp) / child_rate;
292 }
293 }
294
295 if (!clkp)
296 return -ENOSYS;
297
298 child_rate = clk_get_rate(clkp);
299
300 new_rate = clk_set_rate(clkp, rate / div);
301
302 diff = abs(new_rate - rate / div);
303
304 debug("%s: clk=%s, div=%d, rate=%u, new_rate=%u, diff=%u\n", __func__,
305 clkp->dev->name, div, (u32)rate, (u32)new_rate, (u32)diff);
306
307 /*
308 * If the new rate differs by 50% of the target,
309 * modify parent. This handles typical cases where we have a hsdiv
310 * following directly a PLL
311 */
312
313 if (diff > rate / div / 2) {
314 ulong pll_tgt;
315 int pll_div = 0;
316
317 clk = clkp;
318
319 debug("%s: propagating rate change to parent, rate=%u.\n",
320 __func__, (u32)rate / div);
321
322 clkp = clk_get_parent(clkp);
323
324 if (rate > osc_freq) {
325 if (rate > PLL_MAX_FREQ / 2 && rate < PLL_MAX_FREQ) {
326 pll_tgt = rate;
327 pll_div = 1;
328 } else {
329 for (pll_div = 2; pll_div < PLL_MAX_DIV; pll_div++) {
330 pll_tgt = rate / div * pll_div;
331 if (pll_tgt >= PLL_MIN_FREQ && pll_tgt <= PLL_MAX_FREQ)
332 break;
333 }
334 }
335 } else {
336 pll_tgt = osc_freq;
337 pll_div = rate / div / osc_freq;
338 }
339
340 debug("%s: pll_tgt=%u, rate=%u, div=%u\n", __func__,
341 (u32)pll_tgt, (u32)rate, pll_div);
342
343 clk_set_rate(clkp, pll_tgt);
344
345 return clk_set_rate(clk, rate / div) * div;
346 }
347
348 /*
349 * If the new rate differs by at least 5% of the target,
350 * we must check for rounding error in a divider, so try
351 * set rate with rate + (parent_freq % rate).
352 */
353
354 if (diff > rate / div / 20) {
355 u64 parent_freq = clk_get_parent_rate(clkp);
356
357 rem = parent_freq % rate;
358 new_rate = clk_set_rate(clkp, (rate / div) + rem);
359 new_diff = abs(new_rate - rate / div);
360
361 if (new_diff > diff) {
362 new_rate = clk_set_rate(clkp, rate / div);
363 } else {
364 debug("%s: Using better rate %lu that gives diff %lu\n",
365 __func__, new_rate, new_diff);
366 }
367 }
368
Udit Kumarc648daa2023-09-21 22:30:38 +0530369 if (IS_ENABLED(CONFIG_K3_AVS0) && !freq_scale_up)
370 k3_avs_notify_freq(data->map[clk->id].dev_id,
371 data->map[clk->id].clk_id, rate);
372
Tero Kristo82ceb0d2021-06-11 11:45:14 +0300373 return new_rate;
374}
375
376static int ti_clk_set_parent(struct clk *clk, struct clk *parent)
377{
378 struct ti_clk_data *data = dev_get_priv(clk->dev);
379 struct clk *clkp = data->map[clk->id].clk;
380 struct clk *parentp = data->map[parent->id].clk;
381
382 return clk_set_parent(clkp, parentp);
383}
384
385static int ti_clk_enable(struct clk *clk)
386{
387 struct ti_clk_data *data = dev_get_priv(clk->dev);
388 struct clk *clkp = data->map[clk->id].clk;
389
390 return clk_enable(clkp);
391}
392
393static int ti_clk_disable(struct clk *clk)
394{
395 struct ti_clk_data *data = dev_get_priv(clk->dev);
396 struct clk *clkp = data->map[clk->id].clk;
397
398 return clk_disable(clkp);
399}
400
401static const struct udevice_id ti_clk_of_match[] = {
402 { .compatible = "ti,k2g-sci-clk" },
403 { /* sentinel */ },
404};
405
406static const struct clk_ops ti_clk_ops = {
407 .of_xlate = ti_clk_of_xlate,
408 .set_rate = ti_clk_set_rate,
409 .get_rate = ti_clk_get_rate,
410 .enable = ti_clk_enable,
411 .disable = ti_clk_disable,
412 .set_parent = ti_clk_set_parent,
413};
414
415U_BOOT_DRIVER(ti_clk) = {
416 .name = "ti-clk",
417 .id = UCLASS_CLK,
418 .of_match = ti_clk_of_match,
419 .probe = ti_clk_probe,
420 .priv_auto = sizeof(struct ti_clk_data),
421 .ops = &ti_clk_ops,
422};