]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - arch/arm/mach-omap2/clock.c
Merge unstable branch 'omap-rmk'
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / clock.c
index ab9fc57d25f16e56abc6b59ec9559ebb77bc2ee4..57c4405ccedaa1e7f5a8e820d894a0f5afdf03ff 100644 (file)
 
 #include <asm/io.h>
 
-#include <asm/arch/clock.h>
-#include <asm/arch/sram.h>
-#include <asm/arch/cpu.h>
+#include <mach/clock.h>
+#include <mach/clockdomain.h>
+#include <mach/sram.h>
+#include <mach/cpu.h>
 #include <asm/div64.h>
 
 #include "memory.h"
 
 #define MAX_CLOCK_ENABLE_WAIT          100000
 
+/* DPLL rate rounding: minimum DPLL multiplier, divider values */
+#define DPLL_MIN_MULTIPLIER            1
+#define DPLL_MIN_DIVIDER               1
+
+/* Possible error results from _dpll_test_mult */
+#define DPLL_MULT_UNDERFLOW            (1 << 0)
+
+/*
+ * Scale factor to mitigate roundoff errors in DPLL rate rounding.
+ * The higher the scale factor, the greater the risk of arithmetic overflow,
+ * but the closer the rounded rate to the target rate.  DPLL_SCALE_FACTOR
+ * must be a power of DPLL_SCALE_BASE.
+ */
+#define DPLL_SCALE_FACTOR              64
+#define DPLL_SCALE_BASE                        2
+#define DPLL_ROUNDING_VAL              ((DPLL_SCALE_BASE / 2) * \
+                                        (DPLL_SCALE_FACTOR / DPLL_SCALE_BASE))
+
 u8 cpu_mask;
 
 /*-------------------------------------------------------------------------
- * Omap2 specific clock functions
+ * OMAP2/3 specific clock functions
  *-------------------------------------------------------------------------*/
 
+/**
+ * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
+ * @clk: OMAP clock struct ptr to use
+ *
+ * Convert a clockdomain name stored in a struct clk 'clk' into a
+ * clockdomain pointer, and save it into the struct clk.  Intended to be
+ * called during clk_register().  No return value.
+ */
+void omap2_init_clk_clkdm(struct clk *clk)
+{
+       struct clockdomain *clkdm;
+
+       if (!clk->clkdm_name)
+               return;
+
+       clkdm = clkdm_lookup(clk->clkdm_name);
+       if (clkdm) {
+               pr_debug("clock: associated clk %s to clkdm %s\n",
+                        clk->name, clk->clkdm_name);
+               clk->clkdm = clkdm;
+       } else {
+               pr_debug("clock: could not associate clk %s to "
+                        "clkdm %s\n", clk->name, clk->clkdm_name);
+       }
+}
+
 /**
  * omap2_init_clksel_parent - set a clksel clk's parent field from the hardware
  * @clk: OMAP clock struct ptr to use
@@ -95,7 +140,7 @@ u32 omap2_get_dpll_rate(struct clk *clk)
 {
        long long dpll_clk;
        u32 dpll_mult, dpll_div, dpll;
-       const struct dpll_data *dd;
+       struct dpll_data *dd;
 
        dd = clk->dpll_data;
        /* REVISIT: What do we return on error? */
@@ -233,7 +278,7 @@ int _omap2_clk_enable(struct clk *clk)
        if (clk->enable)
                return clk->enable(clk);
 
-       if (unlikely(clk->enable_reg == 0)) {
+       if (unlikely(clk->enable_reg == NULL)) {
                printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
                       clk->name);
                return 0; /* REVISIT: -EINVAL */
@@ -265,7 +310,7 @@ void _omap2_clk_disable(struct clk *clk)
                return;
        }
 
-       if (clk->enable_reg == 0) {
+       if (clk->enable_reg == NULL) {
                /*
                 * 'Independent' here refers to a clock which is not
                 * controlled by its parent.
@@ -290,6 +335,9 @@ void omap2_clk_disable(struct clk *clk)
                _omap2_clk_disable(clk);
                if (likely((u32)clk->parent))
                        omap2_clk_disable(clk->parent);
+               if (clk->clkdm)
+                       omap2_clkdm_clk_disable(clk->clkdm, clk);
+
        }
 }
 
@@ -306,11 +354,19 @@ int omap2_clk_enable(struct clk *clk)
                        return ret;
                }
 
+               if (clk->clkdm)
+                       omap2_clkdm_clk_enable(clk->clkdm, clk);
+
                ret = _omap2_clk_enable(clk);
 
-               if (unlikely(ret != 0) && clk->parent) {
-                       omap2_clk_disable(clk->parent);
-                       clk->usecount--;
+               if (unlikely(ret != 0)) {
+                       if (clk->clkdm)
+                               omap2_clkdm_clk_disable(clk->clkdm, clk);
+
+                       if (clk->parent) {
+                               omap2_clk_disable(clk->parent);
+                               clk->usecount--;
+                       }
                }
        }
 
@@ -459,7 +515,7 @@ long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
 /* Given a clock and a rate apply a clock specific rounding function */
 long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
 {
-       if (clk->round_rate != 0)
+       if (clk->round_rate != NULL)
                return clk->round_rate(clk, rate);
 
        if (clk->flags & RATE_FIXED)
@@ -548,7 +604,7 @@ u32 omap2_divisor_to_clksel(struct clk *clk, u32 div)
  */
 void __iomem *omap2_get_clksel(struct clk *clk, u32 *field_mask)
 {
-       if (unlikely((clk->clksel_reg == 0) || (clk->clksel_mask == 0)))
+       if (unlikely((clk->clksel_reg == NULL) || (clk->clksel_mask == NULL)))
                return NULL;
 
        *field_mask = clk->clksel_mask;
@@ -568,7 +624,7 @@ u32 omap2_clksel_get_divisor(struct clk *clk)
        void __iomem *div_addr;
 
        div_addr = omap2_get_clksel(clk, &field_mask);
-       if (div_addr == 0)
+       if (div_addr == NULL)
                return 0;
 
        field_val = __raw_readl(div_addr) & field_mask;
@@ -587,7 +643,7 @@ int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
                return -EINVAL;
 
        div_addr = omap2_get_clksel(clk, &field_mask);
-       if (div_addr == 0)
+       if (div_addr == NULL)
                return -EINVAL;
 
        field_val = omap2_divisor_to_clksel(clk, new_div);
@@ -603,7 +659,8 @@ int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
        clk->rate = clk->parent->rate / new_div;
 
        if (clk->flags & DELAYED_APP && cpu_is_omap24xx()) {
-               __raw_writel(OMAP24XX_VALID_CONFIG, OMAP24XX_PRCM_CLKCFG_CTRL);
+               prm_write_mod_reg(OMAP24XX_VALID_CONFIG,
+                       OMAP24XX_GR_MOD, OMAP24XX_PRCM_CLKCFG_CTRL_OFFSET);
                wmb();
        }
 
@@ -624,7 +681,7 @@ int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
                return -EINVAL;
 
        /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
-       if (clk->set_rate != 0)
+       if (clk->set_rate != NULL)
                ret = clk->set_rate(clk, rate);
 
        if (unlikely(ret == 0 && (clk->flags & RATE_PROPAGATES)))
@@ -645,7 +702,7 @@ static u32 omap2_clksel_get_src_field(void __iomem **src_addr,
        const struct clksel_rate *clkr;
 
        *parent_div = 0;
-       *src_addr = 0;
+       *src_addr = NULL;
 
        clks = omap2_get_clksel_by_parent(clk, src_clk);
        if (clks == NULL)
@@ -686,7 +743,7 @@ int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
 
        field_val = omap2_clksel_get_src_field(&src_addr, new_parent,
                                               &field_mask, clk, &parent_div);
-       if (src_addr == 0)
+       if (src_addr == NULL)
                return -EINVAL;
 
        if (clk->usecount > 0)
@@ -723,6 +780,184 @@ int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
        return 0;
 }
 
+/* DPLL rate rounding code */
+
+/**
+ * omap2_dpll_set_rate_tolerance: set the error tolerance during rate rounding
+ * @clk: struct clk * of the DPLL
+ * @tolerance: maximum rate error tolerance
+ *
+ * Set the maximum DPLL rate error tolerance for the rate rounding
+ * algorithm.  The rate tolerance is an attempt to balance DPLL power
+ * saving (the least divider value "n") vs. rate fidelity (the least
+ * difference between the desired DPLL target rate and the rounded
+ * rate out of the algorithm).  So, increasing the tolerance is likely
+ * to decrease DPLL power consumption and increase DPLL rate error.
+ * Returns -EINVAL if provided a null clock ptr or a clk that is not a
+ * DPLL; or 0 upon success.
+ */
+int omap2_dpll_set_rate_tolerance(struct clk *clk, unsigned int tolerance)
+{
+       if (!clk || !clk->dpll_data)
+               return -EINVAL;
+
+       clk->dpll_data->rate_tolerance = tolerance;
+
+       return 0;
+}
+
+static unsigned long _dpll_compute_new_rate(unsigned long parent_rate, unsigned int m, unsigned int n)
+{
+       unsigned long long num;
+
+       num = (unsigned long long)parent_rate * m;
+       do_div(num, n);
+       return num;
+}
+
+/*
+ * _dpll_test_mult - test a DPLL multiplier value
+ * @m: pointer to the DPLL m (multiplier) value under test
+ * @n: current DPLL n (divider) value under test
+ * @new_rate: pointer to storage for the resulting rounded rate
+ * @target_rate: the desired DPLL rate
+ * @parent_rate: the DPLL's parent clock rate
+ *
+ * This code tests a DPLL multiplier value, ensuring that the
+ * resulting rate will not be higher than the target_rate, and that
+ * the multiplier value itself is valid for the DPLL.  Initially, the
+ * integer pointed to by the m argument should be prescaled by
+ * multiplying by DPLL_SCALE_FACTOR.  The code will replace this with
+ * a non-scaled m upon return.  This non-scaled m will result in a
+ * new_rate as close as possible to target_rate (but not greater than
+ * target_rate) given the current (parent_rate, n, prescaled m)
+ * triple. Returns DPLL_MULT_UNDERFLOW in the event that the
+ * non-scaled m attempted to underflow, which can allow the calling
+ * function to bail out early; or 0 upon success.
+ */
+static int _dpll_test_mult(int *m, int n, unsigned long *new_rate,
+                          unsigned long target_rate,
+                          unsigned long parent_rate)
+{
+       int flags = 0, carry = 0;
+
+       /* Unscale m and round if necessary */
+       if (*m % DPLL_SCALE_FACTOR >= DPLL_ROUNDING_VAL)
+               carry = 1;
+       *m = (*m / DPLL_SCALE_FACTOR) + carry;
+
+       /*
+        * The new rate must be <= the target rate to avoid programming
+        * a rate that is impossible for the hardware to handle
+        */
+       *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
+       if (*new_rate > target_rate) {
+               (*m)--;
+               *new_rate = 0;
+       }
+
+       /* Guard against m underflow */
+       if (*m < DPLL_MIN_MULTIPLIER) {
+               *m = DPLL_MIN_MULTIPLIER;
+               *new_rate = 0;
+               flags = DPLL_MULT_UNDERFLOW;
+       }
+
+       if (*new_rate == 0)
+               *new_rate = _dpll_compute_new_rate(parent_rate, *m, n);
+
+       return flags;
+}
+
+/**
+ * omap2_dpll_round_rate - round a target rate for an OMAP DPLL
+ * @clk: struct clk * for a DPLL
+ * @target_rate: desired DPLL clock rate
+ *
+ * Given a DPLL, a desired target rate, and a rate tolerance, round
+ * the target rate to a possible, programmable rate for this DPLL.
+ * Rate tolerance is assumed to be set by the caller before this
+ * function is called.  Attempts to select the minimum possible n
+ * within the tolerance to reduce power consumption.  Stores the
+ * computed (m, n) in the DPLL's dpll_data structure so set_rate()
+ * will not need to call this (expensive) function again.  Returns ~0
+ * if the target rate cannot be rounded, either because the rate is
+ * too low or because the rate tolerance is set too tightly; or the
+ * rounded rate upon success.
+ */
+long omap2_dpll_round_rate(struct clk *clk, unsigned long target_rate)
+{
+       int m, n, r, e, scaled_max_m;
+       unsigned long scaled_rt_rp, new_rate;
+       int min_e = -1, min_e_m = -1, min_e_n = -1;
+
+       if (!clk || !clk->dpll_data)
+               return ~0;
+
+       pr_debug("clock: starting DPLL round_rate for clock %s, target rate "
+                "%ld\n", clk->name, target_rate);
+
+       scaled_rt_rp = target_rate / (clk->parent->rate / DPLL_SCALE_FACTOR);
+       scaled_max_m = clk->dpll_data->max_multiplier * DPLL_SCALE_FACTOR;
+
+       clk->dpll_data->last_rounded_rate = 0;
+
+       for (n = clk->dpll_data->max_divider; n >= DPLL_MIN_DIVIDER; n--) {
+
+               /* Compute the scaled DPLL multiplier, based on the divider */
+               m = scaled_rt_rp * n;
+
+               /*
+                * Since we're counting n down, a m overflow means we can
+                * can immediately skip to the next n
+                */
+               if (m > scaled_max_m)
+                       continue;
+
+               r = _dpll_test_mult(&m, n, &new_rate, target_rate,
+                                   clk->parent->rate);
+
+               e = target_rate - new_rate;
+               pr_debug("clock: n = %d: m = %d: rate error is %d "
+                        "(new_rate = %ld)\n", n, m, e, new_rate);
+
+               if (min_e == -1 ||
+                   min_e >= (int)(abs(e) - clk->dpll_data->rate_tolerance)) {
+                       min_e = e;
+                       min_e_m = m;
+                       min_e_n = n;
+
+                       pr_debug("clock: found new least error %d\n", min_e);
+               }
+
+               /*
+                * Since we're counting n down, a m underflow means we
+                * can bail out completely (since as n decreases in
+                * the next iteration, there's no way that m can
+                * increase beyond the current m)
+                */
+               if (r & DPLL_MULT_UNDERFLOW)
+                       break;
+       }
+
+       if (min_e < 0) {
+               pr_debug("clock: error: target rate or tolerance too low\n");
+               return ~0;
+       }
+
+       clk->dpll_data->last_rounded_m = min_e_m;
+       clk->dpll_data->last_rounded_n = min_e_n;
+       clk->dpll_data->last_rounded_rate =
+               _dpll_compute_new_rate(clk->parent->rate, min_e_m,  min_e_n);
+
+       pr_debug("clock: final least error: e = %d, m = %d, n = %d\n",
+                min_e, min_e_m, min_e_n);
+       pr_debug("clock: final rate: %ld  (target rate: %ld)\n",
+                clk->dpll_data->last_rounded_rate, target_rate);
+
+       return clk->dpll_data->last_rounded_rate;
+}
+
 /*-------------------------------------------------------------------------
  * Omap2 clock reset and init functions
  *-------------------------------------------------------------------------*/