1 /*
2  * omap_hwmod implementation for OMAP2/3/4
3  *
4  * Copyright (C) 2009-2011 Nokia Corporation
5  * Copyright (C) 2011-2012 Texas Instruments, Inc.
6  *
7  * Paul Walmsley, Benoît Cousson, Kevin Hilman
8  *
9  * Created in collaboration with (alphabetical order): Thara Gopinath,
10  * Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari Poussa, Anand
11  * Sawant, Santosh Shilimkar, Richard Woodruff
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  *
17  * Introduction
18  * ------------
19  * One way to view an OMAP SoC is as a collection of largely unrelated
20  * IP blocks connected by interconnects.  The IP blocks include
21  * devices such as ARM processors, audio serial interfaces, UARTs,
22  * etc.  Some of these devices, like the DSP, are created by TI;
23  * others, like the SGX, largely originate from external vendors.  In
24  * TI's documentation, on-chip devices are referred to as "OMAP
25  * modules."  Some of these IP blocks are identical across several
26  * OMAP versions.  Others are revised frequently.
27  *
28  * These OMAP modules are tied together by various interconnects.
29  * Most of the address and data flow between modules is via OCP-based
30  * interconnects such as the L3 and L4 buses; but there are other
31  * interconnects that distribute the hardware clock tree, handle idle
32  * and reset signaling, supply power, and connect the modules to
33  * various pads or balls on the OMAP package.
34  *
35  * OMAP hwmod provides a consistent way to describe the on-chip
36  * hardware blocks and their integration into the rest of the chip.
37  * This description can be automatically generated from the TI
38  * hardware database.  OMAP hwmod provides a standard, consistent API
39  * to reset, enable, idle, and disable these hardware blocks.  And
40  * hwmod provides a way for other core code, such as the Linux device
41  * code or the OMAP power management and address space mapping code,
42  * to query the hardware database.
43  *
44  * Using hwmod
45  * -----------
46  * Drivers won't call hwmod functions directly.  That is done by the
47  * omap_device code, and in rare occasions, by custom integration code
48  * in arch/arm/ *omap*.  The omap_device code includes functions to
49  * build a struct platform_device using omap_hwmod data, and that is
50  * currently how hwmod data is communicated to drivers and to the
51  * Linux driver model.  Most drivers will call omap_hwmod functions only
52  * indirectly, via pm_runtime*() functions.
53  *
54  * From a layering perspective, here is where the OMAP hwmod code
55  * fits into the kernel software stack:
56  *
57  *            +-------------------------------+
58  *            |      Device driver code       |
59  *            |      (e.g., drivers/)         |
60  *            +-------------------------------+
61  *            |      Linux driver model       |
62  *            |     (platform_device /        |
63  *            |  platform_driver data/code)   |
64  *            +-------------------------------+
65  *            | OMAP core-driver integration  |
66  *            |(arch/arm/mach-omap2/devices.c)|
67  *            +-------------------------------+
68  *            |      omap_device code         |
69  *            | (../plat-omap/omap_device.c)  |
70  *            +-------------------------------+
71  *   ---->    |    omap_hwmod code/data       |    <-----
72  *            | (../mach-omap2/omap_hwmod*)   |
73  *            +-------------------------------+
74  *            | OMAP clock/PRCM/register fns  |
75  *            | ({read,write}l_relaxed, clk*) |
76  *            +-------------------------------+
77  *
78  * Device drivers should not contain any OMAP-specific code or data in
79  * them.  They should only contain code to operate the IP block that
80  * the driver is responsible for.  This is because these IP blocks can
81  * also appear in other SoCs, either from TI (such as DaVinci) or from
82  * other manufacturers; and drivers should be reusable across other
83  * platforms.
84  *
85  * The OMAP hwmod code also will attempt to reset and idle all on-chip
86  * devices upon boot.  The goal here is for the kernel to be
87  * completely self-reliant and independent from bootloaders.  This is
88  * to ensure a repeatable configuration, both to ensure consistent
89  * runtime behavior, and to make it easier for others to reproduce
90  * bugs.
91  *
92  * OMAP module activity states
93  * ---------------------------
94  * The hwmod code considers modules to be in one of several activity
95  * states.  IP blocks start out in an UNKNOWN state, then once they
96  * are registered via the hwmod code, proceed to the REGISTERED state.
97  * Once their clock names are resolved to clock pointers, the module
98  * enters the CLKS_INITED state; and finally, once the module has been
99  * reset and the integration registers programmed, the INITIALIZED state
100  * is entered.  The hwmod code will then place the module into either
101  * the IDLE state to save power, or in the case of a critical system
102  * module, the ENABLED state.
103  *
104  * OMAP core integration code can then call omap_hwmod*() functions
105  * directly to move the module between the IDLE, ENABLED, and DISABLED
106  * states, as needed.  This is done during both the PM idle loop, and
107  * in the OMAP core integration code's implementation of the PM runtime
108  * functions.
109  *
110  * References
111  * ----------
112  * This is a partial list.
113  * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
114  * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
115  * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
116  * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
117  * - Open Core Protocol Specification 2.2
118  *
119  * To do:
120  * - handle IO mapping
121  * - bus throughput & module latency measurement code
122  *
123  * XXX add tests at the beginning of each function to ensure the hwmod is
124  * in the appropriate state
125  * XXX error return values should be checked to ensure that they are
126  * appropriate
127  */
128 #undef DEBUG
129 
130 #include <linux/kernel.h>
131 #include <linux/errno.h>
132 #include <linux/io.h>
133 #include <linux/clk-provider.h>
134 #include <linux/delay.h>
135 #include <linux/err.h>
136 #include <linux/list.h>
137 #include <linux/mutex.h>
138 #include <linux/spinlock.h>
139 #include <linux/slab.h>
140 #include <linux/bootmem.h>
141 #include <linux/cpu.h>
142 #include <linux/of.h>
143 #include <linux/of_address.h>
144 
145 #include <asm/system_misc.h>
146 
147 #include "clock.h"
148 #include "omap_hwmod.h"
149 
150 #include "soc.h"
151 #include "common.h"
152 #include "clockdomain.h"
153 #include "powerdomain.h"
154 #include "cm2xxx.h"
155 #include "cm3xxx.h"
156 #include "cm33xx.h"
157 #include "prm.h"
158 #include "prm3xxx.h"
159 #include "prm44xx.h"
160 #include "prm33xx.h"
161 #include "prminst44xx.h"
162 #include "mux.h"
163 #include "pm.h"
164 
165 /* Name of the OMAP hwmod for the MPU */
166 #define MPU_INITIATOR_NAME		"mpu"
167 
168 /*
169  * Number of struct omap_hwmod_link records per struct
170  * omap_hwmod_ocp_if record (master->slave and slave->master)
171  */
172 #define LINKS_PER_OCP_IF		2
173 
174 /*
175  * Address offset (in bytes) between the reset control and the reset
176  * status registers: 4 bytes on OMAP4
177  */
178 #define OMAP4_RST_CTRL_ST_OFFSET	4
179 
180 /**
181  * struct omap_hwmod_soc_ops - fn ptrs for some SoC-specific operations
182  * @enable_module: function to enable a module (via MODULEMODE)
183  * @disable_module: function to disable a module (via MODULEMODE)
184  *
185  * XXX Eventually this functionality will be hidden inside the PRM/CM
186  * device drivers.  Until then, this should avoid huge blocks of cpu_is_*()
187  * conditionals in this code.
188  */
189 struct omap_hwmod_soc_ops {
190 	void (*enable_module)(struct omap_hwmod *oh);
191 	int (*disable_module)(struct omap_hwmod *oh);
192 	int (*wait_target_ready)(struct omap_hwmod *oh);
193 	int (*assert_hardreset)(struct omap_hwmod *oh,
194 				struct omap_hwmod_rst_info *ohri);
195 	int (*deassert_hardreset)(struct omap_hwmod *oh,
196 				  struct omap_hwmod_rst_info *ohri);
197 	int (*is_hardreset_asserted)(struct omap_hwmod *oh,
198 				     struct omap_hwmod_rst_info *ohri);
199 	int (*init_clkdm)(struct omap_hwmod *oh);
200 	void (*update_context_lost)(struct omap_hwmod *oh);
201 	int (*get_context_lost)(struct omap_hwmod *oh);
202 };
203 
204 /* soc_ops: adapts the omap_hwmod code to the currently-booted SoC */
205 static struct omap_hwmod_soc_ops soc_ops;
206 
207 /* omap_hwmod_list contains all registered struct omap_hwmods */
208 static LIST_HEAD(omap_hwmod_list);
209 
210 /* mpu_oh: used to add/remove MPU initiator from sleepdep list */
211 static struct omap_hwmod *mpu_oh;
212 
213 /* io_chain_lock: used to serialize reconfigurations of the I/O chain */
214 static DEFINE_SPINLOCK(io_chain_lock);
215 
216 /*
217  * linkspace: ptr to a buffer that struct omap_hwmod_link records are
218  * allocated from - used to reduce the number of small memory
219  * allocations, which has a significant impact on performance
220  */
221 static struct omap_hwmod_link *linkspace;
222 
223 /*
224  * free_ls, max_ls: array indexes into linkspace; representing the
225  * next free struct omap_hwmod_link index, and the maximum number of
226  * struct omap_hwmod_link records allocated (respectively)
227  */
228 static unsigned short free_ls, max_ls, ls_supp;
229 
230 /* inited: set to true once the hwmod code is initialized */
231 static bool inited;
232 
233 /* Private functions */
234 
235 /**
236  * _fetch_next_ocp_if - return the next OCP interface in a list
237  * @p: ptr to a ptr to the list_head inside the ocp_if to return
238  * @i: pointer to the index of the element pointed to by @p in the list
239  *
240  * Return a pointer to the struct omap_hwmod_ocp_if record
241  * containing the struct list_head pointed to by @p, and increment
242  * @p such that a future call to this routine will return the next
243  * record.
244  */
_fetch_next_ocp_if(struct list_head ** p,int * i)245 static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
246 						    int *i)
247 {
248 	struct omap_hwmod_ocp_if *oi;
249 
250 	oi = list_entry(*p, struct omap_hwmod_link, node)->ocp_if;
251 	*p = (*p)->next;
252 
253 	*i = *i + 1;
254 
255 	return oi;
256 }
257 
258 /**
259  * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
260  * @oh: struct omap_hwmod *
261  *
262  * Load the current value of the hwmod OCP_SYSCONFIG register into the
263  * struct omap_hwmod for later use.  Returns -EINVAL if the hwmod has no
264  * OCP_SYSCONFIG register or 0 upon success.
265  */
_update_sysc_cache(struct omap_hwmod * oh)266 static int _update_sysc_cache(struct omap_hwmod *oh)
267 {
268 	if (!oh->class->sysc) {
269 		WARN(1, "omap_hwmod: %s: cannot read OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
270 		return -EINVAL;
271 	}
272 
273 	/* XXX ensure module interface clock is up */
274 
275 	oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
276 
277 	if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
278 		oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
279 
280 	return 0;
281 }
282 
283 /**
284  * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
285  * @v: OCP_SYSCONFIG value to write
286  * @oh: struct omap_hwmod *
287  *
288  * Write @v into the module class' OCP_SYSCONFIG register, if it has
289  * one.  No return value.
290  */
_write_sysconfig(u32 v,struct omap_hwmod * oh)291 static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
292 {
293 	if (!oh->class->sysc) {
294 		WARN(1, "omap_hwmod: %s: cannot write OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
295 		return;
296 	}
297 
298 	/* XXX ensure module interface clock is up */
299 
300 	/* Module might have lost context, always update cache and register */
301 	oh->_sysc_cache = v;
302 	omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
303 }
304 
305 /**
306  * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
307  * @oh: struct omap_hwmod *
308  * @standbymode: MIDLEMODE field bits
309  * @v: pointer to register contents to modify
310  *
311  * Update the master standby mode bits in @v to be @standbymode for
312  * the @oh hwmod.  Does not write to the hardware.  Returns -EINVAL
313  * upon error or 0 upon success.
314  */
_set_master_standbymode(struct omap_hwmod * oh,u8 standbymode,u32 * v)315 static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
316 				   u32 *v)
317 {
318 	u32 mstandby_mask;
319 	u8 mstandby_shift;
320 
321 	if (!oh->class->sysc ||
322 	    !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
323 		return -EINVAL;
324 
325 	if (!oh->class->sysc->sysc_fields) {
326 		WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
327 		return -EINVAL;
328 	}
329 
330 	mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
331 	mstandby_mask = (0x3 << mstandby_shift);
332 
333 	*v &= ~mstandby_mask;
334 	*v |= __ffs(standbymode) << mstandby_shift;
335 
336 	return 0;
337 }
338 
339 /**
340  * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
341  * @oh: struct omap_hwmod *
342  * @idlemode: SIDLEMODE field bits
343  * @v: pointer to register contents to modify
344  *
345  * Update the slave idle mode bits in @v to be @idlemode for the @oh
346  * hwmod.  Does not write to the hardware.  Returns -EINVAL upon error
347  * or 0 upon success.
348  */
_set_slave_idlemode(struct omap_hwmod * oh,u8 idlemode,u32 * v)349 static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
350 {
351 	u32 sidle_mask;
352 	u8 sidle_shift;
353 
354 	if (!oh->class->sysc ||
355 	    !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
356 		return -EINVAL;
357 
358 	if (!oh->class->sysc->sysc_fields) {
359 		WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
360 		return -EINVAL;
361 	}
362 
363 	sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
364 	sidle_mask = (0x3 << sidle_shift);
365 
366 	*v &= ~sidle_mask;
367 	*v |= __ffs(idlemode) << sidle_shift;
368 
369 	return 0;
370 }
371 
372 /**
373  * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
374  * @oh: struct omap_hwmod *
375  * @clockact: CLOCKACTIVITY field bits
376  * @v: pointer to register contents to modify
377  *
378  * Update the clockactivity mode bits in @v to be @clockact for the
379  * @oh hwmod.  Used for additional powersaving on some modules.  Does
380  * not write to the hardware.  Returns -EINVAL upon error or 0 upon
381  * success.
382  */
_set_clockactivity(struct omap_hwmod * oh,u8 clockact,u32 * v)383 static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
384 {
385 	u32 clkact_mask;
386 	u8  clkact_shift;
387 
388 	if (!oh->class->sysc ||
389 	    !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
390 		return -EINVAL;
391 
392 	if (!oh->class->sysc->sysc_fields) {
393 		WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
394 		return -EINVAL;
395 	}
396 
397 	clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
398 	clkact_mask = (0x3 << clkact_shift);
399 
400 	*v &= ~clkact_mask;
401 	*v |= clockact << clkact_shift;
402 
403 	return 0;
404 }
405 
406 /**
407  * _set_softreset: set OCP_SYSCONFIG.SOFTRESET bit in @v
408  * @oh: struct omap_hwmod *
409  * @v: pointer to register contents to modify
410  *
411  * Set the SOFTRESET bit in @v for hwmod @oh.  Returns -EINVAL upon
412  * error or 0 upon success.
413  */
_set_softreset(struct omap_hwmod * oh,u32 * v)414 static int _set_softreset(struct omap_hwmod *oh, u32 *v)
415 {
416 	u32 softrst_mask;
417 
418 	if (!oh->class->sysc ||
419 	    !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
420 		return -EINVAL;
421 
422 	if (!oh->class->sysc->sysc_fields) {
423 		WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
424 		return -EINVAL;
425 	}
426 
427 	softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
428 
429 	*v |= softrst_mask;
430 
431 	return 0;
432 }
433 
434 /**
435  * _clear_softreset: clear OCP_SYSCONFIG.SOFTRESET bit in @v
436  * @oh: struct omap_hwmod *
437  * @v: pointer to register contents to modify
438  *
439  * Clear the SOFTRESET bit in @v for hwmod @oh.  Returns -EINVAL upon
440  * error or 0 upon success.
441  */
_clear_softreset(struct omap_hwmod * oh,u32 * v)442 static int _clear_softreset(struct omap_hwmod *oh, u32 *v)
443 {
444 	u32 softrst_mask;
445 
446 	if (!oh->class->sysc ||
447 	    !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
448 		return -EINVAL;
449 
450 	if (!oh->class->sysc->sysc_fields) {
451 		WARN(1,
452 		     "omap_hwmod: %s: sysc_fields absent for sysconfig class\n",
453 		     oh->name);
454 		return -EINVAL;
455 	}
456 
457 	softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
458 
459 	*v &= ~softrst_mask;
460 
461 	return 0;
462 }
463 
464 /**
465  * _wait_softreset_complete - wait for an OCP softreset to complete
466  * @oh: struct omap_hwmod * to wait on
467  *
468  * Wait until the IP block represented by @oh reports that its OCP
469  * softreset is complete.  This can be triggered by software (see
470  * _ocp_softreset()) or by hardware upon returning from off-mode (one
471  * example is HSMMC).  Waits for up to MAX_MODULE_SOFTRESET_WAIT
472  * microseconds.  Returns the number of microseconds waited.
473  */
_wait_softreset_complete(struct omap_hwmod * oh)474 static int _wait_softreset_complete(struct omap_hwmod *oh)
475 {
476 	struct omap_hwmod_class_sysconfig *sysc;
477 	u32 softrst_mask;
478 	int c = 0;
479 
480 	sysc = oh->class->sysc;
481 
482 	if (sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
483 		omap_test_timeout((omap_hwmod_read(oh, sysc->syss_offs)
484 				   & SYSS_RESETDONE_MASK),
485 				  MAX_MODULE_SOFTRESET_WAIT, c);
486 	else if (sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
487 		softrst_mask = (0x1 << sysc->sysc_fields->srst_shift);
488 		omap_test_timeout(!(omap_hwmod_read(oh, sysc->sysc_offs)
489 				    & softrst_mask),
490 				  MAX_MODULE_SOFTRESET_WAIT, c);
491 	}
492 
493 	return c;
494 }
495 
496 /**
497  * _set_dmadisable: set OCP_SYSCONFIG.DMADISABLE bit in @v
498  * @oh: struct omap_hwmod *
499  *
500  * The DMADISABLE bit is a semi-automatic bit present in sysconfig register
501  * of some modules. When the DMA must perform read/write accesses, the
502  * DMADISABLE bit is cleared by the hardware. But when the DMA must stop
503  * for power management, software must set the DMADISABLE bit back to 1.
504  *
505  * Set the DMADISABLE bit in @v for hwmod @oh.  Returns -EINVAL upon
506  * error or 0 upon success.
507  */
_set_dmadisable(struct omap_hwmod * oh)508 static int _set_dmadisable(struct omap_hwmod *oh)
509 {
510 	u32 v;
511 	u32 dmadisable_mask;
512 
513 	if (!oh->class->sysc ||
514 	    !(oh->class->sysc->sysc_flags & SYSC_HAS_DMADISABLE))
515 		return -EINVAL;
516 
517 	if (!oh->class->sysc->sysc_fields) {
518 		WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
519 		return -EINVAL;
520 	}
521 
522 	/* clocks must be on for this operation */
523 	if (oh->_state != _HWMOD_STATE_ENABLED) {
524 		pr_warn("omap_hwmod: %s: dma can be disabled only from enabled state\n", oh->name);
525 		return -EINVAL;
526 	}
527 
528 	pr_debug("omap_hwmod: %s: setting DMADISABLE\n", oh->name);
529 
530 	v = oh->_sysc_cache;
531 	dmadisable_mask =
532 		(0x1 << oh->class->sysc->sysc_fields->dmadisable_shift);
533 	v |= dmadisable_mask;
534 	_write_sysconfig(v, oh);
535 
536 	return 0;
537 }
538 
539 /**
540  * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
541  * @oh: struct omap_hwmod *
542  * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
543  * @v: pointer to register contents to modify
544  *
545  * Update the module autoidle bit in @v to be @autoidle for the @oh
546  * hwmod.  The autoidle bit controls whether the module can gate
547  * internal clocks automatically when it isn't doing anything; the
548  * exact function of this bit varies on a per-module basis.  This
549  * function does not write to the hardware.  Returns -EINVAL upon
550  * error or 0 upon success.
551  */
_set_module_autoidle(struct omap_hwmod * oh,u8 autoidle,u32 * v)552 static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
553 				u32 *v)
554 {
555 	u32 autoidle_mask;
556 	u8 autoidle_shift;
557 
558 	if (!oh->class->sysc ||
559 	    !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
560 		return -EINVAL;
561 
562 	if (!oh->class->sysc->sysc_fields) {
563 		WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
564 		return -EINVAL;
565 	}
566 
567 	autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
568 	autoidle_mask = (0x1 << autoidle_shift);
569 
570 	*v &= ~autoidle_mask;
571 	*v |= autoidle << autoidle_shift;
572 
573 	return 0;
574 }
575 
576 /**
577  * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
578  * @oh: struct omap_hwmod *
579  * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
580  *
581  * Set or clear the I/O pad wakeup flag in the mux entries for the
582  * hwmod @oh.  This function changes the @oh->mux->pads_dynamic array
583  * in memory.  If the hwmod is currently idled, and the new idle
584  * values don't match the previous ones, this function will also
585  * update the SCM PADCTRL registers.  Otherwise, if the hwmod is not
586  * currently idled, this function won't touch the hardware: the new
587  * mux settings are written to the SCM PADCTRL registers when the
588  * hwmod is idled.  No return value.
589  */
_set_idle_ioring_wakeup(struct omap_hwmod * oh,bool set_wake)590 static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
591 {
592 	struct omap_device_pad *pad;
593 	bool change = false;
594 	u16 prev_idle;
595 	int j;
596 
597 	if (!oh->mux || !oh->mux->enabled)
598 		return;
599 
600 	for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
601 		pad = oh->mux->pads_dynamic[j];
602 
603 		if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
604 			continue;
605 
606 		prev_idle = pad->idle;
607 
608 		if (set_wake)
609 			pad->idle |= OMAP_WAKEUP_EN;
610 		else
611 			pad->idle &= ~OMAP_WAKEUP_EN;
612 
613 		if (prev_idle != pad->idle)
614 			change = true;
615 	}
616 
617 	if (change && oh->_state == _HWMOD_STATE_IDLE)
618 		omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
619 }
620 
621 /**
622  * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
623  * @oh: struct omap_hwmod *
624  *
625  * Allow the hardware module @oh to send wakeups.  Returns -EINVAL
626  * upon error or 0 upon success.
627  */
_enable_wakeup(struct omap_hwmod * oh,u32 * v)628 static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
629 {
630 	if (!oh->class->sysc ||
631 	    !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
632 	      (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
633 	      (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
634 		return -EINVAL;
635 
636 	if (!oh->class->sysc->sysc_fields) {
637 		WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
638 		return -EINVAL;
639 	}
640 
641 	if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
642 		*v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
643 
644 	if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
645 		_set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
646 	if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
647 		_set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
648 
649 	/* XXX test pwrdm_get_wken for this hwmod's subsystem */
650 
651 	return 0;
652 }
653 
654 /**
655  * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
656  * @oh: struct omap_hwmod *
657  *
658  * Prevent the hardware module @oh to send wakeups.  Returns -EINVAL
659  * upon error or 0 upon success.
660  */
_disable_wakeup(struct omap_hwmod * oh,u32 * v)661 static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
662 {
663 	if (!oh->class->sysc ||
664 	    !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
665 	      (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
666 	      (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
667 		return -EINVAL;
668 
669 	if (!oh->class->sysc->sysc_fields) {
670 		WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
671 		return -EINVAL;
672 	}
673 
674 	if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
675 		*v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
676 
677 	if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
678 		_set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
679 	if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
680 		_set_master_standbymode(oh, HWMOD_IDLEMODE_SMART, v);
681 
682 	/* XXX test pwrdm_get_wken for this hwmod's subsystem */
683 
684 	return 0;
685 }
686 
_get_clkdm(struct omap_hwmod * oh)687 static struct clockdomain *_get_clkdm(struct omap_hwmod *oh)
688 {
689 	struct clk_hw_omap *clk;
690 
691 	if (oh->clkdm) {
692 		return oh->clkdm;
693 	} else if (oh->_clk) {
694 		if (__clk_get_flags(oh->_clk) & CLK_IS_BASIC)
695 			return NULL;
696 		clk = to_clk_hw_omap(__clk_get_hw(oh->_clk));
697 		return  clk->clkdm;
698 	}
699 	return NULL;
700 }
701 
702 /**
703  * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
704  * @oh: struct omap_hwmod *
705  *
706  * Prevent the hardware module @oh from entering idle while the
707  * hardare module initiator @init_oh is active.  Useful when a module
708  * will be accessed by a particular initiator (e.g., if a module will
709  * be accessed by the IVA, there should be a sleepdep between the IVA
710  * initiator and the module).  Only applies to modules in smart-idle
711  * mode.  If the clockdomain is marked as not needing autodeps, return
712  * 0 without doing anything.  Otherwise, returns -EINVAL upon error or
713  * passes along clkdm_add_sleepdep() value upon success.
714  */
_add_initiator_dep(struct omap_hwmod * oh,struct omap_hwmod * init_oh)715 static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
716 {
717 	struct clockdomain *clkdm, *init_clkdm;
718 
719 	clkdm = _get_clkdm(oh);
720 	init_clkdm = _get_clkdm(init_oh);
721 
722 	if (!clkdm || !init_clkdm)
723 		return -EINVAL;
724 
725 	if (clkdm && clkdm->flags & CLKDM_NO_AUTODEPS)
726 		return 0;
727 
728 	return clkdm_add_sleepdep(clkdm, init_clkdm);
729 }
730 
731 /**
732  * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
733  * @oh: struct omap_hwmod *
734  *
735  * Allow the hardware module @oh to enter idle while the hardare
736  * module initiator @init_oh is active.  Useful when a module will not
737  * be accessed by a particular initiator (e.g., if a module will not
738  * be accessed by the IVA, there should be no sleepdep between the IVA
739  * initiator and the module).  Only applies to modules in smart-idle
740  * mode.  If the clockdomain is marked as not needing autodeps, return
741  * 0 without doing anything.  Returns -EINVAL upon error or passes
742  * along clkdm_del_sleepdep() value upon success.
743  */
_del_initiator_dep(struct omap_hwmod * oh,struct omap_hwmod * init_oh)744 static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
745 {
746 	struct clockdomain *clkdm, *init_clkdm;
747 
748 	clkdm = _get_clkdm(oh);
749 	init_clkdm = _get_clkdm(init_oh);
750 
751 	if (!clkdm || !init_clkdm)
752 		return -EINVAL;
753 
754 	if (clkdm && clkdm->flags & CLKDM_NO_AUTODEPS)
755 		return 0;
756 
757 	return clkdm_del_sleepdep(clkdm, init_clkdm);
758 }
759 
760 /**
761  * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
762  * @oh: struct omap_hwmod *
763  *
764  * Called from _init_clocks().  Populates the @oh _clk (main
765  * functional clock pointer) if a main_clk is present.  Returns 0 on
766  * success or -EINVAL on error.
767  */
_init_main_clk(struct omap_hwmod * oh)768 static int _init_main_clk(struct omap_hwmod *oh)
769 {
770 	int ret = 0;
771 
772 	if (!oh->main_clk)
773 		return 0;
774 
775 	oh->_clk = clk_get(NULL, oh->main_clk);
776 	if (IS_ERR(oh->_clk)) {
777 		pr_warn("omap_hwmod: %s: cannot clk_get main_clk %s\n",
778 			oh->name, oh->main_clk);
779 		return -EINVAL;
780 	}
781 	/*
782 	 * HACK: This needs a re-visit once clk_prepare() is implemented
783 	 * to do something meaningful. Today its just a no-op.
784 	 * If clk_prepare() is used at some point to do things like
785 	 * voltage scaling etc, then this would have to be moved to
786 	 * some point where subsystems like i2c and pmic become
787 	 * available.
788 	 */
789 	clk_prepare(oh->_clk);
790 
791 	if (!_get_clkdm(oh))
792 		pr_debug("omap_hwmod: %s: missing clockdomain for %s.\n",
793 			   oh->name, oh->main_clk);
794 
795 	return ret;
796 }
797 
798 /**
799  * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
800  * @oh: struct omap_hwmod *
801  *
802  * Called from _init_clocks().  Populates the @oh OCP slave interface
803  * clock pointers.  Returns 0 on success or -EINVAL on error.
804  */
_init_interface_clks(struct omap_hwmod * oh)805 static int _init_interface_clks(struct omap_hwmod *oh)
806 {
807 	struct omap_hwmod_ocp_if *os;
808 	struct list_head *p;
809 	struct clk *c;
810 	int i = 0;
811 	int ret = 0;
812 
813 	p = oh->slave_ports.next;
814 
815 	while (i < oh->slaves_cnt) {
816 		os = _fetch_next_ocp_if(&p, &i);
817 		if (!os->clk)
818 			continue;
819 
820 		c = clk_get(NULL, os->clk);
821 		if (IS_ERR(c)) {
822 			pr_warn("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
823 				oh->name, os->clk);
824 			ret = -EINVAL;
825 			continue;
826 		}
827 		os->_clk = c;
828 		/*
829 		 * HACK: This needs a re-visit once clk_prepare() is implemented
830 		 * to do something meaningful. Today its just a no-op.
831 		 * If clk_prepare() is used at some point to do things like
832 		 * voltage scaling etc, then this would have to be moved to
833 		 * some point where subsystems like i2c and pmic become
834 		 * available.
835 		 */
836 		clk_prepare(os->_clk);
837 	}
838 
839 	return ret;
840 }
841 
842 /**
843  * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
844  * @oh: struct omap_hwmod *
845  *
846  * Called from _init_clocks().  Populates the @oh omap_hwmod_opt_clk
847  * clock pointers.  Returns 0 on success or -EINVAL on error.
848  */
_init_opt_clks(struct omap_hwmod * oh)849 static int _init_opt_clks(struct omap_hwmod *oh)
850 {
851 	struct omap_hwmod_opt_clk *oc;
852 	struct clk *c;
853 	int i;
854 	int ret = 0;
855 
856 	for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
857 		c = clk_get(NULL, oc->clk);
858 		if (IS_ERR(c)) {
859 			pr_warn("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
860 				oh->name, oc->clk);
861 			ret = -EINVAL;
862 			continue;
863 		}
864 		oc->_clk = c;
865 		/*
866 		 * HACK: This needs a re-visit once clk_prepare() is implemented
867 		 * to do something meaningful. Today its just a no-op.
868 		 * If clk_prepare() is used at some point to do things like
869 		 * voltage scaling etc, then this would have to be moved to
870 		 * some point where subsystems like i2c and pmic become
871 		 * available.
872 		 */
873 		clk_prepare(oc->_clk);
874 	}
875 
876 	return ret;
877 }
878 
_enable_optional_clocks(struct omap_hwmod * oh)879 static void _enable_optional_clocks(struct omap_hwmod *oh)
880 {
881 	struct omap_hwmod_opt_clk *oc;
882 	int i;
883 
884 	pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
885 
886 	for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
887 		if (oc->_clk) {
888 			pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
889 				 __clk_get_name(oc->_clk));
890 			clk_enable(oc->_clk);
891 		}
892 }
893 
_disable_optional_clocks(struct omap_hwmod * oh)894 static void _disable_optional_clocks(struct omap_hwmod *oh)
895 {
896 	struct omap_hwmod_opt_clk *oc;
897 	int i;
898 
899 	pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
900 
901 	for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
902 		if (oc->_clk) {
903 			pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
904 				 __clk_get_name(oc->_clk));
905 			clk_disable(oc->_clk);
906 		}
907 }
908 
909 /**
910  * _enable_clocks - enable hwmod main clock and interface clocks
911  * @oh: struct omap_hwmod *
912  *
913  * Enables all clocks necessary for register reads and writes to succeed
914  * on the hwmod @oh.  Returns 0.
915  */
_enable_clocks(struct omap_hwmod * oh)916 static int _enable_clocks(struct omap_hwmod *oh)
917 {
918 	struct omap_hwmod_ocp_if *os;
919 	struct list_head *p;
920 	int i = 0;
921 
922 	pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
923 
924 	if (oh->_clk)
925 		clk_enable(oh->_clk);
926 
927 	p = oh->slave_ports.next;
928 
929 	while (i < oh->slaves_cnt) {
930 		os = _fetch_next_ocp_if(&p, &i);
931 
932 		if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
933 			clk_enable(os->_clk);
934 	}
935 
936 	if (oh->flags & HWMOD_OPT_CLKS_NEEDED)
937 		_enable_optional_clocks(oh);
938 
939 	/* The opt clocks are controlled by the device driver. */
940 
941 	return 0;
942 }
943 
944 /**
945  * _disable_clocks - disable hwmod main clock and interface clocks
946  * @oh: struct omap_hwmod *
947  *
948  * Disables the hwmod @oh main functional and interface clocks.  Returns 0.
949  */
_disable_clocks(struct omap_hwmod * oh)950 static int _disable_clocks(struct omap_hwmod *oh)
951 {
952 	struct omap_hwmod_ocp_if *os;
953 	struct list_head *p;
954 	int i = 0;
955 
956 	pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
957 
958 	if (oh->_clk)
959 		clk_disable(oh->_clk);
960 
961 	p = oh->slave_ports.next;
962 
963 	while (i < oh->slaves_cnt) {
964 		os = _fetch_next_ocp_if(&p, &i);
965 
966 		if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
967 			clk_disable(os->_clk);
968 	}
969 
970 	if (oh->flags & HWMOD_OPT_CLKS_NEEDED)
971 		_disable_optional_clocks(oh);
972 
973 	/* The opt clocks are controlled by the device driver. */
974 
975 	return 0;
976 }
977 
978 /**
979  * _omap4_enable_module - enable CLKCTRL modulemode on OMAP4
980  * @oh: struct omap_hwmod *
981  *
982  * Enables the PRCM module mode related to the hwmod @oh.
983  * No return value.
984  */
_omap4_enable_module(struct omap_hwmod * oh)985 static void _omap4_enable_module(struct omap_hwmod *oh)
986 {
987 	if (!oh->clkdm || !oh->prcm.omap4.modulemode)
988 		return;
989 
990 	pr_debug("omap_hwmod: %s: %s: %d\n",
991 		 oh->name, __func__, oh->prcm.omap4.modulemode);
992 
993 	omap_cm_module_enable(oh->prcm.omap4.modulemode,
994 			      oh->clkdm->prcm_partition,
995 			      oh->clkdm->cm_inst, oh->prcm.omap4.clkctrl_offs);
996 }
997 
998 /**
999  * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
1000  * @oh: struct omap_hwmod *
1001  *
1002  * Wait for a module @oh to enter slave idle.  Returns 0 if the module
1003  * does not have an IDLEST bit or if the module successfully enters
1004  * slave idle; otherwise, pass along the return value of the
1005  * appropriate *_cm*_wait_module_idle() function.
1006  */
_omap4_wait_target_disable(struct omap_hwmod * oh)1007 static int _omap4_wait_target_disable(struct omap_hwmod *oh)
1008 {
1009 	if (!oh)
1010 		return -EINVAL;
1011 
1012 	if (oh->_int_flags & _HWMOD_NO_MPU_PORT || !oh->clkdm)
1013 		return 0;
1014 
1015 	if (oh->flags & HWMOD_NO_IDLEST)
1016 		return 0;
1017 
1018 	return omap_cm_wait_module_idle(oh->clkdm->prcm_partition,
1019 					oh->clkdm->cm_inst,
1020 					oh->prcm.omap4.clkctrl_offs, 0);
1021 }
1022 
1023 /**
1024  * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
1025  * @oh: struct omap_hwmod *oh
1026  *
1027  * Count and return the number of MPU IRQs associated with the hwmod
1028  * @oh.  Used to allocate struct resource data.  Returns 0 if @oh is
1029  * NULL.
1030  */
_count_mpu_irqs(struct omap_hwmod * oh)1031 static int _count_mpu_irqs(struct omap_hwmod *oh)
1032 {
1033 	struct omap_hwmod_irq_info *ohii;
1034 	int i = 0;
1035 
1036 	if (!oh || !oh->mpu_irqs)
1037 		return 0;
1038 
1039 	do {
1040 		ohii = &oh->mpu_irqs[i++];
1041 	} while (ohii->irq != -1);
1042 
1043 	return i-1;
1044 }
1045 
1046 /**
1047  * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
1048  * @oh: struct omap_hwmod *oh
1049  *
1050  * Count and return the number of SDMA request lines associated with
1051  * the hwmod @oh.  Used to allocate struct resource data.  Returns 0
1052  * if @oh is NULL.
1053  */
_count_sdma_reqs(struct omap_hwmod * oh)1054 static int _count_sdma_reqs(struct omap_hwmod *oh)
1055 {
1056 	struct omap_hwmod_dma_info *ohdi;
1057 	int i = 0;
1058 
1059 	if (!oh || !oh->sdma_reqs)
1060 		return 0;
1061 
1062 	do {
1063 		ohdi = &oh->sdma_reqs[i++];
1064 	} while (ohdi->dma_req != -1);
1065 
1066 	return i-1;
1067 }
1068 
1069 /**
1070  * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
1071  * @oh: struct omap_hwmod *oh
1072  *
1073  * Count and return the number of address space ranges associated with
1074  * the hwmod @oh.  Used to allocate struct resource data.  Returns 0
1075  * if @oh is NULL.
1076  */
_count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if * os)1077 static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
1078 {
1079 	struct omap_hwmod_addr_space *mem;
1080 	int i = 0;
1081 
1082 	if (!os || !os->addr)
1083 		return 0;
1084 
1085 	do {
1086 		mem = &os->addr[i++];
1087 	} while (mem->pa_start != mem->pa_end);
1088 
1089 	return i-1;
1090 }
1091 
1092 /**
1093  * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
1094  * @oh: struct omap_hwmod * to operate on
1095  * @name: pointer to the name of the MPU interrupt number to fetch (optional)
1096  * @irq: pointer to an unsigned int to store the MPU IRQ number to
1097  *
1098  * Retrieve a MPU hardware IRQ line number named by @name associated
1099  * with the IP block pointed to by @oh.  The IRQ number will be filled
1100  * into the address pointed to by @dma.  When @name is non-null, the
1101  * IRQ line number associated with the named entry will be returned.
1102  * If @name is null, the first matching entry will be returned.  Data
1103  * order is not meaningful in hwmod data, so callers are strongly
1104  * encouraged to use a non-null @name whenever possible to avoid
1105  * unpredictable effects if hwmod data is later added that causes data
1106  * ordering to change.  Returns 0 upon success or a negative error
1107  * code upon error.
1108  */
_get_mpu_irq_by_name(struct omap_hwmod * oh,const char * name,unsigned int * irq)1109 static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
1110 				unsigned int *irq)
1111 {
1112 	int i;
1113 	bool found = false;
1114 
1115 	if (!oh->mpu_irqs)
1116 		return -ENOENT;
1117 
1118 	i = 0;
1119 	while (oh->mpu_irqs[i].irq != -1) {
1120 		if (name == oh->mpu_irqs[i].name ||
1121 		    !strcmp(name, oh->mpu_irqs[i].name)) {
1122 			found = true;
1123 			break;
1124 		}
1125 		i++;
1126 	}
1127 
1128 	if (!found)
1129 		return -ENOENT;
1130 
1131 	*irq = oh->mpu_irqs[i].irq;
1132 
1133 	return 0;
1134 }
1135 
1136 /**
1137  * _get_sdma_req_by_name - fetch SDMA request line ID by name
1138  * @oh: struct omap_hwmod * to operate on
1139  * @name: pointer to the name of the SDMA request line to fetch (optional)
1140  * @dma: pointer to an unsigned int to store the request line ID to
1141  *
1142  * Retrieve an SDMA request line ID named by @name on the IP block
1143  * pointed to by @oh.  The ID will be filled into the address pointed
1144  * to by @dma.  When @name is non-null, the request line ID associated
1145  * with the named entry will be returned.  If @name is null, the first
1146  * matching entry will be returned.  Data order is not meaningful in
1147  * hwmod data, so callers are strongly encouraged to use a non-null
1148  * @name whenever possible to avoid unpredictable effects if hwmod
1149  * data is later added that causes data ordering to change.  Returns 0
1150  * upon success or a negative error code upon error.
1151  */
_get_sdma_req_by_name(struct omap_hwmod * oh,const char * name,unsigned int * dma)1152 static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
1153 				 unsigned int *dma)
1154 {
1155 	int i;
1156 	bool found = false;
1157 
1158 	if (!oh->sdma_reqs)
1159 		return -ENOENT;
1160 
1161 	i = 0;
1162 	while (oh->sdma_reqs[i].dma_req != -1) {
1163 		if (name == oh->sdma_reqs[i].name ||
1164 		    !strcmp(name, oh->sdma_reqs[i].name)) {
1165 			found = true;
1166 			break;
1167 		}
1168 		i++;
1169 	}
1170 
1171 	if (!found)
1172 		return -ENOENT;
1173 
1174 	*dma = oh->sdma_reqs[i].dma_req;
1175 
1176 	return 0;
1177 }
1178 
1179 /**
1180  * _get_addr_space_by_name - fetch address space start & end by name
1181  * @oh: struct omap_hwmod * to operate on
1182  * @name: pointer to the name of the address space to fetch (optional)
1183  * @pa_start: pointer to a u32 to store the starting address to
1184  * @pa_end: pointer to a u32 to store the ending address to
1185  *
1186  * Retrieve address space start and end addresses for the IP block
1187  * pointed to by @oh.  The data will be filled into the addresses
1188  * pointed to by @pa_start and @pa_end.  When @name is non-null, the
1189  * address space data associated with the named entry will be
1190  * returned.  If @name is null, the first matching entry will be
1191  * returned.  Data order is not meaningful in hwmod data, so callers
1192  * are strongly encouraged to use a non-null @name whenever possible
1193  * to avoid unpredictable effects if hwmod data is later added that
1194  * causes data ordering to change.  Returns 0 upon success or a
1195  * negative error code upon error.
1196  */
_get_addr_space_by_name(struct omap_hwmod * oh,const char * name,u32 * pa_start,u32 * pa_end)1197 static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
1198 				   u32 *pa_start, u32 *pa_end)
1199 {
1200 	int i, j;
1201 	struct omap_hwmod_ocp_if *os;
1202 	struct list_head *p = NULL;
1203 	bool found = false;
1204 
1205 	p = oh->slave_ports.next;
1206 
1207 	i = 0;
1208 	while (i < oh->slaves_cnt) {
1209 		os = _fetch_next_ocp_if(&p, &i);
1210 
1211 		if (!os->addr)
1212 			return -ENOENT;
1213 
1214 		j = 0;
1215 		while (os->addr[j].pa_start != os->addr[j].pa_end) {
1216 			if (name == os->addr[j].name ||
1217 			    !strcmp(name, os->addr[j].name)) {
1218 				found = true;
1219 				break;
1220 			}
1221 			j++;
1222 		}
1223 
1224 		if (found)
1225 			break;
1226 	}
1227 
1228 	if (!found)
1229 		return -ENOENT;
1230 
1231 	*pa_start = os->addr[j].pa_start;
1232 	*pa_end = os->addr[j].pa_end;
1233 
1234 	return 0;
1235 }
1236 
1237 /**
1238  * _save_mpu_port_index - find and save the index to @oh's MPU port
1239  * @oh: struct omap_hwmod *
1240  *
1241  * Determines the array index of the OCP slave port that the MPU uses
1242  * to address the device, and saves it into the struct omap_hwmod.
1243  * Intended to be called during hwmod registration only. No return
1244  * value.
1245  */
_save_mpu_port_index(struct omap_hwmod * oh)1246 static void __init _save_mpu_port_index(struct omap_hwmod *oh)
1247 {
1248 	struct omap_hwmod_ocp_if *os = NULL;
1249 	struct list_head *p;
1250 	int i = 0;
1251 
1252 	if (!oh)
1253 		return;
1254 
1255 	oh->_int_flags |= _HWMOD_NO_MPU_PORT;
1256 
1257 	p = oh->slave_ports.next;
1258 
1259 	while (i < oh->slaves_cnt) {
1260 		os = _fetch_next_ocp_if(&p, &i);
1261 		if (os->user & OCP_USER_MPU) {
1262 			oh->_mpu_port = os;
1263 			oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
1264 			break;
1265 		}
1266 	}
1267 
1268 	return;
1269 }
1270 
1271 /**
1272  * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
1273  * @oh: struct omap_hwmod *
1274  *
1275  * Given a pointer to a struct omap_hwmod record @oh, return a pointer
1276  * to the struct omap_hwmod_ocp_if record that is used by the MPU to
1277  * communicate with the IP block.  This interface need not be directly
1278  * connected to the MPU (and almost certainly is not), but is directly
1279  * connected to the IP block represented by @oh.  Returns a pointer
1280  * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
1281  * error or if there does not appear to be a path from the MPU to this
1282  * IP block.
1283  */
_find_mpu_rt_port(struct omap_hwmod * oh)1284 static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
1285 {
1286 	if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
1287 		return NULL;
1288 
1289 	return oh->_mpu_port;
1290 };
1291 
1292 /**
1293  * _find_mpu_rt_addr_space - return MPU register target address space for @oh
1294  * @oh: struct omap_hwmod *
1295  *
1296  * Returns a pointer to the struct omap_hwmod_addr_space record representing
1297  * the register target MPU address space; or returns NULL upon error.
1298  */
_find_mpu_rt_addr_space(struct omap_hwmod * oh)1299 static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
1300 {
1301 	struct omap_hwmod_ocp_if *os;
1302 	struct omap_hwmod_addr_space *mem;
1303 	int found = 0, i = 0;
1304 
1305 	os = _find_mpu_rt_port(oh);
1306 	if (!os || !os->addr)
1307 		return NULL;
1308 
1309 	do {
1310 		mem = &os->addr[i++];
1311 		if (mem->flags & ADDR_TYPE_RT)
1312 			found = 1;
1313 	} while (!found && mem->pa_start != mem->pa_end);
1314 
1315 	return (found) ? mem : NULL;
1316 }
1317 
1318 /**
1319  * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
1320  * @oh: struct omap_hwmod *
1321  *
1322  * Ensure that the OCP_SYSCONFIG register for the IP block represented
1323  * by @oh is set to indicate to the PRCM that the IP block is active.
1324  * Usually this means placing the module into smart-idle mode and
1325  * smart-standby, but if there is a bug in the automatic idle handling
1326  * for the IP block, it may need to be placed into the force-idle or
1327  * no-idle variants of these modes.  No return value.
1328  */
_enable_sysc(struct omap_hwmod * oh)1329 static void _enable_sysc(struct omap_hwmod *oh)
1330 {
1331 	u8 idlemode, sf;
1332 	u32 v;
1333 	bool clkdm_act;
1334 	struct clockdomain *clkdm;
1335 
1336 	if (!oh->class->sysc)
1337 		return;
1338 
1339 	/*
1340 	 * Wait until reset has completed, this is needed as the IP
1341 	 * block is reset automatically by hardware in some cases
1342 	 * (off-mode for example), and the drivers require the
1343 	 * IP to be ready when they access it
1344 	 */
1345 	if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1346 		_enable_optional_clocks(oh);
1347 	_wait_softreset_complete(oh);
1348 	if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1349 		_disable_optional_clocks(oh);
1350 
1351 	v = oh->_sysc_cache;
1352 	sf = oh->class->sysc->sysc_flags;
1353 
1354 	clkdm = _get_clkdm(oh);
1355 	if (sf & SYSC_HAS_SIDLEMODE) {
1356 		if (oh->flags & HWMOD_SWSUP_SIDLE ||
1357 		    oh->flags & HWMOD_SWSUP_SIDLE_ACT) {
1358 			idlemode = HWMOD_IDLEMODE_NO;
1359 		} else {
1360 			if (sf & SYSC_HAS_ENAWAKEUP)
1361 				_enable_wakeup(oh, &v);
1362 			if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
1363 				idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1364 			else
1365 				idlemode = HWMOD_IDLEMODE_SMART;
1366 		}
1367 
1368 		/*
1369 		 * This is special handling for some IPs like
1370 		 * 32k sync timer. Force them to idle!
1371 		 */
1372 		clkdm_act = (clkdm && clkdm->flags & CLKDM_ACTIVE_WITH_MPU);
1373 		if (clkdm_act && !(oh->class->sysc->idlemodes &
1374 				   (SIDLE_SMART | SIDLE_SMART_WKUP)))
1375 			idlemode = HWMOD_IDLEMODE_FORCE;
1376 
1377 		_set_slave_idlemode(oh, idlemode, &v);
1378 	}
1379 
1380 	if (sf & SYSC_HAS_MIDLEMODE) {
1381 		if (oh->flags & HWMOD_FORCE_MSTANDBY) {
1382 			idlemode = HWMOD_IDLEMODE_FORCE;
1383 		} else if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
1384 			idlemode = HWMOD_IDLEMODE_NO;
1385 		} else {
1386 			if (sf & SYSC_HAS_ENAWAKEUP)
1387 				_enable_wakeup(oh, &v);
1388 			if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1389 				idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1390 			else
1391 				idlemode = HWMOD_IDLEMODE_SMART;
1392 		}
1393 		_set_master_standbymode(oh, idlemode, &v);
1394 	}
1395 
1396 	/*
1397 	 * XXX The clock framework should handle this, by
1398 	 * calling into this code.  But this must wait until the
1399 	 * clock structures are tagged with omap_hwmod entries
1400 	 */
1401 	if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
1402 	    (sf & SYSC_HAS_CLOCKACTIVITY))
1403 		_set_clockactivity(oh, oh->class->sysc->clockact, &v);
1404 
1405 	_write_sysconfig(v, oh);
1406 
1407 	/*
1408 	 * Set the autoidle bit only after setting the smartidle bit
1409 	 * Setting this will not have any impact on the other modules.
1410 	 */
1411 	if (sf & SYSC_HAS_AUTOIDLE) {
1412 		idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
1413 			0 : 1;
1414 		_set_module_autoidle(oh, idlemode, &v);
1415 		_write_sysconfig(v, oh);
1416 	}
1417 }
1418 
1419 /**
1420  * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
1421  * @oh: struct omap_hwmod *
1422  *
1423  * If module is marked as SWSUP_SIDLE, force the module into slave
1424  * idle; otherwise, configure it for smart-idle.  If module is marked
1425  * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
1426  * configure it for smart-standby.  No return value.
1427  */
_idle_sysc(struct omap_hwmod * oh)1428 static void _idle_sysc(struct omap_hwmod *oh)
1429 {
1430 	u8 idlemode, sf;
1431 	u32 v;
1432 
1433 	if (!oh->class->sysc)
1434 		return;
1435 
1436 	v = oh->_sysc_cache;
1437 	sf = oh->class->sysc->sysc_flags;
1438 
1439 	if (sf & SYSC_HAS_SIDLEMODE) {
1440 		if (oh->flags & HWMOD_SWSUP_SIDLE) {
1441 			idlemode = HWMOD_IDLEMODE_FORCE;
1442 		} else {
1443 			if (sf & SYSC_HAS_ENAWAKEUP)
1444 				_enable_wakeup(oh, &v);
1445 			if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
1446 				idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1447 			else
1448 				idlemode = HWMOD_IDLEMODE_SMART;
1449 		}
1450 		_set_slave_idlemode(oh, idlemode, &v);
1451 	}
1452 
1453 	if (sf & SYSC_HAS_MIDLEMODE) {
1454 		if ((oh->flags & HWMOD_SWSUP_MSTANDBY) ||
1455 		    (oh->flags & HWMOD_FORCE_MSTANDBY)) {
1456 			idlemode = HWMOD_IDLEMODE_FORCE;
1457 		} else {
1458 			if (sf & SYSC_HAS_ENAWAKEUP)
1459 				_enable_wakeup(oh, &v);
1460 			if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
1461 				idlemode = HWMOD_IDLEMODE_SMART_WKUP;
1462 			else
1463 				idlemode = HWMOD_IDLEMODE_SMART;
1464 		}
1465 		_set_master_standbymode(oh, idlemode, &v);
1466 	}
1467 
1468 	/* If the cached value is the same as the new value, skip the write */
1469 	if (oh->_sysc_cache != v)
1470 		_write_sysconfig(v, oh);
1471 }
1472 
1473 /**
1474  * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
1475  * @oh: struct omap_hwmod *
1476  *
1477  * Force the module into slave idle and master suspend. No return
1478  * value.
1479  */
_shutdown_sysc(struct omap_hwmod * oh)1480 static void _shutdown_sysc(struct omap_hwmod *oh)
1481 {
1482 	u32 v;
1483 	u8 sf;
1484 
1485 	if (!oh->class->sysc)
1486 		return;
1487 
1488 	v = oh->_sysc_cache;
1489 	sf = oh->class->sysc->sysc_flags;
1490 
1491 	if (sf & SYSC_HAS_SIDLEMODE)
1492 		_set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
1493 
1494 	if (sf & SYSC_HAS_MIDLEMODE)
1495 		_set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
1496 
1497 	if (sf & SYSC_HAS_AUTOIDLE)
1498 		_set_module_autoidle(oh, 1, &v);
1499 
1500 	_write_sysconfig(v, oh);
1501 }
1502 
1503 /**
1504  * _lookup - find an omap_hwmod by name
1505  * @name: find an omap_hwmod by name
1506  *
1507  * Return a pointer to an omap_hwmod by name, or NULL if not found.
1508  */
_lookup(const char * name)1509 static struct omap_hwmod *_lookup(const char *name)
1510 {
1511 	struct omap_hwmod *oh, *temp_oh;
1512 
1513 	oh = NULL;
1514 
1515 	list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
1516 		if (!strcmp(name, temp_oh->name)) {
1517 			oh = temp_oh;
1518 			break;
1519 		}
1520 	}
1521 
1522 	return oh;
1523 }
1524 
1525 /**
1526  * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
1527  * @oh: struct omap_hwmod *
1528  *
1529  * Convert a clockdomain name stored in a struct omap_hwmod into a
1530  * clockdomain pointer, and save it into the struct omap_hwmod.
1531  * Return -EINVAL if the clkdm_name lookup failed.
1532  */
_init_clkdm(struct omap_hwmod * oh)1533 static int _init_clkdm(struct omap_hwmod *oh)
1534 {
1535 	if (!oh->clkdm_name) {
1536 		pr_debug("omap_hwmod: %s: missing clockdomain\n", oh->name);
1537 		return 0;
1538 	}
1539 
1540 	oh->clkdm = clkdm_lookup(oh->clkdm_name);
1541 	if (!oh->clkdm) {
1542 		pr_warn("omap_hwmod: %s: could not associate to clkdm %s\n",
1543 			oh->name, oh->clkdm_name);
1544 		return 0;
1545 	}
1546 
1547 	pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
1548 		oh->name, oh->clkdm_name);
1549 
1550 	return 0;
1551 }
1552 
1553 /**
1554  * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
1555  * well the clockdomain.
1556  * @oh: struct omap_hwmod *
1557  * @data: not used; pass NULL
1558  *
1559  * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
1560  * Resolves all clock names embedded in the hwmod.  Returns 0 on
1561  * success, or a negative error code on failure.
1562  */
_init_clocks(struct omap_hwmod * oh,void * data)1563 static int _init_clocks(struct omap_hwmod *oh, void *data)
1564 {
1565 	int ret = 0;
1566 
1567 	if (oh->_state != _HWMOD_STATE_REGISTERED)
1568 		return 0;
1569 
1570 	pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
1571 
1572 	if (soc_ops.init_clkdm)
1573 		ret |= soc_ops.init_clkdm(oh);
1574 
1575 	ret |= _init_main_clk(oh);
1576 	ret |= _init_interface_clks(oh);
1577 	ret |= _init_opt_clks(oh);
1578 
1579 	if (!ret)
1580 		oh->_state = _HWMOD_STATE_CLKS_INITED;
1581 	else
1582 		pr_warn("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
1583 
1584 	return ret;
1585 }
1586 
1587 /**
1588  * _lookup_hardreset - fill register bit info for this hwmod/reset line
1589  * @oh: struct omap_hwmod *
1590  * @name: name of the reset line in the context of this hwmod
1591  * @ohri: struct omap_hwmod_rst_info * that this function will fill in
1592  *
1593  * Return the bit position of the reset line that match the
1594  * input name. Return -ENOENT if not found.
1595  */
_lookup_hardreset(struct omap_hwmod * oh,const char * name,struct omap_hwmod_rst_info * ohri)1596 static int _lookup_hardreset(struct omap_hwmod *oh, const char *name,
1597 			     struct omap_hwmod_rst_info *ohri)
1598 {
1599 	int i;
1600 
1601 	for (i = 0; i < oh->rst_lines_cnt; i++) {
1602 		const char *rst_line = oh->rst_lines[i].name;
1603 		if (!strcmp(rst_line, name)) {
1604 			ohri->rst_shift = oh->rst_lines[i].rst_shift;
1605 			ohri->st_shift = oh->rst_lines[i].st_shift;
1606 			pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
1607 				 oh->name, __func__, rst_line, ohri->rst_shift,
1608 				 ohri->st_shift);
1609 
1610 			return 0;
1611 		}
1612 	}
1613 
1614 	return -ENOENT;
1615 }
1616 
1617 /**
1618  * _assert_hardreset - assert the HW reset line of submodules
1619  * contained in the hwmod module.
1620  * @oh: struct omap_hwmod *
1621  * @name: name of the reset line to lookup and assert
1622  *
1623  * Some IP like dsp, ipu or iva contain processor that require an HW
1624  * reset line to be assert / deassert in order to enable fully the IP.
1625  * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1626  * asserting the hardreset line on the currently-booted SoC, or passes
1627  * along the return value from _lookup_hardreset() or the SoC's
1628  * assert_hardreset code.
1629  */
_assert_hardreset(struct omap_hwmod * oh,const char * name)1630 static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
1631 {
1632 	struct omap_hwmod_rst_info ohri;
1633 	int ret = -EINVAL;
1634 
1635 	if (!oh)
1636 		return -EINVAL;
1637 
1638 	if (!soc_ops.assert_hardreset)
1639 		return -ENOSYS;
1640 
1641 	ret = _lookup_hardreset(oh, name, &ohri);
1642 	if (ret < 0)
1643 		return ret;
1644 
1645 	ret = soc_ops.assert_hardreset(oh, &ohri);
1646 
1647 	return ret;
1648 }
1649 
1650 /**
1651  * _deassert_hardreset - deassert the HW reset line of submodules contained
1652  * in the hwmod module.
1653  * @oh: struct omap_hwmod *
1654  * @name: name of the reset line to look up and deassert
1655  *
1656  * Some IP like dsp, ipu or iva contain processor that require an HW
1657  * reset line to be assert / deassert in order to enable fully the IP.
1658  * Returns -EINVAL if @oh is null, -ENOSYS if we have no way of
1659  * deasserting the hardreset line on the currently-booted SoC, or passes
1660  * along the return value from _lookup_hardreset() or the SoC's
1661  * deassert_hardreset code.
1662  */
_deassert_hardreset(struct omap_hwmod * oh,const char * name)1663 static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
1664 {
1665 	struct omap_hwmod_rst_info ohri;
1666 	int ret = -EINVAL;
1667 	int hwsup = 0;
1668 
1669 	if (!oh)
1670 		return -EINVAL;
1671 
1672 	if (!soc_ops.deassert_hardreset)
1673 		return -ENOSYS;
1674 
1675 	ret = _lookup_hardreset(oh, name, &ohri);
1676 	if (ret < 0)
1677 		return ret;
1678 
1679 	if (oh->clkdm) {
1680 		/*
1681 		 * A clockdomain must be in SW_SUP otherwise reset
1682 		 * might not be completed. The clockdomain can be set
1683 		 * in HW_AUTO only when the module become ready.
1684 		 */
1685 		hwsup = clkdm_in_hwsup(oh->clkdm);
1686 		ret = clkdm_hwmod_enable(oh->clkdm, oh);
1687 		if (ret) {
1688 			WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
1689 			     oh->name, oh->clkdm->name, ret);
1690 			return ret;
1691 		}
1692 	}
1693 
1694 	_enable_clocks(oh);
1695 	if (soc_ops.enable_module)
1696 		soc_ops.enable_module(oh);
1697 
1698 	ret = soc_ops.deassert_hardreset(oh, &ohri);
1699 
1700 	if (soc_ops.disable_module)
1701 		soc_ops.disable_module(oh);
1702 	_disable_clocks(oh);
1703 
1704 	if (ret == -EBUSY)
1705 		pr_warn("omap_hwmod: %s: failed to hardreset\n", oh->name);
1706 
1707 	if (oh->clkdm) {
1708 		/*
1709 		 * Set the clockdomain to HW_AUTO, assuming that the
1710 		 * previous state was HW_AUTO.
1711 		 */
1712 		if (hwsup)
1713 			clkdm_allow_idle(oh->clkdm);
1714 
1715 		clkdm_hwmod_disable(oh->clkdm, oh);
1716 	}
1717 
1718 	return ret;
1719 }
1720 
1721 /**
1722  * _read_hardreset - read the HW reset line state of submodules
1723  * contained in the hwmod module
1724  * @oh: struct omap_hwmod *
1725  * @name: name of the reset line to look up and read
1726  *
1727  * Return the state of the reset line.  Returns -EINVAL if @oh is
1728  * null, -ENOSYS if we have no way of reading the hardreset line
1729  * status on the currently-booted SoC, or passes along the return
1730  * value from _lookup_hardreset() or the SoC's is_hardreset_asserted
1731  * code.
1732  */
_read_hardreset(struct omap_hwmod * oh,const char * name)1733 static int _read_hardreset(struct omap_hwmod *oh, const char *name)
1734 {
1735 	struct omap_hwmod_rst_info ohri;
1736 	int ret = -EINVAL;
1737 
1738 	if (!oh)
1739 		return -EINVAL;
1740 
1741 	if (!soc_ops.is_hardreset_asserted)
1742 		return -ENOSYS;
1743 
1744 	ret = _lookup_hardreset(oh, name, &ohri);
1745 	if (ret < 0)
1746 		return ret;
1747 
1748 	return soc_ops.is_hardreset_asserted(oh, &ohri);
1749 }
1750 
1751 /**
1752  * _are_all_hardreset_lines_asserted - return true if the @oh is hard-reset
1753  * @oh: struct omap_hwmod *
1754  *
1755  * If all hardreset lines associated with @oh are asserted, then return true.
1756  * Otherwise, if part of @oh is out hardreset or if no hardreset lines
1757  * associated with @oh are asserted, then return false.
1758  * This function is used to avoid executing some parts of the IP block
1759  * enable/disable sequence if its hardreset line is set.
1760  */
_are_all_hardreset_lines_asserted(struct omap_hwmod * oh)1761 static bool _are_all_hardreset_lines_asserted(struct omap_hwmod *oh)
1762 {
1763 	int i, rst_cnt = 0;
1764 
1765 	if (oh->rst_lines_cnt == 0)
1766 		return false;
1767 
1768 	for (i = 0; i < oh->rst_lines_cnt; i++)
1769 		if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1770 			rst_cnt++;
1771 
1772 	if (oh->rst_lines_cnt == rst_cnt)
1773 		return true;
1774 
1775 	return false;
1776 }
1777 
1778 /**
1779  * _are_any_hardreset_lines_asserted - return true if any part of @oh is
1780  * hard-reset
1781  * @oh: struct omap_hwmod *
1782  *
1783  * If any hardreset lines associated with @oh are asserted, then
1784  * return true.  Otherwise, if no hardreset lines associated with @oh
1785  * are asserted, or if @oh has no hardreset lines, then return false.
1786  * This function is used to avoid executing some parts of the IP block
1787  * enable/disable sequence if any hardreset line is set.
1788  */
_are_any_hardreset_lines_asserted(struct omap_hwmod * oh)1789 static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
1790 {
1791 	int rst_cnt = 0;
1792 	int i;
1793 
1794 	for (i = 0; i < oh->rst_lines_cnt && rst_cnt == 0; i++)
1795 		if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
1796 			rst_cnt++;
1797 
1798 	return (rst_cnt) ? true : false;
1799 }
1800 
1801 /**
1802  * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
1803  * @oh: struct omap_hwmod *
1804  *
1805  * Disable the PRCM module mode related to the hwmod @oh.
1806  * Return EINVAL if the modulemode is not supported and 0 in case of success.
1807  */
_omap4_disable_module(struct omap_hwmod * oh)1808 static int _omap4_disable_module(struct omap_hwmod *oh)
1809 {
1810 	int v;
1811 
1812 	if (!oh->clkdm || !oh->prcm.omap4.modulemode)
1813 		return -EINVAL;
1814 
1815 	/*
1816 	 * Since integration code might still be doing something, only
1817 	 * disable if all lines are under hardreset.
1818 	 */
1819 	if (_are_any_hardreset_lines_asserted(oh))
1820 		return 0;
1821 
1822 	pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
1823 
1824 	omap_cm_module_disable(oh->clkdm->prcm_partition, oh->clkdm->cm_inst,
1825 			       oh->prcm.omap4.clkctrl_offs);
1826 
1827 	v = _omap4_wait_target_disable(oh);
1828 	if (v)
1829 		pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
1830 			oh->name);
1831 
1832 	return 0;
1833 }
1834 
1835 /**
1836  * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
1837  * @oh: struct omap_hwmod *
1838  *
1839  * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit.  hwmod must be
1840  * enabled for this to work.  Returns -ENOENT if the hwmod cannot be
1841  * reset this way, -EINVAL if the hwmod is in the wrong state,
1842  * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
1843  *
1844  * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
1845  * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
1846  * use the SYSCONFIG softreset bit to provide the status.
1847  *
1848  * Note that some IP like McBSP do have reset control but don't have
1849  * reset status.
1850  */
_ocp_softreset(struct omap_hwmod * oh)1851 static int _ocp_softreset(struct omap_hwmod *oh)
1852 {
1853 	u32 v;
1854 	int c = 0;
1855 	int ret = 0;
1856 
1857 	if (!oh->class->sysc ||
1858 	    !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
1859 		return -ENOENT;
1860 
1861 	/* clocks must be on for this operation */
1862 	if (oh->_state != _HWMOD_STATE_ENABLED) {
1863 		pr_warn("omap_hwmod: %s: reset can only be entered from enabled state\n",
1864 			oh->name);
1865 		return -EINVAL;
1866 	}
1867 
1868 	/* For some modules, all optionnal clocks need to be enabled as well */
1869 	if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1870 		_enable_optional_clocks(oh);
1871 
1872 	pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
1873 
1874 	v = oh->_sysc_cache;
1875 	ret = _set_softreset(oh, &v);
1876 	if (ret)
1877 		goto dis_opt_clks;
1878 
1879 	_write_sysconfig(v, oh);
1880 
1881 	if (oh->class->sysc->srst_udelay)
1882 		udelay(oh->class->sysc->srst_udelay);
1883 
1884 	c = _wait_softreset_complete(oh);
1885 	if (c == MAX_MODULE_SOFTRESET_WAIT) {
1886 		pr_warn("omap_hwmod: %s: softreset failed (waited %d usec)\n",
1887 			oh->name, MAX_MODULE_SOFTRESET_WAIT);
1888 		ret = -ETIMEDOUT;
1889 		goto dis_opt_clks;
1890 	} else {
1891 		pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
1892 	}
1893 
1894 	ret = _clear_softreset(oh, &v);
1895 	if (ret)
1896 		goto dis_opt_clks;
1897 
1898 	_write_sysconfig(v, oh);
1899 
1900 	/*
1901 	 * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
1902 	 * _wait_target_ready() or _reset()
1903 	 */
1904 
1905 dis_opt_clks:
1906 	if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
1907 		_disable_optional_clocks(oh);
1908 
1909 	return ret;
1910 }
1911 
1912 /**
1913  * _reset - reset an omap_hwmod
1914  * @oh: struct omap_hwmod *
1915  *
1916  * Resets an omap_hwmod @oh.  If the module has a custom reset
1917  * function pointer defined, then call it to reset the IP block, and
1918  * pass along its return value to the caller.  Otherwise, if the IP
1919  * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
1920  * associated with it, call a function to reset the IP block via that
1921  * method, and pass along the return value to the caller.  Finally, if
1922  * the IP block has some hardreset lines associated with it, assert
1923  * all of those, but do _not_ deassert them. (This is because driver
1924  * authors have expressed an apparent requirement to control the
1925  * deassertion of the hardreset lines themselves.)
1926  *
1927  * The default software reset mechanism for most OMAP IP blocks is
1928  * triggered via the OCP_SYSCONFIG.SOFTRESET bit.  However, some
1929  * hwmods cannot be reset via this method.  Some are not targets and
1930  * therefore have no OCP header registers to access.  Others (like the
1931  * IVA) have idiosyncratic reset sequences.  So for these relatively
1932  * rare cases, custom reset code can be supplied in the struct
1933  * omap_hwmod_class .reset function pointer.
1934  *
1935  * _set_dmadisable() is called to set the DMADISABLE bit so that it
1936  * does not prevent idling of the system. This is necessary for cases
1937  * where ROMCODE/BOOTLOADER uses dma and transfers control to the
1938  * kernel without disabling dma.
1939  *
1940  * Passes along the return value from either _ocp_softreset() or the
1941  * custom reset function - these must return -EINVAL if the hwmod
1942  * cannot be reset this way or if the hwmod is in the wrong state,
1943  * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
1944  */
_reset(struct omap_hwmod * oh)1945 static int _reset(struct omap_hwmod *oh)
1946 {
1947 	int i, r;
1948 
1949 	pr_debug("omap_hwmod: %s: resetting\n", oh->name);
1950 
1951 	if (oh->class->reset) {
1952 		r = oh->class->reset(oh);
1953 	} else {
1954 		if (oh->rst_lines_cnt > 0) {
1955 			for (i = 0; i < oh->rst_lines_cnt; i++)
1956 				_assert_hardreset(oh, oh->rst_lines[i].name);
1957 			return 0;
1958 		} else {
1959 			r = _ocp_softreset(oh);
1960 			if (r == -ENOENT)
1961 				r = 0;
1962 		}
1963 	}
1964 
1965 	_set_dmadisable(oh);
1966 
1967 	/*
1968 	 * OCP_SYSCONFIG bits need to be reprogrammed after a
1969 	 * softreset.  The _enable() function should be split to avoid
1970 	 * the rewrite of the OCP_SYSCONFIG register.
1971 	 */
1972 	if (oh->class->sysc) {
1973 		_update_sysc_cache(oh);
1974 		_enable_sysc(oh);
1975 	}
1976 
1977 	return r;
1978 }
1979 
1980 /**
1981  * _reconfigure_io_chain - clear any I/O chain wakeups and reconfigure chain
1982  *
1983  * Call the appropriate PRM function to clear any logged I/O chain
1984  * wakeups and to reconfigure the chain.  This apparently needs to be
1985  * done upon every mux change.  Since hwmods can be concurrently
1986  * enabled and idled, hold a spinlock around the I/O chain
1987  * reconfiguration sequence.  No return value.
1988  *
1989  * XXX When the PRM code is moved to drivers, this function can be removed,
1990  * as the PRM infrastructure should abstract this.
1991  */
_reconfigure_io_chain(void)1992 static void _reconfigure_io_chain(void)
1993 {
1994 	unsigned long flags;
1995 
1996 	spin_lock_irqsave(&io_chain_lock, flags);
1997 
1998 	omap_prm_reconfigure_io_chain();
1999 
2000 	spin_unlock_irqrestore(&io_chain_lock, flags);
2001 }
2002 
2003 /**
2004  * _omap4_update_context_lost - increment hwmod context loss counter if
2005  * hwmod context was lost, and clear hardware context loss reg
2006  * @oh: hwmod to check for context loss
2007  *
2008  * If the PRCM indicates that the hwmod @oh lost context, increment
2009  * our in-memory context loss counter, and clear the RM_*_CONTEXT
2010  * bits. No return value.
2011  */
_omap4_update_context_lost(struct omap_hwmod * oh)2012 static void _omap4_update_context_lost(struct omap_hwmod *oh)
2013 {
2014 	if (oh->prcm.omap4.flags & HWMOD_OMAP4_NO_CONTEXT_LOSS_BIT)
2015 		return;
2016 
2017 	if (!prm_was_any_context_lost_old(oh->clkdm->pwrdm.ptr->prcm_partition,
2018 					  oh->clkdm->pwrdm.ptr->prcm_offs,
2019 					  oh->prcm.omap4.context_offs))
2020 		return;
2021 
2022 	oh->prcm.omap4.context_lost_counter++;
2023 	prm_clear_context_loss_flags_old(oh->clkdm->pwrdm.ptr->prcm_partition,
2024 					 oh->clkdm->pwrdm.ptr->prcm_offs,
2025 					 oh->prcm.omap4.context_offs);
2026 }
2027 
2028 /**
2029  * _omap4_get_context_lost - get context loss counter for a hwmod
2030  * @oh: hwmod to get context loss counter for
2031  *
2032  * Returns the in-memory context loss counter for a hwmod.
2033  */
_omap4_get_context_lost(struct omap_hwmod * oh)2034 static int _omap4_get_context_lost(struct omap_hwmod *oh)
2035 {
2036 	return oh->prcm.omap4.context_lost_counter;
2037 }
2038 
2039 /**
2040  * _enable_preprogram - Pre-program an IP block during the _enable() process
2041  * @oh: struct omap_hwmod *
2042  *
2043  * Some IP blocks (such as AESS) require some additional programming
2044  * after enable before they can enter idle.  If a function pointer to
2045  * do so is present in the hwmod data, then call it and pass along the
2046  * return value; otherwise, return 0.
2047  */
_enable_preprogram(struct omap_hwmod * oh)2048 static int _enable_preprogram(struct omap_hwmod *oh)
2049 {
2050 	if (!oh->class->enable_preprogram)
2051 		return 0;
2052 
2053 	return oh->class->enable_preprogram(oh);
2054 }
2055 
2056 /**
2057  * _enable - enable an omap_hwmod
2058  * @oh: struct omap_hwmod *
2059  *
2060  * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
2061  * register target.  Returns -EINVAL if the hwmod is in the wrong
2062  * state or passes along the return value of _wait_target_ready().
2063  */
_enable(struct omap_hwmod * oh)2064 static int _enable(struct omap_hwmod *oh)
2065 {
2066 	int r;
2067 	int hwsup = 0;
2068 
2069 	pr_debug("omap_hwmod: %s: enabling\n", oh->name);
2070 
2071 	/*
2072 	 * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
2073 	 * state at init.  Now that someone is really trying to enable
2074 	 * them, just ensure that the hwmod mux is set.
2075 	 */
2076 	if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
2077 		/*
2078 		 * If the caller has mux data populated, do the mux'ing
2079 		 * which wouldn't have been done as part of the _enable()
2080 		 * done during setup.
2081 		 */
2082 		if (oh->mux)
2083 			omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
2084 
2085 		oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
2086 		return 0;
2087 	}
2088 
2089 	if (oh->_state != _HWMOD_STATE_INITIALIZED &&
2090 	    oh->_state != _HWMOD_STATE_IDLE &&
2091 	    oh->_state != _HWMOD_STATE_DISABLED) {
2092 		WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
2093 			oh->name);
2094 		return -EINVAL;
2095 	}
2096 
2097 	/*
2098 	 * If an IP block contains HW reset lines and all of them are
2099 	 * asserted, we let integration code associated with that
2100 	 * block handle the enable.  We've received very little
2101 	 * information on what those driver authors need, and until
2102 	 * detailed information is provided and the driver code is
2103 	 * posted to the public lists, this is probably the best we
2104 	 * can do.
2105 	 */
2106 	if (_are_all_hardreset_lines_asserted(oh))
2107 		return 0;
2108 
2109 	/* Mux pins for device runtime if populated */
2110 	if (oh->mux && (!oh->mux->enabled ||
2111 			((oh->_state == _HWMOD_STATE_IDLE) &&
2112 			 oh->mux->pads_dynamic))) {
2113 		omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
2114 		_reconfigure_io_chain();
2115 	} else if (oh->flags & HWMOD_RECONFIG_IO_CHAIN) {
2116 		_reconfigure_io_chain();
2117 	}
2118 
2119 	_add_initiator_dep(oh, mpu_oh);
2120 
2121 	if (oh->clkdm) {
2122 		/*
2123 		 * A clockdomain must be in SW_SUP before enabling
2124 		 * completely the module. The clockdomain can be set
2125 		 * in HW_AUTO only when the module become ready.
2126 		 */
2127 		hwsup = clkdm_in_hwsup(oh->clkdm) &&
2128 			!clkdm_missing_idle_reporting(oh->clkdm);
2129 		r = clkdm_hwmod_enable(oh->clkdm, oh);
2130 		if (r) {
2131 			WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
2132 			     oh->name, oh->clkdm->name, r);
2133 			return r;
2134 		}
2135 	}
2136 
2137 	_enable_clocks(oh);
2138 	if (soc_ops.enable_module)
2139 		soc_ops.enable_module(oh);
2140 	if (oh->flags & HWMOD_BLOCK_WFI)
2141 		cpu_idle_poll_ctrl(true);
2142 
2143 	if (soc_ops.update_context_lost)
2144 		soc_ops.update_context_lost(oh);
2145 
2146 	r = (soc_ops.wait_target_ready) ? soc_ops.wait_target_ready(oh) :
2147 		-EINVAL;
2148 	if (!r) {
2149 		/*
2150 		 * Set the clockdomain to HW_AUTO only if the target is ready,
2151 		 * assuming that the previous state was HW_AUTO
2152 		 */
2153 		if (oh->clkdm && hwsup)
2154 			clkdm_allow_idle(oh->clkdm);
2155 
2156 		oh->_state = _HWMOD_STATE_ENABLED;
2157 
2158 		/* Access the sysconfig only if the target is ready */
2159 		if (oh->class->sysc) {
2160 			if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
2161 				_update_sysc_cache(oh);
2162 			_enable_sysc(oh);
2163 		}
2164 		r = _enable_preprogram(oh);
2165 	} else {
2166 		if (soc_ops.disable_module)
2167 			soc_ops.disable_module(oh);
2168 		_disable_clocks(oh);
2169 		pr_err("omap_hwmod: %s: _wait_target_ready failed: %d\n",
2170 		       oh->name, r);
2171 
2172 		if (oh->clkdm)
2173 			clkdm_hwmod_disable(oh->clkdm, oh);
2174 	}
2175 
2176 	return r;
2177 }
2178 
2179 /**
2180  * _idle - idle an omap_hwmod
2181  * @oh: struct omap_hwmod *
2182  *
2183  * Idles an omap_hwmod @oh.  This should be called once the hwmod has
2184  * no further work.  Returns -EINVAL if the hwmod is in the wrong
2185  * state or returns 0.
2186  */
_idle(struct omap_hwmod * oh)2187 static int _idle(struct omap_hwmod *oh)
2188 {
2189 	if (oh->flags & HWMOD_NO_IDLE) {
2190 		oh->_int_flags |= _HWMOD_SKIP_ENABLE;
2191 		return 0;
2192 	}
2193 
2194 	pr_debug("omap_hwmod: %s: idling\n", oh->name);
2195 
2196 	if (oh->_state != _HWMOD_STATE_ENABLED) {
2197 		WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
2198 			oh->name);
2199 		return -EINVAL;
2200 	}
2201 
2202 	if (_are_all_hardreset_lines_asserted(oh))
2203 		return 0;
2204 
2205 	if (oh->class->sysc)
2206 		_idle_sysc(oh);
2207 	_del_initiator_dep(oh, mpu_oh);
2208 
2209 	if (oh->flags & HWMOD_BLOCK_WFI)
2210 		cpu_idle_poll_ctrl(false);
2211 	if (soc_ops.disable_module)
2212 		soc_ops.disable_module(oh);
2213 
2214 	/*
2215 	 * The module must be in idle mode before disabling any parents
2216 	 * clocks. Otherwise, the parent clock might be disabled before
2217 	 * the module transition is done, and thus will prevent the
2218 	 * transition to complete properly.
2219 	 */
2220 	_disable_clocks(oh);
2221 	if (oh->clkdm)
2222 		clkdm_hwmod_disable(oh->clkdm, oh);
2223 
2224 	/* Mux pins for device idle if populated */
2225 	if (oh->mux && oh->mux->pads_dynamic) {
2226 		omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
2227 		_reconfigure_io_chain();
2228 	} else if (oh->flags & HWMOD_RECONFIG_IO_CHAIN) {
2229 		_reconfigure_io_chain();
2230 	}
2231 
2232 	oh->_state = _HWMOD_STATE_IDLE;
2233 
2234 	return 0;
2235 }
2236 
2237 /**
2238  * _shutdown - shutdown an omap_hwmod
2239  * @oh: struct omap_hwmod *
2240  *
2241  * Shut down an omap_hwmod @oh.  This should be called when the driver
2242  * used for the hwmod is removed or unloaded or if the driver is not
2243  * used by the system.  Returns -EINVAL if the hwmod is in the wrong
2244  * state or returns 0.
2245  */
_shutdown(struct omap_hwmod * oh)2246 static int _shutdown(struct omap_hwmod *oh)
2247 {
2248 	int ret, i;
2249 	u8 prev_state;
2250 
2251 	if (oh->_state != _HWMOD_STATE_IDLE &&
2252 	    oh->_state != _HWMOD_STATE_ENABLED) {
2253 		WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
2254 			oh->name);
2255 		return -EINVAL;
2256 	}
2257 
2258 	if (_are_all_hardreset_lines_asserted(oh))
2259 		return 0;
2260 
2261 	pr_debug("omap_hwmod: %s: disabling\n", oh->name);
2262 
2263 	if (oh->class->pre_shutdown) {
2264 		prev_state = oh->_state;
2265 		if (oh->_state == _HWMOD_STATE_IDLE)
2266 			_enable(oh);
2267 		ret = oh->class->pre_shutdown(oh);
2268 		if (ret) {
2269 			if (prev_state == _HWMOD_STATE_IDLE)
2270 				_idle(oh);
2271 			return ret;
2272 		}
2273 	}
2274 
2275 	if (oh->class->sysc) {
2276 		if (oh->_state == _HWMOD_STATE_IDLE)
2277 			_enable(oh);
2278 		_shutdown_sysc(oh);
2279 	}
2280 
2281 	/* clocks and deps are already disabled in idle */
2282 	if (oh->_state == _HWMOD_STATE_ENABLED) {
2283 		_del_initiator_dep(oh, mpu_oh);
2284 		/* XXX what about the other system initiators here? dma, dsp */
2285 		if (oh->flags & HWMOD_BLOCK_WFI)
2286 			cpu_idle_poll_ctrl(false);
2287 		if (soc_ops.disable_module)
2288 			soc_ops.disable_module(oh);
2289 		_disable_clocks(oh);
2290 		if (oh->clkdm)
2291 			clkdm_hwmod_disable(oh->clkdm, oh);
2292 	}
2293 	/* XXX Should this code also force-disable the optional clocks? */
2294 
2295 	for (i = 0; i < oh->rst_lines_cnt; i++)
2296 		_assert_hardreset(oh, oh->rst_lines[i].name);
2297 
2298 	/* Mux pins to safe mode or use populated off mode values */
2299 	if (oh->mux)
2300 		omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
2301 
2302 	oh->_state = _HWMOD_STATE_DISABLED;
2303 
2304 	return 0;
2305 }
2306 
of_dev_find_hwmod(struct device_node * np,struct omap_hwmod * oh)2307 static int of_dev_find_hwmod(struct device_node *np,
2308 			     struct omap_hwmod *oh)
2309 {
2310 	int count, i, res;
2311 	const char *p;
2312 
2313 	count = of_property_count_strings(np, "ti,hwmods");
2314 	if (count < 1)
2315 		return -ENODEV;
2316 
2317 	for (i = 0; i < count; i++) {
2318 		res = of_property_read_string_index(np, "ti,hwmods",
2319 						    i, &p);
2320 		if (res)
2321 			continue;
2322 		if (!strcmp(p, oh->name)) {
2323 			pr_debug("omap_hwmod: dt %s[%i] uses hwmod %s\n",
2324 				 np->name, i, oh->name);
2325 			return i;
2326 		}
2327 	}
2328 
2329 	return -ENODEV;
2330 }
2331 
2332 /**
2333  * of_dev_hwmod_lookup - look up needed hwmod from dt blob
2334  * @np: struct device_node *
2335  * @oh: struct omap_hwmod *
2336  * @index: index of the entry found
2337  * @found: struct device_node * found or NULL
2338  *
2339  * Parse the dt blob and find out needed hwmod. Recursive function is
2340  * implemented to take care hierarchical dt blob parsing.
2341  * Return: Returns 0 on success, -ENODEV when not found.
2342  */
of_dev_hwmod_lookup(struct device_node * np,struct omap_hwmod * oh,int * index,struct device_node ** found)2343 static int of_dev_hwmod_lookup(struct device_node *np,
2344 			       struct omap_hwmod *oh,
2345 			       int *index,
2346 			       struct device_node **found)
2347 {
2348 	struct device_node *np0 = NULL;
2349 	int res;
2350 
2351 	res = of_dev_find_hwmod(np, oh);
2352 	if (res >= 0) {
2353 		*found = np;
2354 		*index = res;
2355 		return 0;
2356 	}
2357 
2358 	for_each_child_of_node(np, np0) {
2359 		struct device_node *fc;
2360 		int i;
2361 
2362 		res = of_dev_hwmod_lookup(np0, oh, &i, &fc);
2363 		if (res == 0) {
2364 			*found = fc;
2365 			*index = i;
2366 			return 0;
2367 		}
2368 	}
2369 
2370 	*found = NULL;
2371 	*index = 0;
2372 
2373 	return -ENODEV;
2374 }
2375 
2376 /**
2377  * _init_mpu_rt_base - populate the virtual address for a hwmod
2378  * @oh: struct omap_hwmod * to locate the virtual address
2379  * @data: (unused, caller should pass NULL)
2380  * @index: index of the reg entry iospace in device tree
2381  * @np: struct device_node * of the IP block's device node in the DT data
2382  *
2383  * Cache the virtual address used by the MPU to access this IP block's
2384  * registers.  This address is needed early so the OCP registers that
2385  * are part of the device's address space can be ioremapped properly.
2386  *
2387  * If SYSC access is not needed, the registers will not be remapped
2388  * and non-availability of MPU access is not treated as an error.
2389  *
2390  * Returns 0 on success, -EINVAL if an invalid hwmod is passed, and
2391  * -ENXIO on absent or invalid register target address space.
2392  */
_init_mpu_rt_base(struct omap_hwmod * oh,void * data,int index,struct device_node * np)2393 static int __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data,
2394 				    int index, struct device_node *np)
2395 {
2396 	struct omap_hwmod_addr_space *mem;
2397 	void __iomem *va_start = NULL;
2398 
2399 	if (!oh)
2400 		return -EINVAL;
2401 
2402 	_save_mpu_port_index(oh);
2403 
2404 	/* if we don't need sysc access we don't need to ioremap */
2405 	if (!oh->class->sysc)
2406 		return 0;
2407 
2408 	/* we can't continue without MPU PORT if we need sysc access */
2409 	if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
2410 		return -ENXIO;
2411 
2412 	mem = _find_mpu_rt_addr_space(oh);
2413 	if (!mem) {
2414 		pr_debug("omap_hwmod: %s: no MPU register target found\n",
2415 			 oh->name);
2416 
2417 		/* Extract the IO space from device tree blob */
2418 		if (!np) {
2419 			pr_err("omap_hwmod: %s: no dt node\n", oh->name);
2420 			return -ENXIO;
2421 		}
2422 
2423 		va_start = of_iomap(np, index + oh->mpu_rt_idx);
2424 	} else {
2425 		va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
2426 	}
2427 
2428 	if (!va_start) {
2429 		if (mem)
2430 			pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
2431 		else
2432 			pr_err("omap_hwmod: %s: Missing dt reg%i for %s\n",
2433 			       oh->name, index, np->full_name);
2434 		return -ENXIO;
2435 	}
2436 
2437 	pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
2438 		 oh->name, va_start);
2439 
2440 	oh->_mpu_rt_va = va_start;
2441 	return 0;
2442 }
2443 
2444 /**
2445  * _init - initialize internal data for the hwmod @oh
2446  * @oh: struct omap_hwmod *
2447  * @n: (unused)
2448  *
2449  * Look up the clocks and the address space used by the MPU to access
2450  * registers belonging to the hwmod @oh.  @oh must already be
2451  * registered at this point.  This is the first of two phases for
2452  * hwmod initialization.  Code called here does not touch any hardware
2453  * registers, it simply prepares internal data structures.  Returns 0
2454  * upon success or if the hwmod isn't registered or if the hwmod's
2455  * address space is not defined, or -EINVAL upon failure.
2456  */
_init(struct omap_hwmod * oh,void * data)2457 static int __init _init(struct omap_hwmod *oh, void *data)
2458 {
2459 	int r, index;
2460 	struct device_node *np = NULL;
2461 
2462 	if (oh->_state != _HWMOD_STATE_REGISTERED)
2463 		return 0;
2464 
2465 	if (of_have_populated_dt()) {
2466 		struct device_node *bus;
2467 
2468 		bus = of_find_node_by_name(NULL, "ocp");
2469 		if (!bus)
2470 			return -ENODEV;
2471 
2472 		r = of_dev_hwmod_lookup(bus, oh, &index, &np);
2473 		if (r)
2474 			pr_debug("omap_hwmod: %s missing dt data\n", oh->name);
2475 		else if (np && index)
2476 			pr_warn("omap_hwmod: %s using broken dt data from %s\n",
2477 				oh->name, np->name);
2478 	}
2479 
2480 	r = _init_mpu_rt_base(oh, NULL, index, np);
2481 	if (r < 0) {
2482 		WARN(1, "omap_hwmod: %s: doesn't have mpu register target base\n",
2483 		     oh->name);
2484 		return 0;
2485 	}
2486 
2487 	r = _init_clocks(oh, NULL);
2488 	if (r < 0) {
2489 		WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
2490 		return -EINVAL;
2491 	}
2492 
2493 	if (np) {
2494 		if (of_find_property(np, "ti,no-reset-on-init", NULL))
2495 			oh->flags |= HWMOD_INIT_NO_RESET;
2496 		if (of_find_property(np, "ti,no-idle-on-init", NULL))
2497 			oh->flags |= HWMOD_INIT_NO_IDLE;
2498 		if (of_find_property(np, "ti,no-idle", NULL))
2499 			oh->flags |= HWMOD_NO_IDLE;
2500 	}
2501 
2502 	oh->_state = _HWMOD_STATE_INITIALIZED;
2503 
2504 	return 0;
2505 }
2506 
2507 /**
2508  * _setup_iclk_autoidle - configure an IP block's interface clocks
2509  * @oh: struct omap_hwmod *
2510  *
2511  * Set up the module's interface clocks.  XXX This function is still mostly
2512  * a stub; implementing this properly requires iclk autoidle usecounting in
2513  * the clock code.   No return value.
2514  */
_setup_iclk_autoidle(struct omap_hwmod * oh)2515 static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
2516 {
2517 	struct omap_hwmod_ocp_if *os;
2518 	struct list_head *p;
2519 	int i = 0;
2520 	if (oh->_state != _HWMOD_STATE_INITIALIZED)
2521 		return;
2522 
2523 	p = oh->slave_ports.next;
2524 
2525 	while (i < oh->slaves_cnt) {
2526 		os = _fetch_next_ocp_if(&p, &i);
2527 		if (!os->_clk)
2528 			continue;
2529 
2530 		if (os->flags & OCPIF_SWSUP_IDLE) {
2531 			/* XXX omap_iclk_deny_idle(c); */
2532 		} else {
2533 			/* XXX omap_iclk_allow_idle(c); */
2534 			clk_enable(os->_clk);
2535 		}
2536 	}
2537 
2538 	return;
2539 }
2540 
2541 /**
2542  * _setup_reset - reset an IP block during the setup process
2543  * @oh: struct omap_hwmod *
2544  *
2545  * Reset the IP block corresponding to the hwmod @oh during the setup
2546  * process.  The IP block is first enabled so it can be successfully
2547  * reset.  Returns 0 upon success or a negative error code upon
2548  * failure.
2549  */
_setup_reset(struct omap_hwmod * oh)2550 static int __init _setup_reset(struct omap_hwmod *oh)
2551 {
2552 	int r;
2553 
2554 	if (oh->_state != _HWMOD_STATE_INITIALIZED)
2555 		return -EINVAL;
2556 
2557 	if (oh->flags & HWMOD_EXT_OPT_MAIN_CLK)
2558 		return -EPERM;
2559 
2560 	if (oh->rst_lines_cnt == 0) {
2561 		r = _enable(oh);
2562 		if (r) {
2563 			pr_warn("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
2564 				oh->name, oh->_state);
2565 			return -EINVAL;
2566 		}
2567 	}
2568 
2569 	if (!(oh->flags & HWMOD_INIT_NO_RESET))
2570 		r = _reset(oh);
2571 
2572 	return r;
2573 }
2574 
2575 /**
2576  * _setup_postsetup - transition to the appropriate state after _setup
2577  * @oh: struct omap_hwmod *
2578  *
2579  * Place an IP block represented by @oh into a "post-setup" state --
2580  * either IDLE, ENABLED, or DISABLED.  ("post-setup" simply means that
2581  * this function is called at the end of _setup().)  The postsetup
2582  * state for an IP block can be changed by calling
2583  * omap_hwmod_enter_postsetup_state() early in the boot process,
2584  * before one of the omap_hwmod_setup*() functions are called for the
2585  * IP block.
2586  *
2587  * The IP block stays in this state until a PM runtime-based driver is
2588  * loaded for that IP block.  A post-setup state of IDLE is
2589  * appropriate for almost all IP blocks with runtime PM-enabled
2590  * drivers, since those drivers are able to enable the IP block.  A
2591  * post-setup state of ENABLED is appropriate for kernels with PM
2592  * runtime disabled.  The DISABLED state is appropriate for unusual IP
2593  * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
2594  * included, since the WDTIMER starts running on reset and will reset
2595  * the MPU if left active.
2596  *
2597  * This post-setup mechanism is deprecated.  Once all of the OMAP
2598  * drivers have been converted to use PM runtime, and all of the IP
2599  * block data and interconnect data is available to the hwmod code, it
2600  * should be possible to replace this mechanism with a "lazy reset"
2601  * arrangement.  In a "lazy reset" setup, each IP block is enabled
2602  * when the driver first probes, then all remaining IP blocks without
2603  * drivers are either shut down or enabled after the drivers have
2604  * loaded.  However, this cannot take place until the above
2605  * preconditions have been met, since otherwise the late reset code
2606  * has no way of knowing which IP blocks are in use by drivers, and
2607  * which ones are unused.
2608  *
2609  * No return value.
2610  */
_setup_postsetup(struct omap_hwmod * oh)2611 static void __init _setup_postsetup(struct omap_hwmod *oh)
2612 {
2613 	u8 postsetup_state;
2614 
2615 	if (oh->rst_lines_cnt > 0)
2616 		return;
2617 
2618 	postsetup_state = oh->_postsetup_state;
2619 	if (postsetup_state == _HWMOD_STATE_UNKNOWN)
2620 		postsetup_state = _HWMOD_STATE_ENABLED;
2621 
2622 	/*
2623 	 * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
2624 	 * it should be set by the core code as a runtime flag during startup
2625 	 */
2626 	if ((oh->flags & (HWMOD_INIT_NO_IDLE | HWMOD_NO_IDLE)) &&
2627 	    (postsetup_state == _HWMOD_STATE_IDLE)) {
2628 		oh->_int_flags |= _HWMOD_SKIP_ENABLE;
2629 		postsetup_state = _HWMOD_STATE_ENABLED;
2630 	}
2631 
2632 	if (postsetup_state == _HWMOD_STATE_IDLE)
2633 		_idle(oh);
2634 	else if (postsetup_state == _HWMOD_STATE_DISABLED)
2635 		_shutdown(oh);
2636 	else if (postsetup_state != _HWMOD_STATE_ENABLED)
2637 		WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2638 		     oh->name, postsetup_state);
2639 
2640 	return;
2641 }
2642 
2643 /**
2644  * _setup - prepare IP block hardware for use
2645  * @oh: struct omap_hwmod *
2646  * @n: (unused, pass NULL)
2647  *
2648  * Configure the IP block represented by @oh.  This may include
2649  * enabling the IP block, resetting it, and placing it into a
2650  * post-setup state, depending on the type of IP block and applicable
2651  * flags.  IP blocks are reset to prevent any previous configuration
2652  * by the bootloader or previous operating system from interfering
2653  * with power management or other parts of the system.  The reset can
2654  * be avoided; see omap_hwmod_no_setup_reset().  This is the second of
2655  * two phases for hwmod initialization.  Code called here generally
2656  * affects the IP block hardware, or system integration hardware
2657  * associated with the IP block.  Returns 0.
2658  */
_setup(struct omap_hwmod * oh,void * data)2659 static int __init _setup(struct omap_hwmod *oh, void *data)
2660 {
2661 	if (oh->_state != _HWMOD_STATE_INITIALIZED)
2662 		return 0;
2663 
2664 	if (oh->parent_hwmod) {
2665 		int r;
2666 
2667 		r = _enable(oh->parent_hwmod);
2668 		WARN(r, "hwmod: %s: setup: failed to enable parent hwmod %s\n",
2669 		     oh->name, oh->parent_hwmod->name);
2670 	}
2671 
2672 	_setup_iclk_autoidle(oh);
2673 
2674 	if (!_setup_reset(oh))
2675 		_setup_postsetup(oh);
2676 
2677 	if (oh->parent_hwmod) {
2678 		u8 postsetup_state;
2679 
2680 		postsetup_state = oh->parent_hwmod->_postsetup_state;
2681 
2682 		if (postsetup_state == _HWMOD_STATE_IDLE)
2683 			_idle(oh->parent_hwmod);
2684 		else if (postsetup_state == _HWMOD_STATE_DISABLED)
2685 			_shutdown(oh->parent_hwmod);
2686 		else if (postsetup_state != _HWMOD_STATE_ENABLED)
2687 			WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
2688 			     oh->parent_hwmod->name, postsetup_state);
2689 	}
2690 
2691 	return 0;
2692 }
2693 
2694 /**
2695  * _register - register a struct omap_hwmod
2696  * @oh: struct omap_hwmod *
2697  *
2698  * Registers the omap_hwmod @oh.  Returns -EEXIST if an omap_hwmod
2699  * already has been registered by the same name; -EINVAL if the
2700  * omap_hwmod is in the wrong state, if @oh is NULL, if the
2701  * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
2702  * name, or if the omap_hwmod's class is missing a name; or 0 upon
2703  * success.
2704  *
2705  * XXX The data should be copied into bootmem, so the original data
2706  * should be marked __initdata and freed after init.  This would allow
2707  * unneeded omap_hwmods to be freed on multi-OMAP configurations.  Note
2708  * that the copy process would be relatively complex due to the large number
2709  * of substructures.
2710  */
_register(struct omap_hwmod * oh)2711 static int __init _register(struct omap_hwmod *oh)
2712 {
2713 	if (!oh || !oh->name || !oh->class || !oh->class->name ||
2714 	    (oh->_state != _HWMOD_STATE_UNKNOWN))
2715 		return -EINVAL;
2716 
2717 	pr_debug("omap_hwmod: %s: registering\n", oh->name);
2718 
2719 	if (_lookup(oh->name))
2720 		return -EEXIST;
2721 
2722 	list_add_tail(&oh->node, &omap_hwmod_list);
2723 
2724 	INIT_LIST_HEAD(&oh->master_ports);
2725 	INIT_LIST_HEAD(&oh->slave_ports);
2726 	spin_lock_init(&oh->_lock);
2727 	lockdep_set_class(&oh->_lock, &oh->hwmod_key);
2728 
2729 	oh->_state = _HWMOD_STATE_REGISTERED;
2730 
2731 	/*
2732 	 * XXX Rather than doing a strcmp(), this should test a flag
2733 	 * set in the hwmod data, inserted by the autogenerator code.
2734 	 */
2735 	if (!strcmp(oh->name, MPU_INITIATOR_NAME))
2736 		mpu_oh = oh;
2737 
2738 	return 0;
2739 }
2740 
2741 /**
2742  * _alloc_links - return allocated memory for hwmod links
2743  * @ml: pointer to a struct omap_hwmod_link * for the master link
2744  * @sl: pointer to a struct omap_hwmod_link * for the slave link
2745  *
2746  * Return pointers to two struct omap_hwmod_link records, via the
2747  * addresses pointed to by @ml and @sl.  Will first attempt to return
2748  * memory allocated as part of a large initial block, but if that has
2749  * been exhausted, will allocate memory itself.  Since ideally this
2750  * second allocation path will never occur, the number of these
2751  * 'supplemental' allocations will be logged when debugging is
2752  * enabled.  Returns 0.
2753  */
_alloc_links(struct omap_hwmod_link ** ml,struct omap_hwmod_link ** sl)2754 static int __init _alloc_links(struct omap_hwmod_link **ml,
2755 			       struct omap_hwmod_link **sl)
2756 {
2757 	unsigned int sz;
2758 
2759 	if ((free_ls + LINKS_PER_OCP_IF) <= max_ls) {
2760 		*ml = &linkspace[free_ls++];
2761 		*sl = &linkspace[free_ls++];
2762 		return 0;
2763 	}
2764 
2765 	sz = sizeof(struct omap_hwmod_link) * LINKS_PER_OCP_IF;
2766 
2767 	*sl = NULL;
2768 	*ml = memblock_virt_alloc(sz, 0);
2769 
2770 	*sl = (void *)(*ml) + sizeof(struct omap_hwmod_link);
2771 
2772 	ls_supp++;
2773 	pr_debug("omap_hwmod: supplemental link allocations needed: %d\n",
2774 		 ls_supp * LINKS_PER_OCP_IF);
2775 
2776 	return 0;
2777 };
2778 
2779 /**
2780  * _add_link - add an interconnect between two IP blocks
2781  * @oi: pointer to a struct omap_hwmod_ocp_if record
2782  *
2783  * Add struct omap_hwmod_link records connecting the master IP block
2784  * specified in @oi->master to @oi, and connecting the slave IP block
2785  * specified in @oi->slave to @oi.  This code is assumed to run before
2786  * preemption or SMP has been enabled, thus avoiding the need for
2787  * locking in this code.  Changes to this assumption will require
2788  * additional locking.  Returns 0.
2789  */
_add_link(struct omap_hwmod_ocp_if * oi)2790 static int __init _add_link(struct omap_hwmod_ocp_if *oi)
2791 {
2792 	struct omap_hwmod_link *ml, *sl;
2793 
2794 	pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
2795 		 oi->slave->name);
2796 
2797 	_alloc_links(&ml, &sl);
2798 
2799 	ml->ocp_if = oi;
2800 	list_add(&ml->node, &oi->master->master_ports);
2801 	oi->master->masters_cnt++;
2802 
2803 	sl->ocp_if = oi;
2804 	list_add(&sl->node, &oi->slave->slave_ports);
2805 	oi->slave->slaves_cnt++;
2806 
2807 	return 0;
2808 }
2809 
2810 /**
2811  * _register_link - register a struct omap_hwmod_ocp_if
2812  * @oi: struct omap_hwmod_ocp_if *
2813  *
2814  * Registers the omap_hwmod_ocp_if record @oi.  Returns -EEXIST if it
2815  * has already been registered; -EINVAL if @oi is NULL or if the
2816  * record pointed to by @oi is missing required fields; or 0 upon
2817  * success.
2818  *
2819  * XXX The data should be copied into bootmem, so the original data
2820  * should be marked __initdata and freed after init.  This would allow
2821  * unneeded omap_hwmods to be freed on multi-OMAP configurations.
2822  */
_register_link(struct omap_hwmod_ocp_if * oi)2823 static int __init _register_link(struct omap_hwmod_ocp_if *oi)
2824 {
2825 	if (!oi || !oi->master || !oi->slave || !oi->user)
2826 		return -EINVAL;
2827 
2828 	if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
2829 		return -EEXIST;
2830 
2831 	pr_debug("omap_hwmod: registering link from %s to %s\n",
2832 		 oi->master->name, oi->slave->name);
2833 
2834 	/*
2835 	 * Register the connected hwmods, if they haven't been
2836 	 * registered already
2837 	 */
2838 	if (oi->master->_state != _HWMOD_STATE_REGISTERED)
2839 		_register(oi->master);
2840 
2841 	if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
2842 		_register(oi->slave);
2843 
2844 	_add_link(oi);
2845 
2846 	oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
2847 
2848 	return 0;
2849 }
2850 
2851 /**
2852  * _alloc_linkspace - allocate large block of hwmod links
2853  * @ois: pointer to an array of struct omap_hwmod_ocp_if records to count
2854  *
2855  * Allocate a large block of struct omap_hwmod_link records.  This
2856  * improves boot time significantly by avoiding the need to allocate
2857  * individual records one by one.  If the number of records to
2858  * allocate in the block hasn't been manually specified, this function
2859  * will count the number of struct omap_hwmod_ocp_if records in @ois
2860  * and use that to determine the allocation size.  For SoC families
2861  * that require multiple list registrations, such as OMAP3xxx, this
2862  * estimation process isn't optimal, so manual estimation is advised
2863  * in those cases.  Returns -EEXIST if the allocation has already occurred
2864  * or 0 upon success.
2865  */
_alloc_linkspace(struct omap_hwmod_ocp_if ** ois)2866 static int __init _alloc_linkspace(struct omap_hwmod_ocp_if **ois)
2867 {
2868 	unsigned int i = 0;
2869 	unsigned int sz;
2870 
2871 	if (linkspace) {
2872 		WARN(1, "linkspace already allocated\n");
2873 		return -EEXIST;
2874 	}
2875 
2876 	if (max_ls == 0)
2877 		while (ois[i++])
2878 			max_ls += LINKS_PER_OCP_IF;
2879 
2880 	sz = sizeof(struct omap_hwmod_link) * max_ls;
2881 
2882 	pr_debug("omap_hwmod: %s: allocating %d byte linkspace (%d links)\n",
2883 		 __func__, sz, max_ls);
2884 
2885 	linkspace = memblock_virt_alloc(sz, 0);
2886 
2887 	return 0;
2888 }
2889 
2890 /* Static functions intended only for use in soc_ops field function pointers */
2891 
2892 /**
2893  * _omap2xxx_3xxx_wait_target_ready - wait for a module to leave slave idle
2894  * @oh: struct omap_hwmod *
2895  *
2896  * Wait for a module @oh to leave slave idle.  Returns 0 if the module
2897  * does not have an IDLEST bit or if the module successfully leaves
2898  * slave idle; otherwise, pass along the return value of the
2899  * appropriate *_cm*_wait_module_ready() function.
2900  */
_omap2xxx_3xxx_wait_target_ready(struct omap_hwmod * oh)2901 static int _omap2xxx_3xxx_wait_target_ready(struct omap_hwmod *oh)
2902 {
2903 	if (!oh)
2904 		return -EINVAL;
2905 
2906 	if (oh->flags & HWMOD_NO_IDLEST)
2907 		return 0;
2908 
2909 	if (!_find_mpu_rt_port(oh))
2910 		return 0;
2911 
2912 	/* XXX check module SIDLEMODE, hardreset status, enabled clocks */
2913 
2914 	return omap_cm_wait_module_ready(0, oh->prcm.omap2.module_offs,
2915 					 oh->prcm.omap2.idlest_reg_id,
2916 					 oh->prcm.omap2.idlest_idle_bit);
2917 }
2918 
2919 /**
2920  * _omap4_wait_target_ready - wait for a module to leave slave idle
2921  * @oh: struct omap_hwmod *
2922  *
2923  * Wait for a module @oh to leave slave idle.  Returns 0 if the module
2924  * does not have an IDLEST bit or if the module successfully leaves
2925  * slave idle; otherwise, pass along the return value of the
2926  * appropriate *_cm*_wait_module_ready() function.
2927  */
_omap4_wait_target_ready(struct omap_hwmod * oh)2928 static int _omap4_wait_target_ready(struct omap_hwmod *oh)
2929 {
2930 	if (!oh)
2931 		return -EINVAL;
2932 
2933 	if (oh->flags & HWMOD_NO_IDLEST || !oh->clkdm)
2934 		return 0;
2935 
2936 	if (!_find_mpu_rt_port(oh))
2937 		return 0;
2938 
2939 	/* XXX check module SIDLEMODE, hardreset status */
2940 
2941 	return omap_cm_wait_module_ready(oh->clkdm->prcm_partition,
2942 					 oh->clkdm->cm_inst,
2943 					 oh->prcm.omap4.clkctrl_offs, 0);
2944 }
2945 
2946 /**
2947  * _omap2_assert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2948  * @oh: struct omap_hwmod * to assert hardreset
2949  * @ohri: hardreset line data
2950  *
2951  * Call omap2_prm_assert_hardreset() with parameters extracted from
2952  * the hwmod @oh and the hardreset line data @ohri.  Only intended for
2953  * use as an soc_ops function pointer.  Passes along the return value
2954  * from omap2_prm_assert_hardreset().  XXX This function is scheduled
2955  * for removal when the PRM code is moved into drivers/.
2956  */
_omap2_assert_hardreset(struct omap_hwmod * oh,struct omap_hwmod_rst_info * ohri)2957 static int _omap2_assert_hardreset(struct omap_hwmod *oh,
2958 				   struct omap_hwmod_rst_info *ohri)
2959 {
2960 	return omap_prm_assert_hardreset(ohri->rst_shift, 0,
2961 					 oh->prcm.omap2.module_offs, 0);
2962 }
2963 
2964 /**
2965  * _omap2_deassert_hardreset - call OMAP2 PRM hardreset fn with hwmod args
2966  * @oh: struct omap_hwmod * to deassert hardreset
2967  * @ohri: hardreset line data
2968  *
2969  * Call omap2_prm_deassert_hardreset() with parameters extracted from
2970  * the hwmod @oh and the hardreset line data @ohri.  Only intended for
2971  * use as an soc_ops function pointer.  Passes along the return value
2972  * from omap2_prm_deassert_hardreset().  XXX This function is
2973  * scheduled for removal when the PRM code is moved into drivers/.
2974  */
_omap2_deassert_hardreset(struct omap_hwmod * oh,struct omap_hwmod_rst_info * ohri)2975 static int _omap2_deassert_hardreset(struct omap_hwmod *oh,
2976 				     struct omap_hwmod_rst_info *ohri)
2977 {
2978 	return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->st_shift, 0,
2979 					   oh->prcm.omap2.module_offs, 0, 0);
2980 }
2981 
2982 /**
2983  * _omap2_is_hardreset_asserted - call OMAP2 PRM hardreset fn with hwmod args
2984  * @oh: struct omap_hwmod * to test hardreset
2985  * @ohri: hardreset line data
2986  *
2987  * Call omap2_prm_is_hardreset_asserted() with parameters extracted
2988  * from the hwmod @oh and the hardreset line data @ohri.  Only
2989  * intended for use as an soc_ops function pointer.  Passes along the
2990  * return value from omap2_prm_is_hardreset_asserted().  XXX This
2991  * function is scheduled for removal when the PRM code is moved into
2992  * drivers/.
2993  */
_omap2_is_hardreset_asserted(struct omap_hwmod * oh,struct omap_hwmod_rst_info * ohri)2994 static int _omap2_is_hardreset_asserted(struct omap_hwmod *oh,
2995 					struct omap_hwmod_rst_info *ohri)
2996 {
2997 	return omap_prm_is_hardreset_asserted(ohri->st_shift, 0,
2998 					      oh->prcm.omap2.module_offs, 0);
2999 }
3000 
3001 /**
3002  * _omap4_assert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
3003  * @oh: struct omap_hwmod * to assert hardreset
3004  * @ohri: hardreset line data
3005  *
3006  * Call omap4_prminst_assert_hardreset() with parameters extracted
3007  * from the hwmod @oh and the hardreset line data @ohri.  Only
3008  * intended for use as an soc_ops function pointer.  Passes along the
3009  * return value from omap4_prminst_assert_hardreset().  XXX This
3010  * function is scheduled for removal when the PRM code is moved into
3011  * drivers/.
3012  */
_omap4_assert_hardreset(struct omap_hwmod * oh,struct omap_hwmod_rst_info * ohri)3013 static int _omap4_assert_hardreset(struct omap_hwmod *oh,
3014 				   struct omap_hwmod_rst_info *ohri)
3015 {
3016 	if (!oh->clkdm)
3017 		return -EINVAL;
3018 
3019 	return omap_prm_assert_hardreset(ohri->rst_shift,
3020 					 oh->clkdm->pwrdm.ptr->prcm_partition,
3021 					 oh->clkdm->pwrdm.ptr->prcm_offs,
3022 					 oh->prcm.omap4.rstctrl_offs);
3023 }
3024 
3025 /**
3026  * _omap4_deassert_hardreset - call OMAP4 PRM hardreset fn with hwmod args
3027  * @oh: struct omap_hwmod * to deassert hardreset
3028  * @ohri: hardreset line data
3029  *
3030  * Call omap4_prminst_deassert_hardreset() with parameters extracted
3031  * from the hwmod @oh and the hardreset line data @ohri.  Only
3032  * intended for use as an soc_ops function pointer.  Passes along the
3033  * return value from omap4_prminst_deassert_hardreset().  XXX This
3034  * function is scheduled for removal when the PRM code is moved into
3035  * drivers/.
3036  */
_omap4_deassert_hardreset(struct omap_hwmod * oh,struct omap_hwmod_rst_info * ohri)3037 static int _omap4_deassert_hardreset(struct omap_hwmod *oh,
3038 				     struct omap_hwmod_rst_info *ohri)
3039 {
3040 	if (!oh->clkdm)
3041 		return -EINVAL;
3042 
3043 	if (ohri->st_shift)
3044 		pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
3045 		       oh->name, ohri->name);
3046 	return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->rst_shift,
3047 					   oh->clkdm->pwrdm.ptr->prcm_partition,
3048 					   oh->clkdm->pwrdm.ptr->prcm_offs,
3049 					   oh->prcm.omap4.rstctrl_offs,
3050 					   oh->prcm.omap4.rstctrl_offs +
3051 					   OMAP4_RST_CTRL_ST_OFFSET);
3052 }
3053 
3054 /**
3055  * _omap4_is_hardreset_asserted - call OMAP4 PRM hardreset fn with hwmod args
3056  * @oh: struct omap_hwmod * to test hardreset
3057  * @ohri: hardreset line data
3058  *
3059  * Call omap4_prminst_is_hardreset_asserted() with parameters
3060  * extracted from the hwmod @oh and the hardreset line data @ohri.
3061  * Only intended for use as an soc_ops function pointer.  Passes along
3062  * the return value from omap4_prminst_is_hardreset_asserted().  XXX
3063  * This function is scheduled for removal when the PRM code is moved
3064  * into drivers/.
3065  */
_omap4_is_hardreset_asserted(struct omap_hwmod * oh,struct omap_hwmod_rst_info * ohri)3066 static int _omap4_is_hardreset_asserted(struct omap_hwmod *oh,
3067 					struct omap_hwmod_rst_info *ohri)
3068 {
3069 	if (!oh->clkdm)
3070 		return -EINVAL;
3071 
3072 	return omap_prm_is_hardreset_asserted(ohri->rst_shift,
3073 					      oh->clkdm->pwrdm.ptr->
3074 					      prcm_partition,
3075 					      oh->clkdm->pwrdm.ptr->prcm_offs,
3076 					      oh->prcm.omap4.rstctrl_offs);
3077 }
3078 
3079 /**
3080  * _am33xx_deassert_hardreset - call AM33XX PRM hardreset fn with hwmod args
3081  * @oh: struct omap_hwmod * to deassert hardreset
3082  * @ohri: hardreset line data
3083  *
3084  * Call am33xx_prminst_deassert_hardreset() with parameters extracted
3085  * from the hwmod @oh and the hardreset line data @ohri.  Only
3086  * intended for use as an soc_ops function pointer.  Passes along the
3087  * return value from am33xx_prminst_deassert_hardreset().  XXX This
3088  * function is scheduled for removal when the PRM code is moved into
3089  * drivers/.
3090  */
_am33xx_deassert_hardreset(struct omap_hwmod * oh,struct omap_hwmod_rst_info * ohri)3091 static int _am33xx_deassert_hardreset(struct omap_hwmod *oh,
3092 				     struct omap_hwmod_rst_info *ohri)
3093 {
3094 	return omap_prm_deassert_hardreset(ohri->rst_shift, ohri->st_shift,
3095 					   oh->clkdm->pwrdm.ptr->prcm_partition,
3096 					   oh->clkdm->pwrdm.ptr->prcm_offs,
3097 					   oh->prcm.omap4.rstctrl_offs,
3098 					   oh->prcm.omap4.rstst_offs);
3099 }
3100 
3101 /* Public functions */
3102 
omap_hwmod_read(struct omap_hwmod * oh,u16 reg_offs)3103 u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
3104 {
3105 	if (oh->flags & HWMOD_16BIT_REG)
3106 		return readw_relaxed(oh->_mpu_rt_va + reg_offs);
3107 	else
3108 		return readl_relaxed(oh->_mpu_rt_va + reg_offs);
3109 }
3110 
omap_hwmod_write(u32 v,struct omap_hwmod * oh,u16 reg_offs)3111 void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
3112 {
3113 	if (oh->flags & HWMOD_16BIT_REG)
3114 		writew_relaxed(v, oh->_mpu_rt_va + reg_offs);
3115 	else
3116 		writel_relaxed(v, oh->_mpu_rt_va + reg_offs);
3117 }
3118 
3119 /**
3120  * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
3121  * @oh: struct omap_hwmod *
3122  *
3123  * This is a public function exposed to drivers. Some drivers may need to do
3124  * some settings before and after resetting the device.  Those drivers after
3125  * doing the necessary settings could use this function to start a reset by
3126  * setting the SYSCONFIG.SOFTRESET bit.
3127  */
omap_hwmod_softreset(struct omap_hwmod * oh)3128 int omap_hwmod_softreset(struct omap_hwmod *oh)
3129 {
3130 	u32 v;
3131 	int ret;
3132 
3133 	if (!oh || !(oh->_sysc_cache))
3134 		return -EINVAL;
3135 
3136 	v = oh->_sysc_cache;
3137 	ret = _set_softreset(oh, &v);
3138 	if (ret)
3139 		goto error;
3140 	_write_sysconfig(v, oh);
3141 
3142 	ret = _clear_softreset(oh, &v);
3143 	if (ret)
3144 		goto error;
3145 	_write_sysconfig(v, oh);
3146 
3147 error:
3148 	return ret;
3149 }
3150 
3151 /**
3152  * omap_hwmod_lookup - look up a registered omap_hwmod by name
3153  * @name: name of the omap_hwmod to look up
3154  *
3155  * Given a @name of an omap_hwmod, return a pointer to the registered
3156  * struct omap_hwmod *, or NULL upon error.
3157  */
omap_hwmod_lookup(const char * name)3158 struct omap_hwmod *omap_hwmod_lookup(const char *name)
3159 {
3160 	struct omap_hwmod *oh;
3161 
3162 	if (!name)
3163 		return NULL;
3164 
3165 	oh = _lookup(name);
3166 
3167 	return oh;
3168 }
3169 
3170 /**
3171  * omap_hwmod_for_each - call function for each registered omap_hwmod
3172  * @fn: pointer to a callback function
3173  * @data: void * data to pass to callback function
3174  *
3175  * Call @fn for each registered omap_hwmod, passing @data to each
3176  * function.  @fn must return 0 for success or any other value for
3177  * failure.  If @fn returns non-zero, the iteration across omap_hwmods
3178  * will stop and the non-zero return value will be passed to the
3179  * caller of omap_hwmod_for_each().  @fn is called with
3180  * omap_hwmod_for_each() held.
3181  */
omap_hwmod_for_each(int (* fn)(struct omap_hwmod * oh,void * data),void * data)3182 int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
3183 			void *data)
3184 {
3185 	struct omap_hwmod *temp_oh;
3186 	int ret = 0;
3187 
3188 	if (!fn)
3189 		return -EINVAL;
3190 
3191 	list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3192 		ret = (*fn)(temp_oh, data);
3193 		if (ret)
3194 			break;
3195 	}
3196 
3197 	return ret;
3198 }
3199 
3200 /**
3201  * omap_hwmod_register_links - register an array of hwmod links
3202  * @ois: pointer to an array of omap_hwmod_ocp_if to register
3203  *
3204  * Intended to be called early in boot before the clock framework is
3205  * initialized.  If @ois is not null, will register all omap_hwmods
3206  * listed in @ois that are valid for this chip.  Returns -EINVAL if
3207  * omap_hwmod_init() hasn't been called before calling this function,
3208  * -ENOMEM if the link memory area can't be allocated, or 0 upon
3209  * success.
3210  */
omap_hwmod_register_links(struct omap_hwmod_ocp_if ** ois)3211 int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
3212 {
3213 	int r, i;
3214 
3215 	if (!inited)
3216 		return -EINVAL;
3217 
3218 	if (!ois)
3219 		return 0;
3220 
3221 	if (ois[0] == NULL) /* Empty list */
3222 		return 0;
3223 
3224 	if (!linkspace) {
3225 		if (_alloc_linkspace(ois)) {
3226 			pr_err("omap_hwmod: could not allocate link space\n");
3227 			return -ENOMEM;
3228 		}
3229 	}
3230 
3231 	i = 0;
3232 	do {
3233 		r = _register_link(ois[i]);
3234 		WARN(r && r != -EEXIST,
3235 		     "omap_hwmod: _register_link(%s -> %s) returned %d\n",
3236 		     ois[i]->master->name, ois[i]->slave->name, r);
3237 	} while (ois[++i]);
3238 
3239 	return 0;
3240 }
3241 
3242 /**
3243  * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
3244  * @oh: pointer to the hwmod currently being set up (usually not the MPU)
3245  *
3246  * If the hwmod data corresponding to the MPU subsystem IP block
3247  * hasn't been initialized and set up yet, do so now.  This must be
3248  * done first since sleep dependencies may be added from other hwmods
3249  * to the MPU.  Intended to be called only by omap_hwmod_setup*().  No
3250  * return value.
3251  */
_ensure_mpu_hwmod_is_setup(struct omap_hwmod * oh)3252 static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
3253 {
3254 	if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
3255 		pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
3256 		       __func__, MPU_INITIATOR_NAME);
3257 	else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
3258 		omap_hwmod_setup_one(MPU_INITIATOR_NAME);
3259 }
3260 
3261 /**
3262  * omap_hwmod_setup_one - set up a single hwmod
3263  * @oh_name: const char * name of the already-registered hwmod to set up
3264  *
3265  * Initialize and set up a single hwmod.  Intended to be used for a
3266  * small number of early devices, such as the timer IP blocks used for
3267  * the scheduler clock.  Must be called after omap2_clk_init().
3268  * Resolves the struct clk names to struct clk pointers for each
3269  * registered omap_hwmod.  Also calls _setup() on each hwmod.  Returns
3270  * -EINVAL upon error or 0 upon success.
3271  */
omap_hwmod_setup_one(const char * oh_name)3272 int __init omap_hwmod_setup_one(const char *oh_name)
3273 {
3274 	struct omap_hwmod *oh;
3275 
3276 	pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
3277 
3278 	oh = _lookup(oh_name);
3279 	if (!oh) {
3280 		WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
3281 		return -EINVAL;
3282 	}
3283 
3284 	_ensure_mpu_hwmod_is_setup(oh);
3285 
3286 	_init(oh, NULL);
3287 	_setup(oh, NULL);
3288 
3289 	return 0;
3290 }
3291 
3292 /**
3293  * omap_hwmod_setup_all - set up all registered IP blocks
3294  *
3295  * Initialize and set up all IP blocks registered with the hwmod code.
3296  * Must be called after omap2_clk_init().  Resolves the struct clk
3297  * names to struct clk pointers for each registered omap_hwmod.  Also
3298  * calls _setup() on each hwmod.  Returns 0 upon success.
3299  */
omap_hwmod_setup_all(void)3300 static int __init omap_hwmod_setup_all(void)
3301 {
3302 	_ensure_mpu_hwmod_is_setup(NULL);
3303 
3304 	omap_hwmod_for_each(_init, NULL);
3305 	omap_hwmod_for_each(_setup, NULL);
3306 
3307 	return 0;
3308 }
3309 omap_core_initcall(omap_hwmod_setup_all);
3310 
3311 /**
3312  * omap_hwmod_enable - enable an omap_hwmod
3313  * @oh: struct omap_hwmod *
3314  *
3315  * Enable an omap_hwmod @oh.  Intended to be called by omap_device_enable().
3316  * Returns -EINVAL on error or passes along the return value from _enable().
3317  */
omap_hwmod_enable(struct omap_hwmod * oh)3318 int omap_hwmod_enable(struct omap_hwmod *oh)
3319 {
3320 	int r;
3321 	unsigned long flags;
3322 
3323 	if (!oh)
3324 		return -EINVAL;
3325 
3326 	spin_lock_irqsave(&oh->_lock, flags);
3327 	r = _enable(oh);
3328 	spin_unlock_irqrestore(&oh->_lock, flags);
3329 
3330 	return r;
3331 }
3332 
3333 /**
3334  * omap_hwmod_idle - idle an omap_hwmod
3335  * @oh: struct omap_hwmod *
3336  *
3337  * Idle an omap_hwmod @oh.  Intended to be called by omap_device_idle().
3338  * Returns -EINVAL on error or passes along the return value from _idle().
3339  */
omap_hwmod_idle(struct omap_hwmod * oh)3340 int omap_hwmod_idle(struct omap_hwmod *oh)
3341 {
3342 	unsigned long flags;
3343 
3344 	if (!oh)
3345 		return -EINVAL;
3346 
3347 	spin_lock_irqsave(&oh->_lock, flags);
3348 	_idle(oh);
3349 	spin_unlock_irqrestore(&oh->_lock, flags);
3350 
3351 	return 0;
3352 }
3353 
3354 /**
3355  * omap_hwmod_shutdown - shutdown an omap_hwmod
3356  * @oh: struct omap_hwmod *
3357  *
3358  * Shutdown an omap_hwmod @oh.  Intended to be called by
3359  * omap_device_shutdown().  Returns -EINVAL on error or passes along
3360  * the return value from _shutdown().
3361  */
omap_hwmod_shutdown(struct omap_hwmod * oh)3362 int omap_hwmod_shutdown(struct omap_hwmod *oh)
3363 {
3364 	unsigned long flags;
3365 
3366 	if (!oh)
3367 		return -EINVAL;
3368 
3369 	spin_lock_irqsave(&oh->_lock, flags);
3370 	_shutdown(oh);
3371 	spin_unlock_irqrestore(&oh->_lock, flags);
3372 
3373 	return 0;
3374 }
3375 
3376 /*
3377  * IP block data retrieval functions
3378  */
3379 
3380 /**
3381  * omap_hwmod_count_resources - count number of struct resources needed by hwmod
3382  * @oh: struct omap_hwmod *
3383  * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
3384  *
3385  * Count the number of struct resource array elements necessary to
3386  * contain omap_hwmod @oh resources.  Intended to be called by code
3387  * that registers omap_devices.  Intended to be used to determine the
3388  * size of a dynamically-allocated struct resource array, before
3389  * calling omap_hwmod_fill_resources().  Returns the number of struct
3390  * resource array elements needed.
3391  *
3392  * XXX This code is not optimized.  It could attempt to merge adjacent
3393  * resource IDs.
3394  *
3395  */
omap_hwmod_count_resources(struct omap_hwmod * oh,unsigned long flags)3396 int omap_hwmod_count_resources(struct omap_hwmod *oh, unsigned long flags)
3397 {
3398 	int ret = 0;
3399 
3400 	if (flags & IORESOURCE_IRQ)
3401 		ret += _count_mpu_irqs(oh);
3402 
3403 	if (flags & IORESOURCE_DMA)
3404 		ret += _count_sdma_reqs(oh);
3405 
3406 	if (flags & IORESOURCE_MEM) {
3407 		int i = 0;
3408 		struct omap_hwmod_ocp_if *os;
3409 		struct list_head *p = oh->slave_ports.next;
3410 
3411 		while (i < oh->slaves_cnt) {
3412 			os = _fetch_next_ocp_if(&p, &i);
3413 			ret += _count_ocp_if_addr_spaces(os);
3414 		}
3415 	}
3416 
3417 	return ret;
3418 }
3419 
3420 /**
3421  * omap_hwmod_fill_resources - fill struct resource array with hwmod data
3422  * @oh: struct omap_hwmod *
3423  * @res: pointer to the first element of an array of struct resource to fill
3424  *
3425  * Fill the struct resource array @res with resource data from the
3426  * omap_hwmod @oh.  Intended to be called by code that registers
3427  * omap_devices.  See also omap_hwmod_count_resources().  Returns the
3428  * number of array elements filled.
3429  */
omap_hwmod_fill_resources(struct omap_hwmod * oh,struct resource * res)3430 int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
3431 {
3432 	struct omap_hwmod_ocp_if *os;
3433 	struct list_head *p;
3434 	int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
3435 	int r = 0;
3436 
3437 	/* For each IRQ, DMA, memory area, fill in array.*/
3438 
3439 	mpu_irqs_cnt = _count_mpu_irqs(oh);
3440 	for (i = 0; i < mpu_irqs_cnt; i++) {
3441 		unsigned int irq;
3442 
3443 		if (oh->xlate_irq)
3444 			irq = oh->xlate_irq((oh->mpu_irqs + i)->irq);
3445 		else
3446 			irq = (oh->mpu_irqs + i)->irq;
3447 		(res + r)->name = (oh->mpu_irqs + i)->name;
3448 		(res + r)->start = irq;
3449 		(res + r)->end = irq;
3450 		(res + r)->flags = IORESOURCE_IRQ;
3451 		r++;
3452 	}
3453 
3454 	sdma_reqs_cnt = _count_sdma_reqs(oh);
3455 	for (i = 0; i < sdma_reqs_cnt; i++) {
3456 		(res + r)->name = (oh->sdma_reqs + i)->name;
3457 		(res + r)->start = (oh->sdma_reqs + i)->dma_req;
3458 		(res + r)->end = (oh->sdma_reqs + i)->dma_req;
3459 		(res + r)->flags = IORESOURCE_DMA;
3460 		r++;
3461 	}
3462 
3463 	p = oh->slave_ports.next;
3464 
3465 	i = 0;
3466 	while (i < oh->slaves_cnt) {
3467 		os = _fetch_next_ocp_if(&p, &i);
3468 		addr_cnt = _count_ocp_if_addr_spaces(os);
3469 
3470 		for (j = 0; j < addr_cnt; j++) {
3471 			(res + r)->name = (os->addr + j)->name;
3472 			(res + r)->start = (os->addr + j)->pa_start;
3473 			(res + r)->end = (os->addr + j)->pa_end;
3474 			(res + r)->flags = IORESOURCE_MEM;
3475 			r++;
3476 		}
3477 	}
3478 
3479 	return r;
3480 }
3481 
3482 /**
3483  * omap_hwmod_fill_dma_resources - fill struct resource array with dma data
3484  * @oh: struct omap_hwmod *
3485  * @res: pointer to the array of struct resource to fill
3486  *
3487  * Fill the struct resource array @res with dma resource data from the
3488  * omap_hwmod @oh.  Intended to be called by code that registers
3489  * omap_devices.  See also omap_hwmod_count_resources().  Returns the
3490  * number of array elements filled.
3491  */
omap_hwmod_fill_dma_resources(struct omap_hwmod * oh,struct resource * res)3492 int omap_hwmod_fill_dma_resources(struct omap_hwmod *oh, struct resource *res)
3493 {
3494 	int i, sdma_reqs_cnt;
3495 	int r = 0;
3496 
3497 	sdma_reqs_cnt = _count_sdma_reqs(oh);
3498 	for (i = 0; i < sdma_reqs_cnt; i++) {
3499 		(res + r)->name = (oh->sdma_reqs + i)->name;
3500 		(res + r)->start = (oh->sdma_reqs + i)->dma_req;
3501 		(res + r)->end = (oh->sdma_reqs + i)->dma_req;
3502 		(res + r)->flags = IORESOURCE_DMA;
3503 		r++;
3504 	}
3505 
3506 	return r;
3507 }
3508 
3509 /**
3510  * omap_hwmod_get_resource_byname - fetch IP block integration data by name
3511  * @oh: struct omap_hwmod * to operate on
3512  * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
3513  * @name: pointer to the name of the data to fetch (optional)
3514  * @rsrc: pointer to a struct resource, allocated by the caller
3515  *
3516  * Retrieve MPU IRQ, SDMA request line, or address space start/end
3517  * data for the IP block pointed to by @oh.  The data will be filled
3518  * into a struct resource record pointed to by @rsrc.  The struct
3519  * resource must be allocated by the caller.  When @name is non-null,
3520  * the data associated with the matching entry in the IRQ/SDMA/address
3521  * space hwmod data arrays will be returned.  If @name is null, the
3522  * first array entry will be returned.  Data order is not meaningful
3523  * in hwmod data, so callers are strongly encouraged to use a non-null
3524  * @name whenever possible to avoid unpredictable effects if hwmod
3525  * data is later added that causes data ordering to change.  This
3526  * function is only intended for use by OMAP core code.  Device
3527  * drivers should not call this function - the appropriate bus-related
3528  * data accessor functions should be used instead.  Returns 0 upon
3529  * success or a negative error code upon error.
3530  */
omap_hwmod_get_resource_byname(struct omap_hwmod * oh,unsigned int type,const char * name,struct resource * rsrc)3531 int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
3532 				   const char *name, struct resource *rsrc)
3533 {
3534 	int r;
3535 	unsigned int irq, dma;
3536 	u32 pa_start, pa_end;
3537 
3538 	if (!oh || !rsrc)
3539 		return -EINVAL;
3540 
3541 	if (type == IORESOURCE_IRQ) {
3542 		r = _get_mpu_irq_by_name(oh, name, &irq);
3543 		if (r)
3544 			return r;
3545 
3546 		rsrc->start = irq;
3547 		rsrc->end = irq;
3548 	} else if (type == IORESOURCE_DMA) {
3549 		r = _get_sdma_req_by_name(oh, name, &dma);
3550 		if (r)
3551 			return r;
3552 
3553 		rsrc->start = dma;
3554 		rsrc->end = dma;
3555 	} else if (type == IORESOURCE_MEM) {
3556 		r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
3557 		if (r)
3558 			return r;
3559 
3560 		rsrc->start = pa_start;
3561 		rsrc->end = pa_end;
3562 	} else {
3563 		return -EINVAL;
3564 	}
3565 
3566 	rsrc->flags = type;
3567 	rsrc->name = name;
3568 
3569 	return 0;
3570 }
3571 
3572 /**
3573  * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
3574  * @oh: struct omap_hwmod *
3575  *
3576  * Return the powerdomain pointer associated with the OMAP module
3577  * @oh's main clock.  If @oh does not have a main clk, return the
3578  * powerdomain associated with the interface clock associated with the
3579  * module's MPU port. (XXX Perhaps this should use the SDMA port
3580  * instead?)  Returns NULL on error, or a struct powerdomain * on
3581  * success.
3582  */
omap_hwmod_get_pwrdm(struct omap_hwmod * oh)3583 struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
3584 {
3585 	struct clk *c;
3586 	struct omap_hwmod_ocp_if *oi;
3587 	struct clockdomain *clkdm;
3588 	struct clk_hw_omap *clk;
3589 
3590 	if (!oh)
3591 		return NULL;
3592 
3593 	if (oh->clkdm)
3594 		return oh->clkdm->pwrdm.ptr;
3595 
3596 	if (oh->_clk) {
3597 		c = oh->_clk;
3598 	} else {
3599 		oi = _find_mpu_rt_port(oh);
3600 		if (!oi)
3601 			return NULL;
3602 		c = oi->_clk;
3603 	}
3604 
3605 	clk = to_clk_hw_omap(__clk_get_hw(c));
3606 	clkdm = clk->clkdm;
3607 	if (!clkdm)
3608 		return NULL;
3609 
3610 	return clkdm->pwrdm.ptr;
3611 }
3612 
3613 /**
3614  * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
3615  * @oh: struct omap_hwmod *
3616  *
3617  * Returns the virtual address corresponding to the beginning of the
3618  * module's register target, in the address range that is intended to
3619  * be used by the MPU.  Returns the virtual address upon success or NULL
3620  * upon error.
3621  */
omap_hwmod_get_mpu_rt_va(struct omap_hwmod * oh)3622 void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
3623 {
3624 	if (!oh)
3625 		return NULL;
3626 
3627 	if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
3628 		return NULL;
3629 
3630 	if (oh->_state == _HWMOD_STATE_UNKNOWN)
3631 		return NULL;
3632 
3633 	return oh->_mpu_rt_va;
3634 }
3635 
3636 /*
3637  * XXX what about functions for drivers to save/restore ocp_sysconfig
3638  * for context save/restore operations?
3639  */
3640 
3641 /**
3642  * omap_hwmod_enable_wakeup - allow device to wake up the system
3643  * @oh: struct omap_hwmod *
3644  *
3645  * Sets the module OCP socket ENAWAKEUP bit to allow the module to
3646  * send wakeups to the PRCM, and enable I/O ring wakeup events for
3647  * this IP block if it has dynamic mux entries.  Eventually this
3648  * should set PRCM wakeup registers to cause the PRCM to receive
3649  * wakeup events from the module.  Does not set any wakeup routing
3650  * registers beyond this point - if the module is to wake up any other
3651  * module or subsystem, that must be set separately.  Called by
3652  * omap_device code.  Returns -EINVAL on error or 0 upon success.
3653  */
omap_hwmod_enable_wakeup(struct omap_hwmod * oh)3654 int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
3655 {
3656 	unsigned long flags;
3657 	u32 v;
3658 
3659 	spin_lock_irqsave(&oh->_lock, flags);
3660 
3661 	if (oh->class->sysc &&
3662 	    (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3663 		v = oh->_sysc_cache;
3664 		_enable_wakeup(oh, &v);
3665 		_write_sysconfig(v, oh);
3666 	}
3667 
3668 	_set_idle_ioring_wakeup(oh, true);
3669 	spin_unlock_irqrestore(&oh->_lock, flags);
3670 
3671 	return 0;
3672 }
3673 
3674 /**
3675  * omap_hwmod_disable_wakeup - prevent device from waking the system
3676  * @oh: struct omap_hwmod *
3677  *
3678  * Clears the module OCP socket ENAWAKEUP bit to prevent the module
3679  * from sending wakeups to the PRCM, and disable I/O ring wakeup
3680  * events for this IP block if it has dynamic mux entries.  Eventually
3681  * this should clear PRCM wakeup registers to cause the PRCM to ignore
3682  * wakeup events from the module.  Does not set any wakeup routing
3683  * registers beyond this point - if the module is to wake up any other
3684  * module or subsystem, that must be set separately.  Called by
3685  * omap_device code.  Returns -EINVAL on error or 0 upon success.
3686  */
omap_hwmod_disable_wakeup(struct omap_hwmod * oh)3687 int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
3688 {
3689 	unsigned long flags;
3690 	u32 v;
3691 
3692 	spin_lock_irqsave(&oh->_lock, flags);
3693 
3694 	if (oh->class->sysc &&
3695 	    (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
3696 		v = oh->_sysc_cache;
3697 		_disable_wakeup(oh, &v);
3698 		_write_sysconfig(v, oh);
3699 	}
3700 
3701 	_set_idle_ioring_wakeup(oh, false);
3702 	spin_unlock_irqrestore(&oh->_lock, flags);
3703 
3704 	return 0;
3705 }
3706 
3707 /**
3708  * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
3709  * contained in the hwmod module.
3710  * @oh: struct omap_hwmod *
3711  * @name: name of the reset line to lookup and assert
3712  *
3713  * Some IP like dsp, ipu or iva contain processor that require
3714  * an HW reset line to be assert / deassert in order to enable fully
3715  * the IP.  Returns -EINVAL if @oh is null or if the operation is not
3716  * yet supported on this OMAP; otherwise, passes along the return value
3717  * from _assert_hardreset().
3718  */
omap_hwmod_assert_hardreset(struct omap_hwmod * oh,const char * name)3719 int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
3720 {
3721 	int ret;
3722 	unsigned long flags;
3723 
3724 	if (!oh)
3725 		return -EINVAL;
3726 
3727 	spin_lock_irqsave(&oh->_lock, flags);
3728 	ret = _assert_hardreset(oh, name);
3729 	spin_unlock_irqrestore(&oh->_lock, flags);
3730 
3731 	return ret;
3732 }
3733 
3734 /**
3735  * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
3736  * contained in the hwmod module.
3737  * @oh: struct omap_hwmod *
3738  * @name: name of the reset line to look up and deassert
3739  *
3740  * Some IP like dsp, ipu or iva contain processor that require
3741  * an HW reset line to be assert / deassert in order to enable fully
3742  * the IP.  Returns -EINVAL if @oh is null or if the operation is not
3743  * yet supported on this OMAP; otherwise, passes along the return value
3744  * from _deassert_hardreset().
3745  */
omap_hwmod_deassert_hardreset(struct omap_hwmod * oh,const char * name)3746 int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
3747 {
3748 	int ret;
3749 	unsigned long flags;
3750 
3751 	if (!oh)
3752 		return -EINVAL;
3753 
3754 	spin_lock_irqsave(&oh->_lock, flags);
3755 	ret = _deassert_hardreset(oh, name);
3756 	spin_unlock_irqrestore(&oh->_lock, flags);
3757 
3758 	return ret;
3759 }
3760 
3761 /**
3762  * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
3763  * @classname: struct omap_hwmod_class name to search for
3764  * @fn: callback function pointer to call for each hwmod in class @classname
3765  * @user: arbitrary context data to pass to the callback function
3766  *
3767  * For each omap_hwmod of class @classname, call @fn.
3768  * If the callback function returns something other than
3769  * zero, the iterator is terminated, and the callback function's return
3770  * value is passed back to the caller.  Returns 0 upon success, -EINVAL
3771  * if @classname or @fn are NULL, or passes back the error code from @fn.
3772  */
omap_hwmod_for_each_by_class(const char * classname,int (* fn)(struct omap_hwmod * oh,void * user),void * user)3773 int omap_hwmod_for_each_by_class(const char *classname,
3774 				 int (*fn)(struct omap_hwmod *oh,
3775 					   void *user),
3776 				 void *user)
3777 {
3778 	struct omap_hwmod *temp_oh;
3779 	int ret = 0;
3780 
3781 	if (!classname || !fn)
3782 		return -EINVAL;
3783 
3784 	pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
3785 		 __func__, classname);
3786 
3787 	list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
3788 		if (!strcmp(temp_oh->class->name, classname)) {
3789 			pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
3790 				 __func__, temp_oh->name);
3791 			ret = (*fn)(temp_oh, user);
3792 			if (ret)
3793 				break;
3794 		}
3795 	}
3796 
3797 	if (ret)
3798 		pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
3799 			 __func__, ret);
3800 
3801 	return ret;
3802 }
3803 
3804 /**
3805  * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
3806  * @oh: struct omap_hwmod *
3807  * @state: state that _setup() should leave the hwmod in
3808  *
3809  * Sets the hwmod state that @oh will enter at the end of _setup()
3810  * (called by omap_hwmod_setup_*()).  See also the documentation
3811  * for _setup_postsetup(), above.  Returns 0 upon success or
3812  * -EINVAL if there is a problem with the arguments or if the hwmod is
3813  * in the wrong state.
3814  */
omap_hwmod_set_postsetup_state(struct omap_hwmod * oh,u8 state)3815 int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
3816 {
3817 	int ret;
3818 	unsigned long flags;
3819 
3820 	if (!oh)
3821 		return -EINVAL;
3822 
3823 	if (state != _HWMOD_STATE_DISABLED &&
3824 	    state != _HWMOD_STATE_ENABLED &&
3825 	    state != _HWMOD_STATE_IDLE)
3826 		return -EINVAL;
3827 
3828 	spin_lock_irqsave(&oh->_lock, flags);
3829 
3830 	if (oh->_state != _HWMOD_STATE_REGISTERED) {
3831 		ret = -EINVAL;
3832 		goto ohsps_unlock;
3833 	}
3834 
3835 	oh->_postsetup_state = state;
3836 	ret = 0;
3837 
3838 ohsps_unlock:
3839 	spin_unlock_irqrestore(&oh->_lock, flags);
3840 
3841 	return ret;
3842 }
3843 
3844 /**
3845  * omap_hwmod_get_context_loss_count - get lost context count
3846  * @oh: struct omap_hwmod *
3847  *
3848  * Returns the context loss count of associated @oh
3849  * upon success, or zero if no context loss data is available.
3850  *
3851  * On OMAP4, this queries the per-hwmod context loss register,
3852  * assuming one exists.  If not, or on OMAP2/3, this queries the
3853  * enclosing powerdomain context loss count.
3854  */
omap_hwmod_get_context_loss_count(struct omap_hwmod * oh)3855 int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
3856 {
3857 	struct powerdomain *pwrdm;
3858 	int ret = 0;
3859 
3860 	if (soc_ops.get_context_lost)
3861 		return soc_ops.get_context_lost(oh);
3862 
3863 	pwrdm = omap_hwmod_get_pwrdm(oh);
3864 	if (pwrdm)
3865 		ret = pwrdm_get_context_loss_count(pwrdm);
3866 
3867 	return ret;
3868 }
3869 
3870 /**
3871  * omap_hwmod_init - initialize the hwmod code
3872  *
3873  * Sets up some function pointers needed by the hwmod code to operate on the
3874  * currently-booted SoC.  Intended to be called once during kernel init
3875  * before any hwmods are registered.  No return value.
3876  */
omap_hwmod_init(void)3877 void __init omap_hwmod_init(void)
3878 {
3879 	if (cpu_is_omap24xx()) {
3880 		soc_ops.wait_target_ready = _omap2xxx_3xxx_wait_target_ready;
3881 		soc_ops.assert_hardreset = _omap2_assert_hardreset;
3882 		soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
3883 		soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
3884 	} else if (cpu_is_omap34xx()) {
3885 		soc_ops.wait_target_ready = _omap2xxx_3xxx_wait_target_ready;
3886 		soc_ops.assert_hardreset = _omap2_assert_hardreset;
3887 		soc_ops.deassert_hardreset = _omap2_deassert_hardreset;
3888 		soc_ops.is_hardreset_asserted = _omap2_is_hardreset_asserted;
3889 		soc_ops.init_clkdm = _init_clkdm;
3890 	} else if (cpu_is_omap44xx() || soc_is_omap54xx() || soc_is_dra7xx()) {
3891 		soc_ops.enable_module = _omap4_enable_module;
3892 		soc_ops.disable_module = _omap4_disable_module;
3893 		soc_ops.wait_target_ready = _omap4_wait_target_ready;
3894 		soc_ops.assert_hardreset = _omap4_assert_hardreset;
3895 		soc_ops.deassert_hardreset = _omap4_deassert_hardreset;
3896 		soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
3897 		soc_ops.init_clkdm = _init_clkdm;
3898 		soc_ops.update_context_lost = _omap4_update_context_lost;
3899 		soc_ops.get_context_lost = _omap4_get_context_lost;
3900 	} else if (cpu_is_ti816x() || soc_is_am33xx() || soc_is_am43xx()) {
3901 		soc_ops.enable_module = _omap4_enable_module;
3902 		soc_ops.disable_module = _omap4_disable_module;
3903 		soc_ops.wait_target_ready = _omap4_wait_target_ready;
3904 		soc_ops.assert_hardreset = _omap4_assert_hardreset;
3905 		soc_ops.deassert_hardreset = _am33xx_deassert_hardreset;
3906 		soc_ops.is_hardreset_asserted = _omap4_is_hardreset_asserted;
3907 		soc_ops.init_clkdm = _init_clkdm;
3908 	} else {
3909 		WARN(1, "omap_hwmod: unknown SoC type\n");
3910 	}
3911 
3912 	inited = true;
3913 }
3914 
3915 /**
3916  * omap_hwmod_get_main_clk - get pointer to main clock name
3917  * @oh: struct omap_hwmod *
3918  *
3919  * Returns the main clock name assocated with @oh upon success,
3920  * or NULL if @oh is NULL.
3921  */
omap_hwmod_get_main_clk(struct omap_hwmod * oh)3922 const char *omap_hwmod_get_main_clk(struct omap_hwmod *oh)
3923 {
3924 	if (!oh)
3925 		return NULL;
3926 
3927 	return oh->main_clk;
3928 }
3929