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