blob: 77d96d9acad6ecc553584ad015f311c8dfe724ce [file] [log] [blame]
Gabriel Fernandez9bee94e2018-03-08 17:53:55 +01001// 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 Fernandezc6cf4d32018-03-08 17:53:58 +010010#include <linux/delay.h>
Gabriel Fernandez9bee94e2018-03-08 17:53:55 +010011#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
20static 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 Fernandezdc32eaa2018-03-08 17:53:57 +0100107static const char * const ref12_parents[] = {
108 "ck_hsi", "ck_hse"
109};
110
111static const char * const ref3_parents[] = {
112 "ck_hsi", "ck_hse", "ck_csi"
113};
114
115static const char * const ref4_parents[] = {
116 "ck_hsi", "ck_hse", "ck_csi"
117};
118
Gabriel Fernandeze51d2972018-03-08 17:54:00 +0100119static const char * const cpu_src[] = {
120 "ck_hsi", "ck_hse", "pll1_p"
121};
122
123static const char * const axi_src[] = {
124 "ck_hsi", "ck_hse", "pll2_p", "pll3_p"
125};
126
127static const char * const per_src[] = {
128 "ck_hsi", "ck_csi", "ck_hse"
129};
130
131static const char * const mcu_src[] = {
132 "ck_hsi", "ck_hse", "ck_csi", "pll3_p"
133};
134
135static 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
141static 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
149static 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 Fernandez9bee94e2018-03-08 17:53:55 +0100155struct 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
173struct gate_cfg {
174 u32 reg_off;
175 u8 bit_idx;
176 u8 gate_flags;
177};
178
179struct fixed_factor_cfg {
180 unsigned int mult;
181 unsigned int div;
182};
183
184struct 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 Fernandezdc32eaa2018-03-08 17:53:57 +0100192struct mux_cfg {
193 u32 reg_off;
194 u8 shift;
195 u8 width;
196 u8 mux_flags;
197 u32 *table;
198};
199
Gabriel Fernandez8e6c27c2018-03-08 17:53:56 +0100200struct stm32_gate_cfg {
201 struct gate_cfg *gate;
202 const struct clk_ops *ops;
203};
204
Gabriel Fernandeza97703c2018-03-08 17:53:59 +0100205struct stm32_div_cfg {
206 struct div_cfg *div;
207 const struct clk_ops *ops;
208};
209
210struct stm32_mux_cfg {
211 struct mux_cfg *mux;
212 const struct clk_ops *ops;
213};
214
215/* STM32 Composite clock */
216struct 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 Fernandez9bee94e2018-03-08 17:53:55 +0100222static 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
240static 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
253static 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 Fernandezdc32eaa2018-03-08 17:53:57 +0100273static 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 Fernandez8e6c27c2018-03-08 17:53:56 +0100287/* MP1 Gate clock with set & clear registers */
288
289static 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
297static 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
309const 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 Fernandeza97703c2018-03-08 17:53:59 +0100315static 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
339static 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 Fernandez8e6c27c2018-03-08 17:53:56 +0100360static 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
380static struct clk_hw *
381clk_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 Fernandeza97703c2018-03-08 17:53:59 +0100423static struct clk_hw *
424clk_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 Fernandezc6cf4d32018-03-08 17:53:58 +0100478/* STM32 PLL */
479
480struct 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
500static 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
509static 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
539unlock:
540 spin_unlock_irqrestore(clk_elem->lock, flags);
541
542 return bit_status;
543}
544
545static 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
560static 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
572static 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
597static 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
610static 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
617static 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
653struct stm32_pll_cfg {
654 u32 offset;
655};
656
657struct clk_hw *_clk_register_pll(struct device *dev,
658 struct clk_hw_onecell_data *clk_data,
659 void __iomem *base, spinlock_t *lock,
660 const struct clock_config *cfg)
661{
662 struct stm32_pll_cfg *stm_pll_cfg = cfg->cfg;
663
664 return clk_register_pll(dev, cfg->name, cfg->parent_name,
665 base + stm_pll_cfg->offset, cfg->flags, lock);
666}
667
Gabriel Fernandez8e6c27c2018-03-08 17:53:56 +0100668static struct clk_hw *
669_clk_stm32_register_gate(struct device *dev,
670 struct clk_hw_onecell_data *clk_data,
671 void __iomem *base, spinlock_t *lock,
672 const struct clock_config *cfg)
673{
674 return clk_stm32_register_gate_ops(dev,
675 cfg->name,
676 cfg->parent_name,
677 cfg->flags,
678 base,
679 cfg->cfg,
680 lock);
681}
682
Gabriel Fernandeza97703c2018-03-08 17:53:59 +0100683static struct clk_hw *
684_clk_stm32_register_composite(struct device *dev,
685 struct clk_hw_onecell_data *clk_data,
686 void __iomem *base, spinlock_t *lock,
687 const struct clock_config *cfg)
688{
689 return clk_stm32_register_composite(dev, cfg->name, cfg->parent_names,
690 cfg->num_parents, base, cfg->cfg,
691 cfg->flags, lock);
692}
693
Gabriel Fernandez9bee94e2018-03-08 17:53:55 +0100694#define GATE(_id, _name, _parent, _flags, _offset, _bit_idx, _gate_flags)\
695{\
696 .id = _id,\
697 .name = _name,\
698 .parent_name = _parent,\
699 .flags = _flags,\
700 .cfg = &(struct gate_cfg) {\
701 .reg_off = _offset,\
702 .bit_idx = _bit_idx,\
703 .gate_flags = _gate_flags,\
704 },\
705 .func = _clk_hw_register_gate,\
706}
707
708#define FIXED_FACTOR(_id, _name, _parent, _flags, _mult, _div)\
709{\
710 .id = _id,\
711 .name = _name,\
712 .parent_name = _parent,\
713 .flags = _flags,\
714 .cfg = &(struct fixed_factor_cfg) {\
715 .mult = _mult,\
716 .div = _div,\
717 },\
718 .func = _clk_hw_register_fixed_factor,\
719}
720
721#define DIV_TABLE(_id, _name, _parent, _flags, _offset, _shift, _width,\
722 _div_flags, _div_table)\
723{\
724 .id = _id,\
725 .name = _name,\
726 .parent_name = _parent,\
727 .flags = _flags,\
728 .cfg = &(struct div_cfg) {\
729 .reg_off = _offset,\
730 .shift = _shift,\
731 .width = _width,\
732 .div_flags = _div_flags,\
733 .table = _div_table,\
734 },\
735 .func = _clk_hw_register_divider_table,\
736}
737
738#define DIV(_id, _name, _parent, _flags, _offset, _shift, _width, _div_flags)\
739 DIV_TABLE(_id, _name, _parent, _flags, _offset, _shift, _width,\
740 _div_flags, NULL)
741
Gabriel Fernandezdc32eaa2018-03-08 17:53:57 +0100742#define MUX(_id, _name, _parents, _flags, _offset, _shift, _width, _mux_flags)\
743{\
744 .id = _id,\
745 .name = _name,\
746 .parent_names = _parents,\
747 .num_parents = ARRAY_SIZE(_parents),\
748 .flags = _flags,\
749 .cfg = &(struct mux_cfg) {\
750 .reg_off = _offset,\
751 .shift = _shift,\
752 .width = _width,\
753 .mux_flags = _mux_flags,\
754 },\
755 .func = _clk_hw_register_mux,\
756}
757
Gabriel Fernandezc6cf4d32018-03-08 17:53:58 +0100758#define PLL(_id, _name, _parent, _flags, _offset)\
759{\
760 .id = _id,\
761 .name = _name,\
762 .parent_name = _parent,\
763 .flags = _flags,\
764 .cfg = &(struct stm32_pll_cfg) {\
765 .offset = _offset,\
766 },\
767 .func = _clk_register_pll,\
768}
769
Gabriel Fernandez8e6c27c2018-03-08 17:53:56 +0100770/* STM32 GATE */
771#define STM32_GATE(_id, _name, _parent, _flags, _gate)\
772{\
773 .id = _id,\
774 .name = _name,\
775 .parent_name = _parent,\
776 .flags = _flags,\
777 .cfg = (struct stm32_gate_cfg *) {_gate},\
778 .func = _clk_stm32_register_gate,\
779}
780
781#define _STM32_GATE(_gate_offset, _gate_bit_idx, _gate_flags, _ops)\
782 (&(struct stm32_gate_cfg) {\
783 &(struct gate_cfg) {\
784 .reg_off = _gate_offset,\
785 .bit_idx = _gate_bit_idx,\
786 .gate_flags = _gate_flags,\
787 },\
788 .ops = _ops,\
789 })
790
Gabriel Fernandeza97703c2018-03-08 17:53:59 +0100791#define _GATE(_gate_offset, _gate_bit_idx, _gate_flags)\
792 _STM32_GATE(_gate_offset, _gate_bit_idx, _gate_flags,\
793 NULL)\
794
Gabriel Fernandez8e6c27c2018-03-08 17:53:56 +0100795#define _GATE_MP1(_gate_offset, _gate_bit_idx, _gate_flags)\
796 _STM32_GATE(_gate_offset, _gate_bit_idx, _gate_flags,\
797 &mp1_gate_clk_ops)\
798
799#define GATE_MP1(_id, _name, _parent, _flags, _offset, _bit_idx, _gate_flags)\
800 STM32_GATE(_id, _name, _parent, _flags,\
801 _GATE_MP1(_offset, _bit_idx, _gate_flags))
802
Gabriel Fernandeza97703c2018-03-08 17:53:59 +0100803#define _STM32_DIV(_div_offset, _div_shift, _div_width,\
804 _div_flags, _div_table, _ops)\
805 .div = &(struct stm32_div_cfg) {\
806 &(struct div_cfg) {\
807 .reg_off = _div_offset,\
808 .shift = _div_shift,\
809 .width = _div_width,\
810 .div_flags = _div_flags,\
811 .table = _div_table,\
812 },\
813 .ops = _ops,\
814 }
815
816#define _DIV(_div_offset, _div_shift, _div_width, _div_flags, _div_table)\
817 _STM32_DIV(_div_offset, _div_shift, _div_width,\
818 _div_flags, _div_table, NULL)\
819
Gabriel Fernandeze51d2972018-03-08 17:54:00 +0100820#define _STM32_MUX(_offset, _shift, _width, _mux_flags, _ops)\
821 .mux = &(struct stm32_mux_cfg) {\
822 &(struct mux_cfg) {\
823 .reg_off = _offset,\
824 .shift = _shift,\
825 .width = _width,\
826 .mux_flags = _mux_flags,\
827 .table = NULL,\
828 },\
829 .ops = _ops,\
830 }
831
832#define _MUX(_offset, _shift, _width, _mux_flags)\
833 _STM32_MUX(_offset, _shift, _width, _mux_flags, NULL)\
834
Gabriel Fernandeza97703c2018-03-08 17:53:59 +0100835#define PARENT(_parent) ((const char *[]) { _parent})
836
837#define _NO_MUX .mux = NULL
838#define _NO_DIV .div = NULL
839#define _NO_GATE .gate = NULL
840
841#define COMPOSITE(_id, _name, _parents, _flags, _gate, _mux, _div)\
842{\
843 .id = _id,\
844 .name = _name,\
845 .parent_names = _parents,\
846 .num_parents = ARRAY_SIZE(_parents),\
847 .flags = _flags,\
848 .cfg = &(struct stm32_composite_cfg) {\
849 _gate,\
850 _mux,\
851 _div,\
852 },\
853 .func = _clk_stm32_register_composite,\
854}
855
Gabriel Fernandez9bee94e2018-03-08 17:53:55 +0100856static const struct clock_config stm32mp1_clock_cfg[] = {
857 /* Oscillator divider */
858 DIV(NO_ID, "clk-hsi-div", "clk-hsi", 0, RCC_HSICFGR, 0, 2,
859 CLK_DIVIDER_READ_ONLY),
860
861 /* External / Internal Oscillators */
Gabriel Fernandez8e6c27c2018-03-08 17:53:56 +0100862 GATE_MP1(CK_HSE, "ck_hse", "clk-hse", 0, RCC_OCENSETR, 8, 0),
863 GATE_MP1(CK_CSI, "ck_csi", "clk-csi", 0, RCC_OCENSETR, 4, 0),
864 GATE_MP1(CK_HSI, "ck_hsi", "clk-hsi-div", 0, RCC_OCENSETR, 0, 0),
Gabriel Fernandez9bee94e2018-03-08 17:53:55 +0100865 GATE(CK_LSI, "ck_lsi", "clk-lsi", 0, RCC_RDLSICR, 0, 0),
866 GATE(CK_LSE, "ck_lse", "clk-lse", 0, RCC_BDCR, 0, 0),
867
868 FIXED_FACTOR(CK_HSE_DIV2, "clk-hse-div2", "ck_hse", 0, 1, 2),
Gabriel Fernandezdc32eaa2018-03-08 17:53:57 +0100869
870 /* ref clock pll */
871 MUX(NO_ID, "ref1", ref12_parents, CLK_OPS_PARENT_ENABLE, RCC_RCK12SELR,
872 0, 2, CLK_MUX_READ_ONLY),
873
874 MUX(NO_ID, "ref3", ref3_parents, CLK_OPS_PARENT_ENABLE, RCC_RCK3SELR,
875 0, 2, CLK_MUX_READ_ONLY),
876
877 MUX(NO_ID, "ref4", ref4_parents, CLK_OPS_PARENT_ENABLE, RCC_RCK4SELR,
878 0, 2, CLK_MUX_READ_ONLY),
Gabriel Fernandezc6cf4d32018-03-08 17:53:58 +0100879
880 /* PLLs */
881 PLL(PLL1, "pll1", "ref1", CLK_IGNORE_UNUSED, RCC_PLL1CR),
882 PLL(PLL2, "pll2", "ref1", CLK_IGNORE_UNUSED, RCC_PLL2CR),
883 PLL(PLL3, "pll3", "ref3", CLK_IGNORE_UNUSED, RCC_PLL3CR),
884 PLL(PLL4, "pll4", "ref4", CLK_IGNORE_UNUSED, RCC_PLL4CR),
Gabriel Fernandeza97703c2018-03-08 17:53:59 +0100885
886 /* ODF */
887 COMPOSITE(PLL1_P, "pll1_p", PARENT("pll1"), 0,
888 _GATE(RCC_PLL1CR, 4, 0),
889 _NO_MUX,
890 _DIV(RCC_PLL1CFGR2, 0, 7, 0, NULL)),
891
892 COMPOSITE(PLL2_P, "pll2_p", PARENT("pll2"), 0,
893 _GATE(RCC_PLL2CR, 4, 0),
894 _NO_MUX,
895 _DIV(RCC_PLL2CFGR2, 0, 7, 0, NULL)),
896
897 COMPOSITE(PLL2_Q, "pll2_q", PARENT("pll2"), 0,
898 _GATE(RCC_PLL2CR, 5, 0),
899 _NO_MUX,
900 _DIV(RCC_PLL2CFGR2, 8, 7, 0, NULL)),
901
902 COMPOSITE(PLL2_R, "pll2_r", PARENT("pll2"), CLK_IS_CRITICAL,
903 _GATE(RCC_PLL2CR, 6, 0),
904 _NO_MUX,
905 _DIV(RCC_PLL2CFGR2, 16, 7, 0, NULL)),
906
907 COMPOSITE(PLL3_P, "pll3_p", PARENT("pll3"), 0,
908 _GATE(RCC_PLL3CR, 4, 0),
909 _NO_MUX,
910 _DIV(RCC_PLL3CFGR2, 0, 7, 0, NULL)),
911
912 COMPOSITE(PLL3_Q, "pll3_q", PARENT("pll3"), 0,
913 _GATE(RCC_PLL3CR, 5, 0),
914 _NO_MUX,
915 _DIV(RCC_PLL3CFGR2, 8, 7, 0, NULL)),
916
917 COMPOSITE(PLL3_R, "pll3_r", PARENT("pll3"), 0,
918 _GATE(RCC_PLL3CR, 6, 0),
919 _NO_MUX,
920 _DIV(RCC_PLL3CFGR2, 16, 7, 0, NULL)),
921
922 COMPOSITE(PLL4_P, "pll4_p", PARENT("pll4"), 0,
923 _GATE(RCC_PLL4CR, 4, 0),
924 _NO_MUX,
925 _DIV(RCC_PLL4CFGR2, 0, 7, 0, NULL)),
926
927 COMPOSITE(PLL4_Q, "pll4_q", PARENT("pll4"), 0,
928 _GATE(RCC_PLL4CR, 5, 0),
929 _NO_MUX,
930 _DIV(RCC_PLL4CFGR2, 8, 7, 0, NULL)),
931
932 COMPOSITE(PLL4_R, "pll4_r", PARENT("pll4"), 0,
933 _GATE(RCC_PLL4CR, 6, 0),
934 _NO_MUX,
935 _DIV(RCC_PLL4CFGR2, 16, 7, 0, NULL)),
Gabriel Fernandeze51d2972018-03-08 17:54:00 +0100936
937 /* MUX system clocks */
938 MUX(CK_PER, "ck_per", per_src, CLK_OPS_PARENT_ENABLE,
939 RCC_CPERCKSELR, 0, 2, 0),
940
941 MUX(CK_MPU, "ck_mpu", cpu_src, CLK_OPS_PARENT_ENABLE |
942 CLK_IS_CRITICAL, RCC_MPCKSELR, 0, 2, 0),
943
944 COMPOSITE(CK_AXI, "ck_axi", axi_src, CLK_IS_CRITICAL |
945 CLK_OPS_PARENT_ENABLE,
946 _NO_GATE,
947 _MUX(RCC_ASSCKSELR, 0, 2, 0),
948 _DIV(RCC_AXIDIVR, 0, 3, 0, axi_div_table)),
949
950 COMPOSITE(CK_MCU, "ck_mcu", mcu_src, CLK_IS_CRITICAL |
951 CLK_OPS_PARENT_ENABLE,
952 _NO_GATE,
953 _MUX(RCC_MSSCKSELR, 0, 2, 0),
954 _DIV(RCC_MCUDIVR, 0, 4, 0, mcu_div_table)),
955
956 DIV_TABLE(NO_ID, "pclk1", "ck_mcu", CLK_IGNORE_UNUSED, RCC_APB1DIVR, 0,
957 3, CLK_DIVIDER_READ_ONLY, apb_div_table),
958
959 DIV_TABLE(NO_ID, "pclk2", "ck_mcu", CLK_IGNORE_UNUSED, RCC_APB2DIVR, 0,
960 3, CLK_DIVIDER_READ_ONLY, apb_div_table),
961
962 DIV_TABLE(NO_ID, "pclk3", "ck_mcu", CLK_IGNORE_UNUSED, RCC_APB3DIVR, 0,
963 3, CLK_DIVIDER_READ_ONLY, apb_div_table),
964
965 DIV_TABLE(NO_ID, "pclk4", "ck_axi", CLK_IGNORE_UNUSED, RCC_APB4DIVR, 0,
966 3, CLK_DIVIDER_READ_ONLY, apb_div_table),
967
968 DIV_TABLE(NO_ID, "pclk5", "ck_axi", CLK_IGNORE_UNUSED, RCC_APB5DIVR, 0,
969 3, CLK_DIVIDER_READ_ONLY, apb_div_table),
Gabriel Fernandez9bee94e2018-03-08 17:53:55 +0100970};
971
972struct stm32_clock_match_data {
973 const struct clock_config *cfg;
974 unsigned int num;
975 unsigned int maxbinding;
976};
977
978static struct stm32_clock_match_data stm32mp1_data = {
979 .cfg = stm32mp1_clock_cfg,
980 .num = ARRAY_SIZE(stm32mp1_clock_cfg),
981 .maxbinding = STM32MP1_LAST_CLK,
982};
983
984static const struct of_device_id stm32mp1_match_data[] = {
985 {
986 .compatible = "st,stm32mp1-rcc",
987 .data = &stm32mp1_data,
988 },
989 { }
990};
991
992static int stm32_register_hw_clk(struct device *dev,
993 struct clk_hw_onecell_data *clk_data,
994 void __iomem *base, spinlock_t *lock,
995 const struct clock_config *cfg)
996{
997 static struct clk_hw **hws;
998 struct clk_hw *hw = ERR_PTR(-ENOENT);
999
1000 hws = clk_data->hws;
1001
1002 if (cfg->func)
1003 hw = (*cfg->func)(dev, clk_data, base, lock, cfg);
1004
1005 if (IS_ERR(hw)) {
1006 pr_err("Unable to register %s\n", cfg->name);
1007 return PTR_ERR(hw);
1008 }
1009
1010 if (cfg->id != NO_ID)
1011 hws[cfg->id] = hw;
1012
1013 return 0;
1014}
1015
1016static int stm32_rcc_init(struct device_node *np,
1017 void __iomem *base,
1018 const struct of_device_id *match_data)
1019{
1020 struct clk_hw_onecell_data *clk_data;
1021 struct clk_hw **hws;
1022 const struct of_device_id *match;
1023 const struct stm32_clock_match_data *data;
1024 int err, n, max_binding;
1025
1026 match = of_match_node(match_data, np);
1027 if (!match) {
1028 pr_err("%s: match data not found\n", __func__);
1029 return -ENODEV;
1030 }
1031
1032 data = match->data;
1033
1034 max_binding = data->maxbinding;
1035
1036 clk_data = kzalloc(sizeof(*clk_data) +
1037 sizeof(*clk_data->hws) * max_binding,
1038 GFP_KERNEL);
1039 if (!clk_data)
1040 return -ENOMEM;
1041
1042 clk_data->num = max_binding;
1043
1044 hws = clk_data->hws;
1045
1046 for (n = 0; n < max_binding; n++)
1047 hws[n] = ERR_PTR(-ENOENT);
1048
1049 for (n = 0; n < data->num; n++) {
1050 err = stm32_register_hw_clk(NULL, clk_data, base, &rlock,
1051 &data->cfg[n]);
1052 if (err) {
1053 pr_err("%s: can't register %s\n", __func__,
1054 data->cfg[n].name);
1055
1056 kfree(clk_data);
1057
1058 return err;
1059 }
1060 }
1061
1062 return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data);
1063}
1064
1065static void stm32mp1_rcc_init(struct device_node *np)
1066{
1067 void __iomem *base;
1068
1069 base = of_iomap(np, 0);
1070 if (!base) {
1071 pr_err("%s: unable to map resource", np->name);
1072 of_node_put(np);
1073 return;
1074 }
1075
1076 if (stm32_rcc_init(np, base, stm32mp1_match_data)) {
1077 iounmap(base);
1078 of_node_put(np);
1079 }
1080}
1081
1082CLK_OF_DECLARE_DRIVER(stm32mp1_rcc, "st,stm32mp1-rcc", stm32mp1_rcc_init);