omap_hwmod.c 39 KB

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