blob: 3aec42f33bee5b115e166d41b477f9c6cdb75485 [file] [log] [blame]
Igor Prusov072c98b2023-09-25 18:52:09 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2023 SberDevices, Inc.
4 * Author: Igor Prusov <ivprusov@salutedevices.com>
5 */
6
7#include <common.h>
8#include <clk-uclass.h>
9#include <dm.h>
10#include <regmap.h>
11#include <asm/arch/clock-a1.h>
12#include <dt-bindings/clock/amlogic,a1-pll-clkc.h>
13#include <dt-bindings/clock/amlogic,a1-peripherals-clkc.h>
14#include "clk_meson.h"
15
16/*
17 * This driver supports both PLL and peripherals clock sources.
18 * Following operations are supported:
19 * - calculating clock frequency on a limited tree
20 * - reading muxes and dividers
21 * - enabling/disabling gates without propagation
22 * - reparenting without rate propagation, only on muxes
23 * - setting rates with limited reparenting, only on dividers with mux parent
24 */
25
26#define NR_CLKS 154
27#define NR_PLL_CLKS 11
28
29/* External clock IDs. Those should not overlap with regular IDs */
30#define EXTERNAL_XTAL (NR_CLKS + 0)
31#define EXTERNAL_FCLK_DIV2 (NR_CLKS + 1)
32#define EXTERNAL_FCLK_DIV3 (NR_CLKS + 2)
33#define EXTERNAL_FCLK_DIV5 (NR_CLKS + 3)
34#define EXTERNAL_FCLK_DIV7 (NR_CLKS + 4)
35
36#define EXTERNAL_FIXPLL_IN (NR_PLL_CLKS + 1)
37
38#define SET_PARM_VALUE(_priv, _parm, _val) \
39 regmap_update_bits((_priv)->map, (_parm)->reg_off, \
40 SETPMASK((_parm)->width, (_parm)->shift), \
41 (_val) << (_parm)->shift)
42
43#define GET_PARM_VALUE(_priv, _parm) \
44({ \
45 uint _reg; \
46 regmap_read((_priv)->map, (_parm)->reg_off, &_reg); \
47 PARM_GET((_parm)->width, (_parm)->shift, _reg); \
48})
49
50struct meson_clk {
51 struct regmap *map;
52};
53
54/**
55 * enum meson_clk_type - The type of clock
56 * @MESON_CLK_ANY: Special value that matches any clock type
57 * @MESON_CLK_GATE: This clock is a gate
58 * @MESON_CLK_MUX: This clock is a multiplexer
59 * @MESON_CLK_DIV: This clock is a configurable divider
60 * @MESON_CLK_FIXED_DIV: This clock is a configurable divider
61 * @MESON_CLK_EXTERNAL: This is an external clock from different clock provider
62 * @MESON_CLK_PLL: This is a PLL
63 */
64enum meson_clk_type {
65 MESON_CLK_ANY = 0,
66 MESON_CLK_GATE,
67 MESON_CLK_MUX,
68 MESON_CLK_DIV,
69 MESON_CLK_FIXED_DIV,
70 MESON_CLK_EXTERNAL,
71 MESON_CLK_PLL,
72};
73
74/**
75 * struct meson_clk_info - The parameters defining a clock
76 * @name: Name of the clock
77 * @parm: Register bits description for muxes and dividers
78 * @div: Fixed divider value
79 * @parents: List of parent clock IDs
80 * @type: Clock type
81 */
82struct meson_clk_info {
83 const char *name;
84 union {
85 const struct parm *parm;
86 u8 div;
87 };
88 const unsigned int *parents;
89 const enum meson_clk_type type;
90};
91
92/**
93 * struct meson_clk_data - Clocks supported by clock provider
94 * @num_clocks: Number of clocks
95 * @clocks: Array of clock descriptions
96 *
97 */
98struct meson_clk_data {
99 const u8 num_clocks;
100 const struct meson_clk_info **clocks;
101};
102
103/* Clock description initialization macros */
104
105/* A multiplexer */
106#define CLK_MUX(_name, _reg, _shift, _width, ...) \
107 (&(struct meson_clk_info){ \
108 .parents = (const unsigned int[])__VA_ARGS__, \
109 .parm = &(struct parm) { \
110 .reg_off = (_reg), \
111 .shift = (_shift), \
112 .width = (_width), \
113 }, \
114 .name = (_name), \
115 .type = MESON_CLK_MUX, \
116 })
117
118/* A divider with an integral divisor */
119#define CLK_DIV(_name, _reg, _shift, _width, _parent) \
120 (&(struct meson_clk_info){ \
121 .parents = (const unsigned int[]) { (_parent) }, \
122 .parm = &(struct parm) { \
123 .reg_off = (_reg), \
124 .shift = (_shift), \
125 .width = (_width), \
126 }, \
127 .name = (_name), \
128 .type = MESON_CLK_DIV, \
129 })
130
131/* A fixed divider */
132#define CLK_DIV_FIXED(_name, _div, _parent) \
133 (&(struct meson_clk_info){ \
134 .parents = (const unsigned int[]) { (_parent) }, \
135 .div = (_div), \
136 .name = (_name), \
137 .type = MESON_CLK_FIXED_DIV, \
138 })
139
140/* An external clock */
141#define CLK_EXTERNAL(_name) \
142 (&(struct meson_clk_info){ \
143 .name = (_name), \
144 .parents = (const unsigned int[]) { -ENOENT }, \
145 .type = MESON_CLK_EXTERNAL, \
146 })
147
148/* A clock gate */
149#define CLK_GATE(_name, _reg, _shift, _parent) \
150 (&(struct meson_clk_info){ \
151 .parents = (const unsigned int[]) { (_parent) }, \
152 .parm = &(struct parm) { \
153 .reg_off = (_reg), \
154 .shift = (_shift), \
155 .width = 1, \
156 }, \
157 .name = (_name), \
158 .type = MESON_CLK_GATE, \
159 })
160
161/* A PLL clock */
162#define CLK_PLL(_name, _parent, ...) \
163 (&(struct meson_clk_info){ \
164 .name = (_name), \
165 .parents = (const unsigned int[]) { (_parent) }, \
166 .parm = (const struct parm[])__VA_ARGS__, \
167 .type = MESON_CLK_PLL, \
168 })
169
170/* A1 peripherals clocks */
171static const struct meson_clk_info *meson_clocks[] = {
172 [CLKID_SPIFC_SEL] = CLK_MUX("spifc_sel", A1_SPIFC_CLK_CTRL, 9, 2, {
173 EXTERNAL_FCLK_DIV2,
174 EXTERNAL_FCLK_DIV3,
175 EXTERNAL_FCLK_DIV5,
176 -ENOENT,
177 }),
178 [CLKID_SPIFC_SEL2] = CLK_MUX("spifc_sel2", A1_SPIFC_CLK_CTRL, 15, 1, {
179 CLKID_SPIFC_DIV,
180 EXTERNAL_XTAL,
181 }),
182 [CLKID_USB_BUS_SEL] = CLK_MUX("usb_bus_sel", A1_USB_BUSCLK_CTRL, 9, 2, {
183 -ENOENT,
184 CLKID_SYS,
185 EXTERNAL_FCLK_DIV3,
186 EXTERNAL_FCLK_DIV5,
187 }),
188 [CLKID_SYS] = CLK_MUX("sys", A1_SYS_CLK_CTRL0, 31, 1, {
189 CLKID_SYS_A,
190 CLKID_SYS_B,
191 }),
192 [CLKID_SYS_A_SEL] = CLK_MUX("sys_a_sel", A1_SYS_CLK_CTRL0, 10, 3, {
193 -ENOENT,
194 EXTERNAL_FCLK_DIV2,
195 EXTERNAL_FCLK_DIV3,
196 EXTERNAL_FCLK_DIV5,
197 -ENOENT,
198 -ENOENT,
199 -ENOENT,
200 -ENOENT,
201 }),
202 [CLKID_SYS_B_SEL] = CLK_MUX("sys_b_sel", A1_SYS_CLK_CTRL0, 26, 3, {
203 -ENOENT,
204 EXTERNAL_FCLK_DIV2,
205 EXTERNAL_FCLK_DIV3,
206 EXTERNAL_FCLK_DIV5,
207 -ENOENT,
208 -ENOENT,
209 -ENOENT,
210 -ENOENT,
211 }),
212
213 [CLKID_SPIFC_DIV] = CLK_DIV("spifc_div", A1_SPIFC_CLK_CTRL, 0, 8,
214 CLKID_SPIFC_SEL
215 ),
216 [CLKID_USB_BUS_DIV] = CLK_DIV("usb_bus_div", A1_USB_BUSCLK_CTRL, 0, 8,
217 CLKID_USB_BUS_SEL
218 ),
219 [CLKID_SYS_A_DIV] = CLK_DIV("sys_a_div", A1_SYS_CLK_CTRL0, 0, 10,
220 CLKID_SYS_A_SEL
221 ),
222 [CLKID_SYS_B_DIV] = CLK_DIV("sys_b_div", A1_SYS_CLK_CTRL0, 16, 10,
223 CLKID_SYS_B_SEL
224 ),
225
226 [CLKID_SPIFC] = CLK_GATE("spifc", A1_SPIFC_CLK_CTRL, 8,
227 CLKID_SPIFC_SEL2
228 ),
229 [CLKID_USB_BUS] = CLK_GATE("usb_bus", A1_USB_BUSCLK_CTRL, 8,
230 CLKID_USB_BUS_DIV
231 ),
232 [CLKID_SYS_A] = CLK_GATE("sys_a", A1_SYS_CLK_CTRL0, 13,
233 CLKID_SYS_A_DIV
234 ),
235 [CLKID_SYS_B] = CLK_GATE("sys_b", A1_SYS_CLK_CTRL0, 29,
236 CLKID_SYS_B_DIV
237 ),
238 [CLKID_FIXPLL_IN] = CLK_GATE("fixpll_in", A1_SYS_OSCIN_CTRL, 1,
239 EXTERNAL_XTAL
240 ),
241 [CLKID_SARADC] = CLK_GATE("saradc", A1_SAR_ADC_CLK_CTR, 8,
242 -ENOENT
243 ),
244 [CLKID_SARADC_EN] = CLK_GATE("saradc_en", A1_SYS_CLK_EN0, 13,
245 CLKID_SYS
246 ),
247
248 [EXTERNAL_XTAL] = CLK_EXTERNAL("xtal"),
249 [EXTERNAL_FCLK_DIV2] = CLK_EXTERNAL("fclk_div2"),
250 [EXTERNAL_FCLK_DIV3] = CLK_EXTERNAL("fclk_div3"),
251 [EXTERNAL_FCLK_DIV5] = CLK_EXTERNAL("fclk_div5"),
252 [EXTERNAL_FCLK_DIV7] = CLK_EXTERNAL("fclk_div7"),
253};
254
255/* A1 PLL clocks */
256static const struct meson_clk_info *meson_pll_clocks[] = {
257 [EXTERNAL_FIXPLL_IN] = CLK_EXTERNAL("fixpll_in"),
258
259 [CLKID_FIXED_PLL_DCO] = CLK_PLL("fixed_pll_dco", EXTERNAL_FIXPLL_IN, {
260 {A1_ANACTRL_FIXPLL_CTRL0, 0, 8},
261 {A1_ANACTRL_FIXPLL_CTRL0, 10, 5},
262 }),
263
264 [CLKID_FCLK_DIV2_DIV] = CLK_DIV_FIXED("fclk_div2_div", 2,
265 CLKID_FIXED_PLL
266 ),
267 [CLKID_FCLK_DIV3_DIV] = CLK_DIV_FIXED("fclk_div3_div", 3,
268 CLKID_FIXED_PLL
269 ),
270 [CLKID_FCLK_DIV5_DIV] = CLK_DIV_FIXED("fclk_div5_div", 5,
271 CLKID_FIXED_PLL
272 ),
273 [CLKID_FCLK_DIV7_DIV] = CLK_DIV_FIXED("fclk_div7_div", 7,
274 CLKID_FIXED_PLL
275 ),
276
277 [CLKID_FIXED_PLL] = CLK_GATE("fixed_pll", A1_ANACTRL_FIXPLL_CTRL0, 20,
278 CLKID_FIXED_PLL_DCO
279 ),
280 [CLKID_FCLK_DIV2] = CLK_GATE("fclk_div2", A1_ANACTRL_FIXPLL_CTRL0, 21,
281 CLKID_FCLK_DIV2_DIV
282 ),
283 [CLKID_FCLK_DIV3] = CLK_GATE("fclk_div3", A1_ANACTRL_FIXPLL_CTRL0, 22,
284 CLKID_FCLK_DIV3_DIV
285 ),
286 [CLKID_FCLK_DIV5] = CLK_GATE("fclk_div5", A1_ANACTRL_FIXPLL_CTRL0, 23,
287 CLKID_FCLK_DIV5_DIV
288 ),
289 [CLKID_FCLK_DIV7] = CLK_GATE("fclk_div7", A1_ANACTRL_FIXPLL_CTRL0, 24,
290 CLKID_FCLK_DIV7_DIV
291 ),
292};
293
294static const struct meson_clk_info *meson_clk_get_info(struct clk *clk, ulong id,
295 enum meson_clk_type type)
296{
297 struct meson_clk_data *data;
298 const struct meson_clk_info *info;
299
300 data = (struct meson_clk_data *)dev_get_driver_data(clk->dev);
301 if (id >= data->num_clocks)
302 return ERR_PTR(-EINVAL);
303
304 info = data->clocks[id];
305 if (!info)
306 return ERR_PTR(-ENOENT);
307
308 if (type != MESON_CLK_ANY && type != info->type)
309 return ERR_PTR(-EINVAL);
310
311 return info;
312}
313
314static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id);
315
316static int meson_set_gate(struct clk *clk, bool on)
317{
318 struct meson_clk *priv = dev_get_priv(clk->dev);
319 const struct meson_clk_info *info;
320
321 debug("%s: %sabling %lu\n", __func__, on ? "en" : "dis", clk->id);
322
323 info = meson_clk_get_info(clk, clk->id, MESON_CLK_ANY);
324 if (IS_ERR(info))
325 return PTR_ERR(info);
326
327 SET_PARM_VALUE(priv, info->parm, on);
328
329 return 0;
330}
331
332static int meson_clk_enable(struct clk *clk)
333{
334 return meson_set_gate(clk, true);
335}
336
337static int meson_clk_disable(struct clk *clk)
338{
339 return meson_set_gate(clk, false);
340}
341
342static ulong meson_div_get_rate(struct clk *clk, unsigned long id)
343{
344 struct meson_clk *priv = dev_get_priv(clk->dev);
345 u16 n;
346 ulong rate;
347 const struct meson_clk_info *info;
348
349 info = meson_clk_get_info(clk, id, MESON_CLK_DIV);
350 if (IS_ERR(info))
351 return PTR_ERR(info);
352
353 /* Actual divider value is (field value + 1), hence the increment */
354 n = GET_PARM_VALUE(priv, info->parm) + 1;
355
356 rate = meson_clk_get_rate_by_id(clk, info->parents[0]);
357
358 return rate / n;
359}
360
361static int meson_clk_get_parent(struct clk *clk, unsigned long id)
362{
363 uint reg = 0;
364 struct meson_clk *priv = dev_get_priv(clk->dev);
365 const struct meson_clk_info *info;
366
367 info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
368 if (IS_ERR(info))
369 return PTR_ERR(info);
370
371 /* For muxes we read currently selected parent from register,
372 * for other types there is always only one element in parents array.
373 */
374 if (info->type == MESON_CLK_MUX) {
375 reg = GET_PARM_VALUE(priv, info->parm);
376 if (IS_ERR_VALUE(reg))
377 return reg;
378 }
379
380 return info->parents[reg];
381}
382
383static ulong meson_pll_get_rate(struct clk *clk, unsigned long id)
384{
385 struct meson_clk *priv = dev_get_priv(clk->dev);
386 const struct meson_clk_info *info;
387 const struct parm *pm, *pn;
388 ulong parent_rate_mhz;
389 unsigned int parent;
390 u16 n, m;
391
392 info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
393 if (IS_ERR(info))
394 return PTR_ERR(info);
395
396 pm = &info->parm[0];
397 pn = &info->parm[1];
398
399 n = GET_PARM_VALUE(priv, pn);
400 m = GET_PARM_VALUE(priv, pm);
401
402 if (n == 0)
403 return -EINVAL;
404
405 parent = info->parents[0];
406 parent_rate_mhz = meson_clk_get_rate_by_id(clk, parent) / 1000000;
407
408 return parent_rate_mhz * m / n * 1000000;
409}
410
411static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id)
412{
413 ulong rate, parent;
414 const struct meson_clk_info *info;
415
416 if (IS_ERR_VALUE(id))
417 return id;
418
419 info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
420 if (IS_ERR(info))
421 return PTR_ERR(info);
422
423 switch (info->type) {
424 case MESON_CLK_PLL:
425 rate = meson_pll_get_rate(clk, id);
426 break;
427 case MESON_CLK_GATE:
428 case MESON_CLK_MUX:
429 parent = meson_clk_get_parent(clk, id);
430 rate = meson_clk_get_rate_by_id(clk, parent);
431 break;
432 case MESON_CLK_DIV:
433 rate = meson_div_get_rate(clk, id);
434 break;
435 case MESON_CLK_FIXED_DIV:
436 parent = meson_clk_get_parent(clk, id);
437 rate = meson_clk_get_rate_by_id(clk, parent) / info->div;
438 break;
439 case MESON_CLK_EXTERNAL: {
440 int ret;
441 struct clk external_clk;
442
443 ret = clk_get_by_name(clk->dev, info->name, &external_clk);
444 if (ret)
445 return ret;
446
447 rate = clk_get_rate(&external_clk);
448 break;
449 }
450 default:
451 rate = -EINVAL;
452 break;
453 }
454
455 return rate;
456}
457
458static ulong meson_clk_get_rate(struct clk *clk)
459{
460 return meson_clk_get_rate_by_id(clk, clk->id);
461}
462
463/* This implements rate propagation for dividers placed after multiplexer:
464 * ---------|\
465 * ..... | |---DIV--
466 * ---------|/
467 */
468static ulong meson_composite_set_rate(struct clk *clk, ulong id, ulong rate)
469{
470 unsigned int i, best_div_val;
471 unsigned long best_delta, best_parent;
472 const struct meson_clk_info *div;
473 const struct meson_clk_info *mux;
474 struct meson_clk *priv = dev_get_priv(clk->dev);
475
476 div = meson_clk_get_info(clk, id, MESON_CLK_DIV);
477 if (IS_ERR(div))
478 return PTR_ERR(div);
479
480 mux = meson_clk_get_info(clk, div->parents[0], MESON_CLK_MUX);
481 if (IS_ERR(mux))
482 return PTR_ERR(mux);
483
484 best_parent = -EINVAL;
485 best_delta = ULONG_MAX;
486 for (i = 0; i < (1 << mux->parm->width); i++) {
487 unsigned long parent_rate, delta;
488 unsigned int div_val;
489
490 parent_rate = meson_clk_get_rate_by_id(clk, mux->parents[i]);
491 if (IS_ERR_VALUE(parent_rate))
492 continue;
493
494 /* If overflow, try to use max divider value */
495 div_val = min(DIV_ROUND_CLOSEST(parent_rate, rate),
496 (1UL << div->parm->width));
497
498 delta = abs(rate - (parent_rate / div_val));
499 if (delta < best_delta) {
500 best_delta = delta;
501 best_div_val = div_val;
502 best_parent = i;
503 }
504 }
505
506 if (IS_ERR_VALUE(best_parent))
507 return best_parent;
508
509 SET_PARM_VALUE(priv, mux->parm, best_parent);
510 /* Divider is set to (field value + 1), hence the decrement */
511 SET_PARM_VALUE(priv, div->parm, best_div_val - 1);
512
513 return 0;
514}
515
516static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned int id, ulong rate);
517
518static ulong meson_mux_set_rate(struct clk *clk, unsigned long id, ulong rate)
519{
520 int i;
521 ulong ret = -EINVAL;
522 struct meson_clk *priv = dev_get_priv(clk->dev);
523 const struct meson_clk_info *info;
524
525 info = meson_clk_get_info(clk, id, MESON_CLK_MUX);
526 if (IS_ERR(info))
527 return PTR_ERR(info);
528
529 for (i = 0; i < (1 << info->parm->width); i++) {
530 ret = meson_clk_set_rate_by_id(clk, info->parents[i], rate);
531 if (!ret) {
532 SET_PARM_VALUE(priv, info->parm, i);
533 break;
534 }
535 }
536
537 return ret;
538}
539
540/* Rate propagation is implemented for a subcection of a clock tree, that is
541 * required at boot stage.
542 */
543static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned int id, ulong rate)
544{
545 switch (id) {
546 case CLKID_SPIFC_DIV:
547 case CLKID_USB_BUS_DIV:
548 return meson_composite_set_rate(clk, id, rate);
549 case CLKID_SPIFC:
550 case CLKID_USB_BUS: {
551 unsigned long parent = meson_clk_get_parent(clk, id);
552
553 return meson_clk_set_rate_by_id(clk, parent, rate);
554 }
555 case CLKID_SPIFC_SEL2:
556 return meson_mux_set_rate(clk, id, rate);
557 }
558
559 return -EINVAL;
560}
561
562static ulong meson_clk_set_rate(struct clk *clk, ulong rate)
563{
564 return meson_clk_set_rate_by_id(clk, clk->id, rate);
565}
566
567static int meson_mux_set_parent_by_id(struct clk *clk, unsigned int parent_id)
568{
569 unsigned int i, parent_index;
570 struct meson_clk *priv = dev_get_priv(clk->dev);
571 const struct meson_clk_info *info;
572
573 info = meson_clk_get_info(clk, clk->id, MESON_CLK_MUX);
574 if (IS_ERR(info))
575 return PTR_ERR(info);
576
577 parent_index = -EINVAL;
578 for (i = 0; i < (1 << info->parm->width); i++) {
579 if (parent_id == info->parents[i]) {
580 parent_index = i;
581 break;
582 }
583 }
584
585 if (IS_ERR_VALUE(parent_index))
586 return parent_index;
587
588 SET_PARM_VALUE(priv, info->parm, parent_index);
589
590 return 0;
591}
592
593static int meson_clk_set_parent(struct clk *clk, struct clk *parent_clk)
594{
595 return meson_mux_set_parent_by_id(clk, parent_clk->id);
596}
597
598static struct clk_ops meson_clk_ops = {
599 .disable = meson_clk_disable,
600 .enable = meson_clk_enable,
601 .get_rate = meson_clk_get_rate,
602 .set_rate = meson_clk_set_rate,
603 .set_parent = meson_clk_set_parent,
604};
605
606static int meson_clk_probe(struct udevice *dev)
607{
608 struct meson_clk *priv = dev_get_priv(dev);
609
610 return regmap_init_mem(dev_ofnode(dev), &priv->map);
611}
612
613struct meson_clk_data meson_a1_peripherals_info = {
614 .clocks = meson_clocks,
615 .num_clocks = ARRAY_SIZE(meson_clocks),
616};
617
618struct meson_clk_data meson_a1_pll_info = {
619 .clocks = meson_pll_clocks,
620 .num_clocks = ARRAY_SIZE(meson_pll_clocks),
621};
622
623static const struct udevice_id meson_clk_ids[] = {
624 {
625 .compatible = "amlogic,a1-peripherals-clkc",
626 .data = (ulong)&meson_a1_peripherals_info,
627 },
628 {
629 .compatible = "amlogic,a1-pll-clkc",
630 .data = (ulong)&meson_a1_pll_info,
631 },
632 { }
633};
634
635U_BOOT_DRIVER(meson_clk) = {
636 .name = "meson-clk-a1",
637 .id = UCLASS_CLK,
638 .of_match = meson_clk_ids,
639 .priv_auto = sizeof(struct meson_clk),
640 .ops = &meson_clk_ops,
641 .probe = meson_clk_probe,
642};
643
644static const char *meson_clk_get_name(struct clk *clk, int id)
645{
646 const struct meson_clk_info *info;
647
648 info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
649
650 return IS_ERR(info) ? "unknown" : info->name;
651}
652
653static int meson_clk_dump(struct clk *clk)
654{
655 const struct meson_clk_info *info;
656 struct meson_clk *priv;
657 unsigned long rate;
658 char *state, frequency[80];
659 int parent;
660
661 priv = dev_get_priv(clk->dev);
662
663 info = meson_clk_get_info(clk, clk->id, MESON_CLK_ANY);
664 if (IS_ERR(info) || !info->name)
665 return -EINVAL;
666
667 rate = clk_get_rate(clk);
668 if (IS_ERR_VALUE(rate))
669 sprintf(frequency, "unknown");
670 else
671 sprintf(frequency, "%lu", rate);
672
673 if (info->type == MESON_CLK_GATE)
674 state = GET_PARM_VALUE(priv, info->parm) ? "enabled" : "disabled";
675 else
676 state = "N/A";
677
678 parent = meson_clk_get_parent(clk, clk->id);
679 printf("%15s%20s%20s%15s\n",
680 info->name,
681 frequency,
682 meson_clk_get_name(clk, parent),
683 state);
684
685 return 0;
686}
687
688static int meson_clk_dump_dev(struct udevice *dev)
689{
690 int i;
691 struct meson_clk_data *data;
692 const char *sep = "--------------------";
693
694 printf("%s:\n", dev->name);
695 printf("%.15s%s%s%.15s\n", sep, sep, sep, sep);
696 printf("%15s%20s%20s%15s\n", "clk", "frequency", "parent", "state");
697 printf("%.15s%s%s%.15s\n", sep, sep, sep, sep);
698
699 data = (struct meson_clk_data *)dev_get_driver_data(dev);
700 for (i = 0; i < data->num_clocks; i++) {
701 meson_clk_dump(&(struct clk){
702 .dev = dev,
703 .id = i
704 });
705 }
706
707 return 0;
708}
709
710int soc_clk_dump(void)
711{
712 struct udevice *dev;
713 int i = 0;
714
715 while (!uclass_get_device(UCLASS_CLK, i++, &dev)) {
716 if (dev->driver == DM_DRIVER_GET(meson_clk)) {
717 meson_clk_dump_dev(dev);
718 printf("\n");
719 }
720 }
721
722 return 0;
723}