omap_hwmod.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  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/cpu.h>
  47. #include <plat/clockdomain.h>
  48. #include <plat/powerdomain.h>
  49. #include <plat/clock.h>
  50. #include <plat/omap_hwmod.h>
  51. #include "cm.h"
  52. /* Maximum microseconds to wait for OMAP module to reset */
  53. #define MAX_MODULE_RESET_WAIT 10000
  54. /* Name of the OMAP hwmod for the MPU */
  55. #define MPU_INITIATOR_NAME "mpu_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. void __iomem *va_start;
  421. if (!oh || oh->slaves_cnt == 0)
  422. return NULL;
  423. os = *oh->slaves + index;
  424. for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
  425. if (mem->flags & ADDR_TYPE_RT) {
  426. found = 1;
  427. break;
  428. }
  429. }
  430. if (found) {
  431. va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
  432. if (!va_start) {
  433. pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
  434. return NULL;
  435. }
  436. pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
  437. oh->name, va_start);
  438. } else {
  439. pr_debug("omap_hwmod: %s: no MPU register target found\n",
  440. oh->name);
  441. }
  442. return (found) ? va_start : NULL;
  443. }
  444. /**
  445. * _sysc_enable - try to bring a module out of idle via OCP_SYSCONFIG
  446. * @oh: struct omap_hwmod *
  447. *
  448. * If module is marked as SWSUP_SIDLE, force the module out of slave
  449. * idle; otherwise, configure it for smart-idle. If module is marked
  450. * as SWSUP_MSUSPEND, force the module out of master standby;
  451. * otherwise, configure it for smart-standby. No return value.
  452. */
  453. static void _sysc_enable(struct omap_hwmod *oh)
  454. {
  455. u8 idlemode;
  456. u32 v;
  457. if (!oh->sysconfig)
  458. return;
  459. v = oh->_sysc_cache;
  460. if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
  461. idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
  462. HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
  463. _set_slave_idlemode(oh, idlemode, &v);
  464. }
  465. if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
  466. idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
  467. HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
  468. _set_master_standbymode(oh, idlemode, &v);
  469. }
  470. /* XXX OCP AUTOIDLE bit? */
  471. if (oh->flags & HWMOD_SET_DEFAULT_CLOCKACT &&
  472. oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY)
  473. _set_clockactivity(oh, oh->sysconfig->clockact, &v);
  474. _write_sysconfig(v, oh);
  475. }
  476. /**
  477. * _sysc_idle - try to put a module into idle via OCP_SYSCONFIG
  478. * @oh: struct omap_hwmod *
  479. *
  480. * If module is marked as SWSUP_SIDLE, force the module into slave
  481. * idle; otherwise, configure it for smart-idle. If module is marked
  482. * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
  483. * configure it for smart-standby. No return value.
  484. */
  485. static void _sysc_idle(struct omap_hwmod *oh)
  486. {
  487. u8 idlemode;
  488. u32 v;
  489. if (!oh->sysconfig)
  490. return;
  491. v = oh->_sysc_cache;
  492. if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE) {
  493. idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
  494. HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
  495. _set_slave_idlemode(oh, idlemode, &v);
  496. }
  497. if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE) {
  498. idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
  499. HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
  500. _set_master_standbymode(oh, idlemode, &v);
  501. }
  502. _write_sysconfig(v, oh);
  503. }
  504. /**
  505. * _sysc_shutdown - force a module into idle via OCP_SYSCONFIG
  506. * @oh: struct omap_hwmod *
  507. *
  508. * Force the module into slave idle and master suspend. No return
  509. * value.
  510. */
  511. static void _sysc_shutdown(struct omap_hwmod *oh)
  512. {
  513. u32 v;
  514. if (!oh->sysconfig)
  515. return;
  516. v = oh->_sysc_cache;
  517. if (oh->sysconfig->sysc_flags & SYSC_HAS_SIDLEMODE)
  518. _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
  519. if (oh->sysconfig->sysc_flags & SYSC_HAS_MIDLEMODE)
  520. _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
  521. /* XXX clear OCP AUTOIDLE bit? */
  522. _write_sysconfig(v, oh);
  523. }
  524. /**
  525. * _lookup - find an omap_hwmod by name
  526. * @name: find an omap_hwmod by name
  527. *
  528. * Return a pointer to an omap_hwmod by name, or NULL if not found.
  529. * Caller must hold omap_hwmod_mutex.
  530. */
  531. static struct omap_hwmod *_lookup(const char *name)
  532. {
  533. struct omap_hwmod *oh, *temp_oh;
  534. oh = NULL;
  535. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  536. if (!strcmp(name, temp_oh->name)) {
  537. oh = temp_oh;
  538. break;
  539. }
  540. }
  541. return oh;
  542. }
  543. /**
  544. * _init_clocks - clk_get() all clocks associated with this hwmod
  545. * @oh: struct omap_hwmod *
  546. *
  547. * Called by omap_hwmod_late_init() (after omap2_clk_init()).
  548. * Resolves all clock names embedded in the hwmod. Must be called
  549. * with omap_hwmod_mutex held. Returns -EINVAL if the omap_hwmod
  550. * has not yet been registered or if the clocks have already been
  551. * initialized, 0 on success, or a non-zero error on failure.
  552. */
  553. static int _init_clocks(struct omap_hwmod *oh)
  554. {
  555. int ret = 0;
  556. if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
  557. return -EINVAL;
  558. pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
  559. ret |= _init_main_clk(oh);
  560. ret |= _init_interface_clks(oh);
  561. ret |= _init_opt_clks(oh);
  562. oh->_state = _HWMOD_STATE_CLKS_INITED;
  563. return ret;
  564. }
  565. /**
  566. * _wait_target_ready - wait for a module to leave slave idle
  567. * @oh: struct omap_hwmod *
  568. *
  569. * Wait for a module @oh to leave slave idle. Returns 0 if the module
  570. * does not have an IDLEST bit or if the module successfully leaves
  571. * slave idle; otherwise, pass along the return value of the
  572. * appropriate *_cm_wait_module_ready() function.
  573. */
  574. static int _wait_target_ready(struct omap_hwmod *oh)
  575. {
  576. struct omap_hwmod_ocp_if *os;
  577. int ret;
  578. if (!oh)
  579. return -EINVAL;
  580. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  581. return 0;
  582. os = *oh->slaves + oh->_mpu_port_index;
  583. if (!(os->flags & OCPIF_HAS_IDLEST))
  584. return 0;
  585. /* XXX check module SIDLEMODE */
  586. /* XXX check clock enable states */
  587. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  588. ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
  589. oh->prcm.omap2.idlest_reg_id,
  590. oh->prcm.omap2.idlest_idle_bit);
  591. #if 0
  592. } else if (cpu_is_omap44xx()) {
  593. ret = omap4_cm_wait_module_ready(oh->prcm.omap4.module_offs,
  594. oh->prcm.omap4.device_offs);
  595. #endif
  596. } else {
  597. BUG();
  598. };
  599. return ret;
  600. }
  601. /**
  602. * _reset - reset an omap_hwmod
  603. * @oh: struct omap_hwmod *
  604. *
  605. * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
  606. * enabled for this to work. Must be called with omap_hwmod_mutex
  607. * held. Returns -EINVAL if the hwmod cannot be reset this way or if
  608. * the hwmod is in the wrong state, -ETIMEDOUT if the module did not
  609. * reset in time, or 0 upon success.
  610. */
  611. static int _reset(struct omap_hwmod *oh)
  612. {
  613. u32 r, v;
  614. int c;
  615. if (!oh->sysconfig ||
  616. !(oh->sysconfig->sysc_flags & SYSC_HAS_SOFTRESET) ||
  617. (oh->sysconfig->sysc_flags & SYSS_MISSING))
  618. return -EINVAL;
  619. /* clocks must be on for this operation */
  620. if (oh->_state != _HWMOD_STATE_ENABLED) {
  621. WARN(1, "omap_hwmod: %s: reset can only be entered from "
  622. "enabled state\n", oh->name);
  623. return -EINVAL;
  624. }
  625. pr_debug("omap_hwmod: %s: resetting\n", oh->name);
  626. v = oh->_sysc_cache;
  627. r = _set_softreset(oh, &v);
  628. if (r)
  629. return r;
  630. _write_sysconfig(v, oh);
  631. c = 0;
  632. while (c < MAX_MODULE_RESET_WAIT &&
  633. !(omap_hwmod_readl(oh, oh->sysconfig->syss_offs) &
  634. SYSS_RESETDONE_MASK)) {
  635. udelay(1);
  636. c++;
  637. }
  638. if (c == MAX_MODULE_RESET_WAIT)
  639. WARN(1, "omap_hwmod: %s: failed to reset in %d usec\n",
  640. oh->name, MAX_MODULE_RESET_WAIT);
  641. else
  642. pr_debug("omap_hwmod: %s: reset in %d usec\n", oh->name, c);
  643. /*
  644. * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
  645. * _wait_target_ready() or _reset()
  646. */
  647. return (c == MAX_MODULE_RESET_WAIT) ? -ETIMEDOUT : 0;
  648. }
  649. /**
  650. * _enable - enable an omap_hwmod
  651. * @oh: struct omap_hwmod *
  652. *
  653. * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
  654. * register target. Must be called with omap_hwmod_mutex held.
  655. * Returns -EINVAL if the hwmod is in the wrong state or passes along
  656. * the return value of _wait_target_ready().
  657. */
  658. static int _enable(struct omap_hwmod *oh)
  659. {
  660. int r;
  661. if (oh->_state != _HWMOD_STATE_INITIALIZED &&
  662. oh->_state != _HWMOD_STATE_IDLE &&
  663. oh->_state != _HWMOD_STATE_DISABLED) {
  664. WARN(1, "omap_hwmod: %s: enabled state can only be entered "
  665. "from initialized, idle, or disabled state\n", oh->name);
  666. return -EINVAL;
  667. }
  668. pr_debug("omap_hwmod: %s: enabling\n", oh->name);
  669. /* XXX mux balls */
  670. _add_initiator_dep(oh, mpu_oh);
  671. _enable_clocks(oh);
  672. if (oh->sysconfig) {
  673. if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
  674. _update_sysc_cache(oh);
  675. _sysc_enable(oh);
  676. }
  677. r = _wait_target_ready(oh);
  678. if (!r)
  679. oh->_state = _HWMOD_STATE_ENABLED;
  680. return r;
  681. }
  682. /**
  683. * _idle - idle an omap_hwmod
  684. * @oh: struct omap_hwmod *
  685. *
  686. * Idles an omap_hwmod @oh. This should be called once the hwmod has
  687. * no further work. Returns -EINVAL if the hwmod is in the wrong
  688. * state or returns 0.
  689. */
  690. static int _idle(struct omap_hwmod *oh)
  691. {
  692. if (oh->_state != _HWMOD_STATE_ENABLED) {
  693. WARN(1, "omap_hwmod: %s: idle state can only be entered from "
  694. "enabled state\n", oh->name);
  695. return -EINVAL;
  696. }
  697. pr_debug("omap_hwmod: %s: idling\n", oh->name);
  698. if (oh->sysconfig)
  699. _sysc_idle(oh);
  700. _del_initiator_dep(oh, mpu_oh);
  701. _disable_clocks(oh);
  702. oh->_state = _HWMOD_STATE_IDLE;
  703. return 0;
  704. }
  705. /**
  706. * _shutdown - shutdown an omap_hwmod
  707. * @oh: struct omap_hwmod *
  708. *
  709. * Shut down an omap_hwmod @oh. This should be called when the driver
  710. * used for the hwmod is removed or unloaded or if the driver is not
  711. * used by the system. Returns -EINVAL if the hwmod is in the wrong
  712. * state or returns 0.
  713. */
  714. static int _shutdown(struct omap_hwmod *oh)
  715. {
  716. if (oh->_state != _HWMOD_STATE_IDLE &&
  717. oh->_state != _HWMOD_STATE_ENABLED) {
  718. WARN(1, "omap_hwmod: %s: disabled state can only be entered "
  719. "from idle, or enabled state\n", oh->name);
  720. return -EINVAL;
  721. }
  722. pr_debug("omap_hwmod: %s: disabling\n", oh->name);
  723. if (oh->sysconfig)
  724. _sysc_shutdown(oh);
  725. _del_initiator_dep(oh, mpu_oh);
  726. /* XXX what about the other system initiators here? DMA, tesla, d2d */
  727. _disable_clocks(oh);
  728. /* XXX Should this code also force-disable the optional clocks? */
  729. /* XXX mux any associated balls to safe mode */
  730. oh->_state = _HWMOD_STATE_DISABLED;
  731. return 0;
  732. }
  733. /**
  734. * _write_clockact_lock - set the module's clockactivity bits
  735. * @oh: struct omap_hwmod *
  736. * @clockact: CLOCKACTIVITY field bits
  737. *
  738. * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
  739. * OCP_SYSCONFIG register. Returns -EINVAL if the hwmod is in the
  740. * wrong state or returns 0.
  741. */
  742. static int _write_clockact_lock(struct omap_hwmod *oh, u8 clockact)
  743. {
  744. u32 v;
  745. if (!oh->sysconfig ||
  746. !(oh->sysconfig->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
  747. return -EINVAL;
  748. mutex_lock(&omap_hwmod_mutex);
  749. v = oh->_sysc_cache;
  750. _set_clockactivity(oh, clockact, &v);
  751. _write_sysconfig(v, oh);
  752. mutex_unlock(&omap_hwmod_mutex);
  753. return 0;
  754. }
  755. /**
  756. * _setup - do initial configuration of omap_hwmod
  757. * @oh: struct omap_hwmod *
  758. *
  759. * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
  760. * OCP_SYSCONFIG register. Must be called with omap_hwmod_mutex
  761. * held. Returns -EINVAL if the hwmod is in the wrong state or returns
  762. * 0.
  763. */
  764. static int _setup(struct omap_hwmod *oh)
  765. {
  766. struct omap_hwmod_ocp_if *os;
  767. int i;
  768. if (!oh)
  769. return -EINVAL;
  770. /* Set iclk autoidle mode */
  771. if (oh->slaves_cnt > 0) {
  772. for (i = 0, os = *oh->slaves; i < oh->slaves_cnt; i++, os++) {
  773. struct clk *c = os->_clk;
  774. if (!c || IS_ERR(c))
  775. continue;
  776. if (os->flags & OCPIF_SWSUP_IDLE) {
  777. /* XXX omap_iclk_deny_idle(c); */
  778. } else {
  779. /* XXX omap_iclk_allow_idle(c); */
  780. clk_enable(c);
  781. }
  782. }
  783. }
  784. oh->_state = _HWMOD_STATE_INITIALIZED;
  785. _enable(oh);
  786. if (!(oh->flags & HWMOD_INIT_NO_RESET))
  787. _reset(oh);
  788. /* XXX OCP AUTOIDLE bit? */
  789. /* XXX OCP ENAWAKEUP bit? */
  790. if (!(oh->flags & HWMOD_INIT_NO_IDLE))
  791. _idle(oh);
  792. return 0;
  793. }
  794. /* Public functions */
  795. u32 omap_hwmod_readl(struct omap_hwmod *oh, u16 reg_offs)
  796. {
  797. return __raw_readl(oh->_rt_va + reg_offs);
  798. }
  799. void omap_hwmod_writel(u32 v, struct omap_hwmod *oh, u16 reg_offs)
  800. {
  801. __raw_writel(v, oh->_rt_va + reg_offs);
  802. }
  803. /**
  804. * omap_hwmod_register - register a struct omap_hwmod
  805. * @oh: struct omap_hwmod *
  806. *
  807. * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod already
  808. * has been registered by the same name; -EINVAL if the omap_hwmod is in the
  809. * wrong state, or 0 on success.
  810. *
  811. * XXX The data should be copied into bootmem, so the original data
  812. * should be marked __initdata and freed after init. This would allow
  813. * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
  814. * that the copy process would be relatively complex due to the large number
  815. * of substructures.
  816. */
  817. int omap_hwmod_register(struct omap_hwmod *oh)
  818. {
  819. int ret, ms_id;
  820. if (!oh || (oh->_state != _HWMOD_STATE_UNKNOWN))
  821. return -EINVAL;
  822. mutex_lock(&omap_hwmod_mutex);
  823. pr_debug("omap_hwmod: %s: registering\n", oh->name);
  824. if (_lookup(oh->name)) {
  825. ret = -EEXIST;
  826. goto ohr_unlock;
  827. }
  828. ms_id = _find_mpu_port_index(oh);
  829. if (!IS_ERR_VALUE(ms_id)) {
  830. oh->_mpu_port_index = ms_id;
  831. oh->_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
  832. } else {
  833. oh->_int_flags |= _HWMOD_NO_MPU_PORT;
  834. }
  835. list_add_tail(&oh->node, &omap_hwmod_list);
  836. oh->_state = _HWMOD_STATE_REGISTERED;
  837. ret = 0;
  838. ohr_unlock:
  839. mutex_unlock(&omap_hwmod_mutex);
  840. return ret;
  841. }
  842. /**
  843. * omap_hwmod_lookup - look up a registered omap_hwmod by name
  844. * @name: name of the omap_hwmod to look up
  845. *
  846. * Given a @name of an omap_hwmod, return a pointer to the registered
  847. * struct omap_hwmod *, or NULL upon error.
  848. */
  849. struct omap_hwmod *omap_hwmod_lookup(const char *name)
  850. {
  851. struct omap_hwmod *oh;
  852. if (!name)
  853. return NULL;
  854. mutex_lock(&omap_hwmod_mutex);
  855. oh = _lookup(name);
  856. mutex_unlock(&omap_hwmod_mutex);
  857. return oh;
  858. }
  859. /**
  860. * omap_hwmod_for_each - call function for each registered omap_hwmod
  861. * @fn: pointer to a callback function
  862. *
  863. * Call @fn for each registered omap_hwmod, passing @data to each
  864. * function. @fn must return 0 for success or any other value for
  865. * failure. If @fn returns non-zero, the iteration across omap_hwmods
  866. * will stop and the non-zero return value will be passed to the
  867. * caller of omap_hwmod_for_each(). @fn is called with
  868. * omap_hwmod_for_each() held.
  869. */
  870. int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh))
  871. {
  872. struct omap_hwmod *temp_oh;
  873. int ret;
  874. if (!fn)
  875. return -EINVAL;
  876. mutex_lock(&omap_hwmod_mutex);
  877. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  878. ret = (*fn)(temp_oh);
  879. if (ret)
  880. break;
  881. }
  882. mutex_unlock(&omap_hwmod_mutex);
  883. return ret;
  884. }
  885. /**
  886. * omap_hwmod_init - init omap_hwmod code and register hwmods
  887. * @ohs: pointer to an array of omap_hwmods to register
  888. *
  889. * Intended to be called early in boot before the clock framework is
  890. * initialized. If @ohs is not null, will register all omap_hwmods
  891. * listed in @ohs that are valid for this chip. Returns -EINVAL if
  892. * omap_hwmod_init() has already been called or 0 otherwise.
  893. */
  894. int omap_hwmod_init(struct omap_hwmod **ohs)
  895. {
  896. struct omap_hwmod *oh;
  897. int r;
  898. if (inited)
  899. return -EINVAL;
  900. inited = 1;
  901. if (!ohs)
  902. return 0;
  903. oh = *ohs;
  904. while (oh) {
  905. if (omap_chip_is(oh->omap_chip)) {
  906. r = omap_hwmod_register(oh);
  907. WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
  908. "%d\n", oh->name, r);
  909. }
  910. oh = *++ohs;
  911. }
  912. return 0;
  913. }
  914. /**
  915. * omap_hwmod_late_init - do some post-clock framework initialization
  916. *
  917. * Must be called after omap2_clk_init(). Resolves the struct clk names
  918. * to struct clk pointers for each registered omap_hwmod. Also calls
  919. * _setup() on each hwmod. Returns 0.
  920. */
  921. int omap_hwmod_late_init(void)
  922. {
  923. int r;
  924. /* XXX check return value */
  925. r = omap_hwmod_for_each(_init_clocks);
  926. WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
  927. mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
  928. WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
  929. MPU_INITIATOR_NAME);
  930. omap_hwmod_for_each(_setup);
  931. return 0;
  932. }
  933. /**
  934. * omap_hwmod_unregister - unregister an omap_hwmod
  935. * @oh: struct omap_hwmod *
  936. *
  937. * Unregisters a previously-registered omap_hwmod @oh. There's probably
  938. * no use case for this, so it is likely to be removed in a later version.
  939. *
  940. * XXX Free all of the bootmem-allocated structures here when that is
  941. * implemented. Make it clear that core code is the only code that is
  942. * expected to unregister modules.
  943. */
  944. int omap_hwmod_unregister(struct omap_hwmod *oh)
  945. {
  946. if (!oh)
  947. return -EINVAL;
  948. pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
  949. mutex_lock(&omap_hwmod_mutex);
  950. iounmap(oh->_rt_va);
  951. list_del(&oh->node);
  952. mutex_unlock(&omap_hwmod_mutex);
  953. return 0;
  954. }
  955. /**
  956. * omap_hwmod_enable - enable an omap_hwmod
  957. * @oh: struct omap_hwmod *
  958. *
  959. * Enable an omap_hwomd @oh. Intended to be called by omap_device_enable().
  960. * Returns -EINVAL on error or passes along the return value from _enable().
  961. */
  962. int omap_hwmod_enable(struct omap_hwmod *oh)
  963. {
  964. int r;
  965. if (!oh)
  966. return -EINVAL;
  967. mutex_lock(&omap_hwmod_mutex);
  968. r = _enable(oh);
  969. mutex_unlock(&omap_hwmod_mutex);
  970. return r;
  971. }
  972. /**
  973. * omap_hwmod_idle - idle an omap_hwmod
  974. * @oh: struct omap_hwmod *
  975. *
  976. * Idle an omap_hwomd @oh. Intended to be called by omap_device_idle().
  977. * Returns -EINVAL on error or passes along the return value from _idle().
  978. */
  979. int omap_hwmod_idle(struct omap_hwmod *oh)
  980. {
  981. if (!oh)
  982. return -EINVAL;
  983. mutex_lock(&omap_hwmod_mutex);
  984. _idle(oh);
  985. mutex_unlock(&omap_hwmod_mutex);
  986. return 0;
  987. }
  988. /**
  989. * omap_hwmod_shutdown - shutdown an omap_hwmod
  990. * @oh: struct omap_hwmod *
  991. *
  992. * Shutdown an omap_hwomd @oh. Intended to be called by
  993. * omap_device_shutdown(). Returns -EINVAL on error or passes along
  994. * the return value from _shutdown().
  995. */
  996. int omap_hwmod_shutdown(struct omap_hwmod *oh)
  997. {
  998. if (!oh)
  999. return -EINVAL;
  1000. mutex_lock(&omap_hwmod_mutex);
  1001. _shutdown(oh);
  1002. mutex_unlock(&omap_hwmod_mutex);
  1003. return 0;
  1004. }
  1005. /**
  1006. * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
  1007. * @oh: struct omap_hwmod *oh
  1008. *
  1009. * Intended to be called by the omap_device code.
  1010. */
  1011. int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
  1012. {
  1013. mutex_lock(&omap_hwmod_mutex);
  1014. _enable_clocks(oh);
  1015. mutex_unlock(&omap_hwmod_mutex);
  1016. return 0;
  1017. }
  1018. /**
  1019. * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
  1020. * @oh: struct omap_hwmod *oh
  1021. *
  1022. * Intended to be called by the omap_device code.
  1023. */
  1024. int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
  1025. {
  1026. mutex_lock(&omap_hwmod_mutex);
  1027. _disable_clocks(oh);
  1028. mutex_unlock(&omap_hwmod_mutex);
  1029. return 0;
  1030. }
  1031. /**
  1032. * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
  1033. * @oh: struct omap_hwmod *oh
  1034. *
  1035. * Intended to be called by drivers and core code when all posted
  1036. * writes to a device must complete before continuing further
  1037. * execution (for example, after clearing some device IRQSTATUS
  1038. * register bits)
  1039. *
  1040. * XXX what about targets with multiple OCP threads?
  1041. */
  1042. void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
  1043. {
  1044. BUG_ON(!oh);
  1045. if (!oh->sysconfig || !oh->sysconfig->sysc_flags) {
  1046. WARN(1, "omap_device: %s: OCP barrier impossible due to "
  1047. "device configuration\n", oh->name);
  1048. return;
  1049. }
  1050. /*
  1051. * Forces posted writes to complete on the OCP thread handling
  1052. * register writes
  1053. */
  1054. omap_hwmod_readl(oh, oh->sysconfig->sysc_offs);
  1055. }
  1056. /**
  1057. * omap_hwmod_reset - reset the hwmod
  1058. * @oh: struct omap_hwmod *
  1059. *
  1060. * Under some conditions, a driver may wish to reset the entire device.
  1061. * Called from omap_device code. Returns -EINVAL on error or passes along
  1062. * the return value from _reset()/_enable().
  1063. */
  1064. int omap_hwmod_reset(struct omap_hwmod *oh)
  1065. {
  1066. int r;
  1067. if (!oh || !(oh->_state & _HWMOD_STATE_ENABLED))
  1068. return -EINVAL;
  1069. mutex_lock(&omap_hwmod_mutex);
  1070. r = _reset(oh);
  1071. if (!r)
  1072. r = _enable(oh);
  1073. mutex_unlock(&omap_hwmod_mutex);
  1074. return r;
  1075. }
  1076. /**
  1077. * omap_hwmod_count_resources - count number of struct resources needed by hwmod
  1078. * @oh: struct omap_hwmod *
  1079. * @res: pointer to the first element of an array of struct resource to fill
  1080. *
  1081. * Count the number of struct resource array elements necessary to
  1082. * contain omap_hwmod @oh resources. Intended to be called by code
  1083. * that registers omap_devices. Intended to be used to determine the
  1084. * size of a dynamically-allocated struct resource array, before
  1085. * calling omap_hwmod_fill_resources(). Returns the number of struct
  1086. * resource array elements needed.
  1087. *
  1088. * XXX This code is not optimized. It could attempt to merge adjacent
  1089. * resource IDs.
  1090. *
  1091. */
  1092. int omap_hwmod_count_resources(struct omap_hwmod *oh)
  1093. {
  1094. int ret, i;
  1095. ret = oh->mpu_irqs_cnt + oh->sdma_chs_cnt;
  1096. for (i = 0; i < oh->slaves_cnt; i++)
  1097. ret += (*oh->slaves + i)->addr_cnt;
  1098. return ret;
  1099. }
  1100. /**
  1101. * omap_hwmod_fill_resources - fill struct resource array with hwmod data
  1102. * @oh: struct omap_hwmod *
  1103. * @res: pointer to the first element of an array of struct resource to fill
  1104. *
  1105. * Fill the struct resource array @res with resource data from the
  1106. * omap_hwmod @oh. Intended to be called by code that registers
  1107. * omap_devices. See also omap_hwmod_count_resources(). Returns the
  1108. * number of array elements filled.
  1109. */
  1110. int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
  1111. {
  1112. int i, j;
  1113. int r = 0;
  1114. /* For each IRQ, DMA, memory area, fill in array.*/
  1115. for (i = 0; i < oh->mpu_irqs_cnt; i++) {
  1116. (res + r)->start = *(oh->mpu_irqs + i);
  1117. (res + r)->end = *(oh->mpu_irqs + i);
  1118. (res + r)->flags = IORESOURCE_IRQ;
  1119. r++;
  1120. }
  1121. for (i = 0; i < oh->sdma_chs_cnt; i++) {
  1122. (res + r)->name = (oh->sdma_chs + i)->name;
  1123. (res + r)->start = (oh->sdma_chs + i)->dma_ch;
  1124. (res + r)->end = (oh->sdma_chs + i)->dma_ch;
  1125. (res + r)->flags = IORESOURCE_DMA;
  1126. r++;
  1127. }
  1128. for (i = 0; i < oh->slaves_cnt; i++) {
  1129. struct omap_hwmod_ocp_if *os;
  1130. os = *oh->slaves + i;
  1131. for (j = 0; j < os->addr_cnt; j++) {
  1132. (res + r)->start = (os->addr + j)->pa_start;
  1133. (res + r)->end = (os->addr + j)->pa_end;
  1134. (res + r)->flags = IORESOURCE_MEM;
  1135. r++;
  1136. }
  1137. }
  1138. return r;
  1139. }
  1140. /**
  1141. * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
  1142. * @oh: struct omap_hwmod *
  1143. *
  1144. * Return the powerdomain pointer associated with the OMAP module
  1145. * @oh's main clock. If @oh does not have a main clk, return the
  1146. * powerdomain associated with the interface clock associated with the
  1147. * module's MPU port. (XXX Perhaps this should use the SDMA port
  1148. * instead?) Returns NULL on error, or a struct powerdomain * on
  1149. * success.
  1150. */
  1151. struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
  1152. {
  1153. struct clk *c;
  1154. if (!oh)
  1155. return NULL;
  1156. if (oh->_clk) {
  1157. c = oh->_clk;
  1158. } else {
  1159. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  1160. return NULL;
  1161. c = oh->slaves[oh->_mpu_port_index]->_clk;
  1162. }
  1163. return c->clkdm->pwrdm.ptr;
  1164. }
  1165. /**
  1166. * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
  1167. * @oh: struct omap_hwmod *
  1168. * @init_oh: struct omap_hwmod * (initiator)
  1169. *
  1170. * Add a sleep dependency between the initiator @init_oh and @oh.
  1171. * Intended to be called by DSP/Bridge code via platform_data for the
  1172. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  1173. * code needs to add/del initiator dependencies dynamically
  1174. * before/after accessing a device. Returns the return value from
  1175. * _add_initiator_dep().
  1176. *
  1177. * XXX Keep a usecount in the clockdomain code
  1178. */
  1179. int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
  1180. struct omap_hwmod *init_oh)
  1181. {
  1182. return _add_initiator_dep(oh, init_oh);
  1183. }
  1184. /*
  1185. * XXX what about functions for drivers to save/restore ocp_sysconfig
  1186. * for context save/restore operations?
  1187. */
  1188. /**
  1189. * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
  1190. * @oh: struct omap_hwmod *
  1191. * @init_oh: struct omap_hwmod * (initiator)
  1192. *
  1193. * Remove a sleep dependency between the initiator @init_oh and @oh.
  1194. * Intended to be called by DSP/Bridge code via platform_data for the
  1195. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  1196. * code needs to add/del initiator dependencies dynamically
  1197. * before/after accessing a device. Returns the return value from
  1198. * _del_initiator_dep().
  1199. *
  1200. * XXX Keep a usecount in the clockdomain code
  1201. */
  1202. int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
  1203. struct omap_hwmod *init_oh)
  1204. {
  1205. return _del_initiator_dep(oh, init_oh);
  1206. }
  1207. /**
  1208. * omap_hwmod_set_clockact_none - set clockactivity test to BOTH
  1209. * @oh: struct omap_hwmod *
  1210. *
  1211. * On some modules, this function can affect the wakeup latency vs.
  1212. * power consumption balance. Intended to be called by the
  1213. * omap_device layer. Passes along the return value from
  1214. * _write_clockact_lock().
  1215. */
  1216. int omap_hwmod_set_clockact_both(struct omap_hwmod *oh)
  1217. {
  1218. return _write_clockact_lock(oh, CLOCKACT_TEST_BOTH);
  1219. }
  1220. /**
  1221. * omap_hwmod_set_clockact_none - set clockactivity test to MAIN
  1222. * @oh: struct omap_hwmod *
  1223. *
  1224. * On some modules, this function can affect the wakeup latency vs.
  1225. * power consumption balance. Intended to be called by the
  1226. * omap_device layer. Passes along the return value from
  1227. * _write_clockact_lock().
  1228. */
  1229. int omap_hwmod_set_clockact_main(struct omap_hwmod *oh)
  1230. {
  1231. return _write_clockact_lock(oh, CLOCKACT_TEST_MAIN);
  1232. }
  1233. /**
  1234. * omap_hwmod_set_clockact_none - set clockactivity test to ICLK
  1235. * @oh: struct omap_hwmod *
  1236. *
  1237. * On some modules, this function can affect the wakeup latency vs.
  1238. * power consumption balance. Intended to be called by the
  1239. * omap_device layer. Passes along the return value from
  1240. * _write_clockact_lock().
  1241. */
  1242. int omap_hwmod_set_clockact_iclk(struct omap_hwmod *oh)
  1243. {
  1244. return _write_clockact_lock(oh, CLOCKACT_TEST_ICLK);
  1245. }
  1246. /**
  1247. * omap_hwmod_set_clockact_none - set clockactivity test to NONE
  1248. * @oh: struct omap_hwmod *
  1249. *
  1250. * On some modules, this function can affect the wakeup latency vs.
  1251. * power consumption balance. Intended to be called by the
  1252. * omap_device layer. Passes along the return value from
  1253. * _write_clockact_lock().
  1254. */
  1255. int omap_hwmod_set_clockact_none(struct omap_hwmod *oh)
  1256. {
  1257. return _write_clockact_lock(oh, CLOCKACT_TEST_NONE);
  1258. }
  1259. /**
  1260. * omap_hwmod_enable_wakeup - allow device to wake up the system
  1261. * @oh: struct omap_hwmod *
  1262. *
  1263. * Sets the module OCP socket ENAWAKEUP bit to allow the module to
  1264. * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
  1265. * registers to cause the PRCM to receive wakeup events from the
  1266. * module. Does not set any wakeup routing registers beyond this
  1267. * point - if the module is to wake up any other module or subsystem,
  1268. * that must be set separately. Called by omap_device code. Returns
  1269. * -EINVAL on error or 0 upon success.
  1270. */
  1271. int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
  1272. {
  1273. if (!oh->sysconfig ||
  1274. !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
  1275. return -EINVAL;
  1276. mutex_lock(&omap_hwmod_mutex);
  1277. _enable_wakeup(oh);
  1278. mutex_unlock(&omap_hwmod_mutex);
  1279. return 0;
  1280. }
  1281. /**
  1282. * omap_hwmod_disable_wakeup - prevent device from waking the system
  1283. * @oh: struct omap_hwmod *
  1284. *
  1285. * Clears the module OCP socket ENAWAKEUP bit to prevent the module
  1286. * from sending wakeups to the PRCM. Eventually this should clear
  1287. * PRCM wakeup registers to cause the PRCM to ignore wakeup events
  1288. * from the module. Does not set any wakeup routing registers beyond
  1289. * this point - if the module is to wake up any other module or
  1290. * subsystem, that must be set separately. Called by omap_device
  1291. * code. Returns -EINVAL on error or 0 upon success.
  1292. */
  1293. int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
  1294. {
  1295. if (!oh->sysconfig ||
  1296. !(oh->sysconfig->sysc_flags & SYSC_HAS_ENAWAKEUP))
  1297. return -EINVAL;
  1298. mutex_lock(&omap_hwmod_mutex);
  1299. _disable_wakeup(oh);
  1300. mutex_unlock(&omap_hwmod_mutex);
  1301. return 0;
  1302. }