]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/clock.c
OMAP2xxx clock: consolidate DELAYED_APP clock commits; fix barrier
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / clock.c
1 /*
2  *  linux/arch/arm/mach-omap2/clock.c
3  *
4  *  Copyright (C) 2005-2008 Texas Instruments, Inc.
5  *  Copyright (C) 2004-2008 Nokia Corporation
6  *
7  *  Contacts:
8  *  Richard Woodruff <r-woodruff2@ti.com>
9  *  Paul Walmsley
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15 #undef DEBUG
16
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/list.h>
21 #include <linux/errno.h>
22 #include <linux/delay.h>
23 #include <linux/clk.h>
24 #include <linux/io.h>
25 #include <linux/bitops.h>
26
27 #include <mach/clock.h>
28 #include <mach/clockdomain.h>
29 #include <mach/sram.h>
30 #include <mach/cpu.h>
31 #include <mach/prcm.h>
32 #include <mach/control.h>
33 #include <asm/div64.h>
34
35 #include <mach/sdrc.h>
36 #include "sdrc.h"
37 #include "clock.h"
38 #include "prm.h"
39 #include "prm-regbits-24xx.h"
40 #include "cm.h"
41 #include "cm-regbits-24xx.h"
42 #include "cm-regbits-34xx.h"
43
44 #define MAX_CLOCK_ENABLE_WAIT           100000
45
46 /* DPLL rate rounding: minimum DPLL multiplier, divider values */
47 #define DPLL_MIN_MULTIPLIER             1
48 #define DPLL_MIN_DIVIDER                1
49
50 /* Possible error results from _dpll_test_mult */
51 #define DPLL_MULT_UNDERFLOW             -1
52
53 /*
54  * Scale factor to mitigate roundoff errors in DPLL rate rounding.
55  * The higher the scale factor, the greater the risk of arithmetic overflow,
56  * but the closer the rounded rate to the target rate.  DPLL_SCALE_FACTOR
57  * must be a power of DPLL_SCALE_BASE.
58  */
59 #define DPLL_SCALE_FACTOR               64
60 #define DPLL_SCALE_BASE                 2
61 #define DPLL_ROUNDING_VAL               ((DPLL_SCALE_BASE / 2) * \
62                                          (DPLL_SCALE_FACTOR / DPLL_SCALE_BASE))
63
64 /* DPLL valid Fint frequency band limits - from 34xx TRM Section 4.7.6.2 */
65 #define DPLL_FINT_BAND1_MIN             750000
66 #define DPLL_FINT_BAND1_MAX             2100000
67 #define DPLL_FINT_BAND2_MIN             7500000
68 #define DPLL_FINT_BAND2_MAX             21000000
69
70 /* _dpll_test_fint() return codes */
71 #define DPLL_FINT_UNDERFLOW             -1
72 #define DPLL_FINT_INVALID               -2
73
74 /* Bitmask to isolate the register type of clk.enable_reg */
75 #define PRCM_REGTYPE_MASK               0xf0
76 /* various CM register type options */
77 #define CM_FCLKEN_REGTYPE               0x00
78 #define CM_ICLKEN_REGTYPE               0x10
79 #define CM_IDLEST_REGTYPE               0x20
80
81 u8 cpu_mask;
82
83 /*-------------------------------------------------------------------------
84  * OMAP2/3 specific clock functions
85  *-------------------------------------------------------------------------*/
86
87 /*
88  * _omap2_clk_read_reg - read a clock register
89  * @clk: struct clk *
90  *
91  * Given a struct clk *, returns the value of the clock's register.
92  */
93 static u32 _omap2_clk_read_reg(u16 reg_offset, struct clk *clk)
94 {
95         if (clk->prcm_mod & CLK_REG_IN_SCM)
96                 return omap_ctrl_readl(reg_offset);
97         else if (clk->prcm_mod & CLK_REG_IN_PRM)
98                 return prm_read_mod_reg(clk->prcm_mod & PRCM_MOD_ADDR_MASK,
99                                         reg_offset);
100         else
101                 return cm_read_mod_reg(clk->prcm_mod, reg_offset);
102 }
103
104 /*
105  * _omap2_clk_write_reg - write a clock's register
106  * @v: value to write to the clock's enable_reg
107  * @clk: struct clk *
108  *
109  * Given a register value @v and struct clk * @clk, writes the value of @v to
110  * the clock's enable register.  No return value.
111  */
112 static void _omap2_clk_write_reg(u32 v, u16 reg_offset, struct clk *clk)
113 {
114         if (clk->prcm_mod & CLK_REG_IN_SCM)
115                 omap_ctrl_writel(v, reg_offset);
116         else if (clk->prcm_mod & CLK_REG_IN_PRM)
117                 prm_write_mod_reg(v, clk->prcm_mod & PRCM_MOD_ADDR_MASK,
118                                   reg_offset);
119         else
120                 cm_write_mod_reg(v, clk->prcm_mod, reg_offset);
121 }
122
123 /**
124  * _omap2xxx_clk_commit - commit clock parent/rate changes in hardware
125  * @clk: struct clk *
126  *
127  * If @clk has the DELAYED_APP flag set, meaning that parent/rate changes
128  * don't take effect until the VALID_CONFIG bit is written, write the
129  * VALID_CONFIG bit and wait for the write to complete.  No return value.
130  */
131 static void _omap2xxx_clk_commit(struct clk *clk)
132 {
133         if (!cpu_is_omap24xx())
134                 return;
135
136         if (!(clk->flags & DELAYED_APP))
137                 return;
138
139         prm_write_mod_reg(OMAP24XX_VALID_CONFIG, OMAP24XX_GR_MOD,
140                           OMAP24XX_PRCM_CLKCFG_CTRL_OFFSET);
141         /* OCP barrier */
142         prm_read_mod_reg(OMAP24XX_GR_MOD, OMAP24XX_PRCM_CLKCFG_CTRL_OFFSET);
143 }
144
145 /*
146  * _dpll_test_fint - test whether an Fint value is valid for the DPLL
147  * @clk: DPLL struct clk to test
148  * @n: divider value (N) to test
149  *
150  * Tests whether a particular divider @n will result in a valid DPLL
151  * internal clock frequency Fint. See the 34xx TRM 4.7.6.2 "DPLL Jitter
152  * Correction".  Returns 0 if OK, -1 if the enclosing loop can terminate
153  * (assuming that it is counting N upwards), or -2 if the enclosing loop
154  * should skip to the next iteration (again assuming N is increasing).
155  */
156 static int _dpll_test_fint(struct clk *clk, u8 n)
157 {
158         struct dpll_data *dd;
159         long fint;
160         int ret = 0;
161
162         dd = clk->dpll_data;
163
164         /* DPLL divider must result in a valid jitter correction val */
165         fint = clk->parent->rate / (n + 1);
166         if (fint < DPLL_FINT_BAND1_MIN) {
167
168                 pr_debug("rejecting n=%d due to Fint failure, "
169                          "lowering max_divider\n", n);
170                 dd->max_divider = n;
171                 ret = DPLL_FINT_UNDERFLOW;
172
173         } else if (fint > DPLL_FINT_BAND1_MAX &&
174                    fint < DPLL_FINT_BAND2_MIN) {
175
176                 pr_debug("rejecting n=%d due to Fint failure\n", n);
177                 ret = DPLL_FINT_INVALID;
178
179         } else if (fint > DPLL_FINT_BAND2_MAX) {
180
181                 pr_debug("rejecting n=%d due to Fint failure, "
182                          "boosting min_divider\n", n);
183                 dd->min_divider = n;
184                 ret = DPLL_FINT_INVALID;
185
186         }
187
188         return ret;
189 }
190
191 /**
192  * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
193  * @clk: OMAP clock struct ptr to use
194  *
195  * Convert a clockdomain name stored in a struct clk 'clk' into a
196  * clockdomain pointer, and save it into the struct clk.  Intended to be
197  * called during clk_register().  No return value.
198  */
199 void omap2_init_clk_clkdm(struct clk *clk)
200 {
201         struct clockdomain *clkdm;
202
203         if (!clk->clkdm.name) {
204                 pr_err("clock: %s: missing clockdomain", clk->name);
205                 return;
206         }
207
208         clkdm = clkdm_lookup(clk->clkdm.name);
209         if (clkdm) {
210                 pr_debug("clock: associated clk %s to clkdm %s\n",
211                          clk->name, clk->clkdm.name);
212                 clk->clkdm.ptr = clkdm;
213         } else {
214                 pr_err("clock: %s: could not associate to clkdm %s\n",
215                        clk->name, clk->clkdm.name);
216         }
217 }
218
219 /**
220  * omap2_init_clksel_parent - set a clksel clk's parent field from the hardware
221  * @clk: OMAP clock struct ptr to use
222  *
223  * Given a pointer to a source-selectable struct clk, read the hardware
224  * register and determine what its parent is currently set to.  Update the
225  * clk->parent field with the appropriate clk ptr.
226  */
227 void omap2_init_clksel_parent(struct clk *clk)
228 {
229         const struct clksel *clks;
230         const struct clksel_rate *clkr;
231         u32 r, found = 0;
232
233         if (!clk->clksel)
234                 return;
235
236         r = _omap2_clk_read_reg(clk->clksel_reg, clk);
237         r &= clk->clksel_mask;
238         r >>= __ffs(clk->clksel_mask);
239
240         for (clks = clk->clksel; clks->parent && !found; clks++) {
241                 for (clkr = clks->rates; clkr->div && !found; clkr++) {
242                         if ((clkr->flags & cpu_mask) && (clkr->val == r)) {
243                                 if (clk->parent != clks->parent) {
244                                         pr_debug("clock: inited %s parent "
245                                                  "to %s (was %s)\n",
246                                                  clk->name, clks->parent->name,
247                                                  ((clk->parent) ?
248                                                   clk->parent->name : "NULL"));
249                                         if (clk->parent)
250                                                 omap_clk_del_child(clk->parent,
251                                                                    clk);
252                                         clk->parent = clks->parent;
253                                         omap_clk_add_child(clk->parent, clk);
254                                 };
255                                 found = 1;
256                         }
257                 }
258         }
259
260         if (!found)
261                 printk(KERN_ERR "clock: init parent: could not find "
262                        "regval %0x for clock %s\n", r,  clk->name);
263
264         return;
265 }
266
267 /**
268  * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
269  * @clk: struct clk * of a DPLL
270  * @parent_rate: rate of the parent of the DPLL clock
271  *
272  * DPLLs can be locked or bypassed - basically, enabled or disabled.
273  * When locked, the DPLL output depends on the M and N values.  When
274  * bypassed, on OMAP2xxx, the output rate is either the 32KiHz clock
275  * or sys_clk.  Bypass rates on OMAP3 depend on the DPLL: DPLLs 1 and
276  * 2 are bypassed with dpll1_fclk and dpll2_fclk respectively
277  * (generated by DPLL3), while DPLL 3, 4, and 5 bypass rates are sys_clk.
278  * Returns the current DPLL CLKOUT rate (*not* CLKOUTX2) if the DPLL is
279  * locked, or the appropriate bypass rate if the DPLL is bypassed, or 0
280  * if the clock @clk is not a DPLL.
281  */
282 u32 omap2_get_dpll_rate(struct clk *clk, unsigned long parent_rate)
283 {
284         long long dpll_clk;
285         u32 dpll_mult, dpll_div, v;
286         struct dpll_data *dd;
287
288         dd = clk->dpll_data;
289         if (!dd)
290                 return 0;
291
292         /* Return bypass rate if DPLL is bypassed */
293         v = cm_read_mod_reg(clk->prcm_mod, dd->control_reg);
294         v &= dd->enable_mask;
295         v >>= __ffs(dd->enable_mask);
296
297         if (cpu_is_omap24xx()) {
298
299                 if (v == OMAP2XXX_EN_DPLL_LPBYPASS ||
300                     v == OMAP2XXX_EN_DPLL_FRBYPASS)
301                         return parent_rate;
302
303         } else if (cpu_is_omap34xx()) {
304
305                 if (v == OMAP3XXX_EN_DPLL_LPBYPASS ||
306                     v == OMAP3XXX_EN_DPLL_FRBYPASS)
307                         return dd->bypass_clk->rate;
308
309         }
310
311         v = cm_read_mod_reg(clk->prcm_mod, dd->mult_div1_reg);
312         dpll_mult = v & dd->mult_mask;
313         dpll_mult >>= __ffs(dd->mult_mask);
314         dpll_div = v & dd->div1_mask;
315         dpll_div >>= __ffs(dd->div1_mask);
316
317         dpll_clk = (long long)parent_rate * dpll_mult;
318         do_div(dpll_clk, dpll_div + 1);
319
320         return dpll_clk;
321 }
322
323 /*
324  * Used for clocks that have the same value as the parent clock,
325  * divided by some factor
326  */
327 void omap2_fixed_divisor_recalc(struct clk *clk, unsigned long parent_rate,
328                                 u8 rate_storage)
329 {
330         unsigned long rate;
331
332         WARN_ON(!clk->fixed_div); /* XXX move this to init */
333
334         rate = parent_rate / clk->fixed_div;
335
336         if (rate_storage == CURRENT_RATE)
337                 clk->rate = rate;
338         else if (rate_storage == TEMP_RATE)
339                 clk->temp_rate = rate;
340 }
341
342 /**
343  * omap2_wait_clock_ready - wait for clock to enable
344  * @prcm_mod: CM submodule offset from CM_BASE (e.g., "MPU_MOD")
345  * @reg_index: offset of CM register address from prcm_mod
346  * @mask: value to mask against to determine if the clock is active
347  * @name: name of the clock (for printk)
348  *
349  * Returns 1 if the clock enabled in time, or 0 if it failed to enable
350  * in roughly MAX_CLOCK_ENABLE_WAIT microseconds.
351  */
352 int omap2_wait_clock_ready(s16 prcm_mod, u16 reg_index, u32 mask,
353                            const char *name)
354 {
355         int i = 0, ena = 0;
356
357         /*
358          * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
359          * 34xx reverses this, just to keep us on our toes
360          */
361         if (cpu_mask & (RATE_IN_242X | RATE_IN_243X))
362                 ena = mask;
363         else if (cpu_mask & RATE_IN_343X)
364                 ena = 0;
365
366         /* Wait for lock */
367         while (((cm_read_mod_reg(prcm_mod, reg_index) & mask) != ena) &&
368                (i++ < MAX_CLOCK_ENABLE_WAIT)) {
369                 udelay(1);
370         }
371
372         if (i < MAX_CLOCK_ENABLE_WAIT)
373                 pr_debug("Clock %s stable after %d loops\n", name, i);
374         else
375                 printk(KERN_ERR "Clock %s didn't enable in %d tries\n",
376                        name, MAX_CLOCK_ENABLE_WAIT);
377
378         return (i < MAX_CLOCK_ENABLE_WAIT) ? 1 : 0;
379 };
380
381
382 /*
383  * omap2_clk_wait_ready - wait for a OMAP module to come out of target idle
384  * @clk: struct clk * recently enabled to indicate the module to test
385  *
386  * Wait for an OMAP module with a target idle state bit to come out of
387  * idle once both its interface clock and primary functional clock are
388  * both enabled.  Any register read or write to the device before it
389  * returns from idle will cause an abort.  Not all modules have target
390  * idle state bits (for example, DSS and CAM on OMAP24xx); so we don't
391  * wait for those.  No return value.
392  *
393  * We don't need special code here for INVERT_ENABLE for the time
394  * being since INVERT_ENABLE only applies to clocks enabled by
395  * CM_CLKEN_PLL.
396  *
397  * REVISIT: This function is misnamed: it should be something like
398  * "omap2_module_wait_ready", and in the long-term, it does not belong
399  * in the clock framework. It also shouldn't be doing register
400  * arithmetic to determine the companion clock.
401  */
402 static void omap2_clk_wait_ready(struct clk *clk)
403 {
404         u16 other_reg, idlest_reg;
405         u32 other_bit;
406
407         if (!(clk->flags & WAIT_READY))
408                 return;
409
410         /* If we are enabling an iclk, also test the fclk; and vice versa */
411         other_bit = 1 << clk->enable_bit;
412         other_reg = clk->enable_reg & ~PRCM_REGTYPE_MASK;
413
414         if (clk->enable_reg & CM_ICLKEN_REGTYPE)
415                 other_reg |= CM_FCLKEN_REGTYPE;
416         else
417                 other_reg |= CM_ICLKEN_REGTYPE;
418
419         /* Ensure functional and interface clocks are running. */
420         if (!(cm_read_mod_reg(clk->prcm_mod, other_reg) & other_bit))
421                 return;
422
423         idlest_reg = other_reg & ~PRCM_REGTYPE_MASK;
424         idlest_reg |= CM_IDLEST_REGTYPE;
425
426         omap2_wait_clock_ready(clk->prcm_mod, idlest_reg, 1 << clk->idlest_bit,
427                                clk->name);
428 }
429
430 /* Enables clock without considering parent dependencies or use count
431  * REVISIT: Maybe change this to use clk->enable like on omap1?
432  */
433 static int _omap2_clk_enable(struct clk *clk)
434 {
435         u32 v;
436
437         if (clk->flags & (ALWAYS_ENABLED | PARENT_CONTROLS_CLOCK))
438                 return 0;
439
440         if (clk->enable)
441                 return clk->enable(clk);
442
443         v = _omap2_clk_read_reg(clk->enable_reg, clk);
444         if (clk->flags & INVERT_ENABLE)
445                 v &= ~(1 << clk->enable_bit);
446         else
447                 v |= (1 << clk->enable_bit);
448         _omap2_clk_write_reg(v, clk->enable_reg, clk);
449         wmb();
450
451         omap2_clk_wait_ready(clk);
452
453         return 0;
454 }
455
456 /* Disables clock without considering parent dependencies or use count */
457 static void _omap2_clk_disable(struct clk *clk)
458 {
459         u32 v;
460
461         if (clk->flags & (ALWAYS_ENABLED | PARENT_CONTROLS_CLOCK))
462                 return;
463
464         if (clk->disable) {
465                 clk->disable(clk);
466                 return;
467         }
468
469         v = _omap2_clk_read_reg(clk->enable_reg, clk);
470         if (clk->flags & INVERT_ENABLE)
471                 v |= (1 << clk->enable_bit);
472         else
473                 v &= ~(1 << clk->enable_bit);
474         _omap2_clk_write_reg(v, clk->enable_reg, clk);
475         /* No OCP barrier needed here since it is a disable operation */
476 }
477
478 void omap2_clk_disable(struct clk *clk)
479 {
480         if (clk->usecount > 0 && !(--clk->usecount)) {
481                 _omap2_clk_disable(clk);
482                 if (clk->parent)
483                         omap2_clk_disable(clk->parent);
484                 if (clk->clkdm.ptr)
485                         omap2_clkdm_clk_disable(clk->clkdm.ptr, clk);
486
487         }
488 }
489
490 int omap2_clk_enable(struct clk *clk)
491 {
492         int ret = 0;
493
494         if (clk->usecount++ == 0) {
495                 if (clk->parent)
496                         ret = omap2_clk_enable(clk->parent);
497
498                 if (ret != 0) {
499                         clk->usecount--;
500                         return ret;
501                 }
502
503                 if (clk->clkdm.ptr)
504                         omap2_clkdm_clk_enable(clk->clkdm.ptr, clk);
505
506                 ret = _omap2_clk_enable(clk);
507
508                 if (ret != 0) {
509                         if (clk->clkdm.ptr)
510                                 omap2_clkdm_clk_disable(clk->clkdm.ptr, clk);
511
512                         if (clk->parent) {
513                                 omap2_clk_disable(clk->parent);
514                                 clk->usecount--;
515                         }
516                 }
517         }
518
519         return ret;
520 }
521
522 /*
523  * Used for clocks that are part of CLKSEL_xyz governed clocks.
524  * REVISIT: Maybe change to use clk->enable() functions like on omap1?
525  */
526 void omap2_clksel_recalc(struct clk *clk, unsigned long parent_rate,
527                          u8 rate_storage)
528 {
529         u32 div = 0;
530         unsigned long rate;
531
532         pr_debug("clock: recalc'ing clksel clk %s\n", clk->name);
533
534         div = omap2_clksel_get_divisor(clk);
535         if (div == 0)
536                 return;
537
538         rate = parent_rate / div;
539
540         if (rate_storage == CURRENT_RATE)
541                 clk->rate = rate;
542         else if (rate_storage == TEMP_RATE)
543                 clk->temp_rate = rate;
544
545         pr_debug("clock: new clock rate is %ld (div %d)\n", clk->rate, div);
546 }
547
548 /**
549  * omap2_get_clksel_by_parent - return clksel struct for a given clk & parent
550  * @clk: OMAP struct clk ptr to inspect
551  * @src_clk: OMAP struct clk ptr of the parent clk to search for
552  *
553  * Scan the struct clksel array associated with the clock to find
554  * the element associated with the supplied parent clock address.
555  * Returns a pointer to the struct clksel on success or NULL on error.
556  */
557 static const struct clksel *omap2_get_clksel_by_parent(struct clk *clk,
558                                                        struct clk *src_clk)
559 {
560         const struct clksel *clks;
561
562         if (!clk->clksel)
563                 return NULL;
564
565         for (clks = clk->clksel; clks->parent; clks++) {
566                 if (clks->parent == src_clk)
567                         break; /* Found the requested parent */
568         }
569
570         if (!clks->parent) {
571                 printk(KERN_ERR "clock: Could not find parent clock %s in "
572                        "clksel array of clock %s\n", src_clk->name,
573                        clk->name);
574                 return NULL;
575         }
576
577         return clks;
578 }
579
580 /**
581  * omap2_clksel_round_rate_div - find divisor for the given clock and rate
582  * @clk: OMAP struct clk to use
583  * @target_rate: desired clock rate
584  * @new_div: ptr to where we should store the divisor
585  *
586  * Finds 'best' divider value in an array based on the source and target
587  * rates.  The divider array must be sorted with smallest divider first.
588  *
589  * Returns the rounded clock rate or returns 0xffffffff on error.
590  */
591 u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
592                                 u32 *new_div)
593 {
594         unsigned long test_rate;
595         const struct clksel *clks;
596         const struct clksel_rate *clkr;
597         u32 last_div = 0;
598
599         printk(KERN_INFO "clock: clksel_round_rate_div: %s target_rate %ld\n",
600                clk->name, target_rate);
601
602         *new_div = 1;
603
604         clks = omap2_get_clksel_by_parent(clk, clk->parent);
605         if (!clks)
606                 return ~0;
607
608         for (clkr = clks->rates; clkr->div; clkr++) {
609                 if (!(clkr->flags & cpu_mask))
610                     continue;
611
612                 /* Sanity check */
613                 if (clkr->div <= last_div)
614                         printk(KERN_ERR "clock: clksel_rate table not sorted "
615                                "for clock %s", clk->name);
616
617                 last_div = clkr->div;
618
619                 test_rate = clk->parent->rate / clkr->div;
620
621                 if (test_rate <= target_rate)
622                         break; /* found it */
623         }
624
625         if (!clkr->div) {
626                 printk(KERN_ERR "clock: Could not find divisor for target "
627                        "rate %ld for clock %s parent %s\n", target_rate,
628                        clk->name, clk->parent->name);
629                 return ~0;
630         }
631
632         *new_div = clkr->div;
633
634         printk(KERN_INFO "clock: new_div = %d, new_rate = %ld\n", *new_div,
635                (clk->parent->rate / clkr->div));
636
637         return (clk->parent->rate / clkr->div);
638 }
639
640 /**
641  * omap2_clksel_round_rate - find rounded rate for the given clock and rate
642  * @clk: OMAP struct clk to use
643  * @target_rate: desired clock rate
644  *
645  * Compatibility wrapper for OMAP clock framework
646  * Finds best target rate based on the source clock and possible dividers.
647  * rates. The divider array must be sorted with smallest divider first.
648  *
649  * Returns the rounded clock rate or returns 0xffffffff on error.
650  */
651 long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
652 {
653         u32 new_div;
654
655         return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
656 }
657
658
659 /* Given a clock and a rate apply a clock specific rounding function */
660 long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
661 {
662         if (clk->round_rate != NULL)
663                 return clk->round_rate(clk, rate);
664
665         return clk->rate;
666 }
667
668 /**
669  * omap2_clksel_to_divisor() - turn clksel field value into integer divider
670  * @clk: OMAP struct clk to use
671  * @field_val: register field value to find
672  *
673  * Given a struct clk of a rate-selectable clksel clock, and a register field
674  * value to search for, find the corresponding clock divisor.  The register
675  * field value should be pre-masked and shifted down so the LSB is at bit 0
676  * before calling.  Returns 0 on error
677  */
678 u32 omap2_clksel_to_divisor(struct clk *clk, u32 field_val)
679 {
680         const struct clksel *clks;
681         const struct clksel_rate *clkr;
682
683         clks = omap2_get_clksel_by_parent(clk, clk->parent);
684         if (!clks)
685                 return 0;
686
687         for (clkr = clks->rates; clkr->div; clkr++) {
688                 if ((clkr->flags & cpu_mask) && (clkr->val == field_val))
689                         break;
690         }
691
692         if (!clkr->div) {
693                 printk(KERN_ERR "clock: Could not find fieldval %d for "
694                        "clock %s parent %s\n", field_val, clk->name,
695                        clk->parent->name);
696                 return 0;
697         }
698
699         return clkr->div;
700 }
701
702 /**
703  * omap2_divisor_to_clksel() - turn clksel integer divisor into a field value
704  * @clk: OMAP struct clk to use
705  * @div: integer divisor to search for
706  *
707  * Given a struct clk of a rate-selectable clksel clock, and a clock divisor,
708  * find the corresponding register field value.  The return register value is
709  * the value before left-shifting.  Returns 0xffffffff on error
710  */
711 u32 omap2_divisor_to_clksel(struct clk *clk, u32 div)
712 {
713         const struct clksel *clks;
714         const struct clksel_rate *clkr;
715
716         /* should never happen */
717         WARN_ON(div == 0);
718
719         clks = omap2_get_clksel_by_parent(clk, clk->parent);
720         if (!clks)
721                 return 0;
722
723         for (clkr = clks->rates; clkr->div; clkr++) {
724                 if ((clkr->flags & cpu_mask) && (clkr->div == div))
725                         break;
726         }
727
728         if (!clkr->div) {
729                 printk(KERN_ERR "clock: Could not find divisor %d for "
730                        "clock %s parent %s\n", div, clk->name,
731                        clk->parent->name);
732                 return 0;
733         }
734
735         return clkr->val;
736 }
737
738 /**
739  * omap2_clksel_get_divisor - get current divider applied to parent clock.
740  * @clk: OMAP struct clk to use.
741  *
742  * Returns the integer divisor upon success or 0 on error.
743  */
744 u32 omap2_clksel_get_divisor(struct clk *clk)
745 {
746         u32 v;
747
748         if (!clk->clksel_mask)
749                 return 0;
750
751         v = _omap2_clk_read_reg(clk->clksel_reg, clk);
752         v &= clk->clksel_mask;
753         v >>= __ffs(clk->clksel_mask);
754
755         return omap2_clksel_to_divisor(clk, v);
756 }
757
758 int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
759 {
760         u32 v, field_val, validrate, new_div = 0;
761
762         if (!clk->clksel_mask)
763                 return -EINVAL;
764
765         validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
766         if (validrate != rate)
767                return -EINVAL;
768
769         field_val = omap2_divisor_to_clksel(clk, new_div);
770         if (field_val == ~0)
771                 return -EINVAL;
772
773         v = _omap2_clk_read_reg(clk->clksel_reg, clk);
774         v &= ~clk->clksel_mask;
775         v |= field_val << __ffs(clk->clksel_mask);
776         _omap2_clk_write_reg(v, clk->clksel_reg, clk);
777
778         wmb();
779
780         clk->rate = clk->parent->rate / new_div;
781
782         _omap2xxx_clk_commit(clk);
783
784         return 0;
785 }
786
787
788 /* Set the clock rate for a clock source */
789 int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
790 {
791         int ret = -EINVAL;
792
793         pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
794
795         if (clk->set_rate != NULL)
796                 ret = clk->set_rate(clk, rate);
797
798         return ret;
799 }
800
801 /*
802  * Converts encoded control register address into a full address
803  * On error, the return value (parent_div) will be 0.
804  */
805 static u32 _omap2_clksel_get_src_field(struct clk *src_clk, struct clk *clk,
806                                        u32 *field_val)
807 {
808         const struct clksel *clks;
809         const struct clksel_rate *clkr;
810
811         clks = omap2_get_clksel_by_parent(clk, src_clk);
812         if (!clks)
813                 return 0;
814
815         for (clkr = clks->rates; clkr->div; clkr++) {
816                 if (clkr->flags & (cpu_mask | DEFAULT_RATE))
817                         break; /* Found the default rate for this platform */
818         }
819
820         if (!clkr->div) {
821                 printk(KERN_ERR "clock: Could not find default rate for "
822                        "clock %s parent %s\n", clk->name,
823                        src_clk->parent->name);
824                 return 0;
825         }
826
827         /* Should never happen.  Add a clksel mask to the struct clk. */
828         WARN_ON(clk->clksel_mask == 0);
829
830         *field_val = clkr->val;
831
832         return clkr->div;
833 }
834
835 int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
836 {
837         u32 field_val, v, parent_div;
838
839         if (!clk->clksel)
840                 return -EINVAL;
841
842         parent_div = _omap2_clksel_get_src_field(new_parent, clk, &field_val);
843         if (!parent_div)
844                 return -EINVAL;
845
846         if (clk->usecount > 0)
847                 _omap2_clk_disable(clk);
848
849         /* Set new source value (previous dividers if any in effect) */
850         v = _omap2_clk_read_reg(clk->clksel_reg, clk);
851         v &= ~clk->clksel_mask;
852         v |= field_val << __ffs(clk->clksel_mask);
853         _omap2_clk_write_reg(v, clk->clksel_reg, clk);
854         wmb();
855
856         _omap2xxx_clk_commit(clk);
857
858         if (clk->usecount > 0)
859                 _omap2_clk_enable(clk);
860
861         clk->parent = new_parent;
862
863         /* CLKSEL clocks follow their parents' rates, divided by a divisor */
864         clk->rate = new_parent->rate;
865
866         if (parent_div > 0)
867                 clk->rate /= parent_div;
868
869         pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
870                  clk->name, clk->parent->name, clk->rate);
871
872         return 0;
873 }
874
875 struct clk *omap2_clk_get_parent(struct clk *clk)
876 {
877         return clk->parent;
878 }
879
880 /* DPLL rate rounding code */
881
882 /**
883  * omap2_dpll_set_rate_tolerance: set the error tolerance during rate rounding
884  * @clk: struct clk * of the DPLL
885  * @tolerance: maximum rate error tolerance
886  *
887  * Set the maximum DPLL rate error tolerance for the rate rounding
888  * algorithm.  The rate tolerance is an attempt to balance DPLL power
889  * saving (the least divider value "n") vs. rate fidelity (the least
890  * difference between the desired DPLL target rate and the rounded
891  * rate out of the algorithm).  So, increasing the tolerance is likely
892  * to decrease DPLL power consumption and increase DPLL rate error.
893  * Returns -EINVAL if provided a null clock ptr or a clk that is not a
894  * DPLL; or 0 upon success.
895  */
896 int omap2_dpll_set_rate_tolerance(struct clk *clk, unsigned int tolerance)
897 {
898         if (!clk || !clk->dpll_data)
899                 return -EINVAL;
900
901         clk->dpll_data->rate_tolerance = tolerance;
902
903         return 0;
904 }
905
906 static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
907                                             unsigned int m, unsigned int n)
908 {
909         unsigned long long num;
910
911         num = (unsigned long long)parent_rate * m;
912         do_div(num, n);
913         return num;
914 }
915
916 /*
917  * _dpll_test_mult - test a DPLL multiplier value
918  * @m: pointer to the DPLL m (multiplier) value under test
919  * @n: current DPLL n (divider) value under test
920  * @new_rate: pointer to storage for the resulting rounded rate
921  * @target_rate: the desired DPLL rate
922  * @parent_rate: the DPLL's parent clock rate
923  *
924  * This code tests a DPLL multiplier value, ensuring that the
925  * resulting rate will not be higher than the target_rate, and that
926  * the multiplier value itself is valid for the DPLL.  Initially, the
927  * integer pointed to by the m argument should be prescaled by
928  * multiplying by DPLL_SCALE_FACTOR.  The code will replace this with
929  * a non-scaled m upon return.  This non-scaled m will result in a
930  * new_rate as close as possible to target_rate (but not greater than
931  * target_rate) given the current (parent_rate, n, prescaled m)
932  * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
933  * non-scaled m attempted to underflow, which can allow the calling
934  * function to bail out early; or 0 upon success.
935  */
936 static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
937                            unsigned long target_rate,
938                            unsigned long parent_rate)
939 {
940         int r = 0, carry = 0;
941
942         /* Unscale m and round if necessary */
943         if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
944                 carry = 1;
945         *m = (*m / DPLL_SCALE_FACTOR) + carry;
946
947         /*
948          * The new rate must be <= the target rate to avoid programming
949          * a rate that is impossible for the hardware to handle
950          */
951         *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
952         if (*new_rate > target_rate) {
953                 (*m)--;
954                 *new_rate = 0;
955         }
956
957         /* Guard against m underflow */
958         if (*m < DPLL_MIN_MULTIPLIER) {
959                 *m = DPLL_MIN_MULTIPLIER;
960                 *new_rate = 0;
961                 r = DPLL_MULT_UNDERFLOW;
962         }
963
964         if (*new_rate == 0)
965                 *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
966
967         return r;
968 }
969
970 /**
971  * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
972  * @clk: struct clk * for a DPLL
973  * @target_rate: desired DPLL clock rate
974  *
975  * Given a DPLL, a desired target rate, and a rate tolerance, round
976  * the target rate to a possible, programmable rate for this DPLL.
977  * Rate tolerance is assumed to be set by the caller before this
978  * function is called.  Attempts to select the minimum possible n
979  * within the tolerance to reduce power consumption.  Stores the
980  * computed (m, n) in the DPLL's dpll_data structure so set_rate()
981  * will not need to call this (expensive) function again.  Returns ~0
982  * if the target rate cannot be rounded, either because the rate is
983  * too low or because the rate tolerance is set too tightly; or the
984  * rounded rate upon success.
985  */
986 long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
987 {
988         int m, n, r, e, scaled_max_m;
989         unsigned long scaled_rt_rp, new_rate;
990         int min_e = -1, min_e_m = -1, min_e_n = -1;
991         struct dpll_data *dd;
992
993         if (!clk || !clk->dpll_data)
994                 return ~0;
995
996         dd = clk->dpll_data;
997
998         pr_debug("clock: starting DPLL round_rate for clock %s, target rate "
999                  "%ld\n", clk->name, target_rate);
1000
1001         scaled_rt_rp = target_rate / (clk->parent->rate / DPLL_SCALE_FACTOR);
1002         scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
1003
1004         dd->last_rounded_rate = 0;
1005
1006         for (n = dd->min_divider; n <= dd->max_divider; n++) {
1007
1008                 /* Is the (input clk, divider) pair valid for the DPLL? */
1009                 r = _dpll_test_fint(clk, n);
1010                 if (r == DPLL_FINT_UNDERFLOW)
1011                         break;
1012                 else if (r == DPLL_FINT_INVALID)
1013                         continue;
1014
1015                 /* Compute the scaled DPLL multiplier, based on the divider */
1016                 m = scaled_rt_rp * n;
1017
1018                 /*
1019                  * Since we're counting n up, a m overflow means we
1020                  * can bail out completely (since as n increases in
1021                  * the next iteration, there's no way that m can
1022                  * increase beyond the current m)
1023                  */
1024                 if (m > scaled_max_m)
1025                         break;
1026
1027                 r = _dpll_test_mult(&m, n, &new_rate, target_rate,
1028                                     clk->parent->rate);
1029
1030                 /* m can't be set low enough for this n - try with a larger n */
1031                 if (r == DPLL_MULT_UNDERFLOW)
1032                         continue;
1033
1034                 e = target_rate - new_rate;
1035                 pr_debug("clock: n = %d: m = %d: rate error is %d "
1036                          "(new_rate = %ld)\n", n, m, e, new_rate);
1037
1038                 if (min_e == -1 ||
1039                     min_e >= (int)(abs(e) - dd->rate_tolerance)) {
1040                         min_e = e;
1041                         min_e_m = m;
1042                         min_e_n = n;
1043
1044                         pr_debug("clock: found new least error %d\n", min_e);
1045
1046                         /* We found good settings -- bail out now */
1047                         if (min_e <= dd->rate_tolerance)
1048                                 break;
1049                 }
1050         }
1051
1052         if (min_e < 0) {
1053                 pr_debug("clock: error: target rate or tolerance too low\n");
1054                 return ~0;
1055         }
1056
1057         dd->last_rounded_m = min_e_m;
1058         dd->last_rounded_n = min_e_n;
1059         dd->last_rounded_rate = _dpll_compute_new_rate(clk->parent->rate,
1060                                                        min_e_m,  min_e_n);
1061
1062         pr_debug("clock: final least error: e = %d, m = %d, n = %d\n",
1063                  min_e, min_e_m, min_e_n);
1064         pr_debug("clock: final rate: %ld  (target rate: %ld)\n",
1065                  dd->last_rounded_rate, target_rate);
1066
1067         return dd->last_rounded_rate;
1068 }
1069
1070 /*-------------------------------------------------------------------------
1071  * Omap2 clock reset and init functions
1072  *-------------------------------------------------------------------------*/
1073
1074 #ifdef CONFIG_OMAP_RESET_CLOCKS
1075 void omap2_clk_disable_unused(struct clk *clk)
1076 {
1077         u32 regval32, v;
1078
1079         v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
1080
1081         regval32 = _omap2_clk_read_reg(clk->enable_reg, clk);
1082         if ((regval32 & (1 << clk->enable_bit)) == v)
1083                 return;
1084
1085         printk(KERN_INFO "Disabling unused clock \"%s\"\n", clk->name);
1086         _omap2_clk_disable(clk);
1087 }
1088 #endif