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 * @denom: 1/denominator unit
210 * @lock: register lock
212 * 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.
221 struct xgene_clk_pmd {
231 #define to_xgene_clk_pmd(_hw) container_of(_hw, struct xgene_clk_pmd, hw)
233 #define XGENE_CLK_PMD_SCALE_INVERTED BIT(0)
234 #define XGENE_CLK_PMD_SHIFT 8
235 #define XGENE_CLK_PMD_WIDTH 3
237 static unsigned long xgene_clk_pmd_recalc_rate(struct clk_hw *hw,
238 unsigned long parent_rate)
240 struct xgene_clk_pmd *fd = to_xgene_clk_pmd(hw);
241 unsigned long flags = 0;
246 spin_lock_irqsave(fd->lock, flags);
250 val = readl(fd->reg);
253 spin_unlock_irqrestore(fd->lock, flags);
257 ret = (u64)parent_rate;
259 scale = (val & fd->mask) >> fd->shift;
260 if (fd->flags & XGENE_CLK_PMD_SCALE_INVERTED)
261 scale = fd->denom - scale;
265 /* freq = parent_rate * scaler / denom */
266 do_div(ret, fd->denom);
269 ret = (u64)parent_rate;
274 static long xgene_clk_pmd_round_rate(struct clk_hw *hw, unsigned long rate,
275 unsigned long *parent_rate)
277 struct xgene_clk_pmd *fd = to_xgene_clk_pmd(hw);
280 if (!rate || rate >= *parent_rate)
283 /* freq = parent_rate * scaler / denom */
284 ret = rate * fd->denom;
285 scale = DIV_ROUND_UP_ULL(ret, *parent_rate);
287 ret = (u64)*parent_rate * scale;
288 do_div(ret, fd->denom);
293 static int xgene_clk_pmd_set_rate(struct clk_hw *hw, unsigned long rate,
294 unsigned long parent_rate)
296 struct xgene_clk_pmd *fd = to_xgene_clk_pmd(hw);
297 unsigned long flags = 0;
302 * Compute the scaler:
304 * freq = parent_rate * scaler / denom, or
305 * scaler = freq * denom / parent_rate
307 ret = rate * fd->denom;
308 scale = DIV_ROUND_UP_ULL(ret, (u64)parent_rate);
310 /* Check if inverted */
311 if (fd->flags & XGENE_CLK_PMD_SCALE_INVERTED)
312 scale = fd->denom - scale;
317 spin_lock_irqsave(fd->lock, flags);
321 val = readl(fd->reg);
323 val |= (scale << fd->shift);
324 writel(val, fd->reg);
327 spin_unlock_irqrestore(fd->lock, flags);
334 static const struct clk_ops xgene_clk_pmd_ops = {
335 .recalc_rate = xgene_clk_pmd_recalc_rate,
336 .round_rate = xgene_clk_pmd_round_rate,
337 .set_rate = xgene_clk_pmd_set_rate,
341 xgene_register_clk_pmd(struct device *dev,
342 const char *name, const char *parent_name,
343 unsigned long flags, void __iomem *reg, u8 shift,
344 u8 width, u64 denom, u32 clk_flags, spinlock_t *lock)
346 struct xgene_clk_pmd *fd;
347 struct clk_init_data init;
350 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
352 return ERR_PTR(-ENOMEM);
355 init.ops = &xgene_clk_pmd_ops;
357 init.parent_names = parent_name ? &parent_name : NULL;
358 init.num_parents = parent_name ? 1 : 0;
362 fd->mask = (BIT(width) - 1) << shift;
364 fd->flags = clk_flags;
368 clk = clk_register(dev, &fd->hw);
370 pr_err("%s: could not register clk %s\n", __func__, name);
378 static void xgene_pmdclk_init(struct device_node *np)
380 const char *clk_name = np->full_name;
381 void __iomem *csr_reg;
388 /* Check if the entry is disabled */
389 if (!of_device_is_available(np))
392 /* Parse the DTS register for resource */
393 rc = of_address_to_resource(np, 0, &res);
395 pr_err("no DTS register for %pOF\n", np);
398 csr_reg = of_iomap(np, 0);
400 pr_err("Unable to map resource for %pOF\n", np);
403 of_property_read_string(np, "clock-output-names", &clk_name);
405 denom = BIT(XGENE_CLK_PMD_WIDTH);
406 flags |= XGENE_CLK_PMD_SCALE_INVERTED;
408 clk = xgene_register_clk_pmd(NULL, clk_name,
409 of_clk_get_parent_name(np, 0), 0,
410 csr_reg, XGENE_CLK_PMD_SHIFT,
411 XGENE_CLK_PMD_WIDTH, denom,
414 of_clk_add_provider(np, of_clk_src_simple_get, clk);
415 clk_register_clkdev(clk, clk_name, NULL);
416 pr_debug("Add %s clock\n", clk_name);
424 struct xgene_dev_parameters {
425 void __iomem *csr_reg; /* CSR for IP clock */
426 u32 reg_clk_offset; /* Offset to clock enable CSR */
427 u32 reg_clk_mask; /* Mask bit for clock enable */
428 u32 reg_csr_offset; /* Offset to CSR reset */
429 u32 reg_csr_mask; /* Mask bit for disable CSR reset */
430 void __iomem *divider_reg; /* CSR for divider */
431 u32 reg_divider_offset; /* Offset to divider register */
432 u32 reg_divider_shift; /* Bit shift to divider field */
433 u32 reg_divider_width; /* Width of the bit to divider field */
439 struct xgene_dev_parameters param;
442 #define to_xgene_clk(_hw) container_of(_hw, struct xgene_clk, hw)
444 static int xgene_clk_enable(struct clk_hw *hw)
446 struct xgene_clk *pclk = to_xgene_clk(hw);
447 unsigned long flags = 0;
451 spin_lock_irqsave(pclk->lock, flags);
453 if (pclk->param.csr_reg) {
454 pr_debug("%s clock enabled\n", clk_hw_get_name(hw));
455 /* First enable the clock */
456 data = xgene_clk_read(pclk->param.csr_reg +
457 pclk->param.reg_clk_offset);
458 data |= pclk->param.reg_clk_mask;
459 xgene_clk_write(data, pclk->param.csr_reg +
460 pclk->param.reg_clk_offset);
461 pr_debug("%s clk offset 0x%08X mask 0x%08X value 0x%08X\n",
463 pclk->param.reg_clk_offset, pclk->param.reg_clk_mask,
466 /* Second enable the CSR */
467 data = xgene_clk_read(pclk->param.csr_reg +
468 pclk->param.reg_csr_offset);
469 data &= ~pclk->param.reg_csr_mask;
470 xgene_clk_write(data, pclk->param.csr_reg +
471 pclk->param.reg_csr_offset);
472 pr_debug("%s csr offset 0x%08X mask 0x%08X value 0x%08X\n",
474 pclk->param.reg_csr_offset, pclk->param.reg_csr_mask,
479 spin_unlock_irqrestore(pclk->lock, flags);
484 static void xgene_clk_disable(struct clk_hw *hw)
486 struct xgene_clk *pclk = to_xgene_clk(hw);
487 unsigned long flags = 0;
491 spin_lock_irqsave(pclk->lock, flags);
493 if (pclk->param.csr_reg) {
494 pr_debug("%s clock disabled\n", clk_hw_get_name(hw));
495 /* First put the CSR in reset */
496 data = xgene_clk_read(pclk->param.csr_reg +
497 pclk->param.reg_csr_offset);
498 data |= pclk->param.reg_csr_mask;
499 xgene_clk_write(data, pclk->param.csr_reg +
500 pclk->param.reg_csr_offset);
502 /* Second disable the clock */
503 data = xgene_clk_read(pclk->param.csr_reg +
504 pclk->param.reg_clk_offset);
505 data &= ~pclk->param.reg_clk_mask;
506 xgene_clk_write(data, pclk->param.csr_reg +
507 pclk->param.reg_clk_offset);
511 spin_unlock_irqrestore(pclk->lock, flags);
514 static int xgene_clk_is_enabled(struct clk_hw *hw)
516 struct xgene_clk *pclk = to_xgene_clk(hw);
519 if (pclk->param.csr_reg) {
520 pr_debug("%s clock checking\n", clk_hw_get_name(hw));
521 data = xgene_clk_read(pclk->param.csr_reg +
522 pclk->param.reg_clk_offset);
523 pr_debug("%s clock is %s\n", clk_hw_get_name(hw),
524 data & pclk->param.reg_clk_mask ? "enabled" :
528 if (!pclk->param.csr_reg)
530 return data & pclk->param.reg_clk_mask ? 1 : 0;
533 static unsigned long xgene_clk_recalc_rate(struct clk_hw *hw,
534 unsigned long parent_rate)
536 struct xgene_clk *pclk = to_xgene_clk(hw);
539 if (pclk->param.divider_reg) {
540 data = xgene_clk_read(pclk->param.divider_reg +
541 pclk->param.reg_divider_offset);
542 data >>= pclk->param.reg_divider_shift;
543 data &= (1 << pclk->param.reg_divider_width) - 1;
545 pr_debug("%s clock recalc rate %ld parent %ld\n",
547 parent_rate / data, parent_rate);
549 return parent_rate / data;
551 pr_debug("%s clock recalc rate %ld parent %ld\n",
552 clk_hw_get_name(hw), parent_rate, parent_rate);
557 static int xgene_clk_set_rate(struct clk_hw *hw, unsigned long rate,
558 unsigned long parent_rate)
560 struct xgene_clk *pclk = to_xgene_clk(hw);
561 unsigned long flags = 0;
567 spin_lock_irqsave(pclk->lock, flags);
569 if (pclk->param.divider_reg) {
570 /* Let's compute the divider */
571 if (rate > parent_rate)
573 divider_save = divider = parent_rate / rate; /* Rounded down */
574 divider &= (1 << pclk->param.reg_divider_width) - 1;
575 divider <<= pclk->param.reg_divider_shift;
577 /* Set new divider */
578 data = xgene_clk_read(pclk->param.divider_reg +
579 pclk->param.reg_divider_offset);
580 data &= ~(((1 << pclk->param.reg_divider_width) - 1)
581 << pclk->param.reg_divider_shift);
583 xgene_clk_write(data, pclk->param.divider_reg +
584 pclk->param.reg_divider_offset);
585 pr_debug("%s clock set rate %ld\n", clk_hw_get_name(hw),
586 parent_rate / divider_save);
592 spin_unlock_irqrestore(pclk->lock, flags);
594 return parent_rate / divider_save;
597 static long xgene_clk_round_rate(struct clk_hw *hw, unsigned long rate,
598 unsigned long *prate)
600 struct xgene_clk *pclk = to_xgene_clk(hw);
601 unsigned long parent_rate = *prate;
604 if (pclk->param.divider_reg) {
605 /* Let's compute the divider */
606 if (rate > parent_rate)
608 divider = parent_rate / rate; /* Rounded down */
613 return parent_rate / divider;
616 static const struct clk_ops xgene_clk_ops = {
617 .enable = xgene_clk_enable,
618 .disable = xgene_clk_disable,
619 .is_enabled = xgene_clk_is_enabled,
620 .recalc_rate = xgene_clk_recalc_rate,
621 .set_rate = xgene_clk_set_rate,
622 .round_rate = xgene_clk_round_rate,
625 static struct clk *xgene_register_clk(struct device *dev,
626 const char *name, const char *parent_name,
627 struct xgene_dev_parameters *parameters, spinlock_t *lock)
629 struct xgene_clk *apmclk;
631 struct clk_init_data init;
634 /* allocate the APM clock structure */
635 apmclk = kzalloc(sizeof(*apmclk), GFP_KERNEL);
637 return ERR_PTR(-ENOMEM);
640 init.ops = &xgene_clk_ops;
642 init.parent_names = parent_name ? &parent_name : NULL;
643 init.num_parents = parent_name ? 1 : 0;
646 apmclk->hw.init = &init;
647 apmclk->param = *parameters;
649 /* Register the clock */
650 clk = clk_register(dev, &apmclk->hw);
652 pr_err("%s: could not register clk %s\n", __func__, name);
657 /* Register the clock for lookup */
658 rc = clk_register_clkdev(clk, name, NULL);
660 pr_err("%s: could not register lookup clk %s\n",
666 static void __init xgene_devclk_init(struct device_node *np)
668 const char *clk_name = np->full_name;
672 struct xgene_dev_parameters parameters;
675 /* Check if the entry is disabled */
676 if (!of_device_is_available(np))
679 /* Parse the DTS register for resource */
680 parameters.csr_reg = NULL;
681 parameters.divider_reg = NULL;
682 for (i = 0; i < 2; i++) {
683 void __iomem *map_res;
684 rc = of_address_to_resource(np, i, &res);
687 pr_err("no DTS register for %pOF\n", np);
692 map_res = of_iomap(np, i);
694 pr_err("Unable to map resource %d for %pOF\n", i, np);
697 if (strcmp(res.name, "div-reg") == 0)
698 parameters.divider_reg = map_res;
699 else /* if (strcmp(res->name, "csr-reg") == 0) */
700 parameters.csr_reg = map_res;
702 if (of_property_read_u32(np, "csr-offset", ¶meters.reg_csr_offset))
703 parameters.reg_csr_offset = 0;
704 if (of_property_read_u32(np, "csr-mask", ¶meters.reg_csr_mask))
705 parameters.reg_csr_mask = 0xF;
706 if (of_property_read_u32(np, "enable-offset",
707 ¶meters.reg_clk_offset))
708 parameters.reg_clk_offset = 0x8;
709 if (of_property_read_u32(np, "enable-mask", ¶meters.reg_clk_mask))
710 parameters.reg_clk_mask = 0xF;
711 if (of_property_read_u32(np, "divider-offset",
712 ¶meters.reg_divider_offset))
713 parameters.reg_divider_offset = 0;
714 if (of_property_read_u32(np, "divider-width",
715 ¶meters.reg_divider_width))
716 parameters.reg_divider_width = 0;
717 if (of_property_read_u32(np, "divider-shift",
718 ¶meters.reg_divider_shift))
719 parameters.reg_divider_shift = 0;
720 of_property_read_string(np, "clock-output-names", &clk_name);
722 clk = xgene_register_clk(NULL, clk_name,
723 of_clk_get_parent_name(np, 0), ¶meters, &clk_lock);
726 pr_debug("Add %s clock\n", clk_name);
727 rc = of_clk_add_provider(np, of_clk_src_simple_get, clk);
729 pr_err("%s: could register provider clk %pOF\n", __func__, np);
734 if (parameters.csr_reg)
735 iounmap(parameters.csr_reg);
736 if (parameters.divider_reg)
737 iounmap(parameters.divider_reg);
740 CLK_OF_DECLARE(xgene_socpll_clock, "apm,xgene-socpll-clock", xgene_socpllclk_init);
741 CLK_OF_DECLARE(xgene_pcppll_clock, "apm,xgene-pcppll-clock", xgene_pcppllclk_init);
742 CLK_OF_DECLARE(xgene_pmd_clock, "apm,xgene-pmd-clock", xgene_pmdclk_init);
743 CLK_OF_DECLARE(xgene_socpll_v2_clock, "apm,xgene-socpll-v2-clock",
744 xgene_socpllclk_init);
745 CLK_OF_DECLARE(xgene_pcppll_v2_clock, "apm,xgene-pcppll-v2-clock",
746 xgene_pcppllclk_init);
747 CLK_OF_DECLARE(xgene_dev_clock, "apm,xgene-device-clock", xgene_devclk_init);