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