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