blob: 91b011e3a7cb1bb4b4f5ee3ca243acf4d646c96f [file] [log] [blame]
Paul Walmsley63c85232009-09-03 20:14:03 +03001/*
2 * omap_hwmod implementation for OMAP2/3/4
3 *
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06004 * Copyright (C) 2009-2010 Nokia Corporation
Paul Walmsley63c85232009-09-03 20:14:03 +03005 *
Paul Walmsley4788da22010-05-18 20:24:05 -06006 * Paul Walmsley, Benoît Cousson, Kevin Hilman
7 *
8 * Created in collaboration with (alphabetical order): Thara Gopinath,
9 * Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari Poussa, Anand
10 * Sawant, Santosh Shilimkar, Richard Woodruff
Paul Walmsley63c85232009-09-03 20:14:03 +030011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -060016 * Introduction
17 * ------------
18 * One way to view an OMAP SoC is as a collection of largely unrelated
19 * IP blocks connected by interconnects. The IP blocks include
20 * devices such as ARM processors, audio serial interfaces, UARTs,
21 * etc. Some of these devices, like the DSP, are created by TI;
22 * others, like the SGX, largely originate from external vendors. In
23 * TI's documentation, on-chip devices are referred to as "OMAP
24 * modules." Some of these IP blocks are identical across several
25 * OMAP versions. Others are revised frequently.
Paul Walmsley63c85232009-09-03 20:14:03 +030026 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -060027 * These OMAP modules are tied together by various interconnects.
28 * Most of the address and data flow between modules is via OCP-based
29 * interconnects such as the L3 and L4 buses; but there are other
30 * interconnects that distribute the hardware clock tree, handle idle
31 * and reset signaling, supply power, and connect the modules to
32 * various pads or balls on the OMAP package.
33 *
34 * OMAP hwmod provides a consistent way to describe the on-chip
35 * hardware blocks and their integration into the rest of the chip.
36 * This description can be automatically generated from the TI
37 * hardware database. OMAP hwmod provides a standard, consistent API
38 * to reset, enable, idle, and disable these hardware blocks. And
39 * hwmod provides a way for other core code, such as the Linux device
40 * code or the OMAP power management and address space mapping code,
41 * to query the hardware database.
42 *
43 * Using hwmod
44 * -----------
45 * Drivers won't call hwmod functions directly. That is done by the
46 * omap_device code, and in rare occasions, by custom integration code
47 * in arch/arm/ *omap*. The omap_device code includes functions to
48 * build a struct platform_device using omap_hwmod data, and that is
49 * currently how hwmod data is communicated to drivers and to the
50 * Linux driver model. Most drivers will call omap_hwmod functions only
51 * indirectly, via pm_runtime*() functions.
52 *
53 * From a layering perspective, here is where the OMAP hwmod code
54 * fits into the kernel software stack:
55 *
56 * +-------------------------------+
57 * | Device driver code |
58 * | (e.g., drivers/) |
59 * +-------------------------------+
60 * | Linux driver model |
61 * | (platform_device / |
62 * | platform_driver data/code) |
63 * +-------------------------------+
64 * | OMAP core-driver integration |
65 * |(arch/arm/mach-omap2/devices.c)|
66 * +-------------------------------+
67 * | omap_device code |
68 * | (../plat-omap/omap_device.c) |
69 * +-------------------------------+
70 * ----> | omap_hwmod code/data | <-----
71 * | (../mach-omap2/omap_hwmod*) |
72 * +-------------------------------+
73 * | OMAP clock/PRCM/register fns |
74 * | (__raw_{read,write}l, clk*) |
75 * +-------------------------------+
76 *
77 * Device drivers should not contain any OMAP-specific code or data in
78 * them. They should only contain code to operate the IP block that
79 * the driver is responsible for. This is because these IP blocks can
80 * also appear in other SoCs, either from TI (such as DaVinci) or from
81 * other manufacturers; and drivers should be reusable across other
82 * platforms.
83 *
84 * The OMAP hwmod code also will attempt to reset and idle all on-chip
85 * devices upon boot. The goal here is for the kernel to be
86 * completely self-reliant and independent from bootloaders. This is
87 * to ensure a repeatable configuration, both to ensure consistent
88 * runtime behavior, and to make it easier for others to reproduce
89 * bugs.
90 *
91 * OMAP module activity states
92 * ---------------------------
93 * The hwmod code considers modules to be in one of several activity
94 * states. IP blocks start out in an UNKNOWN state, then once they
95 * are registered via the hwmod code, proceed to the REGISTERED state.
96 * Once their clock names are resolved to clock pointers, the module
97 * enters the CLKS_INITED state; and finally, once the module has been
98 * reset and the integration registers programmed, the INITIALIZED state
99 * is entered. The hwmod code will then place the module into either
100 * the IDLE state to save power, or in the case of a critical system
101 * module, the ENABLED state.
102 *
103 * OMAP core integration code can then call omap_hwmod*() functions
104 * directly to move the module between the IDLE, ENABLED, and DISABLED
105 * states, as needed. This is done during both the PM idle loop, and
106 * in the OMAP core integration code's implementation of the PM runtime
107 * functions.
108 *
109 * References
110 * ----------
111 * This is a partial list.
Paul Walmsley63c85232009-09-03 20:14:03 +0300112 * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
113 * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
114 * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
115 * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
116 * - Open Core Protocol Specification 2.2
117 *
118 * To do:
119 * - pin mux handling
120 * - handle IO mapping
121 * - bus throughput & module latency measurement code
122 *
123 * XXX add tests at the beginning of each function to ensure the hwmod is
124 * in the appropriate state
125 * XXX error return values should be checked to ensure that they are
126 * appropriate
127 */
128#undef DEBUG
129
130#include <linux/kernel.h>
131#include <linux/errno.h>
132#include <linux/io.h>
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>
Paul Walmsley63c85232009-09-03 20:14:03 +0300139
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -0700140#include <plat/common.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -0700141#include <plat/cpu.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -0700142#include "clockdomain.h"
Paul Walmsley72e06d02010-12-21 21:05:16 -0700143#include "powerdomain.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700144#include <plat/clock.h>
145#include <plat/omap_hwmod.h>
Benoît Cousson5365efb2010-09-21 10:34:11 -0600146#include <plat/prcm.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300147
Paul Walmsley59fb6592010-12-21 15:30:55 -0700148#include "cm2xxx_3xxx.h"
149#include "cm44xx.h"
150#include "prm2xxx_3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -0700151#include "prm44xx.h"
Paul Walmsley63c85232009-09-03 20:14:03 +0300152
Benoît Cousson5365efb2010-09-21 10:34:11 -0600153/* Maximum microseconds to wait for OMAP module to softreset */
154#define MAX_MODULE_SOFTRESET_WAIT 10000
Paul Walmsley63c85232009-09-03 20:14:03 +0300155
156/* Name of the OMAP hwmod for the MPU */
Benoit Cousson5c2c0292010-05-20 12:31:10 -0600157#define MPU_INITIATOR_NAME "mpu"
Paul Walmsley63c85232009-09-03 20:14:03 +0300158
159/* omap_hwmod_list contains all registered struct omap_hwmods */
160static LIST_HEAD(omap_hwmod_list);
161
Paul Walmsley63c85232009-09-03 20:14:03 +0300162/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
163static struct omap_hwmod *mpu_oh;
164
165/* inited: 0 if omap_hwmod_init() has not yet been called; 1 otherwise */
166static u8 inited;
167
168
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;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700376 autoidle_mask = (0x3 << autoidle_shift);
377
378 *v &= ~autoidle_mask;
379 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700380
381 return 0;
382}
383
384/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300385 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
386 * @oh: struct omap_hwmod *
387 *
388 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
389 * upon error or 0 upon success.
390 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700391static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300392{
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700393 u32 wakeup_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300394
Paul Walmsley43b40992010-02-22 22:09:34 -0700395 if (!oh->class->sysc ||
396 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
Paul Walmsley63c85232009-09-03 20:14:03 +0300397 return -EINVAL;
398
Paul Walmsley43b40992010-02-22 22:09:34 -0700399 if (!oh->class->sysc->sysc_fields) {
400 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700401 return -EINVAL;
402 }
403
Paul Walmsley43b40992010-02-22 22:09:34 -0700404 wakeup_mask = (0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700405
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700406 *v |= wakeup_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300407
408 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
409
410 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
411
412 return 0;
413}
414
415/**
416 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
417 * @oh: struct omap_hwmod *
418 *
419 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
420 * upon error or 0 upon success.
421 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700422static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300423{
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700424 u32 wakeup_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300425
Paul Walmsley43b40992010-02-22 22:09:34 -0700426 if (!oh->class->sysc ||
427 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
Paul Walmsley63c85232009-09-03 20:14:03 +0300428 return -EINVAL;
429
Paul Walmsley43b40992010-02-22 22:09:34 -0700430 if (!oh->class->sysc->sysc_fields) {
431 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700432 return -EINVAL;
433 }
434
Paul Walmsley43b40992010-02-22 22:09:34 -0700435 wakeup_mask = (0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700436
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700437 *v &= ~wakeup_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300438
439 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
440
441 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
442
443 return 0;
444}
445
446/**
447 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
448 * @oh: struct omap_hwmod *
449 *
450 * Prevent the hardware module @oh from entering idle while the
451 * hardare module initiator @init_oh is active. Useful when a module
452 * will be accessed by a particular initiator (e.g., if a module will
453 * be accessed by the IVA, there should be a sleepdep between the IVA
454 * initiator and the module). Only applies to modules in smart-idle
455 * mode. Returns -EINVAL upon error or passes along
Paul Walmsley55ed9692010-01-26 20:12:59 -0700456 * clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300457 */
458static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
459{
460 if (!oh->_clk)
461 return -EINVAL;
462
Paul Walmsley55ed9692010-01-26 20:12:59 -0700463 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300464}
465
466/**
467 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
468 * @oh: struct omap_hwmod *
469 *
470 * Allow the hardware module @oh to enter idle while the hardare
471 * module initiator @init_oh is active. Useful when a module will not
472 * be accessed by a particular initiator (e.g., if a module will not
473 * be accessed by the IVA, there should be no sleepdep between the IVA
474 * initiator and the module). Only applies to modules in smart-idle
475 * mode. Returns -EINVAL upon error or passes along
Paul Walmsley55ed9692010-01-26 20:12:59 -0700476 * clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300477 */
478static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
479{
480 if (!oh->_clk)
481 return -EINVAL;
482
Paul Walmsley55ed9692010-01-26 20:12:59 -0700483 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300484}
485
486/**
487 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
488 * @oh: struct omap_hwmod *
489 *
490 * Called from _init_clocks(). Populates the @oh _clk (main
491 * functional clock pointer) if a main_clk is present. Returns 0 on
492 * success or -EINVAL on error.
493 */
494static int _init_main_clk(struct omap_hwmod *oh)
495{
Paul Walmsley63c85232009-09-03 20:14:03 +0300496 int ret = 0;
497
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700498 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300499 return 0;
500
Benoit Cousson63403382010-05-20 12:31:10 -0600501 oh->_clk = omap_clk_get_by_name(oh->main_clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600502 if (!oh->_clk) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600503 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
504 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600505 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600506 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300507
Benoit Cousson63403382010-05-20 12:31:10 -0600508 if (!oh->_clk->clkdm)
509 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
510 oh->main_clk, oh->_clk->name);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700511
Paul Walmsley63c85232009-09-03 20:14:03 +0300512 return ret;
513}
514
515/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600516 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300517 * @oh: struct omap_hwmod *
518 *
519 * Called from _init_clocks(). Populates the @oh OCP slave interface
520 * clock pointers. Returns 0 on success or -EINVAL on error.
521 */
522static int _init_interface_clks(struct omap_hwmod *oh)
523{
Paul Walmsley63c85232009-09-03 20:14:03 +0300524 struct clk *c;
525 int i;
526 int ret = 0;
527
528 if (oh->slaves_cnt == 0)
529 return 0;
530
Benoit Cousson682fdc92010-05-20 12:31:09 -0600531 for (i = 0; i < oh->slaves_cnt; i++) {
532 struct omap_hwmod_ocp_if *os = oh->slaves[i];
533
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700534 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300535 continue;
536
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700537 c = omap_clk_get_by_name(os->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600538 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600539 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
540 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300541 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600542 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300543 os->_clk = c;
544 }
545
546 return ret;
547}
548
549/**
550 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
551 * @oh: struct omap_hwmod *
552 *
553 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
554 * clock pointers. Returns 0 on success or -EINVAL on error.
555 */
556static int _init_opt_clks(struct omap_hwmod *oh)
557{
558 struct omap_hwmod_opt_clk *oc;
559 struct clk *c;
560 int i;
561 int ret = 0;
562
563 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700564 c = omap_clk_get_by_name(oc->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600565 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600566 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
567 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300568 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600569 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300570 oc->_clk = c;
571 }
572
573 return ret;
574}
575
576/**
577 * _enable_clocks - enable hwmod main clock and interface clocks
578 * @oh: struct omap_hwmod *
579 *
580 * Enables all clocks necessary for register reads and writes to succeed
581 * on the hwmod @oh. Returns 0.
582 */
583static int _enable_clocks(struct omap_hwmod *oh)
584{
Paul Walmsley63c85232009-09-03 20:14:03 +0300585 int i;
586
587 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
588
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600589 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300590 clk_enable(oh->_clk);
591
592 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -0600593 for (i = 0; i < oh->slaves_cnt; i++) {
594 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +0300595 struct clk *c = os->_clk;
596
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600597 if (c && (os->flags & OCPIF_SWSUP_IDLE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300598 clk_enable(c);
599 }
600 }
601
602 /* The opt clocks are controlled by the device driver. */
603
604 return 0;
605}
606
607/**
608 * _disable_clocks - disable hwmod main clock and interface clocks
609 * @oh: struct omap_hwmod *
610 *
611 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
612 */
613static int _disable_clocks(struct omap_hwmod *oh)
614{
Paul Walmsley63c85232009-09-03 20:14:03 +0300615 int i;
616
617 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
618
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600619 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300620 clk_disable(oh->_clk);
621
622 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -0600623 for (i = 0; i < oh->slaves_cnt; i++) {
624 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +0300625 struct clk *c = os->_clk;
626
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600627 if (c && (os->flags & OCPIF_SWSUP_IDLE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300628 clk_disable(c);
629 }
630 }
631
632 /* The opt clocks are controlled by the device driver. */
633
634 return 0;
635}
636
Benoit Cousson96835af2010-09-21 18:57:58 +0200637static void _enable_optional_clocks(struct omap_hwmod *oh)
638{
639 struct omap_hwmod_opt_clk *oc;
640 int i;
641
642 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
643
644 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
645 if (oc->_clk) {
646 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
647 oc->_clk->name);
648 clk_enable(oc->_clk);
649 }
650}
651
652static void _disable_optional_clocks(struct omap_hwmod *oh)
653{
654 struct omap_hwmod_opt_clk *oc;
655 int i;
656
657 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
658
659 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
660 if (oc->_clk) {
661 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
662 oc->_clk->name);
663 clk_disable(oc->_clk);
664 }
665}
666
Paul Walmsley63c85232009-09-03 20:14:03 +0300667/**
668 * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
669 * @oh: struct omap_hwmod *
670 *
671 * Returns the array index of the OCP slave port that the MPU
672 * addresses the device on, or -EINVAL upon error or not found.
673 */
Benoit Cousson01592df2010-12-21 21:31:28 -0700674static int __init _find_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300675{
Paul Walmsley63c85232009-09-03 20:14:03 +0300676 int i;
677 int found = 0;
678
679 if (!oh || oh->slaves_cnt == 0)
680 return -EINVAL;
681
Benoit Cousson682fdc92010-05-20 12:31:09 -0600682 for (i = 0; i < oh->slaves_cnt; i++) {
683 struct omap_hwmod_ocp_if *os = oh->slaves[i];
684
Paul Walmsley63c85232009-09-03 20:14:03 +0300685 if (os->user & OCP_USER_MPU) {
686 found = 1;
687 break;
688 }
689 }
690
691 if (found)
692 pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
693 oh->name, i);
694 else
695 pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
696 oh->name);
697
698 return (found) ? i : -EINVAL;
699}
700
701/**
702 * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
703 * @oh: struct omap_hwmod *
704 *
705 * Return the virtual address of the base of the register target of
706 * device @oh, or NULL on error.
707 */
Benoit Cousson01592df2010-12-21 21:31:28 -0700708static void __iomem * __init _find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
Paul Walmsley63c85232009-09-03 20:14:03 +0300709{
710 struct omap_hwmod_ocp_if *os;
711 struct omap_hwmod_addr_space *mem;
712 int i;
713 int found = 0;
Tony Lindgren986a13f2009-10-19 15:25:22 -0700714 void __iomem *va_start;
Paul Walmsley63c85232009-09-03 20:14:03 +0300715
716 if (!oh || oh->slaves_cnt == 0)
717 return NULL;
718
Benoit Cousson682fdc92010-05-20 12:31:09 -0600719 os = oh->slaves[index];
Paul Walmsley63c85232009-09-03 20:14:03 +0300720
721 for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
722 if (mem->flags & ADDR_TYPE_RT) {
723 found = 1;
724 break;
725 }
726 }
727
Tony Lindgren986a13f2009-10-19 15:25:22 -0700728 if (found) {
729 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
730 if (!va_start) {
731 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
732 return NULL;
733 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300734 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
Tony Lindgren986a13f2009-10-19 15:25:22 -0700735 oh->name, va_start);
736 } else {
Paul Walmsley63c85232009-09-03 20:14:03 +0300737 pr_debug("omap_hwmod: %s: no MPU register target found\n",
738 oh->name);
Tony Lindgren986a13f2009-10-19 15:25:22 -0700739 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300740
Tony Lindgren986a13f2009-10-19 15:25:22 -0700741 return (found) ? va_start : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +0300742}
743
744/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600745 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +0300746 * @oh: struct omap_hwmod *
747 *
748 * If module is marked as SWSUP_SIDLE, force the module out of slave
749 * idle; otherwise, configure it for smart-idle. If module is marked
750 * as SWSUP_MSUSPEND, force the module out of master standby;
751 * otherwise, configure it for smart-standby. No return value.
752 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600753static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300754{
Paul Walmsley43b40992010-02-22 22:09:34 -0700755 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +0300756 u32 v;
757
Paul Walmsley43b40992010-02-22 22:09:34 -0700758 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +0300759 return;
760
761 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -0700762 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +0300763
Paul Walmsley43b40992010-02-22 22:09:34 -0700764 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +0300765 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
766 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
767 _set_slave_idlemode(oh, idlemode, &v);
768 }
769
Paul Walmsley43b40992010-02-22 22:09:34 -0700770 if (sf & SYSC_HAS_MIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +0300771 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
772 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
773 _set_master_standbymode(oh, idlemode, &v);
774 }
775
Paul Walmsleya16b1f72009-12-08 16:34:17 -0700776 /*
777 * XXX The clock framework should handle this, by
778 * calling into this code. But this must wait until the
779 * clock structures are tagged with omap_hwmod entries
780 */
Paul Walmsley43b40992010-02-22 22:09:34 -0700781 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
782 (sf & SYSC_HAS_CLOCKACTIVITY))
783 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +0300784
Rajendra Nayak9980ce52010-09-21 19:58:30 +0530785 /* If slave is in SMARTIDLE, also enable wakeup */
786 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700787 _enable_wakeup(oh, &v);
788
789 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -0600790
791 /*
792 * Set the autoidle bit only after setting the smartidle bit
793 * Setting this will not have any impact on the other modules.
794 */
795 if (sf & SYSC_HAS_AUTOIDLE) {
796 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
797 0 : 1;
798 _set_module_autoidle(oh, idlemode, &v);
799 _write_sysconfig(v, oh);
800 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300801}
802
803/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600804 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +0300805 * @oh: struct omap_hwmod *
806 *
807 * If module is marked as SWSUP_SIDLE, force the module into slave
808 * idle; otherwise, configure it for smart-idle. If module is marked
809 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
810 * configure it for smart-standby. No return value.
811 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600812static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300813{
Paul Walmsley43b40992010-02-22 22:09:34 -0700814 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +0300815 u32 v;
816
Paul Walmsley43b40992010-02-22 22:09:34 -0700817 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +0300818 return;
819
820 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -0700821 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +0300822
Paul Walmsley43b40992010-02-22 22:09:34 -0700823 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +0300824 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
825 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
826 _set_slave_idlemode(oh, idlemode, &v);
827 }
828
Paul Walmsley43b40992010-02-22 22:09:34 -0700829 if (sf & SYSC_HAS_MIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +0300830 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
831 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
832 _set_master_standbymode(oh, idlemode, &v);
833 }
834
835 _write_sysconfig(v, oh);
836}
837
838/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600839 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +0300840 * @oh: struct omap_hwmod *
841 *
842 * Force the module into slave idle and master suspend. No return
843 * value.
844 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600845static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300846{
847 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -0700848 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +0300849
Paul Walmsley43b40992010-02-22 22:09:34 -0700850 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +0300851 return;
852
853 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -0700854 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +0300855
Paul Walmsley43b40992010-02-22 22:09:34 -0700856 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +0300857 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
858
Paul Walmsley43b40992010-02-22 22:09:34 -0700859 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +0300860 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
861
Paul Walmsley43b40992010-02-22 22:09:34 -0700862 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -0700863 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +0300864
865 _write_sysconfig(v, oh);
866}
867
868/**
869 * _lookup - find an omap_hwmod by name
870 * @name: find an omap_hwmod by name
871 *
872 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +0300873 */
874static struct omap_hwmod *_lookup(const char *name)
875{
876 struct omap_hwmod *oh, *temp_oh;
877
878 oh = NULL;
879
880 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
881 if (!strcmp(name, temp_oh->name)) {
882 oh = temp_oh;
883 break;
884 }
885 }
886
887 return oh;
888}
889
890/**
891 * _init_clocks - clk_get() all clocks associated with this hwmod
892 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -0600893 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +0300894 *
895 * Called by omap_hwmod_late_init() (after omap2_clk_init()).
Kevin Hilman12b1fdb2010-09-21 10:34:09 -0600896 * Resolves all clock names embedded in the hwmod. Returns -EINVAL if
897 * the omap_hwmod has not yet been registered or if the clocks have
898 * already been initialized, 0 on success, or a non-zero error on
899 * failure.
Paul Walmsley63c85232009-09-03 20:14:03 +0300900 */
Paul Walmsley97d60162010-07-26 16:34:30 -0600901static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +0300902{
903 int ret = 0;
904
905 if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
906 return -EINVAL;
907
908 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
909
910 ret |= _init_main_clk(oh);
911 ret |= _init_interface_clks(oh);
912 ret |= _init_opt_clks(oh);
913
Benoit Coussonf5c1f842010-05-20 12:31:10 -0600914 if (!ret)
915 oh->_state = _HWMOD_STATE_CLKS_INITED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300916
Benoit Coussonf5c1f842010-05-20 12:31:10 -0600917 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300918}
919
920/**
921 * _wait_target_ready - wait for a module to leave slave idle
922 * @oh: struct omap_hwmod *
923 *
924 * Wait for a module @oh to leave slave idle. Returns 0 if the module
925 * does not have an IDLEST bit or if the module successfully leaves
926 * slave idle; otherwise, pass along the return value of the
927 * appropriate *_cm_wait_module_ready() function.
928 */
929static int _wait_target_ready(struct omap_hwmod *oh)
930{
931 struct omap_hwmod_ocp_if *os;
932 int ret;
933
934 if (!oh)
935 return -EINVAL;
936
937 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
938 return 0;
939
Benoit Cousson682fdc92010-05-20 12:31:09 -0600940 os = oh->slaves[oh->_mpu_port_index];
Paul Walmsley63c85232009-09-03 20:14:03 +0300941
Benoit Cousson33f7ec82010-05-20 12:31:09 -0600942 if (oh->flags & HWMOD_NO_IDLEST)
Paul Walmsley63c85232009-09-03 20:14:03 +0300943 return 0;
944
945 /* XXX check module SIDLEMODE */
946
947 /* XXX check clock enable states */
948
949 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
950 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
951 oh->prcm.omap2.idlest_reg_id,
952 oh->prcm.omap2.idlest_idle_bit);
Paul Walmsley63c85232009-09-03 20:14:03 +0300953 } else if (cpu_is_omap44xx()) {
Benoit Cousson9a23dfe2010-05-20 12:31:08 -0600954 ret = omap4_cm_wait_module_ready(oh->prcm.omap4.clkctrl_reg);
Paul Walmsley63c85232009-09-03 20:14:03 +0300955 } else {
956 BUG();
957 };
958
959 return ret;
960}
961
962/**
Benoît Cousson5365efb2010-09-21 10:34:11 -0600963 * _lookup_hardreset - return the register bit shift for this hwmod/reset line
964 * @oh: struct omap_hwmod *
965 * @name: name of the reset line in the context of this hwmod
966 *
967 * Return the bit position of the reset line that match the
968 * input name. Return -ENOENT if not found.
969 */
970static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name)
971{
972 int i;
973
974 for (i = 0; i < oh->rst_lines_cnt; i++) {
975 const char *rst_line = oh->rst_lines[i].name;
976 if (!strcmp(rst_line, name)) {
977 u8 shift = oh->rst_lines[i].rst_shift;
978 pr_debug("omap_hwmod: %s: _lookup_hardreset: %s: %d\n",
979 oh->name, rst_line, shift);
980
981 return shift;
982 }
983 }
984
985 return -ENOENT;
986}
987
988/**
989 * _assert_hardreset - assert the HW reset line of submodules
990 * contained in the hwmod module.
991 * @oh: struct omap_hwmod *
992 * @name: name of the reset line to lookup and assert
993 *
994 * Some IP like dsp, ipu or iva contain processor that require
995 * an HW reset line to be assert / deassert in order to enable fully
996 * the IP.
997 */
998static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
999{
1000 u8 shift;
1001
1002 if (!oh)
1003 return -EINVAL;
1004
1005 shift = _lookup_hardreset(oh, name);
1006 if (IS_ERR_VALUE(shift))
1007 return shift;
1008
1009 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1010 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
1011 shift);
1012 else if (cpu_is_omap44xx())
1013 return omap4_prm_assert_hardreset(oh->prcm.omap4.rstctrl_reg,
1014 shift);
1015 else
1016 return -EINVAL;
1017}
1018
1019/**
1020 * _deassert_hardreset - deassert the HW reset line of submodules contained
1021 * in the hwmod module.
1022 * @oh: struct omap_hwmod *
1023 * @name: name of the reset line to look up and deassert
1024 *
1025 * Some IP like dsp, ipu or iva contain processor that require
1026 * an HW reset line to be assert / deassert in order to enable fully
1027 * the IP.
1028 */
1029static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1030{
1031 u8 shift;
1032 int r;
1033
1034 if (!oh)
1035 return -EINVAL;
1036
1037 shift = _lookup_hardreset(oh, name);
1038 if (IS_ERR_VALUE(shift))
1039 return shift;
1040
1041 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1042 r = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
1043 shift);
1044 else if (cpu_is_omap44xx())
1045 r = omap4_prm_deassert_hardreset(oh->prcm.omap4.rstctrl_reg,
1046 shift);
1047 else
1048 return -EINVAL;
1049
1050 if (r == -EBUSY)
1051 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1052
1053 return r;
1054}
1055
1056/**
1057 * _read_hardreset - read the HW reset line state of submodules
1058 * contained in the hwmod module
1059 * @oh: struct omap_hwmod *
1060 * @name: name of the reset line to look up and read
1061 *
1062 * Return the state of the reset line.
1063 */
1064static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1065{
1066 u8 shift;
1067
1068 if (!oh)
1069 return -EINVAL;
1070
1071 shift = _lookup_hardreset(oh, name);
1072 if (IS_ERR_VALUE(shift))
1073 return shift;
1074
1075 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1076 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
1077 shift);
1078 } else if (cpu_is_omap44xx()) {
1079 return omap4_prm_is_hardreset_asserted(oh->prcm.omap4.rstctrl_reg,
1080 shift);
1081 } else {
1082 return -EINVAL;
1083 }
1084}
1085
1086/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001087 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001088 * @oh: struct omap_hwmod *
1089 *
1090 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Kevin Hilman12b1fdb2010-09-21 10:34:09 -06001091 * enabled for this to work. Returns -EINVAL if the hwmod cannot be
1092 * reset this way or if the hwmod is in the wrong state, -ETIMEDOUT if
1093 * the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001094 *
1095 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001096 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001097 * use the SYSCONFIG softreset bit to provide the status.
1098 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001099 * Note that some IP like McBSP do have reset control but don't have
1100 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001101 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001102static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001103{
Benoit Cousson96835af2010-09-21 18:57:58 +02001104 u32 v;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001105 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001106 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001107
Paul Walmsley43b40992010-02-22 22:09:34 -07001108 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001109 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +03001110 return -EINVAL;
1111
1112 /* clocks must be on for this operation */
1113 if (oh->_state != _HWMOD_STATE_ENABLED) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001114 pr_warning("omap_hwmod: %s: reset can only be entered from "
1115 "enabled state\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001116 return -EINVAL;
1117 }
1118
Benoit Cousson96835af2010-09-21 18:57:58 +02001119 /* For some modules, all optionnal clocks need to be enabled as well */
1120 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1121 _enable_optional_clocks(oh);
1122
Paul Walmsleybd361792010-12-14 12:42:35 -07001123 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001124
1125 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001126 ret = _set_softreset(oh, &v);
1127 if (ret)
1128 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001129 _write_sysconfig(v, oh);
1130
Benoit Cousson2cb06812010-09-21 18:57:59 +02001131 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001132 omap_test_timeout((omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001133 oh->class->sysc->syss_offs)
1134 & SYSS_RESETDONE_MASK),
1135 MAX_MODULE_SOFTRESET_WAIT, c);
1136 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001137 omap_test_timeout(!(omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001138 oh->class->sysc->sysc_offs)
1139 & SYSC_TYPE2_SOFTRESET_MASK),
1140 MAX_MODULE_SOFTRESET_WAIT, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001141
Benoît Cousson5365efb2010-09-21 10:34:11 -06001142 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001143 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1144 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001145 else
Benoît Cousson5365efb2010-09-21 10:34:11 -06001146 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001147
1148 /*
1149 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1150 * _wait_target_ready() or _reset()
1151 */
1152
Benoit Cousson96835af2010-09-21 18:57:58 +02001153 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1154
1155dis_opt_clks:
1156 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1157 _disable_optional_clocks(oh);
1158
1159 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001160}
1161
1162/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001163 * _reset - reset an omap_hwmod
1164 * @oh: struct omap_hwmod *
1165 *
1166 * Resets an omap_hwmod @oh. The default software reset mechanism for
1167 * most OMAP IP blocks is triggered via the OCP_SYSCONFIG.SOFTRESET
1168 * bit. However, some hwmods cannot be reset via this method: some
1169 * are not targets and therefore have no OCP header registers to
1170 * access; others (like the IVA) have idiosyncratic reset sequences.
1171 * So for these relatively rare cases, custom reset code can be
1172 * supplied in the struct omap_hwmod_class .reset function pointer.
1173 * Passes along the return value from either _reset() or the custom
1174 * reset function - these must return -EINVAL if the hwmod cannot be
1175 * reset this way or if the hwmod is in the wrong state, -ETIMEDOUT if
1176 * the module did not reset in time, or 0 upon success.
1177 */
1178static int _reset(struct omap_hwmod *oh)
1179{
1180 int ret;
1181
1182 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1183
1184 ret = (oh->class->reset) ? oh->class->reset(oh) : _ocp_softreset(oh);
1185
1186 return ret;
1187}
1188
1189/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001190 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001191 * @oh: struct omap_hwmod *
1192 *
1193 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001194 * register target. Returns -EINVAL if the hwmod is in the wrong
1195 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001196 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001197static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001198{
1199 int r;
1200
1201 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1202 oh->_state != _HWMOD_STATE_IDLE &&
1203 oh->_state != _HWMOD_STATE_DISABLED) {
1204 WARN(1, "omap_hwmod: %s: enabled state can only be entered "
1205 "from initialized, idle, or disabled state\n", oh->name);
1206 return -EINVAL;
1207 }
1208
1209 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1210
Benoît Cousson5365efb2010-09-21 10:34:11 -06001211 /*
1212 * If an IP contains only one HW reset line, then de-assert it in order
1213 * to allow to enable the clocks. Otherwise the PRCM will return
1214 * Intransition status, and the init will failed.
1215 */
1216 if ((oh->_state == _HWMOD_STATE_INITIALIZED ||
1217 oh->_state == _HWMOD_STATE_DISABLED) && oh->rst_lines_cnt == 1)
1218 _deassert_hardreset(oh, oh->rst_lines[0].name);
1219
Paul Walmsley63c85232009-09-03 20:14:03 +03001220 /* XXX mux balls */
1221
1222 _add_initiator_dep(oh, mpu_oh);
1223 _enable_clocks(oh);
1224
Paul Walmsley63c85232009-09-03 20:14:03 +03001225 r = _wait_target_ready(oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001226 if (!r) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001227 oh->_state = _HWMOD_STATE_ENABLED;
1228
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001229 /* Access the sysconfig only if the target is ready */
1230 if (oh->class->sysc) {
1231 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1232 _update_sysc_cache(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001233 _enable_sysc(oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001234 }
1235 } else {
1236 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1237 oh->name, r);
1238 }
1239
Paul Walmsley63c85232009-09-03 20:14:03 +03001240 return r;
1241}
1242
1243/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001244 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001245 * @oh: struct omap_hwmod *
1246 *
1247 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001248 * no further work. Returns -EINVAL if the hwmod is in the wrong
1249 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001250 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001251static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001252{
1253 if (oh->_state != _HWMOD_STATE_ENABLED) {
1254 WARN(1, "omap_hwmod: %s: idle state can only be entered from "
1255 "enabled state\n", oh->name);
1256 return -EINVAL;
1257 }
1258
1259 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1260
Paul Walmsley43b40992010-02-22 22:09:34 -07001261 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001262 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001263 _del_initiator_dep(oh, mpu_oh);
1264 _disable_clocks(oh);
1265
1266 oh->_state = _HWMOD_STATE_IDLE;
1267
1268 return 0;
1269}
1270
1271/**
1272 * _shutdown - shutdown an omap_hwmod
1273 * @oh: struct omap_hwmod *
1274 *
1275 * Shut down an omap_hwmod @oh. This should be called when the driver
1276 * used for the hwmod is removed or unloaded or if the driver is not
1277 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1278 * state or returns 0.
1279 */
1280static int _shutdown(struct omap_hwmod *oh)
1281{
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001282 int ret;
1283 u8 prev_state;
1284
Paul Walmsley63c85232009-09-03 20:14:03 +03001285 if (oh->_state != _HWMOD_STATE_IDLE &&
1286 oh->_state != _HWMOD_STATE_ENABLED) {
1287 WARN(1, "omap_hwmod: %s: disabled state can only be entered "
1288 "from idle, or enabled state\n", oh->name);
1289 return -EINVAL;
1290 }
1291
1292 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
1293
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001294 if (oh->class->pre_shutdown) {
1295 prev_state = oh->_state;
1296 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001297 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001298 ret = oh->class->pre_shutdown(oh);
1299 if (ret) {
1300 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001301 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001302 return ret;
1303 }
1304 }
1305
Paul Walmsley43b40992010-02-22 22:09:34 -07001306 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001307 _shutdown_sysc(oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06001308
Benoît Cousson5365efb2010-09-21 10:34:11 -06001309 /*
1310 * If an IP contains only one HW reset line, then assert it
1311 * before disabling the clocks and shutting down the IP.
1312 */
1313 if (oh->rst_lines_cnt == 1)
1314 _assert_hardreset(oh, oh->rst_lines[0].name);
1315
Benoit Cousson3827f942010-09-21 10:34:08 -06001316 /* clocks and deps are already disabled in idle */
1317 if (oh->_state == _HWMOD_STATE_ENABLED) {
1318 _del_initiator_dep(oh, mpu_oh);
1319 /* XXX what about the other system initiators here? dma, dsp */
1320 _disable_clocks(oh);
1321 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001322 /* XXX Should this code also force-disable the optional clocks? */
1323
1324 /* XXX mux any associated balls to safe mode */
1325
1326 oh->_state = _HWMOD_STATE_DISABLED;
1327
1328 return 0;
1329}
1330
1331/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001332 * _setup - do initial configuration of omap_hwmod
1333 * @oh: struct omap_hwmod *
1334 *
1335 * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001336 * OCP_SYSCONFIG register. Returns -EINVAL if the hwmod is in the
1337 * wrong state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001338 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001339static int _setup(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001340{
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001341 int i, r;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001342 u8 postsetup_state;
Paul Walmsley97d60162010-07-26 16:34:30 -06001343
Paul Walmsley63c85232009-09-03 20:14:03 +03001344 /* Set iclk autoidle mode */
1345 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -06001346 for (i = 0; i < oh->slaves_cnt; i++) {
1347 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +03001348 struct clk *c = os->_clk;
1349
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -06001350 if (!c)
Paul Walmsley63c85232009-09-03 20:14:03 +03001351 continue;
1352
1353 if (os->flags & OCPIF_SWSUP_IDLE) {
1354 /* XXX omap_iclk_deny_idle(c); */
1355 } else {
1356 /* XXX omap_iclk_allow_idle(c); */
1357 clk_enable(c);
1358 }
1359 }
1360 }
1361
1362 oh->_state = _HWMOD_STATE_INITIALIZED;
1363
Benoît Cousson5365efb2010-09-21 10:34:11 -06001364 /*
1365 * In the case of hwmod with hardreset that should not be
1366 * de-assert at boot time, we have to keep the module
1367 * initialized, because we cannot enable it properly with the
1368 * reset asserted. Exit without warning because that behavior is
1369 * expected.
1370 */
1371 if ((oh->flags & HWMOD_INIT_NO_RESET) && oh->rst_lines_cnt == 1)
1372 return 0;
1373
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001374 r = _enable(oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001375 if (r) {
1376 pr_warning("omap_hwmod: %s: cannot be enabled (%d)\n",
1377 oh->name, oh->_state);
1378 return 0;
1379 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001380
Paul Walmsleyb835d012009-12-08 16:34:14 -07001381 if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001382 _reset(oh);
1383
Paul Walmsleyb835d012009-12-08 16:34:14 -07001384 /*
Benoit Cousson76e55892010-09-21 10:34:11 -06001385 * OCP_SYSCONFIG bits need to be reprogrammed after a softreset.
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001386 * The _enable() function should be split to
Benoit Cousson76e55892010-09-21 10:34:11 -06001387 * avoid the rewrite of the OCP_SYSCONFIG register.
Paul Walmsleyb835d012009-12-08 16:34:14 -07001388 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001389 if (oh->class->sysc) {
Paul Walmsleyb835d012009-12-08 16:34:14 -07001390 _update_sysc_cache(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001391 _enable_sysc(oh);
Paul Walmsleyb835d012009-12-08 16:34:14 -07001392 }
1393 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001394
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001395 postsetup_state = oh->_postsetup_state;
1396 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
1397 postsetup_state = _HWMOD_STATE_ENABLED;
1398
1399 /*
1400 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
1401 * it should be set by the core code as a runtime flag during startup
1402 */
1403 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
1404 (postsetup_state == _HWMOD_STATE_IDLE))
1405 postsetup_state = _HWMOD_STATE_ENABLED;
1406
1407 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001408 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001409 else if (postsetup_state == _HWMOD_STATE_DISABLED)
1410 _shutdown(oh);
1411 else if (postsetup_state != _HWMOD_STATE_ENABLED)
1412 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
1413 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03001414
1415 return 0;
1416}
1417
Benoit Cousson0102b622010-12-21 21:31:27 -07001418/**
1419 * _register - register a struct omap_hwmod
1420 * @oh: struct omap_hwmod *
1421 *
1422 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
1423 * already has been registered by the same name; -EINVAL if the
1424 * omap_hwmod is in the wrong state, if @oh is NULL, if the
1425 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
1426 * name, or if the omap_hwmod's class is missing a name; or 0 upon
1427 * success.
1428 *
1429 * XXX The data should be copied into bootmem, so the original data
1430 * should be marked __initdata and freed after init. This would allow
1431 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
1432 * that the copy process would be relatively complex due to the large number
1433 * of substructures.
1434 */
Benoit Cousson01592df2010-12-21 21:31:28 -07001435static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07001436{
1437 int ret, ms_id;
1438
1439 if (!oh || !oh->name || !oh->class || !oh->class->name ||
1440 (oh->_state != _HWMOD_STATE_UNKNOWN))
1441 return -EINVAL;
1442
Benoit Cousson0102b622010-12-21 21:31:27 -07001443 pr_debug("omap_hwmod: %s: registering\n", oh->name);
1444
Benoit Coussonce35b242010-12-21 21:31:28 -07001445 if (_lookup(oh->name))
1446 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07001447
1448 ms_id = _find_mpu_port_index(oh);
1449 if (!IS_ERR_VALUE(ms_id)) {
1450 oh->_mpu_port_index = ms_id;
1451 oh->_mpu_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
1452 } else {
1453 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1454 }
1455
1456 list_add_tail(&oh->node, &omap_hwmod_list);
1457
1458 spin_lock_init(&oh->_lock);
1459
1460 oh->_state = _HWMOD_STATE_REGISTERED;
1461
1462 ret = 0;
1463
Benoit Cousson0102b622010-12-21 21:31:27 -07001464 return ret;
1465}
Paul Walmsley63c85232009-09-03 20:14:03 +03001466
1467
1468/* Public functions */
1469
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001470u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03001471{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001472 if (oh->flags & HWMOD_16BIT_REG)
1473 return __raw_readw(oh->_mpu_rt_va + reg_offs);
1474 else
1475 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001476}
1477
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001478void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03001479{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001480 if (oh->flags & HWMOD_16BIT_REG)
1481 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
1482 else
1483 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001484}
1485
Paul Walmsley887adea2010-07-26 16:34:33 -06001486/**
1487 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
1488 * @oh: struct omap_hwmod *
1489 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
1490 *
1491 * Sets the IP block's OCP slave idlemode in hardware, and updates our
1492 * local copy. Intended to be used by drivers that have some erratum
1493 * that requires direct manipulation of the SIDLEMODE bits. Returns
1494 * -EINVAL if @oh is null, or passes along the return value from
1495 * _set_slave_idlemode().
1496 *
1497 * XXX Does this function have any current users? If not, we should
1498 * remove it; it is better to let the rest of the hwmod code handle this.
1499 * Any users of this function should be scrutinized carefully.
1500 */
Kevin Hilman46273e62010-01-26 20:13:03 -07001501int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
1502{
1503 u32 v;
1504 int retval = 0;
1505
1506 if (!oh)
1507 return -EINVAL;
1508
1509 v = oh->_sysc_cache;
1510
1511 retval = _set_slave_idlemode(oh, idlemode, &v);
1512 if (!retval)
1513 _write_sysconfig(v, oh);
1514
1515 return retval;
1516}
1517
Paul Walmsley63c85232009-09-03 20:14:03 +03001518/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001519 * omap_hwmod_lookup - look up a registered omap_hwmod by name
1520 * @name: name of the omap_hwmod to look up
1521 *
1522 * Given a @name of an omap_hwmod, return a pointer to the registered
1523 * struct omap_hwmod *, or NULL upon error.
1524 */
1525struct omap_hwmod *omap_hwmod_lookup(const char *name)
1526{
1527 struct omap_hwmod *oh;
1528
1529 if (!name)
1530 return NULL;
1531
Paul Walmsley63c85232009-09-03 20:14:03 +03001532 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001533
1534 return oh;
1535}
1536
1537/**
1538 * omap_hwmod_for_each - call function for each registered omap_hwmod
1539 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06001540 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03001541 *
1542 * Call @fn for each registered omap_hwmod, passing @data to each
1543 * function. @fn must return 0 for success or any other value for
1544 * failure. If @fn returns non-zero, the iteration across omap_hwmods
1545 * will stop and the non-zero return value will be passed to the
1546 * caller of omap_hwmod_for_each(). @fn is called with
1547 * omap_hwmod_for_each() held.
1548 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001549int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
1550 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001551{
1552 struct omap_hwmod *temp_oh;
1553 int ret;
1554
1555 if (!fn)
1556 return -EINVAL;
1557
Paul Walmsley63c85232009-09-03 20:14:03 +03001558 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06001559 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03001560 if (ret)
1561 break;
1562 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001563
1564 return ret;
1565}
1566
1567
1568/**
1569 * omap_hwmod_init - init omap_hwmod code and register hwmods
1570 * @ohs: pointer to an array of omap_hwmods to register
1571 *
1572 * Intended to be called early in boot before the clock framework is
1573 * initialized. If @ohs is not null, will register all omap_hwmods
1574 * listed in @ohs that are valid for this chip. Returns -EINVAL if
1575 * omap_hwmod_init() has already been called or 0 otherwise.
1576 */
Benoit Cousson01592df2010-12-21 21:31:28 -07001577int __init omap_hwmod_init(struct omap_hwmod **ohs)
Paul Walmsley63c85232009-09-03 20:14:03 +03001578{
1579 struct omap_hwmod *oh;
1580 int r;
1581
1582 if (inited)
1583 return -EINVAL;
1584
1585 inited = 1;
1586
1587 if (!ohs)
1588 return 0;
1589
1590 oh = *ohs;
1591 while (oh) {
1592 if (omap_chip_is(oh->omap_chip)) {
Benoit Cousson0102b622010-12-21 21:31:27 -07001593 r = _register(oh);
1594 WARN(r, "omap_hwmod: %s: _register returned "
Paul Walmsley63c85232009-09-03 20:14:03 +03001595 "%d\n", oh->name, r);
1596 }
1597 oh = *++ohs;
1598 }
1599
1600 return 0;
1601}
1602
1603/**
1604 * omap_hwmod_late_init - do some post-clock framework initialization
1605 *
1606 * Must be called after omap2_clk_init(). Resolves the struct clk names
1607 * to struct clk pointers for each registered omap_hwmod. Also calls
1608 * _setup() on each hwmod. Returns 0.
1609 */
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001610int omap_hwmod_late_init(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03001611{
1612 int r;
1613
1614 /* XXX check return value */
Paul Walmsley97d60162010-07-26 16:34:30 -06001615 r = omap_hwmod_for_each(_init_clocks, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03001616 WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
1617
1618 mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
1619 WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
1620 MPU_INITIATOR_NAME);
1621
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001622 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03001623
1624 return 0;
1625}
1626
1627/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001628 * omap_hwmod_enable - enable an omap_hwmod
1629 * @oh: struct omap_hwmod *
1630 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001631 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03001632 * Returns -EINVAL on error or passes along the return value from _enable().
1633 */
1634int omap_hwmod_enable(struct omap_hwmod *oh)
1635{
1636 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001637 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001638
1639 if (!oh)
1640 return -EINVAL;
1641
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001642 spin_lock_irqsave(&oh->_lock, flags);
1643 r = _enable(oh);
1644 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001645
1646 return r;
1647}
1648
1649/**
1650 * omap_hwmod_idle - idle an omap_hwmod
1651 * @oh: struct omap_hwmod *
1652 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001653 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03001654 * Returns -EINVAL on error or passes along the return value from _idle().
1655 */
1656int omap_hwmod_idle(struct omap_hwmod *oh)
1657{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001658 unsigned long flags;
1659
Paul Walmsley63c85232009-09-03 20:14:03 +03001660 if (!oh)
1661 return -EINVAL;
1662
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001663 spin_lock_irqsave(&oh->_lock, flags);
1664 _idle(oh);
1665 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001666
1667 return 0;
1668}
1669
1670/**
1671 * omap_hwmod_shutdown - shutdown an omap_hwmod
1672 * @oh: struct omap_hwmod *
1673 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001674 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03001675 * omap_device_shutdown(). Returns -EINVAL on error or passes along
1676 * the return value from _shutdown().
1677 */
1678int omap_hwmod_shutdown(struct omap_hwmod *oh)
1679{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001680 unsigned long flags;
1681
Paul Walmsley63c85232009-09-03 20:14:03 +03001682 if (!oh)
1683 return -EINVAL;
1684
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001685 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001686 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001687 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001688
1689 return 0;
1690}
1691
1692/**
1693 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
1694 * @oh: struct omap_hwmod *oh
1695 *
1696 * Intended to be called by the omap_device code.
1697 */
1698int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
1699{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001700 unsigned long flags;
1701
1702 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001703 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001704 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001705
1706 return 0;
1707}
1708
1709/**
1710 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
1711 * @oh: struct omap_hwmod *oh
1712 *
1713 * Intended to be called by the omap_device code.
1714 */
1715int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
1716{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001717 unsigned long flags;
1718
1719 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001720 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001721 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001722
1723 return 0;
1724}
1725
1726/**
1727 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
1728 * @oh: struct omap_hwmod *oh
1729 *
1730 * Intended to be called by drivers and core code when all posted
1731 * writes to a device must complete before continuing further
1732 * execution (for example, after clearing some device IRQSTATUS
1733 * register bits)
1734 *
1735 * XXX what about targets with multiple OCP threads?
1736 */
1737void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
1738{
1739 BUG_ON(!oh);
1740
Paul Walmsley43b40992010-02-22 22:09:34 -07001741 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001742 WARN(1, "omap_device: %s: OCP barrier impossible due to "
1743 "device configuration\n", oh->name);
1744 return;
1745 }
1746
1747 /*
1748 * Forces posted writes to complete on the OCP thread handling
1749 * register writes
1750 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001751 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001752}
1753
1754/**
1755 * omap_hwmod_reset - reset the hwmod
1756 * @oh: struct omap_hwmod *
1757 *
1758 * Under some conditions, a driver may wish to reset the entire device.
1759 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06001760 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03001761 */
1762int omap_hwmod_reset(struct omap_hwmod *oh)
1763{
1764 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001765 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001766
Liam Girdwood9b579112010-09-21 10:34:09 -06001767 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001768 return -EINVAL;
1769
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001770 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001771 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001772 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001773
1774 return r;
1775}
1776
1777/**
1778 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
1779 * @oh: struct omap_hwmod *
1780 * @res: pointer to the first element of an array of struct resource to fill
1781 *
1782 * Count the number of struct resource array elements necessary to
1783 * contain omap_hwmod @oh resources. Intended to be called by code
1784 * that registers omap_devices. Intended to be used to determine the
1785 * size of a dynamically-allocated struct resource array, before
1786 * calling omap_hwmod_fill_resources(). Returns the number of struct
1787 * resource array elements needed.
1788 *
1789 * XXX This code is not optimized. It could attempt to merge adjacent
1790 * resource IDs.
1791 *
1792 */
1793int omap_hwmod_count_resources(struct omap_hwmod *oh)
1794{
1795 int ret, i;
1796
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06001797 ret = oh->mpu_irqs_cnt + oh->sdma_reqs_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03001798
1799 for (i = 0; i < oh->slaves_cnt; i++)
Benoit Cousson682fdc92010-05-20 12:31:09 -06001800 ret += oh->slaves[i]->addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03001801
1802 return ret;
1803}
1804
1805/**
1806 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
1807 * @oh: struct omap_hwmod *
1808 * @res: pointer to the first element of an array of struct resource to fill
1809 *
1810 * Fill the struct resource array @res with resource data from the
1811 * omap_hwmod @oh. Intended to be called by code that registers
1812 * omap_devices. See also omap_hwmod_count_resources(). Returns the
1813 * number of array elements filled.
1814 */
1815int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
1816{
1817 int i, j;
1818 int r = 0;
1819
1820 /* For each IRQ, DMA, memory area, fill in array.*/
1821
1822 for (i = 0; i < oh->mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07001823 (res + r)->name = (oh->mpu_irqs + i)->name;
1824 (res + r)->start = (oh->mpu_irqs + i)->irq;
1825 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03001826 (res + r)->flags = IORESOURCE_IRQ;
1827 r++;
1828 }
1829
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06001830 for (i = 0; i < oh->sdma_reqs_cnt; i++) {
1831 (res + r)->name = (oh->sdma_reqs + i)->name;
1832 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
1833 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03001834 (res + r)->flags = IORESOURCE_DMA;
1835 r++;
1836 }
1837
1838 for (i = 0; i < oh->slaves_cnt; i++) {
1839 struct omap_hwmod_ocp_if *os;
1840
Benoit Cousson682fdc92010-05-20 12:31:09 -06001841 os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +03001842
1843 for (j = 0; j < os->addr_cnt; j++) {
1844 (res + r)->start = (os->addr + j)->pa_start;
1845 (res + r)->end = (os->addr + j)->pa_end;
1846 (res + r)->flags = IORESOURCE_MEM;
1847 r++;
1848 }
1849 }
1850
1851 return r;
1852}
1853
1854/**
1855 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
1856 * @oh: struct omap_hwmod *
1857 *
1858 * Return the powerdomain pointer associated with the OMAP module
1859 * @oh's main clock. If @oh does not have a main clk, return the
1860 * powerdomain associated with the interface clock associated with the
1861 * module's MPU port. (XXX Perhaps this should use the SDMA port
1862 * instead?) Returns NULL on error, or a struct powerdomain * on
1863 * success.
1864 */
1865struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
1866{
1867 struct clk *c;
1868
1869 if (!oh)
1870 return NULL;
1871
1872 if (oh->_clk) {
1873 c = oh->_clk;
1874 } else {
1875 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1876 return NULL;
1877 c = oh->slaves[oh->_mpu_port_index]->_clk;
1878 }
1879
Thara Gopinathd5647c12010-03-31 04:16:29 -06001880 if (!c->clkdm)
1881 return NULL;
1882
Paul Walmsley63c85232009-09-03 20:14:03 +03001883 return c->clkdm->pwrdm.ptr;
1884
1885}
1886
1887/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06001888 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
1889 * @oh: struct omap_hwmod *
1890 *
1891 * Returns the virtual address corresponding to the beginning of the
1892 * module's register target, in the address range that is intended to
1893 * be used by the MPU. Returns the virtual address upon success or NULL
1894 * upon error.
1895 */
1896void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
1897{
1898 if (!oh)
1899 return NULL;
1900
1901 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1902 return NULL;
1903
1904 if (oh->_state == _HWMOD_STATE_UNKNOWN)
1905 return NULL;
1906
1907 return oh->_mpu_rt_va;
1908}
1909
1910/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001911 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
1912 * @oh: struct omap_hwmod *
1913 * @init_oh: struct omap_hwmod * (initiator)
1914 *
1915 * Add a sleep dependency between the initiator @init_oh and @oh.
1916 * Intended to be called by DSP/Bridge code via platform_data for the
1917 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1918 * code needs to add/del initiator dependencies dynamically
1919 * before/after accessing a device. Returns the return value from
1920 * _add_initiator_dep().
1921 *
1922 * XXX Keep a usecount in the clockdomain code
1923 */
1924int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
1925 struct omap_hwmod *init_oh)
1926{
1927 return _add_initiator_dep(oh, init_oh);
1928}
1929
1930/*
1931 * XXX what about functions for drivers to save/restore ocp_sysconfig
1932 * for context save/restore operations?
1933 */
1934
1935/**
1936 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
1937 * @oh: struct omap_hwmod *
1938 * @init_oh: struct omap_hwmod * (initiator)
1939 *
1940 * Remove a sleep dependency between the initiator @init_oh and @oh.
1941 * Intended to be called by DSP/Bridge code via platform_data for the
1942 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1943 * code needs to add/del initiator dependencies dynamically
1944 * before/after accessing a device. Returns the return value from
1945 * _del_initiator_dep().
1946 *
1947 * XXX Keep a usecount in the clockdomain code
1948 */
1949int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
1950 struct omap_hwmod *init_oh)
1951{
1952 return _del_initiator_dep(oh, init_oh);
1953}
1954
1955/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001956 * omap_hwmod_enable_wakeup - allow device to wake up the system
1957 * @oh: struct omap_hwmod *
1958 *
1959 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
1960 * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
1961 * registers to cause the PRCM to receive wakeup events from the
1962 * module. Does not set any wakeup routing registers beyond this
1963 * point - if the module is to wake up any other module or subsystem,
1964 * that must be set separately. Called by omap_device code. Returns
1965 * -EINVAL on error or 0 upon success.
1966 */
1967int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
1968{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001969 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07001970 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001971
Paul Walmsley43b40992010-02-22 22:09:34 -07001972 if (!oh->class->sysc ||
1973 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
Paul Walmsley63c85232009-09-03 20:14:03 +03001974 return -EINVAL;
1975
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001976 spin_lock_irqsave(&oh->_lock, flags);
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07001977 v = oh->_sysc_cache;
1978 _enable_wakeup(oh, &v);
1979 _write_sysconfig(v, oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001980 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001981
1982 return 0;
1983}
1984
1985/**
1986 * omap_hwmod_disable_wakeup - prevent device from waking the system
1987 * @oh: struct omap_hwmod *
1988 *
1989 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
1990 * from sending wakeups to the PRCM. Eventually this should clear
1991 * PRCM wakeup registers to cause the PRCM to ignore wakeup events
1992 * from the module. Does not set any wakeup routing registers beyond
1993 * this point - if the module is to wake up any other module or
1994 * subsystem, that must be set separately. Called by omap_device
1995 * code. Returns -EINVAL on error or 0 upon success.
1996 */
1997int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
1998{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001999 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002000 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002001
Paul Walmsley43b40992010-02-22 22:09:34 -07002002 if (!oh->class->sysc ||
2003 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
Paul Walmsley63c85232009-09-03 20:14:03 +03002004 return -EINVAL;
2005
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002006 spin_lock_irqsave(&oh->_lock, flags);
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002007 v = oh->_sysc_cache;
2008 _disable_wakeup(oh, &v);
2009 _write_sysconfig(v, oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002010 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002011
2012 return 0;
2013}
Paul Walmsley43b40992010-02-22 22:09:34 -07002014
2015/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002016 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
2017 * contained in the hwmod module.
2018 * @oh: struct omap_hwmod *
2019 * @name: name of the reset line to lookup and assert
2020 *
2021 * Some IP like dsp, ipu or iva contain processor that require
2022 * an HW reset line to be assert / deassert in order to enable fully
2023 * the IP. Returns -EINVAL if @oh is null or if the operation is not
2024 * yet supported on this OMAP; otherwise, passes along the return value
2025 * from _assert_hardreset().
2026 */
2027int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
2028{
2029 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002030 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002031
2032 if (!oh)
2033 return -EINVAL;
2034
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002035 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002036 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002037 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002038
2039 return ret;
2040}
2041
2042/**
2043 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
2044 * contained in the hwmod module.
2045 * @oh: struct omap_hwmod *
2046 * @name: name of the reset line to look up and deassert
2047 *
2048 * Some IP like dsp, ipu or iva contain processor that require
2049 * an HW reset line to be assert / deassert in order to enable fully
2050 * the IP. Returns -EINVAL if @oh is null or if the operation is not
2051 * yet supported on this OMAP; otherwise, passes along the return value
2052 * from _deassert_hardreset().
2053 */
2054int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
2055{
2056 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002057 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002058
2059 if (!oh)
2060 return -EINVAL;
2061
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002062 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002063 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002064 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002065
2066 return ret;
2067}
2068
2069/**
2070 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
2071 * contained in the hwmod module
2072 * @oh: struct omap_hwmod *
2073 * @name: name of the reset line to look up and read
2074 *
2075 * Return the current state of the hwmod @oh's reset line named @name:
2076 * returns -EINVAL upon parameter error or if this operation
2077 * is unsupported on the current OMAP; otherwise, passes along the return
2078 * value from _read_hardreset().
2079 */
2080int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
2081{
2082 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002083 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002084
2085 if (!oh)
2086 return -EINVAL;
2087
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002088 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002089 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002090 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002091
2092 return ret;
2093}
2094
2095
2096/**
Paul Walmsley43b40992010-02-22 22:09:34 -07002097 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
2098 * @classname: struct omap_hwmod_class name to search for
2099 * @fn: callback function pointer to call for each hwmod in class @classname
2100 * @user: arbitrary context data to pass to the callback function
2101 *
Benoit Coussonce35b242010-12-21 21:31:28 -07002102 * For each omap_hwmod of class @classname, call @fn.
2103 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07002104 * zero, the iterator is terminated, and the callback function's return
2105 * value is passed back to the caller. Returns 0 upon success, -EINVAL
2106 * if @classname or @fn are NULL, or passes back the error code from @fn.
2107 */
2108int omap_hwmod_for_each_by_class(const char *classname,
2109 int (*fn)(struct omap_hwmod *oh,
2110 void *user),
2111 void *user)
2112{
2113 struct omap_hwmod *temp_oh;
2114 int ret = 0;
2115
2116 if (!classname || !fn)
2117 return -EINVAL;
2118
2119 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
2120 __func__, classname);
2121
Paul Walmsley43b40992010-02-22 22:09:34 -07002122 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
2123 if (!strcmp(temp_oh->class->name, classname)) {
2124 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
2125 __func__, temp_oh->name);
2126 ret = (*fn)(temp_oh, user);
2127 if (ret)
2128 break;
2129 }
2130 }
2131
Paul Walmsley43b40992010-02-22 22:09:34 -07002132 if (ret)
2133 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
2134 __func__, ret);
2135
2136 return ret;
2137}
2138
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002139/**
2140 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
2141 * @oh: struct omap_hwmod *
2142 * @state: state that _setup() should leave the hwmod in
2143 *
2144 * Sets the hwmod state that @oh will enter at the end of _setup() (called by
2145 * omap_hwmod_late_init()). Only valid to call between calls to
2146 * omap_hwmod_init() and omap_hwmod_late_init(). Returns 0 upon success or
2147 * -EINVAL if there is a problem with the arguments or if the hwmod is
2148 * in the wrong state.
2149 */
2150int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
2151{
2152 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002153 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002154
2155 if (!oh)
2156 return -EINVAL;
2157
2158 if (state != _HWMOD_STATE_DISABLED &&
2159 state != _HWMOD_STATE_ENABLED &&
2160 state != _HWMOD_STATE_IDLE)
2161 return -EINVAL;
2162
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002163 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002164
2165 if (oh->_state != _HWMOD_STATE_REGISTERED) {
2166 ret = -EINVAL;
2167 goto ohsps_unlock;
2168 }
2169
2170 oh->_postsetup_state = state;
2171 ret = 0;
2172
2173ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002174 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002175
2176 return ret;
2177}