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