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