blob: ad884c0aaa421792e6a5f3ad525af04f805b1f4d [file] [log] [blame]
Paul Walmsley63c85232009-09-03 20:14:03 +03001/*
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
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -070048#include <plat/common.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070049#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>
Paul Walmsley63c85232009-09-03 20:14:03 +030054
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{
87 if (!oh->sysconfig) {
88 WARN(!oh->sysconfig, "omap_hwmod: %s: cannot read "
89 "OCP_SYSCONFIG: not defined on hwmod\n", oh->name);
90 return -EINVAL;
91 }
92
93 /* XXX ensure module interface clock is up */
94
95 oh->_sysc_cache = omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
96
Thara Gopinath883edfd2010-01-19 17:30:51 -070097 if (!(oh->sysconfig->sysc_flags & SYSC_NO_CACHE))
98 oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
Paul Walmsley63c85232009-09-03 20:14:03 +030099
100 return 0;
101}
102
103/**
104 * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
105 * @v: OCP_SYSCONFIG value to write
106 * @oh: struct omap_hwmod *
107 *
108 * Write @v into the module OCP_SYSCONFIG register, if it has one. No
109 * return value.
110 */
111static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
112{
113 if (!oh->sysconfig) {
114 WARN(!oh->sysconfig, "omap_hwmod: %s: cannot write "
115 "OCP_SYSCONFIG: not defined on hwmod\n", oh->name);
116 return;
117 }
118
119 /* XXX ensure module interface clock is up */
120
121 if (oh->_sysc_cache != v) {
122 oh->_sysc_cache = v;
123 omap_hwmod_writel(v, oh, oh->sysconfig->sysc_offs);
124 }
125}
126
127/**
128 * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
129 * @oh: struct omap_hwmod *
130 * @standbymode: MIDLEMODE field bits
131 * @v: pointer to register contents to modify
132 *
133 * Update the master standby mode bits in @v to be @standbymode for
134 * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
135 * upon error or 0 upon success.
136 */
137static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
138 u32 *v)
139{
140 if (!oh->sysconfig ||
141 !(oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE))
142 return -EINVAL;
143
144 *v &= ~SYSC_MIDLEMODE_MASK;
145 *v |= __ffs(standbymode) << SYSC_MIDLEMODE_SHIFT;
146
147 return 0;
148}
149
150/**
151 * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
152 * @oh: struct omap_hwmod *
153 * @idlemode: SIDLEMODE field bits
154 * @v: pointer to register contents to modify
155 *
156 * Update the slave idle mode bits in @v to be @idlemode for the @oh
157 * hwmod. Does not write to the hardware. Returns -EINVAL upon error
158 * or 0 upon success.
159 */
160static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
161{
162 if (!oh->sysconfig ||
163 !(oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE))
164 return -EINVAL;
165
166 *v &= ~SYSC_SIDLEMODE_MASK;
167 *v |= __ffs(idlemode) << SYSC_SIDLEMODE_SHIFT;
168
169 return 0;
170}
171
172/**
173 * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
174 * @oh: struct omap_hwmod *
175 * @clockact: CLOCKACTIVITY field bits
176 * @v: pointer to register contents to modify
177 *
178 * Update the clockactivity mode bits in @v to be @clockact for the
179 * @oh hwmod. Used for additional powersaving on some modules. Does
180 * not write to the hardware. Returns -EINVAL upon error or 0 upon
181 * success.
182 */
183static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
184{
185 if (!oh->sysconfig ||
186 !(oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
187 return -EINVAL;
188
189 *v &= ~SYSC_CLOCKACTIVITY_MASK;
190 *v |= clockact << SYSC_CLOCKACTIVITY_SHIFT;
191
192 return 0;
193}
194
195/**
196 * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
197 * @oh: struct omap_hwmod *
198 * @v: pointer to register contents to modify
199 *
200 * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
201 * error or 0 upon success.
202 */
203static int _set_softreset(struct omap_hwmod *oh, u32 *v)
204{
205 if (!oh->sysconfig ||
206 !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET))
207 return -EINVAL;
208
209 *v |= SYSC_SOFTRESET_MASK;
210
211 return 0;
212}
213
214/**
Paul Walmsley726072e2009-12-08 16:34:15 -0700215 * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
216 * @oh: struct omap_hwmod *
217 * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
218 * @v: pointer to register contents to modify
219 *
220 * Update the module autoidle bit in @v to be @autoidle for the @oh
221 * hwmod. The autoidle bit controls whether the module can gate
222 * internal clocks automatically when it isn't doing anything; the
223 * exact function of this bit varies on a per-module basis. This
224 * function does not write to the hardware. Returns -EINVAL upon
225 * error or 0 upon success.
226 */
227static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
228 u32 *v)
229{
230 if (!oh->sysconfig ||
231 !(oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE))
232 return -EINVAL;
233
234 *v &= ~SYSC_AUTOIDLE_MASK;
235 *v |= autoidle << SYSC_AUTOIDLE_SHIFT;
236
237 return 0;
238}
239
240/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300241 * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
242 * @oh: struct omap_hwmod *
243 *
244 * Allow the hardware module @oh to send wakeups. Returns -EINVAL
245 * upon error or 0 upon success.
246 */
247static int _enable_wakeup(struct omap_hwmod *oh)
248{
249 u32 v;
250
251 if (!oh->sysconfig ||
252 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
253 return -EINVAL;
254
255 v = oh->_sysc_cache;
256 v |= SYSC_ENAWAKEUP_MASK;
257 _write_sysconfig(v, oh);
258
259 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
260
261 oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
262
263 return 0;
264}
265
266/**
267 * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
268 * @oh: struct omap_hwmod *
269 *
270 * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
271 * upon error or 0 upon success.
272 */
273static int _disable_wakeup(struct omap_hwmod *oh)
274{
275 u32 v;
276
277 if (!oh->sysconfig ||
278 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
279 return -EINVAL;
280
281 v = oh->_sysc_cache;
282 v &= ~SYSC_ENAWAKEUP_MASK;
283 _write_sysconfig(v, oh);
284
285 /* XXX test pwrdm_get_wken for this hwmod's subsystem */
286
287 oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
288
289 return 0;
290}
291
292/**
293 * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
294 * @oh: struct omap_hwmod *
295 *
296 * Prevent the hardware module @oh from entering idle while the
297 * hardare module initiator @init_oh is active. Useful when a module
298 * will be accessed by a particular initiator (e.g., if a module will
299 * be accessed by the IVA, there should be a sleepdep between the IVA
300 * initiator and the module). Only applies to modules in smart-idle
301 * mode. Returns -EINVAL upon error or passes along
Paul Walmsley55ed9692010-01-26 20:12:59 -0700302 * clkdm_add_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300303 */
304static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
305{
306 if (!oh->_clk)
307 return -EINVAL;
308
Paul Walmsley55ed9692010-01-26 20:12:59 -0700309 return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300310}
311
312/**
313 * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
314 * @oh: struct omap_hwmod *
315 *
316 * Allow the hardware module @oh to enter idle while the hardare
317 * module initiator @init_oh is active. Useful when a module will not
318 * be accessed by a particular initiator (e.g., if a module will not
319 * be accessed by the IVA, there should be no sleepdep between the IVA
320 * initiator and the module). Only applies to modules in smart-idle
321 * mode. Returns -EINVAL upon error or passes along
Paul Walmsley55ed9692010-01-26 20:12:59 -0700322 * clkdm_del_sleepdep() value upon success.
Paul Walmsley63c85232009-09-03 20:14:03 +0300323 */
324static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
325{
326 if (!oh->_clk)
327 return -EINVAL;
328
Paul Walmsley55ed9692010-01-26 20:12:59 -0700329 return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
Paul Walmsley63c85232009-09-03 20:14:03 +0300330}
331
332/**
333 * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
334 * @oh: struct omap_hwmod *
335 *
336 * Called from _init_clocks(). Populates the @oh _clk (main
337 * functional clock pointer) if a main_clk is present. Returns 0 on
338 * success or -EINVAL on error.
339 */
340static int _init_main_clk(struct omap_hwmod *oh)
341{
342 struct clk *c;
343 int ret = 0;
344
345 if (!oh->clkdev_con_id)
346 return 0;
347
348 c = clk_get_sys(oh->clkdev_dev_id, oh->clkdev_con_id);
349 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get main_clk %s.%s\n",
350 oh->name, oh->clkdev_dev_id, oh->clkdev_con_id);
351 if (IS_ERR(c))
352 ret = -EINVAL;
353 oh->_clk = c;
354
Kevin Hilman81d7c6f2009-12-08 16:34:24 -0700355 WARN(!c->clkdm, "omap_hwmod: %s: missing clockdomain for %s.\n",
356 oh->clkdev_con_id, c->name);
357
Paul Walmsley63c85232009-09-03 20:14:03 +0300358 return ret;
359}
360
361/**
362 * _init_interface_clk - get a struct clk * for the the hwmod's interface clks
363 * @oh: struct omap_hwmod *
364 *
365 * Called from _init_clocks(). Populates the @oh OCP slave interface
366 * clock pointers. Returns 0 on success or -EINVAL on error.
367 */
368static int _init_interface_clks(struct omap_hwmod *oh)
369{
370 struct omap_hwmod_ocp_if *os;
371 struct clk *c;
372 int i;
373 int ret = 0;
374
375 if (oh->slaves_cnt == 0)
376 return 0;
377
378 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
379 if (!os->clkdev_con_id)
380 continue;
381
382 c = clk_get_sys(os->clkdev_dev_id, os->clkdev_con_id);
383 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get "
384 "interface_clk %s.%s\n", oh->name,
385 os->clkdev_dev_id, os->clkdev_con_id);
386 if (IS_ERR(c))
387 ret = -EINVAL;
388 os->_clk = c;
389 }
390
391 return ret;
392}
393
394/**
395 * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
396 * @oh: struct omap_hwmod *
397 *
398 * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
399 * clock pointers. Returns 0 on success or -EINVAL on error.
400 */
401static int _init_opt_clks(struct omap_hwmod *oh)
402{
403 struct omap_hwmod_opt_clk *oc;
404 struct clk *c;
405 int i;
406 int ret = 0;
407
408 for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
409 c = clk_get_sys(oc->clkdev_dev_id, oc->clkdev_con_id);
410 WARN(IS_ERR(c), "omap_hwmod: %s: cannot clk_get opt_clk "
411 "%s.%s\n", oh->name, oc->clkdev_dev_id,
412 oc->clkdev_con_id);
413 if (IS_ERR(c))
414 ret = -EINVAL;
415 oc->_clk = c;
416 }
417
418 return ret;
419}
420
421/**
422 * _enable_clocks - enable hwmod main clock and interface clocks
423 * @oh: struct omap_hwmod *
424 *
425 * Enables all clocks necessary for register reads and writes to succeed
426 * on the hwmod @oh. Returns 0.
427 */
428static int _enable_clocks(struct omap_hwmod *oh)
429{
430 struct omap_hwmod_ocp_if *os;
431 int i;
432
433 pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
434
435 if (oh->_clk && !IS_ERR(oh->_clk))
436 clk_enable(oh->_clk);
437
438 if (oh->slaves_cnt > 0) {
439 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
440 struct clk *c = os->_clk;
441
442 if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
443 clk_enable(c);
444 }
445 }
446
447 /* The opt clocks are controlled by the device driver. */
448
449 return 0;
450}
451
452/**
453 * _disable_clocks - disable hwmod main clock and interface clocks
454 * @oh: struct omap_hwmod *
455 *
456 * Disables the hwmod @oh main functional and interface clocks. Returns 0.
457 */
458static int _disable_clocks(struct omap_hwmod *oh)
459{
460 struct omap_hwmod_ocp_if *os;
461 int i;
462
463 pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
464
465 if (oh->_clk && !IS_ERR(oh->_clk))
466 clk_disable(oh->_clk);
467
468 if (oh->slaves_cnt > 0) {
469 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
470 struct clk *c = os->_clk;
471
472 if (c && !IS_ERR(c) && (os->flags & OCPIF_SWSUP_IDLE))
473 clk_disable(c);
474 }
475 }
476
477 /* The opt clocks are controlled by the device driver. */
478
479 return 0;
480}
481
482/**
483 * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
484 * @oh: struct omap_hwmod *
485 *
486 * Returns the array index of the OCP slave port that the MPU
487 * addresses the device on, or -EINVAL upon error or not found.
488 */
489static int _find_mpu_port_index(struct omap_hwmod *oh)
490{
491 struct omap_hwmod_ocp_if *os;
492 int i;
493 int found = 0;
494
495 if (!oh || oh->slaves_cnt == 0)
496 return -EINVAL;
497
498 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
499 if (os->user & OCP_USER_MPU) {
500 found = 1;
501 break;
502 }
503 }
504
505 if (found)
506 pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
507 oh->name, i);
508 else
509 pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
510 oh->name);
511
512 return (found) ? i : -EINVAL;
513}
514
515/**
516 * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
517 * @oh: struct omap_hwmod *
518 *
519 * Return the virtual address of the base of the register target of
520 * device @oh, or NULL on error.
521 */
522static void __iomem *_find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
523{
524 struct omap_hwmod_ocp_if *os;
525 struct omap_hwmod_addr_space *mem;
526 int i;
527 int found = 0;
Tony Lindgren986a13f2009-10-19 15:25:22 -0700528 void __iomem *va_start;
Paul Walmsley63c85232009-09-03 20:14:03 +0300529
530 if (!oh || oh->slaves_cnt == 0)
531 return NULL;
532
533 os = *oh->slaves + index;
534
535 for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
536 if (mem->flags & ADDR_TYPE_RT) {
537 found = 1;
538 break;
539 }
540 }
541
Tony Lindgren986a13f2009-10-19 15:25:22 -0700542 if (found) {
543 va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
544 if (!va_start) {
545 pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
546 return NULL;
547 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300548 pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
Tony Lindgren986a13f2009-10-19 15:25:22 -0700549 oh->name, va_start);
550 } else {
Paul Walmsley63c85232009-09-03 20:14:03 +0300551 pr_debug("omap_hwmod: %s: no MPU register target found\n",
552 oh->name);
Tony Lindgren986a13f2009-10-19 15:25:22 -0700553 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300554
Tony Lindgren986a13f2009-10-19 15:25:22 -0700555 return (found) ? va_start : NULL;
Paul Walmsley63c85232009-09-03 20:14:03 +0300556}
557
558/**
559 * _sysc_enable - try to bring a module out of idle via OCP_SYSCONFIG
560 * @oh: struct omap_hwmod *
561 *
562 * If module is marked as SWSUP_SIDLE, force the module out of slave
563 * idle; otherwise, configure it for smart-idle. If module is marked
564 * as SWSUP_MSUSPEND, force the module out of master standby;
565 * otherwise, configure it for smart-standby. No return value.
566 */
567static void _sysc_enable(struct omap_hwmod *oh)
568{
569 u8 idlemode;
570 u32 v;
571
572 if (!oh->sysconfig)
573 return;
574
575 v = oh->_sysc_cache;
576
577 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
578 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
579 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
580 _set_slave_idlemode(oh, idlemode, &v);
581 }
582
583 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
584 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
585 HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
586 _set_master_standbymode(oh, idlemode, &v);
587 }
588
Paul Walmsley726072e2009-12-08 16:34:15 -0700589 if (oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE) {
590 idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
591 0 : 1;
592 _set_module_autoidle(oh, idlemode, &v);
593 }
594
595 /* XXX OCP ENAWAKEUP bit? */
Paul Walmsley63c85232009-09-03 20:14:03 +0300596
Paul Walmsleya16b1f72009-12-08 16:34:17 -0700597 /*
598 * XXX The clock framework should handle this, by
599 * calling into this code. But this must wait until the
600 * clock structures are tagged with omap_hwmod entries
601 */
Paul Walmsley63c85232009-09-03 20:14:03 +0300602 if (oh->flags & HWMOD_SET_DEFAULT_CLOCKACT &&
603 oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY)
604 _set_clockactivity(oh, oh->sysconfig->clockact, &v);
605
606 _write_sysconfig(v, oh);
607}
608
609/**
610 * _sysc_idle - try to put a module into idle via OCP_SYSCONFIG
611 * @oh: struct omap_hwmod *
612 *
613 * If module is marked as SWSUP_SIDLE, force the module into slave
614 * idle; otherwise, configure it for smart-idle. If module is marked
615 * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
616 * configure it for smart-standby. No return value.
617 */
618static void _sysc_idle(struct omap_hwmod *oh)
619{
620 u8 idlemode;
621 u32 v;
622
623 if (!oh->sysconfig)
624 return;
625
626 v = oh->_sysc_cache;
627
628 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
629 idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
630 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
631 _set_slave_idlemode(oh, idlemode, &v);
632 }
633
634 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
635 idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
636 HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
637 _set_master_standbymode(oh, idlemode, &v);
638 }
639
640 _write_sysconfig(v, oh);
641}
642
643/**
644 * _sysc_shutdown - force a module into idle via OCP_SYSCONFIG
645 * @oh: struct omap_hwmod *
646 *
647 * Force the module into slave idle and master suspend. No return
648 * value.
649 */
650static void _sysc_shutdown(struct omap_hwmod *oh)
651{
652 u32 v;
653
654 if (!oh->sysconfig)
655 return;
656
657 v = oh->_sysc_cache;
658
659 if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE)
660 _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
661
662 if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE)
663 _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
664
Paul Walmsley726072e2009-12-08 16:34:15 -0700665 if (oh->sysconfig->sysc_flags & SYSC_HAS_AUTOIDLE)
666 _set_module_autoidle(oh, 1, &v);
Paul Walmsley63c85232009-09-03 20:14:03 +0300667
668 _write_sysconfig(v, oh);
669}
670
671/**
672 * _lookup - find an omap_hwmod by name
673 * @name: find an omap_hwmod by name
674 *
675 * Return a pointer to an omap_hwmod by name, or NULL if not found.
676 * Caller must hold omap_hwmod_mutex.
677 */
678static struct omap_hwmod *_lookup(const char *name)
679{
680 struct omap_hwmod *oh, *temp_oh;
681
682 oh = NULL;
683
684 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
685 if (!strcmp(name, temp_oh->name)) {
686 oh = temp_oh;
687 break;
688 }
689 }
690
691 return oh;
692}
693
694/**
695 * _init_clocks - clk_get() all clocks associated with this hwmod
696 * @oh: struct omap_hwmod *
697 *
698 * Called by omap_hwmod_late_init() (after omap2_clk_init()).
699 * Resolves all clock names embedded in the hwmod. Must be called
700 * with omap_hwmod_mutex held. Returns -EINVAL if the omap_hwmod
701 * has not yet been registered or if the clocks have already been
702 * initialized, 0 on success, or a non-zero error on failure.
703 */
704static int _init_clocks(struct omap_hwmod *oh)
705{
706 int ret = 0;
707
708 if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
709 return -EINVAL;
710
711 pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
712
713 ret |= _init_main_clk(oh);
714 ret |= _init_interface_clks(oh);
715 ret |= _init_opt_clks(oh);
716
717 oh->_state = _HWMOD_STATE_CLKS_INITED;
718
719 return ret;
720}
721
722/**
723 * _wait_target_ready - wait for a module to leave slave idle
724 * @oh: struct omap_hwmod *
725 *
726 * Wait for a module @oh to leave slave idle. Returns 0 if the module
727 * does not have an IDLEST bit or if the module successfully leaves
728 * slave idle; otherwise, pass along the return value of the
729 * appropriate *_cm_wait_module_ready() function.
730 */
731static int _wait_target_ready(struct omap_hwmod *oh)
732{
733 struct omap_hwmod_ocp_if *os;
734 int ret;
735
736 if (!oh)
737 return -EINVAL;
738
739 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
740 return 0;
741
742 os = *oh->slaves + oh->_mpu_port_index;
743
744 if (!(os->flags & OCPIF_HAS_IDLEST))
745 return 0;
746
747 /* XXX check module SIDLEMODE */
748
749 /* XXX check clock enable states */
750
751 if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
752 ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
753 oh->prcm.omap2.idlest_reg_id,
754 oh->prcm.omap2.idlest_idle_bit);
755#if 0
756 } else if (cpu_is_omap44xx()) {
757 ret = omap4_cm_wait_module_ready(oh->prcm.omap4.module_offs,
758 oh->prcm.omap4.device_offs);
759#endif
760 } else {
761 BUG();
762 };
763
764 return ret;
765}
766
767/**
768 * _reset - reset an omap_hwmod
769 * @oh: struct omap_hwmod *
770 *
771 * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
772 * enabled for this to work. Must be called with omap_hwmod_mutex
773 * held. Returns -EINVAL if the hwmod cannot be reset this way or if
774 * the hwmod is in the wrong state, -ETIMEDOUT if the module did not
775 * reset in time, or 0 upon success.
776 */
777static int _reset(struct omap_hwmod *oh)
778{
779 u32 r, v;
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -0700780 int c = 0;
Paul Walmsley63c85232009-09-03 20:14:03 +0300781
782 if (!oh->sysconfig ||
783 !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET) ||
784 (oh->sysconfig->sysc_flags & SYSS_MISSING))
785 return -EINVAL;
786
787 /* clocks must be on for this operation */
788 if (oh->_state != _HWMOD_STATE_ENABLED) {
789 WARN(1, "omap_hwmod: %s: reset can only be entered from "
790 "enabled state\n", oh->name);
791 return -EINVAL;
792 }
793
794 pr_debug("omap_hwmod: %s: resetting\n", oh->name);
795
796 v = oh->_sysc_cache;
797 r = _set_softreset(oh, &v);
798 if (r)
799 return r;
800 _write_sysconfig(v, oh);
801
Paul Walmsley6f8b7ff2009-12-08 16:33:16 -0700802 omap_test_timeout((omap_hwmod_readl(oh, oh->sysconfig->syss_offs) &
803 SYSS_RESETDONE_MASK),
804 MAX_MODULE_RESET_WAIT, c);
Paul Walmsley63c85232009-09-03 20:14:03 +0300805
806 if (c == MAX_MODULE_RESET_WAIT)
807 WARN(1, "omap_hwmod: %s: failed to reset in %d usec\n",
808 oh->name, MAX_MODULE_RESET_WAIT);
809 else
Paul Walmsley02bfc032009-09-03 20:14:05 +0300810 pr_debug("omap_hwmod: %s: reset in %d usec\n", oh->name, c);
Paul Walmsley63c85232009-09-03 20:14:03 +0300811
812 /*
813 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
814 * _wait_target_ready() or _reset()
815 */
816
817 return (c == MAX_MODULE_RESET_WAIT) ? -ETIMEDOUT : 0;
818}
819
820/**
821 * _enable - enable an omap_hwmod
822 * @oh: struct omap_hwmod *
823 *
824 * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
825 * register target. Must be called with omap_hwmod_mutex held.
826 * Returns -EINVAL if the hwmod is in the wrong state or passes along
827 * the return value of _wait_target_ready().
828 */
829static int _enable(struct omap_hwmod *oh)
830{
831 int r;
832
833 if (oh->_state != _HWMOD_STATE_INITIALIZED &&
834 oh->_state != _HWMOD_STATE_IDLE &&
835 oh->_state != _HWMOD_STATE_DISABLED) {
836 WARN(1, "omap_hwmod: %s: enabled state can only be entered "
837 "from initialized, idle, or disabled state\n", oh->name);
838 return -EINVAL;
839 }
840
841 pr_debug("omap_hwmod: %s: enabling\n", oh->name);
842
843 /* XXX mux balls */
844
845 _add_initiator_dep(oh, mpu_oh);
846 _enable_clocks(oh);
847
848 if (oh->sysconfig) {
849 if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
850 _update_sysc_cache(oh);
851 _sysc_enable(oh);
852 }
853
854 r = _wait_target_ready(oh);
855 if (!r)
856 oh->_state = _HWMOD_STATE_ENABLED;
857
858 return r;
859}
860
861/**
862 * _idle - idle an omap_hwmod
863 * @oh: struct omap_hwmod *
864 *
865 * Idles an omap_hwmod @oh. This should be called once the hwmod has
866 * no further work. Returns -EINVAL if the hwmod is in the wrong
867 * state or returns 0.
868 */
869static int _idle(struct omap_hwmod *oh)
870{
871 if (oh->_state != _HWMOD_STATE_ENABLED) {
872 WARN(1, "omap_hwmod: %s: idle state can only be entered from "
873 "enabled state\n", oh->name);
874 return -EINVAL;
875 }
876
877 pr_debug("omap_hwmod: %s: idling\n", oh->name);
878
879 if (oh->sysconfig)
880 _sysc_idle(oh);
881 _del_initiator_dep(oh, mpu_oh);
882 _disable_clocks(oh);
883
884 oh->_state = _HWMOD_STATE_IDLE;
885
886 return 0;
887}
888
889/**
890 * _shutdown - shutdown an omap_hwmod
891 * @oh: struct omap_hwmod *
892 *
893 * Shut down an omap_hwmod @oh. This should be called when the driver
894 * used for the hwmod is removed or unloaded or if the driver is not
895 * used by the system. Returns -EINVAL if the hwmod is in the wrong
896 * state or returns 0.
897 */
898static int _shutdown(struct omap_hwmod *oh)
899{
900 if (oh->_state != _HWMOD_STATE_IDLE &&
901 oh->_state != _HWMOD_STATE_ENABLED) {
902 WARN(1, "omap_hwmod: %s: disabled state can only be entered "
903 "from idle, or enabled state\n", oh->name);
904 return -EINVAL;
905 }
906
907 pr_debug("omap_hwmod: %s: disabling\n", oh->name);
908
909 if (oh->sysconfig)
910 _sysc_shutdown(oh);
911 _del_initiator_dep(oh, mpu_oh);
912 /* XXX what about the other system initiators here? DMA, tesla, d2d */
913 _disable_clocks(oh);
914 /* XXX Should this code also force-disable the optional clocks? */
915
916 /* XXX mux any associated balls to safe mode */
917
918 oh->_state = _HWMOD_STATE_DISABLED;
919
920 return 0;
921}
922
923/**
Paul Walmsley63c85232009-09-03 20:14:03 +0300924 * _setup - do initial configuration of omap_hwmod
925 * @oh: struct omap_hwmod *
926 *
927 * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
928 * OCP_SYSCONFIG register. Must be called with omap_hwmod_mutex
929 * held. Returns -EINVAL if the hwmod is in the wrong state or returns
930 * 0.
931 */
932static int _setup(struct omap_hwmod *oh)
933{
934 struct omap_hwmod_ocp_if *os;
935 int i;
936
937 if (!oh)
938 return -EINVAL;
939
940 /* Set iclk autoidle mode */
941 if (oh->slaves_cnt > 0) {
942 for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
943 struct clk *c = os->_clk;
944
945 if (!c || IS_ERR(c))
946 continue;
947
948 if (os->flags & OCPIF_SWSUP_IDLE) {
949 /* XXX omap_iclk_deny_idle(c); */
950 } else {
951 /* XXX omap_iclk_allow_idle(c); */
952 clk_enable(c);
953 }
954 }
955 }
956
957 oh->_state = _HWMOD_STATE_INITIALIZED;
958
959 _enable(oh);
960
Paul Walmsleyb835d012009-12-08 16:34:14 -0700961 if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
962 /*
963 * XXX Do the OCP_SYSCONFIG bits need to be
964 * reprogrammed after a reset? If not, then this can
965 * be removed. If they do, then probably the
966 * _enable() function should be split to avoid the
967 * rewrite of the OCP_SYSCONFIG register.
968 */
969 if (oh->sysconfig) {
970 _update_sysc_cache(oh);
971 _sysc_enable(oh);
972 }
973 }
Paul Walmsley63c85232009-09-03 20:14:03 +0300974
975 if (!(oh->flags & HWMOD_INIT_NO_IDLE))
976 _idle(oh);
977
978 return 0;
979}
980
981
982
983/* Public functions */
984
985u32 omap_hwmod_readl(struct omap_hwmod *oh, u16 reg_offs)
986{
987 return __raw_readl(oh->_rt_va + reg_offs);
988}
989
990void omap_hwmod_writel(u32 v, struct omap_hwmod *oh, u16 reg_offs)
991{
992 __raw_writel(v, oh->_rt_va + reg_offs);
993}
994
995/**
996 * omap_hwmod_register - register a struct omap_hwmod
997 * @oh: struct omap_hwmod *
998 *
999 * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod already
1000 * has been registered by the same name; -EINVAL if the omap_hwmod is in the
1001 * wrong state, or 0 on success.
1002 *
1003 * XXX The data should be copied into bootmem, so the original data
1004 * should be marked __initdata and freed after init. This would allow
1005 * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
1006 * that the copy process would be relatively complex due to the large number
1007 * of substructures.
1008 */
1009int omap_hwmod_register(struct omap_hwmod *oh)
1010{
1011 int ret, ms_id;
1012
1013 if (!oh || (oh->_state != _HWMOD_STATE_UNKNOWN))
1014 return -EINVAL;
1015
1016 mutex_lock(&omap_hwmod_mutex);
1017
1018 pr_debug("omap_hwmod: %s: registering\n", oh->name);
1019
1020 if (_lookup(oh->name)) {
1021 ret = -EEXIST;
1022 goto ohr_unlock;
1023 }
1024
1025 ms_id = _find_mpu_port_index(oh);
1026 if (!IS_ERR_VALUE(ms_id)) {
1027 oh->_mpu_port_index = ms_id;
1028 oh->_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
1029 } else {
1030 oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1031 }
1032
1033 list_add_tail(&oh->node, &omap_hwmod_list);
1034
1035 oh->_state = _HWMOD_STATE_REGISTERED;
1036
1037 ret = 0;
1038
1039ohr_unlock:
1040 mutex_unlock(&omap_hwmod_mutex);
1041 return ret;
1042}
1043
1044/**
1045 * omap_hwmod_lookup - look up a registered omap_hwmod by name
1046 * @name: name of the omap_hwmod to look up
1047 *
1048 * Given a @name of an omap_hwmod, return a pointer to the registered
1049 * struct omap_hwmod *, or NULL upon error.
1050 */
1051struct omap_hwmod *omap_hwmod_lookup(const char *name)
1052{
1053 struct omap_hwmod *oh;
1054
1055 if (!name)
1056 return NULL;
1057
1058 mutex_lock(&omap_hwmod_mutex);
1059 oh = _lookup(name);
1060 mutex_unlock(&omap_hwmod_mutex);
1061
1062 return oh;
1063}
1064
1065/**
1066 * omap_hwmod_for_each - call function for each registered omap_hwmod
1067 * @fn: pointer to a callback function
1068 *
1069 * Call @fn for each registered omap_hwmod, passing @data to each
1070 * function. @fn must return 0 for success or any other value for
1071 * failure. If @fn returns non-zero, the iteration across omap_hwmods
1072 * will stop and the non-zero return value will be passed to the
1073 * caller of omap_hwmod_for_each(). @fn is called with
1074 * omap_hwmod_for_each() held.
1075 */
1076int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh))
1077{
1078 struct omap_hwmod *temp_oh;
1079 int ret;
1080
1081 if (!fn)
1082 return -EINVAL;
1083
1084 mutex_lock(&omap_hwmod_mutex);
1085 list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1086 ret = (*fn)(temp_oh);
1087 if (ret)
1088 break;
1089 }
1090 mutex_unlock(&omap_hwmod_mutex);
1091
1092 return ret;
1093}
1094
1095
1096/**
1097 * omap_hwmod_init - init omap_hwmod code and register hwmods
1098 * @ohs: pointer to an array of omap_hwmods to register
1099 *
1100 * Intended to be called early in boot before the clock framework is
1101 * initialized. If @ohs is not null, will register all omap_hwmods
1102 * listed in @ohs that are valid for this chip. Returns -EINVAL if
1103 * omap_hwmod_init() has already been called or 0 otherwise.
1104 */
1105int omap_hwmod_init(struct omap_hwmod **ohs)
1106{
1107 struct omap_hwmod *oh;
1108 int r;
1109
1110 if (inited)
1111 return -EINVAL;
1112
1113 inited = 1;
1114
1115 if (!ohs)
1116 return 0;
1117
1118 oh = *ohs;
1119 while (oh) {
1120 if (omap_chip_is(oh->omap_chip)) {
1121 r = omap_hwmod_register(oh);
1122 WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
1123 "%d\n", oh->name, r);
1124 }
1125 oh = *++ohs;
1126 }
1127
1128 return 0;
1129}
1130
1131/**
1132 * omap_hwmod_late_init - do some post-clock framework initialization
1133 *
1134 * Must be called after omap2_clk_init(). Resolves the struct clk names
1135 * to struct clk pointers for each registered omap_hwmod. Also calls
1136 * _setup() on each hwmod. Returns 0.
1137 */
1138int omap_hwmod_late_init(void)
1139{
1140 int r;
1141
1142 /* XXX check return value */
1143 r = omap_hwmod_for_each(_init_clocks);
1144 WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
1145
1146 mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
1147 WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
1148 MPU_INITIATOR_NAME);
1149
1150 omap_hwmod_for_each(_setup);
1151
1152 return 0;
1153}
1154
1155/**
1156 * omap_hwmod_unregister - unregister an omap_hwmod
1157 * @oh: struct omap_hwmod *
1158 *
1159 * Unregisters a previously-registered omap_hwmod @oh. There's probably
1160 * no use case for this, so it is likely to be removed in a later version.
1161 *
1162 * XXX Free all of the bootmem-allocated structures here when that is
1163 * implemented. Make it clear that core code is the only code that is
1164 * expected to unregister modules.
1165 */
1166int omap_hwmod_unregister(struct omap_hwmod *oh)
1167{
1168 if (!oh)
1169 return -EINVAL;
1170
1171 pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
1172
1173 mutex_lock(&omap_hwmod_mutex);
Tony Lindgren986a13f2009-10-19 15:25:22 -07001174 iounmap(oh->_rt_va);
Paul Walmsley63c85232009-09-03 20:14:03 +03001175 list_del(&oh->node);
1176 mutex_unlock(&omap_hwmod_mutex);
1177
1178 return 0;
1179}
1180
1181/**
1182 * omap_hwmod_enable - enable an omap_hwmod
1183 * @oh: struct omap_hwmod *
1184 *
1185 * Enable an omap_hwomd @oh. Intended to be called by omap_device_enable().
1186 * Returns -EINVAL on error or passes along the return value from _enable().
1187 */
1188int omap_hwmod_enable(struct omap_hwmod *oh)
1189{
1190 int r;
1191
1192 if (!oh)
1193 return -EINVAL;
1194
1195 mutex_lock(&omap_hwmod_mutex);
1196 r = _enable(oh);
1197 mutex_unlock(&omap_hwmod_mutex);
1198
1199 return r;
1200}
1201
1202/**
1203 * omap_hwmod_idle - idle an omap_hwmod
1204 * @oh: struct omap_hwmod *
1205 *
1206 * Idle an omap_hwomd @oh. Intended to be called by omap_device_idle().
1207 * Returns -EINVAL on error or passes along the return value from _idle().
1208 */
1209int omap_hwmod_idle(struct omap_hwmod *oh)
1210{
1211 if (!oh)
1212 return -EINVAL;
1213
1214 mutex_lock(&omap_hwmod_mutex);
1215 _idle(oh);
1216 mutex_unlock(&omap_hwmod_mutex);
1217
1218 return 0;
1219}
1220
1221/**
1222 * omap_hwmod_shutdown - shutdown an omap_hwmod
1223 * @oh: struct omap_hwmod *
1224 *
1225 * Shutdown an omap_hwomd @oh. Intended to be called by
1226 * omap_device_shutdown(). Returns -EINVAL on error or passes along
1227 * the return value from _shutdown().
1228 */
1229int omap_hwmod_shutdown(struct omap_hwmod *oh)
1230{
1231 if (!oh)
1232 return -EINVAL;
1233
1234 mutex_lock(&omap_hwmod_mutex);
1235 _shutdown(oh);
1236 mutex_unlock(&omap_hwmod_mutex);
1237
1238 return 0;
1239}
1240
1241/**
1242 * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
1243 * @oh: struct omap_hwmod *oh
1244 *
1245 * Intended to be called by the omap_device code.
1246 */
1247int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
1248{
1249 mutex_lock(&omap_hwmod_mutex);
1250 _enable_clocks(oh);
1251 mutex_unlock(&omap_hwmod_mutex);
1252
1253 return 0;
1254}
1255
1256/**
1257 * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
1258 * @oh: struct omap_hwmod *oh
1259 *
1260 * Intended to be called by the omap_device code.
1261 */
1262int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
1263{
1264 mutex_lock(&omap_hwmod_mutex);
1265 _disable_clocks(oh);
1266 mutex_unlock(&omap_hwmod_mutex);
1267
1268 return 0;
1269}
1270
1271/**
1272 * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
1273 * @oh: struct omap_hwmod *oh
1274 *
1275 * Intended to be called by drivers and core code when all posted
1276 * writes to a device must complete before continuing further
1277 * execution (for example, after clearing some device IRQSTATUS
1278 * register bits)
1279 *
1280 * XXX what about targets with multiple OCP threads?
1281 */
1282void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
1283{
1284 BUG_ON(!oh);
1285
1286 if (!oh->sysconfig || !oh->sysconfig->sysc_flags) {
1287 WARN(1, "omap_device: %s: OCP barrier impossible due to "
1288 "device configuration\n", oh->name);
1289 return;
1290 }
1291
1292 /*
1293 * Forces posted writes to complete on the OCP thread handling
1294 * register writes
1295 */
1296 omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
1297}
1298
1299/**
1300 * omap_hwmod_reset - reset the hwmod
1301 * @oh: struct omap_hwmod *
1302 *
1303 * Under some conditions, a driver may wish to reset the entire device.
1304 * Called from omap_device code. Returns -EINVAL on error or passes along
1305 * the return value from _reset()/_enable().
1306 */
1307int omap_hwmod_reset(struct omap_hwmod *oh)
1308{
1309 int r;
1310
1311 if (!oh || !(oh->_state & _HWMOD_STATE_ENABLED))
1312 return -EINVAL;
1313
1314 mutex_lock(&omap_hwmod_mutex);
1315 r = _reset(oh);
1316 if (!r)
1317 r = _enable(oh);
1318 mutex_unlock(&omap_hwmod_mutex);
1319
1320 return r;
1321}
1322
1323/**
1324 * omap_hwmod_count_resources - count number of struct resources needed by hwmod
1325 * @oh: struct omap_hwmod *
1326 * @res: pointer to the first element of an array of struct resource to fill
1327 *
1328 * Count the number of struct resource array elements necessary to
1329 * contain omap_hwmod @oh resources. Intended to be called by code
1330 * that registers omap_devices. Intended to be used to determine the
1331 * size of a dynamically-allocated struct resource array, before
1332 * calling omap_hwmod_fill_resources(). Returns the number of struct
1333 * resource array elements needed.
1334 *
1335 * XXX This code is not optimized. It could attempt to merge adjacent
1336 * resource IDs.
1337 *
1338 */
1339int omap_hwmod_count_resources(struct omap_hwmod *oh)
1340{
1341 int ret, i;
1342
1343 ret = oh->mpu_irqs_cnt + oh->sdma_chs_cnt;
1344
1345 for (i = 0; i < oh->slaves_cnt; i++)
1346 ret += (*oh->slaves + i)->addr_cnt;
1347
1348 return ret;
1349}
1350
1351/**
1352 * omap_hwmod_fill_resources - fill struct resource array with hwmod data
1353 * @oh: struct omap_hwmod *
1354 * @res: pointer to the first element of an array of struct resource to fill
1355 *
1356 * Fill the struct resource array @res with resource data from the
1357 * omap_hwmod @oh. Intended to be called by code that registers
1358 * omap_devices. See also omap_hwmod_count_resources(). Returns the
1359 * number of array elements filled.
1360 */
1361int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
1362{
1363 int i, j;
1364 int r = 0;
1365
1366 /* For each IRQ, DMA, memory area, fill in array.*/
1367
1368 for (i = 0; i < oh->mpu_irqs_cnt; i++) {
Paul Walmsley718bfd72009-12-08 16:34:16 -07001369 (res + r)->name = (oh->mpu_irqs + i)->name;
1370 (res + r)->start = (oh->mpu_irqs + i)->irq;
1371 (res + r)->end = (oh->mpu_irqs + i)->irq;
Paul Walmsley63c85232009-09-03 20:14:03 +03001372 (res + r)->flags = IORESOURCE_IRQ;
1373 r++;
1374 }
1375
1376 for (i = 0; i < oh->sdma_chs_cnt; i++) {
1377 (res + r)->name = (oh->sdma_chs + i)->name;
1378 (res + r)->start = (oh->sdma_chs + i)->dma_ch;
1379 (res + r)->end = (oh->sdma_chs + i)->dma_ch;
1380 (res + r)->flags = IORESOURCE_DMA;
1381 r++;
1382 }
1383
1384 for (i = 0; i < oh->slaves_cnt; i++) {
1385 struct omap_hwmod_ocp_if *os;
1386
1387 os = *oh->slaves + i;
1388
1389 for (j = 0; j < os->addr_cnt; j++) {
1390 (res + r)->start = (os->addr + j)->pa_start;
1391 (res + r)->end = (os->addr + j)->pa_end;
1392 (res + r)->flags = IORESOURCE_MEM;
1393 r++;
1394 }
1395 }
1396
1397 return r;
1398}
1399
1400/**
1401 * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
1402 * @oh: struct omap_hwmod *
1403 *
1404 * Return the powerdomain pointer associated with the OMAP module
1405 * @oh's main clock. If @oh does not have a main clk, return the
1406 * powerdomain associated with the interface clock associated with the
1407 * module's MPU port. (XXX Perhaps this should use the SDMA port
1408 * instead?) Returns NULL on error, or a struct powerdomain * on
1409 * success.
1410 */
1411struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
1412{
1413 struct clk *c;
1414
1415 if (!oh)
1416 return NULL;
1417
1418 if (oh->_clk) {
1419 c = oh->_clk;
1420 } else {
1421 if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
1422 return NULL;
1423 c = oh->slaves[oh->_mpu_port_index]->_clk;
1424 }
1425
1426 return c->clkdm->pwrdm.ptr;
1427
1428}
1429
1430/**
1431 * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
1432 * @oh: struct omap_hwmod *
1433 * @init_oh: struct omap_hwmod * (initiator)
1434 *
1435 * Add a sleep dependency between the initiator @init_oh and @oh.
1436 * Intended to be called by DSP/Bridge code via platform_data for the
1437 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1438 * code needs to add/del initiator dependencies dynamically
1439 * before/after accessing a device. Returns the return value from
1440 * _add_initiator_dep().
1441 *
1442 * XXX Keep a usecount in the clockdomain code
1443 */
1444int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
1445 struct omap_hwmod *init_oh)
1446{
1447 return _add_initiator_dep(oh, init_oh);
1448}
1449
1450/*
1451 * XXX what about functions for drivers to save/restore ocp_sysconfig
1452 * for context save/restore operations?
1453 */
1454
1455/**
1456 * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
1457 * @oh: struct omap_hwmod *
1458 * @init_oh: struct omap_hwmod * (initiator)
1459 *
1460 * Remove a sleep dependency between the initiator @init_oh and @oh.
1461 * Intended to be called by DSP/Bridge code via platform_data for the
1462 * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
1463 * code needs to add/del initiator dependencies dynamically
1464 * before/after accessing a device. Returns the return value from
1465 * _del_initiator_dep().
1466 *
1467 * XXX Keep a usecount in the clockdomain code
1468 */
1469int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
1470 struct omap_hwmod *init_oh)
1471{
1472 return _del_initiator_dep(oh, init_oh);
1473}
1474
1475/**
Paul Walmsley63c85232009-09-03 20:14:03 +03001476 * omap_hwmod_enable_wakeup - allow device to wake up the system
1477 * @oh: struct omap_hwmod *
1478 *
1479 * Sets the module OCP socket ENAWAKEUP bit to allow the module to
1480 * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
1481 * registers to cause the PRCM to receive wakeup events from the
1482 * module. Does not set any wakeup routing registers beyond this
1483 * point - if the module is to wake up any other module or subsystem,
1484 * that must be set separately. Called by omap_device code. Returns
1485 * -EINVAL on error or 0 upon success.
1486 */
1487int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
1488{
1489 if (!oh->sysconfig ||
1490 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
1491 return -EINVAL;
1492
1493 mutex_lock(&omap_hwmod_mutex);
1494 _enable_wakeup(oh);
1495 mutex_unlock(&omap_hwmod_mutex);
1496
1497 return 0;
1498}
1499
1500/**
1501 * omap_hwmod_disable_wakeup - prevent device from waking the system
1502 * @oh: struct omap_hwmod *
1503 *
1504 * Clears the module OCP socket ENAWAKEUP bit to prevent the module
1505 * from sending wakeups to the PRCM. Eventually this should clear
1506 * PRCM wakeup registers to cause the PRCM to ignore wakeup events
1507 * from the module. Does not set any wakeup routing registers beyond
1508 * this point - if the module is to wake up any other module or
1509 * subsystem, that must be set separately. Called by omap_device
1510 * code. Returns -EINVAL on error or 0 upon success.
1511 */
1512int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
1513{
1514 if (!oh->sysconfig ||
1515 !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
1516 return -EINVAL;
1517
1518 mutex_lock(&omap_hwmod_mutex);
1519 _disable_wakeup(oh);
1520 mutex_unlock(&omap_hwmod_mutex);
1521
1522 return 0;
1523}