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