blob: 9f24050992566728309a7128dad1032454bd680a [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>
12#include <linux/io.h>
Masahiro Yamada46a624f2016-03-24 22:32:39 +090013#include <linux/sizes.h>
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090014
15#include "clk-uniphier.h"
16
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090017/**
18 * struct uniphier_clk_priv - private data for UniPhier clock driver
19 *
20 * @base: base address of the clock provider
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090021 * @data: SoC specific data
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090022 */
23struct uniphier_clk_priv {
Masahiro Yamada49806682017-10-13 19:21:59 +090024 struct udevice *dev;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090025 void __iomem *base;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090026 const struct uniphier_clk_data *data;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090027};
28
Masahiro Yamada49806682017-10-13 19:21:59 +090029static void uniphier_clk_gate_enable(struct uniphier_clk_priv *priv,
30 const struct uniphier_clk_gate_data *gate)
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090031{
Masahiro Yamada49806682017-10-13 19:21:59 +090032 u32 val;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090033
Masahiro Yamada49806682017-10-13 19:21:59 +090034 val = readl(priv->base + gate->reg);
35 val |= BIT(gate->bit);
36 writel(val, priv->base + gate->reg);
37}
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090038
Masahiro Yamada49806682017-10-13 19:21:59 +090039static void uniphier_clk_mux_set_parent(struct uniphier_clk_priv *priv,
40 const struct uniphier_clk_mux_data *mux,
41 u8 id)
42{
43 u32 val;
44 int i;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090045
Masahiro Yamada49806682017-10-13 19:21:59 +090046 for (i = 0; i < mux->num_parents; i++) {
47 if (mux->parent_ids[i] != id)
48 continue;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090049
Masahiro Yamada49806682017-10-13 19:21:59 +090050 val = readl(priv->base + mux->reg);
51 val &= ~mux->masks[i];
52 val |= mux->vals[i];
53 writel(val, priv->base + mux->reg);
54 return;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090055 }
56
Masahiro Yamada49806682017-10-13 19:21:59 +090057 WARN_ON(1);
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090058}
59
Masahiro Yamada49806682017-10-13 19:21:59 +090060static u8 uniphier_clk_mux_get_parent(struct uniphier_clk_priv *priv,
61 const struct uniphier_clk_mux_data *mux)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090062{
Masahiro Yamada49806682017-10-13 19:21:59 +090063 u32 val;
64 int i;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090065
Masahiro Yamada49806682017-10-13 19:21:59 +090066 val = readl(priv->base + mux->reg);
67
68 for (i = 0; i < mux->num_parents; i++)
69 if ((mux->masks[i] & val) == mux->vals[i])
70 return mux->parent_ids[i];
71
72 dev_err(priv->dev, "invalid mux setting\n");
73
74 return UNIPHIER_CLK_ID_INVALID;
75}
76
77static const struct uniphier_clk_data *uniphier_clk_get_data(
78 struct uniphier_clk_priv *priv, u8 id)
79{
80 const struct uniphier_clk_data *data;
81
82 for (data = priv->data; data->type != UNIPHIER_CLK_TYPE_END; data++)
83 if (data->id == id)
84 return data;
85
86 dev_err(priv->dev, "id=%u not found\n", id);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090087
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090088 return NULL;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090089}
90
Masahiro Yamada49806682017-10-13 19:21:59 +090091static const struct uniphier_clk_data *uniphier_clk_get_parent_data(
92 struct uniphier_clk_priv *priv,
93 const struct uniphier_clk_data *data)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090094{
Masahiro Yamada49806682017-10-13 19:21:59 +090095 const struct uniphier_clk_data *parent_data;
96 u8 parent_id = UNIPHIER_CLK_ID_INVALID;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090097
Masahiro Yamada49806682017-10-13 19:21:59 +090098 switch (data->type) {
99 case UNIPHIER_CLK_TYPE_GATE:
100 parent_id = data->data.gate.parent_id;
101 break;
102 case UNIPHIER_CLK_TYPE_MUX:
103 parent_id = uniphier_clk_mux_get_parent(priv, &data->data.mux);
104 break;
105 default:
106 break;
107 }
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900108
Masahiro Yamada49806682017-10-13 19:21:59 +0900109 if (parent_id == UNIPHIER_CLK_ID_INVALID)
110 return NULL;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900111
Masahiro Yamada49806682017-10-13 19:21:59 +0900112 parent_data = uniphier_clk_get_data(priv, parent_id);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900113
Masahiro Yamada49806682017-10-13 19:21:59 +0900114 WARN_ON(!parent_data);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900115
Masahiro Yamada49806682017-10-13 19:21:59 +0900116 return parent_data;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900117}
118
Masahiro Yamada49806682017-10-13 19:21:59 +0900119static void __uniphier_clk_enable(struct uniphier_clk_priv *priv,
120 const struct uniphier_clk_data *data)
121{
122 const struct uniphier_clk_data *parent_data;
123
124 if (data->type == UNIPHIER_CLK_TYPE_GATE)
125 uniphier_clk_gate_enable(priv, &data->data.gate);
126
127 parent_data = uniphier_clk_get_parent_data(priv, data);
128 if (!parent_data)
129 return;
130
131 return __uniphier_clk_enable(priv, parent_data);
132}
133
134static int uniphier_clk_enable(struct clk *clk)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900135{
Stephen Warrena9622432016-06-17 09:44:00 -0600136 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
Masahiro Yamada49806682017-10-13 19:21:59 +0900137 const struct uniphier_clk_data *data;
138
139 data = uniphier_clk_get_data(priv, clk->id);
140 if (!data)
141 return -ENODEV;
142
143 __uniphier_clk_enable(priv, data);
144
145 return 0;
146}
147
148static unsigned long __uniphier_clk_get_rate(
149 struct uniphier_clk_priv *priv,
150 const struct uniphier_clk_data *data)
151{
152 const struct uniphier_clk_data *parent_data;
153
154 if (data->type == UNIPHIER_CLK_TYPE_FIXED_RATE)
155 return data->data.rate.fixed_rate;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900156
Masahiro Yamada49806682017-10-13 19:21:59 +0900157 parent_data = uniphier_clk_get_parent_data(priv, data);
158 if (!parent_data)
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900159 return 0;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900160
Masahiro Yamada49806682017-10-13 19:21:59 +0900161 return __uniphier_clk_get_rate(priv, parent_data);
162}
163
164static unsigned long uniphier_clk_get_rate(struct clk *clk)
165{
166 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
167 const struct uniphier_clk_data *data;
168
169 data = uniphier_clk_get_data(priv, clk->id);
170 if (!data)
171 return -ENODEV;
172
173 return __uniphier_clk_get_rate(priv, data);
174}
175
176static unsigned long __uniphier_clk_set_rate(
177 struct uniphier_clk_priv *priv,
178 const struct uniphier_clk_data *data,
179 unsigned long rate, bool set)
180{
181 const struct uniphier_clk_data *best_parent_data = NULL;
182 const struct uniphier_clk_data *parent_data;
183 unsigned long best_rate = 0;
184 unsigned long parent_rate;
185 u8 parent_id;
186 int i;
187
188 if (data->type == UNIPHIER_CLK_TYPE_FIXED_RATE)
189 return data->data.rate.fixed_rate;
190
191 if (data->type == UNIPHIER_CLK_TYPE_GATE) {
192 parent_data = uniphier_clk_get_parent_data(priv, data);
193 if (!parent_data)
194 return 0;
195
196 return __uniphier_clk_set_rate(priv, parent_data, rate, set);
197 }
198
199 if (WARN_ON(data->type != UNIPHIER_CLK_TYPE_MUX))
200 return -EINVAL;
201
202 for (i = 0; i < data->data.mux.num_parents; i++) {
203 parent_id = data->data.mux.parent_ids[i];
204 parent_data = uniphier_clk_get_data(priv, parent_id);
205 if (WARN_ON(!parent_data))
206 return -EINVAL;
207
208 parent_rate = __uniphier_clk_set_rate(priv, parent_data, rate,
209 false);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900210
Masahiro Yamada49806682017-10-13 19:21:59 +0900211 if (parent_rate <= rate && best_rate < parent_rate) {
212 best_rate = parent_rate;
213 best_parent_data = parent_data;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900214 }
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900215 }
216
Masahiro Yamada49806682017-10-13 19:21:59 +0900217 dev_dbg(priv->dev, "id=%u, best_rate=%lu\n", data->id, best_rate);
218
219 if (!best_parent_data)
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900220 return -EINVAL;
221
Masahiro Yamada49806682017-10-13 19:21:59 +0900222 if (!set)
223 return best_rate;
224
225 uniphier_clk_mux_set_parent(priv, &data->data.mux,
226 best_parent_data->id);
227
228 return best_rate = __uniphier_clk_set_rate(priv, best_parent_data,
229 rate, true);
230}
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900231
Masahiro Yamada49806682017-10-13 19:21:59 +0900232static unsigned long uniphier_clk_set_rate(struct clk *clk, ulong rate)
233{
234 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
235 const struct uniphier_clk_data *data;
236
237 data = uniphier_clk_get_data(priv, clk->id);
238 if (!data)
239 return -ENODEV;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900240
Masahiro Yamada49806682017-10-13 19:21:59 +0900241 return __uniphier_clk_set_rate(priv, data, rate, true);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900242}
243
Masahiro Yamada5e05c4d2017-06-22 16:42:04 +0900244static const struct clk_ops uniphier_clk_ops = {
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900245 .enable = uniphier_clk_enable,
Stephen Warrena9622432016-06-17 09:44:00 -0600246 .get_rate = uniphier_clk_get_rate,
247 .set_rate = uniphier_clk_set_rate,
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900248};
249
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900250static int uniphier_clk_probe(struct udevice *dev)
251{
252 struct uniphier_clk_priv *priv = dev_get_priv(dev);
253 fdt_addr_t addr;
254
Simon Glassba1dea42017-05-17 17:18:05 -0600255 addr = devfdt_get_addr(dev->parent);
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900256 if (addr == FDT_ADDR_T_NONE)
257 return -EINVAL;
258
259 priv->base = devm_ioremap(dev, addr, SZ_4K);
260 if (!priv->base)
261 return -ENOMEM;
262
Masahiro Yamada49806682017-10-13 19:21:59 +0900263 priv->dev = dev;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900264 priv->data = (void *)dev_get_driver_data(dev);
265
266 return 0;
267}
268
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900269static const struct udevice_id uniphier_clk_match[] = {
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900270 /* System clock */
271 {
272 .compatible = "socionext,uniphier-ld4-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900273 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900274 },
275 {
276 .compatible = "socionext,uniphier-pro4-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900277 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900278 },
279 {
280 .compatible = "socionext,uniphier-sld8-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900281 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900282 },
283 {
284 .compatible = "socionext,uniphier-pro5-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900285 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900286 },
287 {
288 .compatible = "socionext,uniphier-pxs2-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900289 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900290 },
291 {
292 .compatible = "socionext,uniphier-ld11-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900293 .data = (ulong)uniphier_ld20_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900294 },
295 {
296 .compatible = "socionext,uniphier-ld20-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900297 .data = (ulong)uniphier_ld20_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900298 },
Masahiro Yamada52cdec02017-10-13 19:22:00 +0900299 {
300 .compatible = "socionext,uniphier-pxs3-clock",
301 .data = (ulong)uniphier_pxs3_sys_clk_data,
302 },
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900303 /* Media I/O clock */
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900304 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900305 .compatible = "socionext,uniphier-ld4-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900306 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900307 },
308 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900309 .compatible = "socionext,uniphier-pro4-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900310 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900311 },
312 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900313 .compatible = "socionext,uniphier-sld8-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900314 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900315 },
316 {
Masahiro Yamada9e34de52017-01-28 06:53:41 +0900317 .compatible = "socionext,uniphier-pro5-sd-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900318 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900319 },
320 {
Masahiro Yamada9e34de52017-01-28 06:53:41 +0900321 .compatible = "socionext,uniphier-pxs2-sd-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900322 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900323 },
324 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900325 .compatible = "socionext,uniphier-ld11-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900326 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900327 },
328 {
Masahiro Yamada9e34de52017-01-28 06:53:41 +0900329 .compatible = "socionext,uniphier-ld20-sd-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900330 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900331 },
Masahiro Yamada52cdec02017-10-13 19:22:00 +0900332 {
333 .compatible = "socionext,uniphier-pxs3-sd-clock",
334 .data = (ulong)uniphier_mio_clk_data,
335 },
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900336 { /* sentinel */ }
337};
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900338
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900339U_BOOT_DRIVER(uniphier_clk) = {
340 .name = "uniphier-clk",
341 .id = UCLASS_CLK,
342 .of_match = uniphier_clk_match,
343 .probe = uniphier_clk_probe,
344 .priv_auto_alloc_size = sizeof(struct uniphier_clk_priv),
345 .ops = &uniphier_clk_ops,
346};