summaryrefslogtreecommitdiffstats
path: root/drivers/clk/zynqmp/clkc.c
blob: 366a12e70a6c64cdd6f2f429e59150094d0b7f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// SPDX-License-Identifier: GPL-2.0
/*
 * Zynq UltraScale+ MPSoC Clock Controller
 *
 * Copyright (C) 2019 Pengutronix, Michael Tretter <m.tretter@pengutronix.de>
 *
 * Based on the Linux driver in drivers/clk/zynqmp/
 *
 * Copyright (C) 2016-2018 Xilinx
 */

#include <common.h>
#include <init.h>
#include <linux/bitfield.h>
#include <linux/clk.h>
#include <linux/clkdev.h>
#include <mach/firmware-zynqmp.h>

#include "clk-zynqmp.h"

#define MAX_PARENT			100
#define MAX_NODES			6
#define MAX_NAME_LEN			50

#define CLK_TYPE_FIELD_MASK		GENMASK(3, 0)
#define CLK_FLAG_FIELD_MASK		GENMASK(21, 8)
#define CLK_TYPE_FLAG_FIELD_MASK	GENMASK(31, 24)

#define CLK_PARENTS_ID_MASK		GENMASK(15, 0)
#define CLK_PARENTS_TYPE_MASK		GENMASK(31, 16)

#define CLK_GET_ATTR_VALID		BIT(0)
#define CLK_GET_ATTR_TYPE		BIT(2)

#define CLK_NAME_RESERVED		""
#define CLK_NAME_DUMMY			"dummy_name"

#define CLK_GET_NAME_RESP_LEN		16
#define CLK_GET_TOPOLOGY_RESP_WORDS	3
#define CLK_GET_PARENTS_RESP_WORDS	3

#define CLK_GET_PARENTS_NONE		(-1)
#define CLK_GET_PARENTS_DUMMY		(-2)

enum clk_type {
	CLK_TYPE_INVALID,
	CLK_TYPE_OUTPUT,
	CLK_TYPE_EXTERNAL,
};

enum parent_type {
	PARENT_CLK_SELF,
	PARENT_CLK_NODE1,
	PARENT_CLK_NODE2,
	PARENT_CLK_NODE3,
	PARENT_CLK_NODE4,
	PARENT_CLK_EXTERNAL,
};

struct clock_parent {
	char name[MAX_NAME_LEN];
	enum parent_type type;
};

struct zynqmp_clock_info {
	char clk_name[MAX_NAME_LEN];
	bool valid;
	enum clk_type type;
	struct clock_topology node[MAX_NODES];
	unsigned int num_nodes;
	struct clock_parent parent[MAX_PARENT];
	unsigned int num_parents;
};

static const char clk_type_postfix[][10] = {
	[TYPE_INVALID] = "",
	[TYPE_MUX] = "_mux",
	[TYPE_GATE] = "",
	[TYPE_DIV1] = "_div1",
	[TYPE_DIV2] = "_div2",
	[TYPE_FIXEDFACTOR] = "_ff",
	[TYPE_PLL] = ""
};

static struct clk *(*const clk_topology[]) (const char *name,
				            unsigned int clk_id,
					    const char **parents,
					    unsigned int num_parents,
					    const struct clock_topology *nodes)
					= {
	[TYPE_INVALID] = NULL,
	[TYPE_MUX] = zynqmp_clk_register_mux,
	[TYPE_PLL] = zynqmp_clk_register_pll,
	[TYPE_FIXEDFACTOR] = zynqmp_clk_register_fixed_factor,
	[TYPE_DIV1] = zynqmp_clk_register_divider,
	[TYPE_DIV2] = zynqmp_clk_register_divider,
	[TYPE_GATE] = zynqmp_clk_register_gate
};

static struct zynqmp_clock_info *clock_info;
static const struct zynqmp_eemi_ops *eemi_ops;

static inline bool zynqmp_is_valid_clock(unsigned int id)
{
	return clock_info[id].valid;
}

static char *zynqmp_get_clock_name(unsigned int id)
{
	if (!zynqmp_is_valid_clock(id))
		return ERR_PTR(-EINVAL);

	return clock_info[id].clk_name;
}

static enum clk_type zynqmp_get_clock_type(unsigned int id)
{
	if (!zynqmp_is_valid_clock(id))
		return CLK_TYPE_INVALID;

	return clock_info[id].type;
}

static int zynqmp_get_parent_names(struct device_node *np,
				   unsigned int clk_id,
				   const char **parent_names)
{
	int i;
	struct clock_topology *clk_nodes;
	struct clock_parent *parents;
	int ret;

	clk_nodes = clock_info[clk_id].node;
	parents = clock_info[clk_id].parent;

	for (i = 0; i < clock_info[clk_id].num_parents; i++) {
		switch (parents[i].type) {
		case PARENT_CLK_SELF:
			break;
		case PARENT_CLK_EXTERNAL:
			ret = of_property_match_string(np, "clock-names",
						       parents[i].name);
			if (ret < 0)
				strcpy(parents[i].name, CLK_NAME_DUMMY);
			break;
		default:
			strcat(parents[i].name,
			       clk_type_postfix[clk_nodes[parents[i].type - 1].type]);
			break;
		}
		parent_names[i] = parents[i].name;
	}

	return clock_info[clk_id].num_parents;
}

static int zynqmp_pm_clock_query_num_clocks(void)
{
	struct zynqmp_pm_query_data qdata = {0};
	u32 ret_payload[PAYLOAD_ARG_CNT];
	int ret;

	qdata.qid = PM_QID_CLOCK_GET_NUM_CLOCKS;

	ret = eemi_ops->query_data(qdata, ret_payload);
	if (ret)
		return ret;

	return ret_payload[1];
}

static int zynqmp_pm_clock_query_name(unsigned int id, char *response)
{
	struct zynqmp_pm_query_data qdata = {0};
	u32 ret_payload[PAYLOAD_ARG_CNT];
	int ret;

	qdata.qid = PM_QID_CLOCK_GET_NAME;
	qdata.arg1 = id;

	ret = eemi_ops->query_data(qdata, ret_payload);
	if (ret)
		return ret;

	memcpy(response, ret_payload, CLK_GET_NAME_RESP_LEN);

	return 0;
}

struct zynqmp_pm_query_fixed_factor {
	unsigned int mul;
	unsigned int div;
};

static int zynqmp_pm_clock_query_fixed_factor(unsigned int id,
		struct zynqmp_pm_query_fixed_factor *response)
{
	struct zynqmp_pm_query_data qdata = {0};
	u32 ret_payload[PAYLOAD_ARG_CNT];
	int ret;

	qdata.qid = PM_QID_CLOCK_GET_FIXEDFACTOR_PARAMS;
	qdata.arg1 = id;

	ret = eemi_ops->query_data(qdata, ret_payload);
	if (ret)
		return ret;

	response->mul = ret_payload[1];
	response->div = ret_payload[2];

	return 0;
}

struct zynqmp_pm_query_topology {
	unsigned int node[CLK_GET_TOPOLOGY_RESP_WORDS];
};

static int zynqmp_pm_clock_query_topology(unsigned int id, unsigned int index,
		struct zynqmp_pm_query_topology *response)
{
	struct zynqmp_pm_query_data qdata;
	u32 ret_payload[PAYLOAD_ARG_CNT];
	int ret;

	memset(&qdata, 0, sizeof(qdata));
	qdata.qid = PM_QID_CLOCK_GET_TOPOLOGY;
	qdata.arg1 = id;
	qdata.arg2 = index;

	ret = eemi_ops->query_data(qdata, ret_payload);
	if (ret)
		return ret;

	memcpy(response, &ret_payload[1], sizeof(*response));

	return 0;
}

struct zynqmp_pm_query_parents {
	unsigned int parent[CLK_GET_PARENTS_RESP_WORDS];
};

static int zynqmp_pm_clock_query_parents(unsigned int id, unsigned int index,
		struct zynqmp_pm_query_parents *response)
{
	struct zynqmp_pm_query_data qdata;
	u32 ret_payload[PAYLOAD_ARG_CNT];
	int ret;

	memset(&qdata, 0, sizeof(qdata));
	qdata.qid = PM_QID_CLOCK_GET_PARENTS;
	qdata.arg1 = id;
	qdata.arg2 = index;

	ret = eemi_ops->query_data(qdata, ret_payload);
	if (ret)
		return ret;

	memcpy(response, &ret_payload[1], sizeof(*response));

	return 0;
}

static int zynqmp_pm_clock_query_attributes(unsigned int id, unsigned int *attr)
{
	struct zynqmp_pm_query_data qdata;
	u32 ret_payload[PAYLOAD_ARG_CNT];
	int ret;

	memset(&qdata, 0, sizeof(qdata));
	qdata.qid = PM_QID_CLOCK_GET_ATTRIBUTES;
	qdata.arg1 = id;

	ret = eemi_ops->query_data(qdata, ret_payload);
	if (ret)
		return ret;

	*attr = ret_payload[1];

	return 0;
}

static int zynqmp_clock_parse_topology(struct clock_topology *topology,
				       struct zynqmp_pm_query_topology *response,
				       size_t max_nodes)
{
	int i;
	enum topology_type type;

	for (i = 0; i < max_nodes && i < ARRAY_SIZE(response->node); i++) {
		type = FIELD_GET(CLK_TYPE_FIELD_MASK, response->node[i]);
		if (type == TYPE_INVALID)
			break;

		topology[i].type = type;
		topology[i].flag =
			FIELD_GET(CLK_FLAG_FIELD_MASK, response->node[i]);
		topology[i].type_flag =
			FIELD_GET(CLK_TYPE_FLAG_FIELD_MASK, response->node[i]);
	}

	return i;
}

static int zynqmp_clock_parse_parents(struct clock_parent *parents,
				      struct zynqmp_pm_query_parents *response,
				      size_t max_parents)
{
	int i;
	struct clock_parent *parent;
	char *parent_name;
	int parent_id;

	for (i = 0; i < max_parents && i < ARRAY_SIZE(response->parent); i++) {
		if (response->parent[i] == CLK_GET_PARENTS_NONE)
			break;

		parent = &parents[i];
		if (response->parent[i] == CLK_GET_PARENTS_DUMMY) {
			parent->type = PARENT_CLK_SELF;
			strncpy(parent->name, CLK_NAME_DUMMY, MAX_NAME_LEN);
			continue;
		}

		parent_id = FIELD_GET(CLK_PARENTS_ID_MASK, response->parent[i]);
		parent_name = zynqmp_get_clock_name(parent_id);
		if (IS_ERR(parent_name))
			continue;

		strncpy(parent->name, parent_name, MAX_NAME_LEN);
		parent->type = FIELD_GET(CLK_PARENTS_TYPE_MASK, response->parent[i]);
	}

	return i;
}

static int zynqmp_clock_get_topology(unsigned int id,
				     struct clock_topology *topology,
				     size_t max_nodes)
{
	int ret;
	int i;
	struct zynqmp_pm_query_topology response;

	for (i = 0; i < max_nodes; i += ret) {
		ret = zynqmp_pm_clock_query_topology(id, i, &response);
		if (ret)
			return ret;

		ret = zynqmp_clock_parse_topology(&topology[i], &response,
						  max_nodes - i);
		if (ret == 0)
			break;
	}

	return i;
}

static int zynqmp_clock_get_parents(unsigned int clk_id,
				    struct clock_parent *parents,
				    size_t max_parents)
{
	int ret;
	int i;
	struct zynqmp_pm_query_parents response;

	for (i = 0; i < max_parents; i += ret) {
		ret = zynqmp_pm_clock_query_parents(clk_id, i, &response);
		if (ret)
			return ret;

		ret = zynqmp_clock_parse_parents(&parents[i], &response,
						 max_parents - i);
		if (ret == 0)
			break;
	}

	return i;
}

struct clk *zynqmp_clk_register_fixed_factor(const char *name,
					     unsigned int clk_id,
					     const char **parents,
					     unsigned int num_parents,
					     const struct clock_topology *topology)
{
	unsigned int err;
	struct zynqmp_pm_query_fixed_factor response = {0};
	unsigned flags;

	err = zynqmp_pm_clock_query_fixed_factor(clk_id, &response);
	if (err)
		return ERR_PTR(err);

	flags = topology->flag;

	return clk_fixed_factor(strdup(name), strdup(parents[0]),
			        response.mul, response.div, flags);
}

static struct clk *zynqmp_register_clk_topology(char *clk_name,
					        int clk_id,
						int num_parents,
						const char **parent_names)
{
	int i;
	unsigned int num_nodes;
	char tmp_name[MAX_NAME_LEN];
	char parent_name[MAX_NAME_LEN];
	struct clock_topology *nodes;
	struct clk *clk = NULL;

	nodes = clock_info[clk_id].node;
	num_nodes = clock_info[clk_id].num_nodes;

	for (i = 0; i < num_nodes; i++) {
		if (!clk_topology[nodes[i].type])
			continue;

		/*
		 * Register only the last sub-node in the chain with the name of the
		 * original clock, but postfix other sub-node inside the chain with
		 * their type.
		 */
		snprintf(tmp_name, MAX_NAME_LEN, "%s%s", clk_name,
			(i == num_nodes - 1) ? "" : clk_type_postfix[nodes[i].type]);

		clk = (*clk_topology[nodes[i].type])(tmp_name, clk_id,
						     parent_names,
						     num_parents,
						     &nodes[i]);
		if (IS_ERR(clk))
			pr_warn("failed to register node %s of clock %s\n",
				tmp_name, clk_name);

		/*
		 * Only link the first sub-node to the original (first)
		 * parent, but link every other sub-node with their preceeding
		 * sub-clock via the first parent.
		 */
		parent_names[0] = parent_name;
		strncpy(parent_name, tmp_name, MAX_NAME_LEN);
	}

	return clk;
}

static int zynqmp_register_clocks(struct device_d *dev,
				  struct clk **clks, size_t num_clocks)
{
	unsigned int i;
	const char *parent_names[MAX_PARENT];
	char *name;
	struct device_node *node = dev->device_node;
	unsigned int num_parents;

	for (i = 0; i < num_clocks; i++) {
		if (zynqmp_get_clock_type(i) != CLK_TYPE_OUTPUT)
			continue;
		name = zynqmp_get_clock_name(i);
		if (IS_ERR(name))
			continue;

		num_parents = zynqmp_get_parent_names(node, i, parent_names);
		if (num_parents < 0) {
			dev_warn(dev, "failed to find parents for %s\n", name);
			continue;
		}

		clks[i] = zynqmp_register_clk_topology(name, i, num_parents,
						       parent_names);
		if (IS_ERR_OR_NULL(clks[i]))
			dev_warn(dev, "failed to register clock %s: %ld\n",
				 name, PTR_ERR(clks[i]));
	}

	return 0;
}

static void zynqmp_fill_clock_info(struct zynqmp_clock_info *clock_info,
				   size_t num_clocks)
{
	int i;
	int ret;
	unsigned int attr;

	for (i = 0; i < num_clocks; i++) {
		zynqmp_pm_clock_query_name(i, clock_info[i].clk_name);
		if (!strcmp(clock_info[i].clk_name, CLK_NAME_RESERVED))
			continue;

		ret = zynqmp_pm_clock_query_attributes(i, &attr);
		if (ret)
			continue;

		clock_info[i].valid = attr & CLK_GET_ATTR_VALID;
		clock_info[i].type = attr & CLK_GET_ATTR_TYPE ?
			CLK_TYPE_EXTERNAL : CLK_TYPE_OUTPUT;
	}

	for (i = 0; i < num_clocks; i++) {
		if (zynqmp_get_clock_type(i) != CLK_TYPE_OUTPUT)
			continue;
		clock_info[i].num_nodes =
			zynqmp_clock_get_topology(i, clock_info[i].node,
						  ARRAY_SIZE(clock_info[i].node));
	}

	for (i = 0; i < num_clocks; i++) {
		if (zynqmp_get_clock_type(i) != CLK_TYPE_OUTPUT)
			continue;
		ret = zynqmp_clock_get_parents(i, clock_info[i].parent,
					       ARRAY_SIZE(clock_info[i].parent));
		if (ret < 0)
			continue;
		clock_info[i].num_parents = ret;
	}
}

static int zynqmp_clock_probe(struct device_d *dev)
{
	int err;
	u32 api_version;
	int num_clocks;
	struct clk_onecell_data *clk_data;

	eemi_ops = zynqmp_pm_get_eemi_ops();
	if (!eemi_ops)
		return -ENODEV;

	/* Check version to make sure firmware is available */
	err = eemi_ops->get_api_version(&api_version);
	if (err) {
		dev_err(dev, "Firmware not available\n");
		return err;
	}

	num_clocks = zynqmp_pm_clock_query_num_clocks();
	if (num_clocks < 1)
		return num_clocks;

	clock_info = kcalloc(num_clocks, sizeof(*clock_info), GFP_KERNEL);
	if (!clock_info)
		return -ENOMEM;

	zynqmp_fill_clock_info(clock_info, num_clocks);

	clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
	if (!clk_data)
		return -ENOMEM;

	clk_data->clks = kcalloc(num_clocks, sizeof(*clk_data->clks), GFP_KERNEL);
	if (!clk_data->clks) {
		kfree(clk_data);
		return -ENOMEM;
	}

	zynqmp_register_clocks(dev, clk_data->clks, num_clocks);
	clk_data->clk_num = num_clocks;
	of_clk_add_provider(dev->device_node, of_clk_src_onecell_get, clk_data);

	/*
	 * We can free clock_info now, as is only used to store clock info
	 * from firmware for registering the clocks.
	 */
	kfree(clock_info);

	return 0;
}

static struct of_device_id zynqmp_clock_of_match[] = {
	{.compatible = "xlnx,zynqmp-clk"},
	{},
};

static struct driver_d zynqmp_clock_driver = {
	.probe	= zynqmp_clock_probe,
	.name	= "zynqmp_clock",
	.of_compatible = DRV_OF_COMPAT(zynqmp_clock_of_match),
};
postcore_platform_driver(zynqmp_clock_driver);