2 * linux/arch/arm/mach-omap2/clock.c
4 * Copyright (C) 2005-2008 Texas Instruments, Inc.
5 * Copyright (C) 2004-2008 Nokia Corporation
8 * Richard Woodruff <r-woodruff2@ti.com>
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.
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 <asm/bitops.h>
28 #include <asm/arch/clock.h>
29 #include <asm/arch/sram.h>
30 #include <asm/arch/cpu.h>
31 #include <asm/div64.h>
37 #include "prm-regbits-24xx.h"
39 #include "cm-regbits-24xx.h"
40 #include "cm-regbits-34xx.h"
42 #define MAX_CLOCK_ENABLE_WAIT 100000
46 /*-------------------------------------------------------------------------
47 * Omap2 specific clock functions
48 *-------------------------------------------------------------------------*/
51 * omap2_init_clksel_parent - set a clksel clk's parent field from the hardware
52 * @clk: OMAP clock struct ptr to use
54 * Given a pointer to a source-selectable struct clk, read the hardware
55 * register and determine what its parent is currently set to. Update the
56 * clk->parent field with the appropriate clk ptr.
58 void omap2_init_clksel_parent(struct clk *clk)
60 const struct clksel *clks;
61 const struct clksel_rate *clkr;
67 r = __raw_readl(clk->clksel_reg) & clk->clksel_mask;
68 r >>= __ffs(clk->clksel_mask);
70 for (clks = clk->clksel; clks->parent && !found; clks++) {
71 for (clkr = clks->rates; clkr->div && !found; clkr++) {
72 if ((clkr->flags & cpu_mask) && (clkr->val == r)) {
73 if (clk->parent != clks->parent) {
74 pr_debug("clock: inited %s parent "
76 clk->name, clks->parent->name,
78 clk->parent->name : "NULL"));
79 clk->parent = clks->parent;
87 printk(KERN_ERR "clock: init parent: could not find "
88 "regval %0x for clock %s\n", r, clk->name);
93 /* Returns the DPLL rate */
94 u32 omap2_get_dpll_rate(struct clk *clk)
97 u32 dpll_mult, dpll_div, dpll;
98 const struct dpll_data *dd;
101 /* REVISIT: What do we return on error? */
105 dpll = __raw_readl(dd->mult_div1_reg);
106 dpll_mult = dpll & dd->mult_mask;
107 dpll_mult >>= __ffs(dd->mult_mask);
108 dpll_div = dpll & dd->div1_mask;
109 dpll_div >>= __ffs(dd->div1_mask);
111 dpll_clk = (long long)clk->parent->rate * dpll_mult;
112 do_div(dpll_clk, dpll_div + 1);
118 * Used for clocks that have the same value as the parent clock,
119 * divided by some factor
121 void omap2_fixed_divisor_recalc(struct clk *clk)
123 WARN_ON(!clk->fixed_div);
125 clk->rate = clk->parent->rate / clk->fixed_div;
127 if (clk->flags & RATE_PROPAGATES)
132 * omap2_wait_clock_ready - wait for clock to enable
133 * @reg: physical address of clock IDLEST register
134 * @mask: value to mask against to determine if the clock is active
135 * @name: name of the clock (for printk)
137 * Returns 1 if the clock enabled in time, or 0 if it failed to enable
138 * in roughly MAX_CLOCK_ENABLE_WAIT microseconds.
140 int omap2_wait_clock_ready(void __iomem *reg, u32 mask, const char *name)
146 * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
147 * 34xx reverses this, just to keep us on our toes
149 if (cpu_mask & (RATE_IN_242X | RATE_IN_243X)) {
151 } else if (cpu_mask & RATE_IN_343X) {
156 while (((__raw_readl(reg) & mask) != ena) &&
157 (i++ < MAX_CLOCK_ENABLE_WAIT)) {
161 if (i < MAX_CLOCK_ENABLE_WAIT)
162 pr_debug("Clock %s stable after %d loops\n", name, i);
164 printk(KERN_ERR "Clock %s didn't enable in %d tries\n",
165 name, MAX_CLOCK_ENABLE_WAIT);
168 return (i < MAX_CLOCK_ENABLE_WAIT) ? 1 : 0;
173 * Note: We don't need special code here for INVERT_ENABLE
174 * for the time being since INVERT_ENABLE only applies to clocks enabled by
177 static void omap2_clk_wait_ready(struct clk *clk)
179 void __iomem *reg, *other_reg, *st_reg;
183 * REVISIT: This code is pretty ugly. It would be nice to generalize
184 * it and pull it into struct clk itself somehow.
186 reg = clk->enable_reg;
187 if ((((u32)reg & 0xff) >= CM_FCLKEN1) &&
188 (((u32)reg & 0xff) <= OMAP24XX_CM_FCLKEN2))
189 other_reg = (void __iomem *)(((u32)reg & ~0xf0) | 0x10); /* CM_ICLKEN* */
190 else if ((((u32)reg & 0xff) >= CM_ICLKEN1) &&
191 (((u32)reg & 0xff) <= OMAP24XX_CM_ICLKEN4))
192 other_reg = (void __iomem *)(((u32)reg & ~0xf0) | 0x00); /* CM_FCLKEN* */
196 /* REVISIT: What are the appropriate exclusions for 34XX? */
197 /* No check for DSS or cam clocks */
198 if (cpu_is_omap24xx() && ((u32)reg & 0x0f) == 0) { /* CM_{F,I}CLKEN1 */
199 if (clk->enable_bit == OMAP24XX_EN_DSS2_SHIFT ||
200 clk->enable_bit == OMAP24XX_EN_DSS1_SHIFT ||
201 clk->enable_bit == OMAP24XX_EN_CAM_SHIFT)
205 /* REVISIT: What are the appropriate exclusions for 34XX? */
206 /* OMAP3: ignore DSS-mod clocks */
207 if (cpu_is_omap34xx() &&
208 (((u32)reg & ~0xff) == (u32)OMAP_CM_REGADDR(OMAP3430_DSS_MOD, 0) ||
209 ((((u32)reg & ~0xff) == (u32)OMAP_CM_REGADDR(CORE_MOD, 0)) &&
210 clk->enable_bit == OMAP3430_EN_SSI_SHIFT)))
213 /* Check if both functional and interface clocks
215 bit = 1 << clk->enable_bit;
216 if (!(__raw_readl(other_reg) & bit))
218 st_reg = (void __iomem *)(((u32)other_reg & ~0xf0) | 0x20); /* CM_IDLEST* */
220 omap2_wait_clock_ready(st_reg, bit, clk->name);
223 /* Enables clock without considering parent dependencies or use count
224 * REVISIT: Maybe change this to use clk->enable like on omap1?
226 int _omap2_clk_enable(struct clk *clk)
230 if (clk->flags & (ALWAYS_ENABLED | PARENT_CONTROLS_CLOCK))
234 return clk->enable(clk);
236 if (unlikely(clk->enable_reg == 0)) {
237 printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
239 return 0; /* REVISIT: -EINVAL */
242 regval32 = __raw_readl(clk->enable_reg);
243 if (clk->flags & INVERT_ENABLE)
244 regval32 &= ~(1 << clk->enable_bit);
246 regval32 |= (1 << clk->enable_bit);
247 __raw_writel(regval32, clk->enable_reg);
250 omap2_clk_wait_ready(clk);
255 /* Disables clock without considering parent dependencies or use count */
256 void _omap2_clk_disable(struct clk *clk)
260 if (clk->flags & (ALWAYS_ENABLED | PARENT_CONTROLS_CLOCK))
268 if (clk->enable_reg == 0) {
270 * 'Independent' here refers to a clock which is not
271 * controlled by its parent.
273 printk(KERN_ERR "clock: clk_disable called on independent "
274 "clock %s which has no enable_reg\n", clk->name);
278 regval32 = __raw_readl(clk->enable_reg);
279 if (clk->flags & INVERT_ENABLE)
280 regval32 |= (1 << clk->enable_bit);
282 regval32 &= ~(1 << clk->enable_bit);
283 __raw_writel(regval32, clk->enable_reg);
287 void omap2_clk_disable(struct clk *clk)
289 if (clk->usecount > 0 && !(--clk->usecount)) {
290 _omap2_clk_disable(clk);
291 if (likely((u32)clk->parent))
292 omap2_clk_disable(clk->parent);
296 int omap2_clk_enable(struct clk *clk)
300 if (clk->usecount++ == 0) {
301 if (likely((u32)clk->parent))
302 ret = omap2_clk_enable(clk->parent);
304 if (unlikely(ret != 0)) {
309 ret = _omap2_clk_enable(clk);
311 if (unlikely(ret != 0) && clk->parent) {
312 omap2_clk_disable(clk->parent);
321 * Used for clocks that are part of CLKSEL_xyz governed clocks.
322 * REVISIT: Maybe change to use clk->enable() functions like on omap1?
324 void omap2_clksel_recalc(struct clk *clk)
328 pr_debug("clock: recalc'ing clksel clk %s\n", clk->name);
330 div = omap2_clksel_get_divisor(clk);
334 if (unlikely(clk->rate == clk->parent->rate / div))
336 clk->rate = clk->parent->rate / div;
338 pr_debug("clock: new clock rate is %ld (div %d)\n", clk->rate, div);
340 if (unlikely(clk->flags & RATE_PROPAGATES))
345 * omap2_get_clksel_by_parent - return clksel struct for a given clk & parent
346 * @clk: OMAP struct clk ptr to inspect
347 * @src_clk: OMAP struct clk ptr of the parent clk to search for
349 * Scan the struct clksel array associated with the clock to find
350 * the element associated with the supplied parent clock address.
351 * Returns a pointer to the struct clksel on success or NULL on error.
353 const struct clksel *omap2_get_clksel_by_parent(struct clk *clk,
356 const struct clksel *clks;
361 for (clks = clk->clksel; clks->parent; clks++) {
362 if (clks->parent == src_clk)
363 break; /* Found the requested parent */
367 printk(KERN_ERR "clock: Could not find parent clock %s in "
368 "clksel array of clock %s\n", src_clk->name,
377 * omap2_clksel_round_rate_div - find divisor for the given clock and rate
378 * @clk: OMAP struct clk to use
379 * @target_rate: desired clock rate
380 * @new_div: ptr to where we should store the divisor
382 * Finds 'best' divider value in an array based on the source and target
383 * rates. The divider array must be sorted with smallest divider first.
384 * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
385 * they are only settable as part of virtual_prcm set.
387 * Returns the rounded clock rate or returns 0xffffffff on error.
389 u32 omap2_clksel_round_rate_div(struct clk *clk, unsigned long target_rate,
392 unsigned long test_rate;
393 const struct clksel *clks;
394 const struct clksel_rate *clkr;
397 printk(KERN_INFO "clock: clksel_round_rate_div: %s target_rate %ld\n",
398 clk->name, target_rate);
402 clks = omap2_get_clksel_by_parent(clk, clk->parent);
406 for (clkr = clks->rates; clkr->div; clkr++) {
407 if (!(clkr->flags & cpu_mask))
411 if (clkr->div <= last_div)
412 printk(KERN_ERR "clock: clksel_rate table not sorted "
413 "for clock %s", clk->name);
415 last_div = clkr->div;
417 test_rate = clk->parent->rate / clkr->div;
419 if (test_rate <= target_rate)
420 break; /* found it */
424 printk(KERN_ERR "clock: Could not find divisor for target "
425 "rate %ld for clock %s parent %s\n", target_rate,
426 clk->name, clk->parent->name);
430 *new_div = clkr->div;
432 printk(KERN_INFO "clock: new_div = %d, new_rate = %ld\n", *new_div,
433 (clk->parent->rate / clkr->div));
435 return (clk->parent->rate / clkr->div);
439 * omap2_clksel_round_rate - find rounded rate for the given clock and rate
440 * @clk: OMAP struct clk to use
441 * @target_rate: desired clock rate
443 * Compatibility wrapper for OMAP clock framework
444 * Finds best target rate based on the source clock and possible dividers.
445 * rates. The divider array must be sorted with smallest divider first.
446 * Note that this will not work for clocks which are part of CONFIG_PARTICIPANT,
447 * they are only settable as part of virtual_prcm set.
449 * Returns the rounded clock rate or returns 0xffffffff on error.
451 long omap2_clksel_round_rate(struct clk *clk, unsigned long target_rate)
455 return omap2_clksel_round_rate_div(clk, target_rate, &new_div);
459 /* Given a clock and a rate apply a clock specific rounding function */
460 long omap2_clk_round_rate(struct clk *clk, unsigned long rate)
462 if (clk->round_rate != 0)
463 return clk->round_rate(clk, rate);
465 if (clk->flags & RATE_FIXED)
466 printk(KERN_ERR "clock: generic omap2_clk_round_rate called "
467 "on fixed-rate clock %s\n", clk->name);
473 * omap2_clksel_to_divisor() - turn clksel field value into integer divider
474 * @clk: OMAP struct clk to use
475 * @field_val: register field value to find
477 * Given a struct clk of a rate-selectable clksel clock, and a register field
478 * value to search for, find the corresponding clock divisor. The register
479 * field value should be pre-masked and shifted down so the LSB is at bit 0
480 * before calling. Returns 0 on error
482 u32 omap2_clksel_to_divisor(struct clk *clk, u32 field_val)
484 const struct clksel *clks;
485 const struct clksel_rate *clkr;
487 clks = omap2_get_clksel_by_parent(clk, clk->parent);
491 for (clkr = clks->rates; clkr->div; clkr++) {
492 if ((clkr->flags & cpu_mask) && (clkr->val == field_val))
497 printk(KERN_ERR "clock: Could not find fieldval %d for "
498 "clock %s parent %s\n", field_val, clk->name,
507 * omap2_divisor_to_clksel() - turn clksel integer divisor into a field value
508 * @clk: OMAP struct clk to use
509 * @div: integer divisor to search for
511 * Given a struct clk of a rate-selectable clksel clock, and a clock divisor,
512 * find the corresponding register field value. The return register value is
513 * the value before left-shifting. Returns 0xffffffff on error
515 u32 omap2_divisor_to_clksel(struct clk *clk, u32 div)
517 const struct clksel *clks;
518 const struct clksel_rate *clkr;
520 /* should never happen */
523 clks = omap2_get_clksel_by_parent(clk, clk->parent);
527 for (clkr = clks->rates; clkr->div; clkr++) {
528 if ((clkr->flags & cpu_mask) && (clkr->div == div))
533 printk(KERN_ERR "clock: Could not find divisor %d for "
534 "clock %s parent %s\n", div, clk->name,
543 * omap2_get_clksel - find clksel register addr & field mask for a clk
544 * @clk: struct clk to use
545 * @field_mask: ptr to u32 to store the register field mask
547 * Returns the address of the clksel register upon success or NULL on error.
549 void __iomem *omap2_get_clksel(struct clk *clk, u32 *field_mask)
551 if (unlikely((clk->clksel_reg == 0) || (clk->clksel_mask == 0)))
554 *field_mask = clk->clksel_mask;
556 return clk->clksel_reg;
560 * omap2_clksel_get_divisor - get current divider applied to parent clock.
561 * @clk: OMAP struct clk to use.
563 * Returns the integer divisor upon success or 0 on error.
565 u32 omap2_clksel_get_divisor(struct clk *clk)
567 u32 field_mask, field_val;
568 void __iomem *div_addr;
570 div_addr = omap2_get_clksel(clk, &field_mask);
574 field_val = __raw_readl(div_addr) & field_mask;
575 field_val >>= __ffs(field_mask);
577 return omap2_clksel_to_divisor(clk, field_val);
580 int omap2_clksel_set_rate(struct clk *clk, unsigned long rate)
582 u32 field_mask, field_val, reg_val, validrate, new_div = 0;
583 void __iomem *div_addr;
585 validrate = omap2_clksel_round_rate_div(clk, rate, &new_div);
586 if (validrate != rate)
589 div_addr = omap2_get_clksel(clk, &field_mask);
593 field_val = omap2_divisor_to_clksel(clk, new_div);
597 reg_val = __raw_readl(div_addr);
598 reg_val &= ~field_mask;
599 reg_val |= (field_val << __ffs(field_mask));
600 __raw_writel(reg_val, div_addr);
603 clk->rate = clk->parent->rate / new_div;
605 if (clk->flags & DELAYED_APP && cpu_is_omap24xx()) {
606 __raw_writel(OMAP24XX_VALID_CONFIG, OMAP24XX_PRCM_CLKCFG_CTRL);
614 /* Set the clock rate for a clock source */
615 int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
619 pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
621 /* CONFIG_PARTICIPANT clocks are changed only in sets via the
622 rate table mechanism, driven by mpu_speed */
623 if (clk->flags & CONFIG_PARTICIPANT)
626 /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
627 if (clk->set_rate != 0)
628 ret = clk->set_rate(clk, rate);
630 if (unlikely(ret == 0 && (clk->flags & RATE_PROPAGATES)))
637 * Converts encoded control register address into a full address
638 * On error, *src_addr will be returned as 0.
640 static u32 omap2_clksel_get_src_field(void __iomem **src_addr,
641 struct clk *src_clk, u32 *field_mask,
642 struct clk *clk, u32 *parent_div)
644 const struct clksel *clks;
645 const struct clksel_rate *clkr;
650 clks = omap2_get_clksel_by_parent(clk, src_clk);
654 for (clkr = clks->rates; clkr->div; clkr++) {
655 if (clkr->flags & (cpu_mask | DEFAULT_RATE))
656 break; /* Found the default rate for this platform */
660 printk(KERN_ERR "clock: Could not find default rate for "
661 "clock %s parent %s\n", clk->name,
662 src_clk->parent->name);
666 /* Should never happen. Add a clksel mask to the struct clk. */
667 WARN_ON(clk->clksel_mask == 0);
669 *field_mask = clk->clksel_mask;
670 *src_addr = clk->clksel_reg;
671 *parent_div = clkr->div;
676 int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
678 void __iomem *src_addr;
679 u32 field_val, field_mask, reg_val, parent_div;
681 if (unlikely(clk->flags & CONFIG_PARTICIPANT))
687 field_val = omap2_clksel_get_src_field(&src_addr, new_parent,
688 &field_mask, clk, &parent_div);
692 if (clk->usecount > 0)
693 _omap2_clk_disable(clk);
695 /* Set new source value (previous dividers if any in effect) */
696 reg_val = __raw_readl(src_addr) & ~field_mask;
697 reg_val |= (field_val << __ffs(field_mask));
698 __raw_writel(reg_val, src_addr);
701 if (clk->flags & DELAYED_APP && cpu_is_omap24xx()) {
702 __raw_writel(OMAP24XX_VALID_CONFIG, OMAP24XX_PRCM_CLKCFG_CTRL);
706 if (clk->usecount > 0)
707 _omap2_clk_enable(clk);
709 clk->parent = new_parent;
711 /* CLKSEL clocks follow their parents' rates, divided by a divisor */
712 clk->rate = new_parent->rate;
715 clk->rate /= parent_div;
717 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
718 clk->name, clk->parent->name, clk->rate);
720 if (unlikely(clk->flags & RATE_PROPAGATES))
726 /*-------------------------------------------------------------------------
727 * Omap2 clock reset and init functions
728 *-------------------------------------------------------------------------*/
730 #ifdef CONFIG_OMAP_RESET_CLOCKS
731 void omap2_clk_disable_unused(struct clk *clk)
735 v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
737 regval32 = __raw_readl(clk->enable_reg);
738 if ((regval32 & (1 << clk->enable_bit)) == v)
741 printk(KERN_INFO "Disabling unused clock \"%s\"\n", clk->name);
742 _omap2_clk_disable(clk);