omap_hwmod.c 40 KB

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