omap_hwmod.c 42 KB

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