blob: 634a79836c646cebded12c005633afbefe0eaa76 [file] [log] [blame]
Paul Walmsley63c85232009-09-03 20:14:03 +03001/*
2 * omap_hwmod implementation for OMAP2/3/4
3 *
Paul Walmsley550c8092011-02-28 11:58:14 -07004 * Copyright (C) 2009-2011 Nokia Corporation
Paul Walmsley30e105c2012-04-19 00:49:09 -06005 * Copyright (C) 2011-2012 Texas Instruments, Inc.
Paul Walmsley63c85232009-09-03 20:14:03 +03006 *
Paul Walmsley4788da22010-05-18 20:24:05 -06007 * Paul Walmsley, Benoît Cousson, Kevin Hilman
8 *
9 * Created in collaboration with (alphabetical order): Thara Gopinath,
10 * Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari Poussa, Anand
11 * Sawant, Santosh Shilimkar, Richard Woodruff
Paul Walmsley63c85232009-09-03 20:14:03 +030012 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -060017 * Introduction
18 * ------------
19 * One way to view an OMAP SoC is as a collection of largely unrelated
20 * IP blocks connected by interconnects. The IP blocks include
21 * devices such as ARM processors, audio serial interfaces, UARTs,
22 * etc. Some of these devices, like the DSP, are created by TI;
23 * others, like the SGX, largely originate from external vendors. In
24 * TI's documentation, on-chip devices are referred to as "OMAP
25 * modules." Some of these IP blocks are identical across several
26 * OMAP versions. Others are revised frequently.
Paul Walmsley63c85232009-09-03 20:14:03 +030027 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -060028 * These OMAP modules are tied together by various interconnects.
29 * Most of the address and data flow between modules is via OCP-based
30 * interconnects such as the L3 and L4 buses; but there are other
31 * interconnects that distribute the hardware clock tree, handle idle
32 * and reset signaling, supply power, and connect the modules to
33 * various pads or balls on the OMAP package.
34 *
35 * OMAP hwmod provides a consistent way to describe the on-chip
36 * hardware blocks and their integration into the rest of the chip.
37 * This description can be automatically generated from the TI
38 * hardware database. OMAP hwmod provides a standard, consistent API
39 * to reset, enable, idle, and disable these hardware blocks. And
40 * hwmod provides a way for other core code, such as the Linux device
41 * code or the OMAP power management and address space mapping code,
42 * to query the hardware database.
43 *
44 * Using hwmod
45 * -----------
46 * Drivers won't call hwmod functions directly. That is done by the
47 * omap_device code, and in rare occasions, by custom integration code
48 * in arch/arm/ *omap*. The omap_device code includes functions to
49 * build a struct platform_device using omap_hwmod data, and that is
50 * currently how hwmod data is communicated to drivers and to the
51 * Linux driver model. Most drivers will call omap_hwmod functions only
52 * indirectly, via pm_runtime*() functions.
53 *
54 * From a layering perspective, here is where the OMAP hwmod code
55 * fits into the kernel software stack:
56 *
57 * +-------------------------------+
58 * | Device driver code |
59 * | (e.g., drivers/) |
60 * +-------------------------------+
61 * | Linux driver model |
62 * | (platform_device / |
63 * | platform_driver data/code) |
64 * +-------------------------------+
65 * | OMAP core-driver integration |
66 * |(arch/arm/mach-omap2/devices.c)|
67 * +-------------------------------+
68 * | omap_device code |
69 * | (../plat-omap/omap_device.c) |
70 * +-------------------------------+
71 * ----> | omap_hwmod code/data | <-----
72 * | (../mach-omap2/omap_hwmod*) |
73 * +-------------------------------+
74 * | OMAP clock/PRCM/register fns |
75 * | (__raw_{read,write}l, clk*) |
76 * +-------------------------------+
77 *
78 * Device drivers should not contain any OMAP-specific code or data in
79 * them. They should only contain code to operate the IP block that
80 * the driver is responsible for. This is because these IP blocks can
81 * also appear in other SoCs, either from TI (such as DaVinci) or from
82 * other manufacturers; and drivers should be reusable across other
83 * platforms.
84 *
85 * The OMAP hwmod code also will attempt to reset and idle all on-chip
86 * devices upon boot. The goal here is for the kernel to be
87 * completely self-reliant and independent from bootloaders. This is
88 * to ensure a repeatable configuration, both to ensure consistent
89 * runtime behavior, and to make it easier for others to reproduce
90 * bugs.
91 *
92 * OMAP module activity states
93 * ---------------------------
94 * The hwmod code considers modules to be in one of several activity
95 * states. IP blocks start out in an UNKNOWN state, then once they
96 * are registered via the hwmod code, proceed to the REGISTERED state.
97 * Once their clock names are resolved to clock pointers, the module
98 * enters the CLKS_INITED state; and finally, once the module has been
99 * reset and the integration registers programmed, the INITIALIZED state
100 * is entered. The hwmod code will then place the module into either
101 * the IDLE state to save power, or in the case of a critical system
102 * module, the ENABLED state.
103 *
104 * OMAP core integration code can then call omap_hwmod*() functions
105 * directly to move the module between the IDLE, ENABLED, and DISABLED
106 * states, as needed. This is done during both the PM idle loop, and
107 * in the OMAP core integration code's implementation of the PM runtime
108 * functions.
109 *
110 * References
111 * ----------
112 * This is a partial list.
Paul Walmsley63c85232009-09-03 20:14:03 +0300113 * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
114 * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
115 * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
116 * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
117 * - Open Core Protocol Specification 2.2
118 *
119 * To do:
Paul Walmsley63c85232009-09-03 20:14:03 +0300120 * - handle IO mapping
121 * - bus throughput & module latency measurement code
122 *
123 * XXX add tests at the beginning of each function to ensure the hwmod is
124 * in the appropriate state
125 * XXX error return values should be checked to ensure that they are
126 * appropriate
127 */
128#undef DEBUG
129
130#include <linux/kernel.h>
131#include <linux/errno.h>
132#include <linux/io.h>
133#include <linux/clk.h>
134#include <linux/delay.h>
135#include <linux/err.h>
136#include <linux/list.h>
137#include <linux/mutex.h>
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -0700138#include <linux/spinlock.h>
Tero Kristoabc2d542011-12-16 14:36:59 -0700139#include <linux/slab.h>
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600140#include <linux/bootmem.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300141
Tony Lindgren4e653312011-11-10 22:45:17 +0100142#include "common.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700143#include <plat/cpu.h>
Paul Walmsley1540f2142010-12-21 21:05:15 -0700144#include "clockdomain.h"
Paul Walmsley72e06d02010-12-21 21:05:16 -0700145#include "powerdomain.h"
Tony Lindgrence491cf2009-10-20 09:40:47 -0700146#include <plat/clock.h>
147#include <plat/omap_hwmod.h>
Benoît Cousson5365efb2010-09-21 10:34:11 -0600148#include <plat/prcm.h>
Paul Walmsley63c85232009-09-03 20:14:03 +0300149
Paul Walmsley59fb6592010-12-21 15:30:55 -0700150#include "cm2xxx_3xxx.h"
Benoit Coussond0f06312011-07-10 05:56:30 -0600151#include "cminst44xx.h"
Paul Walmsley59fb6592010-12-21 15:30:55 -0700152#include "prm2xxx_3xxx.h"
Paul Walmsleyd198b512010-12-21 15:30:54 -0700153#include "prm44xx.h"
Benoit Coussoneaac3292011-07-10 05:56:31 -0600154#include "prminst44xx.h"
Tony Lindgren8d9af882010-12-22 18:42:35 -0800155#include "mux.h"
Paul Walmsley63c85232009-09-03 20:14:03 +0300156
Benoît Cousson5365efb2010-09-21 10:34:11 -0600157/* Maximum microseconds to wait for OMAP module to softreset */
158#define MAX_MODULE_SOFTRESET_WAIT 10000
Paul Walmsley63c85232009-09-03 20:14:03 +0300159
160/* Name of the OMAP hwmod for the MPU */
Benoit Cousson5c2c0292010-05-20 12:31:10 -0600161#define MPU_INITIATOR_NAME "mpu"
Paul Walmsley63c85232009-09-03 20:14:03 +0300162
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600163/*
164 * Number of struct omap_hwmod_link records per struct
165 * omap_hwmod_ocp_if record (master->slave and slave->master)
166 */
167#define LINKS_PER_OCP_IF 2
168
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600169/**
170 * struct omap_hwmod_soc_ops - fn ptrs for some SoC-specific operations
171 * @enable_module: function to enable a module (via MODULEMODE)
172 * @disable_module: function to disable a module (via MODULEMODE)
173 *
174 * XXX Eventually this functionality will be hidden inside the PRM/CM
175 * device drivers. Until then, this should avoid huge blocks of cpu_is_*()
176 * conditionals in this code.
177 */
178struct omap_hwmod_soc_ops {
179 void (*enable_module)(struct omap_hwmod *oh);
180 int (*disable_module)(struct omap_hwmod *oh);
181};
182
183/* soc_ops: adapts the omap_hwmod code to the currently-booted SoC */
184static struct omap_hwmod_soc_ops soc_ops;
185
Paul Walmsley63c85232009-09-03 20:14:03 +0300186/* omap_hwmod_list contains all registered struct omap_hwmods */
187static LIST_HEAD(omap_hwmod_list);
188
Paul Walmsley63c85232009-09-03 20:14:03 +0300189/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
190static struct omap_hwmod *mpu_oh;
191
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600192/*
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600193 * linkspace: ptr to a buffer that struct omap_hwmod_link records are
194 * allocated from - used to reduce the number of small memory
195 * allocations, which has a significant impact on performance
196 */
197static struct omap_hwmod_link *linkspace;
198
199/*
200 * free_ls, max_ls: array indexes into linkspace; representing the
201 * next free struct omap_hwmod_link index, and the maximum number of
202 * struct omap_hwmod_link records allocated (respectively)
203 */
204static unsigned short free_ls, max_ls, ls_supp;
Paul Walmsley63c85232009-09-03 20:14:03 +0300205
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600206/* inited: set to true once the hwmod code is initialized */
207static bool inited;
208
Paul Walmsley63c85232009-09-03 20:14:03 +0300209/* Private functions */
210
211/**
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600212 * _fetch_next_ocp_if - return the next OCP interface in a list
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600213 * @p: ptr to a ptr to the list_head inside the ocp_if to return
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600214 * @i: pointer to the index of the element pointed to by @p in the list
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600215 *
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600216 * Return a pointer to the struct omap_hwmod_ocp_if record
217 * containing the struct list_head pointed to by @p, and increment
218 * @p such that a future call to this routine will return the next
219 * record.
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600220 */
221static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600222 int *i)
223{
224 struct omap_hwmod_ocp_if *oi;
225
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600226 oi = list_entry(*p, struct omap_hwmod_link, node)->ocp_if;
227 *p = (*p)->next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600228
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600229 *i = *i + 1;
230
231 return oi;
232}
233
234/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300235 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
236 * @oh: struct omap_hwmod *
237 *
238 * Load the current value of the hwmod OCP_SYSCONFIG register into the
239 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
240 * OCP_SYSCONFIG register or 0 upon success.
241 */
242static int _update_sysc_cache(struct omap_hwmod *oh)
243{
Paul Walmsley43b40992010-02-22 22:09:34 -0700244 if (!oh->class->sysc) {
245 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 +0300246 return -EINVAL;
247 }
248
249 /* XXX ensure module interface clock is up */
250
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -0700251 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300252
Paul Walmsley43b40992010-02-22 22:09:34 -0700253 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
Thara Gopinath883edfd2010-01-19 17:30:51 -0700254 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300255
256 return 0;
257}
258
259/**
260 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
261 * @v: OCP_SYSCONFIG value to write
262 * @oh: struct omap_hwmod *
263 *
Paul Walmsley43b40992010-02-22 22:09:34 -0700264 * Write @v into the module class' OCP_SYSCONFIG register, if it has
265 * one. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +0300266 */
267static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
268{
Paul Walmsley43b40992010-02-22 22:09:34 -0700269 if (!oh->class->sysc) {
270 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 +0300271 return;
272 }
273
274 /* XXX ensure module interface clock is up */
275
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700276 /* Module might have lost context, always update cache and register */
277 oh->_sysc_cache = v;
278 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300279}
280
281/**
282 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
283 * @oh: struct omap_hwmod *
284 * @standbymode: MIDLEMODE field bits
285 * @v: pointer to register contents to modify
286 *
287 * Update the master standby mode bits in @v to be @standbymode for
288 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
289 * upon error or 0 upon success.
290 */
291static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
292 u32 *v)
293{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700294 u32 mstandby_mask;
295 u8 mstandby_shift;
296
Paul Walmsley43b40992010-02-22 22:09:34 -0700297 if (!oh->class->sysc ||
298 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300299 return -EINVAL;
300
Paul Walmsley43b40992010-02-22 22:09:34 -0700301 if (!oh->class->sysc->sysc_fields) {
302 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700303 return -EINVAL;
304 }
305
Paul Walmsley43b40992010-02-22 22:09:34 -0700306 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700307 mstandby_mask = (0x3 << mstandby_shift);
308
309 *v &= ~mstandby_mask;
310 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300311
312 return 0;
313}
314
315/**
316 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
317 * @oh: struct omap_hwmod *
318 * @idlemode: SIDLEMODE field bits
319 * @v: pointer to register contents to modify
320 *
321 * Update the slave idle mode bits in @v to be @idlemode for the @oh
322 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
323 * or 0 upon success.
324 */
325static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
326{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700327 u32 sidle_mask;
328 u8 sidle_shift;
329
Paul Walmsley43b40992010-02-22 22:09:34 -0700330 if (!oh->class->sysc ||
331 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
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 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700340 sidle_mask = (0x3 << sidle_shift);
341
342 *v &= ~sidle_mask;
343 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300344
345 return 0;
346}
347
348/**
349 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
350 * @oh: struct omap_hwmod *
351 * @clockact: CLOCKACTIVITY field bits
352 * @v: pointer to register contents to modify
353 *
354 * Update the clockactivity mode bits in @v to be @clockact for the
355 * @oh hwmod. Used for additional powersaving on some modules. Does
356 * not write to the hardware. Returns -EINVAL upon error or 0 upon
357 * success.
358 */
359static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
360{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700361 u32 clkact_mask;
362 u8 clkact_shift;
363
Paul Walmsley43b40992010-02-22 22:09:34 -0700364 if (!oh->class->sysc ||
365 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300366 return -EINVAL;
367
Paul Walmsley43b40992010-02-22 22:09:34 -0700368 if (!oh->class->sysc->sysc_fields) {
369 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700370 return -EINVAL;
371 }
372
Paul Walmsley43b40992010-02-22 22:09:34 -0700373 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700374 clkact_mask = (0x3 << clkact_shift);
375
376 *v &= ~clkact_mask;
377 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300378
379 return 0;
380}
381
382/**
383 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
384 * @oh: struct omap_hwmod *
385 * @v: pointer to register contents to modify
386 *
387 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
388 * error or 0 upon success.
389 */
390static int _set_softreset(struct omap_hwmod *oh, u32 *v)
391{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700392 u32 softrst_mask;
393
Paul Walmsley43b40992010-02-22 22:09:34 -0700394 if (!oh->class->sysc ||
395 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300396 return -EINVAL;
397
Paul Walmsley43b40992010-02-22 22:09:34 -0700398 if (!oh->class->sysc->sysc_fields) {
399 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700400 return -EINVAL;
401 }
402
Paul Walmsley43b40992010-02-22 22:09:34 -0700403 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700404
405 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300406
407 return 0;
408}
409
410/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700411 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
412 * @oh: struct omap_hwmod *
413 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
414 * @v: pointer to register contents to modify
415 *
416 * Update the module autoidle bit in @v to be @autoidle for the @oh
417 * hwmod. The autoidle bit controls whether the module can gate
418 * internal clocks automatically when it isn't doing anything; the
419 * exact function of this bit varies on a per-module basis. This
420 * function does not write to the hardware. Returns -EINVAL upon
421 * error or 0 upon success.
422 */
423static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
424 u32 *v)
425{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700426 u32 autoidle_mask;
427 u8 autoidle_shift;
428
Paul Walmsley43b40992010-02-22 22:09:34 -0700429 if (!oh->class->sysc ||
430 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700431 return -EINVAL;
432
Paul Walmsley43b40992010-02-22 22:09:34 -0700433 if (!oh->class->sysc->sysc_fields) {
434 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700435 return -EINVAL;
436 }
437
Paul Walmsley43b40992010-02-22 22:09:34 -0700438 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700439 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700440
441 *v &= ~autoidle_mask;
442 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700443
444 return 0;
445}
446
447/**
Govindraj Receec002011-12-16 14:36:58 -0700448 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
449 * @oh: struct omap_hwmod *
450 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
451 *
452 * Set or clear the I/O pad wakeup flag in the mux entries for the
453 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
454 * in memory. If the hwmod is currently idled, and the new idle
455 * values don't match the previous ones, this function will also
456 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
457 * currently idled, this function won't touch the hardware: the new
458 * mux settings are written to the SCM PADCTRL registers when the
459 * hwmod is idled. No return value.
460 */
461static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
462{
463 struct omap_device_pad *pad;
464 bool change = false;
465 u16 prev_idle;
466 int j;
467
468 if (!oh->mux || !oh->mux->enabled)
469 return;
470
471 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
472 pad = oh->mux->pads_dynamic[j];
473
474 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
475 continue;
476
477 prev_idle = pad->idle;
478
479 if (set_wake)
480 pad->idle |= OMAP_WAKEUP_EN;
481 else
482 pad->idle &= ~OMAP_WAKEUP_EN;
483
484 if (prev_idle != pad->idle)
485 change = true;
486 }
487
488 if (change && oh->_state == _HWMOD_STATE_IDLE)
489 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
490}
491
492/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300493 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
494 * @oh: struct omap_hwmod *
495 *
496 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
497 * upon error or 0 upon success.
498 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700499static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300500{
Paul Walmsley43b40992010-02-22 22:09:34 -0700501 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700502 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200503 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
504 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300505 return -EINVAL;
506
Paul Walmsley43b40992010-02-22 22:09:34 -0700507 if (!oh->class->sysc->sysc_fields) {
508 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700509 return -EINVAL;
510 }
511
Benoit Cousson1fe74112011-07-01 22:54:03 +0200512 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
513 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300514
Benoit Cousson86009eb2010-12-21 21:31:28 -0700515 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
516 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200517 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
518 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700519
Paul Walmsley63c85232009-09-03 20:14:03 +0300520 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
521
522 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
523
524 return 0;
525}
526
527/**
528 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
529 * @oh: struct omap_hwmod *
530 *
531 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
532 * upon error or 0 upon success.
533 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700534static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300535{
Paul Walmsley43b40992010-02-22 22:09:34 -0700536 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700537 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200538 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
539 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300540 return -EINVAL;
541
Paul Walmsley43b40992010-02-22 22:09:34 -0700542 if (!oh->class->sysc->sysc_fields) {
543 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700544 return -EINVAL;
545 }
546
Benoit Cousson1fe74112011-07-01 22:54:03 +0200547 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
548 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300549
Benoit Cousson86009eb2010-12-21 21:31:28 -0700550 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
551 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200552 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
553 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700554
Paul Walmsley63c85232009-09-03 20:14:03 +0300555 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
556
557 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
558
559 return 0;
560}
561
562/**
563 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
564 * @oh: struct omap_hwmod *
565 *
566 * Prevent the hardware module @oh from entering idle while the
567 * hardare module initiator @init_oh is active. Useful when a module
568 * will be accessed by a particular initiator (e.g., if a module will
569 * be accessed by the IVA, there should be a sleepdep between the IVA
570 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700571 * mode. If the clockdomain is marked as not needing autodeps, return
572 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
573 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300574 */
575static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
576{
577 if (!oh->_clk)
578 return -EINVAL;
579
Paul Walmsley570b54c2011-03-10 03:50:09 -0700580 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
581 return 0;
582
Paul Walmsley55ed9692010-01-26 20:12:59 -0700583 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300584}
585
586/**
587 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
588 * @oh: struct omap_hwmod *
589 *
590 * Allow the hardware module @oh to enter idle while the hardare
591 * module initiator @init_oh is active. Useful when a module will not
592 * be accessed by a particular initiator (e.g., if a module will not
593 * be accessed by the IVA, there should be no sleepdep between the IVA
594 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700595 * mode. If the clockdomain is marked as not needing autodeps, return
596 * 0 without doing anything. Returns -EINVAL upon error or passes
597 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300598 */
599static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
600{
601 if (!oh->_clk)
602 return -EINVAL;
603
Paul Walmsley570b54c2011-03-10 03:50:09 -0700604 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
605 return 0;
606
Paul Walmsley55ed9692010-01-26 20:12:59 -0700607 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300608}
609
610/**
611 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
612 * @oh: struct omap_hwmod *
613 *
614 * Called from _init_clocks(). Populates the @oh _clk (main
615 * functional clock pointer) if a main_clk is present. Returns 0 on
616 * success or -EINVAL on error.
617 */
618static int _init_main_clk(struct omap_hwmod *oh)
619{
Paul Walmsley63c85232009-09-03 20:14:03 +0300620 int ret = 0;
621
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700622 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300623 return 0;
624
Benoit Cousson63403382010-05-20 12:31:10 -0600625 oh->_clk = omap_clk_get_by_name(oh->main_clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600626 if (!oh->_clk) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600627 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
628 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600629 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600630 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300631
Benoit Cousson63403382010-05-20 12:31:10 -0600632 if (!oh->_clk->clkdm)
633 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
634 oh->main_clk, oh->_clk->name);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700635
Paul Walmsley63c85232009-09-03 20:14:03 +0300636 return ret;
637}
638
639/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600640 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300641 * @oh: struct omap_hwmod *
642 *
643 * Called from _init_clocks(). Populates the @oh OCP slave interface
644 * clock pointers. Returns 0 on success or -EINVAL on error.
645 */
646static int _init_interface_clks(struct omap_hwmod *oh)
647{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600648 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600649 struct list_head *p;
Paul Walmsley63c85232009-09-03 20:14:03 +0300650 struct clk *c;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600651 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300652 int ret = 0;
653
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600654 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600655
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600656 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600657 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700658 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300659 continue;
660
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700661 c = omap_clk_get_by_name(os->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600662 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600663 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
664 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300665 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600666 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300667 os->_clk = c;
668 }
669
670 return ret;
671}
672
673/**
674 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
675 * @oh: struct omap_hwmod *
676 *
677 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
678 * clock pointers. Returns 0 on success or -EINVAL on error.
679 */
680static int _init_opt_clks(struct omap_hwmod *oh)
681{
682 struct omap_hwmod_opt_clk *oc;
683 struct clk *c;
684 int i;
685 int ret = 0;
686
687 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700688 c = omap_clk_get_by_name(oc->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600689 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600690 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
691 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300692 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600693 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300694 oc->_clk = c;
695 }
696
697 return ret;
698}
699
700/**
701 * _enable_clocks - enable hwmod main clock and interface clocks
702 * @oh: struct omap_hwmod *
703 *
704 * Enables all clocks necessary for register reads and writes to succeed
705 * on the hwmod @oh. Returns 0.
706 */
707static int _enable_clocks(struct omap_hwmod *oh)
708{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600709 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600710 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600711 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300712
713 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
714
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600715 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300716 clk_enable(oh->_clk);
717
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600718 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600719
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600720 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600721 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300722
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600723 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
724 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300725 }
726
727 /* The opt clocks are controlled by the device driver. */
728
729 return 0;
730}
731
732/**
733 * _disable_clocks - disable hwmod main clock and interface clocks
734 * @oh: struct omap_hwmod *
735 *
736 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
737 */
738static int _disable_clocks(struct omap_hwmod *oh)
739{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600740 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600741 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600742 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300743
744 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
745
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600746 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300747 clk_disable(oh->_clk);
748
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600749 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600750
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600751 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600752 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300753
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600754 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
755 clk_disable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300756 }
757
758 /* The opt clocks are controlled by the device driver. */
759
760 return 0;
761}
762
Benoit Cousson96835af2010-09-21 18:57:58 +0200763static void _enable_optional_clocks(struct omap_hwmod *oh)
764{
765 struct omap_hwmod_opt_clk *oc;
766 int i;
767
768 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
769
770 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
771 if (oc->_clk) {
772 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
773 oc->_clk->name);
774 clk_enable(oc->_clk);
775 }
776}
777
778static void _disable_optional_clocks(struct omap_hwmod *oh)
779{
780 struct omap_hwmod_opt_clk *oc;
781 int i;
782
783 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
784
785 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
786 if (oc->_clk) {
787 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
788 oc->_clk->name);
789 clk_disable(oc->_clk);
790 }
791}
792
Paul Walmsley63c85232009-09-03 20:14:03 +0300793/**
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600794 * _omap4_enable_module - enable CLKCTRL modulemode on OMAP4
Benoit Cousson45c38252011-07-10 05:56:33 -0600795 * @oh: struct omap_hwmod *
796 *
797 * Enables the PRCM module mode related to the hwmod @oh.
798 * No return value.
799 */
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600800static void _omap4_enable_module(struct omap_hwmod *oh)
Benoit Cousson45c38252011-07-10 05:56:33 -0600801{
Benoit Cousson45c38252011-07-10 05:56:33 -0600802 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
803 return;
804
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600805 pr_debug("omap_hwmod: %s: %s: %d\n",
806 oh->name, __func__, oh->prcm.omap4.modulemode);
Benoit Cousson45c38252011-07-10 05:56:33 -0600807
808 omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
809 oh->clkdm->prcm_partition,
810 oh->clkdm->cm_inst,
811 oh->clkdm->clkdm_offs,
812 oh->prcm.omap4.clkctrl_offs);
813}
814
815/**
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800816 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
817 * @oh: struct omap_hwmod *
818 *
819 * Wait for a module @oh to enter slave idle. Returns 0 if the module
820 * does not have an IDLEST bit or if the module successfully enters
821 * slave idle; otherwise, pass along the return value of the
822 * appropriate *_cm*_wait_module_idle() function.
823 */
824static int _omap4_wait_target_disable(struct omap_hwmod *oh)
825{
826 if (!cpu_is_omap44xx())
827 return 0;
828
829 if (!oh)
830 return -EINVAL;
831
832 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
833 return 0;
834
835 if (oh->flags & HWMOD_NO_IDLEST)
836 return 0;
837
838 return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
839 oh->clkdm->cm_inst,
840 oh->clkdm->clkdm_offs,
841 oh->prcm.omap4.clkctrl_offs);
842}
843
844/**
Paul Walmsley212738a2011-07-09 19:14:06 -0600845 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
846 * @oh: struct omap_hwmod *oh
847 *
848 * Count and return the number of MPU IRQs associated with the hwmod
849 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
850 * NULL.
851 */
852static int _count_mpu_irqs(struct omap_hwmod *oh)
853{
854 struct omap_hwmod_irq_info *ohii;
855 int i = 0;
856
857 if (!oh || !oh->mpu_irqs)
858 return 0;
859
860 do {
861 ohii = &oh->mpu_irqs[i++];
862 } while (ohii->irq != -1);
863
sricharancc1b0762011-11-23 14:35:07 -0800864 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -0600865}
866
867/**
Paul Walmsleybc614952011-07-09 19:14:07 -0600868 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
869 * @oh: struct omap_hwmod *oh
870 *
871 * Count and return the number of SDMA request lines associated with
872 * the hwmod @oh. Used to allocate struct resource data. Returns 0
873 * if @oh is NULL.
874 */
875static int _count_sdma_reqs(struct omap_hwmod *oh)
876{
877 struct omap_hwmod_dma_info *ohdi;
878 int i = 0;
879
880 if (!oh || !oh->sdma_reqs)
881 return 0;
882
883 do {
884 ohdi = &oh->sdma_reqs[i++];
885 } while (ohdi->dma_req != -1);
886
sricharancc1b0762011-11-23 14:35:07 -0800887 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -0600888}
889
890/**
Paul Walmsley78183f32011-07-09 19:14:05 -0600891 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
892 * @oh: struct omap_hwmod *oh
893 *
894 * Count and return the number of address space ranges associated with
895 * the hwmod @oh. Used to allocate struct resource data. Returns 0
896 * if @oh is NULL.
897 */
898static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
899{
900 struct omap_hwmod_addr_space *mem;
901 int i = 0;
902
903 if (!os || !os->addr)
904 return 0;
905
906 do {
907 mem = &os->addr[i++];
908 } while (mem->pa_start != mem->pa_end);
909
sricharancc1b0762011-11-23 14:35:07 -0800910 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -0600911}
912
913/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -0600914 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
915 * @oh: struct omap_hwmod * to operate on
916 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
917 * @irq: pointer to an unsigned int to store the MPU IRQ number to
918 *
919 * Retrieve a MPU hardware IRQ line number named by @name associated
920 * with the IP block pointed to by @oh. The IRQ number will be filled
921 * into the address pointed to by @dma. When @name is non-null, the
922 * IRQ line number associated with the named entry will be returned.
923 * If @name is null, the first matching entry will be returned. Data
924 * order is not meaningful in hwmod data, so callers are strongly
925 * encouraged to use a non-null @name whenever possible to avoid
926 * unpredictable effects if hwmod data is later added that causes data
927 * ordering to change. Returns 0 upon success or a negative error
928 * code upon error.
929 */
930static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
931 unsigned int *irq)
932{
933 int i;
934 bool found = false;
935
936 if (!oh->mpu_irqs)
937 return -ENOENT;
938
939 i = 0;
940 while (oh->mpu_irqs[i].irq != -1) {
941 if (name == oh->mpu_irqs[i].name ||
942 !strcmp(name, oh->mpu_irqs[i].name)) {
943 found = true;
944 break;
945 }
946 i++;
947 }
948
949 if (!found)
950 return -ENOENT;
951
952 *irq = oh->mpu_irqs[i].irq;
953
954 return 0;
955}
956
957/**
958 * _get_sdma_req_by_name - fetch SDMA request line ID by name
959 * @oh: struct omap_hwmod * to operate on
960 * @name: pointer to the name of the SDMA request line to fetch (optional)
961 * @dma: pointer to an unsigned int to store the request line ID to
962 *
963 * Retrieve an SDMA request line ID named by @name on the IP block
964 * pointed to by @oh. The ID will be filled into the address pointed
965 * to by @dma. When @name is non-null, the request line ID associated
966 * with the named entry will be returned. If @name is null, the first
967 * matching entry will be returned. Data order is not meaningful in
968 * hwmod data, so callers are strongly encouraged to use a non-null
969 * @name whenever possible to avoid unpredictable effects if hwmod
970 * data is later added that causes data ordering to change. Returns 0
971 * upon success or a negative error code upon error.
972 */
973static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
974 unsigned int *dma)
975{
976 int i;
977 bool found = false;
978
979 if (!oh->sdma_reqs)
980 return -ENOENT;
981
982 i = 0;
983 while (oh->sdma_reqs[i].dma_req != -1) {
984 if (name == oh->sdma_reqs[i].name ||
985 !strcmp(name, oh->sdma_reqs[i].name)) {
986 found = true;
987 break;
988 }
989 i++;
990 }
991
992 if (!found)
993 return -ENOENT;
994
995 *dma = oh->sdma_reqs[i].dma_req;
996
997 return 0;
998}
999
1000/**
1001 * _get_addr_space_by_name - fetch address space start & end by name
1002 * @oh: struct omap_hwmod * to operate on
1003 * @name: pointer to the name of the address space to fetch (optional)
1004 * @pa_start: pointer to a u32 to store the starting address to
1005 * @pa_end: pointer to a u32 to store the ending address to
1006 *
1007 * Retrieve address space start and end addresses for the IP block
1008 * pointed to by @oh. The data will be filled into the addresses
1009 * pointed to by @pa_start and @pa_end. When @name is non-null, the
1010 * address space data associated with the named entry will be
1011 * returned. If @name is null, the first matching entry will be
1012 * returned. Data order is not meaningful in hwmod data, so callers
1013 * are strongly encouraged to use a non-null @name whenever possible
1014 * to avoid unpredictable effects if hwmod data is later added that
1015 * causes data ordering to change. Returns 0 upon success or a
1016 * negative error code upon error.
1017 */
1018static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1019 u32 *pa_start, u32 *pa_end)
1020{
1021 int i, j;
1022 struct omap_hwmod_ocp_if *os;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001023 struct list_head *p = NULL;
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001024 bool found = false;
1025
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001026 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001027
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001028 i = 0;
1029 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001030 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001031
1032 if (!os->addr)
1033 return -ENOENT;
1034
1035 j = 0;
1036 while (os->addr[j].pa_start != os->addr[j].pa_end) {
1037 if (name == os->addr[j].name ||
1038 !strcmp(name, os->addr[j].name)) {
1039 found = true;
1040 break;
1041 }
1042 j++;
1043 }
1044
1045 if (found)
1046 break;
1047 }
1048
1049 if (!found)
1050 return -ENOENT;
1051
1052 *pa_start = os->addr[j].pa_start;
1053 *pa_end = os->addr[j].pa_end;
1054
1055 return 0;
1056}
1057
1058/**
Paul Walmsley24dbc212012-04-19 04:04:29 -06001059 * _save_mpu_port_index - find and save the index to @oh's MPU port
Paul Walmsley63c85232009-09-03 20:14:03 +03001060 * @oh: struct omap_hwmod *
1061 *
Paul Walmsley24dbc212012-04-19 04:04:29 -06001062 * Determines the array index of the OCP slave port that the MPU uses
1063 * to address the device, and saves it into the struct omap_hwmod.
1064 * Intended to be called during hwmod registration only. No return
1065 * value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001066 */
Paul Walmsley24dbc212012-04-19 04:04:29 -06001067static void __init _save_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001068{
Paul Walmsley24dbc212012-04-19 04:04:29 -06001069 struct omap_hwmod_ocp_if *os = NULL;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001070 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001071 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001072
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001073 if (!oh)
Paul Walmsley24dbc212012-04-19 04:04:29 -06001074 return;
1075
1076 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001077
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001078 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001079
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001080 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001081 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +03001082 if (os->user & OCP_USER_MPU) {
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001083 oh->_mpu_port = os;
Paul Walmsley24dbc212012-04-19 04:04:29 -06001084 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001085 break;
1086 }
1087 }
1088
Paul Walmsley24dbc212012-04-19 04:04:29 -06001089 return;
Paul Walmsley63c85232009-09-03 20:14:03 +03001090}
1091
1092/**
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001093 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1094 * @oh: struct omap_hwmod *
1095 *
1096 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1097 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1098 * communicate with the IP block. This interface need not be directly
1099 * connected to the MPU (and almost certainly is not), but is directly
1100 * connected to the IP block represented by @oh. Returns a pointer
1101 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1102 * error or if there does not appear to be a path from the MPU to this
1103 * IP block.
1104 */
1105static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1106{
1107 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1108 return NULL;
1109
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001110 return oh->_mpu_port;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001111};
1112
1113/**
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001114 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
Paul Walmsley63c85232009-09-03 20:14:03 +03001115 * @oh: struct omap_hwmod *
1116 *
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001117 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1118 * the register target MPU address space; or returns NULL upon error.
Paul Walmsley63c85232009-09-03 20:14:03 +03001119 */
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001120static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001121{
1122 struct omap_hwmod_ocp_if *os;
1123 struct omap_hwmod_addr_space *mem;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001124 int found = 0, i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001125
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001126 os = _find_mpu_rt_port(oh);
Paul Walmsley24dbc212012-04-19 04:04:29 -06001127 if (!os || !os->addr)
Paul Walmsley78183f32011-07-09 19:14:05 -06001128 return NULL;
1129
1130 do {
1131 mem = &os->addr[i++];
1132 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +03001133 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001134 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +03001135
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001136 return (found) ? mem : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001137}
1138
1139/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001140 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001141 * @oh: struct omap_hwmod *
1142 *
1143 * If module is marked as SWSUP_SIDLE, force the module out of slave
1144 * idle; otherwise, configure it for smart-idle. If module is marked
1145 * as SWSUP_MSUSPEND, force the module out of master standby;
1146 * otherwise, configure it for smart-standby. No return value.
1147 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001148static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001149{
Paul Walmsley43b40992010-02-22 22:09:34 -07001150 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001151 u32 v;
1152
Paul Walmsley43b40992010-02-22 22:09:34 -07001153 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001154 return;
1155
1156 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001157 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001158
Paul Walmsley43b40992010-02-22 22:09:34 -07001159 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001160 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1161 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
1162 _set_slave_idlemode(oh, idlemode, &v);
1163 }
1164
Paul Walmsley43b40992010-02-22 22:09:34 -07001165 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001166 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1167 idlemode = HWMOD_IDLEMODE_NO;
1168 } else {
1169 if (sf & SYSC_HAS_ENAWAKEUP)
1170 _enable_wakeup(oh, &v);
1171 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1172 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1173 else
1174 idlemode = HWMOD_IDLEMODE_SMART;
1175 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001176 _set_master_standbymode(oh, idlemode, &v);
1177 }
1178
Paul Walmsleya16b1f72009-12-08 16:34:17 -07001179 /*
1180 * XXX The clock framework should handle this, by
1181 * calling into this code. But this must wait until the
1182 * clock structures are tagged with omap_hwmod entries
1183 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001184 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1185 (sf & SYSC_HAS_CLOCKACTIVITY))
1186 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001187
Rajendra Nayak9980ce52010-09-21 19:58:30 +05301188 /* If slave is in SMARTIDLE, also enable wakeup */
1189 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07001190 _enable_wakeup(oh, &v);
1191
1192 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -06001193
1194 /*
1195 * Set the autoidle bit only after setting the smartidle bit
1196 * Setting this will not have any impact on the other modules.
1197 */
1198 if (sf & SYSC_HAS_AUTOIDLE) {
1199 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1200 0 : 1;
1201 _set_module_autoidle(oh, idlemode, &v);
1202 _write_sysconfig(v, oh);
1203 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001204}
1205
1206/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001207 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001208 * @oh: struct omap_hwmod *
1209 *
1210 * If module is marked as SWSUP_SIDLE, force the module into slave
1211 * idle; otherwise, configure it for smart-idle. If module is marked
1212 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1213 * configure it for smart-standby. No return value.
1214 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001215static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001216{
Paul Walmsley43b40992010-02-22 22:09:34 -07001217 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001218 u32 v;
1219
Paul Walmsley43b40992010-02-22 22:09:34 -07001220 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001221 return;
1222
1223 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001224 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001225
Paul Walmsley43b40992010-02-22 22:09:34 -07001226 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001227 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1228 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
1229 _set_slave_idlemode(oh, idlemode, &v);
1230 }
1231
Paul Walmsley43b40992010-02-22 22:09:34 -07001232 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001233 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1234 idlemode = HWMOD_IDLEMODE_FORCE;
1235 } else {
1236 if (sf & SYSC_HAS_ENAWAKEUP)
1237 _enable_wakeup(oh, &v);
1238 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1239 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1240 else
1241 idlemode = HWMOD_IDLEMODE_SMART;
1242 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001243 _set_master_standbymode(oh, idlemode, &v);
1244 }
1245
Benoit Cousson86009eb2010-12-21 21:31:28 -07001246 /* If slave is in SMARTIDLE, also enable wakeup */
1247 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1248 _enable_wakeup(oh, &v);
1249
Paul Walmsley63c85232009-09-03 20:14:03 +03001250 _write_sysconfig(v, oh);
1251}
1252
1253/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001254 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001255 * @oh: struct omap_hwmod *
1256 *
1257 * Force the module into slave idle and master suspend. No return
1258 * value.
1259 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001260static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001261{
1262 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001263 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001264
Paul Walmsley43b40992010-02-22 22:09:34 -07001265 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001266 return;
1267
1268 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001269 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001270
Paul Walmsley43b40992010-02-22 22:09:34 -07001271 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001272 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1273
Paul Walmsley43b40992010-02-22 22:09:34 -07001274 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001275 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1276
Paul Walmsley43b40992010-02-22 22:09:34 -07001277 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001278 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001279
1280 _write_sysconfig(v, oh);
1281}
1282
1283/**
1284 * _lookup - find an omap_hwmod by name
1285 * @name: find an omap_hwmod by name
1286 *
1287 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001288 */
1289static struct omap_hwmod *_lookup(const char *name)
1290{
1291 struct omap_hwmod *oh, *temp_oh;
1292
1293 oh = NULL;
1294
1295 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1296 if (!strcmp(name, temp_oh->name)) {
1297 oh = temp_oh;
1298 break;
1299 }
1300 }
1301
1302 return oh;
1303}
Benoit Cousson6ae76992011-07-10 05:56:30 -06001304/**
1305 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1306 * @oh: struct omap_hwmod *
1307 *
1308 * Convert a clockdomain name stored in a struct omap_hwmod into a
1309 * clockdomain pointer, and save it into the struct omap_hwmod.
1310 * return -EINVAL if clkdm_name does not exist or if the lookup failed.
1311 */
1312static int _init_clkdm(struct omap_hwmod *oh)
1313{
1314 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1315 return 0;
1316
1317 if (!oh->clkdm_name) {
1318 pr_warning("omap_hwmod: %s: no clkdm_name\n", oh->name);
1319 return -EINVAL;
1320 }
1321
1322 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1323 if (!oh->clkdm) {
1324 pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
1325 oh->name, oh->clkdm_name);
1326 return -EINVAL;
1327 }
1328
1329 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1330 oh->name, oh->clkdm_name);
1331
1332 return 0;
1333}
Paul Walmsley63c85232009-09-03 20:14:03 +03001334
1335/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001336 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1337 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001338 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -06001339 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001340 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001341 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001342 * Resolves all clock names embedded in the hwmod. Returns 0 on
1343 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001344 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001345static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001346{
1347 int ret = 0;
1348
Paul Walmsley48d54f32011-02-23 00:14:07 -07001349 if (oh->_state != _HWMOD_STATE_REGISTERED)
1350 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001351
1352 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1353
1354 ret |= _init_main_clk(oh);
1355 ret |= _init_interface_clks(oh);
1356 ret |= _init_opt_clks(oh);
Benoit Cousson6ae76992011-07-10 05:56:30 -06001357 ret |= _init_clkdm(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001358
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001359 if (!ret)
1360 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001361 else
1362 pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001363
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001364 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001365}
1366
1367/**
1368 * _wait_target_ready - wait for a module to leave slave idle
1369 * @oh: struct omap_hwmod *
1370 *
1371 * Wait for a module @oh to leave slave idle. Returns 0 if the module
1372 * does not have an IDLEST bit or if the module successfully leaves
1373 * slave idle; otherwise, pass along the return value of the
Benoit Coussond0f06312011-07-10 05:56:30 -06001374 * appropriate *_cm*_wait_module_ready() function.
Paul Walmsley63c85232009-09-03 20:14:03 +03001375 */
1376static int _wait_target_ready(struct omap_hwmod *oh)
1377{
1378 struct omap_hwmod_ocp_if *os;
1379 int ret;
1380
1381 if (!oh)
1382 return -EINVAL;
1383
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001384 if (oh->flags & HWMOD_NO_IDLEST)
Paul Walmsley63c85232009-09-03 20:14:03 +03001385 return 0;
1386
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001387 os = _find_mpu_rt_port(oh);
1388 if (!os)
Paul Walmsley63c85232009-09-03 20:14:03 +03001389 return 0;
1390
1391 /* XXX check module SIDLEMODE */
1392
1393 /* XXX check clock enable states */
1394
1395 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1396 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
1397 oh->prcm.omap2.idlest_reg_id,
1398 oh->prcm.omap2.idlest_idle_bit);
Paul Walmsley63c85232009-09-03 20:14:03 +03001399 } else if (cpu_is_omap44xx()) {
Benoit Coussond0f06312011-07-10 05:56:30 -06001400 if (!oh->clkdm)
1401 return -EINVAL;
1402
1403 ret = omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
1404 oh->clkdm->cm_inst,
1405 oh->clkdm->clkdm_offs,
1406 oh->prcm.omap4.clkctrl_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03001407 } else {
1408 BUG();
1409 };
1410
1411 return ret;
1412}
1413
1414/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001415 * _lookup_hardreset - fill register bit info for this hwmod/reset line
Benoît Cousson5365efb2010-09-21 10:34:11 -06001416 * @oh: struct omap_hwmod *
1417 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001418 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
Benoît Cousson5365efb2010-09-21 10:34:11 -06001419 *
1420 * Return the bit position of the reset line that match the
1421 * input name. Return -ENOENT if not found.
1422 */
omar ramirezcc1226e2011-03-04 13:32:44 -07001423static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1424 struct omap_hwmod_rst_info *ohri)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001425{
1426 int i;
1427
1428 for (i = 0; i < oh->rst_lines_cnt; i++) {
1429 const char *rst_line = oh->rst_lines[i].name;
1430 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001431 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1432 ohri->st_shift = oh->rst_lines[i].st_shift;
1433 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1434 oh->name, __func__, rst_line, ohri->rst_shift,
1435 ohri->st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001436
omar ramirezcc1226e2011-03-04 13:32:44 -07001437 return 0;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001438 }
1439 }
1440
1441 return -ENOENT;
1442}
1443
1444/**
1445 * _assert_hardreset - assert the HW reset line of submodules
1446 * contained in the hwmod module.
1447 * @oh: struct omap_hwmod *
1448 * @name: name of the reset line to lookup and assert
1449 *
1450 * Some IP like dsp, ipu or iva contain processor that require
1451 * an HW reset line to be assert / deassert in order to enable fully
1452 * the IP.
1453 */
1454static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1455{
omar ramirezcc1226e2011-03-04 13:32:44 -07001456 struct omap_hwmod_rst_info ohri;
1457 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001458
1459 if (!oh)
1460 return -EINVAL;
1461
omar ramirezcc1226e2011-03-04 13:32:44 -07001462 ret = _lookup_hardreset(oh, name, &ohri);
1463 if (IS_ERR_VALUE(ret))
1464 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001465
1466 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1467 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001468 ohri.rst_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001469 else if (cpu_is_omap44xx())
Benoit Coussoneaac3292011-07-10 05:56:31 -06001470 return omap4_prminst_assert_hardreset(ohri.rst_shift,
1471 oh->clkdm->pwrdm.ptr->prcm_partition,
1472 oh->clkdm->pwrdm.ptr->prcm_offs,
1473 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001474 else
1475 return -EINVAL;
1476}
1477
1478/**
1479 * _deassert_hardreset - deassert the HW reset line of submodules contained
1480 * in the hwmod module.
1481 * @oh: struct omap_hwmod *
1482 * @name: name of the reset line to look up and deassert
1483 *
1484 * Some IP like dsp, ipu or iva contain processor that require
1485 * an HW reset line to be assert / deassert in order to enable fully
1486 * the IP.
1487 */
1488static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1489{
omar ramirezcc1226e2011-03-04 13:32:44 -07001490 struct omap_hwmod_rst_info ohri;
1491 int ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001492
1493 if (!oh)
1494 return -EINVAL;
1495
omar ramirezcc1226e2011-03-04 13:32:44 -07001496 ret = _lookup_hardreset(oh, name, &ohri);
1497 if (IS_ERR_VALUE(ret))
1498 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001499
omar ramirezcc1226e2011-03-04 13:32:44 -07001500 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1501 ret = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
1502 ohri.rst_shift,
1503 ohri.st_shift);
1504 } else if (cpu_is_omap44xx()) {
1505 if (ohri.st_shift)
1506 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
1507 oh->name, name);
Benoit Coussoneaac3292011-07-10 05:56:31 -06001508 ret = omap4_prminst_deassert_hardreset(ohri.rst_shift,
1509 oh->clkdm->pwrdm.ptr->prcm_partition,
1510 oh->clkdm->pwrdm.ptr->prcm_offs,
1511 oh->prcm.omap4.rstctrl_offs);
omar ramirezcc1226e2011-03-04 13:32:44 -07001512 } else {
Benoît Cousson5365efb2010-09-21 10:34:11 -06001513 return -EINVAL;
omar ramirezcc1226e2011-03-04 13:32:44 -07001514 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001515
omar ramirezcc1226e2011-03-04 13:32:44 -07001516 if (ret == -EBUSY)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001517 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1518
omar ramirezcc1226e2011-03-04 13:32:44 -07001519 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001520}
1521
1522/**
1523 * _read_hardreset - read the HW reset line state of submodules
1524 * contained in the hwmod module
1525 * @oh: struct omap_hwmod *
1526 * @name: name of the reset line to look up and read
1527 *
1528 * Return the state of the reset line.
1529 */
1530static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1531{
omar ramirezcc1226e2011-03-04 13:32:44 -07001532 struct omap_hwmod_rst_info ohri;
1533 u8 ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001534
1535 if (!oh)
1536 return -EINVAL;
1537
omar ramirezcc1226e2011-03-04 13:32:44 -07001538 ret = _lookup_hardreset(oh, name, &ohri);
1539 if (IS_ERR_VALUE(ret))
1540 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001541
1542 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
1543 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
omar ramirezcc1226e2011-03-04 13:32:44 -07001544 ohri.st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001545 } else if (cpu_is_omap44xx()) {
Benoit Coussoneaac3292011-07-10 05:56:31 -06001546 return omap4_prminst_is_hardreset_asserted(ohri.rst_shift,
1547 oh->clkdm->pwrdm.ptr->prcm_partition,
1548 oh->clkdm->pwrdm.ptr->prcm_offs,
1549 oh->prcm.omap4.rstctrl_offs);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001550 } else {
1551 return -EINVAL;
1552 }
1553}
1554
1555/**
Paul Walmsley747834a2012-04-18 19:10:04 -06001556 * _are_any_hardreset_lines_asserted - return true if part of @oh is hard-reset
1557 * @oh: struct omap_hwmod *
1558 *
1559 * If any hardreset line associated with @oh is asserted, then return true.
1560 * Otherwise, if @oh has no hardreset lines associated with it, or if
1561 * no hardreset lines associated with @oh are asserted, then return false.
1562 * This function is used to avoid executing some parts of the IP block
1563 * enable/disable sequence if a hardreset line is set.
1564 */
1565static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1566{
1567 int i;
1568
1569 if (oh->rst_lines_cnt == 0)
1570 return false;
1571
1572 for (i = 0; i < oh->rst_lines_cnt; i++)
1573 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1574 return true;
1575
1576 return false;
1577}
1578
1579/**
1580 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1581 * @oh: struct omap_hwmod *
1582 *
1583 * Disable the PRCM module mode related to the hwmod @oh.
1584 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1585 */
1586static int _omap4_disable_module(struct omap_hwmod *oh)
1587{
1588 int v;
1589
Paul Walmsley747834a2012-04-18 19:10:04 -06001590 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1591 return -EINVAL;
1592
1593 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1594
1595 omap4_cminst_module_disable(oh->clkdm->prcm_partition,
1596 oh->clkdm->cm_inst,
1597 oh->clkdm->clkdm_offs,
1598 oh->prcm.omap4.clkctrl_offs);
1599
1600 if (_are_any_hardreset_lines_asserted(oh))
1601 return 0;
1602
1603 v = _omap4_wait_target_disable(oh);
1604 if (v)
1605 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1606 oh->name);
1607
1608 return 0;
1609}
1610
1611/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001612 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001613 * @oh: struct omap_hwmod *
1614 *
1615 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Paul Walmsley30e105c2012-04-19 00:49:09 -06001616 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1617 * reset this way, -EINVAL if the hwmod is in the wrong state,
1618 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001619 *
1620 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001621 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001622 * use the SYSCONFIG softreset bit to provide the status.
1623 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001624 * Note that some IP like McBSP do have reset control but don't have
1625 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001626 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001627static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001628{
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001629 u32 v, softrst_mask;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001630 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001631 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001632
Paul Walmsley43b40992010-02-22 22:09:34 -07001633 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001634 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley30e105c2012-04-19 00:49:09 -06001635 return -ENOENT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001636
1637 /* clocks must be on for this operation */
1638 if (oh->_state != _HWMOD_STATE_ENABLED) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001639 pr_warning("omap_hwmod: %s: reset can only be entered from "
1640 "enabled state\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001641 return -EINVAL;
1642 }
1643
Benoit Cousson96835af2010-09-21 18:57:58 +02001644 /* For some modules, all optionnal clocks need to be enabled as well */
1645 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1646 _enable_optional_clocks(oh);
1647
Paul Walmsleybd361792010-12-14 12:42:35 -07001648 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001649
1650 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001651 ret = _set_softreset(oh, &v);
1652 if (ret)
1653 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001654 _write_sysconfig(v, oh);
1655
Fernando Guzman Lugod99de7f2012-04-13 05:08:03 -06001656 if (oh->class->sysc->srst_udelay)
1657 udelay(oh->class->sysc->srst_udelay);
1658
Benoit Cousson2cb06812010-09-21 18:57:59 +02001659 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001660 omap_test_timeout((omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001661 oh->class->sysc->syss_offs)
1662 & SYSS_RESETDONE_MASK),
1663 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001664 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
1665 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001666 omap_test_timeout(!(omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001667 oh->class->sysc->sysc_offs)
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001668 & softrst_mask),
Benoit Cousson2cb06812010-09-21 18:57:59 +02001669 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001670 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001671
Benoît Cousson5365efb2010-09-21 10:34:11 -06001672 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001673 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1674 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001675 else
Benoît Cousson5365efb2010-09-21 10:34:11 -06001676 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001677
1678 /*
1679 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1680 * _wait_target_ready() or _reset()
1681 */
1682
Benoit Cousson96835af2010-09-21 18:57:58 +02001683 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1684
1685dis_opt_clks:
1686 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1687 _disable_optional_clocks(oh);
1688
1689 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001690}
1691
1692/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001693 * _reset - reset an omap_hwmod
1694 * @oh: struct omap_hwmod *
1695 *
Paul Walmsley30e105c2012-04-19 00:49:09 -06001696 * Resets an omap_hwmod @oh. If the module has a custom reset
1697 * function pointer defined, then call it to reset the IP block, and
1698 * pass along its return value to the caller. Otherwise, if the IP
1699 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1700 * associated with it, call a function to reset the IP block via that
1701 * method, and pass along the return value to the caller. Finally, if
1702 * the IP block has some hardreset lines associated with it, assert
1703 * all of those, but do _not_ deassert them. (This is because driver
1704 * authors have expressed an apparent requirement to control the
1705 * deassertion of the hardreset lines themselves.)
1706 *
1707 * The default software reset mechanism for most OMAP IP blocks is
1708 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1709 * hwmods cannot be reset via this method. Some are not targets and
1710 * therefore have no OCP header registers to access. Others (like the
1711 * IVA) have idiosyncratic reset sequences. So for these relatively
1712 * rare cases, custom reset code can be supplied in the struct
1713 * omap_hwmod_class .reset function pointer. Passes along the return
1714 * value from either _ocp_softreset() or the custom reset function -
1715 * these must return -EINVAL if the hwmod cannot be reset this way or
1716 * if the hwmod is in the wrong state, -ETIMEDOUT if the module did
1717 * not reset in time, or 0 upon success.
Paul Walmsleybd361792010-12-14 12:42:35 -07001718 */
1719static int _reset(struct omap_hwmod *oh)
1720{
Paul Walmsley30e105c2012-04-19 00:49:09 -06001721 int i, r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001722
1723 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1724
Paul Walmsley30e105c2012-04-19 00:49:09 -06001725 if (oh->class->reset) {
1726 r = oh->class->reset(oh);
1727 } else {
1728 if (oh->rst_lines_cnt > 0) {
1729 for (i = 0; i < oh->rst_lines_cnt; i++)
1730 _assert_hardreset(oh, oh->rst_lines[i].name);
1731 return 0;
1732 } else {
1733 r = _ocp_softreset(oh);
1734 if (r == -ENOENT)
1735 r = 0;
1736 }
1737 }
Paul Walmsleybd361792010-12-14 12:42:35 -07001738
Paul Walmsley30e105c2012-04-19 00:49:09 -06001739 /*
1740 * OCP_SYSCONFIG bits need to be reprogrammed after a
1741 * softreset. The _enable() function should be split to avoid
1742 * the rewrite of the OCP_SYSCONFIG register.
1743 */
Rajendra Nayak28008522012-03-13 22:55:23 +05301744 if (oh->class->sysc) {
1745 _update_sysc_cache(oh);
1746 _enable_sysc(oh);
1747 }
1748
Paul Walmsley30e105c2012-04-19 00:49:09 -06001749 return r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001750}
1751
1752/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001753 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001754 * @oh: struct omap_hwmod *
1755 *
1756 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001757 * register target. Returns -EINVAL if the hwmod is in the wrong
1758 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001759 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001760static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001761{
Paul Walmsley747834a2012-04-18 19:10:04 -06001762 int r;
Rajendra Nayak665d0012011-07-10 05:57:07 -06001763 int hwsup = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001764
Benoit Cousson34617e22011-07-01 22:54:07 +02001765 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1766
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001767 /*
Paul Walmsley64813c32012-04-18 19:10:03 -06001768 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
1769 * state at init. Now that someone is really trying to enable
1770 * them, just ensure that the hwmod mux is set.
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001771 */
1772 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
1773 /*
1774 * If the caller has mux data populated, do the mux'ing
1775 * which wouldn't have been done as part of the _enable()
1776 * done during setup.
1777 */
1778 if (oh->mux)
1779 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
1780
1781 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
1782 return 0;
1783 }
1784
Paul Walmsley63c85232009-09-03 20:14:03 +03001785 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1786 oh->_state != _HWMOD_STATE_IDLE &&
1787 oh->_state != _HWMOD_STATE_DISABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001788 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
1789 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001790 return -EINVAL;
1791 }
1792
Benoit Cousson31f62862011-07-01 22:54:05 +02001793 /*
Paul Walmsley747834a2012-04-18 19:10:04 -06001794 * If an IP block contains HW reset lines and any of them are
1795 * asserted, we let integration code associated with that
1796 * block handle the enable. We've received very little
1797 * information on what those driver authors need, and until
1798 * detailed information is provided and the driver code is
1799 * posted to the public lists, this is probably the best we
1800 * can do.
Benoit Cousson31f62862011-07-01 22:54:05 +02001801 */
Paul Walmsley747834a2012-04-18 19:10:04 -06001802 if (_are_any_hardreset_lines_asserted(oh))
1803 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001804
Rajendra Nayak665d0012011-07-10 05:57:07 -06001805 /* Mux pins for device runtime if populated */
1806 if (oh->mux && (!oh->mux->enabled ||
1807 ((oh->_state == _HWMOD_STATE_IDLE) &&
1808 oh->mux->pads_dynamic)))
1809 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
Benoit Cousson34617e22011-07-01 22:54:07 +02001810
Rajendra Nayak665d0012011-07-10 05:57:07 -06001811 _add_initiator_dep(oh, mpu_oh);
1812
1813 if (oh->clkdm) {
1814 /*
1815 * A clockdomain must be in SW_SUP before enabling
1816 * completely the module. The clockdomain can be set
1817 * in HW_AUTO only when the module become ready.
1818 */
1819 hwsup = clkdm_in_hwsup(oh->clkdm);
1820 r = clkdm_hwmod_enable(oh->clkdm, oh);
1821 if (r) {
1822 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1823 oh->name, oh->clkdm->name, r);
1824 return r;
1825 }
Benoit Cousson34617e22011-07-01 22:54:07 +02001826 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06001827
1828 _enable_clocks(oh);
Kevin Hilman9ebfd282012-06-18 12:12:23 -06001829 if (soc_ops.enable_module)
1830 soc_ops.enable_module(oh);
Benoit Cousson34617e22011-07-01 22:54:07 +02001831
Rajendra Nayak665d0012011-07-10 05:57:07 -06001832 r = _wait_target_ready(oh);
1833 if (!r) {
1834 /*
1835 * Set the clockdomain to HW_AUTO only if the target is ready,
1836 * assuming that the previous state was HW_AUTO
1837 */
1838 if (oh->clkdm && hwsup)
1839 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02001840
Rajendra Nayak665d0012011-07-10 05:57:07 -06001841 oh->_state = _HWMOD_STATE_ENABLED;
1842
1843 /* Access the sysconfig only if the target is ready */
1844 if (oh->class->sysc) {
1845 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1846 _update_sysc_cache(oh);
1847 _enable_sysc(oh);
1848 }
1849 } else {
1850 _disable_clocks(oh);
1851 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1852 oh->name, r);
1853
1854 if (oh->clkdm)
1855 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001856 }
1857
Paul Walmsley63c85232009-09-03 20:14:03 +03001858 return r;
1859}
1860
1861/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001862 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001863 * @oh: struct omap_hwmod *
1864 *
1865 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001866 * no further work. Returns -EINVAL if the hwmod is in the wrong
1867 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001868 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001869static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001870{
Benoit Cousson34617e22011-07-01 22:54:07 +02001871 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1872
Paul Walmsley63c85232009-09-03 20:14:03 +03001873 if (oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001874 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
1875 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001876 return -EINVAL;
1877 }
1878
Paul Walmsley747834a2012-04-18 19:10:04 -06001879 if (_are_any_hardreset_lines_asserted(oh))
1880 return 0;
1881
Paul Walmsley43b40992010-02-22 22:09:34 -07001882 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001883 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001884 _del_initiator_dep(oh, mpu_oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001885
Kevin Hilman9ebfd282012-06-18 12:12:23 -06001886 if (soc_ops.disable_module)
1887 soc_ops.disable_module(oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001888
Benoit Cousson45c38252011-07-10 05:56:33 -06001889 /*
1890 * The module must be in idle mode before disabling any parents
1891 * clocks. Otherwise, the parent clock might be disabled before
1892 * the module transition is done, and thus will prevent the
1893 * transition to complete properly.
1894 */
1895 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001896 if (oh->clkdm)
1897 clkdm_hwmod_disable(oh->clkdm, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001898
Tony Lindgren8d9af882010-12-22 18:42:35 -08001899 /* Mux pins for device idle if populated */
Tony Lindgren029268e2011-03-11 11:32:25 -08001900 if (oh->mux && oh->mux->pads_dynamic)
Tony Lindgren8d9af882010-12-22 18:42:35 -08001901 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
1902
Paul Walmsley63c85232009-09-03 20:14:03 +03001903 oh->_state = _HWMOD_STATE_IDLE;
1904
1905 return 0;
1906}
1907
1908/**
Kishon Vijay Abraham I95992172011-03-10 03:50:08 -07001909 * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
1910 * @oh: struct omap_hwmod *
1911 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
1912 *
1913 * Sets the IP block's OCP autoidle bit in hardware, and updates our
1914 * local copy. Intended to be used by drivers that require
1915 * direct manipulation of the AUTOIDLE bits.
1916 * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
1917 * along the return value from _set_module_autoidle().
1918 *
1919 * Any users of this function should be scrutinized carefully.
1920 */
1921int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
1922{
1923 u32 v;
1924 int retval = 0;
1925 unsigned long flags;
1926
1927 if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
1928 return -EINVAL;
1929
1930 spin_lock_irqsave(&oh->_lock, flags);
1931
1932 v = oh->_sysc_cache;
1933
1934 retval = _set_module_autoidle(oh, autoidle, &v);
1935
1936 if (!retval)
1937 _write_sysconfig(v, oh);
1938
1939 spin_unlock_irqrestore(&oh->_lock, flags);
1940
1941 return retval;
1942}
1943
1944/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001945 * _shutdown - shutdown an omap_hwmod
1946 * @oh: struct omap_hwmod *
1947 *
1948 * Shut down an omap_hwmod @oh. This should be called when the driver
1949 * used for the hwmod is removed or unloaded or if the driver is not
1950 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1951 * state or returns 0.
1952 */
1953static int _shutdown(struct omap_hwmod *oh)
1954{
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001955 int ret, i;
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001956 u8 prev_state;
1957
Paul Walmsley63c85232009-09-03 20:14:03 +03001958 if (oh->_state != _HWMOD_STATE_IDLE &&
1959 oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001960 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
1961 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001962 return -EINVAL;
1963 }
1964
Paul Walmsley747834a2012-04-18 19:10:04 -06001965 if (_are_any_hardreset_lines_asserted(oh))
1966 return 0;
1967
Paul Walmsley63c85232009-09-03 20:14:03 +03001968 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
1969
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001970 if (oh->class->pre_shutdown) {
1971 prev_state = oh->_state;
1972 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001973 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001974 ret = oh->class->pre_shutdown(oh);
1975 if (ret) {
1976 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001977 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001978 return ret;
1979 }
1980 }
1981
Miguel Vadillo6481c732011-07-01 22:54:02 +02001982 if (oh->class->sysc) {
1983 if (oh->_state == _HWMOD_STATE_IDLE)
1984 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001985 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02001986 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001987
Benoit Cousson3827f942010-09-21 10:34:08 -06001988 /* clocks and deps are already disabled in idle */
1989 if (oh->_state == _HWMOD_STATE_ENABLED) {
1990 _del_initiator_dep(oh, mpu_oh);
1991 /* XXX what about the other system initiators here? dma, dsp */
Kevin Hilman9ebfd282012-06-18 12:12:23 -06001992 if (soc_ops.disable_module)
1993 soc_ops.disable_module(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001994 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001995 if (oh->clkdm)
1996 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06001997 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001998 /* XXX Should this code also force-disable the optional clocks? */
1999
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06002000 for (i = 0; i < oh->rst_lines_cnt; i++)
2001 _assert_hardreset(oh, oh->rst_lines[i].name);
Benoit Cousson31f62862011-07-01 22:54:05 +02002002
Tony Lindgren8d9af882010-12-22 18:42:35 -08002003 /* Mux pins to safe mode or use populated off mode values */
2004 if (oh->mux)
2005 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03002006
2007 oh->_state = _HWMOD_STATE_DISABLED;
2008
2009 return 0;
2010}
2011
2012/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002013 * _init_mpu_rt_base - populate the virtual address for a hwmod
2014 * @oh: struct omap_hwmod * to locate the virtual address
2015 *
2016 * Cache the virtual address used by the MPU to access this IP block's
2017 * registers. This address is needed early so the OCP registers that
2018 * are part of the device's address space can be ioremapped properly.
2019 * No return value.
2020 */
2021static void __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data)
2022{
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002023 struct omap_hwmod_addr_space *mem;
2024 void __iomem *va_start;
2025
2026 if (!oh)
2027 return;
2028
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002029 _save_mpu_port_index(oh);
2030
Paul Walmsley381d0332012-04-19 00:58:22 -06002031 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2032 return;
2033
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06002034 mem = _find_mpu_rt_addr_space(oh);
2035 if (!mem) {
2036 pr_debug("omap_hwmod: %s: no MPU register target found\n",
2037 oh->name);
2038 return;
2039 }
2040
2041 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
2042 if (!va_start) {
2043 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
2044 return;
2045 }
2046
2047 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
2048 oh->name, va_start);
2049
2050 oh->_mpu_rt_va = va_start;
Paul Walmsley381d0332012-04-19 00:58:22 -06002051}
2052
2053/**
2054 * _init - initialize internal data for the hwmod @oh
2055 * @oh: struct omap_hwmod *
2056 * @n: (unused)
2057 *
2058 * Look up the clocks and the address space used by the MPU to access
2059 * registers belonging to the hwmod @oh. @oh must already be
2060 * registered at this point. This is the first of two phases for
2061 * hwmod initialization. Code called here does not touch any hardware
2062 * registers, it simply prepares internal data structures. Returns 0
2063 * upon success or if the hwmod isn't registered, or -EINVAL upon
2064 * failure.
2065 */
2066static int __init _init(struct omap_hwmod *oh, void *data)
2067{
2068 int r;
2069
2070 if (oh->_state != _HWMOD_STATE_REGISTERED)
2071 return 0;
2072
2073 _init_mpu_rt_base(oh, NULL);
2074
2075 r = _init_clocks(oh, NULL);
2076 if (IS_ERR_VALUE(r)) {
2077 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2078 return -EINVAL;
2079 }
2080
2081 oh->_state = _HWMOD_STATE_INITIALIZED;
2082
2083 return 0;
2084}
2085
2086/**
Paul Walmsley64813c32012-04-18 19:10:03 -06002087 * _setup_iclk_autoidle - configure an IP block's interface clocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002088 * @oh: struct omap_hwmod *
2089 *
Paul Walmsley64813c32012-04-18 19:10:03 -06002090 * Set up the module's interface clocks. XXX This function is still mostly
2091 * a stub; implementing this properly requires iclk autoidle usecounting in
2092 * the clock code. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03002093 */
Paul Walmsley64813c32012-04-18 19:10:03 -06002094static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002095{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002096 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002097 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002098 int i = 0;
Paul Walmsley381d0332012-04-19 00:58:22 -06002099 if (oh->_state != _HWMOD_STATE_INITIALIZED)
Paul Walmsley64813c32012-04-18 19:10:03 -06002100 return;
Paul Walmsley48d54f32011-02-23 00:14:07 -07002101
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002102 p = oh->slave_ports.next;
Paul Walmsley63c85232009-09-03 20:14:03 +03002103
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002104 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002105 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002106 if (!os->_clk)
Paul Walmsley64813c32012-04-18 19:10:03 -06002107 continue;
Paul Walmsley63c85232009-09-03 20:14:03 +03002108
Paul Walmsley64813c32012-04-18 19:10:03 -06002109 if (os->flags & OCPIF_SWSUP_IDLE) {
2110 /* XXX omap_iclk_deny_idle(c); */
2111 } else {
2112 /* XXX omap_iclk_allow_idle(c); */
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002113 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +03002114 }
2115 }
2116
Paul Walmsley64813c32012-04-18 19:10:03 -06002117 return;
2118}
2119
2120/**
2121 * _setup_reset - reset an IP block during the setup process
2122 * @oh: struct omap_hwmod *
2123 *
2124 * Reset the IP block corresponding to the hwmod @oh during the setup
2125 * process. The IP block is first enabled so it can be successfully
2126 * reset. Returns 0 upon success or a negative error code upon
2127 * failure.
2128 */
2129static int __init _setup_reset(struct omap_hwmod *oh)
2130{
2131 int r;
2132
2133 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2134 return -EINVAL;
Paul Walmsley63c85232009-09-03 20:14:03 +03002135
Paul Walmsley747834a2012-04-18 19:10:04 -06002136 if (oh->rst_lines_cnt == 0) {
2137 r = _enable(oh);
2138 if (r) {
2139 pr_warning("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2140 oh->name, oh->_state);
2141 return -EINVAL;
2142 }
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002143 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002144
Rajendra Nayak28008522012-03-13 22:55:23 +05302145 if (!(oh->flags & HWMOD_INIT_NO_RESET))
Paul Walmsley64813c32012-04-18 19:10:03 -06002146 r = _reset(oh);
2147
2148 return r;
2149}
2150
2151/**
2152 * _setup_postsetup - transition to the appropriate state after _setup
2153 * @oh: struct omap_hwmod *
2154 *
2155 * Place an IP block represented by @oh into a "post-setup" state --
2156 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2157 * this function is called at the end of _setup().) The postsetup
2158 * state for an IP block can be changed by calling
2159 * omap_hwmod_enter_postsetup_state() early in the boot process,
2160 * before one of the omap_hwmod_setup*() functions are called for the
2161 * IP block.
2162 *
2163 * The IP block stays in this state until a PM runtime-based driver is
2164 * loaded for that IP block. A post-setup state of IDLE is
2165 * appropriate for almost all IP blocks with runtime PM-enabled
2166 * drivers, since those drivers are able to enable the IP block. A
2167 * post-setup state of ENABLED is appropriate for kernels with PM
2168 * runtime disabled. The DISABLED state is appropriate for unusual IP
2169 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2170 * included, since the WDTIMER starts running on reset and will reset
2171 * the MPU if left active.
2172 *
2173 * This post-setup mechanism is deprecated. Once all of the OMAP
2174 * drivers have been converted to use PM runtime, and all of the IP
2175 * block data and interconnect data is available to the hwmod code, it
2176 * should be possible to replace this mechanism with a "lazy reset"
2177 * arrangement. In a "lazy reset" setup, each IP block is enabled
2178 * when the driver first probes, then all remaining IP blocks without
2179 * drivers are either shut down or enabled after the drivers have
2180 * loaded. However, this cannot take place until the above
2181 * preconditions have been met, since otherwise the late reset code
2182 * has no way of knowing which IP blocks are in use by drivers, and
2183 * which ones are unused.
2184 *
2185 * No return value.
2186 */
2187static void __init _setup_postsetup(struct omap_hwmod *oh)
2188{
2189 u8 postsetup_state;
2190
2191 if (oh->rst_lines_cnt > 0)
2192 return;
Benoit Cousson76e55892010-09-21 10:34:11 -06002193
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002194 postsetup_state = oh->_postsetup_state;
2195 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2196 postsetup_state = _HWMOD_STATE_ENABLED;
2197
2198 /*
2199 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2200 * it should be set by the core code as a runtime flag during startup
2201 */
2202 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002203 (postsetup_state == _HWMOD_STATE_IDLE)) {
2204 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002205 postsetup_state = _HWMOD_STATE_ENABLED;
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002206 }
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002207
2208 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002209 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002210 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2211 _shutdown(oh);
2212 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2213 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2214 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03002215
Paul Walmsley64813c32012-04-18 19:10:03 -06002216 return;
2217}
2218
2219/**
2220 * _setup - prepare IP block hardware for use
2221 * @oh: struct omap_hwmod *
2222 * @n: (unused, pass NULL)
2223 *
2224 * Configure the IP block represented by @oh. This may include
2225 * enabling the IP block, resetting it, and placing it into a
2226 * post-setup state, depending on the type of IP block and applicable
2227 * flags. IP blocks are reset to prevent any previous configuration
2228 * by the bootloader or previous operating system from interfering
2229 * with power management or other parts of the system. The reset can
2230 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2231 * two phases for hwmod initialization. Code called here generally
2232 * affects the IP block hardware, or system integration hardware
2233 * associated with the IP block. Returns 0.
2234 */
2235static int __init _setup(struct omap_hwmod *oh, void *data)
2236{
2237 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2238 return 0;
2239
2240 _setup_iclk_autoidle(oh);
2241
2242 if (!_setup_reset(oh))
2243 _setup_postsetup(oh);
2244
Paul Walmsley63c85232009-09-03 20:14:03 +03002245 return 0;
2246}
2247
Benoit Cousson0102b622010-12-21 21:31:27 -07002248/**
2249 * _register - register a struct omap_hwmod
2250 * @oh: struct omap_hwmod *
2251 *
2252 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2253 * already has been registered by the same name; -EINVAL if the
2254 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2255 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2256 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2257 * success.
2258 *
2259 * XXX The data should be copied into bootmem, so the original data
2260 * should be marked __initdata and freed after init. This would allow
2261 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2262 * that the copy process would be relatively complex due to the large number
2263 * of substructures.
2264 */
Benoit Cousson01592df92010-12-21 21:31:28 -07002265static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07002266{
Benoit Cousson0102b622010-12-21 21:31:27 -07002267 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2268 (oh->_state != _HWMOD_STATE_UNKNOWN))
2269 return -EINVAL;
2270
Benoit Cousson0102b622010-12-21 21:31:27 -07002271 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2272
Benoit Coussonce35b242010-12-21 21:31:28 -07002273 if (_lookup(oh->name))
2274 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07002275
Benoit Cousson0102b622010-12-21 21:31:27 -07002276 list_add_tail(&oh->node, &omap_hwmod_list);
2277
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002278 INIT_LIST_HEAD(&oh->master_ports);
2279 INIT_LIST_HEAD(&oh->slave_ports);
Benoit Cousson0102b622010-12-21 21:31:27 -07002280 spin_lock_init(&oh->_lock);
2281
2282 oh->_state = _HWMOD_STATE_REGISTERED;
2283
Paul Walmsley569edd702011-02-23 00:14:06 -07002284 /*
2285 * XXX Rather than doing a strcmp(), this should test a flag
2286 * set in the hwmod data, inserted by the autogenerator code.
2287 */
2288 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2289 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07002290
Paul Walmsley569edd702011-02-23 00:14:06 -07002291 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07002292}
Paul Walmsley63c85232009-09-03 20:14:03 +03002293
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002294/**
2295 * _alloc_links - return allocated memory for hwmod links
2296 * @ml: pointer to a struct omap_hwmod_link * for the master link
2297 * @sl: pointer to a struct omap_hwmod_link * for the slave link
2298 *
2299 * Return pointers to two struct omap_hwmod_link records, via the
2300 * addresses pointed to by @ml and @sl. Will first attempt to return
2301 * memory allocated as part of a large initial block, but if that has
2302 * been exhausted, will allocate memory itself. Since ideally this
2303 * second allocation path will never occur, the number of these
2304 * 'supplemental' allocations will be logged when debugging is
2305 * enabled. Returns 0.
2306 */
2307static int __init _alloc_links(struct omap_hwmod_link **ml,
2308 struct omap_hwmod_link **sl)
2309{
2310 unsigned int sz;
2311
2312 if ((free_ls + LINKS_PER_OCP_IF) <= max_ls) {
2313 *ml = &linkspace[free_ls++];
2314 *sl = &linkspace[free_ls++];
2315 return 0;
2316 }
2317
2318 sz = sizeof(struct omap_hwmod_link) * LINKS_PER_OCP_IF;
2319
2320 *sl = NULL;
2321 *ml = alloc_bootmem(sz);
2322
2323 memset(*ml, 0, sz);
2324
2325 *sl = (void *)(*ml) + sizeof(struct omap_hwmod_link);
2326
2327 ls_supp++;
2328 pr_debug("omap_hwmod: supplemental link allocations needed: %d\n",
2329 ls_supp * LINKS_PER_OCP_IF);
2330
2331 return 0;
2332};
2333
2334/**
2335 * _add_link - add an interconnect between two IP blocks
2336 * @oi: pointer to a struct omap_hwmod_ocp_if record
2337 *
2338 * Add struct omap_hwmod_link records connecting the master IP block
2339 * specified in @oi->master to @oi, and connecting the slave IP block
2340 * specified in @oi->slave to @oi. This code is assumed to run before
2341 * preemption or SMP has been enabled, thus avoiding the need for
2342 * locking in this code. Changes to this assumption will require
2343 * additional locking. Returns 0.
2344 */
2345static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2346{
2347 struct omap_hwmod_link *ml, *sl;
2348
2349 pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2350 oi->slave->name);
2351
2352 _alloc_links(&ml, &sl);
2353
2354 ml->ocp_if = oi;
2355 INIT_LIST_HEAD(&ml->node);
2356 list_add(&ml->node, &oi->master->master_ports);
2357 oi->master->masters_cnt++;
2358
2359 sl->ocp_if = oi;
2360 INIT_LIST_HEAD(&sl->node);
2361 list_add(&sl->node, &oi->slave->slave_ports);
2362 oi->slave->slaves_cnt++;
2363
2364 return 0;
2365}
2366
2367/**
2368 * _register_link - register a struct omap_hwmod_ocp_if
2369 * @oi: struct omap_hwmod_ocp_if *
2370 *
2371 * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
2372 * has already been registered; -EINVAL if @oi is NULL or if the
2373 * record pointed to by @oi is missing required fields; or 0 upon
2374 * success.
2375 *
2376 * XXX The data should be copied into bootmem, so the original data
2377 * should be marked __initdata and freed after init. This would allow
2378 * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2379 */
2380static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2381{
2382 if (!oi || !oi->master || !oi->slave || !oi->user)
2383 return -EINVAL;
2384
2385 if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2386 return -EEXIST;
2387
2388 pr_debug("omap_hwmod: registering link from %s to %s\n",
2389 oi->master->name, oi->slave->name);
2390
2391 /*
2392 * Register the connected hwmods, if they haven't been
2393 * registered already
2394 */
2395 if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2396 _register(oi->master);
2397
2398 if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2399 _register(oi->slave);
2400
2401 _add_link(oi);
2402
2403 oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2404
2405 return 0;
2406}
2407
2408/**
2409 * _alloc_linkspace - allocate large block of hwmod links
2410 * @ois: pointer to an array of struct omap_hwmod_ocp_if records to count
2411 *
2412 * Allocate a large block of struct omap_hwmod_link records. This
2413 * improves boot time significantly by avoiding the need to allocate
2414 * individual records one by one. If the number of records to
2415 * allocate in the block hasn't been manually specified, this function
2416 * will count the number of struct omap_hwmod_ocp_if records in @ois
2417 * and use that to determine the allocation size. For SoC families
2418 * that require multiple list registrations, such as OMAP3xxx, this
2419 * estimation process isn't optimal, so manual estimation is advised
2420 * in those cases. Returns -EEXIST if the allocation has already occurred
2421 * or 0 upon success.
2422 */
2423static int __init _alloc_linkspace(struct omap_hwmod_ocp_if **ois)
2424{
2425 unsigned int i = 0;
2426 unsigned int sz;
2427
2428 if (linkspace) {
2429 WARN(1, "linkspace already allocated\n");
2430 return -EEXIST;
2431 }
2432
2433 if (max_ls == 0)
2434 while (ois[i++])
2435 max_ls += LINKS_PER_OCP_IF;
2436
2437 sz = sizeof(struct omap_hwmod_link) * max_ls;
2438
2439 pr_debug("omap_hwmod: %s: allocating %d byte linkspace (%d links)\n",
2440 __func__, sz, max_ls);
2441
2442 linkspace = alloc_bootmem(sz);
2443
2444 memset(linkspace, 0, sz);
2445
2446 return 0;
2447}
Paul Walmsley63c85232009-09-03 20:14:03 +03002448
2449/* Public functions */
2450
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002451u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002452{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002453 if (oh->flags & HWMOD_16BIT_REG)
2454 return __raw_readw(oh->_mpu_rt_va + reg_offs);
2455 else
2456 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002457}
2458
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002459void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002460{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002461 if (oh->flags & HWMOD_16BIT_REG)
2462 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
2463 else
2464 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002465}
2466
Paul Walmsley887adea2010-07-26 16:34:33 -06002467/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002468 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
2469 * @oh: struct omap_hwmod *
2470 *
2471 * This is a public function exposed to drivers. Some drivers may need to do
2472 * some settings before and after resetting the device. Those drivers after
2473 * doing the necessary settings could use this function to start a reset by
2474 * setting the SYSCONFIG.SOFTRESET bit.
2475 */
2476int omap_hwmod_softreset(struct omap_hwmod *oh)
2477{
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002478 u32 v;
2479 int ret;
2480
2481 if (!oh || !(oh->_sysc_cache))
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002482 return -EINVAL;
2483
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002484 v = oh->_sysc_cache;
2485 ret = _set_softreset(oh, &v);
2486 if (ret)
2487 goto error;
2488 _write_sysconfig(v, oh);
2489
2490error:
2491 return ret;
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002492}
2493
2494/**
Paul Walmsley887adea2010-07-26 16:34:33 -06002495 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
2496 * @oh: struct omap_hwmod *
2497 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
2498 *
2499 * Sets the IP block's OCP slave idlemode in hardware, and updates our
2500 * local copy. Intended to be used by drivers that have some erratum
2501 * that requires direct manipulation of the SIDLEMODE bits. Returns
2502 * -EINVAL if @oh is null, or passes along the return value from
2503 * _set_slave_idlemode().
2504 *
2505 * XXX Does this function have any current users? If not, we should
2506 * remove it; it is better to let the rest of the hwmod code handle this.
2507 * Any users of this function should be scrutinized carefully.
2508 */
Kevin Hilman46273e62010-01-26 20:13:03 -07002509int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
2510{
2511 u32 v;
2512 int retval = 0;
2513
2514 if (!oh)
2515 return -EINVAL;
2516
2517 v = oh->_sysc_cache;
2518
2519 retval = _set_slave_idlemode(oh, idlemode, &v);
2520 if (!retval)
2521 _write_sysconfig(v, oh);
2522
2523 return retval;
2524}
2525
Paul Walmsley63c85232009-09-03 20:14:03 +03002526/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002527 * omap_hwmod_lookup - look up a registered omap_hwmod by name
2528 * @name: name of the omap_hwmod to look up
2529 *
2530 * Given a @name of an omap_hwmod, return a pointer to the registered
2531 * struct omap_hwmod *, or NULL upon error.
2532 */
2533struct omap_hwmod *omap_hwmod_lookup(const char *name)
2534{
2535 struct omap_hwmod *oh;
2536
2537 if (!name)
2538 return NULL;
2539
Paul Walmsley63c85232009-09-03 20:14:03 +03002540 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002541
2542 return oh;
2543}
2544
2545/**
2546 * omap_hwmod_for_each - call function for each registered omap_hwmod
2547 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06002548 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03002549 *
2550 * Call @fn for each registered omap_hwmod, passing @data to each
2551 * function. @fn must return 0 for success or any other value for
2552 * failure. If @fn returns non-zero, the iteration across omap_hwmods
2553 * will stop and the non-zero return value will be passed to the
2554 * caller of omap_hwmod_for_each(). @fn is called with
2555 * omap_hwmod_for_each() held.
2556 */
Paul Walmsley97d60162010-07-26 16:34:30 -06002557int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
2558 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03002559{
2560 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05302561 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002562
2563 if (!fn)
2564 return -EINVAL;
2565
Paul Walmsley63c85232009-09-03 20:14:03 +03002566 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06002567 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03002568 if (ret)
2569 break;
2570 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002571
2572 return ret;
2573}
2574
Paul Walmsley63c85232009-09-03 20:14:03 +03002575/**
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002576 * omap_hwmod_register_links - register an array of hwmod links
2577 * @ois: pointer to an array of omap_hwmod_ocp_if to register
2578 *
2579 * Intended to be called early in boot before the clock framework is
2580 * initialized. If @ois is not null, will register all omap_hwmods
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002581 * listed in @ois that are valid for this chip. Returns -EINVAL if
2582 * omap_hwmod_init() hasn't been called before calling this function,
2583 * -ENOMEM if the link memory area can't be allocated, or 0 upon
2584 * success.
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002585 */
2586int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
2587{
2588 int r, i;
2589
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002590 if (!inited)
2591 return -EINVAL;
2592
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002593 if (!ois)
2594 return 0;
2595
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002596 if (!linkspace) {
2597 if (_alloc_linkspace(ois)) {
2598 pr_err("omap_hwmod: could not allocate link space\n");
2599 return -ENOMEM;
2600 }
2601 }
2602
2603 i = 0;
2604 do {
2605 r = _register_link(ois[i]);
2606 WARN(r && r != -EEXIST,
2607 "omap_hwmod: _register_link(%s -> %s) returned %d\n",
2608 ois[i]->master->name, ois[i]->slave->name, r);
2609 } while (ois[++i]);
2610
2611 return 0;
2612}
2613
2614/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002615 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
2616 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002617 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002618 * If the hwmod data corresponding to the MPU subsystem IP block
2619 * hasn't been initialized and set up yet, do so now. This must be
2620 * done first since sleep dependencies may be added from other hwmods
2621 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
2622 * return value.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002623 */
Paul Walmsley381d0332012-04-19 00:58:22 -06002624static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002625{
Paul Walmsley381d0332012-04-19 00:58:22 -06002626 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
2627 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
2628 __func__, MPU_INITIATOR_NAME);
2629 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
2630 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
Paul Walmsley63c85232009-09-03 20:14:03 +03002631}
2632
2633/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002634 * omap_hwmod_setup_one - set up a single hwmod
2635 * @oh_name: const char * name of the already-registered hwmod to set up
2636 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002637 * Initialize and set up a single hwmod. Intended to be used for a
2638 * small number of early devices, such as the timer IP blocks used for
2639 * the scheduler clock. Must be called after omap2_clk_init().
2640 * Resolves the struct clk names to struct clk pointers for each
2641 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
2642 * -EINVAL upon error or 0 upon success.
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002643 */
2644int __init omap_hwmod_setup_one(const char *oh_name)
2645{
2646 struct omap_hwmod *oh;
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002647
2648 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
2649
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002650 oh = _lookup(oh_name);
2651 if (!oh) {
2652 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
2653 return -EINVAL;
2654 }
2655
Paul Walmsley381d0332012-04-19 00:58:22 -06002656 _ensure_mpu_hwmod_is_setup(oh);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002657
Paul Walmsley381d0332012-04-19 00:58:22 -06002658 _init(oh, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002659 _setup(oh, NULL);
2660
2661 return 0;
2662}
2663
2664/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002665 * omap_hwmod_setup_all - set up all registered IP blocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002666 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002667 * Initialize and set up all IP blocks registered with the hwmod code.
2668 * Must be called after omap2_clk_init(). Resolves the struct clk
2669 * names to struct clk pointers for each registered omap_hwmod. Also
2670 * calls _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002671 */
Paul Walmsley550c8092011-02-28 11:58:14 -07002672static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03002673{
Paul Walmsley381d0332012-04-19 00:58:22 -06002674 _ensure_mpu_hwmod_is_setup(NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002675
Paul Walmsley381d0332012-04-19 00:58:22 -06002676 omap_hwmod_for_each(_init, NULL);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002677 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002678
2679 return 0;
2680}
Paul Walmsley550c8092011-02-28 11:58:14 -07002681core_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03002682
2683/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002684 * omap_hwmod_enable - enable an omap_hwmod
2685 * @oh: struct omap_hwmod *
2686 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002687 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03002688 * Returns -EINVAL on error or passes along the return value from _enable().
2689 */
2690int omap_hwmod_enable(struct omap_hwmod *oh)
2691{
2692 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002693 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002694
2695 if (!oh)
2696 return -EINVAL;
2697
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002698 spin_lock_irqsave(&oh->_lock, flags);
2699 r = _enable(oh);
2700 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002701
2702 return r;
2703}
2704
2705/**
2706 * omap_hwmod_idle - idle an omap_hwmod
2707 * @oh: struct omap_hwmod *
2708 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002709 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03002710 * Returns -EINVAL on error or passes along the return value from _idle().
2711 */
2712int omap_hwmod_idle(struct omap_hwmod *oh)
2713{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002714 unsigned long flags;
2715
Paul Walmsley63c85232009-09-03 20:14:03 +03002716 if (!oh)
2717 return -EINVAL;
2718
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002719 spin_lock_irqsave(&oh->_lock, flags);
2720 _idle(oh);
2721 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002722
2723 return 0;
2724}
2725
2726/**
2727 * omap_hwmod_shutdown - shutdown an omap_hwmod
2728 * @oh: struct omap_hwmod *
2729 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002730 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03002731 * omap_device_shutdown(). Returns -EINVAL on error or passes along
2732 * the return value from _shutdown().
2733 */
2734int omap_hwmod_shutdown(struct omap_hwmod *oh)
2735{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002736 unsigned long flags;
2737
Paul Walmsley63c85232009-09-03 20:14:03 +03002738 if (!oh)
2739 return -EINVAL;
2740
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002741 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002742 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002743 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002744
2745 return 0;
2746}
2747
2748/**
2749 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
2750 * @oh: struct omap_hwmod *oh
2751 *
2752 * Intended to be called by the omap_device code.
2753 */
2754int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
2755{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002756 unsigned long flags;
2757
2758 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002759 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002760 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002761
2762 return 0;
2763}
2764
2765/**
2766 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
2767 * @oh: struct omap_hwmod *oh
2768 *
2769 * Intended to be called by the omap_device code.
2770 */
2771int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
2772{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002773 unsigned long flags;
2774
2775 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002776 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002777 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002778
2779 return 0;
2780}
2781
2782/**
2783 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
2784 * @oh: struct omap_hwmod *oh
2785 *
2786 * Intended to be called by drivers and core code when all posted
2787 * writes to a device must complete before continuing further
2788 * execution (for example, after clearing some device IRQSTATUS
2789 * register bits)
2790 *
2791 * XXX what about targets with multiple OCP threads?
2792 */
2793void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
2794{
2795 BUG_ON(!oh);
2796
Paul Walmsley43b40992010-02-22 22:09:34 -07002797 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Russell King4f8a4282012-02-07 10:59:37 +00002798 WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
2799 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002800 return;
2801 }
2802
2803 /*
2804 * Forces posted writes to complete on the OCP thread handling
2805 * register writes
2806 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002807 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002808}
2809
2810/**
2811 * omap_hwmod_reset - reset the hwmod
2812 * @oh: struct omap_hwmod *
2813 *
2814 * Under some conditions, a driver may wish to reset the entire device.
2815 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06002816 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03002817 */
2818int omap_hwmod_reset(struct omap_hwmod *oh)
2819{
2820 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002821 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002822
Liam Girdwood9b579112010-09-21 10:34:09 -06002823 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002824 return -EINVAL;
2825
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002826 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002827 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002828 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002829
2830 return r;
2831}
2832
Paul Walmsley5e8370f2012-04-18 19:10:06 -06002833/*
2834 * IP block data retrieval functions
2835 */
2836
Paul Walmsley63c85232009-09-03 20:14:03 +03002837/**
2838 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
2839 * @oh: struct omap_hwmod *
2840 * @res: pointer to the first element of an array of struct resource to fill
2841 *
2842 * Count the number of struct resource array elements necessary to
2843 * contain omap_hwmod @oh resources. Intended to be called by code
2844 * that registers omap_devices. Intended to be used to determine the
2845 * size of a dynamically-allocated struct resource array, before
2846 * calling omap_hwmod_fill_resources(). Returns the number of struct
2847 * resource array elements needed.
2848 *
2849 * XXX This code is not optimized. It could attempt to merge adjacent
2850 * resource IDs.
2851 *
2852 */
2853int omap_hwmod_count_resources(struct omap_hwmod *oh)
2854{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002855 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002856 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002857 int ret;
2858 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002859
Paul Walmsleybc614952011-07-09 19:14:07 -06002860 ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002861
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002862 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002863
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002864 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002865 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002866 ret += _count_ocp_if_addr_spaces(os);
2867 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002868
2869 return ret;
2870}
2871
2872/**
2873 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
2874 * @oh: struct omap_hwmod *
2875 * @res: pointer to the first element of an array of struct resource to fill
2876 *
2877 * Fill the struct resource array @res with resource data from the
2878 * omap_hwmod @oh. Intended to be called by code that registers
2879 * omap_devices. See also omap_hwmod_count_resources(). Returns the
2880 * number of array elements filled.
2881 */
2882int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
2883{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002884 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002885 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002886 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03002887 int r = 0;
2888
2889 /* For each IRQ, DMA, memory area, fill in array.*/
2890
Paul Walmsley212738a2011-07-09 19:14:06 -06002891 mpu_irqs_cnt = _count_mpu_irqs(oh);
2892 for (i = 0; i < mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07002893 (res + r)->name = (oh->mpu_irqs + i)->name;
2894 (res + r)->start = (oh->mpu_irqs + i)->irq;
2895 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03002896 (res + r)->flags = IORESOURCE_IRQ;
2897 r++;
2898 }
2899
Paul Walmsleybc614952011-07-09 19:14:07 -06002900 sdma_reqs_cnt = _count_sdma_reqs(oh);
2901 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06002902 (res + r)->name = (oh->sdma_reqs + i)->name;
2903 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
2904 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03002905 (res + r)->flags = IORESOURCE_DMA;
2906 r++;
2907 }
2908
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002909 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002910
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002911 i = 0;
2912 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002913 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley78183f32011-07-09 19:14:05 -06002914 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03002915
Paul Walmsley78183f32011-07-09 19:14:05 -06002916 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08002917 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03002918 (res + r)->start = (os->addr + j)->pa_start;
2919 (res + r)->end = (os->addr + j)->pa_end;
2920 (res + r)->flags = IORESOURCE_MEM;
2921 r++;
2922 }
2923 }
2924
2925 return r;
2926}
2927
2928/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06002929 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
2930 * @oh: struct omap_hwmod * to operate on
2931 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
2932 * @name: pointer to the name of the data to fetch (optional)
2933 * @rsrc: pointer to a struct resource, allocated by the caller
2934 *
2935 * Retrieve MPU IRQ, SDMA request line, or address space start/end
2936 * data for the IP block pointed to by @oh. The data will be filled
2937 * into a struct resource record pointed to by @rsrc. The struct
2938 * resource must be allocated by the caller. When @name is non-null,
2939 * the data associated with the matching entry in the IRQ/SDMA/address
2940 * space hwmod data arrays will be returned. If @name is null, the
2941 * first array entry will be returned. Data order is not meaningful
2942 * in hwmod data, so callers are strongly encouraged to use a non-null
2943 * @name whenever possible to avoid unpredictable effects if hwmod
2944 * data is later added that causes data ordering to change. This
2945 * function is only intended for use by OMAP core code. Device
2946 * drivers should not call this function - the appropriate bus-related
2947 * data accessor functions should be used instead. Returns 0 upon
2948 * success or a negative error code upon error.
2949 */
2950int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
2951 const char *name, struct resource *rsrc)
2952{
2953 int r;
2954 unsigned int irq, dma;
2955 u32 pa_start, pa_end;
2956
2957 if (!oh || !rsrc)
2958 return -EINVAL;
2959
2960 if (type == IORESOURCE_IRQ) {
2961 r = _get_mpu_irq_by_name(oh, name, &irq);
2962 if (r)
2963 return r;
2964
2965 rsrc->start = irq;
2966 rsrc->end = irq;
2967 } else if (type == IORESOURCE_DMA) {
2968 r = _get_sdma_req_by_name(oh, name, &dma);
2969 if (r)
2970 return r;
2971
2972 rsrc->start = dma;
2973 rsrc->end = dma;
2974 } else if (type == IORESOURCE_MEM) {
2975 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
2976 if (r)
2977 return r;
2978
2979 rsrc->start = pa_start;
2980 rsrc->end = pa_end;
2981 } else {
2982 return -EINVAL;
2983 }
2984
2985 rsrc->flags = type;
2986 rsrc->name = name;
2987
2988 return 0;
2989}
2990
2991/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002992 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
2993 * @oh: struct omap_hwmod *
2994 *
2995 * Return the powerdomain pointer associated with the OMAP module
2996 * @oh's main clock. If @oh does not have a main clk, return the
2997 * powerdomain associated with the interface clock associated with the
2998 * module's MPU port. (XXX Perhaps this should use the SDMA port
2999 * instead?) Returns NULL on error, or a struct powerdomain * on
3000 * success.
3001 */
3002struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
3003{
3004 struct clk *c;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003005 struct omap_hwmod_ocp_if *oi;
Paul Walmsley63c85232009-09-03 20:14:03 +03003006
3007 if (!oh)
3008 return NULL;
3009
3010 if (oh->_clk) {
3011 c = oh->_clk;
3012 } else {
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003013 oi = _find_mpu_rt_port(oh);
3014 if (!oi)
Paul Walmsley63c85232009-09-03 20:14:03 +03003015 return NULL;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003016 c = oi->_clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03003017 }
3018
Thara Gopinathd5647c12010-03-31 04:16:29 -06003019 if (!c->clkdm)
3020 return NULL;
3021
Paul Walmsley63c85232009-09-03 20:14:03 +03003022 return c->clkdm->pwrdm.ptr;
3023
3024}
3025
3026/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06003027 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3028 * @oh: struct omap_hwmod *
3029 *
3030 * Returns the virtual address corresponding to the beginning of the
3031 * module's register target, in the address range that is intended to
3032 * be used by the MPU. Returns the virtual address upon success or NULL
3033 * upon error.
3034 */
3035void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3036{
3037 if (!oh)
3038 return NULL;
3039
3040 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3041 return NULL;
3042
3043 if (oh->_state == _HWMOD_STATE_UNKNOWN)
3044 return NULL;
3045
3046 return oh->_mpu_rt_va;
3047}
3048
3049/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003050 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
3051 * @oh: struct omap_hwmod *
3052 * @init_oh: struct omap_hwmod * (initiator)
3053 *
3054 * Add a sleep dependency between the initiator @init_oh and @oh.
3055 * Intended to be called by DSP/Bridge code via platform_data for the
3056 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3057 * code needs to add/del initiator dependencies dynamically
3058 * before/after accessing a device. Returns the return value from
3059 * _add_initiator_dep().
3060 *
3061 * XXX Keep a usecount in the clockdomain code
3062 */
3063int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
3064 struct omap_hwmod *init_oh)
3065{
3066 return _add_initiator_dep(oh, init_oh);
3067}
3068
3069/*
3070 * XXX what about functions for drivers to save/restore ocp_sysconfig
3071 * for context save/restore operations?
3072 */
3073
3074/**
3075 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
3076 * @oh: struct omap_hwmod *
3077 * @init_oh: struct omap_hwmod * (initiator)
3078 *
3079 * Remove a sleep dependency between the initiator @init_oh and @oh.
3080 * Intended to be called by DSP/Bridge code via platform_data for the
3081 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3082 * code needs to add/del initiator dependencies dynamically
3083 * before/after accessing a device. Returns the return value from
3084 * _del_initiator_dep().
3085 *
3086 * XXX Keep a usecount in the clockdomain code
3087 */
3088int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
3089 struct omap_hwmod *init_oh)
3090{
3091 return _del_initiator_dep(oh, init_oh);
3092}
3093
3094/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003095 * omap_hwmod_enable_wakeup - allow device to wake up the system
3096 * @oh: struct omap_hwmod *
3097 *
3098 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
Govindraj.R2a1cc142012-04-05 02:59:32 -06003099 * send wakeups to the PRCM, and enable I/O ring wakeup events for
3100 * this IP block if it has dynamic mux entries. Eventually this
3101 * should set PRCM wakeup registers to cause the PRCM to receive
3102 * wakeup events from the module. Does not set any wakeup routing
3103 * registers beyond this point - if the module is to wake up any other
3104 * module or subsystem, that must be set separately. Called by
3105 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003106 */
3107int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3108{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003109 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003110 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003111
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003112 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003113
3114 if (oh->class->sysc &&
3115 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3116 v = oh->_sysc_cache;
3117 _enable_wakeup(oh, &v);
3118 _write_sysconfig(v, oh);
3119 }
3120
Govindraj Receec002011-12-16 14:36:58 -07003121 _set_idle_ioring_wakeup(oh, true);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003122 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003123
3124 return 0;
3125}
3126
3127/**
3128 * omap_hwmod_disable_wakeup - prevent device from waking the system
3129 * @oh: struct omap_hwmod *
3130 *
3131 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
Govindraj.R2a1cc142012-04-05 02:59:32 -06003132 * from sending wakeups to the PRCM, and disable I/O ring wakeup
3133 * events for this IP block if it has dynamic mux entries. Eventually
3134 * this should clear PRCM wakeup registers to cause the PRCM to ignore
3135 * wakeup events from the module. Does not set any wakeup routing
3136 * registers beyond this point - if the module is to wake up any other
3137 * module or subsystem, that must be set separately. Called by
3138 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003139 */
3140int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3141{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003142 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003143 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003144
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003145 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003146
3147 if (oh->class->sysc &&
3148 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3149 v = oh->_sysc_cache;
3150 _disable_wakeup(oh, &v);
3151 _write_sysconfig(v, oh);
3152 }
3153
Govindraj Receec002011-12-16 14:36:58 -07003154 _set_idle_ioring_wakeup(oh, false);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003155 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003156
3157 return 0;
3158}
Paul Walmsley43b40992010-02-22 22:09:34 -07003159
3160/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003161 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3162 * contained in the hwmod module.
3163 * @oh: struct omap_hwmod *
3164 * @name: name of the reset line to lookup and assert
3165 *
3166 * Some IP like dsp, ipu or iva contain processor that require
3167 * an HW reset line to be assert / deassert in order to enable fully
3168 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3169 * yet supported on this OMAP; otherwise, passes along the return value
3170 * from _assert_hardreset().
3171 */
3172int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3173{
3174 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003175 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003176
3177 if (!oh)
3178 return -EINVAL;
3179
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003180 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003181 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003182 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003183
3184 return ret;
3185}
3186
3187/**
3188 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3189 * contained in the hwmod module.
3190 * @oh: struct omap_hwmod *
3191 * @name: name of the reset line to look up and deassert
3192 *
3193 * Some IP like dsp, ipu or iva contain processor that require
3194 * an HW reset line to be assert / deassert in order to enable fully
3195 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3196 * yet supported on this OMAP; otherwise, passes along the return value
3197 * from _deassert_hardreset().
3198 */
3199int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3200{
3201 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003202 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003203
3204 if (!oh)
3205 return -EINVAL;
3206
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003207 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003208 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003209 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003210
3211 return ret;
3212}
3213
3214/**
3215 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
3216 * contained in the hwmod module
3217 * @oh: struct omap_hwmod *
3218 * @name: name of the reset line to look up and read
3219 *
3220 * Return the current state of the hwmod @oh's reset line named @name:
3221 * returns -EINVAL upon parameter error or if this operation
3222 * is unsupported on the current OMAP; otherwise, passes along the return
3223 * value from _read_hardreset().
3224 */
3225int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
3226{
3227 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003228 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003229
3230 if (!oh)
3231 return -EINVAL;
3232
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003233 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003234 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003235 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003236
3237 return ret;
3238}
3239
3240
3241/**
Paul Walmsley43b40992010-02-22 22:09:34 -07003242 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3243 * @classname: struct omap_hwmod_class name to search for
3244 * @fn: callback function pointer to call for each hwmod in class @classname
3245 * @user: arbitrary context data to pass to the callback function
3246 *
Benoit Coussonce35b242010-12-21 21:31:28 -07003247 * For each omap_hwmod of class @classname, call @fn.
3248 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07003249 * zero, the iterator is terminated, and the callback function's return
3250 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3251 * if @classname or @fn are NULL, or passes back the error code from @fn.
3252 */
3253int omap_hwmod_for_each_by_class(const char *classname,
3254 int (*fn)(struct omap_hwmod *oh,
3255 void *user),
3256 void *user)
3257{
3258 struct omap_hwmod *temp_oh;
3259 int ret = 0;
3260
3261 if (!classname || !fn)
3262 return -EINVAL;
3263
3264 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3265 __func__, classname);
3266
Paul Walmsley43b40992010-02-22 22:09:34 -07003267 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3268 if (!strcmp(temp_oh->class->name, classname)) {
3269 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3270 __func__, temp_oh->name);
3271 ret = (*fn)(temp_oh, user);
3272 if (ret)
3273 break;
3274 }
3275 }
3276
Paul Walmsley43b40992010-02-22 22:09:34 -07003277 if (ret)
3278 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3279 __func__, ret);
3280
3281 return ret;
3282}
3283
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003284/**
3285 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3286 * @oh: struct omap_hwmod *
3287 * @state: state that _setup() should leave the hwmod in
3288 *
Paul Walmsley550c8092011-02-28 11:58:14 -07003289 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsley64813c32012-04-18 19:10:03 -06003290 * (called by omap_hwmod_setup_*()). See also the documentation
3291 * for _setup_postsetup(), above. Returns 0 upon success or
3292 * -EINVAL if there is a problem with the arguments or if the hwmod is
3293 * in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003294 */
3295int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3296{
3297 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003298 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003299
3300 if (!oh)
3301 return -EINVAL;
3302
3303 if (state != _HWMOD_STATE_DISABLED &&
3304 state != _HWMOD_STATE_ENABLED &&
3305 state != _HWMOD_STATE_IDLE)
3306 return -EINVAL;
3307
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003308 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003309
3310 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3311 ret = -EINVAL;
3312 goto ohsps_unlock;
3313 }
3314
3315 oh->_postsetup_state = state;
3316 ret = 0;
3317
3318ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003319 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003320
3321 return ret;
3322}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003323
3324/**
3325 * omap_hwmod_get_context_loss_count - get lost context count
3326 * @oh: struct omap_hwmod *
3327 *
3328 * Query the powerdomain of of @oh to get the context loss
3329 * count for this device.
3330 *
3331 * Returns the context loss count of the powerdomain assocated with @oh
3332 * upon success, or zero if no powerdomain exists for @oh.
3333 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03003334int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003335{
3336 struct powerdomain *pwrdm;
3337 int ret = 0;
3338
3339 pwrdm = omap_hwmod_get_pwrdm(oh);
3340 if (pwrdm)
3341 ret = pwrdm_get_context_loss_count(pwrdm);
3342
3343 return ret;
3344}
Paul Walmsley43b01642011-03-10 03:50:07 -07003345
3346/**
3347 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
3348 * @oh: struct omap_hwmod *
3349 *
3350 * Prevent the hwmod @oh from being reset during the setup process.
3351 * Intended for use by board-*.c files on boards with devices that
3352 * cannot tolerate being reset. Must be called before the hwmod has
3353 * been set up. Returns 0 upon success or negative error code upon
3354 * failure.
3355 */
3356int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
3357{
3358 if (!oh)
3359 return -EINVAL;
3360
3361 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3362 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
3363 oh->name);
3364 return -EINVAL;
3365 }
3366
3367 oh->flags |= HWMOD_INIT_NO_RESET;
3368
3369 return 0;
3370}
Tero Kristoabc2d542011-12-16 14:36:59 -07003371
3372/**
3373 * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
3374 * @oh: struct omap_hwmod * containing hwmod mux entries
3375 * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
3376 * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
3377 *
3378 * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
3379 * entry number @pad_idx for the hwmod @oh, trigger the interrupt
3380 * service routine for the hwmod's mpu_irqs array index @irq_idx. If
3381 * this function is not called for a given pad_idx, then the ISR
3382 * associated with @oh's first MPU IRQ will be triggered when an I/O
3383 * pad wakeup occurs on that pad. Note that @pad_idx is the index of
3384 * the _dynamic or wakeup_ entry: if there are other entries not
3385 * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
3386 * entries are NOT COUNTED in the dynamic pad index. This function
3387 * must be called separately for each pad that requires its interrupt
3388 * to be re-routed this way. Returns -EINVAL if there is an argument
3389 * problem or if @oh does not have hwmod mux entries or MPU IRQs;
3390 * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
3391 *
3392 * XXX This function interface is fragile. Rather than using array
3393 * indexes, which are subject to unpredictable change, it should be
3394 * using hwmod IRQ names, and some other stable key for the hwmod mux
3395 * pad records.
3396 */
3397int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
3398{
3399 int nr_irqs;
3400
3401 might_sleep();
3402
3403 if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
3404 pad_idx >= oh->mux->nr_pads_dynamic)
3405 return -EINVAL;
3406
3407 /* Check the number of available mpu_irqs */
3408 for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
3409 ;
3410
3411 if (irq_idx >= nr_irqs)
3412 return -EINVAL;
3413
3414 if (!oh->mux->irqs) {
3415 /* XXX What frees this? */
3416 oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
3417 GFP_KERNEL);
3418 if (!oh->mux->irqs)
3419 return -ENOMEM;
3420 }
3421 oh->mux->irqs[pad_idx] = irq_idx;
3422
3423 return 0;
3424}
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003425
3426/**
3427 * omap_hwmod_init - initialize the hwmod code
3428 *
3429 * Sets up some function pointers needed by the hwmod code to operate on the
3430 * currently-booted SoC. Intended to be called once during kernel init
3431 * before any hwmods are registered. No return value.
3432 */
3433void __init omap_hwmod_init(void)
3434{
3435 if (cpu_is_omap44xx()) {
3436 soc_ops.enable_module = _omap4_enable_module;
3437 soc_ops.disable_module = _omap4_disable_module;
3438 }
3439
3440 inited = true;
3441}