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_wait_lock(struct clk_pllv3
*pll
)
51 unsigned long timeout
= jiffies
+ msecs_to_jiffies(10);
52 u32 val
= readl_relaxed(pll
->base
) & BM_PLL_POWER
;
54 /* No need to wait for lock when pll is not powered up */
55 if ((pll
->powerup_set
&& !val
) || (!pll
->powerup_set
&& val
))
58 /* Wait for PLL to lock */
60 if (readl_relaxed(pll
->base
) & BM_PLL_LOCK
)
62 if (time_after(jiffies
, timeout
))
64 usleep_range(50, 500);
67 return readl_relaxed(pll
->base
) & BM_PLL_LOCK
? 0 : -ETIMEDOUT
;
70 static int clk_pllv3_prepare(struct clk_hw
*hw
)
72 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
76 val
= readl_relaxed(pll
->base
);
81 writel_relaxed(val
, pll
->base
);
83 ret
= clk_pllv3_wait_lock(pll
);
87 val
= readl_relaxed(pll
->base
);
88 val
&= ~BM_PLL_BYPASS
;
89 writel_relaxed(val
, pll
->base
);
94 static void clk_pllv3_unprepare(struct clk_hw
*hw
)
96 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
99 val
= readl_relaxed(pll
->base
);
100 val
|= BM_PLL_BYPASS
;
101 if (pll
->powerup_set
)
102 val
&= ~BM_PLL_POWER
;
105 writel_relaxed(val
, pll
->base
);
108 static int clk_pllv3_enable(struct clk_hw
*hw
)
110 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
113 val
= readl_relaxed(pll
->base
);
114 val
|= BM_PLL_ENABLE
;
115 writel_relaxed(val
, pll
->base
);
120 static void clk_pllv3_disable(struct clk_hw
*hw
)
122 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
125 val
= readl_relaxed(pll
->base
);
126 val
&= ~BM_PLL_ENABLE
;
127 writel_relaxed(val
, pll
->base
);
130 static unsigned long clk_pllv3_recalc_rate(struct clk_hw
*hw
,
131 unsigned long parent_rate
)
133 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
134 u32 div
= readl_relaxed(pll
->base
) & pll
->div_mask
;
136 return (div
== 1) ? parent_rate
* 22 : parent_rate
* 20;
139 static long clk_pllv3_round_rate(struct clk_hw
*hw
, unsigned long rate
,
140 unsigned long *prate
)
142 unsigned long parent_rate
= *prate
;
144 return (rate
>= parent_rate
* 22) ? parent_rate
* 22 :
148 static int clk_pllv3_set_rate(struct clk_hw
*hw
, unsigned long rate
,
149 unsigned long parent_rate
)
151 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
154 if (rate
== parent_rate
* 22)
156 else if (rate
== parent_rate
* 20)
161 val
= readl_relaxed(pll
->base
);
162 val
&= ~pll
->div_mask
;
164 writel_relaxed(val
, pll
->base
);
166 return clk_pllv3_wait_lock(pll
);
169 static const struct clk_ops clk_pllv3_ops
= {
170 .prepare
= clk_pllv3_prepare
,
171 .unprepare
= clk_pllv3_unprepare
,
172 .enable
= clk_pllv3_enable
,
173 .disable
= clk_pllv3_disable
,
174 .recalc_rate
= clk_pllv3_recalc_rate
,
175 .round_rate
= clk_pllv3_round_rate
,
176 .set_rate
= clk_pllv3_set_rate
,
179 static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw
*hw
,
180 unsigned long parent_rate
)
182 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
183 u32 div
= readl_relaxed(pll
->base
) & pll
->div_mask
;
185 return parent_rate
* div
/ 2;
188 static long clk_pllv3_sys_round_rate(struct clk_hw
*hw
, unsigned long rate
,
189 unsigned long *prate
)
191 unsigned long parent_rate
= *prate
;
192 unsigned long min_rate
= parent_rate
* 54 / 2;
193 unsigned long max_rate
= parent_rate
* 108 / 2;
198 else if (rate
< min_rate
)
200 div
= rate
* 2 / parent_rate
;
202 return parent_rate
* div
/ 2;
205 static int clk_pllv3_sys_set_rate(struct clk_hw
*hw
, unsigned long rate
,
206 unsigned long parent_rate
)
208 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
209 unsigned long min_rate
= parent_rate
* 54 / 2;
210 unsigned long max_rate
= parent_rate
* 108 / 2;
213 if (rate
< min_rate
|| rate
> max_rate
)
216 div
= rate
* 2 / parent_rate
;
217 val
= readl_relaxed(pll
->base
);
218 val
&= ~pll
->div_mask
;
220 writel_relaxed(val
, pll
->base
);
222 return clk_pllv3_wait_lock(pll
);
225 static const struct clk_ops clk_pllv3_sys_ops
= {
226 .prepare
= clk_pllv3_prepare
,
227 .unprepare
= clk_pllv3_unprepare
,
228 .enable
= clk_pllv3_enable
,
229 .disable
= clk_pllv3_disable
,
230 .recalc_rate
= clk_pllv3_sys_recalc_rate
,
231 .round_rate
= clk_pllv3_sys_round_rate
,
232 .set_rate
= clk_pllv3_sys_set_rate
,
235 static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw
*hw
,
236 unsigned long parent_rate
)
238 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
239 u32 mfn
= readl_relaxed(pll
->base
+ PLL_NUM_OFFSET
);
240 u32 mfd
= readl_relaxed(pll
->base
+ PLL_DENOM_OFFSET
);
241 u32 div
= readl_relaxed(pll
->base
) & pll
->div_mask
;
243 return (parent_rate
* div
) + ((parent_rate
/ mfd
) * mfn
);
246 static long clk_pllv3_av_round_rate(struct clk_hw
*hw
, unsigned long rate
,
247 unsigned long *prate
)
249 unsigned long parent_rate
= *prate
;
250 unsigned long min_rate
= parent_rate
* 27;
251 unsigned long max_rate
= parent_rate
* 54;
253 u32 mfn
, mfd
= 1000000;
258 else if (rate
< min_rate
)
261 div
= rate
/ parent_rate
;
262 temp64
= (u64
) (rate
- div
* parent_rate
);
264 do_div(temp64
, parent_rate
);
267 return parent_rate
* div
+ parent_rate
/ mfd
* mfn
;
270 static int clk_pllv3_av_set_rate(struct clk_hw
*hw
, unsigned long rate
,
271 unsigned long parent_rate
)
273 struct clk_pllv3
*pll
= to_clk_pllv3(hw
);
274 unsigned long min_rate
= parent_rate
* 27;
275 unsigned long max_rate
= parent_rate
* 54;
277 u32 mfn
, mfd
= 1000000;
280 if (rate
< min_rate
|| rate
> max_rate
)
283 div
= rate
/ parent_rate
;
284 temp64
= (u64
) (rate
- div
* parent_rate
);
286 do_div(temp64
, parent_rate
);
289 val
= readl_relaxed(pll
->base
);
290 val
&= ~pll
->div_mask
;
292 writel_relaxed(val
, pll
->base
);
293 writel_relaxed(mfn
, pll
->base
+ PLL_NUM_OFFSET
);
294 writel_relaxed(mfd
, pll
->base
+ PLL_DENOM_OFFSET
);
296 return clk_pllv3_wait_lock(pll
);
299 static const struct clk_ops clk_pllv3_av_ops
= {
300 .prepare
= clk_pllv3_prepare
,
301 .unprepare
= clk_pllv3_unprepare
,
302 .enable
= clk_pllv3_enable
,
303 .disable
= clk_pllv3_disable
,
304 .recalc_rate
= clk_pllv3_av_recalc_rate
,
305 .round_rate
= clk_pllv3_av_round_rate
,
306 .set_rate
= clk_pllv3_av_set_rate
,
309 static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw
*hw
,
310 unsigned long parent_rate
)
315 static const struct clk_ops clk_pllv3_enet_ops
= {
316 .prepare
= clk_pllv3_prepare
,
317 .unprepare
= clk_pllv3_unprepare
,
318 .enable
= clk_pllv3_enable
,
319 .disable
= clk_pllv3_disable
,
320 .recalc_rate
= clk_pllv3_enet_recalc_rate
,
323 struct clk
*imx_clk_pllv3(enum imx_pllv3_type type
, const char *name
,
324 const char *parent_name
, void __iomem
*base
,
327 struct clk_pllv3
*pll
;
328 const struct clk_ops
*ops
;
330 struct clk_init_data init
;
332 pll
= kzalloc(sizeof(*pll
), GFP_KERNEL
);
334 return ERR_PTR(-ENOMEM
);
338 ops
= &clk_pllv3_sys_ops
;
341 ops
= &clk_pllv3_ops
;
342 pll
->powerup_set
= true;
345 ops
= &clk_pllv3_av_ops
;
348 ops
= &clk_pllv3_enet_ops
;
351 ops
= &clk_pllv3_ops
;
354 pll
->div_mask
= div_mask
;
359 init
.parent_names
= &parent_name
;
360 init
.num_parents
= 1;
362 pll
->hw
.init
= &init
;
364 clk
= clk_register(NULL
, &pll
->hw
);