omap_hwmod.c 39 KB

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