blob: c121d82de7ee350e50ef5f0ba71b8065fdfa3e90 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutf3b8bf72017-07-21 23:18:03 +02002/*
Marek Vasut3f1a3a12017-10-09 20:52:33 +02003 * Renesas RCar Gen3 CPG MSSR driver
Marek Vasutf3b8bf72017-07-21 23:18:03 +02004 *
5 * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
6 *
7 * Based on the following driver from Linux kernel:
8 * r8a7796 Clock Pulse Generator / Module Standby and Software Reset
9 *
10 * Copyright (C) 2016 Glider bvba
Marek Vasutf3b8bf72017-07-21 23:18:03 +020011 */
12
13#include <common.h>
14#include <clk-uclass.h>
15#include <dm.h>
Marek Vasutf6b32022023-01-26 21:02:03 +010016#include <dm/device-internal.h>
17#include <dm/lists.h>
Marek Vasutf3b8bf72017-07-21 23:18:03 +020018#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Marek Vasutf3b8bf72017-07-21 23:18:03 +020020#include <wait_bit.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Marek Vasutf3b8bf72017-07-21 23:18:03 +020022#include <asm/io.h>
Hai Phame83700a2023-01-26 21:06:03 +010023#include <linux/bitfield.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Marek Vasutb2970fd2023-01-26 21:06:02 +010025#include <linux/clk-provider.h>
Marek Vasutf6b32022023-01-26 21:02:03 +010026#include <reset-uclass.h>
Marek Vasutf3b8bf72017-07-21 23:18:03 +020027
Marek Vasut4eb4e6e2018-01-08 14:01:40 +010028#include <dt-bindings/clock/renesas-cpg-mssr.h>
29
30#include "renesas-cpg-mssr.h"
Marek Vasute11008b2018-01-15 16:44:39 +010031#include "rcar-gen3-cpg.h"
Hai Pham06d8f972023-01-26 21:06:07 +010032#include "rcar-cpg-lib.h"
Marek Vasutf3b8bf72017-07-21 23:18:03 +020033
Marek Vasutf3b8bf72017-07-21 23:18:03 +020034#define CPG_PLL0CR 0x00d8
35#define CPG_PLL2CR 0x002c
36#define CPG_PLL4CR 0x01f4
37
Hai Phame83700a2023-01-26 21:06:03 +010038static const struct clk_div_table cpg_rpcsrc_div_table[] = {
39 { 2, 5 }, { 3, 6 }, { 0, 0 },
40};
41
Hai Pham6811b572023-01-26 21:06:06 +010042static const struct clk_div_table r8a77970_cpg_sd0h_div_table[] = {
43 { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 },
44 { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
45 { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 },
46};
47
48static const struct clk_div_table r8a77970_cpg_sd0_div_table[] = {
49 { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
50 { 8, 24 }, { 10, 36 }, { 11, 48 }, { 12, 10 },
51 { 0, 0 },
52};
53
Marek Vasut69459b22018-05-31 19:47:42 +020054static int gen3_clk_get_parent(struct gen3_clk_priv *priv, struct clk *clk,
55 struct cpg_mssr_info *info, struct clk *parent)
56{
57 const struct cpg_core_clk *core;
58 int ret;
59
60 if (!renesas_clk_is_mod(clk)) {
61 ret = renesas_clk_get_core(clk, info, &core);
62 if (ret)
63 return ret;
64
Marek Vasut78414832019-03-04 21:38:10 +010065 if (core->type == CLK_TYPE_GEN3_MDSEL) {
Marek Vasut69459b22018-05-31 19:47:42 +020066 parent->dev = clk->dev;
67 parent->id = core->parent >> (priv->sscg ? 16 : 0);
68 parent->id &= 0xffff;
69 return 0;
70 }
71 }
72
73 return renesas_clk_get_parent(clk, info, parent);
74}
75
Hai Pham4dae0762023-01-29 02:50:22 +010076static int gen3_clk_enable(struct clk *clk)
77{
78 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
79
80 return renesas_clk_endisable(clk, priv->base, priv->info, true);
81}
82
83static int gen3_clk_disable(struct clk *clk)
84{
85 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
86
87 return renesas_clk_endisable(clk, priv->base, priv->info, false);
88}
89
90static u64 gen3_clk_get_rate64(struct clk *clk);
91
Marek Vasutc26bf892018-10-30 17:54:20 +010092static int gen3_clk_setup_sdif_div(struct clk *clk, ulong rate)
Marek Vasut5a51be52017-09-15 21:10:08 +020093{
94 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
Marek Vasute11008b2018-01-15 16:44:39 +010095 struct cpg_mssr_info *info = priv->info;
Marek Vasut5a51be52017-09-15 21:10:08 +020096 const struct cpg_core_clk *core;
Hai Pham4dae0762023-01-29 02:50:22 +010097 struct clk parent, grandparent;
Marek Vasut5a51be52017-09-15 21:10:08 +020098 int ret;
99
Hai Pham4dae0762023-01-29 02:50:22 +0100100 /*
101 * The clk may be either CPG_MOD or core clock, in case this is MOD
102 * clock, use core clock one level up, otherwise use the clock as-is.
103 * Note that parent clock here always represents core clock. Also note
104 * that grandparent clock are the parent clock of the core clock here.
105 */
106 if (renesas_clk_is_mod(clk)) {
107 ret = gen3_clk_get_parent(priv, clk, info, &parent);
108 if (ret) {
109 printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
110 return ret;
111 }
112 } else {
113 parent = *clk;
Marek Vasut5a51be52017-09-15 21:10:08 +0200114 }
115
Marek Vasute11008b2018-01-15 16:44:39 +0100116 if (renesas_clk_is_mod(&parent))
Marek Vasut5a51be52017-09-15 21:10:08 +0200117 return 0;
118
Marek Vasute11008b2018-01-15 16:44:39 +0100119 ret = renesas_clk_get_core(&parent, info, &core);
Marek Vasut5a51be52017-09-15 21:10:08 +0200120 if (ret)
121 return ret;
122
Hai Pham4dae0762023-01-29 02:50:22 +0100123 ret = renesas_clk_get_parent(&parent, info, &grandparent);
124 if (ret) {
125 printf("%s[%i] grandparent fail, ret=%i\n", __func__, __LINE__, ret);
126 return ret;
127 }
Marek Vasut5a51be52017-09-15 21:10:08 +0200128
Hai Pham4dae0762023-01-29 02:50:22 +0100129 switch (core->type) {
130 case CLK_TYPE_GEN3_SDH:
131 fallthrough;
132 case CLK_TYPE_GEN4_SDH:
Hai Pham06d8f972023-01-26 21:06:07 +0100133 return rcar_clk_set_rate64_sdh(core->parent,
134 gen3_clk_get_rate64(&grandparent),
135 rate, priv->base + core->offset);
Marek Vasut5a51be52017-09-15 21:10:08 +0200136
Hai Pham4dae0762023-01-29 02:50:22 +0100137 case CLK_TYPE_GEN3_SD:
138 fallthrough;
139 case CLK_TYPE_GEN4_SD:
Hai Pham06d8f972023-01-26 21:06:07 +0100140 return rcar_clk_set_rate64_sd(core->parent,
141 gen3_clk_get_rate64(&grandparent),
142 rate, priv->base + core->offset);
Hai Pham6811b572023-01-26 21:06:06 +0100143
144 case CLK_TYPE_R8A77970_SD0:
Hai Pham06d8f972023-01-26 21:06:07 +0100145 return rcar_clk_set_rate64_div_table(core->parent,
146 gen3_clk_get_rate64(&grandparent),
147 rate, priv->base + core->offset,
148 CPG_SDCKCR_SD0FC_MASK,
149 r8a77970_cpg_sd0_div_table, "SD");
Hai Pham4dae0762023-01-29 02:50:22 +0100150 }
Marek Vasute11008b2018-01-15 16:44:39 +0100151
Hai Pham4dae0762023-01-29 02:50:22 +0100152 return 0;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200153}
154
Marek Vasut8f567862021-04-27 19:36:39 +0200155static u64 gen3_clk_get_rate64_pll_mul_reg(struct gen3_clk_priv *priv,
156 struct clk *parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200157 u32 mul_reg, u32 mult, u32 div,
158 char *name)
159{
160 u32 value;
161 u64 rate;
162
163 if (mul_reg) {
164 value = readl(priv->base + mul_reg);
165 mult = (((value >> 24) & 0x7f) + 1) * 2;
166 div = 1;
167 }
168
169 rate = (gen3_clk_get_rate64(parent) * mult) / div;
170
Marek Vasut1bd25212023-01-26 21:02:05 +0100171 debug("%s[%i] %s clk: mult=%u div=%u => rate=%llu\n",
172 __func__, __LINE__, name, mult, div, rate);
Marek Vasut8f567862021-04-27 19:36:39 +0200173 return rate;
174}
175
Marek Vasut7571ac42018-05-31 19:06:02 +0200176static u64 gen3_clk_get_rate64(struct clk *clk)
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200177{
178 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
Marek Vasutb9234192018-01-08 16:05:28 +0100179 struct cpg_mssr_info *info = priv->info;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200180 struct clk parent;
181 const struct cpg_core_clk *core;
182 const struct rcar_gen3_cpg_pll_config *pll_config =
183 priv->cpg_pll_config;
Hai Phame83700a2023-01-26 21:06:03 +0100184 u32 value, div;
Marek Vasut7571ac42018-05-31 19:06:02 +0200185 u64 rate = 0;
Hai Pham4dae0762023-01-29 02:50:22 +0100186 int ret;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200187
188 debug("%s[%i] Clock: id=%lu\n", __func__, __LINE__, clk->id);
189
Marek Vasut69459b22018-05-31 19:47:42 +0200190 ret = gen3_clk_get_parent(priv, clk, info, &parent);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200191 if (ret) {
192 printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
193 return ret;
194 }
195
Marek Vasute11008b2018-01-15 16:44:39 +0100196 if (renesas_clk_is_mod(clk)) {
Marek Vasut7571ac42018-05-31 19:06:02 +0200197 rate = gen3_clk_get_rate64(&parent);
198 debug("%s[%i] MOD clk: parent=%lu => rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200199 __func__, __LINE__, parent.id, rate);
200 return rate;
201 }
202
Marek Vasute11008b2018-01-15 16:44:39 +0100203 ret = renesas_clk_get_core(clk, info, &core);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200204 if (ret)
205 return ret;
206
207 switch (core->type) {
208 case CLK_TYPE_IN:
Marek Vasutb9234192018-01-08 16:05:28 +0100209 if (core->id == info->clk_extal_id) {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200210 rate = clk_get_rate(&priv->clk_extal);
Marek Vasut7571ac42018-05-31 19:06:02 +0200211 debug("%s[%i] EXTAL clk: rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200212 __func__, __LINE__, rate);
213 return rate;
214 }
215
Marek Vasutb9234192018-01-08 16:05:28 +0100216 if (core->id == info->clk_extalr_id) {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200217 rate = clk_get_rate(&priv->clk_extalr);
Marek Vasut7571ac42018-05-31 19:06:02 +0200218 debug("%s[%i] EXTALR clk: rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200219 __func__, __LINE__, rate);
220 return rate;
221 }
222
223 return -EINVAL;
224
225 case CLK_TYPE_GEN3_MAIN:
Marek Vasut1bd25212023-01-26 21:02:05 +0100226 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200227 0, 1, pll_config->extal_div,
228 "MAIN");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200229
230 case CLK_TYPE_GEN3_PLL0:
Marek Vasut1bd25212023-01-26 21:02:05 +0100231 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200232 CPG_PLL0CR, 0, 0, "PLL0");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200233
234 case CLK_TYPE_GEN3_PLL1:
Marek Vasut1bd25212023-01-26 21:02:05 +0100235 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200236 0, pll_config->pll1_mult,
237 pll_config->pll1_div, "PLL1");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200238
239 case CLK_TYPE_GEN3_PLL2:
Marek Vasut1bd25212023-01-26 21:02:05 +0100240 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200241 CPG_PLL2CR, 0, 0, "PLL2");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200242
243 case CLK_TYPE_GEN3_PLL3:
Marek Vasut1bd25212023-01-26 21:02:05 +0100244 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200245 0, pll_config->pll3_mult,
246 pll_config->pll3_div, "PLL3");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200247
248 case CLK_TYPE_GEN3_PLL4:
Marek Vasut1bd25212023-01-26 21:02:05 +0100249 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200250 CPG_PLL4CR, 0, 0, "PLL4");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200251
Marek Vasut569acef2023-01-26 21:01:56 +0100252 case CLK_TYPE_GEN4_MAIN:
Marek Vasut1bd25212023-01-26 21:02:05 +0100253 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200254 0, 1, pll_config->extal_div,
255 "V3U_MAIN");
256
Marek Vasut569acef2023-01-26 21:01:56 +0100257 case CLK_TYPE_GEN4_PLL1:
Marek Vasut1bd25212023-01-26 21:02:05 +0100258 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200259 0, pll_config->pll1_mult,
260 pll_config->pll1_div,
261 "V3U_PLL1");
262
Marek Vasut569acef2023-01-26 21:01:56 +0100263 case CLK_TYPE_GEN4_PLL2X_3X:
Marek Vasut1bd25212023-01-26 21:02:05 +0100264 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200265 core->offset, 0, 0,
266 "V3U_PLL2X_3X");
267
Marek Vasut569acef2023-01-26 21:01:56 +0100268 case CLK_TYPE_GEN4_PLL5:
Marek Vasut1bd25212023-01-26 21:02:05 +0100269 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200270 0, pll_config->pll5_mult,
271 pll_config->pll5_div,
272 "V3U_PLL5");
273
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200274 case CLK_TYPE_FF:
Marek Vasut1bd25212023-01-26 21:02:05 +0100275 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200276 0, core->mult, core->div,
277 "FIXED");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200278
Marek Vasut78414832019-03-04 21:38:10 +0100279 case CLK_TYPE_GEN3_MDSEL:
Marek Vasut69459b22018-05-31 19:47:42 +0200280 div = (core->div >> (priv->sscg ? 16 : 0)) & 0xffff;
281 rate = gen3_clk_get_rate64(&parent) / div;
282 debug("%s[%i] PE clk: parent=%i div=%u => rate=%llu\n",
283 __func__, __LINE__,
284 (core->parent >> (priv->sscg ? 16 : 0)) & 0xffff,
285 div, rate);
286 return rate;
287
Hai Pham0985e0e2023-01-26 21:01:49 +0100288 case CLK_TYPE_GEN3_SDH: /* Fixed factor 1:1 */
Marek Vasut569acef2023-01-26 21:01:56 +0100289 fallthrough;
290 case CLK_TYPE_GEN4_SDH: /* Fixed factor 1:1 */
Hai Pham06d8f972023-01-26 21:06:07 +0100291 return rcar_clk_get_rate64_sdh(core->parent,
292 gen3_clk_get_rate64(&parent),
293 priv->base + core->offset);
Hai Pham0985e0e2023-01-26 21:01:49 +0100294
Hai Pham6811b572023-01-26 21:06:06 +0100295 case CLK_TYPE_R8A77970_SD0H:
296 return rcar_clk_get_rate64_div_table(core->parent,
297 gen3_clk_get_rate64(&parent),
298 priv->base + core->offset,
299 CPG_SDCKCR_SDHFC_MASK,
300 r8a77970_cpg_sd0h_div_table, "SDH");
301
Hai Pham4dae0762023-01-29 02:50:22 +0100302 case CLK_TYPE_GEN3_SD:
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200303 fallthrough;
Marek Vasut569acef2023-01-26 21:01:56 +0100304 case CLK_TYPE_GEN4_SD:
Hai Pham06d8f972023-01-26 21:06:07 +0100305 return rcar_clk_get_rate64_sd(core->parent,
306 gen3_clk_get_rate64(&parent),
307 priv->base + core->offset);
Marek Vasutc1aee322017-09-15 21:10:29 +0200308
Hai Pham6811b572023-01-26 21:06:06 +0100309 case CLK_TYPE_R8A77970_SD0:
310 return rcar_clk_get_rate64_div_table(core->parent,
311 gen3_clk_get_rate64(&parent),
312 priv->base + core->offset,
313 CPG_SDCKCR_SD0FC_MASK,
314 r8a77970_cpg_sd0_div_table, "SD");
315
Hai Phame83700a2023-01-26 21:06:03 +0100316 case CLK_TYPE_GEN3_RPCSRC:
317 return rcar_clk_get_rate64_div_table(core->parent,
318 gen3_clk_get_rate64(&parent),
319 priv->base + CPG_RPCCKCR,
320 CPG_RPCCKCR_DIV_POST_MASK,
321 cpg_rpcsrc_div_table, "RPCSRC");
322
Hai Pham85e691e2023-01-26 21:06:04 +0100323 case CLK_TYPE_GEN3_D3_RPCSRC:
324 case CLK_TYPE_GEN3_E3_RPCSRC:
325 /*
326 * Register RPCSRC as fixed factor clock based on the
327 * MD[4:1] pins and CPG_RPCCKCR[4:3] register value for
328 * which has been set prior to booting the kernel.
329 */
330 value = (readl(priv->base + CPG_RPCCKCR) & GENMASK(4, 3)) >> 3;
331
332 switch (value) {
333 case 0:
334 div = 5;
335 break;
336 case 1:
337 div = 3;
338 break;
339 case 2:
340 div = core->div;
341 break;
342 case 3:
343 default:
344 div = 2;
345 break;
346 }
347
348 rate = gen3_clk_get_rate64(&parent) / div;
349 debug("%s[%i] E3/D3 RPCSRC clk: parent=%i div=%u => rate=%llu\n",
350 __func__, __LINE__, (core->parent >> 16) & 0xffff, div, rate);
351
352 return rate;
353
Marek Vasutc1aee322017-09-15 21:10:29 +0200354 case CLK_TYPE_GEN3_RPC:
Marek Vasut569acef2023-01-26 21:01:56 +0100355 case CLK_TYPE_GEN4_RPC:
Hai Pham06d8f972023-01-26 21:06:07 +0100356 return rcar_clk_get_rate64_rpc(core->parent,
357 gen3_clk_get_rate64(&parent),
358 priv->base + CPG_RPCCKCR);
Marek Vasutc1aee322017-09-15 21:10:29 +0200359
Hai Phame83700a2023-01-26 21:06:03 +0100360 case CLK_TYPE_GEN3_RPCD2:
361 case CLK_TYPE_GEN4_RPCD2:
Hai Pham06d8f972023-01-26 21:06:07 +0100362 return rcar_clk_get_rate64_rpcd2(core->parent,
363 gen3_clk_get_rate64(&parent));
Marek Vasutc1aee322017-09-15 21:10:29 +0200364
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200365 }
366
367 printf("%s[%i] unknown fail\n", __func__, __LINE__);
368
369 return -ENOENT;
370}
371
Marek Vasut7571ac42018-05-31 19:06:02 +0200372static ulong gen3_clk_get_rate(struct clk *clk)
373{
374 return gen3_clk_get_rate64(clk);
375}
376
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200377static ulong gen3_clk_set_rate(struct clk *clk, ulong rate)
378{
Marek Vasut414dbbe2018-01-11 16:28:31 +0100379 /* Force correct SD-IF divider configuration if applicable */
Marek Vasutc26bf892018-10-30 17:54:20 +0100380 gen3_clk_setup_sdif_div(clk, rate);
Marek Vasut7571ac42018-05-31 19:06:02 +0200381 return gen3_clk_get_rate64(clk);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200382}
383
384static int gen3_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
385{
386 if (args->args_count != 2) {
Sean Andersona1b654b2021-12-01 14:26:53 -0500387 debug("Invalid args_count: %d\n", args->args_count);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200388 return -EINVAL;
389 }
390
391 clk->id = (args->args[0] << 16) | args->args[1];
392
393 return 0;
394}
395
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100396const struct clk_ops gen3_clk_ops = {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200397 .enable = gen3_clk_enable,
398 .disable = gen3_clk_disable,
399 .get_rate = gen3_clk_get_rate,
400 .set_rate = gen3_clk_set_rate,
401 .of_xlate = gen3_clk_of_xlate,
402};
403
Marek Vasutf6b32022023-01-26 21:02:03 +0100404static int gen3_clk_probe(struct udevice *dev)
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200405{
406 struct gen3_clk_priv *priv = dev_get_priv(dev);
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100407 struct cpg_mssr_info *info =
408 (struct cpg_mssr_info *)dev_get_driver_data(dev);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200409 fdt_addr_t rst_base;
410 u32 cpg_mode;
411 int ret;
412
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900413 priv->base = dev_read_addr_ptr(dev);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200414 if (!priv->base)
415 return -EINVAL;
416
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100417 priv->info = info;
418 ret = fdt_node_offset_by_compatible(gd->fdt_blob, -1, info->reset_node);
419 if (ret < 0)
420 return ret;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200421
422 rst_base = fdtdec_get_addr(gd->fdt_blob, ret, "reg");
423 if (rst_base == FDT_ADDR_T_NONE)
424 return -EINVAL;
425
Marek Vasut814217e2021-04-25 21:53:05 +0200426 cpg_mode = readl(rst_base + info->reset_modemr_offset);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200427
Marek Vasut28f90042018-01-16 19:23:17 +0100428 priv->cpg_pll_config =
429 (struct rcar_gen3_cpg_pll_config *)info->get_pll_config(cpg_mode);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200430 if (!priv->cpg_pll_config->extal_div)
431 return -EINVAL;
432
Marek Vasut69459b22018-05-31 19:47:42 +0200433 priv->sscg = !(cpg_mode & BIT(12));
434
Hai Pham94803462020-11-05 22:30:37 +0700435 if (info->reg_layout == CLK_REG_LAYOUT_RCAR_GEN2_AND_GEN3) {
436 priv->info->status_regs = mstpsr;
437 priv->info->control_regs = smstpcr;
438 priv->info->reset_regs = srcr;
439 priv->info->reset_clear_regs = srstclr;
Hai Pham86d59f32020-08-11 10:46:34 +0700440 } else if (info->reg_layout == CLK_REG_LAYOUT_RCAR_V3U) {
441 priv->info->status_regs = mstpsr_for_v3u;
442 priv->info->control_regs = mstpcr_for_v3u;
443 priv->info->reset_regs = srcr_for_v3u;
444 priv->info->reset_clear_regs = srstclr_for_v3u;
Hai Pham94803462020-11-05 22:30:37 +0700445 } else {
446 return -EINVAL;
447 }
448
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200449 ret = clk_get_by_name(dev, "extal", &priv->clk_extal);
450 if (ret < 0)
451 return ret;
452
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100453 if (info->extalr_node) {
454 ret = clk_get_by_name(dev, info->extalr_node, &priv->clk_extalr);
Marek Vasutfb0aa292017-10-08 21:09:15 +0200455 if (ret < 0)
456 return ret;
457 }
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200458
459 return 0;
460}
461
Marek Vasutf6b32022023-01-26 21:02:03 +0100462static int gen3_clk_remove(struct udevice *dev)
Marek Vasutdf6a1142017-11-25 22:08:55 +0100463{
464 struct gen3_clk_priv *priv = dev_get_priv(dev);
Marek Vasutdf6a1142017-11-25 22:08:55 +0100465
Marek Vasute11008b2018-01-15 16:44:39 +0100466 return renesas_clk_remove(priv->base, priv->info);
Marek Vasutdf6a1142017-11-25 22:08:55 +0100467}
Marek Vasutf6b32022023-01-26 21:02:03 +0100468
469U_BOOT_DRIVER(clk_gen3) = {
470 .name = "clk_gen3",
471 .id = UCLASS_CLK,
472 .priv_auto = sizeof(struct gen3_clk_priv),
473 .ops = &gen3_clk_ops,
474 .probe = gen3_clk_probe,
475 .remove = gen3_clk_remove,
476};
477
478static int gen3_reset_assert(struct reset_ctl *reset_ctl)
479{
480 struct udevice *cdev = (struct udevice *)dev_get_driver_data(reset_ctl->dev);
481 struct gen3_clk_priv *priv = dev_get_priv(cdev);
Marek Vasutef5c0b12023-03-18 12:30:53 +0100482 unsigned int packed_id = MOD_CLK_PACK(reset_ctl->id);
483 unsigned int reg = packed_id / 32;
484 unsigned int bit = packed_id % 32;
Marek Vasutf6b32022023-01-26 21:02:03 +0100485 u32 bitmask = BIT(bit);
486
487 writel(bitmask, priv->base + priv->info->reset_regs[reg]);
488
489 return 0;
490}
491
492static int gen3_reset_deassert(struct reset_ctl *reset_ctl)
493{
494 struct udevice *cdev = (struct udevice *)dev_get_driver_data(reset_ctl->dev);
495 struct gen3_clk_priv *priv = dev_get_priv(cdev);
Marek Vasutef5c0b12023-03-18 12:30:53 +0100496 unsigned int packed_id = MOD_CLK_PACK(reset_ctl->id);
497 unsigned int reg = packed_id / 32;
498 unsigned int bit = packed_id % 32;
Marek Vasutf6b32022023-01-26 21:02:03 +0100499 u32 bitmask = BIT(bit);
500
501 writel(bitmask, priv->base + priv->info->reset_clear_regs[reg]);
502
503 return 0;
504}
505
506static const struct reset_ops rst_gen3_ops = {
507 .rst_assert = gen3_reset_assert,
508 .rst_deassert = gen3_reset_deassert,
509};
510
511U_BOOT_DRIVER(rst_gen3) = {
512 .name = "rst_gen3",
513 .id = UCLASS_RESET,
514 .ops = &rst_gen3_ops,
515};
516
517int gen3_cpg_bind(struct udevice *parent)
518{
519 struct cpg_mssr_info *info =
520 (struct cpg_mssr_info *)dev_get_driver_data(parent);
521 struct udevice *cdev, *rdev;
522 struct driver *drv;
523 int ret;
524
525 drv = lists_driver_lookup_name("clk_gen3");
526 if (!drv)
527 return -ENOENT;
528
529 ret = device_bind_with_driver_data(parent, drv, "clk_gen3", (ulong)info,
530 dev_ofnode(parent), &cdev);
531 if (ret)
532 return ret;
533
534 drv = lists_driver_lookup_name("rst_gen3");
535 if (!drv)
536 return -ENOENT;
537
538 ret = device_bind_with_driver_data(parent, drv, "rst_gen3", (ulong)cdev,
539 dev_ofnode(parent), &rdev);
540 if (ret)
541 device_unbind(cdev);
542
543 return ret;
544}