OMAP: hwmod: Fix wrong pointer iteration in oh->slaves
[linux-2.6-block.git] / arch / arm / mach-omap2 / omap_hwmod.c
CommitLineData
63c85238
PW
1/*
2 * omap_hwmod implementation for OMAP2/3/4
3 *
4 * Copyright (C) 2009 Nokia Corporation
5 * Paul Walmsley
6 * With fixes and testing from Kevin Hilman
7 *
8 * Created in collaboration with (alphabetical order): Benoit Cousson,
9 * Kevin Hilman, Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari
10 * Poussa, Anand Sawant, Santosh Shilimkar, Richard Woodruff
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 * This code manages "OMAP modules" (on-chip devices) and their
17 * integration with Linux device driver and bus code.
18 *
19 * References:
20 * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
21 * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
22 * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
23 * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
24 * - Open Core Protocol Specification 2.2
25 *
26 * To do:
27 * - pin mux handling
28 * - handle IO mapping
29 * - bus throughput & module latency measurement code
30 *
31 * XXX add tests at the beginning of each function to ensure the hwmod is
32 * in the appropriate state
33 * XXX error return values should be checked to ensure that they are
34 * appropriate
35 */
36#undef DEBUG
37
38#include <linux/kernel.h>
39#include <linux/errno.h>
40#include <linux/io.h>
41#include <linux/clk.h>
42#include <linux/delay.h>
43#include <linux/err.h>
44#include <linux/list.h>
45#include <linux/mutex.h>
46#include <linux/bootmem.h>
47
6f8b7ff5 48#include <plat/common.h>
ce491cf8
TL
49#include <plat/cpu.h>
50#include <plat/clockdomain.h>
51#include <plat/powerdomain.h>
52#include <plat/clock.h>
53#include <plat/omap_hwmod.h>
63c85238
PW
54
55#include "cm.h"
56
57/* Maximum microseconds to wait for OMAP module to reset */
58#define MAX_MODULE_RESET_WAIT 10000
59
60/* Name of the OMAP hwmod for the MPU */
61#define MPU_INITIATOR_NAME "mpu_hwmod"
62
63/* omap_hwmod_list contains all registered struct omap_hwmods */
64static LIST_HEAD(omap_hwmod_list);
65
66static DEFINE_MUTEX(omap_hwmod_mutex);
67
68/* mpu_oh: used to add/remove MPU initiator from sleepdep list */
69static struct omap_hwmod *mpu_oh;
70
71/* inited: 0 if omap_hwmod_init() has not yet been called; 1 otherwise */
72static u8 inited;
73
74
75/* Private functions */
76
77/**
78 * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
79 * @oh: struct omap_hwmod *
80 *
81 * Load the current value of the hwmod OCP_SYSCONFIG register into the
82 * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
83 * OCP_SYSCONFIG register or 0 upon success.
84 */
85static int _update_sysc_cache(struct omap_hwmod *oh)
86{
43b40992
PW
87 if (!oh->class->sysc) {
88 WARN(1, "omap_hwmod: %s: cannot read OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
63c85238
PW
89 return -EINVAL;
90 }
91
92 /* XXX ensure module interface clock is up */
93
43b40992 94 oh->_sysc_cache = omap_hwmod_readl(oh, oh->class->sysc->sysc_offs);
63c85238 95
43b40992 96 if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
883edfdd 97 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
63c85238
PW
98
99 return 0;
100}
101
102/**
103 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
104 * @v: OCP_SYSCONFIG value to write
105 * @oh: struct omap_hwmod *
106 *
43b40992
PW
107 * Write @v into the module class' OCP_SYSCONFIG register, if it has
108 * one. No return value.
63c85238
PW
109 */
110static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
111{
43b40992
PW
112 if (!oh->class->sysc) {
113 WARN(1, "omap_hwmod: %s: cannot write OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
63c85238
PW
114 return;
115 }
116
117 /* XXX ensure module interface clock is up */
118
119 if (oh->_sysc_cache != v) {
120 oh->_sysc_cache = v;
43b40992 121 omap_hwmod_writel(v, oh, oh->class->sysc->sysc_offs);
63c85238
PW
122 }
123}
124
125/**
126 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
127 * @oh: struct omap_hwmod *
128 * @standbymode: MIDLEMODE field bits
129 * @v: pointer to register contents to modify
130 *
131 * Update the master standby mode bits in @v to be @standbymode for
132 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
133 * upon error or 0 upon success.
134 */
135static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
136 u32 *v)
137{
358f0e63
TG
138 u32 mstandby_mask;
139 u8 mstandby_shift;
140
43b40992
PW
141 if (!oh->class->sysc ||
142 !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
63c85238
PW
143 return -EINVAL;
144
43b40992
PW
145 if (!oh->class->sysc->sysc_fields) {
146 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
147 return -EINVAL;
148 }
149
43b40992 150 mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
358f0e63
TG
151 mstandby_mask = (0x3 << mstandby_shift);
152
153 *v &= ~mstandby_mask;
154 *v |= __ffs(standbymode) << mstandby_shift;
63c85238
PW
155
156 return 0;
157}
158
159/**
160 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
161 * @oh: struct omap_hwmod *
162 * @idlemode: SIDLEMODE field bits
163 * @v: pointer to register contents to modify
164 *
165 * Update the slave idle mode bits in @v to be @idlemode for the @oh
166 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
167 * or 0 upon success.
168 */
169static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
170{
358f0e63
TG
171 u32 sidle_mask;
172 u8 sidle_shift;
173
43b40992
PW
174 if (!oh->class->sysc ||
175 !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
63c85238
PW
176 return -EINVAL;
177
43b40992
PW
178 if (!oh->class->sysc->sysc_fields) {
179 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
180 return -EINVAL;
181 }
182
43b40992 183 sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
358f0e63
TG
184 sidle_mask = (0x3 << sidle_shift);
185
186 *v &= ~sidle_mask;
187 *v |= __ffs(idlemode) << sidle_shift;
63c85238
PW
188
189 return 0;
190}
191
192/**
193 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
194 * @oh: struct omap_hwmod *
195 * @clockact: CLOCKACTIVITY field bits
196 * @v: pointer to register contents to modify
197 *
198 * Update the clockactivity mode bits in @v to be @clockact for the
199 * @oh hwmod. Used for additional powersaving on some modules. Does
200 * not write to the hardware. Returns -EINVAL upon error or 0 upon
201 * success.
202 */
203static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
204{
358f0e63
TG
205 u32 clkact_mask;
206 u8 clkact_shift;
207
43b40992
PW
208 if (!oh->class->sysc ||
209 !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
63c85238
PW
210 return -EINVAL;
211
43b40992
PW
212 if (!oh->class->sysc->sysc_fields) {
213 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
214 return -EINVAL;
215 }
216
43b40992 217 clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
358f0e63
TG
218 clkact_mask = (0x3 << clkact_shift);
219
220 *v &= ~clkact_mask;
221 *v |= clockact << clkact_shift;
63c85238
PW
222
223 return 0;
224}
225
226/**
227 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
228 * @oh: struct omap_hwmod *
229 * @v: pointer to register contents to modify
230 *
231 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
232 * error or 0 upon success.
233 */
234static int _set_softreset(struct omap_hwmod *oh, u32 *v)
235{
358f0e63
TG
236 u32 softrst_mask;
237
43b40992
PW
238 if (!oh->class->sysc ||
239 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
63c85238
PW
240 return -EINVAL;
241
43b40992
PW
242 if (!oh->class->sysc->sysc_fields) {
243 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
244 return -EINVAL;
245 }
246
43b40992 247 softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
358f0e63
TG
248
249 *v |= softrst_mask;
63c85238
PW
250
251 return 0;
252}
253
726072e5
PW
254/**
255 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
256 * @oh: struct omap_hwmod *
257 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
258 * @v: pointer to register contents to modify
259 *
260 * Update the module autoidle bit in @v to be @autoidle for the @oh
261 * hwmod. The autoidle bit controls whether the module can gate
262 * internal clocks automatically when it isn't doing anything; the
263 * exact function of this bit varies on a per-module basis. This
264 * function does not write to the hardware. Returns -EINVAL upon
265 * error or 0 upon success.
266 */
267static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
268 u32 *v)
269{
358f0e63
TG
270 u32 autoidle_mask;
271 u8 autoidle_shift;
272
43b40992
PW
273 if (!oh->class->sysc ||
274 !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
726072e5
PW
275 return -EINVAL;
276
43b40992
PW
277 if (!oh->class->sysc->sysc_fields) {
278 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
279 return -EINVAL;
280 }
281
43b40992 282 autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
358f0e63
TG
283 autoidle_mask = (0x3 << autoidle_shift);
284
285 *v &= ~autoidle_mask;
286 *v |= autoidle << autoidle_shift;
726072e5
PW
287
288 return 0;
289}
290
63c85238
PW
291/**
292 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
293 * @oh: struct omap_hwmod *
294 *
295 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
296 * upon error or 0 upon success.
297 */
298static int _enable_wakeup(struct omap_hwmod *oh)
299{
358f0e63 300 u32 v, wakeup_mask;
63c85238 301
43b40992
PW
302 if (!oh->class->sysc ||
303 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
63c85238
PW
304 return -EINVAL;
305
43b40992
PW
306 if (!oh->class->sysc->sysc_fields) {
307 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
308 return -EINVAL;
309 }
310
43b40992 311 wakeup_mask = (0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
358f0e63 312
63c85238 313 v = oh->_sysc_cache;
358f0e63 314 v |= wakeup_mask;
63c85238
PW
315 _write_sysconfig(v, oh);
316
317 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
318
319 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
320
321 return 0;
322}
323
324/**
325 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
326 * @oh: struct omap_hwmod *
327 *
328 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
329 * upon error or 0 upon success.
330 */
331static int _disable_wakeup(struct omap_hwmod *oh)
332{
358f0e63 333 u32 v, wakeup_mask;
63c85238 334
43b40992
PW
335 if (!oh->class->sysc ||
336 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
63c85238
PW
337 return -EINVAL;
338
43b40992
PW
339 if (!oh->class->sysc->sysc_fields) {
340 WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
358f0e63
TG
341 return -EINVAL;
342 }
343
43b40992 344 wakeup_mask = (0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
358f0e63 345
63c85238 346 v = oh->_sysc_cache;
358f0e63 347 v &= ~wakeup_mask;
63c85238
PW
348 _write_sysconfig(v, oh);
349
350 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
351
352 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
353
354 return 0;
355}
356
357/**
358 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
359 * @oh: struct omap_hwmod *
360 *
361 * Prevent the hardware module @oh from entering idle while the
362 * hardare module initiator @init_oh is active. Useful when a module
363 * will be accessed by a particular initiator (e.g., if a module will
364 * be accessed by the IVA, there should be a sleepdep between the IVA
365 * initiator and the module). Only applies to modules in smart-idle
366 * mode. Returns -EINVAL upon error or passes along
55ed9694 367 * clkdm_add_sleepdep() value upon success.
63c85238
PW
368 */
369static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
370{
371 if (!oh->_clk)
372 return -EINVAL;
373
55ed9694 374 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
63c85238
PW
375}
376
377/**
378 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
379 * @oh: struct omap_hwmod *
380 *
381 * Allow the hardware module @oh to enter idle while the hardare
382 * module initiator @init_oh is active. Useful when a module will not
383 * be accessed by a particular initiator (e.g., if a module will not
384 * be accessed by the IVA, there should be no sleepdep between the IVA
385 * initiator and the module). Only applies to modules in smart-idle
386 * mode. Returns -EINVAL upon error or passes along
55ed9694 387 * clkdm_del_sleepdep() value upon success.
63c85238
PW
388 */
389static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
390{
391 if (!oh->_clk)
392 return -EINVAL;
393
55ed9694 394 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
63c85238
PW
395}
396
397/**
398 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
399 * @oh: struct omap_hwmod *
400 *
401 * Called from _init_clocks(). Populates the @oh _clk (main
402 * functional clock pointer) if a main_clk is present. Returns 0 on
403 * success or -EINVAL on error.
404 */
405static int _init_main_clk(struct omap_hwmod *oh)
406{
407 struct clk *c;
408 int ret = 0;
409
50ebdac2 410 if (!oh->main_clk)
63c85238
PW
411 return 0;
412
50ebdac2
PW
413 c = omap_clk_get_by_name(oh->main_clk);
414 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get main_clk %s\n",
415 oh->name, oh->main_clk);
63c85238
PW
416 if (IS_ERR(c))
417 ret = -EINVAL;
418 oh->_clk = c;
419
81d7c6ff 420 WARN(!c->clkdm, "omap_hwmod: %s: missing clockdomain for %s.\n",
50ebdac2 421 oh->main_clk, c->name);
81d7c6ff 422
63c85238
PW
423 return ret;
424}
425
426/**
427 * _init_interface_clk - get a struct clk * for the the hwmod's interface clks
428 * @oh: struct omap_hwmod *
429 *
430 * Called from _init_clocks(). Populates the @oh OCP slave interface
431 * clock pointers. Returns 0 on success or -EINVAL on error.
432 */
433static int _init_interface_clks(struct omap_hwmod *oh)
434{
63c85238
PW
435 struct clk *c;
436 int i;
437 int ret = 0;
438
439 if (oh->slaves_cnt == 0)
440 return 0;
441
682fdc96
BC
442 for (i = 0; i < oh->slaves_cnt; i++) {
443 struct omap_hwmod_ocp_if *os = oh->slaves[i];
444
50ebdac2 445 if (!os->clk)
63c85238
PW
446 continue;
447
50ebdac2 448 c = omap_clk_get_by_name(os->clk);
63c85238 449 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get "
50ebdac2 450 "interface_clk %s\n", oh->name, os->clk);
63c85238
PW
451 if (IS_ERR(c))
452 ret = -EINVAL;
453 os->_clk = c;
454 }
455
456 return ret;
457}
458
459/**
460 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
461 * @oh: struct omap_hwmod *
462 *
463 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
464 * clock pointers. Returns 0 on success or -EINVAL on error.
465 */
466static int _init_opt_clks(struct omap_hwmod *oh)
467{
468 struct omap_hwmod_opt_clk *oc;
469 struct clk *c;
470 int i;
471 int ret = 0;
472
473 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
50ebdac2 474 c = omap_clk_get_by_name(oc->clk);
63c85238 475 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get opt_clk "
50ebdac2 476 "%s\n", oh->name, oc->clk);
63c85238
PW
477 if (IS_ERR(c))
478 ret = -EINVAL;
479 oc->_clk = c;
480 }
481
482 return ret;
483}
484
485/**
486 * _enable_clocks - enable hwmod main clock and interface clocks
487 * @oh: struct omap_hwmod *
488 *
489 * Enables all clocks necessary for register reads and writes to succeed
490 * on the hwmod @oh. Returns 0.
491 */
492static int _enable_clocks(struct omap_hwmod *oh)
493{
63c85238
PW
494 int i;
495
496 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
497
498 if (oh->_clk && !IS_ERR(oh->_clk))
499 clk_enable(oh->_clk);
500
501 if (oh->slaves_cnt > 0) {
682fdc96
BC
502 for (i = 0; i < oh->slaves_cnt; i++) {
503 struct omap_hwmod_ocp_if *os = oh->slaves[i];
63c85238
PW
504 struct clk *c = os->_clk;
505
506 if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
507 clk_enable(c);
508 }
509 }
510
511 /* The opt clocks are controlled by the device driver. */
512
513 return 0;
514}
515
516/**
517 * _disable_clocks - disable hwmod main clock and interface clocks
518 * @oh: struct omap_hwmod *
519 *
520 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
521 */
522static int _disable_clocks(struct omap_hwmod *oh)
523{
63c85238
PW
524 int i;
525
526 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
527
528 if (oh->_clk && !IS_ERR(oh->_clk))
529 clk_disable(oh->_clk);
530
531 if (oh->slaves_cnt > 0) {
682fdc96
BC
532 for (i = 0; i < oh->slaves_cnt; i++) {
533 struct omap_hwmod_ocp_if *os = oh->slaves[i];
63c85238
PW
534 struct clk *c = os->_clk;
535
536 if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
537 clk_disable(c);
538 }
539 }
540
541 /* The opt clocks are controlled by the device driver. */
542
543 return 0;
544}
545
546/**
547 * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
548 * @oh: struct omap_hwmod *
549 *
550 * Returns the array index of the OCP slave port that the MPU
551 * addresses the device on, or -EINVAL upon error or not found.
552 */
553static int _find_mpu_port_index(struct omap_hwmod *oh)
554{
63c85238
PW
555 int i;
556 int found = 0;
557
558 if (!oh || oh->slaves_cnt == 0)
559 return -EINVAL;
560
682fdc96
BC
561 for (i = 0; i < oh->slaves_cnt; i++) {
562 struct omap_hwmod_ocp_if *os = oh->slaves[i];
563
63c85238
PW
564 if (os->user & OCP_USER_MPU) {
565 found = 1;
566 break;
567 }
568 }
569
570 if (found)
571 pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
572 oh->name, i);
573 else
574 pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
575 oh->name);
576
577 return (found) ? i : -EINVAL;
578}
579
580/**
581 * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
582 * @oh: struct omap_hwmod *
583 *
584 * Return the virtual address of the base of the register target of
585 * device @oh, or NULL on error.
586 */
587static void __iomem *_find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
588{
589 struct omap_hwmod_ocp_if *os;
590 struct omap_hwmod_addr_space *mem;
591 int i;
592 int found = 0;
986a13f5 593 void __iomem *va_start;
63c85238
PW
594
595 if (!oh || oh->slaves_cnt == 0)
596 return NULL;
597
682fdc96 598 os = oh->slaves[index];
63c85238
PW
599
600 for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
601 if (mem->flags & ADDR_TYPE_RT) {
602 found = 1;
603 break;
604 }
605 }
606
986a13f5
TL
607 if (found) {
608 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
609 if (!va_start) {
610 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
611 return NULL;
612 }
63c85238 613 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
986a13f5
TL
614 oh->name, va_start);
615 } else {
63c85238
PW
616 pr_debug("omap_hwmod: %s: no MPU register target found\n",
617 oh->name);
986a13f5 618 }
63c85238 619
986a13f5 620 return (found) ? va_start : NULL;
63c85238
PW
621}
622
623/**
624 * _sysc_enable - try to bring a module out of idle via OCP_SYSCONFIG
625 * @oh: struct omap_hwmod *
626 *
627 * If module is marked as SWSUP_SIDLE, force the module out of slave
628 * idle; otherwise, configure it for smart-idle. If module is marked
629 * as SWSUP_MSUSPEND, force the module out of master standby;
630 * otherwise, configure it for smart-standby. No return value.
631 */
632static void _sysc_enable(struct omap_hwmod *oh)
633{
43b40992 634 u8 idlemode, sf;
63c85238
PW
635 u32 v;
636
43b40992 637 if (!oh->class->sysc)
63c85238
PW
638 return;
639
640 v = oh->_sysc_cache;
43b40992 641 sf = oh->class->sysc->sysc_flags;
63c85238 642
43b40992 643 if (sf & SYSC_HAS_SIDLEMODE) {
63c85238
PW
644 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
645 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
646 _set_slave_idlemode(oh, idlemode, &v);
647 }
648
43b40992 649 if (sf & SYSC_HAS_MIDLEMODE) {
63c85238
PW
650 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
651 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
652 _set_master_standbymode(oh, idlemode, &v);
653 }
654
43b40992 655 if (sf & SYSC_HAS_AUTOIDLE) {
726072e5
PW
656 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
657 0 : 1;
658 _set_module_autoidle(oh, idlemode, &v);
659 }
660
661 /* XXX OCP ENAWAKEUP bit? */
63c85238 662
a16b1f7f
PW
663 /*
664 * XXX The clock framework should handle this, by
665 * calling into this code. But this must wait until the
666 * clock structures are tagged with omap_hwmod entries
667 */
43b40992
PW
668 if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
669 (sf & SYSC_HAS_CLOCKACTIVITY))
670 _set_clockactivity(oh, oh->class->sysc->clockact, &v);
63c85238
PW
671
672 _write_sysconfig(v, oh);
673}
674
675/**
676 * _sysc_idle - try to put a module into idle via OCP_SYSCONFIG
677 * @oh: struct omap_hwmod *
678 *
679 * If module is marked as SWSUP_SIDLE, force the module into slave
680 * idle; otherwise, configure it for smart-idle. If module is marked
681 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
682 * configure it for smart-standby. No return value.
683 */
684static void _sysc_idle(struct omap_hwmod *oh)
685{
43b40992 686 u8 idlemode, sf;
63c85238
PW
687 u32 v;
688
43b40992 689 if (!oh->class->sysc)
63c85238
PW
690 return;
691
692 v = oh->_sysc_cache;
43b40992 693 sf = oh->class->sysc->sysc_flags;
63c85238 694
43b40992 695 if (sf & SYSC_HAS_SIDLEMODE) {
63c85238
PW
696 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
697 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
698 _set_slave_idlemode(oh, idlemode, &v);
699 }
700
43b40992 701 if (sf & SYSC_HAS_MIDLEMODE) {
63c85238
PW
702 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
703 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
704 _set_master_standbymode(oh, idlemode, &v);
705 }
706
707 _write_sysconfig(v, oh);
708}
709
710/**
711 * _sysc_shutdown - force a module into idle via OCP_SYSCONFIG
712 * @oh: struct omap_hwmod *
713 *
714 * Force the module into slave idle and master suspend. No return
715 * value.
716 */
717static void _sysc_shutdown(struct omap_hwmod *oh)
718{
719 u32 v;
43b40992 720 u8 sf;
63c85238 721
43b40992 722 if (!oh->class->sysc)
63c85238
PW
723 return;
724
725 v = oh->_sysc_cache;
43b40992 726 sf = oh->class->sysc->sysc_flags;
63c85238 727
43b40992 728 if (sf & SYSC_HAS_SIDLEMODE)
63c85238
PW
729 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
730
43b40992 731 if (sf & SYSC_HAS_MIDLEMODE)
63c85238
PW
732 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
733
43b40992 734 if (sf & SYSC_HAS_AUTOIDLE)
726072e5 735 _set_module_autoidle(oh, 1, &v);
63c85238
PW
736
737 _write_sysconfig(v, oh);
738}
739
740/**
741 * _lookup - find an omap_hwmod by name
742 * @name: find an omap_hwmod by name
743 *
744 * Return a pointer to an omap_hwmod by name, or NULL if not found.
745 * Caller must hold omap_hwmod_mutex.
746 */
747static struct omap_hwmod *_lookup(const char *name)
748{
749 struct omap_hwmod *oh, *temp_oh;
750
751 oh = NULL;
752
753 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
754 if (!strcmp(name, temp_oh->name)) {
755 oh = temp_oh;
756 break;
757 }
758 }
759
760 return oh;
761}
762
763/**
764 * _init_clocks - clk_get() all clocks associated with this hwmod
765 * @oh: struct omap_hwmod *
766 *
767 * Called by omap_hwmod_late_init() (after omap2_clk_init()).
768 * Resolves all clock names embedded in the hwmod. Must be called
769 * with omap_hwmod_mutex held. Returns -EINVAL if the omap_hwmod
770 * has not yet been registered or if the clocks have already been
771 * initialized, 0 on success, or a non-zero error on failure.
772 */
773static int _init_clocks(struct omap_hwmod *oh)
774{
775 int ret = 0;
776
777 if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
778 return -EINVAL;
779
780 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
781
782 ret |= _init_main_clk(oh);
783 ret |= _init_interface_clks(oh);
784 ret |= _init_opt_clks(oh);
785
786 oh->_state = _HWMOD_STATE_CLKS_INITED;
787
788 return ret;
789}
790
791/**
792 * _wait_target_ready - wait for a module to leave slave idle
793 * @oh: struct omap_hwmod *
794 *
795 * Wait for a module @oh to leave slave idle. Returns 0 if the module
796 * does not have an IDLEST bit or if the module successfully leaves
797 * slave idle; otherwise, pass along the return value of the
798 * appropriate *_cm_wait_module_ready() function.
799 */
800static int _wait_target_ready(struct omap_hwmod *oh)
801{
802 struct omap_hwmod_ocp_if *os;
803 int ret;
804
805 if (!oh)
806 return -EINVAL;
807
808 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
809 return 0;
810
682fdc96 811 os = oh->slaves[oh->_mpu_port_index];
63c85238 812
33f7ec81 813 if (oh->flags & HWMOD_NO_IDLEST)
63c85238
PW
814 return 0;
815
816 /* XXX check module SIDLEMODE */
817
818 /* XXX check clock enable states */
819
820 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
821 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
822 oh->prcm.omap2.idlest_reg_id,
823 oh->prcm.omap2.idlest_idle_bit);
63c85238 824 } else if (cpu_is_omap44xx()) {
9a23dfe1 825 ret = omap4_cm_wait_module_ready(oh->prcm.omap4.clkctrl_reg);
63c85238
PW
826 } else {
827 BUG();
828 };
829
830 return ret;
831}
832
833/**
834 * _reset - reset an omap_hwmod
835 * @oh: struct omap_hwmod *
836 *
837 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
838 * enabled for this to work. Must be called with omap_hwmod_mutex
839 * held. Returns -EINVAL if the hwmod cannot be reset this way or if
840 * the hwmod is in the wrong state, -ETIMEDOUT if the module did not
841 * reset in time, or 0 upon success.
842 */
843static int _reset(struct omap_hwmod *oh)
844{
845 u32 r, v;
6f8b7ff5 846 int c = 0;
63c85238 847
43b40992
PW
848 if (!oh->class->sysc ||
849 !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET) ||
850 (oh->class->sysc->sysc_flags & SYSS_MISSING))
63c85238
PW
851 return -EINVAL;
852
853 /* clocks must be on for this operation */
854 if (oh->_state != _HWMOD_STATE_ENABLED) {
855 WARN(1, "omap_hwmod: %s: reset can only be entered from "
856 "enabled state\n", oh->name);
857 return -EINVAL;
858 }
859
860 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
861
862 v = oh->_sysc_cache;
863 r = _set_softreset(oh, &v);
864 if (r)
865 return r;
866 _write_sysconfig(v, oh);
867
43b40992 868 omap_test_timeout((omap_hwmod_readl(oh, oh->class->sysc->syss_offs) &
6f8b7ff5
PW
869 SYSS_RESETDONE_MASK),
870 MAX_MODULE_RESET_WAIT, c);
63c85238
PW
871
872 if (c == MAX_MODULE_RESET_WAIT)
873 WARN(1, "omap_hwmod: %s: failed to reset in %d usec\n",
874 oh->name, MAX_MODULE_RESET_WAIT);
875 else
02bfc030 876 pr_debug("omap_hwmod: %s: reset in %d usec\n", oh->name, c);
63c85238
PW
877
878 /*
879 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
880 * _wait_target_ready() or _reset()
881 */
882
883 return (c == MAX_MODULE_RESET_WAIT) ? -ETIMEDOUT : 0;
884}
885
886/**
887 * _enable - enable an omap_hwmod
888 * @oh: struct omap_hwmod *
889 *
890 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
891 * register target. Must be called with omap_hwmod_mutex held.
892 * Returns -EINVAL if the hwmod is in the wrong state or passes along
893 * the return value of _wait_target_ready().
894 */
895static int _enable(struct omap_hwmod *oh)
896{
897 int r;
898
899 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
900 oh->_state != _HWMOD_STATE_IDLE &&
901 oh->_state != _HWMOD_STATE_DISABLED) {
902 WARN(1, "omap_hwmod: %s: enabled state can only be entered "
903 "from initialized, idle, or disabled state\n", oh->name);
904 return -EINVAL;
905 }
906
907 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
908
909 /* XXX mux balls */
910
911 _add_initiator_dep(oh, mpu_oh);
912 _enable_clocks(oh);
913
63c85238 914 r = _wait_target_ready(oh);
9a23dfe1 915 if (!r) {
63c85238
PW
916 oh->_state = _HWMOD_STATE_ENABLED;
917
9a23dfe1
BC
918 /* Access the sysconfig only if the target is ready */
919 if (oh->class->sysc) {
920 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
921 _update_sysc_cache(oh);
922 _sysc_enable(oh);
923 }
924 } else {
925 pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
926 oh->name, r);
927 }
928
63c85238
PW
929 return r;
930}
931
932/**
933 * _idle - idle an omap_hwmod
934 * @oh: struct omap_hwmod *
935 *
936 * Idles an omap_hwmod @oh. This should be called once the hwmod has
937 * no further work. Returns -EINVAL if the hwmod is in the wrong
938 * state or returns 0.
939 */
940static int _idle(struct omap_hwmod *oh)
941{
942 if (oh->_state != _HWMOD_STATE_ENABLED) {
943 WARN(1, "omap_hwmod: %s: idle state can only be entered from "
944 "enabled state\n", oh->name);
945 return -EINVAL;
946 }
947
948 pr_debug("omap_hwmod: %s: idling\n", oh->name);
949
43b40992 950 if (oh->class->sysc)
63c85238
PW
951 _sysc_idle(oh);
952 _del_initiator_dep(oh, mpu_oh);
953 _disable_clocks(oh);
954
955 oh->_state = _HWMOD_STATE_IDLE;
956
957 return 0;
958}
959
960/**
961 * _shutdown - shutdown an omap_hwmod
962 * @oh: struct omap_hwmod *
963 *
964 * Shut down an omap_hwmod @oh. This should be called when the driver
965 * used for the hwmod is removed or unloaded or if the driver is not
966 * used by the system. Returns -EINVAL if the hwmod is in the wrong
967 * state or returns 0.
968 */
969static int _shutdown(struct omap_hwmod *oh)
970{
971 if (oh->_state != _HWMOD_STATE_IDLE &&
972 oh->_state != _HWMOD_STATE_ENABLED) {
973 WARN(1, "omap_hwmod: %s: disabled state can only be entered "
974 "from idle, or enabled state\n", oh->name);
975 return -EINVAL;
976 }
977
978 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
979
43b40992 980 if (oh->class->sysc)
63c85238
PW
981 _sysc_shutdown(oh);
982 _del_initiator_dep(oh, mpu_oh);
983 /* XXX what about the other system initiators here? DMA, tesla, d2d */
984 _disable_clocks(oh);
985 /* XXX Should this code also force-disable the optional clocks? */
986
987 /* XXX mux any associated balls to safe mode */
988
989 oh->_state = _HWMOD_STATE_DISABLED;
990
991 return 0;
992}
993
63c85238
PW
994/**
995 * _setup - do initial configuration of omap_hwmod
996 * @oh: struct omap_hwmod *
997 *
998 * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
999 * OCP_SYSCONFIG register. Must be called with omap_hwmod_mutex
1000 * held. Returns -EINVAL if the hwmod is in the wrong state or returns
1001 * 0.
1002 */
1003static int _setup(struct omap_hwmod *oh)
1004{
9a23dfe1 1005 int i, r;
63c85238
PW
1006
1007 if (!oh)
1008 return -EINVAL;
1009
1010 /* Set iclk autoidle mode */
1011 if (oh->slaves_cnt > 0) {
682fdc96
BC
1012 for (i = 0; i < oh->slaves_cnt; i++) {
1013 struct omap_hwmod_ocp_if *os = oh->slaves[i];
63c85238
PW
1014 struct clk *c = os->_clk;
1015
1016 if (!c || IS_ERR(c))
1017 continue;
1018
1019 if (os->flags & OCPIF_SWSUP_IDLE) {
1020 /* XXX omap_iclk_deny_idle(c); */
1021 } else {
1022 /* XXX omap_iclk_allow_idle(c); */
1023 clk_enable(c);
1024 }
1025 }
1026 }
1027
1028 oh->_state = _HWMOD_STATE_INITIALIZED;
1029
9a23dfe1
BC
1030 r = _enable(oh);
1031 if (r) {
1032 pr_warning("omap_hwmod: %s: cannot be enabled (%d)\n",
1033 oh->name, oh->_state);
1034 return 0;
1035 }
63c85238 1036
b835d014
PW
1037 if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
1038 /*
1039 * XXX Do the OCP_SYSCONFIG bits need to be
1040 * reprogrammed after a reset? If not, then this can
1041 * be removed. If they do, then probably the
1042 * _enable() function should be split to avoid the
1043 * rewrite of the OCP_SYSCONFIG register.
1044 */
43b40992 1045 if (oh->class->sysc) {
b835d014
PW
1046 _update_sysc_cache(oh);
1047 _sysc_enable(oh);
1048 }
1049 }
63c85238
PW
1050
1051 if (!(oh->flags & HWMOD_INIT_NO_IDLE))
1052 _idle(oh);
1053
1054 return 0;
1055}
1056
1057
1058
1059/* Public functions */
1060
1061u32 omap_hwmod_readl(struct omap_hwmod *oh, u16 reg_offs)
1062{
1063 return __raw_readl(oh->_rt_va + reg_offs);
1064}
1065
1066void omap_hwmod_writel(u32 v, struct omap_hwmod *oh, u16 reg_offs)
1067{
1068 __raw_writel(v, oh->_rt_va + reg_offs);
1069}
1070
46273e6f
KH
1071int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
1072{
1073 u32 v;
1074 int retval = 0;
1075
1076 if (!oh)
1077 return -EINVAL;
1078
1079 v = oh->_sysc_cache;
1080
1081 retval = _set_slave_idlemode(oh, idlemode, &v);
1082 if (!retval)
1083 _write_sysconfig(v, oh);
1084
1085 return retval;
1086}
1087
63c85238
PW
1088/**
1089 * omap_hwmod_register - register a struct omap_hwmod
1090 * @oh: struct omap_hwmod *
1091 *
43b40992
PW
1092 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
1093 * already has been registered by the same name; -EINVAL if the
1094 * omap_hwmod is in the wrong state, if @oh is NULL, if the
1095 * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
1096 * name, or if the omap_hwmod's class is missing a name; or 0 upon
1097 * success.
63c85238
PW
1098 *
1099 * XXX The data should be copied into bootmem, so the original data
1100 * should be marked __initdata and freed after init. This would allow
1101 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
1102 * that the copy process would be relatively complex due to the large number
1103 * of substructures.
1104 */
1105int omap_hwmod_register(struct omap_hwmod *oh)
1106{
1107 int ret, ms_id;
1108
43b40992
PW
1109 if (!oh || !oh->name || !oh->class || !oh->class->name ||
1110 (oh->_state != _HWMOD_STATE_UNKNOWN))
63c85238
PW
1111 return -EINVAL;
1112
1113 mutex_lock(&omap_hwmod_mutex);
1114
1115 pr_debug("omap_hwmod: %s: registering\n", oh->name);
1116
1117 if (_lookup(oh->name)) {
1118 ret = -EEXIST;
1119 goto ohr_unlock;
1120 }
1121
1122 ms_id = _find_mpu_port_index(oh);
1123 if (!IS_ERR_VALUE(ms_id)) {
1124 oh->_mpu_port_index = ms_id;
1125 oh->_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
1126 } else {
1127 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1128 }
1129
1130 list_add_tail(&oh->node, &omap_hwmod_list);
1131
1132 oh->_state = _HWMOD_STATE_REGISTERED;
1133
1134 ret = 0;
1135
1136ohr_unlock:
1137 mutex_unlock(&omap_hwmod_mutex);
1138 return ret;
1139}
1140
1141/**
1142 * omap_hwmod_lookup - look up a registered omap_hwmod by name
1143 * @name: name of the omap_hwmod to look up
1144 *
1145 * Given a @name of an omap_hwmod, return a pointer to the registered
1146 * struct omap_hwmod *, or NULL upon error.
1147 */
1148struct omap_hwmod *omap_hwmod_lookup(const char *name)
1149{
1150 struct omap_hwmod *oh;
1151
1152 if (!name)
1153 return NULL;
1154
1155 mutex_lock(&omap_hwmod_mutex);
1156 oh = _lookup(name);
1157 mutex_unlock(&omap_hwmod_mutex);
1158
1159 return oh;
1160}
1161
1162/**
1163 * omap_hwmod_for_each - call function for each registered omap_hwmod
1164 * @fn: pointer to a callback function
1165 *
1166 * Call @fn for each registered omap_hwmod, passing @data to each
1167 * function. @fn must return 0 for success or any other value for
1168 * failure. If @fn returns non-zero, the iteration across omap_hwmods
1169 * will stop and the non-zero return value will be passed to the
1170 * caller of omap_hwmod_for_each(). @fn is called with
1171 * omap_hwmod_for_each() held.
1172 */
1173int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh))
1174{
1175 struct omap_hwmod *temp_oh;
1176 int ret;
1177
1178 if (!fn)
1179 return -EINVAL;
1180
1181 mutex_lock(&omap_hwmod_mutex);
1182 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1183 ret = (*fn)(temp_oh);
1184 if (ret)
1185 break;
1186 }
1187 mutex_unlock(&omap_hwmod_mutex);
1188
1189 return ret;
1190}
1191
1192
1193/**
1194 * omap_hwmod_init - init omap_hwmod code and register hwmods
1195 * @ohs: pointer to an array of omap_hwmods to register
1196 *
1197 * Intended to be called early in boot before the clock framework is
1198 * initialized. If @ohs is not null, will register all omap_hwmods
1199 * listed in @ohs that are valid for this chip. Returns -EINVAL if
1200 * omap_hwmod_init() has already been called or 0 otherwise.
1201 */
1202int omap_hwmod_init(struct omap_hwmod **ohs)
1203{
1204 struct omap_hwmod *oh;
1205 int r;
1206
1207 if (inited)
1208 return -EINVAL;
1209
1210 inited = 1;
1211
1212 if (!ohs)
1213 return 0;
1214
1215 oh = *ohs;
1216 while (oh) {
1217 if (omap_chip_is(oh->omap_chip)) {
1218 r = omap_hwmod_register(oh);
1219 WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
1220 "%d\n", oh->name, r);
1221 }
1222 oh = *++ohs;
1223 }
1224
1225 return 0;
1226}
1227
1228/**
1229 * omap_hwmod_late_init - do some post-clock framework initialization
1230 *
1231 * Must be called after omap2_clk_init(). Resolves the struct clk names
1232 * to struct clk pointers for each registered omap_hwmod. Also calls
1233 * _setup() on each hwmod. Returns 0.
1234 */
1235int omap_hwmod_late_init(void)
1236{
1237 int r;
1238
1239 /* XXX check return value */
1240 r = omap_hwmod_for_each(_init_clocks);
1241 WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
1242
1243 mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
1244 WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
1245 MPU_INITIATOR_NAME);
1246
1247 omap_hwmod_for_each(_setup);
1248
1249 return 0;
1250}
1251
1252/**
1253 * omap_hwmod_unregister - unregister an omap_hwmod
1254 * @oh: struct omap_hwmod *
1255 *
1256 * Unregisters a previously-registered omap_hwmod @oh. There's probably
1257 * no use case for this, so it is likely to be removed in a later version.
1258 *
1259 * XXX Free all of the bootmem-allocated structures here when that is
1260 * implemented. Make it clear that core code is the only code that is
1261 * expected to unregister modules.
1262 */
1263int omap_hwmod_unregister(struct omap_hwmod *oh)
1264{
1265 if (!oh)
1266 return -EINVAL;
1267
1268 pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
1269
1270 mutex_lock(&omap_hwmod_mutex);
986a13f5 1271 iounmap(oh->_rt_va);
63c85238
PW
1272 list_del(&oh->node);
1273 mutex_unlock(&omap_hwmod_mutex);
1274
1275 return 0;
1276}
1277
1278/**
1279 * omap_hwmod_enable - enable an omap_hwmod
1280 * @oh: struct omap_hwmod *
1281 *
1282 * Enable an omap_hwomd @oh. Intended to be called by omap_device_enable().
1283 * Returns -EINVAL on error or passes along the return value from _enable().
1284 */
1285int omap_hwmod_enable(struct omap_hwmod *oh)
1286{
1287 int r;
1288
1289 if (!oh)
1290 return -EINVAL;
1291
1292 mutex_lock(&omap_hwmod_mutex);
1293 r = _enable(oh);
1294 mutex_unlock(&omap_hwmod_mutex);
1295
1296 return r;
1297}
1298
1299/**
1300 * omap_hwmod_idle - idle an omap_hwmod
1301 * @oh: struct omap_hwmod *
1302 *
1303 * Idle an omap_hwomd @oh. Intended to be called by omap_device_idle().
1304 * Returns -EINVAL on error or passes along the return value from _idle().
1305 */
1306int omap_hwmod_idle(struct omap_hwmod *oh)
1307{
1308 if (!oh)
1309 return -EINVAL;
1310
1311 mutex_lock(&omap_hwmod_mutex);
1312 _idle(oh);
1313 mutex_unlock(&omap_hwmod_mutex);
1314
1315 return 0;
1316}
1317
1318/**
1319 * omap_hwmod_shutdown - shutdown an omap_hwmod
1320 * @oh: struct omap_hwmod *
1321 *
1322 * Shutdown an omap_hwomd @oh. Intended to be called by
1323 * omap_device_shutdown(). Returns -EINVAL on error or passes along
1324 * the return value from _shutdown().
1325 */
1326int omap_hwmod_shutdown(struct omap_hwmod *oh)
1327{
1328 if (!oh)
1329 return -EINVAL;
1330
1331 mutex_lock(&omap_hwmod_mutex);
1332 _shutdown(oh);
1333 mutex_unlock(&omap_hwmod_mutex);
1334
1335 return 0;
1336}
1337
1338/**
1339 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
1340 * @oh: struct omap_hwmod *oh
1341 *
1342 * Intended to be called by the omap_device code.
1343 */
1344int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
1345{
1346 mutex_lock(&omap_hwmod_mutex);
1347 _enable_clocks(oh);
1348 mutex_unlock(&omap_hwmod_mutex);
1349
1350 return 0;
1351}
1352
1353/**
1354 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
1355 * @oh: struct omap_hwmod *oh
1356 *
1357 * Intended to be called by the omap_device code.
1358 */
1359int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
1360{
1361 mutex_lock(&omap_hwmod_mutex);
1362 _disable_clocks(oh);
1363 mutex_unlock(&omap_hwmod_mutex);
1364
1365 return 0;
1366}
1367
1368/**
1369 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
1370 * @oh: struct omap_hwmod *oh
1371 *
1372 * Intended to be called by drivers and core code when all posted
1373 * writes to a device must complete before continuing further
1374 * execution (for example, after clearing some device IRQSTATUS
1375 * register bits)
1376 *
1377 * XXX what about targets with multiple OCP threads?
1378 */
1379void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
1380{
1381 BUG_ON(!oh);
1382
43b40992 1383 if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
63c85238
PW
1384 WARN(1, "omap_device: %s: OCP barrier impossible due to "
1385 "device configuration\n", oh->name);
1386 return;
1387 }
1388
1389 /*
1390 * Forces posted writes to complete on the OCP thread handling
1391 * register writes
1392 */
43b40992 1393 omap_hwmod_readl(oh, oh->class->sysc->sysc_offs);
63c85238
PW
1394}
1395
1396/**
1397 * omap_hwmod_reset - reset the hwmod
1398 * @oh: struct omap_hwmod *
1399 *
1400 * Under some conditions, a driver may wish to reset the entire device.
1401 * Called from omap_device code. Returns -EINVAL on error or passes along
1402 * the return value from _reset()/_enable().
1403 */
1404int omap_hwmod_reset(struct omap_hwmod *oh)
1405{
1406 int r;
1407
1408 if (!oh || !(oh->_state & _HWMOD_STATE_ENABLED))
1409 return -EINVAL;
1410
1411 mutex_lock(&omap_hwmod_mutex);
1412 r = _reset(oh);
1413 if (!r)
1414 r = _enable(oh);
1415 mutex_unlock(&omap_hwmod_mutex);
1416
1417 return r;
1418}
1419
1420/**
1421 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
1422 * @oh: struct omap_hwmod *
1423 * @res: pointer to the first element of an array of struct resource to fill
1424 *
1425 * Count the number of struct resource array elements necessary to
1426 * contain omap_hwmod @oh resources. Intended to be called by code
1427 * that registers omap_devices. Intended to be used to determine the
1428 * size of a dynamically-allocated struct resource array, before
1429 * calling omap_hwmod_fill_resources(). Returns the number of struct
1430 * resource array elements needed.
1431 *
1432 * XXX This code is not optimized. It could attempt to merge adjacent
1433 * resource IDs.
1434 *
1435 */
1436int omap_hwmod_count_resources(struct omap_hwmod *oh)
1437{
1438 int ret, i;
1439
1440 ret = oh->mpu_irqs_cnt + oh->sdma_chs_cnt;
1441
1442 for (i = 0; i < oh->slaves_cnt; i++)
682fdc96 1443 ret += oh->slaves[i]->addr_cnt;
63c85238
PW
1444
1445 return ret;
1446}
1447
1448/**
1449 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
1450 * @oh: struct omap_hwmod *
1451 * @res: pointer to the first element of an array of struct resource to fill
1452 *
1453 * Fill the struct resource array @res with resource data from the
1454 * omap_hwmod @oh. Intended to be called by code that registers
1455 * omap_devices. See also omap_hwmod_count_resources(). Returns the
1456 * number of array elements filled.
1457 */
1458int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
1459{
1460 int i, j;
1461 int r = 0;
1462
1463 /* For each IRQ, DMA, memory area, fill in array.*/
1464
1465 for (i = 0; i < oh->mpu_irqs_cnt; i++) {
718bfd76
PW
1466 (res + r)->name = (oh->mpu_irqs + i)->name;
1467 (res + r)->start = (oh->mpu_irqs + i)->irq;
1468 (res + r)->end = (oh->mpu_irqs + i)->irq;
63c85238
PW
1469 (res + r)->flags = IORESOURCE_IRQ;
1470 r++;
1471 }
1472
1473 for (i = 0; i < oh->sdma_chs_cnt; i++) {
1474 (res + r)->name = (oh->sdma_chs + i)->name;
1475 (res + r)->start = (oh->sdma_chs + i)->dma_ch;
1476 (res + r)->end = (oh->sdma_chs + i)->dma_ch;
1477 (res + r)->flags = IORESOURCE_DMA;
1478 r++;
1479 }
1480
1481 for (i = 0; i < oh->slaves_cnt; i++) {
1482 struct omap_hwmod_ocp_if *os;
1483
682fdc96 1484 os = oh->slaves[i];
63c85238
PW
1485
1486 for (j = 0; j < os->addr_cnt; j++) {
1487 (res + r)->start = (os->addr + j)->pa_start;
1488 (res + r)->end = (os->addr + j)->pa_end;
1489 (res + r)->flags = IORESOURCE_MEM;
1490 r++;
1491 }
1492 }
1493
1494 return r;
1495}
1496
1497/**
1498 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
1499 * @oh: struct omap_hwmod *
1500 *
1501 * Return the powerdomain pointer associated with the OMAP module
1502 * @oh's main clock. If @oh does not have a main clk, return the
1503 * powerdomain associated with the interface clock associated with the
1504 * module's MPU port. (XXX Perhaps this should use the SDMA port
1505 * instead?) Returns NULL on error, or a struct powerdomain * on
1506 * success.
1507 */
1508struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
1509{
1510 struct clk *c;
1511
1512 if (!oh)
1513 return NULL;
1514
1515 if (oh->_clk) {
1516 c = oh->_clk;
1517 } else {
1518 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1519 return NULL;
1520 c = oh->slaves[oh->_mpu_port_index]->_clk;
1521 }
1522
d5647c18
TG
1523 if (!c->clkdm)
1524 return NULL;
1525
63c85238
PW
1526 return c->clkdm->pwrdm.ptr;
1527
1528}
1529
1530/**
1531 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
1532 * @oh: struct omap_hwmod *
1533 * @init_oh: struct omap_hwmod * (initiator)
1534 *
1535 * Add a sleep dependency between the initiator @init_oh and @oh.
1536 * Intended to be called by DSP/Bridge code via platform_data for the
1537 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1538 * code needs to add/del initiator dependencies dynamically
1539 * before/after accessing a device. Returns the return value from
1540 * _add_initiator_dep().
1541 *
1542 * XXX Keep a usecount in the clockdomain code
1543 */
1544int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
1545 struct omap_hwmod *init_oh)
1546{
1547 return _add_initiator_dep(oh, init_oh);
1548}
1549
1550/*
1551 * XXX what about functions for drivers to save/restore ocp_sysconfig
1552 * for context save/restore operations?
1553 */
1554
1555/**
1556 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
1557 * @oh: struct omap_hwmod *
1558 * @init_oh: struct omap_hwmod * (initiator)
1559 *
1560 * Remove a sleep dependency between the initiator @init_oh and @oh.
1561 * Intended to be called by DSP/Bridge code via platform_data for the
1562 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1563 * code needs to add/del initiator dependencies dynamically
1564 * before/after accessing a device. Returns the return value from
1565 * _del_initiator_dep().
1566 *
1567 * XXX Keep a usecount in the clockdomain code
1568 */
1569int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
1570 struct omap_hwmod *init_oh)
1571{
1572 return _del_initiator_dep(oh, init_oh);
1573}
1574
63c85238
PW
1575/**
1576 * omap_hwmod_enable_wakeup - allow device to wake up the system
1577 * @oh: struct omap_hwmod *
1578 *
1579 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
1580 * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
1581 * registers to cause the PRCM to receive wakeup events from the
1582 * module. Does not set any wakeup routing registers beyond this
1583 * point - if the module is to wake up any other module or subsystem,
1584 * that must be set separately. Called by omap_device code. Returns
1585 * -EINVAL on error or 0 upon success.
1586 */
1587int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
1588{
43b40992
PW
1589 if (!oh->class->sysc ||
1590 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
63c85238
PW
1591 return -EINVAL;
1592
1593 mutex_lock(&omap_hwmod_mutex);
1594 _enable_wakeup(oh);
1595 mutex_unlock(&omap_hwmod_mutex);
1596
1597 return 0;
1598}
1599
1600/**
1601 * omap_hwmod_disable_wakeup - prevent device from waking the system
1602 * @oh: struct omap_hwmod *
1603 *
1604 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
1605 * from sending wakeups to the PRCM. Eventually this should clear
1606 * PRCM wakeup registers to cause the PRCM to ignore wakeup events
1607 * from the module. Does not set any wakeup routing registers beyond
1608 * this point - if the module is to wake up any other module or
1609 * subsystem, that must be set separately. Called by omap_device
1610 * code. Returns -EINVAL on error or 0 upon success.
1611 */
1612int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
1613{
43b40992
PW
1614 if (!oh->class->sysc ||
1615 !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
63c85238
PW
1616 return -EINVAL;
1617
1618 mutex_lock(&omap_hwmod_mutex);
1619 _disable_wakeup(oh);
1620 mutex_unlock(&omap_hwmod_mutex);
1621
1622 return 0;
1623}
43b40992
PW
1624
1625/**
1626 * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
1627 * @classname: struct omap_hwmod_class name to search for
1628 * @fn: callback function pointer to call for each hwmod in class @classname
1629 * @user: arbitrary context data to pass to the callback function
1630 *
1631 * For each omap_hwmod of class @classname, call @fn. Takes
1632 * omap_hwmod_mutex to prevent the hwmod list from changing during the
1633 * iteration. If the callback function returns something other than
1634 * zero, the iterator is terminated, and the callback function's return
1635 * value is passed back to the caller. Returns 0 upon success, -EINVAL
1636 * if @classname or @fn are NULL, or passes back the error code from @fn.
1637 */
1638int omap_hwmod_for_each_by_class(const char *classname,
1639 int (*fn)(struct omap_hwmod *oh,
1640 void *user),
1641 void *user)
1642{
1643 struct omap_hwmod *temp_oh;
1644 int ret = 0;
1645
1646 if (!classname || !fn)
1647 return -EINVAL;
1648
1649 pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
1650 __func__, classname);
1651
1652 mutex_lock(&omap_hwmod_mutex);
1653
1654 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1655 if (!strcmp(temp_oh->class->name, classname)) {
1656 pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
1657 __func__, temp_oh->name);
1658 ret = (*fn)(temp_oh, user);
1659 if (ret)
1660 break;
1661 }
1662 }
1663
1664 mutex_unlock(&omap_hwmod_mutex);
1665
1666 if (ret)
1667 pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
1668 __func__, ret);
1669
1670 return ret;
1671}
1672