omap_hwmod.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438
  1. /*
  2. * omap_hwmod implementation for OMAP2/3/4
  3. *
  4. * Copyright (C) 2009-2011 Nokia Corporation
  5. * Copyright (C) 2011-2012 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 <linux/bootmem.h>
  140. #include "common.h"
  141. #include <plat/cpu.h>
  142. #include "clockdomain.h"
  143. #include "powerdomain.h"
  144. #include <plat/clock.h>
  145. #include <plat/omap_hwmod.h>
  146. #include <plat/prcm.h>
  147. #include "cm2xxx_3xxx.h"
  148. #include "cminst44xx.h"
  149. #include "prm2xxx_3xxx.h"
  150. #include "prm44xx.h"
  151. #include "prminst44xx.h"
  152. #include "mux.h"
  153. /* Maximum microseconds to wait for OMAP module to softreset */
  154. #define MAX_MODULE_SOFTRESET_WAIT 10000
  155. /* Name of the OMAP hwmod for the MPU */
  156. #define MPU_INITIATOR_NAME "mpu"
  157. /*
  158. * Number of struct omap_hwmod_link records per struct
  159. * omap_hwmod_ocp_if record (master->slave and slave->master)
  160. */
  161. #define LINKS_PER_OCP_IF 2
  162. /**
  163. * struct omap_hwmod_soc_ops - fn ptrs for some SoC-specific operations
  164. * @enable_module: function to enable a module (via MODULEMODE)
  165. * @disable_module: function to disable a module (via MODULEMODE)
  166. *
  167. * XXX Eventually this functionality will be hidden inside the PRM/CM
  168. * device drivers. Until then, this should avoid huge blocks of cpu_is_*()
  169. * conditionals in this code.
  170. */
  171. struct omap_hwmod_soc_ops {
  172. void (*enable_module)(struct omap_hwmod *oh);
  173. int (*disable_module)(struct omap_hwmod *oh);
  174. };
  175. /* soc_ops: adapts the omap_hwmod code to the currently-booted SoC */
  176. static struct omap_hwmod_soc_ops soc_ops;
  177. /* omap_hwmod_list contains all registered struct omap_hwmods */
  178. static LIST_HEAD(omap_hwmod_list);
  179. /* mpu_oh: used to add/remove MPU initiator from sleepdep list */
  180. static struct omap_hwmod *mpu_oh;
  181. /*
  182. * linkspace: ptr to a buffer that struct omap_hwmod_link records are
  183. * allocated from - used to reduce the number of small memory
  184. * allocations, which has a significant impact on performance
  185. */
  186. static struct omap_hwmod_link *linkspace;
  187. /*
  188. * free_ls, max_ls: array indexes into linkspace; representing the
  189. * next free struct omap_hwmod_link index, and the maximum number of
  190. * struct omap_hwmod_link records allocated (respectively)
  191. */
  192. static unsigned short free_ls, max_ls, ls_supp;
  193. /* inited: set to true once the hwmod code is initialized */
  194. static bool inited;
  195. /* Private functions */
  196. /**
  197. * _fetch_next_ocp_if - return the next OCP interface in a list
  198. * @p: ptr to a ptr to the list_head inside the ocp_if to return
  199. * @i: pointer to the index of the element pointed to by @p in the list
  200. *
  201. * Return a pointer to the struct omap_hwmod_ocp_if record
  202. * containing the struct list_head pointed to by @p, and increment
  203. * @p such that a future call to this routine will return the next
  204. * record.
  205. */
  206. static struct omap_hwmod_ocp_if *_fetch_next_ocp_if(struct list_head **p,
  207. int *i)
  208. {
  209. struct omap_hwmod_ocp_if *oi;
  210. oi = list_entry(*p, struct omap_hwmod_link, node)->ocp_if;
  211. *p = (*p)->next;
  212. *i = *i + 1;
  213. return oi;
  214. }
  215. /**
  216. * _update_sysc_cache - return the module OCP_SYSCONFIG register, keep copy
  217. * @oh: struct omap_hwmod *
  218. *
  219. * Load the current value of the hwmod OCP_SYSCONFIG register into the
  220. * struct omap_hwmod for later use. Returns -EINVAL if the hwmod has no
  221. * OCP_SYSCONFIG register or 0 upon success.
  222. */
  223. static int _update_sysc_cache(struct omap_hwmod *oh)
  224. {
  225. if (!oh->class->sysc) {
  226. WARN(1, "omap_hwmod: %s: cannot read OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
  227. return -EINVAL;
  228. }
  229. /* XXX ensure module interface clock is up */
  230. oh->_sysc_cache = omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
  231. if (!(oh->class->sysc->sysc_flags & SYSC_NO_CACHE))
  232. oh->_int_flags |= _HWMOD_SYSCONFIG_LOADED;
  233. return 0;
  234. }
  235. /**
  236. * _write_sysconfig - write a value to the module's OCP_SYSCONFIG register
  237. * @v: OCP_SYSCONFIG value to write
  238. * @oh: struct omap_hwmod *
  239. *
  240. * Write @v into the module class' OCP_SYSCONFIG register, if it has
  241. * one. No return value.
  242. */
  243. static void _write_sysconfig(u32 v, struct omap_hwmod *oh)
  244. {
  245. if (!oh->class->sysc) {
  246. WARN(1, "omap_hwmod: %s: cannot write OCP_SYSCONFIG: not defined on hwmod's class\n", oh->name);
  247. return;
  248. }
  249. /* XXX ensure module interface clock is up */
  250. /* Module might have lost context, always update cache and register */
  251. oh->_sysc_cache = v;
  252. omap_hwmod_write(v, oh, oh->class->sysc->sysc_offs);
  253. }
  254. /**
  255. * _set_master_standbymode: set the OCP_SYSCONFIG MIDLEMODE field in @v
  256. * @oh: struct omap_hwmod *
  257. * @standbymode: MIDLEMODE field bits
  258. * @v: pointer to register contents to modify
  259. *
  260. * Update the master standby mode bits in @v to be @standbymode for
  261. * the @oh hwmod. Does not write to the hardware. Returns -EINVAL
  262. * upon error or 0 upon success.
  263. */
  264. static int _set_master_standbymode(struct omap_hwmod *oh, u8 standbymode,
  265. u32 *v)
  266. {
  267. u32 mstandby_mask;
  268. u8 mstandby_shift;
  269. if (!oh->class->sysc ||
  270. !(oh->class->sysc->sysc_flags & SYSC_HAS_MIDLEMODE))
  271. return -EINVAL;
  272. if (!oh->class->sysc->sysc_fields) {
  273. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  274. return -EINVAL;
  275. }
  276. mstandby_shift = oh->class->sysc->sysc_fields->midle_shift;
  277. mstandby_mask = (0x3 << mstandby_shift);
  278. *v &= ~mstandby_mask;
  279. *v |= __ffs(standbymode) << mstandby_shift;
  280. return 0;
  281. }
  282. /**
  283. * _set_slave_idlemode: set the OCP_SYSCONFIG SIDLEMODE field in @v
  284. * @oh: struct omap_hwmod *
  285. * @idlemode: SIDLEMODE field bits
  286. * @v: pointer to register contents to modify
  287. *
  288. * Update the slave idle mode bits in @v to be @idlemode for the @oh
  289. * hwmod. Does not write to the hardware. Returns -EINVAL upon error
  290. * or 0 upon success.
  291. */
  292. static int _set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode, u32 *v)
  293. {
  294. u32 sidle_mask;
  295. u8 sidle_shift;
  296. if (!oh->class->sysc ||
  297. !(oh->class->sysc->sysc_flags & SYSC_HAS_SIDLEMODE))
  298. return -EINVAL;
  299. if (!oh->class->sysc->sysc_fields) {
  300. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  301. return -EINVAL;
  302. }
  303. sidle_shift = oh->class->sysc->sysc_fields->sidle_shift;
  304. sidle_mask = (0x3 << sidle_shift);
  305. *v &= ~sidle_mask;
  306. *v |= __ffs(idlemode) << sidle_shift;
  307. return 0;
  308. }
  309. /**
  310. * _set_clockactivity: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
  311. * @oh: struct omap_hwmod *
  312. * @clockact: CLOCKACTIVITY field bits
  313. * @v: pointer to register contents to modify
  314. *
  315. * Update the clockactivity mode bits in @v to be @clockact for the
  316. * @oh hwmod. Used for additional powersaving on some modules. Does
  317. * not write to the hardware. Returns -EINVAL upon error or 0 upon
  318. * success.
  319. */
  320. static int _set_clockactivity(struct omap_hwmod *oh, u8 clockact, u32 *v)
  321. {
  322. u32 clkact_mask;
  323. u8 clkact_shift;
  324. if (!oh->class->sysc ||
  325. !(oh->class->sysc->sysc_flags & SYSC_HAS_CLOCKACTIVITY))
  326. return -EINVAL;
  327. if (!oh->class->sysc->sysc_fields) {
  328. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  329. return -EINVAL;
  330. }
  331. clkact_shift = oh->class->sysc->sysc_fields->clkact_shift;
  332. clkact_mask = (0x3 << clkact_shift);
  333. *v &= ~clkact_mask;
  334. *v |= clockact << clkact_shift;
  335. return 0;
  336. }
  337. /**
  338. * _set_softreset: set OCP_SYSCONFIG.CLOCKACTIVITY bits in @v
  339. * @oh: struct omap_hwmod *
  340. * @v: pointer to register contents to modify
  341. *
  342. * Set the SOFTRESET bit in @v for hwmod @oh. Returns -EINVAL upon
  343. * error or 0 upon success.
  344. */
  345. static int _set_softreset(struct omap_hwmod *oh, u32 *v)
  346. {
  347. u32 softrst_mask;
  348. if (!oh->class->sysc ||
  349. !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
  350. return -EINVAL;
  351. if (!oh->class->sysc->sysc_fields) {
  352. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  353. return -EINVAL;
  354. }
  355. softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
  356. *v |= softrst_mask;
  357. return 0;
  358. }
  359. /**
  360. * _set_module_autoidle: set the OCP_SYSCONFIG AUTOIDLE field in @v
  361. * @oh: struct omap_hwmod *
  362. * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
  363. * @v: pointer to register contents to modify
  364. *
  365. * Update the module autoidle bit in @v to be @autoidle for the @oh
  366. * hwmod. The autoidle bit controls whether the module can gate
  367. * internal clocks automatically when it isn't doing anything; the
  368. * exact function of this bit varies on a per-module basis. This
  369. * function does not write to the hardware. Returns -EINVAL upon
  370. * error or 0 upon success.
  371. */
  372. static int _set_module_autoidle(struct omap_hwmod *oh, u8 autoidle,
  373. u32 *v)
  374. {
  375. u32 autoidle_mask;
  376. u8 autoidle_shift;
  377. if (!oh->class->sysc ||
  378. !(oh->class->sysc->sysc_flags & SYSC_HAS_AUTOIDLE))
  379. return -EINVAL;
  380. if (!oh->class->sysc->sysc_fields) {
  381. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  382. return -EINVAL;
  383. }
  384. autoidle_shift = oh->class->sysc->sysc_fields->autoidle_shift;
  385. autoidle_mask = (0x1 << autoidle_shift);
  386. *v &= ~autoidle_mask;
  387. *v |= autoidle << autoidle_shift;
  388. return 0;
  389. }
  390. /**
  391. * _set_idle_ioring_wakeup - enable/disable IO pad wakeup on hwmod idle for mux
  392. * @oh: struct omap_hwmod *
  393. * @set_wake: bool value indicating to set (true) or clear (false) wakeup enable
  394. *
  395. * Set or clear the I/O pad wakeup flag in the mux entries for the
  396. * hwmod @oh. This function changes the @oh->mux->pads_dynamic array
  397. * in memory. If the hwmod is currently idled, and the new idle
  398. * values don't match the previous ones, this function will also
  399. * update the SCM PADCTRL registers. Otherwise, if the hwmod is not
  400. * currently idled, this function won't touch the hardware: the new
  401. * mux settings are written to the SCM PADCTRL registers when the
  402. * hwmod is idled. No return value.
  403. */
  404. static void _set_idle_ioring_wakeup(struct omap_hwmod *oh, bool set_wake)
  405. {
  406. struct omap_device_pad *pad;
  407. bool change = false;
  408. u16 prev_idle;
  409. int j;
  410. if (!oh->mux || !oh->mux->enabled)
  411. return;
  412. for (j = 0; j < oh->mux->nr_pads_dynamic; j++) {
  413. pad = oh->mux->pads_dynamic[j];
  414. if (!(pad->flags & OMAP_DEVICE_PAD_WAKEUP))
  415. continue;
  416. prev_idle = pad->idle;
  417. if (set_wake)
  418. pad->idle |= OMAP_WAKEUP_EN;
  419. else
  420. pad->idle &= ~OMAP_WAKEUP_EN;
  421. if (prev_idle != pad->idle)
  422. change = true;
  423. }
  424. if (change && oh->_state == _HWMOD_STATE_IDLE)
  425. omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
  426. }
  427. /**
  428. * _enable_wakeup: set OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
  429. * @oh: struct omap_hwmod *
  430. *
  431. * Allow the hardware module @oh to send wakeups. Returns -EINVAL
  432. * upon error or 0 upon success.
  433. */
  434. static int _enable_wakeup(struct omap_hwmod *oh, u32 *v)
  435. {
  436. if (!oh->class->sysc ||
  437. !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
  438. (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
  439. (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
  440. return -EINVAL;
  441. if (!oh->class->sysc->sysc_fields) {
  442. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  443. return -EINVAL;
  444. }
  445. if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
  446. *v |= 0x1 << oh->class->sysc->sysc_fields->enwkup_shift;
  447. if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
  448. _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
  449. if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
  450. _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
  451. /* XXX test pwrdm_get_wken for this hwmod's subsystem */
  452. oh->_int_flags |= _HWMOD_WAKEUP_ENABLED;
  453. return 0;
  454. }
  455. /**
  456. * _disable_wakeup: clear OCP_SYSCONFIG.ENAWAKEUP bit in the hardware
  457. * @oh: struct omap_hwmod *
  458. *
  459. * Prevent the hardware module @oh to send wakeups. Returns -EINVAL
  460. * upon error or 0 upon success.
  461. */
  462. static int _disable_wakeup(struct omap_hwmod *oh, u32 *v)
  463. {
  464. if (!oh->class->sysc ||
  465. !((oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP) ||
  466. (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP) ||
  467. (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)))
  468. return -EINVAL;
  469. if (!oh->class->sysc->sysc_fields) {
  470. WARN(1, "omap_hwmod: %s: offset struct for sysconfig not provided in class\n", oh->name);
  471. return -EINVAL;
  472. }
  473. if (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)
  474. *v &= ~(0x1 << oh->class->sysc->sysc_fields->enwkup_shift);
  475. if (oh->class->sysc->idlemodes & SIDLE_SMART_WKUP)
  476. _set_slave_idlemode(oh, HWMOD_IDLEMODE_SMART, v);
  477. if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
  478. _set_master_standbymode(oh, HWMOD_IDLEMODE_SMART_WKUP, v);
  479. /* XXX test pwrdm_get_wken for this hwmod's subsystem */
  480. oh->_int_flags &= ~_HWMOD_WAKEUP_ENABLED;
  481. return 0;
  482. }
  483. /**
  484. * _add_initiator_dep: prevent @oh from smart-idling while @init_oh is active
  485. * @oh: struct omap_hwmod *
  486. *
  487. * Prevent the hardware module @oh from entering idle while the
  488. * hardare module initiator @init_oh is active. Useful when a module
  489. * will be accessed by a particular initiator (e.g., if a module will
  490. * be accessed by the IVA, there should be a sleepdep between the IVA
  491. * initiator and the module). Only applies to modules in smart-idle
  492. * mode. If the clockdomain is marked as not needing autodeps, return
  493. * 0 without doing anything. Otherwise, returns -EINVAL upon error or
  494. * passes along clkdm_add_sleepdep() value upon success.
  495. */
  496. static int _add_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
  497. {
  498. if (!oh->_clk)
  499. return -EINVAL;
  500. if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
  501. return 0;
  502. return clkdm_add_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
  503. }
  504. /**
  505. * _del_initiator_dep: allow @oh to smart-idle even if @init_oh is active
  506. * @oh: struct omap_hwmod *
  507. *
  508. * Allow the hardware module @oh to enter idle while the hardare
  509. * module initiator @init_oh is active. Useful when a module will not
  510. * be accessed by a particular initiator (e.g., if a module will not
  511. * be accessed by the IVA, there should be no sleepdep between the IVA
  512. * initiator and the module). Only applies to modules in smart-idle
  513. * mode. If the clockdomain is marked as not needing autodeps, return
  514. * 0 without doing anything. Returns -EINVAL upon error or passes
  515. * along clkdm_del_sleepdep() value upon success.
  516. */
  517. static int _del_initiator_dep(struct omap_hwmod *oh, struct omap_hwmod *init_oh)
  518. {
  519. if (!oh->_clk)
  520. return -EINVAL;
  521. if (oh->_clk->clkdm && oh->_clk->clkdm->flags & CLKDM_NO_AUTODEPS)
  522. return 0;
  523. return clkdm_del_sleepdep(oh->_clk->clkdm, init_oh->_clk->clkdm);
  524. }
  525. /**
  526. * _init_main_clk - get a struct clk * for the the hwmod's main functional clk
  527. * @oh: struct omap_hwmod *
  528. *
  529. * Called from _init_clocks(). Populates the @oh _clk (main
  530. * functional clock pointer) if a main_clk is present. Returns 0 on
  531. * success or -EINVAL on error.
  532. */
  533. static int _init_main_clk(struct omap_hwmod *oh)
  534. {
  535. int ret = 0;
  536. if (!oh->main_clk)
  537. return 0;
  538. oh->_clk = omap_clk_get_by_name(oh->main_clk);
  539. if (!oh->_clk) {
  540. pr_warning("omap_hwmod: %s: cannot clk_get main_clk %s\n",
  541. oh->name, oh->main_clk);
  542. return -EINVAL;
  543. }
  544. if (!oh->_clk->clkdm)
  545. pr_warning("omap_hwmod: %s: missing clockdomain for %s.\n",
  546. oh->main_clk, oh->_clk->name);
  547. return ret;
  548. }
  549. /**
  550. * _init_interface_clks - get a struct clk * for the the hwmod's interface clks
  551. * @oh: struct omap_hwmod *
  552. *
  553. * Called from _init_clocks(). Populates the @oh OCP slave interface
  554. * clock pointers. Returns 0 on success or -EINVAL on error.
  555. */
  556. static int _init_interface_clks(struct omap_hwmod *oh)
  557. {
  558. struct omap_hwmod_ocp_if *os;
  559. struct list_head *p;
  560. struct clk *c;
  561. int i = 0;
  562. int ret = 0;
  563. p = oh->slave_ports.next;
  564. while (i < oh->slaves_cnt) {
  565. os = _fetch_next_ocp_if(&p, &i);
  566. if (!os->clk)
  567. continue;
  568. c = omap_clk_get_by_name(os->clk);
  569. if (!c) {
  570. pr_warning("omap_hwmod: %s: cannot clk_get interface_clk %s\n",
  571. oh->name, os->clk);
  572. ret = -EINVAL;
  573. }
  574. os->_clk = c;
  575. }
  576. return ret;
  577. }
  578. /**
  579. * _init_opt_clk - get a struct clk * for the the hwmod's optional clocks
  580. * @oh: struct omap_hwmod *
  581. *
  582. * Called from _init_clocks(). Populates the @oh omap_hwmod_opt_clk
  583. * clock pointers. Returns 0 on success or -EINVAL on error.
  584. */
  585. static int _init_opt_clks(struct omap_hwmod *oh)
  586. {
  587. struct omap_hwmod_opt_clk *oc;
  588. struct clk *c;
  589. int i;
  590. int ret = 0;
  591. for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++) {
  592. c = omap_clk_get_by_name(oc->clk);
  593. if (!c) {
  594. pr_warning("omap_hwmod: %s: cannot clk_get opt_clk %s\n",
  595. oh->name, oc->clk);
  596. ret = -EINVAL;
  597. }
  598. oc->_clk = c;
  599. }
  600. return ret;
  601. }
  602. /**
  603. * _enable_clocks - enable hwmod main clock and interface clocks
  604. * @oh: struct omap_hwmod *
  605. *
  606. * Enables all clocks necessary for register reads and writes to succeed
  607. * on the hwmod @oh. Returns 0.
  608. */
  609. static int _enable_clocks(struct omap_hwmod *oh)
  610. {
  611. struct omap_hwmod_ocp_if *os;
  612. struct list_head *p;
  613. int i = 0;
  614. pr_debug("omap_hwmod: %s: enabling clocks\n", oh->name);
  615. if (oh->_clk)
  616. clk_enable(oh->_clk);
  617. p = oh->slave_ports.next;
  618. while (i < oh->slaves_cnt) {
  619. os = _fetch_next_ocp_if(&p, &i);
  620. if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
  621. clk_enable(os->_clk);
  622. }
  623. /* The opt clocks are controlled by the device driver. */
  624. return 0;
  625. }
  626. /**
  627. * _disable_clocks - disable hwmod main clock and interface clocks
  628. * @oh: struct omap_hwmod *
  629. *
  630. * Disables the hwmod @oh main functional and interface clocks. Returns 0.
  631. */
  632. static int _disable_clocks(struct omap_hwmod *oh)
  633. {
  634. struct omap_hwmod_ocp_if *os;
  635. struct list_head *p;
  636. int i = 0;
  637. pr_debug("omap_hwmod: %s: disabling clocks\n", oh->name);
  638. if (oh->_clk)
  639. clk_disable(oh->_clk);
  640. p = oh->slave_ports.next;
  641. while (i < oh->slaves_cnt) {
  642. os = _fetch_next_ocp_if(&p, &i);
  643. if (os->_clk && (os->flags & OCPIF_SWSUP_IDLE))
  644. clk_disable(os->_clk);
  645. }
  646. /* The opt clocks are controlled by the device driver. */
  647. return 0;
  648. }
  649. static void _enable_optional_clocks(struct omap_hwmod *oh)
  650. {
  651. struct omap_hwmod_opt_clk *oc;
  652. int i;
  653. pr_debug("omap_hwmod: %s: enabling optional clocks\n", oh->name);
  654. for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
  655. if (oc->_clk) {
  656. pr_debug("omap_hwmod: enable %s:%s\n", oc->role,
  657. oc->_clk->name);
  658. clk_enable(oc->_clk);
  659. }
  660. }
  661. static void _disable_optional_clocks(struct omap_hwmod *oh)
  662. {
  663. struct omap_hwmod_opt_clk *oc;
  664. int i;
  665. pr_debug("omap_hwmod: %s: disabling optional clocks\n", oh->name);
  666. for (i = oh->opt_clks_cnt, oc = oh->opt_clks; i > 0; i--, oc++)
  667. if (oc->_clk) {
  668. pr_debug("omap_hwmod: disable %s:%s\n", oc->role,
  669. oc->_clk->name);
  670. clk_disable(oc->_clk);
  671. }
  672. }
  673. /**
  674. * _omap4_enable_module - enable CLKCTRL modulemode on OMAP4
  675. * @oh: struct omap_hwmod *
  676. *
  677. * Enables the PRCM module mode related to the hwmod @oh.
  678. * No return value.
  679. */
  680. static void _omap4_enable_module(struct omap_hwmod *oh)
  681. {
  682. if (!oh->clkdm || !oh->prcm.omap4.modulemode)
  683. return;
  684. pr_debug("omap_hwmod: %s: %s: %d\n",
  685. oh->name, __func__, oh->prcm.omap4.modulemode);
  686. omap4_cminst_module_enable(oh->prcm.omap4.modulemode,
  687. oh->clkdm->prcm_partition,
  688. oh->clkdm->cm_inst,
  689. oh->clkdm->clkdm_offs,
  690. oh->prcm.omap4.clkctrl_offs);
  691. }
  692. /**
  693. * _omap4_wait_target_disable - wait for a module to be disabled on OMAP4
  694. * @oh: struct omap_hwmod *
  695. *
  696. * Wait for a module @oh to enter slave idle. Returns 0 if the module
  697. * does not have an IDLEST bit or if the module successfully enters
  698. * slave idle; otherwise, pass along the return value of the
  699. * appropriate *_cm*_wait_module_idle() function.
  700. */
  701. static int _omap4_wait_target_disable(struct omap_hwmod *oh)
  702. {
  703. if (!oh)
  704. return -EINVAL;
  705. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  706. return 0;
  707. if (oh->flags & HWMOD_NO_IDLEST)
  708. return 0;
  709. return omap4_cminst_wait_module_idle(oh->clkdm->prcm_partition,
  710. oh->clkdm->cm_inst,
  711. oh->clkdm->clkdm_offs,
  712. oh->prcm.omap4.clkctrl_offs);
  713. }
  714. /**
  715. * _count_mpu_irqs - count the number of MPU IRQ lines associated with @oh
  716. * @oh: struct omap_hwmod *oh
  717. *
  718. * Count and return the number of MPU IRQs associated with the hwmod
  719. * @oh. Used to allocate struct resource data. Returns 0 if @oh is
  720. * NULL.
  721. */
  722. static int _count_mpu_irqs(struct omap_hwmod *oh)
  723. {
  724. struct omap_hwmod_irq_info *ohii;
  725. int i = 0;
  726. if (!oh || !oh->mpu_irqs)
  727. return 0;
  728. do {
  729. ohii = &oh->mpu_irqs[i++];
  730. } while (ohii->irq != -1);
  731. return i-1;
  732. }
  733. /**
  734. * _count_sdma_reqs - count the number of SDMA request lines associated with @oh
  735. * @oh: struct omap_hwmod *oh
  736. *
  737. * Count and return the number of SDMA request lines associated with
  738. * the hwmod @oh. Used to allocate struct resource data. Returns 0
  739. * if @oh is NULL.
  740. */
  741. static int _count_sdma_reqs(struct omap_hwmod *oh)
  742. {
  743. struct omap_hwmod_dma_info *ohdi;
  744. int i = 0;
  745. if (!oh || !oh->sdma_reqs)
  746. return 0;
  747. do {
  748. ohdi = &oh->sdma_reqs[i++];
  749. } while (ohdi->dma_req != -1);
  750. return i-1;
  751. }
  752. /**
  753. * _count_ocp_if_addr_spaces - count the number of address space entries for @oh
  754. * @oh: struct omap_hwmod *oh
  755. *
  756. * Count and return the number of address space ranges associated with
  757. * the hwmod @oh. Used to allocate struct resource data. Returns 0
  758. * if @oh is NULL.
  759. */
  760. static int _count_ocp_if_addr_spaces(struct omap_hwmod_ocp_if *os)
  761. {
  762. struct omap_hwmod_addr_space *mem;
  763. int i = 0;
  764. if (!os || !os->addr)
  765. return 0;
  766. do {
  767. mem = &os->addr[i++];
  768. } while (mem->pa_start != mem->pa_end);
  769. return i-1;
  770. }
  771. /**
  772. * _get_mpu_irq_by_name - fetch MPU interrupt line number by name
  773. * @oh: struct omap_hwmod * to operate on
  774. * @name: pointer to the name of the MPU interrupt number to fetch (optional)
  775. * @irq: pointer to an unsigned int to store the MPU IRQ number to
  776. *
  777. * Retrieve a MPU hardware IRQ line number named by @name associated
  778. * with the IP block pointed to by @oh. The IRQ number will be filled
  779. * into the address pointed to by @dma. When @name is non-null, the
  780. * IRQ line number associated with the named entry will be returned.
  781. * If @name is null, the first matching entry will be returned. Data
  782. * order is not meaningful in hwmod data, so callers are strongly
  783. * encouraged to use a non-null @name whenever possible to avoid
  784. * unpredictable effects if hwmod data is later added that causes data
  785. * ordering to change. Returns 0 upon success or a negative error
  786. * code upon error.
  787. */
  788. static int _get_mpu_irq_by_name(struct omap_hwmod *oh, const char *name,
  789. unsigned int *irq)
  790. {
  791. int i;
  792. bool found = false;
  793. if (!oh->mpu_irqs)
  794. return -ENOENT;
  795. i = 0;
  796. while (oh->mpu_irqs[i].irq != -1) {
  797. if (name == oh->mpu_irqs[i].name ||
  798. !strcmp(name, oh->mpu_irqs[i].name)) {
  799. found = true;
  800. break;
  801. }
  802. i++;
  803. }
  804. if (!found)
  805. return -ENOENT;
  806. *irq = oh->mpu_irqs[i].irq;
  807. return 0;
  808. }
  809. /**
  810. * _get_sdma_req_by_name - fetch SDMA request line ID by name
  811. * @oh: struct omap_hwmod * to operate on
  812. * @name: pointer to the name of the SDMA request line to fetch (optional)
  813. * @dma: pointer to an unsigned int to store the request line ID to
  814. *
  815. * Retrieve an SDMA request line ID named by @name on the IP block
  816. * pointed to by @oh. The ID will be filled into the address pointed
  817. * to by @dma. When @name is non-null, the request line ID associated
  818. * with the named entry will be returned. If @name is null, the first
  819. * matching entry will be returned. Data order is not meaningful in
  820. * hwmod data, so callers are strongly encouraged to use a non-null
  821. * @name whenever possible to avoid unpredictable effects if hwmod
  822. * data is later added that causes data ordering to change. Returns 0
  823. * upon success or a negative error code upon error.
  824. */
  825. static int _get_sdma_req_by_name(struct omap_hwmod *oh, const char *name,
  826. unsigned int *dma)
  827. {
  828. int i;
  829. bool found = false;
  830. if (!oh->sdma_reqs)
  831. return -ENOENT;
  832. i = 0;
  833. while (oh->sdma_reqs[i].dma_req != -1) {
  834. if (name == oh->sdma_reqs[i].name ||
  835. !strcmp(name, oh->sdma_reqs[i].name)) {
  836. found = true;
  837. break;
  838. }
  839. i++;
  840. }
  841. if (!found)
  842. return -ENOENT;
  843. *dma = oh->sdma_reqs[i].dma_req;
  844. return 0;
  845. }
  846. /**
  847. * _get_addr_space_by_name - fetch address space start & end by name
  848. * @oh: struct omap_hwmod * to operate on
  849. * @name: pointer to the name of the address space to fetch (optional)
  850. * @pa_start: pointer to a u32 to store the starting address to
  851. * @pa_end: pointer to a u32 to store the ending address to
  852. *
  853. * Retrieve address space start and end addresses for the IP block
  854. * pointed to by @oh. The data will be filled into the addresses
  855. * pointed to by @pa_start and @pa_end. When @name is non-null, the
  856. * address space data associated with the named entry will be
  857. * returned. If @name is null, the first matching entry will be
  858. * returned. Data order is not meaningful in hwmod data, so callers
  859. * are strongly encouraged to use a non-null @name whenever possible
  860. * to avoid unpredictable effects if hwmod data is later added that
  861. * causes data ordering to change. Returns 0 upon success or a
  862. * negative error code upon error.
  863. */
  864. static int _get_addr_space_by_name(struct omap_hwmod *oh, const char *name,
  865. u32 *pa_start, u32 *pa_end)
  866. {
  867. int i, j;
  868. struct omap_hwmod_ocp_if *os;
  869. struct list_head *p = NULL;
  870. bool found = false;
  871. p = oh->slave_ports.next;
  872. i = 0;
  873. while (i < oh->slaves_cnt) {
  874. os = _fetch_next_ocp_if(&p, &i);
  875. if (!os->addr)
  876. return -ENOENT;
  877. j = 0;
  878. while (os->addr[j].pa_start != os->addr[j].pa_end) {
  879. if (name == os->addr[j].name ||
  880. !strcmp(name, os->addr[j].name)) {
  881. found = true;
  882. break;
  883. }
  884. j++;
  885. }
  886. if (found)
  887. break;
  888. }
  889. if (!found)
  890. return -ENOENT;
  891. *pa_start = os->addr[j].pa_start;
  892. *pa_end = os->addr[j].pa_end;
  893. return 0;
  894. }
  895. /**
  896. * _save_mpu_port_index - find and save the index to @oh's MPU port
  897. * @oh: struct omap_hwmod *
  898. *
  899. * Determines the array index of the OCP slave port that the MPU uses
  900. * to address the device, and saves it into the struct omap_hwmod.
  901. * Intended to be called during hwmod registration only. No return
  902. * value.
  903. */
  904. static void __init _save_mpu_port_index(struct omap_hwmod *oh)
  905. {
  906. struct omap_hwmod_ocp_if *os = NULL;
  907. struct list_head *p;
  908. int i = 0;
  909. if (!oh)
  910. return;
  911. oh->_int_flags |= _HWMOD_NO_MPU_PORT;
  912. p = oh->slave_ports.next;
  913. while (i < oh->slaves_cnt) {
  914. os = _fetch_next_ocp_if(&p, &i);
  915. if (os->user & OCP_USER_MPU) {
  916. oh->_mpu_port = os;
  917. oh->_int_flags &= ~_HWMOD_NO_MPU_PORT;
  918. break;
  919. }
  920. }
  921. return;
  922. }
  923. /**
  924. * _find_mpu_rt_port - return omap_hwmod_ocp_if accessible by the MPU
  925. * @oh: struct omap_hwmod *
  926. *
  927. * Given a pointer to a struct omap_hwmod record @oh, return a pointer
  928. * to the struct omap_hwmod_ocp_if record that is used by the MPU to
  929. * communicate with the IP block. This interface need not be directly
  930. * connected to the MPU (and almost certainly is not), but is directly
  931. * connected to the IP block represented by @oh. Returns a pointer
  932. * to the struct omap_hwmod_ocp_if * upon success, or returns NULL upon
  933. * error or if there does not appear to be a path from the MPU to this
  934. * IP block.
  935. */
  936. static struct omap_hwmod_ocp_if *_find_mpu_rt_port(struct omap_hwmod *oh)
  937. {
  938. if (!oh || oh->_int_flags & _HWMOD_NO_MPU_PORT || oh->slaves_cnt == 0)
  939. return NULL;
  940. return oh->_mpu_port;
  941. };
  942. /**
  943. * _find_mpu_rt_addr_space - return MPU register target address space for @oh
  944. * @oh: struct omap_hwmod *
  945. *
  946. * Returns a pointer to the struct omap_hwmod_addr_space record representing
  947. * the register target MPU address space; or returns NULL upon error.
  948. */
  949. static struct omap_hwmod_addr_space * __init _find_mpu_rt_addr_space(struct omap_hwmod *oh)
  950. {
  951. struct omap_hwmod_ocp_if *os;
  952. struct omap_hwmod_addr_space *mem;
  953. int found = 0, i = 0;
  954. os = _find_mpu_rt_port(oh);
  955. if (!os || !os->addr)
  956. return NULL;
  957. do {
  958. mem = &os->addr[i++];
  959. if (mem->flags & ADDR_TYPE_RT)
  960. found = 1;
  961. } while (!found && mem->pa_start != mem->pa_end);
  962. return (found) ? mem : NULL;
  963. }
  964. /**
  965. * _enable_sysc - try to bring a module out of idle via OCP_SYSCONFIG
  966. * @oh: struct omap_hwmod *
  967. *
  968. * If module is marked as SWSUP_SIDLE, force the module out of slave
  969. * idle; otherwise, configure it for smart-idle. If module is marked
  970. * as SWSUP_MSUSPEND, force the module out of master standby;
  971. * otherwise, configure it for smart-standby. No return value.
  972. */
  973. static void _enable_sysc(struct omap_hwmod *oh)
  974. {
  975. u8 idlemode, sf;
  976. u32 v;
  977. if (!oh->class->sysc)
  978. return;
  979. v = oh->_sysc_cache;
  980. sf = oh->class->sysc->sysc_flags;
  981. if (sf & SYSC_HAS_SIDLEMODE) {
  982. idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
  983. HWMOD_IDLEMODE_NO : HWMOD_IDLEMODE_SMART;
  984. _set_slave_idlemode(oh, idlemode, &v);
  985. }
  986. if (sf & SYSC_HAS_MIDLEMODE) {
  987. if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
  988. idlemode = HWMOD_IDLEMODE_NO;
  989. } else {
  990. if (sf & SYSC_HAS_ENAWAKEUP)
  991. _enable_wakeup(oh, &v);
  992. if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
  993. idlemode = HWMOD_IDLEMODE_SMART_WKUP;
  994. else
  995. idlemode = HWMOD_IDLEMODE_SMART;
  996. }
  997. _set_master_standbymode(oh, idlemode, &v);
  998. }
  999. /*
  1000. * XXX The clock framework should handle this, by
  1001. * calling into this code. But this must wait until the
  1002. * clock structures are tagged with omap_hwmod entries
  1003. */
  1004. if ((oh->flags & HWMOD_SET_DEFAULT_CLOCKACT) &&
  1005. (sf & SYSC_HAS_CLOCKACTIVITY))
  1006. _set_clockactivity(oh, oh->class->sysc->clockact, &v);
  1007. /* If slave is in SMARTIDLE, also enable wakeup */
  1008. if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
  1009. _enable_wakeup(oh, &v);
  1010. _write_sysconfig(v, oh);
  1011. /*
  1012. * Set the autoidle bit only after setting the smartidle bit
  1013. * Setting this will not have any impact on the other modules.
  1014. */
  1015. if (sf & SYSC_HAS_AUTOIDLE) {
  1016. idlemode = (oh->flags & HWMOD_NO_OCP_AUTOIDLE) ?
  1017. 0 : 1;
  1018. _set_module_autoidle(oh, idlemode, &v);
  1019. _write_sysconfig(v, oh);
  1020. }
  1021. }
  1022. /**
  1023. * _idle_sysc - try to put a module into idle via OCP_SYSCONFIG
  1024. * @oh: struct omap_hwmod *
  1025. *
  1026. * If module is marked as SWSUP_SIDLE, force the module into slave
  1027. * idle; otherwise, configure it for smart-idle. If module is marked
  1028. * as SWSUP_MSUSPEND, force the module into master standby; otherwise,
  1029. * configure it for smart-standby. No return value.
  1030. */
  1031. static void _idle_sysc(struct omap_hwmod *oh)
  1032. {
  1033. u8 idlemode, sf;
  1034. u32 v;
  1035. if (!oh->class->sysc)
  1036. return;
  1037. v = oh->_sysc_cache;
  1038. sf = oh->class->sysc->sysc_flags;
  1039. if (sf & SYSC_HAS_SIDLEMODE) {
  1040. idlemode = (oh->flags & HWMOD_SWSUP_SIDLE) ?
  1041. HWMOD_IDLEMODE_FORCE : HWMOD_IDLEMODE_SMART;
  1042. _set_slave_idlemode(oh, idlemode, &v);
  1043. }
  1044. if (sf & SYSC_HAS_MIDLEMODE) {
  1045. if (oh->flags & HWMOD_SWSUP_MSTANDBY) {
  1046. idlemode = HWMOD_IDLEMODE_FORCE;
  1047. } else {
  1048. if (sf & SYSC_HAS_ENAWAKEUP)
  1049. _enable_wakeup(oh, &v);
  1050. if (oh->class->sysc->idlemodes & MSTANDBY_SMART_WKUP)
  1051. idlemode = HWMOD_IDLEMODE_SMART_WKUP;
  1052. else
  1053. idlemode = HWMOD_IDLEMODE_SMART;
  1054. }
  1055. _set_master_standbymode(oh, idlemode, &v);
  1056. }
  1057. /* If slave is in SMARTIDLE, also enable wakeup */
  1058. if ((sf & SYSC_HAS_SIDLEMODE) && !(oh->flags & HWMOD_SWSUP_SIDLE))
  1059. _enable_wakeup(oh, &v);
  1060. _write_sysconfig(v, oh);
  1061. }
  1062. /**
  1063. * _shutdown_sysc - force a module into idle via OCP_SYSCONFIG
  1064. * @oh: struct omap_hwmod *
  1065. *
  1066. * Force the module into slave idle and master suspend. No return
  1067. * value.
  1068. */
  1069. static void _shutdown_sysc(struct omap_hwmod *oh)
  1070. {
  1071. u32 v;
  1072. u8 sf;
  1073. if (!oh->class->sysc)
  1074. return;
  1075. v = oh->_sysc_cache;
  1076. sf = oh->class->sysc->sysc_flags;
  1077. if (sf & SYSC_HAS_SIDLEMODE)
  1078. _set_slave_idlemode(oh, HWMOD_IDLEMODE_FORCE, &v);
  1079. if (sf & SYSC_HAS_MIDLEMODE)
  1080. _set_master_standbymode(oh, HWMOD_IDLEMODE_FORCE, &v);
  1081. if (sf & SYSC_HAS_AUTOIDLE)
  1082. _set_module_autoidle(oh, 1, &v);
  1083. _write_sysconfig(v, oh);
  1084. }
  1085. /**
  1086. * _lookup - find an omap_hwmod by name
  1087. * @name: find an omap_hwmod by name
  1088. *
  1089. * Return a pointer to an omap_hwmod by name, or NULL if not found.
  1090. */
  1091. static struct omap_hwmod *_lookup(const char *name)
  1092. {
  1093. struct omap_hwmod *oh, *temp_oh;
  1094. oh = NULL;
  1095. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  1096. if (!strcmp(name, temp_oh->name)) {
  1097. oh = temp_oh;
  1098. break;
  1099. }
  1100. }
  1101. return oh;
  1102. }
  1103. /**
  1104. * _init_clkdm - look up a clockdomain name, store pointer in omap_hwmod
  1105. * @oh: struct omap_hwmod *
  1106. *
  1107. * Convert a clockdomain name stored in a struct omap_hwmod into a
  1108. * clockdomain pointer, and save it into the struct omap_hwmod.
  1109. * return -EINVAL if clkdm_name does not exist or if the lookup failed.
  1110. */
  1111. static int _init_clkdm(struct omap_hwmod *oh)
  1112. {
  1113. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  1114. return 0;
  1115. if (!oh->clkdm_name) {
  1116. pr_warning("omap_hwmod: %s: no clkdm_name\n", oh->name);
  1117. return -EINVAL;
  1118. }
  1119. oh->clkdm = clkdm_lookup(oh->clkdm_name);
  1120. if (!oh->clkdm) {
  1121. pr_warning("omap_hwmod: %s: could not associate to clkdm %s\n",
  1122. oh->name, oh->clkdm_name);
  1123. return -EINVAL;
  1124. }
  1125. pr_debug("omap_hwmod: %s: associated to clkdm %s\n",
  1126. oh->name, oh->clkdm_name);
  1127. return 0;
  1128. }
  1129. /**
  1130. * _init_clocks - clk_get() all clocks associated with this hwmod. Retrieve as
  1131. * well the clockdomain.
  1132. * @oh: struct omap_hwmod *
  1133. * @data: not used; pass NULL
  1134. *
  1135. * Called by omap_hwmod_setup_*() (after omap2_clk_init()).
  1136. * Resolves all clock names embedded in the hwmod. Returns 0 on
  1137. * success, or a negative error code on failure.
  1138. */
  1139. static int _init_clocks(struct omap_hwmod *oh, void *data)
  1140. {
  1141. int ret = 0;
  1142. if (oh->_state != _HWMOD_STATE_REGISTERED)
  1143. return 0;
  1144. pr_debug("omap_hwmod: %s: looking up clocks\n", oh->name);
  1145. ret |= _init_main_clk(oh);
  1146. ret |= _init_interface_clks(oh);
  1147. ret |= _init_opt_clks(oh);
  1148. ret |= _init_clkdm(oh);
  1149. if (!ret)
  1150. oh->_state = _HWMOD_STATE_CLKS_INITED;
  1151. else
  1152. pr_warning("omap_hwmod: %s: cannot _init_clocks\n", oh->name);
  1153. return ret;
  1154. }
  1155. /**
  1156. * _wait_target_ready - wait for a module to leave slave idle
  1157. * @oh: struct omap_hwmod *
  1158. *
  1159. * Wait for a module @oh to leave slave idle. Returns 0 if the module
  1160. * does not have an IDLEST bit or if the module successfully leaves
  1161. * slave idle; otherwise, pass along the return value of the
  1162. * appropriate *_cm*_wait_module_ready() function.
  1163. */
  1164. static int _wait_target_ready(struct omap_hwmod *oh)
  1165. {
  1166. struct omap_hwmod_ocp_if *os;
  1167. int ret;
  1168. if (!oh)
  1169. return -EINVAL;
  1170. if (oh->flags & HWMOD_NO_IDLEST)
  1171. return 0;
  1172. os = _find_mpu_rt_port(oh);
  1173. if (!os)
  1174. return 0;
  1175. /* XXX check module SIDLEMODE */
  1176. /* XXX check clock enable states */
  1177. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1178. ret = omap2_cm_wait_module_ready(oh->prcm.omap2.module_offs,
  1179. oh->prcm.omap2.idlest_reg_id,
  1180. oh->prcm.omap2.idlest_idle_bit);
  1181. } else if (cpu_is_omap44xx()) {
  1182. if (!oh->clkdm)
  1183. return -EINVAL;
  1184. ret = omap4_cminst_wait_module_ready(oh->clkdm->prcm_partition,
  1185. oh->clkdm->cm_inst,
  1186. oh->clkdm->clkdm_offs,
  1187. oh->prcm.omap4.clkctrl_offs);
  1188. } else {
  1189. BUG();
  1190. };
  1191. return ret;
  1192. }
  1193. /**
  1194. * _lookup_hardreset - fill register bit info for this hwmod/reset line
  1195. * @oh: struct omap_hwmod *
  1196. * @name: name of the reset line in the context of this hwmod
  1197. * @ohri: struct omap_hwmod_rst_info * that this function will fill in
  1198. *
  1199. * Return the bit position of the reset line that match the
  1200. * input name. Return -ENOENT if not found.
  1201. */
  1202. static u8 _lookup_hardreset(struct omap_hwmod *oh, const char *name,
  1203. struct omap_hwmod_rst_info *ohri)
  1204. {
  1205. int i;
  1206. for (i = 0; i < oh->rst_lines_cnt; i++) {
  1207. const char *rst_line = oh->rst_lines[i].name;
  1208. if (!strcmp(rst_line, name)) {
  1209. ohri->rst_shift = oh->rst_lines[i].rst_shift;
  1210. ohri->st_shift = oh->rst_lines[i].st_shift;
  1211. pr_debug("omap_hwmod: %s: %s: %s: rst %d st %d\n",
  1212. oh->name, __func__, rst_line, ohri->rst_shift,
  1213. ohri->st_shift);
  1214. return 0;
  1215. }
  1216. }
  1217. return -ENOENT;
  1218. }
  1219. /**
  1220. * _assert_hardreset - assert the HW reset line of submodules
  1221. * contained in the hwmod module.
  1222. * @oh: struct omap_hwmod *
  1223. * @name: name of the reset line to lookup and assert
  1224. *
  1225. * Some IP like dsp, ipu or iva contain processor that require
  1226. * an HW reset line to be assert / deassert in order to enable fully
  1227. * the IP.
  1228. */
  1229. static int _assert_hardreset(struct omap_hwmod *oh, const char *name)
  1230. {
  1231. struct omap_hwmod_rst_info ohri;
  1232. u8 ret;
  1233. if (!oh)
  1234. return -EINVAL;
  1235. ret = _lookup_hardreset(oh, name, &ohri);
  1236. if (IS_ERR_VALUE(ret))
  1237. return ret;
  1238. if (cpu_is_omap24xx() || cpu_is_omap34xx())
  1239. return omap2_prm_assert_hardreset(oh->prcm.omap2.module_offs,
  1240. ohri.rst_shift);
  1241. else if (cpu_is_omap44xx())
  1242. return omap4_prminst_assert_hardreset(ohri.rst_shift,
  1243. oh->clkdm->pwrdm.ptr->prcm_partition,
  1244. oh->clkdm->pwrdm.ptr->prcm_offs,
  1245. oh->prcm.omap4.rstctrl_offs);
  1246. else
  1247. return -EINVAL;
  1248. }
  1249. /**
  1250. * _deassert_hardreset - deassert the HW reset line of submodules contained
  1251. * in the hwmod module.
  1252. * @oh: struct omap_hwmod *
  1253. * @name: name of the reset line to look up and deassert
  1254. *
  1255. * Some IP like dsp, ipu or iva contain processor that require
  1256. * an HW reset line to be assert / deassert in order to enable fully
  1257. * the IP.
  1258. */
  1259. static int _deassert_hardreset(struct omap_hwmod *oh, const char *name)
  1260. {
  1261. struct omap_hwmod_rst_info ohri;
  1262. int ret;
  1263. if (!oh)
  1264. return -EINVAL;
  1265. ret = _lookup_hardreset(oh, name, &ohri);
  1266. if (IS_ERR_VALUE(ret))
  1267. return ret;
  1268. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1269. ret = omap2_prm_deassert_hardreset(oh->prcm.omap2.module_offs,
  1270. ohri.rst_shift,
  1271. ohri.st_shift);
  1272. } else if (cpu_is_omap44xx()) {
  1273. if (ohri.st_shift)
  1274. pr_err("omap_hwmod: %s: %s: hwmod data error: OMAP4 does not support st_shift\n",
  1275. oh->name, name);
  1276. ret = omap4_prminst_deassert_hardreset(ohri.rst_shift,
  1277. oh->clkdm->pwrdm.ptr->prcm_partition,
  1278. oh->clkdm->pwrdm.ptr->prcm_offs,
  1279. oh->prcm.omap4.rstctrl_offs);
  1280. } else {
  1281. return -EINVAL;
  1282. }
  1283. if (ret == -EBUSY)
  1284. pr_warning("omap_hwmod: %s: failed to hardreset\n", oh->name);
  1285. return ret;
  1286. }
  1287. /**
  1288. * _read_hardreset - read the HW reset line state of submodules
  1289. * contained in the hwmod module
  1290. * @oh: struct omap_hwmod *
  1291. * @name: name of the reset line to look up and read
  1292. *
  1293. * Return the state of the reset line.
  1294. */
  1295. static int _read_hardreset(struct omap_hwmod *oh, const char *name)
  1296. {
  1297. struct omap_hwmod_rst_info ohri;
  1298. u8 ret;
  1299. if (!oh)
  1300. return -EINVAL;
  1301. ret = _lookup_hardreset(oh, name, &ohri);
  1302. if (IS_ERR_VALUE(ret))
  1303. return ret;
  1304. if (cpu_is_omap24xx() || cpu_is_omap34xx()) {
  1305. return omap2_prm_is_hardreset_asserted(oh->prcm.omap2.module_offs,
  1306. ohri.st_shift);
  1307. } else if (cpu_is_omap44xx()) {
  1308. return omap4_prminst_is_hardreset_asserted(ohri.rst_shift,
  1309. oh->clkdm->pwrdm.ptr->prcm_partition,
  1310. oh->clkdm->pwrdm.ptr->prcm_offs,
  1311. oh->prcm.omap4.rstctrl_offs);
  1312. } else {
  1313. return -EINVAL;
  1314. }
  1315. }
  1316. /**
  1317. * _are_any_hardreset_lines_asserted - return true if part of @oh is hard-reset
  1318. * @oh: struct omap_hwmod *
  1319. *
  1320. * If any hardreset line associated with @oh is asserted, then return true.
  1321. * Otherwise, if @oh has no hardreset lines associated with it, or if
  1322. * no hardreset lines associated with @oh are asserted, then return false.
  1323. * This function is used to avoid executing some parts of the IP block
  1324. * enable/disable sequence if a hardreset line is set.
  1325. */
  1326. static bool _are_any_hardreset_lines_asserted(struct omap_hwmod *oh)
  1327. {
  1328. int i;
  1329. if (oh->rst_lines_cnt == 0)
  1330. return false;
  1331. for (i = 0; i < oh->rst_lines_cnt; i++)
  1332. if (_read_hardreset(oh, oh->rst_lines[i].name) > 0)
  1333. return true;
  1334. return false;
  1335. }
  1336. /**
  1337. * _omap4_disable_module - enable CLKCTRL modulemode on OMAP4
  1338. * @oh: struct omap_hwmod *
  1339. *
  1340. * Disable the PRCM module mode related to the hwmod @oh.
  1341. * Return EINVAL if the modulemode is not supported and 0 in case of success.
  1342. */
  1343. static int _omap4_disable_module(struct omap_hwmod *oh)
  1344. {
  1345. int v;
  1346. if (!oh->clkdm || !oh->prcm.omap4.modulemode)
  1347. return -EINVAL;
  1348. pr_debug("omap_hwmod: %s: %s\n", oh->name, __func__);
  1349. omap4_cminst_module_disable(oh->clkdm->prcm_partition,
  1350. oh->clkdm->cm_inst,
  1351. oh->clkdm->clkdm_offs,
  1352. oh->prcm.omap4.clkctrl_offs);
  1353. if (_are_any_hardreset_lines_asserted(oh))
  1354. return 0;
  1355. v = _omap4_wait_target_disable(oh);
  1356. if (v)
  1357. pr_warn("omap_hwmod: %s: _wait_target_disable failed\n",
  1358. oh->name);
  1359. return 0;
  1360. }
  1361. /**
  1362. * _ocp_softreset - reset an omap_hwmod via the OCP_SYSCONFIG bit
  1363. * @oh: struct omap_hwmod *
  1364. *
  1365. * Resets an omap_hwmod @oh via the OCP_SYSCONFIG bit. hwmod must be
  1366. * enabled for this to work. Returns -ENOENT if the hwmod cannot be
  1367. * reset this way, -EINVAL if the hwmod is in the wrong state,
  1368. * -ETIMEDOUT if the module did not reset in time, or 0 upon success.
  1369. *
  1370. * In OMAP3 a specific SYSSTATUS register is used to get the reset status.
  1371. * Starting in OMAP4, some IPs do not have SYSSTATUS registers and instead
  1372. * use the SYSCONFIG softreset bit to provide the status.
  1373. *
  1374. * Note that some IP like McBSP do have reset control but don't have
  1375. * reset status.
  1376. */
  1377. static int _ocp_softreset(struct omap_hwmod *oh)
  1378. {
  1379. u32 v, softrst_mask;
  1380. int c = 0;
  1381. int ret = 0;
  1382. if (!oh->class->sysc ||
  1383. !(oh->class->sysc->sysc_flags & SYSC_HAS_SOFTRESET))
  1384. return -ENOENT;
  1385. /* clocks must be on for this operation */
  1386. if (oh->_state != _HWMOD_STATE_ENABLED) {
  1387. pr_warning("omap_hwmod: %s: reset can only be entered from "
  1388. "enabled state\n", oh->name);
  1389. return -EINVAL;
  1390. }
  1391. /* For some modules, all optionnal clocks need to be enabled as well */
  1392. if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
  1393. _enable_optional_clocks(oh);
  1394. pr_debug("omap_hwmod: %s: resetting via OCP SOFTRESET\n", oh->name);
  1395. v = oh->_sysc_cache;
  1396. ret = _set_softreset(oh, &v);
  1397. if (ret)
  1398. goto dis_opt_clks;
  1399. _write_sysconfig(v, oh);
  1400. if (oh->class->sysc->srst_udelay)
  1401. udelay(oh->class->sysc->srst_udelay);
  1402. if (oh->class->sysc->sysc_flags & SYSS_HAS_RESET_STATUS)
  1403. omap_test_timeout((omap_hwmod_read(oh,
  1404. oh->class->sysc->syss_offs)
  1405. & SYSS_RESETDONE_MASK),
  1406. MAX_MODULE_SOFTRESET_WAIT, c);
  1407. else if (oh->class->sysc->sysc_flags & SYSC_HAS_RESET_STATUS) {
  1408. softrst_mask = (0x1 << oh->class->sysc->sysc_fields->srst_shift);
  1409. omap_test_timeout(!(omap_hwmod_read(oh,
  1410. oh->class->sysc->sysc_offs)
  1411. & softrst_mask),
  1412. MAX_MODULE_SOFTRESET_WAIT, c);
  1413. }
  1414. if (c == MAX_MODULE_SOFTRESET_WAIT)
  1415. pr_warning("omap_hwmod: %s: softreset failed (waited %d usec)\n",
  1416. oh->name, MAX_MODULE_SOFTRESET_WAIT);
  1417. else
  1418. pr_debug("omap_hwmod: %s: softreset in %d usec\n", oh->name, c);
  1419. /*
  1420. * XXX add _HWMOD_STATE_WEDGED for modules that don't come back from
  1421. * _wait_target_ready() or _reset()
  1422. */
  1423. ret = (c == MAX_MODULE_SOFTRESET_WAIT) ? -ETIMEDOUT : 0;
  1424. dis_opt_clks:
  1425. if (oh->flags & HWMOD_CONTROL_OPT_CLKS_IN_RESET)
  1426. _disable_optional_clocks(oh);
  1427. return ret;
  1428. }
  1429. /**
  1430. * _reset - reset an omap_hwmod
  1431. * @oh: struct omap_hwmod *
  1432. *
  1433. * Resets an omap_hwmod @oh. If the module has a custom reset
  1434. * function pointer defined, then call it to reset the IP block, and
  1435. * pass along its return value to the caller. Otherwise, if the IP
  1436. * block has an OCP_SYSCONFIG register with a SOFTRESET bitfield
  1437. * associated with it, call a function to reset the IP block via that
  1438. * method, and pass along the return value to the caller. Finally, if
  1439. * the IP block has some hardreset lines associated with it, assert
  1440. * all of those, but do _not_ deassert them. (This is because driver
  1441. * authors have expressed an apparent requirement to control the
  1442. * deassertion of the hardreset lines themselves.)
  1443. *
  1444. * The default software reset mechanism for most OMAP IP blocks is
  1445. * triggered via the OCP_SYSCONFIG.SOFTRESET bit. However, some
  1446. * hwmods cannot be reset via this method. Some are not targets and
  1447. * therefore have no OCP header registers to access. Others (like the
  1448. * IVA) have idiosyncratic reset sequences. So for these relatively
  1449. * rare cases, custom reset code can be supplied in the struct
  1450. * omap_hwmod_class .reset function pointer. Passes along the return
  1451. * value from either _ocp_softreset() or the custom reset function -
  1452. * these must return -EINVAL if the hwmod cannot be reset this way or
  1453. * if the hwmod is in the wrong state, -ETIMEDOUT if the module did
  1454. * not reset in time, or 0 upon success.
  1455. */
  1456. static int _reset(struct omap_hwmod *oh)
  1457. {
  1458. int i, r;
  1459. pr_debug("omap_hwmod: %s: resetting\n", oh->name);
  1460. if (oh->class->reset) {
  1461. r = oh->class->reset(oh);
  1462. } else {
  1463. if (oh->rst_lines_cnt > 0) {
  1464. for (i = 0; i < oh->rst_lines_cnt; i++)
  1465. _assert_hardreset(oh, oh->rst_lines[i].name);
  1466. return 0;
  1467. } else {
  1468. r = _ocp_softreset(oh);
  1469. if (r == -ENOENT)
  1470. r = 0;
  1471. }
  1472. }
  1473. /*
  1474. * OCP_SYSCONFIG bits need to be reprogrammed after a
  1475. * softreset. The _enable() function should be split to avoid
  1476. * the rewrite of the OCP_SYSCONFIG register.
  1477. */
  1478. if (oh->class->sysc) {
  1479. _update_sysc_cache(oh);
  1480. _enable_sysc(oh);
  1481. }
  1482. return r;
  1483. }
  1484. /**
  1485. * _enable - enable an omap_hwmod
  1486. * @oh: struct omap_hwmod *
  1487. *
  1488. * Enables an omap_hwmod @oh such that the MPU can access the hwmod's
  1489. * register target. Returns -EINVAL if the hwmod is in the wrong
  1490. * state or passes along the return value of _wait_target_ready().
  1491. */
  1492. static int _enable(struct omap_hwmod *oh)
  1493. {
  1494. int r;
  1495. int hwsup = 0;
  1496. pr_debug("omap_hwmod: %s: enabling\n", oh->name);
  1497. /*
  1498. * hwmods with HWMOD_INIT_NO_IDLE flag set are left in enabled
  1499. * state at init. Now that someone is really trying to enable
  1500. * them, just ensure that the hwmod mux is set.
  1501. */
  1502. if (oh->_int_flags & _HWMOD_SKIP_ENABLE) {
  1503. /*
  1504. * If the caller has mux data populated, do the mux'ing
  1505. * which wouldn't have been done as part of the _enable()
  1506. * done during setup.
  1507. */
  1508. if (oh->mux)
  1509. omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
  1510. oh->_int_flags &= ~_HWMOD_SKIP_ENABLE;
  1511. return 0;
  1512. }
  1513. if (oh->_state != _HWMOD_STATE_INITIALIZED &&
  1514. oh->_state != _HWMOD_STATE_IDLE &&
  1515. oh->_state != _HWMOD_STATE_DISABLED) {
  1516. WARN(1, "omap_hwmod: %s: enabled state can only be entered from initialized, idle, or disabled state\n",
  1517. oh->name);
  1518. return -EINVAL;
  1519. }
  1520. /*
  1521. * If an IP block contains HW reset lines and any of them are
  1522. * asserted, we let integration code associated with that
  1523. * block handle the enable. We've received very little
  1524. * information on what those driver authors need, and until
  1525. * detailed information is provided and the driver code is
  1526. * posted to the public lists, this is probably the best we
  1527. * can do.
  1528. */
  1529. if (_are_any_hardreset_lines_asserted(oh))
  1530. return 0;
  1531. /* Mux pins for device runtime if populated */
  1532. if (oh->mux && (!oh->mux->enabled ||
  1533. ((oh->_state == _HWMOD_STATE_IDLE) &&
  1534. oh->mux->pads_dynamic)))
  1535. omap_hwmod_mux(oh->mux, _HWMOD_STATE_ENABLED);
  1536. _add_initiator_dep(oh, mpu_oh);
  1537. if (oh->clkdm) {
  1538. /*
  1539. * A clockdomain must be in SW_SUP before enabling
  1540. * completely the module. The clockdomain can be set
  1541. * in HW_AUTO only when the module become ready.
  1542. */
  1543. hwsup = clkdm_in_hwsup(oh->clkdm);
  1544. r = clkdm_hwmod_enable(oh->clkdm, oh);
  1545. if (r) {
  1546. WARN(1, "omap_hwmod: %s: could not enable clockdomain %s: %d\n",
  1547. oh->name, oh->clkdm->name, r);
  1548. return r;
  1549. }
  1550. }
  1551. _enable_clocks(oh);
  1552. if (soc_ops.enable_module)
  1553. soc_ops.enable_module(oh);
  1554. r = _wait_target_ready(oh);
  1555. if (!r) {
  1556. /*
  1557. * Set the clockdomain to HW_AUTO only if the target is ready,
  1558. * assuming that the previous state was HW_AUTO
  1559. */
  1560. if (oh->clkdm && hwsup)
  1561. clkdm_allow_idle(oh->clkdm);
  1562. oh->_state = _HWMOD_STATE_ENABLED;
  1563. /* Access the sysconfig only if the target is ready */
  1564. if (oh->class->sysc) {
  1565. if (!(oh->_int_flags & _HWMOD_SYSCONFIG_LOADED))
  1566. _update_sysc_cache(oh);
  1567. _enable_sysc(oh);
  1568. }
  1569. } else {
  1570. _disable_clocks(oh);
  1571. pr_debug("omap_hwmod: %s: _wait_target_ready: %d\n",
  1572. oh->name, r);
  1573. if (oh->clkdm)
  1574. clkdm_hwmod_disable(oh->clkdm, oh);
  1575. }
  1576. return r;
  1577. }
  1578. /**
  1579. * _idle - idle an omap_hwmod
  1580. * @oh: struct omap_hwmod *
  1581. *
  1582. * Idles an omap_hwmod @oh. This should be called once the hwmod has
  1583. * no further work. Returns -EINVAL if the hwmod is in the wrong
  1584. * state or returns 0.
  1585. */
  1586. static int _idle(struct omap_hwmod *oh)
  1587. {
  1588. pr_debug("omap_hwmod: %s: idling\n", oh->name);
  1589. if (oh->_state != _HWMOD_STATE_ENABLED) {
  1590. WARN(1, "omap_hwmod: %s: idle state can only be entered from enabled state\n",
  1591. oh->name);
  1592. return -EINVAL;
  1593. }
  1594. if (_are_any_hardreset_lines_asserted(oh))
  1595. return 0;
  1596. if (oh->class->sysc)
  1597. _idle_sysc(oh);
  1598. _del_initiator_dep(oh, mpu_oh);
  1599. if (soc_ops.disable_module)
  1600. soc_ops.disable_module(oh);
  1601. /*
  1602. * The module must be in idle mode before disabling any parents
  1603. * clocks. Otherwise, the parent clock might be disabled before
  1604. * the module transition is done, and thus will prevent the
  1605. * transition to complete properly.
  1606. */
  1607. _disable_clocks(oh);
  1608. if (oh->clkdm)
  1609. clkdm_hwmod_disable(oh->clkdm, oh);
  1610. /* Mux pins for device idle if populated */
  1611. if (oh->mux && oh->mux->pads_dynamic)
  1612. omap_hwmod_mux(oh->mux, _HWMOD_STATE_IDLE);
  1613. oh->_state = _HWMOD_STATE_IDLE;
  1614. return 0;
  1615. }
  1616. /**
  1617. * omap_hwmod_set_ocp_autoidle - set the hwmod's OCP autoidle bit
  1618. * @oh: struct omap_hwmod *
  1619. * @autoidle: desired AUTOIDLE bitfield value (0 or 1)
  1620. *
  1621. * Sets the IP block's OCP autoidle bit in hardware, and updates our
  1622. * local copy. Intended to be used by drivers that require
  1623. * direct manipulation of the AUTOIDLE bits.
  1624. * Returns -EINVAL if @oh is null or is not in the ENABLED state, or passes
  1625. * along the return value from _set_module_autoidle().
  1626. *
  1627. * Any users of this function should be scrutinized carefully.
  1628. */
  1629. int omap_hwmod_set_ocp_autoidle(struct omap_hwmod *oh, u8 autoidle)
  1630. {
  1631. u32 v;
  1632. int retval = 0;
  1633. unsigned long flags;
  1634. if (!oh || oh->_state != _HWMOD_STATE_ENABLED)
  1635. return -EINVAL;
  1636. spin_lock_irqsave(&oh->_lock, flags);
  1637. v = oh->_sysc_cache;
  1638. retval = _set_module_autoidle(oh, autoidle, &v);
  1639. if (!retval)
  1640. _write_sysconfig(v, oh);
  1641. spin_unlock_irqrestore(&oh->_lock, flags);
  1642. return retval;
  1643. }
  1644. /**
  1645. * _shutdown - shutdown an omap_hwmod
  1646. * @oh: struct omap_hwmod *
  1647. *
  1648. * Shut down an omap_hwmod @oh. This should be called when the driver
  1649. * used for the hwmod is removed or unloaded or if the driver is not
  1650. * used by the system. Returns -EINVAL if the hwmod is in the wrong
  1651. * state or returns 0.
  1652. */
  1653. static int _shutdown(struct omap_hwmod *oh)
  1654. {
  1655. int ret, i;
  1656. u8 prev_state;
  1657. if (oh->_state != _HWMOD_STATE_IDLE &&
  1658. oh->_state != _HWMOD_STATE_ENABLED) {
  1659. WARN(1, "omap_hwmod: %s: disabled state can only be entered from idle, or enabled state\n",
  1660. oh->name);
  1661. return -EINVAL;
  1662. }
  1663. if (_are_any_hardreset_lines_asserted(oh))
  1664. return 0;
  1665. pr_debug("omap_hwmod: %s: disabling\n", oh->name);
  1666. if (oh->class->pre_shutdown) {
  1667. prev_state = oh->_state;
  1668. if (oh->_state == _HWMOD_STATE_IDLE)
  1669. _enable(oh);
  1670. ret = oh->class->pre_shutdown(oh);
  1671. if (ret) {
  1672. if (prev_state == _HWMOD_STATE_IDLE)
  1673. _idle(oh);
  1674. return ret;
  1675. }
  1676. }
  1677. if (oh->class->sysc) {
  1678. if (oh->_state == _HWMOD_STATE_IDLE)
  1679. _enable(oh);
  1680. _shutdown_sysc(oh);
  1681. }
  1682. /* clocks and deps are already disabled in idle */
  1683. if (oh->_state == _HWMOD_STATE_ENABLED) {
  1684. _del_initiator_dep(oh, mpu_oh);
  1685. /* XXX what about the other system initiators here? dma, dsp */
  1686. if (soc_ops.disable_module)
  1687. soc_ops.disable_module(oh);
  1688. _disable_clocks(oh);
  1689. if (oh->clkdm)
  1690. clkdm_hwmod_disable(oh->clkdm, oh);
  1691. }
  1692. /* XXX Should this code also force-disable the optional clocks? */
  1693. for (i = 0; i < oh->rst_lines_cnt; i++)
  1694. _assert_hardreset(oh, oh->rst_lines[i].name);
  1695. /* Mux pins to safe mode or use populated off mode values */
  1696. if (oh->mux)
  1697. omap_hwmod_mux(oh->mux, _HWMOD_STATE_DISABLED);
  1698. oh->_state = _HWMOD_STATE_DISABLED;
  1699. return 0;
  1700. }
  1701. /**
  1702. * _init_mpu_rt_base - populate the virtual address for a hwmod
  1703. * @oh: struct omap_hwmod * to locate the virtual address
  1704. *
  1705. * Cache the virtual address used by the MPU to access this IP block's
  1706. * registers. This address is needed early so the OCP registers that
  1707. * are part of the device's address space can be ioremapped properly.
  1708. * No return value.
  1709. */
  1710. static void __init _init_mpu_rt_base(struct omap_hwmod *oh, void *data)
  1711. {
  1712. struct omap_hwmod_addr_space *mem;
  1713. void __iomem *va_start;
  1714. if (!oh)
  1715. return;
  1716. _save_mpu_port_index(oh);
  1717. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  1718. return;
  1719. mem = _find_mpu_rt_addr_space(oh);
  1720. if (!mem) {
  1721. pr_debug("omap_hwmod: %s: no MPU register target found\n",
  1722. oh->name);
  1723. return;
  1724. }
  1725. va_start = ioremap(mem->pa_start, mem->pa_end - mem->pa_start);
  1726. if (!va_start) {
  1727. pr_err("omap_hwmod: %s: Could not ioremap\n", oh->name);
  1728. return;
  1729. }
  1730. pr_debug("omap_hwmod: %s: MPU register target at va %p\n",
  1731. oh->name, va_start);
  1732. oh->_mpu_rt_va = va_start;
  1733. }
  1734. /**
  1735. * _init - initialize internal data for the hwmod @oh
  1736. * @oh: struct omap_hwmod *
  1737. * @n: (unused)
  1738. *
  1739. * Look up the clocks and the address space used by the MPU to access
  1740. * registers belonging to the hwmod @oh. @oh must already be
  1741. * registered at this point. This is the first of two phases for
  1742. * hwmod initialization. Code called here does not touch any hardware
  1743. * registers, it simply prepares internal data structures. Returns 0
  1744. * upon success or if the hwmod isn't registered, or -EINVAL upon
  1745. * failure.
  1746. */
  1747. static int __init _init(struct omap_hwmod *oh, void *data)
  1748. {
  1749. int r;
  1750. if (oh->_state != _HWMOD_STATE_REGISTERED)
  1751. return 0;
  1752. _init_mpu_rt_base(oh, NULL);
  1753. r = _init_clocks(oh, NULL);
  1754. if (IS_ERR_VALUE(r)) {
  1755. WARN(1, "omap_hwmod: %s: couldn't init clocks\n", oh->name);
  1756. return -EINVAL;
  1757. }
  1758. oh->_state = _HWMOD_STATE_INITIALIZED;
  1759. return 0;
  1760. }
  1761. /**
  1762. * _setup_iclk_autoidle - configure an IP block's interface clocks
  1763. * @oh: struct omap_hwmod *
  1764. *
  1765. * Set up the module's interface clocks. XXX This function is still mostly
  1766. * a stub; implementing this properly requires iclk autoidle usecounting in
  1767. * the clock code. No return value.
  1768. */
  1769. static void __init _setup_iclk_autoidle(struct omap_hwmod *oh)
  1770. {
  1771. struct omap_hwmod_ocp_if *os;
  1772. struct list_head *p;
  1773. int i = 0;
  1774. if (oh->_state != _HWMOD_STATE_INITIALIZED)
  1775. return;
  1776. p = oh->slave_ports.next;
  1777. while (i < oh->slaves_cnt) {
  1778. os = _fetch_next_ocp_if(&p, &i);
  1779. if (!os->_clk)
  1780. continue;
  1781. if (os->flags & OCPIF_SWSUP_IDLE) {
  1782. /* XXX omap_iclk_deny_idle(c); */
  1783. } else {
  1784. /* XXX omap_iclk_allow_idle(c); */
  1785. clk_enable(os->_clk);
  1786. }
  1787. }
  1788. return;
  1789. }
  1790. /**
  1791. * _setup_reset - reset an IP block during the setup process
  1792. * @oh: struct omap_hwmod *
  1793. *
  1794. * Reset the IP block corresponding to the hwmod @oh during the setup
  1795. * process. The IP block is first enabled so it can be successfully
  1796. * reset. Returns 0 upon success or a negative error code upon
  1797. * failure.
  1798. */
  1799. static int __init _setup_reset(struct omap_hwmod *oh)
  1800. {
  1801. int r;
  1802. if (oh->_state != _HWMOD_STATE_INITIALIZED)
  1803. return -EINVAL;
  1804. if (oh->rst_lines_cnt == 0) {
  1805. r = _enable(oh);
  1806. if (r) {
  1807. pr_warning("omap_hwmod: %s: cannot be enabled for reset (%d)\n",
  1808. oh->name, oh->_state);
  1809. return -EINVAL;
  1810. }
  1811. }
  1812. if (!(oh->flags & HWMOD_INIT_NO_RESET))
  1813. r = _reset(oh);
  1814. return r;
  1815. }
  1816. /**
  1817. * _setup_postsetup - transition to the appropriate state after _setup
  1818. * @oh: struct omap_hwmod *
  1819. *
  1820. * Place an IP block represented by @oh into a "post-setup" state --
  1821. * either IDLE, ENABLED, or DISABLED. ("post-setup" simply means that
  1822. * this function is called at the end of _setup().) The postsetup
  1823. * state for an IP block can be changed by calling
  1824. * omap_hwmod_enter_postsetup_state() early in the boot process,
  1825. * before one of the omap_hwmod_setup*() functions are called for the
  1826. * IP block.
  1827. *
  1828. * The IP block stays in this state until a PM runtime-based driver is
  1829. * loaded for that IP block. A post-setup state of IDLE is
  1830. * appropriate for almost all IP blocks with runtime PM-enabled
  1831. * drivers, since those drivers are able to enable the IP block. A
  1832. * post-setup state of ENABLED is appropriate for kernels with PM
  1833. * runtime disabled. The DISABLED state is appropriate for unusual IP
  1834. * blocks such as the MPU WDTIMER on kernels without WDTIMER drivers
  1835. * included, since the WDTIMER starts running on reset and will reset
  1836. * the MPU if left active.
  1837. *
  1838. * This post-setup mechanism is deprecated. Once all of the OMAP
  1839. * drivers have been converted to use PM runtime, and all of the IP
  1840. * block data and interconnect data is available to the hwmod code, it
  1841. * should be possible to replace this mechanism with a "lazy reset"
  1842. * arrangement. In a "lazy reset" setup, each IP block is enabled
  1843. * when the driver first probes, then all remaining IP blocks without
  1844. * drivers are either shut down or enabled after the drivers have
  1845. * loaded. However, this cannot take place until the above
  1846. * preconditions have been met, since otherwise the late reset code
  1847. * has no way of knowing which IP blocks are in use by drivers, and
  1848. * which ones are unused.
  1849. *
  1850. * No return value.
  1851. */
  1852. static void __init _setup_postsetup(struct omap_hwmod *oh)
  1853. {
  1854. u8 postsetup_state;
  1855. if (oh->rst_lines_cnt > 0)
  1856. return;
  1857. postsetup_state = oh->_postsetup_state;
  1858. if (postsetup_state == _HWMOD_STATE_UNKNOWN)
  1859. postsetup_state = _HWMOD_STATE_ENABLED;
  1860. /*
  1861. * XXX HWMOD_INIT_NO_IDLE does not belong in hwmod data -
  1862. * it should be set by the core code as a runtime flag during startup
  1863. */
  1864. if ((oh->flags & HWMOD_INIT_NO_IDLE) &&
  1865. (postsetup_state == _HWMOD_STATE_IDLE)) {
  1866. oh->_int_flags |= _HWMOD_SKIP_ENABLE;
  1867. postsetup_state = _HWMOD_STATE_ENABLED;
  1868. }
  1869. if (postsetup_state == _HWMOD_STATE_IDLE)
  1870. _idle(oh);
  1871. else if (postsetup_state == _HWMOD_STATE_DISABLED)
  1872. _shutdown(oh);
  1873. else if (postsetup_state != _HWMOD_STATE_ENABLED)
  1874. WARN(1, "hwmod: %s: unknown postsetup state %d! defaulting to enabled\n",
  1875. oh->name, postsetup_state);
  1876. return;
  1877. }
  1878. /**
  1879. * _setup - prepare IP block hardware for use
  1880. * @oh: struct omap_hwmod *
  1881. * @n: (unused, pass NULL)
  1882. *
  1883. * Configure the IP block represented by @oh. This may include
  1884. * enabling the IP block, resetting it, and placing it into a
  1885. * post-setup state, depending on the type of IP block and applicable
  1886. * flags. IP blocks are reset to prevent any previous configuration
  1887. * by the bootloader or previous operating system from interfering
  1888. * with power management or other parts of the system. The reset can
  1889. * be avoided; see omap_hwmod_no_setup_reset(). This is the second of
  1890. * two phases for hwmod initialization. Code called here generally
  1891. * affects the IP block hardware, or system integration hardware
  1892. * associated with the IP block. Returns 0.
  1893. */
  1894. static int __init _setup(struct omap_hwmod *oh, void *data)
  1895. {
  1896. if (oh->_state != _HWMOD_STATE_INITIALIZED)
  1897. return 0;
  1898. _setup_iclk_autoidle(oh);
  1899. if (!_setup_reset(oh))
  1900. _setup_postsetup(oh);
  1901. return 0;
  1902. }
  1903. /**
  1904. * _register - register a struct omap_hwmod
  1905. * @oh: struct omap_hwmod *
  1906. *
  1907. * Registers the omap_hwmod @oh. Returns -EEXIST if an omap_hwmod
  1908. * already has been registered by the same name; -EINVAL if the
  1909. * omap_hwmod is in the wrong state, if @oh is NULL, if the
  1910. * omap_hwmod's class field is NULL; if the omap_hwmod is missing a
  1911. * name, or if the omap_hwmod's class is missing a name; or 0 upon
  1912. * success.
  1913. *
  1914. * XXX The data should be copied into bootmem, so the original data
  1915. * should be marked __initdata and freed after init. This would allow
  1916. * unneeded omap_hwmods to be freed on multi-OMAP configurations. Note
  1917. * that the copy process would be relatively complex due to the large number
  1918. * of substructures.
  1919. */
  1920. static int __init _register(struct omap_hwmod *oh)
  1921. {
  1922. if (!oh || !oh->name || !oh->class || !oh->class->name ||
  1923. (oh->_state != _HWMOD_STATE_UNKNOWN))
  1924. return -EINVAL;
  1925. pr_debug("omap_hwmod: %s: registering\n", oh->name);
  1926. if (_lookup(oh->name))
  1927. return -EEXIST;
  1928. list_add_tail(&oh->node, &omap_hwmod_list);
  1929. INIT_LIST_HEAD(&oh->master_ports);
  1930. INIT_LIST_HEAD(&oh->slave_ports);
  1931. spin_lock_init(&oh->_lock);
  1932. oh->_state = _HWMOD_STATE_REGISTERED;
  1933. /*
  1934. * XXX Rather than doing a strcmp(), this should test a flag
  1935. * set in the hwmod data, inserted by the autogenerator code.
  1936. */
  1937. if (!strcmp(oh->name, MPU_INITIATOR_NAME))
  1938. mpu_oh = oh;
  1939. return 0;
  1940. }
  1941. /**
  1942. * _alloc_links - return allocated memory for hwmod links
  1943. * @ml: pointer to a struct omap_hwmod_link * for the master link
  1944. * @sl: pointer to a struct omap_hwmod_link * for the slave link
  1945. *
  1946. * Return pointers to two struct omap_hwmod_link records, via the
  1947. * addresses pointed to by @ml and @sl. Will first attempt to return
  1948. * memory allocated as part of a large initial block, but if that has
  1949. * been exhausted, will allocate memory itself. Since ideally this
  1950. * second allocation path will never occur, the number of these
  1951. * 'supplemental' allocations will be logged when debugging is
  1952. * enabled. Returns 0.
  1953. */
  1954. static int __init _alloc_links(struct omap_hwmod_link **ml,
  1955. struct omap_hwmod_link **sl)
  1956. {
  1957. unsigned int sz;
  1958. if ((free_ls + LINKS_PER_OCP_IF) <= max_ls) {
  1959. *ml = &linkspace[free_ls++];
  1960. *sl = &linkspace[free_ls++];
  1961. return 0;
  1962. }
  1963. sz = sizeof(struct omap_hwmod_link) * LINKS_PER_OCP_IF;
  1964. *sl = NULL;
  1965. *ml = alloc_bootmem(sz);
  1966. memset(*ml, 0, sz);
  1967. *sl = (void *)(*ml) + sizeof(struct omap_hwmod_link);
  1968. ls_supp++;
  1969. pr_debug("omap_hwmod: supplemental link allocations needed: %d\n",
  1970. ls_supp * LINKS_PER_OCP_IF);
  1971. return 0;
  1972. };
  1973. /**
  1974. * _add_link - add an interconnect between two IP blocks
  1975. * @oi: pointer to a struct omap_hwmod_ocp_if record
  1976. *
  1977. * Add struct omap_hwmod_link records connecting the master IP block
  1978. * specified in @oi->master to @oi, and connecting the slave IP block
  1979. * specified in @oi->slave to @oi. This code is assumed to run before
  1980. * preemption or SMP has been enabled, thus avoiding the need for
  1981. * locking in this code. Changes to this assumption will require
  1982. * additional locking. Returns 0.
  1983. */
  1984. static int __init _add_link(struct omap_hwmod_ocp_if *oi)
  1985. {
  1986. struct omap_hwmod_link *ml, *sl;
  1987. pr_debug("omap_hwmod: %s -> %s: adding link\n", oi->master->name,
  1988. oi->slave->name);
  1989. _alloc_links(&ml, &sl);
  1990. ml->ocp_if = oi;
  1991. INIT_LIST_HEAD(&ml->node);
  1992. list_add(&ml->node, &oi->master->master_ports);
  1993. oi->master->masters_cnt++;
  1994. sl->ocp_if = oi;
  1995. INIT_LIST_HEAD(&sl->node);
  1996. list_add(&sl->node, &oi->slave->slave_ports);
  1997. oi->slave->slaves_cnt++;
  1998. return 0;
  1999. }
  2000. /**
  2001. * _register_link - register a struct omap_hwmod_ocp_if
  2002. * @oi: struct omap_hwmod_ocp_if *
  2003. *
  2004. * Registers the omap_hwmod_ocp_if record @oi. Returns -EEXIST if it
  2005. * has already been registered; -EINVAL if @oi is NULL or if the
  2006. * record pointed to by @oi is missing required fields; or 0 upon
  2007. * success.
  2008. *
  2009. * XXX The data should be copied into bootmem, so the original data
  2010. * should be marked __initdata and freed after init. This would allow
  2011. * unneeded omap_hwmods to be freed on multi-OMAP configurations.
  2012. */
  2013. static int __init _register_link(struct omap_hwmod_ocp_if *oi)
  2014. {
  2015. if (!oi || !oi->master || !oi->slave || !oi->user)
  2016. return -EINVAL;
  2017. if (oi->_int_flags & _OCPIF_INT_FLAGS_REGISTERED)
  2018. return -EEXIST;
  2019. pr_debug("omap_hwmod: registering link from %s to %s\n",
  2020. oi->master->name, oi->slave->name);
  2021. /*
  2022. * Register the connected hwmods, if they haven't been
  2023. * registered already
  2024. */
  2025. if (oi->master->_state != _HWMOD_STATE_REGISTERED)
  2026. _register(oi->master);
  2027. if (oi->slave->_state != _HWMOD_STATE_REGISTERED)
  2028. _register(oi->slave);
  2029. _add_link(oi);
  2030. oi->_int_flags |= _OCPIF_INT_FLAGS_REGISTERED;
  2031. return 0;
  2032. }
  2033. /**
  2034. * _alloc_linkspace - allocate large block of hwmod links
  2035. * @ois: pointer to an array of struct omap_hwmod_ocp_if records to count
  2036. *
  2037. * Allocate a large block of struct omap_hwmod_link records. This
  2038. * improves boot time significantly by avoiding the need to allocate
  2039. * individual records one by one. If the number of records to
  2040. * allocate in the block hasn't been manually specified, this function
  2041. * will count the number of struct omap_hwmod_ocp_if records in @ois
  2042. * and use that to determine the allocation size. For SoC families
  2043. * that require multiple list registrations, such as OMAP3xxx, this
  2044. * estimation process isn't optimal, so manual estimation is advised
  2045. * in those cases. Returns -EEXIST if the allocation has already occurred
  2046. * or 0 upon success.
  2047. */
  2048. static int __init _alloc_linkspace(struct omap_hwmod_ocp_if **ois)
  2049. {
  2050. unsigned int i = 0;
  2051. unsigned int sz;
  2052. if (linkspace) {
  2053. WARN(1, "linkspace already allocated\n");
  2054. return -EEXIST;
  2055. }
  2056. if (max_ls == 0)
  2057. while (ois[i++])
  2058. max_ls += LINKS_PER_OCP_IF;
  2059. sz = sizeof(struct omap_hwmod_link) * max_ls;
  2060. pr_debug("omap_hwmod: %s: allocating %d byte linkspace (%d links)\n",
  2061. __func__, sz, max_ls);
  2062. linkspace = alloc_bootmem(sz);
  2063. memset(linkspace, 0, sz);
  2064. return 0;
  2065. }
  2066. /* Public functions */
  2067. u32 omap_hwmod_read(struct omap_hwmod *oh, u16 reg_offs)
  2068. {
  2069. if (oh->flags & HWMOD_16BIT_REG)
  2070. return __raw_readw(oh->_mpu_rt_va + reg_offs);
  2071. else
  2072. return __raw_readl(oh->_mpu_rt_va + reg_offs);
  2073. }
  2074. void omap_hwmod_write(u32 v, struct omap_hwmod *oh, u16 reg_offs)
  2075. {
  2076. if (oh->flags & HWMOD_16BIT_REG)
  2077. __raw_writew(v, oh->_mpu_rt_va + reg_offs);
  2078. else
  2079. __raw_writel(v, oh->_mpu_rt_va + reg_offs);
  2080. }
  2081. /**
  2082. * omap_hwmod_softreset - reset a module via SYSCONFIG.SOFTRESET bit
  2083. * @oh: struct omap_hwmod *
  2084. *
  2085. * This is a public function exposed to drivers. Some drivers may need to do
  2086. * some settings before and after resetting the device. Those drivers after
  2087. * doing the necessary settings could use this function to start a reset by
  2088. * setting the SYSCONFIG.SOFTRESET bit.
  2089. */
  2090. int omap_hwmod_softreset(struct omap_hwmod *oh)
  2091. {
  2092. u32 v;
  2093. int ret;
  2094. if (!oh || !(oh->_sysc_cache))
  2095. return -EINVAL;
  2096. v = oh->_sysc_cache;
  2097. ret = _set_softreset(oh, &v);
  2098. if (ret)
  2099. goto error;
  2100. _write_sysconfig(v, oh);
  2101. error:
  2102. return ret;
  2103. }
  2104. /**
  2105. * omap_hwmod_set_slave_idlemode - set the hwmod's OCP slave idlemode
  2106. * @oh: struct omap_hwmod *
  2107. * @idlemode: SIDLEMODE field bits (shifted to bit 0)
  2108. *
  2109. * Sets the IP block's OCP slave idlemode in hardware, and updates our
  2110. * local copy. Intended to be used by drivers that have some erratum
  2111. * that requires direct manipulation of the SIDLEMODE bits. Returns
  2112. * -EINVAL if @oh is null, or passes along the return value from
  2113. * _set_slave_idlemode().
  2114. *
  2115. * XXX Does this function have any current users? If not, we should
  2116. * remove it; it is better to let the rest of the hwmod code handle this.
  2117. * Any users of this function should be scrutinized carefully.
  2118. */
  2119. int omap_hwmod_set_slave_idlemode(struct omap_hwmod *oh, u8 idlemode)
  2120. {
  2121. u32 v;
  2122. int retval = 0;
  2123. if (!oh)
  2124. return -EINVAL;
  2125. v = oh->_sysc_cache;
  2126. retval = _set_slave_idlemode(oh, idlemode, &v);
  2127. if (!retval)
  2128. _write_sysconfig(v, oh);
  2129. return retval;
  2130. }
  2131. /**
  2132. * omap_hwmod_lookup - look up a registered omap_hwmod by name
  2133. * @name: name of the omap_hwmod to look up
  2134. *
  2135. * Given a @name of an omap_hwmod, return a pointer to the registered
  2136. * struct omap_hwmod *, or NULL upon error.
  2137. */
  2138. struct omap_hwmod *omap_hwmod_lookup(const char *name)
  2139. {
  2140. struct omap_hwmod *oh;
  2141. if (!name)
  2142. return NULL;
  2143. oh = _lookup(name);
  2144. return oh;
  2145. }
  2146. /**
  2147. * omap_hwmod_for_each - call function for each registered omap_hwmod
  2148. * @fn: pointer to a callback function
  2149. * @data: void * data to pass to callback function
  2150. *
  2151. * Call @fn for each registered omap_hwmod, passing @data to each
  2152. * function. @fn must return 0 for success or any other value for
  2153. * failure. If @fn returns non-zero, the iteration across omap_hwmods
  2154. * will stop and the non-zero return value will be passed to the
  2155. * caller of omap_hwmod_for_each(). @fn is called with
  2156. * omap_hwmod_for_each() held.
  2157. */
  2158. int omap_hwmod_for_each(int (*fn)(struct omap_hwmod *oh, void *data),
  2159. void *data)
  2160. {
  2161. struct omap_hwmod *temp_oh;
  2162. int ret = 0;
  2163. if (!fn)
  2164. return -EINVAL;
  2165. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  2166. ret = (*fn)(temp_oh, data);
  2167. if (ret)
  2168. break;
  2169. }
  2170. return ret;
  2171. }
  2172. /**
  2173. * omap_hwmod_register_links - register an array of hwmod links
  2174. * @ois: pointer to an array of omap_hwmod_ocp_if to register
  2175. *
  2176. * Intended to be called early in boot before the clock framework is
  2177. * initialized. If @ois is not null, will register all omap_hwmods
  2178. * listed in @ois that are valid for this chip. Returns -EINVAL if
  2179. * omap_hwmod_init() hasn't been called before calling this function,
  2180. * -ENOMEM if the link memory area can't be allocated, or 0 upon
  2181. * success.
  2182. */
  2183. int __init omap_hwmod_register_links(struct omap_hwmod_ocp_if **ois)
  2184. {
  2185. int r, i;
  2186. if (!inited)
  2187. return -EINVAL;
  2188. if (!ois)
  2189. return 0;
  2190. if (!linkspace) {
  2191. if (_alloc_linkspace(ois)) {
  2192. pr_err("omap_hwmod: could not allocate link space\n");
  2193. return -ENOMEM;
  2194. }
  2195. }
  2196. i = 0;
  2197. do {
  2198. r = _register_link(ois[i]);
  2199. WARN(r && r != -EEXIST,
  2200. "omap_hwmod: _register_link(%s -> %s) returned %d\n",
  2201. ois[i]->master->name, ois[i]->slave->name, r);
  2202. } while (ois[++i]);
  2203. return 0;
  2204. }
  2205. /**
  2206. * _ensure_mpu_hwmod_is_setup - ensure the MPU SS hwmod is init'ed and set up
  2207. * @oh: pointer to the hwmod currently being set up (usually not the MPU)
  2208. *
  2209. * If the hwmod data corresponding to the MPU subsystem IP block
  2210. * hasn't been initialized and set up yet, do so now. This must be
  2211. * done first since sleep dependencies may be added from other hwmods
  2212. * to the MPU. Intended to be called only by omap_hwmod_setup*(). No
  2213. * return value.
  2214. */
  2215. static void __init _ensure_mpu_hwmod_is_setup(struct omap_hwmod *oh)
  2216. {
  2217. if (!mpu_oh || mpu_oh->_state == _HWMOD_STATE_UNKNOWN)
  2218. pr_err("omap_hwmod: %s: MPU initiator hwmod %s not yet registered\n",
  2219. __func__, MPU_INITIATOR_NAME);
  2220. else if (mpu_oh->_state == _HWMOD_STATE_REGISTERED && oh != mpu_oh)
  2221. omap_hwmod_setup_one(MPU_INITIATOR_NAME);
  2222. }
  2223. /**
  2224. * omap_hwmod_setup_one - set up a single hwmod
  2225. * @oh_name: const char * name of the already-registered hwmod to set up
  2226. *
  2227. * Initialize and set up a single hwmod. Intended to be used for a
  2228. * small number of early devices, such as the timer IP blocks used for
  2229. * the scheduler clock. Must be called after omap2_clk_init().
  2230. * Resolves the struct clk names to struct clk pointers for each
  2231. * registered omap_hwmod. Also calls _setup() on each hwmod. Returns
  2232. * -EINVAL upon error or 0 upon success.
  2233. */
  2234. int __init omap_hwmod_setup_one(const char *oh_name)
  2235. {
  2236. struct omap_hwmod *oh;
  2237. pr_debug("omap_hwmod: %s: %s\n", oh_name, __func__);
  2238. oh = _lookup(oh_name);
  2239. if (!oh) {
  2240. WARN(1, "omap_hwmod: %s: hwmod not yet registered\n", oh_name);
  2241. return -EINVAL;
  2242. }
  2243. _ensure_mpu_hwmod_is_setup(oh);
  2244. _init(oh, NULL);
  2245. _setup(oh, NULL);
  2246. return 0;
  2247. }
  2248. /**
  2249. * omap_hwmod_setup_all - set up all registered IP blocks
  2250. *
  2251. * Initialize and set up all IP blocks registered with the hwmod code.
  2252. * Must be called after omap2_clk_init(). Resolves the struct clk
  2253. * names to struct clk pointers for each registered omap_hwmod. Also
  2254. * calls _setup() on each hwmod. Returns 0 upon success.
  2255. */
  2256. static int __init omap_hwmod_setup_all(void)
  2257. {
  2258. _ensure_mpu_hwmod_is_setup(NULL);
  2259. omap_hwmod_for_each(_init, NULL);
  2260. omap_hwmod_for_each(_setup, NULL);
  2261. return 0;
  2262. }
  2263. core_initcall(omap_hwmod_setup_all);
  2264. /**
  2265. * omap_hwmod_enable - enable an omap_hwmod
  2266. * @oh: struct omap_hwmod *
  2267. *
  2268. * Enable an omap_hwmod @oh. Intended to be called by omap_device_enable().
  2269. * Returns -EINVAL on error or passes along the return value from _enable().
  2270. */
  2271. int omap_hwmod_enable(struct omap_hwmod *oh)
  2272. {
  2273. int r;
  2274. unsigned long flags;
  2275. if (!oh)
  2276. return -EINVAL;
  2277. spin_lock_irqsave(&oh->_lock, flags);
  2278. r = _enable(oh);
  2279. spin_unlock_irqrestore(&oh->_lock, flags);
  2280. return r;
  2281. }
  2282. /**
  2283. * omap_hwmod_idle - idle an omap_hwmod
  2284. * @oh: struct omap_hwmod *
  2285. *
  2286. * Idle an omap_hwmod @oh. Intended to be called by omap_device_idle().
  2287. * Returns -EINVAL on error or passes along the return value from _idle().
  2288. */
  2289. int omap_hwmod_idle(struct omap_hwmod *oh)
  2290. {
  2291. unsigned long flags;
  2292. if (!oh)
  2293. return -EINVAL;
  2294. spin_lock_irqsave(&oh->_lock, flags);
  2295. _idle(oh);
  2296. spin_unlock_irqrestore(&oh->_lock, flags);
  2297. return 0;
  2298. }
  2299. /**
  2300. * omap_hwmod_shutdown - shutdown an omap_hwmod
  2301. * @oh: struct omap_hwmod *
  2302. *
  2303. * Shutdown an omap_hwmod @oh. Intended to be called by
  2304. * omap_device_shutdown(). Returns -EINVAL on error or passes along
  2305. * the return value from _shutdown().
  2306. */
  2307. int omap_hwmod_shutdown(struct omap_hwmod *oh)
  2308. {
  2309. unsigned long flags;
  2310. if (!oh)
  2311. return -EINVAL;
  2312. spin_lock_irqsave(&oh->_lock, flags);
  2313. _shutdown(oh);
  2314. spin_unlock_irqrestore(&oh->_lock, flags);
  2315. return 0;
  2316. }
  2317. /**
  2318. * omap_hwmod_enable_clocks - enable main_clk, all interface clocks
  2319. * @oh: struct omap_hwmod *oh
  2320. *
  2321. * Intended to be called by the omap_device code.
  2322. */
  2323. int omap_hwmod_enable_clocks(struct omap_hwmod *oh)
  2324. {
  2325. unsigned long flags;
  2326. spin_lock_irqsave(&oh->_lock, flags);
  2327. _enable_clocks(oh);
  2328. spin_unlock_irqrestore(&oh->_lock, flags);
  2329. return 0;
  2330. }
  2331. /**
  2332. * omap_hwmod_disable_clocks - disable main_clk, all interface clocks
  2333. * @oh: struct omap_hwmod *oh
  2334. *
  2335. * Intended to be called by the omap_device code.
  2336. */
  2337. int omap_hwmod_disable_clocks(struct omap_hwmod *oh)
  2338. {
  2339. unsigned long flags;
  2340. spin_lock_irqsave(&oh->_lock, flags);
  2341. _disable_clocks(oh);
  2342. spin_unlock_irqrestore(&oh->_lock, flags);
  2343. return 0;
  2344. }
  2345. /**
  2346. * omap_hwmod_ocp_barrier - wait for posted writes against the hwmod to complete
  2347. * @oh: struct omap_hwmod *oh
  2348. *
  2349. * Intended to be called by drivers and core code when all posted
  2350. * writes to a device must complete before continuing further
  2351. * execution (for example, after clearing some device IRQSTATUS
  2352. * register bits)
  2353. *
  2354. * XXX what about targets with multiple OCP threads?
  2355. */
  2356. void omap_hwmod_ocp_barrier(struct omap_hwmod *oh)
  2357. {
  2358. BUG_ON(!oh);
  2359. if (!oh->class->sysc || !oh->class->sysc->sysc_flags) {
  2360. WARN(1, "omap_device: %s: OCP barrier impossible due to device configuration\n",
  2361. oh->name);
  2362. return;
  2363. }
  2364. /*
  2365. * Forces posted writes to complete on the OCP thread handling
  2366. * register writes
  2367. */
  2368. omap_hwmod_read(oh, oh->class->sysc->sysc_offs);
  2369. }
  2370. /**
  2371. * omap_hwmod_reset - reset the hwmod
  2372. * @oh: struct omap_hwmod *
  2373. *
  2374. * Under some conditions, a driver may wish to reset the entire device.
  2375. * Called from omap_device code. Returns -EINVAL on error or passes along
  2376. * the return value from _reset().
  2377. */
  2378. int omap_hwmod_reset(struct omap_hwmod *oh)
  2379. {
  2380. int r;
  2381. unsigned long flags;
  2382. if (!oh)
  2383. return -EINVAL;
  2384. spin_lock_irqsave(&oh->_lock, flags);
  2385. r = _reset(oh);
  2386. spin_unlock_irqrestore(&oh->_lock, flags);
  2387. return r;
  2388. }
  2389. /*
  2390. * IP block data retrieval functions
  2391. */
  2392. /**
  2393. * omap_hwmod_count_resources - count number of struct resources needed by hwmod
  2394. * @oh: struct omap_hwmod *
  2395. * @res: pointer to the first element of an array of struct resource to fill
  2396. *
  2397. * Count the number of struct resource array elements necessary to
  2398. * contain omap_hwmod @oh resources. Intended to be called by code
  2399. * that registers omap_devices. Intended to be used to determine the
  2400. * size of a dynamically-allocated struct resource array, before
  2401. * calling omap_hwmod_fill_resources(). Returns the number of struct
  2402. * resource array elements needed.
  2403. *
  2404. * XXX This code is not optimized. It could attempt to merge adjacent
  2405. * resource IDs.
  2406. *
  2407. */
  2408. int omap_hwmod_count_resources(struct omap_hwmod *oh)
  2409. {
  2410. struct omap_hwmod_ocp_if *os;
  2411. struct list_head *p;
  2412. int ret;
  2413. int i = 0;
  2414. ret = _count_mpu_irqs(oh) + _count_sdma_reqs(oh);
  2415. p = oh->slave_ports.next;
  2416. while (i < oh->slaves_cnt) {
  2417. os = _fetch_next_ocp_if(&p, &i);
  2418. ret += _count_ocp_if_addr_spaces(os);
  2419. }
  2420. return ret;
  2421. }
  2422. /**
  2423. * omap_hwmod_fill_resources - fill struct resource array with hwmod data
  2424. * @oh: struct omap_hwmod *
  2425. * @res: pointer to the first element of an array of struct resource to fill
  2426. *
  2427. * Fill the struct resource array @res with resource data from the
  2428. * omap_hwmod @oh. Intended to be called by code that registers
  2429. * omap_devices. See also omap_hwmod_count_resources(). Returns the
  2430. * number of array elements filled.
  2431. */
  2432. int omap_hwmod_fill_resources(struct omap_hwmod *oh, struct resource *res)
  2433. {
  2434. struct omap_hwmod_ocp_if *os;
  2435. struct list_head *p;
  2436. int i, j, mpu_irqs_cnt, sdma_reqs_cnt, addr_cnt;
  2437. int r = 0;
  2438. /* For each IRQ, DMA, memory area, fill in array.*/
  2439. mpu_irqs_cnt = _count_mpu_irqs(oh);
  2440. for (i = 0; i < mpu_irqs_cnt; i++) {
  2441. (res + r)->name = (oh->mpu_irqs + i)->name;
  2442. (res + r)->start = (oh->mpu_irqs + i)->irq;
  2443. (res + r)->end = (oh->mpu_irqs + i)->irq;
  2444. (res + r)->flags = IORESOURCE_IRQ;
  2445. r++;
  2446. }
  2447. sdma_reqs_cnt = _count_sdma_reqs(oh);
  2448. for (i = 0; i < sdma_reqs_cnt; i++) {
  2449. (res + r)->name = (oh->sdma_reqs + i)->name;
  2450. (res + r)->start = (oh->sdma_reqs + i)->dma_req;
  2451. (res + r)->end = (oh->sdma_reqs + i)->dma_req;
  2452. (res + r)->flags = IORESOURCE_DMA;
  2453. r++;
  2454. }
  2455. p = oh->slave_ports.next;
  2456. i = 0;
  2457. while (i < oh->slaves_cnt) {
  2458. os = _fetch_next_ocp_if(&p, &i);
  2459. addr_cnt = _count_ocp_if_addr_spaces(os);
  2460. for (j = 0; j < addr_cnt; j++) {
  2461. (res + r)->name = (os->addr + j)->name;
  2462. (res + r)->start = (os->addr + j)->pa_start;
  2463. (res + r)->end = (os->addr + j)->pa_end;
  2464. (res + r)->flags = IORESOURCE_MEM;
  2465. r++;
  2466. }
  2467. }
  2468. return r;
  2469. }
  2470. /**
  2471. * omap_hwmod_get_resource_byname - fetch IP block integration data by name
  2472. * @oh: struct omap_hwmod * to operate on
  2473. * @type: one of the IORESOURCE_* constants from include/linux/ioport.h
  2474. * @name: pointer to the name of the data to fetch (optional)
  2475. * @rsrc: pointer to a struct resource, allocated by the caller
  2476. *
  2477. * Retrieve MPU IRQ, SDMA request line, or address space start/end
  2478. * data for the IP block pointed to by @oh. The data will be filled
  2479. * into a struct resource record pointed to by @rsrc. The struct
  2480. * resource must be allocated by the caller. When @name is non-null,
  2481. * the data associated with the matching entry in the IRQ/SDMA/address
  2482. * space hwmod data arrays will be returned. If @name is null, the
  2483. * first array entry will be returned. Data order is not meaningful
  2484. * in hwmod data, so callers are strongly encouraged to use a non-null
  2485. * @name whenever possible to avoid unpredictable effects if hwmod
  2486. * data is later added that causes data ordering to change. This
  2487. * function is only intended for use by OMAP core code. Device
  2488. * drivers should not call this function - the appropriate bus-related
  2489. * data accessor functions should be used instead. Returns 0 upon
  2490. * success or a negative error code upon error.
  2491. */
  2492. int omap_hwmod_get_resource_byname(struct omap_hwmod *oh, unsigned int type,
  2493. const char *name, struct resource *rsrc)
  2494. {
  2495. int r;
  2496. unsigned int irq, dma;
  2497. u32 pa_start, pa_end;
  2498. if (!oh || !rsrc)
  2499. return -EINVAL;
  2500. if (type == IORESOURCE_IRQ) {
  2501. r = _get_mpu_irq_by_name(oh, name, &irq);
  2502. if (r)
  2503. return r;
  2504. rsrc->start = irq;
  2505. rsrc->end = irq;
  2506. } else if (type == IORESOURCE_DMA) {
  2507. r = _get_sdma_req_by_name(oh, name, &dma);
  2508. if (r)
  2509. return r;
  2510. rsrc->start = dma;
  2511. rsrc->end = dma;
  2512. } else if (type == IORESOURCE_MEM) {
  2513. r = _get_addr_space_by_name(oh, name, &pa_start, &pa_end);
  2514. if (r)
  2515. return r;
  2516. rsrc->start = pa_start;
  2517. rsrc->end = pa_end;
  2518. } else {
  2519. return -EINVAL;
  2520. }
  2521. rsrc->flags = type;
  2522. rsrc->name = name;
  2523. return 0;
  2524. }
  2525. /**
  2526. * omap_hwmod_get_pwrdm - return pointer to this module's main powerdomain
  2527. * @oh: struct omap_hwmod *
  2528. *
  2529. * Return the powerdomain pointer associated with the OMAP module
  2530. * @oh's main clock. If @oh does not have a main clk, return the
  2531. * powerdomain associated with the interface clock associated with the
  2532. * module's MPU port. (XXX Perhaps this should use the SDMA port
  2533. * instead?) Returns NULL on error, or a struct powerdomain * on
  2534. * success.
  2535. */
  2536. struct powerdomain *omap_hwmod_get_pwrdm(struct omap_hwmod *oh)
  2537. {
  2538. struct clk *c;
  2539. struct omap_hwmod_ocp_if *oi;
  2540. if (!oh)
  2541. return NULL;
  2542. if (oh->_clk) {
  2543. c = oh->_clk;
  2544. } else {
  2545. oi = _find_mpu_rt_port(oh);
  2546. if (!oi)
  2547. return NULL;
  2548. c = oi->_clk;
  2549. }
  2550. if (!c->clkdm)
  2551. return NULL;
  2552. return c->clkdm->pwrdm.ptr;
  2553. }
  2554. /**
  2555. * omap_hwmod_get_mpu_rt_va - return the module's base address (for the MPU)
  2556. * @oh: struct omap_hwmod *
  2557. *
  2558. * Returns the virtual address corresponding to the beginning of the
  2559. * module's register target, in the address range that is intended to
  2560. * be used by the MPU. Returns the virtual address upon success or NULL
  2561. * upon error.
  2562. */
  2563. void __iomem *omap_hwmod_get_mpu_rt_va(struct omap_hwmod *oh)
  2564. {
  2565. if (!oh)
  2566. return NULL;
  2567. if (oh->_int_flags & _HWMOD_NO_MPU_PORT)
  2568. return NULL;
  2569. if (oh->_state == _HWMOD_STATE_UNKNOWN)
  2570. return NULL;
  2571. return oh->_mpu_rt_va;
  2572. }
  2573. /**
  2574. * omap_hwmod_add_initiator_dep - add sleepdep from @init_oh to @oh
  2575. * @oh: struct omap_hwmod *
  2576. * @init_oh: struct omap_hwmod * (initiator)
  2577. *
  2578. * Add a sleep dependency between the initiator @init_oh and @oh.
  2579. * Intended to be called by DSP/Bridge code via platform_data for the
  2580. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  2581. * code needs to add/del initiator dependencies dynamically
  2582. * before/after accessing a device. Returns the return value from
  2583. * _add_initiator_dep().
  2584. *
  2585. * XXX Keep a usecount in the clockdomain code
  2586. */
  2587. int omap_hwmod_add_initiator_dep(struct omap_hwmod *oh,
  2588. struct omap_hwmod *init_oh)
  2589. {
  2590. return _add_initiator_dep(oh, init_oh);
  2591. }
  2592. /*
  2593. * XXX what about functions for drivers to save/restore ocp_sysconfig
  2594. * for context save/restore operations?
  2595. */
  2596. /**
  2597. * omap_hwmod_del_initiator_dep - remove sleepdep from @init_oh to @oh
  2598. * @oh: struct omap_hwmod *
  2599. * @init_oh: struct omap_hwmod * (initiator)
  2600. *
  2601. * Remove a sleep dependency between the initiator @init_oh and @oh.
  2602. * Intended to be called by DSP/Bridge code via platform_data for the
  2603. * DSP case; and by the DMA code in the sDMA case. DMA code, *Bridge
  2604. * code needs to add/del initiator dependencies dynamically
  2605. * before/after accessing a device. Returns the return value from
  2606. * _del_initiator_dep().
  2607. *
  2608. * XXX Keep a usecount in the clockdomain code
  2609. */
  2610. int omap_hwmod_del_initiator_dep(struct omap_hwmod *oh,
  2611. struct omap_hwmod *init_oh)
  2612. {
  2613. return _del_initiator_dep(oh, init_oh);
  2614. }
  2615. /**
  2616. * omap_hwmod_enable_wakeup - allow device to wake up the system
  2617. * @oh: struct omap_hwmod *
  2618. *
  2619. * Sets the module OCP socket ENAWAKEUP bit to allow the module to
  2620. * send wakeups to the PRCM, and enable I/O ring wakeup events for
  2621. * this IP block if it has dynamic mux entries. Eventually this
  2622. * should set PRCM wakeup registers to cause the PRCM to receive
  2623. * wakeup events from the module. Does not set any wakeup routing
  2624. * registers beyond this point - if the module is to wake up any other
  2625. * module or subsystem, that must be set separately. Called by
  2626. * omap_device code. Returns -EINVAL on error or 0 upon success.
  2627. */
  2628. int omap_hwmod_enable_wakeup(struct omap_hwmod *oh)
  2629. {
  2630. unsigned long flags;
  2631. u32 v;
  2632. spin_lock_irqsave(&oh->_lock, flags);
  2633. if (oh->class->sysc &&
  2634. (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
  2635. v = oh->_sysc_cache;
  2636. _enable_wakeup(oh, &v);
  2637. _write_sysconfig(v, oh);
  2638. }
  2639. _set_idle_ioring_wakeup(oh, true);
  2640. spin_unlock_irqrestore(&oh->_lock, flags);
  2641. return 0;
  2642. }
  2643. /**
  2644. * omap_hwmod_disable_wakeup - prevent device from waking the system
  2645. * @oh: struct omap_hwmod *
  2646. *
  2647. * Clears the module OCP socket ENAWAKEUP bit to prevent the module
  2648. * from sending wakeups to the PRCM, and disable I/O ring wakeup
  2649. * events for this IP block if it has dynamic mux entries. Eventually
  2650. * this should clear PRCM wakeup registers to cause the PRCM to ignore
  2651. * wakeup events from the module. Does not set any wakeup routing
  2652. * registers beyond this point - if the module is to wake up any other
  2653. * module or subsystem, that must be set separately. Called by
  2654. * omap_device code. Returns -EINVAL on error or 0 upon success.
  2655. */
  2656. int omap_hwmod_disable_wakeup(struct omap_hwmod *oh)
  2657. {
  2658. unsigned long flags;
  2659. u32 v;
  2660. spin_lock_irqsave(&oh->_lock, flags);
  2661. if (oh->class->sysc &&
  2662. (oh->class->sysc->sysc_flags & SYSC_HAS_ENAWAKEUP)) {
  2663. v = oh->_sysc_cache;
  2664. _disable_wakeup(oh, &v);
  2665. _write_sysconfig(v, oh);
  2666. }
  2667. _set_idle_ioring_wakeup(oh, false);
  2668. spin_unlock_irqrestore(&oh->_lock, flags);
  2669. return 0;
  2670. }
  2671. /**
  2672. * omap_hwmod_assert_hardreset - assert the HW reset line of submodules
  2673. * contained in the hwmod module.
  2674. * @oh: struct omap_hwmod *
  2675. * @name: name of the reset line to lookup and assert
  2676. *
  2677. * Some IP like dsp, ipu or iva contain processor that require
  2678. * an HW reset line to be assert / deassert in order to enable fully
  2679. * the IP. Returns -EINVAL if @oh is null or if the operation is not
  2680. * yet supported on this OMAP; otherwise, passes along the return value
  2681. * from _assert_hardreset().
  2682. */
  2683. int omap_hwmod_assert_hardreset(struct omap_hwmod *oh, const char *name)
  2684. {
  2685. int ret;
  2686. unsigned long flags;
  2687. if (!oh)
  2688. return -EINVAL;
  2689. spin_lock_irqsave(&oh->_lock, flags);
  2690. ret = _assert_hardreset(oh, name);
  2691. spin_unlock_irqrestore(&oh->_lock, flags);
  2692. return ret;
  2693. }
  2694. /**
  2695. * omap_hwmod_deassert_hardreset - deassert the HW reset line of submodules
  2696. * contained in the hwmod module.
  2697. * @oh: struct omap_hwmod *
  2698. * @name: name of the reset line to look up and deassert
  2699. *
  2700. * Some IP like dsp, ipu or iva contain processor that require
  2701. * an HW reset line to be assert / deassert in order to enable fully
  2702. * the IP. Returns -EINVAL if @oh is null or if the operation is not
  2703. * yet supported on this OMAP; otherwise, passes along the return value
  2704. * from _deassert_hardreset().
  2705. */
  2706. int omap_hwmod_deassert_hardreset(struct omap_hwmod *oh, const char *name)
  2707. {
  2708. int ret;
  2709. unsigned long flags;
  2710. if (!oh)
  2711. return -EINVAL;
  2712. spin_lock_irqsave(&oh->_lock, flags);
  2713. ret = _deassert_hardreset(oh, name);
  2714. spin_unlock_irqrestore(&oh->_lock, flags);
  2715. return ret;
  2716. }
  2717. /**
  2718. * omap_hwmod_read_hardreset - read the HW reset line state of submodules
  2719. * contained in the hwmod module
  2720. * @oh: struct omap_hwmod *
  2721. * @name: name of the reset line to look up and read
  2722. *
  2723. * Return the current state of the hwmod @oh's reset line named @name:
  2724. * returns -EINVAL upon parameter error or if this operation
  2725. * is unsupported on the current OMAP; otherwise, passes along the return
  2726. * value from _read_hardreset().
  2727. */
  2728. int omap_hwmod_read_hardreset(struct omap_hwmod *oh, const char *name)
  2729. {
  2730. int ret;
  2731. unsigned long flags;
  2732. if (!oh)
  2733. return -EINVAL;
  2734. spin_lock_irqsave(&oh->_lock, flags);
  2735. ret = _read_hardreset(oh, name);
  2736. spin_unlock_irqrestore(&oh->_lock, flags);
  2737. return ret;
  2738. }
  2739. /**
  2740. * omap_hwmod_for_each_by_class - call @fn for each hwmod of class @classname
  2741. * @classname: struct omap_hwmod_class name to search for
  2742. * @fn: callback function pointer to call for each hwmod in class @classname
  2743. * @user: arbitrary context data to pass to the callback function
  2744. *
  2745. * For each omap_hwmod of class @classname, call @fn.
  2746. * If the callback function returns something other than
  2747. * zero, the iterator is terminated, and the callback function's return
  2748. * value is passed back to the caller. Returns 0 upon success, -EINVAL
  2749. * if @classname or @fn are NULL, or passes back the error code from @fn.
  2750. */
  2751. int omap_hwmod_for_each_by_class(const char *classname,
  2752. int (*fn)(struct omap_hwmod *oh,
  2753. void *user),
  2754. void *user)
  2755. {
  2756. struct omap_hwmod *temp_oh;
  2757. int ret = 0;
  2758. if (!classname || !fn)
  2759. return -EINVAL;
  2760. pr_debug("omap_hwmod: %s: looking for modules of class %s\n",
  2761. __func__, classname);
  2762. list_for_each_entry(temp_oh, &omap_hwmod_list, node) {
  2763. if (!strcmp(temp_oh->class->name, classname)) {
  2764. pr_debug("omap_hwmod: %s: %s: calling callback fn\n",
  2765. __func__, temp_oh->name);
  2766. ret = (*fn)(temp_oh, user);
  2767. if (ret)
  2768. break;
  2769. }
  2770. }
  2771. if (ret)
  2772. pr_debug("omap_hwmod: %s: iterator terminated early: %d\n",
  2773. __func__, ret);
  2774. return ret;
  2775. }
  2776. /**
  2777. * omap_hwmod_set_postsetup_state - set the post-_setup() state for this hwmod
  2778. * @oh: struct omap_hwmod *
  2779. * @state: state that _setup() should leave the hwmod in
  2780. *
  2781. * Sets the hwmod state that @oh will enter at the end of _setup()
  2782. * (called by omap_hwmod_setup_*()). See also the documentation
  2783. * for _setup_postsetup(), above. Returns 0 upon success or
  2784. * -EINVAL if there is a problem with the arguments or if the hwmod is
  2785. * in the wrong state.
  2786. */
  2787. int omap_hwmod_set_postsetup_state(struct omap_hwmod *oh, u8 state)
  2788. {
  2789. int ret;
  2790. unsigned long flags;
  2791. if (!oh)
  2792. return -EINVAL;
  2793. if (state != _HWMOD_STATE_DISABLED &&
  2794. state != _HWMOD_STATE_ENABLED &&
  2795. state != _HWMOD_STATE_IDLE)
  2796. return -EINVAL;
  2797. spin_lock_irqsave(&oh->_lock, flags);
  2798. if (oh->_state != _HWMOD_STATE_REGISTERED) {
  2799. ret = -EINVAL;
  2800. goto ohsps_unlock;
  2801. }
  2802. oh->_postsetup_state = state;
  2803. ret = 0;
  2804. ohsps_unlock:
  2805. spin_unlock_irqrestore(&oh->_lock, flags);
  2806. return ret;
  2807. }
  2808. /**
  2809. * omap_hwmod_get_context_loss_count - get lost context count
  2810. * @oh: struct omap_hwmod *
  2811. *
  2812. * Query the powerdomain of of @oh to get the context loss
  2813. * count for this device.
  2814. *
  2815. * Returns the context loss count of the powerdomain assocated with @oh
  2816. * upon success, or zero if no powerdomain exists for @oh.
  2817. */
  2818. int omap_hwmod_get_context_loss_count(struct omap_hwmod *oh)
  2819. {
  2820. struct powerdomain *pwrdm;
  2821. int ret = 0;
  2822. pwrdm = omap_hwmod_get_pwrdm(oh);
  2823. if (pwrdm)
  2824. ret = pwrdm_get_context_loss_count(pwrdm);
  2825. return ret;
  2826. }
  2827. /**
  2828. * omap_hwmod_no_setup_reset - prevent a hwmod from being reset upon setup
  2829. * @oh: struct omap_hwmod *
  2830. *
  2831. * Prevent the hwmod @oh from being reset during the setup process.
  2832. * Intended for use by board-*.c files on boards with devices that
  2833. * cannot tolerate being reset. Must be called before the hwmod has
  2834. * been set up. Returns 0 upon success or negative error code upon
  2835. * failure.
  2836. */
  2837. int omap_hwmod_no_setup_reset(struct omap_hwmod *oh)
  2838. {
  2839. if (!oh)
  2840. return -EINVAL;
  2841. if (oh->_state != _HWMOD_STATE_REGISTERED) {
  2842. pr_err("omap_hwmod: %s: cannot prevent setup reset; in wrong state\n",
  2843. oh->name);
  2844. return -EINVAL;
  2845. }
  2846. oh->flags |= HWMOD_INIT_NO_RESET;
  2847. return 0;
  2848. }
  2849. /**
  2850. * omap_hwmod_pad_route_irq - route an I/O pad wakeup to a particular MPU IRQ
  2851. * @oh: struct omap_hwmod * containing hwmod mux entries
  2852. * @pad_idx: array index in oh->mux of the hwmod mux entry to route wakeup
  2853. * @irq_idx: the hwmod mpu_irqs array index of the IRQ to trigger on wakeup
  2854. *
  2855. * When an I/O pad wakeup arrives for the dynamic or wakeup hwmod mux
  2856. * entry number @pad_idx for the hwmod @oh, trigger the interrupt
  2857. * service routine for the hwmod's mpu_irqs array index @irq_idx. If
  2858. * this function is not called for a given pad_idx, then the ISR
  2859. * associated with @oh's first MPU IRQ will be triggered when an I/O
  2860. * pad wakeup occurs on that pad. Note that @pad_idx is the index of
  2861. * the _dynamic or wakeup_ entry: if there are other entries not
  2862. * marked with OMAP_DEVICE_PAD_WAKEUP or OMAP_DEVICE_PAD_REMUX, these
  2863. * entries are NOT COUNTED in the dynamic pad index. This function
  2864. * must be called separately for each pad that requires its interrupt
  2865. * to be re-routed this way. Returns -EINVAL if there is an argument
  2866. * problem or if @oh does not have hwmod mux entries or MPU IRQs;
  2867. * returns -ENOMEM if memory cannot be allocated; or 0 upon success.
  2868. *
  2869. * XXX This function interface is fragile. Rather than using array
  2870. * indexes, which are subject to unpredictable change, it should be
  2871. * using hwmod IRQ names, and some other stable key for the hwmod mux
  2872. * pad records.
  2873. */
  2874. int omap_hwmod_pad_route_irq(struct omap_hwmod *oh, int pad_idx, int irq_idx)
  2875. {
  2876. int nr_irqs;
  2877. might_sleep();
  2878. if (!oh || !oh->mux || !oh->mpu_irqs || pad_idx < 0 ||
  2879. pad_idx >= oh->mux->nr_pads_dynamic)
  2880. return -EINVAL;
  2881. /* Check the number of available mpu_irqs */
  2882. for (nr_irqs = 0; oh->mpu_irqs[nr_irqs].irq >= 0; nr_irqs++)
  2883. ;
  2884. if (irq_idx >= nr_irqs)
  2885. return -EINVAL;
  2886. if (!oh->mux->irqs) {
  2887. /* XXX What frees this? */
  2888. oh->mux->irqs = kzalloc(sizeof(int) * oh->mux->nr_pads_dynamic,
  2889. GFP_KERNEL);
  2890. if (!oh->mux->irqs)
  2891. return -ENOMEM;
  2892. }
  2893. oh->mux->irqs[pad_idx] = irq_idx;
  2894. return 0;
  2895. }
  2896. /**
  2897. * omap_hwmod_init - initialize the hwmod code
  2898. *
  2899. * Sets up some function pointers needed by the hwmod code to operate on the
  2900. * currently-booted SoC. Intended to be called once during kernel init
  2901. * before any hwmods are registered. No return value.
  2902. */
  2903. void __init omap_hwmod_init(void)
  2904. {
  2905. if (cpu_is_omap44xx()) {
  2906. soc_ops.enable_module = _omap4_enable_module;
  2907. soc_ops.disable_module = _omap4_disable_module;
  2908. }
  2909. inited = true;
  2910. }