1 // SPDX-License-Identifier: GPL-2.0
3 * Zynq UltraScale+ MPSoC clock controller
5 * Copyright (C) 2016-2019 Xilinx
7 * Based on drivers/clk/zynq/clkc.c
10 #include <linux/bitfield.h>
11 #include <linux/clk.h>
12 #include <linux/clk-provider.h>
13 #include <linux/module.h>
14 #include <linux/of_platform.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
18 #include "clk-zynqmp.h"
20 #define MAX_PARENT 100
22 #define MAX_NAME_LEN 50
24 /* Flags for parents */
25 #define PARENT_CLK_SELF 0
26 #define PARENT_CLK_NODE1 1
27 #define PARENT_CLK_NODE2 2
28 #define PARENT_CLK_NODE3 3
29 #define PARENT_CLK_NODE4 4
30 #define PARENT_CLK_EXTERNAL 5
32 #define END_OF_CLK_NAME "END_OF_CLK"
33 #define END_OF_TOPOLOGY_NODE 1
34 #define END_OF_PARENTS 1
35 #define RESERVED_CLK_NAME ""
37 #define CLK_GET_NAME_RESP_LEN 16
38 #define CLK_GET_TOPOLOGY_RESP_WORDS 3
39 #define CLK_GET_PARENTS_RESP_WORDS 3
40 #define CLK_GET_ATTR_RESP_WORDS 1
48 * struct clock_parent - Clock parent
50 * @id: Parent clock ID
54 char name[MAX_NAME_LEN];
60 * struct zynqmp_clock - Clock
61 * @clk_name: Clock name
62 * @valid: Validity flag of clock
63 * @type: Clock type (Output/External)
64 * @node: Clock topology nodes
65 * @num_nodes: Number of nodes present in topology
66 * @parent: Parent of clock
67 * @num_parents: Number of parents of clock
71 char clk_name[MAX_NAME_LEN];
74 struct clock_topology node[MAX_NODES];
76 struct clock_parent parent[MAX_PARENT];
82 char name[CLK_GET_NAME_RESP_LEN];
85 struct topology_resp {
86 #define CLK_TOPOLOGY_TYPE GENMASK(3, 0)
87 #define CLK_TOPOLOGY_CUSTOM_TYPE_FLAGS GENMASK(7, 4)
88 #define CLK_TOPOLOGY_FLAGS GENMASK(23, 8)
89 #define CLK_TOPOLOGY_TYPE_FLAGS GENMASK(31, 24)
90 u32 topology[CLK_GET_TOPOLOGY_RESP_WORDS];
94 #define NA_PARENT 0xFFFFFFFF
95 #define DUMMY_PARENT 0xFFFFFFFE
96 #define CLK_PARENTS_ID GENMASK(15, 0)
97 #define CLK_PARENTS_FLAGS GENMASK(31, 16)
98 u32 parents[CLK_GET_PARENTS_RESP_WORDS];
102 #define CLK_ATTR_VALID BIT(0)
103 #define CLK_ATTR_TYPE BIT(2)
104 #define CLK_ATTR_NODE_INDEX GENMASK(13, 0)
105 #define CLK_ATTR_NODE_TYPE GENMASK(19, 14)
106 #define CLK_ATTR_NODE_SUBCLASS GENMASK(25, 20)
107 #define CLK_ATTR_NODE_CLASS GENMASK(31, 26)
108 u32 attr[CLK_GET_ATTR_RESP_WORDS];
111 static const char clk_type_postfix[][10] = {
115 [TYPE_DIV1] = "_div1",
116 [TYPE_DIV2] = "_div2",
117 [TYPE_FIXEDFACTOR] = "_ff",
121 static struct clk_hw *(* const clk_topology[]) (const char *name, u32 clk_id,
122 const char * const *parents,
124 const struct clock_topology *nodes)
126 [TYPE_INVALID] = NULL,
127 [TYPE_MUX] = zynqmp_clk_register_mux,
128 [TYPE_PLL] = zynqmp_clk_register_pll,
129 [TYPE_FIXEDFACTOR] = zynqmp_clk_register_fixed_factor,
130 [TYPE_DIV1] = zynqmp_clk_register_divider,
131 [TYPE_DIV2] = zynqmp_clk_register_divider,
132 [TYPE_GATE] = zynqmp_clk_register_gate
135 static struct zynqmp_clock *clock;
136 static struct clk_hw_onecell_data *zynqmp_data;
137 static unsigned int clock_max_idx;
140 * zynqmp_is_valid_clock() - Check whether clock is valid or not
141 * @clk_id: Clock index
143 * Return: 1 if clock is valid, 0 if clock is invalid else error code
145 static inline int zynqmp_is_valid_clock(u32 clk_id)
147 if (clk_id >= clock_max_idx)
150 return clock[clk_id].valid;
154 * zynqmp_get_clock_name() - Get name of clock from Clock index
155 * @clk_id: Clock index
156 * @clk_name: Name of clock
158 * Return: 0 on success else error code
160 static int zynqmp_get_clock_name(u32 clk_id, char *clk_name)
164 ret = zynqmp_is_valid_clock(clk_id);
166 strscpy(clk_name, clock[clk_id].clk_name, MAX_NAME_LEN);
170 return ret == 0 ? -EINVAL : ret;
174 * zynqmp_get_clock_type() - Get type of clock
175 * @clk_id: Clock index
176 * @type: Clock type: CLK_TYPE_OUTPUT or CLK_TYPE_EXTERNAL
178 * Return: 0 on success else error code
180 static int zynqmp_get_clock_type(u32 clk_id, u32 *type)
184 ret = zynqmp_is_valid_clock(clk_id);
186 *type = clock[clk_id].type;
190 return ret == 0 ? -EINVAL : ret;
194 * zynqmp_pm_clock_get_num_clocks() - Get number of clocks in system
195 * @nclocks: Number of clocks in system/board.
197 * Call firmware API to get number of clocks.
199 * Return: 0 on success else error code.
201 static int zynqmp_pm_clock_get_num_clocks(u32 *nclocks)
203 struct zynqmp_pm_query_data qdata = {0};
204 u32 ret_payload[PAYLOAD_ARG_CNT];
207 qdata.qid = PM_QID_CLOCK_GET_NUM_CLOCKS;
209 ret = zynqmp_pm_query_data(qdata, ret_payload);
210 *nclocks = ret_payload[1];
216 * zynqmp_pm_clock_get_name() - Get the name of clock for given id
217 * @clock_id: ID of the clock to be queried
218 * @response: Name of the clock with the given id
220 * This function is used to get name of clock specified by given
223 * Return: 0 on success else error+reason
225 static int zynqmp_pm_clock_get_name(u32 clock_id,
226 struct name_resp *response)
228 struct zynqmp_pm_query_data qdata = {0};
229 u32 ret_payload[PAYLOAD_ARG_CNT];
232 qdata.qid = PM_QID_CLOCK_GET_NAME;
233 qdata.arg1 = clock_id;
235 ret = zynqmp_pm_query_data(qdata, ret_payload);
239 memcpy(response, ret_payload, sizeof(*response));
245 * zynqmp_pm_clock_get_topology() - Get the topology of clock for given id
246 * @clock_id: ID of the clock to be queried
247 * @index: Node index of clock topology
248 * @response: Buffer used for the topology response
250 * This function is used to get topology information for the clock
251 * specified by given clock ID.
253 * This API will return 3 node of topology with a single response. To get
254 * other nodes, master should call same API in loop with new
255 * index till error is returned. E.g First call should have
256 * index 0 which will return nodes 0,1 and 2. Next call, index
257 * should be 3 which will return nodes 3,4 and 5 and so on.
259 * Return: 0 on success else error+reason
261 static int zynqmp_pm_clock_get_topology(u32 clock_id, u32 index,
262 struct topology_resp *response)
264 struct zynqmp_pm_query_data qdata = {0};
265 u32 ret_payload[PAYLOAD_ARG_CNT];
268 qdata.qid = PM_QID_CLOCK_GET_TOPOLOGY;
269 qdata.arg1 = clock_id;
272 ret = zynqmp_pm_query_data(qdata, ret_payload);
273 memcpy(response, &ret_payload[1], sizeof(*response));
278 unsigned long zynqmp_clk_map_common_ccf_flags(const u32 zynqmp_flag)
280 unsigned long ccf_flag = 0;
282 if (zynqmp_flag & ZYNQMP_CLK_SET_RATE_GATE)
283 ccf_flag |= CLK_SET_RATE_GATE;
284 if (zynqmp_flag & ZYNQMP_CLK_SET_PARENT_GATE)
285 ccf_flag |= CLK_SET_PARENT_GATE;
286 if (zynqmp_flag & ZYNQMP_CLK_SET_RATE_PARENT)
287 ccf_flag |= CLK_SET_RATE_PARENT;
288 if (zynqmp_flag & ZYNQMP_CLK_IGNORE_UNUSED)
289 ccf_flag |= CLK_IGNORE_UNUSED;
290 if (zynqmp_flag & ZYNQMP_CLK_SET_RATE_NO_REPARENT)
291 ccf_flag |= CLK_SET_RATE_NO_REPARENT;
292 if (zynqmp_flag & ZYNQMP_CLK_IS_CRITICAL)
293 ccf_flag |= CLK_IS_CRITICAL;
299 * zynqmp_clk_register_fixed_factor() - Register fixed factor with the
301 * @name: Name of this clock
303 * @parents: Name of this clock's parents
304 * @num_parents: Number of parents
305 * @nodes: Clock topology node
307 * Return: clock hardware to the registered clock
309 struct clk_hw *zynqmp_clk_register_fixed_factor(const char *name, u32 clk_id,
310 const char * const *parents,
312 const struct clock_topology *nodes)
316 struct zynqmp_pm_query_data qdata = {0};
317 u32 ret_payload[PAYLOAD_ARG_CNT];
321 qdata.qid = PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS;
324 ret = zynqmp_pm_query_data(qdata, ret_payload);
328 mult = ret_payload[1];
329 div = ret_payload[2];
331 flag = zynqmp_clk_map_common_ccf_flags(nodes->flag);
333 hw = clk_hw_register_fixed_factor(NULL, name,
342 * zynqmp_pm_clock_get_parents() - Get the first 3 parents of clock for given id
343 * @clock_id: Clock ID
344 * @index: Parent index
345 * @response: Parents of the given clock
347 * This function is used to get 3 parents for the clock specified by
350 * This API will return 3 parents with a single response. To get
351 * other parents, master should call same API in loop with new
352 * parent index till error is returned. E.g First call should have
353 * index 0 which will return parents 0,1 and 2. Next call, index
354 * should be 3 which will return parent 3,4 and 5 and so on.
356 * Return: 0 on success else error+reason
358 static int zynqmp_pm_clock_get_parents(u32 clock_id, u32 index,
359 struct parents_resp *response)
361 struct zynqmp_pm_query_data qdata = {0};
362 u32 ret_payload[PAYLOAD_ARG_CNT];
365 qdata.qid = PM_QID_CLOCK_GET_PARENTS;
366 qdata.arg1 = clock_id;
369 ret = zynqmp_pm_query_data(qdata, ret_payload);
370 memcpy(response, &ret_payload[1], sizeof(*response));
376 * zynqmp_pm_clock_get_attributes() - Get the attributes of clock for given id
377 * @clock_id: Clock ID
378 * @response: Clock attributes response
380 * This function is used to get clock's attributes(e.g. valid, clock type, etc).
382 * Return: 0 on success else error+reason
384 static int zynqmp_pm_clock_get_attributes(u32 clock_id,
385 struct attr_resp *response)
387 struct zynqmp_pm_query_data qdata = {0};
388 u32 ret_payload[PAYLOAD_ARG_CNT];
391 qdata.qid = PM_QID_CLOCK_GET_ATTRIBUTES;
392 qdata.arg1 = clock_id;
394 ret = zynqmp_pm_query_data(qdata, ret_payload);
395 memcpy(response, &ret_payload[1], sizeof(*response));
401 * __zynqmp_clock_get_topology() - Get topology data of clock from firmware
403 * @topology: Clock topology
404 * @response: Clock topology data received from firmware
405 * @nnodes: Number of nodes
407 * Return: 0 on success else error+reason
409 static int __zynqmp_clock_get_topology(struct clock_topology *topology,
410 struct topology_resp *response,
416 for (i = 0; i < ARRAY_SIZE(response->topology); i++) {
417 type = FIELD_GET(CLK_TOPOLOGY_TYPE, response->topology[i]);
418 if (type == TYPE_INVALID)
419 return END_OF_TOPOLOGY_NODE;
420 topology[*nnodes].type = type;
421 topology[*nnodes].flag = FIELD_GET(CLK_TOPOLOGY_FLAGS,
422 response->topology[i]);
423 topology[*nnodes].type_flag =
424 FIELD_GET(CLK_TOPOLOGY_TYPE_FLAGS,
425 response->topology[i]);
426 topology[*nnodes].custom_type_flag =
427 FIELD_GET(CLK_TOPOLOGY_CUSTOM_TYPE_FLAGS,
428 response->topology[i]);
436 * zynqmp_clock_get_topology() - Get topology of clock from firmware using
438 * @clk_id: Clock index
439 * @topology: Clock topology
440 * @num_nodes: Number of nodes
442 * Return: 0 on success else error+reason
444 static int zynqmp_clock_get_topology(u32 clk_id,
445 struct clock_topology *topology,
449 struct topology_resp response = { };
452 for (j = 0; j <= MAX_NODES; j += ARRAY_SIZE(response.topology)) {
453 ret = zynqmp_pm_clock_get_topology(clock[clk_id].clk_id, j,
457 ret = __zynqmp_clock_get_topology(topology, &response,
459 if (ret == END_OF_TOPOLOGY_NODE)
467 * __zynqmp_clock_get_parents() - Get parents info of clock from firmware
469 * @parents: Clock parents
470 * @response: Clock parents data received from firmware
471 * @nparent: Number of parent
473 * Return: 0 on success else error+reason
475 static int __zynqmp_clock_get_parents(struct clock_parent *parents,
476 struct parents_resp *response,
480 struct clock_parent *parent;
482 for (i = 0; i < ARRAY_SIZE(response->parents); i++) {
483 if (response->parents[i] == NA_PARENT)
484 return END_OF_PARENTS;
486 parent = &parents[i];
487 parent->id = FIELD_GET(CLK_PARENTS_ID, response->parents[i]);
488 if (response->parents[i] == DUMMY_PARENT) {
489 strcpy(parent->name, "dummy_name");
492 parent->flag = FIELD_GET(CLK_PARENTS_FLAGS,
493 response->parents[i]);
494 if (zynqmp_get_clock_name(parent->id, parent->name))
504 * zynqmp_clock_get_parents() - Get parents info from firmware using PM_API
505 * @clk_id: Clock index
506 * @parents: Clock parents
507 * @num_parents: Total number of parents
509 * Return: 0 on success else error+reason
511 static int zynqmp_clock_get_parents(u32 clk_id, struct clock_parent *parents,
515 struct parents_resp response = { };
519 /* Get parents from firmware */
520 ret = zynqmp_pm_clock_get_parents(clock[clk_id].clk_id, j,
525 ret = __zynqmp_clock_get_parents(&parents[j], &response,
527 if (ret == END_OF_PARENTS)
529 j += ARRAY_SIZE(response.parents);
530 } while (*num_parents <= MAX_PARENT);
536 * zynqmp_get_parent_list() - Create list of parents name
538 * @clk_id: Clock index
539 * @parent_list: List of parent's name
540 * @num_parents: Total number of parents
542 * Return: 0 on success else error+reason
544 static int zynqmp_get_parent_list(struct device_node *np, u32 clk_id,
545 const char **parent_list, u32 *num_parents)
548 u32 total_parents = clock[clk_id].num_parents;
549 struct clock_topology *clk_nodes;
550 struct clock_parent *parents;
552 clk_nodes = clock[clk_id].node;
553 parents = clock[clk_id].parent;
555 for (i = 0; i < total_parents; i++) {
556 if (!parents[i].flag) {
557 parent_list[i] = parents[i].name;
558 } else if (parents[i].flag == PARENT_CLK_EXTERNAL) {
559 ret = of_property_match_string(np, "clock-names",
562 strcpy(parents[i].name, "dummy_name");
563 parent_list[i] = parents[i].name;
565 strcat(parents[i].name,
566 clk_type_postfix[clk_nodes[parents[i].flag - 1].
568 parent_list[i] = parents[i].name;
572 *num_parents = total_parents;
577 * zynqmp_register_clk_topology() - Register clock topology
578 * @clk_id: Clock index
579 * @clk_name: Clock Name
580 * @num_parents: Total number of parents
581 * @parent_names: List of parents name
583 * Return: Returns either clock hardware or error+reason
585 static struct clk_hw *zynqmp_register_clk_topology(int clk_id, char *clk_name,
587 const char **parent_names)
590 u32 num_nodes, clk_dev_id;
591 char *clk_out[MAX_NODES];
592 struct clock_topology *nodes;
593 struct clk_hw *hw = NULL;
595 nodes = clock[clk_id].node;
596 num_nodes = clock[clk_id].num_nodes;
597 clk_dev_id = clock[clk_id].clk_id;
599 for (j = 0; j < num_nodes; j++) {
601 * Clock name received from firmware is output clock name.
602 * Intermediate clock names are postfixed with type of clock.
604 if (j != (num_nodes - 1)) {
605 clk_out[j] = kasprintf(GFP_KERNEL, "%s%s", clk_name,
606 clk_type_postfix[nodes[j].type]);
608 clk_out[j] = kasprintf(GFP_KERNEL, "%s", clk_name);
611 if (!clk_topology[nodes[j].type])
614 hw = (*clk_topology[nodes[j].type])(clk_out[j], clk_dev_id,
619 pr_warn_once("%s() 0x%x: %s register fail with %ld\n",
620 __func__, clk_dev_id, clk_name,
623 parent_names[0] = clk_out[j];
626 for (j = 0; j < num_nodes; j++)
633 * zynqmp_register_clocks() - Register clocks
636 * Return: 0 on success else error code
638 static int zynqmp_register_clocks(struct device_node *np)
641 u32 i, total_parents = 0, type = 0;
642 const char *parent_names[MAX_PARENT];
644 for (i = 0; i < clock_max_idx; i++) {
645 char clk_name[MAX_NAME_LEN];
647 /* get clock name, continue to next clock if name not found */
648 if (zynqmp_get_clock_name(i, clk_name))
651 /* Check if clock is valid and output clock.
652 * Do not register invalid or external clock.
654 ret = zynqmp_get_clock_type(i, &type);
655 if (ret || type != CLK_TYPE_OUTPUT)
658 /* Get parents of clock*/
659 if (zynqmp_get_parent_list(np, i, parent_names,
661 WARN_ONCE(1, "No parents found for %s\n",
666 zynqmp_data->hws[i] =
667 zynqmp_register_clk_topology(i, clk_name,
672 for (i = 0; i < clock_max_idx; i++) {
673 if (IS_ERR(zynqmp_data->hws[i])) {
674 pr_err("Zynq Ultrascale+ MPSoC clk %s: register failed with %ld\n",
675 clock[i].clk_name, PTR_ERR(zynqmp_data->hws[i]));
683 * zynqmp_get_clock_info() - Get clock information from firmware using PM_API
685 static void zynqmp_get_clock_info(void)
689 u32 nodetype, subclass, class;
690 struct attr_resp attr;
691 struct name_resp name;
693 for (i = 0; i < clock_max_idx; i++) {
694 ret = zynqmp_pm_clock_get_attributes(i, &attr);
698 clock[i].valid = FIELD_GET(CLK_ATTR_VALID, attr.attr[0]);
699 /* skip query for Invalid clock */
700 ret = zynqmp_is_valid_clock(i);
701 if (ret != CLK_ATTR_VALID)
704 clock[i].type = FIELD_GET(CLK_ATTR_TYPE, attr.attr[0]) ?
705 CLK_TYPE_EXTERNAL : CLK_TYPE_OUTPUT;
707 nodetype = FIELD_GET(CLK_ATTR_NODE_TYPE, attr.attr[0]);
708 subclass = FIELD_GET(CLK_ATTR_NODE_SUBCLASS, attr.attr[0]);
709 class = FIELD_GET(CLK_ATTR_NODE_CLASS, attr.attr[0]);
711 clock[i].clk_id = FIELD_PREP(CLK_ATTR_NODE_CLASS, class) |
712 FIELD_PREP(CLK_ATTR_NODE_SUBCLASS, subclass) |
713 FIELD_PREP(CLK_ATTR_NODE_TYPE, nodetype) |
714 FIELD_PREP(CLK_ATTR_NODE_INDEX, i);
716 zynqmp_pm_clock_get_name(clock[i].clk_id, &name);
719 * Terminate with NULL character in case name provided by firmware
720 * is longer and truncated due to size limit.
722 name.name[sizeof(name.name) - 1] = '\0';
724 if (!strcmp(name.name, RESERVED_CLK_NAME))
726 strscpy(clock[i].clk_name, name.name, MAX_NAME_LEN);
729 /* Get topology of all clock */
730 for (i = 0; i < clock_max_idx; i++) {
731 ret = zynqmp_get_clock_type(i, &type);
732 if (ret || type != CLK_TYPE_OUTPUT)
735 ret = zynqmp_clock_get_topology(i, clock[i].node,
736 &clock[i].num_nodes);
740 ret = zynqmp_clock_get_parents(i, clock[i].parent,
741 &clock[i].num_parents);
748 * zynqmp_clk_setup() - Setup the clock framework and register clocks
751 * Return: 0 on success else error code
753 static int zynqmp_clk_setup(struct device_node *np)
757 ret = zynqmp_pm_clock_get_num_clocks(&clock_max_idx);
761 zynqmp_data = kzalloc(struct_size(zynqmp_data, hws, clock_max_idx),
766 clock = kcalloc(clock_max_idx, sizeof(*clock), GFP_KERNEL);
772 zynqmp_get_clock_info();
773 zynqmp_register_clocks(np);
775 zynqmp_data->num = clock_max_idx;
776 return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, zynqmp_data);
779 static int zynqmp_clock_probe(struct platform_device *pdev)
782 struct device *dev = &pdev->dev;
784 ret = zynqmp_clk_setup(dev->of_node);
789 static const struct of_device_id zynqmp_clock_of_match[] = {
790 {.compatible = "xlnx,zynqmp-clk"},
791 {.compatible = "xlnx,versal-clk"},
794 MODULE_DEVICE_TABLE(of, zynqmp_clock_of_match);
796 static struct platform_driver zynqmp_clock_driver = {
798 .name = "zynqmp_clock",
799 .of_match_table = zynqmp_clock_of_match,
801 .probe = zynqmp_clock_probe,
803 module_platform_driver(zynqmp_clock_driver);