blob: d963b3e8861549279bc8cafe5adda544b3fd1ec2 [file] [log] [blame]
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +09001/*
2 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
3 * Copyright (c) 2013 Linaro Ltd.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This file contains the utility functions to register the pll clocks.
10*/
11
12#include <linux/errno.h>
Tomasz Figab4054ac2013-08-26 19:09:05 +020013#include <linux/hrtimer.h>
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +090014#include "clk.h"
15#include "clk-pll.h"
16
Tomasz Figab4054ac2013-08-26 19:09:05 +020017#define PLL_TIMEOUT_MS 10
18
Yadwinder Singh Brar079dbea2013-06-11 15:01:06 +053019struct samsung_clk_pll {
20 struct clk_hw hw;
21 void __iomem *lock_reg;
22 void __iomem *con_reg;
Yadwinder Singh Brar07dc76f2013-06-11 15:01:07 +053023 enum samsung_pll_type type;
Yadwinder Singh Brar3ff6e0d2013-06-11 15:01:12 +053024 unsigned int rate_count;
25 const struct samsung_pll_rate_table *rate_table;
Yadwinder Singh Brar079dbea2013-06-11 15:01:06 +053026};
27
28#define to_clk_pll(_hw) container_of(_hw, struct samsung_clk_pll, hw)
29
Yadwinder Singh Brardfa89312013-06-11 15:01:13 +053030static const struct samsung_pll_rate_table *samsung_get_pll_settings(
31 struct samsung_clk_pll *pll, unsigned long rate)
32{
33 const struct samsung_pll_rate_table *rate_table = pll->rate_table;
34 int i;
35
36 for (i = 0; i < pll->rate_count; i++) {
37 if (rate == rate_table[i].rate)
38 return &rate_table[i];
39 }
40
41 return NULL;
42}
43
44static long samsung_pll_round_rate(struct clk_hw *hw,
45 unsigned long drate, unsigned long *prate)
46{
47 struct samsung_clk_pll *pll = to_clk_pll(hw);
48 const struct samsung_pll_rate_table *rate_table = pll->rate_table;
49 int i;
50
51 /* Assumming rate_table is in descending order */
52 for (i = 0; i < pll->rate_count; i++) {
53 if (drate >= rate_table[i].rate)
54 return rate_table[i].rate;
55 }
56
57 /* return minimum supported value */
58 return rate_table[i - 1].rate;
59}
60
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +090061/*
62 * PLL35xx Clock Type
63 */
Yadwinder Singh Brardfa89312013-06-11 15:01:13 +053064/* Maximum lock time can be 270 * PDIV cycles */
65#define PLL35XX_LOCK_FACTOR (270)
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +090066
67#define PLL35XX_MDIV_MASK (0x3FF)
68#define PLL35XX_PDIV_MASK (0x3F)
69#define PLL35XX_SDIV_MASK (0x7)
Yadwinder Singh Brardfa89312013-06-11 15:01:13 +053070#define PLL35XX_LOCK_STAT_MASK (0x1)
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +090071#define PLL35XX_MDIV_SHIFT (16)
72#define PLL35XX_PDIV_SHIFT (8)
73#define PLL35XX_SDIV_SHIFT (0)
Yadwinder Singh Brardfa89312013-06-11 15:01:13 +053074#define PLL35XX_LOCK_STAT_SHIFT (29)
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +090075
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +090076static unsigned long samsung_pll35xx_recalc_rate(struct clk_hw *hw,
77 unsigned long parent_rate)
78{
Yadwinder Singh Brar079dbea2013-06-11 15:01:06 +053079 struct samsung_clk_pll *pll = to_clk_pll(hw);
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +090080 u32 mdiv, pdiv, sdiv, pll_con;
81 u64 fvco = parent_rate;
82
83 pll_con = __raw_readl(pll->con_reg);
84 mdiv = (pll_con >> PLL35XX_MDIV_SHIFT) & PLL35XX_MDIV_MASK;
85 pdiv = (pll_con >> PLL35XX_PDIV_SHIFT) & PLL35XX_PDIV_MASK;
86 sdiv = (pll_con >> PLL35XX_SDIV_SHIFT) & PLL35XX_SDIV_MASK;
87
88 fvco *= mdiv;
89 do_div(fvco, (pdiv << sdiv));
90
91 return (unsigned long)fvco;
92}
93
Yadwinder Singh Brardfa89312013-06-11 15:01:13 +053094static inline bool samsung_pll35xx_mp_change(
95 const struct samsung_pll_rate_table *rate, u32 pll_con)
96{
97 u32 old_mdiv, old_pdiv;
98
99 old_mdiv = (pll_con >> PLL35XX_MDIV_SHIFT) & PLL35XX_MDIV_MASK;
100 old_pdiv = (pll_con >> PLL35XX_PDIV_SHIFT) & PLL35XX_PDIV_MASK;
101
102 return (rate->mdiv != old_mdiv || rate->pdiv != old_pdiv);
103}
104
105static int samsung_pll35xx_set_rate(struct clk_hw *hw, unsigned long drate,
106 unsigned long prate)
107{
108 struct samsung_clk_pll *pll = to_clk_pll(hw);
109 const struct samsung_pll_rate_table *rate;
110 u32 tmp;
111
112 /* Get required rate settings from table */
113 rate = samsung_get_pll_settings(pll, drate);
114 if (!rate) {
115 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
116 drate, __clk_get_name(hw->clk));
117 return -EINVAL;
118 }
119
120 tmp = __raw_readl(pll->con_reg);
121
122 if (!(samsung_pll35xx_mp_change(rate, tmp))) {
123 /* If only s change, change just s value only*/
124 tmp &= ~(PLL35XX_SDIV_MASK << PLL35XX_SDIV_SHIFT);
125 tmp |= rate->sdiv << PLL35XX_SDIV_SHIFT;
126 __raw_writel(tmp, pll->con_reg);
127
128 return 0;
129 }
130
131 /* Set PLL lock time. */
132 __raw_writel(rate->pdiv * PLL35XX_LOCK_FACTOR,
133 pll->lock_reg);
134
135 /* Change PLL PMS values */
136 tmp &= ~((PLL35XX_MDIV_MASK << PLL35XX_MDIV_SHIFT) |
137 (PLL35XX_PDIV_MASK << PLL35XX_PDIV_SHIFT) |
138 (PLL35XX_SDIV_MASK << PLL35XX_SDIV_SHIFT));
139 tmp |= (rate->mdiv << PLL35XX_MDIV_SHIFT) |
140 (rate->pdiv << PLL35XX_PDIV_SHIFT) |
141 (rate->sdiv << PLL35XX_SDIV_SHIFT);
142 __raw_writel(tmp, pll->con_reg);
143
144 /* wait_lock_time */
145 do {
146 cpu_relax();
147 tmp = __raw_readl(pll->con_reg);
148 } while (!(tmp & (PLL35XX_LOCK_STAT_MASK
149 << PLL35XX_LOCK_STAT_SHIFT)));
150 return 0;
151}
152
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900153static const struct clk_ops samsung_pll35xx_clk_ops = {
154 .recalc_rate = samsung_pll35xx_recalc_rate,
Yadwinder Singh Brardfa89312013-06-11 15:01:13 +0530155 .round_rate = samsung_pll_round_rate,
156 .set_rate = samsung_pll35xx_set_rate,
157};
158
159static const struct clk_ops samsung_pll35xx_clk_min_ops = {
160 .recalc_rate = samsung_pll35xx_recalc_rate,
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900161};
162
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900163/*
164 * PLL36xx Clock Type
165 */
Vikas Sajjanbb4278d2013-06-11 15:01:14 +0530166/* Maximum lock time can be 3000 * PDIV cycles */
167#define PLL36XX_LOCK_FACTOR (3000)
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900168
169#define PLL36XX_KDIV_MASK (0xFFFF)
170#define PLL36XX_MDIV_MASK (0x1FF)
171#define PLL36XX_PDIV_MASK (0x3F)
172#define PLL36XX_SDIV_MASK (0x7)
173#define PLL36XX_MDIV_SHIFT (16)
174#define PLL36XX_PDIV_SHIFT (8)
175#define PLL36XX_SDIV_SHIFT (0)
Vikas Sajjanbb4278d2013-06-11 15:01:14 +0530176#define PLL36XX_KDIV_SHIFT (0)
177#define PLL36XX_LOCK_STAT_SHIFT (29)
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900178
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900179static unsigned long samsung_pll36xx_recalc_rate(struct clk_hw *hw,
180 unsigned long parent_rate)
181{
Yadwinder Singh Brar079dbea2013-06-11 15:01:06 +0530182 struct samsung_clk_pll *pll = to_clk_pll(hw);
Doug Anderson071ff9a2013-06-11 08:24:05 -0700183 u32 mdiv, pdiv, sdiv, pll_con0, pll_con1;
184 s16 kdiv;
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900185 u64 fvco = parent_rate;
186
187 pll_con0 = __raw_readl(pll->con_reg);
188 pll_con1 = __raw_readl(pll->con_reg + 4);
189 mdiv = (pll_con0 >> PLL36XX_MDIV_SHIFT) & PLL36XX_MDIV_MASK;
190 pdiv = (pll_con0 >> PLL36XX_PDIV_SHIFT) & PLL36XX_PDIV_MASK;
191 sdiv = (pll_con0 >> PLL36XX_SDIV_SHIFT) & PLL36XX_SDIV_MASK;
Doug Anderson071ff9a2013-06-11 08:24:05 -0700192 kdiv = (s16)(pll_con1 & PLL36XX_KDIV_MASK);
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900193
194 fvco *= (mdiv << 16) + kdiv;
195 do_div(fvco, (pdiv << sdiv));
196 fvco >>= 16;
197
198 return (unsigned long)fvco;
199}
200
Vikas Sajjanbb4278d2013-06-11 15:01:14 +0530201static inline bool samsung_pll36xx_mpk_change(
202 const struct samsung_pll_rate_table *rate, u32 pll_con0, u32 pll_con1)
203{
204 u32 old_mdiv, old_pdiv, old_kdiv;
205
206 old_mdiv = (pll_con0 >> PLL36XX_MDIV_SHIFT) & PLL36XX_MDIV_MASK;
207 old_pdiv = (pll_con0 >> PLL36XX_PDIV_SHIFT) & PLL36XX_PDIV_MASK;
208 old_kdiv = (pll_con1 >> PLL36XX_KDIV_SHIFT) & PLL36XX_KDIV_MASK;
209
210 return (rate->mdiv != old_mdiv || rate->pdiv != old_pdiv ||
211 rate->kdiv != old_kdiv);
212}
213
214static int samsung_pll36xx_set_rate(struct clk_hw *hw, unsigned long drate,
215 unsigned long parent_rate)
216{
217 struct samsung_clk_pll *pll = to_clk_pll(hw);
218 u32 tmp, pll_con0, pll_con1;
219 const struct samsung_pll_rate_table *rate;
220
221 rate = samsung_get_pll_settings(pll, drate);
222 if (!rate) {
223 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
224 drate, __clk_get_name(hw->clk));
225 return -EINVAL;
226 }
227
228 pll_con0 = __raw_readl(pll->con_reg);
229 pll_con1 = __raw_readl(pll->con_reg + 4);
230
231 if (!(samsung_pll36xx_mpk_change(rate, pll_con0, pll_con1))) {
232 /* If only s change, change just s value only*/
233 pll_con0 &= ~(PLL36XX_SDIV_MASK << PLL36XX_SDIV_SHIFT);
234 pll_con0 |= (rate->sdiv << PLL36XX_SDIV_SHIFT);
235 __raw_writel(pll_con0, pll->con_reg);
236
237 return 0;
238 }
239
240 /* Set PLL lock time. */
241 __raw_writel(rate->pdiv * PLL36XX_LOCK_FACTOR, pll->lock_reg);
242
243 /* Change PLL PMS values */
244 pll_con0 &= ~((PLL36XX_MDIV_MASK << PLL36XX_MDIV_SHIFT) |
245 (PLL36XX_PDIV_MASK << PLL36XX_PDIV_SHIFT) |
246 (PLL36XX_SDIV_MASK << PLL36XX_SDIV_SHIFT));
247 pll_con0 |= (rate->mdiv << PLL36XX_MDIV_SHIFT) |
248 (rate->pdiv << PLL36XX_PDIV_SHIFT) |
249 (rate->sdiv << PLL36XX_SDIV_SHIFT);
250 __raw_writel(pll_con0, pll->con_reg);
251
252 pll_con1 &= ~(PLL36XX_KDIV_MASK << PLL36XX_KDIV_SHIFT);
253 pll_con1 |= rate->kdiv << PLL36XX_KDIV_SHIFT;
254 __raw_writel(pll_con1, pll->con_reg + 4);
255
256 /* wait_lock_time */
257 do {
258 cpu_relax();
259 tmp = __raw_readl(pll->con_reg);
260 } while (!(tmp & (1 << PLL36XX_LOCK_STAT_SHIFT)));
261
262 return 0;
263}
264
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900265static const struct clk_ops samsung_pll36xx_clk_ops = {
266 .recalc_rate = samsung_pll36xx_recalc_rate,
Vikas Sajjanbb4278d2013-06-11 15:01:14 +0530267 .set_rate = samsung_pll36xx_set_rate,
268 .round_rate = samsung_pll_round_rate,
269};
270
271static const struct clk_ops samsung_pll36xx_clk_min_ops = {
272 .recalc_rate = samsung_pll36xx_recalc_rate,
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900273};
274
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900275/*
276 * PLL45xx Clock Type
277 */
Tomasz Figab4054ac2013-08-26 19:09:05 +0200278#define PLL4502_LOCK_FACTOR 400
279#define PLL4508_LOCK_FACTOR 240
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900280
281#define PLL45XX_MDIV_MASK (0x3FF)
282#define PLL45XX_PDIV_MASK (0x3F)
283#define PLL45XX_SDIV_MASK (0x7)
Tomasz Figab4054ac2013-08-26 19:09:05 +0200284#define PLL45XX_AFC_MASK (0x1F)
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900285#define PLL45XX_MDIV_SHIFT (16)
286#define PLL45XX_PDIV_SHIFT (8)
287#define PLL45XX_SDIV_SHIFT (0)
Tomasz Figab4054ac2013-08-26 19:09:05 +0200288#define PLL45XX_AFC_SHIFT (0)
289
290#define PLL45XX_ENABLE BIT(31)
291#define PLL45XX_LOCKED BIT(29)
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900292
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900293static unsigned long samsung_pll45xx_recalc_rate(struct clk_hw *hw,
294 unsigned long parent_rate)
295{
Tomasz Figa52b06012013-08-26 19:09:04 +0200296 struct samsung_clk_pll *pll = to_clk_pll(hw);
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900297 u32 mdiv, pdiv, sdiv, pll_con;
298 u64 fvco = parent_rate;
299
300 pll_con = __raw_readl(pll->con_reg);
301 mdiv = (pll_con >> PLL45XX_MDIV_SHIFT) & PLL45XX_MDIV_MASK;
302 pdiv = (pll_con >> PLL45XX_PDIV_SHIFT) & PLL45XX_PDIV_MASK;
303 sdiv = (pll_con >> PLL45XX_SDIV_SHIFT) & PLL45XX_SDIV_MASK;
304
305 if (pll->type == pll_4508)
306 sdiv = sdiv - 1;
307
308 fvco *= mdiv;
309 do_div(fvco, (pdiv << sdiv));
310
311 return (unsigned long)fvco;
312}
313
Tomasz Figab4054ac2013-08-26 19:09:05 +0200314static bool samsung_pll45xx_mp_change(u32 pll_con0, u32 pll_con1,
315 const struct samsung_pll_rate_table *rate)
316{
317 u32 old_mdiv, old_pdiv, old_afc;
318
319 old_mdiv = (pll_con0 >> PLL45XX_MDIV_SHIFT) & PLL45XX_MDIV_MASK;
320 old_pdiv = (pll_con0 >> PLL45XX_PDIV_SHIFT) & PLL45XX_PDIV_MASK;
321 old_afc = (pll_con1 >> PLL45XX_AFC_SHIFT) & PLL45XX_AFC_MASK;
322
323 return (old_mdiv != rate->mdiv || old_pdiv != rate->pdiv
324 || old_afc != rate->afc);
325}
326
327static int samsung_pll45xx_set_rate(struct clk_hw *hw, unsigned long drate,
328 unsigned long prate)
329{
330 struct samsung_clk_pll *pll = to_clk_pll(hw);
331 const struct samsung_pll_rate_table *rate;
332 u32 con0, con1;
333 ktime_t start;
334
335 /* Get required rate settings from table */
336 rate = samsung_get_pll_settings(pll, drate);
337 if (!rate) {
338 pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__,
339 drate, __clk_get_name(hw->clk));
340 return -EINVAL;
341 }
342
343 con0 = __raw_readl(pll->con_reg);
344 con1 = __raw_readl(pll->con_reg + 0x4);
345
346 if (!(samsung_pll45xx_mp_change(con0, con1, rate))) {
347 /* If only s change, change just s value only*/
348 con0 &= ~(PLL45XX_SDIV_MASK << PLL45XX_SDIV_SHIFT);
349 con0 |= rate->sdiv << PLL45XX_SDIV_SHIFT;
350 __raw_writel(con0, pll->con_reg);
351
352 return 0;
353 }
354
355 /* Set PLL PMS values. */
356 con0 &= ~((PLL45XX_MDIV_MASK << PLL45XX_MDIV_SHIFT) |
357 (PLL45XX_PDIV_MASK << PLL45XX_PDIV_SHIFT) |
358 (PLL45XX_SDIV_MASK << PLL45XX_SDIV_SHIFT));
359 con0 |= (rate->mdiv << PLL45XX_MDIV_SHIFT) |
360 (rate->pdiv << PLL45XX_PDIV_SHIFT) |
361 (rate->sdiv << PLL45XX_SDIV_SHIFT);
362
363 /* Set PLL AFC value. */
364 con1 = __raw_readl(pll->con_reg + 0x4);
365 con1 &= ~(PLL45XX_AFC_MASK << PLL45XX_AFC_SHIFT);
366 con1 |= (rate->afc << PLL45XX_AFC_SHIFT);
367
368 /* Set PLL lock time. */
369 switch (pll->type) {
370 case pll_4502:
371 __raw_writel(rate->pdiv * PLL4502_LOCK_FACTOR, pll->lock_reg);
372 break;
373 case pll_4508:
374 __raw_writel(rate->pdiv * PLL4508_LOCK_FACTOR, pll->lock_reg);
375 break;
376 default:
377 break;
378 };
379
380 /* Set new configuration. */
381 __raw_writel(con1, pll->con_reg + 0x4);
382 __raw_writel(con0, pll->con_reg);
383
384 /* Wait for locking. */
385 start = ktime_get();
386 while (!(__raw_readl(pll->con_reg) & PLL45XX_LOCKED)) {
387 ktime_t delta = ktime_sub(ktime_get(), start);
388
389 if (ktime_to_ms(delta) > PLL_TIMEOUT_MS) {
390 pr_err("%s: could not lock PLL %s\n",
391 __func__, __clk_get_name(hw->clk));
392 return -EFAULT;
393 }
394
395 cpu_relax();
396 }
397
398 return 0;
399}
400
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900401static const struct clk_ops samsung_pll45xx_clk_ops = {
402 .recalc_rate = samsung_pll45xx_recalc_rate,
Tomasz Figab4054ac2013-08-26 19:09:05 +0200403 .round_rate = samsung_pll_round_rate,
404 .set_rate = samsung_pll45xx_set_rate,
405};
406
407static const struct clk_ops samsung_pll45xx_clk_min_ops = {
408 .recalc_rate = samsung_pll45xx_recalc_rate,
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900409};
410
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900411/*
412 * PLL46xx Clock Type
413 */
414
415#define PLL46XX_MDIV_MASK (0x1FF)
416#define PLL46XX_PDIV_MASK (0x3F)
417#define PLL46XX_SDIV_MASK (0x7)
418#define PLL46XX_MDIV_SHIFT (16)
419#define PLL46XX_PDIV_SHIFT (8)
420#define PLL46XX_SDIV_SHIFT (0)
421
422#define PLL46XX_KDIV_MASK (0xFFFF)
423#define PLL4650C_KDIV_MASK (0xFFF)
424#define PLL46XX_KDIV_SHIFT (0)
425
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900426static unsigned long samsung_pll46xx_recalc_rate(struct clk_hw *hw,
427 unsigned long parent_rate)
428{
Tomasz Figac50d11f2013-08-26 19:09:06 +0200429 struct samsung_clk_pll *pll = to_clk_pll(hw);
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900430 u32 mdiv, pdiv, sdiv, kdiv, pll_con0, pll_con1, shift;
431 u64 fvco = parent_rate;
432
433 pll_con0 = __raw_readl(pll->con_reg);
434 pll_con1 = __raw_readl(pll->con_reg + 4);
435 mdiv = (pll_con0 >> PLL46XX_MDIV_SHIFT) & PLL46XX_MDIV_MASK;
436 pdiv = (pll_con0 >> PLL46XX_PDIV_SHIFT) & PLL46XX_PDIV_MASK;
437 sdiv = (pll_con0 >> PLL46XX_SDIV_SHIFT) & PLL46XX_SDIV_MASK;
438 kdiv = pll->type == pll_4650c ? pll_con1 & PLL4650C_KDIV_MASK :
439 pll_con1 & PLL46XX_KDIV_MASK;
440
441 shift = pll->type == pll_4600 ? 16 : 10;
442 fvco *= (mdiv << shift) + kdiv;
443 do_div(fvco, (pdiv << sdiv));
444 fvco >>= shift;
445
446 return (unsigned long)fvco;
447}
448
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900449static const struct clk_ops samsung_pll46xx_clk_ops = {
450 .recalc_rate = samsung_pll46xx_recalc_rate,
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900451};
452
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900453/*
Tomasz Figaeb527122013-07-23 01:49:19 +0200454 * PLL6552 Clock Type
455 */
456
Tomasz Figaeb527122013-07-23 01:49:19 +0200457#define PLL6552_MDIV_MASK 0x3ff
458#define PLL6552_PDIV_MASK 0x3f
459#define PLL6552_SDIV_MASK 0x7
460#define PLL6552_MDIV_SHIFT 16
461#define PLL6552_PDIV_SHIFT 8
462#define PLL6552_SDIV_SHIFT 0
463
Tomasz Figaeb527122013-07-23 01:49:19 +0200464static unsigned long samsung_pll6552_recalc_rate(struct clk_hw *hw,
465 unsigned long parent_rate)
466{
Tomasz Figa40ef7232013-08-21 02:33:21 +0200467 struct samsung_clk_pll *pll = to_clk_pll(hw);
Tomasz Figaeb527122013-07-23 01:49:19 +0200468 u32 mdiv, pdiv, sdiv, pll_con;
469 u64 fvco = parent_rate;
470
Tomasz Figa40ef7232013-08-21 02:33:21 +0200471 pll_con = __raw_readl(pll->con_reg);
Tomasz Figaeb527122013-07-23 01:49:19 +0200472 mdiv = (pll_con >> PLL6552_MDIV_SHIFT) & PLL6552_MDIV_MASK;
473 pdiv = (pll_con >> PLL6552_PDIV_SHIFT) & PLL6552_PDIV_MASK;
474 sdiv = (pll_con >> PLL6552_SDIV_SHIFT) & PLL6552_SDIV_MASK;
475
476 fvco *= mdiv;
477 do_div(fvco, (pdiv << sdiv));
478
479 return (unsigned long)fvco;
480}
481
482static const struct clk_ops samsung_pll6552_clk_ops = {
483 .recalc_rate = samsung_pll6552_recalc_rate,
484};
485
Tomasz Figaeb527122013-07-23 01:49:19 +0200486/*
487 * PLL6553 Clock Type
488 */
489
Tomasz Figaeb527122013-07-23 01:49:19 +0200490#define PLL6553_MDIV_MASK 0xff
491#define PLL6553_PDIV_MASK 0x3f
492#define PLL6553_SDIV_MASK 0x7
493#define PLL6553_KDIV_MASK 0xffff
494#define PLL6553_MDIV_SHIFT 16
495#define PLL6553_PDIV_SHIFT 8
496#define PLL6553_SDIV_SHIFT 0
497#define PLL6553_KDIV_SHIFT 0
498
Tomasz Figaeb527122013-07-23 01:49:19 +0200499static unsigned long samsung_pll6553_recalc_rate(struct clk_hw *hw,
500 unsigned long parent_rate)
501{
Tomasz Figa40ef7232013-08-21 02:33:21 +0200502 struct samsung_clk_pll *pll = to_clk_pll(hw);
Tomasz Figaeb527122013-07-23 01:49:19 +0200503 u32 mdiv, pdiv, sdiv, kdiv, pll_con0, pll_con1;
504 u64 fvco = parent_rate;
505
Tomasz Figa40ef7232013-08-21 02:33:21 +0200506 pll_con0 = __raw_readl(pll->con_reg);
507 pll_con1 = __raw_readl(pll->con_reg + 0x4);
Tomasz Figaeb527122013-07-23 01:49:19 +0200508 mdiv = (pll_con0 >> PLL6553_MDIV_SHIFT) & PLL6553_MDIV_MASK;
509 pdiv = (pll_con0 >> PLL6553_PDIV_SHIFT) & PLL6553_PDIV_MASK;
510 sdiv = (pll_con0 >> PLL6553_SDIV_SHIFT) & PLL6553_SDIV_MASK;
511 kdiv = (pll_con1 >> PLL6553_KDIV_SHIFT) & PLL6553_KDIV_MASK;
512
513 fvco *= (mdiv << 16) + kdiv;
514 do_div(fvco, (pdiv << sdiv));
515 fvco >>= 16;
516
517 return (unsigned long)fvco;
518}
519
520static const struct clk_ops samsung_pll6553_clk_ops = {
521 .recalc_rate = samsung_pll6553_recalc_rate,
522};
523
Tomasz Figaeb527122013-07-23 01:49:19 +0200524/*
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900525 * PLL2550x Clock Type
526 */
527
528#define PLL2550X_R_MASK (0x1)
529#define PLL2550X_P_MASK (0x3F)
530#define PLL2550X_M_MASK (0x3FF)
531#define PLL2550X_S_MASK (0x7)
532#define PLL2550X_R_SHIFT (20)
533#define PLL2550X_P_SHIFT (14)
534#define PLL2550X_M_SHIFT (4)
535#define PLL2550X_S_SHIFT (0)
536
537struct samsung_clk_pll2550x {
538 struct clk_hw hw;
539 const void __iomem *reg_base;
540 unsigned long offset;
541};
542
543#define to_clk_pll2550x(_hw) container_of(_hw, struct samsung_clk_pll2550x, hw)
544
545static unsigned long samsung_pll2550x_recalc_rate(struct clk_hw *hw,
546 unsigned long parent_rate)
547{
548 struct samsung_clk_pll2550x *pll = to_clk_pll2550x(hw);
549 u32 r, p, m, s, pll_stat;
550 u64 fvco = parent_rate;
551
552 pll_stat = __raw_readl(pll->reg_base + pll->offset * 3);
553 r = (pll_stat >> PLL2550X_R_SHIFT) & PLL2550X_R_MASK;
554 if (!r)
555 return 0;
556 p = (pll_stat >> PLL2550X_P_SHIFT) & PLL2550X_P_MASK;
557 m = (pll_stat >> PLL2550X_M_SHIFT) & PLL2550X_M_MASK;
558 s = (pll_stat >> PLL2550X_S_SHIFT) & PLL2550X_S_MASK;
559
560 fvco *= m;
561 do_div(fvco, (p << s));
562
563 return (unsigned long)fvco;
564}
565
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900566static const struct clk_ops samsung_pll2550x_clk_ops = {
567 .recalc_rate = samsung_pll2550x_recalc_rate,
Thomas Abraham1c4c5fe2013-03-09 17:02:48 +0900568};
569
570struct clk * __init samsung_clk_register_pll2550x(const char *name,
571 const char *pname, const void __iomem *reg_base,
572 const unsigned long offset)
573{
574 struct samsung_clk_pll2550x *pll;
575 struct clk *clk;
576 struct clk_init_data init;
577
578 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
579 if (!pll) {
580 pr_err("%s: could not allocate pll clk %s\n", __func__, name);
581 return NULL;
582 }
583
584 init.name = name;
585 init.ops = &samsung_pll2550x_clk_ops;
586 init.flags = CLK_GET_RATE_NOCACHE;
587 init.parent_names = &pname;
588 init.num_parents = 1;
589
590 pll->hw.init = &init;
591 pll->reg_base = reg_base;
592 pll->offset = offset;
593
594 clk = clk_register(NULL, &pll->hw);
595 if (IS_ERR(clk)) {
596 pr_err("%s: failed to register pll clock %s\n", __func__,
597 name);
598 kfree(pll);
599 }
600
601 if (clk_register_clkdev(clk, name, NULL))
602 pr_err("%s: failed to register lookup for %s", __func__, name);
603
604 return clk;
605}
Yadwinder Singh Brar07dc76f2013-06-11 15:01:07 +0530606
607static void __init _samsung_clk_register_pll(struct samsung_pll_clock *pll_clk,
608 void __iomem *base)
609{
610 struct samsung_clk_pll *pll;
611 struct clk *clk;
612 struct clk_init_data init;
Yadwinder Singh Brar3ff6e0d2013-06-11 15:01:12 +0530613 int ret, len;
Yadwinder Singh Brar07dc76f2013-06-11 15:01:07 +0530614
615 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
616 if (!pll) {
617 pr_err("%s: could not allocate pll clk %s\n",
618 __func__, pll_clk->name);
619 return;
620 }
621
622 init.name = pll_clk->name;
623 init.flags = pll_clk->flags;
624 init.parent_names = &pll_clk->parent_name;
625 init.num_parents = 1;
626
Yadwinder Singh Brar3ff6e0d2013-06-11 15:01:12 +0530627 if (pll_clk->rate_table) {
628 /* find count of rates in rate_table */
629 for (len = 0; pll_clk->rate_table[len].rate != 0; )
630 len++;
631
632 pll->rate_count = len;
633 pll->rate_table = kmemdup(pll_clk->rate_table,
634 pll->rate_count *
635 sizeof(struct samsung_pll_rate_table),
636 GFP_KERNEL);
637 WARN(!pll->rate_table,
638 "%s: could not allocate rate table for %s\n",
639 __func__, pll_clk->name);
640 }
641
Yadwinder Singh Brar07dc76f2013-06-11 15:01:07 +0530642 switch (pll_clk->type) {
643 /* clk_ops for 35xx and 2550 are similar */
644 case pll_35xx:
645 case pll_2550:
Yadwinder Singh Brardfa89312013-06-11 15:01:13 +0530646 if (!pll->rate_table)
647 init.ops = &samsung_pll35xx_clk_min_ops;
648 else
649 init.ops = &samsung_pll35xx_clk_ops;
Yadwinder Singh Brar07dc76f2013-06-11 15:01:07 +0530650 break;
Tomasz Figa52b06012013-08-26 19:09:04 +0200651 case pll_4500:
Tomasz Figab4054ac2013-08-26 19:09:05 +0200652 init.ops = &samsung_pll45xx_clk_min_ops;
653 break;
Tomasz Figa52b06012013-08-26 19:09:04 +0200654 case pll_4502:
655 case pll_4508:
Tomasz Figab4054ac2013-08-26 19:09:05 +0200656 if (!pll->rate_table)
657 init.ops = &samsung_pll45xx_clk_min_ops;
658 else
659 init.ops = &samsung_pll45xx_clk_ops;
Tomasz Figa52b06012013-08-26 19:09:04 +0200660 break;
Yadwinder Singh Brar07dc76f2013-06-11 15:01:07 +0530661 /* clk_ops for 36xx and 2650 are similar */
662 case pll_36xx:
663 case pll_2650:
Vikas Sajjanbb4278d2013-06-11 15:01:14 +0530664 if (!pll->rate_table)
665 init.ops = &samsung_pll36xx_clk_min_ops;
666 else
667 init.ops = &samsung_pll36xx_clk_ops;
Yadwinder Singh Brar07dc76f2013-06-11 15:01:07 +0530668 break;
Tomasz Figa40ef7232013-08-21 02:33:21 +0200669 case pll_6552:
670 init.ops = &samsung_pll6552_clk_ops;
671 break;
672 case pll_6553:
673 init.ops = &samsung_pll6553_clk_ops;
674 break;
Tomasz Figac50d11f2013-08-26 19:09:06 +0200675 case pll_4600:
676 case pll_4650:
677 case pll_4650c:
678 init.ops = &samsung_pll46xx_clk_ops;
679 break;
Yadwinder Singh Brar07dc76f2013-06-11 15:01:07 +0530680 default:
681 pr_warn("%s: Unknown pll type for pll clk %s\n",
682 __func__, pll_clk->name);
683 }
684
685 pll->hw.init = &init;
686 pll->type = pll_clk->type;
687 pll->lock_reg = base + pll_clk->lock_offset;
688 pll->con_reg = base + pll_clk->con_offset;
689
690 clk = clk_register(NULL, &pll->hw);
691 if (IS_ERR(clk)) {
692 pr_err("%s: failed to register pll clock %s : %ld\n",
693 __func__, pll_clk->name, PTR_ERR(clk));
694 kfree(pll);
695 return;
696 }
697
698 samsung_clk_add_lookup(clk, pll_clk->id);
699
700 if (!pll_clk->alias)
701 return;
702
703 ret = clk_register_clkdev(clk, pll_clk->alias, pll_clk->dev_name);
704 if (ret)
705 pr_err("%s: failed to register lookup for %s : %d",
706 __func__, pll_clk->name, ret);
707}
708
709void __init samsung_clk_register_pll(struct samsung_pll_clock *pll_list,
710 unsigned int nr_pll, void __iomem *base)
711{
712 int cnt;
713
714 for (cnt = 0; cnt < nr_pll; cnt++)
715 _samsung_clk_register_pll(&pll_list[cnt], base);
716}