ARM: imx: add sleep for pllv3 relock
[firefly-linux-kernel-4.4.55.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
30 /**
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
36  *
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.
39  */
40 struct clk_pllv3 {
41         struct clk_hw   hw;
42         void __iomem    *base;
43         bool            powerup_set;
44         u32             div_mask;
45 };
46
47 #define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
48
49 static int clk_pllv3_prepare(struct clk_hw *hw)
50 {
51         struct clk_pllv3 *pll = to_clk_pllv3(hw);
52         unsigned long timeout;
53         u32 val;
54
55         val = readl_relaxed(pll->base);
56         val &= ~BM_PLL_BYPASS;
57         if (pll->powerup_set)
58                 val |= BM_PLL_POWER;
59         else
60                 val &= ~BM_PLL_POWER;
61         writel_relaxed(val, pll->base);
62
63         timeout = jiffies + msecs_to_jiffies(10);
64         /* Wait for PLL to lock */
65         do {
66                 if (readl_relaxed(pll->base) & BM_PLL_LOCK)
67                         break;
68                 if (time_after(jiffies, timeout))
69                         break;
70                 usleep_range(50, 500);
71         } while (1);
72
73         if (readl_relaxed(pll->base) & BM_PLL_LOCK)
74                 return 0;
75         else
76                 return -ETIMEDOUT;
77 }
78
79 static void clk_pllv3_unprepare(struct clk_hw *hw)
80 {
81         struct clk_pllv3 *pll = to_clk_pllv3(hw);
82         u32 val;
83
84         val = readl_relaxed(pll->base);
85         val |= BM_PLL_BYPASS;
86         if (pll->powerup_set)
87                 val &= ~BM_PLL_POWER;
88         else
89                 val |= BM_PLL_POWER;
90         writel_relaxed(val, pll->base);
91 }
92
93 static int clk_pllv3_enable(struct clk_hw *hw)
94 {
95         struct clk_pllv3 *pll = to_clk_pllv3(hw);
96         u32 val;
97
98         val = readl_relaxed(pll->base);
99         val |= BM_PLL_ENABLE;
100         writel_relaxed(val, pll->base);
101
102         return 0;
103 }
104
105 static void clk_pllv3_disable(struct clk_hw *hw)
106 {
107         struct clk_pllv3 *pll = to_clk_pllv3(hw);
108         u32 val;
109
110         val = readl_relaxed(pll->base);
111         val &= ~BM_PLL_ENABLE;
112         writel_relaxed(val, pll->base);
113 }
114
115 static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
116                                            unsigned long parent_rate)
117 {
118         struct clk_pllv3 *pll = to_clk_pllv3(hw);
119         u32 div = readl_relaxed(pll->base)  & pll->div_mask;
120
121         return (div == 1) ? parent_rate * 22 : parent_rate * 20;
122 }
123
124 static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
125                                  unsigned long *prate)
126 {
127         unsigned long parent_rate = *prate;
128
129         return (rate >= parent_rate * 22) ? parent_rate * 22 :
130                                             parent_rate * 20;
131 }
132
133 static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
134                 unsigned long parent_rate)
135 {
136         struct clk_pllv3 *pll = to_clk_pllv3(hw);
137         u32 val, div;
138
139         if (rate == parent_rate * 22)
140                 div = 1;
141         else if (rate == parent_rate * 20)
142                 div = 0;
143         else
144                 return -EINVAL;
145
146         val = readl_relaxed(pll->base);
147         val &= ~pll->div_mask;
148         val |= div;
149         writel_relaxed(val, pll->base);
150
151         return 0;
152 }
153
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,
162 };
163
164 static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
165                                                unsigned long parent_rate)
166 {
167         struct clk_pllv3 *pll = to_clk_pllv3(hw);
168         u32 div = readl_relaxed(pll->base) & pll->div_mask;
169
170         return parent_rate * div / 2;
171 }
172
173 static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
174                                      unsigned long *prate)
175 {
176         unsigned long parent_rate = *prate;
177         unsigned long min_rate = parent_rate * 54 / 2;
178         unsigned long max_rate = parent_rate * 108 / 2;
179         u32 div;
180
181         if (rate > max_rate)
182                 rate = max_rate;
183         else if (rate < min_rate)
184                 rate = min_rate;
185         div = rate * 2 / parent_rate;
186
187         return parent_rate * div / 2;
188 }
189
190 static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
191                 unsigned long parent_rate)
192 {
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;
196         u32 val, div;
197
198         if (rate < min_rate || rate > max_rate)
199                 return -EINVAL;
200
201         div = rate * 2 / parent_rate;
202         val = readl_relaxed(pll->base);
203         val &= ~pll->div_mask;
204         val |= div;
205         writel_relaxed(val, pll->base);
206
207         return 0;
208 }
209
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,
218 };
219
220 static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
221                                               unsigned long parent_rate)
222 {
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;
227
228         return (parent_rate * div) + ((parent_rate / mfd) * mfn);
229 }
230
231 static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
232                                     unsigned long *prate)
233 {
234         unsigned long parent_rate = *prate;
235         unsigned long min_rate = parent_rate * 27;
236         unsigned long max_rate = parent_rate * 54;
237         u32 div;
238         u32 mfn, mfd = 1000000;
239         s64 temp64;
240
241         if (rate > max_rate)
242                 rate = max_rate;
243         else if (rate < min_rate)
244                 rate = min_rate;
245
246         div = rate / parent_rate;
247         temp64 = (u64) (rate - div * parent_rate);
248         temp64 *= mfd;
249         do_div(temp64, parent_rate);
250         mfn = temp64;
251
252         return parent_rate * div + parent_rate / mfd * mfn;
253 }
254
255 static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
256                 unsigned long parent_rate)
257 {
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;
261         u32 val, div;
262         u32 mfn, mfd = 1000000;
263         s64 temp64;
264
265         if (rate < min_rate || rate > max_rate)
266                 return -EINVAL;
267
268         div = rate / parent_rate;
269         temp64 = (u64) (rate - div * parent_rate);
270         temp64 *= mfd;
271         do_div(temp64, parent_rate);
272         mfn = temp64;
273
274         val = readl_relaxed(pll->base);
275         val &= ~pll->div_mask;
276         val |= div;
277         writel_relaxed(val, pll->base);
278         writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
279         writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
280
281         return 0;
282 }
283
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,
292 };
293
294 static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
295                                                 unsigned long parent_rate)
296 {
297         return 500000000;
298 }
299
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,
306 };
307
308 struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
309                           const char *parent_name, void __iomem *base,
310                           u32 div_mask)
311 {
312         struct clk_pllv3 *pll;
313         const struct clk_ops *ops;
314         struct clk *clk;
315         struct clk_init_data init;
316
317         pll = kzalloc(sizeof(*pll), GFP_KERNEL);
318         if (!pll)
319                 return ERR_PTR(-ENOMEM);
320
321         switch (type) {
322         case IMX_PLLV3_SYS:
323                 ops = &clk_pllv3_sys_ops;
324                 break;
325         case IMX_PLLV3_USB:
326                 ops = &clk_pllv3_ops;
327                 pll->powerup_set = true;
328                 break;
329         case IMX_PLLV3_AV:
330                 ops = &clk_pllv3_av_ops;
331                 break;
332         case IMX_PLLV3_ENET:
333                 ops = &clk_pllv3_enet_ops;
334                 break;
335         default:
336                 ops = &clk_pllv3_ops;
337         }
338         pll->base = base;
339         pll->div_mask = div_mask;
340
341         init.name = name;
342         init.ops = ops;
343         init.flags = 0;
344         init.parent_names = &parent_name;
345         init.num_parents = 1;
346
347         pll->hw.init = &init;
348
349         clk = clk_register(NULL, &pll->hw);
350         if (IS_ERR(clk))
351                 kfree(pll);
352
353         return clk;
354 }