1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Arm Ltd
4  * Author: Liviu Dudau <liviu.dudau@foss.arm.com>
5  *
6  */
7 #define DEBUG
8 #include <common.h>
9 #include <clk-uclass.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <dm/device_compat.h>
13 #include <dm/lists.h>
14 #include <errno.h>
15 #include <misc.h>
16 #include <linux/bitops.h>
17 
18 #define CLK_FUNCTION		BIT(20)
19 
20 struct vexpress_osc_clk_priv {
21 	u8 osc;
22 	ulong rate_min;
23 	ulong rate_max;
24 };
25 
vexpress_osc_clk_get_rate(struct clk * clk)26 static ulong vexpress_osc_clk_get_rate(struct clk *clk)
27 {
28 	int err;
29 	u32 data;
30 	struct udevice *vexpress_cfg = dev_get_parent(clk->dev);
31 	struct vexpress_osc_clk_priv *priv = dev_get_priv(clk->dev);
32 
33 	data = CLK_FUNCTION | priv->osc;
34 	err = misc_read(vexpress_cfg, 0, &data, sizeof(data));
35 	if (err < 0)
36 		return err;
37 
38 	return data;
39 }
40 
41 #ifndef CONFIG_SPL_BUILD
vexpress_osc_clk_set_rate(struct clk * clk,ulong rate)42 static ulong vexpress_osc_clk_set_rate(struct clk *clk, ulong rate)
43 {
44 	int err;
45 	u32 buffer[2];
46 	struct udevice *vexpress_cfg = dev_get_parent(clk->dev);
47 	struct vexpress_osc_clk_priv *priv = dev_get_priv(clk->dev);
48 
49 	if (rate < priv->rate_min || rate > priv->rate_max)
50 		return -EINVAL;
51 
52 	/*
53 	 * we are sending the parent the info about the oscillator
54 	 * and the value we want to set
55 	 */
56 	buffer[0] = CLK_FUNCTION | priv->osc;
57 	buffer[1] = rate;
58 	err = misc_write(vexpress_cfg, 0, buffer, 2 * sizeof(u32));
59 	if (err < 0)
60 		return err;
61 
62 	return rate;
63 }
64 #endif
65 
66 static struct clk_ops vexpress_osc_clk_ops = {
67 	.get_rate = vexpress_osc_clk_get_rate,
68 #ifndef CONFIG_SPL_BUILD
69 	.set_rate = vexpress_osc_clk_set_rate,
70 #endif
71 };
72 
vexpress_osc_clk_probe(struct udevice * dev)73 static int vexpress_osc_clk_probe(struct udevice *dev)
74 {
75 	struct vexpress_osc_clk_priv *priv = dev_get_priv(dev);
76 	u32 values[2];
77 	int err;
78 
79 	err = dev_read_u32_array(dev, "freq-range", values, 2);
80 	if (err)
81 		return err;
82 	priv->rate_min = values[0];
83 	priv->rate_max = values[1];
84 
85 	err = dev_read_u32_array(dev, "arm,vexpress-sysreg,func", values, 2);
86 	if (err)
87 		return err;
88 
89 	if (values[0] != 1) {
90 		dev_err(dev, "Invalid VExpress function for clock, must be '1'");
91 		return -EINVAL;
92 	}
93 	priv->osc = values[1];
94 	debug("clk \"%s%d\", min freq %luHz, max freq %luHz\n", dev->name,
95 	      priv->osc, priv->rate_min, priv->rate_max);
96 
97 	return 0;
98 }
99 
100 static const struct udevice_id vexpress_osc_clk_ids[] = {
101 	{ .compatible = "arm,vexpress-osc", },
102 	{}
103 };
104 
105 U_BOOT_DRIVER(vexpress_osc_clk) = {
106 	.name = "vexpress_osc_clk",
107 	.id = UCLASS_CLK,
108 	.of_match = vexpress_osc_clk_ids,
109 	.ops = &vexpress_osc_clk_ops,
110 	.priv_auto	= sizeof(struct vexpress_osc_clk_priv),
111 	.probe = vexpress_osc_clk_probe,
112 };
113