1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Freescale Semiconductor, Inc.
4 * Copyright 2017~2018 NXP
10 #include <linux/bits.h>
11 #include <linux/clk-provider.h>
12 #include <linux/err.h>
14 #include <linux/iopoll.h>
15 #include <linux/slab.h>
19 /* PLL Control Status Register (xPLLCSR) */
20 #define PLL_CSR_OFFSET 0x0
21 #define PLL_VLD BIT(24)
24 /* PLL Configuration Register (xPLLCFG) */
25 #define PLL_CFG_OFFSET 0x08
26 #define IMX8ULP_PLL_CFG_OFFSET 0x10
27 #define BP_PLL_MULT 16
28 #define BM_PLL_MULT (0x7f << 16)
30 /* PLL Numerator Register (xPLLNUM) */
31 #define PLL_NUM_OFFSET 0x10
32 #define IMX8ULP_PLL_NUM_OFFSET 0x1c
34 /* PLL Denominator Register (xPLLDENOM) */
35 #define PLL_DENOM_OFFSET 0x14
36 #define IMX8ULP_PLL_DENOM_OFFSET 0x18
38 #define MAX_MFD 0x3fffffff
39 #define DEFAULT_MFD 1000000
49 /* Valid PLL MULT Table */
50 static const int pllv4_mult_table[] = {33, 27, 22, 20, 17, 16};
52 #define to_clk_pllv4(__hw) container_of(__hw, struct clk_pllv4, hw)
54 #define LOCK_TIMEOUT_US USEC_PER_MSEC
56 static inline int clk_pllv4_wait_lock(struct clk_pllv4 *pll)
60 return readl_poll_timeout(pll->base + PLL_CSR_OFFSET,
61 csr, csr & PLL_VLD, 0, LOCK_TIMEOUT_US);
64 static int clk_pllv4_is_prepared(struct clk_hw *hw)
66 struct clk_pllv4 *pll = to_clk_pllv4(hw);
68 if (readl_relaxed(pll->base) & PLL_EN)
74 static unsigned long clk_pllv4_recalc_rate(struct clk_hw *hw,
75 unsigned long parent_rate)
77 struct clk_pllv4 *pll = to_clk_pllv4(hw);
81 mult = readl_relaxed(pll->base + pll->cfg_offset);
85 mfn = readl_relaxed(pll->base + pll->num_offset);
86 mfd = readl_relaxed(pll->base + pll->denom_offset);
91 return (parent_rate * mult) + (u32)temp64;
94 static long clk_pllv4_round_rate(struct clk_hw *hw, unsigned long rate,
97 unsigned long parent_rate = *prate;
98 unsigned long round_rate, i;
99 u32 mfn, mfd = DEFAULT_MFD;
103 for (i = 0; i < ARRAY_SIZE(pllv4_mult_table); i++) {
104 round_rate = parent_rate * pllv4_mult_table[i];
105 if (rate >= round_rate) {
112 pr_warn("%s: unable to round rate %lu, parent rate %lu\n",
113 clk_hw_get_name(hw), rate, parent_rate);
117 if (parent_rate <= MAX_MFD)
120 temp64 = (u64)(rate - round_rate);
122 do_div(temp64, parent_rate);
126 * NOTE: The value of numerator must always be configured to be
127 * less than the value of the denominator. If we can't get a proper
128 * pair of mfn/mfd, we simply return the round_rate without using
134 temp64 = (u64)parent_rate;
138 return round_rate + (u32)temp64;
141 static bool clk_pllv4_is_valid_mult(unsigned int mult)
145 /* check if mult is in valid MULT table */
146 for (i = 0; i < ARRAY_SIZE(pllv4_mult_table); i++) {
147 if (pllv4_mult_table[i] == mult)
154 static int clk_pllv4_set_rate(struct clk_hw *hw, unsigned long rate,
155 unsigned long parent_rate)
157 struct clk_pllv4 *pll = to_clk_pllv4(hw);
158 u32 val, mult, mfn, mfd = DEFAULT_MFD;
161 mult = rate / parent_rate;
163 if (!clk_pllv4_is_valid_mult(mult))
166 if (parent_rate <= MAX_MFD)
169 temp64 = (u64)(rate - mult * parent_rate);
171 do_div(temp64, parent_rate);
174 val = readl_relaxed(pll->base + pll->cfg_offset);
176 val |= mult << BP_PLL_MULT;
177 writel_relaxed(val, pll->base + pll->cfg_offset);
179 writel_relaxed(mfn, pll->base + pll->num_offset);
180 writel_relaxed(mfd, pll->base + pll->denom_offset);
185 static int clk_pllv4_prepare(struct clk_hw *hw)
188 struct clk_pllv4 *pll = to_clk_pllv4(hw);
190 val = readl_relaxed(pll->base);
192 writel_relaxed(val, pll->base);
194 return clk_pllv4_wait_lock(pll);
197 static void clk_pllv4_unprepare(struct clk_hw *hw)
200 struct clk_pllv4 *pll = to_clk_pllv4(hw);
202 val = readl_relaxed(pll->base);
204 writel_relaxed(val, pll->base);
207 static const struct clk_ops clk_pllv4_ops = {
208 .recalc_rate = clk_pllv4_recalc_rate,
209 .round_rate = clk_pllv4_round_rate,
210 .set_rate = clk_pllv4_set_rate,
211 .prepare = clk_pllv4_prepare,
212 .unprepare = clk_pllv4_unprepare,
213 .is_prepared = clk_pllv4_is_prepared,
216 struct clk_hw *imx_clk_hw_pllv4(enum imx_pllv4_type type, const char *name,
217 const char *parent_name, void __iomem *base)
219 struct clk_pllv4 *pll;
221 struct clk_init_data init;
224 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
226 return ERR_PTR(-ENOMEM);
230 if (type == IMX_PLLV4_IMX8ULP) {
231 pll->cfg_offset = IMX8ULP_PLL_CFG_OFFSET;
232 pll->num_offset = IMX8ULP_PLL_NUM_OFFSET;
233 pll->denom_offset = IMX8ULP_PLL_DENOM_OFFSET;
235 pll->cfg_offset = PLL_CFG_OFFSET;
236 pll->num_offset = PLL_NUM_OFFSET;
237 pll->denom_offset = PLL_DENOM_OFFSET;
241 init.ops = &clk_pllv4_ops;
242 init.parent_names = &parent_name;
243 init.num_parents = 1;
244 init.flags = CLK_SET_RATE_GATE;
246 pll->hw.init = &init;
249 ret = clk_hw_register(NULL, hw);
257 EXPORT_SYMBOL_GPL(imx_clk_hw_pllv4);