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 div_u64((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 .determine_rate = clk_hw_determine_rate_no_reparent,
541 .set_parent = k210_pll2_set_parent,
542 .get_parent = k210_pll2_get_parent,
545 static int __init k210_register_pll(struct device_node *np,
546 struct k210_sysclk *ksc,
547 enum k210_pll_id pllid, const char *name,
548 int num_parents, const struct clk_ops *ops)
550 struct k210_pll *pll = &ksc->plls[pllid];
551 struct clk_init_data init = {};
552 const struct clk_parent_data parent_data[] = {
553 { /* .index = 0 for in0 */ },
554 { .hw = &ksc->plls[K210_PLL0].hw },
555 { .hw = &ksc->plls[K210_PLL1].hw },
559 init.parent_data = parent_data;
560 init.num_parents = num_parents;
563 pll->hw.init = &init;
566 return of_clk_hw_register(np, &pll->hw);
569 static int __init k210_register_plls(struct device_node *np,
570 struct k210_sysclk *ksc)
574 for (i = 0; i < K210_PLL_NUM; i++)
575 k210_init_pll(ksc->regs, i, &ksc->plls[i]);
577 /* PLL0 and PLL1 only have IN0 as parent */
578 ret = k210_register_pll(np, ksc, K210_PLL0, "pll0", 1, &k210_pll_ops);
580 pr_err("%pOFP: register PLL0 failed\n", np);
583 ret = k210_register_pll(np, ksc, K210_PLL1, "pll1", 1, &k210_pll_ops);
585 pr_err("%pOFP: register PLL1 failed\n", np);
589 /* PLL2 has IN0, PLL0 and PLL1 as parents */
590 ret = k210_register_pll(np, ksc, K210_PLL2, "pll2", 3, &k210_pll2_ops);
592 pr_err("%pOFP: register PLL2 failed\n", np);
599 static int k210_aclk_set_parent(struct clk_hw *hw, u8 index)
601 struct k210_sysclk *ksc = to_k210_sysclk(hw);
604 spin_lock_irqsave(&ksc->clk_lock, flags);
606 k210_aclk_set_selector(ksc->regs, index);
608 spin_unlock_irqrestore(&ksc->clk_lock, flags);
613 static u8 k210_aclk_get_parent(struct clk_hw *hw)
615 struct k210_sysclk *ksc = to_k210_sysclk(hw);
618 sel = readl(ksc->regs + K210_SYSCTL_SEL0) & K210_ACLK_SEL;
623 static unsigned long k210_aclk_get_rate(struct clk_hw *hw,
624 unsigned long parent_rate)
626 struct k210_sysclk *ksc = to_k210_sysclk(hw);
627 u32 reg = readl(ksc->regs + K210_SYSCTL_SEL0);
633 shift = FIELD_GET(K210_ACLK_DIV, reg);
635 return parent_rate / (2UL << shift);
638 static const struct clk_ops k210_aclk_ops = {
639 .determine_rate = clk_hw_determine_rate_no_reparent,
640 .set_parent = k210_aclk_set_parent,
641 .get_parent = k210_aclk_get_parent,
642 .recalc_rate = k210_aclk_get_rate,
646 * ACLK has IN0 and PLL0 as parents.
648 static int __init k210_register_aclk(struct device_node *np,
649 struct k210_sysclk *ksc)
651 struct clk_init_data init = {};
652 const struct clk_parent_data parent_data[] = {
653 { /* .index = 0 for in0 */ },
654 { .hw = &ksc->plls[K210_PLL0].hw },
659 init.parent_data = parent_data;
660 init.num_parents = 2;
661 init.ops = &k210_aclk_ops;
662 ksc->aclk.init = &init;
664 ret = of_clk_hw_register(np, &ksc->aclk);
666 pr_err("%pOFP: register aclk failed\n", np);
673 #define to_k210_clk(_hw) container_of(_hw, struct k210_clk, hw)
675 static int k210_clk_enable(struct clk_hw *hw)
677 struct k210_clk *kclk = to_k210_clk(hw);
678 struct k210_sysclk *ksc = kclk->ksc;
679 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
686 spin_lock_irqsave(&ksc->clk_lock, flags);
687 reg = readl(ksc->regs + cfg->gate_reg);
688 reg |= BIT(cfg->gate_bit);
689 writel(reg, ksc->regs + cfg->gate_reg);
690 spin_unlock_irqrestore(&ksc->clk_lock, flags);
695 static void k210_clk_disable(struct clk_hw *hw)
697 struct k210_clk *kclk = to_k210_clk(hw);
698 struct k210_sysclk *ksc = kclk->ksc;
699 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
706 spin_lock_irqsave(&ksc->clk_lock, flags);
707 reg = readl(ksc->regs + cfg->gate_reg);
708 reg &= ~BIT(cfg->gate_bit);
709 writel(reg, ksc->regs + cfg->gate_reg);
710 spin_unlock_irqrestore(&ksc->clk_lock, flags);
713 static int k210_clk_set_parent(struct clk_hw *hw, u8 index)
715 struct k210_clk *kclk = to_k210_clk(hw);
716 struct k210_sysclk *ksc = kclk->ksc;
717 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
721 spin_lock_irqsave(&ksc->clk_lock, flags);
722 reg = readl(ksc->regs + cfg->mux_reg);
724 reg |= BIT(cfg->mux_bit);
726 reg &= ~BIT(cfg->mux_bit);
727 writel(reg, ksc->regs + cfg->mux_reg);
728 spin_unlock_irqrestore(&ksc->clk_lock, flags);
733 static u8 k210_clk_get_parent(struct clk_hw *hw)
735 struct k210_clk *kclk = to_k210_clk(hw);
736 struct k210_sysclk *ksc = kclk->ksc;
737 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
741 spin_lock_irqsave(&ksc->clk_lock, flags);
742 reg = readl(ksc->regs + cfg->mux_reg);
743 idx = (reg & BIT(cfg->mux_bit)) ? 1 : 0;
744 spin_unlock_irqrestore(&ksc->clk_lock, flags);
749 static unsigned long k210_clk_get_rate(struct clk_hw *hw,
750 unsigned long parent_rate)
752 struct k210_clk *kclk = to_k210_clk(hw);
753 struct k210_sysclk *ksc = kclk->ksc;
754 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
760 reg = readl(ksc->regs + cfg->div_reg);
761 div_val = (reg >> cfg->div_shift) & GENMASK(cfg->div_width - 1, 0);
763 switch (cfg->div_type) {
764 case K210_DIV_ONE_BASED:
765 return parent_rate / (div_val + 1);
766 case K210_DIV_DOUBLE_ONE_BASED:
767 return parent_rate / ((div_val + 1) * 2);
768 case K210_DIV_POWER_OF_TWO:
769 return parent_rate / (2UL << div_val);
776 static const struct clk_ops k210_clk_mux_ops = {
777 .enable = k210_clk_enable,
778 .disable = k210_clk_disable,
779 .determine_rate = clk_hw_determine_rate_no_reparent,
780 .set_parent = k210_clk_set_parent,
781 .get_parent = k210_clk_get_parent,
782 .recalc_rate = k210_clk_get_rate,
785 static const struct clk_ops k210_clk_ops = {
786 .enable = k210_clk_enable,
787 .disable = k210_clk_disable,
788 .recalc_rate = k210_clk_get_rate,
791 static void __init k210_register_clk(struct device_node *np,
792 struct k210_sysclk *ksc, int id,
793 const struct clk_parent_data *parent_data,
794 int num_parents, unsigned long flags)
796 struct k210_clk *kclk = &ksc->clks[id];
797 struct clk_init_data init = {};
800 init.name = k210_clk_cfgs[id].name;
802 init.parent_data = parent_data;
803 init.num_parents = num_parents;
805 init.ops = &k210_clk_mux_ops;
807 init.ops = &k210_clk_ops;
811 kclk->hw.init = &init;
813 ret = of_clk_hw_register(np, &kclk->hw);
815 pr_err("%pOFP: register clock %s failed\n",
816 np, k210_clk_cfgs[id].name);
822 * All muxed clocks have IN0 and PLL0 as parents.
824 static inline void __init k210_register_mux_clk(struct device_node *np,
825 struct k210_sysclk *ksc, int id)
827 const struct clk_parent_data parent_data[2] = {
828 { /* .index = 0 for in0 */ },
829 { .hw = &ksc->plls[K210_PLL0].hw }
832 k210_register_clk(np, ksc, id, parent_data, 2, 0);
835 static inline void __init k210_register_in0_child(struct device_node *np,
836 struct k210_sysclk *ksc, int id)
838 const struct clk_parent_data parent_data = {
839 /* .index = 0 for in0 */
842 k210_register_clk(np, ksc, id, &parent_data, 1, 0);
845 static inline void __init k210_register_pll_child(struct device_node *np,
846 struct k210_sysclk *ksc, int id,
847 enum k210_pll_id pllid,
850 const struct clk_parent_data parent_data = {
851 .hw = &ksc->plls[pllid].hw,
854 k210_register_clk(np, ksc, id, &parent_data, 1, flags);
857 static inline void __init k210_register_aclk_child(struct device_node *np,
858 struct k210_sysclk *ksc, int id,
861 const struct clk_parent_data parent_data = {
865 k210_register_clk(np, ksc, id, &parent_data, 1, flags);
868 static inline void __init k210_register_clk_child(struct device_node *np,
869 struct k210_sysclk *ksc, int id,
872 const struct clk_parent_data parent_data = {
873 .hw = &ksc->clks[parent_id].hw,
876 k210_register_clk(np, ksc, id, &parent_data, 1, 0);
879 static struct clk_hw *k210_clk_hw_onecell_get(struct of_phandle_args *clkspec,
882 struct k210_sysclk *ksc = data;
883 unsigned int idx = clkspec->args[0];
885 if (idx >= K210_NUM_CLKS)
886 return ERR_PTR(-EINVAL);
888 return &ksc->clks[idx].hw;
891 static void __init k210_clk_init(struct device_node *np)
893 struct device_node *sysctl_np;
894 struct k210_sysclk *ksc;
897 ksc = kzalloc(sizeof(*ksc), GFP_KERNEL);
901 spin_lock_init(&ksc->clk_lock);
902 sysctl_np = of_get_parent(np);
903 ksc->regs = of_iomap(sysctl_np, 0);
904 of_node_put(sysctl_np);
906 pr_err("%pOFP: failed to map registers\n", np);
910 ret = k210_register_plls(np, ksc);
914 ret = k210_register_aclk(np, ksc);
919 * Critical clocks: there are no consumers of the SRAM clocks,
920 * including the AI clock for the third SRAM bank. The CPU clock
921 * is only referenced by the uarths serial device and so would be
922 * disabled if the serial console is disabled to switch to another
923 * console. Mark all these clocks as critical so that they are never
924 * disabled by the core clock management.
926 k210_register_aclk_child(np, ksc, K210_CLK_CPU, CLK_IS_CRITICAL);
927 k210_register_aclk_child(np, ksc, K210_CLK_SRAM0, CLK_IS_CRITICAL);
928 k210_register_aclk_child(np, ksc, K210_CLK_SRAM1, CLK_IS_CRITICAL);
929 k210_register_pll_child(np, ksc, K210_CLK_AI, K210_PLL1,
932 /* Clocks with aclk as source */
933 k210_register_aclk_child(np, ksc, K210_CLK_DMA, 0);
934 k210_register_aclk_child(np, ksc, K210_CLK_FFT, 0);
935 k210_register_aclk_child(np, ksc, K210_CLK_ROM, 0);
936 k210_register_aclk_child(np, ksc, K210_CLK_DVP, 0);
937 k210_register_aclk_child(np, ksc, K210_CLK_APB0, 0);
938 k210_register_aclk_child(np, ksc, K210_CLK_APB1, 0);
939 k210_register_aclk_child(np, ksc, K210_CLK_APB2, 0);
941 /* Clocks with PLL0 as source */
942 k210_register_pll_child(np, ksc, K210_CLK_SPI0, K210_PLL0, 0);
943 k210_register_pll_child(np, ksc, K210_CLK_SPI1, K210_PLL0, 0);
944 k210_register_pll_child(np, ksc, K210_CLK_SPI2, K210_PLL0, 0);
945 k210_register_pll_child(np, ksc, K210_CLK_I2C0, K210_PLL0, 0);
946 k210_register_pll_child(np, ksc, K210_CLK_I2C1, K210_PLL0, 0);
947 k210_register_pll_child(np, ksc, K210_CLK_I2C2, K210_PLL0, 0);
949 /* Clocks with PLL2 as source */
950 k210_register_pll_child(np, ksc, K210_CLK_I2S0, K210_PLL2, 0);
951 k210_register_pll_child(np, ksc, K210_CLK_I2S1, K210_PLL2, 0);
952 k210_register_pll_child(np, ksc, K210_CLK_I2S2, K210_PLL2, 0);
953 k210_register_pll_child(np, ksc, K210_CLK_I2S0_M, K210_PLL2, 0);
954 k210_register_pll_child(np, ksc, K210_CLK_I2S1_M, K210_PLL2, 0);
955 k210_register_pll_child(np, ksc, K210_CLK_I2S2_M, K210_PLL2, 0);
957 /* Clocks with IN0 as source */
958 k210_register_in0_child(np, ksc, K210_CLK_WDT0);
959 k210_register_in0_child(np, ksc, K210_CLK_WDT1);
960 k210_register_in0_child(np, ksc, K210_CLK_RTC);
962 /* Clocks with APB0 as source */
963 k210_register_clk_child(np, ksc, K210_CLK_GPIO, K210_CLK_APB0);
964 k210_register_clk_child(np, ksc, K210_CLK_UART1, K210_CLK_APB0);
965 k210_register_clk_child(np, ksc, K210_CLK_UART2, K210_CLK_APB0);
966 k210_register_clk_child(np, ksc, K210_CLK_UART3, K210_CLK_APB0);
967 k210_register_clk_child(np, ksc, K210_CLK_FPIOA, K210_CLK_APB0);
968 k210_register_clk_child(np, ksc, K210_CLK_SHA, K210_CLK_APB0);
970 /* Clocks with APB1 as source */
971 k210_register_clk_child(np, ksc, K210_CLK_AES, K210_CLK_APB1);
972 k210_register_clk_child(np, ksc, K210_CLK_OTP, K210_CLK_APB1);
974 /* Mux clocks with in0 or pll0 as source */
975 k210_register_mux_clk(np, ksc, K210_CLK_SPI3);
976 k210_register_mux_clk(np, ksc, K210_CLK_TIMER0);
977 k210_register_mux_clk(np, ksc, K210_CLK_TIMER1);
978 k210_register_mux_clk(np, ksc, K210_CLK_TIMER2);
980 /* Check for registration errors */
981 for (i = 0; i < K210_NUM_CLKS; i++) {
982 if (ksc->clks[i].id != i)
986 ret = of_clk_add_hw_provider(np, k210_clk_hw_onecell_get, ksc);
988 pr_err("%pOFP: add clock provider failed %d\n", np, ret);
992 pr_info("%pOFP: CPU running at %lu MHz\n",
993 np, clk_hw_get_rate(&ksc->clks[K210_CLK_CPU].hw) / 1000000);
996 CLK_OF_DECLARE(k210_clk, "canaan,k210-clk", k210_clk_init);
999 * Enable PLL1 to be able to use the AI SRAM.
1001 void __init k210_clk_early_init(void __iomem *regs)
1003 struct k210_pll pll1;
1005 /* Make sure ACLK selector is set to PLL0 */
1006 k210_aclk_set_selector(regs, 1);
1008 /* Startup PLL1 to enable the aisram bank for general memory use */
1009 k210_init_pll(regs, K210_PLL1, &pll1);
1010 k210_pll_enable_hw(regs, &pll1);