ARM: OMAP2+: hwmod: use init-time function ptrs for enable/disable module
[linux-2.6.git] / arch / arm / mach-omap2 / omap_hwmod.c
blob634a79836c646cebded12c005633afbefe0eaa76
1 /*
2 * omap_hwmod implementation for OMAP2/3/4
4 * Copyright (C) 2009-2011 Nokia Corporation
5 * Copyright (C) 2011-2012 Texas Instruments, Inc.
7 * Paul Walmsley, BenoƮt Cousson, Kevin Hilman
9 * Created in collaboration with (alphabetical order): Thara Gopinath,
10 * Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari Poussa, Anand
11 * Sawant, Santosh Shilimkar, Richard Woodruff
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
17 * Introduction
18 * ------------
19 * One way to view an OMAP SoC is as a collection of largely unrelated
20 * IP blocks connected by interconnects. The IP blocks include
21 * devices such as ARM processors, audio serial interfaces, UARTs,
22 * etc. Some of these devices, like the DSP, are created by TI;
23 * others, like the SGX, largely originate from external vendors. In
24 * TI's documentation, on-chip devices are referred to as "OMAP
25 * modules." Some of these IP blocks are identical across several
26 * OMAP versions. Others are revised frequently.
28 * These OMAP modules are tied together by various interconnects.
29 * Most of the address and data flow between modules is via OCP-based
30 * interconnects such as the L3 and L4 buses; but there are other
31 * interconnects that distribute the hardware clock tree, handle idle
32 * and reset signaling, supply power, and connect the modules to
33 * various pads or balls on the OMAP package.
35 * OMAP hwmod provides a consistent way to describe the on-chip
36 * hardware blocks and their integration into the rest of the chip.
37 * This description can be automatically generated from the TI
38 * hardware database. OMAP hwmod provides a standard, consistent API
39 * to reset, enable, idle, and disable these hardware blocks. And
40 * hwmod provides a way for other core code, such as the Linux device
41 * code or the OMAP power management and address space mapping code,
42 * to query the hardware database.
44 * Using hwmod
45 * -----------
46 * Drivers won't call hwmod functions directly. That is done by the
47 * omap_device code, and in rare occasions, by custom integration code
48 * in arch/arm/ *omap*. The omap_device code includes functions to
49 * build a struct platform_device using omap_hwmod data, and that is
50 * currently how hwmod data is communicated to drivers and to the
51 * Linux driver model. Most drivers will call omap_hwmod functions only
52 * indirectly, via pm_runtime*() functions.
54 * From a layering perspective, here is where the OMAP hwmod code
55 * fits into the kernel software stack:
57 * +-------------------------------+
58 * | Device driver code |
59 * | (e.g., drivers/) |
60 * +-------------------------------+
61 * | Linux driver model |
62 * | (platform_device / |
63 * | platform_driver data/code) |
64 * +-------------------------------+
65 * | OMAP core-driver integration |
66 * |(arch/arm/mach-omap2/devices.c)|
67 * +-------------------------------+
68 * | omap_device code |
69 * | (../plat-omap/omap_device.c) |
70 * +-------------------------------+
71 * ----> | omap_hwmod code/data | <-----
72 * | (../mach-omap2/omap_hwmod*) |
73 * +-------------------------------+
74 * | OMAP clock/PRCM/register fns |
75 * | (__raw_{read,write}l, clk*) |
76 * +-------------------------------+
78 * Device drivers should not contain any OMAP-specific code or data in
79 * them. They should only contain code to operate the IP block that
80 * the driver is responsible for. This is because these IP blocks can
81 * also appear in other SoCs, either from TI (such as DaVinci) or from
82 * other manufacturers; and drivers should be reusable across other
83 * platforms.
85 * The OMAP hwmod code also will attempt to reset and idle all on-chip
86 * devices upon boot. The goal here is for the kernel to be
87 * completely self-reliant and independent from bootloaders. This is
88 * to ensure a repeatable configuration, both to ensure consistent
89 * runtime behavior, and to make it easier for others to reproduce
90 * bugs.
92 * OMAP module activity states
93 * ---------------------------
94 * The hwmod code considers modules to be in one of several activity
95 * states. IP blocks start out in an UNKNOWN state, then once they
96 * are registered via the hwmod code, proceed to the REGISTERED state.
97 * Once their clock names are resolved to clock pointers, the module
98 * enters the CLKS_INITED state; and finally, once the module has been
99 * reset and the integration registers programmed, the INITIALIZED state
100 * is entered. The hwmod code will then place the module into either
101 * the IDLE state to save power, or in the case of a critical system
102 * module, the ENABLED state.
104 * OMAP core integration code can then call omap_hwmod*() functions
105 * directly to move the module between the IDLE, ENABLED, and DISABLED
106 * states, as needed. This is done during both the PM idle loop, and
107 * in the OMAP core integration code's implementation of the PM runtime
108 * functions.
110 * References
111 * ----------
112 * This is a partial list.
113 * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
114 * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
115 * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
116 * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
117 * - Open Core Protocol Specification 2.2
119 * To do:
120 * - handle IO mapping
121 * - bus throughput & module latency measurement code
123 * XXX add tests at the beginning of each function to ensure the hwmod is
124 * in the appropriate state
125 * XXX error return values should be checked to ensure that they are
126 * appropriate
128 #undef DEBUG
130 #include <linux/kernel.h>
131 #include <linux/errno.h>
132 #include <linux/io.h>
133 #include <linux/clk.h>
134 #include <linux/delay.h>
135 #include <linux/err.h>
136 #include <linux/list.h>
137 #include <linux/mutex.h>
138 #include <linux/spinlock.h>
139 #include <linux/slab.h>
140 #include <linux/bootmem.h>
142 #include "common.h"
143 #include <plat/cpu.h>
144 #include "clockdomain.h"
145 #include "powerdomain.h"
146 #include <plat/clock.h>
147 #include <plat/omap_hwmod.h>
148 #include <plat/prcm.h>
150 #include "cm2xxx_3xxx.h"
151 #include "cminst44xx.h"
152 #include "prm2xxx_3xxx.h"
153 #include "prm44xx.h"
154 #include "prminst44xx.h"
155 #include "mux.h"
157 /* Maximum microseconds to wait for OMAP module to softreset */
158 #define MAX_MODULE_SOFTRESET_WAIT 10000
160 /* Name of the OMAP hwmod for the MPU */
161 #define MPU_INITIATOR_NAME "mpu"
164 * Number of struct omap_hwmod_link records per struct
165 * omap_hwmod_ocp_if record (master->slave and slave->master)
167 #define LINKS_PER_OCP_IF 2
170 * struct omap_hwmod_soc_ops - fn ptrs for some SoC-specific operations
171 * @enable_module: function to enable a module (via MODULEMODE)
172 * @disable_module: function to disable a module (via MODULEMODE)
174 * XXX Eventually this functionality will be hidden inside the PRM/CM
175 * device drivers. Until then, this should avoid huge blocks of cpu_is_*()
176 * conditionals in this code.
178 struct omap_hwmod_soc_ops {
179 void (*enable_module)(struct omap_hwmod *oh);
180 int (*disable_module)(struct omap_hwmod *oh);
183 /* soc_ops: adapts the omap_hwmod code to the currently-booted SoC */
184 static struct omap_hwmod_soc_ops soc_ops;
186 /* omap_hwmod_list contains all registered struct omap_hwmods */
187 static LIST_HEAD(omap_hwmod_list);
189 /* mpu_oh: used to add/remove MPU initiator from sleepdep list */
190 static struct omap_hwmod *mpu_oh;
193 * linkspace: ptr to a buffer that struct omap_hwmod_link records are
194 * allocated from - used to reduce the number of small memory
195 * allocations, which has a significant impact on performance
197 static struct omap_hwmod_link *linkspace;
200 * free_ls, max_ls: array indexes into linkspace; representing the
201 * next free struct omap_hwmod_link index, and the maximum number of
202 * struct omap_hwmod_link records allocated (respectively)
204 static unsigned short free_ls, max_ls, ls_supp;
206 /* inited: set to true once the hwmod code is initialized */
207 static bool inited;
209 /* Private functions */
212 * _fetch_next_ocp_if - return the next OCP interface in a list
213 * @p: ptr to a ptr to the list_head inside the ocp_if to return
214 * @i: pointer to the index of the element pointed to by @p in the list
216 * Return a pointer to the struct omap_hwmod_ocp_if record
217 * containing the struct list_head pointed to by @p, and increment
218 * @p such that a future call to this routine will return the next
219 * record.
221 static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
222 int *i)
224 struct omap_hwmod_ocp_if *oi;
226 oi = list_entry(*p, struct omap_hwmod_link, node)->ocp_if;
227 *p = (*p)->next;
229 *i = *i + 1;
231 return oi;
235 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
236 * @oh: struct omap_hwmod *
238 * Load the current value of the hwmod OCP_SYSCONFIG register into the
239 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
240 * OCP_SYSCONFIG register or 0 upon success.
242 static int _update_sysc_cache(struct omap_hwmod *oh)
244 if (!oh->class->sysc) {
245 WARN(1, "omap_hwmod: %s: cannot read OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
246 return -EINVAL;
249 /* XXX ensure module interface clock is up */
251 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
253 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
254 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
256 return 0;
260 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
261 * @v: OCP_SYSCONFIG value to write
262 * @oh: struct omap_hwmod *
264 * Write @v into the module class' OCP_SYSCONFIG register, if it has
265 * one. No return value.
267 static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
269 if (!oh->class->sysc) {
270 WARN(1, "omap_hwmod: %s: cannot write OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
271 return;
274 /* XXX ensure module interface clock is up */
276 /* Module might have lost context, always update cache and register */
277 oh->_sysc_cache = v;
278 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
282 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
283 * @oh: struct omap_hwmod *
284 * @standbymode: MIDLEMODE field bits
285 * @v: pointer to register contents to modify
287 * Update the master standby mode bits in @v to be @standbymode for
288 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
289 * upon error or 0 upon success.
291 static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
292 u32 *v)
294 u32 mstandby_mask;
295 u8 mstandby_shift;
297 if (!oh->class->sysc ||
298 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
299 return -EINVAL;
301 if (!oh->class->sysc->sysc_fields) {
302 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
303 return -EINVAL;
306 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
307 mstandby_mask = (0x3 << mstandby_shift);
309 *v &= ~mstandby_mask;
310 *v |= __ffs(standbymode) << mstandby_shift;
312 return 0;
316 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
317 * @oh: struct omap_hwmod *
318 * @idlemode: SIDLEMODE field bits
319 * @v: pointer to register contents to modify
321 * Update the slave idle mode bits in @v to be @idlemode for the @oh
322 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
323 * or 0 upon success.
325 static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
327 u32 sidle_mask;
328 u8 sidle_shift;
330 if (!oh->class->sysc ||
331 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
332 return -EINVAL;
334 if (!oh->class->sysc->sysc_fields) {
335 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
336 return -EINVAL;
339 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
340 sidle_mask = (0x3 << sidle_shift);
342 *v &= ~sidle_mask;
343 *v |= __ffs(idlemode) << sidle_shift;
345 return 0;
349 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
350 * @oh: struct omap_hwmod *
351 * @clockact: CLOCKACTIVITY field bits
352 * @v: pointer to register contents to modify
354 * Update the clockactivity mode bits in @v to be @clockact for the
355 * @oh hwmod. Used for additional powersaving on some modules. Does
356 * not write to the hardware. Returns -EINVAL upon error or 0 upon
357 * success.
359 static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
361 u32 clkact_mask;
362 u8 clkact_shift;
364 if (!oh->class->sysc ||
365 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
366 return -EINVAL;
368 if (!oh->class->sysc->sysc_fields) {
369 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
370 return -EINVAL;
373 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
374 clkact_mask = (0x3 << clkact_shift);
376 *v &= ~clkact_mask;
377 *v |= clockact << clkact_shift;
379 return 0;
383 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
384 * @oh: struct omap_hwmod *
385 * @v: pointer to register contents to modify
387 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
388 * error or 0 upon success.
390 static int _set_softreset(struct omap_hwmod *oh, u32 *v)
392 u32 softrst_mask;
394 if (!oh->class->sysc ||
395 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
396 return -EINVAL;
398 if (!oh->class->sysc->sysc_fields) {
399 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
400 return -EINVAL;
403 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
405 *v |= softrst_mask;
407 return 0;
411 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
412 * @oh: struct omap_hwmod *
413 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
414 * @v: pointer to register contents to modify
416 * Update the module autoidle bit in @v to be @autoidle for the @oh
417 * hwmod. The autoidle bit controls whether the module can gate
418 * internal clocks automatically when it isn't doing anything; the
419 * exact function of this bit varies on a per-module basis. This
420 * function does not write to the hardware. Returns -EINVAL upon
421 * error or 0 upon success.
423 static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
424 u32 *v)
426 u32 autoidle_mask;
427 u8 autoidle_shift;
429 if (!oh->class->sysc ||
430 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
431 return -EINVAL;
433 if (!oh->class->sysc->sysc_fields) {
434 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
435 return -EINVAL;
438 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
439 autoidle_mask = (0x1 << autoidle_shift);
441 *v &= ~autoidle_mask;
442 *v |= autoidle << autoidle_shift;
444 return 0;
448 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
449 * @oh: struct omap_hwmod *
450 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
452 * Set or clear the I/O pad wakeup flag in the mux entries for the
453 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
454 * in memory. If the hwmod is currently idled, and the new idle
455 * values don't match the previous ones, this function will also
456 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
457 * currently idled, this function won't touch the hardware: the new
458 * mux settings are written to the SCM PADCTRL registers when the
459 * hwmod is idled. No return value.
461 static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
463 struct omap_device_pad *pad;
464 bool change = false;
465 u16 prev_idle;
466 int j;
468 if (!oh->mux || !oh->mux->enabled)
469 return;
471 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
472 pad = oh->mux->pads_dynamic[j];
474 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
475 continue;
477 prev_idle = pad->idle;
479 if (set_wake)
480 pad->idle |= OMAP_WAKEUP_EN;
481 else
482 pad->idle &= ~OMAP_WAKEUP_EN;
484 if (prev_idle != pad->idle)
485 change = true;
488 if (change && oh->_state == _HWMOD_STATE_IDLE)
489 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
493 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
494 * @oh: struct omap_hwmod *
496 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
497 * upon error or 0 upon success.
499 static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
501 if (!oh->class->sysc ||
502 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
503 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
504 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
505 return -EINVAL;
507 if (!oh->class->sysc->sysc_fields) {
508 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
509 return -EINVAL;
512 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
513 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
515 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
516 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
517 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
518 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
520 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
522 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
524 return 0;
528 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
529 * @oh: struct omap_hwmod *
531 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
532 * upon error or 0 upon success.
534 static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
536 if (!oh->class->sysc ||
537 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
538 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
539 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
540 return -EINVAL;
542 if (!oh->class->sysc->sysc_fields) {
543 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
544 return -EINVAL;
547 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
548 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
550 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
551 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
552 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
553 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
555 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
557 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
559 return 0;
563 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
564 * @oh: struct omap_hwmod *
566 * Prevent the hardware module @oh from entering idle while the
567 * hardare module initiator @init_oh is active. Useful when a module
568 * will be accessed by a particular initiator (e.g., if a module will
569 * be accessed by the IVA, there should be a sleepdep between the IVA
570 * initiator and the module). Only applies to modules in smart-idle
571 * mode. If the clockdomain is marked as not needing autodeps, return
572 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
573 * passes along clkdm_add_sleepdep() value upon success.
575 static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
577 if (!oh->_clk)
578 return -EINVAL;
580 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
581 return 0;
583 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
587 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
588 * @oh: struct omap_hwmod *
590 * Allow the hardware module @oh to enter idle while the hardare
591 * module initiator @init_oh is active. Useful when a module will not
592 * be accessed by a particular initiator (e.g., if a module will not
593 * be accessed by the IVA, there should be no sleepdep between the IVA
594 * initiator and the module). Only applies to modules in smart-idle
595 * mode. If the clockdomain is marked as not needing autodeps, return
596 * 0 without doing anything. Returns -EINVAL upon error or passes
597 * along clkdm_del_sleepdep() value upon success.
599 static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
601 if (!oh->_clk)
602 return -EINVAL;
604 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
605 return 0;
607 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
611 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
612 * @oh: struct omap_hwmod *
614 * Called from _init_clocks(). Populates the @oh _clk (main
615 * functional clock pointer) if a main_clk is present. Returns 0 on
616 * success or -EINVAL on error.
618 static int _init_main_clk(struct omap_hwmod *oh)
620 int ret = 0;
622 if (!oh->main_clk)
623 return 0;
625 oh->_clk = omap_clk_get_by_name(oh->main_clk);
626 if (!oh->_clk) {
627 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
628 oh->name, oh->main_clk);
629 return -EINVAL;
632 if (!oh->_clk->clkdm)
633 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
634 oh->main_clk, oh->_clk->name);
636 return ret;
640 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
641 * @oh: struct omap_hwmod *
643 * Called from _init_clocks(). Populates the @oh OCP slave interface
644 * clock pointers. Returns 0 on success or -EINVAL on error.
646 static int _init_interface_clks(struct omap_hwmod *oh)
648 struct omap_hwmod_ocp_if *os;
649 struct list_head *p;
650 struct clk *c;
651 int i = 0;
652 int ret = 0;
654 p = oh->slave_ports.next;
656 while (i < oh->slaves_cnt) {
657 os = _fetch_next_ocp_if(&p, &i);
658 if (!os->clk)
659 continue;
661 c = omap_clk_get_by_name(os->clk);
662 if (!c) {
663 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
664 oh->name, os->clk);
665 ret = -EINVAL;
667 os->_clk = c;
670 return ret;
674 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
675 * @oh: struct omap_hwmod *
677 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
678 * clock pointers. Returns 0 on success or -EINVAL on error.
680 static int _init_opt_clks(struct omap_hwmod *oh)
682 struct omap_hwmod_opt_clk *oc;
683 struct clk *c;
684 int i;
685 int ret = 0;
687 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
688 c = omap_clk_get_by_name(oc->clk);
689 if (!c) {
690 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
691 oh->name, oc->clk);
692 ret = -EINVAL;
694 oc->_clk = c;
697 return ret;
701 * _enable_clocks - enable hwmod main clock and interface clocks
702 * @oh: struct omap_hwmod *
704 * Enables all clocks necessary for register reads and writes to succeed
705 * on the hwmod @oh. Returns 0.
707 static int _enable_clocks(struct omap_hwmod *oh)
709 struct omap_hwmod_ocp_if *os;
710 struct list_head *p;
711 int i = 0;
713 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
715 if (oh->_clk)
716 clk_enable(oh->_clk);
718 p = oh->slave_ports.next;
720 while (i < oh->slaves_cnt) {
721 os = _fetch_next_ocp_if(&p, &i);
723 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
724 clk_enable(os->_clk);
727 /* The opt clocks are controlled by the device driver. */
729 return 0;
733 * _disable_clocks - disable hwmod main clock and interface clocks
734 * @oh: struct omap_hwmod *
736 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
738 static int _disable_clocks(struct omap_hwmod *oh)
740 struct omap_hwmod_ocp_if *os;
741 struct list_head *p;
742 int i = 0;
744 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
746 if (oh->_clk)
747 clk_disable(oh->_clk);
749 p = oh->slave_ports.next;
751 while (i < oh->slaves_cnt) {
752 os = _fetch_next_ocp_if(&p, &i);
754 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
755 clk_disable(os->_clk);
758 /* The opt clocks are controlled by the device driver. */
760 return 0;
763 static void _enable_optional_clocks(struct omap_hwmod *oh)
765 struct omap_hwmod_opt_clk *oc;
766 int i;
768 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
770 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
771 if (oc->_clk) {
772 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
773 oc->_clk->name);
774 clk_enable(oc->_clk);
778 static void _disable_optional_clocks(struct omap_hwmod *oh)
780 struct omap_hwmod_opt_clk *oc;
781 int i;
783 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
785 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
786 if (oc->_clk) {
787 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
788 oc->_clk->name);
789 clk_disable(oc->_clk);
794 * _omap4_enable_module - enable CLKCTRL modulemode on OMAP4
795 * @oh: struct omap_hwmod *
797 * Enables the PRCM module mode related to the hwmod @oh.
798 * No return value.
800 static void _omap4_enable_module(struct omap_hwmod *oh)
802 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
803 return;
805 pr_debug("omap_hwmod: %s: %s: %d\n",
806 oh->name, __func__, oh->prcm.omap4.modulemode);
808 omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
809 oh->clkdm->prcm_partition,
810 oh->clkdm->cm_inst,
811 oh->clkdm->clkdm_offs,
812 oh->prcm.omap4.clkctrl_offs);
816 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
817 * @oh: struct omap_hwmod *
819 * Wait for a module @oh to enter slave idle. Returns 0 if the module
820 * does not have an IDLEST bit or if the module successfully enters
821 * slave idle; otherwise, pass along the return value of the
822 * appropriate *_cm*_wait_module_idle() function.
824 static int _omap4_wait_target_disable(struct omap_hwmod *oh)
826 if (!cpu_is_omap44xx())
827 return 0;
829 if (!oh)
830 return -EINVAL;
832 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
833 return 0;
835 if (oh->flags & HWMOD_NO_IDLEST)
836 return 0;
838 return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
839 oh->clkdm->cm_inst,
840 oh->clkdm->clkdm_offs,
841 oh->prcm.omap4.clkctrl_offs);
845 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
846 * @oh: struct omap_hwmod *oh
848 * Count and return the number of MPU IRQs associated with the hwmod
849 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
850 * NULL.
852 static int _count_mpu_irqs(struct omap_hwmod *oh)
854 struct omap_hwmod_irq_info *ohii;
855 int i = 0;
857 if (!oh || !oh->mpu_irqs)
858 return 0;
860 do {
861 ohii = &oh->mpu_irqs[i++];
862 } while (ohii->irq != -1);
864 return i-1;
868 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
869 * @oh: struct omap_hwmod *oh
871 * Count and return the number of SDMA request lines associated with
872 * the hwmod @oh. Used to allocate struct resource data. Returns 0
873 * if @oh is NULL.
875 static int _count_sdma_reqs(struct omap_hwmod *oh)
877 struct omap_hwmod_dma_info *ohdi;
878 int i = 0;
880 if (!oh || !oh->sdma_reqs)
881 return 0;
883 do {
884 ohdi = &oh->sdma_reqs[i++];
885 } while (ohdi->dma_req != -1);
887 return i-1;
891 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
892 * @oh: struct omap_hwmod *oh
894 * Count and return the number of address space ranges associated with
895 * the hwmod @oh. Used to allocate struct resource data. Returns 0
896 * if @oh is NULL.
898 static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
900 struct omap_hwmod_addr_space *mem;
901 int i = 0;
903 if (!os || !os->addr)
904 return 0;
906 do {
907 mem = &os->addr[i++];
908 } while (mem->pa_start != mem->pa_end);
910 return i-1;
914 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
915 * @oh: struct omap_hwmod * to operate on
916 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
917 * @irq: pointer to an unsigned int to store the MPU IRQ number to
919 * Retrieve a MPU hardware IRQ line number named by @name associated
920 * with the IP block pointed to by @oh. The IRQ number will be filled
921 * into the address pointed to by @dma. When @name is non-null, the
922 * IRQ line number associated with the named entry will be returned.
923 * If @name is null, the first matching entry will be returned. Data
924 * order is not meaningful in hwmod data, so callers are strongly
925 * encouraged to use a non-null @name whenever possible to avoid
926 * unpredictable effects if hwmod data is later added that causes data
927 * ordering to change. Returns 0 upon success or a negative error
928 * code upon error.
930 static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
931 unsigned int *irq)
933 int i;
934 bool found = false;
936 if (!oh->mpu_irqs)
937 return -ENOENT;
939 i = 0;
940 while (oh->mpu_irqs[i].irq != -1) {
941 if (name == oh->mpu_irqs[i].name ||
942 !strcmp(name, oh->mpu_irqs[i].name)) {
943 found = true;
944 break;
946 i++;
949 if (!found)
950 return -ENOENT;
952 *irq = oh->mpu_irqs[i].irq;
954 return 0;
958 * _get_sdma_req_by_name - fetch SDMA request line ID by name
959 * @oh: struct omap_hwmod * to operate on
960 * @name: pointer to the name of the SDMA request line to fetch (optional)
961 * @dma: pointer to an unsigned int to store the request line ID to
963 * Retrieve an SDMA request line ID named by @name on the IP block
964 * pointed to by @oh. The ID will be filled into the address pointed
965 * to by @dma. When @name is non-null, the request line ID associated
966 * with the named entry will be returned. If @name is null, the first
967 * matching entry will be returned. Data order is not meaningful in
968 * hwmod data, so callers are strongly encouraged to use a non-null
969 * @name whenever possible to avoid unpredictable effects if hwmod
970 * data is later added that causes data ordering to change. Returns 0
971 * upon success or a negative error code upon error.
973 static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
974 unsigned int *dma)
976 int i;
977 bool found = false;
979 if (!oh->sdma_reqs)
980 return -ENOENT;
982 i = 0;
983 while (oh->sdma_reqs[i].dma_req != -1) {
984 if (name == oh->sdma_reqs[i].name ||
985 !strcmp(name, oh->sdma_reqs[i].name)) {
986 found = true;
987 break;
989 i++;
992 if (!found)
993 return -ENOENT;
995 *dma = oh->sdma_reqs[i].dma_req;
997 return 0;
1001 * _get_addr_space_by_name - fetch address space start & end by name
1002 * @oh: struct omap_hwmod * to operate on
1003 * @name: pointer to the name of the address space to fetch (optional)
1004 * @pa_start: pointer to a u32 to store the starting address to
1005 * @pa_end: pointer to a u32 to store the ending address to
1007 * Retrieve address space start and end addresses for the IP block
1008 * pointed to by @oh. The data will be filled into the addresses
1009 * pointed to by @pa_start and @pa_end. When @name is non-null, the
1010 * address space data associated with the named entry will be
1011 * returned. If @name is null, the first matching entry will be
1012 * returned. Data order is not meaningful in hwmod data, so callers
1013 * are strongly encouraged to use a non-null @name whenever possible
1014 * to avoid unpredictable effects if hwmod data is later added that
1015 * causes data ordering to change. Returns 0 upon success or a
1016 * negative error code upon error.
1018 static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1019 u32 *pa_start, u32 *pa_end)
1021 int i, j;
1022 struct omap_hwmod_ocp_if *os;
1023 struct list_head *p = NULL;
1024 bool found = false;
1026 p = oh->slave_ports.next;
1028 i = 0;
1029 while (i < oh->slaves_cnt) {
1030 os = _fetch_next_ocp_if(&p, &i);
1032 if (!os->addr)
1033 return -ENOENT;
1035 j = 0;
1036 while (os->addr[j].pa_start != os->addr[j].pa_end) {
1037 if (name == os->addr[j].name ||
1038 !strcmp(name, os->addr[j].name)) {
1039 found = true;
1040 break;
1042 j++;
1045 if (found)
1046 break;
1049 if (!found)
1050 return -ENOENT;
1052 *pa_start = os->addr[j].pa_start;
1053 *pa_end = os->addr[j].pa_end;
1055 return 0;
1059 * _save_mpu_port_index - find and save the index to @oh's MPU port
1060 * @oh: struct omap_hwmod *
1062 * Determines the array index of the OCP slave port that the MPU uses
1063 * to address the device, and saves it into the struct omap_hwmod.
1064 * Intended to be called during hwmod registration only. No return
1065 * value.
1067 static void __init _save_mpu_port_index(struct omap_hwmod *oh)
1069 struct omap_hwmod_ocp_if *os = NULL;
1070 struct list_head *p;
1071 int i = 0;
1073 if (!oh)
1074 return;
1076 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1078 p = oh->slave_ports.next;
1080 while (i < oh->slaves_cnt) {
1081 os = _fetch_next_ocp_if(&p, &i);
1082 if (os->user & OCP_USER_MPU) {
1083 oh->_mpu_port = os;
1084 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
1085 break;
1089 return;
1093 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1094 * @oh: struct omap_hwmod *
1096 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1097 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1098 * communicate with the IP block. This interface need not be directly
1099 * connected to the MPU (and almost certainly is not), but is directly
1100 * connected to the IP block represented by @oh. Returns a pointer
1101 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1102 * error or if there does not appear to be a path from the MPU to this
1103 * IP block.
1105 static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1107 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1108 return NULL;
1110 return oh->_mpu_port;
1114 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
1115 * @oh: struct omap_hwmod *
1117 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1118 * the register target MPU address space; or returns NULL upon error.
1120 static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
1122 struct omap_hwmod_ocp_if *os;
1123 struct omap_hwmod_addr_space *mem;
1124 int found = 0, i = 0;
1126 os = _find_mpu_rt_port(oh);
1127 if (!os || !os->addr)
1128 return NULL;
1130 do {
1131 mem = &os->addr[i++];
1132 if (mem->flags & ADDR_TYPE_RT)
1133 found = 1;
1134 } while (!found && mem->pa_start != mem->pa_end);
1136 return (found) ? mem : NULL;
1140 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
1141 * @oh: struct omap_hwmod *
1143 * If module is marked as SWSUP_SIDLE, force the module out of slave
1144 * idle; otherwise, configure it for smart-idle. If module is marked
1145 * as SWSUP_MSUSPEND, force the module out of master standby;
1146 * otherwise, configure it for smart-standby. No return value.
1148 static void _enable_sysc(struct omap_hwmod *oh)
1150 u8 idlemode, sf;
1151 u32 v;
1153 if (!oh->class->sysc)
1154 return;
1156 v = oh->_sysc_cache;
1157 sf = oh->class->sysc->sysc_flags;
1159 if (sf & SYSC_HAS_SIDLEMODE) {
1160 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1161 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
1162 _set_slave_idlemode(oh, idlemode, &v);
1165 if (sf & SYSC_HAS_MIDLEMODE) {
1166 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1167 idlemode = HWMOD_IDLEMODE_NO;
1168 } else {
1169 if (sf & SYSC_HAS_ENAWAKEUP)
1170 _enable_wakeup(oh, &v);
1171 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1172 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1173 else
1174 idlemode = HWMOD_IDLEMODE_SMART;
1176 _set_master_standbymode(oh, idlemode, &v);
1180 * XXX The clock framework should handle this, by
1181 * calling into this code. But this must wait until the
1182 * clock structures are tagged with omap_hwmod entries
1184 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1185 (sf & SYSC_HAS_CLOCKACTIVITY))
1186 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
1188 /* If slave is in SMARTIDLE, also enable wakeup */
1189 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1190 _enable_wakeup(oh, &v);
1192 _write_sysconfig(v, oh);
1195 * Set the autoidle bit only after setting the smartidle bit
1196 * Setting this will not have any impact on the other modules.
1198 if (sf & SYSC_HAS_AUTOIDLE) {
1199 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1200 0 : 1;
1201 _set_module_autoidle(oh, idlemode, &v);
1202 _write_sysconfig(v, oh);
1207 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
1208 * @oh: struct omap_hwmod *
1210 * If module is marked as SWSUP_SIDLE, force the module into slave
1211 * idle; otherwise, configure it for smart-idle. If module is marked
1212 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1213 * configure it for smart-standby. No return value.
1215 static void _idle_sysc(struct omap_hwmod *oh)
1217 u8 idlemode, sf;
1218 u32 v;
1220 if (!oh->class->sysc)
1221 return;
1223 v = oh->_sysc_cache;
1224 sf = oh->class->sysc->sysc_flags;
1226 if (sf & SYSC_HAS_SIDLEMODE) {
1227 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1228 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
1229 _set_slave_idlemode(oh, idlemode, &v);
1232 if (sf & SYSC_HAS_MIDLEMODE) {
1233 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1234 idlemode = HWMOD_IDLEMODE_FORCE;
1235 } else {
1236 if (sf & SYSC_HAS_ENAWAKEUP)
1237 _enable_wakeup(oh, &v);
1238 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1239 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1240 else
1241 idlemode = HWMOD_IDLEMODE_SMART;
1243 _set_master_standbymode(oh, idlemode, &v);
1246 /* If slave is in SMARTIDLE, also enable wakeup */
1247 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1248 _enable_wakeup(oh, &v);
1250 _write_sysconfig(v, oh);
1254 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
1255 * @oh: struct omap_hwmod *
1257 * Force the module into slave idle and master suspend. No return
1258 * value.
1260 static void _shutdown_sysc(struct omap_hwmod *oh)
1262 u32 v;
1263 u8 sf;
1265 if (!oh->class->sysc)
1266 return;
1268 v = oh->_sysc_cache;
1269 sf = oh->class->sysc->sysc_flags;
1271 if (sf & SYSC_HAS_SIDLEMODE)
1272 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1274 if (sf & SYSC_HAS_MIDLEMODE)
1275 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1277 if (sf & SYSC_HAS_AUTOIDLE)
1278 _set_module_autoidle(oh, 1, &v);
1280 _write_sysconfig(v, oh);
1284 * _lookup - find an omap_hwmod by name
1285 * @name: find an omap_hwmod by name
1287 * Return a pointer to an omap_hwmod by name, or NULL if not found.
1289 static struct omap_hwmod *_lookup(const char *name)
1291 struct omap_hwmod *oh, *temp_oh;
1293 oh = NULL;
1295 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1296 if (!strcmp(name, temp_oh->name)) {
1297 oh = temp_oh;
1298 break;
1302 return oh;
1305 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1306 * @oh: struct omap_hwmod *
1308 * Convert a clockdomain name stored in a struct omap_hwmod into a
1309 * clockdomain pointer, and save it into the struct omap_hwmod.
1310 * return -EINVAL if clkdm_name does not exist or if the lookup failed.
1312 static int _init_clkdm(struct omap_hwmod *oh)
1314 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1315 return 0;
1317 if (!oh->clkdm_name) {
1318 pr_warning("omap_hwmod: %s: no clkdm_name\n", oh->name);
1319 return -EINVAL;
1322 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1323 if (!oh->clkdm) {
1324 pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
1325 oh->name, oh->clkdm_name);
1326 return -EINVAL;
1329 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1330 oh->name, oh->clkdm_name);
1332 return 0;
1336 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1337 * well the clockdomain.
1338 * @oh: struct omap_hwmod *
1339 * @data: not used; pass NULL
1341 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
1342 * Resolves all clock names embedded in the hwmod. Returns 0 on
1343 * success, or a negative error code on failure.
1345 static int _init_clocks(struct omap_hwmod *oh, void *data)
1347 int ret = 0;
1349 if (oh->_state != _HWMOD_STATE_REGISTERED)
1350 return 0;
1352 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1354 ret |= _init_main_clk(oh);
1355 ret |= _init_interface_clks(oh);
1356 ret |= _init_opt_clks(oh);
1357 ret |= _init_clkdm(oh);
1359 if (!ret)
1360 oh->_state = _HWMOD_STATE_CLKS_INITED;
1361 else
1362 pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
1364 return ret;
1368 * _wait_target_ready - wait for a module to leave slave idle
1369 * @oh: struct omap_hwmod *
1371 * Wait for a module @oh to leave slave idle. Returns 0 if the module
1372 * does not have an IDLEST bit or if the module successfully leaves
1373 * slave idle; otherwise, pass along the return value of the
1374 * appropriate *_cm*_wait_module_ready() function.
1376 static int _wait_target_ready(struct omap_hwmod *oh)
1378 struct omap_hwmod_ocp_if *os;
1379 int ret;
1381 if (!oh)
1382 return -EINVAL;
1384 if (oh->flags & HWMOD_NO_IDLEST)
1385 return 0;
1387 os = _find_mpu_rt_port(oh);
1388 if (!os)
1389 return 0;
1391 /* XXX check module SIDLEMODE */
1393 /* XXX check clock enable states */
1395 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1396 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
1397 oh->prcm.omap2.idlest_reg_id,
1398 oh->prcm.omap2.idlest_idle_bit);
1399 } else if (cpu_is_omap44xx()) {
1400 if (!oh->clkdm)
1401 return -EINVAL;
1403 ret = omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
1404 oh->clkdm->cm_inst,
1405 oh->clkdm->clkdm_offs,
1406 oh->prcm.omap4.clkctrl_offs);
1407 } else {
1408 BUG();
1411 return ret;
1415 * _lookup_hardreset - fill register bit info for this hwmod/reset line
1416 * @oh: struct omap_hwmod *
1417 * @name: name of the reset line in the context of this hwmod
1418 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
1420 * Return the bit position of the reset line that match the
1421 * input name. Return -ENOENT if not found.
1423 static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1424 struct omap_hwmod_rst_info *ohri)
1426 int i;
1428 for (i = 0; i < oh->rst_lines_cnt; i++) {
1429 const char *rst_line = oh->rst_lines[i].name;
1430 if (!strcmp(rst_line, name)) {
1431 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1432 ohri->st_shift = oh->rst_lines[i].st_shift;
1433 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1434 oh->name, __func__, rst_line, ohri->rst_shift,
1435 ohri->st_shift);
1437 return 0;
1441 return -ENOENT;
1445 * _assert_hardreset - assert the HW reset line of submodules
1446 * contained in the hwmod module.
1447 * @oh: struct omap_hwmod *
1448 * @name: name of the reset line to lookup and assert
1450 * Some IP like dsp, ipu or iva contain processor that require
1451 * an HW reset line to be assert / deassert in order to enable fully
1452 * the IP.
1454 static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1456 struct omap_hwmod_rst_info ohri;
1457 u8 ret;
1459 if (!oh)
1460 return -EINVAL;
1462 ret = _lookup_hardreset(oh, name, &ohri);
1463 if (IS_ERR_VALUE(ret))
1464 return ret;
1466 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1467 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
1468 ohri.rst_shift);
1469 else if (cpu_is_omap44xx())
1470 return omap4_prminst_assert_hardreset(ohri.rst_shift,
1471 oh->clkdm->pwrdm.ptr->prcm_partition,
1472 oh->clkdm->pwrdm.ptr->prcm_offs,
1473 oh->prcm.omap4.rstctrl_offs);
1474 else
1475 return -EINVAL;
1479 * _deassert_hardreset - deassert the HW reset line of submodules contained
1480 * in the hwmod module.
1481 * @oh: struct omap_hwmod *
1482 * @name: name of the reset line to look up and deassert
1484 * Some IP like dsp, ipu or iva contain processor that require
1485 * an HW reset line to be assert / deassert in order to enable fully
1486 * the IP.
1488 static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1490 struct omap_hwmod_rst_info ohri;
1491 int ret;
1493 if (!oh)
1494 return -EINVAL;
1496 ret = _lookup_hardreset(oh, name, &ohri);
1497 if (IS_ERR_VALUE(ret))
1498 return ret;
1500 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1501 ret = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
1502 ohri.rst_shift,
1503 ohri.st_shift);
1504 } else if (cpu_is_omap44xx()) {
1505 if (ohri.st_shift)
1506 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
1507 oh->name, name);
1508 ret = omap4_prminst_deassert_hardreset(ohri.rst_shift,
1509 oh->clkdm->pwrdm.ptr->prcm_partition,
1510 oh->clkdm->pwrdm.ptr->prcm_offs,
1511 oh->prcm.omap4.rstctrl_offs);
1512 } else {
1513 return -EINVAL;
1516 if (ret == -EBUSY)
1517 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1519 return ret;
1523 * _read_hardreset - read the HW reset line state of submodules
1524 * contained in the hwmod module
1525 * @oh: struct omap_hwmod *
1526 * @name: name of the reset line to look up and read
1528 * Return the state of the reset line.
1530 static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1532 struct omap_hwmod_rst_info ohri;
1533 u8 ret;
1535 if (!oh)
1536 return -EINVAL;
1538 ret = _lookup_hardreset(oh, name, &ohri);
1539 if (IS_ERR_VALUE(ret))
1540 return ret;
1542 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1543 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
1544 ohri.st_shift);
1545 } else if (cpu_is_omap44xx()) {
1546 return omap4_prminst_is_hardreset_asserted(ohri.rst_shift,
1547 oh->clkdm->pwrdm.ptr->prcm_partition,
1548 oh->clkdm->pwrdm.ptr->prcm_offs,
1549 oh->prcm.omap4.rstctrl_offs);
1550 } else {
1551 return -EINVAL;
1556 * _are_any_hardreset_lines_asserted - return true if part of @oh is hard-reset
1557 * @oh: struct omap_hwmod *
1559 * If any hardreset line associated with @oh is asserted, then return true.
1560 * Otherwise, if @oh has no hardreset lines associated with it, or if
1561 * no hardreset lines associated with @oh are asserted, then return false.
1562 * This function is used to avoid executing some parts of the IP block
1563 * enable/disable sequence if a hardreset line is set.
1565 static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1567 int i;
1569 if (oh->rst_lines_cnt == 0)
1570 return false;
1572 for (i = 0; i < oh->rst_lines_cnt; i++)
1573 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1574 return true;
1576 return false;
1580 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1581 * @oh: struct omap_hwmod *
1583 * Disable the PRCM module mode related to the hwmod @oh.
1584 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1586 static int _omap4_disable_module(struct omap_hwmod *oh)
1588 int v;
1590 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1591 return -EINVAL;
1593 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1595 omap4_cminst_module_disable(oh->clkdm->prcm_partition,
1596 oh->clkdm->cm_inst,
1597 oh->clkdm->clkdm_offs,
1598 oh->prcm.omap4.clkctrl_offs);
1600 if (_are_any_hardreset_lines_asserted(oh))
1601 return 0;
1603 v = _omap4_wait_target_disable(oh);
1604 if (v)
1605 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1606 oh->name);
1608 return 0;
1612 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
1613 * @oh: struct omap_hwmod *
1615 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
1616 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1617 * reset this way, -EINVAL if the hwmod is in the wrong state,
1618 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
1620 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
1621 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
1622 * use the SYSCONFIG softreset bit to provide the status.
1624 * Note that some IP like McBSP do have reset control but don't have
1625 * reset status.
1627 static int _ocp_softreset(struct omap_hwmod *oh)
1629 u32 v, softrst_mask;
1630 int c = 0;
1631 int ret = 0;
1633 if (!oh->class->sysc ||
1634 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
1635 return -ENOENT;
1637 /* clocks must be on for this operation */
1638 if (oh->_state != _HWMOD_STATE_ENABLED) {
1639 pr_warning("omap_hwmod: %s: reset can only be entered from "
1640 "enabled state\n", oh->name);
1641 return -EINVAL;
1644 /* For some modules, all optionnal clocks need to be enabled as well */
1645 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1646 _enable_optional_clocks(oh);
1648 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
1650 v = oh->_sysc_cache;
1651 ret = _set_softreset(oh, &v);
1652 if (ret)
1653 goto dis_opt_clks;
1654 _write_sysconfig(v, oh);
1656 if (oh->class->sysc->srst_udelay)
1657 udelay(oh->class->sysc->srst_udelay);
1659 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
1660 omap_test_timeout((omap_hwmod_read(oh,
1661 oh->class->sysc->syss_offs)
1662 & SYSS_RESETDONE_MASK),
1663 MAX_MODULE_SOFTRESET_WAIT, c);
1664 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
1665 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
1666 omap_test_timeout(!(omap_hwmod_read(oh,
1667 oh->class->sysc->sysc_offs)
1668 & softrst_mask),
1669 MAX_MODULE_SOFTRESET_WAIT, c);
1672 if (c == MAX_MODULE_SOFTRESET_WAIT)
1673 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1674 oh->name, MAX_MODULE_SOFTRESET_WAIT);
1675 else
1676 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
1679 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1680 * _wait_target_ready() or _reset()
1683 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1685 dis_opt_clks:
1686 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1687 _disable_optional_clocks(oh);
1689 return ret;
1693 * _reset - reset an omap_hwmod
1694 * @oh: struct omap_hwmod *
1696 * Resets an omap_hwmod @oh. If the module has a custom reset
1697 * function pointer defined, then call it to reset the IP block, and
1698 * pass along its return value to the caller. Otherwise, if the IP
1699 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1700 * associated with it, call a function to reset the IP block via that
1701 * method, and pass along the return value to the caller. Finally, if
1702 * the IP block has some hardreset lines associated with it, assert
1703 * all of those, but do _not_ deassert them. (This is because driver
1704 * authors have expressed an apparent requirement to control the
1705 * deassertion of the hardreset lines themselves.)
1707 * The default software reset mechanism for most OMAP IP blocks is
1708 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1709 * hwmods cannot be reset via this method. Some are not targets and
1710 * therefore have no OCP header registers to access. Others (like the
1711 * IVA) have idiosyncratic reset sequences. So for these relatively
1712 * rare cases, custom reset code can be supplied in the struct
1713 * omap_hwmod_class .reset function pointer. Passes along the return
1714 * value from either _ocp_softreset() or the custom reset function -
1715 * these must return -EINVAL if the hwmod cannot be reset this way or
1716 * if the hwmod is in the wrong state, -ETIMEDOUT if the module did
1717 * not reset in time, or 0 upon success.
1719 static int _reset(struct omap_hwmod *oh)
1721 int i, r;
1723 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1725 if (oh->class->reset) {
1726 r = oh->class->reset(oh);
1727 } else {
1728 if (oh->rst_lines_cnt > 0) {
1729 for (i = 0; i < oh->rst_lines_cnt; i++)
1730 _assert_hardreset(oh, oh->rst_lines[i].name);
1731 return 0;
1732 } else {
1733 r = _ocp_softreset(oh);
1734 if (r == -ENOENT)
1735 r = 0;
1740 * OCP_SYSCONFIG bits need to be reprogrammed after a
1741 * softreset. The _enable() function should be split to avoid
1742 * the rewrite of the OCP_SYSCONFIG register.
1744 if (oh->class->sysc) {
1745 _update_sysc_cache(oh);
1746 _enable_sysc(oh);
1749 return r;
1753 * _enable - enable an omap_hwmod
1754 * @oh: struct omap_hwmod *
1756 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
1757 * register target. Returns -EINVAL if the hwmod is in the wrong
1758 * state or passes along the return value of _wait_target_ready().
1760 static int _enable(struct omap_hwmod *oh)
1762 int r;
1763 int hwsup = 0;
1765 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1768 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
1769 * state at init. Now that someone is really trying to enable
1770 * them, just ensure that the hwmod mux is set.
1772 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
1774 * If the caller has mux data populated, do the mux'ing
1775 * which wouldn't have been done as part of the _enable()
1776 * done during setup.
1778 if (oh->mux)
1779 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
1781 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
1782 return 0;
1785 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1786 oh->_state != _HWMOD_STATE_IDLE &&
1787 oh->_state != _HWMOD_STATE_DISABLED) {
1788 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
1789 oh->name);
1790 return -EINVAL;
1794 * If an IP block contains HW reset lines and any of them are
1795 * asserted, we let integration code associated with that
1796 * block handle the enable. We've received very little
1797 * information on what those driver authors need, and until
1798 * detailed information is provided and the driver code is
1799 * posted to the public lists, this is probably the best we
1800 * can do.
1802 if (_are_any_hardreset_lines_asserted(oh))
1803 return 0;
1805 /* Mux pins for device runtime if populated */
1806 if (oh->mux && (!oh->mux->enabled ||
1807 ((oh->_state == _HWMOD_STATE_IDLE) &&
1808 oh->mux->pads_dynamic)))
1809 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
1811 _add_initiator_dep(oh, mpu_oh);
1813 if (oh->clkdm) {
1815 * A clockdomain must be in SW_SUP before enabling
1816 * completely the module. The clockdomain can be set
1817 * in HW_AUTO only when the module become ready.
1819 hwsup = clkdm_in_hwsup(oh->clkdm);
1820 r = clkdm_hwmod_enable(oh->clkdm, oh);
1821 if (r) {
1822 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1823 oh->name, oh->clkdm->name, r);
1824 return r;
1828 _enable_clocks(oh);
1829 if (soc_ops.enable_module)
1830 soc_ops.enable_module(oh);
1832 r = _wait_target_ready(oh);
1833 if (!r) {
1835 * Set the clockdomain to HW_AUTO only if the target is ready,
1836 * assuming that the previous state was HW_AUTO
1838 if (oh->clkdm && hwsup)
1839 clkdm_allow_idle(oh->clkdm);
1841 oh->_state = _HWMOD_STATE_ENABLED;
1843 /* Access the sysconfig only if the target is ready */
1844 if (oh->class->sysc) {
1845 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1846 _update_sysc_cache(oh);
1847 _enable_sysc(oh);
1849 } else {
1850 _disable_clocks(oh);
1851 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1852 oh->name, r);
1854 if (oh->clkdm)
1855 clkdm_hwmod_disable(oh->clkdm, oh);
1858 return r;
1862 * _idle - idle an omap_hwmod
1863 * @oh: struct omap_hwmod *
1865 * Idles an omap_hwmod @oh. This should be called once the hwmod has
1866 * no further work. Returns -EINVAL if the hwmod is in the wrong
1867 * state or returns 0.
1869 static int _idle(struct omap_hwmod *oh)
1871 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1873 if (oh->_state != _HWMOD_STATE_ENABLED) {
1874 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
1875 oh->name);
1876 return -EINVAL;
1879 if (_are_any_hardreset_lines_asserted(oh))
1880 return 0;
1882 if (oh->class->sysc)
1883 _idle_sysc(oh);
1884 _del_initiator_dep(oh, mpu_oh);
1886 if (soc_ops.disable_module)
1887 soc_ops.disable_module(oh);
1890 * The module must be in idle mode before disabling any parents
1891 * clocks. Otherwise, the parent clock might be disabled before
1892 * the module transition is done, and thus will prevent the
1893 * transition to complete properly.
1895 _disable_clocks(oh);
1896 if (oh->clkdm)
1897 clkdm_hwmod_disable(oh->clkdm, oh);
1899 /* Mux pins for device idle if populated */
1900 if (oh->mux && oh->mux->pads_dynamic)
1901 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
1903 oh->_state = _HWMOD_STATE_IDLE;
1905 return 0;
1909 * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
1910 * @oh: struct omap_hwmod *
1911 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
1913 * Sets the IP block's OCP autoidle bit in hardware, and updates our
1914 * local copy. Intended to be used by drivers that require
1915 * direct manipulation of the AUTOIDLE bits.
1916 * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
1917 * along the return value from _set_module_autoidle().
1919 * Any users of this function should be scrutinized carefully.
1921 int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
1923 u32 v;
1924 int retval = 0;
1925 unsigned long flags;
1927 if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
1928 return -EINVAL;
1930 spin_lock_irqsave(&oh->_lock, flags);
1932 v = oh->_sysc_cache;
1934 retval = _set_module_autoidle(oh, autoidle, &v);
1936 if (!retval)
1937 _write_sysconfig(v, oh);
1939 spin_unlock_irqrestore(&oh->_lock, flags);
1941 return retval;
1945 * _shutdown - shutdown an omap_hwmod
1946 * @oh: struct omap_hwmod *
1948 * Shut down an omap_hwmod @oh. This should be called when the driver
1949 * used for the hwmod is removed or unloaded or if the driver is not
1950 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1951 * state or returns 0.
1953 static int _shutdown(struct omap_hwmod *oh)
1955 int ret, i;
1956 u8 prev_state;
1958 if (oh->_state != _HWMOD_STATE_IDLE &&
1959 oh->_state != _HWMOD_STATE_ENABLED) {
1960 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
1961 oh->name);
1962 return -EINVAL;
1965 if (_are_any_hardreset_lines_asserted(oh))
1966 return 0;
1968 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
1970 if (oh->class->pre_shutdown) {
1971 prev_state = oh->_state;
1972 if (oh->_state == _HWMOD_STATE_IDLE)
1973 _enable(oh);
1974 ret = oh->class->pre_shutdown(oh);
1975 if (ret) {
1976 if (prev_state == _HWMOD_STATE_IDLE)
1977 _idle(oh);
1978 return ret;
1982 if (oh->class->sysc) {
1983 if (oh->_state == _HWMOD_STATE_IDLE)
1984 _enable(oh);
1985 _shutdown_sysc(oh);
1988 /* clocks and deps are already disabled in idle */
1989 if (oh->_state == _HWMOD_STATE_ENABLED) {
1990 _del_initiator_dep(oh, mpu_oh);
1991 /* XXX what about the other system initiators here? dma, dsp */
1992 if (soc_ops.disable_module)
1993 soc_ops.disable_module(oh);
1994 _disable_clocks(oh);
1995 if (oh->clkdm)
1996 clkdm_hwmod_disable(oh->clkdm, oh);
1998 /* XXX Should this code also force-disable the optional clocks? */
2000 for (i = 0; i < oh->rst_lines_cnt; i++)
2001 _assert_hardreset(oh, oh->rst_lines[i].name);
2003 /* Mux pins to safe mode or use populated off mode values */
2004 if (oh->mux)
2005 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
2007 oh->_state = _HWMOD_STATE_DISABLED;
2009 return 0;
2013 * _init_mpu_rt_base - populate the virtual address for a hwmod
2014 * @oh: struct omap_hwmod * to locate the virtual address
2016 * Cache the virtual address used by the MPU to access this IP block's
2017 * registers. This address is needed early so the OCP registers that
2018 * are part of the device's address space can be ioremapped properly.
2019 * No return value.
2021 static void __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data)
2023 struct omap_hwmod_addr_space *mem;
2024 void __iomem *va_start;
2026 if (!oh)
2027 return;
2029 _save_mpu_port_index(oh);
2031 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2032 return;
2034 mem = _find_mpu_rt_addr_space(oh);
2035 if (!mem) {
2036 pr_debug("omap_hwmod: %s: no MPU register target found\n",
2037 oh->name);
2038 return;
2041 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
2042 if (!va_start) {
2043 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
2044 return;
2047 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
2048 oh->name, va_start);
2050 oh->_mpu_rt_va = va_start;
2054 * _init - initialize internal data for the hwmod @oh
2055 * @oh: struct omap_hwmod *
2056 * @n: (unused)
2058 * Look up the clocks and the address space used by the MPU to access
2059 * registers belonging to the hwmod @oh. @oh must already be
2060 * registered at this point. This is the first of two phases for
2061 * hwmod initialization. Code called here does not touch any hardware
2062 * registers, it simply prepares internal data structures. Returns 0
2063 * upon success or if the hwmod isn't registered, or -EINVAL upon
2064 * failure.
2066 static int __init _init(struct omap_hwmod *oh, void *data)
2068 int r;
2070 if (oh->_state != _HWMOD_STATE_REGISTERED)
2071 return 0;
2073 _init_mpu_rt_base(oh, NULL);
2075 r = _init_clocks(oh, NULL);
2076 if (IS_ERR_VALUE(r)) {
2077 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2078 return -EINVAL;
2081 oh->_state = _HWMOD_STATE_INITIALIZED;
2083 return 0;
2087 * _setup_iclk_autoidle - configure an IP block's interface clocks
2088 * @oh: struct omap_hwmod *
2090 * Set up the module's interface clocks. XXX This function is still mostly
2091 * a stub; implementing this properly requires iclk autoidle usecounting in
2092 * the clock code. No return value.
2094 static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
2096 struct omap_hwmod_ocp_if *os;
2097 struct list_head *p;
2098 int i = 0;
2099 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2100 return;
2102 p = oh->slave_ports.next;
2104 while (i < oh->slaves_cnt) {
2105 os = _fetch_next_ocp_if(&p, &i);
2106 if (!os->_clk)
2107 continue;
2109 if (os->flags & OCPIF_SWSUP_IDLE) {
2110 /* XXX omap_iclk_deny_idle(c); */
2111 } else {
2112 /* XXX omap_iclk_allow_idle(c); */
2113 clk_enable(os->_clk);
2117 return;
2121 * _setup_reset - reset an IP block during the setup process
2122 * @oh: struct omap_hwmod *
2124 * Reset the IP block corresponding to the hwmod @oh during the setup
2125 * process. The IP block is first enabled so it can be successfully
2126 * reset. Returns 0 upon success or a negative error code upon
2127 * failure.
2129 static int __init _setup_reset(struct omap_hwmod *oh)
2131 int r;
2133 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2134 return -EINVAL;
2136 if (oh->rst_lines_cnt == 0) {
2137 r = _enable(oh);
2138 if (r) {
2139 pr_warning("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2140 oh->name, oh->_state);
2141 return -EINVAL;
2145 if (!(oh->flags & HWMOD_INIT_NO_RESET))
2146 r = _reset(oh);
2148 return r;
2152 * _setup_postsetup - transition to the appropriate state after _setup
2153 * @oh: struct omap_hwmod *
2155 * Place an IP block represented by @oh into a "post-setup" state --
2156 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2157 * this function is called at the end of _setup().) The postsetup
2158 * state for an IP block can be changed by calling
2159 * omap_hwmod_enter_postsetup_state() early in the boot process,
2160 * before one of the omap_hwmod_setup*() functions are called for the
2161 * IP block.
2163 * The IP block stays in this state until a PM runtime-based driver is
2164 * loaded for that IP block. A post-setup state of IDLE is
2165 * appropriate for almost all IP blocks with runtime PM-enabled
2166 * drivers, since those drivers are able to enable the IP block. A
2167 * post-setup state of ENABLED is appropriate for kernels with PM
2168 * runtime disabled. The DISABLED state is appropriate for unusual IP
2169 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2170 * included, since the WDTIMER starts running on reset and will reset
2171 * the MPU if left active.
2173 * This post-setup mechanism is deprecated. Once all of the OMAP
2174 * drivers have been converted to use PM runtime, and all of the IP
2175 * block data and interconnect data is available to the hwmod code, it
2176 * should be possible to replace this mechanism with a "lazy reset"
2177 * arrangement. In a "lazy reset" setup, each IP block is enabled
2178 * when the driver first probes, then all remaining IP blocks without
2179 * drivers are either shut down or enabled after the drivers have
2180 * loaded. However, this cannot take place until the above
2181 * preconditions have been met, since otherwise the late reset code
2182 * has no way of knowing which IP blocks are in use by drivers, and
2183 * which ones are unused.
2185 * No return value.
2187 static void __init _setup_postsetup(struct omap_hwmod *oh)
2189 u8 postsetup_state;
2191 if (oh->rst_lines_cnt > 0)
2192 return;
2194 postsetup_state = oh->_postsetup_state;
2195 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2196 postsetup_state = _HWMOD_STATE_ENABLED;
2199 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2200 * it should be set by the core code as a runtime flag during startup
2202 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
2203 (postsetup_state == _HWMOD_STATE_IDLE)) {
2204 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
2205 postsetup_state = _HWMOD_STATE_ENABLED;
2208 if (postsetup_state == _HWMOD_STATE_IDLE)
2209 _idle(oh);
2210 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2211 _shutdown(oh);
2212 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2213 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2214 oh->name, postsetup_state);
2216 return;
2220 * _setup - prepare IP block hardware for use
2221 * @oh: struct omap_hwmod *
2222 * @n: (unused, pass NULL)
2224 * Configure the IP block represented by @oh. This may include
2225 * enabling the IP block, resetting it, and placing it into a
2226 * post-setup state, depending on the type of IP block and applicable
2227 * flags. IP blocks are reset to prevent any previous configuration
2228 * by the bootloader or previous operating system from interfering
2229 * with power management or other parts of the system. The reset can
2230 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2231 * two phases for hwmod initialization. Code called here generally
2232 * affects the IP block hardware, or system integration hardware
2233 * associated with the IP block. Returns 0.
2235 static int __init _setup(struct omap_hwmod *oh, void *data)
2237 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2238 return 0;
2240 _setup_iclk_autoidle(oh);
2242 if (!_setup_reset(oh))
2243 _setup_postsetup(oh);
2245 return 0;
2249 * _register - register a struct omap_hwmod
2250 * @oh: struct omap_hwmod *
2252 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2253 * already has been registered by the same name; -EINVAL if the
2254 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2255 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2256 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2257 * success.
2259 * XXX The data should be copied into bootmem, so the original data
2260 * should be marked __initdata and freed after init. This would allow
2261 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2262 * that the copy process would be relatively complex due to the large number
2263 * of substructures.
2265 static int __init _register(struct omap_hwmod *oh)
2267 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2268 (oh->_state != _HWMOD_STATE_UNKNOWN))
2269 return -EINVAL;
2271 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2273 if (_lookup(oh->name))
2274 return -EEXIST;
2276 list_add_tail(&oh->node, &omap_hwmod_list);
2278 INIT_LIST_HEAD(&oh->master_ports);
2279 INIT_LIST_HEAD(&oh->slave_ports);
2280 spin_lock_init(&oh->_lock);
2282 oh->_state = _HWMOD_STATE_REGISTERED;
2285 * XXX Rather than doing a strcmp(), this should test a flag
2286 * set in the hwmod data, inserted by the autogenerator code.
2288 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2289 mpu_oh = oh;
2291 return 0;
2295 * _alloc_links - return allocated memory for hwmod links
2296 * @ml: pointer to a struct omap_hwmod_link * for the master link
2297 * @sl: pointer to a struct omap_hwmod_link * for the slave link
2299 * Return pointers to two struct omap_hwmod_link records, via the
2300 * addresses pointed to by @ml and @sl. Will first attempt to return
2301 * memory allocated as part of a large initial block, but if that has
2302 * been exhausted, will allocate memory itself. Since ideally this
2303 * second allocation path will never occur, the number of these
2304 * 'supplemental' allocations will be logged when debugging is
2305 * enabled. Returns 0.
2307 static int __init _alloc_links(struct omap_hwmod_link **ml,
2308 struct omap_hwmod_link **sl)
2310 unsigned int sz;
2312 if ((free_ls + LINKS_PER_OCP_IF) <= max_ls) {
2313 *ml = &linkspace[free_ls++];
2314 *sl = &linkspace[free_ls++];
2315 return 0;
2318 sz = sizeof(struct omap_hwmod_link) * LINKS_PER_OCP_IF;
2320 *sl = NULL;
2321 *ml = alloc_bootmem(sz);
2323 memset(*ml, 0, sz);
2325 *sl = (void *)(*ml) + sizeof(struct omap_hwmod_link);
2327 ls_supp++;
2328 pr_debug("omap_hwmod: supplemental link allocations needed: %d\n",
2329 ls_supp * LINKS_PER_OCP_IF);
2331 return 0;
2335 * _add_link - add an interconnect between two IP blocks
2336 * @oi: pointer to a struct omap_hwmod_ocp_if record
2338 * Add struct omap_hwmod_link records connecting the master IP block
2339 * specified in @oi->master to @oi, and connecting the slave IP block
2340 * specified in @oi->slave to @oi. This code is assumed to run before
2341 * preemption or SMP has been enabled, thus avoiding the need for
2342 * locking in this code. Changes to this assumption will require
2343 * additional locking. Returns 0.
2345 static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2347 struct omap_hwmod_link *ml, *sl;
2349 pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2350 oi->slave->name);
2352 _alloc_links(&ml, &sl);
2354 ml->ocp_if = oi;
2355 INIT_LIST_HEAD(&ml->node);
2356 list_add(&ml->node, &oi->master->master_ports);
2357 oi->master->masters_cnt++;
2359 sl->ocp_if = oi;
2360 INIT_LIST_HEAD(&sl->node);
2361 list_add(&sl->node, &oi->slave->slave_ports);
2362 oi->slave->slaves_cnt++;
2364 return 0;
2368 * _register_link - register a struct omap_hwmod_ocp_if
2369 * @oi: struct omap_hwmod_ocp_if *
2371 * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
2372 * has already been registered; -EINVAL if @oi is NULL or if the
2373 * record pointed to by @oi is missing required fields; or 0 upon
2374 * success.
2376 * XXX The data should be copied into bootmem, so the original data
2377 * should be marked __initdata and freed after init. This would allow
2378 * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2380 static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2382 if (!oi || !oi->master || !oi->slave || !oi->user)
2383 return -EINVAL;
2385 if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2386 return -EEXIST;
2388 pr_debug("omap_hwmod: registering link from %s to %s\n",
2389 oi->master->name, oi->slave->name);
2392 * Register the connected hwmods, if they haven't been
2393 * registered already
2395 if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2396 _register(oi->master);
2398 if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2399 _register(oi->slave);
2401 _add_link(oi);
2403 oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2405 return 0;
2409 * _alloc_linkspace - allocate large block of hwmod links
2410 * @ois: pointer to an array of struct omap_hwmod_ocp_if records to count
2412 * Allocate a large block of struct omap_hwmod_link records. This
2413 * improves boot time significantly by avoiding the need to allocate
2414 * individual records one by one. If the number of records to
2415 * allocate in the block hasn't been manually specified, this function
2416 * will count the number of struct omap_hwmod_ocp_if records in @ois
2417 * and use that to determine the allocation size. For SoC families
2418 * that require multiple list registrations, such as OMAP3xxx, this
2419 * estimation process isn't optimal, so manual estimation is advised
2420 * in those cases. Returns -EEXIST if the allocation has already occurred
2421 * or 0 upon success.
2423 static int __init _alloc_linkspace(struct omap_hwmod_ocp_if **ois)
2425 unsigned int i = 0;
2426 unsigned int sz;
2428 if (linkspace) {
2429 WARN(1, "linkspace already allocated\n");
2430 return -EEXIST;
2433 if (max_ls == 0)
2434 while (ois[i++])
2435 max_ls += LINKS_PER_OCP_IF;
2437 sz = sizeof(struct omap_hwmod_link) * max_ls;
2439 pr_debug("omap_hwmod: %s: allocating %d byte linkspace (%d links)\n",
2440 __func__, sz, max_ls);
2442 linkspace = alloc_bootmem(sz);
2444 memset(linkspace, 0, sz);
2446 return 0;
2449 /* Public functions */
2451 u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
2453 if (oh->flags & HWMOD_16BIT_REG)
2454 return __raw_readw(oh->_mpu_rt_va + reg_offs);
2455 else
2456 return __raw_readl(oh->_mpu_rt_va + reg_offs);
2459 void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
2461 if (oh->flags & HWMOD_16BIT_REG)
2462 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
2463 else
2464 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
2468 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
2469 * @oh: struct omap_hwmod *
2471 * This is a public function exposed to drivers. Some drivers may need to do
2472 * some settings before and after resetting the device. Those drivers after
2473 * doing the necessary settings could use this function to start a reset by
2474 * setting the SYSCONFIG.SOFTRESET bit.
2476 int omap_hwmod_softreset(struct omap_hwmod *oh)
2478 u32 v;
2479 int ret;
2481 if (!oh || !(oh->_sysc_cache))
2482 return -EINVAL;
2484 v = oh->_sysc_cache;
2485 ret = _set_softreset(oh, &v);
2486 if (ret)
2487 goto error;
2488 _write_sysconfig(v, oh);
2490 error:
2491 return ret;
2495 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
2496 * @oh: struct omap_hwmod *
2497 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
2499 * Sets the IP block's OCP slave idlemode in hardware, and updates our
2500 * local copy. Intended to be used by drivers that have some erratum
2501 * that requires direct manipulation of the SIDLEMODE bits. Returns
2502 * -EINVAL if @oh is null, or passes along the return value from
2503 * _set_slave_idlemode().
2505 * XXX Does this function have any current users? If not, we should
2506 * remove it; it is better to let the rest of the hwmod code handle this.
2507 * Any users of this function should be scrutinized carefully.
2509 int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
2511 u32 v;
2512 int retval = 0;
2514 if (!oh)
2515 return -EINVAL;
2517 v = oh->_sysc_cache;
2519 retval = _set_slave_idlemode(oh, idlemode, &v);
2520 if (!retval)
2521 _write_sysconfig(v, oh);
2523 return retval;
2527 * omap_hwmod_lookup - look up a registered omap_hwmod by name
2528 * @name: name of the omap_hwmod to look up
2530 * Given a @name of an omap_hwmod, return a pointer to the registered
2531 * struct omap_hwmod *, or NULL upon error.
2533 struct omap_hwmod *omap_hwmod_lookup(const char *name)
2535 struct omap_hwmod *oh;
2537 if (!name)
2538 return NULL;
2540 oh = _lookup(name);
2542 return oh;
2546 * omap_hwmod_for_each - call function for each registered omap_hwmod
2547 * @fn: pointer to a callback function
2548 * @data: void * data to pass to callback function
2550 * Call @fn for each registered omap_hwmod, passing @data to each
2551 * function. @fn must return 0 for success or any other value for
2552 * failure. If @fn returns non-zero, the iteration across omap_hwmods
2553 * will stop and the non-zero return value will be passed to the
2554 * caller of omap_hwmod_for_each(). @fn is called with
2555 * omap_hwmod_for_each() held.
2557 int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
2558 void *data)
2560 struct omap_hwmod *temp_oh;
2561 int ret = 0;
2563 if (!fn)
2564 return -EINVAL;
2566 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
2567 ret = (*fn)(temp_oh, data);
2568 if (ret)
2569 break;
2572 return ret;
2576 * omap_hwmod_register_links - register an array of hwmod links
2577 * @ois: pointer to an array of omap_hwmod_ocp_if to register
2579 * Intended to be called early in boot before the clock framework is
2580 * initialized. If @ois is not null, will register all omap_hwmods
2581 * listed in @ois that are valid for this chip. Returns -EINVAL if
2582 * omap_hwmod_init() hasn't been called before calling this function,
2583 * -ENOMEM if the link memory area can't be allocated, or 0 upon
2584 * success.
2586 int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
2588 int r, i;
2590 if (!inited)
2591 return -EINVAL;
2593 if (!ois)
2594 return 0;
2596 if (!linkspace) {
2597 if (_alloc_linkspace(ois)) {
2598 pr_err("omap_hwmod: could not allocate link space\n");
2599 return -ENOMEM;
2603 i = 0;
2604 do {
2605 r = _register_link(ois[i]);
2606 WARN(r && r != -EEXIST,
2607 "omap_hwmod: _register_link(%s -> %s) returned %d\n",
2608 ois[i]->master->name, ois[i]->slave->name, r);
2609 } while (ois[++i]);
2611 return 0;
2615 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
2616 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
2618 * If the hwmod data corresponding to the MPU subsystem IP block
2619 * hasn't been initialized and set up yet, do so now. This must be
2620 * done first since sleep dependencies may be added from other hwmods
2621 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
2622 * return value.
2624 static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
2626 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
2627 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
2628 __func__, MPU_INITIATOR_NAME);
2629 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
2630 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
2634 * omap_hwmod_setup_one - set up a single hwmod
2635 * @oh_name: const char * name of the already-registered hwmod to set up
2637 * Initialize and set up a single hwmod. Intended to be used for a
2638 * small number of early devices, such as the timer IP blocks used for
2639 * the scheduler clock. Must be called after omap2_clk_init().
2640 * Resolves the struct clk names to struct clk pointers for each
2641 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
2642 * -EINVAL upon error or 0 upon success.
2644 int __init omap_hwmod_setup_one(const char *oh_name)
2646 struct omap_hwmod *oh;
2648 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
2650 oh = _lookup(oh_name);
2651 if (!oh) {
2652 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
2653 return -EINVAL;
2656 _ensure_mpu_hwmod_is_setup(oh);
2658 _init(oh, NULL);
2659 _setup(oh, NULL);
2661 return 0;
2665 * omap_hwmod_setup_all - set up all registered IP blocks
2667 * Initialize and set up all IP blocks registered with the hwmod code.
2668 * Must be called after omap2_clk_init(). Resolves the struct clk
2669 * names to struct clk pointers for each registered omap_hwmod. Also
2670 * calls _setup() on each hwmod. Returns 0 upon success.
2672 static int __init omap_hwmod_setup_all(void)
2674 _ensure_mpu_hwmod_is_setup(NULL);
2676 omap_hwmod_for_each(_init, NULL);
2677 omap_hwmod_for_each(_setup, NULL);
2679 return 0;
2681 core_initcall(omap_hwmod_setup_all);
2684 * omap_hwmod_enable - enable an omap_hwmod
2685 * @oh: struct omap_hwmod *
2687 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
2688 * Returns -EINVAL on error or passes along the return value from _enable().
2690 int omap_hwmod_enable(struct omap_hwmod *oh)
2692 int r;
2693 unsigned long flags;
2695 if (!oh)
2696 return -EINVAL;
2698 spin_lock_irqsave(&oh->_lock, flags);
2699 r = _enable(oh);
2700 spin_unlock_irqrestore(&oh->_lock, flags);
2702 return r;
2706 * omap_hwmod_idle - idle an omap_hwmod
2707 * @oh: struct omap_hwmod *
2709 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
2710 * Returns -EINVAL on error or passes along the return value from _idle().
2712 int omap_hwmod_idle(struct omap_hwmod *oh)
2714 unsigned long flags;
2716 if (!oh)
2717 return -EINVAL;
2719 spin_lock_irqsave(&oh->_lock, flags);
2720 _idle(oh);
2721 spin_unlock_irqrestore(&oh->_lock, flags);
2723 return 0;
2727 * omap_hwmod_shutdown - shutdown an omap_hwmod
2728 * @oh: struct omap_hwmod *
2730 * Shutdown an omap_hwmod @oh. Intended to be called by
2731 * omap_device_shutdown(). Returns -EINVAL on error or passes along
2732 * the return value from _shutdown().
2734 int omap_hwmod_shutdown(struct omap_hwmod *oh)
2736 unsigned long flags;
2738 if (!oh)
2739 return -EINVAL;
2741 spin_lock_irqsave(&oh->_lock, flags);
2742 _shutdown(oh);
2743 spin_unlock_irqrestore(&oh->_lock, flags);
2745 return 0;
2749 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
2750 * @oh: struct omap_hwmod *oh
2752 * Intended to be called by the omap_device code.
2754 int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
2756 unsigned long flags;
2758 spin_lock_irqsave(&oh->_lock, flags);
2759 _enable_clocks(oh);
2760 spin_unlock_irqrestore(&oh->_lock, flags);
2762 return 0;
2766 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
2767 * @oh: struct omap_hwmod *oh
2769 * Intended to be called by the omap_device code.
2771 int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
2773 unsigned long flags;
2775 spin_lock_irqsave(&oh->_lock, flags);
2776 _disable_clocks(oh);
2777 spin_unlock_irqrestore(&oh->_lock, flags);
2779 return 0;
2783 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
2784 * @oh: struct omap_hwmod *oh
2786 * Intended to be called by drivers and core code when all posted
2787 * writes to a device must complete before continuing further
2788 * execution (for example, after clearing some device IRQSTATUS
2789 * register bits)
2791 * XXX what about targets with multiple OCP threads?
2793 void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
2795 BUG_ON(!oh);
2797 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
2798 WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
2799 oh->name);
2800 return;
2804 * Forces posted writes to complete on the OCP thread handling
2805 * register writes
2807 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
2811 * omap_hwmod_reset - reset the hwmod
2812 * @oh: struct omap_hwmod *
2814 * Under some conditions, a driver may wish to reset the entire device.
2815 * Called from omap_device code. Returns -EINVAL on error or passes along
2816 * the return value from _reset().
2818 int omap_hwmod_reset(struct omap_hwmod *oh)
2820 int r;
2821 unsigned long flags;
2823 if (!oh)
2824 return -EINVAL;
2826 spin_lock_irqsave(&oh->_lock, flags);
2827 r = _reset(oh);
2828 spin_unlock_irqrestore(&oh->_lock, flags);
2830 return r;
2834 * IP block data retrieval functions
2838 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
2839 * @oh: struct omap_hwmod *
2840 * @res: pointer to the first element of an array of struct resource to fill
2842 * Count the number of struct resource array elements necessary to
2843 * contain omap_hwmod @oh resources. Intended to be called by code
2844 * that registers omap_devices. Intended to be used to determine the
2845 * size of a dynamically-allocated struct resource array, before
2846 * calling omap_hwmod_fill_resources(). Returns the number of struct
2847 * resource array elements needed.
2849 * XXX This code is not optimized. It could attempt to merge adjacent
2850 * resource IDs.
2853 int omap_hwmod_count_resources(struct omap_hwmod *oh)
2855 struct omap_hwmod_ocp_if *os;
2856 struct list_head *p;
2857 int ret;
2858 int i = 0;
2860 ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
2862 p = oh->slave_ports.next;
2864 while (i < oh->slaves_cnt) {
2865 os = _fetch_next_ocp_if(&p, &i);
2866 ret += _count_ocp_if_addr_spaces(os);
2869 return ret;
2873 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
2874 * @oh: struct omap_hwmod *
2875 * @res: pointer to the first element of an array of struct resource to fill
2877 * Fill the struct resource array @res with resource data from the
2878 * omap_hwmod @oh. Intended to be called by code that registers
2879 * omap_devices. See also omap_hwmod_count_resources(). Returns the
2880 * number of array elements filled.
2882 int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
2884 struct omap_hwmod_ocp_if *os;
2885 struct list_head *p;
2886 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
2887 int r = 0;
2889 /* For each IRQ, DMA, memory area, fill in array.*/
2891 mpu_irqs_cnt = _count_mpu_irqs(oh);
2892 for (i = 0; i < mpu_irqs_cnt; i++) {
2893 (res + r)->name = (oh->mpu_irqs + i)->name;
2894 (res + r)->start = (oh->mpu_irqs + i)->irq;
2895 (res + r)->end = (oh->mpu_irqs + i)->irq;
2896 (res + r)->flags = IORESOURCE_IRQ;
2897 r++;
2900 sdma_reqs_cnt = _count_sdma_reqs(oh);
2901 for (i = 0; i < sdma_reqs_cnt; i++) {
2902 (res + r)->name = (oh->sdma_reqs + i)->name;
2903 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
2904 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
2905 (res + r)->flags = IORESOURCE_DMA;
2906 r++;
2909 p = oh->slave_ports.next;
2911 i = 0;
2912 while (i < oh->slaves_cnt) {
2913 os = _fetch_next_ocp_if(&p, &i);
2914 addr_cnt = _count_ocp_if_addr_spaces(os);
2916 for (j = 0; j < addr_cnt; j++) {
2917 (res + r)->name = (os->addr + j)->name;
2918 (res + r)->start = (os->addr + j)->pa_start;
2919 (res + r)->end = (os->addr + j)->pa_end;
2920 (res + r)->flags = IORESOURCE_MEM;
2921 r++;
2925 return r;
2929 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
2930 * @oh: struct omap_hwmod * to operate on
2931 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
2932 * @name: pointer to the name of the data to fetch (optional)
2933 * @rsrc: pointer to a struct resource, allocated by the caller
2935 * Retrieve MPU IRQ, SDMA request line, or address space start/end
2936 * data for the IP block pointed to by @oh. The data will be filled
2937 * into a struct resource record pointed to by @rsrc. The struct
2938 * resource must be allocated by the caller. When @name is non-null,
2939 * the data associated with the matching entry in the IRQ/SDMA/address
2940 * space hwmod data arrays will be returned. If @name is null, the
2941 * first array entry will be returned. Data order is not meaningful
2942 * in hwmod data, so callers are strongly encouraged to use a non-null
2943 * @name whenever possible to avoid unpredictable effects if hwmod
2944 * data is later added that causes data ordering to change. This
2945 * function is only intended for use by OMAP core code. Device
2946 * drivers should not call this function - the appropriate bus-related
2947 * data accessor functions should be used instead. Returns 0 upon
2948 * success or a negative error code upon error.
2950 int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
2951 const char *name, struct resource *rsrc)
2953 int r;
2954 unsigned int irq, dma;
2955 u32 pa_start, pa_end;
2957 if (!oh || !rsrc)
2958 return -EINVAL;
2960 if (type == IORESOURCE_IRQ) {
2961 r = _get_mpu_irq_by_name(oh, name, &irq);
2962 if (r)
2963 return r;
2965 rsrc->start = irq;
2966 rsrc->end = irq;
2967 } else if (type == IORESOURCE_DMA) {
2968 r = _get_sdma_req_by_name(oh, name, &dma);
2969 if (r)
2970 return r;
2972 rsrc->start = dma;
2973 rsrc->end = dma;
2974 } else if (type == IORESOURCE_MEM) {
2975 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
2976 if (r)
2977 return r;
2979 rsrc->start = pa_start;
2980 rsrc->end = pa_end;
2981 } else {
2982 return -EINVAL;
2985 rsrc->flags = type;
2986 rsrc->name = name;
2988 return 0;
2992 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
2993 * @oh: struct omap_hwmod *
2995 * Return the powerdomain pointer associated with the OMAP module
2996 * @oh's main clock. If @oh does not have a main clk, return the
2997 * powerdomain associated with the interface clock associated with the
2998 * module's MPU port. (XXX Perhaps this should use the SDMA port
2999 * instead?) Returns NULL on error, or a struct powerdomain * on
3000 * success.
3002 struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
3004 struct clk *c;
3005 struct omap_hwmod_ocp_if *oi;
3007 if (!oh)
3008 return NULL;
3010 if (oh->_clk) {
3011 c = oh->_clk;
3012 } else {
3013 oi = _find_mpu_rt_port(oh);
3014 if (!oi)
3015 return NULL;
3016 c = oi->_clk;
3019 if (!c->clkdm)
3020 return NULL;
3022 return c->clkdm->pwrdm.ptr;
3027 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3028 * @oh: struct omap_hwmod *
3030 * Returns the virtual address corresponding to the beginning of the
3031 * module's register target, in the address range that is intended to
3032 * be used by the MPU. Returns the virtual address upon success or NULL
3033 * upon error.
3035 void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3037 if (!oh)
3038 return NULL;
3040 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3041 return NULL;
3043 if (oh->_state == _HWMOD_STATE_UNKNOWN)
3044 return NULL;
3046 return oh->_mpu_rt_va;
3050 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
3051 * @oh: struct omap_hwmod *
3052 * @init_oh: struct omap_hwmod * (initiator)
3054 * Add a sleep dependency between the initiator @init_oh and @oh.
3055 * Intended to be called by DSP/Bridge code via platform_data for the
3056 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3057 * code needs to add/del initiator dependencies dynamically
3058 * before/after accessing a device. Returns the return value from
3059 * _add_initiator_dep().
3061 * XXX Keep a usecount in the clockdomain code
3063 int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
3064 struct omap_hwmod *init_oh)
3066 return _add_initiator_dep(oh, init_oh);
3070 * XXX what about functions for drivers to save/restore ocp_sysconfig
3071 * for context save/restore operations?
3075 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
3076 * @oh: struct omap_hwmod *
3077 * @init_oh: struct omap_hwmod * (initiator)
3079 * Remove a sleep dependency between the initiator @init_oh and @oh.
3080 * Intended to be called by DSP/Bridge code via platform_data for the
3081 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3082 * code needs to add/del initiator dependencies dynamically
3083 * before/after accessing a device. Returns the return value from
3084 * _del_initiator_dep().
3086 * XXX Keep a usecount in the clockdomain code
3088 int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
3089 struct omap_hwmod *init_oh)
3091 return _del_initiator_dep(oh, init_oh);
3095 * omap_hwmod_enable_wakeup - allow device to wake up the system
3096 * @oh: struct omap_hwmod *
3098 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
3099 * send wakeups to the PRCM, and enable I/O ring wakeup events for
3100 * this IP block if it has dynamic mux entries. Eventually this
3101 * should set PRCM wakeup registers to cause the PRCM to receive
3102 * wakeup events from the module. Does not set any wakeup routing
3103 * registers beyond this point - if the module is to wake up any other
3104 * module or subsystem, that must be set separately. Called by
3105 * omap_device code. Returns -EINVAL on error or 0 upon success.
3107 int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3109 unsigned long flags;
3110 u32 v;
3112 spin_lock_irqsave(&oh->_lock, flags);
3114 if (oh->class->sysc &&
3115 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3116 v = oh->_sysc_cache;
3117 _enable_wakeup(oh, &v);
3118 _write_sysconfig(v, oh);
3121 _set_idle_ioring_wakeup(oh, true);
3122 spin_unlock_irqrestore(&oh->_lock, flags);
3124 return 0;
3128 * omap_hwmod_disable_wakeup - prevent device from waking the system
3129 * @oh: struct omap_hwmod *
3131 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
3132 * from sending wakeups to the PRCM, and disable I/O ring wakeup
3133 * events for this IP block if it has dynamic mux entries. Eventually
3134 * this should clear PRCM wakeup registers to cause the PRCM to ignore
3135 * wakeup events from the module. Does not set any wakeup routing
3136 * registers beyond this point - if the module is to wake up any other
3137 * module or subsystem, that must be set separately. Called by
3138 * omap_device code. Returns -EINVAL on error or 0 upon success.
3140 int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3142 unsigned long flags;
3143 u32 v;
3145 spin_lock_irqsave(&oh->_lock, flags);
3147 if (oh->class->sysc &&
3148 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3149 v = oh->_sysc_cache;
3150 _disable_wakeup(oh, &v);
3151 _write_sysconfig(v, oh);
3154 _set_idle_ioring_wakeup(oh, false);
3155 spin_unlock_irqrestore(&oh->_lock, flags);
3157 return 0;
3161 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3162 * contained in the hwmod module.
3163 * @oh: struct omap_hwmod *
3164 * @name: name of the reset line to lookup and assert
3166 * Some IP like dsp, ipu or iva contain processor that require
3167 * an HW reset line to be assert / deassert in order to enable fully
3168 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3169 * yet supported on this OMAP; otherwise, passes along the return value
3170 * from _assert_hardreset().
3172 int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3174 int ret;
3175 unsigned long flags;
3177 if (!oh)
3178 return -EINVAL;
3180 spin_lock_irqsave(&oh->_lock, flags);
3181 ret = _assert_hardreset(oh, name);
3182 spin_unlock_irqrestore(&oh->_lock, flags);
3184 return ret;
3188 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3189 * contained in the hwmod module.
3190 * @oh: struct omap_hwmod *
3191 * @name: name of the reset line to look up and deassert
3193 * Some IP like dsp, ipu or iva contain processor that require
3194 * an HW reset line to be assert / deassert in order to enable fully
3195 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3196 * yet supported on this OMAP; otherwise, passes along the return value
3197 * from _deassert_hardreset().
3199 int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3201 int ret;
3202 unsigned long flags;
3204 if (!oh)
3205 return -EINVAL;
3207 spin_lock_irqsave(&oh->_lock, flags);
3208 ret = _deassert_hardreset(oh, name);
3209 spin_unlock_irqrestore(&oh->_lock, flags);
3211 return ret;
3215 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
3216 * contained in the hwmod module
3217 * @oh: struct omap_hwmod *
3218 * @name: name of the reset line to look up and read
3220 * Return the current state of the hwmod @oh's reset line named @name:
3221 * returns -EINVAL upon parameter error or if this operation
3222 * is unsupported on the current OMAP; otherwise, passes along the return
3223 * value from _read_hardreset().
3225 int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
3227 int ret;
3228 unsigned long flags;
3230 if (!oh)
3231 return -EINVAL;
3233 spin_lock_irqsave(&oh->_lock, flags);
3234 ret = _read_hardreset(oh, name);
3235 spin_unlock_irqrestore(&oh->_lock, flags);
3237 return ret;
3242 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3243 * @classname: struct omap_hwmod_class name to search for
3244 * @fn: callback function pointer to call for each hwmod in class @classname
3245 * @user: arbitrary context data to pass to the callback function
3247 * For each omap_hwmod of class @classname, call @fn.
3248 * If the callback function returns something other than
3249 * zero, the iterator is terminated, and the callback function's return
3250 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3251 * if @classname or @fn are NULL, or passes back the error code from @fn.
3253 int omap_hwmod_for_each_by_class(const char *classname,
3254 int (*fn)(struct omap_hwmod *oh,
3255 void *user),
3256 void *user)
3258 struct omap_hwmod *temp_oh;
3259 int ret = 0;
3261 if (!classname || !fn)
3262 return -EINVAL;
3264 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3265 __func__, classname);
3267 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3268 if (!strcmp(temp_oh->class->name, classname)) {
3269 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3270 __func__, temp_oh->name);
3271 ret = (*fn)(temp_oh, user);
3272 if (ret)
3273 break;
3277 if (ret)
3278 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3279 __func__, ret);
3281 return ret;
3285 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3286 * @oh: struct omap_hwmod *
3287 * @state: state that _setup() should leave the hwmod in
3289 * Sets the hwmod state that @oh will enter at the end of _setup()
3290 * (called by omap_hwmod_setup_*()). See also the documentation
3291 * for _setup_postsetup(), above. Returns 0 upon success or
3292 * -EINVAL if there is a problem with the arguments or if the hwmod is
3293 * in the wrong state.
3295 int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3297 int ret;
3298 unsigned long flags;
3300 if (!oh)
3301 return -EINVAL;
3303 if (state != _HWMOD_STATE_DISABLED &&
3304 state != _HWMOD_STATE_ENABLED &&
3305 state != _HWMOD_STATE_IDLE)
3306 return -EINVAL;
3308 spin_lock_irqsave(&oh->_lock, flags);
3310 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3311 ret = -EINVAL;
3312 goto ohsps_unlock;
3315 oh->_postsetup_state = state;
3316 ret = 0;
3318 ohsps_unlock:
3319 spin_unlock_irqrestore(&oh->_lock, flags);
3321 return ret;
3325 * omap_hwmod_get_context_loss_count - get lost context count
3326 * @oh: struct omap_hwmod *
3328 * Query the powerdomain of of @oh to get the context loss
3329 * count for this device.
3331 * Returns the context loss count of the powerdomain assocated with @oh
3332 * upon success, or zero if no powerdomain exists for @oh.
3334 int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
3336 struct powerdomain *pwrdm;
3337 int ret = 0;
3339 pwrdm = omap_hwmod_get_pwrdm(oh);
3340 if (pwrdm)
3341 ret = pwrdm_get_context_loss_count(pwrdm);
3343 return ret;
3347 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
3348 * @oh: struct omap_hwmod *
3350 * Prevent the hwmod @oh from being reset during the setup process.
3351 * Intended for use by board-*.c files on boards with devices that
3352 * cannot tolerate being reset. Must be called before the hwmod has
3353 * been set up. Returns 0 upon success or negative error code upon
3354 * failure.
3356 int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
3358 if (!oh)
3359 return -EINVAL;
3361 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3362 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
3363 oh->name);
3364 return -EINVAL;
3367 oh->flags |= HWMOD_INIT_NO_RESET;
3369 return 0;
3373 * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
3374 * @oh: struct omap_hwmod * containing hwmod mux entries
3375 * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
3376 * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
3378 * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
3379 * entry number @pad_idx for the hwmod @oh, trigger the interrupt
3380 * service routine for the hwmod's mpu_irqs array index @irq_idx. If
3381 * this function is not called for a given pad_idx, then the ISR
3382 * associated with @oh's first MPU IRQ will be triggered when an I/O
3383 * pad wakeup occurs on that pad. Note that @pad_idx is the index of
3384 * the _dynamic or wakeup_ entry: if there are other entries not
3385 * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
3386 * entries are NOT COUNTED in the dynamic pad index. This function
3387 * must be called separately for each pad that requires its interrupt
3388 * to be re-routed this way. Returns -EINVAL if there is an argument
3389 * problem or if @oh does not have hwmod mux entries or MPU IRQs;
3390 * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
3392 * XXX This function interface is fragile. Rather than using array
3393 * indexes, which are subject to unpredictable change, it should be
3394 * using hwmod IRQ names, and some other stable key for the hwmod mux
3395 * pad records.
3397 int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
3399 int nr_irqs;
3401 might_sleep();
3403 if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
3404 pad_idx >= oh->mux->nr_pads_dynamic)
3405 return -EINVAL;
3407 /* Check the number of available mpu_irqs */
3408 for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
3411 if (irq_idx >= nr_irqs)
3412 return -EINVAL;
3414 if (!oh->mux->irqs) {
3415 /* XXX What frees this? */
3416 oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
3417 GFP_KERNEL);
3418 if (!oh->mux->irqs)
3419 return -ENOMEM;
3421 oh->mux->irqs[pad_idx] = irq_idx;
3423 return 0;
3427 * omap_hwmod_init - initialize the hwmod code
3429 * Sets up some function pointers needed by the hwmod code to operate on the
3430 * currently-booted SoC. Intended to be called once during kernel init
3431 * before any hwmods are registered. No return value.
3433 void __init omap_hwmod_init(void)
3435 if (cpu_is_omap44xx()) {
3436 soc_ops.enable_module = _omap4_enable_module;
3437 soc_ops.disable_module = _omap4_disable_module;
3440 inited = true;