1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2019 Xilinx, Inc.
4 * Siva Durga Prasad Paladugu <siva.durga.paladugu@xilinx.com>
10 #include <asm/ptrace.h>
11 #include <dm/device_compat.h>
12 #include <linux/bitops.h>
13 #include <linux/bitfield.h>
15 #include <clk-uclass.h>
18 #include <asm/arch/sys_proto.h>
19 #include <zynqmp_firmware.h>
20 #include <linux/err.h>
22 #define MAX_PARENT 100
24 #define MAX_NAME_LEN 50
26 #define CLK_TYPE_SHIFT 2
28 #define PM_API_PAYLOAD_LEN 3
30 #define NA_PARENT 0xFFFFFFFF
31 #define DUMMY_PARENT 0xFFFFFFFE
33 #define CLK_TYPE_FIELD_LEN 4
34 #define CLK_TOPOLOGY_NODE_OFFSET 16
35 #define NODES_PER_RESP 3
37 #define CLK_TYPE_FIELD_MASK 0xF
38 #define CLK_FLAG_FIELD_MASK GENMASK(21, 8)
39 #define CLK_TYPE_FLAG_FIELD_MASK GENMASK(31, 24)
40 #define CLK_TYPE_FLAG2_FIELD_MASK GENMASK(7, 4)
41 #define CLK_TYPE_FLAG_BITS 8
43 #define CLK_PARENTS_ID_LEN 16
44 #define CLK_PARENTS_ID_MASK 0xFFFF
46 #define END_OF_TOPOLOGY_NODE 1
47 #define END_OF_PARENTS 1
49 #define CLK_VALID_MASK 0x1
50 #define NODE_CLASS_SHIFT 26U
51 #define NODE_SUBCLASS_SHIFT 20U
52 #define NODE_TYPE_SHIFT 14U
53 #define NODE_INDEX_SHIFT 0U
55 #define CLK_GET_NAME_RESP_LEN 16
56 #define CLK_GET_TOPOLOGY_RESP_WORDS 3
57 #define CLK_GET_PARENTS_RESP_WORDS 3
58 #define CLK_GET_ATTR_RESP_WORDS 1
60 #define NODE_SUBCLASS_CLOCK_PLL 1
61 #define NODE_SUBCLASS_CLOCK_OUT 2
62 #define NODE_SUBCLASS_CLOCK_REF 3
64 #define NODE_CLASS_CLOCK 2
65 #define NODE_CLASS_MASK 0x3F
67 #define CLOCK_NODE_TYPE_MUX 1
68 #define CLOCK_NODE_TYPE_DIV 4
69 #define CLOCK_NODE_TYPE_GATE 6
77 char name[MAX_NAME_LEN];
82 struct clock_topology {
89 char clk_name[MAX_NAME_LEN];
92 struct clock_topology node[MAX_NODES];
94 struct clock_parent parent[MAX_PARENT];
99 struct versal_clk_priv {
100 struct versal_clock *clk;
103 static ulong pl_alt_ref_clk;
104 static ulong ref_clk;
106 struct versal_pm_query_data {
113 static struct versal_clock *clock;
114 static unsigned int clock_max_idx;
116 #define PM_QUERY_DATA 35
118 static int versal_pm_query(struct versal_pm_query_data qdata, u32 *ret_payload)
122 regs.regs[0] = PM_SIP_SVC | PM_QUERY_DATA;
123 regs.regs[1] = ((u64)qdata.arg1 << 32) | qdata.qid;
124 regs.regs[2] = ((u64)qdata.arg3 << 32) | qdata.arg2;
129 ret_payload[0] = (u32)regs.regs[0];
130 ret_payload[1] = upper_32_bits(regs.regs[0]);
131 ret_payload[2] = (u32)regs.regs[1];
132 ret_payload[3] = upper_32_bits(regs.regs[1]);
133 ret_payload[4] = (u32)regs.regs[2];
136 return qdata.qid == PM_QID_CLOCK_GET_NAME ? 0 : regs.regs[0];
139 static inline int versal_is_valid_clock(u32 clk_id)
141 if (clk_id >= clock_max_idx)
144 return clock[clk_id].valid;
147 static int versal_get_clock_name(u32 clk_id, char *clk_name)
151 ret = versal_is_valid_clock(clk_id);
153 strncpy(clk_name, clock[clk_id].clk_name, MAX_NAME_LEN);
157 return ret == 0 ? -EINVAL : ret;
160 static int versal_get_clock_type(u32 clk_id, u32 *type)
164 ret = versal_is_valid_clock(clk_id);
166 *type = clock[clk_id].type;
170 return ret == 0 ? -EINVAL : ret;
173 static int versal_pm_clock_get_num_clocks(u32 *nclocks)
175 struct versal_pm_query_data qdata = {0};
176 u32 ret_payload[PAYLOAD_ARG_CNT];
179 qdata.qid = PM_QID_CLOCK_GET_NUM_CLOCKS;
181 ret = versal_pm_query(qdata, ret_payload);
182 *nclocks = ret_payload[1];
187 static int versal_pm_clock_get_name(u32 clock_id, char *name)
189 struct versal_pm_query_data qdata = {0};
190 u32 ret_payload[PAYLOAD_ARG_CNT];
193 qdata.qid = PM_QID_CLOCK_GET_NAME;
194 qdata.arg1 = clock_id;
196 ret = versal_pm_query(qdata, ret_payload);
199 memcpy(name, ret_payload, CLK_GET_NAME_RESP_LEN);
204 static int versal_pm_clock_get_topology(u32 clock_id, u32 index, u32 *topology)
206 struct versal_pm_query_data qdata = {0};
207 u32 ret_payload[PAYLOAD_ARG_CNT];
210 qdata.qid = PM_QID_CLOCK_GET_TOPOLOGY;
211 qdata.arg1 = clock_id;
214 ret = versal_pm_query(qdata, ret_payload);
215 memcpy(topology, &ret_payload[1], CLK_GET_TOPOLOGY_RESP_WORDS * 4);
220 static int versal_pm_clock_get_parents(u32 clock_id, u32 index, u32 *parents)
222 struct versal_pm_query_data qdata = {0};
223 u32 ret_payload[PAYLOAD_ARG_CNT];
226 qdata.qid = PM_QID_CLOCK_GET_PARENTS;
227 qdata.arg1 = clock_id;
230 ret = versal_pm_query(qdata, ret_payload);
231 memcpy(parents, &ret_payload[1], CLK_GET_PARENTS_RESP_WORDS * 4);
236 static int versal_pm_clock_get_attributes(u32 clock_id, u32 *attr)
238 struct versal_pm_query_data qdata = {0};
239 u32 ret_payload[PAYLOAD_ARG_CNT];
242 qdata.qid = PM_QID_CLOCK_GET_ATTRIBUTES;
243 qdata.arg1 = clock_id;
245 ret = versal_pm_query(qdata, ret_payload);
246 memcpy(attr, &ret_payload[1], CLK_GET_ATTR_RESP_WORDS * 4);
251 static int __versal_clock_get_topology(struct clock_topology *topology,
252 u32 *data, u32 *nnodes)
256 for (i = 0; i < PM_API_PAYLOAD_LEN; i++) {
257 if (!(data[i] & CLK_TYPE_FIELD_MASK))
258 return END_OF_TOPOLOGY_NODE;
259 topology[*nnodes].type = data[i] & CLK_TYPE_FIELD_MASK;
260 topology[*nnodes].flag = FIELD_GET(CLK_FLAG_FIELD_MASK,
262 topology[*nnodes].type_flag =
263 FIELD_GET(CLK_TYPE_FLAG_FIELD_MASK, data[i]);
264 topology[*nnodes].type_flag |=
265 FIELD_GET(CLK_TYPE_FLAG2_FIELD_MASK, data[i]) <<
267 debug("topology type:0x%x, flag:0x%x, type_flag:0x%x\n",
268 topology[*nnodes].type, topology[*nnodes].flag,
269 topology[*nnodes].type_flag);
276 static int versal_clock_get_topology(u32 clk_id,
277 struct clock_topology *topology,
281 u32 pm_resp[PM_API_PAYLOAD_LEN] = {0};
284 for (j = 0; j <= MAX_NODES; j += 3) {
285 ret = versal_pm_clock_get_topology(clock[clk_id].clk_id, j,
289 ret = __versal_clock_get_topology(topology, pm_resp, num_nodes);
290 if (ret == END_OF_TOPOLOGY_NODE)
297 static int __versal_clock_get_parents(struct clock_parent *parents, u32 *data,
301 struct clock_parent *parent;
303 for (i = 0; i < PM_API_PAYLOAD_LEN; i++) {
304 if (data[i] == NA_PARENT)
305 return END_OF_PARENTS;
307 parent = &parents[i];
308 parent->id = data[i] & CLK_PARENTS_ID_MASK;
309 if (data[i] == DUMMY_PARENT) {
310 strcpy(parent->name, "dummy_name");
313 parent->flag = data[i] >> CLK_PARENTS_ID_LEN;
314 if (versal_get_clock_name(parent->id, parent->name))
317 debug("parent name:%s\n", parent->name);
324 static int versal_clock_get_parents(u32 clk_id, struct clock_parent *parents,
328 u32 pm_resp[PM_API_PAYLOAD_LEN] = {0};
332 /* Get parents from firmware */
333 ret = versal_pm_clock_get_parents(clock[clk_id].clk_id, j,
338 ret = __versal_clock_get_parents(&parents[j], pm_resp,
340 if (ret == END_OF_PARENTS)
342 j += PM_API_PAYLOAD_LEN;
343 } while (*num_parents <= MAX_PARENT);
348 static u32 versal_clock_get_div(u32 clk_id)
350 u32 ret_payload[PAYLOAD_ARG_CNT];
353 xilinx_pm_request(PM_CLOCK_GETDIVIDER, clk_id, 0, 0, 0, ret_payload);
354 div = ret_payload[1];
359 static u32 versal_clock_set_div(u32 clk_id, u32 div)
361 u32 ret_payload[PAYLOAD_ARG_CNT];
363 xilinx_pm_request(PM_CLOCK_SETDIVIDER, clk_id, div, 0, 0, ret_payload);
368 static u64 versal_clock_ref(u32 clk_id)
370 u32 ret_payload[PAYLOAD_ARG_CNT];
373 xilinx_pm_request(PM_CLOCK_GETPARENT, clk_id, 0, 0, 0, ret_payload);
374 ref = ret_payload[0];
378 return pl_alt_ref_clk;
382 static u64 versal_clock_get_pll_rate(u32 clk_id)
384 u32 ret_payload[PAYLOAD_ARG_CNT];
389 u32 parent_rate, parent_id;
390 u32 id = clk_id & 0xFFF;
392 xilinx_pm_request(PM_CLOCK_GETSTATE, clk_id, 0, 0, 0, ret_payload);
393 res = ret_payload[1];
395 printf("0%x PLL not enabled\n", clk_id);
399 parent_id = clock[clock[id].parent[0].id].clk_id;
400 parent_rate = versal_clock_ref(parent_id);
402 xilinx_pm_request(PM_CLOCK_GETDIVIDER, clk_id, 0, 0, 0, ret_payload);
403 fbdiv = ret_payload[1];
404 xilinx_pm_request(PM_CLOCK_PLL_GETPARAM, clk_id, 2, 0, 0, ret_payload);
405 frac = ret_payload[1];
407 freq = (fbdiv * parent_rate) >> (1 << frac);
412 static u32 versal_clock_mux(u32 clk_id)
415 u32 id = clk_id & 0xFFF;
417 for (i = 0; i < clock[id].num_nodes; i++)
418 if (clock[id].node[i].type == CLOCK_NODE_TYPE_MUX)
424 static u32 versal_clock_get_parentid(u32 clk_id)
427 u32 ret_payload[PAYLOAD_ARG_CNT];
428 u32 id = clk_id & 0xFFF;
430 if (versal_clock_mux(clk_id)) {
431 xilinx_pm_request(PM_CLOCK_GETPARENT, clk_id, 0, 0, 0,
433 parent_id = ret_payload[1];
436 debug("parent_id:0x%x\n", clock[clock[id].parent[parent_id].id].clk_id);
437 return clock[clock[id].parent[parent_id].id].clk_id;
440 static u32 versal_clock_gate(u32 clk_id)
442 u32 id = clk_id & 0xFFF;
445 for (i = 0; i < clock[id].num_nodes; i++)
446 if (clock[id].node[i].type == CLOCK_NODE_TYPE_GATE)
452 static u32 versal_clock_div(u32 clk_id)
455 u32 id = clk_id & 0xFFF;
457 for (i = 0; i < clock[id].num_nodes; i++)
458 if (clock[id].node[i].type == CLOCK_NODE_TYPE_DIV)
464 static u32 versal_clock_pll(u32 clk_id, u64 *clk_rate)
466 if (((clk_id >> NODE_SUBCLASS_SHIFT) & NODE_CLASS_MASK) ==
467 NODE_SUBCLASS_CLOCK_PLL &&
468 ((clk_id >> NODE_CLASS_SHIFT) & NODE_CLASS_MASK) ==
470 *clk_rate = versal_clock_get_pll_rate(clk_id);
477 static u64 versal_clock_calc(u32 clk_id)
483 if (versal_clock_pll(clk_id, &clk_rate))
486 parent_id = versal_clock_get_parentid(clk_id);
487 if (((parent_id >> NODE_SUBCLASS_SHIFT) &
488 NODE_CLASS_MASK) == NODE_SUBCLASS_CLOCK_REF)
489 return versal_clock_ref(clk_id);
494 clk_rate = versal_clock_calc(parent_id);
496 if (versal_clock_div(clk_id)) {
497 div = versal_clock_get_div(clk_id);
498 clk_rate = DIV_ROUND_CLOSEST(clk_rate, div);
504 static int versal_clock_get_rate(u32 clk_id, u64 *clk_rate)
506 if (((clk_id >> NODE_SUBCLASS_SHIFT) &
507 NODE_CLASS_MASK) == NODE_SUBCLASS_CLOCK_REF)
508 *clk_rate = versal_clock_ref(clk_id);
510 if (versal_clock_pll(clk_id, clk_rate))
513 if (((clk_id >> NODE_SUBCLASS_SHIFT) &
514 NODE_CLASS_MASK) == NODE_SUBCLASS_CLOCK_OUT &&
515 ((clk_id >> NODE_CLASS_SHIFT) &
516 NODE_CLASS_MASK) == NODE_CLASS_CLOCK) {
517 if (!versal_clock_gate(clk_id) && !versal_clock_mux(clk_id))
519 *clk_rate = versal_clock_calc(clk_id);
526 int soc_clk_dump(void)
529 u32 type, ret, i = 0;
531 printf("\n ****** VERSAL CLOCKS *****\n");
533 printf("pl_alt_ref_clk:%ld ref_clk:%ld\n", pl_alt_ref_clk, ref_clk);
534 for (i = 0; i < clock_max_idx; i++) {
535 debug("%s\n", clock[i].clk_name);
536 ret = versal_get_clock_type(i, &type);
537 if (ret || type != CLK_TYPE_OUTPUT)
540 ret = versal_clock_get_rate(clock[i].clk_id, &clk_rate);
543 printf("clk: %s freq:%lld\n",
544 clock[i].clk_name, clk_rate);
550 static void versal_get_clock_info(void)
553 u32 attr, type = 0, nodetype, subclass, class;
555 for (i = 0; i < clock_max_idx; i++) {
556 ret = versal_pm_clock_get_attributes(i, &attr);
560 clock[i].valid = attr & CLK_VALID_MASK;
562 /* skip query for Invalid clock */
563 ret = versal_is_valid_clock(i);
564 if (ret != CLK_VALID_MASK)
567 clock[i].type = ((attr >> CLK_TYPE_SHIFT) & 0x1) ?
568 CLK_TYPE_EXTERNAL : CLK_TYPE_OUTPUT;
569 nodetype = (attr >> NODE_TYPE_SHIFT) & NODE_CLASS_MASK;
570 subclass = (attr >> NODE_SUBCLASS_SHIFT) & NODE_CLASS_MASK;
571 class = (attr >> NODE_CLASS_SHIFT) & NODE_CLASS_MASK;
573 clock[i].clk_id = (class << NODE_CLASS_SHIFT) |
574 (subclass << NODE_SUBCLASS_SHIFT) |
575 (nodetype << NODE_TYPE_SHIFT) |
576 (i << NODE_INDEX_SHIFT);
578 ret = versal_pm_clock_get_name(clock[i].clk_id,
582 debug("clk name:%s, Valid:%d, type:%d, clk_id:0x%x\n",
583 clock[i].clk_name, clock[i].valid,
584 clock[i].type, clock[i].clk_id);
587 /* Get topology of all clock */
588 for (i = 0; i < clock_max_idx; i++) {
589 ret = versal_get_clock_type(i, &type);
590 if (ret || type != CLK_TYPE_OUTPUT)
592 debug("clk name:%s\n", clock[i].clk_name);
593 ret = versal_clock_get_topology(i, clock[i].node,
594 &clock[i].num_nodes);
598 ret = versal_clock_get_parents(i, clock[i].parent,
599 &clock[i].num_parents);
605 static int versal_clock_setup(void)
609 ret = versal_pm_clock_get_num_clocks(&clock_max_idx);
613 debug("%s, clock_max_idx:0x%x\n", __func__, clock_max_idx);
614 clock = calloc(clock_max_idx, sizeof(*clock));
618 versal_get_clock_info();
623 static int versal_clock_get_freq_by_name(char *name, struct udevice *dev,
629 ret = clk_get_by_name(dev, name, &clk);
631 dev_err(dev, "failed to get %s\n", name);
635 *freq = clk_get_rate(&clk);
636 if (IS_ERR_VALUE(*freq)) {
637 dev_err(dev, "failed to get rate %s\n", name);
644 static int versal_clk_probe(struct udevice *dev)
647 struct versal_clk_priv *priv = dev_get_priv(dev);
649 debug("%s\n", __func__);
651 ret = versal_clock_get_freq_by_name("pl_alt_ref_clk",
652 dev, &pl_alt_ref_clk);
656 ret = versal_clock_get_freq_by_name("ref_clk", dev, &ref_clk);
660 ret = versal_clock_setup();
669 static ulong versal_clk_get_rate(struct clk *clk)
671 struct versal_clk_priv *priv = dev_get_priv(clk->dev);
676 debug("%s\n", __func__);
678 clk_id = priv->clk[id].clk_id;
680 versal_clock_get_rate(clk_id, &clk_rate);
685 static ulong versal_clk_set_rate(struct clk *clk, ulong rate)
687 struct versal_clk_priv *priv = dev_get_priv(clk->dev);
694 debug("%s\n", __func__);
696 clk_id = priv->clk[id].clk_id;
698 ret = versal_clock_get_rate(clk_id, &clk_rate);
700 printf("Clock is not a Gate:0x%x\n", clk_id);
705 if (versal_clock_div(clk_id)) {
706 div = versal_clock_get_div(clk_id);
708 div = DIV_ROUND_CLOSEST(clk_rate, rate);
709 versal_clock_set_div(clk_id, div);
710 debug("%s, div:%d, newrate:%lld\n", __func__,
711 div, DIV_ROUND_CLOSEST(clk_rate, div));
712 return DIV_ROUND_CLOSEST(clk_rate, div);
714 clk_id = versal_clock_get_parentid(clk_id);
715 } while (((clk_id >> NODE_SUBCLASS_SHIFT) &
716 NODE_CLASS_MASK) != NODE_SUBCLASS_CLOCK_REF);
718 printf("Clock didn't has Divisors:0x%x\n", priv->clk[id].clk_id);
723 static int versal_clk_enable(struct clk *clk)
725 struct versal_clk_priv *priv = dev_get_priv(clk->dev);
728 clk_id = priv->clk[clk->id].clk_id;
730 if (versal_clock_gate(clk_id))
731 return xilinx_pm_request(PM_CLOCK_ENABLE, clk_id, 0, 0, 0, NULL);
736 static struct clk_ops versal_clk_ops = {
737 .set_rate = versal_clk_set_rate,
738 .get_rate = versal_clk_get_rate,
739 .enable = versal_clk_enable,
742 static const struct udevice_id versal_clk_ids[] = {
743 { .compatible = "xlnx,versal-clk" },
744 { .compatible = "xlnx,versal-net-clk" },
748 U_BOOT_DRIVER(versal_clk) = {
749 .name = "versal-clk",
751 .of_match = versal_clk_ids,
752 .probe = versal_clk_probe,
753 .ops = &versal_clk_ops,
754 .priv_auto = sizeof(struct versal_clk_priv),