blob: c8a5512b65ea4799455d7dbe0ac29d24655b31e7 [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
Marek Vasutba2c7d22023-02-28 22:34:38 +010038#define SD0CKCR1 0x08a4
39
40static const struct clk_div_table gen3_cpg_rpcsrc_div_table[] = {
Hai Phame83700a2023-01-26 21:06:03 +010041 { 2, 5 }, { 3, 6 }, { 0, 0 },
42};
43
Marek Vasutba2c7d22023-02-28 22:34:38 +010044static const struct clk_div_table gen4_cpg_rpcsrc_div_table[] = {
45 { 0, 4 }, { 1, 6 }, { 2, 5 }, { 3, 6 }, { 0, 0 },
46};
47
Hai Pham6811b572023-01-26 21:06:06 +010048static const struct clk_div_table r8a77970_cpg_sd0h_div_table[] = {
49 { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 },
50 { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
51 { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 },
52};
53
54static const struct clk_div_table r8a77970_cpg_sd0_div_table[] = {
55 { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
56 { 8, 24 }, { 10, 36 }, { 11, 48 }, { 12, 10 },
57 { 0, 0 },
58};
59
Marek Vasut69459b22018-05-31 19:47:42 +020060static int gen3_clk_get_parent(struct gen3_clk_priv *priv, struct clk *clk,
61 struct cpg_mssr_info *info, struct clk *parent)
62{
63 const struct cpg_core_clk *core;
Marek Vasutea8505e2023-02-28 07:25:11 +010064 u8 shift;
Marek Vasut69459b22018-05-31 19:47:42 +020065 int ret;
66
67 if (!renesas_clk_is_mod(clk)) {
68 ret = renesas_clk_get_core(clk, info, &core);
69 if (ret)
70 return ret;
71
Marek Vasut78414832019-03-04 21:38:10 +010072 if (core->type == CLK_TYPE_GEN3_MDSEL) {
Marek Vasutea8505e2023-02-28 07:25:11 +010073 shift = priv->cpg_mode & BIT(core->offset) ? 16 : 0;
Marek Vasut69459b22018-05-31 19:47:42 +020074 parent->dev = clk->dev;
Marek Vasutea8505e2023-02-28 07:25:11 +010075 parent->id = core->parent >> shift;
Marek Vasut69459b22018-05-31 19:47:42 +020076 parent->id &= 0xffff;
77 return 0;
78 }
79 }
80
81 return renesas_clk_get_parent(clk, info, parent);
82}
83
Hai Pham4dae0762023-01-29 02:50:22 +010084static int gen3_clk_enable(struct clk *clk)
85{
86 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
87
88 return renesas_clk_endisable(clk, priv->base, priv->info, true);
89}
90
91static int gen3_clk_disable(struct clk *clk)
92{
93 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
94
95 return renesas_clk_endisable(clk, priv->base, priv->info, false);
96}
97
98static u64 gen3_clk_get_rate64(struct clk *clk);
99
Marek Vasutc26bf892018-10-30 17:54:20 +0100100static int gen3_clk_setup_sdif_div(struct clk *clk, ulong rate)
Marek Vasut5a51be52017-09-15 21:10:08 +0200101{
102 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
Marek Vasute11008b2018-01-15 16:44:39 +0100103 struct cpg_mssr_info *info = priv->info;
Marek Vasut5a51be52017-09-15 21:10:08 +0200104 const struct cpg_core_clk *core;
Hai Pham4dae0762023-01-29 02:50:22 +0100105 struct clk parent, grandparent;
Marek Vasut5a51be52017-09-15 21:10:08 +0200106 int ret;
107
Hai Pham4dae0762023-01-29 02:50:22 +0100108 /*
109 * The clk may be either CPG_MOD or core clock, in case this is MOD
110 * clock, use core clock one level up, otherwise use the clock as-is.
111 * Note that parent clock here always represents core clock. Also note
112 * that grandparent clock are the parent clock of the core clock here.
113 */
114 if (renesas_clk_is_mod(clk)) {
115 ret = gen3_clk_get_parent(priv, clk, info, &parent);
116 if (ret) {
117 printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
118 return ret;
119 }
120 } else {
121 parent = *clk;
Marek Vasut5a51be52017-09-15 21:10:08 +0200122 }
123
Marek Vasute11008b2018-01-15 16:44:39 +0100124 if (renesas_clk_is_mod(&parent))
Marek Vasut5a51be52017-09-15 21:10:08 +0200125 return 0;
126
Marek Vasute11008b2018-01-15 16:44:39 +0100127 ret = renesas_clk_get_core(&parent, info, &core);
Marek Vasut5a51be52017-09-15 21:10:08 +0200128 if (ret)
129 return ret;
130
Hai Pham4dae0762023-01-29 02:50:22 +0100131 ret = renesas_clk_get_parent(&parent, info, &grandparent);
132 if (ret) {
133 printf("%s[%i] grandparent fail, ret=%i\n", __func__, __LINE__, ret);
134 return ret;
135 }
Marek Vasut5a51be52017-09-15 21:10:08 +0200136
Hai Pham4dae0762023-01-29 02:50:22 +0100137 switch (core->type) {
138 case CLK_TYPE_GEN3_SDH:
139 fallthrough;
140 case CLK_TYPE_GEN4_SDH:
Hai Pham06d8f972023-01-26 21:06:07 +0100141 return rcar_clk_set_rate64_sdh(core->parent,
142 gen3_clk_get_rate64(&grandparent),
143 rate, priv->base + core->offset);
Marek Vasut5a51be52017-09-15 21:10:08 +0200144
Hai Pham4dae0762023-01-29 02:50:22 +0100145 case CLK_TYPE_GEN3_SD:
146 fallthrough;
147 case CLK_TYPE_GEN4_SD:
Hai Pham06d8f972023-01-26 21:06:07 +0100148 return rcar_clk_set_rate64_sd(core->parent,
149 gen3_clk_get_rate64(&grandparent),
150 rate, priv->base + core->offset);
Hai Pham6811b572023-01-26 21:06:06 +0100151
152 case CLK_TYPE_R8A77970_SD0:
Hai Pham06d8f972023-01-26 21:06:07 +0100153 return rcar_clk_set_rate64_div_table(core->parent,
154 gen3_clk_get_rate64(&grandparent),
155 rate, priv->base + core->offset,
156 CPG_SDCKCR_SD0FC_MASK,
157 r8a77970_cpg_sd0_div_table, "SD");
Hai Pham4dae0762023-01-29 02:50:22 +0100158 }
Marek Vasute11008b2018-01-15 16:44:39 +0100159
Hai Pham4dae0762023-01-29 02:50:22 +0100160 return 0;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200161}
162
Marek Vasut8f567862021-04-27 19:36:39 +0200163static u64 gen3_clk_get_rate64_pll_mul_reg(struct gen3_clk_priv *priv,
164 struct clk *parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200165 u32 mul_reg, u32 mult, u32 div,
166 char *name)
167{
168 u32 value;
169 u64 rate;
170
171 if (mul_reg) {
172 value = readl(priv->base + mul_reg);
173 mult = (((value >> 24) & 0x7f) + 1) * 2;
174 div = 1;
175 }
176
177 rate = (gen3_clk_get_rate64(parent) * mult) / div;
178
Marek Vasut1bd25212023-01-26 21:02:05 +0100179 debug("%s[%i] %s clk: mult=%u div=%u => rate=%llu\n",
180 __func__, __LINE__, name, mult, div, rate);
Marek Vasut8f567862021-04-27 19:36:39 +0200181 return rate;
182}
183
Marek Vasut7571ac42018-05-31 19:06:02 +0200184static u64 gen3_clk_get_rate64(struct clk *clk)
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200185{
186 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
Marek Vasutb9234192018-01-08 16:05:28 +0100187 struct cpg_mssr_info *info = priv->info;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200188 struct clk parent;
189 const struct cpg_core_clk *core;
Marek Vasutba2c7d22023-02-28 22:34:38 +0100190 const struct rcar_gen3_cpg_pll_config *gen3_pll_config =
191 priv->gen3_cpg_pll_config;
192 const struct rcar_gen4_cpg_pll_config *gen4_pll_config =
193 priv->gen4_cpg_pll_config;
Hai Phame83700a2023-01-26 21:06:03 +0100194 u32 value, div;
Marek Vasut7571ac42018-05-31 19:06:02 +0200195 u64 rate = 0;
Marek Vasutea8505e2023-02-28 07:25:11 +0100196 u8 shift;
Hai Pham4dae0762023-01-29 02:50:22 +0100197 int ret;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200198
199 debug("%s[%i] Clock: id=%lu\n", __func__, __LINE__, clk->id);
200
Marek Vasut69459b22018-05-31 19:47:42 +0200201 ret = gen3_clk_get_parent(priv, clk, info, &parent);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200202 if (ret) {
203 printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
204 return ret;
205 }
206
Marek Vasute11008b2018-01-15 16:44:39 +0100207 if (renesas_clk_is_mod(clk)) {
Marek Vasut7571ac42018-05-31 19:06:02 +0200208 rate = gen3_clk_get_rate64(&parent);
209 debug("%s[%i] MOD clk: parent=%lu => rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200210 __func__, __LINE__, parent.id, rate);
211 return rate;
212 }
213
Marek Vasute11008b2018-01-15 16:44:39 +0100214 ret = renesas_clk_get_core(clk, info, &core);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200215 if (ret)
216 return ret;
217
218 switch (core->type) {
219 case CLK_TYPE_IN:
Marek Vasutb9234192018-01-08 16:05:28 +0100220 if (core->id == info->clk_extal_id) {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200221 rate = clk_get_rate(&priv->clk_extal);
Marek Vasut7571ac42018-05-31 19:06:02 +0200222 debug("%s[%i] EXTAL clk: rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200223 __func__, __LINE__, rate);
224 return rate;
225 }
226
Marek Vasutb9234192018-01-08 16:05:28 +0100227 if (core->id == info->clk_extalr_id) {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200228 rate = clk_get_rate(&priv->clk_extalr);
Marek Vasut7571ac42018-05-31 19:06:02 +0200229 debug("%s[%i] EXTALR clk: rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200230 __func__, __LINE__, rate);
231 return rate;
232 }
233
234 return -EINVAL;
235
236 case CLK_TYPE_GEN3_MAIN:
Marek Vasut1bd25212023-01-26 21:02:05 +0100237 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasutba2c7d22023-02-28 22:34:38 +0100238 0, 1, gen3_pll_config->extal_div,
Marek Vasut8f567862021-04-27 19:36:39 +0200239 "MAIN");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200240
241 case CLK_TYPE_GEN3_PLL0:
Marek Vasut1bd25212023-01-26 21:02:05 +0100242 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200243 CPG_PLL0CR, 0, 0, "PLL0");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200244
245 case CLK_TYPE_GEN3_PLL1:
Marek Vasut1bd25212023-01-26 21:02:05 +0100246 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasutba2c7d22023-02-28 22:34:38 +0100247 0, gen3_pll_config->pll1_mult,
248 gen3_pll_config->pll1_div,
249 "PLL1");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200250
251 case CLK_TYPE_GEN3_PLL2:
Marek Vasut1bd25212023-01-26 21:02:05 +0100252 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200253 CPG_PLL2CR, 0, 0, "PLL2");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200254
255 case CLK_TYPE_GEN3_PLL3:
Marek Vasut1bd25212023-01-26 21:02:05 +0100256 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasutba2c7d22023-02-28 22:34:38 +0100257 0, gen3_pll_config->pll3_mult,
258 gen3_pll_config->pll3_div,
259 "PLL3");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200260
261 case CLK_TYPE_GEN3_PLL4:
Marek Vasut1bd25212023-01-26 21:02:05 +0100262 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200263 CPG_PLL4CR, 0, 0, "PLL4");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200264
Marek Vasut569acef2023-01-26 21:01:56 +0100265 case CLK_TYPE_GEN4_MAIN:
Marek Vasut1bd25212023-01-26 21:02:05 +0100266 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasutba2c7d22023-02-28 22:34:38 +0100267 0, 1, gen4_pll_config->extal_div,
268 "MAIN");
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200269
Marek Vasut569acef2023-01-26 21:01:56 +0100270 case CLK_TYPE_GEN4_PLL1:
Marek Vasut1bd25212023-01-26 21:02:05 +0100271 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasutba2c7d22023-02-28 22:34:38 +0100272 0, gen4_pll_config->pll1_mult,
273 gen4_pll_config->pll1_div,
274 "PLL1");
275
276 case CLK_TYPE_GEN4_PLL2:
277 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
278 0, gen4_pll_config->pll2_mult,
279 gen4_pll_config->pll2_div,
280 "PLL2");
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200281
Marek Vasut569acef2023-01-26 21:01:56 +0100282 case CLK_TYPE_GEN4_PLL2X_3X:
Marek Vasut1bd25212023-01-26 21:02:05 +0100283 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasutba2c7d22023-02-28 22:34:38 +0100284 core->offset, 0, 0, "PLL2X_3X");
285
286 case CLK_TYPE_GEN4_PLL3:
287 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
288 0, gen4_pll_config->pll3_mult,
289 gen4_pll_config->pll3_div,
290 "PLL3");
291
292 case CLK_TYPE_GEN4_PLL4:
293 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
294 0, gen4_pll_config->pll4_mult,
295 gen4_pll_config->pll4_div,
296 "PLL4");
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200297
Marek Vasut569acef2023-01-26 21:01:56 +0100298 case CLK_TYPE_GEN4_PLL5:
Marek Vasut1bd25212023-01-26 21:02:05 +0100299 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasutba2c7d22023-02-28 22:34:38 +0100300 0, gen4_pll_config->pll5_mult,
301 gen4_pll_config->pll5_div,
302 "PLL5");
303
304 case CLK_TYPE_GEN4_PLL6:
305 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
306 0, gen4_pll_config->pll6_mult,
307 gen4_pll_config->pll6_div,
308 "PLL6");
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200309
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200310 case CLK_TYPE_FF:
Marek Vasut1bd25212023-01-26 21:02:05 +0100311 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200312 0, core->mult, core->div,
313 "FIXED");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200314
Marek Vasut78414832019-03-04 21:38:10 +0100315 case CLK_TYPE_GEN3_MDSEL:
Marek Vasutea8505e2023-02-28 07:25:11 +0100316 shift = priv->cpg_mode & BIT(core->offset) ? 16 : 0;
317 div = (core->div >> shift) & 0xffff;
Marek Vasut69459b22018-05-31 19:47:42 +0200318 rate = gen3_clk_get_rate64(&parent) / div;
319 debug("%s[%i] PE clk: parent=%i div=%u => rate=%llu\n",
Marek Vasutea8505e2023-02-28 07:25:11 +0100320 __func__, __LINE__, (core->parent >> shift) & 0xffff,
Marek Vasut69459b22018-05-31 19:47:42 +0200321 div, rate);
322 return rate;
323
Marek Vasutba2c7d22023-02-28 22:34:38 +0100324 case CLK_TYPE_GEN4_SDSRC:
325 div = ((readl(priv->base + SD0CKCR1) >> 29) & 0x03) + 4;
326 rate = gen3_clk_get_rate64(&parent) / div;
327 debug("%s[%i] SDSRC clk: parent=%i div=%u => rate=%llu\n",
328 __func__, __LINE__, core->parent, div, rate);
329 return rate;
330
Hai Pham0985e0e2023-01-26 21:01:49 +0100331 case CLK_TYPE_GEN3_SDH: /* Fixed factor 1:1 */
Marek Vasut569acef2023-01-26 21:01:56 +0100332 fallthrough;
333 case CLK_TYPE_GEN4_SDH: /* Fixed factor 1:1 */
Hai Pham06d8f972023-01-26 21:06:07 +0100334 return rcar_clk_get_rate64_sdh(core->parent,
335 gen3_clk_get_rate64(&parent),
336 priv->base + core->offset);
Hai Pham0985e0e2023-01-26 21:01:49 +0100337
Hai Pham6811b572023-01-26 21:06:06 +0100338 case CLK_TYPE_R8A77970_SD0H:
339 return rcar_clk_get_rate64_div_table(core->parent,
340 gen3_clk_get_rate64(&parent),
341 priv->base + core->offset,
342 CPG_SDCKCR_SDHFC_MASK,
343 r8a77970_cpg_sd0h_div_table, "SDH");
344
Hai Pham4dae0762023-01-29 02:50:22 +0100345 case CLK_TYPE_GEN3_SD:
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200346 fallthrough;
Marek Vasut569acef2023-01-26 21:01:56 +0100347 case CLK_TYPE_GEN4_SD:
Hai Pham06d8f972023-01-26 21:06:07 +0100348 return rcar_clk_get_rate64_sd(core->parent,
349 gen3_clk_get_rate64(&parent),
350 priv->base + core->offset);
Marek Vasutc1aee322017-09-15 21:10:29 +0200351
Hai Pham6811b572023-01-26 21:06:06 +0100352 case CLK_TYPE_R8A77970_SD0:
353 return rcar_clk_get_rate64_div_table(core->parent,
354 gen3_clk_get_rate64(&parent),
355 priv->base + core->offset,
356 CPG_SDCKCR_SD0FC_MASK,
357 r8a77970_cpg_sd0_div_table, "SD");
358
Hai Phame83700a2023-01-26 21:06:03 +0100359 case CLK_TYPE_GEN3_RPCSRC:
360 return rcar_clk_get_rate64_div_table(core->parent,
361 gen3_clk_get_rate64(&parent),
362 priv->base + CPG_RPCCKCR,
363 CPG_RPCCKCR_DIV_POST_MASK,
Marek Vasutba2c7d22023-02-28 22:34:38 +0100364 gen3_cpg_rpcsrc_div_table,
365 "RPCSRC");
366
367 case CLK_TYPE_GEN4_RPCSRC:
368 return rcar_clk_get_rate64_div_table(core->parent,
369 gen3_clk_get_rate64(&parent),
370 priv->base + CPG_RPCCKCR,
371 CPG_RPCCKCR_DIV_POST_MASK,
372 gen4_cpg_rpcsrc_div_table,
373 "RPCSRC");
Hai Phame83700a2023-01-26 21:06:03 +0100374
Hai Pham85e691e2023-01-26 21:06:04 +0100375 case CLK_TYPE_GEN3_D3_RPCSRC:
376 case CLK_TYPE_GEN3_E3_RPCSRC:
377 /*
378 * Register RPCSRC as fixed factor clock based on the
379 * MD[4:1] pins and CPG_RPCCKCR[4:3] register value for
380 * which has been set prior to booting the kernel.
381 */
382 value = (readl(priv->base + CPG_RPCCKCR) & GENMASK(4, 3)) >> 3;
383
384 switch (value) {
385 case 0:
386 div = 5;
387 break;
388 case 1:
389 div = 3;
390 break;
391 case 2:
392 div = core->div;
393 break;
394 case 3:
395 default:
396 div = 2;
397 break;
398 }
399
400 rate = gen3_clk_get_rate64(&parent) / div;
401 debug("%s[%i] E3/D3 RPCSRC clk: parent=%i div=%u => rate=%llu\n",
402 __func__, __LINE__, (core->parent >> 16) & 0xffff, div, rate);
403
404 return rate;
405
Marek Vasutc1aee322017-09-15 21:10:29 +0200406 case CLK_TYPE_GEN3_RPC:
Marek Vasut569acef2023-01-26 21:01:56 +0100407 case CLK_TYPE_GEN4_RPC:
Hai Pham06d8f972023-01-26 21:06:07 +0100408 return rcar_clk_get_rate64_rpc(core->parent,
409 gen3_clk_get_rate64(&parent),
410 priv->base + CPG_RPCCKCR);
Marek Vasutc1aee322017-09-15 21:10:29 +0200411
Hai Phame83700a2023-01-26 21:06:03 +0100412 case CLK_TYPE_GEN3_RPCD2:
413 case CLK_TYPE_GEN4_RPCD2:
Hai Pham06d8f972023-01-26 21:06:07 +0100414 return rcar_clk_get_rate64_rpcd2(core->parent,
415 gen3_clk_get_rate64(&parent));
Marek Vasutc1aee322017-09-15 21:10:29 +0200416
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200417 }
418
419 printf("%s[%i] unknown fail\n", __func__, __LINE__);
420
421 return -ENOENT;
422}
423
Marek Vasut7571ac42018-05-31 19:06:02 +0200424static ulong gen3_clk_get_rate(struct clk *clk)
425{
426 return gen3_clk_get_rate64(clk);
427}
428
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200429static ulong gen3_clk_set_rate(struct clk *clk, ulong rate)
430{
Marek Vasut414dbbe2018-01-11 16:28:31 +0100431 /* Force correct SD-IF divider configuration if applicable */
Marek Vasutc26bf892018-10-30 17:54:20 +0100432 gen3_clk_setup_sdif_div(clk, rate);
Marek Vasut7571ac42018-05-31 19:06:02 +0200433 return gen3_clk_get_rate64(clk);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200434}
435
436static int gen3_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
437{
438 if (args->args_count != 2) {
Sean Andersona1b654b2021-12-01 14:26:53 -0500439 debug("Invalid args_count: %d\n", args->args_count);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200440 return -EINVAL;
441 }
442
443 clk->id = (args->args[0] << 16) | args->args[1];
444
445 return 0;
446}
447
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100448const struct clk_ops gen3_clk_ops = {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200449 .enable = gen3_clk_enable,
450 .disable = gen3_clk_disable,
451 .get_rate = gen3_clk_get_rate,
452 .set_rate = gen3_clk_set_rate,
453 .of_xlate = gen3_clk_of_xlate,
454};
455
Marek Vasutf6b32022023-01-26 21:02:03 +0100456static int gen3_clk_probe(struct udevice *dev)
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200457{
458 struct gen3_clk_priv *priv = dev_get_priv(dev);
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100459 struct cpg_mssr_info *info =
460 (struct cpg_mssr_info *)dev_get_driver_data(dev);
Marek Vasutba2c7d22023-02-28 22:34:38 +0100461 const void *pll_config;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200462 fdt_addr_t rst_base;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200463 int ret;
464
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900465 priv->base = dev_read_addr_ptr(dev);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200466 if (!priv->base)
467 return -EINVAL;
468
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100469 priv->info = info;
470 ret = fdt_node_offset_by_compatible(gd->fdt_blob, -1, info->reset_node);
471 if (ret < 0)
472 return ret;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200473
474 rst_base = fdtdec_get_addr(gd->fdt_blob, ret, "reg");
475 if (rst_base == FDT_ADDR_T_NONE)
476 return -EINVAL;
477
Marek Vasutea8505e2023-02-28 07:25:11 +0100478 priv->cpg_mode = readl(rst_base + info->reset_modemr_offset);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200479
Marek Vasutba2c7d22023-02-28 22:34:38 +0100480 pll_config = info->get_pll_config(priv->cpg_mode);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200481
Hai Pham94803462020-11-05 22:30:37 +0700482 if (info->reg_layout == CLK_REG_LAYOUT_RCAR_GEN2_AND_GEN3) {
483 priv->info->status_regs = mstpsr;
484 priv->info->control_regs = smstpcr;
485 priv->info->reset_regs = srcr;
486 priv->info->reset_clear_regs = srstclr;
Marek Vasutba2c7d22023-02-28 22:34:38 +0100487 priv->gen3_cpg_pll_config = pll_config;
488 if (!priv->gen3_cpg_pll_config->extal_div)
489 return -EINVAL;
490 } else if (info->reg_layout == CLK_REG_LAYOUT_RCAR_GEN4) {
491 priv->info->status_regs = mstpsr_for_gen4;
492 priv->info->control_regs = mstpcr_for_gen4;
493 priv->info->reset_regs = srcr_for_gen4;
494 priv->info->reset_clear_regs = srstclr_for_gen4;
495 priv->gen4_cpg_pll_config = pll_config;
496 if (!priv->gen4_cpg_pll_config->extal_div)
497 return -EINVAL;
Hai Pham94803462020-11-05 22:30:37 +0700498 } else {
499 return -EINVAL;
500 }
501
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200502 ret = clk_get_by_name(dev, "extal", &priv->clk_extal);
503 if (ret < 0)
504 return ret;
505
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100506 if (info->extalr_node) {
507 ret = clk_get_by_name(dev, info->extalr_node, &priv->clk_extalr);
Marek Vasutfb0aa292017-10-08 21:09:15 +0200508 if (ret < 0)
509 return ret;
510 }
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200511
512 return 0;
513}
514
Marek Vasutf6b32022023-01-26 21:02:03 +0100515static int gen3_clk_remove(struct udevice *dev)
Marek Vasutdf6a1142017-11-25 22:08:55 +0100516{
517 struct gen3_clk_priv *priv = dev_get_priv(dev);
Marek Vasutdf6a1142017-11-25 22:08:55 +0100518
Marek Vasute11008b2018-01-15 16:44:39 +0100519 return renesas_clk_remove(priv->base, priv->info);
Marek Vasutdf6a1142017-11-25 22:08:55 +0100520}
Marek Vasutf6b32022023-01-26 21:02:03 +0100521
522U_BOOT_DRIVER(clk_gen3) = {
523 .name = "clk_gen3",
524 .id = UCLASS_CLK,
525 .priv_auto = sizeof(struct gen3_clk_priv),
526 .ops = &gen3_clk_ops,
527 .probe = gen3_clk_probe,
528 .remove = gen3_clk_remove,
529};
530
531static int gen3_reset_assert(struct reset_ctl *reset_ctl)
532{
533 struct udevice *cdev = (struct udevice *)dev_get_driver_data(reset_ctl->dev);
534 struct gen3_clk_priv *priv = dev_get_priv(cdev);
Marek Vasutef5c0b12023-03-18 12:30:53 +0100535 unsigned int packed_id = MOD_CLK_PACK(reset_ctl->id);
536 unsigned int reg = packed_id / 32;
537 unsigned int bit = packed_id % 32;
Marek Vasutf6b32022023-01-26 21:02:03 +0100538 u32 bitmask = BIT(bit);
539
540 writel(bitmask, priv->base + priv->info->reset_regs[reg]);
541
542 return 0;
543}
544
545static int gen3_reset_deassert(struct reset_ctl *reset_ctl)
546{
547 struct udevice *cdev = (struct udevice *)dev_get_driver_data(reset_ctl->dev);
548 struct gen3_clk_priv *priv = dev_get_priv(cdev);
Marek Vasutef5c0b12023-03-18 12:30:53 +0100549 unsigned int packed_id = MOD_CLK_PACK(reset_ctl->id);
550 unsigned int reg = packed_id / 32;
551 unsigned int bit = packed_id % 32;
Marek Vasutf6b32022023-01-26 21:02:03 +0100552 u32 bitmask = BIT(bit);
553
554 writel(bitmask, priv->base + priv->info->reset_clear_regs[reg]);
555
556 return 0;
557}
558
559static const struct reset_ops rst_gen3_ops = {
560 .rst_assert = gen3_reset_assert,
561 .rst_deassert = gen3_reset_deassert,
562};
563
564U_BOOT_DRIVER(rst_gen3) = {
565 .name = "rst_gen3",
566 .id = UCLASS_RESET,
567 .ops = &rst_gen3_ops,
568};
569
570int gen3_cpg_bind(struct udevice *parent)
571{
572 struct cpg_mssr_info *info =
573 (struct cpg_mssr_info *)dev_get_driver_data(parent);
574 struct udevice *cdev, *rdev;
575 struct driver *drv;
576 int ret;
577
578 drv = lists_driver_lookup_name("clk_gen3");
579 if (!drv)
580 return -ENOENT;
581
582 ret = device_bind_with_driver_data(parent, drv, "clk_gen3", (ulong)info,
583 dev_ofnode(parent), &cdev);
584 if (ret)
585 return ret;
586
587 drv = lists_driver_lookup_name("rst_gen3");
588 if (!drv)
589 return -ENOENT;
590
591 ret = device_bind_with_driver_data(parent, drv, "rst_gen3", (ulong)cdev,
592 dev_ofnode(parent), &rdev);
593 if (ret)
594 device_unbind(cdev);
595
596 return ret;
597}