blob: bfbd34142b3798b80e5ced6bfa9fca077f5c4700 [file] [log] [blame]
Paul Walmsley63c85232009-09-03 20:14:03 +03001/*
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
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -070048#include <plat/common.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070049#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>
Paul Walmsley63c85232009-09-03 20:14:03 +030054
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 */
64static LIST_HEAD(omap_hwmod_list);
65
66static DEFINE_MUTEX(omap_hwmod_mutex);
67
68/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
69static struct omap_hwmod *mpu_oh;
70
71/* inited: 0 if omap_hwmod_init() has not yet been called; 1 otherwise */
72static 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 */
85static 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 */
110static 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 */
136static 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 */
159static 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 */
182static 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 */
202static 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/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700214 * _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 */
226static 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/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300240 * _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 */
246static 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 */
272static 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 */
303static 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 */
324static 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 */
341static 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 */
366static 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 */
399static 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 */
426static 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 */
456static 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 */
487static 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 */
520static 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;
Tony Lindgren986a13f2009-10-19 15:25:22 -0700526 void __iomem *va_start;
Paul Walmsley63c85232009-09-03 20:14:03 +0300527
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
Tony Lindgren986a13f2009-10-19 15:25:22 -0700540 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 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300546 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
Tony Lindgren986a13f2009-10-19 15:25:22 -0700547 oh->name, va_start);
548 } else {
Paul Walmsley63c85232009-09-03 20:14:03 +0300549 pr_debug("omap_hwmod: %s: no MPU register target found\n",
550 oh->name);
Tony Lindgren986a13f2009-10-19 15:25:22 -0700551 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300552
Tony Lindgren986a13f2009-10-19 15:25:22 -0700553 return (found) ? va_start : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +0300554}
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 */
565static 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
Paul Walmsley726072e2009-12-08 16:34:15 -0700587 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? */
Paul Walmsley63c85232009-09-03 20:14:03 +0300594
Paul Walmsleya16b1f72009-12-08 16:34:17 -0700595 /*
596 * XXX The clock framework should handle this, by
597 * calling into this code. But this must wait until the
598 * clock structures are tagged with omap_hwmod entries
599 */
Paul Walmsley63c85232009-09-03 20:14:03 +0300600 if (oh->flags & HWMOD_SET_DEFAULT_CLOCKACT &&
601 oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY)
602 _set_clockactivity(oh, oh->sysconfig->clockact, &v);
603
604 _write_sysconfig(v, oh);
605}
606
607/**
608 * _sysc_idle - try to put a module into idle via OCP_SYSCONFIG
609 * @oh: struct omap_hwmod *
610 *
611 * If module is marked as SWSUP_SIDLE, force the module into slave
612 * idle; otherwise, configure it for smart-idle. If module is marked
613 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
614 * configure it for smart-standby. No return value.
615 */
616static void _sysc_idle(struct omap_hwmod *oh)
617{
618 u8 idlemode;
619 u32 v;
620
621 if (!oh->sysconfig)
622 return;
623
624 v = oh->_sysc_cache;
625
626 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
627 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
628 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
629 _set_slave_idlemode(oh, idlemode, &v);
630 }
631
632 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
633 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
634 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
635 _set_master_standbymode(oh, idlemode, &v);
636 }
637
638 _write_sysconfig(v, oh);
639}
640
641/**
642 * _sysc_shutdown - force a module into idle via OCP_SYSCONFIG
643 * @oh: struct omap_hwmod *
644 *
645 * Force the module into slave idle and master suspend. No return
646 * value.
647 */
648static void _sysc_shutdown(struct omap_hwmod *oh)
649{
650 u32 v;
651
652 if (!oh->sysconfig)
653 return;
654
655 v = oh->_sysc_cache;
656
657 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE)
658 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
659
660 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE)
661 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
662
Paul Walmsley726072e2009-12-08 16:34:15 -0700663 if (oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE)
664 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +0300665
666 _write_sysconfig(v, oh);
667}
668
669/**
670 * _lookup - find an omap_hwmod by name
671 * @name: find an omap_hwmod by name
672 *
673 * Return a pointer to an omap_hwmod by name, or NULL if not found.
674 * Caller must hold omap_hwmod_mutex.
675 */
676static struct omap_hwmod *_lookup(const char *name)
677{
678 struct omap_hwmod *oh, *temp_oh;
679
680 oh = NULL;
681
682 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
683 if (!strcmp(name, temp_oh->name)) {
684 oh = temp_oh;
685 break;
686 }
687 }
688
689 return oh;
690}
691
692/**
693 * _init_clocks - clk_get() all clocks associated with this hwmod
694 * @oh: struct omap_hwmod *
695 *
696 * Called by omap_hwmod_late_init() (after omap2_clk_init()).
697 * Resolves all clock names embedded in the hwmod. Must be called
698 * with omap_hwmod_mutex held. Returns -EINVAL if the omap_hwmod
699 * has not yet been registered or if the clocks have already been
700 * initialized, 0 on success, or a non-zero error on failure.
701 */
702static int _init_clocks(struct omap_hwmod *oh)
703{
704 int ret = 0;
705
706 if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
707 return -EINVAL;
708
709 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
710
711 ret |= _init_main_clk(oh);
712 ret |= _init_interface_clks(oh);
713 ret |= _init_opt_clks(oh);
714
715 oh->_state = _HWMOD_STATE_CLKS_INITED;
716
717 return ret;
718}
719
720/**
721 * _wait_target_ready - wait for a module to leave slave idle
722 * @oh: struct omap_hwmod *
723 *
724 * Wait for a module @oh to leave slave idle. Returns 0 if the module
725 * does not have an IDLEST bit or if the module successfully leaves
726 * slave idle; otherwise, pass along the return value of the
727 * appropriate *_cm_wait_module_ready() function.
728 */
729static int _wait_target_ready(struct omap_hwmod *oh)
730{
731 struct omap_hwmod_ocp_if *os;
732 int ret;
733
734 if (!oh)
735 return -EINVAL;
736
737 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
738 return 0;
739
740 os = *oh->slaves + oh->_mpu_port_index;
741
742 if (!(os->flags & OCPIF_HAS_IDLEST))
743 return 0;
744
745 /* XXX check module SIDLEMODE */
746
747 /* XXX check clock enable states */
748
749 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
750 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
751 oh->prcm.omap2.idlest_reg_id,
752 oh->prcm.omap2.idlest_idle_bit);
753#if 0
754 } else if (cpu_is_omap44xx()) {
755 ret = omap4_cm_wait_module_ready(oh->prcm.omap4.module_offs,
756 oh->prcm.omap4.device_offs);
757#endif
758 } else {
759 BUG();
760 };
761
762 return ret;
763}
764
765/**
766 * _reset - reset an omap_hwmod
767 * @oh: struct omap_hwmod *
768 *
769 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
770 * enabled for this to work. Must be called with omap_hwmod_mutex
771 * held. Returns -EINVAL if the hwmod cannot be reset this way or if
772 * the hwmod is in the wrong state, -ETIMEDOUT if the module did not
773 * reset in time, or 0 upon success.
774 */
775static int _reset(struct omap_hwmod *oh)
776{
777 u32 r, v;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -0700778 int c = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300779
780 if (!oh->sysconfig ||
781 !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET) ||
782 (oh->sysconfig->sysc_flags & SYSS_MISSING))
783 return -EINVAL;
784
785 /* clocks must be on for this operation */
786 if (oh->_state != _HWMOD_STATE_ENABLED) {
787 WARN(1, "omap_hwmod: %s: reset can only be entered from "
788 "enabled state\n", oh->name);
789 return -EINVAL;
790 }
791
792 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
793
794 v = oh->_sysc_cache;
795 r = _set_softreset(oh, &v);
796 if (r)
797 return r;
798 _write_sysconfig(v, oh);
799
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -0700800 omap_test_timeout((omap_hwmod_readl(oh, oh->sysconfig->syss_offs) &
801 SYSS_RESETDONE_MASK),
802 MAX_MODULE_RESET_WAIT, c);
Paul Walmsley63c85232009-09-03 20:14:03 +0300803
804 if (c == MAX_MODULE_RESET_WAIT)
805 WARN(1, "omap_hwmod: %s: failed to reset in %d usec\n",
806 oh->name, MAX_MODULE_RESET_WAIT);
807 else
Paul Walmsley02bfc0302009-09-03 20:14:05 +0300808 pr_debug("omap_hwmod: %s: reset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +0300809
810 /*
811 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
812 * _wait_target_ready() or _reset()
813 */
814
815 return (c == MAX_MODULE_RESET_WAIT) ? -ETIMEDOUT : 0;
816}
817
818/**
819 * _enable - enable an omap_hwmod
820 * @oh: struct omap_hwmod *
821 *
822 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
823 * register target. Must be called with omap_hwmod_mutex held.
824 * Returns -EINVAL if the hwmod is in the wrong state or passes along
825 * the return value of _wait_target_ready().
826 */
827static int _enable(struct omap_hwmod *oh)
828{
829 int r;
830
831 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
832 oh->_state != _HWMOD_STATE_IDLE &&
833 oh->_state != _HWMOD_STATE_DISABLED) {
834 WARN(1, "omap_hwmod: %s: enabled state can only be entered "
835 "from initialized, idle, or disabled state\n", oh->name);
836 return -EINVAL;
837 }
838
839 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
840
841 /* XXX mux balls */
842
843 _add_initiator_dep(oh, mpu_oh);
844 _enable_clocks(oh);
845
846 if (oh->sysconfig) {
847 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
848 _update_sysc_cache(oh);
849 _sysc_enable(oh);
850 }
851
852 r = _wait_target_ready(oh);
853 if (!r)
854 oh->_state = _HWMOD_STATE_ENABLED;
855
856 return r;
857}
858
859/**
860 * _idle - idle an omap_hwmod
861 * @oh: struct omap_hwmod *
862 *
863 * Idles an omap_hwmod @oh. This should be called once the hwmod has
864 * no further work. Returns -EINVAL if the hwmod is in the wrong
865 * state or returns 0.
866 */
867static int _idle(struct omap_hwmod *oh)
868{
869 if (oh->_state != _HWMOD_STATE_ENABLED) {
870 WARN(1, "omap_hwmod: %s: idle state can only be entered from "
871 "enabled state\n", oh->name);
872 return -EINVAL;
873 }
874
875 pr_debug("omap_hwmod: %s: idling\n", oh->name);
876
877 if (oh->sysconfig)
878 _sysc_idle(oh);
879 _del_initiator_dep(oh, mpu_oh);
880 _disable_clocks(oh);
881
882 oh->_state = _HWMOD_STATE_IDLE;
883
884 return 0;
885}
886
887/**
888 * _shutdown - shutdown an omap_hwmod
889 * @oh: struct omap_hwmod *
890 *
891 * Shut down an omap_hwmod @oh. This should be called when the driver
892 * used for the hwmod is removed or unloaded or if the driver is not
893 * used by the system. Returns -EINVAL if the hwmod is in the wrong
894 * state or returns 0.
895 */
896static int _shutdown(struct omap_hwmod *oh)
897{
898 if (oh->_state != _HWMOD_STATE_IDLE &&
899 oh->_state != _HWMOD_STATE_ENABLED) {
900 WARN(1, "omap_hwmod: %s: disabled state can only be entered "
901 "from idle, or enabled state\n", oh->name);
902 return -EINVAL;
903 }
904
905 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
906
907 if (oh->sysconfig)
908 _sysc_shutdown(oh);
909 _del_initiator_dep(oh, mpu_oh);
910 /* XXX what about the other system initiators here? DMA, tesla, d2d */
911 _disable_clocks(oh);
912 /* XXX Should this code also force-disable the optional clocks? */
913
914 /* XXX mux any associated balls to safe mode */
915
916 oh->_state = _HWMOD_STATE_DISABLED;
917
918 return 0;
919}
920
921/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300922 * _setup - do initial configuration of omap_hwmod
923 * @oh: struct omap_hwmod *
924 *
925 * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
926 * OCP_SYSCONFIG register. Must be called with omap_hwmod_mutex
927 * held. Returns -EINVAL if the hwmod is in the wrong state or returns
928 * 0.
929 */
930static int _setup(struct omap_hwmod *oh)
931{
932 struct omap_hwmod_ocp_if *os;
933 int i;
934
935 if (!oh)
936 return -EINVAL;
937
938 /* Set iclk autoidle mode */
939 if (oh->slaves_cnt > 0) {
940 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
941 struct clk *c = os->_clk;
942
943 if (!c || IS_ERR(c))
944 continue;
945
946 if (os->flags & OCPIF_SWSUP_IDLE) {
947 /* XXX omap_iclk_deny_idle(c); */
948 } else {
949 /* XXX omap_iclk_allow_idle(c); */
950 clk_enable(c);
951 }
952 }
953 }
954
955 oh->_state = _HWMOD_STATE_INITIALIZED;
956
957 _enable(oh);
958
Paul Walmsleyb835d012009-12-08 16:34:14 -0700959 if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
960 /*
961 * XXX Do the OCP_SYSCONFIG bits need to be
962 * reprogrammed after a reset? If not, then this can
963 * be removed. If they do, then probably the
964 * _enable() function should be split to avoid the
965 * rewrite of the OCP_SYSCONFIG register.
966 */
967 if (oh->sysconfig) {
968 _update_sysc_cache(oh);
969 _sysc_enable(oh);
970 }
971 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300972
973 if (!(oh->flags & HWMOD_INIT_NO_IDLE))
974 _idle(oh);
975
976 return 0;
977}
978
979
980
981/* Public functions */
982
983u32 omap_hwmod_readl(struct omap_hwmod *oh, u16 reg_offs)
984{
985 return __raw_readl(oh->_rt_va + reg_offs);
986}
987
988void omap_hwmod_writel(u32 v, struct omap_hwmod *oh, u16 reg_offs)
989{
990 __raw_writel(v, oh->_rt_va + reg_offs);
991}
992
993/**
994 * omap_hwmod_register - register a struct omap_hwmod
995 * @oh: struct omap_hwmod *
996 *
997 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod already
998 * has been registered by the same name; -EINVAL if the omap_hwmod is in the
999 * wrong state, or 0 on success.
1000 *
1001 * XXX The data should be copied into bootmem, so the original data
1002 * should be marked __initdata and freed after init. This would allow
1003 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
1004 * that the copy process would be relatively complex due to the large number
1005 * of substructures.
1006 */
1007int omap_hwmod_register(struct omap_hwmod *oh)
1008{
1009 int ret, ms_id;
1010
1011 if (!oh || (oh->_state != _HWMOD_STATE_UNKNOWN))
1012 return -EINVAL;
1013
1014 mutex_lock(&omap_hwmod_mutex);
1015
1016 pr_debug("omap_hwmod: %s: registering\n", oh->name);
1017
1018 if (_lookup(oh->name)) {
1019 ret = -EEXIST;
1020 goto ohr_unlock;
1021 }
1022
1023 ms_id = _find_mpu_port_index(oh);
1024 if (!IS_ERR_VALUE(ms_id)) {
1025 oh->_mpu_port_index = ms_id;
1026 oh->_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
1027 } else {
1028 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1029 }
1030
1031 list_add_tail(&oh->node, &omap_hwmod_list);
1032
1033 oh->_state = _HWMOD_STATE_REGISTERED;
1034
1035 ret = 0;
1036
1037ohr_unlock:
1038 mutex_unlock(&omap_hwmod_mutex);
1039 return ret;
1040}
1041
1042/**
1043 * omap_hwmod_lookup - look up a registered omap_hwmod by name
1044 * @name: name of the omap_hwmod to look up
1045 *
1046 * Given a @name of an omap_hwmod, return a pointer to the registered
1047 * struct omap_hwmod *, or NULL upon error.
1048 */
1049struct omap_hwmod *omap_hwmod_lookup(const char *name)
1050{
1051 struct omap_hwmod *oh;
1052
1053 if (!name)
1054 return NULL;
1055
1056 mutex_lock(&omap_hwmod_mutex);
1057 oh = _lookup(name);
1058 mutex_unlock(&omap_hwmod_mutex);
1059
1060 return oh;
1061}
1062
1063/**
1064 * omap_hwmod_for_each - call function for each registered omap_hwmod
1065 * @fn: pointer to a callback function
1066 *
1067 * Call @fn for each registered omap_hwmod, passing @data to each
1068 * function. @fn must return 0 for success or any other value for
1069 * failure. If @fn returns non-zero, the iteration across omap_hwmods
1070 * will stop and the non-zero return value will be passed to the
1071 * caller of omap_hwmod_for_each(). @fn is called with
1072 * omap_hwmod_for_each() held.
1073 */
1074int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh))
1075{
1076 struct omap_hwmod *temp_oh;
1077 int ret;
1078
1079 if (!fn)
1080 return -EINVAL;
1081
1082 mutex_lock(&omap_hwmod_mutex);
1083 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1084 ret = (*fn)(temp_oh);
1085 if (ret)
1086 break;
1087 }
1088 mutex_unlock(&omap_hwmod_mutex);
1089
1090 return ret;
1091}
1092
1093
1094/**
1095 * omap_hwmod_init - init omap_hwmod code and register hwmods
1096 * @ohs: pointer to an array of omap_hwmods to register
1097 *
1098 * Intended to be called early in boot before the clock framework is
1099 * initialized. If @ohs is not null, will register all omap_hwmods
1100 * listed in @ohs that are valid for this chip. Returns -EINVAL if
1101 * omap_hwmod_init() has already been called or 0 otherwise.
1102 */
1103int omap_hwmod_init(struct omap_hwmod **ohs)
1104{
1105 struct omap_hwmod *oh;
1106 int r;
1107
1108 if (inited)
1109 return -EINVAL;
1110
1111 inited = 1;
1112
1113 if (!ohs)
1114 return 0;
1115
1116 oh = *ohs;
1117 while (oh) {
1118 if (omap_chip_is(oh->omap_chip)) {
1119 r = omap_hwmod_register(oh);
1120 WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
1121 "%d\n", oh->name, r);
1122 }
1123 oh = *++ohs;
1124 }
1125
1126 return 0;
1127}
1128
1129/**
1130 * omap_hwmod_late_init - do some post-clock framework initialization
1131 *
1132 * Must be called after omap2_clk_init(). Resolves the struct clk names
1133 * to struct clk pointers for each registered omap_hwmod. Also calls
1134 * _setup() on each hwmod. Returns 0.
1135 */
1136int omap_hwmod_late_init(void)
1137{
1138 int r;
1139
1140 /* XXX check return value */
1141 r = omap_hwmod_for_each(_init_clocks);
1142 WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
1143
1144 mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
1145 WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
1146 MPU_INITIATOR_NAME);
1147
1148 omap_hwmod_for_each(_setup);
1149
1150 return 0;
1151}
1152
1153/**
1154 * omap_hwmod_unregister - unregister an omap_hwmod
1155 * @oh: struct omap_hwmod *
1156 *
1157 * Unregisters a previously-registered omap_hwmod @oh. There's probably
1158 * no use case for this, so it is likely to be removed in a later version.
1159 *
1160 * XXX Free all of the bootmem-allocated structures here when that is
1161 * implemented. Make it clear that core code is the only code that is
1162 * expected to unregister modules.
1163 */
1164int omap_hwmod_unregister(struct omap_hwmod *oh)
1165{
1166 if (!oh)
1167 return -EINVAL;
1168
1169 pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
1170
1171 mutex_lock(&omap_hwmod_mutex);
Tony Lindgren986a13f2009-10-19 15:25:22 -07001172 iounmap(oh->_rt_va);
Paul Walmsley63c85232009-09-03 20:14:03 +03001173 list_del(&oh->node);
1174 mutex_unlock(&omap_hwmod_mutex);
1175
1176 return 0;
1177}
1178
1179/**
1180 * omap_hwmod_enable - enable an omap_hwmod
1181 * @oh: struct omap_hwmod *
1182 *
1183 * Enable an omap_hwomd @oh. Intended to be called by omap_device_enable().
1184 * Returns -EINVAL on error or passes along the return value from _enable().
1185 */
1186int omap_hwmod_enable(struct omap_hwmod *oh)
1187{
1188 int r;
1189
1190 if (!oh)
1191 return -EINVAL;
1192
1193 mutex_lock(&omap_hwmod_mutex);
1194 r = _enable(oh);
1195 mutex_unlock(&omap_hwmod_mutex);
1196
1197 return r;
1198}
1199
1200/**
1201 * omap_hwmod_idle - idle an omap_hwmod
1202 * @oh: struct omap_hwmod *
1203 *
1204 * Idle an omap_hwomd @oh. Intended to be called by omap_device_idle().
1205 * Returns -EINVAL on error or passes along the return value from _idle().
1206 */
1207int omap_hwmod_idle(struct omap_hwmod *oh)
1208{
1209 if (!oh)
1210 return -EINVAL;
1211
1212 mutex_lock(&omap_hwmod_mutex);
1213 _idle(oh);
1214 mutex_unlock(&omap_hwmod_mutex);
1215
1216 return 0;
1217}
1218
1219/**
1220 * omap_hwmod_shutdown - shutdown an omap_hwmod
1221 * @oh: struct omap_hwmod *
1222 *
1223 * Shutdown an omap_hwomd @oh. Intended to be called by
1224 * omap_device_shutdown(). Returns -EINVAL on error or passes along
1225 * the return value from _shutdown().
1226 */
1227int omap_hwmod_shutdown(struct omap_hwmod *oh)
1228{
1229 if (!oh)
1230 return -EINVAL;
1231
1232 mutex_lock(&omap_hwmod_mutex);
1233 _shutdown(oh);
1234 mutex_unlock(&omap_hwmod_mutex);
1235
1236 return 0;
1237}
1238
1239/**
1240 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
1241 * @oh: struct omap_hwmod *oh
1242 *
1243 * Intended to be called by the omap_device code.
1244 */
1245int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
1246{
1247 mutex_lock(&omap_hwmod_mutex);
1248 _enable_clocks(oh);
1249 mutex_unlock(&omap_hwmod_mutex);
1250
1251 return 0;
1252}
1253
1254/**
1255 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
1256 * @oh: struct omap_hwmod *oh
1257 *
1258 * Intended to be called by the omap_device code.
1259 */
1260int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
1261{
1262 mutex_lock(&omap_hwmod_mutex);
1263 _disable_clocks(oh);
1264 mutex_unlock(&omap_hwmod_mutex);
1265
1266 return 0;
1267}
1268
1269/**
1270 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
1271 * @oh: struct omap_hwmod *oh
1272 *
1273 * Intended to be called by drivers and core code when all posted
1274 * writes to a device must complete before continuing further
1275 * execution (for example, after clearing some device IRQSTATUS
1276 * register bits)
1277 *
1278 * XXX what about targets with multiple OCP threads?
1279 */
1280void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
1281{
1282 BUG_ON(!oh);
1283
1284 if (!oh->sysconfig || !oh->sysconfig->sysc_flags) {
1285 WARN(1, "omap_device: %s: OCP barrier impossible due to "
1286 "device configuration\n", oh->name);
1287 return;
1288 }
1289
1290 /*
1291 * Forces posted writes to complete on the OCP thread handling
1292 * register writes
1293 */
1294 omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
1295}
1296
1297/**
1298 * omap_hwmod_reset - reset the hwmod
1299 * @oh: struct omap_hwmod *
1300 *
1301 * Under some conditions, a driver may wish to reset the entire device.
1302 * Called from omap_device code. Returns -EINVAL on error or passes along
1303 * the return value from _reset()/_enable().
1304 */
1305int omap_hwmod_reset(struct omap_hwmod *oh)
1306{
1307 int r;
1308
1309 if (!oh || !(oh->_state & _HWMOD_STATE_ENABLED))
1310 return -EINVAL;
1311
1312 mutex_lock(&omap_hwmod_mutex);
1313 r = _reset(oh);
1314 if (!r)
1315 r = _enable(oh);
1316 mutex_unlock(&omap_hwmod_mutex);
1317
1318 return r;
1319}
1320
1321/**
1322 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
1323 * @oh: struct omap_hwmod *
1324 * @res: pointer to the first element of an array of struct resource to fill
1325 *
1326 * Count the number of struct resource array elements necessary to
1327 * contain omap_hwmod @oh resources. Intended to be called by code
1328 * that registers omap_devices. Intended to be used to determine the
1329 * size of a dynamically-allocated struct resource array, before
1330 * calling omap_hwmod_fill_resources(). Returns the number of struct
1331 * resource array elements needed.
1332 *
1333 * XXX This code is not optimized. It could attempt to merge adjacent
1334 * resource IDs.
1335 *
1336 */
1337int omap_hwmod_count_resources(struct omap_hwmod *oh)
1338{
1339 int ret, i;
1340
1341 ret = oh->mpu_irqs_cnt + oh->sdma_chs_cnt;
1342
1343 for (i = 0; i < oh->slaves_cnt; i++)
1344 ret += (*oh->slaves + i)->addr_cnt;
1345
1346 return ret;
1347}
1348
1349/**
1350 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
1351 * @oh: struct omap_hwmod *
1352 * @res: pointer to the first element of an array of struct resource to fill
1353 *
1354 * Fill the struct resource array @res with resource data from the
1355 * omap_hwmod @oh. Intended to be called by code that registers
1356 * omap_devices. See also omap_hwmod_count_resources(). Returns the
1357 * number of array elements filled.
1358 */
1359int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
1360{
1361 int i, j;
1362 int r = 0;
1363
1364 /* For each IRQ, DMA, memory area, fill in array.*/
1365
1366 for (i = 0; i < oh->mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07001367 (res + r)->name = (oh->mpu_irqs + i)->name;
1368 (res + r)->start = (oh->mpu_irqs + i)->irq;
1369 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03001370 (res + r)->flags = IORESOURCE_IRQ;
1371 r++;
1372 }
1373
1374 for (i = 0; i < oh->sdma_chs_cnt; i++) {
1375 (res + r)->name = (oh->sdma_chs + i)->name;
1376 (res + r)->start = (oh->sdma_chs + i)->dma_ch;
1377 (res + r)->end = (oh->sdma_chs + i)->dma_ch;
1378 (res + r)->flags = IORESOURCE_DMA;
1379 r++;
1380 }
1381
1382 for (i = 0; i < oh->slaves_cnt; i++) {
1383 struct omap_hwmod_ocp_if *os;
1384
1385 os = *oh->slaves + i;
1386
1387 for (j = 0; j < os->addr_cnt; j++) {
1388 (res + r)->start = (os->addr + j)->pa_start;
1389 (res + r)->end = (os->addr + j)->pa_end;
1390 (res + r)->flags = IORESOURCE_MEM;
1391 r++;
1392 }
1393 }
1394
1395 return r;
1396}
1397
1398/**
1399 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
1400 * @oh: struct omap_hwmod *
1401 *
1402 * Return the powerdomain pointer associated with the OMAP module
1403 * @oh's main clock. If @oh does not have a main clk, return the
1404 * powerdomain associated with the interface clock associated with the
1405 * module's MPU port. (XXX Perhaps this should use the SDMA port
1406 * instead?) Returns NULL on error, or a struct powerdomain * on
1407 * success.
1408 */
1409struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
1410{
1411 struct clk *c;
1412
1413 if (!oh)
1414 return NULL;
1415
1416 if (oh->_clk) {
1417 c = oh->_clk;
1418 } else {
1419 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1420 return NULL;
1421 c = oh->slaves[oh->_mpu_port_index]->_clk;
1422 }
1423
1424 return c->clkdm->pwrdm.ptr;
1425
1426}
1427
1428/**
1429 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
1430 * @oh: struct omap_hwmod *
1431 * @init_oh: struct omap_hwmod * (initiator)
1432 *
1433 * Add a sleep dependency between the initiator @init_oh and @oh.
1434 * Intended to be called by DSP/Bridge code via platform_data for the
1435 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1436 * code needs to add/del initiator dependencies dynamically
1437 * before/after accessing a device. Returns the return value from
1438 * _add_initiator_dep().
1439 *
1440 * XXX Keep a usecount in the clockdomain code
1441 */
1442int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
1443 struct omap_hwmod *init_oh)
1444{
1445 return _add_initiator_dep(oh, init_oh);
1446}
1447
1448/*
1449 * XXX what about functions for drivers to save/restore ocp_sysconfig
1450 * for context save/restore operations?
1451 */
1452
1453/**
1454 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
1455 * @oh: struct omap_hwmod *
1456 * @init_oh: struct omap_hwmod * (initiator)
1457 *
1458 * Remove a sleep dependency between the initiator @init_oh and @oh.
1459 * Intended to be called by DSP/Bridge code via platform_data for the
1460 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1461 * code needs to add/del initiator dependencies dynamically
1462 * before/after accessing a device. Returns the return value from
1463 * _del_initiator_dep().
1464 *
1465 * XXX Keep a usecount in the clockdomain code
1466 */
1467int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
1468 struct omap_hwmod *init_oh)
1469{
1470 return _del_initiator_dep(oh, init_oh);
1471}
1472
1473/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001474 * omap_hwmod_enable_wakeup - allow device to wake up the system
1475 * @oh: struct omap_hwmod *
1476 *
1477 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
1478 * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
1479 * registers to cause the PRCM to receive wakeup events from the
1480 * module. Does not set any wakeup routing registers beyond this
1481 * point - if the module is to wake up any other module or subsystem,
1482 * that must be set separately. Called by omap_device code. Returns
1483 * -EINVAL on error or 0 upon success.
1484 */
1485int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
1486{
1487 if (!oh->sysconfig ||
1488 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
1489 return -EINVAL;
1490
1491 mutex_lock(&omap_hwmod_mutex);
1492 _enable_wakeup(oh);
1493 mutex_unlock(&omap_hwmod_mutex);
1494
1495 return 0;
1496}
1497
1498/**
1499 * omap_hwmod_disable_wakeup - prevent device from waking the system
1500 * @oh: struct omap_hwmod *
1501 *
1502 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
1503 * from sending wakeups to the PRCM. Eventually this should clear
1504 * PRCM wakeup registers to cause the PRCM to ignore wakeup events
1505 * from the module. Does not set any wakeup routing registers beyond
1506 * this point - if the module is to wake up any other module or
1507 * subsystem, that must be set separately. Called by omap_device
1508 * code. Returns -EINVAL on error or 0 upon success.
1509 */
1510int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
1511{
1512 if (!oh->sysconfig ||
1513 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
1514 return -EINVAL;
1515
1516 mutex_lock(&omap_hwmod_mutex);
1517 _disable_wakeup(oh);
1518 mutex_unlock(&omap_hwmod_mutex);
1519
1520 return 0;
1521}