blob: 8f59d7fb72c9ec1f5c5493381c711a4c18d4bd8a [file] [log] [blame]
Lukasz Majewski1d7993d2019-06-24 15:50:45 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2019 DENX Software Engineering
4 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5 *
6 * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
7 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
8 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
9 *
10 */
11
12#include <common.h>
13#include <asm/io.h>
14#include <malloc.h>
15#include <clk-uclass.h>
16#include <dm/device.h>
Simon Glass61b29b82020-02-03 07:36:15 -070017#include <dm/devres.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020018#include <dm/uclass.h>
19#include <dm/lists.h>
20#include <dm/device-internal.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060021#include <linux/bug.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020022#include <linux/clk-provider.h>
Simon Glass61b29b82020-02-03 07:36:15 -070023#include <linux/err.h>
Peng Fanfe69b032019-07-31 07:01:37 +000024#include <linux/log2.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020025#include <div64.h>
26#include <clk.h>
27#include "clk.h"
28
29#define UBOOT_DM_CLK_CCF_DIVIDER "ccf_clk_divider"
30
31static unsigned int _get_table_div(const struct clk_div_table *table,
32 unsigned int val)
33{
34 const struct clk_div_table *clkt;
35
36 for (clkt = table; clkt->div; clkt++)
37 if (clkt->val == val)
38 return clkt->div;
39 return 0;
40}
41
42static unsigned int _get_div(const struct clk_div_table *table,
43 unsigned int val, unsigned long flags, u8 width)
44{
45 if (flags & CLK_DIVIDER_ONE_BASED)
46 return val;
47 if (flags & CLK_DIVIDER_POWER_OF_TWO)
48 return 1 << val;
49 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
50 return val ? val : clk_div_mask(width) + 1;
51 if (table)
52 return _get_table_div(table, val);
53 return val + 1;
54}
55
56unsigned long divider_recalc_rate(struct clk *hw, unsigned long parent_rate,
57 unsigned int val,
58 const struct clk_div_table *table,
59 unsigned long flags, unsigned long width)
60{
61 unsigned int div;
62
63 div = _get_div(table, val, flags, width);
64 if (!div) {
65 WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
66 "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
67 clk_hw_get_name(hw));
68 return parent_rate;
69 }
70
71 return DIV_ROUND_UP_ULL((u64)parent_rate, div);
72}
73
74static ulong clk_divider_recalc_rate(struct clk *clk)
75{
Sean Anderson78ce0bd2020-06-24 06:41:06 -040076 struct clk_divider *divider = to_clk_divider(clk);
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020077 unsigned long parent_rate = clk_get_parent_rate(clk);
78 unsigned int val;
79
Lukasz Majewski6bb15d62019-06-24 15:50:48 +020080#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
81 val = divider->io_divider_val;
82#else
83 val = readl(divider->reg);
84#endif
85 val >>= divider->shift;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020086 val &= clk_div_mask(divider->width);
87
88 return divider_recalc_rate(clk, parent_rate, val, divider->table,
89 divider->flags, divider->width);
90}
91
Peng Fanfe69b032019-07-31 07:01:37 +000092static bool _is_valid_table_div(const struct clk_div_table *table,
93 unsigned int div)
94{
95 const struct clk_div_table *clkt;
96
97 for (clkt = table; clkt->div; clkt++)
98 if (clkt->div == div)
99 return true;
100 return false;
101}
102
103static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
104 unsigned long flags)
105{
106 if (flags & CLK_DIVIDER_POWER_OF_TWO)
107 return is_power_of_2(div);
108 if (table)
109 return _is_valid_table_div(table, div);
110 return true;
111}
112
113static unsigned int _get_table_val(const struct clk_div_table *table,
114 unsigned int div)
115{
116 const struct clk_div_table *clkt;
117
118 for (clkt = table; clkt->div; clkt++)
119 if (clkt->div == div)
120 return clkt->val;
121 return 0;
122}
123
124static unsigned int _get_val(const struct clk_div_table *table,
125 unsigned int div, unsigned long flags, u8 width)
126{
127 if (flags & CLK_DIVIDER_ONE_BASED)
128 return div;
129 if (flags & CLK_DIVIDER_POWER_OF_TWO)
130 return __ffs(div);
131 if (flags & CLK_DIVIDER_MAX_AT_ZERO)
132 return (div == clk_div_mask(width) + 1) ? 0 : div;
133 if (table)
134 return _get_table_val(table, div);
135 return div - 1;
136}
137int divider_get_val(unsigned long rate, unsigned long parent_rate,
138 const struct clk_div_table *table, u8 width,
139 unsigned long flags)
140{
141 unsigned int div, value;
142
143 div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
144
145 if (!_is_valid_div(table, div, flags))
146 return -EINVAL;
147
148 value = _get_val(table, div, flags, width);
149
150 return min_t(unsigned int, value, clk_div_mask(width));
151}
152
153static ulong clk_divider_set_rate(struct clk *clk, unsigned long rate)
154{
Sean Anderson78ce0bd2020-06-24 06:41:06 -0400155 struct clk_divider *divider = to_clk_divider(clk);
Peng Fanfe69b032019-07-31 07:01:37 +0000156 unsigned long parent_rate = clk_get_parent_rate(clk);
157 int value;
158 u32 val;
159
160 value = divider_get_val(rate, parent_rate, divider->table,
161 divider->width, divider->flags);
162 if (value < 0)
163 return value;
164
165 if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
166 val = clk_div_mask(divider->width) << (divider->shift + 16);
167 } else {
168 val = readl(divider->reg);
169 val &= ~(clk_div_mask(divider->width) << divider->shift);
170 }
171 val |= (u32)value << divider->shift;
172 writel(val, divider->reg);
173
174 return clk_get_rate(clk);
175}
176
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200177const struct clk_ops clk_divider_ops = {
178 .get_rate = clk_divider_recalc_rate,
Peng Fanfe69b032019-07-31 07:01:37 +0000179 .set_rate = clk_divider_set_rate,
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200180};
181
182static struct clk *_register_divider(struct device *dev, const char *name,
183 const char *parent_name, unsigned long flags,
184 void __iomem *reg, u8 shift, u8 width,
185 u8 clk_divider_flags, const struct clk_div_table *table)
186{
187 struct clk_divider *div;
188 struct clk *clk;
189 int ret;
190
191 if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
192 if (width + shift > 16) {
193 pr_warn("divider value exceeds LOWORD field\n");
194 return ERR_PTR(-EINVAL);
195 }
196 }
197
198 /* allocate the divider */
199 div = kzalloc(sizeof(*div), GFP_KERNEL);
200 if (!div)
201 return ERR_PTR(-ENOMEM);
202
203 /* struct clk_divider assignments */
204 div->reg = reg;
205 div->shift = shift;
206 div->width = width;
207 div->flags = clk_divider_flags;
208 div->table = table;
Lukasz Majewski6bb15d62019-06-24 15:50:48 +0200209#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
210 div->io_divider_val = *(u32 *)reg;
211#endif
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200212
213 /* register the clock */
214 clk = &div->clk;
Dario Binacchi16bdc852020-04-13 14:36:27 +0200215 clk->flags = flags;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200216
217 ret = clk_register(clk, UBOOT_DM_CLK_CCF_DIVIDER, name, parent_name);
218 if (ret) {
219 kfree(div);
220 return ERR_PTR(ret);
221 }
222
223 return clk;
224}
225
226struct clk *clk_register_divider(struct device *dev, const char *name,
227 const char *parent_name, unsigned long flags,
228 void __iomem *reg, u8 shift, u8 width,
229 u8 clk_divider_flags)
230{
231 struct clk *clk;
232
233 clk = _register_divider(dev, name, parent_name, flags, reg, shift,
234 width, clk_divider_flags, NULL);
235 if (IS_ERR(clk))
236 return ERR_CAST(clk);
237 return clk;
238}
239
240U_BOOT_DRIVER(ccf_clk_divider) = {
241 .name = UBOOT_DM_CLK_CCF_DIVIDER,
242 .id = UCLASS_CLK,
243 .ops = &clk_divider_ops,
244 .flags = DM_FLAG_PRE_RELOC,
245};