blob: 850d64111904da8d6cf14e419689d997eded03ac [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutf63b2952018-01-08 16:38:51 +01002/*
3 * Renesas RCar Gen2 CPG MSSR driver
4 *
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 Vasutf63b2952018-01-08 16:38:51 +010011 */
12
13#include <common.h>
14#include <clk-uclass.h>
15#include <dm.h>
16#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Marek Vasutf63b2952018-01-08 16:38:51 +010019#include <asm/io.h>
Marek Vasutb2970fd2023-01-26 21:06:02 +010020#include <linux/clk-provider.h>
Marek Vasutf63b2952018-01-08 16:38:51 +010021
22#include <dt-bindings/clock/renesas-cpg-mssr.h>
23
24#include "renesas-cpg-mssr.h"
25#include "rcar-gen2-cpg.h"
26
Marek Vasutf63b2952018-01-08 16:38:51 +010027#define CPG_PLL0CR 0x00d8
28#define CPG_SDCKCR 0x0074
29
Marek Vasutf63b2952018-01-08 16:38:51 +010030/* SDHI divisors */
31static const struct clk_div_table cpg_sdh_div_table[] = {
32 { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 },
33 { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
34 { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 },
35};
36
37static const struct clk_div_table cpg_sd01_div_table[] = {
38 { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
39 { 8, 24 }, { 10, 36 }, { 11, 48 }, { 12, 10 },
40 { 0, 0 },
41};
42
Marek Vasut272daa72019-03-18 05:11:42 +010043static u8 gen2_clk_get_sdh_div(const struct clk_div_table *table, u8 val)
Marek Vasutf63b2952018-01-08 16:38:51 +010044{
Marek Vasut272daa72019-03-18 05:11:42 +010045 for (;;) {
46 if (!(*table).div)
47 return 0xff;
48
49 if ((*table).val == val)
50 return (*table).div;
51
52 table++;
Marek Vasutf63b2952018-01-08 16:38:51 +010053 }
Marek Vasutf63b2952018-01-08 16:38:51 +010054}
55
56static int gen2_clk_enable(struct clk *clk)
57{
58 struct gen2_clk_priv *priv = dev_get_priv(clk->dev);
59
Hai Pham5460ee02020-05-22 10:39:04 +070060 return renesas_clk_endisable(clk, priv->base, priv->info, true);
Marek Vasutf63b2952018-01-08 16:38:51 +010061}
62
63static int gen2_clk_disable(struct clk *clk)
64{
65 struct gen2_clk_priv *priv = dev_get_priv(clk->dev);
66
Hai Pham5460ee02020-05-22 10:39:04 +070067 return renesas_clk_endisable(clk, priv->base, priv->info, false);
Marek Vasutf63b2952018-01-08 16:38:51 +010068}
69
70static ulong gen2_clk_get_rate(struct clk *clk)
71{
72 struct gen2_clk_priv *priv = dev_get_priv(clk->dev);
73 struct cpg_mssr_info *info = priv->info;
74 struct clk parent;
75 const struct cpg_core_clk *core;
76 const struct rcar_gen2_cpg_pll_config *pll_config =
77 priv->cpg_pll_config;
78 u32 value, mult, div, rate = 0;
79 int ret;
80
81 debug("%s[%i] Clock: id=%lu\n", __func__, __LINE__, clk->id);
82
83 ret = renesas_clk_get_parent(clk, info, &parent);
84 if (ret) {
85 printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
86 return ret;
87 }
88
89 if (renesas_clk_is_mod(clk)) {
90 rate = gen2_clk_get_rate(&parent);
91 debug("%s[%i] MOD clk: parent=%lu => rate=%u\n",
92 __func__, __LINE__, parent.id, rate);
93 return rate;
94 }
95
96 ret = renesas_clk_get_core(clk, info, &core);
97 if (ret)
98 return ret;
99
100 switch (core->type) {
101 case CLK_TYPE_IN:
102 if (core->id == info->clk_extal_id) {
103 rate = clk_get_rate(&priv->clk_extal);
104 debug("%s[%i] EXTAL clk: rate=%u\n",
105 __func__, __LINE__, rate);
106 return rate;
107 }
108
109 if (core->id == info->clk_extal_usb_id) {
110 rate = clk_get_rate(&priv->clk_extal_usb);
111 debug("%s[%i] EXTALR clk: rate=%u\n",
112 __func__, __LINE__, rate);
113 return rate;
114 }
115
116 return -EINVAL;
117
118 case CLK_TYPE_FF:
119 rate = (gen2_clk_get_rate(&parent) * core->mult) / core->div;
Marek Vasut31872db2019-03-18 05:38:08 +0100120 debug("%s[%i] FIXED clk: parent=%i mul=%i div=%i => rate=%u\n",
Marek Vasutf63b2952018-01-08 16:38:51 +0100121 __func__, __LINE__,
122 core->parent, core->mult, core->div, rate);
123 return rate;
124
125 case CLK_TYPE_DIV6P1: /* DIV6 Clock with 1 parent clock */
126 value = (readl(priv->base + core->offset) & 0x3f) + 1;
127 rate = gen2_clk_get_rate(&parent) / value;
128 debug("%s[%i] DIV6P1 clk: parent=%i div=%i => rate=%u\n",
129 __func__, __LINE__,
130 core->parent, value, rate);
131 return rate;
132
133 case CLK_TYPE_GEN2_MAIN:
134 rate = gen2_clk_get_rate(&parent) / pll_config->extal_div;
135 debug("%s[%i] MAIN clk: parent=%i extal_div=%i => rate=%u\n",
136 __func__, __LINE__,
137 core->parent, pll_config->extal_div, rate);
138 return rate;
139
140 case CLK_TYPE_GEN2_PLL0:
141 /*
142 * PLL0 is a configurable multiplier clock except on R-Car
143 * V2H/E2. Register the PLL0 clock as a fixed factor clock for
144 * now as there's no generic multiplier clock implementation and
145 * we currently have no need to change the multiplier value.
146 */
147 mult = pll_config->pll0_mult;
148 if (!mult) {
149 value = readl(priv->base + CPG_PLL0CR);
150 mult = (((value >> 24) & 0x7f) + 1) * 2;
151 }
152
153 rate = (gen2_clk_get_rate(&parent) * mult) / info->pll0_div;
154 debug("%s[%i] PLL0 clk: parent=%i mult=%u => rate=%u\n",
155 __func__, __LINE__, core->parent, mult, rate);
156 return rate;
157
158 case CLK_TYPE_GEN2_PLL1:
159 rate = (gen2_clk_get_rate(&parent) * pll_config->pll1_mult) / 2;
160 debug("%s[%i] PLL1 clk: parent=%i mul=%i => rate=%u\n",
161 __func__, __LINE__,
162 core->parent, pll_config->pll1_mult, rate);
163 return rate;
164
165 case CLK_TYPE_GEN2_PLL3:
166 rate = gen2_clk_get_rate(&parent) * pll_config->pll3_mult;
167 debug("%s[%i] PLL3 clk: parent=%i mul=%i => rate=%u\n",
168 __func__, __LINE__,
169 core->parent, pll_config->pll3_mult, rate);
170 return rate;
171
172 case CLK_TYPE_GEN2_SDH:
173 value = (readl(priv->base + CPG_SDCKCR) >> 8) & 0xf;
174 div = gen2_clk_get_sdh_div(cpg_sdh_div_table, value);
175 rate = gen2_clk_get_rate(&parent) / div;
176 debug("%s[%i] SDH clk: parent=%i div=%i => rate=%u\n",
177 __func__, __LINE__,
178 core->parent, div, rate);
179 return rate;
180
181 case CLK_TYPE_GEN2_SD0:
182 value = (readl(priv->base + CPG_SDCKCR) >> 4) & 0xf;
183 div = gen2_clk_get_sdh_div(cpg_sd01_div_table, value);
184 rate = gen2_clk_get_rate(&parent) / div;
185 debug("%s[%i] SD0 clk: parent=%i div=%i => rate=%u\n",
186 __func__, __LINE__,
187 core->parent, div, rate);
188 return rate;
189
190 case CLK_TYPE_GEN2_SD1:
191 value = (readl(priv->base + CPG_SDCKCR) >> 0) & 0xf;
192 div = gen2_clk_get_sdh_div(cpg_sd01_div_table, value);
193 rate = gen2_clk_get_rate(&parent) / div;
194 debug("%s[%i] SD1 clk: parent=%i div=%i => rate=%u\n",
195 __func__, __LINE__,
196 core->parent, div, rate);
197 return rate;
198 }
199
200 printf("%s[%i] unknown fail\n", __func__, __LINE__);
201
202 return -ENOENT;
203}
204
Marek Vasut0f6aa072019-03-18 06:04:02 +0100205static int gen2_clk_setup_mmcif_div(struct clk *clk, ulong rate)
206{
207 struct gen2_clk_priv *priv = dev_get_priv(clk->dev);
208 struct cpg_mssr_info *info = priv->info;
209 const struct cpg_core_clk *core;
210 struct clk parent, pparent;
211 u32 val;
212 int ret;
213
214 ret = renesas_clk_get_parent(clk, info, &parent);
215 if (ret) {
216 debug("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
217 return ret;
218 }
219
220 if (renesas_clk_is_mod(&parent))
221 return 0;
222
223 ret = renesas_clk_get_core(&parent, info, &core);
224 if (ret)
225 return ret;
226
227 if (strcmp(core->name, "mmc0") && strcmp(core->name, "mmc1"))
228 return 0;
229
230 ret = renesas_clk_get_parent(&parent, info, &pparent);
231 if (ret) {
232 debug("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
233 return ret;
234 }
235
236 val = (gen2_clk_get_rate(&pparent) / rate) - 1;
237
238 debug("%s[%i] MMCIF offset=%x\n", __func__, __LINE__, core->offset);
239
240 writel(val, priv->base + core->offset);
241
242 return 0;
243}
244
Marek Vasutf63b2952018-01-08 16:38:51 +0100245static ulong gen2_clk_set_rate(struct clk *clk, ulong rate)
246{
Marek Vasut0f6aa072019-03-18 06:04:02 +0100247 /* Force correct MMC-IF divider configuration if applicable */
248 gen2_clk_setup_mmcif_div(clk, rate);
Marek Vasutf63b2952018-01-08 16:38:51 +0100249 return gen2_clk_get_rate(clk);
250}
251
252static int gen2_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
253{
254 if (args->args_count != 2) {
Sean Andersona1b654b2021-12-01 14:26:53 -0500255 debug("Invalid args_count: %d\n", args->args_count);
Marek Vasutf63b2952018-01-08 16:38:51 +0100256 return -EINVAL;
257 }
258
259 clk->id = (args->args[0] << 16) | args->args[1];
260
261 return 0;
262}
263
264const struct clk_ops gen2_clk_ops = {
265 .enable = gen2_clk_enable,
266 .disable = gen2_clk_disable,
267 .get_rate = gen2_clk_get_rate,
268 .set_rate = gen2_clk_set_rate,
269 .of_xlate = gen2_clk_of_xlate,
270};
271
272int gen2_clk_probe(struct udevice *dev)
273{
274 struct gen2_clk_priv *priv = dev_get_priv(dev);
275 struct cpg_mssr_info *info =
276 (struct cpg_mssr_info *)dev_get_driver_data(dev);
277 fdt_addr_t rst_base;
278 u32 cpg_mode;
279 int ret;
280
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900281 priv->base = dev_read_addr_ptr(dev);
Marek Vasutf63b2952018-01-08 16:38:51 +0100282 if (!priv->base)
283 return -EINVAL;
284
285 priv->info = info;
286 ret = fdt_node_offset_by_compatible(gd->fdt_blob, -1, info->reset_node);
287 if (ret < 0)
288 return ret;
289
Marek Vasutab118762020-03-21 16:45:29 +0100290 rst_base = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, ret, "reg",
291 0, NULL, false);
Marek Vasutf63b2952018-01-08 16:38:51 +0100292 if (rst_base == FDT_ADDR_T_NONE)
293 return -EINVAL;
294
295 cpg_mode = readl(rst_base + CPG_RST_MODEMR);
296
297 priv->cpg_pll_config =
298 (struct rcar_gen2_cpg_pll_config *)info->get_pll_config(cpg_mode);
299 if (!priv->cpg_pll_config->extal_div)
300 return -EINVAL;
301
302 ret = clk_get_by_name(dev, "extal", &priv->clk_extal);
303 if (ret < 0)
304 return ret;
305
306 if (info->extal_usb_node) {
307 ret = clk_get_by_name(dev, info->extal_usb_node,
308 &priv->clk_extal_usb);
309 if (ret < 0)
310 return ret;
311 }
312
313 return 0;
314}
315
316int gen2_clk_remove(struct udevice *dev)
317{
318 struct gen2_clk_priv *priv = dev_get_priv(dev);
319
320 return renesas_clk_remove(priv->base, priv->info);
321}