Merge tag 'efi-2020-07-rc2-2' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / drivers / clk / clk_vexpress_osc.c
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 <dm/device_compat.h>
12 #include <dm/lists.h>
13 #include <errno.h>
14 #include <misc.h>
15
16 #define CLK_FUNCTION            BIT(20)
17
18 struct vexpress_osc_clk_priv {
19         u8 osc;
20         ulong rate_min;
21         ulong rate_max;
22 };
23
24 static ulong vexpress_osc_clk_get_rate(struct clk *clk)
25 {
26         int err;
27         u32 data;
28         struct udevice *vexpress_cfg = dev_get_parent(clk->dev);
29         struct vexpress_osc_clk_priv *priv = dev_get_priv(clk->dev);
30
31         data = CLK_FUNCTION | priv->osc;
32         err = misc_read(vexpress_cfg, 0, &data, sizeof(data));
33         if (err < 0)
34                 return err;
35
36         return data;
37 }
38
39 #ifndef CONFIG_SPL_BUILD
40 static ulong vexpress_osc_clk_set_rate(struct clk *clk, ulong rate)
41 {
42         int err;
43         u32 buffer[2];
44         struct udevice *vexpress_cfg = dev_get_parent(clk->dev);
45         struct vexpress_osc_clk_priv *priv = dev_get_priv(clk->dev);
46
47         if (rate < priv->rate_min || rate > priv->rate_max)
48                 return -EINVAL;
49
50         /*
51          * we are sending the parent the info about the oscillator
52          * and the value we want to set
53          */
54         buffer[0] = CLK_FUNCTION | priv->osc;
55         buffer[1] = rate;
56         err = misc_write(vexpress_cfg, 0, buffer, 2 * sizeof(u32));
57         if (err < 0)
58                 return err;
59
60         return rate;
61 }
62 #endif
63
64 static struct clk_ops vexpress_osc_clk_ops = {
65         .get_rate = vexpress_osc_clk_get_rate,
66 #ifndef CONFIG_SPL_BUILD
67         .set_rate = vexpress_osc_clk_set_rate,
68 #endif
69 };
70
71 static int vexpress_osc_clk_probe(struct udevice *dev)
72 {
73         struct vexpress_osc_clk_priv *priv = dev_get_priv(dev);
74         u32 values[2];
75         int err;
76
77         err = dev_read_u32_array(dev, "freq-range", values, 2);
78         if (err)
79                 return err;
80         priv->rate_min = values[0];
81         priv->rate_max = values[1];
82
83         err = dev_read_u32_array(dev, "arm,vexpress-sysreg,func", values, 2);
84         if (err)
85                 return err;
86
87         if (values[0] != 1) {
88                 dev_err(dev, "Invalid VExpress function for clock, must be '1'");
89                 return -EINVAL;
90         }
91         priv->osc = values[1];
92         debug("clk \"%s%d\", min freq %luHz, max freq %luHz\n", dev->name,
93               priv->osc, priv->rate_min, priv->rate_max);
94
95         return 0;
96 }
97
98 static const struct udevice_id vexpress_osc_clk_ids[] = {
99         { .compatible = "arm,vexpress-osc", },
100         {}
101 };
102
103 U_BOOT_DRIVER(vexpress_osc_clk) = {
104         .name = "vexpress_osc_clk",
105         .id = UCLASS_CLK,
106         .of_match = vexpress_osc_clk_ids,
107         .ops = &vexpress_osc_clk_ops,
108         .priv_auto_alloc_size = sizeof(struct vexpress_osc_clk_priv),
109         .probe = vexpress_osc_clk_probe,
110 };