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>
25 #include <linux/bitops.h>
27 #include <plat/clock.h>
28 #include <plat/clockdomain.h>
30 #include <plat/prcm.h>
34 #include "prm-regbits-24xx.h"
36 #include "cm-regbits-24xx.h"
37 #include "cm-regbits-34xx.h"
41 /*-------------------------------------------------------------------------
42 * OMAP2/3/4 specific clock functions
43 *-------------------------------------------------------------------------*/
45 /* Private functions */
48 * _omap2_module_wait_ready - wait for an OMAP module to leave IDLE
49 * @clk: struct clk * belonging to the module
51 * If the necessary clocks for the OMAP hardware IP block that
52 * corresponds to clock @clk are enabled, then wait for the module to
53 * indicate readiness (i.e., to leave IDLE). This code does not
54 * belong in the clock code and will be moved in the medium term to
55 * module-dependent code. No return value.
57 static void _omap2_module_wait_ready(struct clk *clk)
59 void __iomem *companion_reg, *idlest_reg;
60 u8 other_bit, idlest_bit, idlest_val;
62 /* Not all modules have multiple clocks that their IDLEST depends on */
63 if (clk->ops->find_companion) {
64 clk->ops->find_companion(clk, &companion_reg, &other_bit);
65 if (!(__raw_readl(companion_reg) & (1 << other_bit)))
69 clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit, &idlest_val);
71 omap2_cm_wait_idlest(idlest_reg, (1 << idlest_bit), idlest_val,
75 /* Enables clock without considering parent dependencies or use count
76 * REVISIT: Maybe change this to use clk->enable like on omap1?
78 static int _omap2_clk_enable(struct clk *clk)
80 return clk->ops->enable(clk);
83 /* Disables clock without considering parent dependencies or use count */
84 static void _omap2_clk_disable(struct clk *clk)
86 clk->ops->disable(clk);
89 /* Public functions */
92 * omap2xxx_clk_commit - commit clock parent/rate changes in hardware
95 * If @clk has the DELAYED_APP flag set, meaning that parent/rate changes
96 * don't take effect until the VALID_CONFIG bit is written, write the
97 * VALID_CONFIG bit and wait for the write to complete. No return value.
99 void omap2xxx_clk_commit(struct clk *clk)
101 if (!cpu_is_omap24xx())
104 if (!(clk->flags & DELAYED_APP))
107 prm_write_mod_reg(OMAP24XX_VALID_CONFIG, OMAP24XX_GR_MOD,
108 OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
110 prm_read_mod_reg(OMAP24XX_GR_MOD, OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
114 * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
115 * @clk: OMAP clock struct ptr to use
117 * Convert a clockdomain name stored in a struct clk 'clk' into a
118 * clockdomain pointer, and save it into the struct clk. Intended to be
119 * called during clk_register(). No return value.
121 void omap2_init_clk_clkdm(struct clk *clk)
123 struct clockdomain *clkdm;
125 if (!clk->clkdm_name)
128 clkdm = clkdm_lookup(clk->clkdm_name);
130 pr_debug("clock: associated clk %s to clkdm %s\n",
131 clk->name, clk->clkdm_name);
134 pr_debug("clock: could not associate clk %s to "
135 "clkdm %s\n", clk->name, clk->clkdm_name);
140 * omap2_clk_dflt_find_companion - find companion clock to @clk
141 * @clk: struct clk * to find the companion clock of
142 * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
143 * @other_bit: u8 ** to return the companion clock bit shift in
145 * Note: We don't need special code here for INVERT_ENABLE for the
146 * time being since INVERT_ENABLE only applies to clocks enabled by
149 * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
150 * just a matter of XORing the bits.
152 * Some clocks don't have companion clocks. For example, modules with
153 * only an interface clock (such as MAILBOXES) don't have a companion
154 * clock. Right now, this code relies on the hardware exporting a bit
155 * in the correct companion register that indicates that the
156 * nonexistent 'companion clock' is active. Future patches will
157 * associate this type of code with per-module data structures to
158 * avoid this issue, and remove the casts. No return value.
160 void omap2_clk_dflt_find_companion(struct clk *clk, void __iomem **other_reg,
166 * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
167 * it's just a matter of XORing the bits.
169 r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
171 *other_reg = (__force void __iomem *)r;
172 *other_bit = clk->enable_bit;
176 * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
177 * @clk: struct clk * to find IDLEST info for
178 * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
179 * @idlest_bit: u8 * to return the CM_IDLEST bit shift in
180 * @idlest_val: u8 * to return the idle status indicator
182 * Return the CM_IDLEST register address and bit shift corresponding
183 * to the module that "owns" this clock. This default code assumes
184 * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
185 * the IDLEST register address ID corresponds to the CM_*CLKEN
186 * register address ID (e.g., that CM_FCLKEN2 corresponds to
187 * CM_IDLEST2). This is not true for all modules. No return value.
189 void omap2_clk_dflt_find_idlest(struct clk *clk, void __iomem **idlest_reg,
190 u8 *idlest_bit, u8 *idlest_val)
194 r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
195 *idlest_reg = (__force void __iomem *)r;
196 *idlest_bit = clk->enable_bit;
199 * 24xx uses 0 to indicate not ready, and 1 to indicate ready.
200 * 34xx reverses this, just to keep us on our toes
201 * AM35xx uses both, depending on the module.
203 if (cpu_is_omap24xx())
204 *idlest_val = OMAP24XX_CM_IDLEST_VAL;
205 else if (cpu_is_omap34xx())
206 *idlest_val = OMAP34XX_CM_IDLEST_VAL;
212 int omap2_dflt_clk_enable(struct clk *clk)
216 if (unlikely(clk->enable_reg == NULL)) {
217 pr_err("clock.c: Enable for %s without enable code\n",
219 return 0; /* REVISIT: -EINVAL */
222 v = __raw_readl(clk->enable_reg);
223 if (clk->flags & INVERT_ENABLE)
224 v &= ~(1 << clk->enable_bit);
226 v |= (1 << clk->enable_bit);
227 __raw_writel(v, clk->enable_reg);
228 v = __raw_readl(clk->enable_reg); /* OCP barrier */
230 if (clk->ops->find_idlest)
231 _omap2_module_wait_ready(clk);
236 void omap2_dflt_clk_disable(struct clk *clk)
240 if (!clk->enable_reg) {
242 * 'Independent' here refers to a clock which is not
243 * controlled by its parent.
245 printk(KERN_ERR "clock: clk_disable called on independent "
246 "clock %s which has no enable_reg\n", clk->name);
250 v = __raw_readl(clk->enable_reg);
251 if (clk->flags & INVERT_ENABLE)
252 v |= (1 << clk->enable_bit);
254 v &= ~(1 << clk->enable_bit);
255 __raw_writel(v, clk->enable_reg);
256 /* No OCP barrier needed here since it is a disable operation */
259 const struct clkops clkops_omap2_dflt_wait = {
260 .enable = omap2_dflt_clk_enable,
261 .disable = omap2_dflt_clk_disable,
262 .find_companion = omap2_clk_dflt_find_companion,
263 .find_idlest = omap2_clk_dflt_find_idlest,
266 const struct clkops clkops_omap2_dflt = {
267 .enable = omap2_dflt_clk_enable,
268 .disable = omap2_dflt_clk_disable,
271 void omap2_clk_disable(struct clk *clk)
273 if (clk->usecount > 0 && !(--clk->usecount)) {
274 _omap2_clk_disable(clk);
276 omap2_clk_disable(clk->parent);
278 omap2_clkdm_clk_disable(clk->clkdm, clk);
283 int omap2_clk_enable(struct clk *clk)
287 if (clk->usecount++ == 0) {
289 omap2_clkdm_clk_enable(clk->clkdm, clk);
292 ret = omap2_clk_enable(clk->parent);
297 ret = _omap2_clk_enable(clk);
300 omap2_clk_disable(clk->parent);
309 omap2_clkdm_clk_disable(clk->clkdm, clk);
314 /* Set the clock rate for a clock source */
315 int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
319 pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
321 /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
323 ret = clk->set_rate(clk, rate);
328 int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
333 if (clk->parent == new_parent)
336 return omap2_clksel_set_parent(clk, new_parent);
339 /*-------------------------------------------------------------------------
340 * Omap2 clock reset and init functions
341 *-------------------------------------------------------------------------*/
343 #ifdef CONFIG_OMAP_RESET_CLOCKS
344 void omap2_clk_disable_unused(struct clk *clk)
348 v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
350 regval32 = __raw_readl(clk->enable_reg);
351 if ((regval32 & (1 << clk->enable_bit)) == v)
354 printk(KERN_DEBUG "Disabling unused clock \"%s\"\n", clk->name);
355 if (cpu_is_omap34xx()) {
356 omap2_clk_enable(clk);
357 omap2_clk_disable(clk);
359 _omap2_clk_disable(clk);
360 if (clk->clkdm != NULL)
361 pwrdm_clkdm_state_switch(clk->clkdm);
367 struct clk_functions omap2_clk_functions = {
368 .clk_enable = omap2_clk_enable,
369 .clk_disable = omap2_clk_disable,
370 .clk_round_rate = omap2_clk_round_rate,
371 .clk_set_rate = omap2_clk_set_rate,
372 .clk_set_parent = omap2_clk_set_parent,
373 .clk_disable_unused = omap2_clk_disable_unused,
374 #ifdef CONFIG_CPU_FREQ
375 /* These will be removed when the OPP code is integrated */
376 .clk_init_cpufreq_table = omap2_clk_init_cpufreq_table,
377 .clk_exit_cpufreq_table = omap2_clk_exit_cpufreq_table,