blob: 7b7ba6d48e83e3bf082790c8d677cad476e06904 [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 |
Victor Kamenskyedfaf052014-04-15 20:37:46 +030075 * | ({read,write}l_relaxed, clk*) |
Paul Walmsley74ff3a62010-09-21 15:02:23 -060076 * +-------------------------------+
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>
Stephen Boydf5b00f62015-06-22 17:05:21 -0700133#include <linux/clk.h>
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700134#include <linux/clk-provider.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300135#include <linux/delay.h>
136#include <linux/err.h>
137#include <linux/list.h>
138#include <linux/mutex.h>
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -0700139#include <linux/spinlock.h>
Tero Kristoabc2d542011-12-16 14:36:59 -0700140#include <linux/slab.h>
Thomas Gleixnerf7b861b2013-03-21 22:49:38 +0100141#include <linux/cpu.h>
Santosh Shilimkar079abad2013-01-21 18:40:57 +0530142#include <linux/of.h>
143#include <linux/of_address.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300144
Paul Walmsleyfa200222013-01-26 00:48:56 -0700145#include <asm/system_misc.h>
146
Paul Walmsleya135eaa2012-09-27 10:33:34 -0600147#include "clock.h"
Tony Lindgren2a296c82012-10-02 17:41:35 -0700148#include "omap_hwmod.h"
Paul Walmsley63c85232009-09-03 20:14:03 +0300149
Tony Lindgrendbc04162012-08-31 10:59:07 -0700150#include "soc.h"
151#include "common.h"
152#include "clockdomain.h"
153#include "powerdomain.h"
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -0600154#include "cm2xxx.h"
155#include "cm3xxx.h"
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -0600156#include "cm33xx.h"
Paul Walmsleyb13159a2012-10-29 20:57:44 -0600157#include "prm.h"
Paul Walmsley139563a2012-10-21 01:01:10 -0600158#include "prm3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -0700159#include "prm44xx.h"
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -0600160#include "prm33xx.h"
Benoit Coussoneaac3292011-07-10 05:56:31 -0600161#include "prminst44xx.h"
Vishwanath BS51658822012-06-22 08:40:04 -0600162#include "pm.h"
Paul Walmsley63c85232009-09-03 20:14:03 +0300163
Paul Walmsley63c85232009-09-03 20:14:03 +0300164/* Name of the OMAP hwmod for the MPU */
Benoit Cousson5c2c0292010-05-20 12:31:10 -0600165#define MPU_INITIATOR_NAME "mpu"
Paul Walmsley63c85232009-09-03 20:14:03 +0300166
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600167/*
168 * Number of struct omap_hwmod_link records per struct
169 * omap_hwmod_ocp_if record (master->slave and slave->master)
170 */
171#define LINKS_PER_OCP_IF 2
172
Tero Kristo4ebf5b22015-05-05 16:33:04 +0300173/*
174 * Address offset (in bytes) between the reset control and the reset
175 * status registers: 4 bytes on OMAP4
176 */
177#define OMAP4_RST_CTRL_ST_OFFSET 4
178
Tero Kristo9fabc1a2016-07-04 14:11:48 +0300179/*
180 * Maximum length for module clock handle names
181 */
182#define MOD_CLK_MAX_NAME_LEN 32
183
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600184/**
185 * struct omap_hwmod_soc_ops - fn ptrs for some SoC-specific operations
186 * @enable_module: function to enable a module (via MODULEMODE)
187 * @disable_module: function to disable a module (via MODULEMODE)
188 *
189 * XXX Eventually this functionality will be hidden inside the PRM/CM
190 * device drivers. Until then, this should avoid huge blocks of cpu_is_*()
191 * conditionals in this code.
192 */
193struct omap_hwmod_soc_ops {
194 void (*enable_module)(struct omap_hwmod *oh);
195 int (*disable_module)(struct omap_hwmod *oh);
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -0600196 int (*wait_target_ready)(struct omap_hwmod *oh);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -0600197 int (*assert_hardreset)(struct omap_hwmod *oh,
198 struct omap_hwmod_rst_info *ohri);
199 int (*deassert_hardreset)(struct omap_hwmod *oh,
200 struct omap_hwmod_rst_info *ohri);
201 int (*is_hardreset_asserted)(struct omap_hwmod *oh,
202 struct omap_hwmod_rst_info *ohri);
Kevin Hilman0a179ea2012-06-18 12:12:25 -0600203 int (*init_clkdm)(struct omap_hwmod *oh);
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -0700204 void (*update_context_lost)(struct omap_hwmod *oh);
205 int (*get_context_lost)(struct omap_hwmod *oh);
Tero Kristo9fabc1a2016-07-04 14:11:48 +0300206 int (*disable_direct_prcm)(struct omap_hwmod *oh);
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600207};
208
209/* soc_ops: adapts the omap_hwmod code to the currently-booted SoC */
210static struct omap_hwmod_soc_ops soc_ops;
211
Paul Walmsley63c85232009-09-03 20:14:03 +0300212/* omap_hwmod_list contains all registered struct omap_hwmods */
213static LIST_HEAD(omap_hwmod_list);
214
Paul Walmsley63c85232009-09-03 20:14:03 +0300215/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
216static struct omap_hwmod *mpu_oh;
217
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
Tony Lindgrena1e31232017-03-14 13:13:19 -0700238 oi = list_entry(*p, struct omap_hwmod_ocp_if, node);
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600239 *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;
Lokesh Vutlaaaf2c0f2015-06-10 14:56:24 +0530290
291 /*
292 * Some IP blocks (such as RTC) require unlocking of IP before
293 * accessing its registers. If a function pointer is present
294 * to unlock, then call it before accessing sysconfig and
295 * call lock after writing sysconfig.
296 */
297 if (oh->class->unlock)
298 oh->class->unlock(oh);
299
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700300 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Lokesh Vutlaaaf2c0f2015-06-10 14:56:24 +0530301
302 if (oh->class->lock)
303 oh->class->lock(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +0300304}
305
306/**
307 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
308 * @oh: struct omap_hwmod *
309 * @standbymode: MIDLEMODE field bits
310 * @v: pointer to register contents to modify
311 *
312 * Update the master standby mode bits in @v to be @standbymode for
313 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
314 * upon error or 0 upon success.
315 */
316static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
317 u32 *v)
318{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700319 u32 mstandby_mask;
320 u8 mstandby_shift;
321
Paul Walmsley43b40992010-02-22 22:09:34 -0700322 if (!oh->class->sysc ||
323 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300324 return -EINVAL;
325
Paul Walmsley43b40992010-02-22 22:09:34 -0700326 if (!oh->class->sysc->sysc_fields) {
327 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700328 return -EINVAL;
329 }
330
Paul Walmsley43b40992010-02-22 22:09:34 -0700331 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700332 mstandby_mask = (0x3 << mstandby_shift);
333
334 *v &= ~mstandby_mask;
335 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300336
337 return 0;
338}
339
340/**
341 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
342 * @oh: struct omap_hwmod *
343 * @idlemode: SIDLEMODE field bits
344 * @v: pointer to register contents to modify
345 *
346 * Update the slave idle mode bits in @v to be @idlemode for the @oh
347 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
348 * or 0 upon success.
349 */
350static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
351{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700352 u32 sidle_mask;
353 u8 sidle_shift;
354
Paul Walmsley43b40992010-02-22 22:09:34 -0700355 if (!oh->class->sysc ||
356 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300357 return -EINVAL;
358
Paul Walmsley43b40992010-02-22 22:09:34 -0700359 if (!oh->class->sysc->sysc_fields) {
360 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700361 return -EINVAL;
362 }
363
Paul Walmsley43b40992010-02-22 22:09:34 -0700364 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700365 sidle_mask = (0x3 << sidle_shift);
366
367 *v &= ~sidle_mask;
368 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300369
370 return 0;
371}
372
373/**
374 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
375 * @oh: struct omap_hwmod *
376 * @clockact: CLOCKACTIVITY field bits
377 * @v: pointer to register contents to modify
378 *
379 * Update the clockactivity mode bits in @v to be @clockact for the
380 * @oh hwmod. Used for additional powersaving on some modules. Does
381 * not write to the hardware. Returns -EINVAL upon error or 0 upon
382 * success.
383 */
384static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
385{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700386 u32 clkact_mask;
387 u8 clkact_shift;
388
Paul Walmsley43b40992010-02-22 22:09:34 -0700389 if (!oh->class->sysc ||
390 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300391 return -EINVAL;
392
Paul Walmsley43b40992010-02-22 22:09:34 -0700393 if (!oh->class->sysc->sysc_fields) {
394 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700395 return -EINVAL;
396 }
397
Paul Walmsley43b40992010-02-22 22:09:34 -0700398 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700399 clkact_mask = (0x3 << clkact_shift);
400
401 *v &= ~clkact_mask;
402 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300403
404 return 0;
405}
406
407/**
Roger Quadros313a76e2013-12-08 18:39:02 -0700408 * _set_softreset: set OCP_SYSCONFIG.SOFTRESET bit in @v
Paul Walmsley63c85232009-09-03 20:14:03 +0300409 * @oh: struct omap_hwmod *
410 * @v: pointer to register contents to modify
411 *
412 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
413 * error or 0 upon success.
414 */
415static int _set_softreset(struct omap_hwmod *oh, u32 *v)
416{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700417 u32 softrst_mask;
418
Paul Walmsley43b40992010-02-22 22:09:34 -0700419 if (!oh->class->sysc ||
420 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300421 return -EINVAL;
422
Paul Walmsley43b40992010-02-22 22:09:34 -0700423 if (!oh->class->sysc->sysc_fields) {
424 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700425 return -EINVAL;
426 }
427
Paul Walmsley43b40992010-02-22 22:09:34 -0700428 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700429
430 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300431
432 return 0;
433}
434
435/**
Roger Quadros313a76e2013-12-08 18:39:02 -0700436 * _clear_softreset: clear OCP_SYSCONFIG.SOFTRESET bit in @v
437 * @oh: struct omap_hwmod *
438 * @v: pointer to register contents to modify
439 *
440 * Clear the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
441 * error or 0 upon success.
442 */
443static int _clear_softreset(struct omap_hwmod *oh, u32 *v)
444{
445 u32 softrst_mask;
446
447 if (!oh->class->sysc ||
448 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
449 return -EINVAL;
450
451 if (!oh->class->sysc->sysc_fields) {
452 WARN(1,
453 "omap_hwmod: %s: sysc_fields absent for sysconfig class\n",
454 oh->name);
455 return -EINVAL;
456 }
457
458 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
459
460 *v &= ~softrst_mask;
461
462 return 0;
463}
464
465/**
Tero Kristo613ad0e2012-10-29 22:02:13 -0600466 * _wait_softreset_complete - wait for an OCP softreset to complete
467 * @oh: struct omap_hwmod * to wait on
468 *
469 * Wait until the IP block represented by @oh reports that its OCP
470 * softreset is complete. This can be triggered by software (see
471 * _ocp_softreset()) or by hardware upon returning from off-mode (one
472 * example is HSMMC). Waits for up to MAX_MODULE_SOFTRESET_WAIT
473 * microseconds. Returns the number of microseconds waited.
474 */
475static int _wait_softreset_complete(struct omap_hwmod *oh)
476{
477 struct omap_hwmod_class_sysconfig *sysc;
478 u32 softrst_mask;
479 int c = 0;
480
481 sysc = oh->class->sysc;
482
483 if (sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
484 omap_test_timeout((omap_hwmod_read(oh, sysc->syss_offs)
485 & SYSS_RESETDONE_MASK),
486 MAX_MODULE_SOFTRESET_WAIT, c);
487 else if (sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
488 softrst_mask = (0x1 << sysc->sysc_fields->srst_shift);
489 omap_test_timeout(!(omap_hwmod_read(oh, sysc->sysc_offs)
490 & softrst_mask),
491 MAX_MODULE_SOFTRESET_WAIT, c);
492 }
493
494 return c;
495}
496
497/**
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -0600498 * _set_dmadisable: set OCP_SYSCONFIG.DMADISABLE bit in @v
499 * @oh: struct omap_hwmod *
500 *
501 * The DMADISABLE bit is a semi-automatic bit present in sysconfig register
502 * of some modules. When the DMA must perform read/write accesses, the
503 * DMADISABLE bit is cleared by the hardware. But when the DMA must stop
504 * for power management, software must set the DMADISABLE bit back to 1.
505 *
506 * Set the DMADISABLE bit in @v for hwmod @oh. Returns -EINVAL upon
507 * error or 0 upon success.
508 */
509static int _set_dmadisable(struct omap_hwmod *oh)
510{
511 u32 v;
512 u32 dmadisable_mask;
513
514 if (!oh->class->sysc ||
515 !(oh->class->sysc->sysc_flags & SYSC_HAS_DMADISABLE))
516 return -EINVAL;
517
518 if (!oh->class->sysc->sysc_fields) {
519 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
520 return -EINVAL;
521 }
522
523 /* clocks must be on for this operation */
524 if (oh->_state != _HWMOD_STATE_ENABLED) {
525 pr_warn("omap_hwmod: %s: dma can be disabled only from enabled state\n", oh->name);
526 return -EINVAL;
527 }
528
529 pr_debug("omap_hwmod: %s: setting DMADISABLE\n", oh->name);
530
531 v = oh->_sysc_cache;
532 dmadisable_mask =
533 (0x1 << oh->class->sysc->sysc_fields->dmadisable_shift);
534 v |= dmadisable_mask;
535 _write_sysconfig(v, oh);
536
537 return 0;
538}
539
540/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700541 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
542 * @oh: struct omap_hwmod *
543 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
544 * @v: pointer to register contents to modify
545 *
546 * Update the module autoidle bit in @v to be @autoidle for the @oh
547 * hwmod. The autoidle bit controls whether the module can gate
548 * internal clocks automatically when it isn't doing anything; the
549 * exact function of this bit varies on a per-module basis. This
550 * function does not write to the hardware. Returns -EINVAL upon
551 * error or 0 upon success.
552 */
553static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
554 u32 *v)
555{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700556 u32 autoidle_mask;
557 u8 autoidle_shift;
558
Paul Walmsley43b40992010-02-22 22:09:34 -0700559 if (!oh->class->sysc ||
560 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700561 return -EINVAL;
562
Paul Walmsley43b40992010-02-22 22:09:34 -0700563 if (!oh->class->sysc->sysc_fields) {
564 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700565 return -EINVAL;
566 }
567
Paul Walmsley43b40992010-02-22 22:09:34 -0700568 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700569 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700570
571 *v &= ~autoidle_mask;
572 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700573
574 return 0;
575}
576
577/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300578 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
579 * @oh: struct omap_hwmod *
580 *
581 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
582 * upon error or 0 upon success.
583 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700584static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300585{
Paul Walmsley43b40992010-02-22 22:09:34 -0700586 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700587 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200588 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
589 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300590 return -EINVAL;
591
Paul Walmsley43b40992010-02-22 22:09:34 -0700592 if (!oh->class->sysc->sysc_fields) {
593 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700594 return -EINVAL;
595 }
596
Benoit Cousson1fe74112011-07-01 22:54:03 +0200597 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
598 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300599
Benoit Cousson86009eb2010-12-21 21:31:28 -0700600 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
601 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200602 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
603 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700604
Paul Walmsley63c85232009-09-03 20:14:03 +0300605 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
606
Paul Walmsley63c85232009-09-03 20:14:03 +0300607 return 0;
608}
609
610/**
611 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
612 * @oh: struct omap_hwmod *
613 *
614 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
615 * upon error or 0 upon success.
616 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700617static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300618{
Paul Walmsley43b40992010-02-22 22:09:34 -0700619 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700620 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200621 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
622 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300623 return -EINVAL;
624
Paul Walmsley43b40992010-02-22 22:09:34 -0700625 if (!oh->class->sysc->sysc_fields) {
626 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700627 return -EINVAL;
628 }
629
Benoit Cousson1fe74112011-07-01 22:54:03 +0200630 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
631 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300632
Benoit Cousson86009eb2010-12-21 21:31:28 -0700633 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
634 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200635 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
Djamil Elaidi561038f2012-06-17 11:57:51 -0600636 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700637
Paul Walmsley63c85232009-09-03 20:14:03 +0300638 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
639
Paul Walmsley63c85232009-09-03 20:14:03 +0300640 return 0;
641}
642
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700643static struct clockdomain *_get_clkdm(struct omap_hwmod *oh)
644{
Rajendra Nayakc4a1ea22012-04-27 16:32:53 +0530645 struct clk_hw_omap *clk;
646
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700647 if (oh->clkdm) {
648 return oh->clkdm;
649 } else if (oh->_clk) {
Tero Kristo924f9492013-07-12 12:26:41 +0300650 if (__clk_get_flags(oh->_clk) & CLK_IS_BASIC)
651 return NULL;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700652 clk = to_clk_hw_omap(__clk_get_hw(oh->_clk));
653 return clk->clkdm;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700654 }
655 return NULL;
656}
657
Paul Walmsley63c85232009-09-03 20:14:03 +0300658/**
659 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
660 * @oh: struct omap_hwmod *
661 *
662 * Prevent the hardware module @oh from entering idle while the
663 * hardare module initiator @init_oh is active. Useful when a module
664 * will be accessed by a particular initiator (e.g., if a module will
665 * be accessed by the IVA, there should be a sleepdep between the IVA
666 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700667 * mode. If the clockdomain is marked as not needing autodeps, return
668 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
669 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300670 */
671static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
672{
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700673 struct clockdomain *clkdm, *init_clkdm;
674
675 clkdm = _get_clkdm(oh);
676 init_clkdm = _get_clkdm(init_oh);
677
678 if (!clkdm || !init_clkdm)
Paul Walmsley63c85232009-09-03 20:14:03 +0300679 return -EINVAL;
680
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700681 if (clkdm && clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsley570b54c2011-03-10 03:50:09 -0700682 return 0;
683
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700684 return clkdm_add_sleepdep(clkdm, init_clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300685}
686
687/**
688 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
689 * @oh: struct omap_hwmod *
690 *
691 * Allow the hardware module @oh to enter idle while the hardare
692 * module initiator @init_oh is active. Useful when a module will not
693 * be accessed by a particular initiator (e.g., if a module will not
694 * be accessed by the IVA, there should be no sleepdep between the IVA
695 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700696 * mode. If the clockdomain is marked as not needing autodeps, return
697 * 0 without doing anything. Returns -EINVAL upon error or passes
698 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300699 */
700static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
701{
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700702 struct clockdomain *clkdm, *init_clkdm;
703
704 clkdm = _get_clkdm(oh);
705 init_clkdm = _get_clkdm(init_oh);
706
707 if (!clkdm || !init_clkdm)
Paul Walmsley63c85232009-09-03 20:14:03 +0300708 return -EINVAL;
709
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700710 if (clkdm && clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsley570b54c2011-03-10 03:50:09 -0700711 return 0;
712
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700713 return clkdm_del_sleepdep(clkdm, init_clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300714}
715
716/**
717 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
718 * @oh: struct omap_hwmod *
719 *
720 * Called from _init_clocks(). Populates the @oh _clk (main
Tero Kristo9fabc1a2016-07-04 14:11:48 +0300721 * functional clock pointer) if a clock matching the hwmod name is found,
722 * or a main_clk is present. Returns 0 on success or -EINVAL on error.
Paul Walmsley63c85232009-09-03 20:14:03 +0300723 */
724static int _init_main_clk(struct omap_hwmod *oh)
725{
Paul Walmsley63c85232009-09-03 20:14:03 +0300726 int ret = 0;
Tero Kristo9fabc1a2016-07-04 14:11:48 +0300727 char name[MOD_CLK_MAX_NAME_LEN];
728 struct clk *clk;
Maninder Singh5066d522016-12-08 09:40:30 +0530729 static const char modck[] = "_mod_ck";
Paul Walmsley63c85232009-09-03 20:14:03 +0300730
Maninder Singh5066d522016-12-08 09:40:30 +0530731 if (strlen(oh->name) >= MOD_CLK_MAX_NAME_LEN - strlen(modck))
Tero Kristo9fabc1a2016-07-04 14:11:48 +0300732 pr_warn("%s: warning: cropping name for %s\n", __func__,
733 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +0300734
Maninder Singh5066d522016-12-08 09:40:30 +0530735 strlcpy(name, oh->name, MOD_CLK_MAX_NAME_LEN - strlen(modck));
736 strlcat(name, modck, MOD_CLK_MAX_NAME_LEN);
Tero Kristo9fabc1a2016-07-04 14:11:48 +0300737
738 clk = clk_get(NULL, name);
739 if (!IS_ERR(clk)) {
740 oh->_clk = clk;
741 soc_ops.disable_direct_prcm(oh);
742 oh->main_clk = kstrdup(name, GFP_KERNEL);
743 } else {
744 if (!oh->main_clk)
745 return 0;
746
747 oh->_clk = clk_get(NULL, oh->main_clk);
748 }
749
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600750 if (IS_ERR(oh->_clk)) {
Joe Perches3d0cb732014-09-13 11:31:16 -0700751 pr_warn("omap_hwmod: %s: cannot clk_get main_clk %s\n",
752 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600753 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600754 }
Rajendra Nayak4d7cb452012-09-22 02:24:16 -0600755 /*
756 * HACK: This needs a re-visit once clk_prepare() is implemented
757 * to do something meaningful. Today its just a no-op.
758 * If clk_prepare() is used at some point to do things like
759 * voltage scaling etc, then this would have to be moved to
760 * some point where subsystems like i2c and pmic become
761 * available.
762 */
763 clk_prepare(oh->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300764
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700765 if (!_get_clkdm(oh))
Paul Walmsley3bb05db2012-09-23 17:28:18 -0600766 pr_debug("omap_hwmod: %s: missing clockdomain for %s.\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600767 oh->name, oh->main_clk);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700768
Paul Walmsley63c85232009-09-03 20:14:03 +0300769 return ret;
770}
771
772/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600773 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300774 * @oh: struct omap_hwmod *
775 *
776 * Called from _init_clocks(). Populates the @oh OCP slave interface
777 * clock pointers. Returns 0 on success or -EINVAL on error.
778 */
779static int _init_interface_clks(struct omap_hwmod *oh)
780{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600781 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600782 struct list_head *p;
Paul Walmsley63c85232009-09-03 20:14:03 +0300783 struct clk *c;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600784 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300785 int ret = 0;
786
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600787 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600788
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600789 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600790 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700791 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300792 continue;
793
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600794 c = clk_get(NULL, os->clk);
795 if (IS_ERR(c)) {
Joe Perches3d0cb732014-09-13 11:31:16 -0700796 pr_warn("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
797 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300798 ret = -EINVAL;
Nishanth Menon0e7dc862013-12-08 18:39:03 -0700799 continue;
Benoit Coussondc759252010-06-23 18:15:12 -0600800 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300801 os->_clk = c;
Rajendra Nayak4d7cb452012-09-22 02:24:16 -0600802 /*
803 * HACK: This needs a re-visit once clk_prepare() is implemented
804 * to do something meaningful. Today its just a no-op.
805 * If clk_prepare() is used at some point to do things like
806 * voltage scaling etc, then this would have to be moved to
807 * some point where subsystems like i2c and pmic become
808 * available.
809 */
810 clk_prepare(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300811 }
812
813 return ret;
814}
815
816/**
817 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
818 * @oh: struct omap_hwmod *
819 *
820 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
821 * clock pointers. Returns 0 on success or -EINVAL on error.
822 */
823static int _init_opt_clks(struct omap_hwmod *oh)
824{
825 struct omap_hwmod_opt_clk *oc;
826 struct clk *c;
827 int i;
828 int ret = 0;
829
830 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600831 c = clk_get(NULL, oc->clk);
832 if (IS_ERR(c)) {
Joe Perches3d0cb732014-09-13 11:31:16 -0700833 pr_warn("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
834 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300835 ret = -EINVAL;
Nishanth Menon0e7dc862013-12-08 18:39:03 -0700836 continue;
Benoit Coussondc759252010-06-23 18:15:12 -0600837 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300838 oc->_clk = c;
Rajendra Nayak4d7cb452012-09-22 02:24:16 -0600839 /*
840 * HACK: This needs a re-visit once clk_prepare() is implemented
841 * to do something meaningful. Today its just a no-op.
842 * If clk_prepare() is used at some point to do things like
843 * voltage scaling etc, then this would have to be moved to
844 * some point where subsystems like i2c and pmic become
845 * available.
846 */
847 clk_prepare(oc->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300848 }
849
850 return ret;
851}
852
Peter Ujfalusic12ba8c2015-11-12 09:32:58 +0200853static void _enable_optional_clocks(struct omap_hwmod *oh)
854{
855 struct omap_hwmod_opt_clk *oc;
856 int i;
857
858 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
859
860 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
861 if (oc->_clk) {
862 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
863 __clk_get_name(oc->_clk));
864 clk_enable(oc->_clk);
865 }
866}
867
868static void _disable_optional_clocks(struct omap_hwmod *oh)
869{
870 struct omap_hwmod_opt_clk *oc;
871 int i;
872
873 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
874
875 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
876 if (oc->_clk) {
877 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
878 __clk_get_name(oc->_clk));
879 clk_disable(oc->_clk);
880 }
881}
882
Paul Walmsley63c85232009-09-03 20:14:03 +0300883/**
884 * _enable_clocks - enable hwmod main clock and interface clocks
885 * @oh: struct omap_hwmod *
886 *
887 * Enables all clocks necessary for register reads and writes to succeed
888 * on the hwmod @oh. Returns 0.
889 */
890static int _enable_clocks(struct omap_hwmod *oh)
891{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600892 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600893 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600894 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300895
896 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
897
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600898 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300899 clk_enable(oh->_clk);
900
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600901 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600902
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600903 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600904 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300905
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600906 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
907 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300908 }
909
Peter Ujfalusic12ba8c2015-11-12 09:32:58 +0200910 if (oh->flags & HWMOD_OPT_CLKS_NEEDED)
911 _enable_optional_clocks(oh);
912
Paul Walmsley63c85232009-09-03 20:14:03 +0300913 /* The opt clocks are controlled by the device driver. */
914
915 return 0;
916}
917
918/**
919 * _disable_clocks - disable hwmod main clock and interface clocks
920 * @oh: struct omap_hwmod *
921 *
922 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
923 */
924static int _disable_clocks(struct omap_hwmod *oh)
925{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600926 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600927 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600928 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300929
930 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
931
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600932 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300933 clk_disable(oh->_clk);
934
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600935 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600936
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600937 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600938 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300939
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600940 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
941 clk_disable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300942 }
943
Peter Ujfalusic12ba8c2015-11-12 09:32:58 +0200944 if (oh->flags & HWMOD_OPT_CLKS_NEEDED)
945 _disable_optional_clocks(oh);
946
Paul Walmsley63c85232009-09-03 20:14:03 +0300947 /* The opt clocks are controlled by the device driver. */
948
949 return 0;
950}
951
952/**
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600953 * _omap4_enable_module - enable CLKCTRL modulemode on OMAP4
Benoit Cousson45c38252011-07-10 05:56:33 -0600954 * @oh: struct omap_hwmod *
955 *
956 * Enables the PRCM module mode related to the hwmod @oh.
957 * No return value.
958 */
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600959static void _omap4_enable_module(struct omap_hwmod *oh)
Benoit Cousson45c38252011-07-10 05:56:33 -0600960{
Benoit Cousson45c38252011-07-10 05:56:33 -0600961 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
962 return;
963
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600964 pr_debug("omap_hwmod: %s: %s: %d\n",
965 oh->name, __func__, oh->prcm.omap4.modulemode);
Benoit Cousson45c38252011-07-10 05:56:33 -0600966
Tero Kristo128603f2014-10-27 08:39:24 -0700967 omap_cm_module_enable(oh->prcm.omap4.modulemode,
968 oh->clkdm->prcm_partition,
969 oh->clkdm->cm_inst, oh->prcm.omap4.clkctrl_offs);
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -0600970}
971
972/**
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800973 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
974 * @oh: struct omap_hwmod *
975 *
976 * Wait for a module @oh to enter slave idle. Returns 0 if the module
977 * does not have an IDLEST bit or if the module successfully enters
978 * slave idle; otherwise, pass along the return value of the
979 * appropriate *_cm*_wait_module_idle() function.
980 */
981static int _omap4_wait_target_disable(struct omap_hwmod *oh)
982{
Paul Walmsley2b026d12012-09-23 17:28:18 -0600983 if (!oh)
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800984 return -EINVAL;
985
Paul Walmsley2b026d12012-09-23 17:28:18 -0600986 if (oh->_int_flags & _HWMOD_NO_MPU_PORT || !oh->clkdm)
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800987 return 0;
988
989 if (oh->flags & HWMOD_NO_IDLEST)
990 return 0;
991
Dave Gerlach428929c2016-07-12 12:50:33 -0500992 if (!oh->prcm.omap4.clkctrl_offs &&
993 !(oh->prcm.omap4.flags & HWMOD_OMAP4_ZERO_CLKCTRL_OFFSET))
994 return 0;
995
Tero Kristoa8ae5af2014-10-27 08:39:23 -0700996 return omap_cm_wait_module_idle(oh->clkdm->prcm_partition,
997 oh->clkdm->cm_inst,
998 oh->prcm.omap4.clkctrl_offs, 0);
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -0600999}
1000
1001/**
Paul Walmsley212738a2011-07-09 19:14:06 -06001002 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
1003 * @oh: struct omap_hwmod *oh
1004 *
1005 * Count and return the number of MPU IRQs associated with the hwmod
1006 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
1007 * NULL.
1008 */
1009static int _count_mpu_irqs(struct omap_hwmod *oh)
1010{
1011 struct omap_hwmod_irq_info *ohii;
1012 int i = 0;
1013
1014 if (!oh || !oh->mpu_irqs)
1015 return 0;
1016
1017 do {
1018 ohii = &oh->mpu_irqs[i++];
1019 } while (ohii->irq != -1);
1020
sricharancc1b0762011-11-23 14:35:07 -08001021 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -06001022}
1023
1024/**
Paul Walmsleybc614952011-07-09 19:14:07 -06001025 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
1026 * @oh: struct omap_hwmod *oh
1027 *
1028 * Count and return the number of SDMA request lines associated with
1029 * the hwmod @oh. Used to allocate struct resource data. Returns 0
1030 * if @oh is NULL.
1031 */
1032static int _count_sdma_reqs(struct omap_hwmod *oh)
1033{
1034 struct omap_hwmod_dma_info *ohdi;
1035 int i = 0;
1036
1037 if (!oh || !oh->sdma_reqs)
1038 return 0;
1039
1040 do {
1041 ohdi = &oh->sdma_reqs[i++];
1042 } while (ohdi->dma_req != -1);
1043
sricharancc1b0762011-11-23 14:35:07 -08001044 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -06001045}
1046
1047/**
Paul Walmsley78183f32011-07-09 19:14:05 -06001048 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
1049 * @oh: struct omap_hwmod *oh
1050 *
1051 * Count and return the number of address space ranges associated with
1052 * the hwmod @oh. Used to allocate struct resource data. Returns 0
1053 * if @oh is NULL.
1054 */
1055static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
1056{
1057 struct omap_hwmod_addr_space *mem;
1058 int i = 0;
1059
1060 if (!os || !os->addr)
1061 return 0;
1062
1063 do {
1064 mem = &os->addr[i++];
1065 } while (mem->pa_start != mem->pa_end);
1066
sricharancc1b0762011-11-23 14:35:07 -08001067 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001068}
1069
1070/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001071 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
1072 * @oh: struct omap_hwmod * to operate on
1073 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
1074 * @irq: pointer to an unsigned int to store the MPU IRQ number to
1075 *
1076 * Retrieve a MPU hardware IRQ line number named by @name associated
1077 * with the IP block pointed to by @oh. The IRQ number will be filled
1078 * into the address pointed to by @dma. When @name is non-null, the
1079 * IRQ line number associated with the named entry will be returned.
1080 * If @name is null, the first matching entry will be returned. Data
1081 * order is not meaningful in hwmod data, so callers are strongly
1082 * encouraged to use a non-null @name whenever possible to avoid
1083 * unpredictable effects if hwmod data is later added that causes data
1084 * ordering to change. Returns 0 upon success or a negative error
1085 * code upon error.
1086 */
1087static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
1088 unsigned int *irq)
1089{
1090 int i;
1091 bool found = false;
1092
1093 if (!oh->mpu_irqs)
1094 return -ENOENT;
1095
1096 i = 0;
1097 while (oh->mpu_irqs[i].irq != -1) {
1098 if (name == oh->mpu_irqs[i].name ||
1099 !strcmp(name, oh->mpu_irqs[i].name)) {
1100 found = true;
1101 break;
1102 }
1103 i++;
1104 }
1105
1106 if (!found)
1107 return -ENOENT;
1108
1109 *irq = oh->mpu_irqs[i].irq;
1110
1111 return 0;
1112}
1113
1114/**
1115 * _get_sdma_req_by_name - fetch SDMA request line ID by name
1116 * @oh: struct omap_hwmod * to operate on
1117 * @name: pointer to the name of the SDMA request line to fetch (optional)
1118 * @dma: pointer to an unsigned int to store the request line ID to
1119 *
1120 * Retrieve an SDMA request line ID named by @name on the IP block
1121 * pointed to by @oh. The ID will be filled into the address pointed
1122 * to by @dma. When @name is non-null, the request line ID associated
1123 * with the named entry will be returned. If @name is null, the first
1124 * matching entry will be returned. Data order is not meaningful in
1125 * hwmod data, so callers are strongly encouraged to use a non-null
1126 * @name whenever possible to avoid unpredictable effects if hwmod
1127 * data is later added that causes data ordering to change. Returns 0
1128 * upon success or a negative error code upon error.
1129 */
1130static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
1131 unsigned int *dma)
1132{
1133 int i;
1134 bool found = false;
1135
1136 if (!oh->sdma_reqs)
1137 return -ENOENT;
1138
1139 i = 0;
1140 while (oh->sdma_reqs[i].dma_req != -1) {
1141 if (name == oh->sdma_reqs[i].name ||
1142 !strcmp(name, oh->sdma_reqs[i].name)) {
1143 found = true;
1144 break;
1145 }
1146 i++;
1147 }
1148
1149 if (!found)
1150 return -ENOENT;
1151
1152 *dma = oh->sdma_reqs[i].dma_req;
1153
1154 return 0;
1155}
1156
1157/**
1158 * _get_addr_space_by_name - fetch address space start & end by name
1159 * @oh: struct omap_hwmod * to operate on
1160 * @name: pointer to the name of the address space to fetch (optional)
1161 * @pa_start: pointer to a u32 to store the starting address to
1162 * @pa_end: pointer to a u32 to store the ending address to
1163 *
1164 * Retrieve address space start and end addresses for the IP block
1165 * pointed to by @oh. The data will be filled into the addresses
1166 * pointed to by @pa_start and @pa_end. When @name is non-null, the
1167 * address space data associated with the named entry will be
1168 * returned. If @name is null, the first matching entry will be
1169 * returned. Data order is not meaningful in hwmod data, so callers
1170 * are strongly encouraged to use a non-null @name whenever possible
1171 * to avoid unpredictable effects if hwmod data is later added that
1172 * causes data ordering to change. Returns 0 upon success or a
1173 * negative error code upon error.
1174 */
1175static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1176 u32 *pa_start, u32 *pa_end)
1177{
1178 int i, j;
1179 struct omap_hwmod_ocp_if *os;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001180 struct list_head *p = NULL;
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001181 bool found = false;
1182
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001183 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001184
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001185 i = 0;
1186 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001187 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001188
1189 if (!os->addr)
1190 return -ENOENT;
1191
1192 j = 0;
1193 while (os->addr[j].pa_start != os->addr[j].pa_end) {
1194 if (name == os->addr[j].name ||
1195 !strcmp(name, os->addr[j].name)) {
1196 found = true;
1197 break;
1198 }
1199 j++;
1200 }
1201
1202 if (found)
1203 break;
1204 }
1205
1206 if (!found)
1207 return -ENOENT;
1208
1209 *pa_start = os->addr[j].pa_start;
1210 *pa_end = os->addr[j].pa_end;
1211
1212 return 0;
1213}
1214
1215/**
Paul Walmsley24dbc212012-04-19 04:04:29 -06001216 * _save_mpu_port_index - find and save the index to @oh's MPU port
Paul Walmsley63c85232009-09-03 20:14:03 +03001217 * @oh: struct omap_hwmod *
1218 *
Paul Walmsley24dbc212012-04-19 04:04:29 -06001219 * Determines the array index of the OCP slave port that the MPU uses
1220 * to address the device, and saves it into the struct omap_hwmod.
1221 * Intended to be called during hwmod registration only. No return
1222 * value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001223 */
Paul Walmsley24dbc212012-04-19 04:04:29 -06001224static void __init _save_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001225{
Paul Walmsley24dbc212012-04-19 04:04:29 -06001226 struct omap_hwmod_ocp_if *os = NULL;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001227 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001228 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001229
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001230 if (!oh)
Paul Walmsley24dbc212012-04-19 04:04:29 -06001231 return;
1232
1233 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001234
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001235 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001236
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001237 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001238 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +03001239 if (os->user & OCP_USER_MPU) {
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001240 oh->_mpu_port = os;
Paul Walmsley24dbc212012-04-19 04:04:29 -06001241 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001242 break;
1243 }
1244 }
1245
Paul Walmsley24dbc212012-04-19 04:04:29 -06001246 return;
Paul Walmsley63c85232009-09-03 20:14:03 +03001247}
1248
1249/**
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001250 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1251 * @oh: struct omap_hwmod *
1252 *
1253 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1254 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1255 * communicate with the IP block. This interface need not be directly
1256 * connected to the MPU (and almost certainly is not), but is directly
1257 * connected to the IP block represented by @oh. Returns a pointer
1258 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1259 * error or if there does not appear to be a path from the MPU to this
1260 * IP block.
1261 */
1262static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1263{
1264 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1265 return NULL;
1266
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001267 return oh->_mpu_port;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001268};
1269
1270/**
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001271 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
Paul Walmsley63c85232009-09-03 20:14:03 +03001272 * @oh: struct omap_hwmod *
1273 *
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001274 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1275 * the register target MPU address space; or returns NULL upon error.
Paul Walmsley63c85232009-09-03 20:14:03 +03001276 */
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001277static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001278{
1279 struct omap_hwmod_ocp_if *os;
1280 struct omap_hwmod_addr_space *mem;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001281 int found = 0, i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001282
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001283 os = _find_mpu_rt_port(oh);
Paul Walmsley24dbc212012-04-19 04:04:29 -06001284 if (!os || !os->addr)
Paul Walmsley78183f32011-07-09 19:14:05 -06001285 return NULL;
1286
1287 do {
1288 mem = &os->addr[i++];
1289 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +03001290 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001291 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +03001292
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001293 return (found) ? mem : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001294}
1295
1296/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001297 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001298 * @oh: struct omap_hwmod *
1299 *
Paul Walmsley006c7f12012-07-04 05:22:53 -06001300 * Ensure that the OCP_SYSCONFIG register for the IP block represented
1301 * by @oh is set to indicate to the PRCM that the IP block is active.
1302 * Usually this means placing the module into smart-idle mode and
1303 * smart-standby, but if there is a bug in the automatic idle handling
1304 * for the IP block, it may need to be placed into the force-idle or
1305 * no-idle variants of these modes. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001306 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001307static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001308{
Paul Walmsley43b40992010-02-22 22:09:34 -07001309 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001310 u32 v;
Paul Walmsley006c7f12012-07-04 05:22:53 -06001311 bool clkdm_act;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07001312 struct clockdomain *clkdm;
Paul Walmsley63c85232009-09-03 20:14:03 +03001313
Paul Walmsley43b40992010-02-22 22:09:34 -07001314 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001315 return;
1316
Tero Kristo613ad0e2012-10-29 22:02:13 -06001317 /*
1318 * Wait until reset has completed, this is needed as the IP
1319 * block is reset automatically by hardware in some cases
1320 * (off-mode for example), and the drivers require the
1321 * IP to be ready when they access it
1322 */
1323 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1324 _enable_optional_clocks(oh);
1325 _wait_softreset_complete(oh);
1326 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1327 _disable_optional_clocks(oh);
1328
Paul Walmsley63c85232009-09-03 20:14:03 +03001329 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001330 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001331
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07001332 clkdm = _get_clkdm(oh);
Paul Walmsley43b40992010-02-22 22:09:34 -07001333 if (sf & SYSC_HAS_SIDLEMODE) {
Rajendra Nayakca43ea32013-05-15 20:18:38 +05301334 if (oh->flags & HWMOD_SWSUP_SIDLE ||
1335 oh->flags & HWMOD_SWSUP_SIDLE_ACT) {
Rajendra Nayak35513172013-05-15 20:18:37 +05301336 idlemode = HWMOD_IDLEMODE_NO;
1337 } else {
1338 if (sf & SYSC_HAS_ENAWAKEUP)
1339 _enable_wakeup(oh, &v);
1340 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
1341 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1342 else
1343 idlemode = HWMOD_IDLEMODE_SMART;
1344 }
1345
1346 /*
1347 * This is special handling for some IPs like
1348 * 32k sync timer. Force them to idle!
1349 */
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07001350 clkdm_act = (clkdm && clkdm->flags & CLKDM_ACTIVE_WITH_MPU);
Paul Walmsley006c7f12012-07-04 05:22:53 -06001351 if (clkdm_act && !(oh->class->sysc->idlemodes &
1352 (SIDLE_SMART | SIDLE_SMART_WKUP)))
1353 idlemode = HWMOD_IDLEMODE_FORCE;
Rajendra Nayak35513172013-05-15 20:18:37 +05301354
Paul Walmsley63c85232009-09-03 20:14:03 +03001355 _set_slave_idlemode(oh, idlemode, &v);
1356 }
1357
Paul Walmsley43b40992010-02-22 22:09:34 -07001358 if (sf & SYSC_HAS_MIDLEMODE) {
Grazvydas Ignotas092bc082013-03-11 21:49:00 +02001359 if (oh->flags & HWMOD_FORCE_MSTANDBY) {
1360 idlemode = HWMOD_IDLEMODE_FORCE;
1361 } else if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001362 idlemode = HWMOD_IDLEMODE_NO;
1363 } else {
1364 if (sf & SYSC_HAS_ENAWAKEUP)
1365 _enable_wakeup(oh, &v);
1366 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1367 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1368 else
1369 idlemode = HWMOD_IDLEMODE_SMART;
1370 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001371 _set_master_standbymode(oh, idlemode, &v);
1372 }
1373
Paul Walmsleya16b1f72009-12-08 16:34:17 -07001374 /*
1375 * XXX The clock framework should handle this, by
1376 * calling into this code. But this must wait until the
1377 * clock structures are tagged with omap_hwmod entries
1378 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001379 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1380 (sf & SYSC_HAS_CLOCKACTIVITY))
1381 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001382
Lokesh Vutla3ca4a232016-03-26 23:08:55 -06001383 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -06001384
1385 /*
1386 * Set the autoidle bit only after setting the smartidle bit
1387 * Setting this will not have any impact on the other modules.
1388 */
1389 if (sf & SYSC_HAS_AUTOIDLE) {
1390 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1391 0 : 1;
1392 _set_module_autoidle(oh, idlemode, &v);
1393 _write_sysconfig(v, oh);
1394 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001395}
1396
1397/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001398 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001399 * @oh: struct omap_hwmod *
1400 *
1401 * If module is marked as SWSUP_SIDLE, force the module into slave
1402 * idle; otherwise, configure it for smart-idle. If module is marked
1403 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1404 * configure it for smart-standby. No return value.
1405 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001406static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001407{
Paul Walmsley43b40992010-02-22 22:09:34 -07001408 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001409 u32 v;
1410
Paul Walmsley43b40992010-02-22 22:09:34 -07001411 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001412 return;
1413
1414 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001415 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001416
Paul Walmsley43b40992010-02-22 22:09:34 -07001417 if (sf & SYSC_HAS_SIDLEMODE) {
Rajendra Nayak35513172013-05-15 20:18:37 +05301418 if (oh->flags & HWMOD_SWSUP_SIDLE) {
Paul Walmsley006c7f12012-07-04 05:22:53 -06001419 idlemode = HWMOD_IDLEMODE_FORCE;
Rajendra Nayak35513172013-05-15 20:18:37 +05301420 } else {
1421 if (sf & SYSC_HAS_ENAWAKEUP)
1422 _enable_wakeup(oh, &v);
1423 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
1424 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1425 else
1426 idlemode = HWMOD_IDLEMODE_SMART;
1427 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001428 _set_slave_idlemode(oh, idlemode, &v);
1429 }
1430
Paul Walmsley43b40992010-02-22 22:09:34 -07001431 if (sf & SYSC_HAS_MIDLEMODE) {
Grazvydas Ignotas092bc082013-03-11 21:49:00 +02001432 if ((oh->flags & HWMOD_SWSUP_MSTANDBY) ||
1433 (oh->flags & HWMOD_FORCE_MSTANDBY)) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001434 idlemode = HWMOD_IDLEMODE_FORCE;
1435 } else {
1436 if (sf & SYSC_HAS_ENAWAKEUP)
1437 _enable_wakeup(oh, &v);
1438 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1439 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1440 else
1441 idlemode = HWMOD_IDLEMODE_SMART;
1442 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001443 _set_master_standbymode(oh, idlemode, &v);
1444 }
1445
Lokesh Vutla3ca4a232016-03-26 23:08:55 -06001446 /* If the cached value is the same as the new value, skip the write */
1447 if (oh->_sysc_cache != v)
1448 _write_sysconfig(v, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001449}
1450
1451/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001452 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001453 * @oh: struct omap_hwmod *
1454 *
1455 * Force the module into slave idle and master suspend. No return
1456 * value.
1457 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001458static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001459{
1460 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001461 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001462
Paul Walmsley43b40992010-02-22 22:09:34 -07001463 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001464 return;
1465
1466 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001467 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001468
Paul Walmsley43b40992010-02-22 22:09:34 -07001469 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001470 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1471
Paul Walmsley43b40992010-02-22 22:09:34 -07001472 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001473 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1474
Paul Walmsley43b40992010-02-22 22:09:34 -07001475 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001476 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001477
1478 _write_sysconfig(v, oh);
1479}
1480
1481/**
1482 * _lookup - find an omap_hwmod by name
1483 * @name: find an omap_hwmod by name
1484 *
1485 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001486 */
1487static struct omap_hwmod *_lookup(const char *name)
1488{
1489 struct omap_hwmod *oh, *temp_oh;
1490
1491 oh = NULL;
1492
1493 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1494 if (!strcmp(name, temp_oh->name)) {
1495 oh = temp_oh;
1496 break;
1497 }
1498 }
1499
1500 return oh;
1501}
Paul Walmsley868c1572012-06-19 15:01:02 -06001502
Benoit Cousson6ae76992011-07-10 05:56:30 -06001503/**
1504 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1505 * @oh: struct omap_hwmod *
1506 *
1507 * Convert a clockdomain name stored in a struct omap_hwmod into a
1508 * clockdomain pointer, and save it into the struct omap_hwmod.
Paul Walmsley868c1572012-06-19 15:01:02 -06001509 * Return -EINVAL if the clkdm_name lookup failed.
Benoit Cousson6ae76992011-07-10 05:56:30 -06001510 */
1511static int _init_clkdm(struct omap_hwmod *oh)
1512{
Paul Walmsley3bb05db2012-09-23 17:28:18 -06001513 if (!oh->clkdm_name) {
1514 pr_debug("omap_hwmod: %s: missing clockdomain\n", oh->name);
Benoit Cousson6ae76992011-07-10 05:56:30 -06001515 return 0;
Paul Walmsley3bb05db2012-09-23 17:28:18 -06001516 }
Benoit Cousson6ae76992011-07-10 05:56:30 -06001517
Benoit Cousson6ae76992011-07-10 05:56:30 -06001518 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1519 if (!oh->clkdm) {
Joe Perches3d0cb732014-09-13 11:31:16 -07001520 pr_warn("omap_hwmod: %s: could not associate to clkdm %s\n",
Benoit Cousson6ae76992011-07-10 05:56:30 -06001521 oh->name, oh->clkdm_name);
Tero Kristo0385c582013-07-17 18:03:25 +03001522 return 0;
Benoit Cousson6ae76992011-07-10 05:56:30 -06001523 }
1524
1525 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1526 oh->name, oh->clkdm_name);
1527
1528 return 0;
1529}
Paul Walmsley63c85232009-09-03 20:14:03 +03001530
1531/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001532 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1533 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001534 * @oh: struct omap_hwmod *
Paul Walmsley97d601622010-07-26 16:34:30 -06001535 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001536 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001537 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001538 * Resolves all clock names embedded in the hwmod. Returns 0 on
1539 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001540 */
Paul Walmsley97d601622010-07-26 16:34:30 -06001541static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001542{
1543 int ret = 0;
1544
Paul Walmsley48d54f32011-02-23 00:14:07 -07001545 if (oh->_state != _HWMOD_STATE_REGISTERED)
1546 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001547
1548 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1549
Vaibhav Hiremathb797be1d2012-07-09 18:24:30 +05301550 if (soc_ops.init_clkdm)
1551 ret |= soc_ops.init_clkdm(oh);
1552
Paul Walmsley63c85232009-09-03 20:14:03 +03001553 ret |= _init_main_clk(oh);
1554 ret |= _init_interface_clks(oh);
1555 ret |= _init_opt_clks(oh);
1556
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001557 if (!ret)
1558 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001559 else
Joe Perches3d0cb732014-09-13 11:31:16 -07001560 pr_warn("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001561
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001562 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001563}
1564
1565/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001566 * _lookup_hardreset - fill register bit info for this hwmod/reset line
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001567 * @oh: struct omap_hwmod *
1568 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001569 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001570 *
1571 * Return the bit position of the reset line that match the
1572 * input name. Return -ENOENT if not found.
1573 */
Paul Walmsleya032d332012-08-03 09:21:10 -06001574static int _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1575 struct omap_hwmod_rst_info *ohri)
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001576{
1577 int i;
1578
1579 for (i = 0; i < oh->rst_lines_cnt; i++) {
1580 const char *rst_line = oh->rst_lines[i].name;
1581 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001582 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1583 ohri->st_shift = oh->rst_lines[i].st_shift;
1584 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1585 oh->name, __func__, rst_line, ohri->rst_shift,
1586 ohri->st_shift);
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001587
omar ramirezcc1226e2011-03-04 13:32:44 -07001588 return 0;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001589 }
1590 }
1591
1592 return -ENOENT;
1593}
1594
1595/**
1596 * _assert_hardreset - assert the HW reset line of submodules
1597 * contained in the hwmod module.
1598 * @oh: struct omap_hwmod *
1599 * @name: name of the reset line to lookup and assert
1600 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001601 * Some IP like dsp, ipu or iva contain processor that require an HW
1602 * reset line to be assert / deassert in order to enable fully the IP.
1603 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1604 * asserting the hardreset line on the currently-booted SoC, or passes
1605 * along the return value from _lookup_hardreset() or the SoC's
1606 * assert_hardreset code.
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001607 */
1608static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1609{
omar ramirezcc1226e2011-03-04 13:32:44 -07001610 struct omap_hwmod_rst_info ohri;
Paul Walmsleya032d332012-08-03 09:21:10 -06001611 int ret = -EINVAL;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001612
1613 if (!oh)
1614 return -EINVAL;
1615
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001616 if (!soc_ops.assert_hardreset)
1617 return -ENOSYS;
1618
omar ramirezcc1226e2011-03-04 13:32:44 -07001619 ret = _lookup_hardreset(oh, name, &ohri);
Paul Walmsleya032d332012-08-03 09:21:10 -06001620 if (ret < 0)
omar ramirezcc1226e2011-03-04 13:32:44 -07001621 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001622
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001623 ret = soc_ops.assert_hardreset(oh, &ohri);
1624
1625 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001626}
1627
1628/**
1629 * _deassert_hardreset - deassert the HW reset line of submodules contained
1630 * in the hwmod module.
1631 * @oh: struct omap_hwmod *
1632 * @name: name of the reset line to look up and deassert
1633 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001634 * Some IP like dsp, ipu or iva contain processor that require an HW
1635 * reset line to be assert / deassert in order to enable fully the IP.
1636 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1637 * deasserting the hardreset line on the currently-booted SoC, or passes
1638 * along the return value from _lookup_hardreset() or the SoC's
1639 * deassert_hardreset code.
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001640 */
1641static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1642{
omar ramirezcc1226e2011-03-04 13:32:44 -07001643 struct omap_hwmod_rst_info ohri;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001644 int ret = -EINVAL;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001645
1646 if (!oh)
1647 return -EINVAL;
1648
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001649 if (!soc_ops.deassert_hardreset)
1650 return -ENOSYS;
1651
omar ramirezcc1226e2011-03-04 13:32:44 -07001652 ret = _lookup_hardreset(oh, name, &ohri);
Russell Kingc48cd652013-03-13 20:44:21 +00001653 if (ret < 0)
omar ramirezcc1226e2011-03-04 13:32:44 -07001654 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001655
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001656 if (oh->clkdm) {
1657 /*
1658 * A clockdomain must be in SW_SUP otherwise reset
1659 * might not be completed. The clockdomain can be set
1660 * in HW_AUTO only when the module become ready.
1661 */
Tero Kristo1d9a5422016-06-30 16:15:02 +03001662 clkdm_deny_idle(oh->clkdm);
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001663 ret = clkdm_hwmod_enable(oh->clkdm, oh);
1664 if (ret) {
1665 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1666 oh->name, oh->clkdm->name, ret);
1667 return ret;
1668 }
1669 }
1670
1671 _enable_clocks(oh);
1672 if (soc_ops.enable_module)
1673 soc_ops.enable_module(oh);
1674
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001675 ret = soc_ops.deassert_hardreset(oh, &ohri);
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001676
1677 if (soc_ops.disable_module)
1678 soc_ops.disable_module(oh);
1679 _disable_clocks(oh);
1680
omar ramirezcc1226e2011-03-04 13:32:44 -07001681 if (ret == -EBUSY)
Joe Perches3d0cb732014-09-13 11:31:16 -07001682 pr_warn("omap_hwmod: %s: failed to hardreset\n", oh->name);
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001683
Tero Kristo80d25182015-02-26 18:06:00 +02001684 if (oh->clkdm) {
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001685 /*
1686 * Set the clockdomain to HW_AUTO, assuming that the
1687 * previous state was HW_AUTO.
1688 */
Tero Kristo1d9a5422016-06-30 16:15:02 +03001689 clkdm_allow_idle(oh->clkdm);
Tero Kristo80d25182015-02-26 18:06:00 +02001690
1691 clkdm_hwmod_disable(oh->clkdm, oh);
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001692 }
1693
omar ramirezcc1226e2011-03-04 13:32:44 -07001694 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001695}
1696
1697/**
1698 * _read_hardreset - read the HW reset line state of submodules
1699 * contained in the hwmod module
1700 * @oh: struct omap_hwmod *
1701 * @name: name of the reset line to look up and read
1702 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001703 * Return the state of the reset line. Returns -EINVAL if @oh is
1704 * null, -ENOSYS if we have no way of reading the hardreset line
1705 * status on the currently-booted SoC, or passes along the return
1706 * value from _lookup_hardreset() or the SoC's is_hardreset_asserted
1707 * code.
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001708 */
1709static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1710{
omar ramirezcc1226e2011-03-04 13:32:44 -07001711 struct omap_hwmod_rst_info ohri;
Paul Walmsleya032d332012-08-03 09:21:10 -06001712 int ret = -EINVAL;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001713
1714 if (!oh)
1715 return -EINVAL;
1716
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001717 if (!soc_ops.is_hardreset_asserted)
1718 return -ENOSYS;
1719
omar ramirezcc1226e2011-03-04 13:32:44 -07001720 ret = _lookup_hardreset(oh, name, &ohri);
Paul Walmsleya032d332012-08-03 09:21:10 -06001721 if (ret < 0)
omar ramirezcc1226e2011-03-04 13:32:44 -07001722 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001723
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001724 return soc_ops.is_hardreset_asserted(oh, &ohri);
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001725}
1726
1727/**
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001728 * _are_all_hardreset_lines_asserted - return true if the @oh is hard-reset
Paul Walmsley747834a2012-04-18 19:10:04 -06001729 * @oh: struct omap_hwmod *
1730 *
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001731 * If all hardreset lines associated with @oh are asserted, then return true.
1732 * Otherwise, if part of @oh is out hardreset or if no hardreset lines
1733 * associated with @oh are asserted, then return false.
Paul Walmsley747834a2012-04-18 19:10:04 -06001734 * This function is used to avoid executing some parts of the IP block
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001735 * enable/disable sequence if its hardreset line is set.
Paul Walmsley747834a2012-04-18 19:10:04 -06001736 */
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001737static bool _are_all_hardreset_lines_asserted(struct omap_hwmod *oh)
Paul Walmsley747834a2012-04-18 19:10:04 -06001738{
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001739 int i, rst_cnt = 0;
Paul Walmsley747834a2012-04-18 19:10:04 -06001740
1741 if (oh->rst_lines_cnt == 0)
1742 return false;
1743
1744 for (i = 0; i < oh->rst_lines_cnt; i++)
1745 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001746 rst_cnt++;
1747
1748 if (oh->rst_lines_cnt == rst_cnt)
1749 return true;
Paul Walmsley747834a2012-04-18 19:10:04 -06001750
1751 return false;
1752}
1753
1754/**
Paul Walmsleye9332b62012-10-08 23:08:15 -06001755 * _are_any_hardreset_lines_asserted - return true if any part of @oh is
1756 * hard-reset
1757 * @oh: struct omap_hwmod *
1758 *
1759 * If any hardreset lines associated with @oh are asserted, then
1760 * return true. Otherwise, if no hardreset lines associated with @oh
1761 * are asserted, or if @oh has no hardreset lines, then return false.
1762 * This function is used to avoid executing some parts of the IP block
1763 * enable/disable sequence if any hardreset line is set.
1764 */
1765static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1766{
1767 int rst_cnt = 0;
1768 int i;
1769
1770 for (i = 0; i < oh->rst_lines_cnt && rst_cnt == 0; i++)
1771 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1772 rst_cnt++;
1773
1774 return (rst_cnt) ? true : false;
1775}
1776
1777/**
Paul Walmsley747834a2012-04-18 19:10:04 -06001778 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1779 * @oh: struct omap_hwmod *
1780 *
1781 * Disable the PRCM module mode related to the hwmod @oh.
1782 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1783 */
1784static int _omap4_disable_module(struct omap_hwmod *oh)
1785{
1786 int v;
1787
Paul Walmsley747834a2012-04-18 19:10:04 -06001788 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1789 return -EINVAL;
1790
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001791 /*
1792 * Since integration code might still be doing something, only
1793 * disable if all lines are under hardreset.
1794 */
Paul Walmsleye9332b62012-10-08 23:08:15 -06001795 if (_are_any_hardreset_lines_asserted(oh))
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001796 return 0;
1797
Paul Walmsley747834a2012-04-18 19:10:04 -06001798 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1799
Tero Kristo128603f2014-10-27 08:39:24 -07001800 omap_cm_module_disable(oh->clkdm->prcm_partition, oh->clkdm->cm_inst,
1801 oh->prcm.omap4.clkctrl_offs);
Paul Walmsley747834a2012-04-18 19:10:04 -06001802
Paul Walmsley747834a2012-04-18 19:10:04 -06001803 v = _omap4_wait_target_disable(oh);
1804 if (v)
1805 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1806 oh->name);
1807
1808 return 0;
1809}
1810
1811/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001812 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001813 * @oh: struct omap_hwmod *
1814 *
1815 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Paul Walmsley30e105c2012-04-19 00:49:09 -06001816 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1817 * reset this way, -EINVAL if the hwmod is in the wrong state,
1818 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001819 *
1820 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001821 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001822 * use the SYSCONFIG softreset bit to provide the status.
1823 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001824 * Note that some IP like McBSP do have reset control but don't have
1825 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001826 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001827static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001828{
Tero Kristo613ad0e2012-10-29 22:02:13 -06001829 u32 v;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001830 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001831 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001832
Paul Walmsley43b40992010-02-22 22:09:34 -07001833 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001834 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley30e105c2012-04-19 00:49:09 -06001835 return -ENOENT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001836
1837 /* clocks must be on for this operation */
1838 if (oh->_state != _HWMOD_STATE_ENABLED) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001839 pr_warn("omap_hwmod: %s: reset can only be entered from enabled state\n",
1840 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001841 return -EINVAL;
1842 }
1843
Benoit Cousson96835af2010-09-21 18:57:58 +02001844 /* For some modules, all optionnal clocks need to be enabled as well */
1845 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1846 _enable_optional_clocks(oh);
1847
Paul Walmsleybd361792010-12-14 12:42:35 -07001848 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001849
1850 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001851 ret = _set_softreset(oh, &v);
1852 if (ret)
1853 goto dis_opt_clks;
Roger Quadros313a76e2013-12-08 18:39:02 -07001854
1855 _write_sysconfig(v, oh);
Illia Smyrnov01142512014-02-05 17:06:09 +02001856
1857 if (oh->class->sysc->srst_udelay)
1858 udelay(oh->class->sysc->srst_udelay);
1859
1860 c = _wait_softreset_complete(oh);
1861 if (c == MAX_MODULE_SOFTRESET_WAIT) {
Joe Perches3d0cb732014-09-13 11:31:16 -07001862 pr_warn("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1863 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Illia Smyrnov01142512014-02-05 17:06:09 +02001864 ret = -ETIMEDOUT;
1865 goto dis_opt_clks;
1866 } else {
1867 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
1868 }
1869
Roger Quadros313a76e2013-12-08 18:39:02 -07001870 ret = _clear_softreset(oh, &v);
1871 if (ret)
1872 goto dis_opt_clks;
1873
Paul Walmsley63c85232009-09-03 20:14:03 +03001874 _write_sysconfig(v, oh);
1875
Paul Walmsley63c85232009-09-03 20:14:03 +03001876 /*
1877 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1878 * _wait_target_ready() or _reset()
1879 */
1880
Benoit Cousson96835af2010-09-21 18:57:58 +02001881dis_opt_clks:
1882 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1883 _disable_optional_clocks(oh);
1884
1885 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001886}
1887
1888/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001889 * _reset - reset an omap_hwmod
1890 * @oh: struct omap_hwmod *
1891 *
Paul Walmsley30e105c2012-04-19 00:49:09 -06001892 * Resets an omap_hwmod @oh. If the module has a custom reset
1893 * function pointer defined, then call it to reset the IP block, and
1894 * pass along its return value to the caller. Otherwise, if the IP
1895 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1896 * associated with it, call a function to reset the IP block via that
1897 * method, and pass along the return value to the caller. Finally, if
1898 * the IP block has some hardreset lines associated with it, assert
1899 * all of those, but do _not_ deassert them. (This is because driver
1900 * authors have expressed an apparent requirement to control the
1901 * deassertion of the hardreset lines themselves.)
1902 *
1903 * The default software reset mechanism for most OMAP IP blocks is
1904 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1905 * hwmods cannot be reset via this method. Some are not targets and
1906 * therefore have no OCP header registers to access. Others (like the
1907 * IVA) have idiosyncratic reset sequences. So for these relatively
1908 * rare cases, custom reset code can be supplied in the struct
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -06001909 * omap_hwmod_class .reset function pointer.
1910 *
1911 * _set_dmadisable() is called to set the DMADISABLE bit so that it
1912 * does not prevent idling of the system. This is necessary for cases
1913 * where ROMCODE/BOOTLOADER uses dma and transfers control to the
1914 * kernel without disabling dma.
1915 *
1916 * Passes along the return value from either _ocp_softreset() or the
1917 * custom reset function - these must return -EINVAL if the hwmod
1918 * cannot be reset this way or if the hwmod is in the wrong state,
1919 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Paul Walmsleybd361792010-12-14 12:42:35 -07001920 */
1921static int _reset(struct omap_hwmod *oh)
1922{
Paul Walmsley30e105c2012-04-19 00:49:09 -06001923 int i, r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001924
1925 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1926
Paul Walmsley30e105c2012-04-19 00:49:09 -06001927 if (oh->class->reset) {
1928 r = oh->class->reset(oh);
1929 } else {
1930 if (oh->rst_lines_cnt > 0) {
1931 for (i = 0; i < oh->rst_lines_cnt; i++)
1932 _assert_hardreset(oh, oh->rst_lines[i].name);
1933 return 0;
1934 } else {
1935 r = _ocp_softreset(oh);
1936 if (r == -ENOENT)
1937 r = 0;
1938 }
1939 }
Paul Walmsleybd361792010-12-14 12:42:35 -07001940
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -06001941 _set_dmadisable(oh);
1942
Paul Walmsley30e105c2012-04-19 00:49:09 -06001943 /*
1944 * OCP_SYSCONFIG bits need to be reprogrammed after a
1945 * softreset. The _enable() function should be split to avoid
1946 * the rewrite of the OCP_SYSCONFIG register.
1947 */
Rajendra Nayak28008522012-03-13 22:55:23 +05301948 if (oh->class->sysc) {
1949 _update_sysc_cache(oh);
1950 _enable_sysc(oh);
1951 }
1952
Paul Walmsley30e105c2012-04-19 00:49:09 -06001953 return r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001954}
1955
1956/**
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07001957 * _omap4_update_context_lost - increment hwmod context loss counter if
1958 * hwmod context was lost, and clear hardware context loss reg
1959 * @oh: hwmod to check for context loss
1960 *
1961 * If the PRCM indicates that the hwmod @oh lost context, increment
1962 * our in-memory context loss counter, and clear the RM_*_CONTEXT
1963 * bits. No return value.
1964 */
1965static void _omap4_update_context_lost(struct omap_hwmod *oh)
1966{
1967 if (oh->prcm.omap4.flags & HWMOD_OMAP4_NO_CONTEXT_LOSS_BIT)
1968 return;
1969
1970 if (!prm_was_any_context_lost_old(oh->clkdm->pwrdm.ptr->prcm_partition,
1971 oh->clkdm->pwrdm.ptr->prcm_offs,
1972 oh->prcm.omap4.context_offs))
1973 return;
1974
1975 oh->prcm.omap4.context_lost_counter++;
1976 prm_clear_context_loss_flags_old(oh->clkdm->pwrdm.ptr->prcm_partition,
1977 oh->clkdm->pwrdm.ptr->prcm_offs,
1978 oh->prcm.omap4.context_offs);
1979}
1980
1981/**
1982 * _omap4_get_context_lost - get context loss counter for a hwmod
1983 * @oh: hwmod to get context loss counter for
1984 *
1985 * Returns the in-memory context loss counter for a hwmod.
1986 */
1987static int _omap4_get_context_lost(struct omap_hwmod *oh)
1988{
1989 return oh->prcm.omap4.context_lost_counter;
1990}
1991
1992/**
Paul Walmsley6d266f62013-02-10 11:22:22 -07001993 * _enable_preprogram - Pre-program an IP block during the _enable() process
1994 * @oh: struct omap_hwmod *
1995 *
1996 * Some IP blocks (such as AESS) require some additional programming
1997 * after enable before they can enter idle. If a function pointer to
1998 * do so is present in the hwmod data, then call it and pass along the
1999 * return value; otherwise, return 0.
2000 */
jean-philippe francois0f497032013-05-16 11:25:07 -07002001static int _enable_preprogram(struct omap_hwmod *oh)
Paul Walmsley6d266f62013-02-10 11:22:22 -07002002{
2003 if (!oh->class->enable_preprogram)
2004 return 0;
2005
2006 return oh->class->enable_preprogram(oh);
2007}
2008
2009/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002010 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03002011 * @oh: struct omap_hwmod *
2012 *
2013 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002014 * register target. Returns -EINVAL if the hwmod is in the wrong
2015 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03002016 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002017static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002018{
Paul Walmsley747834a2012-04-18 19:10:04 -06002019 int r;
Paul Walmsley63c85232009-09-03 20:14:03 +03002020
Benoit Cousson34617e22011-07-01 22:54:07 +02002021 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
2022
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002023 /*
Paul Walmsley64813c32012-04-18 19:10:03 -06002024 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
Tony Lindgrenb4281452016-10-20 06:35:21 -07002025 * state at init.
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002026 */
2027 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002028 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
2029 return 0;
2030 }
2031
Paul Walmsley63c85232009-09-03 20:14:03 +03002032 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
2033 oh->_state != _HWMOD_STATE_IDLE &&
2034 oh->_state != _HWMOD_STATE_DISABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00002035 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
2036 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002037 return -EINVAL;
2038 }
2039
Benoit Cousson31f62862011-07-01 22:54:05 +02002040 /*
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06002041 * If an IP block contains HW reset lines and all of them are
Paul Walmsley747834a2012-04-18 19:10:04 -06002042 * asserted, we let integration code associated with that
2043 * block handle the enable. We've received very little
2044 * information on what those driver authors need, and until
2045 * detailed information is provided and the driver code is
2046 * posted to the public lists, this is probably the best we
2047 * can do.
Benoit Cousson31f62862011-07-01 22:54:05 +02002048 */
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06002049 if (_are_all_hardreset_lines_asserted(oh))
Paul Walmsley747834a2012-04-18 19:10:04 -06002050 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002051
Rajendra Nayak665d0012011-07-10 05:57:07 -06002052 _add_initiator_dep(oh, mpu_oh);
2053
2054 if (oh->clkdm) {
2055 /*
2056 * A clockdomain must be in SW_SUP before enabling
2057 * completely the module. The clockdomain can be set
2058 * in HW_AUTO only when the module become ready.
2059 */
Tero Kristo1d9a5422016-06-30 16:15:02 +03002060 clkdm_deny_idle(oh->clkdm);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002061 r = clkdm_hwmod_enable(oh->clkdm, oh);
2062 if (r) {
2063 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
2064 oh->name, oh->clkdm->name, r);
2065 return r;
2066 }
Benoit Cousson34617e22011-07-01 22:54:07 +02002067 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06002068
2069 _enable_clocks(oh);
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002070 if (soc_ops.enable_module)
2071 soc_ops.enable_module(oh);
Paul Walmsleyfa200222013-01-26 00:48:56 -07002072 if (oh->flags & HWMOD_BLOCK_WFI)
Thomas Gleixnerf7b861b2013-03-21 22:49:38 +01002073 cpu_idle_poll_ctrl(true);
Benoit Cousson34617e22011-07-01 22:54:07 +02002074
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07002075 if (soc_ops.update_context_lost)
2076 soc_ops.update_context_lost(oh);
2077
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002078 r = (soc_ops.wait_target_ready) ? soc_ops.wait_target_ready(oh) :
2079 -EINVAL;
Tero Kristo1d9a5422016-06-30 16:15:02 +03002080 if (oh->clkdm)
2081 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02002082
Tero Kristo1d9a5422016-06-30 16:15:02 +03002083 if (!r) {
Rajendra Nayak665d0012011-07-10 05:57:07 -06002084 oh->_state = _HWMOD_STATE_ENABLED;
2085
2086 /* Access the sysconfig only if the target is ready */
2087 if (oh->class->sysc) {
2088 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
2089 _update_sysc_cache(oh);
2090 _enable_sysc(oh);
2091 }
Paul Walmsley6d266f62013-02-10 11:22:22 -07002092 r = _enable_preprogram(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002093 } else {
Paul Walmsley2577a4a2012-10-29 20:57:55 -06002094 if (soc_ops.disable_module)
2095 soc_ops.disable_module(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002096 _disable_clocks(oh);
Lokesh Vutla812ce9d2014-12-19 18:04:50 +05302097 pr_err("omap_hwmod: %s: _wait_target_ready failed: %d\n",
2098 oh->name, r);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002099
2100 if (oh->clkdm)
2101 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002102 }
2103
Paul Walmsley63c85232009-09-03 20:14:03 +03002104 return r;
2105}
2106
2107/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002108 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03002109 * @oh: struct omap_hwmod *
2110 *
2111 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002112 * no further work. Returns -EINVAL if the hwmod is in the wrong
2113 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03002114 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002115static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002116{
Lokesh Vutla2e18f5a2016-03-07 01:41:21 -07002117 if (oh->flags & HWMOD_NO_IDLE) {
2118 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
2119 return 0;
2120 }
2121
Benoit Cousson34617e22011-07-01 22:54:07 +02002122 pr_debug("omap_hwmod: %s: idling\n", oh->name);
2123
Suman Annac20c8f72016-04-10 13:20:11 -06002124 if (_are_all_hardreset_lines_asserted(oh))
2125 return 0;
2126
Paul Walmsley63c85232009-09-03 20:14:03 +03002127 if (oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00002128 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
2129 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002130 return -EINVAL;
2131 }
2132
Paul Walmsley43b40992010-02-22 22:09:34 -07002133 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002134 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002135 _del_initiator_dep(oh, mpu_oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08002136
Tero Kristo1d9a5422016-06-30 16:15:02 +03002137 if (oh->clkdm)
2138 clkdm_deny_idle(oh->clkdm);
2139
Paul Walmsleyfa200222013-01-26 00:48:56 -07002140 if (oh->flags & HWMOD_BLOCK_WFI)
Thomas Gleixnerf7b861b2013-03-21 22:49:38 +01002141 cpu_idle_poll_ctrl(false);
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002142 if (soc_ops.disable_module)
2143 soc_ops.disable_module(oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08002144
Benoit Cousson45c38252011-07-10 05:56:33 -06002145 /*
2146 * The module must be in idle mode before disabling any parents
2147 * clocks. Otherwise, the parent clock might be disabled before
2148 * the module transition is done, and thus will prevent the
2149 * transition to complete properly.
2150 */
2151 _disable_clocks(oh);
Tero Kristo1d9a5422016-06-30 16:15:02 +03002152 if (oh->clkdm) {
2153 clkdm_allow_idle(oh->clkdm);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002154 clkdm_hwmod_disable(oh->clkdm, oh);
Tero Kristo1d9a5422016-06-30 16:15:02 +03002155 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002156
2157 oh->_state = _HWMOD_STATE_IDLE;
2158
2159 return 0;
2160}
2161
2162/**
2163 * _shutdown - shutdown an omap_hwmod
2164 * @oh: struct omap_hwmod *
2165 *
2166 * Shut down an omap_hwmod @oh. This should be called when the driver
2167 * used for the hwmod is removed or unloaded or if the driver is not
2168 * used by the system. Returns -EINVAL if the hwmod is in the wrong
2169 * state or returns 0.
2170 */
2171static int _shutdown(struct omap_hwmod *oh)
2172{
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06002173 int ret, i;
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002174 u8 prev_state;
2175
Suman Annac20c8f72016-04-10 13:20:11 -06002176 if (_are_all_hardreset_lines_asserted(oh))
2177 return 0;
2178
Paul Walmsley63c85232009-09-03 20:14:03 +03002179 if (oh->_state != _HWMOD_STATE_IDLE &&
2180 oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00002181 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
2182 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002183 return -EINVAL;
2184 }
2185
2186 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
2187
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002188 if (oh->class->pre_shutdown) {
2189 prev_state = oh->_state;
2190 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002191 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002192 ret = oh->class->pre_shutdown(oh);
2193 if (ret) {
2194 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002195 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002196 return ret;
2197 }
2198 }
2199
Miguel Vadillo6481c732011-07-01 22:54:02 +02002200 if (oh->class->sysc) {
2201 if (oh->_state == _HWMOD_STATE_IDLE)
2202 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002203 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02002204 }
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06002205
Benoit Cousson3827f942010-09-21 10:34:08 -06002206 /* clocks and deps are already disabled in idle */
2207 if (oh->_state == _HWMOD_STATE_ENABLED) {
2208 _del_initiator_dep(oh, mpu_oh);
2209 /* XXX what about the other system initiators here? dma, dsp */
Paul Walmsleyfa200222013-01-26 00:48:56 -07002210 if (oh->flags & HWMOD_BLOCK_WFI)
Thomas Gleixnerf7b861b2013-03-21 22:49:38 +01002211 cpu_idle_poll_ctrl(false);
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002212 if (soc_ops.disable_module)
2213 soc_ops.disable_module(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06002214 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002215 if (oh->clkdm)
2216 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06002217 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002218 /* XXX Should this code also force-disable the optional clocks? */
2219
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06002220 for (i = 0; i < oh->rst_lines_cnt; i++)
2221 _assert_hardreset(oh, oh->rst_lines[i].name);
Benoit Cousson31f62862011-07-01 22:54:05 +02002222
Paul Walmsley63c85232009-09-03 20:14:03 +03002223 oh->_state = _HWMOD_STATE_DISABLED;
2224
2225 return 0;
2226}
2227
Tony Lindgren5e863c52013-12-06 14:20:16 -08002228static int of_dev_find_hwmod(struct device_node *np,
2229 struct omap_hwmod *oh)
2230{
2231 int count, i, res;
2232 const char *p;
2233
2234 count = of_property_count_strings(np, "ti,hwmods");
2235 if (count < 1)
2236 return -ENODEV;
2237
2238 for (i = 0; i < count; i++) {
2239 res = of_property_read_string_index(np, "ti,hwmods",
2240 i, &p);
2241 if (res)
2242 continue;
2243 if (!strcmp(p, oh->name)) {
2244 pr_debug("omap_hwmod: dt %s[%i] uses hwmod %s\n",
2245 np->name, i, oh->name);
2246 return i;
2247 }
2248 }
2249
2250 return -ENODEV;
2251}
2252
Paul Walmsley63c85232009-09-03 20:14:03 +03002253/**
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302254 * of_dev_hwmod_lookup - look up needed hwmod from dt blob
2255 * @np: struct device_node *
2256 * @oh: struct omap_hwmod *
Tony Lindgren5e863c52013-12-06 14:20:16 -08002257 * @index: index of the entry found
2258 * @found: struct device_node * found or NULL
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302259 *
2260 * Parse the dt blob and find out needed hwmod. Recursive function is
2261 * implemented to take care hierarchical dt blob parsing.
Tony Lindgren5e863c52013-12-06 14:20:16 -08002262 * Return: Returns 0 on success, -ENODEV when not found.
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302263 */
Tony Lindgren5e863c52013-12-06 14:20:16 -08002264static int of_dev_hwmod_lookup(struct device_node *np,
2265 struct omap_hwmod *oh,
2266 int *index,
2267 struct device_node **found)
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302268{
Tony Lindgren5e863c52013-12-06 14:20:16 -08002269 struct device_node *np0 = NULL;
2270 int res;
2271
2272 res = of_dev_find_hwmod(np, oh);
2273 if (res >= 0) {
2274 *found = np;
2275 *index = res;
2276 return 0;
2277 }
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302278
2279 for_each_child_of_node(np, np0) {
Tony Lindgren5e863c52013-12-06 14:20:16 -08002280 struct device_node *fc;
2281 int i;
2282
2283 res = of_dev_hwmod_lookup(np0, oh, &i, &fc);
2284 if (res == 0) {
2285 *found = fc;
2286 *index = i;
2287 return 0;
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302288 }
2289 }
Tony Lindgren5e863c52013-12-06 14:20:16 -08002290
2291 *found = NULL;
2292 *index = 0;
2293
2294 return -ENODEV;
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302295}
2296
2297/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002298 * _init_mpu_rt_base - populate the virtual address for a hwmod
2299 * @oh: struct omap_hwmod * to locate the virtual address
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002300 * @data: (unused, caller should pass NULL)
Tony Lindgren5e863c52013-12-06 14:20:16 -08002301 * @index: index of the reg entry iospace in device tree
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002302 * @np: struct device_node * of the IP block's device node in the DT data
Paul Walmsley381d0332012-04-19 00:58:22 -06002303 *
2304 * Cache the virtual address used by the MPU to access this IP block's
2305 * registers. This address is needed early so the OCP registers that
2306 * are part of the device's address space can be ioremapped properly.
Suman Anna6423d6d2013-10-08 23:46:49 -06002307 *
Roger Quadros9a258af2015-07-16 16:16:44 +03002308 * If SYSC access is not needed, the registers will not be remapped
2309 * and non-availability of MPU access is not treated as an error.
2310 *
Suman Anna6423d6d2013-10-08 23:46:49 -06002311 * Returns 0 on success, -EINVAL if an invalid hwmod is passed, and
2312 * -ENXIO on absent or invalid register target address space.
Paul Walmsley381d0332012-04-19 00:58:22 -06002313 */
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002314static int __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data,
Tony Lindgren5e863c52013-12-06 14:20:16 -08002315 int index, struct device_node *np)
Paul Walmsley381d0332012-04-19 00:58:22 -06002316{
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002317 struct omap_hwmod_addr_space *mem;
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302318 void __iomem *va_start = NULL;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002319
2320 if (!oh)
Suman Anna6423d6d2013-10-08 23:46:49 -06002321 return -EINVAL;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002322
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002323 _save_mpu_port_index(oh);
2324
Roger Quadros9a258af2015-07-16 16:16:44 +03002325 /* if we don't need sysc access we don't need to ioremap */
2326 if (!oh->class->sysc)
2327 return 0;
2328
2329 /* we can't continue without MPU PORT if we need sysc access */
Paul Walmsley381d0332012-04-19 00:58:22 -06002330 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
Suman Anna6423d6d2013-10-08 23:46:49 -06002331 return -ENXIO;
Paul Walmsley381d0332012-04-19 00:58:22 -06002332
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002333 mem = _find_mpu_rt_addr_space(oh);
2334 if (!mem) {
2335 pr_debug("omap_hwmod: %s: no MPU register target found\n",
2336 oh->name);
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302337
2338 /* Extract the IO space from device tree blob */
Roger Quadros9a258af2015-07-16 16:16:44 +03002339 if (!np) {
2340 pr_err("omap_hwmod: %s: no dt node\n", oh->name);
Suman Anna6423d6d2013-10-08 23:46:49 -06002341 return -ENXIO;
Roger Quadros9a258af2015-07-16 16:16:44 +03002342 }
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302343
Tony Lindgren5e863c52013-12-06 14:20:16 -08002344 va_start = of_iomap(np, index + oh->mpu_rt_idx);
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302345 } else {
2346 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002347 }
2348
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002349 if (!va_start) {
Tony Lindgren5e863c52013-12-06 14:20:16 -08002350 if (mem)
2351 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
2352 else
2353 pr_err("omap_hwmod: %s: Missing dt reg%i for %s\n",
2354 oh->name, index, np->full_name);
Suman Anna6423d6d2013-10-08 23:46:49 -06002355 return -ENXIO;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002356 }
2357
2358 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
2359 oh->name, va_start);
2360
2361 oh->_mpu_rt_va = va_start;
Suman Anna6423d6d2013-10-08 23:46:49 -06002362 return 0;
Paul Walmsley381d0332012-04-19 00:58:22 -06002363}
2364
2365/**
2366 * _init - initialize internal data for the hwmod @oh
2367 * @oh: struct omap_hwmod *
2368 * @n: (unused)
2369 *
2370 * Look up the clocks and the address space used by the MPU to access
2371 * registers belonging to the hwmod @oh. @oh must already be
2372 * registered at this point. This is the first of two phases for
2373 * hwmod initialization. Code called here does not touch any hardware
2374 * registers, it simply prepares internal data structures. Returns 0
Suman Anna6423d6d2013-10-08 23:46:49 -06002375 * upon success or if the hwmod isn't registered or if the hwmod's
2376 * address space is not defined, or -EINVAL upon failure.
Paul Walmsley381d0332012-04-19 00:58:22 -06002377 */
2378static int __init _init(struct omap_hwmod *oh, void *data)
2379{
Tony Lindgren5e863c52013-12-06 14:20:16 -08002380 int r, index;
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002381 struct device_node *np = NULL;
Paul Walmsley381d0332012-04-19 00:58:22 -06002382
2383 if (oh->_state != _HWMOD_STATE_REGISTERED)
2384 return 0;
2385
Tony Lindgren5e863c52013-12-06 14:20:16 -08002386 if (of_have_populated_dt()) {
2387 struct device_node *bus;
2388
2389 bus = of_find_node_by_name(NULL, "ocp");
2390 if (!bus)
2391 return -ENODEV;
2392
2393 r = of_dev_hwmod_lookup(bus, oh, &index, &np);
2394 if (r)
2395 pr_debug("omap_hwmod: %s missing dt data\n", oh->name);
2396 else if (np && index)
2397 pr_warn("omap_hwmod: %s using broken dt data from %s\n",
2398 oh->name, np->name);
2399 }
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002400
Roger Quadros9a258af2015-07-16 16:16:44 +03002401 r = _init_mpu_rt_base(oh, NULL, index, np);
2402 if (r < 0) {
2403 WARN(1, "omap_hwmod: %s: doesn't have mpu register target base\n",
2404 oh->name);
2405 return 0;
Suman Anna6423d6d2013-10-08 23:46:49 -06002406 }
Paul Walmsley381d0332012-04-19 00:58:22 -06002407
2408 r = _init_clocks(oh, NULL);
Russell Kingc48cd652013-03-13 20:44:21 +00002409 if (r < 0) {
Paul Walmsley381d0332012-04-19 00:58:22 -06002410 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2411 return -EINVAL;
2412 }
2413
Suman Anna3d36ad72014-03-14 14:45:17 +05302414 if (np) {
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002415 if (of_find_property(np, "ti,no-reset-on-init", NULL))
2416 oh->flags |= HWMOD_INIT_NO_RESET;
2417 if (of_find_property(np, "ti,no-idle-on-init", NULL))
2418 oh->flags |= HWMOD_INIT_NO_IDLE;
Lokesh Vutla2e18f5a2016-03-07 01:41:21 -07002419 if (of_find_property(np, "ti,no-idle", NULL))
2420 oh->flags |= HWMOD_NO_IDLE;
Suman Anna3d36ad72014-03-14 14:45:17 +05302421 }
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002422
Paul Walmsley381d0332012-04-19 00:58:22 -06002423 oh->_state = _HWMOD_STATE_INITIALIZED;
2424
2425 return 0;
2426}
2427
2428/**
Paul Walmsley64813c32012-04-18 19:10:03 -06002429 * _setup_iclk_autoidle - configure an IP block's interface clocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002430 * @oh: struct omap_hwmod *
2431 *
Paul Walmsley64813c32012-04-18 19:10:03 -06002432 * Set up the module's interface clocks. XXX This function is still mostly
2433 * a stub; implementing this properly requires iclk autoidle usecounting in
2434 * the clock code. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03002435 */
Paul Walmsley64813c32012-04-18 19:10:03 -06002436static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002437{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002438 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002439 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002440 int i = 0;
Paul Walmsley381d0332012-04-19 00:58:22 -06002441 if (oh->_state != _HWMOD_STATE_INITIALIZED)
Paul Walmsley64813c32012-04-18 19:10:03 -06002442 return;
Paul Walmsley48d54f32011-02-23 00:14:07 -07002443
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002444 p = oh->slave_ports.next;
Paul Walmsley63c85232009-09-03 20:14:03 +03002445
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002446 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002447 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002448 if (!os->_clk)
Paul Walmsley64813c32012-04-18 19:10:03 -06002449 continue;
Paul Walmsley63c85232009-09-03 20:14:03 +03002450
Paul Walmsley64813c32012-04-18 19:10:03 -06002451 if (os->flags & OCPIF_SWSUP_IDLE) {
2452 /* XXX omap_iclk_deny_idle(c); */
2453 } else {
2454 /* XXX omap_iclk_allow_idle(c); */
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002455 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +03002456 }
2457 }
2458
Paul Walmsley64813c32012-04-18 19:10:03 -06002459 return;
2460}
2461
2462/**
2463 * _setup_reset - reset an IP block during the setup process
2464 * @oh: struct omap_hwmod *
2465 *
2466 * Reset the IP block corresponding to the hwmod @oh during the setup
2467 * process. The IP block is first enabled so it can be successfully
2468 * reset. Returns 0 upon success or a negative error code upon
2469 * failure.
2470 */
2471static int __init _setup_reset(struct omap_hwmod *oh)
2472{
2473 int r;
2474
2475 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2476 return -EINVAL;
Paul Walmsley63c85232009-09-03 20:14:03 +03002477
Paul Walmsley5fb3d522012-10-29 22:11:50 -06002478 if (oh->flags & HWMOD_EXT_OPT_MAIN_CLK)
2479 return -EPERM;
2480
Paul Walmsley747834a2012-04-18 19:10:04 -06002481 if (oh->rst_lines_cnt == 0) {
2482 r = _enable(oh);
2483 if (r) {
Joe Perches3d0cb732014-09-13 11:31:16 -07002484 pr_warn("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2485 oh->name, oh->_state);
Paul Walmsley747834a2012-04-18 19:10:04 -06002486 return -EINVAL;
2487 }
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002488 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002489
Rajendra Nayak28008522012-03-13 22:55:23 +05302490 if (!(oh->flags & HWMOD_INIT_NO_RESET))
Paul Walmsley64813c32012-04-18 19:10:03 -06002491 r = _reset(oh);
2492
2493 return r;
2494}
2495
2496/**
2497 * _setup_postsetup - transition to the appropriate state after _setup
2498 * @oh: struct omap_hwmod *
2499 *
2500 * Place an IP block represented by @oh into a "post-setup" state --
2501 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2502 * this function is called at the end of _setup().) The postsetup
2503 * state for an IP block can be changed by calling
2504 * omap_hwmod_enter_postsetup_state() early in the boot process,
2505 * before one of the omap_hwmod_setup*() functions are called for the
2506 * IP block.
2507 *
2508 * The IP block stays in this state until a PM runtime-based driver is
2509 * loaded for that IP block. A post-setup state of IDLE is
2510 * appropriate for almost all IP blocks with runtime PM-enabled
2511 * drivers, since those drivers are able to enable the IP block. A
2512 * post-setup state of ENABLED is appropriate for kernels with PM
2513 * runtime disabled. The DISABLED state is appropriate for unusual IP
2514 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2515 * included, since the WDTIMER starts running on reset and will reset
2516 * the MPU if left active.
2517 *
2518 * This post-setup mechanism is deprecated. Once all of the OMAP
2519 * drivers have been converted to use PM runtime, and all of the IP
2520 * block data and interconnect data is available to the hwmod code, it
2521 * should be possible to replace this mechanism with a "lazy reset"
2522 * arrangement. In a "lazy reset" setup, each IP block is enabled
2523 * when the driver first probes, then all remaining IP blocks without
2524 * drivers are either shut down or enabled after the drivers have
2525 * loaded. However, this cannot take place until the above
2526 * preconditions have been met, since otherwise the late reset code
2527 * has no way of knowing which IP blocks are in use by drivers, and
2528 * which ones are unused.
2529 *
2530 * No return value.
2531 */
2532static void __init _setup_postsetup(struct omap_hwmod *oh)
2533{
2534 u8 postsetup_state;
2535
2536 if (oh->rst_lines_cnt > 0)
2537 return;
Benoit Cousson76e55892010-09-21 10:34:11 -06002538
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002539 postsetup_state = oh->_postsetup_state;
2540 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2541 postsetup_state = _HWMOD_STATE_ENABLED;
2542
2543 /*
2544 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2545 * it should be set by the core code as a runtime flag during startup
2546 */
Lokesh Vutla2e18f5a2016-03-07 01:41:21 -07002547 if ((oh->flags & (HWMOD_INIT_NO_IDLE | HWMOD_NO_IDLE)) &&
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002548 (postsetup_state == _HWMOD_STATE_IDLE)) {
2549 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002550 postsetup_state = _HWMOD_STATE_ENABLED;
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002551 }
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002552
2553 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002554 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002555 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2556 _shutdown(oh);
2557 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2558 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2559 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03002560
Paul Walmsley64813c32012-04-18 19:10:03 -06002561 return;
2562}
2563
2564/**
2565 * _setup - prepare IP block hardware for use
2566 * @oh: struct omap_hwmod *
2567 * @n: (unused, pass NULL)
2568 *
2569 * Configure the IP block represented by @oh. This may include
2570 * enabling the IP block, resetting it, and placing it into a
2571 * post-setup state, depending on the type of IP block and applicable
2572 * flags. IP blocks are reset to prevent any previous configuration
2573 * by the bootloader or previous operating system from interfering
2574 * with power management or other parts of the system. The reset can
2575 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2576 * two phases for hwmod initialization. Code called here generally
2577 * affects the IP block hardware, or system integration hardware
2578 * associated with the IP block. Returns 0.
2579 */
2580static int __init _setup(struct omap_hwmod *oh, void *data)
2581{
2582 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2583 return 0;
2584
Tomi Valkeinenf22d2542014-10-09 17:03:14 +03002585 if (oh->parent_hwmod) {
2586 int r;
2587
2588 r = _enable(oh->parent_hwmod);
2589 WARN(r, "hwmod: %s: setup: failed to enable parent hwmod %s\n",
2590 oh->name, oh->parent_hwmod->name);
2591 }
2592
Paul Walmsley64813c32012-04-18 19:10:03 -06002593 _setup_iclk_autoidle(oh);
2594
2595 if (!_setup_reset(oh))
2596 _setup_postsetup(oh);
2597
Tomi Valkeinenf22d2542014-10-09 17:03:14 +03002598 if (oh->parent_hwmod) {
2599 u8 postsetup_state;
2600
2601 postsetup_state = oh->parent_hwmod->_postsetup_state;
2602
2603 if (postsetup_state == _HWMOD_STATE_IDLE)
2604 _idle(oh->parent_hwmod);
2605 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2606 _shutdown(oh->parent_hwmod);
2607 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2608 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2609 oh->parent_hwmod->name, postsetup_state);
2610 }
2611
Paul Walmsley63c85232009-09-03 20:14:03 +03002612 return 0;
2613}
2614
Benoit Cousson0102b622010-12-21 21:31:27 -07002615/**
2616 * _register - register a struct omap_hwmod
2617 * @oh: struct omap_hwmod *
2618 *
2619 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2620 * already has been registered by the same name; -EINVAL if the
2621 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2622 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2623 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2624 * success.
2625 *
2626 * XXX The data should be copied into bootmem, so the original data
2627 * should be marked __initdata and freed after init. This would allow
2628 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2629 * that the copy process would be relatively complex due to the large number
2630 * of substructures.
2631 */
Benoit Cousson01592df2010-12-21 21:31:28 -07002632static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07002633{
Benoit Cousson0102b622010-12-21 21:31:27 -07002634 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2635 (oh->_state != _HWMOD_STATE_UNKNOWN))
2636 return -EINVAL;
2637
Benoit Cousson0102b622010-12-21 21:31:27 -07002638 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2639
Benoit Coussonce35b242010-12-21 21:31:28 -07002640 if (_lookup(oh->name))
2641 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07002642
Benoit Cousson0102b622010-12-21 21:31:27 -07002643 list_add_tail(&oh->node, &omap_hwmod_list);
2644
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002645 INIT_LIST_HEAD(&oh->slave_ports);
Benoit Cousson0102b622010-12-21 21:31:27 -07002646 spin_lock_init(&oh->_lock);
Peter Ujfalusi69317952015-02-26 00:00:51 -07002647 lockdep_set_class(&oh->_lock, &oh->hwmod_key);
Benoit Cousson0102b622010-12-21 21:31:27 -07002648
2649 oh->_state = _HWMOD_STATE_REGISTERED;
2650
Paul Walmsley569edd702011-02-23 00:14:06 -07002651 /*
2652 * XXX Rather than doing a strcmp(), this should test a flag
2653 * set in the hwmod data, inserted by the autogenerator code.
2654 */
2655 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2656 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07002657
Paul Walmsley569edd702011-02-23 00:14:06 -07002658 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07002659}
Paul Walmsley63c85232009-09-03 20:14:03 +03002660
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002661/**
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002662 * _add_link - add an interconnect between two IP blocks
2663 * @oi: pointer to a struct omap_hwmod_ocp_if record
2664 *
Tony Lindgrena1e31232017-03-14 13:13:19 -07002665 * Add struct omap_hwmod_link records connecting the slave IP block
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002666 * specified in @oi->slave to @oi. This code is assumed to run before
2667 * preemption or SMP has been enabled, thus avoiding the need for
2668 * locking in this code. Changes to this assumption will require
2669 * additional locking. Returns 0.
2670 */
2671static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2672{
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002673 pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2674 oi->slave->name);
2675
Tony Lindgrena1e31232017-03-14 13:13:19 -07002676 list_add(&oi->node, &oi->slave->slave_ports);
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002677 oi->slave->slaves_cnt++;
2678
2679 return 0;
2680}
2681
2682/**
2683 * _register_link - register a struct omap_hwmod_ocp_if
2684 * @oi: struct omap_hwmod_ocp_if *
2685 *
2686 * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
2687 * has already been registered; -EINVAL if @oi is NULL or if the
2688 * record pointed to by @oi is missing required fields; or 0 upon
2689 * success.
2690 *
2691 * XXX The data should be copied into bootmem, so the original data
2692 * should be marked __initdata and freed after init. This would allow
2693 * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2694 */
2695static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2696{
2697 if (!oi || !oi->master || !oi->slave || !oi->user)
2698 return -EINVAL;
2699
2700 if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2701 return -EEXIST;
2702
2703 pr_debug("omap_hwmod: registering link from %s to %s\n",
2704 oi->master->name, oi->slave->name);
2705
2706 /*
2707 * Register the connected hwmods, if they haven't been
2708 * registered already
2709 */
2710 if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2711 _register(oi->master);
2712
2713 if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2714 _register(oi->slave);
2715
2716 _add_link(oi);
2717
2718 oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2719
2720 return 0;
2721}
2722
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002723/* Static functions intended only for use in soc_ops field function pointers */
2724
2725/**
Tero Kristo9002e9212014-10-27 08:39:23 -07002726 * _omap2xxx_3xxx_wait_target_ready - wait for a module to leave slave idle
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002727 * @oh: struct omap_hwmod *
2728 *
2729 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2730 * does not have an IDLEST bit or if the module successfully leaves
2731 * slave idle; otherwise, pass along the return value of the
2732 * appropriate *_cm*_wait_module_ready() function.
2733 */
Tero Kristo9002e9212014-10-27 08:39:23 -07002734static int _omap2xxx_3xxx_wait_target_ready(struct omap_hwmod *oh)
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002735{
2736 if (!oh)
2737 return -EINVAL;
2738
2739 if (oh->flags & HWMOD_NO_IDLEST)
2740 return 0;
2741
2742 if (!_find_mpu_rt_port(oh))
2743 return 0;
2744
2745 /* XXX check module SIDLEMODE, hardreset status, enabled clocks */
2746
Tero Kristo021b6ff2014-10-27 08:39:23 -07002747 return omap_cm_wait_module_ready(0, oh->prcm.omap2.module_offs,
2748 oh->prcm.omap2.idlest_reg_id,
2749 oh->prcm.omap2.idlest_idle_bit);
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002750}
2751
2752/**
2753 * _omap4_wait_target_ready - wait for a module to leave slave idle
2754 * @oh: struct omap_hwmod *
2755 *
2756 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2757 * does not have an IDLEST bit or if the module successfully leaves
2758 * slave idle; otherwise, pass along the return value of the
2759 * appropriate *_cm*_wait_module_ready() function.
2760 */
2761static int _omap4_wait_target_ready(struct omap_hwmod *oh)
2762{
Paul Walmsley2b026d12012-09-23 17:28:18 -06002763 if (!oh)
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002764 return -EINVAL;
2765
Paul Walmsley2b026d12012-09-23 17:28:18 -06002766 if (oh->flags & HWMOD_NO_IDLEST || !oh->clkdm)
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002767 return 0;
2768
2769 if (!_find_mpu_rt_port(oh))
2770 return 0;
2771
Dave Gerlach428929c2016-07-12 12:50:33 -05002772 if (!oh->prcm.omap4.clkctrl_offs &&
2773 !(oh->prcm.omap4.flags & HWMOD_OMAP4_ZERO_CLKCTRL_OFFSET))
2774 return 0;
2775
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002776 /* XXX check module SIDLEMODE, hardreset status */
2777
Tero Kristo021b6ff2014-10-27 08:39:23 -07002778 return omap_cm_wait_module_ready(oh->clkdm->prcm_partition,
2779 oh->clkdm->cm_inst,
2780 oh->prcm.omap4.clkctrl_offs, 0);
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06002781}
2782
2783/**
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002784 * _omap2_assert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2785 * @oh: struct omap_hwmod * to assert hardreset
2786 * @ohri: hardreset line data
2787 *
2788 * Call omap2_prm_assert_hardreset() with parameters extracted from
2789 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2790 * use as an soc_ops function pointer. Passes along the return value
2791 * from omap2_prm_assert_hardreset(). XXX This function is scheduled
2792 * for removal when the PRM code is moved into drivers/.
2793 */
2794static int _omap2_assert_hardreset(struct omap_hwmod *oh,
2795 struct omap_hwmod_rst_info *ohri)
2796{
Tero Kristoefd44dc2014-10-27 08:39:24 -07002797 return omap_prm_assert_hardreset(ohri->rst_shift, 0,
2798 oh->prcm.omap2.module_offs, 0);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002799}
2800
2801/**
2802 * _omap2_deassert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2803 * @oh: struct omap_hwmod * to deassert hardreset
2804 * @ohri: hardreset line data
2805 *
2806 * Call omap2_prm_deassert_hardreset() with parameters extracted from
2807 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2808 * use as an soc_ops function pointer. Passes along the return value
2809 * from omap2_prm_deassert_hardreset(). XXX This function is
2810 * scheduled for removal when the PRM code is moved into drivers/.
2811 */
2812static int _omap2_deassert_hardreset(struct omap_hwmod *oh,
2813 struct omap_hwmod_rst_info *ohri)
2814{
Tero Kristo37fb59d2014-10-27 08:39:25 -07002815 return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->st_shift, 0,
2816 oh->prcm.omap2.module_offs, 0, 0);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002817}
2818
2819/**
2820 * _omap2_is_hardreset_asserted - call OMAP2 PRM hardreset fn with hwmod args
2821 * @oh: struct omap_hwmod * to test hardreset
2822 * @ohri: hardreset line data
2823 *
2824 * Call omap2_prm_is_hardreset_asserted() with parameters extracted
2825 * from the hwmod @oh and the hardreset line data @ohri. Only
2826 * intended for use as an soc_ops function pointer. Passes along the
2827 * return value from omap2_prm_is_hardreset_asserted(). XXX This
2828 * function is scheduled for removal when the PRM code is moved into
2829 * drivers/.
2830 */
2831static int _omap2_is_hardreset_asserted(struct omap_hwmod *oh,
2832 struct omap_hwmod_rst_info *ohri)
2833{
Tero Kristo1bc28b32014-10-27 08:39:25 -07002834 return omap_prm_is_hardreset_asserted(ohri->st_shift, 0,
2835 oh->prcm.omap2.module_offs, 0);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002836}
2837
2838/**
2839 * _omap4_assert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2840 * @oh: struct omap_hwmod * to assert hardreset
2841 * @ohri: hardreset line data
2842 *
2843 * Call omap4_prminst_assert_hardreset() with parameters extracted
2844 * from the hwmod @oh and the hardreset line data @ohri. Only
2845 * intended for use as an soc_ops function pointer. Passes along the
2846 * return value from omap4_prminst_assert_hardreset(). XXX This
2847 * function is scheduled for removal when the PRM code is moved into
2848 * drivers/.
2849 */
2850static int _omap4_assert_hardreset(struct omap_hwmod *oh,
2851 struct omap_hwmod_rst_info *ohri)
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002852{
Paul Walmsley07b3a132012-06-20 20:11:36 -06002853 if (!oh->clkdm)
2854 return -EINVAL;
2855
Tero Kristoefd44dc2014-10-27 08:39:24 -07002856 return omap_prm_assert_hardreset(ohri->rst_shift,
2857 oh->clkdm->pwrdm.ptr->prcm_partition,
2858 oh->clkdm->pwrdm.ptr->prcm_offs,
2859 oh->prcm.omap4.rstctrl_offs);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002860}
2861
2862/**
2863 * _omap4_deassert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2864 * @oh: struct omap_hwmod * to deassert hardreset
2865 * @ohri: hardreset line data
2866 *
2867 * Call omap4_prminst_deassert_hardreset() with parameters extracted
2868 * from the hwmod @oh and the hardreset line data @ohri. Only
2869 * intended for use as an soc_ops function pointer. Passes along the
2870 * return value from omap4_prminst_deassert_hardreset(). XXX This
2871 * function is scheduled for removal when the PRM code is moved into
2872 * drivers/.
2873 */
2874static int _omap4_deassert_hardreset(struct omap_hwmod *oh,
2875 struct omap_hwmod_rst_info *ohri)
2876{
Paul Walmsley07b3a132012-06-20 20:11:36 -06002877 if (!oh->clkdm)
2878 return -EINVAL;
2879
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002880 if (ohri->st_shift)
2881 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
2882 oh->name, ohri->name);
Tero Kristo4ebf5b22015-05-05 16:33:04 +03002883 return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->rst_shift,
Tero Kristo37fb59d2014-10-27 08:39:25 -07002884 oh->clkdm->pwrdm.ptr->prcm_partition,
2885 oh->clkdm->pwrdm.ptr->prcm_offs,
Tero Kristo4ebf5b22015-05-05 16:33:04 +03002886 oh->prcm.omap4.rstctrl_offs,
2887 oh->prcm.omap4.rstctrl_offs +
2888 OMAP4_RST_CTRL_ST_OFFSET);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002889}
2890
2891/**
2892 * _omap4_is_hardreset_asserted - call OMAP4 PRM hardreset fn with hwmod args
2893 * @oh: struct omap_hwmod * to test hardreset
2894 * @ohri: hardreset line data
2895 *
2896 * Call omap4_prminst_is_hardreset_asserted() with parameters
2897 * extracted from the hwmod @oh and the hardreset line data @ohri.
2898 * Only intended for use as an soc_ops function pointer. Passes along
2899 * the return value from omap4_prminst_is_hardreset_asserted(). XXX
2900 * This function is scheduled for removal when the PRM code is moved
2901 * into drivers/.
2902 */
2903static int _omap4_is_hardreset_asserted(struct omap_hwmod *oh,
2904 struct omap_hwmod_rst_info *ohri)
2905{
Paul Walmsley07b3a132012-06-20 20:11:36 -06002906 if (!oh->clkdm)
2907 return -EINVAL;
2908
Tero Kristo1bc28b32014-10-27 08:39:25 -07002909 return omap_prm_is_hardreset_asserted(ohri->rst_shift,
2910 oh->clkdm->pwrdm.ptr->
2911 prcm_partition,
2912 oh->clkdm->pwrdm.ptr->prcm_offs,
2913 oh->prcm.omap4.rstctrl_offs);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002914}
2915
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06002916/**
Tero Kristo9fabc1a2016-07-04 14:11:48 +03002917 * _omap4_disable_direct_prcm - disable direct PRCM control for hwmod
2918 * @oh: struct omap_hwmod * to disable control for
2919 *
2920 * Disables direct PRCM clkctrl done by hwmod core. Instead, the hwmod
2921 * will be using its main_clk to enable/disable the module. Returns
2922 * 0 if successful.
2923 */
2924static int _omap4_disable_direct_prcm(struct omap_hwmod *oh)
2925{
2926 if (!oh)
2927 return -EINVAL;
2928
2929 oh->prcm.omap4.clkctrl_offs = 0;
2930 oh->prcm.omap4.modulemode = 0;
2931
2932 return 0;
2933}
2934
2935/**
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06002936 * _am33xx_deassert_hardreset - call AM33XX PRM hardreset fn with hwmod args
2937 * @oh: struct omap_hwmod * to deassert hardreset
2938 * @ohri: hardreset line data
2939 *
2940 * Call am33xx_prminst_deassert_hardreset() with parameters extracted
2941 * from the hwmod @oh and the hardreset line data @ohri. Only
2942 * intended for use as an soc_ops function pointer. Passes along the
2943 * return value from am33xx_prminst_deassert_hardreset(). XXX This
2944 * function is scheduled for removal when the PRM code is moved into
2945 * drivers/.
2946 */
2947static int _am33xx_deassert_hardreset(struct omap_hwmod *oh,
2948 struct omap_hwmod_rst_info *ohri)
2949{
Tero Kristoa5bf00c2015-05-05 16:33:05 +03002950 return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->st_shift,
2951 oh->clkdm->pwrdm.ptr->prcm_partition,
Tero Kristo37fb59d2014-10-27 08:39:25 -07002952 oh->clkdm->pwrdm.ptr->prcm_offs,
2953 oh->prcm.omap4.rstctrl_offs,
2954 oh->prcm.omap4.rstst_offs);
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06002955}
2956
Paul Walmsley63c85232009-09-03 20:14:03 +03002957/* Public functions */
2958
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002959u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002960{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002961 if (oh->flags & HWMOD_16BIT_REG)
Victor Kamenskyedfaf052014-04-15 20:37:46 +03002962 return readw_relaxed(oh->_mpu_rt_va + reg_offs);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002963 else
Victor Kamenskyedfaf052014-04-15 20:37:46 +03002964 return readl_relaxed(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002965}
2966
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002967void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002968{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002969 if (oh->flags & HWMOD_16BIT_REG)
Victor Kamenskyedfaf052014-04-15 20:37:46 +03002970 writew_relaxed(v, oh->_mpu_rt_va + reg_offs);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002971 else
Victor Kamenskyedfaf052014-04-15 20:37:46 +03002972 writel_relaxed(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002973}
2974
Paul Walmsley887adea2010-07-26 16:34:33 -06002975/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002976 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
2977 * @oh: struct omap_hwmod *
2978 *
2979 * This is a public function exposed to drivers. Some drivers may need to do
2980 * some settings before and after resetting the device. Those drivers after
2981 * doing the necessary settings could use this function to start a reset by
2982 * setting the SYSCONFIG.SOFTRESET bit.
2983 */
2984int omap_hwmod_softreset(struct omap_hwmod *oh)
2985{
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002986 u32 v;
2987 int ret;
2988
2989 if (!oh || !(oh->_sysc_cache))
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002990 return -EINVAL;
2991
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002992 v = oh->_sysc_cache;
2993 ret = _set_softreset(oh, &v);
2994 if (ret)
2995 goto error;
2996 _write_sysconfig(v, oh);
2997
Roger Quadros313a76e2013-12-08 18:39:02 -07002998 ret = _clear_softreset(oh, &v);
2999 if (ret)
3000 goto error;
3001 _write_sysconfig(v, oh);
3002
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06003003error:
3004 return ret;
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06003005}
3006
3007/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003008 * omap_hwmod_lookup - look up a registered omap_hwmod by name
3009 * @name: name of the omap_hwmod to look up
3010 *
3011 * Given a @name of an omap_hwmod, return a pointer to the registered
3012 * struct omap_hwmod *, or NULL upon error.
3013 */
3014struct omap_hwmod *omap_hwmod_lookup(const char *name)
3015{
3016 struct omap_hwmod *oh;
3017
3018 if (!name)
3019 return NULL;
3020
Paul Walmsley63c85232009-09-03 20:14:03 +03003021 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03003022
3023 return oh;
3024}
3025
3026/**
3027 * omap_hwmod_for_each - call function for each registered omap_hwmod
3028 * @fn: pointer to a callback function
Paul Walmsley97d601622010-07-26 16:34:30 -06003029 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03003030 *
3031 * Call @fn for each registered omap_hwmod, passing @data to each
3032 * function. @fn must return 0 for success or any other value for
3033 * failure. If @fn returns non-zero, the iteration across omap_hwmods
3034 * will stop and the non-zero return value will be passed to the
3035 * caller of omap_hwmod_for_each(). @fn is called with
3036 * omap_hwmod_for_each() held.
3037 */
Paul Walmsley97d601622010-07-26 16:34:30 -06003038int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
3039 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03003040{
3041 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05303042 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03003043
3044 if (!fn)
3045 return -EINVAL;
3046
Paul Walmsley63c85232009-09-03 20:14:03 +03003047 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d601622010-07-26 16:34:30 -06003048 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03003049 if (ret)
3050 break;
3051 }
Paul Walmsley63c85232009-09-03 20:14:03 +03003052
3053 return ret;
3054}
3055
Paul Walmsley63c85232009-09-03 20:14:03 +03003056/**
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003057 * omap_hwmod_register_links - register an array of hwmod links
3058 * @ois: pointer to an array of omap_hwmod_ocp_if to register
3059 *
3060 * Intended to be called early in boot before the clock framework is
3061 * initialized. If @ois is not null, will register all omap_hwmods
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003062 * listed in @ois that are valid for this chip. Returns -EINVAL if
3063 * omap_hwmod_init() hasn't been called before calling this function,
3064 * -ENOMEM if the link memory area can't be allocated, or 0 upon
3065 * success.
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003066 */
3067int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
3068{
3069 int r, i;
3070
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003071 if (!inited)
3072 return -EINVAL;
3073
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003074 if (!ois)
3075 return 0;
3076
Rajendra Nayakf7f7a292014-08-27 19:38:23 -06003077 if (ois[0] == NULL) /* Empty list */
3078 return 0;
3079
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003080 i = 0;
3081 do {
3082 r = _register_link(ois[i]);
3083 WARN(r && r != -EEXIST,
3084 "omap_hwmod: _register_link(%s -> %s) returned %d\n",
3085 ois[i]->master->name, ois[i]->slave->name, r);
3086 } while (ois[++i]);
3087
3088 return 0;
3089}
3090
3091/**
Paul Walmsley381d0332012-04-19 00:58:22 -06003092 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
3093 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08003094 *
Paul Walmsley381d0332012-04-19 00:58:22 -06003095 * If the hwmod data corresponding to the MPU subsystem IP block
3096 * hasn't been initialized and set up yet, do so now. This must be
3097 * done first since sleep dependencies may be added from other hwmods
3098 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
3099 * return value.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08003100 */
Paul Walmsley381d0332012-04-19 00:58:22 -06003101static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08003102{
Paul Walmsley381d0332012-04-19 00:58:22 -06003103 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
3104 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
3105 __func__, MPU_INITIATOR_NAME);
3106 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
3107 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
Paul Walmsley63c85232009-09-03 20:14:03 +03003108}
3109
3110/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003111 * omap_hwmod_setup_one - set up a single hwmod
3112 * @oh_name: const char * name of the already-registered hwmod to set up
3113 *
Paul Walmsley381d0332012-04-19 00:58:22 -06003114 * Initialize and set up a single hwmod. Intended to be used for a
3115 * small number of early devices, such as the timer IP blocks used for
3116 * the scheduler clock. Must be called after omap2_clk_init().
3117 * Resolves the struct clk names to struct clk pointers for each
3118 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
3119 * -EINVAL upon error or 0 upon success.
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003120 */
3121int __init omap_hwmod_setup_one(const char *oh_name)
3122{
3123 struct omap_hwmod *oh;
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003124
3125 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
3126
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003127 oh = _lookup(oh_name);
3128 if (!oh) {
3129 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
3130 return -EINVAL;
3131 }
3132
Paul Walmsley381d0332012-04-19 00:58:22 -06003133 _ensure_mpu_hwmod_is_setup(oh);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003134
Paul Walmsley381d0332012-04-19 00:58:22 -06003135 _init(oh, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003136 _setup(oh, NULL);
3137
3138 return 0;
3139}
3140
3141/**
Lokesh Vutla8dd66662017-01-20 10:39:10 -08003142 * omap_hwmod_setup_earlycon_flags - set up flags for early console
3143 *
3144 * Enable DEBUG_OMAPUART_FLAGS for uart hwmod that is being used as
3145 * early concole so that hwmod core doesn't reset and keep it in idle
3146 * that specific uart.
3147 */
3148#ifdef CONFIG_SERIAL_EARLYCON
3149static void __init omap_hwmod_setup_earlycon_flags(void)
3150{
3151 struct device_node *np;
3152 struct omap_hwmod *oh;
3153 const char *uart;
3154
3155 np = of_find_node_by_path("/chosen");
3156 if (np) {
3157 uart = of_get_property(np, "stdout-path", NULL);
3158 if (uart) {
3159 np = of_find_node_by_path(uart);
3160 if (np) {
3161 uart = of_get_property(np, "ti,hwmods", NULL);
3162 oh = omap_hwmod_lookup(uart);
3163 if (oh)
3164 oh->flags |= DEBUG_OMAPUART_FLAGS;
3165 }
3166 }
3167 }
3168}
3169#endif
3170
3171/**
Paul Walmsley381d0332012-04-19 00:58:22 -06003172 * omap_hwmod_setup_all - set up all registered IP blocks
Paul Walmsley63c85232009-09-03 20:14:03 +03003173 *
Paul Walmsley381d0332012-04-19 00:58:22 -06003174 * Initialize and set up all IP blocks registered with the hwmod code.
3175 * Must be called after omap2_clk_init(). Resolves the struct clk
3176 * names to struct clk pointers for each registered omap_hwmod. Also
3177 * calls _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003178 */
Paul Walmsley550c8092011-02-28 11:58:14 -07003179static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03003180{
Paul Walmsley381d0332012-04-19 00:58:22 -06003181 _ensure_mpu_hwmod_is_setup(NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03003182
Paul Walmsley381d0332012-04-19 00:58:22 -06003183 omap_hwmod_for_each(_init, NULL);
Lokesh Vutla8dd66662017-01-20 10:39:10 -08003184#ifdef CONFIG_SERIAL_EARLYCON
3185 omap_hwmod_setup_earlycon_flags();
3186#endif
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003187 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03003188
3189 return 0;
3190}
Tony Lindgren8dd5ea72015-12-03 11:38:09 -08003191omap_postcore_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03003192
3193/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003194 * omap_hwmod_enable - enable an omap_hwmod
3195 * @oh: struct omap_hwmod *
3196 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06003197 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03003198 * Returns -EINVAL on error or passes along the return value from _enable().
3199 */
3200int omap_hwmod_enable(struct omap_hwmod *oh)
3201{
3202 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003203 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03003204
3205 if (!oh)
3206 return -EINVAL;
3207
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003208 spin_lock_irqsave(&oh->_lock, flags);
3209 r = _enable(oh);
3210 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003211
3212 return r;
3213}
3214
3215/**
3216 * omap_hwmod_idle - idle an omap_hwmod
3217 * @oh: struct omap_hwmod *
3218 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06003219 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03003220 * Returns -EINVAL on error or passes along the return value from _idle().
3221 */
3222int omap_hwmod_idle(struct omap_hwmod *oh)
3223{
Pali RohƔr6da23352015-02-26 14:49:51 +01003224 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003225 unsigned long flags;
3226
Paul Walmsley63c85232009-09-03 20:14:03 +03003227 if (!oh)
3228 return -EINVAL;
3229
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003230 spin_lock_irqsave(&oh->_lock, flags);
Pali RohƔr6da23352015-02-26 14:49:51 +01003231 r = _idle(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003232 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003233
Pali RohƔr6da23352015-02-26 14:49:51 +01003234 return r;
Paul Walmsley63c85232009-09-03 20:14:03 +03003235}
3236
3237/**
3238 * omap_hwmod_shutdown - shutdown an omap_hwmod
3239 * @oh: struct omap_hwmod *
3240 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06003241 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03003242 * omap_device_shutdown(). Returns -EINVAL on error or passes along
3243 * the return value from _shutdown().
3244 */
3245int omap_hwmod_shutdown(struct omap_hwmod *oh)
3246{
Pali RohƔr6da23352015-02-26 14:49:51 +01003247 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003248 unsigned long flags;
3249
Paul Walmsley63c85232009-09-03 20:14:03 +03003250 if (!oh)
3251 return -EINVAL;
3252
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003253 spin_lock_irqsave(&oh->_lock, flags);
Pali RohƔr6da23352015-02-26 14:49:51 +01003254 r = _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003255 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003256
Pali RohƔr6da23352015-02-26 14:49:51 +01003257 return r;
Paul Walmsley63c85232009-09-03 20:14:03 +03003258}
3259
Paul Walmsley5e8370f2012-04-18 19:10:06 -06003260/*
3261 * IP block data retrieval functions
3262 */
3263
Paul Walmsley63c85232009-09-03 20:14:03 +03003264/**
3265 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
3266 * @oh: struct omap_hwmod *
Peter Ujfalusidad41912012-11-21 16:15:17 -07003267 * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
Paul Walmsley63c85232009-09-03 20:14:03 +03003268 *
3269 * Count the number of struct resource array elements necessary to
3270 * contain omap_hwmod @oh resources. Intended to be called by code
3271 * that registers omap_devices. Intended to be used to determine the
3272 * size of a dynamically-allocated struct resource array, before
3273 * calling omap_hwmod_fill_resources(). Returns the number of struct
3274 * resource array elements needed.
3275 *
3276 * XXX This code is not optimized. It could attempt to merge adjacent
3277 * resource IDs.
3278 *
3279 */
Peter Ujfalusidad41912012-11-21 16:15:17 -07003280int omap_hwmod_count_resources(struct omap_hwmod *oh, unsigned long flags)
Paul Walmsley63c85232009-09-03 20:14:03 +03003281{
Peter Ujfalusidad41912012-11-21 16:15:17 -07003282 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03003283
Peter Ujfalusidad41912012-11-21 16:15:17 -07003284 if (flags & IORESOURCE_IRQ)
3285 ret += _count_mpu_irqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03003286
Peter Ujfalusidad41912012-11-21 16:15:17 -07003287 if (flags & IORESOURCE_DMA)
3288 ret += _count_sdma_reqs(oh);
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003289
Peter Ujfalusidad41912012-11-21 16:15:17 -07003290 if (flags & IORESOURCE_MEM) {
3291 int i = 0;
3292 struct omap_hwmod_ocp_if *os;
3293 struct list_head *p = oh->slave_ports.next;
3294
3295 while (i < oh->slaves_cnt) {
3296 os = _fetch_next_ocp_if(&p, &i);
3297 ret += _count_ocp_if_addr_spaces(os);
3298 }
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003299 }
Paul Walmsley63c85232009-09-03 20:14:03 +03003300
3301 return ret;
3302}
3303
3304/**
3305 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
3306 * @oh: struct omap_hwmod *
3307 * @res: pointer to the first element of an array of struct resource to fill
3308 *
3309 * Fill the struct resource array @res with resource data from the
3310 * omap_hwmod @oh. Intended to be called by code that registers
3311 * omap_devices. See also omap_hwmod_count_resources(). Returns the
3312 * number of array elements filled.
3313 */
3314int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
3315{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003316 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003317 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003318 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03003319 int r = 0;
3320
3321 /* For each IRQ, DMA, memory area, fill in array.*/
3322
Paul Walmsley212738a2011-07-09 19:14:06 -06003323 mpu_irqs_cnt = _count_mpu_irqs(oh);
3324 for (i = 0; i < mpu_irqs_cnt; i++) {
Marc Zyngier0fb22a82015-01-17 10:21:08 +00003325 unsigned int irq;
3326
3327 if (oh->xlate_irq)
3328 irq = oh->xlate_irq((oh->mpu_irqs + i)->irq);
3329 else
3330 irq = (oh->mpu_irqs + i)->irq;
Paul Walmsley718bfd72009-12-08 16:34:16 -07003331 (res + r)->name = (oh->mpu_irqs + i)->name;
Marc Zyngier0fb22a82015-01-17 10:21:08 +00003332 (res + r)->start = irq;
3333 (res + r)->end = irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03003334 (res + r)->flags = IORESOURCE_IRQ;
3335 r++;
3336 }
3337
Paul Walmsleybc614952011-07-09 19:14:07 -06003338 sdma_reqs_cnt = _count_sdma_reqs(oh);
3339 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06003340 (res + r)->name = (oh->sdma_reqs + i)->name;
3341 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
3342 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03003343 (res + r)->flags = IORESOURCE_DMA;
3344 r++;
3345 }
3346
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003347 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003348
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003349 i = 0;
3350 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003351 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley78183f32011-07-09 19:14:05 -06003352 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03003353
Paul Walmsley78183f32011-07-09 19:14:05 -06003354 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08003355 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03003356 (res + r)->start = (os->addr + j)->pa_start;
3357 (res + r)->end = (os->addr + j)->pa_end;
3358 (res + r)->flags = IORESOURCE_MEM;
3359 r++;
3360 }
3361 }
3362
3363 return r;
3364}
3365
3366/**
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +05303367 * omap_hwmod_fill_dma_resources - fill struct resource array with dma data
3368 * @oh: struct omap_hwmod *
3369 * @res: pointer to the array of struct resource to fill
3370 *
3371 * Fill the struct resource array @res with dma resource data from the
3372 * omap_hwmod @oh. Intended to be called by code that registers
3373 * omap_devices. See also omap_hwmod_count_resources(). Returns the
3374 * number of array elements filled.
3375 */
3376int omap_hwmod_fill_dma_resources(struct omap_hwmod *oh, struct resource *res)
3377{
3378 int i, sdma_reqs_cnt;
3379 int r = 0;
3380
3381 sdma_reqs_cnt = _count_sdma_reqs(oh);
3382 for (i = 0; i < sdma_reqs_cnt; i++) {
3383 (res + r)->name = (oh->sdma_reqs + i)->name;
3384 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
3385 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
3386 (res + r)->flags = IORESOURCE_DMA;
3387 r++;
3388 }
3389
3390 return r;
3391}
3392
3393/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06003394 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
3395 * @oh: struct omap_hwmod * to operate on
3396 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
3397 * @name: pointer to the name of the data to fetch (optional)
3398 * @rsrc: pointer to a struct resource, allocated by the caller
3399 *
3400 * Retrieve MPU IRQ, SDMA request line, or address space start/end
3401 * data for the IP block pointed to by @oh. The data will be filled
3402 * into a struct resource record pointed to by @rsrc. The struct
3403 * resource must be allocated by the caller. When @name is non-null,
3404 * the data associated with the matching entry in the IRQ/SDMA/address
3405 * space hwmod data arrays will be returned. If @name is null, the
3406 * first array entry will be returned. Data order is not meaningful
3407 * in hwmod data, so callers are strongly encouraged to use a non-null
3408 * @name whenever possible to avoid unpredictable effects if hwmod
3409 * data is later added that causes data ordering to change. This
3410 * function is only intended for use by OMAP core code. Device
3411 * drivers should not call this function - the appropriate bus-related
3412 * data accessor functions should be used instead. Returns 0 upon
3413 * success or a negative error code upon error.
3414 */
3415int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
3416 const char *name, struct resource *rsrc)
3417{
3418 int r;
3419 unsigned int irq, dma;
3420 u32 pa_start, pa_end;
3421
3422 if (!oh || !rsrc)
3423 return -EINVAL;
3424
3425 if (type == IORESOURCE_IRQ) {
3426 r = _get_mpu_irq_by_name(oh, name, &irq);
3427 if (r)
3428 return r;
3429
3430 rsrc->start = irq;
3431 rsrc->end = irq;
3432 } else if (type == IORESOURCE_DMA) {
3433 r = _get_sdma_req_by_name(oh, name, &dma);
3434 if (r)
3435 return r;
3436
3437 rsrc->start = dma;
3438 rsrc->end = dma;
3439 } else if (type == IORESOURCE_MEM) {
3440 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
3441 if (r)
3442 return r;
3443
3444 rsrc->start = pa_start;
3445 rsrc->end = pa_end;
3446 } else {
3447 return -EINVAL;
3448 }
3449
3450 rsrc->flags = type;
3451 rsrc->name = name;
3452
3453 return 0;
3454}
3455
3456/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003457 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
3458 * @oh: struct omap_hwmod *
3459 *
3460 * Return the powerdomain pointer associated with the OMAP module
3461 * @oh's main clock. If @oh does not have a main clk, return the
3462 * powerdomain associated with the interface clock associated with the
3463 * module's MPU port. (XXX Perhaps this should use the SDMA port
3464 * instead?) Returns NULL on error, or a struct powerdomain * on
3465 * success.
3466 */
3467struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
3468{
3469 struct clk *c;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003470 struct omap_hwmod_ocp_if *oi;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003471 struct clockdomain *clkdm;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003472 struct clk_hw_omap *clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03003473
3474 if (!oh)
3475 return NULL;
3476
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003477 if (oh->clkdm)
3478 return oh->clkdm->pwrdm.ptr;
3479
Paul Walmsley63c85232009-09-03 20:14:03 +03003480 if (oh->_clk) {
3481 c = oh->_clk;
3482 } else {
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003483 oi = _find_mpu_rt_port(oh);
3484 if (!oi)
Paul Walmsley63c85232009-09-03 20:14:03 +03003485 return NULL;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003486 c = oi->_clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03003487 }
3488
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003489 clk = to_clk_hw_omap(__clk_get_hw(c));
3490 clkdm = clk->clkdm;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003491 if (!clkdm)
Thara Gopinathd5647c12010-03-31 04:16:29 -06003492 return NULL;
3493
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003494 return clkdm->pwrdm.ptr;
Paul Walmsley63c85232009-09-03 20:14:03 +03003495}
3496
3497/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06003498 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3499 * @oh: struct omap_hwmod *
3500 *
3501 * Returns the virtual address corresponding to the beginning of the
3502 * module's register target, in the address range that is intended to
3503 * be used by the MPU. Returns the virtual address upon success or NULL
3504 * upon error.
3505 */
3506void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3507{
3508 if (!oh)
3509 return NULL;
3510
3511 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3512 return NULL;
3513
3514 if (oh->_state == _HWMOD_STATE_UNKNOWN)
3515 return NULL;
3516
3517 return oh->_mpu_rt_va;
3518}
3519
Paul Walmsley63c85232009-09-03 20:14:03 +03003520/*
3521 * XXX what about functions for drivers to save/restore ocp_sysconfig
3522 * for context save/restore operations?
3523 */
3524
3525/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003526 * omap_hwmod_enable_wakeup - allow device to wake up the system
3527 * @oh: struct omap_hwmod *
3528 *
3529 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
Govindraj.R2a1cc142012-04-05 02:59:32 -06003530 * send wakeups to the PRCM, and enable I/O ring wakeup events for
3531 * this IP block if it has dynamic mux entries. Eventually this
3532 * should set PRCM wakeup registers to cause the PRCM to receive
3533 * wakeup events from the module. Does not set any wakeup routing
3534 * registers beyond this point - if the module is to wake up any other
3535 * module or subsystem, that must be set separately. Called by
3536 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003537 */
3538int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3539{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003540 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003541 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003542
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003543 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003544
3545 if (oh->class->sysc &&
3546 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3547 v = oh->_sysc_cache;
3548 _enable_wakeup(oh, &v);
3549 _write_sysconfig(v, oh);
3550 }
3551
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003552 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003553
3554 return 0;
3555}
3556
3557/**
3558 * omap_hwmod_disable_wakeup - prevent device from waking the system
3559 * @oh: struct omap_hwmod *
3560 *
3561 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
Govindraj.R2a1cc142012-04-05 02:59:32 -06003562 * from sending wakeups to the PRCM, and disable I/O ring wakeup
3563 * events for this IP block if it has dynamic mux entries. Eventually
3564 * this should clear PRCM wakeup registers to cause the PRCM to ignore
3565 * wakeup events from the module. Does not set any wakeup routing
3566 * registers beyond this point - if the module is to wake up any other
3567 * module or subsystem, that must be set separately. Called by
3568 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003569 */
3570int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3571{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003572 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003573 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003574
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003575 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003576
3577 if (oh->class->sysc &&
3578 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3579 v = oh->_sysc_cache;
3580 _disable_wakeup(oh, &v);
3581 _write_sysconfig(v, oh);
3582 }
3583
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003584 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003585
3586 return 0;
3587}
Paul Walmsley43b40992010-02-22 22:09:34 -07003588
3589/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003590 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3591 * contained in the hwmod module.
3592 * @oh: struct omap_hwmod *
3593 * @name: name of the reset line to lookup and assert
3594 *
3595 * Some IP like dsp, ipu or iva contain processor that require
3596 * an HW reset line to be assert / deassert in order to enable fully
3597 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3598 * yet supported on this OMAP; otherwise, passes along the return value
3599 * from _assert_hardreset().
3600 */
3601int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3602{
3603 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003604 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003605
3606 if (!oh)
3607 return -EINVAL;
3608
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003609 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003610 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003611 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003612
3613 return ret;
3614}
3615
3616/**
3617 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3618 * contained in the hwmod module.
3619 * @oh: struct omap_hwmod *
3620 * @name: name of the reset line to look up and deassert
3621 *
3622 * Some IP like dsp, ipu or iva contain processor that require
3623 * an HW reset line to be assert / deassert in order to enable fully
3624 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3625 * yet supported on this OMAP; otherwise, passes along the return value
3626 * from _deassert_hardreset().
3627 */
3628int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3629{
3630 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003631 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003632
3633 if (!oh)
3634 return -EINVAL;
3635
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003636 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003637 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003638 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003639
3640 return ret;
3641}
3642
3643/**
Paul Walmsley43b40992010-02-22 22:09:34 -07003644 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3645 * @classname: struct omap_hwmod_class name to search for
3646 * @fn: callback function pointer to call for each hwmod in class @classname
3647 * @user: arbitrary context data to pass to the callback function
3648 *
Benoit Coussonce35b242010-12-21 21:31:28 -07003649 * For each omap_hwmod of class @classname, call @fn.
3650 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07003651 * zero, the iterator is terminated, and the callback function's return
3652 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3653 * if @classname or @fn are NULL, or passes back the error code from @fn.
3654 */
3655int omap_hwmod_for_each_by_class(const char *classname,
3656 int (*fn)(struct omap_hwmod *oh,
3657 void *user),
3658 void *user)
3659{
3660 struct omap_hwmod *temp_oh;
3661 int ret = 0;
3662
3663 if (!classname || !fn)
3664 return -EINVAL;
3665
3666 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3667 __func__, classname);
3668
Paul Walmsley43b40992010-02-22 22:09:34 -07003669 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3670 if (!strcmp(temp_oh->class->name, classname)) {
3671 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3672 __func__, temp_oh->name);
3673 ret = (*fn)(temp_oh, user);
3674 if (ret)
3675 break;
3676 }
3677 }
3678
Paul Walmsley43b40992010-02-22 22:09:34 -07003679 if (ret)
3680 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3681 __func__, ret);
3682
3683 return ret;
3684}
3685
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003686/**
3687 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3688 * @oh: struct omap_hwmod *
3689 * @state: state that _setup() should leave the hwmod in
3690 *
Paul Walmsley550c8092011-02-28 11:58:14 -07003691 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsley64813c32012-04-18 19:10:03 -06003692 * (called by omap_hwmod_setup_*()). See also the documentation
3693 * for _setup_postsetup(), above. Returns 0 upon success or
3694 * -EINVAL if there is a problem with the arguments or if the hwmod is
3695 * in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003696 */
3697int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3698{
3699 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003700 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003701
3702 if (!oh)
3703 return -EINVAL;
3704
3705 if (state != _HWMOD_STATE_DISABLED &&
3706 state != _HWMOD_STATE_ENABLED &&
3707 state != _HWMOD_STATE_IDLE)
3708 return -EINVAL;
3709
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003710 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003711
3712 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3713 ret = -EINVAL;
3714 goto ohsps_unlock;
3715 }
3716
3717 oh->_postsetup_state = state;
3718 ret = 0;
3719
3720ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003721 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003722
3723 return ret;
3724}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003725
3726/**
3727 * omap_hwmod_get_context_loss_count - get lost context count
3728 * @oh: struct omap_hwmod *
3729 *
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07003730 * Returns the context loss count of associated @oh
3731 * upon success, or zero if no context loss data is available.
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003732 *
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07003733 * On OMAP4, this queries the per-hwmod context loss register,
3734 * assuming one exists. If not, or on OMAP2/3, this queries the
3735 * enclosing powerdomain context loss count.
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003736 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03003737int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003738{
3739 struct powerdomain *pwrdm;
3740 int ret = 0;
3741
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07003742 if (soc_ops.get_context_lost)
3743 return soc_ops.get_context_lost(oh);
3744
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003745 pwrdm = omap_hwmod_get_pwrdm(oh);
3746 if (pwrdm)
3747 ret = pwrdm_get_context_loss_count(pwrdm);
3748
3749 return ret;
3750}
Paul Walmsley43b01642011-03-10 03:50:07 -07003751
3752/**
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003753 * omap_hwmod_init - initialize the hwmod code
3754 *
3755 * Sets up some function pointers needed by the hwmod code to operate on the
3756 * currently-booted SoC. Intended to be called once during kernel init
3757 * before any hwmods are registered. No return value.
3758 */
3759void __init omap_hwmod_init(void)
3760{
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -06003761 if (cpu_is_omap24xx()) {
Tero Kristo9002e9212014-10-27 08:39:23 -07003762 soc_ops.wait_target_ready = _omap2xxx_3xxx_wait_target_ready;
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -06003763 soc_ops.assert_hardreset = _omap2_assert_hardreset;
3764 soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
3765 soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
3766 } else if (cpu_is_omap34xx()) {
Tero Kristo9002e9212014-10-27 08:39:23 -07003767 soc_ops.wait_target_ready = _omap2xxx_3xxx_wait_target_ready;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06003768 soc_ops.assert_hardreset = _omap2_assert_hardreset;
3769 soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
3770 soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
Tero Kristo0385c582013-07-17 18:03:25 +03003771 soc_ops.init_clkdm = _init_clkdm;
Rajendra Nayakdebcd1f2013-07-02 18:20:08 +05303772 } else if (cpu_is_omap44xx() || soc_is_omap54xx() || soc_is_dra7xx()) {
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003773 soc_ops.enable_module = _omap4_enable_module;
3774 soc_ops.disable_module = _omap4_disable_module;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003775 soc_ops.wait_target_ready = _omap4_wait_target_ready;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06003776 soc_ops.assert_hardreset = _omap4_assert_hardreset;
3777 soc_ops.deassert_hardreset = _omap4_deassert_hardreset;
3778 soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
Kevin Hilman0a179ea2012-06-18 12:12:25 -06003779 soc_ops.init_clkdm = _init_clkdm;
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07003780 soc_ops.update_context_lost = _omap4_update_context_lost;
3781 soc_ops.get_context_lost = _omap4_get_context_lost;
Tero Kristo9fabc1a2016-07-04 14:11:48 +03003782 soc_ops.disable_direct_prcm = _omap4_disable_direct_prcm;
Tony Lindgren0f3ccb22015-07-16 01:55:58 -07003783 } else if (cpu_is_ti814x() || cpu_is_ti816x() || soc_is_am33xx() ||
3784 soc_is_am43xx()) {
Afzal Mohammedc8b428a2013-10-12 15:46:20 +05303785 soc_ops.enable_module = _omap4_enable_module;
3786 soc_ops.disable_module = _omap4_disable_module;
3787 soc_ops.wait_target_ready = _omap4_wait_target_ready;
Tero Kristo409d7062014-10-27 08:39:24 -07003788 soc_ops.assert_hardreset = _omap4_assert_hardreset;
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06003789 soc_ops.deassert_hardreset = _am33xx_deassert_hardreset;
Tero Kristoa5bf00c2015-05-05 16:33:05 +03003790 soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06003791 soc_ops.init_clkdm = _init_clkdm;
Tero Kristo9fabc1a2016-07-04 14:11:48 +03003792 soc_ops.disable_direct_prcm = _omap4_disable_direct_prcm;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003793 } else {
3794 WARN(1, "omap_hwmod: unknown SoC type\n");
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003795 }
3796
3797 inited = true;
3798}
Tony Lindgren68c9a952012-07-06 00:58:43 -07003799
3800/**
3801 * omap_hwmod_get_main_clk - get pointer to main clock name
3802 * @oh: struct omap_hwmod *
3803 *
3804 * Returns the main clock name assocated with @oh upon success,
3805 * or NULL if @oh is NULL.
3806 */
3807const char *omap_hwmod_get_main_clk(struct omap_hwmod *oh)
3808{
3809 if (!oh)
3810 return NULL;
3811
3812 return oh->main_clk;
3813}