Merge branch 'davinci-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / arch / arm / mach-omap2 / omap_hwmod.c
1 /*
2  * omap_hwmod implementation for OMAP2/3/4
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  * Paul Walmsley
6  * With fixes and testing from Kevin Hilman
7  *
8  * Created in collaboration with (alphabetical order): Benoit Cousson,
9  * Kevin Hilman, Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari
10  * Poussa, Anand Sawant, Santosh Shilimkar, Richard Woodruff
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  * This code manages "OMAP modules" (on-chip devices) and their
17  * integration with Linux device driver and bus code.
18  *
19  * References:
20  * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
21  * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
22  * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
23  * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
24  * - Open Core Protocol Specification 2.2
25  *
26  * To do:
27  * - pin mux handling
28  * - handle IO mapping
29  * - bus throughput & module latency measurement code
30  *
31  * XXX add tests at the beginning of each function to ensure the hwmod is
32  * in the appropriate state
33  * XXX error return values should be checked to ensure that they are
34  * appropriate
35  */
36 #undef DEBUG
37
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/io.h>
41 #include <linux/clk.h>
42 #include <linux/delay.h>
43 #include <linux/err.h>
44 #include <linux/list.h>
45 #include <linux/mutex.h>
46 #include <linux/bootmem.h>
47
48 #include <plat/common.h>
49 #include <plat/cpu.h>
50 #include <plat/clockdomain.h>
51 #include <plat/powerdomain.h>
52 #include <plat/clock.h>
53 #include <plat/omap_hwmod.h>
54
55 #include "cm.h"
56
57 /* Maximum microseconds to wait for OMAP module to reset */
58 #define MAX_MODULE_RESET_WAIT           10000
59
60 /* Name of the OMAP hwmod for the MPU */
61 #define MPU_INITIATOR_NAME              "mpu_hwmod"
62
63 /* omap_hwmod_list contains all registered struct omap_hwmods */
64 static LIST_HEAD(omap_hwmod_list);
65
66 static DEFINE_MUTEX(omap_hwmod_mutex);
67
68 /* mpu_oh: used to add/remove MPU initiator from sleepdep list */
69 static struct omap_hwmod *mpu_oh;
70
71 /* inited: 0 if omap_hwmod_init() has not yet been called; 1 otherwise */
72 static u8 inited;
73
74
75 /* Private functions */
76
77 /**
78  * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
79  * @oh: struct omap_hwmod *
80  *
81  * Load the current value of the hwmod OCP_SYSCONFIG register into the
82  * struct omap_hwmod for later use.  Returns -EINVAL if the hwmod has no
83  * OCP_SYSCONFIG register or 0 upon success.
84  */
85 static int _update_sysc_cache(struct omap_hwmod *oh)
86 {
87         if (!oh->sysconfig) {
88                 WARN(!oh->sysconfig, "omap_hwmod: %s: cannot read "
89                      "OCP_SYSCONFIG: not defined on hwmod\n", oh->name);
90                 return -EINVAL;
91         }
92
93         /* XXX ensure module interface clock is up */
94
95         oh->_sysc_cache = omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
96
97         if (!(oh->sysconfig->sysc_flags & SYSC_NO_CACHE))
98                 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
99
100         return 0;
101 }
102
103 /**
104  * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
105  * @v: OCP_SYSCONFIG value to write
106  * @oh: struct omap_hwmod *
107  *
108  * Write @v into the module OCP_SYSCONFIG register, if it has one.  No
109  * return value.
110  */
111 static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
112 {
113         if (!oh->sysconfig) {
114                 WARN(!oh->sysconfig, "omap_hwmod: %s: cannot write "
115                      "OCP_SYSCONFIG: not defined on hwmod\n", oh->name);
116                 return;
117         }
118
119         /* XXX ensure module interface clock is up */
120
121         if (oh->_sysc_cache != v) {
122                 oh->_sysc_cache = v;
123                 omap_hwmod_writel(v, oh, oh->sysconfig->sysc_offs);
124         }
125 }
126
127 /**
128  * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
129  * @oh: struct omap_hwmod *
130  * @standbymode: MIDLEMODE field bits
131  * @v: pointer to register contents to modify
132  *
133  * Update the master standby mode bits in @v to be @standbymode for
134  * the @oh hwmod.  Does not write to the hardware.  Returns -EINVAL
135  * upon error or 0 upon success.
136  */
137 static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
138                                    u32 *v)
139 {
140         if (!oh->sysconfig ||
141             !(oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE))
142                 return -EINVAL;
143
144         *v &= ~SYSC_MIDLEMODE_MASK;
145         *v |= __ffs(standbymode) << SYSC_MIDLEMODE_SHIFT;
146
147         return 0;
148 }
149
150 /**
151  * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
152  * @oh: struct omap_hwmod *
153  * @idlemode: SIDLEMODE field bits
154  * @v: pointer to register contents to modify
155  *
156  * Update the slave idle mode bits in @v to be @idlemode for the @oh
157  * hwmod.  Does not write to the hardware.  Returns -EINVAL upon error
158  * or 0 upon success.
159  */
160 static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
161 {
162         if (!oh->sysconfig ||
163             !(oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE))
164                 return -EINVAL;
165
166         *v &= ~SYSC_SIDLEMODE_MASK;
167         *v |= __ffs(idlemode) << SYSC_SIDLEMODE_SHIFT;
168
169         return 0;
170 }
171
172 /**
173  * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
174  * @oh: struct omap_hwmod *
175  * @clockact: CLOCKACTIVITY field bits
176  * @v: pointer to register contents to modify
177  *
178  * Update the clockactivity mode bits in @v to be @clockact for the
179  * @oh hwmod.  Used for additional powersaving on some modules.  Does
180  * not write to the hardware.  Returns -EINVAL upon error or 0 upon
181  * success.
182  */
183 static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
184 {
185         if (!oh->sysconfig ||
186             !(oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
187                 return -EINVAL;
188
189         *v &= ~SYSC_CLOCKACTIVITY_MASK;
190         *v |= clockact << SYSC_CLOCKACTIVITY_SHIFT;
191
192         return 0;
193 }
194
195 /**
196  * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
197  * @oh: struct omap_hwmod *
198  * @v: pointer to register contents to modify
199  *
200  * Set the SOFTRESET bit in @v for hwmod @oh.  Returns -EINVAL upon
201  * error or 0 upon success.
202  */
203 static int _set_softreset(struct omap_hwmod *oh, u32 *v)
204 {
205         if (!oh->sysconfig ||
206             !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET))
207                 return -EINVAL;
208
209         *v |= SYSC_SOFTRESET_MASK;
210
211         return 0;
212 }
213
214 /**
215  * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
216  * @oh: struct omap_hwmod *
217  * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
218  * @v: pointer to register contents to modify
219  *
220  * Update the module autoidle bit in @v to be @autoidle for the @oh
221  * hwmod.  The autoidle bit controls whether the module can gate
222  * internal clocks automatically when it isn't doing anything; the
223  * exact function of this bit varies on a per-module basis.  This
224  * function does not write to the hardware.  Returns -EINVAL upon
225  * error or 0 upon success.
226  */
227 static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
228                                 u32 *v)
229 {
230         if (!oh->sysconfig ||
231             !(oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE))
232                 return -EINVAL;
233
234         *v &= ~SYSC_AUTOIDLE_MASK;
235         *v |= autoidle << SYSC_AUTOIDLE_SHIFT;
236
237         return 0;
238 }
239
240 /**
241  * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
242  * @oh: struct omap_hwmod *
243  *
244  * Allow the hardware module @oh to send wakeups.  Returns -EINVAL
245  * upon error or 0 upon success.
246  */
247 static int _enable_wakeup(struct omap_hwmod *oh)
248 {
249         u32 v;
250
251         if (!oh->sysconfig ||
252             !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
253                 return -EINVAL;
254
255         v = oh->_sysc_cache;
256         v |= SYSC_ENAWAKEUP_MASK;
257         _write_sysconfig(v, oh);
258
259         /* XXX test pwrdm_get_wken for this hwmod's subsystem */
260
261         oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
262
263         return 0;
264 }
265
266 /**
267  * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
268  * @oh: struct omap_hwmod *
269  *
270  * Prevent the hardware module @oh to send wakeups.  Returns -EINVAL
271  * upon error or 0 upon success.
272  */
273 static int _disable_wakeup(struct omap_hwmod *oh)
274 {
275         u32 v;
276
277         if (!oh->sysconfig ||
278             !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
279                 return -EINVAL;
280
281         v = oh->_sysc_cache;
282         v &= ~SYSC_ENAWAKEUP_MASK;
283         _write_sysconfig(v, oh);
284
285         /* XXX test pwrdm_get_wken for this hwmod's subsystem */
286
287         oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
288
289         return 0;
290 }
291
292 /**
293  * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
294  * @oh: struct omap_hwmod *
295  *
296  * Prevent the hardware module @oh from entering idle while the
297  * hardare module initiator @init_oh is active.  Useful when a module
298  * will be accessed by a particular initiator (e.g., if a module will
299  * be accessed by the IVA, there should be a sleepdep between the IVA
300  * initiator and the module).  Only applies to modules in smart-idle
301  * mode.  Returns -EINVAL upon error or passes along
302  * pwrdm_add_sleepdep() value upon success.
303  */
304 static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
305 {
306         if (!oh->_clk)
307                 return -EINVAL;
308
309         return pwrdm_add_sleepdep(oh->_clk->clkdm->pwrdm.ptr,
310                                   init_oh->_clk->clkdm->pwrdm.ptr);
311 }
312
313 /**
314  * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
315  * @oh: struct omap_hwmod *
316  *
317  * Allow the hardware module @oh to enter idle while the hardare
318  * module initiator @init_oh is active.  Useful when a module will not
319  * be accessed by a particular initiator (e.g., if a module will not
320  * be accessed by the IVA, there should be no sleepdep between the IVA
321  * initiator and the module).  Only applies to modules in smart-idle
322  * mode.  Returns -EINVAL upon error or passes along
323  * pwrdm_add_sleepdep() value upon success.
324  */
325 static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
326 {
327         if (!oh->_clk)
328                 return -EINVAL;
329
330         return pwrdm_del_sleepdep(oh->_clk->clkdm->pwrdm.ptr,
331                                   init_oh->_clk->clkdm->pwrdm.ptr);
332 }
333
334 /**
335  * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
336  * @oh: struct omap_hwmod *
337  *
338  * Called from _init_clocks().  Populates the @oh _clk (main
339  * functional clock pointer) if a main_clk is present.  Returns 0 on
340  * success or -EINVAL on error.
341  */
342 static int _init_main_clk(struct omap_hwmod *oh)
343 {
344         struct clk *c;
345         int ret = 0;
346
347         if (!oh->clkdev_con_id)
348                 return 0;
349
350         c = clk_get_sys(oh->clkdev_dev_id, oh->clkdev_con_id);
351         WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get main_clk %s.%s\n",
352              oh->name, oh->clkdev_dev_id, oh->clkdev_con_id);
353         if (IS_ERR(c))
354                 ret = -EINVAL;
355         oh->_clk = c;
356
357         WARN(!c->clkdm, "omap_hwmod: %s: missing clockdomain for %s.\n",
358              oh->clkdev_con_id, c->name);
359
360         return ret;
361 }
362
363 /**
364  * _init_interface_clk - get a struct clk * for the the hwmod's interface clks
365  * @oh: struct omap_hwmod *
366  *
367  * Called from _init_clocks().  Populates the @oh OCP slave interface
368  * clock pointers.  Returns 0 on success or -EINVAL on error.
369  */
370 static int _init_interface_clks(struct omap_hwmod *oh)
371 {
372         struct omap_hwmod_ocp_if *os;
373         struct clk *c;
374         int i;
375         int ret = 0;
376
377         if (oh->slaves_cnt == 0)
378                 return 0;
379
380         for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
381                 if (!os->clkdev_con_id)
382                         continue;
383
384                 c = clk_get_sys(os->clkdev_dev_id, os->clkdev_con_id);
385                 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get "
386                      "interface_clk %s.%s\n", oh->name,
387                      os->clkdev_dev_id, os->clkdev_con_id);
388                 if (IS_ERR(c))
389                         ret = -EINVAL;
390                 os->_clk = c;
391         }
392
393         return ret;
394 }
395
396 /**
397  * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
398  * @oh: struct omap_hwmod *
399  *
400  * Called from _init_clocks().  Populates the @oh omap_hwmod_opt_clk
401  * clock pointers.  Returns 0 on success or -EINVAL on error.
402  */
403 static int _init_opt_clks(struct omap_hwmod *oh)
404 {
405         struct omap_hwmod_opt_clk *oc;
406         struct clk *c;
407         int i;
408         int ret = 0;
409
410         for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
411                 c = clk_get_sys(oc->clkdev_dev_id, oc->clkdev_con_id);
412                 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get opt_clk "
413                      "%s.%s\n", oh->name, oc->clkdev_dev_id,
414                      oc->clkdev_con_id);
415                 if (IS_ERR(c))
416                         ret = -EINVAL;
417                 oc->_clk = c;
418         }
419
420         return ret;
421 }
422
423 /**
424  * _enable_clocks - enable hwmod main clock and interface clocks
425  * @oh: struct omap_hwmod *
426  *
427  * Enables all clocks necessary for register reads and writes to succeed
428  * on the hwmod @oh.  Returns 0.
429  */
430 static int _enable_clocks(struct omap_hwmod *oh)
431 {
432         struct omap_hwmod_ocp_if *os;
433         int i;
434
435         pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
436
437         if (oh->_clk && !IS_ERR(oh->_clk))
438                 clk_enable(oh->_clk);
439
440         if (oh->slaves_cnt > 0) {
441                 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
442                         struct clk *c = os->_clk;
443
444                         if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
445                                 clk_enable(c);
446                 }
447         }
448
449         /* The opt clocks are controlled by the device driver. */
450
451         return 0;
452 }
453
454 /**
455  * _disable_clocks - disable hwmod main clock and interface clocks
456  * @oh: struct omap_hwmod *
457  *
458  * Disables the hwmod @oh main functional and interface clocks.  Returns 0.
459  */
460 static int _disable_clocks(struct omap_hwmod *oh)
461 {
462         struct omap_hwmod_ocp_if *os;
463         int i;
464
465         pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
466
467         if (oh->_clk && !IS_ERR(oh->_clk))
468                 clk_disable(oh->_clk);
469
470         if (oh->slaves_cnt > 0) {
471                 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
472                         struct clk *c = os->_clk;
473
474                         if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
475                                 clk_disable(c);
476                 }
477         }
478
479         /* The opt clocks are controlled by the device driver. */
480
481         return 0;
482 }
483
484 /**
485  * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
486  * @oh: struct omap_hwmod *
487  *
488  * Returns the array index of the OCP slave port that the MPU
489  * addresses the device on, or -EINVAL upon error or not found.
490  */
491 static int _find_mpu_port_index(struct omap_hwmod *oh)
492 {
493         struct omap_hwmod_ocp_if *os;
494         int i;
495         int found = 0;
496
497         if (!oh || oh->slaves_cnt == 0)
498                 return -EINVAL;
499
500         for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
501                 if (os->user & OCP_USER_MPU) {
502                         found = 1;
503                         break;
504                 }
505         }
506
507         if (found)
508                 pr_debug("omap_hwmod: %s: MPU OCP slave port ID  %d\n",
509                          oh->name, i);
510         else
511                 pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
512                          oh->name);
513
514         return (found) ? i : -EINVAL;
515 }
516
517 /**
518  * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
519  * @oh: struct omap_hwmod *
520  *
521  * Return the virtual address of the base of the register target of
522  * device @oh, or NULL on error.
523  */
524 static void __iomem *_find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
525 {
526         struct omap_hwmod_ocp_if *os;
527         struct omap_hwmod_addr_space *mem;
528         int i;
529         int found = 0;
530         void __iomem *va_start;
531
532         if (!oh || oh->slaves_cnt == 0)
533                 return NULL;
534
535         os = *oh->slaves + index;
536
537         for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
538                 if (mem->flags & ADDR_TYPE_RT) {
539                         found = 1;
540                         break;
541                 }
542         }
543
544         if (found) {
545                 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
546                 if (!va_start) {
547                         pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
548                         return NULL;
549                 }
550                 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
551                          oh->name, va_start);
552         } else {
553                 pr_debug("omap_hwmod: %s: no MPU register target found\n",
554                          oh->name);
555         }
556
557         return (found) ? va_start : NULL;
558 }
559
560 /**
561  * _sysc_enable - try to bring a module out of idle via OCP_SYSCONFIG
562  * @oh: struct omap_hwmod *
563  *
564  * If module is marked as SWSUP_SIDLE, force the module out of slave
565  * idle; otherwise, configure it for smart-idle.  If module is marked
566  * as SWSUP_MSUSPEND, force the module out of master standby;
567  * otherwise, configure it for smart-standby.  No return value.
568  */
569 static void _sysc_enable(struct omap_hwmod *oh)
570 {
571         u8 idlemode;
572         u32 v;
573
574         if (!oh->sysconfig)
575                 return;
576
577         v = oh->_sysc_cache;
578
579         if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
580                 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
581                         HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
582                 _set_slave_idlemode(oh, idlemode, &v);
583         }
584
585         if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
586                 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
587                         HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
588                 _set_master_standbymode(oh, idlemode, &v);
589         }
590
591         if (oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE) {
592                 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
593                         0 : 1;
594                 _set_module_autoidle(oh, idlemode, &v);
595         }
596
597         /* XXX OCP ENAWAKEUP bit? */
598
599         /*
600          * XXX The clock framework should handle this, by
601          * calling into this code.  But this must wait until the
602          * clock structures are tagged with omap_hwmod entries
603          */
604         if (oh->flags & HWMOD_SET_DEFAULT_CLOCKACT &&
605             oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY)
606                 _set_clockactivity(oh, oh->sysconfig->clockact, &v);
607
608         _write_sysconfig(v, oh);
609 }
610
611 /**
612  * _sysc_idle - try to put a module into idle via OCP_SYSCONFIG
613  * @oh: struct omap_hwmod *
614  *
615  * If module is marked as SWSUP_SIDLE, force the module into slave
616  * idle; otherwise, configure it for smart-idle.  If module is marked
617  * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
618  * configure it for smart-standby.  No return value.
619  */
620 static void _sysc_idle(struct omap_hwmod *oh)
621 {
622         u8 idlemode;
623         u32 v;
624
625         if (!oh->sysconfig)
626                 return;
627
628         v = oh->_sysc_cache;
629
630         if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
631                 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
632                         HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
633                 _set_slave_idlemode(oh, idlemode, &v);
634         }
635
636         if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
637                 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
638                         HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
639                 _set_master_standbymode(oh, idlemode, &v);
640         }
641
642         _write_sysconfig(v, oh);
643 }
644
645 /**
646  * _sysc_shutdown - force a module into idle via OCP_SYSCONFIG
647  * @oh: struct omap_hwmod *
648  *
649  * Force the module into slave idle and master suspend. No return
650  * value.
651  */
652 static void _sysc_shutdown(struct omap_hwmod *oh)
653 {
654         u32 v;
655
656         if (!oh->sysconfig)
657                 return;
658
659         v = oh->_sysc_cache;
660
661         if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE)
662                 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
663
664         if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE)
665                 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
666
667         if (oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE)
668                 _set_module_autoidle(oh, 1, &v);
669
670         _write_sysconfig(v, oh);
671 }
672
673 /**
674  * _lookup - find an omap_hwmod by name
675  * @name: find an omap_hwmod by name
676  *
677  * Return a pointer to an omap_hwmod by name, or NULL if not found.
678  * Caller must hold omap_hwmod_mutex.
679  */
680 static struct omap_hwmod *_lookup(const char *name)
681 {
682         struct omap_hwmod *oh, *temp_oh;
683
684         oh = NULL;
685
686         list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
687                 if (!strcmp(name, temp_oh->name)) {
688                         oh = temp_oh;
689                         break;
690                 }
691         }
692
693         return oh;
694 }
695
696 /**
697  * _init_clocks - clk_get() all clocks associated with this hwmod
698  * @oh: struct omap_hwmod *
699  *
700  * Called by omap_hwmod_late_init() (after omap2_clk_init()).
701  * Resolves all clock names embedded in the hwmod.  Must be called
702  * with omap_hwmod_mutex held.  Returns -EINVAL if the omap_hwmod
703  * has not yet been registered or if the clocks have already been
704  * initialized, 0 on success, or a non-zero error on failure.
705  */
706 static int _init_clocks(struct omap_hwmod *oh)
707 {
708         int ret = 0;
709
710         if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
711                 return -EINVAL;
712
713         pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
714
715         ret |= _init_main_clk(oh);
716         ret |= _init_interface_clks(oh);
717         ret |= _init_opt_clks(oh);
718
719         oh->_state = _HWMOD_STATE_CLKS_INITED;
720
721         return ret;
722 }
723
724 /**
725  * _wait_target_ready - wait for a module to leave slave idle
726  * @oh: struct omap_hwmod *
727  *
728  * Wait for a module @oh to leave slave idle.  Returns 0 if the module
729  * does not have an IDLEST bit or if the module successfully leaves
730  * slave idle; otherwise, pass along the return value of the
731  * appropriate *_cm_wait_module_ready() function.
732  */
733 static int _wait_target_ready(struct omap_hwmod *oh)
734 {
735         struct omap_hwmod_ocp_if *os;
736         int ret;
737
738         if (!oh)
739                 return -EINVAL;
740
741         if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
742                 return 0;
743
744         os = *oh->slaves + oh->_mpu_port_index;
745
746         if (!(os->flags & OCPIF_HAS_IDLEST))
747                 return 0;
748
749         /* XXX check module SIDLEMODE */
750
751         /* XXX check clock enable states */
752
753         if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
754                 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
755                                                  oh->prcm.omap2.idlest_reg_id,
756                                                  oh->prcm.omap2.idlest_idle_bit);
757 #if 0
758         } else if (cpu_is_omap44xx()) {
759                 ret = omap4_cm_wait_module_ready(oh->prcm.omap4.module_offs,
760                                                  oh->prcm.omap4.device_offs);
761 #endif
762         } else {
763                 BUG();
764         };
765
766         return ret;
767 }
768
769 /**
770  * _reset - reset an omap_hwmod
771  * @oh: struct omap_hwmod *
772  *
773  * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit.  hwmod must be
774  * enabled for this to work.  Must be called with omap_hwmod_mutex
775  * held.  Returns -EINVAL if the hwmod cannot be reset this way or if
776  * the hwmod is in the wrong state, -ETIMEDOUT if the module did not
777  * reset in time, or 0 upon success.
778  */
779 static int _reset(struct omap_hwmod *oh)
780 {
781         u32 r, v;
782         int c = 0;
783
784         if (!oh->sysconfig ||
785             !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET) ||
786             (oh->sysconfig->sysc_flags & SYSS_MISSING))
787                 return -EINVAL;
788
789         /* clocks must be on for this operation */
790         if (oh->_state != _HWMOD_STATE_ENABLED) {
791                 WARN(1, "omap_hwmod: %s: reset can only be entered from "
792                      "enabled state\n", oh->name);
793                 return -EINVAL;
794         }
795
796         pr_debug("omap_hwmod: %s: resetting\n", oh->name);
797
798         v = oh->_sysc_cache;
799         r = _set_softreset(oh, &v);
800         if (r)
801                 return r;
802         _write_sysconfig(v, oh);
803
804         omap_test_timeout((omap_hwmod_readl(oh, oh->sysconfig->syss_offs) &
805                            SYSS_RESETDONE_MASK),
806                           MAX_MODULE_RESET_WAIT, c);
807
808         if (c == MAX_MODULE_RESET_WAIT)
809                 WARN(1, "omap_hwmod: %s: failed to reset in %d usec\n",
810                      oh->name, MAX_MODULE_RESET_WAIT);
811         else
812                 pr_debug("omap_hwmod: %s: reset in %d usec\n", oh->name, c);
813
814         /*
815          * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
816          * _wait_target_ready() or _reset()
817          */
818
819         return (c == MAX_MODULE_RESET_WAIT) ? -ETIMEDOUT : 0;
820 }
821
822 /**
823  * _enable - enable an omap_hwmod
824  * @oh: struct omap_hwmod *
825  *
826  * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
827  * register target.  Must be called with omap_hwmod_mutex held.
828  * Returns -EINVAL if the hwmod is in the wrong state or passes along
829  * the return value of _wait_target_ready().
830  */
831 static int _enable(struct omap_hwmod *oh)
832 {
833         int r;
834
835         if (oh->_state != _HWMOD_STATE_INITIALIZED &&
836             oh->_state != _HWMOD_STATE_IDLE &&
837             oh->_state != _HWMOD_STATE_DISABLED) {
838                 WARN(1, "omap_hwmod: %s: enabled state can only be entered "
839                      "from initialized, idle, or disabled state\n", oh->name);
840                 return -EINVAL;
841         }
842
843         pr_debug("omap_hwmod: %s: enabling\n", oh->name);
844
845         /* XXX mux balls */
846
847         _add_initiator_dep(oh, mpu_oh);
848         _enable_clocks(oh);
849
850         if (oh->sysconfig) {
851                 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
852                         _update_sysc_cache(oh);
853                 _sysc_enable(oh);
854         }
855
856         r = _wait_target_ready(oh);
857         if (!r)
858                 oh->_state = _HWMOD_STATE_ENABLED;
859
860         return r;
861 }
862
863 /**
864  * _idle - idle an omap_hwmod
865  * @oh: struct omap_hwmod *
866  *
867  * Idles an omap_hwmod @oh.  This should be called once the hwmod has
868  * no further work.  Returns -EINVAL if the hwmod is in the wrong
869  * state or returns 0.
870  */
871 static int _idle(struct omap_hwmod *oh)
872 {
873         if (oh->_state != _HWMOD_STATE_ENABLED) {
874                 WARN(1, "omap_hwmod: %s: idle state can only be entered from "
875                      "enabled state\n", oh->name);
876                 return -EINVAL;
877         }
878
879         pr_debug("omap_hwmod: %s: idling\n", oh->name);
880
881         if (oh->sysconfig)
882                 _sysc_idle(oh);
883         _del_initiator_dep(oh, mpu_oh);
884         _disable_clocks(oh);
885
886         oh->_state = _HWMOD_STATE_IDLE;
887
888         return 0;
889 }
890
891 /**
892  * _shutdown - shutdown an omap_hwmod
893  * @oh: struct omap_hwmod *
894  *
895  * Shut down an omap_hwmod @oh.  This should be called when the driver
896  * used for the hwmod is removed or unloaded or if the driver is not
897  * used by the system.  Returns -EINVAL if the hwmod is in the wrong
898  * state or returns 0.
899  */
900 static int _shutdown(struct omap_hwmod *oh)
901 {
902         if (oh->_state != _HWMOD_STATE_IDLE &&
903             oh->_state != _HWMOD_STATE_ENABLED) {
904                 WARN(1, "omap_hwmod: %s: disabled state can only be entered "
905                      "from idle, or enabled state\n", oh->name);
906                 return -EINVAL;
907         }
908
909         pr_debug("omap_hwmod: %s: disabling\n", oh->name);
910
911         if (oh->sysconfig)
912                 _sysc_shutdown(oh);
913         _del_initiator_dep(oh, mpu_oh);
914         /* XXX what about the other system initiators here? DMA, tesla, d2d */
915         _disable_clocks(oh);
916         /* XXX Should this code also force-disable the optional clocks? */
917
918         /* XXX mux any associated balls to safe mode */
919
920         oh->_state = _HWMOD_STATE_DISABLED;
921
922         return 0;
923 }
924
925 /**
926  * _setup - do initial configuration of omap_hwmod
927  * @oh: struct omap_hwmod *
928  *
929  * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
930  * OCP_SYSCONFIG register.  Must be called with omap_hwmod_mutex
931  * held.  Returns -EINVAL if the hwmod is in the wrong state or returns
932  * 0.
933  */
934 static int _setup(struct omap_hwmod *oh)
935 {
936         struct omap_hwmod_ocp_if *os;
937         int i;
938
939         if (!oh)
940                 return -EINVAL;
941
942         /* Set iclk autoidle mode */
943         if (oh->slaves_cnt > 0) {
944                 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
945                         struct clk *c = os->_clk;
946
947                         if (!c || IS_ERR(c))
948                                 continue;
949
950                         if (os->flags & OCPIF_SWSUP_IDLE) {
951                                 /* XXX omap_iclk_deny_idle(c); */
952                         } else {
953                                 /* XXX omap_iclk_allow_idle(c); */
954                                 clk_enable(c);
955                         }
956                 }
957         }
958
959         oh->_state = _HWMOD_STATE_INITIALIZED;
960
961         _enable(oh);
962
963         if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
964                 /*
965                  * XXX Do the OCP_SYSCONFIG bits need to be
966                  * reprogrammed after a reset?  If not, then this can
967                  * be removed.  If they do, then probably the
968                  * _enable() function should be split to avoid the
969                  * rewrite of the OCP_SYSCONFIG register.
970                  */
971                 if (oh->sysconfig) {
972                         _update_sysc_cache(oh);
973                         _sysc_enable(oh);
974                 }
975         }
976
977         if (!(oh->flags & HWMOD_INIT_NO_IDLE))
978                 _idle(oh);
979
980         return 0;
981 }
982
983
984
985 /* Public functions */
986
987 u32 omap_hwmod_readl(struct omap_hwmod *oh, u16 reg_offs)
988 {
989         return __raw_readl(oh->_rt_va + reg_offs);
990 }
991
992 void omap_hwmod_writel(u32 v, struct omap_hwmod *oh, u16 reg_offs)
993 {
994         __raw_writel(v, oh->_rt_va + reg_offs);
995 }
996
997 /**
998  * omap_hwmod_register - register a struct omap_hwmod
999  * @oh: struct omap_hwmod *
1000  *
1001  * Registers the omap_hwmod @oh.  Returns -EEXIST if an omap_hwmod already
1002  * has been registered by the same name; -EINVAL if the omap_hwmod is in the
1003  * wrong state, or 0 on success.
1004  *
1005  * XXX The data should be copied into bootmem, so the original data
1006  * should be marked __initdata and freed after init.  This would allow
1007  * unneeded omap_hwmods to be freed on multi-OMAP configurations.  Note
1008  * that the copy process would be relatively complex due to the large number
1009  * of substructures.
1010  */
1011 int omap_hwmod_register(struct omap_hwmod *oh)
1012 {
1013         int ret, ms_id;
1014
1015         if (!oh || (oh->_state != _HWMOD_STATE_UNKNOWN))
1016                 return -EINVAL;
1017
1018         mutex_lock(&omap_hwmod_mutex);
1019
1020         pr_debug("omap_hwmod: %s: registering\n", oh->name);
1021
1022         if (_lookup(oh->name)) {
1023                 ret = -EEXIST;
1024                 goto ohr_unlock;
1025         }
1026
1027         ms_id = _find_mpu_port_index(oh);
1028         if (!IS_ERR_VALUE(ms_id)) {
1029                 oh->_mpu_port_index = ms_id;
1030                 oh->_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
1031         } else {
1032                 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1033         }
1034
1035         list_add_tail(&oh->node, &omap_hwmod_list);
1036
1037         oh->_state = _HWMOD_STATE_REGISTERED;
1038
1039         ret = 0;
1040
1041 ohr_unlock:
1042         mutex_unlock(&omap_hwmod_mutex);
1043         return ret;
1044 }
1045
1046 /**
1047  * omap_hwmod_lookup - look up a registered omap_hwmod by name
1048  * @name: name of the omap_hwmod to look up
1049  *
1050  * Given a @name of an omap_hwmod, return a pointer to the registered
1051  * struct omap_hwmod *, or NULL upon error.
1052  */
1053 struct omap_hwmod *omap_hwmod_lookup(const char *name)
1054 {
1055         struct omap_hwmod *oh;
1056
1057         if (!name)
1058                 return NULL;
1059
1060         mutex_lock(&omap_hwmod_mutex);
1061         oh = _lookup(name);
1062         mutex_unlock(&omap_hwmod_mutex);
1063
1064         return oh;
1065 }
1066
1067 /**
1068  * omap_hwmod_for_each - call function for each registered omap_hwmod
1069  * @fn: pointer to a callback function
1070  *
1071  * Call @fn for each registered omap_hwmod, passing @data to each
1072  * function.  @fn must return 0 for success or any other value for
1073  * failure.  If @fn returns non-zero, the iteration across omap_hwmods
1074  * will stop and the non-zero return value will be passed to the
1075  * caller of omap_hwmod_for_each().  @fn is called with
1076  * omap_hwmod_for_each() held.
1077  */
1078 int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh))
1079 {
1080         struct omap_hwmod *temp_oh;
1081         int ret;
1082
1083         if (!fn)
1084                 return -EINVAL;
1085
1086         mutex_lock(&omap_hwmod_mutex);
1087         list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1088                 ret = (*fn)(temp_oh);
1089                 if (ret)
1090                         break;
1091         }
1092         mutex_unlock(&omap_hwmod_mutex);
1093
1094         return ret;
1095 }
1096
1097
1098 /**
1099  * omap_hwmod_init - init omap_hwmod code and register hwmods
1100  * @ohs: pointer to an array of omap_hwmods to register
1101  *
1102  * Intended to be called early in boot before the clock framework is
1103  * initialized.  If @ohs is not null, will register all omap_hwmods
1104  * listed in @ohs that are valid for this chip.  Returns -EINVAL if
1105  * omap_hwmod_init() has already been called or 0 otherwise.
1106  */
1107 int omap_hwmod_init(struct omap_hwmod **ohs)
1108 {
1109         struct omap_hwmod *oh;
1110         int r;
1111
1112         if (inited)
1113                 return -EINVAL;
1114
1115         inited = 1;
1116
1117         if (!ohs)
1118                 return 0;
1119
1120         oh = *ohs;
1121         while (oh) {
1122                 if (omap_chip_is(oh->omap_chip)) {
1123                         r = omap_hwmod_register(oh);
1124                         WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
1125                              "%d\n", oh->name, r);
1126                 }
1127                 oh = *++ohs;
1128         }
1129
1130         return 0;
1131 }
1132
1133 /**
1134  * omap_hwmod_late_init - do some post-clock framework initialization
1135  *
1136  * Must be called after omap2_clk_init().  Resolves the struct clk names
1137  * to struct clk pointers for each registered omap_hwmod.  Also calls
1138  * _setup() on each hwmod.  Returns 0.
1139  */
1140 int omap_hwmod_late_init(void)
1141 {
1142         int r;
1143
1144         /* XXX check return value */
1145         r = omap_hwmod_for_each(_init_clocks);
1146         WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
1147
1148         mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
1149         WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
1150              MPU_INITIATOR_NAME);
1151
1152         omap_hwmod_for_each(_setup);
1153
1154         return 0;
1155 }
1156
1157 /**
1158  * omap_hwmod_unregister - unregister an omap_hwmod
1159  * @oh: struct omap_hwmod *
1160  *
1161  * Unregisters a previously-registered omap_hwmod @oh.  There's probably
1162  * no use case for this, so it is likely to be removed in a later version.
1163  *
1164  * XXX Free all of the bootmem-allocated structures here when that is
1165  * implemented.  Make it clear that core code is the only code that is
1166  * expected to unregister modules.
1167  */
1168 int omap_hwmod_unregister(struct omap_hwmod *oh)
1169 {
1170         if (!oh)
1171                 return -EINVAL;
1172
1173         pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
1174
1175         mutex_lock(&omap_hwmod_mutex);
1176         iounmap(oh->_rt_va);
1177         list_del(&oh->node);
1178         mutex_unlock(&omap_hwmod_mutex);
1179
1180         return 0;
1181 }
1182
1183 /**
1184  * omap_hwmod_enable - enable an omap_hwmod
1185  * @oh: struct omap_hwmod *
1186  *
1187  * Enable an omap_hwomd @oh.  Intended to be called by omap_device_enable().
1188  * Returns -EINVAL on error or passes along the return value from _enable().
1189  */
1190 int omap_hwmod_enable(struct omap_hwmod *oh)
1191 {
1192         int r;
1193
1194         if (!oh)
1195                 return -EINVAL;
1196
1197         mutex_lock(&omap_hwmod_mutex);
1198         r = _enable(oh);
1199         mutex_unlock(&omap_hwmod_mutex);
1200
1201         return r;
1202 }
1203
1204 /**
1205  * omap_hwmod_idle - idle an omap_hwmod
1206  * @oh: struct omap_hwmod *
1207  *
1208  * Idle an omap_hwomd @oh.  Intended to be called by omap_device_idle().
1209  * Returns -EINVAL on error or passes along the return value from _idle().
1210  */
1211 int omap_hwmod_idle(struct omap_hwmod *oh)
1212 {
1213         if (!oh)
1214                 return -EINVAL;
1215
1216         mutex_lock(&omap_hwmod_mutex);
1217         _idle(oh);
1218         mutex_unlock(&omap_hwmod_mutex);
1219
1220         return 0;
1221 }
1222
1223 /**
1224  * omap_hwmod_shutdown - shutdown an omap_hwmod
1225  * @oh: struct omap_hwmod *
1226  *
1227  * Shutdown an omap_hwomd @oh.  Intended to be called by
1228  * omap_device_shutdown().  Returns -EINVAL on error or passes along
1229  * the return value from _shutdown().
1230  */
1231 int omap_hwmod_shutdown(struct omap_hwmod *oh)
1232 {
1233         if (!oh)
1234                 return -EINVAL;
1235
1236         mutex_lock(&omap_hwmod_mutex);
1237         _shutdown(oh);
1238         mutex_unlock(&omap_hwmod_mutex);
1239
1240         return 0;
1241 }
1242
1243 /**
1244  * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
1245  * @oh: struct omap_hwmod *oh
1246  *
1247  * Intended to be called by the omap_device code.
1248  */
1249 int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
1250 {
1251         mutex_lock(&omap_hwmod_mutex);
1252         _enable_clocks(oh);
1253         mutex_unlock(&omap_hwmod_mutex);
1254
1255         return 0;
1256 }
1257
1258 /**
1259  * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
1260  * @oh: struct omap_hwmod *oh
1261  *
1262  * Intended to be called by the omap_device code.
1263  */
1264 int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
1265 {
1266         mutex_lock(&omap_hwmod_mutex);
1267         _disable_clocks(oh);
1268         mutex_unlock(&omap_hwmod_mutex);
1269
1270         return 0;
1271 }
1272
1273 /**
1274  * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
1275  * @oh: struct omap_hwmod *oh
1276  *
1277  * Intended to be called by drivers and core code when all posted
1278  * writes to a device must complete before continuing further
1279  * execution (for example, after clearing some device IRQSTATUS
1280  * register bits)
1281  *
1282  * XXX what about targets with multiple OCP threads?
1283  */
1284 void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
1285 {
1286         BUG_ON(!oh);
1287
1288         if (!oh->sysconfig || !oh->sysconfig->sysc_flags) {
1289                 WARN(1, "omap_device: %s: OCP barrier impossible due to "
1290                       "device configuration\n", oh->name);
1291                 return;
1292         }
1293
1294         /*
1295          * Forces posted writes to complete on the OCP thread handling
1296          * register writes
1297          */
1298         omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
1299 }
1300
1301 /**
1302  * omap_hwmod_reset - reset the hwmod
1303  * @oh: struct omap_hwmod *
1304  *
1305  * Under some conditions, a driver may wish to reset the entire device.
1306  * Called from omap_device code.  Returns -EINVAL on error or passes along
1307  * the return value from _reset()/_enable().
1308  */
1309 int omap_hwmod_reset(struct omap_hwmod *oh)
1310 {
1311         int r;
1312
1313         if (!oh || !(oh->_state & _HWMOD_STATE_ENABLED))
1314                 return -EINVAL;
1315
1316         mutex_lock(&omap_hwmod_mutex);
1317         r = _reset(oh);
1318         if (!r)
1319                 r = _enable(oh);
1320         mutex_unlock(&omap_hwmod_mutex);
1321
1322         return r;
1323 }
1324
1325 /**
1326  * omap_hwmod_count_resources - count number of struct resources needed by hwmod
1327  * @oh: struct omap_hwmod *
1328  * @res: pointer to the first element of an array of struct resource to fill
1329  *
1330  * Count the number of struct resource array elements necessary to
1331  * contain omap_hwmod @oh resources.  Intended to be called by code
1332  * that registers omap_devices.  Intended to be used to determine the
1333  * size of a dynamically-allocated struct resource array, before
1334  * calling omap_hwmod_fill_resources().  Returns the number of struct
1335  * resource array elements needed.
1336  *
1337  * XXX This code is not optimized.  It could attempt to merge adjacent
1338  * resource IDs.
1339  *
1340  */
1341 int omap_hwmod_count_resources(struct omap_hwmod *oh)
1342 {
1343         int ret, i;
1344
1345         ret = oh->mpu_irqs_cnt + oh->sdma_chs_cnt;
1346
1347         for (i = 0; i < oh->slaves_cnt; i++)
1348                 ret += (*oh->slaves + i)->addr_cnt;
1349
1350         return ret;
1351 }
1352
1353 /**
1354  * omap_hwmod_fill_resources - fill struct resource array with hwmod data
1355  * @oh: struct omap_hwmod *
1356  * @res: pointer to the first element of an array of struct resource to fill
1357  *
1358  * Fill the struct resource array @res with resource data from the
1359  * omap_hwmod @oh.  Intended to be called by code that registers
1360  * omap_devices.  See also omap_hwmod_count_resources().  Returns the
1361  * number of array elements filled.
1362  */
1363 int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
1364 {
1365         int i, j;
1366         int r = 0;
1367
1368         /* For each IRQ, DMA, memory area, fill in array.*/
1369
1370         for (i = 0; i < oh->mpu_irqs_cnt; i++) {
1371                 (res + r)->name = (oh->mpu_irqs + i)->name;
1372                 (res + r)->start = (oh->mpu_irqs + i)->irq;
1373                 (res + r)->end = (oh->mpu_irqs + i)->irq;
1374                 (res + r)->flags = IORESOURCE_IRQ;
1375                 r++;
1376         }
1377
1378         for (i = 0; i < oh->sdma_chs_cnt; i++) {
1379                 (res + r)->name = (oh->sdma_chs + i)->name;
1380                 (res + r)->start = (oh->sdma_chs + i)->dma_ch;
1381                 (res + r)->end = (oh->sdma_chs + i)->dma_ch;
1382                 (res + r)->flags = IORESOURCE_DMA;
1383                 r++;
1384         }
1385
1386         for (i = 0; i < oh->slaves_cnt; i++) {
1387                 struct omap_hwmod_ocp_if *os;
1388
1389                 os = *oh->slaves + i;
1390
1391                 for (j = 0; j < os->addr_cnt; j++) {
1392                         (res + r)->start = (os->addr + j)->pa_start;
1393                         (res + r)->end = (os->addr + j)->pa_end;
1394                         (res + r)->flags = IORESOURCE_MEM;
1395                         r++;
1396                 }
1397         }
1398
1399         return r;
1400 }
1401
1402 /**
1403  * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
1404  * @oh: struct omap_hwmod *
1405  *
1406  * Return the powerdomain pointer associated with the OMAP module
1407  * @oh's main clock.  If @oh does not have a main clk, return the
1408  * powerdomain associated with the interface clock associated with the
1409  * module's MPU port. (XXX Perhaps this should use the SDMA port
1410  * instead?)  Returns NULL on error, or a struct powerdomain * on
1411  * success.
1412  */
1413 struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
1414 {
1415         struct clk *c;
1416
1417         if (!oh)
1418                 return NULL;
1419
1420         if (oh->_clk) {
1421                 c = oh->_clk;
1422         } else {
1423                 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1424                         return NULL;
1425                 c = oh->slaves[oh->_mpu_port_index]->_clk;
1426         }
1427
1428         return c->clkdm->pwrdm.ptr;
1429
1430 }
1431
1432 /**
1433  * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
1434  * @oh: struct omap_hwmod *
1435  * @init_oh: struct omap_hwmod * (initiator)
1436  *
1437  * Add a sleep dependency between the initiator @init_oh and @oh.
1438  * Intended to be called by DSP/Bridge code via platform_data for the
1439  * DSP case; and by the DMA code in the sDMA case.  DMA code, *Bridge
1440  * code needs to add/del initiator dependencies dynamically
1441  * before/after accessing a device.  Returns the return value from
1442  * _add_initiator_dep().
1443  *
1444  * XXX Keep a usecount in the clockdomain code
1445  */
1446 int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
1447                                  struct omap_hwmod *init_oh)
1448 {
1449         return _add_initiator_dep(oh, init_oh);
1450 }
1451
1452 /*
1453  * XXX what about functions for drivers to save/restore ocp_sysconfig
1454  * for context save/restore operations?
1455  */
1456
1457 /**
1458  * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
1459  * @oh: struct omap_hwmod *
1460  * @init_oh: struct omap_hwmod * (initiator)
1461  *
1462  * Remove a sleep dependency between the initiator @init_oh and @oh.
1463  * Intended to be called by DSP/Bridge code via platform_data for the
1464  * DSP case; and by the DMA code in the sDMA case.  DMA code, *Bridge
1465  * code needs to add/del initiator dependencies dynamically
1466  * before/after accessing a device.  Returns the return value from
1467  * _del_initiator_dep().
1468  *
1469  * XXX Keep a usecount in the clockdomain code
1470  */
1471 int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
1472                                  struct omap_hwmod *init_oh)
1473 {
1474         return _del_initiator_dep(oh, init_oh);
1475 }
1476
1477 /**
1478  * omap_hwmod_enable_wakeup - allow device to wake up the system
1479  * @oh: struct omap_hwmod *
1480  *
1481  * Sets the module OCP socket ENAWAKEUP bit to allow the module to
1482  * send wakeups to the PRCM.  Eventually this should sets PRCM wakeup
1483  * registers to cause the PRCM to receive wakeup events from the
1484  * module.  Does not set any wakeup routing registers beyond this
1485  * point - if the module is to wake up any other module or subsystem,
1486  * that must be set separately.  Called by omap_device code.  Returns
1487  * -EINVAL on error or 0 upon success.
1488  */
1489 int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
1490 {
1491         if (!oh->sysconfig ||
1492             !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
1493                 return -EINVAL;
1494
1495         mutex_lock(&omap_hwmod_mutex);
1496         _enable_wakeup(oh);
1497         mutex_unlock(&omap_hwmod_mutex);
1498
1499         return 0;
1500 }
1501
1502 /**
1503  * omap_hwmod_disable_wakeup - prevent device from waking the system
1504  * @oh: struct omap_hwmod *
1505  *
1506  * Clears the module OCP socket ENAWAKEUP bit to prevent the module
1507  * from sending wakeups to the PRCM.  Eventually this should clear
1508  * PRCM wakeup registers to cause the PRCM to ignore wakeup events
1509  * from the module.  Does not set any wakeup routing registers beyond
1510  * this point - if the module is to wake up any other module or
1511  * subsystem, that must be set separately.  Called by omap_device
1512  * code.  Returns -EINVAL on error or 0 upon success.
1513  */
1514 int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
1515 {
1516         if (!oh->sysconfig ||
1517             !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
1518                 return -EINVAL;
1519
1520         mutex_lock(&omap_hwmod_mutex);
1521         _disable_wakeup(oh);
1522         mutex_unlock(&omap_hwmod_mutex);
1523
1524         return 0;
1525 }