183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
236c2ee4cSMarek Vasut /*
37691ff2aSMarek Vasut * Renesas RCar Gen3 CPG MSSR driver
436c2ee4cSMarek Vasut *
536c2ee4cSMarek Vasut * Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
636c2ee4cSMarek Vasut *
736c2ee4cSMarek Vasut * Based on the following driver from Linux kernel:
836c2ee4cSMarek Vasut * r8a7796 Clock Pulse Generator / Module Standby and Software Reset
936c2ee4cSMarek Vasut *
1036c2ee4cSMarek Vasut * Copyright (C) 2016 Glider bvba
1136c2ee4cSMarek Vasut */
1236c2ee4cSMarek Vasut
1336c2ee4cSMarek Vasut #include <common.h>
1436c2ee4cSMarek Vasut #include <clk-uclass.h>
1536c2ee4cSMarek Vasut #include <dm.h>
1636c2ee4cSMarek Vasut #include <errno.h>
1736c2ee4cSMarek Vasut #include <wait_bit.h>
1836c2ee4cSMarek Vasut #include <asm/io.h>
1936c2ee4cSMarek Vasut
20f77b5a4cSMarek Vasut #include <dt-bindings/clock/renesas-cpg-mssr.h>
21f77b5a4cSMarek Vasut
22f77b5a4cSMarek Vasut #include "renesas-cpg-mssr.h"
23d2628671SMarek Vasut #include "rcar-gen3-cpg.h"
2436c2ee4cSMarek Vasut
2536c2ee4cSMarek Vasut #define CPG_RST_MODEMR 0x0060
2636c2ee4cSMarek Vasut
2736c2ee4cSMarek Vasut #define CPG_PLL0CR 0x00d8
2836c2ee4cSMarek Vasut #define CPG_PLL2CR 0x002c
2936c2ee4cSMarek Vasut #define CPG_PLL4CR 0x01f4
3036c2ee4cSMarek Vasut
31849ab0a6SMarek Vasut #define CPG_RPC_PREDIV_MASK 0x3
32849ab0a6SMarek Vasut #define CPG_RPC_PREDIV_OFFSET 3
33849ab0a6SMarek Vasut #define CPG_RPC_POSTDIV_MASK 0x7
34849ab0a6SMarek Vasut #define CPG_RPC_POSTDIV_OFFSET 0
35849ab0a6SMarek Vasut
3636c2ee4cSMarek Vasut /*
3736c2ee4cSMarek Vasut * SDn Clock
3836c2ee4cSMarek Vasut */
3936c2ee4cSMarek Vasut #define CPG_SD_STP_HCK BIT(9)
4036c2ee4cSMarek Vasut #define CPG_SD_STP_CK BIT(8)
4136c2ee4cSMarek Vasut
4236c2ee4cSMarek Vasut #define CPG_SD_STP_MASK (CPG_SD_STP_HCK | CPG_SD_STP_CK)
4336c2ee4cSMarek Vasut #define CPG_SD_FC_MASK (0x7 << 2 | 0x3 << 0)
4436c2ee4cSMarek Vasut
4536c2ee4cSMarek Vasut #define CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) \
4636c2ee4cSMarek Vasut { \
4736c2ee4cSMarek Vasut .val = ((stp_hck) ? CPG_SD_STP_HCK : 0) | \
4836c2ee4cSMarek Vasut ((stp_ck) ? CPG_SD_STP_CK : 0) | \
4936c2ee4cSMarek Vasut ((sd_srcfc) << 2) | \
5036c2ee4cSMarek Vasut ((sd_fc) << 0), \
5136c2ee4cSMarek Vasut .div = (sd_div), \
5236c2ee4cSMarek Vasut }
5336c2ee4cSMarek Vasut
5436c2ee4cSMarek Vasut struct sd_div_table {
5536c2ee4cSMarek Vasut u32 val;
5636c2ee4cSMarek Vasut unsigned int div;
5736c2ee4cSMarek Vasut };
5836c2ee4cSMarek Vasut
5936c2ee4cSMarek Vasut /* SDn divider
6036c2ee4cSMarek Vasut * sd_srcfc sd_fc div
6136c2ee4cSMarek Vasut * stp_hck stp_ck (div) (div) = sd_srcfc x sd_fc
6236c2ee4cSMarek Vasut *-------------------------------------------------------------------
6336c2ee4cSMarek Vasut * 0 0 0 (1) 1 (4) 4
6436c2ee4cSMarek Vasut * 0 0 1 (2) 1 (4) 8
6536c2ee4cSMarek Vasut * 1 0 2 (4) 1 (4) 16
6636c2ee4cSMarek Vasut * 1 0 3 (8) 1 (4) 32
6736c2ee4cSMarek Vasut * 1 0 4 (16) 1 (4) 64
6836c2ee4cSMarek Vasut * 0 0 0 (1) 0 (2) 2
6936c2ee4cSMarek Vasut * 0 0 1 (2) 0 (2) 4
7036c2ee4cSMarek Vasut * 1 0 2 (4) 0 (2) 8
7136c2ee4cSMarek Vasut * 1 0 3 (8) 0 (2) 16
7236c2ee4cSMarek Vasut * 1 0 4 (16) 0 (2) 32
7336c2ee4cSMarek Vasut */
7436c2ee4cSMarek Vasut static const struct sd_div_table cpg_sd_div_table[] = {
7536c2ee4cSMarek Vasut /* CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) */
7636c2ee4cSMarek Vasut CPG_SD_DIV_TABLE_DATA(0, 0, 0, 1, 4),
7736c2ee4cSMarek Vasut CPG_SD_DIV_TABLE_DATA(0, 0, 1, 1, 8),
7836c2ee4cSMarek Vasut CPG_SD_DIV_TABLE_DATA(1, 0, 2, 1, 16),
7936c2ee4cSMarek Vasut CPG_SD_DIV_TABLE_DATA(1, 0, 3, 1, 32),
8036c2ee4cSMarek Vasut CPG_SD_DIV_TABLE_DATA(1, 0, 4, 1, 64),
8136c2ee4cSMarek Vasut CPG_SD_DIV_TABLE_DATA(0, 0, 0, 0, 2),
8236c2ee4cSMarek Vasut CPG_SD_DIV_TABLE_DATA(0, 0, 1, 0, 4),
8336c2ee4cSMarek Vasut CPG_SD_DIV_TABLE_DATA(1, 0, 2, 0, 8),
8436c2ee4cSMarek Vasut CPG_SD_DIV_TABLE_DATA(1, 0, 3, 0, 16),
8536c2ee4cSMarek Vasut CPG_SD_DIV_TABLE_DATA(1, 0, 4, 0, 32),
8636c2ee4cSMarek Vasut };
8736c2ee4cSMarek Vasut
gen3_clk_get_parent(struct gen3_clk_priv * priv,struct clk * clk,struct cpg_mssr_info * info,struct clk * parent)88716d7752SMarek Vasut static int gen3_clk_get_parent(struct gen3_clk_priv *priv, struct clk *clk,
89716d7752SMarek Vasut struct cpg_mssr_info *info, struct clk *parent)
90716d7752SMarek Vasut {
91716d7752SMarek Vasut const struct cpg_core_clk *core;
92716d7752SMarek Vasut int ret;
93716d7752SMarek Vasut
94716d7752SMarek Vasut if (!renesas_clk_is_mod(clk)) {
95716d7752SMarek Vasut ret = renesas_clk_get_core(clk, info, &core);
96716d7752SMarek Vasut if (ret)
97716d7752SMarek Vasut return ret;
98716d7752SMarek Vasut
99716d7752SMarek Vasut if (core->type == CLK_TYPE_GEN3_PE) {
100716d7752SMarek Vasut parent->dev = clk->dev;
101716d7752SMarek Vasut parent->id = core->parent >> (priv->sscg ? 16 : 0);
102716d7752SMarek Vasut parent->id &= 0xffff;
103716d7752SMarek Vasut return 0;
104716d7752SMarek Vasut }
105716d7752SMarek Vasut }
106716d7752SMarek Vasut
107716d7752SMarek Vasut return renesas_clk_get_parent(clk, info, parent);
108716d7752SMarek Vasut }
109716d7752SMarek Vasut
gen3_clk_setup_sdif_div(struct clk * clk,ulong rate)110*f58d6771SMarek Vasut static int gen3_clk_setup_sdif_div(struct clk *clk, ulong rate)
1114b20eef3SMarek Vasut {
1124b20eef3SMarek Vasut struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
113d2628671SMarek Vasut struct cpg_mssr_info *info = priv->info;
1144b20eef3SMarek Vasut const struct cpg_core_clk *core;
1154b20eef3SMarek Vasut struct clk parent;
1164b20eef3SMarek Vasut int ret;
1174b20eef3SMarek Vasut
118716d7752SMarek Vasut ret = gen3_clk_get_parent(priv, clk, info, &parent);
1194b20eef3SMarek Vasut if (ret) {
1204b20eef3SMarek Vasut printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
1214b20eef3SMarek Vasut return ret;
1224b20eef3SMarek Vasut }
1234b20eef3SMarek Vasut
124d2628671SMarek Vasut if (renesas_clk_is_mod(&parent))
1254b20eef3SMarek Vasut return 0;
1264b20eef3SMarek Vasut
127d2628671SMarek Vasut ret = renesas_clk_get_core(&parent, info, &core);
1284b20eef3SMarek Vasut if (ret)
1294b20eef3SMarek Vasut return ret;
1304b20eef3SMarek Vasut
1314b20eef3SMarek Vasut if (core->type != CLK_TYPE_GEN3_SD)
1324b20eef3SMarek Vasut return 0;
1334b20eef3SMarek Vasut
1344b20eef3SMarek Vasut debug("%s[%i] SDIF offset=%x\n", __func__, __LINE__, core->offset);
1354b20eef3SMarek Vasut
136*f58d6771SMarek Vasut writel((rate == 400000000) ? 0x4 : 0x1, priv->base + core->offset);
1374b20eef3SMarek Vasut
1384b20eef3SMarek Vasut return 0;
1394b20eef3SMarek Vasut }
1404b20eef3SMarek Vasut
gen3_clk_enable(struct clk * clk)14136c2ee4cSMarek Vasut static int gen3_clk_enable(struct clk *clk)
14236c2ee4cSMarek Vasut {
143d2628671SMarek Vasut struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
144d2628671SMarek Vasut
145d2628671SMarek Vasut return renesas_clk_endisable(clk, priv->base, true);
14636c2ee4cSMarek Vasut }
14736c2ee4cSMarek Vasut
gen3_clk_disable(struct clk * clk)14836c2ee4cSMarek Vasut static int gen3_clk_disable(struct clk *clk)
14936c2ee4cSMarek Vasut {
150d2628671SMarek Vasut struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
151d2628671SMarek Vasut
152d2628671SMarek Vasut return renesas_clk_endisable(clk, priv->base, false);
15336c2ee4cSMarek Vasut }
15436c2ee4cSMarek Vasut
gen3_clk_get_rate64(struct clk * clk)1558376e0e6SMarek Vasut static u64 gen3_clk_get_rate64(struct clk *clk)
15636c2ee4cSMarek Vasut {
15736c2ee4cSMarek Vasut struct gen3_clk_priv *priv = dev_get_priv(clk->dev);
158f11c9679SMarek Vasut struct cpg_mssr_info *info = priv->info;
15936c2ee4cSMarek Vasut struct clk parent;
16036c2ee4cSMarek Vasut const struct cpg_core_clk *core;
16136c2ee4cSMarek Vasut const struct rcar_gen3_cpg_pll_config *pll_config =
16236c2ee4cSMarek Vasut priv->cpg_pll_config;
163716d7752SMarek Vasut u32 value, mult, div, prediv, postdiv;
1648376e0e6SMarek Vasut u64 rate = 0;
16536c2ee4cSMarek Vasut int i, ret;
16636c2ee4cSMarek Vasut
16736c2ee4cSMarek Vasut debug("%s[%i] Clock: id=%lu\n", __func__, __LINE__, clk->id);
16836c2ee4cSMarek Vasut
169716d7752SMarek Vasut ret = gen3_clk_get_parent(priv, clk, info, &parent);
17036c2ee4cSMarek Vasut if (ret) {
17136c2ee4cSMarek Vasut printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret);
17236c2ee4cSMarek Vasut return ret;
17336c2ee4cSMarek Vasut }
17436c2ee4cSMarek Vasut
175d2628671SMarek Vasut if (renesas_clk_is_mod(clk)) {
1768376e0e6SMarek Vasut rate = gen3_clk_get_rate64(&parent);
1778376e0e6SMarek Vasut debug("%s[%i] MOD clk: parent=%lu => rate=%llu\n",
17836c2ee4cSMarek Vasut __func__, __LINE__, parent.id, rate);
17936c2ee4cSMarek Vasut return rate;
18036c2ee4cSMarek Vasut }
18136c2ee4cSMarek Vasut
182d2628671SMarek Vasut ret = renesas_clk_get_core(clk, info, &core);
18336c2ee4cSMarek Vasut if (ret)
18436c2ee4cSMarek Vasut return ret;
18536c2ee4cSMarek Vasut
18636c2ee4cSMarek Vasut switch (core->type) {
18736c2ee4cSMarek Vasut case CLK_TYPE_IN:
188f11c9679SMarek Vasut if (core->id == info->clk_extal_id) {
18936c2ee4cSMarek Vasut rate = clk_get_rate(&priv->clk_extal);
1908376e0e6SMarek Vasut debug("%s[%i] EXTAL clk: rate=%llu\n",
19136c2ee4cSMarek Vasut __func__, __LINE__, rate);
19236c2ee4cSMarek Vasut return rate;
19336c2ee4cSMarek Vasut }
19436c2ee4cSMarek Vasut
195f11c9679SMarek Vasut if (core->id == info->clk_extalr_id) {
19636c2ee4cSMarek Vasut rate = clk_get_rate(&priv->clk_extalr);
1978376e0e6SMarek Vasut debug("%s[%i] EXTALR clk: rate=%llu\n",
19836c2ee4cSMarek Vasut __func__, __LINE__, rate);
19936c2ee4cSMarek Vasut return rate;
20036c2ee4cSMarek Vasut }
20136c2ee4cSMarek Vasut
20236c2ee4cSMarek Vasut return -EINVAL;
20336c2ee4cSMarek Vasut
20436c2ee4cSMarek Vasut case CLK_TYPE_GEN3_MAIN:
2058376e0e6SMarek Vasut rate = gen3_clk_get_rate64(&parent) / pll_config->extal_div;
2068376e0e6SMarek Vasut debug("%s[%i] MAIN clk: parent=%i extal_div=%i => rate=%llu\n",
20736c2ee4cSMarek Vasut __func__, __LINE__,
20836c2ee4cSMarek Vasut core->parent, pll_config->extal_div, rate);
20936c2ee4cSMarek Vasut return rate;
21036c2ee4cSMarek Vasut
21136c2ee4cSMarek Vasut case CLK_TYPE_GEN3_PLL0:
21236c2ee4cSMarek Vasut value = readl(priv->base + CPG_PLL0CR);
21336c2ee4cSMarek Vasut mult = (((value >> 24) & 0x7f) + 1) * 2;
2148376e0e6SMarek Vasut rate = gen3_clk_get_rate64(&parent) * mult;
2158376e0e6SMarek Vasut debug("%s[%i] PLL0 clk: parent=%i mult=%u => rate=%llu\n",
21636c2ee4cSMarek Vasut __func__, __LINE__, core->parent, mult, rate);
21736c2ee4cSMarek Vasut return rate;
21836c2ee4cSMarek Vasut
21936c2ee4cSMarek Vasut case CLK_TYPE_GEN3_PLL1:
2208376e0e6SMarek Vasut rate = gen3_clk_get_rate64(&parent) * pll_config->pll1_mult;
221f0f1de75SMarek Vasut rate /= pll_config->pll1_div;
222f0f1de75SMarek Vasut debug("%s[%i] PLL1 clk: parent=%i mul=%i div=%i => rate=%llu\n",
22336c2ee4cSMarek Vasut __func__, __LINE__,
224f0f1de75SMarek Vasut core->parent, pll_config->pll1_mult,
225f0f1de75SMarek Vasut pll_config->pll1_div, rate);
22636c2ee4cSMarek Vasut return rate;
22736c2ee4cSMarek Vasut
22836c2ee4cSMarek Vasut case CLK_TYPE_GEN3_PLL2:
22936c2ee4cSMarek Vasut value = readl(priv->base + CPG_PLL2CR);
23036c2ee4cSMarek Vasut mult = (((value >> 24) & 0x7f) + 1) * 2;
2318376e0e6SMarek Vasut rate = gen3_clk_get_rate64(&parent) * mult;
2328376e0e6SMarek Vasut debug("%s[%i] PLL2 clk: parent=%i mult=%u => rate=%llu\n",
23336c2ee4cSMarek Vasut __func__, __LINE__, core->parent, mult, rate);
23436c2ee4cSMarek Vasut return rate;
23536c2ee4cSMarek Vasut
23636c2ee4cSMarek Vasut case CLK_TYPE_GEN3_PLL3:
2378376e0e6SMarek Vasut rate = gen3_clk_get_rate64(&parent) * pll_config->pll3_mult;
238f0f1de75SMarek Vasut rate /= pll_config->pll3_div;
239f0f1de75SMarek Vasut debug("%s[%i] PLL3 clk: parent=%i mul=%i div=%i => rate=%llu\n",
24036c2ee4cSMarek Vasut __func__, __LINE__,
241f0f1de75SMarek Vasut core->parent, pll_config->pll3_mult,
242f0f1de75SMarek Vasut pll_config->pll3_div, rate);
24336c2ee4cSMarek Vasut return rate;
24436c2ee4cSMarek Vasut
24536c2ee4cSMarek Vasut case CLK_TYPE_GEN3_PLL4:
24636c2ee4cSMarek Vasut value = readl(priv->base + CPG_PLL4CR);
24736c2ee4cSMarek Vasut mult = (((value >> 24) & 0x7f) + 1) * 2;
2488376e0e6SMarek Vasut rate = gen3_clk_get_rate64(&parent) * mult;
2498376e0e6SMarek Vasut debug("%s[%i] PLL4 clk: parent=%i mult=%u => rate=%llu\n",
25036c2ee4cSMarek Vasut __func__, __LINE__, core->parent, mult, rate);
25136c2ee4cSMarek Vasut return rate;
25236c2ee4cSMarek Vasut
25336c2ee4cSMarek Vasut case CLK_TYPE_FF:
2548376e0e6SMarek Vasut rate = (gen3_clk_get_rate64(&parent) * core->mult) / core->div;
2558376e0e6SMarek Vasut debug("%s[%i] FIXED clk: parent=%i mul=%i div=%i => rate=%llu\n",
25636c2ee4cSMarek Vasut __func__, __LINE__,
25736c2ee4cSMarek Vasut core->parent, core->mult, core->div, rate);
25836c2ee4cSMarek Vasut return rate;
25936c2ee4cSMarek Vasut
260716d7752SMarek Vasut case CLK_TYPE_GEN3_PE:
261716d7752SMarek Vasut div = (core->div >> (priv->sscg ? 16 : 0)) & 0xffff;
262716d7752SMarek Vasut rate = gen3_clk_get_rate64(&parent) / div;
263716d7752SMarek Vasut debug("%s[%i] PE clk: parent=%i div=%u => rate=%llu\n",
264716d7752SMarek Vasut __func__, __LINE__,
265716d7752SMarek Vasut (core->parent >> (priv->sscg ? 16 : 0)) & 0xffff,
266716d7752SMarek Vasut div, rate);
267716d7752SMarek Vasut return rate;
268716d7752SMarek Vasut
26936c2ee4cSMarek Vasut case CLK_TYPE_GEN3_SD: /* FIXME */
27036c2ee4cSMarek Vasut value = readl(priv->base + core->offset);
27136c2ee4cSMarek Vasut value &= CPG_SD_STP_MASK | CPG_SD_FC_MASK;
27236c2ee4cSMarek Vasut
27336c2ee4cSMarek Vasut for (i = 0; i < ARRAY_SIZE(cpg_sd_div_table); i++) {
27436c2ee4cSMarek Vasut if (cpg_sd_div_table[i].val != value)
27536c2ee4cSMarek Vasut continue;
27636c2ee4cSMarek Vasut
2778376e0e6SMarek Vasut rate = gen3_clk_get_rate64(&parent) /
27836c2ee4cSMarek Vasut cpg_sd_div_table[i].div;
2798376e0e6SMarek Vasut debug("%s[%i] SD clk: parent=%i div=%i => rate=%llu\n",
28036c2ee4cSMarek Vasut __func__, __LINE__,
28136c2ee4cSMarek Vasut core->parent, cpg_sd_div_table[i].div, rate);
28236c2ee4cSMarek Vasut
28336c2ee4cSMarek Vasut return rate;
28436c2ee4cSMarek Vasut }
28536c2ee4cSMarek Vasut
28636c2ee4cSMarek Vasut return -EINVAL;
287849ab0a6SMarek Vasut
288849ab0a6SMarek Vasut case CLK_TYPE_GEN3_RPC:
2898376e0e6SMarek Vasut rate = gen3_clk_get_rate64(&parent);
290849ab0a6SMarek Vasut
291849ab0a6SMarek Vasut value = readl(priv->base + core->offset);
292849ab0a6SMarek Vasut
293849ab0a6SMarek Vasut prediv = (value >> CPG_RPC_PREDIV_OFFSET) &
294849ab0a6SMarek Vasut CPG_RPC_PREDIV_MASK;
295849ab0a6SMarek Vasut if (prediv == 2)
296849ab0a6SMarek Vasut rate /= 5;
297849ab0a6SMarek Vasut else if (prediv == 3)
298849ab0a6SMarek Vasut rate /= 6;
299849ab0a6SMarek Vasut else
300849ab0a6SMarek Vasut return -EINVAL;
301849ab0a6SMarek Vasut
302849ab0a6SMarek Vasut postdiv = (value >> CPG_RPC_POSTDIV_OFFSET) &
303849ab0a6SMarek Vasut CPG_RPC_POSTDIV_MASK;
304849ab0a6SMarek Vasut rate /= postdiv + 1;
305849ab0a6SMarek Vasut
3068376e0e6SMarek Vasut debug("%s[%i] RPC clk: parent=%i prediv=%i postdiv=%i => rate=%llu\n",
307849ab0a6SMarek Vasut __func__, __LINE__,
308849ab0a6SMarek Vasut core->parent, prediv, postdiv, rate);
309849ab0a6SMarek Vasut
310849ab0a6SMarek Vasut return -EINVAL;
311849ab0a6SMarek Vasut
31236c2ee4cSMarek Vasut }
31336c2ee4cSMarek Vasut
31436c2ee4cSMarek Vasut printf("%s[%i] unknown fail\n", __func__, __LINE__);
31536c2ee4cSMarek Vasut
31636c2ee4cSMarek Vasut return -ENOENT;
31736c2ee4cSMarek Vasut }
31836c2ee4cSMarek Vasut
gen3_clk_get_rate(struct clk * clk)3198376e0e6SMarek Vasut static ulong gen3_clk_get_rate(struct clk *clk)
3208376e0e6SMarek Vasut {
3218376e0e6SMarek Vasut return gen3_clk_get_rate64(clk);
3228376e0e6SMarek Vasut }
3238376e0e6SMarek Vasut
gen3_clk_set_rate(struct clk * clk,ulong rate)32436c2ee4cSMarek Vasut static ulong gen3_clk_set_rate(struct clk *clk, ulong rate)
32536c2ee4cSMarek Vasut {
326fd5577ceSMarek Vasut /* Force correct SD-IF divider configuration if applicable */
327*f58d6771SMarek Vasut gen3_clk_setup_sdif_div(clk, rate);
3288376e0e6SMarek Vasut return gen3_clk_get_rate64(clk);
32936c2ee4cSMarek Vasut }
33036c2ee4cSMarek Vasut
gen3_clk_of_xlate(struct clk * clk,struct ofnode_phandle_args * args)33136c2ee4cSMarek Vasut static int gen3_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
33236c2ee4cSMarek Vasut {
33336c2ee4cSMarek Vasut if (args->args_count != 2) {
33436c2ee4cSMarek Vasut debug("Invaild args_count: %d\n", args->args_count);
33536c2ee4cSMarek Vasut return -EINVAL;
33636c2ee4cSMarek Vasut }
33736c2ee4cSMarek Vasut
33836c2ee4cSMarek Vasut clk->id = (args->args[0] << 16) | args->args[1];
33936c2ee4cSMarek Vasut
34036c2ee4cSMarek Vasut return 0;
34136c2ee4cSMarek Vasut }
34236c2ee4cSMarek Vasut
343f77b5a4cSMarek Vasut const struct clk_ops gen3_clk_ops = {
34436c2ee4cSMarek Vasut .enable = gen3_clk_enable,
34536c2ee4cSMarek Vasut .disable = gen3_clk_disable,
34636c2ee4cSMarek Vasut .get_rate = gen3_clk_get_rate,
34736c2ee4cSMarek Vasut .set_rate = gen3_clk_set_rate,
34836c2ee4cSMarek Vasut .of_xlate = gen3_clk_of_xlate,
34936c2ee4cSMarek Vasut };
35036c2ee4cSMarek Vasut
gen3_clk_probe(struct udevice * dev)351f77b5a4cSMarek Vasut int gen3_clk_probe(struct udevice *dev)
35236c2ee4cSMarek Vasut {
35336c2ee4cSMarek Vasut struct gen3_clk_priv *priv = dev_get_priv(dev);
354f77b5a4cSMarek Vasut struct cpg_mssr_info *info =
355f77b5a4cSMarek Vasut (struct cpg_mssr_info *)dev_get_driver_data(dev);
35636c2ee4cSMarek Vasut fdt_addr_t rst_base;
35736c2ee4cSMarek Vasut u32 cpg_mode;
35836c2ee4cSMarek Vasut int ret;
35936c2ee4cSMarek Vasut
36036c2ee4cSMarek Vasut priv->base = (struct gen3_base *)devfdt_get_addr(dev);
36136c2ee4cSMarek Vasut if (!priv->base)
36236c2ee4cSMarek Vasut return -EINVAL;
36336c2ee4cSMarek Vasut
364f77b5a4cSMarek Vasut priv->info = info;
365f77b5a4cSMarek Vasut ret = fdt_node_offset_by_compatible(gd->fdt_blob, -1, info->reset_node);
36636c2ee4cSMarek Vasut if (ret < 0)
36736c2ee4cSMarek Vasut return ret;
36836c2ee4cSMarek Vasut
36936c2ee4cSMarek Vasut rst_base = fdtdec_get_addr(gd->fdt_blob, ret, "reg");
37036c2ee4cSMarek Vasut if (rst_base == FDT_ADDR_T_NONE)
37136c2ee4cSMarek Vasut return -EINVAL;
37236c2ee4cSMarek Vasut
37336c2ee4cSMarek Vasut cpg_mode = readl(rst_base + CPG_RST_MODEMR);
37436c2ee4cSMarek Vasut
3757c885563SMarek Vasut priv->cpg_pll_config =
3767c885563SMarek Vasut (struct rcar_gen3_cpg_pll_config *)info->get_pll_config(cpg_mode);
37736c2ee4cSMarek Vasut if (!priv->cpg_pll_config->extal_div)
37836c2ee4cSMarek Vasut return -EINVAL;
37936c2ee4cSMarek Vasut
380716d7752SMarek Vasut priv->sscg = !(cpg_mode & BIT(12));
381716d7752SMarek Vasut
38236c2ee4cSMarek Vasut ret = clk_get_by_name(dev, "extal", &priv->clk_extal);
38336c2ee4cSMarek Vasut if (ret < 0)
38436c2ee4cSMarek Vasut return ret;
38536c2ee4cSMarek Vasut
386f77b5a4cSMarek Vasut if (info->extalr_node) {
387f77b5a4cSMarek Vasut ret = clk_get_by_name(dev, info->extalr_node, &priv->clk_extalr);
38836c2ee4cSMarek Vasut if (ret < 0)
38936c2ee4cSMarek Vasut return ret;
3902c150950SMarek Vasut }
39136c2ee4cSMarek Vasut
39236c2ee4cSMarek Vasut return 0;
39336c2ee4cSMarek Vasut }
39436c2ee4cSMarek Vasut
gen3_clk_remove(struct udevice * dev)395f77b5a4cSMarek Vasut int gen3_clk_remove(struct udevice *dev)
39618cac5afSMarek Vasut {
39718cac5afSMarek Vasut struct gen3_clk_priv *priv = dev_get_priv(dev);
39818cac5afSMarek Vasut
399d2628671SMarek Vasut return renesas_clk_remove(priv->base, priv->info);
40018cac5afSMarek Vasut }
401