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 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
103 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
104 * @v: OCP_SYSCONFIG value to write
105 * @oh: struct omap_hwmod *
107 * Write @v into the module OCP_SYSCONFIG register, if it has one. No
110 static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
112 if (!oh->sysconfig) {
113 WARN(!oh->sysconfig, "omap_hwmod: %s: cannot write "
114 "OCP_SYSCONFIG: not defined on hwmod\n", oh->name);
118 /* XXX ensure module interface clock is up */
120 if (oh->_sysc_cache != v) {
122 omap_hwmod_writel(v, oh, oh->sysconfig->sysc_offs);
127 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
128 * @oh: struct omap_hwmod *
129 * @standbymode: MIDLEMODE field bits
130 * @v: pointer to register contents to modify
132 * Update the master standby mode bits in @v to be @standbymode for
133 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
134 * upon error or 0 upon success.
136 static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
139 if (!oh->sysconfig ||
140 !(oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE))
143 *v &= ~SYSC_MIDLEMODE_MASK;
144 *v |= __ffs(standbymode) << SYSC_MIDLEMODE_SHIFT;
150 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
151 * @oh: struct omap_hwmod *
152 * @idlemode: SIDLEMODE field bits
153 * @v: pointer to register contents to modify
155 * Update the slave idle mode bits in @v to be @idlemode for the @oh
156 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
159 static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
161 if (!oh->sysconfig ||
162 !(oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE))
165 *v &= ~SYSC_SIDLEMODE_MASK;
166 *v |= __ffs(idlemode) << SYSC_SIDLEMODE_SHIFT;
172 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
173 * @oh: struct omap_hwmod *
174 * @clockact: CLOCKACTIVITY field bits
175 * @v: pointer to register contents to modify
177 * Update the clockactivity mode bits in @v to be @clockact for the
178 * @oh hwmod. Used for additional powersaving on some modules. Does
179 * not write to the hardware. Returns -EINVAL upon error or 0 upon
182 static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
184 if (!oh->sysconfig ||
185 !(oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
188 *v &= ~SYSC_CLOCKACTIVITY_MASK;
189 *v |= clockact << SYSC_CLOCKACTIVITY_SHIFT;
195 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
196 * @oh: struct omap_hwmod *
197 * @v: pointer to register contents to modify
199 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
200 * error or 0 upon success.
202 static int _set_softreset(struct omap_hwmod *oh, u32 *v)
204 if (!oh->sysconfig ||
205 !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET))
208 *v |= SYSC_SOFTRESET_MASK;
214 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
215 * @oh: struct omap_hwmod *
217 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
218 * upon error or 0 upon success.
220 static int _enable_wakeup(struct omap_hwmod *oh)
224 if (!oh->sysconfig ||
225 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
229 v |= SYSC_ENAWAKEUP_MASK;
230 _write_sysconfig(v, oh);
232 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
234 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
240 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
241 * @oh: struct omap_hwmod *
243 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
244 * upon error or 0 upon success.
246 static int _disable_wakeup(struct omap_hwmod *oh)
250 if (!oh->sysconfig ||
251 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
255 v &= ~SYSC_ENAWAKEUP_MASK;
256 _write_sysconfig(v, oh);
258 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
260 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
266 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
267 * @oh: struct omap_hwmod *
269 * Prevent the hardware module @oh from entering idle while the
270 * hardare module initiator @init_oh is active. Useful when a module
271 * will be accessed by a particular initiator (e.g., if a module will
272 * be accessed by the IVA, there should be a sleepdep between the IVA
273 * initiator and the module). Only applies to modules in smart-idle
274 * mode. Returns -EINVAL upon error or passes along
275 * pwrdm_add_sleepdep() value upon success.
277 static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
282 return pwrdm_add_sleepdep(oh->_clk->clkdm->pwrdm.ptr,
283 init_oh->_clk->clkdm->pwrdm.ptr);
287 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
288 * @oh: struct omap_hwmod *
290 * Allow the hardware module @oh to enter idle while the hardare
291 * module initiator @init_oh is active. Useful when a module will not
292 * be accessed by a particular initiator (e.g., if a module will not
293 * be accessed by the IVA, there should be no sleepdep between the IVA
294 * initiator and the module). Only applies to modules in smart-idle
295 * mode. Returns -EINVAL upon error or passes along
296 * pwrdm_add_sleepdep() value upon success.
298 static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
303 return pwrdm_del_sleepdep(oh->_clk->clkdm->pwrdm.ptr,
304 init_oh->_clk->clkdm->pwrdm.ptr);
308 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
309 * @oh: struct omap_hwmod *
311 * Called from _init_clocks(). Populates the @oh _clk (main
312 * functional clock pointer) if a main_clk is present. Returns 0 on
313 * success or -EINVAL on error.
315 static int _init_main_clk(struct omap_hwmod *oh)
320 if (!oh->clkdev_con_id)
323 c = clk_get_sys(oh->clkdev_dev_id, oh->clkdev_con_id);
324 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get main_clk %s.%s\n",
325 oh->name, oh->clkdev_dev_id, oh->clkdev_con_id);
334 * _init_interface_clk - get a struct clk * for the the hwmod's interface clks
335 * @oh: struct omap_hwmod *
337 * Called from _init_clocks(). Populates the @oh OCP slave interface
338 * clock pointers. Returns 0 on success or -EINVAL on error.
340 static int _init_interface_clks(struct omap_hwmod *oh)
342 struct omap_hwmod_ocp_if *os;
347 if (oh->slaves_cnt == 0)
350 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
351 if (!os->clkdev_con_id)
354 c = clk_get_sys(os->clkdev_dev_id, os->clkdev_con_id);
355 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get "
356 "interface_clk %s.%s\n", oh->name,
357 os->clkdev_dev_id, os->clkdev_con_id);
367 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
368 * @oh: struct omap_hwmod *
370 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
371 * clock pointers. Returns 0 on success or -EINVAL on error.
373 static int _init_opt_clks(struct omap_hwmod *oh)
375 struct omap_hwmod_opt_clk *oc;
380 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
381 c = clk_get_sys(oc->clkdev_dev_id, oc->clkdev_con_id);
382 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get opt_clk "
383 "%s.%s\n", oh->name, oc->clkdev_dev_id,
394 * _enable_clocks - enable hwmod main clock and interface clocks
395 * @oh: struct omap_hwmod *
397 * Enables all clocks necessary for register reads and writes to succeed
398 * on the hwmod @oh. Returns 0.
400 static int _enable_clocks(struct omap_hwmod *oh)
402 struct omap_hwmod_ocp_if *os;
405 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
407 if (oh->_clk && !IS_ERR(oh->_clk))
408 clk_enable(oh->_clk);
410 if (oh->slaves_cnt > 0) {
411 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
412 struct clk *c = os->_clk;
414 if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
419 /* The opt clocks are controlled by the device driver. */
425 * _disable_clocks - disable hwmod main clock and interface clocks
426 * @oh: struct omap_hwmod *
428 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
430 static int _disable_clocks(struct omap_hwmod *oh)
432 struct omap_hwmod_ocp_if *os;
435 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
437 if (oh->_clk && !IS_ERR(oh->_clk))
438 clk_disable(oh->_clk);
440 if (oh->slaves_cnt > 0) {
441 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
442 struct clk *c = os->_clk;
444 if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
449 /* The opt clocks are controlled by the device driver. */
455 * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
456 * @oh: struct omap_hwmod *
458 * Returns the array index of the OCP slave port that the MPU
459 * addresses the device on, or -EINVAL upon error or not found.
461 static int _find_mpu_port_index(struct omap_hwmod *oh)
463 struct omap_hwmod_ocp_if *os;
467 if (!oh || oh->slaves_cnt == 0)
470 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
471 if (os->user & OCP_USER_MPU) {
478 pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
481 pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
484 return (found) ? i : -EINVAL;
488 * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
489 * @oh: struct omap_hwmod *
491 * Return the virtual address of the base of the register target of
492 * device @oh, or NULL on error.
494 static void __iomem *_find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
496 struct omap_hwmod_ocp_if *os;
497 struct omap_hwmod_addr_space *mem;
500 void __iomem *va_start;
502 if (!oh || oh->slaves_cnt == 0)
505 os = *oh->slaves + index;
507 for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
508 if (mem->flags & ADDR_TYPE_RT) {
515 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
517 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
520 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
523 pr_debug("omap_hwmod: %s: no MPU register target found\n",
527 return (found) ? va_start : NULL;
531 * _sysc_enable - try to bring a module out of idle via OCP_SYSCONFIG
532 * @oh: struct omap_hwmod *
534 * If module is marked as SWSUP_SIDLE, force the module out of slave
535 * idle; otherwise, configure it for smart-idle. If module is marked
536 * as SWSUP_MSUSPEND, force the module out of master standby;
537 * otherwise, configure it for smart-standby. No return value.
539 static void _sysc_enable(struct omap_hwmod *oh)
549 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
550 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
551 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
552 _set_slave_idlemode(oh, idlemode, &v);
555 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
556 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
557 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
558 _set_master_standbymode(oh, idlemode, &v);
561 /* XXX OCP AUTOIDLE bit? */
563 if (oh->flags & HWMOD_SET_DEFAULT_CLOCKACT &&
564 oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY)
565 _set_clockactivity(oh, oh->sysconfig->clockact, &v);
567 _write_sysconfig(v, oh);
571 * _sysc_idle - try to put a module into idle via OCP_SYSCONFIG
572 * @oh: struct omap_hwmod *
574 * If module is marked as SWSUP_SIDLE, force the module into slave
575 * idle; otherwise, configure it for smart-idle. If module is marked
576 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
577 * configure it for smart-standby. No return value.
579 static void _sysc_idle(struct omap_hwmod *oh)
589 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
590 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
591 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
592 _set_slave_idlemode(oh, idlemode, &v);
595 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
596 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
597 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
598 _set_master_standbymode(oh, idlemode, &v);
601 _write_sysconfig(v, oh);
605 * _sysc_shutdown - force a module into idle via OCP_SYSCONFIG
606 * @oh: struct omap_hwmod *
608 * Force the module into slave idle and master suspend. No return
611 static void _sysc_shutdown(struct omap_hwmod *oh)
620 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE)
621 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
623 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE)
624 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
626 /* XXX clear OCP AUTOIDLE bit? */
628 _write_sysconfig(v, oh);
632 * _lookup - find an omap_hwmod by name
633 * @name: find an omap_hwmod by name
635 * Return a pointer to an omap_hwmod by name, or NULL if not found.
636 * Caller must hold omap_hwmod_mutex.
638 static struct omap_hwmod *_lookup(const char *name)
640 struct omap_hwmod *oh, *temp_oh;
644 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
645 if (!strcmp(name, temp_oh->name)) {
655 * _init_clocks - clk_get() all clocks associated with this hwmod
656 * @oh: struct omap_hwmod *
658 * Called by omap_hwmod_late_init() (after omap2_clk_init()).
659 * Resolves all clock names embedded in the hwmod. Must be called
660 * with omap_hwmod_mutex held. Returns -EINVAL if the omap_hwmod
661 * has not yet been registered or if the clocks have already been
662 * initialized, 0 on success, or a non-zero error on failure.
664 static int _init_clocks(struct omap_hwmod *oh)
668 if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
671 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
673 ret |= _init_main_clk(oh);
674 ret |= _init_interface_clks(oh);
675 ret |= _init_opt_clks(oh);
677 oh->_state = _HWMOD_STATE_CLKS_INITED;
683 * _wait_target_ready - wait for a module to leave slave idle
684 * @oh: struct omap_hwmod *
686 * Wait for a module @oh to leave slave idle. Returns 0 if the module
687 * does not have an IDLEST bit or if the module successfully leaves
688 * slave idle; otherwise, pass along the return value of the
689 * appropriate *_cm_wait_module_ready() function.
691 static int _wait_target_ready(struct omap_hwmod *oh)
693 struct omap_hwmod_ocp_if *os;
699 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
702 os = *oh->slaves + oh->_mpu_port_index;
704 if (!(os->flags & OCPIF_HAS_IDLEST))
707 /* XXX check module SIDLEMODE */
709 /* XXX check clock enable states */
711 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
712 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
713 oh->prcm.omap2.idlest_reg_id,
714 oh->prcm.omap2.idlest_idle_bit);
716 } else if (cpu_is_omap44xx()) {
717 ret = omap4_cm_wait_module_ready(oh->prcm.omap4.module_offs,
718 oh->prcm.omap4.device_offs);
728 * _reset - reset an omap_hwmod
729 * @oh: struct omap_hwmod *
731 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
732 * enabled for this to work. Must be called with omap_hwmod_mutex
733 * held. Returns -EINVAL if the hwmod cannot be reset this way or if
734 * the hwmod is in the wrong state, -ETIMEDOUT if the module did not
735 * reset in time, or 0 upon success.
737 static int _reset(struct omap_hwmod *oh)
742 if (!oh->sysconfig ||
743 !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET) ||
744 (oh->sysconfig->sysc_flags & SYSS_MISSING))
747 /* clocks must be on for this operation */
748 if (oh->_state != _HWMOD_STATE_ENABLED) {
749 WARN(1, "omap_hwmod: %s: reset can only be entered from "
750 "enabled state\n", oh->name);
754 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
757 r = _set_softreset(oh, &v);
760 _write_sysconfig(v, oh);
762 omap_test_timeout((omap_hwmod_readl(oh, oh->sysconfig->syss_offs) &
763 SYSS_RESETDONE_MASK),
764 MAX_MODULE_RESET_WAIT, c);
766 if (c == MAX_MODULE_RESET_WAIT)
767 WARN(1, "omap_hwmod: %s: failed to reset in %d usec\n",
768 oh->name, MAX_MODULE_RESET_WAIT);
770 pr_debug("omap_hwmod: %s: reset in %d usec\n", oh->name, c);
773 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
774 * _wait_target_ready() or _reset()
777 return (c == MAX_MODULE_RESET_WAIT) ? -ETIMEDOUT : 0;
781 * _enable - enable an omap_hwmod
782 * @oh: struct omap_hwmod *
784 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
785 * register target. Must be called with omap_hwmod_mutex held.
786 * Returns -EINVAL if the hwmod is in the wrong state or passes along
787 * the return value of _wait_target_ready().
789 static int _enable(struct omap_hwmod *oh)
793 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
794 oh->_state != _HWMOD_STATE_IDLE &&
795 oh->_state != _HWMOD_STATE_DISABLED) {
796 WARN(1, "omap_hwmod: %s: enabled state can only be entered "
797 "from initialized, idle, or disabled state\n", oh->name);
801 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
805 _add_initiator_dep(oh, mpu_oh);
809 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
810 _update_sysc_cache(oh);
814 r = _wait_target_ready(oh);
816 oh->_state = _HWMOD_STATE_ENABLED;
822 * _idle - idle an omap_hwmod
823 * @oh: struct omap_hwmod *
825 * Idles an omap_hwmod @oh. This should be called once the hwmod has
826 * no further work. Returns -EINVAL if the hwmod is in the wrong
827 * state or returns 0.
829 static int _idle(struct omap_hwmod *oh)
831 if (oh->_state != _HWMOD_STATE_ENABLED) {
832 WARN(1, "omap_hwmod: %s: idle state can only be entered from "
833 "enabled state\n", oh->name);
837 pr_debug("omap_hwmod: %s: idling\n", oh->name);
841 _del_initiator_dep(oh, mpu_oh);
844 oh->_state = _HWMOD_STATE_IDLE;
850 * _shutdown - shutdown an omap_hwmod
851 * @oh: struct omap_hwmod *
853 * Shut down an omap_hwmod @oh. This should be called when the driver
854 * used for the hwmod is removed or unloaded or if the driver is not
855 * used by the system. Returns -EINVAL if the hwmod is in the wrong
856 * state or returns 0.
858 static int _shutdown(struct omap_hwmod *oh)
860 if (oh->_state != _HWMOD_STATE_IDLE &&
861 oh->_state != _HWMOD_STATE_ENABLED) {
862 WARN(1, "omap_hwmod: %s: disabled state can only be entered "
863 "from idle, or enabled state\n", oh->name);
867 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
871 _del_initiator_dep(oh, mpu_oh);
872 /* XXX what about the other system initiators here? DMA, tesla, d2d */
874 /* XXX Should this code also force-disable the optional clocks? */
876 /* XXX mux any associated balls to safe mode */
878 oh->_state = _HWMOD_STATE_DISABLED;
884 * _write_clockact_lock - set the module's clockactivity bits
885 * @oh: struct omap_hwmod *
886 * @clockact: CLOCKACTIVITY field bits
888 * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
889 * OCP_SYSCONFIG register. Returns -EINVAL if the hwmod is in the
890 * wrong state or returns 0.
892 static int _write_clockact_lock(struct omap_hwmod *oh, u8 clockact)
896 if (!oh->sysconfig ||
897 !(oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
900 mutex_lock(&omap_hwmod_mutex);
902 _set_clockactivity(oh, clockact, &v);
903 _write_sysconfig(v, oh);
904 mutex_unlock(&omap_hwmod_mutex);
911 * _setup - do initial configuration of omap_hwmod
912 * @oh: struct omap_hwmod *
914 * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
915 * OCP_SYSCONFIG register. Must be called with omap_hwmod_mutex
916 * held. Returns -EINVAL if the hwmod is in the wrong state or returns
919 static int _setup(struct omap_hwmod *oh)
921 struct omap_hwmod_ocp_if *os;
927 /* Set iclk autoidle mode */
928 if (oh->slaves_cnt > 0) {
929 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
930 struct clk *c = os->_clk;
935 if (os->flags & OCPIF_SWSUP_IDLE) {
936 /* XXX omap_iclk_deny_idle(c); */
938 /* XXX omap_iclk_allow_idle(c); */
944 oh->_state = _HWMOD_STATE_INITIALIZED;
948 if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
950 * XXX Do the OCP_SYSCONFIG bits need to be
951 * reprogrammed after a reset? If not, then this can
952 * be removed. If they do, then probably the
953 * _enable() function should be split to avoid the
954 * rewrite of the OCP_SYSCONFIG register.
957 _update_sysc_cache(oh);
962 if (!(oh->flags & HWMOD_INIT_NO_IDLE))
970 /* Public functions */
972 u32 omap_hwmod_readl(struct omap_hwmod *oh, u16 reg_offs)
974 return __raw_readl(oh->_rt_va + reg_offs);
977 void omap_hwmod_writel(u32 v, struct omap_hwmod *oh, u16 reg_offs)
979 __raw_writel(v, oh->_rt_va + reg_offs);
983 * omap_hwmod_register - register a struct omap_hwmod
984 * @oh: struct omap_hwmod *
986 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod already
987 * has been registered by the same name; -EINVAL if the omap_hwmod is in the
988 * wrong state, or 0 on success.
990 * XXX The data should be copied into bootmem, so the original data
991 * should be marked __initdata and freed after init. This would allow
992 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
993 * that the copy process would be relatively complex due to the large number
996 int omap_hwmod_register(struct omap_hwmod *oh)
1000 if (!oh || (oh->_state != _HWMOD_STATE_UNKNOWN))
1003 mutex_lock(&omap_hwmod_mutex);
1005 pr_debug("omap_hwmod: %s: registering\n", oh->name);
1007 if (_lookup(oh->name)) {
1012 ms_id = _find_mpu_port_index(oh);
1013 if (!IS_ERR_VALUE(ms_id)) {
1014 oh->_mpu_port_index = ms_id;
1015 oh->_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
1017 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1020 list_add_tail(&oh->node, &omap_hwmod_list);
1022 oh->_state = _HWMOD_STATE_REGISTERED;
1027 mutex_unlock(&omap_hwmod_mutex);
1032 * omap_hwmod_lookup - look up a registered omap_hwmod by name
1033 * @name: name of the omap_hwmod to look up
1035 * Given a @name of an omap_hwmod, return a pointer to the registered
1036 * struct omap_hwmod *, or NULL upon error.
1038 struct omap_hwmod *omap_hwmod_lookup(const char *name)
1040 struct omap_hwmod *oh;
1045 mutex_lock(&omap_hwmod_mutex);
1047 mutex_unlock(&omap_hwmod_mutex);
1053 * omap_hwmod_for_each - call function for each registered omap_hwmod
1054 * @fn: pointer to a callback function
1056 * Call @fn for each registered omap_hwmod, passing @data to each
1057 * function. @fn must return 0 for success or any other value for
1058 * failure. If @fn returns non-zero, the iteration across omap_hwmods
1059 * will stop and the non-zero return value will be passed to the
1060 * caller of omap_hwmod_for_each(). @fn is called with
1061 * omap_hwmod_for_each() held.
1063 int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh))
1065 struct omap_hwmod *temp_oh;
1071 mutex_lock(&omap_hwmod_mutex);
1072 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1073 ret = (*fn)(temp_oh);
1077 mutex_unlock(&omap_hwmod_mutex);
1084 * omap_hwmod_init - init omap_hwmod code and register hwmods
1085 * @ohs: pointer to an array of omap_hwmods to register
1087 * Intended to be called early in boot before the clock framework is
1088 * initialized. If @ohs is not null, will register all omap_hwmods
1089 * listed in @ohs that are valid for this chip. Returns -EINVAL if
1090 * omap_hwmod_init() has already been called or 0 otherwise.
1092 int omap_hwmod_init(struct omap_hwmod **ohs)
1094 struct omap_hwmod *oh;
1107 if (omap_chip_is(oh->omap_chip)) {
1108 r = omap_hwmod_register(oh);
1109 WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
1110 "%d\n", oh->name, r);
1119 * omap_hwmod_late_init - do some post-clock framework initialization
1121 * Must be called after omap2_clk_init(). Resolves the struct clk names
1122 * to struct clk pointers for each registered omap_hwmod. Also calls
1123 * _setup() on each hwmod. Returns 0.
1125 int omap_hwmod_late_init(void)
1129 /* XXX check return value */
1130 r = omap_hwmod_for_each(_init_clocks);
1131 WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
1133 mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
1134 WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
1135 MPU_INITIATOR_NAME);
1137 omap_hwmod_for_each(_setup);
1143 * omap_hwmod_unregister - unregister an omap_hwmod
1144 * @oh: struct omap_hwmod *
1146 * Unregisters a previously-registered omap_hwmod @oh. There's probably
1147 * no use case for this, so it is likely to be removed in a later version.
1149 * XXX Free all of the bootmem-allocated structures here when that is
1150 * implemented. Make it clear that core code is the only code that is
1151 * expected to unregister modules.
1153 int omap_hwmod_unregister(struct omap_hwmod *oh)
1158 pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
1160 mutex_lock(&omap_hwmod_mutex);
1161 iounmap(oh->_rt_va);
1162 list_del(&oh->node);
1163 mutex_unlock(&omap_hwmod_mutex);
1169 * omap_hwmod_enable - enable an omap_hwmod
1170 * @oh: struct omap_hwmod *
1172 * Enable an omap_hwomd @oh. Intended to be called by omap_device_enable().
1173 * Returns -EINVAL on error or passes along the return value from _enable().
1175 int omap_hwmod_enable(struct omap_hwmod *oh)
1182 mutex_lock(&omap_hwmod_mutex);
1184 mutex_unlock(&omap_hwmod_mutex);
1190 * omap_hwmod_idle - idle an omap_hwmod
1191 * @oh: struct omap_hwmod *
1193 * Idle an omap_hwomd @oh. Intended to be called by omap_device_idle().
1194 * Returns -EINVAL on error or passes along the return value from _idle().
1196 int omap_hwmod_idle(struct omap_hwmod *oh)
1201 mutex_lock(&omap_hwmod_mutex);
1203 mutex_unlock(&omap_hwmod_mutex);
1209 * omap_hwmod_shutdown - shutdown an omap_hwmod
1210 * @oh: struct omap_hwmod *
1212 * Shutdown an omap_hwomd @oh. Intended to be called by
1213 * omap_device_shutdown(). Returns -EINVAL on error or passes along
1214 * the return value from _shutdown().
1216 int omap_hwmod_shutdown(struct omap_hwmod *oh)
1221 mutex_lock(&omap_hwmod_mutex);
1223 mutex_unlock(&omap_hwmod_mutex);
1229 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
1230 * @oh: struct omap_hwmod *oh
1232 * Intended to be called by the omap_device code.
1234 int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
1236 mutex_lock(&omap_hwmod_mutex);
1238 mutex_unlock(&omap_hwmod_mutex);
1244 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
1245 * @oh: struct omap_hwmod *oh
1247 * Intended to be called by the omap_device code.
1249 int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
1251 mutex_lock(&omap_hwmod_mutex);
1252 _disable_clocks(oh);
1253 mutex_unlock(&omap_hwmod_mutex);
1259 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
1260 * @oh: struct omap_hwmod *oh
1262 * Intended to be called by drivers and core code when all posted
1263 * writes to a device must complete before continuing further
1264 * execution (for example, after clearing some device IRQSTATUS
1267 * XXX what about targets with multiple OCP threads?
1269 void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
1273 if (!oh->sysconfig || !oh->sysconfig->sysc_flags) {
1274 WARN(1, "omap_device: %s: OCP barrier impossible due to "
1275 "device configuration\n", oh->name);
1280 * Forces posted writes to complete on the OCP thread handling
1283 omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
1287 * omap_hwmod_reset - reset the hwmod
1288 * @oh: struct omap_hwmod *
1290 * Under some conditions, a driver may wish to reset the entire device.
1291 * Called from omap_device code. Returns -EINVAL on error or passes along
1292 * the return value from _reset()/_enable().
1294 int omap_hwmod_reset(struct omap_hwmod *oh)
1298 if (!oh || !(oh->_state & _HWMOD_STATE_ENABLED))
1301 mutex_lock(&omap_hwmod_mutex);
1305 mutex_unlock(&omap_hwmod_mutex);
1311 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
1312 * @oh: struct omap_hwmod *
1313 * @res: pointer to the first element of an array of struct resource to fill
1315 * Count the number of struct resource array elements necessary to
1316 * contain omap_hwmod @oh resources. Intended to be called by code
1317 * that registers omap_devices. Intended to be used to determine the
1318 * size of a dynamically-allocated struct resource array, before
1319 * calling omap_hwmod_fill_resources(). Returns the number of struct
1320 * resource array elements needed.
1322 * XXX This code is not optimized. It could attempt to merge adjacent
1326 int omap_hwmod_count_resources(struct omap_hwmod *oh)
1330 ret = oh->mpu_irqs_cnt + oh->sdma_chs_cnt;
1332 for (i = 0; i < oh->slaves_cnt; i++)
1333 ret += (*oh->slaves + i)->addr_cnt;
1339 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
1340 * @oh: struct omap_hwmod *
1341 * @res: pointer to the first element of an array of struct resource to fill
1343 * Fill the struct resource array @res with resource data from the
1344 * omap_hwmod @oh. Intended to be called by code that registers
1345 * omap_devices. See also omap_hwmod_count_resources(). Returns the
1346 * number of array elements filled.
1348 int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
1353 /* For each IRQ, DMA, memory area, fill in array.*/
1355 for (i = 0; i < oh->mpu_irqs_cnt; i++) {
1356 (res + r)->start = *(oh->mpu_irqs + i);
1357 (res + r)->end = *(oh->mpu_irqs + i);
1358 (res + r)->flags = IORESOURCE_IRQ;
1362 for (i = 0; i < oh->sdma_chs_cnt; i++) {
1363 (res + r)->name = (oh->sdma_chs + i)->name;
1364 (res + r)->start = (oh->sdma_chs + i)->dma_ch;
1365 (res + r)->end = (oh->sdma_chs + i)->dma_ch;
1366 (res + r)->flags = IORESOURCE_DMA;
1370 for (i = 0; i < oh->slaves_cnt; i++) {
1371 struct omap_hwmod_ocp_if *os;
1373 os = *oh->slaves + i;
1375 for (j = 0; j < os->addr_cnt; j++) {
1376 (res + r)->start = (os->addr + j)->pa_start;
1377 (res + r)->end = (os->addr + j)->pa_end;
1378 (res + r)->flags = IORESOURCE_MEM;
1387 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
1388 * @oh: struct omap_hwmod *
1390 * Return the powerdomain pointer associated with the OMAP module
1391 * @oh's main clock. If @oh does not have a main clk, return the
1392 * powerdomain associated with the interface clock associated with the
1393 * module's MPU port. (XXX Perhaps this should use the SDMA port
1394 * instead?) Returns NULL on error, or a struct powerdomain * on
1397 struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
1407 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1409 c = oh->slaves[oh->_mpu_port_index]->_clk;
1412 return c->clkdm->pwrdm.ptr;
1417 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
1418 * @oh: struct omap_hwmod *
1419 * @init_oh: struct omap_hwmod * (initiator)
1421 * Add a sleep dependency between the initiator @init_oh and @oh.
1422 * Intended to be called by DSP/Bridge code via platform_data for the
1423 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1424 * code needs to add/del initiator dependencies dynamically
1425 * before/after accessing a device. Returns the return value from
1426 * _add_initiator_dep().
1428 * XXX Keep a usecount in the clockdomain code
1430 int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
1431 struct omap_hwmod *init_oh)
1433 return _add_initiator_dep(oh, init_oh);
1437 * XXX what about functions for drivers to save/restore ocp_sysconfig
1438 * for context save/restore operations?
1442 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
1443 * @oh: struct omap_hwmod *
1444 * @init_oh: struct omap_hwmod * (initiator)
1446 * Remove a sleep dependency between the initiator @init_oh and @oh.
1447 * Intended to be called by DSP/Bridge code via platform_data for the
1448 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1449 * code needs to add/del initiator dependencies dynamically
1450 * before/after accessing a device. Returns the return value from
1451 * _del_initiator_dep().
1453 * XXX Keep a usecount in the clockdomain code
1455 int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
1456 struct omap_hwmod *init_oh)
1458 return _del_initiator_dep(oh, init_oh);
1462 * omap_hwmod_set_clockact_none - set clockactivity test to BOTH
1463 * @oh: struct omap_hwmod *
1465 * On some modules, this function can affect the wakeup latency vs.
1466 * power consumption balance. Intended to be called by the
1467 * omap_device layer. Passes along the return value from
1468 * _write_clockact_lock().
1470 int omap_hwmod_set_clockact_both(struct omap_hwmod *oh)
1472 return _write_clockact_lock(oh, CLOCKACT_TEST_BOTH);
1476 * omap_hwmod_set_clockact_none - set clockactivity test to MAIN
1477 * @oh: struct omap_hwmod *
1479 * On some modules, this function can affect the wakeup latency vs.
1480 * power consumption balance. Intended to be called by the
1481 * omap_device layer. Passes along the return value from
1482 * _write_clockact_lock().
1484 int omap_hwmod_set_clockact_main(struct omap_hwmod *oh)
1486 return _write_clockact_lock(oh, CLOCKACT_TEST_MAIN);
1490 * omap_hwmod_set_clockact_none - set clockactivity test to ICLK
1491 * @oh: struct omap_hwmod *
1493 * On some modules, this function can affect the wakeup latency vs.
1494 * power consumption balance. Intended to be called by the
1495 * omap_device layer. Passes along the return value from
1496 * _write_clockact_lock().
1498 int omap_hwmod_set_clockact_iclk(struct omap_hwmod *oh)
1500 return _write_clockact_lock(oh, CLOCKACT_TEST_ICLK);
1504 * omap_hwmod_set_clockact_none - set clockactivity test to NONE
1505 * @oh: struct omap_hwmod *
1507 * On some modules, this function can affect the wakeup latency vs.
1508 * power consumption balance. Intended to be called by the
1509 * omap_device layer. Passes along the return value from
1510 * _write_clockact_lock().
1512 int omap_hwmod_set_clockact_none(struct omap_hwmod *oh)
1514 return _write_clockact_lock(oh, CLOCKACT_TEST_NONE);
1518 * omap_hwmod_enable_wakeup - allow device to wake up the system
1519 * @oh: struct omap_hwmod *
1521 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
1522 * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
1523 * registers to cause the PRCM to receive wakeup events from the
1524 * module. Does not set any wakeup routing registers beyond this
1525 * point - if the module is to wake up any other module or subsystem,
1526 * that must be set separately. Called by omap_device code. Returns
1527 * -EINVAL on error or 0 upon success.
1529 int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
1531 if (!oh->sysconfig ||
1532 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
1535 mutex_lock(&omap_hwmod_mutex);
1537 mutex_unlock(&omap_hwmod_mutex);
1543 * omap_hwmod_disable_wakeup - prevent device from waking the system
1544 * @oh: struct omap_hwmod *
1546 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
1547 * from sending wakeups to the PRCM. Eventually this should clear
1548 * PRCM wakeup registers to cause the PRCM to ignore wakeup events
1549 * from the module. Does not set any wakeup routing registers beyond
1550 * this point - if the module is to wake up any other module or
1551 * subsystem, that must be set separately. Called by omap_device
1552 * code. Returns -EINVAL on error or 0 upon success.
1554 int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
1556 if (!oh->sysconfig ||
1557 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
1560 mutex_lock(&omap_hwmod_mutex);
1561 _disable_wakeup(oh);
1562 mutex_unlock(&omap_hwmod_mutex);