summaryrefslogtreecommitdiff
path: root/drivers/clk/sunxi/clk-sunxi-ccu.c
blob: 6980a56da03aa997a57b6e7ad85ed35b7b540896 (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
/*
 * (C) 2017 Theobroma Systems Design und Consulting GmbH
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <dm.h>

#include "ccu_common.h"

DECLARE_GLOBAL_DATA_PTR;

struct sunxi_clk_priv {
	uint32_t *base;
	size_t  size;
	struct clk hosc_clk;
	struct clk losc_clk;

	struct clk_hw hosc, losc;
};

static ulong ccu_parent_recalc_rate(struct clk_hw *hw, ulong parent_rate)
{
	struct clk clk;

	if (clk_get_by_output_name(clk_hw_get_name(hw), &clk) < 0) {
		error("%s: unknown clk %s\n", __func__, clk_hw_get_name(hw));
		return -ENOENT;
	}

	return clk_get_rate(&clk);
}

static struct sunxi_ccu_clk_ops parent_clk_ops = {
	.recalc_rate = ccu_parent_recalc_rate,
};

static struct clk_init_data hosc_init_data = {
	.name = "osc24M",
	.ops = &parent_clk_ops,
};

static struct clk_init_data losc_init_data = {
	.name = "osc32k",
	.ops = &parent_clk_ops,
};

static inline
const struct sunxi_ccu_clk_ops *clk_hw_get_ops(const struct clk_hw *hw_clk)
{
	return hw_clk->init->ops;
}

static inline
const char *clk_hw_get_parent_name_by_index(const struct clk_hw *hw_clk,
					    unsigned int index)
{
	return hw_clk->init->parent_names[index];
}

struct clk_hw *clk_hw_get_parent_by_index(const struct clk_hw *hw,
					  unsigned int index)
{
	if (!hw->parents)
		return NULL;

	return hw->parents[index];
}

struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
{
	const struct sunxi_ccu_clk_ops *ops = clk_hw_get_ops(hw);
	u8 idx = 0;

	if (ops->get_parent)
		idx = ops->get_parent((struct clk_hw *)hw);

	return clk_hw_get_parent_by_index(hw, idx);
}

static int clk_hw_get_index_by_parent(struct clk_hw *hw,
				      struct clk_hw *parent)
{
	int i;

	for (i = 0; i < clk_hw_get_num_parents(hw); ++i)
		if (!strcmp(clk_hw_get_parent_name_by_index(hw, i),
			    clk_hw_get_name(parent)))
			return i;

	return -ENOENT;
}

const char *clk_hw_get_name(const struct clk_hw *hw_clk)
{
	return hw_clk->init->name;
}

unsigned int clk_hw_get_num_parents(const struct clk_hw *hw_clk)
{
	return hw_clk->init->num_parents;
}

unsigned long clk_hw_get_flags(const struct clk_hw *hw_clk)
{
	return hw_clk->init->flags;
}

bool clk_hw_is_enabled(const struct clk_hw *hw)
{
	const struct sunxi_ccu_clk_ops *ops = clk_hw_get_ops(hw);
	struct clk_hw *parent = clk_hw_get_parent(hw);

	if (parent)
		if (!clk_hw_is_enabled(parent))
			return false;

	if (ops->is_enabled)
		return ops->is_enabled((struct clk_hw *)hw);

	/*
	 * If a clock doesn't have an 'is_enabled', we assume it's
	 * a fixed (i.e. always-enabled) clock.
	 */
	return true;
}

static int clk_hw_enable(struct clk_hw *hw)
{
	const struct sunxi_ccu_clk_ops *ops = clk_hw_get_ops(hw);
	struct clk_hw *parent = clk_hw_get_parent(hw);

	debug("%s: clk name %s\n", __func__, clk_hw_get_name(hw));

	/*
	 * If this clock is already enabled, we assume that its parent
	 * clocks are also enabled.
	 */
	if (ops->is_enabled && ops->is_enabled(hw))
		return 0;

	/* Walk the tree and enable the parents */
	if (parent)
		clk_hw_enable(parent);

	if (ops->enable)
		ops->enable(hw);

	return 0;
}

static int clk_hw_disable(struct clk_hw *hw_clk)
{
	const struct sunxi_ccu_clk_ops *ops = clk_hw_get_ops(hw_clk);

	debug("%s: clk name %s\n", __func__, clk_hw_get_name(hw_clk));

	/* Don't try to disable it, if this clock is not enabled. */
	if (ops->is_enabled && !ops->is_enabled(hw_clk))
		return 0;

	if (ops->disable)
		ops->disable(hw_clk);

	return 0;
}

static int clk_hw_determine_rate(struct clk_hw *hw,
				 struct clk_rate_request *req)
{
	struct clk_hw *parent;
	const struct sunxi_ccu_clk_ops *ops;

	if (!hw)
		return 0;

	parent = clk_hw_get_parent(hw);

	req->best_parent_hw = NULL;
	req->best_parent_rate = 0;
	if (parent) {
		req->best_parent_hw = parent;
		req->best_parent_rate = clk_hw_get_rate(parent);
	}

	ops = clk_hw_get_ops(hw);
	if (ops->determine_rate) {
		return ops->determine_rate(hw, req);
	} else if (ops->round_rate) {
		long rate = ops->round_rate(hw, req->rate,
					    &req->best_parent_rate);

		if (rate < 0)
			return rate;

		req->rate = rate;
	} else if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
		return clk_hw_determine_rate(parent, req);
	} else {
		req->rate = clk_hw_get_rate(hw);
	}

	return 0;
}

int __clk_mux_determine_rate(struct clk_hw *hw,
			     struct clk_rate_request *req)
{
	struct clk_hw *parent, *best_parent = NULL;
	int i, num_parents, ret;
	unsigned long best = 0;
	struct clk_rate_request parent_req = *req;

	/* if NO_REPARENT flag set, pass through to current parent */
	if (clk_hw_get_flags(hw) & CLK_SET_RATE_NO_REPARENT) {
		parent = clk_hw_get_parent(hw);

		if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
			ret = clk_hw_determine_rate(parent, &parent_req);
			if (ret)
				return ret;

			best = parent_req.rate;
		} else if (parent) {
			best = clk_hw_get_rate(parent);
		} else {
			best = clk_hw_get_rate(hw);
		}

		goto out;
	}

	/* find the parent that can provide the fastest rate <= rate */
	num_parents = clk_hw_get_num_parents(hw);
	for (i = 0; i < num_parents; i++) {
		parent = clk_hw_get_parent_by_index(hw, i);
		if (!parent)
			continue;

		if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
			parent_req = *req;
			ret = clk_hw_determine_rate(parent, &parent_req);
			if (ret)
				continue;
		} else {
			parent_req.rate = clk_hw_get_rate(parent);
		}

		if (parent_req.rate <= req->rate && parent_req.rate > best) {
			best_parent = parent;
			best = parent_req.rate;
		}
	}

	if (!best_parent)
		return -EINVAL;

out:
	if (best_parent)
		req->best_parent_hw = best_parent;
	req->best_parent_rate = best;
	req->rate = best;

	return 0;
}

void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *parent)
{
	const struct sunxi_ccu_clk_ops *ops = clk_hw_get_ops(hw);
	int parent_idx = clk_hw_get_index_by_parent(hw, parent);

	if (!ops->set_parent)
		return;

	ops->set_parent(hw, parent_idx);
}

unsigned long clk_hw_get_rate(const struct clk_hw *hw)
{
	const struct sunxi_ccu_clk_ops *ops = clk_hw_get_ops(hw);
	struct clk_hw *parent = clk_hw_get_parent(hw);
	ulong parent_rate = 0;
	ulong rate = 0;

	debug("%s(%s)\n", __func__, clk_hw_get_name(hw));

	if (parent)
		parent_rate = clk_hw_get_rate(parent);

	rate = parent_rate;
	if (ops->recalc_rate)
		rate = ops->recalc_rate((struct clk_hw *)hw, parent_rate);

	return rate;
}

static struct clk_hw *ccu_parent_by_name(struct udevice *dev, const char *name)
{
	const struct sunxi_ccu_desc *desc =
		(const struct sunxi_ccu_desc *)dev_get_driver_data(dev);
	struct sunxi_clk_priv *priv = dev_get_priv(dev);
	int i;

	debug("%s(%s)\n", __func__, name);

	for (i = 0; i < desc->hw_clks->num ; i++) {
		struct clk_hw *hw = desc->hw_clks->hws[i];

		if (!hw)
			continue;

		if (!strcmp(name, clk_hw_get_name(hw)))
			return hw;
	}

	if (!strcmp(name, clk_hw_get_name(&priv->hosc)))
		return &priv->hosc;

	if (!strcmp(name, clk_hw_get_name(&priv->losc)))
		return &priv->losc;

	debug("%s: - not found => returning NULL\n", __func__);
	return NULL;
}

static ulong sunxi_clk_ccu_set_rate(struct clk *clk, ulong rate)
{
	const struct sunxi_ccu_desc *desc =
		(const struct sunxi_ccu_desc *)dev_get_driver_data(clk->dev);
	struct clk_hw *hw = desc->hw_clks->hws[clk->id];
	const struct sunxi_ccu_clk_ops *ops = clk_hw_get_ops(hw);
	struct clk_rate_request req = { .rate = rate };

	debug("%s (%s): id %ld rate %ld\n",
	      clk->dev->name, __func__, clk->id, rate);

	if (!hw)
		return -ENOENT;

	/* bail early, if nothing to do */
	if (rate == clk_hw_get_rate(hw))
		return rate;

	/* enable the clock implicitly if CLK_SET_RATE_UNGATE is set */
	if (clk_hw_get_flags(hw) & CLK_SET_RATE_UNGATE)
		clk_hw_enable(hw);

	/* Find the best clock rate (and possibly parent) */
	if (clk_hw_determine_rate(hw, &req) < 0)
		return -EINVAL;

	/* Change the parent, if necessary */
	if (req.best_parent_hw &&
	    req.best_parent_hw != clk_hw_get_parent(hw))
		clk_hw_reparent(hw, req.best_parent_hw);

	/* Now set the new rate */
	if (ops->set_rate)
		ops->set_rate(hw, req.rate, req.best_parent_rate);
	else
		return -ENOSYS;

	/* Finally recalculate the rate and return it */
	return clk_hw_get_rate(hw);
}

static ulong sunxi_clk_ccu_get_rate(struct clk *clk)
{
	const struct sunxi_ccu_desc *desc =
		(const struct sunxi_ccu_desc *)dev_get_driver_data(clk->dev);
	struct clk_hw *hw_clk = desc->hw_clks->hws[clk->id];
	ulong rate;

	if (!hw_clk)
		return -ENOENT;

	rate = clk_hw_get_rate(hw_clk);

	debug("%s(%s): hw_clk name %s rate %ld\n",
	      clk->dev->name, __func__, clk_hw_get_name(hw_clk), rate);

	return rate;
}

static int sunxi_clk_ccu_enable(struct clk *clk)
{
	const struct sunxi_ccu_desc *desc =
		(const struct sunxi_ccu_desc *)dev_get_driver_data(clk->dev);
	struct clk_hw *hw = desc->hw_clks->hws[clk->id];

	return clk_hw_enable(hw);
}

static int sunxi_clk_ccu_disable(struct clk *clk)
{
	const struct sunxi_ccu_desc *desc =
		(const struct sunxi_ccu_desc *)dev_get_driver_data(clk->dev);
	struct clk_hw *hw = desc->hw_clks->hws[clk->id];

	return clk_hw_disable(hw);
}

static int sunxi_clk_ccu_lookup(struct clk *clock, const char *name)
{
	struct udevice *dev = clock->dev;
	const struct sunxi_ccu_desc *desc =
		(const struct sunxi_ccu_desc *)dev_get_driver_data(dev);
	int i;

	for (i = 0; i < desc->hw_clks->num ; i++) {
		struct clk_hw *hw = desc->hw_clks->hws[i];

		if (!hw)
			continue;

		if (!strcmp(clk_hw_get_name(hw), name)) {
			clock->id = i;
			return 0;
		}
	}

	return -ENOENT;
}

static struct clk_ops sunxi_clk_ccu_ops = {
	.enable = sunxi_clk_ccu_enable,
	.disable = sunxi_clk_ccu_disable,
	.set_rate = sunxi_clk_ccu_set_rate,
	.get_rate = sunxi_clk_ccu_get_rate,
	.lookup = sunxi_clk_ccu_lookup,
};

static int sunxi_clk_ccu_probe(struct udevice *dev)
{
	struct sunxi_clk_priv *priv = dev_get_priv(dev);
	const struct sunxi_ccu_desc *desc =
		(const struct sunxi_ccu_desc *)dev_get_driver_data(dev);
	fdt_addr_t addr;
	fdt_size_t size;
	int i;

	debug("%s: %s\n", dev->name, __func__);

	addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev->of_offset,
						  "reg", 0, &size, false);
	if (addr == FDT_ADDR_T_NONE) {
		debug("%s: could not get addr\n", dev->name);
		return -EINVAL;
	}

	priv->base = (void *)addr;
	priv->size = size;

	/*
	 * Get any parent clocks defined, so searching their output
	 * names can work later on...
	 */
	clk_get_by_name(dev, "hosc", &priv->hosc_clk);
	clk_get_by_name(dev, "losc", &priv->losc_clk);

	priv->hosc.dev = NULL;
	priv->hosc.init = &hosc_init_data;
	priv->losc.dev = NULL;
	priv->losc.init = &losc_init_data;

	/*
	 * All clocks in the ccu_clks array of the descriptor have a
	 * ccu_common structure; propagate our base address into these.
	 */
	for (i = 0; i < desc->num_ccu_clks; i++) {
		struct ccu_common *cclk = desc->ccu_clks[i];

		if (!cclk)
			continue;

		cclk->base = (void *)addr;
	}

	/*
	 * We need to walk both lists, as they may not have the same
	 * entries (i.e. only nodes that have a ccu_common are present
	 * in the ccu_clks).
	 */
	for (i = 0; i < desc->hw_clks->num ; i++) {
		struct clk_hw *hw = desc->hw_clks->hws[i];

		if (!hw)
			continue;

		hw->dev = dev;
		if (clk_hw_get_num_parents(hw))
			hw->parents = calloc(hw->init->num_parents,
					     sizeof(struct clk_hw *));

		debug("%s: init clk %s\n", __func__, clk_hw_get_name(hw));

		for (int j = 0; j < clk_hw_get_num_parents(hw); ++j) {
			const char *parent_name = hw->init->parent_names[j];
			hw->parents[j] = ccu_parent_by_name(dev, parent_name);
		}
	}

	return 0;
}

#if defined(CONFIG_CMD_CLK)
static int sunxi_clk_ccu_dump(struct udevice *dev)
{
	const struct sunxi_ccu_desc *desc =
		(const struct sunxi_ccu_desc *)dev_get_driver_data(dev);
	int i;

	printf("%3s %20s %5s %12s %20s\n",
	       "id", "clk", "on?", "freq", "selected parent");
	for (i = 0; i < desc->hw_clks->num ; i++) {
		struct clk_hw *hw = desc->hw_clks->hws[i];
		struct clk_hw *parent;

		if (!hw)
			continue;

		parent = clk_hw_get_parent(hw);
		printf("%3d %20s %5s %12lu %20s\n", i,
		       clk_hw_get_name(hw),
		       clk_hw_is_enabled(hw) ? "YES" : "---",
		       clk_hw_get_rate(hw),
		       parent ? clk_hw_get_name(parent) : "");
	}

	return 0;
}

/**
 * soc_clk_dump() - Print clock frequencies
 * Returns zero on success
 *
 * Implementation for the clk dump command.
 */
int soc_clk_dump(void)
{
	struct udevice *dev;

	for (uclass_first_device(UCLASS_CLK, &dev);
	     dev;
	     uclass_next_device(&dev)) {
		/* if this is not this driver, skip */
		if (strcmp(dev->driver->name, "sunxi_clk_ccu") != 0)
			continue;

		sunxi_clk_ccu_dump(dev);
	}

	return 0;
}
#endif

#if defined(CONFIG_MACH_SUN50I)
extern const struct sunxi_ccu_desc sun50i_a64_ccu_desc;
#endif

static const struct udevice_id sunxi_clk_ccu_ids[] = {
#if defined(CONFIG_MACH_SUN50I)
	{ .compatible = "allwinner,sun50i-a64-ccu",
	  .data = (ulong)&sun50i_a64_ccu_desc },
#endif
	{}
};

U_BOOT_DRIVER(sunxi_clk_ccu) = {
	.name		= "sunxi_clk_ccu",
	.id		= UCLASS_CLK,
	.of_match	= sunxi_clk_ccu_ids,
	.ops		= &sunxi_clk_ccu_ops,
	.probe		= sunxi_clk_ccu_probe,
	.priv_auto_alloc_size = sizeof(struct sunxi_clk_priv),
};