blob: acc616c02e14eb49f77dccd0d4fcf1de321b7a3b [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
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600169/**
170 * struct omap_hwmod_soc_ops - fn ptrs for some SoC-specific operations
171 * @enable_module: function to enable a module (via MODULEMODE)
172 * @disable_module: function to disable a module (via MODULEMODE)
173 *
174 * XXX Eventually this functionality will be hidden inside the PRM/CM
175 * device drivers. Until then, this should avoid huge blocks of cpu_is_*()
176 * conditionals in this code.
177 */
178struct omap_hwmod_soc_ops {
179 void (*enable_module)(struct omap_hwmod *oh);
180 int (*disable_module)(struct omap_hwmod *oh);
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -0600181 int (*wait_target_ready)(struct omap_hwmod *oh);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -0600182 int (*assert_hardreset)(struct omap_hwmod *oh,
183 struct omap_hwmod_rst_info *ohri);
184 int (*deassert_hardreset)(struct omap_hwmod *oh,
185 struct omap_hwmod_rst_info *ohri);
186 int (*is_hardreset_asserted)(struct omap_hwmod *oh,
187 struct omap_hwmod_rst_info *ohri);
Kevin Hilman0a179ea2012-06-18 12:12:25 -0600188 int (*init_clkdm)(struct omap_hwmod *oh);
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600189};
190
191/* soc_ops: adapts the omap_hwmod code to the currently-booted SoC */
192static struct omap_hwmod_soc_ops soc_ops;
193
Paul Walmsley63c85232009-09-03 20:14:03 +0300194/* omap_hwmod_list contains all registered struct omap_hwmods */
195static LIST_HEAD(omap_hwmod_list);
196
Paul Walmsley63c85232009-09-03 20:14:03 +0300197/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
198static struct omap_hwmod *mpu_oh;
199
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600200/*
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600201 * linkspace: ptr to a buffer that struct omap_hwmod_link records are
202 * allocated from - used to reduce the number of small memory
203 * allocations, which has a significant impact on performance
204 */
205static struct omap_hwmod_link *linkspace;
206
207/*
208 * free_ls, max_ls: array indexes into linkspace; representing the
209 * next free struct omap_hwmod_link index, and the maximum number of
210 * struct omap_hwmod_link records allocated (respectively)
211 */
212static unsigned short free_ls, max_ls, ls_supp;
Paul Walmsley63c85232009-09-03 20:14:03 +0300213
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600214/* inited: set to true once the hwmod code is initialized */
215static bool inited;
216
Paul Walmsley63c85232009-09-03 20:14:03 +0300217/* Private functions */
218
219/**
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600220 * _fetch_next_ocp_if - return the next OCP interface in a list
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600221 * @p: ptr to a ptr to the list_head inside the ocp_if to return
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600222 * @i: pointer to the index of the element pointed to by @p in the list
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600223 *
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600224 * Return a pointer to the struct omap_hwmod_ocp_if record
225 * containing the struct list_head pointed to by @p, and increment
226 * @p such that a future call to this routine will return the next
227 * record.
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600228 */
229static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600230 int *i)
231{
232 struct omap_hwmod_ocp_if *oi;
233
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600234 oi = list_entry(*p, struct omap_hwmod_link, node)->ocp_if;
235 *p = (*p)->next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600236
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600237 *i = *i + 1;
238
239 return oi;
240}
241
242/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300243 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
244 * @oh: struct omap_hwmod *
245 *
246 * Load the current value of the hwmod OCP_SYSCONFIG register into the
247 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
248 * OCP_SYSCONFIG register or 0 upon success.
249 */
250static int _update_sysc_cache(struct omap_hwmod *oh)
251{
Paul Walmsley43b40992010-02-22 22:09:34 -0700252 if (!oh->class->sysc) {
253 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 +0300254 return -EINVAL;
255 }
256
257 /* XXX ensure module interface clock is up */
258
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -0700259 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300260
Paul Walmsley43b40992010-02-22 22:09:34 -0700261 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
Thara Gopinath883edfd2010-01-19 17:30:51 -0700262 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300263
264 return 0;
265}
266
267/**
268 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
269 * @v: OCP_SYSCONFIG value to write
270 * @oh: struct omap_hwmod *
271 *
Paul Walmsley43b40992010-02-22 22:09:34 -0700272 * Write @v into the module class' OCP_SYSCONFIG register, if it has
273 * one. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +0300274 */
275static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
276{
Paul Walmsley43b40992010-02-22 22:09:34 -0700277 if (!oh->class->sysc) {
278 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 +0300279 return;
280 }
281
282 /* XXX ensure module interface clock is up */
283
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700284 /* Module might have lost context, always update cache and register */
285 oh->_sysc_cache = v;
286 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300287}
288
289/**
290 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
291 * @oh: struct omap_hwmod *
292 * @standbymode: MIDLEMODE field bits
293 * @v: pointer to register contents to modify
294 *
295 * Update the master standby mode bits in @v to be @standbymode for
296 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
297 * upon error or 0 upon success.
298 */
299static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
300 u32 *v)
301{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700302 u32 mstandby_mask;
303 u8 mstandby_shift;
304
Paul Walmsley43b40992010-02-22 22:09:34 -0700305 if (!oh->class->sysc ||
306 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300307 return -EINVAL;
308
Paul Walmsley43b40992010-02-22 22:09:34 -0700309 if (!oh->class->sysc->sysc_fields) {
310 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700311 return -EINVAL;
312 }
313
Paul Walmsley43b40992010-02-22 22:09:34 -0700314 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700315 mstandby_mask = (0x3 << mstandby_shift);
316
317 *v &= ~mstandby_mask;
318 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300319
320 return 0;
321}
322
323/**
324 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
325 * @oh: struct omap_hwmod *
326 * @idlemode: SIDLEMODE field bits
327 * @v: pointer to register contents to modify
328 *
329 * Update the slave idle mode bits in @v to be @idlemode for the @oh
330 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
331 * or 0 upon success.
332 */
333static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
334{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700335 u32 sidle_mask;
336 u8 sidle_shift;
337
Paul Walmsley43b40992010-02-22 22:09:34 -0700338 if (!oh->class->sysc ||
339 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300340 return -EINVAL;
341
Paul Walmsley43b40992010-02-22 22:09:34 -0700342 if (!oh->class->sysc->sysc_fields) {
343 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700344 return -EINVAL;
345 }
346
Paul Walmsley43b40992010-02-22 22:09:34 -0700347 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700348 sidle_mask = (0x3 << sidle_shift);
349
350 *v &= ~sidle_mask;
351 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300352
353 return 0;
354}
355
356/**
357 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
358 * @oh: struct omap_hwmod *
359 * @clockact: CLOCKACTIVITY field bits
360 * @v: pointer to register contents to modify
361 *
362 * Update the clockactivity mode bits in @v to be @clockact for the
363 * @oh hwmod. Used for additional powersaving on some modules. Does
364 * not write to the hardware. Returns -EINVAL upon error or 0 upon
365 * success.
366 */
367static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
368{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700369 u32 clkact_mask;
370 u8 clkact_shift;
371
Paul Walmsley43b40992010-02-22 22:09:34 -0700372 if (!oh->class->sysc ||
373 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300374 return -EINVAL;
375
Paul Walmsley43b40992010-02-22 22:09:34 -0700376 if (!oh->class->sysc->sysc_fields) {
377 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700378 return -EINVAL;
379 }
380
Paul Walmsley43b40992010-02-22 22:09:34 -0700381 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700382 clkact_mask = (0x3 << clkact_shift);
383
384 *v &= ~clkact_mask;
385 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300386
387 return 0;
388}
389
390/**
391 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
392 * @oh: struct omap_hwmod *
393 * @v: pointer to register contents to modify
394 *
395 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
396 * error or 0 upon success.
397 */
398static int _set_softreset(struct omap_hwmod *oh, u32 *v)
399{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700400 u32 softrst_mask;
401
Paul Walmsley43b40992010-02-22 22:09:34 -0700402 if (!oh->class->sysc ||
403 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300404 return -EINVAL;
405
Paul Walmsley43b40992010-02-22 22:09:34 -0700406 if (!oh->class->sysc->sysc_fields) {
407 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700408 return -EINVAL;
409 }
410
Paul Walmsley43b40992010-02-22 22:09:34 -0700411 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700412
413 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300414
415 return 0;
416}
417
418/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700419 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
420 * @oh: struct omap_hwmod *
421 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
422 * @v: pointer to register contents to modify
423 *
424 * Update the module autoidle bit in @v to be @autoidle for the @oh
425 * hwmod. The autoidle bit controls whether the module can gate
426 * internal clocks automatically when it isn't doing anything; the
427 * exact function of this bit varies on a per-module basis. This
428 * function does not write to the hardware. Returns -EINVAL upon
429 * error or 0 upon success.
430 */
431static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
432 u32 *v)
433{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700434 u32 autoidle_mask;
435 u8 autoidle_shift;
436
Paul Walmsley43b40992010-02-22 22:09:34 -0700437 if (!oh->class->sysc ||
438 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700439 return -EINVAL;
440
Paul Walmsley43b40992010-02-22 22:09:34 -0700441 if (!oh->class->sysc->sysc_fields) {
442 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700443 return -EINVAL;
444 }
445
Paul Walmsley43b40992010-02-22 22:09:34 -0700446 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700447 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700448
449 *v &= ~autoidle_mask;
450 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700451
452 return 0;
453}
454
455/**
Govindraj Receec002011-12-16 14:36:58 -0700456 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
457 * @oh: struct omap_hwmod *
458 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
459 *
460 * Set or clear the I/O pad wakeup flag in the mux entries for the
461 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
462 * in memory. If the hwmod is currently idled, and the new idle
463 * values don't match the previous ones, this function will also
464 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
465 * currently idled, this function won't touch the hardware: the new
466 * mux settings are written to the SCM PADCTRL registers when the
467 * hwmod is idled. No return value.
468 */
469static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
470{
471 struct omap_device_pad *pad;
472 bool change = false;
473 u16 prev_idle;
474 int j;
475
476 if (!oh->mux || !oh->mux->enabled)
477 return;
478
479 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
480 pad = oh->mux->pads_dynamic[j];
481
482 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
483 continue;
484
485 prev_idle = pad->idle;
486
487 if (set_wake)
488 pad->idle |= OMAP_WAKEUP_EN;
489 else
490 pad->idle &= ~OMAP_WAKEUP_EN;
491
492 if (prev_idle != pad->idle)
493 change = true;
494 }
495
496 if (change && oh->_state == _HWMOD_STATE_IDLE)
497 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
498}
499
500/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300501 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
502 * @oh: struct omap_hwmod *
503 *
504 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
505 * upon error or 0 upon success.
506 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700507static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300508{
Paul Walmsley43b40992010-02-22 22:09:34 -0700509 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700510 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200511 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
512 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300513 return -EINVAL;
514
Paul Walmsley43b40992010-02-22 22:09:34 -0700515 if (!oh->class->sysc->sysc_fields) {
516 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700517 return -EINVAL;
518 }
519
Benoit Cousson1fe74112011-07-01 22:54:03 +0200520 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
521 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300522
Benoit Cousson86009eb2010-12-21 21:31:28 -0700523 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
524 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200525 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
526 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700527
Paul Walmsley63c85232009-09-03 20:14:03 +0300528 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
529
530 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
531
532 return 0;
533}
534
535/**
536 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
537 * @oh: struct omap_hwmod *
538 *
539 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
540 * upon error or 0 upon success.
541 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700542static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300543{
Paul Walmsley43b40992010-02-22 22:09:34 -0700544 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700545 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200546 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
547 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300548 return -EINVAL;
549
Paul Walmsley43b40992010-02-22 22:09:34 -0700550 if (!oh->class->sysc->sysc_fields) {
551 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700552 return -EINVAL;
553 }
554
Benoit Cousson1fe74112011-07-01 22:54:03 +0200555 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
556 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300557
Benoit Cousson86009eb2010-12-21 21:31:28 -0700558 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
559 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200560 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
561 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700562
Paul Walmsley63c85232009-09-03 20:14:03 +0300563 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
564
565 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
566
567 return 0;
568}
569
570/**
571 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
572 * @oh: struct omap_hwmod *
573 *
574 * Prevent the hardware module @oh from entering idle while the
575 * hardare module initiator @init_oh is active. Useful when a module
576 * will be accessed by a particular initiator (e.g., if a module will
577 * be accessed by the IVA, there should be a sleepdep between the IVA
578 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700579 * mode. If the clockdomain is marked as not needing autodeps, return
580 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
581 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300582 */
583static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
584{
585 if (!oh->_clk)
586 return -EINVAL;
587
Paul Walmsley570b54c2011-03-10 03:50:09 -0700588 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
589 return 0;
590
Paul Walmsley55ed9692010-01-26 20:12:59 -0700591 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300592}
593
594/**
595 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
596 * @oh: struct omap_hwmod *
597 *
598 * Allow the hardware module @oh to enter idle while the hardare
599 * module initiator @init_oh is active. Useful when a module will not
600 * be accessed by a particular initiator (e.g., if a module will not
601 * be accessed by the IVA, there should be no sleepdep between the IVA
602 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700603 * mode. If the clockdomain is marked as not needing autodeps, return
604 * 0 without doing anything. Returns -EINVAL upon error or passes
605 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300606 */
607static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
608{
609 if (!oh->_clk)
610 return -EINVAL;
611
Paul Walmsley570b54c2011-03-10 03:50:09 -0700612 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
613 return 0;
614
Paul Walmsley55ed9692010-01-26 20:12:59 -0700615 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300616}
617
618/**
619 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
620 * @oh: struct omap_hwmod *
621 *
622 * Called from _init_clocks(). Populates the @oh _clk (main
623 * functional clock pointer) if a main_clk is present. Returns 0 on
624 * success or -EINVAL on error.
625 */
626static int _init_main_clk(struct omap_hwmod *oh)
627{
Paul Walmsley63c85232009-09-03 20:14:03 +0300628 int ret = 0;
629
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700630 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300631 return 0;
632
Benoit Cousson63403382010-05-20 12:31:10 -0600633 oh->_clk = omap_clk_get_by_name(oh->main_clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600634 if (!oh->_clk) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600635 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
636 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600637 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600638 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300639
Benoit Cousson63403382010-05-20 12:31:10 -0600640 if (!oh->_clk->clkdm)
641 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
642 oh->main_clk, oh->_clk->name);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700643
Paul Walmsley63c85232009-09-03 20:14:03 +0300644 return ret;
645}
646
647/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600648 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300649 * @oh: struct omap_hwmod *
650 *
651 * Called from _init_clocks(). Populates the @oh OCP slave interface
652 * clock pointers. Returns 0 on success or -EINVAL on error.
653 */
654static int _init_interface_clks(struct omap_hwmod *oh)
655{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600656 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600657 struct list_head *p;
Paul Walmsley63c85232009-09-03 20:14:03 +0300658 struct clk *c;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600659 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300660 int ret = 0;
661
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600662 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600663
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600664 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600665 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700666 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300667 continue;
668
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700669 c = omap_clk_get_by_name(os->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600670 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600671 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
672 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300673 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600674 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300675 os->_clk = c;
676 }
677
678 return ret;
679}
680
681/**
682 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
683 * @oh: struct omap_hwmod *
684 *
685 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
686 * clock pointers. Returns 0 on success or -EINVAL on error.
687 */
688static int _init_opt_clks(struct omap_hwmod *oh)
689{
690 struct omap_hwmod_opt_clk *oc;
691 struct clk *c;
692 int i;
693 int ret = 0;
694
695 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700696 c = omap_clk_get_by_name(oc->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600697 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600698 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
699 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300700 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600701 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300702 oc->_clk = c;
703 }
704
705 return ret;
706}
707
708/**
709 * _enable_clocks - enable hwmod main clock and interface clocks
710 * @oh: struct omap_hwmod *
711 *
712 * Enables all clocks necessary for register reads and writes to succeed
713 * on the hwmod @oh. Returns 0.
714 */
715static int _enable_clocks(struct omap_hwmod *oh)
716{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600717 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600718 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600719 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300720
721 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
722
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600723 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300724 clk_enable(oh->_clk);
725
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600726 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600727
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600728 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600729 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300730
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600731 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
732 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300733 }
734
735 /* The opt clocks are controlled by the device driver. */
736
737 return 0;
738}
739
740/**
741 * _disable_clocks - disable hwmod main clock and interface clocks
742 * @oh: struct omap_hwmod *
743 *
744 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
745 */
746static int _disable_clocks(struct omap_hwmod *oh)
747{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600748 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600749 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600750 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300751
752 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
753
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600754 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300755 clk_disable(oh->_clk);
756
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600757 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600758
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600759 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600760 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300761
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600762 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
763 clk_disable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300764 }
765
766 /* The opt clocks are controlled by the device driver. */
767
768 return 0;
769}
770
Benoit Cousson96835af2010-09-21 18:57:58 +0200771static void _enable_optional_clocks(struct omap_hwmod *oh)
772{
773 struct omap_hwmod_opt_clk *oc;
774 int i;
775
776 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
777
778 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
779 if (oc->_clk) {
780 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
781 oc->_clk->name);
782 clk_enable(oc->_clk);
783 }
784}
785
786static void _disable_optional_clocks(struct omap_hwmod *oh)
787{
788 struct omap_hwmod_opt_clk *oc;
789 int i;
790
791 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
792
793 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
794 if (oc->_clk) {
795 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
796 oc->_clk->name);
797 clk_disable(oc->_clk);
798 }
799}
800
Paul Walmsley63c85232009-09-03 20:14:03 +0300801/**
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600802 * _omap4_enable_module - enable CLKCTRL modulemode on OMAP4
Benoit Cousson45c38252011-07-10 05:56:33 -0600803 * @oh: struct omap_hwmod *
804 *
805 * Enables the PRCM module mode related to the hwmod @oh.
806 * No return value.
807 */
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600808static void _omap4_enable_module(struct omap_hwmod *oh)
Benoit Cousson45c38252011-07-10 05:56:33 -0600809{
Benoit Cousson45c38252011-07-10 05:56:33 -0600810 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
811 return;
812
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600813 pr_debug("omap_hwmod: %s: %s: %d\n",
814 oh->name, __func__, oh->prcm.omap4.modulemode);
Benoit Cousson45c38252011-07-10 05:56:33 -0600815
816 omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
817 oh->clkdm->prcm_partition,
818 oh->clkdm->cm_inst,
819 oh->clkdm->clkdm_offs,
820 oh->prcm.omap4.clkctrl_offs);
821}
822
823/**
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800824 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
825 * @oh: struct omap_hwmod *
826 *
827 * Wait for a module @oh to enter slave idle. Returns 0 if the module
828 * does not have an IDLEST bit or if the module successfully enters
829 * slave idle; otherwise, pass along the return value of the
830 * appropriate *_cm*_wait_module_idle() function.
831 */
832static int _omap4_wait_target_disable(struct omap_hwmod *oh)
833{
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800834 if (!oh)
835 return -EINVAL;
836
837 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
838 return 0;
839
840 if (oh->flags & HWMOD_NO_IDLEST)
841 return 0;
842
843 return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
844 oh->clkdm->cm_inst,
845 oh->clkdm->clkdm_offs,
846 oh->prcm.omap4.clkctrl_offs);
847}
848
849/**
Paul Walmsley212738a2011-07-09 19:14:06 -0600850 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
851 * @oh: struct omap_hwmod *oh
852 *
853 * Count and return the number of MPU IRQs associated with the hwmod
854 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
855 * NULL.
856 */
857static int _count_mpu_irqs(struct omap_hwmod *oh)
858{
859 struct omap_hwmod_irq_info *ohii;
860 int i = 0;
861
862 if (!oh || !oh->mpu_irqs)
863 return 0;
864
865 do {
866 ohii = &oh->mpu_irqs[i++];
867 } while (ohii->irq != -1);
868
sricharancc1b0762011-11-23 14:35:07 -0800869 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -0600870}
871
872/**
Paul Walmsleybc614952011-07-09 19:14:07 -0600873 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
874 * @oh: struct omap_hwmod *oh
875 *
876 * Count and return the number of SDMA request lines associated with
877 * the hwmod @oh. Used to allocate struct resource data. Returns 0
878 * if @oh is NULL.
879 */
880static int _count_sdma_reqs(struct omap_hwmod *oh)
881{
882 struct omap_hwmod_dma_info *ohdi;
883 int i = 0;
884
885 if (!oh || !oh->sdma_reqs)
886 return 0;
887
888 do {
889 ohdi = &oh->sdma_reqs[i++];
890 } while (ohdi->dma_req != -1);
891
sricharancc1b0762011-11-23 14:35:07 -0800892 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -0600893}
894
895/**
Paul Walmsley78183f32011-07-09 19:14:05 -0600896 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
897 * @oh: struct omap_hwmod *oh
898 *
899 * Count and return the number of address space ranges associated with
900 * the hwmod @oh. Used to allocate struct resource data. Returns 0
901 * if @oh is NULL.
902 */
903static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
904{
905 struct omap_hwmod_addr_space *mem;
906 int i = 0;
907
908 if (!os || !os->addr)
909 return 0;
910
911 do {
912 mem = &os->addr[i++];
913 } while (mem->pa_start != mem->pa_end);
914
sricharancc1b0762011-11-23 14:35:07 -0800915 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -0600916}
917
918/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -0600919 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
920 * @oh: struct omap_hwmod * to operate on
921 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
922 * @irq: pointer to an unsigned int to store the MPU IRQ number to
923 *
924 * Retrieve a MPU hardware IRQ line number named by @name associated
925 * with the IP block pointed to by @oh. The IRQ number will be filled
926 * into the address pointed to by @dma. When @name is non-null, the
927 * IRQ line number associated with the named entry will be returned.
928 * If @name is null, the first matching entry will be returned. Data
929 * order is not meaningful in hwmod data, so callers are strongly
930 * encouraged to use a non-null @name whenever possible to avoid
931 * unpredictable effects if hwmod data is later added that causes data
932 * ordering to change. Returns 0 upon success or a negative error
933 * code upon error.
934 */
935static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
936 unsigned int *irq)
937{
938 int i;
939 bool found = false;
940
941 if (!oh->mpu_irqs)
942 return -ENOENT;
943
944 i = 0;
945 while (oh->mpu_irqs[i].irq != -1) {
946 if (name == oh->mpu_irqs[i].name ||
947 !strcmp(name, oh->mpu_irqs[i].name)) {
948 found = true;
949 break;
950 }
951 i++;
952 }
953
954 if (!found)
955 return -ENOENT;
956
957 *irq = oh->mpu_irqs[i].irq;
958
959 return 0;
960}
961
962/**
963 * _get_sdma_req_by_name - fetch SDMA request line ID by name
964 * @oh: struct omap_hwmod * to operate on
965 * @name: pointer to the name of the SDMA request line to fetch (optional)
966 * @dma: pointer to an unsigned int to store the request line ID to
967 *
968 * Retrieve an SDMA request line ID named by @name on the IP block
969 * pointed to by @oh. The ID will be filled into the address pointed
970 * to by @dma. When @name is non-null, the request line ID associated
971 * with the named entry will be returned. If @name is null, the first
972 * matching entry will be returned. Data order is not meaningful in
973 * hwmod data, so callers are strongly encouraged to use a non-null
974 * @name whenever possible to avoid unpredictable effects if hwmod
975 * data is later added that causes data ordering to change. Returns 0
976 * upon success or a negative error code upon error.
977 */
978static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
979 unsigned int *dma)
980{
981 int i;
982 bool found = false;
983
984 if (!oh->sdma_reqs)
985 return -ENOENT;
986
987 i = 0;
988 while (oh->sdma_reqs[i].dma_req != -1) {
989 if (name == oh->sdma_reqs[i].name ||
990 !strcmp(name, oh->sdma_reqs[i].name)) {
991 found = true;
992 break;
993 }
994 i++;
995 }
996
997 if (!found)
998 return -ENOENT;
999
1000 *dma = oh->sdma_reqs[i].dma_req;
1001
1002 return 0;
1003}
1004
1005/**
1006 * _get_addr_space_by_name - fetch address space start & end by name
1007 * @oh: struct omap_hwmod * to operate on
1008 * @name: pointer to the name of the address space to fetch (optional)
1009 * @pa_start: pointer to a u32 to store the starting address to
1010 * @pa_end: pointer to a u32 to store the ending address to
1011 *
1012 * Retrieve address space start and end addresses for the IP block
1013 * pointed to by @oh. The data will be filled into the addresses
1014 * pointed to by @pa_start and @pa_end. When @name is non-null, the
1015 * address space data associated with the named entry will be
1016 * returned. If @name is null, the first matching entry will be
1017 * returned. Data order is not meaningful in hwmod data, so callers
1018 * are strongly encouraged to use a non-null @name whenever possible
1019 * to avoid unpredictable effects if hwmod data is later added that
1020 * causes data ordering to change. Returns 0 upon success or a
1021 * negative error code upon error.
1022 */
1023static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1024 u32 *pa_start, u32 *pa_end)
1025{
1026 int i, j;
1027 struct omap_hwmod_ocp_if *os;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001028 struct list_head *p = NULL;
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001029 bool found = false;
1030
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001031 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001032
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001033 i = 0;
1034 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001035 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001036
1037 if (!os->addr)
1038 return -ENOENT;
1039
1040 j = 0;
1041 while (os->addr[j].pa_start != os->addr[j].pa_end) {
1042 if (name == os->addr[j].name ||
1043 !strcmp(name, os->addr[j].name)) {
1044 found = true;
1045 break;
1046 }
1047 j++;
1048 }
1049
1050 if (found)
1051 break;
1052 }
1053
1054 if (!found)
1055 return -ENOENT;
1056
1057 *pa_start = os->addr[j].pa_start;
1058 *pa_end = os->addr[j].pa_end;
1059
1060 return 0;
1061}
1062
1063/**
Paul Walmsley24dbc212012-04-19 04:04:29 -06001064 * _save_mpu_port_index - find and save the index to @oh's MPU port
Paul Walmsley63c85232009-09-03 20:14:03 +03001065 * @oh: struct omap_hwmod *
1066 *
Paul Walmsley24dbc212012-04-19 04:04:29 -06001067 * Determines the array index of the OCP slave port that the MPU uses
1068 * to address the device, and saves it into the struct omap_hwmod.
1069 * Intended to be called during hwmod registration only. No return
1070 * value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001071 */
Paul Walmsley24dbc212012-04-19 04:04:29 -06001072static void __init _save_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001073{
Paul Walmsley24dbc212012-04-19 04:04:29 -06001074 struct omap_hwmod_ocp_if *os = NULL;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001075 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001076 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001077
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001078 if (!oh)
Paul Walmsley24dbc212012-04-19 04:04:29 -06001079 return;
1080
1081 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001082
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001083 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001084
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001085 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001086 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +03001087 if (os->user & OCP_USER_MPU) {
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001088 oh->_mpu_port = os;
Paul Walmsley24dbc212012-04-19 04:04:29 -06001089 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001090 break;
1091 }
1092 }
1093
Paul Walmsley24dbc212012-04-19 04:04:29 -06001094 return;
Paul Walmsley63c85232009-09-03 20:14:03 +03001095}
1096
1097/**
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001098 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1099 * @oh: struct omap_hwmod *
1100 *
1101 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1102 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1103 * communicate with the IP block. This interface need not be directly
1104 * connected to the MPU (and almost certainly is not), but is directly
1105 * connected to the IP block represented by @oh. Returns a pointer
1106 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1107 * error or if there does not appear to be a path from the MPU to this
1108 * IP block.
1109 */
1110static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1111{
1112 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1113 return NULL;
1114
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001115 return oh->_mpu_port;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001116};
1117
1118/**
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001119 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
Paul Walmsley63c85232009-09-03 20:14:03 +03001120 * @oh: struct omap_hwmod *
1121 *
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001122 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1123 * the register target MPU address space; or returns NULL upon error.
Paul Walmsley63c85232009-09-03 20:14:03 +03001124 */
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001125static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001126{
1127 struct omap_hwmod_ocp_if *os;
1128 struct omap_hwmod_addr_space *mem;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001129 int found = 0, i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001130
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001131 os = _find_mpu_rt_port(oh);
Paul Walmsley24dbc212012-04-19 04:04:29 -06001132 if (!os || !os->addr)
Paul Walmsley78183f32011-07-09 19:14:05 -06001133 return NULL;
1134
1135 do {
1136 mem = &os->addr[i++];
1137 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +03001138 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001139 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +03001140
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001141 return (found) ? mem : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001142}
1143
1144/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001145 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001146 * @oh: struct omap_hwmod *
1147 *
1148 * If module is marked as SWSUP_SIDLE, force the module out of slave
1149 * idle; otherwise, configure it for smart-idle. If module is marked
1150 * as SWSUP_MSUSPEND, force the module out of master standby;
1151 * otherwise, configure it for smart-standby. No return value.
1152 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001153static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001154{
Paul Walmsley43b40992010-02-22 22:09:34 -07001155 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001156 u32 v;
1157
Paul Walmsley43b40992010-02-22 22:09:34 -07001158 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001159 return;
1160
1161 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001162 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001163
Paul Walmsley43b40992010-02-22 22:09:34 -07001164 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001165 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1166 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
1167 _set_slave_idlemode(oh, idlemode, &v);
1168 }
1169
Paul Walmsley43b40992010-02-22 22:09:34 -07001170 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001171 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1172 idlemode = HWMOD_IDLEMODE_NO;
1173 } else {
1174 if (sf & SYSC_HAS_ENAWAKEUP)
1175 _enable_wakeup(oh, &v);
1176 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1177 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1178 else
1179 idlemode = HWMOD_IDLEMODE_SMART;
1180 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001181 _set_master_standbymode(oh, idlemode, &v);
1182 }
1183
Paul Walmsleya16b1f72009-12-08 16:34:17 -07001184 /*
1185 * XXX The clock framework should handle this, by
1186 * calling into this code. But this must wait until the
1187 * clock structures are tagged with omap_hwmod entries
1188 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001189 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1190 (sf & SYSC_HAS_CLOCKACTIVITY))
1191 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001192
Rajendra Nayak9980ce52010-09-21 19:58:30 +05301193 /* If slave is in SMARTIDLE, also enable wakeup */
1194 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07001195 _enable_wakeup(oh, &v);
1196
1197 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -06001198
1199 /*
1200 * Set the autoidle bit only after setting the smartidle bit
1201 * Setting this will not have any impact on the other modules.
1202 */
1203 if (sf & SYSC_HAS_AUTOIDLE) {
1204 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1205 0 : 1;
1206 _set_module_autoidle(oh, idlemode, &v);
1207 _write_sysconfig(v, oh);
1208 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001209}
1210
1211/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001212 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001213 * @oh: struct omap_hwmod *
1214 *
1215 * If module is marked as SWSUP_SIDLE, force the module into slave
1216 * idle; otherwise, configure it for smart-idle. If module is marked
1217 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1218 * configure it for smart-standby. No return value.
1219 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001220static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001221{
Paul Walmsley43b40992010-02-22 22:09:34 -07001222 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001223 u32 v;
1224
Paul Walmsley43b40992010-02-22 22:09:34 -07001225 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001226 return;
1227
1228 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001229 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001230
Paul Walmsley43b40992010-02-22 22:09:34 -07001231 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001232 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1233 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
1234 _set_slave_idlemode(oh, idlemode, &v);
1235 }
1236
Paul Walmsley43b40992010-02-22 22:09:34 -07001237 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001238 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1239 idlemode = HWMOD_IDLEMODE_FORCE;
1240 } else {
1241 if (sf & SYSC_HAS_ENAWAKEUP)
1242 _enable_wakeup(oh, &v);
1243 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1244 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1245 else
1246 idlemode = HWMOD_IDLEMODE_SMART;
1247 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001248 _set_master_standbymode(oh, idlemode, &v);
1249 }
1250
Benoit Cousson86009eb2010-12-21 21:31:28 -07001251 /* If slave is in SMARTIDLE, also enable wakeup */
1252 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1253 _enable_wakeup(oh, &v);
1254
Paul Walmsley63c85232009-09-03 20:14:03 +03001255 _write_sysconfig(v, oh);
1256}
1257
1258/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001259 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001260 * @oh: struct omap_hwmod *
1261 *
1262 * Force the module into slave idle and master suspend. No return
1263 * value.
1264 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001265static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001266{
1267 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001268 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001269
Paul Walmsley43b40992010-02-22 22:09:34 -07001270 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001271 return;
1272
1273 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001274 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001275
Paul Walmsley43b40992010-02-22 22:09:34 -07001276 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001277 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1278
Paul Walmsley43b40992010-02-22 22:09:34 -07001279 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001280 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1281
Paul Walmsley43b40992010-02-22 22:09:34 -07001282 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001283 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001284
1285 _write_sysconfig(v, oh);
1286}
1287
1288/**
1289 * _lookup - find an omap_hwmod by name
1290 * @name: find an omap_hwmod by name
1291 *
1292 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001293 */
1294static struct omap_hwmod *_lookup(const char *name)
1295{
1296 struct omap_hwmod *oh, *temp_oh;
1297
1298 oh = NULL;
1299
1300 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1301 if (!strcmp(name, temp_oh->name)) {
1302 oh = temp_oh;
1303 break;
1304 }
1305 }
1306
1307 return oh;
1308}
Benoit Cousson6ae76992011-07-10 05:56:30 -06001309/**
1310 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1311 * @oh: struct omap_hwmod *
1312 *
1313 * Convert a clockdomain name stored in a struct omap_hwmod into a
1314 * clockdomain pointer, and save it into the struct omap_hwmod.
1315 * return -EINVAL if clkdm_name does not exist or if the lookup failed.
1316 */
1317static int _init_clkdm(struct omap_hwmod *oh)
1318{
Benoit Cousson6ae76992011-07-10 05:56:30 -06001319 if (!oh->clkdm_name) {
1320 pr_warning("omap_hwmod: %s: no clkdm_name\n", oh->name);
1321 return -EINVAL;
1322 }
1323
1324 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1325 if (!oh->clkdm) {
1326 pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
1327 oh->name, oh->clkdm_name);
1328 return -EINVAL;
1329 }
1330
1331 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1332 oh->name, oh->clkdm_name);
1333
1334 return 0;
1335}
Paul Walmsley63c85232009-09-03 20:14:03 +03001336
1337/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001338 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1339 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001340 * @oh: struct omap_hwmod *
Paul Walmsley97d601622010-07-26 16:34:30 -06001341 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001342 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001343 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001344 * Resolves all clock names embedded in the hwmod. Returns 0 on
1345 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001346 */
Paul Walmsley97d601622010-07-26 16:34:30 -06001347static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001348{
1349 int ret = 0;
1350
Paul Walmsley48d54f32011-02-23 00:14:07 -07001351 if (oh->_state != _HWMOD_STATE_REGISTERED)
1352 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001353
1354 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1355
1356 ret |= _init_main_clk(oh);
1357 ret |= _init_interface_clks(oh);
1358 ret |= _init_opt_clks(oh);
Kevin Hilman0a179ea2012-06-18 12:12:25 -06001359 if (soc_ops.init_clkdm)
1360 ret |= soc_ops.init_clkdm(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001361
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001362 if (!ret)
1363 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001364 else
1365 pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001366
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001367 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001368}
1369
1370/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001371 * _lookup_hardreset - fill register bit info for this hwmod/reset line
Benoît Cousson5365efb2010-09-21 10:34:11 -06001372 * @oh: struct omap_hwmod *
1373 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001374 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
Benoît Cousson5365efb2010-09-21 10:34:11 -06001375 *
1376 * Return the bit position of the reset line that match the
1377 * input name. Return -ENOENT if not found.
1378 */
omar ramirezcc1226e2011-03-04 13:32:44 -07001379static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1380 struct omap_hwmod_rst_info *ohri)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001381{
1382 int i;
1383
1384 for (i = 0; i < oh->rst_lines_cnt; i++) {
1385 const char *rst_line = oh->rst_lines[i].name;
1386 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001387 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1388 ohri->st_shift = oh->rst_lines[i].st_shift;
1389 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1390 oh->name, __func__, rst_line, ohri->rst_shift,
1391 ohri->st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001392
omar ramirezcc1226e2011-03-04 13:32:44 -07001393 return 0;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001394 }
1395 }
1396
1397 return -ENOENT;
1398}
1399
1400/**
1401 * _assert_hardreset - assert the HW reset line of submodules
1402 * contained in the hwmod module.
1403 * @oh: struct omap_hwmod *
1404 * @name: name of the reset line to lookup and assert
1405 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001406 * Some IP like dsp, ipu or iva contain processor that require an HW
1407 * reset line to be assert / deassert in order to enable fully the IP.
1408 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1409 * asserting the hardreset line on the currently-booted SoC, or passes
1410 * along the return value from _lookup_hardreset() or the SoC's
1411 * assert_hardreset code.
Benoît Cousson5365efb2010-09-21 10:34:11 -06001412 */
1413static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1414{
omar ramirezcc1226e2011-03-04 13:32:44 -07001415 struct omap_hwmod_rst_info ohri;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001416 u8 ret = -EINVAL;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001417
1418 if (!oh)
1419 return -EINVAL;
1420
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001421 if (!soc_ops.assert_hardreset)
1422 return -ENOSYS;
1423
omar ramirezcc1226e2011-03-04 13:32:44 -07001424 ret = _lookup_hardreset(oh, name, &ohri);
1425 if (IS_ERR_VALUE(ret))
1426 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001427
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001428 ret = soc_ops.assert_hardreset(oh, &ohri);
1429
1430 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001431}
1432
1433/**
1434 * _deassert_hardreset - deassert the HW reset line of submodules contained
1435 * in the hwmod module.
1436 * @oh: struct omap_hwmod *
1437 * @name: name of the reset line to look up and deassert
1438 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001439 * Some IP like dsp, ipu or iva contain processor that require an HW
1440 * reset line to be assert / deassert in order to enable fully the IP.
1441 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1442 * deasserting the hardreset line on the currently-booted SoC, or passes
1443 * along the return value from _lookup_hardreset() or the SoC's
1444 * deassert_hardreset code.
Benoît Cousson5365efb2010-09-21 10:34:11 -06001445 */
1446static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1447{
omar ramirezcc1226e2011-03-04 13:32:44 -07001448 struct omap_hwmod_rst_info ohri;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001449 int ret = -EINVAL;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001450
1451 if (!oh)
1452 return -EINVAL;
1453
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001454 if (!soc_ops.deassert_hardreset)
1455 return -ENOSYS;
1456
omar ramirezcc1226e2011-03-04 13:32:44 -07001457 ret = _lookup_hardreset(oh, name, &ohri);
1458 if (IS_ERR_VALUE(ret))
1459 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001460
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001461 ret = soc_ops.deassert_hardreset(oh, &ohri);
omar ramirezcc1226e2011-03-04 13:32:44 -07001462 if (ret == -EBUSY)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001463 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1464
omar ramirezcc1226e2011-03-04 13:32:44 -07001465 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001466}
1467
1468/**
1469 * _read_hardreset - read the HW reset line state of submodules
1470 * contained in the hwmod module
1471 * @oh: struct omap_hwmod *
1472 * @name: name of the reset line to look up and read
1473 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001474 * Return the state of the reset line. Returns -EINVAL if @oh is
1475 * null, -ENOSYS if we have no way of reading the hardreset line
1476 * status on the currently-booted SoC, or passes along the return
1477 * value from _lookup_hardreset() or the SoC's is_hardreset_asserted
1478 * code.
Benoît Cousson5365efb2010-09-21 10:34:11 -06001479 */
1480static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1481{
omar ramirezcc1226e2011-03-04 13:32:44 -07001482 struct omap_hwmod_rst_info ohri;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001483 u8 ret = -EINVAL;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001484
1485 if (!oh)
1486 return -EINVAL;
1487
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001488 if (!soc_ops.is_hardreset_asserted)
1489 return -ENOSYS;
1490
omar ramirezcc1226e2011-03-04 13:32:44 -07001491 ret = _lookup_hardreset(oh, name, &ohri);
1492 if (IS_ERR_VALUE(ret))
1493 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001494
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001495 return soc_ops.is_hardreset_asserted(oh, &ohri);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001496}
1497
1498/**
Paul Walmsley747834a2012-04-18 19:10:04 -06001499 * _are_any_hardreset_lines_asserted - return true if part of @oh is hard-reset
1500 * @oh: struct omap_hwmod *
1501 *
1502 * If any hardreset line associated with @oh is asserted, then return true.
1503 * Otherwise, if @oh has no hardreset lines associated with it, or if
1504 * no hardreset lines associated with @oh are asserted, then return false.
1505 * This function is used to avoid executing some parts of the IP block
1506 * enable/disable sequence if a hardreset line is set.
1507 */
1508static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1509{
1510 int i;
1511
1512 if (oh->rst_lines_cnt == 0)
1513 return false;
1514
1515 for (i = 0; i < oh->rst_lines_cnt; i++)
1516 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1517 return true;
1518
1519 return false;
1520}
1521
1522/**
1523 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1524 * @oh: struct omap_hwmod *
1525 *
1526 * Disable the PRCM module mode related to the hwmod @oh.
1527 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1528 */
1529static int _omap4_disable_module(struct omap_hwmod *oh)
1530{
1531 int v;
1532
Paul Walmsley747834a2012-04-18 19:10:04 -06001533 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1534 return -EINVAL;
1535
1536 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1537
1538 omap4_cminst_module_disable(oh->clkdm->prcm_partition,
1539 oh->clkdm->cm_inst,
1540 oh->clkdm->clkdm_offs,
1541 oh->prcm.omap4.clkctrl_offs);
1542
1543 if (_are_any_hardreset_lines_asserted(oh))
1544 return 0;
1545
1546 v = _omap4_wait_target_disable(oh);
1547 if (v)
1548 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1549 oh->name);
1550
1551 return 0;
1552}
1553
1554/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001555 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001556 * @oh: struct omap_hwmod *
1557 *
1558 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Paul Walmsley30e105c2012-04-19 00:49:09 -06001559 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1560 * reset this way, -EINVAL if the hwmod is in the wrong state,
1561 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001562 *
1563 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001564 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001565 * use the SYSCONFIG softreset bit to provide the status.
1566 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001567 * Note that some IP like McBSP do have reset control but don't have
1568 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001569 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001570static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001571{
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001572 u32 v, softrst_mask;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001573 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001574 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001575
Paul Walmsley43b40992010-02-22 22:09:34 -07001576 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001577 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley30e105c2012-04-19 00:49:09 -06001578 return -ENOENT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001579
1580 /* clocks must be on for this operation */
1581 if (oh->_state != _HWMOD_STATE_ENABLED) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001582 pr_warning("omap_hwmod: %s: reset can only be entered from "
1583 "enabled state\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001584 return -EINVAL;
1585 }
1586
Benoit Cousson96835af2010-09-21 18:57:58 +02001587 /* For some modules, all optionnal clocks need to be enabled as well */
1588 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1589 _enable_optional_clocks(oh);
1590
Paul Walmsleybd361792010-12-14 12:42:35 -07001591 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001592
1593 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001594 ret = _set_softreset(oh, &v);
1595 if (ret)
1596 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001597 _write_sysconfig(v, oh);
1598
Fernando Guzman Lugod99de7f2012-04-13 05:08:03 -06001599 if (oh->class->sysc->srst_udelay)
1600 udelay(oh->class->sysc->srst_udelay);
1601
Benoit Cousson2cb06812010-09-21 18:57:59 +02001602 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001603 omap_test_timeout((omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001604 oh->class->sysc->syss_offs)
1605 & SYSS_RESETDONE_MASK),
1606 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001607 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
1608 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001609 omap_test_timeout(!(omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001610 oh->class->sysc->sysc_offs)
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001611 & softrst_mask),
Benoit Cousson2cb06812010-09-21 18:57:59 +02001612 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001613 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001614
Benoît Cousson5365efb2010-09-21 10:34:11 -06001615 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001616 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1617 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001618 else
Benoît Cousson5365efb2010-09-21 10:34:11 -06001619 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001620
1621 /*
1622 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1623 * _wait_target_ready() or _reset()
1624 */
1625
Benoit Cousson96835af2010-09-21 18:57:58 +02001626 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1627
1628dis_opt_clks:
1629 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1630 _disable_optional_clocks(oh);
1631
1632 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001633}
1634
1635/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001636 * _reset - reset an omap_hwmod
1637 * @oh: struct omap_hwmod *
1638 *
Paul Walmsley30e105c2012-04-19 00:49:09 -06001639 * Resets an omap_hwmod @oh. If the module has a custom reset
1640 * function pointer defined, then call it to reset the IP block, and
1641 * pass along its return value to the caller. Otherwise, if the IP
1642 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1643 * associated with it, call a function to reset the IP block via that
1644 * method, and pass along the return value to the caller. Finally, if
1645 * the IP block has some hardreset lines associated with it, assert
1646 * all of those, but do _not_ deassert them. (This is because driver
1647 * authors have expressed an apparent requirement to control the
1648 * deassertion of the hardreset lines themselves.)
1649 *
1650 * The default software reset mechanism for most OMAP IP blocks is
1651 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1652 * hwmods cannot be reset via this method. Some are not targets and
1653 * therefore have no OCP header registers to access. Others (like the
1654 * IVA) have idiosyncratic reset sequences. So for these relatively
1655 * rare cases, custom reset code can be supplied in the struct
1656 * omap_hwmod_class .reset function pointer. Passes along the return
1657 * value from either _ocp_softreset() or the custom reset function -
1658 * these must return -EINVAL if the hwmod cannot be reset this way or
1659 * if the hwmod is in the wrong state, -ETIMEDOUT if the module did
1660 * not reset in time, or 0 upon success.
Paul Walmsleybd361792010-12-14 12:42:35 -07001661 */
1662static int _reset(struct omap_hwmod *oh)
1663{
Paul Walmsley30e105c2012-04-19 00:49:09 -06001664 int i, r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001665
1666 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1667
Paul Walmsley30e105c2012-04-19 00:49:09 -06001668 if (oh->class->reset) {
1669 r = oh->class->reset(oh);
1670 } else {
1671 if (oh->rst_lines_cnt > 0) {
1672 for (i = 0; i < oh->rst_lines_cnt; i++)
1673 _assert_hardreset(oh, oh->rst_lines[i].name);
1674 return 0;
1675 } else {
1676 r = _ocp_softreset(oh);
1677 if (r == -ENOENT)
1678 r = 0;
1679 }
1680 }
Paul Walmsleybd361792010-12-14 12:42:35 -07001681
Paul Walmsley30e105c2012-04-19 00:49:09 -06001682 /*
1683 * OCP_SYSCONFIG bits need to be reprogrammed after a
1684 * softreset. The _enable() function should be split to avoid
1685 * the rewrite of the OCP_SYSCONFIG register.
1686 */
Rajendra Nayak28008522012-03-13 22:55:23 +05301687 if (oh->class->sysc) {
1688 _update_sysc_cache(oh);
1689 _enable_sysc(oh);
1690 }
1691
Paul Walmsley30e105c2012-04-19 00:49:09 -06001692 return r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001693}
1694
1695/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001696 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001697 * @oh: struct omap_hwmod *
1698 *
1699 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001700 * register target. Returns -EINVAL if the hwmod is in the wrong
1701 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001702 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001703static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001704{
Paul Walmsley747834a2012-04-18 19:10:04 -06001705 int r;
Rajendra Nayak665d0012011-07-10 05:57:07 -06001706 int hwsup = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001707
Benoit Cousson34617e22011-07-01 22:54:07 +02001708 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1709
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001710 /*
Paul Walmsley64813c32012-04-18 19:10:03 -06001711 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
1712 * state at init. Now that someone is really trying to enable
1713 * them, just ensure that the hwmod mux is set.
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001714 */
1715 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
1716 /*
1717 * If the caller has mux data populated, do the mux'ing
1718 * which wouldn't have been done as part of the _enable()
1719 * done during setup.
1720 */
1721 if (oh->mux)
1722 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
1723
1724 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
1725 return 0;
1726 }
1727
Paul Walmsley63c85232009-09-03 20:14:03 +03001728 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1729 oh->_state != _HWMOD_STATE_IDLE &&
1730 oh->_state != _HWMOD_STATE_DISABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001731 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
1732 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001733 return -EINVAL;
1734 }
1735
Benoit Cousson31f62862011-07-01 22:54:05 +02001736 /*
Paul Walmsley747834a2012-04-18 19:10:04 -06001737 * If an IP block contains HW reset lines and any of them are
1738 * asserted, we let integration code associated with that
1739 * block handle the enable. We've received very little
1740 * information on what those driver authors need, and until
1741 * detailed information is provided and the driver code is
1742 * posted to the public lists, this is probably the best we
1743 * can do.
Benoit Cousson31f62862011-07-01 22:54:05 +02001744 */
Paul Walmsley747834a2012-04-18 19:10:04 -06001745 if (_are_any_hardreset_lines_asserted(oh))
1746 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001747
Rajendra Nayak665d0012011-07-10 05:57:07 -06001748 /* Mux pins for device runtime if populated */
1749 if (oh->mux && (!oh->mux->enabled ||
1750 ((oh->_state == _HWMOD_STATE_IDLE) &&
1751 oh->mux->pads_dynamic)))
1752 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
Benoit Cousson34617e22011-07-01 22:54:07 +02001753
Rajendra Nayak665d0012011-07-10 05:57:07 -06001754 _add_initiator_dep(oh, mpu_oh);
1755
1756 if (oh->clkdm) {
1757 /*
1758 * A clockdomain must be in SW_SUP before enabling
1759 * completely the module. The clockdomain can be set
1760 * in HW_AUTO only when the module become ready.
1761 */
1762 hwsup = clkdm_in_hwsup(oh->clkdm);
1763 r = clkdm_hwmod_enable(oh->clkdm, oh);
1764 if (r) {
1765 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1766 oh->name, oh->clkdm->name, r);
1767 return r;
1768 }
Benoit Cousson34617e22011-07-01 22:54:07 +02001769 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06001770
1771 _enable_clocks(oh);
Kevin Hilman9ebfd282012-06-18 12:12:23 -06001772 if (soc_ops.enable_module)
1773 soc_ops.enable_module(oh);
Benoit Cousson34617e22011-07-01 22:54:07 +02001774
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06001775 r = (soc_ops.wait_target_ready) ? soc_ops.wait_target_ready(oh) :
1776 -EINVAL;
Rajendra Nayak665d0012011-07-10 05:57:07 -06001777 if (!r) {
1778 /*
1779 * Set the clockdomain to HW_AUTO only if the target is ready,
1780 * assuming that the previous state was HW_AUTO
1781 */
1782 if (oh->clkdm && hwsup)
1783 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02001784
Rajendra Nayak665d0012011-07-10 05:57:07 -06001785 oh->_state = _HWMOD_STATE_ENABLED;
1786
1787 /* Access the sysconfig only if the target is ready */
1788 if (oh->class->sysc) {
1789 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1790 _update_sysc_cache(oh);
1791 _enable_sysc(oh);
1792 }
1793 } else {
1794 _disable_clocks(oh);
1795 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1796 oh->name, r);
1797
1798 if (oh->clkdm)
1799 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001800 }
1801
Paul Walmsley63c85232009-09-03 20:14:03 +03001802 return r;
1803}
1804
1805/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001806 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001807 * @oh: struct omap_hwmod *
1808 *
1809 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001810 * no further work. Returns -EINVAL if the hwmod is in the wrong
1811 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001812 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001813static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001814{
Benoit Cousson34617e22011-07-01 22:54:07 +02001815 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1816
Paul Walmsley63c85232009-09-03 20:14:03 +03001817 if (oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001818 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
1819 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001820 return -EINVAL;
1821 }
1822
Paul Walmsley747834a2012-04-18 19:10:04 -06001823 if (_are_any_hardreset_lines_asserted(oh))
1824 return 0;
1825
Paul Walmsley43b40992010-02-22 22:09:34 -07001826 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001827 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001828 _del_initiator_dep(oh, mpu_oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001829
Kevin Hilman9ebfd282012-06-18 12:12:23 -06001830 if (soc_ops.disable_module)
1831 soc_ops.disable_module(oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001832
Benoit Cousson45c38252011-07-10 05:56:33 -06001833 /*
1834 * The module must be in idle mode before disabling any parents
1835 * clocks. Otherwise, the parent clock might be disabled before
1836 * the module transition is done, and thus will prevent the
1837 * transition to complete properly.
1838 */
1839 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001840 if (oh->clkdm)
1841 clkdm_hwmod_disable(oh->clkdm, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001842
Tony Lindgren8d9af882010-12-22 18:42:35 -08001843 /* Mux pins for device idle if populated */
Tony Lindgren029268e2011-03-11 11:32:25 -08001844 if (oh->mux && oh->mux->pads_dynamic)
Tony Lindgren8d9af882010-12-22 18:42:35 -08001845 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
1846
Paul Walmsley63c85232009-09-03 20:14:03 +03001847 oh->_state = _HWMOD_STATE_IDLE;
1848
1849 return 0;
1850}
1851
1852/**
Kishon Vijay Abraham I95992172011-03-10 03:50:08 -07001853 * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
1854 * @oh: struct omap_hwmod *
1855 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
1856 *
1857 * Sets the IP block's OCP autoidle bit in hardware, and updates our
1858 * local copy. Intended to be used by drivers that require
1859 * direct manipulation of the AUTOIDLE bits.
1860 * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
1861 * along the return value from _set_module_autoidle().
1862 *
1863 * Any users of this function should be scrutinized carefully.
1864 */
1865int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
1866{
1867 u32 v;
1868 int retval = 0;
1869 unsigned long flags;
1870
1871 if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
1872 return -EINVAL;
1873
1874 spin_lock_irqsave(&oh->_lock, flags);
1875
1876 v = oh->_sysc_cache;
1877
1878 retval = _set_module_autoidle(oh, autoidle, &v);
1879
1880 if (!retval)
1881 _write_sysconfig(v, oh);
1882
1883 spin_unlock_irqrestore(&oh->_lock, flags);
1884
1885 return retval;
1886}
1887
1888/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001889 * _shutdown - shutdown an omap_hwmod
1890 * @oh: struct omap_hwmod *
1891 *
1892 * Shut down an omap_hwmod @oh. This should be called when the driver
1893 * used for the hwmod is removed or unloaded or if the driver is not
1894 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1895 * state or returns 0.
1896 */
1897static int _shutdown(struct omap_hwmod *oh)
1898{
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001899 int ret, i;
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001900 u8 prev_state;
1901
Paul Walmsley63c85232009-09-03 20:14:03 +03001902 if (oh->_state != _HWMOD_STATE_IDLE &&
1903 oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001904 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
1905 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001906 return -EINVAL;
1907 }
1908
Paul Walmsley747834a2012-04-18 19:10:04 -06001909 if (_are_any_hardreset_lines_asserted(oh))
1910 return 0;
1911
Paul Walmsley63c85232009-09-03 20:14:03 +03001912 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
1913
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001914 if (oh->class->pre_shutdown) {
1915 prev_state = oh->_state;
1916 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001917 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001918 ret = oh->class->pre_shutdown(oh);
1919 if (ret) {
1920 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001921 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001922 return ret;
1923 }
1924 }
1925
Miguel Vadillo6481c732011-07-01 22:54:02 +02001926 if (oh->class->sysc) {
1927 if (oh->_state == _HWMOD_STATE_IDLE)
1928 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001929 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02001930 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001931
Benoit Cousson3827f942010-09-21 10:34:08 -06001932 /* clocks and deps are already disabled in idle */
1933 if (oh->_state == _HWMOD_STATE_ENABLED) {
1934 _del_initiator_dep(oh, mpu_oh);
1935 /* XXX what about the other system initiators here? dma, dsp */
Kevin Hilman9ebfd282012-06-18 12:12:23 -06001936 if (soc_ops.disable_module)
1937 soc_ops.disable_module(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001938 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001939 if (oh->clkdm)
1940 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06001941 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001942 /* XXX Should this code also force-disable the optional clocks? */
1943
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001944 for (i = 0; i < oh->rst_lines_cnt; i++)
1945 _assert_hardreset(oh, oh->rst_lines[i].name);
Benoit Cousson31f62862011-07-01 22:54:05 +02001946
Tony Lindgren8d9af882010-12-22 18:42:35 -08001947 /* Mux pins to safe mode or use populated off mode values */
1948 if (oh->mux)
1949 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03001950
1951 oh->_state = _HWMOD_STATE_DISABLED;
1952
1953 return 0;
1954}
1955
1956/**
Paul Walmsley381d0332012-04-19 00:58:22 -06001957 * _init_mpu_rt_base - populate the virtual address for a hwmod
1958 * @oh: struct omap_hwmod * to locate the virtual address
1959 *
1960 * Cache the virtual address used by the MPU to access this IP block's
1961 * registers. This address is needed early so the OCP registers that
1962 * are part of the device's address space can be ioremapped properly.
1963 * No return value.
1964 */
1965static void __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data)
1966{
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001967 struct omap_hwmod_addr_space *mem;
1968 void __iomem *va_start;
1969
1970 if (!oh)
1971 return;
1972
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001973 _save_mpu_port_index(oh);
1974
Paul Walmsley381d0332012-04-19 00:58:22 -06001975 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1976 return;
1977
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001978 mem = _find_mpu_rt_addr_space(oh);
1979 if (!mem) {
1980 pr_debug("omap_hwmod: %s: no MPU register target found\n",
1981 oh->name);
1982 return;
1983 }
1984
1985 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
1986 if (!va_start) {
1987 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
1988 return;
1989 }
1990
1991 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
1992 oh->name, va_start);
1993
1994 oh->_mpu_rt_va = va_start;
Paul Walmsley381d0332012-04-19 00:58:22 -06001995}
1996
1997/**
1998 * _init - initialize internal data for the hwmod @oh
1999 * @oh: struct omap_hwmod *
2000 * @n: (unused)
2001 *
2002 * Look up the clocks and the address space used by the MPU to access
2003 * registers belonging to the hwmod @oh. @oh must already be
2004 * registered at this point. This is the first of two phases for
2005 * hwmod initialization. Code called here does not touch any hardware
2006 * registers, it simply prepares internal data structures. Returns 0
2007 * upon success or if the hwmod isn't registered, or -EINVAL upon
2008 * failure.
2009 */
2010static int __init _init(struct omap_hwmod *oh, void *data)
2011{
2012 int r;
2013
2014 if (oh->_state != _HWMOD_STATE_REGISTERED)
2015 return 0;
2016
2017 _init_mpu_rt_base(oh, NULL);
2018
2019 r = _init_clocks(oh, NULL);
2020 if (IS_ERR_VALUE(r)) {
2021 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2022 return -EINVAL;
2023 }
2024
2025 oh->_state = _HWMOD_STATE_INITIALIZED;
2026
2027 return 0;
2028}
2029
2030/**
Paul Walmsley64813c32012-04-18 19:10:03 -06002031 * _setup_iclk_autoidle - configure an IP block's interface clocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002032 * @oh: struct omap_hwmod *
2033 *
Paul Walmsley64813c32012-04-18 19:10:03 -06002034 * Set up the module's interface clocks. XXX This function is still mostly
2035 * a stub; implementing this properly requires iclk autoidle usecounting in
2036 * the clock code. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03002037 */
Paul Walmsley64813c32012-04-18 19:10:03 -06002038static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002039{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002040 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002041 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002042 int i = 0;
Paul Walmsley381d0332012-04-19 00:58:22 -06002043 if (oh->_state != _HWMOD_STATE_INITIALIZED)
Paul Walmsley64813c32012-04-18 19:10:03 -06002044 return;
Paul Walmsley48d54f32011-02-23 00:14:07 -07002045
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002046 p = oh->slave_ports.next;
Paul Walmsley63c85232009-09-03 20:14:03 +03002047
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002048 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002049 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002050 if (!os->_clk)
Paul Walmsley64813c32012-04-18 19:10:03 -06002051 continue;
Paul Walmsley63c85232009-09-03 20:14:03 +03002052
Paul Walmsley64813c32012-04-18 19:10:03 -06002053 if (os->flags & OCPIF_SWSUP_IDLE) {
2054 /* XXX omap_iclk_deny_idle(c); */
2055 } else {
2056 /* XXX omap_iclk_allow_idle(c); */
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002057 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +03002058 }
2059 }
2060
Paul Walmsley64813c32012-04-18 19:10:03 -06002061 return;
2062}
2063
2064/**
2065 * _setup_reset - reset an IP block during the setup process
2066 * @oh: struct omap_hwmod *
2067 *
2068 * Reset the IP block corresponding to the hwmod @oh during the setup
2069 * process. The IP block is first enabled so it can be successfully
2070 * reset. Returns 0 upon success or a negative error code upon
2071 * failure.
2072 */
2073static int __init _setup_reset(struct omap_hwmod *oh)
2074{
2075 int r;
2076
2077 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2078 return -EINVAL;
Paul Walmsley63c85232009-09-03 20:14:03 +03002079
Paul Walmsley747834a2012-04-18 19:10:04 -06002080 if (oh->rst_lines_cnt == 0) {
2081 r = _enable(oh);
2082 if (r) {
2083 pr_warning("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2084 oh->name, oh->_state);
2085 return -EINVAL;
2086 }
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002087 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002088
Rajendra Nayak28008522012-03-13 22:55:23 +05302089 if (!(oh->flags & HWMOD_INIT_NO_RESET))
Paul Walmsley64813c32012-04-18 19:10:03 -06002090 r = _reset(oh);
2091
2092 return r;
2093}
2094
2095/**
2096 * _setup_postsetup - transition to the appropriate state after _setup
2097 * @oh: struct omap_hwmod *
2098 *
2099 * Place an IP block represented by @oh into a "post-setup" state --
2100 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2101 * this function is called at the end of _setup().) The postsetup
2102 * state for an IP block can be changed by calling
2103 * omap_hwmod_enter_postsetup_state() early in the boot process,
2104 * before one of the omap_hwmod_setup*() functions are called for the
2105 * IP block.
2106 *
2107 * The IP block stays in this state until a PM runtime-based driver is
2108 * loaded for that IP block. A post-setup state of IDLE is
2109 * appropriate for almost all IP blocks with runtime PM-enabled
2110 * drivers, since those drivers are able to enable the IP block. A
2111 * post-setup state of ENABLED is appropriate for kernels with PM
2112 * runtime disabled. The DISABLED state is appropriate for unusual IP
2113 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2114 * included, since the WDTIMER starts running on reset and will reset
2115 * the MPU if left active.
2116 *
2117 * This post-setup mechanism is deprecated. Once all of the OMAP
2118 * drivers have been converted to use PM runtime, and all of the IP
2119 * block data and interconnect data is available to the hwmod code, it
2120 * should be possible to replace this mechanism with a "lazy reset"
2121 * arrangement. In a "lazy reset" setup, each IP block is enabled
2122 * when the driver first probes, then all remaining IP blocks without
2123 * drivers are either shut down or enabled after the drivers have
2124 * loaded. However, this cannot take place until the above
2125 * preconditions have been met, since otherwise the late reset code
2126 * has no way of knowing which IP blocks are in use by drivers, and
2127 * which ones are unused.
2128 *
2129 * No return value.
2130 */
2131static void __init _setup_postsetup(struct omap_hwmod *oh)
2132{
2133 u8 postsetup_state;
2134
2135 if (oh->rst_lines_cnt > 0)
2136 return;
Benoit Cousson76e55892010-09-21 10:34:11 -06002137
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002138 postsetup_state = oh->_postsetup_state;
2139 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2140 postsetup_state = _HWMOD_STATE_ENABLED;
2141
2142 /*
2143 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2144 * it should be set by the core code as a runtime flag during startup
2145 */
2146 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002147 (postsetup_state == _HWMOD_STATE_IDLE)) {
2148 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002149 postsetup_state = _HWMOD_STATE_ENABLED;
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002150 }
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002151
2152 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002153 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002154 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2155 _shutdown(oh);
2156 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2157 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2158 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03002159
Paul Walmsley64813c32012-04-18 19:10:03 -06002160 return;
2161}
2162
2163/**
2164 * _setup - prepare IP block hardware for use
2165 * @oh: struct omap_hwmod *
2166 * @n: (unused, pass NULL)
2167 *
2168 * Configure the IP block represented by @oh. This may include
2169 * enabling the IP block, resetting it, and placing it into a
2170 * post-setup state, depending on the type of IP block and applicable
2171 * flags. IP blocks are reset to prevent any previous configuration
2172 * by the bootloader or previous operating system from interfering
2173 * with power management or other parts of the system. The reset can
2174 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2175 * two phases for hwmod initialization. Code called here generally
2176 * affects the IP block hardware, or system integration hardware
2177 * associated with the IP block. Returns 0.
2178 */
2179static int __init _setup(struct omap_hwmod *oh, void *data)
2180{
2181 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2182 return 0;
2183
2184 _setup_iclk_autoidle(oh);
2185
2186 if (!_setup_reset(oh))
2187 _setup_postsetup(oh);
2188
Paul Walmsley63c85232009-09-03 20:14:03 +03002189 return 0;
2190}
2191
Benoit Cousson0102b622010-12-21 21:31:27 -07002192/**
2193 * _register - register a struct omap_hwmod
2194 * @oh: struct omap_hwmod *
2195 *
2196 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2197 * already has been registered by the same name; -EINVAL if the
2198 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2199 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2200 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2201 * success.
2202 *
2203 * XXX The data should be copied into bootmem, so the original data
2204 * should be marked __initdata and freed after init. This would allow
2205 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2206 * that the copy process would be relatively complex due to the large number
2207 * of substructures.
2208 */
Benoit Cousson01592df2010-12-21 21:31:28 -07002209static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07002210{
Benoit Cousson0102b622010-12-21 21:31:27 -07002211 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2212 (oh->_state != _HWMOD_STATE_UNKNOWN))
2213 return -EINVAL;
2214
Benoit Cousson0102b622010-12-21 21:31:27 -07002215 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2216
Benoit Coussonce35b242010-12-21 21:31:28 -07002217 if (_lookup(oh->name))
2218 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07002219
Benoit Cousson0102b622010-12-21 21:31:27 -07002220 list_add_tail(&oh->node, &omap_hwmod_list);
2221
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002222 INIT_LIST_HEAD(&oh->master_ports);
2223 INIT_LIST_HEAD(&oh->slave_ports);
Benoit Cousson0102b622010-12-21 21:31:27 -07002224 spin_lock_init(&oh->_lock);
2225
2226 oh->_state = _HWMOD_STATE_REGISTERED;
2227
Paul Walmsley569edd702011-02-23 00:14:06 -07002228 /*
2229 * XXX Rather than doing a strcmp(), this should test a flag
2230 * set in the hwmod data, inserted by the autogenerator code.
2231 */
2232 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2233 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07002234
Paul Walmsley569edd702011-02-23 00:14:06 -07002235 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07002236}
Paul Walmsley63c85232009-09-03 20:14:03 +03002237
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002238/**
2239 * _alloc_links - return allocated memory for hwmod links
2240 * @ml: pointer to a struct omap_hwmod_link * for the master link
2241 * @sl: pointer to a struct omap_hwmod_link * for the slave link
2242 *
2243 * Return pointers to two struct omap_hwmod_link records, via the
2244 * addresses pointed to by @ml and @sl. Will first attempt to return
2245 * memory allocated as part of a large initial block, but if that has
2246 * been exhausted, will allocate memory itself. Since ideally this
2247 * second allocation path will never occur, the number of these
2248 * 'supplemental' allocations will be logged when debugging is
2249 * enabled. Returns 0.
2250 */
2251static int __init _alloc_links(struct omap_hwmod_link **ml,
2252 struct omap_hwmod_link **sl)
2253{
2254 unsigned int sz;
2255
2256 if ((free_ls + LINKS_PER_OCP_IF) <= max_ls) {
2257 *ml = &linkspace[free_ls++];
2258 *sl = &linkspace[free_ls++];
2259 return 0;
2260 }
2261
2262 sz = sizeof(struct omap_hwmod_link) * LINKS_PER_OCP_IF;
2263
2264 *sl = NULL;
2265 *ml = alloc_bootmem(sz);
2266
2267 memset(*ml, 0, sz);
2268
2269 *sl = (void *)(*ml) + sizeof(struct omap_hwmod_link);
2270
2271 ls_supp++;
2272 pr_debug("omap_hwmod: supplemental link allocations needed: %d\n",
2273 ls_supp * LINKS_PER_OCP_IF);
2274
2275 return 0;
2276};
2277
2278/**
2279 * _add_link - add an interconnect between two IP blocks
2280 * @oi: pointer to a struct omap_hwmod_ocp_if record
2281 *
2282 * Add struct omap_hwmod_link records connecting the master IP block
2283 * specified in @oi->master to @oi, and connecting the slave IP block
2284 * specified in @oi->slave to @oi. This code is assumed to run before
2285 * preemption or SMP has been enabled, thus avoiding the need for
2286 * locking in this code. Changes to this assumption will require
2287 * additional locking. Returns 0.
2288 */
2289static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2290{
2291 struct omap_hwmod_link *ml, *sl;
2292
2293 pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2294 oi->slave->name);
2295
2296 _alloc_links(&ml, &sl);
2297
2298 ml->ocp_if = oi;
2299 INIT_LIST_HEAD(&ml->node);
2300 list_add(&ml->node, &oi->master->master_ports);
2301 oi->master->masters_cnt++;
2302
2303 sl->ocp_if = oi;
2304 INIT_LIST_HEAD(&sl->node);
2305 list_add(&sl->node, &oi->slave->slave_ports);
2306 oi->slave->slaves_cnt++;
2307
2308 return 0;
2309}
2310
2311/**
2312 * _register_link - register a struct omap_hwmod_ocp_if
2313 * @oi: struct omap_hwmod_ocp_if *
2314 *
2315 * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
2316 * has already been registered; -EINVAL if @oi is NULL or if the
2317 * record pointed to by @oi is missing required fields; or 0 upon
2318 * success.
2319 *
2320 * XXX The data should be copied into bootmem, so the original data
2321 * should be marked __initdata and freed after init. This would allow
2322 * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2323 */
2324static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2325{
2326 if (!oi || !oi->master || !oi->slave || !oi->user)
2327 return -EINVAL;
2328
2329 if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2330 return -EEXIST;
2331
2332 pr_debug("omap_hwmod: registering link from %s to %s\n",
2333 oi->master->name, oi->slave->name);
2334
2335 /*
2336 * Register the connected hwmods, if they haven't been
2337 * registered already
2338 */
2339 if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2340 _register(oi->master);
2341
2342 if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2343 _register(oi->slave);
2344
2345 _add_link(oi);
2346
2347 oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2348
2349 return 0;
2350}
2351
2352/**
2353 * _alloc_linkspace - allocate large block of hwmod links
2354 * @ois: pointer to an array of struct omap_hwmod_ocp_if records to count
2355 *
2356 * Allocate a large block of struct omap_hwmod_link records. This
2357 * improves boot time significantly by avoiding the need to allocate
2358 * individual records one by one. If the number of records to
2359 * allocate in the block hasn't been manually specified, this function
2360 * will count the number of struct omap_hwmod_ocp_if records in @ois
2361 * and use that to determine the allocation size. For SoC families
2362 * that require multiple list registrations, such as OMAP3xxx, this
2363 * estimation process isn't optimal, so manual estimation is advised
2364 * in those cases. Returns -EEXIST if the allocation has already occurred
2365 * or 0 upon success.
2366 */
2367static int __init _alloc_linkspace(struct omap_hwmod_ocp_if **ois)
2368{
2369 unsigned int i = 0;
2370 unsigned int sz;
2371
2372 if (linkspace) {
2373 WARN(1, "linkspace already allocated\n");
2374 return -EEXIST;
2375 }
2376
2377 if (max_ls == 0)
2378 while (ois[i++])
2379 max_ls += LINKS_PER_OCP_IF;
2380
2381 sz = sizeof(struct omap_hwmod_link) * max_ls;
2382
2383 pr_debug("omap_hwmod: %s: allocating %d byte linkspace (%d links)\n",
2384 __func__, sz, max_ls);
2385
2386 linkspace = alloc_bootmem(sz);
2387
2388 memset(linkspace, 0, sz);
2389
2390 return 0;
2391}
Paul Walmsley63c85232009-09-03 20:14:03 +03002392
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002393/* Static functions intended only for use in soc_ops field function pointers */
2394
2395/**
2396 * _omap2_wait_target_ready - wait for a module to leave slave idle
2397 * @oh: struct omap_hwmod *
2398 *
2399 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2400 * does not have an IDLEST bit or if the module successfully leaves
2401 * slave idle; otherwise, pass along the return value of the
2402 * appropriate *_cm*_wait_module_ready() function.
2403 */
2404static int _omap2_wait_target_ready(struct omap_hwmod *oh)
2405{
2406 if (!oh)
2407 return -EINVAL;
2408
2409 if (oh->flags & HWMOD_NO_IDLEST)
2410 return 0;
2411
2412 if (!_find_mpu_rt_port(oh))
2413 return 0;
2414
2415 /* XXX check module SIDLEMODE, hardreset status, enabled clocks */
2416
2417 return omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
2418 oh->prcm.omap2.idlest_reg_id,
2419 oh->prcm.omap2.idlest_idle_bit);
2420}
2421
2422/**
2423 * _omap4_wait_target_ready - wait for a module to leave slave idle
2424 * @oh: struct omap_hwmod *
2425 *
2426 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2427 * does not have an IDLEST bit or if the module successfully leaves
2428 * slave idle; otherwise, pass along the return value of the
2429 * appropriate *_cm*_wait_module_ready() function.
2430 */
2431static int _omap4_wait_target_ready(struct omap_hwmod *oh)
2432{
2433 if (!oh || !oh->clkdm)
2434 return -EINVAL;
2435
2436 if (oh->flags & HWMOD_NO_IDLEST)
2437 return 0;
2438
2439 if (!_find_mpu_rt_port(oh))
2440 return 0;
2441
2442 /* XXX check module SIDLEMODE, hardreset status */
2443
2444 return omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
2445 oh->clkdm->cm_inst,
2446 oh->clkdm->clkdm_offs,
2447 oh->prcm.omap4.clkctrl_offs);
2448}
2449
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002450/**
2451 * _omap2_assert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2452 * @oh: struct omap_hwmod * to assert hardreset
2453 * @ohri: hardreset line data
2454 *
2455 * Call omap2_prm_assert_hardreset() with parameters extracted from
2456 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2457 * use as an soc_ops function pointer. Passes along the return value
2458 * from omap2_prm_assert_hardreset(). XXX This function is scheduled
2459 * for removal when the PRM code is moved into drivers/.
2460 */
2461static int _omap2_assert_hardreset(struct omap_hwmod *oh,
2462 struct omap_hwmod_rst_info *ohri)
2463{
2464 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
2465 ohri->rst_shift);
2466}
2467
2468/**
2469 * _omap2_deassert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2470 * @oh: struct omap_hwmod * to deassert hardreset
2471 * @ohri: hardreset line data
2472 *
2473 * Call omap2_prm_deassert_hardreset() with parameters extracted from
2474 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2475 * use as an soc_ops function pointer. Passes along the return value
2476 * from omap2_prm_deassert_hardreset(). XXX This function is
2477 * scheduled for removal when the PRM code is moved into drivers/.
2478 */
2479static int _omap2_deassert_hardreset(struct omap_hwmod *oh,
2480 struct omap_hwmod_rst_info *ohri)
2481{
2482 return omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
2483 ohri->rst_shift,
2484 ohri->st_shift);
2485}
2486
2487/**
2488 * _omap2_is_hardreset_asserted - call OMAP2 PRM hardreset fn with hwmod args
2489 * @oh: struct omap_hwmod * to test hardreset
2490 * @ohri: hardreset line data
2491 *
2492 * Call omap2_prm_is_hardreset_asserted() with parameters extracted
2493 * from the hwmod @oh and the hardreset line data @ohri. Only
2494 * intended for use as an soc_ops function pointer. Passes along the
2495 * return value from omap2_prm_is_hardreset_asserted(). XXX This
2496 * function is scheduled for removal when the PRM code is moved into
2497 * drivers/.
2498 */
2499static int _omap2_is_hardreset_asserted(struct omap_hwmod *oh,
2500 struct omap_hwmod_rst_info *ohri)
2501{
2502 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
2503 ohri->st_shift);
2504}
2505
2506/**
2507 * _omap4_assert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2508 * @oh: struct omap_hwmod * to assert hardreset
2509 * @ohri: hardreset line data
2510 *
2511 * Call omap4_prminst_assert_hardreset() with parameters extracted
2512 * from the hwmod @oh and the hardreset line data @ohri. Only
2513 * intended for use as an soc_ops function pointer. Passes along the
2514 * return value from omap4_prminst_assert_hardreset(). XXX This
2515 * function is scheduled for removal when the PRM code is moved into
2516 * drivers/.
2517 */
2518static int _omap4_assert_hardreset(struct omap_hwmod *oh,
2519 struct omap_hwmod_rst_info *ohri)
2520
2521{
2522 return omap4_prminst_assert_hardreset(ohri->rst_shift,
2523 oh->clkdm->pwrdm.ptr->prcm_partition,
2524 oh->clkdm->pwrdm.ptr->prcm_offs,
2525 oh->prcm.omap4.rstctrl_offs);
2526}
2527
2528/**
2529 * _omap4_deassert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2530 * @oh: struct omap_hwmod * to deassert hardreset
2531 * @ohri: hardreset line data
2532 *
2533 * Call omap4_prminst_deassert_hardreset() with parameters extracted
2534 * from the hwmod @oh and the hardreset line data @ohri. Only
2535 * intended for use as an soc_ops function pointer. Passes along the
2536 * return value from omap4_prminst_deassert_hardreset(). XXX This
2537 * function is scheduled for removal when the PRM code is moved into
2538 * drivers/.
2539 */
2540static int _omap4_deassert_hardreset(struct omap_hwmod *oh,
2541 struct omap_hwmod_rst_info *ohri)
2542{
2543 if (ohri->st_shift)
2544 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
2545 oh->name, ohri->name);
2546 return omap4_prminst_deassert_hardreset(ohri->rst_shift,
2547 oh->clkdm->pwrdm.ptr->prcm_partition,
2548 oh->clkdm->pwrdm.ptr->prcm_offs,
2549 oh->prcm.omap4.rstctrl_offs);
2550}
2551
2552/**
2553 * _omap4_is_hardreset_asserted - call OMAP4 PRM hardreset fn with hwmod args
2554 * @oh: struct omap_hwmod * to test hardreset
2555 * @ohri: hardreset line data
2556 *
2557 * Call omap4_prminst_is_hardreset_asserted() with parameters
2558 * extracted from the hwmod @oh and the hardreset line data @ohri.
2559 * Only intended for use as an soc_ops function pointer. Passes along
2560 * the return value from omap4_prminst_is_hardreset_asserted(). XXX
2561 * This function is scheduled for removal when the PRM code is moved
2562 * into drivers/.
2563 */
2564static int _omap4_is_hardreset_asserted(struct omap_hwmod *oh,
2565 struct omap_hwmod_rst_info *ohri)
2566{
2567 return omap4_prminst_is_hardreset_asserted(ohri->rst_shift,
2568 oh->clkdm->pwrdm.ptr->prcm_partition,
2569 oh->clkdm->pwrdm.ptr->prcm_offs,
2570 oh->prcm.omap4.rstctrl_offs);
2571}
2572
Paul Walmsley63c85232009-09-03 20:14:03 +03002573/* Public functions */
2574
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002575u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002576{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002577 if (oh->flags & HWMOD_16BIT_REG)
2578 return __raw_readw(oh->_mpu_rt_va + reg_offs);
2579 else
2580 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002581}
2582
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002583void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002584{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002585 if (oh->flags & HWMOD_16BIT_REG)
2586 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
2587 else
2588 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002589}
2590
Paul Walmsley887adea2010-07-26 16:34:33 -06002591/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002592 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
2593 * @oh: struct omap_hwmod *
2594 *
2595 * This is a public function exposed to drivers. Some drivers may need to do
2596 * some settings before and after resetting the device. Those drivers after
2597 * doing the necessary settings could use this function to start a reset by
2598 * setting the SYSCONFIG.SOFTRESET bit.
2599 */
2600int omap_hwmod_softreset(struct omap_hwmod *oh)
2601{
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002602 u32 v;
2603 int ret;
2604
2605 if (!oh || !(oh->_sysc_cache))
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002606 return -EINVAL;
2607
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002608 v = oh->_sysc_cache;
2609 ret = _set_softreset(oh, &v);
2610 if (ret)
2611 goto error;
2612 _write_sysconfig(v, oh);
2613
2614error:
2615 return ret;
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002616}
2617
2618/**
Paul Walmsley887adea2010-07-26 16:34:33 -06002619 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
2620 * @oh: struct omap_hwmod *
2621 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
2622 *
2623 * Sets the IP block's OCP slave idlemode in hardware, and updates our
2624 * local copy. Intended to be used by drivers that have some erratum
2625 * that requires direct manipulation of the SIDLEMODE bits. Returns
2626 * -EINVAL if @oh is null, or passes along the return value from
2627 * _set_slave_idlemode().
2628 *
2629 * XXX Does this function have any current users? If not, we should
2630 * remove it; it is better to let the rest of the hwmod code handle this.
2631 * Any users of this function should be scrutinized carefully.
2632 */
Kevin Hilman46273e62010-01-26 20:13:03 -07002633int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
2634{
2635 u32 v;
2636 int retval = 0;
2637
2638 if (!oh)
2639 return -EINVAL;
2640
2641 v = oh->_sysc_cache;
2642
2643 retval = _set_slave_idlemode(oh, idlemode, &v);
2644 if (!retval)
2645 _write_sysconfig(v, oh);
2646
2647 return retval;
2648}
2649
Paul Walmsley63c85232009-09-03 20:14:03 +03002650/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002651 * omap_hwmod_lookup - look up a registered omap_hwmod by name
2652 * @name: name of the omap_hwmod to look up
2653 *
2654 * Given a @name of an omap_hwmod, return a pointer to the registered
2655 * struct omap_hwmod *, or NULL upon error.
2656 */
2657struct omap_hwmod *omap_hwmod_lookup(const char *name)
2658{
2659 struct omap_hwmod *oh;
2660
2661 if (!name)
2662 return NULL;
2663
Paul Walmsley63c85232009-09-03 20:14:03 +03002664 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002665
2666 return oh;
2667}
2668
2669/**
2670 * omap_hwmod_for_each - call function for each registered omap_hwmod
2671 * @fn: pointer to a callback function
Paul Walmsley97d601622010-07-26 16:34:30 -06002672 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03002673 *
2674 * Call @fn for each registered omap_hwmod, passing @data to each
2675 * function. @fn must return 0 for success or any other value for
2676 * failure. If @fn returns non-zero, the iteration across omap_hwmods
2677 * will stop and the non-zero return value will be passed to the
2678 * caller of omap_hwmod_for_each(). @fn is called with
2679 * omap_hwmod_for_each() held.
2680 */
Paul Walmsley97d601622010-07-26 16:34:30 -06002681int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
2682 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03002683{
2684 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05302685 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002686
2687 if (!fn)
2688 return -EINVAL;
2689
Paul Walmsley63c85232009-09-03 20:14:03 +03002690 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d601622010-07-26 16:34:30 -06002691 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03002692 if (ret)
2693 break;
2694 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002695
2696 return ret;
2697}
2698
Paul Walmsley63c85232009-09-03 20:14:03 +03002699/**
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002700 * omap_hwmod_register_links - register an array of hwmod links
2701 * @ois: pointer to an array of omap_hwmod_ocp_if to register
2702 *
2703 * Intended to be called early in boot before the clock framework is
2704 * initialized. If @ois is not null, will register all omap_hwmods
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002705 * listed in @ois that are valid for this chip. Returns -EINVAL if
2706 * omap_hwmod_init() hasn't been called before calling this function,
2707 * -ENOMEM if the link memory area can't be allocated, or 0 upon
2708 * success.
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002709 */
2710int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
2711{
2712 int r, i;
2713
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002714 if (!inited)
2715 return -EINVAL;
2716
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002717 if (!ois)
2718 return 0;
2719
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002720 if (!linkspace) {
2721 if (_alloc_linkspace(ois)) {
2722 pr_err("omap_hwmod: could not allocate link space\n");
2723 return -ENOMEM;
2724 }
2725 }
2726
2727 i = 0;
2728 do {
2729 r = _register_link(ois[i]);
2730 WARN(r && r != -EEXIST,
2731 "omap_hwmod: _register_link(%s -> %s) returned %d\n",
2732 ois[i]->master->name, ois[i]->slave->name, r);
2733 } while (ois[++i]);
2734
2735 return 0;
2736}
2737
2738/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002739 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
2740 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002741 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002742 * If the hwmod data corresponding to the MPU subsystem IP block
2743 * hasn't been initialized and set up yet, do so now. This must be
2744 * done first since sleep dependencies may be added from other hwmods
2745 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
2746 * return value.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002747 */
Paul Walmsley381d0332012-04-19 00:58:22 -06002748static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002749{
Paul Walmsley381d0332012-04-19 00:58:22 -06002750 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
2751 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
2752 __func__, MPU_INITIATOR_NAME);
2753 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
2754 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
Paul Walmsley63c85232009-09-03 20:14:03 +03002755}
2756
2757/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002758 * omap_hwmod_setup_one - set up a single hwmod
2759 * @oh_name: const char * name of the already-registered hwmod to set up
2760 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002761 * Initialize and set up a single hwmod. Intended to be used for a
2762 * small number of early devices, such as the timer IP blocks used for
2763 * the scheduler clock. Must be called after omap2_clk_init().
2764 * Resolves the struct clk names to struct clk pointers for each
2765 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
2766 * -EINVAL upon error or 0 upon success.
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002767 */
2768int __init omap_hwmod_setup_one(const char *oh_name)
2769{
2770 struct omap_hwmod *oh;
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002771
2772 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
2773
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002774 oh = _lookup(oh_name);
2775 if (!oh) {
2776 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
2777 return -EINVAL;
2778 }
2779
Paul Walmsley381d0332012-04-19 00:58:22 -06002780 _ensure_mpu_hwmod_is_setup(oh);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002781
Paul Walmsley381d0332012-04-19 00:58:22 -06002782 _init(oh, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002783 _setup(oh, NULL);
2784
2785 return 0;
2786}
2787
2788/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002789 * omap_hwmod_setup_all - set up all registered IP blocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002790 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002791 * Initialize and set up all IP blocks registered with the hwmod code.
2792 * Must be called after omap2_clk_init(). Resolves the struct clk
2793 * names to struct clk pointers for each registered omap_hwmod. Also
2794 * calls _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002795 */
Paul Walmsley550c8092011-02-28 11:58:14 -07002796static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03002797{
Paul Walmsley381d0332012-04-19 00:58:22 -06002798 _ensure_mpu_hwmod_is_setup(NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002799
Paul Walmsley381d0332012-04-19 00:58:22 -06002800 omap_hwmod_for_each(_init, NULL);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002801 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002802
2803 return 0;
2804}
Paul Walmsley550c8092011-02-28 11:58:14 -07002805core_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03002806
2807/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002808 * omap_hwmod_enable - enable an omap_hwmod
2809 * @oh: struct omap_hwmod *
2810 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002811 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03002812 * Returns -EINVAL on error or passes along the return value from _enable().
2813 */
2814int omap_hwmod_enable(struct omap_hwmod *oh)
2815{
2816 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002817 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002818
2819 if (!oh)
2820 return -EINVAL;
2821
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002822 spin_lock_irqsave(&oh->_lock, flags);
2823 r = _enable(oh);
2824 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002825
2826 return r;
2827}
2828
2829/**
2830 * omap_hwmod_idle - idle an omap_hwmod
2831 * @oh: struct omap_hwmod *
2832 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002833 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03002834 * Returns -EINVAL on error or passes along the return value from _idle().
2835 */
2836int omap_hwmod_idle(struct omap_hwmod *oh)
2837{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002838 unsigned long flags;
2839
Paul Walmsley63c85232009-09-03 20:14:03 +03002840 if (!oh)
2841 return -EINVAL;
2842
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002843 spin_lock_irqsave(&oh->_lock, flags);
2844 _idle(oh);
2845 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002846
2847 return 0;
2848}
2849
2850/**
2851 * omap_hwmod_shutdown - shutdown an omap_hwmod
2852 * @oh: struct omap_hwmod *
2853 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002854 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03002855 * omap_device_shutdown(). Returns -EINVAL on error or passes along
2856 * the return value from _shutdown().
2857 */
2858int omap_hwmod_shutdown(struct omap_hwmod *oh)
2859{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002860 unsigned long flags;
2861
Paul Walmsley63c85232009-09-03 20:14:03 +03002862 if (!oh)
2863 return -EINVAL;
2864
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002865 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002866 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002867 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002868
2869 return 0;
2870}
2871
2872/**
2873 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
2874 * @oh: struct omap_hwmod *oh
2875 *
2876 * Intended to be called by the omap_device code.
2877 */
2878int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
2879{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002880 unsigned long flags;
2881
2882 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002883 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002884 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002885
2886 return 0;
2887}
2888
2889/**
2890 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
2891 * @oh: struct omap_hwmod *oh
2892 *
2893 * Intended to be called by the omap_device code.
2894 */
2895int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
2896{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002897 unsigned long flags;
2898
2899 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002900 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002901 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002902
2903 return 0;
2904}
2905
2906/**
2907 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
2908 * @oh: struct omap_hwmod *oh
2909 *
2910 * Intended to be called by drivers and core code when all posted
2911 * writes to a device must complete before continuing further
2912 * execution (for example, after clearing some device IRQSTATUS
2913 * register bits)
2914 *
2915 * XXX what about targets with multiple OCP threads?
2916 */
2917void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
2918{
2919 BUG_ON(!oh);
2920
Paul Walmsley43b40992010-02-22 22:09:34 -07002921 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Russell King4f8a4282012-02-07 10:59:37 +00002922 WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
2923 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002924 return;
2925 }
2926
2927 /*
2928 * Forces posted writes to complete on the OCP thread handling
2929 * register writes
2930 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002931 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002932}
2933
2934/**
2935 * omap_hwmod_reset - reset the hwmod
2936 * @oh: struct omap_hwmod *
2937 *
2938 * Under some conditions, a driver may wish to reset the entire device.
2939 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06002940 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03002941 */
2942int omap_hwmod_reset(struct omap_hwmod *oh)
2943{
2944 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002945 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002946
Liam Girdwood9b579112010-09-21 10:34:09 -06002947 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002948 return -EINVAL;
2949
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002950 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002951 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002952 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002953
2954 return r;
2955}
2956
Paul Walmsley5e8370f2012-04-18 19:10:06 -06002957/*
2958 * IP block data retrieval functions
2959 */
2960
Paul Walmsley63c85232009-09-03 20:14:03 +03002961/**
2962 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
2963 * @oh: struct omap_hwmod *
2964 * @res: pointer to the first element of an array of struct resource to fill
2965 *
2966 * Count the number of struct resource array elements necessary to
2967 * contain omap_hwmod @oh resources. Intended to be called by code
2968 * that registers omap_devices. Intended to be used to determine the
2969 * size of a dynamically-allocated struct resource array, before
2970 * calling omap_hwmod_fill_resources(). Returns the number of struct
2971 * resource array elements needed.
2972 *
2973 * XXX This code is not optimized. It could attempt to merge adjacent
2974 * resource IDs.
2975 *
2976 */
2977int omap_hwmod_count_resources(struct omap_hwmod *oh)
2978{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002979 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002980 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002981 int ret;
2982 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002983
Paul Walmsleybc614952011-07-09 19:14:07 -06002984 ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002985
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002986 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002987
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002988 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002989 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002990 ret += _count_ocp_if_addr_spaces(os);
2991 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002992
2993 return ret;
2994}
2995
2996/**
2997 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
2998 * @oh: struct omap_hwmod *
2999 * @res: pointer to the first element of an array of struct resource to fill
3000 *
3001 * Fill the struct resource array @res with resource data from the
3002 * omap_hwmod @oh. Intended to be called by code that registers
3003 * omap_devices. See also omap_hwmod_count_resources(). Returns the
3004 * number of array elements filled.
3005 */
3006int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
3007{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003008 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003009 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003010 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03003011 int r = 0;
3012
3013 /* For each IRQ, DMA, memory area, fill in array.*/
3014
Paul Walmsley212738a2011-07-09 19:14:06 -06003015 mpu_irqs_cnt = _count_mpu_irqs(oh);
3016 for (i = 0; i < mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07003017 (res + r)->name = (oh->mpu_irqs + i)->name;
3018 (res + r)->start = (oh->mpu_irqs + i)->irq;
3019 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03003020 (res + r)->flags = IORESOURCE_IRQ;
3021 r++;
3022 }
3023
Paul Walmsleybc614952011-07-09 19:14:07 -06003024 sdma_reqs_cnt = _count_sdma_reqs(oh);
3025 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06003026 (res + r)->name = (oh->sdma_reqs + i)->name;
3027 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
3028 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03003029 (res + r)->flags = IORESOURCE_DMA;
3030 r++;
3031 }
3032
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003033 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003034
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003035 i = 0;
3036 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003037 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley78183f32011-07-09 19:14:05 -06003038 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03003039
Paul Walmsley78183f32011-07-09 19:14:05 -06003040 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08003041 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03003042 (res + r)->start = (os->addr + j)->pa_start;
3043 (res + r)->end = (os->addr + j)->pa_end;
3044 (res + r)->flags = IORESOURCE_MEM;
3045 r++;
3046 }
3047 }
3048
3049 return r;
3050}
3051
3052/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06003053 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
3054 * @oh: struct omap_hwmod * to operate on
3055 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
3056 * @name: pointer to the name of the data to fetch (optional)
3057 * @rsrc: pointer to a struct resource, allocated by the caller
3058 *
3059 * Retrieve MPU IRQ, SDMA request line, or address space start/end
3060 * data for the IP block pointed to by @oh. The data will be filled
3061 * into a struct resource record pointed to by @rsrc. The struct
3062 * resource must be allocated by the caller. When @name is non-null,
3063 * the data associated with the matching entry in the IRQ/SDMA/address
3064 * space hwmod data arrays will be returned. If @name is null, the
3065 * first array entry will be returned. Data order is not meaningful
3066 * in hwmod data, so callers are strongly encouraged to use a non-null
3067 * @name whenever possible to avoid unpredictable effects if hwmod
3068 * data is later added that causes data ordering to change. This
3069 * function is only intended for use by OMAP core code. Device
3070 * drivers should not call this function - the appropriate bus-related
3071 * data accessor functions should be used instead. Returns 0 upon
3072 * success or a negative error code upon error.
3073 */
3074int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
3075 const char *name, struct resource *rsrc)
3076{
3077 int r;
3078 unsigned int irq, dma;
3079 u32 pa_start, pa_end;
3080
3081 if (!oh || !rsrc)
3082 return -EINVAL;
3083
3084 if (type == IORESOURCE_IRQ) {
3085 r = _get_mpu_irq_by_name(oh, name, &irq);
3086 if (r)
3087 return r;
3088
3089 rsrc->start = irq;
3090 rsrc->end = irq;
3091 } else if (type == IORESOURCE_DMA) {
3092 r = _get_sdma_req_by_name(oh, name, &dma);
3093 if (r)
3094 return r;
3095
3096 rsrc->start = dma;
3097 rsrc->end = dma;
3098 } else if (type == IORESOURCE_MEM) {
3099 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
3100 if (r)
3101 return r;
3102
3103 rsrc->start = pa_start;
3104 rsrc->end = pa_end;
3105 } else {
3106 return -EINVAL;
3107 }
3108
3109 rsrc->flags = type;
3110 rsrc->name = name;
3111
3112 return 0;
3113}
3114
3115/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003116 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
3117 * @oh: struct omap_hwmod *
3118 *
3119 * Return the powerdomain pointer associated with the OMAP module
3120 * @oh's main clock. If @oh does not have a main clk, return the
3121 * powerdomain associated with the interface clock associated with the
3122 * module's MPU port. (XXX Perhaps this should use the SDMA port
3123 * instead?) Returns NULL on error, or a struct powerdomain * on
3124 * success.
3125 */
3126struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
3127{
3128 struct clk *c;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003129 struct omap_hwmod_ocp_if *oi;
Paul Walmsley63c85232009-09-03 20:14:03 +03003130
3131 if (!oh)
3132 return NULL;
3133
3134 if (oh->_clk) {
3135 c = oh->_clk;
3136 } else {
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003137 oi = _find_mpu_rt_port(oh);
3138 if (!oi)
Paul Walmsley63c85232009-09-03 20:14:03 +03003139 return NULL;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003140 c = oi->_clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03003141 }
3142
Thara Gopinathd5647c12010-03-31 04:16:29 -06003143 if (!c->clkdm)
3144 return NULL;
3145
Paul Walmsley63c85232009-09-03 20:14:03 +03003146 return c->clkdm->pwrdm.ptr;
3147
3148}
3149
3150/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06003151 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3152 * @oh: struct omap_hwmod *
3153 *
3154 * Returns the virtual address corresponding to the beginning of the
3155 * module's register target, in the address range that is intended to
3156 * be used by the MPU. Returns the virtual address upon success or NULL
3157 * upon error.
3158 */
3159void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3160{
3161 if (!oh)
3162 return NULL;
3163
3164 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3165 return NULL;
3166
3167 if (oh->_state == _HWMOD_STATE_UNKNOWN)
3168 return NULL;
3169
3170 return oh->_mpu_rt_va;
3171}
3172
3173/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003174 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
3175 * @oh: struct omap_hwmod *
3176 * @init_oh: struct omap_hwmod * (initiator)
3177 *
3178 * Add a sleep dependency between the initiator @init_oh and @oh.
3179 * Intended to be called by DSP/Bridge code via platform_data for the
3180 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3181 * code needs to add/del initiator dependencies dynamically
3182 * before/after accessing a device. Returns the return value from
3183 * _add_initiator_dep().
3184 *
3185 * XXX Keep a usecount in the clockdomain code
3186 */
3187int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
3188 struct omap_hwmod *init_oh)
3189{
3190 return _add_initiator_dep(oh, init_oh);
3191}
3192
3193/*
3194 * XXX what about functions for drivers to save/restore ocp_sysconfig
3195 * for context save/restore operations?
3196 */
3197
3198/**
3199 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
3200 * @oh: struct omap_hwmod *
3201 * @init_oh: struct omap_hwmod * (initiator)
3202 *
3203 * Remove a sleep dependency between the initiator @init_oh and @oh.
3204 * Intended to be called by DSP/Bridge code via platform_data for the
3205 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3206 * code needs to add/del initiator dependencies dynamically
3207 * before/after accessing a device. Returns the return value from
3208 * _del_initiator_dep().
3209 *
3210 * XXX Keep a usecount in the clockdomain code
3211 */
3212int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
3213 struct omap_hwmod *init_oh)
3214{
3215 return _del_initiator_dep(oh, init_oh);
3216}
3217
3218/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003219 * omap_hwmod_enable_wakeup - allow device to wake up the system
3220 * @oh: struct omap_hwmod *
3221 *
3222 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
Govindraj.R2a1cc142012-04-05 02:59:32 -06003223 * send wakeups to the PRCM, and enable I/O ring wakeup events for
3224 * this IP block if it has dynamic mux entries. Eventually this
3225 * should set PRCM wakeup registers to cause the PRCM to receive
3226 * wakeup events from the module. Does not set any wakeup routing
3227 * registers beyond this point - if the module is to wake up any other
3228 * module or subsystem, that must be set separately. Called by
3229 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003230 */
3231int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3232{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003233 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003234 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003235
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003236 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003237
3238 if (oh->class->sysc &&
3239 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3240 v = oh->_sysc_cache;
3241 _enable_wakeup(oh, &v);
3242 _write_sysconfig(v, oh);
3243 }
3244
Govindraj Receec002011-12-16 14:36:58 -07003245 _set_idle_ioring_wakeup(oh, true);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003246 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003247
3248 return 0;
3249}
3250
3251/**
3252 * omap_hwmod_disable_wakeup - prevent device from waking the system
3253 * @oh: struct omap_hwmod *
3254 *
3255 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
Govindraj.R2a1cc142012-04-05 02:59:32 -06003256 * from sending wakeups to the PRCM, and disable I/O ring wakeup
3257 * events for this IP block if it has dynamic mux entries. Eventually
3258 * this should clear PRCM wakeup registers to cause the PRCM to ignore
3259 * wakeup events from the module. Does not set any wakeup routing
3260 * registers beyond this point - if the module is to wake up any other
3261 * module or subsystem, that must be set separately. Called by
3262 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003263 */
3264int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3265{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003266 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003267 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003268
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003269 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003270
3271 if (oh->class->sysc &&
3272 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3273 v = oh->_sysc_cache;
3274 _disable_wakeup(oh, &v);
3275 _write_sysconfig(v, oh);
3276 }
3277
Govindraj Receec002011-12-16 14:36:58 -07003278 _set_idle_ioring_wakeup(oh, false);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003279 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003280
3281 return 0;
3282}
Paul Walmsley43b40992010-02-22 22:09:34 -07003283
3284/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003285 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3286 * contained in the hwmod module.
3287 * @oh: struct omap_hwmod *
3288 * @name: name of the reset line to lookup and assert
3289 *
3290 * Some IP like dsp, ipu or iva contain processor that require
3291 * an HW reset line to be assert / deassert in order to enable fully
3292 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3293 * yet supported on this OMAP; otherwise, passes along the return value
3294 * from _assert_hardreset().
3295 */
3296int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3297{
3298 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003299 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003300
3301 if (!oh)
3302 return -EINVAL;
3303
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003304 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003305 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003306 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003307
3308 return ret;
3309}
3310
3311/**
3312 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3313 * contained in the hwmod module.
3314 * @oh: struct omap_hwmod *
3315 * @name: name of the reset line to look up and deassert
3316 *
3317 * Some IP like dsp, ipu or iva contain processor that require
3318 * an HW reset line to be assert / deassert in order to enable fully
3319 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3320 * yet supported on this OMAP; otherwise, passes along the return value
3321 * from _deassert_hardreset().
3322 */
3323int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3324{
3325 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003326 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003327
3328 if (!oh)
3329 return -EINVAL;
3330
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003331 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003332 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003333 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003334
3335 return ret;
3336}
3337
3338/**
3339 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
3340 * contained in the hwmod module
3341 * @oh: struct omap_hwmod *
3342 * @name: name of the reset line to look up and read
3343 *
3344 * Return the current state of the hwmod @oh's reset line named @name:
3345 * returns -EINVAL upon parameter error or if this operation
3346 * is unsupported on the current OMAP; otherwise, passes along the return
3347 * value from _read_hardreset().
3348 */
3349int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
3350{
3351 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003352 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003353
3354 if (!oh)
3355 return -EINVAL;
3356
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003357 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003358 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003359 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003360
3361 return ret;
3362}
3363
3364
3365/**
Paul Walmsley43b40992010-02-22 22:09:34 -07003366 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3367 * @classname: struct omap_hwmod_class name to search for
3368 * @fn: callback function pointer to call for each hwmod in class @classname
3369 * @user: arbitrary context data to pass to the callback function
3370 *
Benoit Coussonce35b242010-12-21 21:31:28 -07003371 * For each omap_hwmod of class @classname, call @fn.
3372 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07003373 * zero, the iterator is terminated, and the callback function's return
3374 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3375 * if @classname or @fn are NULL, or passes back the error code from @fn.
3376 */
3377int omap_hwmod_for_each_by_class(const char *classname,
3378 int (*fn)(struct omap_hwmod *oh,
3379 void *user),
3380 void *user)
3381{
3382 struct omap_hwmod *temp_oh;
3383 int ret = 0;
3384
3385 if (!classname || !fn)
3386 return -EINVAL;
3387
3388 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3389 __func__, classname);
3390
Paul Walmsley43b40992010-02-22 22:09:34 -07003391 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3392 if (!strcmp(temp_oh->class->name, classname)) {
3393 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3394 __func__, temp_oh->name);
3395 ret = (*fn)(temp_oh, user);
3396 if (ret)
3397 break;
3398 }
3399 }
3400
Paul Walmsley43b40992010-02-22 22:09:34 -07003401 if (ret)
3402 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3403 __func__, ret);
3404
3405 return ret;
3406}
3407
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003408/**
3409 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3410 * @oh: struct omap_hwmod *
3411 * @state: state that _setup() should leave the hwmod in
3412 *
Paul Walmsley550c8092011-02-28 11:58:14 -07003413 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsley64813c32012-04-18 19:10:03 -06003414 * (called by omap_hwmod_setup_*()). See also the documentation
3415 * for _setup_postsetup(), above. Returns 0 upon success or
3416 * -EINVAL if there is a problem with the arguments or if the hwmod is
3417 * in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003418 */
3419int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3420{
3421 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003422 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003423
3424 if (!oh)
3425 return -EINVAL;
3426
3427 if (state != _HWMOD_STATE_DISABLED &&
3428 state != _HWMOD_STATE_ENABLED &&
3429 state != _HWMOD_STATE_IDLE)
3430 return -EINVAL;
3431
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003432 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003433
3434 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3435 ret = -EINVAL;
3436 goto ohsps_unlock;
3437 }
3438
3439 oh->_postsetup_state = state;
3440 ret = 0;
3441
3442ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003443 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003444
3445 return ret;
3446}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003447
3448/**
3449 * omap_hwmod_get_context_loss_count - get lost context count
3450 * @oh: struct omap_hwmod *
3451 *
3452 * Query the powerdomain of of @oh to get the context loss
3453 * count for this device.
3454 *
3455 * Returns the context loss count of the powerdomain assocated with @oh
3456 * upon success, or zero if no powerdomain exists for @oh.
3457 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03003458int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003459{
3460 struct powerdomain *pwrdm;
3461 int ret = 0;
3462
3463 pwrdm = omap_hwmod_get_pwrdm(oh);
3464 if (pwrdm)
3465 ret = pwrdm_get_context_loss_count(pwrdm);
3466
3467 return ret;
3468}
Paul Walmsley43b01642011-03-10 03:50:07 -07003469
3470/**
3471 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
3472 * @oh: struct omap_hwmod *
3473 *
3474 * Prevent the hwmod @oh from being reset during the setup process.
3475 * Intended for use by board-*.c files on boards with devices that
3476 * cannot tolerate being reset. Must be called before the hwmod has
3477 * been set up. Returns 0 upon success or negative error code upon
3478 * failure.
3479 */
3480int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
3481{
3482 if (!oh)
3483 return -EINVAL;
3484
3485 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3486 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
3487 oh->name);
3488 return -EINVAL;
3489 }
3490
3491 oh->flags |= HWMOD_INIT_NO_RESET;
3492
3493 return 0;
3494}
Tero Kristoabc2d542011-12-16 14:36:59 -07003495
3496/**
3497 * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
3498 * @oh: struct omap_hwmod * containing hwmod mux entries
3499 * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
3500 * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
3501 *
3502 * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
3503 * entry number @pad_idx for the hwmod @oh, trigger the interrupt
3504 * service routine for the hwmod's mpu_irqs array index @irq_idx. If
3505 * this function is not called for a given pad_idx, then the ISR
3506 * associated with @oh's first MPU IRQ will be triggered when an I/O
3507 * pad wakeup occurs on that pad. Note that @pad_idx is the index of
3508 * the _dynamic or wakeup_ entry: if there are other entries not
3509 * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
3510 * entries are NOT COUNTED in the dynamic pad index. This function
3511 * must be called separately for each pad that requires its interrupt
3512 * to be re-routed this way. Returns -EINVAL if there is an argument
3513 * problem or if @oh does not have hwmod mux entries or MPU IRQs;
3514 * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
3515 *
3516 * XXX This function interface is fragile. Rather than using array
3517 * indexes, which are subject to unpredictable change, it should be
3518 * using hwmod IRQ names, and some other stable key for the hwmod mux
3519 * pad records.
3520 */
3521int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
3522{
3523 int nr_irqs;
3524
3525 might_sleep();
3526
3527 if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
3528 pad_idx >= oh->mux->nr_pads_dynamic)
3529 return -EINVAL;
3530
3531 /* Check the number of available mpu_irqs */
3532 for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
3533 ;
3534
3535 if (irq_idx >= nr_irqs)
3536 return -EINVAL;
3537
3538 if (!oh->mux->irqs) {
3539 /* XXX What frees this? */
3540 oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
3541 GFP_KERNEL);
3542 if (!oh->mux->irqs)
3543 return -ENOMEM;
3544 }
3545 oh->mux->irqs[pad_idx] = irq_idx;
3546
3547 return 0;
3548}
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003549
3550/**
3551 * omap_hwmod_init - initialize the hwmod code
3552 *
3553 * Sets up some function pointers needed by the hwmod code to operate on the
3554 * currently-booted SoC. Intended to be called once during kernel init
3555 * before any hwmods are registered. No return value.
3556 */
3557void __init omap_hwmod_init(void)
3558{
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003559 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
3560 soc_ops.wait_target_ready = _omap2_wait_target_ready;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06003561 soc_ops.assert_hardreset = _omap2_assert_hardreset;
3562 soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
3563 soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003564 } else if (cpu_is_omap44xx()) {
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003565 soc_ops.enable_module = _omap4_enable_module;
3566 soc_ops.disable_module = _omap4_disable_module;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003567 soc_ops.wait_target_ready = _omap4_wait_target_ready;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06003568 soc_ops.assert_hardreset = _omap4_assert_hardreset;
3569 soc_ops.deassert_hardreset = _omap4_deassert_hardreset;
3570 soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
Kevin Hilman0a179ea2012-06-18 12:12:25 -06003571 soc_ops.init_clkdm = _init_clkdm;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003572 } else {
3573 WARN(1, "omap_hwmod: unknown SoC type\n");
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003574 }
3575
3576 inited = true;
3577}