2 * OMAP2/3 clockdomain framework functions
4 * Copyright (C) 2008 Texas Instruments, Inc.
5 * Copyright (C) 2008 Nokia Corporation
7 * Written by Paul Walmsley and Jouni Högander
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 #ifdef CONFIG_OMAP_DEBUG_CLOCKDOMAIN
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 <linux/limits.h>
28 #include <linux/bitops.h>
30 #include <asm/arch/clock.h>
33 #include "prm-regbits-24xx.h"
36 #include <asm/arch/powerdomain.h>
37 #include <asm/arch/clockdomain.h>
39 /* clkdm_list contains all registered struct clockdomains */
40 static LIST_HEAD(clkdm_list);
42 /* clkdm_mutex protects clkdm_list add and del ops */
43 static DEFINE_MUTEX(clkdm_mutex);
45 /* array of powerdomain deps to be added/removed when clkdm in hwsup mode */
46 static struct clkdm_pwrdm_autodep *autodeps;
49 /* Private functions */
52 * _autodep_lookup - resolve autodep pwrdm names to pwrdm pointers; store
53 * @autodep: struct clkdm_pwrdm_autodep * to resolve
55 * Resolve autodep powerdomain names to powerdomain pointers via
56 * pwrdm_lookup() and store the pointers in the autodep structure. An
57 * "autodep" is a powerdomain sleep/wakeup dependency that is
58 * automatically added and removed whenever clocks in the associated
59 * clockdomain are enabled or disabled (respectively) when the
60 * clockdomain is in hardware-supervised mode. Meant to be called
61 * once at clockdomain layer initialization, since these should remain
62 * fixed for a particular architecture. No return value.
64 static void _autodep_lookup(struct clkdm_pwrdm_autodep *autodep)
66 struct powerdomain *pwrdm;
71 if (!omap_chip_is(autodep->omap_chip))
74 pwrdm = pwrdm_lookup(autodep->pwrdm.name);
76 pr_debug("clockdomain: _autodep_lookup: powerdomain %s "
77 "does not exist\n", autodep->pwrdm.name);
81 autodep->pwrdm.ptr = pwrdm;
87 * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
88 * @clkdm: struct clockdomain *
90 * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
91 * in hardware-supervised mode. Meant to be called from clock framework
92 * when a clock inside clockdomain 'clkdm' is enabled. No return value.
94 static void _clkdm_add_autodeps(struct clockdomain *clkdm)
96 struct clkdm_pwrdm_autodep *autodep;
98 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
99 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
100 "pwrdm %s\n", autodep->pwrdm.ptr->name,
101 clkdm->pwrdm.ptr->name);
103 pwrdm_add_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
104 pwrdm_add_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
109 * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
110 * @clkdm: struct clockdomain *
112 * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
113 * in hardware-supervised mode. Meant to be called from clock framework
114 * when a clock inside clockdomain 'clkdm' is disabled. No return value.
116 static void _clkdm_del_autodeps(struct clockdomain *clkdm)
118 struct clkdm_pwrdm_autodep *autodep;
120 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
121 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
122 "pwrdm %s\n", autodep->pwrdm.ptr->name,
123 clkdm->pwrdm.ptr->name);
125 pwrdm_del_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
126 pwrdm_del_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
131 static struct clockdomain *_clkdm_lookup(const char *name)
133 struct clockdomain *clkdm, *temp_clkdm;
140 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
141 if (!strcmp(name, temp_clkdm->name)) {
151 /* Public functions */
154 * clkdm_init - set up the clockdomain layer
155 * @clkdms: optional pointer to an array of clockdomains to register
156 * @init_autodeps: optional pointer to an array of autodeps to register
158 * Set up internal state. If a pointer to an array of clockdomains
159 * was supplied, loop through the list of clockdomains, register all
160 * that are available on the current platform. Similarly, if a
161 * pointer to an array of clockdomain-powerdomain autodependencies was
162 * provided, register those. No return value.
164 void clkdm_init(struct clockdomain **clkdms,
165 struct clkdm_pwrdm_autodep *init_autodeps)
167 struct clockdomain **c = NULL;
168 struct clkdm_pwrdm_autodep *autodep = NULL;
171 for (c = clkdms; *c; c++)
174 autodeps = init_autodeps;
176 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++)
177 _autodep_lookup(autodep);
181 * clkdm_register - register a clockdomain
182 * @clkdm: struct clockdomain * to register
184 * Adds a clockdomain to the internal clockdomain list.
185 * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
186 * already registered by the provided name, or 0 upon success.
188 int clkdm_register(struct clockdomain *clkdm)
191 struct powerdomain *pwrdm;
193 if (!clkdm || !clkdm->name)
196 if (!omap_chip_is(clkdm->omap_chip))
199 pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
201 pr_debug("clockdomain: clkdm_register %s: powerdomain %s "
202 "does not exist\n", clkdm->name, clkdm->pwrdm.name);
205 clkdm->pwrdm.ptr = pwrdm;
207 mutex_lock(&clkdm_mutex);
208 /* Verify that the clockdomain is not already registered */
209 if (_clkdm_lookup(clkdm->name)) {
214 list_add(&clkdm->node, &clkdm_list);
216 pwrdm_add_clkdm(pwrdm, clkdm);
218 pr_debug("clockdomain: registered %s\n", clkdm->name);
222 mutex_unlock(&clkdm_mutex);
228 * clkdm_unregister - unregister a clockdomain
229 * @clkdm: struct clockdomain * to unregister
231 * Removes a clockdomain from the internal clockdomain list. Returns
232 * -EINVAL if clkdm argument is NULL.
234 int clkdm_unregister(struct clockdomain *clkdm)
239 pwrdm_del_clkdm(clkdm->pwrdm.ptr, clkdm);
241 mutex_lock(&clkdm_mutex);
242 list_del(&clkdm->node);
243 mutex_unlock(&clkdm_mutex);
245 pr_debug("clockdomain: unregistered %s\n", clkdm->name);
251 * clkdm_lookup - look up a clockdomain by name, return a pointer
252 * @name: name of clockdomain
254 * Find a registered clockdomain by its name. Returns a pointer to the
255 * struct clockdomain if found, or NULL otherwise.
257 struct clockdomain *clkdm_lookup(const char *name)
259 struct clockdomain *clkdm, *temp_clkdm;
266 mutex_lock(&clkdm_mutex);
267 list_for_each_entry(temp_clkdm, &clkdm_list, node) {
268 if (!strcmp(name, temp_clkdm->name)) {
273 mutex_unlock(&clkdm_mutex);
279 * clkdm_for_each - call function on each registered clockdomain
280 * @fn: callback function *
282 * Call the supplied function for each registered clockdomain.
283 * The callback function can return anything but 0 to bail
284 * out early from the iterator. The callback function is called with
285 * the clkdm_mutex held, so no clockdomain structure manipulation
286 * functions should be called from the callback, although hardware
287 * clockdomain control functions are fine. Returns the last return
288 * value of the callback function, which should be 0 for success or
289 * anything else to indicate failure; or -EINVAL if the function pointer
292 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm))
294 struct clockdomain *clkdm;
300 mutex_lock(&clkdm_mutex);
301 list_for_each_entry(clkdm, &clkdm_list, node) {
306 mutex_unlock(&clkdm_mutex);
313 * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
314 * @clkdm: struct clockdomain *
316 * Return a pointer to the struct powerdomain that the specified clockdomain
317 * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
319 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
324 return clkdm->pwrdm.ptr;
328 /* Hardware clockdomain control */
331 * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
332 * @clk: struct clk * of a clockdomain
334 * Return the clockdomain's current state transition mode from the
335 * corresponding domain CM_CLKSTCTRL register. Returns -EINVAL if clk
336 * is NULL or the current mode upon success.
338 static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
345 v = cm_read_mod_reg(clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
346 v &= clkdm->clktrctrl_mask;
347 v >>= __ffs(clkdm->clktrctrl_mask);
353 * omap2_clkdm_sleep - force clockdomain sleep transition
354 * @clkdm: struct clockdomain *
356 * Instruct the CM to force a sleep transition on the specified
357 * clockdomain 'clkdm'. Returns -EINVAL if clk is NULL or if
358 * clockdomain does not support software-initiated sleep; 0 upon
361 int omap2_clkdm_sleep(struct clockdomain *clkdm)
366 if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
367 pr_debug("clockdomain: %s does not support forcing "
368 "sleep via software\n", clkdm->name);
372 pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
374 if (cpu_is_omap24xx()) {
376 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
377 clkdm->pwrdm.ptr->prcm_offs, PM_PWSTCTRL);
379 } else if (cpu_is_omap34xx()) {
381 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
382 __ffs(clkdm->clktrctrl_mask));
384 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
385 clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
395 * omap2_clkdm_wakeup - force clockdomain wakeup transition
396 * @clkdm: struct clockdomain *
398 * Instruct the CM to force a wakeup transition on the specified
399 * clockdomain 'clkdm'. Returns -EINVAL if clkdm is NULL or if the
400 * clockdomain does not support software-controlled wakeup; 0 upon
403 int omap2_clkdm_wakeup(struct clockdomain *clkdm)
408 if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
409 pr_debug("clockdomain: %s does not support forcing "
410 "wakeup via software\n", clkdm->name);
414 pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
416 if (cpu_is_omap24xx()) {
418 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
419 clkdm->pwrdm.ptr->prcm_offs, PM_PWSTCTRL);
421 } else if (cpu_is_omap34xx()) {
423 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
424 __ffs(clkdm->clktrctrl_mask));
426 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
427 clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
437 * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
438 * @clkdm: struct clockdomain *
440 * Allow the hardware to automatically switch the clockdomain into
441 * active or idle states, as needed by downstream clocks. If the
442 * clockdomain has any downstream clocks enabled in the clock
443 * framework, wkdep/sleepdep autodependencies are added; this is so
444 * device drivers can read and write to the device. No return value.
446 void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
453 if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
454 pr_debug("clock: automatic idle transitions cannot be enabled "
455 "on clockdomain %s\n", clkdm->name);
459 pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
462 if (atomic_read(&clkdm->usecount) > 0)
463 _clkdm_add_autodeps(clkdm);
465 if (cpu_is_omap24xx())
466 v = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
467 else if (cpu_is_omap34xx())
468 v = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
473 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
474 v << __ffs(clkdm->clktrctrl_mask),
475 clkdm->pwrdm.ptr->prcm_offs,
480 * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
481 * @clkdm: struct clockdomain *
483 * Prevent the hardware from automatically switching the clockdomain
484 * into inactive or idle states. If the clockdomain has downstream
485 * clocks enabled in the clock framework, wkdep/sleepdep
486 * autodependencies are removed. No return value.
488 void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
495 if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
496 pr_debug("clockdomain: automatic idle transitions cannot be "
497 "disabled on %s\n", clkdm->name);
501 pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
504 if (cpu_is_omap24xx())
505 v = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
506 else if (cpu_is_omap34xx())
507 v = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
511 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
512 v << __ffs(clkdm->clktrctrl_mask),
513 clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
515 if (atomic_read(&clkdm->usecount) > 0)
516 _clkdm_del_autodeps(clkdm);
520 /* Clockdomain-to-clock framework interface code */
523 * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
524 * @clkdm: struct clockdomain *
525 * @clk: struct clk * of the enabled downstream clock
527 * Increment the usecount of this clockdomain 'clkdm' and ensure that
528 * it is awake. Intended to be called by clk_enable() code. If the
529 * clockdomain is in software-supervised idle mode, force the
530 * clockdomain to wake. If the clockdomain is in hardware-supervised
531 * idle mode, add clkdm-pwrdm autodependencies, to ensure that devices
532 * in the clockdomain can be read from/written to by on-chip processors.
533 * Returns -EINVAL if passed null pointers; returns 0 upon success or
534 * if the clockdomain is in hwsup idle mode.
536 int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
541 * XXX Rewrite this code to maintain a list of enabled
542 * downstream clocks for debugging purposes?
548 if (atomic_inc_return(&clkdm->usecount) > 1)
551 /* Clockdomain now has one enabled downstream clock */
553 pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
556 v = omap2_clkdm_clktrctrl_read(clkdm);
558 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
559 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
560 _clkdm_add_autodeps(clkdm);
562 omap2_clkdm_wakeup(clkdm);
568 * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
569 * @clkdm: struct clockdomain *
570 * @clk: struct clk * of the disabled downstream clock
572 * Decrement the usecount of this clockdomain 'clkdm'. Intended to be
573 * called by clk_disable() code. If the usecount goes to 0, put the
574 * clockdomain to sleep (software-supervised mode) or remove the
575 * clkdm-pwrdm autodependencies (hardware-supervised mode). Returns
576 * -EINVAL if passed null pointers; -ERANGE if the clkdm usecount
577 * underflows and debugging is enabled; or returns 0 upon success or
578 * if the clockdomain is in hwsup idle mode.
580 int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
585 * XXX Rewrite this code to maintain a list of enabled
586 * downstream clocks for debugging purposes?
593 if (atomic_read(&clkdm->usecount) == 0) {
594 WARN_ON(1); /* underflow */
599 if (atomic_dec_return(&clkdm->usecount) > 0)
602 /* All downstream clocks of this clockdomain are now disabled */
604 pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
607 v = omap2_clkdm_clktrctrl_read(clkdm);
609 if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
610 (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
611 _clkdm_del_autodeps(clkdm);
613 omap2_clkdm_sleep(clkdm);