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