omap_hwmod.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789
  1. /*
  2. * omap_hwmod implementation for OMAP2/3/4
  3. *
  4. * Copyright (C) 2009-2011 Nokia Corporation
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. *
  7. * Paul Walmsley, Benoît Cousson, Kevin Hilman
  8. *
  9. * Created in collaboration with (alphabetical order): Thara Gopinath,
  10. * Tony Lindgren, Rajendra Nayak, Vikram Pandita, Sakari Poussa, Anand
  11. * Sawant, Santosh Shilimkar, Richard Woodruff
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. * Introduction
  18. * ------------
  19. * One way to view an OMAP SoC is as a collection of largely unrelated
  20. * IP blocks connected by interconnects. The IP blocks include
  21. * devices such as ARM processors, audio serial interfaces, UARTs,
  22. * etc. Some of these devices, like the DSP, are created by TI;
  23. * others, like the SGX, largely originate from external vendors. In
  24. * TI's documentation, on-chip devices are referred to as "OMAP
  25. * modules." Some of these IP blocks are identical across several
  26. * OMAP versions. Others are revised frequently.
  27. *
  28. * These OMAP modules are tied together by various interconnects.
  29. * Most of the address and data flow between modules is via OCP-based
  30. * interconnects such as the L3 and L4 buses; but there are other
  31. * interconnects that distribute the hardware clock tree, handle idle
  32. * and reset signaling, supply power, and connect the modules to
  33. * various pads or balls on the OMAP package.
  34. *
  35. * OMAP hwmod provides a consistent way to describe the on-chip
  36. * hardware blocks and their integration into the rest of the chip.
  37. * This description can be automatically generated from the TI
  38. * hardware database. OMAP hwmod provides a standard, consistent API
  39. * to reset, enable, idle, and disable these hardware blocks. And
  40. * hwmod provides a way for other core code, such as the Linux device
  41. * code or the OMAP power management and address space mapping code,
  42. * to query the hardware database.
  43. *
  44. * Using hwmod
  45. * -----------
  46. * Drivers won't call hwmod functions directly. That is done by the
  47. * omap_device code, and in rare occasions, by custom integration code
  48. * in arch/arm/ *omap*. The omap_device code includes functions to
  49. * build a struct platform_device using omap_hwmod data, and that is
  50. * currently how hwmod data is communicated to drivers and to the
  51. * Linux driver model. Most drivers will call omap_hwmod functions only
  52. * indirectly, via pm_runtime*() functions.
  53. *
  54. * From a layering perspective, here is where the OMAP hwmod code
  55. * fits into the kernel software stack:
  56. *
  57. * +-------------------------------+
  58. * | Device driver code |
  59. * | (e.g., drivers/) |
  60. * +-------------------------------+
  61. * | Linux driver model |
  62. * | (platform_device / |
  63. * | platform_driver data/code) |
  64. * +-------------------------------+
  65. * | OMAP core-driver integration |
  66. * |(arch/arm/mach-omap2/devices.c)|
  67. * +-------------------------------+
  68. * | omap_device code |
  69. * | (../plat-omap/omap_device.c) |
  70. * +-------------------------------+
  71. * ----> | omap_hwmod code/data | <-----
  72. * | (../mach-omap2/omap_hwmod*) |
  73. * +-------------------------------+
  74. * | OMAP clock/PRCM/register fns |
  75. * | (__raw_{read,write}l, clk*) |
  76. * +-------------------------------+
  77. *
  78. * Device drivers should not contain any OMAP-specific code or data in
  79. * them. They should only contain code to operate the IP block that
  80. * the driver is responsible for. This is because these IP blocks can
  81. * also appear in other SoCs, either from TI (such as DaVinci) or from
  82. * other manufacturers; and drivers should be reusable across other
  83. * platforms.
  84. *
  85. * The OMAP hwmod code also will attempt to reset and idle all on-chip
  86. * devices upon boot. The goal here is for the kernel to be
  87. * completely self-reliant and independent from bootloaders. This is
  88. * to ensure a repeatable configuration, both to ensure consistent
  89. * runtime behavior, and to make it easier for others to reproduce
  90. * bugs.
  91. *
  92. * OMAP module activity states
  93. * ---------------------------
  94. * The hwmod code considers modules to be in one of several activity
  95. * states. IP blocks start out in an UNKNOWN state, then once they
  96. * are registered via the hwmod code, proceed to the REGISTERED state.
  97. * Once their clock names are resolved to clock pointers, the module
  98. * enters the CLKS_INITED state; and finally, once the module has been
  99. * reset and the integration registers programmed, the INITIALIZED state
  100. * is entered. The hwmod code will then place the module into either
  101. * the IDLE state to save power, or in the case of a critical system
  102. * module, the ENABLED state.
  103. *
  104. * OMAP core integration code can then call omap_hwmod*() functions
  105. * directly to move the module between the IDLE, ENABLED, and DISABLED
  106. * states, as needed. This is done during both the PM idle loop, and
  107. * in the OMAP core integration code's implementation of the PM runtime
  108. * functions.
  109. *
  110. * References
  111. * ----------
  112. * This is a partial list.
  113. * - OMAP2420 Multimedia Processor Silicon Revision 2.1.1, 2.2 (SWPU064)
  114. * - OMAP2430 Multimedia Device POP Silicon Revision 2.1 (SWPU090)
  115. * - OMAP34xx Multimedia Device Silicon Revision 3.1 (SWPU108)
  116. * - OMAP4430 Multimedia Device Silicon Revision 1.0 (SWPU140)
  117. * - Open Core Protocol Specification 2.2
  118. *
  119. * To do:
  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 <linux/slab.h>
  139. #include "common.h"
  140. #include <plat/cpu.h>
  141. #include "clockdomain.h"
  142. #include "powerdomain.h"
  143. #include <plat/clock.h>
  144. #include <plat/omap_hwmod.h>
  145. #include <plat/prcm.h>
  146. #include "cm2xxx_3xxx.h"
  147. #include "cminst44xx.h"
  148. #include "prm2xxx_3xxx.h"
  149. #include "prm44xx.h"
  150. #include "prminst44xx.h"
  151. #include "mux.h"
  152. /* Maximum microseconds to wait for OMAP module to softreset */
  153. #define MAX_MODULE_SOFTRESET_WAIT 10000
  154. /* Name of the OMAP hwmod for the MPU */
  155. #define MPU_INITIATOR_NAME "mpu"
  156. /* omap_hwmod_list contains all registered struct omap_hwmods */
  157. static LIST_HEAD(omap_hwmod_list);
  158. /* mpu_oh: used to add/remove MPU initiator from sleepdep list */
  159. static struct omap_hwmod *mpu_oh;
  160. /* Private functions */
  161. /**
  162. * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
  163. * @oh: struct omap_hwmod *
  164. *
  165. * Load the current value of the hwmod OCP_SYSCONFIG register into the
  166. * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
  167. * OCP_SYSCONFIG register or 0 upon success.
  168. */
  169. static int _update_sysc_cache(struct omap_hwmod *oh)
  170. {
  171. if (!oh->class->sysc) {
  172. WARN(1, "omap_hwmod: %s: cannot read OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
  173. return -EINVAL;
  174. }
  175. /* XXX ensure module interface clock is up */
  176. oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
  177. if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
  178. oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
  179. return 0;
  180. }
  181. /**
  182. * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
  183. * @v: OCP_SYSCONFIG value to write
  184. * @oh: struct omap_hwmod *
  185. *
  186. * Write @v into the module class' OCP_SYSCONFIG register, if it has
  187. * one. No return value.
  188. */
  189. static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
  190. {
  191. if (!oh->class->sysc) {
  192. WARN(1, "omap_hwmod: %s: cannot write OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
  193. return;
  194. }
  195. /* XXX ensure module interface clock is up */
  196. /* Module might have lost context, always update cache and register */
  197. oh->_sysc_cache = v;
  198. omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
  199. }
  200. /**
  201. * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
  202. * @oh: struct omap_hwmod *
  203. * @standbymode: MIDLEMODE field bits
  204. * @v: pointer to register contents to modify
  205. *
  206. * Update the master standby mode bits in @v to be @standbymode for
  207. * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
  208. * upon error or 0 upon success.
  209. */
  210. static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
  211. u32 *v)
  212. {
  213. u32 mstandby_mask;
  214. u8 mstandby_shift;
  215. if (!oh->class->sysc ||
  216. !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
  217. return -EINVAL;
  218. if (!oh->class->sysc->sysc_fields) {
  219. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  220. return -EINVAL;
  221. }
  222. mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
  223. mstandby_mask = (0x3 << mstandby_shift);
  224. *v &= ~mstandby_mask;
  225. *v |= __ffs(standbymode) << mstandby_shift;
  226. return 0;
  227. }
  228. /**
  229. * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
  230. * @oh: struct omap_hwmod *
  231. * @idlemode: SIDLEMODE field bits
  232. * @v: pointer to register contents to modify
  233. *
  234. * Update the slave idle mode bits in @v to be @idlemode for the @oh
  235. * hwmod. Does not write to the hardware. Returns -EINVAL upon error
  236. * or 0 upon success.
  237. */
  238. static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
  239. {
  240. u32 sidle_mask;
  241. u8 sidle_shift;
  242. if (!oh->class->sysc ||
  243. !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
  244. return -EINVAL;
  245. if (!oh->class->sysc->sysc_fields) {
  246. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  247. return -EINVAL;
  248. }
  249. sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
  250. sidle_mask = (0x3 << sidle_shift);
  251. *v &= ~sidle_mask;
  252. *v |= __ffs(idlemode) << sidle_shift;
  253. return 0;
  254. }
  255. /**
  256. * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
  257. * @oh: struct omap_hwmod *
  258. * @clockact: CLOCKACTIVITY field bits
  259. * @v: pointer to register contents to modify
  260. *
  261. * Update the clockactivity mode bits in @v to be @clockact for the
  262. * @oh hwmod. Used for additional powersaving on some modules. Does
  263. * not write to the hardware. Returns -EINVAL upon error or 0 upon
  264. * success.
  265. */
  266. static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
  267. {
  268. u32 clkact_mask;
  269. u8 clkact_shift;
  270. if (!oh->class->sysc ||
  271. !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
  272. return -EINVAL;
  273. if (!oh->class->sysc->sysc_fields) {
  274. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  275. return -EINVAL;
  276. }
  277. clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
  278. clkact_mask = (0x3 << clkact_shift);
  279. *v &= ~clkact_mask;
  280. *v |= clockact << clkact_shift;
  281. return 0;
  282. }
  283. /**
  284. * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
  285. * @oh: struct omap_hwmod *
  286. * @v: pointer to register contents to modify
  287. *
  288. * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
  289. * error or 0 upon success.
  290. */
  291. static int _set_softreset(struct omap_hwmod *oh, u32 *v)
  292. {
  293. u32 softrst_mask;
  294. if (!oh->class->sysc ||
  295. !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
  296. return -EINVAL;
  297. if (!oh->class->sysc->sysc_fields) {
  298. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  299. return -EINVAL;
  300. }
  301. softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
  302. *v |= softrst_mask;
  303. return 0;
  304. }
  305. /**
  306. * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
  307. * @oh: struct omap_hwmod *
  308. * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
  309. * @v: pointer to register contents to modify
  310. *
  311. * Update the module autoidle bit in @v to be @autoidle for the @oh
  312. * hwmod. The autoidle bit controls whether the module can gate
  313. * internal clocks automatically when it isn't doing anything; the
  314. * exact function of this bit varies on a per-module basis. This
  315. * function does not write to the hardware. Returns -EINVAL upon
  316. * error or 0 upon success.
  317. */
  318. static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
  319. u32 *v)
  320. {
  321. u32 autoidle_mask;
  322. u8 autoidle_shift;
  323. if (!oh->class->sysc ||
  324. !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
  325. return -EINVAL;
  326. if (!oh->class->sysc->sysc_fields) {
  327. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  328. return -EINVAL;
  329. }
  330. autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
  331. autoidle_mask = (0x1 << autoidle_shift);
  332. *v &= ~autoidle_mask;
  333. *v |= autoidle << autoidle_shift;
  334. return 0;
  335. }
  336. /**
  337. * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
  338. * @oh: struct omap_hwmod *
  339. * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
  340. *
  341. * Set or clear the I/O pad wakeup flag in the mux entries for the
  342. * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
  343. * in memory. If the hwmod is currently idled, and the new idle
  344. * values don't match the previous ones, this function will also
  345. * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
  346. * currently idled, this function won't touch the hardware: the new
  347. * mux settings are written to the SCM PADCTRL registers when the
  348. * hwmod is idled. No return value.
  349. */
  350. static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
  351. {
  352. struct omap_device_pad *pad;
  353. bool change = false;
  354. u16 prev_idle;
  355. int j;
  356. if (!oh->mux || !oh->mux->enabled)
  357. return;
  358. for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
  359. pad = oh->mux->pads_dynamic[j];
  360. if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
  361. continue;
  362. prev_idle = pad->idle;
  363. if (set_wake)
  364. pad->idle |= OMAP_WAKEUP_EN;
  365. else
  366. pad->idle &= ~OMAP_WAKEUP_EN;
  367. if (prev_idle != pad->idle)
  368. change = true;
  369. }
  370. if (change && oh->_state == _HWMOD_STATE_IDLE)
  371. omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
  372. }
  373. /**
  374. * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
  375. * @oh: struct omap_hwmod *
  376. *
  377. * Allow the hardware module @oh to send wakeups. Returns -EINVAL
  378. * upon error or 0 upon success.
  379. */
  380. static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
  381. {
  382. if (!oh->class->sysc ||
  383. !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
  384. (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
  385. (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
  386. return -EINVAL;
  387. if (!oh->class->sysc->sysc_fields) {
  388. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  389. return -EINVAL;
  390. }
  391. if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
  392. *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
  393. if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
  394. _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
  395. if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
  396. _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
  397. /* XXX test pwrdm_get_wken for this hwmod's subsystem */
  398. oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
  399. return 0;
  400. }
  401. /**
  402. * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
  403. * @oh: struct omap_hwmod *
  404. *
  405. * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
  406. * upon error or 0 upon success.
  407. */
  408. static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
  409. {
  410. if (!oh->class->sysc ||
  411. !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
  412. (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
  413. (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
  414. return -EINVAL;
  415. if (!oh->class->sysc->sysc_fields) {
  416. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  417. return -EINVAL;
  418. }
  419. if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
  420. *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
  421. if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
  422. _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
  423. if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
  424. _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
  425. /* XXX test pwrdm_get_wken for this hwmod's subsystem */
  426. oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
  427. return 0;
  428. }
  429. /**
  430. * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
  431. * @oh: struct omap_hwmod *
  432. *
  433. * Prevent the hardware module @oh from entering idle while the
  434. * hardare module initiator @init_oh is active. Useful when a module
  435. * will be accessed by a particular initiator (e.g., if a module will
  436. * be accessed by the IVA, there should be a sleepdep between the IVA
  437. * initiator and the module). Only applies to modules in smart-idle
  438. * mode. If the clockdomain is marked as not needing autodeps, return
  439. * 0 without doing anything. Otherwise, returns -EINVAL upon error or
  440. * passes along clkdm_add_sleepdep() value upon success.
  441. */
  442. static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
  443. {
  444. if (!oh->_clk)
  445. return -EINVAL;
  446. if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
  447. return 0;
  448. return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
  449. }
  450. /**
  451. * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
  452. * @oh: struct omap_hwmod *
  453. *
  454. * Allow the hardware module @oh to enter idle while the hardare
  455. * module initiator @init_oh is active. Useful when a module will not
  456. * be accessed by a particular initiator (e.g., if a module will not
  457. * be accessed by the IVA, there should be no sleepdep between the IVA
  458. * initiator and the module). Only applies to modules in smart-idle
  459. * mode. If the clockdomain is marked as not needing autodeps, return
  460. * 0 without doing anything. Returns -EINVAL upon error or passes
  461. * along clkdm_del_sleepdep() value upon success.
  462. */
  463. static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
  464. {
  465. if (!oh->_clk)
  466. return -EINVAL;
  467. if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
  468. return 0;
  469. return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
  470. }
  471. /**
  472. * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
  473. * @oh: struct omap_hwmod *
  474. *
  475. * Called from _init_clocks(). Populates the @oh _clk (main
  476. * functional clock pointer) if a main_clk is present. Returns 0 on
  477. * success or -EINVAL on error.
  478. */
  479. static int _init_main_clk(struct omap_hwmod *oh)
  480. {
  481. int ret = 0;
  482. if (!oh->main_clk)
  483. return 0;
  484. oh->_clk = omap_clk_get_by_name(oh->main_clk);
  485. if (!oh->_clk) {
  486. pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
  487. oh->name, oh->main_clk);
  488. return -EINVAL;
  489. }
  490. if (!oh->_clk->clkdm)
  491. pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
  492. oh->main_clk, oh->_clk->name);
  493. return ret;
  494. }
  495. /**
  496. * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
  497. * @oh: struct omap_hwmod *
  498. *
  499. * Called from _init_clocks(). Populates the @oh OCP slave interface
  500. * clock pointers. Returns 0 on success or -EINVAL on error.
  501. */
  502. static int _init_interface_clks(struct omap_hwmod *oh)
  503. {
  504. struct clk *c;
  505. int i;
  506. int ret = 0;
  507. if (oh->slaves_cnt == 0)
  508. return 0;
  509. for (i = 0; i < oh->slaves_cnt; i++) {
  510. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  511. if (!os->clk)
  512. continue;
  513. c = omap_clk_get_by_name(os->clk);
  514. if (!c) {
  515. pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
  516. oh->name, os->clk);
  517. ret = -EINVAL;
  518. }
  519. os->_clk = c;
  520. }
  521. return ret;
  522. }
  523. /**
  524. * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
  525. * @oh: struct omap_hwmod *
  526. *
  527. * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
  528. * clock pointers. Returns 0 on success or -EINVAL on error.
  529. */
  530. static int _init_opt_clks(struct omap_hwmod *oh)
  531. {
  532. struct omap_hwmod_opt_clk *oc;
  533. struct clk *c;
  534. int i;
  535. int ret = 0;
  536. for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
  537. c = omap_clk_get_by_name(oc->clk);
  538. if (!c) {
  539. pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
  540. oh->name, oc->clk);
  541. ret = -EINVAL;
  542. }
  543. oc->_clk = c;
  544. }
  545. return ret;
  546. }
  547. /**
  548. * _enable_clocks - enable hwmod main clock and interface clocks
  549. * @oh: struct omap_hwmod *
  550. *
  551. * Enables all clocks necessary for register reads and writes to succeed
  552. * on the hwmod @oh. Returns 0.
  553. */
  554. static int _enable_clocks(struct omap_hwmod *oh)
  555. {
  556. int i;
  557. pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
  558. if (oh->_clk)
  559. clk_enable(oh->_clk);
  560. if (oh->slaves_cnt > 0) {
  561. for (i = 0; i < oh->slaves_cnt; i++) {
  562. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  563. struct clk *c = os->_clk;
  564. if (c && (os->flags & OCPIF_SWSUP_IDLE))
  565. clk_enable(c);
  566. }
  567. }
  568. /* The opt clocks are controlled by the device driver. */
  569. return 0;
  570. }
  571. /**
  572. * _disable_clocks - disable hwmod main clock and interface clocks
  573. * @oh: struct omap_hwmod *
  574. *
  575. * Disables the hwmod @oh main functional and interface clocks. Returns 0.
  576. */
  577. static int _disable_clocks(struct omap_hwmod *oh)
  578. {
  579. int i;
  580. pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
  581. if (oh->_clk)
  582. clk_disable(oh->_clk);
  583. if (oh->slaves_cnt > 0) {
  584. for (i = 0; i < oh->slaves_cnt; i++) {
  585. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  586. struct clk *c = os->_clk;
  587. if (c && (os->flags & OCPIF_SWSUP_IDLE))
  588. clk_disable(c);
  589. }
  590. }
  591. /* The opt clocks are controlled by the device driver. */
  592. return 0;
  593. }
  594. static void _enable_optional_clocks(struct omap_hwmod *oh)
  595. {
  596. struct omap_hwmod_opt_clk *oc;
  597. int i;
  598. pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
  599. for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
  600. if (oc->_clk) {
  601. pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
  602. oc->_clk->name);
  603. clk_enable(oc->_clk);
  604. }
  605. }
  606. static void _disable_optional_clocks(struct omap_hwmod *oh)
  607. {
  608. struct omap_hwmod_opt_clk *oc;
  609. int i;
  610. pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
  611. for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
  612. if (oc->_clk) {
  613. pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
  614. oc->_clk->name);
  615. clk_disable(oc->_clk);
  616. }
  617. }
  618. /**
  619. * _enable_module - enable CLKCTRL modulemode on OMAP4
  620. * @oh: struct omap_hwmod *
  621. *
  622. * Enables the PRCM module mode related to the hwmod @oh.
  623. * No return value.
  624. */
  625. static void _enable_module(struct omap_hwmod *oh)
  626. {
  627. /* The module mode does not exist prior OMAP4 */
  628. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  629. return;
  630. if (!oh->clkdm || !oh->prcm.omap4.modulemode)
  631. return;
  632. pr_debug("omap_hwmod: %s: _enable_module: %d\n",
  633. oh->name, oh->prcm.omap4.modulemode);
  634. omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
  635. oh->clkdm->prcm_partition,
  636. oh->clkdm->cm_inst,
  637. oh->clkdm->clkdm_offs,
  638. oh->prcm.omap4.clkctrl_offs);
  639. }
  640. /**
  641. * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
  642. * @oh: struct omap_hwmod *
  643. *
  644. * Wait for a module @oh to enter slave idle. Returns 0 if the module
  645. * does not have an IDLEST bit or if the module successfully enters
  646. * slave idle; otherwise, pass along the return value of the
  647. * appropriate *_cm*_wait_module_idle() function.
  648. */
  649. static int _omap4_wait_target_disable(struct omap_hwmod *oh)
  650. {
  651. if (!cpu_is_omap44xx())
  652. return 0;
  653. if (!oh)
  654. return -EINVAL;
  655. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  656. return 0;
  657. if (oh->flags & HWMOD_NO_IDLEST)
  658. return 0;
  659. return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
  660. oh->clkdm->cm_inst,
  661. oh->clkdm->clkdm_offs,
  662. oh->prcm.omap4.clkctrl_offs);
  663. }
  664. /**
  665. * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
  666. * @oh: struct omap_hwmod *
  667. *
  668. * Disable the PRCM module mode related to the hwmod @oh.
  669. * Return EINVAL if the modulemode is not supported and 0 in case of success.
  670. */
  671. static int _omap4_disable_module(struct omap_hwmod *oh)
  672. {
  673. int v;
  674. /* The module mode does not exist prior OMAP4 */
  675. if (!cpu_is_omap44xx())
  676. return -EINVAL;
  677. if (!oh->clkdm || !oh->prcm.omap4.modulemode)
  678. return -EINVAL;
  679. pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
  680. omap4_cminst_module_disable(oh->clkdm->prcm_partition,
  681. oh->clkdm->cm_inst,
  682. oh->clkdm->clkdm_offs,
  683. oh->prcm.omap4.clkctrl_offs);
  684. v = _omap4_wait_target_disable(oh);
  685. if (v)
  686. pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
  687. oh->name);
  688. return 0;
  689. }
  690. /**
  691. * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
  692. * @oh: struct omap_hwmod *oh
  693. *
  694. * Count and return the number of MPU IRQs associated with the hwmod
  695. * @oh. Used to allocate struct resource data. Returns 0 if @oh is
  696. * NULL.
  697. */
  698. static int _count_mpu_irqs(struct omap_hwmod *oh)
  699. {
  700. struct omap_hwmod_irq_info *ohii;
  701. int i = 0;
  702. if (!oh || !oh->mpu_irqs)
  703. return 0;
  704. do {
  705. ohii = &oh->mpu_irqs[i++];
  706. } while (ohii->irq != -1);
  707. return i-1;
  708. }
  709. /**
  710. * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
  711. * @oh: struct omap_hwmod *oh
  712. *
  713. * Count and return the number of SDMA request lines associated with
  714. * the hwmod @oh. Used to allocate struct resource data. Returns 0
  715. * if @oh is NULL.
  716. */
  717. static int _count_sdma_reqs(struct omap_hwmod *oh)
  718. {
  719. struct omap_hwmod_dma_info *ohdi;
  720. int i = 0;
  721. if (!oh || !oh->sdma_reqs)
  722. return 0;
  723. do {
  724. ohdi = &oh->sdma_reqs[i++];
  725. } while (ohdi->dma_req != -1);
  726. return i-1;
  727. }
  728. /**
  729. * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
  730. * @oh: struct omap_hwmod *oh
  731. *
  732. * Count and return the number of address space ranges associated with
  733. * the hwmod @oh. Used to allocate struct resource data. Returns 0
  734. * if @oh is NULL.
  735. */
  736. static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
  737. {
  738. struct omap_hwmod_addr_space *mem;
  739. int i = 0;
  740. if (!os || !os->addr)
  741. return 0;
  742. do {
  743. mem = &os->addr[i++];
  744. } while (mem->pa_start != mem->pa_end);
  745. return i-1;
  746. }
  747. /**
  748. * _find_mpu_port_index - find hwmod OCP slave port ID intended for MPU use
  749. * @oh: struct omap_hwmod *
  750. *
  751. * Returns the array index of the OCP slave port that the MPU
  752. * addresses the device on, or -EINVAL upon error or not found.
  753. */
  754. static int __init _find_mpu_port_index(struct omap_hwmod *oh)
  755. {
  756. int i;
  757. int found = 0;
  758. if (!oh || oh->slaves_cnt == 0)
  759. return -EINVAL;
  760. for (i = 0; i < oh->slaves_cnt; i++) {
  761. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  762. if (os->user & OCP_USER_MPU) {
  763. found = 1;
  764. break;
  765. }
  766. }
  767. if (found)
  768. pr_debug("omap_hwmod: %s: MPU OCP slave port ID %d\n",
  769. oh->name, i);
  770. else
  771. pr_debug("omap_hwmod: %s: no MPU OCP slave port found\n",
  772. oh->name);
  773. return (found) ? i : -EINVAL;
  774. }
  775. /**
  776. * _find_mpu_rt_base - find hwmod register target base addr accessible by MPU
  777. * @oh: struct omap_hwmod *
  778. *
  779. * Return the virtual address of the base of the register target of
  780. * device @oh, or NULL on error.
  781. */
  782. static void __iomem * __init _find_mpu_rt_base(struct omap_hwmod *oh, u8 index)
  783. {
  784. struct omap_hwmod_ocp_if *os;
  785. struct omap_hwmod_addr_space *mem;
  786. int i = 0, found = 0;
  787. void __iomem *va_start;
  788. if (!oh || oh->slaves_cnt == 0)
  789. return NULL;
  790. os = oh->slaves[index];
  791. if (!os->addr)
  792. return NULL;
  793. do {
  794. mem = &os->addr[i++];
  795. if (mem->flags & ADDR_TYPE_RT)
  796. found = 1;
  797. } while (!found && mem->pa_start != mem->pa_end);
  798. if (found) {
  799. va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
  800. if (!va_start) {
  801. pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
  802. return NULL;
  803. }
  804. pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
  805. oh->name, va_start);
  806. } else {
  807. pr_debug("omap_hwmod: %s: no MPU register target found\n",
  808. oh->name);
  809. }
  810. return (found) ? va_start : NULL;
  811. }
  812. /**
  813. * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
  814. * @oh: struct omap_hwmod *
  815. *
  816. * If module is marked as SWSUP_SIDLE, force the module out of slave
  817. * idle; otherwise, configure it for smart-idle. If module is marked
  818. * as SWSUP_MSUSPEND, force the module out of master standby;
  819. * otherwise, configure it for smart-standby. No return value.
  820. */
  821. static void _enable_sysc(struct omap_hwmod *oh)
  822. {
  823. u8 idlemode, sf;
  824. u32 v;
  825. if (!oh->class->sysc)
  826. return;
  827. v = oh->_sysc_cache;
  828. sf = oh->class->sysc->sysc_flags;
  829. if (sf & SYSC_HAS_SIDLEMODE) {
  830. idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
  831. HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
  832. _set_slave_idlemode(oh, idlemode, &v);
  833. }
  834. if (sf & SYSC_HAS_MIDLEMODE) {
  835. if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
  836. idlemode = HWMOD_IDLEMODE_NO;
  837. } else {
  838. if (sf & SYSC_HAS_ENAWAKEUP)
  839. _enable_wakeup(oh, &v);
  840. if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
  841. idlemode = HWMOD_IDLEMODE_SMART_WKUP;
  842. else
  843. idlemode = HWMOD_IDLEMODE_SMART;
  844. }
  845. _set_master_standbymode(oh, idlemode, &v);
  846. }
  847. /*
  848. * XXX The clock framework should handle this, by
  849. * calling into this code. But this must wait until the
  850. * clock structures are tagged with omap_hwmod entries
  851. */
  852. if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
  853. (sf & SYSC_HAS_CLOCKACTIVITY))
  854. _set_clockactivity(oh, oh->class->sysc->clockact, &v);
  855. /* If slave is in SMARTIDLE, also enable wakeup */
  856. if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
  857. _enable_wakeup(oh, &v);
  858. _write_sysconfig(v, oh);
  859. /*
  860. * Set the autoidle bit only after setting the smartidle bit
  861. * Setting this will not have any impact on the other modules.
  862. */
  863. if (sf & SYSC_HAS_AUTOIDLE) {
  864. idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
  865. 0 : 1;
  866. _set_module_autoidle(oh, idlemode, &v);
  867. _write_sysconfig(v, oh);
  868. }
  869. }
  870. /**
  871. * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
  872. * @oh: struct omap_hwmod *
  873. *
  874. * If module is marked as SWSUP_SIDLE, force the module into slave
  875. * idle; otherwise, configure it for smart-idle. If module is marked
  876. * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
  877. * configure it for smart-standby. No return value.
  878. */
  879. static void _idle_sysc(struct omap_hwmod *oh)
  880. {
  881. u8 idlemode, sf;
  882. u32 v;
  883. if (!oh->class->sysc)
  884. return;
  885. v = oh->_sysc_cache;
  886. sf = oh->class->sysc->sysc_flags;
  887. if (sf & SYSC_HAS_SIDLEMODE) {
  888. idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
  889. HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
  890. _set_slave_idlemode(oh, idlemode, &v);
  891. }
  892. if (sf & SYSC_HAS_MIDLEMODE) {
  893. if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
  894. idlemode = HWMOD_IDLEMODE_FORCE;
  895. } else {
  896. if (sf & SYSC_HAS_ENAWAKEUP)
  897. _enable_wakeup(oh, &v);
  898. if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
  899. idlemode = HWMOD_IDLEMODE_SMART_WKUP;
  900. else
  901. idlemode = HWMOD_IDLEMODE_SMART;
  902. }
  903. _set_master_standbymode(oh, idlemode, &v);
  904. }
  905. /* If slave is in SMARTIDLE, also enable wakeup */
  906. if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
  907. _enable_wakeup(oh, &v);
  908. _write_sysconfig(v, oh);
  909. }
  910. /**
  911. * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
  912. * @oh: struct omap_hwmod *
  913. *
  914. * Force the module into slave idle and master suspend. No return
  915. * value.
  916. */
  917. static void _shutdown_sysc(struct omap_hwmod *oh)
  918. {
  919. u32 v;
  920. u8 sf;
  921. if (!oh->class->sysc)
  922. return;
  923. v = oh->_sysc_cache;
  924. sf = oh->class->sysc->sysc_flags;
  925. if (sf & SYSC_HAS_SIDLEMODE)
  926. _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
  927. if (sf & SYSC_HAS_MIDLEMODE)
  928. _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
  929. if (sf & SYSC_HAS_AUTOIDLE)
  930. _set_module_autoidle(oh, 1, &v);
  931. _write_sysconfig(v, oh);
  932. }
  933. /**
  934. * _lookup - find an omap_hwmod by name
  935. * @name: find an omap_hwmod by name
  936. *
  937. * Return a pointer to an omap_hwmod by name, or NULL if not found.
  938. */
  939. static struct omap_hwmod *_lookup(const char *name)
  940. {
  941. struct omap_hwmod *oh, *temp_oh;
  942. oh = NULL;
  943. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  944. if (!strcmp(name, temp_oh->name)) {
  945. oh = temp_oh;
  946. break;
  947. }
  948. }
  949. return oh;
  950. }
  951. /**
  952. * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
  953. * @oh: struct omap_hwmod *
  954. *
  955. * Convert a clockdomain name stored in a struct omap_hwmod into a
  956. * clockdomain pointer, and save it into the struct omap_hwmod.
  957. * return -EINVAL if clkdm_name does not exist or if the lookup failed.
  958. */
  959. static int _init_clkdm(struct omap_hwmod *oh)
  960. {
  961. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  962. return 0;
  963. if (!oh->clkdm_name) {
  964. pr_warning("omap_hwmod: %s: no clkdm_name\n", oh->name);
  965. return -EINVAL;
  966. }
  967. oh->clkdm = clkdm_lookup(oh->clkdm_name);
  968. if (!oh->clkdm) {
  969. pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
  970. oh->name, oh->clkdm_name);
  971. return -EINVAL;
  972. }
  973. pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
  974. oh->name, oh->clkdm_name);
  975. return 0;
  976. }
  977. /**
  978. * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
  979. * well the clockdomain.
  980. * @oh: struct omap_hwmod *
  981. * @data: not used; pass NULL
  982. *
  983. * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
  984. * Resolves all clock names embedded in the hwmod. Returns 0 on
  985. * success, or a negative error code on failure.
  986. */
  987. static int _init_clocks(struct omap_hwmod *oh, void *data)
  988. {
  989. int ret = 0;
  990. if (oh->_state != _HWMOD_STATE_REGISTERED)
  991. return 0;
  992. pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
  993. ret |= _init_main_clk(oh);
  994. ret |= _init_interface_clks(oh);
  995. ret |= _init_opt_clks(oh);
  996. ret |= _init_clkdm(oh);
  997. if (!ret)
  998. oh->_state = _HWMOD_STATE_CLKS_INITED;
  999. else
  1000. pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
  1001. return ret;
  1002. }
  1003. /**
  1004. * _wait_target_ready - wait for a module to leave slave idle
  1005. * @oh: struct omap_hwmod *
  1006. *
  1007. * Wait for a module @oh to leave slave idle. Returns 0 if the module
  1008. * does not have an IDLEST bit or if the module successfully leaves
  1009. * slave idle; otherwise, pass along the return value of the
  1010. * appropriate *_cm*_wait_module_ready() function.
  1011. */
  1012. static int _wait_target_ready(struct omap_hwmod *oh)
  1013. {
  1014. struct omap_hwmod_ocp_if *os;
  1015. int ret;
  1016. if (!oh)
  1017. return -EINVAL;
  1018. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  1019. return 0;
  1020. os = oh->slaves[oh->_mpu_port_index];
  1021. if (oh->flags & HWMOD_NO_IDLEST)
  1022. return 0;
  1023. /* XXX check module SIDLEMODE */
  1024. /* XXX check clock enable states */
  1025. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1026. ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
  1027. oh->prcm.omap2.idlest_reg_id,
  1028. oh->prcm.omap2.idlest_idle_bit);
  1029. } else if (cpu_is_omap44xx()) {
  1030. if (!oh->clkdm)
  1031. return -EINVAL;
  1032. ret = omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
  1033. oh->clkdm->cm_inst,
  1034. oh->clkdm->clkdm_offs,
  1035. oh->prcm.omap4.clkctrl_offs);
  1036. } else {
  1037. BUG();
  1038. };
  1039. return ret;
  1040. }
  1041. /**
  1042. * _lookup_hardreset - fill register bit info for this hwmod/reset line
  1043. * @oh: struct omap_hwmod *
  1044. * @name: name of the reset line in the context of this hwmod
  1045. * @ohri: struct omap_hwmod_rst_info * that this function will fill in
  1046. *
  1047. * Return the bit position of the reset line that match the
  1048. * input name. Return -ENOENT if not found.
  1049. */
  1050. static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
  1051. struct omap_hwmod_rst_info *ohri)
  1052. {
  1053. int i;
  1054. for (i = 0; i < oh->rst_lines_cnt; i++) {
  1055. const char *rst_line = oh->rst_lines[i].name;
  1056. if (!strcmp(rst_line, name)) {
  1057. ohri->rst_shift = oh->rst_lines[i].rst_shift;
  1058. ohri->st_shift = oh->rst_lines[i].st_shift;
  1059. pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
  1060. oh->name, __func__, rst_line, ohri->rst_shift,
  1061. ohri->st_shift);
  1062. return 0;
  1063. }
  1064. }
  1065. return -ENOENT;
  1066. }
  1067. /**
  1068. * _assert_hardreset - assert the HW reset line of submodules
  1069. * contained in the hwmod module.
  1070. * @oh: struct omap_hwmod *
  1071. * @name: name of the reset line to lookup and assert
  1072. *
  1073. * Some IP like dsp, ipu or iva contain processor that require
  1074. * an HW reset line to be assert / deassert in order to enable fully
  1075. * the IP.
  1076. */
  1077. static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
  1078. {
  1079. struct omap_hwmod_rst_info ohri;
  1080. u8 ret;
  1081. if (!oh)
  1082. return -EINVAL;
  1083. ret = _lookup_hardreset(oh, name, &ohri);
  1084. if (IS_ERR_VALUE(ret))
  1085. return ret;
  1086. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  1087. return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
  1088. ohri.rst_shift);
  1089. else if (cpu_is_omap44xx())
  1090. return omap4_prminst_assert_hardreset(ohri.rst_shift,
  1091. oh->clkdm->pwrdm.ptr->prcm_partition,
  1092. oh->clkdm->pwrdm.ptr->prcm_offs,
  1093. oh->prcm.omap4.rstctrl_offs);
  1094. else
  1095. return -EINVAL;
  1096. }
  1097. /**
  1098. * _deassert_hardreset - deassert the HW reset line of submodules contained
  1099. * in the hwmod module.
  1100. * @oh: struct omap_hwmod *
  1101. * @name: name of the reset line to look up and deassert
  1102. *
  1103. * Some IP like dsp, ipu or iva contain processor that require
  1104. * an HW reset line to be assert / deassert in order to enable fully
  1105. * the IP.
  1106. */
  1107. static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
  1108. {
  1109. struct omap_hwmod_rst_info ohri;
  1110. int ret;
  1111. if (!oh)
  1112. return -EINVAL;
  1113. ret = _lookup_hardreset(oh, name, &ohri);
  1114. if (IS_ERR_VALUE(ret))
  1115. return ret;
  1116. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1117. ret = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
  1118. ohri.rst_shift,
  1119. ohri.st_shift);
  1120. } else if (cpu_is_omap44xx()) {
  1121. if (ohri.st_shift)
  1122. pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
  1123. oh->name, name);
  1124. ret = omap4_prminst_deassert_hardreset(ohri.rst_shift,
  1125. oh->clkdm->pwrdm.ptr->prcm_partition,
  1126. oh->clkdm->pwrdm.ptr->prcm_offs,
  1127. oh->prcm.omap4.rstctrl_offs);
  1128. } else {
  1129. return -EINVAL;
  1130. }
  1131. if (ret == -EBUSY)
  1132. pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
  1133. return ret;
  1134. }
  1135. /**
  1136. * _read_hardreset - read the HW reset line state of submodules
  1137. * contained in the hwmod module
  1138. * @oh: struct omap_hwmod *
  1139. * @name: name of the reset line to look up and read
  1140. *
  1141. * Return the state of the reset line.
  1142. */
  1143. static int _read_hardreset(struct omap_hwmod *oh, const char *name)
  1144. {
  1145. struct omap_hwmod_rst_info ohri;
  1146. u8 ret;
  1147. if (!oh)
  1148. return -EINVAL;
  1149. ret = _lookup_hardreset(oh, name, &ohri);
  1150. if (IS_ERR_VALUE(ret))
  1151. return ret;
  1152. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1153. return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
  1154. ohri.st_shift);
  1155. } else if (cpu_is_omap44xx()) {
  1156. return omap4_prminst_is_hardreset_asserted(ohri.rst_shift,
  1157. oh->clkdm->pwrdm.ptr->prcm_partition,
  1158. oh->clkdm->pwrdm.ptr->prcm_offs,
  1159. oh->prcm.omap4.rstctrl_offs);
  1160. } else {
  1161. return -EINVAL;
  1162. }
  1163. }
  1164. /**
  1165. * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
  1166. * @oh: struct omap_hwmod *
  1167. *
  1168. * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
  1169. * enabled for this to work. Returns -EINVAL if the hwmod cannot be
  1170. * reset this way or if the hwmod is in the wrong state, -ETIMEDOUT if
  1171. * the module did not reset in time, or 0 upon success.
  1172. *
  1173. * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
  1174. * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
  1175. * use the SYSCONFIG softreset bit to provide the status.
  1176. *
  1177. * Note that some IP like McBSP do have reset control but don't have
  1178. * reset status.
  1179. */
  1180. static int _ocp_softreset(struct omap_hwmod *oh)
  1181. {
  1182. u32 v, softrst_mask;
  1183. int c = 0;
  1184. int ret = 0;
  1185. if (!oh->class->sysc ||
  1186. !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
  1187. return -EINVAL;
  1188. /* clocks must be on for this operation */
  1189. if (oh->_state != _HWMOD_STATE_ENABLED) {
  1190. pr_warning("omap_hwmod: %s: reset can only be entered from "
  1191. "enabled state\n", oh->name);
  1192. return -EINVAL;
  1193. }
  1194. /* For some modules, all optionnal clocks need to be enabled as well */
  1195. if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
  1196. _enable_optional_clocks(oh);
  1197. pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
  1198. v = oh->_sysc_cache;
  1199. ret = _set_softreset(oh, &v);
  1200. if (ret)
  1201. goto dis_opt_clks;
  1202. _write_sysconfig(v, oh);
  1203. if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
  1204. omap_test_timeout((omap_hwmod_read(oh,
  1205. oh->class->sysc->syss_offs)
  1206. & SYSS_RESETDONE_MASK),
  1207. MAX_MODULE_SOFTRESET_WAIT, c);
  1208. else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
  1209. softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
  1210. omap_test_timeout(!(omap_hwmod_read(oh,
  1211. oh->class->sysc->sysc_offs)
  1212. & softrst_mask),
  1213. MAX_MODULE_SOFTRESET_WAIT, c);
  1214. }
  1215. if (c == MAX_MODULE_SOFTRESET_WAIT)
  1216. pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
  1217. oh->name, MAX_MODULE_SOFTRESET_WAIT);
  1218. else
  1219. pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
  1220. /*
  1221. * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
  1222. * _wait_target_ready() or _reset()
  1223. */
  1224. ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
  1225. dis_opt_clks:
  1226. if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
  1227. _disable_optional_clocks(oh);
  1228. return ret;
  1229. }
  1230. /**
  1231. * _reset - reset an omap_hwmod
  1232. * @oh: struct omap_hwmod *
  1233. *
  1234. * Resets an omap_hwmod @oh. The default software reset mechanism for
  1235. * most OMAP IP blocks is triggered via the OCP_SYSCONFIG.SOFTRESET
  1236. * bit. However, some hwmods cannot be reset via this method: some
  1237. * are not targets and therefore have no OCP header registers to
  1238. * access; others (like the IVA) have idiosyncratic reset sequences.
  1239. * So for these relatively rare cases, custom reset code can be
  1240. * supplied in the struct omap_hwmod_class .reset function pointer.
  1241. * Passes along the return value from either _reset() or the custom
  1242. * reset function - these must return -EINVAL if the hwmod cannot be
  1243. * reset this way or if the hwmod is in the wrong state, -ETIMEDOUT if
  1244. * the module did not reset in time, or 0 upon success.
  1245. */
  1246. static int _reset(struct omap_hwmod *oh)
  1247. {
  1248. int ret;
  1249. pr_debug("omap_hwmod: %s: resetting\n", oh->name);
  1250. ret = (oh->class->reset) ? oh->class->reset(oh) : _ocp_softreset(oh);
  1251. return ret;
  1252. }
  1253. /**
  1254. * _enable - enable an omap_hwmod
  1255. * @oh: struct omap_hwmod *
  1256. *
  1257. * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
  1258. * register target. Returns -EINVAL if the hwmod is in the wrong
  1259. * state or passes along the return value of _wait_target_ready().
  1260. */
  1261. static int _enable(struct omap_hwmod *oh)
  1262. {
  1263. int r;
  1264. int hwsup = 0;
  1265. pr_debug("omap_hwmod: %s: enabling\n", oh->name);
  1266. /*
  1267. * hwmods with HWMOD_INIT_NO_IDLE flag set are left
  1268. * in enabled state at init.
  1269. * Now that someone is really trying to enable them,
  1270. * just ensure that the hwmod mux is set.
  1271. */
  1272. if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
  1273. /*
  1274. * If the caller has mux data populated, do the mux'ing
  1275. * which wouldn't have been done as part of the _enable()
  1276. * done during setup.
  1277. */
  1278. if (oh->mux)
  1279. omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
  1280. oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
  1281. return 0;
  1282. }
  1283. if (oh->_state != _HWMOD_STATE_INITIALIZED &&
  1284. oh->_state != _HWMOD_STATE_IDLE &&
  1285. oh->_state != _HWMOD_STATE_DISABLED) {
  1286. WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
  1287. oh->name);
  1288. return -EINVAL;
  1289. }
  1290. /*
  1291. * If an IP contains only one HW reset line, then de-assert it in order
  1292. * to allow the module state transition. Otherwise the PRCM will return
  1293. * Intransition status, and the init will failed.
  1294. */
  1295. if ((oh->_state == _HWMOD_STATE_INITIALIZED ||
  1296. oh->_state == _HWMOD_STATE_DISABLED) && oh->rst_lines_cnt == 1)
  1297. _deassert_hardreset(oh, oh->rst_lines[0].name);
  1298. /* Mux pins for device runtime if populated */
  1299. if (oh->mux && (!oh->mux->enabled ||
  1300. ((oh->_state == _HWMOD_STATE_IDLE) &&
  1301. oh->mux->pads_dynamic)))
  1302. omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
  1303. _add_initiator_dep(oh, mpu_oh);
  1304. if (oh->clkdm) {
  1305. /*
  1306. * A clockdomain must be in SW_SUP before enabling
  1307. * completely the module. The clockdomain can be set
  1308. * in HW_AUTO only when the module become ready.
  1309. */
  1310. hwsup = clkdm_in_hwsup(oh->clkdm);
  1311. r = clkdm_hwmod_enable(oh->clkdm, oh);
  1312. if (r) {
  1313. WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
  1314. oh->name, oh->clkdm->name, r);
  1315. return r;
  1316. }
  1317. }
  1318. _enable_clocks(oh);
  1319. _enable_module(oh);
  1320. r = _wait_target_ready(oh);
  1321. if (!r) {
  1322. /*
  1323. * Set the clockdomain to HW_AUTO only if the target is ready,
  1324. * assuming that the previous state was HW_AUTO
  1325. */
  1326. if (oh->clkdm && hwsup)
  1327. clkdm_allow_idle(oh->clkdm);
  1328. oh->_state = _HWMOD_STATE_ENABLED;
  1329. /* Access the sysconfig only if the target is ready */
  1330. if (oh->class->sysc) {
  1331. if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
  1332. _update_sysc_cache(oh);
  1333. _enable_sysc(oh);
  1334. }
  1335. } else {
  1336. _disable_clocks(oh);
  1337. pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
  1338. oh->name, r);
  1339. if (oh->clkdm)
  1340. clkdm_hwmod_disable(oh->clkdm, oh);
  1341. }
  1342. return r;
  1343. }
  1344. /**
  1345. * _idle - idle an omap_hwmod
  1346. * @oh: struct omap_hwmod *
  1347. *
  1348. * Idles an omap_hwmod @oh. This should be called once the hwmod has
  1349. * no further work. Returns -EINVAL if the hwmod is in the wrong
  1350. * state or returns 0.
  1351. */
  1352. static int _idle(struct omap_hwmod *oh)
  1353. {
  1354. pr_debug("omap_hwmod: %s: idling\n", oh->name);
  1355. if (oh->_state != _HWMOD_STATE_ENABLED) {
  1356. WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
  1357. oh->name);
  1358. return -EINVAL;
  1359. }
  1360. if (oh->class->sysc)
  1361. _idle_sysc(oh);
  1362. _del_initiator_dep(oh, mpu_oh);
  1363. _omap4_disable_module(oh);
  1364. /*
  1365. * The module must be in idle mode before disabling any parents
  1366. * clocks. Otherwise, the parent clock might be disabled before
  1367. * the module transition is done, and thus will prevent the
  1368. * transition to complete properly.
  1369. */
  1370. _disable_clocks(oh);
  1371. if (oh->clkdm)
  1372. clkdm_hwmod_disable(oh->clkdm, oh);
  1373. /* Mux pins for device idle if populated */
  1374. if (oh->mux && oh->mux->pads_dynamic)
  1375. omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
  1376. oh->_state = _HWMOD_STATE_IDLE;
  1377. return 0;
  1378. }
  1379. /**
  1380. * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
  1381. * @oh: struct omap_hwmod *
  1382. * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
  1383. *
  1384. * Sets the IP block's OCP autoidle bit in hardware, and updates our
  1385. * local copy. Intended to be used by drivers that require
  1386. * direct manipulation of the AUTOIDLE bits.
  1387. * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
  1388. * along the return value from _set_module_autoidle().
  1389. *
  1390. * Any users of this function should be scrutinized carefully.
  1391. */
  1392. int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
  1393. {
  1394. u32 v;
  1395. int retval = 0;
  1396. unsigned long flags;
  1397. if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
  1398. return -EINVAL;
  1399. spin_lock_irqsave(&oh->_lock, flags);
  1400. v = oh->_sysc_cache;
  1401. retval = _set_module_autoidle(oh, autoidle, &v);
  1402. if (!retval)
  1403. _write_sysconfig(v, oh);
  1404. spin_unlock_irqrestore(&oh->_lock, flags);
  1405. return retval;
  1406. }
  1407. /**
  1408. * _shutdown - shutdown an omap_hwmod
  1409. * @oh: struct omap_hwmod *
  1410. *
  1411. * Shut down an omap_hwmod @oh. This should be called when the driver
  1412. * used for the hwmod is removed or unloaded or if the driver is not
  1413. * used by the system. Returns -EINVAL if the hwmod is in the wrong
  1414. * state or returns 0.
  1415. */
  1416. static int _shutdown(struct omap_hwmod *oh)
  1417. {
  1418. int ret;
  1419. u8 prev_state;
  1420. if (oh->_state != _HWMOD_STATE_IDLE &&
  1421. oh->_state != _HWMOD_STATE_ENABLED) {
  1422. WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
  1423. oh->name);
  1424. return -EINVAL;
  1425. }
  1426. pr_debug("omap_hwmod: %s: disabling\n", oh->name);
  1427. if (oh->class->pre_shutdown) {
  1428. prev_state = oh->_state;
  1429. if (oh->_state == _HWMOD_STATE_IDLE)
  1430. _enable(oh);
  1431. ret = oh->class->pre_shutdown(oh);
  1432. if (ret) {
  1433. if (prev_state == _HWMOD_STATE_IDLE)
  1434. _idle(oh);
  1435. return ret;
  1436. }
  1437. }
  1438. if (oh->class->sysc) {
  1439. if (oh->_state == _HWMOD_STATE_IDLE)
  1440. _enable(oh);
  1441. _shutdown_sysc(oh);
  1442. }
  1443. /* clocks and deps are already disabled in idle */
  1444. if (oh->_state == _HWMOD_STATE_ENABLED) {
  1445. _del_initiator_dep(oh, mpu_oh);
  1446. /* XXX what about the other system initiators here? dma, dsp */
  1447. _omap4_disable_module(oh);
  1448. _disable_clocks(oh);
  1449. if (oh->clkdm)
  1450. clkdm_hwmod_disable(oh->clkdm, oh);
  1451. }
  1452. /* XXX Should this code also force-disable the optional clocks? */
  1453. /*
  1454. * If an IP contains only one HW reset line, then assert it
  1455. * after disabling the clocks and before shutting down the IP.
  1456. */
  1457. if (oh->rst_lines_cnt == 1)
  1458. _assert_hardreset(oh, oh->rst_lines[0].name);
  1459. /* Mux pins to safe mode or use populated off mode values */
  1460. if (oh->mux)
  1461. omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
  1462. oh->_state = _HWMOD_STATE_DISABLED;
  1463. return 0;
  1464. }
  1465. /**
  1466. * _setup - do initial configuration of omap_hwmod
  1467. * @oh: struct omap_hwmod *
  1468. *
  1469. * Writes the CLOCKACTIVITY bits @clockact to the hwmod @oh
  1470. * OCP_SYSCONFIG register. Returns 0.
  1471. */
  1472. static int _setup(struct omap_hwmod *oh, void *data)
  1473. {
  1474. int i, r;
  1475. u8 postsetup_state;
  1476. if (oh->_state != _HWMOD_STATE_CLKS_INITED)
  1477. return 0;
  1478. /* Set iclk autoidle mode */
  1479. if (oh->slaves_cnt > 0) {
  1480. for (i = 0; i < oh->slaves_cnt; i++) {
  1481. struct omap_hwmod_ocp_if *os = oh->slaves[i];
  1482. struct clk *c = os->_clk;
  1483. if (!c)
  1484. continue;
  1485. if (os->flags & OCPIF_SWSUP_IDLE) {
  1486. /* XXX omap_iclk_deny_idle(c); */
  1487. } else {
  1488. /* XXX omap_iclk_allow_idle(c); */
  1489. clk_enable(c);
  1490. }
  1491. }
  1492. }
  1493. oh->_state = _HWMOD_STATE_INITIALIZED;
  1494. /*
  1495. * In the case of hwmod with hardreset that should not be
  1496. * de-assert at boot time, we have to keep the module
  1497. * initialized, because we cannot enable it properly with the
  1498. * reset asserted. Exit without warning because that behavior is
  1499. * expected.
  1500. */
  1501. if ((oh->flags & HWMOD_INIT_NO_RESET) && oh->rst_lines_cnt == 1)
  1502. return 0;
  1503. r = _enable(oh);
  1504. if (r) {
  1505. pr_warning("omap_hwmod: %s: cannot be enabled (%d)\n",
  1506. oh->name, oh->_state);
  1507. return 0;
  1508. }
  1509. if (!(oh->flags & HWMOD_INIT_NO_RESET)) {
  1510. _reset(oh);
  1511. /*
  1512. * OCP_SYSCONFIG bits need to be reprogrammed after a softreset.
  1513. * The _enable() function should be split to
  1514. * avoid the rewrite of the OCP_SYSCONFIG register.
  1515. */
  1516. if (oh->class->sysc) {
  1517. _update_sysc_cache(oh);
  1518. _enable_sysc(oh);
  1519. }
  1520. }
  1521. postsetup_state = oh->_postsetup_state;
  1522. if (postsetup_state == _HWMOD_STATE_UNKNOWN)
  1523. postsetup_state = _HWMOD_STATE_ENABLED;
  1524. /*
  1525. * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
  1526. * it should be set by the core code as a runtime flag during startup
  1527. */
  1528. if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
  1529. (postsetup_state == _HWMOD_STATE_IDLE)) {
  1530. oh->_int_flags |= _HWMOD_SKIP_ENABLE;
  1531. postsetup_state = _HWMOD_STATE_ENABLED;
  1532. }
  1533. if (postsetup_state == _HWMOD_STATE_IDLE)
  1534. _idle(oh);
  1535. else if (postsetup_state == _HWMOD_STATE_DISABLED)
  1536. _shutdown(oh);
  1537. else if (postsetup_state != _HWMOD_STATE_ENABLED)
  1538. WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
  1539. oh->name, postsetup_state);
  1540. return 0;
  1541. }
  1542. /**
  1543. * _register - register a struct omap_hwmod
  1544. * @oh: struct omap_hwmod *
  1545. *
  1546. * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
  1547. * already has been registered by the same name; -EINVAL if the
  1548. * omap_hwmod is in the wrong state, if @oh is NULL, if the
  1549. * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
  1550. * name, or if the omap_hwmod's class is missing a name; or 0 upon
  1551. * success.
  1552. *
  1553. * XXX The data should be copied into bootmem, so the original data
  1554. * should be marked __initdata and freed after init. This would allow
  1555. * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
  1556. * that the copy process would be relatively complex due to the large number
  1557. * of substructures.
  1558. */
  1559. static int __init _register(struct omap_hwmod *oh)
  1560. {
  1561. int ms_id;
  1562. if (!oh || !oh->name || !oh->class || !oh->class->name ||
  1563. (oh->_state != _HWMOD_STATE_UNKNOWN))
  1564. return -EINVAL;
  1565. pr_debug("omap_hwmod: %s: registering\n", oh->name);
  1566. if (_lookup(oh->name))
  1567. return -EEXIST;
  1568. ms_id = _find_mpu_port_index(oh);
  1569. if (!IS_ERR_VALUE(ms_id))
  1570. oh->_mpu_port_index = ms_id;
  1571. else
  1572. oh->_int_flags |= _HWMOD_NO_MPU_PORT;
  1573. list_add_tail(&oh->node, &omap_hwmod_list);
  1574. spin_lock_init(&oh->_lock);
  1575. oh->_state = _HWMOD_STATE_REGISTERED;
  1576. /*
  1577. * XXX Rather than doing a strcmp(), this should test a flag
  1578. * set in the hwmod data, inserted by the autogenerator code.
  1579. */
  1580. if (!strcmp(oh->name, MPU_INITIATOR_NAME))
  1581. mpu_oh = oh;
  1582. return 0;
  1583. }
  1584. /* Public functions */
  1585. u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
  1586. {
  1587. if (oh->flags & HWMOD_16BIT_REG)
  1588. return __raw_readw(oh->_mpu_rt_va + reg_offs);
  1589. else
  1590. return __raw_readl(oh->_mpu_rt_va + reg_offs);
  1591. }
  1592. void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
  1593. {
  1594. if (oh->flags & HWMOD_16BIT_REG)
  1595. __raw_writew(v, oh->_mpu_rt_va + reg_offs);
  1596. else
  1597. __raw_writel(v, oh->_mpu_rt_va + reg_offs);
  1598. }
  1599. /**
  1600. * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
  1601. * @oh: struct omap_hwmod *
  1602. *
  1603. * This is a public function exposed to drivers. Some drivers may need to do
  1604. * some settings before and after resetting the device. Those drivers after
  1605. * doing the necessary settings could use this function to start a reset by
  1606. * setting the SYSCONFIG.SOFTRESET bit.
  1607. */
  1608. int omap_hwmod_softreset(struct omap_hwmod *oh)
  1609. {
  1610. u32 v;
  1611. int ret;
  1612. if (!oh || !(oh->_sysc_cache))
  1613. return -EINVAL;
  1614. v = oh->_sysc_cache;
  1615. ret = _set_softreset(oh, &v);
  1616. if (ret)
  1617. goto error;
  1618. _write_sysconfig(v, oh);
  1619. error:
  1620. return ret;
  1621. }
  1622. /**
  1623. * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
  1624. * @oh: struct omap_hwmod *
  1625. * @idlemode: SIDLEMODE field bits (shifted to bit 0)
  1626. *
  1627. * Sets the IP block's OCP slave idlemode in hardware, and updates our
  1628. * local copy. Intended to be used by drivers that have some erratum
  1629. * that requires direct manipulation of the SIDLEMODE bits. Returns
  1630. * -EINVAL if @oh is null, or passes along the return value from
  1631. * _set_slave_idlemode().
  1632. *
  1633. * XXX Does this function have any current users? If not, we should
  1634. * remove it; it is better to let the rest of the hwmod code handle this.
  1635. * Any users of this function should be scrutinized carefully.
  1636. */
  1637. int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
  1638. {
  1639. u32 v;
  1640. int retval = 0;
  1641. if (!oh)
  1642. return -EINVAL;
  1643. v = oh->_sysc_cache;
  1644. retval = _set_slave_idlemode(oh, idlemode, &v);
  1645. if (!retval)
  1646. _write_sysconfig(v, oh);
  1647. return retval;
  1648. }
  1649. /**
  1650. * omap_hwmod_lookup - look up a registered omap_hwmod by name
  1651. * @name: name of the omap_hwmod to look up
  1652. *
  1653. * Given a @name of an omap_hwmod, return a pointer to the registered
  1654. * struct omap_hwmod *, or NULL upon error.
  1655. */
  1656. struct omap_hwmod *omap_hwmod_lookup(const char *name)
  1657. {
  1658. struct omap_hwmod *oh;
  1659. if (!name)
  1660. return NULL;
  1661. oh = _lookup(name);
  1662. return oh;
  1663. }
  1664. /**
  1665. * omap_hwmod_for_each - call function for each registered omap_hwmod
  1666. * @fn: pointer to a callback function
  1667. * @data: void * data to pass to callback function
  1668. *
  1669. * Call @fn for each registered omap_hwmod, passing @data to each
  1670. * function. @fn must return 0 for success or any other value for
  1671. * failure. If @fn returns non-zero, the iteration across omap_hwmods
  1672. * will stop and the non-zero return value will be passed to the
  1673. * caller of omap_hwmod_for_each(). @fn is called with
  1674. * omap_hwmod_for_each() held.
  1675. */
  1676. int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
  1677. void *data)
  1678. {
  1679. struct omap_hwmod *temp_oh;
  1680. int ret = 0;
  1681. if (!fn)
  1682. return -EINVAL;
  1683. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  1684. ret = (*fn)(temp_oh, data);
  1685. if (ret)
  1686. break;
  1687. }
  1688. return ret;
  1689. }
  1690. /**
  1691. * omap_hwmod_register - register an array of hwmods
  1692. * @ohs: pointer to an array of omap_hwmods to register
  1693. *
  1694. * Intended to be called early in boot before the clock framework is
  1695. * initialized. If @ohs is not null, will register all omap_hwmods
  1696. * listed in @ohs that are valid for this chip. Returns 0.
  1697. */
  1698. int __init omap_hwmod_register(struct omap_hwmod **ohs)
  1699. {
  1700. int r, i;
  1701. if (!ohs)
  1702. return 0;
  1703. i = 0;
  1704. do {
  1705. r = _register(ohs[i]);
  1706. WARN(r, "omap_hwmod: %s: _register returned %d\n", ohs[i]->name,
  1707. r);
  1708. } while (ohs[++i]);
  1709. return 0;
  1710. }
  1711. /*
  1712. * _populate_mpu_rt_base - populate the virtual address for a hwmod
  1713. *
  1714. * Must be called only from omap_hwmod_setup_*() so ioremap works properly.
  1715. * Assumes the caller takes care of locking if needed.
  1716. */
  1717. static int __init _populate_mpu_rt_base(struct omap_hwmod *oh, void *data)
  1718. {
  1719. if (oh->_state != _HWMOD_STATE_REGISTERED)
  1720. return 0;
  1721. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  1722. return 0;
  1723. oh->_mpu_rt_va = _find_mpu_rt_base(oh, oh->_mpu_port_index);
  1724. return 0;
  1725. }
  1726. /**
  1727. * omap_hwmod_setup_one - set up a single hwmod
  1728. * @oh_name: const char * name of the already-registered hwmod to set up
  1729. *
  1730. * Must be called after omap2_clk_init(). Resolves the struct clk
  1731. * names to struct clk pointers for each registered omap_hwmod. Also
  1732. * calls _setup() on each hwmod. Returns -EINVAL upon error or 0 upon
  1733. * success.
  1734. */
  1735. int __init omap_hwmod_setup_one(const char *oh_name)
  1736. {
  1737. struct omap_hwmod *oh;
  1738. int r;
  1739. pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
  1740. if (!mpu_oh) {
  1741. pr_err("omap_hwmod: %s: cannot setup_one: MPU initiator hwmod %s not yet registered\n",
  1742. oh_name, MPU_INITIATOR_NAME);
  1743. return -EINVAL;
  1744. }
  1745. oh = _lookup(oh_name);
  1746. if (!oh) {
  1747. WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
  1748. return -EINVAL;
  1749. }
  1750. if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
  1751. omap_hwmod_setup_one(MPU_INITIATOR_NAME);
  1752. r = _populate_mpu_rt_base(oh, NULL);
  1753. if (IS_ERR_VALUE(r)) {
  1754. WARN(1, "omap_hwmod: %s: couldn't set mpu_rt_base\n", oh_name);
  1755. return -EINVAL;
  1756. }
  1757. r = _init_clocks(oh, NULL);
  1758. if (IS_ERR_VALUE(r)) {
  1759. WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh_name);
  1760. return -EINVAL;
  1761. }
  1762. _setup(oh, NULL);
  1763. return 0;
  1764. }
  1765. /**
  1766. * omap_hwmod_setup - do some post-clock framework initialization
  1767. *
  1768. * Must be called after omap2_clk_init(). Resolves the struct clk names
  1769. * to struct clk pointers for each registered omap_hwmod. Also calls
  1770. * _setup() on each hwmod. Returns 0 upon success.
  1771. */
  1772. static int __init omap_hwmod_setup_all(void)
  1773. {
  1774. int r;
  1775. if (!mpu_oh) {
  1776. pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
  1777. __func__, MPU_INITIATOR_NAME);
  1778. return -EINVAL;
  1779. }
  1780. r = omap_hwmod_for_each(_populate_mpu_rt_base, NULL);
  1781. r = omap_hwmod_for_each(_init_clocks, NULL);
  1782. WARN(IS_ERR_VALUE(r),
  1783. "omap_hwmod: %s: _init_clocks failed\n", __func__);
  1784. omap_hwmod_for_each(_setup, NULL);
  1785. return 0;
  1786. }
  1787. core_initcall(omap_hwmod_setup_all);
  1788. /**
  1789. * omap_hwmod_enable - enable an omap_hwmod
  1790. * @oh: struct omap_hwmod *
  1791. *
  1792. * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
  1793. * Returns -EINVAL on error or passes along the return value from _enable().
  1794. */
  1795. int omap_hwmod_enable(struct omap_hwmod *oh)
  1796. {
  1797. int r;
  1798. unsigned long flags;
  1799. if (!oh)
  1800. return -EINVAL;
  1801. spin_lock_irqsave(&oh->_lock, flags);
  1802. r = _enable(oh);
  1803. spin_unlock_irqrestore(&oh->_lock, flags);
  1804. return r;
  1805. }
  1806. /**
  1807. * omap_hwmod_idle - idle an omap_hwmod
  1808. * @oh: struct omap_hwmod *
  1809. *
  1810. * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
  1811. * Returns -EINVAL on error or passes along the return value from _idle().
  1812. */
  1813. int omap_hwmod_idle(struct omap_hwmod *oh)
  1814. {
  1815. unsigned long flags;
  1816. if (!oh)
  1817. return -EINVAL;
  1818. spin_lock_irqsave(&oh->_lock, flags);
  1819. _idle(oh);
  1820. spin_unlock_irqrestore(&oh->_lock, flags);
  1821. return 0;
  1822. }
  1823. /**
  1824. * omap_hwmod_shutdown - shutdown an omap_hwmod
  1825. * @oh: struct omap_hwmod *
  1826. *
  1827. * Shutdown an omap_hwmod @oh. Intended to be called by
  1828. * omap_device_shutdown(). Returns -EINVAL on error or passes along
  1829. * the return value from _shutdown().
  1830. */
  1831. int omap_hwmod_shutdown(struct omap_hwmod *oh)
  1832. {
  1833. unsigned long flags;
  1834. if (!oh)
  1835. return -EINVAL;
  1836. spin_lock_irqsave(&oh->_lock, flags);
  1837. _shutdown(oh);
  1838. spin_unlock_irqrestore(&oh->_lock, flags);
  1839. return 0;
  1840. }
  1841. /**
  1842. * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
  1843. * @oh: struct omap_hwmod *oh
  1844. *
  1845. * Intended to be called by the omap_device code.
  1846. */
  1847. int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
  1848. {
  1849. unsigned long flags;
  1850. spin_lock_irqsave(&oh->_lock, flags);
  1851. _enable_clocks(oh);
  1852. spin_unlock_irqrestore(&oh->_lock, flags);
  1853. return 0;
  1854. }
  1855. /**
  1856. * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
  1857. * @oh: struct omap_hwmod *oh
  1858. *
  1859. * Intended to be called by the omap_device code.
  1860. */
  1861. int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
  1862. {
  1863. unsigned long flags;
  1864. spin_lock_irqsave(&oh->_lock, flags);
  1865. _disable_clocks(oh);
  1866. spin_unlock_irqrestore(&oh->_lock, flags);
  1867. return 0;
  1868. }
  1869. /**
  1870. * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
  1871. * @oh: struct omap_hwmod *oh
  1872. *
  1873. * Intended to be called by drivers and core code when all posted
  1874. * writes to a device must complete before continuing further
  1875. * execution (for example, after clearing some device IRQSTATUS
  1876. * register bits)
  1877. *
  1878. * XXX what about targets with multiple OCP threads?
  1879. */
  1880. void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
  1881. {
  1882. BUG_ON(!oh);
  1883. if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
  1884. WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
  1885. oh->name);
  1886. return;
  1887. }
  1888. /*
  1889. * Forces posted writes to complete on the OCP thread handling
  1890. * register writes
  1891. */
  1892. omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
  1893. }
  1894. /**
  1895. * omap_hwmod_reset - reset the hwmod
  1896. * @oh: struct omap_hwmod *
  1897. *
  1898. * Under some conditions, a driver may wish to reset the entire device.
  1899. * Called from omap_device code. Returns -EINVAL on error or passes along
  1900. * the return value from _reset().
  1901. */
  1902. int omap_hwmod_reset(struct omap_hwmod *oh)
  1903. {
  1904. int r;
  1905. unsigned long flags;
  1906. if (!oh)
  1907. return -EINVAL;
  1908. spin_lock_irqsave(&oh->_lock, flags);
  1909. r = _reset(oh);
  1910. spin_unlock_irqrestore(&oh->_lock, flags);
  1911. return r;
  1912. }
  1913. /**
  1914. * omap_hwmod_count_resources - count number of struct resources needed by hwmod
  1915. * @oh: struct omap_hwmod *
  1916. * @res: pointer to the first element of an array of struct resource to fill
  1917. *
  1918. * Count the number of struct resource array elements necessary to
  1919. * contain omap_hwmod @oh resources. Intended to be called by code
  1920. * that registers omap_devices. Intended to be used to determine the
  1921. * size of a dynamically-allocated struct resource array, before
  1922. * calling omap_hwmod_fill_resources(). Returns the number of struct
  1923. * resource array elements needed.
  1924. *
  1925. * XXX This code is not optimized. It could attempt to merge adjacent
  1926. * resource IDs.
  1927. *
  1928. */
  1929. int omap_hwmod_count_resources(struct omap_hwmod *oh)
  1930. {
  1931. int ret, i;
  1932. ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
  1933. for (i = 0; i < oh->slaves_cnt; i++)
  1934. ret += _count_ocp_if_addr_spaces(oh->slaves[i]);
  1935. return ret;
  1936. }
  1937. /**
  1938. * omap_hwmod_fill_resources - fill struct resource array with hwmod data
  1939. * @oh: struct omap_hwmod *
  1940. * @res: pointer to the first element of an array of struct resource to fill
  1941. *
  1942. * Fill the struct resource array @res with resource data from the
  1943. * omap_hwmod @oh. Intended to be called by code that registers
  1944. * omap_devices. See also omap_hwmod_count_resources(). Returns the
  1945. * number of array elements filled.
  1946. */
  1947. int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
  1948. {
  1949. int i, j, mpu_irqs_cnt, sdma_reqs_cnt;
  1950. int r = 0;
  1951. /* For each IRQ, DMA, memory area, fill in array.*/
  1952. mpu_irqs_cnt = _count_mpu_irqs(oh);
  1953. for (i = 0; i < mpu_irqs_cnt; i++) {
  1954. (res + r)->name = (oh->mpu_irqs + i)->name;
  1955. (res + r)->start = (oh->mpu_irqs + i)->irq;
  1956. (res + r)->end = (oh->mpu_irqs + i)->irq;
  1957. (res + r)->flags = IORESOURCE_IRQ;
  1958. r++;
  1959. }
  1960. sdma_reqs_cnt = _count_sdma_reqs(oh);
  1961. for (i = 0; i < sdma_reqs_cnt; i++) {
  1962. (res + r)->name = (oh->sdma_reqs + i)->name;
  1963. (res + r)->start = (oh->sdma_reqs + i)->dma_req;
  1964. (res + r)->end = (oh->sdma_reqs + i)->dma_req;
  1965. (res + r)->flags = IORESOURCE_DMA;
  1966. r++;
  1967. }
  1968. for (i = 0; i < oh->slaves_cnt; i++) {
  1969. struct omap_hwmod_ocp_if *os;
  1970. int addr_cnt;
  1971. os = oh->slaves[i];
  1972. addr_cnt = _count_ocp_if_addr_spaces(os);
  1973. for (j = 0; j < addr_cnt; j++) {
  1974. (res + r)->name = (os->addr + j)->name;
  1975. (res + r)->start = (os->addr + j)->pa_start;
  1976. (res + r)->end = (os->addr + j)->pa_end;
  1977. (res + r)->flags = IORESOURCE_MEM;
  1978. r++;
  1979. }
  1980. }
  1981. return r;
  1982. }
  1983. /**
  1984. * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
  1985. * @oh: struct omap_hwmod *
  1986. *
  1987. * Return the powerdomain pointer associated with the OMAP module
  1988. * @oh's main clock. If @oh does not have a main clk, return the
  1989. * powerdomain associated with the interface clock associated with the
  1990. * module's MPU port. (XXX Perhaps this should use the SDMA port
  1991. * instead?) Returns NULL on error, or a struct powerdomain * on
  1992. * success.
  1993. */
  1994. struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
  1995. {
  1996. struct clk *c;
  1997. if (!oh)
  1998. return NULL;
  1999. if (oh->_clk) {
  2000. c = oh->_clk;
  2001. } else {
  2002. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  2003. return NULL;
  2004. c = oh->slaves[oh->_mpu_port_index]->_clk;
  2005. }
  2006. if (!c->clkdm)
  2007. return NULL;
  2008. return c->clkdm->pwrdm.ptr;
  2009. }
  2010. /**
  2011. * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
  2012. * @oh: struct omap_hwmod *
  2013. *
  2014. * Returns the virtual address corresponding to the beginning of the
  2015. * module's register target, in the address range that is intended to
  2016. * be used by the MPU. Returns the virtual address upon success or NULL
  2017. * upon error.
  2018. */
  2019. void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
  2020. {
  2021. if (!oh)
  2022. return NULL;
  2023. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  2024. return NULL;
  2025. if (oh->_state == _HWMOD_STATE_UNKNOWN)
  2026. return NULL;
  2027. return oh->_mpu_rt_va;
  2028. }
  2029. /**
  2030. * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
  2031. * @oh: struct omap_hwmod *
  2032. * @init_oh: struct omap_hwmod * (initiator)
  2033. *
  2034. * Add a sleep dependency between the initiator @init_oh and @oh.
  2035. * Intended to be called by DSP/Bridge code via platform_data for the
  2036. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  2037. * code needs to add/del initiator dependencies dynamically
  2038. * before/after accessing a device. Returns the return value from
  2039. * _add_initiator_dep().
  2040. *
  2041. * XXX Keep a usecount in the clockdomain code
  2042. */
  2043. int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
  2044. struct omap_hwmod *init_oh)
  2045. {
  2046. return _add_initiator_dep(oh, init_oh);
  2047. }
  2048. /*
  2049. * XXX what about functions for drivers to save/restore ocp_sysconfig
  2050. * for context save/restore operations?
  2051. */
  2052. /**
  2053. * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
  2054. * @oh: struct omap_hwmod *
  2055. * @init_oh: struct omap_hwmod * (initiator)
  2056. *
  2057. * Remove a sleep dependency between the initiator @init_oh and @oh.
  2058. * Intended to be called by DSP/Bridge code via platform_data for the
  2059. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  2060. * code needs to add/del initiator dependencies dynamically
  2061. * before/after accessing a device. Returns the return value from
  2062. * _del_initiator_dep().
  2063. *
  2064. * XXX Keep a usecount in the clockdomain code
  2065. */
  2066. int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
  2067. struct omap_hwmod *init_oh)
  2068. {
  2069. return _del_initiator_dep(oh, init_oh);
  2070. }
  2071. /**
  2072. * omap_hwmod_enable_wakeup - allow device to wake up the system
  2073. * @oh: struct omap_hwmod *
  2074. *
  2075. * Sets the module OCP socket ENAWAKEUP bit to allow the module to
  2076. * send wakeups to the PRCM. Eventually this should sets PRCM wakeup
  2077. * registers to cause the PRCM to receive wakeup events from the
  2078. * module. Does not set any wakeup routing registers beyond this
  2079. * point - if the module is to wake up any other module or subsystem,
  2080. * that must be set separately. Called by omap_device code. Returns
  2081. * -EINVAL on error or 0 upon success.
  2082. */
  2083. int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
  2084. {
  2085. unsigned long flags;
  2086. u32 v;
  2087. if (!oh->class->sysc ||
  2088. !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
  2089. return -EINVAL;
  2090. spin_lock_irqsave(&oh->_lock, flags);
  2091. v = oh->_sysc_cache;
  2092. _enable_wakeup(oh, &v);
  2093. _write_sysconfig(v, oh);
  2094. _set_idle_ioring_wakeup(oh, true);
  2095. spin_unlock_irqrestore(&oh->_lock, flags);
  2096. return 0;
  2097. }
  2098. /**
  2099. * omap_hwmod_disable_wakeup - prevent device from waking the system
  2100. * @oh: struct omap_hwmod *
  2101. *
  2102. * Clears the module OCP socket ENAWAKEUP bit to prevent the module
  2103. * from sending wakeups to the PRCM. Eventually this should clear
  2104. * PRCM wakeup registers to cause the PRCM to ignore wakeup events
  2105. * from the module. Does not set any wakeup routing registers beyond
  2106. * this point - if the module is to wake up any other module or
  2107. * subsystem, that must be set separately. Called by omap_device
  2108. * code. Returns -EINVAL on error or 0 upon success.
  2109. */
  2110. int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
  2111. {
  2112. unsigned long flags;
  2113. u32 v;
  2114. if (!oh->class->sysc ||
  2115. !(oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP))
  2116. return -EINVAL;
  2117. spin_lock_irqsave(&oh->_lock, flags);
  2118. v = oh->_sysc_cache;
  2119. _disable_wakeup(oh, &v);
  2120. _write_sysconfig(v, oh);
  2121. _set_idle_ioring_wakeup(oh, false);
  2122. spin_unlock_irqrestore(&oh->_lock, flags);
  2123. return 0;
  2124. }
  2125. /**
  2126. * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
  2127. * contained in the hwmod module.
  2128. * @oh: struct omap_hwmod *
  2129. * @name: name of the reset line to lookup and assert
  2130. *
  2131. * Some IP like dsp, ipu or iva contain processor that require
  2132. * an HW reset line to be assert / deassert in order to enable fully
  2133. * the IP. Returns -EINVAL if @oh is null or if the operation is not
  2134. * yet supported on this OMAP; otherwise, passes along the return value
  2135. * from _assert_hardreset().
  2136. */
  2137. int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
  2138. {
  2139. int ret;
  2140. unsigned long flags;
  2141. if (!oh)
  2142. return -EINVAL;
  2143. spin_lock_irqsave(&oh->_lock, flags);
  2144. ret = _assert_hardreset(oh, name);
  2145. spin_unlock_irqrestore(&oh->_lock, flags);
  2146. return ret;
  2147. }
  2148. /**
  2149. * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
  2150. * contained in the hwmod module.
  2151. * @oh: struct omap_hwmod *
  2152. * @name: name of the reset line to look up and deassert
  2153. *
  2154. * Some IP like dsp, ipu or iva contain processor that require
  2155. * an HW reset line to be assert / deassert in order to enable fully
  2156. * the IP. Returns -EINVAL if @oh is null or if the operation is not
  2157. * yet supported on this OMAP; otherwise, passes along the return value
  2158. * from _deassert_hardreset().
  2159. */
  2160. int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
  2161. {
  2162. int ret;
  2163. unsigned long flags;
  2164. if (!oh)
  2165. return -EINVAL;
  2166. spin_lock_irqsave(&oh->_lock, flags);
  2167. ret = _deassert_hardreset(oh, name);
  2168. spin_unlock_irqrestore(&oh->_lock, flags);
  2169. return ret;
  2170. }
  2171. /**
  2172. * omap_hwmod_read_hardreset - read the HW reset line state of submodules
  2173. * contained in the hwmod module
  2174. * @oh: struct omap_hwmod *
  2175. * @name: name of the reset line to look up and read
  2176. *
  2177. * Return the current state of the hwmod @oh's reset line named @name:
  2178. * returns -EINVAL upon parameter error or if this operation
  2179. * is unsupported on the current OMAP; otherwise, passes along the return
  2180. * value from _read_hardreset().
  2181. */
  2182. int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
  2183. {
  2184. int ret;
  2185. unsigned long flags;
  2186. if (!oh)
  2187. return -EINVAL;
  2188. spin_lock_irqsave(&oh->_lock, flags);
  2189. ret = _read_hardreset(oh, name);
  2190. spin_unlock_irqrestore(&oh->_lock, flags);
  2191. return ret;
  2192. }
  2193. /**
  2194. * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
  2195. * @classname: struct omap_hwmod_class name to search for
  2196. * @fn: callback function pointer to call for each hwmod in class @classname
  2197. * @user: arbitrary context data to pass to the callback function
  2198. *
  2199. * For each omap_hwmod of class @classname, call @fn.
  2200. * If the callback function returns something other than
  2201. * zero, the iterator is terminated, and the callback function's return
  2202. * value is passed back to the caller. Returns 0 upon success, -EINVAL
  2203. * if @classname or @fn are NULL, or passes back the error code from @fn.
  2204. */
  2205. int omap_hwmod_for_each_by_class(const char *classname,
  2206. int (*fn)(struct omap_hwmod *oh,
  2207. void *user),
  2208. void *user)
  2209. {
  2210. struct omap_hwmod *temp_oh;
  2211. int ret = 0;
  2212. if (!classname || !fn)
  2213. return -EINVAL;
  2214. pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
  2215. __func__, classname);
  2216. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  2217. if (!strcmp(temp_oh->class->name, classname)) {
  2218. pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
  2219. __func__, temp_oh->name);
  2220. ret = (*fn)(temp_oh, user);
  2221. if (ret)
  2222. break;
  2223. }
  2224. }
  2225. if (ret)
  2226. pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
  2227. __func__, ret);
  2228. return ret;
  2229. }
  2230. /**
  2231. * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
  2232. * @oh: struct omap_hwmod *
  2233. * @state: state that _setup() should leave the hwmod in
  2234. *
  2235. * Sets the hwmod state that @oh will enter at the end of _setup()
  2236. * (called by omap_hwmod_setup_*()). Only valid to call between
  2237. * calling omap_hwmod_register() and omap_hwmod_setup_*(). Returns
  2238. * 0 upon success or -EINVAL if there is a problem with the arguments
  2239. * or if the hwmod is in the wrong state.
  2240. */
  2241. int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
  2242. {
  2243. int ret;
  2244. unsigned long flags;
  2245. if (!oh)
  2246. return -EINVAL;
  2247. if (state != _HWMOD_STATE_DISABLED &&
  2248. state != _HWMOD_STATE_ENABLED &&
  2249. state != _HWMOD_STATE_IDLE)
  2250. return -EINVAL;
  2251. spin_lock_irqsave(&oh->_lock, flags);
  2252. if (oh->_state != _HWMOD_STATE_REGISTERED) {
  2253. ret = -EINVAL;
  2254. goto ohsps_unlock;
  2255. }
  2256. oh->_postsetup_state = state;
  2257. ret = 0;
  2258. ohsps_unlock:
  2259. spin_unlock_irqrestore(&oh->_lock, flags);
  2260. return ret;
  2261. }
  2262. /**
  2263. * omap_hwmod_get_context_loss_count - get lost context count
  2264. * @oh: struct omap_hwmod *
  2265. *
  2266. * Query the powerdomain of of @oh to get the context loss
  2267. * count for this device.
  2268. *
  2269. * Returns the context loss count of the powerdomain assocated with @oh
  2270. * upon success, or zero if no powerdomain exists for @oh.
  2271. */
  2272. int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
  2273. {
  2274. struct powerdomain *pwrdm;
  2275. int ret = 0;
  2276. pwrdm = omap_hwmod_get_pwrdm(oh);
  2277. if (pwrdm)
  2278. ret = pwrdm_get_context_loss_count(pwrdm);
  2279. return ret;
  2280. }
  2281. /**
  2282. * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
  2283. * @oh: struct omap_hwmod *
  2284. *
  2285. * Prevent the hwmod @oh from being reset during the setup process.
  2286. * Intended for use by board-*.c files on boards with devices that
  2287. * cannot tolerate being reset. Must be called before the hwmod has
  2288. * been set up. Returns 0 upon success or negative error code upon
  2289. * failure.
  2290. */
  2291. int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
  2292. {
  2293. if (!oh)
  2294. return -EINVAL;
  2295. if (oh->_state != _HWMOD_STATE_REGISTERED) {
  2296. pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
  2297. oh->name);
  2298. return -EINVAL;
  2299. }
  2300. oh->flags |= HWMOD_INIT_NO_RESET;
  2301. return 0;
  2302. }
  2303. /**
  2304. * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
  2305. * @oh: struct omap_hwmod * containing hwmod mux entries
  2306. * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
  2307. * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
  2308. *
  2309. * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
  2310. * entry number @pad_idx for the hwmod @oh, trigger the interrupt
  2311. * service routine for the hwmod's mpu_irqs array index @irq_idx. If
  2312. * this function is not called for a given pad_idx, then the ISR
  2313. * associated with @oh's first MPU IRQ will be triggered when an I/O
  2314. * pad wakeup occurs on that pad. Note that @pad_idx is the index of
  2315. * the _dynamic or wakeup_ entry: if there are other entries not
  2316. * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
  2317. * entries are NOT COUNTED in the dynamic pad index. This function
  2318. * must be called separately for each pad that requires its interrupt
  2319. * to be re-routed this way. Returns -EINVAL if there is an argument
  2320. * problem or if @oh does not have hwmod mux entries or MPU IRQs;
  2321. * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
  2322. *
  2323. * XXX This function interface is fragile. Rather than using array
  2324. * indexes, which are subject to unpredictable change, it should be
  2325. * using hwmod IRQ names, and some other stable key for the hwmod mux
  2326. * pad records.
  2327. */
  2328. int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
  2329. {
  2330. int nr_irqs;
  2331. might_sleep();
  2332. if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
  2333. pad_idx >= oh->mux->nr_pads_dynamic)
  2334. return -EINVAL;
  2335. /* Check the number of available mpu_irqs */
  2336. for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
  2337. ;
  2338. if (irq_idx >= nr_irqs)
  2339. return -EINVAL;
  2340. if (!oh->mux->irqs) {
  2341. /* XXX What frees this? */
  2342. oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
  2343. GFP_KERNEL);
  2344. if (!oh->mux->irqs)
  2345. return -ENOMEM;
  2346. }
  2347. oh->mux->irqs[pad_idx] = irq_idx;
  2348. return 0;
  2349. }