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