omap_hwmod.c 61 KB

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