blob: 32a5e08ffb9b733a7fcda74b812030986d7be616 [file] [log] [blame]
Paul Walmsley63c85232009-09-03 20:14:03 +03001/*
2 * omap_hwmod implementation for OMAP2/3/4
3 *
Paul Walmsley550c8092011-02-28 11:58:14 -07004 * Copyright (C) 2009-2011 Nokia Corporation
Paul Walmsley30e105c2012-04-19 00:49:09 -06005 * Copyright (C) 2011-2012 Texas Instruments, Inc.
Paul Walmsley63c85232009-09-03 20:14:03 +03006 *
Paul Walmsley4788da22010-05-18 20:24:05 -06007 * Paul Walmsley, Benoît Cousson, Kevin Hilman
8 *
9 * Created in collaboration with (alphabetical order): Thara Gopinath,
10 * Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari Poussa, Anand
11 * Sawant, Santosh Shilimkar, Richard Woodruff
Paul Walmsley63c85232009-09-03 20:14:03 +030012 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -060017 * Introduction
18 * ------------
19 * One way to view an OMAP SoC is as a collection of largely unrelated
20 * IP blocks connected by interconnects. The IP blocks include
21 * devices such as ARM processors, audio serial interfaces, UARTs,
22 * etc. Some of these devices, like the DSP, are created by TI;
23 * others, like the SGX, largely originate from external vendors. In
24 * TI's documentation, on-chip devices are referred to as "OMAP
25 * modules." Some of these IP blocks are identical across several
26 * OMAP versions. Others are revised frequently.
Paul Walmsley63c85232009-09-03 20:14:03 +030027 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -060028 * These OMAP modules are tied together by various interconnects.
29 * Most of the address and data flow between modules is via OCP-based
30 * interconnects such as the L3 and L4 buses; but there are other
31 * interconnects that distribute the hardware clock tree, handle idle
32 * and reset signaling, supply power, and connect the modules to
33 * various pads or balls on the OMAP package.
34 *
35 * OMAP hwmod provides a consistent way to describe the on-chip
36 * hardware blocks and their integration into the rest of the chip.
37 * This description can be automatically generated from the TI
38 * hardware database. OMAP hwmod provides a standard, consistent API
39 * to reset, enable, idle, and disable these hardware blocks. And
40 * hwmod provides a way for other core code, such as the Linux device
41 * code or the OMAP power management and address space mapping code,
42 * to query the hardware database.
43 *
44 * Using hwmod
45 * -----------
46 * Drivers won't call hwmod functions directly. That is done by the
47 * omap_device code, and in rare occasions, by custom integration code
48 * in arch/arm/ *omap*. The omap_device code includes functions to
49 * build a struct platform_device using omap_hwmod data, and that is
50 * currently how hwmod data is communicated to drivers and to the
51 * Linux driver model. Most drivers will call omap_hwmod functions only
52 * indirectly, via pm_runtime*() functions.
53 *
54 * From a layering perspective, here is where the OMAP hwmod code
55 * fits into the kernel software stack:
56 *
57 * +-------------------------------+
58 * | Device driver code |
59 * | (e.g., drivers/) |
60 * +-------------------------------+
61 * | Linux driver model |
62 * | (platform_device / |
63 * | platform_driver data/code) |
64 * +-------------------------------+
65 * | OMAP core-driver integration |
66 * |(arch/arm/mach-omap2/devices.c)|
67 * +-------------------------------+
68 * | omap_device code |
69 * | (../plat-omap/omap_device.c) |
70 * +-------------------------------+
71 * ----> | omap_hwmod code/data | <-----
72 * | (../mach-omap2/omap_hwmod*) |
73 * +-------------------------------+
74 * | OMAP clock/PRCM/register fns |
75 * | (__raw_{read,write}l, clk*) |
76 * +-------------------------------+
77 *
78 * Device drivers should not contain any OMAP-specific code or data in
79 * them. They should only contain code to operate the IP block that
80 * the driver is responsible for. This is because these IP blocks can
81 * also appear in other SoCs, either from TI (such as DaVinci) or from
82 * other manufacturers; and drivers should be reusable across other
83 * platforms.
84 *
85 * The OMAP hwmod code also will attempt to reset and idle all on-chip
86 * devices upon boot. The goal here is for the kernel to be
87 * completely self-reliant and independent from bootloaders. This is
88 * to ensure a repeatable configuration, both to ensure consistent
89 * runtime behavior, and to make it easier for others to reproduce
90 * bugs.
91 *
92 * OMAP module activity states
93 * ---------------------------
94 * The hwmod code considers modules to be in one of several activity
95 * states. IP blocks start out in an UNKNOWN state, then once they
96 * are registered via the hwmod code, proceed to the REGISTERED state.
97 * Once their clock names are resolved to clock pointers, the module
98 * enters the CLKS_INITED state; and finally, once the module has been
99 * reset and the integration registers programmed, the INITIALIZED state
100 * is entered. The hwmod code will then place the module into either
101 * the IDLE state to save power, or in the case of a critical system
102 * module, the ENABLED state.
103 *
104 * OMAP core integration code can then call omap_hwmod*() functions
105 * directly to move the module between the IDLE, ENABLED, and DISABLED
106 * states, as needed. This is done during both the PM idle loop, and
107 * in the OMAP core integration code's implementation of the PM runtime
108 * functions.
109 *
110 * References
111 * ----------
112 * This is a partial list.
Paul Walmsley63c85232009-09-03 20:14:03 +0300113 * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
114 * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
115 * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
116 * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
117 * - Open Core Protocol Specification 2.2
118 *
119 * To do:
Paul Walmsley63c85232009-09-03 20:14:03 +0300120 * - handle IO mapping
121 * - bus throughput & module latency measurement code
122 *
123 * XXX add tests at the beginning of each function to ensure the hwmod is
124 * in the appropriate state
125 * XXX error return values should be checked to ensure that they are
126 * appropriate
127 */
128#undef DEBUG
129
130#include <linux/kernel.h>
131#include <linux/errno.h>
132#include <linux/io.h>
133#include <linux/clk.h>
134#include <linux/delay.h>
135#include <linux/err.h>
136#include <linux/list.h>
137#include <linux/mutex.h>
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -0700138#include <linux/spinlock.h>
Tero Kristoabc2d542011-12-16 14:36:59 -0700139#include <linux/slab.h>
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600140#include <linux/bootmem.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300141
Tony Lindgren4e653312011-11-10 22:45:17 +0100142#include "common.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700143#include <plat/cpu.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -0700144#include "clockdomain.h"
Paul Walmsley72e06d02010-12-21 21:05:16 -0700145#include "powerdomain.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700146#include <plat/clock.h>
147#include <plat/omap_hwmod.h>
Benoît Cousson5365efb2010-09-21 10:34:11 -0600148#include <plat/prcm.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300149
Paul Walmsley59fb6592010-12-21 15:30:55 -0700150#include "cm2xxx_3xxx.h"
Benoit Coussond0f06312011-07-10 05:56:30 -0600151#include "cminst44xx.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -0700152#include "prm2xxx_3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -0700153#include "prm44xx.h"
Benoit Coussoneaac3292011-07-10 05:56:31 -0600154#include "prminst44xx.h"
Tony Lindgren8d9af882010-12-22 18:42:35 -0800155#include "mux.h"
Paul Walmsley63c85232009-09-03 20:14:03 +0300156
Benoît Cousson5365efb2010-09-21 10:34:11 -0600157/* Maximum microseconds to wait for OMAP module to softreset */
158#define MAX_MODULE_SOFTRESET_WAIT 10000
Paul Walmsley63c85232009-09-03 20:14:03 +0300159
160/* Name of the OMAP hwmod for the MPU */
Benoit Cousson5c2c0292010-05-20 12:31:10 -0600161#define MPU_INITIATOR_NAME "mpu"
Paul Walmsley63c85232009-09-03 20:14:03 +0300162
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600163/*
164 * Number of struct omap_hwmod_link records per struct
165 * omap_hwmod_ocp_if record (master->slave and slave->master)
166 */
167#define LINKS_PER_OCP_IF 2
168
Paul Walmsley63c85232009-09-03 20:14:03 +0300169/* omap_hwmod_list contains all registered struct omap_hwmods */
170static LIST_HEAD(omap_hwmod_list);
171
Paul Walmsley63c85232009-09-03 20:14:03 +0300172/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
173static struct omap_hwmod *mpu_oh;
174
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600175/*
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600176 * linkspace: ptr to a buffer that struct omap_hwmod_link records are
177 * allocated from - used to reduce the number of small memory
178 * allocations, which has a significant impact on performance
179 */
180static struct omap_hwmod_link *linkspace;
181
182/*
183 * free_ls, max_ls: array indexes into linkspace; representing the
184 * next free struct omap_hwmod_link index, and the maximum number of
185 * struct omap_hwmod_link records allocated (respectively)
186 */
187static unsigned short free_ls, max_ls, ls_supp;
Paul Walmsley63c85232009-09-03 20:14:03 +0300188
189/* Private functions */
190
191/**
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600192 * _fetch_next_ocp_if - return the next OCP interface in a list
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600193 * @p: ptr to a ptr to the list_head inside the ocp_if to return
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600194 * @i: pointer to the index of the element pointed to by @p in the list
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600195 *
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600196 * Return a pointer to the struct omap_hwmod_ocp_if record
197 * containing the struct list_head pointed to by @p, and increment
198 * @p such that a future call to this routine will return the next
199 * record.
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600200 */
201static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600202 int *i)
203{
204 struct omap_hwmod_ocp_if *oi;
205
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600206 oi = list_entry(*p, struct omap_hwmod_link, node)->ocp_if;
207 *p = (*p)->next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600208
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600209 *i = *i + 1;
210
211 return oi;
212}
213
214/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300215 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
216 * @oh: struct omap_hwmod *
217 *
218 * Load the current value of the hwmod OCP_SYSCONFIG register into the
219 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
220 * OCP_SYSCONFIG register or 0 upon success.
221 */
222static int _update_sysc_cache(struct omap_hwmod *oh)
223{
Paul Walmsley43b40992010-02-22 22:09:34 -0700224 if (!oh->class->sysc) {
225 WARN(1, "omap_hwmod: %s: cannot read OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +0300226 return -EINVAL;
227 }
228
229 /* XXX ensure module interface clock is up */
230
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -0700231 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300232
Paul Walmsley43b40992010-02-22 22:09:34 -0700233 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
Thara Gopinath883edfd2010-01-19 17:30:51 -0700234 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300235
236 return 0;
237}
238
239/**
240 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
241 * @v: OCP_SYSCONFIG value to write
242 * @oh: struct omap_hwmod *
243 *
Paul Walmsley43b40992010-02-22 22:09:34 -0700244 * Write @v into the module class' OCP_SYSCONFIG register, if it has
245 * one. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +0300246 */
247static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
248{
Paul Walmsley43b40992010-02-22 22:09:34 -0700249 if (!oh->class->sysc) {
250 WARN(1, "omap_hwmod: %s: cannot write OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +0300251 return;
252 }
253
254 /* XXX ensure module interface clock is up */
255
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700256 /* Module might have lost context, always update cache and register */
257 oh->_sysc_cache = v;
258 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300259}
260
261/**
262 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
263 * @oh: struct omap_hwmod *
264 * @standbymode: MIDLEMODE field bits
265 * @v: pointer to register contents to modify
266 *
267 * Update the master standby mode bits in @v to be @standbymode for
268 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
269 * upon error or 0 upon success.
270 */
271static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
272 u32 *v)
273{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700274 u32 mstandby_mask;
275 u8 mstandby_shift;
276
Paul Walmsley43b40992010-02-22 22:09:34 -0700277 if (!oh->class->sysc ||
278 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300279 return -EINVAL;
280
Paul Walmsley43b40992010-02-22 22:09:34 -0700281 if (!oh->class->sysc->sysc_fields) {
282 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700283 return -EINVAL;
284 }
285
Paul Walmsley43b40992010-02-22 22:09:34 -0700286 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700287 mstandby_mask = (0x3 << mstandby_shift);
288
289 *v &= ~mstandby_mask;
290 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300291
292 return 0;
293}
294
295/**
296 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
297 * @oh: struct omap_hwmod *
298 * @idlemode: SIDLEMODE field bits
299 * @v: pointer to register contents to modify
300 *
301 * Update the slave idle mode bits in @v to be @idlemode for the @oh
302 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
303 * or 0 upon success.
304 */
305static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
306{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700307 u32 sidle_mask;
308 u8 sidle_shift;
309
Paul Walmsley43b40992010-02-22 22:09:34 -0700310 if (!oh->class->sysc ||
311 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300312 return -EINVAL;
313
Paul Walmsley43b40992010-02-22 22:09:34 -0700314 if (!oh->class->sysc->sysc_fields) {
315 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700316 return -EINVAL;
317 }
318
Paul Walmsley43b40992010-02-22 22:09:34 -0700319 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700320 sidle_mask = (0x3 << sidle_shift);
321
322 *v &= ~sidle_mask;
323 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300324
325 return 0;
326}
327
328/**
329 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
330 * @oh: struct omap_hwmod *
331 * @clockact: CLOCKACTIVITY field bits
332 * @v: pointer to register contents to modify
333 *
334 * Update the clockactivity mode bits in @v to be @clockact for the
335 * @oh hwmod. Used for additional powersaving on some modules. Does
336 * not write to the hardware. Returns -EINVAL upon error or 0 upon
337 * success.
338 */
339static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
340{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700341 u32 clkact_mask;
342 u8 clkact_shift;
343
Paul Walmsley43b40992010-02-22 22:09:34 -0700344 if (!oh->class->sysc ||
345 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300346 return -EINVAL;
347
Paul Walmsley43b40992010-02-22 22:09:34 -0700348 if (!oh->class->sysc->sysc_fields) {
349 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700350 return -EINVAL;
351 }
352
Paul Walmsley43b40992010-02-22 22:09:34 -0700353 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700354 clkact_mask = (0x3 << clkact_shift);
355
356 *v &= ~clkact_mask;
357 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300358
359 return 0;
360}
361
362/**
363 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
364 * @oh: struct omap_hwmod *
365 * @v: pointer to register contents to modify
366 *
367 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
368 * error or 0 upon success.
369 */
370static int _set_softreset(struct omap_hwmod *oh, u32 *v)
371{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700372 u32 softrst_mask;
373
Paul Walmsley43b40992010-02-22 22:09:34 -0700374 if (!oh->class->sysc ||
375 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300376 return -EINVAL;
377
Paul Walmsley43b40992010-02-22 22:09:34 -0700378 if (!oh->class->sysc->sysc_fields) {
379 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700380 return -EINVAL;
381 }
382
Paul Walmsley43b40992010-02-22 22:09:34 -0700383 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700384
385 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300386
387 return 0;
388}
389
390/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700391 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
392 * @oh: struct omap_hwmod *
393 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
394 * @v: pointer to register contents to modify
395 *
396 * Update the module autoidle bit in @v to be @autoidle for the @oh
397 * hwmod. The autoidle bit controls whether the module can gate
398 * internal clocks automatically when it isn't doing anything; the
399 * exact function of this bit varies on a per-module basis. This
400 * function does not write to the hardware. Returns -EINVAL upon
401 * error or 0 upon success.
402 */
403static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
404 u32 *v)
405{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700406 u32 autoidle_mask;
407 u8 autoidle_shift;
408
Paul Walmsley43b40992010-02-22 22:09:34 -0700409 if (!oh->class->sysc ||
410 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700411 return -EINVAL;
412
Paul Walmsley43b40992010-02-22 22:09:34 -0700413 if (!oh->class->sysc->sysc_fields) {
414 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700415 return -EINVAL;
416 }
417
Paul Walmsley43b40992010-02-22 22:09:34 -0700418 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700419 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700420
421 *v &= ~autoidle_mask;
422 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700423
424 return 0;
425}
426
427/**
Govindraj Receec002011-12-16 14:36:58 -0700428 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
429 * @oh: struct omap_hwmod *
430 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
431 *
432 * Set or clear the I/O pad wakeup flag in the mux entries for the
433 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
434 * in memory. If the hwmod is currently idled, and the new idle
435 * values don't match the previous ones, this function will also
436 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
437 * currently idled, this function won't touch the hardware: the new
438 * mux settings are written to the SCM PADCTRL registers when the
439 * hwmod is idled. No return value.
440 */
441static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
442{
443 struct omap_device_pad *pad;
444 bool change = false;
445 u16 prev_idle;
446 int j;
447
448 if (!oh->mux || !oh->mux->enabled)
449 return;
450
451 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
452 pad = oh->mux->pads_dynamic[j];
453
454 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
455 continue;
456
457 prev_idle = pad->idle;
458
459 if (set_wake)
460 pad->idle |= OMAP_WAKEUP_EN;
461 else
462 pad->idle &= ~OMAP_WAKEUP_EN;
463
464 if (prev_idle != pad->idle)
465 change = true;
466 }
467
468 if (change && oh->_state == _HWMOD_STATE_IDLE)
469 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
470}
471
472/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300473 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
474 * @oh: struct omap_hwmod *
475 *
476 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
477 * upon error or 0 upon success.
478 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700479static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300480{
Paul Walmsley43b40992010-02-22 22:09:34 -0700481 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700482 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200483 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
484 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300485 return -EINVAL;
486
Paul Walmsley43b40992010-02-22 22:09:34 -0700487 if (!oh->class->sysc->sysc_fields) {
488 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700489 return -EINVAL;
490 }
491
Benoit Cousson1fe74112011-07-01 22:54:03 +0200492 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
493 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300494
Benoit Cousson86009eb2010-12-21 21:31:28 -0700495 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
496 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200497 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
498 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700499
Paul Walmsley63c85232009-09-03 20:14:03 +0300500 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
501
502 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
503
504 return 0;
505}
506
507/**
508 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
509 * @oh: struct omap_hwmod *
510 *
511 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
512 * upon error or 0 upon success.
513 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700514static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300515{
Paul Walmsley43b40992010-02-22 22:09:34 -0700516 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700517 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200518 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
519 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300520 return -EINVAL;
521
Paul Walmsley43b40992010-02-22 22:09:34 -0700522 if (!oh->class->sysc->sysc_fields) {
523 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700524 return -EINVAL;
525 }
526
Benoit Cousson1fe74112011-07-01 22:54:03 +0200527 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
528 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300529
Benoit Cousson86009eb2010-12-21 21:31:28 -0700530 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
531 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200532 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
533 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700534
Paul Walmsley63c85232009-09-03 20:14:03 +0300535 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
536
537 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
538
539 return 0;
540}
541
542/**
543 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
544 * @oh: struct omap_hwmod *
545 *
546 * Prevent the hardware module @oh from entering idle while the
547 * hardare module initiator @init_oh is active. Useful when a module
548 * will be accessed by a particular initiator (e.g., if a module will
549 * be accessed by the IVA, there should be a sleepdep between the IVA
550 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700551 * mode. If the clockdomain is marked as not needing autodeps, return
552 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
553 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300554 */
555static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
556{
557 if (!oh->_clk)
558 return -EINVAL;
559
Paul Walmsley570b54c2011-03-10 03:50:09 -0700560 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
561 return 0;
562
Paul Walmsley55ed9692010-01-26 20:12:59 -0700563 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300564}
565
566/**
567 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
568 * @oh: struct omap_hwmod *
569 *
570 * Allow the hardware module @oh to enter idle while the hardare
571 * module initiator @init_oh is active. Useful when a module will not
572 * be accessed by a particular initiator (e.g., if a module will not
573 * be accessed by the IVA, there should be no sleepdep between the IVA
574 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700575 * mode. If the clockdomain is marked as not needing autodeps, return
576 * 0 without doing anything. Returns -EINVAL upon error or passes
577 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300578 */
579static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
580{
581 if (!oh->_clk)
582 return -EINVAL;
583
Paul Walmsley570b54c2011-03-10 03:50:09 -0700584 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
585 return 0;
586
Paul Walmsley55ed9692010-01-26 20:12:59 -0700587 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300588}
589
590/**
591 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
592 * @oh: struct omap_hwmod *
593 *
594 * Called from _init_clocks(). Populates the @oh _clk (main
595 * functional clock pointer) if a main_clk is present. Returns 0 on
596 * success or -EINVAL on error.
597 */
598static int _init_main_clk(struct omap_hwmod *oh)
599{
Paul Walmsley63c85232009-09-03 20:14:03 +0300600 int ret = 0;
601
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700602 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300603 return 0;
604
Benoit Cousson63403382010-05-20 12:31:10 -0600605 oh->_clk = omap_clk_get_by_name(oh->main_clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600606 if (!oh->_clk) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600607 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
608 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600609 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600610 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300611
Benoit Cousson63403382010-05-20 12:31:10 -0600612 if (!oh->_clk->clkdm)
613 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
614 oh->main_clk, oh->_clk->name);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700615
Paul Walmsley63c85232009-09-03 20:14:03 +0300616 return ret;
617}
618
619/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600620 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300621 * @oh: struct omap_hwmod *
622 *
623 * Called from _init_clocks(). Populates the @oh OCP slave interface
624 * clock pointers. Returns 0 on success or -EINVAL on error.
625 */
626static int _init_interface_clks(struct omap_hwmod *oh)
627{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600628 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600629 struct list_head *p;
Paul Walmsley63c85232009-09-03 20:14:03 +0300630 struct clk *c;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600631 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300632 int ret = 0;
633
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600634 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600635
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600636 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600637 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700638 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300639 continue;
640
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700641 c = omap_clk_get_by_name(os->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600642 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600643 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
644 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300645 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600646 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300647 os->_clk = c;
648 }
649
650 return ret;
651}
652
653/**
654 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
655 * @oh: struct omap_hwmod *
656 *
657 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
658 * clock pointers. Returns 0 on success or -EINVAL on error.
659 */
660static int _init_opt_clks(struct omap_hwmod *oh)
661{
662 struct omap_hwmod_opt_clk *oc;
663 struct clk *c;
664 int i;
665 int ret = 0;
666
667 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700668 c = omap_clk_get_by_name(oc->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600669 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600670 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
671 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300672 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600673 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300674 oc->_clk = c;
675 }
676
677 return ret;
678}
679
680/**
681 * _enable_clocks - enable hwmod main clock and interface clocks
682 * @oh: struct omap_hwmod *
683 *
684 * Enables all clocks necessary for register reads and writes to succeed
685 * on the hwmod @oh. Returns 0.
686 */
687static int _enable_clocks(struct omap_hwmod *oh)
688{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600689 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600690 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600691 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300692
693 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
694
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600695 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300696 clk_enable(oh->_clk);
697
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600698 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600699
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600700 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600701 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300702
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600703 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
704 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300705 }
706
707 /* The opt clocks are controlled by the device driver. */
708
709 return 0;
710}
711
712/**
713 * _disable_clocks - disable hwmod main clock and interface clocks
714 * @oh: struct omap_hwmod *
715 *
716 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
717 */
718static int _disable_clocks(struct omap_hwmod *oh)
719{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600720 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600721 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600722 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300723
724 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
725
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600726 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300727 clk_disable(oh->_clk);
728
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600729 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600730
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600731 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600732 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300733
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600734 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
735 clk_disable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300736 }
737
738 /* The opt clocks are controlled by the device driver. */
739
740 return 0;
741}
742
Benoit Cousson96835af2010-09-21 18:57:58 +0200743static void _enable_optional_clocks(struct omap_hwmod *oh)
744{
745 struct omap_hwmod_opt_clk *oc;
746 int i;
747
748 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
749
750 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
751 if (oc->_clk) {
752 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
753 oc->_clk->name);
754 clk_enable(oc->_clk);
755 }
756}
757
758static void _disable_optional_clocks(struct omap_hwmod *oh)
759{
760 struct omap_hwmod_opt_clk *oc;
761 int i;
762
763 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
764
765 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
766 if (oc->_clk) {
767 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
768 oc->_clk->name);
769 clk_disable(oc->_clk);
770 }
771}
772
Paul Walmsley63c85232009-09-03 20:14:03 +0300773/**
Benoit Cousson45c38252011-07-10 05:56:33 -0600774 * _enable_module - enable CLKCTRL modulemode on OMAP4
775 * @oh: struct omap_hwmod *
776 *
777 * Enables the PRCM module mode related to the hwmod @oh.
778 * No return value.
779 */
780static void _enable_module(struct omap_hwmod *oh)
781{
782 /* The module mode does not exist prior OMAP4 */
783 if (cpu_is_omap24xx() || cpu_is_omap34xx())
784 return;
785
786 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
787 return;
788
789 pr_debug("omap_hwmod: %s: _enable_module: %d\n",
790 oh->name, oh->prcm.omap4.modulemode);
791
792 omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
793 oh->clkdm->prcm_partition,
794 oh->clkdm->cm_inst,
795 oh->clkdm->clkdm_offs,
796 oh->prcm.omap4.clkctrl_offs);
797}
798
799/**
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800800 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
801 * @oh: struct omap_hwmod *
802 *
803 * Wait for a module @oh to enter slave idle. Returns 0 if the module
804 * does not have an IDLEST bit or if the module successfully enters
805 * slave idle; otherwise, pass along the return value of the
806 * appropriate *_cm*_wait_module_idle() function.
807 */
808static int _omap4_wait_target_disable(struct omap_hwmod *oh)
809{
810 if (!cpu_is_omap44xx())
811 return 0;
812
Paul Walmsley868c1572012-06-19 15:01:02 -0600813 if (!oh || !oh->clkdm)
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800814 return -EINVAL;
815
816 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
817 return 0;
818
819 if (oh->flags & HWMOD_NO_IDLEST)
820 return 0;
821
822 return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
823 oh->clkdm->cm_inst,
824 oh->clkdm->clkdm_offs,
825 oh->prcm.omap4.clkctrl_offs);
826}
827
828/**
Paul Walmsley212738a2011-07-09 19:14:06 -0600829 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
830 * @oh: struct omap_hwmod *oh
831 *
832 * Count and return the number of MPU IRQs associated with the hwmod
833 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
834 * NULL.
835 */
836static int _count_mpu_irqs(struct omap_hwmod *oh)
837{
838 struct omap_hwmod_irq_info *ohii;
839 int i = 0;
840
841 if (!oh || !oh->mpu_irqs)
842 return 0;
843
844 do {
845 ohii = &oh->mpu_irqs[i++];
846 } while (ohii->irq != -1);
847
sricharancc1b07652011-11-23 14:35:07 -0800848 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -0600849}
850
851/**
Paul Walmsleybc614952011-07-09 19:14:07 -0600852 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
853 * @oh: struct omap_hwmod *oh
854 *
855 * Count and return the number of SDMA request lines associated with
856 * the hwmod @oh. Used to allocate struct resource data. Returns 0
857 * if @oh is NULL.
858 */
859static int _count_sdma_reqs(struct omap_hwmod *oh)
860{
861 struct omap_hwmod_dma_info *ohdi;
862 int i = 0;
863
864 if (!oh || !oh->sdma_reqs)
865 return 0;
866
867 do {
868 ohdi = &oh->sdma_reqs[i++];
869 } while (ohdi->dma_req != -1);
870
sricharancc1b07652011-11-23 14:35:07 -0800871 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -0600872}
873
874/**
Paul Walmsley78183f32011-07-09 19:14:05 -0600875 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
876 * @oh: struct omap_hwmod *oh
877 *
878 * Count and return the number of address space ranges associated with
879 * the hwmod @oh. Used to allocate struct resource data. Returns 0
880 * if @oh is NULL.
881 */
882static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
883{
884 struct omap_hwmod_addr_space *mem;
885 int i = 0;
886
887 if (!os || !os->addr)
888 return 0;
889
890 do {
891 mem = &os->addr[i++];
892 } while (mem->pa_start != mem->pa_end);
893
sricharancc1b07652011-11-23 14:35:07 -0800894 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -0600895}
896
897/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -0600898 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
899 * @oh: struct omap_hwmod * to operate on
900 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
901 * @irq: pointer to an unsigned int to store the MPU IRQ number to
902 *
903 * Retrieve a MPU hardware IRQ line number named by @name associated
904 * with the IP block pointed to by @oh. The IRQ number will be filled
905 * into the address pointed to by @dma. When @name is non-null, the
906 * IRQ line number associated with the named entry will be returned.
907 * If @name is null, the first matching entry will be returned. Data
908 * order is not meaningful in hwmod data, so callers are strongly
909 * encouraged to use a non-null @name whenever possible to avoid
910 * unpredictable effects if hwmod data is later added that causes data
911 * ordering to change. Returns 0 upon success or a negative error
912 * code upon error.
913 */
914static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
915 unsigned int *irq)
916{
917 int i;
918 bool found = false;
919
920 if (!oh->mpu_irqs)
921 return -ENOENT;
922
923 i = 0;
924 while (oh->mpu_irqs[i].irq != -1) {
925 if (name == oh->mpu_irqs[i].name ||
926 !strcmp(name, oh->mpu_irqs[i].name)) {
927 found = true;
928 break;
929 }
930 i++;
931 }
932
933 if (!found)
934 return -ENOENT;
935
936 *irq = oh->mpu_irqs[i].irq;
937
938 return 0;
939}
940
941/**
942 * _get_sdma_req_by_name - fetch SDMA request line ID by name
943 * @oh: struct omap_hwmod * to operate on
944 * @name: pointer to the name of the SDMA request line to fetch (optional)
945 * @dma: pointer to an unsigned int to store the request line ID to
946 *
947 * Retrieve an SDMA request line ID named by @name on the IP block
948 * pointed to by @oh. The ID will be filled into the address pointed
949 * to by @dma. When @name is non-null, the request line ID associated
950 * with the named entry will be returned. If @name is null, the first
951 * matching entry will be returned. Data order is not meaningful in
952 * hwmod data, so callers are strongly encouraged to use a non-null
953 * @name whenever possible to avoid unpredictable effects if hwmod
954 * data is later added that causes data ordering to change. Returns 0
955 * upon success or a negative error code upon error.
956 */
957static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
958 unsigned int *dma)
959{
960 int i;
961 bool found = false;
962
963 if (!oh->sdma_reqs)
964 return -ENOENT;
965
966 i = 0;
967 while (oh->sdma_reqs[i].dma_req != -1) {
968 if (name == oh->sdma_reqs[i].name ||
969 !strcmp(name, oh->sdma_reqs[i].name)) {
970 found = true;
971 break;
972 }
973 i++;
974 }
975
976 if (!found)
977 return -ENOENT;
978
979 *dma = oh->sdma_reqs[i].dma_req;
980
981 return 0;
982}
983
984/**
985 * _get_addr_space_by_name - fetch address space start & end by name
986 * @oh: struct omap_hwmod * to operate on
987 * @name: pointer to the name of the address space to fetch (optional)
988 * @pa_start: pointer to a u32 to store the starting address to
989 * @pa_end: pointer to a u32 to store the ending address to
990 *
991 * Retrieve address space start and end addresses for the IP block
992 * pointed to by @oh. The data will be filled into the addresses
993 * pointed to by @pa_start and @pa_end. When @name is non-null, the
994 * address space data associated with the named entry will be
995 * returned. If @name is null, the first matching entry will be
996 * returned. Data order is not meaningful in hwmod data, so callers
997 * are strongly encouraged to use a non-null @name whenever possible
998 * to avoid unpredictable effects if hwmod data is later added that
999 * causes data ordering to change. Returns 0 upon success or a
1000 * negative error code upon error.
1001 */
1002static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1003 u32 *pa_start, u32 *pa_end)
1004{
1005 int i, j;
1006 struct omap_hwmod_ocp_if *os;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001007 struct list_head *p = NULL;
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001008 bool found = false;
1009
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001010 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001011
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001012 i = 0;
1013 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001014 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001015
1016 if (!os->addr)
1017 return -ENOENT;
1018
1019 j = 0;
1020 while (os->addr[j].pa_start != os->addr[j].pa_end) {
1021 if (name == os->addr[j].name ||
1022 !strcmp(name, os->addr[j].name)) {
1023 found = true;
1024 break;
1025 }
1026 j++;
1027 }
1028
1029 if (found)
1030 break;
1031 }
1032
1033 if (!found)
1034 return -ENOENT;
1035
1036 *pa_start = os->addr[j].pa_start;
1037 *pa_end = os->addr[j].pa_end;
1038
1039 return 0;
1040}
1041
1042/**
Paul Walmsley24dbc212012-04-19 04:04:29 -06001043 * _save_mpu_port_index - find and save the index to @oh's MPU port
Paul Walmsley63c85232009-09-03 20:14:03 +03001044 * @oh: struct omap_hwmod *
1045 *
Paul Walmsley24dbc212012-04-19 04:04:29 -06001046 * Determines the array index of the OCP slave port that the MPU uses
1047 * to address the device, and saves it into the struct omap_hwmod.
1048 * Intended to be called during hwmod registration only. No return
1049 * value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001050 */
Paul Walmsley24dbc212012-04-19 04:04:29 -06001051static void __init _save_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001052{
Paul Walmsley24dbc212012-04-19 04:04:29 -06001053 struct omap_hwmod_ocp_if *os = NULL;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001054 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001055 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001056
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001057 if (!oh)
Paul Walmsley24dbc212012-04-19 04:04:29 -06001058 return;
1059
1060 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001061
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001062 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001063
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001064 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001065 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +03001066 if (os->user & OCP_USER_MPU) {
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001067 oh->_mpu_port = os;
Paul Walmsley24dbc212012-04-19 04:04:29 -06001068 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001069 break;
1070 }
1071 }
1072
Paul Walmsley24dbc212012-04-19 04:04:29 -06001073 return;
Paul Walmsley63c85232009-09-03 20:14:03 +03001074}
1075
1076/**
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001077 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1078 * @oh: struct omap_hwmod *
1079 *
1080 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1081 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1082 * communicate with the IP block. This interface need not be directly
1083 * connected to the MPU (and almost certainly is not), but is directly
1084 * connected to the IP block represented by @oh. Returns a pointer
1085 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1086 * error or if there does not appear to be a path from the MPU to this
1087 * IP block.
1088 */
1089static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1090{
1091 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1092 return NULL;
1093
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001094 return oh->_mpu_port;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001095};
1096
1097/**
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001098 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
Paul Walmsley63c85232009-09-03 20:14:03 +03001099 * @oh: struct omap_hwmod *
1100 *
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001101 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1102 * the register target MPU address space; or returns NULL upon error.
Paul Walmsley63c85232009-09-03 20:14:03 +03001103 */
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001104static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001105{
1106 struct omap_hwmod_ocp_if *os;
1107 struct omap_hwmod_addr_space *mem;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001108 int found = 0, i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001109
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001110 os = _find_mpu_rt_port(oh);
Paul Walmsley24dbc212012-04-19 04:04:29 -06001111 if (!os || !os->addr)
Paul Walmsley78183f32011-07-09 19:14:05 -06001112 return NULL;
1113
1114 do {
1115 mem = &os->addr[i++];
1116 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +03001117 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001118 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +03001119
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001120 return (found) ? mem : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001121}
1122
1123/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001124 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001125 * @oh: struct omap_hwmod *
1126 *
1127 * If module is marked as SWSUP_SIDLE, force the module out of slave
1128 * idle; otherwise, configure it for smart-idle. If module is marked
1129 * as SWSUP_MSUSPEND, force the module out of master standby;
1130 * otherwise, configure it for smart-standby. No return value.
1131 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001132static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001133{
Paul Walmsley43b40992010-02-22 22:09:34 -07001134 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001135 u32 v;
1136
Paul Walmsley43b40992010-02-22 22:09:34 -07001137 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001138 return;
1139
1140 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001141 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001142
Paul Walmsley43b40992010-02-22 22:09:34 -07001143 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001144 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1145 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
1146 _set_slave_idlemode(oh, idlemode, &v);
1147 }
1148
Paul Walmsley43b40992010-02-22 22:09:34 -07001149 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001150 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1151 idlemode = HWMOD_IDLEMODE_NO;
1152 } else {
1153 if (sf & SYSC_HAS_ENAWAKEUP)
1154 _enable_wakeup(oh, &v);
1155 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1156 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1157 else
1158 idlemode = HWMOD_IDLEMODE_SMART;
1159 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001160 _set_master_standbymode(oh, idlemode, &v);
1161 }
1162
Paul Walmsleya16b1f72009-12-08 16:34:17 -07001163 /*
1164 * XXX The clock framework should handle this, by
1165 * calling into this code. But this must wait until the
1166 * clock structures are tagged with omap_hwmod entries
1167 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001168 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1169 (sf & SYSC_HAS_CLOCKACTIVITY))
1170 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001171
Rajendra Nayak9980ce52010-09-21 19:58:30 +05301172 /* If slave is in SMARTIDLE, also enable wakeup */
1173 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07001174 _enable_wakeup(oh, &v);
1175
1176 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -06001177
1178 /*
1179 * Set the autoidle bit only after setting the smartidle bit
1180 * Setting this will not have any impact on the other modules.
1181 */
1182 if (sf & SYSC_HAS_AUTOIDLE) {
1183 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1184 0 : 1;
1185 _set_module_autoidle(oh, idlemode, &v);
1186 _write_sysconfig(v, oh);
1187 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001188}
1189
1190/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001191 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001192 * @oh: struct omap_hwmod *
1193 *
1194 * If module is marked as SWSUP_SIDLE, force the module into slave
1195 * idle; otherwise, configure it for smart-idle. If module is marked
1196 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1197 * configure it for smart-standby. No return value.
1198 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001199static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001200{
Paul Walmsley43b40992010-02-22 22:09:34 -07001201 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001202 u32 v;
1203
Paul Walmsley43b40992010-02-22 22:09:34 -07001204 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001205 return;
1206
1207 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001208 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001209
Paul Walmsley43b40992010-02-22 22:09:34 -07001210 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001211 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1212 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
1213 _set_slave_idlemode(oh, idlemode, &v);
1214 }
1215
Paul Walmsley43b40992010-02-22 22:09:34 -07001216 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001217 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1218 idlemode = HWMOD_IDLEMODE_FORCE;
1219 } else {
1220 if (sf & SYSC_HAS_ENAWAKEUP)
1221 _enable_wakeup(oh, &v);
1222 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1223 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1224 else
1225 idlemode = HWMOD_IDLEMODE_SMART;
1226 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001227 _set_master_standbymode(oh, idlemode, &v);
1228 }
1229
Benoit Cousson86009eb2010-12-21 21:31:28 -07001230 /* If slave is in SMARTIDLE, also enable wakeup */
1231 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1232 _enable_wakeup(oh, &v);
1233
Paul Walmsley63c85232009-09-03 20:14:03 +03001234 _write_sysconfig(v, oh);
1235}
1236
1237/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001238 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001239 * @oh: struct omap_hwmod *
1240 *
1241 * Force the module into slave idle and master suspend. No return
1242 * value.
1243 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001244static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001245{
1246 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001247 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001248
Paul Walmsley43b40992010-02-22 22:09:34 -07001249 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001250 return;
1251
1252 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001253 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001254
Paul Walmsley43b40992010-02-22 22:09:34 -07001255 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001256 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1257
Paul Walmsley43b40992010-02-22 22:09:34 -07001258 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001259 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1260
Paul Walmsley43b40992010-02-22 22:09:34 -07001261 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001262 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001263
1264 _write_sysconfig(v, oh);
1265}
1266
1267/**
1268 * _lookup - find an omap_hwmod by name
1269 * @name: find an omap_hwmod by name
1270 *
1271 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001272 */
1273static struct omap_hwmod *_lookup(const char *name)
1274{
1275 struct omap_hwmod *oh, *temp_oh;
1276
1277 oh = NULL;
1278
1279 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1280 if (!strcmp(name, temp_oh->name)) {
1281 oh = temp_oh;
1282 break;
1283 }
1284 }
1285
1286 return oh;
1287}
Paul Walmsley868c1572012-06-19 15:01:02 -06001288
Benoit Cousson6ae76992011-07-10 05:56:30 -06001289/**
1290 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1291 * @oh: struct omap_hwmod *
1292 *
1293 * Convert a clockdomain name stored in a struct omap_hwmod into a
1294 * clockdomain pointer, and save it into the struct omap_hwmod.
Paul Walmsley868c1572012-06-19 15:01:02 -06001295 * Return -EINVAL if the clkdm_name lookup failed.
Benoit Cousson6ae76992011-07-10 05:56:30 -06001296 */
1297static int _init_clkdm(struct omap_hwmod *oh)
1298{
1299 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1300 return 0;
1301
Paul Walmsley868c1572012-06-19 15:01:02 -06001302 if (!oh->clkdm_name)
1303 return 0;
Benoit Cousson6ae76992011-07-10 05:56:30 -06001304
1305 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1306 if (!oh->clkdm) {
1307 pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
1308 oh->name, oh->clkdm_name);
1309 return -EINVAL;
1310 }
1311
1312 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1313 oh->name, oh->clkdm_name);
1314
1315 return 0;
1316}
Paul Walmsley63c85232009-09-03 20:14:03 +03001317
1318/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001319 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1320 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001321 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -06001322 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001323 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001324 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001325 * Resolves all clock names embedded in the hwmod. Returns 0 on
1326 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001327 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001328static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001329{
1330 int ret = 0;
1331
Paul Walmsley48d54f32011-02-23 00:14:07 -07001332 if (oh->_state != _HWMOD_STATE_REGISTERED)
1333 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001334
1335 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1336
1337 ret |= _init_main_clk(oh);
1338 ret |= _init_interface_clks(oh);
1339 ret |= _init_opt_clks(oh);
Benoit Cousson6ae76992011-07-10 05:56:30 -06001340 ret |= _init_clkdm(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001341
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001342 if (!ret)
1343 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001344 else
1345 pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001346
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001347 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001348}
1349
1350/**
1351 * _wait_target_ready - wait for a module to leave slave idle
1352 * @oh: struct omap_hwmod *
1353 *
1354 * Wait for a module @oh to leave slave idle. Returns 0 if the module
1355 * does not have an IDLEST bit or if the module successfully leaves
1356 * slave idle; otherwise, pass along the return value of the
Benoit Coussond0f06312011-07-10 05:56:30 -06001357 * appropriate *_cm*_wait_module_ready() function.
Paul Walmsley63c85232009-09-03 20:14:03 +03001358 */
1359static int _wait_target_ready(struct omap_hwmod *oh)
1360{
1361 struct omap_hwmod_ocp_if *os;
1362 int ret;
1363
1364 if (!oh)
1365 return -EINVAL;
1366
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001367 if (oh->flags & HWMOD_NO_IDLEST)
Paul Walmsley63c85232009-09-03 20:14:03 +03001368 return 0;
1369
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001370 os = _find_mpu_rt_port(oh);
1371 if (!os)
Paul Walmsley63c85232009-09-03 20:14:03 +03001372 return 0;
1373
1374 /* XXX check module SIDLEMODE */
1375
1376 /* XXX check clock enable states */
1377
1378 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1379 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
1380 oh->prcm.omap2.idlest_reg_id,
1381 oh->prcm.omap2.idlest_idle_bit);
Paul Walmsley63c85232009-09-03 20:14:03 +03001382 } else if (cpu_is_omap44xx()) {
Benoit Coussond0f06312011-07-10 05:56:30 -06001383 if (!oh->clkdm)
1384 return -EINVAL;
1385
1386 ret = omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
1387 oh->clkdm->cm_inst,
1388 oh->clkdm->clkdm_offs,
1389 oh->prcm.omap4.clkctrl_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001390 } else {
1391 BUG();
1392 };
1393
1394 return ret;
1395}
1396
1397/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001398 * _lookup_hardreset - fill register bit info for this hwmod/reset line
Benoît Cousson5365efb2010-09-21 10:34:11 -06001399 * @oh: struct omap_hwmod *
1400 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001401 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
Benoît Cousson5365efb2010-09-21 10:34:11 -06001402 *
1403 * Return the bit position of the reset line that match the
1404 * input name. Return -ENOENT if not found.
1405 */
omar ramirezcc1226e2011-03-04 13:32:44 -07001406static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1407 struct omap_hwmod_rst_info *ohri)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001408{
1409 int i;
1410
1411 for (i = 0; i < oh->rst_lines_cnt; i++) {
1412 const char *rst_line = oh->rst_lines[i].name;
1413 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001414 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1415 ohri->st_shift = oh->rst_lines[i].st_shift;
1416 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1417 oh->name, __func__, rst_line, ohri->rst_shift,
1418 ohri->st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001419
omar ramirezcc1226e2011-03-04 13:32:44 -07001420 return 0;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001421 }
1422 }
1423
1424 return -ENOENT;
1425}
1426
1427/**
1428 * _assert_hardreset - assert the HW reset line of submodules
1429 * contained in the hwmod module.
1430 * @oh: struct omap_hwmod *
1431 * @name: name of the reset line to lookup and assert
1432 *
1433 * Some IP like dsp, ipu or iva contain processor that require
1434 * an HW reset line to be assert / deassert in order to enable fully
1435 * the IP.
1436 */
1437static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1438{
omar ramirezcc1226e2011-03-04 13:32:44 -07001439 struct omap_hwmod_rst_info ohri;
1440 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001441
1442 if (!oh)
1443 return -EINVAL;
1444
omar ramirezcc1226e2011-03-04 13:32:44 -07001445 ret = _lookup_hardreset(oh, name, &ohri);
1446 if (IS_ERR_VALUE(ret))
1447 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001448
Paul Walmsley868c1572012-06-19 15:01:02 -06001449 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
Benoît Cousson5365efb2010-09-21 10:34:11 -06001450 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001451 ohri.rst_shift);
Paul Walmsley868c1572012-06-19 15:01:02 -06001452 } else if (cpu_is_omap44xx()) {
1453 if (!oh->clkdm)
1454 return -EINVAL;
1455
Benoit Coussoneaac3292011-07-10 05:56:31 -06001456 return omap4_prminst_assert_hardreset(ohri.rst_shift,
1457 oh->clkdm->pwrdm.ptr->prcm_partition,
1458 oh->clkdm->pwrdm.ptr->prcm_offs,
1459 oh->prcm.omap4.rstctrl_offs);
Paul Walmsley868c1572012-06-19 15:01:02 -06001460 } else {
Benoît Cousson5365efb2010-09-21 10:34:11 -06001461 return -EINVAL;
Paul Walmsley868c1572012-06-19 15:01:02 -06001462 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001463}
1464
1465/**
1466 * _deassert_hardreset - deassert the HW reset line of submodules contained
1467 * in the hwmod module.
1468 * @oh: struct omap_hwmod *
1469 * @name: name of the reset line to look up and deassert
1470 *
1471 * Some IP like dsp, ipu or iva contain processor that require
1472 * an HW reset line to be assert / deassert in order to enable fully
1473 * the IP.
1474 */
1475static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1476{
omar ramirezcc1226e2011-03-04 13:32:44 -07001477 struct omap_hwmod_rst_info ohri;
1478 int ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001479
1480 if (!oh)
1481 return -EINVAL;
1482
omar ramirezcc1226e2011-03-04 13:32:44 -07001483 ret = _lookup_hardreset(oh, name, &ohri);
1484 if (IS_ERR_VALUE(ret))
1485 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001486
omar ramirezcc1226e2011-03-04 13:32:44 -07001487 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1488 ret = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
1489 ohri.rst_shift,
1490 ohri.st_shift);
1491 } else if (cpu_is_omap44xx()) {
1492 if (ohri.st_shift)
1493 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
1494 oh->name, name);
Paul Walmsley868c1572012-06-19 15:01:02 -06001495
1496 if (!oh->clkdm)
1497 return -EINVAL;
1498
Benoit Coussoneaac3292011-07-10 05:56:31 -06001499 ret = omap4_prminst_deassert_hardreset(ohri.rst_shift,
1500 oh->clkdm->pwrdm.ptr->prcm_partition,
1501 oh->clkdm->pwrdm.ptr->prcm_offs,
1502 oh->prcm.omap4.rstctrl_offs);
omar ramirezcc1226e2011-03-04 13:32:44 -07001503 } else {
Benoît Cousson5365efb2010-09-21 10:34:11 -06001504 return -EINVAL;
omar ramirezcc1226e2011-03-04 13:32:44 -07001505 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001506
omar ramirezcc1226e2011-03-04 13:32:44 -07001507 if (ret == -EBUSY)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001508 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1509
omar ramirezcc1226e2011-03-04 13:32:44 -07001510 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001511}
1512
1513/**
1514 * _read_hardreset - read the HW reset line state of submodules
1515 * contained in the hwmod module
1516 * @oh: struct omap_hwmod *
1517 * @name: name of the reset line to look up and read
1518 *
1519 * Return the state of the reset line.
1520 */
1521static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1522{
omar ramirezcc1226e2011-03-04 13:32:44 -07001523 struct omap_hwmod_rst_info ohri;
1524 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001525
1526 if (!oh)
1527 return -EINVAL;
1528
omar ramirezcc1226e2011-03-04 13:32:44 -07001529 ret = _lookup_hardreset(oh, name, &ohri);
1530 if (IS_ERR_VALUE(ret))
1531 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001532
1533 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1534 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001535 ohri.st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001536 } else if (cpu_is_omap44xx()) {
Paul Walmsley868c1572012-06-19 15:01:02 -06001537 if (!oh->clkdm)
1538 return -EINVAL;
1539
Benoit Coussoneaac3292011-07-10 05:56:31 -06001540 return omap4_prminst_is_hardreset_asserted(ohri.rst_shift,
1541 oh->clkdm->pwrdm.ptr->prcm_partition,
1542 oh->clkdm->pwrdm.ptr->prcm_offs,
1543 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001544 } else {
1545 return -EINVAL;
1546 }
1547}
1548
1549/**
Paul Walmsley747834a2012-04-18 19:10:04 -06001550 * _are_any_hardreset_lines_asserted - return true if part of @oh is hard-reset
1551 * @oh: struct omap_hwmod *
1552 *
1553 * If any hardreset line associated with @oh is asserted, then return true.
1554 * Otherwise, if @oh has no hardreset lines associated with it, or if
1555 * no hardreset lines associated with @oh are asserted, then return false.
1556 * This function is used to avoid executing some parts of the IP block
1557 * enable/disable sequence if a hardreset line is set.
1558 */
1559static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1560{
1561 int i;
1562
1563 if (oh->rst_lines_cnt == 0)
1564 return false;
1565
1566 for (i = 0; i < oh->rst_lines_cnt; i++)
1567 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1568 return true;
1569
1570 return false;
1571}
1572
1573/**
1574 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1575 * @oh: struct omap_hwmod *
1576 *
1577 * Disable the PRCM module mode related to the hwmod @oh.
1578 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1579 */
1580static int _omap4_disable_module(struct omap_hwmod *oh)
1581{
1582 int v;
1583
1584 /* The module mode does not exist prior OMAP4 */
1585 if (!cpu_is_omap44xx())
1586 return -EINVAL;
1587
1588 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1589 return -EINVAL;
1590
1591 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1592
1593 omap4_cminst_module_disable(oh->clkdm->prcm_partition,
1594 oh->clkdm->cm_inst,
1595 oh->clkdm->clkdm_offs,
1596 oh->prcm.omap4.clkctrl_offs);
1597
1598 if (_are_any_hardreset_lines_asserted(oh))
1599 return 0;
1600
1601 v = _omap4_wait_target_disable(oh);
1602 if (v)
1603 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1604 oh->name);
1605
1606 return 0;
1607}
1608
1609/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001610 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001611 * @oh: struct omap_hwmod *
1612 *
1613 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Paul Walmsley30e105c2012-04-19 00:49:09 -06001614 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1615 * reset this way, -EINVAL if the hwmod is in the wrong state,
1616 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001617 *
1618 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001619 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001620 * use the SYSCONFIG softreset bit to provide the status.
1621 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001622 * Note that some IP like McBSP do have reset control but don't have
1623 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001624 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001625static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001626{
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001627 u32 v, softrst_mask;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001628 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001629 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001630
Paul Walmsley43b40992010-02-22 22:09:34 -07001631 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001632 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley30e105c2012-04-19 00:49:09 -06001633 return -ENOENT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001634
1635 /* clocks must be on for this operation */
1636 if (oh->_state != _HWMOD_STATE_ENABLED) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001637 pr_warning("omap_hwmod: %s: reset can only be entered from "
1638 "enabled state\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001639 return -EINVAL;
1640 }
1641
Benoit Cousson96835af2010-09-21 18:57:58 +02001642 /* For some modules, all optionnal clocks need to be enabled as well */
1643 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1644 _enable_optional_clocks(oh);
1645
Paul Walmsleybd361792010-12-14 12:42:35 -07001646 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001647
1648 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001649 ret = _set_softreset(oh, &v);
1650 if (ret)
1651 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001652 _write_sysconfig(v, oh);
1653
Fernando Guzman Lugod99de7f2012-04-13 05:08:03 -06001654 if (oh->class->sysc->srst_udelay)
1655 udelay(oh->class->sysc->srst_udelay);
1656
Benoit Cousson2cb06812010-09-21 18:57:59 +02001657 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001658 omap_test_timeout((omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001659 oh->class->sysc->syss_offs)
1660 & SYSS_RESETDONE_MASK),
1661 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001662 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
1663 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001664 omap_test_timeout(!(omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001665 oh->class->sysc->sysc_offs)
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001666 & softrst_mask),
Benoit Cousson2cb06812010-09-21 18:57:59 +02001667 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001668 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001669
Benoît Cousson5365efb2010-09-21 10:34:11 -06001670 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001671 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1672 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001673 else
Benoît Cousson5365efb2010-09-21 10:34:11 -06001674 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001675
1676 /*
1677 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1678 * _wait_target_ready() or _reset()
1679 */
1680
Benoit Cousson96835af2010-09-21 18:57:58 +02001681 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1682
1683dis_opt_clks:
1684 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1685 _disable_optional_clocks(oh);
1686
1687 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001688}
1689
1690/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001691 * _reset - reset an omap_hwmod
1692 * @oh: struct omap_hwmod *
1693 *
Paul Walmsley30e105c2012-04-19 00:49:09 -06001694 * Resets an omap_hwmod @oh. If the module has a custom reset
1695 * function pointer defined, then call it to reset the IP block, and
1696 * pass along its return value to the caller. Otherwise, if the IP
1697 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1698 * associated with it, call a function to reset the IP block via that
1699 * method, and pass along the return value to the caller. Finally, if
1700 * the IP block has some hardreset lines associated with it, assert
1701 * all of those, but do _not_ deassert them. (This is because driver
1702 * authors have expressed an apparent requirement to control the
1703 * deassertion of the hardreset lines themselves.)
1704 *
1705 * The default software reset mechanism for most OMAP IP blocks is
1706 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1707 * hwmods cannot be reset via this method. Some are not targets and
1708 * therefore have no OCP header registers to access. Others (like the
1709 * IVA) have idiosyncratic reset sequences. So for these relatively
1710 * rare cases, custom reset code can be supplied in the struct
1711 * omap_hwmod_class .reset function pointer. Passes along the return
1712 * value from either _ocp_softreset() or the custom reset function -
1713 * these must return -EINVAL if the hwmod cannot be reset this way or
1714 * if the hwmod is in the wrong state, -ETIMEDOUT if the module did
1715 * not reset in time, or 0 upon success.
Paul Walmsleybd361792010-12-14 12:42:35 -07001716 */
1717static int _reset(struct omap_hwmod *oh)
1718{
Paul Walmsley30e105c2012-04-19 00:49:09 -06001719 int i, r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001720
1721 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1722
Paul Walmsley30e105c2012-04-19 00:49:09 -06001723 if (oh->class->reset) {
1724 r = oh->class->reset(oh);
1725 } else {
1726 if (oh->rst_lines_cnt > 0) {
1727 for (i = 0; i < oh->rst_lines_cnt; i++)
1728 _assert_hardreset(oh, oh->rst_lines[i].name);
1729 return 0;
1730 } else {
1731 r = _ocp_softreset(oh);
1732 if (r == -ENOENT)
1733 r = 0;
1734 }
1735 }
Paul Walmsleybd361792010-12-14 12:42:35 -07001736
Paul Walmsley30e105c2012-04-19 00:49:09 -06001737 /*
1738 * OCP_SYSCONFIG bits need to be reprogrammed after a
1739 * softreset. The _enable() function should be split to avoid
1740 * the rewrite of the OCP_SYSCONFIG register.
1741 */
Rajendra Nayak28008522012-03-13 22:55:23 +05301742 if (oh->class->sysc) {
1743 _update_sysc_cache(oh);
1744 _enable_sysc(oh);
1745 }
1746
Paul Walmsley30e105c2012-04-19 00:49:09 -06001747 return r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001748}
1749
1750/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001751 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001752 * @oh: struct omap_hwmod *
1753 *
1754 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001755 * register target. Returns -EINVAL if the hwmod is in the wrong
1756 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001757 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001758static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001759{
Paul Walmsley747834a2012-04-18 19:10:04 -06001760 int r;
Rajendra Nayak665d0012011-07-10 05:57:07 -06001761 int hwsup = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001762
Benoit Cousson34617e22011-07-01 22:54:07 +02001763 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1764
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001765 /*
Paul Walmsley64813c32012-04-18 19:10:03 -06001766 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
1767 * state at init. Now that someone is really trying to enable
1768 * them, just ensure that the hwmod mux is set.
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001769 */
1770 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
1771 /*
1772 * If the caller has mux data populated, do the mux'ing
1773 * which wouldn't have been done as part of the _enable()
1774 * done during setup.
1775 */
1776 if (oh->mux)
1777 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
1778
1779 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
1780 return 0;
1781 }
1782
Paul Walmsley63c85232009-09-03 20:14:03 +03001783 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1784 oh->_state != _HWMOD_STATE_IDLE &&
1785 oh->_state != _HWMOD_STATE_DISABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001786 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
1787 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001788 return -EINVAL;
1789 }
1790
Benoit Cousson31f62862011-07-01 22:54:05 +02001791 /*
Paul Walmsley747834a2012-04-18 19:10:04 -06001792 * If an IP block contains HW reset lines and any of them are
1793 * asserted, we let integration code associated with that
1794 * block handle the enable. We've received very little
1795 * information on what those driver authors need, and until
1796 * detailed information is provided and the driver code is
1797 * posted to the public lists, this is probably the best we
1798 * can do.
Benoit Cousson31f62862011-07-01 22:54:05 +02001799 */
Paul Walmsley747834a2012-04-18 19:10:04 -06001800 if (_are_any_hardreset_lines_asserted(oh))
1801 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001802
Rajendra Nayak665d0012011-07-10 05:57:07 -06001803 /* Mux pins for device runtime if populated */
1804 if (oh->mux && (!oh->mux->enabled ||
1805 ((oh->_state == _HWMOD_STATE_IDLE) &&
1806 oh->mux->pads_dynamic)))
1807 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
Benoit Cousson34617e22011-07-01 22:54:07 +02001808
Rajendra Nayak665d0012011-07-10 05:57:07 -06001809 _add_initiator_dep(oh, mpu_oh);
1810
1811 if (oh->clkdm) {
1812 /*
1813 * A clockdomain must be in SW_SUP before enabling
1814 * completely the module. The clockdomain can be set
1815 * in HW_AUTO only when the module become ready.
1816 */
1817 hwsup = clkdm_in_hwsup(oh->clkdm);
1818 r = clkdm_hwmod_enable(oh->clkdm, oh);
1819 if (r) {
1820 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1821 oh->name, oh->clkdm->name, r);
1822 return r;
1823 }
Benoit Cousson34617e22011-07-01 22:54:07 +02001824 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06001825
1826 _enable_clocks(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001827 _enable_module(oh);
Benoit Cousson34617e22011-07-01 22:54:07 +02001828
Rajendra Nayak665d0012011-07-10 05:57:07 -06001829 r = _wait_target_ready(oh);
1830 if (!r) {
1831 /*
1832 * Set the clockdomain to HW_AUTO only if the target is ready,
1833 * assuming that the previous state was HW_AUTO
1834 */
1835 if (oh->clkdm && hwsup)
1836 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02001837
Rajendra Nayak665d0012011-07-10 05:57:07 -06001838 oh->_state = _HWMOD_STATE_ENABLED;
1839
1840 /* Access the sysconfig only if the target is ready */
1841 if (oh->class->sysc) {
1842 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1843 _update_sysc_cache(oh);
1844 _enable_sysc(oh);
1845 }
1846 } else {
1847 _disable_clocks(oh);
1848 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1849 oh->name, r);
1850
1851 if (oh->clkdm)
1852 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001853 }
1854
Paul Walmsley63c85232009-09-03 20:14:03 +03001855 return r;
1856}
1857
1858/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001859 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001860 * @oh: struct omap_hwmod *
1861 *
1862 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001863 * no further work. Returns -EINVAL if the hwmod is in the wrong
1864 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001865 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001866static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001867{
Benoit Cousson34617e22011-07-01 22:54:07 +02001868 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1869
Paul Walmsley63c85232009-09-03 20:14:03 +03001870 if (oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001871 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
1872 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001873 return -EINVAL;
1874 }
1875
Paul Walmsley747834a2012-04-18 19:10:04 -06001876 if (_are_any_hardreset_lines_asserted(oh))
1877 return 0;
1878
Paul Walmsley43b40992010-02-22 22:09:34 -07001879 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001880 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001881 _del_initiator_dep(oh, mpu_oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001882
1883 _omap4_disable_module(oh);
1884
Benoit Cousson45c38252011-07-10 05:56:33 -06001885 /*
1886 * The module must be in idle mode before disabling any parents
1887 * clocks. Otherwise, the parent clock might be disabled before
1888 * the module transition is done, and thus will prevent the
1889 * transition to complete properly.
1890 */
1891 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001892 if (oh->clkdm)
1893 clkdm_hwmod_disable(oh->clkdm, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001894
Tony Lindgren8d9af882010-12-22 18:42:35 -08001895 /* Mux pins for device idle if populated */
Tony Lindgren029268e2011-03-11 11:32:25 -08001896 if (oh->mux && oh->mux->pads_dynamic)
Tony Lindgren8d9af882010-12-22 18:42:35 -08001897 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
1898
Paul Walmsley63c85232009-09-03 20:14:03 +03001899 oh->_state = _HWMOD_STATE_IDLE;
1900
1901 return 0;
1902}
1903
1904/**
Kishon Vijay Abraham I95992172011-03-10 03:50:08 -07001905 * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
1906 * @oh: struct omap_hwmod *
1907 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
1908 *
1909 * Sets the IP block's OCP autoidle bit in hardware, and updates our
1910 * local copy. Intended to be used by drivers that require
1911 * direct manipulation of the AUTOIDLE bits.
1912 * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
1913 * along the return value from _set_module_autoidle().
1914 *
1915 * Any users of this function should be scrutinized carefully.
1916 */
1917int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
1918{
1919 u32 v;
1920 int retval = 0;
1921 unsigned long flags;
1922
1923 if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
1924 return -EINVAL;
1925
1926 spin_lock_irqsave(&oh->_lock, flags);
1927
1928 v = oh->_sysc_cache;
1929
1930 retval = _set_module_autoidle(oh, autoidle, &v);
1931
1932 if (!retval)
1933 _write_sysconfig(v, oh);
1934
1935 spin_unlock_irqrestore(&oh->_lock, flags);
1936
1937 return retval;
1938}
1939
1940/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001941 * _shutdown - shutdown an omap_hwmod
1942 * @oh: struct omap_hwmod *
1943 *
1944 * Shut down an omap_hwmod @oh. This should be called when the driver
1945 * used for the hwmod is removed or unloaded or if the driver is not
1946 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1947 * state or returns 0.
1948 */
1949static int _shutdown(struct omap_hwmod *oh)
1950{
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001951 int ret, i;
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001952 u8 prev_state;
1953
Paul Walmsley63c85232009-09-03 20:14:03 +03001954 if (oh->_state != _HWMOD_STATE_IDLE &&
1955 oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001956 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
1957 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001958 return -EINVAL;
1959 }
1960
Paul Walmsley747834a2012-04-18 19:10:04 -06001961 if (_are_any_hardreset_lines_asserted(oh))
1962 return 0;
1963
Paul Walmsley63c85232009-09-03 20:14:03 +03001964 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
1965
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001966 if (oh->class->pre_shutdown) {
1967 prev_state = oh->_state;
1968 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001969 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001970 ret = oh->class->pre_shutdown(oh);
1971 if (ret) {
1972 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001973 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001974 return ret;
1975 }
1976 }
1977
Miguel Vadillo6481c732011-07-01 22:54:02 +02001978 if (oh->class->sysc) {
1979 if (oh->_state == _HWMOD_STATE_IDLE)
1980 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001981 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02001982 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001983
Benoit Cousson3827f942010-09-21 10:34:08 -06001984 /* clocks and deps are already disabled in idle */
1985 if (oh->_state == _HWMOD_STATE_ENABLED) {
1986 _del_initiator_dep(oh, mpu_oh);
1987 /* XXX what about the other system initiators here? dma, dsp */
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001988 _omap4_disable_module(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001989 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001990 if (oh->clkdm)
1991 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06001992 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001993 /* XXX Should this code also force-disable the optional clocks? */
1994
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001995 for (i = 0; i < oh->rst_lines_cnt; i++)
1996 _assert_hardreset(oh, oh->rst_lines[i].name);
Benoit Cousson31f62862011-07-01 22:54:05 +02001997
Tony Lindgren8d9af882010-12-22 18:42:35 -08001998 /* Mux pins to safe mode or use populated off mode values */
1999 if (oh->mux)
2000 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03002001
2002 oh->_state = _HWMOD_STATE_DISABLED;
2003
2004 return 0;
2005}
2006
2007/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002008 * _init_mpu_rt_base - populate the virtual address for a hwmod
2009 * @oh: struct omap_hwmod * to locate the virtual address
2010 *
2011 * Cache the virtual address used by the MPU to access this IP block's
2012 * registers. This address is needed early so the OCP registers that
2013 * are part of the device's address space can be ioremapped properly.
2014 * No return value.
2015 */
2016static void __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data)
2017{
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002018 struct omap_hwmod_addr_space *mem;
2019 void __iomem *va_start;
2020
2021 if (!oh)
2022 return;
2023
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002024 _save_mpu_port_index(oh);
2025
Paul Walmsley381d0332012-04-19 00:58:22 -06002026 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2027 return;
2028
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002029 mem = _find_mpu_rt_addr_space(oh);
2030 if (!mem) {
2031 pr_debug("omap_hwmod: %s: no MPU register target found\n",
2032 oh->name);
2033 return;
2034 }
2035
2036 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
2037 if (!va_start) {
2038 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
2039 return;
2040 }
2041
2042 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
2043 oh->name, va_start);
2044
2045 oh->_mpu_rt_va = va_start;
Paul Walmsley381d0332012-04-19 00:58:22 -06002046}
2047
2048/**
2049 * _init - initialize internal data for the hwmod @oh
2050 * @oh: struct omap_hwmod *
2051 * @n: (unused)
2052 *
2053 * Look up the clocks and the address space used by the MPU to access
2054 * registers belonging to the hwmod @oh. @oh must already be
2055 * registered at this point. This is the first of two phases for
2056 * hwmod initialization. Code called here does not touch any hardware
2057 * registers, it simply prepares internal data structures. Returns 0
2058 * upon success or if the hwmod isn't registered, or -EINVAL upon
2059 * failure.
2060 */
2061static int __init _init(struct omap_hwmod *oh, void *data)
2062{
2063 int r;
2064
2065 if (oh->_state != _HWMOD_STATE_REGISTERED)
2066 return 0;
2067
2068 _init_mpu_rt_base(oh, NULL);
2069
2070 r = _init_clocks(oh, NULL);
2071 if (IS_ERR_VALUE(r)) {
2072 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2073 return -EINVAL;
2074 }
2075
2076 oh->_state = _HWMOD_STATE_INITIALIZED;
2077
2078 return 0;
2079}
2080
2081/**
Paul Walmsley64813c32012-04-18 19:10:03 -06002082 * _setup_iclk_autoidle - configure an IP block's interface clocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002083 * @oh: struct omap_hwmod *
2084 *
Paul Walmsley64813c32012-04-18 19:10:03 -06002085 * Set up the module's interface clocks. XXX This function is still mostly
2086 * a stub; implementing this properly requires iclk autoidle usecounting in
2087 * the clock code. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03002088 */
Paul Walmsley64813c32012-04-18 19:10:03 -06002089static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002090{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002091 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002092 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002093 int i = 0;
Paul Walmsley381d0332012-04-19 00:58:22 -06002094 if (oh->_state != _HWMOD_STATE_INITIALIZED)
Paul Walmsley64813c32012-04-18 19:10:03 -06002095 return;
Paul Walmsley48d54f32011-02-23 00:14:07 -07002096
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002097 p = oh->slave_ports.next;
Paul Walmsley63c85232009-09-03 20:14:03 +03002098
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002099 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002100 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002101 if (!os->_clk)
Paul Walmsley64813c32012-04-18 19:10:03 -06002102 continue;
Paul Walmsley63c85232009-09-03 20:14:03 +03002103
Paul Walmsley64813c32012-04-18 19:10:03 -06002104 if (os->flags & OCPIF_SWSUP_IDLE) {
2105 /* XXX omap_iclk_deny_idle(c); */
2106 } else {
2107 /* XXX omap_iclk_allow_idle(c); */
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002108 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +03002109 }
2110 }
2111
Paul Walmsley64813c32012-04-18 19:10:03 -06002112 return;
2113}
2114
2115/**
2116 * _setup_reset - reset an IP block during the setup process
2117 * @oh: struct omap_hwmod *
2118 *
2119 * Reset the IP block corresponding to the hwmod @oh during the setup
2120 * process. The IP block is first enabled so it can be successfully
2121 * reset. Returns 0 upon success or a negative error code upon
2122 * failure.
2123 */
2124static int __init _setup_reset(struct omap_hwmod *oh)
2125{
2126 int r;
2127
2128 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2129 return -EINVAL;
Paul Walmsley63c85232009-09-03 20:14:03 +03002130
Paul Walmsley747834a2012-04-18 19:10:04 -06002131 if (oh->rst_lines_cnt == 0) {
2132 r = _enable(oh);
2133 if (r) {
2134 pr_warning("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2135 oh->name, oh->_state);
2136 return -EINVAL;
2137 }
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002138 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002139
Rajendra Nayak28008522012-03-13 22:55:23 +05302140 if (!(oh->flags & HWMOD_INIT_NO_RESET))
Paul Walmsley64813c32012-04-18 19:10:03 -06002141 r = _reset(oh);
2142
2143 return r;
2144}
2145
2146/**
2147 * _setup_postsetup - transition to the appropriate state after _setup
2148 * @oh: struct omap_hwmod *
2149 *
2150 * Place an IP block represented by @oh into a "post-setup" state --
2151 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2152 * this function is called at the end of _setup().) The postsetup
2153 * state for an IP block can be changed by calling
2154 * omap_hwmod_enter_postsetup_state() early in the boot process,
2155 * before one of the omap_hwmod_setup*() functions are called for the
2156 * IP block.
2157 *
2158 * The IP block stays in this state until a PM runtime-based driver is
2159 * loaded for that IP block. A post-setup state of IDLE is
2160 * appropriate for almost all IP blocks with runtime PM-enabled
2161 * drivers, since those drivers are able to enable the IP block. A
2162 * post-setup state of ENABLED is appropriate for kernels with PM
2163 * runtime disabled. The DISABLED state is appropriate for unusual IP
2164 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2165 * included, since the WDTIMER starts running on reset and will reset
2166 * the MPU if left active.
2167 *
2168 * This post-setup mechanism is deprecated. Once all of the OMAP
2169 * drivers have been converted to use PM runtime, and all of the IP
2170 * block data and interconnect data is available to the hwmod code, it
2171 * should be possible to replace this mechanism with a "lazy reset"
2172 * arrangement. In a "lazy reset" setup, each IP block is enabled
2173 * when the driver first probes, then all remaining IP blocks without
2174 * drivers are either shut down or enabled after the drivers have
2175 * loaded. However, this cannot take place until the above
2176 * preconditions have been met, since otherwise the late reset code
2177 * has no way of knowing which IP blocks are in use by drivers, and
2178 * which ones are unused.
2179 *
2180 * No return value.
2181 */
2182static void __init _setup_postsetup(struct omap_hwmod *oh)
2183{
2184 u8 postsetup_state;
2185
2186 if (oh->rst_lines_cnt > 0)
2187 return;
Benoit Cousson76e55892010-09-21 10:34:11 -06002188
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002189 postsetup_state = oh->_postsetup_state;
2190 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2191 postsetup_state = _HWMOD_STATE_ENABLED;
2192
2193 /*
2194 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2195 * it should be set by the core code as a runtime flag during startup
2196 */
2197 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002198 (postsetup_state == _HWMOD_STATE_IDLE)) {
2199 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002200 postsetup_state = _HWMOD_STATE_ENABLED;
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002201 }
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002202
2203 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002204 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002205 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2206 _shutdown(oh);
2207 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2208 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2209 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03002210
Paul Walmsley64813c32012-04-18 19:10:03 -06002211 return;
2212}
2213
2214/**
2215 * _setup - prepare IP block hardware for use
2216 * @oh: struct omap_hwmod *
2217 * @n: (unused, pass NULL)
2218 *
2219 * Configure the IP block represented by @oh. This may include
2220 * enabling the IP block, resetting it, and placing it into a
2221 * post-setup state, depending on the type of IP block and applicable
2222 * flags. IP blocks are reset to prevent any previous configuration
2223 * by the bootloader or previous operating system from interfering
2224 * with power management or other parts of the system. The reset can
2225 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2226 * two phases for hwmod initialization. Code called here generally
2227 * affects the IP block hardware, or system integration hardware
2228 * associated with the IP block. Returns 0.
2229 */
2230static int __init _setup(struct omap_hwmod *oh, void *data)
2231{
2232 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2233 return 0;
2234
2235 _setup_iclk_autoidle(oh);
2236
2237 if (!_setup_reset(oh))
2238 _setup_postsetup(oh);
2239
Paul Walmsley63c85232009-09-03 20:14:03 +03002240 return 0;
2241}
2242
Benoit Cousson0102b622010-12-21 21:31:27 -07002243/**
2244 * _register - register a struct omap_hwmod
2245 * @oh: struct omap_hwmod *
2246 *
2247 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2248 * already has been registered by the same name; -EINVAL if the
2249 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2250 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2251 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2252 * success.
2253 *
2254 * XXX The data should be copied into bootmem, so the original data
2255 * should be marked __initdata and freed after init. This would allow
2256 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2257 * that the copy process would be relatively complex due to the large number
2258 * of substructures.
2259 */
Benoit Cousson01592df2010-12-21 21:31:28 -07002260static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07002261{
Benoit Cousson0102b622010-12-21 21:31:27 -07002262 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2263 (oh->_state != _HWMOD_STATE_UNKNOWN))
2264 return -EINVAL;
2265
Benoit Cousson0102b622010-12-21 21:31:27 -07002266 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2267
Benoit Coussonce35b242010-12-21 21:31:28 -07002268 if (_lookup(oh->name))
2269 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07002270
Benoit Cousson0102b622010-12-21 21:31:27 -07002271 list_add_tail(&oh->node, &omap_hwmod_list);
2272
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002273 INIT_LIST_HEAD(&oh->master_ports);
2274 INIT_LIST_HEAD(&oh->slave_ports);
Benoit Cousson0102b622010-12-21 21:31:27 -07002275 spin_lock_init(&oh->_lock);
2276
2277 oh->_state = _HWMOD_STATE_REGISTERED;
2278
Paul Walmsley569edd702011-02-23 00:14:06 -07002279 /*
2280 * XXX Rather than doing a strcmp(), this should test a flag
2281 * set in the hwmod data, inserted by the autogenerator code.
2282 */
2283 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2284 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07002285
Paul Walmsley569edd702011-02-23 00:14:06 -07002286 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07002287}
Paul Walmsley63c85232009-09-03 20:14:03 +03002288
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002289/**
2290 * _alloc_links - return allocated memory for hwmod links
2291 * @ml: pointer to a struct omap_hwmod_link * for the master link
2292 * @sl: pointer to a struct omap_hwmod_link * for the slave link
2293 *
2294 * Return pointers to two struct omap_hwmod_link records, via the
2295 * addresses pointed to by @ml and @sl. Will first attempt to return
2296 * memory allocated as part of a large initial block, but if that has
2297 * been exhausted, will allocate memory itself. Since ideally this
2298 * second allocation path will never occur, the number of these
2299 * 'supplemental' allocations will be logged when debugging is
2300 * enabled. Returns 0.
2301 */
2302static int __init _alloc_links(struct omap_hwmod_link **ml,
2303 struct omap_hwmod_link **sl)
2304{
2305 unsigned int sz;
2306
2307 if ((free_ls + LINKS_PER_OCP_IF) <= max_ls) {
2308 *ml = &linkspace[free_ls++];
2309 *sl = &linkspace[free_ls++];
2310 return 0;
2311 }
2312
2313 sz = sizeof(struct omap_hwmod_link) * LINKS_PER_OCP_IF;
2314
2315 *sl = NULL;
2316 *ml = alloc_bootmem(sz);
2317
2318 memset(*ml, 0, sz);
2319
2320 *sl = (void *)(*ml) + sizeof(struct omap_hwmod_link);
2321
2322 ls_supp++;
2323 pr_debug("omap_hwmod: supplemental link allocations needed: %d\n",
2324 ls_supp * LINKS_PER_OCP_IF);
2325
2326 return 0;
2327};
2328
2329/**
2330 * _add_link - add an interconnect between two IP blocks
2331 * @oi: pointer to a struct omap_hwmod_ocp_if record
2332 *
2333 * Add struct omap_hwmod_link records connecting the master IP block
2334 * specified in @oi->master to @oi, and connecting the slave IP block
2335 * specified in @oi->slave to @oi. This code is assumed to run before
2336 * preemption or SMP has been enabled, thus avoiding the need for
2337 * locking in this code. Changes to this assumption will require
2338 * additional locking. Returns 0.
2339 */
2340static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2341{
2342 struct omap_hwmod_link *ml, *sl;
2343
2344 pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2345 oi->slave->name);
2346
2347 _alloc_links(&ml, &sl);
2348
2349 ml->ocp_if = oi;
2350 INIT_LIST_HEAD(&ml->node);
2351 list_add(&ml->node, &oi->master->master_ports);
2352 oi->master->masters_cnt++;
2353
2354 sl->ocp_if = oi;
2355 INIT_LIST_HEAD(&sl->node);
2356 list_add(&sl->node, &oi->slave->slave_ports);
2357 oi->slave->slaves_cnt++;
2358
2359 return 0;
2360}
2361
2362/**
2363 * _register_link - register a struct omap_hwmod_ocp_if
2364 * @oi: struct omap_hwmod_ocp_if *
2365 *
2366 * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
2367 * has already been registered; -EINVAL if @oi is NULL or if the
2368 * record pointed to by @oi is missing required fields; or 0 upon
2369 * success.
2370 *
2371 * XXX The data should be copied into bootmem, so the original data
2372 * should be marked __initdata and freed after init. This would allow
2373 * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2374 */
2375static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2376{
2377 if (!oi || !oi->master || !oi->slave || !oi->user)
2378 return -EINVAL;
2379
2380 if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2381 return -EEXIST;
2382
2383 pr_debug("omap_hwmod: registering link from %s to %s\n",
2384 oi->master->name, oi->slave->name);
2385
2386 /*
2387 * Register the connected hwmods, if they haven't been
2388 * registered already
2389 */
2390 if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2391 _register(oi->master);
2392
2393 if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2394 _register(oi->slave);
2395
2396 _add_link(oi);
2397
2398 oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2399
2400 return 0;
2401}
2402
2403/**
2404 * _alloc_linkspace - allocate large block of hwmod links
2405 * @ois: pointer to an array of struct omap_hwmod_ocp_if records to count
2406 *
2407 * Allocate a large block of struct omap_hwmod_link records. This
2408 * improves boot time significantly by avoiding the need to allocate
2409 * individual records one by one. If the number of records to
2410 * allocate in the block hasn't been manually specified, this function
2411 * will count the number of struct omap_hwmod_ocp_if records in @ois
2412 * and use that to determine the allocation size. For SoC families
2413 * that require multiple list registrations, such as OMAP3xxx, this
2414 * estimation process isn't optimal, so manual estimation is advised
2415 * in those cases. Returns -EEXIST if the allocation has already occurred
2416 * or 0 upon success.
2417 */
2418static int __init _alloc_linkspace(struct omap_hwmod_ocp_if **ois)
2419{
2420 unsigned int i = 0;
2421 unsigned int sz;
2422
2423 if (linkspace) {
2424 WARN(1, "linkspace already allocated\n");
2425 return -EEXIST;
2426 }
2427
2428 if (max_ls == 0)
2429 while (ois[i++])
2430 max_ls += LINKS_PER_OCP_IF;
2431
2432 sz = sizeof(struct omap_hwmod_link) * max_ls;
2433
2434 pr_debug("omap_hwmod: %s: allocating %d byte linkspace (%d links)\n",
2435 __func__, sz, max_ls);
2436
2437 linkspace = alloc_bootmem(sz);
2438
2439 memset(linkspace, 0, sz);
2440
2441 return 0;
2442}
Paul Walmsley63c85232009-09-03 20:14:03 +03002443
2444/* Public functions */
2445
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002446u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002447{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002448 if (oh->flags & HWMOD_16BIT_REG)
2449 return __raw_readw(oh->_mpu_rt_va + reg_offs);
2450 else
2451 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002452}
2453
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002454void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002455{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002456 if (oh->flags & HWMOD_16BIT_REG)
2457 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
2458 else
2459 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002460}
2461
Paul Walmsley887adea2010-07-26 16:34:33 -06002462/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002463 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
2464 * @oh: struct omap_hwmod *
2465 *
2466 * This is a public function exposed to drivers. Some drivers may need to do
2467 * some settings before and after resetting the device. Those drivers after
2468 * doing the necessary settings could use this function to start a reset by
2469 * setting the SYSCONFIG.SOFTRESET bit.
2470 */
2471int omap_hwmod_softreset(struct omap_hwmod *oh)
2472{
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002473 u32 v;
2474 int ret;
2475
2476 if (!oh || !(oh->_sysc_cache))
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002477 return -EINVAL;
2478
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002479 v = oh->_sysc_cache;
2480 ret = _set_softreset(oh, &v);
2481 if (ret)
2482 goto error;
2483 _write_sysconfig(v, oh);
2484
2485error:
2486 return ret;
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002487}
2488
2489/**
Paul Walmsley887adea2010-07-26 16:34:33 -06002490 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
2491 * @oh: struct omap_hwmod *
2492 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
2493 *
2494 * Sets the IP block's OCP slave idlemode in hardware, and updates our
2495 * local copy. Intended to be used by drivers that have some erratum
2496 * that requires direct manipulation of the SIDLEMODE bits. Returns
2497 * -EINVAL if @oh is null, or passes along the return value from
2498 * _set_slave_idlemode().
2499 *
2500 * XXX Does this function have any current users? If not, we should
2501 * remove it; it is better to let the rest of the hwmod code handle this.
2502 * Any users of this function should be scrutinized carefully.
2503 */
Kevin Hilman46273e62010-01-26 20:13:03 -07002504int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
2505{
2506 u32 v;
2507 int retval = 0;
2508
2509 if (!oh)
2510 return -EINVAL;
2511
2512 v = oh->_sysc_cache;
2513
2514 retval = _set_slave_idlemode(oh, idlemode, &v);
2515 if (!retval)
2516 _write_sysconfig(v, oh);
2517
2518 return retval;
2519}
2520
Paul Walmsley63c85232009-09-03 20:14:03 +03002521/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002522 * omap_hwmod_lookup - look up a registered omap_hwmod by name
2523 * @name: name of the omap_hwmod to look up
2524 *
2525 * Given a @name of an omap_hwmod, return a pointer to the registered
2526 * struct omap_hwmod *, or NULL upon error.
2527 */
2528struct omap_hwmod *omap_hwmod_lookup(const char *name)
2529{
2530 struct omap_hwmod *oh;
2531
2532 if (!name)
2533 return NULL;
2534
Paul Walmsley63c85232009-09-03 20:14:03 +03002535 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002536
2537 return oh;
2538}
2539
2540/**
2541 * omap_hwmod_for_each - call function for each registered omap_hwmod
2542 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06002543 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03002544 *
2545 * Call @fn for each registered omap_hwmod, passing @data to each
2546 * function. @fn must return 0 for success or any other value for
2547 * failure. If @fn returns non-zero, the iteration across omap_hwmods
2548 * will stop and the non-zero return value will be passed to the
2549 * caller of omap_hwmod_for_each(). @fn is called with
2550 * omap_hwmod_for_each() held.
2551 */
Paul Walmsley97d60162010-07-26 16:34:30 -06002552int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
2553 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03002554{
2555 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05302556 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002557
2558 if (!fn)
2559 return -EINVAL;
2560
Paul Walmsley63c85232009-09-03 20:14:03 +03002561 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06002562 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03002563 if (ret)
2564 break;
2565 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002566
2567 return ret;
2568}
2569
Paul Walmsley63c85232009-09-03 20:14:03 +03002570/**
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002571 * omap_hwmod_register_links - register an array of hwmod links
2572 * @ois: pointer to an array of omap_hwmod_ocp_if to register
2573 *
2574 * Intended to be called early in boot before the clock framework is
2575 * initialized. If @ois is not null, will register all omap_hwmods
2576 * listed in @ois that are valid for this chip. Returns 0.
2577 */
2578int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
2579{
2580 int r, i;
2581
2582 if (!ois)
2583 return 0;
2584
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002585 if (!linkspace) {
2586 if (_alloc_linkspace(ois)) {
2587 pr_err("omap_hwmod: could not allocate link space\n");
2588 return -ENOMEM;
2589 }
2590 }
2591
2592 i = 0;
2593 do {
2594 r = _register_link(ois[i]);
2595 WARN(r && r != -EEXIST,
2596 "omap_hwmod: _register_link(%s -> %s) returned %d\n",
2597 ois[i]->master->name, ois[i]->slave->name, r);
2598 } while (ois[++i]);
2599
2600 return 0;
2601}
2602
2603/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002604 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
2605 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002606 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002607 * If the hwmod data corresponding to the MPU subsystem IP block
2608 * hasn't been initialized and set up yet, do so now. This must be
2609 * done first since sleep dependencies may be added from other hwmods
2610 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
2611 * return value.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002612 */
Paul Walmsley381d0332012-04-19 00:58:22 -06002613static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002614{
Paul Walmsley381d0332012-04-19 00:58:22 -06002615 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
2616 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
2617 __func__, MPU_INITIATOR_NAME);
2618 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
2619 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
Paul Walmsley63c85232009-09-03 20:14:03 +03002620}
2621
2622/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002623 * omap_hwmod_setup_one - set up a single hwmod
2624 * @oh_name: const char * name of the already-registered hwmod to set up
2625 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002626 * Initialize and set up a single hwmod. Intended to be used for a
2627 * small number of early devices, such as the timer IP blocks used for
2628 * the scheduler clock. Must be called after omap2_clk_init().
2629 * Resolves the struct clk names to struct clk pointers for each
2630 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
2631 * -EINVAL upon error or 0 upon success.
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002632 */
2633int __init omap_hwmod_setup_one(const char *oh_name)
2634{
2635 struct omap_hwmod *oh;
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002636
2637 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
2638
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002639 oh = _lookup(oh_name);
2640 if (!oh) {
2641 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
2642 return -EINVAL;
2643 }
2644
Paul Walmsley381d0332012-04-19 00:58:22 -06002645 _ensure_mpu_hwmod_is_setup(oh);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002646
Paul Walmsley381d0332012-04-19 00:58:22 -06002647 _init(oh, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002648 _setup(oh, NULL);
2649
2650 return 0;
2651}
2652
2653/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002654 * omap_hwmod_setup_all - set up all registered IP blocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002655 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002656 * Initialize and set up all IP blocks registered with the hwmod code.
2657 * Must be called after omap2_clk_init(). Resolves the struct clk
2658 * names to struct clk pointers for each registered omap_hwmod. Also
2659 * calls _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002660 */
Paul Walmsley550c8092011-02-28 11:58:14 -07002661static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03002662{
Paul Walmsley381d0332012-04-19 00:58:22 -06002663 _ensure_mpu_hwmod_is_setup(NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002664
Paul Walmsley381d0332012-04-19 00:58:22 -06002665 omap_hwmod_for_each(_init, NULL);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002666 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002667
2668 return 0;
2669}
Paul Walmsley550c8092011-02-28 11:58:14 -07002670core_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03002671
2672/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002673 * omap_hwmod_enable - enable an omap_hwmod
2674 * @oh: struct omap_hwmod *
2675 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002676 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03002677 * Returns -EINVAL on error or passes along the return value from _enable().
2678 */
2679int omap_hwmod_enable(struct omap_hwmod *oh)
2680{
2681 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002682 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002683
2684 if (!oh)
2685 return -EINVAL;
2686
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002687 spin_lock_irqsave(&oh->_lock, flags);
2688 r = _enable(oh);
2689 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002690
2691 return r;
2692}
2693
2694/**
2695 * omap_hwmod_idle - idle an omap_hwmod
2696 * @oh: struct omap_hwmod *
2697 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002698 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03002699 * Returns -EINVAL on error or passes along the return value from _idle().
2700 */
2701int omap_hwmod_idle(struct omap_hwmod *oh)
2702{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002703 unsigned long flags;
2704
Paul Walmsley63c85232009-09-03 20:14:03 +03002705 if (!oh)
2706 return -EINVAL;
2707
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002708 spin_lock_irqsave(&oh->_lock, flags);
2709 _idle(oh);
2710 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002711
2712 return 0;
2713}
2714
2715/**
2716 * omap_hwmod_shutdown - shutdown an omap_hwmod
2717 * @oh: struct omap_hwmod *
2718 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002719 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03002720 * omap_device_shutdown(). Returns -EINVAL on error or passes along
2721 * the return value from _shutdown().
2722 */
2723int omap_hwmod_shutdown(struct omap_hwmod *oh)
2724{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002725 unsigned long flags;
2726
Paul Walmsley63c85232009-09-03 20:14:03 +03002727 if (!oh)
2728 return -EINVAL;
2729
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002730 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002731 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002732 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002733
2734 return 0;
2735}
2736
2737/**
2738 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
2739 * @oh: struct omap_hwmod *oh
2740 *
2741 * Intended to be called by the omap_device code.
2742 */
2743int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
2744{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002745 unsigned long flags;
2746
2747 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002748 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002749 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002750
2751 return 0;
2752}
2753
2754/**
2755 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
2756 * @oh: struct omap_hwmod *oh
2757 *
2758 * Intended to be called by the omap_device code.
2759 */
2760int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
2761{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002762 unsigned long flags;
2763
2764 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002765 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002766 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002767
2768 return 0;
2769}
2770
2771/**
2772 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
2773 * @oh: struct omap_hwmod *oh
2774 *
2775 * Intended to be called by drivers and core code when all posted
2776 * writes to a device must complete before continuing further
2777 * execution (for example, after clearing some device IRQSTATUS
2778 * register bits)
2779 *
2780 * XXX what about targets with multiple OCP threads?
2781 */
2782void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
2783{
2784 BUG_ON(!oh);
2785
Paul Walmsley43b40992010-02-22 22:09:34 -07002786 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Russell King4f8a4282012-02-07 10:59:37 +00002787 WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
2788 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002789 return;
2790 }
2791
2792 /*
2793 * Forces posted writes to complete on the OCP thread handling
2794 * register writes
2795 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002796 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002797}
2798
2799/**
2800 * omap_hwmod_reset - reset the hwmod
2801 * @oh: struct omap_hwmod *
2802 *
2803 * Under some conditions, a driver may wish to reset the entire device.
2804 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06002805 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03002806 */
2807int omap_hwmod_reset(struct omap_hwmod *oh)
2808{
2809 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002810 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002811
Liam Girdwood9b579112010-09-21 10:34:09 -06002812 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002813 return -EINVAL;
2814
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002815 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002816 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002817 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002818
2819 return r;
2820}
2821
Paul Walmsley5e8370f2012-04-18 19:10:06 -06002822/*
2823 * IP block data retrieval functions
2824 */
2825
Paul Walmsley63c85232009-09-03 20:14:03 +03002826/**
2827 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
2828 * @oh: struct omap_hwmod *
2829 * @res: pointer to the first element of an array of struct resource to fill
2830 *
2831 * Count the number of struct resource array elements necessary to
2832 * contain omap_hwmod @oh resources. Intended to be called by code
2833 * that registers omap_devices. Intended to be used to determine the
2834 * size of a dynamically-allocated struct resource array, before
2835 * calling omap_hwmod_fill_resources(). Returns the number of struct
2836 * resource array elements needed.
2837 *
2838 * XXX This code is not optimized. It could attempt to merge adjacent
2839 * resource IDs.
2840 *
2841 */
2842int omap_hwmod_count_resources(struct omap_hwmod *oh)
2843{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002844 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002845 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002846 int ret;
2847 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002848
Paul Walmsleybc614952011-07-09 19:14:07 -06002849 ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002850
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002851 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002852
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002853 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002854 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002855 ret += _count_ocp_if_addr_spaces(os);
2856 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002857
2858 return ret;
2859}
2860
2861/**
2862 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
2863 * @oh: struct omap_hwmod *
2864 * @res: pointer to the first element of an array of struct resource to fill
2865 *
2866 * Fill the struct resource array @res with resource data from the
2867 * omap_hwmod @oh. Intended to be called by code that registers
2868 * omap_devices. See also omap_hwmod_count_resources(). Returns the
2869 * number of array elements filled.
2870 */
2871int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
2872{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002873 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002874 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002875 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03002876 int r = 0;
2877
2878 /* For each IRQ, DMA, memory area, fill in array.*/
2879
Paul Walmsley212738a2011-07-09 19:14:06 -06002880 mpu_irqs_cnt = _count_mpu_irqs(oh);
2881 for (i = 0; i < mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07002882 (res + r)->name = (oh->mpu_irqs + i)->name;
2883 (res + r)->start = (oh->mpu_irqs + i)->irq;
2884 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03002885 (res + r)->flags = IORESOURCE_IRQ;
2886 r++;
2887 }
2888
Paul Walmsleybc614952011-07-09 19:14:07 -06002889 sdma_reqs_cnt = _count_sdma_reqs(oh);
2890 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06002891 (res + r)->name = (oh->sdma_reqs + i)->name;
2892 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
2893 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03002894 (res + r)->flags = IORESOURCE_DMA;
2895 r++;
2896 }
2897
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002898 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002899
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002900 i = 0;
2901 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002902 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley78183f32011-07-09 19:14:05 -06002903 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03002904
Paul Walmsley78183f32011-07-09 19:14:05 -06002905 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08002906 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03002907 (res + r)->start = (os->addr + j)->pa_start;
2908 (res + r)->end = (os->addr + j)->pa_end;
2909 (res + r)->flags = IORESOURCE_MEM;
2910 r++;
2911 }
2912 }
2913
2914 return r;
2915}
2916
2917/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06002918 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
2919 * @oh: struct omap_hwmod * to operate on
2920 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
2921 * @name: pointer to the name of the data to fetch (optional)
2922 * @rsrc: pointer to a struct resource, allocated by the caller
2923 *
2924 * Retrieve MPU IRQ, SDMA request line, or address space start/end
2925 * data for the IP block pointed to by @oh. The data will be filled
2926 * into a struct resource record pointed to by @rsrc. The struct
2927 * resource must be allocated by the caller. When @name is non-null,
2928 * the data associated with the matching entry in the IRQ/SDMA/address
2929 * space hwmod data arrays will be returned. If @name is null, the
2930 * first array entry will be returned. Data order is not meaningful
2931 * in hwmod data, so callers are strongly encouraged to use a non-null
2932 * @name whenever possible to avoid unpredictable effects if hwmod
2933 * data is later added that causes data ordering to change. This
2934 * function is only intended for use by OMAP core code. Device
2935 * drivers should not call this function - the appropriate bus-related
2936 * data accessor functions should be used instead. Returns 0 upon
2937 * success or a negative error code upon error.
2938 */
2939int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
2940 const char *name, struct resource *rsrc)
2941{
2942 int r;
2943 unsigned int irq, dma;
2944 u32 pa_start, pa_end;
2945
2946 if (!oh || !rsrc)
2947 return -EINVAL;
2948
2949 if (type == IORESOURCE_IRQ) {
2950 r = _get_mpu_irq_by_name(oh, name, &irq);
2951 if (r)
2952 return r;
2953
2954 rsrc->start = irq;
2955 rsrc->end = irq;
2956 } else if (type == IORESOURCE_DMA) {
2957 r = _get_sdma_req_by_name(oh, name, &dma);
2958 if (r)
2959 return r;
2960
2961 rsrc->start = dma;
2962 rsrc->end = dma;
2963 } else if (type == IORESOURCE_MEM) {
2964 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
2965 if (r)
2966 return r;
2967
2968 rsrc->start = pa_start;
2969 rsrc->end = pa_end;
2970 } else {
2971 return -EINVAL;
2972 }
2973
2974 rsrc->flags = type;
2975 rsrc->name = name;
2976
2977 return 0;
2978}
2979
2980/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002981 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
2982 * @oh: struct omap_hwmod *
2983 *
2984 * Return the powerdomain pointer associated with the OMAP module
2985 * @oh's main clock. If @oh does not have a main clk, return the
2986 * powerdomain associated with the interface clock associated with the
2987 * module's MPU port. (XXX Perhaps this should use the SDMA port
2988 * instead?) Returns NULL on error, or a struct powerdomain * on
2989 * success.
2990 */
2991struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
2992{
2993 struct clk *c;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06002994 struct omap_hwmod_ocp_if *oi;
Paul Walmsley63c85232009-09-03 20:14:03 +03002995
2996 if (!oh)
2997 return NULL;
2998
2999 if (oh->_clk) {
3000 c = oh->_clk;
3001 } else {
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003002 oi = _find_mpu_rt_port(oh);
3003 if (!oi)
Paul Walmsley63c85232009-09-03 20:14:03 +03003004 return NULL;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003005 c = oi->_clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03003006 }
3007
Thara Gopinathd5647c12010-03-31 04:16:29 -06003008 if (!c->clkdm)
3009 return NULL;
3010
Paul Walmsley63c85232009-09-03 20:14:03 +03003011 return c->clkdm->pwrdm.ptr;
3012
3013}
3014
3015/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06003016 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3017 * @oh: struct omap_hwmod *
3018 *
3019 * Returns the virtual address corresponding to the beginning of the
3020 * module's register target, in the address range that is intended to
3021 * be used by the MPU. Returns the virtual address upon success or NULL
3022 * upon error.
3023 */
3024void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3025{
3026 if (!oh)
3027 return NULL;
3028
3029 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3030 return NULL;
3031
3032 if (oh->_state == _HWMOD_STATE_UNKNOWN)
3033 return NULL;
3034
3035 return oh->_mpu_rt_va;
3036}
3037
3038/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003039 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
3040 * @oh: struct omap_hwmod *
3041 * @init_oh: struct omap_hwmod * (initiator)
3042 *
3043 * Add a sleep dependency between the initiator @init_oh and @oh.
3044 * Intended to be called by DSP/Bridge code via platform_data for the
3045 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3046 * code needs to add/del initiator dependencies dynamically
3047 * before/after accessing a device. Returns the return value from
3048 * _add_initiator_dep().
3049 *
3050 * XXX Keep a usecount in the clockdomain code
3051 */
3052int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
3053 struct omap_hwmod *init_oh)
3054{
3055 return _add_initiator_dep(oh, init_oh);
3056}
3057
3058/*
3059 * XXX what about functions for drivers to save/restore ocp_sysconfig
3060 * for context save/restore operations?
3061 */
3062
3063/**
3064 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
3065 * @oh: struct omap_hwmod *
3066 * @init_oh: struct omap_hwmod * (initiator)
3067 *
3068 * Remove a sleep dependency between the initiator @init_oh and @oh.
3069 * Intended to be called by DSP/Bridge code via platform_data for the
3070 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3071 * code needs to add/del initiator dependencies dynamically
3072 * before/after accessing a device. Returns the return value from
3073 * _del_initiator_dep().
3074 *
3075 * XXX Keep a usecount in the clockdomain code
3076 */
3077int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
3078 struct omap_hwmod *init_oh)
3079{
3080 return _del_initiator_dep(oh, init_oh);
3081}
3082
3083/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003084 * omap_hwmod_enable_wakeup - allow device to wake up the system
3085 * @oh: struct omap_hwmod *
3086 *
3087 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
Govindraj.R2a1cc142012-04-05 02:59:32 -06003088 * send wakeups to the PRCM, and enable I/O ring wakeup events for
3089 * this IP block if it has dynamic mux entries. Eventually this
3090 * should set PRCM wakeup registers to cause the PRCM to receive
3091 * wakeup events from the module. Does not set any wakeup routing
3092 * registers beyond this point - if the module is to wake up any other
3093 * module or subsystem, that must be set separately. Called by
3094 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003095 */
3096int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3097{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003098 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003099 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003100
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003101 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003102
3103 if (oh->class->sysc &&
3104 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3105 v = oh->_sysc_cache;
3106 _enable_wakeup(oh, &v);
3107 _write_sysconfig(v, oh);
3108 }
3109
Govindraj Receec002011-12-16 14:36:58 -07003110 _set_idle_ioring_wakeup(oh, true);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003111 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003112
3113 return 0;
3114}
3115
3116/**
3117 * omap_hwmod_disable_wakeup - prevent device from waking the system
3118 * @oh: struct omap_hwmod *
3119 *
3120 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
Govindraj.R2a1cc142012-04-05 02:59:32 -06003121 * from sending wakeups to the PRCM, and disable I/O ring wakeup
3122 * events for this IP block if it has dynamic mux entries. Eventually
3123 * this should clear PRCM wakeup registers to cause the PRCM to ignore
3124 * wakeup events from the module. Does not set any wakeup routing
3125 * registers beyond this point - if the module is to wake up any other
3126 * module or subsystem, that must be set separately. Called by
3127 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003128 */
3129int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3130{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003131 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003132 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003133
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003134 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003135
3136 if (oh->class->sysc &&
3137 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3138 v = oh->_sysc_cache;
3139 _disable_wakeup(oh, &v);
3140 _write_sysconfig(v, oh);
3141 }
3142
Govindraj Receec002011-12-16 14:36:58 -07003143 _set_idle_ioring_wakeup(oh, false);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003144 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003145
3146 return 0;
3147}
Paul Walmsley43b40992010-02-22 22:09:34 -07003148
3149/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003150 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3151 * contained in the hwmod module.
3152 * @oh: struct omap_hwmod *
3153 * @name: name of the reset line to lookup and assert
3154 *
3155 * Some IP like dsp, ipu or iva contain processor that require
3156 * an HW reset line to be assert / deassert in order to enable fully
3157 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3158 * yet supported on this OMAP; otherwise, passes along the return value
3159 * from _assert_hardreset().
3160 */
3161int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3162{
3163 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003164 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003165
3166 if (!oh)
3167 return -EINVAL;
3168
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003169 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003170 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003171 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003172
3173 return ret;
3174}
3175
3176/**
3177 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3178 * contained in the hwmod module.
3179 * @oh: struct omap_hwmod *
3180 * @name: name of the reset line to look up and deassert
3181 *
3182 * Some IP like dsp, ipu or iva contain processor that require
3183 * an HW reset line to be assert / deassert in order to enable fully
3184 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3185 * yet supported on this OMAP; otherwise, passes along the return value
3186 * from _deassert_hardreset().
3187 */
3188int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3189{
3190 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003191 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003192
3193 if (!oh)
3194 return -EINVAL;
3195
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003196 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003197 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003198 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003199
3200 return ret;
3201}
3202
3203/**
3204 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
3205 * contained in the hwmod module
3206 * @oh: struct omap_hwmod *
3207 * @name: name of the reset line to look up and read
3208 *
3209 * Return the current state of the hwmod @oh's reset line named @name:
3210 * returns -EINVAL upon parameter error or if this operation
3211 * is unsupported on the current OMAP; otherwise, passes along the return
3212 * value from _read_hardreset().
3213 */
3214int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
3215{
3216 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003217 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003218
3219 if (!oh)
3220 return -EINVAL;
3221
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003222 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003223 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003224 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003225
3226 return ret;
3227}
3228
3229
3230/**
Paul Walmsley43b40992010-02-22 22:09:34 -07003231 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3232 * @classname: struct omap_hwmod_class name to search for
3233 * @fn: callback function pointer to call for each hwmod in class @classname
3234 * @user: arbitrary context data to pass to the callback function
3235 *
Benoit Coussonce35b242010-12-21 21:31:28 -07003236 * For each omap_hwmod of class @classname, call @fn.
3237 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07003238 * zero, the iterator is terminated, and the callback function's return
3239 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3240 * if @classname or @fn are NULL, or passes back the error code from @fn.
3241 */
3242int omap_hwmod_for_each_by_class(const char *classname,
3243 int (*fn)(struct omap_hwmod *oh,
3244 void *user),
3245 void *user)
3246{
3247 struct omap_hwmod *temp_oh;
3248 int ret = 0;
3249
3250 if (!classname || !fn)
3251 return -EINVAL;
3252
3253 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3254 __func__, classname);
3255
Paul Walmsley43b40992010-02-22 22:09:34 -07003256 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3257 if (!strcmp(temp_oh->class->name, classname)) {
3258 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3259 __func__, temp_oh->name);
3260 ret = (*fn)(temp_oh, user);
3261 if (ret)
3262 break;
3263 }
3264 }
3265
Paul Walmsley43b40992010-02-22 22:09:34 -07003266 if (ret)
3267 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3268 __func__, ret);
3269
3270 return ret;
3271}
3272
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003273/**
3274 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3275 * @oh: struct omap_hwmod *
3276 * @state: state that _setup() should leave the hwmod in
3277 *
Paul Walmsley550c8092011-02-28 11:58:14 -07003278 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsley64813c32012-04-18 19:10:03 -06003279 * (called by omap_hwmod_setup_*()). See also the documentation
3280 * for _setup_postsetup(), above. Returns 0 upon success or
3281 * -EINVAL if there is a problem with the arguments or if the hwmod is
3282 * in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003283 */
3284int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3285{
3286 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003287 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003288
3289 if (!oh)
3290 return -EINVAL;
3291
3292 if (state != _HWMOD_STATE_DISABLED &&
3293 state != _HWMOD_STATE_ENABLED &&
3294 state != _HWMOD_STATE_IDLE)
3295 return -EINVAL;
3296
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003297 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003298
3299 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3300 ret = -EINVAL;
3301 goto ohsps_unlock;
3302 }
3303
3304 oh->_postsetup_state = state;
3305 ret = 0;
3306
3307ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003308 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003309
3310 return ret;
3311}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003312
3313/**
3314 * omap_hwmod_get_context_loss_count - get lost context count
3315 * @oh: struct omap_hwmod *
3316 *
3317 * Query the powerdomain of of @oh to get the context loss
3318 * count for this device.
3319 *
3320 * Returns the context loss count of the powerdomain assocated with @oh
3321 * upon success, or zero if no powerdomain exists for @oh.
3322 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03003323int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003324{
3325 struct powerdomain *pwrdm;
3326 int ret = 0;
3327
3328 pwrdm = omap_hwmod_get_pwrdm(oh);
3329 if (pwrdm)
3330 ret = pwrdm_get_context_loss_count(pwrdm);
3331
3332 return ret;
3333}
Paul Walmsley43b01642011-03-10 03:50:07 -07003334
3335/**
3336 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
3337 * @oh: struct omap_hwmod *
3338 *
3339 * Prevent the hwmod @oh from being reset during the setup process.
3340 * Intended for use by board-*.c files on boards with devices that
3341 * cannot tolerate being reset. Must be called before the hwmod has
3342 * been set up. Returns 0 upon success or negative error code upon
3343 * failure.
3344 */
3345int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
3346{
3347 if (!oh)
3348 return -EINVAL;
3349
3350 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3351 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
3352 oh->name);
3353 return -EINVAL;
3354 }
3355
3356 oh->flags |= HWMOD_INIT_NO_RESET;
3357
3358 return 0;
3359}
Tero Kristoabc2d542011-12-16 14:36:59 -07003360
3361/**
3362 * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
3363 * @oh: struct omap_hwmod * containing hwmod mux entries
3364 * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
3365 * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
3366 *
3367 * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
3368 * entry number @pad_idx for the hwmod @oh, trigger the interrupt
3369 * service routine for the hwmod's mpu_irqs array index @irq_idx. If
3370 * this function is not called for a given pad_idx, then the ISR
3371 * associated with @oh's first MPU IRQ will be triggered when an I/O
3372 * pad wakeup occurs on that pad. Note that @pad_idx is the index of
3373 * the _dynamic or wakeup_ entry: if there are other entries not
3374 * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
3375 * entries are NOT COUNTED in the dynamic pad index. This function
3376 * must be called separately for each pad that requires its interrupt
3377 * to be re-routed this way. Returns -EINVAL if there is an argument
3378 * problem or if @oh does not have hwmod mux entries or MPU IRQs;
3379 * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
3380 *
3381 * XXX This function interface is fragile. Rather than using array
3382 * indexes, which are subject to unpredictable change, it should be
3383 * using hwmod IRQ names, and some other stable key for the hwmod mux
3384 * pad records.
3385 */
3386int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
3387{
3388 int nr_irqs;
3389
3390 might_sleep();
3391
3392 if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
3393 pad_idx >= oh->mux->nr_pads_dynamic)
3394 return -EINVAL;
3395
3396 /* Check the number of available mpu_irqs */
3397 for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
3398 ;
3399
3400 if (irq_idx >= nr_irqs)
3401 return -EINVAL;
3402
3403 if (!oh->mux->irqs) {
3404 /* XXX What frees this? */
3405 oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
3406 GFP_KERNEL);
3407 if (!oh->mux->irqs)
3408 return -ENOMEM;
3409 }
3410 oh->mux->irqs[pad_idx] = irq_idx;
3411
3412 return 0;
3413}