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