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