omap_hwmod.c 41 KB

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