blob: b3b00f43dd7ca43d9a7fdbb8ffae75e8cde47944 [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
Paul Walmsleya135eaa2012-09-27 10:33:34 -0600142#include "clock.h"
Tony Lindgren2a296c82012-10-02 17:41:35 -0700143#include "omap_hwmod.h"
Paul Walmsley63c85232009-09-03 20:14:03 +0300144
Tony Lindgrendbc04162012-08-31 10:59:07 -0700145#include "soc.h"
146#include "common.h"
147#include "clockdomain.h"
148#include "powerdomain.h"
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -0600149#include "cm2xxx.h"
150#include "cm3xxx.h"
Benoit Coussond0f06312011-07-10 05:56:30 -0600151#include "cminst44xx.h"
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -0600152#include "cm33xx.h"
Paul Walmsleyb13159a2012-10-29 20:57:44 -0600153#include "prm.h"
Paul Walmsley139563a2012-10-21 01:01:10 -0600154#include "prm3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -0700155#include "prm44xx.h"
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -0600156#include "prm33xx.h"
Benoit Coussoneaac3292011-07-10 05:56:31 -0600157#include "prminst44xx.h"
Tony Lindgren8d9af882010-12-22 18:42:35 -0800158#include "mux.h"
Vishwanath BS51658822012-06-22 08:40:04 -0600159#include "pm.h"
Paul Walmsley63c85232009-09-03 20:14:03 +0300160
Paul Walmsley63c85232009-09-03 20:14:03 +0300161/* Name of the OMAP hwmod for the MPU */
Benoit Cousson5c2c0292010-05-20 12:31:10 -0600162#define MPU_INITIATOR_NAME "mpu"
Paul Walmsley63c85232009-09-03 20:14:03 +0300163
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600164/*
165 * Number of struct omap_hwmod_link records per struct
166 * omap_hwmod_ocp_if record (master->slave and slave->master)
167 */
168#define LINKS_PER_OCP_IF 2
169
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600170/**
171 * struct omap_hwmod_soc_ops - fn ptrs for some SoC-specific operations
172 * @enable_module: function to enable a module (via MODULEMODE)
173 * @disable_module: function to disable a module (via MODULEMODE)
174 *
175 * XXX Eventually this functionality will be hidden inside the PRM/CM
176 * device drivers. Until then, this should avoid huge blocks of cpu_is_*()
177 * conditionals in this code.
178 */
179struct omap_hwmod_soc_ops {
180 void (*enable_module)(struct omap_hwmod *oh);
181 int (*disable_module)(struct omap_hwmod *oh);
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -0600182 int (*wait_target_ready)(struct omap_hwmod *oh);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -0600183 int (*assert_hardreset)(struct omap_hwmod *oh,
184 struct omap_hwmod_rst_info *ohri);
185 int (*deassert_hardreset)(struct omap_hwmod *oh,
186 struct omap_hwmod_rst_info *ohri);
187 int (*is_hardreset_asserted)(struct omap_hwmod *oh,
188 struct omap_hwmod_rst_info *ohri);
Kevin Hilman0a179ea2012-06-18 12:12:25 -0600189 int (*init_clkdm)(struct omap_hwmod *oh);
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600190};
191
192/* soc_ops: adapts the omap_hwmod code to the currently-booted SoC */
193static struct omap_hwmod_soc_ops soc_ops;
194
Paul Walmsley63c85232009-09-03 20:14:03 +0300195/* omap_hwmod_list contains all registered struct omap_hwmods */
196static LIST_HEAD(omap_hwmod_list);
197
Paul Walmsley63c85232009-09-03 20:14:03 +0300198/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
199static struct omap_hwmod *mpu_oh;
200
Vishwanath BS51658822012-06-22 08:40:04 -0600201/* io_chain_lock: used to serialize reconfigurations of the I/O chain */
202static DEFINE_SPINLOCK(io_chain_lock);
203
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600204/*
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600205 * linkspace: ptr to a buffer that struct omap_hwmod_link records are
206 * allocated from - used to reduce the number of small memory
207 * allocations, which has a significant impact on performance
208 */
209static struct omap_hwmod_link *linkspace;
210
211/*
212 * free_ls, max_ls: array indexes into linkspace; representing the
213 * next free struct omap_hwmod_link index, and the maximum number of
214 * struct omap_hwmod_link records allocated (respectively)
215 */
216static unsigned short free_ls, max_ls, ls_supp;
Paul Walmsley63c85232009-09-03 20:14:03 +0300217
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600218/* inited: set to true once the hwmod code is initialized */
219static bool inited;
220
Paul Walmsley63c85232009-09-03 20:14:03 +0300221/* Private functions */
222
223/**
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600224 * _fetch_next_ocp_if - return the next OCP interface in a list
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600225 * @p: ptr to a ptr to the list_head inside the ocp_if to return
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600226 * @i: pointer to the index of the element pointed to by @p in the list
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600227 *
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600228 * Return a pointer to the struct omap_hwmod_ocp_if record
229 * containing the struct list_head pointed to by @p, and increment
230 * @p such that a future call to this routine will return the next
231 * record.
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600232 */
233static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600234 int *i)
235{
236 struct omap_hwmod_ocp_if *oi;
237
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600238 oi = list_entry(*p, struct omap_hwmod_link, node)->ocp_if;
239 *p = (*p)->next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600240
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600241 *i = *i + 1;
242
243 return oi;
244}
245
246/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300247 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
248 * @oh: struct omap_hwmod *
249 *
250 * Load the current value of the hwmod OCP_SYSCONFIG register into the
251 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
252 * OCP_SYSCONFIG register or 0 upon success.
253 */
254static int _update_sysc_cache(struct omap_hwmod *oh)
255{
Paul Walmsley43b40992010-02-22 22:09:34 -0700256 if (!oh->class->sysc) {
257 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 +0300258 return -EINVAL;
259 }
260
261 /* XXX ensure module interface clock is up */
262
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -0700263 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300264
Paul Walmsley43b40992010-02-22 22:09:34 -0700265 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
Thara Gopinath883edfd2010-01-19 17:30:51 -0700266 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300267
268 return 0;
269}
270
271/**
272 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
273 * @v: OCP_SYSCONFIG value to write
274 * @oh: struct omap_hwmod *
275 *
Paul Walmsley43b40992010-02-22 22:09:34 -0700276 * Write @v into the module class' OCP_SYSCONFIG register, if it has
277 * one. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +0300278 */
279static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
280{
Paul Walmsley43b40992010-02-22 22:09:34 -0700281 if (!oh->class->sysc) {
282 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 +0300283 return;
284 }
285
286 /* XXX ensure module interface clock is up */
287
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700288 /* Module might have lost context, always update cache and register */
289 oh->_sysc_cache = v;
290 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300291}
292
293/**
294 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
295 * @oh: struct omap_hwmod *
296 * @standbymode: MIDLEMODE field bits
297 * @v: pointer to register contents to modify
298 *
299 * Update the master standby mode bits in @v to be @standbymode for
300 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
301 * upon error or 0 upon success.
302 */
303static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
304 u32 *v)
305{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700306 u32 mstandby_mask;
307 u8 mstandby_shift;
308
Paul Walmsley43b40992010-02-22 22:09:34 -0700309 if (!oh->class->sysc ||
310 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300311 return -EINVAL;
312
Paul Walmsley43b40992010-02-22 22:09:34 -0700313 if (!oh->class->sysc->sysc_fields) {
314 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700315 return -EINVAL;
316 }
317
Paul Walmsley43b40992010-02-22 22:09:34 -0700318 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700319 mstandby_mask = (0x3 << mstandby_shift);
320
321 *v &= ~mstandby_mask;
322 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300323
324 return 0;
325}
326
327/**
328 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
329 * @oh: struct omap_hwmod *
330 * @idlemode: SIDLEMODE field bits
331 * @v: pointer to register contents to modify
332 *
333 * Update the slave idle mode bits in @v to be @idlemode for the @oh
334 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
335 * or 0 upon success.
336 */
337static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
338{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700339 u32 sidle_mask;
340 u8 sidle_shift;
341
Paul Walmsley43b40992010-02-22 22:09:34 -0700342 if (!oh->class->sysc ||
343 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300344 return -EINVAL;
345
Paul Walmsley43b40992010-02-22 22:09:34 -0700346 if (!oh->class->sysc->sysc_fields) {
347 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700348 return -EINVAL;
349 }
350
Paul Walmsley43b40992010-02-22 22:09:34 -0700351 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700352 sidle_mask = (0x3 << sidle_shift);
353
354 *v &= ~sidle_mask;
355 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300356
357 return 0;
358}
359
360/**
361 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
362 * @oh: struct omap_hwmod *
363 * @clockact: CLOCKACTIVITY field bits
364 * @v: pointer to register contents to modify
365 *
366 * Update the clockactivity mode bits in @v to be @clockact for the
367 * @oh hwmod. Used for additional powersaving on some modules. Does
368 * not write to the hardware. Returns -EINVAL upon error or 0 upon
369 * success.
370 */
371static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
372{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700373 u32 clkact_mask;
374 u8 clkact_shift;
375
Paul Walmsley43b40992010-02-22 22:09:34 -0700376 if (!oh->class->sysc ||
377 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300378 return -EINVAL;
379
Paul Walmsley43b40992010-02-22 22:09:34 -0700380 if (!oh->class->sysc->sysc_fields) {
381 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700382 return -EINVAL;
383 }
384
Paul Walmsley43b40992010-02-22 22:09:34 -0700385 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700386 clkact_mask = (0x3 << clkact_shift);
387
388 *v &= ~clkact_mask;
389 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300390
391 return 0;
392}
393
394/**
395 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
396 * @oh: struct omap_hwmod *
397 * @v: pointer to register contents to modify
398 *
399 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
400 * error or 0 upon success.
401 */
402static int _set_softreset(struct omap_hwmod *oh, u32 *v)
403{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700404 u32 softrst_mask;
405
Paul Walmsley43b40992010-02-22 22:09:34 -0700406 if (!oh->class->sysc ||
407 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300408 return -EINVAL;
409
Paul Walmsley43b40992010-02-22 22:09:34 -0700410 if (!oh->class->sysc->sysc_fields) {
411 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700412 return -EINVAL;
413 }
414
Paul Walmsley43b40992010-02-22 22:09:34 -0700415 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700416
417 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300418
419 return 0;
420}
421
422/**
Tero Kristo613ad0e2012-10-29 22:02:13 -0600423 * _wait_softreset_complete - wait for an OCP softreset to complete
424 * @oh: struct omap_hwmod * to wait on
425 *
426 * Wait until the IP block represented by @oh reports that its OCP
427 * softreset is complete. This can be triggered by software (see
428 * _ocp_softreset()) or by hardware upon returning from off-mode (one
429 * example is HSMMC). Waits for up to MAX_MODULE_SOFTRESET_WAIT
430 * microseconds. Returns the number of microseconds waited.
431 */
432static int _wait_softreset_complete(struct omap_hwmod *oh)
433{
434 struct omap_hwmod_class_sysconfig *sysc;
435 u32 softrst_mask;
436 int c = 0;
437
438 sysc = oh->class->sysc;
439
440 if (sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
441 omap_test_timeout((omap_hwmod_read(oh, sysc->syss_offs)
442 & SYSS_RESETDONE_MASK),
443 MAX_MODULE_SOFTRESET_WAIT, c);
444 else if (sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
445 softrst_mask = (0x1 << sysc->sysc_fields->srst_shift);
446 omap_test_timeout(!(omap_hwmod_read(oh, sysc->sysc_offs)
447 & softrst_mask),
448 MAX_MODULE_SOFTRESET_WAIT, c);
449 }
450
451 return c;
452}
453
454/**
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -0600455 * _set_dmadisable: set OCP_SYSCONFIG.DMADISABLE bit in @v
456 * @oh: struct omap_hwmod *
457 *
458 * The DMADISABLE bit is a semi-automatic bit present in sysconfig register
459 * of some modules. When the DMA must perform read/write accesses, the
460 * DMADISABLE bit is cleared by the hardware. But when the DMA must stop
461 * for power management, software must set the DMADISABLE bit back to 1.
462 *
463 * Set the DMADISABLE bit in @v for hwmod @oh. Returns -EINVAL upon
464 * error or 0 upon success.
465 */
466static int _set_dmadisable(struct omap_hwmod *oh)
467{
468 u32 v;
469 u32 dmadisable_mask;
470
471 if (!oh->class->sysc ||
472 !(oh->class->sysc->sysc_flags & SYSC_HAS_DMADISABLE))
473 return -EINVAL;
474
475 if (!oh->class->sysc->sysc_fields) {
476 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
477 return -EINVAL;
478 }
479
480 /* clocks must be on for this operation */
481 if (oh->_state != _HWMOD_STATE_ENABLED) {
482 pr_warn("omap_hwmod: %s: dma can be disabled only from enabled state\n", oh->name);
483 return -EINVAL;
484 }
485
486 pr_debug("omap_hwmod: %s: setting DMADISABLE\n", oh->name);
487
488 v = oh->_sysc_cache;
489 dmadisable_mask =
490 (0x1 << oh->class->sysc->sysc_fields->dmadisable_shift);
491 v |= dmadisable_mask;
492 _write_sysconfig(v, oh);
493
494 return 0;
495}
496
497/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700498 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
499 * @oh: struct omap_hwmod *
500 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
501 * @v: pointer to register contents to modify
502 *
503 * Update the module autoidle bit in @v to be @autoidle for the @oh
504 * hwmod. The autoidle bit controls whether the module can gate
505 * internal clocks automatically when it isn't doing anything; the
506 * exact function of this bit varies on a per-module basis. This
507 * function does not write to the hardware. Returns -EINVAL upon
508 * error or 0 upon success.
509 */
510static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
511 u32 *v)
512{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700513 u32 autoidle_mask;
514 u8 autoidle_shift;
515
Paul Walmsley43b40992010-02-22 22:09:34 -0700516 if (!oh->class->sysc ||
517 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700518 return -EINVAL;
519
Paul Walmsley43b40992010-02-22 22:09:34 -0700520 if (!oh->class->sysc->sysc_fields) {
521 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700522 return -EINVAL;
523 }
524
Paul Walmsley43b40992010-02-22 22:09:34 -0700525 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700526 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700527
528 *v &= ~autoidle_mask;
529 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700530
531 return 0;
532}
533
534/**
Govindraj Receec002011-12-16 14:36:58 -0700535 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
536 * @oh: struct omap_hwmod *
537 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
538 *
539 * Set or clear the I/O pad wakeup flag in the mux entries for the
540 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
541 * in memory. If the hwmod is currently idled, and the new idle
542 * values don't match the previous ones, this function will also
543 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
544 * currently idled, this function won't touch the hardware: the new
545 * mux settings are written to the SCM PADCTRL registers when the
546 * hwmod is idled. No return value.
547 */
548static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
549{
550 struct omap_device_pad *pad;
551 bool change = false;
552 u16 prev_idle;
553 int j;
554
555 if (!oh->mux || !oh->mux->enabled)
556 return;
557
558 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
559 pad = oh->mux->pads_dynamic[j];
560
561 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
562 continue;
563
564 prev_idle = pad->idle;
565
566 if (set_wake)
567 pad->idle |= OMAP_WAKEUP_EN;
568 else
569 pad->idle &= ~OMAP_WAKEUP_EN;
570
571 if (prev_idle != pad->idle)
572 change = true;
573 }
574
575 if (change && oh->_state == _HWMOD_STATE_IDLE)
576 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
577}
578
579/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300580 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
581 * @oh: struct omap_hwmod *
582 *
583 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
584 * upon error or 0 upon success.
585 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700586static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300587{
Paul Walmsley43b40992010-02-22 22:09:34 -0700588 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700589 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200590 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
591 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300592 return -EINVAL;
593
Paul Walmsley43b40992010-02-22 22:09:34 -0700594 if (!oh->class->sysc->sysc_fields) {
595 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700596 return -EINVAL;
597 }
598
Benoit Cousson1fe74112011-07-01 22:54:03 +0200599 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
600 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300601
Benoit Cousson86009eb2010-12-21 21:31:28 -0700602 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
603 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200604 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
605 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700606
Paul Walmsley63c85232009-09-03 20:14:03 +0300607 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
608
609 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
610
611 return 0;
612}
613
614/**
615 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
616 * @oh: struct omap_hwmod *
617 *
618 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
619 * upon error or 0 upon success.
620 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700621static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300622{
Paul Walmsley43b40992010-02-22 22:09:34 -0700623 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700624 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200625 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
626 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300627 return -EINVAL;
628
Paul Walmsley43b40992010-02-22 22:09:34 -0700629 if (!oh->class->sysc->sysc_fields) {
630 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700631 return -EINVAL;
632 }
633
Benoit Cousson1fe74112011-07-01 22:54:03 +0200634 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
635 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300636
Benoit Cousson86009eb2010-12-21 21:31:28 -0700637 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
638 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200639 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
Djamil Elaidi561038f2012-06-17 11:57:51 -0600640 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700641
Paul Walmsley63c85232009-09-03 20:14:03 +0300642 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
643
644 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
645
646 return 0;
647}
648
649/**
650 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
651 * @oh: struct omap_hwmod *
652 *
653 * Prevent the hardware module @oh from entering idle while the
654 * hardare module initiator @init_oh is active. Useful when a module
655 * will be accessed by a particular initiator (e.g., if a module will
656 * be accessed by the IVA, there should be a sleepdep between the IVA
657 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700658 * mode. If the clockdomain is marked as not needing autodeps, return
659 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
660 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300661 */
662static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
663{
664 if (!oh->_clk)
665 return -EINVAL;
666
Paul Walmsley570b54c2011-03-10 03:50:09 -0700667 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
668 return 0;
669
Paul Walmsley55ed9692010-01-26 20:12:59 -0700670 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300671}
672
673/**
674 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
675 * @oh: struct omap_hwmod *
676 *
677 * Allow the hardware module @oh to enter idle while the hardare
678 * module initiator @init_oh is active. Useful when a module will not
679 * be accessed by a particular initiator (e.g., if a module will not
680 * be accessed by the IVA, there should be no sleepdep between the IVA
681 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700682 * mode. If the clockdomain is marked as not needing autodeps, return
683 * 0 without doing anything. Returns -EINVAL upon error or passes
684 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300685 */
686static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
687{
688 if (!oh->_clk)
689 return -EINVAL;
690
Paul Walmsley570b54c2011-03-10 03:50:09 -0700691 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
692 return 0;
693
Paul Walmsley55ed9692010-01-26 20:12:59 -0700694 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300695}
696
697/**
698 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
699 * @oh: struct omap_hwmod *
700 *
701 * Called from _init_clocks(). Populates the @oh _clk (main
702 * functional clock pointer) if a main_clk is present. Returns 0 on
703 * success or -EINVAL on error.
704 */
705static int _init_main_clk(struct omap_hwmod *oh)
706{
Paul Walmsley63c85232009-09-03 20:14:03 +0300707 int ret = 0;
708
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700709 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300710 return 0;
711
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600712 oh->_clk = clk_get(NULL, oh->main_clk);
713 if (IS_ERR(oh->_clk)) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600714 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
715 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600716 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600717 }
Rajendra Nayak4d7cb452012-09-22 02:24:16 -0600718 /*
719 * HACK: This needs a re-visit once clk_prepare() is implemented
720 * to do something meaningful. Today its just a no-op.
721 * If clk_prepare() is used at some point to do things like
722 * voltage scaling etc, then this would have to be moved to
723 * some point where subsystems like i2c and pmic become
724 * available.
725 */
726 clk_prepare(oh->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300727
Benoit Cousson63403382010-05-20 12:31:10 -0600728 if (!oh->_clk->clkdm)
Paul Walmsley3bb05db2012-09-23 17:28:18 -0600729 pr_debug("omap_hwmod: %s: missing clockdomain for %s.\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600730 oh->name, oh->main_clk);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700731
Paul Walmsley63c85232009-09-03 20:14:03 +0300732 return ret;
733}
734
735/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600736 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300737 * @oh: struct omap_hwmod *
738 *
739 * Called from _init_clocks(). Populates the @oh OCP slave interface
740 * clock pointers. Returns 0 on success or -EINVAL on error.
741 */
742static int _init_interface_clks(struct omap_hwmod *oh)
743{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600744 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600745 struct list_head *p;
Paul Walmsley63c85232009-09-03 20:14:03 +0300746 struct clk *c;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600747 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300748 int ret = 0;
749
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600750 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600751
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600752 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600753 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700754 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300755 continue;
756
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600757 c = clk_get(NULL, os->clk);
758 if (IS_ERR(c)) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600759 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
760 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300761 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600762 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300763 os->_clk = c;
Rajendra Nayak4d7cb452012-09-22 02:24:16 -0600764 /*
765 * HACK: This needs a re-visit once clk_prepare() is implemented
766 * to do something meaningful. Today its just a no-op.
767 * If clk_prepare() is used at some point to do things like
768 * voltage scaling etc, then this would have to be moved to
769 * some point where subsystems like i2c and pmic become
770 * available.
771 */
772 clk_prepare(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300773 }
774
775 return ret;
776}
777
778/**
779 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
780 * @oh: struct omap_hwmod *
781 *
782 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
783 * clock pointers. Returns 0 on success or -EINVAL on error.
784 */
785static int _init_opt_clks(struct omap_hwmod *oh)
786{
787 struct omap_hwmod_opt_clk *oc;
788 struct clk *c;
789 int i;
790 int ret = 0;
791
792 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600793 c = clk_get(NULL, oc->clk);
794 if (IS_ERR(c)) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600795 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
796 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300797 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600798 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300799 oc->_clk = c;
Rajendra Nayak4d7cb452012-09-22 02:24:16 -0600800 /*
801 * HACK: This needs a re-visit once clk_prepare() is implemented
802 * to do something meaningful. Today its just a no-op.
803 * If clk_prepare() is used at some point to do things like
804 * voltage scaling etc, then this would have to be moved to
805 * some point where subsystems like i2c and pmic become
806 * available.
807 */
808 clk_prepare(oc->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300809 }
810
811 return ret;
812}
813
814/**
815 * _enable_clocks - enable hwmod main clock and interface clocks
816 * @oh: struct omap_hwmod *
817 *
818 * Enables all clocks necessary for register reads and writes to succeed
819 * on the hwmod @oh. Returns 0.
820 */
821static int _enable_clocks(struct omap_hwmod *oh)
822{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600823 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600824 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600825 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300826
827 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
828
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600829 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300830 clk_enable(oh->_clk);
831
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600832 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600833
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600834 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600835 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300836
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600837 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
838 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300839 }
840
841 /* The opt clocks are controlled by the device driver. */
842
843 return 0;
844}
845
846/**
847 * _disable_clocks - disable hwmod main clock and interface clocks
848 * @oh: struct omap_hwmod *
849 *
850 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
851 */
852static int _disable_clocks(struct omap_hwmod *oh)
853{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600854 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600855 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600856 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300857
858 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
859
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600860 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300861 clk_disable(oh->_clk);
862
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600863 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600864
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600865 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600866 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300867
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600868 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
869 clk_disable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300870 }
871
872 /* The opt clocks are controlled by the device driver. */
873
874 return 0;
875}
876
Benoit Cousson96835af2010-09-21 18:57:58 +0200877static void _enable_optional_clocks(struct omap_hwmod *oh)
878{
879 struct omap_hwmod_opt_clk *oc;
880 int i;
881
882 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
883
884 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
885 if (oc->_clk) {
886 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600887 __clk_get_name(oc->_clk));
Benoit Cousson96835af2010-09-21 18:57:58 +0200888 clk_enable(oc->_clk);
889 }
890}
891
892static void _disable_optional_clocks(struct omap_hwmod *oh)
893{
894 struct omap_hwmod_opt_clk *oc;
895 int i;
896
897 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
898
899 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
900 if (oc->_clk) {
901 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600902 __clk_get_name(oc->_clk));
Benoit Cousson96835af2010-09-21 18:57:58 +0200903 clk_disable(oc->_clk);
904 }
905}
906
Paul Walmsley63c85232009-09-03 20:14:03 +0300907/**
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600908 * _omap4_enable_module - enable CLKCTRL modulemode on OMAP4
Benoit Cousson45c38252011-07-10 05:56:33 -0600909 * @oh: struct omap_hwmod *
910 *
911 * Enables the PRCM module mode related to the hwmod @oh.
912 * No return value.
913 */
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600914static void _omap4_enable_module(struct omap_hwmod *oh)
Benoit Cousson45c38252011-07-10 05:56:33 -0600915{
Benoit Cousson45c38252011-07-10 05:56:33 -0600916 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
917 return;
918
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600919 pr_debug("omap_hwmod: %s: %s: %d\n",
920 oh->name, __func__, oh->prcm.omap4.modulemode);
Benoit Cousson45c38252011-07-10 05:56:33 -0600921
922 omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
923 oh->clkdm->prcm_partition,
924 oh->clkdm->cm_inst,
925 oh->clkdm->clkdm_offs,
926 oh->prcm.omap4.clkctrl_offs);
927}
928
929/**
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -0600930 * _am33xx_enable_module - enable CLKCTRL modulemode on AM33XX
931 * @oh: struct omap_hwmod *
932 *
933 * Enables the PRCM module mode related to the hwmod @oh.
934 * No return value.
935 */
936static void _am33xx_enable_module(struct omap_hwmod *oh)
937{
938 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
939 return;
940
941 pr_debug("omap_hwmod: %s: %s: %d\n",
942 oh->name, __func__, oh->prcm.omap4.modulemode);
943
944 am33xx_cm_module_enable(oh->prcm.omap4.modulemode, oh->clkdm->cm_inst,
945 oh->clkdm->clkdm_offs,
946 oh->prcm.omap4.clkctrl_offs);
947}
948
949/**
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800950 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
951 * @oh: struct omap_hwmod *
952 *
953 * Wait for a module @oh to enter slave idle. Returns 0 if the module
954 * does not have an IDLEST bit or if the module successfully enters
955 * slave idle; otherwise, pass along the return value of the
956 * appropriate *_cm*_wait_module_idle() function.
957 */
958static int _omap4_wait_target_disable(struct omap_hwmod *oh)
959{
Paul Walmsley2b026d12012-09-23 17:28:18 -0600960 if (!oh)
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800961 return -EINVAL;
962
Paul Walmsley2b026d12012-09-23 17:28:18 -0600963 if (oh->_int_flags & _HWMOD_NO_MPU_PORT || !oh->clkdm)
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800964 return 0;
965
966 if (oh->flags & HWMOD_NO_IDLEST)
967 return 0;
968
969 return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
970 oh->clkdm->cm_inst,
971 oh->clkdm->clkdm_offs,
972 oh->prcm.omap4.clkctrl_offs);
973}
974
975/**
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -0600976 * _am33xx_wait_target_disable - wait for a module to be disabled on AM33XX
977 * @oh: struct omap_hwmod *
978 *
979 * Wait for a module @oh to enter slave idle. Returns 0 if the module
980 * does not have an IDLEST bit or if the module successfully enters
981 * slave idle; otherwise, pass along the return value of the
982 * appropriate *_cm*_wait_module_idle() function.
983 */
984static int _am33xx_wait_target_disable(struct omap_hwmod *oh)
985{
986 if (!oh)
987 return -EINVAL;
988
989 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
990 return 0;
991
992 if (oh->flags & HWMOD_NO_IDLEST)
993 return 0;
994
995 return am33xx_cm_wait_module_idle(oh->clkdm->cm_inst,
996 oh->clkdm->clkdm_offs,
997 oh->prcm.omap4.clkctrl_offs);
998}
999
1000/**
Paul Walmsley212738a2011-07-09 19:14:06 -06001001 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
1002 * @oh: struct omap_hwmod *oh
1003 *
1004 * Count and return the number of MPU IRQs associated with the hwmod
1005 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
1006 * NULL.
1007 */
1008static int _count_mpu_irqs(struct omap_hwmod *oh)
1009{
1010 struct omap_hwmod_irq_info *ohii;
1011 int i = 0;
1012
1013 if (!oh || !oh->mpu_irqs)
1014 return 0;
1015
1016 do {
1017 ohii = &oh->mpu_irqs[i++];
1018 } while (ohii->irq != -1);
1019
sricharancc1b0762011-11-23 14:35:07 -08001020 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -06001021}
1022
1023/**
Paul Walmsleybc614952011-07-09 19:14:07 -06001024 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
1025 * @oh: struct omap_hwmod *oh
1026 *
1027 * Count and return the number of SDMA request lines associated with
1028 * the hwmod @oh. Used to allocate struct resource data. Returns 0
1029 * if @oh is NULL.
1030 */
1031static int _count_sdma_reqs(struct omap_hwmod *oh)
1032{
1033 struct omap_hwmod_dma_info *ohdi;
1034 int i = 0;
1035
1036 if (!oh || !oh->sdma_reqs)
1037 return 0;
1038
1039 do {
1040 ohdi = &oh->sdma_reqs[i++];
1041 } while (ohdi->dma_req != -1);
1042
sricharancc1b0762011-11-23 14:35:07 -08001043 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -06001044}
1045
1046/**
Paul Walmsley78183f32011-07-09 19:14:05 -06001047 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
1048 * @oh: struct omap_hwmod *oh
1049 *
1050 * Count and return the number of address space ranges associated with
1051 * the hwmod @oh. Used to allocate struct resource data. Returns 0
1052 * if @oh is NULL.
1053 */
1054static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
1055{
1056 struct omap_hwmod_addr_space *mem;
1057 int i = 0;
1058
1059 if (!os || !os->addr)
1060 return 0;
1061
1062 do {
1063 mem = &os->addr[i++];
1064 } while (mem->pa_start != mem->pa_end);
1065
sricharancc1b0762011-11-23 14:35:07 -08001066 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001067}
1068
1069/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001070 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
1071 * @oh: struct omap_hwmod * to operate on
1072 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
1073 * @irq: pointer to an unsigned int to store the MPU IRQ number to
1074 *
1075 * Retrieve a MPU hardware IRQ line number named by @name associated
1076 * with the IP block pointed to by @oh. The IRQ number will be filled
1077 * into the address pointed to by @dma. When @name is non-null, the
1078 * IRQ line number associated with the named entry will be returned.
1079 * If @name is null, the first matching entry will be returned. Data
1080 * order is not meaningful in hwmod data, so callers are strongly
1081 * encouraged to use a non-null @name whenever possible to avoid
1082 * unpredictable effects if hwmod data is later added that causes data
1083 * ordering to change. Returns 0 upon success or a negative error
1084 * code upon error.
1085 */
1086static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
1087 unsigned int *irq)
1088{
1089 int i;
1090 bool found = false;
1091
1092 if (!oh->mpu_irqs)
1093 return -ENOENT;
1094
1095 i = 0;
1096 while (oh->mpu_irqs[i].irq != -1) {
1097 if (name == oh->mpu_irqs[i].name ||
1098 !strcmp(name, oh->mpu_irqs[i].name)) {
1099 found = true;
1100 break;
1101 }
1102 i++;
1103 }
1104
1105 if (!found)
1106 return -ENOENT;
1107
1108 *irq = oh->mpu_irqs[i].irq;
1109
1110 return 0;
1111}
1112
1113/**
1114 * _get_sdma_req_by_name - fetch SDMA request line ID by name
1115 * @oh: struct omap_hwmod * to operate on
1116 * @name: pointer to the name of the SDMA request line to fetch (optional)
1117 * @dma: pointer to an unsigned int to store the request line ID to
1118 *
1119 * Retrieve an SDMA request line ID named by @name on the IP block
1120 * pointed to by @oh. The ID will be filled into the address pointed
1121 * to by @dma. When @name is non-null, the request line ID associated
1122 * with the named entry will be returned. If @name is null, the first
1123 * matching entry will be returned. Data order is not meaningful in
1124 * hwmod data, so callers are strongly encouraged to use a non-null
1125 * @name whenever possible to avoid unpredictable effects if hwmod
1126 * data is later added that causes data ordering to change. Returns 0
1127 * upon success or a negative error code upon error.
1128 */
1129static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
1130 unsigned int *dma)
1131{
1132 int i;
1133 bool found = false;
1134
1135 if (!oh->sdma_reqs)
1136 return -ENOENT;
1137
1138 i = 0;
1139 while (oh->sdma_reqs[i].dma_req != -1) {
1140 if (name == oh->sdma_reqs[i].name ||
1141 !strcmp(name, oh->sdma_reqs[i].name)) {
1142 found = true;
1143 break;
1144 }
1145 i++;
1146 }
1147
1148 if (!found)
1149 return -ENOENT;
1150
1151 *dma = oh->sdma_reqs[i].dma_req;
1152
1153 return 0;
1154}
1155
1156/**
1157 * _get_addr_space_by_name - fetch address space start & end by name
1158 * @oh: struct omap_hwmod * to operate on
1159 * @name: pointer to the name of the address space to fetch (optional)
1160 * @pa_start: pointer to a u32 to store the starting address to
1161 * @pa_end: pointer to a u32 to store the ending address to
1162 *
1163 * Retrieve address space start and end addresses for the IP block
1164 * pointed to by @oh. The data will be filled into the addresses
1165 * pointed to by @pa_start and @pa_end. When @name is non-null, the
1166 * address space data associated with the named entry will be
1167 * returned. If @name is null, the first matching entry will be
1168 * returned. Data order is not meaningful in hwmod data, so callers
1169 * are strongly encouraged to use a non-null @name whenever possible
1170 * to avoid unpredictable effects if hwmod data is later added that
1171 * causes data ordering to change. Returns 0 upon success or a
1172 * negative error code upon error.
1173 */
1174static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1175 u32 *pa_start, u32 *pa_end)
1176{
1177 int i, j;
1178 struct omap_hwmod_ocp_if *os;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001179 struct list_head *p = NULL;
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001180 bool found = false;
1181
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001182 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001183
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001184 i = 0;
1185 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001186 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001187
1188 if (!os->addr)
1189 return -ENOENT;
1190
1191 j = 0;
1192 while (os->addr[j].pa_start != os->addr[j].pa_end) {
1193 if (name == os->addr[j].name ||
1194 !strcmp(name, os->addr[j].name)) {
1195 found = true;
1196 break;
1197 }
1198 j++;
1199 }
1200
1201 if (found)
1202 break;
1203 }
1204
1205 if (!found)
1206 return -ENOENT;
1207
1208 *pa_start = os->addr[j].pa_start;
1209 *pa_end = os->addr[j].pa_end;
1210
1211 return 0;
1212}
1213
1214/**
Paul Walmsley24dbc212012-04-19 04:04:29 -06001215 * _save_mpu_port_index - find and save the index to @oh's MPU port
Paul Walmsley63c85232009-09-03 20:14:03 +03001216 * @oh: struct omap_hwmod *
1217 *
Paul Walmsley24dbc212012-04-19 04:04:29 -06001218 * Determines the array index of the OCP slave port that the MPU uses
1219 * to address the device, and saves it into the struct omap_hwmod.
1220 * Intended to be called during hwmod registration only. No return
1221 * value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001222 */
Paul Walmsley24dbc212012-04-19 04:04:29 -06001223static void __init _save_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001224{
Paul Walmsley24dbc212012-04-19 04:04:29 -06001225 struct omap_hwmod_ocp_if *os = NULL;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001226 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001227 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001228
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001229 if (!oh)
Paul Walmsley24dbc212012-04-19 04:04:29 -06001230 return;
1231
1232 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001233
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001234 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001235
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001236 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001237 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +03001238 if (os->user & OCP_USER_MPU) {
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001239 oh->_mpu_port = os;
Paul Walmsley24dbc212012-04-19 04:04:29 -06001240 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001241 break;
1242 }
1243 }
1244
Paul Walmsley24dbc212012-04-19 04:04:29 -06001245 return;
Paul Walmsley63c85232009-09-03 20:14:03 +03001246}
1247
1248/**
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001249 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1250 * @oh: struct omap_hwmod *
1251 *
1252 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1253 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1254 * communicate with the IP block. This interface need not be directly
1255 * connected to the MPU (and almost certainly is not), but is directly
1256 * connected to the IP block represented by @oh. Returns a pointer
1257 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1258 * error or if there does not appear to be a path from the MPU to this
1259 * IP block.
1260 */
1261static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1262{
1263 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1264 return NULL;
1265
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001266 return oh->_mpu_port;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001267};
1268
1269/**
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001270 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
Paul Walmsley63c85232009-09-03 20:14:03 +03001271 * @oh: struct omap_hwmod *
1272 *
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001273 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1274 * the register target MPU address space; or returns NULL upon error.
Paul Walmsley63c85232009-09-03 20:14:03 +03001275 */
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001276static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001277{
1278 struct omap_hwmod_ocp_if *os;
1279 struct omap_hwmod_addr_space *mem;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001280 int found = 0, i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001281
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001282 os = _find_mpu_rt_port(oh);
Paul Walmsley24dbc212012-04-19 04:04:29 -06001283 if (!os || !os->addr)
Paul Walmsley78183f32011-07-09 19:14:05 -06001284 return NULL;
1285
1286 do {
1287 mem = &os->addr[i++];
1288 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +03001289 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001290 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +03001291
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001292 return (found) ? mem : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001293}
1294
1295/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001296 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001297 * @oh: struct omap_hwmod *
1298 *
Paul Walmsley006c7f12012-07-04 05:22:53 -06001299 * Ensure that the OCP_SYSCONFIG register for the IP block represented
1300 * by @oh is set to indicate to the PRCM that the IP block is active.
1301 * Usually this means placing the module into smart-idle mode and
1302 * smart-standby, but if there is a bug in the automatic idle handling
1303 * for the IP block, it may need to be placed into the force-idle or
1304 * no-idle variants of these modes. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001305 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001306static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001307{
Paul Walmsley43b40992010-02-22 22:09:34 -07001308 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001309 u32 v;
Paul Walmsley006c7f12012-07-04 05:22:53 -06001310 bool clkdm_act;
Paul Walmsley63c85232009-09-03 20:14:03 +03001311
Paul Walmsley43b40992010-02-22 22:09:34 -07001312 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001313 return;
1314
Tero Kristo613ad0e2012-10-29 22:02:13 -06001315 /*
1316 * Wait until reset has completed, this is needed as the IP
1317 * block is reset automatically by hardware in some cases
1318 * (off-mode for example), and the drivers require the
1319 * IP to be ready when they access it
1320 */
1321 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1322 _enable_optional_clocks(oh);
1323 _wait_softreset_complete(oh);
1324 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1325 _disable_optional_clocks(oh);
1326
Paul Walmsley63c85232009-09-03 20:14:03 +03001327 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001328 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001329
Paul Walmsley43b40992010-02-22 22:09:34 -07001330 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley006c7f12012-07-04 05:22:53 -06001331 clkdm_act = ((oh->clkdm &&
1332 oh->clkdm->flags & CLKDM_ACTIVE_WITH_MPU) ||
1333 (oh->_clk && oh->_clk->clkdm &&
1334 oh->_clk->clkdm->flags & CLKDM_ACTIVE_WITH_MPU));
1335 if (clkdm_act && !(oh->class->sysc->idlemodes &
1336 (SIDLE_SMART | SIDLE_SMART_WKUP)))
1337 idlemode = HWMOD_IDLEMODE_FORCE;
1338 else
1339 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1340 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
Paul Walmsley63c85232009-09-03 20:14:03 +03001341 _set_slave_idlemode(oh, idlemode, &v);
1342 }
1343
Paul Walmsley43b40992010-02-22 22:09:34 -07001344 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001345 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1346 idlemode = HWMOD_IDLEMODE_NO;
1347 } else {
1348 if (sf & SYSC_HAS_ENAWAKEUP)
1349 _enable_wakeup(oh, &v);
1350 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1351 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1352 else
1353 idlemode = HWMOD_IDLEMODE_SMART;
1354 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001355 _set_master_standbymode(oh, idlemode, &v);
1356 }
1357
Paul Walmsleya16b1f72009-12-08 16:34:17 -07001358 /*
1359 * XXX The clock framework should handle this, by
1360 * calling into this code. But this must wait until the
1361 * clock structures are tagged with omap_hwmod entries
1362 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001363 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1364 (sf & SYSC_HAS_CLOCKACTIVITY))
1365 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001366
Rajendra Nayak9980ce52010-09-21 19:58:30 +05301367 /* If slave is in SMARTIDLE, also enable wakeup */
1368 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07001369 _enable_wakeup(oh, &v);
1370
1371 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -06001372
1373 /*
1374 * Set the autoidle bit only after setting the smartidle bit
1375 * Setting this will not have any impact on the other modules.
1376 */
1377 if (sf & SYSC_HAS_AUTOIDLE) {
1378 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1379 0 : 1;
1380 _set_module_autoidle(oh, idlemode, &v);
1381 _write_sysconfig(v, oh);
1382 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001383}
1384
1385/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001386 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001387 * @oh: struct omap_hwmod *
1388 *
1389 * If module is marked as SWSUP_SIDLE, force the module into slave
1390 * idle; otherwise, configure it for smart-idle. If module is marked
1391 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1392 * configure it for smart-standby. No return value.
1393 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001394static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001395{
Paul Walmsley43b40992010-02-22 22:09:34 -07001396 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001397 u32 v;
1398
Paul Walmsley43b40992010-02-22 22:09:34 -07001399 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001400 return;
1401
1402 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001403 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001404
Paul Walmsley43b40992010-02-22 22:09:34 -07001405 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley006c7f12012-07-04 05:22:53 -06001406 /* XXX What about HWMOD_IDLEMODE_SMART_WKUP? */
1407 if (oh->flags & HWMOD_SWSUP_SIDLE ||
1408 !(oh->class->sysc->idlemodes &
1409 (SIDLE_SMART | SIDLE_SMART_WKUP)))
1410 idlemode = HWMOD_IDLEMODE_FORCE;
1411 else
1412 idlemode = HWMOD_IDLEMODE_SMART;
Paul Walmsley63c85232009-09-03 20:14:03 +03001413 _set_slave_idlemode(oh, idlemode, &v);
1414 }
1415
Paul Walmsley43b40992010-02-22 22:09:34 -07001416 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001417 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1418 idlemode = HWMOD_IDLEMODE_FORCE;
1419 } else {
1420 if (sf & SYSC_HAS_ENAWAKEUP)
1421 _enable_wakeup(oh, &v);
1422 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1423 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1424 else
1425 idlemode = HWMOD_IDLEMODE_SMART;
1426 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001427 _set_master_standbymode(oh, idlemode, &v);
1428 }
1429
Benoit Cousson86009eb2010-12-21 21:31:28 -07001430 /* If slave is in SMARTIDLE, also enable wakeup */
1431 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1432 _enable_wakeup(oh, &v);
1433
Paul Walmsley63c85232009-09-03 20:14:03 +03001434 _write_sysconfig(v, oh);
1435}
1436
1437/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001438 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001439 * @oh: struct omap_hwmod *
1440 *
1441 * Force the module into slave idle and master suspend. No return
1442 * value.
1443 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001444static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001445{
1446 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001447 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001448
Paul Walmsley43b40992010-02-22 22:09:34 -07001449 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001450 return;
1451
1452 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001453 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001454
Paul Walmsley43b40992010-02-22 22:09:34 -07001455 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001456 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1457
Paul Walmsley43b40992010-02-22 22:09:34 -07001458 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001459 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1460
Paul Walmsley43b40992010-02-22 22:09:34 -07001461 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001462 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001463
1464 _write_sysconfig(v, oh);
1465}
1466
1467/**
1468 * _lookup - find an omap_hwmod by name
1469 * @name: find an omap_hwmod by name
1470 *
1471 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001472 */
1473static struct omap_hwmod *_lookup(const char *name)
1474{
1475 struct omap_hwmod *oh, *temp_oh;
1476
1477 oh = NULL;
1478
1479 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1480 if (!strcmp(name, temp_oh->name)) {
1481 oh = temp_oh;
1482 break;
1483 }
1484 }
1485
1486 return oh;
1487}
Paul Walmsley868c1572012-06-19 15:01:02 -06001488
Benoit Cousson6ae76992011-07-10 05:56:30 -06001489/**
1490 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1491 * @oh: struct omap_hwmod *
1492 *
1493 * Convert a clockdomain name stored in a struct omap_hwmod into a
1494 * clockdomain pointer, and save it into the struct omap_hwmod.
Paul Walmsley868c1572012-06-19 15:01:02 -06001495 * Return -EINVAL if the clkdm_name lookup failed.
Benoit Cousson6ae76992011-07-10 05:56:30 -06001496 */
1497static int _init_clkdm(struct omap_hwmod *oh)
1498{
Paul Walmsley3bb05db2012-09-23 17:28:18 -06001499 if (!oh->clkdm_name) {
1500 pr_debug("omap_hwmod: %s: missing clockdomain\n", oh->name);
Benoit Cousson6ae76992011-07-10 05:56:30 -06001501 return 0;
Paul Walmsley3bb05db2012-09-23 17:28:18 -06001502 }
Benoit Cousson6ae76992011-07-10 05:56:30 -06001503
Benoit Cousson6ae76992011-07-10 05:56:30 -06001504 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1505 if (!oh->clkdm) {
1506 pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
1507 oh->name, oh->clkdm_name);
1508 return -EINVAL;
1509 }
1510
1511 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1512 oh->name, oh->clkdm_name);
1513
1514 return 0;
1515}
Paul Walmsley63c85232009-09-03 20:14:03 +03001516
1517/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001518 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1519 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001520 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -06001521 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001522 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001523 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001524 * Resolves all clock names embedded in the hwmod. Returns 0 on
1525 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001526 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001527static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001528{
1529 int ret = 0;
1530
Paul Walmsley48d54f32011-02-23 00:14:07 -07001531 if (oh->_state != _HWMOD_STATE_REGISTERED)
1532 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001533
1534 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1535
1536 ret |= _init_main_clk(oh);
1537 ret |= _init_interface_clks(oh);
1538 ret |= _init_opt_clks(oh);
Kevin Hilman0a179ea2012-06-18 12:12:25 -06001539 if (soc_ops.init_clkdm)
1540 ret |= soc_ops.init_clkdm(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001541
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001542 if (!ret)
1543 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001544 else
1545 pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001546
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001547 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001548}
1549
1550/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001551 * _lookup_hardreset - fill register bit info for this hwmod/reset line
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001552 * @oh: struct omap_hwmod *
1553 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001554 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001555 *
1556 * Return the bit position of the reset line that match the
1557 * input name. Return -ENOENT if not found.
1558 */
Paul Walmsleya032d332012-08-03 09:21:10 -06001559static int _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1560 struct omap_hwmod_rst_info *ohri)
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001561{
1562 int i;
1563
1564 for (i = 0; i < oh->rst_lines_cnt; i++) {
1565 const char *rst_line = oh->rst_lines[i].name;
1566 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001567 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1568 ohri->st_shift = oh->rst_lines[i].st_shift;
1569 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1570 oh->name, __func__, rst_line, ohri->rst_shift,
1571 ohri->st_shift);
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001572
omar ramirezcc1226e2011-03-04 13:32:44 -07001573 return 0;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001574 }
1575 }
1576
1577 return -ENOENT;
1578}
1579
1580/**
1581 * _assert_hardreset - assert the HW reset line of submodules
1582 * contained in the hwmod module.
1583 * @oh: struct omap_hwmod *
1584 * @name: name of the reset line to lookup and assert
1585 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001586 * Some IP like dsp, ipu or iva contain processor that require an HW
1587 * reset line to be assert / deassert in order to enable fully the IP.
1588 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1589 * asserting the hardreset line on the currently-booted SoC, or passes
1590 * along the return value from _lookup_hardreset() or the SoC's
1591 * assert_hardreset code.
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001592 */
1593static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1594{
omar ramirezcc1226e2011-03-04 13:32:44 -07001595 struct omap_hwmod_rst_info ohri;
Paul Walmsleya032d332012-08-03 09:21:10 -06001596 int ret = -EINVAL;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001597
1598 if (!oh)
1599 return -EINVAL;
1600
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001601 if (!soc_ops.assert_hardreset)
1602 return -ENOSYS;
1603
omar ramirezcc1226e2011-03-04 13:32:44 -07001604 ret = _lookup_hardreset(oh, name, &ohri);
Paul Walmsleya032d332012-08-03 09:21:10 -06001605 if (ret < 0)
omar ramirezcc1226e2011-03-04 13:32:44 -07001606 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001607
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001608 ret = soc_ops.assert_hardreset(oh, &ohri);
1609
1610 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001611}
1612
1613/**
1614 * _deassert_hardreset - deassert the HW reset line of submodules contained
1615 * in the hwmod module.
1616 * @oh: struct omap_hwmod *
1617 * @name: name of the reset line to look up and deassert
1618 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001619 * Some IP like dsp, ipu or iva contain processor that require an HW
1620 * reset line to be assert / deassert in order to enable fully the IP.
1621 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1622 * deasserting the hardreset line on the currently-booted SoC, or passes
1623 * along the return value from _lookup_hardreset() or the SoC's
1624 * deassert_hardreset code.
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001625 */
1626static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1627{
omar ramirezcc1226e2011-03-04 13:32:44 -07001628 struct omap_hwmod_rst_info ohri;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001629 int ret = -EINVAL;
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001630 int hwsup = 0;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001631
1632 if (!oh)
1633 return -EINVAL;
1634
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001635 if (!soc_ops.deassert_hardreset)
1636 return -ENOSYS;
1637
omar ramirezcc1226e2011-03-04 13:32:44 -07001638 ret = _lookup_hardreset(oh, name, &ohri);
1639 if (IS_ERR_VALUE(ret))
1640 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001641
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001642 if (oh->clkdm) {
1643 /*
1644 * A clockdomain must be in SW_SUP otherwise reset
1645 * might not be completed. The clockdomain can be set
1646 * in HW_AUTO only when the module become ready.
1647 */
1648 hwsup = clkdm_in_hwsup(oh->clkdm);
1649 ret = clkdm_hwmod_enable(oh->clkdm, oh);
1650 if (ret) {
1651 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1652 oh->name, oh->clkdm->name, ret);
1653 return ret;
1654 }
1655 }
1656
1657 _enable_clocks(oh);
1658 if (soc_ops.enable_module)
1659 soc_ops.enable_module(oh);
1660
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001661 ret = soc_ops.deassert_hardreset(oh, &ohri);
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001662
1663 if (soc_ops.disable_module)
1664 soc_ops.disable_module(oh);
1665 _disable_clocks(oh);
1666
omar ramirezcc1226e2011-03-04 13:32:44 -07001667 if (ret == -EBUSY)
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001668 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1669
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001670 if (!ret) {
1671 /*
1672 * Set the clockdomain to HW_AUTO, assuming that the
1673 * previous state was HW_AUTO.
1674 */
1675 if (oh->clkdm && hwsup)
1676 clkdm_allow_idle(oh->clkdm);
1677 } else {
1678 if (oh->clkdm)
1679 clkdm_hwmod_disable(oh->clkdm, oh);
1680 }
1681
omar ramirezcc1226e2011-03-04 13:32:44 -07001682 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001683}
1684
1685/**
1686 * _read_hardreset - read the HW reset line state of submodules
1687 * contained in the hwmod module
1688 * @oh: struct omap_hwmod *
1689 * @name: name of the reset line to look up and read
1690 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001691 * Return the state of the reset line. Returns -EINVAL if @oh is
1692 * null, -ENOSYS if we have no way of reading the hardreset line
1693 * status on the currently-booted SoC, or passes along the return
1694 * value from _lookup_hardreset() or the SoC's is_hardreset_asserted
1695 * code.
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001696 */
1697static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1698{
omar ramirezcc1226e2011-03-04 13:32:44 -07001699 struct omap_hwmod_rst_info ohri;
Paul Walmsleya032d332012-08-03 09:21:10 -06001700 int ret = -EINVAL;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001701
1702 if (!oh)
1703 return -EINVAL;
1704
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001705 if (!soc_ops.is_hardreset_asserted)
1706 return -ENOSYS;
1707
omar ramirezcc1226e2011-03-04 13:32:44 -07001708 ret = _lookup_hardreset(oh, name, &ohri);
Paul Walmsleya032d332012-08-03 09:21:10 -06001709 if (ret < 0)
omar ramirezcc1226e2011-03-04 13:32:44 -07001710 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001711
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001712 return soc_ops.is_hardreset_asserted(oh, &ohri);
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001713}
1714
1715/**
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001716 * _are_all_hardreset_lines_asserted - return true if the @oh is hard-reset
Paul Walmsley747834a2012-04-18 19:10:04 -06001717 * @oh: struct omap_hwmod *
1718 *
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001719 * If all hardreset lines associated with @oh are asserted, then return true.
1720 * Otherwise, if part of @oh is out hardreset or if no hardreset lines
1721 * associated with @oh are asserted, then return false.
Paul Walmsley747834a2012-04-18 19:10:04 -06001722 * This function is used to avoid executing some parts of the IP block
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001723 * enable/disable sequence if its hardreset line is set.
Paul Walmsley747834a2012-04-18 19:10:04 -06001724 */
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001725static bool _are_all_hardreset_lines_asserted(struct omap_hwmod *oh)
Paul Walmsley747834a2012-04-18 19:10:04 -06001726{
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001727 int i, rst_cnt = 0;
Paul Walmsley747834a2012-04-18 19:10:04 -06001728
1729 if (oh->rst_lines_cnt == 0)
1730 return false;
1731
1732 for (i = 0; i < oh->rst_lines_cnt; i++)
1733 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001734 rst_cnt++;
1735
1736 if (oh->rst_lines_cnt == rst_cnt)
1737 return true;
Paul Walmsley747834a2012-04-18 19:10:04 -06001738
1739 return false;
1740}
1741
1742/**
Paul Walmsleye9332b62012-10-08 23:08:15 -06001743 * _are_any_hardreset_lines_asserted - return true if any part of @oh is
1744 * hard-reset
1745 * @oh: struct omap_hwmod *
1746 *
1747 * If any hardreset lines associated with @oh are asserted, then
1748 * return true. Otherwise, if no hardreset lines associated with @oh
1749 * are asserted, or if @oh has no hardreset lines, then return false.
1750 * This function is used to avoid executing some parts of the IP block
1751 * enable/disable sequence if any hardreset line is set.
1752 */
1753static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1754{
1755 int rst_cnt = 0;
1756 int i;
1757
1758 for (i = 0; i < oh->rst_lines_cnt && rst_cnt == 0; i++)
1759 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1760 rst_cnt++;
1761
1762 return (rst_cnt) ? true : false;
1763}
1764
1765/**
Paul Walmsley747834a2012-04-18 19:10:04 -06001766 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1767 * @oh: struct omap_hwmod *
1768 *
1769 * Disable the PRCM module mode related to the hwmod @oh.
1770 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1771 */
1772static int _omap4_disable_module(struct omap_hwmod *oh)
1773{
1774 int v;
1775
Paul Walmsley747834a2012-04-18 19:10:04 -06001776 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1777 return -EINVAL;
1778
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001779 /*
1780 * Since integration code might still be doing something, only
1781 * disable if all lines are under hardreset.
1782 */
Paul Walmsleye9332b62012-10-08 23:08:15 -06001783 if (_are_any_hardreset_lines_asserted(oh))
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001784 return 0;
1785
Paul Walmsley747834a2012-04-18 19:10:04 -06001786 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1787
1788 omap4_cminst_module_disable(oh->clkdm->prcm_partition,
1789 oh->clkdm->cm_inst,
1790 oh->clkdm->clkdm_offs,
1791 oh->prcm.omap4.clkctrl_offs);
1792
Paul Walmsley747834a2012-04-18 19:10:04 -06001793 v = _omap4_wait_target_disable(oh);
1794 if (v)
1795 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1796 oh->name);
1797
1798 return 0;
1799}
1800
1801/**
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06001802 * _am33xx_disable_module - enable CLKCTRL modulemode on AM33XX
1803 * @oh: struct omap_hwmod *
1804 *
1805 * Disable the PRCM module mode related to the hwmod @oh.
1806 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1807 */
1808static int _am33xx_disable_module(struct omap_hwmod *oh)
1809{
1810 int v;
1811
1812 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1813 return -EINVAL;
1814
1815 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1816
Paul Walmsleye9332b62012-10-08 23:08:15 -06001817 if (_are_any_hardreset_lines_asserted(oh))
1818 return 0;
1819
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06001820 am33xx_cm_module_disable(oh->clkdm->cm_inst, oh->clkdm->clkdm_offs,
1821 oh->prcm.omap4.clkctrl_offs);
1822
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06001823 v = _am33xx_wait_target_disable(oh);
1824 if (v)
1825 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1826 oh->name);
1827
1828 return 0;
1829}
1830
1831/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001832 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001833 * @oh: struct omap_hwmod *
1834 *
1835 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Paul Walmsley30e105c2012-04-19 00:49:09 -06001836 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1837 * reset this way, -EINVAL if the hwmod is in the wrong state,
1838 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001839 *
1840 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001841 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001842 * use the SYSCONFIG softreset bit to provide the status.
1843 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001844 * Note that some IP like McBSP do have reset control but don't have
1845 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001846 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001847static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001848{
Tero Kristo613ad0e2012-10-29 22:02:13 -06001849 u32 v;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001850 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001851 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001852
Paul Walmsley43b40992010-02-22 22:09:34 -07001853 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001854 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley30e105c2012-04-19 00:49:09 -06001855 return -ENOENT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001856
1857 /* clocks must be on for this operation */
1858 if (oh->_state != _HWMOD_STATE_ENABLED) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001859 pr_warn("omap_hwmod: %s: reset can only be entered from enabled state\n",
1860 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001861 return -EINVAL;
1862 }
1863
Benoit Cousson96835af2010-09-21 18:57:58 +02001864 /* For some modules, all optionnal clocks need to be enabled as well */
1865 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1866 _enable_optional_clocks(oh);
1867
Paul Walmsleybd361792010-12-14 12:42:35 -07001868 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001869
1870 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001871 ret = _set_softreset(oh, &v);
1872 if (ret)
1873 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001874 _write_sysconfig(v, oh);
1875
Fernando Guzman Lugod99de7f2012-04-13 05:08:03 -06001876 if (oh->class->sysc->srst_udelay)
1877 udelay(oh->class->sysc->srst_udelay);
1878
Tero Kristo613ad0e2012-10-29 22:02:13 -06001879 c = _wait_softreset_complete(oh);
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001880 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001881 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1882 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001883 else
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001884 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001885
1886 /*
1887 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1888 * _wait_target_ready() or _reset()
1889 */
1890
Benoit Cousson96835af2010-09-21 18:57:58 +02001891 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1892
1893dis_opt_clks:
1894 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1895 _disable_optional_clocks(oh);
1896
1897 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001898}
1899
1900/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001901 * _reset - reset an omap_hwmod
1902 * @oh: struct omap_hwmod *
1903 *
Paul Walmsley30e105c2012-04-19 00:49:09 -06001904 * Resets an omap_hwmod @oh. If the module has a custom reset
1905 * function pointer defined, then call it to reset the IP block, and
1906 * pass along its return value to the caller. Otherwise, if the IP
1907 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1908 * associated with it, call a function to reset the IP block via that
1909 * method, and pass along the return value to the caller. Finally, if
1910 * the IP block has some hardreset lines associated with it, assert
1911 * all of those, but do _not_ deassert them. (This is because driver
1912 * authors have expressed an apparent requirement to control the
1913 * deassertion of the hardreset lines themselves.)
1914 *
1915 * The default software reset mechanism for most OMAP IP blocks is
1916 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1917 * hwmods cannot be reset via this method. Some are not targets and
1918 * therefore have no OCP header registers to access. Others (like the
1919 * IVA) have idiosyncratic reset sequences. So for these relatively
1920 * rare cases, custom reset code can be supplied in the struct
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -06001921 * omap_hwmod_class .reset function pointer.
1922 *
1923 * _set_dmadisable() is called to set the DMADISABLE bit so that it
1924 * does not prevent idling of the system. This is necessary for cases
1925 * where ROMCODE/BOOTLOADER uses dma and transfers control to the
1926 * kernel without disabling dma.
1927 *
1928 * Passes along the return value from either _ocp_softreset() or the
1929 * custom reset function - these must return -EINVAL if the hwmod
1930 * cannot be reset this way or if the hwmod is in the wrong state,
1931 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Paul Walmsleybd361792010-12-14 12:42:35 -07001932 */
1933static int _reset(struct omap_hwmod *oh)
1934{
Paul Walmsley30e105c2012-04-19 00:49:09 -06001935 int i, r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001936
1937 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1938
Paul Walmsley30e105c2012-04-19 00:49:09 -06001939 if (oh->class->reset) {
1940 r = oh->class->reset(oh);
1941 } else {
1942 if (oh->rst_lines_cnt > 0) {
1943 for (i = 0; i < oh->rst_lines_cnt; i++)
1944 _assert_hardreset(oh, oh->rst_lines[i].name);
1945 return 0;
1946 } else {
1947 r = _ocp_softreset(oh);
1948 if (r == -ENOENT)
1949 r = 0;
1950 }
1951 }
Paul Walmsleybd361792010-12-14 12:42:35 -07001952
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -06001953 _set_dmadisable(oh);
1954
Paul Walmsley30e105c2012-04-19 00:49:09 -06001955 /*
1956 * OCP_SYSCONFIG bits need to be reprogrammed after a
1957 * softreset. The _enable() function should be split to avoid
1958 * the rewrite of the OCP_SYSCONFIG register.
1959 */
Rajendra Nayak28008522012-03-13 22:55:23 +05301960 if (oh->class->sysc) {
1961 _update_sysc_cache(oh);
1962 _enable_sysc(oh);
1963 }
1964
Paul Walmsley30e105c2012-04-19 00:49:09 -06001965 return r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001966}
1967
1968/**
Vishwanath BS51658822012-06-22 08:40:04 -06001969 * _reconfigure_io_chain - clear any I/O chain wakeups and reconfigure chain
1970 *
1971 * Call the appropriate PRM function to clear any logged I/O chain
1972 * wakeups and to reconfigure the chain. This apparently needs to be
1973 * done upon every mux change. Since hwmods can be concurrently
1974 * enabled and idled, hold a spinlock around the I/O chain
1975 * reconfiguration sequence. No return value.
1976 *
1977 * XXX When the PRM code is moved to drivers, this function can be removed,
1978 * as the PRM infrastructure should abstract this.
1979 */
1980static void _reconfigure_io_chain(void)
1981{
1982 unsigned long flags;
1983
1984 spin_lock_irqsave(&io_chain_lock, flags);
1985
1986 if (cpu_is_omap34xx() && omap3_has_io_chain_ctrl())
1987 omap3xxx_prm_reconfigure_io_chain();
1988 else if (cpu_is_omap44xx())
1989 omap44xx_prm_reconfigure_io_chain();
1990
1991 spin_unlock_irqrestore(&io_chain_lock, flags);
1992}
1993
1994/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001995 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001996 * @oh: struct omap_hwmod *
1997 *
1998 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001999 * register target. Returns -EINVAL if the hwmod is in the wrong
2000 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03002001 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002002static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002003{
Paul Walmsley747834a2012-04-18 19:10:04 -06002004 int r;
Rajendra Nayak665d0012011-07-10 05:57:07 -06002005 int hwsup = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002006
Benoit Cousson34617e22011-07-01 22:54:07 +02002007 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
2008
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002009 /*
Paul Walmsley64813c32012-04-18 19:10:03 -06002010 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
2011 * state at init. Now that someone is really trying to enable
2012 * them, just ensure that the hwmod mux is set.
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002013 */
2014 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
2015 /*
2016 * If the caller has mux data populated, do the mux'ing
2017 * which wouldn't have been done as part of the _enable()
2018 * done during setup.
2019 */
2020 if (oh->mux)
2021 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
2022
2023 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
2024 return 0;
2025 }
2026
Paul Walmsley63c85232009-09-03 20:14:03 +03002027 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
2028 oh->_state != _HWMOD_STATE_IDLE &&
2029 oh->_state != _HWMOD_STATE_DISABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00002030 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
2031 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002032 return -EINVAL;
2033 }
2034
Benoit Cousson31f62862011-07-01 22:54:05 +02002035 /*
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06002036 * If an IP block contains HW reset lines and all of them are
Paul Walmsley747834a2012-04-18 19:10:04 -06002037 * asserted, we let integration code associated with that
2038 * block handle the enable. We've received very little
2039 * information on what those driver authors need, and until
2040 * detailed information is provided and the driver code is
2041 * posted to the public lists, this is probably the best we
2042 * can do.
Benoit Cousson31f62862011-07-01 22:54:05 +02002043 */
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06002044 if (_are_all_hardreset_lines_asserted(oh))
Paul Walmsley747834a2012-04-18 19:10:04 -06002045 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002046
Rajendra Nayak665d0012011-07-10 05:57:07 -06002047 /* Mux pins for device runtime if populated */
2048 if (oh->mux && (!oh->mux->enabled ||
2049 ((oh->_state == _HWMOD_STATE_IDLE) &&
Vishwanath BS51658822012-06-22 08:40:04 -06002050 oh->mux->pads_dynamic))) {
Rajendra Nayak665d0012011-07-10 05:57:07 -06002051 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
Vishwanath BS51658822012-06-22 08:40:04 -06002052 _reconfigure_io_chain();
2053 }
Benoit Cousson34617e22011-07-01 22:54:07 +02002054
Rajendra Nayak665d0012011-07-10 05:57:07 -06002055 _add_initiator_dep(oh, mpu_oh);
2056
2057 if (oh->clkdm) {
2058 /*
2059 * A clockdomain must be in SW_SUP before enabling
2060 * completely the module. The clockdomain can be set
2061 * in HW_AUTO only when the module become ready.
2062 */
Paul Walmsleyb71c7212012-09-23 17:28:28 -06002063 hwsup = clkdm_in_hwsup(oh->clkdm) &&
2064 !clkdm_missing_idle_reporting(oh->clkdm);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002065 r = clkdm_hwmod_enable(oh->clkdm, oh);
2066 if (r) {
2067 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
2068 oh->name, oh->clkdm->name, r);
2069 return r;
2070 }
Benoit Cousson34617e22011-07-01 22:54:07 +02002071 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06002072
2073 _enable_clocks(oh);
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002074 if (soc_ops.enable_module)
2075 soc_ops.enable_module(oh);
Benoit Cousson34617e22011-07-01 22:54:07 +02002076
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002077 r = (soc_ops.wait_target_ready) ? soc_ops.wait_target_ready(oh) :
2078 -EINVAL;
Rajendra Nayak665d0012011-07-10 05:57:07 -06002079 if (!r) {
2080 /*
2081 * Set the clockdomain to HW_AUTO only if the target is ready,
2082 * assuming that the previous state was HW_AUTO
2083 */
2084 if (oh->clkdm && hwsup)
2085 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02002086
Rajendra Nayak665d0012011-07-10 05:57:07 -06002087 oh->_state = _HWMOD_STATE_ENABLED;
2088
2089 /* Access the sysconfig only if the target is ready */
2090 if (oh->class->sysc) {
2091 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
2092 _update_sysc_cache(oh);
2093 _enable_sysc(oh);
2094 }
2095 } else {
Paul Walmsley2577a4a2012-10-29 20:57:55 -06002096 if (soc_ops.disable_module)
2097 soc_ops.disable_module(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002098 _disable_clocks(oh);
2099 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
2100 oh->name, r);
2101
2102 if (oh->clkdm)
2103 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002104 }
2105
Paul Walmsley63c85232009-09-03 20:14:03 +03002106 return r;
2107}
2108
2109/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002110 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03002111 * @oh: struct omap_hwmod *
2112 *
2113 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002114 * no further work. Returns -EINVAL if the hwmod is in the wrong
2115 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03002116 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002117static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002118{
Benoit Cousson34617e22011-07-01 22:54:07 +02002119 pr_debug("omap_hwmod: %s: idling\n", oh->name);
2120
Paul Walmsley63c85232009-09-03 20:14:03 +03002121 if (oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00002122 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
2123 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002124 return -EINVAL;
2125 }
2126
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06002127 if (_are_all_hardreset_lines_asserted(oh))
Paul Walmsley747834a2012-04-18 19:10:04 -06002128 return 0;
2129
Paul Walmsley43b40992010-02-22 22:09:34 -07002130 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002131 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002132 _del_initiator_dep(oh, mpu_oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08002133
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002134 if (soc_ops.disable_module)
2135 soc_ops.disable_module(oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08002136
Benoit Cousson45c38252011-07-10 05:56:33 -06002137 /*
2138 * The module must be in idle mode before disabling any parents
2139 * clocks. Otherwise, the parent clock might be disabled before
2140 * the module transition is done, and thus will prevent the
2141 * transition to complete properly.
2142 */
2143 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002144 if (oh->clkdm)
2145 clkdm_hwmod_disable(oh->clkdm, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002146
Tony Lindgren8d9af882010-12-22 18:42:35 -08002147 /* Mux pins for device idle if populated */
Vishwanath BS51658822012-06-22 08:40:04 -06002148 if (oh->mux && oh->mux->pads_dynamic) {
Tony Lindgren8d9af882010-12-22 18:42:35 -08002149 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
Vishwanath BS51658822012-06-22 08:40:04 -06002150 _reconfigure_io_chain();
2151 }
Tony Lindgren8d9af882010-12-22 18:42:35 -08002152
Paul Walmsley63c85232009-09-03 20:14:03 +03002153 oh->_state = _HWMOD_STATE_IDLE;
2154
2155 return 0;
2156}
2157
2158/**
Kishon Vijay Abraham I95992172011-03-10 03:50:08 -07002159 * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
2160 * @oh: struct omap_hwmod *
2161 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
2162 *
2163 * Sets the IP block's OCP autoidle bit in hardware, and updates our
2164 * local copy. Intended to be used by drivers that require
2165 * direct manipulation of the AUTOIDLE bits.
2166 * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
2167 * along the return value from _set_module_autoidle().
2168 *
2169 * Any users of this function should be scrutinized carefully.
2170 */
2171int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
2172{
2173 u32 v;
2174 int retval = 0;
2175 unsigned long flags;
2176
2177 if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
2178 return -EINVAL;
2179
2180 spin_lock_irqsave(&oh->_lock, flags);
2181
2182 v = oh->_sysc_cache;
2183
2184 retval = _set_module_autoidle(oh, autoidle, &v);
2185
2186 if (!retval)
2187 _write_sysconfig(v, oh);
2188
2189 spin_unlock_irqrestore(&oh->_lock, flags);
2190
2191 return retval;
2192}
2193
2194/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002195 * _shutdown - shutdown an omap_hwmod
2196 * @oh: struct omap_hwmod *
2197 *
2198 * Shut down an omap_hwmod @oh. This should be called when the driver
2199 * used for the hwmod is removed or unloaded or if the driver is not
2200 * used by the system. Returns -EINVAL if the hwmod is in the wrong
2201 * state or returns 0.
2202 */
2203static int _shutdown(struct omap_hwmod *oh)
2204{
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06002205 int ret, i;
Paul Walmsleye4dc8f502010-12-14 12:42:34 -07002206 u8 prev_state;
2207
Paul Walmsley63c85232009-09-03 20:14:03 +03002208 if (oh->_state != _HWMOD_STATE_IDLE &&
2209 oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00002210 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
2211 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002212 return -EINVAL;
2213 }
2214
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06002215 if (_are_all_hardreset_lines_asserted(oh))
Paul Walmsley747834a2012-04-18 19:10:04 -06002216 return 0;
2217
Paul Walmsley63c85232009-09-03 20:14:03 +03002218 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
2219
Paul Walmsleye4dc8f502010-12-14 12:42:34 -07002220 if (oh->class->pre_shutdown) {
2221 prev_state = oh->_state;
2222 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002223 _enable(oh);
Paul Walmsleye4dc8f502010-12-14 12:42:34 -07002224 ret = oh->class->pre_shutdown(oh);
2225 if (ret) {
2226 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002227 _idle(oh);
Paul Walmsleye4dc8f502010-12-14 12:42:34 -07002228 return ret;
2229 }
2230 }
2231
Miguel Vadillo6481c732011-07-01 22:54:02 +02002232 if (oh->class->sysc) {
2233 if (oh->_state == _HWMOD_STATE_IDLE)
2234 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002235 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02002236 }
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06002237
Benoit Cousson3827f942010-09-21 10:34:08 -06002238 /* clocks and deps are already disabled in idle */
2239 if (oh->_state == _HWMOD_STATE_ENABLED) {
2240 _del_initiator_dep(oh, mpu_oh);
2241 /* XXX what about the other system initiators here? dma, dsp */
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002242 if (soc_ops.disable_module)
2243 soc_ops.disable_module(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06002244 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002245 if (oh->clkdm)
2246 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06002247 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002248 /* XXX Should this code also force-disable the optional clocks? */
2249
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06002250 for (i = 0; i < oh->rst_lines_cnt; i++)
2251 _assert_hardreset(oh, oh->rst_lines[i].name);
Benoit Cousson31f62862011-07-01 22:54:05 +02002252
Tony Lindgren8d9af882010-12-22 18:42:35 -08002253 /* Mux pins to safe mode or use populated off mode values */
2254 if (oh->mux)
2255 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03002256
2257 oh->_state = _HWMOD_STATE_DISABLED;
2258
2259 return 0;
2260}
2261
2262/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002263 * _init_mpu_rt_base - populate the virtual address for a hwmod
2264 * @oh: struct omap_hwmod * to locate the virtual address
2265 *
2266 * Cache the virtual address used by the MPU to access this IP block's
2267 * registers. This address is needed early so the OCP registers that
2268 * are part of the device's address space can be ioremapped properly.
2269 * No return value.
2270 */
2271static void __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data)
2272{
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002273 struct omap_hwmod_addr_space *mem;
2274 void __iomem *va_start;
2275
2276 if (!oh)
2277 return;
2278
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002279 _save_mpu_port_index(oh);
2280
Paul Walmsley381d0332012-04-19 00:58:22 -06002281 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2282 return;
2283
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002284 mem = _find_mpu_rt_addr_space(oh);
2285 if (!mem) {
2286 pr_debug("omap_hwmod: %s: no MPU register target found\n",
2287 oh->name);
2288 return;
2289 }
2290
2291 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
2292 if (!va_start) {
2293 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
2294 return;
2295 }
2296
2297 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
2298 oh->name, va_start);
2299
2300 oh->_mpu_rt_va = va_start;
Paul Walmsley381d0332012-04-19 00:58:22 -06002301}
2302
2303/**
2304 * _init - initialize internal data for the hwmod @oh
2305 * @oh: struct omap_hwmod *
2306 * @n: (unused)
2307 *
2308 * Look up the clocks and the address space used by the MPU to access
2309 * registers belonging to the hwmod @oh. @oh must already be
2310 * registered at this point. This is the first of two phases for
2311 * hwmod initialization. Code called here does not touch any hardware
2312 * registers, it simply prepares internal data structures. Returns 0
2313 * upon success or if the hwmod isn't registered, or -EINVAL upon
2314 * failure.
2315 */
2316static int __init _init(struct omap_hwmod *oh, void *data)
2317{
2318 int r;
2319
2320 if (oh->_state != _HWMOD_STATE_REGISTERED)
2321 return 0;
2322
2323 _init_mpu_rt_base(oh, NULL);
2324
2325 r = _init_clocks(oh, NULL);
2326 if (IS_ERR_VALUE(r)) {
2327 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2328 return -EINVAL;
2329 }
2330
2331 oh->_state = _HWMOD_STATE_INITIALIZED;
2332
2333 return 0;
2334}
2335
2336/**
Paul Walmsley64813c32012-04-18 19:10:03 -06002337 * _setup_iclk_autoidle - configure an IP block's interface clocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002338 * @oh: struct omap_hwmod *
2339 *
Paul Walmsley64813c32012-04-18 19:10:03 -06002340 * Set up the module's interface clocks. XXX This function is still mostly
2341 * a stub; implementing this properly requires iclk autoidle usecounting in
2342 * the clock code. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03002343 */
Paul Walmsley64813c32012-04-18 19:10:03 -06002344static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002345{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002346 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002347 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002348 int i = 0;
Paul Walmsley381d0332012-04-19 00:58:22 -06002349 if (oh->_state != _HWMOD_STATE_INITIALIZED)
Paul Walmsley64813c32012-04-18 19:10:03 -06002350 return;
Paul Walmsley48d54f32011-02-23 00:14:07 -07002351
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002352 p = oh->slave_ports.next;
Paul Walmsley63c85232009-09-03 20:14:03 +03002353
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002354 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002355 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002356 if (!os->_clk)
Paul Walmsley64813c32012-04-18 19:10:03 -06002357 continue;
Paul Walmsley63c85232009-09-03 20:14:03 +03002358
Paul Walmsley64813c32012-04-18 19:10:03 -06002359 if (os->flags & OCPIF_SWSUP_IDLE) {
2360 /* XXX omap_iclk_deny_idle(c); */
2361 } else {
2362 /* XXX omap_iclk_allow_idle(c); */
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002363 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +03002364 }
2365 }
2366
Paul Walmsley64813c32012-04-18 19:10:03 -06002367 return;
2368}
2369
2370/**
2371 * _setup_reset - reset an IP block during the setup process
2372 * @oh: struct omap_hwmod *
2373 *
2374 * Reset the IP block corresponding to the hwmod @oh during the setup
2375 * process. The IP block is first enabled so it can be successfully
2376 * reset. Returns 0 upon success or a negative error code upon
2377 * failure.
2378 */
2379static int __init _setup_reset(struct omap_hwmod *oh)
2380{
2381 int r;
2382
2383 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2384 return -EINVAL;
Paul Walmsley63c85232009-09-03 20:14:03 +03002385
Paul Walmsley5fb3d522012-10-29 22:11:50 -06002386 if (oh->flags & HWMOD_EXT_OPT_MAIN_CLK)
2387 return -EPERM;
2388
Paul Walmsley747834a2012-04-18 19:10:04 -06002389 if (oh->rst_lines_cnt == 0) {
2390 r = _enable(oh);
2391 if (r) {
2392 pr_warning("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2393 oh->name, oh->_state);
2394 return -EINVAL;
2395 }
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002396 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002397
Rajendra Nayak28008522012-03-13 22:55:23 +05302398 if (!(oh->flags & HWMOD_INIT_NO_RESET))
Paul Walmsley64813c32012-04-18 19:10:03 -06002399 r = _reset(oh);
2400
2401 return r;
2402}
2403
2404/**
2405 * _setup_postsetup - transition to the appropriate state after _setup
2406 * @oh: struct omap_hwmod *
2407 *
2408 * Place an IP block represented by @oh into a "post-setup" state --
2409 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2410 * this function is called at the end of _setup().) The postsetup
2411 * state for an IP block can be changed by calling
2412 * omap_hwmod_enter_postsetup_state() early in the boot process,
2413 * before one of the omap_hwmod_setup*() functions are called for the
2414 * IP block.
2415 *
2416 * The IP block stays in this state until a PM runtime-based driver is
2417 * loaded for that IP block. A post-setup state of IDLE is
2418 * appropriate for almost all IP blocks with runtime PM-enabled
2419 * drivers, since those drivers are able to enable the IP block. A
2420 * post-setup state of ENABLED is appropriate for kernels with PM
2421 * runtime disabled. The DISABLED state is appropriate for unusual IP
2422 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2423 * included, since the WDTIMER starts running on reset and will reset
2424 * the MPU if left active.
2425 *
2426 * This post-setup mechanism is deprecated. Once all of the OMAP
2427 * drivers have been converted to use PM runtime, and all of the IP
2428 * block data and interconnect data is available to the hwmod code, it
2429 * should be possible to replace this mechanism with a "lazy reset"
2430 * arrangement. In a "lazy reset" setup, each IP block is enabled
2431 * when the driver first probes, then all remaining IP blocks without
2432 * drivers are either shut down or enabled after the drivers have
2433 * loaded. However, this cannot take place until the above
2434 * preconditions have been met, since otherwise the late reset code
2435 * has no way of knowing which IP blocks are in use by drivers, and
2436 * which ones are unused.
2437 *
2438 * No return value.
2439 */
2440static void __init _setup_postsetup(struct omap_hwmod *oh)
2441{
2442 u8 postsetup_state;
2443
2444 if (oh->rst_lines_cnt > 0)
2445 return;
Benoit Cousson76e55892010-09-21 10:34:11 -06002446
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002447 postsetup_state = oh->_postsetup_state;
2448 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2449 postsetup_state = _HWMOD_STATE_ENABLED;
2450
2451 /*
2452 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2453 * it should be set by the core code as a runtime flag during startup
2454 */
2455 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002456 (postsetup_state == _HWMOD_STATE_IDLE)) {
2457 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002458 postsetup_state = _HWMOD_STATE_ENABLED;
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002459 }
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002460
2461 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002462 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002463 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2464 _shutdown(oh);
2465 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2466 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2467 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03002468
Paul Walmsley64813c32012-04-18 19:10:03 -06002469 return;
2470}
2471
2472/**
2473 * _setup - prepare IP block hardware for use
2474 * @oh: struct omap_hwmod *
2475 * @n: (unused, pass NULL)
2476 *
2477 * Configure the IP block represented by @oh. This may include
2478 * enabling the IP block, resetting it, and placing it into a
2479 * post-setup state, depending on the type of IP block and applicable
2480 * flags. IP blocks are reset to prevent any previous configuration
2481 * by the bootloader or previous operating system from interfering
2482 * with power management or other parts of the system. The reset can
2483 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2484 * two phases for hwmod initialization. Code called here generally
2485 * affects the IP block hardware, or system integration hardware
2486 * associated with the IP block. Returns 0.
2487 */
2488static int __init _setup(struct omap_hwmod *oh, void *data)
2489{
2490 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2491 return 0;
2492
2493 _setup_iclk_autoidle(oh);
2494
2495 if (!_setup_reset(oh))
2496 _setup_postsetup(oh);
2497
Paul Walmsley63c85232009-09-03 20:14:03 +03002498 return 0;
2499}
2500
Benoit Cousson0102b622010-12-21 21:31:27 -07002501/**
2502 * _register - register a struct omap_hwmod
2503 * @oh: struct omap_hwmod *
2504 *
2505 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2506 * already has been registered by the same name; -EINVAL if the
2507 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2508 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2509 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2510 * success.
2511 *
2512 * XXX The data should be copied into bootmem, so the original data
2513 * should be marked __initdata and freed after init. This would allow
2514 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2515 * that the copy process would be relatively complex due to the large number
2516 * of substructures.
2517 */
Benoit Cousson01592df2010-12-21 21:31:28 -07002518static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07002519{
Benoit Cousson0102b622010-12-21 21:31:27 -07002520 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2521 (oh->_state != _HWMOD_STATE_UNKNOWN))
2522 return -EINVAL;
2523
Benoit Cousson0102b622010-12-21 21:31:27 -07002524 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2525
Benoit Coussonce35b242010-12-21 21:31:28 -07002526 if (_lookup(oh->name))
2527 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07002528
Benoit Cousson0102b622010-12-21 21:31:27 -07002529 list_add_tail(&oh->node, &omap_hwmod_list);
2530
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002531 INIT_LIST_HEAD(&oh->master_ports);
2532 INIT_LIST_HEAD(&oh->slave_ports);
Benoit Cousson0102b622010-12-21 21:31:27 -07002533 spin_lock_init(&oh->_lock);
2534
2535 oh->_state = _HWMOD_STATE_REGISTERED;
2536
Paul Walmsley569edd72011-02-23 00:14:06 -07002537 /*
2538 * XXX Rather than doing a strcmp(), this should test a flag
2539 * set in the hwmod data, inserted by the autogenerator code.
2540 */
2541 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2542 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07002543
Paul Walmsley569edd72011-02-23 00:14:06 -07002544 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07002545}
Paul Walmsley63c85232009-09-03 20:14:03 +03002546
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002547/**
2548 * _alloc_links - return allocated memory for hwmod links
2549 * @ml: pointer to a struct omap_hwmod_link * for the master link
2550 * @sl: pointer to a struct omap_hwmod_link * for the slave link
2551 *
2552 * Return pointers to two struct omap_hwmod_link records, via the
2553 * addresses pointed to by @ml and @sl. Will first attempt to return
2554 * memory allocated as part of a large initial block, but if that has
2555 * been exhausted, will allocate memory itself. Since ideally this
2556 * second allocation path will never occur, the number of these
2557 * 'supplemental' allocations will be logged when debugging is
2558 * enabled. Returns 0.
2559 */
2560static int __init _alloc_links(struct omap_hwmod_link **ml,
2561 struct omap_hwmod_link **sl)
2562{
2563 unsigned int sz;
2564
2565 if ((free_ls + LINKS_PER_OCP_IF) <= max_ls) {
2566 *ml = &linkspace[free_ls++];
2567 *sl = &linkspace[free_ls++];
2568 return 0;
2569 }
2570
2571 sz = sizeof(struct omap_hwmod_link) * LINKS_PER_OCP_IF;
2572
2573 *sl = NULL;
2574 *ml = alloc_bootmem(sz);
2575
2576 memset(*ml, 0, sz);
2577
2578 *sl = (void *)(*ml) + sizeof(struct omap_hwmod_link);
2579
2580 ls_supp++;
2581 pr_debug("omap_hwmod: supplemental link allocations needed: %d\n",
2582 ls_supp * LINKS_PER_OCP_IF);
2583
2584 return 0;
2585};
2586
2587/**
2588 * _add_link - add an interconnect between two IP blocks
2589 * @oi: pointer to a struct omap_hwmod_ocp_if record
2590 *
2591 * Add struct omap_hwmod_link records connecting the master IP block
2592 * specified in @oi->master to @oi, and connecting the slave IP block
2593 * specified in @oi->slave to @oi. This code is assumed to run before
2594 * preemption or SMP has been enabled, thus avoiding the need for
2595 * locking in this code. Changes to this assumption will require
2596 * additional locking. Returns 0.
2597 */
2598static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2599{
2600 struct omap_hwmod_link *ml, *sl;
2601
2602 pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2603 oi->slave->name);
2604
2605 _alloc_links(&ml, &sl);
2606
2607 ml->ocp_if = oi;
2608 INIT_LIST_HEAD(&ml->node);
2609 list_add(&ml->node, &oi->master->master_ports);
2610 oi->master->masters_cnt++;
2611
2612 sl->ocp_if = oi;
2613 INIT_LIST_HEAD(&sl->node);
2614 list_add(&sl->node, &oi->slave->slave_ports);
2615 oi->slave->slaves_cnt++;
2616
2617 return 0;
2618}
2619
2620/**
2621 * _register_link - register a struct omap_hwmod_ocp_if
2622 * @oi: struct omap_hwmod_ocp_if *
2623 *
2624 * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
2625 * has already been registered; -EINVAL if @oi is NULL or if the
2626 * record pointed to by @oi is missing required fields; or 0 upon
2627 * success.
2628 *
2629 * XXX The data should be copied into bootmem, so the original data
2630 * should be marked __initdata and freed after init. This would allow
2631 * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2632 */
2633static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2634{
2635 if (!oi || !oi->master || !oi->slave || !oi->user)
2636 return -EINVAL;
2637
2638 if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2639 return -EEXIST;
2640
2641 pr_debug("omap_hwmod: registering link from %s to %s\n",
2642 oi->master->name, oi->slave->name);
2643
2644 /*
2645 * Register the connected hwmods, if they haven't been
2646 * registered already
2647 */
2648 if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2649 _register(oi->master);
2650
2651 if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2652 _register(oi->slave);
2653
2654 _add_link(oi);
2655
2656 oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2657
2658 return 0;
2659}
2660
2661/**
2662 * _alloc_linkspace - allocate large block of hwmod links
2663 * @ois: pointer to an array of struct omap_hwmod_ocp_if records to count
2664 *
2665 * Allocate a large block of struct omap_hwmod_link records. This
2666 * improves boot time significantly by avoiding the need to allocate
2667 * individual records one by one. If the number of records to
2668 * allocate in the block hasn't been manually specified, this function
2669 * will count the number of struct omap_hwmod_ocp_if records in @ois
2670 * and use that to determine the allocation size. For SoC families
2671 * that require multiple list registrations, such as OMAP3xxx, this
2672 * estimation process isn't optimal, so manual estimation is advised
2673 * in those cases. Returns -EEXIST if the allocation has already occurred
2674 * or 0 upon success.
2675 */
2676static int __init _alloc_linkspace(struct omap_hwmod_ocp_if **ois)
2677{
2678 unsigned int i = 0;
2679 unsigned int sz;
2680
2681 if (linkspace) {
2682 WARN(1, "linkspace already allocated\n");
2683 return -EEXIST;
2684 }
2685
2686 if (max_ls == 0)
2687 while (ois[i++])
2688 max_ls += LINKS_PER_OCP_IF;
2689
2690 sz = sizeof(struct omap_hwmod_link) * max_ls;
2691
2692 pr_debug("omap_hwmod: %s: allocating %d byte linkspace (%d links)\n",
2693 __func__, sz, max_ls);
2694
2695 linkspace = alloc_bootmem(sz);
2696
2697 memset(linkspace, 0, sz);
2698
2699 return 0;
2700}
Paul Walmsley63c85232009-09-03 20:14:03 +03002701
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002702/* Static functions intended only for use in soc_ops field function pointers */
2703
2704/**
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -06002705 * _omap2xxx_wait_target_ready - wait for a module to leave slave idle
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002706 * @oh: struct omap_hwmod *
2707 *
2708 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2709 * does not have an IDLEST bit or if the module successfully leaves
2710 * slave idle; otherwise, pass along the return value of the
2711 * appropriate *_cm*_wait_module_ready() function.
2712 */
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -06002713static int _omap2xxx_wait_target_ready(struct omap_hwmod *oh)
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002714{
2715 if (!oh)
2716 return -EINVAL;
2717
2718 if (oh->flags & HWMOD_NO_IDLEST)
2719 return 0;
2720
2721 if (!_find_mpu_rt_port(oh))
2722 return 0;
2723
2724 /* XXX check module SIDLEMODE, hardreset status, enabled clocks */
2725
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -06002726 return omap2xxx_cm_wait_module_ready(oh->prcm.omap2.module_offs,
2727 oh->prcm.omap2.idlest_reg_id,
2728 oh->prcm.omap2.idlest_idle_bit);
2729}
2730
2731/**
2732 * _omap3xxx_wait_target_ready - wait for a module to leave slave idle
2733 * @oh: struct omap_hwmod *
2734 *
2735 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2736 * does not have an IDLEST bit or if the module successfully leaves
2737 * slave idle; otherwise, pass along the return value of the
2738 * appropriate *_cm*_wait_module_ready() function.
2739 */
2740static int _omap3xxx_wait_target_ready(struct omap_hwmod *oh)
2741{
2742 if (!oh)
2743 return -EINVAL;
2744
2745 if (oh->flags & HWMOD_NO_IDLEST)
2746 return 0;
2747
2748 if (!_find_mpu_rt_port(oh))
2749 return 0;
2750
2751 /* XXX check module SIDLEMODE, hardreset status, enabled clocks */
2752
2753 return omap3xxx_cm_wait_module_ready(oh->prcm.omap2.module_offs,
2754 oh->prcm.omap2.idlest_reg_id,
2755 oh->prcm.omap2.idlest_idle_bit);
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002756}
2757
2758/**
2759 * _omap4_wait_target_ready - wait for a module to leave slave idle
2760 * @oh: struct omap_hwmod *
2761 *
2762 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2763 * does not have an IDLEST bit or if the module successfully leaves
2764 * slave idle; otherwise, pass along the return value of the
2765 * appropriate *_cm*_wait_module_ready() function.
2766 */
2767static int _omap4_wait_target_ready(struct omap_hwmod *oh)
2768{
Paul Walmsley2b026d12012-09-23 17:28:18 -06002769 if (!oh)
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002770 return -EINVAL;
2771
Paul Walmsley2b026d12012-09-23 17:28:18 -06002772 if (oh->flags & HWMOD_NO_IDLEST || !oh->clkdm)
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002773 return 0;
2774
2775 if (!_find_mpu_rt_port(oh))
2776 return 0;
2777
2778 /* XXX check module SIDLEMODE, hardreset status */
2779
2780 return omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
2781 oh->clkdm->cm_inst,
2782 oh->clkdm->clkdm_offs,
2783 oh->prcm.omap4.clkctrl_offs);
2784}
2785
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002786/**
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06002787 * _am33xx_wait_target_ready - wait for a module to leave slave idle
2788 * @oh: struct omap_hwmod *
2789 *
2790 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2791 * does not have an IDLEST bit or if the module successfully leaves
2792 * slave idle; otherwise, pass along the return value of the
2793 * appropriate *_cm*_wait_module_ready() function.
2794 */
2795static int _am33xx_wait_target_ready(struct omap_hwmod *oh)
2796{
2797 if (!oh || !oh->clkdm)
2798 return -EINVAL;
2799
2800 if (oh->flags & HWMOD_NO_IDLEST)
2801 return 0;
2802
2803 if (!_find_mpu_rt_port(oh))
2804 return 0;
2805
2806 /* XXX check module SIDLEMODE, hardreset status */
2807
2808 return am33xx_cm_wait_module_ready(oh->clkdm->cm_inst,
2809 oh->clkdm->clkdm_offs,
2810 oh->prcm.omap4.clkctrl_offs);
2811}
2812
2813/**
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002814 * _omap2_assert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2815 * @oh: struct omap_hwmod * to assert hardreset
2816 * @ohri: hardreset line data
2817 *
2818 * Call omap2_prm_assert_hardreset() with parameters extracted from
2819 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2820 * use as an soc_ops function pointer. Passes along the return value
2821 * from omap2_prm_assert_hardreset(). XXX This function is scheduled
2822 * for removal when the PRM code is moved into drivers/.
2823 */
2824static int _omap2_assert_hardreset(struct omap_hwmod *oh,
2825 struct omap_hwmod_rst_info *ohri)
2826{
2827 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
2828 ohri->rst_shift);
2829}
2830
2831/**
2832 * _omap2_deassert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2833 * @oh: struct omap_hwmod * to deassert hardreset
2834 * @ohri: hardreset line data
2835 *
2836 * Call omap2_prm_deassert_hardreset() with parameters extracted from
2837 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2838 * use as an soc_ops function pointer. Passes along the return value
2839 * from omap2_prm_deassert_hardreset(). XXX This function is
2840 * scheduled for removal when the PRM code is moved into drivers/.
2841 */
2842static int _omap2_deassert_hardreset(struct omap_hwmod *oh,
2843 struct omap_hwmod_rst_info *ohri)
2844{
2845 return omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
2846 ohri->rst_shift,
2847 ohri->st_shift);
2848}
2849
2850/**
2851 * _omap2_is_hardreset_asserted - call OMAP2 PRM hardreset fn with hwmod args
2852 * @oh: struct omap_hwmod * to test hardreset
2853 * @ohri: hardreset line data
2854 *
2855 * Call omap2_prm_is_hardreset_asserted() with parameters extracted
2856 * from the hwmod @oh and the hardreset line data @ohri. Only
2857 * intended for use as an soc_ops function pointer. Passes along the
2858 * return value from omap2_prm_is_hardreset_asserted(). XXX This
2859 * function is scheduled for removal when the PRM code is moved into
2860 * drivers/.
2861 */
2862static int _omap2_is_hardreset_asserted(struct omap_hwmod *oh,
2863 struct omap_hwmod_rst_info *ohri)
2864{
2865 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
2866 ohri->st_shift);
2867}
2868
2869/**
2870 * _omap4_assert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2871 * @oh: struct omap_hwmod * to assert hardreset
2872 * @ohri: hardreset line data
2873 *
2874 * Call omap4_prminst_assert_hardreset() with parameters extracted
2875 * from the hwmod @oh and the hardreset line data @ohri. Only
2876 * intended for use as an soc_ops function pointer. Passes along the
2877 * return value from omap4_prminst_assert_hardreset(). XXX This
2878 * function is scheduled for removal when the PRM code is moved into
2879 * drivers/.
2880 */
2881static int _omap4_assert_hardreset(struct omap_hwmod *oh,
2882 struct omap_hwmod_rst_info *ohri)
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002883{
Paul Walmsley07b3a132012-06-20 20:11:36 -06002884 if (!oh->clkdm)
2885 return -EINVAL;
2886
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002887 return omap4_prminst_assert_hardreset(ohri->rst_shift,
2888 oh->clkdm->pwrdm.ptr->prcm_partition,
2889 oh->clkdm->pwrdm.ptr->prcm_offs,
2890 oh->prcm.omap4.rstctrl_offs);
2891}
2892
2893/**
2894 * _omap4_deassert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2895 * @oh: struct omap_hwmod * to deassert hardreset
2896 * @ohri: hardreset line data
2897 *
2898 * Call omap4_prminst_deassert_hardreset() with parameters extracted
2899 * from the hwmod @oh and the hardreset line data @ohri. Only
2900 * intended for use as an soc_ops function pointer. Passes along the
2901 * return value from omap4_prminst_deassert_hardreset(). XXX This
2902 * function is scheduled for removal when the PRM code is moved into
2903 * drivers/.
2904 */
2905static int _omap4_deassert_hardreset(struct omap_hwmod *oh,
2906 struct omap_hwmod_rst_info *ohri)
2907{
Paul Walmsley07b3a132012-06-20 20:11:36 -06002908 if (!oh->clkdm)
2909 return -EINVAL;
2910
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002911 if (ohri->st_shift)
2912 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
2913 oh->name, ohri->name);
2914 return omap4_prminst_deassert_hardreset(ohri->rst_shift,
2915 oh->clkdm->pwrdm.ptr->prcm_partition,
2916 oh->clkdm->pwrdm.ptr->prcm_offs,
2917 oh->prcm.omap4.rstctrl_offs);
2918}
2919
2920/**
2921 * _omap4_is_hardreset_asserted - call OMAP4 PRM hardreset fn with hwmod args
2922 * @oh: struct omap_hwmod * to test hardreset
2923 * @ohri: hardreset line data
2924 *
2925 * Call omap4_prminst_is_hardreset_asserted() with parameters
2926 * extracted from the hwmod @oh and the hardreset line data @ohri.
2927 * Only intended for use as an soc_ops function pointer. Passes along
2928 * the return value from omap4_prminst_is_hardreset_asserted(). XXX
2929 * This function is scheduled for removal when the PRM code is moved
2930 * into drivers/.
2931 */
2932static int _omap4_is_hardreset_asserted(struct omap_hwmod *oh,
2933 struct omap_hwmod_rst_info *ohri)
2934{
Paul Walmsley07b3a132012-06-20 20:11:36 -06002935 if (!oh->clkdm)
2936 return -EINVAL;
2937
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002938 return omap4_prminst_is_hardreset_asserted(ohri->rst_shift,
2939 oh->clkdm->pwrdm.ptr->prcm_partition,
2940 oh->clkdm->pwrdm.ptr->prcm_offs,
2941 oh->prcm.omap4.rstctrl_offs);
2942}
2943
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06002944/**
2945 * _am33xx_assert_hardreset - call AM33XX PRM hardreset fn with hwmod args
2946 * @oh: struct omap_hwmod * to assert hardreset
2947 * @ohri: hardreset line data
2948 *
2949 * Call am33xx_prminst_assert_hardreset() with parameters extracted
2950 * from the hwmod @oh and the hardreset line data @ohri. Only
2951 * intended for use as an soc_ops function pointer. Passes along the
2952 * return value from am33xx_prminst_assert_hardreset(). XXX This
2953 * function is scheduled for removal when the PRM code is moved into
2954 * drivers/.
2955 */
2956static int _am33xx_assert_hardreset(struct omap_hwmod *oh,
2957 struct omap_hwmod_rst_info *ohri)
2958
2959{
2960 return am33xx_prm_assert_hardreset(ohri->rst_shift,
2961 oh->clkdm->pwrdm.ptr->prcm_offs,
2962 oh->prcm.omap4.rstctrl_offs);
2963}
2964
2965/**
2966 * _am33xx_deassert_hardreset - call AM33XX PRM hardreset fn with hwmod args
2967 * @oh: struct omap_hwmod * to deassert hardreset
2968 * @ohri: hardreset line data
2969 *
2970 * Call am33xx_prminst_deassert_hardreset() with parameters extracted
2971 * from the hwmod @oh and the hardreset line data @ohri. Only
2972 * intended for use as an soc_ops function pointer. Passes along the
2973 * return value from am33xx_prminst_deassert_hardreset(). XXX This
2974 * function is scheduled for removal when the PRM code is moved into
2975 * drivers/.
2976 */
2977static int _am33xx_deassert_hardreset(struct omap_hwmod *oh,
2978 struct omap_hwmod_rst_info *ohri)
2979{
2980 if (ohri->st_shift)
2981 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
2982 oh->name, ohri->name);
2983
2984 return am33xx_prm_deassert_hardreset(ohri->rst_shift,
2985 oh->clkdm->pwrdm.ptr->prcm_offs,
2986 oh->prcm.omap4.rstctrl_offs,
2987 oh->prcm.omap4.rstst_offs);
2988}
2989
2990/**
2991 * _am33xx_is_hardreset_asserted - call AM33XX PRM hardreset fn with hwmod args
2992 * @oh: struct omap_hwmod * to test hardreset
2993 * @ohri: hardreset line data
2994 *
2995 * Call am33xx_prminst_is_hardreset_asserted() with parameters
2996 * extracted from the hwmod @oh and the hardreset line data @ohri.
2997 * Only intended for use as an soc_ops function pointer. Passes along
2998 * the return value from am33xx_prminst_is_hardreset_asserted(). XXX
2999 * This function is scheduled for removal when the PRM code is moved
3000 * into drivers/.
3001 */
3002static int _am33xx_is_hardreset_asserted(struct omap_hwmod *oh,
3003 struct omap_hwmod_rst_info *ohri)
3004{
3005 return am33xx_prm_is_hardreset_asserted(ohri->rst_shift,
3006 oh->clkdm->pwrdm.ptr->prcm_offs,
3007 oh->prcm.omap4.rstctrl_offs);
3008}
3009
Paul Walmsley63c85232009-09-03 20:14:03 +03003010/* Public functions */
3011
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07003012u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03003013{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07003014 if (oh->flags & HWMOD_16BIT_REG)
3015 return __raw_readw(oh->_mpu_rt_va + reg_offs);
3016 else
3017 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03003018}
3019
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07003020void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03003021{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07003022 if (oh->flags & HWMOD_16BIT_REG)
3023 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
3024 else
3025 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03003026}
3027
Paul Walmsley887adea2010-07-26 16:34:33 -06003028/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06003029 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
3030 * @oh: struct omap_hwmod *
3031 *
3032 * This is a public function exposed to drivers. Some drivers may need to do
3033 * some settings before and after resetting the device. Those drivers after
3034 * doing the necessary settings could use this function to start a reset by
3035 * setting the SYSCONFIG.SOFTRESET bit.
3036 */
3037int omap_hwmod_softreset(struct omap_hwmod *oh)
3038{
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06003039 u32 v;
3040 int ret;
3041
3042 if (!oh || !(oh->_sysc_cache))
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06003043 return -EINVAL;
3044
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06003045 v = oh->_sysc_cache;
3046 ret = _set_softreset(oh, &v);
3047 if (ret)
3048 goto error;
3049 _write_sysconfig(v, oh);
3050
3051error:
3052 return ret;
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06003053}
3054
3055/**
Paul Walmsley887adea2010-07-26 16:34:33 -06003056 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
3057 * @oh: struct omap_hwmod *
3058 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
3059 *
3060 * Sets the IP block's OCP slave idlemode in hardware, and updates our
3061 * local copy. Intended to be used by drivers that have some erratum
3062 * that requires direct manipulation of the SIDLEMODE bits. Returns
3063 * -EINVAL if @oh is null, or passes along the return value from
3064 * _set_slave_idlemode().
3065 *
3066 * XXX Does this function have any current users? If not, we should
3067 * remove it; it is better to let the rest of the hwmod code handle this.
3068 * Any users of this function should be scrutinized carefully.
3069 */
Kevin Hilman46273e62010-01-26 20:13:03 -07003070int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
3071{
3072 u32 v;
3073 int retval = 0;
3074
3075 if (!oh)
3076 return -EINVAL;
3077
3078 v = oh->_sysc_cache;
3079
3080 retval = _set_slave_idlemode(oh, idlemode, &v);
3081 if (!retval)
3082 _write_sysconfig(v, oh);
3083
3084 return retval;
3085}
3086
Paul Walmsley63c85232009-09-03 20:14:03 +03003087/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003088 * omap_hwmod_lookup - look up a registered omap_hwmod by name
3089 * @name: name of the omap_hwmod to look up
3090 *
3091 * Given a @name of an omap_hwmod, return a pointer to the registered
3092 * struct omap_hwmod *, or NULL upon error.
3093 */
3094struct omap_hwmod *omap_hwmod_lookup(const char *name)
3095{
3096 struct omap_hwmod *oh;
3097
3098 if (!name)
3099 return NULL;
3100
Paul Walmsley63c85232009-09-03 20:14:03 +03003101 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03003102
3103 return oh;
3104}
3105
3106/**
3107 * omap_hwmod_for_each - call function for each registered omap_hwmod
3108 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06003109 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03003110 *
3111 * Call @fn for each registered omap_hwmod, passing @data to each
3112 * function. @fn must return 0 for success or any other value for
3113 * failure. If @fn returns non-zero, the iteration across omap_hwmods
3114 * will stop and the non-zero return value will be passed to the
3115 * caller of omap_hwmod_for_each(). @fn is called with
3116 * omap_hwmod_for_each() held.
3117 */
Paul Walmsley97d60162010-07-26 16:34:30 -06003118int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
3119 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03003120{
3121 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05303122 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03003123
3124 if (!fn)
3125 return -EINVAL;
3126
Paul Walmsley63c85232009-09-03 20:14:03 +03003127 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06003128 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03003129 if (ret)
3130 break;
3131 }
Paul Walmsley63c85232009-09-03 20:14:03 +03003132
3133 return ret;
3134}
3135
Paul Walmsley63c85232009-09-03 20:14:03 +03003136/**
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003137 * omap_hwmod_register_links - register an array of hwmod links
3138 * @ois: pointer to an array of omap_hwmod_ocp_if to register
3139 *
3140 * Intended to be called early in boot before the clock framework is
3141 * initialized. If @ois is not null, will register all omap_hwmods
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003142 * listed in @ois that are valid for this chip. Returns -EINVAL if
3143 * omap_hwmod_init() hasn't been called before calling this function,
3144 * -ENOMEM if the link memory area can't be allocated, or 0 upon
3145 * success.
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003146 */
3147int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
3148{
3149 int r, i;
3150
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003151 if (!inited)
3152 return -EINVAL;
3153
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003154 if (!ois)
3155 return 0;
3156
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003157 if (!linkspace) {
3158 if (_alloc_linkspace(ois)) {
3159 pr_err("omap_hwmod: could not allocate link space\n");
3160 return -ENOMEM;
3161 }
3162 }
3163
3164 i = 0;
3165 do {
3166 r = _register_link(ois[i]);
3167 WARN(r && r != -EEXIST,
3168 "omap_hwmod: _register_link(%s -> %s) returned %d\n",
3169 ois[i]->master->name, ois[i]->slave->name, r);
3170 } while (ois[++i]);
3171
3172 return 0;
3173}
3174
3175/**
Paul Walmsley381d0332012-04-19 00:58:22 -06003176 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
3177 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08003178 *
Paul Walmsley381d0332012-04-19 00:58:22 -06003179 * If the hwmod data corresponding to the MPU subsystem IP block
3180 * hasn't been initialized and set up yet, do so now. This must be
3181 * done first since sleep dependencies may be added from other hwmods
3182 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
3183 * return value.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08003184 */
Paul Walmsley381d0332012-04-19 00:58:22 -06003185static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08003186{
Paul Walmsley381d0332012-04-19 00:58:22 -06003187 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
3188 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
3189 __func__, MPU_INITIATOR_NAME);
3190 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
3191 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
Paul Walmsley63c85232009-09-03 20:14:03 +03003192}
3193
3194/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003195 * omap_hwmod_setup_one - set up a single hwmod
3196 * @oh_name: const char * name of the already-registered hwmod to set up
3197 *
Paul Walmsley381d0332012-04-19 00:58:22 -06003198 * Initialize and set up a single hwmod. Intended to be used for a
3199 * small number of early devices, such as the timer IP blocks used for
3200 * the scheduler clock. Must be called after omap2_clk_init().
3201 * Resolves the struct clk names to struct clk pointers for each
3202 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
3203 * -EINVAL upon error or 0 upon success.
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003204 */
3205int __init omap_hwmod_setup_one(const char *oh_name)
3206{
3207 struct omap_hwmod *oh;
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003208
3209 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
3210
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003211 oh = _lookup(oh_name);
3212 if (!oh) {
3213 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
3214 return -EINVAL;
3215 }
3216
Paul Walmsley381d0332012-04-19 00:58:22 -06003217 _ensure_mpu_hwmod_is_setup(oh);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003218
Paul Walmsley381d0332012-04-19 00:58:22 -06003219 _init(oh, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003220 _setup(oh, NULL);
3221
3222 return 0;
3223}
3224
3225/**
Paul Walmsley381d0332012-04-19 00:58:22 -06003226 * omap_hwmod_setup_all - set up all registered IP blocks
Paul Walmsley63c85232009-09-03 20:14:03 +03003227 *
Paul Walmsley381d0332012-04-19 00:58:22 -06003228 * Initialize and set up all IP blocks registered with the hwmod code.
3229 * Must be called after omap2_clk_init(). Resolves the struct clk
3230 * names to struct clk pointers for each registered omap_hwmod. Also
3231 * calls _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003232 */
Paul Walmsley550c8092011-02-28 11:58:14 -07003233static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03003234{
Paul Walmsley381d0332012-04-19 00:58:22 -06003235 _ensure_mpu_hwmod_is_setup(NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03003236
Paul Walmsley381d0332012-04-19 00:58:22 -06003237 omap_hwmod_for_each(_init, NULL);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003238 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03003239
3240 return 0;
3241}
Paul Walmsley550c8092011-02-28 11:58:14 -07003242core_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03003243
3244/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003245 * omap_hwmod_enable - enable an omap_hwmod
3246 * @oh: struct omap_hwmod *
3247 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06003248 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03003249 * Returns -EINVAL on error or passes along the return value from _enable().
3250 */
3251int omap_hwmod_enable(struct omap_hwmod *oh)
3252{
3253 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003254 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03003255
3256 if (!oh)
3257 return -EINVAL;
3258
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003259 spin_lock_irqsave(&oh->_lock, flags);
3260 r = _enable(oh);
3261 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003262
3263 return r;
3264}
3265
3266/**
3267 * omap_hwmod_idle - idle an omap_hwmod
3268 * @oh: struct omap_hwmod *
3269 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06003270 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03003271 * Returns -EINVAL on error or passes along the return value from _idle().
3272 */
3273int omap_hwmod_idle(struct omap_hwmod *oh)
3274{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003275 unsigned long flags;
3276
Paul Walmsley63c85232009-09-03 20:14:03 +03003277 if (!oh)
3278 return -EINVAL;
3279
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003280 spin_lock_irqsave(&oh->_lock, flags);
3281 _idle(oh);
3282 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003283
3284 return 0;
3285}
3286
3287/**
3288 * omap_hwmod_shutdown - shutdown an omap_hwmod
3289 * @oh: struct omap_hwmod *
3290 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06003291 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03003292 * omap_device_shutdown(). Returns -EINVAL on error or passes along
3293 * the return value from _shutdown().
3294 */
3295int omap_hwmod_shutdown(struct omap_hwmod *oh)
3296{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003297 unsigned long flags;
3298
Paul Walmsley63c85232009-09-03 20:14:03 +03003299 if (!oh)
3300 return -EINVAL;
3301
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003302 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003303 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003304 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003305
3306 return 0;
3307}
3308
3309/**
3310 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
3311 * @oh: struct omap_hwmod *oh
3312 *
3313 * Intended to be called by the omap_device code.
3314 */
3315int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
3316{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003317 unsigned long flags;
3318
3319 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003320 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003321 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003322
3323 return 0;
3324}
3325
3326/**
3327 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
3328 * @oh: struct omap_hwmod *oh
3329 *
3330 * Intended to be called by the omap_device code.
3331 */
3332int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
3333{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003334 unsigned long flags;
3335
3336 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003337 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003338 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003339
3340 return 0;
3341}
3342
3343/**
3344 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
3345 * @oh: struct omap_hwmod *oh
3346 *
3347 * Intended to be called by drivers and core code when all posted
3348 * writes to a device must complete before continuing further
3349 * execution (for example, after clearing some device IRQSTATUS
3350 * register bits)
3351 *
3352 * XXX what about targets with multiple OCP threads?
3353 */
3354void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
3355{
3356 BUG_ON(!oh);
3357
Paul Walmsley43b40992010-02-22 22:09:34 -07003358 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Russell King4f8a4282012-02-07 10:59:37 +00003359 WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
3360 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03003361 return;
3362 }
3363
3364 /*
3365 * Forces posted writes to complete on the OCP thread handling
3366 * register writes
3367 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07003368 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03003369}
3370
3371/**
3372 * omap_hwmod_reset - reset the hwmod
3373 * @oh: struct omap_hwmod *
3374 *
3375 * Under some conditions, a driver may wish to reset the entire device.
3376 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06003377 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03003378 */
3379int omap_hwmod_reset(struct omap_hwmod *oh)
3380{
3381 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003382 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03003383
Liam Girdwood9b579112010-09-21 10:34:09 -06003384 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03003385 return -EINVAL;
3386
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003387 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003388 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003389 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003390
3391 return r;
3392}
3393
Paul Walmsley5e8370f2012-04-18 19:10:06 -06003394/*
3395 * IP block data retrieval functions
3396 */
3397
Paul Walmsley63c85232009-09-03 20:14:03 +03003398/**
3399 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
3400 * @oh: struct omap_hwmod *
3401 * @res: pointer to the first element of an array of struct resource to fill
3402 *
3403 * Count the number of struct resource array elements necessary to
3404 * contain omap_hwmod @oh resources. Intended to be called by code
3405 * that registers omap_devices. Intended to be used to determine the
3406 * size of a dynamically-allocated struct resource array, before
3407 * calling omap_hwmod_fill_resources(). Returns the number of struct
3408 * resource array elements needed.
3409 *
3410 * XXX This code is not optimized. It could attempt to merge adjacent
3411 * resource IDs.
3412 *
3413 */
3414int omap_hwmod_count_resources(struct omap_hwmod *oh)
3415{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003416 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003417 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003418 int ret;
3419 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03003420
Paul Walmsleybc614952011-07-09 19:14:07 -06003421 ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03003422
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003423 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003424
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003425 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003426 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003427 ret += _count_ocp_if_addr_spaces(os);
3428 }
Paul Walmsley63c85232009-09-03 20:14:03 +03003429
3430 return ret;
3431}
3432
3433/**
3434 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
3435 * @oh: struct omap_hwmod *
3436 * @res: pointer to the first element of an array of struct resource to fill
3437 *
3438 * Fill the struct resource array @res with resource data from the
3439 * omap_hwmod @oh. Intended to be called by code that registers
3440 * omap_devices. See also omap_hwmod_count_resources(). Returns the
3441 * number of array elements filled.
3442 */
3443int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
3444{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003445 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003446 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003447 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03003448 int r = 0;
3449
3450 /* For each IRQ, DMA, memory area, fill in array.*/
3451
Paul Walmsley212738a2011-07-09 19:14:06 -06003452 mpu_irqs_cnt = _count_mpu_irqs(oh);
3453 for (i = 0; i < mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07003454 (res + r)->name = (oh->mpu_irqs + i)->name;
3455 (res + r)->start = (oh->mpu_irqs + i)->irq;
3456 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03003457 (res + r)->flags = IORESOURCE_IRQ;
3458 r++;
3459 }
3460
Paul Walmsleybc614952011-07-09 19:14:07 -06003461 sdma_reqs_cnt = _count_sdma_reqs(oh);
3462 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06003463 (res + r)->name = (oh->sdma_reqs + i)->name;
3464 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
3465 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03003466 (res + r)->flags = IORESOURCE_DMA;
3467 r++;
3468 }
3469
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003470 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003471
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003472 i = 0;
3473 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003474 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley78183f32011-07-09 19:14:05 -06003475 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03003476
Paul Walmsley78183f32011-07-09 19:14:05 -06003477 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08003478 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03003479 (res + r)->start = (os->addr + j)->pa_start;
3480 (res + r)->end = (os->addr + j)->pa_end;
3481 (res + r)->flags = IORESOURCE_MEM;
3482 r++;
3483 }
3484 }
3485
3486 return r;
3487}
3488
3489/**
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +05303490 * omap_hwmod_fill_dma_resources - fill struct resource array with dma data
3491 * @oh: struct omap_hwmod *
3492 * @res: pointer to the array of struct resource to fill
3493 *
3494 * Fill the struct resource array @res with dma resource data from the
3495 * omap_hwmod @oh. Intended to be called by code that registers
3496 * omap_devices. See also omap_hwmod_count_resources(). Returns the
3497 * number of array elements filled.
3498 */
3499int omap_hwmod_fill_dma_resources(struct omap_hwmod *oh, struct resource *res)
3500{
3501 int i, sdma_reqs_cnt;
3502 int r = 0;
3503
3504 sdma_reqs_cnt = _count_sdma_reqs(oh);
3505 for (i = 0; i < sdma_reqs_cnt; i++) {
3506 (res + r)->name = (oh->sdma_reqs + i)->name;
3507 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
3508 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
3509 (res + r)->flags = IORESOURCE_DMA;
3510 r++;
3511 }
3512
3513 return r;
3514}
3515
3516/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06003517 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
3518 * @oh: struct omap_hwmod * to operate on
3519 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
3520 * @name: pointer to the name of the data to fetch (optional)
3521 * @rsrc: pointer to a struct resource, allocated by the caller
3522 *
3523 * Retrieve MPU IRQ, SDMA request line, or address space start/end
3524 * data for the IP block pointed to by @oh. The data will be filled
3525 * into a struct resource record pointed to by @rsrc. The struct
3526 * resource must be allocated by the caller. When @name is non-null,
3527 * the data associated with the matching entry in the IRQ/SDMA/address
3528 * space hwmod data arrays will be returned. If @name is null, the
3529 * first array entry will be returned. Data order is not meaningful
3530 * in hwmod data, so callers are strongly encouraged to use a non-null
3531 * @name whenever possible to avoid unpredictable effects if hwmod
3532 * data is later added that causes data ordering to change. This
3533 * function is only intended for use by OMAP core code. Device
3534 * drivers should not call this function - the appropriate bus-related
3535 * data accessor functions should be used instead. Returns 0 upon
3536 * success or a negative error code upon error.
3537 */
3538int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
3539 const char *name, struct resource *rsrc)
3540{
3541 int r;
3542 unsigned int irq, dma;
3543 u32 pa_start, pa_end;
3544
3545 if (!oh || !rsrc)
3546 return -EINVAL;
3547
3548 if (type == IORESOURCE_IRQ) {
3549 r = _get_mpu_irq_by_name(oh, name, &irq);
3550 if (r)
3551 return r;
3552
3553 rsrc->start = irq;
3554 rsrc->end = irq;
3555 } else if (type == IORESOURCE_DMA) {
3556 r = _get_sdma_req_by_name(oh, name, &dma);
3557 if (r)
3558 return r;
3559
3560 rsrc->start = dma;
3561 rsrc->end = dma;
3562 } else if (type == IORESOURCE_MEM) {
3563 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
3564 if (r)
3565 return r;
3566
3567 rsrc->start = pa_start;
3568 rsrc->end = pa_end;
3569 } else {
3570 return -EINVAL;
3571 }
3572
3573 rsrc->flags = type;
3574 rsrc->name = name;
3575
3576 return 0;
3577}
3578
3579/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003580 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
3581 * @oh: struct omap_hwmod *
3582 *
3583 * Return the powerdomain pointer associated with the OMAP module
3584 * @oh's main clock. If @oh does not have a main clk, return the
3585 * powerdomain associated with the interface clock associated with the
3586 * module's MPU port. (XXX Perhaps this should use the SDMA port
3587 * instead?) Returns NULL on error, or a struct powerdomain * on
3588 * success.
3589 */
3590struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
3591{
3592 struct clk *c;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003593 struct omap_hwmod_ocp_if *oi;
Paul Walmsley63c85232009-09-03 20:14:03 +03003594
3595 if (!oh)
3596 return NULL;
3597
3598 if (oh->_clk) {
3599 c = oh->_clk;
3600 } else {
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003601 oi = _find_mpu_rt_port(oh);
3602 if (!oi)
Paul Walmsley63c85232009-09-03 20:14:03 +03003603 return NULL;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003604 c = oi->_clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03003605 }
3606
Thara Gopinathd5647c12010-03-31 04:16:29 -06003607 if (!c->clkdm)
3608 return NULL;
3609
Paul Walmsley63c85232009-09-03 20:14:03 +03003610 return c->clkdm->pwrdm.ptr;
3611
3612}
3613
3614/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06003615 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3616 * @oh: struct omap_hwmod *
3617 *
3618 * Returns the virtual address corresponding to the beginning of the
3619 * module's register target, in the address range that is intended to
3620 * be used by the MPU. Returns the virtual address upon success or NULL
3621 * upon error.
3622 */
3623void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3624{
3625 if (!oh)
3626 return NULL;
3627
3628 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3629 return NULL;
3630
3631 if (oh->_state == _HWMOD_STATE_UNKNOWN)
3632 return NULL;
3633
3634 return oh->_mpu_rt_va;
3635}
3636
3637/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003638 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
3639 * @oh: struct omap_hwmod *
3640 * @init_oh: struct omap_hwmod * (initiator)
3641 *
3642 * Add a sleep dependency between the initiator @init_oh and @oh.
3643 * Intended to be called by DSP/Bridge code via platform_data for the
3644 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3645 * code needs to add/del initiator dependencies dynamically
3646 * before/after accessing a device. Returns the return value from
3647 * _add_initiator_dep().
3648 *
3649 * XXX Keep a usecount in the clockdomain code
3650 */
3651int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
3652 struct omap_hwmod *init_oh)
3653{
3654 return _add_initiator_dep(oh, init_oh);
3655}
3656
3657/*
3658 * XXX what about functions for drivers to save/restore ocp_sysconfig
3659 * for context save/restore operations?
3660 */
3661
3662/**
3663 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
3664 * @oh: struct omap_hwmod *
3665 * @init_oh: struct omap_hwmod * (initiator)
3666 *
3667 * Remove a sleep dependency between the initiator @init_oh and @oh.
3668 * Intended to be called by DSP/Bridge code via platform_data for the
3669 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3670 * code needs to add/del initiator dependencies dynamically
3671 * before/after accessing a device. Returns the return value from
3672 * _del_initiator_dep().
3673 *
3674 * XXX Keep a usecount in the clockdomain code
3675 */
3676int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
3677 struct omap_hwmod *init_oh)
3678{
3679 return _del_initiator_dep(oh, init_oh);
3680}
3681
3682/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003683 * omap_hwmod_enable_wakeup - allow device to wake up the system
3684 * @oh: struct omap_hwmod *
3685 *
3686 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
Govindraj.R2a1cc142012-04-05 02:59:32 -06003687 * send wakeups to the PRCM, and enable I/O ring wakeup events for
3688 * this IP block if it has dynamic mux entries. Eventually this
3689 * should set PRCM wakeup registers to cause the PRCM to receive
3690 * wakeup events from the module. Does not set any wakeup routing
3691 * registers beyond this point - if the module is to wake up any other
3692 * module or subsystem, that must be set separately. Called by
3693 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003694 */
3695int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3696{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003697 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003698 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003699
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003700 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003701
3702 if (oh->class->sysc &&
3703 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3704 v = oh->_sysc_cache;
3705 _enable_wakeup(oh, &v);
3706 _write_sysconfig(v, oh);
3707 }
3708
Govindraj Receec002011-12-16 14:36:58 -07003709 _set_idle_ioring_wakeup(oh, true);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003710 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003711
3712 return 0;
3713}
3714
3715/**
3716 * omap_hwmod_disable_wakeup - prevent device from waking the system
3717 * @oh: struct omap_hwmod *
3718 *
3719 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
Govindraj.R2a1cc142012-04-05 02:59:32 -06003720 * from sending wakeups to the PRCM, and disable I/O ring wakeup
3721 * events for this IP block if it has dynamic mux entries. Eventually
3722 * this should clear PRCM wakeup registers to cause the PRCM to ignore
3723 * wakeup events from the module. Does not set any wakeup routing
3724 * registers beyond this point - if the module is to wake up any other
3725 * module or subsystem, that must be set separately. Called by
3726 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003727 */
3728int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3729{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003730 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003731 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003732
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003733 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003734
3735 if (oh->class->sysc &&
3736 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3737 v = oh->_sysc_cache;
3738 _disable_wakeup(oh, &v);
3739 _write_sysconfig(v, oh);
3740 }
3741
Govindraj Receec002011-12-16 14:36:58 -07003742 _set_idle_ioring_wakeup(oh, false);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003743 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003744
3745 return 0;
3746}
Paul Walmsley43b40992010-02-22 22:09:34 -07003747
3748/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003749 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3750 * contained in the hwmod module.
3751 * @oh: struct omap_hwmod *
3752 * @name: name of the reset line to lookup and assert
3753 *
3754 * Some IP like dsp, ipu or iva contain processor that require
3755 * an HW reset line to be assert / deassert in order to enable fully
3756 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3757 * yet supported on this OMAP; otherwise, passes along the return value
3758 * from _assert_hardreset().
3759 */
3760int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3761{
3762 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003763 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003764
3765 if (!oh)
3766 return -EINVAL;
3767
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003768 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003769 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003770 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003771
3772 return ret;
3773}
3774
3775/**
3776 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3777 * contained in the hwmod module.
3778 * @oh: struct omap_hwmod *
3779 * @name: name of the reset line to look up and deassert
3780 *
3781 * Some IP like dsp, ipu or iva contain processor that require
3782 * an HW reset line to be assert / deassert in order to enable fully
3783 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3784 * yet supported on this OMAP; otherwise, passes along the return value
3785 * from _deassert_hardreset().
3786 */
3787int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3788{
3789 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003790 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003791
3792 if (!oh)
3793 return -EINVAL;
3794
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003795 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003796 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003797 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003798
3799 return ret;
3800}
3801
3802/**
3803 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
3804 * contained in the hwmod module
3805 * @oh: struct omap_hwmod *
3806 * @name: name of the reset line to look up and read
3807 *
3808 * Return the current state of the hwmod @oh's reset line named @name:
3809 * returns -EINVAL upon parameter error or if this operation
3810 * is unsupported on the current OMAP; otherwise, passes along the return
3811 * value from _read_hardreset().
3812 */
3813int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
3814{
3815 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003816 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003817
3818 if (!oh)
3819 return -EINVAL;
3820
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003821 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003822 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003823 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003824
3825 return ret;
3826}
3827
3828
3829/**
Paul Walmsley43b40992010-02-22 22:09:34 -07003830 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3831 * @classname: struct omap_hwmod_class name to search for
3832 * @fn: callback function pointer to call for each hwmod in class @classname
3833 * @user: arbitrary context data to pass to the callback function
3834 *
Benoit Coussonce35b242010-12-21 21:31:28 -07003835 * For each omap_hwmod of class @classname, call @fn.
3836 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07003837 * zero, the iterator is terminated, and the callback function's return
3838 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3839 * if @classname or @fn are NULL, or passes back the error code from @fn.
3840 */
3841int omap_hwmod_for_each_by_class(const char *classname,
3842 int (*fn)(struct omap_hwmod *oh,
3843 void *user),
3844 void *user)
3845{
3846 struct omap_hwmod *temp_oh;
3847 int ret = 0;
3848
3849 if (!classname || !fn)
3850 return -EINVAL;
3851
3852 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3853 __func__, classname);
3854
Paul Walmsley43b40992010-02-22 22:09:34 -07003855 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3856 if (!strcmp(temp_oh->class->name, classname)) {
3857 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3858 __func__, temp_oh->name);
3859 ret = (*fn)(temp_oh, user);
3860 if (ret)
3861 break;
3862 }
3863 }
3864
Paul Walmsley43b40992010-02-22 22:09:34 -07003865 if (ret)
3866 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3867 __func__, ret);
3868
3869 return ret;
3870}
3871
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003872/**
3873 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3874 * @oh: struct omap_hwmod *
3875 * @state: state that _setup() should leave the hwmod in
3876 *
Paul Walmsley550c8092011-02-28 11:58:14 -07003877 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsley64813c32012-04-18 19:10:03 -06003878 * (called by omap_hwmod_setup_*()). See also the documentation
3879 * for _setup_postsetup(), above. Returns 0 upon success or
3880 * -EINVAL if there is a problem with the arguments or if the hwmod is
3881 * in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003882 */
3883int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3884{
3885 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003886 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003887
3888 if (!oh)
3889 return -EINVAL;
3890
3891 if (state != _HWMOD_STATE_DISABLED &&
3892 state != _HWMOD_STATE_ENABLED &&
3893 state != _HWMOD_STATE_IDLE)
3894 return -EINVAL;
3895
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003896 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003897
3898 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3899 ret = -EINVAL;
3900 goto ohsps_unlock;
3901 }
3902
3903 oh->_postsetup_state = state;
3904 ret = 0;
3905
3906ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003907 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003908
3909 return ret;
3910}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003911
3912/**
3913 * omap_hwmod_get_context_loss_count - get lost context count
3914 * @oh: struct omap_hwmod *
3915 *
3916 * Query the powerdomain of of @oh to get the context loss
3917 * count for this device.
3918 *
3919 * Returns the context loss count of the powerdomain assocated with @oh
3920 * upon success, or zero if no powerdomain exists for @oh.
3921 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03003922int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003923{
3924 struct powerdomain *pwrdm;
3925 int ret = 0;
3926
3927 pwrdm = omap_hwmod_get_pwrdm(oh);
3928 if (pwrdm)
3929 ret = pwrdm_get_context_loss_count(pwrdm);
3930
3931 return ret;
3932}
Paul Walmsley43b01642011-03-10 03:50:07 -07003933
3934/**
3935 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
3936 * @oh: struct omap_hwmod *
3937 *
3938 * Prevent the hwmod @oh from being reset during the setup process.
3939 * Intended for use by board-*.c files on boards with devices that
3940 * cannot tolerate being reset. Must be called before the hwmod has
3941 * been set up. Returns 0 upon success or negative error code upon
3942 * failure.
3943 */
3944int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
3945{
3946 if (!oh)
3947 return -EINVAL;
3948
3949 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3950 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
3951 oh->name);
3952 return -EINVAL;
3953 }
3954
3955 oh->flags |= HWMOD_INIT_NO_RESET;
3956
3957 return 0;
3958}
Tero Kristoabc2d542011-12-16 14:36:59 -07003959
3960/**
3961 * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
3962 * @oh: struct omap_hwmod * containing hwmod mux entries
3963 * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
3964 * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
3965 *
3966 * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
3967 * entry number @pad_idx for the hwmod @oh, trigger the interrupt
3968 * service routine for the hwmod's mpu_irqs array index @irq_idx. If
3969 * this function is not called for a given pad_idx, then the ISR
3970 * associated with @oh's first MPU IRQ will be triggered when an I/O
3971 * pad wakeup occurs on that pad. Note that @pad_idx is the index of
3972 * the _dynamic or wakeup_ entry: if there are other entries not
3973 * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
3974 * entries are NOT COUNTED in the dynamic pad index. This function
3975 * must be called separately for each pad that requires its interrupt
3976 * to be re-routed this way. Returns -EINVAL if there is an argument
3977 * problem or if @oh does not have hwmod mux entries or MPU IRQs;
3978 * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
3979 *
3980 * XXX This function interface is fragile. Rather than using array
3981 * indexes, which are subject to unpredictable change, it should be
3982 * using hwmod IRQ names, and some other stable key for the hwmod mux
3983 * pad records.
3984 */
3985int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
3986{
3987 int nr_irqs;
3988
3989 might_sleep();
3990
3991 if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
3992 pad_idx >= oh->mux->nr_pads_dynamic)
3993 return -EINVAL;
3994
3995 /* Check the number of available mpu_irqs */
3996 for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
3997 ;
3998
3999 if (irq_idx >= nr_irqs)
4000 return -EINVAL;
4001
4002 if (!oh->mux->irqs) {
4003 /* XXX What frees this? */
4004 oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
4005 GFP_KERNEL);
4006 if (!oh->mux->irqs)
4007 return -ENOMEM;
4008 }
4009 oh->mux->irqs[pad_idx] = irq_idx;
4010
4011 return 0;
4012}
Kevin Hilman9ebfd282012-06-18 12:12:23 -06004013
4014/**
4015 * omap_hwmod_init - initialize the hwmod code
4016 *
4017 * Sets up some function pointers needed by the hwmod code to operate on the
4018 * currently-booted SoC. Intended to be called once during kernel init
4019 * before any hwmods are registered. No return value.
4020 */
4021void __init omap_hwmod_init(void)
4022{
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -06004023 if (cpu_is_omap24xx()) {
4024 soc_ops.wait_target_ready = _omap2xxx_wait_target_ready;
4025 soc_ops.assert_hardreset = _omap2_assert_hardreset;
4026 soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
4027 soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
4028 } else if (cpu_is_omap34xx()) {
4029 soc_ops.wait_target_ready = _omap3xxx_wait_target_ready;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06004030 soc_ops.assert_hardreset = _omap2_assert_hardreset;
4031 soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
4032 soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
R Sricharan05e152c2012-06-05 16:21:32 +05304033 } else if (cpu_is_omap44xx() || soc_is_omap54xx()) {
Kevin Hilman9ebfd282012-06-18 12:12:23 -06004034 soc_ops.enable_module = _omap4_enable_module;
4035 soc_ops.disable_module = _omap4_disable_module;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06004036 soc_ops.wait_target_ready = _omap4_wait_target_ready;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06004037 soc_ops.assert_hardreset = _omap4_assert_hardreset;
4038 soc_ops.deassert_hardreset = _omap4_deassert_hardreset;
4039 soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
Kevin Hilman0a179ea2012-06-18 12:12:25 -06004040 soc_ops.init_clkdm = _init_clkdm;
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06004041 } else if (soc_is_am33xx()) {
4042 soc_ops.enable_module = _am33xx_enable_module;
4043 soc_ops.disable_module = _am33xx_disable_module;
4044 soc_ops.wait_target_ready = _am33xx_wait_target_ready;
4045 soc_ops.assert_hardreset = _am33xx_assert_hardreset;
4046 soc_ops.deassert_hardreset = _am33xx_deassert_hardreset;
4047 soc_ops.is_hardreset_asserted = _am33xx_is_hardreset_asserted;
4048 soc_ops.init_clkdm = _init_clkdm;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06004049 } else {
4050 WARN(1, "omap_hwmod: unknown SoC type\n");
Kevin Hilman9ebfd282012-06-18 12:12:23 -06004051 }
4052
4053 inited = true;
4054}
Tony Lindgren68c9a952012-07-06 00:58:43 -07004055
4056/**
4057 * omap_hwmod_get_main_clk - get pointer to main clock name
4058 * @oh: struct omap_hwmod *
4059 *
4060 * Returns the main clock name assocated with @oh upon success,
4061 * or NULL if @oh is NULL.
4062 */
4063const char *omap_hwmod_get_main_clk(struct omap_hwmod *oh)
4064{
4065 if (!oh)
4066 return NULL;
4067
4068 return oh->main_clk;
4069}