blob: 8cf837d2332a768dbafd45d29bb2e6db825b8501 [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 Walmsley63c85232009-09-03 20:14:03 +0300140
Tony Lindgren4e653312011-11-10 22:45:17 +0100141#include "common.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700142#include <plat/cpu.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -0700143#include "clockdomain.h"
Paul Walmsley72e06d02010-12-21 21:05:16 -0700144#include "powerdomain.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700145#include <plat/clock.h>
146#include <plat/omap_hwmod.h>
Benoît Cousson5365efb2010-09-21 10:34:11 -0600147#include <plat/prcm.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300148
Paul Walmsley59fb6592010-12-21 15:30:55 -0700149#include "cm2xxx_3xxx.h"
Benoit Coussond0f06312011-07-10 05:56:30 -0600150#include "cminst44xx.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -0700151#include "prm2xxx_3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -0700152#include "prm44xx.h"
Benoit Coussoneaac3292011-07-10 05:56:31 -0600153#include "prminst44xx.h"
Tony Lindgren8d9af882010-12-22 18:42:35 -0800154#include "mux.h"
Paul Walmsley63c85232009-09-03 20:14:03 +0300155
Benoît Cousson5365efb2010-09-21 10:34:11 -0600156/* Maximum microseconds to wait for OMAP module to softreset */
157#define MAX_MODULE_SOFTRESET_WAIT 10000
Paul Walmsley63c85232009-09-03 20:14:03 +0300158
159/* Name of the OMAP hwmod for the MPU */
Benoit Cousson5c2c0292010-05-20 12:31:10 -0600160#define MPU_INITIATOR_NAME "mpu"
Paul Walmsley63c85232009-09-03 20:14:03 +0300161
162/* omap_hwmod_list contains all registered struct omap_hwmods */
163static LIST_HEAD(omap_hwmod_list);
164
Paul Walmsley63c85232009-09-03 20:14:03 +0300165/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
166static struct omap_hwmod *mpu_oh;
167
Paul Walmsley63c85232009-09-03 20:14:03 +0300168
169/* Private functions */
170
171/**
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600172 * _fetch_next_ocp_if - return @i'th OCP interface in an array
173 * @p: ptr to a ptr to the list_head inside the ocp_if to return (not yet used)
174 * @old: ptr to an array of struct omap_hwmod_ocp_if records
175 * @i: pointer to the index into the @old array
176 *
177 * Return a pointer to the next struct omap_hwmod_ocp_if record in a
178 * sequence. Currently returns a struct omap_hwmod_ocp_if record
179 * corresponding to the element index pointed to by @i in the @old
180 * array, and increments the index pointed to by @i.
181 */
182static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
183 struct omap_hwmod_ocp_if **old,
184 int *i)
185{
186 struct omap_hwmod_ocp_if *oi;
187
188 oi = old[*i];
189 *i = *i + 1;
190
191 return oi;
192}
193
194/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300195 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
196 * @oh: struct omap_hwmod *
197 *
198 * Load the current value of the hwmod OCP_SYSCONFIG register into the
199 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
200 * OCP_SYSCONFIG register or 0 upon success.
201 */
202static int _update_sysc_cache(struct omap_hwmod *oh)
203{
Paul Walmsley43b40992010-02-22 22:09:34 -0700204 if (!oh->class->sysc) {
205 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 +0300206 return -EINVAL;
207 }
208
209 /* XXX ensure module interface clock is up */
210
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -0700211 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300212
Paul Walmsley43b40992010-02-22 22:09:34 -0700213 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
Thara Gopinath883edfd2010-01-19 17:30:51 -0700214 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300215
216 return 0;
217}
218
219/**
220 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
221 * @v: OCP_SYSCONFIG value to write
222 * @oh: struct omap_hwmod *
223 *
Paul Walmsley43b40992010-02-22 22:09:34 -0700224 * Write @v into the module class' OCP_SYSCONFIG register, if it has
225 * one. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +0300226 */
227static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
228{
Paul Walmsley43b40992010-02-22 22:09:34 -0700229 if (!oh->class->sysc) {
230 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 +0300231 return;
232 }
233
234 /* XXX ensure module interface clock is up */
235
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700236 /* Module might have lost context, always update cache and register */
237 oh->_sysc_cache = v;
238 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300239}
240
241/**
242 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
243 * @oh: struct omap_hwmod *
244 * @standbymode: MIDLEMODE field bits
245 * @v: pointer to register contents to modify
246 *
247 * Update the master standby mode bits in @v to be @standbymode for
248 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
249 * upon error or 0 upon success.
250 */
251static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
252 u32 *v)
253{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700254 u32 mstandby_mask;
255 u8 mstandby_shift;
256
Paul Walmsley43b40992010-02-22 22:09:34 -0700257 if (!oh->class->sysc ||
258 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300259 return -EINVAL;
260
Paul Walmsley43b40992010-02-22 22:09:34 -0700261 if (!oh->class->sysc->sysc_fields) {
262 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700263 return -EINVAL;
264 }
265
Paul Walmsley43b40992010-02-22 22:09:34 -0700266 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700267 mstandby_mask = (0x3 << mstandby_shift);
268
269 *v &= ~mstandby_mask;
270 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300271
272 return 0;
273}
274
275/**
276 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
277 * @oh: struct omap_hwmod *
278 * @idlemode: SIDLEMODE field bits
279 * @v: pointer to register contents to modify
280 *
281 * Update the slave idle mode bits in @v to be @idlemode for the @oh
282 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
283 * or 0 upon success.
284 */
285static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
286{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700287 u32 sidle_mask;
288 u8 sidle_shift;
289
Paul Walmsley43b40992010-02-22 22:09:34 -0700290 if (!oh->class->sysc ||
291 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300292 return -EINVAL;
293
Paul Walmsley43b40992010-02-22 22:09:34 -0700294 if (!oh->class->sysc->sysc_fields) {
295 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700296 return -EINVAL;
297 }
298
Paul Walmsley43b40992010-02-22 22:09:34 -0700299 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700300 sidle_mask = (0x3 << sidle_shift);
301
302 *v &= ~sidle_mask;
303 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300304
305 return 0;
306}
307
308/**
309 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
310 * @oh: struct omap_hwmod *
311 * @clockact: CLOCKACTIVITY field bits
312 * @v: pointer to register contents to modify
313 *
314 * Update the clockactivity mode bits in @v to be @clockact for the
315 * @oh hwmod. Used for additional powersaving on some modules. Does
316 * not write to the hardware. Returns -EINVAL upon error or 0 upon
317 * success.
318 */
319static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
320{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700321 u32 clkact_mask;
322 u8 clkact_shift;
323
Paul Walmsley43b40992010-02-22 22:09:34 -0700324 if (!oh->class->sysc ||
325 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300326 return -EINVAL;
327
Paul Walmsley43b40992010-02-22 22:09:34 -0700328 if (!oh->class->sysc->sysc_fields) {
329 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700330 return -EINVAL;
331 }
332
Paul Walmsley43b40992010-02-22 22:09:34 -0700333 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700334 clkact_mask = (0x3 << clkact_shift);
335
336 *v &= ~clkact_mask;
337 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300338
339 return 0;
340}
341
342/**
343 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
344 * @oh: struct omap_hwmod *
345 * @v: pointer to register contents to modify
346 *
347 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
348 * error or 0 upon success.
349 */
350static int _set_softreset(struct omap_hwmod *oh, u32 *v)
351{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700352 u32 softrst_mask;
353
Paul Walmsley43b40992010-02-22 22:09:34 -0700354 if (!oh->class->sysc ||
355 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300356 return -EINVAL;
357
Paul Walmsley43b40992010-02-22 22:09:34 -0700358 if (!oh->class->sysc->sysc_fields) {
359 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700360 return -EINVAL;
361 }
362
Paul Walmsley43b40992010-02-22 22:09:34 -0700363 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700364
365 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300366
367 return 0;
368}
369
370/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700371 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
372 * @oh: struct omap_hwmod *
373 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
374 * @v: pointer to register contents to modify
375 *
376 * Update the module autoidle bit in @v to be @autoidle for the @oh
377 * hwmod. The autoidle bit controls whether the module can gate
378 * internal clocks automatically when it isn't doing anything; the
379 * exact function of this bit varies on a per-module basis. This
380 * function does not write to the hardware. Returns -EINVAL upon
381 * error or 0 upon success.
382 */
383static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
384 u32 *v)
385{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700386 u32 autoidle_mask;
387 u8 autoidle_shift;
388
Paul Walmsley43b40992010-02-22 22:09:34 -0700389 if (!oh->class->sysc ||
390 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700391 return -EINVAL;
392
Paul Walmsley43b40992010-02-22 22:09:34 -0700393 if (!oh->class->sysc->sysc_fields) {
394 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700395 return -EINVAL;
396 }
397
Paul Walmsley43b40992010-02-22 22:09:34 -0700398 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700399 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700400
401 *v &= ~autoidle_mask;
402 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700403
404 return 0;
405}
406
407/**
Govindraj Receec002011-12-16 14:36:58 -0700408 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
409 * @oh: struct omap_hwmod *
410 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
411 *
412 * Set or clear the I/O pad wakeup flag in the mux entries for the
413 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
414 * in memory. If the hwmod is currently idled, and the new idle
415 * values don't match the previous ones, this function will also
416 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
417 * currently idled, this function won't touch the hardware: the new
418 * mux settings are written to the SCM PADCTRL registers when the
419 * hwmod is idled. No return value.
420 */
421static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
422{
423 struct omap_device_pad *pad;
424 bool change = false;
425 u16 prev_idle;
426 int j;
427
428 if (!oh->mux || !oh->mux->enabled)
429 return;
430
431 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
432 pad = oh->mux->pads_dynamic[j];
433
434 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
435 continue;
436
437 prev_idle = pad->idle;
438
439 if (set_wake)
440 pad->idle |= OMAP_WAKEUP_EN;
441 else
442 pad->idle &= ~OMAP_WAKEUP_EN;
443
444 if (prev_idle != pad->idle)
445 change = true;
446 }
447
448 if (change && oh->_state == _HWMOD_STATE_IDLE)
449 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
450}
451
452/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300453 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
454 * @oh: struct omap_hwmod *
455 *
456 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
457 * upon error or 0 upon success.
458 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700459static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300460{
Paul Walmsley43b40992010-02-22 22:09:34 -0700461 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700462 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200463 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
464 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300465 return -EINVAL;
466
Paul Walmsley43b40992010-02-22 22:09:34 -0700467 if (!oh->class->sysc->sysc_fields) {
468 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700469 return -EINVAL;
470 }
471
Benoit Cousson1fe74112011-07-01 22:54:03 +0200472 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
473 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300474
Benoit Cousson86009eb2010-12-21 21:31:28 -0700475 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
476 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200477 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
478 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700479
Paul Walmsley63c85232009-09-03 20:14:03 +0300480 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
481
482 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
483
484 return 0;
485}
486
487/**
488 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
489 * @oh: struct omap_hwmod *
490 *
491 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
492 * upon error or 0 upon success.
493 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700494static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300495{
Paul Walmsley43b40992010-02-22 22:09:34 -0700496 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700497 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200498 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
499 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300500 return -EINVAL;
501
Paul Walmsley43b40992010-02-22 22:09:34 -0700502 if (!oh->class->sysc->sysc_fields) {
503 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700504 return -EINVAL;
505 }
506
Benoit Cousson1fe74112011-07-01 22:54:03 +0200507 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
508 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300509
Benoit Cousson86009eb2010-12-21 21:31:28 -0700510 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
511 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200512 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
513 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700514
Paul Walmsley63c85232009-09-03 20:14:03 +0300515 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
516
517 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
518
519 return 0;
520}
521
522/**
523 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
524 * @oh: struct omap_hwmod *
525 *
526 * Prevent the hardware module @oh from entering idle while the
527 * hardare module initiator @init_oh is active. Useful when a module
528 * will be accessed by a particular initiator (e.g., if a module will
529 * be accessed by the IVA, there should be a sleepdep between the IVA
530 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700531 * mode. If the clockdomain is marked as not needing autodeps, return
532 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
533 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300534 */
535static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
536{
537 if (!oh->_clk)
538 return -EINVAL;
539
Paul Walmsley570b54c2011-03-10 03:50:09 -0700540 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
541 return 0;
542
Paul Walmsley55ed9692010-01-26 20:12:59 -0700543 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300544}
545
546/**
547 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
548 * @oh: struct omap_hwmod *
549 *
550 * Allow the hardware module @oh to enter idle while the hardare
551 * module initiator @init_oh is active. Useful when a module will not
552 * be accessed by a particular initiator (e.g., if a module will not
553 * be accessed by the IVA, there should be no sleepdep between the IVA
554 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700555 * mode. If the clockdomain is marked as not needing autodeps, return
556 * 0 without doing anything. Returns -EINVAL upon error or passes
557 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300558 */
559static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
560{
561 if (!oh->_clk)
562 return -EINVAL;
563
Paul Walmsley570b54c2011-03-10 03:50:09 -0700564 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
565 return 0;
566
Paul Walmsley55ed9692010-01-26 20:12:59 -0700567 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300568}
569
570/**
571 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
572 * @oh: struct omap_hwmod *
573 *
574 * Called from _init_clocks(). Populates the @oh _clk (main
575 * functional clock pointer) if a main_clk is present. Returns 0 on
576 * success or -EINVAL on error.
577 */
578static int _init_main_clk(struct omap_hwmod *oh)
579{
Paul Walmsley63c85232009-09-03 20:14:03 +0300580 int ret = 0;
581
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700582 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300583 return 0;
584
Benoit Cousson63403382010-05-20 12:31:10 -0600585 oh->_clk = omap_clk_get_by_name(oh->main_clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600586 if (!oh->_clk) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600587 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
588 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600589 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600590 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300591
Benoit Cousson63403382010-05-20 12:31:10 -0600592 if (!oh->_clk->clkdm)
593 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
594 oh->main_clk, oh->_clk->name);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700595
Paul Walmsley63c85232009-09-03 20:14:03 +0300596 return ret;
597}
598
599/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600600 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300601 * @oh: struct omap_hwmod *
602 *
603 * Called from _init_clocks(). Populates the @oh OCP slave interface
604 * clock pointers. Returns 0 on success or -EINVAL on error.
605 */
606static int _init_interface_clks(struct omap_hwmod *oh)
607{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600608 struct omap_hwmod_ocp_if *os;
Paul Walmsley63c85232009-09-03 20:14:03 +0300609 struct clk *c;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600610 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300611 int ret = 0;
612
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600613 while (i < oh->slaves_cnt) {
614 os = _fetch_next_ocp_if(NULL, oh->slaves, &i);
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700615 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300616 continue;
617
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700618 c = omap_clk_get_by_name(os->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600619 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600620 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
621 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300622 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600623 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300624 os->_clk = c;
625 }
626
627 return ret;
628}
629
630/**
631 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
632 * @oh: struct omap_hwmod *
633 *
634 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
635 * clock pointers. Returns 0 on success or -EINVAL on error.
636 */
637static int _init_opt_clks(struct omap_hwmod *oh)
638{
639 struct omap_hwmod_opt_clk *oc;
640 struct clk *c;
641 int i;
642 int ret = 0;
643
644 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700645 c = omap_clk_get_by_name(oc->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600646 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600647 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
648 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300649 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600650 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300651 oc->_clk = c;
652 }
653
654 return ret;
655}
656
657/**
658 * _enable_clocks - enable hwmod main clock and interface clocks
659 * @oh: struct omap_hwmod *
660 *
661 * Enables all clocks necessary for register reads and writes to succeed
662 * on the hwmod @oh. Returns 0.
663 */
664static int _enable_clocks(struct omap_hwmod *oh)
665{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600666 struct omap_hwmod_ocp_if *os;
667 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300668
669 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
670
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600671 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300672 clk_enable(oh->_clk);
673
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600674 while (i < oh->slaves_cnt) {
675 os = _fetch_next_ocp_if(NULL, oh->slaves, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300676
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600677 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
678 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300679 }
680
681 /* The opt clocks are controlled by the device driver. */
682
683 return 0;
684}
685
686/**
687 * _disable_clocks - disable hwmod main clock and interface clocks
688 * @oh: struct omap_hwmod *
689 *
690 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
691 */
692static int _disable_clocks(struct omap_hwmod *oh)
693{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600694 struct omap_hwmod_ocp_if *os;
695 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300696
697 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
698
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600699 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300700 clk_disable(oh->_clk);
701
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600702 while (i < oh->slaves_cnt) {
703 os = _fetch_next_ocp_if(NULL, oh->slaves, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300704
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600705 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
706 clk_disable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300707 }
708
709 /* The opt clocks are controlled by the device driver. */
710
711 return 0;
712}
713
Benoit Cousson96835af2010-09-21 18:57:58 +0200714static void _enable_optional_clocks(struct omap_hwmod *oh)
715{
716 struct omap_hwmod_opt_clk *oc;
717 int i;
718
719 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
720
721 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
722 if (oc->_clk) {
723 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
724 oc->_clk->name);
725 clk_enable(oc->_clk);
726 }
727}
728
729static void _disable_optional_clocks(struct omap_hwmod *oh)
730{
731 struct omap_hwmod_opt_clk *oc;
732 int i;
733
734 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
735
736 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
737 if (oc->_clk) {
738 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
739 oc->_clk->name);
740 clk_disable(oc->_clk);
741 }
742}
743
Paul Walmsley63c85232009-09-03 20:14:03 +0300744/**
Benoit Cousson45c38252011-07-10 05:56:33 -0600745 * _enable_module - enable CLKCTRL modulemode on OMAP4
746 * @oh: struct omap_hwmod *
747 *
748 * Enables the PRCM module mode related to the hwmod @oh.
749 * No return value.
750 */
751static void _enable_module(struct omap_hwmod *oh)
752{
753 /* The module mode does not exist prior OMAP4 */
754 if (cpu_is_omap24xx() || cpu_is_omap34xx())
755 return;
756
757 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
758 return;
759
760 pr_debug("omap_hwmod: %s: _enable_module: %d\n",
761 oh->name, oh->prcm.omap4.modulemode);
762
763 omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
764 oh->clkdm->prcm_partition,
765 oh->clkdm->cm_inst,
766 oh->clkdm->clkdm_offs,
767 oh->prcm.omap4.clkctrl_offs);
768}
769
770/**
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800771 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
772 * @oh: struct omap_hwmod *
773 *
774 * Wait for a module @oh to enter slave idle. Returns 0 if the module
775 * does not have an IDLEST bit or if the module successfully enters
776 * slave idle; otherwise, pass along the return value of the
777 * appropriate *_cm*_wait_module_idle() function.
778 */
779static int _omap4_wait_target_disable(struct omap_hwmod *oh)
780{
781 if (!cpu_is_omap44xx())
782 return 0;
783
784 if (!oh)
785 return -EINVAL;
786
787 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
788 return 0;
789
790 if (oh->flags & HWMOD_NO_IDLEST)
791 return 0;
792
793 return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
794 oh->clkdm->cm_inst,
795 oh->clkdm->clkdm_offs,
796 oh->prcm.omap4.clkctrl_offs);
797}
798
799/**
Paul Walmsley212738a2011-07-09 19:14:06 -0600800 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
801 * @oh: struct omap_hwmod *oh
802 *
803 * Count and return the number of MPU IRQs associated with the hwmod
804 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
805 * NULL.
806 */
807static int _count_mpu_irqs(struct omap_hwmod *oh)
808{
809 struct omap_hwmod_irq_info *ohii;
810 int i = 0;
811
812 if (!oh || !oh->mpu_irqs)
813 return 0;
814
815 do {
816 ohii = &oh->mpu_irqs[i++];
817 } while (ohii->irq != -1);
818
sricharancc1b0762011-11-23 14:35:07 -0800819 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -0600820}
821
822/**
Paul Walmsleybc614952011-07-09 19:14:07 -0600823 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
824 * @oh: struct omap_hwmod *oh
825 *
826 * Count and return the number of SDMA request lines associated with
827 * the hwmod @oh. Used to allocate struct resource data. Returns 0
828 * if @oh is NULL.
829 */
830static int _count_sdma_reqs(struct omap_hwmod *oh)
831{
832 struct omap_hwmod_dma_info *ohdi;
833 int i = 0;
834
835 if (!oh || !oh->sdma_reqs)
836 return 0;
837
838 do {
839 ohdi = &oh->sdma_reqs[i++];
840 } while (ohdi->dma_req != -1);
841
sricharancc1b0762011-11-23 14:35:07 -0800842 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -0600843}
844
845/**
Paul Walmsley78183f32011-07-09 19:14:05 -0600846 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
847 * @oh: struct omap_hwmod *oh
848 *
849 * Count and return the number of address space ranges associated with
850 * the hwmod @oh. Used to allocate struct resource data. Returns 0
851 * if @oh is NULL.
852 */
853static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
854{
855 struct omap_hwmod_addr_space *mem;
856 int i = 0;
857
858 if (!os || !os->addr)
859 return 0;
860
861 do {
862 mem = &os->addr[i++];
863 } while (mem->pa_start != mem->pa_end);
864
sricharancc1b0762011-11-23 14:35:07 -0800865 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -0600866}
867
868/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -0600869 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
870 * @oh: struct omap_hwmod * to operate on
871 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
872 * @irq: pointer to an unsigned int to store the MPU IRQ number to
873 *
874 * Retrieve a MPU hardware IRQ line number named by @name associated
875 * with the IP block pointed to by @oh. The IRQ number will be filled
876 * into the address pointed to by @dma. When @name is non-null, the
877 * IRQ line number associated with the named entry will be returned.
878 * If @name is null, the first matching entry will be returned. Data
879 * order is not meaningful in hwmod data, so callers are strongly
880 * encouraged to use a non-null @name whenever possible to avoid
881 * unpredictable effects if hwmod data is later added that causes data
882 * ordering to change. Returns 0 upon success or a negative error
883 * code upon error.
884 */
885static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
886 unsigned int *irq)
887{
888 int i;
889 bool found = false;
890
891 if (!oh->mpu_irqs)
892 return -ENOENT;
893
894 i = 0;
895 while (oh->mpu_irqs[i].irq != -1) {
896 if (name == oh->mpu_irqs[i].name ||
897 !strcmp(name, oh->mpu_irqs[i].name)) {
898 found = true;
899 break;
900 }
901 i++;
902 }
903
904 if (!found)
905 return -ENOENT;
906
907 *irq = oh->mpu_irqs[i].irq;
908
909 return 0;
910}
911
912/**
913 * _get_sdma_req_by_name - fetch SDMA request line ID by name
914 * @oh: struct omap_hwmod * to operate on
915 * @name: pointer to the name of the SDMA request line to fetch (optional)
916 * @dma: pointer to an unsigned int to store the request line ID to
917 *
918 * Retrieve an SDMA request line ID named by @name on the IP block
919 * pointed to by @oh. The ID will be filled into the address pointed
920 * to by @dma. When @name is non-null, the request line ID associated
921 * with the named entry will be returned. If @name is null, the first
922 * matching entry will be returned. Data order is not meaningful in
923 * hwmod data, so callers are strongly encouraged to use a non-null
924 * @name whenever possible to avoid unpredictable effects if hwmod
925 * data is later added that causes data ordering to change. Returns 0
926 * upon success or a negative error code upon error.
927 */
928static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
929 unsigned int *dma)
930{
931 int i;
932 bool found = false;
933
934 if (!oh->sdma_reqs)
935 return -ENOENT;
936
937 i = 0;
938 while (oh->sdma_reqs[i].dma_req != -1) {
939 if (name == oh->sdma_reqs[i].name ||
940 !strcmp(name, oh->sdma_reqs[i].name)) {
941 found = true;
942 break;
943 }
944 i++;
945 }
946
947 if (!found)
948 return -ENOENT;
949
950 *dma = oh->sdma_reqs[i].dma_req;
951
952 return 0;
953}
954
955/**
956 * _get_addr_space_by_name - fetch address space start & end by name
957 * @oh: struct omap_hwmod * to operate on
958 * @name: pointer to the name of the address space to fetch (optional)
959 * @pa_start: pointer to a u32 to store the starting address to
960 * @pa_end: pointer to a u32 to store the ending address to
961 *
962 * Retrieve address space start and end addresses for the IP block
963 * pointed to by @oh. The data will be filled into the addresses
964 * pointed to by @pa_start and @pa_end. When @name is non-null, the
965 * address space data associated with the named entry will be
966 * returned. If @name is null, the first matching entry will be
967 * returned. Data order is not meaningful in hwmod data, so callers
968 * are strongly encouraged to use a non-null @name whenever possible
969 * to avoid unpredictable effects if hwmod data is later added that
970 * causes data ordering to change. Returns 0 upon success or a
971 * negative error code upon error.
972 */
973static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
974 u32 *pa_start, u32 *pa_end)
975{
976 int i, j;
977 struct omap_hwmod_ocp_if *os;
978 bool found = false;
979
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600980 i = 0;
981 while (i < oh->slaves_cnt) {
982 os = _fetch_next_ocp_if(NULL, oh->slaves, &i);
Paul Walmsley5e8370f2012-04-18 19:10:06 -0600983
984 if (!os->addr)
985 return -ENOENT;
986
987 j = 0;
988 while (os->addr[j].pa_start != os->addr[j].pa_end) {
989 if (name == os->addr[j].name ||
990 !strcmp(name, os->addr[j].name)) {
991 found = true;
992 break;
993 }
994 j++;
995 }
996
997 if (found)
998 break;
999 }
1000
1001 if (!found)
1002 return -ENOENT;
1003
1004 *pa_start = os->addr[j].pa_start;
1005 *pa_end = os->addr[j].pa_end;
1006
1007 return 0;
1008}
1009
1010/**
Paul Walmsley24dbc212012-04-19 04:04:29 -06001011 * _save_mpu_port_index - find and save the index to @oh's MPU port
Paul Walmsley63c85232009-09-03 20:14:03 +03001012 * @oh: struct omap_hwmod *
1013 *
Paul Walmsley24dbc212012-04-19 04:04:29 -06001014 * Determines the array index of the OCP slave port that the MPU uses
1015 * to address the device, and saves it into the struct omap_hwmod.
1016 * Intended to be called during hwmod registration only. No return
1017 * value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001018 */
Paul Walmsley24dbc212012-04-19 04:04:29 -06001019static void __init _save_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001020{
Paul Walmsley24dbc212012-04-19 04:04:29 -06001021 struct omap_hwmod_ocp_if *os = NULL;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001022 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001023
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001024 if (!oh)
Paul Walmsley24dbc212012-04-19 04:04:29 -06001025 return;
1026
1027 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001028
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001029 while (i < oh->slaves_cnt) {
1030 os = _fetch_next_ocp_if(NULL, oh->slaves, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +03001031 if (os->user & OCP_USER_MPU) {
Paul Walmsley24dbc212012-04-19 04:04:29 -06001032 oh->_mpu_port_index = i - 1;
1033 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001034 break;
1035 }
1036 }
1037
Paul Walmsley24dbc212012-04-19 04:04:29 -06001038 return;
Paul Walmsley63c85232009-09-03 20:14:03 +03001039}
1040
1041/**
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001042 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1043 * @oh: struct omap_hwmod *
1044 *
1045 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1046 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1047 * communicate with the IP block. This interface need not be directly
1048 * connected to the MPU (and almost certainly is not), but is directly
1049 * connected to the IP block represented by @oh. Returns a pointer
1050 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1051 * error or if there does not appear to be a path from the MPU to this
1052 * IP block.
1053 */
1054static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1055{
1056 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1057 return NULL;
1058
1059 return oh->slaves[oh->_mpu_port_index];
1060};
1061
1062/**
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001063 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
Paul Walmsley63c85232009-09-03 20:14:03 +03001064 * @oh: struct omap_hwmod *
1065 *
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001066 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1067 * the register target MPU address space; or returns NULL upon error.
Paul Walmsley63c85232009-09-03 20:14:03 +03001068 */
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001069static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001070{
1071 struct omap_hwmod_ocp_if *os;
1072 struct omap_hwmod_addr_space *mem;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001073 int found = 0, i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001074
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001075 os = _find_mpu_rt_port(oh);
Paul Walmsley24dbc212012-04-19 04:04:29 -06001076 if (!os || !os->addr)
Paul Walmsley78183f32011-07-09 19:14:05 -06001077 return NULL;
1078
1079 do {
1080 mem = &os->addr[i++];
1081 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +03001082 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001083 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +03001084
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001085 return (found) ? mem : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001086}
1087
1088/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001089 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001090 * @oh: struct omap_hwmod *
1091 *
1092 * If module is marked as SWSUP_SIDLE, force the module out of slave
1093 * idle; otherwise, configure it for smart-idle. If module is marked
1094 * as SWSUP_MSUSPEND, force the module out of master standby;
1095 * otherwise, configure it for smart-standby. No return value.
1096 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001097static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001098{
Paul Walmsley43b40992010-02-22 22:09:34 -07001099 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001100 u32 v;
1101
Paul Walmsley43b40992010-02-22 22:09:34 -07001102 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001103 return;
1104
1105 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001106 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001107
Paul Walmsley43b40992010-02-22 22:09:34 -07001108 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001109 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1110 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
1111 _set_slave_idlemode(oh, idlemode, &v);
1112 }
1113
Paul Walmsley43b40992010-02-22 22:09:34 -07001114 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001115 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1116 idlemode = HWMOD_IDLEMODE_NO;
1117 } else {
1118 if (sf & SYSC_HAS_ENAWAKEUP)
1119 _enable_wakeup(oh, &v);
1120 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1121 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1122 else
1123 idlemode = HWMOD_IDLEMODE_SMART;
1124 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001125 _set_master_standbymode(oh, idlemode, &v);
1126 }
1127
Paul Walmsleya16b1f72009-12-08 16:34:17 -07001128 /*
1129 * XXX The clock framework should handle this, by
1130 * calling into this code. But this must wait until the
1131 * clock structures are tagged with omap_hwmod entries
1132 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001133 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1134 (sf & SYSC_HAS_CLOCKACTIVITY))
1135 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001136
Rajendra Nayak9980ce52010-09-21 19:58:30 +05301137 /* If slave is in SMARTIDLE, also enable wakeup */
1138 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07001139 _enable_wakeup(oh, &v);
1140
1141 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -06001142
1143 /*
1144 * Set the autoidle bit only after setting the smartidle bit
1145 * Setting this will not have any impact on the other modules.
1146 */
1147 if (sf & SYSC_HAS_AUTOIDLE) {
1148 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1149 0 : 1;
1150 _set_module_autoidle(oh, idlemode, &v);
1151 _write_sysconfig(v, oh);
1152 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001153}
1154
1155/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001156 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001157 * @oh: struct omap_hwmod *
1158 *
1159 * If module is marked as SWSUP_SIDLE, force the module into slave
1160 * idle; otherwise, configure it for smart-idle. If module is marked
1161 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1162 * configure it for smart-standby. No return value.
1163 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001164static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001165{
Paul Walmsley43b40992010-02-22 22:09:34 -07001166 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001167 u32 v;
1168
Paul Walmsley43b40992010-02-22 22:09:34 -07001169 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001170 return;
1171
1172 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001173 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001174
Paul Walmsley43b40992010-02-22 22:09:34 -07001175 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001176 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1177 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
1178 _set_slave_idlemode(oh, idlemode, &v);
1179 }
1180
Paul Walmsley43b40992010-02-22 22:09:34 -07001181 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001182 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1183 idlemode = HWMOD_IDLEMODE_FORCE;
1184 } else {
1185 if (sf & SYSC_HAS_ENAWAKEUP)
1186 _enable_wakeup(oh, &v);
1187 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1188 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1189 else
1190 idlemode = HWMOD_IDLEMODE_SMART;
1191 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001192 _set_master_standbymode(oh, idlemode, &v);
1193 }
1194
Benoit Cousson86009eb2010-12-21 21:31:28 -07001195 /* If slave is in SMARTIDLE, also enable wakeup */
1196 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1197 _enable_wakeup(oh, &v);
1198
Paul Walmsley63c85232009-09-03 20:14:03 +03001199 _write_sysconfig(v, oh);
1200}
1201
1202/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001203 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001204 * @oh: struct omap_hwmod *
1205 *
1206 * Force the module into slave idle and master suspend. No return
1207 * value.
1208 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001209static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001210{
1211 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001212 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001213
Paul Walmsley43b40992010-02-22 22:09:34 -07001214 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001215 return;
1216
1217 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001218 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001219
Paul Walmsley43b40992010-02-22 22:09:34 -07001220 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001221 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1222
Paul Walmsley43b40992010-02-22 22:09:34 -07001223 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001224 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1225
Paul Walmsley43b40992010-02-22 22:09:34 -07001226 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001227 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001228
1229 _write_sysconfig(v, oh);
1230}
1231
1232/**
1233 * _lookup - find an omap_hwmod by name
1234 * @name: find an omap_hwmod by name
1235 *
1236 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001237 */
1238static struct omap_hwmod *_lookup(const char *name)
1239{
1240 struct omap_hwmod *oh, *temp_oh;
1241
1242 oh = NULL;
1243
1244 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1245 if (!strcmp(name, temp_oh->name)) {
1246 oh = temp_oh;
1247 break;
1248 }
1249 }
1250
1251 return oh;
1252}
Benoit Cousson6ae76992011-07-10 05:56:30 -06001253/**
1254 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1255 * @oh: struct omap_hwmod *
1256 *
1257 * Convert a clockdomain name stored in a struct omap_hwmod into a
1258 * clockdomain pointer, and save it into the struct omap_hwmod.
1259 * return -EINVAL if clkdm_name does not exist or if the lookup failed.
1260 */
1261static int _init_clkdm(struct omap_hwmod *oh)
1262{
1263 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1264 return 0;
1265
1266 if (!oh->clkdm_name) {
1267 pr_warning("omap_hwmod: %s: no clkdm_name\n", oh->name);
1268 return -EINVAL;
1269 }
1270
1271 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1272 if (!oh->clkdm) {
1273 pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
1274 oh->name, oh->clkdm_name);
1275 return -EINVAL;
1276 }
1277
1278 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1279 oh->name, oh->clkdm_name);
1280
1281 return 0;
1282}
Paul Walmsley63c85232009-09-03 20:14:03 +03001283
1284/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001285 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1286 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001287 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -06001288 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001289 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001290 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001291 * Resolves all clock names embedded in the hwmod. Returns 0 on
1292 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001293 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001294static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001295{
1296 int ret = 0;
1297
Paul Walmsley48d54f32011-02-23 00:14:07 -07001298 if (oh->_state != _HWMOD_STATE_REGISTERED)
1299 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001300
1301 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1302
1303 ret |= _init_main_clk(oh);
1304 ret |= _init_interface_clks(oh);
1305 ret |= _init_opt_clks(oh);
Benoit Cousson6ae76992011-07-10 05:56:30 -06001306 ret |= _init_clkdm(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001307
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001308 if (!ret)
1309 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001310 else
1311 pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001312
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001313 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001314}
1315
1316/**
1317 * _wait_target_ready - wait for a module to leave slave idle
1318 * @oh: struct omap_hwmod *
1319 *
1320 * Wait for a module @oh to leave slave idle. Returns 0 if the module
1321 * does not have an IDLEST bit or if the module successfully leaves
1322 * slave idle; otherwise, pass along the return value of the
Benoit Coussond0f06312011-07-10 05:56:30 -06001323 * appropriate *_cm*_wait_module_ready() function.
Paul Walmsley63c85232009-09-03 20:14:03 +03001324 */
1325static int _wait_target_ready(struct omap_hwmod *oh)
1326{
1327 struct omap_hwmod_ocp_if *os;
1328 int ret;
1329
1330 if (!oh)
1331 return -EINVAL;
1332
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001333 if (oh->flags & HWMOD_NO_IDLEST)
Paul Walmsley63c85232009-09-03 20:14:03 +03001334 return 0;
1335
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001336 os = _find_mpu_rt_port(oh);
1337 if (!os)
Paul Walmsley63c85232009-09-03 20:14:03 +03001338 return 0;
1339
1340 /* XXX check module SIDLEMODE */
1341
1342 /* XXX check clock enable states */
1343
1344 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1345 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
1346 oh->prcm.omap2.idlest_reg_id,
1347 oh->prcm.omap2.idlest_idle_bit);
Paul Walmsley63c85232009-09-03 20:14:03 +03001348 } else if (cpu_is_omap44xx()) {
Benoit Coussond0f06312011-07-10 05:56:30 -06001349 if (!oh->clkdm)
1350 return -EINVAL;
1351
1352 ret = omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
1353 oh->clkdm->cm_inst,
1354 oh->clkdm->clkdm_offs,
1355 oh->prcm.omap4.clkctrl_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001356 } else {
1357 BUG();
1358 };
1359
1360 return ret;
1361}
1362
1363/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001364 * _lookup_hardreset - fill register bit info for this hwmod/reset line
Benoît Cousson5365efb2010-09-21 10:34:11 -06001365 * @oh: struct omap_hwmod *
1366 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001367 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
Benoît Cousson5365efb2010-09-21 10:34:11 -06001368 *
1369 * Return the bit position of the reset line that match the
1370 * input name. Return -ENOENT if not found.
1371 */
omar ramirezcc1226e2011-03-04 13:32:44 -07001372static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1373 struct omap_hwmod_rst_info *ohri)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001374{
1375 int i;
1376
1377 for (i = 0; i < oh->rst_lines_cnt; i++) {
1378 const char *rst_line = oh->rst_lines[i].name;
1379 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001380 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1381 ohri->st_shift = oh->rst_lines[i].st_shift;
1382 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1383 oh->name, __func__, rst_line, ohri->rst_shift,
1384 ohri->st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001385
omar ramirezcc1226e2011-03-04 13:32:44 -07001386 return 0;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001387 }
1388 }
1389
1390 return -ENOENT;
1391}
1392
1393/**
1394 * _assert_hardreset - assert the HW reset line of submodules
1395 * contained in the hwmod module.
1396 * @oh: struct omap_hwmod *
1397 * @name: name of the reset line to lookup and assert
1398 *
1399 * Some IP like dsp, ipu or iva contain processor that require
1400 * an HW reset line to be assert / deassert in order to enable fully
1401 * the IP.
1402 */
1403static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1404{
omar ramirezcc1226e2011-03-04 13:32:44 -07001405 struct omap_hwmod_rst_info ohri;
1406 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001407
1408 if (!oh)
1409 return -EINVAL;
1410
omar ramirezcc1226e2011-03-04 13:32:44 -07001411 ret = _lookup_hardreset(oh, name, &ohri);
1412 if (IS_ERR_VALUE(ret))
1413 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001414
1415 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1416 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001417 ohri.rst_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001418 else if (cpu_is_omap44xx())
Benoit Coussoneaac3292011-07-10 05:56:31 -06001419 return omap4_prminst_assert_hardreset(ohri.rst_shift,
1420 oh->clkdm->pwrdm.ptr->prcm_partition,
1421 oh->clkdm->pwrdm.ptr->prcm_offs,
1422 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001423 else
1424 return -EINVAL;
1425}
1426
1427/**
1428 * _deassert_hardreset - deassert the HW reset line of submodules contained
1429 * in the hwmod module.
1430 * @oh: struct omap_hwmod *
1431 * @name: name of the reset line to look up and deassert
1432 *
1433 * Some IP like dsp, ipu or iva contain processor that require
1434 * an HW reset line to be assert / deassert in order to enable fully
1435 * the IP.
1436 */
1437static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1438{
omar ramirezcc1226e2011-03-04 13:32:44 -07001439 struct omap_hwmod_rst_info ohri;
1440 int ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001441
1442 if (!oh)
1443 return -EINVAL;
1444
omar ramirezcc1226e2011-03-04 13:32:44 -07001445 ret = _lookup_hardreset(oh, name, &ohri);
1446 if (IS_ERR_VALUE(ret))
1447 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001448
omar ramirezcc1226e2011-03-04 13:32:44 -07001449 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1450 ret = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
1451 ohri.rst_shift,
1452 ohri.st_shift);
1453 } else if (cpu_is_omap44xx()) {
1454 if (ohri.st_shift)
1455 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
1456 oh->name, name);
Benoit Coussoneaac3292011-07-10 05:56:31 -06001457 ret = omap4_prminst_deassert_hardreset(ohri.rst_shift,
1458 oh->clkdm->pwrdm.ptr->prcm_partition,
1459 oh->clkdm->pwrdm.ptr->prcm_offs,
1460 oh->prcm.omap4.rstctrl_offs);
omar ramirezcc1226e2011-03-04 13:32:44 -07001461 } else {
Benoît Cousson5365efb2010-09-21 10:34:11 -06001462 return -EINVAL;
omar ramirezcc1226e2011-03-04 13:32:44 -07001463 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001464
omar ramirezcc1226e2011-03-04 13:32:44 -07001465 if (ret == -EBUSY)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001466 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1467
omar ramirezcc1226e2011-03-04 13:32:44 -07001468 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001469}
1470
1471/**
1472 * _read_hardreset - read the HW reset line state of submodules
1473 * contained in the hwmod module
1474 * @oh: struct omap_hwmod *
1475 * @name: name of the reset line to look up and read
1476 *
1477 * Return the state of the reset line.
1478 */
1479static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1480{
omar ramirezcc1226e2011-03-04 13:32:44 -07001481 struct omap_hwmod_rst_info ohri;
1482 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001483
1484 if (!oh)
1485 return -EINVAL;
1486
omar ramirezcc1226e2011-03-04 13:32:44 -07001487 ret = _lookup_hardreset(oh, name, &ohri);
1488 if (IS_ERR_VALUE(ret))
1489 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001490
1491 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1492 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001493 ohri.st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001494 } else if (cpu_is_omap44xx()) {
Benoit Coussoneaac3292011-07-10 05:56:31 -06001495 return omap4_prminst_is_hardreset_asserted(ohri.rst_shift,
1496 oh->clkdm->pwrdm.ptr->prcm_partition,
1497 oh->clkdm->pwrdm.ptr->prcm_offs,
1498 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001499 } else {
1500 return -EINVAL;
1501 }
1502}
1503
1504/**
Paul Walmsley747834a2012-04-18 19:10:04 -06001505 * _are_any_hardreset_lines_asserted - return true if part of @oh is hard-reset
1506 * @oh: struct omap_hwmod *
1507 *
1508 * If any hardreset line associated with @oh is asserted, then return true.
1509 * Otherwise, if @oh has no hardreset lines associated with it, or if
1510 * no hardreset lines associated with @oh are asserted, then return false.
1511 * This function is used to avoid executing some parts of the IP block
1512 * enable/disable sequence if a hardreset line is set.
1513 */
1514static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1515{
1516 int i;
1517
1518 if (oh->rst_lines_cnt == 0)
1519 return false;
1520
1521 for (i = 0; i < oh->rst_lines_cnt; i++)
1522 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1523 return true;
1524
1525 return false;
1526}
1527
1528/**
1529 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1530 * @oh: struct omap_hwmod *
1531 *
1532 * Disable the PRCM module mode related to the hwmod @oh.
1533 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1534 */
1535static int _omap4_disable_module(struct omap_hwmod *oh)
1536{
1537 int v;
1538
1539 /* The module mode does not exist prior OMAP4 */
1540 if (!cpu_is_omap44xx())
1541 return -EINVAL;
1542
1543 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1544 return -EINVAL;
1545
1546 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1547
1548 omap4_cminst_module_disable(oh->clkdm->prcm_partition,
1549 oh->clkdm->cm_inst,
1550 oh->clkdm->clkdm_offs,
1551 oh->prcm.omap4.clkctrl_offs);
1552
1553 if (_are_any_hardreset_lines_asserted(oh))
1554 return 0;
1555
1556 v = _omap4_wait_target_disable(oh);
1557 if (v)
1558 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1559 oh->name);
1560
1561 return 0;
1562}
1563
1564/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001565 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001566 * @oh: struct omap_hwmod *
1567 *
1568 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Paul Walmsley30e105c2012-04-19 00:49:09 -06001569 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1570 * reset this way, -EINVAL if the hwmod is in the wrong state,
1571 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001572 *
1573 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001574 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001575 * use the SYSCONFIG softreset bit to provide the status.
1576 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001577 * Note that some IP like McBSP do have reset control but don't have
1578 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001579 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001580static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001581{
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001582 u32 v, softrst_mask;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001583 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001584 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001585
Paul Walmsley43b40992010-02-22 22:09:34 -07001586 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001587 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley30e105c2012-04-19 00:49:09 -06001588 return -ENOENT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001589
1590 /* clocks must be on for this operation */
1591 if (oh->_state != _HWMOD_STATE_ENABLED) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001592 pr_warning("omap_hwmod: %s: reset can only be entered from "
1593 "enabled state\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001594 return -EINVAL;
1595 }
1596
Benoit Cousson96835af2010-09-21 18:57:58 +02001597 /* For some modules, all optionnal clocks need to be enabled as well */
1598 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1599 _enable_optional_clocks(oh);
1600
Paul Walmsleybd361792010-12-14 12:42:35 -07001601 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001602
1603 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001604 ret = _set_softreset(oh, &v);
1605 if (ret)
1606 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001607 _write_sysconfig(v, oh);
1608
Fernando Guzman Lugod99de7f2012-04-13 05:08:03 -06001609 if (oh->class->sysc->srst_udelay)
1610 udelay(oh->class->sysc->srst_udelay);
1611
Benoit Cousson2cb06812010-09-21 18:57:59 +02001612 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001613 omap_test_timeout((omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001614 oh->class->sysc->syss_offs)
1615 & SYSS_RESETDONE_MASK),
1616 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001617 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
1618 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001619 omap_test_timeout(!(omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001620 oh->class->sysc->sysc_offs)
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001621 & softrst_mask),
Benoit Cousson2cb06812010-09-21 18:57:59 +02001622 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001623 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001624
Benoît Cousson5365efb2010-09-21 10:34:11 -06001625 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001626 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1627 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001628 else
Benoît Cousson5365efb2010-09-21 10:34:11 -06001629 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001630
1631 /*
1632 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1633 * _wait_target_ready() or _reset()
1634 */
1635
Benoit Cousson96835af2010-09-21 18:57:58 +02001636 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1637
1638dis_opt_clks:
1639 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1640 _disable_optional_clocks(oh);
1641
1642 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001643}
1644
1645/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001646 * _reset - reset an omap_hwmod
1647 * @oh: struct omap_hwmod *
1648 *
Paul Walmsley30e105c2012-04-19 00:49:09 -06001649 * Resets an omap_hwmod @oh. If the module has a custom reset
1650 * function pointer defined, then call it to reset the IP block, and
1651 * pass along its return value to the caller. Otherwise, if the IP
1652 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1653 * associated with it, call a function to reset the IP block via that
1654 * method, and pass along the return value to the caller. Finally, if
1655 * the IP block has some hardreset lines associated with it, assert
1656 * all of those, but do _not_ deassert them. (This is because driver
1657 * authors have expressed an apparent requirement to control the
1658 * deassertion of the hardreset lines themselves.)
1659 *
1660 * The default software reset mechanism for most OMAP IP blocks is
1661 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1662 * hwmods cannot be reset via this method. Some are not targets and
1663 * therefore have no OCP header registers to access. Others (like the
1664 * IVA) have idiosyncratic reset sequences. So for these relatively
1665 * rare cases, custom reset code can be supplied in the struct
1666 * omap_hwmod_class .reset function pointer. Passes along the return
1667 * value from either _ocp_softreset() or the custom reset function -
1668 * these must return -EINVAL if the hwmod cannot be reset this way or
1669 * if the hwmod is in the wrong state, -ETIMEDOUT if the module did
1670 * not reset in time, or 0 upon success.
Paul Walmsleybd361792010-12-14 12:42:35 -07001671 */
1672static int _reset(struct omap_hwmod *oh)
1673{
Paul Walmsley30e105c2012-04-19 00:49:09 -06001674 int i, r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001675
1676 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1677
Paul Walmsley30e105c2012-04-19 00:49:09 -06001678 if (oh->class->reset) {
1679 r = oh->class->reset(oh);
1680 } else {
1681 if (oh->rst_lines_cnt > 0) {
1682 for (i = 0; i < oh->rst_lines_cnt; i++)
1683 _assert_hardreset(oh, oh->rst_lines[i].name);
1684 return 0;
1685 } else {
1686 r = _ocp_softreset(oh);
1687 if (r == -ENOENT)
1688 r = 0;
1689 }
1690 }
Paul Walmsleybd361792010-12-14 12:42:35 -07001691
Paul Walmsley30e105c2012-04-19 00:49:09 -06001692 /*
1693 * OCP_SYSCONFIG bits need to be reprogrammed after a
1694 * softreset. The _enable() function should be split to avoid
1695 * the rewrite of the OCP_SYSCONFIG register.
1696 */
Rajendra Nayak28008522012-03-13 22:55:23 +05301697 if (oh->class->sysc) {
1698 _update_sysc_cache(oh);
1699 _enable_sysc(oh);
1700 }
1701
Paul Walmsley30e105c2012-04-19 00:49:09 -06001702 return r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001703}
1704
1705/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001706 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001707 * @oh: struct omap_hwmod *
1708 *
1709 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001710 * register target. Returns -EINVAL if the hwmod is in the wrong
1711 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001712 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001713static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001714{
Paul Walmsley747834a2012-04-18 19:10:04 -06001715 int r;
Rajendra Nayak665d0012011-07-10 05:57:07 -06001716 int hwsup = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001717
Benoit Cousson34617e22011-07-01 22:54:07 +02001718 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1719
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001720 /*
Paul Walmsley64813c32012-04-18 19:10:03 -06001721 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
1722 * state at init. Now that someone is really trying to enable
1723 * them, just ensure that the hwmod mux is set.
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001724 */
1725 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
1726 /*
1727 * If the caller has mux data populated, do the mux'ing
1728 * which wouldn't have been done as part of the _enable()
1729 * done during setup.
1730 */
1731 if (oh->mux)
1732 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
1733
1734 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
1735 return 0;
1736 }
1737
Paul Walmsley63c85232009-09-03 20:14:03 +03001738 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1739 oh->_state != _HWMOD_STATE_IDLE &&
1740 oh->_state != _HWMOD_STATE_DISABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001741 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
1742 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001743 return -EINVAL;
1744 }
1745
Benoit Cousson31f62862011-07-01 22:54:05 +02001746 /*
Paul Walmsley747834a2012-04-18 19:10:04 -06001747 * If an IP block contains HW reset lines and any of them are
1748 * asserted, we let integration code associated with that
1749 * block handle the enable. We've received very little
1750 * information on what those driver authors need, and until
1751 * detailed information is provided and the driver code is
1752 * posted to the public lists, this is probably the best we
1753 * can do.
Benoit Cousson31f62862011-07-01 22:54:05 +02001754 */
Paul Walmsley747834a2012-04-18 19:10:04 -06001755 if (_are_any_hardreset_lines_asserted(oh))
1756 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001757
Rajendra Nayak665d0012011-07-10 05:57:07 -06001758 /* Mux pins for device runtime if populated */
1759 if (oh->mux && (!oh->mux->enabled ||
1760 ((oh->_state == _HWMOD_STATE_IDLE) &&
1761 oh->mux->pads_dynamic)))
1762 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
Benoit Cousson34617e22011-07-01 22:54:07 +02001763
Rajendra Nayak665d0012011-07-10 05:57:07 -06001764 _add_initiator_dep(oh, mpu_oh);
1765
1766 if (oh->clkdm) {
1767 /*
1768 * A clockdomain must be in SW_SUP before enabling
1769 * completely the module. The clockdomain can be set
1770 * in HW_AUTO only when the module become ready.
1771 */
1772 hwsup = clkdm_in_hwsup(oh->clkdm);
1773 r = clkdm_hwmod_enable(oh->clkdm, oh);
1774 if (r) {
1775 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1776 oh->name, oh->clkdm->name, r);
1777 return r;
1778 }
Benoit Cousson34617e22011-07-01 22:54:07 +02001779 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06001780
1781 _enable_clocks(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001782 _enable_module(oh);
Benoit Cousson34617e22011-07-01 22:54:07 +02001783
Rajendra Nayak665d0012011-07-10 05:57:07 -06001784 r = _wait_target_ready(oh);
1785 if (!r) {
1786 /*
1787 * Set the clockdomain to HW_AUTO only if the target is ready,
1788 * assuming that the previous state was HW_AUTO
1789 */
1790 if (oh->clkdm && hwsup)
1791 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02001792
Rajendra Nayak665d0012011-07-10 05:57:07 -06001793 oh->_state = _HWMOD_STATE_ENABLED;
1794
1795 /* Access the sysconfig only if the target is ready */
1796 if (oh->class->sysc) {
1797 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1798 _update_sysc_cache(oh);
1799 _enable_sysc(oh);
1800 }
1801 } else {
1802 _disable_clocks(oh);
1803 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1804 oh->name, r);
1805
1806 if (oh->clkdm)
1807 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001808 }
1809
Paul Walmsley63c85232009-09-03 20:14:03 +03001810 return r;
1811}
1812
1813/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001814 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001815 * @oh: struct omap_hwmod *
1816 *
1817 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001818 * no further work. Returns -EINVAL if the hwmod is in the wrong
1819 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001820 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001821static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001822{
Benoit Cousson34617e22011-07-01 22:54:07 +02001823 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1824
Paul Walmsley63c85232009-09-03 20:14:03 +03001825 if (oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001826 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
1827 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001828 return -EINVAL;
1829 }
1830
Paul Walmsley747834a2012-04-18 19:10:04 -06001831 if (_are_any_hardreset_lines_asserted(oh))
1832 return 0;
1833
Paul Walmsley43b40992010-02-22 22:09:34 -07001834 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001835 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001836 _del_initiator_dep(oh, mpu_oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001837
1838 _omap4_disable_module(oh);
1839
Benoit Cousson45c38252011-07-10 05:56:33 -06001840 /*
1841 * The module must be in idle mode before disabling any parents
1842 * clocks. Otherwise, the parent clock might be disabled before
1843 * the module transition is done, and thus will prevent the
1844 * transition to complete properly.
1845 */
1846 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001847 if (oh->clkdm)
1848 clkdm_hwmod_disable(oh->clkdm, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001849
Tony Lindgren8d9af882010-12-22 18:42:35 -08001850 /* Mux pins for device idle if populated */
Tony Lindgren029268e2011-03-11 11:32:25 -08001851 if (oh->mux && oh->mux->pads_dynamic)
Tony Lindgren8d9af882010-12-22 18:42:35 -08001852 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
1853
Paul Walmsley63c85232009-09-03 20:14:03 +03001854 oh->_state = _HWMOD_STATE_IDLE;
1855
1856 return 0;
1857}
1858
1859/**
Kishon Vijay Abraham I95992172011-03-10 03:50:08 -07001860 * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
1861 * @oh: struct omap_hwmod *
1862 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
1863 *
1864 * Sets the IP block's OCP autoidle bit in hardware, and updates our
1865 * local copy. Intended to be used by drivers that require
1866 * direct manipulation of the AUTOIDLE bits.
1867 * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
1868 * along the return value from _set_module_autoidle().
1869 *
1870 * Any users of this function should be scrutinized carefully.
1871 */
1872int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
1873{
1874 u32 v;
1875 int retval = 0;
1876 unsigned long flags;
1877
1878 if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
1879 return -EINVAL;
1880
1881 spin_lock_irqsave(&oh->_lock, flags);
1882
1883 v = oh->_sysc_cache;
1884
1885 retval = _set_module_autoidle(oh, autoidle, &v);
1886
1887 if (!retval)
1888 _write_sysconfig(v, oh);
1889
1890 spin_unlock_irqrestore(&oh->_lock, flags);
1891
1892 return retval;
1893}
1894
1895/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001896 * _shutdown - shutdown an omap_hwmod
1897 * @oh: struct omap_hwmod *
1898 *
1899 * Shut down an omap_hwmod @oh. This should be called when the driver
1900 * used for the hwmod is removed or unloaded or if the driver is not
1901 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1902 * state or returns 0.
1903 */
1904static int _shutdown(struct omap_hwmod *oh)
1905{
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001906 int ret, i;
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001907 u8 prev_state;
1908
Paul Walmsley63c85232009-09-03 20:14:03 +03001909 if (oh->_state != _HWMOD_STATE_IDLE &&
1910 oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001911 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
1912 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001913 return -EINVAL;
1914 }
1915
Paul Walmsley747834a2012-04-18 19:10:04 -06001916 if (_are_any_hardreset_lines_asserted(oh))
1917 return 0;
1918
Paul Walmsley63c85232009-09-03 20:14:03 +03001919 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
1920
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001921 if (oh->class->pre_shutdown) {
1922 prev_state = oh->_state;
1923 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001924 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001925 ret = oh->class->pre_shutdown(oh);
1926 if (ret) {
1927 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001928 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001929 return ret;
1930 }
1931 }
1932
Miguel Vadillo6481c732011-07-01 22:54:02 +02001933 if (oh->class->sysc) {
1934 if (oh->_state == _HWMOD_STATE_IDLE)
1935 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001936 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02001937 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001938
Benoit Cousson3827f942010-09-21 10:34:08 -06001939 /* clocks and deps are already disabled in idle */
1940 if (oh->_state == _HWMOD_STATE_ENABLED) {
1941 _del_initiator_dep(oh, mpu_oh);
1942 /* XXX what about the other system initiators here? dma, dsp */
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001943 _omap4_disable_module(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001944 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001945 if (oh->clkdm)
1946 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06001947 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001948 /* XXX Should this code also force-disable the optional clocks? */
1949
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001950 for (i = 0; i < oh->rst_lines_cnt; i++)
1951 _assert_hardreset(oh, oh->rst_lines[i].name);
Benoit Cousson31f62862011-07-01 22:54:05 +02001952
Tony Lindgren8d9af882010-12-22 18:42:35 -08001953 /* Mux pins to safe mode or use populated off mode values */
1954 if (oh->mux)
1955 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03001956
1957 oh->_state = _HWMOD_STATE_DISABLED;
1958
1959 return 0;
1960}
1961
1962/**
Paul Walmsley381d0332012-04-19 00:58:22 -06001963 * _init_mpu_rt_base - populate the virtual address for a hwmod
1964 * @oh: struct omap_hwmod * to locate the virtual address
1965 *
1966 * Cache the virtual address used by the MPU to access this IP block's
1967 * registers. This address is needed early so the OCP registers that
1968 * are part of the device's address space can be ioremapped properly.
1969 * No return value.
1970 */
1971static void __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data)
1972{
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001973 struct omap_hwmod_addr_space *mem;
1974 void __iomem *va_start;
1975
1976 if (!oh)
1977 return;
1978
Paul Walmsley381d0332012-04-19 00:58:22 -06001979 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1980 return;
1981
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001982 mem = _find_mpu_rt_addr_space(oh);
1983 if (!mem) {
1984 pr_debug("omap_hwmod: %s: no MPU register target found\n",
1985 oh->name);
1986 return;
1987 }
1988
1989 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
1990 if (!va_start) {
1991 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
1992 return;
1993 }
1994
1995 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
1996 oh->name, va_start);
1997
1998 oh->_mpu_rt_va = va_start;
Paul Walmsley381d0332012-04-19 00:58:22 -06001999}
2000
2001/**
2002 * _init - initialize internal data for the hwmod @oh
2003 * @oh: struct omap_hwmod *
2004 * @n: (unused)
2005 *
2006 * Look up the clocks and the address space used by the MPU to access
2007 * registers belonging to the hwmod @oh. @oh must already be
2008 * registered at this point. This is the first of two phases for
2009 * hwmod initialization. Code called here does not touch any hardware
2010 * registers, it simply prepares internal data structures. Returns 0
2011 * upon success or if the hwmod isn't registered, or -EINVAL upon
2012 * failure.
2013 */
2014static int __init _init(struct omap_hwmod *oh, void *data)
2015{
2016 int r;
2017
2018 if (oh->_state != _HWMOD_STATE_REGISTERED)
2019 return 0;
2020
2021 _init_mpu_rt_base(oh, NULL);
2022
2023 r = _init_clocks(oh, NULL);
2024 if (IS_ERR_VALUE(r)) {
2025 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2026 return -EINVAL;
2027 }
2028
2029 oh->_state = _HWMOD_STATE_INITIALIZED;
2030
2031 return 0;
2032}
2033
2034/**
Paul Walmsley64813c32012-04-18 19:10:03 -06002035 * _setup_iclk_autoidle - configure an IP block's interface clocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002036 * @oh: struct omap_hwmod *
2037 *
Paul Walmsley64813c32012-04-18 19:10:03 -06002038 * Set up the module's interface clocks. XXX This function is still mostly
2039 * a stub; implementing this properly requires iclk autoidle usecounting in
2040 * the clock code. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03002041 */
Paul Walmsley64813c32012-04-18 19:10:03 -06002042static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002043{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002044 struct omap_hwmod_ocp_if *os;
2045 int i = 0;
Paul Walmsley381d0332012-04-19 00:58:22 -06002046 if (oh->_state != _HWMOD_STATE_INITIALIZED)
Paul Walmsley64813c32012-04-18 19:10:03 -06002047 return;
Paul Walmsley48d54f32011-02-23 00:14:07 -07002048
Paul Walmsley63c85232009-09-03 20:14:03 +03002049
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002050 while (i < oh->slaves_cnt) {
2051 os = _fetch_next_ocp_if(NULL, oh->slaves, &i);
2052 if (!os->_clk)
Paul Walmsley64813c32012-04-18 19:10:03 -06002053 continue;
Paul Walmsley63c85232009-09-03 20:14:03 +03002054
Paul Walmsley64813c32012-04-18 19:10:03 -06002055 if (os->flags & OCPIF_SWSUP_IDLE) {
2056 /* XXX omap_iclk_deny_idle(c); */
2057 } else {
2058 /* XXX omap_iclk_allow_idle(c); */
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002059 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +03002060 }
2061 }
2062
Paul Walmsley64813c32012-04-18 19:10:03 -06002063 return;
2064}
2065
2066/**
2067 * _setup_reset - reset an IP block during the setup process
2068 * @oh: struct omap_hwmod *
2069 *
2070 * Reset the IP block corresponding to the hwmod @oh during the setup
2071 * process. The IP block is first enabled so it can be successfully
2072 * reset. Returns 0 upon success or a negative error code upon
2073 * failure.
2074 */
2075static int __init _setup_reset(struct omap_hwmod *oh)
2076{
2077 int r;
2078
2079 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2080 return -EINVAL;
Paul Walmsley63c85232009-09-03 20:14:03 +03002081
Paul Walmsley747834a2012-04-18 19:10:04 -06002082 if (oh->rst_lines_cnt == 0) {
2083 r = _enable(oh);
2084 if (r) {
2085 pr_warning("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2086 oh->name, oh->_state);
2087 return -EINVAL;
2088 }
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002089 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002090
Rajendra Nayak28008522012-03-13 22:55:23 +05302091 if (!(oh->flags & HWMOD_INIT_NO_RESET))
Paul Walmsley64813c32012-04-18 19:10:03 -06002092 r = _reset(oh);
2093
2094 return r;
2095}
2096
2097/**
2098 * _setup_postsetup - transition to the appropriate state after _setup
2099 * @oh: struct omap_hwmod *
2100 *
2101 * Place an IP block represented by @oh into a "post-setup" state --
2102 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2103 * this function is called at the end of _setup().) The postsetup
2104 * state for an IP block can be changed by calling
2105 * omap_hwmod_enter_postsetup_state() early in the boot process,
2106 * before one of the omap_hwmod_setup*() functions are called for the
2107 * IP block.
2108 *
2109 * The IP block stays in this state until a PM runtime-based driver is
2110 * loaded for that IP block. A post-setup state of IDLE is
2111 * appropriate for almost all IP blocks with runtime PM-enabled
2112 * drivers, since those drivers are able to enable the IP block. A
2113 * post-setup state of ENABLED is appropriate for kernels with PM
2114 * runtime disabled. The DISABLED state is appropriate for unusual IP
2115 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2116 * included, since the WDTIMER starts running on reset and will reset
2117 * the MPU if left active.
2118 *
2119 * This post-setup mechanism is deprecated. Once all of the OMAP
2120 * drivers have been converted to use PM runtime, and all of the IP
2121 * block data and interconnect data is available to the hwmod code, it
2122 * should be possible to replace this mechanism with a "lazy reset"
2123 * arrangement. In a "lazy reset" setup, each IP block is enabled
2124 * when the driver first probes, then all remaining IP blocks without
2125 * drivers are either shut down or enabled after the drivers have
2126 * loaded. However, this cannot take place until the above
2127 * preconditions have been met, since otherwise the late reset code
2128 * has no way of knowing which IP blocks are in use by drivers, and
2129 * which ones are unused.
2130 *
2131 * No return value.
2132 */
2133static void __init _setup_postsetup(struct omap_hwmod *oh)
2134{
2135 u8 postsetup_state;
2136
2137 if (oh->rst_lines_cnt > 0)
2138 return;
Benoit Cousson76e55892010-09-21 10:34:11 -06002139
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002140 postsetup_state = oh->_postsetup_state;
2141 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2142 postsetup_state = _HWMOD_STATE_ENABLED;
2143
2144 /*
2145 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2146 * it should be set by the core code as a runtime flag during startup
2147 */
2148 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002149 (postsetup_state == _HWMOD_STATE_IDLE)) {
2150 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002151 postsetup_state = _HWMOD_STATE_ENABLED;
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002152 }
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002153
2154 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002155 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002156 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2157 _shutdown(oh);
2158 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2159 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2160 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03002161
Paul Walmsley64813c32012-04-18 19:10:03 -06002162 return;
2163}
2164
2165/**
2166 * _setup - prepare IP block hardware for use
2167 * @oh: struct omap_hwmod *
2168 * @n: (unused, pass NULL)
2169 *
2170 * Configure the IP block represented by @oh. This may include
2171 * enabling the IP block, resetting it, and placing it into a
2172 * post-setup state, depending on the type of IP block and applicable
2173 * flags. IP blocks are reset to prevent any previous configuration
2174 * by the bootloader or previous operating system from interfering
2175 * with power management or other parts of the system. The reset can
2176 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2177 * two phases for hwmod initialization. Code called here generally
2178 * affects the IP block hardware, or system integration hardware
2179 * associated with the IP block. Returns 0.
2180 */
2181static int __init _setup(struct omap_hwmod *oh, void *data)
2182{
2183 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2184 return 0;
2185
2186 _setup_iclk_autoidle(oh);
2187
2188 if (!_setup_reset(oh))
2189 _setup_postsetup(oh);
2190
Paul Walmsley63c85232009-09-03 20:14:03 +03002191 return 0;
2192}
2193
Benoit Cousson0102b622010-12-21 21:31:27 -07002194/**
2195 * _register - register a struct omap_hwmod
2196 * @oh: struct omap_hwmod *
2197 *
2198 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2199 * already has been registered by the same name; -EINVAL if the
2200 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2201 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2202 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2203 * success.
2204 *
2205 * XXX The data should be copied into bootmem, so the original data
2206 * should be marked __initdata and freed after init. This would allow
2207 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2208 * that the copy process would be relatively complex due to the large number
2209 * of substructures.
2210 */
Benoit Cousson01592df2010-12-21 21:31:28 -07002211static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07002212{
Benoit Cousson0102b622010-12-21 21:31:27 -07002213 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2214 (oh->_state != _HWMOD_STATE_UNKNOWN))
2215 return -EINVAL;
2216
Benoit Cousson0102b622010-12-21 21:31:27 -07002217 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2218
Benoit Coussonce35b242010-12-21 21:31:28 -07002219 if (_lookup(oh->name))
2220 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07002221
Paul Walmsley24dbc212012-04-19 04:04:29 -06002222 _save_mpu_port_index(oh);
Benoit Cousson0102b622010-12-21 21:31:27 -07002223
2224 list_add_tail(&oh->node, &omap_hwmod_list);
2225
2226 spin_lock_init(&oh->_lock);
2227
2228 oh->_state = _HWMOD_STATE_REGISTERED;
2229
Paul Walmsley569edd702011-02-23 00:14:06 -07002230 /*
2231 * XXX Rather than doing a strcmp(), this should test a flag
2232 * set in the hwmod data, inserted by the autogenerator code.
2233 */
2234 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2235 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07002236
Paul Walmsley569edd702011-02-23 00:14:06 -07002237 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07002238}
Paul Walmsley63c85232009-09-03 20:14:03 +03002239
2240
2241/* Public functions */
2242
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002243u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002244{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002245 if (oh->flags & HWMOD_16BIT_REG)
2246 return __raw_readw(oh->_mpu_rt_va + reg_offs);
2247 else
2248 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002249}
2250
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002251void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002252{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002253 if (oh->flags & HWMOD_16BIT_REG)
2254 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
2255 else
2256 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002257}
2258
Paul Walmsley887adea2010-07-26 16:34:33 -06002259/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002260 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
2261 * @oh: struct omap_hwmod *
2262 *
2263 * This is a public function exposed to drivers. Some drivers may need to do
2264 * some settings before and after resetting the device. Those drivers after
2265 * doing the necessary settings could use this function to start a reset by
2266 * setting the SYSCONFIG.SOFTRESET bit.
2267 */
2268int omap_hwmod_softreset(struct omap_hwmod *oh)
2269{
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002270 u32 v;
2271 int ret;
2272
2273 if (!oh || !(oh->_sysc_cache))
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002274 return -EINVAL;
2275
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002276 v = oh->_sysc_cache;
2277 ret = _set_softreset(oh, &v);
2278 if (ret)
2279 goto error;
2280 _write_sysconfig(v, oh);
2281
2282error:
2283 return ret;
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002284}
2285
2286/**
Paul Walmsley887adea2010-07-26 16:34:33 -06002287 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
2288 * @oh: struct omap_hwmod *
2289 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
2290 *
2291 * Sets the IP block's OCP slave idlemode in hardware, and updates our
2292 * local copy. Intended to be used by drivers that have some erratum
2293 * that requires direct manipulation of the SIDLEMODE bits. Returns
2294 * -EINVAL if @oh is null, or passes along the return value from
2295 * _set_slave_idlemode().
2296 *
2297 * XXX Does this function have any current users? If not, we should
2298 * remove it; it is better to let the rest of the hwmod code handle this.
2299 * Any users of this function should be scrutinized carefully.
2300 */
Kevin Hilman46273e62010-01-26 20:13:03 -07002301int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
2302{
2303 u32 v;
2304 int retval = 0;
2305
2306 if (!oh)
2307 return -EINVAL;
2308
2309 v = oh->_sysc_cache;
2310
2311 retval = _set_slave_idlemode(oh, idlemode, &v);
2312 if (!retval)
2313 _write_sysconfig(v, oh);
2314
2315 return retval;
2316}
2317
Paul Walmsley63c85232009-09-03 20:14:03 +03002318/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002319 * omap_hwmod_lookup - look up a registered omap_hwmod by name
2320 * @name: name of the omap_hwmod to look up
2321 *
2322 * Given a @name of an omap_hwmod, return a pointer to the registered
2323 * struct omap_hwmod *, or NULL upon error.
2324 */
2325struct omap_hwmod *omap_hwmod_lookup(const char *name)
2326{
2327 struct omap_hwmod *oh;
2328
2329 if (!name)
2330 return NULL;
2331
Paul Walmsley63c85232009-09-03 20:14:03 +03002332 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002333
2334 return oh;
2335}
2336
2337/**
2338 * omap_hwmod_for_each - call function for each registered omap_hwmod
2339 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06002340 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03002341 *
2342 * Call @fn for each registered omap_hwmod, passing @data to each
2343 * function. @fn must return 0 for success or any other value for
2344 * failure. If @fn returns non-zero, the iteration across omap_hwmods
2345 * will stop and the non-zero return value will be passed to the
2346 * caller of omap_hwmod_for_each(). @fn is called with
2347 * omap_hwmod_for_each() held.
2348 */
Paul Walmsley97d60162010-07-26 16:34:30 -06002349int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
2350 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03002351{
2352 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05302353 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002354
2355 if (!fn)
2356 return -EINVAL;
2357
Paul Walmsley63c85232009-09-03 20:14:03 +03002358 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06002359 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03002360 if (ret)
2361 break;
2362 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002363
2364 return ret;
2365}
2366
Paul Walmsley63c85232009-09-03 20:14:03 +03002367/**
Paul Walmsley550c8092011-02-28 11:58:14 -07002368 * omap_hwmod_register - register an array of hwmods
Paul Walmsley63c85232009-09-03 20:14:03 +03002369 * @ohs: pointer to an array of omap_hwmods to register
2370 *
2371 * Intended to be called early in boot before the clock framework is
2372 * initialized. If @ohs is not null, will register all omap_hwmods
Paul Walmsley550c8092011-02-28 11:58:14 -07002373 * listed in @ohs that are valid for this chip. Returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03002374 */
Paul Walmsley550c8092011-02-28 11:58:14 -07002375int __init omap_hwmod_register(struct omap_hwmod **ohs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002376{
Paul Walmsleybac1a0f2011-02-23 00:14:06 -07002377 int r, i;
Paul Walmsley63c85232009-09-03 20:14:03 +03002378
2379 if (!ohs)
2380 return 0;
2381
Paul Walmsleybac1a0f2011-02-23 00:14:06 -07002382 i = 0;
2383 do {
Paul Walmsleybac1a0f2011-02-23 00:14:06 -07002384 r = _register(ohs[i]);
2385 WARN(r, "omap_hwmod: %s: _register returned %d\n", ohs[i]->name,
2386 r);
2387 } while (ohs[++i]);
Paul Walmsley63c85232009-09-03 20:14:03 +03002388
2389 return 0;
2390}
2391
Paul Walmsley381d0332012-04-19 00:58:22 -06002392/**
2393 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
2394 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002395 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002396 * If the hwmod data corresponding to the MPU subsystem IP block
2397 * hasn't been initialized and set up yet, do so now. This must be
2398 * done first since sleep dependencies may be added from other hwmods
2399 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
2400 * return value.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002401 */
Paul Walmsley381d0332012-04-19 00:58:22 -06002402static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002403{
Paul Walmsley381d0332012-04-19 00:58:22 -06002404 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
2405 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
2406 __func__, MPU_INITIATOR_NAME);
2407 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
2408 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
Paul Walmsley63c85232009-09-03 20:14:03 +03002409}
2410
2411/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002412 * omap_hwmod_setup_one - set up a single hwmod
2413 * @oh_name: const char * name of the already-registered hwmod to set up
2414 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002415 * Initialize and set up a single hwmod. Intended to be used for a
2416 * small number of early devices, such as the timer IP blocks used for
2417 * the scheduler clock. Must be called after omap2_clk_init().
2418 * Resolves the struct clk names to struct clk pointers for each
2419 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
2420 * -EINVAL upon error or 0 upon success.
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002421 */
2422int __init omap_hwmod_setup_one(const char *oh_name)
2423{
2424 struct omap_hwmod *oh;
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002425
2426 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
2427
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002428 oh = _lookup(oh_name);
2429 if (!oh) {
2430 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
2431 return -EINVAL;
2432 }
2433
Paul Walmsley381d0332012-04-19 00:58:22 -06002434 _ensure_mpu_hwmod_is_setup(oh);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002435
Paul Walmsley381d0332012-04-19 00:58:22 -06002436 _init(oh, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002437 _setup(oh, NULL);
2438
2439 return 0;
2440}
2441
2442/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002443 * omap_hwmod_setup_all - set up all registered IP blocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002444 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002445 * Initialize and set up all IP blocks registered with the hwmod code.
2446 * Must be called after omap2_clk_init(). Resolves the struct clk
2447 * names to struct clk pointers for each registered omap_hwmod. Also
2448 * calls _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002449 */
Paul Walmsley550c8092011-02-28 11:58:14 -07002450static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03002451{
Paul Walmsley381d0332012-04-19 00:58:22 -06002452 _ensure_mpu_hwmod_is_setup(NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002453
Paul Walmsley381d0332012-04-19 00:58:22 -06002454 omap_hwmod_for_each(_init, NULL);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002455 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002456
2457 return 0;
2458}
Paul Walmsley550c8092011-02-28 11:58:14 -07002459core_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03002460
2461/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002462 * omap_hwmod_enable - enable an omap_hwmod
2463 * @oh: struct omap_hwmod *
2464 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002465 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03002466 * Returns -EINVAL on error or passes along the return value from _enable().
2467 */
2468int omap_hwmod_enable(struct omap_hwmod *oh)
2469{
2470 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002471 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002472
2473 if (!oh)
2474 return -EINVAL;
2475
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002476 spin_lock_irqsave(&oh->_lock, flags);
2477 r = _enable(oh);
2478 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002479
2480 return r;
2481}
2482
2483/**
2484 * omap_hwmod_idle - idle an omap_hwmod
2485 * @oh: struct omap_hwmod *
2486 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002487 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03002488 * Returns -EINVAL on error or passes along the return value from _idle().
2489 */
2490int omap_hwmod_idle(struct omap_hwmod *oh)
2491{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002492 unsigned long flags;
2493
Paul Walmsley63c85232009-09-03 20:14:03 +03002494 if (!oh)
2495 return -EINVAL;
2496
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002497 spin_lock_irqsave(&oh->_lock, flags);
2498 _idle(oh);
2499 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002500
2501 return 0;
2502}
2503
2504/**
2505 * omap_hwmod_shutdown - shutdown an omap_hwmod
2506 * @oh: struct omap_hwmod *
2507 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002508 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03002509 * omap_device_shutdown(). Returns -EINVAL on error or passes along
2510 * the return value from _shutdown().
2511 */
2512int omap_hwmod_shutdown(struct omap_hwmod *oh)
2513{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002514 unsigned long flags;
2515
Paul Walmsley63c85232009-09-03 20:14:03 +03002516 if (!oh)
2517 return -EINVAL;
2518
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002519 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002520 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002521 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002522
2523 return 0;
2524}
2525
2526/**
2527 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
2528 * @oh: struct omap_hwmod *oh
2529 *
2530 * Intended to be called by the omap_device code.
2531 */
2532int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
2533{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002534 unsigned long flags;
2535
2536 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002537 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002538 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002539
2540 return 0;
2541}
2542
2543/**
2544 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
2545 * @oh: struct omap_hwmod *oh
2546 *
2547 * Intended to be called by the omap_device code.
2548 */
2549int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
2550{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002551 unsigned long flags;
2552
2553 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002554 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002555 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002556
2557 return 0;
2558}
2559
2560/**
2561 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
2562 * @oh: struct omap_hwmod *oh
2563 *
2564 * Intended to be called by drivers and core code when all posted
2565 * writes to a device must complete before continuing further
2566 * execution (for example, after clearing some device IRQSTATUS
2567 * register bits)
2568 *
2569 * XXX what about targets with multiple OCP threads?
2570 */
2571void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
2572{
2573 BUG_ON(!oh);
2574
Paul Walmsley43b40992010-02-22 22:09:34 -07002575 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Russell King4f8a4282012-02-07 10:59:37 +00002576 WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
2577 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002578 return;
2579 }
2580
2581 /*
2582 * Forces posted writes to complete on the OCP thread handling
2583 * register writes
2584 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002585 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002586}
2587
2588/**
2589 * omap_hwmod_reset - reset the hwmod
2590 * @oh: struct omap_hwmod *
2591 *
2592 * Under some conditions, a driver may wish to reset the entire device.
2593 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06002594 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03002595 */
2596int omap_hwmod_reset(struct omap_hwmod *oh)
2597{
2598 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002599 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002600
Liam Girdwood9b579112010-09-21 10:34:09 -06002601 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002602 return -EINVAL;
2603
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002604 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002605 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002606 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002607
2608 return r;
2609}
2610
Paul Walmsley5e8370f2012-04-18 19:10:06 -06002611/*
2612 * IP block data retrieval functions
2613 */
2614
Paul Walmsley63c85232009-09-03 20:14:03 +03002615/**
2616 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
2617 * @oh: struct omap_hwmod *
2618 * @res: pointer to the first element of an array of struct resource to fill
2619 *
2620 * Count the number of struct resource array elements necessary to
2621 * contain omap_hwmod @oh resources. Intended to be called by code
2622 * that registers omap_devices. Intended to be used to determine the
2623 * size of a dynamically-allocated struct resource array, before
2624 * calling omap_hwmod_fill_resources(). Returns the number of struct
2625 * resource array elements needed.
2626 *
2627 * XXX This code is not optimized. It could attempt to merge adjacent
2628 * resource IDs.
2629 *
2630 */
2631int omap_hwmod_count_resources(struct omap_hwmod *oh)
2632{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002633 struct omap_hwmod_ocp_if *os;
2634 int ret;
2635 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002636
Paul Walmsleybc614952011-07-09 19:14:07 -06002637 ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002638
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002639 while (i < oh->slaves_cnt) {
2640 os = _fetch_next_ocp_if(NULL, oh->slaves, &i);
2641 ret += _count_ocp_if_addr_spaces(os);
2642 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002643
2644 return ret;
2645}
2646
2647/**
2648 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
2649 * @oh: struct omap_hwmod *
2650 * @res: pointer to the first element of an array of struct resource to fill
2651 *
2652 * Fill the struct resource array @res with resource data from the
2653 * omap_hwmod @oh. Intended to be called by code that registers
2654 * omap_devices. See also omap_hwmod_count_resources(). Returns the
2655 * number of array elements filled.
2656 */
2657int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
2658{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002659 struct omap_hwmod_ocp_if *os;
2660 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03002661 int r = 0;
2662
2663 /* For each IRQ, DMA, memory area, fill in array.*/
2664
Paul Walmsley212738a2011-07-09 19:14:06 -06002665 mpu_irqs_cnt = _count_mpu_irqs(oh);
2666 for (i = 0; i < mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07002667 (res + r)->name = (oh->mpu_irqs + i)->name;
2668 (res + r)->start = (oh->mpu_irqs + i)->irq;
2669 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03002670 (res + r)->flags = IORESOURCE_IRQ;
2671 r++;
2672 }
2673
Paul Walmsleybc614952011-07-09 19:14:07 -06002674 sdma_reqs_cnt = _count_sdma_reqs(oh);
2675 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06002676 (res + r)->name = (oh->sdma_reqs + i)->name;
2677 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
2678 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03002679 (res + r)->flags = IORESOURCE_DMA;
2680 r++;
2681 }
2682
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002683 i = 0;
2684 while (i < oh->slaves_cnt) {
2685 os = _fetch_next_ocp_if(NULL, oh->slaves, &i);
Paul Walmsley78183f32011-07-09 19:14:05 -06002686 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03002687
Paul Walmsley78183f32011-07-09 19:14:05 -06002688 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08002689 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03002690 (res + r)->start = (os->addr + j)->pa_start;
2691 (res + r)->end = (os->addr + j)->pa_end;
2692 (res + r)->flags = IORESOURCE_MEM;
2693 r++;
2694 }
2695 }
2696
2697 return r;
2698}
2699
2700/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06002701 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
2702 * @oh: struct omap_hwmod * to operate on
2703 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
2704 * @name: pointer to the name of the data to fetch (optional)
2705 * @rsrc: pointer to a struct resource, allocated by the caller
2706 *
2707 * Retrieve MPU IRQ, SDMA request line, or address space start/end
2708 * data for the IP block pointed to by @oh. The data will be filled
2709 * into a struct resource record pointed to by @rsrc. The struct
2710 * resource must be allocated by the caller. When @name is non-null,
2711 * the data associated with the matching entry in the IRQ/SDMA/address
2712 * space hwmod data arrays will be returned. If @name is null, the
2713 * first array entry will be returned. Data order is not meaningful
2714 * in hwmod data, so callers are strongly encouraged to use a non-null
2715 * @name whenever possible to avoid unpredictable effects if hwmod
2716 * data is later added that causes data ordering to change. This
2717 * function is only intended for use by OMAP core code. Device
2718 * drivers should not call this function - the appropriate bus-related
2719 * data accessor functions should be used instead. Returns 0 upon
2720 * success or a negative error code upon error.
2721 */
2722int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
2723 const char *name, struct resource *rsrc)
2724{
2725 int r;
2726 unsigned int irq, dma;
2727 u32 pa_start, pa_end;
2728
2729 if (!oh || !rsrc)
2730 return -EINVAL;
2731
2732 if (type == IORESOURCE_IRQ) {
2733 r = _get_mpu_irq_by_name(oh, name, &irq);
2734 if (r)
2735 return r;
2736
2737 rsrc->start = irq;
2738 rsrc->end = irq;
2739 } else if (type == IORESOURCE_DMA) {
2740 r = _get_sdma_req_by_name(oh, name, &dma);
2741 if (r)
2742 return r;
2743
2744 rsrc->start = dma;
2745 rsrc->end = dma;
2746 } else if (type == IORESOURCE_MEM) {
2747 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
2748 if (r)
2749 return r;
2750
2751 rsrc->start = pa_start;
2752 rsrc->end = pa_end;
2753 } else {
2754 return -EINVAL;
2755 }
2756
2757 rsrc->flags = type;
2758 rsrc->name = name;
2759
2760 return 0;
2761}
2762
2763/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002764 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
2765 * @oh: struct omap_hwmod *
2766 *
2767 * Return the powerdomain pointer associated with the OMAP module
2768 * @oh's main clock. If @oh does not have a main clk, return the
2769 * powerdomain associated with the interface clock associated with the
2770 * module's MPU port. (XXX Perhaps this should use the SDMA port
2771 * instead?) Returns NULL on error, or a struct powerdomain * on
2772 * success.
2773 */
2774struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
2775{
2776 struct clk *c;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06002777 struct omap_hwmod_ocp_if *oi;
Paul Walmsley63c85232009-09-03 20:14:03 +03002778
2779 if (!oh)
2780 return NULL;
2781
2782 if (oh->_clk) {
2783 c = oh->_clk;
2784 } else {
Paul Walmsley2d6141b2012-04-19 04:04:27 -06002785 oi = _find_mpu_rt_port(oh);
2786 if (!oi)
Paul Walmsley63c85232009-09-03 20:14:03 +03002787 return NULL;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06002788 c = oi->_clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03002789 }
2790
Thara Gopinathd5647c12010-03-31 04:16:29 -06002791 if (!c->clkdm)
2792 return NULL;
2793
Paul Walmsley63c85232009-09-03 20:14:03 +03002794 return c->clkdm->pwrdm.ptr;
2795
2796}
2797
2798/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06002799 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
2800 * @oh: struct omap_hwmod *
2801 *
2802 * Returns the virtual address corresponding to the beginning of the
2803 * module's register target, in the address range that is intended to
2804 * be used by the MPU. Returns the virtual address upon success or NULL
2805 * upon error.
2806 */
2807void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
2808{
2809 if (!oh)
2810 return NULL;
2811
2812 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2813 return NULL;
2814
2815 if (oh->_state == _HWMOD_STATE_UNKNOWN)
2816 return NULL;
2817
2818 return oh->_mpu_rt_va;
2819}
2820
2821/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002822 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
2823 * @oh: struct omap_hwmod *
2824 * @init_oh: struct omap_hwmod * (initiator)
2825 *
2826 * Add a sleep dependency between the initiator @init_oh and @oh.
2827 * Intended to be called by DSP/Bridge code via platform_data for the
2828 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
2829 * code needs to add/del initiator dependencies dynamically
2830 * before/after accessing a device. Returns the return value from
2831 * _add_initiator_dep().
2832 *
2833 * XXX Keep a usecount in the clockdomain code
2834 */
2835int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
2836 struct omap_hwmod *init_oh)
2837{
2838 return _add_initiator_dep(oh, init_oh);
2839}
2840
2841/*
2842 * XXX what about functions for drivers to save/restore ocp_sysconfig
2843 * for context save/restore operations?
2844 */
2845
2846/**
2847 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
2848 * @oh: struct omap_hwmod *
2849 * @init_oh: struct omap_hwmod * (initiator)
2850 *
2851 * Remove a sleep dependency between the initiator @init_oh and @oh.
2852 * Intended to be called by DSP/Bridge code via platform_data for the
2853 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
2854 * code needs to add/del initiator dependencies dynamically
2855 * before/after accessing a device. Returns the return value from
2856 * _del_initiator_dep().
2857 *
2858 * XXX Keep a usecount in the clockdomain code
2859 */
2860int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
2861 struct omap_hwmod *init_oh)
2862{
2863 return _del_initiator_dep(oh, init_oh);
2864}
2865
2866/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002867 * omap_hwmod_enable_wakeup - allow device to wake up the system
2868 * @oh: struct omap_hwmod *
2869 *
2870 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
Govindraj.R2a1cc142012-04-05 02:59:32 -06002871 * send wakeups to the PRCM, and enable I/O ring wakeup events for
2872 * this IP block if it has dynamic mux entries. Eventually this
2873 * should set PRCM wakeup registers to cause the PRCM to receive
2874 * wakeup events from the module. Does not set any wakeup routing
2875 * registers beyond this point - if the module is to wake up any other
2876 * module or subsystem, that must be set separately. Called by
2877 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002878 */
2879int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
2880{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002881 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002882 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002883
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002884 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06002885
2886 if (oh->class->sysc &&
2887 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
2888 v = oh->_sysc_cache;
2889 _enable_wakeup(oh, &v);
2890 _write_sysconfig(v, oh);
2891 }
2892
Govindraj Receec002011-12-16 14:36:58 -07002893 _set_idle_ioring_wakeup(oh, true);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002894 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002895
2896 return 0;
2897}
2898
2899/**
2900 * omap_hwmod_disable_wakeup - prevent device from waking the system
2901 * @oh: struct omap_hwmod *
2902 *
2903 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
Govindraj.R2a1cc142012-04-05 02:59:32 -06002904 * from sending wakeups to the PRCM, and disable I/O ring wakeup
2905 * events for this IP block if it has dynamic mux entries. Eventually
2906 * this should clear PRCM wakeup registers to cause the PRCM to ignore
2907 * wakeup events from the module. Does not set any wakeup routing
2908 * registers beyond this point - if the module is to wake up any other
2909 * module or subsystem, that must be set separately. Called by
2910 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002911 */
2912int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
2913{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002914 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002915 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002916
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002917 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06002918
2919 if (oh->class->sysc &&
2920 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
2921 v = oh->_sysc_cache;
2922 _disable_wakeup(oh, &v);
2923 _write_sysconfig(v, oh);
2924 }
2925
Govindraj Receec002011-12-16 14:36:58 -07002926 _set_idle_ioring_wakeup(oh, false);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002927 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002928
2929 return 0;
2930}
Paul Walmsley43b40992010-02-22 22:09:34 -07002931
2932/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002933 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
2934 * contained in the hwmod module.
2935 * @oh: struct omap_hwmod *
2936 * @name: name of the reset line to lookup and assert
2937 *
2938 * Some IP like dsp, ipu or iva contain processor that require
2939 * an HW reset line to be assert / deassert in order to enable fully
2940 * the IP. Returns -EINVAL if @oh is null or if the operation is not
2941 * yet supported on this OMAP; otherwise, passes along the return value
2942 * from _assert_hardreset().
2943 */
2944int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
2945{
2946 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002947 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002948
2949 if (!oh)
2950 return -EINVAL;
2951
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002952 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002953 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002954 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002955
2956 return ret;
2957}
2958
2959/**
2960 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
2961 * contained in the hwmod module.
2962 * @oh: struct omap_hwmod *
2963 * @name: name of the reset line to look up and deassert
2964 *
2965 * Some IP like dsp, ipu or iva contain processor that require
2966 * an HW reset line to be assert / deassert in order to enable fully
2967 * the IP. Returns -EINVAL if @oh is null or if the operation is not
2968 * yet supported on this OMAP; otherwise, passes along the return value
2969 * from _deassert_hardreset().
2970 */
2971int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
2972{
2973 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002974 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002975
2976 if (!oh)
2977 return -EINVAL;
2978
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002979 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002980 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002981 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002982
2983 return ret;
2984}
2985
2986/**
2987 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
2988 * contained in the hwmod module
2989 * @oh: struct omap_hwmod *
2990 * @name: name of the reset line to look up and read
2991 *
2992 * Return the current state of the hwmod @oh's reset line named @name:
2993 * returns -EINVAL upon parameter error or if this operation
2994 * is unsupported on the current OMAP; otherwise, passes along the return
2995 * value from _read_hardreset().
2996 */
2997int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
2998{
2999 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003000 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003001
3002 if (!oh)
3003 return -EINVAL;
3004
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003005 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003006 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003007 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003008
3009 return ret;
3010}
3011
3012
3013/**
Paul Walmsley43b40992010-02-22 22:09:34 -07003014 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3015 * @classname: struct omap_hwmod_class name to search for
3016 * @fn: callback function pointer to call for each hwmod in class @classname
3017 * @user: arbitrary context data to pass to the callback function
3018 *
Benoit Coussonce35b242010-12-21 21:31:28 -07003019 * For each omap_hwmod of class @classname, call @fn.
3020 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07003021 * zero, the iterator is terminated, and the callback function's return
3022 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3023 * if @classname or @fn are NULL, or passes back the error code from @fn.
3024 */
3025int omap_hwmod_for_each_by_class(const char *classname,
3026 int (*fn)(struct omap_hwmod *oh,
3027 void *user),
3028 void *user)
3029{
3030 struct omap_hwmod *temp_oh;
3031 int ret = 0;
3032
3033 if (!classname || !fn)
3034 return -EINVAL;
3035
3036 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3037 __func__, classname);
3038
Paul Walmsley43b40992010-02-22 22:09:34 -07003039 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3040 if (!strcmp(temp_oh->class->name, classname)) {
3041 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3042 __func__, temp_oh->name);
3043 ret = (*fn)(temp_oh, user);
3044 if (ret)
3045 break;
3046 }
3047 }
3048
Paul Walmsley43b40992010-02-22 22:09:34 -07003049 if (ret)
3050 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3051 __func__, ret);
3052
3053 return ret;
3054}
3055
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003056/**
3057 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3058 * @oh: struct omap_hwmod *
3059 * @state: state that _setup() should leave the hwmod in
3060 *
Paul Walmsley550c8092011-02-28 11:58:14 -07003061 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsley64813c32012-04-18 19:10:03 -06003062 * (called by omap_hwmod_setup_*()). See also the documentation
3063 * for _setup_postsetup(), above. Returns 0 upon success or
3064 * -EINVAL if there is a problem with the arguments or if the hwmod is
3065 * in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003066 */
3067int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3068{
3069 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003070 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003071
3072 if (!oh)
3073 return -EINVAL;
3074
3075 if (state != _HWMOD_STATE_DISABLED &&
3076 state != _HWMOD_STATE_ENABLED &&
3077 state != _HWMOD_STATE_IDLE)
3078 return -EINVAL;
3079
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003080 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003081
3082 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3083 ret = -EINVAL;
3084 goto ohsps_unlock;
3085 }
3086
3087 oh->_postsetup_state = state;
3088 ret = 0;
3089
3090ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003091 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003092
3093 return ret;
3094}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003095
3096/**
3097 * omap_hwmod_get_context_loss_count - get lost context count
3098 * @oh: struct omap_hwmod *
3099 *
3100 * Query the powerdomain of of @oh to get the context loss
3101 * count for this device.
3102 *
3103 * Returns the context loss count of the powerdomain assocated with @oh
3104 * upon success, or zero if no powerdomain exists for @oh.
3105 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03003106int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003107{
3108 struct powerdomain *pwrdm;
3109 int ret = 0;
3110
3111 pwrdm = omap_hwmod_get_pwrdm(oh);
3112 if (pwrdm)
3113 ret = pwrdm_get_context_loss_count(pwrdm);
3114
3115 return ret;
3116}
Paul Walmsley43b01642011-03-10 03:50:07 -07003117
3118/**
3119 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
3120 * @oh: struct omap_hwmod *
3121 *
3122 * Prevent the hwmod @oh from being reset during the setup process.
3123 * Intended for use by board-*.c files on boards with devices that
3124 * cannot tolerate being reset. Must be called before the hwmod has
3125 * been set up. Returns 0 upon success or negative error code upon
3126 * failure.
3127 */
3128int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
3129{
3130 if (!oh)
3131 return -EINVAL;
3132
3133 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3134 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
3135 oh->name);
3136 return -EINVAL;
3137 }
3138
3139 oh->flags |= HWMOD_INIT_NO_RESET;
3140
3141 return 0;
3142}
Tero Kristoabc2d542011-12-16 14:36:59 -07003143
3144/**
3145 * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
3146 * @oh: struct omap_hwmod * containing hwmod mux entries
3147 * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
3148 * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
3149 *
3150 * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
3151 * entry number @pad_idx for the hwmod @oh, trigger the interrupt
3152 * service routine for the hwmod's mpu_irqs array index @irq_idx. If
3153 * this function is not called for a given pad_idx, then the ISR
3154 * associated with @oh's first MPU IRQ will be triggered when an I/O
3155 * pad wakeup occurs on that pad. Note that @pad_idx is the index of
3156 * the _dynamic or wakeup_ entry: if there are other entries not
3157 * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
3158 * entries are NOT COUNTED in the dynamic pad index. This function
3159 * must be called separately for each pad that requires its interrupt
3160 * to be re-routed this way. Returns -EINVAL if there is an argument
3161 * problem or if @oh does not have hwmod mux entries or MPU IRQs;
3162 * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
3163 *
3164 * XXX This function interface is fragile. Rather than using array
3165 * indexes, which are subject to unpredictable change, it should be
3166 * using hwmod IRQ names, and some other stable key for the hwmod mux
3167 * pad records.
3168 */
3169int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
3170{
3171 int nr_irqs;
3172
3173 might_sleep();
3174
3175 if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
3176 pad_idx >= oh->mux->nr_pads_dynamic)
3177 return -EINVAL;
3178
3179 /* Check the number of available mpu_irqs */
3180 for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
3181 ;
3182
3183 if (irq_idx >= nr_irqs)
3184 return -EINVAL;
3185
3186 if (!oh->mux->irqs) {
3187 /* XXX What frees this? */
3188 oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
3189 GFP_KERNEL);
3190 if (!oh->mux->irqs)
3191 return -ENOMEM;
3192 }
3193 oh->mux->irqs[pad_idx] = irq_idx;
3194
3195 return 0;
3196}