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)
31 * struct clk_pllv3 - IMX PLL clock version 3
32 * @clk_hw: clock source
33 * @base: base address of PLL registers
34 * @powerup_set: set POWER bit to power up the PLL
35 * @div_mask: mask 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.
47 #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
49 static int clk_pllv3_prepare(struct clk_hw *hw)
51 struct clk_pllv3 *pll = to_clk_pllv3(hw);
52 unsigned long timeout;
55 val = readl_relaxed(pll->base);
56 val &= ~BM_PLL_BYPASS;
61 writel_relaxed(val, pll->base);
63 timeout = jiffies + msecs_to_jiffies(10);
64 /* Wait for PLL to lock */
66 if (readl_relaxed(pll->base) & BM_PLL_LOCK)
68 if (time_after(jiffies, timeout))
70 usleep_range(50, 500);
73 if (readl_relaxed(pll->base) & BM_PLL_LOCK)
79 static void clk_pllv3_unprepare(struct clk_hw *hw)
81 struct clk_pllv3 *pll = to_clk_pllv3(hw);
84 val = readl_relaxed(pll->base);
90 writel_relaxed(val, pll->base);
93 static int clk_pllv3_enable(struct clk_hw *hw)
95 struct clk_pllv3 *pll = to_clk_pllv3(hw);
98 val = readl_relaxed(pll->base);
100 writel_relaxed(val, pll->base);
105 static void clk_pllv3_disable(struct clk_hw *hw)
107 struct clk_pllv3 *pll = to_clk_pllv3(hw);
110 val = readl_relaxed(pll->base);
111 val &= ~BM_PLL_ENABLE;
112 writel_relaxed(val, pll->base);
115 static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
116 unsigned long parent_rate)
118 struct clk_pllv3 *pll = to_clk_pllv3(hw);
119 u32 div = readl_relaxed(pll->base) & pll->div_mask;
121 return (div == 1) ? parent_rate * 22 : parent_rate * 20;
124 static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
125 unsigned long *prate)
127 unsigned long parent_rate = *prate;
129 return (rate >= parent_rate * 22) ? parent_rate * 22 :
133 static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
134 unsigned long parent_rate)
136 struct clk_pllv3 *pll = to_clk_pllv3(hw);
139 if (rate == parent_rate * 22)
141 else if (rate == parent_rate * 20)
146 val = readl_relaxed(pll->base);
147 val &= ~pll->div_mask;
149 writel_relaxed(val, pll->base);
154 static const struct clk_ops clk_pllv3_ops = {
155 .prepare = clk_pllv3_prepare,
156 .unprepare = clk_pllv3_unprepare,
157 .enable = clk_pllv3_enable,
158 .disable = clk_pllv3_disable,
159 .recalc_rate = clk_pllv3_recalc_rate,
160 .round_rate = clk_pllv3_round_rate,
161 .set_rate = clk_pllv3_set_rate,
164 static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
165 unsigned long parent_rate)
167 struct clk_pllv3 *pll = to_clk_pllv3(hw);
168 u32 div = readl_relaxed(pll->base) & pll->div_mask;
170 return parent_rate * div / 2;
173 static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
174 unsigned long *prate)
176 unsigned long parent_rate = *prate;
177 unsigned long min_rate = parent_rate * 54 / 2;
178 unsigned long max_rate = parent_rate * 108 / 2;
183 else if (rate < min_rate)
185 div = rate * 2 / parent_rate;
187 return parent_rate * div / 2;
190 static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
191 unsigned long parent_rate)
193 struct clk_pllv3 *pll = to_clk_pllv3(hw);
194 unsigned long min_rate = parent_rate * 54 / 2;
195 unsigned long max_rate = parent_rate * 108 / 2;
198 if (rate < min_rate || rate > max_rate)
201 div = rate * 2 / parent_rate;
202 val = readl_relaxed(pll->base);
203 val &= ~pll->div_mask;
205 writel_relaxed(val, pll->base);
210 static const struct clk_ops clk_pllv3_sys_ops = {
211 .prepare = clk_pllv3_prepare,
212 .unprepare = clk_pllv3_unprepare,
213 .enable = clk_pllv3_enable,
214 .disable = clk_pllv3_disable,
215 .recalc_rate = clk_pllv3_sys_recalc_rate,
216 .round_rate = clk_pllv3_sys_round_rate,
217 .set_rate = clk_pllv3_sys_set_rate,
220 static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
221 unsigned long parent_rate)
223 struct clk_pllv3 *pll = to_clk_pllv3(hw);
224 u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
225 u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
226 u32 div = readl_relaxed(pll->base) & pll->div_mask;
228 return (parent_rate * div) + ((parent_rate / mfd) * mfn);
231 static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
232 unsigned long *prate)
234 unsigned long parent_rate = *prate;
235 unsigned long min_rate = parent_rate * 27;
236 unsigned long max_rate = parent_rate * 54;
238 u32 mfn, mfd = 1000000;
243 else if (rate < min_rate)
246 div = rate / parent_rate;
247 temp64 = (u64) (rate - div * parent_rate);
249 do_div(temp64, parent_rate);
252 return parent_rate * div + parent_rate / mfd * mfn;
255 static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
256 unsigned long parent_rate)
258 struct clk_pllv3 *pll = to_clk_pllv3(hw);
259 unsigned long min_rate = parent_rate * 27;
260 unsigned long max_rate = parent_rate * 54;
262 u32 mfn, mfd = 1000000;
265 if (rate < min_rate || rate > max_rate)
268 div = rate / parent_rate;
269 temp64 = (u64) (rate - div * parent_rate);
271 do_div(temp64, parent_rate);
274 val = readl_relaxed(pll->base);
275 val &= ~pll->div_mask;
277 writel_relaxed(val, pll->base);
278 writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
279 writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
284 static const struct clk_ops clk_pllv3_av_ops = {
285 .prepare = clk_pllv3_prepare,
286 .unprepare = clk_pllv3_unprepare,
287 .enable = clk_pllv3_enable,
288 .disable = clk_pllv3_disable,
289 .recalc_rate = clk_pllv3_av_recalc_rate,
290 .round_rate = clk_pllv3_av_round_rate,
291 .set_rate = clk_pllv3_av_set_rate,
294 static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
295 unsigned long parent_rate)
300 static const struct clk_ops clk_pllv3_enet_ops = {
301 .prepare = clk_pllv3_prepare,
302 .unprepare = clk_pllv3_unprepare,
303 .enable = clk_pllv3_enable,
304 .disable = clk_pllv3_disable,
305 .recalc_rate = clk_pllv3_enet_recalc_rate,
308 struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
309 const char *parent_name, void __iomem *base,
312 struct clk_pllv3 *pll;
313 const struct clk_ops *ops;
315 struct clk_init_data init;
317 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
319 return ERR_PTR(-ENOMEM);
323 ops = &clk_pllv3_sys_ops;
326 ops = &clk_pllv3_ops;
327 pll->powerup_set = true;
330 ops = &clk_pllv3_av_ops;
333 ops = &clk_pllv3_enet_ops;
336 ops = &clk_pllv3_ops;
339 pll->div_mask = div_mask;
344 init.parent_names = &parent_name;
345 init.num_parents = 1;
347 pll->hw.init = &init;
349 clk = clk_register(NULL, &pll->hw);