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