omap_hwmod.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  1. /*
  2. * omap_hwmod implementation for OMAP2/3/4
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. *
  6. * Paul Walmsley, Benoît Cousson, Kevin Hilman
  7. *
  8. * Created in collaboration with (alphabetical order): Thara Gopinath,
  9. * Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari Poussa, Anand
  10. * 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. * Introduction
  17. * ------------
  18. * One way to view an OMAP SoC is as a collection of largely unrelated
  19. * IP blocks connected by interconnects. The IP blocks include
  20. * devices such as ARM processors, audio serial interfaces, UARTs,
  21. * etc. Some of these devices, like the DSP, are created by TI;
  22. * others, like the SGX, largely originate from external vendors. In
  23. * TI's documentation, on-chip devices are referred to as "OMAP
  24. * modules." Some of these IP blocks are identical across several
  25. * OMAP versions. Others are revised frequently.
  26. *
  27. * These OMAP modules are tied together by various interconnects.
  28. * Most of the address and data flow between modules is via OCP-based
  29. * interconnects such as the L3 and L4 buses; but there are other
  30. * interconnects that distribute the hardware clock tree, handle idle
  31. * and reset signaling, supply power, and connect the modules to
  32. * various pads or balls on the OMAP package.
  33. *
  34. * OMAP hwmod provides a consistent way to describe the on-chip
  35. * hardware blocks and their integration into the rest of the chip.
  36. * This description can be automatically generated from the TI
  37. * hardware database. OMAP hwmod provides a standard, consistent API
  38. * to reset, enable, idle, and disable these hardware blocks. And
  39. * hwmod provides a way for other core code, such as the Linux device
  40. * code or the OMAP power management and address space mapping code,
  41. * to query the hardware database.
  42. *
  43. * Using hwmod
  44. * -----------
  45. * Drivers won't call hwmod functions directly. That is done by the
  46. * omap_device code, and in rare occasions, by custom integration code
  47. * in arch/arm/ *omap*. The omap_device code includes functions to
  48. * build a struct platform_device using omap_hwmod data, and that is
  49. * currently how hwmod data is communicated to drivers and to the
  50. * Linux driver model. Most drivers will call omap_hwmod functions only
  51. * indirectly, via pm_runtime*() functions.
  52. *
  53. * From a layering perspective, here is where the OMAP hwmod code
  54. * fits into the kernel software stack:
  55. *
  56. * +-------------------------------+
  57. * | Device driver code |
  58. * | (e.g., drivers/) |
  59. * +-------------------------------+
  60. * | Linux driver model |
  61. * | (platform_device / |
  62. * | platform_driver data/code) |
  63. * +-------------------------------+
  64. * | OMAP core-driver integration |
  65. * |(arch/arm/mach-omap2/devices.c)|
  66. * +-------------------------------+
  67. * | omap_device code |
  68. * | (../plat-omap/omap_device.c) |
  69. * +-------------------------------+
  70. * ----> | omap_hwmod code/data | <-----
  71. * | (../mach-omap2/omap_hwmod*) |
  72. * +-------------------------------+
  73. * | OMAP clock/PRCM/register fns |
  74. * | (__raw_{read,write}l, clk*) |
  75. * +-------------------------------+
  76. *
  77. * Device drivers should not contain any OMAP-specific code or data in
  78. * them. They should only contain code to operate the IP block that
  79. * the driver is responsible for. This is because these IP blocks can
  80. * also appear in other SoCs, either from TI (such as DaVinci) or from
  81. * other manufacturers; and drivers should be reusable across other
  82. * platforms.
  83. *
  84. * The OMAP hwmod code also will attempt to reset and idle all on-chip
  85. * devices upon boot. The goal here is for the kernel to be
  86. * completely self-reliant and independent from bootloaders. This is
  87. * to ensure a repeatable configuration, both to ensure consistent
  88. * runtime behavior, and to make it easier for others to reproduce
  89. * bugs.
  90. *
  91. * OMAP module activity states
  92. * ---------------------------
  93. * The hwmod code considers modules to be in one of several activity
  94. * states. IP blocks start out in an UNKNOWN state, then once they
  95. * are registered via the hwmod code, proceed to the REGISTERED state.
  96. * Once their clock names are resolved to clock pointers, the module
  97. * enters the CLKS_INITED state; and finally, once the module has been
  98. * reset and the integration registers programmed, the INITIALIZED state
  99. * is entered. The hwmod code will then place the module into either
  100. * the IDLE state to save power, or in the case of a critical system
  101. * module, the ENABLED state.
  102. *
  103. * OMAP core integration code can then call omap_hwmod*() functions
  104. * directly to move the module between the IDLE, ENABLED, and DISABLED
  105. * states, as needed. This is done during both the PM idle loop, and
  106. * in the OMAP core integration code's implementation of the PM runtime
  107. * functions.
  108. *
  109. * References
  110. * ----------
  111. * This is a partial list.
  112. * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
  113. * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
  114. * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
  115. * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
  116. * - Open Core Protocol Specification 2.2
  117. *
  118. * To do:
  119. * - pin mux handling
  120. * - handle IO mapping
  121. * - bus throughput & module latency measurement code
  122. *
  123. * XXX add tests at the beginning of each function to ensure the hwmod is
  124. * in the appropriate state
  125. * XXX error return values should be checked to ensure that they are
  126. * appropriate
  127. */
  128. #undef DEBUG
  129. #include <linux/kernel.h>
  130. #include <linux/errno.h>
  131. #include <linux/io.h>
  132. #include <linux/clk.h>
  133. #include <linux/delay.h>
  134. #include <linux/err.h>
  135. #include <linux/list.h>
  136. #include <linux/mutex.h>
  137. #include <plat/common.h>
  138. #include <plat/cpu.h>
  139. #include <plat/clockdomain.h>
  140. #include <plat/powerdomain.h>
  141. #include <plat/clock.h>
  142. #include <plat/omap_hwmod.h>
  143. #include <plat/prcm.h>
  144. #include "cm.h"
  145. #include "prm.h"
  146. /* Maximum microseconds to wait for OMAP module to softreset */
  147. #define MAX_MODULE_SOFTRESET_WAIT 10000
  148. /* Name of the OMAP hwmod for the MPU */
  149. #define MPU_INITIATOR_NAME "mpu"
  150. /* omap_hwmod_list contains all registered struct omap_hwmods */
  151. static LIST_HEAD(omap_hwmod_list);
  152. static DEFINE_MUTEX(omap_hwmod_mutex);
  153. /* mpu_oh: used to add/remove MPU initiator from sleepdep list */
  154. static struct omap_hwmod *mpu_oh;
  155. /* inited: 0 if omap_hwmod_init() has not yet been called; 1 otherwise */
  156. static u8 inited;
  157. /* Private functions */
  158. /**
  159. * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
  160. * @oh: struct omap_hwmod *
  161. *
  162. * Load the current value of the hwmod OCP_SYSCONFIG register into the
  163. * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
  164. * OCP_SYSCONFIG register or 0 upon success.
  165. */
  166. static int _update_sysc_cache(struct omap_hwmod *oh)
  167. {
  168. if (!oh->class->sysc) {
  169. WARN(1, "omap_hwmod: %s: cannot read OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
  170. return -EINVAL;
  171. }
  172. /* XXX ensure module interface clock is up */
  173. oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
  174. if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
  175. oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
  176. return 0;
  177. }
  178. /**
  179. * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
  180. * @v: OCP_SYSCONFIG value to write
  181. * @oh: struct omap_hwmod *
  182. *
  183. * Write @v into the module class' OCP_SYSCONFIG register, if it has
  184. * one. No return value.
  185. */
  186. static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
  187. {
  188. if (!oh->class->sysc) {
  189. WARN(1, "omap_hwmod: %s: cannot write OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
  190. return;
  191. }
  192. /* XXX ensure module interface clock is up */
  193. if (oh->_sysc_cache != v) {
  194. oh->_sysc_cache = v;
  195. omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
  196. }
  197. }
  198. /**
  199. * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
  200. * @oh: struct omap_hwmod *
  201. * @standbymode: MIDLEMODE field bits
  202. * @v: pointer to register contents to modify
  203. *
  204. * Update the master standby mode bits in @v to be @standbymode for
  205. * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
  206. * upon error or 0 upon success.
  207. */
  208. static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
  209. u32 *v)
  210. {
  211. u32 mstandby_mask;
  212. u8 mstandby_shift;
  213. if (!oh->class->sysc ||
  214. !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
  215. return -EINVAL;
  216. if (!oh->class->sysc->sysc_fields) {
  217. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  218. return -EINVAL;
  219. }
  220. mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
  221. mstandby_mask = (0x3 << mstandby_shift);
  222. *v &= ~mstandby_mask;
  223. *v |= __ffs(standbymode) << mstandby_shift;
  224. return 0;
  225. }
  226. /**
  227. * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
  228. * @oh: struct omap_hwmod *
  229. * @idlemode: SIDLEMODE field bits
  230. * @v: pointer to register contents to modify
  231. *
  232. * Update the slave idle mode bits in @v to be @idlemode for the @oh
  233. * hwmod. Does not write to the hardware. Returns -EINVAL upon error
  234. * or 0 upon success.
  235. */
  236. static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
  237. {
  238. u32 sidle_mask;
  239. u8 sidle_shift;
  240. if (!oh->class->sysc ||
  241. !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
  242. return -EINVAL;
  243. if (!oh->class->sysc->sysc_fields) {
  244. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  245. return -EINVAL;
  246. }
  247. sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
  248. sidle_mask = (0x3 << sidle_shift);
  249. *v &= ~sidle_mask;
  250. *v |= __ffs(idlemode) << sidle_shift;
  251. return 0;
  252. }
  253. /**
  254. * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
  255. * @oh: struct omap_hwmod *
  256. * @clockact: CLOCKACTIVITY field bits
  257. * @v: pointer to register contents to modify
  258. *
  259. * Update the clockactivity mode bits in @v to be @clockact for the
  260. * @oh hwmod. Used for additional powersaving on some modules. Does
  261. * not write to the hardware. Returns -EINVAL upon error or 0 upon
  262. * success.
  263. */
  264. static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
  265. {
  266. u32 clkact_mask;
  267. u8 clkact_shift;
  268. if (!oh->class->sysc ||
  269. !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
  270. return -EINVAL;
  271. if (!oh->class->sysc->sysc_fields) {
  272. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  273. return -EINVAL;
  274. }
  275. clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
  276. clkact_mask = (0x3 << clkact_shift);
  277. *v &= ~clkact_mask;
  278. *v |= clockact << clkact_shift;
  279. return 0;
  280. }
  281. /**
  282. * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
  283. * @oh: struct omap_hwmod *
  284. * @v: pointer to register contents to modify
  285. *
  286. * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
  287. * error or 0 upon success.
  288. */
  289. static int _set_softreset(struct omap_hwmod *oh, u32 *v)
  290. {
  291. u32 softrst_mask;
  292. if (!oh->class->sysc ||
  293. !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
  294. return -EINVAL;
  295. if (!oh->class->sysc->sysc_fields) {
  296. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  297. return -EINVAL;
  298. }
  299. softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
  300. *v |= softrst_mask;
  301. return 0;
  302. }
  303. /**
  304. * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
  305. * @oh: struct omap_hwmod *
  306. * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
  307. * @v: pointer to register contents to modify
  308. *
  309. * Update the module autoidle bit in @v to be @autoidle for the @oh
  310. * hwmod. The autoidle bit controls whether the module can gate
  311. * internal clocks automatically when it isn't doing anything; the
  312. * exact function of this bit varies on a per-module basis. This
  313. * function does not write to the hardware. Returns -EINVAL upon
  314. * error or 0 upon success.
  315. */
  316. static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
  317. u32 *v)
  318. {
  319. u32 autoidle_mask;
  320. u8 autoidle_shift;
  321. if (!oh->class->sysc ||
  322. !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
  323. return -EINVAL;
  324. if (!oh->class->sysc->sysc_fields) {
  325. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  326. return -EINVAL;
  327. }
  328. autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
  329. autoidle_mask = (0x3 << autoidle_shift);
  330. *v &= ~autoidle_mask;
  331. *v |= autoidle << autoidle_shift;
  332. return 0;
  333. }
  334. /**
  335. * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
  336. * @oh: struct omap_hwmod *
  337. *
  338. * Allow the hardware module @oh to send wakeups. Returns -EINVAL
  339. * upon error or 0 upon success.
  340. */
  341. static int _enable_wakeup(struct omap_hwmod *oh)
  342. {
  343. u32 v, wakeup_mask;
  344. if (!oh->class->sysc ||
  345. !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
  346. return -EINVAL;
  347. if (!oh->class->sysc->sysc_fields) {
  348. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  349. return -EINVAL;
  350. }
  351. wakeup_mask = (0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
  352. v = oh->_sysc_cache;
  353. v |= wakeup_mask;
  354. _write_sysconfig(v, oh);
  355. /* XXX test pwrdm_get_wken for this hwmod's subsystem */
  356. oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
  357. return 0;
  358. }
  359. /**
  360. * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
  361. * @oh: struct omap_hwmod *
  362. *
  363. * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
  364. * upon error or 0 upon success.
  365. */
  366. static int _disable_wakeup(struct omap_hwmod *oh)
  367. {
  368. u32 v, wakeup_mask;
  369. if (!oh->class->sysc ||
  370. !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
  371. return -EINVAL;
  372. if (!oh->class->sysc->sysc_fields) {
  373. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  374. return -EINVAL;
  375. }
  376. wakeup_mask = (0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
  377. v = oh->_sysc_cache;
  378. v &= ~wakeup_mask;
  379. _write_sysconfig(v, oh);
  380. /* XXX test pwrdm_get_wken for this hwmod's subsystem */
  381. oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
  382. return 0;
  383. }
  384. /**
  385. * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
  386. * @oh: struct omap_hwmod *
  387. *
  388. * Prevent the hardware module @oh from entering idle while the
  389. * hardare module initiator @init_oh is active. Useful when a module
  390. * will be accessed by a particular initiator (e.g., if a module will
  391. * be accessed by the IVA, there should be a sleepdep between the IVA
  392. * initiator and the module). Only applies to modules in smart-idle
  393. * mode. Returns -EINVAL upon error or passes along
  394. * clkdm_add_sleepdep() value upon success.
  395. */
  396. static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
  397. {
  398. if (!oh->_clk)
  399. return -EINVAL;
  400. return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
  401. }
  402. /**
  403. * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
  404. * @oh: struct omap_hwmod *
  405. *
  406. * Allow the hardware module @oh to enter idle while the hardare
  407. * module initiator @init_oh is active. Useful when a module will not
  408. * be accessed by a particular initiator (e.g., if a module will not
  409. * be accessed by the IVA, there should be no sleepdep between the IVA
  410. * initiator and the module). Only applies to modules in smart-idle
  411. * mode. Returns -EINVAL upon error or passes along
  412. * clkdm_del_sleepdep() value upon success.
  413. */
  414. static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
  415. {
  416. if (!oh->_clk)
  417. return -EINVAL;
  418. return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
  419. }
  420. /**
  421. * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
  422. * @oh: struct omap_hwmod *
  423. *
  424. * Called from _init_clocks(). Populates the @oh _clk (main
  425. * functional clock pointer) if a main_clk is present. Returns 0 on
  426. * success or -EINVAL on error.
  427. */
  428. static int _init_main_clk(struct omap_hwmod *oh)
  429. {
  430. int ret = 0;
  431. if (!oh->main_clk)
  432. return 0;
  433. oh->_clk = omap_clk_get_by_name(oh->main_clk);
  434. if (!oh->_clk) {
  435. pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
  436. oh->name, oh->main_clk);
  437. return -EINVAL;
  438. }
  439. if (!oh->_clk->clkdm)
  440. pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
  441. oh->main_clk, oh->_clk->name);
  442. return ret;
  443. }
  444. /**
  445. * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
  446. * @oh: struct omap_hwmod *
  447. *
  448. * Called from _init_clocks(). Populates the @oh OCP slave interface
  449. * clock pointers. Returns 0 on success or -EINVAL on error.
  450. */
  451. static int _init_interface_clks(struct omap_hwmod *oh)
  452. {
  453. struct clk *c;
  454. int i;
  455. int ret = 0;
  456. if (oh->slaves_cnt == 0)
  457. return 0;
  458. for (i = 0; i < oh->slaves_cnt; i++) {
  459. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  460. if (!os->clk)
  461. continue;
  462. c = omap_clk_get_by_name(os->clk);
  463. if (!c) {
  464. pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
  465. oh->name, os->clk);
  466. ret = -EINVAL;
  467. }
  468. os->_clk = c;
  469. }
  470. return ret;
  471. }
  472. /**
  473. * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
  474. * @oh: struct omap_hwmod *
  475. *
  476. * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
  477. * clock pointers. Returns 0 on success or -EINVAL on error.
  478. */
  479. static int _init_opt_clks(struct omap_hwmod *oh)
  480. {
  481. struct omap_hwmod_opt_clk *oc;
  482. struct clk *c;
  483. int i;
  484. int ret = 0;
  485. for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
  486. c = omap_clk_get_by_name(oc->clk);
  487. if (!c) {
  488. pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
  489. oh->name, oc->clk);
  490. ret = -EINVAL;
  491. }
  492. oc->_clk = c;
  493. }
  494. return ret;
  495. }
  496. /**
  497. * _enable_clocks - enable hwmod main clock and interface clocks
  498. * @oh: struct omap_hwmod *
  499. *
  500. * Enables all clocks necessary for register reads and writes to succeed
  501. * on the hwmod @oh. Returns 0.
  502. */
  503. static int _enable_clocks(struct omap_hwmod *oh)
  504. {
  505. int i;
  506. pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
  507. if (oh->_clk)
  508. clk_enable(oh->_clk);
  509. if (oh->slaves_cnt > 0) {
  510. for (i = 0; i < oh->slaves_cnt; i++) {
  511. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  512. struct clk *c = os->_clk;
  513. if (c && (os->flags & OCPIF_SWSUP_IDLE))
  514. clk_enable(c);
  515. }
  516. }
  517. /* The opt clocks are controlled by the device driver. */
  518. return 0;
  519. }
  520. /**
  521. * _disable_clocks - disable hwmod main clock and interface clocks
  522. * @oh: struct omap_hwmod *
  523. *
  524. * Disables the hwmod @oh main functional and interface clocks. Returns 0.
  525. */
  526. static int _disable_clocks(struct omap_hwmod *oh)
  527. {
  528. int i;
  529. pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
  530. if (oh->_clk)
  531. clk_disable(oh->_clk);
  532. if (oh->slaves_cnt > 0) {
  533. for (i = 0; i < oh->slaves_cnt; i++) {
  534. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  535. struct clk *c = os->_clk;
  536. if (c && (os->flags & OCPIF_SWSUP_IDLE))
  537. clk_disable(c);
  538. }
  539. }
  540. /* The opt clocks are controlled by the device driver. */
  541. return 0;
  542. }
  543. static void _enable_optional_clocks(struct omap_hwmod *oh)
  544. {
  545. struct omap_hwmod_opt_clk *oc;
  546. int i;
  547. pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
  548. for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
  549. if (oc->_clk) {
  550. pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
  551. oc->_clk->name);
  552. clk_enable(oc->_clk);
  553. }
  554. }
  555. static void _disable_optional_clocks(struct omap_hwmod *oh)
  556. {
  557. struct omap_hwmod_opt_clk *oc;
  558. int i;
  559. pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
  560. for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
  561. if (oc->_clk) {
  562. pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
  563. oc->_clk->name);
  564. clk_disable(oc->_clk);
  565. }
  566. }
  567. /**
  568. * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
  569. * @oh: struct omap_hwmod *
  570. *
  571. * Returns the array index of the OCP slave port that the MPU
  572. * addresses the device on, or -EINVAL upon error or not found.
  573. */
  574. static int _find_mpu_port_index(struct omap_hwmod *oh)
  575. {
  576. int i;
  577. int found = 0;
  578. if (!oh || oh->slaves_cnt == 0)
  579. return -EINVAL;
  580. for (i = 0; i < oh->slaves_cnt; i++) {
  581. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  582. if (os->user & OCP_USER_MPU) {
  583. found = 1;
  584. break;
  585. }
  586. }
  587. if (found)
  588. pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
  589. oh->name, i);
  590. else
  591. pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
  592. oh->name);
  593. return (found) ? i : -EINVAL;
  594. }
  595. /**
  596. * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
  597. * @oh: struct omap_hwmod *
  598. *
  599. * Return the virtual address of the base of the register target of
  600. * device @oh, or NULL on error.
  601. */
  602. static void __iomem *_find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
  603. {
  604. struct omap_hwmod_ocp_if *os;
  605. struct omap_hwmod_addr_space *mem;
  606. int i;
  607. int found = 0;
  608. void __iomem *va_start;
  609. if (!oh || oh->slaves_cnt == 0)
  610. return NULL;
  611. os = oh->slaves[index];
  612. for (i = 0, mem = os->addr; i < os->addr_cnt; i++, mem++) {
  613. if (mem->flags & ADDR_TYPE_RT) {
  614. found = 1;
  615. break;
  616. }
  617. }
  618. if (found) {
  619. va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
  620. if (!va_start) {
  621. pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
  622. return NULL;
  623. }
  624. pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
  625. oh->name, va_start);
  626. } else {
  627. pr_debug("omap_hwmod: %s: no MPU register target found\n",
  628. oh->name);
  629. }
  630. return (found) ? va_start : NULL;
  631. }
  632. /**
  633. * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
  634. * @oh: struct omap_hwmod *
  635. *
  636. * If module is marked as SWSUP_SIDLE, force the module out of slave
  637. * idle; otherwise, configure it for smart-idle. If module is marked
  638. * as SWSUP_MSUSPEND, force the module out of master standby;
  639. * otherwise, configure it for smart-standby. No return value.
  640. */
  641. static void _enable_sysc(struct omap_hwmod *oh)
  642. {
  643. u8 idlemode, sf;
  644. u32 v;
  645. if (!oh->class->sysc)
  646. return;
  647. v = oh->_sysc_cache;
  648. sf = oh->class->sysc->sysc_flags;
  649. if (sf & SYSC_HAS_SIDLEMODE) {
  650. idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
  651. HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
  652. _set_slave_idlemode(oh, idlemode, &v);
  653. }
  654. if (sf & SYSC_HAS_MIDLEMODE) {
  655. idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
  656. HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
  657. _set_master_standbymode(oh, idlemode, &v);
  658. }
  659. /*
  660. * XXX The clock framework should handle this, by
  661. * calling into this code. But this must wait until the
  662. * clock structures are tagged with omap_hwmod entries
  663. */
  664. if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
  665. (sf & SYSC_HAS_CLOCKACTIVITY))
  666. _set_clockactivity(oh, oh->class->sysc->clockact, &v);
  667. _write_sysconfig(v, oh);
  668. /* If slave is in SMARTIDLE, also enable wakeup */
  669. if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
  670. _enable_wakeup(oh);
  671. /*
  672. * Set the autoidle bit only after setting the smartidle bit
  673. * Setting this will not have any impact on the other modules.
  674. */
  675. if (sf & SYSC_HAS_AUTOIDLE) {
  676. idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
  677. 0 : 1;
  678. _set_module_autoidle(oh, idlemode, &v);
  679. _write_sysconfig(v, oh);
  680. }
  681. }
  682. /**
  683. * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
  684. * @oh: struct omap_hwmod *
  685. *
  686. * If module is marked as SWSUP_SIDLE, force the module into slave
  687. * idle; otherwise, configure it for smart-idle. If module is marked
  688. * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
  689. * configure it for smart-standby. No return value.
  690. */
  691. static void _idle_sysc(struct omap_hwmod *oh)
  692. {
  693. u8 idlemode, sf;
  694. u32 v;
  695. if (!oh->class->sysc)
  696. return;
  697. v = oh->_sysc_cache;
  698. sf = oh->class->sysc->sysc_flags;
  699. if (sf & SYSC_HAS_SIDLEMODE) {
  700. idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
  701. HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
  702. _set_slave_idlemode(oh, idlemode, &v);
  703. }
  704. if (sf & SYSC_HAS_MIDLEMODE) {
  705. idlemode = (oh->flags & HWMOD_SWSUP_MSTANDBY) ?
  706. HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
  707. _set_master_standbymode(oh, idlemode, &v);
  708. }
  709. _write_sysconfig(v, oh);
  710. }
  711. /**
  712. * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
  713. * @oh: struct omap_hwmod *
  714. *
  715. * Force the module into slave idle and master suspend. No return
  716. * value.
  717. */
  718. static void _shutdown_sysc(struct omap_hwmod *oh)
  719. {
  720. u32 v;
  721. u8 sf;
  722. if (!oh->class->sysc)
  723. return;
  724. v = oh->_sysc_cache;
  725. sf = oh->class->sysc->sysc_flags;
  726. if (sf & SYSC_HAS_SIDLEMODE)
  727. _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
  728. if (sf & SYSC_HAS_MIDLEMODE)
  729. _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
  730. if (sf & SYSC_HAS_AUTOIDLE)
  731. _set_module_autoidle(oh, 1, &v);
  732. _write_sysconfig(v, oh);
  733. }
  734. /**
  735. * _lookup - find an omap_hwmod by name
  736. * @name: find an omap_hwmod by name
  737. *
  738. * Return a pointer to an omap_hwmod by name, or NULL if not found.
  739. * Caller must hold omap_hwmod_mutex.
  740. */
  741. static struct omap_hwmod *_lookup(const char *name)
  742. {
  743. struct omap_hwmod *oh, *temp_oh;
  744. oh = NULL;
  745. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  746. if (!strcmp(name, temp_oh->name)) {
  747. oh = temp_oh;
  748. break;
  749. }
  750. }
  751. return oh;
  752. }
  753. /**
  754. * _init_clocks - clk_get() all clocks associated with this hwmod
  755. * @oh: struct omap_hwmod *
  756. * @data: not used; pass NULL
  757. *
  758. * Called by omap_hwmod_late_init() (after omap2_clk_init()).
  759. * Resolves all clock names embedded in the hwmod. Returns -EINVAL if
  760. * the omap_hwmod has not yet been registered or if the clocks have
  761. * already been initialized, 0 on success, or a non-zero error on
  762. * failure.
  763. */
  764. static int _init_clocks(struct omap_hwmod *oh, void *data)
  765. {
  766. int ret = 0;
  767. if (!oh || (oh->_state != _HWMOD_STATE_REGISTERED))
  768. return -EINVAL;
  769. pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
  770. ret |= _init_main_clk(oh);
  771. ret |= _init_interface_clks(oh);
  772. ret |= _init_opt_clks(oh);
  773. if (!ret)
  774. oh->_state = _HWMOD_STATE_CLKS_INITED;
  775. return 0;
  776. }
  777. /**
  778. * _wait_target_ready - wait for a module to leave slave idle
  779. * @oh: struct omap_hwmod *
  780. *
  781. * Wait for a module @oh to leave slave idle. Returns 0 if the module
  782. * does not have an IDLEST bit or if the module successfully leaves
  783. * slave idle; otherwise, pass along the return value of the
  784. * appropriate *_cm_wait_module_ready() function.
  785. */
  786. static int _wait_target_ready(struct omap_hwmod *oh)
  787. {
  788. struct omap_hwmod_ocp_if *os;
  789. int ret;
  790. if (!oh)
  791. return -EINVAL;
  792. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  793. return 0;
  794. os = oh->slaves[oh->_mpu_port_index];
  795. if (oh->flags & HWMOD_NO_IDLEST)
  796. return 0;
  797. /* XXX check module SIDLEMODE */
  798. /* XXX check clock enable states */
  799. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  800. ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
  801. oh->prcm.omap2.idlest_reg_id,
  802. oh->prcm.omap2.idlest_idle_bit);
  803. } else if (cpu_is_omap44xx()) {
  804. ret = omap4_cm_wait_module_ready(oh->prcm.omap4.clkctrl_reg);
  805. } else {
  806. BUG();
  807. };
  808. return ret;
  809. }
  810. /**
  811. * _lookup_hardreset - return the register bit shift for this hwmod/reset line
  812. * @oh: struct omap_hwmod *
  813. * @name: name of the reset line in the context of this hwmod
  814. *
  815. * Return the bit position of the reset line that match the
  816. * input name. Return -ENOENT if not found.
  817. */
  818. static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name)
  819. {
  820. int i;
  821. for (i = 0; i < oh->rst_lines_cnt; i++) {
  822. const char *rst_line = oh->rst_lines[i].name;
  823. if (!strcmp(rst_line, name)) {
  824. u8 shift = oh->rst_lines[i].rst_shift;
  825. pr_debug("omap_hwmod: %s: _lookup_hardreset: %s: %d\n",
  826. oh->name, rst_line, shift);
  827. return shift;
  828. }
  829. }
  830. return -ENOENT;
  831. }
  832. /**
  833. * _assert_hardreset - assert the HW reset line of submodules
  834. * contained in the hwmod module.
  835. * @oh: struct omap_hwmod *
  836. * @name: name of the reset line to lookup and assert
  837. *
  838. * Some IP like dsp, ipu or iva contain processor that require
  839. * an HW reset line to be assert / deassert in order to enable fully
  840. * the IP.
  841. */
  842. static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
  843. {
  844. u8 shift;
  845. if (!oh)
  846. return -EINVAL;
  847. shift = _lookup_hardreset(oh, name);
  848. if (IS_ERR_VALUE(shift))
  849. return shift;
  850. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  851. return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
  852. shift);
  853. else if (cpu_is_omap44xx())
  854. return omap4_prm_assert_hardreset(oh->prcm.omap4.rstctrl_reg,
  855. shift);
  856. else
  857. return -EINVAL;
  858. }
  859. /**
  860. * _deassert_hardreset - deassert the HW reset line of submodules contained
  861. * in the hwmod module.
  862. * @oh: struct omap_hwmod *
  863. * @name: name of the reset line to look up and deassert
  864. *
  865. * Some IP like dsp, ipu or iva contain processor that require
  866. * an HW reset line to be assert / deassert in order to enable fully
  867. * the IP.
  868. */
  869. static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
  870. {
  871. u8 shift;
  872. int r;
  873. if (!oh)
  874. return -EINVAL;
  875. shift = _lookup_hardreset(oh, name);
  876. if (IS_ERR_VALUE(shift))
  877. return shift;
  878. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  879. r = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
  880. shift);
  881. else if (cpu_is_omap44xx())
  882. r = omap4_prm_deassert_hardreset(oh->prcm.omap4.rstctrl_reg,
  883. shift);
  884. else
  885. return -EINVAL;
  886. if (r == -EBUSY)
  887. pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
  888. return r;
  889. }
  890. /**
  891. * _read_hardreset - read the HW reset line state of submodules
  892. * contained in the hwmod module
  893. * @oh: struct omap_hwmod *
  894. * @name: name of the reset line to look up and read
  895. *
  896. * Return the state of the reset line.
  897. */
  898. static int _read_hardreset(struct omap_hwmod *oh, const char *name)
  899. {
  900. u8 shift;
  901. if (!oh)
  902. return -EINVAL;
  903. shift = _lookup_hardreset(oh, name);
  904. if (IS_ERR_VALUE(shift))
  905. return shift;
  906. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  907. return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
  908. shift);
  909. } else if (cpu_is_omap44xx()) {
  910. return omap4_prm_is_hardreset_asserted(oh->prcm.omap4.rstctrl_reg,
  911. shift);
  912. } else {
  913. return -EINVAL;
  914. }
  915. }
  916. /**
  917. * _reset - reset an omap_hwmod
  918. * @oh: struct omap_hwmod *
  919. *
  920. * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
  921. * enabled for this to work. Returns -EINVAL if the hwmod cannot be
  922. * reset this way or if the hwmod is in the wrong state, -ETIMEDOUT if
  923. * the module did not reset in time, or 0 upon success.
  924. *
  925. * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
  926. * Starting in OMAP4, some IPs does not have SYSSTATUS register and instead
  927. * use the SYSCONFIG softreset bit to provide the status.
  928. *
  929. * Note that some IP like McBSP does have a reset control but no reset status.
  930. */
  931. static int _reset(struct omap_hwmod *oh)
  932. {
  933. u32 v;
  934. int c = 0;
  935. int ret = 0;
  936. if (!oh->class->sysc ||
  937. !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
  938. return -EINVAL;
  939. /* clocks must be on for this operation */
  940. if (oh->_state != _HWMOD_STATE_ENABLED) {
  941. pr_warning("omap_hwmod: %s: reset can only be entered from "
  942. "enabled state\n", oh->name);
  943. return -EINVAL;
  944. }
  945. /* For some modules, all optionnal clocks need to be enabled as well */
  946. if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
  947. _enable_optional_clocks(oh);
  948. pr_debug("omap_hwmod: %s: resetting\n", oh->name);
  949. v = oh->_sysc_cache;
  950. ret = _set_softreset(oh, &v);
  951. if (ret)
  952. goto dis_opt_clks;
  953. _write_sysconfig(v, oh);
  954. if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
  955. omap_test_timeout((omap_hwmod_read(oh,
  956. oh->class->sysc->syss_offs)
  957. & SYSS_RESETDONE_MASK),
  958. MAX_MODULE_SOFTRESET_WAIT, c);
  959. else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS)
  960. omap_test_timeout(!(omap_hwmod_read(oh,
  961. oh->class->sysc->sysc_offs)
  962. & SYSC_TYPE2_SOFTRESET_MASK),
  963. MAX_MODULE_SOFTRESET_WAIT, c);
  964. if (c == MAX_MODULE_SOFTRESET_WAIT)
  965. pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
  966. oh->name, MAX_MODULE_SOFTRESET_WAIT);
  967. else
  968. pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
  969. /*
  970. * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
  971. * _wait_target_ready() or _reset()
  972. */
  973. ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
  974. dis_opt_clks:
  975. if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
  976. _disable_optional_clocks(oh);
  977. return ret;
  978. }
  979. /**
  980. * _omap_hwmod_enable - enable an omap_hwmod
  981. * @oh: struct omap_hwmod *
  982. *
  983. * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
  984. * register target. (This function has a full name --
  985. * _omap_hwmod_enable() rather than simply _enable() -- because it is
  986. * currently required by the pm34xx.c idle loop.) Returns -EINVAL if
  987. * the hwmod is in the wrong state or passes along the return value of
  988. * _wait_target_ready().
  989. */
  990. int _omap_hwmod_enable(struct omap_hwmod *oh)
  991. {
  992. int r;
  993. if (oh->_state != _HWMOD_STATE_INITIALIZED &&
  994. oh->_state != _HWMOD_STATE_IDLE &&
  995. oh->_state != _HWMOD_STATE_DISABLED) {
  996. WARN(1, "omap_hwmod: %s: enabled state can only be entered "
  997. "from initialized, idle, or disabled state\n", oh->name);
  998. return -EINVAL;
  999. }
  1000. pr_debug("omap_hwmod: %s: enabling\n", oh->name);
  1001. /*
  1002. * If an IP contains only one HW reset line, then de-assert it in order
  1003. * to allow to enable the clocks. Otherwise the PRCM will return
  1004. * Intransition status, and the init will failed.
  1005. */
  1006. if ((oh->_state == _HWMOD_STATE_INITIALIZED ||
  1007. oh->_state == _HWMOD_STATE_DISABLED) && oh->rst_lines_cnt == 1)
  1008. _deassert_hardreset(oh, oh->rst_lines[0].name);
  1009. /* XXX mux balls */
  1010. _add_initiator_dep(oh, mpu_oh);
  1011. _enable_clocks(oh);
  1012. r = _wait_target_ready(oh);
  1013. if (!r) {
  1014. oh->_state = _HWMOD_STATE_ENABLED;
  1015. /* Access the sysconfig only if the target is ready */
  1016. if (oh->class->sysc) {
  1017. if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
  1018. _update_sysc_cache(oh);
  1019. _enable_sysc(oh);
  1020. }
  1021. } else {
  1022. pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
  1023. oh->name, r);
  1024. }
  1025. return r;
  1026. }
  1027. /**
  1028. * _omap_hwmod_idle - idle an omap_hwmod
  1029. * @oh: struct omap_hwmod *
  1030. *
  1031. * Idles an omap_hwmod @oh. This should be called once the hwmod has
  1032. * no further work. (This function has a full name --
  1033. * _omap_hwmod_idle() rather than simply _idle() -- because it is
  1034. * currently required by the pm34xx.c idle loop.) Returns -EINVAL if
  1035. * the hwmod is in the wrong state or returns 0.
  1036. */
  1037. int _omap_hwmod_idle(struct omap_hwmod *oh)
  1038. {
  1039. if (oh->_state != _HWMOD_STATE_ENABLED) {
  1040. WARN(1, "omap_hwmod: %s: idle state can only be entered from "
  1041. "enabled state\n", oh->name);
  1042. return -EINVAL;
  1043. }
  1044. pr_debug("omap_hwmod: %s: idling\n", oh->name);
  1045. if (oh->class->sysc)
  1046. _idle_sysc(oh);
  1047. _del_initiator_dep(oh, mpu_oh);
  1048. _disable_clocks(oh);
  1049. oh->_state = _HWMOD_STATE_IDLE;
  1050. return 0;
  1051. }
  1052. /**
  1053. * _shutdown - shutdown an omap_hwmod
  1054. * @oh: struct omap_hwmod *
  1055. *
  1056. * Shut down an omap_hwmod @oh. This should be called when the driver
  1057. * used for the hwmod is removed or unloaded or if the driver is not
  1058. * used by the system. Returns -EINVAL if the hwmod is in the wrong
  1059. * state or returns 0.
  1060. */
  1061. static int _shutdown(struct omap_hwmod *oh)
  1062. {
  1063. int ret;
  1064. u8 prev_state;
  1065. if (oh->_state != _HWMOD_STATE_IDLE &&
  1066. oh->_state != _HWMOD_STATE_ENABLED) {
  1067. WARN(1, "omap_hwmod: %s: disabled state can only be entered "
  1068. "from idle, or enabled state\n", oh->name);
  1069. return -EINVAL;
  1070. }
  1071. pr_debug("omap_hwmod: %s: disabling\n", oh->name);
  1072. if (oh->class->pre_shutdown) {
  1073. prev_state = oh->_state;
  1074. if (oh->_state == _HWMOD_STATE_IDLE)
  1075. _omap_hwmod_enable(oh);
  1076. ret = oh->class->pre_shutdown(oh);
  1077. if (ret) {
  1078. if (prev_state == _HWMOD_STATE_IDLE)
  1079. _omap_hwmod_idle(oh);
  1080. return ret;
  1081. }
  1082. }
  1083. if (oh->class->sysc)
  1084. _shutdown_sysc(oh);
  1085. /*
  1086. * If an IP contains only one HW reset line, then assert it
  1087. * before disabling the clocks and shutting down the IP.
  1088. */
  1089. if (oh->rst_lines_cnt == 1)
  1090. _assert_hardreset(oh, oh->rst_lines[0].name);
  1091. /* clocks and deps are already disabled in idle */
  1092. if (oh->_state == _HWMOD_STATE_ENABLED) {
  1093. _del_initiator_dep(oh, mpu_oh);
  1094. /* XXX what about the other system initiators here? dma, dsp */
  1095. _disable_clocks(oh);
  1096. }
  1097. /* XXX Should this code also force-disable the optional clocks? */
  1098. /* XXX mux any associated balls to safe mode */
  1099. oh->_state = _HWMOD_STATE_DISABLED;
  1100. return 0;
  1101. }
  1102. /**
  1103. * _setup - do initial configuration of omap_hwmod
  1104. * @oh: struct omap_hwmod *
  1105. *
  1106. * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
  1107. * OCP_SYSCONFIG register. Returns -EINVAL if the hwmod is in the
  1108. * wrong state or returns 0.
  1109. */
  1110. static int _setup(struct omap_hwmod *oh, void *data)
  1111. {
  1112. int i, r;
  1113. u8 postsetup_state;
  1114. /* Set iclk autoidle mode */
  1115. if (oh->slaves_cnt > 0) {
  1116. for (i = 0; i < oh->slaves_cnt; i++) {
  1117. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  1118. struct clk *c = os->_clk;
  1119. if (!c)
  1120. continue;
  1121. if (os->flags & OCPIF_SWSUP_IDLE) {
  1122. /* XXX omap_iclk_deny_idle(c); */
  1123. } else {
  1124. /* XXX omap_iclk_allow_idle(c); */
  1125. clk_enable(c);
  1126. }
  1127. }
  1128. }
  1129. oh->_state = _HWMOD_STATE_INITIALIZED;
  1130. /*
  1131. * In the case of hwmod with hardreset that should not be
  1132. * de-assert at boot time, we have to keep the module
  1133. * initialized, because we cannot enable it properly with the
  1134. * reset asserted. Exit without warning because that behavior is
  1135. * expected.
  1136. */
  1137. if ((oh->flags & HWMOD_INIT_NO_RESET) && oh->rst_lines_cnt == 1)
  1138. return 0;
  1139. r = _omap_hwmod_enable(oh);
  1140. if (r) {
  1141. pr_warning("omap_hwmod: %s: cannot be enabled (%d)\n",
  1142. oh->name, oh->_state);
  1143. return 0;
  1144. }
  1145. if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
  1146. _reset(oh);
  1147. /*
  1148. * OCP_SYSCONFIG bits need to be reprogrammed after a softreset.
  1149. * The _omap_hwmod_enable() function should be split to
  1150. * avoid the rewrite of the OCP_SYSCONFIG register.
  1151. */
  1152. if (oh->class->sysc) {
  1153. _update_sysc_cache(oh);
  1154. _enable_sysc(oh);
  1155. }
  1156. }
  1157. postsetup_state = oh->_postsetup_state;
  1158. if (postsetup_state == _HWMOD_STATE_UNKNOWN)
  1159. postsetup_state = _HWMOD_STATE_ENABLED;
  1160. /*
  1161. * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
  1162. * it should be set by the core code as a runtime flag during startup
  1163. */
  1164. if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
  1165. (postsetup_state == _HWMOD_STATE_IDLE))
  1166. postsetup_state = _HWMOD_STATE_ENABLED;
  1167. if (postsetup_state == _HWMOD_STATE_IDLE)
  1168. _omap_hwmod_idle(oh);
  1169. else if (postsetup_state == _HWMOD_STATE_DISABLED)
  1170. _shutdown(oh);
  1171. else if (postsetup_state != _HWMOD_STATE_ENABLED)
  1172. WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
  1173. oh->name, postsetup_state);
  1174. return 0;
  1175. }
  1176. /* Public functions */
  1177. u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
  1178. {
  1179. if (oh->flags & HWMOD_16BIT_REG)
  1180. return __raw_readw(oh->_mpu_rt_va + reg_offs);
  1181. else
  1182. return __raw_readl(oh->_mpu_rt_va + reg_offs);
  1183. }
  1184. void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
  1185. {
  1186. if (oh->flags & HWMOD_16BIT_REG)
  1187. __raw_writew(v, oh->_mpu_rt_va + reg_offs);
  1188. else
  1189. __raw_writel(v, oh->_mpu_rt_va + reg_offs);
  1190. }
  1191. /**
  1192. * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
  1193. * @oh: struct omap_hwmod *
  1194. * @idlemode: SIDLEMODE field bits (shifted to bit 0)
  1195. *
  1196. * Sets the IP block's OCP slave idlemode in hardware, and updates our
  1197. * local copy. Intended to be used by drivers that have some erratum
  1198. * that requires direct manipulation of the SIDLEMODE bits. Returns
  1199. * -EINVAL if @oh is null, or passes along the return value from
  1200. * _set_slave_idlemode().
  1201. *
  1202. * XXX Does this function have any current users? If not, we should
  1203. * remove it; it is better to let the rest of the hwmod code handle this.
  1204. * Any users of this function should be scrutinized carefully.
  1205. */
  1206. int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
  1207. {
  1208. u32 v;
  1209. int retval = 0;
  1210. if (!oh)
  1211. return -EINVAL;
  1212. v = oh->_sysc_cache;
  1213. retval = _set_slave_idlemode(oh, idlemode, &v);
  1214. if (!retval)
  1215. _write_sysconfig(v, oh);
  1216. return retval;
  1217. }
  1218. /**
  1219. * omap_hwmod_register - register a struct omap_hwmod
  1220. * @oh: struct omap_hwmod *
  1221. *
  1222. * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
  1223. * already has been registered by the same name; -EINVAL if the
  1224. * omap_hwmod is in the wrong state, if @oh is NULL, if the
  1225. * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
  1226. * name, or if the omap_hwmod's class is missing a name; or 0 upon
  1227. * success.
  1228. *
  1229. * XXX The data should be copied into bootmem, so the original data
  1230. * should be marked __initdata and freed after init. This would allow
  1231. * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
  1232. * that the copy process would be relatively complex due to the large number
  1233. * of substructures.
  1234. */
  1235. int omap_hwmod_register(struct omap_hwmod *oh)
  1236. {
  1237. int ret, ms_id;
  1238. if (!oh || !oh->name || !oh->class || !oh->class->name ||
  1239. (oh->_state != _HWMOD_STATE_UNKNOWN))
  1240. return -EINVAL;
  1241. mutex_lock(&omap_hwmod_mutex);
  1242. pr_debug("omap_hwmod: %s: registering\n", oh->name);
  1243. if (_lookup(oh->name)) {
  1244. ret = -EEXIST;
  1245. goto ohr_unlock;
  1246. }
  1247. ms_id = _find_mpu_port_index(oh);
  1248. if (!IS_ERR_VALUE(ms_id)) {
  1249. oh->_mpu_port_index = ms_id;
  1250. oh->_mpu_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
  1251. } else {
  1252. oh->_int_flags |= _HWMOD_NO_MPU_PORT;
  1253. }
  1254. list_add_tail(&oh->node, &omap_hwmod_list);
  1255. mutex_init(&oh->_mutex);
  1256. oh->_state = _HWMOD_STATE_REGISTERED;
  1257. ret = 0;
  1258. ohr_unlock:
  1259. mutex_unlock(&omap_hwmod_mutex);
  1260. return ret;
  1261. }
  1262. /**
  1263. * omap_hwmod_lookup - look up a registered omap_hwmod by name
  1264. * @name: name of the omap_hwmod to look up
  1265. *
  1266. * Given a @name of an omap_hwmod, return a pointer to the registered
  1267. * struct omap_hwmod *, or NULL upon error.
  1268. */
  1269. struct omap_hwmod *omap_hwmod_lookup(const char *name)
  1270. {
  1271. struct omap_hwmod *oh;
  1272. if (!name)
  1273. return NULL;
  1274. mutex_lock(&omap_hwmod_mutex);
  1275. oh = _lookup(name);
  1276. mutex_unlock(&omap_hwmod_mutex);
  1277. return oh;
  1278. }
  1279. /**
  1280. * omap_hwmod_for_each - call function for each registered omap_hwmod
  1281. * @fn: pointer to a callback function
  1282. * @data: void * data to pass to callback function
  1283. *
  1284. * Call @fn for each registered omap_hwmod, passing @data to each
  1285. * function. @fn must return 0 for success or any other value for
  1286. * failure. If @fn returns non-zero, the iteration across omap_hwmods
  1287. * will stop and the non-zero return value will be passed to the
  1288. * caller of omap_hwmod_for_each(). @fn is called with
  1289. * omap_hwmod_for_each() held.
  1290. */
  1291. int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
  1292. void *data)
  1293. {
  1294. struct omap_hwmod *temp_oh;
  1295. int ret;
  1296. if (!fn)
  1297. return -EINVAL;
  1298. mutex_lock(&omap_hwmod_mutex);
  1299. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  1300. ret = (*fn)(temp_oh, data);
  1301. if (ret)
  1302. break;
  1303. }
  1304. mutex_unlock(&omap_hwmod_mutex);
  1305. return ret;
  1306. }
  1307. /**
  1308. * omap_hwmod_init - init omap_hwmod code and register hwmods
  1309. * @ohs: pointer to an array of omap_hwmods to register
  1310. *
  1311. * Intended to be called early in boot before the clock framework is
  1312. * initialized. If @ohs is not null, will register all omap_hwmods
  1313. * listed in @ohs that are valid for this chip. Returns -EINVAL if
  1314. * omap_hwmod_init() has already been called or 0 otherwise.
  1315. */
  1316. int omap_hwmod_init(struct omap_hwmod **ohs)
  1317. {
  1318. struct omap_hwmod *oh;
  1319. int r;
  1320. if (inited)
  1321. return -EINVAL;
  1322. inited = 1;
  1323. if (!ohs)
  1324. return 0;
  1325. oh = *ohs;
  1326. while (oh) {
  1327. if (omap_chip_is(oh->omap_chip)) {
  1328. r = omap_hwmod_register(oh);
  1329. WARN(r, "omap_hwmod: %s: omap_hwmod_register returned "
  1330. "%d\n", oh->name, r);
  1331. }
  1332. oh = *++ohs;
  1333. }
  1334. return 0;
  1335. }
  1336. /**
  1337. * omap_hwmod_late_init - do some post-clock framework initialization
  1338. *
  1339. * Must be called after omap2_clk_init(). Resolves the struct clk names
  1340. * to struct clk pointers for each registered omap_hwmod. Also calls
  1341. * _setup() on each hwmod. Returns 0.
  1342. */
  1343. int omap_hwmod_late_init(void)
  1344. {
  1345. int r;
  1346. /* XXX check return value */
  1347. r = omap_hwmod_for_each(_init_clocks, NULL);
  1348. WARN(r, "omap_hwmod: omap_hwmod_late_init(): _init_clocks failed\n");
  1349. mpu_oh = omap_hwmod_lookup(MPU_INITIATOR_NAME);
  1350. WARN(!mpu_oh, "omap_hwmod: could not find MPU initiator hwmod %s\n",
  1351. MPU_INITIATOR_NAME);
  1352. omap_hwmod_for_each(_setup, NULL);
  1353. return 0;
  1354. }
  1355. /**
  1356. * omap_hwmod_unregister - unregister an omap_hwmod
  1357. * @oh: struct omap_hwmod *
  1358. *
  1359. * Unregisters a previously-registered omap_hwmod @oh. There's probably
  1360. * no use case for this, so it is likely to be removed in a later version.
  1361. *
  1362. * XXX Free all of the bootmem-allocated structures here when that is
  1363. * implemented. Make it clear that core code is the only code that is
  1364. * expected to unregister modules.
  1365. */
  1366. int omap_hwmod_unregister(struct omap_hwmod *oh)
  1367. {
  1368. if (!oh)
  1369. return -EINVAL;
  1370. pr_debug("omap_hwmod: %s: unregistering\n", oh->name);
  1371. mutex_lock(&omap_hwmod_mutex);
  1372. iounmap(oh->_mpu_rt_va);
  1373. list_del(&oh->node);
  1374. mutex_unlock(&omap_hwmod_mutex);
  1375. return 0;
  1376. }
  1377. /**
  1378. * omap_hwmod_enable - enable an omap_hwmod
  1379. * @oh: struct omap_hwmod *
  1380. *
  1381. * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
  1382. * Returns -EINVAL on error or passes along the return value from _enable().
  1383. */
  1384. int omap_hwmod_enable(struct omap_hwmod *oh)
  1385. {
  1386. int r;
  1387. if (!oh)
  1388. return -EINVAL;
  1389. mutex_lock(&oh->_mutex);
  1390. r = _omap_hwmod_enable(oh);
  1391. mutex_unlock(&oh->_mutex);
  1392. return r;
  1393. }
  1394. /**
  1395. * omap_hwmod_idle - idle an omap_hwmod
  1396. * @oh: struct omap_hwmod *
  1397. *
  1398. * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
  1399. * Returns -EINVAL on error or passes along the return value from _idle().
  1400. */
  1401. int omap_hwmod_idle(struct omap_hwmod *oh)
  1402. {
  1403. if (!oh)
  1404. return -EINVAL;
  1405. mutex_lock(&oh->_mutex);
  1406. _omap_hwmod_idle(oh);
  1407. mutex_unlock(&oh->_mutex);
  1408. return 0;
  1409. }
  1410. /**
  1411. * omap_hwmod_shutdown - shutdown an omap_hwmod
  1412. * @oh: struct omap_hwmod *
  1413. *
  1414. * Shutdown an omap_hwmod @oh. Intended to be called by
  1415. * omap_device_shutdown(). Returns -EINVAL on error or passes along
  1416. * the return value from _shutdown().
  1417. */
  1418. int omap_hwmod_shutdown(struct omap_hwmod *oh)
  1419. {
  1420. if (!oh)
  1421. return -EINVAL;
  1422. mutex_lock(&oh->_mutex);
  1423. _shutdown(oh);
  1424. mutex_unlock(&oh->_mutex);
  1425. return 0;
  1426. }
  1427. /**
  1428. * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
  1429. * @oh: struct omap_hwmod *oh
  1430. *
  1431. * Intended to be called by the omap_device code.
  1432. */
  1433. int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
  1434. {
  1435. mutex_lock(&oh->_mutex);
  1436. _enable_clocks(oh);
  1437. mutex_unlock(&oh->_mutex);
  1438. return 0;
  1439. }
  1440. /**
  1441. * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
  1442. * @oh: struct omap_hwmod *oh
  1443. *
  1444. * Intended to be called by the omap_device code.
  1445. */
  1446. int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
  1447. {
  1448. mutex_lock(&oh->_mutex);
  1449. _disable_clocks(oh);
  1450. mutex_unlock(&oh->_mutex);
  1451. return 0;
  1452. }
  1453. /**
  1454. * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
  1455. * @oh: struct omap_hwmod *oh
  1456. *
  1457. * Intended to be called by drivers and core code when all posted
  1458. * writes to a device must complete before continuing further
  1459. * execution (for example, after clearing some device IRQSTATUS
  1460. * register bits)
  1461. *
  1462. * XXX what about targets with multiple OCP threads?
  1463. */
  1464. void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
  1465. {
  1466. BUG_ON(!oh);
  1467. if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
  1468. WARN(1, "omap_device: %s: OCP barrier impossible due to "
  1469. "device configuration\n", oh->name);
  1470. return;
  1471. }
  1472. /*
  1473. * Forces posted writes to complete on the OCP thread handling
  1474. * register writes
  1475. */
  1476. omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
  1477. }
  1478. /**
  1479. * omap_hwmod_reset - reset the hwmod
  1480. * @oh: struct omap_hwmod *
  1481. *
  1482. * Under some conditions, a driver may wish to reset the entire device.
  1483. * Called from omap_device code. Returns -EINVAL on error or passes along
  1484. * the return value from _reset().
  1485. */
  1486. int omap_hwmod_reset(struct omap_hwmod *oh)
  1487. {
  1488. int r;
  1489. if (!oh)
  1490. return -EINVAL;
  1491. mutex_lock(&oh->_mutex);
  1492. r = _reset(oh);
  1493. mutex_unlock(&oh->_mutex);
  1494. return r;
  1495. }
  1496. /**
  1497. * omap_hwmod_count_resources - count number of struct resources needed by hwmod
  1498. * @oh: struct omap_hwmod *
  1499. * @res: pointer to the first element of an array of struct resource to fill
  1500. *
  1501. * Count the number of struct resource array elements necessary to
  1502. * contain omap_hwmod @oh resources. Intended to be called by code
  1503. * that registers omap_devices. Intended to be used to determine the
  1504. * size of a dynamically-allocated struct resource array, before
  1505. * calling omap_hwmod_fill_resources(). Returns the number of struct
  1506. * resource array elements needed.
  1507. *
  1508. * XXX This code is not optimized. It could attempt to merge adjacent
  1509. * resource IDs.
  1510. *
  1511. */
  1512. int omap_hwmod_count_resources(struct omap_hwmod *oh)
  1513. {
  1514. int ret, i;
  1515. ret = oh->mpu_irqs_cnt + oh->sdma_reqs_cnt;
  1516. for (i = 0; i < oh->slaves_cnt; i++)
  1517. ret += oh->slaves[i]->addr_cnt;
  1518. return ret;
  1519. }
  1520. /**
  1521. * omap_hwmod_fill_resources - fill struct resource array with hwmod data
  1522. * @oh: struct omap_hwmod *
  1523. * @res: pointer to the first element of an array of struct resource to fill
  1524. *
  1525. * Fill the struct resource array @res with resource data from the
  1526. * omap_hwmod @oh. Intended to be called by code that registers
  1527. * omap_devices. See also omap_hwmod_count_resources(). Returns the
  1528. * number of array elements filled.
  1529. */
  1530. int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
  1531. {
  1532. int i, j;
  1533. int r = 0;
  1534. /* For each IRQ, DMA, memory area, fill in array.*/
  1535. for (i = 0; i < oh->mpu_irqs_cnt; i++) {
  1536. (res + r)->name = (oh->mpu_irqs + i)->name;
  1537. (res + r)->start = (oh->mpu_irqs + i)->irq;
  1538. (res + r)->end = (oh->mpu_irqs + i)->irq;
  1539. (res + r)->flags = IORESOURCE_IRQ;
  1540. r++;
  1541. }
  1542. for (i = 0; i < oh->sdma_reqs_cnt; i++) {
  1543. (res + r)->name = (oh->sdma_reqs + i)->name;
  1544. (res + r)->start = (oh->sdma_reqs + i)->dma_req;
  1545. (res + r)->end = (oh->sdma_reqs + i)->dma_req;
  1546. (res + r)->flags = IORESOURCE_DMA;
  1547. r++;
  1548. }
  1549. for (i = 0; i < oh->slaves_cnt; i++) {
  1550. struct omap_hwmod_ocp_if *os;
  1551. os = oh->slaves[i];
  1552. for (j = 0; j < os->addr_cnt; j++) {
  1553. (res + r)->start = (os->addr + j)->pa_start;
  1554. (res + r)->end = (os->addr + j)->pa_end;
  1555. (res + r)->flags = IORESOURCE_MEM;
  1556. r++;
  1557. }
  1558. }
  1559. return r;
  1560. }
  1561. /**
  1562. * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
  1563. * @oh: struct omap_hwmod *
  1564. *
  1565. * Return the powerdomain pointer associated with the OMAP module
  1566. * @oh's main clock. If @oh does not have a main clk, return the
  1567. * powerdomain associated with the interface clock associated with the
  1568. * module's MPU port. (XXX Perhaps this should use the SDMA port
  1569. * instead?) Returns NULL on error, or a struct powerdomain * on
  1570. * success.
  1571. */
  1572. struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
  1573. {
  1574. struct clk *c;
  1575. if (!oh)
  1576. return NULL;
  1577. if (oh->_clk) {
  1578. c = oh->_clk;
  1579. } else {
  1580. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  1581. return NULL;
  1582. c = oh->slaves[oh->_mpu_port_index]->_clk;
  1583. }
  1584. if (!c->clkdm)
  1585. return NULL;
  1586. return c->clkdm->pwrdm.ptr;
  1587. }
  1588. /**
  1589. * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
  1590. * @oh: struct omap_hwmod *
  1591. *
  1592. * Returns the virtual address corresponding to the beginning of the
  1593. * module's register target, in the address range that is intended to
  1594. * be used by the MPU. Returns the virtual address upon success or NULL
  1595. * upon error.
  1596. */
  1597. void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
  1598. {
  1599. if (!oh)
  1600. return NULL;
  1601. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  1602. return NULL;
  1603. if (oh->_state == _HWMOD_STATE_UNKNOWN)
  1604. return NULL;
  1605. return oh->_mpu_rt_va;
  1606. }
  1607. /**
  1608. * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
  1609. * @oh: struct omap_hwmod *
  1610. * @init_oh: struct omap_hwmod * (initiator)
  1611. *
  1612. * Add a sleep dependency between the initiator @init_oh and @oh.
  1613. * Intended to be called by DSP/Bridge code via platform_data for the
  1614. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  1615. * code needs to add/del initiator dependencies dynamically
  1616. * before/after accessing a device. Returns the return value from
  1617. * _add_initiator_dep().
  1618. *
  1619. * XXX Keep a usecount in the clockdomain code
  1620. */
  1621. int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
  1622. struct omap_hwmod *init_oh)
  1623. {
  1624. return _add_initiator_dep(oh, init_oh);
  1625. }
  1626. /*
  1627. * XXX what about functions for drivers to save/restore ocp_sysconfig
  1628. * for context save/restore operations?
  1629. */
  1630. /**
  1631. * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
  1632. * @oh: struct omap_hwmod *
  1633. * @init_oh: struct omap_hwmod * (initiator)
  1634. *
  1635. * Remove a sleep dependency between the initiator @init_oh and @oh.
  1636. * Intended to be called by DSP/Bridge code via platform_data for the
  1637. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  1638. * code needs to add/del initiator dependencies dynamically
  1639. * before/after accessing a device. Returns the return value from
  1640. * _del_initiator_dep().
  1641. *
  1642. * XXX Keep a usecount in the clockdomain code
  1643. */
  1644. int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
  1645. struct omap_hwmod *init_oh)
  1646. {
  1647. return _del_initiator_dep(oh, init_oh);
  1648. }
  1649. /**
  1650. * omap_hwmod_enable_wakeup - allow device to wake up the system
  1651. * @oh: struct omap_hwmod *
  1652. *
  1653. * Sets the module OCP socket ENAWAKEUP bit to allow the module to
  1654. * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
  1655. * registers to cause the PRCM to receive wakeup events from the
  1656. * module. Does not set any wakeup routing registers beyond this
  1657. * point - if the module is to wake up any other module or subsystem,
  1658. * that must be set separately. Called by omap_device code. Returns
  1659. * -EINVAL on error or 0 upon success.
  1660. */
  1661. int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
  1662. {
  1663. if (!oh->class->sysc ||
  1664. !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
  1665. return -EINVAL;
  1666. mutex_lock(&oh->_mutex);
  1667. _enable_wakeup(oh);
  1668. mutex_unlock(&oh->_mutex);
  1669. return 0;
  1670. }
  1671. /**
  1672. * omap_hwmod_disable_wakeup - prevent device from waking the system
  1673. * @oh: struct omap_hwmod *
  1674. *
  1675. * Clears the module OCP socket ENAWAKEUP bit to prevent the module
  1676. * from sending wakeups to the PRCM. Eventually this should clear
  1677. * PRCM wakeup registers to cause the PRCM to ignore wakeup events
  1678. * from the module. Does not set any wakeup routing registers beyond
  1679. * this point - if the module is to wake up any other module or
  1680. * subsystem, that must be set separately. Called by omap_device
  1681. * code. Returns -EINVAL on error or 0 upon success.
  1682. */
  1683. int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
  1684. {
  1685. if (!oh->class->sysc ||
  1686. !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
  1687. return -EINVAL;
  1688. mutex_lock(&oh->_mutex);
  1689. _disable_wakeup(oh);
  1690. mutex_unlock(&oh->_mutex);
  1691. return 0;
  1692. }
  1693. /**
  1694. * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
  1695. * contained in the hwmod module.
  1696. * @oh: struct omap_hwmod *
  1697. * @name: name of the reset line to lookup and assert
  1698. *
  1699. * Some IP like dsp, ipu or iva contain processor that require
  1700. * an HW reset line to be assert / deassert in order to enable fully
  1701. * the IP. Returns -EINVAL if @oh is null or if the operation is not
  1702. * yet supported on this OMAP; otherwise, passes along the return value
  1703. * from _assert_hardreset().
  1704. */
  1705. int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
  1706. {
  1707. int ret;
  1708. if (!oh)
  1709. return -EINVAL;
  1710. mutex_lock(&oh->_mutex);
  1711. ret = _assert_hardreset(oh, name);
  1712. mutex_unlock(&oh->_mutex);
  1713. return ret;
  1714. }
  1715. /**
  1716. * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
  1717. * contained in the hwmod module.
  1718. * @oh: struct omap_hwmod *
  1719. * @name: name of the reset line to look up and deassert
  1720. *
  1721. * Some IP like dsp, ipu or iva contain processor that require
  1722. * an HW reset line to be assert / deassert in order to enable fully
  1723. * the IP. Returns -EINVAL if @oh is null or if the operation is not
  1724. * yet supported on this OMAP; otherwise, passes along the return value
  1725. * from _deassert_hardreset().
  1726. */
  1727. int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
  1728. {
  1729. int ret;
  1730. if (!oh)
  1731. return -EINVAL;
  1732. mutex_lock(&oh->_mutex);
  1733. ret = _deassert_hardreset(oh, name);
  1734. mutex_unlock(&oh->_mutex);
  1735. return ret;
  1736. }
  1737. /**
  1738. * omap_hwmod_read_hardreset - read the HW reset line state of submodules
  1739. * contained in the hwmod module
  1740. * @oh: struct omap_hwmod *
  1741. * @name: name of the reset line to look up and read
  1742. *
  1743. * Return the current state of the hwmod @oh's reset line named @name:
  1744. * returns -EINVAL upon parameter error or if this operation
  1745. * is unsupported on the current OMAP; otherwise, passes along the return
  1746. * value from _read_hardreset().
  1747. */
  1748. int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
  1749. {
  1750. int ret;
  1751. if (!oh)
  1752. return -EINVAL;
  1753. mutex_lock(&oh->_mutex);
  1754. ret = _read_hardreset(oh, name);
  1755. mutex_unlock(&oh->_mutex);
  1756. return ret;
  1757. }
  1758. /**
  1759. * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
  1760. * @classname: struct omap_hwmod_class name to search for
  1761. * @fn: callback function pointer to call for each hwmod in class @classname
  1762. * @user: arbitrary context data to pass to the callback function
  1763. *
  1764. * For each omap_hwmod of class @classname, call @fn. Takes
  1765. * omap_hwmod_mutex to prevent the hwmod list from changing during the
  1766. * iteration. If the callback function returns something other than
  1767. * zero, the iterator is terminated, and the callback function's return
  1768. * value is passed back to the caller. Returns 0 upon success, -EINVAL
  1769. * if @classname or @fn are NULL, or passes back the error code from @fn.
  1770. */
  1771. int omap_hwmod_for_each_by_class(const char *classname,
  1772. int (*fn)(struct omap_hwmod *oh,
  1773. void *user),
  1774. void *user)
  1775. {
  1776. struct omap_hwmod *temp_oh;
  1777. int ret = 0;
  1778. if (!classname || !fn)
  1779. return -EINVAL;
  1780. pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
  1781. __func__, classname);
  1782. mutex_lock(&omap_hwmod_mutex);
  1783. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  1784. if (!strcmp(temp_oh->class->name, classname)) {
  1785. pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
  1786. __func__, temp_oh->name);
  1787. ret = (*fn)(temp_oh, user);
  1788. if (ret)
  1789. break;
  1790. }
  1791. }
  1792. mutex_unlock(&omap_hwmod_mutex);
  1793. if (ret)
  1794. pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
  1795. __func__, ret);
  1796. return ret;
  1797. }
  1798. /**
  1799. * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
  1800. * @oh: struct omap_hwmod *
  1801. * @state: state that _setup() should leave the hwmod in
  1802. *
  1803. * Sets the hwmod state that @oh will enter at the end of _setup() (called by
  1804. * omap_hwmod_late_init()). Only valid to call between calls to
  1805. * omap_hwmod_init() and omap_hwmod_late_init(). Returns 0 upon success or
  1806. * -EINVAL if there is a problem with the arguments or if the hwmod is
  1807. * in the wrong state.
  1808. */
  1809. int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
  1810. {
  1811. int ret;
  1812. if (!oh)
  1813. return -EINVAL;
  1814. if (state != _HWMOD_STATE_DISABLED &&
  1815. state != _HWMOD_STATE_ENABLED &&
  1816. state != _HWMOD_STATE_IDLE)
  1817. return -EINVAL;
  1818. mutex_lock(&oh->_mutex);
  1819. if (oh->_state != _HWMOD_STATE_REGISTERED) {
  1820. ret = -EINVAL;
  1821. goto ohsps_unlock;
  1822. }
  1823. oh->_postsetup_state = state;
  1824. ret = 0;
  1825. ohsps_unlock:
  1826. mutex_unlock(&oh->_mutex);
  1827. return ret;
  1828. }