]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/clockdomain.c
Merge mainline v2.6.27-rc2 tree into linux-omap tree
[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
26 #include <linux/io.h>
27
28 #include <linux/bitops.h>
29
30 #include <asm/arch/clock.h>
31
32 #include "prm.h"
33 #include "prm-regbits-24xx.h"
34 #include "cm.h"
35
36 #include <asm/arch/powerdomain.h>
37 #include <asm/arch/clockdomain.h>
38
39 /* clkdm_list contains all registered struct clockdomains */
40 static LIST_HEAD(clkdm_list);
41
42 /* clkdm_mutex protects clkdm_list add and del ops */
43 static DEFINE_MUTEX(clkdm_mutex);
44
45 /* array of powerdomain deps to be added/removed when clkdm in hwsup mode */
46 static struct clkdm_pwrdm_autodep *autodeps;
47
48
49 /* Private functions */
50
51 /*
52  * _autodep_lookup - resolve autodep pwrdm names to pwrdm pointers; store
53  * @autodep: struct clkdm_pwrdm_autodep * to resolve
54  *
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.
63  */
64 static void _autodep_lookup(struct clkdm_pwrdm_autodep *autodep)
65 {
66         struct powerdomain *pwrdm;
67
68         if (!autodep)
69                 return;
70
71         if (!omap_chip_is(autodep->omap_chip))
72                 return;
73
74         pwrdm = pwrdm_lookup(autodep->pwrdm.name);
75         if (!pwrdm) {
76                 pr_debug("clockdomain: _autodep_lookup: powerdomain %s "
77                          "does not exist\n", autodep->pwrdm.name);
78                 WARN_ON(1);
79                 return;
80         }
81         autodep->pwrdm.ptr = pwrdm;
82
83         return;
84 }
85
86 /*
87  * _clkdm_add_autodeps - add auto sleepdeps/wkdeps to clkdm upon clock enable
88  * @clkdm: struct clockdomain *
89  *
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.
93  */
94 static void _clkdm_add_autodeps(struct clockdomain *clkdm)
95 {
96         struct clkdm_pwrdm_autodep *autodep;
97
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);
102
103                 pwrdm_add_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
104                 pwrdm_add_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
105         }
106 }
107
108 /*
109  * _clkdm_add_autodeps - remove auto sleepdeps/wkdeps from clkdm
110  * @clkdm: struct clockdomain *
111  *
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.
115  */
116 static void _clkdm_del_autodeps(struct clockdomain *clkdm)
117 {
118         struct clkdm_pwrdm_autodep *autodep;
119
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);
124
125                 pwrdm_del_sleepdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
126                 pwrdm_del_wkdep(clkdm->pwrdm.ptr, autodep->pwrdm.ptr);
127         }
128 }
129
130
131 static struct clockdomain *_clkdm_lookup(const char *name)
132 {
133         struct clockdomain *clkdm, *temp_clkdm;
134
135         if (!name)
136                 return NULL;
137
138         clkdm = NULL;
139
140         list_for_each_entry(temp_clkdm, &clkdm_list, node) {
141                 if (!strcmp(name, temp_clkdm->name)) {
142                         clkdm = temp_clkdm;
143                         break;
144                 }
145         }
146
147         return clkdm;
148 }
149
150
151 /* Public functions */
152
153 /**
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
157  *
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.
163  */
164 void clkdm_init(struct clockdomain **clkdms,
165                 struct clkdm_pwrdm_autodep *init_autodeps)
166 {
167         struct clockdomain **c = NULL;
168         struct clkdm_pwrdm_autodep *autodep = NULL;
169
170         if (clkdms)
171                 for (c = clkdms; *c; c++)
172                         clkdm_register(*c);
173
174         autodeps = init_autodeps;
175         if (autodeps)
176                 for (autodep = autodeps; autodep->pwrdm.ptr; autodep++)
177                         _autodep_lookup(autodep);
178 }
179
180 /**
181  * clkdm_register - register a clockdomain
182  * @clkdm: struct clockdomain * to register
183  *
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.
187  */
188 int clkdm_register(struct clockdomain *clkdm)
189 {
190         int ret = -EINVAL;
191         struct powerdomain *pwrdm;
192
193         if (!clkdm || !clkdm->name)
194                 return -EINVAL;
195
196         if (!omap_chip_is(clkdm->omap_chip))
197                 return -EINVAL;
198
199         pwrdm = pwrdm_lookup(clkdm->pwrdm.name);
200         if (!pwrdm) {
201                 pr_debug("clockdomain: clkdm_register %s: powerdomain %s "
202                          "does not exist\n", clkdm->name, clkdm->pwrdm.name);
203                 return -EINVAL;
204         }
205         clkdm->pwrdm.ptr = pwrdm;
206
207         mutex_lock(&clkdm_mutex);
208         /* Verify that the clockdomain is not already registered */
209         if (_clkdm_lookup(clkdm->name)) {
210                 ret = -EEXIST;
211                 goto cr_unlock;
212         };
213
214         list_add(&clkdm->node, &clkdm_list);
215
216         pwrdm_add_clkdm(pwrdm, clkdm);
217
218         pr_debug("clockdomain: registered %s\n", clkdm->name);
219         ret = 0;
220
221 cr_unlock:
222         mutex_unlock(&clkdm_mutex);
223
224         return ret;
225 }
226
227 /**
228  * clkdm_unregister - unregister a clockdomain
229  * @clkdm: struct clockdomain * to unregister
230  *
231  * Removes a clockdomain from the internal clockdomain list.  Returns
232  * -EINVAL if clkdm argument is NULL.
233  */
234 int clkdm_unregister(struct clockdomain *clkdm)
235 {
236         if (!clkdm)
237                 return -EINVAL;
238
239         pwrdm_del_clkdm(clkdm->pwrdm.ptr, clkdm);
240
241         mutex_lock(&clkdm_mutex);
242         list_del(&clkdm->node);
243         mutex_unlock(&clkdm_mutex);
244
245         pr_debug("clockdomain: unregistered %s\n", clkdm->name);
246
247         return 0;
248 }
249
250 /**
251  * clkdm_lookup - look up a clockdomain by name, return a pointer
252  * @name: name of clockdomain
253  *
254  * Find a registered clockdomain by its name.  Returns a pointer to the
255  * struct clockdomain if found, or NULL otherwise.
256  */
257 struct clockdomain *clkdm_lookup(const char *name)
258 {
259         struct clockdomain *clkdm, *temp_clkdm;
260
261         if (!name)
262                 return NULL;
263
264         clkdm = NULL;
265
266         mutex_lock(&clkdm_mutex);
267         list_for_each_entry(temp_clkdm, &clkdm_list, node) {
268                 if (!strcmp(name, temp_clkdm->name)) {
269                         clkdm = temp_clkdm;
270                         break;
271                 }
272         }
273         mutex_unlock(&clkdm_mutex);
274
275         return clkdm;
276 }
277
278 /**
279  * clkdm_for_each - call function on each registered clockdomain
280  * @fn: callback function *
281  *
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
290  * is null.
291  */
292 int clkdm_for_each(int (*fn)(struct clockdomain *clkdm))
293 {
294         struct clockdomain *clkdm;
295         int ret = 0;
296
297         if (!fn)
298                 return -EINVAL;
299
300         mutex_lock(&clkdm_mutex);
301         list_for_each_entry(clkdm, &clkdm_list, node) {
302                 ret = (*fn)(clkdm);
303                 if (ret)
304                         break;
305         }
306         mutex_unlock(&clkdm_mutex);
307
308         return ret;
309 }
310
311
312 /**
313  * clkdm_get_pwrdm - return a ptr to the pwrdm that this clkdm resides in
314  * @clkdm: struct clockdomain *
315  *
316  * Return a pointer to the struct powerdomain that the specified clockdomain
317  * 'clkdm' exists in, or returns NULL if clkdm argument is NULL.
318  */
319 struct powerdomain *clkdm_get_pwrdm(struct clockdomain *clkdm)
320 {
321         if (!clkdm)
322                 return NULL;
323
324         return clkdm->pwrdm.ptr;
325 }
326
327
328 /* Hardware clockdomain control */
329
330 /**
331  * omap2_clkdm_clktrctrl_read - read the clkdm's current state transition mode
332  * @clk: struct clk * of a clockdomain
333  *
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.
337  */
338 static int omap2_clkdm_clktrctrl_read(struct clockdomain *clkdm)
339 {
340         u32 v;
341
342         if (!clkdm)
343                 return -EINVAL;
344
345         v = cm_read_mod_reg(clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
346         v &= clkdm->clktrctrl_mask;
347         v >>= __ffs(clkdm->clktrctrl_mask);
348
349         return v;
350 }
351
352 /**
353  * omap2_clkdm_sleep - force clockdomain sleep transition
354  * @clkdm: struct clockdomain *
355  *
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
359  * success.
360  */
361 int omap2_clkdm_sleep(struct clockdomain *clkdm)
362 {
363         if (!clkdm)
364                 return -EINVAL;
365
366         if (!(clkdm->flags & CLKDM_CAN_FORCE_SLEEP)) {
367                 pr_debug("clockdomain: %s does not support forcing "
368                          "sleep via software\n", clkdm->name);
369                 return -EINVAL;
370         }
371
372         pr_debug("clockdomain: forcing sleep on %s\n", clkdm->name);
373
374         if (cpu_is_omap24xx()) {
375
376                 cm_set_mod_reg_bits(OMAP24XX_FORCESTATE,
377                                     clkdm->pwrdm.ptr->prcm_offs, PM_PWSTCTRL);
378
379         } else if (cpu_is_omap34xx()) {
380
381                 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_SLEEP <<
382                          __ffs(clkdm->clktrctrl_mask));
383
384                 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
385                                     clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
386
387         } else {
388                 BUG();
389         };
390
391         return 0;
392 }
393
394 /**
395  * omap2_clkdm_wakeup - force clockdomain wakeup transition
396  * @clkdm: struct clockdomain *
397  *
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
401  * success.
402  */
403 int omap2_clkdm_wakeup(struct clockdomain *clkdm)
404 {
405         if (!clkdm)
406                 return -EINVAL;
407
408         if (!(clkdm->flags & CLKDM_CAN_FORCE_WAKEUP)) {
409                 pr_debug("clockdomain: %s does not support forcing "
410                          "wakeup via software\n", clkdm->name);
411                 return -EINVAL;
412         }
413
414         pr_debug("clockdomain: forcing wakeup on %s\n", clkdm->name);
415
416         if (cpu_is_omap24xx()) {
417
418                 cm_clear_mod_reg_bits(OMAP24XX_FORCESTATE,
419                                       clkdm->pwrdm.ptr->prcm_offs, PM_PWSTCTRL);
420
421         } else if (cpu_is_omap34xx()) {
422
423                 u32 v = (OMAP34XX_CLKSTCTRL_FORCE_WAKEUP <<
424                          __ffs(clkdm->clktrctrl_mask));
425
426                 cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask, v,
427                                     clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
428
429         } else {
430                 BUG();
431         };
432
433         return 0;
434 }
435
436 /**
437  * omap2_clkdm_allow_idle - enable hwsup idle transitions for clkdm
438  * @clkdm: struct clockdomain *
439  *
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.
445  */
446 void omap2_clkdm_allow_idle(struct clockdomain *clkdm)
447 {
448         u32 v;
449
450         if (!clkdm)
451                 return;
452
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);
456                 return;
457         }
458
459         pr_debug("clockdomain: enabling automatic idle transitions for %s\n",
460                  clkdm->name);
461
462         if (atomic_read(&clkdm->usecount) > 0)
463                 _clkdm_add_autodeps(clkdm);
464
465         if (cpu_is_omap24xx())
466                 v = OMAP24XX_CLKSTCTRL_ENABLE_AUTO;
467         else if (cpu_is_omap34xx())
468                 v = OMAP34XX_CLKSTCTRL_ENABLE_AUTO;
469         else
470                 BUG();
471
472
473         cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
474                             v << __ffs(clkdm->clktrctrl_mask),
475                             clkdm->pwrdm.ptr->prcm_offs,
476                             CM_CLKSTCTRL);
477 }
478
479 /**
480  * omap2_clkdm_deny_idle - disable hwsup idle transitions for clkdm
481  * @clkdm: struct clockdomain *
482  *
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.
487  */
488 void omap2_clkdm_deny_idle(struct clockdomain *clkdm)
489 {
490         u32 v;
491
492         if (!clkdm)
493                 return;
494
495         if (!(clkdm->flags & CLKDM_CAN_DISABLE_AUTO)) {
496                 pr_debug("clockdomain: automatic idle transitions cannot be "
497                          "disabled on %s\n", clkdm->name);
498                 return;
499         }
500
501         pr_debug("clockdomain: disabling automatic idle transitions for %s\n",
502                  clkdm->name);
503
504         if (cpu_is_omap24xx())
505                 v = OMAP24XX_CLKSTCTRL_DISABLE_AUTO;
506         else if (cpu_is_omap34xx())
507                 v = OMAP34XX_CLKSTCTRL_DISABLE_AUTO;
508         else
509                 BUG();
510
511         cm_rmw_mod_reg_bits(clkdm->clktrctrl_mask,
512                             v << __ffs(clkdm->clktrctrl_mask),
513                             clkdm->pwrdm.ptr->prcm_offs, CM_CLKSTCTRL);
514
515         if (atomic_read(&clkdm->usecount) > 0)
516                 _clkdm_del_autodeps(clkdm);
517 }
518
519
520 /* Clockdomain-to-clock framework interface code */
521
522 /**
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
526  *
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.
535  */
536 int omap2_clkdm_clk_enable(struct clockdomain *clkdm, struct clk *clk)
537 {
538         int v;
539
540         /*
541          * XXX Rewrite this code to maintain a list of enabled
542          * downstream clocks for debugging purposes?
543          */
544
545         if (!clkdm || !clk)
546                 return -EINVAL;
547
548         if (atomic_inc_return(&clkdm->usecount) > 1)
549                 return 0;
550
551         /* Clockdomain now has one enabled downstream clock */
552
553         pr_debug("clockdomain: clkdm %s: clk %s now enabled\n", clkdm->name,
554                  clk->name);
555
556         v = omap2_clkdm_clktrctrl_read(clkdm);
557
558         if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
559             (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
560                 _clkdm_add_autodeps(clkdm);
561         else
562                 omap2_clkdm_wakeup(clkdm);
563
564         return 0;
565 }
566
567 /**
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
571  *
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.
579  */
580 int omap2_clkdm_clk_disable(struct clockdomain *clkdm, struct clk *clk)
581 {
582         int v;
583
584         /*
585          * XXX Rewrite this code to maintain a list of enabled
586          * downstream clocks for debugging purposes?
587          */
588
589         if (!clkdm || !clk)
590                 return -EINVAL;
591
592 #ifdef DEBUG
593         if (atomic_read(&clkdm->usecount) == 0) {
594                 WARN_ON(1); /* underflow */
595                 return -ERANGE;
596         }
597 #endif
598
599         if (atomic_dec_return(&clkdm->usecount) > 0)
600                 return 0;
601
602         /* All downstream clocks of this clockdomain are now disabled */
603
604         pr_debug("clockdomain: clkdm %s: clk %s now disabled\n", clkdm->name,
605                  clk->name);
606
607         v = omap2_clkdm_clktrctrl_read(clkdm);
608
609         if ((cpu_is_omap34xx() && v == OMAP34XX_CLKSTCTRL_ENABLE_AUTO) ||
610             (cpu_is_omap24xx() && v == OMAP24XX_CLKSTCTRL_ENABLE_AUTO))
611                 _clkdm_del_autodeps(clkdm);
612         else
613                 omap2_clkdm_sleep(clkdm);
614
615         return 0;
616 }
617