x86, efi: Set runtime_version to the EFI spec revision
[linux/fpc-iii.git] / arch / arm / mach-imx / clk-pllv3.c
blobd09bc3df9a7a69bc7a4d63c7abeb7b078077a1f0
1 /*
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/io.h>
16 #include <linux/slab.h>
17 #include <linux/jiffies.h>
18 #include <linux/err.h>
19 #include "clk.h"
21 #define PLL_NUM_OFFSET 0x10
22 #define PLL_DENOM_OFFSET 0x20
24 #define BM_PLL_POWER (0x1 << 12)
25 #define BM_PLL_ENABLE (0x1 << 13)
26 #define BM_PLL_BYPASS (0x1 << 16)
27 #define BM_PLL_LOCK (0x1 << 31)
29 /**
30 * struct clk_pllv3 - IMX PLL clock version 3
31 * @clk_hw: clock source
32 * @base: base address of PLL registers
33 * @powerup_set: set POWER bit to power up the PLL
34 * @div_mask: mask of divider bits
36 * IMX PLL clock version 3, found on i.MX6 series. Divider for pllv3
37 * is actually a multiplier, and always sits at bit 0.
39 struct clk_pllv3 {
40 struct clk_hw hw;
41 void __iomem *base;
42 bool powerup_set;
43 u32 div_mask;
46 #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
48 static int clk_pllv3_prepare(struct clk_hw *hw)
50 struct clk_pllv3 *pll = to_clk_pllv3(hw);
51 unsigned long timeout = jiffies + msecs_to_jiffies(10);
52 u32 val;
54 val = readl_relaxed(pll->base);
55 val &= ~BM_PLL_BYPASS;
56 if (pll->powerup_set)
57 val |= BM_PLL_POWER;
58 else
59 val &= ~BM_PLL_POWER;
60 writel_relaxed(val, pll->base);
62 /* Wait for PLL to lock */
63 while (!(readl_relaxed(pll->base) & BM_PLL_LOCK))
64 if (time_after(jiffies, timeout))
65 return -ETIMEDOUT;
67 return 0;
70 static void clk_pllv3_unprepare(struct clk_hw *hw)
72 struct clk_pllv3 *pll = to_clk_pllv3(hw);
73 u32 val;
75 val = readl_relaxed(pll->base);
76 val |= BM_PLL_BYPASS;
77 if (pll->powerup_set)
78 val &= ~BM_PLL_POWER;
79 else
80 val |= BM_PLL_POWER;
81 writel_relaxed(val, pll->base);
84 static int clk_pllv3_enable(struct clk_hw *hw)
86 struct clk_pllv3 *pll = to_clk_pllv3(hw);
87 u32 val;
89 val = readl_relaxed(pll->base);
90 val |= BM_PLL_ENABLE;
91 writel_relaxed(val, pll->base);
93 return 0;
96 static void clk_pllv3_disable(struct clk_hw *hw)
98 struct clk_pllv3 *pll = to_clk_pllv3(hw);
99 u32 val;
101 val = readl_relaxed(pll->base);
102 val &= ~BM_PLL_ENABLE;
103 writel_relaxed(val, pll->base);
106 static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
107 unsigned long parent_rate)
109 struct clk_pllv3 *pll = to_clk_pllv3(hw);
110 u32 div = readl_relaxed(pll->base) & pll->div_mask;
112 return (div == 1) ? parent_rate * 22 : parent_rate * 20;
115 static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
116 unsigned long *prate)
118 unsigned long parent_rate = *prate;
120 return (rate >= parent_rate * 22) ? parent_rate * 22 :
121 parent_rate * 20;
124 static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
125 unsigned long parent_rate)
127 struct clk_pllv3 *pll = to_clk_pllv3(hw);
128 u32 val, div;
130 if (rate == parent_rate * 22)
131 div = 1;
132 else if (rate == parent_rate * 20)
133 div = 0;
134 else
135 return -EINVAL;
137 val = readl_relaxed(pll->base);
138 val &= ~pll->div_mask;
139 val |= div;
140 writel_relaxed(val, pll->base);
142 return 0;
145 static const struct clk_ops clk_pllv3_ops = {
146 .prepare = clk_pllv3_prepare,
147 .unprepare = clk_pllv3_unprepare,
148 .enable = clk_pllv3_enable,
149 .disable = clk_pllv3_disable,
150 .recalc_rate = clk_pllv3_recalc_rate,
151 .round_rate = clk_pllv3_round_rate,
152 .set_rate = clk_pllv3_set_rate,
155 static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
156 unsigned long parent_rate)
158 struct clk_pllv3 *pll = to_clk_pllv3(hw);
159 u32 div = readl_relaxed(pll->base) & pll->div_mask;
161 return parent_rate * div / 2;
164 static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
165 unsigned long *prate)
167 unsigned long parent_rate = *prate;
168 unsigned long min_rate = parent_rate * 54 / 2;
169 unsigned long max_rate = parent_rate * 108 / 2;
170 u32 div;
172 if (rate > max_rate)
173 rate = max_rate;
174 else if (rate < min_rate)
175 rate = min_rate;
176 div = rate * 2 / parent_rate;
178 return parent_rate * div / 2;
181 static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
182 unsigned long parent_rate)
184 struct clk_pllv3 *pll = to_clk_pllv3(hw);
185 unsigned long min_rate = parent_rate * 54 / 2;
186 unsigned long max_rate = parent_rate * 108 / 2;
187 u32 val, div;
189 if (rate < min_rate || rate > max_rate)
190 return -EINVAL;
192 div = rate * 2 / parent_rate;
193 val = readl_relaxed(pll->base);
194 val &= ~pll->div_mask;
195 val |= div;
196 writel_relaxed(val, pll->base);
198 return 0;
201 static const struct clk_ops clk_pllv3_sys_ops = {
202 .prepare = clk_pllv3_prepare,
203 .unprepare = clk_pllv3_unprepare,
204 .enable = clk_pllv3_enable,
205 .disable = clk_pllv3_disable,
206 .recalc_rate = clk_pllv3_sys_recalc_rate,
207 .round_rate = clk_pllv3_sys_round_rate,
208 .set_rate = clk_pllv3_sys_set_rate,
211 static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
212 unsigned long parent_rate)
214 struct clk_pllv3 *pll = to_clk_pllv3(hw);
215 u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
216 u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
217 u32 div = readl_relaxed(pll->base) & pll->div_mask;
219 return (parent_rate * div) + ((parent_rate / mfd) * mfn);
222 static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
223 unsigned long *prate)
225 unsigned long parent_rate = *prate;
226 unsigned long min_rate = parent_rate * 27;
227 unsigned long max_rate = parent_rate * 54;
228 u32 div;
229 u32 mfn, mfd = 1000000;
230 s64 temp64;
232 if (rate > max_rate)
233 rate = max_rate;
234 else if (rate < min_rate)
235 rate = min_rate;
237 div = rate / parent_rate;
238 temp64 = (u64) (rate - div * parent_rate);
239 temp64 *= mfd;
240 do_div(temp64, parent_rate);
241 mfn = temp64;
243 return parent_rate * div + parent_rate / mfd * mfn;
246 static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
247 unsigned long parent_rate)
249 struct clk_pllv3 *pll = to_clk_pllv3(hw);
250 unsigned long min_rate = parent_rate * 27;
251 unsigned long max_rate = parent_rate * 54;
252 u32 val, div;
253 u32 mfn, mfd = 1000000;
254 s64 temp64;
256 if (rate < min_rate || rate > max_rate)
257 return -EINVAL;
259 div = rate / parent_rate;
260 temp64 = (u64) (rate - div * parent_rate);
261 temp64 *= mfd;
262 do_div(temp64, parent_rate);
263 mfn = temp64;
265 val = readl_relaxed(pll->base);
266 val &= ~pll->div_mask;
267 val |= div;
268 writel_relaxed(val, pll->base);
269 writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
270 writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
272 return 0;
275 static const struct clk_ops clk_pllv3_av_ops = {
276 .prepare = clk_pllv3_prepare,
277 .unprepare = clk_pllv3_unprepare,
278 .enable = clk_pllv3_enable,
279 .disable = clk_pllv3_disable,
280 .recalc_rate = clk_pllv3_av_recalc_rate,
281 .round_rate = clk_pllv3_av_round_rate,
282 .set_rate = clk_pllv3_av_set_rate,
285 static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
286 unsigned long parent_rate)
288 return 500000000;
291 static const struct clk_ops clk_pllv3_enet_ops = {
292 .prepare = clk_pllv3_prepare,
293 .unprepare = clk_pllv3_unprepare,
294 .enable = clk_pllv3_enable,
295 .disable = clk_pllv3_disable,
296 .recalc_rate = clk_pllv3_enet_recalc_rate,
299 static const struct clk_ops clk_pllv3_mlb_ops = {
300 .prepare = clk_pllv3_prepare,
301 .unprepare = clk_pllv3_unprepare,
302 .enable = clk_pllv3_enable,
303 .disable = clk_pllv3_disable,
306 struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
307 const char *parent_name, void __iomem *base,
308 u32 div_mask)
310 struct clk_pllv3 *pll;
311 const struct clk_ops *ops;
312 struct clk *clk;
313 struct clk_init_data init;
315 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
316 if (!pll)
317 return ERR_PTR(-ENOMEM);
319 switch (type) {
320 case IMX_PLLV3_SYS:
321 ops = &clk_pllv3_sys_ops;
322 break;
323 case IMX_PLLV3_USB:
324 ops = &clk_pllv3_ops;
325 pll->powerup_set = true;
326 break;
327 case IMX_PLLV3_AV:
328 ops = &clk_pllv3_av_ops;
329 break;
330 case IMX_PLLV3_ENET:
331 ops = &clk_pllv3_enet_ops;
332 break;
333 case IMX_PLLV3_MLB:
334 ops = &clk_pllv3_mlb_ops;
335 break;
336 default:
337 ops = &clk_pllv3_ops;
339 pll->base = base;
340 pll->div_mask = div_mask;
342 init.name = name;
343 init.ops = ops;
344 init.flags = 0;
345 init.parent_names = &parent_name;
346 init.num_parents = 1;
348 pll->hw.init = &init;
350 clk = clk_register(NULL, &pll->hw);
351 if (IS_ERR(clk))
352 kfree(pll);
354 return clk;