omap_hwmod.c 39 KB

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