blob: 93cb96dbb5432dffd20d08d7c655c77d4336bfc4 [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);
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -0600181 int (*wait_target_ready)(struct omap_hwmod *oh);
Kevin Hilmanb8249cf2012-06-18 12:12:24 -0600182 int (*assert_hardreset)(struct omap_hwmod *oh,
183 struct omap_hwmod_rst_info *ohri);
184 int (*deassert_hardreset)(struct omap_hwmod *oh,
185 struct omap_hwmod_rst_info *ohri);
186 int (*is_hardreset_asserted)(struct omap_hwmod *oh,
187 struct omap_hwmod_rst_info *ohri);
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600188};
189
190/* soc_ops: adapts the omap_hwmod code to the currently-booted SoC */
191static struct omap_hwmod_soc_ops soc_ops;
192
Paul Walmsley63c85232009-09-03 20:14:03 +0300193/* omap_hwmod_list contains all registered struct omap_hwmods */
194static LIST_HEAD(omap_hwmod_list);
195
Paul Walmsley63c85232009-09-03 20:14:03 +0300196/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
197static struct omap_hwmod *mpu_oh;
198
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600199/*
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600200 * linkspace: ptr to a buffer that struct omap_hwmod_link records are
201 * allocated from - used to reduce the number of small memory
202 * allocations, which has a significant impact on performance
203 */
204static struct omap_hwmod_link *linkspace;
205
206/*
207 * free_ls, max_ls: array indexes into linkspace; representing the
208 * next free struct omap_hwmod_link index, and the maximum number of
209 * struct omap_hwmod_link records allocated (respectively)
210 */
211static unsigned short free_ls, max_ls, ls_supp;
Paul Walmsley63c85232009-09-03 20:14:03 +0300212
Kevin Hilman9ebfd282012-06-18 12:12:23 -0600213/* inited: set to true once the hwmod code is initialized */
214static bool inited;
215
Paul Walmsley63c85232009-09-03 20:14:03 +0300216/* Private functions */
217
218/**
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600219 * _fetch_next_ocp_if - return the next OCP interface in a list
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600220 * @p: ptr to a ptr to the list_head inside the ocp_if to return
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600221 * @i: pointer to the index of the element pointed to by @p in the list
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600222 *
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600223 * Return a pointer to the struct omap_hwmod_ocp_if record
224 * containing the struct list_head pointed to by @p, and increment
225 * @p such that a future call to this routine will return the next
226 * record.
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600227 */
228static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600229 int *i)
230{
231 struct omap_hwmod_ocp_if *oi;
232
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600233 oi = list_entry(*p, struct omap_hwmod_link, node)->ocp_if;
234 *p = (*p)->next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600235
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600236 *i = *i + 1;
237
238 return oi;
239}
240
241/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300242 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
243 * @oh: struct omap_hwmod *
244 *
245 * Load the current value of the hwmod OCP_SYSCONFIG register into the
246 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
247 * OCP_SYSCONFIG register or 0 upon success.
248 */
249static int _update_sysc_cache(struct omap_hwmod *oh)
250{
Paul Walmsley43b40992010-02-22 22:09:34 -0700251 if (!oh->class->sysc) {
252 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 +0300253 return -EINVAL;
254 }
255
256 /* XXX ensure module interface clock is up */
257
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -0700258 oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300259
Paul Walmsley43b40992010-02-22 22:09:34 -0700260 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
Thara Gopinath883edfd2010-01-19 17:30:51 -0700261 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +0300262
263 return 0;
264}
265
266/**
267 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
268 * @v: OCP_SYSCONFIG value to write
269 * @oh: struct omap_hwmod *
270 *
Paul Walmsley43b40992010-02-22 22:09:34 -0700271 * Write @v into the module class' OCP_SYSCONFIG register, if it has
272 * one. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +0300273 */
274static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
275{
Paul Walmsley43b40992010-02-22 22:09:34 -0700276 if (!oh->class->sysc) {
277 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 +0300278 return;
279 }
280
281 /* XXX ensure module interface clock is up */
282
Rajendra Nayak233cbe52010-12-14 12:42:36 -0700283 /* Module might have lost context, always update cache and register */
284 oh->_sysc_cache = v;
285 omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +0300286}
287
288/**
289 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
290 * @oh: struct omap_hwmod *
291 * @standbymode: MIDLEMODE field bits
292 * @v: pointer to register contents to modify
293 *
294 * Update the master standby mode bits in @v to be @standbymode for
295 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
296 * upon error or 0 upon success.
297 */
298static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
299 u32 *v)
300{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700301 u32 mstandby_mask;
302 u8 mstandby_shift;
303
Paul Walmsley43b40992010-02-22 22:09:34 -0700304 if (!oh->class->sysc ||
305 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300306 return -EINVAL;
307
Paul Walmsley43b40992010-02-22 22:09:34 -0700308 if (!oh->class->sysc->sysc_fields) {
309 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700310 return -EINVAL;
311 }
312
Paul Walmsley43b40992010-02-22 22:09:34 -0700313 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700314 mstandby_mask = (0x3 << mstandby_shift);
315
316 *v &= ~mstandby_mask;
317 *v |= __ffs(standbymode) << mstandby_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300318
319 return 0;
320}
321
322/**
323 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
324 * @oh: struct omap_hwmod *
325 * @idlemode: SIDLEMODE field bits
326 * @v: pointer to register contents to modify
327 *
328 * Update the slave idle mode bits in @v to be @idlemode for the @oh
329 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
330 * or 0 upon success.
331 */
332static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
333{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700334 u32 sidle_mask;
335 u8 sidle_shift;
336
Paul Walmsley43b40992010-02-22 22:09:34 -0700337 if (!oh->class->sysc ||
338 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
Paul Walmsley63c85232009-09-03 20:14:03 +0300339 return -EINVAL;
340
Paul Walmsley43b40992010-02-22 22:09:34 -0700341 if (!oh->class->sysc->sysc_fields) {
342 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700343 return -EINVAL;
344 }
345
Paul Walmsley43b40992010-02-22 22:09:34 -0700346 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700347 sidle_mask = (0x3 << sidle_shift);
348
349 *v &= ~sidle_mask;
350 *v |= __ffs(idlemode) << sidle_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300351
352 return 0;
353}
354
355/**
356 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
357 * @oh: struct omap_hwmod *
358 * @clockact: CLOCKACTIVITY field bits
359 * @v: pointer to register contents to modify
360 *
361 * Update the clockactivity mode bits in @v to be @clockact for the
362 * @oh hwmod. Used for additional powersaving on some modules. Does
363 * not write to the hardware. Returns -EINVAL upon error or 0 upon
364 * success.
365 */
366static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
367{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700368 u32 clkact_mask;
369 u8 clkact_shift;
370
Paul Walmsley43b40992010-02-22 22:09:34 -0700371 if (!oh->class->sysc ||
372 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
Paul Walmsley63c85232009-09-03 20:14:03 +0300373 return -EINVAL;
374
Paul Walmsley43b40992010-02-22 22:09:34 -0700375 if (!oh->class->sysc->sysc_fields) {
376 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700377 return -EINVAL;
378 }
379
Paul Walmsley43b40992010-02-22 22:09:34 -0700380 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
Thara Gopinath358f0e62010-02-24 12:05:58 -0700381 clkact_mask = (0x3 << clkact_shift);
382
383 *v &= ~clkact_mask;
384 *v |= clockact << clkact_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300385
386 return 0;
387}
388
389/**
390 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
391 * @oh: struct omap_hwmod *
392 * @v: pointer to register contents to modify
393 *
394 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
395 * error or 0 upon success.
396 */
397static int _set_softreset(struct omap_hwmod *oh, u32 *v)
398{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700399 u32 softrst_mask;
400
Paul Walmsley43b40992010-02-22 22:09:34 -0700401 if (!oh->class->sysc ||
402 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley63c85232009-09-03 20:14:03 +0300403 return -EINVAL;
404
Paul Walmsley43b40992010-02-22 22:09:34 -0700405 if (!oh->class->sysc->sysc_fields) {
406 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700407 return -EINVAL;
408 }
409
Paul Walmsley43b40992010-02-22 22:09:34 -0700410 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700411
412 *v |= softrst_mask;
Paul Walmsley63c85232009-09-03 20:14:03 +0300413
414 return 0;
415}
416
417/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700418 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
419 * @oh: struct omap_hwmod *
420 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
421 * @v: pointer to register contents to modify
422 *
423 * Update the module autoidle bit in @v to be @autoidle for the @oh
424 * hwmod. The autoidle bit controls whether the module can gate
425 * internal clocks automatically when it isn't doing anything; the
426 * exact function of this bit varies on a per-module basis. This
427 * function does not write to the hardware. Returns -EINVAL upon
428 * error or 0 upon success.
429 */
430static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
431 u32 *v)
432{
Thara Gopinath358f0e62010-02-24 12:05:58 -0700433 u32 autoidle_mask;
434 u8 autoidle_shift;
435
Paul Walmsley43b40992010-02-22 22:09:34 -0700436 if (!oh->class->sysc ||
437 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
Paul Walmsley726072e2009-12-08 16:34:15 -0700438 return -EINVAL;
439
Paul Walmsley43b40992010-02-22 22:09:34 -0700440 if (!oh->class->sysc->sysc_fields) {
441 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700442 return -EINVAL;
443 }
444
Paul Walmsley43b40992010-02-22 22:09:34 -0700445 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
Tarun Kanti DebBarma8985b632011-03-03 14:22:46 -0700446 autoidle_mask = (0x1 << autoidle_shift);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700447
448 *v &= ~autoidle_mask;
449 *v |= autoidle << autoidle_shift;
Paul Walmsley726072e2009-12-08 16:34:15 -0700450
451 return 0;
452}
453
454/**
Govindraj Receec002011-12-16 14:36:58 -0700455 * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
456 * @oh: struct omap_hwmod *
457 * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
458 *
459 * Set or clear the I/O pad wakeup flag in the mux entries for the
460 * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
461 * in memory. If the hwmod is currently idled, and the new idle
462 * values don't match the previous ones, this function will also
463 * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
464 * currently idled, this function won't touch the hardware: the new
465 * mux settings are written to the SCM PADCTRL registers when the
466 * hwmod is idled. No return value.
467 */
468static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
469{
470 struct omap_device_pad *pad;
471 bool change = false;
472 u16 prev_idle;
473 int j;
474
475 if (!oh->mux || !oh->mux->enabled)
476 return;
477
478 for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
479 pad = oh->mux->pads_dynamic[j];
480
481 if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
482 continue;
483
484 prev_idle = pad->idle;
485
486 if (set_wake)
487 pad->idle |= OMAP_WAKEUP_EN;
488 else
489 pad->idle &= ~OMAP_WAKEUP_EN;
490
491 if (prev_idle != pad->idle)
492 change = true;
493 }
494
495 if (change && oh->_state == _HWMOD_STATE_IDLE)
496 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
497}
498
499/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300500 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
501 * @oh: struct omap_hwmod *
502 *
503 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
504 * upon error or 0 upon success.
505 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700506static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300507{
Paul Walmsley43b40992010-02-22 22:09:34 -0700508 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700509 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200510 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
511 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300512 return -EINVAL;
513
Paul Walmsley43b40992010-02-22 22:09:34 -0700514 if (!oh->class->sysc->sysc_fields) {
515 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700516 return -EINVAL;
517 }
518
Benoit Cousson1fe74112011-07-01 22:54:03 +0200519 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
520 *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
Paul Walmsley63c85232009-09-03 20:14:03 +0300521
Benoit Cousson86009eb2010-12-21 21:31:28 -0700522 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
523 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200524 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
525 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700526
Paul Walmsley63c85232009-09-03 20:14:03 +0300527 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
528
529 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
530
531 return 0;
532}
533
534/**
535 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
536 * @oh: struct omap_hwmod *
537 *
538 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
539 * upon error or 0 upon success.
540 */
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -0700541static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
Paul Walmsley63c85232009-09-03 20:14:03 +0300542{
Paul Walmsley43b40992010-02-22 22:09:34 -0700543 if (!oh->class->sysc ||
Benoit Cousson86009eb2010-12-21 21:31:28 -0700544 !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
Benoit Cousson724019b2011-07-01 22:54:00 +0200545 (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
546 (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
Paul Walmsley63c85232009-09-03 20:14:03 +0300547 return -EINVAL;
548
Paul Walmsley43b40992010-02-22 22:09:34 -0700549 if (!oh->class->sysc->sysc_fields) {
550 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
Thara Gopinath358f0e62010-02-24 12:05:58 -0700551 return -EINVAL;
552 }
553
Benoit Cousson1fe74112011-07-01 22:54:03 +0200554 if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
555 *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
Paul Walmsley63c85232009-09-03 20:14:03 +0300556
Benoit Cousson86009eb2010-12-21 21:31:28 -0700557 if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
558 _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
Benoit Cousson724019b2011-07-01 22:54:00 +0200559 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
560 _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
Benoit Cousson86009eb2010-12-21 21:31:28 -0700561
Paul Walmsley63c85232009-09-03 20:14:03 +0300562 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
563
564 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
565
566 return 0;
567}
568
569/**
570 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
571 * @oh: struct omap_hwmod *
572 *
573 * Prevent the hardware module @oh from entering idle while the
574 * hardare module initiator @init_oh is active. Useful when a module
575 * will be accessed by a particular initiator (e.g., if a module will
576 * be accessed by the IVA, there should be a sleepdep between the IVA
577 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700578 * mode. If the clockdomain is marked as not needing autodeps, return
579 * 0 without doing anything. Otherwise, returns -EINVAL upon error or
580 * passes along clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300581 */
582static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
583{
584 if (!oh->_clk)
585 return -EINVAL;
586
Paul Walmsley570b54c2011-03-10 03:50:09 -0700587 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
588 return 0;
589
Paul Walmsley55ed9692010-01-26 20:12:59 -0700590 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300591}
592
593/**
594 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
595 * @oh: struct omap_hwmod *
596 *
597 * Allow the hardware module @oh to enter idle while the hardare
598 * module initiator @init_oh is active. Useful when a module will not
599 * be accessed by a particular initiator (e.g., if a module will not
600 * be accessed by the IVA, there should be no sleepdep between the IVA
601 * initiator and the module). Only applies to modules in smart-idle
Paul Walmsley570b54c2011-03-10 03:50:09 -0700602 * mode. If the clockdomain is marked as not needing autodeps, return
603 * 0 without doing anything. Returns -EINVAL upon error or passes
604 * along clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300605 */
606static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
607{
608 if (!oh->_clk)
609 return -EINVAL;
610
Paul Walmsley570b54c2011-03-10 03:50:09 -0700611 if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
612 return 0;
613
Paul Walmsley55ed9692010-01-26 20:12:59 -0700614 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300615}
616
617/**
618 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
619 * @oh: struct omap_hwmod *
620 *
621 * Called from _init_clocks(). Populates the @oh _clk (main
622 * functional clock pointer) if a main_clk is present. Returns 0 on
623 * success or -EINVAL on error.
624 */
625static int _init_main_clk(struct omap_hwmod *oh)
626{
Paul Walmsley63c85232009-09-03 20:14:03 +0300627 int ret = 0;
628
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700629 if (!oh->main_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300630 return 0;
631
Benoit Cousson63403382010-05-20 12:31:10 -0600632 oh->_clk = omap_clk_get_by_name(oh->main_clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600633 if (!oh->_clk) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600634 pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
635 oh->name, oh->main_clk);
Benoit Cousson63403382010-05-20 12:31:10 -0600636 return -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600637 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300638
Benoit Cousson63403382010-05-20 12:31:10 -0600639 if (!oh->_clk->clkdm)
640 pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
641 oh->main_clk, oh->_clk->name);
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700642
Paul Walmsley63c85232009-09-03 20:14:03 +0300643 return ret;
644}
645
646/**
Paul Walmsley887adea2010-07-26 16:34:33 -0600647 * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
Paul Walmsley63c85232009-09-03 20:14:03 +0300648 * @oh: struct omap_hwmod *
649 *
650 * Called from _init_clocks(). Populates the @oh OCP slave interface
651 * clock pointers. Returns 0 on success or -EINVAL on error.
652 */
653static int _init_interface_clks(struct omap_hwmod *oh)
654{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600655 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600656 struct list_head *p;
Paul Walmsley63c85232009-09-03 20:14:03 +0300657 struct clk *c;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600658 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300659 int ret = 0;
660
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600661 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600662
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600663 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600664 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700665 if (!os->clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300666 continue;
667
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700668 c = omap_clk_get_by_name(os->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600669 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600670 pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
671 oh->name, os->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300672 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600673 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300674 os->_clk = c;
675 }
676
677 return ret;
678}
679
680/**
681 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
682 * @oh: struct omap_hwmod *
683 *
684 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
685 * clock pointers. Returns 0 on success or -EINVAL on error.
686 */
687static int _init_opt_clks(struct omap_hwmod *oh)
688{
689 struct omap_hwmod_opt_clk *oc;
690 struct clk *c;
691 int i;
692 int ret = 0;
693
694 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
Paul Walmsley50ebdac2010-02-22 22:09:31 -0700695 c = omap_clk_get_by_name(oc->clk);
Benoit Coussondc759252010-06-23 18:15:12 -0600696 if (!c) {
Benoit Cousson20383d82010-05-20 12:31:09 -0600697 pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
698 oh->name, oc->clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300699 ret = -EINVAL;
Benoit Coussondc759252010-06-23 18:15:12 -0600700 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300701 oc->_clk = c;
702 }
703
704 return ret;
705}
706
707/**
708 * _enable_clocks - enable hwmod main clock and interface clocks
709 * @oh: struct omap_hwmod *
710 *
711 * Enables all clocks necessary for register reads and writes to succeed
712 * on the hwmod @oh. Returns 0.
713 */
714static int _enable_clocks(struct omap_hwmod *oh)
715{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600716 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600717 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600718 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300719
720 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
721
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600722 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300723 clk_enable(oh->_clk);
724
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600725 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600726
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600727 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600728 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300729
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600730 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
731 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300732 }
733
734 /* The opt clocks are controlled by the device driver. */
735
736 return 0;
737}
738
739/**
740 * _disable_clocks - disable hwmod main clock and interface clocks
741 * @oh: struct omap_hwmod *
742 *
743 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
744 */
745static int _disable_clocks(struct omap_hwmod *oh)
746{
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600747 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600748 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600749 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300750
751 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
752
Benoit Cousson4d3ae5a2010-05-20 12:31:09 -0600753 if (oh->_clk)
Paul Walmsley63c85232009-09-03 20:14:03 +0300754 clk_disable(oh->_clk);
755
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600756 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -0600757
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600758 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -0600759 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +0300760
Paul Walmsley5d95dde2012-04-19 04:04:28 -0600761 if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
762 clk_disable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +0300763 }
764
765 /* The opt clocks are controlled by the device driver. */
766
767 return 0;
768}
769
Benoit Cousson96835af2010-09-21 18:57:58 +0200770static void _enable_optional_clocks(struct omap_hwmod *oh)
771{
772 struct omap_hwmod_opt_clk *oc;
773 int i;
774
775 pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
776
777 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
778 if (oc->_clk) {
779 pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
780 oc->_clk->name);
781 clk_enable(oc->_clk);
782 }
783}
784
785static void _disable_optional_clocks(struct omap_hwmod *oh)
786{
787 struct omap_hwmod_opt_clk *oc;
788 int i;
789
790 pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
791
792 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
793 if (oc->_clk) {
794 pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
795 oc->_clk->name);
796 clk_disable(oc->_clk);
797 }
798}
799
Paul Walmsley63c85232009-09-03 20:14:03 +0300800/**
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600801 * _omap4_enable_module - enable CLKCTRL modulemode on OMAP4
Benoit Cousson45c38252011-07-10 05:56:33 -0600802 * @oh: struct omap_hwmod *
803 *
804 * Enables the PRCM module mode related to the hwmod @oh.
805 * No return value.
806 */
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600807static void _omap4_enable_module(struct omap_hwmod *oh)
Benoit Cousson45c38252011-07-10 05:56:33 -0600808{
Benoit Cousson45c38252011-07-10 05:56:33 -0600809 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
810 return;
811
Kevin Hilman3d9f0322012-06-18 12:12:23 -0600812 pr_debug("omap_hwmod: %s: %s: %d\n",
813 oh->name, __func__, oh->prcm.omap4.modulemode);
Benoit Cousson45c38252011-07-10 05:56:33 -0600814
815 omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
816 oh->clkdm->prcm_partition,
817 oh->clkdm->cm_inst,
818 oh->clkdm->clkdm_offs,
819 oh->prcm.omap4.clkctrl_offs);
820}
821
822/**
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800823 * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
824 * @oh: struct omap_hwmod *
825 *
826 * Wait for a module @oh to enter slave idle. Returns 0 if the module
827 * does not have an IDLEST bit or if the module successfully enters
828 * slave idle; otherwise, pass along the return value of the
829 * appropriate *_cm*_wait_module_idle() function.
830 */
831static int _omap4_wait_target_disable(struct omap_hwmod *oh)
832{
Benoit Coussonbfc141e2011-12-16 16:09:11 -0800833 if (!oh)
834 return -EINVAL;
835
836 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
837 return 0;
838
839 if (oh->flags & HWMOD_NO_IDLEST)
840 return 0;
841
842 return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
843 oh->clkdm->cm_inst,
844 oh->clkdm->clkdm_offs,
845 oh->prcm.omap4.clkctrl_offs);
846}
847
848/**
Paul Walmsley212738a2011-07-09 19:14:06 -0600849 * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
850 * @oh: struct omap_hwmod *oh
851 *
852 * Count and return the number of MPU IRQs associated with the hwmod
853 * @oh. Used to allocate struct resource data. Returns 0 if @oh is
854 * NULL.
855 */
856static int _count_mpu_irqs(struct omap_hwmod *oh)
857{
858 struct omap_hwmod_irq_info *ohii;
859 int i = 0;
860
861 if (!oh || !oh->mpu_irqs)
862 return 0;
863
864 do {
865 ohii = &oh->mpu_irqs[i++];
866 } while (ohii->irq != -1);
867
sricharancc1b0762011-11-23 14:35:07 -0800868 return i-1;
Paul Walmsley212738a2011-07-09 19:14:06 -0600869}
870
871/**
Paul Walmsleybc614952011-07-09 19:14:07 -0600872 * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
873 * @oh: struct omap_hwmod *oh
874 *
875 * Count and return the number of SDMA request lines associated with
876 * the hwmod @oh. Used to allocate struct resource data. Returns 0
877 * if @oh is NULL.
878 */
879static int _count_sdma_reqs(struct omap_hwmod *oh)
880{
881 struct omap_hwmod_dma_info *ohdi;
882 int i = 0;
883
884 if (!oh || !oh->sdma_reqs)
885 return 0;
886
887 do {
888 ohdi = &oh->sdma_reqs[i++];
889 } while (ohdi->dma_req != -1);
890
sricharancc1b0762011-11-23 14:35:07 -0800891 return i-1;
Paul Walmsleybc614952011-07-09 19:14:07 -0600892}
893
894/**
Paul Walmsley78183f32011-07-09 19:14:05 -0600895 * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
896 * @oh: struct omap_hwmod *oh
897 *
898 * Count and return the number of address space ranges associated with
899 * the hwmod @oh. Used to allocate struct resource data. Returns 0
900 * if @oh is NULL.
901 */
902static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
903{
904 struct omap_hwmod_addr_space *mem;
905 int i = 0;
906
907 if (!os || !os->addr)
908 return 0;
909
910 do {
911 mem = &os->addr[i++];
912 } while (mem->pa_start != mem->pa_end);
913
sricharancc1b0762011-11-23 14:35:07 -0800914 return i-1;
Paul Walmsley78183f32011-07-09 19:14:05 -0600915}
916
917/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -0600918 * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
919 * @oh: struct omap_hwmod * to operate on
920 * @name: pointer to the name of the MPU interrupt number to fetch (optional)
921 * @irq: pointer to an unsigned int to store the MPU IRQ number to
922 *
923 * Retrieve a MPU hardware IRQ line number named by @name associated
924 * with the IP block pointed to by @oh. The IRQ number will be filled
925 * into the address pointed to by @dma. When @name is non-null, the
926 * IRQ line number associated with the named entry will be returned.
927 * If @name is null, the first matching entry will be returned. Data
928 * order is not meaningful in hwmod data, so callers are strongly
929 * encouraged to use a non-null @name whenever possible to avoid
930 * unpredictable effects if hwmod data is later added that causes data
931 * ordering to change. Returns 0 upon success or a negative error
932 * code upon error.
933 */
934static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
935 unsigned int *irq)
936{
937 int i;
938 bool found = false;
939
940 if (!oh->mpu_irqs)
941 return -ENOENT;
942
943 i = 0;
944 while (oh->mpu_irqs[i].irq != -1) {
945 if (name == oh->mpu_irqs[i].name ||
946 !strcmp(name, oh->mpu_irqs[i].name)) {
947 found = true;
948 break;
949 }
950 i++;
951 }
952
953 if (!found)
954 return -ENOENT;
955
956 *irq = oh->mpu_irqs[i].irq;
957
958 return 0;
959}
960
961/**
962 * _get_sdma_req_by_name - fetch SDMA request line ID by name
963 * @oh: struct omap_hwmod * to operate on
964 * @name: pointer to the name of the SDMA request line to fetch (optional)
965 * @dma: pointer to an unsigned int to store the request line ID to
966 *
967 * Retrieve an SDMA request line ID named by @name on the IP block
968 * pointed to by @oh. The ID will be filled into the address pointed
969 * to by @dma. When @name is non-null, the request line ID associated
970 * with the named entry will be returned. If @name is null, the first
971 * matching entry will be returned. Data order is not meaningful in
972 * hwmod data, so callers are strongly encouraged to use a non-null
973 * @name whenever possible to avoid unpredictable effects if hwmod
974 * data is later added that causes data ordering to change. Returns 0
975 * upon success or a negative error code upon error.
976 */
977static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
978 unsigned int *dma)
979{
980 int i;
981 bool found = false;
982
983 if (!oh->sdma_reqs)
984 return -ENOENT;
985
986 i = 0;
987 while (oh->sdma_reqs[i].dma_req != -1) {
988 if (name == oh->sdma_reqs[i].name ||
989 !strcmp(name, oh->sdma_reqs[i].name)) {
990 found = true;
991 break;
992 }
993 i++;
994 }
995
996 if (!found)
997 return -ENOENT;
998
999 *dma = oh->sdma_reqs[i].dma_req;
1000
1001 return 0;
1002}
1003
1004/**
1005 * _get_addr_space_by_name - fetch address space start & end by name
1006 * @oh: struct omap_hwmod * to operate on
1007 * @name: pointer to the name of the address space to fetch (optional)
1008 * @pa_start: pointer to a u32 to store the starting address to
1009 * @pa_end: pointer to a u32 to store the ending address to
1010 *
1011 * Retrieve address space start and end addresses for the IP block
1012 * pointed to by @oh. The data will be filled into the addresses
1013 * pointed to by @pa_start and @pa_end. When @name is non-null, the
1014 * address space data associated with the named entry will be
1015 * returned. If @name is null, the first matching entry will be
1016 * returned. Data order is not meaningful in hwmod data, so callers
1017 * are strongly encouraged to use a non-null @name whenever possible
1018 * to avoid unpredictable effects if hwmod data is later added that
1019 * causes data ordering to change. Returns 0 upon success or a
1020 * negative error code upon error.
1021 */
1022static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1023 u32 *pa_start, u32 *pa_end)
1024{
1025 int i, j;
1026 struct omap_hwmod_ocp_if *os;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001027 struct list_head *p = NULL;
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001028 bool found = false;
1029
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001030 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001031
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001032 i = 0;
1033 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001034 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5e8370f2012-04-18 19:10:06 -06001035
1036 if (!os->addr)
1037 return -ENOENT;
1038
1039 j = 0;
1040 while (os->addr[j].pa_start != os->addr[j].pa_end) {
1041 if (name == os->addr[j].name ||
1042 !strcmp(name, os->addr[j].name)) {
1043 found = true;
1044 break;
1045 }
1046 j++;
1047 }
1048
1049 if (found)
1050 break;
1051 }
1052
1053 if (!found)
1054 return -ENOENT;
1055
1056 *pa_start = os->addr[j].pa_start;
1057 *pa_end = os->addr[j].pa_end;
1058
1059 return 0;
1060}
1061
1062/**
Paul Walmsley24dbc212012-04-19 04:04:29 -06001063 * _save_mpu_port_index - find and save the index to @oh's MPU port
Paul Walmsley63c85232009-09-03 20:14:03 +03001064 * @oh: struct omap_hwmod *
1065 *
Paul Walmsley24dbc212012-04-19 04:04:29 -06001066 * Determines the array index of the OCP slave port that the MPU uses
1067 * to address the device, and saves it into the struct omap_hwmod.
1068 * Intended to be called during hwmod registration only. No return
1069 * value.
Paul Walmsley63c85232009-09-03 20:14:03 +03001070 */
Paul Walmsley24dbc212012-04-19 04:04:29 -06001071static void __init _save_mpu_port_index(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001072{
Paul Walmsley24dbc212012-04-19 04:04:29 -06001073 struct omap_hwmod_ocp_if *os = NULL;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001074 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001075 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001076
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001077 if (!oh)
Paul Walmsley24dbc212012-04-19 04:04:29 -06001078 return;
1079
1080 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001081
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001082 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001083
Paul Walmsley5d95dde2012-04-19 04:04:28 -06001084 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001085 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley63c85232009-09-03 20:14:03 +03001086 if (os->user & OCP_USER_MPU) {
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001087 oh->_mpu_port = os;
Paul Walmsley24dbc212012-04-19 04:04:29 -06001088 oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001089 break;
1090 }
1091 }
1092
Paul Walmsley24dbc212012-04-19 04:04:29 -06001093 return;
Paul Walmsley63c85232009-09-03 20:14:03 +03001094}
1095
1096/**
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001097 * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1098 * @oh: struct omap_hwmod *
1099 *
1100 * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1101 * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1102 * communicate with the IP block. This interface need not be directly
1103 * connected to the MPU (and almost certainly is not), but is directly
1104 * connected to the IP block represented by @oh. Returns a pointer
1105 * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1106 * error or if there does not appear to be a path from the MPU to this
1107 * IP block.
1108 */
1109static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1110{
1111 if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1112 return NULL;
1113
Paul Walmsley11cd4b92012-04-19 04:04:32 -06001114 return oh->_mpu_port;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001115};
1116
1117/**
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001118 * _find_mpu_rt_addr_space - return MPU register target address space for @oh
Paul Walmsley63c85232009-09-03 20:14:03 +03001119 * @oh: struct omap_hwmod *
1120 *
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001121 * Returns a pointer to the struct omap_hwmod_addr_space record representing
1122 * the register target MPU address space; or returns NULL upon error.
Paul Walmsley63c85232009-09-03 20:14:03 +03001123 */
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001124static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001125{
1126 struct omap_hwmod_ocp_if *os;
1127 struct omap_hwmod_addr_space *mem;
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001128 int found = 0, i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001129
Paul Walmsley2d6141b2012-04-19 04:04:27 -06001130 os = _find_mpu_rt_port(oh);
Paul Walmsley24dbc212012-04-19 04:04:29 -06001131 if (!os || !os->addr)
Paul Walmsley78183f32011-07-09 19:14:05 -06001132 return NULL;
1133
1134 do {
1135 mem = &os->addr[i++];
1136 if (mem->flags & ADDR_TYPE_RT)
Paul Walmsley63c85232009-09-03 20:14:03 +03001137 found = 1;
Paul Walmsley78183f32011-07-09 19:14:05 -06001138 } while (!found && mem->pa_start != mem->pa_end);
Paul Walmsley63c85232009-09-03 20:14:03 +03001139
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001140 return (found) ? mem : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +03001141}
1142
1143/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001144 * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001145 * @oh: struct omap_hwmod *
1146 *
1147 * If module is marked as SWSUP_SIDLE, force the module out of slave
1148 * idle; otherwise, configure it for smart-idle. If module is marked
1149 * as SWSUP_MSUSPEND, force the module out of master standby;
1150 * otherwise, configure it for smart-standby. No return value.
1151 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001152static void _enable_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001153{
Paul Walmsley43b40992010-02-22 22:09:34 -07001154 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001155 u32 v;
1156
Paul Walmsley43b40992010-02-22 22:09:34 -07001157 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001158 return;
1159
1160 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001161 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001162
Paul Walmsley43b40992010-02-22 22:09:34 -07001163 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001164 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1165 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
1166 _set_slave_idlemode(oh, idlemode, &v);
1167 }
1168
Paul Walmsley43b40992010-02-22 22:09:34 -07001169 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001170 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1171 idlemode = HWMOD_IDLEMODE_NO;
1172 } else {
1173 if (sf & SYSC_HAS_ENAWAKEUP)
1174 _enable_wakeup(oh, &v);
1175 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1176 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1177 else
1178 idlemode = HWMOD_IDLEMODE_SMART;
1179 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001180 _set_master_standbymode(oh, idlemode, &v);
1181 }
1182
Paul Walmsleya16b1f72009-12-08 16:34:17 -07001183 /*
1184 * XXX The clock framework should handle this, by
1185 * calling into this code. But this must wait until the
1186 * clock structures are tagged with omap_hwmod entries
1187 */
Paul Walmsley43b40992010-02-22 22:09:34 -07001188 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1189 (sf & SYSC_HAS_CLOCKACTIVITY))
1190 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001191
Rajendra Nayak9980ce52010-09-21 19:58:30 +05301192 /* If slave is in SMARTIDLE, also enable wakeup */
1193 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07001194 _enable_wakeup(oh, &v);
1195
1196 _write_sysconfig(v, oh);
Hema HK78f26e82010-09-24 10:23:19 -06001197
1198 /*
1199 * Set the autoidle bit only after setting the smartidle bit
1200 * Setting this will not have any impact on the other modules.
1201 */
1202 if (sf & SYSC_HAS_AUTOIDLE) {
1203 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1204 0 : 1;
1205 _set_module_autoidle(oh, idlemode, &v);
1206 _write_sysconfig(v, oh);
1207 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001208}
1209
1210/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001211 * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001212 * @oh: struct omap_hwmod *
1213 *
1214 * If module is marked as SWSUP_SIDLE, force the module into slave
1215 * idle; otherwise, configure it for smart-idle. If module is marked
1216 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1217 * configure it for smart-standby. No return value.
1218 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001219static void _idle_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001220{
Paul Walmsley43b40992010-02-22 22:09:34 -07001221 u8 idlemode, sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001222 u32 v;
1223
Paul Walmsley43b40992010-02-22 22:09:34 -07001224 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001225 return;
1226
1227 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001228 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001229
Paul Walmsley43b40992010-02-22 22:09:34 -07001230 if (sf & SYSC_HAS_SIDLEMODE) {
Paul Walmsley63c85232009-09-03 20:14:03 +03001231 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
1232 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
1233 _set_slave_idlemode(oh, idlemode, &v);
1234 }
1235
Paul Walmsley43b40992010-02-22 22:09:34 -07001236 if (sf & SYSC_HAS_MIDLEMODE) {
Benoit Cousson724019b2011-07-01 22:54:00 +02001237 if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1238 idlemode = HWMOD_IDLEMODE_FORCE;
1239 } else {
1240 if (sf & SYSC_HAS_ENAWAKEUP)
1241 _enable_wakeup(oh, &v);
1242 if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1243 idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1244 else
1245 idlemode = HWMOD_IDLEMODE_SMART;
1246 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001247 _set_master_standbymode(oh, idlemode, &v);
1248 }
1249
Benoit Cousson86009eb2010-12-21 21:31:28 -07001250 /* If slave is in SMARTIDLE, also enable wakeup */
1251 if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
1252 _enable_wakeup(oh, &v);
1253
Paul Walmsley63c85232009-09-03 20:14:03 +03001254 _write_sysconfig(v, oh);
1255}
1256
1257/**
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001258 * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
Paul Walmsley63c85232009-09-03 20:14:03 +03001259 * @oh: struct omap_hwmod *
1260 *
1261 * Force the module into slave idle and master suspend. No return
1262 * value.
1263 */
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001264static void _shutdown_sysc(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001265{
1266 u32 v;
Paul Walmsley43b40992010-02-22 22:09:34 -07001267 u8 sf;
Paul Walmsley63c85232009-09-03 20:14:03 +03001268
Paul Walmsley43b40992010-02-22 22:09:34 -07001269 if (!oh->class->sysc)
Paul Walmsley63c85232009-09-03 20:14:03 +03001270 return;
1271
1272 v = oh->_sysc_cache;
Paul Walmsley43b40992010-02-22 22:09:34 -07001273 sf = oh->class->sysc->sysc_flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03001274
Paul Walmsley43b40992010-02-22 22:09:34 -07001275 if (sf & SYSC_HAS_SIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001276 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1277
Paul Walmsley43b40992010-02-22 22:09:34 -07001278 if (sf & SYSC_HAS_MIDLEMODE)
Paul Walmsley63c85232009-09-03 20:14:03 +03001279 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1280
Paul Walmsley43b40992010-02-22 22:09:34 -07001281 if (sf & SYSC_HAS_AUTOIDLE)
Paul Walmsley726072e2009-12-08 16:34:15 -07001282 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +03001283
1284 _write_sysconfig(v, oh);
1285}
1286
1287/**
1288 * _lookup - find an omap_hwmod by name
1289 * @name: find an omap_hwmod by name
1290 *
1291 * Return a pointer to an omap_hwmod by name, or NULL if not found.
Paul Walmsley63c85232009-09-03 20:14:03 +03001292 */
1293static struct omap_hwmod *_lookup(const char *name)
1294{
1295 struct omap_hwmod *oh, *temp_oh;
1296
1297 oh = NULL;
1298
1299 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1300 if (!strcmp(name, temp_oh->name)) {
1301 oh = temp_oh;
1302 break;
1303 }
1304 }
1305
1306 return oh;
1307}
Benoit Cousson6ae76992011-07-10 05:56:30 -06001308/**
1309 * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1310 * @oh: struct omap_hwmod *
1311 *
1312 * Convert a clockdomain name stored in a struct omap_hwmod into a
1313 * clockdomain pointer, and save it into the struct omap_hwmod.
1314 * return -EINVAL if clkdm_name does not exist or if the lookup failed.
1315 */
1316static int _init_clkdm(struct omap_hwmod *oh)
1317{
1318 if (cpu_is_omap24xx() || cpu_is_omap34xx())
1319 return 0;
1320
1321 if (!oh->clkdm_name) {
1322 pr_warning("omap_hwmod: %s: no clkdm_name\n", oh->name);
1323 return -EINVAL;
1324 }
1325
1326 oh->clkdm = clkdm_lookup(oh->clkdm_name);
1327 if (!oh->clkdm) {
1328 pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
1329 oh->name, oh->clkdm_name);
1330 return -EINVAL;
1331 }
1332
1333 pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1334 oh->name, oh->clkdm_name);
1335
1336 return 0;
1337}
Paul Walmsley63c85232009-09-03 20:14:03 +03001338
1339/**
Benoit Cousson6ae76992011-07-10 05:56:30 -06001340 * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1341 * well the clockdomain.
Paul Walmsley63c85232009-09-03 20:14:03 +03001342 * @oh: struct omap_hwmod *
Paul Walmsley97d60162010-07-26 16:34:30 -06001343 * @data: not used; pass NULL
Paul Walmsley63c85232009-09-03 20:14:03 +03001344 *
Paul Walmsleya2debdb2011-02-23 00:14:07 -07001345 * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
Paul Walmsley48d54f32011-02-23 00:14:07 -07001346 * Resolves all clock names embedded in the hwmod. Returns 0 on
1347 * success, or a negative error code on failure.
Paul Walmsley63c85232009-09-03 20:14:03 +03001348 */
Paul Walmsley97d60162010-07-26 16:34:30 -06001349static int _init_clocks(struct omap_hwmod *oh, void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03001350{
1351 int ret = 0;
1352
Paul Walmsley48d54f32011-02-23 00:14:07 -07001353 if (oh->_state != _HWMOD_STATE_REGISTERED)
1354 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001355
1356 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1357
1358 ret |= _init_main_clk(oh);
1359 ret |= _init_interface_clks(oh);
1360 ret |= _init_opt_clks(oh);
Benoit Cousson6ae76992011-07-10 05:56:30 -06001361 ret |= _init_clkdm(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001362
Benoit Coussonf5c1f842010-05-20 12:31:10 -06001363 if (!ret)
1364 oh->_state = _HWMOD_STATE_CLKS_INITED;
Benoit Cousson66522712011-07-01 22:54:06 +02001365 else
1366 pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001367
Rajendra Nayak09c35f22011-02-16 12:11:24 +00001368 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001369}
1370
1371/**
omar ramirezcc1226e2011-03-04 13:32:44 -07001372 * _lookup_hardreset - fill register bit info for this hwmod/reset line
Benoît Cousson5365efb2010-09-21 10:34:11 -06001373 * @oh: struct omap_hwmod *
1374 * @name: name of the reset line in the context of this hwmod
omar ramirezcc1226e2011-03-04 13:32:44 -07001375 * @ohri: struct omap_hwmod_rst_info * that this function will fill in
Benoît Cousson5365efb2010-09-21 10:34:11 -06001376 *
1377 * Return the bit position of the reset line that match the
1378 * input name. Return -ENOENT if not found.
1379 */
omar ramirezcc1226e2011-03-04 13:32:44 -07001380static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1381 struct omap_hwmod_rst_info *ohri)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001382{
1383 int i;
1384
1385 for (i = 0; i < oh->rst_lines_cnt; i++) {
1386 const char *rst_line = oh->rst_lines[i].name;
1387 if (!strcmp(rst_line, name)) {
omar ramirezcc1226e2011-03-04 13:32:44 -07001388 ohri->rst_shift = oh->rst_lines[i].rst_shift;
1389 ohri->st_shift = oh->rst_lines[i].st_shift;
1390 pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1391 oh->name, __func__, rst_line, ohri->rst_shift,
1392 ohri->st_shift);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001393
omar ramirezcc1226e2011-03-04 13:32:44 -07001394 return 0;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001395 }
1396 }
1397
1398 return -ENOENT;
1399}
1400
1401/**
1402 * _assert_hardreset - assert the HW reset line of submodules
1403 * contained in the hwmod module.
1404 * @oh: struct omap_hwmod *
1405 * @name: name of the reset line to lookup and assert
1406 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001407 * Some IP like dsp, ipu or iva contain processor that require an HW
1408 * reset line to be assert / deassert in order to enable fully the IP.
1409 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1410 * asserting the hardreset line on the currently-booted SoC, or passes
1411 * along the return value from _lookup_hardreset() or the SoC's
1412 * assert_hardreset code.
Benoît Cousson5365efb2010-09-21 10:34:11 -06001413 */
1414static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1415{
omar ramirezcc1226e2011-03-04 13:32:44 -07001416 struct omap_hwmod_rst_info ohri;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001417 u8 ret = -EINVAL;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001418
1419 if (!oh)
1420 return -EINVAL;
1421
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001422 if (!soc_ops.assert_hardreset)
1423 return -ENOSYS;
1424
omar ramirezcc1226e2011-03-04 13:32:44 -07001425 ret = _lookup_hardreset(oh, name, &ohri);
1426 if (IS_ERR_VALUE(ret))
1427 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001428
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001429 ret = soc_ops.assert_hardreset(oh, &ohri);
1430
1431 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001432}
1433
1434/**
1435 * _deassert_hardreset - deassert the HW reset line of submodules contained
1436 * in the hwmod module.
1437 * @oh: struct omap_hwmod *
1438 * @name: name of the reset line to look up and deassert
1439 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001440 * Some IP like dsp, ipu or iva contain processor that require an HW
1441 * reset line to be assert / deassert in order to enable fully the IP.
1442 * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1443 * deasserting the hardreset line on the currently-booted SoC, or passes
1444 * along the return value from _lookup_hardreset() or the SoC's
1445 * deassert_hardreset code.
Benoît Cousson5365efb2010-09-21 10:34:11 -06001446 */
1447static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1448{
omar ramirezcc1226e2011-03-04 13:32:44 -07001449 struct omap_hwmod_rst_info ohri;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001450 int ret = -EINVAL;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001451
1452 if (!oh)
1453 return -EINVAL;
1454
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001455 if (!soc_ops.deassert_hardreset)
1456 return -ENOSYS;
1457
omar ramirezcc1226e2011-03-04 13:32:44 -07001458 ret = _lookup_hardreset(oh, name, &ohri);
1459 if (IS_ERR_VALUE(ret))
1460 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001461
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001462 ret = soc_ops.deassert_hardreset(oh, &ohri);
omar ramirezcc1226e2011-03-04 13:32:44 -07001463 if (ret == -EBUSY)
Benoît Cousson5365efb2010-09-21 10:34:11 -06001464 pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
1465
omar ramirezcc1226e2011-03-04 13:32:44 -07001466 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001467}
1468
1469/**
1470 * _read_hardreset - read the HW reset line state of submodules
1471 * contained in the hwmod module
1472 * @oh: struct omap_hwmod *
1473 * @name: name of the reset line to look up and read
1474 *
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001475 * Return the state of the reset line. Returns -EINVAL if @oh is
1476 * null, -ENOSYS if we have no way of reading the hardreset line
1477 * status on the currently-booted SoC, or passes along the return
1478 * value from _lookup_hardreset() or the SoC's is_hardreset_asserted
1479 * code.
Benoît Cousson5365efb2010-09-21 10:34:11 -06001480 */
1481static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1482{
omar ramirezcc1226e2011-03-04 13:32:44 -07001483 struct omap_hwmod_rst_info ohri;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001484 u8 ret = -EINVAL;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001485
1486 if (!oh)
1487 return -EINVAL;
1488
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001489 if (!soc_ops.is_hardreset_asserted)
1490 return -ENOSYS;
1491
omar ramirezcc1226e2011-03-04 13:32:44 -07001492 ret = _lookup_hardreset(oh, name, &ohri);
1493 if (IS_ERR_VALUE(ret))
1494 return ret;
Benoît Cousson5365efb2010-09-21 10:34:11 -06001495
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06001496 return soc_ops.is_hardreset_asserted(oh, &ohri);
Benoît Cousson5365efb2010-09-21 10:34:11 -06001497}
1498
1499/**
Paul Walmsley747834a2012-04-18 19:10:04 -06001500 * _are_any_hardreset_lines_asserted - return true if part of @oh is hard-reset
1501 * @oh: struct omap_hwmod *
1502 *
1503 * If any hardreset line associated with @oh is asserted, then return true.
1504 * Otherwise, if @oh has no hardreset lines associated with it, or if
1505 * no hardreset lines associated with @oh are asserted, then return false.
1506 * This function is used to avoid executing some parts of the IP block
1507 * enable/disable sequence if a hardreset line is set.
1508 */
1509static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1510{
1511 int i;
1512
1513 if (oh->rst_lines_cnt == 0)
1514 return false;
1515
1516 for (i = 0; i < oh->rst_lines_cnt; i++)
1517 if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1518 return true;
1519
1520 return false;
1521}
1522
1523/**
1524 * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1525 * @oh: struct omap_hwmod *
1526 *
1527 * Disable the PRCM module mode related to the hwmod @oh.
1528 * Return EINVAL if the modulemode is not supported and 0 in case of success.
1529 */
1530static int _omap4_disable_module(struct omap_hwmod *oh)
1531{
1532 int v;
1533
Paul Walmsley747834a2012-04-18 19:10:04 -06001534 if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1535 return -EINVAL;
1536
1537 pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1538
1539 omap4_cminst_module_disable(oh->clkdm->prcm_partition,
1540 oh->clkdm->cm_inst,
1541 oh->clkdm->clkdm_offs,
1542 oh->prcm.omap4.clkctrl_offs);
1543
1544 if (_are_any_hardreset_lines_asserted(oh))
1545 return 0;
1546
1547 v = _omap4_wait_target_disable(oh);
1548 if (v)
1549 pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1550 oh->name);
1551
1552 return 0;
1553}
1554
1555/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001556 * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
Paul Walmsley63c85232009-09-03 20:14:03 +03001557 * @oh: struct omap_hwmod *
1558 *
1559 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
Paul Walmsley30e105c2012-04-19 00:49:09 -06001560 * enabled for this to work. Returns -ENOENT if the hwmod cannot be
1561 * reset this way, -EINVAL if the hwmod is in the wrong state,
1562 * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
Benoit Cousson2cb06812010-09-21 18:57:59 +02001563 *
1564 * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
Paul Walmsleybd361792010-12-14 12:42:35 -07001565 * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
Benoit Cousson2cb06812010-09-21 18:57:59 +02001566 * use the SYSCONFIG softreset bit to provide the status.
1567 *
Paul Walmsleybd361792010-12-14 12:42:35 -07001568 * Note that some IP like McBSP do have reset control but don't have
1569 * reset status.
Paul Walmsley63c85232009-09-03 20:14:03 +03001570 */
Paul Walmsleybd361792010-12-14 12:42:35 -07001571static int _ocp_softreset(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001572{
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001573 u32 v, softrst_mask;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -07001574 int c = 0;
Benoit Cousson96835af2010-09-21 18:57:58 +02001575 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001576
Paul Walmsley43b40992010-02-22 22:09:34 -07001577 if (!oh->class->sysc ||
Benoit Cousson2cb06812010-09-21 18:57:59 +02001578 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
Paul Walmsley30e105c2012-04-19 00:49:09 -06001579 return -ENOENT;
Paul Walmsley63c85232009-09-03 20:14:03 +03001580
1581 /* clocks must be on for this operation */
1582 if (oh->_state != _HWMOD_STATE_ENABLED) {
Benoit Cousson76e55892010-09-21 10:34:11 -06001583 pr_warning("omap_hwmod: %s: reset can only be entered from "
1584 "enabled state\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001585 return -EINVAL;
1586 }
1587
Benoit Cousson96835af2010-09-21 18:57:58 +02001588 /* For some modules, all optionnal clocks need to be enabled as well */
1589 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1590 _enable_optional_clocks(oh);
1591
Paul Walmsleybd361792010-12-14 12:42:35 -07001592 pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001593
1594 v = oh->_sysc_cache;
Benoit Cousson96835af2010-09-21 18:57:58 +02001595 ret = _set_softreset(oh, &v);
1596 if (ret)
1597 goto dis_opt_clks;
Paul Walmsley63c85232009-09-03 20:14:03 +03001598 _write_sysconfig(v, oh);
1599
Fernando Guzman Lugod99de7f2012-04-13 05:08:03 -06001600 if (oh->class->sysc->srst_udelay)
1601 udelay(oh->class->sysc->srst_udelay);
1602
Benoit Cousson2cb06812010-09-21 18:57:59 +02001603 if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001604 omap_test_timeout((omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001605 oh->class->sysc->syss_offs)
1606 & SYSS_RESETDONE_MASK),
1607 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001608 else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
1609 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07001610 omap_test_timeout(!(omap_hwmod_read(oh,
Benoit Cousson2cb06812010-09-21 18:57:59 +02001611 oh->class->sysc->sysc_offs)
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001612 & softrst_mask),
Benoit Cousson2cb06812010-09-21 18:57:59 +02001613 MAX_MODULE_SOFTRESET_WAIT, c);
Rajendra Nayak387ca5b2012-03-12 04:29:58 -06001614 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001615
Benoît Cousson5365efb2010-09-21 10:34:11 -06001616 if (c == MAX_MODULE_SOFTRESET_WAIT)
Benoit Cousson76e55892010-09-21 10:34:11 -06001617 pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1618 oh->name, MAX_MODULE_SOFTRESET_WAIT);
Paul Walmsley63c85232009-09-03 20:14:03 +03001619 else
Benoît Cousson5365efb2010-09-21 10:34:11 -06001620 pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +03001621
1622 /*
1623 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1624 * _wait_target_ready() or _reset()
1625 */
1626
Benoit Cousson96835af2010-09-21 18:57:58 +02001627 ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
1628
1629dis_opt_clks:
1630 if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1631 _disable_optional_clocks(oh);
1632
1633 return ret;
Paul Walmsley63c85232009-09-03 20:14:03 +03001634}
1635
1636/**
Paul Walmsleybd361792010-12-14 12:42:35 -07001637 * _reset - reset an omap_hwmod
1638 * @oh: struct omap_hwmod *
1639 *
Paul Walmsley30e105c2012-04-19 00:49:09 -06001640 * Resets an omap_hwmod @oh. If the module has a custom reset
1641 * function pointer defined, then call it to reset the IP block, and
1642 * pass along its return value to the caller. Otherwise, if the IP
1643 * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1644 * associated with it, call a function to reset the IP block via that
1645 * method, and pass along the return value to the caller. Finally, if
1646 * the IP block has some hardreset lines associated with it, assert
1647 * all of those, but do _not_ deassert them. (This is because driver
1648 * authors have expressed an apparent requirement to control the
1649 * deassertion of the hardreset lines themselves.)
1650 *
1651 * The default software reset mechanism for most OMAP IP blocks is
1652 * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
1653 * hwmods cannot be reset via this method. Some are not targets and
1654 * therefore have no OCP header registers to access. Others (like the
1655 * IVA) have idiosyncratic reset sequences. So for these relatively
1656 * rare cases, custom reset code can be supplied in the struct
1657 * omap_hwmod_class .reset function pointer. Passes along the return
1658 * value from either _ocp_softreset() or the custom reset function -
1659 * these must return -EINVAL if the hwmod cannot be reset this way or
1660 * if the hwmod is in the wrong state, -ETIMEDOUT if the module did
1661 * not reset in time, or 0 upon success.
Paul Walmsleybd361792010-12-14 12:42:35 -07001662 */
1663static int _reset(struct omap_hwmod *oh)
1664{
Paul Walmsley30e105c2012-04-19 00:49:09 -06001665 int i, r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001666
1667 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1668
Paul Walmsley30e105c2012-04-19 00:49:09 -06001669 if (oh->class->reset) {
1670 r = oh->class->reset(oh);
1671 } else {
1672 if (oh->rst_lines_cnt > 0) {
1673 for (i = 0; i < oh->rst_lines_cnt; i++)
1674 _assert_hardreset(oh, oh->rst_lines[i].name);
1675 return 0;
1676 } else {
1677 r = _ocp_softreset(oh);
1678 if (r == -ENOENT)
1679 r = 0;
1680 }
1681 }
Paul Walmsleybd361792010-12-14 12:42:35 -07001682
Paul Walmsley30e105c2012-04-19 00:49:09 -06001683 /*
1684 * OCP_SYSCONFIG bits need to be reprogrammed after a
1685 * softreset. The _enable() function should be split to avoid
1686 * the rewrite of the OCP_SYSCONFIG register.
1687 */
Rajendra Nayak28008522012-03-13 22:55:23 +05301688 if (oh->class->sysc) {
1689 _update_sysc_cache(oh);
1690 _enable_sysc(oh);
1691 }
1692
Paul Walmsley30e105c2012-04-19 00:49:09 -06001693 return r;
Paul Walmsleybd361792010-12-14 12:42:35 -07001694}
1695
1696/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001697 * _enable - enable an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001698 * @oh: struct omap_hwmod *
1699 *
1700 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001701 * register target. Returns -EINVAL if the hwmod is in the wrong
1702 * state or passes along the return value of _wait_target_ready().
Paul Walmsley63c85232009-09-03 20:14:03 +03001703 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001704static int _enable(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001705{
Paul Walmsley747834a2012-04-18 19:10:04 -06001706 int r;
Rajendra Nayak665d0012011-07-10 05:57:07 -06001707 int hwsup = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001708
Benoit Cousson34617e22011-07-01 22:54:07 +02001709 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
1710
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001711 /*
Paul Walmsley64813c32012-04-18 19:10:03 -06001712 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
1713 * state at init. Now that someone is really trying to enable
1714 * them, just ensure that the hwmod mux is set.
Rajendra Nayakaacf0942011-12-16 05:50:12 -07001715 */
1716 if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
1717 /*
1718 * If the caller has mux data populated, do the mux'ing
1719 * which wouldn't have been done as part of the _enable()
1720 * done during setup.
1721 */
1722 if (oh->mux)
1723 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
1724
1725 oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
1726 return 0;
1727 }
1728
Paul Walmsley63c85232009-09-03 20:14:03 +03001729 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
1730 oh->_state != _HWMOD_STATE_IDLE &&
1731 oh->_state != _HWMOD_STATE_DISABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001732 WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
1733 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001734 return -EINVAL;
1735 }
1736
Benoit Cousson31f62862011-07-01 22:54:05 +02001737 /*
Paul Walmsley747834a2012-04-18 19:10:04 -06001738 * If an IP block contains HW reset lines and any of them are
1739 * asserted, we let integration code associated with that
1740 * block handle the enable. We've received very little
1741 * information on what those driver authors need, and until
1742 * detailed information is provided and the driver code is
1743 * posted to the public lists, this is probably the best we
1744 * can do.
Benoit Cousson31f62862011-07-01 22:54:05 +02001745 */
Paul Walmsley747834a2012-04-18 19:10:04 -06001746 if (_are_any_hardreset_lines_asserted(oh))
1747 return 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03001748
Rajendra Nayak665d0012011-07-10 05:57:07 -06001749 /* Mux pins for device runtime if populated */
1750 if (oh->mux && (!oh->mux->enabled ||
1751 ((oh->_state == _HWMOD_STATE_IDLE) &&
1752 oh->mux->pads_dynamic)))
1753 omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
Benoit Cousson34617e22011-07-01 22:54:07 +02001754
Rajendra Nayak665d0012011-07-10 05:57:07 -06001755 _add_initiator_dep(oh, mpu_oh);
1756
1757 if (oh->clkdm) {
1758 /*
1759 * A clockdomain must be in SW_SUP before enabling
1760 * completely the module. The clockdomain can be set
1761 * in HW_AUTO only when the module become ready.
1762 */
1763 hwsup = clkdm_in_hwsup(oh->clkdm);
1764 r = clkdm_hwmod_enable(oh->clkdm, oh);
1765 if (r) {
1766 WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1767 oh->name, oh->clkdm->name, r);
1768 return r;
1769 }
Benoit Cousson34617e22011-07-01 22:54:07 +02001770 }
Rajendra Nayak665d0012011-07-10 05:57:07 -06001771
1772 _enable_clocks(oh);
Kevin Hilman9ebfd282012-06-18 12:12:23 -06001773 if (soc_ops.enable_module)
1774 soc_ops.enable_module(oh);
Benoit Cousson34617e22011-07-01 22:54:07 +02001775
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06001776 r = (soc_ops.wait_target_ready) ? soc_ops.wait_target_ready(oh) :
1777 -EINVAL;
Rajendra Nayak665d0012011-07-10 05:57:07 -06001778 if (!r) {
1779 /*
1780 * Set the clockdomain to HW_AUTO only if the target is ready,
1781 * assuming that the previous state was HW_AUTO
1782 */
1783 if (oh->clkdm && hwsup)
1784 clkdm_allow_idle(oh->clkdm);
Benoit Cousson34617e22011-07-01 22:54:07 +02001785
Rajendra Nayak665d0012011-07-10 05:57:07 -06001786 oh->_state = _HWMOD_STATE_ENABLED;
1787
1788 /* Access the sysconfig only if the target is ready */
1789 if (oh->class->sysc) {
1790 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
1791 _update_sysc_cache(oh);
1792 _enable_sysc(oh);
1793 }
1794 } else {
1795 _disable_clocks(oh);
1796 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
1797 oh->name, r);
1798
1799 if (oh->clkdm)
1800 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06001801 }
1802
Paul Walmsley63c85232009-09-03 20:14:03 +03001803 return r;
1804}
1805
1806/**
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001807 * _idle - idle an omap_hwmod
Paul Walmsley63c85232009-09-03 20:14:03 +03001808 * @oh: struct omap_hwmod *
1809 *
1810 * Idles an omap_hwmod @oh. This should be called once the hwmod has
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001811 * no further work. Returns -EINVAL if the hwmod is in the wrong
1812 * state or returns 0.
Paul Walmsley63c85232009-09-03 20:14:03 +03001813 */
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001814static int _idle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03001815{
Benoit Cousson34617e22011-07-01 22:54:07 +02001816 pr_debug("omap_hwmod: %s: idling\n", oh->name);
1817
Paul Walmsley63c85232009-09-03 20:14:03 +03001818 if (oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001819 WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
1820 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001821 return -EINVAL;
1822 }
1823
Paul Walmsley747834a2012-04-18 19:10:04 -06001824 if (_are_any_hardreset_lines_asserted(oh))
1825 return 0;
1826
Paul Walmsley43b40992010-02-22 22:09:34 -07001827 if (oh->class->sysc)
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001828 _idle_sysc(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001829 _del_initiator_dep(oh, mpu_oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001830
Kevin Hilman9ebfd282012-06-18 12:12:23 -06001831 if (soc_ops.disable_module)
1832 soc_ops.disable_module(oh);
Benoit Coussonbfc141e2011-12-16 16:09:11 -08001833
Benoit Cousson45c38252011-07-10 05:56:33 -06001834 /*
1835 * The module must be in idle mode before disabling any parents
1836 * clocks. Otherwise, the parent clock might be disabled before
1837 * the module transition is done, and thus will prevent the
1838 * transition to complete properly.
1839 */
1840 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001841 if (oh->clkdm)
1842 clkdm_hwmod_disable(oh->clkdm, oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03001843
Tony Lindgren8d9af882010-12-22 18:42:35 -08001844 /* Mux pins for device idle if populated */
Tony Lindgren029268e2011-03-11 11:32:25 -08001845 if (oh->mux && oh->mux->pads_dynamic)
Tony Lindgren8d9af882010-12-22 18:42:35 -08001846 omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
1847
Paul Walmsley63c85232009-09-03 20:14:03 +03001848 oh->_state = _HWMOD_STATE_IDLE;
1849
1850 return 0;
1851}
1852
1853/**
Kishon Vijay Abraham I95992172011-03-10 03:50:08 -07001854 * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
1855 * @oh: struct omap_hwmod *
1856 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
1857 *
1858 * Sets the IP block's OCP autoidle bit in hardware, and updates our
1859 * local copy. Intended to be used by drivers that require
1860 * direct manipulation of the AUTOIDLE bits.
1861 * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
1862 * along the return value from _set_module_autoidle().
1863 *
1864 * Any users of this function should be scrutinized carefully.
1865 */
1866int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
1867{
1868 u32 v;
1869 int retval = 0;
1870 unsigned long flags;
1871
1872 if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
1873 return -EINVAL;
1874
1875 spin_lock_irqsave(&oh->_lock, flags);
1876
1877 v = oh->_sysc_cache;
1878
1879 retval = _set_module_autoidle(oh, autoidle, &v);
1880
1881 if (!retval)
1882 _write_sysconfig(v, oh);
1883
1884 spin_unlock_irqrestore(&oh->_lock, flags);
1885
1886 return retval;
1887}
1888
1889/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001890 * _shutdown - shutdown an omap_hwmod
1891 * @oh: struct omap_hwmod *
1892 *
1893 * Shut down an omap_hwmod @oh. This should be called when the driver
1894 * used for the hwmod is removed or unloaded or if the driver is not
1895 * used by the system. Returns -EINVAL if the hwmod is in the wrong
1896 * state or returns 0.
1897 */
1898static int _shutdown(struct omap_hwmod *oh)
1899{
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001900 int ret, i;
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001901 u8 prev_state;
1902
Paul Walmsley63c85232009-09-03 20:14:03 +03001903 if (oh->_state != _HWMOD_STATE_IDLE &&
1904 oh->_state != _HWMOD_STATE_ENABLED) {
Russell King4f8a4282012-02-07 10:59:37 +00001905 WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
1906 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03001907 return -EINVAL;
1908 }
1909
Paul Walmsley747834a2012-04-18 19:10:04 -06001910 if (_are_any_hardreset_lines_asserted(oh))
1911 return 0;
1912
Paul Walmsley63c85232009-09-03 20:14:03 +03001913 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
1914
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001915 if (oh->class->pre_shutdown) {
1916 prev_state = oh->_state;
1917 if (oh->_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001918 _enable(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001919 ret = oh->class->pre_shutdown(oh);
1920 if (ret) {
1921 if (prev_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07001922 _idle(oh);
Paul Walmsleye4dc8f52010-12-14 12:42:34 -07001923 return ret;
1924 }
1925 }
1926
Miguel Vadillo6481c732011-07-01 22:54:02 +02001927 if (oh->class->sysc) {
1928 if (oh->_state == _HWMOD_STATE_IDLE)
1929 _enable(oh);
Paul Walmsley74ff3a62010-09-21 15:02:23 -06001930 _shutdown_sysc(oh);
Miguel Vadillo6481c732011-07-01 22:54:02 +02001931 }
Benoît Cousson5365efb2010-09-21 10:34:11 -06001932
Benoit Cousson3827f942010-09-21 10:34:08 -06001933 /* clocks and deps are already disabled in idle */
1934 if (oh->_state == _HWMOD_STATE_ENABLED) {
1935 _del_initiator_dep(oh, mpu_oh);
1936 /* XXX what about the other system initiators here? dma, dsp */
Kevin Hilman9ebfd282012-06-18 12:12:23 -06001937 if (soc_ops.disable_module)
1938 soc_ops.disable_module(oh);
Benoit Cousson45c38252011-07-10 05:56:33 -06001939 _disable_clocks(oh);
Rajendra Nayak665d0012011-07-10 05:57:07 -06001940 if (oh->clkdm)
1941 clkdm_hwmod_disable(oh->clkdm, oh);
Benoit Cousson3827f942010-09-21 10:34:08 -06001942 }
Paul Walmsley63c85232009-09-03 20:14:03 +03001943 /* XXX Should this code also force-disable the optional clocks? */
1944
Paul Walmsley9c8b0ec2012-04-18 19:10:02 -06001945 for (i = 0; i < oh->rst_lines_cnt; i++)
1946 _assert_hardreset(oh, oh->rst_lines[i].name);
Benoit Cousson31f62862011-07-01 22:54:05 +02001947
Tony Lindgren8d9af882010-12-22 18:42:35 -08001948 /* Mux pins to safe mode or use populated off mode values */
1949 if (oh->mux)
1950 omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
Paul Walmsley63c85232009-09-03 20:14:03 +03001951
1952 oh->_state = _HWMOD_STATE_DISABLED;
1953
1954 return 0;
1955}
1956
1957/**
Paul Walmsley381d0332012-04-19 00:58:22 -06001958 * _init_mpu_rt_base - populate the virtual address for a hwmod
1959 * @oh: struct omap_hwmod * to locate the virtual address
1960 *
1961 * Cache the virtual address used by the MPU to access this IP block's
1962 * registers. This address is needed early so the OCP registers that
1963 * are part of the device's address space can be ioremapped properly.
1964 * No return value.
1965 */
1966static void __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data)
1967{
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001968 struct omap_hwmod_addr_space *mem;
1969 void __iomem *va_start;
1970
1971 if (!oh)
1972 return;
1973
Paul Walmsley2221b5c2012-04-19 04:04:30 -06001974 _save_mpu_port_index(oh);
1975
Paul Walmsley381d0332012-04-19 00:58:22 -06001976 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1977 return;
1978
Paul Walmsleyc9aafd22012-04-18 19:10:05 -06001979 mem = _find_mpu_rt_addr_space(oh);
1980 if (!mem) {
1981 pr_debug("omap_hwmod: %s: no MPU register target found\n",
1982 oh->name);
1983 return;
1984 }
1985
1986 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
1987 if (!va_start) {
1988 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
1989 return;
1990 }
1991
1992 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
1993 oh->name, va_start);
1994
1995 oh->_mpu_rt_va = va_start;
Paul Walmsley381d0332012-04-19 00:58:22 -06001996}
1997
1998/**
1999 * _init - initialize internal data for the hwmod @oh
2000 * @oh: struct omap_hwmod *
2001 * @n: (unused)
2002 *
2003 * Look up the clocks and the address space used by the MPU to access
2004 * registers belonging to the hwmod @oh. @oh must already be
2005 * registered at this point. This is the first of two phases for
2006 * hwmod initialization. Code called here does not touch any hardware
2007 * registers, it simply prepares internal data structures. Returns 0
2008 * upon success or if the hwmod isn't registered, or -EINVAL upon
2009 * failure.
2010 */
2011static int __init _init(struct omap_hwmod *oh, void *data)
2012{
2013 int r;
2014
2015 if (oh->_state != _HWMOD_STATE_REGISTERED)
2016 return 0;
2017
2018 _init_mpu_rt_base(oh, NULL);
2019
2020 r = _init_clocks(oh, NULL);
2021 if (IS_ERR_VALUE(r)) {
2022 WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2023 return -EINVAL;
2024 }
2025
2026 oh->_state = _HWMOD_STATE_INITIALIZED;
2027
2028 return 0;
2029}
2030
2031/**
Paul Walmsley64813c32012-04-18 19:10:03 -06002032 * _setup_iclk_autoidle - configure an IP block's interface clocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002033 * @oh: struct omap_hwmod *
2034 *
Paul Walmsley64813c32012-04-18 19:10:03 -06002035 * Set up the module's interface clocks. XXX This function is still mostly
2036 * a stub; implementing this properly requires iclk autoidle usecounting in
2037 * the clock code. No return value.
Paul Walmsley63c85232009-09-03 20:14:03 +03002038 */
Paul Walmsley64813c32012-04-18 19:10:03 -06002039static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002040{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002041 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002042 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002043 int i = 0;
Paul Walmsley381d0332012-04-19 00:58:22 -06002044 if (oh->_state != _HWMOD_STATE_INITIALIZED)
Paul Walmsley64813c32012-04-18 19:10:03 -06002045 return;
Paul Walmsley48d54f32011-02-23 00:14:07 -07002046
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002047 p = oh->slave_ports.next;
Paul Walmsley63c85232009-09-03 20:14:03 +03002048
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002049 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002050 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002051 if (!os->_clk)
Paul Walmsley64813c32012-04-18 19:10:03 -06002052 continue;
Paul Walmsley63c85232009-09-03 20:14:03 +03002053
Paul Walmsley64813c32012-04-18 19:10:03 -06002054 if (os->flags & OCPIF_SWSUP_IDLE) {
2055 /* XXX omap_iclk_deny_idle(c); */
2056 } else {
2057 /* XXX omap_iclk_allow_idle(c); */
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002058 clk_enable(os->_clk);
Paul Walmsley63c85232009-09-03 20:14:03 +03002059 }
2060 }
2061
Paul Walmsley64813c32012-04-18 19:10:03 -06002062 return;
2063}
2064
2065/**
2066 * _setup_reset - reset an IP block during the setup process
2067 * @oh: struct omap_hwmod *
2068 *
2069 * Reset the IP block corresponding to the hwmod @oh during the setup
2070 * process. The IP block is first enabled so it can be successfully
2071 * reset. Returns 0 upon success or a negative error code upon
2072 * failure.
2073 */
2074static int __init _setup_reset(struct omap_hwmod *oh)
2075{
2076 int r;
2077
2078 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2079 return -EINVAL;
Paul Walmsley63c85232009-09-03 20:14:03 +03002080
Paul Walmsley747834a2012-04-18 19:10:04 -06002081 if (oh->rst_lines_cnt == 0) {
2082 r = _enable(oh);
2083 if (r) {
2084 pr_warning("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2085 oh->name, oh->_state);
2086 return -EINVAL;
2087 }
Benoit Cousson9a23dfe2010-05-20 12:31:08 -06002088 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002089
Rajendra Nayak28008522012-03-13 22:55:23 +05302090 if (!(oh->flags & HWMOD_INIT_NO_RESET))
Paul Walmsley64813c32012-04-18 19:10:03 -06002091 r = _reset(oh);
2092
2093 return r;
2094}
2095
2096/**
2097 * _setup_postsetup - transition to the appropriate state after _setup
2098 * @oh: struct omap_hwmod *
2099 *
2100 * Place an IP block represented by @oh into a "post-setup" state --
2101 * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
2102 * this function is called at the end of _setup().) The postsetup
2103 * state for an IP block can be changed by calling
2104 * omap_hwmod_enter_postsetup_state() early in the boot process,
2105 * before one of the omap_hwmod_setup*() functions are called for the
2106 * IP block.
2107 *
2108 * The IP block stays in this state until a PM runtime-based driver is
2109 * loaded for that IP block. A post-setup state of IDLE is
2110 * appropriate for almost all IP blocks with runtime PM-enabled
2111 * drivers, since those drivers are able to enable the IP block. A
2112 * post-setup state of ENABLED is appropriate for kernels with PM
2113 * runtime disabled. The DISABLED state is appropriate for unusual IP
2114 * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2115 * included, since the WDTIMER starts running on reset and will reset
2116 * the MPU if left active.
2117 *
2118 * This post-setup mechanism is deprecated. Once all of the OMAP
2119 * drivers have been converted to use PM runtime, and all of the IP
2120 * block data and interconnect data is available to the hwmod code, it
2121 * should be possible to replace this mechanism with a "lazy reset"
2122 * arrangement. In a "lazy reset" setup, each IP block is enabled
2123 * when the driver first probes, then all remaining IP blocks without
2124 * drivers are either shut down or enabled after the drivers have
2125 * loaded. However, this cannot take place until the above
2126 * preconditions have been met, since otherwise the late reset code
2127 * has no way of knowing which IP blocks are in use by drivers, and
2128 * which ones are unused.
2129 *
2130 * No return value.
2131 */
2132static void __init _setup_postsetup(struct omap_hwmod *oh)
2133{
2134 u8 postsetup_state;
2135
2136 if (oh->rst_lines_cnt > 0)
2137 return;
Benoit Cousson76e55892010-09-21 10:34:11 -06002138
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002139 postsetup_state = oh->_postsetup_state;
2140 if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2141 postsetup_state = _HWMOD_STATE_ENABLED;
2142
2143 /*
2144 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2145 * it should be set by the core code as a runtime flag during startup
2146 */
2147 if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002148 (postsetup_state == _HWMOD_STATE_IDLE)) {
2149 oh->_int_flags |= _HWMOD_SKIP_ENABLE;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002150 postsetup_state = _HWMOD_STATE_ENABLED;
Rajendra Nayakaacf0942011-12-16 05:50:12 -07002151 }
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002152
2153 if (postsetup_state == _HWMOD_STATE_IDLE)
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002154 _idle(oh);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002155 else if (postsetup_state == _HWMOD_STATE_DISABLED)
2156 _shutdown(oh);
2157 else if (postsetup_state != _HWMOD_STATE_ENABLED)
2158 WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2159 oh->name, postsetup_state);
Paul Walmsley63c85232009-09-03 20:14:03 +03002160
Paul Walmsley64813c32012-04-18 19:10:03 -06002161 return;
2162}
2163
2164/**
2165 * _setup - prepare IP block hardware for use
2166 * @oh: struct omap_hwmod *
2167 * @n: (unused, pass NULL)
2168 *
2169 * Configure the IP block represented by @oh. This may include
2170 * enabling the IP block, resetting it, and placing it into a
2171 * post-setup state, depending on the type of IP block and applicable
2172 * flags. IP blocks are reset to prevent any previous configuration
2173 * by the bootloader or previous operating system from interfering
2174 * with power management or other parts of the system. The reset can
2175 * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
2176 * two phases for hwmod initialization. Code called here generally
2177 * affects the IP block hardware, or system integration hardware
2178 * associated with the IP block. Returns 0.
2179 */
2180static int __init _setup(struct omap_hwmod *oh, void *data)
2181{
2182 if (oh->_state != _HWMOD_STATE_INITIALIZED)
2183 return 0;
2184
2185 _setup_iclk_autoidle(oh);
2186
2187 if (!_setup_reset(oh))
2188 _setup_postsetup(oh);
2189
Paul Walmsley63c85232009-09-03 20:14:03 +03002190 return 0;
2191}
2192
Benoit Cousson0102b622010-12-21 21:31:27 -07002193/**
2194 * _register - register a struct omap_hwmod
2195 * @oh: struct omap_hwmod *
2196 *
2197 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
2198 * already has been registered by the same name; -EINVAL if the
2199 * omap_hwmod is in the wrong state, if @oh is NULL, if the
2200 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2201 * name, or if the omap_hwmod's class is missing a name; or 0 upon
2202 * success.
2203 *
2204 * XXX The data should be copied into bootmem, so the original data
2205 * should be marked __initdata and freed after init. This would allow
2206 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
2207 * that the copy process would be relatively complex due to the large number
2208 * of substructures.
2209 */
Benoit Cousson01592df2010-12-21 21:31:28 -07002210static int __init _register(struct omap_hwmod *oh)
Benoit Cousson0102b622010-12-21 21:31:27 -07002211{
Benoit Cousson0102b622010-12-21 21:31:27 -07002212 if (!oh || !oh->name || !oh->class || !oh->class->name ||
2213 (oh->_state != _HWMOD_STATE_UNKNOWN))
2214 return -EINVAL;
2215
Benoit Cousson0102b622010-12-21 21:31:27 -07002216 pr_debug("omap_hwmod: %s: registering\n", oh->name);
2217
Benoit Coussonce35b242010-12-21 21:31:28 -07002218 if (_lookup(oh->name))
2219 return -EEXIST;
Benoit Cousson0102b622010-12-21 21:31:27 -07002220
Benoit Cousson0102b622010-12-21 21:31:27 -07002221 list_add_tail(&oh->node, &omap_hwmod_list);
2222
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002223 INIT_LIST_HEAD(&oh->master_ports);
2224 INIT_LIST_HEAD(&oh->slave_ports);
Benoit Cousson0102b622010-12-21 21:31:27 -07002225 spin_lock_init(&oh->_lock);
2226
2227 oh->_state = _HWMOD_STATE_REGISTERED;
2228
Paul Walmsley569edd702011-02-23 00:14:06 -07002229 /*
2230 * XXX Rather than doing a strcmp(), this should test a flag
2231 * set in the hwmod data, inserted by the autogenerator code.
2232 */
2233 if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2234 mpu_oh = oh;
Benoit Cousson0102b622010-12-21 21:31:27 -07002235
Paul Walmsley569edd702011-02-23 00:14:06 -07002236 return 0;
Benoit Cousson0102b622010-12-21 21:31:27 -07002237}
Paul Walmsley63c85232009-09-03 20:14:03 +03002238
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002239/**
2240 * _alloc_links - return allocated memory for hwmod links
2241 * @ml: pointer to a struct omap_hwmod_link * for the master link
2242 * @sl: pointer to a struct omap_hwmod_link * for the slave link
2243 *
2244 * Return pointers to two struct omap_hwmod_link records, via the
2245 * addresses pointed to by @ml and @sl. Will first attempt to return
2246 * memory allocated as part of a large initial block, but if that has
2247 * been exhausted, will allocate memory itself. Since ideally this
2248 * second allocation path will never occur, the number of these
2249 * 'supplemental' allocations will be logged when debugging is
2250 * enabled. Returns 0.
2251 */
2252static int __init _alloc_links(struct omap_hwmod_link **ml,
2253 struct omap_hwmod_link **sl)
2254{
2255 unsigned int sz;
2256
2257 if ((free_ls + LINKS_PER_OCP_IF) <= max_ls) {
2258 *ml = &linkspace[free_ls++];
2259 *sl = &linkspace[free_ls++];
2260 return 0;
2261 }
2262
2263 sz = sizeof(struct omap_hwmod_link) * LINKS_PER_OCP_IF;
2264
2265 *sl = NULL;
2266 *ml = alloc_bootmem(sz);
2267
2268 memset(*ml, 0, sz);
2269
2270 *sl = (void *)(*ml) + sizeof(struct omap_hwmod_link);
2271
2272 ls_supp++;
2273 pr_debug("omap_hwmod: supplemental link allocations needed: %d\n",
2274 ls_supp * LINKS_PER_OCP_IF);
2275
2276 return 0;
2277};
2278
2279/**
2280 * _add_link - add an interconnect between two IP blocks
2281 * @oi: pointer to a struct omap_hwmod_ocp_if record
2282 *
2283 * Add struct omap_hwmod_link records connecting the master IP block
2284 * specified in @oi->master to @oi, and connecting the slave IP block
2285 * specified in @oi->slave to @oi. This code is assumed to run before
2286 * preemption or SMP has been enabled, thus avoiding the need for
2287 * locking in this code. Changes to this assumption will require
2288 * additional locking. Returns 0.
2289 */
2290static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2291{
2292 struct omap_hwmod_link *ml, *sl;
2293
2294 pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2295 oi->slave->name);
2296
2297 _alloc_links(&ml, &sl);
2298
2299 ml->ocp_if = oi;
2300 INIT_LIST_HEAD(&ml->node);
2301 list_add(&ml->node, &oi->master->master_ports);
2302 oi->master->masters_cnt++;
2303
2304 sl->ocp_if = oi;
2305 INIT_LIST_HEAD(&sl->node);
2306 list_add(&sl->node, &oi->slave->slave_ports);
2307 oi->slave->slaves_cnt++;
2308
2309 return 0;
2310}
2311
2312/**
2313 * _register_link - register a struct omap_hwmod_ocp_if
2314 * @oi: struct omap_hwmod_ocp_if *
2315 *
2316 * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
2317 * has already been registered; -EINVAL if @oi is NULL or if the
2318 * record pointed to by @oi is missing required fields; or 0 upon
2319 * success.
2320 *
2321 * XXX The data should be copied into bootmem, so the original data
2322 * should be marked __initdata and freed after init. This would allow
2323 * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2324 */
2325static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2326{
2327 if (!oi || !oi->master || !oi->slave || !oi->user)
2328 return -EINVAL;
2329
2330 if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2331 return -EEXIST;
2332
2333 pr_debug("omap_hwmod: registering link from %s to %s\n",
2334 oi->master->name, oi->slave->name);
2335
2336 /*
2337 * Register the connected hwmods, if they haven't been
2338 * registered already
2339 */
2340 if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2341 _register(oi->master);
2342
2343 if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2344 _register(oi->slave);
2345
2346 _add_link(oi);
2347
2348 oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2349
2350 return 0;
2351}
2352
2353/**
2354 * _alloc_linkspace - allocate large block of hwmod links
2355 * @ois: pointer to an array of struct omap_hwmod_ocp_if records to count
2356 *
2357 * Allocate a large block of struct omap_hwmod_link records. This
2358 * improves boot time significantly by avoiding the need to allocate
2359 * individual records one by one. If the number of records to
2360 * allocate in the block hasn't been manually specified, this function
2361 * will count the number of struct omap_hwmod_ocp_if records in @ois
2362 * and use that to determine the allocation size. For SoC families
2363 * that require multiple list registrations, such as OMAP3xxx, this
2364 * estimation process isn't optimal, so manual estimation is advised
2365 * in those cases. Returns -EEXIST if the allocation has already occurred
2366 * or 0 upon success.
2367 */
2368static int __init _alloc_linkspace(struct omap_hwmod_ocp_if **ois)
2369{
2370 unsigned int i = 0;
2371 unsigned int sz;
2372
2373 if (linkspace) {
2374 WARN(1, "linkspace already allocated\n");
2375 return -EEXIST;
2376 }
2377
2378 if (max_ls == 0)
2379 while (ois[i++])
2380 max_ls += LINKS_PER_OCP_IF;
2381
2382 sz = sizeof(struct omap_hwmod_link) * max_ls;
2383
2384 pr_debug("omap_hwmod: %s: allocating %d byte linkspace (%d links)\n",
2385 __func__, sz, max_ls);
2386
2387 linkspace = alloc_bootmem(sz);
2388
2389 memset(linkspace, 0, sz);
2390
2391 return 0;
2392}
Paul Walmsley63c85232009-09-03 20:14:03 +03002393
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06002394/* Static functions intended only for use in soc_ops field function pointers */
2395
2396/**
2397 * _omap2_wait_target_ready - wait for a module to leave slave idle
2398 * @oh: struct omap_hwmod *
2399 *
2400 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2401 * does not have an IDLEST bit or if the module successfully leaves
2402 * slave idle; otherwise, pass along the return value of the
2403 * appropriate *_cm*_wait_module_ready() function.
2404 */
2405static int _omap2_wait_target_ready(struct omap_hwmod *oh)
2406{
2407 if (!oh)
2408 return -EINVAL;
2409
2410 if (oh->flags & HWMOD_NO_IDLEST)
2411 return 0;
2412
2413 if (!_find_mpu_rt_port(oh))
2414 return 0;
2415
2416 /* XXX check module SIDLEMODE, hardreset status, enabled clocks */
2417
2418 return omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
2419 oh->prcm.omap2.idlest_reg_id,
2420 oh->prcm.omap2.idlest_idle_bit);
2421}
2422
2423/**
2424 * _omap4_wait_target_ready - wait for a module to leave slave idle
2425 * @oh: struct omap_hwmod *
2426 *
2427 * Wait for a module @oh to leave slave idle. Returns 0 if the module
2428 * does not have an IDLEST bit or if the module successfully leaves
2429 * slave idle; otherwise, pass along the return value of the
2430 * appropriate *_cm*_wait_module_ready() function.
2431 */
2432static int _omap4_wait_target_ready(struct omap_hwmod *oh)
2433{
2434 if (!oh || !oh->clkdm)
2435 return -EINVAL;
2436
2437 if (oh->flags & HWMOD_NO_IDLEST)
2438 return 0;
2439
2440 if (!_find_mpu_rt_port(oh))
2441 return 0;
2442
2443 /* XXX check module SIDLEMODE, hardreset status */
2444
2445 return omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
2446 oh->clkdm->cm_inst,
2447 oh->clkdm->clkdm_offs,
2448 oh->prcm.omap4.clkctrl_offs);
2449}
2450
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06002451/**
2452 * _omap2_assert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2453 * @oh: struct omap_hwmod * to assert hardreset
2454 * @ohri: hardreset line data
2455 *
2456 * Call omap2_prm_assert_hardreset() with parameters extracted from
2457 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2458 * use as an soc_ops function pointer. Passes along the return value
2459 * from omap2_prm_assert_hardreset(). XXX This function is scheduled
2460 * for removal when the PRM code is moved into drivers/.
2461 */
2462static int _omap2_assert_hardreset(struct omap_hwmod *oh,
2463 struct omap_hwmod_rst_info *ohri)
2464{
2465 return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
2466 ohri->rst_shift);
2467}
2468
2469/**
2470 * _omap2_deassert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2471 * @oh: struct omap_hwmod * to deassert hardreset
2472 * @ohri: hardreset line data
2473 *
2474 * Call omap2_prm_deassert_hardreset() with parameters extracted from
2475 * the hwmod @oh and the hardreset line data @ohri. Only intended for
2476 * use as an soc_ops function pointer. Passes along the return value
2477 * from omap2_prm_deassert_hardreset(). XXX This function is
2478 * scheduled for removal when the PRM code is moved into drivers/.
2479 */
2480static int _omap2_deassert_hardreset(struct omap_hwmod *oh,
2481 struct omap_hwmod_rst_info *ohri)
2482{
2483 return omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
2484 ohri->rst_shift,
2485 ohri->st_shift);
2486}
2487
2488/**
2489 * _omap2_is_hardreset_asserted - call OMAP2 PRM hardreset fn with hwmod args
2490 * @oh: struct omap_hwmod * to test hardreset
2491 * @ohri: hardreset line data
2492 *
2493 * Call omap2_prm_is_hardreset_asserted() with parameters extracted
2494 * from the hwmod @oh and the hardreset line data @ohri. Only
2495 * intended for use as an soc_ops function pointer. Passes along the
2496 * return value from omap2_prm_is_hardreset_asserted(). XXX This
2497 * function is scheduled for removal when the PRM code is moved into
2498 * drivers/.
2499 */
2500static int _omap2_is_hardreset_asserted(struct omap_hwmod *oh,
2501 struct omap_hwmod_rst_info *ohri)
2502{
2503 return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
2504 ohri->st_shift);
2505}
2506
2507/**
2508 * _omap4_assert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2509 * @oh: struct omap_hwmod * to assert hardreset
2510 * @ohri: hardreset line data
2511 *
2512 * Call omap4_prminst_assert_hardreset() with parameters extracted
2513 * from the hwmod @oh and the hardreset line data @ohri. Only
2514 * intended for use as an soc_ops function pointer. Passes along the
2515 * return value from omap4_prminst_assert_hardreset(). XXX This
2516 * function is scheduled for removal when the PRM code is moved into
2517 * drivers/.
2518 */
2519static int _omap4_assert_hardreset(struct omap_hwmod *oh,
2520 struct omap_hwmod_rst_info *ohri)
2521
2522{
2523 return omap4_prminst_assert_hardreset(ohri->rst_shift,
2524 oh->clkdm->pwrdm.ptr->prcm_partition,
2525 oh->clkdm->pwrdm.ptr->prcm_offs,
2526 oh->prcm.omap4.rstctrl_offs);
2527}
2528
2529/**
2530 * _omap4_deassert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
2531 * @oh: struct omap_hwmod * to deassert hardreset
2532 * @ohri: hardreset line data
2533 *
2534 * Call omap4_prminst_deassert_hardreset() with parameters extracted
2535 * from the hwmod @oh and the hardreset line data @ohri. Only
2536 * intended for use as an soc_ops function pointer. Passes along the
2537 * return value from omap4_prminst_deassert_hardreset(). XXX This
2538 * function is scheduled for removal when the PRM code is moved into
2539 * drivers/.
2540 */
2541static int _omap4_deassert_hardreset(struct omap_hwmod *oh,
2542 struct omap_hwmod_rst_info *ohri)
2543{
2544 if (ohri->st_shift)
2545 pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
2546 oh->name, ohri->name);
2547 return omap4_prminst_deassert_hardreset(ohri->rst_shift,
2548 oh->clkdm->pwrdm.ptr->prcm_partition,
2549 oh->clkdm->pwrdm.ptr->prcm_offs,
2550 oh->prcm.omap4.rstctrl_offs);
2551}
2552
2553/**
2554 * _omap4_is_hardreset_asserted - call OMAP4 PRM hardreset fn with hwmod args
2555 * @oh: struct omap_hwmod * to test hardreset
2556 * @ohri: hardreset line data
2557 *
2558 * Call omap4_prminst_is_hardreset_asserted() with parameters
2559 * extracted from the hwmod @oh and the hardreset line data @ohri.
2560 * Only intended for use as an soc_ops function pointer. Passes along
2561 * the return value from omap4_prminst_is_hardreset_asserted(). XXX
2562 * This function is scheduled for removal when the PRM code is moved
2563 * into drivers/.
2564 */
2565static int _omap4_is_hardreset_asserted(struct omap_hwmod *oh,
2566 struct omap_hwmod_rst_info *ohri)
2567{
2568 return omap4_prminst_is_hardreset_asserted(ohri->rst_shift,
2569 oh->clkdm->pwrdm.ptr->prcm_partition,
2570 oh->clkdm->pwrdm.ptr->prcm_offs,
2571 oh->prcm.omap4.rstctrl_offs);
2572}
2573
Paul Walmsley63c85232009-09-03 20:14:03 +03002574/* Public functions */
2575
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002576u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002577{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002578 if (oh->flags & HWMOD_16BIT_REG)
2579 return __raw_readw(oh->_mpu_rt_va + reg_offs);
2580 else
2581 return __raw_readl(oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002582}
2583
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002584void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
Paul Walmsley63c85232009-09-03 20:14:03 +03002585{
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002586 if (oh->flags & HWMOD_16BIT_REG)
2587 __raw_writew(v, oh->_mpu_rt_va + reg_offs);
2588 else
2589 __raw_writel(v, oh->_mpu_rt_va + reg_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002590}
2591
Paul Walmsley887adea2010-07-26 16:34:33 -06002592/**
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002593 * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
2594 * @oh: struct omap_hwmod *
2595 *
2596 * This is a public function exposed to drivers. Some drivers may need to do
2597 * some settings before and after resetting the device. Those drivers after
2598 * doing the necessary settings could use this function to start a reset by
2599 * setting the SYSCONFIG.SOFTRESET bit.
2600 */
2601int omap_hwmod_softreset(struct omap_hwmod *oh)
2602{
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002603 u32 v;
2604 int ret;
2605
2606 if (!oh || !(oh->_sysc_cache))
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002607 return -EINVAL;
2608
Paul Walmsley3c55c1b2012-04-13 05:08:43 -06002609 v = oh->_sysc_cache;
2610 ret = _set_softreset(oh, &v);
2611 if (ret)
2612 goto error;
2613 _write_sysconfig(v, oh);
2614
2615error:
2616 return ret;
Avinash.H.M6d3c55f2011-07-10 05:27:16 -06002617}
2618
2619/**
Paul Walmsley887adea2010-07-26 16:34:33 -06002620 * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
2621 * @oh: struct omap_hwmod *
2622 * @idlemode: SIDLEMODE field bits (shifted to bit 0)
2623 *
2624 * Sets the IP block's OCP slave idlemode in hardware, and updates our
2625 * local copy. Intended to be used by drivers that have some erratum
2626 * that requires direct manipulation of the SIDLEMODE bits. Returns
2627 * -EINVAL if @oh is null, or passes along the return value from
2628 * _set_slave_idlemode().
2629 *
2630 * XXX Does this function have any current users? If not, we should
2631 * remove it; it is better to let the rest of the hwmod code handle this.
2632 * Any users of this function should be scrutinized carefully.
2633 */
Kevin Hilman46273e62010-01-26 20:13:03 -07002634int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
2635{
2636 u32 v;
2637 int retval = 0;
2638
2639 if (!oh)
2640 return -EINVAL;
2641
2642 v = oh->_sysc_cache;
2643
2644 retval = _set_slave_idlemode(oh, idlemode, &v);
2645 if (!retval)
2646 _write_sysconfig(v, oh);
2647
2648 return retval;
2649}
2650
Paul Walmsley63c85232009-09-03 20:14:03 +03002651/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002652 * omap_hwmod_lookup - look up a registered omap_hwmod by name
2653 * @name: name of the omap_hwmod to look up
2654 *
2655 * Given a @name of an omap_hwmod, return a pointer to the registered
2656 * struct omap_hwmod *, or NULL upon error.
2657 */
2658struct omap_hwmod *omap_hwmod_lookup(const char *name)
2659{
2660 struct omap_hwmod *oh;
2661
2662 if (!name)
2663 return NULL;
2664
Paul Walmsley63c85232009-09-03 20:14:03 +03002665 oh = _lookup(name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002666
2667 return oh;
2668}
2669
2670/**
2671 * omap_hwmod_for_each - call function for each registered omap_hwmod
2672 * @fn: pointer to a callback function
Paul Walmsley97d60162010-07-26 16:34:30 -06002673 * @data: void * data to pass to callback function
Paul Walmsley63c85232009-09-03 20:14:03 +03002674 *
2675 * Call @fn for each registered omap_hwmod, passing @data to each
2676 * function. @fn must return 0 for success or any other value for
2677 * failure. If @fn returns non-zero, the iteration across omap_hwmods
2678 * will stop and the non-zero return value will be passed to the
2679 * caller of omap_hwmod_for_each(). @fn is called with
2680 * omap_hwmod_for_each() held.
2681 */
Paul Walmsley97d60162010-07-26 16:34:30 -06002682int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
2683 void *data)
Paul Walmsley63c85232009-09-03 20:14:03 +03002684{
2685 struct omap_hwmod *temp_oh;
Govindraj.R30ebad92011-06-01 11:28:56 +05302686 int ret = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002687
2688 if (!fn)
2689 return -EINVAL;
2690
Paul Walmsley63c85232009-09-03 20:14:03 +03002691 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
Paul Walmsley97d60162010-07-26 16:34:30 -06002692 ret = (*fn)(temp_oh, data);
Paul Walmsley63c85232009-09-03 20:14:03 +03002693 if (ret)
2694 break;
2695 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002696
2697 return ret;
2698}
2699
Paul Walmsley63c85232009-09-03 20:14:03 +03002700/**
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002701 * omap_hwmod_register_links - register an array of hwmod links
2702 * @ois: pointer to an array of omap_hwmod_ocp_if to register
2703 *
2704 * Intended to be called early in boot before the clock framework is
2705 * initialized. If @ois is not null, will register all omap_hwmods
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002706 * listed in @ois that are valid for this chip. Returns -EINVAL if
2707 * omap_hwmod_init() hasn't been called before calling this function,
2708 * -ENOMEM if the link memory area can't be allocated, or 0 upon
2709 * success.
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002710 */
2711int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
2712{
2713 int r, i;
2714
Kevin Hilman9ebfd282012-06-18 12:12:23 -06002715 if (!inited)
2716 return -EINVAL;
2717
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002718 if (!ois)
2719 return 0;
2720
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002721 if (!linkspace) {
2722 if (_alloc_linkspace(ois)) {
2723 pr_err("omap_hwmod: could not allocate link space\n");
2724 return -ENOMEM;
2725 }
2726 }
2727
2728 i = 0;
2729 do {
2730 r = _register_link(ois[i]);
2731 WARN(r && r != -EEXIST,
2732 "omap_hwmod: _register_link(%s -> %s) returned %d\n",
2733 ois[i]->master->name, ois[i]->slave->name, r);
2734 } while (ois[++i]);
2735
2736 return 0;
2737}
2738
2739/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002740 * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
2741 * @oh: pointer to the hwmod currently being set up (usually not the MPU)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002742 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002743 * If the hwmod data corresponding to the MPU subsystem IP block
2744 * hasn't been initialized and set up yet, do so now. This must be
2745 * done first since sleep dependencies may be added from other hwmods
2746 * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
2747 * return value.
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002748 */
Paul Walmsley381d0332012-04-19 00:58:22 -06002749static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
Tony Lindgrene7c7d762011-02-14 15:40:21 -08002750{
Paul Walmsley381d0332012-04-19 00:58:22 -06002751 if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
2752 pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
2753 __func__, MPU_INITIATOR_NAME);
2754 else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
2755 omap_hwmod_setup_one(MPU_INITIATOR_NAME);
Paul Walmsley63c85232009-09-03 20:14:03 +03002756}
2757
2758/**
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002759 * omap_hwmod_setup_one - set up a single hwmod
2760 * @oh_name: const char * name of the already-registered hwmod to set up
2761 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002762 * Initialize and set up a single hwmod. Intended to be used for a
2763 * small number of early devices, such as the timer IP blocks used for
2764 * the scheduler clock. Must be called after omap2_clk_init().
2765 * Resolves the struct clk names to struct clk pointers for each
2766 * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
2767 * -EINVAL upon error or 0 upon success.
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002768 */
2769int __init omap_hwmod_setup_one(const char *oh_name)
2770{
2771 struct omap_hwmod *oh;
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002772
2773 pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
2774
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002775 oh = _lookup(oh_name);
2776 if (!oh) {
2777 WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
2778 return -EINVAL;
2779 }
2780
Paul Walmsley381d0332012-04-19 00:58:22 -06002781 _ensure_mpu_hwmod_is_setup(oh);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002782
Paul Walmsley381d0332012-04-19 00:58:22 -06002783 _init(oh, NULL);
Paul Walmsleya2debdb2011-02-23 00:14:07 -07002784 _setup(oh, NULL);
2785
2786 return 0;
2787}
2788
2789/**
Paul Walmsley381d0332012-04-19 00:58:22 -06002790 * omap_hwmod_setup_all - set up all registered IP blocks
Paul Walmsley63c85232009-09-03 20:14:03 +03002791 *
Paul Walmsley381d0332012-04-19 00:58:22 -06002792 * Initialize and set up all IP blocks registered with the hwmod code.
2793 * Must be called after omap2_clk_init(). Resolves the struct clk
2794 * names to struct clk pointers for each registered omap_hwmod. Also
2795 * calls _setup() on each hwmod. Returns 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03002796 */
Paul Walmsley550c8092011-02-28 11:58:14 -07002797static int __init omap_hwmod_setup_all(void)
Paul Walmsley63c85232009-09-03 20:14:03 +03002798{
Paul Walmsley381d0332012-04-19 00:58:22 -06002799 _ensure_mpu_hwmod_is_setup(NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002800
Paul Walmsley381d0332012-04-19 00:58:22 -06002801 omap_hwmod_for_each(_init, NULL);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07002802 omap_hwmod_for_each(_setup, NULL);
Paul Walmsley63c85232009-09-03 20:14:03 +03002803
2804 return 0;
2805}
Paul Walmsley550c8092011-02-28 11:58:14 -07002806core_initcall(omap_hwmod_setup_all);
Paul Walmsley63c85232009-09-03 20:14:03 +03002807
2808/**
Paul Walmsley63c85232009-09-03 20:14:03 +03002809 * omap_hwmod_enable - enable an omap_hwmod
2810 * @oh: struct omap_hwmod *
2811 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002812 * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
Paul Walmsley63c85232009-09-03 20:14:03 +03002813 * Returns -EINVAL on error or passes along the return value from _enable().
2814 */
2815int omap_hwmod_enable(struct omap_hwmod *oh)
2816{
2817 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002818 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002819
2820 if (!oh)
2821 return -EINVAL;
2822
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002823 spin_lock_irqsave(&oh->_lock, flags);
2824 r = _enable(oh);
2825 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002826
2827 return r;
2828}
2829
2830/**
2831 * omap_hwmod_idle - idle an omap_hwmod
2832 * @oh: struct omap_hwmod *
2833 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002834 * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
Paul Walmsley63c85232009-09-03 20:14:03 +03002835 * Returns -EINVAL on error or passes along the return value from _idle().
2836 */
2837int omap_hwmod_idle(struct omap_hwmod *oh)
2838{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002839 unsigned long flags;
2840
Paul Walmsley63c85232009-09-03 20:14:03 +03002841 if (!oh)
2842 return -EINVAL;
2843
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002844 spin_lock_irqsave(&oh->_lock, flags);
2845 _idle(oh);
2846 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002847
2848 return 0;
2849}
2850
2851/**
2852 * omap_hwmod_shutdown - shutdown an omap_hwmod
2853 * @oh: struct omap_hwmod *
2854 *
Paul Walmsley74ff3a62010-09-21 15:02:23 -06002855 * Shutdown an omap_hwmod @oh. Intended to be called by
Paul Walmsley63c85232009-09-03 20:14:03 +03002856 * omap_device_shutdown(). Returns -EINVAL on error or passes along
2857 * the return value from _shutdown().
2858 */
2859int omap_hwmod_shutdown(struct omap_hwmod *oh)
2860{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002861 unsigned long flags;
2862
Paul Walmsley63c85232009-09-03 20:14:03 +03002863 if (!oh)
2864 return -EINVAL;
2865
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002866 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002867 _shutdown(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002868 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002869
2870 return 0;
2871}
2872
2873/**
2874 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
2875 * @oh: struct omap_hwmod *oh
2876 *
2877 * Intended to be called by the omap_device code.
2878 */
2879int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
2880{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002881 unsigned long flags;
2882
2883 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002884 _enable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002885 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002886
2887 return 0;
2888}
2889
2890/**
2891 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
2892 * @oh: struct omap_hwmod *oh
2893 *
2894 * Intended to be called by the omap_device code.
2895 */
2896int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
2897{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002898 unsigned long flags;
2899
2900 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002901 _disable_clocks(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002902 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002903
2904 return 0;
2905}
2906
2907/**
2908 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
2909 * @oh: struct omap_hwmod *oh
2910 *
2911 * Intended to be called by drivers and core code when all posted
2912 * writes to a device must complete before continuing further
2913 * execution (for example, after clearing some device IRQSTATUS
2914 * register bits)
2915 *
2916 * XXX what about targets with multiple OCP threads?
2917 */
2918void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
2919{
2920 BUG_ON(!oh);
2921
Paul Walmsley43b40992010-02-22 22:09:34 -07002922 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
Russell King4f8a4282012-02-07 10:59:37 +00002923 WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
2924 oh->name);
Paul Walmsley63c85232009-09-03 20:14:03 +03002925 return;
2926 }
2927
2928 /*
2929 * Forces posted writes to complete on the OCP thread handling
2930 * register writes
2931 */
Rajendra Nayakcc7a1d22010-10-08 10:23:22 -07002932 omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
Paul Walmsley63c85232009-09-03 20:14:03 +03002933}
2934
2935/**
2936 * omap_hwmod_reset - reset the hwmod
2937 * @oh: struct omap_hwmod *
2938 *
2939 * Under some conditions, a driver may wish to reset the entire device.
2940 * Called from omap_device code. Returns -EINVAL on error or passes along
Liam Girdwood9b579112010-09-21 10:34:09 -06002941 * the return value from _reset().
Paul Walmsley63c85232009-09-03 20:14:03 +03002942 */
2943int omap_hwmod_reset(struct omap_hwmod *oh)
2944{
2945 int r;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002946 unsigned long flags;
Paul Walmsley63c85232009-09-03 20:14:03 +03002947
Liam Girdwood9b579112010-09-21 10:34:09 -06002948 if (!oh)
Paul Walmsley63c85232009-09-03 20:14:03 +03002949 return -EINVAL;
2950
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002951 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002952 r = _reset(oh);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07002953 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03002954
2955 return r;
2956}
2957
Paul Walmsley5e8370f2012-04-18 19:10:06 -06002958/*
2959 * IP block data retrieval functions
2960 */
2961
Paul Walmsley63c85232009-09-03 20:14:03 +03002962/**
2963 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
2964 * @oh: struct omap_hwmod *
2965 * @res: pointer to the first element of an array of struct resource to fill
2966 *
2967 * Count the number of struct resource array elements necessary to
2968 * contain omap_hwmod @oh resources. Intended to be called by code
2969 * that registers omap_devices. Intended to be used to determine the
2970 * size of a dynamically-allocated struct resource array, before
2971 * calling omap_hwmod_fill_resources(). Returns the number of struct
2972 * resource array elements needed.
2973 *
2974 * XXX This code is not optimized. It could attempt to merge adjacent
2975 * resource IDs.
2976 *
2977 */
2978int omap_hwmod_count_resources(struct omap_hwmod *oh)
2979{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002980 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002981 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002982 int ret;
2983 int i = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +03002984
Paul Walmsleybc614952011-07-09 19:14:07 -06002985 ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
Paul Walmsley63c85232009-09-03 20:14:03 +03002986
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002987 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06002988
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002989 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06002990 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley5d95dde2012-04-19 04:04:28 -06002991 ret += _count_ocp_if_addr_spaces(os);
2992 }
Paul Walmsley63c85232009-09-03 20:14:03 +03002993
2994 return ret;
2995}
2996
2997/**
2998 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
2999 * @oh: struct omap_hwmod *
3000 * @res: pointer to the first element of an array of struct resource to fill
3001 *
3002 * Fill the struct resource array @res with resource data from the
3003 * omap_hwmod @oh. Intended to be called by code that registers
3004 * omap_devices. See also omap_hwmod_count_resources(). Returns the
3005 * number of array elements filled.
3006 */
3007int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
3008{
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003009 struct omap_hwmod_ocp_if *os;
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003010 struct list_head *p;
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003011 int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
Paul Walmsley63c85232009-09-03 20:14:03 +03003012 int r = 0;
3013
3014 /* For each IRQ, DMA, memory area, fill in array.*/
3015
Paul Walmsley212738a2011-07-09 19:14:06 -06003016 mpu_irqs_cnt = _count_mpu_irqs(oh);
3017 for (i = 0; i < mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07003018 (res + r)->name = (oh->mpu_irqs + i)->name;
3019 (res + r)->start = (oh->mpu_irqs + i)->irq;
3020 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03003021 (res + r)->flags = IORESOURCE_IRQ;
3022 r++;
3023 }
3024
Paul Walmsleybc614952011-07-09 19:14:07 -06003025 sdma_reqs_cnt = _count_sdma_reqs(oh);
3026 for (i = 0; i < sdma_reqs_cnt; i++) {
Benoit Cousson9ee9fff2010-09-21 10:34:08 -06003027 (res + r)->name = (oh->sdma_reqs + i)->name;
3028 (res + r)->start = (oh->sdma_reqs + i)->dma_req;
3029 (res + r)->end = (oh->sdma_reqs + i)->dma_req;
Paul Walmsley63c85232009-09-03 20:14:03 +03003030 (res + r)->flags = IORESOURCE_DMA;
3031 r++;
3032 }
3033
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003034 p = oh->slave_ports.next;
Paul Walmsley2221b5c2012-04-19 04:04:30 -06003035
Paul Walmsley5d95dde2012-04-19 04:04:28 -06003036 i = 0;
3037 while (i < oh->slaves_cnt) {
Paul Walmsley11cd4b92012-04-19 04:04:32 -06003038 os = _fetch_next_ocp_if(&p, &i);
Paul Walmsley78183f32011-07-09 19:14:05 -06003039 addr_cnt = _count_ocp_if_addr_spaces(os);
Paul Walmsley63c85232009-09-03 20:14:03 +03003040
Paul Walmsley78183f32011-07-09 19:14:05 -06003041 for (j = 0; j < addr_cnt; j++) {
Kishon Vijay Abraham Icd503802011-02-24 12:51:45 -08003042 (res + r)->name = (os->addr + j)->name;
Paul Walmsley63c85232009-09-03 20:14:03 +03003043 (res + r)->start = (os->addr + j)->pa_start;
3044 (res + r)->end = (os->addr + j)->pa_end;
3045 (res + r)->flags = IORESOURCE_MEM;
3046 r++;
3047 }
3048 }
3049
3050 return r;
3051}
3052
3053/**
Paul Walmsley5e8370f2012-04-18 19:10:06 -06003054 * omap_hwmod_get_resource_byname - fetch IP block integration data by name
3055 * @oh: struct omap_hwmod * to operate on
3056 * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
3057 * @name: pointer to the name of the data to fetch (optional)
3058 * @rsrc: pointer to a struct resource, allocated by the caller
3059 *
3060 * Retrieve MPU IRQ, SDMA request line, or address space start/end
3061 * data for the IP block pointed to by @oh. The data will be filled
3062 * into a struct resource record pointed to by @rsrc. The struct
3063 * resource must be allocated by the caller. When @name is non-null,
3064 * the data associated with the matching entry in the IRQ/SDMA/address
3065 * space hwmod data arrays will be returned. If @name is null, the
3066 * first array entry will be returned. Data order is not meaningful
3067 * in hwmod data, so callers are strongly encouraged to use a non-null
3068 * @name whenever possible to avoid unpredictable effects if hwmod
3069 * data is later added that causes data ordering to change. This
3070 * function is only intended for use by OMAP core code. Device
3071 * drivers should not call this function - the appropriate bus-related
3072 * data accessor functions should be used instead. Returns 0 upon
3073 * success or a negative error code upon error.
3074 */
3075int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
3076 const char *name, struct resource *rsrc)
3077{
3078 int r;
3079 unsigned int irq, dma;
3080 u32 pa_start, pa_end;
3081
3082 if (!oh || !rsrc)
3083 return -EINVAL;
3084
3085 if (type == IORESOURCE_IRQ) {
3086 r = _get_mpu_irq_by_name(oh, name, &irq);
3087 if (r)
3088 return r;
3089
3090 rsrc->start = irq;
3091 rsrc->end = irq;
3092 } else if (type == IORESOURCE_DMA) {
3093 r = _get_sdma_req_by_name(oh, name, &dma);
3094 if (r)
3095 return r;
3096
3097 rsrc->start = dma;
3098 rsrc->end = dma;
3099 } else if (type == IORESOURCE_MEM) {
3100 r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
3101 if (r)
3102 return r;
3103
3104 rsrc->start = pa_start;
3105 rsrc->end = pa_end;
3106 } else {
3107 return -EINVAL;
3108 }
3109
3110 rsrc->flags = type;
3111 rsrc->name = name;
3112
3113 return 0;
3114}
3115
3116/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003117 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
3118 * @oh: struct omap_hwmod *
3119 *
3120 * Return the powerdomain pointer associated with the OMAP module
3121 * @oh's main clock. If @oh does not have a main clk, return the
3122 * powerdomain associated with the interface clock associated with the
3123 * module's MPU port. (XXX Perhaps this should use the SDMA port
3124 * instead?) Returns NULL on error, or a struct powerdomain * on
3125 * success.
3126 */
3127struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
3128{
3129 struct clk *c;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003130 struct omap_hwmod_ocp_if *oi;
Paul Walmsley63c85232009-09-03 20:14:03 +03003131
3132 if (!oh)
3133 return NULL;
3134
3135 if (oh->_clk) {
3136 c = oh->_clk;
3137 } else {
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003138 oi = _find_mpu_rt_port(oh);
3139 if (!oi)
Paul Walmsley63c85232009-09-03 20:14:03 +03003140 return NULL;
Paul Walmsley2d6141b2012-04-19 04:04:27 -06003141 c = oi->_clk;
Paul Walmsley63c85232009-09-03 20:14:03 +03003142 }
3143
Thara Gopinathd5647c12010-03-31 04:16:29 -06003144 if (!c->clkdm)
3145 return NULL;
3146
Paul Walmsley63c85232009-09-03 20:14:03 +03003147 return c->clkdm->pwrdm.ptr;
3148
3149}
3150
3151/**
Paul Walmsleydb2a60b2010-07-26 16:34:33 -06003152 * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3153 * @oh: struct omap_hwmod *
3154 *
3155 * Returns the virtual address corresponding to the beginning of the
3156 * module's register target, in the address range that is intended to
3157 * be used by the MPU. Returns the virtual address upon success or NULL
3158 * upon error.
3159 */
3160void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3161{
3162 if (!oh)
3163 return NULL;
3164
3165 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3166 return NULL;
3167
3168 if (oh->_state == _HWMOD_STATE_UNKNOWN)
3169 return NULL;
3170
3171 return oh->_mpu_rt_va;
3172}
3173
3174/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003175 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
3176 * @oh: struct omap_hwmod *
3177 * @init_oh: struct omap_hwmod * (initiator)
3178 *
3179 * Add a sleep dependency between the initiator @init_oh and @oh.
3180 * Intended to be called by DSP/Bridge code via platform_data for the
3181 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3182 * code needs to add/del initiator dependencies dynamically
3183 * before/after accessing a device. Returns the return value from
3184 * _add_initiator_dep().
3185 *
3186 * XXX Keep a usecount in the clockdomain code
3187 */
3188int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
3189 struct omap_hwmod *init_oh)
3190{
3191 return _add_initiator_dep(oh, init_oh);
3192}
3193
3194/*
3195 * XXX what about functions for drivers to save/restore ocp_sysconfig
3196 * for context save/restore operations?
3197 */
3198
3199/**
3200 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
3201 * @oh: struct omap_hwmod *
3202 * @init_oh: struct omap_hwmod * (initiator)
3203 *
3204 * Remove a sleep dependency between the initiator @init_oh and @oh.
3205 * Intended to be called by DSP/Bridge code via platform_data for the
3206 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
3207 * code needs to add/del initiator dependencies dynamically
3208 * before/after accessing a device. Returns the return value from
3209 * _del_initiator_dep().
3210 *
3211 * XXX Keep a usecount in the clockdomain code
3212 */
3213int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
3214 struct omap_hwmod *init_oh)
3215{
3216 return _del_initiator_dep(oh, init_oh);
3217}
3218
3219/**
Paul Walmsley63c85232009-09-03 20:14:03 +03003220 * omap_hwmod_enable_wakeup - allow device to wake up the system
3221 * @oh: struct omap_hwmod *
3222 *
3223 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
Govindraj.R2a1cc142012-04-05 02:59:32 -06003224 * send wakeups to the PRCM, and enable I/O ring wakeup events for
3225 * this IP block if it has dynamic mux entries. Eventually this
3226 * should set PRCM wakeup registers to cause the PRCM to receive
3227 * wakeup events from the module. Does not set any wakeup routing
3228 * registers beyond this point - if the module is to wake up any other
3229 * module or subsystem, that must be set separately. Called by
3230 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003231 */
3232int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3233{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003234 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003235 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003236
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003237 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003238
3239 if (oh->class->sysc &&
3240 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3241 v = oh->_sysc_cache;
3242 _enable_wakeup(oh, &v);
3243 _write_sysconfig(v, oh);
3244 }
3245
Govindraj Receec002011-12-16 14:36:58 -07003246 _set_idle_ioring_wakeup(oh, true);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003247 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003248
3249 return 0;
3250}
3251
3252/**
3253 * omap_hwmod_disable_wakeup - prevent device from waking the system
3254 * @oh: struct omap_hwmod *
3255 *
3256 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
Govindraj.R2a1cc142012-04-05 02:59:32 -06003257 * from sending wakeups to the PRCM, and disable I/O ring wakeup
3258 * events for this IP block if it has dynamic mux entries. Eventually
3259 * this should clear PRCM wakeup registers to cause the PRCM to ignore
3260 * wakeup events from the module. Does not set any wakeup routing
3261 * registers beyond this point - if the module is to wake up any other
3262 * module or subsystem, that must be set separately. Called by
3263 * omap_device code. Returns -EINVAL on error or 0 upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +03003264 */
3265int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3266{
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003267 unsigned long flags;
Kevin Hilman5a7ddcb2010-12-21 21:08:34 -07003268 u32 v;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003269
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003270 spin_lock_irqsave(&oh->_lock, flags);
Govindraj.R2a1cc142012-04-05 02:59:32 -06003271
3272 if (oh->class->sysc &&
3273 (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3274 v = oh->_sysc_cache;
3275 _disable_wakeup(oh, &v);
3276 _write_sysconfig(v, oh);
3277 }
3278
Govindraj Receec002011-12-16 14:36:58 -07003279 _set_idle_ioring_wakeup(oh, false);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003280 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley63c85232009-09-03 20:14:03 +03003281
3282 return 0;
3283}
Paul Walmsley43b40992010-02-22 22:09:34 -07003284
3285/**
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003286 * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3287 * contained in the hwmod module.
3288 * @oh: struct omap_hwmod *
3289 * @name: name of the reset line to lookup and assert
3290 *
3291 * Some IP like dsp, ipu or iva contain processor that require
3292 * an HW reset line to be assert / deassert in order to enable fully
3293 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3294 * yet supported on this OMAP; otherwise, passes along the return value
3295 * from _assert_hardreset().
3296 */
3297int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3298{
3299 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003300 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003301
3302 if (!oh)
3303 return -EINVAL;
3304
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003305 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003306 ret = _assert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003307 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003308
3309 return ret;
3310}
3311
3312/**
3313 * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3314 * contained in the hwmod module.
3315 * @oh: struct omap_hwmod *
3316 * @name: name of the reset line to look up and deassert
3317 *
3318 * Some IP like dsp, ipu or iva contain processor that require
3319 * an HW reset line to be assert / deassert in order to enable fully
3320 * the IP. Returns -EINVAL if @oh is null or if the operation is not
3321 * yet supported on this OMAP; otherwise, passes along the return value
3322 * from _deassert_hardreset().
3323 */
3324int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3325{
3326 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003327 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003328
3329 if (!oh)
3330 return -EINVAL;
3331
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003332 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003333 ret = _deassert_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003334 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003335
3336 return ret;
3337}
3338
3339/**
3340 * omap_hwmod_read_hardreset - read the HW reset line state of submodules
3341 * contained in the hwmod module
3342 * @oh: struct omap_hwmod *
3343 * @name: name of the reset line to look up and read
3344 *
3345 * Return the current state of the hwmod @oh's reset line named @name:
3346 * returns -EINVAL upon parameter error or if this operation
3347 * is unsupported on the current OMAP; otherwise, passes along the return
3348 * value from _read_hardreset().
3349 */
3350int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
3351{
3352 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003353 unsigned long flags;
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003354
3355 if (!oh)
3356 return -EINVAL;
3357
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003358 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003359 ret = _read_hardreset(oh, name);
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003360 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsleyaee48e32010-09-21 10:34:11 -06003361
3362 return ret;
3363}
3364
3365
3366/**
Paul Walmsley43b40992010-02-22 22:09:34 -07003367 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3368 * @classname: struct omap_hwmod_class name to search for
3369 * @fn: callback function pointer to call for each hwmod in class @classname
3370 * @user: arbitrary context data to pass to the callback function
3371 *
Benoit Coussonce35b242010-12-21 21:31:28 -07003372 * For each omap_hwmod of class @classname, call @fn.
3373 * If the callback function returns something other than
Paul Walmsley43b40992010-02-22 22:09:34 -07003374 * zero, the iterator is terminated, and the callback function's return
3375 * value is passed back to the caller. Returns 0 upon success, -EINVAL
3376 * if @classname or @fn are NULL, or passes back the error code from @fn.
3377 */
3378int omap_hwmod_for_each_by_class(const char *classname,
3379 int (*fn)(struct omap_hwmod *oh,
3380 void *user),
3381 void *user)
3382{
3383 struct omap_hwmod *temp_oh;
3384 int ret = 0;
3385
3386 if (!classname || !fn)
3387 return -EINVAL;
3388
3389 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3390 __func__, classname);
3391
Paul Walmsley43b40992010-02-22 22:09:34 -07003392 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3393 if (!strcmp(temp_oh->class->name, classname)) {
3394 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3395 __func__, temp_oh->name);
3396 ret = (*fn)(temp_oh, user);
3397 if (ret)
3398 break;
3399 }
3400 }
3401
Paul Walmsley43b40992010-02-22 22:09:34 -07003402 if (ret)
3403 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3404 __func__, ret);
3405
3406 return ret;
3407}
3408
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003409/**
3410 * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3411 * @oh: struct omap_hwmod *
3412 * @state: state that _setup() should leave the hwmod in
3413 *
Paul Walmsley550c8092011-02-28 11:58:14 -07003414 * Sets the hwmod state that @oh will enter at the end of _setup()
Paul Walmsley64813c32012-04-18 19:10:03 -06003415 * (called by omap_hwmod_setup_*()). See also the documentation
3416 * for _setup_postsetup(), above. Returns 0 upon success or
3417 * -EINVAL if there is a problem with the arguments or if the hwmod is
3418 * in the wrong state.
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003419 */
3420int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3421{
3422 int ret;
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003423 unsigned long flags;
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003424
3425 if (!oh)
3426 return -EINVAL;
3427
3428 if (state != _HWMOD_STATE_DISABLED &&
3429 state != _HWMOD_STATE_ENABLED &&
3430 state != _HWMOD_STATE_IDLE)
3431 return -EINVAL;
3432
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003433 spin_lock_irqsave(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003434
3435 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3436 ret = -EINVAL;
3437 goto ohsps_unlock;
3438 }
3439
3440 oh->_postsetup_state = state;
3441 ret = 0;
3442
3443ohsps_unlock:
Paul Walmsleydc6d1cd2010-12-14 12:42:35 -07003444 spin_unlock_irqrestore(&oh->_lock, flags);
Paul Walmsley2092e5c2010-12-14 12:42:35 -07003445
3446 return ret;
3447}
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003448
3449/**
3450 * omap_hwmod_get_context_loss_count - get lost context count
3451 * @oh: struct omap_hwmod *
3452 *
3453 * Query the powerdomain of of @oh to get the context loss
3454 * count for this device.
3455 *
3456 * Returns the context loss count of the powerdomain assocated with @oh
3457 * upon success, or zero if no powerdomain exists for @oh.
3458 */
Tomi Valkeinenfc013872011-06-09 16:56:23 +03003459int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
Kevin Hilmanc80705a2010-12-21 21:31:55 -07003460{
3461 struct powerdomain *pwrdm;
3462 int ret = 0;
3463
3464 pwrdm = omap_hwmod_get_pwrdm(oh);
3465 if (pwrdm)
3466 ret = pwrdm_get_context_loss_count(pwrdm);
3467
3468 return ret;
3469}
Paul Walmsley43b01642011-03-10 03:50:07 -07003470
3471/**
3472 * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
3473 * @oh: struct omap_hwmod *
3474 *
3475 * Prevent the hwmod @oh from being reset during the setup process.
3476 * Intended for use by board-*.c files on boards with devices that
3477 * cannot tolerate being reset. Must be called before the hwmod has
3478 * been set up. Returns 0 upon success or negative error code upon
3479 * failure.
3480 */
3481int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
3482{
3483 if (!oh)
3484 return -EINVAL;
3485
3486 if (oh->_state != _HWMOD_STATE_REGISTERED) {
3487 pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
3488 oh->name);
3489 return -EINVAL;
3490 }
3491
3492 oh->flags |= HWMOD_INIT_NO_RESET;
3493
3494 return 0;
3495}
Tero Kristoabc2d542011-12-16 14:36:59 -07003496
3497/**
3498 * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
3499 * @oh: struct omap_hwmod * containing hwmod mux entries
3500 * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
3501 * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
3502 *
3503 * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
3504 * entry number @pad_idx for the hwmod @oh, trigger the interrupt
3505 * service routine for the hwmod's mpu_irqs array index @irq_idx. If
3506 * this function is not called for a given pad_idx, then the ISR
3507 * associated with @oh's first MPU IRQ will be triggered when an I/O
3508 * pad wakeup occurs on that pad. Note that @pad_idx is the index of
3509 * the _dynamic or wakeup_ entry: if there are other entries not
3510 * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
3511 * entries are NOT COUNTED in the dynamic pad index. This function
3512 * must be called separately for each pad that requires its interrupt
3513 * to be re-routed this way. Returns -EINVAL if there is an argument
3514 * problem or if @oh does not have hwmod mux entries or MPU IRQs;
3515 * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
3516 *
3517 * XXX This function interface is fragile. Rather than using array
3518 * indexes, which are subject to unpredictable change, it should be
3519 * using hwmod IRQ names, and some other stable key for the hwmod mux
3520 * pad records.
3521 */
3522int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
3523{
3524 int nr_irqs;
3525
3526 might_sleep();
3527
3528 if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
3529 pad_idx >= oh->mux->nr_pads_dynamic)
3530 return -EINVAL;
3531
3532 /* Check the number of available mpu_irqs */
3533 for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
3534 ;
3535
3536 if (irq_idx >= nr_irqs)
3537 return -EINVAL;
3538
3539 if (!oh->mux->irqs) {
3540 /* XXX What frees this? */
3541 oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
3542 GFP_KERNEL);
3543 if (!oh->mux->irqs)
3544 return -ENOMEM;
3545 }
3546 oh->mux->irqs[pad_idx] = irq_idx;
3547
3548 return 0;
3549}
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003550
3551/**
3552 * omap_hwmod_init - initialize the hwmod code
3553 *
3554 * Sets up some function pointers needed by the hwmod code to operate on the
3555 * currently-booted SoC. Intended to be called once during kernel init
3556 * before any hwmods are registered. No return value.
3557 */
3558void __init omap_hwmod_init(void)
3559{
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003560 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
3561 soc_ops.wait_target_ready = _omap2_wait_target_ready;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06003562 soc_ops.assert_hardreset = _omap2_assert_hardreset;
3563 soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
3564 soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003565 } else if (cpu_is_omap44xx()) {
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003566 soc_ops.enable_module = _omap4_enable_module;
3567 soc_ops.disable_module = _omap4_disable_module;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003568 soc_ops.wait_target_ready = _omap4_wait_target_ready;
Kevin Hilmanb8249cf2012-06-18 12:12:24 -06003569 soc_ops.assert_hardreset = _omap4_assert_hardreset;
3570 soc_ops.deassert_hardreset = _omap4_deassert_hardreset;
3571 soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
Kevin Hilman8f6aa8e2012-06-18 12:12:24 -06003572 } else {
3573 WARN(1, "omap_hwmod: unknown SoC type\n");
Kevin Hilman9ebfd282012-06-18 12:12:23 -06003574 }
3575
3576 inited = true;
3577}