]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/arm/mach-imx/clk-pllv3.c
ARM: dts: imx6: fix messed up LDB clocks
[karo-tx-linux.git] / arch / arm / mach-imx / clk-pllv3.c
1 /*
2  * Copyright 2012 Freescale Semiconductor, Inc.
3  * Copyright 2012 Linaro Ltd.
4  *
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:
8  *
9  * http://www.opensource.org/licenses/gpl-license.html
10  * http://www.gnu.org/copyleft/gpl.html
11  */
12
13 #include <linux/clk.h>
14 #include <linux/clk-provider.h>
15 #include <linux/delay.h>
16 #include <linux/io.h>
17 #include <linux/slab.h>
18 #include <linux/jiffies.h>
19 #include <linux/err.h>
20 #include "clk.h"
21
22 #define PLL_NUM_OFFSET          0x10
23 #define PLL_DENOM_OFFSET        0x20
24
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
31
32 /**
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
39  *
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.
42  */
43 struct clk_pllv3 {
44         struct clk_hw   hw;
45         void __iomem    *base;
46         bool            powerup_set;
47         bool            always_on;
48         u32             div_mask;
49 };
50
51 #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
52
53 static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
54 {
55         unsigned long timeout = jiffies + msecs_to_jiffies(10);
56         u32 val = readl_relaxed(pll->base) & BM_PLL_POWER;
57
58         /* No need to wait for lock when pll is not powered up */
59         if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
60                 return 0;
61
62         /* Wait for PLL to lock */
63         do {
64                 if (readl_relaxed(pll->base) & BM_PLL_LOCK)
65                         break;
66                 if (time_after(jiffies, timeout))
67                         break;
68                 usleep_range(50, 500);
69         } while (1);
70
71         return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
72 }
73
74 static int clk_pllv3_prepare(struct clk_hw *hw)
75 {
76         struct clk_pllv3 *pll = to_clk_pllv3(hw);
77         u32 val;
78         int ret;
79
80         val = readl_relaxed(pll->base);
81         if (pll->powerup_set)
82                 val |= BM_PLL_POWER;
83         else
84                 val &= ~BM_PLL_POWER;
85         writel_relaxed(val, pll->base);
86
87         ret = clk_pllv3_wait_lock(pll);
88         if (ret)
89                 return ret;
90
91         val = readl_relaxed(pll->base);
92         val &= ~BM_PLL_BYPASS;
93         writel_relaxed(val, pll->base);
94
95         return 0;
96 }
97
98 static void clk_pllv3_unprepare(struct clk_hw *hw)
99 {
100         struct clk_pllv3 *pll = to_clk_pllv3(hw);
101         u32 val;
102
103         val = readl_relaxed(pll->base);
104         val |= BM_PLL_BYPASS;
105         if (pll->powerup_set)
106                 val &= ~BM_PLL_POWER;
107         else
108                 val |= BM_PLL_POWER;
109         writel_relaxed(val, pll->base);
110 }
111
112 static int clk_pllv3_enable(struct clk_hw *hw)
113 {
114         struct clk_pllv3 *pll = to_clk_pllv3(hw);
115         u32 val;
116
117         val = readl_relaxed(pll->base);
118         val |= BM_PLL_ENABLE;
119         writel_relaxed(val, pll->base);
120
121         return 0;
122 }
123
124 static void clk_pllv3_disable(struct clk_hw *hw)
125 {
126         struct clk_pllv3 *pll = to_clk_pllv3(hw);
127         u32 val;
128
129         val = readl_relaxed(pll->base);
130         if (!pll->always_on)
131                 val &= ~BM_PLL_ENABLE;
132         writel_relaxed(val, pll->base);
133 }
134
135 static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
136                                            unsigned long parent_rate)
137 {
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;
141         u32 rate;
142
143         if (bypass)
144                 rate = BYPASS_RATE;
145         else
146                 rate = (div == 1) ? parent_rate * 22 : parent_rate * 20;
147
148         return rate;
149 }
150
151 static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
152                                  unsigned long *prate)
153 {
154         unsigned long parent_rate = *prate;
155         struct clk_pllv3 *pll = to_clk_pllv3(hw);
156         u32 bypass = readl_relaxed(pll->base) & BYPASS_MASK;
157
158         /* If the PLL is bypassed, its rate is 24MHz. */
159         if (bypass)
160                 return BYPASS_RATE;
161
162         return (rate >= parent_rate * 22) ? parent_rate * 22 :
163                                             parent_rate * 20;
164 }
165
166 static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
167                 unsigned long parent_rate)
168 {
169         struct clk_pllv3 *pll = to_clk_pllv3(hw);
170         u32 val, div;
171         u32 bypass = readl_relaxed(pll->base) & BYPASS_MASK;
172
173         /* If the PLL is bypassed, its rate is 24MHz. */
174         if (bypass)
175                 return 0;
176
177         if (rate == parent_rate * 22)
178                 div = 1;
179         else if (rate == parent_rate * 20)
180                 div = 0;
181         else
182                 return -EINVAL;
183
184         val = readl_relaxed(pll->base);
185         val &= ~pll->div_mask;
186         val |= div;
187         writel_relaxed(val, pll->base);
188
189         return clk_pllv3_wait_lock(pll);
190 }
191
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,
200 };
201
202 static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
203                                                unsigned long parent_rate)
204 {
205         struct clk_pllv3 *pll = to_clk_pllv3(hw);
206         u32 div = readl_relaxed(pll->base) & pll->div_mask;
207
208         return parent_rate * div / 2;
209 }
210
211 static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
212                                      unsigned long *prate)
213 {
214         unsigned long parent_rate = *prate;
215         unsigned long min_rate = parent_rate * 54 / 2;
216         unsigned long max_rate = parent_rate * 108 / 2;
217         u32 div;
218
219         if (rate > max_rate)
220                 rate = max_rate;
221         else if (rate < min_rate)
222                 rate = min_rate;
223         div = rate * 2 / parent_rate;
224
225         return parent_rate * div / 2;
226 }
227
228 static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
229                 unsigned long parent_rate)
230 {
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;
234         u32 val, div;
235
236         if (rate < min_rate || rate > max_rate)
237                 return -EINVAL;
238
239         div = rate * 2 / parent_rate;
240         val = readl_relaxed(pll->base);
241         val &= ~pll->div_mask;
242         val |= div;
243         writel_relaxed(val, pll->base);
244
245         return clk_pllv3_wait_lock(pll);
246 }
247
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,
256 };
257
258 static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
259                                               unsigned long parent_rate)
260 {
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;
265
266         return (parent_rate * div) + ((parent_rate / mfd) * mfn);
267 }
268
269 static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
270                                     unsigned long *prate)
271 {
272         unsigned long parent_rate = *prate;
273         unsigned long min_rate = parent_rate * 27;
274         unsigned long max_rate = parent_rate * 54;
275         u32 div;
276         u32 mfn, mfd = 1000000;
277         s64 temp64;
278
279         if (rate > max_rate)
280                 rate = max_rate;
281         else if (rate < min_rate)
282                 rate = min_rate;
283
284         div = rate / parent_rate;
285         temp64 = (u64) (rate - div * parent_rate);
286         temp64 *= mfd;
287         do_div(temp64, parent_rate);
288         mfn = temp64;
289
290         return parent_rate * div + parent_rate / mfd * mfn;
291 }
292
293 static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
294                 unsigned long parent_rate)
295 {
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;
299         u32 val, div;
300         u32 mfn, mfd = 1000000;
301         s64 temp64;
302
303         if (rate < min_rate || rate > max_rate)
304                 return -EINVAL;
305
306         div = rate / parent_rate;
307         temp64 = (u64) (rate - div * parent_rate);
308         temp64 *= mfd;
309         do_div(temp64, parent_rate);
310         mfn = temp64;
311
312         val = readl_relaxed(pll->base);
313         val &= ~pll->div_mask;
314         val |= div;
315         writel_relaxed(val, pll->base);
316         writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
317         writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
318
319         return clk_pllv3_wait_lock(pll);
320 }
321
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,
330 };
331
332 static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
333                                                 unsigned long parent_rate)
334 {
335         return 500000000;
336 }
337
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,
344 };
345
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)
349 {
350         struct clk_pllv3 *pll;
351         const struct clk_ops *ops;
352         struct clk *clk;
353         struct clk_init_data init;
354
355         pll = kzalloc(sizeof(*pll), GFP_KERNEL);
356         if (!pll)
357                 return ERR_PTR(-ENOMEM);
358
359         switch (type) {
360         case IMX_PLLV3_SYS:
361                 ops = &clk_pllv3_sys_ops;
362                 break;
363         case IMX_PLLV3_USB:
364                 ops = &clk_pllv3_ops;
365                 pll->powerup_set = true;
366                 break;
367         case IMX_PLLV3_AV:
368                 ops = &clk_pllv3_av_ops;
369                 break;
370         case IMX_PLLV3_ENET:
371                 ops = &clk_pllv3_enet_ops;
372                 break;
373         default:
374                 ops = &clk_pllv3_ops;
375         }
376         pll->base = base;
377         pll->div_mask = div_mask;
378         pll->always_on = always_on;
379
380         init.name = name;
381         init.ops = ops;
382         init.flags = 0;
383         init.parent_names = &parent_name;
384         init.num_parents = 1;
385
386         pll->hw.init = &init;
387
388         clk = clk_register(NULL, &pll->hw);
389         if (IS_ERR(clk))
390                 kfree(pll);
391
392         return clk;
393 }