blob: db7d41f25046e276525ec9ab7dedfb04a3e8c4bf [file] [log] [blame]
Tony Prisk85814d62012-08-22 02:01:39 +12001/*
2 * Clock implementation for VIA/Wondermedia SoC's
3 * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#include <linux/io.h>
17#include <linux/of.h>
18#include <linux/slab.h>
19#include <linux/bitops.h>
20#include <linux/clkdev.h>
21#include <linux/clk-provider.h>
22
23/* All clocks share the same lock as none can be changed concurrently */
24static DEFINE_SPINLOCK(_lock);
25
26struct clk_device {
27 struct clk_hw hw;
28 void __iomem *div_reg;
29 unsigned int div_mask;
30 void __iomem *en_reg;
31 int en_bit;
32 spinlock_t *lock;
33};
34
35/*
36 * Add new PLL_TYPE_x definitions here as required. Use the first known model
37 * to support the new type as the name.
38 * Add case statements to vtwm_pll_recalc_rate(), vtwm_pll_round_round() and
39 * vtwm_pll_set_rate() to handle the new PLL_TYPE_x
40 */
41
42#define PLL_TYPE_VT8500 0
43#define PLL_TYPE_WM8650 1
44
45struct clk_pll {
46 struct clk_hw hw;
47 void __iomem *reg;
48 spinlock_t *lock;
49 int type;
50};
51
52static void __iomem *pmc_base;
53
54#define to_clk_device(_hw) container_of(_hw, struct clk_device, hw)
55
56#define VT8500_PMC_BUSY_MASK 0x18
57
58static void vt8500_pmc_wait_busy(void)
59{
60 while (readl(pmc_base) & VT8500_PMC_BUSY_MASK)
61 cpu_relax();
62}
63
64static int vt8500_dclk_enable(struct clk_hw *hw)
65{
66 struct clk_device *cdev = to_clk_device(hw);
67 u32 en_val;
68 unsigned long flags = 0;
69
70 spin_lock_irqsave(cdev->lock, flags);
71
72 en_val = readl(cdev->en_reg);
73 en_val |= BIT(cdev->en_bit);
74 writel(en_val, cdev->en_reg);
75
76 spin_unlock_irqrestore(cdev->lock, flags);
77 return 0;
78}
79
80static void vt8500_dclk_disable(struct clk_hw *hw)
81{
82 struct clk_device *cdev = to_clk_device(hw);
83 u32 en_val;
84 unsigned long flags = 0;
85
86 spin_lock_irqsave(cdev->lock, flags);
87
88 en_val = readl(cdev->en_reg);
89 en_val &= ~BIT(cdev->en_bit);
90 writel(en_val, cdev->en_reg);
91
92 spin_unlock_irqrestore(cdev->lock, flags);
93}
94
95static int vt8500_dclk_is_enabled(struct clk_hw *hw)
96{
97 struct clk_device *cdev = to_clk_device(hw);
98 u32 en_val = (readl(cdev->en_reg) & BIT(cdev->en_bit));
99
100 return en_val ? 1 : 0;
101}
102
103static unsigned long vt8500_dclk_recalc_rate(struct clk_hw *hw,
104 unsigned long parent_rate)
105{
106 struct clk_device *cdev = to_clk_device(hw);
107 u32 div = readl(cdev->div_reg) & cdev->div_mask;
108
109 /* Special case for SDMMC devices */
110 if ((cdev->div_mask == 0x3F) && (div & BIT(5)))
111 div = 64 * (div & 0x1f);
112
113 /* div == 0 is actually the highest divisor */
114 if (div == 0)
115 div = (cdev->div_mask + 1);
116
117 return parent_rate / div;
118}
119
120static long vt8500_dclk_round_rate(struct clk_hw *hw, unsigned long rate,
121 unsigned long *prate)
122{
Tony Prisk973e1d12012-10-18 22:26:53 +1300123 struct clk_device *cdev = to_clk_device(hw);
Tony Prisk58eb5a62012-12-27 13:14:31 +1300124 u32 divisor;
125
126 if (rate == 0)
127 return 0;
128
129 divisor = *prate / rate;
Tony Prisk85814d62012-08-22 02:01:39 +1200130
Tony Prisk72480012012-12-27 13:14:30 +1300131 /* If prate / rate would be decimal, incr the divisor */
132 if (rate * divisor < *prate)
133 divisor++;
134
Tony Prisk973e1d12012-10-18 22:26:53 +1300135 /*
136 * If this is a request for SDMMC we have to adjust the divisor
137 * when >31 to use the fixed predivisor
138 */
139 if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
140 divisor = 64 * ((divisor / 64) + 1);
141 }
142
Tony Prisk85814d62012-08-22 02:01:39 +1200143 return *prate / divisor;
144}
145
146static int vt8500_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
147 unsigned long parent_rate)
148{
149 struct clk_device *cdev = to_clk_device(hw);
Tony Prisk58eb5a62012-12-27 13:14:31 +1300150 u32 divisor;
Tony Prisk85814d62012-08-22 02:01:39 +1200151 unsigned long flags = 0;
152
Tony Prisk58eb5a62012-12-27 13:14:31 +1300153 if (rate == 0)
154 return 0;
155
156 divisor = parent_rate / rate;
157
Tony Prisk72480012012-12-27 13:14:30 +1300158 /* If prate / rate would be decimal, incr the divisor */
159 if (rate * divisor < *prate)
160 divisor++;
161
Tony Prisk85814d62012-08-22 02:01:39 +1200162 if (divisor == cdev->div_mask + 1)
163 divisor = 0;
164
Tony Prisk973e1d12012-10-18 22:26:53 +1300165 /* SDMMC mask may need to be corrected before testing if its valid */
166 if ((cdev->div_mask == 0x3F) && (divisor > 31)) {
167 /*
168 * Bit 5 is a fixed /64 predivisor. If the requested divisor
169 * is >31 then correct for the fixed divisor being required.
170 */
171 divisor = 0x20 + (divisor / 64);
172 }
173
Tony Prisk85814d62012-08-22 02:01:39 +1200174 if (divisor > cdev->div_mask) {
175 pr_err("%s: invalid divisor for clock\n", __func__);
176 return -EINVAL;
177 }
178
179 spin_lock_irqsave(cdev->lock, flags);
180
181 vt8500_pmc_wait_busy();
182 writel(divisor, cdev->div_reg);
183 vt8500_pmc_wait_busy();
184
185 spin_lock_irqsave(cdev->lock, flags);
186
187 return 0;
188}
189
190
191static const struct clk_ops vt8500_gated_clk_ops = {
192 .enable = vt8500_dclk_enable,
193 .disable = vt8500_dclk_disable,
194 .is_enabled = vt8500_dclk_is_enabled,
195};
196
197static const struct clk_ops vt8500_divisor_clk_ops = {
198 .round_rate = vt8500_dclk_round_rate,
199 .set_rate = vt8500_dclk_set_rate,
200 .recalc_rate = vt8500_dclk_recalc_rate,
201};
202
203static const struct clk_ops vt8500_gated_divisor_clk_ops = {
204 .enable = vt8500_dclk_enable,
205 .disable = vt8500_dclk_disable,
206 .is_enabled = vt8500_dclk_is_enabled,
207 .round_rate = vt8500_dclk_round_rate,
208 .set_rate = vt8500_dclk_set_rate,
209 .recalc_rate = vt8500_dclk_recalc_rate,
210};
211
212#define CLK_INIT_GATED BIT(0)
213#define CLK_INIT_DIVISOR BIT(1)
214#define CLK_INIT_GATED_DIVISOR (CLK_INIT_DIVISOR | CLK_INIT_GATED)
215
216static __init void vtwm_device_clk_init(struct device_node *node)
217{
218 u32 en_reg, div_reg;
219 struct clk *clk;
220 struct clk_device *dev_clk;
221 const char *clk_name = node->name;
222 const char *parent_name;
223 struct clk_init_data init;
224 int rc;
225 int clk_init_flags = 0;
226
227 dev_clk = kzalloc(sizeof(*dev_clk), GFP_KERNEL);
228 if (WARN_ON(!dev_clk))
229 return;
230
231 dev_clk->lock = &_lock;
232
233 rc = of_property_read_u32(node, "enable-reg", &en_reg);
234 if (!rc) {
235 dev_clk->en_reg = pmc_base + en_reg;
236 rc = of_property_read_u32(node, "enable-bit", &dev_clk->en_bit);
237 if (rc) {
238 pr_err("%s: enable-bit property required for gated clock\n",
239 __func__);
240 return;
241 }
242 clk_init_flags |= CLK_INIT_GATED;
243 }
244
245 rc = of_property_read_u32(node, "divisor-reg", &div_reg);
246 if (!rc) {
247 dev_clk->div_reg = pmc_base + div_reg;
248 /*
249 * use 0x1f as the default mask since it covers
250 * almost all the clocks and reduces dts properties
251 */
252 dev_clk->div_mask = 0x1f;
253
254 of_property_read_u32(node, "divisor-mask", &dev_clk->div_mask);
255 clk_init_flags |= CLK_INIT_DIVISOR;
256 }
257
258 of_property_read_string(node, "clock-output-names", &clk_name);
259
260 switch (clk_init_flags) {
261 case CLK_INIT_GATED:
262 init.ops = &vt8500_gated_clk_ops;
263 break;
264 case CLK_INIT_DIVISOR:
265 init.ops = &vt8500_divisor_clk_ops;
266 break;
267 case CLK_INIT_GATED_DIVISOR:
268 init.ops = &vt8500_gated_divisor_clk_ops;
269 break;
270 default:
271 pr_err("%s: Invalid clock description in device tree\n",
272 __func__);
273 kfree(dev_clk);
274 return;
275 }
276
277 init.name = clk_name;
278 init.flags = 0;
279 parent_name = of_clk_get_parent_name(node, 0);
280 init.parent_names = &parent_name;
281 init.num_parents = 1;
282
283 dev_clk->hw.init = &init;
284
285 clk = clk_register(NULL, &dev_clk->hw);
286 if (WARN_ON(IS_ERR(clk))) {
287 kfree(dev_clk);
288 return;
289 }
290 rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
291 clk_register_clkdev(clk, clk_name, NULL);
292}
293
294
295/* PLL clock related functions */
296
297#define to_clk_pll(_hw) container_of(_hw, struct clk_pll, hw)
298
299/* Helper macros for PLL_VT8500 */
300#define VT8500_PLL_MUL(x) ((x & 0x1F) << 1)
301#define VT8500_PLL_DIV(x) ((x & 0x100) ? 1 : 2)
302
303#define VT8500_BITS_TO_FREQ(r, m, d) \
304 ((r / d) * m)
305
306#define VT8500_BITS_TO_VAL(m, d) \
307 ((d == 2 ? 0 : 0x100) | ((m >> 1) & 0x1F))
308
309/* Helper macros for PLL_WM8650 */
310#define WM8650_PLL_MUL(x) (x & 0x3FF)
311#define WM8650_PLL_DIV(x) (((x >> 10) & 7) * (1 << ((x >> 13) & 3)))
312
313#define WM8650_BITS_TO_FREQ(r, m, d1, d2) \
314 (r * m / (d1 * (1 << d2)))
315
316#define WM8650_BITS_TO_VAL(m, d1, d2) \
317 ((d2 << 13) | (d1 << 10) | (m & 0x3FF))
318
319
320static void vt8500_find_pll_bits(unsigned long rate, unsigned long parent_rate,
321 u32 *multiplier, u32 *prediv)
322{
323 unsigned long tclk;
324
325 /* sanity check */
326 if ((rate < parent_rate * 4) || (rate > parent_rate * 62)) {
327 pr_err("%s: requested rate out of range\n", __func__);
328 *multiplier = 0;
329 *prediv = 1;
330 return;
331 }
332 if (rate <= parent_rate * 31)
333 /* use the prediv to double the resolution */
334 *prediv = 2;
335 else
336 *prediv = 1;
337
338 *multiplier = rate / (parent_rate / *prediv);
339 tclk = (parent_rate / *prediv) * *multiplier;
340
341 if (tclk != rate)
342 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__,
343 rate, tclk);
344}
345
346static void wm8650_find_pll_bits(unsigned long rate, unsigned long parent_rate,
347 u32 *multiplier, u32 *divisor1, u32 *divisor2)
348{
349 u32 mul, div1, div2;
350 u32 best_mul, best_div1, best_div2;
351 unsigned long tclk, rate_err, best_err;
352
353 best_err = (unsigned long)-1;
354
355 /* Find the closest match (lower or equal to requested) */
356 for (div1 = 5; div1 >= 3; div1--)
357 for (div2 = 3; div2 >= 0; div2--)
358 for (mul = 3; mul <= 1023; mul++) {
359 tclk = parent_rate * mul / (div1 * (1 << div2));
360 if (tclk > rate)
361 continue;
362 /* error will always be +ve */
363 rate_err = rate - tclk;
364 if (rate_err == 0) {
365 *multiplier = mul;
366 *divisor1 = div1;
367 *divisor2 = div2;
368 return;
369 }
370
371 if (rate_err < best_err) {
372 best_err = rate_err;
373 best_mul = mul;
374 best_div1 = div1;
375 best_div2 = div2;
376 }
377 }
378
379 /* if we got here, it wasn't an exact match */
380 pr_warn("%s: requested rate %lu, found rate %lu\n", __func__, rate,
381 rate - best_err);
Tony Prisk35a5db52012-12-27 13:14:29 +1300382 *multiplier = best_mul;
383 *divisor1 = best_div1;
384 *divisor2 = best_div2;
Tony Prisk85814d62012-08-22 02:01:39 +1200385}
386
387static int vtwm_pll_set_rate(struct clk_hw *hw, unsigned long rate,
388 unsigned long parent_rate)
389{
390 struct clk_pll *pll = to_clk_pll(hw);
391 u32 mul, div1, div2;
392 u32 pll_val;
393 unsigned long flags = 0;
394
395 /* sanity check */
396
397 switch (pll->type) {
398 case PLL_TYPE_VT8500:
399 vt8500_find_pll_bits(rate, parent_rate, &mul, &div1);
400 pll_val = VT8500_BITS_TO_VAL(mul, div1);
401 break;
402 case PLL_TYPE_WM8650:
403 wm8650_find_pll_bits(rate, parent_rate, &mul, &div1, &div2);
404 pll_val = WM8650_BITS_TO_VAL(mul, div1, div2);
405 break;
406 default:
407 pr_err("%s: invalid pll type\n", __func__);
408 return 0;
409 }
410
411 spin_lock_irqsave(pll->lock, flags);
412
413 vt8500_pmc_wait_busy();
414 writel(pll_val, pll->reg);
415 vt8500_pmc_wait_busy();
416
417 spin_unlock_irqrestore(pll->lock, flags);
418
419 return 0;
420}
421
422static long vtwm_pll_round_rate(struct clk_hw *hw, unsigned long rate,
423 unsigned long *prate)
424{
425 struct clk_pll *pll = to_clk_pll(hw);
426 u32 mul, div1, div2;
427 long round_rate;
428
429 switch (pll->type) {
430 case PLL_TYPE_VT8500:
431 vt8500_find_pll_bits(rate, *prate, &mul, &div1);
432 round_rate = VT8500_BITS_TO_FREQ(*prate, mul, div1);
433 break;
434 case PLL_TYPE_WM8650:
435 wm8650_find_pll_bits(rate, *prate, &mul, &div1, &div2);
436 round_rate = WM8650_BITS_TO_FREQ(*prate, mul, div1, div2);
437 break;
438 default:
439 round_rate = 0;
440 }
441
442 return round_rate;
443}
444
445static unsigned long vtwm_pll_recalc_rate(struct clk_hw *hw,
446 unsigned long parent_rate)
447{
448 struct clk_pll *pll = to_clk_pll(hw);
449 u32 pll_val = readl(pll->reg);
450 unsigned long pll_freq;
451
452 switch (pll->type) {
453 case PLL_TYPE_VT8500:
454 pll_freq = parent_rate * VT8500_PLL_MUL(pll_val);
455 pll_freq /= VT8500_PLL_DIV(pll_val);
456 break;
457 case PLL_TYPE_WM8650:
458 pll_freq = parent_rate * WM8650_PLL_MUL(pll_val);
459 pll_freq /= WM8650_PLL_DIV(pll_val);
460 break;
461 default:
462 pll_freq = 0;
463 }
464
465 return pll_freq;
466}
467
468const struct clk_ops vtwm_pll_ops = {
469 .round_rate = vtwm_pll_round_rate,
470 .set_rate = vtwm_pll_set_rate,
471 .recalc_rate = vtwm_pll_recalc_rate,
472};
473
474static __init void vtwm_pll_clk_init(struct device_node *node, int pll_type)
475{
476 u32 reg;
477 struct clk *clk;
478 struct clk_pll *pll_clk;
479 const char *clk_name = node->name;
480 const char *parent_name;
481 struct clk_init_data init;
482 int rc;
483
484 rc = of_property_read_u32(node, "reg", &reg);
485 if (WARN_ON(rc))
486 return;
487
488 pll_clk = kzalloc(sizeof(*pll_clk), GFP_KERNEL);
489 if (WARN_ON(!pll_clk))
490 return;
491
492 pll_clk->reg = pmc_base + reg;
493 pll_clk->lock = &_lock;
494 pll_clk->type = pll_type;
495
496 of_property_read_string(node, "clock-output-names", &clk_name);
497
498 init.name = clk_name;
499 init.ops = &vtwm_pll_ops;
500 init.flags = 0;
501 parent_name = of_clk_get_parent_name(node, 0);
502 init.parent_names = &parent_name;
503 init.num_parents = 1;
504
505 pll_clk->hw.init = &init;
506
507 clk = clk_register(NULL, &pll_clk->hw);
508 if (WARN_ON(IS_ERR(clk))) {
509 kfree(pll_clk);
510 return;
511 }
512 rc = of_clk_add_provider(node, of_clk_src_simple_get, clk);
513 clk_register_clkdev(clk, clk_name, NULL);
514}
515
516
517/* Wrappers for initialization functions */
518
519static void __init vt8500_pll_init(struct device_node *node)
520{
521 vtwm_pll_clk_init(node, PLL_TYPE_VT8500);
522}
523
524static void __init wm8650_pll_init(struct device_node *node)
525{
526 vtwm_pll_clk_init(node, PLL_TYPE_WM8650);
527}
528
529static const __initconst struct of_device_id clk_match[] = {
530 { .compatible = "fixed-clock", .data = of_fixed_clk_setup, },
531 { .compatible = "via,vt8500-pll-clock", .data = vt8500_pll_init, },
532 { .compatible = "wm,wm8650-pll-clock", .data = wm8650_pll_init, },
533 { .compatible = "via,vt8500-device-clock",
534 .data = vtwm_device_clk_init, },
535 { /* sentinel */ }
536};
537
538void __init vtwm_clk_init(void __iomem *base)
539{
540 if (!base)
541 return;
542
543 pmc_base = base;
544
545 of_clk_init(clk_match);
546}