omap_hwmod.c 42 KB

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