blob: f8a23623223507d0ce3ab6ccccf619cd31fe231f [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"
Marek Vasutf3b8bf72017-07-21 23:18:03 +020032
Marek Vasutf3b8bf72017-07-21 23:18:03 +020033#define CPG_PLL0CR 0x00d8
34#define CPG_PLL2CR 0x002c
35#define CPG_PLL4CR 0x01f4
36
Hai Pham4dae0762023-01-29 02:50:22 +010037#define SDnSRCFC_SHIFT 2
38#define STPnHCK_TABLE (CPG_SDCKCR_STPnHCK >> SDnSRCFC_SHIFT)
Marek Vasutf3b8bf72017-07-21 23:18:03 +020039
Hai Pham4dae0762023-01-29 02:50:22 +010040/* Non-constant mask variant of FIELD_GET/FIELD_PREP */
41#define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1))
Marek Vasutf3b8bf72017-07-21 23:18:03 +020042
Hai Phame83700a2023-01-26 21:06:03 +010043static const struct clk_div_table cpg_rpcsrc_div_table[] = {
44 { 2, 5 }, { 3, 6 }, { 0, 0 },
45};
46
47static const struct clk_div_table cpg_rpc_div_table[] = {
48 { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 0, 0 },
49};
50
Hai Pham4dae0762023-01-29 02:50:22 +010051static const struct clk_div_table cpg_sdh_div_table[] = {
52 { 0, 1 }, { 1, 2 }, { STPnHCK_TABLE | 2, 4 }, { STPnHCK_TABLE | 3, 8 },
53 { STPnHCK_TABLE | 4, 16 }, { 0, 0 },
54};
55
56static const struct clk_div_table cpg_sd_div_table[] = {
57 { 0, 2 }, { 1, 4 }, { 0, 0 },
58};
59
Hai Pham6811b572023-01-26 21:06:06 +010060static const struct clk_div_table r8a77970_cpg_sd0h_div_table[] = {
61 { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 },
62 { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
63 { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 },
64};
65
66static const struct clk_div_table r8a77970_cpg_sd0_div_table[] = {
67 { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
68 { 8, 24 }, { 10, 36 }, { 11, 48 }, { 12, 10 },
69 { 0, 0 },
70};
71
Hai Phame83700a2023-01-26 21:06:03 +010072static unsigned int rcar_clk_get_table_div(const struct clk_div_table *table,
73 const u32 value)
74{
75 const struct clk_div_table *clkt;
76
77 for (clkt = table; clkt->div; clkt++)
78 if (clkt->val == value)
79 return clkt->div;
80 return 0;
81}
82
Hai Pham4dae0762023-01-29 02:50:22 +010083static int rcar_clk_get_table_val(const struct clk_div_table *table,
84 unsigned int div)
85{
86 const struct clk_div_table *clkt;
87
88 for (clkt = table; clkt->div; clkt++)
89 if (clkt->div == div)
90 return clkt->val;
91 return -EINVAL;
92}
93
Hai Phame83700a2023-01-26 21:06:03 +010094static __always_inline s64
95rcar_clk_get_rate64_div_table(unsigned int parent, u64 parent_rate,
96 void __iomem *reg, const u32 mask,
97 const struct clk_div_table *table, char *name)
98{
99 u32 value, div;
100 u64 rate;
101
102 value = field_get(mask, readl(reg));
103 div = rcar_clk_get_table_div(table, value);
104 if (!div)
105 return -EINVAL;
106
107 rate = parent_rate / div;
108 debug("%s[%i] %s clk: parent=%i div=%u => rate=%llu\n",
109 __func__, __LINE__, name, parent, div, rate);
110
111 return rate;
112}
113
Marek Vasut69459b22018-05-31 19:47:42 +0200114static int gen3_clk_get_parent(struct gen3_clk_priv *priv, struct clk *clk,
115 struct cpg_mssr_info *info, struct clk *parent)
116{
117 const struct cpg_core_clk *core;
118 int ret;
119
120 if (!renesas_clk_is_mod(clk)) {
121 ret = renesas_clk_get_core(clk, info, &core);
122 if (ret)
123 return ret;
124
Marek Vasut78414832019-03-04 21:38:10 +0100125 if (core->type == CLK_TYPE_GEN3_MDSEL) {
Marek Vasut69459b22018-05-31 19:47:42 +0200126 parent->dev = clk->dev;
127 parent->id = core->parent >> (priv->sscg ? 16 : 0);
128 parent->id &= 0xffff;
129 return 0;
130 }
131 }
132
133 return renesas_clk_get_parent(clk, info, parent);
134}
135
Hai Pham4dae0762023-01-29 02:50:22 +0100136static int gen3_clk_enable(struct clk *clk)
137{
138 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
139
140 return renesas_clk_endisable(clk, priv->base, priv->info, true);
141}
142
143static int gen3_clk_disable(struct clk *clk)
144{
145 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
146
147 return renesas_clk_endisable(clk, priv->base, priv->info, false);
148}
149
150static u64 gen3_clk_get_rate64(struct clk *clk);
151
Marek Vasutc26bf892018-10-30 17:54:20 +0100152static int gen3_clk_setup_sdif_div(struct clk *clk, ulong rate)
Marek Vasut5a51be52017-09-15 21:10:08 +0200153{
154 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
Marek Vasute11008b2018-01-15 16:44:39 +0100155 struct cpg_mssr_info *info = priv->info;
Marek Vasut5a51be52017-09-15 21:10:08 +0200156 const struct cpg_core_clk *core;
Hai Pham4dae0762023-01-29 02:50:22 +0100157 struct clk parent, grandparent;
Marek Vasut5a51be52017-09-15 21:10:08 +0200158 int ret;
Hai Pham4dae0762023-01-29 02:50:22 +0100159 u32 value = 0, div = 0;
Marek Vasut5a51be52017-09-15 21:10:08 +0200160
Hai Pham4dae0762023-01-29 02:50:22 +0100161 /*
162 * The clk may be either CPG_MOD or core clock, in case this is MOD
163 * clock, use core clock one level up, otherwise use the clock as-is.
164 * Note that parent clock here always represents core clock. Also note
165 * that grandparent clock are the parent clock of the core clock here.
166 */
167 if (renesas_clk_is_mod(clk)) {
168 ret = gen3_clk_get_parent(priv, clk, info, &parent);
169 if (ret) {
170 printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
171 return ret;
172 }
173 } else {
174 parent = *clk;
Marek Vasut5a51be52017-09-15 21:10:08 +0200175 }
176
Marek Vasute11008b2018-01-15 16:44:39 +0100177 if (renesas_clk_is_mod(&parent))
Marek Vasut5a51be52017-09-15 21:10:08 +0200178 return 0;
179
Marek Vasute11008b2018-01-15 16:44:39 +0100180 ret = renesas_clk_get_core(&parent, info, &core);
Marek Vasut5a51be52017-09-15 21:10:08 +0200181 if (ret)
182 return ret;
183
Hai Pham4dae0762023-01-29 02:50:22 +0100184 ret = renesas_clk_get_parent(&parent, info, &grandparent);
185 if (ret) {
186 printf("%s[%i] grandparent fail, ret=%i\n", __func__, __LINE__, ret);
187 return ret;
188 }
Marek Vasut5a51be52017-09-15 21:10:08 +0200189
Hai Pham4dae0762023-01-29 02:50:22 +0100190 switch (core->type) {
191 case CLK_TYPE_GEN3_SDH:
192 fallthrough;
193 case CLK_TYPE_GEN4_SDH:
194 div = DIV_ROUND_CLOSEST(gen3_clk_get_rate64(&grandparent), rate);
195 value = rcar_clk_get_table_val(cpg_sdh_div_table, div);
196 if (value < 0)
197 return value;
Marek Vasut5a51be52017-09-15 21:10:08 +0200198
Hai Pham4dae0762023-01-29 02:50:22 +0100199 clrsetbits_le32(priv->base + core->offset,
200 GENMASK(9, 2), value << 2);
Marek Vasut5a51be52017-09-15 21:10:08 +0200201
Hai Pham4dae0762023-01-29 02:50:22 +0100202 debug("%s[%i] SDH clk: parent=%i offset=%x div=%u rate=%lu => val=%u\n",
203 __func__, __LINE__, core->parent, core->offset, div, rate, value);
204 break;
Marek Vasut5a51be52017-09-15 21:10:08 +0200205
Hai Pham4dae0762023-01-29 02:50:22 +0100206 case CLK_TYPE_GEN3_SD:
207 fallthrough;
208 case CLK_TYPE_GEN4_SD:
209 div = DIV_ROUND_CLOSEST(gen3_clk_get_rate64(&grandparent), rate);
210 value = rcar_clk_get_table_val(cpg_sd_div_table, div);
211 if (value < 0)
212 return value;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200213
Hai Pham4dae0762023-01-29 02:50:22 +0100214 clrsetbits_le32(priv->base + core->offset,
215 GENMASK(1, 0), value);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200216
Hai Pham4dae0762023-01-29 02:50:22 +0100217 debug("%s[%i] SD clk: parent=%i offset=%x div=%u rate=%lu => val=%u\n",
218 __func__, __LINE__, core->parent, core->offset, div, rate, value);
219 break;
Hai Pham6811b572023-01-26 21:06:06 +0100220
221 case CLK_TYPE_R8A77970_SD0:
222 div = gen3_clk_get_rate64(&grandparent) / rate;
223 value = rcar_clk_get_table_val(cpg_sd_div_table, div);
224 if (!value)
225 return -EINVAL;
226
227 clrsetbits_le32(priv->base + core->offset,
228 GENMASK(7, 4), value << 4);
229
230 debug("%s[%i] SD clk: parent=%i offset=%x div=%u rate=%lu => val=%u\n",
231 __func__, __LINE__, core->parent, core->offset, div, rate, value);
232 break;
Hai Pham4dae0762023-01-29 02:50:22 +0100233 }
Marek Vasute11008b2018-01-15 16:44:39 +0100234
Hai Pham4dae0762023-01-29 02:50:22 +0100235 return 0;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200236}
237
Marek Vasut8f567862021-04-27 19:36:39 +0200238static u64 gen3_clk_get_rate64_pll_mul_reg(struct gen3_clk_priv *priv,
239 struct clk *parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200240 u32 mul_reg, u32 mult, u32 div,
241 char *name)
242{
243 u32 value;
244 u64 rate;
245
246 if (mul_reg) {
247 value = readl(priv->base + mul_reg);
248 mult = (((value >> 24) & 0x7f) + 1) * 2;
249 div = 1;
250 }
251
252 rate = (gen3_clk_get_rate64(parent) * mult) / div;
253
Marek Vasut1bd25212023-01-26 21:02:05 +0100254 debug("%s[%i] %s clk: mult=%u div=%u => rate=%llu\n",
255 __func__, __LINE__, name, mult, div, rate);
Marek Vasut8f567862021-04-27 19:36:39 +0200256 return rate;
257}
258
Marek Vasut7571ac42018-05-31 19:06:02 +0200259static u64 gen3_clk_get_rate64(struct clk *clk)
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200260{
261 struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
Marek Vasutb9234192018-01-08 16:05:28 +0100262 struct cpg_mssr_info *info = priv->info;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200263 struct clk parent;
264 const struct cpg_core_clk *core;
265 const struct rcar_gen3_cpg_pll_config *pll_config =
266 priv->cpg_pll_config;
Hai Phame83700a2023-01-26 21:06:03 +0100267 u32 value, div;
Marek Vasut7571ac42018-05-31 19:06:02 +0200268 u64 rate = 0;
Hai Pham4dae0762023-01-29 02:50:22 +0100269 int ret;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200270
271 debug("%s[%i] Clock: id=%lu\n", __func__, __LINE__, clk->id);
272
Marek Vasut69459b22018-05-31 19:47:42 +0200273 ret = gen3_clk_get_parent(priv, clk, info, &parent);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200274 if (ret) {
275 printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
276 return ret;
277 }
278
Marek Vasute11008b2018-01-15 16:44:39 +0100279 if (renesas_clk_is_mod(clk)) {
Marek Vasut7571ac42018-05-31 19:06:02 +0200280 rate = gen3_clk_get_rate64(&parent);
281 debug("%s[%i] MOD clk: parent=%lu => rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200282 __func__, __LINE__, parent.id, rate);
283 return rate;
284 }
285
Marek Vasute11008b2018-01-15 16:44:39 +0100286 ret = renesas_clk_get_core(clk, info, &core);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200287 if (ret)
288 return ret;
289
290 switch (core->type) {
291 case CLK_TYPE_IN:
Marek Vasutb9234192018-01-08 16:05:28 +0100292 if (core->id == info->clk_extal_id) {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200293 rate = clk_get_rate(&priv->clk_extal);
Marek Vasut7571ac42018-05-31 19:06:02 +0200294 debug("%s[%i] EXTAL clk: rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200295 __func__, __LINE__, rate);
296 return rate;
297 }
298
Marek Vasutb9234192018-01-08 16:05:28 +0100299 if (core->id == info->clk_extalr_id) {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200300 rate = clk_get_rate(&priv->clk_extalr);
Marek Vasut7571ac42018-05-31 19:06:02 +0200301 debug("%s[%i] EXTALR clk: rate=%llu\n",
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200302 __func__, __LINE__, rate);
303 return rate;
304 }
305
306 return -EINVAL;
307
308 case CLK_TYPE_GEN3_MAIN:
Marek Vasut1bd25212023-01-26 21:02:05 +0100309 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200310 0, 1, pll_config->extal_div,
311 "MAIN");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200312
313 case CLK_TYPE_GEN3_PLL0:
Marek Vasut1bd25212023-01-26 21:02:05 +0100314 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200315 CPG_PLL0CR, 0, 0, "PLL0");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200316
317 case CLK_TYPE_GEN3_PLL1:
Marek Vasut1bd25212023-01-26 21:02:05 +0100318 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200319 0, pll_config->pll1_mult,
320 pll_config->pll1_div, "PLL1");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200321
322 case CLK_TYPE_GEN3_PLL2:
Marek Vasut1bd25212023-01-26 21:02:05 +0100323 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200324 CPG_PLL2CR, 0, 0, "PLL2");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200325
326 case CLK_TYPE_GEN3_PLL3:
Marek Vasut1bd25212023-01-26 21:02:05 +0100327 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200328 0, pll_config->pll3_mult,
329 pll_config->pll3_div, "PLL3");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200330
331 case CLK_TYPE_GEN3_PLL4:
Marek Vasut1bd25212023-01-26 21:02:05 +0100332 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200333 CPG_PLL4CR, 0, 0, "PLL4");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200334
Marek Vasut569acef2023-01-26 21:01:56 +0100335 case CLK_TYPE_GEN4_MAIN:
Marek Vasut1bd25212023-01-26 21:02:05 +0100336 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200337 0, 1, pll_config->extal_div,
338 "V3U_MAIN");
339
Marek Vasut569acef2023-01-26 21:01:56 +0100340 case CLK_TYPE_GEN4_PLL1:
Marek Vasut1bd25212023-01-26 21:02:05 +0100341 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200342 0, pll_config->pll1_mult,
343 pll_config->pll1_div,
344 "V3U_PLL1");
345
Marek Vasut569acef2023-01-26 21:01:56 +0100346 case CLK_TYPE_GEN4_PLL2X_3X:
Marek Vasut1bd25212023-01-26 21:02:05 +0100347 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200348 core->offset, 0, 0,
349 "V3U_PLL2X_3X");
350
Marek Vasut569acef2023-01-26 21:01:56 +0100351 case CLK_TYPE_GEN4_PLL5:
Marek Vasut1bd25212023-01-26 21:02:05 +0100352 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200353 0, pll_config->pll5_mult,
354 pll_config->pll5_div,
355 "V3U_PLL5");
356
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200357 case CLK_TYPE_FF:
Marek Vasut1bd25212023-01-26 21:02:05 +0100358 return gen3_clk_get_rate64_pll_mul_reg(priv, &parent,
Marek Vasut8f567862021-04-27 19:36:39 +0200359 0, core->mult, core->div,
360 "FIXED");
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200361
Marek Vasut78414832019-03-04 21:38:10 +0100362 case CLK_TYPE_GEN3_MDSEL:
Marek Vasut69459b22018-05-31 19:47:42 +0200363 div = (core->div >> (priv->sscg ? 16 : 0)) & 0xffff;
364 rate = gen3_clk_get_rate64(&parent) / div;
365 debug("%s[%i] PE clk: parent=%i div=%u => rate=%llu\n",
366 __func__, __LINE__,
367 (core->parent >> (priv->sscg ? 16 : 0)) & 0xffff,
368 div, rate);
369 return rate;
370
Hai Pham0985e0e2023-01-26 21:01:49 +0100371 case CLK_TYPE_GEN3_SDH: /* Fixed factor 1:1 */
Marek Vasut569acef2023-01-26 21:01:56 +0100372 fallthrough;
373 case CLK_TYPE_GEN4_SDH: /* Fixed factor 1:1 */
Hai Pham4dae0762023-01-29 02:50:22 +0100374 /*
375 * This takes STPnHCK and STPnCK bits into consideration
376 * in the table look up too, hence the inobvious GENMASK
377 * below. Bits [7:5] always read zero, so this is OKish.
378 */
379 return rcar_clk_get_rate64_div_table(core->parent,
380 gen3_clk_get_rate64(&parent),
381 priv->base + core->offset,
382 CPG_SDCKCR_SRCFC_MASK |
383 GENMASK(9, 5),
384 cpg_sdh_div_table, "SDH");
Hai Pham0985e0e2023-01-26 21:01:49 +0100385
Hai Pham6811b572023-01-26 21:06:06 +0100386 case CLK_TYPE_R8A77970_SD0H:
387 return rcar_clk_get_rate64_div_table(core->parent,
388 gen3_clk_get_rate64(&parent),
389 priv->base + core->offset,
390 CPG_SDCKCR_SDHFC_MASK,
391 r8a77970_cpg_sd0h_div_table, "SDH");
392
Hai Pham4dae0762023-01-29 02:50:22 +0100393 case CLK_TYPE_GEN3_SD:
Marek Vasut0fbb8a72021-04-27 19:52:53 +0200394 fallthrough;
Marek Vasut569acef2023-01-26 21:01:56 +0100395 case CLK_TYPE_GEN4_SD:
Hai Pham4dae0762023-01-29 02:50:22 +0100396 return rcar_clk_get_rate64_div_table(core->parent,
397 gen3_clk_get_rate64(&parent),
398 priv->base + core->offset,
399 CPG_SDCKCR_FC_MASK,
400 cpg_sd_div_table, "SD");
Marek Vasutc1aee322017-09-15 21:10:29 +0200401
Hai Pham6811b572023-01-26 21:06:06 +0100402 case CLK_TYPE_R8A77970_SD0:
403 return rcar_clk_get_rate64_div_table(core->parent,
404 gen3_clk_get_rate64(&parent),
405 priv->base + core->offset,
406 CPG_SDCKCR_SD0FC_MASK,
407 r8a77970_cpg_sd0_div_table, "SD");
408
Hai Phame83700a2023-01-26 21:06:03 +0100409 case CLK_TYPE_GEN3_RPCSRC:
410 return rcar_clk_get_rate64_div_table(core->parent,
411 gen3_clk_get_rate64(&parent),
412 priv->base + CPG_RPCCKCR,
413 CPG_RPCCKCR_DIV_POST_MASK,
414 cpg_rpcsrc_div_table, "RPCSRC");
415
Hai Pham85e691e2023-01-26 21:06:04 +0100416 case CLK_TYPE_GEN3_D3_RPCSRC:
417 case CLK_TYPE_GEN3_E3_RPCSRC:
418 /*
419 * Register RPCSRC as fixed factor clock based on the
420 * MD[4:1] pins and CPG_RPCCKCR[4:3] register value for
421 * which has been set prior to booting the kernel.
422 */
423 value = (readl(priv->base + CPG_RPCCKCR) & GENMASK(4, 3)) >> 3;
424
425 switch (value) {
426 case 0:
427 div = 5;
428 break;
429 case 1:
430 div = 3;
431 break;
432 case 2:
433 div = core->div;
434 break;
435 case 3:
436 default:
437 div = 2;
438 break;
439 }
440
441 rate = gen3_clk_get_rate64(&parent) / div;
442 debug("%s[%i] E3/D3 RPCSRC clk: parent=%i div=%u => rate=%llu\n",
443 __func__, __LINE__, (core->parent >> 16) & 0xffff, div, rate);
444
445 return rate;
446
Marek Vasutc1aee322017-09-15 21:10:29 +0200447 case CLK_TYPE_GEN3_RPC:
Marek Vasut569acef2023-01-26 21:01:56 +0100448 case CLK_TYPE_GEN4_RPC:
Hai Phame83700a2023-01-26 21:06:03 +0100449 return rcar_clk_get_rate64_div_table(core->parent,
450 gen3_clk_get_rate64(&parent),
451 priv->base + CPG_RPCCKCR,
452 CPG_RPCCKCR_DIV_PRE_MASK,
453 cpg_rpc_div_table, "RPC");
Marek Vasutc1aee322017-09-15 21:10:29 +0200454
Hai Phame83700a2023-01-26 21:06:03 +0100455 case CLK_TYPE_GEN3_RPCD2:
456 case CLK_TYPE_GEN4_RPCD2:
457 rate = gen3_clk_get_rate64(&parent) / 2;
Hai Pham215de2b2020-08-11 10:25:28 +0700458
Hai Phame83700a2023-01-26 21:06:03 +0100459 debug("%s[%i] RPCD2 clk: parent=%i => rate=%llu\n",
460 __func__, __LINE__, core->parent, rate);
Marek Vasutc1aee322017-09-15 21:10:29 +0200461
Hai Phame83700a2023-01-26 21:06:03 +0100462 return rate;
Marek Vasutc1aee322017-09-15 21:10:29 +0200463
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200464 }
465
466 printf("%s[%i] unknown fail\n", __func__, __LINE__);
467
468 return -ENOENT;
469}
470
Marek Vasut7571ac42018-05-31 19:06:02 +0200471static ulong gen3_clk_get_rate(struct clk *clk)
472{
473 return gen3_clk_get_rate64(clk);
474}
475
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200476static ulong gen3_clk_set_rate(struct clk *clk, ulong rate)
477{
Marek Vasut414dbbe2018-01-11 16:28:31 +0100478 /* Force correct SD-IF divider configuration if applicable */
Marek Vasutc26bf892018-10-30 17:54:20 +0100479 gen3_clk_setup_sdif_div(clk, rate);
Marek Vasut7571ac42018-05-31 19:06:02 +0200480 return gen3_clk_get_rate64(clk);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200481}
482
483static int gen3_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
484{
485 if (args->args_count != 2) {
Sean Andersona1b654b2021-12-01 14:26:53 -0500486 debug("Invalid args_count: %d\n", args->args_count);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200487 return -EINVAL;
488 }
489
490 clk->id = (args->args[0] << 16) | args->args[1];
491
492 return 0;
493}
494
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100495const struct clk_ops gen3_clk_ops = {
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200496 .enable = gen3_clk_enable,
497 .disable = gen3_clk_disable,
498 .get_rate = gen3_clk_get_rate,
499 .set_rate = gen3_clk_set_rate,
500 .of_xlate = gen3_clk_of_xlate,
501};
502
Marek Vasutf6b32022023-01-26 21:02:03 +0100503static int gen3_clk_probe(struct udevice *dev)
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200504{
505 struct gen3_clk_priv *priv = dev_get_priv(dev);
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100506 struct cpg_mssr_info *info =
507 (struct cpg_mssr_info *)dev_get_driver_data(dev);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200508 fdt_addr_t rst_base;
509 u32 cpg_mode;
510 int ret;
511
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900512 priv->base = dev_read_addr_ptr(dev);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200513 if (!priv->base)
514 return -EINVAL;
515
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100516 priv->info = info;
517 ret = fdt_node_offset_by_compatible(gd->fdt_blob, -1, info->reset_node);
518 if (ret < 0)
519 return ret;
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200520
521 rst_base = fdtdec_get_addr(gd->fdt_blob, ret, "reg");
522 if (rst_base == FDT_ADDR_T_NONE)
523 return -EINVAL;
524
Marek Vasut814217e2021-04-25 21:53:05 +0200525 cpg_mode = readl(rst_base + info->reset_modemr_offset);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200526
Marek Vasut28f90042018-01-16 19:23:17 +0100527 priv->cpg_pll_config =
528 (struct rcar_gen3_cpg_pll_config *)info->get_pll_config(cpg_mode);
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200529 if (!priv->cpg_pll_config->extal_div)
530 return -EINVAL;
531
Marek Vasut69459b22018-05-31 19:47:42 +0200532 priv->sscg = !(cpg_mode & BIT(12));
533
Hai Pham94803462020-11-05 22:30:37 +0700534 if (info->reg_layout == CLK_REG_LAYOUT_RCAR_GEN2_AND_GEN3) {
535 priv->info->status_regs = mstpsr;
536 priv->info->control_regs = smstpcr;
537 priv->info->reset_regs = srcr;
538 priv->info->reset_clear_regs = srstclr;
Hai Pham86d59f32020-08-11 10:46:34 +0700539 } else if (info->reg_layout == CLK_REG_LAYOUT_RCAR_V3U) {
540 priv->info->status_regs = mstpsr_for_v3u;
541 priv->info->control_regs = mstpcr_for_v3u;
542 priv->info->reset_regs = srcr_for_v3u;
543 priv->info->reset_clear_regs = srstclr_for_v3u;
Hai Pham94803462020-11-05 22:30:37 +0700544 } else {
545 return -EINVAL;
546 }
547
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200548 ret = clk_get_by_name(dev, "extal", &priv->clk_extal);
549 if (ret < 0)
550 return ret;
551
Marek Vasut4eb4e6e2018-01-08 14:01:40 +0100552 if (info->extalr_node) {
553 ret = clk_get_by_name(dev, info->extalr_node, &priv->clk_extalr);
Marek Vasutfb0aa292017-10-08 21:09:15 +0200554 if (ret < 0)
555 return ret;
556 }
Marek Vasutf3b8bf72017-07-21 23:18:03 +0200557
558 return 0;
559}
560
Marek Vasutf6b32022023-01-26 21:02:03 +0100561static int gen3_clk_remove(struct udevice *dev)
Marek Vasutdf6a1142017-11-25 22:08:55 +0100562{
563 struct gen3_clk_priv *priv = dev_get_priv(dev);
Marek Vasutdf6a1142017-11-25 22:08:55 +0100564
Marek Vasute11008b2018-01-15 16:44:39 +0100565 return renesas_clk_remove(priv->base, priv->info);
Marek Vasutdf6a1142017-11-25 22:08:55 +0100566}
Marek Vasutf6b32022023-01-26 21:02:03 +0100567
568U_BOOT_DRIVER(clk_gen3) = {
569 .name = "clk_gen3",
570 .id = UCLASS_CLK,
571 .priv_auto = sizeof(struct gen3_clk_priv),
572 .ops = &gen3_clk_ops,
573 .probe = gen3_clk_probe,
574 .remove = gen3_clk_remove,
575};
576
577static int gen3_reset_assert(struct reset_ctl *reset_ctl)
578{
579 struct udevice *cdev = (struct udevice *)dev_get_driver_data(reset_ctl->dev);
580 struct gen3_clk_priv *priv = dev_get_priv(cdev);
581 unsigned int reg = reset_ctl->id / 32;
582 unsigned int bit = reset_ctl->id % 32;
583 u32 bitmask = BIT(bit);
584
585 writel(bitmask, priv->base + priv->info->reset_regs[reg]);
586
587 return 0;
588}
589
590static int gen3_reset_deassert(struct reset_ctl *reset_ctl)
591{
592 struct udevice *cdev = (struct udevice *)dev_get_driver_data(reset_ctl->dev);
593 struct gen3_clk_priv *priv = dev_get_priv(cdev);
594 unsigned int reg = reset_ctl->id / 32;
595 unsigned int bit = reset_ctl->id % 32;
596 u32 bitmask = BIT(bit);
597
598 writel(bitmask, priv->base + priv->info->reset_clear_regs[reg]);
599
600 return 0;
601}
602
603static const struct reset_ops rst_gen3_ops = {
604 .rst_assert = gen3_reset_assert,
605 .rst_deassert = gen3_reset_deassert,
606};
607
608U_BOOT_DRIVER(rst_gen3) = {
609 .name = "rst_gen3",
610 .id = UCLASS_RESET,
611 .ops = &rst_gen3_ops,
612};
613
614int gen3_cpg_bind(struct udevice *parent)
615{
616 struct cpg_mssr_info *info =
617 (struct cpg_mssr_info *)dev_get_driver_data(parent);
618 struct udevice *cdev, *rdev;
619 struct driver *drv;
620 int ret;
621
622 drv = lists_driver_lookup_name("clk_gen3");
623 if (!drv)
624 return -ENOENT;
625
626 ret = device_bind_with_driver_data(parent, drv, "clk_gen3", (ulong)info,
627 dev_ofnode(parent), &cdev);
628 if (ret)
629 return ret;
630
631 drv = lists_driver_lookup_name("rst_gen3");
632 if (!drv)
633 return -ENOENT;
634
635 ret = device_bind_with_driver_data(parent, drv, "rst_gen3", (ulong)cdev,
636 dev_ofnode(parent), &rdev);
637 if (ret)
638 device_unbind(cdev);
639
640 return ret;
641}