Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 3 | * Common clock driver for Actions Semi SoCs. |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2015 Actions Semi Co., Ltd. |
| 6 | * Copyright (C) 2018 Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> |
| 7 | */ |
| 8 | |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 9 | #include <dm.h> |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 10 | #include "clk_owl.h" |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 11 | #include <asm/io.h> |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 12 | #if defined(CONFIG_MACH_S900) |
| 13 | #include <asm/arch-owl/regs_s900.h> |
Amit Singh Tomar | 7c8e84c | 2020-04-19 19:28:28 +0530 | [diff] [blame] | 14 | #include <dt-bindings/clock/actions,s900-cmu.h> |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 15 | #elif defined(CONFIG_MACH_S700) |
| 16 | #include <asm/arch-owl/regs_s700.h> |
| 17 | #include <dt-bindings/clock/actions,s700-cmu.h> |
| 18 | #endif |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 19 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 20 | #include <linux/delay.h> |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 21 | |
Amit Singh Tomar | 1ec12dc | 2021-11-28 17:02:21 +0530 | [diff] [blame] | 22 | #define CMU_DEVCLKEN0_SD0 BIT(22) |
| 23 | |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 24 | void owl_clk_init(struct owl_clk_priv *priv) |
| 25 | { |
| 26 | u32 bus_clk = 0, core_pll, dev_pll; |
| 27 | |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 28 | #if defined(CONFIG_MACH_S900) |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 29 | /* Enable ASSIST_PLL */ |
| 30 | setbits_le32(priv->base + CMU_ASSISTPLL, BIT(0)); |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 31 | udelay(PLL_STABILITY_WAIT_US); |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 32 | #endif |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 33 | |
| 34 | /* Source HOSC to DEV_CLK */ |
| 35 | clrbits_le32(priv->base + CMU_DEVPLL, CMU_DEVPLL_CLK); |
| 36 | |
| 37 | /* Configure BUS_CLK */ |
| 38 | bus_clk |= (CMU_PDBGDIV_DIV | CMU_PERDIV_DIV | CMU_NOCDIV_DIV | |
| 39 | CMU_DMMCLK_SRC | CMU_APBCLK_DIV | CMU_AHBCLK_DIV | |
| 40 | CMU_NOCCLK_SRC | CMU_CORECLK_HOSC); |
| 41 | writel(bus_clk, priv->base + CMU_BUSCLK); |
| 42 | |
| 43 | udelay(PLL_STABILITY_WAIT_US); |
| 44 | |
| 45 | /* Configure CORE_PLL */ |
| 46 | core_pll = readl(priv->base + CMU_COREPLL); |
| 47 | core_pll |= (CMU_COREPLL_EN | CMU_COREPLL_HOSC_EN | CMU_COREPLL_OUT); |
| 48 | writel(core_pll, priv->base + CMU_COREPLL); |
| 49 | |
| 50 | udelay(PLL_STABILITY_WAIT_US); |
| 51 | |
| 52 | /* Configure DEV_PLL */ |
| 53 | dev_pll = readl(priv->base + CMU_DEVPLL); |
| 54 | dev_pll |= (CMU_DEVPLL_EN | CMU_DEVPLL_OUT); |
| 55 | writel(dev_pll, priv->base + CMU_DEVPLL); |
| 56 | |
| 57 | udelay(PLL_STABILITY_WAIT_US); |
| 58 | |
| 59 | /* Source CORE_PLL for CORE_CLK */ |
| 60 | clrsetbits_le32(priv->base + CMU_BUSCLK, CMU_CORECLK_MASK, |
| 61 | CMU_CORECLK_CPLL); |
| 62 | |
| 63 | /* Source DEV_PLL for DEV_CLK */ |
| 64 | setbits_le32(priv->base + CMU_DEVPLL, CMU_DEVPLL_CLK); |
| 65 | |
| 66 | udelay(PLL_STABILITY_WAIT_US); |
| 67 | } |
| 68 | |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 69 | int owl_clk_enable(struct clk *clk) |
| 70 | { |
| 71 | struct owl_clk_priv *priv = dev_get_priv(clk->dev); |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 72 | enum owl_soc model = dev_get_driver_data(clk->dev); |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 73 | |
| 74 | switch (clk->id) { |
Amit Singh Tomar | 7c8e84c | 2020-04-19 19:28:28 +0530 | [diff] [blame] | 75 | case CLK_UART5: |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 76 | if (model != S900) |
| 77 | return -EINVAL; |
| 78 | /* Source HOSC for UART5 interface */ |
| 79 | clrbits_le32(priv->base + CMU_UART5CLK, CMU_UARTCLK_SRC_DEVPLL); |
| 80 | /* Enable UART5 interface clock */ |
| 81 | setbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_UART5); |
| 82 | break; |
| 83 | case CLK_UART3: |
| 84 | if (model != S700) |
| 85 | return -EINVAL; |
| 86 | /* Source HOSC for UART3 interface */ |
| 87 | clrbits_le32(priv->base + CMU_UART3CLK, CMU_UARTCLK_SRC_DEVPLL); |
| 88 | /* Enable UART3 interface clock */ |
| 89 | setbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_UART3); |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 90 | break; |
Amit Singh Tomar | ea4179e | 2020-05-09 19:55:09 +0530 | [diff] [blame] | 91 | case CLK_RMII_REF: |
| 92 | case CLK_ETHERNET: |
| 93 | setbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_ETH); |
| 94 | setbits_le32(priv->base + CMU_ETHERNETPLL, 5); |
| 95 | break; |
Amit Singh Tomar | 1ec12dc | 2021-11-28 17:02:21 +0530 | [diff] [blame] | 96 | case CLK_SD0: |
| 97 | setbits_le32(priv->base + CMU_DEVCLKEN0, CMU_DEVCLKEN0_SD0); |
| 98 | break; |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 99 | default: |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 100 | return -EINVAL; |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | int owl_clk_disable(struct clk *clk) |
| 107 | { |
| 108 | struct owl_clk_priv *priv = dev_get_priv(clk->dev); |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 109 | enum owl_soc model = dev_get_driver_data(clk->dev); |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 110 | |
| 111 | switch (clk->id) { |
Amit Singh Tomar | 7c8e84c | 2020-04-19 19:28:28 +0530 | [diff] [blame] | 112 | case CLK_UART5: |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 113 | if (model != S900) |
| 114 | return -EINVAL; |
| 115 | /* Disable UART5 interface clock */ |
| 116 | clrbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_UART5); |
| 117 | break; |
| 118 | case CLK_UART3: |
| 119 | if (model != S700) |
| 120 | return -EINVAL; |
| 121 | /* Disable UART3 interface clock */ |
| 122 | clrbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_UART3); |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 123 | break; |
Amit Singh Tomar | ea4179e | 2020-05-09 19:55:09 +0530 | [diff] [blame] | 124 | case CLK_RMII_REF: |
| 125 | case CLK_ETHERNET: |
| 126 | clrbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_ETH); |
| 127 | break; |
Amit Singh Tomar | 1ec12dc | 2021-11-28 17:02:21 +0530 | [diff] [blame] | 128 | case CLK_SD0: |
| 129 | clrbits_le32(priv->base + CMU_DEVCLKEN0, CMU_DEVCLKEN0_SD0); |
| 130 | break; |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 131 | default: |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 132 | return -EINVAL; |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Amit Singh Tomar | 1ec12dc | 2021-11-28 17:02:21 +0530 | [diff] [blame] | 138 | static ulong get_sd_parent_rate(struct owl_clk_priv *priv, u32 dev_index) |
| 139 | { |
| 140 | ulong rate; |
| 141 | u32 reg; |
| 142 | |
| 143 | reg = readl(priv->base + (CMU_SD0CLK + dev_index * 0x4)); |
| 144 | /* Clock output of DEV/NAND_PLL |
| 145 | * Range: 48M ~ 756M |
| 146 | * Frequency= PLLCLK * 6 |
| 147 | */ |
| 148 | if (reg & 0x200) |
| 149 | rate = readl(priv->base + CMU_NANDPLL) & 0x7f; |
| 150 | else |
| 151 | rate = readl(priv->base + CMU_DEVPLL) & 0x7f; |
| 152 | |
| 153 | rate *= 6000000; |
| 154 | |
| 155 | return rate; |
| 156 | } |
| 157 | |
| 158 | static ulong owl_get_sd_clk_rate(struct owl_clk_priv *priv, int sd_index) |
| 159 | { |
| 160 | uint div, val; |
| 161 | ulong parent_rate = get_sd_parent_rate(priv, sd_index); |
| 162 | |
| 163 | val = readl(priv->base + (CMU_SD0CLK + sd_index * 0x4)); |
| 164 | div = (val & 0x1f) + 1; |
| 165 | |
| 166 | return (parent_rate / div); |
| 167 | } |
| 168 | |
| 169 | static ulong owl_set_sd_clk_rate(struct owl_clk_priv *priv, ulong rate, |
| 170 | int sd_index) |
| 171 | { |
| 172 | uint div, val; |
| 173 | ulong parent_rate = get_sd_parent_rate(priv, sd_index); |
| 174 | |
| 175 | if (rate == 0) |
| 176 | return rate; |
| 177 | |
| 178 | div = (parent_rate / rate); |
| 179 | |
| 180 | val = readl(priv->base + (CMU_SD0CLK + sd_index * 0x4)); |
| 181 | /* Bits 4..0 is used to program div value and bit 8 to enable |
| 182 | * divide by 128 circuit |
| 183 | */ |
| 184 | val &= ~0x11f; |
| 185 | if (div >= 128) { |
| 186 | div = div / 128; |
| 187 | val |= 0x100; /* enable divide by 128 circuit */ |
| 188 | } |
| 189 | val |= ((div - 1) & 0x1f); |
| 190 | writel(val, priv->base + (CMU_SD0CLK + sd_index * 0x4)); |
| 191 | |
| 192 | return owl_get_sd_clk_rate(priv, 0); |
| 193 | } |
| 194 | |
Amit Singh Tomar | 5433df9 | 2021-11-28 17:02:20 +0530 | [diff] [blame] | 195 | static ulong owl_clk_get_rate(struct clk *clk) |
| 196 | { |
Amit Singh Tomar | 1ec12dc | 2021-11-28 17:02:21 +0530 | [diff] [blame] | 197 | struct owl_clk_priv *priv = dev_get_priv(clk->dev); |
Amit Singh Tomar | 5433df9 | 2021-11-28 17:02:20 +0530 | [diff] [blame] | 198 | ulong rate; |
| 199 | |
| 200 | switch (clk->id) { |
Amit Singh Tomar | 1ec12dc | 2021-11-28 17:02:21 +0530 | [diff] [blame] | 201 | case CLK_SD0: |
| 202 | rate = owl_get_sd_clk_rate(priv, 0); |
| 203 | break; |
Amit Singh Tomar | 5433df9 | 2021-11-28 17:02:20 +0530 | [diff] [blame] | 204 | default: |
| 205 | return -ENOENT; |
| 206 | } |
| 207 | |
| 208 | return rate; |
| 209 | } |
| 210 | |
| 211 | static ulong owl_clk_set_rate(struct clk *clk, ulong rate) |
| 212 | { |
Amit Singh Tomar | 1ec12dc | 2021-11-28 17:02:21 +0530 | [diff] [blame] | 213 | struct owl_clk_priv *priv = dev_get_priv(clk->dev); |
Amit Singh Tomar | 5433df9 | 2021-11-28 17:02:20 +0530 | [diff] [blame] | 214 | ulong new_rate; |
| 215 | |
| 216 | switch (clk->id) { |
Amit Singh Tomar | 1ec12dc | 2021-11-28 17:02:21 +0530 | [diff] [blame] | 217 | case CLK_SD0: |
| 218 | new_rate = owl_set_sd_clk_rate(priv, rate, 0); |
| 219 | break; |
Amit Singh Tomar | 5433df9 | 2021-11-28 17:02:20 +0530 | [diff] [blame] | 220 | default: |
| 221 | return -ENOENT; |
| 222 | } |
| 223 | |
| 224 | return new_rate; |
| 225 | } |
| 226 | |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 227 | static int owl_clk_probe(struct udevice *dev) |
| 228 | { |
| 229 | struct owl_clk_priv *priv = dev_get_priv(dev); |
| 230 | |
| 231 | priv->base = dev_read_addr(dev); |
| 232 | if (priv->base == FDT_ADDR_T_NONE) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | /* setup necessary clocks */ |
| 236 | owl_clk_init(priv); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 241 | static const struct clk_ops owl_clk_ops = { |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 242 | .enable = owl_clk_enable, |
| 243 | .disable = owl_clk_disable, |
Amit Singh Tomar | 5433df9 | 2021-11-28 17:02:20 +0530 | [diff] [blame] | 244 | .get_rate = owl_clk_get_rate, |
| 245 | .set_rate = owl_clk_set_rate, |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | static const struct udevice_id owl_clk_ids[] = { |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 249 | #if defined(CONFIG_MACH_S900) |
| 250 | { .compatible = "actions,s900-cmu", .data = S900 }, |
| 251 | #elif defined(CONFIG_MACH_S700) |
| 252 | { .compatible = "actions,s700-cmu", .data = S700 }, |
| 253 | #endif |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 254 | { } |
| 255 | }; |
| 256 | |
| 257 | U_BOOT_DRIVER(clk_owl) = { |
Amit Singh Tomar | 8821be4 | 2020-04-19 19:28:30 +0530 | [diff] [blame] | 258 | .name = "clk_owl", |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 259 | .id = UCLASS_CLK, |
| 260 | .of_match = owl_clk_ids, |
| 261 | .ops = &owl_clk_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 262 | .priv_auto = sizeof(struct owl_clk_priv), |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 263 | .probe = owl_clk_probe, |
Manivannan Sadhasivam | 91a8513 | 2018-06-14 23:38:35 +0530 | [diff] [blame] | 264 | }; |