omap_hwmod.c 44 KB

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