summaryrefslogtreecommitdiffstats
path: root/drivers/clk/clk_vexpress_osc.c
blob: 82e589e239fe1aba952283753d5856ab118cd70f (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2018 Arm Ltd
 * Author: Liviu Dudau <liviu.dudau@foss.arm.com>
 *
 */
#define DEBUG
#include <common.h>
#include <clk-uclass.h>
#include <dm.h>
#include <dm/device_compat.h>
#include <dm/lists.h>
#include <errno.h>
#include <misc.h>

#define CLK_FUNCTION		BIT(20)

struct vexpress_osc_clk_priv {
	u8 osc;
	ulong rate_min;
	ulong rate_max;
};

static ulong vexpress_osc_clk_get_rate(struct clk *clk)
{
	int err;
	u32 data;
	struct udevice *vexpress_cfg = dev_get_parent(clk->dev);
	struct vexpress_osc_clk_priv *priv = dev_get_priv(clk->dev);

	data = CLK_FUNCTION | priv->osc;
	err = misc_read(vexpress_cfg, 0, &data, sizeof(data));
	if (err < 0)
		return err;

	return data;
}

#ifndef CONFIG_SPL_BUILD
static ulong vexpress_osc_clk_set_rate(struct clk *clk, ulong rate)
{
	int err;
	u32 buffer[2];
	struct udevice *vexpress_cfg = dev_get_parent(clk->dev);
	struct vexpress_osc_clk_priv *priv = dev_get_priv(clk->dev);

	if (rate < priv->rate_min || rate > priv->rate_max)
		return -EINVAL;

	/*
	 * we are sending the parent the info about the oscillator
	 * and the value we want to set
	 */
	buffer[0] = CLK_FUNCTION | priv->osc;
	buffer[1] = rate;
	err = misc_write(vexpress_cfg, 0, buffer, 2 * sizeof(u32));
	if (err < 0)
		return err;

	return rate;
}
#endif

static struct clk_ops vexpress_osc_clk_ops = {
	.get_rate = vexpress_osc_clk_get_rate,
#ifndef CONFIG_SPL_BUILD
	.set_rate = vexpress_osc_clk_set_rate,
#endif
};

static int vexpress_osc_clk_probe(struct udevice *dev)
{
	struct vexpress_osc_clk_priv *priv = dev_get_priv(dev);
	u32 values[2];
	int err;

	err = dev_read_u32_array(dev, "freq-range", values, 2);
	if (err)
		return err;
	priv->rate_min = values[0];
	priv->rate_max = values[1];

	err = dev_read_u32_array(dev, "arm,vexpress-sysreg,func", values, 2);
	if (err)
		return err;

	if (values[0] != 1) {
		dev_err(dev, "Invalid VExpress function for clock, must be '1'");
		return -EINVAL;
	}
	priv->osc = values[1];
	debug("clk \"%s%d\", min freq %luHz, max freq %luHz\n", dev->name,
	      priv->osc, priv->rate_min, priv->rate_max);

	return 0;
}

static const struct udevice_id vexpress_osc_clk_ids[] = {
	{ .compatible = "arm,vexpress-osc", },
	{}
};

U_BOOT_DRIVER(vexpress_osc_clk) = {
	.name = "vexpress_osc_clk",
	.id = UCLASS_CLK,
	.of_match = vexpress_osc_clk_ids,
	.ops = &vexpress_osc_clk_ops,
	.priv_auto_alloc_size = sizeof(struct vexpress_osc_clk_priv),
	.probe = vexpress_osc_clk_probe,
};