Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Renesas RCar Gen2 CPG MSSR driver |
| 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 | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 13 | #include <clk-uclass.h> |
| 14 | #include <dm.h> |
| 15 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 18 | #include <asm/io.h> |
Marek Vasut | b2970fd | 2023-01-26 21:06:02 +0100 | [diff] [blame] | 19 | #include <linux/clk-provider.h> |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 20 | |
| 21 | #include <dt-bindings/clock/renesas-cpg-mssr.h> |
| 22 | |
| 23 | #include "renesas-cpg-mssr.h" |
| 24 | #include "rcar-gen2-cpg.h" |
| 25 | |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 26 | #define CPG_PLL0CR 0x00d8 |
| 27 | #define CPG_SDCKCR 0x0074 |
| 28 | |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 29 | /* SDHI divisors */ |
| 30 | static const struct clk_div_table cpg_sdh_div_table[] = { |
| 31 | { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 }, |
| 32 | { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 }, |
| 33 | { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 }, |
| 34 | }; |
| 35 | |
| 36 | static const struct clk_div_table cpg_sd01_div_table[] = { |
| 37 | { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 }, |
| 38 | { 8, 24 }, { 10, 36 }, { 11, 48 }, { 12, 10 }, |
| 39 | { 0, 0 }, |
| 40 | }; |
| 41 | |
Marek Vasut | 272daa7 | 2019-03-18 05:11:42 +0100 | [diff] [blame] | 42 | static u8 gen2_clk_get_sdh_div(const struct clk_div_table *table, u8 val) |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 43 | { |
Marek Vasut | 272daa7 | 2019-03-18 05:11:42 +0100 | [diff] [blame] | 44 | for (;;) { |
| 45 | if (!(*table).div) |
| 46 | return 0xff; |
| 47 | |
| 48 | if ((*table).val == val) |
| 49 | return (*table).div; |
| 50 | |
| 51 | table++; |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 52 | } |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static int gen2_clk_enable(struct clk *clk) |
| 56 | { |
| 57 | struct gen2_clk_priv *priv = dev_get_priv(clk->dev); |
| 58 | |
Hai Pham | 5460ee0 | 2020-05-22 10:39:04 +0700 | [diff] [blame] | 59 | return renesas_clk_endisable(clk, priv->base, priv->info, true); |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static int gen2_clk_disable(struct clk *clk) |
| 63 | { |
| 64 | struct gen2_clk_priv *priv = dev_get_priv(clk->dev); |
| 65 | |
Hai Pham | 5460ee0 | 2020-05-22 10:39:04 +0700 | [diff] [blame] | 66 | return renesas_clk_endisable(clk, priv->base, priv->info, false); |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static ulong gen2_clk_get_rate(struct clk *clk) |
| 70 | { |
| 71 | struct gen2_clk_priv *priv = dev_get_priv(clk->dev); |
| 72 | struct cpg_mssr_info *info = priv->info; |
| 73 | struct clk parent; |
| 74 | const struct cpg_core_clk *core; |
| 75 | const struct rcar_gen2_cpg_pll_config *pll_config = |
| 76 | priv->cpg_pll_config; |
| 77 | u32 value, mult, div, rate = 0; |
| 78 | int ret; |
| 79 | |
| 80 | debug("%s[%i] Clock: id=%lu\n", __func__, __LINE__, clk->id); |
| 81 | |
| 82 | ret = renesas_clk_get_parent(clk, info, &parent); |
| 83 | if (ret) { |
| 84 | printf("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret); |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | if (renesas_clk_is_mod(clk)) { |
| 89 | rate = gen2_clk_get_rate(&parent); |
| 90 | debug("%s[%i] MOD clk: parent=%lu => rate=%u\n", |
| 91 | __func__, __LINE__, parent.id, rate); |
| 92 | return rate; |
| 93 | } |
| 94 | |
| 95 | ret = renesas_clk_get_core(clk, info, &core); |
| 96 | if (ret) |
| 97 | return ret; |
| 98 | |
| 99 | switch (core->type) { |
| 100 | case CLK_TYPE_IN: |
| 101 | if (core->id == info->clk_extal_id) { |
| 102 | rate = clk_get_rate(&priv->clk_extal); |
| 103 | debug("%s[%i] EXTAL clk: rate=%u\n", |
| 104 | __func__, __LINE__, rate); |
| 105 | return rate; |
| 106 | } |
| 107 | |
| 108 | if (core->id == info->clk_extal_usb_id) { |
| 109 | rate = clk_get_rate(&priv->clk_extal_usb); |
| 110 | debug("%s[%i] EXTALR clk: rate=%u\n", |
| 111 | __func__, __LINE__, rate); |
| 112 | return rate; |
| 113 | } |
| 114 | |
| 115 | return -EINVAL; |
| 116 | |
| 117 | case CLK_TYPE_FF: |
| 118 | rate = (gen2_clk_get_rate(&parent) * core->mult) / core->div; |
Marek Vasut | 31872db | 2019-03-18 05:38:08 +0100 | [diff] [blame] | 119 | debug("%s[%i] FIXED clk: parent=%i mul=%i div=%i => rate=%u\n", |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 120 | __func__, __LINE__, |
| 121 | core->parent, core->mult, core->div, rate); |
| 122 | return rate; |
| 123 | |
| 124 | case CLK_TYPE_DIV6P1: /* DIV6 Clock with 1 parent clock */ |
| 125 | value = (readl(priv->base + core->offset) & 0x3f) + 1; |
| 126 | rate = gen2_clk_get_rate(&parent) / value; |
| 127 | debug("%s[%i] DIV6P1 clk: parent=%i div=%i => rate=%u\n", |
| 128 | __func__, __LINE__, |
| 129 | core->parent, value, rate); |
| 130 | return rate; |
| 131 | |
| 132 | case CLK_TYPE_GEN2_MAIN: |
| 133 | rate = gen2_clk_get_rate(&parent) / pll_config->extal_div; |
| 134 | debug("%s[%i] MAIN clk: parent=%i extal_div=%i => rate=%u\n", |
| 135 | __func__, __LINE__, |
| 136 | core->parent, pll_config->extal_div, rate); |
| 137 | return rate; |
| 138 | |
| 139 | case CLK_TYPE_GEN2_PLL0: |
| 140 | /* |
| 141 | * PLL0 is a configurable multiplier clock except on R-Car |
| 142 | * V2H/E2. Register the PLL0 clock as a fixed factor clock for |
| 143 | * now as there's no generic multiplier clock implementation and |
| 144 | * we currently have no need to change the multiplier value. |
| 145 | */ |
| 146 | mult = pll_config->pll0_mult; |
| 147 | if (!mult) { |
| 148 | value = readl(priv->base + CPG_PLL0CR); |
| 149 | mult = (((value >> 24) & 0x7f) + 1) * 2; |
| 150 | } |
| 151 | |
| 152 | rate = (gen2_clk_get_rate(&parent) * mult) / info->pll0_div; |
| 153 | debug("%s[%i] PLL0 clk: parent=%i mult=%u => rate=%u\n", |
| 154 | __func__, __LINE__, core->parent, mult, rate); |
| 155 | return rate; |
| 156 | |
| 157 | case CLK_TYPE_GEN2_PLL1: |
| 158 | rate = (gen2_clk_get_rate(&parent) * pll_config->pll1_mult) / 2; |
| 159 | debug("%s[%i] PLL1 clk: parent=%i mul=%i => rate=%u\n", |
| 160 | __func__, __LINE__, |
| 161 | core->parent, pll_config->pll1_mult, rate); |
| 162 | return rate; |
| 163 | |
| 164 | case CLK_TYPE_GEN2_PLL3: |
| 165 | rate = gen2_clk_get_rate(&parent) * pll_config->pll3_mult; |
| 166 | debug("%s[%i] PLL3 clk: parent=%i mul=%i => rate=%u\n", |
| 167 | __func__, __LINE__, |
| 168 | core->parent, pll_config->pll3_mult, rate); |
| 169 | return rate; |
| 170 | |
| 171 | case CLK_TYPE_GEN2_SDH: |
| 172 | value = (readl(priv->base + CPG_SDCKCR) >> 8) & 0xf; |
| 173 | div = gen2_clk_get_sdh_div(cpg_sdh_div_table, value); |
| 174 | rate = gen2_clk_get_rate(&parent) / div; |
| 175 | debug("%s[%i] SDH clk: parent=%i div=%i => rate=%u\n", |
| 176 | __func__, __LINE__, |
| 177 | core->parent, div, rate); |
| 178 | return rate; |
| 179 | |
| 180 | case CLK_TYPE_GEN2_SD0: |
| 181 | value = (readl(priv->base + CPG_SDCKCR) >> 4) & 0xf; |
| 182 | div = gen2_clk_get_sdh_div(cpg_sd01_div_table, value); |
| 183 | rate = gen2_clk_get_rate(&parent) / div; |
| 184 | debug("%s[%i] SD0 clk: parent=%i div=%i => rate=%u\n", |
| 185 | __func__, __LINE__, |
| 186 | core->parent, div, rate); |
| 187 | return rate; |
| 188 | |
| 189 | case CLK_TYPE_GEN2_SD1: |
| 190 | value = (readl(priv->base + CPG_SDCKCR) >> 0) & 0xf; |
| 191 | div = gen2_clk_get_sdh_div(cpg_sd01_div_table, value); |
| 192 | rate = gen2_clk_get_rate(&parent) / div; |
| 193 | debug("%s[%i] SD1 clk: parent=%i div=%i => rate=%u\n", |
| 194 | __func__, __LINE__, |
| 195 | core->parent, div, rate); |
| 196 | return rate; |
| 197 | } |
| 198 | |
| 199 | printf("%s[%i] unknown fail\n", __func__, __LINE__); |
| 200 | |
| 201 | return -ENOENT; |
| 202 | } |
| 203 | |
Marek Vasut | 0f6aa07 | 2019-03-18 06:04:02 +0100 | [diff] [blame] | 204 | static int gen2_clk_setup_mmcif_div(struct clk *clk, ulong rate) |
| 205 | { |
| 206 | struct gen2_clk_priv *priv = dev_get_priv(clk->dev); |
| 207 | struct cpg_mssr_info *info = priv->info; |
| 208 | const struct cpg_core_clk *core; |
| 209 | struct clk parent, pparent; |
| 210 | u32 val; |
| 211 | int ret; |
| 212 | |
| 213 | ret = renesas_clk_get_parent(clk, info, &parent); |
| 214 | if (ret) { |
| 215 | debug("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret); |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | if (renesas_clk_is_mod(&parent)) |
| 220 | return 0; |
| 221 | |
| 222 | ret = renesas_clk_get_core(&parent, info, &core); |
| 223 | if (ret) |
| 224 | return ret; |
| 225 | |
| 226 | if (strcmp(core->name, "mmc0") && strcmp(core->name, "mmc1")) |
| 227 | return 0; |
| 228 | |
| 229 | ret = renesas_clk_get_parent(&parent, info, &pparent); |
| 230 | if (ret) { |
| 231 | debug("%s[%i] parent fail, ret=%i\n", __func__, __LINE__, ret); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | val = (gen2_clk_get_rate(&pparent) / rate) - 1; |
| 236 | |
| 237 | debug("%s[%i] MMCIF offset=%x\n", __func__, __LINE__, core->offset); |
| 238 | |
| 239 | writel(val, priv->base + core->offset); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 244 | static ulong gen2_clk_set_rate(struct clk *clk, ulong rate) |
| 245 | { |
Marek Vasut | 0f6aa07 | 2019-03-18 06:04:02 +0100 | [diff] [blame] | 246 | /* Force correct MMC-IF divider configuration if applicable */ |
| 247 | gen2_clk_setup_mmcif_div(clk, rate); |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 248 | return gen2_clk_get_rate(clk); |
| 249 | } |
| 250 | |
| 251 | static int gen2_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args) |
| 252 | { |
| 253 | if (args->args_count != 2) { |
Sean Anderson | a1b654b | 2021-12-01 14:26:53 -0500 | [diff] [blame] | 254 | debug("Invalid args_count: %d\n", args->args_count); |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 255 | return -EINVAL; |
| 256 | } |
| 257 | |
| 258 | clk->id = (args->args[0] << 16) | args->args[1]; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | const struct clk_ops gen2_clk_ops = { |
| 264 | .enable = gen2_clk_enable, |
| 265 | .disable = gen2_clk_disable, |
| 266 | .get_rate = gen2_clk_get_rate, |
| 267 | .set_rate = gen2_clk_set_rate, |
| 268 | .of_xlate = gen2_clk_of_xlate, |
| 269 | }; |
| 270 | |
| 271 | int gen2_clk_probe(struct udevice *dev) |
| 272 | { |
| 273 | struct gen2_clk_priv *priv = dev_get_priv(dev); |
| 274 | struct cpg_mssr_info *info = |
| 275 | (struct cpg_mssr_info *)dev_get_driver_data(dev); |
| 276 | fdt_addr_t rst_base; |
| 277 | u32 cpg_mode; |
| 278 | int ret; |
| 279 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 280 | priv->base = dev_read_addr_ptr(dev); |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 281 | if (!priv->base) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | priv->info = info; |
| 285 | ret = fdt_node_offset_by_compatible(gd->fdt_blob, -1, info->reset_node); |
| 286 | if (ret < 0) |
| 287 | return ret; |
| 288 | |
Marek Vasut | ab11876 | 2020-03-21 16:45:29 +0100 | [diff] [blame] | 289 | rst_base = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, ret, "reg", |
| 290 | 0, NULL, false); |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 291 | if (rst_base == FDT_ADDR_T_NONE) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | cpg_mode = readl(rst_base + CPG_RST_MODEMR); |
| 295 | |
| 296 | priv->cpg_pll_config = |
| 297 | (struct rcar_gen2_cpg_pll_config *)info->get_pll_config(cpg_mode); |
| 298 | if (!priv->cpg_pll_config->extal_div) |
| 299 | return -EINVAL; |
| 300 | |
Niklas Söderlund | d301ed7 | 2024-02-09 22:15:35 +0100 | [diff] [blame^] | 301 | if (info->reg_layout == CLK_REG_LAYOUT_RCAR_GEN2_AND_GEN3) { |
| 302 | priv->info->status_regs = mstpsr; |
| 303 | priv->info->control_regs = smstpcr; |
| 304 | priv->info->reset_regs = srcr; |
| 305 | priv->info->reset_clear_regs = srstclr; |
| 306 | } else { |
| 307 | return -EINVAL; |
| 308 | } |
| 309 | |
Marek Vasut | f63b295 | 2018-01-08 16:38:51 +0100 | [diff] [blame] | 310 | ret = clk_get_by_name(dev, "extal", &priv->clk_extal); |
| 311 | if (ret < 0) |
| 312 | return ret; |
| 313 | |
| 314 | if (info->extal_usb_node) { |
| 315 | ret = clk_get_by_name(dev, info->extal_usb_node, |
| 316 | &priv->clk_extal_usb); |
| 317 | if (ret < 0) |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | int gen2_clk_remove(struct udevice *dev) |
| 325 | { |
| 326 | struct gen2_clk_priv *priv = dev_get_priv(dev); |
| 327 | |
| 328 | return renesas_clk_remove(priv->base, priv->info); |
| 329 | } |