2 * linux/arch/arm/mach-omap2/clock.c
4 * Copyright (C) 2005-2008 Texas Instruments, Inc.
5 * Copyright (C) 2004-2008 Nokia Corporation
8 * Richard Woodruff <r-woodruff2@ti.com>
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.
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>
25 #include <linux/bitops.h>
27 #include <mach/clock.h>
28 #include <mach/clockdomain.h>
29 #include <mach/sram.h>
31 #include <mach/prcm.h>
32 #include <mach/control.h>
33 #include <asm/div64.h>
35 #include <mach/sdrc.h>
39 #include "prm-regbits-24xx.h"
41 #include "cm-regbits-24xx.h"
42 #include "cm-regbits-34xx.h"
44 #define MAX_CLOCK_ENABLE_WAIT 100000
46 /* DPLL rate rounding: minimum DPLL multiplier, divider values */
47 #define DPLL_MIN_MULTIPLIER 1
48 #define DPLL_MIN_DIVIDER 1
50 /* Possible error results from _dpll_test_mult */
51 #define DPLL_MULT_UNDERFLOW (1 << 0)
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.
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))
64 /* Some OMAP2xxx CM_CLKSEL_PLL.ST_CORE_CLK bits - for omap2_get_dpll_rate() */
65 #define ST_CORE_CLK_REF 0x1
66 #define ST_CORE_CLK_32K 0x3
68 /* Bitmask to isolate the register type of clk.enable_reg */
69 #define PRCM_REGTYPE_MASK 0xf0
70 /* various CM register type options */
71 #define CM_FCLKEN_REGTYPE 0x00
72 #define CM_ICLKEN_REGTYPE 0x10
73 #define CM_IDLEST_REGTYPE 0x20
77 /*-------------------------------------------------------------------------
78 * OMAP2/3 specific clock functions
79 *-------------------------------------------------------------------------*/
82 * _omap2_clk_read_reg - read a clock register
85 * Given a struct clk *, returns the value of the clock's register.
87 static u32 _omap2_clk_read_reg(u16 reg_offset, struct clk *clk)
89 if (clk->prcm_mod & CLK_REG_IN_SCM)
90 return omap_ctrl_readl(reg_offset);
91 else if (clk->prcm_mod & CLK_REG_IN_PRM)
92 return prm_read_mod_reg(clk->prcm_mod & PRCM_MOD_ADDR_MASK,
95 return cm_read_mod_reg(clk->prcm_mod, reg_offset);
99 * _omap2_clk_write_reg - write a clock's register
100 * @v: value to write to the clock's enable_reg
103 * Given a register value @v and struct clk * @clk, writes the value of @v to
104 * the clock's enable register. No return value.
106 static void _omap2_clk_write_reg(u32 v, u16 reg_offset, struct clk *clk)
108 if (clk->prcm_mod & CLK_REG_IN_SCM)
109 omap_ctrl_writel(v, reg_offset);
110 else if (clk->prcm_mod & CLK_REG_IN_PRM)
111 prm_write_mod_reg(v, clk->prcm_mod & PRCM_MOD_ADDR_MASK,
114 cm_write_mod_reg(v, clk->prcm_mod, reg_offset);
119 * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
120 * @clk: OMAP clock struct ptr to use
122 * Convert a clockdomain name stored in a struct clk 'clk' into a
123 * clockdomain pointer, and save it into the struct clk. Intended to be
124 * called during clk_register(). No return value.
126 void omap2_init_clk_clkdm(struct clk *clk)
128 struct clockdomain *clkdm;
130 if (!clk->clkdm.name) {
131 pr_err("clock: %s: missing clockdomain", clk->name);
135 clkdm = clkdm_lookup(clk->clkdm.name);
137 pr_debug("clock: associated clk %s to clkdm %s\n",
138 clk->name, clk->clkdm.name);
139 clk->clkdm.ptr = clkdm;
141 pr_err("clock: %s: could not associate to clkdm %s\n",
142 clk->name, clk->clkdm.name);
147 * omap2_init_clksel_parent - set a clksel clk's parent field from the hardware
148 * @clk: OMAP clock struct ptr to use
150 * Given a pointer to a source-selectable struct clk, read the hardware
151 * register and determine what its parent is currently set to. Update the
152 * clk->parent field with the appropriate clk ptr.
154 void omap2_init_clksel_parent(struct clk *clk)
156 const struct clksel *clks;
157 const struct clksel_rate *clkr;
163 r = _omap2_clk_read_reg(clk->clksel_reg, clk);
164 r &= clk->clksel_mask;
165 r >>= __ffs(clk->clksel_mask);
167 for (clks = clk->clksel; clks->parent && !found; clks++) {
168 for (clkr = clks->rates; clkr->div && !found; clkr++) {
169 if ((clkr->flags & cpu_mask) && (clkr->val == r)) {
170 if (clk->parent != clks->parent) {
171 pr_debug("clock: inited %s parent "
173 clk->name, clks->parent->name,
175 clk->parent->name : "NULL"));
176 clk->parent = clks->parent;
184 printk(KERN_ERR "clock: init parent: could not find "
185 "regval %0x for clock %s\n", r, clk->name);
191 * omap2_get_dpll_rate - returns the current DPLL CLKOUT rate
192 * @clk: struct clk * of a DPLL
194 * DPLLs can be locked or bypassed - basically, enabled or disabled.
195 * When locked, the DPLL output depends on the M and N values. When
196 * bypassed, on OMAP2xxx, the output rate is either the 32KiHz clock
197 * or sys_clk. Bypass rates on OMAP3 depend on the DPLL: DPLLs 1 and
198 * 2 are bypassed with dpll1_fclk and dpll2_fclk respectively
199 * (generated by DPLL3), while DPLL 3, 4, and 5 bypass rates are sys_clk.
200 * Returns the current DPLL CLKOUT rate (*not* CLKOUTX2) if the DPLL is
201 * locked, or the appropriate bypass rate if the DPLL is bypassed, or 0
202 * if the clock @clk is not a DPLL.
204 u32 omap2_get_dpll_rate(struct clk *clk)
207 u32 dpll_mult, dpll_div, v;
208 struct dpll_data *dd;
214 /* Return bypass rate if DPLL is bypassed */
215 v = cm_read_mod_reg(clk->prcm_mod, dd->idlest_reg);
216 v &= dd->idlest_mask;
217 v >>= __ffs(dd->idlest_mask);
218 if (cpu_is_omap24xx()) {
220 if (v == ST_CORE_CLK_REF)
221 return clk->parent->rate; /* sys_clk */
222 else if (v == ST_CORE_CLK_32K)
225 } else if (cpu_is_omap34xx()) {
228 return dd->bypass_clk->rate;
232 v = cm_read_mod_reg(clk->prcm_mod, dd->mult_div1_reg);
233 dpll_mult = v & dd->mult_mask;
234 dpll_mult >>= __ffs(dd->mult_mask);
235 dpll_div = v & dd->div1_mask;
236 dpll_div >>= __ffs(dd->div1_mask);
238 dpll_clk = (long long)clk->parent->rate * dpll_mult;
239 do_div(dpll_clk, dpll_div + 1);
245 * Used for clocks that have the same value as the parent clock,
246 * divided by some factor
248 void omap2_fixed_divisor_recalc(struct clk *clk)
250 WARN_ON(!clk->fixed_div);
252 clk->rate = clk->parent->rate / clk->fixed_div;
254 if (clk->flags & RATE_PROPAGATES)
259 * omap2_wait_clock_ready - wait for clock to enable
260 * @prcm_mod: CM submodule offset from CM_BASE (e.g., "MPU_MOD")
261 * @reg_index: offset of CM register address from prcm_mod
262 * @mask: value to mask against to determine if the clock is active
263 * @name: name of the clock (for printk)
265 * Returns 1 if the clock enabled in time, or 0 if it failed to enable
266 * in roughly MAX_CLOCK_ENABLE_WAIT microseconds.
268 int omap2_wait_clock_ready(s16 prcm_mod, u16 reg_index, u32 mask,
274 * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
275 * 34xx reverses this, just to keep us on our toes
277 if (cpu_mask & (RATE_IN_242X | RATE_IN_243X))
279 else if (cpu_mask & RATE_IN_343X)
283 while (((cm_read_mod_reg(prcm_mod, reg_index) & mask) != ena) &&
284 (i++ < MAX_CLOCK_ENABLE_WAIT)) {
288 if (i < MAX_CLOCK_ENABLE_WAIT)
289 pr_debug("Clock %s stable after %d loops\n", name, i);
291 printk(KERN_ERR "Clock %s didn't enable in %d tries\n",
292 name, MAX_CLOCK_ENABLE_WAIT);
294 return (i < MAX_CLOCK_ENABLE_WAIT) ? 1 : 0;
299 * omap2_clk_wait_ready - wait for a OMAP module to come out of target idle
300 * @clk: struct clk * recently enabled to indicate the module to test
302 * Wait for an OMAP module with a target idle state bit to come out of
303 * idle once both its interface clock and primary functional clock are
304 * both enabled. Any register read or write to the device before it
305 * returns from idle will cause an abort. Not all modules have target
306 * idle state bits (for example, DSS and CAM on OMAP24xx); so we don't
307 * wait for those. No return value.
309 * We don't need special code here for INVERT_ENABLE for the time
310 * being since INVERT_ENABLE only applies to clocks enabled by
313 * REVISIT: This function is misnamed: it should be something like
314 * "omap2_module_wait_ready", and in the long-term, it does not belong
315 * in the clock framework. It also shouldn't be doing register
316 * arithmetic to determine the companion clock.
318 static void omap2_clk_wait_ready(struct clk *clk)
320 u16 other_reg, idlest_reg;
323 if (!(clk->flags & WAIT_READY))
326 /* If we are enabling an iclk, also test the fclk; and vice versa */
327 other_bit = 1 << clk->enable_bit;
328 other_reg = clk->enable_reg & ~PRCM_REGTYPE_MASK;
330 if (clk->enable_reg & CM_ICLKEN_REGTYPE)
331 other_reg |= CM_FCLKEN_REGTYPE;
333 other_reg |= CM_ICLKEN_REGTYPE;
335 /* Ensure functional and interface clocks are running. */
336 if (!(cm_read_mod_reg(clk->prcm_mod, other_reg) & other_bit))
339 idlest_reg = other_reg & ~PRCM_REGTYPE_MASK;
340 idlest_reg |= CM_IDLEST_REGTYPE;
342 omap2_wait_clock_ready(clk->prcm_mod, idlest_reg, 1 << clk->idlest_bit,
346 /* Enables clock without considering parent dependencies or use count
347 * REVISIT: Maybe change this to use clk->enable like on omap1?
349 static int _omap2_clk_enable(struct clk *clk)
353 if (clk->flags & (ALWAYS_ENABLED | PARENT_CONTROLS_CLOCK))
357 return clk->enable(clk);
359 v = _omap2_clk_read_reg(clk->enable_reg, clk);
360 if (clk->flags & INVERT_ENABLE)
361 v &= ~(1 << clk->enable_bit);
363 v |= (1 << clk->enable_bit);
364 _omap2_clk_write_reg(v, clk->enable_reg, clk);
367 omap2_clk_wait_ready(clk);
372 /* Disables clock without considering parent dependencies or use count */
373 static void _omap2_clk_disable(struct clk *clk)
377 if (clk->flags & (ALWAYS_ENABLED | PARENT_CONTROLS_CLOCK))
385 v = _omap2_clk_read_reg(clk->enable_reg, clk);
386 if (clk->flags & INVERT_ENABLE)
387 v |= (1 << clk->enable_bit);
389 v &= ~(1 << clk->enable_bit);
390 _omap2_clk_write_reg(v, clk->enable_reg, clk);
394 void omap2_clk_disable(struct clk *clk)
396 if (clk->usecount > 0 && !(--clk->usecount)) {
397 _omap2_clk_disable(clk);
399 omap2_clk_disable(clk->parent);
401 omap2_clkdm_clk_disable(clk->clkdm.ptr, clk);
406 int omap2_clk_enable(struct clk *clk)
410 if (clk->usecount++ == 0) {
412 ret = omap2_clk_enable(clk->parent);
420 omap2_clkdm_clk_enable(clk->clkdm.ptr, clk);
422 ret = _omap2_clk_enable(clk);
426 omap2_clkdm_clk_disable(clk->clkdm.ptr, clk);
429 omap2_clk_disable(clk->parent);
439 * Used for clocks that are part of CLKSEL_xyz governed clocks.
440 * REVISIT: Maybe change to use clk->enable() functions like on omap1?
442 void omap2_clksel_recalc(struct clk *clk)
446 pr_debug("clock: recalc'ing clksel clk %s\n", clk->name);
448 div = omap2_clksel_get_divisor(clk);
452 if (clk->rate == (clk->parent->rate / div))
454 clk->rate = clk->parent->rate / div;
456 pr_debug("clock: new clock rate is %ld (div %d)\n", clk->rate, div);
458 if (clk->flags & RATE_PROPAGATES)
463 * omap2_get_clksel_by_parent - return clksel struct for a given clk & parent
464 * @clk: OMAP struct clk ptr to inspect
465 * @src_clk: OMAP struct clk ptr of the parent clk to search for
467 * Scan the struct clksel array associated with the clock to find
468 * the element associated with the supplied parent clock address.
469 * Returns a pointer to the struct clksel on success or NULL on error.
471 static const struct clksel *omap2_get_clksel_by_parent(struct clk *clk,
474 const struct clksel *clks;
479 for (clks = clk->clksel; clks->parent; clks++) {
480 if (clks->parent == src_clk)
481 break; /* Found the requested parent */
485 printk(KERN_ERR "clock: Could not find parent clock %s in "
486 "clksel array of clock %s\n", src_clk->name,
495 * omap2_clksel_round_rate_div - find divisor for the given clock and rate
496 * @clk: OMAP struct clk to use
497 * @target_rate: desired clock rate
498 * @new_div: ptr to where we should store the divisor
500 * Finds 'best' divider value in an array based on the source and target
501 * rates. The divider array must be sorted with smallest divider first.
502 * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
503 * they are only settable as part of virtual_prcm set.
505 * Returns the rounded clock rate or returns 0xffffffff on error.
507 u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
510 unsigned long test_rate;
511 const struct clksel *clks;
512 const struct clksel_rate *clkr;
515 printk(KERN_INFO "clock: clksel_round_rate_div: %s target_rate %ld\n",
516 clk->name, target_rate);
520 clks = omap2_get_clksel_by_parent(clk, clk->parent);
524 for (clkr = clks->rates; clkr->div; clkr++) {
525 if (!(clkr->flags & cpu_mask))
529 if (clkr->div <= last_div)
530 printk(KERN_ERR "clock: clksel_rate table not sorted "
531 "for clock %s", clk->name);
533 last_div = clkr->div;
535 test_rate = clk->parent->rate / clkr->div;
537 if (test_rate <= target_rate)
538 break; /* found it */
542 printk(KERN_ERR "clock: Could not find divisor for target "
543 "rate %ld for clock %s parent %s\n", target_rate,
544 clk->name, clk->parent->name);
548 *new_div = clkr->div;
550 printk(KERN_INFO "clock: new_div = %d, new_rate = %ld\n", *new_div,
551 (clk->parent->rate / clkr->div));
553 return (clk->parent->rate / clkr->div);
557 * omap2_clksel_round_rate - find rounded rate for the given clock and rate
558 * @clk: OMAP struct clk to use
559 * @target_rate: desired clock rate
561 * Compatibility wrapper for OMAP clock framework
562 * Finds best target rate based on the source clock and possible dividers.
563 * rates. The divider array must be sorted with smallest divider first.
564 * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
565 * they are only settable as part of virtual_prcm set.
567 * Returns the rounded clock rate or returns 0xffffffff on error.
569 long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
573 return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
577 /* Given a clock and a rate apply a clock specific rounding function */
578 long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
580 if (clk->round_rate != NULL)
581 return clk->round_rate(clk, rate);
583 if (clk->flags & RATE_FIXED)
584 printk(KERN_ERR "clock: generic omap2_clk_round_rate called "
585 "on fixed-rate clock %s\n", clk->name);
591 * omap2_clksel_to_divisor() - turn clksel field value into integer divider
592 * @clk: OMAP struct clk to use
593 * @field_val: register field value to find
595 * Given a struct clk of a rate-selectable clksel clock, and a register field
596 * value to search for, find the corresponding clock divisor. The register
597 * field value should be pre-masked and shifted down so the LSB is at bit 0
598 * before calling. Returns 0 on error
600 u32 omap2_clksel_to_divisor(struct clk *clk, u32 field_val)
602 const struct clksel *clks;
603 const struct clksel_rate *clkr;
605 clks = omap2_get_clksel_by_parent(clk, clk->parent);
609 for (clkr = clks->rates; clkr->div; clkr++) {
610 if ((clkr->flags & cpu_mask) && (clkr->val == field_val))
615 printk(KERN_ERR "clock: Could not find fieldval %d for "
616 "clock %s parent %s\n", field_val, clk->name,
625 * omap2_divisor_to_clksel() - turn clksel integer divisor into a field value
626 * @clk: OMAP struct clk to use
627 * @div: integer divisor to search for
629 * Given a struct clk of a rate-selectable clksel clock, and a clock divisor,
630 * find the corresponding register field value. The return register value is
631 * the value before left-shifting. Returns 0xffffffff on error
633 u32 omap2_divisor_to_clksel(struct clk *clk, u32 div)
635 const struct clksel *clks;
636 const struct clksel_rate *clkr;
638 /* should never happen */
641 clks = omap2_get_clksel_by_parent(clk, clk->parent);
645 for (clkr = clks->rates; clkr->div; clkr++) {
646 if ((clkr->flags & cpu_mask) && (clkr->div == div))
651 printk(KERN_ERR "clock: Could not find divisor %d for "
652 "clock %s parent %s\n", div, clk->name,
661 * omap2_clksel_get_divisor - get current divider applied to parent clock.
662 * @clk: OMAP struct clk to use.
664 * Returns the integer divisor upon success or 0 on error.
666 u32 omap2_clksel_get_divisor(struct clk *clk)
670 if (!clk->clksel_mask)
673 v = _omap2_clk_read_reg(clk->clksel_reg, clk);
674 v &= clk->clksel_mask;
675 v >>= __ffs(clk->clksel_mask);
677 return omap2_clksel_to_divisor(clk, v);
680 int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
682 u32 v, field_val, validrate, new_div = 0;
684 if (!clk->clksel_mask)
687 validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
688 if (validrate != rate)
691 field_val = omap2_divisor_to_clksel(clk, new_div);
695 v = _omap2_clk_read_reg(clk->clksel_reg, clk);
696 v &= ~clk->clksel_mask;
697 v |= field_val << __ffs(clk->clksel_mask);
698 _omap2_clk_write_reg(v, clk->clksel_reg, clk);
702 clk->rate = clk->parent->rate / new_div;
704 if (clk->flags & DELAYED_APP && cpu_is_omap24xx()) {
705 prm_write_mod_reg(OMAP24XX_VALID_CONFIG,
706 OMAP24XX_GR_MOD, OMAP24XX_PRCM_CLKCFG_CTRL_OFFSET);
714 /* Set the clock rate for a clock source */
715 int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
719 pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
721 /* CONFIG_PARTICIPANT clocks are changed only in sets via the
722 rate table mechanism, driven by mpu_speed */
723 if (clk->flags & CONFIG_PARTICIPANT)
726 /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
727 if (clk->set_rate != NULL)
728 ret = clk->set_rate(clk, rate);
730 if (ret == 0 && (clk->flags & RATE_PROPAGATES))
737 * Converts encoded control register address into a full address
738 * On error, the return value (parent_div) will be 0.
740 static u32 _omap2_clksel_get_src_field(struct clk *src_clk, struct clk *clk,
743 const struct clksel *clks;
744 const struct clksel_rate *clkr;
746 clks = omap2_get_clksel_by_parent(clk, src_clk);
750 for (clkr = clks->rates; clkr->div; clkr++) {
751 if (clkr->flags & (cpu_mask | DEFAULT_RATE))
752 break; /* Found the default rate for this platform */
756 printk(KERN_ERR "clock: Could not find default rate for "
757 "clock %s parent %s\n", clk->name,
758 src_clk->parent->name);
762 /* Should never happen. Add a clksel mask to the struct clk. */
763 WARN_ON(clk->clksel_mask == 0);
765 *field_val = clkr->val;
770 int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
772 u32 field_val, v, parent_div;
774 if (clk->flags & CONFIG_PARTICIPANT)
780 parent_div = _omap2_clksel_get_src_field(new_parent, clk, &field_val);
784 if (clk->usecount > 0)
785 _omap2_clk_disable(clk);
787 /* Set new source value (previous dividers if any in effect) */
788 v = _omap2_clk_read_reg(clk->clksel_reg, clk);
789 v &= ~clk->clksel_mask;
790 v |= field_val << __ffs(clk->clksel_mask);
791 _omap2_clk_write_reg(v, clk->clksel_reg, clk);
794 if (clk->flags & DELAYED_APP && cpu_is_omap24xx()) {
795 prm_write_mod_reg(OMAP24XX_VALID_CONFIG,
796 OMAP24XX_GR_MOD, OMAP24XX_PRCM_CLKCFG_CTRL_OFFSET);
800 if (clk->usecount > 0)
801 _omap2_clk_enable(clk);
803 clk->parent = new_parent;
805 /* CLKSEL clocks follow their parents' rates, divided by a divisor */
806 clk->rate = new_parent->rate;
809 clk->rate /= parent_div;
811 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
812 clk->name, clk->parent->name, clk->rate);
814 if (clk->flags & RATE_PROPAGATES)
820 /* DPLL rate rounding code */
823 * omap2_dpll_set_rate_tolerance: set the error tolerance during rate rounding
824 * @clk: struct clk * of the DPLL
825 * @tolerance: maximum rate error tolerance
827 * Set the maximum DPLL rate error tolerance for the rate rounding
828 * algorithm. The rate tolerance is an attempt to balance DPLL power
829 * saving (the least divider value "n") vs. rate fidelity (the least
830 * difference between the desired DPLL target rate and the rounded
831 * rate out of the algorithm). So, increasing the tolerance is likely
832 * to decrease DPLL power consumption and increase DPLL rate error.
833 * Returns -EINVAL if provided a null clock ptr or a clk that is not a
834 * DPLL; or 0 upon success.
836 int omap2_dpll_set_rate_tolerance(struct clk *clk, unsigned int tolerance)
838 if (!clk || !clk->dpll_data)
841 clk->dpll_data->rate_tolerance = tolerance;
846 static unsigned long _dpll_compute_new_rate(unsigned long parent_rate,
847 unsigned int m, unsigned int n)
849 unsigned long long num;
851 num = (unsigned long long)parent_rate * m;
857 * _dpll_test_mult - test a DPLL multiplier value
858 * @m: pointer to the DPLL m (multiplier) value under test
859 * @n: current DPLL n (divider) value under test
860 * @new_rate: pointer to storage for the resulting rounded rate
861 * @target_rate: the desired DPLL rate
862 * @parent_rate: the DPLL's parent clock rate
864 * This code tests a DPLL multiplier value, ensuring that the
865 * resulting rate will not be higher than the target_rate, and that
866 * the multiplier value itself is valid for the DPLL. Initially, the
867 * integer pointed to by the m argument should be prescaled by
868 * multiplying by DPLL_SCALE_FACTOR. The code will replace this with
869 * a non-scaled m upon return. This non-scaled m will result in a
870 * new_rate as close as possible to target_rate (but not greater than
871 * target_rate) given the current (parent_rate, n, prescaled m)
872 * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
873 * non-scaled m attempted to underflow, which can allow the calling
874 * function to bail out early; or 0 upon success.
876 static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
877 unsigned long target_rate,
878 unsigned long parent_rate)
880 int flags = 0, carry = 0;
882 /* Unscale m and round if necessary */
883 if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
885 *m = (*m / DPLL_SCALE_FACTOR) + carry;
888 * The new rate must be <= the target rate to avoid programming
889 * a rate that is impossible for the hardware to handle
891 *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
892 if (*new_rate > target_rate) {
897 /* Guard against m underflow */
898 if (*m < DPLL_MIN_MULTIPLIER) {
899 *m = DPLL_MIN_MULTIPLIER;
901 flags = DPLL_MULT_UNDERFLOW;
905 *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
911 * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
912 * @clk: struct clk * for a DPLL
913 * @target_rate: desired DPLL clock rate
915 * Given a DPLL, a desired target rate, and a rate tolerance, round
916 * the target rate to a possible, programmable rate for this DPLL.
917 * Rate tolerance is assumed to be set by the caller before this
918 * function is called. Attempts to select the minimum possible n
919 * within the tolerance to reduce power consumption. Stores the
920 * computed (m, n) in the DPLL's dpll_data structure so set_rate()
921 * will not need to call this (expensive) function again. Returns ~0
922 * if the target rate cannot be rounded, either because the rate is
923 * too low or because the rate tolerance is set too tightly; or the
924 * rounded rate upon success.
926 long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
928 int m, n, r, e, scaled_max_m;
929 unsigned long scaled_rt_rp, new_rate;
930 int min_e = -1, min_e_m = -1, min_e_n = -1;
931 struct dpll_data *dd;
933 if (!clk || !clk->dpll_data)
938 pr_debug("clock: starting DPLL round_rate for clock %s, target rate "
939 "%ld\n", clk->name, target_rate);
941 scaled_rt_rp = target_rate / (clk->parent->rate / DPLL_SCALE_FACTOR);
942 scaled_max_m = dd->max_multiplier * DPLL_SCALE_FACTOR;
944 dd->last_rounded_rate = 0;
946 for (n = dd->max_divider; n >= DPLL_MIN_DIVIDER; n--) {
948 /* Compute the scaled DPLL multiplier, based on the divider */
949 m = scaled_rt_rp * n;
952 * Since we're counting n down, a m overflow means we can
953 * can immediately skip to the next n
955 if (m > scaled_max_m)
958 r = _dpll_test_mult(&m, n, &new_rate, target_rate,
961 e = target_rate - new_rate;
962 pr_debug("clock: n = %d: m = %d: rate error is %d "
963 "(new_rate = %ld)\n", n, m, e, new_rate);
966 min_e >= (int)(abs(e) - dd->rate_tolerance)) {
971 pr_debug("clock: found new least error %d\n", min_e);
975 * Since we're counting n down, a m underflow means we
976 * can bail out completely (since as n decreases in
977 * the next iteration, there's no way that m can
978 * increase beyond the current m)
980 if (r & DPLL_MULT_UNDERFLOW)
985 pr_debug("clock: error: target rate or tolerance too low\n");
989 dd->last_rounded_m = min_e_m;
990 dd->last_rounded_n = min_e_n;
991 dd->last_rounded_rate = _dpll_compute_new_rate(clk->parent->rate,
994 pr_debug("clock: final least error: e = %d, m = %d, n = %d\n",
995 min_e, min_e_m, min_e_n);
996 pr_debug("clock: final rate: %ld (target rate: %ld)\n",
997 dd->last_rounded_rate, target_rate);
999 return dd->last_rounded_rate;
1002 /*-------------------------------------------------------------------------
1003 * Omap2 clock reset and init functions
1004 *-------------------------------------------------------------------------*/
1006 #ifdef CONFIG_OMAP_RESET_CLOCKS
1007 void omap2_clk_disable_unused(struct clk *clk)
1011 v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
1013 regval32 = _omap2_clk_read_reg(clk->enable_reg, clk);
1014 if ((regval32 & (1 << clk->enable_bit)) == v)
1017 printk(KERN_INFO "Disabling unused clock \"%s\"\n", clk->name);
1018 _omap2_clk_disable(clk);