Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 2 | /* |
Marek Vasut | 3f1a3a1 | 2017-10-09 20:52:33 +0200 | [diff] [blame] | 3 | * Renesas RCar Gen3 CPG MSSR driver |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 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 Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <clk-uclass.h> |
| 15 | #include <dm.h> |
| 16 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 18 | #include <wait_bit.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 20 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 22 | |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 23 | #include <dt-bindings/clock/renesas-cpg-mssr.h> |
| 24 | |
| 25 | #include "renesas-cpg-mssr.h" |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 26 | #include "rcar-gen3-cpg.h" |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 27 | |
| 28 | #define CPG_RST_MODEMR 0x0060 |
| 29 | |
| 30 | #define CPG_PLL0CR 0x00d8 |
| 31 | #define CPG_PLL2CR 0x002c |
| 32 | #define CPG_PLL4CR 0x01f4 |
| 33 | |
Marek Vasut | c1aee32 | 2017-09-15 21:10:29 +0200 | [diff] [blame] | 34 | #define CPG_RPC_PREDIV_MASK 0x3 |
| 35 | #define CPG_RPC_PREDIV_OFFSET 3 |
| 36 | #define CPG_RPC_POSTDIV_MASK 0x7 |
| 37 | #define CPG_RPC_POSTDIV_OFFSET 0 |
| 38 | |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 39 | /* |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 40 | * SDn Clock |
| 41 | */ |
| 42 | #define CPG_SD_STP_HCK BIT(9) |
| 43 | #define CPG_SD_STP_CK BIT(8) |
| 44 | |
| 45 | #define CPG_SD_STP_MASK (CPG_SD_STP_HCK | CPG_SD_STP_CK) |
| 46 | #define CPG_SD_FC_MASK (0x7 << 2 | 0x3 << 0) |
| 47 | |
| 48 | #define CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) \ |
| 49 | { \ |
| 50 | .val = ((stp_hck) ? CPG_SD_STP_HCK : 0) | \ |
| 51 | ((stp_ck) ? CPG_SD_STP_CK : 0) | \ |
| 52 | ((sd_srcfc) << 2) | \ |
| 53 | ((sd_fc) << 0), \ |
| 54 | .div = (sd_div), \ |
| 55 | } |
| 56 | |
| 57 | struct sd_div_table { |
| 58 | u32 val; |
| 59 | unsigned int div; |
| 60 | }; |
| 61 | |
| 62 | /* SDn divider |
| 63 | * sd_srcfc sd_fc div |
| 64 | * stp_hck stp_ck (div) (div) = sd_srcfc x sd_fc |
| 65 | *------------------------------------------------------------------- |
| 66 | * 0 0 0 (1) 1 (4) 4 |
| 67 | * 0 0 1 (2) 1 (4) 8 |
| 68 | * 1 0 2 (4) 1 (4) 16 |
| 69 | * 1 0 3 (8) 1 (4) 32 |
| 70 | * 1 0 4 (16) 1 (4) 64 |
| 71 | * 0 0 0 (1) 0 (2) 2 |
| 72 | * 0 0 1 (2) 0 (2) 4 |
| 73 | * 1 0 2 (4) 0 (2) 8 |
| 74 | * 1 0 3 (8) 0 (2) 16 |
| 75 | * 1 0 4 (16) 0 (2) 32 |
| 76 | */ |
| 77 | static const struct sd_div_table cpg_sd_div_table[] = { |
| 78 | /* CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) */ |
| 79 | CPG_SD_DIV_TABLE_DATA(0, 0, 0, 1, 4), |
| 80 | CPG_SD_DIV_TABLE_DATA(0, 0, 1, 1, 8), |
| 81 | CPG_SD_DIV_TABLE_DATA(1, 0, 2, 1, 16), |
| 82 | CPG_SD_DIV_TABLE_DATA(1, 0, 3, 1, 32), |
| 83 | CPG_SD_DIV_TABLE_DATA(1, 0, 4, 1, 64), |
| 84 | CPG_SD_DIV_TABLE_DATA(0, 0, 0, 0, 2), |
| 85 | CPG_SD_DIV_TABLE_DATA(0, 0, 1, 0, 4), |
| 86 | CPG_SD_DIV_TABLE_DATA(1, 0, 2, 0, 8), |
| 87 | CPG_SD_DIV_TABLE_DATA(1, 0, 3, 0, 16), |
| 88 | CPG_SD_DIV_TABLE_DATA(1, 0, 4, 0, 32), |
| 89 | }; |
| 90 | |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 91 | static int gen3_clk_get_parent(struct gen3_clk_priv *priv, struct clk *clk, |
| 92 | struct cpg_mssr_info *info, struct clk *parent) |
| 93 | { |
| 94 | const struct cpg_core_clk *core; |
| 95 | int ret; |
| 96 | |
| 97 | if (!renesas_clk_is_mod(clk)) { |
| 98 | ret = renesas_clk_get_core(clk, info, &core); |
| 99 | if (ret) |
| 100 | return ret; |
| 101 | |
Marek Vasut | 7841483 | 2019-03-04 21:38:10 +0100 | [diff] [blame] | 102 | if (core->type == CLK_TYPE_GEN3_MDSEL) { |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 103 | parent->dev = clk->dev; |
| 104 | parent->id = core->parent >> (priv->sscg ? 16 : 0); |
| 105 | parent->id &= 0xffff; |
| 106 | return 0; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return renesas_clk_get_parent(clk, info, parent); |
| 111 | } |
| 112 | |
Marek Vasut | c26bf89 | 2018-10-30 17:54:20 +0100 | [diff] [blame] | 113 | static int gen3_clk_setup_sdif_div(struct clk *clk, ulong rate) |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 114 | { |
| 115 | struct gen3_clk_priv *priv = dev_get_priv(clk->dev); |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 116 | struct cpg_mssr_info *info = priv->info; |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 117 | const struct cpg_core_clk *core; |
| 118 | struct clk parent; |
| 119 | int ret; |
| 120 | |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 121 | ret = gen3_clk_get_parent(priv, clk, info, &parent); |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 122 | if (ret) { |
| 123 | printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret); |
| 124 | return ret; |
| 125 | } |
| 126 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 127 | if (renesas_clk_is_mod(&parent)) |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 128 | return 0; |
| 129 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 130 | ret = renesas_clk_get_core(&parent, info, &core); |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 131 | if (ret) |
| 132 | return ret; |
| 133 | |
| 134 | if (core->type != CLK_TYPE_GEN3_SD) |
| 135 | return 0; |
| 136 | |
| 137 | debug("%s[%i] SDIF offset=%x\n", __func__, __LINE__, core->offset); |
| 138 | |
Marek Vasut | c26bf89 | 2018-10-30 17:54:20 +0100 | [diff] [blame] | 139 | writel((rate == 400000000) ? 0x4 : 0x1, priv->base + core->offset); |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 144 | static int gen3_clk_enable(struct clk *clk) |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 145 | { |
| 146 | struct gen3_clk_priv *priv = dev_get_priv(clk->dev); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 147 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 148 | return renesas_clk_endisable(clk, priv->base, true); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static int gen3_clk_disable(struct clk *clk) |
| 152 | { |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 153 | struct gen3_clk_priv *priv = dev_get_priv(clk->dev); |
| 154 | |
| 155 | return renesas_clk_endisable(clk, priv->base, false); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 158 | static u64 gen3_clk_get_rate64(struct clk *clk) |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 159 | { |
| 160 | struct gen3_clk_priv *priv = dev_get_priv(clk->dev); |
Marek Vasut | b923419 | 2018-01-08 16:05:28 +0100 | [diff] [blame] | 161 | struct cpg_mssr_info *info = priv->info; |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 162 | struct clk parent; |
| 163 | const struct cpg_core_clk *core; |
| 164 | const struct rcar_gen3_cpg_pll_config *pll_config = |
| 165 | priv->cpg_pll_config; |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 166 | u32 value, mult, div, prediv, postdiv; |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 167 | u64 rate = 0; |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 168 | int i, ret; |
| 169 | |
| 170 | debug("%s[%i] Clock: id=%lu\n", __func__, __LINE__, clk->id); |
| 171 | |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 172 | ret = gen3_clk_get_parent(priv, clk, info, &parent); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 173 | if (ret) { |
| 174 | printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret); |
| 175 | return ret; |
| 176 | } |
| 177 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 178 | if (renesas_clk_is_mod(clk)) { |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 179 | rate = gen3_clk_get_rate64(&parent); |
| 180 | debug("%s[%i] MOD clk: parent=%lu => rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 181 | __func__, __LINE__, parent.id, rate); |
| 182 | return rate; |
| 183 | } |
| 184 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 185 | ret = renesas_clk_get_core(clk, info, &core); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 186 | if (ret) |
| 187 | return ret; |
| 188 | |
| 189 | switch (core->type) { |
| 190 | case CLK_TYPE_IN: |
Marek Vasut | b923419 | 2018-01-08 16:05:28 +0100 | [diff] [blame] | 191 | if (core->id == info->clk_extal_id) { |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 192 | rate = clk_get_rate(&priv->clk_extal); |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 193 | debug("%s[%i] EXTAL clk: rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 194 | __func__, __LINE__, rate); |
| 195 | return rate; |
| 196 | } |
| 197 | |
Marek Vasut | b923419 | 2018-01-08 16:05:28 +0100 | [diff] [blame] | 198 | if (core->id == info->clk_extalr_id) { |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 199 | rate = clk_get_rate(&priv->clk_extalr); |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 200 | debug("%s[%i] EXTALR clk: rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 201 | __func__, __LINE__, rate); |
| 202 | return rate; |
| 203 | } |
| 204 | |
| 205 | return -EINVAL; |
| 206 | |
| 207 | case CLK_TYPE_GEN3_MAIN: |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 208 | rate = gen3_clk_get_rate64(&parent) / pll_config->extal_div; |
| 209 | debug("%s[%i] MAIN clk: parent=%i extal_div=%i => rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 210 | __func__, __LINE__, |
| 211 | core->parent, pll_config->extal_div, rate); |
| 212 | return rate; |
| 213 | |
| 214 | case CLK_TYPE_GEN3_PLL0: |
| 215 | value = readl(priv->base + CPG_PLL0CR); |
| 216 | mult = (((value >> 24) & 0x7f) + 1) * 2; |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 217 | rate = gen3_clk_get_rate64(&parent) * mult; |
| 218 | debug("%s[%i] PLL0 clk: parent=%i mult=%u => rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 219 | __func__, __LINE__, core->parent, mult, rate); |
| 220 | return rate; |
| 221 | |
| 222 | case CLK_TYPE_GEN3_PLL1: |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 223 | rate = gen3_clk_get_rate64(&parent) * pll_config->pll1_mult; |
Marek Vasut | 52389f0 | 2018-05-31 19:25:41 +0200 | [diff] [blame] | 224 | rate /= pll_config->pll1_div; |
| 225 | debug("%s[%i] PLL1 clk: parent=%i mul=%i div=%i => rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 226 | __func__, __LINE__, |
Marek Vasut | 52389f0 | 2018-05-31 19:25:41 +0200 | [diff] [blame] | 227 | core->parent, pll_config->pll1_mult, |
| 228 | pll_config->pll1_div, rate); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 229 | return rate; |
| 230 | |
| 231 | case CLK_TYPE_GEN3_PLL2: |
| 232 | value = readl(priv->base + CPG_PLL2CR); |
| 233 | mult = (((value >> 24) & 0x7f) + 1) * 2; |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 234 | rate = gen3_clk_get_rate64(&parent) * mult; |
| 235 | debug("%s[%i] PLL2 clk: parent=%i mult=%u => rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 236 | __func__, __LINE__, core->parent, mult, rate); |
| 237 | return rate; |
| 238 | |
| 239 | case CLK_TYPE_GEN3_PLL3: |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 240 | rate = gen3_clk_get_rate64(&parent) * pll_config->pll3_mult; |
Marek Vasut | 52389f0 | 2018-05-31 19:25:41 +0200 | [diff] [blame] | 241 | rate /= pll_config->pll3_div; |
| 242 | debug("%s[%i] PLL3 clk: parent=%i mul=%i div=%i => rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 243 | __func__, __LINE__, |
Marek Vasut | 52389f0 | 2018-05-31 19:25:41 +0200 | [diff] [blame] | 244 | core->parent, pll_config->pll3_mult, |
| 245 | pll_config->pll3_div, rate); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 246 | return rate; |
| 247 | |
| 248 | case CLK_TYPE_GEN3_PLL4: |
| 249 | value = readl(priv->base + CPG_PLL4CR); |
| 250 | mult = (((value >> 24) & 0x7f) + 1) * 2; |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 251 | rate = gen3_clk_get_rate64(&parent) * mult; |
| 252 | debug("%s[%i] PLL4 clk: parent=%i mult=%u => rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 253 | __func__, __LINE__, core->parent, mult, rate); |
| 254 | return rate; |
| 255 | |
| 256 | case CLK_TYPE_FF: |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 257 | rate = (gen3_clk_get_rate64(&parent) * core->mult) / core->div; |
| 258 | debug("%s[%i] FIXED clk: parent=%i mul=%i div=%i => rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 259 | __func__, __LINE__, |
| 260 | core->parent, core->mult, core->div, rate); |
| 261 | return rate; |
| 262 | |
Marek Vasut | 7841483 | 2019-03-04 21:38:10 +0100 | [diff] [blame] | 263 | case CLK_TYPE_GEN3_MDSEL: |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 264 | div = (core->div >> (priv->sscg ? 16 : 0)) & 0xffff; |
| 265 | rate = gen3_clk_get_rate64(&parent) / div; |
| 266 | debug("%s[%i] PE clk: parent=%i div=%u => rate=%llu\n", |
| 267 | __func__, __LINE__, |
| 268 | (core->parent >> (priv->sscg ? 16 : 0)) & 0xffff, |
| 269 | div, rate); |
| 270 | return rate; |
| 271 | |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 272 | case CLK_TYPE_GEN3_SD: /* FIXME */ |
| 273 | value = readl(priv->base + core->offset); |
| 274 | value &= CPG_SD_STP_MASK | CPG_SD_FC_MASK; |
| 275 | |
| 276 | for (i = 0; i < ARRAY_SIZE(cpg_sd_div_table); i++) { |
| 277 | if (cpg_sd_div_table[i].val != value) |
| 278 | continue; |
| 279 | |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 280 | rate = gen3_clk_get_rate64(&parent) / |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 281 | cpg_sd_div_table[i].div; |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 282 | debug("%s[%i] SD clk: parent=%i div=%i => rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 283 | __func__, __LINE__, |
| 284 | core->parent, cpg_sd_div_table[i].div, rate); |
| 285 | |
| 286 | return rate; |
| 287 | } |
| 288 | |
| 289 | return -EINVAL; |
Marek Vasut | c1aee32 | 2017-09-15 21:10:29 +0200 | [diff] [blame] | 290 | |
| 291 | case CLK_TYPE_GEN3_RPC: |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 292 | rate = gen3_clk_get_rate64(&parent); |
Marek Vasut | c1aee32 | 2017-09-15 21:10:29 +0200 | [diff] [blame] | 293 | |
| 294 | value = readl(priv->base + core->offset); |
| 295 | |
| 296 | prediv = (value >> CPG_RPC_PREDIV_OFFSET) & |
| 297 | CPG_RPC_PREDIV_MASK; |
| 298 | if (prediv == 2) |
| 299 | rate /= 5; |
| 300 | else if (prediv == 3) |
| 301 | rate /= 6; |
| 302 | else |
| 303 | return -EINVAL; |
| 304 | |
| 305 | postdiv = (value >> CPG_RPC_POSTDIV_OFFSET) & |
| 306 | CPG_RPC_POSTDIV_MASK; |
| 307 | rate /= postdiv + 1; |
| 308 | |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 309 | debug("%s[%i] RPC clk: parent=%i prediv=%i postdiv=%i => rate=%llu\n", |
Marek Vasut | c1aee32 | 2017-09-15 21:10:29 +0200 | [diff] [blame] | 310 | __func__, __LINE__, |
| 311 | core->parent, prediv, postdiv, rate); |
| 312 | |
Hai Pham | f2279df | 2020-12-05 09:35:40 +0700 | [diff] [blame^] | 313 | return rate; |
Marek Vasut | c1aee32 | 2017-09-15 21:10:29 +0200 | [diff] [blame] | 314 | |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | printf("%s[%i] unknown fail\n", __func__, __LINE__); |
| 318 | |
| 319 | return -ENOENT; |
| 320 | } |
| 321 | |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 322 | static ulong gen3_clk_get_rate(struct clk *clk) |
| 323 | { |
| 324 | return gen3_clk_get_rate64(clk); |
| 325 | } |
| 326 | |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 327 | static ulong gen3_clk_set_rate(struct clk *clk, ulong rate) |
| 328 | { |
Marek Vasut | 414dbbe | 2018-01-11 16:28:31 +0100 | [diff] [blame] | 329 | /* Force correct SD-IF divider configuration if applicable */ |
Marek Vasut | c26bf89 | 2018-10-30 17:54:20 +0100 | [diff] [blame] | 330 | gen3_clk_setup_sdif_div(clk, rate); |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 331 | return gen3_clk_get_rate64(clk); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static int gen3_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args) |
| 335 | { |
| 336 | if (args->args_count != 2) { |
| 337 | debug("Invaild args_count: %d\n", args->args_count); |
| 338 | return -EINVAL; |
| 339 | } |
| 340 | |
| 341 | clk->id = (args->args[0] << 16) | args->args[1]; |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 346 | const struct clk_ops gen3_clk_ops = { |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 347 | .enable = gen3_clk_enable, |
| 348 | .disable = gen3_clk_disable, |
| 349 | .get_rate = gen3_clk_get_rate, |
| 350 | .set_rate = gen3_clk_set_rate, |
| 351 | .of_xlate = gen3_clk_of_xlate, |
| 352 | }; |
| 353 | |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 354 | int gen3_clk_probe(struct udevice *dev) |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 355 | { |
| 356 | struct gen3_clk_priv *priv = dev_get_priv(dev); |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 357 | struct cpg_mssr_info *info = |
| 358 | (struct cpg_mssr_info *)dev_get_driver_data(dev); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 359 | fdt_addr_t rst_base; |
| 360 | u32 cpg_mode; |
| 361 | int ret; |
| 362 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 363 | priv->base = dev_read_addr_ptr(dev); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 364 | if (!priv->base) |
| 365 | return -EINVAL; |
| 366 | |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 367 | priv->info = info; |
| 368 | ret = fdt_node_offset_by_compatible(gd->fdt_blob, -1, info->reset_node); |
| 369 | if (ret < 0) |
| 370 | return ret; |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 371 | |
| 372 | rst_base = fdtdec_get_addr(gd->fdt_blob, ret, "reg"); |
| 373 | if (rst_base == FDT_ADDR_T_NONE) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | cpg_mode = readl(rst_base + CPG_RST_MODEMR); |
| 377 | |
Marek Vasut | 28f9004 | 2018-01-16 19:23:17 +0100 | [diff] [blame] | 378 | priv->cpg_pll_config = |
| 379 | (struct rcar_gen3_cpg_pll_config *)info->get_pll_config(cpg_mode); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 380 | if (!priv->cpg_pll_config->extal_div) |
| 381 | return -EINVAL; |
| 382 | |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 383 | priv->sscg = !(cpg_mode & BIT(12)); |
| 384 | |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 385 | ret = clk_get_by_name(dev, "extal", &priv->clk_extal); |
| 386 | if (ret < 0) |
| 387 | return ret; |
| 388 | |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 389 | if (info->extalr_node) { |
| 390 | ret = clk_get_by_name(dev, info->extalr_node, &priv->clk_extalr); |
Marek Vasut | fb0aa29 | 2017-10-08 21:09:15 +0200 | [diff] [blame] | 391 | if (ret < 0) |
| 392 | return ret; |
| 393 | } |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 398 | int gen3_clk_remove(struct udevice *dev) |
Marek Vasut | df6a114 | 2017-11-25 22:08:55 +0100 | [diff] [blame] | 399 | { |
| 400 | struct gen3_clk_priv *priv = dev_get_priv(dev); |
Marek Vasut | df6a114 | 2017-11-25 22:08:55 +0100 | [diff] [blame] | 401 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 402 | return renesas_clk_remove(priv->base, priv->info); |
Marek Vasut | df6a114 | 2017-11-25 22:08:55 +0100 | [diff] [blame] | 403 | } |