blob: 999b91e023b149641f2ff66f0d6460089909dde8 [file] [log] [blame]
Paul Walmsley543d9372008-03-18 10:22:06 +02001/*
2 * linux/arch/arm/mach-omap2/clock.c
3 *
Tony Lindgrena16e9702008-03-18 11:56:39 +02004 * Copyright (C) 2005-2008 Texas Instruments, Inc.
5 * Copyright (C) 2004-2008 Nokia Corporation
6 *
7 * Contacts:
Paul Walmsley543d9372008-03-18 10:22:06 +02008 * Richard Woodruff <r-woodruff2@ti.com>
Paul Walmsley543d9372008-03-18 10:22:06 +02009 * Paul Walmsley
10 *
Paul Walmsley543d9372008-03-18 10:22:06 +020011 * 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>
Russell Kingfced80c2008-09-06 12:10:45 +010024#include <linux/io.h>
Russell Kingfbd3bdb2008-09-06 12:13:59 +010025#include <linux/bitops.h>
Paul Walmsley543d9372008-03-18 10:22:06 +020026
Tony Lindgrence491cf2009-10-20 09:40:47 -070027#include <plat/clock.h>
28#include <plat/clockdomain.h>
29#include <plat/cpu.h>
30#include <plat/prcm.h>
Paul Walmsley543d9372008-03-18 10:22:06 +020031
Paul Walmsley543d9372008-03-18 10:22:06 +020032#include "clock.h"
33#include "prm.h"
34#include "prm-regbits-24xx.h"
35#include "cm.h"
36#include "cm-regbits-24xx.h"
37#include "cm-regbits-34xx.h"
38
Paul Walmsley543d9372008-03-18 10:22:06 +020039u8 cpu_mask;
40
41/*-------------------------------------------------------------------------
Rajendra Nayak911bd732009-12-08 18:47:17 -070042 * OMAP2/3/4 specific clock functions
Paul Walmsley543d9372008-03-18 10:22:06 +020043 *-------------------------------------------------------------------------*/
44
Paul Walmsley4b1f76e2010-01-26 20:13:04 -070045/* Private functions */
46
47/**
48 * _omap2_module_wait_ready - wait for an OMAP module to leave IDLE
49 * @clk: struct clk * belonging to the module
50 *
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.
56 */
57static void _omap2_module_wait_ready(struct clk *clk)
58{
59 void __iomem *companion_reg, *idlest_reg;
60 u8 other_bit, idlest_bit;
61
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)))
66 return;
67 }
68
69 clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit);
70
71 omap2_cm_wait_idlest(idlest_reg, (1 << idlest_bit), clk->name);
72}
73
74/* Enables clock without considering parent dependencies or use count
75 * REVISIT: Maybe change this to use clk->enable like on omap1?
76 */
77static int _omap2_clk_enable(struct clk *clk)
78{
79 return clk->ops->enable(clk);
80}
81
82/* Disables clock without considering parent dependencies or use count */
83static void _omap2_clk_disable(struct clk *clk)
84{
85 clk->ops->disable(clk);
86}
87
88/* Public functions */
89
Paul Walmsley543d9372008-03-18 10:22:06 +020090/**
Paul Walmsleydf791b32010-01-26 20:13:04 -070091 * omap2xxx_clk_commit - commit clock parent/rate changes in hardware
Paul Walmsley439764c2009-01-28 12:35:03 -070092 * @clk: struct clk *
93 *
94 * If @clk has the DELAYED_APP flag set, meaning that parent/rate changes
95 * don't take effect until the VALID_CONFIG bit is written, write the
96 * VALID_CONFIG bit and wait for the write to complete. No return value.
97 */
Paul Walmsleydf791b32010-01-26 20:13:04 -070098void omap2xxx_clk_commit(struct clk *clk)
Paul Walmsley439764c2009-01-28 12:35:03 -070099{
100 if (!cpu_is_omap24xx())
101 return;
102
103 if (!(clk->flags & DELAYED_APP))
104 return;
105
106 prm_write_mod_reg(OMAP24XX_VALID_CONFIG, OMAP24XX_GR_MOD,
Tony Lindgren8e3bd352009-05-25 11:26:42 -0700107 OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
Paul Walmsley439764c2009-01-28 12:35:03 -0700108 /* OCP barrier */
Tony Lindgren8e3bd352009-05-25 11:26:42 -0700109 prm_read_mod_reg(OMAP24XX_GR_MOD, OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
Paul Walmsley439764c2009-01-28 12:35:03 -0700110}
111
Paul Walmsley543d9372008-03-18 10:22:06 +0200112/**
Paul Walmsley333943b2008-08-19 11:08:45 +0300113 * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
114 * @clk: OMAP clock struct ptr to use
115 *
116 * Convert a clockdomain name stored in a struct clk 'clk' into a
117 * clockdomain pointer, and save it into the struct clk. Intended to be
118 * called during clk_register(). No return value.
119 */
120void omap2_init_clk_clkdm(struct clk *clk)
121{
122 struct clockdomain *clkdm;
123
124 if (!clk->clkdm_name)
125 return;
126
127 clkdm = clkdm_lookup(clk->clkdm_name);
128 if (clkdm) {
129 pr_debug("clock: associated clk %s to clkdm %s\n",
130 clk->name, clk->clkdm_name);
131 clk->clkdm = clkdm;
132 } else {
133 pr_debug("clock: could not associate clk %s to "
134 "clkdm %s\n", clk->name, clk->clkdm_name);
135 }
136}
137
138/**
Paul Walmsley72350b22009-07-24 19:44:03 -0600139 * omap2_clk_dflt_find_companion - find companion clock to @clk
140 * @clk: struct clk * to find the companion clock of
141 * @other_reg: void __iomem ** to return the companion clock CM_*CLKEN va in
142 * @other_bit: u8 ** to return the companion clock bit shift in
Paul Walmsley543d9372008-03-18 10:22:06 +0200143 *
Paul Walmsley72350b22009-07-24 19:44:03 -0600144 * Note: We don't need special code here for INVERT_ENABLE for the
145 * time being since INVERT_ENABLE only applies to clocks enabled by
Paul Walmsley543d9372008-03-18 10:22:06 +0200146 * CM_CLKEN_PLL
Paul Walmsley72350b22009-07-24 19:44:03 -0600147 *
148 * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
149 * just a matter of XORing the bits.
150 *
151 * Some clocks don't have companion clocks. For example, modules with
152 * only an interface clock (such as MAILBOXES) don't have a companion
153 * clock. Right now, this code relies on the hardware exporting a bit
154 * in the correct companion register that indicates that the
155 * nonexistent 'companion clock' is active. Future patches will
156 * associate this type of code with per-module data structures to
157 * avoid this issue, and remove the casts. No return value.
Paul Walmsley543d9372008-03-18 10:22:06 +0200158 */
Paul Walmsley72350b22009-07-24 19:44:03 -0600159void omap2_clk_dflt_find_companion(struct clk *clk, void __iomem **other_reg,
160 u8 *other_bit)
Paul Walmsley543d9372008-03-18 10:22:06 +0200161{
Paul Walmsley72350b22009-07-24 19:44:03 -0600162 u32 r;
Paul Walmsley543d9372008-03-18 10:22:06 +0200163
Russell Kingc1168dc2008-11-04 21:24:00 +0000164 /*
165 * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
166 * it's just a matter of XORing the bits.
167 */
Paul Walmsley72350b22009-07-24 19:44:03 -0600168 r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
Paul Walmsley543d9372008-03-18 10:22:06 +0200169
Paul Walmsley72350b22009-07-24 19:44:03 -0600170 *other_reg = (__force void __iomem *)r;
171 *other_bit = clk->enable_bit;
Paul Walmsley543d9372008-03-18 10:22:06 +0200172}
173
Paul Walmsley72350b22009-07-24 19:44:03 -0600174/**
175 * omap2_clk_dflt_find_idlest - find CM_IDLEST reg va, bit shift for @clk
176 * @clk: struct clk * to find IDLEST info for
177 * @idlest_reg: void __iomem ** to return the CM_IDLEST va in
178 * @idlest_bit: u8 ** to return the CM_IDLEST bit shift in
179 *
180 * Return the CM_IDLEST register address and bit shift corresponding
181 * to the module that "owns" this clock. This default code assumes
182 * that the CM_IDLEST bit shift is the CM_*CLKEN bit shift, and that
183 * the IDLEST register address ID corresponds to the CM_*CLKEN
184 * register address ID (e.g., that CM_FCLKEN2 corresponds to
185 * CM_IDLEST2). This is not true for all modules. No return value.
186 */
187void omap2_clk_dflt_find_idlest(struct clk *clk, void __iomem **idlest_reg,
188 u8 *idlest_bit)
189{
190 u32 r;
191
192 r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
193 *idlest_reg = (__force void __iomem *)r;
194 *idlest_bit = clk->enable_bit;
195}
196
Paul Walmsley72350b22009-07-24 19:44:03 -0600197int omap2_dflt_clk_enable(struct clk *clk)
Paul Walmsley543d9372008-03-18 10:22:06 +0200198{
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700199 u32 v;
Paul Walmsley543d9372008-03-18 10:22:06 +0200200
Russell Kingc0fc18c2008-09-05 15:10:27 +0100201 if (unlikely(clk->enable_reg == NULL)) {
Paul Walmsley72350b22009-07-24 19:44:03 -0600202 pr_err("clock.c: Enable for %s without enable code\n",
Paul Walmsley543d9372008-03-18 10:22:06 +0200203 clk->name);
204 return 0; /* REVISIT: -EINVAL */
205 }
206
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700207 v = __raw_readl(clk->enable_reg);
Paul Walmsley543d9372008-03-18 10:22:06 +0200208 if (clk->flags & INVERT_ENABLE)
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700209 v &= ~(1 << clk->enable_bit);
Paul Walmsley543d9372008-03-18 10:22:06 +0200210 else
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700211 v |= (1 << clk->enable_bit);
212 __raw_writel(v, clk->enable_reg);
Paul Walmsleyf11fda62009-01-28 12:35:06 -0700213 v = __raw_readl(clk->enable_reg); /* OCP barrier */
Paul Walmsley543d9372008-03-18 10:22:06 +0200214
Paul Walmsley72350b22009-07-24 19:44:03 -0600215 if (clk->ops->find_idlest)
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700216 _omap2_module_wait_ready(clk);
Paul Walmsley72350b22009-07-24 19:44:03 -0600217
Paul Walmsley543d9372008-03-18 10:22:06 +0200218 return 0;
219}
220
Paul Walmsley72350b22009-07-24 19:44:03 -0600221void omap2_dflt_clk_disable(struct clk *clk)
Paul Walmsley543d9372008-03-18 10:22:06 +0200222{
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700223 u32 v;
Paul Walmsley543d9372008-03-18 10:22:06 +0200224
Paul Walmsleyfecb4942009-01-27 19:12:50 -0700225 if (!clk->enable_reg) {
Paul Walmsley543d9372008-03-18 10:22:06 +0200226 /*
227 * 'Independent' here refers to a clock which is not
228 * controlled by its parent.
229 */
230 printk(KERN_ERR "clock: clk_disable called on independent "
231 "clock %s which has no enable_reg\n", clk->name);
232 return;
233 }
234
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700235 v = __raw_readl(clk->enable_reg);
Paul Walmsley543d9372008-03-18 10:22:06 +0200236 if (clk->flags & INVERT_ENABLE)
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700237 v |= (1 << clk->enable_bit);
Paul Walmsley543d9372008-03-18 10:22:06 +0200238 else
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700239 v &= ~(1 << clk->enable_bit);
240 __raw_writel(v, clk->enable_reg);
Paul Walmsleyde07fed2009-01-28 12:35:01 -0700241 /* No OCP barrier needed here since it is a disable operation */
Paul Walmsley543d9372008-03-18 10:22:06 +0200242}
243
Russell Kingb36ee722008-11-04 17:59:52 +0000244const struct clkops clkops_omap2_dflt_wait = {
Paul Walmsley72350b22009-07-24 19:44:03 -0600245 .enable = omap2_dflt_clk_enable,
Russell Kingb36ee722008-11-04 17:59:52 +0000246 .disable = omap2_dflt_clk_disable,
Paul Walmsley72350b22009-07-24 19:44:03 -0600247 .find_companion = omap2_clk_dflt_find_companion,
248 .find_idlest = omap2_clk_dflt_find_idlest,
Russell Kingb36ee722008-11-04 17:59:52 +0000249};
250
Russell Kingbc51da42008-11-04 18:59:32 +0000251const struct clkops clkops_omap2_dflt = {
252 .enable = omap2_dflt_clk_enable,
253 .disable = omap2_dflt_clk_disable,
254};
255
Paul Walmsley543d9372008-03-18 10:22:06 +0200256void omap2_clk_disable(struct clk *clk)
257{
258 if (clk->usecount > 0 && !(--clk->usecount)) {
259 _omap2_clk_disable(clk);
Paul Walmsleyfecb4942009-01-27 19:12:50 -0700260 if (clk->parent)
Paul Walmsley543d9372008-03-18 10:22:06 +0200261 omap2_clk_disable(clk->parent);
Paul Walmsley333943b2008-08-19 11:08:45 +0300262 if (clk->clkdm)
263 omap2_clkdm_clk_disable(clk->clkdm, clk);
264
Paul Walmsley543d9372008-03-18 10:22:06 +0200265 }
266}
267
268int omap2_clk_enable(struct clk *clk)
269{
270 int ret = 0;
271
272 if (clk->usecount++ == 0) {
Paul Walmsley333943b2008-08-19 11:08:45 +0300273 if (clk->clkdm)
274 omap2_clkdm_clk_enable(clk->clkdm, clk);
275
Russell Kinga7f8c592009-01-31 11:00:17 +0000276 if (clk->parent) {
Paul Walmsley543d9372008-03-18 10:22:06 +0200277 ret = omap2_clk_enable(clk->parent);
Russell Kinga7f8c592009-01-31 11:00:17 +0000278 if (ret)
279 goto err;
Paul Walmsley543d9372008-03-18 10:22:06 +0200280 }
281
Paul Walmsley543d9372008-03-18 10:22:06 +0200282 ret = _omap2_clk_enable(clk);
Russell Kinga7f8c592009-01-31 11:00:17 +0000283 if (ret) {
Russell Kinga7f8c592009-01-31 11:00:17 +0000284 if (clk->parent)
Paul Walmsley333943b2008-08-19 11:08:45 +0300285 omap2_clk_disable(clk->parent);
Russell Kinga7f8c592009-01-31 11:00:17 +0000286
287 goto err;
Paul Walmsley543d9372008-03-18 10:22:06 +0200288 }
289 }
Russell Kinga7f8c592009-01-31 11:00:17 +0000290 return ret;
Paul Walmsley543d9372008-03-18 10:22:06 +0200291
Russell Kinga7f8c592009-01-31 11:00:17 +0000292err:
Russell King8263e5b2009-01-31 11:02:37 +0000293 if (clk->clkdm)
294 omap2_clkdm_clk_disable(clk->clkdm, clk);
Russell Kinga7f8c592009-01-31 11:00:17 +0000295 clk->usecount--;
Paul Walmsley543d9372008-03-18 10:22:06 +0200296 return ret;
297}
298
Paul Walmsley543d9372008-03-18 10:22:06 +0200299/* Set the clock rate for a clock source */
300int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
301{
302 int ret = -EINVAL;
303
304 pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
305
306 /* CONFIG_PARTICIPANT clocks are changed only in sets via the
307 rate table mechanism, driven by mpu_speed */
308 if (clk->flags & CONFIG_PARTICIPANT)
309 return -EINVAL;
310
311 /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
Paul Walmsleyfecb4942009-01-27 19:12:50 -0700312 if (clk->set_rate)
Paul Walmsley543d9372008-03-18 10:22:06 +0200313 ret = clk->set_rate(clk, rate);
314
Paul Walmsley543d9372008-03-18 10:22:06 +0200315 return ret;
316}
317
Paul Walmsley543d9372008-03-18 10:22:06 +0200318int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
319{
Paul Walmsleyfecb4942009-01-27 19:12:50 -0700320 if (clk->flags & CONFIG_PARTICIPANT)
Paul Walmsley543d9372008-03-18 10:22:06 +0200321 return -EINVAL;
322
323 if (!clk->clksel)
324 return -EINVAL;
325
Paul Walmsleydf791b32010-01-26 20:13:04 -0700326 return omap2_clksel_set_parent(clk, new_parent);
Paul Walmsley543d9372008-03-18 10:22:06 +0200327}
328
Paul Walmsley543d9372008-03-18 10:22:06 +0200329/*-------------------------------------------------------------------------
330 * Omap2 clock reset and init functions
331 *-------------------------------------------------------------------------*/
332
333#ifdef CONFIG_OMAP_RESET_CLOCKS
334void omap2_clk_disable_unused(struct clk *clk)
335{
336 u32 regval32, v;
337
338 v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
339
340 regval32 = __raw_readl(clk->enable_reg);
341 if ((regval32 & (1 << clk->enable_bit)) == v)
342 return;
343
Artem Bityutskiy0db4e822009-05-12 17:34:40 -0600344 printk(KERN_DEBUG "Disabling unused clock \"%s\"\n", clk->name);
Tero Kristo8463e202009-01-28 12:27:45 -0700345 if (cpu_is_omap34xx()) {
346 omap2_clk_enable(clk);
347 omap2_clk_disable(clk);
348 } else
349 _omap2_clk_disable(clk);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300350 if (clk->clkdm != NULL)
351 pwrdm_clkdm_state_switch(clk->clkdm);
Paul Walmsley543d9372008-03-18 10:22:06 +0200352}
353#endif
Paul Walmsley69ecefc2010-01-26 20:13:04 -0700354
355/* Common data */
356
357struct clk_functions omap2_clk_functions = {
358 .clk_enable = omap2_clk_enable,
359 .clk_disable = omap2_clk_disable,
360 .clk_round_rate = omap2_clk_round_rate,
361 .clk_set_rate = omap2_clk_set_rate,
362 .clk_set_parent = omap2_clk_set_parent,
363 .clk_disable_unused = omap2_clk_disable_unused,
364#ifdef CONFIG_CPU_FREQ
365 /* These will be removed when the OPP code is integrated */
366 .clk_init_cpufreq_table = omap2_clk_init_cpufreq_table,
367 .clk_exit_cpufreq_table = omap2_clk_exit_cpufreq_table,
368#endif
369};
370