1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * clk-xgene.c - AppliedMicro X-Gene Clock Interface
5 * Copyright (c) 2013, Applied Micro Circuits Corporation
6 * Author: Loc Ho <lho@apm.com>
8 #include <linux/module.h>
9 #include <linux/spinlock.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk-provider.h>
14 #include <linux/of_address.h>
16 /* Register SCU_PCPPLL bit fields */
17 #define N_DIV_RD(src) ((src) & 0x000001ff)
18 #define SC_N_DIV_RD(src) ((src) & 0x0000007f)
19 #define SC_OUTDIV2(src) (((src) & 0x00000100) >> 8)
21 /* Register SCU_SOCPLL bit fields */
22 #define CLKR_RD(src) (((src) & 0x07000000)>>24)
23 #define CLKOD_RD(src) (((src) & 0x00300000)>>20)
24 #define REGSPEC_RESET_F1_MASK 0x00010000
25 #define CLKF_RD(src) (((src) & 0x000001ff))
27 #define XGENE_CLK_DRIVER_VER "0.1"
29 static DEFINE_SPINLOCK(clk_lock
);
31 static inline u32
xgene_clk_read(void __iomem
*csr
)
33 return readl_relaxed(csr
);
36 static inline void xgene_clk_write(u32 data
, void __iomem
*csr
)
38 writel_relaxed(data
, csr
);
47 struct xgene_clk_pll
{
52 enum xgene_pll_type type
;
56 #define to_xgene_clk_pll(_hw) container_of(_hw, struct xgene_clk_pll, hw)
58 static int xgene_clk_pll_is_enabled(struct clk_hw
*hw
)
60 struct xgene_clk_pll
*pllclk
= to_xgene_clk_pll(hw
);
63 data
= xgene_clk_read(pllclk
->reg
+ pllclk
->pll_offset
);
64 pr_debug("%s pll %s\n", clk_hw_get_name(hw
),
65 data
& REGSPEC_RESET_F1_MASK
? "disabled" : "enabled");
67 return data
& REGSPEC_RESET_F1_MASK
? 0 : 1;
70 static unsigned long xgene_clk_pll_recalc_rate(struct clk_hw
*hw
,
71 unsigned long parent_rate
)
73 struct xgene_clk_pll
*pllclk
= to_xgene_clk_pll(hw
);
81 pll
= xgene_clk_read(pllclk
->reg
+ pllclk
->pll_offset
);
83 if (pllclk
->version
<= 1) {
84 if (pllclk
->type
== PLL_TYPE_PCP
) {
86 * PLL VCO = Reference clock * NF
87 * PCP PLL = PLL_VCO / 2
90 fvco
= parent_rate
* (N_DIV_RD(pll
) + 4);
93 * Fref = Reference Clock / NREF;
97 nref
= CLKR_RD(pll
) + 1;
98 nout
= CLKOD_RD(pll
) + 1;
100 fref
= parent_rate
/ nref
;
105 * fvco = Reference clock * FBDIVC
106 * PLL freq = fvco / NOUT
108 nout
= SC_OUTDIV2(pll
) ? 2 : 3;
109 fvco
= parent_rate
* SC_N_DIV_RD(pll
);
111 pr_debug("%s pll recalc rate %ld parent %ld version %d\n",
112 clk_hw_get_name(hw
), fvco
/ nout
, parent_rate
,
118 static const struct clk_ops xgene_clk_pll_ops
= {
119 .is_enabled
= xgene_clk_pll_is_enabled
,
120 .recalc_rate
= xgene_clk_pll_recalc_rate
,
123 static struct clk
*xgene_register_clk_pll(struct device
*dev
,
124 const char *name
, const char *parent_name
,
125 unsigned long flags
, void __iomem
*reg
, u32 pll_offset
,
126 u32 type
, spinlock_t
*lock
, int version
)
128 struct xgene_clk_pll
*apmclk
;
130 struct clk_init_data init
;
132 /* allocate the APM clock structure */
133 apmclk
= kzalloc(sizeof(*apmclk
), GFP_KERNEL
);
135 return ERR_PTR(-ENOMEM
);
138 init
.ops
= &xgene_clk_pll_ops
;
140 init
.parent_names
= parent_name
? &parent_name
: NULL
;
141 init
.num_parents
= parent_name
? 1 : 0;
143 apmclk
->version
= version
;
146 apmclk
->pll_offset
= pll_offset
;
148 apmclk
->hw
.init
= &init
;
150 /* Register the clock */
151 clk
= clk_register(dev
, &apmclk
->hw
);
153 pr_err("%s: could not register clk %s\n", __func__
, name
);
160 static int xgene_pllclk_version(struct device_node
*np
)
162 if (of_device_is_compatible(np
, "apm,xgene-socpll-clock"))
164 if (of_device_is_compatible(np
, "apm,xgene-pcppll-clock"))
169 static void xgene_pllclk_init(struct device_node
*np
, enum xgene_pll_type pll_type
)
171 const char *clk_name
= np
->full_name
;
174 int version
= xgene_pllclk_version(np
);
176 reg
= of_iomap(np
, 0);
178 pr_err("Unable to map CSR register for %pOF\n", np
);
181 of_property_read_string(np
, "clock-output-names", &clk_name
);
182 clk
= xgene_register_clk_pll(NULL
,
183 clk_name
, of_clk_get_parent_name(np
, 0),
184 0, reg
, 0, pll_type
, &clk_lock
,
187 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
188 clk_register_clkdev(clk
, clk_name
, NULL
);
189 pr_debug("Add %s clock PLL\n", clk_name
);
193 static void xgene_socpllclk_init(struct device_node
*np
)
195 xgene_pllclk_init(np
, PLL_TYPE_SOC
);
198 static void xgene_pcppllclk_init(struct device_node
*np
)
200 xgene_pllclk_init(np
, PLL_TYPE_PCP
);
204 * struct xgene_clk_pmd - PMD clock
206 * @hw: handle between common and hardware-specific interfaces
207 * @reg: register containing the fractional scale multiplier (scaler)
208 * @shift: shift to the unit bit field
209 * @mask: mask to the unit bit field
210 * @denom: 1/denominator unit
211 * @lock: register lock
212 * @flags: XGENE_CLK_PMD_SCALE_INVERTED - By default the scaler is the value read
213 * from the register plus one. For example,
214 * 0 for (0 + 1) / denom,
215 * 1 for (1 + 1) / denom and etc.
216 * If this flag is set, it is
217 * 0 for (denom - 0) / denom,
218 * 1 for (denom - 1) / denom and etc.
220 struct xgene_clk_pmd
{
230 #define to_xgene_clk_pmd(_hw) container_of(_hw, struct xgene_clk_pmd, hw)
232 #define XGENE_CLK_PMD_SCALE_INVERTED BIT(0)
233 #define XGENE_CLK_PMD_SHIFT 8
234 #define XGENE_CLK_PMD_WIDTH 3
236 static unsigned long xgene_clk_pmd_recalc_rate(struct clk_hw
*hw
,
237 unsigned long parent_rate
)
239 struct xgene_clk_pmd
*fd
= to_xgene_clk_pmd(hw
);
240 unsigned long flags
= 0;
245 spin_lock_irqsave(fd
->lock
, flags
);
249 val
= readl(fd
->reg
);
252 spin_unlock_irqrestore(fd
->lock
, flags
);
256 ret
= (u64
)parent_rate
;
258 scale
= (val
& fd
->mask
) >> fd
->shift
;
259 if (fd
->flags
& XGENE_CLK_PMD_SCALE_INVERTED
)
260 scale
= fd
->denom
- scale
;
264 /* freq = parent_rate * scaler / denom */
265 do_div(ret
, fd
->denom
);
268 ret
= (u64
)parent_rate
;
273 static long xgene_clk_pmd_round_rate(struct clk_hw
*hw
, unsigned long rate
,
274 unsigned long *parent_rate
)
276 struct xgene_clk_pmd
*fd
= to_xgene_clk_pmd(hw
);
279 if (!rate
|| rate
>= *parent_rate
)
282 /* freq = parent_rate * scaler / denom */
283 ret
= rate
* fd
->denom
;
284 scale
= DIV_ROUND_UP_ULL(ret
, *parent_rate
);
286 ret
= (u64
)*parent_rate
* scale
;
287 do_div(ret
, fd
->denom
);
292 static int xgene_clk_pmd_set_rate(struct clk_hw
*hw
, unsigned long rate
,
293 unsigned long parent_rate
)
295 struct xgene_clk_pmd
*fd
= to_xgene_clk_pmd(hw
);
296 unsigned long flags
= 0;
301 * Compute the scaler:
303 * freq = parent_rate * scaler / denom, or
304 * scaler = freq * denom / parent_rate
306 ret
= rate
* fd
->denom
;
307 scale
= DIV_ROUND_UP_ULL(ret
, (u64
)parent_rate
);
309 /* Check if inverted */
310 if (fd
->flags
& XGENE_CLK_PMD_SCALE_INVERTED
)
311 scale
= fd
->denom
- scale
;
316 spin_lock_irqsave(fd
->lock
, flags
);
320 val
= readl(fd
->reg
);
322 val
|= (scale
<< fd
->shift
);
323 writel(val
, fd
->reg
);
326 spin_unlock_irqrestore(fd
->lock
, flags
);
333 static const struct clk_ops xgene_clk_pmd_ops
= {
334 .recalc_rate
= xgene_clk_pmd_recalc_rate
,
335 .round_rate
= xgene_clk_pmd_round_rate
,
336 .set_rate
= xgene_clk_pmd_set_rate
,
340 xgene_register_clk_pmd(struct device
*dev
,
341 const char *name
, const char *parent_name
,
342 unsigned long flags
, void __iomem
*reg
, u8 shift
,
343 u8 width
, u64 denom
, u32 clk_flags
, spinlock_t
*lock
)
345 struct xgene_clk_pmd
*fd
;
346 struct clk_init_data init
;
349 fd
= kzalloc(sizeof(*fd
), GFP_KERNEL
);
351 return ERR_PTR(-ENOMEM
);
354 init
.ops
= &xgene_clk_pmd_ops
;
356 init
.parent_names
= parent_name
? &parent_name
: NULL
;
357 init
.num_parents
= parent_name
? 1 : 0;
361 fd
->mask
= (BIT(width
) - 1) << shift
;
363 fd
->flags
= clk_flags
;
367 clk
= clk_register(dev
, &fd
->hw
);
369 pr_err("%s: could not register clk %s\n", __func__
, name
);
377 static void xgene_pmdclk_init(struct device_node
*np
)
379 const char *clk_name
= np
->full_name
;
380 void __iomem
*csr_reg
;
387 /* Check if the entry is disabled */
388 if (!of_device_is_available(np
))
391 /* Parse the DTS register for resource */
392 rc
= of_address_to_resource(np
, 0, &res
);
394 pr_err("no DTS register for %pOF\n", np
);
397 csr_reg
= of_iomap(np
, 0);
399 pr_err("Unable to map resource for %pOF\n", np
);
402 of_property_read_string(np
, "clock-output-names", &clk_name
);
404 denom
= BIT(XGENE_CLK_PMD_WIDTH
);
405 flags
|= XGENE_CLK_PMD_SCALE_INVERTED
;
407 clk
= xgene_register_clk_pmd(NULL
, clk_name
,
408 of_clk_get_parent_name(np
, 0), 0,
409 csr_reg
, XGENE_CLK_PMD_SHIFT
,
410 XGENE_CLK_PMD_WIDTH
, denom
,
413 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
414 clk_register_clkdev(clk
, clk_name
, NULL
);
415 pr_debug("Add %s clock\n", clk_name
);
423 struct xgene_dev_parameters
{
424 void __iomem
*csr_reg
; /* CSR for IP clock */
425 u32 reg_clk_offset
; /* Offset to clock enable CSR */
426 u32 reg_clk_mask
; /* Mask bit for clock enable */
427 u32 reg_csr_offset
; /* Offset to CSR reset */
428 u32 reg_csr_mask
; /* Mask bit for disable CSR reset */
429 void __iomem
*divider_reg
; /* CSR for divider */
430 u32 reg_divider_offset
; /* Offset to divider register */
431 u32 reg_divider_shift
; /* Bit shift to divider field */
432 u32 reg_divider_width
; /* Width of the bit to divider field */
438 struct xgene_dev_parameters param
;
441 #define to_xgene_clk(_hw) container_of(_hw, struct xgene_clk, hw)
443 static int xgene_clk_enable(struct clk_hw
*hw
)
445 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
446 unsigned long flags
= 0;
450 spin_lock_irqsave(pclk
->lock
, flags
);
452 if (pclk
->param
.csr_reg
) {
453 pr_debug("%s clock enabled\n", clk_hw_get_name(hw
));
454 /* First enable the clock */
455 data
= xgene_clk_read(pclk
->param
.csr_reg
+
456 pclk
->param
.reg_clk_offset
);
457 data
|= pclk
->param
.reg_clk_mask
;
458 xgene_clk_write(data
, pclk
->param
.csr_reg
+
459 pclk
->param
.reg_clk_offset
);
460 pr_debug("%s clk offset 0x%08X mask 0x%08X value 0x%08X\n",
462 pclk
->param
.reg_clk_offset
, pclk
->param
.reg_clk_mask
,
465 /* Second enable the CSR */
466 data
= xgene_clk_read(pclk
->param
.csr_reg
+
467 pclk
->param
.reg_csr_offset
);
468 data
&= ~pclk
->param
.reg_csr_mask
;
469 xgene_clk_write(data
, pclk
->param
.csr_reg
+
470 pclk
->param
.reg_csr_offset
);
471 pr_debug("%s csr offset 0x%08X mask 0x%08X value 0x%08X\n",
473 pclk
->param
.reg_csr_offset
, pclk
->param
.reg_csr_mask
,
478 spin_unlock_irqrestore(pclk
->lock
, flags
);
483 static void xgene_clk_disable(struct clk_hw
*hw
)
485 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
486 unsigned long flags
= 0;
490 spin_lock_irqsave(pclk
->lock
, flags
);
492 if (pclk
->param
.csr_reg
) {
493 pr_debug("%s clock disabled\n", clk_hw_get_name(hw
));
494 /* First put the CSR in reset */
495 data
= xgene_clk_read(pclk
->param
.csr_reg
+
496 pclk
->param
.reg_csr_offset
);
497 data
|= pclk
->param
.reg_csr_mask
;
498 xgene_clk_write(data
, pclk
->param
.csr_reg
+
499 pclk
->param
.reg_csr_offset
);
501 /* Second disable the clock */
502 data
= xgene_clk_read(pclk
->param
.csr_reg
+
503 pclk
->param
.reg_clk_offset
);
504 data
&= ~pclk
->param
.reg_clk_mask
;
505 xgene_clk_write(data
, pclk
->param
.csr_reg
+
506 pclk
->param
.reg_clk_offset
);
510 spin_unlock_irqrestore(pclk
->lock
, flags
);
513 static int xgene_clk_is_enabled(struct clk_hw
*hw
)
515 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
518 if (pclk
->param
.csr_reg
) {
519 pr_debug("%s clock checking\n", clk_hw_get_name(hw
));
520 data
= xgene_clk_read(pclk
->param
.csr_reg
+
521 pclk
->param
.reg_clk_offset
);
522 pr_debug("%s clock is %s\n", clk_hw_get_name(hw
),
523 data
& pclk
->param
.reg_clk_mask
? "enabled" :
529 return data
& pclk
->param
.reg_clk_mask
? 1 : 0;
532 static unsigned long xgene_clk_recalc_rate(struct clk_hw
*hw
,
533 unsigned long parent_rate
)
535 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
538 if (pclk
->param
.divider_reg
) {
539 data
= xgene_clk_read(pclk
->param
.divider_reg
+
540 pclk
->param
.reg_divider_offset
);
541 data
>>= pclk
->param
.reg_divider_shift
;
542 data
&= (1 << pclk
->param
.reg_divider_width
) - 1;
544 pr_debug("%s clock recalc rate %ld parent %ld\n",
546 parent_rate
/ data
, parent_rate
);
548 return parent_rate
/ data
;
550 pr_debug("%s clock recalc rate %ld parent %ld\n",
551 clk_hw_get_name(hw
), parent_rate
, parent_rate
);
556 static int xgene_clk_set_rate(struct clk_hw
*hw
, unsigned long rate
,
557 unsigned long parent_rate
)
559 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
560 unsigned long flags
= 0;
566 spin_lock_irqsave(pclk
->lock
, flags
);
568 if (pclk
->param
.divider_reg
) {
569 /* Let's compute the divider */
570 if (rate
> parent_rate
)
572 divider_save
= divider
= parent_rate
/ rate
; /* Rounded down */
573 divider
&= (1 << pclk
->param
.reg_divider_width
) - 1;
574 divider
<<= pclk
->param
.reg_divider_shift
;
576 /* Set new divider */
577 data
= xgene_clk_read(pclk
->param
.divider_reg
+
578 pclk
->param
.reg_divider_offset
);
579 data
&= ~(((1 << pclk
->param
.reg_divider_width
) - 1)
580 << pclk
->param
.reg_divider_shift
);
582 xgene_clk_write(data
, pclk
->param
.divider_reg
+
583 pclk
->param
.reg_divider_offset
);
584 pr_debug("%s clock set rate %ld\n", clk_hw_get_name(hw
),
585 parent_rate
/ divider_save
);
591 spin_unlock_irqrestore(pclk
->lock
, flags
);
593 return parent_rate
/ divider_save
;
596 static long xgene_clk_round_rate(struct clk_hw
*hw
, unsigned long rate
,
597 unsigned long *prate
)
599 struct xgene_clk
*pclk
= to_xgene_clk(hw
);
600 unsigned long parent_rate
= *prate
;
603 if (pclk
->param
.divider_reg
) {
604 /* Let's compute the divider */
605 if (rate
> parent_rate
)
607 divider
= parent_rate
/ rate
; /* Rounded down */
612 return parent_rate
/ divider
;
615 static const struct clk_ops xgene_clk_ops
= {
616 .enable
= xgene_clk_enable
,
617 .disable
= xgene_clk_disable
,
618 .is_enabled
= xgene_clk_is_enabled
,
619 .recalc_rate
= xgene_clk_recalc_rate
,
620 .set_rate
= xgene_clk_set_rate
,
621 .round_rate
= xgene_clk_round_rate
,
624 static struct clk
*xgene_register_clk(struct device
*dev
,
625 const char *name
, const char *parent_name
,
626 struct xgene_dev_parameters
*parameters
, spinlock_t
*lock
)
628 struct xgene_clk
*apmclk
;
630 struct clk_init_data init
;
633 /* allocate the APM clock structure */
634 apmclk
= kzalloc(sizeof(*apmclk
), GFP_KERNEL
);
636 return ERR_PTR(-ENOMEM
);
639 init
.ops
= &xgene_clk_ops
;
641 init
.parent_names
= parent_name
? &parent_name
: NULL
;
642 init
.num_parents
= parent_name
? 1 : 0;
645 apmclk
->hw
.init
= &init
;
646 apmclk
->param
= *parameters
;
648 /* Register the clock */
649 clk
= clk_register(dev
, &apmclk
->hw
);
651 pr_err("%s: could not register clk %s\n", __func__
, name
);
656 /* Register the clock for lookup */
657 rc
= clk_register_clkdev(clk
, name
, NULL
);
659 pr_err("%s: could not register lookup clk %s\n",
665 static void __init
xgene_devclk_init(struct device_node
*np
)
667 const char *clk_name
= np
->full_name
;
671 struct xgene_dev_parameters parameters
;
674 /* Check if the entry is disabled */
675 if (!of_device_is_available(np
))
678 /* Parse the DTS register for resource */
679 parameters
.csr_reg
= NULL
;
680 parameters
.divider_reg
= NULL
;
681 for (i
= 0; i
< 2; i
++) {
682 void __iomem
*map_res
;
683 rc
= of_address_to_resource(np
, i
, &res
);
686 pr_err("no DTS register for %pOF\n", np
);
691 map_res
= of_iomap(np
, i
);
693 pr_err("Unable to map resource %d for %pOF\n", i
, np
);
696 if (strcmp(res
.name
, "div-reg") == 0)
697 parameters
.divider_reg
= map_res
;
698 else /* if (strcmp(res->name, "csr-reg") == 0) */
699 parameters
.csr_reg
= map_res
;
701 if (of_property_read_u32(np
, "csr-offset", ¶meters
.reg_csr_offset
))
702 parameters
.reg_csr_offset
= 0;
703 if (of_property_read_u32(np
, "csr-mask", ¶meters
.reg_csr_mask
))
704 parameters
.reg_csr_mask
= 0xF;
705 if (of_property_read_u32(np
, "enable-offset",
706 ¶meters
.reg_clk_offset
))
707 parameters
.reg_clk_offset
= 0x8;
708 if (of_property_read_u32(np
, "enable-mask", ¶meters
.reg_clk_mask
))
709 parameters
.reg_clk_mask
= 0xF;
710 if (of_property_read_u32(np
, "divider-offset",
711 ¶meters
.reg_divider_offset
))
712 parameters
.reg_divider_offset
= 0;
713 if (of_property_read_u32(np
, "divider-width",
714 ¶meters
.reg_divider_width
))
715 parameters
.reg_divider_width
= 0;
716 if (of_property_read_u32(np
, "divider-shift",
717 ¶meters
.reg_divider_shift
))
718 parameters
.reg_divider_shift
= 0;
719 of_property_read_string(np
, "clock-output-names", &clk_name
);
721 clk
= xgene_register_clk(NULL
, clk_name
,
722 of_clk_get_parent_name(np
, 0), ¶meters
, &clk_lock
);
725 pr_debug("Add %s clock\n", clk_name
);
726 rc
= of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
728 pr_err("%s: could register provider clk %pOF\n", __func__
, np
);
733 if (parameters
.csr_reg
)
734 iounmap(parameters
.csr_reg
);
735 if (parameters
.divider_reg
)
736 iounmap(parameters
.divider_reg
);
739 CLK_OF_DECLARE(xgene_socpll_clock
, "apm,xgene-socpll-clock", xgene_socpllclk_init
);
740 CLK_OF_DECLARE(xgene_pcppll_clock
, "apm,xgene-pcppll-clock", xgene_pcppllclk_init
);
741 CLK_OF_DECLARE(xgene_pmd_clock
, "apm,xgene-pmd-clock", xgene_pmdclk_init
);
742 CLK_OF_DECLARE(xgene_socpll_v2_clock
, "apm,xgene-socpll-v2-clock",
743 xgene_socpllclk_init
);
744 CLK_OF_DECLARE(xgene_pcppll_v2_clock
, "apm,xgene-pcppll-v2-clock",
745 xgene_pcppllclk_init
);
746 CLK_OF_DECLARE(xgene_dev_clock
, "apm,xgene-device-clock", xgene_devclk_init
);