blob: a70ba29f66cc98319c2d4b5fcb7f30c494d60322 [file] [log] [blame]
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03001/*
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -07002 * OMAP2/3/4 clockdomain framework functions
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03003 *
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -07004 * Copyright (C) 2008-2009 Texas Instruments, Inc.
Paul Walmsley33903eb2009-12-08 16:33:10 -07005 * Copyright (C) 2008-2009 Nokia Corporation
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03006 *
7 * Written by Paul Walmsley and Jouni Högander
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -07008 * Added OMAP4 specific support by Abhijit Pagare <abhijitpagare@ti.com>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +03009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
Paul Walmsley33903eb2009-12-08 16:33:10 -070014#undef DEBUG
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030015
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/device.h>
19#include <linux/list.h>
20#include <linux/errno.h>
21#include <linux/delay.h>
22#include <linux/clk.h>
23#include <linux/limits.h>
Paul Walmsley5b74c672009-02-03 02:10:03 -070024#include <linux/err.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030025
26#include <linux/io.h>
27
28#include <linux/bitops.h>
29
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030030#include "prm.h"
31#include "prm-regbits-24xx.h"
32#include "cm.h"
33
Paul Walmsley55ed9692010-01-26 20:12:59 -070034#include <plat/clock.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070035#include <plat/powerdomain.h>
36#include <plat/clockdomain.h>
Paul Walmsley55ed9692010-01-26 20:12:59 -070037#include <plat/prcm.h>
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030038
39/* clkdm_list contains all registered struct clockdomains */
40static LIST_HEAD(clkdm_list);
41
42/* clkdm_mutex protects clkdm_list add and del ops */
43static DEFINE_MUTEX(clkdm_mutex);
44
Paul Walmsley55ed9692010-01-26 20:12:59 -070045/* array of clockdomain deps to be added/removed when clkdm in hwsup mode */
46static struct clkdm_autodep *autodeps;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030047
48
49/* Private functions */
50
Paul Walmsley55ed9692010-01-26 20:12:59 -070051static struct clockdomain *_clkdm_lookup(const char *name)
52{
53 struct clockdomain *clkdm, *temp_clkdm;
54
55 if (!name)
56 return NULL;
57
58 clkdm = NULL;
59
60 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
61 if (!strcmp(name, temp_clkdm->name)) {
62 clkdm = temp_clkdm;
63 break;
64 }
65 }
66
67 return clkdm;
68}
69
70/* _clkdm_deps_lookup - look up the specified clockdomain in a clkdm list */
71static struct clkdm_dep *_clkdm_deps_lookup(struct clockdomain *clkdm,
72 struct clkdm_dep *deps)
73{
74 struct clkdm_dep *cd;
75
76 if (!clkdm || !deps || !omap_chip_is(clkdm->omap_chip))
77 return ERR_PTR(-EINVAL);
78
79 for (cd = deps; cd->clkdm_name; cd++) {
80
81 if (!omap_chip_is(cd->omap_chip))
82 continue;
83
84 if (!cd->clkdm && cd->clkdm_name)
85 cd->clkdm = _clkdm_lookup(cd->clkdm_name);
86
87 if (cd->clkdm == clkdm)
88 break;
89
90 }
91
92 if (!cd->clkdm_name)
93 return ERR_PTR(-ENOENT);
94
95 return cd;
96}
97
Paul Walmsleyd459bfe2008-08-19 11:08:43 +030098/*
Paul Walmsley55ed9692010-01-26 20:12:59 -070099 * _autodep_lookup - resolve autodep clkdm names to clkdm pointers; store
100 * @autodep: struct clkdm_autodep * to resolve
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300101 *
Paul Walmsley55ed9692010-01-26 20:12:59 -0700102 * Resolve autodep clockdomain names to clockdomain pointers via
103 * clkdm_lookup() and store the pointers in the autodep structure. An
104 * "autodep" is a clockdomain sleep/wakeup dependency that is
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300105 * automatically added and removed whenever clocks in the associated
106 * clockdomain are enabled or disabled (respectively) when the
107 * clockdomain is in hardware-supervised mode. Meant to be called
108 * once at clockdomain layer initialization, since these should remain
109 * fixed for a particular architecture. No return value.
110 */
Paul Walmsley55ed9692010-01-26 20:12:59 -0700111static void _autodep_lookup(struct clkdm_autodep *autodep)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300112{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700113 struct clockdomain *clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300114
115 if (!autodep)
116 return;
117
118 if (!omap_chip_is(autodep->omap_chip))
119 return;
120
Paul Walmsley55ed9692010-01-26 20:12:59 -0700121 clkdm = clkdm_lookup(autodep->clkdm.name);
122 if (!clkdm) {
123 pr_err("clockdomain: autodeps: clockdomain %s does not exist\n",
124 autodep->clkdm.name);
125 clkdm = ERR_PTR(-ENOENT);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300126 }
Paul Walmsley55ed9692010-01-26 20:12:59 -0700127 autodep->clkdm.ptr = clkdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300128}
129
130/*
131 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
132 * @clkdm: struct clockdomain *
133 *
134 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
135 * in hardware-supervised mode. Meant to be called from clock framework
136 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
137 */
138static void _clkdm_add_autodeps(struct clockdomain *clkdm)
139{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700140 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300141
Paul Walmsley55ed9692010-01-26 20:12:59 -0700142 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
143 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300144 continue;
145
Paul Walmsleyd96df002009-01-27 19:44:35 -0700146 if (!omap_chip_is(autodep->omap_chip))
147 continue;
148
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300149 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700150 "clkdm %s\n", autodep->clkdm.ptr->name,
151 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300152
Paul Walmsley55ed9692010-01-26 20:12:59 -0700153 clkdm_add_sleepdep(clkdm, autodep->clkdm.ptr);
154 clkdm_add_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300155 }
156}
157
158/*
159 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
160 * @clkdm: struct clockdomain *
161 *
162 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
163 * in hardware-supervised mode. Meant to be called from clock framework
164 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
165 */
166static void _clkdm_del_autodeps(struct clockdomain *clkdm)
167{
Paul Walmsley55ed9692010-01-26 20:12:59 -0700168 struct clkdm_autodep *autodep;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300169
Paul Walmsley55ed9692010-01-26 20:12:59 -0700170 for (autodep = autodeps; autodep->clkdm.ptr; autodep++) {
171 if (IS_ERR(autodep->clkdm.ptr))
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300172 continue;
173
Paul Walmsleyd96df002009-01-27 19:44:35 -0700174 if (!omap_chip_is(autodep->omap_chip))
175 continue;
176
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300177 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
Paul Walmsley55ed9692010-01-26 20:12:59 -0700178 "clkdm %s\n", autodep->clkdm.ptr->name,
179 clkdm->name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300180
Paul Walmsley55ed9692010-01-26 20:12:59 -0700181 clkdm_del_sleepdep(clkdm, autodep->clkdm.ptr);
182 clkdm_del_wkdep(clkdm, autodep->clkdm.ptr);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300183 }
184}
185
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600186/*
187 * _omap2_clkdm_set_hwsup - set the hwsup idle transition bit
188 * @clkdm: struct clockdomain *
189 * @enable: int 0 to disable, 1 to enable
190 *
191 * Internal helper for actually switching the bit that controls hwsup
192 * idle transitions for clkdm.
193 */
194static void _omap2_clkdm_set_hwsup(struct clockdomain *clkdm, int enable)
195{
Abhijit Pagareb0994742010-01-26 20:12:53 -0700196 u32 bits, v;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600197
198 if (cpu_is_omap24xx()) {
199 if (enable)
Abhijit Pagareb0994742010-01-26 20:12:53 -0700200 bits = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600201 else
Abhijit Pagareb0994742010-01-26 20:12:53 -0700202 bits = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -0700203 } else if (cpu_is_omap34xx() | cpu_is_omap44xx()) {
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600204 if (enable)
Abhijit Pagareb0994742010-01-26 20:12:53 -0700205 bits = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600206 else
Abhijit Pagareb0994742010-01-26 20:12:53 -0700207 bits = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600208 } else {
209 BUG();
210 }
211
Abhijit Pagareb0994742010-01-26 20:12:53 -0700212 bits = bits << __ffs(clkdm->clktrctrl_mask);
213
214 v = __raw_readl(clkdm->clkstctrl_reg);
215 v &= ~(clkdm->clktrctrl_mask);
216 v |= bits;
217 __raw_writel(v, clkdm->clkstctrl_reg);
218
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600219}
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300220
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300221
222/* Public functions */
223
224/**
225 * clkdm_init - set up the clockdomain layer
226 * @clkdms: optional pointer to an array of clockdomains to register
227 * @init_autodeps: optional pointer to an array of autodeps to register
228 *
229 * Set up internal state. If a pointer to an array of clockdomains
230 * was supplied, loop through the list of clockdomains, register all
Paul Walmsley55ed9692010-01-26 20:12:59 -0700231 * that are available on the current platform. Similarly, if a pointer
232 * to an array of clockdomain autodependencies was provided, register
233 * those. No return value.
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300234 */
235void clkdm_init(struct clockdomain **clkdms,
Paul Walmsley55ed9692010-01-26 20:12:59 -0700236 struct clkdm_autodep *init_autodeps)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300237{
238 struct clockdomain **c = NULL;
Paul Walmsley55ed9692010-01-26 20:12:59 -0700239 struct clkdm_autodep *autodep = NULL;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300240
241 if (clkdms)
242 for (c = clkdms; *c; c++)
243 clkdm_register(*c);
244
Paul Walmsley55ed9692010-01-26 20:12:59 -0700245 autodeps = init_autodeps;
246 if (autodeps)
247 for (autodep = autodeps; autodep->clkdm.ptr; autodep++)
248 _autodep_lookup(autodep);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300249}
250
251/**
252 * clkdm_register - register a clockdomain
253 * @clkdm: struct clockdomain * to register
254 *
255 * Adds a clockdomain to the internal clockdomain list.
256 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
257 * already registered by the provided name, or 0 upon success.
258 */
259int clkdm_register(struct clockdomain *clkdm)
260{
261 int ret = -EINVAL;
262 struct powerdomain *pwrdm;
263
264 if (!clkdm || !clkdm->name)
265 return -EINVAL;
266
267 if (!omap_chip_is(clkdm->omap_chip))
268 return -EINVAL;
269
Paul Walmsley5b74c672009-02-03 02:10:03 -0700270 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300271 if (!pwrdm) {
Russell King7aec53a2009-02-22 21:00:55 +0000272 pr_err("clockdomain: %s: powerdomain %s does not exist\n",
273 clkdm->name, clkdm->pwrdm.name);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300274 return -EINVAL;
275 }
Paul Walmsley5b74c672009-02-03 02:10:03 -0700276 clkdm->pwrdm.ptr = pwrdm;
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300277
278 mutex_lock(&clkdm_mutex);
279 /* Verify that the clockdomain is not already registered */
280 if (_clkdm_lookup(clkdm->name)) {
281 ret = -EEXIST;
282 goto cr_unlock;
Russell King7aec53a2009-02-22 21:00:55 +0000283 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300284
285 list_add(&clkdm->node, &clkdm_list);
286
Paul Walmsley8420bb12008-08-19 11:08:44 +0300287 pwrdm_add_clkdm(pwrdm, clkdm);
288
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300289 pr_debug("clockdomain: registered %s\n", clkdm->name);
290 ret = 0;
291
292cr_unlock:
293 mutex_unlock(&clkdm_mutex);
294
295 return ret;
296}
297
298/**
299 * clkdm_unregister - unregister a clockdomain
300 * @clkdm: struct clockdomain * to unregister
301 *
302 * Removes a clockdomain from the internal clockdomain list. Returns
303 * -EINVAL if clkdm argument is NULL.
304 */
305int clkdm_unregister(struct clockdomain *clkdm)
306{
307 if (!clkdm)
308 return -EINVAL;
309
Paul Walmsley5b74c672009-02-03 02:10:03 -0700310 pwrdm_del_clkdm(clkdm->pwrdm.ptr, clkdm);
Paul Walmsley8420bb12008-08-19 11:08:44 +0300311
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300312 mutex_lock(&clkdm_mutex);
313 list_del(&clkdm->node);
314 mutex_unlock(&clkdm_mutex);
315
316 pr_debug("clockdomain: unregistered %s\n", clkdm->name);
317
318 return 0;
319}
320
321/**
322 * clkdm_lookup - look up a clockdomain by name, return a pointer
323 * @name: name of clockdomain
324 *
325 * Find a registered clockdomain by its name. Returns a pointer to the
326 * struct clockdomain if found, or NULL otherwise.
327 */
328struct clockdomain *clkdm_lookup(const char *name)
329{
330 struct clockdomain *clkdm, *temp_clkdm;
331
332 if (!name)
333 return NULL;
334
335 clkdm = NULL;
336
337 mutex_lock(&clkdm_mutex);
338 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
339 if (!strcmp(name, temp_clkdm->name)) {
340 clkdm = temp_clkdm;
341 break;
342 }
343 }
344 mutex_unlock(&clkdm_mutex);
345
346 return clkdm;
347}
348
349/**
350 * clkdm_for_each - call function on each registered clockdomain
351 * @fn: callback function *
352 *
353 * Call the supplied function for each registered clockdomain.
354 * The callback function can return anything but 0 to bail
355 * out early from the iterator. The callback function is called with
356 * the clkdm_mutex held, so no clockdomain structure manipulation
357 * functions should be called from the callback, although hardware
358 * clockdomain control functions are fine. Returns the last return
359 * value of the callback function, which should be 0 for success or
360 * anything else to indicate failure; or -EINVAL if the function pointer
361 * is null.
362 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300363int clkdm_for_each(int (*fn)(struct clockdomain *clkdm, void *user),
364 void *user)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300365{
366 struct clockdomain *clkdm;
367 int ret = 0;
368
369 if (!fn)
370 return -EINVAL;
371
372 mutex_lock(&clkdm_mutex);
373 list_for_each_entry(clkdm, &clkdm_list, node) {
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300374 ret = (*fn)(clkdm, user);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300375 if (ret)
376 break;
377 }
378 mutex_unlock(&clkdm_mutex);
379
380 return ret;
381}
382
383
Paul Walmsleye89087c2008-05-20 18:41:35 -0600384/**
385 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
386 * @clkdm: struct clockdomain *
387 *
388 * Return a pointer to the struct powerdomain that the specified clockdomain
389 * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
390 */
391struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
392{
393 if (!clkdm)
394 return NULL;
395
Paul Walmsley5b74c672009-02-03 02:10:03 -0700396 return clkdm->pwrdm.ptr;
Paul Walmsleye89087c2008-05-20 18:41:35 -0600397}
398
399
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300400/* Hardware clockdomain control */
401
402/**
Paul Walmsley55ed9692010-01-26 20:12:59 -0700403 * clkdm_add_wkdep - add a wakeup dependency from clkdm2 to clkdm1
404 * @clkdm1: wake this struct clockdomain * up (dependent)
405 * @clkdm2: when this struct clockdomain * wakes up (source)
406 *
407 * When the clockdomain represented by @clkdm2 wakes up, wake up
408 * @clkdm1. Implemented in hardware on the OMAP, this feature is
409 * designed to reduce wakeup latency of the dependent clockdomain @clkdm1.
410 * Returns -EINVAL if presented with invalid clockdomain pointers,
411 * -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or 0 upon
412 * success.
413 */
414int clkdm_add_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
415{
416 struct clkdm_dep *cd;
417
418 if (!clkdm1 || !clkdm2)
419 return -EINVAL;
420
421 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
422 if (IS_ERR(cd)) {
423 pr_debug("clockdomain: hardware cannot set/clear wake up of "
424 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
425 return PTR_ERR(cd);
426 }
427
428 pr_debug("clockdomain: hardware will wake up %s when %s wakes up\n",
429 clkdm1->name, clkdm2->name);
430
431 prm_set_mod_reg_bits((1 << clkdm2->dep_bit),
432 clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
433
434 return 0;
435}
436
437/**
438 * clkdm_del_wkdep - remove a wakeup dependency from clkdm2 to clkdm1
439 * @clkdm1: wake this struct clockdomain * up (dependent)
440 * @clkdm2: when this struct clockdomain * wakes up (source)
441 *
442 * Remove a wakeup dependency causing @clkdm1 to wake up when @clkdm2
443 * wakes up. Returns -EINVAL if presented with invalid clockdomain
444 * pointers, -ENOENT if @clkdm2 cannot wake up clkdm1 in hardware, or
445 * 0 upon success.
446 */
447int clkdm_del_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
448{
449 struct clkdm_dep *cd;
450
451 if (!clkdm1 || !clkdm2)
452 return -EINVAL;
453
454 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
455 if (IS_ERR(cd)) {
456 pr_debug("clockdomain: hardware cannot set/clear wake up of "
457 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
458 return PTR_ERR(cd);
459 }
460
461 pr_debug("clockdomain: hardware will no longer wake up %s after %s "
462 "wakes up\n", clkdm1->name, clkdm2->name);
463
464 prm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
465 clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP);
466
467 return 0;
468}
469
470/**
471 * clkdm_read_wkdep - read wakeup dependency state from clkdm2 to clkdm1
472 * @clkdm1: wake this struct clockdomain * up (dependent)
473 * @clkdm2: when this struct clockdomain * wakes up (source)
474 *
475 * Return 1 if a hardware wakeup dependency exists wherein @clkdm1 will be
476 * awoken when @clkdm2 wakes up; 0 if dependency is not set; -EINVAL
477 * if either clockdomain pointer is invalid; or -ENOENT if the hardware
478 * is incapable.
479 *
480 * REVISIT: Currently this function only represents software-controllable
481 * wakeup dependencies. Wakeup dependencies fixed in hardware are not
482 * yet handled here.
483 */
484int clkdm_read_wkdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
485{
486 struct clkdm_dep *cd;
487
488 if (!clkdm1 || !clkdm2)
489 return -EINVAL;
490
491 cd = _clkdm_deps_lookup(clkdm2, clkdm1->wkdep_srcs);
492 if (IS_ERR(cd)) {
493 pr_debug("clockdomain: hardware cannot set/clear wake up of "
494 "%s when %s wakes up\n", clkdm1->name, clkdm2->name);
495 return PTR_ERR(cd);
496 }
497
498 return prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs, PM_WKDEP,
499 (1 << clkdm2->dep_bit));
500}
501
502/**
503 * clkdm_add_sleepdep - add a sleep dependency from clkdm2 to clkdm1
504 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
505 * @clkdm2: when this struct clockdomain * is active (source)
506 *
507 * Prevent @clkdm1 from automatically going inactive (and then to
508 * retention or off) if @clkdm2 is active. Returns -EINVAL if
509 * presented with invalid clockdomain pointers or called on a machine
510 * that does not support software-configurable hardware sleep
511 * dependencies, -ENOENT if the specified dependency cannot be set in
512 * hardware, or 0 upon success.
513 */
514int clkdm_add_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
515{
516 struct clkdm_dep *cd;
517
518 if (!cpu_is_omap34xx())
519 return -EINVAL;
520
521 if (!clkdm1 || !clkdm2)
522 return -EINVAL;
523
524 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
525 if (IS_ERR(cd)) {
526 pr_debug("clockdomain: hardware cannot set/clear sleep "
527 "dependency affecting %s from %s\n", clkdm1->name,
528 clkdm2->name);
529 return PTR_ERR(cd);
530 }
531
532 pr_debug("clockdomain: will prevent %s from sleeping if %s is active\n",
533 clkdm1->name, clkdm2->name);
534
535 cm_set_mod_reg_bits((1 << clkdm2->dep_bit),
536 clkdm1->pwrdm.ptr->prcm_offs,
537 OMAP3430_CM_SLEEPDEP);
538
539 return 0;
540}
541
542/**
543 * clkdm_del_sleepdep - remove a sleep dependency from clkdm2 to clkdm1
544 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
545 * @clkdm2: when this struct clockdomain * is active (source)
546 *
547 * Allow @clkdm1 to automatically go inactive (and then to retention or
548 * off), independent of the activity state of @clkdm2. Returns -EINVAL
549 * if presented with invalid clockdomain pointers or called on a machine
550 * that does not support software-configurable hardware sleep dependencies,
551 * -ENOENT if the specified dependency cannot be cleared in hardware, or
552 * 0 upon success.
553 */
554int clkdm_del_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
555{
556 struct clkdm_dep *cd;
557
558 if (!cpu_is_omap34xx())
559 return -EINVAL;
560
561 if (!clkdm1 || !clkdm2)
562 return -EINVAL;
563
564 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
565 if (IS_ERR(cd)) {
566 pr_debug("clockdomain: hardware cannot set/clear sleep "
567 "dependency affecting %s from %s\n", clkdm1->name,
568 clkdm2->name);
569 return PTR_ERR(cd);
570 }
571
572 pr_debug("clockdomain: will no longer prevent %s from sleeping if "
573 "%s is active\n", clkdm1->name, clkdm2->name);
574
575 cm_clear_mod_reg_bits((1 << clkdm2->dep_bit),
576 clkdm1->pwrdm.ptr->prcm_offs,
577 OMAP3430_CM_SLEEPDEP);
578
579 return 0;
580}
581
582/**
583 * clkdm_read_sleepdep - read sleep dependency state from clkdm2 to clkdm1
584 * @clkdm1: prevent this struct clockdomain * from sleeping (dependent)
585 * @clkdm2: when this struct clockdomain * is active (source)
586 *
587 * Return 1 if a hardware sleep dependency exists wherein @clkdm1 will
588 * not be allowed to automatically go inactive if @clkdm2 is active;
589 * 0 if @clkdm1's automatic power state inactivity transition is independent
590 * of @clkdm2's; -EINVAL if either clockdomain pointer is invalid or called
591 * on a machine that does not support software-configurable hardware sleep
592 * dependencies; or -ENOENT if the hardware is incapable.
593 *
594 * REVISIT: Currently this function only represents software-controllable
595 * sleep dependencies. Sleep dependencies fixed in hardware are not
596 * yet handled here.
597 */
598int clkdm_read_sleepdep(struct clockdomain *clkdm1, struct clockdomain *clkdm2)
599{
600 struct clkdm_dep *cd;
601
602 if (!cpu_is_omap34xx())
603 return -EINVAL;
604
605 if (!clkdm1 || !clkdm2)
606 return -EINVAL;
607
608 cd = _clkdm_deps_lookup(clkdm2, clkdm1->sleepdep_srcs);
609 if (IS_ERR(cd)) {
610 pr_debug("clockdomain: hardware cannot set/clear sleep "
611 "dependency affecting %s from %s\n", clkdm1->name,
612 clkdm2->name);
613 return PTR_ERR(cd);
614 }
615
616 return prm_read_mod_bits_shift(clkdm1->pwrdm.ptr->prcm_offs,
617 OMAP3430_CM_SLEEPDEP,
618 (1 << clkdm2->dep_bit));
619}
620
621
622/**
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300623 * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
624 * @clk: struct clk * of a clockdomain
625 *
626 * Return the clockdomain's current state transition mode from the
Abhijit Pagare84c0c392010-01-26 20:12:53 -0700627 * corresponding domain OMAP2_CM_CLKSTCTRL register. Returns -EINVAL if clk
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300628 * is NULL or the current mode upon success.
629 */
630static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
631{
632 u32 v;
633
634 if (!clkdm)
635 return -EINVAL;
636
Abhijit Pagareb0994742010-01-26 20:12:53 -0700637 v = __raw_readl(clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300638 v &= clkdm->clktrctrl_mask;
639 v >>= __ffs(clkdm->clktrctrl_mask);
640
641 return v;
642}
643
644/**
645 * omap2_clkdm_sleep - force clockdomain sleep transition
646 * @clkdm: struct clockdomain *
647 *
648 * Instruct the CM to force a sleep transition on the specified
649 * clockdomain 'clkdm'. Returns -EINVAL if clk is NULL or if
650 * clockdomain does not support software-initiated sleep; 0 upon
651 * success.
652 */
653int omap2_clkdm_sleep(struct clockdomain *clkdm)
654{
655 if (!clkdm)
656 return -EINVAL;
657
658 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
659 pr_debug("clockdomain: %s does not support forcing "
660 "sleep via software\n", clkdm->name);
661 return -EINVAL;
662 }
663
664 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
665
666 if (cpu_is_omap24xx()) {
667
668 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
Abhijit Pagare37903002010-01-26 20:12:51 -0700669 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300670
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -0700671 } else if (cpu_is_omap34xx() | cpu_is_omap44xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300672
Abhijit Pagareb0994742010-01-26 20:12:53 -0700673 u32 bits = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300674 __ffs(clkdm->clktrctrl_mask));
675
Abhijit Pagareb0994742010-01-26 20:12:53 -0700676 u32 v = __raw_readl(clkdm->clkstctrl_reg);
677 v &= ~(clkdm->clktrctrl_mask);
678 v |= bits;
679 __raw_writel(v, clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300680
681 } else {
682 BUG();
683 };
684
685 return 0;
686}
687
688/**
689 * omap2_clkdm_wakeup - force clockdomain wakeup transition
690 * @clkdm: struct clockdomain *
691 *
692 * Instruct the CM to force a wakeup transition on the specified
693 * clockdomain 'clkdm'. Returns -EINVAL if clkdm is NULL or if the
694 * clockdomain does not support software-controlled wakeup; 0 upon
695 * success.
696 */
697int omap2_clkdm_wakeup(struct clockdomain *clkdm)
698{
699 if (!clkdm)
700 return -EINVAL;
701
702 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
703 pr_debug("clockdomain: %s does not support forcing "
704 "wakeup via software\n", clkdm->name);
705 return -EINVAL;
706 }
707
708 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
709
710 if (cpu_is_omap24xx()) {
711
712 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
Abhijit Pagare37903002010-01-26 20:12:51 -0700713 clkdm->pwrdm.ptr->prcm_offs, OMAP2_PM_PWSTCTRL);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300714
Abhijit Pagare8a3ddc72010-01-26 20:12:54 -0700715 } else if (cpu_is_omap34xx() | cpu_is_omap44xx()) {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300716
Abhijit Pagareb0994742010-01-26 20:12:53 -0700717 u32 bits = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300718 __ffs(clkdm->clktrctrl_mask));
719
Abhijit Pagareb0994742010-01-26 20:12:53 -0700720 u32 v = __raw_readl(clkdm->clkstctrl_reg);
721 v &= ~(clkdm->clktrctrl_mask);
722 v |= bits;
723 __raw_writel(v, clkdm->clkstctrl_reg);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300724
725 } else {
726 BUG();
727 };
728
729 return 0;
730}
731
732/**
733 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
734 * @clkdm: struct clockdomain *
735 *
736 * Allow the hardware to automatically switch the clockdomain into
737 * active or idle states, as needed by downstream clocks. If the
738 * clockdomain has any downstream clocks enabled in the clock
739 * framework, wkdep/sleepdep autodependencies are added; this is so
740 * device drivers can read and write to the device. No return value.
741 */
742void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
743{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300744 if (!clkdm)
745 return;
746
747 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
748 pr_debug("clock: automatic idle transitions cannot be enabled "
749 "on clockdomain %s\n", clkdm->name);
750 return;
751 }
752
753 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
754 clkdm->name);
755
756 if (atomic_read(&clkdm->usecount) > 0)
757 _clkdm_add_autodeps(clkdm);
758
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600759 _omap2_clkdm_set_hwsup(clkdm, 1);
Peter 'p2' De Schrijverba20bb12008-10-15 17:48:43 +0300760
761 pwrdm_clkdm_state_switch(clkdm);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300762}
763
764/**
765 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
766 * @clkdm: struct clockdomain *
767 *
768 * Prevent the hardware from automatically switching the clockdomain
769 * into inactive or idle states. If the clockdomain has downstream
770 * clocks enabled in the clock framework, wkdep/sleepdep
771 * autodependencies are removed. No return value.
772 */
773void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
774{
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300775 if (!clkdm)
776 return;
777
778 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
779 pr_debug("clockdomain: automatic idle transitions cannot be "
780 "disabled on %s\n", clkdm->name);
781 return;
782 }
783
784 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
785 clkdm->name);
786
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600787 _omap2_clkdm_set_hwsup(clkdm, 0);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300788
789 if (atomic_read(&clkdm->usecount) > 0)
790 _clkdm_del_autodeps(clkdm);
791}
792
793
794/* Clockdomain-to-clock framework interface code */
795
796/**
797 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
798 * @clkdm: struct clockdomain *
799 * @clk: struct clk * of the enabled downstream clock
800 *
801 * Increment the usecount of this clockdomain 'clkdm' and ensure that
802 * it is awake. Intended to be called by clk_enable() code. If the
803 * clockdomain is in software-supervised idle mode, force the
804 * clockdomain to wake. If the clockdomain is in hardware-supervised
805 * idle mode, add clkdm-pwrdm autodependencies, to ensure that devices
806 * in the clockdomain can be read from/written to by on-chip processors.
807 * Returns -EINVAL if passed null pointers; returns 0 upon success or
808 * if the clockdomain is in hwsup idle mode.
809 */
810int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
811{
812 int v;
813
814 /*
815 * XXX Rewrite this code to maintain a list of enabled
816 * downstream clocks for debugging purposes?
817 */
818
Abhijit Pagareb0994742010-01-26 20:12:53 -0700819 if (!clkdm || !clk || !clkdm->clkstctrl_reg)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300820 return -EINVAL;
821
822 if (atomic_inc_return(&clkdm->usecount) > 1)
823 return 0;
824
825 /* Clockdomain now has one enabled downstream clock */
826
827 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
828 clk->name);
829
830 v = omap2_clkdm_clktrctrl_read(clkdm);
831
832 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600833 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
834 /* Disable HW transitions when we are changing deps */
835 _omap2_clkdm_set_hwsup(clkdm, 0);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300836 _clkdm_add_autodeps(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600837 _omap2_clkdm_set_hwsup(clkdm, 1);
838 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300839 omap2_clkdm_wakeup(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600840 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300841
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700842 pwrdm_wait_transition(clkdm->pwrdm.ptr);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300843 pwrdm_clkdm_state_switch(clkdm);
Tomi Valkeinen054ce502009-01-27 19:44:31 -0700844
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300845 return 0;
846}
847
848/**
849 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
850 * @clkdm: struct clockdomain *
851 * @clk: struct clk * of the disabled downstream clock
852 *
853 * Decrement the usecount of this clockdomain 'clkdm'. Intended to be
854 * called by clk_disable() code. If the usecount goes to 0, put the
855 * clockdomain to sleep (software-supervised mode) or remove the
856 * clkdm-pwrdm autodependencies (hardware-supervised mode). Returns
857 * -EINVAL if passed null pointers; -ERANGE if the clkdm usecount
858 * underflows and debugging is enabled; or returns 0 upon success or
859 * if the clockdomain is in hwsup idle mode.
860 */
861int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
862{
863 int v;
864
865 /*
866 * XXX Rewrite this code to maintain a list of enabled
867 * downstream clocks for debugging purposes?
868 */
869
Abhijit Pagareb0994742010-01-26 20:12:53 -0700870 if (!clkdm || !clk || !clkdm->clkstctrl_reg)
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300871 return -EINVAL;
872
873#ifdef DEBUG
874 if (atomic_read(&clkdm->usecount) == 0) {
875 WARN_ON(1); /* underflow */
876 return -ERANGE;
877 }
878#endif
879
880 if (atomic_dec_return(&clkdm->usecount) > 0)
881 return 0;
882
883 /* All downstream clocks of this clockdomain are now disabled */
884
885 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
886 clk->name);
887
888 v = omap2_clkdm_clktrctrl_read(clkdm);
889
890 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600891 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO)) {
892 /* Disable HW transitions when we are changing deps */
893 _omap2_clkdm_set_hwsup(clkdm, 0);
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300894 _clkdm_del_autodeps(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600895 _omap2_clkdm_set_hwsup(clkdm, 1);
896 } else {
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300897 omap2_clkdm_sleep(clkdm);
Kalle Jokiniemia0219fb2009-10-14 16:40:37 -0600898 }
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300899
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300900 pwrdm_clkdm_state_switch(clkdm);
901
Paul Walmsleyd459bfe2008-08-19 11:08:43 +0300902 return 0;
903}
904