omap_hwmod.c 112 KB

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