blob: 21ffd8a831c3483ee2a6258677d91b61131bdf9d [file] [log] [blame]
Paul Walmsley63c85232009-09-03 20:14:03 +03001/*
2 * omap_hwmod implementation for OMAP2/3/4
3 *
Paul Walmsley550c8092011-02-28 11:58:14 -07004 * Copyright (C) 2009-2011 Nokia Corporation
Paul Walmsley78183f32011-07-09 19:14:05 -06005 * Copyright (C) 2011 Texas Instruments, Inc.
Paul Walmsley63c85232009-09-03 20:14:03 +03006 *
Paul Walmsley4788da22010-05-18 20:24:05 -06007 * Paul Walmsley, Benoît Cousson, Kevin Hilman
8 *
9 * Created in collaboration with (alphabetical order): Thara Gopinath,
10 * Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari Poussa, Anand
11 * Sawant, Santosh Shilimkar, Richard Woodruff
Paul Walmsley63c85232009-09-03 20:14:03 +030012 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -060017 * Introduction
18 * ------------
19 * One way to view an OMAP SoC is as a collection of largely unrelated
20 * IP blocks connected by interconnects. The IP blocks include
21 * devices such as ARM processors, audio serial interfaces, UARTs,
22 * etc. Some of these devices, like the DSP, are created by TI;
23 * others, like the SGX, largely originate from external vendors. In
24 * TI's documentation, on-chip devices are referred to as "OMAP
25 * modules." Some of these IP blocks are identical across several
26 * OMAP versions. Others are revised frequently.
Paul Walmsley63c85232009-09-03 20:14:03 +030027 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -060028 * These OMAP modules are tied together by various interconnects.
29 * Most of the address and data flow between modules is via OCP-based
30 * interconnects such as the L3 and L4 buses; but there are other
31 * interconnects that distribute the hardware clock tree, handle idle
32 * and reset signaling, supply power, and connect the modules to
33 * various pads or balls on the OMAP package.
34 *
35 * OMAP hwmod provides a consistent way to describe the on-chip
36 * hardware blocks and their integration into the rest of the chip.
37 * This description can be automatically generated from the TI
38 * hardware database. OMAP hwmod provides a standard, consistent API
39 * to reset, enable, idle, and disable these hardware blocks. And
40 * hwmod provides a way for other core code, such as the Linux device
41 * code or the OMAP power management and address space mapping code,
42 * to query the hardware database.
43 *
44 * Using hwmod
45 * -----------
46 * Drivers won't call hwmod functions directly. That is done by the
47 * omap_device code, and in rare occasions, by custom integration code
48 * in arch/arm/ *omap*. The omap_device code includes functions to
49 * build a struct platform_device using omap_hwmod data, and that is
50 * currently how hwmod data is communicated to drivers and to the
51 * Linux driver model. Most drivers will call omap_hwmod functions only
52 * indirectly, via pm_runtime*() functions.
53 *
54 * From a layering perspective, here is where the OMAP hwmod code
55 * fits into the kernel software stack:
56 *
57 * +-------------------------------+
58 * | Device driver code |
59 * | (e.g., drivers/) |
60 * +-------------------------------+
61 * | Linux driver model |
62 * | (platform_device / |
63 * | platform_driver data/code) |
64 * +-------------------------------+
65 * | OMAP core-driver integration |
66 * |(arch/arm/mach-omap2/devices.c)|
67 * +-------------------------------+
68 * | omap_device code |
69 * | (../plat-omap/omap_device.c) |
70 * +-------------------------------+
71 * ----> | omap_hwmod code/data | <-----
72 * | (../mach-omap2/omap_hwmod*) |
73 * +-------------------------------+
74 * | OMAP clock/PRCM/register fns |
75 * | (__raw_{read,write}l, clk*) |
76 * +-------------------------------+
77 *
78 * Device drivers should not contain any OMAP-specific code or data in
79 * them. They should only contain code to operate the IP block that
80 * the driver is responsible for. This is because these IP blocks can
81 * also appear in other SoCs, either from TI (such as DaVinci) or from
82 * other manufacturers; and drivers should be reusable across other
83 * platforms.
84 *
85 * The OMAP hwmod code also will attempt to reset and idle all on-chip
86 * devices upon boot. The goal here is for the kernel to be
87 * completely self-reliant and independent from bootloaders. This is
88 * to ensure a repeatable configuration, both to ensure consistent
89 * runtime behavior, and to make it easier for others to reproduce
90 * bugs.
91 *
92 * OMAP module activity states
93 * ---------------------------
94 * The hwmod code considers modules to be in one of several activity
95 * states. IP blocks start out in an UNKNOWN state, then once they
96 * are registered via the hwmod code, proceed to the REGISTERED state.
97 * Once their clock names are resolved to clock pointers, the module
98 * enters the CLKS_INITED state; and finally, once the module has been
99 * reset and the integration registers programmed, the INITIALIZED state
100 * is entered. The hwmod code will then place the module into either
101 * the IDLE state to save power, or in the case of a critical system
102 * module, the ENABLED state.
103 *
104 * OMAP core integration code can then call omap_hwmod*() functions
105 * directly to move the module between the IDLE, ENABLED, and DISABLED
106 * states, as needed. This is done during both the PM idle loop, and
107 * in the OMAP core integration code's implementation of the PM runtime
108 * functions.
109 *
110 * References
111 * ----------
112 * This is a partial list.
Paul Walmsley63c85232009-09-03 20:14:03 +0300113 * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
114 * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
115 * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
116 * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
117 * - Open Core Protocol Specification 2.2
118 *
119 * To do:
Paul Walmsley63c85232009-09-03 20:14:03 +0300120 * - handle IO mapping
121 * - bus throughput & module latency measurement code
122 *
123 * XXX add tests at the beginning of each function to ensure the hwmod is
124 * in the appropriate state
125 * XXX error return values should be checked to ensure that they are
126 * appropriate
127 */
128#undef DEBUG
129
130#include <linux/kernel.h>
131#include <linux/errno.h>
132#include <linux/io.h>
133#include <linux/clk.h>
134#include <linux/delay.h>
135#include <linux/err.h>
136#include <linux/list.h>
137#include <linux/mutex.h>
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -0700138#include <linux/spinlock.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300139
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -0700140#include <plat/common.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -0700141#include <plat/cpu.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -0700142#include "clockdomain.h"
Paul Walmsley72e06d02010-12-21 21:05:16 -0700143#include "powerdomain.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700144#include <plat/clock.h>
145#include <plat/omap_hwmod.h>
Benoît Cousson5365efb2010-09-21 10:34:11 -0600146#include <plat/prcm.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300147
Paul Walmsley59fb6592010-12-21 15:30:55 -0700148#include "cm2xxx_3xxx.h"
Benoit Coussond0f06312011-07-10 05:56:30 -0600149#include "cminst44xx.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -0700150#include "prm2xxx_3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -0700151#include "prm44xx.h"
Benoit Coussoneaac3292011-07-10 05:56:31 -0600152#include "prminst44xx.h"
Tony Lindgren8d9af882010-12-22 18:42:35 -0800153#include "mux.h"
Paul Walmsley63c85232009-09-03 20:14:03 +0300154
Benoît Cousson5365efb2010-09-21 10:34:11 -0600155/* Maximum microseconds to wait for OMAP module to softreset */
156#define MAX_MODULE_SOFTRESET_WAIT 10000
Paul Walmsley63c85232009-09-03 20:14:03 +0300157
158/* Name of the OMAP hwmod for the MPU */
Benoit Cousson5c2c0292010-05-20 12:31:10 -0600159#define MPU_INITIATOR_NAME "mpu"
Paul Walmsley63c85232009-09-03 20:14:03 +0300160
161/* omap_hwmod_list contains all registered struct omap_hwmods */
162static LIST_HEAD(omap_hwmod_list);
163
Paul Walmsley63c85232009-09-03 20:14:03 +0300164/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
165static struct omap_hwmod *mpu_oh;
166
Paul Walmsley63c85232009-09-03 20:14:03 +0300167
168/* Private functions */
169
170/**
171 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
172 * @oh: struct omap_hwmod *
173 *
174 * Load the current value of the hwmod OCP_SYSCONFIG register into the
175 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
176 * OCP_SYSCONFIG register or 0 upon success.
177 */
178static int _update_sysc_cache(struct omap_hwmod *oh)
179{
Paul Walmsley43b40992010-02-22 22:09:34 -0700180 if (!oh->class->sysc) {
181 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 +0300182 return -EINVAL;
183 }
184
185 /* XXX ensure module interface clock is up */
186
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -0700187 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300188
Paul Walmsley43b40992010-02-22 22:09:34 -0700189 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
Thara Gopinath883edfd2010-01-19 17:30:51 -0700190 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300191
192 return 0;
193}
194
195/**
196 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
197 * @v: OCP_SYSCONFIG value to write
198 * @oh: struct omap_hwmod *
199 *
Paul Walmsley43b40992010-02-22 22:09:34 -0700200 * Write @v into the module class' OCP_SYSCONFIG register, if it has
201 * one. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +0300202 */
203static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
204{
Paul Walmsley43b40992010-02-22 22:09:34 -0700205 if (!oh->class->sysc) {
206 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 +0300207 return;
208 }
209
210 /* XXX ensure module interface clock is up */
211
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700212 /* Module might have lost context, always update cache and register */
213 oh->_sysc_cache = v;
214 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300215}
216
217/**
218 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
219 * @oh: struct omap_hwmod *
220 * @standbymode: MIDLEMODE field bits
221 * @v: pointer to register contents to modify
222 *
223 * Update the master standby mode bits in @v to be @standbymode for
224 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
225 * upon error or 0 upon success.
226 */
227static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
228 u32 *v)
229{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700230 u32 mstandby_mask;
231 u8 mstandby_shift;
232
Paul Walmsley43b40992010-02-22 22:09:34 -0700233 if (!oh->class->sysc ||
234 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300235 return -EINVAL;
236
Paul Walmsley43b40992010-02-22 22:09:34 -0700237 if (!oh->class->sysc->sysc_fields) {
238 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700239 return -EINVAL;
240 }
241
Paul Walmsley43b40992010-02-22 22:09:34 -0700242 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700243 mstandby_mask = (0x3 << mstandby_shift);
244
245 *v &= ~mstandby_mask;
246 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300247
248 return 0;
249}
250
251/**
252 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
253 * @oh: struct omap_hwmod *
254 * @idlemode: SIDLEMODE field bits
255 * @v: pointer to register contents to modify
256 *
257 * Update the slave idle mode bits in @v to be @idlemode for the @oh
258 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
259 * or 0 upon success.
260 */
261static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
262{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700263 u32 sidle_mask;
264 u8 sidle_shift;
265
Paul Walmsley43b40992010-02-22 22:09:34 -0700266 if (!oh->class->sysc ||
267 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300268 return -EINVAL;
269
Paul Walmsley43b40992010-02-22 22:09:34 -0700270 if (!oh->class->sysc->sysc_fields) {
271 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700272 return -EINVAL;
273 }
274
Paul Walmsley43b40992010-02-22 22:09:34 -0700275 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700276 sidle_mask = (0x3 << sidle_shift);
277
278 *v &= ~sidle_mask;
279 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300280
281 return 0;
282}
283
284/**
285 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
286 * @oh: struct omap_hwmod *
287 * @clockact: CLOCKACTIVITY field bits
288 * @v: pointer to register contents to modify
289 *
290 * Update the clockactivity mode bits in @v to be @clockact for the
291 * @oh hwmod. Used for additional powersaving on some modules. Does
292 * not write to the hardware. Returns -EINVAL upon error or 0 upon
293 * success.
294 */
295static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
296{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700297 u32 clkact_mask;
298 u8 clkact_shift;
299
Paul Walmsley43b40992010-02-22 22:09:34 -0700300 if (!oh->class->sysc ||
301 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300302 return -EINVAL;
303
Paul Walmsley43b40992010-02-22 22:09:34 -0700304 if (!oh->class->sysc->sysc_fields) {
305 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700306 return -EINVAL;
307 }
308
Paul Walmsley43b40992010-02-22 22:09:34 -0700309 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700310 clkact_mask = (0x3 << clkact_shift);
311
312 *v &= ~clkact_mask;
313 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300314
315 return 0;
316}
317
318/**
319 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
320 * @oh: struct omap_hwmod *
321 * @v: pointer to register contents to modify
322 *
323 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
324 * error or 0 upon success.
325 */
326static int _set_softreset(struct omap_hwmod *oh, u32 *v)
327{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700328 u32 softrst_mask;
329
Paul Walmsley43b40992010-02-22 22:09:34 -0700330 if (!oh->class->sysc ||
331 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300332 return -EINVAL;
333
Paul Walmsley43b40992010-02-22 22:09:34 -0700334 if (!oh->class->sysc->sysc_fields) {
335 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700336 return -EINVAL;
337 }
338
Paul Walmsley43b40992010-02-22 22:09:34 -0700339 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700340
341 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300342
343 return 0;
344}
345
346/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700347 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
348 * @oh: struct omap_hwmod *
349 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
350 * @v: pointer to register contents to modify
351 *
352 * Update the module autoidle bit in @v to be @autoidle for the @oh
353 * hwmod. The autoidle bit controls whether the module can gate
354 * internal clocks automatically when it isn't doing anything; the
355 * exact function of this bit varies on a per-module basis. This
356 * function does not write to the hardware. Returns -EINVAL upon
357 * error or 0 upon success.
358 */
359static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
360 u32 *v)
361{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700362 u32 autoidle_mask;
363 u8 autoidle_shift;
364
Paul Walmsley43b40992010-02-22 22:09:34 -0700365 if (!oh->class->sysc ||
366 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700367 return -EINVAL;
368
Paul Walmsley43b40992010-02-22 22:09:34 -0700369 if (!oh->class->sysc->sysc_fields) {
370 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700371 return -EINVAL;
372 }
373
Paul Walmsley43b40992010-02-22 22:09:34 -0700374 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700375 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700376
377 *v &= ~autoidle_mask;
378 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700379
380 return 0;
381}
382
383/**
Govindraj Receec002011-12-16 14:36:58 -0700384 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
385 * @oh: struct omap_hwmod *
386 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
387 *
388 * Set or clear the I/O pad wakeup flag in the mux entries for the
389 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
390 * in memory. If the hwmod is currently idled, and the new idle
391 * values don't match the previous ones, this function will also
392 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
393 * currently idled, this function won't touch the hardware: the new
394 * mux settings are written to the SCM PADCTRL registers when the
395 * hwmod is idled. No return value.
396 */
397static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
398{
399 struct omap_device_pad *pad;
400 bool change = false;
401 u16 prev_idle;
402 int j;
403
404 if (!oh->mux || !oh->mux->enabled)
405 return;
406
407 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
408 pad = oh->mux->pads_dynamic[j];
409
410 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
411 continue;
412
413 prev_idle = pad->idle;
414
415 if (set_wake)
416 pad->idle |= OMAP_WAKEUP_EN;
417 else
418 pad->idle &= ~OMAP_WAKEUP_EN;
419
420 if (prev_idle != pad->idle)
421 change = true;
422 }
423
424 if (change && oh->_state == _HWMOD_STATE_IDLE)
425 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
426}
427
428/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300429 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
430 * @oh: struct omap_hwmod *
431 *
432 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
433 * upon error or 0 upon success.
434 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700435static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300436{
Paul Walmsley43b40992010-02-22 22:09:34 -0700437 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700438 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200439 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
440 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300441 return -EINVAL;
442
Paul Walmsley43b40992010-02-22 22:09:34 -0700443 if (!oh->class->sysc->sysc_fields) {
444 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700445 return -EINVAL;
446 }
447
Benoit Cousson1fe74112011-07-01 22:54:03 +0200448 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
449 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300450
Benoit Cousson86009eb2010-12-21 21:31:28 -0700451 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
452 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200453 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
454 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700455
Paul Walmsley63c85232009-09-03 20:14:03 +0300456 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
457
458 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
459
460 return 0;
461}
462
463/**
464 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
465 * @oh: struct omap_hwmod *
466 *
467 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
468 * upon error or 0 upon success.
469 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700470static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300471{
Paul Walmsley43b40992010-02-22 22:09:34 -0700472 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700473 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200474 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
475 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300476 return -EINVAL;
477
Paul Walmsley43b40992010-02-22 22:09:34 -0700478 if (!oh->class->sysc->sysc_fields) {
479 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700480 return -EINVAL;
481 }
482
Benoit Cousson1fe74112011-07-01 22:54:03 +0200483 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
484 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300485
Benoit Cousson86009eb2010-12-21 21:31:28 -0700486 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
487 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200488 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
489 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700490
Paul Walmsley63c85232009-09-03 20:14:03 +0300491 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
492
493 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
494
495 return 0;
496}
497
498/**
499 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
500 * @oh: struct omap_hwmod *
501 *
502 * Prevent the hardware module @oh from entering idle while the
503 * hardare module initiator @init_oh is active. Useful when a module
504 * will be accessed by a particular initiator (e.g., if a module will
505 * be accessed by the IVA, there should be a sleepdep between the IVA
506 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700507 * mode. If the clockdomain is marked as not needing autodeps, return
508 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
509 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300510 */
511static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
512{
513 if (!oh->_clk)
514 return -EINVAL;
515
Paul Walmsley570b54c2011-03-10 03:50:09 -0700516 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
517 return 0;
518
Paul Walmsley55ed9692010-01-26 20:12:59 -0700519 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300520}
521
522/**
523 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
524 * @oh: struct omap_hwmod *
525 *
526 * Allow the hardware module @oh to enter idle while the hardare
527 * module initiator @init_oh is active. Useful when a module will not
528 * be accessed by a particular initiator (e.g., if a module will not
529 * be accessed by the IVA, there should be no sleepdep between the IVA
530 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700531 * mode. If the clockdomain is marked as not needing autodeps, return
532 * 0 without doing anything. Returns -EINVAL upon error or passes
533 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300534 */
535static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
536{
537 if (!oh->_clk)
538 return -EINVAL;
539
Paul Walmsley570b54c2011-03-10 03:50:09 -0700540 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
541 return 0;
542
Paul Walmsley55ed9692010-01-26 20:12:59 -0700543 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300544}
545
546/**
547 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
548 * @oh: struct omap_hwmod *
549 *
550 * Called from _init_clocks(). Populates the @oh _clk (main
551 * functional clock pointer) if a main_clk is present. Returns 0 on
552 * success or -EINVAL on error.
553 */
554static int _init_main_clk(struct omap_hwmod *oh)
555{
Paul Walmsley63c85232009-09-03 20:14:03 +0300556 int ret = 0;
557
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700558 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300559 return 0;
560
Benoit Cousson63403382010-05-20 12:31:10 -0600561 oh->_clk = omap_clk_get_by_name(oh->main_clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600562 if (!oh->_clk) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600563 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
564 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600565 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600566 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300567
Benoit Cousson63403382010-05-20 12:31:10 -0600568 if (!oh->_clk->clkdm)
569 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
570 oh->main_clk, oh->_clk->name);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700571
Paul Walmsley63c85232009-09-03 20:14:03 +0300572 return ret;
573}
574
575/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600576 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300577 * @oh: struct omap_hwmod *
578 *
579 * Called from _init_clocks(). Populates the @oh OCP slave interface
580 * clock pointers. Returns 0 on success or -EINVAL on error.
581 */
582static int _init_interface_clks(struct omap_hwmod *oh)
583{
Paul Walmsley63c85232009-09-03 20:14:03 +0300584 struct clk *c;
585 int i;
586 int ret = 0;
587
588 if (oh->slaves_cnt == 0)
589 return 0;
590
Benoit Cousson682fdc92010-05-20 12:31:09 -0600591 for (i = 0; i < oh->slaves_cnt; i++) {
592 struct omap_hwmod_ocp_if *os = oh->slaves[i];
593
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700594 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300595 continue;
596
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700597 c = omap_clk_get_by_name(os->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600598 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600599 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
600 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300601 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600602 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300603 os->_clk = c;
604 }
605
606 return ret;
607}
608
609/**
610 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
611 * @oh: struct omap_hwmod *
612 *
613 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
614 * clock pointers. Returns 0 on success or -EINVAL on error.
615 */
616static int _init_opt_clks(struct omap_hwmod *oh)
617{
618 struct omap_hwmod_opt_clk *oc;
619 struct clk *c;
620 int i;
621 int ret = 0;
622
623 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700624 c = omap_clk_get_by_name(oc->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600625 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600626 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
627 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300628 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600629 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300630 oc->_clk = c;
631 }
632
633 return ret;
634}
635
636/**
637 * _enable_clocks - enable hwmod main clock and interface clocks
638 * @oh: struct omap_hwmod *
639 *
640 * Enables all clocks necessary for register reads and writes to succeed
641 * on the hwmod @oh. Returns 0.
642 */
643static int _enable_clocks(struct omap_hwmod *oh)
644{
Paul Walmsley63c85232009-09-03 20:14:03 +0300645 int i;
646
647 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
648
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600649 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300650 clk_enable(oh->_clk);
651
652 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -0600653 for (i = 0; i < oh->slaves_cnt; i++) {
654 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +0300655 struct clk *c = os->_clk;
656
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600657 if (c && (os->flags & OCPIF_SWSUP_IDLE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300658 clk_enable(c);
659 }
660 }
661
662 /* The opt clocks are controlled by the device driver. */
663
664 return 0;
665}
666
667/**
668 * _disable_clocks - disable hwmod main clock and interface clocks
669 * @oh: struct omap_hwmod *
670 *
671 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
672 */
673static int _disable_clocks(struct omap_hwmod *oh)
674{
Paul Walmsley63c85232009-09-03 20:14:03 +0300675 int i;
676
677 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
678
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600679 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300680 clk_disable(oh->_clk);
681
682 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -0600683 for (i = 0; i < oh->slaves_cnt; i++) {
684 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +0300685 struct clk *c = os->_clk;
686
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600687 if (c && (os->flags & OCPIF_SWSUP_IDLE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300688 clk_disable(c);
689 }
690 }
691
692 /* The opt clocks are controlled by the device driver. */
693
694 return 0;
695}
696
Benoit Cousson96835af2010-09-21 18:57:58 +0200697static void _enable_optional_clocks(struct omap_hwmod *oh)
698{
699 struct omap_hwmod_opt_clk *oc;
700 int i;
701
702 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
703
704 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
705 if (oc->_clk) {
706 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
707 oc->_clk->name);
708 clk_enable(oc->_clk);
709 }
710}
711
712static void _disable_optional_clocks(struct omap_hwmod *oh)
713{
714 struct omap_hwmod_opt_clk *oc;
715 int i;
716
717 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
718
719 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
720 if (oc->_clk) {
721 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
722 oc->_clk->name);
723 clk_disable(oc->_clk);
724 }
725}
726
Paul Walmsley63c85232009-09-03 20:14:03 +0300727/**
Benoit Cousson45c38252011-07-10 05:56:33 -0600728 * _enable_module - enable CLKCTRL modulemode on OMAP4
729 * @oh: struct omap_hwmod *
730 *
731 * Enables the PRCM module mode related to the hwmod @oh.
732 * No return value.
733 */
734static void _enable_module(struct omap_hwmod *oh)
735{
736 /* The module mode does not exist prior OMAP4 */
737 if (cpu_is_omap24xx() || cpu_is_omap34xx())
738 return;
739
740 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
741 return;
742
743 pr_debug("omap_hwmod: %s: _enable_module: %d\n",
744 oh->name, oh->prcm.omap4.modulemode);
745
746 omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
747 oh->clkdm->prcm_partition,
748 oh->clkdm->cm_inst,
749 oh->clkdm->clkdm_offs,
750 oh->prcm.omap4.clkctrl_offs);
751}
752
753/**
754 * _disable_module - enable CLKCTRL modulemode on OMAP4
755 * @oh: struct omap_hwmod *
756 *
757 * Disable the PRCM module mode related to the hwmod @oh.
758 * No return value.
759 */
760static void _disable_module(struct omap_hwmod *oh)
761{
762 /* The module mode does not exist prior OMAP4 */
763 if (cpu_is_omap24xx() || cpu_is_omap34xx())
764 return;
765
766 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
767 return;
768
769 pr_debug("omap_hwmod: %s: _disable_module\n", oh->name);
770
771 omap4_cminst_module_disable(oh->clkdm->prcm_partition,
772 oh->clkdm->cm_inst,
773 oh->clkdm->clkdm_offs,
774 oh->prcm.omap4.clkctrl_offs);
775}
776
777/**
Paul Walmsley212738a2011-07-09 19:14:06 -0600778 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
779 * @oh: struct omap_hwmod *oh
780 *
781 * Count and return the number of MPU IRQs associated with the hwmod
782 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
783 * NULL.
784 */
785static int _count_mpu_irqs(struct omap_hwmod *oh)
786{
787 struct omap_hwmod_irq_info *ohii;
788 int i = 0;
789
790 if (!oh || !oh->mpu_irqs)
791 return 0;
792
793 do {
794 ohii = &oh->mpu_irqs[i++];
795 } while (ohii->irq != -1);
796
sricharancc1b0762011-11-23 14:35:07 -0800797 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -0600798}
799
800/**
Paul Walmsleybc614952011-07-09 19:14:07 -0600801 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
802 * @oh: struct omap_hwmod *oh
803 *
804 * Count and return the number of SDMA request lines associated with
805 * the hwmod @oh. Used to allocate struct resource data. Returns 0
806 * if @oh is NULL.
807 */
808static int _count_sdma_reqs(struct omap_hwmod *oh)
809{
810 struct omap_hwmod_dma_info *ohdi;
811 int i = 0;
812
813 if (!oh || !oh->sdma_reqs)
814 return 0;
815
816 do {
817 ohdi = &oh->sdma_reqs[i++];
818 } while (ohdi->dma_req != -1);
819
sricharancc1b0762011-11-23 14:35:07 -0800820 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -0600821}
822
823/**
Paul Walmsley78183f32011-07-09 19:14:05 -0600824 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
825 * @oh: struct omap_hwmod *oh
826 *
827 * Count and return the number of address space ranges associated with
828 * the hwmod @oh. Used to allocate struct resource data. Returns 0
829 * if @oh is NULL.
830 */
831static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
832{
833 struct omap_hwmod_addr_space *mem;
834 int i = 0;
835
836 if (!os || !os->addr)
837 return 0;
838
839 do {
840 mem = &os->addr[i++];
841 } while (mem->pa_start != mem->pa_end);
842
sricharancc1b0762011-11-23 14:35:07 -0800843 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -0600844}
845
846/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300847 * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
848 * @oh: struct omap_hwmod *
849 *
850 * Returns the array index of the OCP slave port that the MPU
851 * addresses the device on, or -EINVAL upon error or not found.
852 */
Benoit Cousson01592df2010-12-21 21:31:28 -0700853static int __init _find_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300854{
Paul Walmsley63c85232009-09-03 20:14:03 +0300855 int i;
856 int found = 0;
857
858 if (!oh || oh->slaves_cnt == 0)
859 return -EINVAL;
860
Benoit Cousson682fdc92010-05-20 12:31:09 -0600861 for (i = 0; i < oh->slaves_cnt; i++) {
862 struct omap_hwmod_ocp_if *os = oh->slaves[i];
863
Paul Walmsley63c85232009-09-03 20:14:03 +0300864 if (os->user & OCP_USER_MPU) {
865 found = 1;
866 break;
867 }
868 }
869
870 if (found)
871 pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
872 oh->name, i);
873 else
874 pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
875 oh->name);
876
877 return (found) ? i : -EINVAL;
878}
879
880/**
881 * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
882 * @oh: struct omap_hwmod *
883 *
884 * Return the virtual address of the base of the register target of
885 * device @oh, or NULL on error.
886 */
Benoit Cousson01592df2010-12-21 21:31:28 -0700887static void __iomem * __init _find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
Paul Walmsley63c85232009-09-03 20:14:03 +0300888{
889 struct omap_hwmod_ocp_if *os;
890 struct omap_hwmod_addr_space *mem;
Paul Walmsley78183f32011-07-09 19:14:05 -0600891 int i = 0, found = 0;
Tony Lindgren986a13f2009-10-19 15:25:22 -0700892 void __iomem *va_start;
Paul Walmsley63c85232009-09-03 20:14:03 +0300893
894 if (!oh || oh->slaves_cnt == 0)
895 return NULL;
896
Benoit Cousson682fdc92010-05-20 12:31:09 -0600897 os = oh->slaves[index];
Paul Walmsley63c85232009-09-03 20:14:03 +0300898
Paul Walmsley78183f32011-07-09 19:14:05 -0600899 if (!os->addr)
900 return NULL;
901
902 do {
903 mem = &os->addr[i++];
904 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +0300905 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -0600906 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +0300907
Tony Lindgren986a13f2009-10-19 15:25:22 -0700908 if (found) {
909 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
910 if (!va_start) {
911 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
912 return NULL;
913 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300914 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
Tony Lindgren986a13f2009-10-19 15:25:22 -0700915 oh->name, va_start);
916 } else {
Paul Walmsley63c85232009-09-03 20:14:03 +0300917 pr_debug("omap_hwmod: %s: no MPU register target found\n",
918 oh->name);
Tony Lindgren986a13f2009-10-19 15:25:22 -0700919 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300920
Tony Lindgren986a13f2009-10-19 15:25:22 -0700921 return (found) ? va_start : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +0300922}
923
924/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600925 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +0300926 * @oh: struct omap_hwmod *
927 *
928 * If module is marked as SWSUP_SIDLE, force the module out of slave
929 * idle; otherwise, configure it for smart-idle. If module is marked
930 * as SWSUP_MSUSPEND, force the module out of master standby;
931 * otherwise, configure it for smart-standby. No return value.
932 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600933static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +0300934{
Paul Walmsley43b40992010-02-22 22:09:34 -0700935 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +0300936 u32 v;
937
Paul Walmsley43b40992010-02-22 22:09:34 -0700938 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +0300939 return;
940
941 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -0700942 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +0300943
Paul Walmsley43b40992010-02-22 22:09:34 -0700944 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +0300945 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
946 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
947 _set_slave_idlemode(oh, idlemode, &v);
948 }
949
Paul Walmsley43b40992010-02-22 22:09:34 -0700950 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +0200951 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
952 idlemode = HWMOD_IDLEMODE_NO;
953 } else {
954 if (sf & SYSC_HAS_ENAWAKEUP)
955 _enable_wakeup(oh, &v);
956 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
957 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
958 else
959 idlemode = HWMOD_IDLEMODE_SMART;
960 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300961 _set_master_standbymode(oh, idlemode, &v);
962 }
963
Paul Walmsleya16b1f72009-12-08 16:34:17 -0700964 /*
965 * XXX The clock framework should handle this, by
966 * calling into this code. But this must wait until the
967 * clock structures are tagged with omap_hwmod entries
968 */
Paul Walmsley43b40992010-02-22 22:09:34 -0700969 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
970 (sf & SYSC_HAS_CLOCKACTIVITY))
971 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +0300972
Rajendra Nayak9980ce52010-09-21 19:58:30 +0530973 /* If slave is in SMARTIDLE, also enable wakeup */
974 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700975 _enable_wakeup(oh, &v);
976
977 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -0600978
979 /*
980 * Set the autoidle bit only after setting the smartidle bit
981 * Setting this will not have any impact on the other modules.
982 */
983 if (sf & SYSC_HAS_AUTOIDLE) {
984 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
985 0 : 1;
986 _set_module_autoidle(oh, idlemode, &v);
987 _write_sysconfig(v, oh);
988 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300989}
990
991/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -0600992 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +0300993 * @oh: struct omap_hwmod *
994 *
995 * If module is marked as SWSUP_SIDLE, force the module into slave
996 * idle; otherwise, configure it for smart-idle. If module is marked
997 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
998 * configure it for smart-standby. No return value.
999 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001000static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001001{
Paul Walmsley43b40992010-02-22 22:09:34 -07001002 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001003 u32 v;
1004
Paul Walmsley43b40992010-02-22 22:09:34 -07001005 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001006 return;
1007
1008 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001009 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001010
Paul Walmsley43b40992010-02-22 22:09:34 -07001011 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001012 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1013 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
1014 _set_slave_idlemode(oh, idlemode, &v);
1015 }
1016
Paul Walmsley43b40992010-02-22 22:09:34 -07001017 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001018 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1019 idlemode = HWMOD_IDLEMODE_FORCE;
1020 } else {
1021 if (sf & SYSC_HAS_ENAWAKEUP)
1022 _enable_wakeup(oh, &v);
1023 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1024 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1025 else
1026 idlemode = HWMOD_IDLEMODE_SMART;
1027 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001028 _set_master_standbymode(oh, idlemode, &v);
1029 }
1030
Benoit Cousson86009eb2010-12-21 21:31:28 -07001031 /* If slave is in SMARTIDLE, also enable wakeup */
1032 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1033 _enable_wakeup(oh, &v);
1034
Paul Walmsley63c85232009-09-03 20:14:03 +03001035 _write_sysconfig(v, oh);
1036}
1037
1038/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001039 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001040 * @oh: struct omap_hwmod *
1041 *
1042 * Force the module into slave idle and master suspend. No return
1043 * value.
1044 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001045static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001046{
1047 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001048 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001049
Paul Walmsley43b40992010-02-22 22:09:34 -07001050 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001051 return;
1052
1053 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001054 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001055
Paul Walmsley43b40992010-02-22 22:09:34 -07001056 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001057 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1058
Paul Walmsley43b40992010-02-22 22:09:34 -07001059 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001060 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1061
Paul Walmsley43b40992010-02-22 22:09:34 -07001062 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001063 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001064
1065 _write_sysconfig(v, oh);
1066}
1067
1068/**
1069 * _lookup - find an omap_hwmod by name
1070 * @name: find an omap_hwmod by name
1071 *
1072 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001073 */
1074static struct omap_hwmod *_lookup(const char *name)
1075{
1076 struct omap_hwmod *oh, *temp_oh;
1077
1078 oh = NULL;
1079
1080 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1081 if (!strcmp(name, temp_oh->name)) {
1082 oh = temp_oh;
1083 break;
1084 }
1085 }
1086
1087 return oh;
1088}
Benoit Cousson6ae76992011-07-10 05:56:30 -06001089/**
1090 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1091 * @oh: struct omap_hwmod *
1092 *
1093 * Convert a clockdomain name stored in a struct omap_hwmod into a
1094 * clockdomain pointer, and save it into the struct omap_hwmod.
1095 * return -EINVAL if clkdm_name does not exist or if the lookup failed.
1096 */
1097static int _init_clkdm(struct omap_hwmod *oh)
1098{
1099 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1100 return 0;
1101
1102 if (!oh->clkdm_name) {
1103 pr_warning("omap_hwmod: %s: no clkdm_name\n", oh->name);
1104 return -EINVAL;
1105 }
1106
1107 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1108 if (!oh->clkdm) {
1109 pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
1110 oh->name, oh->clkdm_name);
1111 return -EINVAL;
1112 }
1113
1114 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1115 oh->name, oh->clkdm_name);
1116
1117 return 0;
1118}
Paul Walmsley63c85232009-09-03 20:14:03 +03001119
1120/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001121 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1122 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001123 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -06001124 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001125 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001126 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001127 * Resolves all clock names embedded in the hwmod. Returns 0 on
1128 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001129 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001130static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001131{
1132 int ret = 0;
1133
Paul Walmsley48d54f32011-02-23 00:14:07 -07001134 if (oh->_state != _HWMOD_STATE_REGISTERED)
1135 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001136
1137 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1138
1139 ret |= _init_main_clk(oh);
1140 ret |= _init_interface_clks(oh);
1141 ret |= _init_opt_clks(oh);
Benoit Cousson6ae76992011-07-10 05:56:30 -06001142 ret |= _init_clkdm(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001143
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001144 if (!ret)
1145 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001146 else
1147 pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001148
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001149 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001150}
1151
1152/**
1153 * _wait_target_ready - wait for a module to leave slave idle
1154 * @oh: struct omap_hwmod *
1155 *
1156 * Wait for a module @oh to leave slave idle. Returns 0 if the module
1157 * does not have an IDLEST bit or if the module successfully leaves
1158 * slave idle; otherwise, pass along the return value of the
Benoit Coussond0f06312011-07-10 05:56:30 -06001159 * appropriate *_cm*_wait_module_ready() function.
Paul Walmsley63c85232009-09-03 20:14:03 +03001160 */
1161static int _wait_target_ready(struct omap_hwmod *oh)
1162{
1163 struct omap_hwmod_ocp_if *os;
1164 int ret;
1165
1166 if (!oh)
1167 return -EINVAL;
1168
1169 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1170 return 0;
1171
Benoit Cousson682fdc92010-05-20 12:31:09 -06001172 os = oh->slaves[oh->_mpu_port_index];
Paul Walmsley63c85232009-09-03 20:14:03 +03001173
Benoit Cousson33f7ec82010-05-20 12:31:09 -06001174 if (oh->flags & HWMOD_NO_IDLEST)
Paul Walmsley63c85232009-09-03 20:14:03 +03001175 return 0;
1176
1177 /* XXX check module SIDLEMODE */
1178
1179 /* XXX check clock enable states */
1180
1181 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1182 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
1183 oh->prcm.omap2.idlest_reg_id,
1184 oh->prcm.omap2.idlest_idle_bit);
Paul Walmsley63c85232009-09-03 20:14:03 +03001185 } else if (cpu_is_omap44xx()) {
Benoit Coussond0f06312011-07-10 05:56:30 -06001186 if (!oh->clkdm)
1187 return -EINVAL;
1188
1189 ret = omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
1190 oh->clkdm->cm_inst,
1191 oh->clkdm->clkdm_offs,
1192 oh->prcm.omap4.clkctrl_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001193 } else {
1194 BUG();
1195 };
1196
1197 return ret;
1198}
1199
1200/**
Benoit Cousson11b10342011-07-10 05:56:30 -06001201 * _wait_target_disable - wait for a module to be disabled
1202 * @oh: struct omap_hwmod *
1203 *
1204 * Wait for a module @oh to enter slave idle. Returns 0 if the module
1205 * does not have an IDLEST bit or if the module successfully enters
1206 * slave idle; otherwise, pass along the return value of the
1207 * appropriate *_cm*_wait_module_idle() function.
1208 */
1209static int _wait_target_disable(struct omap_hwmod *oh)
1210{
1211 /* TODO: For now just handle OMAP4+ */
1212 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1213 return 0;
1214
1215 if (!oh)
1216 return -EINVAL;
1217
1218 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1219 return 0;
1220
1221 if (oh->flags & HWMOD_NO_IDLEST)
1222 return 0;
1223
1224 return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
1225 oh->clkdm->cm_inst,
1226 oh->clkdm->clkdm_offs,
1227 oh->prcm.omap4.clkctrl_offs);
1228}
1229
1230/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001231 * _lookup_hardreset - fill register bit info for this hwmod/reset line
Benoît Cousson5365efb2010-09-21 10:34:11 -06001232 * @oh: struct omap_hwmod *
1233 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001234 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
Benoît Cousson5365efb2010-09-21 10:34:11 -06001235 *
1236 * Return the bit position of the reset line that match the
1237 * input name. Return -ENOENT if not found.
1238 */
omar ramirezcc1226e2011-03-04 13:32:44 -07001239static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1240 struct omap_hwmod_rst_info *ohri)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001241{
1242 int i;
1243
1244 for (i = 0; i < oh->rst_lines_cnt; i++) {
1245 const char *rst_line = oh->rst_lines[i].name;
1246 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001247 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1248 ohri->st_shift = oh->rst_lines[i].st_shift;
1249 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1250 oh->name, __func__, rst_line, ohri->rst_shift,
1251 ohri->st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001252
omar ramirezcc1226e2011-03-04 13:32:44 -07001253 return 0;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001254 }
1255 }
1256
1257 return -ENOENT;
1258}
1259
1260/**
1261 * _assert_hardreset - assert the HW reset line of submodules
1262 * contained in the hwmod module.
1263 * @oh: struct omap_hwmod *
1264 * @name: name of the reset line to lookup and assert
1265 *
1266 * Some IP like dsp, ipu or iva contain processor that require
1267 * an HW reset line to be assert / deassert in order to enable fully
1268 * the IP.
1269 */
1270static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1271{
omar ramirezcc1226e2011-03-04 13:32:44 -07001272 struct omap_hwmod_rst_info ohri;
1273 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001274
1275 if (!oh)
1276 return -EINVAL;
1277
omar ramirezcc1226e2011-03-04 13:32:44 -07001278 ret = _lookup_hardreset(oh, name, &ohri);
1279 if (IS_ERR_VALUE(ret))
1280 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001281
1282 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1283 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001284 ohri.rst_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001285 else if (cpu_is_omap44xx())
Benoit Coussoneaac3292011-07-10 05:56:31 -06001286 return omap4_prminst_assert_hardreset(ohri.rst_shift,
1287 oh->clkdm->pwrdm.ptr->prcm_partition,
1288 oh->clkdm->pwrdm.ptr->prcm_offs,
1289 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001290 else
1291 return -EINVAL;
1292}
1293
1294/**
1295 * _deassert_hardreset - deassert the HW reset line of submodules contained
1296 * in the hwmod module.
1297 * @oh: struct omap_hwmod *
1298 * @name: name of the reset line to look up and deassert
1299 *
1300 * Some IP like dsp, ipu or iva contain processor that require
1301 * an HW reset line to be assert / deassert in order to enable fully
1302 * the IP.
1303 */
1304static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1305{
omar ramirezcc1226e2011-03-04 13:32:44 -07001306 struct omap_hwmod_rst_info ohri;
1307 int ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001308
1309 if (!oh)
1310 return -EINVAL;
1311
omar ramirezcc1226e2011-03-04 13:32:44 -07001312 ret = _lookup_hardreset(oh, name, &ohri);
1313 if (IS_ERR_VALUE(ret))
1314 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001315
omar ramirezcc1226e2011-03-04 13:32:44 -07001316 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1317 ret = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
1318 ohri.rst_shift,
1319 ohri.st_shift);
1320 } else if (cpu_is_omap44xx()) {
1321 if (ohri.st_shift)
1322 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
1323 oh->name, name);
Benoit Coussoneaac3292011-07-10 05:56:31 -06001324 ret = omap4_prminst_deassert_hardreset(ohri.rst_shift,
1325 oh->clkdm->pwrdm.ptr->prcm_partition,
1326 oh->clkdm->pwrdm.ptr->prcm_offs,
1327 oh->prcm.omap4.rstctrl_offs);
omar ramirezcc1226e2011-03-04 13:32:44 -07001328 } else {
Benoît Cousson5365efb2010-09-21 10:34:11 -06001329 return -EINVAL;
omar ramirezcc1226e2011-03-04 13:32:44 -07001330 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001331
omar ramirezcc1226e2011-03-04 13:32:44 -07001332 if (ret == -EBUSY)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001333 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1334
omar ramirezcc1226e2011-03-04 13:32:44 -07001335 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001336}
1337
1338/**
1339 * _read_hardreset - read the HW reset line state of submodules
1340 * contained in the hwmod module
1341 * @oh: struct omap_hwmod *
1342 * @name: name of the reset line to look up and read
1343 *
1344 * Return the state of the reset line.
1345 */
1346static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1347{
omar ramirezcc1226e2011-03-04 13:32:44 -07001348 struct omap_hwmod_rst_info ohri;
1349 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001350
1351 if (!oh)
1352 return -EINVAL;
1353
omar ramirezcc1226e2011-03-04 13:32:44 -07001354 ret = _lookup_hardreset(oh, name, &ohri);
1355 if (IS_ERR_VALUE(ret))
1356 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001357
1358 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1359 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001360 ohri.st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001361 } else if (cpu_is_omap44xx()) {
Benoit Coussoneaac3292011-07-10 05:56:31 -06001362 return omap4_prminst_is_hardreset_asserted(ohri.rst_shift,
1363 oh->clkdm->pwrdm.ptr->prcm_partition,
1364 oh->clkdm->pwrdm.ptr->prcm_offs,
1365 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001366 } else {
1367 return -EINVAL;
1368 }
1369}
1370
1371/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001372 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001373 * @oh: struct omap_hwmod *
1374 *
1375 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Kevin Hilman12b1fdb2010-09-21 10:34:09 -06001376 * enabled for this to work. Returns -EINVAL if the hwmod cannot be
1377 * reset this way or if the hwmod is in the wrong state, -ETIMEDOUT if
1378 * the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001379 *
1380 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001381 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001382 * use the SYSCONFIG softreset bit to provide the status.
1383 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001384 * Note that some IP like McBSP do have reset control but don't have
1385 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001386 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001387static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001388{
Benoit Cousson96835af2010-09-21 18:57:58 +02001389 u32 v;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001390 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001391 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001392
Paul Walmsley43b40992010-02-22 22:09:34 -07001393 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001394 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +03001395 return -EINVAL;
1396
1397 /* clocks must be on for this operation */
1398 if (oh->_state != _HWMOD_STATE_ENABLED) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001399 pr_warning("omap_hwmod: %s: reset can only be entered from "
1400 "enabled state\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001401 return -EINVAL;
1402 }
1403
Benoit Cousson96835af2010-09-21 18:57:58 +02001404 /* For some modules, all optionnal clocks need to be enabled as well */
1405 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1406 _enable_optional_clocks(oh);
1407
Paul Walmsleybd361792010-12-14 12:42:35 -07001408 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001409
1410 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001411 ret = _set_softreset(oh, &v);
1412 if (ret)
1413 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001414 _write_sysconfig(v, oh);
1415
Benoit Cousson2cb06812010-09-21 18:57:59 +02001416 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001417 omap_test_timeout((omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001418 oh->class->sysc->syss_offs)
1419 & SYSS_RESETDONE_MASK),
1420 MAX_MODULE_SOFTRESET_WAIT, c);
1421 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001422 omap_test_timeout(!(omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001423 oh->class->sysc->sysc_offs)
1424 & SYSC_TYPE2_SOFTRESET_MASK),
1425 MAX_MODULE_SOFTRESET_WAIT, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001426
Benoît Cousson5365efb2010-09-21 10:34:11 -06001427 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001428 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1429 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001430 else
Benoît Cousson5365efb2010-09-21 10:34:11 -06001431 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001432
1433 /*
1434 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1435 * _wait_target_ready() or _reset()
1436 */
1437
Benoit Cousson96835af2010-09-21 18:57:58 +02001438 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1439
1440dis_opt_clks:
1441 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1442 _disable_optional_clocks(oh);
1443
1444 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001445}
1446
1447/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001448 * _reset - reset an omap_hwmod
1449 * @oh: struct omap_hwmod *
1450 *
1451 * Resets an omap_hwmod @oh. The default software reset mechanism for
1452 * most OMAP IP blocks is triggered via the OCP_SYSCONFIG.SOFTRESET
1453 * bit. However, some hwmods cannot be reset via this method: some
1454 * are not targets and therefore have no OCP header registers to
1455 * access; others (like the IVA) have idiosyncratic reset sequences.
1456 * So for these relatively rare cases, custom reset code can be
1457 * supplied in the struct omap_hwmod_class .reset function pointer.
1458 * Passes along the return value from either _reset() or the custom
1459 * reset function - these must return -EINVAL if the hwmod cannot be
1460 * reset this way or if the hwmod is in the wrong state, -ETIMEDOUT if
1461 * the module did not reset in time, or 0 upon success.
1462 */
1463static int _reset(struct omap_hwmod *oh)
1464{
1465 int ret;
1466
1467 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1468
1469 ret = (oh->class->reset) ? oh->class->reset(oh) : _ocp_softreset(oh);
1470
1471 return ret;
1472}
1473
1474/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001475 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001476 * @oh: struct omap_hwmod *
1477 *
1478 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001479 * register target. Returns -EINVAL if the hwmod is in the wrong
1480 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001481 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001482static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001483{
1484 int r;
Rajendra Nayak665d0012011-07-10 05:57:07 -06001485 int hwsup = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001486
Benoit Cousson34617e22011-07-01 22:54:07 +02001487 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1488
Paul Walmsley63c85232009-09-03 20:14:03 +03001489 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1490 oh->_state != _HWMOD_STATE_IDLE &&
1491 oh->_state != _HWMOD_STATE_DISABLED) {
1492 WARN(1, "omap_hwmod: %s: enabled state can only be entered "
1493 "from initialized, idle, or disabled state\n", oh->name);
1494 return -EINVAL;
1495 }
1496
Paul Walmsley63c85232009-09-03 20:14:03 +03001497
Benoit Cousson31f62862011-07-01 22:54:05 +02001498 /*
1499 * If an IP contains only one HW reset line, then de-assert it in order
1500 * to allow the module state transition. Otherwise the PRCM will return
1501 * Intransition status, and the init will failed.
1502 */
1503 if ((oh->_state == _HWMOD_STATE_INITIALIZED ||
1504 oh->_state == _HWMOD_STATE_DISABLED) && oh->rst_lines_cnt == 1)
1505 _deassert_hardreset(oh, oh->rst_lines[0].name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001506
Rajendra Nayak665d0012011-07-10 05:57:07 -06001507 /* Mux pins for device runtime if populated */
1508 if (oh->mux && (!oh->mux->enabled ||
1509 ((oh->_state == _HWMOD_STATE_IDLE) &&
1510 oh->mux->pads_dynamic)))
1511 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
Benoit Cousson34617e22011-07-01 22:54:07 +02001512
Rajendra Nayak665d0012011-07-10 05:57:07 -06001513 _add_initiator_dep(oh, mpu_oh);
1514
1515 if (oh->clkdm) {
1516 /*
1517 * A clockdomain must be in SW_SUP before enabling
1518 * completely the module. The clockdomain can be set
1519 * in HW_AUTO only when the module become ready.
1520 */
1521 hwsup = clkdm_in_hwsup(oh->clkdm);
1522 r = clkdm_hwmod_enable(oh->clkdm, oh);
1523 if (r) {
1524 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1525 oh->name, oh->clkdm->name, r);
1526 return r;
1527 }
Benoit Cousson34617e22011-07-01 22:54:07 +02001528 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06001529
1530 _enable_clocks(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001531 _enable_module(oh);
Benoit Cousson34617e22011-07-01 22:54:07 +02001532
Rajendra Nayak665d0012011-07-10 05:57:07 -06001533 r = _wait_target_ready(oh);
1534 if (!r) {
1535 /*
1536 * Set the clockdomain to HW_AUTO only if the target is ready,
1537 * assuming that the previous state was HW_AUTO
1538 */
1539 if (oh->clkdm && hwsup)
1540 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02001541
Rajendra Nayak665d0012011-07-10 05:57:07 -06001542 oh->_state = _HWMOD_STATE_ENABLED;
1543
1544 /* Access the sysconfig only if the target is ready */
1545 if (oh->class->sysc) {
1546 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1547 _update_sysc_cache(oh);
1548 _enable_sysc(oh);
1549 }
1550 } else {
1551 _disable_clocks(oh);
1552 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1553 oh->name, r);
1554
1555 if (oh->clkdm)
1556 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001557 }
1558
Paul Walmsley63c85232009-09-03 20:14:03 +03001559 return r;
1560}
1561
1562/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001563 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001564 * @oh: struct omap_hwmod *
1565 *
1566 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001567 * no further work. Returns -EINVAL if the hwmod is in the wrong
1568 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001569 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001570static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001571{
Benoit Cousson11b10342011-07-10 05:56:30 -06001572 int ret;
1573
Benoit Cousson34617e22011-07-01 22:54:07 +02001574 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1575
Paul Walmsley63c85232009-09-03 20:14:03 +03001576 if (oh->_state != _HWMOD_STATE_ENABLED) {
1577 WARN(1, "omap_hwmod: %s: idle state can only be entered from "
1578 "enabled state\n", oh->name);
1579 return -EINVAL;
1580 }
1581
Paul Walmsley43b40992010-02-22 22:09:34 -07001582 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001583 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001584 _del_initiator_dep(oh, mpu_oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001585 _disable_module(oh);
Benoit Cousson11b10342011-07-10 05:56:30 -06001586 ret = _wait_target_disable(oh);
1587 if (ret)
1588 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1589 oh->name);
Benoit Cousson45c38252011-07-10 05:56:33 -06001590 /*
1591 * The module must be in idle mode before disabling any parents
1592 * clocks. Otherwise, the parent clock might be disabled before
1593 * the module transition is done, and thus will prevent the
1594 * transition to complete properly.
1595 */
1596 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001597 if (oh->clkdm)
1598 clkdm_hwmod_disable(oh->clkdm, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001599
Tony Lindgren8d9af882010-12-22 18:42:35 -08001600 /* Mux pins for device idle if populated */
Tony Lindgren029268e2011-03-11 11:32:25 -08001601 if (oh->mux && oh->mux->pads_dynamic)
Tony Lindgren8d9af882010-12-22 18:42:35 -08001602 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
1603
Paul Walmsley63c85232009-09-03 20:14:03 +03001604 oh->_state = _HWMOD_STATE_IDLE;
1605
1606 return 0;
1607}
1608
1609/**
Kishon Vijay Abraham I95992172011-03-10 03:50:08 -07001610 * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
1611 * @oh: struct omap_hwmod *
1612 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
1613 *
1614 * Sets the IP block's OCP autoidle bit in hardware, and updates our
1615 * local copy. Intended to be used by drivers that require
1616 * direct manipulation of the AUTOIDLE bits.
1617 * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
1618 * along the return value from _set_module_autoidle().
1619 *
1620 * Any users of this function should be scrutinized carefully.
1621 */
1622int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
1623{
1624 u32 v;
1625 int retval = 0;
1626 unsigned long flags;
1627
1628 if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
1629 return -EINVAL;
1630
1631 spin_lock_irqsave(&oh->_lock, flags);
1632
1633 v = oh->_sysc_cache;
1634
1635 retval = _set_module_autoidle(oh, autoidle, &v);
1636
1637 if (!retval)
1638 _write_sysconfig(v, oh);
1639
1640 spin_unlock_irqrestore(&oh->_lock, flags);
1641
1642 return retval;
1643}
1644
1645/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001646 * _shutdown - shutdown an omap_hwmod
1647 * @oh: struct omap_hwmod *
1648 *
1649 * Shut down an omap_hwmod @oh. This should be called when the driver
1650 * used for the hwmod is removed or unloaded or if the driver is not
1651 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1652 * state or returns 0.
1653 */
1654static int _shutdown(struct omap_hwmod *oh)
1655{
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001656 int ret;
1657 u8 prev_state;
1658
Paul Walmsley63c85232009-09-03 20:14:03 +03001659 if (oh->_state != _HWMOD_STATE_IDLE &&
1660 oh->_state != _HWMOD_STATE_ENABLED) {
1661 WARN(1, "omap_hwmod: %s: disabled state can only be entered "
1662 "from idle, or enabled state\n", oh->name);
1663 return -EINVAL;
1664 }
1665
1666 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
1667
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001668 if (oh->class->pre_shutdown) {
1669 prev_state = oh->_state;
1670 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001671 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001672 ret = oh->class->pre_shutdown(oh);
1673 if (ret) {
1674 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001675 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001676 return ret;
1677 }
1678 }
1679
Miguel Vadillo6481c732011-07-01 22:54:02 +02001680 if (oh->class->sysc) {
1681 if (oh->_state == _HWMOD_STATE_IDLE)
1682 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001683 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02001684 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001685
Benoit Cousson3827f942010-09-21 10:34:08 -06001686 /* clocks and deps are already disabled in idle */
1687 if (oh->_state == _HWMOD_STATE_ENABLED) {
1688 _del_initiator_dep(oh, mpu_oh);
1689 /* XXX what about the other system initiators here? dma, dsp */
Benoit Cousson45c38252011-07-10 05:56:33 -06001690 _disable_module(oh);
Benoit Cousson11b10342011-07-10 05:56:30 -06001691 ret = _wait_target_disable(oh);
1692 if (ret)
1693 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1694 oh->name);
Benoit Cousson45c38252011-07-10 05:56:33 -06001695 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001696 if (oh->clkdm)
1697 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06001698 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001699 /* XXX Should this code also force-disable the optional clocks? */
1700
Benoit Cousson31f62862011-07-01 22:54:05 +02001701 /*
1702 * If an IP contains only one HW reset line, then assert it
1703 * after disabling the clocks and before shutting down the IP.
1704 */
1705 if (oh->rst_lines_cnt == 1)
1706 _assert_hardreset(oh, oh->rst_lines[0].name);
1707
Tony Lindgren8d9af882010-12-22 18:42:35 -08001708 /* Mux pins to safe mode or use populated off mode values */
1709 if (oh->mux)
1710 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03001711
1712 oh->_state = _HWMOD_STATE_DISABLED;
1713
1714 return 0;
1715}
1716
1717/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001718 * _setup - do initial configuration of omap_hwmod
1719 * @oh: struct omap_hwmod *
1720 *
1721 * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
Paul Walmsley48d54f32011-02-23 00:14:07 -07001722 * OCP_SYSCONFIG register. Returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001723 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001724static int _setup(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001725{
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001726 int i, r;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001727 u8 postsetup_state;
Paul Walmsley97d60162010-07-26 16:34:30 -06001728
Paul Walmsley48d54f32011-02-23 00:14:07 -07001729 if (oh->_state != _HWMOD_STATE_CLKS_INITED)
1730 return 0;
1731
Paul Walmsley63c85232009-09-03 20:14:03 +03001732 /* Set iclk autoidle mode */
1733 if (oh->slaves_cnt > 0) {
Benoit Cousson682fdc92010-05-20 12:31:09 -06001734 for (i = 0; i < oh->slaves_cnt; i++) {
1735 struct omap_hwmod_ocp_if *os = oh->slaves[i];
Paul Walmsley63c85232009-09-03 20:14:03 +03001736 struct clk *c = os->_clk;
1737
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -06001738 if (!c)
Paul Walmsley63c85232009-09-03 20:14:03 +03001739 continue;
1740
1741 if (os->flags & OCPIF_SWSUP_IDLE) {
1742 /* XXX omap_iclk_deny_idle(c); */
1743 } else {
1744 /* XXX omap_iclk_allow_idle(c); */
1745 clk_enable(c);
1746 }
1747 }
1748 }
1749
1750 oh->_state = _HWMOD_STATE_INITIALIZED;
1751
Benoît Cousson5365efb2010-09-21 10:34:11 -06001752 /*
1753 * In the case of hwmod with hardreset that should not be
1754 * de-assert at boot time, we have to keep the module
1755 * initialized, because we cannot enable it properly with the
1756 * reset asserted. Exit without warning because that behavior is
1757 * expected.
1758 */
1759 if ((oh->flags & HWMOD_INIT_NO_RESET) && oh->rst_lines_cnt == 1)
1760 return 0;
1761
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001762 r = _enable(oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001763 if (r) {
1764 pr_warning("omap_hwmod: %s: cannot be enabled (%d)\n",
1765 oh->name, oh->_state);
1766 return 0;
1767 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001768
Paul Walmsleyb835d012009-12-08 16:34:14 -07001769 if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001770 _reset(oh);
1771
Paul Walmsleyb835d012009-12-08 16:34:14 -07001772 /*
Benoit Cousson76e55892010-09-21 10:34:11 -06001773 * OCP_SYSCONFIG bits need to be reprogrammed after a softreset.
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001774 * The _enable() function should be split to
Benoit Cousson76e55892010-09-21 10:34:11 -06001775 * avoid the rewrite of the OCP_SYSCONFIG register.
Paul Walmsleyb835d012009-12-08 16:34:14 -07001776 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001777 if (oh->class->sysc) {
Paul Walmsleyb835d012009-12-08 16:34:14 -07001778 _update_sysc_cache(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001779 _enable_sysc(oh);
Paul Walmsleyb835d012009-12-08 16:34:14 -07001780 }
1781 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001782
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001783 postsetup_state = oh->_postsetup_state;
1784 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
1785 postsetup_state = _HWMOD_STATE_ENABLED;
1786
1787 /*
1788 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
1789 * it should be set by the core code as a runtime flag during startup
1790 */
1791 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
1792 (postsetup_state == _HWMOD_STATE_IDLE))
1793 postsetup_state = _HWMOD_STATE_ENABLED;
1794
1795 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001796 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07001797 else if (postsetup_state == _HWMOD_STATE_DISABLED)
1798 _shutdown(oh);
1799 else if (postsetup_state != _HWMOD_STATE_ENABLED)
1800 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
1801 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03001802
1803 return 0;
1804}
1805
Benoit Cousson0102b622010-12-21 21:31:27 -07001806/**
1807 * _register - register a struct omap_hwmod
1808 * @oh: struct omap_hwmod *
1809 *
1810 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
1811 * already has been registered by the same name; -EINVAL if the
1812 * omap_hwmod is in the wrong state, if @oh is NULL, if the
1813 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
1814 * name, or if the omap_hwmod's class is missing a name; or 0 upon
1815 * success.
1816 *
1817 * XXX The data should be copied into bootmem, so the original data
1818 * should be marked __initdata and freed after init. This would allow
1819 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
1820 * that the copy process would be relatively complex due to the large number
1821 * of substructures.
1822 */
Benoit Cousson01592df2010-12-21 21:31:28 -07001823static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07001824{
Paul Walmsley569edd702011-02-23 00:14:06 -07001825 int ms_id;
Benoit Cousson0102b622010-12-21 21:31:27 -07001826
1827 if (!oh || !oh->name || !oh->class || !oh->class->name ||
1828 (oh->_state != _HWMOD_STATE_UNKNOWN))
1829 return -EINVAL;
1830
Benoit Cousson0102b622010-12-21 21:31:27 -07001831 pr_debug("omap_hwmod: %s: registering\n", oh->name);
1832
Benoit Coussonce35b242010-12-21 21:31:28 -07001833 if (_lookup(oh->name))
1834 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07001835
1836 ms_id = _find_mpu_port_index(oh);
Tony Lindgrene7c7d762011-02-14 15:40:21 -08001837 if (!IS_ERR_VALUE(ms_id))
Benoit Cousson0102b622010-12-21 21:31:27 -07001838 oh->_mpu_port_index = ms_id;
Tony Lindgrene7c7d762011-02-14 15:40:21 -08001839 else
Benoit Cousson0102b622010-12-21 21:31:27 -07001840 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Benoit Cousson0102b622010-12-21 21:31:27 -07001841
1842 list_add_tail(&oh->node, &omap_hwmod_list);
1843
1844 spin_lock_init(&oh->_lock);
1845
1846 oh->_state = _HWMOD_STATE_REGISTERED;
1847
Paul Walmsley569edd702011-02-23 00:14:06 -07001848 /*
1849 * XXX Rather than doing a strcmp(), this should test a flag
1850 * set in the hwmod data, inserted by the autogenerator code.
1851 */
1852 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
1853 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07001854
Paul Walmsley569edd702011-02-23 00:14:06 -07001855 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07001856}
Paul Walmsley63c85232009-09-03 20:14:03 +03001857
1858
1859/* Public functions */
1860
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001861u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03001862{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001863 if (oh->flags & HWMOD_16BIT_REG)
1864 return __raw_readw(oh->_mpu_rt_va + reg_offs);
1865 else
1866 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001867}
1868
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001869void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03001870{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001871 if (oh->flags & HWMOD_16BIT_REG)
1872 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
1873 else
1874 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001875}
1876
Paul Walmsley887adea2010-07-26 16:34:33 -06001877/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06001878 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
1879 * @oh: struct omap_hwmod *
1880 *
1881 * This is a public function exposed to drivers. Some drivers may need to do
1882 * some settings before and after resetting the device. Those drivers after
1883 * doing the necessary settings could use this function to start a reset by
1884 * setting the SYSCONFIG.SOFTRESET bit.
1885 */
1886int omap_hwmod_softreset(struct omap_hwmod *oh)
1887{
1888 u32 v;
1889 int ret;
1890
1891 if (!oh || !(oh->_sysc_cache))
1892 return -EINVAL;
1893
1894 v = oh->_sysc_cache;
1895 ret = _set_softreset(oh, &v);
1896 if (ret)
1897 goto error;
1898 _write_sysconfig(v, oh);
1899
1900error:
1901 return ret;
1902}
1903
1904/**
Paul Walmsley887adea2010-07-26 16:34:33 -06001905 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
1906 * @oh: struct omap_hwmod *
1907 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
1908 *
1909 * Sets the IP block's OCP slave idlemode in hardware, and updates our
1910 * local copy. Intended to be used by drivers that have some erratum
1911 * that requires direct manipulation of the SIDLEMODE bits. Returns
1912 * -EINVAL if @oh is null, or passes along the return value from
1913 * _set_slave_idlemode().
1914 *
1915 * XXX Does this function have any current users? If not, we should
1916 * remove it; it is better to let the rest of the hwmod code handle this.
1917 * Any users of this function should be scrutinized carefully.
1918 */
Kevin Hilman46273e62010-01-26 20:13:03 -07001919int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
1920{
1921 u32 v;
1922 int retval = 0;
1923
1924 if (!oh)
1925 return -EINVAL;
1926
1927 v = oh->_sysc_cache;
1928
1929 retval = _set_slave_idlemode(oh, idlemode, &v);
1930 if (!retval)
1931 _write_sysconfig(v, oh);
1932
1933 return retval;
1934}
1935
Paul Walmsley63c85232009-09-03 20:14:03 +03001936/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001937 * omap_hwmod_lookup - look up a registered omap_hwmod by name
1938 * @name: name of the omap_hwmod to look up
1939 *
1940 * Given a @name of an omap_hwmod, return a pointer to the registered
1941 * struct omap_hwmod *, or NULL upon error.
1942 */
1943struct omap_hwmod *omap_hwmod_lookup(const char *name)
1944{
1945 struct omap_hwmod *oh;
1946
1947 if (!name)
1948 return NULL;
1949
Paul Walmsley63c85232009-09-03 20:14:03 +03001950 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001951
1952 return oh;
1953}
1954
1955/**
1956 * omap_hwmod_for_each - call function for each registered omap_hwmod
1957 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06001958 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03001959 *
1960 * Call @fn for each registered omap_hwmod, passing @data to each
1961 * function. @fn must return 0 for success or any other value for
1962 * failure. If @fn returns non-zero, the iteration across omap_hwmods
1963 * will stop and the non-zero return value will be passed to the
1964 * caller of omap_hwmod_for_each(). @fn is called with
1965 * omap_hwmod_for_each() held.
1966 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001967int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
1968 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001969{
1970 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05301971 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001972
1973 if (!fn)
1974 return -EINVAL;
1975
Paul Walmsley63c85232009-09-03 20:14:03 +03001976 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06001977 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03001978 if (ret)
1979 break;
1980 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001981
1982 return ret;
1983}
1984
Paul Walmsley63c85232009-09-03 20:14:03 +03001985/**
Paul Walmsley550c8092011-02-28 11:58:14 -07001986 * omap_hwmod_register - register an array of hwmods
Paul Walmsley63c85232009-09-03 20:14:03 +03001987 * @ohs: pointer to an array of omap_hwmods to register
1988 *
1989 * Intended to be called early in boot before the clock framework is
1990 * initialized. If @ohs is not null, will register all omap_hwmods
Paul Walmsley550c8092011-02-28 11:58:14 -07001991 * listed in @ohs that are valid for this chip. Returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001992 */
Paul Walmsley550c8092011-02-28 11:58:14 -07001993int __init omap_hwmod_register(struct omap_hwmod **ohs)
Paul Walmsley63c85232009-09-03 20:14:03 +03001994{
Paul Walmsleybac1a0f2011-02-23 00:14:06 -07001995 int r, i;
Paul Walmsley63c85232009-09-03 20:14:03 +03001996
1997 if (!ohs)
1998 return 0;
1999
Paul Walmsleybac1a0f2011-02-23 00:14:06 -07002000 i = 0;
2001 do {
Paul Walmsleybac1a0f2011-02-23 00:14:06 -07002002 r = _register(ohs[i]);
2003 WARN(r, "omap_hwmod: %s: _register returned %d\n", ohs[i]->name,
2004 r);
2005 } while (ohs[++i]);
Paul Walmsley63c85232009-09-03 20:14:03 +03002006
2007 return 0;
2008}
2009
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002010/*
2011 * _populate_mpu_rt_base - populate the virtual address for a hwmod
2012 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002013 * Must be called only from omap_hwmod_setup_*() so ioremap works properly.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002014 * Assumes the caller takes care of locking if needed.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002015 */
2016static int __init _populate_mpu_rt_base(struct omap_hwmod *oh, void *data)
2017{
Paul Walmsley48d54f32011-02-23 00:14:07 -07002018 if (oh->_state != _HWMOD_STATE_REGISTERED)
2019 return 0;
2020
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002021 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2022 return 0;
2023
2024 oh->_mpu_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
Paul Walmsley63c85232009-09-03 20:14:03 +03002025
2026 return 0;
2027}
2028
2029/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002030 * omap_hwmod_setup_one - set up a single hwmod
2031 * @oh_name: const char * name of the already-registered hwmod to set up
2032 *
2033 * Must be called after omap2_clk_init(). Resolves the struct clk
2034 * names to struct clk pointers for each registered omap_hwmod. Also
2035 * calls _setup() on each hwmod. Returns -EINVAL upon error or 0 upon
2036 * success.
2037 */
2038int __init omap_hwmod_setup_one(const char *oh_name)
2039{
2040 struct omap_hwmod *oh;
2041 int r;
2042
2043 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
2044
2045 if (!mpu_oh) {
2046 pr_err("omap_hwmod: %s: cannot setup_one: MPU initiator hwmod %s not yet registered\n",
2047 oh_name, MPU_INITIATOR_NAME);
2048 return -EINVAL;
2049 }
2050
2051 oh = _lookup(oh_name);
2052 if (!oh) {
2053 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
2054 return -EINVAL;
2055 }
2056
2057 if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
2058 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
2059
2060 r = _populate_mpu_rt_base(oh, NULL);
2061 if (IS_ERR_VALUE(r)) {
2062 WARN(1, "omap_hwmod: %s: couldn't set mpu_rt_base\n", oh_name);
2063 return -EINVAL;
2064 }
2065
2066 r = _init_clocks(oh, NULL);
2067 if (IS_ERR_VALUE(r)) {
2068 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh_name);
2069 return -EINVAL;
2070 }
2071
2072 _setup(oh, NULL);
2073
2074 return 0;
2075}
2076
2077/**
Paul Walmsley550c8092011-02-28 11:58:14 -07002078 * omap_hwmod_setup - do some post-clock framework initialization
Paul Walmsley63c85232009-09-03 20:14:03 +03002079 *
2080 * Must be called after omap2_clk_init(). Resolves the struct clk names
2081 * to struct clk pointers for each registered omap_hwmod. Also calls
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002082 * _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002083 */
Paul Walmsley550c8092011-02-28 11:58:14 -07002084static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03002085{
2086 int r;
2087
Paul Walmsley569edd702011-02-23 00:14:06 -07002088 if (!mpu_oh) {
2089 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
2090 __func__, MPU_INITIATOR_NAME);
2091 return -EINVAL;
2092 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002093
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002094 r = omap_hwmod_for_each(_populate_mpu_rt_base, NULL);
2095
Paul Walmsley63c85232009-09-03 20:14:03 +03002096 r = omap_hwmod_for_each(_init_clocks, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002097 WARN(IS_ERR_VALUE(r),
2098 "omap_hwmod: %s: _init_clocks failed\n", __func__);
Paul Walmsley63c85232009-09-03 20:14:03 +03002099
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002100 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002101
2102 return 0;
2103}
Paul Walmsley550c8092011-02-28 11:58:14 -07002104core_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03002105
2106/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002107 * omap_hwmod_enable - enable an omap_hwmod
2108 * @oh: struct omap_hwmod *
2109 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002110 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03002111 * Returns -EINVAL on error or passes along the return value from _enable().
2112 */
2113int omap_hwmod_enable(struct omap_hwmod *oh)
2114{
2115 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002116 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002117
2118 if (!oh)
2119 return -EINVAL;
2120
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002121 spin_lock_irqsave(&oh->_lock, flags);
2122 r = _enable(oh);
2123 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002124
2125 return r;
2126}
2127
2128/**
2129 * omap_hwmod_idle - idle an omap_hwmod
2130 * @oh: struct omap_hwmod *
2131 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002132 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03002133 * Returns -EINVAL on error or passes along the return value from _idle().
2134 */
2135int omap_hwmod_idle(struct omap_hwmod *oh)
2136{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002137 unsigned long flags;
2138
Paul Walmsley63c85232009-09-03 20:14:03 +03002139 if (!oh)
2140 return -EINVAL;
2141
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002142 spin_lock_irqsave(&oh->_lock, flags);
2143 _idle(oh);
2144 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002145
2146 return 0;
2147}
2148
2149/**
2150 * omap_hwmod_shutdown - shutdown an omap_hwmod
2151 * @oh: struct omap_hwmod *
2152 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002153 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03002154 * omap_device_shutdown(). Returns -EINVAL on error or passes along
2155 * the return value from _shutdown().
2156 */
2157int omap_hwmod_shutdown(struct omap_hwmod *oh)
2158{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002159 unsigned long flags;
2160
Paul Walmsley63c85232009-09-03 20:14:03 +03002161 if (!oh)
2162 return -EINVAL;
2163
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002164 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002165 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002166 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002167
2168 return 0;
2169}
2170
2171/**
2172 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
2173 * @oh: struct omap_hwmod *oh
2174 *
2175 * Intended to be called by the omap_device code.
2176 */
2177int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
2178{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002179 unsigned long flags;
2180
2181 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002182 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002183 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002184
2185 return 0;
2186}
2187
2188/**
2189 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
2190 * @oh: struct omap_hwmod *oh
2191 *
2192 * Intended to be called by the omap_device code.
2193 */
2194int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
2195{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002196 unsigned long flags;
2197
2198 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002199 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002200 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002201
2202 return 0;
2203}
2204
2205/**
2206 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
2207 * @oh: struct omap_hwmod *oh
2208 *
2209 * Intended to be called by drivers and core code when all posted
2210 * writes to a device must complete before continuing further
2211 * execution (for example, after clearing some device IRQSTATUS
2212 * register bits)
2213 *
2214 * XXX what about targets with multiple OCP threads?
2215 */
2216void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
2217{
2218 BUG_ON(!oh);
2219
Paul Walmsley43b40992010-02-22 22:09:34 -07002220 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Paul Walmsley63c85232009-09-03 20:14:03 +03002221 WARN(1, "omap_device: %s: OCP barrier impossible due to "
2222 "device configuration\n", oh->name);
2223 return;
2224 }
2225
2226 /*
2227 * Forces posted writes to complete on the OCP thread handling
2228 * register writes
2229 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002230 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002231}
2232
2233/**
2234 * omap_hwmod_reset - reset the hwmod
2235 * @oh: struct omap_hwmod *
2236 *
2237 * Under some conditions, a driver may wish to reset the entire device.
2238 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06002239 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03002240 */
2241int omap_hwmod_reset(struct omap_hwmod *oh)
2242{
2243 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002244 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002245
Liam Girdwood9b579112010-09-21 10:34:09 -06002246 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002247 return -EINVAL;
2248
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002249 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002250 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002251 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002252
2253 return r;
2254}
2255
2256/**
2257 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
2258 * @oh: struct omap_hwmod *
2259 * @res: pointer to the first element of an array of struct resource to fill
2260 *
2261 * Count the number of struct resource array elements necessary to
2262 * contain omap_hwmod @oh resources. Intended to be called by code
2263 * that registers omap_devices. Intended to be used to determine the
2264 * size of a dynamically-allocated struct resource array, before
2265 * calling omap_hwmod_fill_resources(). Returns the number of struct
2266 * resource array elements needed.
2267 *
2268 * XXX This code is not optimized. It could attempt to merge adjacent
2269 * resource IDs.
2270 *
2271 */
2272int omap_hwmod_count_resources(struct omap_hwmod *oh)
2273{
2274 int ret, i;
2275
Paul Walmsleybc614952011-07-09 19:14:07 -06002276 ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002277
2278 for (i = 0; i < oh->slaves_cnt; i++)
Paul Walmsley78183f32011-07-09 19:14:05 -06002279 ret += _count_ocp_if_addr_spaces(oh->slaves[i]);
Paul Walmsley63c85232009-09-03 20:14:03 +03002280
2281 return ret;
2282}
2283
2284/**
2285 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
2286 * @oh: struct omap_hwmod *
2287 * @res: pointer to the first element of an array of struct resource to fill
2288 *
2289 * Fill the struct resource array @res with resource data from the
2290 * omap_hwmod @oh. Intended to be called by code that registers
2291 * omap_devices. See also omap_hwmod_count_resources(). Returns the
2292 * number of array elements filled.
2293 */
2294int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
2295{
Paul Walmsleybc614952011-07-09 19:14:07 -06002296 int i, j, mpu_irqs_cnt, sdma_reqs_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03002297 int r = 0;
2298
2299 /* For each IRQ, DMA, memory area, fill in array.*/
2300
Paul Walmsley212738a2011-07-09 19:14:06 -06002301 mpu_irqs_cnt = _count_mpu_irqs(oh);
2302 for (i = 0; i < mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07002303 (res + r)->name = (oh->mpu_irqs + i)->name;
2304 (res + r)->start = (oh->mpu_irqs + i)->irq;
2305 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03002306 (res + r)->flags = IORESOURCE_IRQ;
2307 r++;
2308 }
2309
Paul Walmsleybc614952011-07-09 19:14:07 -06002310 sdma_reqs_cnt = _count_sdma_reqs(oh);
2311 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06002312 (res + r)->name = (oh->sdma_reqs + i)->name;
2313 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
2314 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03002315 (res + r)->flags = IORESOURCE_DMA;
2316 r++;
2317 }
2318
2319 for (i = 0; i < oh->slaves_cnt; i++) {
2320 struct omap_hwmod_ocp_if *os;
Paul Walmsley78183f32011-07-09 19:14:05 -06002321 int addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03002322
Benoit Cousson682fdc92010-05-20 12:31:09 -06002323 os = oh->slaves[i];
Paul Walmsley78183f32011-07-09 19:14:05 -06002324 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03002325
Paul Walmsley78183f32011-07-09 19:14:05 -06002326 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08002327 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03002328 (res + r)->start = (os->addr + j)->pa_start;
2329 (res + r)->end = (os->addr + j)->pa_end;
2330 (res + r)->flags = IORESOURCE_MEM;
2331 r++;
2332 }
2333 }
2334
2335 return r;
2336}
2337
2338/**
2339 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
2340 * @oh: struct omap_hwmod *
2341 *
2342 * Return the powerdomain pointer associated with the OMAP module
2343 * @oh's main clock. If @oh does not have a main clk, return the
2344 * powerdomain associated with the interface clock associated with the
2345 * module's MPU port. (XXX Perhaps this should use the SDMA port
2346 * instead?) Returns NULL on error, or a struct powerdomain * on
2347 * success.
2348 */
2349struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
2350{
2351 struct clk *c;
2352
2353 if (!oh)
2354 return NULL;
2355
2356 if (oh->_clk) {
2357 c = oh->_clk;
2358 } else {
2359 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2360 return NULL;
2361 c = oh->slaves[oh->_mpu_port_index]->_clk;
2362 }
2363
Thara Gopinathd5647c12010-03-31 04:16:29 -06002364 if (!c->clkdm)
2365 return NULL;
2366
Paul Walmsley63c85232009-09-03 20:14:03 +03002367 return c->clkdm->pwrdm.ptr;
2368
2369}
2370
2371/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06002372 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
2373 * @oh: struct omap_hwmod *
2374 *
2375 * Returns the virtual address corresponding to the beginning of the
2376 * module's register target, in the address range that is intended to
2377 * be used by the MPU. Returns the virtual address upon success or NULL
2378 * upon error.
2379 */
2380void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
2381{
2382 if (!oh)
2383 return NULL;
2384
2385 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2386 return NULL;
2387
2388 if (oh->_state == _HWMOD_STATE_UNKNOWN)
2389 return NULL;
2390
2391 return oh->_mpu_rt_va;
2392}
2393
2394/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002395 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
2396 * @oh: struct omap_hwmod *
2397 * @init_oh: struct omap_hwmod * (initiator)
2398 *
2399 * Add a sleep dependency between the initiator @init_oh and @oh.
2400 * Intended to be called by DSP/Bridge code via platform_data for the
2401 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
2402 * code needs to add/del initiator dependencies dynamically
2403 * before/after accessing a device. Returns the return value from
2404 * _add_initiator_dep().
2405 *
2406 * XXX Keep a usecount in the clockdomain code
2407 */
2408int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
2409 struct omap_hwmod *init_oh)
2410{
2411 return _add_initiator_dep(oh, init_oh);
2412}
2413
2414/*
2415 * XXX what about functions for drivers to save/restore ocp_sysconfig
2416 * for context save/restore operations?
2417 */
2418
2419/**
2420 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
2421 * @oh: struct omap_hwmod *
2422 * @init_oh: struct omap_hwmod * (initiator)
2423 *
2424 * Remove a sleep dependency between the initiator @init_oh and @oh.
2425 * Intended to be called by DSP/Bridge code via platform_data for the
2426 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
2427 * code needs to add/del initiator dependencies dynamically
2428 * before/after accessing a device. Returns the return value from
2429 * _del_initiator_dep().
2430 *
2431 * XXX Keep a usecount in the clockdomain code
2432 */
2433int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
2434 struct omap_hwmod *init_oh)
2435{
2436 return _del_initiator_dep(oh, init_oh);
2437}
2438
2439/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002440 * omap_hwmod_enable_wakeup - allow device to wake up the system
2441 * @oh: struct omap_hwmod *
2442 *
2443 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
2444 * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
2445 * registers to cause the PRCM to receive wakeup events from the
2446 * module. Does not set any wakeup routing registers beyond this
2447 * point - if the module is to wake up any other module or subsystem,
2448 * that must be set separately. Called by omap_device code. Returns
2449 * -EINVAL on error or 0 upon success.
2450 */
2451int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
2452{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002453 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002454 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002455
Paul Walmsley43b40992010-02-22 22:09:34 -07002456 if (!oh->class->sysc ||
2457 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
Paul Walmsley63c85232009-09-03 20:14:03 +03002458 return -EINVAL;
2459
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002460 spin_lock_irqsave(&oh->_lock, flags);
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002461 v = oh->_sysc_cache;
2462 _enable_wakeup(oh, &v);
2463 _write_sysconfig(v, oh);
Govindraj Receec002011-12-16 14:36:58 -07002464 _set_idle_ioring_wakeup(oh, true);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002465 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002466
2467 return 0;
2468}
2469
2470/**
2471 * omap_hwmod_disable_wakeup - prevent device from waking the system
2472 * @oh: struct omap_hwmod *
2473 *
2474 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
2475 * from sending wakeups to the PRCM. Eventually this should clear
2476 * PRCM wakeup registers to cause the PRCM to ignore wakeup events
2477 * from the module. Does not set any wakeup routing registers beyond
2478 * this point - if the module is to wake up any other module or
2479 * subsystem, that must be set separately. Called by omap_device
2480 * code. Returns -EINVAL on error or 0 upon success.
2481 */
2482int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
2483{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002484 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002485 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002486
Paul Walmsley43b40992010-02-22 22:09:34 -07002487 if (!oh->class->sysc ||
2488 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
Paul Walmsley63c85232009-09-03 20:14:03 +03002489 return -EINVAL;
2490
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002491 spin_lock_irqsave(&oh->_lock, flags);
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07002492 v = oh->_sysc_cache;
2493 _disable_wakeup(oh, &v);
2494 _write_sysconfig(v, oh);
Govindraj Receec002011-12-16 14:36:58 -07002495 _set_idle_ioring_wakeup(oh, false);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002496 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002497
2498 return 0;
2499}
Paul Walmsley43b40992010-02-22 22:09:34 -07002500
2501/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002502 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
2503 * contained in the hwmod module.
2504 * @oh: struct omap_hwmod *
2505 * @name: name of the reset line to lookup and assert
2506 *
2507 * Some IP like dsp, ipu or iva contain processor that require
2508 * an HW reset line to be assert / deassert in order to enable fully
2509 * the IP. Returns -EINVAL if @oh is null or if the operation is not
2510 * yet supported on this OMAP; otherwise, passes along the return value
2511 * from _assert_hardreset().
2512 */
2513int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
2514{
2515 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002516 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002517
2518 if (!oh)
2519 return -EINVAL;
2520
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002521 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002522 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002523 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002524
2525 return ret;
2526}
2527
2528/**
2529 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
2530 * contained in the hwmod module.
2531 * @oh: struct omap_hwmod *
2532 * @name: name of the reset line to look up and deassert
2533 *
2534 * Some IP like dsp, ipu or iva contain processor that require
2535 * an HW reset line to be assert / deassert in order to enable fully
2536 * the IP. Returns -EINVAL if @oh is null or if the operation is not
2537 * yet supported on this OMAP; otherwise, passes along the return value
2538 * from _deassert_hardreset().
2539 */
2540int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
2541{
2542 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002543 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002544
2545 if (!oh)
2546 return -EINVAL;
2547
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002548 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002549 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002550 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002551
2552 return ret;
2553}
2554
2555/**
2556 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
2557 * contained in the hwmod module
2558 * @oh: struct omap_hwmod *
2559 * @name: name of the reset line to look up and read
2560 *
2561 * Return the current state of the hwmod @oh's reset line named @name:
2562 * returns -EINVAL upon parameter error or if this operation
2563 * is unsupported on the current OMAP; otherwise, passes along the return
2564 * value from _read_hardreset().
2565 */
2566int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
2567{
2568 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002569 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002570
2571 if (!oh)
2572 return -EINVAL;
2573
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002574 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002575 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002576 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06002577
2578 return ret;
2579}
2580
2581
2582/**
Paul Walmsley43b40992010-02-22 22:09:34 -07002583 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
2584 * @classname: struct omap_hwmod_class name to search for
2585 * @fn: callback function pointer to call for each hwmod in class @classname
2586 * @user: arbitrary context data to pass to the callback function
2587 *
Benoit Coussonce35b242010-12-21 21:31:28 -07002588 * For each omap_hwmod of class @classname, call @fn.
2589 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07002590 * zero, the iterator is terminated, and the callback function's return
2591 * value is passed back to the caller. Returns 0 upon success, -EINVAL
2592 * if @classname or @fn are NULL, or passes back the error code from @fn.
2593 */
2594int omap_hwmod_for_each_by_class(const char *classname,
2595 int (*fn)(struct omap_hwmod *oh,
2596 void *user),
2597 void *user)
2598{
2599 struct omap_hwmod *temp_oh;
2600 int ret = 0;
2601
2602 if (!classname || !fn)
2603 return -EINVAL;
2604
2605 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
2606 __func__, classname);
2607
Paul Walmsley43b40992010-02-22 22:09:34 -07002608 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
2609 if (!strcmp(temp_oh->class->name, classname)) {
2610 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
2611 __func__, temp_oh->name);
2612 ret = (*fn)(temp_oh, user);
2613 if (ret)
2614 break;
2615 }
2616 }
2617
Paul Walmsley43b40992010-02-22 22:09:34 -07002618 if (ret)
2619 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
2620 __func__, ret);
2621
2622 return ret;
2623}
2624
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002625/**
2626 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
2627 * @oh: struct omap_hwmod *
2628 * @state: state that _setup() should leave the hwmod in
2629 *
Paul Walmsley550c8092011-02-28 11:58:14 -07002630 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002631 * (called by omap_hwmod_setup_*()). Only valid to call between
2632 * calling omap_hwmod_register() and omap_hwmod_setup_*(). Returns
Paul Walmsley550c8092011-02-28 11:58:14 -07002633 * 0 upon success or -EINVAL if there is a problem with the arguments
2634 * or if the hwmod is in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002635 */
2636int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
2637{
2638 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002639 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002640
2641 if (!oh)
2642 return -EINVAL;
2643
2644 if (state != _HWMOD_STATE_DISABLED &&
2645 state != _HWMOD_STATE_ENABLED &&
2646 state != _HWMOD_STATE_IDLE)
2647 return -EINVAL;
2648
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002649 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002650
2651 if (oh->_state != _HWMOD_STATE_REGISTERED) {
2652 ret = -EINVAL;
2653 goto ohsps_unlock;
2654 }
2655
2656 oh->_postsetup_state = state;
2657 ret = 0;
2658
2659ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002660 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002661
2662 return ret;
2663}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07002664
2665/**
2666 * omap_hwmod_get_context_loss_count - get lost context count
2667 * @oh: struct omap_hwmod *
2668 *
2669 * Query the powerdomain of of @oh to get the context loss
2670 * count for this device.
2671 *
2672 * Returns the context loss count of the powerdomain assocated with @oh
2673 * upon success, or zero if no powerdomain exists for @oh.
2674 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03002675int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07002676{
2677 struct powerdomain *pwrdm;
2678 int ret = 0;
2679
2680 pwrdm = omap_hwmod_get_pwrdm(oh);
2681 if (pwrdm)
2682 ret = pwrdm_get_context_loss_count(pwrdm);
2683
2684 return ret;
2685}
Paul Walmsley43b01642011-03-10 03:50:07 -07002686
2687/**
2688 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
2689 * @oh: struct omap_hwmod *
2690 *
2691 * Prevent the hwmod @oh from being reset during the setup process.
2692 * Intended for use by board-*.c files on boards with devices that
2693 * cannot tolerate being reset. Must be called before the hwmod has
2694 * been set up. Returns 0 upon success or negative error code upon
2695 * failure.
2696 */
2697int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
2698{
2699 if (!oh)
2700 return -EINVAL;
2701
2702 if (oh->_state != _HWMOD_STATE_REGISTERED) {
2703 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
2704 oh->name);
2705 return -EINVAL;
2706 }
2707
2708 oh->flags |= HWMOD_INIT_NO_RESET;
2709
2710 return 0;
2711}