omap_hwmod.c 58 KB

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