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> |
Marek Vasut | f6b3202 | 2023-01-26 21:02:03 +0100 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/lists.h> |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 18 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 19 | #include <log.h> |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 20 | #include <wait_bit.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 21 | #include <asm/global_data.h> |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 22 | #include <asm/io.h> |
Hai Pham | e83700a | 2023-01-26 21:06:03 +0100 | [diff] [blame] | 23 | #include <linux/bitfield.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 24 | #include <linux/bitops.h> |
Marek Vasut | b2970fd | 2023-01-26 21:06:02 +0100 | [diff] [blame] | 25 | #include <linux/clk-provider.h> |
Marek Vasut | f6b3202 | 2023-01-26 21:02:03 +0100 | [diff] [blame] | 26 | #include <reset-uclass.h> |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 27 | |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 28 | #include <dt-bindings/clock/renesas-cpg-mssr.h> |
| 29 | |
| 30 | #include "renesas-cpg-mssr.h" |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 31 | #include "rcar-gen3-cpg.h" |
Hai Pham | 06d8f97 | 2023-01-26 21:06:07 +0100 | [diff] [blame] | 32 | #include "rcar-cpg-lib.h" |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 33 | |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 34 | #define CPG_PLL0CR 0x00d8 |
| 35 | #define CPG_PLL2CR 0x002c |
| 36 | #define CPG_PLL4CR 0x01f4 |
| 37 | |
Hai Pham | e83700a | 2023-01-26 21:06:03 +0100 | [diff] [blame] | 38 | static const struct clk_div_table cpg_rpcsrc_div_table[] = { |
| 39 | { 2, 5 }, { 3, 6 }, { 0, 0 }, |
| 40 | }; |
| 41 | |
Hai Pham | 6811b57 | 2023-01-26 21:06:06 +0100 | [diff] [blame] | 42 | static const struct clk_div_table r8a77970_cpg_sd0h_div_table[] = { |
| 43 | { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 }, |
| 44 | { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 }, |
| 45 | { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 }, |
| 46 | }; |
| 47 | |
| 48 | static const struct clk_div_table r8a77970_cpg_sd0_div_table[] = { |
| 49 | { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 }, |
| 50 | { 8, 24 }, { 10, 36 }, { 11, 48 }, { 12, 10 }, |
| 51 | { 0, 0 }, |
| 52 | }; |
| 53 | |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 54 | static int gen3_clk_get_parent(struct gen3_clk_priv *priv, struct clk *clk, |
| 55 | struct cpg_mssr_info *info, struct clk *parent) |
| 56 | { |
| 57 | const struct cpg_core_clk *core; |
| 58 | int ret; |
| 59 | |
| 60 | if (!renesas_clk_is_mod(clk)) { |
| 61 | ret = renesas_clk_get_core(clk, info, &core); |
| 62 | if (ret) |
| 63 | return ret; |
| 64 | |
Marek Vasut | 7841483 | 2019-03-04 21:38:10 +0100 | [diff] [blame] | 65 | if (core->type == CLK_TYPE_GEN3_MDSEL) { |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 66 | parent->dev = clk->dev; |
| 67 | parent->id = core->parent >> (priv->sscg ? 16 : 0); |
| 68 | parent->id &= 0xffff; |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return renesas_clk_get_parent(clk, info, parent); |
| 74 | } |
| 75 | |
Hai Pham | 4dae076 | 2023-01-29 02:50:22 +0100 | [diff] [blame] | 76 | static int gen3_clk_enable(struct clk *clk) |
| 77 | { |
| 78 | struct gen3_clk_priv *priv = dev_get_priv(clk->dev); |
| 79 | |
| 80 | return renesas_clk_endisable(clk, priv->base, priv->info, true); |
| 81 | } |
| 82 | |
| 83 | static int gen3_clk_disable(struct clk *clk) |
| 84 | { |
| 85 | struct gen3_clk_priv *priv = dev_get_priv(clk->dev); |
| 86 | |
| 87 | return renesas_clk_endisable(clk, priv->base, priv->info, false); |
| 88 | } |
| 89 | |
| 90 | static u64 gen3_clk_get_rate64(struct clk *clk); |
| 91 | |
Marek Vasut | c26bf89 | 2018-10-30 17:54:20 +0100 | [diff] [blame] | 92 | static int gen3_clk_setup_sdif_div(struct clk *clk, ulong rate) |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 93 | { |
| 94 | struct gen3_clk_priv *priv = dev_get_priv(clk->dev); |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 95 | struct cpg_mssr_info *info = priv->info; |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 96 | const struct cpg_core_clk *core; |
Hai Pham | 4dae076 | 2023-01-29 02:50:22 +0100 | [diff] [blame] | 97 | struct clk parent, grandparent; |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 98 | int ret; |
| 99 | |
Hai Pham | 4dae076 | 2023-01-29 02:50:22 +0100 | [diff] [blame] | 100 | /* |
| 101 | * The clk may be either CPG_MOD or core clock, in case this is MOD |
| 102 | * clock, use core clock one level up, otherwise use the clock as-is. |
| 103 | * Note that parent clock here always represents core clock. Also note |
| 104 | * that grandparent clock are the parent clock of the core clock here. |
| 105 | */ |
| 106 | if (renesas_clk_is_mod(clk)) { |
| 107 | ret = gen3_clk_get_parent(priv, clk, info, &parent); |
| 108 | if (ret) { |
| 109 | printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret); |
| 110 | return ret; |
| 111 | } |
| 112 | } else { |
| 113 | parent = *clk; |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 116 | if (renesas_clk_is_mod(&parent)) |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 117 | return 0; |
| 118 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 119 | ret = renesas_clk_get_core(&parent, info, &core); |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 120 | if (ret) |
| 121 | return ret; |
| 122 | |
Hai Pham | 4dae076 | 2023-01-29 02:50:22 +0100 | [diff] [blame] | 123 | ret = renesas_clk_get_parent(&parent, info, &grandparent); |
| 124 | if (ret) { |
| 125 | printf("%s[%i] grandparent fail, ret=%i\n", __func__, __LINE__, ret); |
| 126 | return ret; |
| 127 | } |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 128 | |
Hai Pham | 4dae076 | 2023-01-29 02:50:22 +0100 | [diff] [blame] | 129 | switch (core->type) { |
| 130 | case CLK_TYPE_GEN3_SDH: |
| 131 | fallthrough; |
| 132 | case CLK_TYPE_GEN4_SDH: |
Hai Pham | 06d8f97 | 2023-01-26 21:06:07 +0100 | [diff] [blame] | 133 | return rcar_clk_set_rate64_sdh(core->parent, |
| 134 | gen3_clk_get_rate64(&grandparent), |
| 135 | rate, priv->base + core->offset); |
Marek Vasut | 5a51be5 | 2017-09-15 21:10:08 +0200 | [diff] [blame] | 136 | |
Hai Pham | 4dae076 | 2023-01-29 02:50:22 +0100 | [diff] [blame] | 137 | case CLK_TYPE_GEN3_SD: |
| 138 | fallthrough; |
| 139 | case CLK_TYPE_GEN4_SD: |
Hai Pham | 06d8f97 | 2023-01-26 21:06:07 +0100 | [diff] [blame] | 140 | return rcar_clk_set_rate64_sd(core->parent, |
| 141 | gen3_clk_get_rate64(&grandparent), |
| 142 | rate, priv->base + core->offset); |
Hai Pham | 6811b57 | 2023-01-26 21:06:06 +0100 | [diff] [blame] | 143 | |
| 144 | case CLK_TYPE_R8A77970_SD0: |
Hai Pham | 06d8f97 | 2023-01-26 21:06:07 +0100 | [diff] [blame] | 145 | return rcar_clk_set_rate64_div_table(core->parent, |
| 146 | gen3_clk_get_rate64(&grandparent), |
| 147 | rate, priv->base + core->offset, |
| 148 | CPG_SDCKCR_SD0FC_MASK, |
| 149 | r8a77970_cpg_sd0_div_table, "SD"); |
Hai Pham | 4dae076 | 2023-01-29 02:50:22 +0100 | [diff] [blame] | 150 | } |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 151 | |
Hai Pham | 4dae076 | 2023-01-29 02:50:22 +0100 | [diff] [blame] | 152 | return 0; |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Marek Vasut | 8f56786 | 2021-04-27 19:36:39 +0200 | [diff] [blame] | 155 | static u64 gen3_clk_get_rate64_pll_mul_reg(struct gen3_clk_priv *priv, |
| 156 | struct clk *parent, |
Marek Vasut | 8f56786 | 2021-04-27 19:36:39 +0200 | [diff] [blame] | 157 | u32 mul_reg, u32 mult, u32 div, |
| 158 | char *name) |
| 159 | { |
| 160 | u32 value; |
| 161 | u64 rate; |
| 162 | |
| 163 | if (mul_reg) { |
| 164 | value = readl(priv->base + mul_reg); |
| 165 | mult = (((value >> 24) & 0x7f) + 1) * 2; |
| 166 | div = 1; |
| 167 | } |
| 168 | |
| 169 | rate = (gen3_clk_get_rate64(parent) * mult) / div; |
| 170 | |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 171 | debug("%s[%i] %s clk: mult=%u div=%u => rate=%llu\n", |
| 172 | __func__, __LINE__, name, mult, div, rate); |
Marek Vasut | 8f56786 | 2021-04-27 19:36:39 +0200 | [diff] [blame] | 173 | return rate; |
| 174 | } |
| 175 | |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 176 | static u64 gen3_clk_get_rate64(struct clk *clk) |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 177 | { |
| 178 | struct gen3_clk_priv *priv = dev_get_priv(clk->dev); |
Marek Vasut | b923419 | 2018-01-08 16:05:28 +0100 | [diff] [blame] | 179 | struct cpg_mssr_info *info = priv->info; |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 180 | struct clk parent; |
| 181 | const struct cpg_core_clk *core; |
| 182 | const struct rcar_gen3_cpg_pll_config *pll_config = |
| 183 | priv->cpg_pll_config; |
Hai Pham | e83700a | 2023-01-26 21:06:03 +0100 | [diff] [blame] | 184 | u32 value, div; |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 185 | u64 rate = 0; |
Hai Pham | 4dae076 | 2023-01-29 02:50:22 +0100 | [diff] [blame] | 186 | int ret; |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 187 | |
| 188 | debug("%s[%i] Clock: id=%lu\n", __func__, __LINE__, clk->id); |
| 189 | |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 190 | ret = gen3_clk_get_parent(priv, clk, info, &parent); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 191 | if (ret) { |
| 192 | printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret); |
| 193 | return ret; |
| 194 | } |
| 195 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 196 | if (renesas_clk_is_mod(clk)) { |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 197 | rate = gen3_clk_get_rate64(&parent); |
| 198 | debug("%s[%i] MOD clk: parent=%lu => rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 199 | __func__, __LINE__, parent.id, rate); |
| 200 | return rate; |
| 201 | } |
| 202 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 203 | ret = renesas_clk_get_core(clk, info, &core); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 204 | if (ret) |
| 205 | return ret; |
| 206 | |
| 207 | switch (core->type) { |
| 208 | case CLK_TYPE_IN: |
Marek Vasut | b923419 | 2018-01-08 16:05:28 +0100 | [diff] [blame] | 209 | if (core->id == info->clk_extal_id) { |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 210 | rate = clk_get_rate(&priv->clk_extal); |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 211 | debug("%s[%i] EXTAL clk: rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 212 | __func__, __LINE__, rate); |
| 213 | return rate; |
| 214 | } |
| 215 | |
Marek Vasut | b923419 | 2018-01-08 16:05:28 +0100 | [diff] [blame] | 216 | if (core->id == info->clk_extalr_id) { |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 217 | rate = clk_get_rate(&priv->clk_extalr); |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 218 | debug("%s[%i] EXTALR clk: rate=%llu\n", |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 219 | __func__, __LINE__, rate); |
| 220 | return rate; |
| 221 | } |
| 222 | |
| 223 | return -EINVAL; |
| 224 | |
| 225 | case CLK_TYPE_GEN3_MAIN: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 226 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 8f56786 | 2021-04-27 19:36:39 +0200 | [diff] [blame] | 227 | 0, 1, pll_config->extal_div, |
| 228 | "MAIN"); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 229 | |
| 230 | case CLK_TYPE_GEN3_PLL0: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 231 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 8f56786 | 2021-04-27 19:36:39 +0200 | [diff] [blame] | 232 | CPG_PLL0CR, 0, 0, "PLL0"); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 233 | |
| 234 | case CLK_TYPE_GEN3_PLL1: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 235 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 8f56786 | 2021-04-27 19:36:39 +0200 | [diff] [blame] | 236 | 0, pll_config->pll1_mult, |
| 237 | pll_config->pll1_div, "PLL1"); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 238 | |
| 239 | case CLK_TYPE_GEN3_PLL2: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 240 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 8f56786 | 2021-04-27 19:36:39 +0200 | [diff] [blame] | 241 | CPG_PLL2CR, 0, 0, "PLL2"); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 242 | |
| 243 | case CLK_TYPE_GEN3_PLL3: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 244 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 8f56786 | 2021-04-27 19:36:39 +0200 | [diff] [blame] | 245 | 0, pll_config->pll3_mult, |
| 246 | pll_config->pll3_div, "PLL3"); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 247 | |
| 248 | case CLK_TYPE_GEN3_PLL4: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 249 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 8f56786 | 2021-04-27 19:36:39 +0200 | [diff] [blame] | 250 | CPG_PLL4CR, 0, 0, "PLL4"); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 251 | |
Marek Vasut | 569acef | 2023-01-26 21:01:56 +0100 | [diff] [blame] | 252 | case CLK_TYPE_GEN4_MAIN: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 253 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 0fbb8a7 | 2021-04-27 19:52:53 +0200 | [diff] [blame] | 254 | 0, 1, pll_config->extal_div, |
| 255 | "V3U_MAIN"); |
| 256 | |
Marek Vasut | 569acef | 2023-01-26 21:01:56 +0100 | [diff] [blame] | 257 | case CLK_TYPE_GEN4_PLL1: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 258 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 0fbb8a7 | 2021-04-27 19:52:53 +0200 | [diff] [blame] | 259 | 0, pll_config->pll1_mult, |
| 260 | pll_config->pll1_div, |
| 261 | "V3U_PLL1"); |
| 262 | |
Marek Vasut | 569acef | 2023-01-26 21:01:56 +0100 | [diff] [blame] | 263 | case CLK_TYPE_GEN4_PLL2X_3X: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 264 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 0fbb8a7 | 2021-04-27 19:52:53 +0200 | [diff] [blame] | 265 | core->offset, 0, 0, |
| 266 | "V3U_PLL2X_3X"); |
| 267 | |
Marek Vasut | 569acef | 2023-01-26 21:01:56 +0100 | [diff] [blame] | 268 | case CLK_TYPE_GEN4_PLL5: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 269 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 0fbb8a7 | 2021-04-27 19:52:53 +0200 | [diff] [blame] | 270 | 0, pll_config->pll5_mult, |
| 271 | pll_config->pll5_div, |
| 272 | "V3U_PLL5"); |
| 273 | |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 274 | case CLK_TYPE_FF: |
Marek Vasut | 1bd2521 | 2023-01-26 21:02:05 +0100 | [diff] [blame] | 275 | return gen3_clk_get_rate64_pll_mul_reg(priv, &parent, |
Marek Vasut | 8f56786 | 2021-04-27 19:36:39 +0200 | [diff] [blame] | 276 | 0, core->mult, core->div, |
| 277 | "FIXED"); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 278 | |
Marek Vasut | 7841483 | 2019-03-04 21:38:10 +0100 | [diff] [blame] | 279 | case CLK_TYPE_GEN3_MDSEL: |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 280 | div = (core->div >> (priv->sscg ? 16 : 0)) & 0xffff; |
| 281 | rate = gen3_clk_get_rate64(&parent) / div; |
| 282 | debug("%s[%i] PE clk: parent=%i div=%u => rate=%llu\n", |
| 283 | __func__, __LINE__, |
| 284 | (core->parent >> (priv->sscg ? 16 : 0)) & 0xffff, |
| 285 | div, rate); |
| 286 | return rate; |
| 287 | |
Hai Pham | 0985e0e | 2023-01-26 21:01:49 +0100 | [diff] [blame] | 288 | case CLK_TYPE_GEN3_SDH: /* Fixed factor 1:1 */ |
Marek Vasut | 569acef | 2023-01-26 21:01:56 +0100 | [diff] [blame] | 289 | fallthrough; |
| 290 | case CLK_TYPE_GEN4_SDH: /* Fixed factor 1:1 */ |
Hai Pham | 06d8f97 | 2023-01-26 21:06:07 +0100 | [diff] [blame] | 291 | return rcar_clk_get_rate64_sdh(core->parent, |
| 292 | gen3_clk_get_rate64(&parent), |
| 293 | priv->base + core->offset); |
Hai Pham | 0985e0e | 2023-01-26 21:01:49 +0100 | [diff] [blame] | 294 | |
Hai Pham | 6811b57 | 2023-01-26 21:06:06 +0100 | [diff] [blame] | 295 | case CLK_TYPE_R8A77970_SD0H: |
| 296 | return rcar_clk_get_rate64_div_table(core->parent, |
| 297 | gen3_clk_get_rate64(&parent), |
| 298 | priv->base + core->offset, |
| 299 | CPG_SDCKCR_SDHFC_MASK, |
| 300 | r8a77970_cpg_sd0h_div_table, "SDH"); |
| 301 | |
Hai Pham | 4dae076 | 2023-01-29 02:50:22 +0100 | [diff] [blame] | 302 | case CLK_TYPE_GEN3_SD: |
Marek Vasut | 0fbb8a7 | 2021-04-27 19:52:53 +0200 | [diff] [blame] | 303 | fallthrough; |
Marek Vasut | 569acef | 2023-01-26 21:01:56 +0100 | [diff] [blame] | 304 | case CLK_TYPE_GEN4_SD: |
Hai Pham | 06d8f97 | 2023-01-26 21:06:07 +0100 | [diff] [blame] | 305 | return rcar_clk_get_rate64_sd(core->parent, |
| 306 | gen3_clk_get_rate64(&parent), |
| 307 | priv->base + core->offset); |
Marek Vasut | c1aee32 | 2017-09-15 21:10:29 +0200 | [diff] [blame] | 308 | |
Hai Pham | 6811b57 | 2023-01-26 21:06:06 +0100 | [diff] [blame] | 309 | case CLK_TYPE_R8A77970_SD0: |
| 310 | return rcar_clk_get_rate64_div_table(core->parent, |
| 311 | gen3_clk_get_rate64(&parent), |
| 312 | priv->base + core->offset, |
| 313 | CPG_SDCKCR_SD0FC_MASK, |
| 314 | r8a77970_cpg_sd0_div_table, "SD"); |
| 315 | |
Hai Pham | e83700a | 2023-01-26 21:06:03 +0100 | [diff] [blame] | 316 | case CLK_TYPE_GEN3_RPCSRC: |
| 317 | return rcar_clk_get_rate64_div_table(core->parent, |
| 318 | gen3_clk_get_rate64(&parent), |
| 319 | priv->base + CPG_RPCCKCR, |
| 320 | CPG_RPCCKCR_DIV_POST_MASK, |
| 321 | cpg_rpcsrc_div_table, "RPCSRC"); |
| 322 | |
Hai Pham | 85e691e | 2023-01-26 21:06:04 +0100 | [diff] [blame] | 323 | case CLK_TYPE_GEN3_D3_RPCSRC: |
| 324 | case CLK_TYPE_GEN3_E3_RPCSRC: |
| 325 | /* |
| 326 | * Register RPCSRC as fixed factor clock based on the |
| 327 | * MD[4:1] pins and CPG_RPCCKCR[4:3] register value for |
| 328 | * which has been set prior to booting the kernel. |
| 329 | */ |
| 330 | value = (readl(priv->base + CPG_RPCCKCR) & GENMASK(4, 3)) >> 3; |
| 331 | |
| 332 | switch (value) { |
| 333 | case 0: |
| 334 | div = 5; |
| 335 | break; |
| 336 | case 1: |
| 337 | div = 3; |
| 338 | break; |
| 339 | case 2: |
| 340 | div = core->div; |
| 341 | break; |
| 342 | case 3: |
| 343 | default: |
| 344 | div = 2; |
| 345 | break; |
| 346 | } |
| 347 | |
| 348 | rate = gen3_clk_get_rate64(&parent) / div; |
| 349 | debug("%s[%i] E3/D3 RPCSRC clk: parent=%i div=%u => rate=%llu\n", |
| 350 | __func__, __LINE__, (core->parent >> 16) & 0xffff, div, rate); |
| 351 | |
| 352 | return rate; |
| 353 | |
Marek Vasut | c1aee32 | 2017-09-15 21:10:29 +0200 | [diff] [blame] | 354 | case CLK_TYPE_GEN3_RPC: |
Marek Vasut | 569acef | 2023-01-26 21:01:56 +0100 | [diff] [blame] | 355 | case CLK_TYPE_GEN4_RPC: |
Hai Pham | 06d8f97 | 2023-01-26 21:06:07 +0100 | [diff] [blame] | 356 | return rcar_clk_get_rate64_rpc(core->parent, |
| 357 | gen3_clk_get_rate64(&parent), |
| 358 | priv->base + CPG_RPCCKCR); |
Marek Vasut | c1aee32 | 2017-09-15 21:10:29 +0200 | [diff] [blame] | 359 | |
Hai Pham | e83700a | 2023-01-26 21:06:03 +0100 | [diff] [blame] | 360 | case CLK_TYPE_GEN3_RPCD2: |
| 361 | case CLK_TYPE_GEN4_RPCD2: |
Hai Pham | 06d8f97 | 2023-01-26 21:06:07 +0100 | [diff] [blame] | 362 | return rcar_clk_get_rate64_rpcd2(core->parent, |
| 363 | gen3_clk_get_rate64(&parent)); |
Marek Vasut | c1aee32 | 2017-09-15 21:10:29 +0200 | [diff] [blame] | 364 | |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | printf("%s[%i] unknown fail\n", __func__, __LINE__); |
| 368 | |
| 369 | return -ENOENT; |
| 370 | } |
| 371 | |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 372 | static ulong gen3_clk_get_rate(struct clk *clk) |
| 373 | { |
| 374 | return gen3_clk_get_rate64(clk); |
| 375 | } |
| 376 | |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 377 | static ulong gen3_clk_set_rate(struct clk *clk, ulong rate) |
| 378 | { |
Marek Vasut | 414dbbe | 2018-01-11 16:28:31 +0100 | [diff] [blame] | 379 | /* Force correct SD-IF divider configuration if applicable */ |
Marek Vasut | c26bf89 | 2018-10-30 17:54:20 +0100 | [diff] [blame] | 380 | gen3_clk_setup_sdif_div(clk, rate); |
Marek Vasut | 7571ac4 | 2018-05-31 19:06:02 +0200 | [diff] [blame] | 381 | return gen3_clk_get_rate64(clk); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static int gen3_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args) |
| 385 | { |
| 386 | if (args->args_count != 2) { |
Sean Anderson | a1b654b | 2021-12-01 14:26:53 -0500 | [diff] [blame] | 387 | debug("Invalid args_count: %d\n", args->args_count); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 388 | return -EINVAL; |
| 389 | } |
| 390 | |
| 391 | clk->id = (args->args[0] << 16) | args->args[1]; |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 396 | const struct clk_ops gen3_clk_ops = { |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 397 | .enable = gen3_clk_enable, |
| 398 | .disable = gen3_clk_disable, |
| 399 | .get_rate = gen3_clk_get_rate, |
| 400 | .set_rate = gen3_clk_set_rate, |
| 401 | .of_xlate = gen3_clk_of_xlate, |
| 402 | }; |
| 403 | |
Marek Vasut | f6b3202 | 2023-01-26 21:02:03 +0100 | [diff] [blame] | 404 | static int gen3_clk_probe(struct udevice *dev) |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 405 | { |
| 406 | struct gen3_clk_priv *priv = dev_get_priv(dev); |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 407 | struct cpg_mssr_info *info = |
| 408 | (struct cpg_mssr_info *)dev_get_driver_data(dev); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 409 | fdt_addr_t rst_base; |
| 410 | u32 cpg_mode; |
| 411 | int ret; |
| 412 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 413 | priv->base = dev_read_addr_ptr(dev); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 414 | if (!priv->base) |
| 415 | return -EINVAL; |
| 416 | |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 417 | priv->info = info; |
| 418 | ret = fdt_node_offset_by_compatible(gd->fdt_blob, -1, info->reset_node); |
| 419 | if (ret < 0) |
| 420 | return ret; |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 421 | |
| 422 | rst_base = fdtdec_get_addr(gd->fdt_blob, ret, "reg"); |
| 423 | if (rst_base == FDT_ADDR_T_NONE) |
| 424 | return -EINVAL; |
| 425 | |
Marek Vasut | 814217e | 2021-04-25 21:53:05 +0200 | [diff] [blame] | 426 | cpg_mode = readl(rst_base + info->reset_modemr_offset); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 427 | |
Marek Vasut | 28f9004 | 2018-01-16 19:23:17 +0100 | [diff] [blame] | 428 | priv->cpg_pll_config = |
| 429 | (struct rcar_gen3_cpg_pll_config *)info->get_pll_config(cpg_mode); |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 430 | if (!priv->cpg_pll_config->extal_div) |
| 431 | return -EINVAL; |
| 432 | |
Marek Vasut | 69459b2 | 2018-05-31 19:47:42 +0200 | [diff] [blame] | 433 | priv->sscg = !(cpg_mode & BIT(12)); |
| 434 | |
Hai Pham | 9480346 | 2020-11-05 22:30:37 +0700 | [diff] [blame] | 435 | if (info->reg_layout == CLK_REG_LAYOUT_RCAR_GEN2_AND_GEN3) { |
| 436 | priv->info->status_regs = mstpsr; |
| 437 | priv->info->control_regs = smstpcr; |
| 438 | priv->info->reset_regs = srcr; |
| 439 | priv->info->reset_clear_regs = srstclr; |
Hai Pham | 86d59f3 | 2020-08-11 10:46:34 +0700 | [diff] [blame] | 440 | } else if (info->reg_layout == CLK_REG_LAYOUT_RCAR_V3U) { |
| 441 | priv->info->status_regs = mstpsr_for_v3u; |
| 442 | priv->info->control_regs = mstpcr_for_v3u; |
| 443 | priv->info->reset_regs = srcr_for_v3u; |
| 444 | priv->info->reset_clear_regs = srstclr_for_v3u; |
Hai Pham | 9480346 | 2020-11-05 22:30:37 +0700 | [diff] [blame] | 445 | } else { |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 449 | ret = clk_get_by_name(dev, "extal", &priv->clk_extal); |
| 450 | if (ret < 0) |
| 451 | return ret; |
| 452 | |
Marek Vasut | 4eb4e6e | 2018-01-08 14:01:40 +0100 | [diff] [blame] | 453 | if (info->extalr_node) { |
| 454 | ret = clk_get_by_name(dev, info->extalr_node, &priv->clk_extalr); |
Marek Vasut | fb0aa29 | 2017-10-08 21:09:15 +0200 | [diff] [blame] | 455 | if (ret < 0) |
| 456 | return ret; |
| 457 | } |
Marek Vasut | f3b8bf7 | 2017-07-21 23:18:03 +0200 | [diff] [blame] | 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
Marek Vasut | f6b3202 | 2023-01-26 21:02:03 +0100 | [diff] [blame] | 462 | static int gen3_clk_remove(struct udevice *dev) |
Marek Vasut | df6a114 | 2017-11-25 22:08:55 +0100 | [diff] [blame] | 463 | { |
| 464 | struct gen3_clk_priv *priv = dev_get_priv(dev); |
Marek Vasut | df6a114 | 2017-11-25 22:08:55 +0100 | [diff] [blame] | 465 | |
Marek Vasut | e11008b | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 466 | return renesas_clk_remove(priv->base, priv->info); |
Marek Vasut | df6a114 | 2017-11-25 22:08:55 +0100 | [diff] [blame] | 467 | } |
Marek Vasut | f6b3202 | 2023-01-26 21:02:03 +0100 | [diff] [blame] | 468 | |
| 469 | U_BOOT_DRIVER(clk_gen3) = { |
| 470 | .name = "clk_gen3", |
| 471 | .id = UCLASS_CLK, |
| 472 | .priv_auto = sizeof(struct gen3_clk_priv), |
| 473 | .ops = &gen3_clk_ops, |
| 474 | .probe = gen3_clk_probe, |
| 475 | .remove = gen3_clk_remove, |
| 476 | }; |
| 477 | |
| 478 | static int gen3_reset_assert(struct reset_ctl *reset_ctl) |
| 479 | { |
| 480 | struct udevice *cdev = (struct udevice *)dev_get_driver_data(reset_ctl->dev); |
| 481 | struct gen3_clk_priv *priv = dev_get_priv(cdev); |
Marek Vasut | ef5c0b1 | 2023-03-18 12:30:53 +0100 | [diff] [blame^] | 482 | unsigned int packed_id = MOD_CLK_PACK(reset_ctl->id); |
| 483 | unsigned int reg = packed_id / 32; |
| 484 | unsigned int bit = packed_id % 32; |
Marek Vasut | f6b3202 | 2023-01-26 21:02:03 +0100 | [diff] [blame] | 485 | u32 bitmask = BIT(bit); |
| 486 | |
| 487 | writel(bitmask, priv->base + priv->info->reset_regs[reg]); |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static int gen3_reset_deassert(struct reset_ctl *reset_ctl) |
| 493 | { |
| 494 | struct udevice *cdev = (struct udevice *)dev_get_driver_data(reset_ctl->dev); |
| 495 | struct gen3_clk_priv *priv = dev_get_priv(cdev); |
Marek Vasut | ef5c0b1 | 2023-03-18 12:30:53 +0100 | [diff] [blame^] | 496 | unsigned int packed_id = MOD_CLK_PACK(reset_ctl->id); |
| 497 | unsigned int reg = packed_id / 32; |
| 498 | unsigned int bit = packed_id % 32; |
Marek Vasut | f6b3202 | 2023-01-26 21:02:03 +0100 | [diff] [blame] | 499 | u32 bitmask = BIT(bit); |
| 500 | |
| 501 | writel(bitmask, priv->base + priv->info->reset_clear_regs[reg]); |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static const struct reset_ops rst_gen3_ops = { |
| 507 | .rst_assert = gen3_reset_assert, |
| 508 | .rst_deassert = gen3_reset_deassert, |
| 509 | }; |
| 510 | |
| 511 | U_BOOT_DRIVER(rst_gen3) = { |
| 512 | .name = "rst_gen3", |
| 513 | .id = UCLASS_RESET, |
| 514 | .ops = &rst_gen3_ops, |
| 515 | }; |
| 516 | |
| 517 | int gen3_cpg_bind(struct udevice *parent) |
| 518 | { |
| 519 | struct cpg_mssr_info *info = |
| 520 | (struct cpg_mssr_info *)dev_get_driver_data(parent); |
| 521 | struct udevice *cdev, *rdev; |
| 522 | struct driver *drv; |
| 523 | int ret; |
| 524 | |
| 525 | drv = lists_driver_lookup_name("clk_gen3"); |
| 526 | if (!drv) |
| 527 | return -ENOENT; |
| 528 | |
| 529 | ret = device_bind_with_driver_data(parent, drv, "clk_gen3", (ulong)info, |
| 530 | dev_ofnode(parent), &cdev); |
| 531 | if (ret) |
| 532 | return ret; |
| 533 | |
| 534 | drv = lists_driver_lookup_name("rst_gen3"); |
| 535 | if (!drv) |
| 536 | return -ENOENT; |
| 537 | |
| 538 | ret = device_bind_with_driver_data(parent, drv, "rst_gen3", (ulong)cdev, |
| 539 | dev_ofnode(parent), &rdev); |
| 540 | if (ret) |
| 541 | device_unbind(cdev); |
| 542 | |
| 543 | return ret; |
| 544 | } |