2 * Copyright 2012 Freescale Semiconductor, Inc.
3 * Copyright 2012 Linaro Ltd.
5 * The code contained herein is licensed under the GNU General Public
6 * License. You may obtain a copy of the GNU General Public License
7 * Version 2 or later at the following locations:
9 * http://www.opensource.org/licenses/gpl-license.html
10 * http://www.gnu.org/copyleft/gpl.html
13 #include <linux/clk-provider.h>
14 #include <linux/delay.h>
16 #include <linux/slab.h>
17 #include <linux/jiffies.h>
18 #include <linux/err.h>
21 #define PLL_NUM_OFFSET 0x10
22 #define PLL_DENOM_OFFSET 0x20
24 #define BM_PLL_POWER (0x1 << 12)
25 #define BM_PLL_LOCK (0x1 << 31)
26 #define IMX7_ENET_PLL_POWER (0x1 << 5)
29 * struct clk_pllv3 - IMX PLL clock version 3
30 * @clk_hw: clock source
31 * @base: base address of PLL registers
32 * @powerup_set: set POWER bit to power up the PLL
33 * @powerdown: pll powerdown offset bit
34 * @div_mask: mask of divider bits
35 * @div_shift: shift of divider bits
37 * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
38 * is actually a multiplier, and always sits at bit 0.
49 #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
51 static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
53 unsigned long timeout = jiffies + msecs_to_jiffies(10);
54 u32 val = readl_relaxed(pll->base) & pll->powerdown;
56 /* No need to wait for lock when pll is not powered up */
57 if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
60 /* Wait for PLL to lock */
62 if (readl_relaxed(pll->base) & BM_PLL_LOCK)
64 if (time_after(jiffies, timeout))
66 usleep_range(50, 500);
69 return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
72 static int clk_pllv3_prepare(struct clk_hw *hw)
74 struct clk_pllv3 *pll = to_clk_pllv3(hw);
77 val = readl_relaxed(pll->base);
82 writel_relaxed(val, pll->base);
84 return clk_pllv3_wait_lock(pll);
87 static void clk_pllv3_unprepare(struct clk_hw *hw)
89 struct clk_pllv3 *pll = to_clk_pllv3(hw);
92 val = readl_relaxed(pll->base);
97 writel_relaxed(val, pll->base);
100 static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
101 unsigned long parent_rate)
103 struct clk_pllv3 *pll = to_clk_pllv3(hw);
104 u32 div = (readl_relaxed(pll->base) >> pll->div_shift) & pll->div_mask;
106 return (div == 1) ? parent_rate * 22 : parent_rate * 20;
109 static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
110 unsigned long *prate)
112 unsigned long parent_rate = *prate;
114 return (rate >= parent_rate * 22) ? parent_rate * 22 :
118 static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
119 unsigned long parent_rate)
121 struct clk_pllv3 *pll = to_clk_pllv3(hw);
124 if (rate == parent_rate * 22)
126 else if (rate == parent_rate * 20)
131 val = readl_relaxed(pll->base);
132 val &= ~(pll->div_mask << pll->div_shift);
133 val |= (div << pll->div_shift);
134 writel_relaxed(val, pll->base);
136 return clk_pllv3_wait_lock(pll);
139 static const struct clk_ops clk_pllv3_ops = {
140 .prepare = clk_pllv3_prepare,
141 .unprepare = clk_pllv3_unprepare,
142 .recalc_rate = clk_pllv3_recalc_rate,
143 .round_rate = clk_pllv3_round_rate,
144 .set_rate = clk_pllv3_set_rate,
147 static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
148 unsigned long parent_rate)
150 struct clk_pllv3 *pll = to_clk_pllv3(hw);
151 u32 div = readl_relaxed(pll->base) & pll->div_mask;
153 return parent_rate * div / 2;
156 static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
157 unsigned long *prate)
159 unsigned long parent_rate = *prate;
160 unsigned long min_rate = parent_rate * 54 / 2;
161 unsigned long max_rate = parent_rate * 108 / 2;
166 else if (rate < min_rate)
168 div = rate * 2 / parent_rate;
170 return parent_rate * div / 2;
173 static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
174 unsigned long parent_rate)
176 struct clk_pllv3 *pll = to_clk_pllv3(hw);
177 unsigned long min_rate = parent_rate * 54 / 2;
178 unsigned long max_rate = parent_rate * 108 / 2;
181 if (rate < min_rate || rate > max_rate)
184 div = rate * 2 / parent_rate;
185 val = readl_relaxed(pll->base);
186 val &= ~pll->div_mask;
188 writel_relaxed(val, pll->base);
190 return clk_pllv3_wait_lock(pll);
193 static const struct clk_ops clk_pllv3_sys_ops = {
194 .prepare = clk_pllv3_prepare,
195 .unprepare = clk_pllv3_unprepare,
196 .recalc_rate = clk_pllv3_sys_recalc_rate,
197 .round_rate = clk_pllv3_sys_round_rate,
198 .set_rate = clk_pllv3_sys_set_rate,
201 static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
202 unsigned long parent_rate)
204 struct clk_pllv3 *pll = to_clk_pllv3(hw);
205 u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
206 u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
207 u32 div = readl_relaxed(pll->base) & pll->div_mask;
209 return (parent_rate * div) + ((parent_rate / mfd) * mfn);
212 static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
213 unsigned long *prate)
215 unsigned long parent_rate = *prate;
216 unsigned long min_rate = parent_rate * 27;
217 unsigned long max_rate = parent_rate * 54;
219 u32 mfn, mfd = 1000000;
224 else if (rate < min_rate)
227 div = rate / parent_rate;
228 temp64 = (u64) (rate - div * parent_rate);
230 do_div(temp64, parent_rate);
233 return parent_rate * div + parent_rate / mfd * mfn;
236 static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
237 unsigned long parent_rate)
239 struct clk_pllv3 *pll = to_clk_pllv3(hw);
240 unsigned long min_rate = parent_rate * 27;
241 unsigned long max_rate = parent_rate * 54;
243 u32 mfn, mfd = 1000000;
246 if (rate < min_rate || rate > max_rate)
249 div = rate / parent_rate;
250 temp64 = (u64) (rate - div * parent_rate);
252 do_div(temp64, parent_rate);
255 val = readl_relaxed(pll->base);
256 val &= ~pll->div_mask;
258 writel_relaxed(val, pll->base);
259 writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
260 writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
262 return clk_pllv3_wait_lock(pll);
265 static const struct clk_ops clk_pllv3_av_ops = {
266 .prepare = clk_pllv3_prepare,
267 .unprepare = clk_pllv3_unprepare,
268 .recalc_rate = clk_pllv3_av_recalc_rate,
269 .round_rate = clk_pllv3_av_round_rate,
270 .set_rate = clk_pllv3_av_set_rate,
273 static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
274 unsigned long parent_rate)
279 static const struct clk_ops clk_pllv3_enet_ops = {
280 .prepare = clk_pllv3_prepare,
281 .unprepare = clk_pllv3_unprepare,
282 .recalc_rate = clk_pllv3_enet_recalc_rate,
285 struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
286 const char *parent_name, void __iomem *base,
289 struct clk_pllv3 *pll;
290 const struct clk_ops *ops;
292 struct clk_init_data init;
294 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
296 return ERR_PTR(-ENOMEM);
298 pll->powerdown = BM_PLL_POWER;
302 ops = &clk_pllv3_sys_ops;
304 case IMX_PLLV3_USB_VF610:
307 ops = &clk_pllv3_ops;
308 pll->powerup_set = true;
311 ops = &clk_pllv3_av_ops;
313 case IMX_PLLV3_ENET_IMX7:
314 pll->powerdown = IMX7_ENET_PLL_POWER;
316 ops = &clk_pllv3_enet_ops;
319 ops = &clk_pllv3_ops;
322 pll->div_mask = div_mask;
327 init.parent_names = &parent_name;
328 init.num_parents = 1;
330 pll->hw.init = &init;
332 clk = clk_register(NULL, &pll->hw);