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