omap_hwmod.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  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. /* clocks and deps are already disabled in idle */
  811. if (oh->_state == _HWMOD_STATE_ENABLED) {
  812. _del_initiator_dep(oh, mpu_oh);
  813. /* XXX what about the other system initiators here? dma, dsp */
  814. _disable_clocks(oh);
  815. }
  816. /* XXX Should this code also force-disable the optional clocks? */
  817. /* XXX mux any associated balls to safe mode */
  818. oh->_state = _HWMOD_STATE_DISABLED;
  819. return 0;
  820. }
  821. /**
  822. * _setup - do initial configuration of omap_hwmod
  823. * @oh: struct omap_hwmod *
  824. * @skip_setup_idle_p: do not idle hwmods at the end of the fn if 1
  825. *
  826. * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
  827. * OCP_SYSCONFIG register. Must be called with omap_hwmod_mutex held.
  828. * @skip_setup_idle is intended to be used on a system that will not
  829. * call omap_hwmod_enable() to enable devices (e.g., a system without
  830. * PM runtime). Returns -EINVAL if the hwmod is in the wrong state or
  831. * returns 0.
  832. */
  833. static int _setup(struct omap_hwmod *oh, void *data)
  834. {
  835. int i, r;
  836. u8 skip_setup_idle;
  837. if (!oh || !data)
  838. return -EINVAL;
  839. skip_setup_idle = *(u8 *)data;
  840. /* Set iclk autoidle mode */
  841. if (oh->slaves_cnt > 0) {
  842. for (i = 0; i < oh->slaves_cnt; i++) {
  843. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  844. struct clk *c = os->_clk;
  845. if (!c)
  846. continue;
  847. if (os->flags & OCPIF_SWSUP_IDLE) {
  848. /* XXX omap_iclk_deny_idle(c); */
  849. } else {
  850. /* XXX omap_iclk_allow_idle(c); */
  851. clk_enable(c);
  852. }
  853. }
  854. }
  855. oh->_state = _HWMOD_STATE_INITIALIZED;
  856. r = _omap_hwmod_enable(oh);
  857. if (r) {
  858. pr_warning("omap_hwmod: %s: cannot be enabled (%d)\n",
  859. oh->name, oh->_state);
  860. return 0;
  861. }
  862. if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
  863. /*
  864. * XXX Do the OCP_SYSCONFIG bits need to be
  865. * reprogrammed after a reset? If not, then this can
  866. * be removed. If they do, then probably the
  867. * _omap_hwmod_enable() function should be split to avoid the
  868. * rewrite of the OCP_SYSCONFIG register.
  869. */
  870. if (oh->class->sysc) {
  871. _update_sysc_cache(oh);
  872. _sysc_enable(oh);
  873. }
  874. }
  875. if (!(oh->flags & HWMOD_INIT_NO_IDLE) && !skip_setup_idle)
  876. _omap_hwmod_idle(oh);
  877. return 0;
  878. }
  879. /* Public functions */
  880. u32 omap_hwmod_readl(struct omap_hwmod *oh, u16 reg_offs)
  881. {
  882. return __raw_readl(oh->_mpu_rt_va + reg_offs);
  883. }
  884. void omap_hwmod_writel(u32 v, struct omap_hwmod *oh, u16 reg_offs)
  885. {
  886. __raw_writel(v, oh->_mpu_rt_va + reg_offs);
  887. }
  888. /**
  889. * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
  890. * @oh: struct omap_hwmod *
  891. * @idlemode: SIDLEMODE field bits (shifted to bit 0)
  892. *
  893. * Sets the IP block's OCP slave idlemode in hardware, and updates our
  894. * local copy. Intended to be used by drivers that have some erratum
  895. * that requires direct manipulation of the SIDLEMODE bits. Returns
  896. * -EINVAL if @oh is null, or passes along the return value from
  897. * _set_slave_idlemode().
  898. *
  899. * XXX Does this function have any current users? If not, we should
  900. * remove it; it is better to let the rest of the hwmod code handle this.
  901. * Any users of this function should be scrutinized carefully.
  902. */
  903. int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
  904. {
  905. u32 v;
  906. int retval = 0;
  907. if (!oh)
  908. return -EINVAL;
  909. v = oh->_sysc_cache;
  910. retval = _set_slave_idlemode(oh, idlemode, &v);
  911. if (!retval)
  912. _write_sysconfig(v, oh);
  913. return retval;
  914. }
  915. /**
  916. * omap_hwmod_register - register a struct omap_hwmod
  917. * @oh: struct omap_hwmod *
  918. *
  919. * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
  920. * already has been registered by the same name; -EINVAL if the
  921. * omap_hwmod is in the wrong state, if @oh is NULL, if the
  922. * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
  923. * name, or if the omap_hwmod's class is missing a name; or 0 upon
  924. * success.
  925. *
  926. * XXX The data should be copied into bootmem, so the original data
  927. * should be marked __initdata and freed after init. This would allow
  928. * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
  929. * that the copy process would be relatively complex due to the large number
  930. * of substructures.
  931. */
  932. int omap_hwmod_register(struct omap_hwmod *oh)
  933. {
  934. int ret, ms_id;
  935. if (!oh || !oh->name || !oh->class || !oh->class->name ||
  936. (oh->_state != _HWMOD_STATE_UNKNOWN))
  937. return -EINVAL;
  938. mutex_lock(&omap_hwmod_mutex);
  939. pr_debug("omap_hwmod: %s: registering\n", oh->name);
  940. if (_lookup(oh->name)) {
  941. ret = -EEXIST;
  942. goto ohr_unlock;
  943. }
  944. ms_id = _find_mpu_port_index(oh);
  945. if (!IS_ERR_VALUE(ms_id)) {
  946. oh->_mpu_port_index = ms_id;
  947. oh->_mpu_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
  948. } else {
  949. oh->_int_flags |= _HWMOD_NO_MPU_PORT;
  950. }
  951. list_add_tail(&oh->node, &omap_hwmod_list);
  952. oh->_state = _HWMOD_STATE_REGISTERED;
  953. ret = 0;
  954. ohr_unlock:
  955. mutex_unlock(&omap_hwmod_mutex);
  956. return ret;
  957. }
  958. /**
  959. * omap_hwmod_lookup - look up a registered omap_hwmod by name
  960. * @name: name of the omap_hwmod to look up
  961. *
  962. * Given a @name of an omap_hwmod, return a pointer to the registered
  963. * struct omap_hwmod *, or NULL upon error.
  964. */
  965. struct omap_hwmod *omap_hwmod_lookup(const char *name)
  966. {
  967. struct omap_hwmod *oh;
  968. if (!name)
  969. return NULL;
  970. mutex_lock(&omap_hwmod_mutex);
  971. oh = _lookup(name);
  972. mutex_unlock(&omap_hwmod_mutex);
  973. return oh;
  974. }
  975. /**
  976. * omap_hwmod_for_each - call function for each registered omap_hwmod
  977. * @fn: pointer to a callback function
  978. * @data: void * data to pass to callback function
  979. *
  980. * Call @fn for each registered omap_hwmod, passing @data to each
  981. * function. @fn must return 0 for success or any other value for
  982. * failure. If @fn returns non-zero, the iteration across omap_hwmods
  983. * will stop and the non-zero return value will be passed to the
  984. * caller of omap_hwmod_for_each(). @fn is called with
  985. * omap_hwmod_for_each() held.
  986. */
  987. int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
  988. void *data)
  989. {
  990. struct omap_hwmod *temp_oh;
  991. int ret;
  992. if (!fn)
  993. return -EINVAL;
  994. mutex_lock(&omap_hwmod_mutex);
  995. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  996. ret = (*fn)(temp_oh, data);
  997. if (ret)
  998. break;
  999. }
  1000. mutex_unlock(&omap_hwmod_mutex);
  1001. return ret;
  1002. }
  1003. /**
  1004. * omap_hwmod_init - init omap_hwmod code and register hwmods
  1005. * @ohs: pointer to an array of omap_hwmods to register
  1006. *
  1007. * Intended to be called early in boot before the clock framework is
  1008. * initialized. If @ohs is not null, will register all omap_hwmods
  1009. * listed in @ohs that are valid for this chip. Returns -EINVAL if
  1010. * omap_hwmod_init() has already been called or 0 otherwise.
  1011. */
  1012. int omap_hwmod_init(struct omap_hwmod **ohs)
  1013. {
  1014. struct omap_hwmod *oh;
  1015. int r;
  1016. if (inited)
  1017. return -EINVAL;
  1018. inited = 1;
  1019. if (!ohs)
  1020. return 0;
  1021. oh = *ohs;
  1022. while (oh) {
  1023. if (omap_chip_is(oh->omap_chip)) {
  1024. r = omap_hwmod_register(oh);
  1025. WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
  1026. "%d\n", oh->name, r);
  1027. }
  1028. oh = *++ohs;
  1029. }
  1030. return 0;
  1031. }
  1032. /**
  1033. * omap_hwmod_late_init - do some post-clock framework initialization
  1034. * @skip_setup_idle: if 1, do not idle hwmods in _setup()
  1035. *
  1036. * Must be called after omap2_clk_init(). Resolves the struct clk names
  1037. * to struct clk pointers for each registered omap_hwmod. Also calls
  1038. * _setup() on each hwmod. Returns 0.
  1039. */
  1040. int omap_hwmod_late_init(u8 skip_setup_idle)
  1041. {
  1042. int r;
  1043. /* XXX check return value */
  1044. r = omap_hwmod_for_each(_init_clocks, NULL);
  1045. WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
  1046. mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
  1047. WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
  1048. MPU_INITIATOR_NAME);
  1049. if (skip_setup_idle)
  1050. pr_debug("omap_hwmod: will leave hwmods enabled during setup\n");
  1051. omap_hwmod_for_each(_setup, &skip_setup_idle);
  1052. return 0;
  1053. }
  1054. /**
  1055. * omap_hwmod_unregister - unregister an omap_hwmod
  1056. * @oh: struct omap_hwmod *
  1057. *
  1058. * Unregisters a previously-registered omap_hwmod @oh. There's probably
  1059. * no use case for this, so it is likely to be removed in a later version.
  1060. *
  1061. * XXX Free all of the bootmem-allocated structures here when that is
  1062. * implemented. Make it clear that core code is the only code that is
  1063. * expected to unregister modules.
  1064. */
  1065. int omap_hwmod_unregister(struct omap_hwmod *oh)
  1066. {
  1067. if (!oh)
  1068. return -EINVAL;
  1069. pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
  1070. mutex_lock(&omap_hwmod_mutex);
  1071. iounmap(oh->_mpu_rt_va);
  1072. list_del(&oh->node);
  1073. mutex_unlock(&omap_hwmod_mutex);
  1074. return 0;
  1075. }
  1076. /**
  1077. * omap_hwmod_enable - enable an omap_hwmod
  1078. * @oh: struct omap_hwmod *
  1079. *
  1080. * Enable an omap_hwomd @oh. Intended to be called by omap_device_enable().
  1081. * Returns -EINVAL on error or passes along the return value from _enable().
  1082. */
  1083. int omap_hwmod_enable(struct omap_hwmod *oh)
  1084. {
  1085. int r;
  1086. if (!oh)
  1087. return -EINVAL;
  1088. mutex_lock(&omap_hwmod_mutex);
  1089. r = _omap_hwmod_enable(oh);
  1090. mutex_unlock(&omap_hwmod_mutex);
  1091. return r;
  1092. }
  1093. /**
  1094. * omap_hwmod_idle - idle an omap_hwmod
  1095. * @oh: struct omap_hwmod *
  1096. *
  1097. * Idle an omap_hwomd @oh. Intended to be called by omap_device_idle().
  1098. * Returns -EINVAL on error or passes along the return value from _idle().
  1099. */
  1100. int omap_hwmod_idle(struct omap_hwmod *oh)
  1101. {
  1102. if (!oh)
  1103. return -EINVAL;
  1104. mutex_lock(&omap_hwmod_mutex);
  1105. _omap_hwmod_idle(oh);
  1106. mutex_unlock(&omap_hwmod_mutex);
  1107. return 0;
  1108. }
  1109. /**
  1110. * omap_hwmod_shutdown - shutdown an omap_hwmod
  1111. * @oh: struct omap_hwmod *
  1112. *
  1113. * Shutdown an omap_hwomd @oh. Intended to be called by
  1114. * omap_device_shutdown(). Returns -EINVAL on error or passes along
  1115. * the return value from _shutdown().
  1116. */
  1117. int omap_hwmod_shutdown(struct omap_hwmod *oh)
  1118. {
  1119. if (!oh)
  1120. return -EINVAL;
  1121. mutex_lock(&omap_hwmod_mutex);
  1122. _shutdown(oh);
  1123. mutex_unlock(&omap_hwmod_mutex);
  1124. return 0;
  1125. }
  1126. /**
  1127. * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
  1128. * @oh: struct omap_hwmod *oh
  1129. *
  1130. * Intended to be called by the omap_device code.
  1131. */
  1132. int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
  1133. {
  1134. mutex_lock(&omap_hwmod_mutex);
  1135. _enable_clocks(oh);
  1136. mutex_unlock(&omap_hwmod_mutex);
  1137. return 0;
  1138. }
  1139. /**
  1140. * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
  1141. * @oh: struct omap_hwmod *oh
  1142. *
  1143. * Intended to be called by the omap_device code.
  1144. */
  1145. int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
  1146. {
  1147. mutex_lock(&omap_hwmod_mutex);
  1148. _disable_clocks(oh);
  1149. mutex_unlock(&omap_hwmod_mutex);
  1150. return 0;
  1151. }
  1152. /**
  1153. * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
  1154. * @oh: struct omap_hwmod *oh
  1155. *
  1156. * Intended to be called by drivers and core code when all posted
  1157. * writes to a device must complete before continuing further
  1158. * execution (for example, after clearing some device IRQSTATUS
  1159. * register bits)
  1160. *
  1161. * XXX what about targets with multiple OCP threads?
  1162. */
  1163. void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
  1164. {
  1165. BUG_ON(!oh);
  1166. if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
  1167. WARN(1, "omap_device: %s: OCP barrier impossible due to "
  1168. "device configuration\n", oh->name);
  1169. return;
  1170. }
  1171. /*
  1172. * Forces posted writes to complete on the OCP thread handling
  1173. * register writes
  1174. */
  1175. omap_hwmod_readl(oh, oh->class->sysc->sysc_offs);
  1176. }
  1177. /**
  1178. * omap_hwmod_reset - reset the hwmod
  1179. * @oh: struct omap_hwmod *
  1180. *
  1181. * Under some conditions, a driver may wish to reset the entire device.
  1182. * Called from omap_device code. Returns -EINVAL on error or passes along
  1183. * the return value from _reset().
  1184. */
  1185. int omap_hwmod_reset(struct omap_hwmod *oh)
  1186. {
  1187. int r;
  1188. if (!oh)
  1189. return -EINVAL;
  1190. mutex_lock(&omap_hwmod_mutex);
  1191. r = _reset(oh);
  1192. mutex_unlock(&omap_hwmod_mutex);
  1193. return r;
  1194. }
  1195. /**
  1196. * omap_hwmod_count_resources - count number of struct resources needed by hwmod
  1197. * @oh: struct omap_hwmod *
  1198. * @res: pointer to the first element of an array of struct resource to fill
  1199. *
  1200. * Count the number of struct resource array elements necessary to
  1201. * contain omap_hwmod @oh resources. Intended to be called by code
  1202. * that registers omap_devices. Intended to be used to determine the
  1203. * size of a dynamically-allocated struct resource array, before
  1204. * calling omap_hwmod_fill_resources(). Returns the number of struct
  1205. * resource array elements needed.
  1206. *
  1207. * XXX This code is not optimized. It could attempt to merge adjacent
  1208. * resource IDs.
  1209. *
  1210. */
  1211. int omap_hwmod_count_resources(struct omap_hwmod *oh)
  1212. {
  1213. int ret, i;
  1214. ret = oh->mpu_irqs_cnt + oh->sdma_reqs_cnt;
  1215. for (i = 0; i < oh->slaves_cnt; i++)
  1216. ret += oh->slaves[i]->addr_cnt;
  1217. return ret;
  1218. }
  1219. /**
  1220. * omap_hwmod_fill_resources - fill struct resource array with hwmod data
  1221. * @oh: struct omap_hwmod *
  1222. * @res: pointer to the first element of an array of struct resource to fill
  1223. *
  1224. * Fill the struct resource array @res with resource data from the
  1225. * omap_hwmod @oh. Intended to be called by code that registers
  1226. * omap_devices. See also omap_hwmod_count_resources(). Returns the
  1227. * number of array elements filled.
  1228. */
  1229. int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
  1230. {
  1231. int i, j;
  1232. int r = 0;
  1233. /* For each IRQ, DMA, memory area, fill in array.*/
  1234. for (i = 0; i < oh->mpu_irqs_cnt; i++) {
  1235. (res + r)->name = (oh->mpu_irqs + i)->name;
  1236. (res + r)->start = (oh->mpu_irqs + i)->irq;
  1237. (res + r)->end = (oh->mpu_irqs + i)->irq;
  1238. (res + r)->flags = IORESOURCE_IRQ;
  1239. r++;
  1240. }
  1241. for (i = 0; i < oh->sdma_reqs_cnt; i++) {
  1242. (res + r)->name = (oh->sdma_reqs + i)->name;
  1243. (res + r)->start = (oh->sdma_reqs + i)->dma_req;
  1244. (res + r)->end = (oh->sdma_reqs + i)->dma_req;
  1245. (res + r)->flags = IORESOURCE_DMA;
  1246. r++;
  1247. }
  1248. for (i = 0; i < oh->slaves_cnt; i++) {
  1249. struct omap_hwmod_ocp_if *os;
  1250. os = oh->slaves[i];
  1251. for (j = 0; j < os->addr_cnt; j++) {
  1252. (res + r)->start = (os->addr + j)->pa_start;
  1253. (res + r)->end = (os->addr + j)->pa_end;
  1254. (res + r)->flags = IORESOURCE_MEM;
  1255. r++;
  1256. }
  1257. }
  1258. return r;
  1259. }
  1260. /**
  1261. * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
  1262. * @oh: struct omap_hwmod *
  1263. *
  1264. * Return the powerdomain pointer associated with the OMAP module
  1265. * @oh's main clock. If @oh does not have a main clk, return the
  1266. * powerdomain associated with the interface clock associated with the
  1267. * module's MPU port. (XXX Perhaps this should use the SDMA port
  1268. * instead?) Returns NULL on error, or a struct powerdomain * on
  1269. * success.
  1270. */
  1271. struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
  1272. {
  1273. struct clk *c;
  1274. if (!oh)
  1275. return NULL;
  1276. if (oh->_clk) {
  1277. c = oh->_clk;
  1278. } else {
  1279. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  1280. return NULL;
  1281. c = oh->slaves[oh->_mpu_port_index]->_clk;
  1282. }
  1283. if (!c->clkdm)
  1284. return NULL;
  1285. return c->clkdm->pwrdm.ptr;
  1286. }
  1287. /**
  1288. * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
  1289. * @oh: struct omap_hwmod *
  1290. *
  1291. * Returns the virtual address corresponding to the beginning of the
  1292. * module's register target, in the address range that is intended to
  1293. * be used by the MPU. Returns the virtual address upon success or NULL
  1294. * upon error.
  1295. */
  1296. void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
  1297. {
  1298. if (!oh)
  1299. return NULL;
  1300. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  1301. return NULL;
  1302. if (oh->_state == _HWMOD_STATE_UNKNOWN)
  1303. return NULL;
  1304. return oh->_mpu_rt_va;
  1305. }
  1306. /**
  1307. * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
  1308. * @oh: struct omap_hwmod *
  1309. * @init_oh: struct omap_hwmod * (initiator)
  1310. *
  1311. * Add a sleep dependency between the initiator @init_oh and @oh.
  1312. * Intended to be called by DSP/Bridge code via platform_data for the
  1313. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  1314. * code needs to add/del initiator dependencies dynamically
  1315. * before/after accessing a device. Returns the return value from
  1316. * _add_initiator_dep().
  1317. *
  1318. * XXX Keep a usecount in the clockdomain code
  1319. */
  1320. int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
  1321. struct omap_hwmod *init_oh)
  1322. {
  1323. return _add_initiator_dep(oh, init_oh);
  1324. }
  1325. /*
  1326. * XXX what about functions for drivers to save/restore ocp_sysconfig
  1327. * for context save/restore operations?
  1328. */
  1329. /**
  1330. * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
  1331. * @oh: struct omap_hwmod *
  1332. * @init_oh: struct omap_hwmod * (initiator)
  1333. *
  1334. * Remove a sleep dependency between the initiator @init_oh and @oh.
  1335. * Intended to be called by DSP/Bridge code via platform_data for the
  1336. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  1337. * code needs to add/del initiator dependencies dynamically
  1338. * before/after accessing a device. Returns the return value from
  1339. * _del_initiator_dep().
  1340. *
  1341. * XXX Keep a usecount in the clockdomain code
  1342. */
  1343. int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
  1344. struct omap_hwmod *init_oh)
  1345. {
  1346. return _del_initiator_dep(oh, init_oh);
  1347. }
  1348. /**
  1349. * omap_hwmod_enable_wakeup - allow device to wake up the system
  1350. * @oh: struct omap_hwmod *
  1351. *
  1352. * Sets the module OCP socket ENAWAKEUP bit to allow the module to
  1353. * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
  1354. * registers to cause the PRCM to receive wakeup events from the
  1355. * module. Does not set any wakeup routing registers beyond this
  1356. * point - if the module is to wake up any other module or subsystem,
  1357. * that must be set separately. Called by omap_device code. Returns
  1358. * -EINVAL on error or 0 upon success.
  1359. */
  1360. int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
  1361. {
  1362. if (!oh->class->sysc ||
  1363. !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
  1364. return -EINVAL;
  1365. mutex_lock(&omap_hwmod_mutex);
  1366. _enable_wakeup(oh);
  1367. mutex_unlock(&omap_hwmod_mutex);
  1368. return 0;
  1369. }
  1370. /**
  1371. * omap_hwmod_disable_wakeup - prevent device from waking the system
  1372. * @oh: struct omap_hwmod *
  1373. *
  1374. * Clears the module OCP socket ENAWAKEUP bit to prevent the module
  1375. * from sending wakeups to the PRCM. Eventually this should clear
  1376. * PRCM wakeup registers to cause the PRCM to ignore wakeup events
  1377. * from the module. Does not set any wakeup routing registers beyond
  1378. * this point - if the module is to wake up any other module or
  1379. * subsystem, that must be set separately. Called by omap_device
  1380. * code. Returns -EINVAL on error or 0 upon success.
  1381. */
  1382. int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
  1383. {
  1384. if (!oh->class->sysc ||
  1385. !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
  1386. return -EINVAL;
  1387. mutex_lock(&omap_hwmod_mutex);
  1388. _disable_wakeup(oh);
  1389. mutex_unlock(&omap_hwmod_mutex);
  1390. return 0;
  1391. }
  1392. /**
  1393. * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
  1394. * @classname: struct omap_hwmod_class name to search for
  1395. * @fn: callback function pointer to call for each hwmod in class @classname
  1396. * @user: arbitrary context data to pass to the callback function
  1397. *
  1398. * For each omap_hwmod of class @classname, call @fn. Takes
  1399. * omap_hwmod_mutex to prevent the hwmod list from changing during the
  1400. * iteration. If the callback function returns something other than
  1401. * zero, the iterator is terminated, and the callback function's return
  1402. * value is passed back to the caller. Returns 0 upon success, -EINVAL
  1403. * if @classname or @fn are NULL, or passes back the error code from @fn.
  1404. */
  1405. int omap_hwmod_for_each_by_class(const char *classname,
  1406. int (*fn)(struct omap_hwmod *oh,
  1407. void *user),
  1408. void *user)
  1409. {
  1410. struct omap_hwmod *temp_oh;
  1411. int ret = 0;
  1412. if (!classname || !fn)
  1413. return -EINVAL;
  1414. pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
  1415. __func__, classname);
  1416. mutex_lock(&omap_hwmod_mutex);
  1417. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  1418. if (!strcmp(temp_oh->class->name, classname)) {
  1419. pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
  1420. __func__, temp_oh->name);
  1421. ret = (*fn)(temp_oh, user);
  1422. if (ret)
  1423. break;
  1424. }
  1425. }
  1426. mutex_unlock(&omap_hwmod_mutex);
  1427. if (ret)
  1428. pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
  1429. __func__, ret);
  1430. return ret;
  1431. }