blob: 9df5937999cb358880b4afa3fe7f06503b57670a [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;
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -070060 u8 other_bit, idlest_bit, idlest_val;
Paul Walmsley4b1f76e2010-01-26 20:13:04 -070061
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
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -070069 clk->ops->find_idlest(clk, &idlest_reg, &idlest_bit, &idlest_val);
Paul Walmsley4b1f76e2010-01-26 20:13:04 -070070
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -070071 omap2_cm_wait_idlest(idlest_reg, (1 << idlest_bit), idlest_val,
72 clk->name);
Paul Walmsley4b1f76e2010-01-26 20:13:04 -070073}
74
75/* Enables clock without considering parent dependencies or use count
76 * REVISIT: Maybe change this to use clk->enable like on omap1?
77 */
78static int _omap2_clk_enable(struct clk *clk)
79{
80 return clk->ops->enable(clk);
81}
82
83/* Disables clock without considering parent dependencies or use count */
84static void _omap2_clk_disable(struct clk *clk)
85{
86 clk->ops->disable(clk);
87}
88
89/* Public functions */
90
Paul Walmsley543d9372008-03-18 10:22:06 +020091/**
Paul Walmsleydf791b32010-01-26 20:13:04 -070092 * omap2xxx_clk_commit - commit clock parent/rate changes in hardware
Paul Walmsley439764c2009-01-28 12:35:03 -070093 * @clk: struct clk *
94 *
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.
98 */
Paul Walmsleydf791b32010-01-26 20:13:04 -070099void omap2xxx_clk_commit(struct clk *clk)
Paul Walmsley439764c2009-01-28 12:35:03 -0700100{
101 if (!cpu_is_omap24xx())
102 return;
103
104 if (!(clk->flags & DELAYED_APP))
105 return;
106
107 prm_write_mod_reg(OMAP24XX_VALID_CONFIG, OMAP24XX_GR_MOD,
Tony Lindgren8e3bd352009-05-25 11:26:42 -0700108 OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
Paul Walmsley439764c2009-01-28 12:35:03 -0700109 /* OCP barrier */
Tony Lindgren8e3bd352009-05-25 11:26:42 -0700110 prm_read_mod_reg(OMAP24XX_GR_MOD, OMAP2_PRCM_CLKCFG_CTRL_OFFSET);
Paul Walmsley439764c2009-01-28 12:35:03 -0700111}
112
Paul Walmsley543d9372008-03-18 10:22:06 +0200113/**
Paul Walmsley333943b2008-08-19 11:08:45 +0300114 * omap2_init_clk_clkdm - look up a clockdomain name, store pointer in clk
115 * @clk: OMAP clock struct ptr to use
116 *
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.
120 */
121void omap2_init_clk_clkdm(struct clk *clk)
122{
123 struct clockdomain *clkdm;
124
125 if (!clk->clkdm_name)
126 return;
127
128 clkdm = clkdm_lookup(clk->clkdm_name);
129 if (clkdm) {
130 pr_debug("clock: associated clk %s to clkdm %s\n",
131 clk->name, clk->clkdm_name);
132 clk->clkdm = clkdm;
133 } else {
134 pr_debug("clock: could not associate clk %s to "
135 "clkdm %s\n", clk->name, clk->clkdm_name);
136 }
137}
138
139/**
Paul Walmsley72350b22009-07-24 19:44:03 -0600140 * 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
Paul Walmsley543d9372008-03-18 10:22:06 +0200144 *
Paul Walmsley72350b22009-07-24 19:44:03 -0600145 * 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
Paul Walmsley543d9372008-03-18 10:22:06 +0200147 * CM_CLKEN_PLL
Paul Walmsley72350b22009-07-24 19:44:03 -0600148 *
149 * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes it's
150 * just a matter of XORing the bits.
151 *
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.
Paul Walmsley543d9372008-03-18 10:22:06 +0200159 */
Paul Walmsley72350b22009-07-24 19:44:03 -0600160void omap2_clk_dflt_find_companion(struct clk *clk, void __iomem **other_reg,
161 u8 *other_bit)
Paul Walmsley543d9372008-03-18 10:22:06 +0200162{
Paul Walmsley72350b22009-07-24 19:44:03 -0600163 u32 r;
Paul Walmsley543d9372008-03-18 10:22:06 +0200164
Russell Kingc1168dc2008-11-04 21:24:00 +0000165 /*
166 * Convert CM_ICLKEN* <-> CM_FCLKEN*. This conversion assumes
167 * it's just a matter of XORing the bits.
168 */
Paul Walmsley72350b22009-07-24 19:44:03 -0600169 r = ((__force u32)clk->enable_reg ^ (CM_FCLKEN ^ CM_ICLKEN));
Paul Walmsley543d9372008-03-18 10:22:06 +0200170
Paul Walmsley72350b22009-07-24 19:44:03 -0600171 *other_reg = (__force void __iomem *)r;
172 *other_bit = clk->enable_bit;
Paul Walmsley543d9372008-03-18 10:22:06 +0200173}
174
Paul Walmsley72350b22009-07-24 19:44:03 -0600175/**
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
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -0700179 * @idlest_bit: u8 * to return the CM_IDLEST bit shift in
180 * @idlest_val: u8 * to return the idle status indicator
Paul Walmsley72350b22009-07-24 19:44:03 -0600181 *
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.
188 */
189void omap2_clk_dflt_find_idlest(struct clk *clk, void __iomem **idlest_reg,
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -0700190 u8 *idlest_bit, u8 *idlest_val)
Paul Walmsley72350b22009-07-24 19:44:03 -0600191{
192 u32 r;
193
194 r = (((__force u32)clk->enable_reg & ~0xf0) | 0x20);
195 *idlest_reg = (__force void __iomem *)r;
196 *idlest_bit = clk->enable_bit;
Ranjith Lohithakshan419cc972010-02-24 12:05:54 -0700197
198 /*
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.
202 */
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;
207 else
208 BUG();
209
Paul Walmsley72350b22009-07-24 19:44:03 -0600210}
211
Paul Walmsley72350b22009-07-24 19:44:03 -0600212int omap2_dflt_clk_enable(struct clk *clk)
Paul Walmsley543d9372008-03-18 10:22:06 +0200213{
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700214 u32 v;
Paul Walmsley543d9372008-03-18 10:22:06 +0200215
Russell Kingc0fc18c2008-09-05 15:10:27 +0100216 if (unlikely(clk->enable_reg == NULL)) {
Paul Walmsley72350b22009-07-24 19:44:03 -0600217 pr_err("clock.c: Enable for %s without enable code\n",
Paul Walmsley543d9372008-03-18 10:22:06 +0200218 clk->name);
219 return 0; /* REVISIT: -EINVAL */
220 }
221
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700222 v = __raw_readl(clk->enable_reg);
Paul Walmsley543d9372008-03-18 10:22:06 +0200223 if (clk->flags & INVERT_ENABLE)
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700224 v &= ~(1 << clk->enable_bit);
Paul Walmsley543d9372008-03-18 10:22:06 +0200225 else
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700226 v |= (1 << clk->enable_bit);
227 __raw_writel(v, clk->enable_reg);
Paul Walmsleyf11fda62009-01-28 12:35:06 -0700228 v = __raw_readl(clk->enable_reg); /* OCP barrier */
Paul Walmsley543d9372008-03-18 10:22:06 +0200229
Paul Walmsley72350b22009-07-24 19:44:03 -0600230 if (clk->ops->find_idlest)
Paul Walmsley4b1f76e2010-01-26 20:13:04 -0700231 _omap2_module_wait_ready(clk);
Paul Walmsley72350b22009-07-24 19:44:03 -0600232
Paul Walmsley543d9372008-03-18 10:22:06 +0200233 return 0;
234}
235
Paul Walmsley72350b22009-07-24 19:44:03 -0600236void omap2_dflt_clk_disable(struct clk *clk)
Paul Walmsley543d9372008-03-18 10:22:06 +0200237{
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700238 u32 v;
Paul Walmsley543d9372008-03-18 10:22:06 +0200239
Paul Walmsleyfecb4942009-01-27 19:12:50 -0700240 if (!clk->enable_reg) {
Paul Walmsley543d9372008-03-18 10:22:06 +0200241 /*
242 * 'Independent' here refers to a clock which is not
243 * controlled by its parent.
244 */
245 printk(KERN_ERR "clock: clk_disable called on independent "
246 "clock %s which has no enable_reg\n", clk->name);
247 return;
248 }
249
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700250 v = __raw_readl(clk->enable_reg);
Paul Walmsley543d9372008-03-18 10:22:06 +0200251 if (clk->flags & INVERT_ENABLE)
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700252 v |= (1 << clk->enable_bit);
Paul Walmsley543d9372008-03-18 10:22:06 +0200253 else
Paul Walmsleyee1eec32009-01-28 12:18:19 -0700254 v &= ~(1 << clk->enable_bit);
255 __raw_writel(v, clk->enable_reg);
Paul Walmsleyde07fed2009-01-28 12:35:01 -0700256 /* No OCP barrier needed here since it is a disable operation */
Paul Walmsley543d9372008-03-18 10:22:06 +0200257}
258
Russell Kingb36ee722008-11-04 17:59:52 +0000259const struct clkops clkops_omap2_dflt_wait = {
Paul Walmsley72350b22009-07-24 19:44:03 -0600260 .enable = omap2_dflt_clk_enable,
Russell Kingb36ee722008-11-04 17:59:52 +0000261 .disable = omap2_dflt_clk_disable,
Paul Walmsley72350b22009-07-24 19:44:03 -0600262 .find_companion = omap2_clk_dflt_find_companion,
263 .find_idlest = omap2_clk_dflt_find_idlest,
Russell Kingb36ee722008-11-04 17:59:52 +0000264};
265
Russell Kingbc51da42008-11-04 18:59:32 +0000266const struct clkops clkops_omap2_dflt = {
267 .enable = omap2_dflt_clk_enable,
268 .disable = omap2_dflt_clk_disable,
269};
270
Paul Walmsley543d9372008-03-18 10:22:06 +0200271void omap2_clk_disable(struct clk *clk)
272{
273 if (clk->usecount > 0 && !(--clk->usecount)) {
274 _omap2_clk_disable(clk);
Paul Walmsleyfecb4942009-01-27 19:12:50 -0700275 if (clk->parent)
Paul Walmsley543d9372008-03-18 10:22:06 +0200276 omap2_clk_disable(clk->parent);
Paul Walmsley333943b2008-08-19 11:08:45 +0300277 if (clk->clkdm)
278 omap2_clkdm_clk_disable(clk->clkdm, clk);
279
Paul Walmsley543d9372008-03-18 10:22:06 +0200280 }
281}
282
283int omap2_clk_enable(struct clk *clk)
284{
285 int ret = 0;
286
287 if (clk->usecount++ == 0) {
Paul Walmsley333943b2008-08-19 11:08:45 +0300288 if (clk->clkdm)
289 omap2_clkdm_clk_enable(clk->clkdm, clk);
290
Russell Kinga7f8c592009-01-31 11:00:17 +0000291 if (clk->parent) {
Paul Walmsley543d9372008-03-18 10:22:06 +0200292 ret = omap2_clk_enable(clk->parent);
Russell Kinga7f8c592009-01-31 11:00:17 +0000293 if (ret)
294 goto err;
Paul Walmsley543d9372008-03-18 10:22:06 +0200295 }
296
Paul Walmsley543d9372008-03-18 10:22:06 +0200297 ret = _omap2_clk_enable(clk);
Russell Kinga7f8c592009-01-31 11:00:17 +0000298 if (ret) {
Russell Kinga7f8c592009-01-31 11:00:17 +0000299 if (clk->parent)
Paul Walmsley333943b2008-08-19 11:08:45 +0300300 omap2_clk_disable(clk->parent);
Russell Kinga7f8c592009-01-31 11:00:17 +0000301
302 goto err;
Paul Walmsley543d9372008-03-18 10:22:06 +0200303 }
304 }
Russell Kinga7f8c592009-01-31 11:00:17 +0000305 return ret;
Paul Walmsley543d9372008-03-18 10:22:06 +0200306
Russell Kinga7f8c592009-01-31 11:00:17 +0000307err:
Russell King8263e5b2009-01-31 11:02:37 +0000308 if (clk->clkdm)
309 omap2_clkdm_clk_disable(clk->clkdm, clk);
Russell Kinga7f8c592009-01-31 11:00:17 +0000310 clk->usecount--;
Paul Walmsley543d9372008-03-18 10:22:06 +0200311 return ret;
312}
313
Paul Walmsley543d9372008-03-18 10:22:06 +0200314/* Set the clock rate for a clock source */
315int omap2_clk_set_rate(struct clk *clk, unsigned long rate)
316{
317 int ret = -EINVAL;
318
319 pr_debug("clock: set_rate for clock %s to rate %ld\n", clk->name, rate);
320
Paul Walmsley543d9372008-03-18 10:22:06 +0200321 /* dpll_ck, core_ck, virt_prcm_set; plus all clksel clocks */
Paul Walmsleyfecb4942009-01-27 19:12:50 -0700322 if (clk->set_rate)
Paul Walmsley543d9372008-03-18 10:22:06 +0200323 ret = clk->set_rate(clk, rate);
324
Paul Walmsley543d9372008-03-18 10:22:06 +0200325 return ret;
326}
327
Paul Walmsley543d9372008-03-18 10:22:06 +0200328int omap2_clk_set_parent(struct clk *clk, struct clk *new_parent)
329{
Paul Walmsley543d9372008-03-18 10:22:06 +0200330 if (!clk->clksel)
331 return -EINVAL;
332
Paul Walmsley1a337712010-02-22 22:09:16 -0700333 if (clk->parent == new_parent)
334 return 0;
335
Paul Walmsleydf791b32010-01-26 20:13:04 -0700336 return omap2_clksel_set_parent(clk, new_parent);
Paul Walmsley543d9372008-03-18 10:22:06 +0200337}
338
Paul Walmsley543d9372008-03-18 10:22:06 +0200339/*-------------------------------------------------------------------------
340 * Omap2 clock reset and init functions
341 *-------------------------------------------------------------------------*/
342
343#ifdef CONFIG_OMAP_RESET_CLOCKS
344void omap2_clk_disable_unused(struct clk *clk)
345{
346 u32 regval32, v;
347
348 v = (clk->flags & INVERT_ENABLE) ? (1 << clk->enable_bit) : 0;
349
350 regval32 = __raw_readl(clk->enable_reg);
351 if ((regval32 & (1 << clk->enable_bit)) == v)
352 return;
353
Artem Bityutskiy0db4e822009-05-12 17:34:40 -0600354 printk(KERN_DEBUG "Disabling unused clock \"%s\"\n", clk->name);
Tero Kristo8463e202009-01-28 12:27:45 -0700355 if (cpu_is_omap34xx()) {
356 omap2_clk_enable(clk);
357 omap2_clk_disable(clk);
358 } else
359 _omap2_clk_disable(clk);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300360 if (clk->clkdm != NULL)
361 pwrdm_clkdm_state_switch(clk->clkdm);
Paul Walmsley543d9372008-03-18 10:22:06 +0200362}
363#endif
Paul Walmsley69ecefc2010-01-26 20:13:04 -0700364
365/* Common data */
366
367struct 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,
378#endif
379};
380