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