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