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 | e51d297 | 2018-03-08 17:54:00 +0100 | [diff] [blame] | 119 | static const char * const cpu_src[] = { |
| 120 | "ck_hsi", "ck_hse", "pll1_p" |
| 121 | }; |
| 122 | |
| 123 | static const char * const axi_src[] = { |
| 124 | "ck_hsi", "ck_hse", "pll2_p", "pll3_p" |
| 125 | }; |
| 126 | |
| 127 | static const char * const per_src[] = { |
| 128 | "ck_hsi", "ck_csi", "ck_hse" |
| 129 | }; |
| 130 | |
| 131 | static const char * const mcu_src[] = { |
| 132 | "ck_hsi", "ck_hse", "ck_csi", "pll3_p" |
| 133 | }; |
| 134 | |
| 135 | static const struct clk_div_table axi_div_table[] = { |
| 136 | { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 }, |
| 137 | { 4, 4 }, { 5, 4 }, { 6, 4 }, { 7, 4 }, |
| 138 | { 0 }, |
| 139 | }; |
| 140 | |
| 141 | static const struct clk_div_table mcu_div_table[] = { |
| 142 | { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 }, |
| 143 | { 4, 16 }, { 5, 32 }, { 6, 64 }, { 7, 128 }, |
| 144 | { 8, 512 }, { 9, 512 }, { 10, 512}, { 11, 512 }, |
| 145 | { 12, 512 }, { 13, 512 }, { 14, 512}, { 15, 512 }, |
| 146 | { 0 }, |
| 147 | }; |
| 148 | |
| 149 | static const struct clk_div_table apb_div_table[] = { |
| 150 | { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 }, |
| 151 | { 4, 16 }, { 5, 16 }, { 6, 16 }, { 7, 16 }, |
| 152 | { 0 }, |
| 153 | }; |
| 154 | |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 155 | struct clock_config { |
| 156 | u32 id; |
| 157 | const char *name; |
| 158 | union { |
| 159 | const char *parent_name; |
| 160 | const char * const *parent_names; |
| 161 | }; |
| 162 | int num_parents; |
| 163 | unsigned long flags; |
| 164 | void *cfg; |
| 165 | struct clk_hw * (*func)(struct device *dev, |
| 166 | struct clk_hw_onecell_data *clk_data, |
| 167 | void __iomem *base, spinlock_t *lock, |
| 168 | const struct clock_config *cfg); |
| 169 | }; |
| 170 | |
| 171 | #define NO_ID ~0 |
| 172 | |
| 173 | struct gate_cfg { |
| 174 | u32 reg_off; |
| 175 | u8 bit_idx; |
| 176 | u8 gate_flags; |
| 177 | }; |
| 178 | |
| 179 | struct fixed_factor_cfg { |
| 180 | unsigned int mult; |
| 181 | unsigned int div; |
| 182 | }; |
| 183 | |
| 184 | struct div_cfg { |
| 185 | u32 reg_off; |
| 186 | u8 shift; |
| 187 | u8 width; |
| 188 | u8 div_flags; |
| 189 | const struct clk_div_table *table; |
| 190 | }; |
| 191 | |
Gabriel Fernandez | dc32eaa | 2018-03-08 17:53:57 +0100 | [diff] [blame] | 192 | struct mux_cfg { |
| 193 | u32 reg_off; |
| 194 | u8 shift; |
| 195 | u8 width; |
| 196 | u8 mux_flags; |
| 197 | u32 *table; |
| 198 | }; |
| 199 | |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 200 | struct stm32_gate_cfg { |
| 201 | struct gate_cfg *gate; |
| 202 | const struct clk_ops *ops; |
| 203 | }; |
| 204 | |
Gabriel Fernandez | a97703c | 2018-03-08 17:53:59 +0100 | [diff] [blame] | 205 | struct stm32_div_cfg { |
| 206 | struct div_cfg *div; |
| 207 | const struct clk_ops *ops; |
| 208 | }; |
| 209 | |
| 210 | struct stm32_mux_cfg { |
| 211 | struct mux_cfg *mux; |
| 212 | const struct clk_ops *ops; |
| 213 | }; |
| 214 | |
| 215 | /* STM32 Composite clock */ |
| 216 | struct stm32_composite_cfg { |
| 217 | const struct stm32_gate_cfg *gate; |
| 218 | const struct stm32_div_cfg *div; |
| 219 | const struct stm32_mux_cfg *mux; |
| 220 | }; |
| 221 | |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 222 | static struct clk_hw * |
| 223 | _clk_hw_register_gate(struct device *dev, |
| 224 | struct clk_hw_onecell_data *clk_data, |
| 225 | void __iomem *base, spinlock_t *lock, |
| 226 | const struct clock_config *cfg) |
| 227 | { |
| 228 | struct gate_cfg *gate_cfg = cfg->cfg; |
| 229 | |
| 230 | return clk_hw_register_gate(dev, |
| 231 | cfg->name, |
| 232 | cfg->parent_name, |
| 233 | cfg->flags, |
| 234 | gate_cfg->reg_off + base, |
| 235 | gate_cfg->bit_idx, |
| 236 | gate_cfg->gate_flags, |
| 237 | lock); |
| 238 | } |
| 239 | |
| 240 | static struct clk_hw * |
| 241 | _clk_hw_register_fixed_factor(struct device *dev, |
| 242 | struct clk_hw_onecell_data *clk_data, |
| 243 | void __iomem *base, spinlock_t *lock, |
| 244 | const struct clock_config *cfg) |
| 245 | { |
| 246 | struct fixed_factor_cfg *ff_cfg = cfg->cfg; |
| 247 | |
| 248 | return clk_hw_register_fixed_factor(dev, cfg->name, cfg->parent_name, |
| 249 | cfg->flags, ff_cfg->mult, |
| 250 | ff_cfg->div); |
| 251 | } |
| 252 | |
| 253 | static struct clk_hw * |
| 254 | _clk_hw_register_divider_table(struct device *dev, |
| 255 | struct clk_hw_onecell_data *clk_data, |
| 256 | void __iomem *base, spinlock_t *lock, |
| 257 | const struct clock_config *cfg) |
| 258 | { |
| 259 | struct div_cfg *div_cfg = cfg->cfg; |
| 260 | |
| 261 | return clk_hw_register_divider_table(dev, |
| 262 | cfg->name, |
| 263 | cfg->parent_name, |
| 264 | cfg->flags, |
| 265 | div_cfg->reg_off + base, |
| 266 | div_cfg->shift, |
| 267 | div_cfg->width, |
| 268 | div_cfg->div_flags, |
| 269 | div_cfg->table, |
| 270 | lock); |
| 271 | } |
| 272 | |
Gabriel Fernandez | dc32eaa | 2018-03-08 17:53:57 +0100 | [diff] [blame] | 273 | static struct clk_hw * |
| 274 | _clk_hw_register_mux(struct device *dev, |
| 275 | struct clk_hw_onecell_data *clk_data, |
| 276 | void __iomem *base, spinlock_t *lock, |
| 277 | const struct clock_config *cfg) |
| 278 | { |
| 279 | struct mux_cfg *mux_cfg = cfg->cfg; |
| 280 | |
| 281 | return clk_hw_register_mux(dev, cfg->name, cfg->parent_names, |
| 282 | cfg->num_parents, cfg->flags, |
| 283 | mux_cfg->reg_off + base, mux_cfg->shift, |
| 284 | mux_cfg->width, mux_cfg->mux_flags, lock); |
| 285 | } |
| 286 | |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 287 | /* MP1 Gate clock with set & clear registers */ |
| 288 | |
| 289 | static int mp1_gate_clk_enable(struct clk_hw *hw) |
| 290 | { |
| 291 | if (!clk_gate_ops.is_enabled(hw)) |
| 292 | clk_gate_ops.enable(hw); |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static void mp1_gate_clk_disable(struct clk_hw *hw) |
| 298 | { |
| 299 | struct clk_gate *gate = to_clk_gate(hw); |
| 300 | unsigned long flags = 0; |
| 301 | |
| 302 | if (clk_gate_ops.is_enabled(hw)) { |
| 303 | spin_lock_irqsave(gate->lock, flags); |
| 304 | writel_relaxed(BIT(gate->bit_idx), gate->reg + RCC_CLR); |
| 305 | spin_unlock_irqrestore(gate->lock, flags); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | const struct clk_ops mp1_gate_clk_ops = { |
| 310 | .enable = mp1_gate_clk_enable, |
| 311 | .disable = mp1_gate_clk_disable, |
| 312 | .is_enabled = clk_gate_is_enabled, |
| 313 | }; |
| 314 | |
Gabriel Fernandez | a97703c | 2018-03-08 17:53:59 +0100 | [diff] [blame] | 315 | static struct clk_hw *_get_stm32_mux(void __iomem *base, |
| 316 | const struct stm32_mux_cfg *cfg, |
| 317 | spinlock_t *lock) |
| 318 | { |
| 319 | struct clk_mux *mux; |
| 320 | struct clk_hw *mux_hw; |
| 321 | |
| 322 | mux = kzalloc(sizeof(*mux), GFP_KERNEL); |
| 323 | if (!mux) |
| 324 | return ERR_PTR(-ENOMEM); |
| 325 | |
| 326 | mux->reg = cfg->mux->reg_off + base; |
| 327 | mux->shift = cfg->mux->shift; |
| 328 | mux->mask = (1 << cfg->mux->width) - 1; |
| 329 | mux->flags = cfg->mux->mux_flags; |
| 330 | mux->table = cfg->mux->table; |
| 331 | |
| 332 | mux->lock = lock; |
| 333 | |
| 334 | mux_hw = &mux->hw; |
| 335 | |
| 336 | return mux_hw; |
| 337 | } |
| 338 | |
| 339 | static struct clk_hw *_get_stm32_div(void __iomem *base, |
| 340 | const struct stm32_div_cfg *cfg, |
| 341 | spinlock_t *lock) |
| 342 | { |
| 343 | struct clk_divider *div; |
| 344 | |
| 345 | div = kzalloc(sizeof(*div), GFP_KERNEL); |
| 346 | |
| 347 | if (!div) |
| 348 | return ERR_PTR(-ENOMEM); |
| 349 | |
| 350 | div->reg = cfg->div->reg_off + base; |
| 351 | div->shift = cfg->div->shift; |
| 352 | div->width = cfg->div->width; |
| 353 | div->flags = cfg->div->div_flags; |
| 354 | div->table = cfg->div->table; |
| 355 | div->lock = lock; |
| 356 | |
| 357 | return &div->hw; |
| 358 | } |
| 359 | |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 360 | static struct clk_hw * |
| 361 | _get_stm32_gate(void __iomem *base, |
| 362 | const struct stm32_gate_cfg *cfg, spinlock_t *lock) |
| 363 | { |
| 364 | struct clk_gate *gate; |
| 365 | struct clk_hw *gate_hw; |
| 366 | |
| 367 | gate = kzalloc(sizeof(*gate), GFP_KERNEL); |
| 368 | if (!gate) |
| 369 | return ERR_PTR(-ENOMEM); |
| 370 | |
| 371 | gate->reg = cfg->gate->reg_off + base; |
| 372 | gate->bit_idx = cfg->gate->bit_idx; |
| 373 | gate->flags = cfg->gate->gate_flags; |
| 374 | gate->lock = lock; |
| 375 | gate_hw = &gate->hw; |
| 376 | |
| 377 | return gate_hw; |
| 378 | } |
| 379 | |
| 380 | static struct clk_hw * |
| 381 | clk_stm32_register_gate_ops(struct device *dev, |
| 382 | const char *name, |
| 383 | const char *parent_name, |
| 384 | unsigned long flags, |
| 385 | void __iomem *base, |
| 386 | const struct stm32_gate_cfg *cfg, |
| 387 | spinlock_t *lock) |
| 388 | { |
| 389 | struct clk_init_data init = { NULL }; |
| 390 | struct clk_gate *gate; |
| 391 | struct clk_hw *hw; |
| 392 | int ret; |
| 393 | |
| 394 | gate = kzalloc(sizeof(*gate), GFP_KERNEL); |
| 395 | if (!gate) |
| 396 | return ERR_PTR(-ENOMEM); |
| 397 | |
| 398 | init.name = name; |
| 399 | init.parent_names = &parent_name; |
| 400 | init.num_parents = 1; |
| 401 | init.flags = flags; |
| 402 | |
| 403 | init.ops = &clk_gate_ops; |
| 404 | |
| 405 | if (cfg->ops) |
| 406 | init.ops = cfg->ops; |
| 407 | |
| 408 | hw = _get_stm32_gate(base, cfg, lock); |
| 409 | if (IS_ERR(hw)) |
| 410 | return ERR_PTR(-ENOMEM); |
| 411 | |
| 412 | hw->init = &init; |
| 413 | |
| 414 | ret = clk_hw_register(dev, hw); |
| 415 | if (ret) { |
| 416 | kfree(gate); |
| 417 | hw = ERR_PTR(ret); |
| 418 | } |
| 419 | |
| 420 | return hw; |
| 421 | } |
| 422 | |
Gabriel Fernandez | a97703c | 2018-03-08 17:53:59 +0100 | [diff] [blame] | 423 | static struct clk_hw * |
| 424 | clk_stm32_register_composite(struct device *dev, |
| 425 | const char *name, const char * const *parent_names, |
| 426 | int num_parents, void __iomem *base, |
| 427 | const struct stm32_composite_cfg *cfg, |
| 428 | unsigned long flags, spinlock_t *lock) |
| 429 | { |
| 430 | const struct clk_ops *mux_ops, *div_ops, *gate_ops; |
| 431 | struct clk_hw *mux_hw, *div_hw, *gate_hw; |
| 432 | |
| 433 | mux_hw = NULL; |
| 434 | div_hw = NULL; |
| 435 | gate_hw = NULL; |
| 436 | mux_ops = NULL; |
| 437 | div_ops = NULL; |
| 438 | gate_ops = NULL; |
| 439 | |
| 440 | if (cfg->mux) { |
| 441 | mux_hw = _get_stm32_mux(base, cfg->mux, lock); |
| 442 | |
| 443 | if (!IS_ERR(mux_hw)) { |
| 444 | mux_ops = &clk_mux_ops; |
| 445 | |
| 446 | if (cfg->mux->ops) |
| 447 | mux_ops = cfg->mux->ops; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | if (cfg->div) { |
| 452 | div_hw = _get_stm32_div(base, cfg->div, lock); |
| 453 | |
| 454 | if (!IS_ERR(div_hw)) { |
| 455 | div_ops = &clk_divider_ops; |
| 456 | |
| 457 | if (cfg->div->ops) |
| 458 | div_ops = cfg->div->ops; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | if (cfg->gate) { |
| 463 | gate_hw = _get_stm32_gate(base, cfg->gate, lock); |
| 464 | |
| 465 | if (!IS_ERR(gate_hw)) { |
| 466 | gate_ops = &clk_gate_ops; |
| 467 | |
| 468 | if (cfg->gate->ops) |
| 469 | gate_ops = cfg->gate->ops; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | return clk_hw_register_composite(dev, name, parent_names, num_parents, |
| 474 | mux_hw, mux_ops, div_hw, div_ops, |
| 475 | gate_hw, gate_ops, flags); |
| 476 | } |
| 477 | |
Gabriel Fernandez | c6cf4d3 | 2018-03-08 17:53:58 +0100 | [diff] [blame] | 478 | /* STM32 PLL */ |
| 479 | |
| 480 | struct stm32_pll_obj { |
| 481 | /* lock pll enable/disable registers */ |
| 482 | spinlock_t *lock; |
| 483 | void __iomem *reg; |
| 484 | struct clk_hw hw; |
| 485 | }; |
| 486 | |
| 487 | #define to_pll(_hw) container_of(_hw, struct stm32_pll_obj, hw) |
| 488 | |
| 489 | #define PLL_ON BIT(0) |
| 490 | #define PLL_RDY BIT(1) |
| 491 | #define DIVN_MASK 0x1FF |
| 492 | #define DIVM_MASK 0x3F |
| 493 | #define DIVM_SHIFT 16 |
| 494 | #define DIVN_SHIFT 0 |
| 495 | #define FRAC_OFFSET 0xC |
| 496 | #define FRAC_MASK 0x1FFF |
| 497 | #define FRAC_SHIFT 3 |
| 498 | #define FRACLE BIT(16) |
| 499 | |
| 500 | static int __pll_is_enabled(struct clk_hw *hw) |
| 501 | { |
| 502 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 503 | |
| 504 | return readl_relaxed(clk_elem->reg) & PLL_ON; |
| 505 | } |
| 506 | |
| 507 | #define TIMEOUT 5 |
| 508 | |
| 509 | static int pll_enable(struct clk_hw *hw) |
| 510 | { |
| 511 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 512 | u32 reg; |
| 513 | unsigned long flags = 0; |
| 514 | unsigned int timeout = TIMEOUT; |
| 515 | int bit_status = 0; |
| 516 | |
| 517 | spin_lock_irqsave(clk_elem->lock, flags); |
| 518 | |
| 519 | if (__pll_is_enabled(hw)) |
| 520 | goto unlock; |
| 521 | |
| 522 | reg = readl_relaxed(clk_elem->reg); |
| 523 | reg |= PLL_ON; |
| 524 | writel_relaxed(reg, clk_elem->reg); |
| 525 | |
| 526 | /* We can't use readl_poll_timeout() because we can be blocked if |
| 527 | * someone enables this clock before clocksource changes. |
| 528 | * Only jiffies counter is available. Jiffies are incremented by |
| 529 | * interruptions and enable op does not allow to be interrupted. |
| 530 | */ |
| 531 | do { |
| 532 | bit_status = !(readl_relaxed(clk_elem->reg) & PLL_RDY); |
| 533 | |
| 534 | if (bit_status) |
| 535 | udelay(120); |
| 536 | |
| 537 | } while (bit_status && --timeout); |
| 538 | |
| 539 | unlock: |
| 540 | spin_unlock_irqrestore(clk_elem->lock, flags); |
| 541 | |
| 542 | return bit_status; |
| 543 | } |
| 544 | |
| 545 | static void pll_disable(struct clk_hw *hw) |
| 546 | { |
| 547 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 548 | u32 reg; |
| 549 | unsigned long flags = 0; |
| 550 | |
| 551 | spin_lock_irqsave(clk_elem->lock, flags); |
| 552 | |
| 553 | reg = readl_relaxed(clk_elem->reg); |
| 554 | reg &= ~PLL_ON; |
| 555 | writel_relaxed(reg, clk_elem->reg); |
| 556 | |
| 557 | spin_unlock_irqrestore(clk_elem->lock, flags); |
| 558 | } |
| 559 | |
| 560 | static u32 pll_frac_val(struct clk_hw *hw) |
| 561 | { |
| 562 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 563 | u32 reg, frac = 0; |
| 564 | |
| 565 | reg = readl_relaxed(clk_elem->reg + FRAC_OFFSET); |
| 566 | if (reg & FRACLE) |
| 567 | frac = (reg >> FRAC_SHIFT) & FRAC_MASK; |
| 568 | |
| 569 | return frac; |
| 570 | } |
| 571 | |
| 572 | static unsigned long pll_recalc_rate(struct clk_hw *hw, |
| 573 | unsigned long parent_rate) |
| 574 | { |
| 575 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 576 | u32 reg; |
| 577 | u32 frac, divm, divn; |
| 578 | u64 rate, rate_frac = 0; |
| 579 | |
| 580 | reg = readl_relaxed(clk_elem->reg + 4); |
| 581 | |
| 582 | divm = ((reg >> DIVM_SHIFT) & DIVM_MASK) + 1; |
| 583 | divn = ((reg >> DIVN_SHIFT) & DIVN_MASK) + 1; |
| 584 | rate = (u64)parent_rate * divn; |
| 585 | |
| 586 | do_div(rate, divm); |
| 587 | |
| 588 | frac = pll_frac_val(hw); |
| 589 | if (frac) { |
| 590 | rate_frac = (u64)parent_rate * (u64)frac; |
| 591 | do_div(rate_frac, (divm * 8192)); |
| 592 | } |
| 593 | |
| 594 | return rate + rate_frac; |
| 595 | } |
| 596 | |
| 597 | static int pll_is_enabled(struct clk_hw *hw) |
| 598 | { |
| 599 | struct stm32_pll_obj *clk_elem = to_pll(hw); |
| 600 | unsigned long flags = 0; |
| 601 | int ret; |
| 602 | |
| 603 | spin_lock_irqsave(clk_elem->lock, flags); |
| 604 | ret = __pll_is_enabled(hw); |
| 605 | spin_unlock_irqrestore(clk_elem->lock, flags); |
| 606 | |
| 607 | return ret; |
| 608 | } |
| 609 | |
| 610 | static const struct clk_ops pll_ops = { |
| 611 | .enable = pll_enable, |
| 612 | .disable = pll_disable, |
| 613 | .recalc_rate = pll_recalc_rate, |
| 614 | .is_enabled = pll_is_enabled, |
| 615 | }; |
| 616 | |
| 617 | static struct clk_hw *clk_register_pll(struct device *dev, const char *name, |
| 618 | const char *parent_name, |
| 619 | void __iomem *reg, |
| 620 | unsigned long flags, |
| 621 | spinlock_t *lock) |
| 622 | { |
| 623 | struct stm32_pll_obj *element; |
| 624 | struct clk_init_data init; |
| 625 | struct clk_hw *hw; |
| 626 | int err; |
| 627 | |
| 628 | element = kzalloc(sizeof(*element), GFP_KERNEL); |
| 629 | if (!element) |
| 630 | return ERR_PTR(-ENOMEM); |
| 631 | |
| 632 | init.name = name; |
| 633 | init.ops = &pll_ops; |
| 634 | init.flags = flags; |
| 635 | init.parent_names = &parent_name; |
| 636 | init.num_parents = 1; |
| 637 | |
| 638 | element->hw.init = &init; |
| 639 | element->reg = reg; |
| 640 | element->lock = lock; |
| 641 | |
| 642 | hw = &element->hw; |
| 643 | err = clk_hw_register(dev, hw); |
| 644 | |
| 645 | if (err) { |
| 646 | kfree(element); |
| 647 | return ERR_PTR(err); |
| 648 | } |
| 649 | |
| 650 | return hw; |
| 651 | } |
| 652 | |
Gabriel Fernandez | 799b6a1 | 2018-03-08 17:54:01 +0100 | [diff] [blame^] | 653 | /* Kernel Timer */ |
| 654 | struct timer_cker { |
| 655 | /* lock the kernel output divider register */ |
| 656 | spinlock_t *lock; |
| 657 | void __iomem *apbdiv; |
| 658 | void __iomem *timpre; |
| 659 | struct clk_hw hw; |
| 660 | }; |
| 661 | |
| 662 | #define to_timer_cker(_hw) container_of(_hw, struct timer_cker, hw) |
| 663 | |
| 664 | #define APB_DIV_MASK 0x07 |
| 665 | #define TIM_PRE_MASK 0x01 |
| 666 | |
| 667 | static unsigned long __bestmult(struct clk_hw *hw, unsigned long rate, |
| 668 | unsigned long parent_rate) |
| 669 | { |
| 670 | struct timer_cker *tim_ker = to_timer_cker(hw); |
| 671 | u32 prescaler; |
| 672 | unsigned int mult = 0; |
| 673 | |
| 674 | prescaler = readl_relaxed(tim_ker->apbdiv) & APB_DIV_MASK; |
| 675 | if (prescaler < 2) |
| 676 | return 1; |
| 677 | |
| 678 | mult = 2; |
| 679 | |
| 680 | if (rate / parent_rate >= 4) |
| 681 | mult = 4; |
| 682 | |
| 683 | return mult; |
| 684 | } |
| 685 | |
| 686 | static long timer_ker_round_rate(struct clk_hw *hw, unsigned long rate, |
| 687 | unsigned long *parent_rate) |
| 688 | { |
| 689 | unsigned long factor = __bestmult(hw, rate, *parent_rate); |
| 690 | |
| 691 | return *parent_rate * factor; |
| 692 | } |
| 693 | |
| 694 | static int timer_ker_set_rate(struct clk_hw *hw, unsigned long rate, |
| 695 | unsigned long parent_rate) |
| 696 | { |
| 697 | struct timer_cker *tim_ker = to_timer_cker(hw); |
| 698 | unsigned long flags = 0; |
| 699 | unsigned long factor = __bestmult(hw, rate, parent_rate); |
| 700 | int ret = 0; |
| 701 | |
| 702 | spin_lock_irqsave(tim_ker->lock, flags); |
| 703 | |
| 704 | switch (factor) { |
| 705 | case 1: |
| 706 | break; |
| 707 | case 2: |
| 708 | writel_relaxed(0, tim_ker->timpre); |
| 709 | break; |
| 710 | case 4: |
| 711 | writel_relaxed(1, tim_ker->timpre); |
| 712 | break; |
| 713 | default: |
| 714 | ret = -EINVAL; |
| 715 | } |
| 716 | spin_unlock_irqrestore(tim_ker->lock, flags); |
| 717 | |
| 718 | return ret; |
| 719 | } |
| 720 | |
| 721 | static unsigned long timer_ker_recalc_rate(struct clk_hw *hw, |
| 722 | unsigned long parent_rate) |
| 723 | { |
| 724 | struct timer_cker *tim_ker = to_timer_cker(hw); |
| 725 | u32 prescaler, timpre; |
| 726 | u32 mul; |
| 727 | |
| 728 | prescaler = readl_relaxed(tim_ker->apbdiv) & APB_DIV_MASK; |
| 729 | |
| 730 | timpre = readl_relaxed(tim_ker->timpre) & TIM_PRE_MASK; |
| 731 | |
| 732 | if (!prescaler) |
| 733 | return parent_rate; |
| 734 | |
| 735 | mul = (timpre + 1) * 2; |
| 736 | |
| 737 | return parent_rate * mul; |
| 738 | } |
| 739 | |
| 740 | static const struct clk_ops timer_ker_ops = { |
| 741 | .recalc_rate = timer_ker_recalc_rate, |
| 742 | .round_rate = timer_ker_round_rate, |
| 743 | .set_rate = timer_ker_set_rate, |
| 744 | |
| 745 | }; |
| 746 | |
| 747 | static struct clk_hw *clk_register_cktim(struct device *dev, const char *name, |
| 748 | const char *parent_name, |
| 749 | unsigned long flags, |
| 750 | void __iomem *apbdiv, |
| 751 | void __iomem *timpre, |
| 752 | spinlock_t *lock) |
| 753 | { |
| 754 | struct timer_cker *tim_ker; |
| 755 | struct clk_init_data init; |
| 756 | struct clk_hw *hw; |
| 757 | int err; |
| 758 | |
| 759 | tim_ker = kzalloc(sizeof(*tim_ker), GFP_KERNEL); |
| 760 | if (!tim_ker) |
| 761 | return ERR_PTR(-ENOMEM); |
| 762 | |
| 763 | init.name = name; |
| 764 | init.ops = &timer_ker_ops; |
| 765 | init.flags = flags; |
| 766 | init.parent_names = &parent_name; |
| 767 | init.num_parents = 1; |
| 768 | |
| 769 | tim_ker->hw.init = &init; |
| 770 | tim_ker->lock = lock; |
| 771 | tim_ker->apbdiv = apbdiv; |
| 772 | tim_ker->timpre = timpre; |
| 773 | |
| 774 | hw = &tim_ker->hw; |
| 775 | err = clk_hw_register(dev, hw); |
| 776 | |
| 777 | if (err) { |
| 778 | kfree(tim_ker); |
| 779 | return ERR_PTR(err); |
| 780 | } |
| 781 | |
| 782 | return hw; |
| 783 | } |
| 784 | |
Gabriel Fernandez | c6cf4d3 | 2018-03-08 17:53:58 +0100 | [diff] [blame] | 785 | struct stm32_pll_cfg { |
| 786 | u32 offset; |
| 787 | }; |
| 788 | |
| 789 | struct clk_hw *_clk_register_pll(struct device *dev, |
| 790 | struct clk_hw_onecell_data *clk_data, |
| 791 | void __iomem *base, spinlock_t *lock, |
| 792 | const struct clock_config *cfg) |
| 793 | { |
| 794 | struct stm32_pll_cfg *stm_pll_cfg = cfg->cfg; |
| 795 | |
| 796 | return clk_register_pll(dev, cfg->name, cfg->parent_name, |
| 797 | base + stm_pll_cfg->offset, cfg->flags, lock); |
| 798 | } |
| 799 | |
Gabriel Fernandez | 799b6a1 | 2018-03-08 17:54:01 +0100 | [diff] [blame^] | 800 | struct stm32_cktim_cfg { |
| 801 | u32 offset_apbdiv; |
| 802 | u32 offset_timpre; |
| 803 | }; |
| 804 | |
| 805 | static struct clk_hw *_clk_register_cktim(struct device *dev, |
| 806 | struct clk_hw_onecell_data *clk_data, |
| 807 | void __iomem *base, spinlock_t *lock, |
| 808 | const struct clock_config *cfg) |
| 809 | { |
| 810 | struct stm32_cktim_cfg *cktim_cfg = cfg->cfg; |
| 811 | |
| 812 | return clk_register_cktim(dev, cfg->name, cfg->parent_name, cfg->flags, |
| 813 | cktim_cfg->offset_apbdiv + base, |
| 814 | cktim_cfg->offset_timpre + base, lock); |
| 815 | } |
| 816 | |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 817 | static struct clk_hw * |
| 818 | _clk_stm32_register_gate(struct device *dev, |
| 819 | struct clk_hw_onecell_data *clk_data, |
| 820 | void __iomem *base, spinlock_t *lock, |
| 821 | const struct clock_config *cfg) |
| 822 | { |
| 823 | return clk_stm32_register_gate_ops(dev, |
| 824 | cfg->name, |
| 825 | cfg->parent_name, |
| 826 | cfg->flags, |
| 827 | base, |
| 828 | cfg->cfg, |
| 829 | lock); |
| 830 | } |
| 831 | |
Gabriel Fernandez | a97703c | 2018-03-08 17:53:59 +0100 | [diff] [blame] | 832 | static struct clk_hw * |
| 833 | _clk_stm32_register_composite(struct device *dev, |
| 834 | struct clk_hw_onecell_data *clk_data, |
| 835 | void __iomem *base, spinlock_t *lock, |
| 836 | const struct clock_config *cfg) |
| 837 | { |
| 838 | return clk_stm32_register_composite(dev, cfg->name, cfg->parent_names, |
| 839 | cfg->num_parents, base, cfg->cfg, |
| 840 | cfg->flags, lock); |
| 841 | } |
| 842 | |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 843 | #define GATE(_id, _name, _parent, _flags, _offset, _bit_idx, _gate_flags)\ |
| 844 | {\ |
| 845 | .id = _id,\ |
| 846 | .name = _name,\ |
| 847 | .parent_name = _parent,\ |
| 848 | .flags = _flags,\ |
| 849 | .cfg = &(struct gate_cfg) {\ |
| 850 | .reg_off = _offset,\ |
| 851 | .bit_idx = _bit_idx,\ |
| 852 | .gate_flags = _gate_flags,\ |
| 853 | },\ |
| 854 | .func = _clk_hw_register_gate,\ |
| 855 | } |
| 856 | |
| 857 | #define FIXED_FACTOR(_id, _name, _parent, _flags, _mult, _div)\ |
| 858 | {\ |
| 859 | .id = _id,\ |
| 860 | .name = _name,\ |
| 861 | .parent_name = _parent,\ |
| 862 | .flags = _flags,\ |
| 863 | .cfg = &(struct fixed_factor_cfg) {\ |
| 864 | .mult = _mult,\ |
| 865 | .div = _div,\ |
| 866 | },\ |
| 867 | .func = _clk_hw_register_fixed_factor,\ |
| 868 | } |
| 869 | |
| 870 | #define DIV_TABLE(_id, _name, _parent, _flags, _offset, _shift, _width,\ |
| 871 | _div_flags, _div_table)\ |
| 872 | {\ |
| 873 | .id = _id,\ |
| 874 | .name = _name,\ |
| 875 | .parent_name = _parent,\ |
| 876 | .flags = _flags,\ |
| 877 | .cfg = &(struct div_cfg) {\ |
| 878 | .reg_off = _offset,\ |
| 879 | .shift = _shift,\ |
| 880 | .width = _width,\ |
| 881 | .div_flags = _div_flags,\ |
| 882 | .table = _div_table,\ |
| 883 | },\ |
| 884 | .func = _clk_hw_register_divider_table,\ |
| 885 | } |
| 886 | |
| 887 | #define DIV(_id, _name, _parent, _flags, _offset, _shift, _width, _div_flags)\ |
| 888 | DIV_TABLE(_id, _name, _parent, _flags, _offset, _shift, _width,\ |
| 889 | _div_flags, NULL) |
| 890 | |
Gabriel Fernandez | dc32eaa | 2018-03-08 17:53:57 +0100 | [diff] [blame] | 891 | #define MUX(_id, _name, _parents, _flags, _offset, _shift, _width, _mux_flags)\ |
| 892 | {\ |
| 893 | .id = _id,\ |
| 894 | .name = _name,\ |
| 895 | .parent_names = _parents,\ |
| 896 | .num_parents = ARRAY_SIZE(_parents),\ |
| 897 | .flags = _flags,\ |
| 898 | .cfg = &(struct mux_cfg) {\ |
| 899 | .reg_off = _offset,\ |
| 900 | .shift = _shift,\ |
| 901 | .width = _width,\ |
| 902 | .mux_flags = _mux_flags,\ |
| 903 | },\ |
| 904 | .func = _clk_hw_register_mux,\ |
| 905 | } |
| 906 | |
Gabriel Fernandez | c6cf4d3 | 2018-03-08 17:53:58 +0100 | [diff] [blame] | 907 | #define PLL(_id, _name, _parent, _flags, _offset)\ |
| 908 | {\ |
| 909 | .id = _id,\ |
| 910 | .name = _name,\ |
| 911 | .parent_name = _parent,\ |
| 912 | .flags = _flags,\ |
| 913 | .cfg = &(struct stm32_pll_cfg) {\ |
| 914 | .offset = _offset,\ |
| 915 | },\ |
| 916 | .func = _clk_register_pll,\ |
| 917 | } |
| 918 | |
Gabriel Fernandez | 799b6a1 | 2018-03-08 17:54:01 +0100 | [diff] [blame^] | 919 | #define STM32_CKTIM(_name, _parent, _flags, _offset_apbdiv, _offset_timpre)\ |
| 920 | {\ |
| 921 | .id = NO_ID,\ |
| 922 | .name = _name,\ |
| 923 | .parent_name = _parent,\ |
| 924 | .flags = _flags,\ |
| 925 | .cfg = &(struct stm32_cktim_cfg) {\ |
| 926 | .offset_apbdiv = _offset_apbdiv,\ |
| 927 | .offset_timpre = _offset_timpre,\ |
| 928 | },\ |
| 929 | .func = _clk_register_cktim,\ |
| 930 | } |
| 931 | |
| 932 | #define STM32_TIM(_id, _name, _parent, _offset_set, _bit_idx)\ |
| 933 | GATE_MP1(_id, _name, _parent, CLK_SET_RATE_PARENT,\ |
| 934 | _offset_set, _bit_idx, 0) |
| 935 | |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 936 | /* STM32 GATE */ |
| 937 | #define STM32_GATE(_id, _name, _parent, _flags, _gate)\ |
| 938 | {\ |
| 939 | .id = _id,\ |
| 940 | .name = _name,\ |
| 941 | .parent_name = _parent,\ |
| 942 | .flags = _flags,\ |
| 943 | .cfg = (struct stm32_gate_cfg *) {_gate},\ |
| 944 | .func = _clk_stm32_register_gate,\ |
| 945 | } |
| 946 | |
| 947 | #define _STM32_GATE(_gate_offset, _gate_bit_idx, _gate_flags, _ops)\ |
| 948 | (&(struct stm32_gate_cfg) {\ |
| 949 | &(struct gate_cfg) {\ |
| 950 | .reg_off = _gate_offset,\ |
| 951 | .bit_idx = _gate_bit_idx,\ |
| 952 | .gate_flags = _gate_flags,\ |
| 953 | },\ |
| 954 | .ops = _ops,\ |
| 955 | }) |
| 956 | |
Gabriel Fernandez | a97703c | 2018-03-08 17:53:59 +0100 | [diff] [blame] | 957 | #define _GATE(_gate_offset, _gate_bit_idx, _gate_flags)\ |
| 958 | _STM32_GATE(_gate_offset, _gate_bit_idx, _gate_flags,\ |
| 959 | NULL)\ |
| 960 | |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 961 | #define _GATE_MP1(_gate_offset, _gate_bit_idx, _gate_flags)\ |
| 962 | _STM32_GATE(_gate_offset, _gate_bit_idx, _gate_flags,\ |
| 963 | &mp1_gate_clk_ops)\ |
| 964 | |
| 965 | #define GATE_MP1(_id, _name, _parent, _flags, _offset, _bit_idx, _gate_flags)\ |
| 966 | STM32_GATE(_id, _name, _parent, _flags,\ |
| 967 | _GATE_MP1(_offset, _bit_idx, _gate_flags)) |
| 968 | |
Gabriel Fernandez | a97703c | 2018-03-08 17:53:59 +0100 | [diff] [blame] | 969 | #define _STM32_DIV(_div_offset, _div_shift, _div_width,\ |
| 970 | _div_flags, _div_table, _ops)\ |
| 971 | .div = &(struct stm32_div_cfg) {\ |
| 972 | &(struct div_cfg) {\ |
| 973 | .reg_off = _div_offset,\ |
| 974 | .shift = _div_shift,\ |
| 975 | .width = _div_width,\ |
| 976 | .div_flags = _div_flags,\ |
| 977 | .table = _div_table,\ |
| 978 | },\ |
| 979 | .ops = _ops,\ |
| 980 | } |
| 981 | |
| 982 | #define _DIV(_div_offset, _div_shift, _div_width, _div_flags, _div_table)\ |
| 983 | _STM32_DIV(_div_offset, _div_shift, _div_width,\ |
| 984 | _div_flags, _div_table, NULL)\ |
| 985 | |
Gabriel Fernandez | e51d297 | 2018-03-08 17:54:00 +0100 | [diff] [blame] | 986 | #define _STM32_MUX(_offset, _shift, _width, _mux_flags, _ops)\ |
| 987 | .mux = &(struct stm32_mux_cfg) {\ |
| 988 | &(struct mux_cfg) {\ |
| 989 | .reg_off = _offset,\ |
| 990 | .shift = _shift,\ |
| 991 | .width = _width,\ |
| 992 | .mux_flags = _mux_flags,\ |
| 993 | .table = NULL,\ |
| 994 | },\ |
| 995 | .ops = _ops,\ |
| 996 | } |
| 997 | |
| 998 | #define _MUX(_offset, _shift, _width, _mux_flags)\ |
| 999 | _STM32_MUX(_offset, _shift, _width, _mux_flags, NULL)\ |
| 1000 | |
Gabriel Fernandez | a97703c | 2018-03-08 17:53:59 +0100 | [diff] [blame] | 1001 | #define PARENT(_parent) ((const char *[]) { _parent}) |
| 1002 | |
| 1003 | #define _NO_MUX .mux = NULL |
| 1004 | #define _NO_DIV .div = NULL |
| 1005 | #define _NO_GATE .gate = NULL |
| 1006 | |
| 1007 | #define COMPOSITE(_id, _name, _parents, _flags, _gate, _mux, _div)\ |
| 1008 | {\ |
| 1009 | .id = _id,\ |
| 1010 | .name = _name,\ |
| 1011 | .parent_names = _parents,\ |
| 1012 | .num_parents = ARRAY_SIZE(_parents),\ |
| 1013 | .flags = _flags,\ |
| 1014 | .cfg = &(struct stm32_composite_cfg) {\ |
| 1015 | _gate,\ |
| 1016 | _mux,\ |
| 1017 | _div,\ |
| 1018 | },\ |
| 1019 | .func = _clk_stm32_register_composite,\ |
| 1020 | } |
| 1021 | |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 1022 | static const struct clock_config stm32mp1_clock_cfg[] = { |
| 1023 | /* Oscillator divider */ |
| 1024 | DIV(NO_ID, "clk-hsi-div", "clk-hsi", 0, RCC_HSICFGR, 0, 2, |
| 1025 | CLK_DIVIDER_READ_ONLY), |
| 1026 | |
| 1027 | /* External / Internal Oscillators */ |
Gabriel Fernandez | 8e6c27c | 2018-03-08 17:53:56 +0100 | [diff] [blame] | 1028 | GATE_MP1(CK_HSE, "ck_hse", "clk-hse", 0, RCC_OCENSETR, 8, 0), |
| 1029 | GATE_MP1(CK_CSI, "ck_csi", "clk-csi", 0, RCC_OCENSETR, 4, 0), |
| 1030 | 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] | 1031 | GATE(CK_LSI, "ck_lsi", "clk-lsi", 0, RCC_RDLSICR, 0, 0), |
| 1032 | GATE(CK_LSE, "ck_lse", "clk-lse", 0, RCC_BDCR, 0, 0), |
| 1033 | |
| 1034 | 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] | 1035 | |
| 1036 | /* ref clock pll */ |
| 1037 | MUX(NO_ID, "ref1", ref12_parents, CLK_OPS_PARENT_ENABLE, RCC_RCK12SELR, |
| 1038 | 0, 2, CLK_MUX_READ_ONLY), |
| 1039 | |
| 1040 | MUX(NO_ID, "ref3", ref3_parents, CLK_OPS_PARENT_ENABLE, RCC_RCK3SELR, |
| 1041 | 0, 2, CLK_MUX_READ_ONLY), |
| 1042 | |
| 1043 | MUX(NO_ID, "ref4", ref4_parents, CLK_OPS_PARENT_ENABLE, RCC_RCK4SELR, |
| 1044 | 0, 2, CLK_MUX_READ_ONLY), |
Gabriel Fernandez | c6cf4d3 | 2018-03-08 17:53:58 +0100 | [diff] [blame] | 1045 | |
| 1046 | /* PLLs */ |
| 1047 | PLL(PLL1, "pll1", "ref1", CLK_IGNORE_UNUSED, RCC_PLL1CR), |
| 1048 | PLL(PLL2, "pll2", "ref1", CLK_IGNORE_UNUSED, RCC_PLL2CR), |
| 1049 | PLL(PLL3, "pll3", "ref3", CLK_IGNORE_UNUSED, RCC_PLL3CR), |
| 1050 | PLL(PLL4, "pll4", "ref4", CLK_IGNORE_UNUSED, RCC_PLL4CR), |
Gabriel Fernandez | a97703c | 2018-03-08 17:53:59 +0100 | [diff] [blame] | 1051 | |
| 1052 | /* ODF */ |
| 1053 | COMPOSITE(PLL1_P, "pll1_p", PARENT("pll1"), 0, |
| 1054 | _GATE(RCC_PLL1CR, 4, 0), |
| 1055 | _NO_MUX, |
| 1056 | _DIV(RCC_PLL1CFGR2, 0, 7, 0, NULL)), |
| 1057 | |
| 1058 | COMPOSITE(PLL2_P, "pll2_p", PARENT("pll2"), 0, |
| 1059 | _GATE(RCC_PLL2CR, 4, 0), |
| 1060 | _NO_MUX, |
| 1061 | _DIV(RCC_PLL2CFGR2, 0, 7, 0, NULL)), |
| 1062 | |
| 1063 | COMPOSITE(PLL2_Q, "pll2_q", PARENT("pll2"), 0, |
| 1064 | _GATE(RCC_PLL2CR, 5, 0), |
| 1065 | _NO_MUX, |
| 1066 | _DIV(RCC_PLL2CFGR2, 8, 7, 0, NULL)), |
| 1067 | |
| 1068 | COMPOSITE(PLL2_R, "pll2_r", PARENT("pll2"), CLK_IS_CRITICAL, |
| 1069 | _GATE(RCC_PLL2CR, 6, 0), |
| 1070 | _NO_MUX, |
| 1071 | _DIV(RCC_PLL2CFGR2, 16, 7, 0, NULL)), |
| 1072 | |
| 1073 | COMPOSITE(PLL3_P, "pll3_p", PARENT("pll3"), 0, |
| 1074 | _GATE(RCC_PLL3CR, 4, 0), |
| 1075 | _NO_MUX, |
| 1076 | _DIV(RCC_PLL3CFGR2, 0, 7, 0, NULL)), |
| 1077 | |
| 1078 | COMPOSITE(PLL3_Q, "pll3_q", PARENT("pll3"), 0, |
| 1079 | _GATE(RCC_PLL3CR, 5, 0), |
| 1080 | _NO_MUX, |
| 1081 | _DIV(RCC_PLL3CFGR2, 8, 7, 0, NULL)), |
| 1082 | |
| 1083 | COMPOSITE(PLL3_R, "pll3_r", PARENT("pll3"), 0, |
| 1084 | _GATE(RCC_PLL3CR, 6, 0), |
| 1085 | _NO_MUX, |
| 1086 | _DIV(RCC_PLL3CFGR2, 16, 7, 0, NULL)), |
| 1087 | |
| 1088 | COMPOSITE(PLL4_P, "pll4_p", PARENT("pll4"), 0, |
| 1089 | _GATE(RCC_PLL4CR, 4, 0), |
| 1090 | _NO_MUX, |
| 1091 | _DIV(RCC_PLL4CFGR2, 0, 7, 0, NULL)), |
| 1092 | |
| 1093 | COMPOSITE(PLL4_Q, "pll4_q", PARENT("pll4"), 0, |
| 1094 | _GATE(RCC_PLL4CR, 5, 0), |
| 1095 | _NO_MUX, |
| 1096 | _DIV(RCC_PLL4CFGR2, 8, 7, 0, NULL)), |
| 1097 | |
| 1098 | COMPOSITE(PLL4_R, "pll4_r", PARENT("pll4"), 0, |
| 1099 | _GATE(RCC_PLL4CR, 6, 0), |
| 1100 | _NO_MUX, |
| 1101 | _DIV(RCC_PLL4CFGR2, 16, 7, 0, NULL)), |
Gabriel Fernandez | e51d297 | 2018-03-08 17:54:00 +0100 | [diff] [blame] | 1102 | |
| 1103 | /* MUX system clocks */ |
| 1104 | MUX(CK_PER, "ck_per", per_src, CLK_OPS_PARENT_ENABLE, |
| 1105 | RCC_CPERCKSELR, 0, 2, 0), |
| 1106 | |
| 1107 | MUX(CK_MPU, "ck_mpu", cpu_src, CLK_OPS_PARENT_ENABLE | |
| 1108 | CLK_IS_CRITICAL, RCC_MPCKSELR, 0, 2, 0), |
| 1109 | |
| 1110 | COMPOSITE(CK_AXI, "ck_axi", axi_src, CLK_IS_CRITICAL | |
| 1111 | CLK_OPS_PARENT_ENABLE, |
| 1112 | _NO_GATE, |
| 1113 | _MUX(RCC_ASSCKSELR, 0, 2, 0), |
| 1114 | _DIV(RCC_AXIDIVR, 0, 3, 0, axi_div_table)), |
| 1115 | |
| 1116 | COMPOSITE(CK_MCU, "ck_mcu", mcu_src, CLK_IS_CRITICAL | |
| 1117 | CLK_OPS_PARENT_ENABLE, |
| 1118 | _NO_GATE, |
| 1119 | _MUX(RCC_MSSCKSELR, 0, 2, 0), |
| 1120 | _DIV(RCC_MCUDIVR, 0, 4, 0, mcu_div_table)), |
| 1121 | |
| 1122 | DIV_TABLE(NO_ID, "pclk1", "ck_mcu", CLK_IGNORE_UNUSED, RCC_APB1DIVR, 0, |
| 1123 | 3, CLK_DIVIDER_READ_ONLY, apb_div_table), |
| 1124 | |
| 1125 | DIV_TABLE(NO_ID, "pclk2", "ck_mcu", CLK_IGNORE_UNUSED, RCC_APB2DIVR, 0, |
| 1126 | 3, CLK_DIVIDER_READ_ONLY, apb_div_table), |
| 1127 | |
| 1128 | DIV_TABLE(NO_ID, "pclk3", "ck_mcu", CLK_IGNORE_UNUSED, RCC_APB3DIVR, 0, |
| 1129 | 3, CLK_DIVIDER_READ_ONLY, apb_div_table), |
| 1130 | |
| 1131 | DIV_TABLE(NO_ID, "pclk4", "ck_axi", CLK_IGNORE_UNUSED, RCC_APB4DIVR, 0, |
| 1132 | 3, CLK_DIVIDER_READ_ONLY, apb_div_table), |
| 1133 | |
| 1134 | DIV_TABLE(NO_ID, "pclk5", "ck_axi", CLK_IGNORE_UNUSED, RCC_APB5DIVR, 0, |
| 1135 | 3, CLK_DIVIDER_READ_ONLY, apb_div_table), |
Gabriel Fernandez | 799b6a1 | 2018-03-08 17:54:01 +0100 | [diff] [blame^] | 1136 | |
| 1137 | /* Kernel Timers */ |
| 1138 | STM32_CKTIM("ck1_tim", "pclk1", 0, RCC_APB1DIVR, RCC_TIMG1PRER), |
| 1139 | STM32_CKTIM("ck2_tim", "pclk2", 0, RCC_APB2DIVR, RCC_TIMG2PRER), |
| 1140 | |
| 1141 | STM32_TIM(TIM2_K, "tim2_k", "ck1_tim", RCC_APB1ENSETR, 0), |
| 1142 | STM32_TIM(TIM3_K, "tim3_k", "ck1_tim", RCC_APB1ENSETR, 1), |
| 1143 | STM32_TIM(TIM4_K, "tim4_k", "ck1_tim", RCC_APB1ENSETR, 2), |
| 1144 | STM32_TIM(TIM5_K, "tim5_k", "ck1_tim", RCC_APB1ENSETR, 3), |
| 1145 | STM32_TIM(TIM6_K, "tim6_k", "ck1_tim", RCC_APB1ENSETR, 4), |
| 1146 | STM32_TIM(TIM7_K, "tim7_k", "ck1_tim", RCC_APB1ENSETR, 5), |
| 1147 | STM32_TIM(TIM12_K, "tim12_k", "ck1_tim", RCC_APB1ENSETR, 6), |
| 1148 | STM32_TIM(TIM13_K, "tim13_k", "ck1_tim", RCC_APB1ENSETR, 7), |
| 1149 | STM32_TIM(TIM14_K, "tim14_k", "ck1_tim", RCC_APB1ENSETR, 8), |
| 1150 | STM32_TIM(TIM1_K, "tim1_k", "ck2_tim", RCC_APB2ENSETR, 0), |
| 1151 | STM32_TIM(TIM8_K, "tim8_k", "ck2_tim", RCC_APB2ENSETR, 1), |
| 1152 | STM32_TIM(TIM15_K, "tim15_k", "ck2_tim", RCC_APB2ENSETR, 2), |
| 1153 | STM32_TIM(TIM16_K, "tim16_k", "ck2_tim", RCC_APB2ENSETR, 3), |
| 1154 | STM32_TIM(TIM17_K, "tim17_k", "ck2_tim", RCC_APB2ENSETR, 4), |
Gabriel Fernandez | 9bee94e | 2018-03-08 17:53:55 +0100 | [diff] [blame] | 1155 | }; |
| 1156 | |
| 1157 | struct stm32_clock_match_data { |
| 1158 | const struct clock_config *cfg; |
| 1159 | unsigned int num; |
| 1160 | unsigned int maxbinding; |
| 1161 | }; |
| 1162 | |
| 1163 | static struct stm32_clock_match_data stm32mp1_data = { |
| 1164 | .cfg = stm32mp1_clock_cfg, |
| 1165 | .num = ARRAY_SIZE(stm32mp1_clock_cfg), |
| 1166 | .maxbinding = STM32MP1_LAST_CLK, |
| 1167 | }; |
| 1168 | |
| 1169 | static const struct of_device_id stm32mp1_match_data[] = { |
| 1170 | { |
| 1171 | .compatible = "st,stm32mp1-rcc", |
| 1172 | .data = &stm32mp1_data, |
| 1173 | }, |
| 1174 | { } |
| 1175 | }; |
| 1176 | |
| 1177 | static int stm32_register_hw_clk(struct device *dev, |
| 1178 | struct clk_hw_onecell_data *clk_data, |
| 1179 | void __iomem *base, spinlock_t *lock, |
| 1180 | const struct clock_config *cfg) |
| 1181 | { |
| 1182 | static struct clk_hw **hws; |
| 1183 | struct clk_hw *hw = ERR_PTR(-ENOENT); |
| 1184 | |
| 1185 | hws = clk_data->hws; |
| 1186 | |
| 1187 | if (cfg->func) |
| 1188 | hw = (*cfg->func)(dev, clk_data, base, lock, cfg); |
| 1189 | |
| 1190 | if (IS_ERR(hw)) { |
| 1191 | pr_err("Unable to register %s\n", cfg->name); |
| 1192 | return PTR_ERR(hw); |
| 1193 | } |
| 1194 | |
| 1195 | if (cfg->id != NO_ID) |
| 1196 | hws[cfg->id] = hw; |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | static int stm32_rcc_init(struct device_node *np, |
| 1202 | void __iomem *base, |
| 1203 | const struct of_device_id *match_data) |
| 1204 | { |
| 1205 | struct clk_hw_onecell_data *clk_data; |
| 1206 | struct clk_hw **hws; |
| 1207 | const struct of_device_id *match; |
| 1208 | const struct stm32_clock_match_data *data; |
| 1209 | int err, n, max_binding; |
| 1210 | |
| 1211 | match = of_match_node(match_data, np); |
| 1212 | if (!match) { |
| 1213 | pr_err("%s: match data not found\n", __func__); |
| 1214 | return -ENODEV; |
| 1215 | } |
| 1216 | |
| 1217 | data = match->data; |
| 1218 | |
| 1219 | max_binding = data->maxbinding; |
| 1220 | |
| 1221 | clk_data = kzalloc(sizeof(*clk_data) + |
| 1222 | sizeof(*clk_data->hws) * max_binding, |
| 1223 | GFP_KERNEL); |
| 1224 | if (!clk_data) |
| 1225 | return -ENOMEM; |
| 1226 | |
| 1227 | clk_data->num = max_binding; |
| 1228 | |
| 1229 | hws = clk_data->hws; |
| 1230 | |
| 1231 | for (n = 0; n < max_binding; n++) |
| 1232 | hws[n] = ERR_PTR(-ENOENT); |
| 1233 | |
| 1234 | for (n = 0; n < data->num; n++) { |
| 1235 | err = stm32_register_hw_clk(NULL, clk_data, base, &rlock, |
| 1236 | &data->cfg[n]); |
| 1237 | if (err) { |
| 1238 | pr_err("%s: can't register %s\n", __func__, |
| 1239 | data->cfg[n].name); |
| 1240 | |
| 1241 | kfree(clk_data); |
| 1242 | |
| 1243 | return err; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data); |
| 1248 | } |
| 1249 | |
| 1250 | static void stm32mp1_rcc_init(struct device_node *np) |
| 1251 | { |
| 1252 | void __iomem *base; |
| 1253 | |
| 1254 | base = of_iomap(np, 0); |
| 1255 | if (!base) { |
| 1256 | pr_err("%s: unable to map resource", np->name); |
| 1257 | of_node_put(np); |
| 1258 | return; |
| 1259 | } |
| 1260 | |
| 1261 | if (stm32_rcc_init(np, base, stm32mp1_match_data)) { |
| 1262 | iounmap(base); |
| 1263 | of_node_put(np); |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | CLK_OF_DECLARE_DRIVER(stm32mp1_rcc, "st,stm32mp1-rcc", stm32mp1_rcc_init); |