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