2 * omap_hwmod implementation for OMAP2/3/4
4 * Copyright (C) 2009 Nokia Corporation
6 * With fixes and testing from Kevin Hilman
8 * Created in collaboration with (alphabetical order): Benoit Cousson,
9 * Kevin Hilman, Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari
10 * Poussa, Anand Sawant, Santosh Shilimkar, Richard Woodruff
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
16 * This code manages "OMAP modules" (on-chip devices) and their
17 * integration with Linux device driver and bus code.
20 * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
21 * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
22 * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
23 * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
24 * - Open Core Protocol Specification 2.2
29 * - bus throughput & module latency measurement code
31 * XXX add tests at the beginning of each function to ensure the hwmod is
32 * in the appropriate state
33 * XXX error return values should be checked to ensure that they are
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
41 #include <linux/clk.h>
42 #include <linux/delay.h>
43 #include <linux/err.h>
44 #include <linux/list.h>
45 #include <linux/mutex.h>
47 #include <plat/common.h>
49 #include <plat/clockdomain.h>
50 #include <plat/powerdomain.h>
51 #include <plat/clock.h>
52 #include <plat/omap_hwmod.h>
56 /* Maximum microseconds to wait for OMAP module to reset */
57 #define MAX_MODULE_RESET_WAIT 10000
59 /* Name of the OMAP hwmod for the MPU */
60 #define MPU_INITIATOR_NAME "mpu_hwmod"
62 /* omap_hwmod_list contains all registered struct omap_hwmods */
63 static LIST_HEAD(omap_hwmod_list);
65 static DEFINE_MUTEX(omap_hwmod_mutex);
67 /* mpu_oh: used to add/remove MPU initiator from sleepdep list */
68 static struct omap_hwmod *mpu_oh;
70 /* inited: 0 if omap_hwmod_init() has not yet been called; 1 otherwise */
74 /* Private functions */
77 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
78 * @oh: struct omap_hwmod *
80 * Load the current value of the hwmod OCP_SYSCONFIG register into the
81 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
82 * OCP_SYSCONFIG register or 0 upon success.
84 static int _update_sysc_cache(struct omap_hwmod *oh)
86 if (!oh->class->sysc) {
87 WARN(1, "omap_hwmod: %s: cannot read OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
91 /* XXX ensure module interface clock is up */
93 oh->_sysc_cache = omap_hwmod_readl(oh, oh->class->sysc->sysc_offs);
95 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
96 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
102 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
103 * @v: OCP_SYSCONFIG value to write
104 * @oh: struct omap_hwmod *
106 * Write @v into the module class' OCP_SYSCONFIG register, if it has
107 * one. No return value.
109 static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
111 if (!oh->class->sysc) {
112 WARN(1, "omap_hwmod: %s: cannot write OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
116 /* XXX ensure module interface clock is up */
118 if (oh->_sysc_cache != v) {
120 omap_hwmod_writel(v, oh, oh->class->sysc->sysc_offs);
125 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
126 * @oh: struct omap_hwmod *
127 * @standbymode: MIDLEMODE field bits
128 * @v: pointer to register contents to modify
130 * Update the master standby mode bits in @v to be @standbymode for
131 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
132 * upon error or 0 upon success.
134 static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
140 if (!oh->class->sysc ||
141 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
144 if (!oh->class->sysc->sysc_fields) {
145 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
149 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
150 mstandby_mask = (0x3 << mstandby_shift);
152 *v &= ~mstandby_mask;
153 *v |= __ffs(standbymode) << mstandby_shift;
159 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
160 * @oh: struct omap_hwmod *
161 * @idlemode: SIDLEMODE field bits
162 * @v: pointer to register contents to modify
164 * Update the slave idle mode bits in @v to be @idlemode for the @oh
165 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
168 static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
173 if (!oh->class->sysc ||
174 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
177 if (!oh->class->sysc->sysc_fields) {
178 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
182 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
183 sidle_mask = (0x3 << sidle_shift);
186 *v |= __ffs(idlemode) << sidle_shift;
192 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
193 * @oh: struct omap_hwmod *
194 * @clockact: CLOCKACTIVITY field bits
195 * @v: pointer to register contents to modify
197 * Update the clockactivity mode bits in @v to be @clockact for the
198 * @oh hwmod. Used for additional powersaving on some modules. Does
199 * not write to the hardware. Returns -EINVAL upon error or 0 upon
202 static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
207 if (!oh->class->sysc ||
208 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
211 if (!oh->class->sysc->sysc_fields) {
212 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
216 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
217 clkact_mask = (0x3 << clkact_shift);
220 *v |= clockact << clkact_shift;
226 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
227 * @oh: struct omap_hwmod *
228 * @v: pointer to register contents to modify
230 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
231 * error or 0 upon success.
233 static int _set_softreset(struct omap_hwmod *oh, u32 *v)
237 if (!oh->class->sysc ||
238 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
241 if (!oh->class->sysc->sysc_fields) {
242 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
246 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
254 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
255 * @oh: struct omap_hwmod *
256 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
257 * @v: pointer to register contents to modify
259 * Update the module autoidle bit in @v to be @autoidle for the @oh
260 * hwmod. The autoidle bit controls whether the module can gate
261 * internal clocks automatically when it isn't doing anything; the
262 * exact function of this bit varies on a per-module basis. This
263 * function does not write to the hardware. Returns -EINVAL upon
264 * error or 0 upon success.
266 static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
272 if (!oh->class->sysc ||
273 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
276 if (!oh->class->sysc->sysc_fields) {
277 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
281 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
282 autoidle_mask = (0x3 << autoidle_shift);
284 *v &= ~autoidle_mask;
285 *v |= autoidle << autoidle_shift;
291 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
292 * @oh: struct omap_hwmod *
294 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
295 * upon error or 0 upon success.
297 static int _enable_wakeup(struct omap_hwmod *oh)
301 if (!oh->class->sysc ||
302 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
305 if (!oh->class->sysc->sysc_fields) {
306 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
310 wakeup_mask = (0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
314 _write_sysconfig(v, oh);
316 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
318 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
324 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
325 * @oh: struct omap_hwmod *
327 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
328 * upon error or 0 upon success.
330 static int _disable_wakeup(struct omap_hwmod *oh)
334 if (!oh->class->sysc ||
335 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
338 if (!oh->class->sysc->sysc_fields) {
339 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
343 wakeup_mask = (0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
347 _write_sysconfig(v, oh);
349 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
351 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
357 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
358 * @oh: struct omap_hwmod *
360 * Prevent the hardware module @oh from entering idle while the
361 * hardare module initiator @init_oh is active. Useful when a module
362 * will be accessed by a particular initiator (e.g., if a module will
363 * be accessed by the IVA, there should be a sleepdep between the IVA
364 * initiator and the module). Only applies to modules in smart-idle
365 * mode. Returns -EINVAL upon error or passes along
366 * clkdm_add_sleepdep() value upon success.
368 static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
373 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
377 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
378 * @oh: struct omap_hwmod *
380 * Allow the hardware module @oh to enter idle while the hardare
381 * module initiator @init_oh is active. Useful when a module will not
382 * be accessed by a particular initiator (e.g., if a module will not
383 * be accessed by the IVA, there should be no sleepdep between the IVA
384 * initiator and the module). Only applies to modules in smart-idle
385 * mode. Returns -EINVAL upon error or passes along
386 * clkdm_del_sleepdep() value upon success.
388 static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
393 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
397 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
398 * @oh: struct omap_hwmod *
400 * Called from _init_clocks(). Populates the @oh _clk (main
401 * functional clock pointer) if a main_clk is present. Returns 0 on
402 * success or -EINVAL on error.
404 static int _init_main_clk(struct omap_hwmod *oh)
412 c = omap_clk_get_by_name(oh->main_clk);
413 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get main_clk %s\n",
414 oh->name, oh->main_clk);
419 WARN(!c->clkdm, "omap_hwmod: %s: missing clockdomain for %s.\n",
420 oh->main_clk, c->name);
426 * _init_interface_clk - get a struct clk * for the the hwmod's interface clks
427 * @oh: struct omap_hwmod *
429 * Called from _init_clocks(). Populates the @oh OCP slave interface
430 * clock pointers. Returns 0 on success or -EINVAL on error.
432 static int _init_interface_clks(struct omap_hwmod *oh)
434 struct omap_hwmod_ocp_if *os;
439 if (oh->slaves_cnt == 0)
442 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
446 c = omap_clk_get_by_name(os->clk);
447 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get "
448 "interface_clk %s\n", oh->name, os->clk);
458 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
459 * @oh: struct omap_hwmod *
461 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
462 * clock pointers. Returns 0 on success or -EINVAL on error.
464 static int _init_opt_clks(struct omap_hwmod *oh)
466 struct omap_hwmod_opt_clk *oc;
471 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
472 c = omap_clk_get_by_name(oc->clk);
473 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get opt_clk "
474 "%s\n", oh->name, oc->clk);
484 * _enable_clocks - enable hwmod main clock and interface clocks
485 * @oh: struct omap_hwmod *
487 * Enables all clocks necessary for register reads and writes to succeed
488 * on the hwmod @oh. Returns 0.
490 static int _enable_clocks(struct omap_hwmod *oh)
492 struct omap_hwmod_ocp_if *os;
495 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
497 if (oh->_clk && !IS_ERR(oh->_clk))
498 clk_enable(oh->_clk);
500 if (oh->slaves_cnt > 0) {
501 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
502 struct clk *c = os->_clk;
504 if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
509 /* The opt clocks are controlled by the device driver. */
515 * _disable_clocks - disable hwmod main clock and interface clocks
516 * @oh: struct omap_hwmod *
518 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
520 static int _disable_clocks(struct omap_hwmod *oh)
522 struct omap_hwmod_ocp_if *os;
525 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
527 if (oh->_clk && !IS_ERR(oh->_clk))
528 clk_disable(oh->_clk);
530 if (oh->slaves_cnt > 0) {
531 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
532 struct clk *c = os->_clk;
534 if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
539 /* The opt clocks are controlled by the device driver. */
545 * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
546 * @oh: struct omap_hwmod *
548 * Returns the array index of the OCP slave port that the MPU
549 * addresses the device on, or -EINVAL upon error or not found.
551 static int _find_mpu_port_index(struct omap_hwmod *oh)
553 struct omap_hwmod_ocp_if *os;
557 if (!oh || oh->slaves_cnt == 0)
560 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
561 if (os->user & OCP_USER_MPU) {
568 pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
571 pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
574 return (found) ? i : -EINVAL;
578 * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
579 * @oh: struct omap_hwmod *
581 * Return the virtual address of the base of the register target of
582 * device @oh, or NULL on error.
584 static void __iomem *_find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
586 struct omap_hwmod_ocp_if *os;
587 struct omap_hwmod_addr_space *mem;
590 void __iomem *va_start;
592 if (!oh || oh->slaves_cnt == 0)
595 os = *oh->slaves + index;
597 for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
598 if (mem->flags & ADDR_TYPE_RT) {
605 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
607 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
610 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
613 pr_debug("omap_hwmod: %s: no MPU register target found\n",
617 return (found) ? va_start : NULL;
621 * _sysc_enable - try to bring a module out of idle via OCP_SYSCONFIG
622 * @oh: struct omap_hwmod *
624 * If module is marked as SWSUP_SIDLE, force the module out of slave
625 * idle; otherwise, configure it for smart-idle. If module is marked
626 * as SWSUP_MSUSPEND, force the module out of master standby;
627 * otherwise, configure it for smart-standby. No return value.
629 static void _sysc_enable(struct omap_hwmod *oh)
634 if (!oh->class->sysc)
638 sf = oh->class->sysc->sysc_flags;
640 if (sf & SYSC_HAS_SIDLEMODE) {
641 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
642 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
643 _set_slave_idlemode(oh, idlemode, &v);
646 if (sf & SYSC_HAS_MIDLEMODE) {
647 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
648 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
649 _set_master_standbymode(oh, idlemode, &v);
652 if (sf & SYSC_HAS_AUTOIDLE) {
653 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
655 _set_module_autoidle(oh, idlemode, &v);
658 /* XXX OCP ENAWAKEUP bit? */
661 * XXX The clock framework should handle this, by
662 * calling into this code. But this must wait until the
663 * clock structures are tagged with omap_hwmod entries
665 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
666 (sf & SYSC_HAS_CLOCKACTIVITY))
667 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
669 _write_sysconfig(v, oh);
673 * _sysc_idle - try to put a module into idle via OCP_SYSCONFIG
674 * @oh: struct omap_hwmod *
676 * If module is marked as SWSUP_SIDLE, force the module into slave
677 * idle; otherwise, configure it for smart-idle. If module is marked
678 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
679 * configure it for smart-standby. No return value.
681 static void _sysc_idle(struct omap_hwmod *oh)
686 if (!oh->class->sysc)
690 sf = oh->class->sysc->sysc_flags;
692 if (sf & SYSC_HAS_SIDLEMODE) {
693 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
694 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
695 _set_slave_idlemode(oh, idlemode, &v);
698 if (sf & SYSC_HAS_MIDLEMODE) {
699 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
700 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
701 _set_master_standbymode(oh, idlemode, &v);
704 _write_sysconfig(v, oh);
708 * _sysc_shutdown - force a module into idle via OCP_SYSCONFIG
709 * @oh: struct omap_hwmod *
711 * Force the module into slave idle and master suspend. No return
714 static void _sysc_shutdown(struct omap_hwmod *oh)
719 if (!oh->class->sysc)
723 sf = oh->class->sysc->sysc_flags;
725 if (sf & SYSC_HAS_SIDLEMODE)
726 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
728 if (sf & SYSC_HAS_MIDLEMODE)
729 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
731 if (sf & SYSC_HAS_AUTOIDLE)
732 _set_module_autoidle(oh, 1, &v);
734 _write_sysconfig(v, oh);
738 * _lookup - find an omap_hwmod by name
739 * @name: find an omap_hwmod by name
741 * Return a pointer to an omap_hwmod by name, or NULL if not found.
742 * Caller must hold omap_hwmod_mutex.
744 static struct omap_hwmod *_lookup(const char *name)
746 struct omap_hwmod *oh, *temp_oh;
750 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
751 if (!strcmp(name, temp_oh->name)) {
761 * _init_clocks - clk_get() all clocks associated with this hwmod
762 * @oh: struct omap_hwmod *
764 * Called by omap_hwmod_late_init() (after omap2_clk_init()).
765 * Resolves all clock names embedded in the hwmod. Must be called
766 * with omap_hwmod_mutex held. Returns -EINVAL if the omap_hwmod
767 * has not yet been registered or if the clocks have already been
768 * initialized, 0 on success, or a non-zero error on failure.
770 static int _init_clocks(struct omap_hwmod *oh)
774 if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
777 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
779 ret |= _init_main_clk(oh);
780 ret |= _init_interface_clks(oh);
781 ret |= _init_opt_clks(oh);
783 oh->_state = _HWMOD_STATE_CLKS_INITED;
789 * _wait_target_ready - wait for a module to leave slave idle
790 * @oh: struct omap_hwmod *
792 * Wait for a module @oh to leave slave idle. Returns 0 if the module
793 * does not have an IDLEST bit or if the module successfully leaves
794 * slave idle; otherwise, pass along the return value of the
795 * appropriate *_cm_wait_module_ready() function.
797 static int _wait_target_ready(struct omap_hwmod *oh)
799 struct omap_hwmod_ocp_if *os;
805 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
808 os = *oh->slaves + oh->_mpu_port_index;
810 if (!(os->flags & OCPIF_HAS_IDLEST))
813 /* XXX check module SIDLEMODE */
815 /* XXX check clock enable states */
817 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
818 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
819 oh->prcm.omap2.idlest_reg_id,
820 oh->prcm.omap2.idlest_idle_bit);
822 } else if (cpu_is_omap44xx()) {
823 ret = omap4_cm_wait_module_ready(oh->prcm.omap4.module_offs,
824 oh->prcm.omap4.device_offs);
834 * _reset - reset an omap_hwmod
835 * @oh: struct omap_hwmod *
837 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
838 * enabled for this to work. Must be called with omap_hwmod_mutex
839 * held. Returns -EINVAL if the hwmod cannot be reset this way or if
840 * the hwmod is in the wrong state, -ETIMEDOUT if the module did not
841 * reset in time, or 0 upon success.
843 static int _reset(struct omap_hwmod *oh)
848 if (!oh->class->sysc ||
849 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET) ||
850 (oh->class->sysc->sysc_flags & SYSS_MISSING))
853 /* clocks must be on for this operation */
854 if (oh->_state != _HWMOD_STATE_ENABLED) {
855 WARN(1, "omap_hwmod: %s: reset can only be entered from "
856 "enabled state\n", oh->name);
860 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
863 r = _set_softreset(oh, &v);
866 _write_sysconfig(v, oh);
868 omap_test_timeout((omap_hwmod_readl(oh, oh->class->sysc->syss_offs) &
869 SYSS_RESETDONE_MASK),
870 MAX_MODULE_RESET_WAIT, c);
872 if (c == MAX_MODULE_RESET_WAIT)
873 WARN(1, "omap_hwmod: %s: failed to reset in %d usec\n",
874 oh->name, MAX_MODULE_RESET_WAIT);
876 pr_debug("omap_hwmod: %s: reset in %d usec\n", oh->name, c);
879 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
880 * _wait_target_ready() or _reset()
883 return (c == MAX_MODULE_RESET_WAIT) ? -ETIMEDOUT : 0;
887 * _enable - enable an omap_hwmod
888 * @oh: struct omap_hwmod *
890 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
891 * register target. Must be called with omap_hwmod_mutex held.
892 * Returns -EINVAL if the hwmod is in the wrong state or passes along
893 * the return value of _wait_target_ready().
895 static int _enable(struct omap_hwmod *oh)
899 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
900 oh->_state != _HWMOD_STATE_IDLE &&
901 oh->_state != _HWMOD_STATE_DISABLED) {
902 WARN(1, "omap_hwmod: %s: enabled state can only be entered "
903 "from initialized, idle, or disabled state\n", oh->name);
907 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
911 _add_initiator_dep(oh, mpu_oh);
914 if (oh->class->sysc) {
915 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
916 _update_sysc_cache(oh);
920 r = _wait_target_ready(oh);
922 oh->_state = _HWMOD_STATE_ENABLED;
928 * _idle - idle an omap_hwmod
929 * @oh: struct omap_hwmod *
931 * Idles an omap_hwmod @oh. This should be called once the hwmod has
932 * no further work. Returns -EINVAL if the hwmod is in the wrong
933 * state or returns 0.
935 static int _idle(struct omap_hwmod *oh)
937 if (oh->_state != _HWMOD_STATE_ENABLED) {
938 WARN(1, "omap_hwmod: %s: idle state can only be entered from "
939 "enabled state\n", oh->name);
943 pr_debug("omap_hwmod: %s: idling\n", oh->name);
947 _del_initiator_dep(oh, mpu_oh);
950 oh->_state = _HWMOD_STATE_IDLE;
956 * _shutdown - shutdown an omap_hwmod
957 * @oh: struct omap_hwmod *
959 * Shut down an omap_hwmod @oh. This should be called when the driver
960 * used for the hwmod is removed or unloaded or if the driver is not
961 * used by the system. Returns -EINVAL if the hwmod is in the wrong
962 * state or returns 0.
964 static int _shutdown(struct omap_hwmod *oh)
966 if (oh->_state != _HWMOD_STATE_IDLE &&
967 oh->_state != _HWMOD_STATE_ENABLED) {
968 WARN(1, "omap_hwmod: %s: disabled state can only be entered "
969 "from idle, or enabled state\n", oh->name);
973 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
977 _del_initiator_dep(oh, mpu_oh);
978 /* XXX what about the other system initiators here? DMA, tesla, d2d */
980 /* XXX Should this code also force-disable the optional clocks? */
982 /* XXX mux any associated balls to safe mode */
984 oh->_state = _HWMOD_STATE_DISABLED;
990 * _setup - do initial configuration of omap_hwmod
991 * @oh: struct omap_hwmod *
993 * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
994 * OCP_SYSCONFIG register. Must be called with omap_hwmod_mutex
995 * held. Returns -EINVAL if the hwmod is in the wrong state or returns
998 static int _setup(struct omap_hwmod *oh)
1000 struct omap_hwmod_ocp_if *os;
1006 /* Set iclk autoidle mode */
1007 if (oh->slaves_cnt > 0) {
1008 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
1009 struct clk *c = os->_clk;
1011 if (!c || IS_ERR(c))
1014 if (os->flags & OCPIF_SWSUP_IDLE) {
1015 /* XXX omap_iclk_deny_idle(c); */
1017 /* XXX omap_iclk_allow_idle(c); */
1023 oh->_state = _HWMOD_STATE_INITIALIZED;
1027 if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
1029 * XXX Do the OCP_SYSCONFIG bits need to be
1030 * reprogrammed after a reset? If not, then this can
1031 * be removed. If they do, then probably the
1032 * _enable() function should be split to avoid the
1033 * rewrite of the OCP_SYSCONFIG register.
1035 if (oh->class->sysc) {
1036 _update_sysc_cache(oh);
1041 if (!(oh->flags & HWMOD_INIT_NO_IDLE))
1049 /* Public functions */
1051 u32 omap_hwmod_readl(struct omap_hwmod *oh, u16 reg_offs)
1053 return __raw_readl(oh->_rt_va + reg_offs);
1056 void omap_hwmod_writel(u32 v, struct omap_hwmod *oh, u16 reg_offs)
1058 __raw_writel(v, oh->_rt_va + reg_offs);
1061 int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
1069 v = oh->_sysc_cache;
1071 retval = _set_slave_idlemode(oh, idlemode, &v);
1073 _write_sysconfig(v, oh);
1079 * omap_hwmod_register - register a struct omap_hwmod
1080 * @oh: struct omap_hwmod *
1082 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
1083 * already has been registered by the same name; -EINVAL if the
1084 * omap_hwmod is in the wrong state, if @oh is NULL, if the
1085 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
1086 * name, or if the omap_hwmod's class is missing a name; or 0 upon
1089 * XXX The data should be copied into bootmem, so the original data
1090 * should be marked __initdata and freed after init. This would allow
1091 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
1092 * that the copy process would be relatively complex due to the large number
1095 int omap_hwmod_register(struct omap_hwmod *oh)
1099 if (!oh || !oh->name || !oh->class || !oh->class->name ||
1100 (oh->_state != _HWMOD_STATE_UNKNOWN))
1103 mutex_lock(&omap_hwmod_mutex);
1105 pr_debug("omap_hwmod: %s: registering\n", oh->name);
1107 if (_lookup(oh->name)) {
1112 ms_id = _find_mpu_port_index(oh);
1113 if (!IS_ERR_VALUE(ms_id)) {
1114 oh->_mpu_port_index = ms_id;
1115 oh->_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
1117 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1120 list_add_tail(&oh->node, &omap_hwmod_list);
1122 oh->_state = _HWMOD_STATE_REGISTERED;
1127 mutex_unlock(&omap_hwmod_mutex);
1132 * omap_hwmod_lookup - look up a registered omap_hwmod by name
1133 * @name: name of the omap_hwmod to look up
1135 * Given a @name of an omap_hwmod, return a pointer to the registered
1136 * struct omap_hwmod *, or NULL upon error.
1138 struct omap_hwmod *omap_hwmod_lookup(const char *name)
1140 struct omap_hwmod *oh;
1145 mutex_lock(&omap_hwmod_mutex);
1147 mutex_unlock(&omap_hwmod_mutex);
1153 * omap_hwmod_for_each - call function for each registered omap_hwmod
1154 * @fn: pointer to a callback function
1156 * Call @fn for each registered omap_hwmod, passing @data to each
1157 * function. @fn must return 0 for success or any other value for
1158 * failure. If @fn returns non-zero, the iteration across omap_hwmods
1159 * will stop and the non-zero return value will be passed to the
1160 * caller of omap_hwmod_for_each(). @fn is called with
1161 * omap_hwmod_for_each() held.
1163 int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh))
1165 struct omap_hwmod *temp_oh;
1171 mutex_lock(&omap_hwmod_mutex);
1172 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1173 ret = (*fn)(temp_oh);
1177 mutex_unlock(&omap_hwmod_mutex);
1184 * omap_hwmod_init - init omap_hwmod code and register hwmods
1185 * @ohs: pointer to an array of omap_hwmods to register
1187 * Intended to be called early in boot before the clock framework is
1188 * initialized. If @ohs is not null, will register all omap_hwmods
1189 * listed in @ohs that are valid for this chip. Returns -EINVAL if
1190 * omap_hwmod_init() has already been called or 0 otherwise.
1192 int omap_hwmod_init(struct omap_hwmod **ohs)
1194 struct omap_hwmod *oh;
1207 if (omap_chip_is(oh->omap_chip)) {
1208 r = omap_hwmod_register(oh);
1209 WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
1210 "%d\n", oh->name, r);
1219 * omap_hwmod_late_init - do some post-clock framework initialization
1221 * Must be called after omap2_clk_init(). Resolves the struct clk names
1222 * to struct clk pointers for each registered omap_hwmod. Also calls
1223 * _setup() on each hwmod. Returns 0.
1225 int omap_hwmod_late_init(void)
1229 /* XXX check return value */
1230 r = omap_hwmod_for_each(_init_clocks);
1231 WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
1233 mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
1234 WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
1235 MPU_INITIATOR_NAME);
1237 omap_hwmod_for_each(_setup);
1243 * omap_hwmod_unregister - unregister an omap_hwmod
1244 * @oh: struct omap_hwmod *
1246 * Unregisters a previously-registered omap_hwmod @oh. There's probably
1247 * no use case for this, so it is likely to be removed in a later version.
1249 * XXX Free all of the bootmem-allocated structures here when that is
1250 * implemented. Make it clear that core code is the only code that is
1251 * expected to unregister modules.
1253 int omap_hwmod_unregister(struct omap_hwmod *oh)
1258 pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
1260 mutex_lock(&omap_hwmod_mutex);
1261 iounmap(oh->_rt_va);
1262 list_del(&oh->node);
1263 mutex_unlock(&omap_hwmod_mutex);
1269 * omap_hwmod_enable - enable an omap_hwmod
1270 * @oh: struct omap_hwmod *
1272 * Enable an omap_hwomd @oh. Intended to be called by omap_device_enable().
1273 * Returns -EINVAL on error or passes along the return value from _enable().
1275 int omap_hwmod_enable(struct omap_hwmod *oh)
1282 mutex_lock(&omap_hwmod_mutex);
1284 mutex_unlock(&omap_hwmod_mutex);
1290 * omap_hwmod_idle - idle an omap_hwmod
1291 * @oh: struct omap_hwmod *
1293 * Idle an omap_hwomd @oh. Intended to be called by omap_device_idle().
1294 * Returns -EINVAL on error or passes along the return value from _idle().
1296 int omap_hwmod_idle(struct omap_hwmod *oh)
1301 mutex_lock(&omap_hwmod_mutex);
1303 mutex_unlock(&omap_hwmod_mutex);
1309 * omap_hwmod_shutdown - shutdown an omap_hwmod
1310 * @oh: struct omap_hwmod *
1312 * Shutdown an omap_hwomd @oh. Intended to be called by
1313 * omap_device_shutdown(). Returns -EINVAL on error or passes along
1314 * the return value from _shutdown().
1316 int omap_hwmod_shutdown(struct omap_hwmod *oh)
1321 mutex_lock(&omap_hwmod_mutex);
1323 mutex_unlock(&omap_hwmod_mutex);
1329 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
1330 * @oh: struct omap_hwmod *oh
1332 * Intended to be called by the omap_device code.
1334 int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
1336 mutex_lock(&omap_hwmod_mutex);
1338 mutex_unlock(&omap_hwmod_mutex);
1344 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
1345 * @oh: struct omap_hwmod *oh
1347 * Intended to be called by the omap_device code.
1349 int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
1351 mutex_lock(&omap_hwmod_mutex);
1352 _disable_clocks(oh);
1353 mutex_unlock(&omap_hwmod_mutex);
1359 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
1360 * @oh: struct omap_hwmod *oh
1362 * Intended to be called by drivers and core code when all posted
1363 * writes to a device must complete before continuing further
1364 * execution (for example, after clearing some device IRQSTATUS
1367 * XXX what about targets with multiple OCP threads?
1369 void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
1373 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
1374 WARN(1, "omap_device: %s: OCP barrier impossible due to "
1375 "device configuration\n", oh->name);
1380 * Forces posted writes to complete on the OCP thread handling
1383 omap_hwmod_readl(oh, oh->class->sysc->sysc_offs);
1387 * omap_hwmod_reset - reset the hwmod
1388 * @oh: struct omap_hwmod *
1390 * Under some conditions, a driver may wish to reset the entire device.
1391 * Called from omap_device code. Returns -EINVAL on error or passes along
1392 * the return value from _reset()/_enable().
1394 int omap_hwmod_reset(struct omap_hwmod *oh)
1398 if (!oh || !(oh->_state & _HWMOD_STATE_ENABLED))
1401 mutex_lock(&omap_hwmod_mutex);
1405 mutex_unlock(&omap_hwmod_mutex);
1411 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
1412 * @oh: struct omap_hwmod *
1413 * @res: pointer to the first element of an array of struct resource to fill
1415 * Count the number of struct resource array elements necessary to
1416 * contain omap_hwmod @oh resources. Intended to be called by code
1417 * that registers omap_devices. Intended to be used to determine the
1418 * size of a dynamically-allocated struct resource array, before
1419 * calling omap_hwmod_fill_resources(). Returns the number of struct
1420 * resource array elements needed.
1422 * XXX This code is not optimized. It could attempt to merge adjacent
1426 int omap_hwmod_count_resources(struct omap_hwmod *oh)
1430 ret = oh->mpu_irqs_cnt + oh->sdma_chs_cnt;
1432 for (i = 0; i < oh->slaves_cnt; i++)
1433 ret += (*oh->slaves + i)->addr_cnt;
1439 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
1440 * @oh: struct omap_hwmod *
1441 * @res: pointer to the first element of an array of struct resource to fill
1443 * Fill the struct resource array @res with resource data from the
1444 * omap_hwmod @oh. Intended to be called by code that registers
1445 * omap_devices. See also omap_hwmod_count_resources(). Returns the
1446 * number of array elements filled.
1448 int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
1453 /* For each IRQ, DMA, memory area, fill in array.*/
1455 for (i = 0; i < oh->mpu_irqs_cnt; i++) {
1456 (res + r)->name = (oh->mpu_irqs + i)->name;
1457 (res + r)->start = (oh->mpu_irqs + i)->irq;
1458 (res + r)->end = (oh->mpu_irqs + i)->irq;
1459 (res + r)->flags = IORESOURCE_IRQ;
1463 for (i = 0; i < oh->sdma_chs_cnt; i++) {
1464 (res + r)->name = (oh->sdma_chs + i)->name;
1465 (res + r)->start = (oh->sdma_chs + i)->dma_ch;
1466 (res + r)->end = (oh->sdma_chs + i)->dma_ch;
1467 (res + r)->flags = IORESOURCE_DMA;
1471 for (i = 0; i < oh->slaves_cnt; i++) {
1472 struct omap_hwmod_ocp_if *os;
1474 os = *oh->slaves + i;
1476 for (j = 0; j < os->addr_cnt; j++) {
1477 (res + r)->start = (os->addr + j)->pa_start;
1478 (res + r)->end = (os->addr + j)->pa_end;
1479 (res + r)->flags = IORESOURCE_MEM;
1488 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
1489 * @oh: struct omap_hwmod *
1491 * Return the powerdomain pointer associated with the OMAP module
1492 * @oh's main clock. If @oh does not have a main clk, return the
1493 * powerdomain associated with the interface clock associated with the
1494 * module's MPU port. (XXX Perhaps this should use the SDMA port
1495 * instead?) Returns NULL on error, or a struct powerdomain * on
1498 struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
1508 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1510 c = oh->slaves[oh->_mpu_port_index]->_clk;
1516 return c->clkdm->pwrdm.ptr;
1521 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
1522 * @oh: struct omap_hwmod *
1523 * @init_oh: struct omap_hwmod * (initiator)
1525 * Add a sleep dependency between the initiator @init_oh and @oh.
1526 * Intended to be called by DSP/Bridge code via platform_data for the
1527 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1528 * code needs to add/del initiator dependencies dynamically
1529 * before/after accessing a device. Returns the return value from
1530 * _add_initiator_dep().
1532 * XXX Keep a usecount in the clockdomain code
1534 int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
1535 struct omap_hwmod *init_oh)
1537 return _add_initiator_dep(oh, init_oh);
1541 * XXX what about functions for drivers to save/restore ocp_sysconfig
1542 * for context save/restore operations?
1546 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
1547 * @oh: struct omap_hwmod *
1548 * @init_oh: struct omap_hwmod * (initiator)
1550 * Remove a sleep dependency between the initiator @init_oh and @oh.
1551 * Intended to be called by DSP/Bridge code via platform_data for the
1552 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1553 * code needs to add/del initiator dependencies dynamically
1554 * before/after accessing a device. Returns the return value from
1555 * _del_initiator_dep().
1557 * XXX Keep a usecount in the clockdomain code
1559 int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
1560 struct omap_hwmod *init_oh)
1562 return _del_initiator_dep(oh, init_oh);
1566 * omap_hwmod_enable_wakeup - allow device to wake up the system
1567 * @oh: struct omap_hwmod *
1569 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
1570 * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
1571 * registers to cause the PRCM to receive wakeup events from the
1572 * module. Does not set any wakeup routing registers beyond this
1573 * point - if the module is to wake up any other module or subsystem,
1574 * that must be set separately. Called by omap_device code. Returns
1575 * -EINVAL on error or 0 upon success.
1577 int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
1579 if (!oh->class->sysc ||
1580 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
1583 mutex_lock(&omap_hwmod_mutex);
1585 mutex_unlock(&omap_hwmod_mutex);
1591 * omap_hwmod_disable_wakeup - prevent device from waking the system
1592 * @oh: struct omap_hwmod *
1594 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
1595 * from sending wakeups to the PRCM. Eventually this should clear
1596 * PRCM wakeup registers to cause the PRCM to ignore wakeup events
1597 * from the module. Does not set any wakeup routing registers beyond
1598 * this point - if the module is to wake up any other module or
1599 * subsystem, that must be set separately. Called by omap_device
1600 * code. Returns -EINVAL on error or 0 upon success.
1602 int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
1604 if (!oh->class->sysc ||
1605 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
1608 mutex_lock(&omap_hwmod_mutex);
1609 _disable_wakeup(oh);
1610 mutex_unlock(&omap_hwmod_mutex);
1616 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
1617 * @classname: struct omap_hwmod_class name to search for
1618 * @fn: callback function pointer to call for each hwmod in class @classname
1619 * @user: arbitrary context data to pass to the callback function
1621 * For each omap_hwmod of class @classname, call @fn. Takes
1622 * omap_hwmod_mutex to prevent the hwmod list from changing during the
1623 * iteration. If the callback function returns something other than
1624 * zero, the iterator is terminated, and the callback function's return
1625 * value is passed back to the caller. Returns 0 upon success, -EINVAL
1626 * if @classname or @fn are NULL, or passes back the error code from @fn.
1628 int omap_hwmod_for_each_by_class(const char *classname,
1629 int (*fn)(struct omap_hwmod *oh,
1633 struct omap_hwmod *temp_oh;
1636 if (!classname || !fn)
1639 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
1640 __func__, classname);
1642 mutex_lock(&omap_hwmod_mutex);
1644 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1645 if (!strcmp(temp_oh->class->name, classname)) {
1646 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
1647 __func__, temp_oh->name);
1648 ret = (*fn)(temp_oh, user);
1654 mutex_unlock(&omap_hwmod_mutex);
1657 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",