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