omap_hwmod.c 39 KB

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