blob: 9d4d2149e327fe32b37378929756662d9d416f68 [file] [log] [blame]
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2019 Xilinx, Inc.
4 * Siva Durga Prasad Paladugu <siva.durga.paladugu@xilinx.com>
5 */
6
7#include <common.h>
Simon Glass336d4612020-02-03 07:36:16 -07008#include <dm/device_compat.h>
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +05309#include <linux/bitops.h>
10#include <linux/bitfield.h>
11#include <malloc.h>
12#include <clk-uclass.h>
13#include <clk.h>
14#include <dm.h>
15#include <asm/arch/sys_proto.h>
Michal Simek0f3604a2019-10-04 15:25:18 +020016#include <zynqmp_firmware.h>
Simon Glass61b29b82020-02-03 07:36:15 -070017#include <linux/err.h>
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +053018
19#define MAX_PARENT 100
20#define MAX_NODES 6
21#define MAX_NAME_LEN 50
22
23#define CLK_TYPE_SHIFT 2
24
25#define PM_API_PAYLOAD_LEN 3
26
27#define NA_PARENT 0xFFFFFFFF
28#define DUMMY_PARENT 0xFFFFFFFE
29
30#define CLK_TYPE_FIELD_LEN 4
31#define CLK_TOPOLOGY_NODE_OFFSET 16
32#define NODES_PER_RESP 3
33
34#define CLK_TYPE_FIELD_MASK 0xF
35#define CLK_FLAG_FIELD_MASK GENMASK(21, 8)
36#define CLK_TYPE_FLAG_FIELD_MASK GENMASK(31, 24)
37#define CLK_TYPE_FLAG2_FIELD_MASK GENMASK(7, 4)
38#define CLK_TYPE_FLAG_BITS 8
39
40#define CLK_PARENTS_ID_LEN 16
41#define CLK_PARENTS_ID_MASK 0xFFFF
42
43#define END_OF_TOPOLOGY_NODE 1
44#define END_OF_PARENTS 1
45
46#define CLK_VALID_MASK 0x1
47#define NODE_CLASS_SHIFT 26U
48#define NODE_SUBCLASS_SHIFT 20U
49#define NODE_TYPE_SHIFT 14U
50#define NODE_INDEX_SHIFT 0U
51
52#define CLK_GET_NAME_RESP_LEN 16
53#define CLK_GET_TOPOLOGY_RESP_WORDS 3
54#define CLK_GET_PARENTS_RESP_WORDS 3
55#define CLK_GET_ATTR_RESP_WORDS 1
56
57#define NODE_SUBCLASS_CLOCK_PLL 1
58#define NODE_SUBCLASS_CLOCK_OUT 2
59#define NODE_SUBCLASS_CLOCK_REF 3
60
61#define NODE_CLASS_CLOCK 2
62#define NODE_CLASS_MASK 0x3F
63
64#define CLOCK_NODE_TYPE_MUX 1
65#define CLOCK_NODE_TYPE_DIV 4
66#define CLOCK_NODE_TYPE_GATE 6
67
68enum pm_query_id {
69 PM_QID_INVALID,
70 PM_QID_CLOCK_GET_NAME,
71 PM_QID_CLOCK_GET_TOPOLOGY,
72 PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS,
73 PM_QID_CLOCK_GET_PARENTS,
74 PM_QID_CLOCK_GET_ATTRIBUTES,
75 PM_QID_PINCTRL_GET_NUM_PINS,
76 PM_QID_PINCTRL_GET_NUM_FUNCTIONS,
77 PM_QID_PINCTRL_GET_NUM_FUNCTION_GROUPS,
78 PM_QID_PINCTRL_GET_FUNCTION_NAME,
79 PM_QID_PINCTRL_GET_FUNCTION_GROUPS,
80 PM_QID_PINCTRL_GET_PIN_GROUPS,
81 PM_QID_CLOCK_GET_NUM_CLOCKS,
82 PM_QID_CLOCK_GET_MAX_DIVISOR,
83};
84
85enum clk_type {
86 CLK_TYPE_OUTPUT,
87 CLK_TYPE_EXTERNAL,
88};
89
90struct clock_parent {
91 char name[MAX_NAME_LEN];
92 int id;
93 u32 flag;
94};
95
96struct clock_topology {
97 u32 type;
98 u32 flag;
99 u32 type_flag;
100};
101
102struct versal_clock {
103 char clk_name[MAX_NAME_LEN];
104 u32 valid;
105 enum clk_type type;
106 struct clock_topology node[MAX_NODES];
107 u32 num_nodes;
108 struct clock_parent parent[MAX_PARENT];
109 u32 num_parents;
110 u32 clk_id;
111};
112
113struct versal_clk_priv {
114 struct versal_clock *clk;
115};
116
117static ulong alt_ref_clk;
118static ulong pl_alt_ref_clk;
119static ulong ref_clk;
120
121struct versal_pm_query_data {
122 u32 qid;
123 u32 arg1;
124 u32 arg2;
125 u32 arg3;
126};
127
128static struct versal_clock *clock;
129static unsigned int clock_max_idx;
130
131#define PM_QUERY_DATA 35
132
133static int versal_pm_query(struct versal_pm_query_data qdata, u32 *ret_payload)
134{
135 struct pt_regs regs;
136
137 regs.regs[0] = PM_SIP_SVC | PM_QUERY_DATA;
138 regs.regs[1] = ((u64)qdata.arg1 << 32) | qdata.qid;
139 regs.regs[2] = ((u64)qdata.arg3 << 32) | qdata.arg2;
140
141 smc_call(&regs);
142
143 if (ret_payload) {
144 ret_payload[0] = (u32)regs.regs[0];
145 ret_payload[1] = upper_32_bits(regs.regs[0]);
146 ret_payload[2] = (u32)regs.regs[1];
147 ret_payload[3] = upper_32_bits(regs.regs[1]);
148 ret_payload[4] = (u32)regs.regs[2];
149 }
150
151 return qdata.qid == PM_QID_CLOCK_GET_NAME ? 0 : regs.regs[0];
152}
153
154static inline int versal_is_valid_clock(u32 clk_id)
155{
156 if (clk_id >= clock_max_idx)
157 return -ENODEV;
158
159 return clock[clk_id].valid;
160}
161
162static int versal_get_clock_name(u32 clk_id, char *clk_name)
163{
164 int ret;
165
166 ret = versal_is_valid_clock(clk_id);
167 if (ret == 1) {
168 strncpy(clk_name, clock[clk_id].clk_name, MAX_NAME_LEN);
169 return 0;
170 }
171
172 return ret == 0 ? -EINVAL : ret;
173}
174
175static int versal_get_clock_type(u32 clk_id, u32 *type)
176{
177 int ret;
178
179 ret = versal_is_valid_clock(clk_id);
180 if (ret == 1) {
181 *type = clock[clk_id].type;
182 return 0;
183 }
184
185 return ret == 0 ? -EINVAL : ret;
186}
187
188static int versal_pm_clock_get_num_clocks(u32 *nclocks)
189{
190 struct versal_pm_query_data qdata = {0};
191 u32 ret_payload[PAYLOAD_ARG_CNT];
192 int ret;
193
194 qdata.qid = PM_QID_CLOCK_GET_NUM_CLOCKS;
195
196 ret = versal_pm_query(qdata, ret_payload);
197 *nclocks = ret_payload[1];
198
199 return ret;
200}
201
202static int versal_pm_clock_get_name(u32 clock_id, char *name)
203{
204 struct versal_pm_query_data qdata = {0};
205 u32 ret_payload[PAYLOAD_ARG_CNT];
206 int ret;
207
208 qdata.qid = PM_QID_CLOCK_GET_NAME;
209 qdata.arg1 = clock_id;
210
211 ret = versal_pm_query(qdata, ret_payload);
212 if (ret)
213 return ret;
214 memcpy(name, ret_payload, CLK_GET_NAME_RESP_LEN);
215
216 return 0;
217}
218
219static int versal_pm_clock_get_topology(u32 clock_id, u32 index, u32 *topology)
220{
221 struct versal_pm_query_data qdata = {0};
222 u32 ret_payload[PAYLOAD_ARG_CNT];
223 int ret;
224
225 qdata.qid = PM_QID_CLOCK_GET_TOPOLOGY;
226 qdata.arg1 = clock_id;
227 qdata.arg2 = index;
228
229 ret = versal_pm_query(qdata, ret_payload);
230 memcpy(topology, &ret_payload[1], CLK_GET_TOPOLOGY_RESP_WORDS * 4);
231
232 return ret;
233}
234
235static int versal_pm_clock_get_parents(u32 clock_id, u32 index, u32 *parents)
236{
237 struct versal_pm_query_data qdata = {0};
238 u32 ret_payload[PAYLOAD_ARG_CNT];
239 int ret;
240
241 qdata.qid = PM_QID_CLOCK_GET_PARENTS;
242 qdata.arg1 = clock_id;
243 qdata.arg2 = index;
244
245 ret = versal_pm_query(qdata, ret_payload);
246 memcpy(parents, &ret_payload[1], CLK_GET_PARENTS_RESP_WORDS * 4);
247
248 return ret;
249}
250
251static int versal_pm_clock_get_attributes(u32 clock_id, u32 *attr)
252{
253 struct versal_pm_query_data qdata = {0};
254 u32 ret_payload[PAYLOAD_ARG_CNT];
255 int ret;
256
257 qdata.qid = PM_QID_CLOCK_GET_ATTRIBUTES;
258 qdata.arg1 = clock_id;
259
260 ret = versal_pm_query(qdata, ret_payload);
261 memcpy(attr, &ret_payload[1], CLK_GET_ATTR_RESP_WORDS * 4);
262
263 return ret;
264}
265
266static int __versal_clock_get_topology(struct clock_topology *topology,
267 u32 *data, u32 *nnodes)
268{
269 int i;
270
271 for (i = 0; i < PM_API_PAYLOAD_LEN; i++) {
272 if (!(data[i] & CLK_TYPE_FIELD_MASK))
273 return END_OF_TOPOLOGY_NODE;
274 topology[*nnodes].type = data[i] & CLK_TYPE_FIELD_MASK;
275 topology[*nnodes].flag = FIELD_GET(CLK_FLAG_FIELD_MASK,
276 data[i]);
277 topology[*nnodes].type_flag =
278 FIELD_GET(CLK_TYPE_FLAG_FIELD_MASK, data[i]);
279 topology[*nnodes].type_flag |=
280 FIELD_GET(CLK_TYPE_FLAG2_FIELD_MASK, data[i]) <<
281 CLK_TYPE_FLAG_BITS;
282 debug("topology type:0x%x, flag:0x%x, type_flag:0x%x\n",
283 topology[*nnodes].type, topology[*nnodes].flag,
284 topology[*nnodes].type_flag);
285 (*nnodes)++;
286 }
287
288 return 0;
289}
290
291static int versal_clock_get_topology(u32 clk_id,
292 struct clock_topology *topology,
293 u32 *num_nodes)
294{
295 int j, ret;
296 u32 pm_resp[PM_API_PAYLOAD_LEN] = {0};
297
298 *num_nodes = 0;
299 for (j = 0; j <= MAX_NODES; j += 3) {
300 ret = versal_pm_clock_get_topology(clock[clk_id].clk_id, j,
301 pm_resp);
302 if (ret)
303 return ret;
304 ret = __versal_clock_get_topology(topology, pm_resp, num_nodes);
305 if (ret == END_OF_TOPOLOGY_NODE)
306 return 0;
307 }
308
309 return 0;
310}
311
312static int __versal_clock_get_parents(struct clock_parent *parents, u32 *data,
313 u32 *nparent)
314{
315 int i;
316 struct clock_parent *parent;
317
318 for (i = 0; i < PM_API_PAYLOAD_LEN; i++) {
319 if (data[i] == NA_PARENT)
320 return END_OF_PARENTS;
321
322 parent = &parents[i];
323 parent->id = data[i] & CLK_PARENTS_ID_MASK;
324 if (data[i] == DUMMY_PARENT) {
325 strcpy(parent->name, "dummy_name");
326 parent->flag = 0;
327 } else {
328 parent->flag = data[i] >> CLK_PARENTS_ID_LEN;
329 if (versal_get_clock_name(parent->id, parent->name))
330 continue;
331 }
332 debug("parent name:%s\n", parent->name);
333 *nparent += 1;
334 }
335
336 return 0;
337}
338
339static int versal_clock_get_parents(u32 clk_id, struct clock_parent *parents,
340 u32 *num_parents)
341{
342 int j = 0, ret;
343 u32 pm_resp[PM_API_PAYLOAD_LEN] = {0};
344
345 *num_parents = 0;
346 do {
347 /* Get parents from firmware */
348 ret = versal_pm_clock_get_parents(clock[clk_id].clk_id, j,
349 pm_resp);
350 if (ret)
351 return ret;
352
353 ret = __versal_clock_get_parents(&parents[j], pm_resp,
354 num_parents);
355 if (ret == END_OF_PARENTS)
356 return 0;
357 j += PM_API_PAYLOAD_LEN;
358 } while (*num_parents <= MAX_PARENT);
359
360 return 0;
361}
362
363static u32 versal_clock_get_div(u32 clk_id)
364{
365 u32 ret_payload[PAYLOAD_ARG_CNT];
366 u32 div;
367
Michal Simek65962702019-10-04 15:52:43 +0200368 xilinx_pm_request(PM_CLOCK_GETDIVIDER, clk_id, 0, 0, 0, ret_payload);
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530369 div = ret_payload[1];
370
371 return div;
372}
373
374static u32 versal_clock_set_div(u32 clk_id, u32 div)
375{
376 u32 ret_payload[PAYLOAD_ARG_CNT];
377
Michal Simek65962702019-10-04 15:52:43 +0200378 xilinx_pm_request(PM_CLOCK_SETDIVIDER, clk_id, div, 0, 0, ret_payload);
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530379
380 return div;
381}
382
383static u64 versal_clock_ref(u32 clk_id)
384{
385 u32 ret_payload[PAYLOAD_ARG_CNT];
386 int ref;
387
Michal Simek65962702019-10-04 15:52:43 +0200388 xilinx_pm_request(PM_CLOCK_GETPARENT, clk_id, 0, 0, 0, ret_payload);
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530389 ref = ret_payload[0];
390 if (!(ref & 1))
391 return ref_clk;
392 if (ref & 2)
393 return pl_alt_ref_clk;
394 return 0;
395}
396
397static u64 versal_clock_get_pll_rate(u32 clk_id)
398{
399 u32 ret_payload[PAYLOAD_ARG_CNT];
400 u32 fbdiv;
401 u32 res;
402 u32 frac;
403 u64 freq;
404 u32 parent_rate, parent_id;
405 u32 id = clk_id & 0xFFF;
406
Michal Simek65962702019-10-04 15:52:43 +0200407 xilinx_pm_request(PM_CLOCK_GETSTATE, clk_id, 0, 0, 0, ret_payload);
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530408 res = ret_payload[1];
409 if (!res) {
410 printf("0%x PLL not enabled\n", clk_id);
411 return 0;
412 }
413
414 parent_id = clock[clock[id].parent[0].id].clk_id;
415 parent_rate = versal_clock_ref(parent_id);
416
Michal Simek65962702019-10-04 15:52:43 +0200417 xilinx_pm_request(PM_CLOCK_GETDIVIDER, clk_id, 0, 0, 0, ret_payload);
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530418 fbdiv = ret_payload[1];
Michal Simek65962702019-10-04 15:52:43 +0200419 xilinx_pm_request(PM_CLOCK_PLL_GETPARAM, clk_id, 2, 0, 0, ret_payload);
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530420 frac = ret_payload[1];
421
422 freq = (fbdiv * parent_rate) >> (1 << frac);
423
424 return freq;
425}
426
427static u32 versal_clock_mux(u32 clk_id)
428{
429 int i;
430 u32 id = clk_id & 0xFFF;
431
432 for (i = 0; i < clock[id].num_nodes; i++)
433 if (clock[id].node[i].type == CLOCK_NODE_TYPE_MUX)
434 return 1;
435
436 return 0;
437}
438
439static u32 versal_clock_get_parentid(u32 clk_id)
440{
441 u32 parent_id = 0;
442 u32 ret_payload[PAYLOAD_ARG_CNT];
443 u32 id = clk_id & 0xFFF;
444
445 if (versal_clock_mux(clk_id)) {
Michal Simek65962702019-10-04 15:52:43 +0200446 xilinx_pm_request(PM_CLOCK_GETPARENT, clk_id, 0, 0, 0,
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530447 ret_payload);
448 parent_id = ret_payload[1];
449 }
450
451 debug("parent_id:0x%x\n", clock[clock[id].parent[parent_id].id].clk_id);
452 return clock[clock[id].parent[parent_id].id].clk_id;
453}
454
455static u32 versal_clock_gate(u32 clk_id)
456{
457 u32 id = clk_id & 0xFFF;
458 int i;
459
460 for (i = 0; i < clock[id].num_nodes; i++)
461 if (clock[id].node[i].type == CLOCK_NODE_TYPE_GATE)
462 return 1;
463
464 return 0;
465}
466
467static u32 versal_clock_div(u32 clk_id)
468{
469 int i;
470 u32 id = clk_id & 0xFFF;
471
472 for (i = 0; i < clock[id].num_nodes; i++)
473 if (clock[id].node[i].type == CLOCK_NODE_TYPE_DIV)
474 return 1;
475
476 return 0;
477}
478
479static u32 versal_clock_pll(u32 clk_id, u64 *clk_rate)
480{
481 if (((clk_id >> NODE_SUBCLASS_SHIFT) & NODE_CLASS_MASK) ==
482 NODE_SUBCLASS_CLOCK_PLL &&
483 ((clk_id >> NODE_CLASS_SHIFT) & NODE_CLASS_MASK) ==
484 NODE_CLASS_CLOCK) {
485 *clk_rate = versal_clock_get_pll_rate(clk_id);
486 return 1;
487 }
488
489 return 0;
490}
491
492static u64 versal_clock_calc(u32 clk_id)
493{
494 u32 parent_id;
495 u64 clk_rate;
496 u32 div;
497
498 if (versal_clock_pll(clk_id, &clk_rate))
499 return clk_rate;
500
501 parent_id = versal_clock_get_parentid(clk_id);
502 if (((parent_id >> NODE_SUBCLASS_SHIFT) &
503 NODE_CLASS_MASK) == NODE_SUBCLASS_CLOCK_REF)
504 return versal_clock_ref(clk_id);
505
506 clk_rate = versal_clock_calc(parent_id);
507
508 if (versal_clock_div(clk_id)) {
509 div = versal_clock_get_div(clk_id);
510 clk_rate = DIV_ROUND_CLOSEST(clk_rate, div);
511 }
512
513 return clk_rate;
514}
515
516static int versal_clock_get_rate(u32 clk_id, u64 *clk_rate)
517{
518 if (((clk_id >> NODE_SUBCLASS_SHIFT) &
519 NODE_CLASS_MASK) == NODE_SUBCLASS_CLOCK_REF)
520 *clk_rate = versal_clock_ref(clk_id);
521
522 if (versal_clock_pll(clk_id, clk_rate))
523 return 0;
524
525 if (((clk_id >> NODE_SUBCLASS_SHIFT) &
526 NODE_CLASS_MASK) == NODE_SUBCLASS_CLOCK_OUT &&
527 ((clk_id >> NODE_CLASS_SHIFT) &
528 NODE_CLASS_MASK) == NODE_CLASS_CLOCK) {
529 if (!versal_clock_gate(clk_id))
530 return -EINVAL;
531 *clk_rate = versal_clock_calc(clk_id);
532 return 0;
533 }
534
535 return 0;
536}
537
538int soc_clk_dump(void)
539{
540 u64 clk_rate = 0;
541 u32 type, ret, i = 0;
542
543 printf("\n ****** VERSAL CLOCKS *****\n");
544
545 printf("alt_ref_clk:%ld pl_alt_ref_clk:%ld ref_clk:%ld\n",
546 alt_ref_clk, pl_alt_ref_clk, ref_clk);
547 for (i = 0; i < clock_max_idx; i++) {
548 debug("%s\n", clock[i].clk_name);
549 ret = versal_get_clock_type(i, &type);
550 if (ret || type != CLK_TYPE_OUTPUT)
551 continue;
552
553 ret = versal_clock_get_rate(clock[i].clk_id, &clk_rate);
554
555 if (ret != -EINVAL)
556 printf("clk: %s freq:%lld\n",
557 clock[i].clk_name, clk_rate);
558 }
559
560 return 0;
561}
562
563static void versal_get_clock_info(void)
564{
565 int i, ret;
566 u32 attr, type = 0, nodetype, subclass, class;
567
568 for (i = 0; i < clock_max_idx; i++) {
569 ret = versal_pm_clock_get_attributes(i, &attr);
570 if (ret)
571 continue;
572
573 clock[i].valid = attr & CLK_VALID_MASK;
574 clock[i].type = ((attr >> CLK_TYPE_SHIFT) & 0x1) ?
575 CLK_TYPE_EXTERNAL : CLK_TYPE_OUTPUT;
576 nodetype = (attr >> NODE_TYPE_SHIFT) & NODE_CLASS_MASK;
577 subclass = (attr >> NODE_SUBCLASS_SHIFT) & NODE_CLASS_MASK;
578 class = (attr >> NODE_CLASS_SHIFT) & NODE_CLASS_MASK;
579
580 clock[i].clk_id = (class << NODE_CLASS_SHIFT) |
581 (subclass << NODE_SUBCLASS_SHIFT) |
582 (nodetype << NODE_TYPE_SHIFT) |
583 (i << NODE_INDEX_SHIFT);
584
585 ret = versal_pm_clock_get_name(clock[i].clk_id,
586 clock[i].clk_name);
587 if (ret)
588 continue;
589 debug("clk name:%s, Valid:%d, type:%d, clk_id:0x%x\n",
590 clock[i].clk_name, clock[i].valid,
591 clock[i].type, clock[i].clk_id);
592 }
593
594 /* Get topology of all clock */
595 for (i = 0; i < clock_max_idx; i++) {
596 ret = versal_get_clock_type(i, &type);
597 if (ret || type != CLK_TYPE_OUTPUT)
598 continue;
599 debug("clk name:%s\n", clock[i].clk_name);
600 ret = versal_clock_get_topology(i, clock[i].node,
601 &clock[i].num_nodes);
602 if (ret)
603 continue;
604
605 ret = versal_clock_get_parents(i, clock[i].parent,
606 &clock[i].num_parents);
607 if (ret)
608 continue;
609 }
610}
611
612int versal_clock_setup(void)
613{
614 int ret;
615
616 ret = versal_pm_clock_get_num_clocks(&clock_max_idx);
617 if (ret)
618 return ret;
619
620 debug("%s, clock_max_idx:0x%x\n", __func__, clock_max_idx);
621 clock = calloc(clock_max_idx, sizeof(*clock));
622 if (!clock)
623 return -ENOMEM;
624
625 versal_get_clock_info();
626
627 return 0;
628}
629
630static int versal_clock_get_freq_by_name(char *name, struct udevice *dev,
631 ulong *freq)
632{
633 struct clk clk;
634 int ret;
635
636 ret = clk_get_by_name(dev, name, &clk);
637 if (ret < 0) {
638 dev_err(dev, "failed to get %s\n", name);
639 return ret;
640 }
641
642 *freq = clk_get_rate(&clk);
643 if (IS_ERR_VALUE(*freq)) {
644 dev_err(dev, "failed to get rate %s\n", name);
645 return -EINVAL;
646 }
647
648 return 0;
649}
650
651static int versal_clk_probe(struct udevice *dev)
652{
653 int ret;
654 struct versal_clk_priv *priv = dev_get_priv(dev);
655
656 debug("%s\n", __func__);
657
658 ret = versal_clock_get_freq_by_name("alt_ref_clk", dev, &alt_ref_clk);
659 if (ret < 0)
660 return -EINVAL;
661
662 ret = versal_clock_get_freq_by_name("pl_alt_ref_clk",
663 dev, &pl_alt_ref_clk);
664 if (ret < 0)
665 return -EINVAL;
666
667 ret = versal_clock_get_freq_by_name("ref_clk", dev, &ref_clk);
668 if (ret < 0)
669 return -EINVAL;
670
671 versal_clock_setup();
672
673 priv->clk = clock;
674
675 return ret;
676}
677
678static ulong versal_clk_get_rate(struct clk *clk)
679{
680 struct versal_clk_priv *priv = dev_get_priv(clk->dev);
681 u32 id = clk->id;
682 u32 clk_id;
683 u64 clk_rate = 0;
684
685 debug("%s\n", __func__);
686
687 clk_id = priv->clk[id].clk_id;
688
689 versal_clock_get_rate(clk_id, &clk_rate);
690
691 return clk_rate;
692}
693
694static ulong versal_clk_set_rate(struct clk *clk, ulong rate)
695{
696 struct versal_clk_priv *priv = dev_get_priv(clk->dev);
697 u32 id = clk->id;
698 u32 clk_id;
699 u64 clk_rate = 0;
700 u32 div;
701 int ret;
702
703 debug("%s\n", __func__);
704
705 clk_id = priv->clk[id].clk_id;
706
707 ret = versal_clock_get_rate(clk_id, &clk_rate);
708 if (ret) {
709 printf("Clock is not a Gate:0x%x\n", clk_id);
710 return 0;
711 }
712
713 do {
714 if (versal_clock_div(clk_id)) {
715 div = versal_clock_get_div(clk_id);
716 clk_rate *= div;
717 div = DIV_ROUND_CLOSEST(clk_rate, rate);
718 versal_clock_set_div(clk_id, div);
719 debug("%s, div:%d, newrate:%lld\n", __func__,
720 div, DIV_ROUND_CLOSEST(clk_rate, div));
721 return DIV_ROUND_CLOSEST(clk_rate, div);
722 }
723 clk_id = versal_clock_get_parentid(clk_id);
724 } while (((clk_id >> NODE_SUBCLASS_SHIFT) &
725 NODE_CLASS_MASK) != NODE_SUBCLASS_CLOCK_REF);
726
727 printf("Clock didn't has Divisors:0x%x\n", priv->clk[id].clk_id);
728
729 return clk_rate;
730}
731
732static struct clk_ops versal_clk_ops = {
733 .set_rate = versal_clk_set_rate,
734 .get_rate = versal_clk_get_rate,
735};
736
737static const struct udevice_id versal_clk_ids[] = {
738 { .compatible = "xlnx,versal-clk" },
739 { }
740};
741
742U_BOOT_DRIVER(versal_clk) = {
743 .name = "versal-clk",
744 .id = UCLASS_CLK,
745 .of_match = versal_clk_ids,
746 .probe = versal_clk_probe,
747 .ops = &versal_clk_ops,
748 .priv_auto_alloc_size = sizeof(struct versal_clk_priv),
749};