blob: 8bcea0d83fa09e8e4985d81697097d1756d34931 [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/**
224 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
225 * @oh: struct omap_hwmod *
226 *
227 * Load the current value of the hwmod OCP_SYSCONFIG register into the
228 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
229 * OCP_SYSCONFIG register or 0 upon success.
230 */
231static int _update_sysc_cache(struct omap_hwmod *oh)
232{
Paul Walmsley43b40992010-02-22 22:09:34 -0700233 if (!oh->class->sysc) {
234 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 +0300235 return -EINVAL;
236 }
237
238 /* XXX ensure module interface clock is up */
239
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -0700240 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300241
Paul Walmsley43b40992010-02-22 22:09:34 -0700242 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
Thara Gopinath883edfd2010-01-19 17:30:51 -0700243 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300244
245 return 0;
246}
247
248/**
249 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
250 * @v: OCP_SYSCONFIG value to write
251 * @oh: struct omap_hwmod *
252 *
Paul Walmsley43b40992010-02-22 22:09:34 -0700253 * Write @v into the module class' OCP_SYSCONFIG register, if it has
254 * one. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +0300255 */
256static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
257{
Paul Walmsley43b40992010-02-22 22:09:34 -0700258 if (!oh->class->sysc) {
259 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 +0300260 return;
261 }
262
263 /* XXX ensure module interface clock is up */
264
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700265 /* Module might have lost context, always update cache and register */
266 oh->_sysc_cache = v;
Lokesh Vutlaaaf2c0f2015-06-10 14:56:24 +0530267
268 /*
269 * Some IP blocks (such as RTC) require unlocking of IP before
270 * accessing its registers. If a function pointer is present
271 * to unlock, then call it before accessing sysconfig and
272 * call lock after writing sysconfig.
273 */
274 if (oh->class->unlock)
275 oh->class->unlock(oh);
276
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700277 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Lokesh Vutlaaaf2c0f2015-06-10 14:56:24 +0530278
279 if (oh->class->lock)
280 oh->class->lock(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +0300281}
282
283/**
284 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
285 * @oh: struct omap_hwmod *
286 * @standbymode: MIDLEMODE field bits
287 * @v: pointer to register contents to modify
288 *
289 * Update the master standby mode bits in @v to be @standbymode for
290 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
291 * upon error or 0 upon success.
292 */
293static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
294 u32 *v)
295{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700296 u32 mstandby_mask;
297 u8 mstandby_shift;
298
Paul Walmsley43b40992010-02-22 22:09:34 -0700299 if (!oh->class->sysc ||
300 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300301 return -EINVAL;
302
Paul Walmsley43b40992010-02-22 22:09:34 -0700303 if (!oh->class->sysc->sysc_fields) {
304 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700305 return -EINVAL;
306 }
307
Paul Walmsley43b40992010-02-22 22:09:34 -0700308 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700309 mstandby_mask = (0x3 << mstandby_shift);
310
311 *v &= ~mstandby_mask;
312 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300313
314 return 0;
315}
316
317/**
318 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
319 * @oh: struct omap_hwmod *
320 * @idlemode: SIDLEMODE field bits
321 * @v: pointer to register contents to modify
322 *
323 * Update the slave idle mode bits in @v to be @idlemode for the @oh
324 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
325 * or 0 upon success.
326 */
327static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
328{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700329 u32 sidle_mask;
330 u8 sidle_shift;
331
Paul Walmsley43b40992010-02-22 22:09:34 -0700332 if (!oh->class->sysc ||
333 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300334 return -EINVAL;
335
Paul Walmsley43b40992010-02-22 22:09:34 -0700336 if (!oh->class->sysc->sysc_fields) {
337 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700338 return -EINVAL;
339 }
340
Paul Walmsley43b40992010-02-22 22:09:34 -0700341 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700342 sidle_mask = (0x3 << sidle_shift);
343
344 *v &= ~sidle_mask;
345 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300346
347 return 0;
348}
349
350/**
351 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
352 * @oh: struct omap_hwmod *
353 * @clockact: CLOCKACTIVITY field bits
354 * @v: pointer to register contents to modify
355 *
356 * Update the clockactivity mode bits in @v to be @clockact for the
357 * @oh hwmod. Used for additional powersaving on some modules. Does
358 * not write to the hardware. Returns -EINVAL upon error or 0 upon
359 * success.
360 */
361static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
362{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700363 u32 clkact_mask;
364 u8 clkact_shift;
365
Paul Walmsley43b40992010-02-22 22:09:34 -0700366 if (!oh->class->sysc ||
367 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300368 return -EINVAL;
369
Paul Walmsley43b40992010-02-22 22:09:34 -0700370 if (!oh->class->sysc->sysc_fields) {
371 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700372 return -EINVAL;
373 }
374
Paul Walmsley43b40992010-02-22 22:09:34 -0700375 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700376 clkact_mask = (0x3 << clkact_shift);
377
378 *v &= ~clkact_mask;
379 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300380
381 return 0;
382}
383
384/**
Roger Quadros313a76e2013-12-08 18:39:02 -0700385 * _set_softreset: set OCP_SYSCONFIG.SOFTRESET bit in @v
Paul Walmsley63c85232009-09-03 20:14:03 +0300386 * @oh: struct omap_hwmod *
387 * @v: pointer to register contents to modify
388 *
389 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
390 * error or 0 upon success.
391 */
392static int _set_softreset(struct omap_hwmod *oh, u32 *v)
393{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700394 u32 softrst_mask;
395
Paul Walmsley43b40992010-02-22 22:09:34 -0700396 if (!oh->class->sysc ||
397 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300398 return -EINVAL;
399
Paul Walmsley43b40992010-02-22 22:09:34 -0700400 if (!oh->class->sysc->sysc_fields) {
401 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700402 return -EINVAL;
403 }
404
Paul Walmsley43b40992010-02-22 22:09:34 -0700405 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700406
407 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300408
409 return 0;
410}
411
412/**
Roger Quadros313a76e2013-12-08 18:39:02 -0700413 * _clear_softreset: clear OCP_SYSCONFIG.SOFTRESET bit in @v
414 * @oh: struct omap_hwmod *
415 * @v: pointer to register contents to modify
416 *
417 * Clear the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
418 * error or 0 upon success.
419 */
420static int _clear_softreset(struct omap_hwmod *oh, u32 *v)
421{
422 u32 softrst_mask;
423
424 if (!oh->class->sysc ||
425 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
426 return -EINVAL;
427
428 if (!oh->class->sysc->sysc_fields) {
429 WARN(1,
430 "omap_hwmod: %s: sysc_fields absent for sysconfig class\n",
431 oh->name);
432 return -EINVAL;
433 }
434
435 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
436
437 *v &= ~softrst_mask;
438
439 return 0;
440}
441
442/**
Tero Kristo613ad0e2012-10-29 22:02:13 -0600443 * _wait_softreset_complete - wait for an OCP softreset to complete
444 * @oh: struct omap_hwmod * to wait on
445 *
446 * Wait until the IP block represented by @oh reports that its OCP
447 * softreset is complete. This can be triggered by software (see
448 * _ocp_softreset()) or by hardware upon returning from off-mode (one
449 * example is HSMMC). Waits for up to MAX_MODULE_SOFTRESET_WAIT
450 * microseconds. Returns the number of microseconds waited.
451 */
452static int _wait_softreset_complete(struct omap_hwmod *oh)
453{
454 struct omap_hwmod_class_sysconfig *sysc;
455 u32 softrst_mask;
456 int c = 0;
457
458 sysc = oh->class->sysc;
459
460 if (sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
461 omap_test_timeout((omap_hwmod_read(oh, sysc->syss_offs)
462 & SYSS_RESETDONE_MASK),
463 MAX_MODULE_SOFTRESET_WAIT, c);
464 else if (sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
465 softrst_mask = (0x1 << sysc->sysc_fields->srst_shift);
466 omap_test_timeout(!(omap_hwmod_read(oh, sysc->sysc_offs)
467 & softrst_mask),
468 MAX_MODULE_SOFTRESET_WAIT, c);
469 }
470
471 return c;
472}
473
474/**
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -0600475 * _set_dmadisable: set OCP_SYSCONFIG.DMADISABLE bit in @v
476 * @oh: struct omap_hwmod *
477 *
478 * The DMADISABLE bit is a semi-automatic bit present in sysconfig register
479 * of some modules. When the DMA must perform read/write accesses, the
480 * DMADISABLE bit is cleared by the hardware. But when the DMA must stop
481 * for power management, software must set the DMADISABLE bit back to 1.
482 *
483 * Set the DMADISABLE bit in @v for hwmod @oh. Returns -EINVAL upon
484 * error or 0 upon success.
485 */
486static int _set_dmadisable(struct omap_hwmod *oh)
487{
488 u32 v;
489 u32 dmadisable_mask;
490
491 if (!oh->class->sysc ||
492 !(oh->class->sysc->sysc_flags & SYSC_HAS_DMADISABLE))
493 return -EINVAL;
494
495 if (!oh->class->sysc->sysc_fields) {
496 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
497 return -EINVAL;
498 }
499
500 /* clocks must be on for this operation */
501 if (oh->_state != _HWMOD_STATE_ENABLED) {
502 pr_warn("omap_hwmod: %s: dma can be disabled only from enabled state\n", oh->name);
503 return -EINVAL;
504 }
505
506 pr_debug("omap_hwmod: %s: setting DMADISABLE\n", oh->name);
507
508 v = oh->_sysc_cache;
509 dmadisable_mask =
510 (0x1 << oh->class->sysc->sysc_fields->dmadisable_shift);
511 v |= dmadisable_mask;
512 _write_sysconfig(v, oh);
513
514 return 0;
515}
516
517/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700518 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
519 * @oh: struct omap_hwmod *
520 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
521 * @v: pointer to register contents to modify
522 *
523 * Update the module autoidle bit in @v to be @autoidle for the @oh
524 * hwmod. The autoidle bit controls whether the module can gate
525 * internal clocks automatically when it isn't doing anything; the
526 * exact function of this bit varies on a per-module basis. This
527 * function does not write to the hardware. Returns -EINVAL upon
528 * error or 0 upon success.
529 */
530static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
531 u32 *v)
532{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700533 u32 autoidle_mask;
534 u8 autoidle_shift;
535
Paul Walmsley43b40992010-02-22 22:09:34 -0700536 if (!oh->class->sysc ||
537 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700538 return -EINVAL;
539
Paul Walmsley43b40992010-02-22 22:09:34 -0700540 if (!oh->class->sysc->sysc_fields) {
541 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700542 return -EINVAL;
543 }
544
Paul Walmsley43b40992010-02-22 22:09:34 -0700545 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700546 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700547
548 *v &= ~autoidle_mask;
549 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700550
551 return 0;
552}
553
554/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300555 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
556 * @oh: struct omap_hwmod *
557 *
558 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
559 * upon error or 0 upon success.
560 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700561static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300562{
Paul Walmsley43b40992010-02-22 22:09:34 -0700563 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700564 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200565 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
566 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300567 return -EINVAL;
568
Paul Walmsley43b40992010-02-22 22:09:34 -0700569 if (!oh->class->sysc->sysc_fields) {
570 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700571 return -EINVAL;
572 }
573
Benoit Cousson1fe74112011-07-01 22:54:03 +0200574 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
575 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300576
Benoit Cousson86009eb2010-12-21 21:31:28 -0700577 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
578 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200579 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
580 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700581
Paul Walmsley63c85232009-09-03 20:14:03 +0300582 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
583
Paul Walmsley63c85232009-09-03 20:14:03 +0300584 return 0;
585}
586
587/**
588 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
589 * @oh: struct omap_hwmod *
590 *
591 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
592 * upon error or 0 upon success.
593 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700594static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300595{
Paul Walmsley43b40992010-02-22 22:09:34 -0700596 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700597 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200598 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
599 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300600 return -EINVAL;
601
Paul Walmsley43b40992010-02-22 22:09:34 -0700602 if (!oh->class->sysc->sysc_fields) {
603 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700604 return -EINVAL;
605 }
606
Benoit Cousson1fe74112011-07-01 22:54:03 +0200607 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
608 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300609
Benoit Cousson86009eb2010-12-21 21:31:28 -0700610 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
611 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200612 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
Djamil Elaidi561038f2012-06-17 11:57:51 -0600613 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700614
Paul Walmsley63c85232009-09-03 20:14:03 +0300615 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
616
Paul Walmsley63c85232009-09-03 20:14:03 +0300617 return 0;
618}
619
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700620static struct clockdomain *_get_clkdm(struct omap_hwmod *oh)
621{
Rajendra Nayakc4a1ea22012-04-27 16:32:53 +0530622 struct clk_hw_omap *clk;
623
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700624 if (oh->clkdm) {
625 return oh->clkdm;
626 } else if (oh->_clk) {
Tero Kristo924f9492013-07-12 12:26:41 +0300627 if (__clk_get_flags(oh->_clk) & CLK_IS_BASIC)
628 return NULL;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700629 clk = to_clk_hw_omap(__clk_get_hw(oh->_clk));
630 return clk->clkdm;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700631 }
632 return NULL;
633}
634
Paul Walmsley63c85232009-09-03 20:14:03 +0300635/**
636 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
637 * @oh: struct omap_hwmod *
638 *
639 * Prevent the hardware module @oh from entering idle while the
640 * hardare module initiator @init_oh is active. Useful when a module
641 * will be accessed by a particular initiator (e.g., if a module will
642 * be accessed by the IVA, there should be a sleepdep between the IVA
643 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700644 * mode. If the clockdomain is marked as not needing autodeps, return
645 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
646 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300647 */
648static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
649{
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700650 struct clockdomain *clkdm, *init_clkdm;
651
652 clkdm = _get_clkdm(oh);
653 init_clkdm = _get_clkdm(init_oh);
654
655 if (!clkdm || !init_clkdm)
Paul Walmsley63c85232009-09-03 20:14:03 +0300656 return -EINVAL;
657
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700658 if (clkdm && clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsley570b54c2011-03-10 03:50:09 -0700659 return 0;
660
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700661 return clkdm_add_sleepdep(clkdm, init_clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300662}
663
664/**
665 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
666 * @oh: struct omap_hwmod *
667 *
668 * Allow the hardware module @oh to enter idle while the hardare
669 * module initiator @init_oh is active. Useful when a module will not
670 * be accessed by a particular initiator (e.g., if a module will not
671 * be accessed by the IVA, there should be no sleepdep between the IVA
672 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700673 * mode. If the clockdomain is marked as not needing autodeps, return
674 * 0 without doing anything. Returns -EINVAL upon error or passes
675 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300676 */
677static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
678{
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700679 struct clockdomain *clkdm, *init_clkdm;
680
681 clkdm = _get_clkdm(oh);
682 init_clkdm = _get_clkdm(init_oh);
683
684 if (!clkdm || !init_clkdm)
Paul Walmsley63c85232009-09-03 20:14:03 +0300685 return -EINVAL;
686
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700687 if (clkdm && clkdm->flags & CLKDM_NO_AUTODEPS)
Paul Walmsley570b54c2011-03-10 03:50:09 -0700688 return 0;
689
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700690 return clkdm_del_sleepdep(clkdm, init_clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300691}
692
693/**
694 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
695 * @oh: struct omap_hwmod *
696 *
697 * Called from _init_clocks(). Populates the @oh _clk (main
Tero Kristo9fabc1a2016-07-04 14:11:48 +0300698 * functional clock pointer) if a clock matching the hwmod name is found,
699 * or a main_clk is present. Returns 0 on success or -EINVAL on error.
Paul Walmsley63c85232009-09-03 20:14:03 +0300700 */
701static int _init_main_clk(struct omap_hwmod *oh)
702{
Paul Walmsley63c85232009-09-03 20:14:03 +0300703 int ret = 0;
Tero Kristo9fabc1a2016-07-04 14:11:48 +0300704 char name[MOD_CLK_MAX_NAME_LEN];
705 struct clk *clk;
Maninder Singh5066d522016-12-08 09:40:30 +0530706 static const char modck[] = "_mod_ck";
Paul Walmsley63c85232009-09-03 20:14:03 +0300707
Maninder Singh5066d522016-12-08 09:40:30 +0530708 if (strlen(oh->name) >= MOD_CLK_MAX_NAME_LEN - strlen(modck))
Tero Kristo9fabc1a2016-07-04 14:11:48 +0300709 pr_warn("%s: warning: cropping name for %s\n", __func__,
710 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +0300711
Maninder Singh5066d522016-12-08 09:40:30 +0530712 strlcpy(name, oh->name, MOD_CLK_MAX_NAME_LEN - strlen(modck));
713 strlcat(name, modck, MOD_CLK_MAX_NAME_LEN);
Tero Kristo9fabc1a2016-07-04 14:11:48 +0300714
715 clk = clk_get(NULL, name);
716 if (!IS_ERR(clk)) {
717 oh->_clk = clk;
718 soc_ops.disable_direct_prcm(oh);
719 oh->main_clk = kstrdup(name, GFP_KERNEL);
720 } else {
721 if (!oh->main_clk)
722 return 0;
723
724 oh->_clk = clk_get(NULL, oh->main_clk);
725 }
726
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600727 if (IS_ERR(oh->_clk)) {
Joe Perches3d0cb732014-09-13 11:31:16 -0700728 pr_warn("omap_hwmod: %s: cannot clk_get main_clk %s\n",
729 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600730 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600731 }
Rajendra Nayak4d7cb452012-09-22 02:24:16 -0600732 /*
733 * HACK: This needs a re-visit once clk_prepare() is implemented
734 * to do something meaningful. Today its just a no-op.
735 * If clk_prepare() is used at some point to do things like
736 * voltage scaling etc, then this would have to be moved to
737 * some point where subsystems like i2c and pmic become
738 * available.
739 */
740 clk_prepare(oh->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300741
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -0700742 if (!_get_clkdm(oh))
Paul Walmsley3bb05db2012-09-23 17:28:18 -0600743 pr_debug("omap_hwmod: %s: missing clockdomain for %s.\n",
Rajendra Nayak5dcc3b92012-09-22 02:24:17 -0600744 oh->name, oh->main_clk);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700745
Paul Walmsley63c85232009-09-03 20:14:03 +0300746 return ret;
747}
748
749/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600750 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300751 * @oh: struct omap_hwmod *
752 *
753 * Called from _init_clocks(). Populates the @oh OCP slave interface
754 * clock pointers. Returns 0 on success or -EINVAL on error.
755 */
756static int _init_interface_clks(struct omap_hwmod *oh)
757{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600758 struct omap_hwmod_ocp_if *os;
Paul Walmsley63c85232009-09-03 20:14:03 +0300759 struct clk *c;
Paul Walmsley63c85232009-09-03 20:14:03 +0300760 int ret = 0;
761
Tony Lindgrenb8e1bdd2017-03-14 13:13:19 -0700762 list_for_each_entry(os, &oh->slave_ports, node) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700763 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300764 continue;
765
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600766 c = clk_get(NULL, os->clk);
767 if (IS_ERR(c)) {
Joe Perches3d0cb732014-09-13 11:31:16 -0700768 pr_warn("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
769 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300770 ret = -EINVAL;
Nishanth Menon0e7dc862013-12-08 18:39:03 -0700771 continue;
Benoit Coussondc759252010-06-23 18:15:12 -0600772 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300773 os->_clk = c;
Rajendra Nayak4d7cb452012-09-22 02:24:16 -0600774 /*
775 * HACK: This needs a re-visit once clk_prepare() is implemented
776 * to do something meaningful. Today its just a no-op.
777 * If clk_prepare() is used at some point to do things like
778 * voltage scaling etc, then this would have to be moved to
779 * some point where subsystems like i2c and pmic become
780 * available.
781 */
782 clk_prepare(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300783 }
784
785 return ret;
786}
787
788/**
789 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
790 * @oh: struct omap_hwmod *
791 *
792 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
793 * clock pointers. Returns 0 on success or -EINVAL on error.
794 */
795static int _init_opt_clks(struct omap_hwmod *oh)
796{
797 struct omap_hwmod_opt_clk *oc;
798 struct clk *c;
799 int i;
800 int ret = 0;
801
802 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Rajendra Nayak6ea74cb2012-09-22 02:24:16 -0600803 c = clk_get(NULL, oc->clk);
804 if (IS_ERR(c)) {
Joe Perches3d0cb732014-09-13 11:31:16 -0700805 pr_warn("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
806 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300807 ret = -EINVAL;
Nishanth Menon0e7dc862013-12-08 18:39:03 -0700808 continue;
Benoit Coussondc759252010-06-23 18:15:12 -0600809 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300810 oc->_clk = c;
Rajendra Nayak4d7cb452012-09-22 02:24:16 -0600811 /*
812 * HACK: This needs a re-visit once clk_prepare() is implemented
813 * to do something meaningful. Today its just a no-op.
814 * If clk_prepare() is used at some point to do things like
815 * voltage scaling etc, then this would have to be moved to
816 * some point where subsystems like i2c and pmic become
817 * available.
818 */
819 clk_prepare(oc->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300820 }
821
822 return ret;
823}
824
Peter Ujfalusic12ba8c2015-11-12 09:32:58 +0200825static void _enable_optional_clocks(struct omap_hwmod *oh)
826{
827 struct omap_hwmod_opt_clk *oc;
828 int i;
829
830 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
831
832 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
833 if (oc->_clk) {
834 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
835 __clk_get_name(oc->_clk));
836 clk_enable(oc->_clk);
837 }
838}
839
840static void _disable_optional_clocks(struct omap_hwmod *oh)
841{
842 struct omap_hwmod_opt_clk *oc;
843 int i;
844
845 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
846
847 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
848 if (oc->_clk) {
849 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
850 __clk_get_name(oc->_clk));
851 clk_disable(oc->_clk);
852 }
853}
854
Paul Walmsley63c85232009-09-03 20:14:03 +0300855/**
856 * _enable_clocks - enable hwmod main clock and interface clocks
857 * @oh: struct omap_hwmod *
858 *
859 * Enables all clocks necessary for register reads and writes to succeed
860 * on the hwmod @oh. Returns 0.
861 */
862static int _enable_clocks(struct omap_hwmod *oh)
863{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600864 struct omap_hwmod_ocp_if *os;
Paul Walmsley63c85232009-09-03 20:14:03 +0300865
866 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
867
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600868 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300869 clk_enable(oh->_clk);
870
Tony Lindgrenb8e1bdd2017-03-14 13:13:19 -0700871 list_for_each_entry(os, &oh->slave_ports, node) {
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600872 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
873 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300874 }
875
Peter Ujfalusic12ba8c2015-11-12 09:32:58 +0200876 if (oh->flags & HWMOD_OPT_CLKS_NEEDED)
877 _enable_optional_clocks(oh);
878
Paul Walmsley63c85232009-09-03 20:14:03 +0300879 /* The opt clocks are controlled by the device driver. */
880
881 return 0;
882}
883
884/**
885 * _disable_clocks - disable hwmod main clock and interface clocks
886 * @oh: struct omap_hwmod *
887 *
888 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
889 */
890static int _disable_clocks(struct omap_hwmod *oh)
891{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600892 struct omap_hwmod_ocp_if *os;
Paul Walmsley63c85232009-09-03 20:14:03 +0300893
894 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
895
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600896 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300897 clk_disable(oh->_clk);
898
Tony Lindgrenb8e1bdd2017-03-14 13:13:19 -0700899 list_for_each_entry(os, &oh->slave_ports, node) {
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600900 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
901 clk_disable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300902 }
903
Peter Ujfalusic12ba8c2015-11-12 09:32:58 +0200904 if (oh->flags & HWMOD_OPT_CLKS_NEEDED)
905 _disable_optional_clocks(oh);
906
Paul Walmsley63c85232009-09-03 20:14:03 +0300907 /* The opt clocks are controlled by the device driver. */
908
909 return 0;
910}
911
912/**
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600913 * _omap4_enable_module - enable CLKCTRL modulemode on OMAP4
Benoit Cousson45c38252011-07-10 05:56:33 -0600914 * @oh: struct omap_hwmod *
915 *
916 * Enables the PRCM module mode related to the hwmod @oh.
917 * No return value.
918 */
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600919static void _omap4_enable_module(struct omap_hwmod *oh)
Benoit Cousson45c38252011-07-10 05:56:33 -0600920{
Benoit Cousson45c38252011-07-10 05:56:33 -0600921 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
922 return;
923
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600924 pr_debug("omap_hwmod: %s: %s: %d\n",
925 oh->name, __func__, oh->prcm.omap4.modulemode);
Benoit Cousson45c38252011-07-10 05:56:33 -0600926
Tero Kristo128603f2014-10-27 08:39:24 -0700927 omap_cm_module_enable(oh->prcm.omap4.modulemode,
928 oh->clkdm->prcm_partition,
929 oh->clkdm->cm_inst, oh->prcm.omap4.clkctrl_offs);
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -0600930}
931
932/**
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800933 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
934 * @oh: struct omap_hwmod *
935 *
936 * Wait for a module @oh to enter slave idle. Returns 0 if the module
937 * does not have an IDLEST bit or if the module successfully enters
938 * slave idle; otherwise, pass along the return value of the
939 * appropriate *_cm*_wait_module_idle() function.
940 */
941static int _omap4_wait_target_disable(struct omap_hwmod *oh)
942{
Paul Walmsley2b026d12012-09-23 17:28:18 -0600943 if (!oh)
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800944 return -EINVAL;
945
Paul Walmsley2b026d12012-09-23 17:28:18 -0600946 if (oh->_int_flags & _HWMOD_NO_MPU_PORT || !oh->clkdm)
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800947 return 0;
948
949 if (oh->flags & HWMOD_NO_IDLEST)
950 return 0;
951
Dave Gerlach428929c2016-07-12 12:50:33 -0500952 if (!oh->prcm.omap4.clkctrl_offs &&
953 !(oh->prcm.omap4.flags & HWMOD_OMAP4_ZERO_CLKCTRL_OFFSET))
954 return 0;
955
Tero Kristoa8ae5af2014-10-27 08:39:23 -0700956 return omap_cm_wait_module_idle(oh->clkdm->prcm_partition,
957 oh->clkdm->cm_inst,
958 oh->prcm.omap4.clkctrl_offs, 0);
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -0600959}
960
961/**
Paul Walmsley212738a2011-07-09 19:14:06 -0600962 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
963 * @oh: struct omap_hwmod *oh
964 *
965 * Count and return the number of MPU IRQs associated with the hwmod
966 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
967 * NULL.
968 */
969static int _count_mpu_irqs(struct omap_hwmod *oh)
970{
971 struct omap_hwmod_irq_info *ohii;
972 int i = 0;
973
974 if (!oh || !oh->mpu_irqs)
975 return 0;
976
977 do {
978 ohii = &oh->mpu_irqs[i++];
979 } while (ohii->irq != -1);
980
sricharancc1b0762011-11-23 14:35:07 -0800981 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -0600982}
983
984/**
Paul Walmsleybc614952011-07-09 19:14:07 -0600985 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
986 * @oh: struct omap_hwmod *oh
987 *
988 * Count and return the number of SDMA request lines associated with
989 * the hwmod @oh. Used to allocate struct resource data. Returns 0
990 * if @oh is NULL.
991 */
992static int _count_sdma_reqs(struct omap_hwmod *oh)
993{
994 struct omap_hwmod_dma_info *ohdi;
995 int i = 0;
996
997 if (!oh || !oh->sdma_reqs)
998 return 0;
999
1000 do {
1001 ohdi = &oh->sdma_reqs[i++];
1002 } while (ohdi->dma_req != -1);
1003
sricharancc1b0762011-11-23 14:35:07 -08001004 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -06001005}
1006
1007/**
Paul Walmsley78183f32011-07-09 19:14:05 -06001008 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
1009 * @oh: struct omap_hwmod *oh
1010 *
1011 * Count and return the number of address space ranges associated with
1012 * the hwmod @oh. Used to allocate struct resource data. Returns 0
1013 * if @oh is NULL.
1014 */
1015static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
1016{
1017 struct omap_hwmod_addr_space *mem;
1018 int i = 0;
1019
1020 if (!os || !os->addr)
1021 return 0;
1022
1023 do {
1024 mem = &os->addr[i++];
1025 } while (mem->pa_start != mem->pa_end);
1026
sricharancc1b0762011-11-23 14:35:07 -08001027 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001028}
1029
1030/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001031 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
1032 * @oh: struct omap_hwmod * to operate on
1033 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
1034 * @irq: pointer to an unsigned int to store the MPU IRQ number to
1035 *
1036 * Retrieve a MPU hardware IRQ line number named by @name associated
1037 * with the IP block pointed to by @oh. The IRQ number will be filled
1038 * into the address pointed to by @dma. When @name is non-null, the
1039 * IRQ line number associated with the named entry will be returned.
1040 * If @name is null, the first matching entry will be returned. Data
1041 * order is not meaningful in hwmod data, so callers are strongly
1042 * encouraged to use a non-null @name whenever possible to avoid
1043 * unpredictable effects if hwmod data is later added that causes data
1044 * ordering to change. Returns 0 upon success or a negative error
1045 * code upon error.
1046 */
1047static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
1048 unsigned int *irq)
1049{
1050 int i;
1051 bool found = false;
1052
1053 if (!oh->mpu_irqs)
1054 return -ENOENT;
1055
1056 i = 0;
1057 while (oh->mpu_irqs[i].irq != -1) {
1058 if (name == oh->mpu_irqs[i].name ||
1059 !strcmp(name, oh->mpu_irqs[i].name)) {
1060 found = true;
1061 break;
1062 }
1063 i++;
1064 }
1065
1066 if (!found)
1067 return -ENOENT;
1068
1069 *irq = oh->mpu_irqs[i].irq;
1070
1071 return 0;
1072}
1073
1074/**
1075 * _get_sdma_req_by_name - fetch SDMA request line ID by name
1076 * @oh: struct omap_hwmod * to operate on
1077 * @name: pointer to the name of the SDMA request line to fetch (optional)
1078 * @dma: pointer to an unsigned int to store the request line ID to
1079 *
1080 * Retrieve an SDMA request line ID named by @name on the IP block
1081 * pointed to by @oh. The ID will be filled into the address pointed
1082 * to by @dma. When @name is non-null, the request line ID associated
1083 * with the named entry will be returned. If @name is null, the first
1084 * matching entry will be returned. Data order is not meaningful in
1085 * hwmod data, so callers are strongly encouraged to use a non-null
1086 * @name whenever possible to avoid unpredictable effects if hwmod
1087 * data is later added that causes data ordering to change. Returns 0
1088 * upon success or a negative error code upon error.
1089 */
1090static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
1091 unsigned int *dma)
1092{
1093 int i;
1094 bool found = false;
1095
1096 if (!oh->sdma_reqs)
1097 return -ENOENT;
1098
1099 i = 0;
1100 while (oh->sdma_reqs[i].dma_req != -1) {
1101 if (name == oh->sdma_reqs[i].name ||
1102 !strcmp(name, oh->sdma_reqs[i].name)) {
1103 found = true;
1104 break;
1105 }
1106 i++;
1107 }
1108
1109 if (!found)
1110 return -ENOENT;
1111
1112 *dma = oh->sdma_reqs[i].dma_req;
1113
1114 return 0;
1115}
1116
1117/**
1118 * _get_addr_space_by_name - fetch address space start & end by name
1119 * @oh: struct omap_hwmod * to operate on
1120 * @name: pointer to the name of the address space to fetch (optional)
1121 * @pa_start: pointer to a u32 to store the starting address to
1122 * @pa_end: pointer to a u32 to store the ending address to
1123 *
1124 * Retrieve address space start and end addresses for the IP block
1125 * pointed to by @oh. The data will be filled into the addresses
1126 * pointed to by @pa_start and @pa_end. When @name is non-null, the
1127 * address space data associated with the named entry will be
1128 * returned. If @name is null, the first matching entry will be
1129 * returned. Data order is not meaningful in hwmod data, so callers
1130 * are strongly encouraged to use a non-null @name whenever possible
1131 * to avoid unpredictable effects if hwmod data is later added that
1132 * causes data ordering to change. Returns 0 upon success or a
1133 * negative error code upon error.
1134 */
1135static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1136 u32 *pa_start, u32 *pa_end)
1137{
Tony Lindgrenb8e1bdd2017-03-14 13:13:19 -07001138 int j;
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001139 struct omap_hwmod_ocp_if *os;
1140 bool found = false;
1141
Tony Lindgrenb8e1bdd2017-03-14 13:13:19 -07001142 list_for_each_entry(os, &oh->slave_ports, node) {
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001143
1144 if (!os->addr)
1145 return -ENOENT;
1146
1147 j = 0;
1148 while (os->addr[j].pa_start != os->addr[j].pa_end) {
1149 if (name == os->addr[j].name ||
1150 !strcmp(name, os->addr[j].name)) {
1151 found = true;
1152 break;
1153 }
1154 j++;
1155 }
1156
1157 if (found)
1158 break;
1159 }
1160
1161 if (!found)
1162 return -ENOENT;
1163
1164 *pa_start = os->addr[j].pa_start;
1165 *pa_end = os->addr[j].pa_end;
1166
1167 return 0;
1168}
1169
1170/**
Paul Walmsley24dbc212012-04-19 04:04:29 -06001171 * _save_mpu_port_index - find and save the index to @oh's MPU port
Paul Walmsley63c85232009-09-03 20:14:03 +03001172 * @oh: struct omap_hwmod *
1173 *
Paul Walmsley24dbc212012-04-19 04:04:29 -06001174 * Determines the array index of the OCP slave port that the MPU uses
1175 * to address the device, and saves it into the struct omap_hwmod.
1176 * Intended to be called during hwmod registration only. No return
1177 * value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001178 */
Paul Walmsley24dbc212012-04-19 04:04:29 -06001179static void __init _save_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001180{
Paul Walmsley24dbc212012-04-19 04:04:29 -06001181 struct omap_hwmod_ocp_if *os = NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001182
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001183 if (!oh)
Paul Walmsley24dbc212012-04-19 04:04:29 -06001184 return;
1185
1186 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001187
Tony Lindgrenb8e1bdd2017-03-14 13:13:19 -07001188 list_for_each_entry(os, &oh->slave_ports, node) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001189 if (os->user & OCP_USER_MPU) {
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001190 oh->_mpu_port = os;
Paul Walmsley24dbc212012-04-19 04:04:29 -06001191 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001192 break;
1193 }
1194 }
1195
Paul Walmsley24dbc212012-04-19 04:04:29 -06001196 return;
Paul Walmsley63c85232009-09-03 20:14:03 +03001197}
1198
1199/**
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001200 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1201 * @oh: struct omap_hwmod *
1202 *
1203 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1204 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1205 * communicate with the IP block. This interface need not be directly
1206 * connected to the MPU (and almost certainly is not), but is directly
1207 * connected to the IP block represented by @oh. Returns a pointer
1208 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1209 * error or if there does not appear to be a path from the MPU to this
1210 * IP block.
1211 */
1212static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1213{
1214 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1215 return NULL;
1216
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001217 return oh->_mpu_port;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001218};
1219
1220/**
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001221 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
Paul Walmsley63c85232009-09-03 20:14:03 +03001222 * @oh: struct omap_hwmod *
1223 *
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001224 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1225 * the register target MPU address space; or returns NULL upon error.
Paul Walmsley63c85232009-09-03 20:14:03 +03001226 */
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001227static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001228{
1229 struct omap_hwmod_ocp_if *os;
1230 struct omap_hwmod_addr_space *mem;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001231 int found = 0, i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001232
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001233 os = _find_mpu_rt_port(oh);
Paul Walmsley24dbc212012-04-19 04:04:29 -06001234 if (!os || !os->addr)
Paul Walmsley78183f32011-07-09 19:14:05 -06001235 return NULL;
1236
1237 do {
1238 mem = &os->addr[i++];
1239 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +03001240 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001241 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +03001242
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001243 return (found) ? mem : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001244}
1245
1246/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001247 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001248 * @oh: struct omap_hwmod *
1249 *
Paul Walmsley006c7f12012-07-04 05:22:53 -06001250 * Ensure that the OCP_SYSCONFIG register for the IP block represented
1251 * by @oh is set to indicate to the PRCM that the IP block is active.
1252 * Usually this means placing the module into smart-idle mode and
1253 * smart-standby, but if there is a bug in the automatic idle handling
1254 * for the IP block, it may need to be placed into the force-idle or
1255 * no-idle variants of these modes. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001256 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001257static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001258{
Paul Walmsley43b40992010-02-22 22:09:34 -07001259 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001260 u32 v;
Paul Walmsley006c7f12012-07-04 05:22:53 -06001261 bool clkdm_act;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07001262 struct clockdomain *clkdm;
Paul Walmsley63c85232009-09-03 20:14:03 +03001263
Paul Walmsley43b40992010-02-22 22:09:34 -07001264 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001265 return;
1266
Tero Kristo613ad0e2012-10-29 22:02:13 -06001267 /*
1268 * Wait until reset has completed, this is needed as the IP
1269 * block is reset automatically by hardware in some cases
1270 * (off-mode for example), and the drivers require the
1271 * IP to be ready when they access it
1272 */
1273 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1274 _enable_optional_clocks(oh);
1275 _wait_softreset_complete(oh);
1276 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1277 _disable_optional_clocks(oh);
1278
Paul Walmsley63c85232009-09-03 20:14:03 +03001279 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001280 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001281
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07001282 clkdm = _get_clkdm(oh);
Paul Walmsley43b40992010-02-22 22:09:34 -07001283 if (sf & SYSC_HAS_SIDLEMODE) {
Rajendra Nayakca43ea32013-05-15 20:18:38 +05301284 if (oh->flags & HWMOD_SWSUP_SIDLE ||
1285 oh->flags & HWMOD_SWSUP_SIDLE_ACT) {
Rajendra Nayak355131712013-05-15 20:18:37 +05301286 idlemode = HWMOD_IDLEMODE_NO;
1287 } else {
1288 if (sf & SYSC_HAS_ENAWAKEUP)
1289 _enable_wakeup(oh, &v);
1290 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
1291 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1292 else
1293 idlemode = HWMOD_IDLEMODE_SMART;
1294 }
1295
1296 /*
1297 * This is special handling for some IPs like
1298 * 32k sync timer. Force them to idle!
1299 */
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07001300 clkdm_act = (clkdm && clkdm->flags & CLKDM_ACTIVE_WITH_MPU);
Paul Walmsley006c7f12012-07-04 05:22:53 -06001301 if (clkdm_act && !(oh->class->sysc->idlemodes &
1302 (SIDLE_SMART | SIDLE_SMART_WKUP)))
1303 idlemode = HWMOD_IDLEMODE_FORCE;
Rajendra Nayak355131712013-05-15 20:18:37 +05301304
Paul Walmsley63c85232009-09-03 20:14:03 +03001305 _set_slave_idlemode(oh, idlemode, &v);
1306 }
1307
Paul Walmsley43b40992010-02-22 22:09:34 -07001308 if (sf & SYSC_HAS_MIDLEMODE) {
Grazvydas Ignotas092bc082013-03-11 21:49:00 +02001309 if (oh->flags & HWMOD_FORCE_MSTANDBY) {
1310 idlemode = HWMOD_IDLEMODE_FORCE;
1311 } else if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001312 idlemode = HWMOD_IDLEMODE_NO;
1313 } else {
1314 if (sf & SYSC_HAS_ENAWAKEUP)
1315 _enable_wakeup(oh, &v);
1316 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1317 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1318 else
1319 idlemode = HWMOD_IDLEMODE_SMART;
1320 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001321 _set_master_standbymode(oh, idlemode, &v);
1322 }
1323
Paul Walmsleya16b1f72009-12-08 16:34:17 -07001324 /*
1325 * XXX The clock framework should handle this, by
1326 * calling into this code. But this must wait until the
1327 * clock structures are tagged with omap_hwmod entries
1328 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001329 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1330 (sf & SYSC_HAS_CLOCKACTIVITY))
Tony Lindgrenca5339b2017-03-14 13:13:19 -07001331 _set_clockactivity(oh, CLOCKACT_TEST_ICLK, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001332
Lokesh Vutla3ca4a232016-03-26 23:08:55 -06001333 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -06001334
1335 /*
1336 * Set the autoidle bit only after setting the smartidle bit
1337 * Setting this will not have any impact on the other modules.
1338 */
1339 if (sf & SYSC_HAS_AUTOIDLE) {
1340 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1341 0 : 1;
1342 _set_module_autoidle(oh, idlemode, &v);
1343 _write_sysconfig(v, oh);
1344 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001345}
1346
1347/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001348 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001349 * @oh: struct omap_hwmod *
1350 *
1351 * If module is marked as SWSUP_SIDLE, force the module into slave
1352 * idle; otherwise, configure it for smart-idle. If module is marked
1353 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1354 * configure it for smart-standby. No return value.
1355 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001356static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001357{
Paul Walmsley43b40992010-02-22 22:09:34 -07001358 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001359 u32 v;
1360
Paul Walmsley43b40992010-02-22 22:09:34 -07001361 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001362 return;
1363
1364 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001365 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001366
Paul Walmsley43b40992010-02-22 22:09:34 -07001367 if (sf & SYSC_HAS_SIDLEMODE) {
Rajendra Nayak355131712013-05-15 20:18:37 +05301368 if (oh->flags & HWMOD_SWSUP_SIDLE) {
Paul Walmsley006c7f12012-07-04 05:22:53 -06001369 idlemode = HWMOD_IDLEMODE_FORCE;
Rajendra Nayak355131712013-05-15 20:18:37 +05301370 } else {
1371 if (sf & SYSC_HAS_ENAWAKEUP)
1372 _enable_wakeup(oh, &v);
1373 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
1374 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1375 else
1376 idlemode = HWMOD_IDLEMODE_SMART;
1377 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001378 _set_slave_idlemode(oh, idlemode, &v);
1379 }
1380
Paul Walmsley43b40992010-02-22 22:09:34 -07001381 if (sf & SYSC_HAS_MIDLEMODE) {
Grazvydas Ignotas092bc082013-03-11 21:49:00 +02001382 if ((oh->flags & HWMOD_SWSUP_MSTANDBY) ||
1383 (oh->flags & HWMOD_FORCE_MSTANDBY)) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001384 idlemode = HWMOD_IDLEMODE_FORCE;
1385 } else {
1386 if (sf & SYSC_HAS_ENAWAKEUP)
1387 _enable_wakeup(oh, &v);
1388 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1389 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1390 else
1391 idlemode = HWMOD_IDLEMODE_SMART;
1392 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001393 _set_master_standbymode(oh, idlemode, &v);
1394 }
1395
Lokesh Vutla3ca4a232016-03-26 23:08:55 -06001396 /* If the cached value is the same as the new value, skip the write */
1397 if (oh->_sysc_cache != v)
1398 _write_sysconfig(v, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001399}
1400
1401/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001402 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001403 * @oh: struct omap_hwmod *
1404 *
1405 * Force the module into slave idle and master suspend. No return
1406 * value.
1407 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001408static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001409{
1410 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001411 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001412
Paul Walmsley43b40992010-02-22 22:09:34 -07001413 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001414 return;
1415
1416 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001417 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001418
Paul Walmsley43b40992010-02-22 22:09:34 -07001419 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001420 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1421
Paul Walmsley43b40992010-02-22 22:09:34 -07001422 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001423 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1424
Paul Walmsley43b40992010-02-22 22:09:34 -07001425 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001426 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001427
1428 _write_sysconfig(v, oh);
1429}
1430
1431/**
1432 * _lookup - find an omap_hwmod by name
1433 * @name: find an omap_hwmod by name
1434 *
1435 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001436 */
1437static struct omap_hwmod *_lookup(const char *name)
1438{
1439 struct omap_hwmod *oh, *temp_oh;
1440
1441 oh = NULL;
1442
1443 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1444 if (!strcmp(name, temp_oh->name)) {
1445 oh = temp_oh;
1446 break;
1447 }
1448 }
1449
1450 return oh;
1451}
Paul Walmsley868c1572012-06-19 15:01:02 -06001452
Benoit Cousson6ae76992011-07-10 05:56:30 -06001453/**
1454 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1455 * @oh: struct omap_hwmod *
1456 *
1457 * Convert a clockdomain name stored in a struct omap_hwmod into a
1458 * clockdomain pointer, and save it into the struct omap_hwmod.
Paul Walmsley868c1572012-06-19 15:01:02 -06001459 * Return -EINVAL if the clkdm_name lookup failed.
Benoit Cousson6ae76992011-07-10 05:56:30 -06001460 */
1461static int _init_clkdm(struct omap_hwmod *oh)
1462{
Paul Walmsley3bb05db2012-09-23 17:28:18 -06001463 if (!oh->clkdm_name) {
1464 pr_debug("omap_hwmod: %s: missing clockdomain\n", oh->name);
Benoit Cousson6ae76992011-07-10 05:56:30 -06001465 return 0;
Paul Walmsley3bb05db2012-09-23 17:28:18 -06001466 }
Benoit Cousson6ae76992011-07-10 05:56:30 -06001467
Benoit Cousson6ae76992011-07-10 05:56:30 -06001468 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1469 if (!oh->clkdm) {
Joe Perches3d0cb732014-09-13 11:31:16 -07001470 pr_warn("omap_hwmod: %s: could not associate to clkdm %s\n",
Benoit Cousson6ae76992011-07-10 05:56:30 -06001471 oh->name, oh->clkdm_name);
Tero Kristo0385c582013-07-17 18:03:25 +03001472 return 0;
Benoit Cousson6ae76992011-07-10 05:56:30 -06001473 }
1474
1475 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1476 oh->name, oh->clkdm_name);
1477
1478 return 0;
1479}
Paul Walmsley63c85232009-09-03 20:14:03 +03001480
1481/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001482 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1483 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001484 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -06001485 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001486 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001487 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001488 * Resolves all clock names embedded in the hwmod. Returns 0 on
1489 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001490 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001491static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001492{
1493 int ret = 0;
1494
Paul Walmsley48d54f32011-02-23 00:14:07 -07001495 if (oh->_state != _HWMOD_STATE_REGISTERED)
1496 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001497
1498 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1499
Vaibhav Hiremathb797be1d2012-07-09 18:24:30 +05301500 if (soc_ops.init_clkdm)
1501 ret |= soc_ops.init_clkdm(oh);
1502
Paul Walmsley63c85232009-09-03 20:14:03 +03001503 ret |= _init_main_clk(oh);
1504 ret |= _init_interface_clks(oh);
1505 ret |= _init_opt_clks(oh);
1506
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001507 if (!ret)
1508 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001509 else
Joe Perches3d0cb732014-09-13 11:31:16 -07001510 pr_warn("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001511
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001512 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001513}
1514
1515/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001516 * _lookup_hardreset - fill register bit info for this hwmod/reset line
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001517 * @oh: struct omap_hwmod *
1518 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001519 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001520 *
1521 * Return the bit position of the reset line that match the
1522 * input name. Return -ENOENT if not found.
1523 */
Paul Walmsleya032d332012-08-03 09:21:10 -06001524static int _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1525 struct omap_hwmod_rst_info *ohri)
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001526{
1527 int i;
1528
1529 for (i = 0; i < oh->rst_lines_cnt; i++) {
1530 const char *rst_line = oh->rst_lines[i].name;
1531 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001532 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1533 ohri->st_shift = oh->rst_lines[i].st_shift;
1534 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1535 oh->name, __func__, rst_line, ohri->rst_shift,
1536 ohri->st_shift);
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001537
omar ramirezcc1226e2011-03-04 13:32:44 -07001538 return 0;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001539 }
1540 }
1541
1542 return -ENOENT;
1543}
1544
1545/**
1546 * _assert_hardreset - assert the HW reset line of submodules
1547 * contained in the hwmod module.
1548 * @oh: struct omap_hwmod *
1549 * @name: name of the reset line to lookup and assert
1550 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001551 * Some IP like dsp, ipu or iva contain processor that require an HW
1552 * reset line to be assert / deassert in order to enable fully the IP.
1553 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1554 * asserting the hardreset line on the currently-booted SoC, or passes
1555 * along the return value from _lookup_hardreset() or the SoC's
1556 * assert_hardreset code.
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001557 */
1558static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1559{
omar ramirezcc1226e2011-03-04 13:32:44 -07001560 struct omap_hwmod_rst_info ohri;
Paul Walmsleya032d332012-08-03 09:21:10 -06001561 int ret = -EINVAL;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001562
1563 if (!oh)
1564 return -EINVAL;
1565
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001566 if (!soc_ops.assert_hardreset)
1567 return -ENOSYS;
1568
omar ramirezcc1226e2011-03-04 13:32:44 -07001569 ret = _lookup_hardreset(oh, name, &ohri);
Paul Walmsleya032d332012-08-03 09:21:10 -06001570 if (ret < 0)
omar ramirezcc1226e2011-03-04 13:32:44 -07001571 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001572
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001573 ret = soc_ops.assert_hardreset(oh, &ohri);
1574
1575 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001576}
1577
1578/**
1579 * _deassert_hardreset - deassert the HW reset line of submodules contained
1580 * in the hwmod module.
1581 * @oh: struct omap_hwmod *
1582 * @name: name of the reset line to look up and deassert
1583 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001584 * Some IP like dsp, ipu or iva contain processor that require an HW
1585 * reset line to be assert / deassert in order to enable fully the IP.
1586 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1587 * deasserting the hardreset line on the currently-booted SoC, or passes
1588 * along the return value from _lookup_hardreset() or the SoC's
1589 * deassert_hardreset code.
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001590 */
1591static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1592{
omar ramirezcc1226e2011-03-04 13:32:44 -07001593 struct omap_hwmod_rst_info ohri;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001594 int ret = -EINVAL;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001595
1596 if (!oh)
1597 return -EINVAL;
1598
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001599 if (!soc_ops.deassert_hardreset)
1600 return -ENOSYS;
1601
omar ramirezcc1226e2011-03-04 13:32:44 -07001602 ret = _lookup_hardreset(oh, name, &ohri);
Russell Kingc48cd652013-03-13 20:44:21 +00001603 if (ret < 0)
omar ramirezcc1226e2011-03-04 13:32:44 -07001604 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001605
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001606 if (oh->clkdm) {
1607 /*
1608 * A clockdomain must be in SW_SUP otherwise reset
1609 * might not be completed. The clockdomain can be set
1610 * in HW_AUTO only when the module become ready.
1611 */
Tero Kristo1d9a5422016-06-30 16:15:02 +03001612 clkdm_deny_idle(oh->clkdm);
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001613 ret = clkdm_hwmod_enable(oh->clkdm, oh);
1614 if (ret) {
1615 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1616 oh->name, oh->clkdm->name, ret);
1617 return ret;
1618 }
1619 }
1620
1621 _enable_clocks(oh);
1622 if (soc_ops.enable_module)
1623 soc_ops.enable_module(oh);
1624
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001625 ret = soc_ops.deassert_hardreset(oh, &ohri);
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001626
1627 if (soc_ops.disable_module)
1628 soc_ops.disable_module(oh);
1629 _disable_clocks(oh);
1630
omar ramirezcc1226e2011-03-04 13:32:44 -07001631 if (ret == -EBUSY)
Joe Perches3d0cb732014-09-13 11:31:16 -07001632 pr_warn("omap_hwmod: %s: failed to hardreset\n", oh->name);
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001633
Tero Kristo80d25182015-02-26 18:06:00 +02001634 if (oh->clkdm) {
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001635 /*
1636 * Set the clockdomain to HW_AUTO, assuming that the
1637 * previous state was HW_AUTO.
1638 */
Tero Kristo1d9a5422016-06-30 16:15:02 +03001639 clkdm_allow_idle(oh->clkdm);
Tero Kristo80d25182015-02-26 18:06:00 +02001640
1641 clkdm_hwmod_disable(oh->clkdm, oh);
Omar Ramirez Lunae8e96df2012-09-23 17:28:21 -06001642 }
1643
omar ramirezcc1226e2011-03-04 13:32:44 -07001644 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001645}
1646
1647/**
1648 * _read_hardreset - read the HW reset line state of submodules
1649 * contained in the hwmod module
1650 * @oh: struct omap_hwmod *
1651 * @name: name of the reset line to look up and read
1652 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001653 * Return the state of the reset line. Returns -EINVAL if @oh is
1654 * null, -ENOSYS if we have no way of reading the hardreset line
1655 * status on the currently-booted SoC, or passes along the return
1656 * value from _lookup_hardreset() or the SoC's is_hardreset_asserted
1657 * code.
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001658 */
1659static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1660{
omar ramirezcc1226e2011-03-04 13:32:44 -07001661 struct omap_hwmod_rst_info ohri;
Paul Walmsleya032d332012-08-03 09:21:10 -06001662 int ret = -EINVAL;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001663
1664 if (!oh)
1665 return -EINVAL;
1666
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001667 if (!soc_ops.is_hardreset_asserted)
1668 return -ENOSYS;
1669
omar ramirezcc1226e2011-03-04 13:32:44 -07001670 ret = _lookup_hardreset(oh, name, &ohri);
Paul Walmsleya032d332012-08-03 09:21:10 -06001671 if (ret < 0)
omar ramirezcc1226e2011-03-04 13:32:44 -07001672 return ret;
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001673
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001674 return soc_ops.is_hardreset_asserted(oh, &ohri);
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06001675}
1676
1677/**
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001678 * _are_all_hardreset_lines_asserted - return true if the @oh is hard-reset
Paul Walmsley747834a2012-04-18 19:10:04 -06001679 * @oh: struct omap_hwmod *
1680 *
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001681 * If all hardreset lines associated with @oh are asserted, then return true.
1682 * Otherwise, if part of @oh is out hardreset or if no hardreset lines
1683 * associated with @oh are asserted, then return false.
Paul Walmsley747834a2012-04-18 19:10:04 -06001684 * This function is used to avoid executing some parts of the IP block
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001685 * enable/disable sequence if its hardreset line is set.
Paul Walmsley747834a2012-04-18 19:10:04 -06001686 */
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001687static bool _are_all_hardreset_lines_asserted(struct omap_hwmod *oh)
Paul Walmsley747834a2012-04-18 19:10:04 -06001688{
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001689 int i, rst_cnt = 0;
Paul Walmsley747834a2012-04-18 19:10:04 -06001690
1691 if (oh->rst_lines_cnt == 0)
1692 return false;
1693
1694 for (i = 0; i < oh->rst_lines_cnt; i++)
1695 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001696 rst_cnt++;
1697
1698 if (oh->rst_lines_cnt == rst_cnt)
1699 return true;
Paul Walmsley747834a2012-04-18 19:10:04 -06001700
1701 return false;
1702}
1703
1704/**
Paul Walmsleye9332b62012-10-08 23:08:15 -06001705 * _are_any_hardreset_lines_asserted - return true if any part of @oh is
1706 * hard-reset
1707 * @oh: struct omap_hwmod *
1708 *
1709 * If any hardreset lines associated with @oh are asserted, then
1710 * return true. Otherwise, if no hardreset lines associated with @oh
1711 * are asserted, or if @oh has no hardreset lines, then return false.
1712 * This function is used to avoid executing some parts of the IP block
1713 * enable/disable sequence if any hardreset line is set.
1714 */
1715static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1716{
1717 int rst_cnt = 0;
1718 int i;
1719
1720 for (i = 0; i < oh->rst_lines_cnt && rst_cnt == 0; i++)
1721 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1722 rst_cnt++;
1723
1724 return (rst_cnt) ? true : false;
1725}
1726
1727/**
Paul Walmsley747834a2012-04-18 19:10:04 -06001728 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1729 * @oh: struct omap_hwmod *
1730 *
1731 * Disable the PRCM module mode related to the hwmod @oh.
1732 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1733 */
1734static int _omap4_disable_module(struct omap_hwmod *oh)
1735{
1736 int v;
1737
Paul Walmsley747834a2012-04-18 19:10:04 -06001738 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1739 return -EINVAL;
1740
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001741 /*
1742 * Since integration code might still be doing something, only
1743 * disable if all lines are under hardreset.
1744 */
Paul Walmsleye9332b62012-10-08 23:08:15 -06001745 if (_are_any_hardreset_lines_asserted(oh))
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001746 return 0;
1747
Paul Walmsley747834a2012-04-18 19:10:04 -06001748 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1749
Tero Kristo128603f2014-10-27 08:39:24 -07001750 omap_cm_module_disable(oh->clkdm->prcm_partition, oh->clkdm->cm_inst,
1751 oh->prcm.omap4.clkctrl_offs);
Paul Walmsley747834a2012-04-18 19:10:04 -06001752
Paul Walmsley747834a2012-04-18 19:10:04 -06001753 v = _omap4_wait_target_disable(oh);
1754 if (v)
1755 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1756 oh->name);
1757
1758 return 0;
1759}
1760
1761/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001762 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001763 * @oh: struct omap_hwmod *
1764 *
1765 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Paul Walmsley30e105c2012-04-19 00:49:09 -06001766 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1767 * reset this way, -EINVAL if the hwmod is in the wrong state,
1768 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001769 *
1770 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001771 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001772 * use the SYSCONFIG softreset bit to provide the status.
1773 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001774 * Note that some IP like McBSP do have reset control but don't have
1775 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001776 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001777static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001778{
Tero Kristo613ad0e2012-10-29 22:02:13 -06001779 u32 v;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001780 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001781 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001782
Paul Walmsley43b40992010-02-22 22:09:34 -07001783 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001784 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley30e105c2012-04-19 00:49:09 -06001785 return -ENOENT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001786
1787 /* clocks must be on for this operation */
1788 if (oh->_state != _HWMOD_STATE_ENABLED) {
Paul Walmsley7852ec02012-07-26 00:54:26 -06001789 pr_warn("omap_hwmod: %s: reset can only be entered from enabled state\n",
1790 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001791 return -EINVAL;
1792 }
1793
Benoit Cousson96835af2010-09-21 18:57:58 +02001794 /* For some modules, all optionnal clocks need to be enabled as well */
1795 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1796 _enable_optional_clocks(oh);
1797
Paul Walmsleybd361792010-12-14 12:42:35 -07001798 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001799
1800 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001801 ret = _set_softreset(oh, &v);
1802 if (ret)
1803 goto dis_opt_clks;
Roger Quadros313a76e2013-12-08 18:39:02 -07001804
1805 _write_sysconfig(v, oh);
Illia Smyrnov01142512014-02-05 17:06:09 +02001806
1807 if (oh->class->sysc->srst_udelay)
1808 udelay(oh->class->sysc->srst_udelay);
1809
1810 c = _wait_softreset_complete(oh);
1811 if (c == MAX_MODULE_SOFTRESET_WAIT) {
Joe Perches3d0cb732014-09-13 11:31:16 -07001812 pr_warn("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1813 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Illia Smyrnov01142512014-02-05 17:06:09 +02001814 ret = -ETIMEDOUT;
1815 goto dis_opt_clks;
1816 } else {
1817 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
1818 }
1819
Roger Quadros313a76e2013-12-08 18:39:02 -07001820 ret = _clear_softreset(oh, &v);
1821 if (ret)
1822 goto dis_opt_clks;
1823
Paul Walmsley63c85232009-09-03 20:14:03 +03001824 _write_sysconfig(v, oh);
1825
Paul Walmsley63c85232009-09-03 20:14:03 +03001826 /*
1827 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1828 * _wait_target_ready() or _reset()
1829 */
1830
Benoit Cousson96835af2010-09-21 18:57:58 +02001831dis_opt_clks:
1832 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1833 _disable_optional_clocks(oh);
1834
1835 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001836}
1837
1838/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001839 * _reset - reset an omap_hwmod
1840 * @oh: struct omap_hwmod *
1841 *
Paul Walmsley30e105c2012-04-19 00:49:09 -06001842 * Resets an omap_hwmod @oh. If the module has a custom reset
1843 * function pointer defined, then call it to reset the IP block, and
1844 * pass along its return value to the caller. Otherwise, if the IP
1845 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1846 * associated with it, call a function to reset the IP block via that
1847 * method, and pass along the return value to the caller. Finally, if
1848 * the IP block has some hardreset lines associated with it, assert
1849 * all of those, but do _not_ deassert them. (This is because driver
1850 * authors have expressed an apparent requirement to control the
1851 * deassertion of the hardreset lines themselves.)
1852 *
1853 * The default software reset mechanism for most OMAP IP blocks is
1854 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1855 * hwmods cannot be reset via this method. Some are not targets and
1856 * therefore have no OCP header registers to access. Others (like the
1857 * IVA) have idiosyncratic reset sequences. So for these relatively
1858 * rare cases, custom reset code can be supplied in the struct
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -06001859 * omap_hwmod_class .reset function pointer.
1860 *
1861 * _set_dmadisable() is called to set the DMADISABLE bit so that it
1862 * does not prevent idling of the system. This is necessary for cases
1863 * where ROMCODE/BOOTLOADER uses dma and transfers control to the
1864 * kernel without disabling dma.
1865 *
1866 * Passes along the return value from either _ocp_softreset() or the
1867 * custom reset function - these must return -EINVAL if the hwmod
1868 * cannot be reset this way or if the hwmod is in the wrong state,
1869 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Paul Walmsleybd361792010-12-14 12:42:35 -07001870 */
1871static int _reset(struct omap_hwmod *oh)
1872{
Paul Walmsley30e105c2012-04-19 00:49:09 -06001873 int i, r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001874
1875 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1876
Paul Walmsley30e105c2012-04-19 00:49:09 -06001877 if (oh->class->reset) {
1878 r = oh->class->reset(oh);
1879 } else {
1880 if (oh->rst_lines_cnt > 0) {
1881 for (i = 0; i < oh->rst_lines_cnt; i++)
1882 _assert_hardreset(oh, oh->rst_lines[i].name);
1883 return 0;
1884 } else {
1885 r = _ocp_softreset(oh);
1886 if (r == -ENOENT)
1887 r = 0;
1888 }
1889 }
Paul Walmsleybd361792010-12-14 12:42:35 -07001890
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -06001891 _set_dmadisable(oh);
1892
Paul Walmsley30e105c2012-04-19 00:49:09 -06001893 /*
1894 * OCP_SYSCONFIG bits need to be reprogrammed after a
1895 * softreset. The _enable() function should be split to avoid
1896 * the rewrite of the OCP_SYSCONFIG register.
1897 */
Rajendra Nayak28008522012-03-13 22:55:23 +05301898 if (oh->class->sysc) {
1899 _update_sysc_cache(oh);
1900 _enable_sysc(oh);
1901 }
1902
Paul Walmsley30e105c2012-04-19 00:49:09 -06001903 return r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001904}
1905
1906/**
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07001907 * _omap4_update_context_lost - increment hwmod context loss counter if
1908 * hwmod context was lost, and clear hardware context loss reg
1909 * @oh: hwmod to check for context loss
1910 *
1911 * If the PRCM indicates that the hwmod @oh lost context, increment
1912 * our in-memory context loss counter, and clear the RM_*_CONTEXT
1913 * bits. No return value.
1914 */
1915static void _omap4_update_context_lost(struct omap_hwmod *oh)
1916{
1917 if (oh->prcm.omap4.flags & HWMOD_OMAP4_NO_CONTEXT_LOSS_BIT)
1918 return;
1919
1920 if (!prm_was_any_context_lost_old(oh->clkdm->pwrdm.ptr->prcm_partition,
1921 oh->clkdm->pwrdm.ptr->prcm_offs,
1922 oh->prcm.omap4.context_offs))
1923 return;
1924
1925 oh->prcm.omap4.context_lost_counter++;
1926 prm_clear_context_loss_flags_old(oh->clkdm->pwrdm.ptr->prcm_partition,
1927 oh->clkdm->pwrdm.ptr->prcm_offs,
1928 oh->prcm.omap4.context_offs);
1929}
1930
1931/**
1932 * _omap4_get_context_lost - get context loss counter for a hwmod
1933 * @oh: hwmod to get context loss counter for
1934 *
1935 * Returns the in-memory context loss counter for a hwmod.
1936 */
1937static int _omap4_get_context_lost(struct omap_hwmod *oh)
1938{
1939 return oh->prcm.omap4.context_lost_counter;
1940}
1941
1942/**
Paul Walmsley6d266f62013-02-10 11:22:22 -07001943 * _enable_preprogram - Pre-program an IP block during the _enable() process
1944 * @oh: struct omap_hwmod *
1945 *
1946 * Some IP blocks (such as AESS) require some additional programming
1947 * after enable before they can enter idle. If a function pointer to
1948 * do so is present in the hwmod data, then call it and pass along the
1949 * return value; otherwise, return 0.
1950 */
jean-philippe francois0f497032013-05-16 11:25:07 -07001951static int _enable_preprogram(struct omap_hwmod *oh)
Paul Walmsley6d266f62013-02-10 11:22:22 -07001952{
1953 if (!oh->class->enable_preprogram)
1954 return 0;
1955
1956 return oh->class->enable_preprogram(oh);
1957}
1958
1959/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001960 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001961 * @oh: struct omap_hwmod *
1962 *
1963 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001964 * register target. Returns -EINVAL if the hwmod is in the wrong
1965 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001966 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001967static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001968{
Paul Walmsley747834a2012-04-18 19:10:04 -06001969 int r;
Paul Walmsley63c85232009-09-03 20:14:03 +03001970
Benoit Cousson34617e22011-07-01 22:54:07 +02001971 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1972
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001973 /*
Paul Walmsley64813c32012-04-18 19:10:03 -06001974 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
Tony Lindgrenb4281452016-10-20 06:35:21 -07001975 * state at init.
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001976 */
1977 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001978 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
1979 return 0;
1980 }
1981
Paul Walmsley63c85232009-09-03 20:14:03 +03001982 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1983 oh->_state != _HWMOD_STATE_IDLE &&
1984 oh->_state != _HWMOD_STATE_DISABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001985 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
1986 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001987 return -EINVAL;
1988 }
1989
Benoit Cousson31f62862011-07-01 22:54:05 +02001990 /*
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001991 * If an IP block contains HW reset lines and all of them are
Paul Walmsley747834a2012-04-18 19:10:04 -06001992 * asserted, we let integration code associated with that
1993 * block handle the enable. We've received very little
1994 * information on what those driver authors need, and until
1995 * detailed information is provided and the driver code is
1996 * posted to the public lists, this is probably the best we
1997 * can do.
Benoit Cousson31f62862011-07-01 22:54:05 +02001998 */
Omar Ramirez Lunaeb05f692012-09-23 17:28:20 -06001999 if (_are_all_hardreset_lines_asserted(oh))
Paul Walmsley747834a2012-04-18 19:10:04 -06002000 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002001
Rajendra Nayak665d0012011-07-10 05:57:07 -06002002 _add_initiator_dep(oh, mpu_oh);
2003
2004 if (oh->clkdm) {
2005 /*
2006 * A clockdomain must be in SW_SUP before enabling
2007 * completely the module. The clockdomain can be set
2008 * in HW_AUTO only when the module become ready.
2009 */
Tero Kristo1d9a5422016-06-30 16:15:02 +03002010 clkdm_deny_idle(oh->clkdm);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002011 r = clkdm_hwmod_enable(oh->clkdm, oh);
2012 if (r) {
2013 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
2014 oh->name, oh->clkdm->name, r);
2015 return r;
2016 }
Benoit Cousson34617e22011-07-01 22:54:07 +02002017 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06002018
2019 _enable_clocks(oh);
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002020 if (soc_ops.enable_module)
2021 soc_ops.enable_module(oh);
Paul Walmsleyfa200222013-01-26 00:48:56 -07002022 if (oh->flags & HWMOD_BLOCK_WFI)
Thomas Gleixnerf7b861b2013-03-21 22:49:38 +01002023 cpu_idle_poll_ctrl(true);
Benoit Cousson34617e22011-07-01 22:54:07 +02002024
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07002025 if (soc_ops.update_context_lost)
2026 soc_ops.update_context_lost(oh);
2027
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002028 r = (soc_ops.wait_target_ready) ? soc_ops.wait_target_ready(oh) :
2029 -EINVAL;
Roger Quadros8ff42da2017-03-17 10:58:18 +02002030 if (oh->clkdm && !(oh->flags & HWMOD_CLKDM_NOAUTO))
Tero Kristo1d9a5422016-06-30 16:15:02 +03002031 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02002032
Tero Kristo1d9a5422016-06-30 16:15:02 +03002033 if (!r) {
Rajendra Nayak665d0012011-07-10 05:57:07 -06002034 oh->_state = _HWMOD_STATE_ENABLED;
2035
2036 /* Access the sysconfig only if the target is ready */
2037 if (oh->class->sysc) {
2038 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
2039 _update_sysc_cache(oh);
2040 _enable_sysc(oh);
2041 }
Paul Walmsley6d266f62013-02-10 11:22:22 -07002042 r = _enable_preprogram(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002043 } else {
Paul Walmsley2577a4a2012-10-29 20:57:55 -06002044 if (soc_ops.disable_module)
2045 soc_ops.disable_module(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002046 _disable_clocks(oh);
Lokesh Vutla812ce9d2014-12-19 18:04:50 +05302047 pr_err("omap_hwmod: %s: _wait_target_ready failed: %d\n",
2048 oh->name, r);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002049
2050 if (oh->clkdm)
2051 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002052 }
2053
Paul Walmsley63c85232009-09-03 20:14:03 +03002054 return r;
2055}
2056
2057/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002058 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03002059 * @oh: struct omap_hwmod *
2060 *
2061 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002062 * no further work. Returns -EINVAL if the hwmod is in the wrong
2063 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03002064 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002065static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002066{
Lokesh Vutla2e18f5a2016-03-07 01:41:21 -07002067 if (oh->flags & HWMOD_NO_IDLE) {
2068 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
2069 return 0;
2070 }
2071
Benoit Cousson34617e22011-07-01 22:54:07 +02002072 pr_debug("omap_hwmod: %s: idling\n", oh->name);
2073
Suman Annac20c8f72016-04-10 13:20:11 -06002074 if (_are_all_hardreset_lines_asserted(oh))
2075 return 0;
2076
Paul Walmsley63c85232009-09-03 20:14:03 +03002077 if (oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00002078 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
2079 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002080 return -EINVAL;
2081 }
2082
Paul Walmsley43b40992010-02-22 22:09:34 -07002083 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002084 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002085 _del_initiator_dep(oh, mpu_oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08002086
Roger Quadros8ff42da2017-03-17 10:58:18 +02002087 /*
2088 * If HWMOD_CLKDM_NOAUTO is set then we don't
2089 * deny idle the clkdm again since idle was already denied
2090 * in _enable()
2091 */
2092 if (oh->clkdm && !(oh->flags & HWMOD_CLKDM_NOAUTO))
Tero Kristo1d9a5422016-06-30 16:15:02 +03002093 clkdm_deny_idle(oh->clkdm);
2094
Paul Walmsleyfa200222013-01-26 00:48:56 -07002095 if (oh->flags & HWMOD_BLOCK_WFI)
Thomas Gleixnerf7b861b2013-03-21 22:49:38 +01002096 cpu_idle_poll_ctrl(false);
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002097 if (soc_ops.disable_module)
2098 soc_ops.disable_module(oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08002099
Benoit Cousson45c38252011-07-10 05:56:33 -06002100 /*
2101 * The module must be in idle mode before disabling any parents
2102 * clocks. Otherwise, the parent clock might be disabled before
2103 * the module transition is done, and thus will prevent the
2104 * transition to complete properly.
2105 */
2106 _disable_clocks(oh);
Tero Kristo1d9a5422016-06-30 16:15:02 +03002107 if (oh->clkdm) {
2108 clkdm_allow_idle(oh->clkdm);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002109 clkdm_hwmod_disable(oh->clkdm, oh);
Tero Kristo1d9a5422016-06-30 16:15:02 +03002110 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002111
2112 oh->_state = _HWMOD_STATE_IDLE;
2113
2114 return 0;
2115}
2116
2117/**
2118 * _shutdown - shutdown an omap_hwmod
2119 * @oh: struct omap_hwmod *
2120 *
2121 * Shut down an omap_hwmod @oh. This should be called when the driver
2122 * used for the hwmod is removed or unloaded or if the driver is not
2123 * used by the system. Returns -EINVAL if the hwmod is in the wrong
2124 * state or returns 0.
2125 */
2126static int _shutdown(struct omap_hwmod *oh)
2127{
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06002128 int ret, i;
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002129 u8 prev_state;
2130
Suman Annac20c8f72016-04-10 13:20:11 -06002131 if (_are_all_hardreset_lines_asserted(oh))
2132 return 0;
2133
Paul Walmsley63c85232009-09-03 20:14:03 +03002134 if (oh->_state != _HWMOD_STATE_IDLE &&
2135 oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00002136 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
2137 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002138 return -EINVAL;
2139 }
2140
2141 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
2142
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002143 if (oh->class->pre_shutdown) {
2144 prev_state = oh->_state;
2145 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002146 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002147 ret = oh->class->pre_shutdown(oh);
2148 if (ret) {
2149 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002150 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002151 return ret;
2152 }
2153 }
2154
Miguel Vadillo6481c732011-07-01 22:54:02 +02002155 if (oh->class->sysc) {
2156 if (oh->_state == _HWMOD_STATE_IDLE)
2157 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002158 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02002159 }
BenoƮt Cousson5365efb2010-09-21 10:34:11 -06002160
Benoit Cousson3827f942010-09-21 10:34:08 -06002161 /* clocks and deps are already disabled in idle */
2162 if (oh->_state == _HWMOD_STATE_ENABLED) {
2163 _del_initiator_dep(oh, mpu_oh);
2164 /* XXX what about the other system initiators here? dma, dsp */
Paul Walmsleyfa200222013-01-26 00:48:56 -07002165 if (oh->flags & HWMOD_BLOCK_WFI)
Thomas Gleixnerf7b861b2013-03-21 22:49:38 +01002166 cpu_idle_poll_ctrl(false);
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002167 if (soc_ops.disable_module)
2168 soc_ops.disable_module(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06002169 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002170 if (oh->clkdm)
2171 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06002172 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002173 /* XXX Should this code also force-disable the optional clocks? */
2174
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06002175 for (i = 0; i < oh->rst_lines_cnt; i++)
2176 _assert_hardreset(oh, oh->rst_lines[i].name);
Benoit Cousson31f62862011-07-01 22:54:05 +02002177
Paul Walmsley63c85232009-09-03 20:14:03 +03002178 oh->_state = _HWMOD_STATE_DISABLED;
2179
2180 return 0;
2181}
2182
Tony Lindgren5e863c52013-12-06 14:20:16 -08002183static int of_dev_find_hwmod(struct device_node *np,
2184 struct omap_hwmod *oh)
2185{
2186 int count, i, res;
2187 const char *p;
2188
2189 count = of_property_count_strings(np, "ti,hwmods");
2190 if (count < 1)
2191 return -ENODEV;
2192
2193 for (i = 0; i < count; i++) {
2194 res = of_property_read_string_index(np, "ti,hwmods",
2195 i, &p);
2196 if (res)
2197 continue;
2198 if (!strcmp(p, oh->name)) {
2199 pr_debug("omap_hwmod: dt %s[%i] uses hwmod %s\n",
2200 np->name, i, oh->name);
2201 return i;
2202 }
2203 }
2204
2205 return -ENODEV;
2206}
2207
Paul Walmsley63c85232009-09-03 20:14:03 +03002208/**
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302209 * of_dev_hwmod_lookup - look up needed hwmod from dt blob
2210 * @np: struct device_node *
2211 * @oh: struct omap_hwmod *
Tony Lindgren5e863c52013-12-06 14:20:16 -08002212 * @index: index of the entry found
2213 * @found: struct device_node * found or NULL
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302214 *
2215 * Parse the dt blob and find out needed hwmod. Recursive function is
2216 * implemented to take care hierarchical dt blob parsing.
Tony Lindgren5e863c52013-12-06 14:20:16 -08002217 * Return: Returns 0 on success, -ENODEV when not found.
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302218 */
Tony Lindgren5e863c52013-12-06 14:20:16 -08002219static int of_dev_hwmod_lookup(struct device_node *np,
2220 struct omap_hwmod *oh,
2221 int *index,
2222 struct device_node **found)
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302223{
Tony Lindgren5e863c52013-12-06 14:20:16 -08002224 struct device_node *np0 = NULL;
2225 int res;
2226
2227 res = of_dev_find_hwmod(np, oh);
2228 if (res >= 0) {
2229 *found = np;
2230 *index = res;
2231 return 0;
2232 }
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302233
2234 for_each_child_of_node(np, np0) {
Tony Lindgren5e863c52013-12-06 14:20:16 -08002235 struct device_node *fc;
2236 int i;
2237
2238 res = of_dev_hwmod_lookup(np0, oh, &i, &fc);
2239 if (res == 0) {
2240 *found = fc;
2241 *index = i;
2242 return 0;
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302243 }
2244 }
Tony Lindgren5e863c52013-12-06 14:20:16 -08002245
2246 *found = NULL;
2247 *index = 0;
2248
2249 return -ENODEV;
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302250}
2251
2252/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002253 * _init_mpu_rt_base - populate the virtual address for a hwmod
2254 * @oh: struct omap_hwmod * to locate the virtual address
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002255 * @data: (unused, caller should pass NULL)
Tony Lindgren5e863c52013-12-06 14:20:16 -08002256 * @index: index of the reg entry iospace in device tree
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002257 * @np: struct device_node * of the IP block's device node in the DT data
Paul Walmsley381d0332012-04-19 00:58:22 -06002258 *
2259 * Cache the virtual address used by the MPU to access this IP block's
2260 * registers. This address is needed early so the OCP registers that
2261 * are part of the device's address space can be ioremapped properly.
Suman Anna6423d6d2013-10-08 23:46:49 -06002262 *
Roger Quadros9a258af2015-07-16 16:16:44 +03002263 * If SYSC access is not needed, the registers will not be remapped
2264 * and non-availability of MPU access is not treated as an error.
2265 *
Suman Anna6423d6d2013-10-08 23:46:49 -06002266 * Returns 0 on success, -EINVAL if an invalid hwmod is passed, and
2267 * -ENXIO on absent or invalid register target address space.
Paul Walmsley381d0332012-04-19 00:58:22 -06002268 */
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002269static int __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data,
Tony Lindgren5e863c52013-12-06 14:20:16 -08002270 int index, struct device_node *np)
Paul Walmsley381d0332012-04-19 00:58:22 -06002271{
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002272 struct omap_hwmod_addr_space *mem;
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302273 void __iomem *va_start = NULL;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002274
2275 if (!oh)
Suman Anna6423d6d2013-10-08 23:46:49 -06002276 return -EINVAL;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002277
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002278 _save_mpu_port_index(oh);
2279
Roger Quadros9a258af2015-07-16 16:16:44 +03002280 /* if we don't need sysc access we don't need to ioremap */
2281 if (!oh->class->sysc)
2282 return 0;
2283
2284 /* we can't continue without MPU PORT if we need sysc access */
Paul Walmsley381d0332012-04-19 00:58:22 -06002285 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
Suman Anna6423d6d2013-10-08 23:46:49 -06002286 return -ENXIO;
Paul Walmsley381d0332012-04-19 00:58:22 -06002287
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002288 mem = _find_mpu_rt_addr_space(oh);
2289 if (!mem) {
2290 pr_debug("omap_hwmod: %s: no MPU register target found\n",
2291 oh->name);
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302292
2293 /* Extract the IO space from device tree blob */
Roger Quadros9a258af2015-07-16 16:16:44 +03002294 if (!np) {
2295 pr_err("omap_hwmod: %s: no dt node\n", oh->name);
Suman Anna6423d6d2013-10-08 23:46:49 -06002296 return -ENXIO;
Roger Quadros9a258af2015-07-16 16:16:44 +03002297 }
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302298
Tony Lindgren5e863c52013-12-06 14:20:16 -08002299 va_start = of_iomap(np, index + oh->mpu_rt_idx);
Santosh Shilimkar079abad2013-01-21 18:40:57 +05302300 } else {
2301 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002302 }
2303
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002304 if (!va_start) {
Tony Lindgren5e863c52013-12-06 14:20:16 -08002305 if (mem)
2306 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
2307 else
2308 pr_err("omap_hwmod: %s: Missing dt reg%i for %s\n",
2309 oh->name, index, np->full_name);
Suman Anna6423d6d2013-10-08 23:46:49 -06002310 return -ENXIO;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002311 }
2312
2313 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
2314 oh->name, va_start);
2315
2316 oh->_mpu_rt_va = va_start;
Suman Anna6423d6d2013-10-08 23:46:49 -06002317 return 0;
Paul Walmsley381d0332012-04-19 00:58:22 -06002318}
2319
2320/**
2321 * _init - initialize internal data for the hwmod @oh
2322 * @oh: struct omap_hwmod *
2323 * @n: (unused)
2324 *
2325 * Look up the clocks and the address space used by the MPU to access
2326 * registers belonging to the hwmod @oh. @oh must already be
2327 * registered at this point. This is the first of two phases for
2328 * hwmod initialization. Code called here does not touch any hardware
2329 * registers, it simply prepares internal data structures. Returns 0
Suman Anna6423d6d2013-10-08 23:46:49 -06002330 * upon success or if the hwmod isn't registered or if the hwmod's
2331 * address space is not defined, or -EINVAL upon failure.
Paul Walmsley381d0332012-04-19 00:58:22 -06002332 */
2333static int __init _init(struct omap_hwmod *oh, void *data)
2334{
Tony Lindgren5e863c52013-12-06 14:20:16 -08002335 int r, index;
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002336 struct device_node *np = NULL;
Paul Walmsley381d0332012-04-19 00:58:22 -06002337
2338 if (oh->_state != _HWMOD_STATE_REGISTERED)
2339 return 0;
2340
Tony Lindgren5e863c52013-12-06 14:20:16 -08002341 if (of_have_populated_dt()) {
2342 struct device_node *bus;
2343
2344 bus = of_find_node_by_name(NULL, "ocp");
2345 if (!bus)
2346 return -ENODEV;
2347
2348 r = of_dev_hwmod_lookup(bus, oh, &index, &np);
2349 if (r)
2350 pr_debug("omap_hwmod: %s missing dt data\n", oh->name);
2351 else if (np && index)
2352 pr_warn("omap_hwmod: %s using broken dt data from %s\n",
2353 oh->name, np->name);
2354 }
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002355
Roger Quadros9a258af2015-07-16 16:16:44 +03002356 r = _init_mpu_rt_base(oh, NULL, index, np);
2357 if (r < 0) {
2358 WARN(1, "omap_hwmod: %s: doesn't have mpu register target base\n",
2359 oh->name);
2360 return 0;
Suman Anna6423d6d2013-10-08 23:46:49 -06002361 }
Paul Walmsley381d0332012-04-19 00:58:22 -06002362
2363 r = _init_clocks(oh, NULL);
Russell Kingc48cd652013-03-13 20:44:21 +00002364 if (r < 0) {
Paul Walmsley381d0332012-04-19 00:58:22 -06002365 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2366 return -EINVAL;
2367 }
2368
Suman Anna3d36ad72014-03-14 14:45:17 +05302369 if (np) {
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002370 if (of_find_property(np, "ti,no-reset-on-init", NULL))
2371 oh->flags |= HWMOD_INIT_NO_RESET;
2372 if (of_find_property(np, "ti,no-idle-on-init", NULL))
2373 oh->flags |= HWMOD_INIT_NO_IDLE;
Lokesh Vutla2e18f5a2016-03-07 01:41:21 -07002374 if (of_find_property(np, "ti,no-idle", NULL))
2375 oh->flags |= HWMOD_NO_IDLE;
Suman Anna3d36ad72014-03-14 14:45:17 +05302376 }
Rajendra Nayakf92d9592013-10-09 01:26:55 -06002377
Paul Walmsley381d0332012-04-19 00:58:22 -06002378 oh->_state = _HWMOD_STATE_INITIALIZED;
2379
2380 return 0;
2381}
2382
2383/**
Paul Walmsley64813c32012-04-18 19:10:03 -06002384 * _setup_iclk_autoidle - configure an IP block's interface clocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002385 * @oh: struct omap_hwmod *
2386 *
Paul Walmsley64813c32012-04-18 19:10:03 -06002387 * Set up the module's interface clocks. XXX This function is still mostly
2388 * a stub; implementing this properly requires iclk autoidle usecounting in
2389 * the clock code. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03002390 */
Paul Walmsley64813c32012-04-18 19:10:03 -06002391static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002392{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002393 struct omap_hwmod_ocp_if *os;
Tony Lindgrenb8e1bdd2017-03-14 13:13:19 -07002394
Paul Walmsley381d0332012-04-19 00:58:22 -06002395 if (oh->_state != _HWMOD_STATE_INITIALIZED)
Paul Walmsley64813c32012-04-18 19:10:03 -06002396 return;
Paul Walmsley48d54f32011-02-23 00:14:07 -07002397
Tony Lindgrenb8e1bdd2017-03-14 13:13:19 -07002398 list_for_each_entry(os, &oh->slave_ports, node) {
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002399 if (!os->_clk)
Paul Walmsley64813c32012-04-18 19:10:03 -06002400 continue;
Paul Walmsley63c85232009-09-03 20:14:03 +03002401
Paul Walmsley64813c32012-04-18 19:10:03 -06002402 if (os->flags & OCPIF_SWSUP_IDLE) {
2403 /* XXX omap_iclk_deny_idle(c); */
2404 } else {
2405 /* XXX omap_iclk_allow_idle(c); */
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002406 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +03002407 }
2408 }
2409
Paul Walmsley64813c32012-04-18 19:10:03 -06002410 return;
2411}
2412
2413/**
2414 * _setup_reset - reset an IP block during the setup process
2415 * @oh: struct omap_hwmod *
2416 *
2417 * Reset the IP block corresponding to the hwmod @oh during the setup
2418 * process. The IP block is first enabled so it can be successfully
2419 * reset. Returns 0 upon success or a negative error code upon
2420 * failure.
2421 */
2422static int __init _setup_reset(struct omap_hwmod *oh)
2423{
2424 int r;
2425
2426 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2427 return -EINVAL;
Paul Walmsley63c85232009-09-03 20:14:03 +03002428
Paul Walmsley5fb3d522012-10-29 22:11:50 -06002429 if (oh->flags & HWMOD_EXT_OPT_MAIN_CLK)
2430 return -EPERM;
2431
Paul Walmsley747834a2012-04-18 19:10:04 -06002432 if (oh->rst_lines_cnt == 0) {
2433 r = _enable(oh);
2434 if (r) {
Joe Perches3d0cb732014-09-13 11:31:16 -07002435 pr_warn("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2436 oh->name, oh->_state);
Paul Walmsley747834a2012-04-18 19:10:04 -06002437 return -EINVAL;
2438 }
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002439 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002440
Rajendra Nayak28008522012-03-13 22:55:23 +05302441 if (!(oh->flags & HWMOD_INIT_NO_RESET))
Paul Walmsley64813c32012-04-18 19:10:03 -06002442 r = _reset(oh);
2443
2444 return r;
2445}
2446
2447/**
2448 * _setup_postsetup - transition to the appropriate state after _setup
2449 * @oh: struct omap_hwmod *
2450 *
2451 * Place an IP block represented by @oh into a "post-setup" state --
2452 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2453 * this function is called at the end of _setup().) The postsetup
2454 * state for an IP block can be changed by calling
2455 * omap_hwmod_enter_postsetup_state() early in the boot process,
2456 * before one of the omap_hwmod_setup*() functions are called for the
2457 * IP block.
2458 *
2459 * The IP block stays in this state until a PM runtime-based driver is
2460 * loaded for that IP block. A post-setup state of IDLE is
2461 * appropriate for almost all IP blocks with runtime PM-enabled
2462 * drivers, since those drivers are able to enable the IP block. A
2463 * post-setup state of ENABLED is appropriate for kernels with PM
2464 * runtime disabled. The DISABLED state is appropriate for unusual IP
2465 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2466 * included, since the WDTIMER starts running on reset and will reset
2467 * the MPU if left active.
2468 *
2469 * This post-setup mechanism is deprecated. Once all of the OMAP
2470 * drivers have been converted to use PM runtime, and all of the IP
2471 * block data and interconnect data is available to the hwmod code, it
2472 * should be possible to replace this mechanism with a "lazy reset"
2473 * arrangement. In a "lazy reset" setup, each IP block is enabled
2474 * when the driver first probes, then all remaining IP blocks without
2475 * drivers are either shut down or enabled after the drivers have
2476 * loaded. However, this cannot take place until the above
2477 * preconditions have been met, since otherwise the late reset code
2478 * has no way of knowing which IP blocks are in use by drivers, and
2479 * which ones are unused.
2480 *
2481 * No return value.
2482 */
2483static void __init _setup_postsetup(struct omap_hwmod *oh)
2484{
2485 u8 postsetup_state;
2486
2487 if (oh->rst_lines_cnt > 0)
2488 return;
Benoit Cousson76e55892010-09-21 10:34:11 -06002489
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002490 postsetup_state = oh->_postsetup_state;
2491 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2492 postsetup_state = _HWMOD_STATE_ENABLED;
2493
2494 /*
2495 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2496 * it should be set by the core code as a runtime flag during startup
2497 */
Lokesh Vutla2e18f5a2016-03-07 01:41:21 -07002498 if ((oh->flags & (HWMOD_INIT_NO_IDLE | HWMOD_NO_IDLE)) &&
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002499 (postsetup_state == _HWMOD_STATE_IDLE)) {
2500 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002501 postsetup_state = _HWMOD_STATE_ENABLED;
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002502 }
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002503
2504 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002505 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002506 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2507 _shutdown(oh);
2508 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2509 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2510 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03002511
Paul Walmsley64813c32012-04-18 19:10:03 -06002512 return;
2513}
2514
2515/**
2516 * _setup - prepare IP block hardware for use
2517 * @oh: struct omap_hwmod *
2518 * @n: (unused, pass NULL)
2519 *
2520 * Configure the IP block represented by @oh. This may include
2521 * enabling the IP block, resetting it, and placing it into a
2522 * post-setup state, depending on the type of IP block and applicable
2523 * flags. IP blocks are reset to prevent any previous configuration
2524 * by the bootloader or previous operating system from interfering
2525 * with power management or other parts of the system. The reset can
2526 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2527 * two phases for hwmod initialization. Code called here generally
2528 * affects the IP block hardware, or system integration hardware
2529 * associated with the IP block. Returns 0.
2530 */
2531static int __init _setup(struct omap_hwmod *oh, void *data)
2532{
2533 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2534 return 0;
2535
Tomi Valkeinenf22d2542014-10-09 17:03:14 +03002536 if (oh->parent_hwmod) {
2537 int r;
2538
2539 r = _enable(oh->parent_hwmod);
2540 WARN(r, "hwmod: %s: setup: failed to enable parent hwmod %s\n",
2541 oh->name, oh->parent_hwmod->name);
2542 }
2543
Paul Walmsley64813c32012-04-18 19:10:03 -06002544 _setup_iclk_autoidle(oh);
2545
2546 if (!_setup_reset(oh))
2547 _setup_postsetup(oh);
2548
Tomi Valkeinenf22d2542014-10-09 17:03:14 +03002549 if (oh->parent_hwmod) {
2550 u8 postsetup_state;
2551
2552 postsetup_state = oh->parent_hwmod->_postsetup_state;
2553
2554 if (postsetup_state == _HWMOD_STATE_IDLE)
2555 _idle(oh->parent_hwmod);
2556 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2557 _shutdown(oh->parent_hwmod);
2558 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2559 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2560 oh->parent_hwmod->name, postsetup_state);
2561 }
2562
Paul Walmsley63c85232009-09-03 20:14:03 +03002563 return 0;
2564}
2565
Benoit Cousson0102b622010-12-21 21:31:27 -07002566/**
2567 * _register - register a struct omap_hwmod
2568 * @oh: struct omap_hwmod *
2569 *
2570 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2571 * already has been registered by the same name; -EINVAL if the
2572 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2573 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2574 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2575 * success.
2576 *
2577 * XXX The data should be copied into bootmem, so the original data
2578 * should be marked __initdata and freed after init. This would allow
2579 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2580 * that the copy process would be relatively complex due to the large number
2581 * of substructures.
2582 */
Benoit Cousson01592df2010-12-21 21:31:28 -07002583static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07002584{
Benoit Cousson0102b622010-12-21 21:31:27 -07002585 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2586 (oh->_state != _HWMOD_STATE_UNKNOWN))
2587 return -EINVAL;
2588
Benoit Cousson0102b622010-12-21 21:31:27 -07002589 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2590
Benoit Coussonce35b242010-12-21 21:31:28 -07002591 if (_lookup(oh->name))
2592 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07002593
Benoit Cousson0102b622010-12-21 21:31:27 -07002594 list_add_tail(&oh->node, &omap_hwmod_list);
2595
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002596 INIT_LIST_HEAD(&oh->slave_ports);
Benoit Cousson0102b622010-12-21 21:31:27 -07002597 spin_lock_init(&oh->_lock);
Peter Ujfalusi69317952015-02-26 00:00:51 -07002598 lockdep_set_class(&oh->_lock, &oh->hwmod_key);
Benoit Cousson0102b622010-12-21 21:31:27 -07002599
2600 oh->_state = _HWMOD_STATE_REGISTERED;
2601
Paul Walmsley569edd702011-02-23 00:14:06 -07002602 /*
2603 * XXX Rather than doing a strcmp(), this should test a flag
2604 * set in the hwmod data, inserted by the autogenerator code.
2605 */
2606 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2607 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07002608
Paul Walmsley569edd702011-02-23 00:14:06 -07002609 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07002610}
Paul Walmsley63c85232009-09-03 20:14:03 +03002611
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002612/**
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002613 * _add_link - add an interconnect between two IP blocks
2614 * @oi: pointer to a struct omap_hwmod_ocp_if record
2615 *
Tony Lindgrena1e31232017-03-14 13:13:19 -07002616 * Add struct omap_hwmod_link records connecting the slave IP block
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002617 * specified in @oi->slave to @oi. This code is assumed to run before
2618 * preemption or SMP has been enabled, thus avoiding the need for
2619 * locking in this code. Changes to this assumption will require
2620 * additional locking. Returns 0.
2621 */
2622static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2623{
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002624 pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2625 oi->slave->name);
2626
Tony Lindgrena1e31232017-03-14 13:13:19 -07002627 list_add(&oi->node, &oi->slave->slave_ports);
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002628 oi->slave->slaves_cnt++;
2629
2630 return 0;
2631}
2632
2633/**
2634 * _register_link - register a struct omap_hwmod_ocp_if
2635 * @oi: struct omap_hwmod_ocp_if *
2636 *
2637 * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
2638 * has already been registered; -EINVAL if @oi is NULL or if the
2639 * record pointed to by @oi is missing required fields; or 0 upon
2640 * success.
2641 *
2642 * XXX The data should be copied into bootmem, so the original data
2643 * should be marked __initdata and freed after init. This would allow
2644 * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2645 */
2646static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2647{
2648 if (!oi || !oi->master || !oi->slave || !oi->user)
2649 return -EINVAL;
2650
2651 if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2652 return -EEXIST;
2653
2654 pr_debug("omap_hwmod: registering link from %s to %s\n",
2655 oi->master->name, oi->slave->name);
2656
2657 /*
2658 * Register the connected hwmods, if they haven't been
2659 * registered already
2660 */
2661 if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2662 _register(oi->master);
2663
2664 if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2665 _register(oi->slave);
2666
2667 _add_link(oi);
2668
2669 oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2670
2671 return 0;
2672}
2673
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002674/* Static functions intended only for use in soc_ops field function pointers */
2675
2676/**
Tero Kristo9002e9212014-10-27 08:39:23 -07002677 * _omap2xxx_3xxx_wait_target_ready - wait for a module to leave slave idle
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002678 * @oh: struct omap_hwmod *
2679 *
2680 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2681 * does not have an IDLEST bit or if the module successfully leaves
2682 * slave idle; otherwise, pass along the return value of the
2683 * appropriate *_cm*_wait_module_ready() function.
2684 */
Tero Kristo9002e9212014-10-27 08:39:23 -07002685static int _omap2xxx_3xxx_wait_target_ready(struct omap_hwmod *oh)
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002686{
2687 if (!oh)
2688 return -EINVAL;
2689
2690 if (oh->flags & HWMOD_NO_IDLEST)
2691 return 0;
2692
2693 if (!_find_mpu_rt_port(oh))
2694 return 0;
2695
2696 /* XXX check module SIDLEMODE, hardreset status, enabled clocks */
2697
Tero Kristo021b6ff2014-10-27 08:39:23 -07002698 return omap_cm_wait_module_ready(0, oh->prcm.omap2.module_offs,
2699 oh->prcm.omap2.idlest_reg_id,
2700 oh->prcm.omap2.idlest_idle_bit);
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002701}
2702
2703/**
2704 * _omap4_wait_target_ready - wait for a module to leave slave idle
2705 * @oh: struct omap_hwmod *
2706 *
2707 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2708 * does not have an IDLEST bit or if the module successfully leaves
2709 * slave idle; otherwise, pass along the return value of the
2710 * appropriate *_cm*_wait_module_ready() function.
2711 */
2712static int _omap4_wait_target_ready(struct omap_hwmod *oh)
2713{
Paul Walmsley2b026d12012-09-23 17:28:18 -06002714 if (!oh)
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002715 return -EINVAL;
2716
Paul Walmsley2b026d12012-09-23 17:28:18 -06002717 if (oh->flags & HWMOD_NO_IDLEST || !oh->clkdm)
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002718 return 0;
2719
2720 if (!_find_mpu_rt_port(oh))
2721 return 0;
2722
Dave Gerlach428929c2016-07-12 12:50:33 -05002723 if (!oh->prcm.omap4.clkctrl_offs &&
2724 !(oh->prcm.omap4.flags & HWMOD_OMAP4_ZERO_CLKCTRL_OFFSET))
2725 return 0;
2726
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002727 /* XXX check module SIDLEMODE, hardreset status */
2728
Tero Kristo021b6ff2014-10-27 08:39:23 -07002729 return omap_cm_wait_module_ready(oh->clkdm->prcm_partition,
2730 oh->clkdm->cm_inst,
2731 oh->prcm.omap4.clkctrl_offs, 0);
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06002732}
2733
2734/**
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002735 * _omap2_assert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2736 * @oh: struct omap_hwmod * to assert hardreset
2737 * @ohri: hardreset line data
2738 *
2739 * Call omap2_prm_assert_hardreset() with parameters extracted from
2740 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2741 * use as an soc_ops function pointer. Passes along the return value
2742 * from omap2_prm_assert_hardreset(). XXX This function is scheduled
2743 * for removal when the PRM code is moved into drivers/.
2744 */
2745static int _omap2_assert_hardreset(struct omap_hwmod *oh,
2746 struct omap_hwmod_rst_info *ohri)
2747{
Tero Kristoefd44dc2014-10-27 08:39:24 -07002748 return omap_prm_assert_hardreset(ohri->rst_shift, 0,
2749 oh->prcm.omap2.module_offs, 0);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002750}
2751
2752/**
2753 * _omap2_deassert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2754 * @oh: struct omap_hwmod * to deassert hardreset
2755 * @ohri: hardreset line data
2756 *
2757 * Call omap2_prm_deassert_hardreset() with parameters extracted from
2758 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2759 * use as an soc_ops function pointer. Passes along the return value
2760 * from omap2_prm_deassert_hardreset(). XXX This function is
2761 * scheduled for removal when the PRM code is moved into drivers/.
2762 */
2763static int _omap2_deassert_hardreset(struct omap_hwmod *oh,
2764 struct omap_hwmod_rst_info *ohri)
2765{
Tero Kristo37fb59d2014-10-27 08:39:25 -07002766 return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->st_shift, 0,
2767 oh->prcm.omap2.module_offs, 0, 0);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002768}
2769
2770/**
2771 * _omap2_is_hardreset_asserted - call OMAP2 PRM hardreset fn with hwmod args
2772 * @oh: struct omap_hwmod * to test hardreset
2773 * @ohri: hardreset line data
2774 *
2775 * Call omap2_prm_is_hardreset_asserted() with parameters extracted
2776 * from the hwmod @oh and the hardreset line data @ohri. Only
2777 * intended for use as an soc_ops function pointer. Passes along the
2778 * return value from omap2_prm_is_hardreset_asserted(). XXX This
2779 * function is scheduled for removal when the PRM code is moved into
2780 * drivers/.
2781 */
2782static int _omap2_is_hardreset_asserted(struct omap_hwmod *oh,
2783 struct omap_hwmod_rst_info *ohri)
2784{
Tero Kristo1bc28b32014-10-27 08:39:25 -07002785 return omap_prm_is_hardreset_asserted(ohri->st_shift, 0,
2786 oh->prcm.omap2.module_offs, 0);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002787}
2788
2789/**
2790 * _omap4_assert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2791 * @oh: struct omap_hwmod * to assert hardreset
2792 * @ohri: hardreset line data
2793 *
2794 * Call omap4_prminst_assert_hardreset() with parameters extracted
2795 * from the hwmod @oh and the hardreset line data @ohri. Only
2796 * intended for use as an soc_ops function pointer. Passes along the
2797 * return value from omap4_prminst_assert_hardreset(). XXX This
2798 * function is scheduled for removal when the PRM code is moved into
2799 * drivers/.
2800 */
2801static int _omap4_assert_hardreset(struct omap_hwmod *oh,
2802 struct omap_hwmod_rst_info *ohri)
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002803{
Paul Walmsley07b3a132012-06-20 20:11:36 -06002804 if (!oh->clkdm)
2805 return -EINVAL;
2806
Tero Kristoefd44dc2014-10-27 08:39:24 -07002807 return omap_prm_assert_hardreset(ohri->rst_shift,
2808 oh->clkdm->pwrdm.ptr->prcm_partition,
2809 oh->clkdm->pwrdm.ptr->prcm_offs,
2810 oh->prcm.omap4.rstctrl_offs);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002811}
2812
2813/**
2814 * _omap4_deassert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2815 * @oh: struct omap_hwmod * to deassert hardreset
2816 * @ohri: hardreset line data
2817 *
2818 * Call omap4_prminst_deassert_hardreset() with parameters extracted
2819 * from the hwmod @oh and the hardreset line data @ohri. Only
2820 * intended for use as an soc_ops function pointer. Passes along the
2821 * return value from omap4_prminst_deassert_hardreset(). XXX This
2822 * function is scheduled for removal when the PRM code is moved into
2823 * drivers/.
2824 */
2825static int _omap4_deassert_hardreset(struct omap_hwmod *oh,
2826 struct omap_hwmod_rst_info *ohri)
2827{
Paul Walmsley07b3a132012-06-20 20:11:36 -06002828 if (!oh->clkdm)
2829 return -EINVAL;
2830
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002831 if (ohri->st_shift)
2832 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
2833 oh->name, ohri->name);
Tero Kristo4ebf5b22015-05-05 16:33:04 +03002834 return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->rst_shift,
Tero Kristo37fb59d2014-10-27 08:39:25 -07002835 oh->clkdm->pwrdm.ptr->prcm_partition,
2836 oh->clkdm->pwrdm.ptr->prcm_offs,
Tero Kristo4ebf5b22015-05-05 16:33:04 +03002837 oh->prcm.omap4.rstctrl_offs,
2838 oh->prcm.omap4.rstctrl_offs +
2839 OMAP4_RST_CTRL_ST_OFFSET);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002840}
2841
2842/**
2843 * _omap4_is_hardreset_asserted - call OMAP4 PRM hardreset fn with hwmod args
2844 * @oh: struct omap_hwmod * to test hardreset
2845 * @ohri: hardreset line data
2846 *
2847 * Call omap4_prminst_is_hardreset_asserted() with parameters
2848 * extracted from the hwmod @oh and the hardreset line data @ohri.
2849 * Only intended for use as an soc_ops function pointer. Passes along
2850 * the return value from omap4_prminst_is_hardreset_asserted(). XXX
2851 * This function is scheduled for removal when the PRM code is moved
2852 * into drivers/.
2853 */
2854static int _omap4_is_hardreset_asserted(struct omap_hwmod *oh,
2855 struct omap_hwmod_rst_info *ohri)
2856{
Paul Walmsley07b3a132012-06-20 20:11:36 -06002857 if (!oh->clkdm)
2858 return -EINVAL;
2859
Tero Kristo1bc28b32014-10-27 08:39:25 -07002860 return omap_prm_is_hardreset_asserted(ohri->rst_shift,
2861 oh->clkdm->pwrdm.ptr->
2862 prcm_partition,
2863 oh->clkdm->pwrdm.ptr->prcm_offs,
2864 oh->prcm.omap4.rstctrl_offs);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002865}
2866
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06002867/**
Tero Kristo9fabc1a2016-07-04 14:11:48 +03002868 * _omap4_disable_direct_prcm - disable direct PRCM control for hwmod
2869 * @oh: struct omap_hwmod * to disable control for
2870 *
2871 * Disables direct PRCM clkctrl done by hwmod core. Instead, the hwmod
2872 * will be using its main_clk to enable/disable the module. Returns
2873 * 0 if successful.
2874 */
2875static int _omap4_disable_direct_prcm(struct omap_hwmod *oh)
2876{
2877 if (!oh)
2878 return -EINVAL;
2879
2880 oh->prcm.omap4.clkctrl_offs = 0;
2881 oh->prcm.omap4.modulemode = 0;
2882
2883 return 0;
2884}
2885
2886/**
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06002887 * _am33xx_deassert_hardreset - call AM33XX PRM hardreset fn with hwmod args
2888 * @oh: struct omap_hwmod * to deassert hardreset
2889 * @ohri: hardreset line data
2890 *
2891 * Call am33xx_prminst_deassert_hardreset() with parameters extracted
2892 * from the hwmod @oh and the hardreset line data @ohri. Only
2893 * intended for use as an soc_ops function pointer. Passes along the
2894 * return value from am33xx_prminst_deassert_hardreset(). XXX This
2895 * function is scheduled for removal when the PRM code is moved into
2896 * drivers/.
2897 */
2898static int _am33xx_deassert_hardreset(struct omap_hwmod *oh,
2899 struct omap_hwmod_rst_info *ohri)
2900{
Tero Kristoa5bf00c2015-05-05 16:33:05 +03002901 return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->st_shift,
2902 oh->clkdm->pwrdm.ptr->prcm_partition,
Tero Kristo37fb59d2014-10-27 08:39:25 -07002903 oh->clkdm->pwrdm.ptr->prcm_offs,
2904 oh->prcm.omap4.rstctrl_offs,
2905 oh->prcm.omap4.rstst_offs);
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06002906}
2907
Paul Walmsley63c85232009-09-03 20:14:03 +03002908/* Public functions */
2909
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002910u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002911{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002912 if (oh->flags & HWMOD_16BIT_REG)
Victor Kamenskyedfaf052014-04-15 20:37:46 +03002913 return readw_relaxed(oh->_mpu_rt_va + reg_offs);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002914 else
Victor Kamenskyedfaf052014-04-15 20:37:46 +03002915 return readl_relaxed(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002916}
2917
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002918void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002919{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002920 if (oh->flags & HWMOD_16BIT_REG)
Victor Kamenskyedfaf052014-04-15 20:37:46 +03002921 writew_relaxed(v, oh->_mpu_rt_va + reg_offs);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002922 else
Victor Kamenskyedfaf052014-04-15 20:37:46 +03002923 writel_relaxed(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002924}
2925
Paul Walmsley887adea2010-07-26 16:34:33 -06002926/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002927 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
2928 * @oh: struct omap_hwmod *
2929 *
2930 * This is a public function exposed to drivers. Some drivers may need to do
2931 * some settings before and after resetting the device. Those drivers after
2932 * doing the necessary settings could use this function to start a reset by
2933 * setting the SYSCONFIG.SOFTRESET bit.
2934 */
2935int omap_hwmod_softreset(struct omap_hwmod *oh)
2936{
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002937 u32 v;
2938 int ret;
2939
2940 if (!oh || !(oh->_sysc_cache))
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002941 return -EINVAL;
2942
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002943 v = oh->_sysc_cache;
2944 ret = _set_softreset(oh, &v);
2945 if (ret)
2946 goto error;
2947 _write_sysconfig(v, oh);
2948
Roger Quadros313a76e2013-12-08 18:39:02 -07002949 ret = _clear_softreset(oh, &v);
2950 if (ret)
2951 goto error;
2952 _write_sysconfig(v, oh);
2953
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002954error:
2955 return ret;
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002956}
2957
2958/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002959 * omap_hwmod_lookup - look up a registered omap_hwmod by name
2960 * @name: name of the omap_hwmod to look up
2961 *
2962 * Given a @name of an omap_hwmod, return a pointer to the registered
2963 * struct omap_hwmod *, or NULL upon error.
2964 */
2965struct omap_hwmod *omap_hwmod_lookup(const char *name)
2966{
2967 struct omap_hwmod *oh;
2968
2969 if (!name)
2970 return NULL;
2971
Paul Walmsley63c85232009-09-03 20:14:03 +03002972 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002973
2974 return oh;
2975}
2976
2977/**
2978 * omap_hwmod_for_each - call function for each registered omap_hwmod
2979 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06002980 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03002981 *
2982 * Call @fn for each registered omap_hwmod, passing @data to each
2983 * function. @fn must return 0 for success or any other value for
2984 * failure. If @fn returns non-zero, the iteration across omap_hwmods
2985 * will stop and the non-zero return value will be passed to the
2986 * caller of omap_hwmod_for_each(). @fn is called with
2987 * omap_hwmod_for_each() held.
2988 */
Paul Walmsley97d60162010-07-26 16:34:30 -06002989int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
2990 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03002991{
2992 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05302993 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002994
2995 if (!fn)
2996 return -EINVAL;
2997
Paul Walmsley63c85232009-09-03 20:14:03 +03002998 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06002999 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03003000 if (ret)
3001 break;
3002 }
Paul Walmsley63c85232009-09-03 20:14:03 +03003003
3004 return ret;
3005}
3006
Paul Walmsley63c85232009-09-03 20:14:03 +03003007/**
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003008 * omap_hwmod_register_links - register an array of hwmod links
3009 * @ois: pointer to an array of omap_hwmod_ocp_if to register
3010 *
3011 * Intended to be called early in boot before the clock framework is
3012 * initialized. If @ois is not null, will register all omap_hwmods
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003013 * listed in @ois that are valid for this chip. Returns -EINVAL if
3014 * omap_hwmod_init() hasn't been called before calling this function,
3015 * -ENOMEM if the link memory area can't be allocated, or 0 upon
3016 * success.
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003017 */
3018int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
3019{
3020 int r, i;
3021
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003022 if (!inited)
3023 return -EINVAL;
3024
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003025 if (!ois)
3026 return 0;
3027
Rajendra Nayakf7f7a292014-08-27 19:38:23 -06003028 if (ois[0] == NULL) /* Empty list */
3029 return 0;
3030
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003031 i = 0;
3032 do {
3033 r = _register_link(ois[i]);
3034 WARN(r && r != -EEXIST,
3035 "omap_hwmod: _register_link(%s -> %s) returned %d\n",
3036 ois[i]->master->name, ois[i]->slave->name, r);
3037 } while (ois[++i]);
3038
3039 return 0;
3040}
3041
3042/**
Paul Walmsley381d0332012-04-19 00:58:22 -06003043 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
3044 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08003045 *
Paul Walmsley381d0332012-04-19 00:58:22 -06003046 * If the hwmod data corresponding to the MPU subsystem IP block
3047 * hasn't been initialized and set up yet, do so now. This must be
3048 * done first since sleep dependencies may be added from other hwmods
3049 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
3050 * return value.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08003051 */
Paul Walmsley381d0332012-04-19 00:58:22 -06003052static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08003053{
Paul Walmsley381d0332012-04-19 00:58:22 -06003054 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
3055 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
3056 __func__, MPU_INITIATOR_NAME);
3057 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
3058 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
Paul Walmsley63c85232009-09-03 20:14:03 +03003059}
3060
3061/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003062 * omap_hwmod_setup_one - set up a single hwmod
3063 * @oh_name: const char * name of the already-registered hwmod to set up
3064 *
Paul Walmsley381d0332012-04-19 00:58:22 -06003065 * Initialize and set up a single hwmod. Intended to be used for a
3066 * small number of early devices, such as the timer IP blocks used for
3067 * the scheduler clock. Must be called after omap2_clk_init().
3068 * Resolves the struct clk names to struct clk pointers for each
3069 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
3070 * -EINVAL upon error or 0 upon success.
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003071 */
3072int __init omap_hwmod_setup_one(const char *oh_name)
3073{
3074 struct omap_hwmod *oh;
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003075
3076 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
3077
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003078 oh = _lookup(oh_name);
3079 if (!oh) {
3080 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
3081 return -EINVAL;
3082 }
3083
Paul Walmsley381d0332012-04-19 00:58:22 -06003084 _ensure_mpu_hwmod_is_setup(oh);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003085
Paul Walmsley381d0332012-04-19 00:58:22 -06003086 _init(oh, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07003087 _setup(oh, NULL);
3088
3089 return 0;
3090}
3091
3092/**
Lokesh Vutla8dd66662017-01-20 10:39:10 -08003093 * omap_hwmod_setup_earlycon_flags - set up flags for early console
3094 *
3095 * Enable DEBUG_OMAPUART_FLAGS for uart hwmod that is being used as
3096 * early concole so that hwmod core doesn't reset and keep it in idle
3097 * that specific uart.
3098 */
3099#ifdef CONFIG_SERIAL_EARLYCON
3100static void __init omap_hwmod_setup_earlycon_flags(void)
3101{
3102 struct device_node *np;
3103 struct omap_hwmod *oh;
3104 const char *uart;
3105
3106 np = of_find_node_by_path("/chosen");
3107 if (np) {
3108 uart = of_get_property(np, "stdout-path", NULL);
3109 if (uart) {
3110 np = of_find_node_by_path(uart);
3111 if (np) {
3112 uart = of_get_property(np, "ti,hwmods", NULL);
3113 oh = omap_hwmod_lookup(uart);
3114 if (oh)
3115 oh->flags |= DEBUG_OMAPUART_FLAGS;
3116 }
3117 }
3118 }
3119}
3120#endif
3121
3122/**
Paul Walmsley381d0332012-04-19 00:58:22 -06003123 * omap_hwmod_setup_all - set up all registered IP blocks
Paul Walmsley63c85232009-09-03 20:14:03 +03003124 *
Paul Walmsley381d0332012-04-19 00:58:22 -06003125 * Initialize and set up all IP blocks registered with the hwmod code.
3126 * Must be called after omap2_clk_init(). Resolves the struct clk
3127 * names to struct clk pointers for each registered omap_hwmod. Also
3128 * calls _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003129 */
Paul Walmsley550c8092011-02-28 11:58:14 -07003130static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03003131{
Paul Walmsley381d0332012-04-19 00:58:22 -06003132 _ensure_mpu_hwmod_is_setup(NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03003133
Paul Walmsley381d0332012-04-19 00:58:22 -06003134 omap_hwmod_for_each(_init, NULL);
Lokesh Vutla8dd66662017-01-20 10:39:10 -08003135#ifdef CONFIG_SERIAL_EARLYCON
3136 omap_hwmod_setup_earlycon_flags();
3137#endif
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003138 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03003139
3140 return 0;
3141}
Tony Lindgren8dd5ea72015-12-03 11:38:09 -08003142omap_postcore_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03003143
3144/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003145 * omap_hwmod_enable - enable an omap_hwmod
3146 * @oh: struct omap_hwmod *
3147 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06003148 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03003149 * Returns -EINVAL on error or passes along the return value from _enable().
3150 */
3151int omap_hwmod_enable(struct omap_hwmod *oh)
3152{
3153 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003154 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03003155
3156 if (!oh)
3157 return -EINVAL;
3158
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003159 spin_lock_irqsave(&oh->_lock, flags);
3160 r = _enable(oh);
3161 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003162
3163 return r;
3164}
3165
3166/**
3167 * omap_hwmod_idle - idle an omap_hwmod
3168 * @oh: struct omap_hwmod *
3169 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06003170 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03003171 * Returns -EINVAL on error or passes along the return value from _idle().
3172 */
3173int omap_hwmod_idle(struct omap_hwmod *oh)
3174{
Pali RohƔr6da23352015-02-26 14:49:51 +01003175 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003176 unsigned long flags;
3177
Paul Walmsley63c85232009-09-03 20:14:03 +03003178 if (!oh)
3179 return -EINVAL;
3180
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003181 spin_lock_irqsave(&oh->_lock, flags);
Pali RohƔr6da23352015-02-26 14:49:51 +01003182 r = _idle(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003183 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003184
Pali RohƔr6da23352015-02-26 14:49:51 +01003185 return r;
Paul Walmsley63c85232009-09-03 20:14:03 +03003186}
3187
3188/**
3189 * omap_hwmod_shutdown - shutdown an omap_hwmod
3190 * @oh: struct omap_hwmod *
3191 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06003192 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03003193 * omap_device_shutdown(). Returns -EINVAL on error or passes along
3194 * the return value from _shutdown().
3195 */
3196int omap_hwmod_shutdown(struct omap_hwmod *oh)
3197{
Pali RohƔr6da23352015-02-26 14:49:51 +01003198 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003199 unsigned long flags;
3200
Paul Walmsley63c85232009-09-03 20:14:03 +03003201 if (!oh)
3202 return -EINVAL;
3203
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003204 spin_lock_irqsave(&oh->_lock, flags);
Pali RohƔr6da23352015-02-26 14:49:51 +01003205 r = _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003206 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003207
Pali RohƔr6da23352015-02-26 14:49:51 +01003208 return r;
Paul Walmsley63c85232009-09-03 20:14:03 +03003209}
3210
Paul Walmsley5e8370f2012-04-18 19:10:06 -06003211/*
3212 * IP block data retrieval functions
3213 */
3214
Paul Walmsley63c85232009-09-03 20:14:03 +03003215/**
3216 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
3217 * @oh: struct omap_hwmod *
Peter Ujfalusidad41912012-11-21 16:15:17 -07003218 * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
Paul Walmsley63c85232009-09-03 20:14:03 +03003219 *
3220 * Count the number of struct resource array elements necessary to
3221 * contain omap_hwmod @oh resources. Intended to be called by code
3222 * that registers omap_devices. Intended to be used to determine the
3223 * size of a dynamically-allocated struct resource array, before
3224 * calling omap_hwmod_fill_resources(). Returns the number of struct
3225 * resource array elements needed.
3226 *
3227 * XXX This code is not optimized. It could attempt to merge adjacent
3228 * resource IDs.
3229 *
3230 */
Peter Ujfalusidad41912012-11-21 16:15:17 -07003231int omap_hwmod_count_resources(struct omap_hwmod *oh, unsigned long flags)
Paul Walmsley63c85232009-09-03 20:14:03 +03003232{
Peter Ujfalusidad41912012-11-21 16:15:17 -07003233 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03003234
Peter Ujfalusidad41912012-11-21 16:15:17 -07003235 if (flags & IORESOURCE_IRQ)
3236 ret += _count_mpu_irqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03003237
Peter Ujfalusidad41912012-11-21 16:15:17 -07003238 if (flags & IORESOURCE_DMA)
3239 ret += _count_sdma_reqs(oh);
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003240
Peter Ujfalusidad41912012-11-21 16:15:17 -07003241 if (flags & IORESOURCE_MEM) {
Peter Ujfalusidad41912012-11-21 16:15:17 -07003242 struct omap_hwmod_ocp_if *os;
Peter Ujfalusidad41912012-11-21 16:15:17 -07003243
Tony Lindgrenb8e1bdd2017-03-14 13:13:19 -07003244 list_for_each_entry(os, &oh->slave_ports, node)
Peter Ujfalusidad41912012-11-21 16:15:17 -07003245 ret += _count_ocp_if_addr_spaces(os);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003246 }
Paul Walmsley63c85232009-09-03 20:14:03 +03003247
3248 return ret;
3249}
3250
3251/**
3252 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
3253 * @oh: struct omap_hwmod *
3254 * @res: pointer to the first element of an array of struct resource to fill
3255 *
3256 * Fill the struct resource array @res with resource data from the
3257 * omap_hwmod @oh. Intended to be called by code that registers
3258 * omap_devices. See also omap_hwmod_count_resources(). Returns the
3259 * number of array elements filled.
3260 */
3261int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
3262{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003263 struct omap_hwmod_ocp_if *os;
3264 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03003265 int r = 0;
3266
3267 /* For each IRQ, DMA, memory area, fill in array.*/
3268
Paul Walmsley212738a2011-07-09 19:14:06 -06003269 mpu_irqs_cnt = _count_mpu_irqs(oh);
3270 for (i = 0; i < mpu_irqs_cnt; i++) {
Marc Zyngier0fb22a82015-01-17 10:21:08 +00003271 unsigned int irq;
3272
3273 if (oh->xlate_irq)
3274 irq = oh->xlate_irq((oh->mpu_irqs + i)->irq);
3275 else
3276 irq = (oh->mpu_irqs + i)->irq;
Paul Walmsley718bfd72009-12-08 16:34:16 -07003277 (res + r)->name = (oh->mpu_irqs + i)->name;
Marc Zyngier0fb22a82015-01-17 10:21:08 +00003278 (res + r)->start = irq;
3279 (res + r)->end = irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03003280 (res + r)->flags = IORESOURCE_IRQ;
3281 r++;
3282 }
3283
Paul Walmsleybc614952011-07-09 19:14:07 -06003284 sdma_reqs_cnt = _count_sdma_reqs(oh);
3285 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06003286 (res + r)->name = (oh->sdma_reqs + i)->name;
3287 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
3288 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03003289 (res + r)->flags = IORESOURCE_DMA;
3290 r++;
3291 }
3292
Tony Lindgrenb8e1bdd2017-03-14 13:13:19 -07003293 list_for_each_entry(os, &oh->slave_ports, node) {
Paul Walmsley78183f32011-07-09 19:14:05 -06003294 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03003295
Paul Walmsley78183f32011-07-09 19:14:05 -06003296 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08003297 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03003298 (res + r)->start = (os->addr + j)->pa_start;
3299 (res + r)->end = (os->addr + j)->pa_end;
3300 (res + r)->flags = IORESOURCE_MEM;
3301 r++;
3302 }
3303 }
3304
3305 return r;
3306}
3307
3308/**
Vaibhav Hiremathb82b04e2012-08-29 15:18:11 +05303309 * omap_hwmod_fill_dma_resources - fill struct resource array with dma data
3310 * @oh: struct omap_hwmod *
3311 * @res: pointer to the array of struct resource to fill
3312 *
3313 * Fill the struct resource array @res with dma resource data from the
3314 * omap_hwmod @oh. Intended to be called by code that registers
3315 * omap_devices. See also omap_hwmod_count_resources(). Returns the
3316 * number of array elements filled.
3317 */
3318int omap_hwmod_fill_dma_resources(struct omap_hwmod *oh, struct resource *res)
3319{
3320 int i, sdma_reqs_cnt;
3321 int r = 0;
3322
3323 sdma_reqs_cnt = _count_sdma_reqs(oh);
3324 for (i = 0; i < sdma_reqs_cnt; i++) {
3325 (res + r)->name = (oh->sdma_reqs + i)->name;
3326 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
3327 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
3328 (res + r)->flags = IORESOURCE_DMA;
3329 r++;
3330 }
3331
3332 return r;
3333}
3334
3335/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06003336 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
3337 * @oh: struct omap_hwmod * to operate on
3338 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
3339 * @name: pointer to the name of the data to fetch (optional)
3340 * @rsrc: pointer to a struct resource, allocated by the caller
3341 *
3342 * Retrieve MPU IRQ, SDMA request line, or address space start/end
3343 * data for the IP block pointed to by @oh. The data will be filled
3344 * into a struct resource record pointed to by @rsrc. The struct
3345 * resource must be allocated by the caller. When @name is non-null,
3346 * the data associated with the matching entry in the IRQ/SDMA/address
3347 * space hwmod data arrays will be returned. If @name is null, the
3348 * first array entry will be returned. Data order is not meaningful
3349 * in hwmod data, so callers are strongly encouraged to use a non-null
3350 * @name whenever possible to avoid unpredictable effects if hwmod
3351 * data is later added that causes data ordering to change. This
3352 * function is only intended for use by OMAP core code. Device
3353 * drivers should not call this function - the appropriate bus-related
3354 * data accessor functions should be used instead. Returns 0 upon
3355 * success or a negative error code upon error.
3356 */
3357int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
3358 const char *name, struct resource *rsrc)
3359{
3360 int r;
3361 unsigned int irq, dma;
3362 u32 pa_start, pa_end;
3363
3364 if (!oh || !rsrc)
3365 return -EINVAL;
3366
3367 if (type == IORESOURCE_IRQ) {
3368 r = _get_mpu_irq_by_name(oh, name, &irq);
3369 if (r)
3370 return r;
3371
3372 rsrc->start = irq;
3373 rsrc->end = irq;
3374 } else if (type == IORESOURCE_DMA) {
3375 r = _get_sdma_req_by_name(oh, name, &dma);
3376 if (r)
3377 return r;
3378
3379 rsrc->start = dma;
3380 rsrc->end = dma;
3381 } else if (type == IORESOURCE_MEM) {
3382 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
3383 if (r)
3384 return r;
3385
3386 rsrc->start = pa_start;
3387 rsrc->end = pa_end;
3388 } else {
3389 return -EINVAL;
3390 }
3391
3392 rsrc->flags = type;
3393 rsrc->name = name;
3394
3395 return 0;
3396}
3397
3398/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003399 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
3400 * @oh: struct omap_hwmod *
3401 *
3402 * Return the powerdomain pointer associated with the OMAP module
3403 * @oh's main clock. If @oh does not have a main clk, return the
3404 * powerdomain associated with the interface clock associated with the
3405 * module's MPU port. (XXX Perhaps this should use the SDMA port
3406 * instead?) Returns NULL on error, or a struct powerdomain * on
3407 * success.
3408 */
3409struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
3410{
3411 struct clk *c;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003412 struct omap_hwmod_ocp_if *oi;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003413 struct clockdomain *clkdm;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003414 struct clk_hw_omap *clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03003415
3416 if (!oh)
3417 return NULL;
3418
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003419 if (oh->clkdm)
3420 return oh->clkdm->pwrdm.ptr;
3421
Paul Walmsley63c85232009-09-03 20:14:03 +03003422 if (oh->_clk) {
3423 c = oh->_clk;
3424 } else {
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003425 oi = _find_mpu_rt_port(oh);
3426 if (!oi)
Paul Walmsley63c85232009-09-03 20:14:03 +03003427 return NULL;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003428 c = oi->_clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03003429 }
3430
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003431 clk = to_clk_hw_omap(__clk_get_hw(c));
3432 clkdm = clk->clkdm;
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003433 if (!clkdm)
Thara Gopinathd5647c12010-03-31 04:16:29 -06003434 return NULL;
3435
Rajendra Nayakf5dd3bb2012-11-10 16:58:41 -07003436 return clkdm->pwrdm.ptr;
Paul Walmsley63c85232009-09-03 20:14:03 +03003437}
3438
3439/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06003440 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3441 * @oh: struct omap_hwmod *
3442 *
3443 * Returns the virtual address corresponding to the beginning of the
3444 * module's register target, in the address range that is intended to
3445 * be used by the MPU. Returns the virtual address upon success or NULL
3446 * upon error.
3447 */
3448void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3449{
3450 if (!oh)
3451 return NULL;
3452
3453 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3454 return NULL;
3455
3456 if (oh->_state == _HWMOD_STATE_UNKNOWN)
3457 return NULL;
3458
3459 return oh->_mpu_rt_va;
3460}
3461
Paul Walmsley63c85232009-09-03 20:14:03 +03003462/*
3463 * XXX what about functions for drivers to save/restore ocp_sysconfig
3464 * for context save/restore operations?
3465 */
3466
3467/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003468 * omap_hwmod_enable_wakeup - allow device to wake up the system
3469 * @oh: struct omap_hwmod *
3470 *
3471 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
Govindraj.R2a1cc142012-04-05 02:59:32 -06003472 * send wakeups to the PRCM, and enable I/O ring wakeup events for
3473 * this IP block if it has dynamic mux entries. Eventually this
3474 * should set PRCM wakeup registers to cause the PRCM to receive
3475 * wakeup events from the module. Does not set any wakeup routing
3476 * registers beyond this point - if the module is to wake up any other
3477 * module or subsystem, that must be set separately. Called by
3478 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003479 */
3480int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3481{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003482 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003483 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003484
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003485 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003486
3487 if (oh->class->sysc &&
3488 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3489 v = oh->_sysc_cache;
3490 _enable_wakeup(oh, &v);
3491 _write_sysconfig(v, oh);
3492 }
3493
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003494 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003495
3496 return 0;
3497}
3498
3499/**
3500 * omap_hwmod_disable_wakeup - prevent device from waking the system
3501 * @oh: struct omap_hwmod *
3502 *
3503 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
Govindraj.R2a1cc142012-04-05 02:59:32 -06003504 * from sending wakeups to the PRCM, and disable I/O ring wakeup
3505 * events for this IP block if it has dynamic mux entries. Eventually
3506 * this should clear PRCM wakeup registers to cause the PRCM to ignore
3507 * wakeup events from the module. Does not set any wakeup routing
3508 * registers beyond this point - if the module is to wake up any other
3509 * module or subsystem, that must be set separately. Called by
3510 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003511 */
3512int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3513{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003514 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003515 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003516
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003517 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003518
3519 if (oh->class->sysc &&
3520 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3521 v = oh->_sysc_cache;
3522 _disable_wakeup(oh, &v);
3523 _write_sysconfig(v, oh);
3524 }
3525
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003526 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003527
3528 return 0;
3529}
Paul Walmsley43b40992010-02-22 22:09:34 -07003530
3531/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003532 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3533 * contained in the hwmod module.
3534 * @oh: struct omap_hwmod *
3535 * @name: name of the reset line to lookup and assert
3536 *
3537 * Some IP like dsp, ipu or iva contain processor that require
3538 * an HW reset line to be assert / deassert in order to enable fully
3539 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3540 * yet supported on this OMAP; otherwise, passes along the return value
3541 * from _assert_hardreset().
3542 */
3543int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3544{
3545 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003546 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003547
3548 if (!oh)
3549 return -EINVAL;
3550
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003551 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003552 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003553 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003554
3555 return ret;
3556}
3557
3558/**
3559 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3560 * contained in the hwmod module.
3561 * @oh: struct omap_hwmod *
3562 * @name: name of the reset line to look up and deassert
3563 *
3564 * Some IP like dsp, ipu or iva contain processor that require
3565 * an HW reset line to be assert / deassert in order to enable fully
3566 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3567 * yet supported on this OMAP; otherwise, passes along the return value
3568 * from _deassert_hardreset().
3569 */
3570int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3571{
3572 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003573 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003574
3575 if (!oh)
3576 return -EINVAL;
3577
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003578 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003579 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003580 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003581
3582 return ret;
3583}
3584
3585/**
Paul Walmsley43b40992010-02-22 22:09:34 -07003586 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3587 * @classname: struct omap_hwmod_class name to search for
3588 * @fn: callback function pointer to call for each hwmod in class @classname
3589 * @user: arbitrary context data to pass to the callback function
3590 *
Benoit Coussonce35b242010-12-21 21:31:28 -07003591 * For each omap_hwmod of class @classname, call @fn.
3592 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07003593 * zero, the iterator is terminated, and the callback function's return
3594 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3595 * if @classname or @fn are NULL, or passes back the error code from @fn.
3596 */
3597int omap_hwmod_for_each_by_class(const char *classname,
3598 int (*fn)(struct omap_hwmod *oh,
3599 void *user),
3600 void *user)
3601{
3602 struct omap_hwmod *temp_oh;
3603 int ret = 0;
3604
3605 if (!classname || !fn)
3606 return -EINVAL;
3607
3608 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3609 __func__, classname);
3610
Paul Walmsley43b40992010-02-22 22:09:34 -07003611 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3612 if (!strcmp(temp_oh->class->name, classname)) {
3613 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3614 __func__, temp_oh->name);
3615 ret = (*fn)(temp_oh, user);
3616 if (ret)
3617 break;
3618 }
3619 }
3620
Paul Walmsley43b40992010-02-22 22:09:34 -07003621 if (ret)
3622 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3623 __func__, ret);
3624
3625 return ret;
3626}
3627
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003628/**
3629 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3630 * @oh: struct omap_hwmod *
3631 * @state: state that _setup() should leave the hwmod in
3632 *
Paul Walmsley550c8092011-02-28 11:58:14 -07003633 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsley64813c32012-04-18 19:10:03 -06003634 * (called by omap_hwmod_setup_*()). See also the documentation
3635 * for _setup_postsetup(), above. Returns 0 upon success or
3636 * -EINVAL if there is a problem with the arguments or if the hwmod is
3637 * in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003638 */
3639int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3640{
3641 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003642 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003643
3644 if (!oh)
3645 return -EINVAL;
3646
3647 if (state != _HWMOD_STATE_DISABLED &&
3648 state != _HWMOD_STATE_ENABLED &&
3649 state != _HWMOD_STATE_IDLE)
3650 return -EINVAL;
3651
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003652 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003653
3654 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3655 ret = -EINVAL;
3656 goto ohsps_unlock;
3657 }
3658
3659 oh->_postsetup_state = state;
3660 ret = 0;
3661
3662ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003663 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003664
3665 return ret;
3666}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003667
3668/**
3669 * omap_hwmod_get_context_loss_count - get lost context count
3670 * @oh: struct omap_hwmod *
3671 *
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07003672 * Returns the context loss count of associated @oh
3673 * upon success, or zero if no context loss data is available.
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003674 *
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07003675 * On OMAP4, this queries the per-hwmod context loss register,
3676 * assuming one exists. If not, or on OMAP2/3, this queries the
3677 * enclosing powerdomain context loss count.
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003678 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03003679int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003680{
3681 struct powerdomain *pwrdm;
3682 int ret = 0;
3683
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07003684 if (soc_ops.get_context_lost)
3685 return soc_ops.get_context_lost(oh);
3686
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003687 pwrdm = omap_hwmod_get_pwrdm(oh);
3688 if (pwrdm)
3689 ret = pwrdm_get_context_loss_count(pwrdm);
3690
3691 return ret;
3692}
Paul Walmsley43b01642011-03-10 03:50:07 -07003693
3694/**
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003695 * omap_hwmod_init - initialize the hwmod code
3696 *
3697 * Sets up some function pointers needed by the hwmod code to operate on the
3698 * currently-booted SoC. Intended to be called once during kernel init
3699 * before any hwmods are registered. No return value.
3700 */
3701void __init omap_hwmod_init(void)
3702{
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -06003703 if (cpu_is_omap24xx()) {
Tero Kristo9002e9212014-10-27 08:39:23 -07003704 soc_ops.wait_target_ready = _omap2xxx_3xxx_wait_target_ready;
Paul Walmsleyff4ae5d2012-10-21 01:01:11 -06003705 soc_ops.assert_hardreset = _omap2_assert_hardreset;
3706 soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
3707 soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
3708 } else if (cpu_is_omap34xx()) {
Tero Kristo9002e9212014-10-27 08:39:23 -07003709 soc_ops.wait_target_ready = _omap2xxx_3xxx_wait_target_ready;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06003710 soc_ops.assert_hardreset = _omap2_assert_hardreset;
3711 soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
3712 soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
Tero Kristo0385c582013-07-17 18:03:25 +03003713 soc_ops.init_clkdm = _init_clkdm;
Rajendra Nayakdebcd1f2013-07-02 18:20:08 +05303714 } else if (cpu_is_omap44xx() || soc_is_omap54xx() || soc_is_dra7xx()) {
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003715 soc_ops.enable_module = _omap4_enable_module;
3716 soc_ops.disable_module = _omap4_disable_module;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003717 soc_ops.wait_target_ready = _omap4_wait_target_ready;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06003718 soc_ops.assert_hardreset = _omap4_assert_hardreset;
3719 soc_ops.deassert_hardreset = _omap4_deassert_hardreset;
3720 soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
Kevin Hilman0a179ea2012-06-18 12:12:25 -06003721 soc_ops.init_clkdm = _init_clkdm;
Rajendra Nayake6d3a8b2012-11-21 16:15:17 -07003722 soc_ops.update_context_lost = _omap4_update_context_lost;
3723 soc_ops.get_context_lost = _omap4_get_context_lost;
Tero Kristo9fabc1a2016-07-04 14:11:48 +03003724 soc_ops.disable_direct_prcm = _omap4_disable_direct_prcm;
Tony Lindgren0f3ccb22015-07-16 01:55:58 -07003725 } else if (cpu_is_ti814x() || cpu_is_ti816x() || soc_is_am33xx() ||
3726 soc_is_am43xx()) {
Afzal Mohammedc8b428a2013-10-12 15:46:20 +05303727 soc_ops.enable_module = _omap4_enable_module;
3728 soc_ops.disable_module = _omap4_disable_module;
3729 soc_ops.wait_target_ready = _omap4_wait_target_ready;
Tero Kristo409d7062014-10-27 08:39:24 -07003730 soc_ops.assert_hardreset = _omap4_assert_hardreset;
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06003731 soc_ops.deassert_hardreset = _am33xx_deassert_hardreset;
Tero Kristoa5bf00c2015-05-05 16:33:05 +03003732 soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
Vaibhav Hiremath1688bf12012-09-11 17:18:53 -06003733 soc_ops.init_clkdm = _init_clkdm;
Tero Kristo9fabc1a2016-07-04 14:11:48 +03003734 soc_ops.disable_direct_prcm = _omap4_disable_direct_prcm;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003735 } else {
3736 WARN(1, "omap_hwmod: unknown SoC type\n");
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003737 }
3738
3739 inited = true;
3740}
Tony Lindgren68c9a952012-07-06 00:58:43 -07003741
3742/**
3743 * omap_hwmod_get_main_clk - get pointer to main clock name
3744 * @oh: struct omap_hwmod *
3745 *
3746 * Returns the main clock name assocated with @oh upon success,
3747 * or NULL if @oh is NULL.
3748 */
3749const char *omap_hwmod_get_main_clk(struct omap_hwmod *oh)
3750{
3751 if (!oh)
3752 return NULL;
3753
3754 return oh->main_clk;
3755}