blob: bd9fc10ea73702d49c03fa1fe4e0e96f0ce054a9 [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/**
172 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
173 * @oh: struct omap_hwmod *
174 *
175 * Load the current value of the hwmod OCP_SYSCONFIG register into the
176 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
177 * OCP_SYSCONFIG register or 0 upon success.
178 */
179static int _update_sysc_cache(struct omap_hwmod *oh)
180{
Paul Walmsley43b40992010-02-22 22:09:34 -0700181 if (!oh->class->sysc) {
182 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 +0300183 return -EINVAL;
184 }
185
186 /* XXX ensure module interface clock is up */
187
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -0700188 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300189
Paul Walmsley43b40992010-02-22 22:09:34 -0700190 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
Thara Gopinath883edfd2010-01-19 17:30:51 -0700191 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300192
193 return 0;
194}
195
196/**
197 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
198 * @v: OCP_SYSCONFIG value to write
199 * @oh: struct omap_hwmod *
200 *
Paul Walmsley43b40992010-02-22 22:09:34 -0700201 * Write @v into the module class' OCP_SYSCONFIG register, if it has
202 * one. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +0300203 */
204static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
205{
Paul Walmsley43b40992010-02-22 22:09:34 -0700206 if (!oh->class->sysc) {
207 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 +0300208 return;
209 }
210
211 /* XXX ensure module interface clock is up */
212
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700213 /* Module might have lost context, always update cache and register */
214 oh->_sysc_cache = v;
215 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300216}
217
218/**
219 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
220 * @oh: struct omap_hwmod *
221 * @standbymode: MIDLEMODE field bits
222 * @v: pointer to register contents to modify
223 *
224 * Update the master standby mode bits in @v to be @standbymode for
225 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
226 * upon error or 0 upon success.
227 */
228static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
229 u32 *v)
230{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700231 u32 mstandby_mask;
232 u8 mstandby_shift;
233
Paul Walmsley43b40992010-02-22 22:09:34 -0700234 if (!oh->class->sysc ||
235 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300236 return -EINVAL;
237
Paul Walmsley43b40992010-02-22 22:09:34 -0700238 if (!oh->class->sysc->sysc_fields) {
239 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700240 return -EINVAL;
241 }
242
Paul Walmsley43b40992010-02-22 22:09:34 -0700243 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700244 mstandby_mask = (0x3 << mstandby_shift);
245
246 *v &= ~mstandby_mask;
247 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300248
249 return 0;
250}
251
252/**
253 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
254 * @oh: struct omap_hwmod *
255 * @idlemode: SIDLEMODE field bits
256 * @v: pointer to register contents to modify
257 *
258 * Update the slave idle mode bits in @v to be @idlemode for the @oh
259 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
260 * or 0 upon success.
261 */
262static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
263{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700264 u32 sidle_mask;
265 u8 sidle_shift;
266
Paul Walmsley43b40992010-02-22 22:09:34 -0700267 if (!oh->class->sysc ||
268 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300269 return -EINVAL;
270
Paul Walmsley43b40992010-02-22 22:09:34 -0700271 if (!oh->class->sysc->sysc_fields) {
272 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700273 return -EINVAL;
274 }
275
Paul Walmsley43b40992010-02-22 22:09:34 -0700276 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700277 sidle_mask = (0x3 << sidle_shift);
278
279 *v &= ~sidle_mask;
280 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300281
282 return 0;
283}
284
285/**
286 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
287 * @oh: struct omap_hwmod *
288 * @clockact: CLOCKACTIVITY field bits
289 * @v: pointer to register contents to modify
290 *
291 * Update the clockactivity mode bits in @v to be @clockact for the
292 * @oh hwmod. Used for additional powersaving on some modules. Does
293 * not write to the hardware. Returns -EINVAL upon error or 0 upon
294 * success.
295 */
296static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
297{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700298 u32 clkact_mask;
299 u8 clkact_shift;
300
Paul Walmsley43b40992010-02-22 22:09:34 -0700301 if (!oh->class->sysc ||
302 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300303 return -EINVAL;
304
Paul Walmsley43b40992010-02-22 22:09:34 -0700305 if (!oh->class->sysc->sysc_fields) {
306 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700307 return -EINVAL;
308 }
309
Paul Walmsley43b40992010-02-22 22:09:34 -0700310 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700311 clkact_mask = (0x3 << clkact_shift);
312
313 *v &= ~clkact_mask;
314 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300315
316 return 0;
317}
318
319/**
320 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
321 * @oh: struct omap_hwmod *
322 * @v: pointer to register contents to modify
323 *
324 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
325 * error or 0 upon success.
326 */
327static int _set_softreset(struct omap_hwmod *oh, u32 *v)
328{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700329 u32 softrst_mask;
330
Paul Walmsley43b40992010-02-22 22:09:34 -0700331 if (!oh->class->sysc ||
332 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300333 return -EINVAL;
334
Paul Walmsley43b40992010-02-22 22:09:34 -0700335 if (!oh->class->sysc->sysc_fields) {
336 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700337 return -EINVAL;
338 }
339
Paul Walmsley43b40992010-02-22 22:09:34 -0700340 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700341
342 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300343
344 return 0;
345}
346
347/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700348 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
349 * @oh: struct omap_hwmod *
350 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
351 * @v: pointer to register contents to modify
352 *
353 * Update the module autoidle bit in @v to be @autoidle for the @oh
354 * hwmod. The autoidle bit controls whether the module can gate
355 * internal clocks automatically when it isn't doing anything; the
356 * exact function of this bit varies on a per-module basis. This
357 * function does not write to the hardware. Returns -EINVAL upon
358 * error or 0 upon success.
359 */
360static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
361 u32 *v)
362{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700363 u32 autoidle_mask;
364 u8 autoidle_shift;
365
Paul Walmsley43b40992010-02-22 22:09:34 -0700366 if (!oh->class->sysc ||
367 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700368 return -EINVAL;
369
Paul Walmsley43b40992010-02-22 22:09:34 -0700370 if (!oh->class->sysc->sysc_fields) {
371 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700372 return -EINVAL;
373 }
374
Paul Walmsley43b40992010-02-22 22:09:34 -0700375 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700376 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700377
378 *v &= ~autoidle_mask;
379 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700380
381 return 0;
382}
383
384/**
Govindraj Receec002011-12-16 14:36:58 -0700385 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
386 * @oh: struct omap_hwmod *
387 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
388 *
389 * Set or clear the I/O pad wakeup flag in the mux entries for the
390 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
391 * in memory. If the hwmod is currently idled, and the new idle
392 * values don't match the previous ones, this function will also
393 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
394 * currently idled, this function won't touch the hardware: the new
395 * mux settings are written to the SCM PADCTRL registers when the
396 * hwmod is idled. No return value.
397 */
398static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
399{
400 struct omap_device_pad *pad;
401 bool change = false;
402 u16 prev_idle;
403 int j;
404
405 if (!oh->mux || !oh->mux->enabled)
406 return;
407
408 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
409 pad = oh->mux->pads_dynamic[j];
410
411 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
412 continue;
413
414 prev_idle = pad->idle;
415
416 if (set_wake)
417 pad->idle |= OMAP_WAKEUP_EN;
418 else
419 pad->idle &= ~OMAP_WAKEUP_EN;
420
421 if (prev_idle != pad->idle)
422 change = true;
423 }
424
425 if (change && oh->_state == _HWMOD_STATE_IDLE)
426 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
427}
428
429/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300430 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
431 * @oh: struct omap_hwmod *
432 *
433 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
434 * upon error or 0 upon success.
435 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700436static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300437{
Paul Walmsley43b40992010-02-22 22:09:34 -0700438 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700439 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200440 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
441 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300442 return -EINVAL;
443
Paul Walmsley43b40992010-02-22 22:09:34 -0700444 if (!oh->class->sysc->sysc_fields) {
445 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700446 return -EINVAL;
447 }
448
Benoit Cousson1fe74112011-07-01 22:54:03 +0200449 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
450 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300451
Benoit Cousson86009eb2010-12-21 21:31:28 -0700452 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
453 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200454 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
455 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700456
Paul Walmsley63c85232009-09-03 20:14:03 +0300457 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
458
459 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
460
461 return 0;
462}
463
464/**
465 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
466 * @oh: struct omap_hwmod *
467 *
468 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
469 * upon error or 0 upon success.
470 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700471static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300472{
Paul Walmsley43b40992010-02-22 22:09:34 -0700473 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700474 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200475 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
476 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300477 return -EINVAL;
478
Paul Walmsley43b40992010-02-22 22:09:34 -0700479 if (!oh->class->sysc->sysc_fields) {
480 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700481 return -EINVAL;
482 }
483
Benoit Cousson1fe74112011-07-01 22:54:03 +0200484 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
485 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300486
Benoit Cousson86009eb2010-12-21 21:31:28 -0700487 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
488 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200489 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
490 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700491
Paul Walmsley63c85232009-09-03 20:14:03 +0300492 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
493
494 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
495
496 return 0;
497}
498
499/**
500 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
501 * @oh: struct omap_hwmod *
502 *
503 * Prevent the hardware module @oh from entering idle while the
504 * hardare module initiator @init_oh is active. Useful when a module
505 * will be accessed by a particular initiator (e.g., if a module will
506 * be accessed by the IVA, there should be a sleepdep between the IVA
507 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700508 * mode. If the clockdomain is marked as not needing autodeps, return
509 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
510 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300511 */
512static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
513{
514 if (!oh->_clk)
515 return -EINVAL;
516
Paul Walmsley570b54c2011-03-10 03:50:09 -0700517 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
518 return 0;
519
Paul Walmsley55ed9692010-01-26 20:12:59 -0700520 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300521}
522
523/**
524 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
525 * @oh: struct omap_hwmod *
526 *
527 * Allow the hardware module @oh to enter idle while the hardare
528 * module initiator @init_oh is active. Useful when a module will not
529 * be accessed by a particular initiator (e.g., if a module will not
530 * be accessed by the IVA, there should be no sleepdep between the IVA
531 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700532 * mode. If the clockdomain is marked as not needing autodeps, return
533 * 0 without doing anything. Returns -EINVAL upon error or passes
534 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300535 */
536static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
537{
538 if (!oh->_clk)
539 return -EINVAL;
540
Paul Walmsley570b54c2011-03-10 03:50:09 -0700541 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
542 return 0;
543
Paul Walmsley55ed9692010-01-26 20:12:59 -0700544 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300545}
546
547/**
548 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
549 * @oh: struct omap_hwmod *
550 *
551 * Called from _init_clocks(). Populates the @oh _clk (main
552 * functional clock pointer) if a main_clk is present. Returns 0 on
553 * success or -EINVAL on error.
554 */
555static int _init_main_clk(struct omap_hwmod *oh)
556{
Paul Walmsley63c85232009-09-03 20:14:03 +0300557 int ret = 0;
558
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700559 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300560 return 0;
561
Benoit Cousson63403382010-05-20 12:31:10 -0600562 oh->_clk = omap_clk_get_by_name(oh->main_clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600563 if (!oh->_clk) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600564 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
565 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600566 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600567 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300568
Benoit Cousson63403382010-05-20 12:31:10 -0600569 if (!oh->_clk->clkdm)
570 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
571 oh->main_clk, oh->_clk->name);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700572
Paul Walmsley63c85232009-09-03 20:14:03 +0300573 return ret;
574}
575
576/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600577 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300578 * @oh: struct omap_hwmod *
579 *
580 * Called from _init_clocks(). Populates the @oh OCP slave interface
581 * clock pointers. Returns 0 on success or -EINVAL on error.
582 */
583static int _init_interface_clks(struct omap_hwmod *oh)
584{
Paul Walmsley63c85232009-09-03 20:14:03 +0300585 struct clk *c;
586 int i;
587 int ret = 0;
588
589 if (oh->slaves_cnt == 0)
590 return 0;
591
Benoit Cousson682fdc92010-05-20 12:31:09 -0600592 for (i = 0; i < oh->slaves_cnt; i++) {
593 struct omap_hwmod_ocp_if *os = oh->slaves[i];
594
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700595 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300596 continue;
597
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700598 c = omap_clk_get_by_name(os->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600599 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600600 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
601 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300602 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600603 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300604 os->_clk = c;
605 }
606
607 return ret;
608}
609
610/**
611 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
612 * @oh: struct omap_hwmod *
613 *
614 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
615 * clock pointers. Returns 0 on success or -EINVAL on error.
616 */
617static int _init_opt_clks(struct omap_hwmod *oh)
618{
619 struct omap_hwmod_opt_clk *oc;
620 struct clk *c;
621 int i;
622 int ret = 0;
623
624 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700625 c = omap_clk_get_by_name(oc->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600626 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600627 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
628 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300629 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600630 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300631 oc->_clk = c;
632 }
633
634 return ret;
635}
636
637/**
638 * _enable_clocks - enable hwmod main clock and interface clocks
639 * @oh: struct omap_hwmod *
640 *
641 * Enables all clocks necessary for register reads and writes to succeed
642 * on the hwmod @oh. Returns 0.
643 */
644static int _enable_clocks(struct omap_hwmod *oh)
645{
Paul Walmsley63c85232009-09-03 20:14:03 +0300646 int i;
647
648 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
649
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600650 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300651 clk_enable(oh->_clk);
652
653 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -0600654 for (i = 0; i < oh->slaves_cnt; i++) {
655 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +0300656 struct clk *c = os->_clk;
657
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600658 if (c && (os->flags & OCPIF_SWSUP_IDLE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300659 clk_enable(c);
660 }
661 }
662
663 /* The opt clocks are controlled by the device driver. */
664
665 return 0;
666}
667
668/**
669 * _disable_clocks - disable hwmod main clock and interface clocks
670 * @oh: struct omap_hwmod *
671 *
672 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
673 */
674static int _disable_clocks(struct omap_hwmod *oh)
675{
Paul Walmsley63c85232009-09-03 20:14:03 +0300676 int i;
677
678 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
679
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600680 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300681 clk_disable(oh->_clk);
682
683 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -0600684 for (i = 0; i < oh->slaves_cnt; i++) {
685 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +0300686 struct clk *c = os->_clk;
687
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600688 if (c && (os->flags & OCPIF_SWSUP_IDLE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300689 clk_disable(c);
690 }
691 }
692
693 /* The opt clocks are controlled by the device driver. */
694
695 return 0;
696}
697
Benoit Cousson96835af2010-09-21 18:57:58 +0200698static void _enable_optional_clocks(struct omap_hwmod *oh)
699{
700 struct omap_hwmod_opt_clk *oc;
701 int i;
702
703 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
704
705 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
706 if (oc->_clk) {
707 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
708 oc->_clk->name);
709 clk_enable(oc->_clk);
710 }
711}
712
713static void _disable_optional_clocks(struct omap_hwmod *oh)
714{
715 struct omap_hwmod_opt_clk *oc;
716 int i;
717
718 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
719
720 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
721 if (oc->_clk) {
722 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
723 oc->_clk->name);
724 clk_disable(oc->_clk);
725 }
726}
727
Paul Walmsley63c85232009-09-03 20:14:03 +0300728/**
Benoit Cousson45c38252011-07-10 05:56:33 -0600729 * _enable_module - enable CLKCTRL modulemode on OMAP4
730 * @oh: struct omap_hwmod *
731 *
732 * Enables the PRCM module mode related to the hwmod @oh.
733 * No return value.
734 */
735static void _enable_module(struct omap_hwmod *oh)
736{
737 /* The module mode does not exist prior OMAP4 */
738 if (cpu_is_omap24xx() || cpu_is_omap34xx())
739 return;
740
741 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
742 return;
743
744 pr_debug("omap_hwmod: %s: _enable_module: %d\n",
745 oh->name, oh->prcm.omap4.modulemode);
746
747 omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
748 oh->clkdm->prcm_partition,
749 oh->clkdm->cm_inst,
750 oh->clkdm->clkdm_offs,
751 oh->prcm.omap4.clkctrl_offs);
752}
753
754/**
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800755 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
756 * @oh: struct omap_hwmod *
757 *
758 * Wait for a module @oh to enter slave idle. Returns 0 if the module
759 * does not have an IDLEST bit or if the module successfully enters
760 * slave idle; otherwise, pass along the return value of the
761 * appropriate *_cm*_wait_module_idle() function.
762 */
763static int _omap4_wait_target_disable(struct omap_hwmod *oh)
764{
765 if (!cpu_is_omap44xx())
766 return 0;
767
768 if (!oh)
769 return -EINVAL;
770
771 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
772 return 0;
773
774 if (oh->flags & HWMOD_NO_IDLEST)
775 return 0;
776
777 return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
778 oh->clkdm->cm_inst,
779 oh->clkdm->clkdm_offs,
780 oh->prcm.omap4.clkctrl_offs);
781}
782
783/**
Paul Walmsley212738a2011-07-09 19:14:06 -0600784 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
785 * @oh: struct omap_hwmod *oh
786 *
787 * Count and return the number of MPU IRQs associated with the hwmod
788 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
789 * NULL.
790 */
791static int _count_mpu_irqs(struct omap_hwmod *oh)
792{
793 struct omap_hwmod_irq_info *ohii;
794 int i = 0;
795
796 if (!oh || !oh->mpu_irqs)
797 return 0;
798
799 do {
800 ohii = &oh->mpu_irqs[i++];
801 } while (ohii->irq != -1);
802
sricharancc1b07652011-11-23 14:35:07 -0800803 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -0600804}
805
806/**
Paul Walmsleybc614952011-07-09 19:14:07 -0600807 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
808 * @oh: struct omap_hwmod *oh
809 *
810 * Count and return the number of SDMA request lines associated with
811 * the hwmod @oh. Used to allocate struct resource data. Returns 0
812 * if @oh is NULL.
813 */
814static int _count_sdma_reqs(struct omap_hwmod *oh)
815{
816 struct omap_hwmod_dma_info *ohdi;
817 int i = 0;
818
819 if (!oh || !oh->sdma_reqs)
820 return 0;
821
822 do {
823 ohdi = &oh->sdma_reqs[i++];
824 } while (ohdi->dma_req != -1);
825
sricharancc1b07652011-11-23 14:35:07 -0800826 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -0600827}
828
829/**
Paul Walmsley78183f32011-07-09 19:14:05 -0600830 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
831 * @oh: struct omap_hwmod *oh
832 *
833 * Count and return the number of address space ranges associated with
834 * the hwmod @oh. Used to allocate struct resource data. Returns 0
835 * if @oh is NULL.
836 */
837static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
838{
839 struct omap_hwmod_addr_space *mem;
840 int i = 0;
841
842 if (!os || !os->addr)
843 return 0;
844
845 do {
846 mem = &os->addr[i++];
847 } while (mem->pa_start != mem->pa_end);
848
sricharancc1b07652011-11-23 14:35:07 -0800849 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -0600850}
851
852/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300853 * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
854 * @oh: struct omap_hwmod *
855 *
856 * Returns the array index of the OCP slave port that the MPU
857 * addresses the device on, or -EINVAL upon error or not found.
858 */
Benoit Cousson01592df2010-12-21 21:31:28 -0700859static int __init _find_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300860{
Paul Walmsley63c85232009-09-03 20:14:03 +0300861 int i;
862 int found = 0;
863
864 if (!oh || oh->slaves_cnt == 0)
865 return -EINVAL;
866
Benoit Cousson682fdc92010-05-20 12:31:09 -0600867 for (i = 0; i < oh->slaves_cnt; i++) {
868 struct omap_hwmod_ocp_if *os = oh->slaves[i];
869
Paul Walmsley63c85232009-09-03 20:14:03 +0300870 if (os->user & OCP_USER_MPU) {
871 found = 1;
872 break;
873 }
874 }
875
876 if (found)
877 pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
878 oh->name, i);
879 else
880 pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
881 oh->name);
882
883 return (found) ? i : -EINVAL;
884}
885
886/**
887 * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
888 * @oh: struct omap_hwmod *
889 *
890 * Return the virtual address of the base of the register target of
891 * device @oh, or NULL on error.
892 */
Benoit Cousson01592df2010-12-21 21:31:28 -0700893static void __iomem * __init _find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
Paul Walmsley63c85232009-09-03 20:14:03 +0300894{
895 struct omap_hwmod_ocp_if *os;
896 struct omap_hwmod_addr_space *mem;
Paul Walmsley78183f32011-07-09 19:14:05 -0600897 int i = 0, found = 0;
Tony Lindgren986a13f2009-10-19 15:25:22 -0700898 void __iomem *va_start;
Paul Walmsley63c85232009-09-03 20:14:03 +0300899
900 if (!oh || oh->slaves_cnt == 0)
901 return NULL;
902
Benoit Cousson682fdc92010-05-20 12:31:09 -0600903 os = oh->slaves[index];
Paul Walmsley63c85232009-09-03 20:14:03 +0300904
Paul Walmsley78183f32011-07-09 19:14:05 -0600905 if (!os->addr)
906 return NULL;
907
908 do {
909 mem = &os->addr[i++];
910 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +0300911 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -0600912 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +0300913
Tony Lindgren986a13f2009-10-19 15:25:22 -0700914 if (found) {
915 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
916 if (!va_start) {
917 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
918 return NULL;
919 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300920 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
Tony Lindgren986a13f2009-10-19 15:25:22 -0700921 oh->name, va_start);
922 } else {
Paul Walmsley63c85232009-09-03 20:14:03 +0300923 pr_debug("omap_hwmod: %s: no MPU register target found\n",
924 oh->name);
Tony Lindgren986a13f2009-10-19 15:25:22 -0700925 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300926
Tony Lindgren986a13f2009-10-19 15:25:22 -0700927 return (found) ? va_start : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +0300928}
929
930/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600931 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +0300932 * @oh: struct omap_hwmod *
933 *
934 * If module is marked as SWSUP_SIDLE, force the module out of slave
935 * idle; otherwise, configure it for smart-idle. If module is marked
936 * as SWSUP_MSUSPEND, force the module out of master standby;
937 * otherwise, configure it for smart-standby. No return value.
938 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600939static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300940{
Paul Walmsley43b40992010-02-22 22:09:34 -0700941 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +0300942 u32 v;
943
Paul Walmsley43b40992010-02-22 22:09:34 -0700944 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +0300945 return;
946
947 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -0700948 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +0300949
Paul Walmsley43b40992010-02-22 22:09:34 -0700950 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +0300951 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
952 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
953 _set_slave_idlemode(oh, idlemode, &v);
954 }
955
Paul Walmsley43b40992010-02-22 22:09:34 -0700956 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +0200957 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
958 idlemode = HWMOD_IDLEMODE_NO;
959 } else {
960 if (sf & SYSC_HAS_ENAWAKEUP)
961 _enable_wakeup(oh, &v);
962 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
963 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
964 else
965 idlemode = HWMOD_IDLEMODE_SMART;
966 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300967 _set_master_standbymode(oh, idlemode, &v);
968 }
969
Paul Walmsleya16b1f72009-12-08 16:34:17 -0700970 /*
971 * XXX The clock framework should handle this, by
972 * calling into this code. But this must wait until the
973 * clock structures are tagged with omap_hwmod entries
974 */
Paul Walmsley43b40992010-02-22 22:09:34 -0700975 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
976 (sf & SYSC_HAS_CLOCKACTIVITY))
977 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +0300978
Rajendra Nayak9980ce52010-09-21 19:58:30 +0530979 /* If slave is in SMARTIDLE, also enable wakeup */
980 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700981 _enable_wakeup(oh, &v);
982
983 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -0600984
985 /*
986 * Set the autoidle bit only after setting the smartidle bit
987 * Setting this will not have any impact on the other modules.
988 */
989 if (sf & SYSC_HAS_AUTOIDLE) {
990 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
991 0 : 1;
992 _set_module_autoidle(oh, idlemode, &v);
993 _write_sysconfig(v, oh);
994 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300995}
996
997/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600998 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +0300999 * @oh: struct omap_hwmod *
1000 *
1001 * If module is marked as SWSUP_SIDLE, force the module into slave
1002 * idle; otherwise, configure it for smart-idle. If module is marked
1003 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1004 * configure it for smart-standby. No return value.
1005 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001006static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001007{
Paul Walmsley43b40992010-02-22 22:09:34 -07001008 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001009 u32 v;
1010
Paul Walmsley43b40992010-02-22 22:09:34 -07001011 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001012 return;
1013
1014 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001015 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001016
Paul Walmsley43b40992010-02-22 22:09:34 -07001017 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001018 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1019 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
1020 _set_slave_idlemode(oh, idlemode, &v);
1021 }
1022
Paul Walmsley43b40992010-02-22 22:09:34 -07001023 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001024 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1025 idlemode = HWMOD_IDLEMODE_FORCE;
1026 } else {
1027 if (sf & SYSC_HAS_ENAWAKEUP)
1028 _enable_wakeup(oh, &v);
1029 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1030 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1031 else
1032 idlemode = HWMOD_IDLEMODE_SMART;
1033 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001034 _set_master_standbymode(oh, idlemode, &v);
1035 }
1036
Benoit Cousson86009eb2010-12-21 21:31:28 -07001037 /* If slave is in SMARTIDLE, also enable wakeup */
1038 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1039 _enable_wakeup(oh, &v);
1040
Paul Walmsley63c85232009-09-03 20:14:03 +03001041 _write_sysconfig(v, oh);
1042}
1043
1044/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001045 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001046 * @oh: struct omap_hwmod *
1047 *
1048 * Force the module into slave idle and master suspend. No return
1049 * value.
1050 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001051static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001052{
1053 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001054 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001055
Paul Walmsley43b40992010-02-22 22:09:34 -07001056 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001057 return;
1058
1059 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001060 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001061
Paul Walmsley43b40992010-02-22 22:09:34 -07001062 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001063 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1064
Paul Walmsley43b40992010-02-22 22:09:34 -07001065 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001066 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1067
Paul Walmsley43b40992010-02-22 22:09:34 -07001068 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001069 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001070
1071 _write_sysconfig(v, oh);
1072}
1073
1074/**
1075 * _lookup - find an omap_hwmod by name
1076 * @name: find an omap_hwmod by name
1077 *
1078 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001079 */
1080static struct omap_hwmod *_lookup(const char *name)
1081{
1082 struct omap_hwmod *oh, *temp_oh;
1083
1084 oh = NULL;
1085
1086 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1087 if (!strcmp(name, temp_oh->name)) {
1088 oh = temp_oh;
1089 break;
1090 }
1091 }
1092
1093 return oh;
1094}
Benoit Cousson6ae76992011-07-10 05:56:30 -06001095/**
1096 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1097 * @oh: struct omap_hwmod *
1098 *
1099 * Convert a clockdomain name stored in a struct omap_hwmod into a
1100 * clockdomain pointer, and save it into the struct omap_hwmod.
1101 * return -EINVAL if clkdm_name does not exist or if the lookup failed.
1102 */
1103static int _init_clkdm(struct omap_hwmod *oh)
1104{
1105 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1106 return 0;
1107
1108 if (!oh->clkdm_name) {
1109 pr_warning("omap_hwmod: %s: no clkdm_name\n", oh->name);
1110 return -EINVAL;
1111 }
1112
1113 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1114 if (!oh->clkdm) {
1115 pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
1116 oh->name, oh->clkdm_name);
1117 return -EINVAL;
1118 }
1119
1120 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1121 oh->name, oh->clkdm_name);
1122
1123 return 0;
1124}
Paul Walmsley63c85232009-09-03 20:14:03 +03001125
1126/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001127 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1128 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001129 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -06001130 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001131 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001132 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001133 * Resolves all clock names embedded in the hwmod. Returns 0 on
1134 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001135 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001136static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001137{
1138 int ret = 0;
1139
Paul Walmsley48d54f32011-02-23 00:14:07 -07001140 if (oh->_state != _HWMOD_STATE_REGISTERED)
1141 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001142
1143 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1144
1145 ret |= _init_main_clk(oh);
1146 ret |= _init_interface_clks(oh);
1147 ret |= _init_opt_clks(oh);
Benoit Cousson6ae76992011-07-10 05:56:30 -06001148 ret |= _init_clkdm(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001149
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001150 if (!ret)
1151 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001152 else
1153 pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001154
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001155 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001156}
1157
1158/**
1159 * _wait_target_ready - wait for a module to leave slave idle
1160 * @oh: struct omap_hwmod *
1161 *
1162 * Wait for a module @oh to leave slave idle. Returns 0 if the module
1163 * does not have an IDLEST bit or if the module successfully leaves
1164 * slave idle; otherwise, pass along the return value of the
Benoit Coussond0f06312011-07-10 05:56:30 -06001165 * appropriate *_cm*_wait_module_ready() function.
Paul Walmsley63c85232009-09-03 20:14:03 +03001166 */
1167static int _wait_target_ready(struct omap_hwmod *oh)
1168{
1169 struct omap_hwmod_ocp_if *os;
1170 int ret;
1171
1172 if (!oh)
1173 return -EINVAL;
1174
1175 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1176 return 0;
1177
Benoit Cousson682fdc92010-05-20 12:31:09 -06001178 os = oh->slaves[oh->_mpu_port_index];
Paul Walmsley63c85232009-09-03 20:14:03 +03001179
Benoit Cousson33f7ec82010-05-20 12:31:09 -06001180 if (oh->flags & HWMOD_NO_IDLEST)
Paul Walmsley63c85232009-09-03 20:14:03 +03001181 return 0;
1182
1183 /* XXX check module SIDLEMODE */
1184
1185 /* XXX check clock enable states */
1186
1187 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1188 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
1189 oh->prcm.omap2.idlest_reg_id,
1190 oh->prcm.omap2.idlest_idle_bit);
Paul Walmsley63c85232009-09-03 20:14:03 +03001191 } else if (cpu_is_omap44xx()) {
Benoit Coussond0f06312011-07-10 05:56:30 -06001192 if (!oh->clkdm)
1193 return -EINVAL;
1194
1195 ret = omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
1196 oh->clkdm->cm_inst,
1197 oh->clkdm->clkdm_offs,
1198 oh->prcm.omap4.clkctrl_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001199 } else {
1200 BUG();
1201 };
1202
1203 return ret;
1204}
1205
1206/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001207 * _lookup_hardreset - fill register bit info for this hwmod/reset line
Benoît Cousson5365efb2010-09-21 10:34:11 -06001208 * @oh: struct omap_hwmod *
1209 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001210 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
Benoît Cousson5365efb2010-09-21 10:34:11 -06001211 *
1212 * Return the bit position of the reset line that match the
1213 * input name. Return -ENOENT if not found.
1214 */
omar ramirezcc1226e2011-03-04 13:32:44 -07001215static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1216 struct omap_hwmod_rst_info *ohri)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001217{
1218 int i;
1219
1220 for (i = 0; i < oh->rst_lines_cnt; i++) {
1221 const char *rst_line = oh->rst_lines[i].name;
1222 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001223 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1224 ohri->st_shift = oh->rst_lines[i].st_shift;
1225 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1226 oh->name, __func__, rst_line, ohri->rst_shift,
1227 ohri->st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001228
omar ramirezcc1226e2011-03-04 13:32:44 -07001229 return 0;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001230 }
1231 }
1232
1233 return -ENOENT;
1234}
1235
1236/**
1237 * _assert_hardreset - assert the HW reset line of submodules
1238 * contained in the hwmod module.
1239 * @oh: struct omap_hwmod *
1240 * @name: name of the reset line to lookup and assert
1241 *
1242 * Some IP like dsp, ipu or iva contain processor that require
1243 * an HW reset line to be assert / deassert in order to enable fully
1244 * the IP.
1245 */
1246static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1247{
omar ramirezcc1226e2011-03-04 13:32:44 -07001248 struct omap_hwmod_rst_info ohri;
1249 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001250
1251 if (!oh)
1252 return -EINVAL;
1253
omar ramirezcc1226e2011-03-04 13:32:44 -07001254 ret = _lookup_hardreset(oh, name, &ohri);
1255 if (IS_ERR_VALUE(ret))
1256 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001257
1258 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1259 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001260 ohri.rst_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001261 else if (cpu_is_omap44xx())
Benoit Coussoneaac3292011-07-10 05:56:31 -06001262 return omap4_prminst_assert_hardreset(ohri.rst_shift,
1263 oh->clkdm->pwrdm.ptr->prcm_partition,
1264 oh->clkdm->pwrdm.ptr->prcm_offs,
1265 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001266 else
1267 return -EINVAL;
1268}
1269
1270/**
1271 * _deassert_hardreset - deassert the HW reset line of submodules contained
1272 * in the hwmod module.
1273 * @oh: struct omap_hwmod *
1274 * @name: name of the reset line to look up and deassert
1275 *
1276 * Some IP like dsp, ipu or iva contain processor that require
1277 * an HW reset line to be assert / deassert in order to enable fully
1278 * the IP.
1279 */
1280static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1281{
omar ramirezcc1226e2011-03-04 13:32:44 -07001282 struct omap_hwmod_rst_info ohri;
1283 int ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001284
1285 if (!oh)
1286 return -EINVAL;
1287
omar ramirezcc1226e2011-03-04 13:32:44 -07001288 ret = _lookup_hardreset(oh, name, &ohri);
1289 if (IS_ERR_VALUE(ret))
1290 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001291
omar ramirezcc1226e2011-03-04 13:32:44 -07001292 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1293 ret = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
1294 ohri.rst_shift,
1295 ohri.st_shift);
1296 } else if (cpu_is_omap44xx()) {
1297 if (ohri.st_shift)
1298 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
1299 oh->name, name);
Benoit Coussoneaac3292011-07-10 05:56:31 -06001300 ret = omap4_prminst_deassert_hardreset(ohri.rst_shift,
1301 oh->clkdm->pwrdm.ptr->prcm_partition,
1302 oh->clkdm->pwrdm.ptr->prcm_offs,
1303 oh->prcm.omap4.rstctrl_offs);
omar ramirezcc1226e2011-03-04 13:32:44 -07001304 } else {
Benoît Cousson5365efb2010-09-21 10:34:11 -06001305 return -EINVAL;
omar ramirezcc1226e2011-03-04 13:32:44 -07001306 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001307
omar ramirezcc1226e2011-03-04 13:32:44 -07001308 if (ret == -EBUSY)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001309 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1310
omar ramirezcc1226e2011-03-04 13:32:44 -07001311 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001312}
1313
1314/**
1315 * _read_hardreset - read the HW reset line state of submodules
1316 * contained in the hwmod module
1317 * @oh: struct omap_hwmod *
1318 * @name: name of the reset line to look up and read
1319 *
1320 * Return the state of the reset line.
1321 */
1322static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1323{
omar ramirezcc1226e2011-03-04 13:32:44 -07001324 struct omap_hwmod_rst_info ohri;
1325 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001326
1327 if (!oh)
1328 return -EINVAL;
1329
omar ramirezcc1226e2011-03-04 13:32:44 -07001330 ret = _lookup_hardreset(oh, name, &ohri);
1331 if (IS_ERR_VALUE(ret))
1332 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001333
1334 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1335 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001336 ohri.st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001337 } else if (cpu_is_omap44xx()) {
Benoit Coussoneaac3292011-07-10 05:56:31 -06001338 return omap4_prminst_is_hardreset_asserted(ohri.rst_shift,
1339 oh->clkdm->pwrdm.ptr->prcm_partition,
1340 oh->clkdm->pwrdm.ptr->prcm_offs,
1341 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001342 } else {
1343 return -EINVAL;
1344 }
1345}
1346
1347/**
Paul Walmsley747834a2012-04-18 19:10:04 -06001348 * _are_any_hardreset_lines_asserted - return true if part of @oh is hard-reset
1349 * @oh: struct omap_hwmod *
1350 *
1351 * If any hardreset line associated with @oh is asserted, then return true.
1352 * Otherwise, if @oh has no hardreset lines associated with it, or if
1353 * no hardreset lines associated with @oh are asserted, then return false.
1354 * This function is used to avoid executing some parts of the IP block
1355 * enable/disable sequence if a hardreset line is set.
1356 */
1357static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1358{
1359 int i;
1360
1361 if (oh->rst_lines_cnt == 0)
1362 return false;
1363
1364 for (i = 0; i < oh->rst_lines_cnt; i++)
1365 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1366 return true;
1367
1368 return false;
1369}
1370
1371/**
1372 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1373 * @oh: struct omap_hwmod *
1374 *
1375 * Disable the PRCM module mode related to the hwmod @oh.
1376 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1377 */
1378static int _omap4_disable_module(struct omap_hwmod *oh)
1379{
1380 int v;
1381
1382 /* The module mode does not exist prior OMAP4 */
1383 if (!cpu_is_omap44xx())
1384 return -EINVAL;
1385
1386 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1387 return -EINVAL;
1388
1389 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1390
1391 omap4_cminst_module_disable(oh->clkdm->prcm_partition,
1392 oh->clkdm->cm_inst,
1393 oh->clkdm->clkdm_offs,
1394 oh->prcm.omap4.clkctrl_offs);
1395
1396 if (_are_any_hardreset_lines_asserted(oh))
1397 return 0;
1398
1399 v = _omap4_wait_target_disable(oh);
1400 if (v)
1401 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1402 oh->name);
1403
1404 return 0;
1405}
1406
1407/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001408 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001409 * @oh: struct omap_hwmod *
1410 *
1411 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Paul Walmsley30e105c2012-04-19 00:49:09 -06001412 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1413 * reset this way, -EINVAL if the hwmod is in the wrong state,
1414 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001415 *
1416 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001417 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001418 * use the SYSCONFIG softreset bit to provide the status.
1419 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001420 * Note that some IP like McBSP do have reset control but don't have
1421 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001422 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001423static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001424{
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001425 u32 v, softrst_mask;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001426 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001427 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001428
Paul Walmsley43b40992010-02-22 22:09:34 -07001429 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001430 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley30e105c2012-04-19 00:49:09 -06001431 return -ENOENT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001432
1433 /* clocks must be on for this operation */
1434 if (oh->_state != _HWMOD_STATE_ENABLED) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001435 pr_warning("omap_hwmod: %s: reset can only be entered from "
1436 "enabled state\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001437 return -EINVAL;
1438 }
1439
Benoit Cousson96835af2010-09-21 18:57:58 +02001440 /* For some modules, all optionnal clocks need to be enabled as well */
1441 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1442 _enable_optional_clocks(oh);
1443
Paul Walmsleybd361792010-12-14 12:42:35 -07001444 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001445
1446 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001447 ret = _set_softreset(oh, &v);
1448 if (ret)
1449 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001450 _write_sysconfig(v, oh);
1451
Fernando Guzman Lugod99de7f2012-04-13 05:08:03 -06001452 if (oh->class->sysc->srst_udelay)
1453 udelay(oh->class->sysc->srst_udelay);
1454
Benoit Cousson2cb06812010-09-21 18:57:59 +02001455 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001456 omap_test_timeout((omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001457 oh->class->sysc->syss_offs)
1458 & SYSS_RESETDONE_MASK),
1459 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001460 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
1461 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001462 omap_test_timeout(!(omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001463 oh->class->sysc->sysc_offs)
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001464 & softrst_mask),
Benoit Cousson2cb06812010-09-21 18:57:59 +02001465 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001466 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001467
Benoît Cousson5365efb2010-09-21 10:34:11 -06001468 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001469 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1470 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001471 else
Benoît Cousson5365efb2010-09-21 10:34:11 -06001472 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001473
1474 /*
1475 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1476 * _wait_target_ready() or _reset()
1477 */
1478
Benoit Cousson96835af2010-09-21 18:57:58 +02001479 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1480
1481dis_opt_clks:
1482 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1483 _disable_optional_clocks(oh);
1484
1485 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001486}
1487
1488/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001489 * _reset - reset an omap_hwmod
1490 * @oh: struct omap_hwmod *
1491 *
Paul Walmsley30e105c2012-04-19 00:49:09 -06001492 * Resets an omap_hwmod @oh. If the module has a custom reset
1493 * function pointer defined, then call it to reset the IP block, and
1494 * pass along its return value to the caller. Otherwise, if the IP
1495 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1496 * associated with it, call a function to reset the IP block via that
1497 * method, and pass along the return value to the caller. Finally, if
1498 * the IP block has some hardreset lines associated with it, assert
1499 * all of those, but do _not_ deassert them. (This is because driver
1500 * authors have expressed an apparent requirement to control the
1501 * deassertion of the hardreset lines themselves.)
1502 *
1503 * The default software reset mechanism for most OMAP IP blocks is
1504 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1505 * hwmods cannot be reset via this method. Some are not targets and
1506 * therefore have no OCP header registers to access. Others (like the
1507 * IVA) have idiosyncratic reset sequences. So for these relatively
1508 * rare cases, custom reset code can be supplied in the struct
1509 * omap_hwmod_class .reset function pointer. Passes along the return
1510 * value from either _ocp_softreset() or the custom reset function -
1511 * these must return -EINVAL if the hwmod cannot be reset this way or
1512 * if the hwmod is in the wrong state, -ETIMEDOUT if the module did
1513 * not reset in time, or 0 upon success.
Paul Walmsleybd361792010-12-14 12:42:35 -07001514 */
1515static int _reset(struct omap_hwmod *oh)
1516{
Paul Walmsley30e105c2012-04-19 00:49:09 -06001517 int i, r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001518
1519 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1520
Paul Walmsley30e105c2012-04-19 00:49:09 -06001521 if (oh->class->reset) {
1522 r = oh->class->reset(oh);
1523 } else {
1524 if (oh->rst_lines_cnt > 0) {
1525 for (i = 0; i < oh->rst_lines_cnt; i++)
1526 _assert_hardreset(oh, oh->rst_lines[i].name);
1527 return 0;
1528 } else {
1529 r = _ocp_softreset(oh);
1530 if (r == -ENOENT)
1531 r = 0;
1532 }
1533 }
Paul Walmsleybd361792010-12-14 12:42:35 -07001534
Paul Walmsley30e105c2012-04-19 00:49:09 -06001535 /*
1536 * OCP_SYSCONFIG bits need to be reprogrammed after a
1537 * softreset. The _enable() function should be split to avoid
1538 * the rewrite of the OCP_SYSCONFIG register.
1539 */
Rajendra Nayak28008522012-03-13 22:55:23 +05301540 if (oh->class->sysc) {
1541 _update_sysc_cache(oh);
1542 _enable_sysc(oh);
1543 }
1544
Paul Walmsley30e105c2012-04-19 00:49:09 -06001545 return r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001546}
1547
1548/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001549 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001550 * @oh: struct omap_hwmod *
1551 *
1552 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001553 * register target. Returns -EINVAL if the hwmod is in the wrong
1554 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001555 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001556static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001557{
Paul Walmsley747834a2012-04-18 19:10:04 -06001558 int r;
Rajendra Nayak665d0012011-07-10 05:57:07 -06001559 int hwsup = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001560
Benoit Cousson34617e22011-07-01 22:54:07 +02001561 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1562
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001563 /*
Paul Walmsley64813c32012-04-18 19:10:03 -06001564 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
1565 * state at init. Now that someone is really trying to enable
1566 * them, just ensure that the hwmod mux is set.
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001567 */
1568 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
1569 /*
1570 * If the caller has mux data populated, do the mux'ing
1571 * which wouldn't have been done as part of the _enable()
1572 * done during setup.
1573 */
1574 if (oh->mux)
1575 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
1576
1577 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
1578 return 0;
1579 }
1580
Paul Walmsley63c85232009-09-03 20:14:03 +03001581 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1582 oh->_state != _HWMOD_STATE_IDLE &&
1583 oh->_state != _HWMOD_STATE_DISABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001584 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
1585 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001586 return -EINVAL;
1587 }
1588
Benoit Cousson31f62862011-07-01 22:54:05 +02001589 /*
Paul Walmsley747834a2012-04-18 19:10:04 -06001590 * If an IP block contains HW reset lines and any of them are
1591 * asserted, we let integration code associated with that
1592 * block handle the enable. We've received very little
1593 * information on what those driver authors need, and until
1594 * detailed information is provided and the driver code is
1595 * posted to the public lists, this is probably the best we
1596 * can do.
Benoit Cousson31f62862011-07-01 22:54:05 +02001597 */
Paul Walmsley747834a2012-04-18 19:10:04 -06001598 if (_are_any_hardreset_lines_asserted(oh))
1599 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001600
Rajendra Nayak665d0012011-07-10 05:57:07 -06001601 /* Mux pins for device runtime if populated */
1602 if (oh->mux && (!oh->mux->enabled ||
1603 ((oh->_state == _HWMOD_STATE_IDLE) &&
1604 oh->mux->pads_dynamic)))
1605 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
Benoit Cousson34617e22011-07-01 22:54:07 +02001606
Rajendra Nayak665d0012011-07-10 05:57:07 -06001607 _add_initiator_dep(oh, mpu_oh);
1608
1609 if (oh->clkdm) {
1610 /*
1611 * A clockdomain must be in SW_SUP before enabling
1612 * completely the module. The clockdomain can be set
1613 * in HW_AUTO only when the module become ready.
1614 */
1615 hwsup = clkdm_in_hwsup(oh->clkdm);
1616 r = clkdm_hwmod_enable(oh->clkdm, oh);
1617 if (r) {
1618 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1619 oh->name, oh->clkdm->name, r);
1620 return r;
1621 }
Benoit Cousson34617e22011-07-01 22:54:07 +02001622 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06001623
1624 _enable_clocks(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001625 _enable_module(oh);
Benoit Cousson34617e22011-07-01 22:54:07 +02001626
Rajendra Nayak665d0012011-07-10 05:57:07 -06001627 r = _wait_target_ready(oh);
1628 if (!r) {
1629 /*
1630 * Set the clockdomain to HW_AUTO only if the target is ready,
1631 * assuming that the previous state was HW_AUTO
1632 */
1633 if (oh->clkdm && hwsup)
1634 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02001635
Rajendra Nayak665d0012011-07-10 05:57:07 -06001636 oh->_state = _HWMOD_STATE_ENABLED;
1637
1638 /* Access the sysconfig only if the target is ready */
1639 if (oh->class->sysc) {
1640 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1641 _update_sysc_cache(oh);
1642 _enable_sysc(oh);
1643 }
1644 } else {
1645 _disable_clocks(oh);
1646 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1647 oh->name, r);
1648
1649 if (oh->clkdm)
1650 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001651 }
1652
Paul Walmsley63c85232009-09-03 20:14:03 +03001653 return r;
1654}
1655
1656/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001657 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001658 * @oh: struct omap_hwmod *
1659 *
1660 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001661 * no further work. Returns -EINVAL if the hwmod is in the wrong
1662 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001663 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001664static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001665{
Benoit Cousson34617e22011-07-01 22:54:07 +02001666 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1667
Paul Walmsley63c85232009-09-03 20:14:03 +03001668 if (oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001669 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
1670 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001671 return -EINVAL;
1672 }
1673
Paul Walmsley747834a2012-04-18 19:10:04 -06001674 if (_are_any_hardreset_lines_asserted(oh))
1675 return 0;
1676
Paul Walmsley43b40992010-02-22 22:09:34 -07001677 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001678 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001679 _del_initiator_dep(oh, mpu_oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001680
1681 _omap4_disable_module(oh);
1682
Benoit Cousson45c38252011-07-10 05:56:33 -06001683 /*
1684 * The module must be in idle mode before disabling any parents
1685 * clocks. Otherwise, the parent clock might be disabled before
1686 * the module transition is done, and thus will prevent the
1687 * transition to complete properly.
1688 */
1689 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001690 if (oh->clkdm)
1691 clkdm_hwmod_disable(oh->clkdm, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001692
Tony Lindgren8d9af882010-12-22 18:42:35 -08001693 /* Mux pins for device idle if populated */
Tony Lindgren029268e2011-03-11 11:32:25 -08001694 if (oh->mux && oh->mux->pads_dynamic)
Tony Lindgren8d9af882010-12-22 18:42:35 -08001695 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
1696
Paul Walmsley63c85232009-09-03 20:14:03 +03001697 oh->_state = _HWMOD_STATE_IDLE;
1698
1699 return 0;
1700}
1701
1702/**
Kishon Vijay Abraham I95992172011-03-10 03:50:08 -07001703 * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
1704 * @oh: struct omap_hwmod *
1705 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
1706 *
1707 * Sets the IP block's OCP autoidle bit in hardware, and updates our
1708 * local copy. Intended to be used by drivers that require
1709 * direct manipulation of the AUTOIDLE bits.
1710 * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
1711 * along the return value from _set_module_autoidle().
1712 *
1713 * Any users of this function should be scrutinized carefully.
1714 */
1715int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
1716{
1717 u32 v;
1718 int retval = 0;
1719 unsigned long flags;
1720
1721 if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
1722 return -EINVAL;
1723
1724 spin_lock_irqsave(&oh->_lock, flags);
1725
1726 v = oh->_sysc_cache;
1727
1728 retval = _set_module_autoidle(oh, autoidle, &v);
1729
1730 if (!retval)
1731 _write_sysconfig(v, oh);
1732
1733 spin_unlock_irqrestore(&oh->_lock, flags);
1734
1735 return retval;
1736}
1737
1738/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001739 * _shutdown - shutdown an omap_hwmod
1740 * @oh: struct omap_hwmod *
1741 *
1742 * Shut down an omap_hwmod @oh. This should be called when the driver
1743 * used for the hwmod is removed or unloaded or if the driver is not
1744 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1745 * state or returns 0.
1746 */
1747static int _shutdown(struct omap_hwmod *oh)
1748{
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001749 int ret, i;
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001750 u8 prev_state;
1751
Paul Walmsley63c85232009-09-03 20:14:03 +03001752 if (oh->_state != _HWMOD_STATE_IDLE &&
1753 oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001754 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
1755 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001756 return -EINVAL;
1757 }
1758
Paul Walmsley747834a2012-04-18 19:10:04 -06001759 if (_are_any_hardreset_lines_asserted(oh))
1760 return 0;
1761
Paul Walmsley63c85232009-09-03 20:14:03 +03001762 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
1763
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001764 if (oh->class->pre_shutdown) {
1765 prev_state = oh->_state;
1766 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001767 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001768 ret = oh->class->pre_shutdown(oh);
1769 if (ret) {
1770 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001771 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001772 return ret;
1773 }
1774 }
1775
Miguel Vadillo6481c732011-07-01 22:54:02 +02001776 if (oh->class->sysc) {
1777 if (oh->_state == _HWMOD_STATE_IDLE)
1778 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001779 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02001780 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001781
Benoit Cousson3827f942010-09-21 10:34:08 -06001782 /* clocks and deps are already disabled in idle */
1783 if (oh->_state == _HWMOD_STATE_ENABLED) {
1784 _del_initiator_dep(oh, mpu_oh);
1785 /* XXX what about the other system initiators here? dma, dsp */
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001786 _omap4_disable_module(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001787 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001788 if (oh->clkdm)
1789 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06001790 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001791 /* XXX Should this code also force-disable the optional clocks? */
1792
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001793 for (i = 0; i < oh->rst_lines_cnt; i++)
1794 _assert_hardreset(oh, oh->rst_lines[i].name);
Benoit Cousson31f62862011-07-01 22:54:05 +02001795
Tony Lindgren8d9af882010-12-22 18:42:35 -08001796 /* Mux pins to safe mode or use populated off mode values */
1797 if (oh->mux)
1798 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03001799
1800 oh->_state = _HWMOD_STATE_DISABLED;
1801
1802 return 0;
1803}
1804
1805/**
Paul Walmsley381d0332012-04-19 00:58:22 -06001806 * _init_mpu_rt_base - populate the virtual address for a hwmod
1807 * @oh: struct omap_hwmod * to locate the virtual address
1808 *
1809 * Cache the virtual address used by the MPU to access this IP block's
1810 * registers. This address is needed early so the OCP registers that
1811 * are part of the device's address space can be ioremapped properly.
1812 * No return value.
1813 */
1814static void __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data)
1815{
1816 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1817 return;
1818
1819 oh->_mpu_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
1820}
1821
1822/**
1823 * _init - initialize internal data for the hwmod @oh
1824 * @oh: struct omap_hwmod *
1825 * @n: (unused)
1826 *
1827 * Look up the clocks and the address space used by the MPU to access
1828 * registers belonging to the hwmod @oh. @oh must already be
1829 * registered at this point. This is the first of two phases for
1830 * hwmod initialization. Code called here does not touch any hardware
1831 * registers, it simply prepares internal data structures. Returns 0
1832 * upon success or if the hwmod isn't registered, or -EINVAL upon
1833 * failure.
1834 */
1835static int __init _init(struct omap_hwmod *oh, void *data)
1836{
1837 int r;
1838
1839 if (oh->_state != _HWMOD_STATE_REGISTERED)
1840 return 0;
1841
1842 _init_mpu_rt_base(oh, NULL);
1843
1844 r = _init_clocks(oh, NULL);
1845 if (IS_ERR_VALUE(r)) {
1846 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
1847 return -EINVAL;
1848 }
1849
1850 oh->_state = _HWMOD_STATE_INITIALIZED;
1851
1852 return 0;
1853}
1854
1855/**
Paul Walmsley64813c32012-04-18 19:10:03 -06001856 * _setup_iclk_autoidle - configure an IP block's interface clocks
Paul Walmsley63c85232009-09-03 20:14:03 +03001857 * @oh: struct omap_hwmod *
1858 *
Paul Walmsley64813c32012-04-18 19:10:03 -06001859 * Set up the module's interface clocks. XXX This function is still mostly
1860 * a stub; implementing this properly requires iclk autoidle usecounting in
1861 * the clock code. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001862 */
Paul Walmsley64813c32012-04-18 19:10:03 -06001863static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001864{
Paul Walmsley64813c32012-04-18 19:10:03 -06001865 int i;
Paul Walmsley97d60162010-07-26 16:34:30 -06001866
Paul Walmsley381d0332012-04-19 00:58:22 -06001867 if (oh->_state != _HWMOD_STATE_INITIALIZED)
Paul Walmsley64813c32012-04-18 19:10:03 -06001868 return;
Paul Walmsley48d54f32011-02-23 00:14:07 -07001869
Paul Walmsley64813c32012-04-18 19:10:03 -06001870 for (i = 0; i < oh->slaves_cnt; i++) {
1871 struct omap_hwmod_ocp_if *os = oh->slaves[i];
1872 struct clk *c = os->_clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03001873
Paul Walmsley64813c32012-04-18 19:10:03 -06001874 if (!c)
1875 continue;
Paul Walmsley63c85232009-09-03 20:14:03 +03001876
Paul Walmsley64813c32012-04-18 19:10:03 -06001877 if (os->flags & OCPIF_SWSUP_IDLE) {
1878 /* XXX omap_iclk_deny_idle(c); */
1879 } else {
1880 /* XXX omap_iclk_allow_idle(c); */
1881 clk_enable(c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001882 }
1883 }
1884
Paul Walmsley64813c32012-04-18 19:10:03 -06001885 return;
1886}
1887
1888/**
1889 * _setup_reset - reset an IP block during the setup process
1890 * @oh: struct omap_hwmod *
1891 *
1892 * Reset the IP block corresponding to the hwmod @oh during the setup
1893 * process. The IP block is first enabled so it can be successfully
1894 * reset. Returns 0 upon success or a negative error code upon
1895 * failure.
1896 */
1897static int __init _setup_reset(struct omap_hwmod *oh)
1898{
1899 int r;
1900
1901 if (oh->_state != _HWMOD_STATE_INITIALIZED)
1902 return -EINVAL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001903
Paul Walmsley747834a2012-04-18 19:10:04 -06001904 if (oh->rst_lines_cnt == 0) {
1905 r = _enable(oh);
1906 if (r) {
1907 pr_warning("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
1908 oh->name, oh->_state);
1909 return -EINVAL;
1910 }
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001911 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001912
Rajendra Nayak28008522012-03-13 22:55:23 +05301913 if (!(oh->flags & HWMOD_INIT_NO_RESET))
Paul Walmsley64813c32012-04-18 19:10:03 -06001914 r = _reset(oh);
1915
1916 return r;
1917}
1918
1919/**
1920 * _setup_postsetup - transition to the appropriate state after _setup
1921 * @oh: struct omap_hwmod *
1922 *
1923 * Place an IP block represented by @oh into a "post-setup" state --
1924 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
1925 * this function is called at the end of _setup().) The postsetup
1926 * state for an IP block can be changed by calling
1927 * omap_hwmod_enter_postsetup_state() early in the boot process,
1928 * before one of the omap_hwmod_setup*() functions are called for the
1929 * IP block.
1930 *
1931 * The IP block stays in this state until a PM runtime-based driver is
1932 * loaded for that IP block. A post-setup state of IDLE is
1933 * appropriate for almost all IP blocks with runtime PM-enabled
1934 * drivers, since those drivers are able to enable the IP block. A
1935 * post-setup state of ENABLED is appropriate for kernels with PM
1936 * runtime disabled. The DISABLED state is appropriate for unusual IP
1937 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
1938 * included, since the WDTIMER starts running on reset and will reset
1939 * the MPU if left active.
1940 *
1941 * This post-setup mechanism is deprecated. Once all of the OMAP
1942 * drivers have been converted to use PM runtime, and all of the IP
1943 * block data and interconnect data is available to the hwmod code, it
1944 * should be possible to replace this mechanism with a "lazy reset"
1945 * arrangement. In a "lazy reset" setup, each IP block is enabled
1946 * when the driver first probes, then all remaining IP blocks without
1947 * drivers are either shut down or enabled after the drivers have
1948 * loaded. However, this cannot take place until the above
1949 * preconditions have been met, since otherwise the late reset code
1950 * has no way of knowing which IP blocks are in use by drivers, and
1951 * which ones are unused.
1952 *
1953 * No return value.
1954 */
1955static void __init _setup_postsetup(struct omap_hwmod *oh)
1956{
1957 u8 postsetup_state;
1958
1959 if (oh->rst_lines_cnt > 0)
1960 return;
Benoit Cousson76e55892010-09-21 10:34:11 -06001961
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001962 postsetup_state = oh->_postsetup_state;
1963 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
1964 postsetup_state = _HWMOD_STATE_ENABLED;
1965
1966 /*
1967 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
1968 * it should be set by the core code as a runtime flag during startup
1969 */
1970 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001971 (postsetup_state == _HWMOD_STATE_IDLE)) {
1972 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001973 postsetup_state = _HWMOD_STATE_ENABLED;
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001974 }
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001975
1976 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001977 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001978 else if (postsetup_state == _HWMOD_STATE_DISABLED)
1979 _shutdown(oh);
1980 else if (postsetup_state != _HWMOD_STATE_ENABLED)
1981 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
1982 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03001983
Paul Walmsley64813c32012-04-18 19:10:03 -06001984 return;
1985}
1986
1987/**
1988 * _setup - prepare IP block hardware for use
1989 * @oh: struct omap_hwmod *
1990 * @n: (unused, pass NULL)
1991 *
1992 * Configure the IP block represented by @oh. This may include
1993 * enabling the IP block, resetting it, and placing it into a
1994 * post-setup state, depending on the type of IP block and applicable
1995 * flags. IP blocks are reset to prevent any previous configuration
1996 * by the bootloader or previous operating system from interfering
1997 * with power management or other parts of the system. The reset can
1998 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
1999 * two phases for hwmod initialization. Code called here generally
2000 * affects the IP block hardware, or system integration hardware
2001 * associated with the IP block. Returns 0.
2002 */
2003static int __init _setup(struct omap_hwmod *oh, void *data)
2004{
2005 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2006 return 0;
2007
2008 _setup_iclk_autoidle(oh);
2009
2010 if (!_setup_reset(oh))
2011 _setup_postsetup(oh);
2012
Paul Walmsley63c85232009-09-03 20:14:03 +03002013 return 0;
2014}
2015
Benoit Cousson0102b622010-12-21 21:31:27 -07002016/**
2017 * _register - register a struct omap_hwmod
2018 * @oh: struct omap_hwmod *
2019 *
2020 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2021 * already has been registered by the same name; -EINVAL if the
2022 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2023 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2024 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2025 * success.
2026 *
2027 * XXX The data should be copied into bootmem, so the original data
2028 * should be marked __initdata and freed after init. This would allow
2029 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2030 * that the copy process would be relatively complex due to the large number
2031 * of substructures.
2032 */
Benoit Cousson01592df2010-12-21 21:31:28 -07002033static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07002034{
Paul Walmsley569edd702011-02-23 00:14:06 -07002035 int ms_id;
Benoit Cousson0102b622010-12-21 21:31:27 -07002036
2037 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2038 (oh->_state != _HWMOD_STATE_UNKNOWN))
2039 return -EINVAL;
2040
Benoit Cousson0102b622010-12-21 21:31:27 -07002041 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2042
Benoit Coussonce35b242010-12-21 21:31:28 -07002043 if (_lookup(oh->name))
2044 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07002045
2046 ms_id = _find_mpu_port_index(oh);
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002047 if (!IS_ERR_VALUE(ms_id))
Benoit Cousson0102b622010-12-21 21:31:27 -07002048 oh->_mpu_port_index = ms_id;
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002049 else
Benoit Cousson0102b622010-12-21 21:31:27 -07002050 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Benoit Cousson0102b622010-12-21 21:31:27 -07002051
2052 list_add_tail(&oh->node, &omap_hwmod_list);
2053
2054 spin_lock_init(&oh->_lock);
2055
2056 oh->_state = _HWMOD_STATE_REGISTERED;
2057
Paul Walmsley569edd702011-02-23 00:14:06 -07002058 /*
2059 * XXX Rather than doing a strcmp(), this should test a flag
2060 * set in the hwmod data, inserted by the autogenerator code.
2061 */
2062 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2063 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07002064
Paul Walmsley569edd702011-02-23 00:14:06 -07002065 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07002066}
Paul Walmsley63c85232009-09-03 20:14:03 +03002067
2068
2069/* Public functions */
2070
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002071u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002072{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002073 if (oh->flags & HWMOD_16BIT_REG)
2074 return __raw_readw(oh->_mpu_rt_va + reg_offs);
2075 else
2076 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002077}
2078
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002079void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002080{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002081 if (oh->flags & HWMOD_16BIT_REG)
2082 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
2083 else
2084 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002085}
2086
Paul Walmsley887adea2010-07-26 16:34:33 -06002087/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002088 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
2089 * @oh: struct omap_hwmod *
2090 *
2091 * This is a public function exposed to drivers. Some drivers may need to do
2092 * some settings before and after resetting the device. Those drivers after
2093 * doing the necessary settings could use this function to start a reset by
2094 * setting the SYSCONFIG.SOFTRESET bit.
2095 */
2096int omap_hwmod_softreset(struct omap_hwmod *oh)
2097{
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002098 u32 v;
2099 int ret;
2100
2101 if (!oh || !(oh->_sysc_cache))
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002102 return -EINVAL;
2103
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002104 v = oh->_sysc_cache;
2105 ret = _set_softreset(oh, &v);
2106 if (ret)
2107 goto error;
2108 _write_sysconfig(v, oh);
2109
2110error:
2111 return ret;
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002112}
2113
2114/**
Paul Walmsley887adea2010-07-26 16:34:33 -06002115 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
2116 * @oh: struct omap_hwmod *
2117 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
2118 *
2119 * Sets the IP block's OCP slave idlemode in hardware, and updates our
2120 * local copy. Intended to be used by drivers that have some erratum
2121 * that requires direct manipulation of the SIDLEMODE bits. Returns
2122 * -EINVAL if @oh is null, or passes along the return value from
2123 * _set_slave_idlemode().
2124 *
2125 * XXX Does this function have any current users? If not, we should
2126 * remove it; it is better to let the rest of the hwmod code handle this.
2127 * Any users of this function should be scrutinized carefully.
2128 */
Kevin Hilman46273e62010-01-26 20:13:03 -07002129int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
2130{
2131 u32 v;
2132 int retval = 0;
2133
2134 if (!oh)
2135 return -EINVAL;
2136
2137 v = oh->_sysc_cache;
2138
2139 retval = _set_slave_idlemode(oh, idlemode, &v);
2140 if (!retval)
2141 _write_sysconfig(v, oh);
2142
2143 return retval;
2144}
2145
Paul Walmsley63c85232009-09-03 20:14:03 +03002146/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002147 * omap_hwmod_lookup - look up a registered omap_hwmod by name
2148 * @name: name of the omap_hwmod to look up
2149 *
2150 * Given a @name of an omap_hwmod, return a pointer to the registered
2151 * struct omap_hwmod *, or NULL upon error.
2152 */
2153struct omap_hwmod *omap_hwmod_lookup(const char *name)
2154{
2155 struct omap_hwmod *oh;
2156
2157 if (!name)
2158 return NULL;
2159
Paul Walmsley63c85232009-09-03 20:14:03 +03002160 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002161
2162 return oh;
2163}
2164
2165/**
2166 * omap_hwmod_for_each - call function for each registered omap_hwmod
2167 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06002168 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03002169 *
2170 * Call @fn for each registered omap_hwmod, passing @data to each
2171 * function. @fn must return 0 for success or any other value for
2172 * failure. If @fn returns non-zero, the iteration across omap_hwmods
2173 * will stop and the non-zero return value will be passed to the
2174 * caller of omap_hwmod_for_each(). @fn is called with
2175 * omap_hwmod_for_each() held.
2176 */
Paul Walmsley97d60162010-07-26 16:34:30 -06002177int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
2178 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03002179{
2180 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05302181 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002182
2183 if (!fn)
2184 return -EINVAL;
2185
Paul Walmsley63c85232009-09-03 20:14:03 +03002186 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06002187 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03002188 if (ret)
2189 break;
2190 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002191
2192 return ret;
2193}
2194
Paul Walmsley63c85232009-09-03 20:14:03 +03002195/**
Paul Walmsley550c8092011-02-28 11:58:14 -07002196 * omap_hwmod_register - register an array of hwmods
Paul Walmsley63c85232009-09-03 20:14:03 +03002197 * @ohs: pointer to an array of omap_hwmods to register
2198 *
2199 * Intended to be called early in boot before the clock framework is
2200 * initialized. If @ohs is not null, will register all omap_hwmods
Paul Walmsley550c8092011-02-28 11:58:14 -07002201 * listed in @ohs that are valid for this chip. Returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03002202 */
Paul Walmsley550c8092011-02-28 11:58:14 -07002203int __init omap_hwmod_register(struct omap_hwmod **ohs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002204{
Paul Walmsleybac1a0f2011-02-23 00:14:06 -07002205 int r, i;
Paul Walmsley63c85232009-09-03 20:14:03 +03002206
2207 if (!ohs)
2208 return 0;
2209
Paul Walmsleybac1a0f2011-02-23 00:14:06 -07002210 i = 0;
2211 do {
Paul Walmsleybac1a0f2011-02-23 00:14:06 -07002212 r = _register(ohs[i]);
2213 WARN(r, "omap_hwmod: %s: _register returned %d\n", ohs[i]->name,
2214 r);
2215 } while (ohs[++i]);
Paul Walmsley63c85232009-09-03 20:14:03 +03002216
2217 return 0;
2218}
2219
Paul Walmsley381d0332012-04-19 00:58:22 -06002220/**
2221 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
2222 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002223 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002224 * If the hwmod data corresponding to the MPU subsystem IP block
2225 * hasn't been initialized and set up yet, do so now. This must be
2226 * done first since sleep dependencies may be added from other hwmods
2227 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
2228 * return value.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002229 */
Paul Walmsley381d0332012-04-19 00:58:22 -06002230static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002231{
Paul Walmsley381d0332012-04-19 00:58:22 -06002232 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
2233 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
2234 __func__, MPU_INITIATOR_NAME);
2235 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
2236 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
Paul Walmsley63c85232009-09-03 20:14:03 +03002237}
2238
2239/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002240 * omap_hwmod_setup_one - set up a single hwmod
2241 * @oh_name: const char * name of the already-registered hwmod to set up
2242 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002243 * Initialize and set up a single hwmod. Intended to be used for a
2244 * small number of early devices, such as the timer IP blocks used for
2245 * the scheduler clock. Must be called after omap2_clk_init().
2246 * Resolves the struct clk names to struct clk pointers for each
2247 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
2248 * -EINVAL upon error or 0 upon success.
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002249 */
2250int __init omap_hwmod_setup_one(const char *oh_name)
2251{
2252 struct omap_hwmod *oh;
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002253
2254 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
2255
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002256 oh = _lookup(oh_name);
2257 if (!oh) {
2258 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
2259 return -EINVAL;
2260 }
2261
Paul Walmsley381d0332012-04-19 00:58:22 -06002262 _ensure_mpu_hwmod_is_setup(oh);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002263
Paul Walmsley381d0332012-04-19 00:58:22 -06002264 _init(oh, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002265 _setup(oh, NULL);
2266
2267 return 0;
2268}
2269
2270/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002271 * omap_hwmod_setup_all - set up all registered IP blocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002272 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002273 * Initialize and set up all IP blocks registered with the hwmod code.
2274 * Must be called after omap2_clk_init(). Resolves the struct clk
2275 * names to struct clk pointers for each registered omap_hwmod. Also
2276 * calls _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002277 */
Paul Walmsley550c8092011-02-28 11:58:14 -07002278static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03002279{
Paul Walmsley381d0332012-04-19 00:58:22 -06002280 _ensure_mpu_hwmod_is_setup(NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002281
Paul Walmsley381d0332012-04-19 00:58:22 -06002282 omap_hwmod_for_each(_init, NULL);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002283 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002284
2285 return 0;
2286}
Paul Walmsley550c8092011-02-28 11:58:14 -07002287core_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03002288
2289/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002290 * omap_hwmod_enable - enable an omap_hwmod
2291 * @oh: struct omap_hwmod *
2292 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002293 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03002294 * Returns -EINVAL on error or passes along the return value from _enable().
2295 */
2296int omap_hwmod_enable(struct omap_hwmod *oh)
2297{
2298 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002299 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002300
2301 if (!oh)
2302 return -EINVAL;
2303
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002304 spin_lock_irqsave(&oh->_lock, flags);
2305 r = _enable(oh);
2306 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002307
2308 return r;
2309}
2310
2311/**
2312 * omap_hwmod_idle - idle an omap_hwmod
2313 * @oh: struct omap_hwmod *
2314 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002315 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03002316 * Returns -EINVAL on error or passes along the return value from _idle().
2317 */
2318int omap_hwmod_idle(struct omap_hwmod *oh)
2319{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002320 unsigned long flags;
2321
Paul Walmsley63c85232009-09-03 20:14:03 +03002322 if (!oh)
2323 return -EINVAL;
2324
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002325 spin_lock_irqsave(&oh->_lock, flags);
2326 _idle(oh);
2327 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002328
2329 return 0;
2330}
2331
2332/**
2333 * omap_hwmod_shutdown - shutdown an omap_hwmod
2334 * @oh: struct omap_hwmod *
2335 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002336 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03002337 * omap_device_shutdown(). Returns -EINVAL on error or passes along
2338 * the return value from _shutdown().
2339 */
2340int omap_hwmod_shutdown(struct omap_hwmod *oh)
2341{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002342 unsigned long flags;
2343
Paul Walmsley63c85232009-09-03 20:14:03 +03002344 if (!oh)
2345 return -EINVAL;
2346
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002347 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002348 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002349 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002350
2351 return 0;
2352}
2353
2354/**
2355 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
2356 * @oh: struct omap_hwmod *oh
2357 *
2358 * Intended to be called by the omap_device code.
2359 */
2360int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
2361{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002362 unsigned long flags;
2363
2364 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002365 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002366 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002367
2368 return 0;
2369}
2370
2371/**
2372 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
2373 * @oh: struct omap_hwmod *oh
2374 *
2375 * Intended to be called by the omap_device code.
2376 */
2377int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
2378{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002379 unsigned long flags;
2380
2381 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002382 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002383 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002384
2385 return 0;
2386}
2387
2388/**
2389 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
2390 * @oh: struct omap_hwmod *oh
2391 *
2392 * Intended to be called by drivers and core code when all posted
2393 * writes to a device must complete before continuing further
2394 * execution (for example, after clearing some device IRQSTATUS
2395 * register bits)
2396 *
2397 * XXX what about targets with multiple OCP threads?
2398 */
2399void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
2400{
2401 BUG_ON(!oh);
2402
Paul Walmsley43b40992010-02-22 22:09:34 -07002403 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Russell King4f8a4282012-02-07 10:59:37 +00002404 WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
2405 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002406 return;
2407 }
2408
2409 /*
2410 * Forces posted writes to complete on the OCP thread handling
2411 * register writes
2412 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002413 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002414}
2415
2416/**
2417 * omap_hwmod_reset - reset the hwmod
2418 * @oh: struct omap_hwmod *
2419 *
2420 * Under some conditions, a driver may wish to reset the entire device.
2421 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06002422 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03002423 */
2424int omap_hwmod_reset(struct omap_hwmod *oh)
2425{
2426 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002427 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002428
Liam Girdwood9b579112010-09-21 10:34:09 -06002429 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002430 return -EINVAL;
2431
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002432 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002433 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002434 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002435
2436 return r;
2437}
2438
2439/**
2440 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
2441 * @oh: struct omap_hwmod *
2442 * @res: pointer to the first element of an array of struct resource to fill
2443 *
2444 * Count the number of struct resource array elements necessary to
2445 * contain omap_hwmod @oh resources. Intended to be called by code
2446 * that registers omap_devices. Intended to be used to determine the
2447 * size of a dynamically-allocated struct resource array, before
2448 * calling omap_hwmod_fill_resources(). Returns the number of struct
2449 * resource array elements needed.
2450 *
2451 * XXX This code is not optimized. It could attempt to merge adjacent
2452 * resource IDs.
2453 *
2454 */
2455int omap_hwmod_count_resources(struct omap_hwmod *oh)
2456{
2457 int ret, i;
2458
Paul Walmsleybc614952011-07-09 19:14:07 -06002459 ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002460
2461 for (i = 0; i < oh->slaves_cnt; i++)
Paul Walmsley78183f32011-07-09 19:14:05 -06002462 ret += _count_ocp_if_addr_spaces(oh->slaves[i]);
Paul Walmsley63c85232009-09-03 20:14:03 +03002463
2464 return ret;
2465}
2466
2467/**
2468 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
2469 * @oh: struct omap_hwmod *
2470 * @res: pointer to the first element of an array of struct resource to fill
2471 *
2472 * Fill the struct resource array @res with resource data from the
2473 * omap_hwmod @oh. Intended to be called by code that registers
2474 * omap_devices. See also omap_hwmod_count_resources(). Returns the
2475 * number of array elements filled.
2476 */
2477int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
2478{
Paul Walmsleybc614952011-07-09 19:14:07 -06002479 int i, j, mpu_irqs_cnt, sdma_reqs_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03002480 int r = 0;
2481
2482 /* For each IRQ, DMA, memory area, fill in array.*/
2483
Paul Walmsley212738a2011-07-09 19:14:06 -06002484 mpu_irqs_cnt = _count_mpu_irqs(oh);
2485 for (i = 0; i < mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07002486 (res + r)->name = (oh->mpu_irqs + i)->name;
2487 (res + r)->start = (oh->mpu_irqs + i)->irq;
2488 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03002489 (res + r)->flags = IORESOURCE_IRQ;
2490 r++;
2491 }
2492
Paul Walmsleybc614952011-07-09 19:14:07 -06002493 sdma_reqs_cnt = _count_sdma_reqs(oh);
2494 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06002495 (res + r)->name = (oh->sdma_reqs + i)->name;
2496 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
2497 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03002498 (res + r)->flags = IORESOURCE_DMA;
2499 r++;
2500 }
2501
2502 for (i = 0; i < oh->slaves_cnt; i++) {
2503 struct omap_hwmod_ocp_if *os;
Paul Walmsley78183f32011-07-09 19:14:05 -06002504 int addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03002505
Benoit Cousson682fdc92010-05-20 12:31:09 -06002506 os = oh->slaves[i];
Paul Walmsley78183f32011-07-09 19:14:05 -06002507 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03002508
Paul Walmsley78183f32011-07-09 19:14:05 -06002509 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08002510 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03002511 (res + r)->start = (os->addr + j)->pa_start;
2512 (res + r)->end = (os->addr + j)->pa_end;
2513 (res + r)->flags = IORESOURCE_MEM;
2514 r++;
2515 }
2516 }
2517
2518 return r;
2519}
2520
2521/**
2522 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
2523 * @oh: struct omap_hwmod *
2524 *
2525 * Return the powerdomain pointer associated with the OMAP module
2526 * @oh's main clock. If @oh does not have a main clk, return the
2527 * powerdomain associated with the interface clock associated with the
2528 * module's MPU port. (XXX Perhaps this should use the SDMA port
2529 * instead?) Returns NULL on error, or a struct powerdomain * on
2530 * success.
2531 */
2532struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
2533{
2534 struct clk *c;
2535
2536 if (!oh)
2537 return NULL;
2538
2539 if (oh->_clk) {
2540 c = oh->_clk;
2541 } else {
2542 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2543 return NULL;
2544 c = oh->slaves[oh->_mpu_port_index]->_clk;
2545 }
2546
Thara Gopinathd5647c12010-03-31 04:16:29 -06002547 if (!c->clkdm)
2548 return NULL;
2549
Paul Walmsley63c85232009-09-03 20:14:03 +03002550 return c->clkdm->pwrdm.ptr;
2551
2552}
2553
2554/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06002555 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
2556 * @oh: struct omap_hwmod *
2557 *
2558 * Returns the virtual address corresponding to the beginning of the
2559 * module's register target, in the address range that is intended to
2560 * be used by the MPU. Returns the virtual address upon success or NULL
2561 * upon error.
2562 */
2563void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
2564{
2565 if (!oh)
2566 return NULL;
2567
2568 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2569 return NULL;
2570
2571 if (oh->_state == _HWMOD_STATE_UNKNOWN)
2572 return NULL;
2573
2574 return oh->_mpu_rt_va;
2575}
2576
2577/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002578 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
2579 * @oh: struct omap_hwmod *
2580 * @init_oh: struct omap_hwmod * (initiator)
2581 *
2582 * Add a sleep dependency between the initiator @init_oh and @oh.
2583 * Intended to be called by DSP/Bridge code via platform_data for the
2584 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
2585 * code needs to add/del initiator dependencies dynamically
2586 * before/after accessing a device. Returns the return value from
2587 * _add_initiator_dep().
2588 *
2589 * XXX Keep a usecount in the clockdomain code
2590 */
2591int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
2592 struct omap_hwmod *init_oh)
2593{
2594 return _add_initiator_dep(oh, init_oh);
2595}
2596
2597/*
2598 * XXX what about functions for drivers to save/restore ocp_sysconfig
2599 * for context save/restore operations?
2600 */
2601
2602/**
2603 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
2604 * @oh: struct omap_hwmod *
2605 * @init_oh: struct omap_hwmod * (initiator)
2606 *
2607 * Remove a sleep dependency between the initiator @init_oh and @oh.
2608 * Intended to be called by DSP/Bridge code via platform_data for the
2609 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
2610 * code needs to add/del initiator dependencies dynamically
2611 * before/after accessing a device. Returns the return value from
2612 * _del_initiator_dep().
2613 *
2614 * XXX Keep a usecount in the clockdomain code
2615 */
2616int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
2617 struct omap_hwmod *init_oh)
2618{
2619 return _del_initiator_dep(oh, init_oh);
2620}
2621
2622/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002623 * omap_hwmod_enable_wakeup - allow device to wake up the system
2624 * @oh: struct omap_hwmod *
2625 *
2626 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
Govindraj.R2a1cc142012-04-05 02:59:32 -06002627 * send wakeups to the PRCM, and enable I/O ring wakeup events for
2628 * this IP block if it has dynamic mux entries. Eventually this
2629 * should set PRCM wakeup registers to cause the PRCM to receive
2630 * wakeup events from the module. Does not set any wakeup routing
2631 * registers beyond this point - if the module is to wake up any other
2632 * module or subsystem, that must be set separately. Called by
2633 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002634 */
2635int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
2636{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002637 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002638 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002639
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002640 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06002641
2642 if (oh->class->sysc &&
2643 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
2644 v = oh->_sysc_cache;
2645 _enable_wakeup(oh, &v);
2646 _write_sysconfig(v, oh);
2647 }
2648
Govindraj Receec002011-12-16 14:36:58 -07002649 _set_idle_ioring_wakeup(oh, true);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002650 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002651
2652 return 0;
2653}
2654
2655/**
2656 * omap_hwmod_disable_wakeup - prevent device from waking the system
2657 * @oh: struct omap_hwmod *
2658 *
2659 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
Govindraj.R2a1cc142012-04-05 02:59:32 -06002660 * from sending wakeups to the PRCM, and disable I/O ring wakeup
2661 * events for this IP block if it has dynamic mux entries. Eventually
2662 * this should clear PRCM wakeup registers to cause the PRCM to ignore
2663 * wakeup events from the module. Does not set any wakeup routing
2664 * registers beyond this point - if the module is to wake up any other
2665 * module or subsystem, that must be set separately. Called by
2666 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002667 */
2668int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
2669{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002670 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002671 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002672
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002673 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06002674
2675 if (oh->class->sysc &&
2676 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
2677 v = oh->_sysc_cache;
2678 _disable_wakeup(oh, &v);
2679 _write_sysconfig(v, oh);
2680 }
2681
Govindraj Receec002011-12-16 14:36:58 -07002682 _set_idle_ioring_wakeup(oh, false);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002683 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002684
2685 return 0;
2686}
Paul Walmsley43b40992010-02-22 22:09:34 -07002687
2688/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002689 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
2690 * contained in the hwmod module.
2691 * @oh: struct omap_hwmod *
2692 * @name: name of the reset line to lookup and assert
2693 *
2694 * Some IP like dsp, ipu or iva contain processor that require
2695 * an HW reset line to be assert / deassert in order to enable fully
2696 * the IP. Returns -EINVAL if @oh is null or if the operation is not
2697 * yet supported on this OMAP; otherwise, passes along the return value
2698 * from _assert_hardreset().
2699 */
2700int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
2701{
2702 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002703 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002704
2705 if (!oh)
2706 return -EINVAL;
2707
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002708 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002709 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002710 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002711
2712 return ret;
2713}
2714
2715/**
2716 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
2717 * contained in the hwmod module.
2718 * @oh: struct omap_hwmod *
2719 * @name: name of the reset line to look up and deassert
2720 *
2721 * Some IP like dsp, ipu or iva contain processor that require
2722 * an HW reset line to be assert / deassert in order to enable fully
2723 * the IP. Returns -EINVAL if @oh is null or if the operation is not
2724 * yet supported on this OMAP; otherwise, passes along the return value
2725 * from _deassert_hardreset().
2726 */
2727int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
2728{
2729 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002730 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002731
2732 if (!oh)
2733 return -EINVAL;
2734
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002735 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002736 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002737 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002738
2739 return ret;
2740}
2741
2742/**
2743 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
2744 * contained in the hwmod module
2745 * @oh: struct omap_hwmod *
2746 * @name: name of the reset line to look up and read
2747 *
2748 * Return the current state of the hwmod @oh's reset line named @name:
2749 * returns -EINVAL upon parameter error or if this operation
2750 * is unsupported on the current OMAP; otherwise, passes along the return
2751 * value from _read_hardreset().
2752 */
2753int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
2754{
2755 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002756 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002757
2758 if (!oh)
2759 return -EINVAL;
2760
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002761 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002762 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002763 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002764
2765 return ret;
2766}
2767
2768
2769/**
Paul Walmsley43b40992010-02-22 22:09:34 -07002770 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
2771 * @classname: struct omap_hwmod_class name to search for
2772 * @fn: callback function pointer to call for each hwmod in class @classname
2773 * @user: arbitrary context data to pass to the callback function
2774 *
Benoit Coussonce35b242010-12-21 21:31:28 -07002775 * For each omap_hwmod of class @classname, call @fn.
2776 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07002777 * zero, the iterator is terminated, and the callback function's return
2778 * value is passed back to the caller. Returns 0 upon success, -EINVAL
2779 * if @classname or @fn are NULL, or passes back the error code from @fn.
2780 */
2781int omap_hwmod_for_each_by_class(const char *classname,
2782 int (*fn)(struct omap_hwmod *oh,
2783 void *user),
2784 void *user)
2785{
2786 struct omap_hwmod *temp_oh;
2787 int ret = 0;
2788
2789 if (!classname || !fn)
2790 return -EINVAL;
2791
2792 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
2793 __func__, classname);
2794
Paul Walmsley43b40992010-02-22 22:09:34 -07002795 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
2796 if (!strcmp(temp_oh->class->name, classname)) {
2797 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
2798 __func__, temp_oh->name);
2799 ret = (*fn)(temp_oh, user);
2800 if (ret)
2801 break;
2802 }
2803 }
2804
Paul Walmsley43b40992010-02-22 22:09:34 -07002805 if (ret)
2806 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
2807 __func__, ret);
2808
2809 return ret;
2810}
2811
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002812/**
2813 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
2814 * @oh: struct omap_hwmod *
2815 * @state: state that _setup() should leave the hwmod in
2816 *
Paul Walmsley550c8092011-02-28 11:58:14 -07002817 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsley64813c32012-04-18 19:10:03 -06002818 * (called by omap_hwmod_setup_*()). See also the documentation
2819 * for _setup_postsetup(), above. Returns 0 upon success or
2820 * -EINVAL if there is a problem with the arguments or if the hwmod is
2821 * in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002822 */
2823int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
2824{
2825 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002826 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002827
2828 if (!oh)
2829 return -EINVAL;
2830
2831 if (state != _HWMOD_STATE_DISABLED &&
2832 state != _HWMOD_STATE_ENABLED &&
2833 state != _HWMOD_STATE_IDLE)
2834 return -EINVAL;
2835
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002836 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002837
2838 if (oh->_state != _HWMOD_STATE_REGISTERED) {
2839 ret = -EINVAL;
2840 goto ohsps_unlock;
2841 }
2842
2843 oh->_postsetup_state = state;
2844 ret = 0;
2845
2846ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002847 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002848
2849 return ret;
2850}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07002851
2852/**
2853 * omap_hwmod_get_context_loss_count - get lost context count
2854 * @oh: struct omap_hwmod *
2855 *
2856 * Query the powerdomain of of @oh to get the context loss
2857 * count for this device.
2858 *
2859 * Returns the context loss count of the powerdomain assocated with @oh
2860 * upon success, or zero if no powerdomain exists for @oh.
2861 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03002862int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07002863{
2864 struct powerdomain *pwrdm;
2865 int ret = 0;
2866
2867 pwrdm = omap_hwmod_get_pwrdm(oh);
2868 if (pwrdm)
2869 ret = pwrdm_get_context_loss_count(pwrdm);
2870
2871 return ret;
2872}
Paul Walmsley43b01642011-03-10 03:50:07 -07002873
2874/**
2875 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
2876 * @oh: struct omap_hwmod *
2877 *
2878 * Prevent the hwmod @oh from being reset during the setup process.
2879 * Intended for use by board-*.c files on boards with devices that
2880 * cannot tolerate being reset. Must be called before the hwmod has
2881 * been set up. Returns 0 upon success or negative error code upon
2882 * failure.
2883 */
2884int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
2885{
2886 if (!oh)
2887 return -EINVAL;
2888
2889 if (oh->_state != _HWMOD_STATE_REGISTERED) {
2890 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
2891 oh->name);
2892 return -EINVAL;
2893 }
2894
2895 oh->flags |= HWMOD_INIT_NO_RESET;
2896
2897 return 0;
2898}
Tero Kristoabc2d542011-12-16 14:36:59 -07002899
2900/**
2901 * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
2902 * @oh: struct omap_hwmod * containing hwmod mux entries
2903 * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
2904 * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
2905 *
2906 * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
2907 * entry number @pad_idx for the hwmod @oh, trigger the interrupt
2908 * service routine for the hwmod's mpu_irqs array index @irq_idx. If
2909 * this function is not called for a given pad_idx, then the ISR
2910 * associated with @oh's first MPU IRQ will be triggered when an I/O
2911 * pad wakeup occurs on that pad. Note that @pad_idx is the index of
2912 * the _dynamic or wakeup_ entry: if there are other entries not
2913 * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
2914 * entries are NOT COUNTED in the dynamic pad index. This function
2915 * must be called separately for each pad that requires its interrupt
2916 * to be re-routed this way. Returns -EINVAL if there is an argument
2917 * problem or if @oh does not have hwmod mux entries or MPU IRQs;
2918 * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
2919 *
2920 * XXX This function interface is fragile. Rather than using array
2921 * indexes, which are subject to unpredictable change, it should be
2922 * using hwmod IRQ names, and some other stable key for the hwmod mux
2923 * pad records.
2924 */
2925int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
2926{
2927 int nr_irqs;
2928
2929 might_sleep();
2930
2931 if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
2932 pad_idx >= oh->mux->nr_pads_dynamic)
2933 return -EINVAL;
2934
2935 /* Check the number of available mpu_irqs */
2936 for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
2937 ;
2938
2939 if (irq_idx >= nr_irqs)
2940 return -EINVAL;
2941
2942 if (!oh->mux->irqs) {
2943 /* XXX What frees this? */
2944 oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
2945 GFP_KERNEL);
2946 if (!oh->mux->irqs)
2947 return -ENOMEM;
2948 }
2949 oh->mux->irqs[pad_idx] = irq_idx;
2950
2951 return 0;
2952}