]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/clockdomain.c
[ARM] OMAP2/3 clockdomains: combine pwrdm, pwrdm_name into union in struct clockdomain
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / clockdomain.c
1 /*
2  * OMAP2/3 clockdomain framework functions
3  *
4  * Copyright (C) 2008 Texas Instruments, Inc.
5  * Copyright (C) 2008 Nokia Corporation
6  *
7  * Written by Paul Walmsley and Jouni Högander
8  *
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.
12  */
13 #ifdef CONFIG_OMAP_DEBUG_CLOCKDOMAIN
14 #  define DEBUG
15 #endif
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>
24 #include <linux/limits.h>
25 #include <linux/err.h>
26
27 #include <linux/io.h>
28
29 #include <linux/bitops.h>
30
31 #include <mach/clock.h>
32
33 #include "prm.h"
34 #include "prm-regbits-24xx.h"
35 #include "cm.h"
36
37 #include <mach/powerdomain.h>
38 #include <mach/clockdomain.h>
39
40 /* clkdm_list contains all registered struct clockdomains */
41 static LIST_HEAD(clkdm_list);
42
43 /* clkdm_mutex protects clkdm_list add and del ops */
44 static DEFINE_MUTEX(clkdm_mutex);
45
46 /* array of powerdomain deps to be added/removed when clkdm in hwsup mode */
47 static struct clkdm_pwrdm_autodep *autodeps;
48
49
50 /* Private functions */
51
52 /*
53  * _autodep_lookup - resolve autodep pwrdm names to pwrdm pointers; store
54  * @autodep: struct clkdm_pwrdm_autodep * to resolve
55  *
56  * Resolve autodep powerdomain names to powerdomain pointers via
57  * pwrdm_lookup() and store the pointers in the autodep structure.  An
58  * "autodep" is a powerdomain sleep/wakeup dependency that is
59  * automatically added and removed whenever clocks in the associated
60  * clockdomain are enabled or disabled (respectively) when the
61  * clockdomain is in hardware-supervised mode.  Meant to be called
62  * once at clockdomain layer initialization, since these should remain
63  * fixed for a particular architecture.  No return value.
64  */
65 static void _autodep_lookup(struct clkdm_pwrdm_autodep *autodep)
66 {
67         struct powerdomain *pwrdm;
68
69         if (!autodep)
70                 return;
71
72         if (!omap_chip_is(autodep->omap_chip))
73                 return;
74
75         pwrdm = pwrdm_lookup(autodep->pwrdm.name);
76         if (!pwrdm) {
77                 pr_debug("clockdomain: _autodep_lookup: powerdomain %s "
78                          "does not exist\n", autodep->pwrdm.name);
79                 WARN_ON(1);
80                 pwrdm = ERR_PTR(-ENOENT);
81         }
82         autodep->pwrdm.ptr = pwrdm;
83
84         return;
85 }
86
87 /*
88  * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
89  * @clkdm: struct clockdomain *
90  *
91  * Add the "autodep" sleep & wakeup dependencies to clockdomain 'clkdm'
92  * in hardware-supervised mode.  Meant to be called from clock framework
93  * when a clock inside clockdomain 'clkdm' is enabled.  No return value.
94  */
95 static void _clkdm_add_autodeps(struct clockdomain *clkdm)
96 {
97         struct clkdm_pwrdm_autodep *autodep;
98
99         for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
100                 if (IS_ERR(autodep->pwrdm.ptr))
101                         continue;
102
103                 pr_debug("clockdomain: adding %s sleepdep/wkdep for "
104                          "pwrdm %s\n", autodep->pwrdm.ptr->name,
105                          clkdm->pwrdm.ptr->name);
106
107                 pwrdm_add_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
108                 pwrdm_add_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
109         }
110 }
111
112 /*
113  * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
114  * @clkdm: struct clockdomain *
115  *
116  * Remove the "autodep" sleep & wakeup dependencies from clockdomain 'clkdm'
117  * in hardware-supervised mode.  Meant to be called from clock framework
118  * when a clock inside clockdomain 'clkdm' is disabled.  No return value.
119  */
120 static void _clkdm_del_autodeps(struct clockdomain *clkdm)
121 {
122         struct clkdm_pwrdm_autodep *autodep;
123
124         for (autodep = autodeps; autodep->pwrdm.ptr; autodep++) {
125                 if (IS_ERR(autodep->pwrdm.ptr))
126                         continue;
127
128                 pr_debug("clockdomain: removing %s sleepdep/wkdep for "
129                          "pwrdm %s\n", autodep->pwrdm.ptr->name,
130                          clkdm->pwrdm.ptr->name);
131
132                 pwrdm_del_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
133                 pwrdm_del_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
134         }
135 }
136
137
138 static struct clockdomain *_clkdm_lookup(const char *name)
139 {
140         struct clockdomain *clkdm, *temp_clkdm;
141
142         if (!name)
143                 return NULL;
144
145         clkdm = NULL;
146
147         list_for_each_entry(temp_clkdm, &clkdm_list, node) {
148                 if (!strcmp(name, temp_clkdm->name)) {
149                         clkdm = temp_clkdm;
150                         break;
151                 }
152         }
153
154         return clkdm;
155 }
156
157
158 /* Public functions */
159
160 /**
161  * clkdm_init - set up the clockdomain layer
162  * @clkdms: optional pointer to an array of clockdomains to register
163  * @init_autodeps: optional pointer to an array of autodeps to register
164  *
165  * Set up internal state.  If a pointer to an array of clockdomains
166  * was supplied, loop through the list of clockdomains, register all
167  * that are available on the current platform.  Similarly, if a
168  * pointer to an array of clockdomain-powerdomain autodependencies was
169  * provided, register those.  No return value.
170  */
171 void clkdm_init(struct clockdomain **clkdms,
172                 struct clkdm_pwrdm_autodep *init_autodeps)
173 {
174         struct clockdomain **c = NULL;
175         struct clkdm_pwrdm_autodep *autodep = NULL;
176
177         if (clkdms)
178                 for (c = clkdms; *c; c++)
179                         clkdm_register(*c);
180
181         autodeps = init_autodeps;
182         if (autodeps)
183                 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++)
184                         _autodep_lookup(autodep);
185 }
186
187 /**
188  * clkdm_register - register a clockdomain
189  * @clkdm: struct clockdomain * to register
190  *
191  * Adds a clockdomain to the internal clockdomain list.
192  * Returns -EINVAL if given a null pointer, -EEXIST if a clockdomain is
193  * already registered by the provided name, or 0 upon success.
194  */
195 int clkdm_register(struct clockdomain *clkdm)
196 {
197         int ret = -EINVAL;
198         struct powerdomain *pwrdm;
199
200         if (!clkdm || !clkdm->name)
201                 return -EINVAL;
202
203         if (!omap_chip_is(clkdm->omap_chip))
204                 return -EINVAL;
205
206         pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
207         if (!pwrdm) {
208                 pr_debug("clockdomain: clkdm_register %s: powerdomain %s "
209                          "does not exist\n", clkdm->name, clkdm->pwrdm.name);
210                 return -EINVAL;
211         }
212         clkdm->pwrdm.ptr = pwrdm;
213
214         mutex_lock(&clkdm_mutex);
215         /* Verify that the clockdomain is not already registered */
216         if (_clkdm_lookup(clkdm->name)) {
217                 ret = -EEXIST;
218                 goto cr_unlock;
219         };
220
221         list_add(&clkdm->node, &clkdm_list);
222
223         pwrdm_add_clkdm(pwrdm, clkdm);
224
225         pr_debug("clockdomain: registered %s\n", clkdm->name);
226         ret = 0;
227
228 cr_unlock:
229         mutex_unlock(&clkdm_mutex);
230
231         return ret;
232 }
233
234 /**
235  * clkdm_unregister - unregister a clockdomain
236  * @clkdm: struct clockdomain * to unregister
237  *
238  * Removes a clockdomain from the internal clockdomain list.  Returns
239  * -EINVAL if clkdm argument is NULL.
240  */
241 int clkdm_unregister(struct clockdomain *clkdm)
242 {
243         if (!clkdm)
244                 return -EINVAL;
245
246         pwrdm_del_clkdm(clkdm->pwrdm.ptr, clkdm);
247
248         mutex_lock(&clkdm_mutex);
249         list_del(&clkdm->node);
250         mutex_unlock(&clkdm_mutex);
251
252         pr_debug("clockdomain: unregistered %s\n", clkdm->name);
253
254         return 0;
255 }
256
257 /**
258  * clkdm_lookup - look up a clockdomain by name, return a pointer
259  * @name: name of clockdomain
260  *
261  * Find a registered clockdomain by its name.  Returns a pointer to the
262  * struct clockdomain if found, or NULL otherwise.
263  */
264 struct clockdomain *clkdm_lookup(const char *name)
265 {
266         struct clockdomain *clkdm, *temp_clkdm;
267
268         if (!name)
269                 return NULL;
270
271         clkdm = NULL;
272
273         mutex_lock(&clkdm_mutex);
274         list_for_each_entry(temp_clkdm, &clkdm_list, node) {
275                 if (!strcmp(name, temp_clkdm->name)) {
276                         clkdm = temp_clkdm;
277                         break;
278                 }
279         }
280         mutex_unlock(&clkdm_mutex);
281
282         return clkdm;
283 }
284
285 /**
286  * clkdm_for_each - call function on each registered clockdomain
287  * @fn: callback function *
288  *
289  * Call the supplied function for each registered clockdomain.
290  * The callback function can return anything but 0 to bail
291  * out early from the iterator.  The callback function is called with
292  * the clkdm_mutex held, so no clockdomain structure manipulation
293  * functions should be called from the callback, although hardware
294  * clockdomain control functions are fine.  Returns the last return
295  * value of the callback function, which should be 0 for success or
296  * anything else to indicate failure; or -EINVAL if the function pointer
297  * is null.
298  */
299 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm))
300 {
301         struct clockdomain *clkdm;
302         int ret = 0;
303
304         if (!fn)
305                 return -EINVAL;
306
307         mutex_lock(&clkdm_mutex);
308         list_for_each_entry(clkdm, &clkdm_list, node) {
309                 ret = (*fn)(clkdm);
310                 if (ret)
311                         break;
312         }
313         mutex_unlock(&clkdm_mutex);
314
315         return ret;
316 }
317
318
319 /**
320  * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
321  * @clkdm: struct clockdomain *
322  *
323  * Return a pointer to the struct powerdomain that the specified clockdomain
324  * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
325  */
326 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
327 {
328         if (!clkdm)
329                 return NULL;
330
331         return clkdm->pwrdm.ptr;
332 }
333
334
335 /* Hardware clockdomain control */
336
337 /**
338  * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
339  * @clk: struct clk * of a clockdomain
340  *
341  * Return the clockdomain's current state transition mode from the
342  * corresponding domain CM_CLKSTCTRL register.  Returns -EINVAL if clk
343  * is NULL or the current mode upon success.
344  */
345 static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
346 {
347         u32 v;
348
349         if (!clkdm)
350                 return -EINVAL;
351
352         v = cm_read_mod_reg(clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
353         v &= clkdm->clktrctrl_mask;
354         v >>= __ffs(clkdm->clktrctrl_mask);
355
356         return v;
357 }
358
359 /**
360  * omap2_clkdm_sleep - force clockdomain sleep transition
361  * @clkdm: struct clockdomain *
362  *
363  * Instruct the CM to force a sleep transition on the specified
364  * clockdomain 'clkdm'.  Returns -EINVAL if clk is NULL or if
365  * clockdomain does not support software-initiated sleep; 0 upon
366  * success.
367  */
368 int omap2_clkdm_sleep(struct clockdomain *clkdm)
369 {
370         if (!clkdm)
371                 return -EINVAL;
372
373         if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
374                 pr_debug("clockdomain: %s does not support forcing "
375                          "sleep via software\n", clkdm->name);
376                 return -EINVAL;
377         }
378
379         pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
380
381         if (cpu_is_omap24xx()) {
382
383                 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
384                                     clkdm->pwrdm.ptr->prcm_offs, PM_PWSTCTRL);
385
386         } else if (cpu_is_omap34xx()) {
387
388                 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
389                          __ffs(clkdm->clktrctrl_mask));
390
391                 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
392                                     clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
393
394         } else {
395                 BUG();
396         };
397
398         return 0;
399 }
400
401 /**
402  * omap2_clkdm_wakeup - force clockdomain wakeup transition
403  * @clkdm: struct clockdomain *
404  *
405  * Instruct the CM to force a wakeup transition on the specified
406  * clockdomain 'clkdm'.  Returns -EINVAL if clkdm is NULL or if the
407  * clockdomain does not support software-controlled wakeup; 0 upon
408  * success.
409  */
410 int omap2_clkdm_wakeup(struct clockdomain *clkdm)
411 {
412         if (!clkdm)
413                 return -EINVAL;
414
415         if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
416                 pr_debug("clockdomain: %s does not support forcing "
417                          "wakeup via software\n", clkdm->name);
418                 return -EINVAL;
419         }
420
421         pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
422
423         if (cpu_is_omap24xx()) {
424
425                 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
426                                       clkdm->pwrdm.ptr->prcm_offs, PM_PWSTCTRL);
427
428         } else if (cpu_is_omap34xx()) {
429
430                 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
431                          __ffs(clkdm->clktrctrl_mask));
432
433                 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
434                                     clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
435
436         } else {
437                 BUG();
438         };
439
440         return 0;
441 }
442
443 /**
444  * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
445  * @clkdm: struct clockdomain *
446  *
447  * Allow the hardware to automatically switch the clockdomain into
448  * active or idle states, as needed by downstream clocks.  If the
449  * clockdomain has any downstream clocks enabled in the clock
450  * framework, wkdep/sleepdep autodependencies are added; this is so
451  * device drivers can read and write to the device.  No return value.
452  */
453 void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
454 {
455         u32 v;
456
457         if (!clkdm)
458                 return;
459
460         if (!(clkdm->flags & CLKDM_CAN_ENABLE_AUTO)) {
461                 pr_debug("clock: automatic idle transitions cannot be enabled "
462                          "on clockdomain %s\n", clkdm->name);
463                 return;
464         }
465
466         pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
467                  clkdm->name);
468
469         if (atomic_read(&clkdm->usecount) > 0)
470                 _clkdm_add_autodeps(clkdm);
471
472         if (cpu_is_omap24xx())
473                 v = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
474         else if (cpu_is_omap34xx())
475                 v = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
476         else
477                 BUG();
478
479
480         cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
481                             v << __ffs(clkdm->clktrctrl_mask),
482                             clkdm->pwrdm.ptr->prcm_offs,
483                             CM_CLKSTCTRL);
484 }
485
486 /**
487  * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
488  * @clkdm: struct clockdomain *
489  *
490  * Prevent the hardware from automatically switching the clockdomain
491  * into inactive or idle states.  If the clockdomain has downstream
492  * clocks enabled in the clock framework, wkdep/sleepdep
493  * autodependencies are removed.  No return value.
494  */
495 void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
496 {
497         u32 v;
498
499         if (!clkdm)
500                 return;
501
502         if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
503                 pr_debug("clockdomain: automatic idle transitions cannot be "
504                          "disabled on %s\n", clkdm->name);
505                 return;
506         }
507
508         pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
509                  clkdm->name);
510
511         if (cpu_is_omap24xx())
512                 v = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
513         else if (cpu_is_omap34xx())
514                 v = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
515         else
516                 BUG();
517
518         cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
519                             v << __ffs(clkdm->clktrctrl_mask),
520                             clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
521
522         if (atomic_read(&clkdm->usecount) > 0)
523                 _clkdm_del_autodeps(clkdm);
524 }
525
526
527 /* Clockdomain-to-clock framework interface code */
528
529 /**
530  * omap2_clkdm_clk_enable - add an enabled downstream clock to this clkdm
531  * @clkdm: struct clockdomain *
532  * @clk: struct clk * of the enabled downstream clock
533  *
534  * Increment the usecount of this clockdomain 'clkdm' and ensure that
535  * it is awake.  Intended to be called by clk_enable() code.  If the
536  * clockdomain is in software-supervised idle mode, force the
537  * clockdomain to wake.  If the clockdomain is in hardware-supervised
538  * idle mode, add clkdm-pwrdm autodependencies, to ensure that devices
539  * in the clockdomain can be read from/written to by on-chip processors.
540  * Returns -EINVAL if passed null pointers; returns 0 upon success or
541  * if the clockdomain is in hwsup idle mode.
542  */
543 int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
544 {
545         int v;
546
547         /*
548          * XXX Rewrite this code to maintain a list of enabled
549          * downstream clocks for debugging purposes?
550          */
551
552         if (!clkdm || !clk)
553                 return -EINVAL;
554
555         if (atomic_inc_return(&clkdm->usecount) > 1)
556                 return 0;
557
558         /* Clockdomain now has one enabled downstream clock */
559
560         pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
561                  clk->name);
562
563         v = omap2_clkdm_clktrctrl_read(clkdm);
564
565         if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
566             (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
567                 _clkdm_add_autodeps(clkdm);
568         else
569                 omap2_clkdm_wakeup(clkdm);
570
571         return 0;
572 }
573
574 /**
575  * omap2_clkdm_clk_disable - remove an enabled downstream clock from this clkdm
576  * @clkdm: struct clockdomain *
577  * @clk: struct clk * of the disabled downstream clock
578  *
579  * Decrement the usecount of this clockdomain 'clkdm'. Intended to be
580  * called by clk_disable() code.  If the usecount goes to 0, put the
581  * clockdomain to sleep (software-supervised mode) or remove the
582  * clkdm-pwrdm autodependencies (hardware-supervised mode).  Returns
583  * -EINVAL if passed null pointers; -ERANGE if the clkdm usecount
584  * underflows and debugging is enabled; or returns 0 upon success or
585  * if the clockdomain is in hwsup idle mode.
586  */
587 int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
588 {
589         int v;
590
591         /*
592          * XXX Rewrite this code to maintain a list of enabled
593          * downstream clocks for debugging purposes?
594          */
595
596         if (!clkdm || !clk)
597                 return -EINVAL;
598
599 #ifdef DEBUG
600         if (atomic_read(&clkdm->usecount) == 0) {
601                 WARN_ON(1); /* underflow */
602                 return -ERANGE;
603         }
604 #endif
605
606         if (atomic_dec_return(&clkdm->usecount) > 0)
607                 return 0;
608
609         /* All downstream clocks of this clockdomain are now disabled */
610
611         pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
612                  clk->name);
613
614         v = omap2_clkdm_clktrctrl_read(clkdm);
615
616         if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
617             (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
618                 _clkdm_del_autodeps(clkdm);
619         else
620                 omap2_clkdm_sleep(clkdm);
621
622         return 0;
623 }
624