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 BP_PLL_MULT 16
27 #define BM_PLL_MULT (0x7f << 16)
29 /* PLL Numerator Register (xPLLNUM) */
30 #define PLL_NUM_OFFSET 0x10
32 /* PLL Denominator Register (xPLLDENOM) */
33 #define PLL_DENOM_OFFSET 0x14
35 #define MAX_MFD 0x3fffffff
36 #define DEFAULT_MFD 1000000
43 /* Valid PLL MULT Table */
44 static const int pllv4_mult_table[] = {33, 27, 22, 20, 17, 16};
46 #define to_clk_pllv4(__hw) container_of(__hw, struct clk_pllv4, hw)
48 #define LOCK_TIMEOUT_US USEC_PER_MSEC
50 static inline int clk_pllv4_wait_lock(struct clk_pllv4 *pll)
54 return readl_poll_timeout(pll->base + PLL_CSR_OFFSET,
55 csr, csr & PLL_VLD, 0, LOCK_TIMEOUT_US);
58 static int clk_pllv4_is_prepared(struct clk_hw *hw)
60 struct clk_pllv4 *pll = to_clk_pllv4(hw);
62 if (readl_relaxed(pll->base) & PLL_EN)
68 static unsigned long clk_pllv4_recalc_rate(struct clk_hw *hw,
69 unsigned long parent_rate)
71 struct clk_pllv4 *pll = to_clk_pllv4(hw);
75 mult = readl_relaxed(pll->base + PLL_CFG_OFFSET);
79 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
80 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
85 return (parent_rate * mult) + (u32)temp64;
88 static long clk_pllv4_round_rate(struct clk_hw *hw, unsigned long rate,
91 unsigned long parent_rate = *prate;
92 unsigned long round_rate, i;
93 u32 mfn, mfd = DEFAULT_MFD;
97 for (i = 0; i < ARRAY_SIZE(pllv4_mult_table); i++) {
98 round_rate = parent_rate * pllv4_mult_table[i];
99 if (rate >= round_rate) {
106 pr_warn("%s: unable to round rate %lu, parent rate %lu\n",
107 clk_hw_get_name(hw), rate, parent_rate);
111 if (parent_rate <= MAX_MFD)
114 temp64 = (u64)(rate - round_rate);
116 do_div(temp64, parent_rate);
120 * NOTE: The value of numerator must always be configured to be
121 * less than the value of the denominator. If we can't get a proper
122 * pair of mfn/mfd, we simply return the round_rate without using
128 temp64 = (u64)parent_rate;
132 return round_rate + (u32)temp64;
135 static bool clk_pllv4_is_valid_mult(unsigned int mult)
139 /* check if mult is in valid MULT table */
140 for (i = 0; i < ARRAY_SIZE(pllv4_mult_table); i++) {
141 if (pllv4_mult_table[i] == mult)
148 static int clk_pllv4_set_rate(struct clk_hw *hw, unsigned long rate,
149 unsigned long parent_rate)
151 struct clk_pllv4 *pll = to_clk_pllv4(hw);
152 u32 val, mult, mfn, mfd = DEFAULT_MFD;
155 mult = rate / parent_rate;
157 if (!clk_pllv4_is_valid_mult(mult))
160 if (parent_rate <= MAX_MFD)
163 temp64 = (u64)(rate - mult * parent_rate);
165 do_div(temp64, parent_rate);
168 val = readl_relaxed(pll->base + PLL_CFG_OFFSET);
170 val |= mult << BP_PLL_MULT;
171 writel_relaxed(val, pll->base + PLL_CFG_OFFSET);
173 writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
174 writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
179 static int clk_pllv4_prepare(struct clk_hw *hw)
182 struct clk_pllv4 *pll = to_clk_pllv4(hw);
184 val = readl_relaxed(pll->base);
186 writel_relaxed(val, pll->base);
188 return clk_pllv4_wait_lock(pll);
191 static void clk_pllv4_unprepare(struct clk_hw *hw)
194 struct clk_pllv4 *pll = to_clk_pllv4(hw);
196 val = readl_relaxed(pll->base);
198 writel_relaxed(val, pll->base);
201 static const struct clk_ops clk_pllv4_ops = {
202 .recalc_rate = clk_pllv4_recalc_rate,
203 .round_rate = clk_pllv4_round_rate,
204 .set_rate = clk_pllv4_set_rate,
205 .prepare = clk_pllv4_prepare,
206 .unprepare = clk_pllv4_unprepare,
207 .is_prepared = clk_pllv4_is_prepared,
210 struct clk_hw *imx_clk_hw_pllv4(const char *name, const char *parent_name,
213 struct clk_pllv4 *pll;
215 struct clk_init_data init;
218 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
220 return ERR_PTR(-ENOMEM);
225 init.ops = &clk_pllv4_ops;
226 init.parent_names = &parent_name;
227 init.num_parents = 1;
228 init.flags = CLK_SET_RATE_GATE;
230 pll->hw.init = &init;
233 ret = clk_hw_register(NULL, hw);