1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2019-20 Sean Anderson <seanga2@gmail.com>
4 * Copyright (c) 2019 Western Digital Corporation or its affiliates.
6 #define pr_fmt(fmt) "k210-clk: " fmt
9 #include <linux/slab.h>
10 #include <linux/spinlock.h>
11 #include <linux/platform_device.h>
13 #include <linux/of_clk.h>
14 #include <linux/of_platform.h>
15 #include <linux/of_address.h>
16 #include <linux/clk-provider.h>
17 #include <linux/bitfield.h>
18 #include <linux/delay.h>
19 #include <soc/canaan/k210-sysctl.h>
21 #include <dt-bindings/clock/k210-clk.h>
27 struct k210_sysclk *ksc;
43 enum k210_clk_div_type {
46 K210_DIV_DOUBLE_ONE_BASED,
47 K210_DIV_POWER_OF_TWO,
50 #define K210_GATE(_reg, _bit) \
54 #define K210_DIV(_reg, _shift, _width, _type) \
56 .div_shift = (_shift), \
57 .div_width = (_width), \
60 #define K210_MUX(_reg, _bit) \
64 static struct k210_clk_cfg k210_clk_cfgs[K210_NUM_CLKS] = {
65 /* Gated clocks, no mux, no divider */
68 K210_GATE(K210_SYSCTL_EN_CENT, 0)
72 K210_GATE(K210_SYSCTL_EN_PERI, 1)
76 K210_GATE(K210_SYSCTL_EN_PERI, 4)
80 K210_GATE(K210_SYSCTL_EN_PERI, 5)
84 K210_GATE(K210_SYSCTL_EN_PERI, 16)
88 K210_GATE(K210_SYSCTL_EN_PERI, 17)
92 K210_GATE(K210_SYSCTL_EN_PERI, 18)
96 K210_GATE(K210_SYSCTL_EN_PERI, 20)
100 K210_GATE(K210_SYSCTL_EN_PERI, 26)
104 K210_GATE(K210_SYSCTL_EN_PERI, 19)
108 K210_GATE(K210_SYSCTL_EN_PERI, 27)
112 K210_GATE(K210_SYSCTL_EN_PERI, 29)
115 /* Gated divider clocks */
118 K210_GATE(K210_SYSCTL_EN_CENT, 1),
119 K210_DIV(K210_SYSCTL_THR0, 0, 4, K210_DIV_ONE_BASED)
123 K210_GATE(K210_SYSCTL_EN_CENT, 2),
124 K210_DIV(K210_SYSCTL_THR0, 4, 4, K210_DIV_ONE_BASED)
128 K210_GATE(K210_SYSCTL_EN_PERI, 0),
129 K210_DIV(K210_SYSCTL_THR0, 16, 4, K210_DIV_ONE_BASED)
133 K210_GATE(K210_SYSCTL_EN_PERI, 3),
134 K210_DIV(K210_SYSCTL_THR0, 12, 4, K210_DIV_ONE_BASED)
138 K210_GATE(K210_SYSCTL_EN_CENT, 3),
139 K210_DIV(K210_SYSCTL_SEL0, 3, 3, K210_DIV_ONE_BASED)
143 K210_GATE(K210_SYSCTL_EN_CENT, 4),
144 K210_DIV(K210_SYSCTL_SEL0, 6, 3, K210_DIV_ONE_BASED)
148 K210_GATE(K210_SYSCTL_EN_CENT, 5),
149 K210_DIV(K210_SYSCTL_SEL0, 9, 3, K210_DIV_ONE_BASED)
153 K210_GATE(K210_SYSCTL_EN_PERI, 2),
154 K210_DIV(K210_SYSCTL_THR0, 8, 4, K210_DIV_ONE_BASED)
158 K210_GATE(K210_SYSCTL_EN_PERI, 6),
159 K210_DIV(K210_SYSCTL_THR1, 0, 8, K210_DIV_DOUBLE_ONE_BASED)
163 K210_GATE(K210_SYSCTL_EN_PERI, 7),
164 K210_DIV(K210_SYSCTL_THR1, 8, 8, K210_DIV_DOUBLE_ONE_BASED)
168 K210_GATE(K210_SYSCTL_EN_PERI, 8),
169 K210_DIV(K210_SYSCTL_THR1, 16, 8, K210_DIV_DOUBLE_ONE_BASED)
173 K210_GATE(K210_SYSCTL_EN_PERI, 13),
174 K210_DIV(K210_SYSCTL_THR5, 8, 8, K210_DIV_DOUBLE_ONE_BASED)
178 K210_GATE(K210_SYSCTL_EN_PERI, 14),
179 K210_DIV(K210_SYSCTL_THR5, 16, 8, K210_DIV_DOUBLE_ONE_BASED)
183 K210_GATE(K210_SYSCTL_EN_PERI, 15),
184 K210_DIV(K210_SYSCTL_THR5, 24, 8, K210_DIV_DOUBLE_ONE_BASED)
188 K210_GATE(K210_SYSCTL_EN_PERI, 24),
189 K210_DIV(K210_SYSCTL_THR6, 0, 8, K210_DIV_DOUBLE_ONE_BASED)
193 K210_GATE(K210_SYSCTL_EN_PERI, 25),
194 K210_DIV(K210_SYSCTL_THR6, 8, 8, K210_DIV_DOUBLE_ONE_BASED)
198 K210_GATE(K210_SYSCTL_EN_PERI, 10),
199 K210_DIV(K210_SYSCTL_THR3, 0, 16, K210_DIV_DOUBLE_ONE_BASED)
203 K210_GATE(K210_SYSCTL_EN_PERI, 11),
204 K210_DIV(K210_SYSCTL_THR3, 16, 16, K210_DIV_DOUBLE_ONE_BASED)
208 K210_GATE(K210_SYSCTL_EN_PERI, 12),
209 K210_DIV(K210_SYSCTL_THR4, 0, 16, K210_DIV_DOUBLE_ONE_BASED)
212 /* Divider clocks, no gate, no mux */
213 [K210_CLK_I2S0_M] = {
215 K210_DIV(K210_SYSCTL_THR4, 16, 8, K210_DIV_DOUBLE_ONE_BASED)
217 [K210_CLK_I2S1_M] = {
219 K210_DIV(K210_SYSCTL_THR4, 24, 8, K210_DIV_DOUBLE_ONE_BASED)
221 [K210_CLK_I2S2_M] = {
223 K210_DIV(K210_SYSCTL_THR4, 0, 8, K210_DIV_DOUBLE_ONE_BASED)
226 /* Muxed gated divider clocks */
229 K210_GATE(K210_SYSCTL_EN_PERI, 9),
230 K210_DIV(K210_SYSCTL_THR1, 24, 8, K210_DIV_DOUBLE_ONE_BASED),
231 K210_MUX(K210_SYSCTL_SEL0, 12)
233 [K210_CLK_TIMER0] = {
235 K210_GATE(K210_SYSCTL_EN_PERI, 21),
236 K210_DIV(K210_SYSCTL_THR2, 0, 8, K210_DIV_DOUBLE_ONE_BASED),
237 K210_MUX(K210_SYSCTL_SEL0, 13)
239 [K210_CLK_TIMER1] = {
241 K210_GATE(K210_SYSCTL_EN_PERI, 22),
242 K210_DIV(K210_SYSCTL_THR2, 8, 8, K210_DIV_DOUBLE_ONE_BASED),
243 K210_MUX(K210_SYSCTL_SEL0, 14)
245 [K210_CLK_TIMER2] = {
247 K210_GATE(K210_SYSCTL_EN_PERI, 23),
248 K210_DIV(K210_SYSCTL_THR2, 16, 8, K210_DIV_DOUBLE_ONE_BASED),
249 K210_MUX(K210_SYSCTL_SEL0, 15)
254 * PLL control register bits.
256 #define K210_PLL_CLKR GENMASK(3, 0)
257 #define K210_PLL_CLKF GENMASK(9, 4)
258 #define K210_PLL_CLKOD GENMASK(13, 10)
259 #define K210_PLL_BWADJ GENMASK(19, 14)
260 #define K210_PLL_RESET (1 << 20)
261 #define K210_PLL_PWRD (1 << 21)
262 #define K210_PLL_INTFB (1 << 22)
263 #define K210_PLL_BYPASS (1 << 23)
264 #define K210_PLL_TEST (1 << 24)
265 #define K210_PLL_EN (1 << 25)
266 #define K210_PLL_SEL GENMASK(27, 26) /* PLL2 only */
269 * PLL lock register bits.
271 #define K210_PLL_LOCK 0
272 #define K210_PLL_CLEAR_SLIP 2
273 #define K210_PLL_TEST_OUT 3
276 * Clock selector register bits.
278 #define K210_ACLK_SEL BIT(0)
279 #define K210_ACLK_DIV GENMASK(2, 1)
285 K210_PLL0, K210_PLL1, K210_PLL2, K210_PLL_NUM
290 struct k210_sysclk *ksc;
298 #define to_k210_pll(_hw) container_of(_hw, struct k210_pll, hw)
301 * PLLs configuration: by default PLL0 runs at 780 MHz and PLL1 at 299 MHz.
302 * The first 2 SRAM banks depend on ACLK/CPU clock which is by default PLL0
303 * rate divided by 2. Set PLL1 to 390 MHz so that the third SRAM bank has the
304 * same clock as the first 2.
306 struct k210_pll_cfg {
316 static struct k210_pll_cfg k210_plls_cfg[] = {
317 { K210_SYSCTL_PLL0, 0, 2, 0, 59, 1, 59 }, /* 780 MHz */
318 { K210_SYSCTL_PLL1, 8, 1, 0, 59, 3, 59 }, /* 390 MHz */
319 { K210_SYSCTL_PLL2, 16, 1, 0, 22, 1, 22 }, /* 299 MHz */
323 * struct k210_sysclk - sysclk driver data
324 * @regs: system controller registers start address
325 * @clk_lock: clock setting spinlock
326 * @plls: SoC PLLs descriptors
328 * @clks: All other clocks
333 struct k210_pll plls[K210_PLL_NUM];
335 struct k210_clk clks[K210_NUM_CLKS];
338 #define to_k210_sysclk(_hw) container_of(_hw, struct k210_sysclk, aclk)
341 * Set ACLK parent selector: 0 for IN0, 1 for PLL0.
343 static void k210_aclk_set_selector(void __iomem *regs, u8 sel)
345 u32 reg = readl(regs + K210_SYSCTL_SEL0);
348 reg |= K210_ACLK_SEL;
350 reg &= K210_ACLK_SEL;
351 writel(reg, regs + K210_SYSCTL_SEL0);
354 static void k210_init_pll(void __iomem *regs, enum k210_pll_id pllid,
355 struct k210_pll *pll)
358 pll->reg = regs + k210_plls_cfg[pllid].reg;
359 pll->lock = regs + K210_SYSCTL_PLL_LOCK;
360 pll->lock_shift = k210_plls_cfg[pllid].lock_shift;
361 pll->lock_width = k210_plls_cfg[pllid].lock_width;
364 static void k210_pll_wait_for_lock(struct k210_pll *pll)
366 u32 reg, mask = GENMASK(pll->lock_shift + pll->lock_width - 1,
370 reg = readl(pll->lock);
371 if ((reg & mask) == mask)
374 reg |= BIT(pll->lock_shift + K210_PLL_CLEAR_SLIP);
375 writel(reg, pll->lock);
379 static bool k210_pll_hw_is_enabled(struct k210_pll *pll)
381 u32 reg = readl(pll->reg);
382 u32 mask = K210_PLL_PWRD | K210_PLL_EN;
384 if (reg & K210_PLL_RESET)
387 return (reg & mask) == mask;
390 static void k210_pll_enable_hw(void __iomem *regs, struct k210_pll *pll)
392 struct k210_pll_cfg *pll_cfg = &k210_plls_cfg[pll->id];
395 if (k210_pll_hw_is_enabled(pll))
399 * For PLL0, we need to re-parent ACLK to IN0 to keep the CPU cores and
402 if (pll->id == K210_PLL0)
403 k210_aclk_set_selector(regs, 0);
405 /* Set PLL factors */
406 reg = readl(pll->reg);
407 reg &= ~GENMASK(19, 0);
408 reg |= FIELD_PREP(K210_PLL_CLKR, pll_cfg->r);
409 reg |= FIELD_PREP(K210_PLL_CLKF, pll_cfg->f);
410 reg |= FIELD_PREP(K210_PLL_CLKOD, pll_cfg->od);
411 reg |= FIELD_PREP(K210_PLL_BWADJ, pll_cfg->bwadj);
412 reg |= K210_PLL_PWRD;
413 writel(reg, pll->reg);
416 * Reset the PLL: ensure reset is low before asserting it.
417 * The magic NOPs come from the Kendryte reference SDK.
419 reg &= ~K210_PLL_RESET;
420 writel(reg, pll->reg);
421 reg |= K210_PLL_RESET;
422 writel(reg, pll->reg);
425 reg &= ~K210_PLL_RESET;
426 writel(reg, pll->reg);
428 k210_pll_wait_for_lock(pll);
430 reg &= ~K210_PLL_BYPASS;
432 writel(reg, pll->reg);
434 if (pll->id == K210_PLL0)
435 k210_aclk_set_selector(regs, 1);
438 static int k210_pll_enable(struct clk_hw *hw)
440 struct k210_pll *pll = to_k210_pll(hw);
441 struct k210_sysclk *ksc = pll->ksc;
444 spin_lock_irqsave(&ksc->clk_lock, flags);
446 k210_pll_enable_hw(ksc->regs, pll);
448 spin_unlock_irqrestore(&ksc->clk_lock, flags);
453 static void k210_pll_disable(struct clk_hw *hw)
455 struct k210_pll *pll = to_k210_pll(hw);
456 struct k210_sysclk *ksc = pll->ksc;
461 * Bypassing before powering off is important so child clocks do not
462 * stop working. This is especially important for pll0, the indirect
463 * parent of the cpu clock.
465 spin_lock_irqsave(&ksc->clk_lock, flags);
466 reg = readl(pll->reg);
467 reg |= K210_PLL_BYPASS;
468 writel(reg, pll->reg);
470 reg &= ~K210_PLL_PWRD;
472 writel(reg, pll->reg);
473 spin_unlock_irqrestore(&ksc->clk_lock, flags);
476 static int k210_pll_is_enabled(struct clk_hw *hw)
478 return k210_pll_hw_is_enabled(to_k210_pll(hw));
481 static unsigned long k210_pll_get_rate(struct clk_hw *hw,
482 unsigned long parent_rate)
484 struct k210_pll *pll = to_k210_pll(hw);
485 u32 reg = readl(pll->reg);
488 if (reg & K210_PLL_BYPASS)
491 if (!(reg & K210_PLL_PWRD))
494 r = FIELD_GET(K210_PLL_CLKR, reg) + 1;
495 f = FIELD_GET(K210_PLL_CLKF, reg) + 1;
496 od = FIELD_GET(K210_PLL_CLKOD, reg) + 1;
498 return (u64)parent_rate * f / (r * od);
501 static const struct clk_ops k210_pll_ops = {
502 .enable = k210_pll_enable,
503 .disable = k210_pll_disable,
504 .is_enabled = k210_pll_is_enabled,
505 .recalc_rate = k210_pll_get_rate,
508 static int k210_pll2_set_parent(struct clk_hw *hw, u8 index)
510 struct k210_pll *pll = to_k210_pll(hw);
511 struct k210_sysclk *ksc = pll->ksc;
515 spin_lock_irqsave(&ksc->clk_lock, flags);
517 reg = readl(pll->reg);
518 reg &= ~K210_PLL_SEL;
519 reg |= FIELD_PREP(K210_PLL_SEL, index);
520 writel(reg, pll->reg);
522 spin_unlock_irqrestore(&ksc->clk_lock, flags);
527 static u8 k210_pll2_get_parent(struct clk_hw *hw)
529 struct k210_pll *pll = to_k210_pll(hw);
530 u32 reg = readl(pll->reg);
532 return FIELD_GET(K210_PLL_SEL, reg);
535 static const struct clk_ops k210_pll2_ops = {
536 .enable = k210_pll_enable,
537 .disable = k210_pll_disable,
538 .is_enabled = k210_pll_is_enabled,
539 .recalc_rate = k210_pll_get_rate,
540 .set_parent = k210_pll2_set_parent,
541 .get_parent = k210_pll2_get_parent,
544 static int __init k210_register_pll(struct device_node *np,
545 struct k210_sysclk *ksc,
546 enum k210_pll_id pllid, const char *name,
547 int num_parents, const struct clk_ops *ops)
549 struct k210_pll *pll = &ksc->plls[pllid];
550 struct clk_init_data init = {};
551 const struct clk_parent_data parent_data[] = {
552 { /* .index = 0 for in0 */ },
553 { .hw = &ksc->plls[K210_PLL0].hw },
554 { .hw = &ksc->plls[K210_PLL1].hw },
558 init.parent_data = parent_data;
559 init.num_parents = num_parents;
562 pll->hw.init = &init;
565 return of_clk_hw_register(np, &pll->hw);
568 static int __init k210_register_plls(struct device_node *np,
569 struct k210_sysclk *ksc)
573 for (i = 0; i < K210_PLL_NUM; i++)
574 k210_init_pll(ksc->regs, i, &ksc->plls[i]);
576 /* PLL0 and PLL1 only have IN0 as parent */
577 ret = k210_register_pll(np, ksc, K210_PLL0, "pll0", 1, &k210_pll_ops);
579 pr_err("%pOFP: register PLL0 failed\n", np);
582 ret = k210_register_pll(np, ksc, K210_PLL1, "pll1", 1, &k210_pll_ops);
584 pr_err("%pOFP: register PLL1 failed\n", np);
588 /* PLL2 has IN0, PLL0 and PLL1 as parents */
589 ret = k210_register_pll(np, ksc, K210_PLL2, "pll2", 3, &k210_pll2_ops);
591 pr_err("%pOFP: register PLL2 failed\n", np);
598 static int k210_aclk_set_parent(struct clk_hw *hw, u8 index)
600 struct k210_sysclk *ksc = to_k210_sysclk(hw);
603 spin_lock_irqsave(&ksc->clk_lock, flags);
605 k210_aclk_set_selector(ksc->regs, index);
607 spin_unlock_irqrestore(&ksc->clk_lock, flags);
612 static u8 k210_aclk_get_parent(struct clk_hw *hw)
614 struct k210_sysclk *ksc = to_k210_sysclk(hw);
617 sel = readl(ksc->regs + K210_SYSCTL_SEL0) & K210_ACLK_SEL;
622 static unsigned long k210_aclk_get_rate(struct clk_hw *hw,
623 unsigned long parent_rate)
625 struct k210_sysclk *ksc = to_k210_sysclk(hw);
626 u32 reg = readl(ksc->regs + K210_SYSCTL_SEL0);
632 shift = FIELD_GET(K210_ACLK_DIV, reg);
634 return parent_rate / (2UL << shift);
637 static const struct clk_ops k210_aclk_ops = {
638 .set_parent = k210_aclk_set_parent,
639 .get_parent = k210_aclk_get_parent,
640 .recalc_rate = k210_aclk_get_rate,
644 * ACLK has IN0 and PLL0 as parents.
646 static int __init k210_register_aclk(struct device_node *np,
647 struct k210_sysclk *ksc)
649 struct clk_init_data init = {};
650 const struct clk_parent_data parent_data[] = {
651 { /* .index = 0 for in0 */ },
652 { .hw = &ksc->plls[K210_PLL0].hw },
657 init.parent_data = parent_data;
658 init.num_parents = 2;
659 init.ops = &k210_aclk_ops;
660 ksc->aclk.init = &init;
662 ret = of_clk_hw_register(np, &ksc->aclk);
664 pr_err("%pOFP: register aclk failed\n", np);
671 #define to_k210_clk(_hw) container_of(_hw, struct k210_clk, hw)
673 static int k210_clk_enable(struct clk_hw *hw)
675 struct k210_clk *kclk = to_k210_clk(hw);
676 struct k210_sysclk *ksc = kclk->ksc;
677 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
684 spin_lock_irqsave(&ksc->clk_lock, flags);
685 reg = readl(ksc->regs + cfg->gate_reg);
686 reg |= BIT(cfg->gate_bit);
687 writel(reg, ksc->regs + cfg->gate_reg);
688 spin_unlock_irqrestore(&ksc->clk_lock, flags);
693 static void k210_clk_disable(struct clk_hw *hw)
695 struct k210_clk *kclk = to_k210_clk(hw);
696 struct k210_sysclk *ksc = kclk->ksc;
697 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
704 spin_lock_irqsave(&ksc->clk_lock, flags);
705 reg = readl(ksc->regs + cfg->gate_reg);
706 reg &= ~BIT(cfg->gate_bit);
707 writel(reg, ksc->regs + cfg->gate_reg);
708 spin_unlock_irqrestore(&ksc->clk_lock, flags);
711 static int k210_clk_set_parent(struct clk_hw *hw, u8 index)
713 struct k210_clk *kclk = to_k210_clk(hw);
714 struct k210_sysclk *ksc = kclk->ksc;
715 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
719 spin_lock_irqsave(&ksc->clk_lock, flags);
720 reg = readl(ksc->regs + cfg->mux_reg);
722 reg |= BIT(cfg->mux_bit);
724 reg &= ~BIT(cfg->mux_bit);
725 writel(reg, ksc->regs + cfg->mux_reg);
726 spin_unlock_irqrestore(&ksc->clk_lock, flags);
731 static u8 k210_clk_get_parent(struct clk_hw *hw)
733 struct k210_clk *kclk = to_k210_clk(hw);
734 struct k210_sysclk *ksc = kclk->ksc;
735 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
739 spin_lock_irqsave(&ksc->clk_lock, flags);
740 reg = readl(ksc->regs + cfg->mux_reg);
741 idx = (reg & BIT(cfg->mux_bit)) ? 1 : 0;
742 spin_unlock_irqrestore(&ksc->clk_lock, flags);
747 static unsigned long k210_clk_get_rate(struct clk_hw *hw,
748 unsigned long parent_rate)
750 struct k210_clk *kclk = to_k210_clk(hw);
751 struct k210_sysclk *ksc = kclk->ksc;
752 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
758 reg = readl(ksc->regs + cfg->div_reg);
759 div_val = (reg >> cfg->div_shift) & GENMASK(cfg->div_width - 1, 0);
761 switch (cfg->div_type) {
762 case K210_DIV_ONE_BASED:
763 return parent_rate / (div_val + 1);
764 case K210_DIV_DOUBLE_ONE_BASED:
765 return parent_rate / ((div_val + 1) * 2);
766 case K210_DIV_POWER_OF_TWO:
767 return parent_rate / (2UL << div_val);
774 static const struct clk_ops k210_clk_mux_ops = {
775 .enable = k210_clk_enable,
776 .disable = k210_clk_disable,
777 .set_parent = k210_clk_set_parent,
778 .get_parent = k210_clk_get_parent,
779 .recalc_rate = k210_clk_get_rate,
782 static const struct clk_ops k210_clk_ops = {
783 .enable = k210_clk_enable,
784 .disable = k210_clk_disable,
785 .recalc_rate = k210_clk_get_rate,
788 static void __init k210_register_clk(struct device_node *np,
789 struct k210_sysclk *ksc, int id,
790 const struct clk_parent_data *parent_data,
791 int num_parents, unsigned long flags)
793 struct k210_clk *kclk = &ksc->clks[id];
794 struct clk_init_data init = {};
797 init.name = k210_clk_cfgs[id].name;
799 init.parent_data = parent_data;
800 init.num_parents = num_parents;
802 init.ops = &k210_clk_mux_ops;
804 init.ops = &k210_clk_ops;
808 kclk->hw.init = &init;
810 ret = of_clk_hw_register(np, &kclk->hw);
812 pr_err("%pOFP: register clock %s failed\n",
813 np, k210_clk_cfgs[id].name);
819 * All muxed clocks have IN0 and PLL0 as parents.
821 static inline void __init k210_register_mux_clk(struct device_node *np,
822 struct k210_sysclk *ksc, int id)
824 const struct clk_parent_data parent_data[2] = {
825 { /* .index = 0 for in0 */ },
826 { .hw = &ksc->plls[K210_PLL0].hw }
829 k210_register_clk(np, ksc, id, parent_data, 2, 0);
832 static inline void __init k210_register_in0_child(struct device_node *np,
833 struct k210_sysclk *ksc, int id)
835 const struct clk_parent_data parent_data = {
836 /* .index = 0 for in0 */
839 k210_register_clk(np, ksc, id, &parent_data, 1, 0);
842 static inline void __init k210_register_pll_child(struct device_node *np,
843 struct k210_sysclk *ksc, int id,
844 enum k210_pll_id pllid,
847 const struct clk_parent_data parent_data = {
848 .hw = &ksc->plls[pllid].hw,
851 k210_register_clk(np, ksc, id, &parent_data, 1, flags);
854 static inline void __init k210_register_aclk_child(struct device_node *np,
855 struct k210_sysclk *ksc, int id,
858 const struct clk_parent_data parent_data = {
862 k210_register_clk(np, ksc, id, &parent_data, 1, flags);
865 static inline void __init k210_register_clk_child(struct device_node *np,
866 struct k210_sysclk *ksc, int id,
869 const struct clk_parent_data parent_data = {
870 .hw = &ksc->clks[parent_id].hw,
873 k210_register_clk(np, ksc, id, &parent_data, 1, 0);
876 static struct clk_hw *k210_clk_hw_onecell_get(struct of_phandle_args *clkspec,
879 struct k210_sysclk *ksc = data;
880 unsigned int idx = clkspec->args[0];
882 if (idx >= K210_NUM_CLKS)
883 return ERR_PTR(-EINVAL);
885 return &ksc->clks[idx].hw;
888 static void __init k210_clk_init(struct device_node *np)
890 struct device_node *sysctl_np;
891 struct k210_sysclk *ksc;
894 ksc = kzalloc(sizeof(*ksc), GFP_KERNEL);
898 spin_lock_init(&ksc->clk_lock);
899 sysctl_np = of_get_parent(np);
900 ksc->regs = of_iomap(sysctl_np, 0);
901 of_node_put(sysctl_np);
903 pr_err("%pOFP: failed to map registers\n", np);
907 ret = k210_register_plls(np, ksc);
911 ret = k210_register_aclk(np, ksc);
916 * Critical clocks: there are no consumers of the SRAM clocks,
917 * including the AI clock for the third SRAM bank. The CPU clock
918 * is only referenced by the uarths serial device and so would be
919 * disabled if the serial console is disabled to switch to another
920 * console. Mark all these clocks as critical so that they are never
921 * disabled by the core clock management.
923 k210_register_aclk_child(np, ksc, K210_CLK_CPU, CLK_IS_CRITICAL);
924 k210_register_aclk_child(np, ksc, K210_CLK_SRAM0, CLK_IS_CRITICAL);
925 k210_register_aclk_child(np, ksc, K210_CLK_SRAM1, CLK_IS_CRITICAL);
926 k210_register_pll_child(np, ksc, K210_CLK_AI, K210_PLL1,
929 /* Clocks with aclk as source */
930 k210_register_aclk_child(np, ksc, K210_CLK_DMA, 0);
931 k210_register_aclk_child(np, ksc, K210_CLK_FFT, 0);
932 k210_register_aclk_child(np, ksc, K210_CLK_ROM, 0);
933 k210_register_aclk_child(np, ksc, K210_CLK_DVP, 0);
934 k210_register_aclk_child(np, ksc, K210_CLK_APB0, 0);
935 k210_register_aclk_child(np, ksc, K210_CLK_APB1, 0);
936 k210_register_aclk_child(np, ksc, K210_CLK_APB2, 0);
938 /* Clocks with PLL0 as source */
939 k210_register_pll_child(np, ksc, K210_CLK_SPI0, K210_PLL0, 0);
940 k210_register_pll_child(np, ksc, K210_CLK_SPI1, K210_PLL0, 0);
941 k210_register_pll_child(np, ksc, K210_CLK_SPI2, K210_PLL0, 0);
942 k210_register_pll_child(np, ksc, K210_CLK_I2C0, K210_PLL0, 0);
943 k210_register_pll_child(np, ksc, K210_CLK_I2C1, K210_PLL0, 0);
944 k210_register_pll_child(np, ksc, K210_CLK_I2C2, K210_PLL0, 0);
946 /* Clocks with PLL2 as source */
947 k210_register_pll_child(np, ksc, K210_CLK_I2S0, K210_PLL2, 0);
948 k210_register_pll_child(np, ksc, K210_CLK_I2S1, K210_PLL2, 0);
949 k210_register_pll_child(np, ksc, K210_CLK_I2S2, K210_PLL2, 0);
950 k210_register_pll_child(np, ksc, K210_CLK_I2S0_M, K210_PLL2, 0);
951 k210_register_pll_child(np, ksc, K210_CLK_I2S1_M, K210_PLL2, 0);
952 k210_register_pll_child(np, ksc, K210_CLK_I2S2_M, K210_PLL2, 0);
954 /* Clocks with IN0 as source */
955 k210_register_in0_child(np, ksc, K210_CLK_WDT0);
956 k210_register_in0_child(np, ksc, K210_CLK_WDT1);
957 k210_register_in0_child(np, ksc, K210_CLK_RTC);
959 /* Clocks with APB0 as source */
960 k210_register_clk_child(np, ksc, K210_CLK_GPIO, K210_CLK_APB0);
961 k210_register_clk_child(np, ksc, K210_CLK_UART1, K210_CLK_APB0);
962 k210_register_clk_child(np, ksc, K210_CLK_UART2, K210_CLK_APB0);
963 k210_register_clk_child(np, ksc, K210_CLK_UART3, K210_CLK_APB0);
964 k210_register_clk_child(np, ksc, K210_CLK_FPIOA, K210_CLK_APB0);
965 k210_register_clk_child(np, ksc, K210_CLK_SHA, K210_CLK_APB0);
967 /* Clocks with APB1 as source */
968 k210_register_clk_child(np, ksc, K210_CLK_AES, K210_CLK_APB1);
969 k210_register_clk_child(np, ksc, K210_CLK_OTP, K210_CLK_APB1);
971 /* Mux clocks with in0 or pll0 as source */
972 k210_register_mux_clk(np, ksc, K210_CLK_SPI3);
973 k210_register_mux_clk(np, ksc, K210_CLK_TIMER0);
974 k210_register_mux_clk(np, ksc, K210_CLK_TIMER1);
975 k210_register_mux_clk(np, ksc, K210_CLK_TIMER2);
977 /* Check for registration errors */
978 for (i = 0; i < K210_NUM_CLKS; i++) {
979 if (ksc->clks[i].id != i)
983 ret = of_clk_add_hw_provider(np, k210_clk_hw_onecell_get, ksc);
985 pr_err("%pOFP: add clock provider failed %d\n", np, ret);
989 pr_info("%pOFP: CPU running at %lu MHz\n",
990 np, clk_hw_get_rate(&ksc->clks[K210_CLK_CPU].hw) / 1000000);
993 CLK_OF_DECLARE(k210_clk, "canaan,k210-clk", k210_clk_init);
996 * Enable PLL1 to be able to use the AI SRAM.
998 void __init k210_clk_early_init(void __iomem *regs)
1000 struct k210_pll pll1;
1002 /* Make sure ACLK selector is set to PLL0 */
1003 k210_aclk_set_selector(regs, 1);
1005 /* Startup PLL1 to enable the aisram bank for general memory use */
1006 k210_init_pll(regs, K210_PLL1, &pll1);
1007 k210_pll_enable_hw(regs, &pll1);