Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) STMicroelectronics 2018 - All Rights Reserved |
| 4 | * Author: Olivier Bideau <olivier.bideau@st.com> for STMicroelectronics. |
| 5 | * Author: Gabriel Fernandez <gabriel.fernandez@st.com> for STMicroelectronics. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/clk-provider.h> |
Gabriel Fernandez | c6cf4d3 | 2018-03-08 17:53:58 +0100 | [diff] [blame^] | 10 | #include <linux/delay.h> |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 11 | #include <linux/err.h> |
| 12 | #include <linux/io.h> |
| 13 | #include <linux/of.h> |
| 14 | #include <linux/of_address.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | |
| 18 | #include <dt-bindings/clock/stm32mp1-clks.h> |
| 19 | |
| 20 | static DEFINE_SPINLOCK(rlock); |
| 21 | |
| 22 | #define RCC_OCENSETR 0x0C |
| 23 | #define RCC_HSICFGR 0x18 |
| 24 | #define RCC_RDLSICR 0x144 |
| 25 | #define RCC_PLL1CR 0x80 |
| 26 | #define RCC_PLL1CFGR1 0x84 |
| 27 | #define RCC_PLL1CFGR2 0x88 |
| 28 | #define RCC_PLL2CR 0x94 |
| 29 | #define RCC_PLL2CFGR1 0x98 |
| 30 | #define RCC_PLL2CFGR2 0x9C |
| 31 | #define RCC_PLL3CR 0x880 |
| 32 | #define RCC_PLL3CFGR1 0x884 |
| 33 | #define RCC_PLL3CFGR2 0x888 |
| 34 | #define RCC_PLL4CR 0x894 |
| 35 | #define RCC_PLL4CFGR1 0x898 |
| 36 | #define RCC_PLL4CFGR2 0x89C |
| 37 | #define RCC_APB1ENSETR 0xA00 |
| 38 | #define RCC_APB2ENSETR 0xA08 |
| 39 | #define RCC_APB3ENSETR 0xA10 |
| 40 | #define RCC_APB4ENSETR 0x200 |
| 41 | #define RCC_APB5ENSETR 0x208 |
| 42 | #define RCC_AHB2ENSETR 0xA18 |
| 43 | #define RCC_AHB3ENSETR 0xA20 |
| 44 | #define RCC_AHB4ENSETR 0xA28 |
| 45 | #define RCC_AHB5ENSETR 0x210 |
| 46 | #define RCC_AHB6ENSETR 0x218 |
| 47 | #define RCC_AHB6LPENSETR 0x318 |
| 48 | #define RCC_RCK12SELR 0x28 |
| 49 | #define RCC_RCK3SELR 0x820 |
| 50 | #define RCC_RCK4SELR 0x824 |
| 51 | #define RCC_MPCKSELR 0x20 |
| 52 | #define RCC_ASSCKSELR 0x24 |
| 53 | #define RCC_MSSCKSELR 0x48 |
| 54 | #define RCC_SPI6CKSELR 0xC4 |
| 55 | #define RCC_SDMMC12CKSELR 0x8F4 |
| 56 | #define RCC_SDMMC3CKSELR 0x8F8 |
| 57 | #define RCC_FMCCKSELR 0x904 |
| 58 | #define RCC_I2C46CKSELR 0xC0 |
| 59 | #define RCC_I2C12CKSELR 0x8C0 |
| 60 | #define RCC_I2C35CKSELR 0x8C4 |
| 61 | #define RCC_UART1CKSELR 0xC8 |
| 62 | #define RCC_QSPICKSELR 0x900 |
| 63 | #define RCC_ETHCKSELR 0x8FC |
| 64 | #define RCC_RNG1CKSELR 0xCC |
| 65 | #define RCC_RNG2CKSELR 0x920 |
| 66 | #define RCC_GPUCKSELR 0x938 |
| 67 | #define RCC_USBCKSELR 0x91C |
| 68 | #define RCC_STGENCKSELR 0xD4 |
| 69 | #define RCC_SPDIFCKSELR 0x914 |
| 70 | #define RCC_SPI2S1CKSELR 0x8D8 |
| 71 | #define RCC_SPI2S23CKSELR 0x8DC |
| 72 | #define RCC_SPI2S45CKSELR 0x8E0 |
| 73 | #define RCC_CECCKSELR 0x918 |
| 74 | #define RCC_LPTIM1CKSELR 0x934 |
| 75 | #define RCC_LPTIM23CKSELR 0x930 |
| 76 | #define RCC_LPTIM45CKSELR 0x92C |
| 77 | #define RCC_UART24CKSELR 0x8E8 |
| 78 | #define RCC_UART35CKSELR 0x8EC |
| 79 | #define RCC_UART6CKSELR 0x8E4 |
| 80 | #define RCC_UART78CKSELR 0x8F0 |
| 81 | #define RCC_FDCANCKSELR 0x90C |
| 82 | #define RCC_SAI1CKSELR 0x8C8 |
| 83 | #define RCC_SAI2CKSELR 0x8CC |
| 84 | #define RCC_SAI3CKSELR 0x8D0 |
| 85 | #define RCC_SAI4CKSELR 0x8D4 |
| 86 | #define RCC_ADCCKSELR 0x928 |
| 87 | #define RCC_MPCKDIVR 0x2C |
| 88 | #define RCC_DSICKSELR 0x924 |
| 89 | #define RCC_CPERCKSELR 0xD0 |
| 90 | #define RCC_MCO1CFGR 0x800 |
| 91 | #define RCC_MCO2CFGR 0x804 |
| 92 | #define RCC_BDCR 0x140 |
| 93 | #define RCC_AXIDIVR 0x30 |
| 94 | #define RCC_MCUDIVR 0x830 |
| 95 | #define RCC_APB1DIVR 0x834 |
| 96 | #define RCC_APB2DIVR 0x838 |
| 97 | #define RCC_APB3DIVR 0x83C |
| 98 | #define RCC_APB4DIVR 0x3C |
| 99 | #define RCC_APB5DIVR 0x40 |
| 100 | #define RCC_TIMG1PRER 0x828 |
| 101 | #define RCC_TIMG2PRER 0x82C |
| 102 | #define RCC_RTCDIVR 0x44 |
| 103 | #define RCC_DBGCFGR 0x80C |
| 104 | |
| 105 | #define RCC_CLR 0x4 |
| 106 | |
Gabriel Fernandez | dc32eaa | 2018-03-08 17:53:57 +0100 | [diff] [blame] | 107 | static const char * const ref12_parents[] = { |
| 108 | "ck_hsi", "ck_hse" |
| 109 | }; |
| 110 | |
| 111 | static const char * const ref3_parents[] = { |
| 112 | "ck_hsi", "ck_hse", "ck_csi" |
| 113 | }; |
| 114 | |
| 115 | static const char * const ref4_parents[] = { |
| 116 | "ck_hsi", "ck_hse", "ck_csi" |
| 117 | }; |
| 118 | |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 119 | struct clock_config { |
| 120 | u32 id; |
| 121 | const char *name; |
| 122 | union { |
| 123 | const char *parent_name; |
| 124 | const char * const *parent_names; |
| 125 | }; |
| 126 | int num_parents; |
| 127 | unsigned long flags; |
| 128 | void *cfg; |
| 129 | struct clk_hw * (*func)(struct device *dev, |
| 130 | struct clk_hw_onecell_data *clk_data, |
| 131 | void __iomem *base, spinlock_t *lock, |
| 132 | const struct clock_config *cfg); |
| 133 | }; |
| 134 | |
| 135 | #define NO_ID ~0 |
| 136 | |
| 137 | struct gate_cfg { |
| 138 | u32 reg_off; |
| 139 | u8 bit_idx; |
| 140 | u8 gate_flags; |
| 141 | }; |
| 142 | |
| 143 | struct fixed_factor_cfg { |
| 144 | unsigned int mult; |
| 145 | unsigned int div; |
| 146 | }; |
| 147 | |
| 148 | struct div_cfg { |
| 149 | u32 reg_off; |
| 150 | u8 shift; |
| 151 | u8 width; |
| 152 | u8 div_flags; |
| 153 | const struct clk_div_table *table; |
| 154 | }; |
| 155 | |
Gabriel Fernandez | dc32eaa | 2018-03-08 17:53:57 +0100 | [diff] [blame] | 156 | struct mux_cfg { |
| 157 | u32 reg_off; |
| 158 | u8 shift; |
| 159 | u8 width; |
| 160 | u8 mux_flags; |
| 161 | u32 *table; |
| 162 | }; |
| 163 | |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 164 | struct stm32_gate_cfg { |
| 165 | struct gate_cfg *gate; |
| 166 | const struct clk_ops *ops; |
| 167 | }; |
| 168 | |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 169 | static struct clk_hw * |
| 170 | _clk_hw_register_gate(struct device *dev, |
| 171 | struct clk_hw_onecell_data *clk_data, |
| 172 | void __iomem *base, spinlock_t *lock, |
| 173 | const struct clock_config *cfg) |
| 174 | { |
| 175 | struct gate_cfg *gate_cfg = cfg->cfg; |
| 176 | |
| 177 | return clk_hw_register_gate(dev, |
| 178 | cfg->name, |
| 179 | cfg->parent_name, |
| 180 | cfg->flags, |
| 181 | gate_cfg->reg_off + base, |
| 182 | gate_cfg->bit_idx, |
| 183 | gate_cfg->gate_flags, |
| 184 | lock); |
| 185 | } |
| 186 | |
| 187 | static struct clk_hw * |
| 188 | _clk_hw_register_fixed_factor(struct device *dev, |
| 189 | struct clk_hw_onecell_data *clk_data, |
| 190 | void __iomem *base, spinlock_t *lock, |
| 191 | const struct clock_config *cfg) |
| 192 | { |
| 193 | struct fixed_factor_cfg *ff_cfg = cfg->cfg; |
| 194 | |
| 195 | return clk_hw_register_fixed_factor(dev, cfg->name, cfg->parent_name, |
| 196 | cfg->flags, ff_cfg->mult, |
| 197 | ff_cfg->div); |
| 198 | } |
| 199 | |
| 200 | static struct clk_hw * |
| 201 | _clk_hw_register_divider_table(struct device *dev, |
| 202 | struct clk_hw_onecell_data *clk_data, |
| 203 | void __iomem *base, spinlock_t *lock, |
| 204 | const struct clock_config *cfg) |
| 205 | { |
| 206 | struct div_cfg *div_cfg = cfg->cfg; |
| 207 | |
| 208 | return clk_hw_register_divider_table(dev, |
| 209 | cfg->name, |
| 210 | cfg->parent_name, |
| 211 | cfg->flags, |
| 212 | div_cfg->reg_off + base, |
| 213 | div_cfg->shift, |
| 214 | div_cfg->width, |
| 215 | div_cfg->div_flags, |
| 216 | div_cfg->table, |
| 217 | lock); |
| 218 | } |
| 219 | |
Gabriel Fernandez | dc32eaa | 2018-03-08 17:53:57 +0100 | [diff] [blame] | 220 | static struct clk_hw * |
| 221 | _clk_hw_register_mux(struct device *dev, |
| 222 | struct clk_hw_onecell_data *clk_data, |
| 223 | void __iomem *base, spinlock_t *lock, |
| 224 | const struct clock_config *cfg) |
| 225 | { |
| 226 | struct mux_cfg *mux_cfg = cfg->cfg; |
| 227 | |
| 228 | return clk_hw_register_mux(dev, cfg->name, cfg->parent_names, |
| 229 | cfg->num_parents, cfg->flags, |
| 230 | mux_cfg->reg_off + base, mux_cfg->shift, |
| 231 | mux_cfg->width, mux_cfg->mux_flags, lock); |
| 232 | } |
| 233 | |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 234 | /* MP1 Gate clock with set & clear registers */ |
| 235 | |
| 236 | static int mp1_gate_clk_enable(struct clk_hw *hw) |
| 237 | { |
| 238 | if (!clk_gate_ops.is_enabled(hw)) |
| 239 | clk_gate_ops.enable(hw); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static void mp1_gate_clk_disable(struct clk_hw *hw) |
| 245 | { |
| 246 | struct clk_gate *gate = to_clk_gate(hw); |
| 247 | unsigned long flags = 0; |
| 248 | |
| 249 | if (clk_gate_ops.is_enabled(hw)) { |
| 250 | spin_lock_irqsave(gate->lock, flags); |
| 251 | writel_relaxed(BIT(gate->bit_idx), gate->reg + RCC_CLR); |
| 252 | spin_unlock_irqrestore(gate->lock, flags); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | const struct clk_ops mp1_gate_clk_ops = { |
| 257 | .enable = mp1_gate_clk_enable, |
| 258 | .disable = mp1_gate_clk_disable, |
| 259 | .is_enabled = clk_gate_is_enabled, |
| 260 | }; |
| 261 | |
| 262 | static struct clk_hw * |
| 263 | _get_stm32_gate(void __iomem *base, |
| 264 | const struct stm32_gate_cfg *cfg, spinlock_t *lock) |
| 265 | { |
| 266 | struct clk_gate *gate; |
| 267 | struct clk_hw *gate_hw; |
| 268 | |
| 269 | gate = kzalloc(sizeof(*gate), GFP_KERNEL); |
| 270 | if (!gate) |
| 271 | return ERR_PTR(-ENOMEM); |
| 272 | |
| 273 | gate->reg = cfg->gate->reg_off + base; |
| 274 | gate->bit_idx = cfg->gate->bit_idx; |
| 275 | gate->flags = cfg->gate->gate_flags; |
| 276 | gate->lock = lock; |
| 277 | gate_hw = &gate->hw; |
| 278 | |
| 279 | return gate_hw; |
| 280 | } |
| 281 | |
| 282 | static struct clk_hw * |
| 283 | clk_stm32_register_gate_ops(struct device *dev, |
| 284 | const char *name, |
| 285 | const char *parent_name, |
| 286 | unsigned long flags, |
| 287 | void __iomem *base, |
| 288 | const struct stm32_gate_cfg *cfg, |
| 289 | spinlock_t *lock) |
| 290 | { |
| 291 | struct clk_init_data init = { NULL }; |
| 292 | struct clk_gate *gate; |
| 293 | struct clk_hw *hw; |
| 294 | int ret; |
| 295 | |
| 296 | gate = kzalloc(sizeof(*gate), GFP_KERNEL); |
| 297 | if (!gate) |
| 298 | return ERR_PTR(-ENOMEM); |
| 299 | |
| 300 | init.name = name; |
| 301 | init.parent_names = &parent_name; |
| 302 | init.num_parents = 1; |
| 303 | init.flags = flags; |
| 304 | |
| 305 | init.ops = &clk_gate_ops; |
| 306 | |
| 307 | if (cfg->ops) |
| 308 | init.ops = cfg->ops; |
| 309 | |
| 310 | hw = _get_stm32_gate(base, cfg, lock); |
| 311 | if (IS_ERR(hw)) |
| 312 | return ERR_PTR(-ENOMEM); |
| 313 | |
| 314 | hw->init = &init; |
| 315 | |
| 316 | ret = clk_hw_register(dev, hw); |
| 317 | if (ret) { |
| 318 | kfree(gate); |
| 319 | hw = ERR_PTR(ret); |
| 320 | } |
| 321 | |
| 322 | return hw; |
| 323 | } |
| 324 | |
Gabriel Fernandez | c6cf4d3 | 2018-03-08 17:53:58 +0100 | [diff] [blame^] | 325 | /* STM32 PLL */ |
| 326 | |
| 327 | struct stm32_pll_obj { |
| 328 | /* lock pll enable/disable registers */ |
| 329 | spinlock_t *lock; |
| 330 | void __iomem *reg; |
| 331 | struct clk_hw hw; |
| 332 | }; |
| 333 | |
| 334 | #define to_pll(_hw) container_of(_hw, struct stm32_pll_obj, hw) |
| 335 | |
| 336 | #define PLL_ON BIT(0) |
| 337 | #define PLL_RDY BIT(1) |
| 338 | #define DIVN_MASK 0x1FF |
| 339 | #define DIVM_MASK 0x3F |
| 340 | #define DIVM_SHIFT 16 |
| 341 | #define DIVN_SHIFT 0 |
| 342 | #define FRAC_OFFSET 0xC |
| 343 | #define FRAC_MASK 0x1FFF |
| 344 | #define FRAC_SHIFT 3 |
| 345 | #define FRACLE BIT(16) |
| 346 | |
| 347 | static int __pll_is_enabled(struct clk_hw *hw) |
| 348 | { |
| 349 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 350 | |
| 351 | return readl_relaxed(clk_elem->reg) & PLL_ON; |
| 352 | } |
| 353 | |
| 354 | #define TIMEOUT 5 |
| 355 | |
| 356 | static int pll_enable(struct clk_hw *hw) |
| 357 | { |
| 358 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 359 | u32 reg; |
| 360 | unsigned long flags = 0; |
| 361 | unsigned int timeout = TIMEOUT; |
| 362 | int bit_status = 0; |
| 363 | |
| 364 | spin_lock_irqsave(clk_elem->lock, flags); |
| 365 | |
| 366 | if (__pll_is_enabled(hw)) |
| 367 | goto unlock; |
| 368 | |
| 369 | reg = readl_relaxed(clk_elem->reg); |
| 370 | reg |= PLL_ON; |
| 371 | writel_relaxed(reg, clk_elem->reg); |
| 372 | |
| 373 | /* We can't use readl_poll_timeout() because we can be blocked if |
| 374 | * someone enables this clock before clocksource changes. |
| 375 | * Only jiffies counter is available. Jiffies are incremented by |
| 376 | * interruptions and enable op does not allow to be interrupted. |
| 377 | */ |
| 378 | do { |
| 379 | bit_status = !(readl_relaxed(clk_elem->reg) & PLL_RDY); |
| 380 | |
| 381 | if (bit_status) |
| 382 | udelay(120); |
| 383 | |
| 384 | } while (bit_status && --timeout); |
| 385 | |
| 386 | unlock: |
| 387 | spin_unlock_irqrestore(clk_elem->lock, flags); |
| 388 | |
| 389 | return bit_status; |
| 390 | } |
| 391 | |
| 392 | static void pll_disable(struct clk_hw *hw) |
| 393 | { |
| 394 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 395 | u32 reg; |
| 396 | unsigned long flags = 0; |
| 397 | |
| 398 | spin_lock_irqsave(clk_elem->lock, flags); |
| 399 | |
| 400 | reg = readl_relaxed(clk_elem->reg); |
| 401 | reg &= ~PLL_ON; |
| 402 | writel_relaxed(reg, clk_elem->reg); |
| 403 | |
| 404 | spin_unlock_irqrestore(clk_elem->lock, flags); |
| 405 | } |
| 406 | |
| 407 | static u32 pll_frac_val(struct clk_hw *hw) |
| 408 | { |
| 409 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 410 | u32 reg, frac = 0; |
| 411 | |
| 412 | reg = readl_relaxed(clk_elem->reg + FRAC_OFFSET); |
| 413 | if (reg & FRACLE) |
| 414 | frac = (reg >> FRAC_SHIFT) & FRAC_MASK; |
| 415 | |
| 416 | return frac; |
| 417 | } |
| 418 | |
| 419 | static unsigned long pll_recalc_rate(struct clk_hw *hw, |
| 420 | unsigned long parent_rate) |
| 421 | { |
| 422 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 423 | u32 reg; |
| 424 | u32 frac, divm, divn; |
| 425 | u64 rate, rate_frac = 0; |
| 426 | |
| 427 | reg = readl_relaxed(clk_elem->reg + 4); |
| 428 | |
| 429 | divm = ((reg >> DIVM_SHIFT) & DIVM_MASK) + 1; |
| 430 | divn = ((reg >> DIVN_SHIFT) & DIVN_MASK) + 1; |
| 431 | rate = (u64)parent_rate * divn; |
| 432 | |
| 433 | do_div(rate, divm); |
| 434 | |
| 435 | frac = pll_frac_val(hw); |
| 436 | if (frac) { |
| 437 | rate_frac = (u64)parent_rate * (u64)frac; |
| 438 | do_div(rate_frac, (divm * 8192)); |
| 439 | } |
| 440 | |
| 441 | return rate + rate_frac; |
| 442 | } |
| 443 | |
| 444 | static int pll_is_enabled(struct clk_hw *hw) |
| 445 | { |
| 446 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 447 | unsigned long flags = 0; |
| 448 | int ret; |
| 449 | |
| 450 | spin_lock_irqsave(clk_elem->lock, flags); |
| 451 | ret = __pll_is_enabled(hw); |
| 452 | spin_unlock_irqrestore(clk_elem->lock, flags); |
| 453 | |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | static const struct clk_ops pll_ops = { |
| 458 | .enable = pll_enable, |
| 459 | .disable = pll_disable, |
| 460 | .recalc_rate = pll_recalc_rate, |
| 461 | .is_enabled = pll_is_enabled, |
| 462 | }; |
| 463 | |
| 464 | static struct clk_hw *clk_register_pll(struct device *dev, const char *name, |
| 465 | const char *parent_name, |
| 466 | void __iomem *reg, |
| 467 | unsigned long flags, |
| 468 | spinlock_t *lock) |
| 469 | { |
| 470 | struct stm32_pll_obj *element; |
| 471 | struct clk_init_data init; |
| 472 | struct clk_hw *hw; |
| 473 | int err; |
| 474 | |
| 475 | element = kzalloc(sizeof(*element), GFP_KERNEL); |
| 476 | if (!element) |
| 477 | return ERR_PTR(-ENOMEM); |
| 478 | |
| 479 | init.name = name; |
| 480 | init.ops = &pll_ops; |
| 481 | init.flags = flags; |
| 482 | init.parent_names = &parent_name; |
| 483 | init.num_parents = 1; |
| 484 | |
| 485 | element->hw.init = &init; |
| 486 | element->reg = reg; |
| 487 | element->lock = lock; |
| 488 | |
| 489 | hw = &element->hw; |
| 490 | err = clk_hw_register(dev, hw); |
| 491 | |
| 492 | if (err) { |
| 493 | kfree(element); |
| 494 | return ERR_PTR(err); |
| 495 | } |
| 496 | |
| 497 | return hw; |
| 498 | } |
| 499 | |
| 500 | struct stm32_pll_cfg { |
| 501 | u32 offset; |
| 502 | }; |
| 503 | |
| 504 | struct clk_hw *_clk_register_pll(struct device *dev, |
| 505 | struct clk_hw_onecell_data *clk_data, |
| 506 | void __iomem *base, spinlock_t *lock, |
| 507 | const struct clock_config *cfg) |
| 508 | { |
| 509 | struct stm32_pll_cfg *stm_pll_cfg = cfg->cfg; |
| 510 | |
| 511 | return clk_register_pll(dev, cfg->name, cfg->parent_name, |
| 512 | base + stm_pll_cfg->offset, cfg->flags, lock); |
| 513 | } |
| 514 | |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 515 | static struct clk_hw * |
| 516 | _clk_stm32_register_gate(struct device *dev, |
| 517 | struct clk_hw_onecell_data *clk_data, |
| 518 | void __iomem *base, spinlock_t *lock, |
| 519 | const struct clock_config *cfg) |
| 520 | { |
| 521 | return clk_stm32_register_gate_ops(dev, |
| 522 | cfg->name, |
| 523 | cfg->parent_name, |
| 524 | cfg->flags, |
| 525 | base, |
| 526 | cfg->cfg, |
| 527 | lock); |
| 528 | } |
| 529 | |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 530 | #define GATE(_id, _name, _parent, _flags, _offset, _bit_idx, _gate_flags)\ |
| 531 | {\ |
| 532 | .id = _id,\ |
| 533 | .name = _name,\ |
| 534 | .parent_name = _parent,\ |
| 535 | .flags = _flags,\ |
| 536 | .cfg = &(struct gate_cfg) {\ |
| 537 | .reg_off = _offset,\ |
| 538 | .bit_idx = _bit_idx,\ |
| 539 | .gate_flags = _gate_flags,\ |
| 540 | },\ |
| 541 | .func = _clk_hw_register_gate,\ |
| 542 | } |
| 543 | |
| 544 | #define FIXED_FACTOR(_id, _name, _parent, _flags, _mult, _div)\ |
| 545 | {\ |
| 546 | .id = _id,\ |
| 547 | .name = _name,\ |
| 548 | .parent_name = _parent,\ |
| 549 | .flags = _flags,\ |
| 550 | .cfg = &(struct fixed_factor_cfg) {\ |
| 551 | .mult = _mult,\ |
| 552 | .div = _div,\ |
| 553 | },\ |
| 554 | .func = _clk_hw_register_fixed_factor,\ |
| 555 | } |
| 556 | |
| 557 | #define DIV_TABLE(_id, _name, _parent, _flags, _offset, _shift, _width,\ |
| 558 | _div_flags, _div_table)\ |
| 559 | {\ |
| 560 | .id = _id,\ |
| 561 | .name = _name,\ |
| 562 | .parent_name = _parent,\ |
| 563 | .flags = _flags,\ |
| 564 | .cfg = &(struct div_cfg) {\ |
| 565 | .reg_off = _offset,\ |
| 566 | .shift = _shift,\ |
| 567 | .width = _width,\ |
| 568 | .div_flags = _div_flags,\ |
| 569 | .table = _div_table,\ |
| 570 | },\ |
| 571 | .func = _clk_hw_register_divider_table,\ |
| 572 | } |
| 573 | |
| 574 | #define DIV(_id, _name, _parent, _flags, _offset, _shift, _width, _div_flags)\ |
| 575 | DIV_TABLE(_id, _name, _parent, _flags, _offset, _shift, _width,\ |
| 576 | _div_flags, NULL) |
| 577 | |
Gabriel Fernandez | dc32eaa | 2018-03-08 17:53:57 +0100 | [diff] [blame] | 578 | #define MUX(_id, _name, _parents, _flags, _offset, _shift, _width, _mux_flags)\ |
| 579 | {\ |
| 580 | .id = _id,\ |
| 581 | .name = _name,\ |
| 582 | .parent_names = _parents,\ |
| 583 | .num_parents = ARRAY_SIZE(_parents),\ |
| 584 | .flags = _flags,\ |
| 585 | .cfg = &(struct mux_cfg) {\ |
| 586 | .reg_off = _offset,\ |
| 587 | .shift = _shift,\ |
| 588 | .width = _width,\ |
| 589 | .mux_flags = _mux_flags,\ |
| 590 | },\ |
| 591 | .func = _clk_hw_register_mux,\ |
| 592 | } |
| 593 | |
Gabriel Fernandez | c6cf4d3 | 2018-03-08 17:53:58 +0100 | [diff] [blame^] | 594 | #define PLL(_id, _name, _parent, _flags, _offset)\ |
| 595 | {\ |
| 596 | .id = _id,\ |
| 597 | .name = _name,\ |
| 598 | .parent_name = _parent,\ |
| 599 | .flags = _flags,\ |
| 600 | .cfg = &(struct stm32_pll_cfg) {\ |
| 601 | .offset = _offset,\ |
| 602 | },\ |
| 603 | .func = _clk_register_pll,\ |
| 604 | } |
| 605 | |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 606 | /* STM32 GATE */ |
| 607 | #define STM32_GATE(_id, _name, _parent, _flags, _gate)\ |
| 608 | {\ |
| 609 | .id = _id,\ |
| 610 | .name = _name,\ |
| 611 | .parent_name = _parent,\ |
| 612 | .flags = _flags,\ |
| 613 | .cfg = (struct stm32_gate_cfg *) {_gate},\ |
| 614 | .func = _clk_stm32_register_gate,\ |
| 615 | } |
| 616 | |
| 617 | #define _STM32_GATE(_gate_offset, _gate_bit_idx, _gate_flags, _ops)\ |
| 618 | (&(struct stm32_gate_cfg) {\ |
| 619 | &(struct gate_cfg) {\ |
| 620 | .reg_off = _gate_offset,\ |
| 621 | .bit_idx = _gate_bit_idx,\ |
| 622 | .gate_flags = _gate_flags,\ |
| 623 | },\ |
| 624 | .ops = _ops,\ |
| 625 | }) |
| 626 | |
| 627 | #define _GATE_MP1(_gate_offset, _gate_bit_idx, _gate_flags)\ |
| 628 | _STM32_GATE(_gate_offset, _gate_bit_idx, _gate_flags,\ |
| 629 | &mp1_gate_clk_ops)\ |
| 630 | |
| 631 | #define GATE_MP1(_id, _name, _parent, _flags, _offset, _bit_idx, _gate_flags)\ |
| 632 | STM32_GATE(_id, _name, _parent, _flags,\ |
| 633 | _GATE_MP1(_offset, _bit_idx, _gate_flags)) |
| 634 | |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 635 | static const struct clock_config stm32mp1_clock_cfg[] = { |
| 636 | /* Oscillator divider */ |
| 637 | DIV(NO_ID, "clk-hsi-div", "clk-hsi", 0, RCC_HSICFGR, 0, 2, |
| 638 | CLK_DIVIDER_READ_ONLY), |
| 639 | |
| 640 | /* External / Internal Oscillators */ |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 641 | GATE_MP1(CK_HSE, "ck_hse", "clk-hse", 0, RCC_OCENSETR, 8, 0), |
| 642 | GATE_MP1(CK_CSI, "ck_csi", "clk-csi", 0, RCC_OCENSETR, 4, 0), |
| 643 | GATE_MP1(CK_HSI, "ck_hsi", "clk-hsi-div", 0, RCC_OCENSETR, 0, 0), |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 644 | GATE(CK_LSI, "ck_lsi", "clk-lsi", 0, RCC_RDLSICR, 0, 0), |
| 645 | GATE(CK_LSE, "ck_lse", "clk-lse", 0, RCC_BDCR, 0, 0), |
| 646 | |
| 647 | FIXED_FACTOR(CK_HSE_DIV2, "clk-hse-div2", "ck_hse", 0, 1, 2), |
Gabriel Fernandez | dc32eaa | 2018-03-08 17:53:57 +0100 | [diff] [blame] | 648 | |
| 649 | /* ref clock pll */ |
| 650 | MUX(NO_ID, "ref1", ref12_parents, CLK_OPS_PARENT_ENABLE, RCC_RCK12SELR, |
| 651 | 0, 2, CLK_MUX_READ_ONLY), |
| 652 | |
| 653 | MUX(NO_ID, "ref3", ref3_parents, CLK_OPS_PARENT_ENABLE, RCC_RCK3SELR, |
| 654 | 0, 2, CLK_MUX_READ_ONLY), |
| 655 | |
| 656 | MUX(NO_ID, "ref4", ref4_parents, CLK_OPS_PARENT_ENABLE, RCC_RCK4SELR, |
| 657 | 0, 2, CLK_MUX_READ_ONLY), |
Gabriel Fernandez | c6cf4d3 | 2018-03-08 17:53:58 +0100 | [diff] [blame^] | 658 | |
| 659 | /* PLLs */ |
| 660 | PLL(PLL1, "pll1", "ref1", CLK_IGNORE_UNUSED, RCC_PLL1CR), |
| 661 | PLL(PLL2, "pll2", "ref1", CLK_IGNORE_UNUSED, RCC_PLL2CR), |
| 662 | PLL(PLL3, "pll3", "ref3", CLK_IGNORE_UNUSED, RCC_PLL3CR), |
| 663 | PLL(PLL4, "pll4", "ref4", CLK_IGNORE_UNUSED, RCC_PLL4CR), |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 664 | }; |
| 665 | |
| 666 | struct stm32_clock_match_data { |
| 667 | const struct clock_config *cfg; |
| 668 | unsigned int num; |
| 669 | unsigned int maxbinding; |
| 670 | }; |
| 671 | |
| 672 | static struct stm32_clock_match_data stm32mp1_data = { |
| 673 | .cfg = stm32mp1_clock_cfg, |
| 674 | .num = ARRAY_SIZE(stm32mp1_clock_cfg), |
| 675 | .maxbinding = STM32MP1_LAST_CLK, |
| 676 | }; |
| 677 | |
| 678 | static const struct of_device_id stm32mp1_match_data[] = { |
| 679 | { |
| 680 | .compatible = "st,stm32mp1-rcc", |
| 681 | .data = &stm32mp1_data, |
| 682 | }, |
| 683 | { } |
| 684 | }; |
| 685 | |
| 686 | static int stm32_register_hw_clk(struct device *dev, |
| 687 | struct clk_hw_onecell_data *clk_data, |
| 688 | void __iomem *base, spinlock_t *lock, |
| 689 | const struct clock_config *cfg) |
| 690 | { |
| 691 | static struct clk_hw **hws; |
| 692 | struct clk_hw *hw = ERR_PTR(-ENOENT); |
| 693 | |
| 694 | hws = clk_data->hws; |
| 695 | |
| 696 | if (cfg->func) |
| 697 | hw = (*cfg->func)(dev, clk_data, base, lock, cfg); |
| 698 | |
| 699 | if (IS_ERR(hw)) { |
| 700 | pr_err("Unable to register %s\n", cfg->name); |
| 701 | return PTR_ERR(hw); |
| 702 | } |
| 703 | |
| 704 | if (cfg->id != NO_ID) |
| 705 | hws[cfg->id] = hw; |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | static int stm32_rcc_init(struct device_node *np, |
| 711 | void __iomem *base, |
| 712 | const struct of_device_id *match_data) |
| 713 | { |
| 714 | struct clk_hw_onecell_data *clk_data; |
| 715 | struct clk_hw **hws; |
| 716 | const struct of_device_id *match; |
| 717 | const struct stm32_clock_match_data *data; |
| 718 | int err, n, max_binding; |
| 719 | |
| 720 | match = of_match_node(match_data, np); |
| 721 | if (!match) { |
| 722 | pr_err("%s: match data not found\n", __func__); |
| 723 | return -ENODEV; |
| 724 | } |
| 725 | |
| 726 | data = match->data; |
| 727 | |
| 728 | max_binding = data->maxbinding; |
| 729 | |
| 730 | clk_data = kzalloc(sizeof(*clk_data) + |
| 731 | sizeof(*clk_data->hws) * max_binding, |
| 732 | GFP_KERNEL); |
| 733 | if (!clk_data) |
| 734 | return -ENOMEM; |
| 735 | |
| 736 | clk_data->num = max_binding; |
| 737 | |
| 738 | hws = clk_data->hws; |
| 739 | |
| 740 | for (n = 0; n < max_binding; n++) |
| 741 | hws[n] = ERR_PTR(-ENOENT); |
| 742 | |
| 743 | for (n = 0; n < data->num; n++) { |
| 744 | err = stm32_register_hw_clk(NULL, clk_data, base, &rlock, |
| 745 | &data->cfg[n]); |
| 746 | if (err) { |
| 747 | pr_err("%s: can't register %s\n", __func__, |
| 748 | data->cfg[n].name); |
| 749 | |
| 750 | kfree(clk_data); |
| 751 | |
| 752 | return err; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data); |
| 757 | } |
| 758 | |
| 759 | static void stm32mp1_rcc_init(struct device_node *np) |
| 760 | { |
| 761 | void __iomem *base; |
| 762 | |
| 763 | base = of_iomap(np, 0); |
| 764 | if (!base) { |
| 765 | pr_err("%s: unable to map resource", np->name); |
| 766 | of_node_put(np); |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | if (stm32_rcc_init(np, base, stm32mp1_match_data)) { |
| 771 | iounmap(base); |
| 772 | of_node_put(np); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | CLK_OF_DECLARE_DRIVER(stm32mp1_rcc, "st,stm32mp1-rcc", stm32mp1_rcc_init); |