blob: 1da63819e7db62f86b95d8ce3d4561dac8dad7bb [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>
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090010#include <linux/bitops.h>
11#include <linux/io.h>
Masahiro Yamada46a624f2016-03-24 22:32:39 +090012#include <linux/sizes.h>
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090013
14#include "clk-uniphier.h"
15
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090016/**
17 * struct uniphier_clk_priv - private data for UniPhier clock driver
18 *
19 * @base: base address of the clock provider
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090020 * @data: SoC specific data
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090021 */
22struct uniphier_clk_priv {
Masahiro Yamada49806682017-10-13 19:21:59 +090023 struct udevice *dev;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090024 void __iomem *base;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090025 const struct uniphier_clk_data *data;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090026};
27
Masahiro Yamada49806682017-10-13 19:21:59 +090028static void uniphier_clk_gate_enable(struct uniphier_clk_priv *priv,
29 const struct uniphier_clk_gate_data *gate)
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090030{
Masahiro Yamada49806682017-10-13 19:21:59 +090031 u32 val;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090032
Masahiro Yamada49806682017-10-13 19:21:59 +090033 val = readl(priv->base + gate->reg);
34 val |= BIT(gate->bit);
35 writel(val, priv->base + gate->reg);
36}
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090037
Masahiro Yamada49806682017-10-13 19:21:59 +090038static void uniphier_clk_mux_set_parent(struct uniphier_clk_priv *priv,
39 const struct uniphier_clk_mux_data *mux,
40 u8 id)
41{
42 u32 val;
43 int i;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090044
Masahiro Yamada49806682017-10-13 19:21:59 +090045 for (i = 0; i < mux->num_parents; i++) {
46 if (mux->parent_ids[i] != id)
47 continue;
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090048
Masahiro Yamada49806682017-10-13 19:21:59 +090049 val = readl(priv->base + mux->reg);
50 val &= ~mux->masks[i];
51 val |= mux->vals[i];
52 writel(val, priv->base + mux->reg);
53 return;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090054 }
55
Masahiro Yamada49806682017-10-13 19:21:59 +090056 WARN_ON(1);
Masahiro Yamadaf18559e2016-09-22 07:42:21 +090057}
58
Masahiro Yamada49806682017-10-13 19:21:59 +090059static u8 uniphier_clk_mux_get_parent(struct uniphier_clk_priv *priv,
60 const struct uniphier_clk_mux_data *mux)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090061{
Masahiro Yamada49806682017-10-13 19:21:59 +090062 u32 val;
63 int i;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090064
Masahiro Yamada49806682017-10-13 19:21:59 +090065 val = readl(priv->base + mux->reg);
66
67 for (i = 0; i < mux->num_parents; i++)
68 if ((mux->masks[i] & val) == mux->vals[i])
69 return mux->parent_ids[i];
70
71 dev_err(priv->dev, "invalid mux setting\n");
72
73 return UNIPHIER_CLK_ID_INVALID;
74}
75
76static const struct uniphier_clk_data *uniphier_clk_get_data(
77 struct uniphier_clk_priv *priv, u8 id)
78{
79 const struct uniphier_clk_data *data;
80
81 for (data = priv->data; data->type != UNIPHIER_CLK_TYPE_END; data++)
82 if (data->id == id)
83 return data;
84
85 dev_err(priv->dev, "id=%u not found\n", id);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090086
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +090087 return NULL;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090088}
89
Masahiro Yamada49806682017-10-13 19:21:59 +090090static const struct uniphier_clk_data *uniphier_clk_get_parent_data(
91 struct uniphier_clk_priv *priv,
92 const struct uniphier_clk_data *data)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090093{
Masahiro Yamada49806682017-10-13 19:21:59 +090094 const struct uniphier_clk_data *parent_data;
95 u8 parent_id = UNIPHIER_CLK_ID_INVALID;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +090096
Masahiro Yamada49806682017-10-13 19:21:59 +090097 switch (data->type) {
98 case UNIPHIER_CLK_TYPE_GATE:
99 parent_id = data->data.gate.parent_id;
100 break;
101 case UNIPHIER_CLK_TYPE_MUX:
102 parent_id = uniphier_clk_mux_get_parent(priv, &data->data.mux);
103 break;
104 default:
105 break;
106 }
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900107
Masahiro Yamada49806682017-10-13 19:21:59 +0900108 if (parent_id == UNIPHIER_CLK_ID_INVALID)
109 return NULL;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900110
Masahiro Yamada49806682017-10-13 19:21:59 +0900111 parent_data = uniphier_clk_get_data(priv, parent_id);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900112
Masahiro Yamada49806682017-10-13 19:21:59 +0900113 WARN_ON(!parent_data);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900114
Masahiro Yamada49806682017-10-13 19:21:59 +0900115 return parent_data;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900116}
117
Masahiro Yamada49806682017-10-13 19:21:59 +0900118static void __uniphier_clk_enable(struct uniphier_clk_priv *priv,
119 const struct uniphier_clk_data *data)
120{
121 const struct uniphier_clk_data *parent_data;
122
123 if (data->type == UNIPHIER_CLK_TYPE_GATE)
124 uniphier_clk_gate_enable(priv, &data->data.gate);
125
126 parent_data = uniphier_clk_get_parent_data(priv, data);
127 if (!parent_data)
128 return;
129
130 return __uniphier_clk_enable(priv, parent_data);
131}
132
133static int uniphier_clk_enable(struct clk *clk)
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900134{
Stephen Warrena9622432016-06-17 09:44:00 -0600135 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
Masahiro Yamada49806682017-10-13 19:21:59 +0900136 const struct uniphier_clk_data *data;
137
138 data = uniphier_clk_get_data(priv, clk->id);
139 if (!data)
140 return -ENODEV;
141
142 __uniphier_clk_enable(priv, data);
143
144 return 0;
145}
146
147static unsigned long __uniphier_clk_get_rate(
148 struct uniphier_clk_priv *priv,
149 const struct uniphier_clk_data *data)
150{
151 const struct uniphier_clk_data *parent_data;
152
153 if (data->type == UNIPHIER_CLK_TYPE_FIXED_RATE)
154 return data->data.rate.fixed_rate;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900155
Masahiro Yamada49806682017-10-13 19:21:59 +0900156 parent_data = uniphier_clk_get_parent_data(priv, data);
157 if (!parent_data)
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900158 return 0;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900159
Masahiro Yamada49806682017-10-13 19:21:59 +0900160 return __uniphier_clk_get_rate(priv, parent_data);
161}
162
163static unsigned long uniphier_clk_get_rate(struct clk *clk)
164{
165 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
166 const struct uniphier_clk_data *data;
167
168 data = uniphier_clk_get_data(priv, clk->id);
169 if (!data)
170 return -ENODEV;
171
172 return __uniphier_clk_get_rate(priv, data);
173}
174
175static unsigned long __uniphier_clk_set_rate(
176 struct uniphier_clk_priv *priv,
177 const struct uniphier_clk_data *data,
178 unsigned long rate, bool set)
179{
180 const struct uniphier_clk_data *best_parent_data = NULL;
181 const struct uniphier_clk_data *parent_data;
182 unsigned long best_rate = 0;
183 unsigned long parent_rate;
184 u8 parent_id;
185 int i;
186
187 if (data->type == UNIPHIER_CLK_TYPE_FIXED_RATE)
188 return data->data.rate.fixed_rate;
189
190 if (data->type == UNIPHIER_CLK_TYPE_GATE) {
191 parent_data = uniphier_clk_get_parent_data(priv, data);
192 if (!parent_data)
193 return 0;
194
195 return __uniphier_clk_set_rate(priv, parent_data, rate, set);
196 }
197
198 if (WARN_ON(data->type != UNIPHIER_CLK_TYPE_MUX))
199 return -EINVAL;
200
201 for (i = 0; i < data->data.mux.num_parents; i++) {
202 parent_id = data->data.mux.parent_ids[i];
203 parent_data = uniphier_clk_get_data(priv, parent_id);
204 if (WARN_ON(!parent_data))
205 return -EINVAL;
206
207 parent_rate = __uniphier_clk_set_rate(priv, parent_data, rate,
208 false);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900209
Masahiro Yamada49806682017-10-13 19:21:59 +0900210 if (parent_rate <= rate && best_rate < parent_rate) {
211 best_rate = parent_rate;
212 best_parent_data = parent_data;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900213 }
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900214 }
215
Masahiro Yamada49806682017-10-13 19:21:59 +0900216 dev_dbg(priv->dev, "id=%u, best_rate=%lu\n", data->id, best_rate);
217
218 if (!best_parent_data)
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900219 return -EINVAL;
220
Masahiro Yamada49806682017-10-13 19:21:59 +0900221 if (!set)
222 return best_rate;
223
224 uniphier_clk_mux_set_parent(priv, &data->data.mux,
225 best_parent_data->id);
226
227 return best_rate = __uniphier_clk_set_rate(priv, best_parent_data,
228 rate, true);
229}
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900230
Masahiro Yamada49806682017-10-13 19:21:59 +0900231static unsigned long uniphier_clk_set_rate(struct clk *clk, ulong rate)
232{
233 struct uniphier_clk_priv *priv = dev_get_priv(clk->dev);
234 const struct uniphier_clk_data *data;
235
236 data = uniphier_clk_get_data(priv, clk->id);
237 if (!data)
238 return -ENODEV;
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900239
Masahiro Yamada49806682017-10-13 19:21:59 +0900240 return __uniphier_clk_set_rate(priv, data, rate, true);
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900241}
242
Masahiro Yamada5e05c4d2017-06-22 16:42:04 +0900243static const struct clk_ops uniphier_clk_ops = {
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900244 .enable = uniphier_clk_enable,
Stephen Warrena9622432016-06-17 09:44:00 -0600245 .get_rate = uniphier_clk_get_rate,
246 .set_rate = uniphier_clk_set_rate,
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900247};
248
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900249static int uniphier_clk_probe(struct udevice *dev)
250{
251 struct uniphier_clk_priv *priv = dev_get_priv(dev);
252 fdt_addr_t addr;
253
Simon Glassba1dea42017-05-17 17:18:05 -0600254 addr = devfdt_get_addr(dev->parent);
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900255 if (addr == FDT_ADDR_T_NONE)
256 return -EINVAL;
257
258 priv->base = devm_ioremap(dev, addr, SZ_4K);
259 if (!priv->base)
260 return -ENOMEM;
261
Masahiro Yamada49806682017-10-13 19:21:59 +0900262 priv->dev = dev;
Masahiro Yamada8bdfe0a2016-10-17 22:18:01 +0900263 priv->data = (void *)dev_get_driver_data(dev);
264
265 return 0;
266}
267
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900268static const struct udevice_id uniphier_clk_match[] = {
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900269 /* System clock */
270 {
271 .compatible = "socionext,uniphier-ld4-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900272 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900273 },
274 {
275 .compatible = "socionext,uniphier-pro4-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900276 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900277 },
278 {
279 .compatible = "socionext,uniphier-sld8-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900280 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900281 },
282 {
283 .compatible = "socionext,uniphier-pro5-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900284 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900285 },
286 {
287 .compatible = "socionext,uniphier-pxs2-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900288 .data = (ulong)uniphier_pxs2_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900289 },
290 {
291 .compatible = "socionext,uniphier-ld11-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900292 .data = (ulong)uniphier_ld20_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900293 },
294 {
295 .compatible = "socionext,uniphier-ld20-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900296 .data = (ulong)uniphier_ld20_sys_clk_data,
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900297 },
Masahiro Yamada52cdec02017-10-13 19:22:00 +0900298 {
299 .compatible = "socionext,uniphier-pxs3-clock",
300 .data = (ulong)uniphier_pxs3_sys_clk_data,
301 },
Masahiro Yamada644f5bf2017-08-29 01:06:15 +0900302 /* Media I/O clock */
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900303 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900304 .compatible = "socionext,uniphier-ld4-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900305 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900306 },
307 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900308 .compatible = "socionext,uniphier-pro4-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900309 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900310 },
311 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900312 .compatible = "socionext,uniphier-sld8-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900313 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900314 },
315 {
Masahiro Yamada9e34de52017-01-28 06:53:41 +0900316 .compatible = "socionext,uniphier-pro5-sd-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900317 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900318 },
319 {
Masahiro Yamada9e34de52017-01-28 06:53:41 +0900320 .compatible = "socionext,uniphier-pxs2-sd-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900321 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900322 },
323 {
Masahiro Yamadac3a29fe2016-09-22 07:42:22 +0900324 .compatible = "socionext,uniphier-ld11-mio-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900325 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900326 },
327 {
Masahiro Yamada9e34de52017-01-28 06:53:41 +0900328 .compatible = "socionext,uniphier-ld20-sd-clock",
Masahiro Yamada49806682017-10-13 19:21:59 +0900329 .data = (ulong)uniphier_mio_clk_data,
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900330 },
Masahiro Yamada52cdec02017-10-13 19:22:00 +0900331 {
332 .compatible = "socionext,uniphier-pxs3-sd-clock",
333 .data = (ulong)uniphier_mio_clk_data,
334 },
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900335 { /* sentinel */ }
336};
Masahiro Yamadae4dfb052016-02-02 21:11:32 +0900337
Masahiro Yamadaf18559e2016-09-22 07:42:21 +0900338U_BOOT_DRIVER(uniphier_clk) = {
339 .name = "uniphier-clk",
340 .id = UCLASS_CLK,
341 .of_match = uniphier_clk_match,
342 .probe = uniphier_clk_probe,
343 .priv_auto_alloc_size = sizeof(struct uniphier_clk_priv),
344 .ops = &uniphier_clk_ops,
345};