blob: c31e59641d9344bd5f6e870bfd0c2c3ed081e657 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadae4dfb052016-02-02 21:11:32 +09002/*
Masahiro Yamada49806682017-10-13 19:21:59 +09003 * Copyright (C) 2016-2017 Socionext Inc.
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +09004 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadae4dfb052016-02-02 21:11:32 +09005 */
6
7#include <common.h>
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +09008#include <clk-uclass.h>
Simon Glass11c89f32017-05-17 17:18:03 -06009#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <dm/device_compat.h>
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090011#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060012#include <linux/bug.h>
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090013#include <linux/io.h>
Masahiro Yamada46a624f2016-03-24 22:32:39 +090014#include <linux/sizes.h>
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090015
16#include "clk-uniphier.h"
17
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090018/**
19 * struct uniphier_clk_priv - private data for UniPhier clock driver
20 *
21 * @base: base address of the clock provider
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090022 * @data: SoC specific data
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090023 */
24struct uniphier_clk_priv {
Masahiro Yamada49806682017-10-13 19:21:59 +090025 struct udevice *dev;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090026 void __iomem *base;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090027 const struct uniphier_clk_data *data;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090028};
29
Masahiro Yamada49806682017-10-13 19:21:59 +090030static void uniphier_clk_gate_enable(struct uniphier_clk_priv *priv,
31 const struct uniphier_clk_gate_data *gate)
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090032{
Masahiro Yamada49806682017-10-13 19:21:59 +090033 u32 val;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090034
Masahiro Yamada49806682017-10-13 19:21:59 +090035 val = readl(priv->base + gate->reg);
36 val |= BIT(gate->bit);
37 writel(val, priv->base + gate->reg);
38}
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090039
Masahiro Yamada49806682017-10-13 19:21:59 +090040static void uniphier_clk_mux_set_parent(struct uniphier_clk_priv *priv,
41 const struct uniphier_clk_mux_data *mux,
42 u8 id)
43{
44 u32 val;
45 int i;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090046
Masahiro Yamada49806682017-10-13 19:21:59 +090047 for (i = 0; i < mux->num_parents; i++) {
48 if (mux->parent_ids[i] != id)
49 continue;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090050
Masahiro Yamada49806682017-10-13 19:21:59 +090051 val = readl(priv->base + mux->reg);
52 val &= ~mux->masks[i];
53 val |= mux->vals[i];
54 writel(val, priv->base + mux->reg);
55 return;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090056 }
57
Masahiro Yamada49806682017-10-13 19:21:59 +090058 WARN_ON(1);
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090059}
60
Masahiro Yamada49806682017-10-13 19:21:59 +090061static u8 uniphier_clk_mux_get_parent(struct uniphier_clk_priv *priv,
62 const struct uniphier_clk_mux_data *mux)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090063{
Masahiro Yamada49806682017-10-13 19:21:59 +090064 u32 val;
65 int i;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090066
Masahiro Yamada49806682017-10-13 19:21:59 +090067 val = readl(priv->base + mux->reg);
68
69 for (i = 0; i < mux->num_parents; i++)
70 if ((mux->masks[i] & val) == mux->vals[i])
71 return mux->parent_ids[i];
72
73 dev_err(priv->dev, "invalid mux setting\n");
74
75 return UNIPHIER_CLK_ID_INVALID;
76}
77
78static const struct uniphier_clk_data *uniphier_clk_get_data(
79 struct uniphier_clk_priv *priv, u8 id)
80{
81 const struct uniphier_clk_data *data;
82
83 for (data = priv->data; data->type != UNIPHIER_CLK_TYPE_END; data++)
84 if (data->id == id)
85 return data;
86
87 dev_err(priv->dev, "id=%u not found\n", id);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090088
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090089 return NULL;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090090}
91
Masahiro Yamada49806682017-10-13 19:21:59 +090092static const struct uniphier_clk_data *uniphier_clk_get_parent_data(
93 struct uniphier_clk_priv *priv,
94 const struct uniphier_clk_data *data)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090095{
Masahiro Yamada49806682017-10-13 19:21:59 +090096 const struct uniphier_clk_data *parent_data;
97 u8 parent_id = UNIPHIER_CLK_ID_INVALID;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090098
Masahiro Yamada49806682017-10-13 19:21:59 +090099 switch (data->type) {
100 case UNIPHIER_CLK_TYPE_GATE:
101 parent_id = data->data.gate.parent_id;
102 break;
103 case UNIPHIER_CLK_TYPE_MUX:
104 parent_id = uniphier_clk_mux_get_parent(priv, &data->data.mux);
105 break;
106 default:
107 break;
108 }
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900109
Masahiro Yamada49806682017-10-13 19:21:59 +0900110 if (parent_id == UNIPHIER_CLK_ID_INVALID)
111 return NULL;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900112
Masahiro Yamada49806682017-10-13 19:21:59 +0900113 parent_data = uniphier_clk_get_data(priv, parent_id);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900114
Masahiro Yamada49806682017-10-13 19:21:59 +0900115 WARN_ON(!parent_data);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900116
Masahiro Yamada49806682017-10-13 19:21:59 +0900117 return parent_data;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900118}
119
Masahiro Yamada49806682017-10-13 19:21:59 +0900120static void __uniphier_clk_enable(struct uniphier_clk_priv *priv,
121 const struct uniphier_clk_data *data)
122{
123 const struct uniphier_clk_data *parent_data;
124
125 if (data->type == UNIPHIER_CLK_TYPE_GATE)
126 uniphier_clk_gate_enable(priv, &data->data.gate);
127
128 parent_data = uniphier_clk_get_parent_data(priv, data);
129 if (!parent_data)
130 return;
131
132 return __uniphier_clk_enable(priv, parent_data);
133}
134
135static int uniphier_clk_enable(struct clk *clk)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900136{
Stephen Warrena9622432016-06-17 09:44:00 -0600137 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
Masahiro Yamada49806682017-10-13 19:21:59 +0900138 const struct uniphier_clk_data *data;
139
140 data = uniphier_clk_get_data(priv, clk->id);
141 if (!data)
142 return -ENODEV;
143
144 __uniphier_clk_enable(priv, data);
145
146 return 0;
147}
148
149static unsigned long __uniphier_clk_get_rate(
150 struct uniphier_clk_priv *priv,
151 const struct uniphier_clk_data *data)
152{
153 const struct uniphier_clk_data *parent_data;
154
155 if (data->type == UNIPHIER_CLK_TYPE_FIXED_RATE)
156 return data->data.rate.fixed_rate;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900157
Masahiro Yamada49806682017-10-13 19:21:59 +0900158 parent_data = uniphier_clk_get_parent_data(priv, data);
159 if (!parent_data)
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900160 return 0;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900161
Masahiro Yamada49806682017-10-13 19:21:59 +0900162 return __uniphier_clk_get_rate(priv, parent_data);
163}
164
165static unsigned long uniphier_clk_get_rate(struct clk *clk)
166{
167 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
168 const struct uniphier_clk_data *data;
169
170 data = uniphier_clk_get_data(priv, clk->id);
171 if (!data)
172 return -ENODEV;
173
174 return __uniphier_clk_get_rate(priv, data);
175}
176
177static unsigned long __uniphier_clk_set_rate(
178 struct uniphier_clk_priv *priv,
179 const struct uniphier_clk_data *data,
180 unsigned long rate, bool set)
181{
182 const struct uniphier_clk_data *best_parent_data = NULL;
183 const struct uniphier_clk_data *parent_data;
184 unsigned long best_rate = 0;
185 unsigned long parent_rate;
186 u8 parent_id;
187 int i;
188
189 if (data->type == UNIPHIER_CLK_TYPE_FIXED_RATE)
190 return data->data.rate.fixed_rate;
191
192 if (data->type == UNIPHIER_CLK_TYPE_GATE) {
193 parent_data = uniphier_clk_get_parent_data(priv, data);
194 if (!parent_data)
195 return 0;
196
197 return __uniphier_clk_set_rate(priv, parent_data, rate, set);
198 }
199
200 if (WARN_ON(data->type != UNIPHIER_CLK_TYPE_MUX))
201 return -EINVAL;
202
203 for (i = 0; i < data->data.mux.num_parents; i++) {
204 parent_id = data->data.mux.parent_ids[i];
205 parent_data = uniphier_clk_get_data(priv, parent_id);
206 if (WARN_ON(!parent_data))
207 return -EINVAL;
208
209 parent_rate = __uniphier_clk_set_rate(priv, parent_data, rate,
210 false);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900211
Masahiro Yamada49806682017-10-13 19:21:59 +0900212 if (parent_rate <= rate && best_rate < parent_rate) {
213 best_rate = parent_rate;
214 best_parent_data = parent_data;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900215 }
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900216 }
217
Masahiro Yamada49806682017-10-13 19:21:59 +0900218 dev_dbg(priv->dev, "id=%u, best_rate=%lu\n", data->id, best_rate);
219
220 if (!best_parent_data)
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900221 return -EINVAL;
222
Masahiro Yamada49806682017-10-13 19:21:59 +0900223 if (!set)
224 return best_rate;
225
226 uniphier_clk_mux_set_parent(priv, &data->data.mux,
227 best_parent_data->id);
228
229 return best_rate = __uniphier_clk_set_rate(priv, best_parent_data,
230 rate, true);
231}
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900232
Masahiro Yamada49806682017-10-13 19:21:59 +0900233static unsigned long uniphier_clk_set_rate(struct clk *clk, ulong rate)
234{
235 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
236 const struct uniphier_clk_data *data;
237
238 data = uniphier_clk_get_data(priv, clk->id);
239 if (!data)
240 return -ENODEV;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900241
Masahiro Yamada49806682017-10-13 19:21:59 +0900242 return __uniphier_clk_set_rate(priv, data, rate, true);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900243}
244
Masahiro Yamada5e05c4d2017-06-22 16:42:04 +0900245static const struct clk_ops uniphier_clk_ops = {
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900246 .enable = uniphier_clk_enable,
Stephen Warrena9622432016-06-17 09:44:00 -0600247 .get_rate = uniphier_clk_get_rate,
248 .set_rate = uniphier_clk_set_rate,
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900249};
250
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900251static int uniphier_clk_probe(struct udevice *dev)
252{
253 struct uniphier_clk_priv *priv = dev_get_priv(dev);
254 fdt_addr_t addr;
255
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900256 addr = dev_read_addr(dev->parent);
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900257 if (addr == FDT_ADDR_T_NONE)
258 return -EINVAL;
259
260 priv->base = devm_ioremap(dev, addr, SZ_4K);
261 if (!priv->base)
262 return -ENOMEM;
263
Masahiro Yamada49806682017-10-13 19:21:59 +0900264 priv->dev = dev;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900265 priv->data = (void *)dev_get_driver_data(dev);
266
267 return 0;
268}
269
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900270static const struct udevice_id uniphier_clk_match[] = {
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900271 /* System clock */
272 {
273 .compatible = "socionext,uniphier-ld4-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900274 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900275 },
276 {
277 .compatible = "socionext,uniphier-pro4-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900278 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900279 },
280 {
281 .compatible = "socionext,uniphier-sld8-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900282 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900283 },
284 {
285 .compatible = "socionext,uniphier-pro5-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900286 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900287 },
288 {
289 .compatible = "socionext,uniphier-pxs2-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900290 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900291 },
292 {
293 .compatible = "socionext,uniphier-ld11-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900294 .data = (ulong)uniphier_ld20_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900295 },
296 {
297 .compatible = "socionext,uniphier-ld20-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900298 .data = (ulong)uniphier_ld20_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900299 },
Masahiro Yamada52cdec02017-10-13 19:22:00 +0900300 {
301 .compatible = "socionext,uniphier-pxs3-clock",
302 .data = (ulong)uniphier_pxs3_sys_clk_data,
303 },
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900304 /* Media I/O clock */
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900305 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900306 .compatible = "socionext,uniphier-ld4-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900307 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900308 },
309 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900310 .compatible = "socionext,uniphier-pro4-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900311 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900312 },
313 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900314 .compatible = "socionext,uniphier-sld8-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900315 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900316 },
317 {
Masahiro Yamada9e34de52017-01-28 06:53:41 +0900318 .compatible = "socionext,uniphier-pro5-sd-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900319 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900320 },
321 {
Masahiro Yamada9e34de52017-01-28 06:53:41 +0900322 .compatible = "socionext,uniphier-pxs2-sd-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900323 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900324 },
325 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900326 .compatible = "socionext,uniphier-ld11-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900327 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900328 },
329 {
Masahiro Yamada9e34de52017-01-28 06:53:41 +0900330 .compatible = "socionext,uniphier-ld20-sd-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900331 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900332 },
Masahiro Yamada52cdec02017-10-13 19:22:00 +0900333 {
334 .compatible = "socionext,uniphier-pxs3-sd-clock",
335 .data = (ulong)uniphier_mio_clk_data,
336 },
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900337 { /* sentinel */ }
338};
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900339
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900340U_BOOT_DRIVER(uniphier_clk) = {
341 .name = "uniphier-clk",
342 .id = UCLASS_CLK,
343 .of_match = uniphier_clk_match,
344 .probe = uniphier_clk_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700345 .priv_auto = sizeof(struct uniphier_clk_priv),
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900346 .ops = &uniphier_clk_ops,
347};