omap_hwmod.c 59 KB

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