Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Samsung Exynos7420 clock driver. |
| 4 | * Copyright (C) 2016 Samsung Electronics |
| 5 | * Thomas Abraham <thomas.ab@samsung.com> |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 10 | #include <clk-uclass.h> |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 11 | #include <asm/io.h> |
Sam Protsenko | 8bcc884 | 2024-01-10 21:09:02 -0600 | [diff] [blame] | 12 | #include <div64.h> |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 13 | #include <dt-bindings/clock/exynos7420-clk.h> |
Sam Protsenko | 8bcc884 | 2024-01-10 21:09:02 -0600 | [diff] [blame] | 14 | |
| 15 | #define PLL145X_MDIV_SHIFT 16 |
| 16 | #define PLL145X_MDIV_MASK 0x3ff |
| 17 | #define PLL145X_PDIV_SHIFT 8 |
| 18 | #define PLL145X_PDIV_MASK 0x3f |
| 19 | #define PLL145X_SDIV_SHIFT 0 |
| 20 | #define PLL145X_SDIV_MASK 0x7 |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 21 | |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 22 | #define DIVIDER(reg, shift, mask) \ |
| 23 | (((readl(reg) >> shift) & mask) + 1) |
| 24 | |
| 25 | /* CMU TOPC block device structure */ |
| 26 | struct exynos7420_clk_cmu_topc { |
| 27 | unsigned int rsvd1[68]; |
| 28 | unsigned int bus0_pll_con[2]; |
| 29 | unsigned int rsvd2[2]; |
| 30 | unsigned int bus1_pll_con[2]; |
| 31 | unsigned int rsvd3[54]; |
| 32 | unsigned int mux_sel[6]; |
| 33 | unsigned int rsvd4[250]; |
| 34 | unsigned int div[4]; |
| 35 | }; |
| 36 | |
| 37 | /* CMU TOP0 block device structure */ |
| 38 | struct exynos7420_clk_cmu_top0 { |
| 39 | unsigned int rsvd0[128]; |
| 40 | unsigned int mux_sel[7]; |
| 41 | unsigned int rsvd1[261]; |
| 42 | unsigned int div_peric[5]; |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * struct exynos7420_clk_topc_priv - private data for CMU topc clock driver. |
| 47 | * |
| 48 | * @topc: base address of the memory mapped CMU TOPC controller. |
| 49 | * @fin_freq: frequency of the Oscillator clock. |
| 50 | * @sclk_bus0_pll_a: frequency of sclk_bus0_pll_a clock. |
| 51 | * @sclk_bus1_pll_a: frequency of sclk_bus1_pll_a clock. |
| 52 | */ |
| 53 | struct exynos7420_clk_topc_priv { |
| 54 | struct exynos7420_clk_cmu_topc *topc; |
| 55 | unsigned long fin_freq; |
| 56 | unsigned long sclk_bus0_pll_a; |
| 57 | unsigned long sclk_bus1_pll_a; |
| 58 | }; |
| 59 | |
| 60 | /** |
| 61 | * struct exynos7420_clk_top0_priv - private data for CMU top0 clock driver. |
| 62 | * |
| 63 | * @top0: base address of the memory mapped CMU TOP0 controller. |
| 64 | * @mout_top0_bus0_pll_half: frequency of mout_top0_bus0_pll_half clock |
| 65 | * @sclk_uart2: frequency of sclk_uart2 clock. |
| 66 | */ |
| 67 | struct exynos7420_clk_top0_priv { |
| 68 | struct exynos7420_clk_cmu_top0 *top0; |
| 69 | unsigned long mout_top0_bus0_pll_half; |
| 70 | unsigned long sclk_uart2; |
| 71 | }; |
| 72 | |
Sam Protsenko | 8bcc884 | 2024-01-10 21:09:02 -0600 | [diff] [blame] | 73 | static unsigned long pll145x_get_rate(unsigned int *con1, |
| 74 | unsigned long fin_freq) |
| 75 | { |
| 76 | unsigned long pll_con1 = readl(con1); |
| 77 | unsigned long mdiv, sdiv, pdiv; |
| 78 | u64 fvco = fin_freq; |
| 79 | |
| 80 | mdiv = (pll_con1 >> PLL145X_MDIV_SHIFT) & PLL145X_MDIV_MASK; |
| 81 | pdiv = (pll_con1 >> PLL145X_PDIV_SHIFT) & PLL145X_PDIV_MASK; |
| 82 | sdiv = (pll_con1 >> PLL145X_SDIV_SHIFT) & PLL145X_SDIV_MASK; |
| 83 | |
| 84 | fvco *= mdiv; |
| 85 | do_div(fvco, (pdiv << sdiv)); |
| 86 | return (unsigned long)fvco; |
| 87 | } |
| 88 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 89 | static ulong exynos7420_topc_get_rate(struct clk *clk) |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 90 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 91 | struct exynos7420_clk_topc_priv *priv = dev_get_priv(clk->dev); |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 92 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 93 | switch (clk->id) { |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 94 | case DOUT_SCLK_BUS0_PLL: |
| 95 | case SCLK_BUS0_PLL_A: |
| 96 | case SCLK_BUS0_PLL_B: |
| 97 | return priv->sclk_bus0_pll_a; |
| 98 | case DOUT_SCLK_BUS1_PLL: |
| 99 | case SCLK_BUS1_PLL_A: |
| 100 | case SCLK_BUS1_PLL_B: |
| 101 | return priv->sclk_bus1_pll_a; |
| 102 | default: |
| 103 | return 0; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | static struct clk_ops exynos7420_clk_topc_ops = { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 108 | .get_rate = exynos7420_topc_get_rate, |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | static int exynos7420_clk_topc_probe(struct udevice *dev) |
| 112 | { |
| 113 | struct exynos7420_clk_topc_priv *priv = dev_get_priv(dev); |
| 114 | struct exynos7420_clk_cmu_topc *topc; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 115 | struct clk in_clk; |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 116 | unsigned long rate; |
| 117 | fdt_addr_t base; |
| 118 | int ret; |
| 119 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 120 | base = dev_read_addr(dev); |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 121 | if (base == FDT_ADDR_T_NONE) |
| 122 | return -EINVAL; |
| 123 | |
| 124 | topc = (struct exynos7420_clk_cmu_topc *)base; |
| 125 | priv->topc = topc; |
| 126 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 127 | ret = clk_get_by_index(dev, 0, &in_clk); |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 128 | if (ret >= 0) |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 129 | priv->fin_freq = clk_get_rate(&in_clk); |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 130 | |
| 131 | rate = pll145x_get_rate(&topc->bus0_pll_con[0], priv->fin_freq); |
| 132 | if (readl(&topc->mux_sel[1]) & (1 << 16)) |
| 133 | rate >>= 1; |
| 134 | rate /= DIVIDER(&topc->div[3], 0, 0xf); |
| 135 | priv->sclk_bus0_pll_a = rate; |
| 136 | |
| 137 | rate = pll145x_get_rate(&topc->bus1_pll_con[0], priv->fin_freq) / |
| 138 | DIVIDER(&topc->div[3], 8, 0xf); |
| 139 | priv->sclk_bus1_pll_a = rate; |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 144 | static ulong exynos7420_top0_get_rate(struct clk *clk) |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 145 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 146 | struct exynos7420_clk_top0_priv *priv = dev_get_priv(clk->dev); |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 147 | struct exynos7420_clk_cmu_top0 *top0 = priv->top0; |
| 148 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 149 | switch (clk->id) { |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 150 | case CLK_SCLK_UART2: |
| 151 | return priv->mout_top0_bus0_pll_half / |
| 152 | DIVIDER(&top0->div_peric[3], 8, 0xf); |
| 153 | default: |
| 154 | return 0; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static struct clk_ops exynos7420_clk_top0_ops = { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 159 | .get_rate = exynos7420_top0_get_rate, |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | static int exynos7420_clk_top0_probe(struct udevice *dev) |
| 163 | { |
| 164 | struct exynos7420_clk_top0_priv *priv; |
| 165 | struct exynos7420_clk_cmu_top0 *top0; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 166 | struct clk in_clk; |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 167 | fdt_addr_t base; |
| 168 | int ret; |
| 169 | |
| 170 | priv = dev_get_priv(dev); |
| 171 | if (!priv) |
| 172 | return -EINVAL; |
| 173 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 174 | base = dev_read_addr(dev); |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 175 | if (base == FDT_ADDR_T_NONE) |
| 176 | return -EINVAL; |
| 177 | |
| 178 | top0 = (struct exynos7420_clk_cmu_top0 *)base; |
| 179 | priv->top0 = top0; |
| 180 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 181 | ret = clk_get_by_index(dev, 1, &in_clk); |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 182 | if (ret >= 0) { |
| 183 | priv->mout_top0_bus0_pll_half = |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 184 | clk_get_rate(&in_clk); |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 185 | if (readl(&top0->mux_sel[1]) & (1 << 16)) |
| 186 | priv->mout_top0_bus0_pll_half >>= 1; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 192 | static ulong exynos7420_peric1_get_rate(struct clk *clk) |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 193 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 194 | struct clk in_clk; |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 195 | unsigned int ret; |
| 196 | unsigned long freq = 0; |
| 197 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 198 | switch (clk->id) { |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 199 | case SCLK_UART2: |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 200 | ret = clk_get_by_index(clk->dev, 3, &in_clk); |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 201 | if (ret < 0) |
| 202 | return ret; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 203 | freq = clk_get_rate(&in_clk); |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | |
| 207 | return freq; |
| 208 | } |
| 209 | |
| 210 | static struct clk_ops exynos7420_clk_peric1_ops = { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 211 | .get_rate = exynos7420_peric1_get_rate, |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | static const struct udevice_id exynos7420_clk_topc_compat[] = { |
| 215 | { .compatible = "samsung,exynos7-clock-topc" }, |
| 216 | { } |
| 217 | }; |
| 218 | |
| 219 | U_BOOT_DRIVER(exynos7420_clk_topc) = { |
| 220 | .name = "exynos7420-clock-topc", |
| 221 | .id = UCLASS_CLK, |
| 222 | .of_match = exynos7420_clk_topc_compat, |
| 223 | .probe = exynos7420_clk_topc_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 224 | .priv_auto = sizeof(struct exynos7420_clk_topc_priv), |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 225 | .ops = &exynos7420_clk_topc_ops, |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | static const struct udevice_id exynos7420_clk_top0_compat[] = { |
| 229 | { .compatible = "samsung,exynos7-clock-top0" }, |
| 230 | { } |
| 231 | }; |
| 232 | |
| 233 | U_BOOT_DRIVER(exynos7420_clk_top0) = { |
| 234 | .name = "exynos7420-clock-top0", |
| 235 | .id = UCLASS_CLK, |
| 236 | .of_match = exynos7420_clk_top0_compat, |
| 237 | .probe = exynos7420_clk_top0_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 238 | .priv_auto = sizeof(struct exynos7420_clk_top0_priv), |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 239 | .ops = &exynos7420_clk_top0_ops, |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | static const struct udevice_id exynos7420_clk_peric1_compat[] = { |
| 243 | { .compatible = "samsung,exynos7-clock-peric1" }, |
| 244 | { } |
| 245 | }; |
| 246 | |
| 247 | U_BOOT_DRIVER(exynos7420_clk_peric1) = { |
| 248 | .name = "exynos7420-clock-peric1", |
| 249 | .id = UCLASS_CLK, |
| 250 | .of_match = exynos7420_clk_peric1_compat, |
| 251 | .ops = &exynos7420_clk_peric1_ops, |
Thomas Abraham | e3fc84c | 2016-04-23 22:18:09 +0530 | [diff] [blame] | 252 | }; |