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>
46 #include <linux/bootmem.h>
48 #include <plat/common.h>
50 #include <plat/clockdomain.h>
51 #include <plat/powerdomain.h>
52 #include <plat/clock.h>
53 #include <plat/omap_hwmod.h>
57 /* Maximum microseconds to wait for OMAP module to reset */
58 #define MAX_MODULE_RESET_WAIT 10000
60 /* Name of the OMAP hwmod for the MPU */
61 #define MPU_INITIATOR_NAME "mpu_hwmod"
63 /* omap_hwmod_list contains all registered struct omap_hwmods */
64 static LIST_HEAD(omap_hwmod_list);
66 static DEFINE_MUTEX(omap_hwmod_mutex);
68 /* mpu_oh: used to add/remove MPU initiator from sleepdep list */
69 static struct omap_hwmod *mpu_oh;
71 /* inited: 0 if omap_hwmod_init() has not yet been called; 1 otherwise */
75 /* Private functions */
78 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
79 * @oh: struct omap_hwmod *
81 * Load the current value of the hwmod OCP_SYSCONFIG register into the
82 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
83 * OCP_SYSCONFIG register or 0 upon success.
85 static int _update_sysc_cache(struct omap_hwmod *oh)
88 WARN(!oh->sysconfig, "omap_hwmod: %s: cannot read "
89 "OCP_SYSCONFIG: not defined on hwmod\n", oh->name);
93 /* XXX ensure module interface clock is up */
95 oh->_sysc_cache = omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
97 if (!(oh->sysconfig->sysc_flags & SYSC_NO_CACHE))
98 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
104 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
105 * @v: OCP_SYSCONFIG value to write
106 * @oh: struct omap_hwmod *
108 * Write @v into the module OCP_SYSCONFIG register, if it has one. No
111 static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
113 if (!oh->sysconfig) {
114 WARN(!oh->sysconfig, "omap_hwmod: %s: cannot write "
115 "OCP_SYSCONFIG: not defined on hwmod\n", oh->name);
119 /* XXX ensure module interface clock is up */
121 if (oh->_sysc_cache != v) {
123 omap_hwmod_writel(v, oh, oh->sysconfig->sysc_offs);
128 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
129 * @oh: struct omap_hwmod *
130 * @standbymode: MIDLEMODE field bits
131 * @v: pointer to register contents to modify
133 * Update the master standby mode bits in @v to be @standbymode for
134 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
135 * upon error or 0 upon success.
137 static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
143 if (!oh->sysconfig ||
144 !(oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE))
147 if (!oh->sysconfig->sysc_fields) {
148 WARN(!oh->sysconfig->sysc_fields, "offset struct for "
149 "sysconfig not provided!\n");
153 mstandby_shift = oh->sysconfig->sysc_fields->midle_shift;
154 mstandby_mask = (0x3 << mstandby_shift);
156 *v &= ~mstandby_mask;
157 *v |= __ffs(standbymode) << mstandby_shift;
163 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
164 * @oh: struct omap_hwmod *
165 * @idlemode: SIDLEMODE field bits
166 * @v: pointer to register contents to modify
168 * Update the slave idle mode bits in @v to be @idlemode for the @oh
169 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
172 static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
177 if (!oh->sysconfig ||
178 !(oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE))
181 if (!oh->sysconfig->sysc_fields) {
182 WARN(!oh->sysconfig->sysc_fields, "offset struct for "
183 "sysconfig not provided!\n");
187 sidle_shift = oh->sysconfig->sysc_fields->sidle_shift;
188 sidle_mask = (0x3 << sidle_shift);
191 *v |= __ffs(idlemode) << sidle_shift;
197 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
198 * @oh: struct omap_hwmod *
199 * @clockact: CLOCKACTIVITY field bits
200 * @v: pointer to register contents to modify
202 * Update the clockactivity mode bits in @v to be @clockact for the
203 * @oh hwmod. Used for additional powersaving on some modules. Does
204 * not write to the hardware. Returns -EINVAL upon error or 0 upon
207 static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
212 if (!oh->sysconfig ||
213 !(oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
216 if (!oh->sysconfig->sysc_fields) {
217 WARN(!oh->sysconfig->sysc_fields, "offset struct for "
218 "sysconfig not provided!\n");
222 clkact_shift = oh->sysconfig->sysc_fields->clkact_shift;
223 clkact_mask = (0x3 << clkact_shift);
226 *v |= clockact << clkact_shift;
232 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
233 * @oh: struct omap_hwmod *
234 * @v: pointer to register contents to modify
236 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
237 * error or 0 upon success.
239 static int _set_softreset(struct omap_hwmod *oh, u32 *v)
243 if (!oh->sysconfig ||
244 !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET))
247 if (!oh->sysconfig->sysc_fields) {
248 WARN(!oh->sysconfig->sysc_fields, "offset struct for "
249 "sysconfig not provided!\n");
253 softrst_mask = (0x1 << oh->sysconfig->sysc_fields->srst_shift);
261 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
262 * @oh: struct omap_hwmod *
263 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
264 * @v: pointer to register contents to modify
266 * Update the module autoidle bit in @v to be @autoidle for the @oh
267 * hwmod. The autoidle bit controls whether the module can gate
268 * internal clocks automatically when it isn't doing anything; the
269 * exact function of this bit varies on a per-module basis. This
270 * function does not write to the hardware. Returns -EINVAL upon
271 * error or 0 upon success.
273 static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
279 if (!oh->sysconfig ||
280 !(oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE))
283 if (!oh->sysconfig->sysc_fields) {
284 WARN(oh->sysconfig->sysc_fields, "offset struct for "
285 "sysconfig not provided!\n");
289 autoidle_shift = oh->sysconfig->sysc_fields->autoidle_shift;
290 autoidle_mask = (0x3 << autoidle_shift);
292 *v &= ~autoidle_mask;
293 *v |= autoidle << autoidle_shift;
299 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
300 * @oh: struct omap_hwmod *
302 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
303 * upon error or 0 upon success.
305 static int _enable_wakeup(struct omap_hwmod *oh)
309 if (!oh->sysconfig ||
310 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
313 if (!oh->sysconfig->sysc_fields) {
314 WARN(!oh->sysconfig->sysc_fields, "offset struct for "
315 "sysconfig not provided!\n");
319 wakeup_mask = (0x1 << oh->sysconfig->sysc_fields->enwkup_shift);
323 _write_sysconfig(v, oh);
325 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
327 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
333 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
334 * @oh: struct omap_hwmod *
336 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
337 * upon error or 0 upon success.
339 static int _disable_wakeup(struct omap_hwmod *oh)
343 if (!oh->sysconfig ||
344 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
347 if (!oh->sysconfig->sysc_fields) {
348 WARN(!oh->sysconfig->sysc_fields, "offset struct for "
349 "sysconfig not provided!\n");
353 wakeup_mask = (0x1 << oh->sysconfig->sysc_fields->enwkup_shift);
357 _write_sysconfig(v, oh);
359 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
361 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
367 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
368 * @oh: struct omap_hwmod *
370 * Prevent the hardware module @oh from entering idle while the
371 * hardare module initiator @init_oh is active. Useful when a module
372 * will be accessed by a particular initiator (e.g., if a module will
373 * be accessed by the IVA, there should be a sleepdep between the IVA
374 * initiator and the module). Only applies to modules in smart-idle
375 * mode. Returns -EINVAL upon error or passes along
376 * clkdm_add_sleepdep() value upon success.
378 static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
383 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
387 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
388 * @oh: struct omap_hwmod *
390 * Allow the hardware module @oh to enter idle while the hardare
391 * module initiator @init_oh is active. Useful when a module will not
392 * be accessed by a particular initiator (e.g., if a module will not
393 * be accessed by the IVA, there should be no sleepdep between the IVA
394 * initiator and the module). Only applies to modules in smart-idle
395 * mode. Returns -EINVAL upon error or passes along
396 * clkdm_del_sleepdep() value upon success.
398 static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
403 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
407 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
408 * @oh: struct omap_hwmod *
410 * Called from _init_clocks(). Populates the @oh _clk (main
411 * functional clock pointer) if a main_clk is present. Returns 0 on
412 * success or -EINVAL on error.
414 static int _init_main_clk(struct omap_hwmod *oh)
419 if (!oh->clkdev_con_id)
422 c = clk_get_sys(oh->clkdev_dev_id, oh->clkdev_con_id);
423 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get main_clk %s.%s\n",
424 oh->name, oh->clkdev_dev_id, oh->clkdev_con_id);
429 WARN(!c->clkdm, "omap_hwmod: %s: missing clockdomain for %s.\n",
430 oh->clkdev_con_id, c->name);
436 * _init_interface_clk - get a struct clk * for the the hwmod's interface clks
437 * @oh: struct omap_hwmod *
439 * Called from _init_clocks(). Populates the @oh OCP slave interface
440 * clock pointers. Returns 0 on success or -EINVAL on error.
442 static int _init_interface_clks(struct omap_hwmod *oh)
444 struct omap_hwmod_ocp_if *os;
449 if (oh->slaves_cnt == 0)
452 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
453 if (!os->clkdev_con_id)
456 c = clk_get_sys(os->clkdev_dev_id, os->clkdev_con_id);
457 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get "
458 "interface_clk %s.%s\n", oh->name,
459 os->clkdev_dev_id, os->clkdev_con_id);
469 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
470 * @oh: struct omap_hwmod *
472 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
473 * clock pointers. Returns 0 on success or -EINVAL on error.
475 static int _init_opt_clks(struct omap_hwmod *oh)
477 struct omap_hwmod_opt_clk *oc;
482 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
483 c = clk_get_sys(oc->clkdev_dev_id, oc->clkdev_con_id);
484 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get opt_clk "
485 "%s.%s\n", oh->name, oc->clkdev_dev_id,
496 * _enable_clocks - enable hwmod main clock and interface clocks
497 * @oh: struct omap_hwmod *
499 * Enables all clocks necessary for register reads and writes to succeed
500 * on the hwmod @oh. Returns 0.
502 static int _enable_clocks(struct omap_hwmod *oh)
504 struct omap_hwmod_ocp_if *os;
507 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
509 if (oh->_clk && !IS_ERR(oh->_clk))
510 clk_enable(oh->_clk);
512 if (oh->slaves_cnt > 0) {
513 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
514 struct clk *c = os->_clk;
516 if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
521 /* The opt clocks are controlled by the device driver. */
527 * _disable_clocks - disable hwmod main clock and interface clocks
528 * @oh: struct omap_hwmod *
530 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
532 static int _disable_clocks(struct omap_hwmod *oh)
534 struct omap_hwmod_ocp_if *os;
537 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
539 if (oh->_clk && !IS_ERR(oh->_clk))
540 clk_disable(oh->_clk);
542 if (oh->slaves_cnt > 0) {
543 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
544 struct clk *c = os->_clk;
546 if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
551 /* The opt clocks are controlled by the device driver. */
557 * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
558 * @oh: struct omap_hwmod *
560 * Returns the array index of the OCP slave port that the MPU
561 * addresses the device on, or -EINVAL upon error or not found.
563 static int _find_mpu_port_index(struct omap_hwmod *oh)
565 struct omap_hwmod_ocp_if *os;
569 if (!oh || oh->slaves_cnt == 0)
572 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
573 if (os->user & OCP_USER_MPU) {
580 pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
583 pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
586 return (found) ? i : -EINVAL;
590 * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
591 * @oh: struct omap_hwmod *
593 * Return the virtual address of the base of the register target of
594 * device @oh, or NULL on error.
596 static void __iomem *_find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
598 struct omap_hwmod_ocp_if *os;
599 struct omap_hwmod_addr_space *mem;
602 void __iomem *va_start;
604 if (!oh || oh->slaves_cnt == 0)
607 os = *oh->slaves + index;
609 for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
610 if (mem->flags & ADDR_TYPE_RT) {
617 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
619 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
622 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
625 pr_debug("omap_hwmod: %s: no MPU register target found\n",
629 return (found) ? va_start : NULL;
633 * _sysc_enable - try to bring a module out of idle via OCP_SYSCONFIG
634 * @oh: struct omap_hwmod *
636 * If module is marked as SWSUP_SIDLE, force the module out of slave
637 * idle; otherwise, configure it for smart-idle. If module is marked
638 * as SWSUP_MSUSPEND, force the module out of master standby;
639 * otherwise, configure it for smart-standby. No return value.
641 static void _sysc_enable(struct omap_hwmod *oh)
651 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
652 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
653 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
654 _set_slave_idlemode(oh, idlemode, &v);
657 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
658 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
659 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
660 _set_master_standbymode(oh, idlemode, &v);
663 if (oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE) {
664 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
666 _set_module_autoidle(oh, idlemode, &v);
669 /* XXX OCP ENAWAKEUP bit? */
672 * XXX The clock framework should handle this, by
673 * calling into this code. But this must wait until the
674 * clock structures are tagged with omap_hwmod entries
676 if (oh->flags & HWMOD_SET_DEFAULT_CLOCKACT &&
677 oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY)
678 _set_clockactivity(oh, oh->sysconfig->clockact, &v);
680 _write_sysconfig(v, oh);
684 * _sysc_idle - try to put a module into idle via OCP_SYSCONFIG
685 * @oh: struct omap_hwmod *
687 * If module is marked as SWSUP_SIDLE, force the module into slave
688 * idle; otherwise, configure it for smart-idle. If module is marked
689 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
690 * configure it for smart-standby. No return value.
692 static void _sysc_idle(struct omap_hwmod *oh)
702 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
703 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
704 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
705 _set_slave_idlemode(oh, idlemode, &v);
708 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
709 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
710 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
711 _set_master_standbymode(oh, idlemode, &v);
714 _write_sysconfig(v, oh);
718 * _sysc_shutdown - force a module into idle via OCP_SYSCONFIG
719 * @oh: struct omap_hwmod *
721 * Force the module into slave idle and master suspend. No return
724 static void _sysc_shutdown(struct omap_hwmod *oh)
733 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE)
734 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
736 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE)
737 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
739 if (oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE)
740 _set_module_autoidle(oh, 1, &v);
742 _write_sysconfig(v, oh);
746 * _lookup - find an omap_hwmod by name
747 * @name: find an omap_hwmod by name
749 * Return a pointer to an omap_hwmod by name, or NULL if not found.
750 * Caller must hold omap_hwmod_mutex.
752 static struct omap_hwmod *_lookup(const char *name)
754 struct omap_hwmod *oh, *temp_oh;
758 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
759 if (!strcmp(name, temp_oh->name)) {
769 * _init_clocks - clk_get() all clocks associated with this hwmod
770 * @oh: struct omap_hwmod *
772 * Called by omap_hwmod_late_init() (after omap2_clk_init()).
773 * Resolves all clock names embedded in the hwmod. Must be called
774 * with omap_hwmod_mutex held. Returns -EINVAL if the omap_hwmod
775 * has not yet been registered or if the clocks have already been
776 * initialized, 0 on success, or a non-zero error on failure.
778 static int _init_clocks(struct omap_hwmod *oh)
782 if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
785 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
787 ret |= _init_main_clk(oh);
788 ret |= _init_interface_clks(oh);
789 ret |= _init_opt_clks(oh);
791 oh->_state = _HWMOD_STATE_CLKS_INITED;
797 * _wait_target_ready - wait for a module to leave slave idle
798 * @oh: struct omap_hwmod *
800 * Wait for a module @oh to leave slave idle. Returns 0 if the module
801 * does not have an IDLEST bit or if the module successfully leaves
802 * slave idle; otherwise, pass along the return value of the
803 * appropriate *_cm_wait_module_ready() function.
805 static int _wait_target_ready(struct omap_hwmod *oh)
807 struct omap_hwmod_ocp_if *os;
813 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
816 os = *oh->slaves + oh->_mpu_port_index;
818 if (!(os->flags & OCPIF_HAS_IDLEST))
821 /* XXX check module SIDLEMODE */
823 /* XXX check clock enable states */
825 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
826 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
827 oh->prcm.omap2.idlest_reg_id,
828 oh->prcm.omap2.idlest_idle_bit);
830 } else if (cpu_is_omap44xx()) {
831 ret = omap4_cm_wait_module_ready(oh->prcm.omap4.module_offs,
832 oh->prcm.omap4.device_offs);
842 * _reset - reset an omap_hwmod
843 * @oh: struct omap_hwmod *
845 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
846 * enabled for this to work. Must be called with omap_hwmod_mutex
847 * held. Returns -EINVAL if the hwmod cannot be reset this way or if
848 * the hwmod is in the wrong state, -ETIMEDOUT if the module did not
849 * reset in time, or 0 upon success.
851 static int _reset(struct omap_hwmod *oh)
856 if (!oh->sysconfig ||
857 !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET) ||
858 (oh->sysconfig->sysc_flags & SYSS_MISSING))
861 /* clocks must be on for this operation */
862 if (oh->_state != _HWMOD_STATE_ENABLED) {
863 WARN(1, "omap_hwmod: %s: reset can only be entered from "
864 "enabled state\n", oh->name);
868 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
871 r = _set_softreset(oh, &v);
874 _write_sysconfig(v, oh);
876 omap_test_timeout((omap_hwmod_readl(oh, oh->sysconfig->syss_offs) &
877 SYSS_RESETDONE_MASK),
878 MAX_MODULE_RESET_WAIT, c);
880 if (c == MAX_MODULE_RESET_WAIT)
881 WARN(1, "omap_hwmod: %s: failed to reset in %d usec\n",
882 oh->name, MAX_MODULE_RESET_WAIT);
884 pr_debug("omap_hwmod: %s: reset in %d usec\n", oh->name, c);
887 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
888 * _wait_target_ready() or _reset()
891 return (c == MAX_MODULE_RESET_WAIT) ? -ETIMEDOUT : 0;
895 * _enable - enable an omap_hwmod
896 * @oh: struct omap_hwmod *
898 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
899 * register target. Must be called with omap_hwmod_mutex held.
900 * Returns -EINVAL if the hwmod is in the wrong state or passes along
901 * the return value of _wait_target_ready().
903 static int _enable(struct omap_hwmod *oh)
907 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
908 oh->_state != _HWMOD_STATE_IDLE &&
909 oh->_state != _HWMOD_STATE_DISABLED) {
910 WARN(1, "omap_hwmod: %s: enabled state can only be entered "
911 "from initialized, idle, or disabled state\n", oh->name);
915 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
919 _add_initiator_dep(oh, mpu_oh);
923 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
924 _update_sysc_cache(oh);
928 r = _wait_target_ready(oh);
930 oh->_state = _HWMOD_STATE_ENABLED;
936 * _idle - idle an omap_hwmod
937 * @oh: struct omap_hwmod *
939 * Idles an omap_hwmod @oh. This should be called once the hwmod has
940 * no further work. Returns -EINVAL if the hwmod is in the wrong
941 * state or returns 0.
943 static int _idle(struct omap_hwmod *oh)
945 if (oh->_state != _HWMOD_STATE_ENABLED) {
946 WARN(1, "omap_hwmod: %s: idle state can only be entered from "
947 "enabled state\n", oh->name);
951 pr_debug("omap_hwmod: %s: idling\n", oh->name);
955 _del_initiator_dep(oh, mpu_oh);
958 oh->_state = _HWMOD_STATE_IDLE;
964 * _shutdown - shutdown an omap_hwmod
965 * @oh: struct omap_hwmod *
967 * Shut down an omap_hwmod @oh. This should be called when the driver
968 * used for the hwmod is removed or unloaded or if the driver is not
969 * used by the system. Returns -EINVAL if the hwmod is in the wrong
970 * state or returns 0.
972 static int _shutdown(struct omap_hwmod *oh)
974 if (oh->_state != _HWMOD_STATE_IDLE &&
975 oh->_state != _HWMOD_STATE_ENABLED) {
976 WARN(1, "omap_hwmod: %s: disabled state can only be entered "
977 "from idle, or enabled state\n", oh->name);
981 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
985 _del_initiator_dep(oh, mpu_oh);
986 /* XXX what about the other system initiators here? DMA, tesla, d2d */
988 /* XXX Should this code also force-disable the optional clocks? */
990 /* XXX mux any associated balls to safe mode */
992 oh->_state = _HWMOD_STATE_DISABLED;
998 * _setup - do initial configuration of omap_hwmod
999 * @oh: struct omap_hwmod *
1001 * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
1002 * OCP_SYSCONFIG register. Must be called with omap_hwmod_mutex
1003 * held. Returns -EINVAL if the hwmod is in the wrong state or returns
1006 static int _setup(struct omap_hwmod *oh)
1008 struct omap_hwmod_ocp_if *os;
1014 /* Set iclk autoidle mode */
1015 if (oh->slaves_cnt > 0) {
1016 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
1017 struct clk *c = os->_clk;
1019 if (!c || IS_ERR(c))
1022 if (os->flags & OCPIF_SWSUP_IDLE) {
1023 /* XXX omap_iclk_deny_idle(c); */
1025 /* XXX omap_iclk_allow_idle(c); */
1031 oh->_state = _HWMOD_STATE_INITIALIZED;
1035 if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
1037 * XXX Do the OCP_SYSCONFIG bits need to be
1038 * reprogrammed after a reset? If not, then this can
1039 * be removed. If they do, then probably the
1040 * _enable() function should be split to avoid the
1041 * rewrite of the OCP_SYSCONFIG register.
1043 if (oh->sysconfig) {
1044 _update_sysc_cache(oh);
1049 if (!(oh->flags & HWMOD_INIT_NO_IDLE))
1057 /* Public functions */
1059 u32 omap_hwmod_readl(struct omap_hwmod *oh, u16 reg_offs)
1061 return __raw_readl(oh->_rt_va + reg_offs);
1064 void omap_hwmod_writel(u32 v, struct omap_hwmod *oh, u16 reg_offs)
1066 __raw_writel(v, oh->_rt_va + reg_offs);
1069 int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
1077 v = oh->_sysc_cache;
1079 retval = _set_slave_idlemode(oh, idlemode, &v);
1081 _write_sysconfig(v, oh);
1087 * omap_hwmod_register - register a struct omap_hwmod
1088 * @oh: struct omap_hwmod *
1090 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod already
1091 * has been registered by the same name; -EINVAL if the omap_hwmod is in the
1092 * wrong state, or 0 on success.
1094 * XXX The data should be copied into bootmem, so the original data
1095 * should be marked __initdata and freed after init. This would allow
1096 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
1097 * that the copy process would be relatively complex due to the large number
1100 int omap_hwmod_register(struct omap_hwmod *oh)
1104 if (!oh || (oh->_state != _HWMOD_STATE_UNKNOWN))
1107 mutex_lock(&omap_hwmod_mutex);
1109 pr_debug("omap_hwmod: %s: registering\n", oh->name);
1111 if (_lookup(oh->name)) {
1116 ms_id = _find_mpu_port_index(oh);
1117 if (!IS_ERR_VALUE(ms_id)) {
1118 oh->_mpu_port_index = ms_id;
1119 oh->_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
1121 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1124 list_add_tail(&oh->node, &omap_hwmod_list);
1126 oh->_state = _HWMOD_STATE_REGISTERED;
1131 mutex_unlock(&omap_hwmod_mutex);
1136 * omap_hwmod_lookup - look up a registered omap_hwmod by name
1137 * @name: name of the omap_hwmod to look up
1139 * Given a @name of an omap_hwmod, return a pointer to the registered
1140 * struct omap_hwmod *, or NULL upon error.
1142 struct omap_hwmod *omap_hwmod_lookup(const char *name)
1144 struct omap_hwmod *oh;
1149 mutex_lock(&omap_hwmod_mutex);
1151 mutex_unlock(&omap_hwmod_mutex);
1157 * omap_hwmod_for_each - call function for each registered omap_hwmod
1158 * @fn: pointer to a callback function
1160 * Call @fn for each registered omap_hwmod, passing @data to each
1161 * function. @fn must return 0 for success or any other value for
1162 * failure. If @fn returns non-zero, the iteration across omap_hwmods
1163 * will stop and the non-zero return value will be passed to the
1164 * caller of omap_hwmod_for_each(). @fn is called with
1165 * omap_hwmod_for_each() held.
1167 int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh))
1169 struct omap_hwmod *temp_oh;
1175 mutex_lock(&omap_hwmod_mutex);
1176 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1177 ret = (*fn)(temp_oh);
1181 mutex_unlock(&omap_hwmod_mutex);
1188 * omap_hwmod_init - init omap_hwmod code and register hwmods
1189 * @ohs: pointer to an array of omap_hwmods to register
1191 * Intended to be called early in boot before the clock framework is
1192 * initialized. If @ohs is not null, will register all omap_hwmods
1193 * listed in @ohs that are valid for this chip. Returns -EINVAL if
1194 * omap_hwmod_init() has already been called or 0 otherwise.
1196 int omap_hwmod_init(struct omap_hwmod **ohs)
1198 struct omap_hwmod *oh;
1211 if (omap_chip_is(oh->omap_chip)) {
1212 r = omap_hwmod_register(oh);
1213 WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
1214 "%d\n", oh->name, r);
1223 * omap_hwmod_late_init - do some post-clock framework initialization
1225 * Must be called after omap2_clk_init(). Resolves the struct clk names
1226 * to struct clk pointers for each registered omap_hwmod. Also calls
1227 * _setup() on each hwmod. Returns 0.
1229 int omap_hwmod_late_init(void)
1233 /* XXX check return value */
1234 r = omap_hwmod_for_each(_init_clocks);
1235 WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
1237 mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
1238 WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
1239 MPU_INITIATOR_NAME);
1241 omap_hwmod_for_each(_setup);
1247 * omap_hwmod_unregister - unregister an omap_hwmod
1248 * @oh: struct omap_hwmod *
1250 * Unregisters a previously-registered omap_hwmod @oh. There's probably
1251 * no use case for this, so it is likely to be removed in a later version.
1253 * XXX Free all of the bootmem-allocated structures here when that is
1254 * implemented. Make it clear that core code is the only code that is
1255 * expected to unregister modules.
1257 int omap_hwmod_unregister(struct omap_hwmod *oh)
1262 pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
1264 mutex_lock(&omap_hwmod_mutex);
1265 iounmap(oh->_rt_va);
1266 list_del(&oh->node);
1267 mutex_unlock(&omap_hwmod_mutex);
1273 * omap_hwmod_enable - enable an omap_hwmod
1274 * @oh: struct omap_hwmod *
1276 * Enable an omap_hwomd @oh. Intended to be called by omap_device_enable().
1277 * Returns -EINVAL on error or passes along the return value from _enable().
1279 int omap_hwmod_enable(struct omap_hwmod *oh)
1286 mutex_lock(&omap_hwmod_mutex);
1288 mutex_unlock(&omap_hwmod_mutex);
1294 * omap_hwmod_idle - idle an omap_hwmod
1295 * @oh: struct omap_hwmod *
1297 * Idle an omap_hwomd @oh. Intended to be called by omap_device_idle().
1298 * Returns -EINVAL on error or passes along the return value from _idle().
1300 int omap_hwmod_idle(struct omap_hwmod *oh)
1305 mutex_lock(&omap_hwmod_mutex);
1307 mutex_unlock(&omap_hwmod_mutex);
1313 * omap_hwmod_shutdown - shutdown an omap_hwmod
1314 * @oh: struct omap_hwmod *
1316 * Shutdown an omap_hwomd @oh. Intended to be called by
1317 * omap_device_shutdown(). Returns -EINVAL on error or passes along
1318 * the return value from _shutdown().
1320 int omap_hwmod_shutdown(struct omap_hwmod *oh)
1325 mutex_lock(&omap_hwmod_mutex);
1327 mutex_unlock(&omap_hwmod_mutex);
1333 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
1334 * @oh: struct omap_hwmod *oh
1336 * Intended to be called by the omap_device code.
1338 int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
1340 mutex_lock(&omap_hwmod_mutex);
1342 mutex_unlock(&omap_hwmod_mutex);
1348 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
1349 * @oh: struct omap_hwmod *oh
1351 * Intended to be called by the omap_device code.
1353 int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
1355 mutex_lock(&omap_hwmod_mutex);
1356 _disable_clocks(oh);
1357 mutex_unlock(&omap_hwmod_mutex);
1363 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
1364 * @oh: struct omap_hwmod *oh
1366 * Intended to be called by drivers and core code when all posted
1367 * writes to a device must complete before continuing further
1368 * execution (for example, after clearing some device IRQSTATUS
1371 * XXX what about targets with multiple OCP threads?
1373 void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
1377 if (!oh->sysconfig || !oh->sysconfig->sysc_flags) {
1378 WARN(1, "omap_device: %s: OCP barrier impossible due to "
1379 "device configuration\n", oh->name);
1384 * Forces posted writes to complete on the OCP thread handling
1387 omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
1391 * omap_hwmod_reset - reset the hwmod
1392 * @oh: struct omap_hwmod *
1394 * Under some conditions, a driver may wish to reset the entire device.
1395 * Called from omap_device code. Returns -EINVAL on error or passes along
1396 * the return value from _reset()/_enable().
1398 int omap_hwmod_reset(struct omap_hwmod *oh)
1402 if (!oh || !(oh->_state & _HWMOD_STATE_ENABLED))
1405 mutex_lock(&omap_hwmod_mutex);
1409 mutex_unlock(&omap_hwmod_mutex);
1415 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
1416 * @oh: struct omap_hwmod *
1417 * @res: pointer to the first element of an array of struct resource to fill
1419 * Count the number of struct resource array elements necessary to
1420 * contain omap_hwmod @oh resources. Intended to be called by code
1421 * that registers omap_devices. Intended to be used to determine the
1422 * size of a dynamically-allocated struct resource array, before
1423 * calling omap_hwmod_fill_resources(). Returns the number of struct
1424 * resource array elements needed.
1426 * XXX This code is not optimized. It could attempt to merge adjacent
1430 int omap_hwmod_count_resources(struct omap_hwmod *oh)
1434 ret = oh->mpu_irqs_cnt + oh->sdma_chs_cnt;
1436 for (i = 0; i < oh->slaves_cnt; i++)
1437 ret += (*oh->slaves + i)->addr_cnt;
1443 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
1444 * @oh: struct omap_hwmod *
1445 * @res: pointer to the first element of an array of struct resource to fill
1447 * Fill the struct resource array @res with resource data from the
1448 * omap_hwmod @oh. Intended to be called by code that registers
1449 * omap_devices. See also omap_hwmod_count_resources(). Returns the
1450 * number of array elements filled.
1452 int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
1457 /* For each IRQ, DMA, memory area, fill in array.*/
1459 for (i = 0; i < oh->mpu_irqs_cnt; i++) {
1460 (res + r)->name = (oh->mpu_irqs + i)->name;
1461 (res + r)->start = (oh->mpu_irqs + i)->irq;
1462 (res + r)->end = (oh->mpu_irqs + i)->irq;
1463 (res + r)->flags = IORESOURCE_IRQ;
1467 for (i = 0; i < oh->sdma_chs_cnt; i++) {
1468 (res + r)->name = (oh->sdma_chs + i)->name;
1469 (res + r)->start = (oh->sdma_chs + i)->dma_ch;
1470 (res + r)->end = (oh->sdma_chs + i)->dma_ch;
1471 (res + r)->flags = IORESOURCE_DMA;
1475 for (i = 0; i < oh->slaves_cnt; i++) {
1476 struct omap_hwmod_ocp_if *os;
1478 os = *oh->slaves + i;
1480 for (j = 0; j < os->addr_cnt; j++) {
1481 (res + r)->start = (os->addr + j)->pa_start;
1482 (res + r)->end = (os->addr + j)->pa_end;
1483 (res + r)->flags = IORESOURCE_MEM;
1492 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
1493 * @oh: struct omap_hwmod *
1495 * Return the powerdomain pointer associated with the OMAP module
1496 * @oh's main clock. If @oh does not have a main clk, return the
1497 * powerdomain associated with the interface clock associated with the
1498 * module's MPU port. (XXX Perhaps this should use the SDMA port
1499 * instead?) Returns NULL on error, or a struct powerdomain * on
1502 struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
1512 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1514 c = oh->slaves[oh->_mpu_port_index]->_clk;
1517 return c->clkdm->pwrdm.ptr;
1522 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
1523 * @oh: struct omap_hwmod *
1524 * @init_oh: struct omap_hwmod * (initiator)
1526 * Add a sleep dependency between the initiator @init_oh and @oh.
1527 * Intended to be called by DSP/Bridge code via platform_data for the
1528 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1529 * code needs to add/del initiator dependencies dynamically
1530 * before/after accessing a device. Returns the return value from
1531 * _add_initiator_dep().
1533 * XXX Keep a usecount in the clockdomain code
1535 int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
1536 struct omap_hwmod *init_oh)
1538 return _add_initiator_dep(oh, init_oh);
1542 * XXX what about functions for drivers to save/restore ocp_sysconfig
1543 * for context save/restore operations?
1547 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
1548 * @oh: struct omap_hwmod *
1549 * @init_oh: struct omap_hwmod * (initiator)
1551 * Remove a sleep dependency between the initiator @init_oh and @oh.
1552 * Intended to be called by DSP/Bridge code via platform_data for the
1553 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1554 * code needs to add/del initiator dependencies dynamically
1555 * before/after accessing a device. Returns the return value from
1556 * _del_initiator_dep().
1558 * XXX Keep a usecount in the clockdomain code
1560 int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
1561 struct omap_hwmod *init_oh)
1563 return _del_initiator_dep(oh, init_oh);
1567 * omap_hwmod_enable_wakeup - allow device to wake up the system
1568 * @oh: struct omap_hwmod *
1570 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
1571 * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
1572 * registers to cause the PRCM to receive wakeup events from the
1573 * module. Does not set any wakeup routing registers beyond this
1574 * point - if the module is to wake up any other module or subsystem,
1575 * that must be set separately. Called by omap_device code. Returns
1576 * -EINVAL on error or 0 upon success.
1578 int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
1580 if (!oh->sysconfig ||
1581 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
1584 mutex_lock(&omap_hwmod_mutex);
1586 mutex_unlock(&omap_hwmod_mutex);
1592 * omap_hwmod_disable_wakeup - prevent device from waking the system
1593 * @oh: struct omap_hwmod *
1595 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
1596 * from sending wakeups to the PRCM. Eventually this should clear
1597 * PRCM wakeup registers to cause the PRCM to ignore wakeup events
1598 * from the module. Does not set any wakeup routing registers beyond
1599 * this point - if the module is to wake up any other module or
1600 * subsystem, that must be set separately. Called by omap_device
1601 * code. Returns -EINVAL on error or 0 upon success.
1603 int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
1605 if (!oh->sysconfig ||
1606 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
1609 mutex_lock(&omap_hwmod_mutex);
1610 _disable_wakeup(oh);
1611 mutex_unlock(&omap_hwmod_mutex);