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_address.h>
15 #include <linux/clk-provider.h>
16 #include <linux/bitfield.h>
17 #include <linux/delay.h>
18 #include <soc/canaan/k210-sysctl.h>
20 #include <dt-bindings/clock/k210-clk.h>
26 struct k210_sysclk *ksc;
42 enum k210_clk_div_type {
45 K210_DIV_DOUBLE_ONE_BASED,
46 K210_DIV_POWER_OF_TWO,
49 #define K210_GATE(_reg, _bit) \
53 #define K210_DIV(_reg, _shift, _width, _type) \
55 .div_shift = (_shift), \
56 .div_width = (_width), \
59 #define K210_MUX(_reg, _bit) \
63 static struct k210_clk_cfg k210_clk_cfgs[K210_NUM_CLKS] = {
64 /* Gated clocks, no mux, no divider */
67 K210_GATE(K210_SYSCTL_EN_CENT, 0)
71 K210_GATE(K210_SYSCTL_EN_PERI, 1)
75 K210_GATE(K210_SYSCTL_EN_PERI, 4)
79 K210_GATE(K210_SYSCTL_EN_PERI, 5)
83 K210_GATE(K210_SYSCTL_EN_PERI, 16)
87 K210_GATE(K210_SYSCTL_EN_PERI, 17)
91 K210_GATE(K210_SYSCTL_EN_PERI, 18)
95 K210_GATE(K210_SYSCTL_EN_PERI, 20)
99 K210_GATE(K210_SYSCTL_EN_PERI, 26)
103 K210_GATE(K210_SYSCTL_EN_PERI, 19)
107 K210_GATE(K210_SYSCTL_EN_PERI, 27)
111 K210_GATE(K210_SYSCTL_EN_PERI, 29)
114 /* Gated divider clocks */
117 K210_GATE(K210_SYSCTL_EN_CENT, 1),
118 K210_DIV(K210_SYSCTL_THR0, 0, 4, K210_DIV_ONE_BASED)
122 K210_GATE(K210_SYSCTL_EN_CENT, 2),
123 K210_DIV(K210_SYSCTL_THR0, 4, 4, K210_DIV_ONE_BASED)
127 K210_GATE(K210_SYSCTL_EN_PERI, 0),
128 K210_DIV(K210_SYSCTL_THR0, 16, 4, K210_DIV_ONE_BASED)
132 K210_GATE(K210_SYSCTL_EN_PERI, 3),
133 K210_DIV(K210_SYSCTL_THR0, 12, 4, K210_DIV_ONE_BASED)
137 K210_GATE(K210_SYSCTL_EN_CENT, 3),
138 K210_DIV(K210_SYSCTL_SEL0, 3, 3, K210_DIV_ONE_BASED)
142 K210_GATE(K210_SYSCTL_EN_CENT, 4),
143 K210_DIV(K210_SYSCTL_SEL0, 6, 3, K210_DIV_ONE_BASED)
147 K210_GATE(K210_SYSCTL_EN_CENT, 5),
148 K210_DIV(K210_SYSCTL_SEL0, 9, 3, K210_DIV_ONE_BASED)
152 K210_GATE(K210_SYSCTL_EN_PERI, 2),
153 K210_DIV(K210_SYSCTL_THR0, 8, 4, K210_DIV_ONE_BASED)
157 K210_GATE(K210_SYSCTL_EN_PERI, 6),
158 K210_DIV(K210_SYSCTL_THR1, 0, 8, K210_DIV_DOUBLE_ONE_BASED)
162 K210_GATE(K210_SYSCTL_EN_PERI, 7),
163 K210_DIV(K210_SYSCTL_THR1, 8, 8, K210_DIV_DOUBLE_ONE_BASED)
167 K210_GATE(K210_SYSCTL_EN_PERI, 8),
168 K210_DIV(K210_SYSCTL_THR1, 16, 8, K210_DIV_DOUBLE_ONE_BASED)
172 K210_GATE(K210_SYSCTL_EN_PERI, 13),
173 K210_DIV(K210_SYSCTL_THR5, 8, 8, K210_DIV_DOUBLE_ONE_BASED)
177 K210_GATE(K210_SYSCTL_EN_PERI, 14),
178 K210_DIV(K210_SYSCTL_THR5, 16, 8, K210_DIV_DOUBLE_ONE_BASED)
182 K210_GATE(K210_SYSCTL_EN_PERI, 15),
183 K210_DIV(K210_SYSCTL_THR5, 24, 8, K210_DIV_DOUBLE_ONE_BASED)
187 K210_GATE(K210_SYSCTL_EN_PERI, 24),
188 K210_DIV(K210_SYSCTL_THR6, 0, 8, K210_DIV_DOUBLE_ONE_BASED)
192 K210_GATE(K210_SYSCTL_EN_PERI, 25),
193 K210_DIV(K210_SYSCTL_THR6, 8, 8, K210_DIV_DOUBLE_ONE_BASED)
197 K210_GATE(K210_SYSCTL_EN_PERI, 10),
198 K210_DIV(K210_SYSCTL_THR3, 0, 16, K210_DIV_DOUBLE_ONE_BASED)
202 K210_GATE(K210_SYSCTL_EN_PERI, 11),
203 K210_DIV(K210_SYSCTL_THR3, 16, 16, K210_DIV_DOUBLE_ONE_BASED)
207 K210_GATE(K210_SYSCTL_EN_PERI, 12),
208 K210_DIV(K210_SYSCTL_THR4, 0, 16, K210_DIV_DOUBLE_ONE_BASED)
211 /* Divider clocks, no gate, no mux */
212 [K210_CLK_I2S0_M] = {
214 K210_DIV(K210_SYSCTL_THR4, 16, 8, K210_DIV_DOUBLE_ONE_BASED)
216 [K210_CLK_I2S1_M] = {
218 K210_DIV(K210_SYSCTL_THR4, 24, 8, K210_DIV_DOUBLE_ONE_BASED)
220 [K210_CLK_I2S2_M] = {
222 K210_DIV(K210_SYSCTL_THR4, 0, 8, K210_DIV_DOUBLE_ONE_BASED)
225 /* Muxed gated divider clocks */
228 K210_GATE(K210_SYSCTL_EN_PERI, 9),
229 K210_DIV(K210_SYSCTL_THR1, 24, 8, K210_DIV_DOUBLE_ONE_BASED),
230 K210_MUX(K210_SYSCTL_SEL0, 12)
232 [K210_CLK_TIMER0] = {
234 K210_GATE(K210_SYSCTL_EN_PERI, 21),
235 K210_DIV(K210_SYSCTL_THR2, 0, 8, K210_DIV_DOUBLE_ONE_BASED),
236 K210_MUX(K210_SYSCTL_SEL0, 13)
238 [K210_CLK_TIMER1] = {
240 K210_GATE(K210_SYSCTL_EN_PERI, 22),
241 K210_DIV(K210_SYSCTL_THR2, 8, 8, K210_DIV_DOUBLE_ONE_BASED),
242 K210_MUX(K210_SYSCTL_SEL0, 14)
244 [K210_CLK_TIMER2] = {
246 K210_GATE(K210_SYSCTL_EN_PERI, 23),
247 K210_DIV(K210_SYSCTL_THR2, 16, 8, K210_DIV_DOUBLE_ONE_BASED),
248 K210_MUX(K210_SYSCTL_SEL0, 15)
253 * PLL control register bits.
255 #define K210_PLL_CLKR GENMASK(3, 0)
256 #define K210_PLL_CLKF GENMASK(9, 4)
257 #define K210_PLL_CLKOD GENMASK(13, 10)
258 #define K210_PLL_BWADJ GENMASK(19, 14)
259 #define K210_PLL_RESET (1 << 20)
260 #define K210_PLL_PWRD (1 << 21)
261 #define K210_PLL_INTFB (1 << 22)
262 #define K210_PLL_BYPASS (1 << 23)
263 #define K210_PLL_TEST (1 << 24)
264 #define K210_PLL_EN (1 << 25)
265 #define K210_PLL_SEL GENMASK(27, 26) /* PLL2 only */
268 * PLL lock register bits.
270 #define K210_PLL_LOCK 0
271 #define K210_PLL_CLEAR_SLIP 2
272 #define K210_PLL_TEST_OUT 3
275 * Clock selector register bits.
277 #define K210_ACLK_SEL BIT(0)
278 #define K210_ACLK_DIV GENMASK(2, 1)
284 K210_PLL0, K210_PLL1, K210_PLL2, K210_PLL_NUM
289 struct k210_sysclk *ksc;
297 #define to_k210_pll(_hw) container_of(_hw, struct k210_pll, hw)
300 * PLLs configuration: by default PLL0 runs at 780 MHz and PLL1 at 299 MHz.
301 * The first 2 SRAM banks depend on ACLK/CPU clock which is by default PLL0
302 * rate divided by 2. Set PLL1 to 390 MHz so that the third SRAM bank has the
303 * same clock as the first 2.
305 struct k210_pll_cfg {
315 static struct k210_pll_cfg k210_plls_cfg[] = {
316 { K210_SYSCTL_PLL0, 0, 2, 0, 59, 1, 59 }, /* 780 MHz */
317 { K210_SYSCTL_PLL1, 8, 1, 0, 59, 3, 59 }, /* 390 MHz */
318 { K210_SYSCTL_PLL2, 16, 1, 0, 22, 1, 22 }, /* 299 MHz */
322 * struct k210_sysclk - sysclk driver data
323 * @regs: system controller registers start address
324 * @clk_lock: clock setting spinlock
325 * @plls: SoC PLLs descriptors
327 * @clks: All other clocks
332 struct k210_pll plls[K210_PLL_NUM];
334 struct k210_clk clks[K210_NUM_CLKS];
337 #define to_k210_sysclk(_hw) container_of(_hw, struct k210_sysclk, aclk)
340 * Set ACLK parent selector: 0 for IN0, 1 for PLL0.
342 static void k210_aclk_set_selector(void __iomem *regs, u8 sel)
344 u32 reg = readl(regs + K210_SYSCTL_SEL0);
347 reg |= K210_ACLK_SEL;
349 reg &= K210_ACLK_SEL;
350 writel(reg, regs + K210_SYSCTL_SEL0);
353 static void k210_init_pll(void __iomem *regs, enum k210_pll_id pllid,
354 struct k210_pll *pll)
357 pll->reg = regs + k210_plls_cfg[pllid].reg;
358 pll->lock = regs + K210_SYSCTL_PLL_LOCK;
359 pll->lock_shift = k210_plls_cfg[pllid].lock_shift;
360 pll->lock_width = k210_plls_cfg[pllid].lock_width;
363 static void k210_pll_wait_for_lock(struct k210_pll *pll)
365 u32 reg, mask = GENMASK(pll->lock_shift + pll->lock_width - 1,
369 reg = readl(pll->lock);
370 if ((reg & mask) == mask)
373 reg |= BIT(pll->lock_shift + K210_PLL_CLEAR_SLIP);
374 writel(reg, pll->lock);
378 static bool k210_pll_hw_is_enabled(struct k210_pll *pll)
380 u32 reg = readl(pll->reg);
381 u32 mask = K210_PLL_PWRD | K210_PLL_EN;
383 if (reg & K210_PLL_RESET)
386 return (reg & mask) == mask;
389 static void k210_pll_enable_hw(void __iomem *regs, struct k210_pll *pll)
391 struct k210_pll_cfg *pll_cfg = &k210_plls_cfg[pll->id];
394 if (k210_pll_hw_is_enabled(pll))
398 * For PLL0, we need to re-parent ACLK to IN0 to keep the CPU cores and
401 if (pll->id == K210_PLL0)
402 k210_aclk_set_selector(regs, 0);
404 /* Set PLL factors */
405 reg = readl(pll->reg);
406 reg &= ~GENMASK(19, 0);
407 reg |= FIELD_PREP(K210_PLL_CLKR, pll_cfg->r);
408 reg |= FIELD_PREP(K210_PLL_CLKF, pll_cfg->f);
409 reg |= FIELD_PREP(K210_PLL_CLKOD, pll_cfg->od);
410 reg |= FIELD_PREP(K210_PLL_BWADJ, pll_cfg->bwadj);
411 reg |= K210_PLL_PWRD;
412 writel(reg, pll->reg);
415 * Reset the PLL: ensure reset is low before asserting it.
416 * The magic NOPs come from the Kendryte reference SDK.
418 reg &= ~K210_PLL_RESET;
419 writel(reg, pll->reg);
420 reg |= K210_PLL_RESET;
421 writel(reg, pll->reg);
424 reg &= ~K210_PLL_RESET;
425 writel(reg, pll->reg);
427 k210_pll_wait_for_lock(pll);
429 reg &= ~K210_PLL_BYPASS;
431 writel(reg, pll->reg);
433 if (pll->id == K210_PLL0)
434 k210_aclk_set_selector(regs, 1);
437 static int k210_pll_enable(struct clk_hw *hw)
439 struct k210_pll *pll = to_k210_pll(hw);
440 struct k210_sysclk *ksc = pll->ksc;
443 spin_lock_irqsave(&ksc->clk_lock, flags);
445 k210_pll_enable_hw(ksc->regs, pll);
447 spin_unlock_irqrestore(&ksc->clk_lock, flags);
452 static void k210_pll_disable(struct clk_hw *hw)
454 struct k210_pll *pll = to_k210_pll(hw);
455 struct k210_sysclk *ksc = pll->ksc;
460 * Bypassing before powering off is important so child clocks do not
461 * stop working. This is especially important for pll0, the indirect
462 * parent of the cpu clock.
464 spin_lock_irqsave(&ksc->clk_lock, flags);
465 reg = readl(pll->reg);
466 reg |= K210_PLL_BYPASS;
467 writel(reg, pll->reg);
469 reg &= ~K210_PLL_PWRD;
471 writel(reg, pll->reg);
472 spin_unlock_irqrestore(&ksc->clk_lock, flags);
475 static int k210_pll_is_enabled(struct clk_hw *hw)
477 return k210_pll_hw_is_enabled(to_k210_pll(hw));
480 static unsigned long k210_pll_get_rate(struct clk_hw *hw,
481 unsigned long parent_rate)
483 struct k210_pll *pll = to_k210_pll(hw);
484 u32 reg = readl(pll->reg);
487 if (reg & K210_PLL_BYPASS)
490 if (!(reg & K210_PLL_PWRD))
493 r = FIELD_GET(K210_PLL_CLKR, reg) + 1;
494 f = FIELD_GET(K210_PLL_CLKF, reg) + 1;
495 od = FIELD_GET(K210_PLL_CLKOD, reg) + 1;
497 return div_u64((u64)parent_rate * f, r * od);
500 static const struct clk_ops k210_pll_ops = {
501 .enable = k210_pll_enable,
502 .disable = k210_pll_disable,
503 .is_enabled = k210_pll_is_enabled,
504 .recalc_rate = k210_pll_get_rate,
507 static int k210_pll2_set_parent(struct clk_hw *hw, u8 index)
509 struct k210_pll *pll = to_k210_pll(hw);
510 struct k210_sysclk *ksc = pll->ksc;
514 spin_lock_irqsave(&ksc->clk_lock, flags);
516 reg = readl(pll->reg);
517 reg &= ~K210_PLL_SEL;
518 reg |= FIELD_PREP(K210_PLL_SEL, index);
519 writel(reg, pll->reg);
521 spin_unlock_irqrestore(&ksc->clk_lock, flags);
526 static u8 k210_pll2_get_parent(struct clk_hw *hw)
528 struct k210_pll *pll = to_k210_pll(hw);
529 u32 reg = readl(pll->reg);
531 return FIELD_GET(K210_PLL_SEL, reg);
534 static const struct clk_ops k210_pll2_ops = {
535 .enable = k210_pll_enable,
536 .disable = k210_pll_disable,
537 .is_enabled = k210_pll_is_enabled,
538 .recalc_rate = k210_pll_get_rate,
539 .determine_rate = clk_hw_determine_rate_no_reparent,
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 .determine_rate = clk_hw_determine_rate_no_reparent,
639 .set_parent = k210_aclk_set_parent,
640 .get_parent = k210_aclk_get_parent,
641 .recalc_rate = k210_aclk_get_rate,
645 * ACLK has IN0 and PLL0 as parents.
647 static int __init k210_register_aclk(struct device_node *np,
648 struct k210_sysclk *ksc)
650 struct clk_init_data init = {};
651 const struct clk_parent_data parent_data[] = {
652 { /* .index = 0 for in0 */ },
653 { .hw = &ksc->plls[K210_PLL0].hw },
658 init.parent_data = parent_data;
659 init.num_parents = 2;
660 init.ops = &k210_aclk_ops;
661 ksc->aclk.init = &init;
663 ret = of_clk_hw_register(np, &ksc->aclk);
665 pr_err("%pOFP: register aclk failed\n", np);
672 #define to_k210_clk(_hw) container_of(_hw, struct k210_clk, hw)
674 static int k210_clk_enable(struct clk_hw *hw)
676 struct k210_clk *kclk = to_k210_clk(hw);
677 struct k210_sysclk *ksc = kclk->ksc;
678 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
685 spin_lock_irqsave(&ksc->clk_lock, flags);
686 reg = readl(ksc->regs + cfg->gate_reg);
687 reg |= BIT(cfg->gate_bit);
688 writel(reg, ksc->regs + cfg->gate_reg);
689 spin_unlock_irqrestore(&ksc->clk_lock, flags);
694 static void k210_clk_disable(struct clk_hw *hw)
696 struct k210_clk *kclk = to_k210_clk(hw);
697 struct k210_sysclk *ksc = kclk->ksc;
698 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
705 spin_lock_irqsave(&ksc->clk_lock, flags);
706 reg = readl(ksc->regs + cfg->gate_reg);
707 reg &= ~BIT(cfg->gate_bit);
708 writel(reg, ksc->regs + cfg->gate_reg);
709 spin_unlock_irqrestore(&ksc->clk_lock, flags);
712 static int k210_clk_set_parent(struct clk_hw *hw, u8 index)
714 struct k210_clk *kclk = to_k210_clk(hw);
715 struct k210_sysclk *ksc = kclk->ksc;
716 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
720 spin_lock_irqsave(&ksc->clk_lock, flags);
721 reg = readl(ksc->regs + cfg->mux_reg);
723 reg |= BIT(cfg->mux_bit);
725 reg &= ~BIT(cfg->mux_bit);
726 writel(reg, ksc->regs + cfg->mux_reg);
727 spin_unlock_irqrestore(&ksc->clk_lock, flags);
732 static u8 k210_clk_get_parent(struct clk_hw *hw)
734 struct k210_clk *kclk = to_k210_clk(hw);
735 struct k210_sysclk *ksc = kclk->ksc;
736 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
740 spin_lock_irqsave(&ksc->clk_lock, flags);
741 reg = readl(ksc->regs + cfg->mux_reg);
742 idx = (reg & BIT(cfg->mux_bit)) ? 1 : 0;
743 spin_unlock_irqrestore(&ksc->clk_lock, flags);
748 static unsigned long k210_clk_get_rate(struct clk_hw *hw,
749 unsigned long parent_rate)
751 struct k210_clk *kclk = to_k210_clk(hw);
752 struct k210_sysclk *ksc = kclk->ksc;
753 struct k210_clk_cfg *cfg = &k210_clk_cfgs[kclk->id];
759 reg = readl(ksc->regs + cfg->div_reg);
760 div_val = (reg >> cfg->div_shift) & GENMASK(cfg->div_width - 1, 0);
762 switch (cfg->div_type) {
763 case K210_DIV_ONE_BASED:
764 return parent_rate / (div_val + 1);
765 case K210_DIV_DOUBLE_ONE_BASED:
766 return parent_rate / ((div_val + 1) * 2);
767 case K210_DIV_POWER_OF_TWO:
768 return parent_rate / (2UL << div_val);
775 static const struct clk_ops k210_clk_mux_ops = {
776 .enable = k210_clk_enable,
777 .disable = k210_clk_disable,
778 .determine_rate = clk_hw_determine_rate_no_reparent,
779 .set_parent = k210_clk_set_parent,
780 .get_parent = k210_clk_get_parent,
781 .recalc_rate = k210_clk_get_rate,
784 static const struct clk_ops k210_clk_ops = {
785 .enable = k210_clk_enable,
786 .disable = k210_clk_disable,
787 .recalc_rate = k210_clk_get_rate,
790 static void __init k210_register_clk(struct device_node *np,
791 struct k210_sysclk *ksc, int id,
792 const struct clk_parent_data *parent_data,
793 int num_parents, unsigned long flags)
795 struct k210_clk *kclk = &ksc->clks[id];
796 struct clk_init_data init = {};
799 init.name = k210_clk_cfgs[id].name;
801 init.parent_data = parent_data;
802 init.num_parents = num_parents;
804 init.ops = &k210_clk_mux_ops;
806 init.ops = &k210_clk_ops;
810 kclk->hw.init = &init;
812 ret = of_clk_hw_register(np, &kclk->hw);
814 pr_err("%pOFP: register clock %s failed\n",
815 np, k210_clk_cfgs[id].name);
821 * All muxed clocks have IN0 and PLL0 as parents.
823 static inline void __init k210_register_mux_clk(struct device_node *np,
824 struct k210_sysclk *ksc, int id)
826 const struct clk_parent_data parent_data[2] = {
827 { /* .index = 0 for in0 */ },
828 { .hw = &ksc->plls[K210_PLL0].hw }
831 k210_register_clk(np, ksc, id, parent_data, 2, 0);
834 static inline void __init k210_register_in0_child(struct device_node *np,
835 struct k210_sysclk *ksc, int id)
837 const struct clk_parent_data parent_data = {
838 /* .index = 0 for in0 */
841 k210_register_clk(np, ksc, id, &parent_data, 1, 0);
844 static inline void __init k210_register_pll_child(struct device_node *np,
845 struct k210_sysclk *ksc, int id,
846 enum k210_pll_id pllid,
849 const struct clk_parent_data parent_data = {
850 .hw = &ksc->plls[pllid].hw,
853 k210_register_clk(np, ksc, id, &parent_data, 1, flags);
856 static inline void __init k210_register_aclk_child(struct device_node *np,
857 struct k210_sysclk *ksc, int id,
860 const struct clk_parent_data parent_data = {
864 k210_register_clk(np, ksc, id, &parent_data, 1, flags);
867 static inline void __init k210_register_clk_child(struct device_node *np,
868 struct k210_sysclk *ksc, int id,
871 const struct clk_parent_data parent_data = {
872 .hw = &ksc->clks[parent_id].hw,
875 k210_register_clk(np, ksc, id, &parent_data, 1, 0);
878 static struct clk_hw *k210_clk_hw_onecell_get(struct of_phandle_args *clkspec,
881 struct k210_sysclk *ksc = data;
882 unsigned int idx = clkspec->args[0];
884 if (idx >= K210_NUM_CLKS)
885 return ERR_PTR(-EINVAL);
887 return &ksc->clks[idx].hw;
890 static void __init k210_clk_init(struct device_node *np)
892 struct device_node *sysctl_np;
893 struct k210_sysclk *ksc;
896 ksc = kzalloc(sizeof(*ksc), GFP_KERNEL);
900 spin_lock_init(&ksc->clk_lock);
901 sysctl_np = of_get_parent(np);
902 ksc->regs = of_iomap(sysctl_np, 0);
903 of_node_put(sysctl_np);
905 pr_err("%pOFP: failed to map registers\n", np);
909 ret = k210_register_plls(np, ksc);
913 ret = k210_register_aclk(np, ksc);
918 * Critical clocks: there are no consumers of the SRAM clocks,
919 * including the AI clock for the third SRAM bank. The CPU clock
920 * is only referenced by the uarths serial device and so would be
921 * disabled if the serial console is disabled to switch to another
922 * console. Mark all these clocks as critical so that they are never
923 * disabled by the core clock management.
925 k210_register_aclk_child(np, ksc, K210_CLK_CPU, CLK_IS_CRITICAL);
926 k210_register_aclk_child(np, ksc, K210_CLK_SRAM0, CLK_IS_CRITICAL);
927 k210_register_aclk_child(np, ksc, K210_CLK_SRAM1, CLK_IS_CRITICAL);
928 k210_register_pll_child(np, ksc, K210_CLK_AI, K210_PLL1,
931 /* Clocks with aclk as source */
932 k210_register_aclk_child(np, ksc, K210_CLK_DMA, 0);
933 k210_register_aclk_child(np, ksc, K210_CLK_FFT, 0);
934 k210_register_aclk_child(np, ksc, K210_CLK_ROM, 0);
935 k210_register_aclk_child(np, ksc, K210_CLK_DVP, 0);
936 k210_register_aclk_child(np, ksc, K210_CLK_APB0, 0);
937 k210_register_aclk_child(np, ksc, K210_CLK_APB1, 0);
938 k210_register_aclk_child(np, ksc, K210_CLK_APB2, 0);
940 /* Clocks with PLL0 as source */
941 k210_register_pll_child(np, ksc, K210_CLK_SPI0, K210_PLL0, 0);
942 k210_register_pll_child(np, ksc, K210_CLK_SPI1, K210_PLL0, 0);
943 k210_register_pll_child(np, ksc, K210_CLK_SPI2, K210_PLL0, 0);
944 k210_register_pll_child(np, ksc, K210_CLK_I2C0, K210_PLL0, 0);
945 k210_register_pll_child(np, ksc, K210_CLK_I2C1, K210_PLL0, 0);
946 k210_register_pll_child(np, ksc, K210_CLK_I2C2, K210_PLL0, 0);
948 /* Clocks with PLL2 as source */
949 k210_register_pll_child(np, ksc, K210_CLK_I2S0, K210_PLL2, 0);
950 k210_register_pll_child(np, ksc, K210_CLK_I2S1, K210_PLL2, 0);
951 k210_register_pll_child(np, ksc, K210_CLK_I2S2, K210_PLL2, 0);
952 k210_register_pll_child(np, ksc, K210_CLK_I2S0_M, K210_PLL2, 0);
953 k210_register_pll_child(np, ksc, K210_CLK_I2S1_M, K210_PLL2, 0);
954 k210_register_pll_child(np, ksc, K210_CLK_I2S2_M, K210_PLL2, 0);
956 /* Clocks with IN0 as source */
957 k210_register_in0_child(np, ksc, K210_CLK_WDT0);
958 k210_register_in0_child(np, ksc, K210_CLK_WDT1);
959 k210_register_in0_child(np, ksc, K210_CLK_RTC);
961 /* Clocks with APB0 as source */
962 k210_register_clk_child(np, ksc, K210_CLK_GPIO, K210_CLK_APB0);
963 k210_register_clk_child(np, ksc, K210_CLK_UART1, K210_CLK_APB0);
964 k210_register_clk_child(np, ksc, K210_CLK_UART2, K210_CLK_APB0);
965 k210_register_clk_child(np, ksc, K210_CLK_UART3, K210_CLK_APB0);
966 k210_register_clk_child(np, ksc, K210_CLK_FPIOA, K210_CLK_APB0);
967 k210_register_clk_child(np, ksc, K210_CLK_SHA, K210_CLK_APB0);
969 /* Clocks with APB1 as source */
970 k210_register_clk_child(np, ksc, K210_CLK_AES, K210_CLK_APB1);
971 k210_register_clk_child(np, ksc, K210_CLK_OTP, K210_CLK_APB1);
973 /* Mux clocks with in0 or pll0 as source */
974 k210_register_mux_clk(np, ksc, K210_CLK_SPI3);
975 k210_register_mux_clk(np, ksc, K210_CLK_TIMER0);
976 k210_register_mux_clk(np, ksc, K210_CLK_TIMER1);
977 k210_register_mux_clk(np, ksc, K210_CLK_TIMER2);
979 /* Check for registration errors */
980 for (i = 0; i < K210_NUM_CLKS; i++) {
981 if (ksc->clks[i].id != i)
985 ret = of_clk_add_hw_provider(np, k210_clk_hw_onecell_get, ksc);
987 pr_err("%pOFP: add clock provider failed %d\n", np, ret);
991 pr_info("%pOFP: CPU running at %lu MHz\n",
992 np, clk_hw_get_rate(&ksc->clks[K210_CLK_CPU].hw) / 1000000);
995 CLK_OF_DECLARE(k210_clk, "canaan,k210-clk", k210_clk_init);
998 * Enable PLL1 to be able to use the AI SRAM.
1000 void __init k210_clk_early_init(void __iomem *regs)
1002 struct k210_pll pll1;
1004 /* Make sure ACLK selector is set to PLL0 */
1005 k210_aclk_set_selector(regs, 1);
1007 /* Startup PLL1 to enable the aisram bank for general memory use */
1008 k210_init_pll(regs, K210_PLL1, &pll1);
1009 k210_pll_enable_hw(regs, &pll1);