blob: 1075ba73339977a386ec37f563cad71ac36c5663 [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 ),
Igor Prusov99c20ae72023-10-05 11:54:27 +0300241 [CLKID_USB_PHY_IN] = CLK_GATE("usb_phy_in", A1_SYS_OSCIN_CTRL, 2,
242 EXTERNAL_XTAL
243 ),
244 [CLKID_USB_PHY] = CLK_GATE("usb_phy", A1_SYS_CLK_EN0, 27,
245 CLKID_SYS
246 ),
Igor Prusov072c98b2023-09-25 18:52:09 +0300247 [CLKID_SARADC] = CLK_GATE("saradc", A1_SAR_ADC_CLK_CTR, 8,
248 -ENOENT
249 ),
250 [CLKID_SARADC_EN] = CLK_GATE("saradc_en", A1_SYS_CLK_EN0, 13,
251 CLKID_SYS
252 ),
253
254 [EXTERNAL_XTAL] = CLK_EXTERNAL("xtal"),
255 [EXTERNAL_FCLK_DIV2] = CLK_EXTERNAL("fclk_div2"),
256 [EXTERNAL_FCLK_DIV3] = CLK_EXTERNAL("fclk_div3"),
257 [EXTERNAL_FCLK_DIV5] = CLK_EXTERNAL("fclk_div5"),
258 [EXTERNAL_FCLK_DIV7] = CLK_EXTERNAL("fclk_div7"),
259};
260
261/* A1 PLL clocks */
262static const struct meson_clk_info *meson_pll_clocks[] = {
263 [EXTERNAL_FIXPLL_IN] = CLK_EXTERNAL("fixpll_in"),
264
265 [CLKID_FIXED_PLL_DCO] = CLK_PLL("fixed_pll_dco", EXTERNAL_FIXPLL_IN, {
266 {A1_ANACTRL_FIXPLL_CTRL0, 0, 8},
267 {A1_ANACTRL_FIXPLL_CTRL0, 10, 5},
268 }),
269
270 [CLKID_FCLK_DIV2_DIV] = CLK_DIV_FIXED("fclk_div2_div", 2,
271 CLKID_FIXED_PLL
272 ),
273 [CLKID_FCLK_DIV3_DIV] = CLK_DIV_FIXED("fclk_div3_div", 3,
274 CLKID_FIXED_PLL
275 ),
276 [CLKID_FCLK_DIV5_DIV] = CLK_DIV_FIXED("fclk_div5_div", 5,
277 CLKID_FIXED_PLL
278 ),
279 [CLKID_FCLK_DIV7_DIV] = CLK_DIV_FIXED("fclk_div7_div", 7,
280 CLKID_FIXED_PLL
281 ),
282
283 [CLKID_FIXED_PLL] = CLK_GATE("fixed_pll", A1_ANACTRL_FIXPLL_CTRL0, 20,
284 CLKID_FIXED_PLL_DCO
285 ),
286 [CLKID_FCLK_DIV2] = CLK_GATE("fclk_div2", A1_ANACTRL_FIXPLL_CTRL0, 21,
287 CLKID_FCLK_DIV2_DIV
288 ),
289 [CLKID_FCLK_DIV3] = CLK_GATE("fclk_div3", A1_ANACTRL_FIXPLL_CTRL0, 22,
290 CLKID_FCLK_DIV3_DIV
291 ),
292 [CLKID_FCLK_DIV5] = CLK_GATE("fclk_div5", A1_ANACTRL_FIXPLL_CTRL0, 23,
293 CLKID_FCLK_DIV5_DIV
294 ),
295 [CLKID_FCLK_DIV7] = CLK_GATE("fclk_div7", A1_ANACTRL_FIXPLL_CTRL0, 24,
296 CLKID_FCLK_DIV7_DIV
297 ),
298};
299
300static const struct meson_clk_info *meson_clk_get_info(struct clk *clk, ulong id,
301 enum meson_clk_type type)
302{
303 struct meson_clk_data *data;
304 const struct meson_clk_info *info;
305
306 data = (struct meson_clk_data *)dev_get_driver_data(clk->dev);
307 if (id >= data->num_clocks)
308 return ERR_PTR(-EINVAL);
309
310 info = data->clocks[id];
311 if (!info)
312 return ERR_PTR(-ENOENT);
313
314 if (type != MESON_CLK_ANY && type != info->type)
315 return ERR_PTR(-EINVAL);
316
317 return info;
318}
319
320static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id);
321
322static int meson_set_gate(struct clk *clk, bool on)
323{
324 struct meson_clk *priv = dev_get_priv(clk->dev);
325 const struct meson_clk_info *info;
326
327 debug("%s: %sabling %lu\n", __func__, on ? "en" : "dis", clk->id);
328
329 info = meson_clk_get_info(clk, clk->id, MESON_CLK_ANY);
330 if (IS_ERR(info))
331 return PTR_ERR(info);
332
333 SET_PARM_VALUE(priv, info->parm, on);
334
335 return 0;
336}
337
338static int meson_clk_enable(struct clk *clk)
339{
340 return meson_set_gate(clk, true);
341}
342
343static int meson_clk_disable(struct clk *clk)
344{
345 return meson_set_gate(clk, false);
346}
347
348static ulong meson_div_get_rate(struct clk *clk, unsigned long id)
349{
350 struct meson_clk *priv = dev_get_priv(clk->dev);
351 u16 n;
352 ulong rate;
353 const struct meson_clk_info *info;
354
355 info = meson_clk_get_info(clk, id, MESON_CLK_DIV);
356 if (IS_ERR(info))
357 return PTR_ERR(info);
358
359 /* Actual divider value is (field value + 1), hence the increment */
360 n = GET_PARM_VALUE(priv, info->parm) + 1;
361
362 rate = meson_clk_get_rate_by_id(clk, info->parents[0]);
363
364 return rate / n;
365}
366
367static int meson_clk_get_parent(struct clk *clk, unsigned long id)
368{
369 uint reg = 0;
370 struct meson_clk *priv = dev_get_priv(clk->dev);
371 const struct meson_clk_info *info;
372
373 info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
374 if (IS_ERR(info))
375 return PTR_ERR(info);
376
377 /* For muxes we read currently selected parent from register,
378 * for other types there is always only one element in parents array.
379 */
380 if (info->type == MESON_CLK_MUX) {
381 reg = GET_PARM_VALUE(priv, info->parm);
382 if (IS_ERR_VALUE(reg))
383 return reg;
384 }
385
386 return info->parents[reg];
387}
388
389static ulong meson_pll_get_rate(struct clk *clk, unsigned long id)
390{
391 struct meson_clk *priv = dev_get_priv(clk->dev);
392 const struct meson_clk_info *info;
393 const struct parm *pm, *pn;
394 ulong parent_rate_mhz;
395 unsigned int parent;
396 u16 n, m;
397
398 info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
399 if (IS_ERR(info))
400 return PTR_ERR(info);
401
402 pm = &info->parm[0];
403 pn = &info->parm[1];
404
405 n = GET_PARM_VALUE(priv, pn);
406 m = GET_PARM_VALUE(priv, pm);
407
408 if (n == 0)
409 return -EINVAL;
410
411 parent = info->parents[0];
412 parent_rate_mhz = meson_clk_get_rate_by_id(clk, parent) / 1000000;
413
414 return parent_rate_mhz * m / n * 1000000;
415}
416
417static ulong meson_clk_get_rate_by_id(struct clk *clk, unsigned long id)
418{
419 ulong rate, parent;
420 const struct meson_clk_info *info;
421
422 if (IS_ERR_VALUE(id))
423 return id;
424
425 info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
426 if (IS_ERR(info))
427 return PTR_ERR(info);
428
429 switch (info->type) {
430 case MESON_CLK_PLL:
431 rate = meson_pll_get_rate(clk, id);
432 break;
433 case MESON_CLK_GATE:
434 case MESON_CLK_MUX:
435 parent = meson_clk_get_parent(clk, id);
436 rate = meson_clk_get_rate_by_id(clk, parent);
437 break;
438 case MESON_CLK_DIV:
439 rate = meson_div_get_rate(clk, id);
440 break;
441 case MESON_CLK_FIXED_DIV:
442 parent = meson_clk_get_parent(clk, id);
443 rate = meson_clk_get_rate_by_id(clk, parent) / info->div;
444 break;
445 case MESON_CLK_EXTERNAL: {
446 int ret;
447 struct clk external_clk;
448
449 ret = clk_get_by_name(clk->dev, info->name, &external_clk);
450 if (ret)
451 return ret;
452
453 rate = clk_get_rate(&external_clk);
454 break;
455 }
456 default:
457 rate = -EINVAL;
458 break;
459 }
460
461 return rate;
462}
463
464static ulong meson_clk_get_rate(struct clk *clk)
465{
466 return meson_clk_get_rate_by_id(clk, clk->id);
467}
468
469/* This implements rate propagation for dividers placed after multiplexer:
470 * ---------|\
471 * ..... | |---DIV--
472 * ---------|/
473 */
474static ulong meson_composite_set_rate(struct clk *clk, ulong id, ulong rate)
475{
476 unsigned int i, best_div_val;
477 unsigned long best_delta, best_parent;
478 const struct meson_clk_info *div;
479 const struct meson_clk_info *mux;
480 struct meson_clk *priv = dev_get_priv(clk->dev);
481
482 div = meson_clk_get_info(clk, id, MESON_CLK_DIV);
483 if (IS_ERR(div))
484 return PTR_ERR(div);
485
486 mux = meson_clk_get_info(clk, div->parents[0], MESON_CLK_MUX);
487 if (IS_ERR(mux))
488 return PTR_ERR(mux);
489
490 best_parent = -EINVAL;
491 best_delta = ULONG_MAX;
492 for (i = 0; i < (1 << mux->parm->width); i++) {
493 unsigned long parent_rate, delta;
494 unsigned int div_val;
495
496 parent_rate = meson_clk_get_rate_by_id(clk, mux->parents[i]);
497 if (IS_ERR_VALUE(parent_rate))
498 continue;
499
500 /* If overflow, try to use max divider value */
501 div_val = min(DIV_ROUND_CLOSEST(parent_rate, rate),
502 (1UL << div->parm->width));
503
504 delta = abs(rate - (parent_rate / div_val));
505 if (delta < best_delta) {
506 best_delta = delta;
507 best_div_val = div_val;
508 best_parent = i;
509 }
510 }
511
512 if (IS_ERR_VALUE(best_parent))
513 return best_parent;
514
515 SET_PARM_VALUE(priv, mux->parm, best_parent);
516 /* Divider is set to (field value + 1), hence the decrement */
517 SET_PARM_VALUE(priv, div->parm, best_div_val - 1);
518
519 return 0;
520}
521
522static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned int id, ulong rate);
523
524static ulong meson_mux_set_rate(struct clk *clk, unsigned long id, ulong rate)
525{
526 int i;
527 ulong ret = -EINVAL;
528 struct meson_clk *priv = dev_get_priv(clk->dev);
529 const struct meson_clk_info *info;
530
531 info = meson_clk_get_info(clk, id, MESON_CLK_MUX);
532 if (IS_ERR(info))
533 return PTR_ERR(info);
534
535 for (i = 0; i < (1 << info->parm->width); i++) {
536 ret = meson_clk_set_rate_by_id(clk, info->parents[i], rate);
537 if (!ret) {
538 SET_PARM_VALUE(priv, info->parm, i);
539 break;
540 }
541 }
542
543 return ret;
544}
545
546/* Rate propagation is implemented for a subcection of a clock tree, that is
547 * required at boot stage.
548 */
549static ulong meson_clk_set_rate_by_id(struct clk *clk, unsigned int id, ulong rate)
550{
551 switch (id) {
552 case CLKID_SPIFC_DIV:
553 case CLKID_USB_BUS_DIV:
554 return meson_composite_set_rate(clk, id, rate);
555 case CLKID_SPIFC:
556 case CLKID_USB_BUS: {
557 unsigned long parent = meson_clk_get_parent(clk, id);
558
559 return meson_clk_set_rate_by_id(clk, parent, rate);
560 }
561 case CLKID_SPIFC_SEL2:
562 return meson_mux_set_rate(clk, id, rate);
563 }
564
565 return -EINVAL;
566}
567
568static ulong meson_clk_set_rate(struct clk *clk, ulong rate)
569{
570 return meson_clk_set_rate_by_id(clk, clk->id, rate);
571}
572
573static int meson_mux_set_parent_by_id(struct clk *clk, unsigned int parent_id)
574{
575 unsigned int i, parent_index;
576 struct meson_clk *priv = dev_get_priv(clk->dev);
577 const struct meson_clk_info *info;
578
579 info = meson_clk_get_info(clk, clk->id, MESON_CLK_MUX);
580 if (IS_ERR(info))
581 return PTR_ERR(info);
582
583 parent_index = -EINVAL;
584 for (i = 0; i < (1 << info->parm->width); i++) {
585 if (parent_id == info->parents[i]) {
586 parent_index = i;
587 break;
588 }
589 }
590
591 if (IS_ERR_VALUE(parent_index))
592 return parent_index;
593
594 SET_PARM_VALUE(priv, info->parm, parent_index);
595
596 return 0;
597}
598
599static int meson_clk_set_parent(struct clk *clk, struct clk *parent_clk)
600{
601 return meson_mux_set_parent_by_id(clk, parent_clk->id);
602}
603
604static struct clk_ops meson_clk_ops = {
605 .disable = meson_clk_disable,
606 .enable = meson_clk_enable,
607 .get_rate = meson_clk_get_rate,
608 .set_rate = meson_clk_set_rate,
609 .set_parent = meson_clk_set_parent,
610};
611
612static int meson_clk_probe(struct udevice *dev)
613{
614 struct meson_clk *priv = dev_get_priv(dev);
615
616 return regmap_init_mem(dev_ofnode(dev), &priv->map);
617}
618
619struct meson_clk_data meson_a1_peripherals_info = {
620 .clocks = meson_clocks,
621 .num_clocks = ARRAY_SIZE(meson_clocks),
622};
623
624struct meson_clk_data meson_a1_pll_info = {
625 .clocks = meson_pll_clocks,
626 .num_clocks = ARRAY_SIZE(meson_pll_clocks),
627};
628
629static const struct udevice_id meson_clk_ids[] = {
630 {
631 .compatible = "amlogic,a1-peripherals-clkc",
632 .data = (ulong)&meson_a1_peripherals_info,
633 },
634 {
635 .compatible = "amlogic,a1-pll-clkc",
636 .data = (ulong)&meson_a1_pll_info,
637 },
638 { }
639};
640
641U_BOOT_DRIVER(meson_clk) = {
642 .name = "meson-clk-a1",
643 .id = UCLASS_CLK,
644 .of_match = meson_clk_ids,
645 .priv_auto = sizeof(struct meson_clk),
646 .ops = &meson_clk_ops,
647 .probe = meson_clk_probe,
648};
649
650static const char *meson_clk_get_name(struct clk *clk, int id)
651{
652 const struct meson_clk_info *info;
653
654 info = meson_clk_get_info(clk, id, MESON_CLK_ANY);
655
656 return IS_ERR(info) ? "unknown" : info->name;
657}
658
659static int meson_clk_dump(struct clk *clk)
660{
661 const struct meson_clk_info *info;
662 struct meson_clk *priv;
663 unsigned long rate;
664 char *state, frequency[80];
665 int parent;
666
667 priv = dev_get_priv(clk->dev);
668
669 info = meson_clk_get_info(clk, clk->id, MESON_CLK_ANY);
670 if (IS_ERR(info) || !info->name)
671 return -EINVAL;
672
673 rate = clk_get_rate(clk);
674 if (IS_ERR_VALUE(rate))
675 sprintf(frequency, "unknown");
676 else
677 sprintf(frequency, "%lu", rate);
678
679 if (info->type == MESON_CLK_GATE)
680 state = GET_PARM_VALUE(priv, info->parm) ? "enabled" : "disabled";
681 else
682 state = "N/A";
683
684 parent = meson_clk_get_parent(clk, clk->id);
685 printf("%15s%20s%20s%15s\n",
686 info->name,
687 frequency,
688 meson_clk_get_name(clk, parent),
689 state);
690
691 return 0;
692}
693
694static int meson_clk_dump_dev(struct udevice *dev)
695{
696 int i;
697 struct meson_clk_data *data;
698 const char *sep = "--------------------";
699
700 printf("%s:\n", dev->name);
701 printf("%.15s%s%s%.15s\n", sep, sep, sep, sep);
702 printf("%15s%20s%20s%15s\n", "clk", "frequency", "parent", "state");
703 printf("%.15s%s%s%.15s\n", sep, sep, sep, sep);
704
705 data = (struct meson_clk_data *)dev_get_driver_data(dev);
706 for (i = 0; i < data->num_clocks; i++) {
707 meson_clk_dump(&(struct clk){
708 .dev = dev,
709 .id = i
710 });
711 }
712
713 return 0;
714}
715
716int soc_clk_dump(void)
717{
718 struct udevice *dev;
719 int i = 0;
720
721 while (!uclass_get_device(UCLASS_CLK, i++, &dev)) {
722 if (dev->driver == DM_DRIVER_GET(meson_clk)) {
723 meson_clk_dump_dev(dev);
724 printf("\n");
725 }
726 }
727
728 return 0;
729}