blob: 812580475c6ce5a09291a541a31fa2409b147b50 [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>
Simon Glass4dcacfc2020-05-10 11:40:13 -060023#include <linux/bitops.h>
Marek Vasutf6b32022023-01-26 21:02:03 +010024#include <reset-uclass.h>
Marek Vasutf3b8bf72017-07-21 23:18:03 +020025
Marek Vasut4eb4e6e2018-01-08 14:01:40 +010026#include <dt-bindings/clock/renesas-cpg-mssr.h>
27
28#include "renesas-cpg-mssr.h"
Marek Vasute11008b2018-01-15 16:44:39 +010029#include "rcar-gen3-cpg.h"
Marek Vasutf3b8bf72017-07-21 23:18:03 +020030
Marek Vasutf3b8bf72017-07-21 23:18:03 +020031#define CPG_PLL0CR 0x00d8
32#define CPG_PLL2CR 0x002c
33#define CPG_PLL4CR 0x01f4
34
Marek Vasutc1aee322017-09-15 21:10:29 +020035#define CPG_RPC_PREDIV_MASK 0x3
36#define CPG_RPC_PREDIV_OFFSET 3
37#define CPG_RPC_POSTDIV_MASK 0x7
38#define CPG_RPC_POSTDIV_OFFSET 0
39
Marek Vasutf3b8bf72017-07-21 23:18:03 +020040/*
Marek Vasutf3b8bf72017-07-21 23:18:03 +020041 * SDn Clock
42 */
43#define CPG_SD_STP_HCK BIT(9)
44#define CPG_SD_STP_CK BIT(8)
45
46#define CPG_SD_STP_MASK (CPG_SD_STP_HCK | CPG_SD_STP_CK)
47#define CPG_SD_FC_MASK (0x7 << 2 | 0x3 << 0)
48
49#define CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) \
50{ \
51 .val = ((stp_hck) ? CPG_SD_STP_HCK : 0) | \
52 ((stp_ck) ? CPG_SD_STP_CK : 0) | \
53 ((sd_srcfc) << 2) | \
54 ((sd_fc) << 0), \
55 .div = (sd_div), \
56}
57
58struct sd_div_table {
59 u32 val;
60 unsigned int div;
61};
62
63/* SDn divider
64 * sd_srcfc sd_fc div
65 * stp_hck stp_ck (div) (div) = sd_srcfc x sd_fc
66 *-------------------------------------------------------------------
67 * 0 0 0 (1) 1 (4) 4
68 * 0 0 1 (2) 1 (4) 8
69 * 1 0 2 (4) 1 (4) 16
70 * 1 0 3 (8) 1 (4) 32
71 * 1 0 4 (16) 1 (4) 64
72 * 0 0 0 (1) 0 (2) 2
73 * 0 0 1 (2) 0 (2) 4
74 * 1 0 2 (4) 0 (2) 8
75 * 1 0 3 (8) 0 (2) 16
76 * 1 0 4 (16) 0 (2) 32
77 */
78static const struct sd_div_table cpg_sd_div_table[] = {
79/* CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) */
80 CPG_SD_DIV_TABLE_DATA(0, 0, 0, 1, 4),
81 CPG_SD_DIV_TABLE_DATA(0, 0, 1, 1, 8),
82 CPG_SD_DIV_TABLE_DATA(1, 0, 2, 1, 16),
83 CPG_SD_DIV_TABLE_DATA(1, 0, 3, 1, 32),
84 CPG_SD_DIV_TABLE_DATA(1, 0, 4, 1, 64),
85 CPG_SD_DIV_TABLE_DATA(0, 0, 0, 0, 2),
86 CPG_SD_DIV_TABLE_DATA(0, 0, 1, 0, 4),
87 CPG_SD_DIV_TABLE_DATA(1, 0, 2, 0, 8),
88 CPG_SD_DIV_TABLE_DATA(1, 0, 3, 0, 16),
89 CPG_SD_DIV_TABLE_DATA(1, 0, 4, 0, 32),
90};
91
Marek Vasut69459b22018-05-31 19:47:42 +020092static int gen3_clk_get_parent(struct gen3_clk_priv *priv, struct clk *clk,
93 struct cpg_mssr_info *info, struct clk *parent)
94{
95 const struct cpg_core_clk *core;
96 int ret;
97
98 if (!renesas_clk_is_mod(clk)) {
99 ret = renesas_clk_get_core(clk, info, &core);
100 if (ret)
101 return ret;
102
Marek Vasut78414832019-03-04 21:38:10 +0100103 if (core->type == CLK_TYPE_GEN3_MDSEL) {
Marek Vasut69459b22018-05-31 19:47:42 +0200104 parent->dev = clk->dev;
105 parent->id = core->parent >> (priv->sscg ? 16 : 0);
106 parent->id &= 0xffff;
107 return 0;
108 }
109 }
110
111 return renesas_clk_get_parent(clk, info, parent);
112}
113
Marek Vasutc26bf892018-10-30 17:54:20 +0100114static int gen3_clk_setup_sdif_div(struct clk *clk, ulong rate)
Marek Vasut5a51be52017-09-15 21:10:08 +0200115{
116 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
Marek Vasute11008b2018-01-15 16:44:39 +0100117 struct cpg_mssr_info *info = priv->info;
Marek Vasut5a51be52017-09-15 21:10:08 +0200118 const struct cpg_core_clk *core;
119 struct clk parent;
120 int ret;
121
Marek Vasut69459b22018-05-31 19:47:42 +0200122 ret = gen3_clk_get_parent(priv, clk, info, &parent);
Marek Vasut5a51be52017-09-15 21:10:08 +0200123 if (ret) {
124 printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
125 return ret;
126 }
127
Marek Vasute11008b2018-01-15 16:44:39 +0100128 if (renesas_clk_is_mod(&parent))
Marek Vasut5a51be52017-09-15 21:10:08 +0200129 return 0;
130
Marek Vasute11008b2018-01-15 16:44:39 +0100131 ret = renesas_clk_get_core(&parent, info, &core);
Marek Vasut5a51be52017-09-15 21:10:08 +0200132 if (ret)
133 return ret;
134
135 if (core->type != CLK_TYPE_GEN3_SD)
136 return 0;
137
138 debug("%s[%i] SDIF offset=%x\n", __func__, __LINE__, core->offset);
139
Marek Vasutc26bf892018-10-30 17:54:20 +0100140 writel((rate == 400000000) ? 0x4 : 0x1, priv->base + core->offset);
Marek Vasut5a51be52017-09-15 21:10:08 +0200141
142 return 0;
143}
144
Marek Vasute11008b2018-01-15 16:44:39 +0100145static int gen3_clk_enable(struct clk *clk)
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200146{
147 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200148
Hai Pham5460ee02020-05-22 10:39:04 +0700149 return renesas_clk_endisable(clk, priv->base, priv->info, true);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200150}
151
152static int gen3_clk_disable(struct clk *clk)
153{
Marek Vasute11008b2018-01-15 16:44:39 +0100154 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
155
Hai Pham5460ee02020-05-22 10:39:04 +0700156 return renesas_clk_endisable(clk, priv->base, priv->info, false);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200157}
158
Marek Vasut8f567862021-04-27 19:36:39 +0200159static u64 gen3_clk_get_rate64(struct clk *clk);
160
161static u64 gen3_clk_get_rate64_pll_mul_reg(struct gen3_clk_priv *priv,
162 struct clk *parent,
163 const struct cpg_core_clk *core,
164 u32 mul_reg, u32 mult, u32 div,
165 char *name)
166{
167 u32 value;
168 u64 rate;
169
170 if (mul_reg) {
171 value = readl(priv->base + mul_reg);
172 mult = (((value >> 24) & 0x7f) + 1) * 2;
173 div = 1;
174 }
175
176 rate = (gen3_clk_get_rate64(parent) * mult) / div;
177
178 debug("%s[%i] %s clk: parent=%i mult=%u div=%u => rate=%llu\n",
179 __func__, __LINE__, name, core->parent, mult, div, rate);
180 return rate;
181}
182
Marek Vasut7571ac42018-05-31 19:06:02 +0200183static u64 gen3_clk_get_rate64(struct clk *clk)
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200184{
185 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
Marek Vasutb9234192018-01-08 16:05:28 +0100186 struct cpg_mssr_info *info = priv->info;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200187 struct clk parent;
188 const struct cpg_core_clk *core;
189 const struct rcar_gen3_cpg_pll_config *pll_config =
190 priv->cpg_pll_config;
Marek Vasut8f567862021-04-27 19:36:39 +0200191 u32 value, div, prediv, postdiv;
Marek Vasut7571ac42018-05-31 19:06:02 +0200192 u64 rate = 0;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200193 int i, ret;
194
195 debug("%s[%i] Clock: id=%lu\n", __func__, __LINE__, clk->id);
196
Marek Vasut69459b22018-05-31 19:47:42 +0200197 ret = gen3_clk_get_parent(priv, clk, info, &parent);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200198 if (ret) {
199 printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
200 return ret;
201 }
202
Marek Vasute11008b2018-01-15 16:44:39 +0100203 if (renesas_clk_is_mod(clk)) {
Marek Vasut7571ac42018-05-31 19:06:02 +0200204 rate = gen3_clk_get_rate64(&parent);
205 debug("%s[%i] MOD clk: parent=%lu => rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200206 __func__, __LINE__, parent.id, rate);
207 return rate;
208 }
209
Marek Vasute11008b2018-01-15 16:44:39 +0100210 ret = renesas_clk_get_core(clk, info, &core);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200211 if (ret)
212 return ret;
213
214 switch (core->type) {
215 case CLK_TYPE_IN:
Marek Vasutb9234192018-01-08 16:05:28 +0100216 if (core->id == info->clk_extal_id) {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200217 rate = clk_get_rate(&priv->clk_extal);
Marek Vasut7571ac42018-05-31 19:06:02 +0200218 debug("%s[%i] EXTAL clk: rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200219 __func__, __LINE__, rate);
220 return rate;
221 }
222
Marek Vasutb9234192018-01-08 16:05:28 +0100223 if (core->id == info->clk_extalr_id) {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200224 rate = clk_get_rate(&priv->clk_extalr);
Marek Vasut7571ac42018-05-31 19:06:02 +0200225 debug("%s[%i] EXTALR clk: rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200226 __func__, __LINE__, rate);
227 return rate;
228 }
229
230 return -EINVAL;
231
232 case CLK_TYPE_GEN3_MAIN:
Marek Vasut8f567862021-04-27 19:36:39 +0200233 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
234 0, 1, pll_config->extal_div,
235 "MAIN");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200236
237 case CLK_TYPE_GEN3_PLL0:
Marek Vasut8f567862021-04-27 19:36:39 +0200238 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
239 CPG_PLL0CR, 0, 0, "PLL0");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200240
241 case CLK_TYPE_GEN3_PLL1:
Marek Vasut8f567862021-04-27 19:36:39 +0200242 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
243 0, pll_config->pll1_mult,
244 pll_config->pll1_div, "PLL1");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200245
246 case CLK_TYPE_GEN3_PLL2:
Marek Vasut8f567862021-04-27 19:36:39 +0200247 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
248 CPG_PLL2CR, 0, 0, "PLL2");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200249
250 case CLK_TYPE_GEN3_PLL3:
Marek Vasut8f567862021-04-27 19:36:39 +0200251 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
252 0, pll_config->pll3_mult,
253 pll_config->pll3_div, "PLL3");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200254
255 case CLK_TYPE_GEN3_PLL4:
Marek Vasut8f567862021-04-27 19:36:39 +0200256 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
257 CPG_PLL4CR, 0, 0, "PLL4");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200258
Marek Vasut569acef2023-01-26 21:01:56 +0100259 case CLK_TYPE_GEN4_MAIN:
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200260 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
261 0, 1, pll_config->extal_div,
262 "V3U_MAIN");
263
Marek Vasut569acef2023-01-26 21:01:56 +0100264 case CLK_TYPE_GEN4_PLL1:
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200265 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
266 0, pll_config->pll1_mult,
267 pll_config->pll1_div,
268 "V3U_PLL1");
269
Marek Vasut569acef2023-01-26 21:01:56 +0100270 case CLK_TYPE_GEN4_PLL2X_3X:
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200271 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
272 core->offset, 0, 0,
273 "V3U_PLL2X_3X");
274
Marek Vasut569acef2023-01-26 21:01:56 +0100275 case CLK_TYPE_GEN4_PLL5:
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200276 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
277 0, pll_config->pll5_mult,
278 pll_config->pll5_div,
279 "V3U_PLL5");
280
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200281 case CLK_TYPE_FF:
Marek Vasut8f567862021-04-27 19:36:39 +0200282 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, core,
283 0, core->mult, core->div,
284 "FIXED");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200285
Marek Vasut78414832019-03-04 21:38:10 +0100286 case CLK_TYPE_GEN3_MDSEL:
Marek Vasut69459b22018-05-31 19:47:42 +0200287 div = (core->div >> (priv->sscg ? 16 : 0)) & 0xffff;
288 rate = gen3_clk_get_rate64(&parent) / div;
289 debug("%s[%i] PE clk: parent=%i div=%u => rate=%llu\n",
290 __func__, __LINE__,
291 (core->parent >> (priv->sscg ? 16 : 0)) & 0xffff,
292 div, rate);
293 return rate;
294
Hai Pham0985e0e2023-01-26 21:01:49 +0100295 case CLK_TYPE_GEN3_SDH: /* Fixed factor 1:1 */
Marek Vasut569acef2023-01-26 21:01:56 +0100296 fallthrough;
297 case CLK_TYPE_GEN4_SDH: /* Fixed factor 1:1 */
Hai Pham0985e0e2023-01-26 21:01:49 +0100298 return gen3_clk_get_rate64(&parent);
299
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200300 case CLK_TYPE_GEN3_SD: /* FIXME */
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200301 fallthrough;
Marek Vasut569acef2023-01-26 21:01:56 +0100302 case CLK_TYPE_GEN4_SD:
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200303 value = readl(priv->base + core->offset);
304 value &= CPG_SD_STP_MASK | CPG_SD_FC_MASK;
305
306 for (i = 0; i < ARRAY_SIZE(cpg_sd_div_table); i++) {
307 if (cpg_sd_div_table[i].val != value)
308 continue;
309
Marek Vasut7571ac42018-05-31 19:06:02 +0200310 rate = gen3_clk_get_rate64(&parent) /
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200311 cpg_sd_div_table[i].div;
Marek Vasut7571ac42018-05-31 19:06:02 +0200312 debug("%s[%i] SD clk: parent=%i div=%i => rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200313 __func__, __LINE__,
314 core->parent, cpg_sd_div_table[i].div, rate);
315
316 return rate;
317 }
318
319 return -EINVAL;
Marek Vasutc1aee322017-09-15 21:10:29 +0200320
321 case CLK_TYPE_GEN3_RPC:
Hai Pham215de2b2020-08-11 10:25:28 +0700322 case CLK_TYPE_GEN3_RPCD2:
Marek Vasut569acef2023-01-26 21:01:56 +0100323 case CLK_TYPE_GEN4_RPC:
324 case CLK_TYPE_GEN4_RPCD2:
Marek Vasut7571ac42018-05-31 19:06:02 +0200325 rate = gen3_clk_get_rate64(&parent);
Marek Vasutc1aee322017-09-15 21:10:29 +0200326
Hai Phama80b0612023-01-26 21:02:04 +0100327 value = readl(priv->base + CPG_RPCCKCR);
Marek Vasutc1aee322017-09-15 21:10:29 +0200328
329 prediv = (value >> CPG_RPC_PREDIV_OFFSET) &
330 CPG_RPC_PREDIV_MASK;
331 if (prediv == 2)
332 rate /= 5;
333 else if (prediv == 3)
334 rate /= 6;
335 else
336 return -EINVAL;
337
338 postdiv = (value >> CPG_RPC_POSTDIV_OFFSET) &
339 CPG_RPC_POSTDIV_MASK;
Marek Vasutc1aee322017-09-15 21:10:29 +0200340
Hai Pham215de2b2020-08-11 10:25:28 +0700341 if (postdiv % 2 != 0) {
342 rate /= postdiv + 1;
343
344 if (core->type == CLK_TYPE_GEN3_RPCD2)
345 rate /= 2;
346
347 debug("%s[%i] RPC clk: parent=%i prediv=%i postdiv=%i => rate=%llu\n",
348 __func__, __LINE__,
349 core->parent, prediv, postdiv, rate);
350
351 return rate;
352 }
Marek Vasutc1aee322017-09-15 21:10:29 +0200353
Hai Pham215de2b2020-08-11 10:25:28 +0700354 return -EINVAL;
Marek Vasutc1aee322017-09-15 21:10:29 +0200355
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200356 }
357
358 printf("%s[%i] unknown fail\n", __func__, __LINE__);
359
360 return -ENOENT;
361}
362
Marek Vasut7571ac42018-05-31 19:06:02 +0200363static ulong gen3_clk_get_rate(struct clk *clk)
364{
365 return gen3_clk_get_rate64(clk);
366}
367
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200368static ulong gen3_clk_set_rate(struct clk *clk, ulong rate)
369{
Marek Vasut414dbbe2018-01-11 16:28:31 +0100370 /* Force correct SD-IF divider configuration if applicable */
Marek Vasutc26bf892018-10-30 17:54:20 +0100371 gen3_clk_setup_sdif_div(clk, rate);
Marek Vasut7571ac42018-05-31 19:06:02 +0200372 return gen3_clk_get_rate64(clk);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200373}
374
375static int gen3_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
376{
377 if (args->args_count != 2) {
Sean Andersona1b654b2021-12-01 14:26:53 -0500378 debug("Invalid args_count: %d\n", args->args_count);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200379 return -EINVAL;
380 }
381
382 clk->id = (args->args[0] << 16) | args->args[1];
383
384 return 0;
385}
386
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100387const struct clk_ops gen3_clk_ops = {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200388 .enable = gen3_clk_enable,
389 .disable = gen3_clk_disable,
390 .get_rate = gen3_clk_get_rate,
391 .set_rate = gen3_clk_set_rate,
392 .of_xlate = gen3_clk_of_xlate,
393};
394
Marek Vasutf6b32022023-01-26 21:02:03 +0100395static int gen3_clk_probe(struct udevice *dev)
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200396{
397 struct gen3_clk_priv *priv = dev_get_priv(dev);
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100398 struct cpg_mssr_info *info =
399 (struct cpg_mssr_info *)dev_get_driver_data(dev);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200400 fdt_addr_t rst_base;
401 u32 cpg_mode;
402 int ret;
403
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900404 priv->base = dev_read_addr_ptr(dev);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200405 if (!priv->base)
406 return -EINVAL;
407
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100408 priv->info = info;
409 ret = fdt_node_offset_by_compatible(gd->fdt_blob, -1, info->reset_node);
410 if (ret < 0)
411 return ret;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200412
413 rst_base = fdtdec_get_addr(gd->fdt_blob, ret, "reg");
414 if (rst_base == FDT_ADDR_T_NONE)
415 return -EINVAL;
416
Marek Vasut814217e2021-04-25 21:53:05 +0200417 cpg_mode = readl(rst_base + info->reset_modemr_offset);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200418
Marek Vasut28f90042018-01-16 19:23:17 +0100419 priv->cpg_pll_config =
420 (struct rcar_gen3_cpg_pll_config *)info->get_pll_config(cpg_mode);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200421 if (!priv->cpg_pll_config->extal_div)
422 return -EINVAL;
423
Marek Vasut69459b22018-05-31 19:47:42 +0200424 priv->sscg = !(cpg_mode & BIT(12));
425
Hai Pham94803462020-11-05 22:30:37 +0700426 if (info->reg_layout == CLK_REG_LAYOUT_RCAR_GEN2_AND_GEN3) {
427 priv->info->status_regs = mstpsr;
428 priv->info->control_regs = smstpcr;
429 priv->info->reset_regs = srcr;
430 priv->info->reset_clear_regs = srstclr;
Hai Pham86d59f32020-08-11 10:46:34 +0700431 } else if (info->reg_layout == CLK_REG_LAYOUT_RCAR_V3U) {
432 priv->info->status_regs = mstpsr_for_v3u;
433 priv->info->control_regs = mstpcr_for_v3u;
434 priv->info->reset_regs = srcr_for_v3u;
435 priv->info->reset_clear_regs = srstclr_for_v3u;
Hai Pham94803462020-11-05 22:30:37 +0700436 } else {
437 return -EINVAL;
438 }
439
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200440 ret = clk_get_by_name(dev, "extal", &priv->clk_extal);
441 if (ret < 0)
442 return ret;
443
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100444 if (info->extalr_node) {
445 ret = clk_get_by_name(dev, info->extalr_node, &priv->clk_extalr);
Marek Vasutfb0aa292017-10-08 21:09:15 +0200446 if (ret < 0)
447 return ret;
448 }
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200449
450 return 0;
451}
452
Marek Vasutf6b32022023-01-26 21:02:03 +0100453static int gen3_clk_remove(struct udevice *dev)
Marek Vasutdf6a1142017-11-25 22:08:55 +0100454{
455 struct gen3_clk_priv *priv = dev_get_priv(dev);
Marek Vasutdf6a1142017-11-25 22:08:55 +0100456
Marek Vasute11008b2018-01-15 16:44:39 +0100457 return renesas_clk_remove(priv->base, priv->info);
Marek Vasutdf6a1142017-11-25 22:08:55 +0100458}
Marek Vasutf6b32022023-01-26 21:02:03 +0100459
460U_BOOT_DRIVER(clk_gen3) = {
461 .name = "clk_gen3",
462 .id = UCLASS_CLK,
463 .priv_auto = sizeof(struct gen3_clk_priv),
464 .ops = &gen3_clk_ops,
465 .probe = gen3_clk_probe,
466 .remove = gen3_clk_remove,
467};
468
469static int gen3_reset_assert(struct reset_ctl *reset_ctl)
470{
471 struct udevice *cdev = (struct udevice *)dev_get_driver_data(reset_ctl->dev);
472 struct gen3_clk_priv *priv = dev_get_priv(cdev);
473 unsigned int reg = reset_ctl->id / 32;
474 unsigned int bit = reset_ctl->id % 32;
475 u32 bitmask = BIT(bit);
476
477 writel(bitmask, priv->base + priv->info->reset_regs[reg]);
478
479 return 0;
480}
481
482static int gen3_reset_deassert(struct reset_ctl *reset_ctl)
483{
484 struct udevice *cdev = (struct udevice *)dev_get_driver_data(reset_ctl->dev);
485 struct gen3_clk_priv *priv = dev_get_priv(cdev);
486 unsigned int reg = reset_ctl->id / 32;
487 unsigned int bit = reset_ctl->id % 32;
488 u32 bitmask = BIT(bit);
489
490 writel(bitmask, priv->base + priv->info->reset_clear_regs[reg]);
491
492 return 0;
493}
494
495static const struct reset_ops rst_gen3_ops = {
496 .rst_assert = gen3_reset_assert,
497 .rst_deassert = gen3_reset_deassert,
498};
499
500U_BOOT_DRIVER(rst_gen3) = {
501 .name = "rst_gen3",
502 .id = UCLASS_RESET,
503 .ops = &rst_gen3_ops,
504};
505
506int gen3_cpg_bind(struct udevice *parent)
507{
508 struct cpg_mssr_info *info =
509 (struct cpg_mssr_info *)dev_get_driver_data(parent);
510 struct udevice *cdev, *rdev;
511 struct driver *drv;
512 int ret;
513
514 drv = lists_driver_lookup_name("clk_gen3");
515 if (!drv)
516 return -ENOENT;
517
518 ret = device_bind_with_driver_data(parent, drv, "clk_gen3", (ulong)info,
519 dev_ofnode(parent), &cdev);
520 if (ret)
521 return ret;
522
523 drv = lists_driver_lookup_name("rst_gen3");
524 if (!drv)
525 return -ENOENT;
526
527 ret = device_bind_with_driver_data(parent, drv, "rst_gen3", (ulong)cdev,
528 dev_ofnode(parent), &rdev);
529 if (ret)
530 device_unbind(cdev);
531
532 return ret;
533}