blob: c05c43d56a9425f4450ef1a3fbc64f3de3729743 [file] [log] [blame]
Shawn Guoa3f6b9d2012-04-04 16:02:28 +08001/*
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
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080013#include <linux/clk-provider.h>
Shawn Guo322503a2013-10-30 15:12:55 +080014#include <linux/delay.h>
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080015#include <linux/io.h>
16#include <linux/slab.h>
17#include <linux/jiffies.h>
18#include <linux/err.h>
19#include "clk.h"
20
21#define PLL_NUM_OFFSET 0x10
22#define PLL_DENOM_OFFSET 0x20
23
24#define BM_PLL_POWER (0x1 << 12)
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080025#define BM_PLL_LOCK (0x1 << 31)
Frank Lif5394742015-05-19 02:45:02 +080026#define IMX7_ENET_PLL_POWER (0x1 << 5)
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080027
28/**
29 * struct clk_pllv3 - IMX PLL clock version 3
30 * @clk_hw: clock source
31 * @base: base address of PLL registers
32 * @powerup_set: set POWER bit to power up the PLL
Frank Lif5394742015-05-19 02:45:02 +080033 * @powerdown: pll powerdown offset bit
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080034 * @div_mask: mask of divider bits
Stefan Agner60ad8462014-12-02 17:59:42 +010035 * @div_shift: shift of divider bits
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080036 *
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 */
40struct clk_pllv3 {
41 struct clk_hw hw;
42 void __iomem *base;
43 bool powerup_set;
Frank Lif5394742015-05-19 02:45:02 +080044 u32 powerdown;
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080045 u32 div_mask;
Stefan Agner60ad8462014-12-02 17:59:42 +010046 u32 div_shift;
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080047};
48
49#define to_clk_pllv3(_hw) container_of(_hw, struct clk_pllv3, hw)
50
Shawn Guobc3b84d2013-10-30 15:56:22 +080051static int clk_pllv3_wait_lock(struct clk_pllv3 *pll)
52{
53 unsigned long timeout = jiffies + msecs_to_jiffies(10);
Frank Lif5394742015-05-19 02:45:02 +080054 u32 val = readl_relaxed(pll->base) & pll->powerdown;
Shawn Guobc3b84d2013-10-30 15:56:22 +080055
56 /* No need to wait for lock when pll is not powered up */
57 if ((pll->powerup_set && !val) || (!pll->powerup_set && val))
58 return 0;
59
60 /* Wait for PLL to lock */
61 do {
62 if (readl_relaxed(pll->base) & BM_PLL_LOCK)
63 break;
64 if (time_after(jiffies, timeout))
65 break;
66 usleep_range(50, 500);
67 } while (1);
68
69 return readl_relaxed(pll->base) & BM_PLL_LOCK ? 0 : -ETIMEDOUT;
70}
71
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080072static int clk_pllv3_prepare(struct clk_hw *hw)
73{
74 struct clk_pllv3 *pll = to_clk_pllv3(hw);
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080075 u32 val;
76
77 val = readl_relaxed(pll->base);
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080078 if (pll->powerup_set)
79 val |= BM_PLL_POWER;
80 else
81 val &= ~BM_PLL_POWER;
82 writel_relaxed(val, pll->base);
83
Dmitry Voytikc400f7a2014-11-06 22:49:32 +040084 return clk_pllv3_wait_lock(pll);
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080085}
86
87static void clk_pllv3_unprepare(struct clk_hw *hw)
88{
89 struct clk_pllv3 *pll = to_clk_pllv3(hw);
90 u32 val;
91
92 val = readl_relaxed(pll->base);
Shawn Guoa3f6b9d2012-04-04 16:02:28 +080093 if (pll->powerup_set)
94 val &= ~BM_PLL_POWER;
95 else
96 val |= BM_PLL_POWER;
97 writel_relaxed(val, pll->base);
98}
99
Bai Ping4824b612015-11-25 00:06:53 +0800100static int clk_pllv3_is_prepared(struct clk_hw *hw)
101{
102 struct clk_pllv3 *pll = to_clk_pllv3(hw);
103
104 if (readl_relaxed(pll->base) & BM_PLL_LOCK)
105 return 1;
106
107 return 0;
108}
109
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800110static unsigned long clk_pllv3_recalc_rate(struct clk_hw *hw,
111 unsigned long parent_rate)
112{
113 struct clk_pllv3 *pll = to_clk_pllv3(hw);
Stefan Agner60ad8462014-12-02 17:59:42 +0100114 u32 div = (readl_relaxed(pll->base) >> pll->div_shift) & pll->div_mask;
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800115
116 return (div == 1) ? parent_rate * 22 : parent_rate * 20;
117}
118
119static long clk_pllv3_round_rate(struct clk_hw *hw, unsigned long rate,
120 unsigned long *prate)
121{
122 unsigned long parent_rate = *prate;
123
124 return (rate >= parent_rate * 22) ? parent_rate * 22 :
125 parent_rate * 20;
126}
127
128static int clk_pllv3_set_rate(struct clk_hw *hw, unsigned long rate,
129 unsigned long parent_rate)
130{
131 struct clk_pllv3 *pll = to_clk_pllv3(hw);
132 u32 val, div;
133
134 if (rate == parent_rate * 22)
135 div = 1;
136 else if (rate == parent_rate * 20)
137 div = 0;
138 else
139 return -EINVAL;
140
141 val = readl_relaxed(pll->base);
Stefan Agner60ad8462014-12-02 17:59:42 +0100142 val &= ~(pll->div_mask << pll->div_shift);
143 val |= (div << pll->div_shift);
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800144 writel_relaxed(val, pll->base);
145
Shawn Guobc3b84d2013-10-30 15:56:22 +0800146 return clk_pllv3_wait_lock(pll);
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800147}
148
149static const struct clk_ops clk_pllv3_ops = {
150 .prepare = clk_pllv3_prepare,
151 .unprepare = clk_pllv3_unprepare,
Bai Ping4824b612015-11-25 00:06:53 +0800152 .is_prepared = clk_pllv3_is_prepared,
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800153 .recalc_rate = clk_pllv3_recalc_rate,
154 .round_rate = clk_pllv3_round_rate,
155 .set_rate = clk_pllv3_set_rate,
156};
157
158static unsigned long clk_pllv3_sys_recalc_rate(struct clk_hw *hw,
159 unsigned long parent_rate)
160{
161 struct clk_pllv3 *pll = to_clk_pllv3(hw);
162 u32 div = readl_relaxed(pll->base) & pll->div_mask;
163
164 return parent_rate * div / 2;
165}
166
167static long clk_pllv3_sys_round_rate(struct clk_hw *hw, unsigned long rate,
168 unsigned long *prate)
169{
170 unsigned long parent_rate = *prate;
171 unsigned long min_rate = parent_rate * 54 / 2;
172 unsigned long max_rate = parent_rate * 108 / 2;
173 u32 div;
174
175 if (rate > max_rate)
176 rate = max_rate;
177 else if (rate < min_rate)
178 rate = min_rate;
179 div = rate * 2 / parent_rate;
180
181 return parent_rate * div / 2;
182}
183
184static int clk_pllv3_sys_set_rate(struct clk_hw *hw, unsigned long rate,
185 unsigned long parent_rate)
186{
187 struct clk_pllv3 *pll = to_clk_pllv3(hw);
188 unsigned long min_rate = parent_rate * 54 / 2;
189 unsigned long max_rate = parent_rate * 108 / 2;
190 u32 val, div;
191
192 if (rate < min_rate || rate > max_rate)
193 return -EINVAL;
194
195 div = rate * 2 / parent_rate;
196 val = readl_relaxed(pll->base);
197 val &= ~pll->div_mask;
198 val |= div;
199 writel_relaxed(val, pll->base);
200
Shawn Guobc3b84d2013-10-30 15:56:22 +0800201 return clk_pllv3_wait_lock(pll);
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800202}
203
204static const struct clk_ops clk_pllv3_sys_ops = {
205 .prepare = clk_pllv3_prepare,
206 .unprepare = clk_pllv3_unprepare,
Bai Ping4824b612015-11-25 00:06:53 +0800207 .is_prepared = clk_pllv3_is_prepared,
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800208 .recalc_rate = clk_pllv3_sys_recalc_rate,
209 .round_rate = clk_pllv3_sys_round_rate,
210 .set_rate = clk_pllv3_sys_set_rate,
211};
212
213static unsigned long clk_pllv3_av_recalc_rate(struct clk_hw *hw,
214 unsigned long parent_rate)
215{
216 struct clk_pllv3 *pll = to_clk_pllv3(hw);
217 u32 mfn = readl_relaxed(pll->base + PLL_NUM_OFFSET);
218 u32 mfd = readl_relaxed(pll->base + PLL_DENOM_OFFSET);
219 u32 div = readl_relaxed(pll->base) & pll->div_mask;
220
221 return (parent_rate * div) + ((parent_rate / mfd) * mfn);
222}
223
224static long clk_pllv3_av_round_rate(struct clk_hw *hw, unsigned long rate,
225 unsigned long *prate)
226{
227 unsigned long parent_rate = *prate;
228 unsigned long min_rate = parent_rate * 27;
229 unsigned long max_rate = parent_rate * 54;
230 u32 div;
231 u32 mfn, mfd = 1000000;
Anson Huang7a5568c2015-05-08 00:16:51 +0800232 u64 temp64;
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800233
234 if (rate > max_rate)
235 rate = max_rate;
236 else if (rate < min_rate)
237 rate = min_rate;
238
239 div = rate / parent_rate;
240 temp64 = (u64) (rate - div * parent_rate);
241 temp64 *= mfd;
242 do_div(temp64, parent_rate);
243 mfn = temp64;
244
245 return parent_rate * div + parent_rate / mfd * mfn;
246}
247
248static int clk_pllv3_av_set_rate(struct clk_hw *hw, unsigned long rate,
249 unsigned long parent_rate)
250{
251 struct clk_pllv3 *pll = to_clk_pllv3(hw);
252 unsigned long min_rate = parent_rate * 27;
253 unsigned long max_rate = parent_rate * 54;
254 u32 val, div;
255 u32 mfn, mfd = 1000000;
Anson Huang7a5568c2015-05-08 00:16:51 +0800256 u64 temp64;
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800257
258 if (rate < min_rate || rate > max_rate)
259 return -EINVAL;
260
261 div = rate / parent_rate;
262 temp64 = (u64) (rate - div * parent_rate);
263 temp64 *= mfd;
264 do_div(temp64, parent_rate);
265 mfn = temp64;
266
267 val = readl_relaxed(pll->base);
268 val &= ~pll->div_mask;
269 val |= div;
270 writel_relaxed(val, pll->base);
271 writel_relaxed(mfn, pll->base + PLL_NUM_OFFSET);
272 writel_relaxed(mfd, pll->base + PLL_DENOM_OFFSET);
273
Shawn Guobc3b84d2013-10-30 15:56:22 +0800274 return clk_pllv3_wait_lock(pll);
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800275}
276
277static const struct clk_ops clk_pllv3_av_ops = {
278 .prepare = clk_pllv3_prepare,
279 .unprepare = clk_pllv3_unprepare,
Bai Ping4824b612015-11-25 00:06:53 +0800280 .is_prepared = clk_pllv3_is_prepared,
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800281 .recalc_rate = clk_pllv3_av_recalc_rate,
282 .round_rate = clk_pllv3_av_round_rate,
283 .set_rate = clk_pllv3_av_set_rate,
284};
285
286static unsigned long clk_pllv3_enet_recalc_rate(struct clk_hw *hw,
287 unsigned long parent_rate)
288{
Sascha Hauer7a040922012-11-21 14:42:31 +0100289 return 500000000;
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800290}
291
292static const struct clk_ops clk_pllv3_enet_ops = {
293 .prepare = clk_pllv3_prepare,
294 .unprepare = clk_pllv3_unprepare,
Bai Ping4824b612015-11-25 00:06:53 +0800295 .is_prepared = clk_pllv3_is_prepared,
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800296 .recalc_rate = clk_pllv3_enet_recalc_rate,
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800297};
298
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800299struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
300 const char *parent_name, void __iomem *base,
Sascha Hauer2b254692012-11-22 10:18:41 +0100301 u32 div_mask)
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800302{
303 struct clk_pllv3 *pll;
304 const struct clk_ops *ops;
305 struct clk *clk;
306 struct clk_init_data init;
307
308 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
309 if (!pll)
310 return ERR_PTR(-ENOMEM);
311
Frank Lif5394742015-05-19 02:45:02 +0800312 pll->powerdown = BM_PLL_POWER;
313
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800314 switch (type) {
315 case IMX_PLLV3_SYS:
316 ops = &clk_pllv3_sys_ops;
317 break;
Stefan Agner60ad8462014-12-02 17:59:42 +0100318 case IMX_PLLV3_USB_VF610:
319 pll->div_shift = 1;
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800320 case IMX_PLLV3_USB:
321 ops = &clk_pllv3_ops;
322 pll->powerup_set = true;
323 break;
324 case IMX_PLLV3_AV:
325 ops = &clk_pllv3_av_ops;
326 break;
Frank Lif5394742015-05-19 02:45:02 +0800327 case IMX_PLLV3_ENET_IMX7:
328 pll->powerdown = IMX7_ENET_PLL_POWER;
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800329 case IMX_PLLV3_ENET:
330 ops = &clk_pllv3_enet_ops;
331 break;
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800332 default:
333 ops = &clk_pllv3_ops;
334 }
335 pll->base = base;
Shawn Guoa3f6b9d2012-04-04 16:02:28 +0800336 pll->div_mask = div_mask;
337
338 init.name = name;
339 init.ops = ops;
340 init.flags = 0;
341 init.parent_names = &parent_name;
342 init.num_parents = 1;
343
344 pll->hw.init = &init;
345
346 clk = clk_register(NULL, &pll->hw);
347 if (IS_ERR(clk))
348 kfree(pll);
349
350 return clk;
351}