ARM: OMAP2+: PRM: add generic API for deasserting hardware reset
[linux-2.6-block.git] / arch / arm / mach-omap2 / omap_hwmod.c
CommitLineData
63c85238
PW
1/*
2 * omap_hwmod implementation for OMAP2/3/4
3 *
550c8092 4 * Copyright (C) 2009-2011 Nokia Corporation
30e105c0 5 * Copyright (C) 2011-2012 Texas Instruments, Inc.
63c85238 6 *
4788da26
PW
7 * 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
63c85238
PW
12 *
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 *
74ff3a68
PW
17 * 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.
63c85238 27 *
74ff3a68
PW
28 * 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 |
edfaf05c 75 * | ({read,write}l_relaxed, clk*) |
74ff3a68
PW
76 * +-------------------------------+
77 *
78 * Device drivers should not contain any OMAP-specific code or data in
79 * them. They should only contain code to operate the IP block that
80 * the driver is responsible for. This is because these IP blocks can
81 * also appear in other SoCs, either from TI (such as DaVinci) or from
82 * other manufacturers; and drivers should be reusable across other
83 * platforms.
84 *
85 * The OMAP hwmod code also will attempt to reset and idle all on-chip
86 * devices upon boot. The goal here is for the kernel to be
87 * completely self-reliant and independent from bootloaders. This is
88 * to ensure a repeatable configuration, both to ensure consistent
89 * runtime behavior, and to make it easier for others to reproduce
90 * bugs.
91 *
92 * OMAP module activity states
93 * ---------------------------
94 * The hwmod code considers modules to be in one of several activity
95 * states. IP blocks start out in an UNKNOWN state, then once they
96 * are registered via the hwmod code, proceed to the REGISTERED state.
97 * Once their clock names are resolved to clock pointers, the module
98 * enters the CLKS_INITED state; and finally, once the module has been
99 * reset and the integration registers programmed, the INITIALIZED state
100 * is entered. The hwmod code will then place the module into either
101 * the IDLE state to save power, or in the case of a critical system
102 * module, the ENABLED state.
103 *
104 * OMAP core integration code can then call omap_hwmod*() functions
105 * directly to move the module between the IDLE, ENABLED, and DISABLED
106 * states, as needed. This is done during both the PM idle loop, and
107 * in the OMAP core integration code's implementation of the PM runtime
108 * functions.
109 *
110 * References
111 * ----------
112 * This is a partial list.
63c85238
PW
113 * - 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:
63c85238
PW
120 * - 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>
f5dd3bb5 133#include <linux/clk-provider.h>
63c85238
PW
134#include <linux/delay.h>
135#include <linux/err.h>
136#include <linux/list.h>
137#include <linux/mutex.h>
dc6d1cda 138#include <linux/spinlock.h>
abc2d545 139#include <linux/slab.h>
2221b5cd 140#include <linux/bootmem.h>
f7b861b7 141#include <linux/cpu.h>
079abade
SS
142#include <linux/of.h>
143#include <linux/of_address.h>
63c85238 144
fa200222
PW
145#include <asm/system_misc.h>
146
a135eaae 147#include "clock.h"
2a296c8f 148#include "omap_hwmod.h"
63c85238 149
dbc04161
TL
150#include "soc.h"
151#include "common.h"
152#include "clockdomain.h"
153#include "powerdomain.h"
ff4ae5d9
PW
154#include "cm2xxx.h"
155#include "cm3xxx.h"
d0f0631d 156#include "cminst44xx.h"
1688bf19 157#include "cm33xx.h"
b13159af 158#include "prm.h"
139563ad 159#include "prm3xxx.h"
d198b514 160#include "prm44xx.h"
1688bf19 161#include "prm33xx.h"
eaac329d 162#include "prminst44xx.h"
8d9af88f 163#include "mux.h"
5165882a 164#include "pm.h"
63c85238 165
63c85238 166/* Name of the OMAP hwmod for the MPU */
5c2c0296 167#define MPU_INITIATOR_NAME "mpu"
63c85238 168
2221b5cd
PW
169/*
170 * Number of struct omap_hwmod_link records per struct
171 * omap_hwmod_ocp_if record (master->slave and slave->master)
172 */
173#define LINKS_PER_OCP_IF 2
174
9ebfd285
KH
175/**
176 * struct omap_hwmod_soc_ops - fn ptrs for some SoC-specific operations
177 * @enable_module: function to enable a module (via MODULEMODE)
178 * @disable_module: function to disable a module (via MODULEMODE)
179 *
180 * XXX Eventually this functionality will be hidden inside the PRM/CM
181 * device drivers. Until then, this should avoid huge blocks of cpu_is_*()
182 * conditionals in this code.
183 */
184struct omap_hwmod_soc_ops {
185 void (*enable_module)(struct omap_hwmod *oh);
186 int (*disable_module)(struct omap_hwmod *oh);
8f6aa8ee 187 int (*wait_target_ready)(struct omap_hwmod *oh);
b8249cf2
KH
188 int (*assert_hardreset)(struct omap_hwmod *oh,
189 struct omap_hwmod_rst_info *ohri);
190 int (*deassert_hardreset)(struct omap_hwmod *oh,
191 struct omap_hwmod_rst_info *ohri);
192 int (*is_hardreset_asserted)(struct omap_hwmod *oh,
193 struct omap_hwmod_rst_info *ohri);
0a179eaa 194 int (*init_clkdm)(struct omap_hwmod *oh);
e6d3a8b0
RN
195 void (*update_context_lost)(struct omap_hwmod *oh);
196 int (*get_context_lost)(struct omap_hwmod *oh);
9ebfd285
KH
197};
198
199/* soc_ops: adapts the omap_hwmod code to the currently-booted SoC */
200static struct omap_hwmod_soc_ops soc_ops;
201
63c85238
PW
202/* omap_hwmod_list contains all registered struct omap_hwmods */
203static LIST_HEAD(omap_hwmod_list);
204
63c85238
PW
205/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
206static struct omap_hwmod *mpu_oh;
207
5165882a
VB
208/* io_chain_lock: used to serialize reconfigurations of the I/O chain */
209static DEFINE_SPINLOCK(io_chain_lock);
210
2221b5cd
PW
211/*
212 * linkspace: ptr to a buffer that struct omap_hwmod_link records are
213 * allocated from - used to reduce the number of small memory
214 * allocations, which has a significant impact on performance
215 */
216static struct omap_hwmod_link *linkspace;
217
218/*
219 * free_ls, max_ls: array indexes into linkspace; representing the
220 * next free struct omap_hwmod_link index, and the maximum number of
221 * struct omap_hwmod_link records allocated (respectively)
222 */
223static unsigned short free_ls, max_ls, ls_supp;
63c85238 224
9ebfd285
KH
225/* inited: set to true once the hwmod code is initialized */
226static bool inited;
227
63c85238
PW
228/* Private functions */
229
5d95dde7 230/**
11cd4b94 231 * _fetch_next_ocp_if - return the next OCP interface in a list
2221b5cd 232 * @p: ptr to a ptr to the list_head inside the ocp_if to return
11cd4b94
PW
233 * @i: pointer to the index of the element pointed to by @p in the list
234 *
235 * Return a pointer to the struct omap_hwmod_ocp_if record
236 * containing the struct list_head pointed to by @p, and increment
237 * @p such that a future call to this routine will return the next
238 * record.
5d95dde7
PW
239 */
240static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
5d95dde7
PW
241 int *i)
242{
243 struct omap_hwmod_ocp_if *oi;
244
11cd4b94
PW
245 oi = list_entry(*p, struct omap_hwmod_link, node)->ocp_if;
246 *p = (*p)->next;
2221b5cd 247
5d95dde7
PW
248 *i = *i + 1;
249
250 return oi;
251}
252
63c85238
PW
253/**
254 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
255 * @oh: struct omap_hwmod *
256 *
257 * Load the current value of the hwmod OCP_SYSCONFIG register into the
258 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
259 * OCP_SYSCONFIG register or 0 upon success.
260 */
261static int _update_sysc_cache(struct omap_hwmod *oh)
262{
43b40992
PW
263 if (!oh->class->sysc) {
264 WARN(1, "omap_hwmod: %s: cannot read OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
63c85238
PW
265 return -EINVAL;
266 }
267
268 /* XXX ensure module interface clock is up */
269
cc7a1d2a 270 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
63c85238 271
43b40992 272 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
883edfdd 273 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
63c85238
PW
274
275 return 0;
276}
277
278/**
279 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
280 * @v: OCP_SYSCONFIG value to write
281 * @oh: struct omap_hwmod *
282 *
43b40992
PW
283 * Write @v into the module class' OCP_SYSCONFIG register, if it has
284 * one. No return value.
63c85238
PW
285 */
286static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
287{
43b40992
PW
288 if (!oh->class->sysc) {
289 WARN(1, "omap_hwmod: %s: cannot write OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
63c85238
PW
290 return;
291 }
292
293 /* XXX ensure module interface clock is up */
294
233cbe5b
RN
295 /* Module might have lost context, always update cache and register */
296 oh->_sysc_cache = v;
297 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
63c85238
PW
298}
299
300/**
301 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
302 * @oh: struct omap_hwmod *
303 * @standbymode: MIDLEMODE field bits
304 * @v: pointer to register contents to modify
305 *
306 * Update the master standby mode bits in @v to be @standbymode for
307 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
308 * upon error or 0 upon success.
309 */
310static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
311 u32 *v)
312{
358f0e63
TG
313 u32 mstandby_mask;
314 u8 mstandby_shift;
315
43b40992
PW
316 if (!oh->class->sysc ||
317 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
63c85238
PW
318 return -EINVAL;
319
43b40992
PW
320 if (!oh->class->sysc->sysc_fields) {
321 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
322 return -EINVAL;
323 }
324
43b40992 325 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
358f0e63
TG
326 mstandby_mask = (0x3 << mstandby_shift);
327
328 *v &= ~mstandby_mask;
329 *v |= __ffs(standbymode) << mstandby_shift;
63c85238
PW
330
331 return 0;
332}
333
334/**
335 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
336 * @oh: struct omap_hwmod *
337 * @idlemode: SIDLEMODE field bits
338 * @v: pointer to register contents to modify
339 *
340 * Update the slave idle mode bits in @v to be @idlemode for the @oh
341 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
342 * or 0 upon success.
343 */
344static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
345{
358f0e63
TG
346 u32 sidle_mask;
347 u8 sidle_shift;
348
43b40992
PW
349 if (!oh->class->sysc ||
350 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
63c85238
PW
351 return -EINVAL;
352
43b40992
PW
353 if (!oh->class->sysc->sysc_fields) {
354 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
355 return -EINVAL;
356 }
357
43b40992 358 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
358f0e63
TG
359 sidle_mask = (0x3 << sidle_shift);
360
361 *v &= ~sidle_mask;
362 *v |= __ffs(idlemode) << sidle_shift;
63c85238
PW
363
364 return 0;
365}
366
367/**
368 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
369 * @oh: struct omap_hwmod *
370 * @clockact: CLOCKACTIVITY field bits
371 * @v: pointer to register contents to modify
372 *
373 * Update the clockactivity mode bits in @v to be @clockact for the
374 * @oh hwmod. Used for additional powersaving on some modules. Does
375 * not write to the hardware. Returns -EINVAL upon error or 0 upon
376 * success.
377 */
378static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
379{
358f0e63
TG
380 u32 clkact_mask;
381 u8 clkact_shift;
382
43b40992
PW
383 if (!oh->class->sysc ||
384 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
63c85238
PW
385 return -EINVAL;
386
43b40992
PW
387 if (!oh->class->sysc->sysc_fields) {
388 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
389 return -EINVAL;
390 }
391
43b40992 392 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
358f0e63
TG
393 clkact_mask = (0x3 << clkact_shift);
394
395 *v &= ~clkact_mask;
396 *v |= clockact << clkact_shift;
63c85238
PW
397
398 return 0;
399}
400
401/**
313a76ee 402 * _set_softreset: set OCP_SYSCONFIG.SOFTRESET bit in @v
63c85238
PW
403 * @oh: struct omap_hwmod *
404 * @v: pointer to register contents to modify
405 *
406 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
407 * error or 0 upon success.
408 */
409static int _set_softreset(struct omap_hwmod *oh, u32 *v)
410{
358f0e63
TG
411 u32 softrst_mask;
412
43b40992
PW
413 if (!oh->class->sysc ||
414 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
63c85238
PW
415 return -EINVAL;
416
43b40992
PW
417 if (!oh->class->sysc->sysc_fields) {
418 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
419 return -EINVAL;
420 }
421
43b40992 422 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
358f0e63
TG
423
424 *v |= softrst_mask;
63c85238
PW
425
426 return 0;
427}
428
313a76ee
RQ
429/**
430 * _clear_softreset: clear OCP_SYSCONFIG.SOFTRESET bit in @v
431 * @oh: struct omap_hwmod *
432 * @v: pointer to register contents to modify
433 *
434 * Clear the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
435 * error or 0 upon success.
436 */
437static int _clear_softreset(struct omap_hwmod *oh, u32 *v)
438{
439 u32 softrst_mask;
440
441 if (!oh->class->sysc ||
442 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
443 return -EINVAL;
444
445 if (!oh->class->sysc->sysc_fields) {
446 WARN(1,
447 "omap_hwmod: %s: sysc_fields absent for sysconfig class\n",
448 oh->name);
449 return -EINVAL;
450 }
451
452 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
453
454 *v &= ~softrst_mask;
455
456 return 0;
457}
458
613ad0e9
TK
459/**
460 * _wait_softreset_complete - wait for an OCP softreset to complete
461 * @oh: struct omap_hwmod * to wait on
462 *
463 * Wait until the IP block represented by @oh reports that its OCP
464 * softreset is complete. This can be triggered by software (see
465 * _ocp_softreset()) or by hardware upon returning from off-mode (one
466 * example is HSMMC). Waits for up to MAX_MODULE_SOFTRESET_WAIT
467 * microseconds. Returns the number of microseconds waited.
468 */
469static int _wait_softreset_complete(struct omap_hwmod *oh)
470{
471 struct omap_hwmod_class_sysconfig *sysc;
472 u32 softrst_mask;
473 int c = 0;
474
475 sysc = oh->class->sysc;
476
477 if (sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
478 omap_test_timeout((omap_hwmod_read(oh, sysc->syss_offs)
479 & SYSS_RESETDONE_MASK),
480 MAX_MODULE_SOFTRESET_WAIT, c);
481 else if (sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
482 softrst_mask = (0x1 << sysc->sysc_fields->srst_shift);
483 omap_test_timeout(!(omap_hwmod_read(oh, sysc->sysc_offs)
484 & softrst_mask),
485 MAX_MODULE_SOFTRESET_WAIT, c);
486 }
487
488 return c;
489}
490
6668546f
KVA
491/**
492 * _set_dmadisable: set OCP_SYSCONFIG.DMADISABLE bit in @v
493 * @oh: struct omap_hwmod *
494 *
495 * The DMADISABLE bit is a semi-automatic bit present in sysconfig register
496 * of some modules. When the DMA must perform read/write accesses, the
497 * DMADISABLE bit is cleared by the hardware. But when the DMA must stop
498 * for power management, software must set the DMADISABLE bit back to 1.
499 *
500 * Set the DMADISABLE bit in @v for hwmod @oh. Returns -EINVAL upon
501 * error or 0 upon success.
502 */
503static int _set_dmadisable(struct omap_hwmod *oh)
504{
505 u32 v;
506 u32 dmadisable_mask;
507
508 if (!oh->class->sysc ||
509 !(oh->class->sysc->sysc_flags & SYSC_HAS_DMADISABLE))
510 return -EINVAL;
511
512 if (!oh->class->sysc->sysc_fields) {
513 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
514 return -EINVAL;
515 }
516
517 /* clocks must be on for this operation */
518 if (oh->_state != _HWMOD_STATE_ENABLED) {
519 pr_warn("omap_hwmod: %s: dma can be disabled only from enabled state\n", oh->name);
520 return -EINVAL;
521 }
522
523 pr_debug("omap_hwmod: %s: setting DMADISABLE\n", oh->name);
524
525 v = oh->_sysc_cache;
526 dmadisable_mask =
527 (0x1 << oh->class->sysc->sysc_fields->dmadisable_shift);
528 v |= dmadisable_mask;
529 _write_sysconfig(v, oh);
530
531 return 0;
532}
533
726072e5
PW
534/**
535 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
536 * @oh: struct omap_hwmod *
537 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
538 * @v: pointer to register contents to modify
539 *
540 * Update the module autoidle bit in @v to be @autoidle for the @oh
541 * hwmod. The autoidle bit controls whether the module can gate
542 * internal clocks automatically when it isn't doing anything; the
543 * exact function of this bit varies on a per-module basis. This
544 * function does not write to the hardware. Returns -EINVAL upon
545 * error or 0 upon success.
546 */
547static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
548 u32 *v)
549{
358f0e63
TG
550 u32 autoidle_mask;
551 u8 autoidle_shift;
552
43b40992
PW
553 if (!oh->class->sysc ||
554 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
726072e5
PW
555 return -EINVAL;
556
43b40992
PW
557 if (!oh->class->sysc->sysc_fields) {
558 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
559 return -EINVAL;
560 }
561
43b40992 562 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
8985b63d 563 autoidle_mask = (0x1 << autoidle_shift);
358f0e63
TG
564
565 *v &= ~autoidle_mask;
566 *v |= autoidle << autoidle_shift;
726072e5
PW
567
568 return 0;
569}
570
eceec009
G
571/**
572 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
573 * @oh: struct omap_hwmod *
574 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
575 *
576 * Set or clear the I/O pad wakeup flag in the mux entries for the
577 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
578 * in memory. If the hwmod is currently idled, and the new idle
579 * values don't match the previous ones, this function will also
580 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
581 * currently idled, this function won't touch the hardware: the new
582 * mux settings are written to the SCM PADCTRL registers when the
583 * hwmod is idled. No return value.
584 */
585static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
586{
587 struct omap_device_pad *pad;
588 bool change = false;
589 u16 prev_idle;
590 int j;
591
592 if (!oh->mux || !oh->mux->enabled)
593 return;
594
595 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
596 pad = oh->mux->pads_dynamic[j];
597
598 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
599 continue;
600
601 prev_idle = pad->idle;
602
603 if (set_wake)
604 pad->idle |= OMAP_WAKEUP_EN;
605 else
606 pad->idle &= ~OMAP_WAKEUP_EN;
607
608 if (prev_idle != pad->idle)
609 change = true;
610 }
611
612 if (change && oh->_state == _HWMOD_STATE_IDLE)
613 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
614}
615
63c85238
PW
616/**
617 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
618 * @oh: struct omap_hwmod *
619 *
620 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
621 * upon error or 0 upon success.
622 */
5a7ddcbd 623static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
63c85238 624{
43b40992 625 if (!oh->class->sysc ||
86009eb3 626 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
724019b0
BC
627 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
628 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
63c85238
PW
629 return -EINVAL;
630
43b40992
PW
631 if (!oh->class->sysc->sysc_fields) {
632 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
633 return -EINVAL;
634 }
635
1fe74113
BC
636 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
637 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
63c85238 638
86009eb3
BC
639 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
640 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
724019b0
BC
641 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
642 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
86009eb3 643
63c85238
PW
644 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
645
63c85238
PW
646 return 0;
647}
648
649/**
650 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
651 * @oh: struct omap_hwmod *
652 *
653 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
654 * upon error or 0 upon success.
655 */
5a7ddcbd 656static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
63c85238 657{
43b40992 658 if (!oh->class->sysc ||
86009eb3 659 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
724019b0
BC
660 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
661 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
63c85238
PW
662 return -EINVAL;
663
43b40992
PW
664 if (!oh->class->sysc->sysc_fields) {
665 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
666 return -EINVAL;
667 }
668
1fe74113
BC
669 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
670 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
63c85238 671
86009eb3
BC
672 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
673 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
724019b0 674 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
561038f0 675 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART, v);
86009eb3 676
63c85238
PW
677 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
678
63c85238
PW
679 return 0;
680}
681
f5dd3bb5
RN
682static struct clockdomain *_get_clkdm(struct omap_hwmod *oh)
683{
c4a1ea2c
RN
684 struct clk_hw_omap *clk;
685
f5dd3bb5
RN
686 if (oh->clkdm) {
687 return oh->clkdm;
688 } else if (oh->_clk) {
924f9498
TK
689 if (__clk_get_flags(oh->_clk) & CLK_IS_BASIC)
690 return NULL;
f5dd3bb5
RN
691 clk = to_clk_hw_omap(__clk_get_hw(oh->_clk));
692 return clk->clkdm;
f5dd3bb5
RN
693 }
694 return NULL;
695}
696
63c85238
PW
697/**
698 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
699 * @oh: struct omap_hwmod *
700 *
701 * Prevent the hardware module @oh from entering idle while the
702 * hardare module initiator @init_oh is active. Useful when a module
703 * will be accessed by a particular initiator (e.g., if a module will
704 * be accessed by the IVA, there should be a sleepdep between the IVA
705 * initiator and the module). Only applies to modules in smart-idle
570b54c7
PW
706 * mode. If the clockdomain is marked as not needing autodeps, return
707 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
708 * passes along clkdm_add_sleepdep() value upon success.
63c85238
PW
709 */
710static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
711{
f5dd3bb5
RN
712 struct clockdomain *clkdm, *init_clkdm;
713
714 clkdm = _get_clkdm(oh);
715 init_clkdm = _get_clkdm(init_oh);
716
717 if (!clkdm || !init_clkdm)
63c85238
PW
718 return -EINVAL;
719
f5dd3bb5 720 if (clkdm && clkdm->flags & CLKDM_NO_AUTODEPS)
570b54c7
PW
721 return 0;
722
f5dd3bb5 723 return clkdm_add_sleepdep(clkdm, init_clkdm);
63c85238
PW
724}
725
726/**
727 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
728 * @oh: struct omap_hwmod *
729 *
730 * Allow the hardware module @oh to enter idle while the hardare
731 * module initiator @init_oh is active. Useful when a module will not
732 * be accessed by a particular initiator (e.g., if a module will not
733 * be accessed by the IVA, there should be no sleepdep between the IVA
734 * initiator and the module). Only applies to modules in smart-idle
570b54c7
PW
735 * mode. If the clockdomain is marked as not needing autodeps, return
736 * 0 without doing anything. Returns -EINVAL upon error or passes
737 * along clkdm_del_sleepdep() value upon success.
63c85238
PW
738 */
739static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
740{
f5dd3bb5
RN
741 struct clockdomain *clkdm, *init_clkdm;
742
743 clkdm = _get_clkdm(oh);
744 init_clkdm = _get_clkdm(init_oh);
745
746 if (!clkdm || !init_clkdm)
63c85238
PW
747 return -EINVAL;
748
f5dd3bb5 749 if (clkdm && clkdm->flags & CLKDM_NO_AUTODEPS)
570b54c7
PW
750 return 0;
751
f5dd3bb5 752 return clkdm_del_sleepdep(clkdm, init_clkdm);
63c85238
PW
753}
754
755/**
756 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
757 * @oh: struct omap_hwmod *
758 *
759 * Called from _init_clocks(). Populates the @oh _clk (main
760 * functional clock pointer) if a main_clk is present. Returns 0 on
761 * success or -EINVAL on error.
762 */
763static int _init_main_clk(struct omap_hwmod *oh)
764{
63c85238
PW
765 int ret = 0;
766
50ebdac2 767 if (!oh->main_clk)
63c85238
PW
768 return 0;
769
6ea74cb9
RN
770 oh->_clk = clk_get(NULL, oh->main_clk);
771 if (IS_ERR(oh->_clk)) {
3d0cb73e
JP
772 pr_warn("omap_hwmod: %s: cannot clk_get main_clk %s\n",
773 oh->name, oh->main_clk);
63403384 774 return -EINVAL;
dc75925d 775 }
4d7cb45e
RN
776 /*
777 * HACK: This needs a re-visit once clk_prepare() is implemented
778 * to do something meaningful. Today its just a no-op.
779 * If clk_prepare() is used at some point to do things like
780 * voltage scaling etc, then this would have to be moved to
781 * some point where subsystems like i2c and pmic become
782 * available.
783 */
784 clk_prepare(oh->_clk);
63c85238 785
f5dd3bb5 786 if (!_get_clkdm(oh))
3bb05dbf 787 pr_debug("omap_hwmod: %s: missing clockdomain for %s.\n",
5dcc3b97 788 oh->name, oh->main_clk);
81d7c6ff 789
63c85238
PW
790 return ret;
791}
792
793/**
887adeac 794 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
63c85238
PW
795 * @oh: struct omap_hwmod *
796 *
797 * Called from _init_clocks(). Populates the @oh OCP slave interface
798 * clock pointers. Returns 0 on success or -EINVAL on error.
799 */
800static int _init_interface_clks(struct omap_hwmod *oh)
801{
5d95dde7 802 struct omap_hwmod_ocp_if *os;
11cd4b94 803 struct list_head *p;
63c85238 804 struct clk *c;
5d95dde7 805 int i = 0;
63c85238
PW
806 int ret = 0;
807
11cd4b94 808 p = oh->slave_ports.next;
2221b5cd 809
5d95dde7 810 while (i < oh->slaves_cnt) {
11cd4b94 811 os = _fetch_next_ocp_if(&p, &i);
50ebdac2 812 if (!os->clk)
63c85238
PW
813 continue;
814
6ea74cb9
RN
815 c = clk_get(NULL, os->clk);
816 if (IS_ERR(c)) {
3d0cb73e
JP
817 pr_warn("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
818 oh->name, os->clk);
63c85238 819 ret = -EINVAL;
0e7dc862 820 continue;
dc75925d 821 }
63c85238 822 os->_clk = c;
4d7cb45e
RN
823 /*
824 * HACK: This needs a re-visit once clk_prepare() is implemented
825 * to do something meaningful. Today its just a no-op.
826 * If clk_prepare() is used at some point to do things like
827 * voltage scaling etc, then this would have to be moved to
828 * some point where subsystems like i2c and pmic become
829 * available.
830 */
831 clk_prepare(os->_clk);
63c85238
PW
832 }
833
834 return ret;
835}
836
837/**
838 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
839 * @oh: struct omap_hwmod *
840 *
841 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
842 * clock pointers. Returns 0 on success or -EINVAL on error.
843 */
844static int _init_opt_clks(struct omap_hwmod *oh)
845{
846 struct omap_hwmod_opt_clk *oc;
847 struct clk *c;
848 int i;
849 int ret = 0;
850
851 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
6ea74cb9
RN
852 c = clk_get(NULL, oc->clk);
853 if (IS_ERR(c)) {
3d0cb73e
JP
854 pr_warn("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
855 oh->name, oc->clk);
63c85238 856 ret = -EINVAL;
0e7dc862 857 continue;
dc75925d 858 }
63c85238 859 oc->_clk = c;
4d7cb45e
RN
860 /*
861 * HACK: This needs a re-visit once clk_prepare() is implemented
862 * to do something meaningful. Today its just a no-op.
863 * If clk_prepare() is used at some point to do things like
864 * voltage scaling etc, then this would have to be moved to
865 * some point where subsystems like i2c and pmic become
866 * available.
867 */
868 clk_prepare(oc->_clk);
63c85238
PW
869 }
870
871 return ret;
872}
873
874/**
875 * _enable_clocks - enable hwmod main clock and interface clocks
876 * @oh: struct omap_hwmod *
877 *
878 * Enables all clocks necessary for register reads and writes to succeed
879 * on the hwmod @oh. Returns 0.
880 */
881static int _enable_clocks(struct omap_hwmod *oh)
882{
5d95dde7 883 struct omap_hwmod_ocp_if *os;
11cd4b94 884 struct list_head *p;
5d95dde7 885 int i = 0;
63c85238
PW
886
887 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
888
4d3ae5a9 889 if (oh->_clk)
63c85238
PW
890 clk_enable(oh->_clk);
891
11cd4b94 892 p = oh->slave_ports.next;
2221b5cd 893
5d95dde7 894 while (i < oh->slaves_cnt) {
11cd4b94 895 os = _fetch_next_ocp_if(&p, &i);
63c85238 896
5d95dde7
PW
897 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
898 clk_enable(os->_clk);
63c85238
PW
899 }
900
901 /* The opt clocks are controlled by the device driver. */
902
903 return 0;
904}
905
906/**
907 * _disable_clocks - disable hwmod main clock and interface clocks
908 * @oh: struct omap_hwmod *
909 *
910 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
911 */
912static int _disable_clocks(struct omap_hwmod *oh)
913{
5d95dde7 914 struct omap_hwmod_ocp_if *os;
11cd4b94 915 struct list_head *p;
5d95dde7 916 int i = 0;
63c85238
PW
917
918 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
919
4d3ae5a9 920 if (oh->_clk)
63c85238
PW
921 clk_disable(oh->_clk);
922
11cd4b94 923 p = oh->slave_ports.next;
2221b5cd 924
5d95dde7 925 while (i < oh->slaves_cnt) {
11cd4b94 926 os = _fetch_next_ocp_if(&p, &i);
63c85238 927
5d95dde7
PW
928 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
929 clk_disable(os->_clk);
63c85238
PW
930 }
931
932 /* The opt clocks are controlled by the device driver. */
933
934 return 0;
935}
936
96835af9
BC
937static void _enable_optional_clocks(struct omap_hwmod *oh)
938{
939 struct omap_hwmod_opt_clk *oc;
940 int i;
941
942 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
943
944 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
945 if (oc->_clk) {
946 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
5dcc3b97 947 __clk_get_name(oc->_clk));
96835af9
BC
948 clk_enable(oc->_clk);
949 }
950}
951
952static void _disable_optional_clocks(struct omap_hwmod *oh)
953{
954 struct omap_hwmod_opt_clk *oc;
955 int i;
956
957 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
958
959 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
960 if (oc->_clk) {
961 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
5dcc3b97 962 __clk_get_name(oc->_clk));
96835af9
BC
963 clk_disable(oc->_clk);
964 }
965}
966
45c38252 967/**
3d9f0327 968 * _omap4_enable_module - enable CLKCTRL modulemode on OMAP4
45c38252
BC
969 * @oh: struct omap_hwmod *
970 *
971 * Enables the PRCM module mode related to the hwmod @oh.
972 * No return value.
973 */
3d9f0327 974static void _omap4_enable_module(struct omap_hwmod *oh)
45c38252 975{
45c38252
BC
976 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
977 return;
978
3d9f0327
KH
979 pr_debug("omap_hwmod: %s: %s: %d\n",
980 oh->name, __func__, oh->prcm.omap4.modulemode);
45c38252 981
128603f0
TK
982 omap_cm_module_enable(oh->prcm.omap4.modulemode,
983 oh->clkdm->prcm_partition,
984 oh->clkdm->cm_inst, oh->prcm.omap4.clkctrl_offs);
45c38252
BC
985}
986
987/**
bfc141e3
BC
988 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
989 * @oh: struct omap_hwmod *
990 *
991 * Wait for a module @oh to enter slave idle. Returns 0 if the module
992 * does not have an IDLEST bit or if the module successfully enters
993 * slave idle; otherwise, pass along the return value of the
994 * appropriate *_cm*_wait_module_idle() function.
995 */
996static int _omap4_wait_target_disable(struct omap_hwmod *oh)
997{
2b026d13 998 if (!oh)
bfc141e3
BC
999 return -EINVAL;
1000
2b026d13 1001 if (oh->_int_flags & _HWMOD_NO_MPU_PORT || !oh->clkdm)
bfc141e3
BC
1002 return 0;
1003
1004 if (oh->flags & HWMOD_NO_IDLEST)
1005 return 0;
1006
a8ae5afa
TK
1007 return omap_cm_wait_module_idle(oh->clkdm->prcm_partition,
1008 oh->clkdm->cm_inst,
1009 oh->prcm.omap4.clkctrl_offs, 0);
bfc141e3
BC
1010}
1011
212738a4
PW
1012/**
1013 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
1014 * @oh: struct omap_hwmod *oh
1015 *
1016 * Count and return the number of MPU IRQs associated with the hwmod
1017 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
1018 * NULL.
1019 */
1020static int _count_mpu_irqs(struct omap_hwmod *oh)
1021{
1022 struct omap_hwmod_irq_info *ohii;
1023 int i = 0;
1024
1025 if (!oh || !oh->mpu_irqs)
1026 return 0;
1027
1028 do {
1029 ohii = &oh->mpu_irqs[i++];
1030 } while (ohii->irq != -1);
1031
cc1b0765 1032 return i-1;
212738a4
PW
1033}
1034
bc614958
PW
1035/**
1036 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
1037 * @oh: struct omap_hwmod *oh
1038 *
1039 * Count and return the number of SDMA request lines associated with
1040 * the hwmod @oh. Used to allocate struct resource data. Returns 0
1041 * if @oh is NULL.
1042 */
1043static int _count_sdma_reqs(struct omap_hwmod *oh)
1044{
1045 struct omap_hwmod_dma_info *ohdi;
1046 int i = 0;
1047
1048 if (!oh || !oh->sdma_reqs)
1049 return 0;
1050
1051 do {
1052 ohdi = &oh->sdma_reqs[i++];
1053 } while (ohdi->dma_req != -1);
1054
cc1b0765 1055 return i-1;
bc614958
PW
1056}
1057
78183f3f
PW
1058/**
1059 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
1060 * @oh: struct omap_hwmod *oh
1061 *
1062 * Count and return the number of address space ranges associated with
1063 * the hwmod @oh. Used to allocate struct resource data. Returns 0
1064 * if @oh is NULL.
1065 */
1066static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
1067{
1068 struct omap_hwmod_addr_space *mem;
1069 int i = 0;
1070
1071 if (!os || !os->addr)
1072 return 0;
1073
1074 do {
1075 mem = &os->addr[i++];
1076 } while (mem->pa_start != mem->pa_end);
1077
cc1b0765 1078 return i-1;
78183f3f
PW
1079}
1080
5e8370f1
PW
1081/**
1082 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
1083 * @oh: struct omap_hwmod * to operate on
1084 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
1085 * @irq: pointer to an unsigned int to store the MPU IRQ number to
1086 *
1087 * Retrieve a MPU hardware IRQ line number named by @name associated
1088 * with the IP block pointed to by @oh. The IRQ number will be filled
1089 * into the address pointed to by @dma. When @name is non-null, the
1090 * IRQ line number associated with the named entry will be returned.
1091 * If @name is null, the first matching entry will be returned. Data
1092 * order is not meaningful in hwmod data, so callers are strongly
1093 * encouraged to use a non-null @name whenever possible to avoid
1094 * unpredictable effects if hwmod data is later added that causes data
1095 * ordering to change. Returns 0 upon success or a negative error
1096 * code upon error.
1097 */
1098static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
1099 unsigned int *irq)
1100{
1101 int i;
1102 bool found = false;
1103
1104 if (!oh->mpu_irqs)
1105 return -ENOENT;
1106
1107 i = 0;
1108 while (oh->mpu_irqs[i].irq != -1) {
1109 if (name == oh->mpu_irqs[i].name ||
1110 !strcmp(name, oh->mpu_irqs[i].name)) {
1111 found = true;
1112 break;
1113 }
1114 i++;
1115 }
1116
1117 if (!found)
1118 return -ENOENT;
1119
1120 *irq = oh->mpu_irqs[i].irq;
1121
1122 return 0;
1123}
1124
1125/**
1126 * _get_sdma_req_by_name - fetch SDMA request line ID by name
1127 * @oh: struct omap_hwmod * to operate on
1128 * @name: pointer to the name of the SDMA request line to fetch (optional)
1129 * @dma: pointer to an unsigned int to store the request line ID to
1130 *
1131 * Retrieve an SDMA request line ID named by @name on the IP block
1132 * pointed to by @oh. The ID will be filled into the address pointed
1133 * to by @dma. When @name is non-null, the request line ID associated
1134 * with the named entry will be returned. If @name is null, the first
1135 * matching entry will be returned. Data order is not meaningful in
1136 * hwmod data, so callers are strongly encouraged to use a non-null
1137 * @name whenever possible to avoid unpredictable effects if hwmod
1138 * data is later added that causes data ordering to change. Returns 0
1139 * upon success or a negative error code upon error.
1140 */
1141static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
1142 unsigned int *dma)
1143{
1144 int i;
1145 bool found = false;
1146
1147 if (!oh->sdma_reqs)
1148 return -ENOENT;
1149
1150 i = 0;
1151 while (oh->sdma_reqs[i].dma_req != -1) {
1152 if (name == oh->sdma_reqs[i].name ||
1153 !strcmp(name, oh->sdma_reqs[i].name)) {
1154 found = true;
1155 break;
1156 }
1157 i++;
1158 }
1159
1160 if (!found)
1161 return -ENOENT;
1162
1163 *dma = oh->sdma_reqs[i].dma_req;
1164
1165 return 0;
1166}
1167
1168/**
1169 * _get_addr_space_by_name - fetch address space start & end by name
1170 * @oh: struct omap_hwmod * to operate on
1171 * @name: pointer to the name of the address space to fetch (optional)
1172 * @pa_start: pointer to a u32 to store the starting address to
1173 * @pa_end: pointer to a u32 to store the ending address to
1174 *
1175 * Retrieve address space start and end addresses for the IP block
1176 * pointed to by @oh. The data will be filled into the addresses
1177 * pointed to by @pa_start and @pa_end. When @name is non-null, the
1178 * address space data associated with the named entry will be
1179 * returned. If @name is null, the first matching entry will be
1180 * returned. Data order is not meaningful in hwmod data, so callers
1181 * are strongly encouraged to use a non-null @name whenever possible
1182 * to avoid unpredictable effects if hwmod data is later added that
1183 * causes data ordering to change. Returns 0 upon success or a
1184 * negative error code upon error.
1185 */
1186static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1187 u32 *pa_start, u32 *pa_end)
1188{
1189 int i, j;
1190 struct omap_hwmod_ocp_if *os;
2221b5cd 1191 struct list_head *p = NULL;
5e8370f1
PW
1192 bool found = false;
1193
11cd4b94 1194 p = oh->slave_ports.next;
2221b5cd 1195
5d95dde7
PW
1196 i = 0;
1197 while (i < oh->slaves_cnt) {
11cd4b94 1198 os = _fetch_next_ocp_if(&p, &i);
5e8370f1
PW
1199
1200 if (!os->addr)
1201 return -ENOENT;
1202
1203 j = 0;
1204 while (os->addr[j].pa_start != os->addr[j].pa_end) {
1205 if (name == os->addr[j].name ||
1206 !strcmp(name, os->addr[j].name)) {
1207 found = true;
1208 break;
1209 }
1210 j++;
1211 }
1212
1213 if (found)
1214 break;
1215 }
1216
1217 if (!found)
1218 return -ENOENT;
1219
1220 *pa_start = os->addr[j].pa_start;
1221 *pa_end = os->addr[j].pa_end;
1222
1223 return 0;
1224}
1225
63c85238 1226/**
24dbc213 1227 * _save_mpu_port_index - find and save the index to @oh's MPU port
63c85238
PW
1228 * @oh: struct omap_hwmod *
1229 *
24dbc213
PW
1230 * Determines the array index of the OCP slave port that the MPU uses
1231 * to address the device, and saves it into the struct omap_hwmod.
1232 * Intended to be called during hwmod registration only. No return
1233 * value.
63c85238 1234 */
24dbc213 1235static void __init _save_mpu_port_index(struct omap_hwmod *oh)
63c85238 1236{
24dbc213 1237 struct omap_hwmod_ocp_if *os = NULL;
11cd4b94 1238 struct list_head *p;
5d95dde7 1239 int i = 0;
63c85238 1240
5d95dde7 1241 if (!oh)
24dbc213
PW
1242 return;
1243
1244 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
63c85238 1245
11cd4b94 1246 p = oh->slave_ports.next;
2221b5cd 1247
5d95dde7 1248 while (i < oh->slaves_cnt) {
11cd4b94 1249 os = _fetch_next_ocp_if(&p, &i);
63c85238 1250 if (os->user & OCP_USER_MPU) {
2221b5cd 1251 oh->_mpu_port = os;
24dbc213 1252 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
63c85238
PW
1253 break;
1254 }
1255 }
1256
24dbc213 1257 return;
63c85238
PW
1258}
1259
2d6141ba
PW
1260/**
1261 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1262 * @oh: struct omap_hwmod *
1263 *
1264 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1265 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1266 * communicate with the IP block. This interface need not be directly
1267 * connected to the MPU (and almost certainly is not), but is directly
1268 * connected to the IP block represented by @oh. Returns a pointer
1269 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1270 * error or if there does not appear to be a path from the MPU to this
1271 * IP block.
1272 */
1273static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1274{
1275 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1276 return NULL;
1277
11cd4b94 1278 return oh->_mpu_port;
2d6141ba
PW
1279};
1280
63c85238 1281/**
c9aafd23 1282 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
63c85238
PW
1283 * @oh: struct omap_hwmod *
1284 *
c9aafd23
PW
1285 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1286 * the register target MPU address space; or returns NULL upon error.
63c85238 1287 */
c9aafd23 1288static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
63c85238
PW
1289{
1290 struct omap_hwmod_ocp_if *os;
1291 struct omap_hwmod_addr_space *mem;
c9aafd23 1292 int found = 0, i = 0;
63c85238 1293
2d6141ba 1294 os = _find_mpu_rt_port(oh);
24dbc213 1295 if (!os || !os->addr)
78183f3f
PW
1296 return NULL;
1297
1298 do {
1299 mem = &os->addr[i++];
1300 if (mem->flags & ADDR_TYPE_RT)
63c85238 1301 found = 1;
78183f3f 1302 } while (!found && mem->pa_start != mem->pa_end);
63c85238 1303
c9aafd23 1304 return (found) ? mem : NULL;
63c85238
PW
1305}
1306
1307/**
74ff3a68 1308 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
63c85238
PW
1309 * @oh: struct omap_hwmod *
1310 *
006c7f18
PW
1311 * Ensure that the OCP_SYSCONFIG register for the IP block represented
1312 * by @oh is set to indicate to the PRCM that the IP block is active.
1313 * Usually this means placing the module into smart-idle mode and
1314 * smart-standby, but if there is a bug in the automatic idle handling
1315 * for the IP block, it may need to be placed into the force-idle or
1316 * no-idle variants of these modes. No return value.
63c85238 1317 */
74ff3a68 1318static void _enable_sysc(struct omap_hwmod *oh)
63c85238 1319{
43b40992 1320 u8 idlemode, sf;
63c85238 1321 u32 v;
006c7f18 1322 bool clkdm_act;
f5dd3bb5 1323 struct clockdomain *clkdm;
63c85238 1324
43b40992 1325 if (!oh->class->sysc)
63c85238
PW
1326 return;
1327
613ad0e9
TK
1328 /*
1329 * Wait until reset has completed, this is needed as the IP
1330 * block is reset automatically by hardware in some cases
1331 * (off-mode for example), and the drivers require the
1332 * IP to be ready when they access it
1333 */
1334 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1335 _enable_optional_clocks(oh);
1336 _wait_softreset_complete(oh);
1337 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1338 _disable_optional_clocks(oh);
1339
63c85238 1340 v = oh->_sysc_cache;
43b40992 1341 sf = oh->class->sysc->sysc_flags;
63c85238 1342
f5dd3bb5 1343 clkdm = _get_clkdm(oh);
43b40992 1344 if (sf & SYSC_HAS_SIDLEMODE) {
ca43ea34
RN
1345 if (oh->flags & HWMOD_SWSUP_SIDLE ||
1346 oh->flags & HWMOD_SWSUP_SIDLE_ACT) {
35513171
RN
1347 idlemode = HWMOD_IDLEMODE_NO;
1348 } else {
1349 if (sf & SYSC_HAS_ENAWAKEUP)
1350 _enable_wakeup(oh, &v);
1351 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
1352 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1353 else
1354 idlemode = HWMOD_IDLEMODE_SMART;
1355 }
1356
1357 /*
1358 * This is special handling for some IPs like
1359 * 32k sync timer. Force them to idle!
1360 */
f5dd3bb5 1361 clkdm_act = (clkdm && clkdm->flags & CLKDM_ACTIVE_WITH_MPU);
006c7f18
PW
1362 if (clkdm_act && !(oh->class->sysc->idlemodes &
1363 (SIDLE_SMART | SIDLE_SMART_WKUP)))
1364 idlemode = HWMOD_IDLEMODE_FORCE;
35513171 1365
63c85238
PW
1366 _set_slave_idlemode(oh, idlemode, &v);
1367 }
1368
43b40992 1369 if (sf & SYSC_HAS_MIDLEMODE) {
092bc089
GI
1370 if (oh->flags & HWMOD_FORCE_MSTANDBY) {
1371 idlemode = HWMOD_IDLEMODE_FORCE;
1372 } else if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
724019b0
BC
1373 idlemode = HWMOD_IDLEMODE_NO;
1374 } else {
1375 if (sf & SYSC_HAS_ENAWAKEUP)
1376 _enable_wakeup(oh, &v);
1377 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1378 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1379 else
1380 idlemode = HWMOD_IDLEMODE_SMART;
1381 }
63c85238
PW
1382 _set_master_standbymode(oh, idlemode, &v);
1383 }
1384
a16b1f7f
PW
1385 /*
1386 * XXX The clock framework should handle this, by
1387 * calling into this code. But this must wait until the
1388 * clock structures are tagged with omap_hwmod entries
1389 */
43b40992
PW
1390 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1391 (sf & SYSC_HAS_CLOCKACTIVITY))
1392 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
63c85238 1393
127500cc
JH
1394 /* If the cached value is the same as the new value, skip the write */
1395 if (oh->_sysc_cache != v)
1396 _write_sysconfig(v, oh);
78f26e87
HH
1397
1398 /*
1399 * Set the autoidle bit only after setting the smartidle bit
1400 * Setting this will not have any impact on the other modules.
1401 */
1402 if (sf & SYSC_HAS_AUTOIDLE) {
1403 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1404 0 : 1;
1405 _set_module_autoidle(oh, idlemode, &v);
1406 _write_sysconfig(v, oh);
1407 }
63c85238
PW
1408}
1409
1410/**
74ff3a68 1411 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
63c85238
PW
1412 * @oh: struct omap_hwmod *
1413 *
1414 * If module is marked as SWSUP_SIDLE, force the module into slave
1415 * idle; otherwise, configure it for smart-idle. If module is marked
1416 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1417 * configure it for smart-standby. No return value.
1418 */
74ff3a68 1419static void _idle_sysc(struct omap_hwmod *oh)
63c85238 1420{
43b40992 1421 u8 idlemode, sf;
63c85238
PW
1422 u32 v;
1423
43b40992 1424 if (!oh->class->sysc)
63c85238
PW
1425 return;
1426
1427 v = oh->_sysc_cache;
43b40992 1428 sf = oh->class->sysc->sysc_flags;
63c85238 1429
43b40992 1430 if (sf & SYSC_HAS_SIDLEMODE) {
35513171 1431 if (oh->flags & HWMOD_SWSUP_SIDLE) {
006c7f18 1432 idlemode = HWMOD_IDLEMODE_FORCE;
35513171
RN
1433 } else {
1434 if (sf & SYSC_HAS_ENAWAKEUP)
1435 _enable_wakeup(oh, &v);
1436 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
1437 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1438 else
1439 idlemode = HWMOD_IDLEMODE_SMART;
1440 }
63c85238
PW
1441 _set_slave_idlemode(oh, idlemode, &v);
1442 }
1443
43b40992 1444 if (sf & SYSC_HAS_MIDLEMODE) {
092bc089
GI
1445 if ((oh->flags & HWMOD_SWSUP_MSTANDBY) ||
1446 (oh->flags & HWMOD_FORCE_MSTANDBY)) {
724019b0
BC
1447 idlemode = HWMOD_IDLEMODE_FORCE;
1448 } else {
1449 if (sf & SYSC_HAS_ENAWAKEUP)
1450 _enable_wakeup(oh, &v);
1451 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1452 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1453 else
1454 idlemode = HWMOD_IDLEMODE_SMART;
1455 }
63c85238
PW
1456 _set_master_standbymode(oh, idlemode, &v);
1457 }
1458
1459 _write_sysconfig(v, oh);
1460}
1461
1462/**
74ff3a68 1463 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
63c85238
PW
1464 * @oh: struct omap_hwmod *
1465 *
1466 * Force the module into slave idle and master suspend. No return
1467 * value.
1468 */
74ff3a68 1469static void _shutdown_sysc(struct omap_hwmod *oh)
63c85238
PW
1470{
1471 u32 v;
43b40992 1472 u8 sf;
63c85238 1473
43b40992 1474 if (!oh->class->sysc)
63c85238
PW
1475 return;
1476
1477 v = oh->_sysc_cache;
43b40992 1478 sf = oh->class->sysc->sysc_flags;
63c85238 1479
43b40992 1480 if (sf & SYSC_HAS_SIDLEMODE)
63c85238
PW
1481 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1482
43b40992 1483 if (sf & SYSC_HAS_MIDLEMODE)
63c85238
PW
1484 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1485
43b40992 1486 if (sf & SYSC_HAS_AUTOIDLE)
726072e5 1487 _set_module_autoidle(oh, 1, &v);
63c85238
PW
1488
1489 _write_sysconfig(v, oh);
1490}
1491
1492/**
1493 * _lookup - find an omap_hwmod by name
1494 * @name: find an omap_hwmod by name
1495 *
1496 * Return a pointer to an omap_hwmod by name, or NULL if not found.
63c85238
PW
1497 */
1498static struct omap_hwmod *_lookup(const char *name)
1499{
1500 struct omap_hwmod *oh, *temp_oh;
1501
1502 oh = NULL;
1503
1504 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1505 if (!strcmp(name, temp_oh->name)) {
1506 oh = temp_oh;
1507 break;
1508 }
1509 }
1510
1511 return oh;
1512}
868c157d 1513
6ae76997
BC
1514/**
1515 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1516 * @oh: struct omap_hwmod *
1517 *
1518 * Convert a clockdomain name stored in a struct omap_hwmod into a
1519 * clockdomain pointer, and save it into the struct omap_hwmod.
868c157d 1520 * Return -EINVAL if the clkdm_name lookup failed.
6ae76997
BC
1521 */
1522static int _init_clkdm(struct omap_hwmod *oh)
1523{
3bb05dbf
PW
1524 if (!oh->clkdm_name) {
1525 pr_debug("omap_hwmod: %s: missing clockdomain\n", oh->name);
6ae76997 1526 return 0;
3bb05dbf 1527 }
6ae76997 1528
6ae76997
BC
1529 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1530 if (!oh->clkdm) {
3d0cb73e 1531 pr_warn("omap_hwmod: %s: could not associate to clkdm %s\n",
6ae76997 1532 oh->name, oh->clkdm_name);
0385c582 1533 return 0;
6ae76997
BC
1534 }
1535
1536 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1537 oh->name, oh->clkdm_name);
1538
1539 return 0;
1540}
63c85238
PW
1541
1542/**
6ae76997
BC
1543 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1544 * well the clockdomain.
63c85238 1545 * @oh: struct omap_hwmod *
97d60162 1546 * @data: not used; pass NULL
63c85238 1547 *
a2debdbd 1548 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
48d54f3f
PW
1549 * Resolves all clock names embedded in the hwmod. Returns 0 on
1550 * success, or a negative error code on failure.
63c85238 1551 */
97d60162 1552static int _init_clocks(struct omap_hwmod *oh, void *data)
63c85238
PW
1553{
1554 int ret = 0;
1555
48d54f3f
PW
1556 if (oh->_state != _HWMOD_STATE_REGISTERED)
1557 return 0;
63c85238
PW
1558
1559 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1560
b797be1d
VH
1561 if (soc_ops.init_clkdm)
1562 ret |= soc_ops.init_clkdm(oh);
1563
63c85238
PW
1564 ret |= _init_main_clk(oh);
1565 ret |= _init_interface_clks(oh);
1566 ret |= _init_opt_clks(oh);
1567
f5c1f84b
BC
1568 if (!ret)
1569 oh->_state = _HWMOD_STATE_CLKS_INITED;
6652271a 1570 else
3d0cb73e 1571 pr_warn("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
63c85238 1572
09c35f2f 1573 return ret;
63c85238
PW
1574}
1575
5365efbe 1576/**
cc1226e7 1577 * _lookup_hardreset - fill register bit info for this hwmod/reset line
5365efbe
BC
1578 * @oh: struct omap_hwmod *
1579 * @name: name of the reset line in the context of this hwmod
cc1226e7 1580 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
5365efbe
BC
1581 *
1582 * Return the bit position of the reset line that match the
1583 * input name. Return -ENOENT if not found.
1584 */
a032d33b
PW
1585static int _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1586 struct omap_hwmod_rst_info *ohri)
5365efbe
BC
1587{
1588 int i;
1589
1590 for (i = 0; i < oh->rst_lines_cnt; i++) {
1591 const char *rst_line = oh->rst_lines[i].name;
1592 if (!strcmp(rst_line, name)) {
cc1226e7 1593 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1594 ohri->st_shift = oh->rst_lines[i].st_shift;
1595 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1596 oh->name, __func__, rst_line, ohri->rst_shift,
1597 ohri->st_shift);
5365efbe 1598
cc1226e7 1599 return 0;
5365efbe
BC
1600 }
1601 }
1602
1603 return -ENOENT;
1604}
1605
1606/**
1607 * _assert_hardreset - assert the HW reset line of submodules
1608 * contained in the hwmod module.
1609 * @oh: struct omap_hwmod *
1610 * @name: name of the reset line to lookup and assert
1611 *
b8249cf2
KH
1612 * Some IP like dsp, ipu or iva contain processor that require an HW
1613 * reset line to be assert / deassert in order to enable fully the IP.
1614 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1615 * asserting the hardreset line on the currently-booted SoC, or passes
1616 * along the return value from _lookup_hardreset() or the SoC's
1617 * assert_hardreset code.
5365efbe
BC
1618 */
1619static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1620{
cc1226e7 1621 struct omap_hwmod_rst_info ohri;
a032d33b 1622 int ret = -EINVAL;
5365efbe
BC
1623
1624 if (!oh)
1625 return -EINVAL;
1626
b8249cf2
KH
1627 if (!soc_ops.assert_hardreset)
1628 return -ENOSYS;
1629
cc1226e7 1630 ret = _lookup_hardreset(oh, name, &ohri);
a032d33b 1631 if (ret < 0)
cc1226e7 1632 return ret;
5365efbe 1633
b8249cf2
KH
1634 ret = soc_ops.assert_hardreset(oh, &ohri);
1635
1636 return ret;
5365efbe
BC
1637}
1638
1639/**
1640 * _deassert_hardreset - deassert the HW reset line of submodules contained
1641 * in the hwmod module.
1642 * @oh: struct omap_hwmod *
1643 * @name: name of the reset line to look up and deassert
1644 *
b8249cf2
KH
1645 * Some IP like dsp, ipu or iva contain processor that require an HW
1646 * reset line to be assert / deassert in order to enable fully the IP.
1647 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1648 * deasserting the hardreset line on the currently-booted SoC, or passes
1649 * along the return value from _lookup_hardreset() or the SoC's
1650 * deassert_hardreset code.
5365efbe
BC
1651 */
1652static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1653{
cc1226e7 1654 struct omap_hwmod_rst_info ohri;
b8249cf2 1655 int ret = -EINVAL;
e8e96dff 1656 int hwsup = 0;
5365efbe
BC
1657
1658 if (!oh)
1659 return -EINVAL;
1660
b8249cf2
KH
1661 if (!soc_ops.deassert_hardreset)
1662 return -ENOSYS;
1663
cc1226e7 1664 ret = _lookup_hardreset(oh, name, &ohri);
c48cd659 1665 if (ret < 0)
cc1226e7 1666 return ret;
5365efbe 1667
e8e96dff
ORL
1668 if (oh->clkdm) {
1669 /*
1670 * A clockdomain must be in SW_SUP otherwise reset
1671 * might not be completed. The clockdomain can be set
1672 * in HW_AUTO only when the module become ready.
1673 */
1674 hwsup = clkdm_in_hwsup(oh->clkdm);
1675 ret = clkdm_hwmod_enable(oh->clkdm, oh);
1676 if (ret) {
1677 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1678 oh->name, oh->clkdm->name, ret);
1679 return ret;
1680 }
1681 }
1682
1683 _enable_clocks(oh);
1684 if (soc_ops.enable_module)
1685 soc_ops.enable_module(oh);
1686
b8249cf2 1687 ret = soc_ops.deassert_hardreset(oh, &ohri);
e8e96dff
ORL
1688
1689 if (soc_ops.disable_module)
1690 soc_ops.disable_module(oh);
1691 _disable_clocks(oh);
1692
cc1226e7 1693 if (ret == -EBUSY)
3d0cb73e 1694 pr_warn("omap_hwmod: %s: failed to hardreset\n", oh->name);
5365efbe 1695
e8e96dff
ORL
1696 if (!ret) {
1697 /*
1698 * Set the clockdomain to HW_AUTO, assuming that the
1699 * previous state was HW_AUTO.
1700 */
1701 if (oh->clkdm && hwsup)
1702 clkdm_allow_idle(oh->clkdm);
1703 } else {
1704 if (oh->clkdm)
1705 clkdm_hwmod_disable(oh->clkdm, oh);
1706 }
1707
cc1226e7 1708 return ret;
5365efbe
BC
1709}
1710
1711/**
1712 * _read_hardreset - read the HW reset line state of submodules
1713 * contained in the hwmod module
1714 * @oh: struct omap_hwmod *
1715 * @name: name of the reset line to look up and read
1716 *
b8249cf2
KH
1717 * Return the state of the reset line. Returns -EINVAL if @oh is
1718 * null, -ENOSYS if we have no way of reading the hardreset line
1719 * status on the currently-booted SoC, or passes along the return
1720 * value from _lookup_hardreset() or the SoC's is_hardreset_asserted
1721 * code.
5365efbe
BC
1722 */
1723static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1724{
cc1226e7 1725 struct omap_hwmod_rst_info ohri;
a032d33b 1726 int ret = -EINVAL;
5365efbe
BC
1727
1728 if (!oh)
1729 return -EINVAL;
1730
b8249cf2
KH
1731 if (!soc_ops.is_hardreset_asserted)
1732 return -ENOSYS;
1733
cc1226e7 1734 ret = _lookup_hardreset(oh, name, &ohri);
a032d33b 1735 if (ret < 0)
cc1226e7 1736 return ret;
5365efbe 1737
b8249cf2 1738 return soc_ops.is_hardreset_asserted(oh, &ohri);
5365efbe
BC
1739}
1740
747834ab 1741/**
eb05f691 1742 * _are_all_hardreset_lines_asserted - return true if the @oh is hard-reset
747834ab
PW
1743 * @oh: struct omap_hwmod *
1744 *
eb05f691
ORL
1745 * If all hardreset lines associated with @oh are asserted, then return true.
1746 * Otherwise, if part of @oh is out hardreset or if no hardreset lines
1747 * associated with @oh are asserted, then return false.
747834ab 1748 * This function is used to avoid executing some parts of the IP block
eb05f691 1749 * enable/disable sequence if its hardreset line is set.
747834ab 1750 */
eb05f691 1751static bool _are_all_hardreset_lines_asserted(struct omap_hwmod *oh)
747834ab 1752{
eb05f691 1753 int i, rst_cnt = 0;
747834ab
PW
1754
1755 if (oh->rst_lines_cnt == 0)
1756 return false;
1757
1758 for (i = 0; i < oh->rst_lines_cnt; i++)
1759 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
eb05f691
ORL
1760 rst_cnt++;
1761
1762 if (oh->rst_lines_cnt == rst_cnt)
1763 return true;
747834ab
PW
1764
1765 return false;
1766}
1767
e9332b6e
PW
1768/**
1769 * _are_any_hardreset_lines_asserted - return true if any part of @oh is
1770 * hard-reset
1771 * @oh: struct omap_hwmod *
1772 *
1773 * If any hardreset lines associated with @oh are asserted, then
1774 * return true. Otherwise, if no hardreset lines associated with @oh
1775 * are asserted, or if @oh has no hardreset lines, then return false.
1776 * This function is used to avoid executing some parts of the IP block
1777 * enable/disable sequence if any hardreset line is set.
1778 */
1779static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1780{
1781 int rst_cnt = 0;
1782 int i;
1783
1784 for (i = 0; i < oh->rst_lines_cnt && rst_cnt == 0; i++)
1785 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1786 rst_cnt++;
1787
1788 return (rst_cnt) ? true : false;
1789}
1790
747834ab
PW
1791/**
1792 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1793 * @oh: struct omap_hwmod *
1794 *
1795 * Disable the PRCM module mode related to the hwmod @oh.
1796 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1797 */
1798static int _omap4_disable_module(struct omap_hwmod *oh)
1799{
1800 int v;
1801
747834ab
PW
1802 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1803 return -EINVAL;
1804
eb05f691
ORL
1805 /*
1806 * Since integration code might still be doing something, only
1807 * disable if all lines are under hardreset.
1808 */
e9332b6e 1809 if (_are_any_hardreset_lines_asserted(oh))
eb05f691
ORL
1810 return 0;
1811
747834ab
PW
1812 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1813
128603f0
TK
1814 omap_cm_module_disable(oh->clkdm->prcm_partition, oh->clkdm->cm_inst,
1815 oh->prcm.omap4.clkctrl_offs);
747834ab 1816
747834ab
PW
1817 v = _omap4_wait_target_disable(oh);
1818 if (v)
1819 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1820 oh->name);
1821
1822 return 0;
1823}
1824
63c85238 1825/**
bd36179e 1826 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
63c85238
PW
1827 * @oh: struct omap_hwmod *
1828 *
1829 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
30e105c0
PW
1830 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1831 * reset this way, -EINVAL if the hwmod is in the wrong state,
1832 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
2cb06814
BC
1833 *
1834 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
bd36179e 1835 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
2cb06814
BC
1836 * use the SYSCONFIG softreset bit to provide the status.
1837 *
bd36179e
PW
1838 * Note that some IP like McBSP do have reset control but don't have
1839 * reset status.
63c85238 1840 */
bd36179e 1841static int _ocp_softreset(struct omap_hwmod *oh)
63c85238 1842{
613ad0e9 1843 u32 v;
6f8b7ff5 1844 int c = 0;
96835af9 1845 int ret = 0;
63c85238 1846
43b40992 1847 if (!oh->class->sysc ||
2cb06814 1848 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
30e105c0 1849 return -ENOENT;
63c85238
PW
1850
1851 /* clocks must be on for this operation */
1852 if (oh->_state != _HWMOD_STATE_ENABLED) {
7852ec05
PW
1853 pr_warn("omap_hwmod: %s: reset can only be entered from enabled state\n",
1854 oh->name);
63c85238
PW
1855 return -EINVAL;
1856 }
1857
96835af9
BC
1858 /* For some modules, all optionnal clocks need to be enabled as well */
1859 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1860 _enable_optional_clocks(oh);
1861
bd36179e 1862 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
63c85238
PW
1863
1864 v = oh->_sysc_cache;
96835af9
BC
1865 ret = _set_softreset(oh, &v);
1866 if (ret)
1867 goto dis_opt_clks;
313a76ee 1868
63c85238
PW
1869 _write_sysconfig(v, oh);
1870
d99de7f5
FGL
1871 if (oh->class->sysc->srst_udelay)
1872 udelay(oh->class->sysc->srst_udelay);
1873
613ad0e9 1874 c = _wait_softreset_complete(oh);
01142519 1875 if (c == MAX_MODULE_SOFTRESET_WAIT) {
3d0cb73e
JP
1876 pr_warn("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1877 oh->name, MAX_MODULE_SOFTRESET_WAIT);
01142519
IS
1878 ret = -ETIMEDOUT;
1879 goto dis_opt_clks;
1880 } else {
5365efbe 1881 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
01142519
IS
1882 }
1883
1884 ret = _clear_softreset(oh, &v);
1885 if (ret)
1886 goto dis_opt_clks;
1887
1888 _write_sysconfig(v, oh);
63c85238
PW
1889
1890 /*
1891 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1892 * _wait_target_ready() or _reset()
1893 */
1894
96835af9
BC
1895dis_opt_clks:
1896 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1897 _disable_optional_clocks(oh);
1898
1899 return ret;
63c85238
PW
1900}
1901
bd36179e
PW
1902/**
1903 * _reset - reset an omap_hwmod
1904 * @oh: struct omap_hwmod *
1905 *
30e105c0
PW
1906 * Resets an omap_hwmod @oh. If the module has a custom reset
1907 * function pointer defined, then call it to reset the IP block, and
1908 * pass along its return value to the caller. Otherwise, if the IP
1909 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1910 * associated with it, call a function to reset the IP block via that
1911 * method, and pass along the return value to the caller. Finally, if
1912 * the IP block has some hardreset lines associated with it, assert
1913 * all of those, but do _not_ deassert them. (This is because driver
1914 * authors have expressed an apparent requirement to control the
1915 * deassertion of the hardreset lines themselves.)
1916 *
1917 * The default software reset mechanism for most OMAP IP blocks is
1918 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1919 * hwmods cannot be reset via this method. Some are not targets and
1920 * therefore have no OCP header registers to access. Others (like the
1921 * IVA) have idiosyncratic reset sequences. So for these relatively
1922 * rare cases, custom reset code can be supplied in the struct
6668546f
KVA
1923 * omap_hwmod_class .reset function pointer.
1924 *
1925 * _set_dmadisable() is called to set the DMADISABLE bit so that it
1926 * does not prevent idling of the system. This is necessary for cases
1927 * where ROMCODE/BOOTLOADER uses dma and transfers control to the
1928 * kernel without disabling dma.
1929 *
1930 * Passes along the return value from either _ocp_softreset() or the
1931 * custom reset function - these must return -EINVAL if the hwmod
1932 * cannot be reset this way or if the hwmod is in the wrong state,
1933 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
bd36179e
PW
1934 */
1935static int _reset(struct omap_hwmod *oh)
1936{
30e105c0 1937 int i, r;
bd36179e
PW
1938
1939 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1940
30e105c0
PW
1941 if (oh->class->reset) {
1942 r = oh->class->reset(oh);
1943 } else {
1944 if (oh->rst_lines_cnt > 0) {
1945 for (i = 0; i < oh->rst_lines_cnt; i++)
1946 _assert_hardreset(oh, oh->rst_lines[i].name);
1947 return 0;
1948 } else {
1949 r = _ocp_softreset(oh);
1950 if (r == -ENOENT)
1951 r = 0;
1952 }
1953 }
1954
6668546f
KVA
1955 _set_dmadisable(oh);
1956
9c8b0ec7 1957 /*
30e105c0
PW
1958 * OCP_SYSCONFIG bits need to be reprogrammed after a
1959 * softreset. The _enable() function should be split to avoid
1960 * the rewrite of the OCP_SYSCONFIG register.
9c8b0ec7 1961 */
2800852a
RN
1962 if (oh->class->sysc) {
1963 _update_sysc_cache(oh);
1964 _enable_sysc(oh);
1965 }
1966
30e105c0 1967 return r;
bd36179e
PW
1968}
1969
5165882a
VB
1970/**
1971 * _reconfigure_io_chain - clear any I/O chain wakeups and reconfigure chain
1972 *
1973 * Call the appropriate PRM function to clear any logged I/O chain
1974 * wakeups and to reconfigure the chain. This apparently needs to be
1975 * done upon every mux change. Since hwmods can be concurrently
1976 * enabled and idled, hold a spinlock around the I/O chain
1977 * reconfiguration sequence. No return value.
1978 *
1979 * XXX When the PRM code is moved to drivers, this function can be removed,
1980 * as the PRM infrastructure should abstract this.
1981 */
1982static void _reconfigure_io_chain(void)
1983{
1984 unsigned long flags;
1985
1986 spin_lock_irqsave(&io_chain_lock, flags);
1987
7db143b8 1988 if (cpu_is_omap34xx())
5165882a
VB
1989 omap3xxx_prm_reconfigure_io_chain();
1990 else if (cpu_is_omap44xx())
1991 omap44xx_prm_reconfigure_io_chain();
1992
1993 spin_unlock_irqrestore(&io_chain_lock, flags);
1994}
1995
e6d3a8b0
RN
1996/**
1997 * _omap4_update_context_lost - increment hwmod context loss counter if
1998 * hwmod context was lost, and clear hardware context loss reg
1999 * @oh: hwmod to check for context loss
2000 *
2001 * If the PRCM indicates that the hwmod @oh lost context, increment
2002 * our in-memory context loss counter, and clear the RM_*_CONTEXT
2003 * bits. No return value.
2004 */
2005static void _omap4_update_context_lost(struct omap_hwmod *oh)
2006{
2007 if (oh->prcm.omap4.flags & HWMOD_OMAP4_NO_CONTEXT_LOSS_BIT)
2008 return;
2009
2010 if (!prm_was_any_context_lost_old(oh->clkdm->pwrdm.ptr->prcm_partition,
2011 oh->clkdm->pwrdm.ptr->prcm_offs,
2012 oh->prcm.omap4.context_offs))
2013 return;
2014
2015 oh->prcm.omap4.context_lost_counter++;
2016 prm_clear_context_loss_flags_old(oh->clkdm->pwrdm.ptr->prcm_partition,
2017 oh->clkdm->pwrdm.ptr->prcm_offs,
2018 oh->prcm.omap4.context_offs);
2019}
2020
2021/**
2022 * _omap4_get_context_lost - get context loss counter for a hwmod
2023 * @oh: hwmod to get context loss counter for
2024 *
2025 * Returns the in-memory context loss counter for a hwmod.
2026 */
2027static int _omap4_get_context_lost(struct omap_hwmod *oh)
2028{
2029 return oh->prcm.omap4.context_lost_counter;
2030}
2031
6d266f63
PW
2032/**
2033 * _enable_preprogram - Pre-program an IP block during the _enable() process
2034 * @oh: struct omap_hwmod *
2035 *
2036 * Some IP blocks (such as AESS) require some additional programming
2037 * after enable before they can enter idle. If a function pointer to
2038 * do so is present in the hwmod data, then call it and pass along the
2039 * return value; otherwise, return 0.
2040 */
0f497039 2041static int _enable_preprogram(struct omap_hwmod *oh)
6d266f63
PW
2042{
2043 if (!oh->class->enable_preprogram)
2044 return 0;
2045
2046 return oh->class->enable_preprogram(oh);
2047}
2048
63c85238 2049/**
dc6d1cda 2050 * _enable - enable an omap_hwmod
63c85238
PW
2051 * @oh: struct omap_hwmod *
2052 *
2053 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
dc6d1cda
PW
2054 * register target. Returns -EINVAL if the hwmod is in the wrong
2055 * state or passes along the return value of _wait_target_ready().
63c85238 2056 */
dc6d1cda 2057static int _enable(struct omap_hwmod *oh)
63c85238 2058{
747834ab 2059 int r;
665d0013 2060 int hwsup = 0;
63c85238 2061
34617e2a
BC
2062 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
2063
aacf0941 2064 /*
64813c3f
PW
2065 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
2066 * state at init. Now that someone is really trying to enable
2067 * them, just ensure that the hwmod mux is set.
aacf0941
RN
2068 */
2069 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
2070 /*
2071 * If the caller has mux data populated, do the mux'ing
2072 * which wouldn't have been done as part of the _enable()
2073 * done during setup.
2074 */
2075 if (oh->mux)
2076 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
2077
2078 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
2079 return 0;
2080 }
2081
63c85238
PW
2082 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
2083 oh->_state != _HWMOD_STATE_IDLE &&
2084 oh->_state != _HWMOD_STATE_DISABLED) {
4f8a428d
RK
2085 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
2086 oh->name);
63c85238
PW
2087 return -EINVAL;
2088 }
2089
31f62866 2090 /*
eb05f691 2091 * If an IP block contains HW reset lines and all of them are
747834ab
PW
2092 * asserted, we let integration code associated with that
2093 * block handle the enable. We've received very little
2094 * information on what those driver authors need, and until
2095 * detailed information is provided and the driver code is
2096 * posted to the public lists, this is probably the best we
2097 * can do.
31f62866 2098 */
eb05f691 2099 if (_are_all_hardreset_lines_asserted(oh))
747834ab 2100 return 0;
63c85238 2101
665d0013
RN
2102 /* Mux pins for device runtime if populated */
2103 if (oh->mux && (!oh->mux->enabled ||
2104 ((oh->_state == _HWMOD_STATE_IDLE) &&
5165882a 2105 oh->mux->pads_dynamic))) {
665d0013 2106 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
5165882a 2107 _reconfigure_io_chain();
6a08b11a 2108 } else if (oh->flags & HWMOD_RECONFIG_IO_CHAIN) {
cc824534 2109 _reconfigure_io_chain();
5165882a 2110 }
665d0013
RN
2111
2112 _add_initiator_dep(oh, mpu_oh);
34617e2a 2113
665d0013
RN
2114 if (oh->clkdm) {
2115 /*
2116 * A clockdomain must be in SW_SUP before enabling
2117 * completely the module. The clockdomain can be set
2118 * in HW_AUTO only when the module become ready.
2119 */
b71c7217
PW
2120 hwsup = clkdm_in_hwsup(oh->clkdm) &&
2121 !clkdm_missing_idle_reporting(oh->clkdm);
665d0013
RN
2122 r = clkdm_hwmod_enable(oh->clkdm, oh);
2123 if (r) {
2124 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
2125 oh->name, oh->clkdm->name, r);
2126 return r;
2127 }
34617e2a 2128 }
665d0013
RN
2129
2130 _enable_clocks(oh);
9ebfd285
KH
2131 if (soc_ops.enable_module)
2132 soc_ops.enable_module(oh);
fa200222 2133 if (oh->flags & HWMOD_BLOCK_WFI)
f7b861b7 2134 cpu_idle_poll_ctrl(true);
34617e2a 2135
e6d3a8b0
RN
2136 if (soc_ops.update_context_lost)
2137 soc_ops.update_context_lost(oh);
2138
8f6aa8ee
KH
2139 r = (soc_ops.wait_target_ready) ? soc_ops.wait_target_ready(oh) :
2140 -EINVAL;
665d0013
RN
2141 if (!r) {
2142 /*
2143 * Set the clockdomain to HW_AUTO only if the target is ready,
2144 * assuming that the previous state was HW_AUTO
2145 */
2146 if (oh->clkdm && hwsup)
2147 clkdm_allow_idle(oh->clkdm);
2148
2149 oh->_state = _HWMOD_STATE_ENABLED;
2150
2151 /* Access the sysconfig only if the target is ready */
2152 if (oh->class->sysc) {
2153 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
2154 _update_sysc_cache(oh);
2155 _enable_sysc(oh);
2156 }
6d266f63 2157 r = _enable_preprogram(oh);
665d0013 2158 } else {
2577a4a6
PW
2159 if (soc_ops.disable_module)
2160 soc_ops.disable_module(oh);
665d0013
RN
2161 _disable_clocks(oh);
2162 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
2163 oh->name, r);
34617e2a 2164
665d0013
RN
2165 if (oh->clkdm)
2166 clkdm_hwmod_disable(oh->clkdm, oh);
9a23dfe1
BC
2167 }
2168
63c85238
PW
2169 return r;
2170}
2171
2172/**
dc6d1cda 2173 * _idle - idle an omap_hwmod
63c85238
PW
2174 * @oh: struct omap_hwmod *
2175 *
2176 * Idles an omap_hwmod @oh. This should be called once the hwmod has
dc6d1cda
PW
2177 * no further work. Returns -EINVAL if the hwmod is in the wrong
2178 * state or returns 0.
63c85238 2179 */
dc6d1cda 2180static int _idle(struct omap_hwmod *oh)
63c85238 2181{
34617e2a
BC
2182 pr_debug("omap_hwmod: %s: idling\n", oh->name);
2183
63c85238 2184 if (oh->_state != _HWMOD_STATE_ENABLED) {
4f8a428d
RK
2185 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
2186 oh->name);
63c85238
PW
2187 return -EINVAL;
2188 }
2189
eb05f691 2190 if (_are_all_hardreset_lines_asserted(oh))
747834ab
PW
2191 return 0;
2192
43b40992 2193 if (oh->class->sysc)
74ff3a68 2194 _idle_sysc(oh);
63c85238 2195 _del_initiator_dep(oh, mpu_oh);
bfc141e3 2196
fa200222 2197 if (oh->flags & HWMOD_BLOCK_WFI)
f7b861b7 2198 cpu_idle_poll_ctrl(false);
9ebfd285
KH
2199 if (soc_ops.disable_module)
2200 soc_ops.disable_module(oh);
bfc141e3 2201
45c38252
BC
2202 /*
2203 * The module must be in idle mode before disabling any parents
2204 * clocks. Otherwise, the parent clock might be disabled before
2205 * the module transition is done, and thus will prevent the
2206 * transition to complete properly.
2207 */
2208 _disable_clocks(oh);
665d0013
RN
2209 if (oh->clkdm)
2210 clkdm_hwmod_disable(oh->clkdm, oh);
63c85238 2211
8d9af88f 2212 /* Mux pins for device idle if populated */
5165882a 2213 if (oh->mux && oh->mux->pads_dynamic) {
8d9af88f 2214 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
5165882a 2215 _reconfigure_io_chain();
6a08b11a 2216 } else if (oh->flags & HWMOD_RECONFIG_IO_CHAIN) {
cc824534 2217 _reconfigure_io_chain();
5165882a 2218 }
8d9af88f 2219
63c85238
PW
2220 oh->_state = _HWMOD_STATE_IDLE;
2221
2222 return 0;
2223}
2224
2225/**
2226 * _shutdown - shutdown an omap_hwmod
2227 * @oh: struct omap_hwmod *
2228 *
2229 * Shut down an omap_hwmod @oh. This should be called when the driver
2230 * used for the hwmod is removed or unloaded or if the driver is not
2231 * used by the system. Returns -EINVAL if the hwmod is in the wrong
2232 * state or returns 0.
2233 */
2234static int _shutdown(struct omap_hwmod *oh)
2235{
9c8b0ec7 2236 int ret, i;
e4dc8f50
PW
2237 u8 prev_state;
2238
63c85238
PW
2239 if (oh->_state != _HWMOD_STATE_IDLE &&
2240 oh->_state != _HWMOD_STATE_ENABLED) {
4f8a428d
RK
2241 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
2242 oh->name);
63c85238
PW
2243 return -EINVAL;
2244 }
2245
eb05f691 2246 if (_are_all_hardreset_lines_asserted(oh))
747834ab
PW
2247 return 0;
2248
63c85238
PW
2249 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
2250
e4dc8f50
PW
2251 if (oh->class->pre_shutdown) {
2252 prev_state = oh->_state;
2253 if (oh->_state == _HWMOD_STATE_IDLE)
dc6d1cda 2254 _enable(oh);
e4dc8f50
PW
2255 ret = oh->class->pre_shutdown(oh);
2256 if (ret) {
2257 if (prev_state == _HWMOD_STATE_IDLE)
dc6d1cda 2258 _idle(oh);
e4dc8f50
PW
2259 return ret;
2260 }
2261 }
2262
6481c73c
MV
2263 if (oh->class->sysc) {
2264 if (oh->_state == _HWMOD_STATE_IDLE)
2265 _enable(oh);
74ff3a68 2266 _shutdown_sysc(oh);
6481c73c 2267 }
5365efbe 2268
3827f949
BC
2269 /* clocks and deps are already disabled in idle */
2270 if (oh->_state == _HWMOD_STATE_ENABLED) {
2271 _del_initiator_dep(oh, mpu_oh);
2272 /* XXX what about the other system initiators here? dma, dsp */
fa200222 2273 if (oh->flags & HWMOD_BLOCK_WFI)
f7b861b7 2274 cpu_idle_poll_ctrl(false);
9ebfd285
KH
2275 if (soc_ops.disable_module)
2276 soc_ops.disable_module(oh);
45c38252 2277 _disable_clocks(oh);
665d0013
RN
2278 if (oh->clkdm)
2279 clkdm_hwmod_disable(oh->clkdm, oh);
3827f949 2280 }
63c85238
PW
2281 /* XXX Should this code also force-disable the optional clocks? */
2282
9c8b0ec7
PW
2283 for (i = 0; i < oh->rst_lines_cnt; i++)
2284 _assert_hardreset(oh, oh->rst_lines[i].name);
31f62866 2285
8d9af88f
TL
2286 /* Mux pins to safe mode or use populated off mode values */
2287 if (oh->mux)
2288 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
63c85238
PW
2289
2290 oh->_state = _HWMOD_STATE_DISABLED;
2291
2292 return 0;
2293}
2294
5e863c56
TL
2295static int of_dev_find_hwmod(struct device_node *np,
2296 struct omap_hwmod *oh)
2297{
2298 int count, i, res;
2299 const char *p;
2300
2301 count = of_property_count_strings(np, "ti,hwmods");
2302 if (count < 1)
2303 return -ENODEV;
2304
2305 for (i = 0; i < count; i++) {
2306 res = of_property_read_string_index(np, "ti,hwmods",
2307 i, &p);
2308 if (res)
2309 continue;
2310 if (!strcmp(p, oh->name)) {
2311 pr_debug("omap_hwmod: dt %s[%i] uses hwmod %s\n",
2312 np->name, i, oh->name);
2313 return i;
2314 }
2315 }
2316
2317 return -ENODEV;
2318}
2319
079abade
SS
2320/**
2321 * of_dev_hwmod_lookup - look up needed hwmod from dt blob
2322 * @np: struct device_node *
2323 * @oh: struct omap_hwmod *
5e863c56
TL
2324 * @index: index of the entry found
2325 * @found: struct device_node * found or NULL
079abade
SS
2326 *
2327 * Parse the dt blob and find out needed hwmod. Recursive function is
2328 * implemented to take care hierarchical dt blob parsing.
5e863c56 2329 * Return: Returns 0 on success, -ENODEV when not found.
079abade 2330 */
5e863c56
TL
2331static int of_dev_hwmod_lookup(struct device_node *np,
2332 struct omap_hwmod *oh,
2333 int *index,
2334 struct device_node **found)
079abade 2335{
5e863c56
TL
2336 struct device_node *np0 = NULL;
2337 int res;
2338
2339 res = of_dev_find_hwmod(np, oh);
2340 if (res >= 0) {
2341 *found = np;
2342 *index = res;
2343 return 0;
2344 }
079abade
SS
2345
2346 for_each_child_of_node(np, np0) {
5e863c56
TL
2347 struct device_node *fc;
2348 int i;
2349
2350 res = of_dev_hwmod_lookup(np0, oh, &i, &fc);
2351 if (res == 0) {
2352 *found = fc;
2353 *index = i;
2354 return 0;
079abade
SS
2355 }
2356 }
5e863c56
TL
2357
2358 *found = NULL;
2359 *index = 0;
2360
2361 return -ENODEV;
079abade
SS
2362}
2363
381d033a
PW
2364/**
2365 * _init_mpu_rt_base - populate the virtual address for a hwmod
2366 * @oh: struct omap_hwmod * to locate the virtual address
f92d9597 2367 * @data: (unused, caller should pass NULL)
5e863c56 2368 * @index: index of the reg entry iospace in device tree
f92d9597 2369 * @np: struct device_node * of the IP block's device node in the DT data
381d033a
PW
2370 *
2371 * Cache the virtual address used by the MPU to access this IP block's
2372 * registers. This address is needed early so the OCP registers that
2373 * are part of the device's address space can be ioremapped properly.
6423d6df
SA
2374 *
2375 * Returns 0 on success, -EINVAL if an invalid hwmod is passed, and
2376 * -ENXIO on absent or invalid register target address space.
381d033a 2377 */
f92d9597 2378static int __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data,
5e863c56 2379 int index, struct device_node *np)
381d033a 2380{
c9aafd23 2381 struct omap_hwmod_addr_space *mem;
079abade 2382 void __iomem *va_start = NULL;
c9aafd23
PW
2383
2384 if (!oh)
6423d6df 2385 return -EINVAL;
c9aafd23 2386
2221b5cd
PW
2387 _save_mpu_port_index(oh);
2388
381d033a 2389 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
6423d6df 2390 return -ENXIO;
381d033a 2391
c9aafd23
PW
2392 mem = _find_mpu_rt_addr_space(oh);
2393 if (!mem) {
2394 pr_debug("omap_hwmod: %s: no MPU register target found\n",
2395 oh->name);
079abade
SS
2396
2397 /* Extract the IO space from device tree blob */
f92d9597 2398 if (!np)
6423d6df 2399 return -ENXIO;
079abade 2400
5e863c56 2401 va_start = of_iomap(np, index + oh->mpu_rt_idx);
079abade
SS
2402 } else {
2403 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
c9aafd23
PW
2404 }
2405
c9aafd23 2406 if (!va_start) {
5e863c56
TL
2407 if (mem)
2408 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
2409 else
2410 pr_err("omap_hwmod: %s: Missing dt reg%i for %s\n",
2411 oh->name, index, np->full_name);
6423d6df 2412 return -ENXIO;
c9aafd23
PW
2413 }
2414
2415 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
2416 oh->name, va_start);
2417
2418 oh->_mpu_rt_va = va_start;
6423d6df 2419 return 0;
381d033a
PW
2420}
2421
2422/**
2423 * _init - initialize internal data for the hwmod @oh
2424 * @oh: struct omap_hwmod *
2425 * @n: (unused)
2426 *
2427 * Look up the clocks and the address space used by the MPU to access
2428 * registers belonging to the hwmod @oh. @oh must already be
2429 * registered at this point. This is the first of two phases for
2430 * hwmod initialization. Code called here does not touch any hardware
2431 * registers, it simply prepares internal data structures. Returns 0
6423d6df
SA
2432 * upon success or if the hwmod isn't registered or if the hwmod's
2433 * address space is not defined, or -EINVAL upon failure.
381d033a
PW
2434 */
2435static int __init _init(struct omap_hwmod *oh, void *data)
2436{
5e863c56 2437 int r, index;
f92d9597 2438 struct device_node *np = NULL;
381d033a
PW
2439
2440 if (oh->_state != _HWMOD_STATE_REGISTERED)
2441 return 0;
2442
5e863c56
TL
2443 if (of_have_populated_dt()) {
2444 struct device_node *bus;
2445
2446 bus = of_find_node_by_name(NULL, "ocp");
2447 if (!bus)
2448 return -ENODEV;
2449
2450 r = of_dev_hwmod_lookup(bus, oh, &index, &np);
2451 if (r)
2452 pr_debug("omap_hwmod: %s missing dt data\n", oh->name);
2453 else if (np && index)
2454 pr_warn("omap_hwmod: %s using broken dt data from %s\n",
2455 oh->name, np->name);
2456 }
f92d9597 2457
6423d6df 2458 if (oh->class->sysc) {
5e863c56 2459 r = _init_mpu_rt_base(oh, NULL, index, np);
6423d6df
SA
2460 if (r < 0) {
2461 WARN(1, "omap_hwmod: %s: doesn't have mpu register target base\n",
2462 oh->name);
2463 return 0;
2464 }
2465 }
381d033a
PW
2466
2467 r = _init_clocks(oh, NULL);
c48cd659 2468 if (r < 0) {
381d033a
PW
2469 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2470 return -EINVAL;
2471 }
2472
3d36ad7e 2473 if (np) {
f92d9597
RN
2474 if (of_find_property(np, "ti,no-reset-on-init", NULL))
2475 oh->flags |= HWMOD_INIT_NO_RESET;
2476 if (of_find_property(np, "ti,no-idle-on-init", NULL))
2477 oh->flags |= HWMOD_INIT_NO_IDLE;
3d36ad7e 2478 }
f92d9597 2479
381d033a
PW
2480 oh->_state = _HWMOD_STATE_INITIALIZED;
2481
2482 return 0;
2483}
2484
63c85238 2485/**
64813c3f 2486 * _setup_iclk_autoidle - configure an IP block's interface clocks
63c85238
PW
2487 * @oh: struct omap_hwmod *
2488 *
64813c3f
PW
2489 * Set up the module's interface clocks. XXX This function is still mostly
2490 * a stub; implementing this properly requires iclk autoidle usecounting in
2491 * the clock code. No return value.
63c85238 2492 */
64813c3f 2493static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
63c85238 2494{
5d95dde7 2495 struct omap_hwmod_ocp_if *os;
11cd4b94 2496 struct list_head *p;
5d95dde7 2497 int i = 0;
381d033a 2498 if (oh->_state != _HWMOD_STATE_INITIALIZED)
64813c3f 2499 return;
48d54f3f 2500
11cd4b94 2501 p = oh->slave_ports.next;
63c85238 2502
5d95dde7 2503 while (i < oh->slaves_cnt) {
11cd4b94 2504 os = _fetch_next_ocp_if(&p, &i);
5d95dde7 2505 if (!os->_clk)
64813c3f 2506 continue;
63c85238 2507
64813c3f
PW
2508 if (os->flags & OCPIF_SWSUP_IDLE) {
2509 /* XXX omap_iclk_deny_idle(c); */
2510 } else {
2511 /* XXX omap_iclk_allow_idle(c); */
5d95dde7 2512 clk_enable(os->_clk);
63c85238
PW
2513 }
2514 }
2515
64813c3f
PW
2516 return;
2517}
2518
2519/**
2520 * _setup_reset - reset an IP block during the setup process
2521 * @oh: struct omap_hwmod *
2522 *
2523 * Reset the IP block corresponding to the hwmod @oh during the setup
2524 * process. The IP block is first enabled so it can be successfully
2525 * reset. Returns 0 upon success or a negative error code upon
2526 * failure.
2527 */
2528static int __init _setup_reset(struct omap_hwmod *oh)
2529{
2530 int r;
2531
2532 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2533 return -EINVAL;
63c85238 2534
5fb3d522
PW
2535 if (oh->flags & HWMOD_EXT_OPT_MAIN_CLK)
2536 return -EPERM;
2537
747834ab
PW
2538 if (oh->rst_lines_cnt == 0) {
2539 r = _enable(oh);
2540 if (r) {
3d0cb73e
JP
2541 pr_warn("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2542 oh->name, oh->_state);
747834ab
PW
2543 return -EINVAL;
2544 }
9a23dfe1 2545 }
63c85238 2546
2800852a 2547 if (!(oh->flags & HWMOD_INIT_NO_RESET))
64813c3f
PW
2548 r = _reset(oh);
2549
2550 return r;
2551}
2552
2553/**
2554 * _setup_postsetup - transition to the appropriate state after _setup
2555 * @oh: struct omap_hwmod *
2556 *
2557 * Place an IP block represented by @oh into a "post-setup" state --
2558 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2559 * this function is called at the end of _setup().) The postsetup
2560 * state for an IP block can be changed by calling
2561 * omap_hwmod_enter_postsetup_state() early in the boot process,
2562 * before one of the omap_hwmod_setup*() functions are called for the
2563 * IP block.
2564 *
2565 * The IP block stays in this state until a PM runtime-based driver is
2566 * loaded for that IP block. A post-setup state of IDLE is
2567 * appropriate for almost all IP blocks with runtime PM-enabled
2568 * drivers, since those drivers are able to enable the IP block. A
2569 * post-setup state of ENABLED is appropriate for kernels with PM
2570 * runtime disabled. The DISABLED state is appropriate for unusual IP
2571 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2572 * included, since the WDTIMER starts running on reset and will reset
2573 * the MPU if left active.
2574 *
2575 * This post-setup mechanism is deprecated. Once all of the OMAP
2576 * drivers have been converted to use PM runtime, and all of the IP
2577 * block data and interconnect data is available to the hwmod code, it
2578 * should be possible to replace this mechanism with a "lazy reset"
2579 * arrangement. In a "lazy reset" setup, each IP block is enabled
2580 * when the driver first probes, then all remaining IP blocks without
2581 * drivers are either shut down or enabled after the drivers have
2582 * loaded. However, this cannot take place until the above
2583 * preconditions have been met, since otherwise the late reset code
2584 * has no way of knowing which IP blocks are in use by drivers, and
2585 * which ones are unused.
2586 *
2587 * No return value.
2588 */
2589static void __init _setup_postsetup(struct omap_hwmod *oh)
2590{
2591 u8 postsetup_state;
2592
2593 if (oh->rst_lines_cnt > 0)
2594 return;
76e5589e 2595
2092e5cc
PW
2596 postsetup_state = oh->_postsetup_state;
2597 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2598 postsetup_state = _HWMOD_STATE_ENABLED;
2599
2600 /*
2601 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2602 * it should be set by the core code as a runtime flag during startup
2603 */
2604 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
aacf0941
RN
2605 (postsetup_state == _HWMOD_STATE_IDLE)) {
2606 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
2092e5cc 2607 postsetup_state = _HWMOD_STATE_ENABLED;
aacf0941 2608 }
2092e5cc
PW
2609
2610 if (postsetup_state == _HWMOD_STATE_IDLE)
dc6d1cda 2611 _idle(oh);
2092e5cc
PW
2612 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2613 _shutdown(oh);
2614 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2615 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2616 oh->name, postsetup_state);
63c85238 2617
64813c3f
PW
2618 return;
2619}
2620
2621/**
2622 * _setup - prepare IP block hardware for use
2623 * @oh: struct omap_hwmod *
2624 * @n: (unused, pass NULL)
2625 *
2626 * Configure the IP block represented by @oh. This may include
2627 * enabling the IP block, resetting it, and placing it into a
2628 * post-setup state, depending on the type of IP block and applicable
2629 * flags. IP blocks are reset to prevent any previous configuration
2630 * by the bootloader or previous operating system from interfering
2631 * with power management or other parts of the system. The reset can
2632 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2633 * two phases for hwmod initialization. Code called here generally
2634 * affects the IP block hardware, or system integration hardware
2635 * associated with the IP block. Returns 0.
2636 */
2637static int __init _setup(struct omap_hwmod *oh, void *data)
2638{
2639 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2640 return 0;
2641
2642 _setup_iclk_autoidle(oh);
2643
2644 if (!_setup_reset(oh))
2645 _setup_postsetup(oh);
2646
63c85238
PW
2647 return 0;
2648}
2649
63c85238 2650/**
0102b627 2651 * _register - register a struct omap_hwmod
63c85238
PW
2652 * @oh: struct omap_hwmod *
2653 *
43b40992
PW
2654 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2655 * already has been registered by the same name; -EINVAL if the
2656 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2657 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2658 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2659 * success.
63c85238
PW
2660 *
2661 * XXX The data should be copied into bootmem, so the original data
2662 * should be marked __initdata and freed after init. This would allow
2663 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2664 * that the copy process would be relatively complex due to the large number
2665 * of substructures.
2666 */
01592df9 2667static int __init _register(struct omap_hwmod *oh)
63c85238 2668{
43b40992
PW
2669 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2670 (oh->_state != _HWMOD_STATE_UNKNOWN))
63c85238
PW
2671 return -EINVAL;
2672
63c85238
PW
2673 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2674
ce35b244
BC
2675 if (_lookup(oh->name))
2676 return -EEXIST;
63c85238 2677
63c85238
PW
2678 list_add_tail(&oh->node, &omap_hwmod_list);
2679
2221b5cd
PW
2680 INIT_LIST_HEAD(&oh->master_ports);
2681 INIT_LIST_HEAD(&oh->slave_ports);
dc6d1cda 2682 spin_lock_init(&oh->_lock);
2092e5cc 2683
63c85238
PW
2684 oh->_state = _HWMOD_STATE_REGISTERED;
2685
569edd70
PW
2686 /*
2687 * XXX Rather than doing a strcmp(), this should test a flag
2688 * set in the hwmod data, inserted by the autogenerator code.
2689 */
2690 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2691 mpu_oh = oh;
63c85238 2692
569edd70 2693 return 0;
63c85238
PW
2694}
2695
2221b5cd
PW
2696/**
2697 * _alloc_links - return allocated memory for hwmod links
2698 * @ml: pointer to a struct omap_hwmod_link * for the master link
2699 * @sl: pointer to a struct omap_hwmod_link * for the slave link
2700 *
2701 * Return pointers to two struct omap_hwmod_link records, via the
2702 * addresses pointed to by @ml and @sl. Will first attempt to return
2703 * memory allocated as part of a large initial block, but if that has
2704 * been exhausted, will allocate memory itself. Since ideally this
2705 * second allocation path will never occur, the number of these
2706 * 'supplemental' allocations will be logged when debugging is
2707 * enabled. Returns 0.
2708 */
2709static int __init _alloc_links(struct omap_hwmod_link **ml,
2710 struct omap_hwmod_link **sl)
2711{
2712 unsigned int sz;
2713
2714 if ((free_ls + LINKS_PER_OCP_IF) <= max_ls) {
2715 *ml = &linkspace[free_ls++];
2716 *sl = &linkspace[free_ls++];
2717 return 0;
2718 }
2719
2720 sz = sizeof(struct omap_hwmod_link) * LINKS_PER_OCP_IF;
2721
2722 *sl = NULL;
b6cb5bab 2723 *ml = memblock_virt_alloc(sz, 0);
2221b5cd
PW
2724
2725 *sl = (void *)(*ml) + sizeof(struct omap_hwmod_link);
2726
2727 ls_supp++;
2728 pr_debug("omap_hwmod: supplemental link allocations needed: %d\n",
2729 ls_supp * LINKS_PER_OCP_IF);
2730
2731 return 0;
2732};
2733
2734/**
2735 * _add_link - add an interconnect between two IP blocks
2736 * @oi: pointer to a struct omap_hwmod_ocp_if record
2737 *
2738 * Add struct omap_hwmod_link records connecting the master IP block
2739 * specified in @oi->master to @oi, and connecting the slave IP block
2740 * specified in @oi->slave to @oi. This code is assumed to run before
2741 * preemption or SMP has been enabled, thus avoiding the need for
2742 * locking in this code. Changes to this assumption will require
2743 * additional locking. Returns 0.
2744 */
2745static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2746{
2747 struct omap_hwmod_link *ml, *sl;
2748
2749 pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2750 oi->slave->name);
2751
2752 _alloc_links(&ml, &sl);
2753
2754 ml->ocp_if = oi;
2755 INIT_LIST_HEAD(&ml->node);
2756 list_add(&ml->node, &oi->master->master_ports);
2757 oi->master->masters_cnt++;
2758
2759 sl->ocp_if = oi;
2760 INIT_LIST_HEAD(&sl->node);
2761 list_add(&sl->node, &oi->slave->slave_ports);
2762 oi->slave->slaves_cnt++;
2763
2764 return 0;
2765}
2766
2767/**
2768 * _register_link - register a struct omap_hwmod_ocp_if
2769 * @oi: struct omap_hwmod_ocp_if *
2770 *
2771 * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
2772 * has already been registered; -EINVAL if @oi is NULL or if the
2773 * record pointed to by @oi is missing required fields; or 0 upon
2774 * success.
2775 *
2776 * XXX The data should be copied into bootmem, so the original data
2777 * should be marked __initdata and freed after init. This would allow
2778 * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2779 */
2780static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2781{
2782 if (!oi || !oi->master || !oi->slave || !oi->user)
2783 return -EINVAL;
2784
2785 if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2786 return -EEXIST;
2787
2788 pr_debug("omap_hwmod: registering link from %s to %s\n",
2789 oi->master->name, oi->slave->name);
2790
2791 /*
2792 * Register the connected hwmods, if they haven't been
2793 * registered already
2794 */
2795 if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2796 _register(oi->master);
2797
2798 if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2799 _register(oi->slave);
2800
2801 _add_link(oi);
2802
2803 oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2804
2805 return 0;
2806}
2807
2808/**
2809 * _alloc_linkspace - allocate large block of hwmod links
2810 * @ois: pointer to an array of struct omap_hwmod_ocp_if records to count
2811 *
2812 * Allocate a large block of struct omap_hwmod_link records. This
2813 * improves boot time significantly by avoiding the need to allocate
2814 * individual records one by one. If the number of records to
2815 * allocate in the block hasn't been manually specified, this function
2816 * will count the number of struct omap_hwmod_ocp_if records in @ois
2817 * and use that to determine the allocation size. For SoC families
2818 * that require multiple list registrations, such as OMAP3xxx, this
2819 * estimation process isn't optimal, so manual estimation is advised
2820 * in those cases. Returns -EEXIST if the allocation has already occurred
2821 * or 0 upon success.
2822 */
2823static int __init _alloc_linkspace(struct omap_hwmod_ocp_if **ois)
2824{
2825 unsigned int i = 0;
2826 unsigned int sz;
2827
2828 if (linkspace) {
2829 WARN(1, "linkspace already allocated\n");
2830 return -EEXIST;
2831 }
2832
2833 if (max_ls == 0)
2834 while (ois[i++])
2835 max_ls += LINKS_PER_OCP_IF;
2836
2837 sz = sizeof(struct omap_hwmod_link) * max_ls;
2838
2839 pr_debug("omap_hwmod: %s: allocating %d byte linkspace (%d links)\n",
2840 __func__, sz, max_ls);
2841
b6cb5bab 2842 linkspace = memblock_virt_alloc(sz, 0);
2221b5cd
PW
2843
2844 return 0;
2845}
0102b627 2846
8f6aa8ee
KH
2847/* Static functions intended only for use in soc_ops field function pointers */
2848
2849/**
9002e921 2850 * _omap2xxx_3xxx_wait_target_ready - wait for a module to leave slave idle
8f6aa8ee
KH
2851 * @oh: struct omap_hwmod *
2852 *
2853 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2854 * does not have an IDLEST bit or if the module successfully leaves
2855 * slave idle; otherwise, pass along the return value of the
2856 * appropriate *_cm*_wait_module_ready() function.
2857 */
9002e921 2858static int _omap2xxx_3xxx_wait_target_ready(struct omap_hwmod *oh)
8f6aa8ee
KH
2859{
2860 if (!oh)
2861 return -EINVAL;
2862
2863 if (oh->flags & HWMOD_NO_IDLEST)
2864 return 0;
2865
2866 if (!_find_mpu_rt_port(oh))
2867 return 0;
2868
2869 /* XXX check module SIDLEMODE, hardreset status, enabled clocks */
2870
021b6ff0
TK
2871 return omap_cm_wait_module_ready(0, oh->prcm.omap2.module_offs,
2872 oh->prcm.omap2.idlest_reg_id,
2873 oh->prcm.omap2.idlest_idle_bit);
ff4ae5d9
PW
2874}
2875
8f6aa8ee
KH
2876/**
2877 * _omap4_wait_target_ready - wait for a module to leave slave idle
2878 * @oh: struct omap_hwmod *
2879 *
2880 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2881 * does not have an IDLEST bit or if the module successfully leaves
2882 * slave idle; otherwise, pass along the return value of the
2883 * appropriate *_cm*_wait_module_ready() function.
2884 */
2885static int _omap4_wait_target_ready(struct omap_hwmod *oh)
2886{
2b026d13 2887 if (!oh)
8f6aa8ee
KH
2888 return -EINVAL;
2889
2b026d13 2890 if (oh->flags & HWMOD_NO_IDLEST || !oh->clkdm)
8f6aa8ee
KH
2891 return 0;
2892
2893 if (!_find_mpu_rt_port(oh))
2894 return 0;
2895
2896 /* XXX check module SIDLEMODE, hardreset status */
2897
021b6ff0
TK
2898 return omap_cm_wait_module_ready(oh->clkdm->prcm_partition,
2899 oh->clkdm->cm_inst,
2900 oh->prcm.omap4.clkctrl_offs, 0);
8f6aa8ee
KH
2901}
2902
b8249cf2
KH
2903/**
2904 * _omap2_assert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2905 * @oh: struct omap_hwmod * to assert hardreset
2906 * @ohri: hardreset line data
2907 *
2908 * Call omap2_prm_assert_hardreset() with parameters extracted from
2909 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2910 * use as an soc_ops function pointer. Passes along the return value
2911 * from omap2_prm_assert_hardreset(). XXX This function is scheduled
2912 * for removal when the PRM code is moved into drivers/.
2913 */
2914static int _omap2_assert_hardreset(struct omap_hwmod *oh,
2915 struct omap_hwmod_rst_info *ohri)
2916{
efd44dc3
TK
2917 return omap_prm_assert_hardreset(ohri->rst_shift, 0,
2918 oh->prcm.omap2.module_offs, 0);
b8249cf2
KH
2919}
2920
2921/**
2922 * _omap2_deassert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2923 * @oh: struct omap_hwmod * to deassert hardreset
2924 * @ohri: hardreset line data
2925 *
2926 * Call omap2_prm_deassert_hardreset() with parameters extracted from
2927 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2928 * use as an soc_ops function pointer. Passes along the return value
2929 * from omap2_prm_deassert_hardreset(). XXX This function is
2930 * scheduled for removal when the PRM code is moved into drivers/.
2931 */
2932static int _omap2_deassert_hardreset(struct omap_hwmod *oh,
2933 struct omap_hwmod_rst_info *ohri)
2934{
37fb59d7
TK
2935 return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->st_shift, 0,
2936 oh->prcm.omap2.module_offs, 0, 0);
b8249cf2
KH
2937}
2938
2939/**
2940 * _omap2_is_hardreset_asserted - call OMAP2 PRM hardreset fn with hwmod args
2941 * @oh: struct omap_hwmod * to test hardreset
2942 * @ohri: hardreset line data
2943 *
2944 * Call omap2_prm_is_hardreset_asserted() with parameters extracted
2945 * from the hwmod @oh and the hardreset line data @ohri. Only
2946 * intended for use as an soc_ops function pointer. Passes along the
2947 * return value from omap2_prm_is_hardreset_asserted(). XXX This
2948 * function is scheduled for removal when the PRM code is moved into
2949 * drivers/.
2950 */
2951static int _omap2_is_hardreset_asserted(struct omap_hwmod *oh,
2952 struct omap_hwmod_rst_info *ohri)
2953{
2954 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
2955 ohri->st_shift);
2956}
2957
2958/**
2959 * _omap4_assert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2960 * @oh: struct omap_hwmod * to assert hardreset
2961 * @ohri: hardreset line data
2962 *
2963 * Call omap4_prminst_assert_hardreset() with parameters extracted
2964 * from the hwmod @oh and the hardreset line data @ohri. Only
2965 * intended for use as an soc_ops function pointer. Passes along the
2966 * return value from omap4_prminst_assert_hardreset(). XXX This
2967 * function is scheduled for removal when the PRM code is moved into
2968 * drivers/.
2969 */
2970static int _omap4_assert_hardreset(struct omap_hwmod *oh,
2971 struct omap_hwmod_rst_info *ohri)
b8249cf2 2972{
07b3a139
PW
2973 if (!oh->clkdm)
2974 return -EINVAL;
2975
efd44dc3
TK
2976 return omap_prm_assert_hardreset(ohri->rst_shift,
2977 oh->clkdm->pwrdm.ptr->prcm_partition,
2978 oh->clkdm->pwrdm.ptr->prcm_offs,
2979 oh->prcm.omap4.rstctrl_offs);
b8249cf2
KH
2980}
2981
2982/**
2983 * _omap4_deassert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2984 * @oh: struct omap_hwmod * to deassert hardreset
2985 * @ohri: hardreset line data
2986 *
2987 * Call omap4_prminst_deassert_hardreset() with parameters extracted
2988 * from the hwmod @oh and the hardreset line data @ohri. Only
2989 * intended for use as an soc_ops function pointer. Passes along the
2990 * return value from omap4_prminst_deassert_hardreset(). XXX This
2991 * function is scheduled for removal when the PRM code is moved into
2992 * drivers/.
2993 */
2994static int _omap4_deassert_hardreset(struct omap_hwmod *oh,
2995 struct omap_hwmod_rst_info *ohri)
2996{
07b3a139
PW
2997 if (!oh->clkdm)
2998 return -EINVAL;
2999
b8249cf2
KH
3000 if (ohri->st_shift)
3001 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
3002 oh->name, ohri->name);
37fb59d7
TK
3003 return omap_prm_deassert_hardreset(ohri->rst_shift, 0,
3004 oh->clkdm->pwrdm.ptr->prcm_partition,
3005 oh->clkdm->pwrdm.ptr->prcm_offs,
3006 oh->prcm.omap4.rstctrl_offs, 0);
b8249cf2
KH
3007}
3008
3009/**
3010 * _omap4_is_hardreset_asserted - call OMAP4 PRM hardreset fn with hwmod args
3011 * @oh: struct omap_hwmod * to test hardreset
3012 * @ohri: hardreset line data
3013 *
3014 * Call omap4_prminst_is_hardreset_asserted() with parameters
3015 * extracted from the hwmod @oh and the hardreset line data @ohri.
3016 * Only intended for use as an soc_ops function pointer. Passes along
3017 * the return value from omap4_prminst_is_hardreset_asserted(). XXX
3018 * This function is scheduled for removal when the PRM code is moved
3019 * into drivers/.
3020 */
3021static int _omap4_is_hardreset_asserted(struct omap_hwmod *oh,
3022 struct omap_hwmod_rst_info *ohri)
3023{
07b3a139
PW
3024 if (!oh->clkdm)
3025 return -EINVAL;
3026
b8249cf2
KH
3027 return omap4_prminst_is_hardreset_asserted(ohri->rst_shift,
3028 oh->clkdm->pwrdm.ptr->prcm_partition,
3029 oh->clkdm->pwrdm.ptr->prcm_offs,
3030 oh->prcm.omap4.rstctrl_offs);
3031}
3032
1688bf19
VH
3033/**
3034 * _am33xx_assert_hardreset - call AM33XX PRM hardreset fn with hwmod args
3035 * @oh: struct omap_hwmod * to assert hardreset
3036 * @ohri: hardreset line data
3037 *
3038 * Call am33xx_prminst_assert_hardreset() with parameters extracted
3039 * from the hwmod @oh and the hardreset line data @ohri. Only
3040 * intended for use as an soc_ops function pointer. Passes along the
3041 * return value from am33xx_prminst_assert_hardreset(). XXX This
3042 * function is scheduled for removal when the PRM code is moved into
3043 * drivers/.
3044 */
3045static int _am33xx_assert_hardreset(struct omap_hwmod *oh,
3046 struct omap_hwmod_rst_info *ohri)
3047
3048{
efd44dc3
TK
3049 return omap_prm_assert_hardreset(ohri->rst_shift, 0,
3050 oh->clkdm->pwrdm.ptr->prcm_offs,
3051 oh->prcm.omap4.rstctrl_offs);
1688bf19
VH
3052}
3053
3054/**
3055 * _am33xx_deassert_hardreset - call AM33XX PRM hardreset fn with hwmod args
3056 * @oh: struct omap_hwmod * to deassert hardreset
3057 * @ohri: hardreset line data
3058 *
3059 * Call am33xx_prminst_deassert_hardreset() with parameters extracted
3060 * from the hwmod @oh and the hardreset line data @ohri. Only
3061 * intended for use as an soc_ops function pointer. Passes along the
3062 * return value from am33xx_prminst_deassert_hardreset(). XXX This
3063 * function is scheduled for removal when the PRM code is moved into
3064 * drivers/.
3065 */
3066static int _am33xx_deassert_hardreset(struct omap_hwmod *oh,
3067 struct omap_hwmod_rst_info *ohri)
3068{
37fb59d7
TK
3069 return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->st_shift, 0,
3070 oh->clkdm->pwrdm.ptr->prcm_offs,
3071 oh->prcm.omap4.rstctrl_offs,
3072 oh->prcm.omap4.rstst_offs);
1688bf19
VH
3073}
3074
3075/**
3076 * _am33xx_is_hardreset_asserted - call AM33XX PRM hardreset fn with hwmod args
3077 * @oh: struct omap_hwmod * to test hardreset
3078 * @ohri: hardreset line data
3079 *
3080 * Call am33xx_prminst_is_hardreset_asserted() with parameters
3081 * extracted from the hwmod @oh and the hardreset line data @ohri.
3082 * Only intended for use as an soc_ops function pointer. Passes along
3083 * the return value from am33xx_prminst_is_hardreset_asserted(). XXX
3084 * This function is scheduled for removal when the PRM code is moved
3085 * into drivers/.
3086 */
3087static int _am33xx_is_hardreset_asserted(struct omap_hwmod *oh,
3088 struct omap_hwmod_rst_info *ohri)
3089{
3090 return am33xx_prm_is_hardreset_asserted(ohri->rst_shift,
3091 oh->clkdm->pwrdm.ptr->prcm_offs,
3092 oh->prcm.omap4.rstctrl_offs);
3093}
3094
0102b627
BC
3095/* Public functions */
3096
3097u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
3098{
3099 if (oh->flags & HWMOD_16BIT_REG)
edfaf05c 3100 return readw_relaxed(oh->_mpu_rt_va + reg_offs);
0102b627 3101 else
edfaf05c 3102 return readl_relaxed(oh->_mpu_rt_va + reg_offs);
0102b627
BC
3103}
3104
3105void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
3106{
3107 if (oh->flags & HWMOD_16BIT_REG)
edfaf05c 3108 writew_relaxed(v, oh->_mpu_rt_va + reg_offs);
0102b627 3109 else
edfaf05c 3110 writel_relaxed(v, oh->_mpu_rt_va + reg_offs);
0102b627
BC
3111}
3112
6d3c55fd
A
3113/**
3114 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
3115 * @oh: struct omap_hwmod *
3116 *
3117 * This is a public function exposed to drivers. Some drivers may need to do
3118 * some settings before and after resetting the device. Those drivers after
3119 * doing the necessary settings could use this function to start a reset by
3120 * setting the SYSCONFIG.SOFTRESET bit.
3121 */
3122int omap_hwmod_softreset(struct omap_hwmod *oh)
3123{
3c55c1ba
PW
3124 u32 v;
3125 int ret;
3126
3127 if (!oh || !(oh->_sysc_cache))
6d3c55fd
A
3128 return -EINVAL;
3129
3c55c1ba
PW
3130 v = oh->_sysc_cache;
3131 ret = _set_softreset(oh, &v);
3132 if (ret)
3133 goto error;
3134 _write_sysconfig(v, oh);
3135
313a76ee
RQ
3136 ret = _clear_softreset(oh, &v);
3137 if (ret)
3138 goto error;
3139 _write_sysconfig(v, oh);
3140
3c55c1ba
PW
3141error:
3142 return ret;
6d3c55fd
A
3143}
3144
63c85238
PW
3145/**
3146 * omap_hwmod_lookup - look up a registered omap_hwmod by name
3147 * @name: name of the omap_hwmod to look up
3148 *
3149 * Given a @name of an omap_hwmod, return a pointer to the registered
3150 * struct omap_hwmod *, or NULL upon error.
3151 */
3152struct omap_hwmod *omap_hwmod_lookup(const char *name)
3153{
3154 struct omap_hwmod *oh;
3155
3156 if (!name)
3157 return NULL;
3158
63c85238 3159 oh = _lookup(name);
63c85238
PW
3160
3161 return oh;
3162}
3163
3164/**
3165 * omap_hwmod_for_each - call function for each registered omap_hwmod
3166 * @fn: pointer to a callback function
97d60162 3167 * @data: void * data to pass to callback function
63c85238
PW
3168 *
3169 * Call @fn for each registered omap_hwmod, passing @data to each
3170 * function. @fn must return 0 for success or any other value for
3171 * failure. If @fn returns non-zero, the iteration across omap_hwmods
3172 * will stop and the non-zero return value will be passed to the
3173 * caller of omap_hwmod_for_each(). @fn is called with
3174 * omap_hwmod_for_each() held.
3175 */
97d60162
PW
3176int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
3177 void *data)
63c85238
PW
3178{
3179 struct omap_hwmod *temp_oh;
30ebad9d 3180 int ret = 0;
63c85238
PW
3181
3182 if (!fn)
3183 return -EINVAL;
3184
63c85238 3185 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
97d60162 3186 ret = (*fn)(temp_oh, data);
63c85238
PW
3187 if (ret)
3188 break;
3189 }
63c85238
PW
3190
3191 return ret;
3192}
3193
2221b5cd
PW
3194/**
3195 * omap_hwmod_register_links - register an array of hwmod links
3196 * @ois: pointer to an array of omap_hwmod_ocp_if to register
3197 *
3198 * Intended to be called early in boot before the clock framework is
3199 * initialized. If @ois is not null, will register all omap_hwmods
9ebfd285
KH
3200 * listed in @ois that are valid for this chip. Returns -EINVAL if
3201 * omap_hwmod_init() hasn't been called before calling this function,
3202 * -ENOMEM if the link memory area can't be allocated, or 0 upon
3203 * success.
2221b5cd
PW
3204 */
3205int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
3206{
3207 int r, i;
3208
9ebfd285
KH
3209 if (!inited)
3210 return -EINVAL;
3211
2221b5cd
PW
3212 if (!ois)
3213 return 0;
3214
f7f7a29b
RN
3215 if (ois[0] == NULL) /* Empty list */
3216 return 0;
3217
2221b5cd
PW
3218 if (!linkspace) {
3219 if (_alloc_linkspace(ois)) {
3220 pr_err("omap_hwmod: could not allocate link space\n");
3221 return -ENOMEM;
3222 }
3223 }
3224
3225 i = 0;
3226 do {
3227 r = _register_link(ois[i]);
3228 WARN(r && r != -EEXIST,
3229 "omap_hwmod: _register_link(%s -> %s) returned %d\n",
3230 ois[i]->master->name, ois[i]->slave->name, r);
3231 } while (ois[++i]);
3232
3233 return 0;
3234}
3235
381d033a
PW
3236/**
3237 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
3238 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
3239 *
3240 * If the hwmod data corresponding to the MPU subsystem IP block
3241 * hasn't been initialized and set up yet, do so now. This must be
3242 * done first since sleep dependencies may be added from other hwmods
3243 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
3244 * return value.
63c85238 3245 */
381d033a 3246static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
e7c7d760 3247{
381d033a
PW
3248 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
3249 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
3250 __func__, MPU_INITIATOR_NAME);
3251 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
3252 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
e7c7d760
TL
3253}
3254
63c85238 3255/**
a2debdbd
PW
3256 * omap_hwmod_setup_one - set up a single hwmod
3257 * @oh_name: const char * name of the already-registered hwmod to set up
3258 *
381d033a
PW
3259 * Initialize and set up a single hwmod. Intended to be used for a
3260 * small number of early devices, such as the timer IP blocks used for
3261 * the scheduler clock. Must be called after omap2_clk_init().
3262 * Resolves the struct clk names to struct clk pointers for each
3263 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
3264 * -EINVAL upon error or 0 upon success.
a2debdbd
PW
3265 */
3266int __init omap_hwmod_setup_one(const char *oh_name)
63c85238
PW
3267{
3268 struct omap_hwmod *oh;
63c85238 3269
a2debdbd
PW
3270 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
3271
a2debdbd
PW
3272 oh = _lookup(oh_name);
3273 if (!oh) {
3274 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
3275 return -EINVAL;
3276 }
63c85238 3277
381d033a 3278 _ensure_mpu_hwmod_is_setup(oh);
63c85238 3279
381d033a 3280 _init(oh, NULL);
a2debdbd
PW
3281 _setup(oh, NULL);
3282
63c85238
PW
3283 return 0;
3284}
3285
3286/**
381d033a 3287 * omap_hwmod_setup_all - set up all registered IP blocks
63c85238 3288 *
381d033a
PW
3289 * Initialize and set up all IP blocks registered with the hwmod code.
3290 * Must be called after omap2_clk_init(). Resolves the struct clk
3291 * names to struct clk pointers for each registered omap_hwmod. Also
3292 * calls _setup() on each hwmod. Returns 0 upon success.
63c85238 3293 */
550c8092 3294static int __init omap_hwmod_setup_all(void)
63c85238 3295{
381d033a 3296 _ensure_mpu_hwmod_is_setup(NULL);
63c85238 3297
381d033a 3298 omap_hwmod_for_each(_init, NULL);
2092e5cc 3299 omap_hwmod_for_each(_setup, NULL);
63c85238
PW
3300
3301 return 0;
3302}
b76c8b19 3303omap_core_initcall(omap_hwmod_setup_all);
63c85238 3304
63c85238
PW
3305/**
3306 * omap_hwmod_enable - enable an omap_hwmod
3307 * @oh: struct omap_hwmod *
3308 *
74ff3a68 3309 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
63c85238
PW
3310 * Returns -EINVAL on error or passes along the return value from _enable().
3311 */
3312int omap_hwmod_enable(struct omap_hwmod *oh)
3313{
3314 int r;
dc6d1cda 3315 unsigned long flags;
63c85238
PW
3316
3317 if (!oh)
3318 return -EINVAL;
3319
dc6d1cda
PW
3320 spin_lock_irqsave(&oh->_lock, flags);
3321 r = _enable(oh);
3322 spin_unlock_irqrestore(&oh->_lock, flags);
63c85238
PW
3323
3324 return r;
3325}
3326
3327/**
3328 * omap_hwmod_idle - idle an omap_hwmod
3329 * @oh: struct omap_hwmod *
3330 *
74ff3a68 3331 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
63c85238
PW
3332 * Returns -EINVAL on error or passes along the return value from _idle().
3333 */
3334int omap_hwmod_idle(struct omap_hwmod *oh)
3335{
dc6d1cda
PW
3336 unsigned long flags;
3337
63c85238
PW
3338 if (!oh)
3339 return -EINVAL;
3340
dc6d1cda
PW
3341 spin_lock_irqsave(&oh->_lock, flags);
3342 _idle(oh);
3343 spin_unlock_irqrestore(&oh->_lock, flags);
63c85238
PW
3344
3345 return 0;
3346}
3347
3348/**
3349 * omap_hwmod_shutdown - shutdown an omap_hwmod
3350 * @oh: struct omap_hwmod *
3351 *
74ff3a68 3352 * Shutdown an omap_hwmod @oh. Intended to be called by
63c85238
PW
3353 * omap_device_shutdown(). Returns -EINVAL on error or passes along
3354 * the return value from _shutdown().
3355 */
3356int omap_hwmod_shutdown(struct omap_hwmod *oh)
3357{
dc6d1cda
PW
3358 unsigned long flags;
3359
63c85238
PW
3360 if (!oh)
3361 return -EINVAL;
3362
dc6d1cda 3363 spin_lock_irqsave(&oh->_lock, flags);
63c85238 3364 _shutdown(oh);
dc6d1cda 3365 spin_unlock_irqrestore(&oh->_lock, flags);
63c85238
PW
3366
3367 return 0;
3368}
3369
3370/**
3371 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
3372 * @oh: struct omap_hwmod *oh
3373 *
3374 * Intended to be called by the omap_device code.
3375 */
3376int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
3377{
dc6d1cda
PW
3378 unsigned long flags;
3379
3380 spin_lock_irqsave(&oh->_lock, flags);
63c85238 3381 _enable_clocks(oh);
dc6d1cda 3382 spin_unlock_irqrestore(&oh->_lock, flags);
63c85238
PW
3383
3384 return 0;
3385}
3386
3387/**
3388 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
3389 * @oh: struct omap_hwmod *oh
3390 *
3391 * Intended to be called by the omap_device code.
3392 */
3393int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
3394{
dc6d1cda
PW
3395 unsigned long flags;
3396
3397 spin_lock_irqsave(&oh->_lock, flags);
63c85238 3398 _disable_clocks(oh);
dc6d1cda 3399 spin_unlock_irqrestore(&oh->_lock, flags);
63c85238
PW
3400
3401 return 0;
3402}
3403
3404/**
3405 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
3406 * @oh: struct omap_hwmod *oh
3407 *
3408 * Intended to be called by drivers and core code when all posted
3409 * writes to a device must complete before continuing further
3410 * execution (for example, after clearing some device IRQSTATUS
3411 * register bits)
3412 *
3413 * XXX what about targets with multiple OCP threads?
3414 */
3415void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
3416{
3417 BUG_ON(!oh);
3418
43b40992 3419 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
4f8a428d
RK
3420 WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
3421 oh->name);
63c85238
PW
3422 return;
3423 }
3424
3425 /*
3426 * Forces posted writes to complete on the OCP thread handling
3427 * register writes
3428 */
cc7a1d2a 3429 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
63c85238
PW
3430}
3431
3432/**
3433 * omap_hwmod_reset - reset the hwmod
3434 * @oh: struct omap_hwmod *
3435 *
3436 * Under some conditions, a driver may wish to reset the entire device.
3437 * Called from omap_device code. Returns -EINVAL on error or passes along
9b579114 3438 * the return value from _reset().
63c85238
PW
3439 */
3440int omap_hwmod_reset(struct omap_hwmod *oh)
3441{
3442 int r;
dc6d1cda 3443 unsigned long flags;
63c85238 3444
9b579114 3445 if (!oh)
63c85238
PW
3446 return -EINVAL;
3447
dc6d1cda 3448 spin_lock_irqsave(&oh->_lock, flags);
63c85238 3449 r = _reset(oh);
dc6d1cda 3450 spin_unlock_irqrestore(&oh->_lock, flags);
63c85238
PW
3451
3452 return r;
3453}
3454
5e8370f1
PW
3455/*
3456 * IP block data retrieval functions
3457 */
3458
63c85238
PW
3459/**
3460 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
3461 * @oh: struct omap_hwmod *
dad4191d 3462 * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
63c85238
PW
3463 *
3464 * Count the number of struct resource array elements necessary to
3465 * contain omap_hwmod @oh resources. Intended to be called by code
3466 * that registers omap_devices. Intended to be used to determine the
3467 * size of a dynamically-allocated struct resource array, before
3468 * calling omap_hwmod_fill_resources(). Returns the number of struct
3469 * resource array elements needed.
3470 *
3471 * XXX This code is not optimized. It could attempt to merge adjacent
3472 * resource IDs.
3473 *
3474 */
dad4191d 3475int omap_hwmod_count_resources(struct omap_hwmod *oh, unsigned long flags)
63c85238 3476{
dad4191d 3477 int ret = 0;
63c85238 3478
dad4191d
PU
3479 if (flags & IORESOURCE_IRQ)
3480 ret += _count_mpu_irqs(oh);
63c85238 3481
dad4191d
PU
3482 if (flags & IORESOURCE_DMA)
3483 ret += _count_sdma_reqs(oh);
2221b5cd 3484
dad4191d
PU
3485 if (flags & IORESOURCE_MEM) {
3486 int i = 0;
3487 struct omap_hwmod_ocp_if *os;
3488 struct list_head *p = oh->slave_ports.next;
3489
3490 while (i < oh->slaves_cnt) {
3491 os = _fetch_next_ocp_if(&p, &i);
3492 ret += _count_ocp_if_addr_spaces(os);
3493 }
5d95dde7 3494 }
63c85238
PW
3495
3496 return ret;
3497}
3498
3499/**
3500 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
3501 * @oh: struct omap_hwmod *
3502 * @res: pointer to the first element of an array of struct resource to fill
3503 *
3504 * Fill the struct resource array @res with resource data from the
3505 * omap_hwmod @oh. Intended to be called by code that registers
3506 * omap_devices. See also omap_hwmod_count_resources(). Returns the
3507 * number of array elements filled.
3508 */
3509int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
3510{
5d95dde7 3511 struct omap_hwmod_ocp_if *os;
11cd4b94 3512 struct list_head *p;
5d95dde7 3513 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
63c85238
PW
3514 int r = 0;
3515
3516 /* For each IRQ, DMA, memory area, fill in array.*/
3517
212738a4
PW
3518 mpu_irqs_cnt = _count_mpu_irqs(oh);
3519 for (i = 0; i < mpu_irqs_cnt; i++) {
718bfd76
PW
3520 (res + r)->name = (oh->mpu_irqs + i)->name;
3521 (res + r)->start = (oh->mpu_irqs + i)->irq;
3522 (res + r)->end = (oh->mpu_irqs + i)->irq;
63c85238
PW
3523 (res + r)->flags = IORESOURCE_IRQ;
3524 r++;
3525 }
3526
bc614958
PW
3527 sdma_reqs_cnt = _count_sdma_reqs(oh);
3528 for (i = 0; i < sdma_reqs_cnt; i++) {
9ee9fff9
BC
3529 (res + r)->name = (oh->sdma_reqs + i)->name;
3530 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
3531 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
63c85238
PW
3532 (res + r)->flags = IORESOURCE_DMA;
3533 r++;
3534 }
3535
11cd4b94 3536 p = oh->slave_ports.next;
2221b5cd 3537
5d95dde7
PW
3538 i = 0;
3539 while (i < oh->slaves_cnt) {
11cd4b94 3540 os = _fetch_next_ocp_if(&p, &i);
78183f3f 3541 addr_cnt = _count_ocp_if_addr_spaces(os);
63c85238 3542
78183f3f 3543 for (j = 0; j < addr_cnt; j++) {
cd503802 3544 (res + r)->name = (os->addr + j)->name;
63c85238
PW
3545 (res + r)->start = (os->addr + j)->pa_start;
3546 (res + r)->end = (os->addr + j)->pa_end;
3547 (res + r)->flags = IORESOURCE_MEM;
3548 r++;
3549 }
3550 }
3551
3552 return r;
3553}
3554
b82b04e8
VH
3555/**
3556 * omap_hwmod_fill_dma_resources - fill struct resource array with dma data
3557 * @oh: struct omap_hwmod *
3558 * @res: pointer to the array of struct resource to fill
3559 *
3560 * Fill the struct resource array @res with dma resource data from the
3561 * omap_hwmod @oh. Intended to be called by code that registers
3562 * omap_devices. See also omap_hwmod_count_resources(). Returns the
3563 * number of array elements filled.
3564 */
3565int omap_hwmod_fill_dma_resources(struct omap_hwmod *oh, struct resource *res)
3566{
3567 int i, sdma_reqs_cnt;
3568 int r = 0;
3569
3570 sdma_reqs_cnt = _count_sdma_reqs(oh);
3571 for (i = 0; i < sdma_reqs_cnt; i++) {
3572 (res + r)->name = (oh->sdma_reqs + i)->name;
3573 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
3574 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
3575 (res + r)->flags = IORESOURCE_DMA;
3576 r++;
3577 }
3578
3579 return r;
3580}
3581
5e8370f1
PW
3582/**
3583 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
3584 * @oh: struct omap_hwmod * to operate on
3585 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
3586 * @name: pointer to the name of the data to fetch (optional)
3587 * @rsrc: pointer to a struct resource, allocated by the caller
3588 *
3589 * Retrieve MPU IRQ, SDMA request line, or address space start/end
3590 * data for the IP block pointed to by @oh. The data will be filled
3591 * into a struct resource record pointed to by @rsrc. The struct
3592 * resource must be allocated by the caller. When @name is non-null,
3593 * the data associated with the matching entry in the IRQ/SDMA/address
3594 * space hwmod data arrays will be returned. If @name is null, the
3595 * first array entry will be returned. Data order is not meaningful
3596 * in hwmod data, so callers are strongly encouraged to use a non-null
3597 * @name whenever possible to avoid unpredictable effects if hwmod
3598 * data is later added that causes data ordering to change. This
3599 * function is only intended for use by OMAP core code. Device
3600 * drivers should not call this function - the appropriate bus-related
3601 * data accessor functions should be used instead. Returns 0 upon
3602 * success or a negative error code upon error.
3603 */
3604int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
3605 const char *name, struct resource *rsrc)
3606{
3607 int r;
3608 unsigned int irq, dma;
3609 u32 pa_start, pa_end;
3610
3611 if (!oh || !rsrc)
3612 return -EINVAL;
3613
3614 if (type == IORESOURCE_IRQ) {
3615 r = _get_mpu_irq_by_name(oh, name, &irq);
3616 if (r)
3617 return r;
3618
3619 rsrc->start = irq;
3620 rsrc->end = irq;
3621 } else if (type == IORESOURCE_DMA) {
3622 r = _get_sdma_req_by_name(oh, name, &dma);
3623 if (r)
3624 return r;
3625
3626 rsrc->start = dma;
3627 rsrc->end = dma;
3628 } else if (type == IORESOURCE_MEM) {
3629 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
3630 if (r)
3631 return r;
3632
3633 rsrc->start = pa_start;
3634 rsrc->end = pa_end;
3635 } else {
3636 return -EINVAL;
3637 }
3638
3639 rsrc->flags = type;
3640 rsrc->name = name;
3641
3642 return 0;
3643}
3644
63c85238
PW
3645/**
3646 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
3647 * @oh: struct omap_hwmod *
3648 *
3649 * Return the powerdomain pointer associated with the OMAP module
3650 * @oh's main clock. If @oh does not have a main clk, return the
3651 * powerdomain associated with the interface clock associated with the
3652 * module's MPU port. (XXX Perhaps this should use the SDMA port
3653 * instead?) Returns NULL on error, or a struct powerdomain * on
3654 * success.
3655 */
3656struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
3657{
3658 struct clk *c;
2d6141ba 3659 struct omap_hwmod_ocp_if *oi;
f5dd3bb5 3660 struct clockdomain *clkdm;
f5dd3bb5 3661 struct clk_hw_omap *clk;
63c85238
PW
3662
3663 if (!oh)
3664 return NULL;
3665
f5dd3bb5
RN
3666 if (oh->clkdm)
3667 return oh->clkdm->pwrdm.ptr;
3668
63c85238
PW
3669 if (oh->_clk) {
3670 c = oh->_clk;
3671 } else {
2d6141ba
PW
3672 oi = _find_mpu_rt_port(oh);
3673 if (!oi)
63c85238 3674 return NULL;
2d6141ba 3675 c = oi->_clk;
63c85238
PW
3676 }
3677
f5dd3bb5
RN
3678 clk = to_clk_hw_omap(__clk_get_hw(c));
3679 clkdm = clk->clkdm;
f5dd3bb5 3680 if (!clkdm)
d5647c18
TG
3681 return NULL;
3682
f5dd3bb5 3683 return clkdm->pwrdm.ptr;
63c85238
PW
3684}
3685
db2a60bf
PW
3686/**
3687 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3688 * @oh: struct omap_hwmod *
3689 *
3690 * Returns the virtual address corresponding to the beginning of the
3691 * module's register target, in the address range that is intended to
3692 * be used by the MPU. Returns the virtual address upon success or NULL
3693 * upon error.
3694 */
3695void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3696{
3697 if (!oh)
3698 return NULL;
3699
3700 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3701 return NULL;
3702
3703 if (oh->_state == _HWMOD_STATE_UNKNOWN)
3704 return NULL;
3705
3706 return oh->_mpu_rt_va;
3707}
3708
63c85238
PW
3709/**
3710 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
3711 * @oh: struct omap_hwmod *
3712 * @init_oh: struct omap_hwmod * (initiator)
3713 *
3714 * Add a sleep dependency between the initiator @init_oh and @oh.
3715 * Intended to be called by DSP/Bridge code via platform_data for the
3716 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3717 * code needs to add/del initiator dependencies dynamically
3718 * before/after accessing a device. Returns the return value from
3719 * _add_initiator_dep().
3720 *
3721 * XXX Keep a usecount in the clockdomain code
3722 */
3723int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
3724 struct omap_hwmod *init_oh)
3725{
3726 return _add_initiator_dep(oh, init_oh);
3727}
3728
3729/*
3730 * XXX what about functions for drivers to save/restore ocp_sysconfig
3731 * for context save/restore operations?
3732 */
3733
3734/**
3735 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
3736 * @oh: struct omap_hwmod *
3737 * @init_oh: struct omap_hwmod * (initiator)
3738 *
3739 * Remove a sleep dependency between the initiator @init_oh and @oh.
3740 * Intended to be called by DSP/Bridge code via platform_data for the
3741 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3742 * code needs to add/del initiator dependencies dynamically
3743 * before/after accessing a device. Returns the return value from
3744 * _del_initiator_dep().
3745 *
3746 * XXX Keep a usecount in the clockdomain code
3747 */
3748int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
3749 struct omap_hwmod *init_oh)
3750{
3751 return _del_initiator_dep(oh, init_oh);
3752}
3753
63c85238
PW
3754/**
3755 * omap_hwmod_enable_wakeup - allow device to wake up the system
3756 * @oh: struct omap_hwmod *
3757 *
3758 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
2a1cc144
G
3759 * send wakeups to the PRCM, and enable I/O ring wakeup events for
3760 * this IP block if it has dynamic mux entries. Eventually this
3761 * should set PRCM wakeup registers to cause the PRCM to receive
3762 * wakeup events from the module. Does not set any wakeup routing
3763 * registers beyond this point - if the module is to wake up any other
3764 * module or subsystem, that must be set separately. Called by
3765 * omap_device code. Returns -EINVAL on error or 0 upon success.
63c85238
PW
3766 */
3767int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3768{
dc6d1cda 3769 unsigned long flags;
5a7ddcbd 3770 u32 v;
dc6d1cda 3771
dc6d1cda 3772 spin_lock_irqsave(&oh->_lock, flags);
2a1cc144
G
3773
3774 if (oh->class->sysc &&
3775 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3776 v = oh->_sysc_cache;
3777 _enable_wakeup(oh, &v);
3778 _write_sysconfig(v, oh);
3779 }
3780
eceec009 3781 _set_idle_ioring_wakeup(oh, true);
dc6d1cda 3782 spin_unlock_irqrestore(&oh->_lock, flags);
63c85238
PW
3783
3784 return 0;
3785}
3786
3787/**
3788 * omap_hwmod_disable_wakeup - prevent device from waking the system
3789 * @oh: struct omap_hwmod *
3790 *
3791 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
2a1cc144
G
3792 * from sending wakeups to the PRCM, and disable I/O ring wakeup
3793 * events for this IP block if it has dynamic mux entries. Eventually
3794 * this should clear PRCM wakeup registers to cause the PRCM to ignore
3795 * wakeup events from the module. Does not set any wakeup routing
3796 * registers beyond this point - if the module is to wake up any other
3797 * module or subsystem, that must be set separately. Called by
3798 * omap_device code. Returns -EINVAL on error or 0 upon success.
63c85238
PW
3799 */
3800int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3801{
dc6d1cda 3802 unsigned long flags;
5a7ddcbd 3803 u32 v;
dc6d1cda 3804
dc6d1cda 3805 spin_lock_irqsave(&oh->_lock, flags);
2a1cc144
G
3806
3807 if (oh->class->sysc &&
3808 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3809 v = oh->_sysc_cache;
3810 _disable_wakeup(oh, &v);
3811 _write_sysconfig(v, oh);
3812 }
3813
eceec009 3814 _set_idle_ioring_wakeup(oh, false);
dc6d1cda 3815 spin_unlock_irqrestore(&oh->_lock, flags);
63c85238
PW
3816
3817 return 0;
3818}
43b40992 3819
aee48e3c
PW
3820/**
3821 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3822 * contained in the hwmod module.
3823 * @oh: struct omap_hwmod *
3824 * @name: name of the reset line to lookup and assert
3825 *
3826 * Some IP like dsp, ipu or iva contain processor that require
3827 * an HW reset line to be assert / deassert in order to enable fully
3828 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3829 * yet supported on this OMAP; otherwise, passes along the return value
3830 * from _assert_hardreset().
3831 */
3832int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3833{
3834 int ret;
dc6d1cda 3835 unsigned long flags;
aee48e3c
PW
3836
3837 if (!oh)
3838 return -EINVAL;
3839
dc6d1cda 3840 spin_lock_irqsave(&oh->_lock, flags);
aee48e3c 3841 ret = _assert_hardreset(oh, name);
dc6d1cda 3842 spin_unlock_irqrestore(&oh->_lock, flags);
aee48e3c
PW
3843
3844 return ret;
3845}
3846
3847/**
3848 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3849 * contained in the hwmod module.
3850 * @oh: struct omap_hwmod *
3851 * @name: name of the reset line to look up and deassert
3852 *
3853 * Some IP like dsp, ipu or iva contain processor that require
3854 * an HW reset line to be assert / deassert in order to enable fully
3855 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3856 * yet supported on this OMAP; otherwise, passes along the return value
3857 * from _deassert_hardreset().
3858 */
3859int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3860{
3861 int ret;
dc6d1cda 3862 unsigned long flags;
aee48e3c
PW
3863
3864 if (!oh)
3865 return -EINVAL;
3866
dc6d1cda 3867 spin_lock_irqsave(&oh->_lock, flags);
aee48e3c 3868 ret = _deassert_hardreset(oh, name);
dc6d1cda 3869 spin_unlock_irqrestore(&oh->_lock, flags);
aee48e3c
PW
3870
3871 return ret;
3872}
3873
3874/**
3875 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
3876 * contained in the hwmod module
3877 * @oh: struct omap_hwmod *
3878 * @name: name of the reset line to look up and read
3879 *
3880 * Return the current state of the hwmod @oh's reset line named @name:
3881 * returns -EINVAL upon parameter error or if this operation
3882 * is unsupported on the current OMAP; otherwise, passes along the return
3883 * value from _read_hardreset().
3884 */
3885int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
3886{
3887 int ret;
dc6d1cda 3888 unsigned long flags;
aee48e3c
PW
3889
3890 if (!oh)
3891 return -EINVAL;
3892
dc6d1cda 3893 spin_lock_irqsave(&oh->_lock, flags);
aee48e3c 3894 ret = _read_hardreset(oh, name);
dc6d1cda 3895 spin_unlock_irqrestore(&oh->_lock, flags);
aee48e3c
PW
3896
3897 return ret;
3898}
3899
3900
43b40992
PW
3901/**
3902 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3903 * @classname: struct omap_hwmod_class name to search for
3904 * @fn: callback function pointer to call for each hwmod in class @classname
3905 * @user: arbitrary context data to pass to the callback function
3906 *
ce35b244
BC
3907 * For each omap_hwmod of class @classname, call @fn.
3908 * If the callback function returns something other than
43b40992
PW
3909 * zero, the iterator is terminated, and the callback function's return
3910 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3911 * if @classname or @fn are NULL, or passes back the error code from @fn.
3912 */
3913int omap_hwmod_for_each_by_class(const char *classname,
3914 int (*fn)(struct omap_hwmod *oh,
3915 void *user),
3916 void *user)
3917{
3918 struct omap_hwmod *temp_oh;
3919 int ret = 0;
3920
3921 if (!classname || !fn)
3922 return -EINVAL;
3923
3924 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3925 __func__, classname);
3926
43b40992
PW
3927 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3928 if (!strcmp(temp_oh->class->name, classname)) {
3929 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3930 __func__, temp_oh->name);
3931 ret = (*fn)(temp_oh, user);
3932 if (ret)
3933 break;
3934 }
3935 }
3936
43b40992
PW
3937 if (ret)
3938 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3939 __func__, ret);
3940
3941 return ret;
3942}
3943
2092e5cc
PW
3944/**
3945 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3946 * @oh: struct omap_hwmod *
3947 * @state: state that _setup() should leave the hwmod in
3948 *
550c8092 3949 * Sets the hwmod state that @oh will enter at the end of _setup()
64813c3f
PW
3950 * (called by omap_hwmod_setup_*()). See also the documentation
3951 * for _setup_postsetup(), above. Returns 0 upon success or
3952 * -EINVAL if there is a problem with the arguments or if the hwmod is
3953 * in the wrong state.
2092e5cc
PW
3954 */
3955int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3956{
3957 int ret;
dc6d1cda 3958 unsigned long flags;
2092e5cc
PW
3959
3960 if (!oh)
3961 return -EINVAL;
3962
3963 if (state != _HWMOD_STATE_DISABLED &&
3964 state != _HWMOD_STATE_ENABLED &&
3965 state != _HWMOD_STATE_IDLE)
3966 return -EINVAL;
3967
dc6d1cda 3968 spin_lock_irqsave(&oh->_lock, flags);
2092e5cc
PW
3969
3970 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3971 ret = -EINVAL;
3972 goto ohsps_unlock;
3973 }
3974
3975 oh->_postsetup_state = state;
3976 ret = 0;
3977
3978ohsps_unlock:
dc6d1cda 3979 spin_unlock_irqrestore(&oh->_lock, flags);
2092e5cc
PW
3980
3981 return ret;
3982}
c80705aa
KH
3983
3984/**
3985 * omap_hwmod_get_context_loss_count - get lost context count
3986 * @oh: struct omap_hwmod *
3987 *
e6d3a8b0
RN
3988 * Returns the context loss count of associated @oh
3989 * upon success, or zero if no context loss data is available.
c80705aa 3990 *
e6d3a8b0
RN
3991 * On OMAP4, this queries the per-hwmod context loss register,
3992 * assuming one exists. If not, or on OMAP2/3, this queries the
3993 * enclosing powerdomain context loss count.
c80705aa 3994 */
fc013873 3995int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
c80705aa
KH
3996{
3997 struct powerdomain *pwrdm;
3998 int ret = 0;
3999
e6d3a8b0
RN
4000 if (soc_ops.get_context_lost)
4001 return soc_ops.get_context_lost(oh);
4002
c80705aa
KH
4003 pwrdm = omap_hwmod_get_pwrdm(oh);
4004 if (pwrdm)
4005 ret = pwrdm_get_context_loss_count(pwrdm);
4006
4007 return ret;
4008}
43b01643
PW
4009
4010/**
4011 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
4012 * @oh: struct omap_hwmod *
4013 *
4014 * Prevent the hwmod @oh from being reset during the setup process.
4015 * Intended for use by board-*.c files on boards with devices that
4016 * cannot tolerate being reset. Must be called before the hwmod has
4017 * been set up. Returns 0 upon success or negative error code upon
4018 * failure.
4019 */
4020int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
4021{
4022 if (!oh)
4023 return -EINVAL;
4024
4025 if (oh->_state != _HWMOD_STATE_REGISTERED) {
4026 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
4027 oh->name);
4028 return -EINVAL;
4029 }
4030
4031 oh->flags |= HWMOD_INIT_NO_RESET;
4032
4033 return 0;
4034}
abc2d545
TK
4035
4036/**
4037 * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
4038 * @oh: struct omap_hwmod * containing hwmod mux entries
4039 * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
4040 * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
4041 *
4042 * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
4043 * entry number @pad_idx for the hwmod @oh, trigger the interrupt
4044 * service routine for the hwmod's mpu_irqs array index @irq_idx. If
4045 * this function is not called for a given pad_idx, then the ISR
4046 * associated with @oh's first MPU IRQ will be triggered when an I/O
4047 * pad wakeup occurs on that pad. Note that @pad_idx is the index of
4048 * the _dynamic or wakeup_ entry: if there are other entries not
4049 * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
4050 * entries are NOT COUNTED in the dynamic pad index. This function
4051 * must be called separately for each pad that requires its interrupt
4052 * to be re-routed this way. Returns -EINVAL if there is an argument
4053 * problem or if @oh does not have hwmod mux entries or MPU IRQs;
4054 * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
4055 *
4056 * XXX This function interface is fragile. Rather than using array
4057 * indexes, which are subject to unpredictable change, it should be
4058 * using hwmod IRQ names, and some other stable key for the hwmod mux
4059 * pad records.
4060 */
4061int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
4062{
4063 int nr_irqs;
4064
4065 might_sleep();
4066
4067 if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
4068 pad_idx >= oh->mux->nr_pads_dynamic)
4069 return -EINVAL;
4070
4071 /* Check the number of available mpu_irqs */
4072 for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
4073 ;
4074
4075 if (irq_idx >= nr_irqs)
4076 return -EINVAL;
4077
4078 if (!oh->mux->irqs) {
4079 /* XXX What frees this? */
4080 oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
4081 GFP_KERNEL);
4082 if (!oh->mux->irqs)
4083 return -ENOMEM;
4084 }
4085 oh->mux->irqs[pad_idx] = irq_idx;
4086
4087 return 0;
4088}
9ebfd285
KH
4089
4090/**
4091 * omap_hwmod_init - initialize the hwmod code
4092 *
4093 * Sets up some function pointers needed by the hwmod code to operate on the
4094 * currently-booted SoC. Intended to be called once during kernel init
4095 * before any hwmods are registered. No return value.
4096 */
4097void __init omap_hwmod_init(void)
4098{
ff4ae5d9 4099 if (cpu_is_omap24xx()) {
9002e921 4100 soc_ops.wait_target_ready = _omap2xxx_3xxx_wait_target_ready;
ff4ae5d9
PW
4101 soc_ops.assert_hardreset = _omap2_assert_hardreset;
4102 soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
4103 soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
4104 } else if (cpu_is_omap34xx()) {
9002e921 4105 soc_ops.wait_target_ready = _omap2xxx_3xxx_wait_target_ready;
b8249cf2
KH
4106 soc_ops.assert_hardreset = _omap2_assert_hardreset;
4107 soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
4108 soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
0385c582 4109 soc_ops.init_clkdm = _init_clkdm;
debcd1f8 4110 } else if (cpu_is_omap44xx() || soc_is_omap54xx() || soc_is_dra7xx()) {
9ebfd285
KH
4111 soc_ops.enable_module = _omap4_enable_module;
4112 soc_ops.disable_module = _omap4_disable_module;
8f6aa8ee 4113 soc_ops.wait_target_ready = _omap4_wait_target_ready;
b8249cf2
KH
4114 soc_ops.assert_hardreset = _omap4_assert_hardreset;
4115 soc_ops.deassert_hardreset = _omap4_deassert_hardreset;
4116 soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
0a179eaa 4117 soc_ops.init_clkdm = _init_clkdm;
e6d3a8b0
RN
4118 soc_ops.update_context_lost = _omap4_update_context_lost;
4119 soc_ops.get_context_lost = _omap4_get_context_lost;
c8b428a5
AM
4120 } else if (soc_is_am43xx()) {
4121 soc_ops.enable_module = _omap4_enable_module;
4122 soc_ops.disable_module = _omap4_disable_module;
4123 soc_ops.wait_target_ready = _omap4_wait_target_ready;
409d7063
TK
4124 soc_ops.assert_hardreset = _omap4_assert_hardreset;
4125 soc_ops.deassert_hardreset = _omap4_deassert_hardreset;
4126 soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
c8b428a5 4127 soc_ops.init_clkdm = _init_clkdm;
1688bf19 4128 } else if (soc_is_am33xx()) {
05d2b093
TK
4129 soc_ops.enable_module = _omap4_enable_module;
4130 soc_ops.disable_module = _omap4_disable_module;
4131 soc_ops.wait_target_ready = _omap4_wait_target_ready;
1688bf19
VH
4132 soc_ops.assert_hardreset = _am33xx_assert_hardreset;
4133 soc_ops.deassert_hardreset = _am33xx_deassert_hardreset;
4134 soc_ops.is_hardreset_asserted = _am33xx_is_hardreset_asserted;
4135 soc_ops.init_clkdm = _init_clkdm;
8f6aa8ee
KH
4136 } else {
4137 WARN(1, "omap_hwmod: unknown SoC type\n");
9ebfd285
KH
4138 }
4139
4140 inited = true;
4141}
68c9a95e
TL
4142
4143/**
4144 * omap_hwmod_get_main_clk - get pointer to main clock name
4145 * @oh: struct omap_hwmod *
4146 *
4147 * Returns the main clock name assocated with @oh upon success,
4148 * or NULL if @oh is NULL.
4149 */
4150const char *omap_hwmod_get_main_clk(struct omap_hwmod *oh)
4151{
4152 if (!oh)
4153 return NULL;
4154
4155 return oh->main_clk;
4156}