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.h>
14 #include <linux/clk-provider.h>
15 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/jiffies.h>
19 #include <linux/err.h>
22 #define PLL_NUM_OFFSET 0x10
23 #define PLL_DENOM_OFFSET 0x20
25 #define BM_PLL_POWER (0x1 << 12)
26 #define BM_PLL_ENABLE (0x1 << 13)
27 #define BM_PLL_BYPASS (0x1 << 16)
28 #define BM_PLL_LOCK (0x1 << 31)
29 #define BYPASS_RATE 24000000
30 #define BYPASS_MASK 0x10000
33 * struct clk_pllv3 - IMX PLL clock version 3
34 * @clk_hw: clock source
35 * @base: base address of PLL registers
36 * @powerup_set: set POWER bit to power up the PLL
37 * @always_on : Leave the PLL powered up all the time.
38 * @div_mask: mask of divider bits
40 * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
41 * is actually a multiplier, and always sits at bit 0.
51 #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
53 static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
55 unsigned long timeout = jiffies + msecs_to_jiffies(10);
56 u32 val = readl_relaxed(pll->base) & BM_PLL_POWER;
58 /* No need to wait for lock when pll is not powered up */
59 if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
62 /* Wait for PLL to lock */
64 if (readl_relaxed(pll->base) & BM_PLL_LOCK)
66 if (time_after(jiffies, timeout))
68 usleep_range(50, 500);
71 return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
74 static int clk_pllv3_prepare(struct clk_hw *hw)
76 struct clk_pllv3 *pll = to_clk_pllv3(hw);
80 val = readl_relaxed(pll->base);
85 writel_relaxed(val, pll->base);
87 ret = clk_pllv3_wait_lock(pll);
91 val = readl_relaxed(pll->base);
92 val &= ~BM_PLL_BYPASS;
93 writel_relaxed(val, pll->base);
98 static void clk_pllv3_unprepare(struct clk_hw *hw)
100 struct clk_pllv3 *pll = to_clk_pllv3(hw);
103 val = readl_relaxed(pll->base);
104 val |= BM_PLL_BYPASS;
105 if (pll->powerup_set)
106 val &= ~BM_PLL_POWER;
109 writel_relaxed(val, pll->base);
112 static int clk_pllv3_enable(struct clk_hw *hw)
114 struct clk_pllv3 *pll = to_clk_pllv3(hw);
117 val = readl_relaxed(pll->base);
118 val |= BM_PLL_ENABLE;
119 writel_relaxed(val, pll->base);
124 static void clk_pllv3_disable(struct clk_hw *hw)
126 struct clk_pllv3 *pll = to_clk_pllv3(hw);
129 val = readl_relaxed(pll->base);
131 val &= ~BM_PLL_ENABLE;
132 writel_relaxed(val, pll->base);
135 static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
136 unsigned long parent_rate)
138 struct clk_pllv3 *pll = to_clk_pllv3(hw);
139 u32 div = readl_relaxed(pll->base) & pll->div_mask;
140 u32 bypass = readl_relaxed(pll->base) & BYPASS_MASK;
146 rate = (div == 1) ? parent_rate * 22 : parent_rate * 20;
151 static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
152 unsigned long *prate)
154 unsigned long parent_rate = *prate;
155 struct clk_pllv3 *pll = to_clk_pllv3(hw);
156 u32 bypass = readl_relaxed(pll->base) & BYPASS_MASK;
158 /* If the PLL is bypassed, its rate is 24MHz. */
162 return (rate >= parent_rate * 22) ? parent_rate * 22 :
166 static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
167 unsigned long parent_rate)
169 struct clk_pllv3 *pll = to_clk_pllv3(hw);
171 u32 bypass = readl_relaxed(pll->base) & BYPASS_MASK;
173 /* If the PLL is bypassed, its rate is 24MHz. */
177 if (rate == parent_rate * 22)
179 else if (rate == parent_rate * 20)
184 val = readl_relaxed(pll->base);
185 val &= ~pll->div_mask;
187 writel_relaxed(val, pll->base);
189 return clk_pllv3_wait_lock(pll);
192 static const struct clk_ops clk_pllv3_ops = {
193 .prepare = clk_pllv3_prepare,
194 .unprepare = clk_pllv3_unprepare,
195 .enable = clk_pllv3_enable,
196 .disable = clk_pllv3_disable,
197 .recalc_rate = clk_pllv3_recalc_rate,
198 .round_rate = clk_pllv3_round_rate,
199 .set_rate = clk_pllv3_set_rate,
202 static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
203 unsigned long parent_rate)
205 struct clk_pllv3 *pll = to_clk_pllv3(hw);
206 u32 div = readl_relaxed(pll->base) & pll->div_mask;
208 return parent_rate * div / 2;
211 static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
212 unsigned long *prate)
214 unsigned long parent_rate = *prate;
215 unsigned long min_rate = parent_rate * 54 / 2;
216 unsigned long max_rate = parent_rate * 108 / 2;
221 else if (rate < min_rate)
223 div = rate * 2 / parent_rate;
225 return parent_rate * div / 2;
228 static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
229 unsigned long parent_rate)
231 struct clk_pllv3 *pll = to_clk_pllv3(hw);
232 unsigned long min_rate = parent_rate * 54 / 2;
233 unsigned long max_rate = parent_rate * 108 / 2;
236 if (rate < min_rate || rate > max_rate)
239 div = rate * 2 / parent_rate;
240 val = readl_relaxed(pll->base);
241 val &= ~pll->div_mask;
243 writel_relaxed(val, pll->base);
245 return clk_pllv3_wait_lock(pll);
248 static const struct clk_ops clk_pllv3_sys_ops = {
249 .prepare = clk_pllv3_prepare,
250 .unprepare = clk_pllv3_unprepare,
251 .enable = clk_pllv3_enable,
252 .disable = clk_pllv3_disable,
253 .recalc_rate = clk_pllv3_sys_recalc_rate,
254 .round_rate = clk_pllv3_sys_round_rate,
255 .set_rate = clk_pllv3_sys_set_rate,
258 static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
259 unsigned long parent_rate)
261 struct clk_pllv3 *pll = to_clk_pllv3(hw);
262 u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
263 u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
264 u32 div = readl_relaxed(pll->base) & pll->div_mask;
266 return (parent_rate * div) + ((parent_rate / mfd) * mfn);
269 static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
270 unsigned long *prate)
272 unsigned long parent_rate = *prate;
273 unsigned long min_rate = parent_rate * 27;
274 unsigned long max_rate = parent_rate * 54;
276 u32 mfn, mfd = 1000000;
281 else if (rate < min_rate)
284 div = rate / parent_rate;
285 temp64 = (u64) (rate - div * parent_rate);
287 do_div(temp64, parent_rate);
290 return parent_rate * div + parent_rate / mfd * mfn;
293 static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
294 unsigned long parent_rate)
296 struct clk_pllv3 *pll = to_clk_pllv3(hw);
297 unsigned long min_rate = parent_rate * 27;
298 unsigned long max_rate = parent_rate * 54;
300 u32 mfn, mfd = 1000000;
303 if (rate < min_rate || rate > max_rate)
306 div = rate / parent_rate;
307 temp64 = (u64) (rate - div * parent_rate);
309 do_div(temp64, parent_rate);
312 val = readl_relaxed(pll->base);
313 val &= ~pll->div_mask;
315 writel_relaxed(val, pll->base);
316 writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
317 writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
319 return clk_pllv3_wait_lock(pll);
322 static const struct clk_ops clk_pllv3_av_ops = {
323 .prepare = clk_pllv3_prepare,
324 .unprepare = clk_pllv3_unprepare,
325 .enable = clk_pllv3_enable,
326 .disable = clk_pllv3_disable,
327 .recalc_rate = clk_pllv3_av_recalc_rate,
328 .round_rate = clk_pllv3_av_round_rate,
329 .set_rate = clk_pllv3_av_set_rate,
332 static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
333 unsigned long parent_rate)
338 static const struct clk_ops clk_pllv3_enet_ops = {
339 .prepare = clk_pllv3_prepare,
340 .unprepare = clk_pllv3_unprepare,
341 .enable = clk_pllv3_enable,
342 .disable = clk_pllv3_disable,
343 .recalc_rate = clk_pllv3_enet_recalc_rate,
346 struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
347 const char *parent_name, void __iomem *base,
348 u32 div_mask, bool always_on)
350 struct clk_pllv3 *pll;
351 const struct clk_ops *ops;
353 struct clk_init_data init;
355 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
357 return ERR_PTR(-ENOMEM);
361 ops = &clk_pllv3_sys_ops;
364 ops = &clk_pllv3_ops;
365 pll->powerup_set = true;
368 ops = &clk_pllv3_av_ops;
371 ops = &clk_pllv3_enet_ops;
374 ops = &clk_pllv3_ops;
377 pll->div_mask = div_mask;
378 pll->always_on = always_on;
383 init.parent_names = &parent_name;
384 init.num_parents = 1;
386 pll->hw.init = &init;
388 clk = clk_register(NULL, &pll->hw);