blob: 98cde62c0cacec1e7cdc6f33e4d6eabb3e3ba57f [file] [log] [blame]
Paul Walmsley63c85232009-09-03 20:14:03 +03001/*
2 * omap_hwmod implementation for OMAP2/3/4
3 *
Paul Walmsley550c8092011-02-28 11:58:14 -07004 * Copyright (C) 2009-2011 Nokia Corporation
Paul Walmsley30e105c2012-04-19 00:49:09 -06005 * Copyright (C) 2011-2012 Texas Instruments, Inc.
Paul Walmsley63c85232009-09-03 20:14:03 +03006 *
Paul Walmsley4788da22010-05-18 20:24:05 -06007 * Paul Walmsley, Benoît Cousson, Kevin Hilman
8 *
9 * Created in collaboration with (alphabetical order): Thara Gopinath,
10 * Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari Poussa, Anand
11 * Sawant, Santosh Shilimkar, Richard Woodruff
Paul Walmsley63c85232009-09-03 20:14:03 +030012 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -060017 * Introduction
18 * ------------
19 * One way to view an OMAP SoC is as a collection of largely unrelated
20 * IP blocks connected by interconnects. The IP blocks include
21 * devices such as ARM processors, audio serial interfaces, UARTs,
22 * etc. Some of these devices, like the DSP, are created by TI;
23 * others, like the SGX, largely originate from external vendors. In
24 * TI's documentation, on-chip devices are referred to as "OMAP
25 * modules." Some of these IP blocks are identical across several
26 * OMAP versions. Others are revised frequently.
Paul Walmsley63c85232009-09-03 20:14:03 +030027 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -060028 * These OMAP modules are tied together by various interconnects.
29 * Most of the address and data flow between modules is via OCP-based
30 * interconnects such as the L3 and L4 buses; but there are other
31 * interconnects that distribute the hardware clock tree, handle idle
32 * and reset signaling, supply power, and connect the modules to
33 * various pads or balls on the OMAP package.
34 *
35 * OMAP hwmod provides a consistent way to describe the on-chip
36 * hardware blocks and their integration into the rest of the chip.
37 * This description can be automatically generated from the TI
38 * hardware database. OMAP hwmod provides a standard, consistent API
39 * to reset, enable, idle, and disable these hardware blocks. And
40 * hwmod provides a way for other core code, such as the Linux device
41 * code or the OMAP power management and address space mapping code,
42 * to query the hardware database.
43 *
44 * Using hwmod
45 * -----------
46 * Drivers won't call hwmod functions directly. That is done by the
47 * omap_device code, and in rare occasions, by custom integration code
48 * in arch/arm/ *omap*. The omap_device code includes functions to
49 * build a struct platform_device using omap_hwmod data, and that is
50 * currently how hwmod data is communicated to drivers and to the
51 * Linux driver model. Most drivers will call omap_hwmod functions only
52 * indirectly, via pm_runtime*() functions.
53 *
54 * From a layering perspective, here is where the OMAP hwmod code
55 * fits into the kernel software stack:
56 *
57 * +-------------------------------+
58 * | Device driver code |
59 * | (e.g., drivers/) |
60 * +-------------------------------+
61 * | Linux driver model |
62 * | (platform_device / |
63 * | platform_driver data/code) |
64 * +-------------------------------+
65 * | OMAP core-driver integration |
66 * |(arch/arm/mach-omap2/devices.c)|
67 * +-------------------------------+
68 * | omap_device code |
69 * | (../plat-omap/omap_device.c) |
70 * +-------------------------------+
71 * ----> | omap_hwmod code/data | <-----
72 * | (../mach-omap2/omap_hwmod*) |
73 * +-------------------------------+
74 * | OMAP clock/PRCM/register fns |
75 * | (__raw_{read,write}l, clk*) |
76 * +-------------------------------+
77 *
78 * Device drivers should not contain any OMAP-specific code or data in
79 * them. They should only contain code to operate the IP block that
80 * the driver is responsible for. This is because these IP blocks can
81 * also appear in other SoCs, either from TI (such as DaVinci) or from
82 * other manufacturers; and drivers should be reusable across other
83 * platforms.
84 *
85 * The OMAP hwmod code also will attempt to reset and idle all on-chip
86 * devices upon boot. The goal here is for the kernel to be
87 * completely self-reliant and independent from bootloaders. This is
88 * to ensure a repeatable configuration, both to ensure consistent
89 * runtime behavior, and to make it easier for others to reproduce
90 * bugs.
91 *
92 * OMAP module activity states
93 * ---------------------------
94 * The hwmod code considers modules to be in one of several activity
95 * states. IP blocks start out in an UNKNOWN state, then once they
96 * are registered via the hwmod code, proceed to the REGISTERED state.
97 * Once their clock names are resolved to clock pointers, the module
98 * enters the CLKS_INITED state; and finally, once the module has been
99 * reset and the integration registers programmed, the INITIALIZED state
100 * is entered. The hwmod code will then place the module into either
101 * the IDLE state to save power, or in the case of a critical system
102 * module, the ENABLED state.
103 *
104 * OMAP core integration code can then call omap_hwmod*() functions
105 * directly to move the module between the IDLE, ENABLED, and DISABLED
106 * states, as needed. This is done during both the PM idle loop, and
107 * in the OMAP core integration code's implementation of the PM runtime
108 * functions.
109 *
110 * References
111 * ----------
112 * This is a partial list.
Paul Walmsley63c85232009-09-03 20:14:03 +0300113 * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
114 * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
115 * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
116 * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
117 * - Open Core Protocol Specification 2.2
118 *
119 * To do:
Paul Walmsley63c85232009-09-03 20:14:03 +0300120 * - handle IO mapping
121 * - bus throughput & module latency measurement code
122 *
123 * XXX add tests at the beginning of each function to ensure the hwmod is
124 * in the appropriate state
125 * XXX error return values should be checked to ensure that they are
126 * appropriate
127 */
128#undef DEBUG
129
130#include <linux/kernel.h>
131#include <linux/errno.h>
132#include <linux/io.h>
133#include <linux/clk.h>
134#include <linux/delay.h>
135#include <linux/err.h>
136#include <linux/list.h>
137#include <linux/mutex.h>
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -0700138#include <linux/spinlock.h>
Tero Kristoabc2d542011-12-16 14:36:59 -0700139#include <linux/slab.h>
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600140#include <linux/bootmem.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300141
Tony Lindgren4e653312011-11-10 22:45:17 +0100142#include "common.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700143#include <plat/cpu.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -0700144#include "clockdomain.h"
Paul Walmsley72e06d02010-12-21 21:05:16 -0700145#include "powerdomain.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700146#include <plat/clock.h>
147#include <plat/omap_hwmod.h>
Benoît Cousson5365efb2010-09-21 10:34:11 -0600148#include <plat/prcm.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300149
Paul Walmsley59fb6592010-12-21 15:30:55 -0700150#include "cm2xxx_3xxx.h"
Benoit Coussond0f06312011-07-10 05:56:30 -0600151#include "cminst44xx.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -0700152#include "prm2xxx_3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -0700153#include "prm44xx.h"
Benoit Coussoneaac3292011-07-10 05:56:31 -0600154#include "prminst44xx.h"
Tony Lindgren8d9af882010-12-22 18:42:35 -0800155#include "mux.h"
Paul Walmsley63c85232009-09-03 20:14:03 +0300156
Benoît Cousson5365efb2010-09-21 10:34:11 -0600157/* Maximum microseconds to wait for OMAP module to softreset */
158#define MAX_MODULE_SOFTRESET_WAIT 10000
Paul Walmsley63c85232009-09-03 20:14:03 +0300159
160/* Name of the OMAP hwmod for the MPU */
Benoit Cousson5c2c0292010-05-20 12:31:10 -0600161#define MPU_INITIATOR_NAME "mpu"
Paul Walmsley63c85232009-09-03 20:14:03 +0300162
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600163/*
164 * Number of struct omap_hwmod_link records per struct
165 * omap_hwmod_ocp_if record (master->slave and slave->master)
166 */
167#define LINKS_PER_OCP_IF 2
168
Paul Walmsley63c85232009-09-03 20:14:03 +0300169/* omap_hwmod_list contains all registered struct omap_hwmods */
170static LIST_HEAD(omap_hwmod_list);
171
Paul Walmsley63c85232009-09-03 20:14:03 +0300172/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
173static struct omap_hwmod *mpu_oh;
174
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600175/*
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600176 * linkspace: ptr to a buffer that struct omap_hwmod_link records are
177 * allocated from - used to reduce the number of small memory
178 * allocations, which has a significant impact on performance
179 */
180static struct omap_hwmod_link *linkspace;
181
182/*
183 * free_ls, max_ls: array indexes into linkspace; representing the
184 * next free struct omap_hwmod_link index, and the maximum number of
185 * struct omap_hwmod_link records allocated (respectively)
186 */
187static unsigned short free_ls, max_ls, ls_supp;
Paul Walmsley63c85232009-09-03 20:14:03 +0300188
189/* Private functions */
190
191/**
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600192 * _fetch_next_ocp_if - return the next OCP interface in a list
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600193 * @p: ptr to a ptr to the list_head inside the ocp_if to return
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600194 * @i: pointer to the index of the element pointed to by @p in the list
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600195 *
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600196 * Return a pointer to the struct omap_hwmod_ocp_if record
197 * containing the struct list_head pointed to by @p, and increment
198 * @p such that a future call to this routine will return the next
199 * record.
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600200 */
201static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600202 int *i)
203{
204 struct omap_hwmod_ocp_if *oi;
205
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600206 oi = list_entry(*p, struct omap_hwmod_link, node)->ocp_if;
207 *p = (*p)->next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600208
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600209 *i = *i + 1;
210
211 return oi;
212}
213
214/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300215 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
216 * @oh: struct omap_hwmod *
217 *
218 * Load the current value of the hwmod OCP_SYSCONFIG register into the
219 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
220 * OCP_SYSCONFIG register or 0 upon success.
221 */
222static int _update_sysc_cache(struct omap_hwmod *oh)
223{
Paul Walmsley43b40992010-02-22 22:09:34 -0700224 if (!oh->class->sysc) {
225 WARN(1, "omap_hwmod: %s: cannot read OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +0300226 return -EINVAL;
227 }
228
229 /* XXX ensure module interface clock is up */
230
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -0700231 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300232
Paul Walmsley43b40992010-02-22 22:09:34 -0700233 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
Thara Gopinath883edfd2010-01-19 17:30:51 -0700234 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300235
236 return 0;
237}
238
239/**
240 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
241 * @v: OCP_SYSCONFIG value to write
242 * @oh: struct omap_hwmod *
243 *
Paul Walmsley43b40992010-02-22 22:09:34 -0700244 * Write @v into the module class' OCP_SYSCONFIG register, if it has
245 * one. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +0300246 */
247static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
248{
Paul Walmsley43b40992010-02-22 22:09:34 -0700249 if (!oh->class->sysc) {
250 WARN(1, "omap_hwmod: %s: cannot write OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +0300251 return;
252 }
253
254 /* XXX ensure module interface clock is up */
255
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700256 /* Module might have lost context, always update cache and register */
257 oh->_sysc_cache = v;
258 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300259}
260
261/**
262 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
263 * @oh: struct omap_hwmod *
264 * @standbymode: MIDLEMODE field bits
265 * @v: pointer to register contents to modify
266 *
267 * Update the master standby mode bits in @v to be @standbymode for
268 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
269 * upon error or 0 upon success.
270 */
271static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
272 u32 *v)
273{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700274 u32 mstandby_mask;
275 u8 mstandby_shift;
276
Paul Walmsley43b40992010-02-22 22:09:34 -0700277 if (!oh->class->sysc ||
278 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300279 return -EINVAL;
280
Paul Walmsley43b40992010-02-22 22:09:34 -0700281 if (!oh->class->sysc->sysc_fields) {
282 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700283 return -EINVAL;
284 }
285
Paul Walmsley43b40992010-02-22 22:09:34 -0700286 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700287 mstandby_mask = (0x3 << mstandby_shift);
288
289 *v &= ~mstandby_mask;
290 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300291
292 return 0;
293}
294
295/**
296 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
297 * @oh: struct omap_hwmod *
298 * @idlemode: SIDLEMODE field bits
299 * @v: pointer to register contents to modify
300 *
301 * Update the slave idle mode bits in @v to be @idlemode for the @oh
302 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
303 * or 0 upon success.
304 */
305static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
306{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700307 u32 sidle_mask;
308 u8 sidle_shift;
309
Paul Walmsley43b40992010-02-22 22:09:34 -0700310 if (!oh->class->sysc ||
311 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300312 return -EINVAL;
313
Paul Walmsley43b40992010-02-22 22:09:34 -0700314 if (!oh->class->sysc->sysc_fields) {
315 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700316 return -EINVAL;
317 }
318
Paul Walmsley43b40992010-02-22 22:09:34 -0700319 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700320 sidle_mask = (0x3 << sidle_shift);
321
322 *v &= ~sidle_mask;
323 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300324
325 return 0;
326}
327
328/**
329 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
330 * @oh: struct omap_hwmod *
331 * @clockact: CLOCKACTIVITY field bits
332 * @v: pointer to register contents to modify
333 *
334 * Update the clockactivity mode bits in @v to be @clockact for the
335 * @oh hwmod. Used for additional powersaving on some modules. Does
336 * not write to the hardware. Returns -EINVAL upon error or 0 upon
337 * success.
338 */
339static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
340{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700341 u32 clkact_mask;
342 u8 clkact_shift;
343
Paul Walmsley43b40992010-02-22 22:09:34 -0700344 if (!oh->class->sysc ||
345 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300346 return -EINVAL;
347
Paul Walmsley43b40992010-02-22 22:09:34 -0700348 if (!oh->class->sysc->sysc_fields) {
349 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700350 return -EINVAL;
351 }
352
Paul Walmsley43b40992010-02-22 22:09:34 -0700353 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700354 clkact_mask = (0x3 << clkact_shift);
355
356 *v &= ~clkact_mask;
357 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300358
359 return 0;
360}
361
362/**
363 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
364 * @oh: struct omap_hwmod *
365 * @v: pointer to register contents to modify
366 *
367 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
368 * error or 0 upon success.
369 */
370static int _set_softreset(struct omap_hwmod *oh, u32 *v)
371{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700372 u32 softrst_mask;
373
Paul Walmsley43b40992010-02-22 22:09:34 -0700374 if (!oh->class->sysc ||
375 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300376 return -EINVAL;
377
Paul Walmsley43b40992010-02-22 22:09:34 -0700378 if (!oh->class->sysc->sysc_fields) {
379 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700380 return -EINVAL;
381 }
382
Paul Walmsley43b40992010-02-22 22:09:34 -0700383 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700384
385 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300386
387 return 0;
388}
389
390/**
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -0600391 * _set_dmadisable: set OCP_SYSCONFIG.DMADISABLE bit in @v
392 * @oh: struct omap_hwmod *
393 *
394 * The DMADISABLE bit is a semi-automatic bit present in sysconfig register
395 * of some modules. When the DMA must perform read/write accesses, the
396 * DMADISABLE bit is cleared by the hardware. But when the DMA must stop
397 * for power management, software must set the DMADISABLE bit back to 1.
398 *
399 * Set the DMADISABLE bit in @v for hwmod @oh. Returns -EINVAL upon
400 * error or 0 upon success.
401 */
402static int _set_dmadisable(struct omap_hwmod *oh)
403{
404 u32 v;
405 u32 dmadisable_mask;
406
407 if (!oh->class->sysc ||
408 !(oh->class->sysc->sysc_flags & SYSC_HAS_DMADISABLE))
409 return -EINVAL;
410
411 if (!oh->class->sysc->sysc_fields) {
412 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
413 return -EINVAL;
414 }
415
416 /* clocks must be on for this operation */
417 if (oh->_state != _HWMOD_STATE_ENABLED) {
418 pr_warn("omap_hwmod: %s: dma can be disabled only from enabled state\n", oh->name);
419 return -EINVAL;
420 }
421
422 pr_debug("omap_hwmod: %s: setting DMADISABLE\n", oh->name);
423
424 v = oh->_sysc_cache;
425 dmadisable_mask =
426 (0x1 << oh->class->sysc->sysc_fields->dmadisable_shift);
427 v |= dmadisable_mask;
428 _write_sysconfig(v, oh);
429
430 return 0;
431}
432
433/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700434 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
435 * @oh: struct omap_hwmod *
436 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
437 * @v: pointer to register contents to modify
438 *
439 * Update the module autoidle bit in @v to be @autoidle for the @oh
440 * hwmod. The autoidle bit controls whether the module can gate
441 * internal clocks automatically when it isn't doing anything; the
442 * exact function of this bit varies on a per-module basis. This
443 * function does not write to the hardware. Returns -EINVAL upon
444 * error or 0 upon success.
445 */
446static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
447 u32 *v)
448{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700449 u32 autoidle_mask;
450 u8 autoidle_shift;
451
Paul Walmsley43b40992010-02-22 22:09:34 -0700452 if (!oh->class->sysc ||
453 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700454 return -EINVAL;
455
Paul Walmsley43b40992010-02-22 22:09:34 -0700456 if (!oh->class->sysc->sysc_fields) {
457 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700458 return -EINVAL;
459 }
460
Paul Walmsley43b40992010-02-22 22:09:34 -0700461 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700462 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700463
464 *v &= ~autoidle_mask;
465 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700466
467 return 0;
468}
469
470/**
Govindraj Receec002011-12-16 14:36:58 -0700471 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
472 * @oh: struct omap_hwmod *
473 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
474 *
475 * Set or clear the I/O pad wakeup flag in the mux entries for the
476 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
477 * in memory. If the hwmod is currently idled, and the new idle
478 * values don't match the previous ones, this function will also
479 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
480 * currently idled, this function won't touch the hardware: the new
481 * mux settings are written to the SCM PADCTRL registers when the
482 * hwmod is idled. No return value.
483 */
484static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
485{
486 struct omap_device_pad *pad;
487 bool change = false;
488 u16 prev_idle;
489 int j;
490
491 if (!oh->mux || !oh->mux->enabled)
492 return;
493
494 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
495 pad = oh->mux->pads_dynamic[j];
496
497 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
498 continue;
499
500 prev_idle = pad->idle;
501
502 if (set_wake)
503 pad->idle |= OMAP_WAKEUP_EN;
504 else
505 pad->idle &= ~OMAP_WAKEUP_EN;
506
507 if (prev_idle != pad->idle)
508 change = true;
509 }
510
511 if (change && oh->_state == _HWMOD_STATE_IDLE)
512 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
513}
514
515/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300516 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
517 * @oh: struct omap_hwmod *
518 *
519 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
520 * upon error or 0 upon success.
521 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700522static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300523{
Paul Walmsley43b40992010-02-22 22:09:34 -0700524 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700525 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200526 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
527 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300528 return -EINVAL;
529
Paul Walmsley43b40992010-02-22 22:09:34 -0700530 if (!oh->class->sysc->sysc_fields) {
531 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700532 return -EINVAL;
533 }
534
Benoit Cousson1fe74112011-07-01 22:54:03 +0200535 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
536 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300537
Benoit Cousson86009eb2010-12-21 21:31:28 -0700538 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
539 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200540 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
541 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700542
Paul Walmsley63c85232009-09-03 20:14:03 +0300543 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
544
545 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
546
547 return 0;
548}
549
550/**
551 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
552 * @oh: struct omap_hwmod *
553 *
554 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
555 * upon error or 0 upon success.
556 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700557static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300558{
Paul Walmsley43b40992010-02-22 22:09:34 -0700559 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700560 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200561 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
562 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300563 return -EINVAL;
564
Paul Walmsley43b40992010-02-22 22:09:34 -0700565 if (!oh->class->sysc->sysc_fields) {
566 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700567 return -EINVAL;
568 }
569
Benoit Cousson1fe74112011-07-01 22:54:03 +0200570 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
571 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300572
Benoit Cousson86009eb2010-12-21 21:31:28 -0700573 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
574 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200575 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
Djamil Elaidi561038f2012-06-17 11:57:51 -0600576 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700577
Paul Walmsley63c85232009-09-03 20:14:03 +0300578 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
579
580 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
581
582 return 0;
583}
584
585/**
586 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
587 * @oh: struct omap_hwmod *
588 *
589 * Prevent the hardware module @oh from entering idle while the
590 * hardare module initiator @init_oh is active. Useful when a module
591 * will be accessed by a particular initiator (e.g., if a module will
592 * be accessed by the IVA, there should be a sleepdep between the IVA
593 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700594 * mode. If the clockdomain is marked as not needing autodeps, return
595 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
596 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300597 */
598static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
599{
600 if (!oh->_clk)
601 return -EINVAL;
602
Paul Walmsley570b54c2011-03-10 03:50:09 -0700603 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
604 return 0;
605
Paul Walmsley55ed9692010-01-26 20:12:59 -0700606 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300607}
608
609/**
610 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
611 * @oh: struct omap_hwmod *
612 *
613 * Allow the hardware module @oh to enter idle while the hardare
614 * module initiator @init_oh is active. Useful when a module will not
615 * be accessed by a particular initiator (e.g., if a module will not
616 * be accessed by the IVA, there should be no sleepdep between the IVA
617 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700618 * mode. If the clockdomain is marked as not needing autodeps, return
619 * 0 without doing anything. Returns -EINVAL upon error or passes
620 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300621 */
622static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
623{
624 if (!oh->_clk)
625 return -EINVAL;
626
Paul Walmsley570b54c2011-03-10 03:50:09 -0700627 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
628 return 0;
629
Paul Walmsley55ed9692010-01-26 20:12:59 -0700630 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300631}
632
633/**
634 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
635 * @oh: struct omap_hwmod *
636 *
637 * Called from _init_clocks(). Populates the @oh _clk (main
638 * functional clock pointer) if a main_clk is present. Returns 0 on
639 * success or -EINVAL on error.
640 */
641static int _init_main_clk(struct omap_hwmod *oh)
642{
Paul Walmsley63c85232009-09-03 20:14:03 +0300643 int ret = 0;
644
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700645 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300646 return 0;
647
Benoit Cousson63403382010-05-20 12:31:10 -0600648 oh->_clk = omap_clk_get_by_name(oh->main_clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600649 if (!oh->_clk) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600650 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
651 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600652 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600653 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300654
Benoit Cousson63403382010-05-20 12:31:10 -0600655 if (!oh->_clk->clkdm)
656 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
657 oh->main_clk, oh->_clk->name);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700658
Paul Walmsley63c85232009-09-03 20:14:03 +0300659 return ret;
660}
661
662/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600663 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300664 * @oh: struct omap_hwmod *
665 *
666 * Called from _init_clocks(). Populates the @oh OCP slave interface
667 * clock pointers. Returns 0 on success or -EINVAL on error.
668 */
669static int _init_interface_clks(struct omap_hwmod *oh)
670{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600671 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600672 struct list_head *p;
Paul Walmsley63c85232009-09-03 20:14:03 +0300673 struct clk *c;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600674 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300675 int ret = 0;
676
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600677 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600678
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600679 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600680 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700681 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300682 continue;
683
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700684 c = omap_clk_get_by_name(os->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600685 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600686 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
687 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300688 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600689 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300690 os->_clk = c;
691 }
692
693 return ret;
694}
695
696/**
697 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
698 * @oh: struct omap_hwmod *
699 *
700 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
701 * clock pointers. Returns 0 on success or -EINVAL on error.
702 */
703static int _init_opt_clks(struct omap_hwmod *oh)
704{
705 struct omap_hwmod_opt_clk *oc;
706 struct clk *c;
707 int i;
708 int ret = 0;
709
710 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700711 c = omap_clk_get_by_name(oc->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600712 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600713 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
714 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300715 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600716 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300717 oc->_clk = c;
718 }
719
720 return ret;
721}
722
723/**
724 * _enable_clocks - enable hwmod main clock and interface clocks
725 * @oh: struct omap_hwmod *
726 *
727 * Enables all clocks necessary for register reads and writes to succeed
728 * on the hwmod @oh. Returns 0.
729 */
730static int _enable_clocks(struct omap_hwmod *oh)
731{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600732 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600733 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600734 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300735
736 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
737
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600738 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300739 clk_enable(oh->_clk);
740
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600741 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600742
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600743 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600744 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300745
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600746 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
747 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300748 }
749
750 /* The opt clocks are controlled by the device driver. */
751
752 return 0;
753}
754
755/**
756 * _disable_clocks - disable hwmod main clock and interface clocks
757 * @oh: struct omap_hwmod *
758 *
759 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
760 */
761static int _disable_clocks(struct omap_hwmod *oh)
762{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600763 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600764 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600765 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300766
767 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
768
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600769 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300770 clk_disable(oh->_clk);
771
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600772 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600773
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600774 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600775 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300776
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600777 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
778 clk_disable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300779 }
780
781 /* The opt clocks are controlled by the device driver. */
782
783 return 0;
784}
785
Benoit Cousson96835af2010-09-21 18:57:58 +0200786static void _enable_optional_clocks(struct omap_hwmod *oh)
787{
788 struct omap_hwmod_opt_clk *oc;
789 int i;
790
791 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
792
793 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
794 if (oc->_clk) {
795 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
796 oc->_clk->name);
797 clk_enable(oc->_clk);
798 }
799}
800
801static void _disable_optional_clocks(struct omap_hwmod *oh)
802{
803 struct omap_hwmod_opt_clk *oc;
804 int i;
805
806 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
807
808 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
809 if (oc->_clk) {
810 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
811 oc->_clk->name);
812 clk_disable(oc->_clk);
813 }
814}
815
Paul Walmsley63c85232009-09-03 20:14:03 +0300816/**
Benoit Cousson45c38252011-07-10 05:56:33 -0600817 * _enable_module - enable CLKCTRL modulemode on OMAP4
818 * @oh: struct omap_hwmod *
819 *
820 * Enables the PRCM module mode related to the hwmod @oh.
821 * No return value.
822 */
823static void _enable_module(struct omap_hwmod *oh)
824{
825 /* The module mode does not exist prior OMAP4 */
826 if (cpu_is_omap24xx() || cpu_is_omap34xx())
827 return;
828
829 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
830 return;
831
832 pr_debug("omap_hwmod: %s: _enable_module: %d\n",
833 oh->name, oh->prcm.omap4.modulemode);
834
835 omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
836 oh->clkdm->prcm_partition,
837 oh->clkdm->cm_inst,
838 oh->clkdm->clkdm_offs,
839 oh->prcm.omap4.clkctrl_offs);
840}
841
842/**
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800843 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
844 * @oh: struct omap_hwmod *
845 *
846 * Wait for a module @oh to enter slave idle. Returns 0 if the module
847 * does not have an IDLEST bit or if the module successfully enters
848 * slave idle; otherwise, pass along the return value of the
849 * appropriate *_cm*_wait_module_idle() function.
850 */
851static int _omap4_wait_target_disable(struct omap_hwmod *oh)
852{
853 if (!cpu_is_omap44xx())
854 return 0;
855
856 if (!oh)
857 return -EINVAL;
858
859 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
860 return 0;
861
862 if (oh->flags & HWMOD_NO_IDLEST)
863 return 0;
864
865 return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
866 oh->clkdm->cm_inst,
867 oh->clkdm->clkdm_offs,
868 oh->prcm.omap4.clkctrl_offs);
869}
870
871/**
Paul Walmsley212738a2011-07-09 19:14:06 -0600872 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
873 * @oh: struct omap_hwmod *oh
874 *
875 * Count and return the number of MPU IRQs associated with the hwmod
876 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
877 * NULL.
878 */
879static int _count_mpu_irqs(struct omap_hwmod *oh)
880{
881 struct omap_hwmod_irq_info *ohii;
882 int i = 0;
883
884 if (!oh || !oh->mpu_irqs)
885 return 0;
886
887 do {
888 ohii = &oh->mpu_irqs[i++];
889 } while (ohii->irq != -1);
890
sricharancc1b0762011-11-23 14:35:07 -0800891 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -0600892}
893
894/**
Paul Walmsleybc614952011-07-09 19:14:07 -0600895 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
896 * @oh: struct omap_hwmod *oh
897 *
898 * Count and return the number of SDMA request lines associated with
899 * the hwmod @oh. Used to allocate struct resource data. Returns 0
900 * if @oh is NULL.
901 */
902static int _count_sdma_reqs(struct omap_hwmod *oh)
903{
904 struct omap_hwmod_dma_info *ohdi;
905 int i = 0;
906
907 if (!oh || !oh->sdma_reqs)
908 return 0;
909
910 do {
911 ohdi = &oh->sdma_reqs[i++];
912 } while (ohdi->dma_req != -1);
913
sricharancc1b0762011-11-23 14:35:07 -0800914 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -0600915}
916
917/**
Paul Walmsley78183f32011-07-09 19:14:05 -0600918 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
919 * @oh: struct omap_hwmod *oh
920 *
921 * Count and return the number of address space ranges associated with
922 * the hwmod @oh. Used to allocate struct resource data. Returns 0
923 * if @oh is NULL.
924 */
925static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
926{
927 struct omap_hwmod_addr_space *mem;
928 int i = 0;
929
930 if (!os || !os->addr)
931 return 0;
932
933 do {
934 mem = &os->addr[i++];
935 } while (mem->pa_start != mem->pa_end);
936
sricharancc1b0762011-11-23 14:35:07 -0800937 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -0600938}
939
940/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -0600941 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
942 * @oh: struct omap_hwmod * to operate on
943 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
944 * @irq: pointer to an unsigned int to store the MPU IRQ number to
945 *
946 * Retrieve a MPU hardware IRQ line number named by @name associated
947 * with the IP block pointed to by @oh. The IRQ number will be filled
948 * into the address pointed to by @dma. When @name is non-null, the
949 * IRQ line number associated with the named entry will be returned.
950 * If @name is null, the first matching entry will be returned. Data
951 * order is not meaningful in hwmod data, so callers are strongly
952 * encouraged to use a non-null @name whenever possible to avoid
953 * unpredictable effects if hwmod data is later added that causes data
954 * ordering to change. Returns 0 upon success or a negative error
955 * code upon error.
956 */
957static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
958 unsigned int *irq)
959{
960 int i;
961 bool found = false;
962
963 if (!oh->mpu_irqs)
964 return -ENOENT;
965
966 i = 0;
967 while (oh->mpu_irqs[i].irq != -1) {
968 if (name == oh->mpu_irqs[i].name ||
969 !strcmp(name, oh->mpu_irqs[i].name)) {
970 found = true;
971 break;
972 }
973 i++;
974 }
975
976 if (!found)
977 return -ENOENT;
978
979 *irq = oh->mpu_irqs[i].irq;
980
981 return 0;
982}
983
984/**
985 * _get_sdma_req_by_name - fetch SDMA request line ID by name
986 * @oh: struct omap_hwmod * to operate on
987 * @name: pointer to the name of the SDMA request line to fetch (optional)
988 * @dma: pointer to an unsigned int to store the request line ID to
989 *
990 * Retrieve an SDMA request line ID named by @name on the IP block
991 * pointed to by @oh. The ID will be filled into the address pointed
992 * to by @dma. When @name is non-null, the request line ID associated
993 * with the named entry will be returned. If @name is null, the first
994 * matching entry will be returned. Data order is not meaningful in
995 * hwmod data, so callers are strongly encouraged to use a non-null
996 * @name whenever possible to avoid unpredictable effects if hwmod
997 * data is later added that causes data ordering to change. Returns 0
998 * upon success or a negative error code upon error.
999 */
1000static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
1001 unsigned int *dma)
1002{
1003 int i;
1004 bool found = false;
1005
1006 if (!oh->sdma_reqs)
1007 return -ENOENT;
1008
1009 i = 0;
1010 while (oh->sdma_reqs[i].dma_req != -1) {
1011 if (name == oh->sdma_reqs[i].name ||
1012 !strcmp(name, oh->sdma_reqs[i].name)) {
1013 found = true;
1014 break;
1015 }
1016 i++;
1017 }
1018
1019 if (!found)
1020 return -ENOENT;
1021
1022 *dma = oh->sdma_reqs[i].dma_req;
1023
1024 return 0;
1025}
1026
1027/**
1028 * _get_addr_space_by_name - fetch address space start & end by name
1029 * @oh: struct omap_hwmod * to operate on
1030 * @name: pointer to the name of the address space to fetch (optional)
1031 * @pa_start: pointer to a u32 to store the starting address to
1032 * @pa_end: pointer to a u32 to store the ending address to
1033 *
1034 * Retrieve address space start and end addresses for the IP block
1035 * pointed to by @oh. The data will be filled into the addresses
1036 * pointed to by @pa_start and @pa_end. When @name is non-null, the
1037 * address space data associated with the named entry will be
1038 * returned. If @name is null, the first matching entry will be
1039 * returned. Data order is not meaningful in hwmod data, so callers
1040 * are strongly encouraged to use a non-null @name whenever possible
1041 * to avoid unpredictable effects if hwmod data is later added that
1042 * causes data ordering to change. Returns 0 upon success or a
1043 * negative error code upon error.
1044 */
1045static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1046 u32 *pa_start, u32 *pa_end)
1047{
1048 int i, j;
1049 struct omap_hwmod_ocp_if *os;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001050 struct list_head *p = NULL;
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001051 bool found = false;
1052
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001053 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001054
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001055 i = 0;
1056 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001057 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001058
1059 if (!os->addr)
1060 return -ENOENT;
1061
1062 j = 0;
1063 while (os->addr[j].pa_start != os->addr[j].pa_end) {
1064 if (name == os->addr[j].name ||
1065 !strcmp(name, os->addr[j].name)) {
1066 found = true;
1067 break;
1068 }
1069 j++;
1070 }
1071
1072 if (found)
1073 break;
1074 }
1075
1076 if (!found)
1077 return -ENOENT;
1078
1079 *pa_start = os->addr[j].pa_start;
1080 *pa_end = os->addr[j].pa_end;
1081
1082 return 0;
1083}
1084
1085/**
Paul Walmsley24dbc212012-04-19 04:04:29 -06001086 * _save_mpu_port_index - find and save the index to @oh's MPU port
Paul Walmsley63c85232009-09-03 20:14:03 +03001087 * @oh: struct omap_hwmod *
1088 *
Paul Walmsley24dbc212012-04-19 04:04:29 -06001089 * Determines the array index of the OCP slave port that the MPU uses
1090 * to address the device, and saves it into the struct omap_hwmod.
1091 * Intended to be called during hwmod registration only. No return
1092 * value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001093 */
Paul Walmsley24dbc212012-04-19 04:04:29 -06001094static void __init _save_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001095{
Paul Walmsley24dbc212012-04-19 04:04:29 -06001096 struct omap_hwmod_ocp_if *os = NULL;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001097 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001098 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001099
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001100 if (!oh)
Paul Walmsley24dbc212012-04-19 04:04:29 -06001101 return;
1102
1103 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001104
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001105 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001106
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001107 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001108 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +03001109 if (os->user & OCP_USER_MPU) {
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001110 oh->_mpu_port = os;
Paul Walmsley24dbc212012-04-19 04:04:29 -06001111 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001112 break;
1113 }
1114 }
1115
Paul Walmsley24dbc212012-04-19 04:04:29 -06001116 return;
Paul Walmsley63c85232009-09-03 20:14:03 +03001117}
1118
1119/**
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001120 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1121 * @oh: struct omap_hwmod *
1122 *
1123 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1124 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1125 * communicate with the IP block. This interface need not be directly
1126 * connected to the MPU (and almost certainly is not), but is directly
1127 * connected to the IP block represented by @oh. Returns a pointer
1128 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1129 * error or if there does not appear to be a path from the MPU to this
1130 * IP block.
1131 */
1132static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1133{
1134 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1135 return NULL;
1136
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001137 return oh->_mpu_port;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001138};
1139
1140/**
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001141 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
Paul Walmsley63c85232009-09-03 20:14:03 +03001142 * @oh: struct omap_hwmod *
1143 *
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001144 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1145 * the register target MPU address space; or returns NULL upon error.
Paul Walmsley63c85232009-09-03 20:14:03 +03001146 */
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001147static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001148{
1149 struct omap_hwmod_ocp_if *os;
1150 struct omap_hwmod_addr_space *mem;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001151 int found = 0, i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001152
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001153 os = _find_mpu_rt_port(oh);
Paul Walmsley24dbc212012-04-19 04:04:29 -06001154 if (!os || !os->addr)
Paul Walmsley78183f32011-07-09 19:14:05 -06001155 return NULL;
1156
1157 do {
1158 mem = &os->addr[i++];
1159 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +03001160 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001161 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +03001162
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001163 return (found) ? mem : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001164}
1165
1166/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001167 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001168 * @oh: struct omap_hwmod *
1169 *
1170 * If module is marked as SWSUP_SIDLE, force the module out of slave
1171 * idle; otherwise, configure it for smart-idle. If module is marked
1172 * as SWSUP_MSUSPEND, force the module out of master standby;
1173 * otherwise, configure it for smart-standby. No return value.
1174 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001175static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001176{
Paul Walmsley43b40992010-02-22 22:09:34 -07001177 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001178 u32 v;
1179
Paul Walmsley43b40992010-02-22 22:09:34 -07001180 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001181 return;
1182
1183 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001184 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001185
Paul Walmsley43b40992010-02-22 22:09:34 -07001186 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001187 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1188 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
1189 _set_slave_idlemode(oh, idlemode, &v);
1190 }
1191
Paul Walmsley43b40992010-02-22 22:09:34 -07001192 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001193 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1194 idlemode = HWMOD_IDLEMODE_NO;
1195 } else {
1196 if (sf & SYSC_HAS_ENAWAKEUP)
1197 _enable_wakeup(oh, &v);
1198 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1199 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1200 else
1201 idlemode = HWMOD_IDLEMODE_SMART;
1202 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001203 _set_master_standbymode(oh, idlemode, &v);
1204 }
1205
Paul Walmsleya16b1f72009-12-08 16:34:17 -07001206 /*
1207 * XXX The clock framework should handle this, by
1208 * calling into this code. But this must wait until the
1209 * clock structures are tagged with omap_hwmod entries
1210 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001211 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1212 (sf & SYSC_HAS_CLOCKACTIVITY))
1213 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001214
Rajendra Nayak9980ce52010-09-21 19:58:30 +05301215 /* If slave is in SMARTIDLE, also enable wakeup */
1216 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07001217 _enable_wakeup(oh, &v);
1218
1219 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -06001220
1221 /*
1222 * Set the autoidle bit only after setting the smartidle bit
1223 * Setting this will not have any impact on the other modules.
1224 */
1225 if (sf & SYSC_HAS_AUTOIDLE) {
1226 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1227 0 : 1;
1228 _set_module_autoidle(oh, idlemode, &v);
1229 _write_sysconfig(v, oh);
1230 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001231}
1232
1233/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001234 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001235 * @oh: struct omap_hwmod *
1236 *
1237 * If module is marked as SWSUP_SIDLE, force the module into slave
1238 * idle; otherwise, configure it for smart-idle. If module is marked
1239 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1240 * configure it for smart-standby. No return value.
1241 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001242static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001243{
Paul Walmsley43b40992010-02-22 22:09:34 -07001244 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001245 u32 v;
1246
Paul Walmsley43b40992010-02-22 22:09:34 -07001247 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001248 return;
1249
1250 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001251 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001252
Paul Walmsley43b40992010-02-22 22:09:34 -07001253 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001254 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1255 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
1256 _set_slave_idlemode(oh, idlemode, &v);
1257 }
1258
Paul Walmsley43b40992010-02-22 22:09:34 -07001259 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001260 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1261 idlemode = HWMOD_IDLEMODE_FORCE;
1262 } else {
1263 if (sf & SYSC_HAS_ENAWAKEUP)
1264 _enable_wakeup(oh, &v);
1265 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1266 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1267 else
1268 idlemode = HWMOD_IDLEMODE_SMART;
1269 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001270 _set_master_standbymode(oh, idlemode, &v);
1271 }
1272
Benoit Cousson86009eb2010-12-21 21:31:28 -07001273 /* If slave is in SMARTIDLE, also enable wakeup */
1274 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1275 _enable_wakeup(oh, &v);
1276
Paul Walmsley63c85232009-09-03 20:14:03 +03001277 _write_sysconfig(v, oh);
1278}
1279
1280/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001281 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001282 * @oh: struct omap_hwmod *
1283 *
1284 * Force the module into slave idle and master suspend. No return
1285 * value.
1286 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001287static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001288{
1289 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001290 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001291
Paul Walmsley43b40992010-02-22 22:09:34 -07001292 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001293 return;
1294
1295 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001296 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001297
Paul Walmsley43b40992010-02-22 22:09:34 -07001298 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001299 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1300
Paul Walmsley43b40992010-02-22 22:09:34 -07001301 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001302 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1303
Paul Walmsley43b40992010-02-22 22:09:34 -07001304 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001305 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001306
1307 _write_sysconfig(v, oh);
1308}
1309
1310/**
1311 * _lookup - find an omap_hwmod by name
1312 * @name: find an omap_hwmod by name
1313 *
1314 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001315 */
1316static struct omap_hwmod *_lookup(const char *name)
1317{
1318 struct omap_hwmod *oh, *temp_oh;
1319
1320 oh = NULL;
1321
1322 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1323 if (!strcmp(name, temp_oh->name)) {
1324 oh = temp_oh;
1325 break;
1326 }
1327 }
1328
1329 return oh;
1330}
Benoit Cousson6ae76992011-07-10 05:56:30 -06001331/**
1332 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1333 * @oh: struct omap_hwmod *
1334 *
1335 * Convert a clockdomain name stored in a struct omap_hwmod into a
1336 * clockdomain pointer, and save it into the struct omap_hwmod.
1337 * return -EINVAL if clkdm_name does not exist or if the lookup failed.
1338 */
1339static int _init_clkdm(struct omap_hwmod *oh)
1340{
1341 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1342 return 0;
1343
1344 if (!oh->clkdm_name) {
1345 pr_warning("omap_hwmod: %s: no clkdm_name\n", oh->name);
1346 return -EINVAL;
1347 }
1348
1349 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1350 if (!oh->clkdm) {
1351 pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
1352 oh->name, oh->clkdm_name);
1353 return -EINVAL;
1354 }
1355
1356 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1357 oh->name, oh->clkdm_name);
1358
1359 return 0;
1360}
Paul Walmsley63c85232009-09-03 20:14:03 +03001361
1362/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001363 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1364 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001365 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -06001366 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001367 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001368 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001369 * Resolves all clock names embedded in the hwmod. Returns 0 on
1370 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001371 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001372static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001373{
1374 int ret = 0;
1375
Paul Walmsley48d54f32011-02-23 00:14:07 -07001376 if (oh->_state != _HWMOD_STATE_REGISTERED)
1377 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001378
1379 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1380
1381 ret |= _init_main_clk(oh);
1382 ret |= _init_interface_clks(oh);
1383 ret |= _init_opt_clks(oh);
Benoit Cousson6ae76992011-07-10 05:56:30 -06001384 ret |= _init_clkdm(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001385
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001386 if (!ret)
1387 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001388 else
1389 pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001390
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001391 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001392}
1393
1394/**
1395 * _wait_target_ready - wait for a module to leave slave idle
1396 * @oh: struct omap_hwmod *
1397 *
1398 * Wait for a module @oh to leave slave idle. Returns 0 if the module
1399 * does not have an IDLEST bit or if the module successfully leaves
1400 * slave idle; otherwise, pass along the return value of the
Benoit Coussond0f06312011-07-10 05:56:30 -06001401 * appropriate *_cm*_wait_module_ready() function.
Paul Walmsley63c85232009-09-03 20:14:03 +03001402 */
1403static int _wait_target_ready(struct omap_hwmod *oh)
1404{
1405 struct omap_hwmod_ocp_if *os;
1406 int ret;
1407
1408 if (!oh)
1409 return -EINVAL;
1410
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001411 if (oh->flags & HWMOD_NO_IDLEST)
Paul Walmsley63c85232009-09-03 20:14:03 +03001412 return 0;
1413
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001414 os = _find_mpu_rt_port(oh);
1415 if (!os)
Paul Walmsley63c85232009-09-03 20:14:03 +03001416 return 0;
1417
1418 /* XXX check module SIDLEMODE */
1419
1420 /* XXX check clock enable states */
1421
1422 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1423 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
1424 oh->prcm.omap2.idlest_reg_id,
1425 oh->prcm.omap2.idlest_idle_bit);
Paul Walmsley63c85232009-09-03 20:14:03 +03001426 } else if (cpu_is_omap44xx()) {
Benoit Coussond0f06312011-07-10 05:56:30 -06001427 if (!oh->clkdm)
1428 return -EINVAL;
1429
1430 ret = omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
1431 oh->clkdm->cm_inst,
1432 oh->clkdm->clkdm_offs,
1433 oh->prcm.omap4.clkctrl_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001434 } else {
1435 BUG();
1436 };
1437
1438 return ret;
1439}
1440
1441/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001442 * _lookup_hardreset - fill register bit info for this hwmod/reset line
Benoît Cousson5365efb2010-09-21 10:34:11 -06001443 * @oh: struct omap_hwmod *
1444 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001445 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
Benoît Cousson5365efb2010-09-21 10:34:11 -06001446 *
1447 * Return the bit position of the reset line that match the
1448 * input name. Return -ENOENT if not found.
1449 */
omar ramirezcc1226e2011-03-04 13:32:44 -07001450static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1451 struct omap_hwmod_rst_info *ohri)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001452{
1453 int i;
1454
1455 for (i = 0; i < oh->rst_lines_cnt; i++) {
1456 const char *rst_line = oh->rst_lines[i].name;
1457 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001458 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1459 ohri->st_shift = oh->rst_lines[i].st_shift;
1460 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1461 oh->name, __func__, rst_line, ohri->rst_shift,
1462 ohri->st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001463
omar ramirezcc1226e2011-03-04 13:32:44 -07001464 return 0;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001465 }
1466 }
1467
1468 return -ENOENT;
1469}
1470
1471/**
1472 * _assert_hardreset - assert the HW reset line of submodules
1473 * contained in the hwmod module.
1474 * @oh: struct omap_hwmod *
1475 * @name: name of the reset line to lookup and assert
1476 *
1477 * Some IP like dsp, ipu or iva contain processor that require
1478 * an HW reset line to be assert / deassert in order to enable fully
1479 * the IP.
1480 */
1481static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1482{
omar ramirezcc1226e2011-03-04 13:32:44 -07001483 struct omap_hwmod_rst_info ohri;
1484 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001485
1486 if (!oh)
1487 return -EINVAL;
1488
omar ramirezcc1226e2011-03-04 13:32:44 -07001489 ret = _lookup_hardreset(oh, name, &ohri);
1490 if (IS_ERR_VALUE(ret))
1491 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001492
1493 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1494 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001495 ohri.rst_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001496 else if (cpu_is_omap44xx())
Benoit Coussoneaac3292011-07-10 05:56:31 -06001497 return omap4_prminst_assert_hardreset(ohri.rst_shift,
1498 oh->clkdm->pwrdm.ptr->prcm_partition,
1499 oh->clkdm->pwrdm.ptr->prcm_offs,
1500 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001501 else
1502 return -EINVAL;
1503}
1504
1505/**
1506 * _deassert_hardreset - deassert the HW reset line of submodules contained
1507 * in the hwmod module.
1508 * @oh: struct omap_hwmod *
1509 * @name: name of the reset line to look up and deassert
1510 *
1511 * Some IP like dsp, ipu or iva contain processor that require
1512 * an HW reset line to be assert / deassert in order to enable fully
1513 * the IP.
1514 */
1515static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1516{
omar ramirezcc1226e2011-03-04 13:32:44 -07001517 struct omap_hwmod_rst_info ohri;
1518 int ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001519
1520 if (!oh)
1521 return -EINVAL;
1522
omar ramirezcc1226e2011-03-04 13:32:44 -07001523 ret = _lookup_hardreset(oh, name, &ohri);
1524 if (IS_ERR_VALUE(ret))
1525 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001526
omar ramirezcc1226e2011-03-04 13:32:44 -07001527 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1528 ret = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
1529 ohri.rst_shift,
1530 ohri.st_shift);
1531 } else if (cpu_is_omap44xx()) {
1532 if (ohri.st_shift)
1533 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
1534 oh->name, name);
Benoit Coussoneaac3292011-07-10 05:56:31 -06001535 ret = omap4_prminst_deassert_hardreset(ohri.rst_shift,
1536 oh->clkdm->pwrdm.ptr->prcm_partition,
1537 oh->clkdm->pwrdm.ptr->prcm_offs,
1538 oh->prcm.omap4.rstctrl_offs);
omar ramirezcc1226e2011-03-04 13:32:44 -07001539 } else {
Benoît Cousson5365efb2010-09-21 10:34:11 -06001540 return -EINVAL;
omar ramirezcc1226e2011-03-04 13:32:44 -07001541 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001542
omar ramirezcc1226e2011-03-04 13:32:44 -07001543 if (ret == -EBUSY)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001544 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1545
omar ramirezcc1226e2011-03-04 13:32:44 -07001546 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001547}
1548
1549/**
1550 * _read_hardreset - read the HW reset line state of submodules
1551 * contained in the hwmod module
1552 * @oh: struct omap_hwmod *
1553 * @name: name of the reset line to look up and read
1554 *
1555 * Return the state of the reset line.
1556 */
1557static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1558{
omar ramirezcc1226e2011-03-04 13:32:44 -07001559 struct omap_hwmod_rst_info ohri;
1560 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001561
1562 if (!oh)
1563 return -EINVAL;
1564
omar ramirezcc1226e2011-03-04 13:32:44 -07001565 ret = _lookup_hardreset(oh, name, &ohri);
1566 if (IS_ERR_VALUE(ret))
1567 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001568
1569 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1570 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001571 ohri.st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001572 } else if (cpu_is_omap44xx()) {
Benoit Coussoneaac3292011-07-10 05:56:31 -06001573 return omap4_prminst_is_hardreset_asserted(ohri.rst_shift,
1574 oh->clkdm->pwrdm.ptr->prcm_partition,
1575 oh->clkdm->pwrdm.ptr->prcm_offs,
1576 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001577 } else {
1578 return -EINVAL;
1579 }
1580}
1581
1582/**
Paul Walmsley747834a2012-04-18 19:10:04 -06001583 * _are_any_hardreset_lines_asserted - return true if part of @oh is hard-reset
1584 * @oh: struct omap_hwmod *
1585 *
1586 * If any hardreset line associated with @oh is asserted, then return true.
1587 * Otherwise, if @oh has no hardreset lines associated with it, or if
1588 * no hardreset lines associated with @oh are asserted, then return false.
1589 * This function is used to avoid executing some parts of the IP block
1590 * enable/disable sequence if a hardreset line is set.
1591 */
1592static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1593{
1594 int i;
1595
1596 if (oh->rst_lines_cnt == 0)
1597 return false;
1598
1599 for (i = 0; i < oh->rst_lines_cnt; i++)
1600 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1601 return true;
1602
1603 return false;
1604}
1605
1606/**
1607 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1608 * @oh: struct omap_hwmod *
1609 *
1610 * Disable the PRCM module mode related to the hwmod @oh.
1611 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1612 */
1613static int _omap4_disable_module(struct omap_hwmod *oh)
1614{
1615 int v;
1616
1617 /* The module mode does not exist prior OMAP4 */
1618 if (!cpu_is_omap44xx())
1619 return -EINVAL;
1620
1621 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1622 return -EINVAL;
1623
1624 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1625
1626 omap4_cminst_module_disable(oh->clkdm->prcm_partition,
1627 oh->clkdm->cm_inst,
1628 oh->clkdm->clkdm_offs,
1629 oh->prcm.omap4.clkctrl_offs);
1630
1631 if (_are_any_hardreset_lines_asserted(oh))
1632 return 0;
1633
1634 v = _omap4_wait_target_disable(oh);
1635 if (v)
1636 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1637 oh->name);
1638
1639 return 0;
1640}
1641
1642/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001643 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001644 * @oh: struct omap_hwmod *
1645 *
1646 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Paul Walmsley30e105c2012-04-19 00:49:09 -06001647 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1648 * reset this way, -EINVAL if the hwmod is in the wrong state,
1649 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001650 *
1651 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001652 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001653 * use the SYSCONFIG softreset bit to provide the status.
1654 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001655 * Note that some IP like McBSP do have reset control but don't have
1656 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001657 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001658static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001659{
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001660 u32 v, softrst_mask;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001661 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001662 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001663
Paul Walmsley43b40992010-02-22 22:09:34 -07001664 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001665 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley30e105c2012-04-19 00:49:09 -06001666 return -ENOENT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001667
1668 /* clocks must be on for this operation */
1669 if (oh->_state != _HWMOD_STATE_ENABLED) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001670 pr_warning("omap_hwmod: %s: reset can only be entered from "
1671 "enabled state\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001672 return -EINVAL;
1673 }
1674
Benoit Cousson96835af2010-09-21 18:57:58 +02001675 /* For some modules, all optionnal clocks need to be enabled as well */
1676 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1677 _enable_optional_clocks(oh);
1678
Paul Walmsleybd361792010-12-14 12:42:35 -07001679 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001680
1681 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001682 ret = _set_softreset(oh, &v);
1683 if (ret)
1684 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001685 _write_sysconfig(v, oh);
1686
Fernando Guzman Lugod99de7f2012-04-13 05:08:03 -06001687 if (oh->class->sysc->srst_udelay)
1688 udelay(oh->class->sysc->srst_udelay);
1689
Benoit Cousson2cb06812010-09-21 18:57:59 +02001690 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001691 omap_test_timeout((omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001692 oh->class->sysc->syss_offs)
1693 & SYSS_RESETDONE_MASK),
1694 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001695 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
1696 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001697 omap_test_timeout(!(omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001698 oh->class->sysc->sysc_offs)
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001699 & softrst_mask),
Benoit Cousson2cb06812010-09-21 18:57:59 +02001700 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001701 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001702
Benoît Cousson5365efb2010-09-21 10:34:11 -06001703 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001704 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1705 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001706 else
Benoît Cousson5365efb2010-09-21 10:34:11 -06001707 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001708
1709 /*
1710 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1711 * _wait_target_ready() or _reset()
1712 */
1713
Benoit Cousson96835af2010-09-21 18:57:58 +02001714 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1715
1716dis_opt_clks:
1717 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1718 _disable_optional_clocks(oh);
1719
1720 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001721}
1722
1723/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001724 * _reset - reset an omap_hwmod
1725 * @oh: struct omap_hwmod *
1726 *
Paul Walmsley30e105c2012-04-19 00:49:09 -06001727 * Resets an omap_hwmod @oh. If the module has a custom reset
1728 * function pointer defined, then call it to reset the IP block, and
1729 * pass along its return value to the caller. Otherwise, if the IP
1730 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1731 * associated with it, call a function to reset the IP block via that
1732 * method, and pass along the return value to the caller. Finally, if
1733 * the IP block has some hardreset lines associated with it, assert
1734 * all of those, but do _not_ deassert them. (This is because driver
1735 * authors have expressed an apparent requirement to control the
1736 * deassertion of the hardreset lines themselves.)
1737 *
1738 * The default software reset mechanism for most OMAP IP blocks is
1739 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1740 * hwmods cannot be reset via this method. Some are not targets and
1741 * therefore have no OCP header registers to access. Others (like the
1742 * IVA) have idiosyncratic reset sequences. So for these relatively
1743 * rare cases, custom reset code can be supplied in the struct
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -06001744 * omap_hwmod_class .reset function pointer.
1745 *
1746 * _set_dmadisable() is called to set the DMADISABLE bit so that it
1747 * does not prevent idling of the system. This is necessary for cases
1748 * where ROMCODE/BOOTLOADER uses dma and transfers control to the
1749 * kernel without disabling dma.
1750 *
1751 * Passes along the return value from either _ocp_softreset() or the
1752 * custom reset function - these must return -EINVAL if the hwmod
1753 * cannot be reset this way or if the hwmod is in the wrong state,
1754 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Paul Walmsleybd361792010-12-14 12:42:35 -07001755 */
1756static int _reset(struct omap_hwmod *oh)
1757{
Paul Walmsley30e105c2012-04-19 00:49:09 -06001758 int i, r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001759
1760 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1761
Paul Walmsley30e105c2012-04-19 00:49:09 -06001762 if (oh->class->reset) {
1763 r = oh->class->reset(oh);
1764 } else {
1765 if (oh->rst_lines_cnt > 0) {
1766 for (i = 0; i < oh->rst_lines_cnt; i++)
1767 _assert_hardreset(oh, oh->rst_lines[i].name);
1768 return 0;
1769 } else {
1770 r = _ocp_softreset(oh);
1771 if (r == -ENOENT)
1772 r = 0;
1773 }
1774 }
Paul Walmsleybd361792010-12-14 12:42:35 -07001775
Kishon Vijay Abraham I66685462012-07-04 05:09:21 -06001776 _set_dmadisable(oh);
1777
Paul Walmsley30e105c2012-04-19 00:49:09 -06001778 /*
1779 * OCP_SYSCONFIG bits need to be reprogrammed after a
1780 * softreset. The _enable() function should be split to avoid
1781 * the rewrite of the OCP_SYSCONFIG register.
1782 */
Rajendra Nayak28008522012-03-13 22:55:23 +05301783 if (oh->class->sysc) {
1784 _update_sysc_cache(oh);
1785 _enable_sysc(oh);
1786 }
1787
Paul Walmsley30e105c2012-04-19 00:49:09 -06001788 return r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001789}
1790
1791/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001792 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001793 * @oh: struct omap_hwmod *
1794 *
1795 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001796 * register target. Returns -EINVAL if the hwmod is in the wrong
1797 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001798 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001799static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001800{
Paul Walmsley747834a2012-04-18 19:10:04 -06001801 int r;
Rajendra Nayak665d0012011-07-10 05:57:07 -06001802 int hwsup = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001803
Benoit Cousson34617e22011-07-01 22:54:07 +02001804 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1805
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001806 /*
Paul Walmsley64813c32012-04-18 19:10:03 -06001807 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
1808 * state at init. Now that someone is really trying to enable
1809 * them, just ensure that the hwmod mux is set.
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001810 */
1811 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
1812 /*
1813 * If the caller has mux data populated, do the mux'ing
1814 * which wouldn't have been done as part of the _enable()
1815 * done during setup.
1816 */
1817 if (oh->mux)
1818 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
1819
1820 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
1821 return 0;
1822 }
1823
Paul Walmsley63c85232009-09-03 20:14:03 +03001824 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1825 oh->_state != _HWMOD_STATE_IDLE &&
1826 oh->_state != _HWMOD_STATE_DISABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001827 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
1828 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001829 return -EINVAL;
1830 }
1831
Benoit Cousson31f62862011-07-01 22:54:05 +02001832 /*
Paul Walmsley747834a2012-04-18 19:10:04 -06001833 * If an IP block contains HW reset lines and any of them are
1834 * asserted, we let integration code associated with that
1835 * block handle the enable. We've received very little
1836 * information on what those driver authors need, and until
1837 * detailed information is provided and the driver code is
1838 * posted to the public lists, this is probably the best we
1839 * can do.
Benoit Cousson31f62862011-07-01 22:54:05 +02001840 */
Paul Walmsley747834a2012-04-18 19:10:04 -06001841 if (_are_any_hardreset_lines_asserted(oh))
1842 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001843
Rajendra Nayak665d0012011-07-10 05:57:07 -06001844 /* Mux pins for device runtime if populated */
1845 if (oh->mux && (!oh->mux->enabled ||
1846 ((oh->_state == _HWMOD_STATE_IDLE) &&
1847 oh->mux->pads_dynamic)))
1848 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
Benoit Cousson34617e22011-07-01 22:54:07 +02001849
Rajendra Nayak665d0012011-07-10 05:57:07 -06001850 _add_initiator_dep(oh, mpu_oh);
1851
1852 if (oh->clkdm) {
1853 /*
1854 * A clockdomain must be in SW_SUP before enabling
1855 * completely the module. The clockdomain can be set
1856 * in HW_AUTO only when the module become ready.
1857 */
1858 hwsup = clkdm_in_hwsup(oh->clkdm);
1859 r = clkdm_hwmod_enable(oh->clkdm, oh);
1860 if (r) {
1861 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1862 oh->name, oh->clkdm->name, r);
1863 return r;
1864 }
Benoit Cousson34617e22011-07-01 22:54:07 +02001865 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06001866
1867 _enable_clocks(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001868 _enable_module(oh);
Benoit Cousson34617e22011-07-01 22:54:07 +02001869
Rajendra Nayak665d0012011-07-10 05:57:07 -06001870 r = _wait_target_ready(oh);
1871 if (!r) {
1872 /*
1873 * Set the clockdomain to HW_AUTO only if the target is ready,
1874 * assuming that the previous state was HW_AUTO
1875 */
1876 if (oh->clkdm && hwsup)
1877 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02001878
Rajendra Nayak665d0012011-07-10 05:57:07 -06001879 oh->_state = _HWMOD_STATE_ENABLED;
1880
1881 /* Access the sysconfig only if the target is ready */
1882 if (oh->class->sysc) {
1883 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1884 _update_sysc_cache(oh);
1885 _enable_sysc(oh);
1886 }
1887 } else {
1888 _disable_clocks(oh);
1889 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1890 oh->name, r);
1891
1892 if (oh->clkdm)
1893 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001894 }
1895
Paul Walmsley63c85232009-09-03 20:14:03 +03001896 return r;
1897}
1898
1899/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001900 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001901 * @oh: struct omap_hwmod *
1902 *
1903 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001904 * no further work. Returns -EINVAL if the hwmod is in the wrong
1905 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001906 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001907static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001908{
Benoit Cousson34617e22011-07-01 22:54:07 +02001909 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1910
Paul Walmsley63c85232009-09-03 20:14:03 +03001911 if (oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001912 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
1913 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001914 return -EINVAL;
1915 }
1916
Paul Walmsley747834a2012-04-18 19:10:04 -06001917 if (_are_any_hardreset_lines_asserted(oh))
1918 return 0;
1919
Paul Walmsley43b40992010-02-22 22:09:34 -07001920 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001921 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001922 _del_initiator_dep(oh, mpu_oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001923
1924 _omap4_disable_module(oh);
1925
Benoit Cousson45c38252011-07-10 05:56:33 -06001926 /*
1927 * The module must be in idle mode before disabling any parents
1928 * clocks. Otherwise, the parent clock might be disabled before
1929 * the module transition is done, and thus will prevent the
1930 * transition to complete properly.
1931 */
1932 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001933 if (oh->clkdm)
1934 clkdm_hwmod_disable(oh->clkdm, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001935
Tony Lindgren8d9af882010-12-22 18:42:35 -08001936 /* Mux pins for device idle if populated */
Tony Lindgren029268e2011-03-11 11:32:25 -08001937 if (oh->mux && oh->mux->pads_dynamic)
Tony Lindgren8d9af882010-12-22 18:42:35 -08001938 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
1939
Paul Walmsley63c85232009-09-03 20:14:03 +03001940 oh->_state = _HWMOD_STATE_IDLE;
1941
1942 return 0;
1943}
1944
1945/**
Kishon Vijay Abraham I95992172011-03-10 03:50:08 -07001946 * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
1947 * @oh: struct omap_hwmod *
1948 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
1949 *
1950 * Sets the IP block's OCP autoidle bit in hardware, and updates our
1951 * local copy. Intended to be used by drivers that require
1952 * direct manipulation of the AUTOIDLE bits.
1953 * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
1954 * along the return value from _set_module_autoidle().
1955 *
1956 * Any users of this function should be scrutinized carefully.
1957 */
1958int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
1959{
1960 u32 v;
1961 int retval = 0;
1962 unsigned long flags;
1963
1964 if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
1965 return -EINVAL;
1966
1967 spin_lock_irqsave(&oh->_lock, flags);
1968
1969 v = oh->_sysc_cache;
1970
1971 retval = _set_module_autoidle(oh, autoidle, &v);
1972
1973 if (!retval)
1974 _write_sysconfig(v, oh);
1975
1976 spin_unlock_irqrestore(&oh->_lock, flags);
1977
1978 return retval;
1979}
1980
1981/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001982 * _shutdown - shutdown an omap_hwmod
1983 * @oh: struct omap_hwmod *
1984 *
1985 * Shut down an omap_hwmod @oh. This should be called when the driver
1986 * used for the hwmod is removed or unloaded or if the driver is not
1987 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1988 * state or returns 0.
1989 */
1990static int _shutdown(struct omap_hwmod *oh)
1991{
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001992 int ret, i;
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001993 u8 prev_state;
1994
Paul Walmsley63c85232009-09-03 20:14:03 +03001995 if (oh->_state != _HWMOD_STATE_IDLE &&
1996 oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001997 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
1998 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001999 return -EINVAL;
2000 }
2001
Paul Walmsley747834a2012-04-18 19:10:04 -06002002 if (_are_any_hardreset_lines_asserted(oh))
2003 return 0;
2004
Paul Walmsley63c85232009-09-03 20:14:03 +03002005 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
2006
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002007 if (oh->class->pre_shutdown) {
2008 prev_state = oh->_state;
2009 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002010 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002011 ret = oh->class->pre_shutdown(oh);
2012 if (ret) {
2013 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002014 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07002015 return ret;
2016 }
2017 }
2018
Miguel Vadillo6481c732011-07-01 22:54:02 +02002019 if (oh->class->sysc) {
2020 if (oh->_state == _HWMOD_STATE_IDLE)
2021 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002022 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02002023 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06002024
Benoit Cousson3827f942010-09-21 10:34:08 -06002025 /* clocks and deps are already disabled in idle */
2026 if (oh->_state == _HWMOD_STATE_ENABLED) {
2027 _del_initiator_dep(oh, mpu_oh);
2028 /* XXX what about the other system initiators here? dma, dsp */
Benoit Coussonbfc141e2011-12-16 16:09:11 -08002029 _omap4_disable_module(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06002030 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06002031 if (oh->clkdm)
2032 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06002033 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002034 /* XXX Should this code also force-disable the optional clocks? */
2035
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06002036 for (i = 0; i < oh->rst_lines_cnt; i++)
2037 _assert_hardreset(oh, oh->rst_lines[i].name);
Benoit Cousson31f62862011-07-01 22:54:05 +02002038
Tony Lindgren8d9af882010-12-22 18:42:35 -08002039 /* Mux pins to safe mode or use populated off mode values */
2040 if (oh->mux)
2041 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03002042
2043 oh->_state = _HWMOD_STATE_DISABLED;
2044
2045 return 0;
2046}
2047
2048/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002049 * _init_mpu_rt_base - populate the virtual address for a hwmod
2050 * @oh: struct omap_hwmod * to locate the virtual address
2051 *
2052 * Cache the virtual address used by the MPU to access this IP block's
2053 * registers. This address is needed early so the OCP registers that
2054 * are part of the device's address space can be ioremapped properly.
2055 * No return value.
2056 */
2057static void __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data)
2058{
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002059 struct omap_hwmod_addr_space *mem;
2060 void __iomem *va_start;
2061
2062 if (!oh)
2063 return;
2064
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002065 _save_mpu_port_index(oh);
2066
Paul Walmsley381d0332012-04-19 00:58:22 -06002067 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2068 return;
2069
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002070 mem = _find_mpu_rt_addr_space(oh);
2071 if (!mem) {
2072 pr_debug("omap_hwmod: %s: no MPU register target found\n",
2073 oh->name);
2074 return;
2075 }
2076
2077 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
2078 if (!va_start) {
2079 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
2080 return;
2081 }
2082
2083 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
2084 oh->name, va_start);
2085
2086 oh->_mpu_rt_va = va_start;
Paul Walmsley381d0332012-04-19 00:58:22 -06002087}
2088
2089/**
2090 * _init - initialize internal data for the hwmod @oh
2091 * @oh: struct omap_hwmod *
2092 * @n: (unused)
2093 *
2094 * Look up the clocks and the address space used by the MPU to access
2095 * registers belonging to the hwmod @oh. @oh must already be
2096 * registered at this point. This is the first of two phases for
2097 * hwmod initialization. Code called here does not touch any hardware
2098 * registers, it simply prepares internal data structures. Returns 0
2099 * upon success or if the hwmod isn't registered, or -EINVAL upon
2100 * failure.
2101 */
2102static int __init _init(struct omap_hwmod *oh, void *data)
2103{
2104 int r;
2105
2106 if (oh->_state != _HWMOD_STATE_REGISTERED)
2107 return 0;
2108
2109 _init_mpu_rt_base(oh, NULL);
2110
2111 r = _init_clocks(oh, NULL);
2112 if (IS_ERR_VALUE(r)) {
2113 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2114 return -EINVAL;
2115 }
2116
2117 oh->_state = _HWMOD_STATE_INITIALIZED;
2118
2119 return 0;
2120}
2121
2122/**
Paul Walmsley64813c32012-04-18 19:10:03 -06002123 * _setup_iclk_autoidle - configure an IP block's interface clocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002124 * @oh: struct omap_hwmod *
2125 *
Paul Walmsley64813c32012-04-18 19:10:03 -06002126 * Set up the module's interface clocks. XXX This function is still mostly
2127 * a stub; implementing this properly requires iclk autoidle usecounting in
2128 * the clock code. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03002129 */
Paul Walmsley64813c32012-04-18 19:10:03 -06002130static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002131{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002132 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002133 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002134 int i = 0;
Paul Walmsley381d0332012-04-19 00:58:22 -06002135 if (oh->_state != _HWMOD_STATE_INITIALIZED)
Paul Walmsley64813c32012-04-18 19:10:03 -06002136 return;
Paul Walmsley48d54f32011-02-23 00:14:07 -07002137
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002138 p = oh->slave_ports.next;
Paul Walmsley63c85232009-09-03 20:14:03 +03002139
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002140 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002141 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002142 if (!os->_clk)
Paul Walmsley64813c32012-04-18 19:10:03 -06002143 continue;
Paul Walmsley63c85232009-09-03 20:14:03 +03002144
Paul Walmsley64813c32012-04-18 19:10:03 -06002145 if (os->flags & OCPIF_SWSUP_IDLE) {
2146 /* XXX omap_iclk_deny_idle(c); */
2147 } else {
2148 /* XXX omap_iclk_allow_idle(c); */
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002149 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +03002150 }
2151 }
2152
Paul Walmsley64813c32012-04-18 19:10:03 -06002153 return;
2154}
2155
2156/**
2157 * _setup_reset - reset an IP block during the setup process
2158 * @oh: struct omap_hwmod *
2159 *
2160 * Reset the IP block corresponding to the hwmod @oh during the setup
2161 * process. The IP block is first enabled so it can be successfully
2162 * reset. Returns 0 upon success or a negative error code upon
2163 * failure.
2164 */
2165static int __init _setup_reset(struct omap_hwmod *oh)
2166{
2167 int r;
2168
2169 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2170 return -EINVAL;
Paul Walmsley63c85232009-09-03 20:14:03 +03002171
Paul Walmsley747834a2012-04-18 19:10:04 -06002172 if (oh->rst_lines_cnt == 0) {
2173 r = _enable(oh);
2174 if (r) {
2175 pr_warning("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2176 oh->name, oh->_state);
2177 return -EINVAL;
2178 }
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002179 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002180
Rajendra Nayak28008522012-03-13 22:55:23 +05302181 if (!(oh->flags & HWMOD_INIT_NO_RESET))
Paul Walmsley64813c32012-04-18 19:10:03 -06002182 r = _reset(oh);
2183
2184 return r;
2185}
2186
2187/**
2188 * _setup_postsetup - transition to the appropriate state after _setup
2189 * @oh: struct omap_hwmod *
2190 *
2191 * Place an IP block represented by @oh into a "post-setup" state --
2192 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2193 * this function is called at the end of _setup().) The postsetup
2194 * state for an IP block can be changed by calling
2195 * omap_hwmod_enter_postsetup_state() early in the boot process,
2196 * before one of the omap_hwmod_setup*() functions are called for the
2197 * IP block.
2198 *
2199 * The IP block stays in this state until a PM runtime-based driver is
2200 * loaded for that IP block. A post-setup state of IDLE is
2201 * appropriate for almost all IP blocks with runtime PM-enabled
2202 * drivers, since those drivers are able to enable the IP block. A
2203 * post-setup state of ENABLED is appropriate for kernels with PM
2204 * runtime disabled. The DISABLED state is appropriate for unusual IP
2205 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2206 * included, since the WDTIMER starts running on reset and will reset
2207 * the MPU if left active.
2208 *
2209 * This post-setup mechanism is deprecated. Once all of the OMAP
2210 * drivers have been converted to use PM runtime, and all of the IP
2211 * block data and interconnect data is available to the hwmod code, it
2212 * should be possible to replace this mechanism with a "lazy reset"
2213 * arrangement. In a "lazy reset" setup, each IP block is enabled
2214 * when the driver first probes, then all remaining IP blocks without
2215 * drivers are either shut down or enabled after the drivers have
2216 * loaded. However, this cannot take place until the above
2217 * preconditions have been met, since otherwise the late reset code
2218 * has no way of knowing which IP blocks are in use by drivers, and
2219 * which ones are unused.
2220 *
2221 * No return value.
2222 */
2223static void __init _setup_postsetup(struct omap_hwmod *oh)
2224{
2225 u8 postsetup_state;
2226
2227 if (oh->rst_lines_cnt > 0)
2228 return;
Benoit Cousson76e55892010-09-21 10:34:11 -06002229
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002230 postsetup_state = oh->_postsetup_state;
2231 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2232 postsetup_state = _HWMOD_STATE_ENABLED;
2233
2234 /*
2235 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2236 * it should be set by the core code as a runtime flag during startup
2237 */
2238 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002239 (postsetup_state == _HWMOD_STATE_IDLE)) {
2240 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002241 postsetup_state = _HWMOD_STATE_ENABLED;
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002242 }
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002243
2244 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002245 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002246 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2247 _shutdown(oh);
2248 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2249 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2250 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03002251
Paul Walmsley64813c32012-04-18 19:10:03 -06002252 return;
2253}
2254
2255/**
2256 * _setup - prepare IP block hardware for use
2257 * @oh: struct omap_hwmod *
2258 * @n: (unused, pass NULL)
2259 *
2260 * Configure the IP block represented by @oh. This may include
2261 * enabling the IP block, resetting it, and placing it into a
2262 * post-setup state, depending on the type of IP block and applicable
2263 * flags. IP blocks are reset to prevent any previous configuration
2264 * by the bootloader or previous operating system from interfering
2265 * with power management or other parts of the system. The reset can
2266 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2267 * two phases for hwmod initialization. Code called here generally
2268 * affects the IP block hardware, or system integration hardware
2269 * associated with the IP block. Returns 0.
2270 */
2271static int __init _setup(struct omap_hwmod *oh, void *data)
2272{
2273 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2274 return 0;
2275
2276 _setup_iclk_autoidle(oh);
2277
2278 if (!_setup_reset(oh))
2279 _setup_postsetup(oh);
2280
Paul Walmsley63c85232009-09-03 20:14:03 +03002281 return 0;
2282}
2283
Benoit Cousson0102b622010-12-21 21:31:27 -07002284/**
2285 * _register - register a struct omap_hwmod
2286 * @oh: struct omap_hwmod *
2287 *
2288 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2289 * already has been registered by the same name; -EINVAL if the
2290 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2291 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2292 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2293 * success.
2294 *
2295 * XXX The data should be copied into bootmem, so the original data
2296 * should be marked __initdata and freed after init. This would allow
2297 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2298 * that the copy process would be relatively complex due to the large number
2299 * of substructures.
2300 */
Benoit Cousson01592df2010-12-21 21:31:28 -07002301static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07002302{
Benoit Cousson0102b622010-12-21 21:31:27 -07002303 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2304 (oh->_state != _HWMOD_STATE_UNKNOWN))
2305 return -EINVAL;
2306
Benoit Cousson0102b622010-12-21 21:31:27 -07002307 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2308
Benoit Coussonce35b242010-12-21 21:31:28 -07002309 if (_lookup(oh->name))
2310 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07002311
Benoit Cousson0102b622010-12-21 21:31:27 -07002312 list_add_tail(&oh->node, &omap_hwmod_list);
2313
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002314 INIT_LIST_HEAD(&oh->master_ports);
2315 INIT_LIST_HEAD(&oh->slave_ports);
Benoit Cousson0102b622010-12-21 21:31:27 -07002316 spin_lock_init(&oh->_lock);
2317
2318 oh->_state = _HWMOD_STATE_REGISTERED;
2319
Paul Walmsley569edd702011-02-23 00:14:06 -07002320 /*
2321 * XXX Rather than doing a strcmp(), this should test a flag
2322 * set in the hwmod data, inserted by the autogenerator code.
2323 */
2324 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2325 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07002326
Paul Walmsley569edd702011-02-23 00:14:06 -07002327 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07002328}
Paul Walmsley63c85232009-09-03 20:14:03 +03002329
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002330/**
2331 * _alloc_links - return allocated memory for hwmod links
2332 * @ml: pointer to a struct omap_hwmod_link * for the master link
2333 * @sl: pointer to a struct omap_hwmod_link * for the slave link
2334 *
2335 * Return pointers to two struct omap_hwmod_link records, via the
2336 * addresses pointed to by @ml and @sl. Will first attempt to return
2337 * memory allocated as part of a large initial block, but if that has
2338 * been exhausted, will allocate memory itself. Since ideally this
2339 * second allocation path will never occur, the number of these
2340 * 'supplemental' allocations will be logged when debugging is
2341 * enabled. Returns 0.
2342 */
2343static int __init _alloc_links(struct omap_hwmod_link **ml,
2344 struct omap_hwmod_link **sl)
2345{
2346 unsigned int sz;
2347
2348 if ((free_ls + LINKS_PER_OCP_IF) <= max_ls) {
2349 *ml = &linkspace[free_ls++];
2350 *sl = &linkspace[free_ls++];
2351 return 0;
2352 }
2353
2354 sz = sizeof(struct omap_hwmod_link) * LINKS_PER_OCP_IF;
2355
2356 *sl = NULL;
2357 *ml = alloc_bootmem(sz);
2358
2359 memset(*ml, 0, sz);
2360
2361 *sl = (void *)(*ml) + sizeof(struct omap_hwmod_link);
2362
2363 ls_supp++;
2364 pr_debug("omap_hwmod: supplemental link allocations needed: %d\n",
2365 ls_supp * LINKS_PER_OCP_IF);
2366
2367 return 0;
2368};
2369
2370/**
2371 * _add_link - add an interconnect between two IP blocks
2372 * @oi: pointer to a struct omap_hwmod_ocp_if record
2373 *
2374 * Add struct omap_hwmod_link records connecting the master IP block
2375 * specified in @oi->master to @oi, and connecting the slave IP block
2376 * specified in @oi->slave to @oi. This code is assumed to run before
2377 * preemption or SMP has been enabled, thus avoiding the need for
2378 * locking in this code. Changes to this assumption will require
2379 * additional locking. Returns 0.
2380 */
2381static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2382{
2383 struct omap_hwmod_link *ml, *sl;
2384
2385 pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2386 oi->slave->name);
2387
2388 _alloc_links(&ml, &sl);
2389
2390 ml->ocp_if = oi;
2391 INIT_LIST_HEAD(&ml->node);
2392 list_add(&ml->node, &oi->master->master_ports);
2393 oi->master->masters_cnt++;
2394
2395 sl->ocp_if = oi;
2396 INIT_LIST_HEAD(&sl->node);
2397 list_add(&sl->node, &oi->slave->slave_ports);
2398 oi->slave->slaves_cnt++;
2399
2400 return 0;
2401}
2402
2403/**
2404 * _register_link - register a struct omap_hwmod_ocp_if
2405 * @oi: struct omap_hwmod_ocp_if *
2406 *
2407 * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
2408 * has already been registered; -EINVAL if @oi is NULL or if the
2409 * record pointed to by @oi is missing required fields; or 0 upon
2410 * success.
2411 *
2412 * XXX The data should be copied into bootmem, so the original data
2413 * should be marked __initdata and freed after init. This would allow
2414 * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2415 */
2416static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2417{
2418 if (!oi || !oi->master || !oi->slave || !oi->user)
2419 return -EINVAL;
2420
2421 if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2422 return -EEXIST;
2423
2424 pr_debug("omap_hwmod: registering link from %s to %s\n",
2425 oi->master->name, oi->slave->name);
2426
2427 /*
2428 * Register the connected hwmods, if they haven't been
2429 * registered already
2430 */
2431 if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2432 _register(oi->master);
2433
2434 if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2435 _register(oi->slave);
2436
2437 _add_link(oi);
2438
2439 oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2440
2441 return 0;
2442}
2443
2444/**
2445 * _alloc_linkspace - allocate large block of hwmod links
2446 * @ois: pointer to an array of struct omap_hwmod_ocp_if records to count
2447 *
2448 * Allocate a large block of struct omap_hwmod_link records. This
2449 * improves boot time significantly by avoiding the need to allocate
2450 * individual records one by one. If the number of records to
2451 * allocate in the block hasn't been manually specified, this function
2452 * will count the number of struct omap_hwmod_ocp_if records in @ois
2453 * and use that to determine the allocation size. For SoC families
2454 * that require multiple list registrations, such as OMAP3xxx, this
2455 * estimation process isn't optimal, so manual estimation is advised
2456 * in those cases. Returns -EEXIST if the allocation has already occurred
2457 * or 0 upon success.
2458 */
2459static int __init _alloc_linkspace(struct omap_hwmod_ocp_if **ois)
2460{
2461 unsigned int i = 0;
2462 unsigned int sz;
2463
2464 if (linkspace) {
2465 WARN(1, "linkspace already allocated\n");
2466 return -EEXIST;
2467 }
2468
2469 if (max_ls == 0)
2470 while (ois[i++])
2471 max_ls += LINKS_PER_OCP_IF;
2472
2473 sz = sizeof(struct omap_hwmod_link) * max_ls;
2474
2475 pr_debug("omap_hwmod: %s: allocating %d byte linkspace (%d links)\n",
2476 __func__, sz, max_ls);
2477
2478 linkspace = alloc_bootmem(sz);
2479
2480 memset(linkspace, 0, sz);
2481
2482 return 0;
2483}
Paul Walmsley63c85232009-09-03 20:14:03 +03002484
2485/* Public functions */
2486
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002487u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002488{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002489 if (oh->flags & HWMOD_16BIT_REG)
2490 return __raw_readw(oh->_mpu_rt_va + reg_offs);
2491 else
2492 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002493}
2494
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002495void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002496{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002497 if (oh->flags & HWMOD_16BIT_REG)
2498 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
2499 else
2500 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002501}
2502
Paul Walmsley887adea2010-07-26 16:34:33 -06002503/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002504 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
2505 * @oh: struct omap_hwmod *
2506 *
2507 * This is a public function exposed to drivers. Some drivers may need to do
2508 * some settings before and after resetting the device. Those drivers after
2509 * doing the necessary settings could use this function to start a reset by
2510 * setting the SYSCONFIG.SOFTRESET bit.
2511 */
2512int omap_hwmod_softreset(struct omap_hwmod *oh)
2513{
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002514 u32 v;
2515 int ret;
2516
2517 if (!oh || !(oh->_sysc_cache))
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002518 return -EINVAL;
2519
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002520 v = oh->_sysc_cache;
2521 ret = _set_softreset(oh, &v);
2522 if (ret)
2523 goto error;
2524 _write_sysconfig(v, oh);
2525
2526error:
2527 return ret;
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002528}
2529
2530/**
Paul Walmsley887adea2010-07-26 16:34:33 -06002531 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
2532 * @oh: struct omap_hwmod *
2533 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
2534 *
2535 * Sets the IP block's OCP slave idlemode in hardware, and updates our
2536 * local copy. Intended to be used by drivers that have some erratum
2537 * that requires direct manipulation of the SIDLEMODE bits. Returns
2538 * -EINVAL if @oh is null, or passes along the return value from
2539 * _set_slave_idlemode().
2540 *
2541 * XXX Does this function have any current users? If not, we should
2542 * remove it; it is better to let the rest of the hwmod code handle this.
2543 * Any users of this function should be scrutinized carefully.
2544 */
Kevin Hilman46273e62010-01-26 20:13:03 -07002545int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
2546{
2547 u32 v;
2548 int retval = 0;
2549
2550 if (!oh)
2551 return -EINVAL;
2552
2553 v = oh->_sysc_cache;
2554
2555 retval = _set_slave_idlemode(oh, idlemode, &v);
2556 if (!retval)
2557 _write_sysconfig(v, oh);
2558
2559 return retval;
2560}
2561
Paul Walmsley63c85232009-09-03 20:14:03 +03002562/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002563 * omap_hwmod_lookup - look up a registered omap_hwmod by name
2564 * @name: name of the omap_hwmod to look up
2565 *
2566 * Given a @name of an omap_hwmod, return a pointer to the registered
2567 * struct omap_hwmod *, or NULL upon error.
2568 */
2569struct omap_hwmod *omap_hwmod_lookup(const char *name)
2570{
2571 struct omap_hwmod *oh;
2572
2573 if (!name)
2574 return NULL;
2575
Paul Walmsley63c85232009-09-03 20:14:03 +03002576 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002577
2578 return oh;
2579}
2580
2581/**
2582 * omap_hwmod_for_each - call function for each registered omap_hwmod
2583 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06002584 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03002585 *
2586 * Call @fn for each registered omap_hwmod, passing @data to each
2587 * function. @fn must return 0 for success or any other value for
2588 * failure. If @fn returns non-zero, the iteration across omap_hwmods
2589 * will stop and the non-zero return value will be passed to the
2590 * caller of omap_hwmod_for_each(). @fn is called with
2591 * omap_hwmod_for_each() held.
2592 */
Paul Walmsley97d60162010-07-26 16:34:30 -06002593int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
2594 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03002595{
2596 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05302597 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002598
2599 if (!fn)
2600 return -EINVAL;
2601
Paul Walmsley63c85232009-09-03 20:14:03 +03002602 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06002603 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03002604 if (ret)
2605 break;
2606 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002607
2608 return ret;
2609}
2610
Paul Walmsley63c85232009-09-03 20:14:03 +03002611/**
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002612 * omap_hwmod_register_links - register an array of hwmod links
2613 * @ois: pointer to an array of omap_hwmod_ocp_if to register
2614 *
2615 * Intended to be called early in boot before the clock framework is
2616 * initialized. If @ois is not null, will register all omap_hwmods
2617 * listed in @ois that are valid for this chip. Returns 0.
2618 */
2619int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
2620{
2621 int r, i;
2622
2623 if (!ois)
2624 return 0;
2625
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002626 if (!linkspace) {
2627 if (_alloc_linkspace(ois)) {
2628 pr_err("omap_hwmod: could not allocate link space\n");
2629 return -ENOMEM;
2630 }
2631 }
2632
2633 i = 0;
2634 do {
2635 r = _register_link(ois[i]);
2636 WARN(r && r != -EEXIST,
2637 "omap_hwmod: _register_link(%s -> %s) returned %d\n",
2638 ois[i]->master->name, ois[i]->slave->name, r);
2639 } while (ois[++i]);
2640
2641 return 0;
2642}
2643
2644/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002645 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
2646 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002647 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002648 * If the hwmod data corresponding to the MPU subsystem IP block
2649 * hasn't been initialized and set up yet, do so now. This must be
2650 * done first since sleep dependencies may be added from other hwmods
2651 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
2652 * return value.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002653 */
Paul Walmsley381d0332012-04-19 00:58:22 -06002654static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002655{
Paul Walmsley381d0332012-04-19 00:58:22 -06002656 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
2657 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
2658 __func__, MPU_INITIATOR_NAME);
2659 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
2660 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
Paul Walmsley63c85232009-09-03 20:14:03 +03002661}
2662
2663/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002664 * omap_hwmod_setup_one - set up a single hwmod
2665 * @oh_name: const char * name of the already-registered hwmod to set up
2666 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002667 * Initialize and set up a single hwmod. Intended to be used for a
2668 * small number of early devices, such as the timer IP blocks used for
2669 * the scheduler clock. Must be called after omap2_clk_init().
2670 * Resolves the struct clk names to struct clk pointers for each
2671 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
2672 * -EINVAL upon error or 0 upon success.
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002673 */
2674int __init omap_hwmod_setup_one(const char *oh_name)
2675{
2676 struct omap_hwmod *oh;
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002677
2678 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
2679
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002680 oh = _lookup(oh_name);
2681 if (!oh) {
2682 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
2683 return -EINVAL;
2684 }
2685
Paul Walmsley381d0332012-04-19 00:58:22 -06002686 _ensure_mpu_hwmod_is_setup(oh);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002687
Paul Walmsley381d0332012-04-19 00:58:22 -06002688 _init(oh, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002689 _setup(oh, NULL);
2690
2691 return 0;
2692}
2693
2694/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002695 * omap_hwmod_setup_all - set up all registered IP blocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002696 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002697 * Initialize and set up all IP blocks registered with the hwmod code.
2698 * Must be called after omap2_clk_init(). Resolves the struct clk
2699 * names to struct clk pointers for each registered omap_hwmod. Also
2700 * calls _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002701 */
Paul Walmsley550c8092011-02-28 11:58:14 -07002702static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03002703{
Paul Walmsley381d0332012-04-19 00:58:22 -06002704 _ensure_mpu_hwmod_is_setup(NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002705
Paul Walmsley381d0332012-04-19 00:58:22 -06002706 omap_hwmod_for_each(_init, NULL);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002707 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002708
2709 return 0;
2710}
Paul Walmsley550c8092011-02-28 11:58:14 -07002711core_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03002712
2713/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002714 * omap_hwmod_enable - enable an omap_hwmod
2715 * @oh: struct omap_hwmod *
2716 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002717 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03002718 * Returns -EINVAL on error or passes along the return value from _enable().
2719 */
2720int omap_hwmod_enable(struct omap_hwmod *oh)
2721{
2722 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002723 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002724
2725 if (!oh)
2726 return -EINVAL;
2727
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002728 spin_lock_irqsave(&oh->_lock, flags);
2729 r = _enable(oh);
2730 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002731
2732 return r;
2733}
2734
2735/**
2736 * omap_hwmod_idle - idle an omap_hwmod
2737 * @oh: struct omap_hwmod *
2738 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002739 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03002740 * Returns -EINVAL on error or passes along the return value from _idle().
2741 */
2742int omap_hwmod_idle(struct omap_hwmod *oh)
2743{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002744 unsigned long flags;
2745
Paul Walmsley63c85232009-09-03 20:14:03 +03002746 if (!oh)
2747 return -EINVAL;
2748
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002749 spin_lock_irqsave(&oh->_lock, flags);
2750 _idle(oh);
2751 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002752
2753 return 0;
2754}
2755
2756/**
2757 * omap_hwmod_shutdown - shutdown an omap_hwmod
2758 * @oh: struct omap_hwmod *
2759 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002760 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03002761 * omap_device_shutdown(). Returns -EINVAL on error or passes along
2762 * the return value from _shutdown().
2763 */
2764int omap_hwmod_shutdown(struct omap_hwmod *oh)
2765{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002766 unsigned long flags;
2767
Paul Walmsley63c85232009-09-03 20:14:03 +03002768 if (!oh)
2769 return -EINVAL;
2770
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002771 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002772 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002773 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002774
2775 return 0;
2776}
2777
2778/**
2779 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
2780 * @oh: struct omap_hwmod *oh
2781 *
2782 * Intended to be called by the omap_device code.
2783 */
2784int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
2785{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002786 unsigned long flags;
2787
2788 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002789 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002790 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002791
2792 return 0;
2793}
2794
2795/**
2796 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
2797 * @oh: struct omap_hwmod *oh
2798 *
2799 * Intended to be called by the omap_device code.
2800 */
2801int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
2802{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002803 unsigned long flags;
2804
2805 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002806 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002807 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002808
2809 return 0;
2810}
2811
2812/**
2813 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
2814 * @oh: struct omap_hwmod *oh
2815 *
2816 * Intended to be called by drivers and core code when all posted
2817 * writes to a device must complete before continuing further
2818 * execution (for example, after clearing some device IRQSTATUS
2819 * register bits)
2820 *
2821 * XXX what about targets with multiple OCP threads?
2822 */
2823void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
2824{
2825 BUG_ON(!oh);
2826
Paul Walmsley43b40992010-02-22 22:09:34 -07002827 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Russell King4f8a4282012-02-07 10:59:37 +00002828 WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
2829 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002830 return;
2831 }
2832
2833 /*
2834 * Forces posted writes to complete on the OCP thread handling
2835 * register writes
2836 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002837 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002838}
2839
2840/**
2841 * omap_hwmod_reset - reset the hwmod
2842 * @oh: struct omap_hwmod *
2843 *
2844 * Under some conditions, a driver may wish to reset the entire device.
2845 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06002846 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03002847 */
2848int omap_hwmod_reset(struct omap_hwmod *oh)
2849{
2850 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002851 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002852
Liam Girdwood9b579112010-09-21 10:34:09 -06002853 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002854 return -EINVAL;
2855
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002856 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002857 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002858 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002859
2860 return r;
2861}
2862
Paul Walmsley5e8370f2012-04-18 19:10:06 -06002863/*
2864 * IP block data retrieval functions
2865 */
2866
Paul Walmsley63c85232009-09-03 20:14:03 +03002867/**
2868 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
2869 * @oh: struct omap_hwmod *
2870 * @res: pointer to the first element of an array of struct resource to fill
2871 *
2872 * Count the number of struct resource array elements necessary to
2873 * contain omap_hwmod @oh resources. Intended to be called by code
2874 * that registers omap_devices. Intended to be used to determine the
2875 * size of a dynamically-allocated struct resource array, before
2876 * calling omap_hwmod_fill_resources(). Returns the number of struct
2877 * resource array elements needed.
2878 *
2879 * XXX This code is not optimized. It could attempt to merge adjacent
2880 * resource IDs.
2881 *
2882 */
2883int omap_hwmod_count_resources(struct omap_hwmod *oh)
2884{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002885 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002886 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002887 int ret;
2888 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002889
Paul Walmsleybc614952011-07-09 19:14:07 -06002890 ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002891
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002892 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002893
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002894 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002895 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002896 ret += _count_ocp_if_addr_spaces(os);
2897 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002898
2899 return ret;
2900}
2901
2902/**
2903 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
2904 * @oh: struct omap_hwmod *
2905 * @res: pointer to the first element of an array of struct resource to fill
2906 *
2907 * Fill the struct resource array @res with resource data from the
2908 * omap_hwmod @oh. Intended to be called by code that registers
2909 * omap_devices. See also omap_hwmod_count_resources(). Returns the
2910 * number of array elements filled.
2911 */
2912int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
2913{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002914 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002915 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002916 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03002917 int r = 0;
2918
2919 /* For each IRQ, DMA, memory area, fill in array.*/
2920
Paul Walmsley212738a2011-07-09 19:14:06 -06002921 mpu_irqs_cnt = _count_mpu_irqs(oh);
2922 for (i = 0; i < mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07002923 (res + r)->name = (oh->mpu_irqs + i)->name;
2924 (res + r)->start = (oh->mpu_irqs + i)->irq;
2925 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03002926 (res + r)->flags = IORESOURCE_IRQ;
2927 r++;
2928 }
2929
Paul Walmsleybc614952011-07-09 19:14:07 -06002930 sdma_reqs_cnt = _count_sdma_reqs(oh);
2931 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06002932 (res + r)->name = (oh->sdma_reqs + i)->name;
2933 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
2934 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03002935 (res + r)->flags = IORESOURCE_DMA;
2936 r++;
2937 }
2938
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002939 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002940
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002941 i = 0;
2942 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002943 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley78183f32011-07-09 19:14:05 -06002944 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03002945
Paul Walmsley78183f32011-07-09 19:14:05 -06002946 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08002947 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03002948 (res + r)->start = (os->addr + j)->pa_start;
2949 (res + r)->end = (os->addr + j)->pa_end;
2950 (res + r)->flags = IORESOURCE_MEM;
2951 r++;
2952 }
2953 }
2954
2955 return r;
2956}
2957
2958/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06002959 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
2960 * @oh: struct omap_hwmod * to operate on
2961 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
2962 * @name: pointer to the name of the data to fetch (optional)
2963 * @rsrc: pointer to a struct resource, allocated by the caller
2964 *
2965 * Retrieve MPU IRQ, SDMA request line, or address space start/end
2966 * data for the IP block pointed to by @oh. The data will be filled
2967 * into a struct resource record pointed to by @rsrc. The struct
2968 * resource must be allocated by the caller. When @name is non-null,
2969 * the data associated with the matching entry in the IRQ/SDMA/address
2970 * space hwmod data arrays will be returned. If @name is null, the
2971 * first array entry will be returned. Data order is not meaningful
2972 * in hwmod data, so callers are strongly encouraged to use a non-null
2973 * @name whenever possible to avoid unpredictable effects if hwmod
2974 * data is later added that causes data ordering to change. This
2975 * function is only intended for use by OMAP core code. Device
2976 * drivers should not call this function - the appropriate bus-related
2977 * data accessor functions should be used instead. Returns 0 upon
2978 * success or a negative error code upon error.
2979 */
2980int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
2981 const char *name, struct resource *rsrc)
2982{
2983 int r;
2984 unsigned int irq, dma;
2985 u32 pa_start, pa_end;
2986
2987 if (!oh || !rsrc)
2988 return -EINVAL;
2989
2990 if (type == IORESOURCE_IRQ) {
2991 r = _get_mpu_irq_by_name(oh, name, &irq);
2992 if (r)
2993 return r;
2994
2995 rsrc->start = irq;
2996 rsrc->end = irq;
2997 } else if (type == IORESOURCE_DMA) {
2998 r = _get_sdma_req_by_name(oh, name, &dma);
2999 if (r)
3000 return r;
3001
3002 rsrc->start = dma;
3003 rsrc->end = dma;
3004 } else if (type == IORESOURCE_MEM) {
3005 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
3006 if (r)
3007 return r;
3008
3009 rsrc->start = pa_start;
3010 rsrc->end = pa_end;
3011 } else {
3012 return -EINVAL;
3013 }
3014
3015 rsrc->flags = type;
3016 rsrc->name = name;
3017
3018 return 0;
3019}
3020
3021/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003022 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
3023 * @oh: struct omap_hwmod *
3024 *
3025 * Return the powerdomain pointer associated with the OMAP module
3026 * @oh's main clock. If @oh does not have a main clk, return the
3027 * powerdomain associated with the interface clock associated with the
3028 * module's MPU port. (XXX Perhaps this should use the SDMA port
3029 * instead?) Returns NULL on error, or a struct powerdomain * on
3030 * success.
3031 */
3032struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
3033{
3034 struct clk *c;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003035 struct omap_hwmod_ocp_if *oi;
Paul Walmsley63c85232009-09-03 20:14:03 +03003036
3037 if (!oh)
3038 return NULL;
3039
3040 if (oh->_clk) {
3041 c = oh->_clk;
3042 } else {
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003043 oi = _find_mpu_rt_port(oh);
3044 if (!oi)
Paul Walmsley63c85232009-09-03 20:14:03 +03003045 return NULL;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003046 c = oi->_clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03003047 }
3048
Thara Gopinathd5647c12010-03-31 04:16:29 -06003049 if (!c->clkdm)
3050 return NULL;
3051
Paul Walmsley63c85232009-09-03 20:14:03 +03003052 return c->clkdm->pwrdm.ptr;
3053
3054}
3055
3056/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06003057 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3058 * @oh: struct omap_hwmod *
3059 *
3060 * Returns the virtual address corresponding to the beginning of the
3061 * module's register target, in the address range that is intended to
3062 * be used by the MPU. Returns the virtual address upon success or NULL
3063 * upon error.
3064 */
3065void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3066{
3067 if (!oh)
3068 return NULL;
3069
3070 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3071 return NULL;
3072
3073 if (oh->_state == _HWMOD_STATE_UNKNOWN)
3074 return NULL;
3075
3076 return oh->_mpu_rt_va;
3077}
3078
3079/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003080 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
3081 * @oh: struct omap_hwmod *
3082 * @init_oh: struct omap_hwmod * (initiator)
3083 *
3084 * Add a sleep dependency between the initiator @init_oh and @oh.
3085 * Intended to be called by DSP/Bridge code via platform_data for the
3086 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3087 * code needs to add/del initiator dependencies dynamically
3088 * before/after accessing a device. Returns the return value from
3089 * _add_initiator_dep().
3090 *
3091 * XXX Keep a usecount in the clockdomain code
3092 */
3093int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
3094 struct omap_hwmod *init_oh)
3095{
3096 return _add_initiator_dep(oh, init_oh);
3097}
3098
3099/*
3100 * XXX what about functions for drivers to save/restore ocp_sysconfig
3101 * for context save/restore operations?
3102 */
3103
3104/**
3105 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
3106 * @oh: struct omap_hwmod *
3107 * @init_oh: struct omap_hwmod * (initiator)
3108 *
3109 * Remove a sleep dependency between the initiator @init_oh and @oh.
3110 * Intended to be called by DSP/Bridge code via platform_data for the
3111 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3112 * code needs to add/del initiator dependencies dynamically
3113 * before/after accessing a device. Returns the return value from
3114 * _del_initiator_dep().
3115 *
3116 * XXX Keep a usecount in the clockdomain code
3117 */
3118int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
3119 struct omap_hwmod *init_oh)
3120{
3121 return _del_initiator_dep(oh, init_oh);
3122}
3123
3124/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003125 * omap_hwmod_enable_wakeup - allow device to wake up the system
3126 * @oh: struct omap_hwmod *
3127 *
3128 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
Govindraj.R2a1cc142012-04-05 02:59:32 -06003129 * send wakeups to the PRCM, and enable I/O ring wakeup events for
3130 * this IP block if it has dynamic mux entries. Eventually this
3131 * should set PRCM wakeup registers to cause the PRCM to receive
3132 * wakeup events from the module. Does not set any wakeup routing
3133 * registers beyond this point - if the module is to wake up any other
3134 * module or subsystem, that must be set separately. Called by
3135 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003136 */
3137int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3138{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003139 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003140 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003141
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003142 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003143
3144 if (oh->class->sysc &&
3145 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3146 v = oh->_sysc_cache;
3147 _enable_wakeup(oh, &v);
3148 _write_sysconfig(v, oh);
3149 }
3150
Govindraj Receec002011-12-16 14:36:58 -07003151 _set_idle_ioring_wakeup(oh, true);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003152 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003153
3154 return 0;
3155}
3156
3157/**
3158 * omap_hwmod_disable_wakeup - prevent device from waking the system
3159 * @oh: struct omap_hwmod *
3160 *
3161 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
Govindraj.R2a1cc142012-04-05 02:59:32 -06003162 * from sending wakeups to the PRCM, and disable I/O ring wakeup
3163 * events for this IP block if it has dynamic mux entries. Eventually
3164 * this should clear PRCM wakeup registers to cause the PRCM to ignore
3165 * wakeup events from the module. Does not set any wakeup routing
3166 * registers beyond this point - if the module is to wake up any other
3167 * module or subsystem, that must be set separately. Called by
3168 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003169 */
3170int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3171{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003172 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003173 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003174
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003175 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003176
3177 if (oh->class->sysc &&
3178 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3179 v = oh->_sysc_cache;
3180 _disable_wakeup(oh, &v);
3181 _write_sysconfig(v, oh);
3182 }
3183
Govindraj Receec002011-12-16 14:36:58 -07003184 _set_idle_ioring_wakeup(oh, false);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003185 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003186
3187 return 0;
3188}
Paul Walmsley43b40992010-02-22 22:09:34 -07003189
3190/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003191 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3192 * contained in the hwmod module.
3193 * @oh: struct omap_hwmod *
3194 * @name: name of the reset line to lookup and assert
3195 *
3196 * Some IP like dsp, ipu or iva contain processor that require
3197 * an HW reset line to be assert / deassert in order to enable fully
3198 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3199 * yet supported on this OMAP; otherwise, passes along the return value
3200 * from _assert_hardreset().
3201 */
3202int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3203{
3204 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003205 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003206
3207 if (!oh)
3208 return -EINVAL;
3209
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003210 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003211 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003212 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003213
3214 return ret;
3215}
3216
3217/**
3218 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3219 * contained in the hwmod module.
3220 * @oh: struct omap_hwmod *
3221 * @name: name of the reset line to look up and deassert
3222 *
3223 * Some IP like dsp, ipu or iva contain processor that require
3224 * an HW reset line to be assert / deassert in order to enable fully
3225 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3226 * yet supported on this OMAP; otherwise, passes along the return value
3227 * from _deassert_hardreset().
3228 */
3229int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3230{
3231 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003232 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003233
3234 if (!oh)
3235 return -EINVAL;
3236
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003237 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003238 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003239 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003240
3241 return ret;
3242}
3243
3244/**
3245 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
3246 * contained in the hwmod module
3247 * @oh: struct omap_hwmod *
3248 * @name: name of the reset line to look up and read
3249 *
3250 * Return the current state of the hwmod @oh's reset line named @name:
3251 * returns -EINVAL upon parameter error or if this operation
3252 * is unsupported on the current OMAP; otherwise, passes along the return
3253 * value from _read_hardreset().
3254 */
3255int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
3256{
3257 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003258 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003259
3260 if (!oh)
3261 return -EINVAL;
3262
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003263 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003264 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003265 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003266
3267 return ret;
3268}
3269
3270
3271/**
Paul Walmsley43b40992010-02-22 22:09:34 -07003272 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3273 * @classname: struct omap_hwmod_class name to search for
3274 * @fn: callback function pointer to call for each hwmod in class @classname
3275 * @user: arbitrary context data to pass to the callback function
3276 *
Benoit Coussonce35b242010-12-21 21:31:28 -07003277 * For each omap_hwmod of class @classname, call @fn.
3278 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07003279 * zero, the iterator is terminated, and the callback function's return
3280 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3281 * if @classname or @fn are NULL, or passes back the error code from @fn.
3282 */
3283int omap_hwmod_for_each_by_class(const char *classname,
3284 int (*fn)(struct omap_hwmod *oh,
3285 void *user),
3286 void *user)
3287{
3288 struct omap_hwmod *temp_oh;
3289 int ret = 0;
3290
3291 if (!classname || !fn)
3292 return -EINVAL;
3293
3294 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3295 __func__, classname);
3296
Paul Walmsley43b40992010-02-22 22:09:34 -07003297 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3298 if (!strcmp(temp_oh->class->name, classname)) {
3299 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3300 __func__, temp_oh->name);
3301 ret = (*fn)(temp_oh, user);
3302 if (ret)
3303 break;
3304 }
3305 }
3306
Paul Walmsley43b40992010-02-22 22:09:34 -07003307 if (ret)
3308 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3309 __func__, ret);
3310
3311 return ret;
3312}
3313
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003314/**
3315 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3316 * @oh: struct omap_hwmod *
3317 * @state: state that _setup() should leave the hwmod in
3318 *
Paul Walmsley550c8092011-02-28 11:58:14 -07003319 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsley64813c32012-04-18 19:10:03 -06003320 * (called by omap_hwmod_setup_*()). See also the documentation
3321 * for _setup_postsetup(), above. Returns 0 upon success or
3322 * -EINVAL if there is a problem with the arguments or if the hwmod is
3323 * in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003324 */
3325int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3326{
3327 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003328 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003329
3330 if (!oh)
3331 return -EINVAL;
3332
3333 if (state != _HWMOD_STATE_DISABLED &&
3334 state != _HWMOD_STATE_ENABLED &&
3335 state != _HWMOD_STATE_IDLE)
3336 return -EINVAL;
3337
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003338 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003339
3340 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3341 ret = -EINVAL;
3342 goto ohsps_unlock;
3343 }
3344
3345 oh->_postsetup_state = state;
3346 ret = 0;
3347
3348ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003349 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003350
3351 return ret;
3352}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003353
3354/**
3355 * omap_hwmod_get_context_loss_count - get lost context count
3356 * @oh: struct omap_hwmod *
3357 *
3358 * Query the powerdomain of of @oh to get the context loss
3359 * count for this device.
3360 *
3361 * Returns the context loss count of the powerdomain assocated with @oh
3362 * upon success, or zero if no powerdomain exists for @oh.
3363 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03003364int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003365{
3366 struct powerdomain *pwrdm;
3367 int ret = 0;
3368
3369 pwrdm = omap_hwmod_get_pwrdm(oh);
3370 if (pwrdm)
3371 ret = pwrdm_get_context_loss_count(pwrdm);
3372
3373 return ret;
3374}
Paul Walmsley43b01642011-03-10 03:50:07 -07003375
3376/**
3377 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
3378 * @oh: struct omap_hwmod *
3379 *
3380 * Prevent the hwmod @oh from being reset during the setup process.
3381 * Intended for use by board-*.c files on boards with devices that
3382 * cannot tolerate being reset. Must be called before the hwmod has
3383 * been set up. Returns 0 upon success or negative error code upon
3384 * failure.
3385 */
3386int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
3387{
3388 if (!oh)
3389 return -EINVAL;
3390
3391 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3392 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
3393 oh->name);
3394 return -EINVAL;
3395 }
3396
3397 oh->flags |= HWMOD_INIT_NO_RESET;
3398
3399 return 0;
3400}
Tero Kristoabc2d542011-12-16 14:36:59 -07003401
3402/**
3403 * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
3404 * @oh: struct omap_hwmod * containing hwmod mux entries
3405 * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
3406 * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
3407 *
3408 * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
3409 * entry number @pad_idx for the hwmod @oh, trigger the interrupt
3410 * service routine for the hwmod's mpu_irqs array index @irq_idx. If
3411 * this function is not called for a given pad_idx, then the ISR
3412 * associated with @oh's first MPU IRQ will be triggered when an I/O
3413 * pad wakeup occurs on that pad. Note that @pad_idx is the index of
3414 * the _dynamic or wakeup_ entry: if there are other entries not
3415 * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
3416 * entries are NOT COUNTED in the dynamic pad index. This function
3417 * must be called separately for each pad that requires its interrupt
3418 * to be re-routed this way. Returns -EINVAL if there is an argument
3419 * problem or if @oh does not have hwmod mux entries or MPU IRQs;
3420 * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
3421 *
3422 * XXX This function interface is fragile. Rather than using array
3423 * indexes, which are subject to unpredictable change, it should be
3424 * using hwmod IRQ names, and some other stable key for the hwmod mux
3425 * pad records.
3426 */
3427int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
3428{
3429 int nr_irqs;
3430
3431 might_sleep();
3432
3433 if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
3434 pad_idx >= oh->mux->nr_pads_dynamic)
3435 return -EINVAL;
3436
3437 /* Check the number of available mpu_irqs */
3438 for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
3439 ;
3440
3441 if (irq_idx >= nr_irqs)
3442 return -EINVAL;
3443
3444 if (!oh->mux->irqs) {
3445 /* XXX What frees this? */
3446 oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
3447 GFP_KERNEL);
3448 if (!oh->mux->irqs)
3449 return -ENOMEM;
3450 }
3451 oh->mux->irqs[pad_idx] = irq_idx;
3452
3453 return 0;
3454}
Tarun Kanti DebBarmabed9d1b2012-07-04 05:00:48 -06003455
3456/**
3457 * omap_hwmod_get_main_clk - get pointer to main clock name
3458 * @oh: struct omap_hwmod *
3459 *
3460 * Returns the main clock name assocated with @oh upon success,
3461 * or NULL if @oh is NULL.
3462 */
3463const char *omap_hwmod_get_main_clk(struct omap_hwmod *oh)
3464{
3465 if (!oh)
3466 return NULL;
3467
3468 return oh->main_clk;
3469}