blob: 005264779f8dd7a26d466f002c6369a0099cdac0 [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:
Paul Walmsley63c85232009-09-03 20:14:03 +0300119 * - handle IO mapping
120 * - bus throughput & module latency measurement code
121 *
122 * XXX add tests at the beginning of each function to ensure the hwmod is
123 * in the appropriate state
124 * XXX error return values should be checked to ensure that they are
125 * appropriate
126 */
127#undef DEBUG
128
129#include <linux/kernel.h>
130#include <linux/errno.h>
131#include <linux/io.h>
132#include <linux/clk.h>
133#include <linux/delay.h>
134#include <linux/err.h>
135#include <linux/list.h>
136#include <linux/mutex.h>
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -0700137#include <linux/spinlock.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300138
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -0700139#include <plat/common.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -0700140#include <plat/cpu.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -0700141#include "clockdomain.h"
Paul Walmsley72e06d02010-12-21 21:05:16 -0700142#include "powerdomain.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700143#include <plat/clock.h>
144#include <plat/omap_hwmod.h>
Benoît Cousson5365efb2010-09-21 10:34:11 -0600145#include <plat/prcm.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300146
Paul Walmsley59fb6592010-12-21 15:30:55 -0700147#include "cm2xxx_3xxx.h"
148#include "cm44xx.h"
149#include "prm2xxx_3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -0700150#include "prm44xx.h"
Tony Lindgren8d9af882010-12-22 18:42:35 -0800151#include "mux.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;
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/**
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 ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700396 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
397 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300398 return -EINVAL;
399
Paul Walmsley43b40992010-02-22 22:09:34 -0700400 if (!oh->class->sysc->sysc_fields) {
401 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700402 return -EINVAL;
403 }
404
Paul Walmsley43b40992010-02-22 22:09:34 -0700405 wakeup_mask = (0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700406
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700407 *v |= wakeup_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300408
Benoit Cousson86009eb2010-12-21 21:31:28 -0700409 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
410 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
411
Paul Walmsley63c85232009-09-03 20:14:03 +0300412 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
413
414 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
415
416 return 0;
417}
418
419/**
420 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
421 * @oh: struct omap_hwmod *
422 *
423 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
424 * upon error or 0 upon success.
425 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700426static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300427{
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700428 u32 wakeup_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300429
Paul Walmsley43b40992010-02-22 22:09:34 -0700430 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700431 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
432 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300433 return -EINVAL;
434
Paul Walmsley43b40992010-02-22 22:09:34 -0700435 if (!oh->class->sysc->sysc_fields) {
436 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700437 return -EINVAL;
438 }
439
Paul Walmsley43b40992010-02-22 22:09:34 -0700440 wakeup_mask = (0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700441
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700442 *v &= ~wakeup_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300443
Benoit Cousson86009eb2010-12-21 21:31:28 -0700444 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
445 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
446
Paul Walmsley63c85232009-09-03 20:14:03 +0300447 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
448
449 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
450
451 return 0;
452}
453
454/**
455 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
456 * @oh: struct omap_hwmod *
457 *
458 * Prevent the hardware module @oh from entering idle while the
459 * hardare module initiator @init_oh is active. Useful when a module
460 * will be accessed by a particular initiator (e.g., if a module will
461 * be accessed by the IVA, there should be a sleepdep between the IVA
462 * initiator and the module). Only applies to modules in smart-idle
463 * mode. Returns -EINVAL upon error or passes along
Paul Walmsley55ed9692010-01-26 20:12:59 -0700464 * clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300465 */
466static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
467{
468 if (!oh->_clk)
469 return -EINVAL;
470
Paul Walmsley55ed9692010-01-26 20:12:59 -0700471 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300472}
473
474/**
475 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
476 * @oh: struct omap_hwmod *
477 *
478 * Allow the hardware module @oh to enter idle while the hardare
479 * module initiator @init_oh is active. Useful when a module will not
480 * be accessed by a particular initiator (e.g., if a module will not
481 * be accessed by the IVA, there should be no sleepdep between the IVA
482 * initiator and the module). Only applies to modules in smart-idle
483 * mode. Returns -EINVAL upon error or passes along
Paul Walmsley55ed9692010-01-26 20:12:59 -0700484 * clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300485 */
486static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
487{
488 if (!oh->_clk)
489 return -EINVAL;
490
Paul Walmsley55ed9692010-01-26 20:12:59 -0700491 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300492}
493
494/**
495 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
496 * @oh: struct omap_hwmod *
497 *
498 * Called from _init_clocks(). Populates the @oh _clk (main
499 * functional clock pointer) if a main_clk is present. Returns 0 on
500 * success or -EINVAL on error.
501 */
502static int _init_main_clk(struct omap_hwmod *oh)
503{
Paul Walmsley63c85232009-09-03 20:14:03 +0300504 int ret = 0;
505
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700506 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300507 return 0;
508
Benoit Cousson63403382010-05-20 12:31:10 -0600509 oh->_clk = omap_clk_get_by_name(oh->main_clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600510 if (!oh->_clk) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600511 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
512 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600513 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600514 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300515
Benoit Cousson63403382010-05-20 12:31:10 -0600516 if (!oh->_clk->clkdm)
517 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
518 oh->main_clk, oh->_clk->name);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700519
Paul Walmsley63c85232009-09-03 20:14:03 +0300520 return ret;
521}
522
523/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600524 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300525 * @oh: struct omap_hwmod *
526 *
527 * Called from _init_clocks(). Populates the @oh OCP slave interface
528 * clock pointers. Returns 0 on success or -EINVAL on error.
529 */
530static int _init_interface_clks(struct omap_hwmod *oh)
531{
Paul Walmsley63c85232009-09-03 20:14:03 +0300532 struct clk *c;
533 int i;
534 int ret = 0;
535
536 if (oh->slaves_cnt == 0)
537 return 0;
538
Benoit Cousson682fdc92010-05-20 12:31:09 -0600539 for (i = 0; i < oh->slaves_cnt; i++) {
540 struct omap_hwmod_ocp_if *os = oh->slaves[i];
541
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700542 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300543 continue;
544
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700545 c = omap_clk_get_by_name(os->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600546 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600547 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
548 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300549 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600550 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300551 os->_clk = c;
552 }
553
554 return ret;
555}
556
557/**
558 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
559 * @oh: struct omap_hwmod *
560 *
561 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
562 * clock pointers. Returns 0 on success or -EINVAL on error.
563 */
564static int _init_opt_clks(struct omap_hwmod *oh)
565{
566 struct omap_hwmod_opt_clk *oc;
567 struct clk *c;
568 int i;
569 int ret = 0;
570
571 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700572 c = omap_clk_get_by_name(oc->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600573 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600574 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
575 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300576 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600577 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300578 oc->_clk = c;
579 }
580
581 return ret;
582}
583
584/**
585 * _enable_clocks - enable hwmod main clock and interface clocks
586 * @oh: struct omap_hwmod *
587 *
588 * Enables all clocks necessary for register reads and writes to succeed
589 * on the hwmod @oh. Returns 0.
590 */
591static int _enable_clocks(struct omap_hwmod *oh)
592{
Paul Walmsley63c85232009-09-03 20:14:03 +0300593 int i;
594
595 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
596
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600597 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300598 clk_enable(oh->_clk);
599
600 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -0600601 for (i = 0; i < oh->slaves_cnt; i++) {
602 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +0300603 struct clk *c = os->_clk;
604
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600605 if (c && (os->flags & OCPIF_SWSUP_IDLE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300606 clk_enable(c);
607 }
608 }
609
610 /* The opt clocks are controlled by the device driver. */
611
612 return 0;
613}
614
615/**
616 * _disable_clocks - disable hwmod main clock and interface clocks
617 * @oh: struct omap_hwmod *
618 *
619 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
620 */
621static int _disable_clocks(struct omap_hwmod *oh)
622{
Paul Walmsley63c85232009-09-03 20:14:03 +0300623 int i;
624
625 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
626
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600627 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300628 clk_disable(oh->_clk);
629
630 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -0600631 for (i = 0; i < oh->slaves_cnt; i++) {
632 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +0300633 struct clk *c = os->_clk;
634
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600635 if (c && (os->flags & OCPIF_SWSUP_IDLE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300636 clk_disable(c);
637 }
638 }
639
640 /* The opt clocks are controlled by the device driver. */
641
642 return 0;
643}
644
Benoit Cousson96835af2010-09-21 18:57:58 +0200645static void _enable_optional_clocks(struct omap_hwmod *oh)
646{
647 struct omap_hwmod_opt_clk *oc;
648 int i;
649
650 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
651
652 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
653 if (oc->_clk) {
654 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
655 oc->_clk->name);
656 clk_enable(oc->_clk);
657 }
658}
659
660static void _disable_optional_clocks(struct omap_hwmod *oh)
661{
662 struct omap_hwmod_opt_clk *oc;
663 int i;
664
665 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
666
667 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
668 if (oc->_clk) {
669 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
670 oc->_clk->name);
671 clk_disable(oc->_clk);
672 }
673}
674
Paul Walmsley63c85232009-09-03 20:14:03 +0300675/**
676 * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
677 * @oh: struct omap_hwmod *
678 *
679 * Returns the array index of the OCP slave port that the MPU
680 * addresses the device on, or -EINVAL upon error or not found.
681 */
Benoit Cousson01592df2010-12-21 21:31:28 -0700682static int __init _find_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300683{
Paul Walmsley63c85232009-09-03 20:14:03 +0300684 int i;
685 int found = 0;
686
687 if (!oh || oh->slaves_cnt == 0)
688 return -EINVAL;
689
Benoit Cousson682fdc92010-05-20 12:31:09 -0600690 for (i = 0; i < oh->slaves_cnt; i++) {
691 struct omap_hwmod_ocp_if *os = oh->slaves[i];
692
Paul Walmsley63c85232009-09-03 20:14:03 +0300693 if (os->user & OCP_USER_MPU) {
694 found = 1;
695 break;
696 }
697 }
698
699 if (found)
700 pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
701 oh->name, i);
702 else
703 pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
704 oh->name);
705
706 return (found) ? i : -EINVAL;
707}
708
709/**
710 * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
711 * @oh: struct omap_hwmod *
712 *
713 * Return the virtual address of the base of the register target of
714 * device @oh, or NULL on error.
715 */
Benoit Cousson01592df2010-12-21 21:31:28 -0700716static void __iomem * __init _find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
Paul Walmsley63c85232009-09-03 20:14:03 +0300717{
718 struct omap_hwmod_ocp_if *os;
719 struct omap_hwmod_addr_space *mem;
720 int i;
721 int found = 0;
Tony Lindgren986a13f2009-10-19 15:25:22 -0700722 void __iomem *va_start;
Paul Walmsley63c85232009-09-03 20:14:03 +0300723
724 if (!oh || oh->slaves_cnt == 0)
725 return NULL;
726
Benoit Cousson682fdc92010-05-20 12:31:09 -0600727 os = oh->slaves[index];
Paul Walmsley63c85232009-09-03 20:14:03 +0300728
729 for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
730 if (mem->flags & ADDR_TYPE_RT) {
731 found = 1;
732 break;
733 }
734 }
735
Tony Lindgren986a13f2009-10-19 15:25:22 -0700736 if (found) {
737 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
738 if (!va_start) {
739 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
740 return NULL;
741 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300742 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
Tony Lindgren986a13f2009-10-19 15:25:22 -0700743 oh->name, va_start);
744 } else {
Paul Walmsley63c85232009-09-03 20:14:03 +0300745 pr_debug("omap_hwmod: %s: no MPU register target found\n",
746 oh->name);
Tony Lindgren986a13f2009-10-19 15:25:22 -0700747 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300748
Tony Lindgren986a13f2009-10-19 15:25:22 -0700749 return (found) ? va_start : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +0300750}
751
752/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600753 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +0300754 * @oh: struct omap_hwmod *
755 *
756 * If module is marked as SWSUP_SIDLE, force the module out of slave
757 * idle; otherwise, configure it for smart-idle. If module is marked
758 * as SWSUP_MSUSPEND, force the module out of master standby;
759 * otherwise, configure it for smart-standby. No return value.
760 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600761static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300762{
Paul Walmsley43b40992010-02-22 22:09:34 -0700763 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +0300764 u32 v;
765
Paul Walmsley43b40992010-02-22 22:09:34 -0700766 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +0300767 return;
768
769 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -0700770 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +0300771
Paul Walmsley43b40992010-02-22 22:09:34 -0700772 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +0300773 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
774 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
775 _set_slave_idlemode(oh, idlemode, &v);
776 }
777
Paul Walmsley43b40992010-02-22 22:09:34 -0700778 if (sf & SYSC_HAS_MIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +0300779 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
780 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
781 _set_master_standbymode(oh, idlemode, &v);
782 }
783
Paul Walmsleya16b1f72009-12-08 16:34:17 -0700784 /*
785 * XXX The clock framework should handle this, by
786 * calling into this code. But this must wait until the
787 * clock structures are tagged with omap_hwmod entries
788 */
Paul Walmsley43b40992010-02-22 22:09:34 -0700789 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
790 (sf & SYSC_HAS_CLOCKACTIVITY))
791 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +0300792
Rajendra Nayak9980ce52010-09-21 19:58:30 +0530793 /* If slave is in SMARTIDLE, also enable wakeup */
794 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700795 _enable_wakeup(oh, &v);
796
797 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -0600798
799 /*
800 * Set the autoidle bit only after setting the smartidle bit
801 * Setting this will not have any impact on the other modules.
802 */
803 if (sf & SYSC_HAS_AUTOIDLE) {
804 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
805 0 : 1;
806 _set_module_autoidle(oh, idlemode, &v);
807 _write_sysconfig(v, oh);
808 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300809}
810
811/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600812 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +0300813 * @oh: struct omap_hwmod *
814 *
815 * If module is marked as SWSUP_SIDLE, force the module into slave
816 * idle; otherwise, configure it for smart-idle. If module is marked
817 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
818 * configure it for smart-standby. No return value.
819 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600820static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300821{
Paul Walmsley43b40992010-02-22 22:09:34 -0700822 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +0300823 u32 v;
824
Paul Walmsley43b40992010-02-22 22:09:34 -0700825 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +0300826 return;
827
828 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -0700829 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +0300830
Paul Walmsley43b40992010-02-22 22:09:34 -0700831 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +0300832 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
833 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
834 _set_slave_idlemode(oh, idlemode, &v);
835 }
836
Paul Walmsley43b40992010-02-22 22:09:34 -0700837 if (sf & SYSC_HAS_MIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +0300838 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
839 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
840 _set_master_standbymode(oh, idlemode, &v);
841 }
842
Benoit Cousson86009eb2010-12-21 21:31:28 -0700843 /* If slave is in SMARTIDLE, also enable wakeup */
844 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
845 _enable_wakeup(oh, &v);
846
Paul Walmsley63c85232009-09-03 20:14:03 +0300847 _write_sysconfig(v, oh);
848}
849
850/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600851 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +0300852 * @oh: struct omap_hwmod *
853 *
854 * Force the module into slave idle and master suspend. No return
855 * value.
856 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600857static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300858{
859 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -0700860 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +0300861
Paul Walmsley43b40992010-02-22 22:09:34 -0700862 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +0300863 return;
864
865 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -0700866 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +0300867
Paul Walmsley43b40992010-02-22 22:09:34 -0700868 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +0300869 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
870
Paul Walmsley43b40992010-02-22 22:09:34 -0700871 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +0300872 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
873
Paul Walmsley43b40992010-02-22 22:09:34 -0700874 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -0700875 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +0300876
877 _write_sysconfig(v, oh);
878}
879
880/**
881 * _lookup - find an omap_hwmod by name
882 * @name: find an omap_hwmod by name
883 *
884 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +0300885 */
886static struct omap_hwmod *_lookup(const char *name)
887{
888 struct omap_hwmod *oh, *temp_oh;
889
890 oh = NULL;
891
892 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
893 if (!strcmp(name, temp_oh->name)) {
894 oh = temp_oh;
895 break;
896 }
897 }
898
899 return oh;
900}
901
902/**
903 * _init_clocks - clk_get() all clocks associated with this hwmod
904 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -0600905 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +0300906 *
907 * Called by omap_hwmod_late_init() (after omap2_clk_init()).
Kevin Hilman12b1fdb2010-09-21 10:34:09 -0600908 * Resolves all clock names embedded in the hwmod. Returns -EINVAL if
909 * the omap_hwmod has not yet been registered or if the clocks have
910 * already been initialized, 0 on success, or a non-zero error on
911 * failure.
Paul Walmsley63c85232009-09-03 20:14:03 +0300912 */
Paul Walmsley97d60162010-07-26 16:34:30 -0600913static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +0300914{
915 int ret = 0;
916
917 if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
918 return -EINVAL;
919
920 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
921
922 ret |= _init_main_clk(oh);
923 ret |= _init_interface_clks(oh);
924 ret |= _init_opt_clks(oh);
925
Benoit Coussonf5c1f842010-05-20 12:31:10 -0600926 if (!ret)
927 oh->_state = _HWMOD_STATE_CLKS_INITED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300928
Rajendra Nayak09c35f22011-02-16 12:11:24 +0000929 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +0300930}
931
932/**
933 * _wait_target_ready - wait for a module to leave slave idle
934 * @oh: struct omap_hwmod *
935 *
936 * Wait for a module @oh to leave slave idle. Returns 0 if the module
937 * does not have an IDLEST bit or if the module successfully leaves
938 * slave idle; otherwise, pass along the return value of the
939 * appropriate *_cm_wait_module_ready() function.
940 */
941static int _wait_target_ready(struct omap_hwmod *oh)
942{
943 struct omap_hwmod_ocp_if *os;
944 int ret;
945
946 if (!oh)
947 return -EINVAL;
948
949 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
950 return 0;
951
Benoit Cousson682fdc92010-05-20 12:31:09 -0600952 os = oh->slaves[oh->_mpu_port_index];
Paul Walmsley63c85232009-09-03 20:14:03 +0300953
Benoit Cousson33f7ec82010-05-20 12:31:09 -0600954 if (oh->flags & HWMOD_NO_IDLEST)
Paul Walmsley63c85232009-09-03 20:14:03 +0300955 return 0;
956
957 /* XXX check module SIDLEMODE */
958
959 /* XXX check clock enable states */
960
961 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
962 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
963 oh->prcm.omap2.idlest_reg_id,
964 oh->prcm.omap2.idlest_idle_bit);
Paul Walmsley63c85232009-09-03 20:14:03 +0300965 } else if (cpu_is_omap44xx()) {
Benoit Cousson9a23dfe2010-05-20 12:31:08 -0600966 ret = omap4_cm_wait_module_ready(oh->prcm.omap4.clkctrl_reg);
Paul Walmsley63c85232009-09-03 20:14:03 +0300967 } else {
968 BUG();
969 };
970
971 return ret;
972}
973
974/**
omar ramirezcc1226e2011-03-04 13:32:44 -0700975 * _lookup_hardreset - fill register bit info for this hwmod/reset line
Benoît Cousson5365efb2010-09-21 10:34:11 -0600976 * @oh: struct omap_hwmod *
977 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -0700978 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
Benoît Cousson5365efb2010-09-21 10:34:11 -0600979 *
980 * Return the bit position of the reset line that match the
981 * input name. Return -ENOENT if not found.
982 */
omar ramirezcc1226e2011-03-04 13:32:44 -0700983static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
984 struct omap_hwmod_rst_info *ohri)
Benoît Cousson5365efb2010-09-21 10:34:11 -0600985{
986 int i;
987
988 for (i = 0; i < oh->rst_lines_cnt; i++) {
989 const char *rst_line = oh->rst_lines[i].name;
990 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -0700991 ohri->rst_shift = oh->rst_lines[i].rst_shift;
992 ohri->st_shift = oh->rst_lines[i].st_shift;
993 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
994 oh->name, __func__, rst_line, ohri->rst_shift,
995 ohri->st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -0600996
omar ramirezcc1226e2011-03-04 13:32:44 -0700997 return 0;
Benoît Cousson5365efb2010-09-21 10:34:11 -0600998 }
999 }
1000
1001 return -ENOENT;
1002}
1003
1004/**
1005 * _assert_hardreset - assert the HW reset line of submodules
1006 * contained in the hwmod module.
1007 * @oh: struct omap_hwmod *
1008 * @name: name of the reset line to lookup and assert
1009 *
1010 * Some IP like dsp, ipu or iva contain processor that require
1011 * an HW reset line to be assert / deassert in order to enable fully
1012 * the IP.
1013 */
1014static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1015{
omar ramirezcc1226e2011-03-04 13:32:44 -07001016 struct omap_hwmod_rst_info ohri;
1017 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001018
1019 if (!oh)
1020 return -EINVAL;
1021
omar ramirezcc1226e2011-03-04 13:32:44 -07001022 ret = _lookup_hardreset(oh, name, &ohri);
1023 if (IS_ERR_VALUE(ret))
1024 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001025
1026 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1027 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001028 ohri.rst_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001029 else if (cpu_is_omap44xx())
1030 return omap4_prm_assert_hardreset(oh->prcm.omap4.rstctrl_reg,
omar ramirezcc1226e2011-03-04 13:32:44 -07001031 ohri.rst_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001032 else
1033 return -EINVAL;
1034}
1035
1036/**
1037 * _deassert_hardreset - deassert the HW reset line of submodules contained
1038 * in the hwmod module.
1039 * @oh: struct omap_hwmod *
1040 * @name: name of the reset line to look up and deassert
1041 *
1042 * Some IP like dsp, ipu or iva contain processor that require
1043 * an HW reset line to be assert / deassert in order to enable fully
1044 * the IP.
1045 */
1046static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1047{
omar ramirezcc1226e2011-03-04 13:32:44 -07001048 struct omap_hwmod_rst_info ohri;
1049 int ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001050
1051 if (!oh)
1052 return -EINVAL;
1053
omar ramirezcc1226e2011-03-04 13:32:44 -07001054 ret = _lookup_hardreset(oh, name, &ohri);
1055 if (IS_ERR_VALUE(ret))
1056 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001057
omar ramirezcc1226e2011-03-04 13:32:44 -07001058 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1059 ret = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
1060 ohri.rst_shift,
1061 ohri.st_shift);
1062 } else if (cpu_is_omap44xx()) {
1063 if (ohri.st_shift)
1064 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
1065 oh->name, name);
1066 ret = omap4_prm_deassert_hardreset(oh->prcm.omap4.rstctrl_reg,
1067 ohri.rst_shift);
1068 } else {
Benoît Cousson5365efb2010-09-21 10:34:11 -06001069 return -EINVAL;
omar ramirezcc1226e2011-03-04 13:32:44 -07001070 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001071
omar ramirezcc1226e2011-03-04 13:32:44 -07001072 if (ret == -EBUSY)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001073 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1074
omar ramirezcc1226e2011-03-04 13:32:44 -07001075 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001076}
1077
1078/**
1079 * _read_hardreset - read the HW reset line state of submodules
1080 * contained in the hwmod module
1081 * @oh: struct omap_hwmod *
1082 * @name: name of the reset line to look up and read
1083 *
1084 * Return the state of the reset line.
1085 */
1086static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1087{
omar ramirezcc1226e2011-03-04 13:32:44 -07001088 struct omap_hwmod_rst_info ohri;
1089 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001090
1091 if (!oh)
1092 return -EINVAL;
1093
omar ramirezcc1226e2011-03-04 13:32:44 -07001094 ret = _lookup_hardreset(oh, name, &ohri);
1095 if (IS_ERR_VALUE(ret))
1096 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001097
1098 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1099 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001100 ohri.st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001101 } else if (cpu_is_omap44xx()) {
1102 return omap4_prm_is_hardreset_asserted(oh->prcm.omap4.rstctrl_reg,
omar ramirezcc1226e2011-03-04 13:32:44 -07001103 ohri.rst_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001104 } else {
1105 return -EINVAL;
1106 }
1107}
1108
1109/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001110 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001111 * @oh: struct omap_hwmod *
1112 *
1113 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Kevin Hilman12b1fdb2010-09-21 10:34:09 -06001114 * enabled for this to work. Returns -EINVAL if the hwmod cannot be
1115 * reset this way or if the hwmod is in the wrong state, -ETIMEDOUT if
1116 * the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001117 *
1118 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001119 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001120 * use the SYSCONFIG softreset bit to provide the status.
1121 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001122 * Note that some IP like McBSP do have reset control but don't have
1123 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001124 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001125static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001126{
Benoit Cousson96835af2010-09-21 18:57:58 +02001127 u32 v;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001128 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001129 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001130
Paul Walmsley43b40992010-02-22 22:09:34 -07001131 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001132 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +03001133 return -EINVAL;
1134
1135 /* clocks must be on for this operation */
1136 if (oh->_state != _HWMOD_STATE_ENABLED) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001137 pr_warning("omap_hwmod: %s: reset can only be entered from "
1138 "enabled state\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001139 return -EINVAL;
1140 }
1141
Benoit Cousson96835af2010-09-21 18:57:58 +02001142 /* For some modules, all optionnal clocks need to be enabled as well */
1143 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1144 _enable_optional_clocks(oh);
1145
Paul Walmsleybd361792010-12-14 12:42:35 -07001146 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001147
1148 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001149 ret = _set_softreset(oh, &v);
1150 if (ret)
1151 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001152 _write_sysconfig(v, oh);
1153
Benoit Cousson2cb06812010-09-21 18:57:59 +02001154 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001155 omap_test_timeout((omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001156 oh->class->sysc->syss_offs)
1157 & SYSS_RESETDONE_MASK),
1158 MAX_MODULE_SOFTRESET_WAIT, c);
1159 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001160 omap_test_timeout(!(omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001161 oh->class->sysc->sysc_offs)
1162 & SYSC_TYPE2_SOFTRESET_MASK),
1163 MAX_MODULE_SOFTRESET_WAIT, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001164
Benoît Cousson5365efb2010-09-21 10:34:11 -06001165 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001166 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1167 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001168 else
Benoît Cousson5365efb2010-09-21 10:34:11 -06001169 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001170
1171 /*
1172 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1173 * _wait_target_ready() or _reset()
1174 */
1175
Benoit Cousson96835af2010-09-21 18:57:58 +02001176 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1177
1178dis_opt_clks:
1179 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1180 _disable_optional_clocks(oh);
1181
1182 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001183}
1184
1185/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001186 * _reset - reset an omap_hwmod
1187 * @oh: struct omap_hwmod *
1188 *
1189 * Resets an omap_hwmod @oh. The default software reset mechanism for
1190 * most OMAP IP blocks is triggered via the OCP_SYSCONFIG.SOFTRESET
1191 * bit. However, some hwmods cannot be reset via this method: some
1192 * are not targets and therefore have no OCP header registers to
1193 * access; others (like the IVA) have idiosyncratic reset sequences.
1194 * So for these relatively rare cases, custom reset code can be
1195 * supplied in the struct omap_hwmod_class .reset function pointer.
1196 * Passes along the return value from either _reset() or the custom
1197 * reset function - these must return -EINVAL if the hwmod cannot be
1198 * reset this way or if the hwmod is in the wrong state, -ETIMEDOUT if
1199 * the module did not reset in time, or 0 upon success.
1200 */
1201static int _reset(struct omap_hwmod *oh)
1202{
1203 int ret;
1204
1205 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1206
1207 ret = (oh->class->reset) ? oh->class->reset(oh) : _ocp_softreset(oh);
1208
1209 return ret;
1210}
1211
1212/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001213 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001214 * @oh: struct omap_hwmod *
1215 *
1216 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001217 * register target. Returns -EINVAL if the hwmod is in the wrong
1218 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001219 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001220static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001221{
1222 int r;
1223
1224 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1225 oh->_state != _HWMOD_STATE_IDLE &&
1226 oh->_state != _HWMOD_STATE_DISABLED) {
1227 WARN(1, "omap_hwmod: %s: enabled state can only be entered "
1228 "from initialized, idle, or disabled state\n", oh->name);
1229 return -EINVAL;
1230 }
1231
1232 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1233
Benoît Cousson5365efb2010-09-21 10:34:11 -06001234 /*
1235 * If an IP contains only one HW reset line, then de-assert it in order
1236 * to allow to enable the clocks. Otherwise the PRCM will return
1237 * Intransition status, and the init will failed.
1238 */
1239 if ((oh->_state == _HWMOD_STATE_INITIALIZED ||
1240 oh->_state == _HWMOD_STATE_DISABLED) && oh->rst_lines_cnt == 1)
1241 _deassert_hardreset(oh, oh->rst_lines[0].name);
1242
Tony Lindgren8d9af882010-12-22 18:42:35 -08001243 /* Mux pins for device runtime if populated */
1244 if (oh->mux)
1245 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03001246
1247 _add_initiator_dep(oh, mpu_oh);
1248 _enable_clocks(oh);
1249
Paul Walmsley63c85232009-09-03 20:14:03 +03001250 r = _wait_target_ready(oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001251 if (!r) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001252 oh->_state = _HWMOD_STATE_ENABLED;
1253
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001254 /* Access the sysconfig only if the target is ready */
1255 if (oh->class->sysc) {
1256 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1257 _update_sysc_cache(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001258 _enable_sysc(oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001259 }
1260 } else {
Rajendra Nayakf2dd7e02010-12-21 21:31:28 -07001261 _disable_clocks(oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001262 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1263 oh->name, r);
1264 }
1265
Paul Walmsley63c85232009-09-03 20:14:03 +03001266 return r;
1267}
1268
1269/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001270 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001271 * @oh: struct omap_hwmod *
1272 *
1273 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001274 * no further work. Returns -EINVAL if the hwmod is in the wrong
1275 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001276 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001277static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001278{
1279 if (oh->_state != _HWMOD_STATE_ENABLED) {
1280 WARN(1, "omap_hwmod: %s: idle state can only be entered from "
1281 "enabled state\n", oh->name);
1282 return -EINVAL;
1283 }
1284
1285 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1286
Paul Walmsley43b40992010-02-22 22:09:34 -07001287 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001288 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001289 _del_initiator_dep(oh, mpu_oh);
1290 _disable_clocks(oh);
1291
Tony Lindgren8d9af882010-12-22 18:42:35 -08001292 /* Mux pins for device idle if populated */
1293 if (oh->mux)
1294 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
1295
Paul Walmsley63c85232009-09-03 20:14:03 +03001296 oh->_state = _HWMOD_STATE_IDLE;
1297
1298 return 0;
1299}
1300
1301/**
1302 * _shutdown - shutdown an omap_hwmod
1303 * @oh: struct omap_hwmod *
1304 *
1305 * Shut down an omap_hwmod @oh. This should be called when the driver
1306 * used for the hwmod is removed or unloaded or if the driver is not
1307 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1308 * state or returns 0.
1309 */
1310static int _shutdown(struct omap_hwmod *oh)
1311{
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001312 int ret;
1313 u8 prev_state;
1314
Paul Walmsley63c85232009-09-03 20:14:03 +03001315 if (oh->_state != _HWMOD_STATE_IDLE &&
1316 oh->_state != _HWMOD_STATE_ENABLED) {
1317 WARN(1, "omap_hwmod: %s: disabled state can only be entered "
1318 "from idle, or enabled state\n", oh->name);
1319 return -EINVAL;
1320 }
1321
1322 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
1323
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001324 if (oh->class->pre_shutdown) {
1325 prev_state = oh->_state;
1326 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001327 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001328 ret = oh->class->pre_shutdown(oh);
1329 if (ret) {
1330 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001331 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001332 return ret;
1333 }
1334 }
1335
Paul Walmsley43b40992010-02-22 22:09:34 -07001336 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001337 _shutdown_sysc(oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06001338
Benoît Cousson5365efb2010-09-21 10:34:11 -06001339 /*
1340 * If an IP contains only one HW reset line, then assert it
1341 * before disabling the clocks and shutting down the IP.
1342 */
1343 if (oh->rst_lines_cnt == 1)
1344 _assert_hardreset(oh, oh->rst_lines[0].name);
1345
Benoit Cousson3827f942010-09-21 10:34:08 -06001346 /* clocks and deps are already disabled in idle */
1347 if (oh->_state == _HWMOD_STATE_ENABLED) {
1348 _del_initiator_dep(oh, mpu_oh);
1349 /* XXX what about the other system initiators here? dma, dsp */
1350 _disable_clocks(oh);
1351 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001352 /* XXX Should this code also force-disable the optional clocks? */
1353
Tony Lindgren8d9af882010-12-22 18:42:35 -08001354 /* Mux pins to safe mode or use populated off mode values */
1355 if (oh->mux)
1356 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03001357
1358 oh->_state = _HWMOD_STATE_DISABLED;
1359
1360 return 0;
1361}
1362
1363/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001364 * _setup - do initial configuration of omap_hwmod
1365 * @oh: struct omap_hwmod *
1366 *
1367 * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001368 * OCP_SYSCONFIG register. Returns -EINVAL if the hwmod is in the
1369 * wrong state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001370 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001371static int _setup(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001372{
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001373 int i, r;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001374 u8 postsetup_state;
Paul Walmsley97d60162010-07-26 16:34:30 -06001375
Paul Walmsley63c85232009-09-03 20:14:03 +03001376 /* Set iclk autoidle mode */
1377 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -06001378 for (i = 0; i < oh->slaves_cnt; i++) {
1379 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +03001380 struct clk *c = os->_clk;
1381
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -06001382 if (!c)
Paul Walmsley63c85232009-09-03 20:14:03 +03001383 continue;
1384
1385 if (os->flags & OCPIF_SWSUP_IDLE) {
1386 /* XXX omap_iclk_deny_idle(c); */
1387 } else {
1388 /* XXX omap_iclk_allow_idle(c); */
1389 clk_enable(c);
1390 }
1391 }
1392 }
1393
1394 oh->_state = _HWMOD_STATE_INITIALIZED;
1395
Benoît Cousson5365efb2010-09-21 10:34:11 -06001396 /*
1397 * In the case of hwmod with hardreset that should not be
1398 * de-assert at boot time, we have to keep the module
1399 * initialized, because we cannot enable it properly with the
1400 * reset asserted. Exit without warning because that behavior is
1401 * expected.
1402 */
1403 if ((oh->flags & HWMOD_INIT_NO_RESET) && oh->rst_lines_cnt == 1)
1404 return 0;
1405
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001406 r = _enable(oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001407 if (r) {
1408 pr_warning("omap_hwmod: %s: cannot be enabled (%d)\n",
1409 oh->name, oh->_state);
1410 return 0;
1411 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001412
Paul Walmsleyb835d012009-12-08 16:34:14 -07001413 if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001414 _reset(oh);
1415
Paul Walmsleyb835d012009-12-08 16:34:14 -07001416 /*
Benoit Cousson76e55892010-09-21 10:34:11 -06001417 * OCP_SYSCONFIG bits need to be reprogrammed after a softreset.
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001418 * The _enable() function should be split to
Benoit Cousson76e55892010-09-21 10:34:11 -06001419 * avoid the rewrite of the OCP_SYSCONFIG register.
Paul Walmsleyb835d012009-12-08 16:34:14 -07001420 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001421 if (oh->class->sysc) {
Paul Walmsleyb835d012009-12-08 16:34:14 -07001422 _update_sysc_cache(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001423 _enable_sysc(oh);
Paul Walmsleyb835d012009-12-08 16:34:14 -07001424 }
1425 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001426
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001427 postsetup_state = oh->_postsetup_state;
1428 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
1429 postsetup_state = _HWMOD_STATE_ENABLED;
1430
1431 /*
1432 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
1433 * it should be set by the core code as a runtime flag during startup
1434 */
1435 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
1436 (postsetup_state == _HWMOD_STATE_IDLE))
1437 postsetup_state = _HWMOD_STATE_ENABLED;
1438
1439 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001440 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001441 else if (postsetup_state == _HWMOD_STATE_DISABLED)
1442 _shutdown(oh);
1443 else if (postsetup_state != _HWMOD_STATE_ENABLED)
1444 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
1445 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03001446
1447 return 0;
1448}
1449
Benoit Cousson0102b622010-12-21 21:31:27 -07001450/**
1451 * _register - register a struct omap_hwmod
1452 * @oh: struct omap_hwmod *
1453 *
1454 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
1455 * already has been registered by the same name; -EINVAL if the
1456 * omap_hwmod is in the wrong state, if @oh is NULL, if the
1457 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
1458 * name, or if the omap_hwmod's class is missing a name; or 0 upon
1459 * success.
1460 *
1461 * XXX The data should be copied into bootmem, so the original data
1462 * should be marked __initdata and freed after init. This would allow
1463 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
1464 * that the copy process would be relatively complex due to the large number
1465 * of substructures.
1466 */
Benoit Cousson01592df2010-12-21 21:31:28 -07001467static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07001468{
1469 int ret, ms_id;
1470
1471 if (!oh || !oh->name || !oh->class || !oh->class->name ||
1472 (oh->_state != _HWMOD_STATE_UNKNOWN))
1473 return -EINVAL;
1474
Benoit Cousson0102b622010-12-21 21:31:27 -07001475 pr_debug("omap_hwmod: %s: registering\n", oh->name);
1476
Benoit Coussonce35b242010-12-21 21:31:28 -07001477 if (_lookup(oh->name))
1478 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07001479
1480 ms_id = _find_mpu_port_index(oh);
1481 if (!IS_ERR_VALUE(ms_id)) {
1482 oh->_mpu_port_index = ms_id;
1483 oh->_mpu_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
1484 } else {
1485 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1486 }
1487
1488 list_add_tail(&oh->node, &omap_hwmod_list);
1489
1490 spin_lock_init(&oh->_lock);
1491
1492 oh->_state = _HWMOD_STATE_REGISTERED;
1493
1494 ret = 0;
1495
Benoit Cousson0102b622010-12-21 21:31:27 -07001496 return ret;
1497}
Paul Walmsley63c85232009-09-03 20:14:03 +03001498
1499
1500/* Public functions */
1501
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001502u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03001503{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001504 if (oh->flags & HWMOD_16BIT_REG)
1505 return __raw_readw(oh->_mpu_rt_va + reg_offs);
1506 else
1507 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001508}
1509
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001510void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03001511{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001512 if (oh->flags & HWMOD_16BIT_REG)
1513 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
1514 else
1515 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001516}
1517
Paul Walmsley887adea2010-07-26 16:34:33 -06001518/**
1519 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
1520 * @oh: struct omap_hwmod *
1521 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
1522 *
1523 * Sets the IP block's OCP slave idlemode in hardware, and updates our
1524 * local copy. Intended to be used by drivers that have some erratum
1525 * that requires direct manipulation of the SIDLEMODE bits. Returns
1526 * -EINVAL if @oh is null, or passes along the return value from
1527 * _set_slave_idlemode().
1528 *
1529 * XXX Does this function have any current users? If not, we should
1530 * remove it; it is better to let the rest of the hwmod code handle this.
1531 * Any users of this function should be scrutinized carefully.
1532 */
Kevin Hilman46273e62010-01-26 20:13:03 -07001533int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
1534{
1535 u32 v;
1536 int retval = 0;
1537
1538 if (!oh)
1539 return -EINVAL;
1540
1541 v = oh->_sysc_cache;
1542
1543 retval = _set_slave_idlemode(oh, idlemode, &v);
1544 if (!retval)
1545 _write_sysconfig(v, oh);
1546
1547 return retval;
1548}
1549
Paul Walmsley63c85232009-09-03 20:14:03 +03001550/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001551 * omap_hwmod_lookup - look up a registered omap_hwmod by name
1552 * @name: name of the omap_hwmod to look up
1553 *
1554 * Given a @name of an omap_hwmod, return a pointer to the registered
1555 * struct omap_hwmod *, or NULL upon error.
1556 */
1557struct omap_hwmod *omap_hwmod_lookup(const char *name)
1558{
1559 struct omap_hwmod *oh;
1560
1561 if (!name)
1562 return NULL;
1563
Paul Walmsley63c85232009-09-03 20:14:03 +03001564 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001565
1566 return oh;
1567}
1568
1569/**
1570 * omap_hwmod_for_each - call function for each registered omap_hwmod
1571 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06001572 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03001573 *
1574 * Call @fn for each registered omap_hwmod, passing @data to each
1575 * function. @fn must return 0 for success or any other value for
1576 * failure. If @fn returns non-zero, the iteration across omap_hwmods
1577 * will stop and the non-zero return value will be passed to the
1578 * caller of omap_hwmod_for_each(). @fn is called with
1579 * omap_hwmod_for_each() held.
1580 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001581int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
1582 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001583{
1584 struct omap_hwmod *temp_oh;
1585 int ret;
1586
1587 if (!fn)
1588 return -EINVAL;
1589
Paul Walmsley63c85232009-09-03 20:14:03 +03001590 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06001591 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03001592 if (ret)
1593 break;
1594 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001595
1596 return ret;
1597}
1598
1599
1600/**
1601 * omap_hwmod_init - init omap_hwmod code and register hwmods
1602 * @ohs: pointer to an array of omap_hwmods to register
1603 *
1604 * Intended to be called early in boot before the clock framework is
1605 * initialized. If @ohs is not null, will register all omap_hwmods
1606 * listed in @ohs that are valid for this chip. Returns -EINVAL if
1607 * omap_hwmod_init() has already been called or 0 otherwise.
1608 */
Benoit Cousson01592df2010-12-21 21:31:28 -07001609int __init omap_hwmod_init(struct omap_hwmod **ohs)
Paul Walmsley63c85232009-09-03 20:14:03 +03001610{
1611 struct omap_hwmod *oh;
1612 int r;
1613
1614 if (inited)
1615 return -EINVAL;
1616
1617 inited = 1;
1618
1619 if (!ohs)
1620 return 0;
1621
1622 oh = *ohs;
1623 while (oh) {
1624 if (omap_chip_is(oh->omap_chip)) {
Benoit Cousson0102b622010-12-21 21:31:27 -07001625 r = _register(oh);
1626 WARN(r, "omap_hwmod: %s: _register returned "
Paul Walmsley63c85232009-09-03 20:14:03 +03001627 "%d\n", oh->name, r);
1628 }
1629 oh = *++ohs;
1630 }
1631
1632 return 0;
1633}
1634
1635/**
1636 * omap_hwmod_late_init - do some post-clock framework initialization
1637 *
1638 * Must be called after omap2_clk_init(). Resolves the struct clk names
1639 * to struct clk pointers for each registered omap_hwmod. Also calls
1640 * _setup() on each hwmod. Returns 0.
1641 */
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001642int omap_hwmod_late_init(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03001643{
1644 int r;
1645
1646 /* XXX check return value */
Paul Walmsley97d60162010-07-26 16:34:30 -06001647 r = omap_hwmod_for_each(_init_clocks, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03001648 WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
1649
1650 mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
1651 WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
1652 MPU_INITIATOR_NAME);
1653
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001654 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03001655
1656 return 0;
1657}
1658
1659/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001660 * omap_hwmod_enable - enable an omap_hwmod
1661 * @oh: struct omap_hwmod *
1662 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001663 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03001664 * Returns -EINVAL on error or passes along the return value from _enable().
1665 */
1666int omap_hwmod_enable(struct omap_hwmod *oh)
1667{
1668 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001669 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001670
1671 if (!oh)
1672 return -EINVAL;
1673
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001674 spin_lock_irqsave(&oh->_lock, flags);
1675 r = _enable(oh);
1676 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001677
1678 return r;
1679}
1680
1681/**
1682 * omap_hwmod_idle - idle an omap_hwmod
1683 * @oh: struct omap_hwmod *
1684 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001685 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03001686 * Returns -EINVAL on error or passes along the return value from _idle().
1687 */
1688int omap_hwmod_idle(struct omap_hwmod *oh)
1689{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001690 unsigned long flags;
1691
Paul Walmsley63c85232009-09-03 20:14:03 +03001692 if (!oh)
1693 return -EINVAL;
1694
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001695 spin_lock_irqsave(&oh->_lock, flags);
1696 _idle(oh);
1697 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001698
1699 return 0;
1700}
1701
1702/**
1703 * omap_hwmod_shutdown - shutdown an omap_hwmod
1704 * @oh: struct omap_hwmod *
1705 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001706 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03001707 * omap_device_shutdown(). Returns -EINVAL on error or passes along
1708 * the return value from _shutdown().
1709 */
1710int omap_hwmod_shutdown(struct omap_hwmod *oh)
1711{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001712 unsigned long flags;
1713
Paul Walmsley63c85232009-09-03 20:14:03 +03001714 if (!oh)
1715 return -EINVAL;
1716
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001717 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001718 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001719 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001720
1721 return 0;
1722}
1723
1724/**
1725 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
1726 * @oh: struct omap_hwmod *oh
1727 *
1728 * Intended to be called by the omap_device code.
1729 */
1730int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
1731{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001732 unsigned long flags;
1733
1734 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001735 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001736 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001737
1738 return 0;
1739}
1740
1741/**
1742 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
1743 * @oh: struct omap_hwmod *oh
1744 *
1745 * Intended to be called by the omap_device code.
1746 */
1747int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
1748{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001749 unsigned long flags;
1750
1751 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001752 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001753 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001754
1755 return 0;
1756}
1757
1758/**
1759 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
1760 * @oh: struct omap_hwmod *oh
1761 *
1762 * Intended to be called by drivers and core code when all posted
1763 * writes to a device must complete before continuing further
1764 * execution (for example, after clearing some device IRQSTATUS
1765 * register bits)
1766 *
1767 * XXX what about targets with multiple OCP threads?
1768 */
1769void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
1770{
1771 BUG_ON(!oh);
1772
Paul Walmsley43b40992010-02-22 22:09:34 -07001773 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001774 WARN(1, "omap_device: %s: OCP barrier impossible due to "
1775 "device configuration\n", oh->name);
1776 return;
1777 }
1778
1779 /*
1780 * Forces posted writes to complete on the OCP thread handling
1781 * register writes
1782 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001783 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001784}
1785
1786/**
1787 * omap_hwmod_reset - reset the hwmod
1788 * @oh: struct omap_hwmod *
1789 *
1790 * Under some conditions, a driver may wish to reset the entire device.
1791 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06001792 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03001793 */
1794int omap_hwmod_reset(struct omap_hwmod *oh)
1795{
1796 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001797 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001798
Liam Girdwood9b579112010-09-21 10:34:09 -06001799 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001800 return -EINVAL;
1801
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001802 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001803 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001804 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03001805
1806 return r;
1807}
1808
1809/**
1810 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
1811 * @oh: struct omap_hwmod *
1812 * @res: pointer to the first element of an array of struct resource to fill
1813 *
1814 * Count the number of struct resource array elements necessary to
1815 * contain omap_hwmod @oh resources. Intended to be called by code
1816 * that registers omap_devices. Intended to be used to determine the
1817 * size of a dynamically-allocated struct resource array, before
1818 * calling omap_hwmod_fill_resources(). Returns the number of struct
1819 * resource array elements needed.
1820 *
1821 * XXX This code is not optimized. It could attempt to merge adjacent
1822 * resource IDs.
1823 *
1824 */
1825int omap_hwmod_count_resources(struct omap_hwmod *oh)
1826{
1827 int ret, i;
1828
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06001829 ret = oh->mpu_irqs_cnt + oh->sdma_reqs_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03001830
1831 for (i = 0; i < oh->slaves_cnt; i++)
Benoit Cousson682fdc92010-05-20 12:31:09 -06001832 ret += oh->slaves[i]->addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03001833
1834 return ret;
1835}
1836
1837/**
1838 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
1839 * @oh: struct omap_hwmod *
1840 * @res: pointer to the first element of an array of struct resource to fill
1841 *
1842 * Fill the struct resource array @res with resource data from the
1843 * omap_hwmod @oh. Intended to be called by code that registers
1844 * omap_devices. See also omap_hwmod_count_resources(). Returns the
1845 * number of array elements filled.
1846 */
1847int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
1848{
1849 int i, j;
1850 int r = 0;
1851
1852 /* For each IRQ, DMA, memory area, fill in array.*/
1853
1854 for (i = 0; i < oh->mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07001855 (res + r)->name = (oh->mpu_irqs + i)->name;
1856 (res + r)->start = (oh->mpu_irqs + i)->irq;
1857 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03001858 (res + r)->flags = IORESOURCE_IRQ;
1859 r++;
1860 }
1861
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06001862 for (i = 0; i < oh->sdma_reqs_cnt; i++) {
1863 (res + r)->name = (oh->sdma_reqs + i)->name;
1864 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
1865 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03001866 (res + r)->flags = IORESOURCE_DMA;
1867 r++;
1868 }
1869
1870 for (i = 0; i < oh->slaves_cnt; i++) {
1871 struct omap_hwmod_ocp_if *os;
1872
Benoit Cousson682fdc92010-05-20 12:31:09 -06001873 os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +03001874
1875 for (j = 0; j < os->addr_cnt; j++) {
1876 (res + r)->start = (os->addr + j)->pa_start;
1877 (res + r)->end = (os->addr + j)->pa_end;
1878 (res + r)->flags = IORESOURCE_MEM;
1879 r++;
1880 }
1881 }
1882
1883 return r;
1884}
1885
1886/**
1887 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
1888 * @oh: struct omap_hwmod *
1889 *
1890 * Return the powerdomain pointer associated with the OMAP module
1891 * @oh's main clock. If @oh does not have a main clk, return the
1892 * powerdomain associated with the interface clock associated with the
1893 * module's MPU port. (XXX Perhaps this should use the SDMA port
1894 * instead?) Returns NULL on error, or a struct powerdomain * on
1895 * success.
1896 */
1897struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
1898{
1899 struct clk *c;
1900
1901 if (!oh)
1902 return NULL;
1903
1904 if (oh->_clk) {
1905 c = oh->_clk;
1906 } else {
1907 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1908 return NULL;
1909 c = oh->slaves[oh->_mpu_port_index]->_clk;
1910 }
1911
Thara Gopinathd5647c12010-03-31 04:16:29 -06001912 if (!c->clkdm)
1913 return NULL;
1914
Paul Walmsley63c85232009-09-03 20:14:03 +03001915 return c->clkdm->pwrdm.ptr;
1916
1917}
1918
1919/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06001920 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
1921 * @oh: struct omap_hwmod *
1922 *
1923 * Returns the virtual address corresponding to the beginning of the
1924 * module's register target, in the address range that is intended to
1925 * be used by the MPU. Returns the virtual address upon success or NULL
1926 * upon error.
1927 */
1928void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
1929{
1930 if (!oh)
1931 return NULL;
1932
1933 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1934 return NULL;
1935
1936 if (oh->_state == _HWMOD_STATE_UNKNOWN)
1937 return NULL;
1938
1939 return oh->_mpu_rt_va;
1940}
1941
1942/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001943 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
1944 * @oh: struct omap_hwmod *
1945 * @init_oh: struct omap_hwmod * (initiator)
1946 *
1947 * Add a sleep dependency between the initiator @init_oh and @oh.
1948 * Intended to be called by DSP/Bridge code via platform_data for the
1949 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1950 * code needs to add/del initiator dependencies dynamically
1951 * before/after accessing a device. Returns the return value from
1952 * _add_initiator_dep().
1953 *
1954 * XXX Keep a usecount in the clockdomain code
1955 */
1956int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
1957 struct omap_hwmod *init_oh)
1958{
1959 return _add_initiator_dep(oh, init_oh);
1960}
1961
1962/*
1963 * XXX what about functions for drivers to save/restore ocp_sysconfig
1964 * for context save/restore operations?
1965 */
1966
1967/**
1968 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
1969 * @oh: struct omap_hwmod *
1970 * @init_oh: struct omap_hwmod * (initiator)
1971 *
1972 * Remove a sleep dependency between the initiator @init_oh and @oh.
1973 * Intended to be called by DSP/Bridge code via platform_data for the
1974 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1975 * code needs to add/del initiator dependencies dynamically
1976 * before/after accessing a device. Returns the return value from
1977 * _del_initiator_dep().
1978 *
1979 * XXX Keep a usecount in the clockdomain code
1980 */
1981int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
1982 struct omap_hwmod *init_oh)
1983{
1984 return _del_initiator_dep(oh, init_oh);
1985}
1986
1987/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001988 * omap_hwmod_enable_wakeup - allow device to wake up the system
1989 * @oh: struct omap_hwmod *
1990 *
1991 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
1992 * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
1993 * registers to cause the PRCM to receive wakeup events from the
1994 * module. Does not set any wakeup routing registers beyond this
1995 * point - if the module is to wake up any other module or subsystem,
1996 * that must be set separately. Called by omap_device code. Returns
1997 * -EINVAL on error or 0 upon success.
1998 */
1999int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
2000{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002001 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002002 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002003
Paul Walmsley43b40992010-02-22 22:09:34 -07002004 if (!oh->class->sysc ||
2005 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
Paul Walmsley63c85232009-09-03 20:14:03 +03002006 return -EINVAL;
2007
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002008 spin_lock_irqsave(&oh->_lock, flags);
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002009 v = oh->_sysc_cache;
2010 _enable_wakeup(oh, &v);
2011 _write_sysconfig(v, oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002012 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002013
2014 return 0;
2015}
2016
2017/**
2018 * omap_hwmod_disable_wakeup - prevent device from waking the system
2019 * @oh: struct omap_hwmod *
2020 *
2021 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
2022 * from sending wakeups to the PRCM. Eventually this should clear
2023 * PRCM wakeup registers to cause the PRCM to ignore wakeup events
2024 * from the module. Does not set any wakeup routing registers beyond
2025 * this point - if the module is to wake up any other module or
2026 * subsystem, that must be set separately. Called by omap_device
2027 * code. Returns -EINVAL on error or 0 upon success.
2028 */
2029int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
2030{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002031 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002032 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002033
Paul Walmsley43b40992010-02-22 22:09:34 -07002034 if (!oh->class->sysc ||
2035 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
Paul Walmsley63c85232009-09-03 20:14:03 +03002036 return -EINVAL;
2037
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002038 spin_lock_irqsave(&oh->_lock, flags);
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002039 v = oh->_sysc_cache;
2040 _disable_wakeup(oh, &v);
2041 _write_sysconfig(v, oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002042 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002043
2044 return 0;
2045}
Paul Walmsley43b40992010-02-22 22:09:34 -07002046
2047/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002048 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
2049 * contained in the hwmod module.
2050 * @oh: struct omap_hwmod *
2051 * @name: name of the reset line to lookup and assert
2052 *
2053 * Some IP like dsp, ipu or iva contain processor that require
2054 * an HW reset line to be assert / deassert in order to enable fully
2055 * the IP. Returns -EINVAL if @oh is null or if the operation is not
2056 * yet supported on this OMAP; otherwise, passes along the return value
2057 * from _assert_hardreset().
2058 */
2059int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
2060{
2061 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002062 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002063
2064 if (!oh)
2065 return -EINVAL;
2066
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002067 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002068 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002069 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002070
2071 return ret;
2072}
2073
2074/**
2075 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
2076 * contained in the hwmod module.
2077 * @oh: struct omap_hwmod *
2078 * @name: name of the reset line to look up and deassert
2079 *
2080 * Some IP like dsp, ipu or iva contain processor that require
2081 * an HW reset line to be assert / deassert in order to enable fully
2082 * the IP. Returns -EINVAL if @oh is null or if the operation is not
2083 * yet supported on this OMAP; otherwise, passes along the return value
2084 * from _deassert_hardreset().
2085 */
2086int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
2087{
2088 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002089 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002090
2091 if (!oh)
2092 return -EINVAL;
2093
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002094 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002095 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002096 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002097
2098 return ret;
2099}
2100
2101/**
2102 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
2103 * contained in the hwmod module
2104 * @oh: struct omap_hwmod *
2105 * @name: name of the reset line to look up and read
2106 *
2107 * Return the current state of the hwmod @oh's reset line named @name:
2108 * returns -EINVAL upon parameter error or if this operation
2109 * is unsupported on the current OMAP; otherwise, passes along the return
2110 * value from _read_hardreset().
2111 */
2112int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
2113{
2114 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002115 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002116
2117 if (!oh)
2118 return -EINVAL;
2119
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002120 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002121 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002122 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002123
2124 return ret;
2125}
2126
2127
2128/**
Paul Walmsley43b40992010-02-22 22:09:34 -07002129 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
2130 * @classname: struct omap_hwmod_class name to search for
2131 * @fn: callback function pointer to call for each hwmod in class @classname
2132 * @user: arbitrary context data to pass to the callback function
2133 *
Benoit Coussonce35b242010-12-21 21:31:28 -07002134 * For each omap_hwmod of class @classname, call @fn.
2135 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07002136 * zero, the iterator is terminated, and the callback function's return
2137 * value is passed back to the caller. Returns 0 upon success, -EINVAL
2138 * if @classname or @fn are NULL, or passes back the error code from @fn.
2139 */
2140int omap_hwmod_for_each_by_class(const char *classname,
2141 int (*fn)(struct omap_hwmod *oh,
2142 void *user),
2143 void *user)
2144{
2145 struct omap_hwmod *temp_oh;
2146 int ret = 0;
2147
2148 if (!classname || !fn)
2149 return -EINVAL;
2150
2151 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
2152 __func__, classname);
2153
Paul Walmsley43b40992010-02-22 22:09:34 -07002154 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
2155 if (!strcmp(temp_oh->class->name, classname)) {
2156 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
2157 __func__, temp_oh->name);
2158 ret = (*fn)(temp_oh, user);
2159 if (ret)
2160 break;
2161 }
2162 }
2163
Paul Walmsley43b40992010-02-22 22:09:34 -07002164 if (ret)
2165 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
2166 __func__, ret);
2167
2168 return ret;
2169}
2170
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002171/**
2172 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
2173 * @oh: struct omap_hwmod *
2174 * @state: state that _setup() should leave the hwmod in
2175 *
2176 * Sets the hwmod state that @oh will enter at the end of _setup() (called by
2177 * omap_hwmod_late_init()). Only valid to call between calls to
2178 * omap_hwmod_init() and omap_hwmod_late_init(). Returns 0 upon success or
2179 * -EINVAL if there is a problem with the arguments or if the hwmod is
2180 * in the wrong state.
2181 */
2182int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
2183{
2184 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002185 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002186
2187 if (!oh)
2188 return -EINVAL;
2189
2190 if (state != _HWMOD_STATE_DISABLED &&
2191 state != _HWMOD_STATE_ENABLED &&
2192 state != _HWMOD_STATE_IDLE)
2193 return -EINVAL;
2194
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002195 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002196
2197 if (oh->_state != _HWMOD_STATE_REGISTERED) {
2198 ret = -EINVAL;
2199 goto ohsps_unlock;
2200 }
2201
2202 oh->_postsetup_state = state;
2203 ret = 0;
2204
2205ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002206 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002207
2208 return ret;
2209}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07002210
2211/**
2212 * omap_hwmod_get_context_loss_count - get lost context count
2213 * @oh: struct omap_hwmod *
2214 *
2215 * Query the powerdomain of of @oh to get the context loss
2216 * count for this device.
2217 *
2218 * Returns the context loss count of the powerdomain assocated with @oh
2219 * upon success, or zero if no powerdomain exists for @oh.
2220 */
2221u32 omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
2222{
2223 struct powerdomain *pwrdm;
2224 int ret = 0;
2225
2226 pwrdm = omap_hwmod_get_pwrdm(oh);
2227 if (pwrdm)
2228 ret = pwrdm_get_context_loss_count(pwrdm);
2229
2230 return ret;
2231}