ste_dma40.c 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762
  1. /*
  2. * Copyright (C) Ericsson AB 2007-2008
  3. * Copyright (C) ST-Ericsson SA 2008-2010
  4. * Author: Per Forlin <per.forlin@stericsson.com> for ST-Ericsson
  5. * Author: Jonas Aaberg <jonas.aberg@stericsson.com> for ST-Ericsson
  6. * License terms: GNU General Public License (GPL) version 2
  7. */
  8. #include <linux/dma-mapping.h>
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/export.h>
  12. #include <linux/dmaengine.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/log2.h>
  17. #include <linux/pm.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/err.h>
  20. #include <linux/of.h>
  21. #include <linux/of_dma.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/platform_data/dma-ste-dma40.h>
  25. #include "dmaengine.h"
  26. #include "ste_dma40_ll.h"
  27. #define D40_NAME "dma40"
  28. #define D40_PHY_CHAN -1
  29. /* For masking out/in 2 bit channel positions */
  30. #define D40_CHAN_POS(chan) (2 * (chan / 2))
  31. #define D40_CHAN_POS_MASK(chan) (0x3 << D40_CHAN_POS(chan))
  32. /* Maximum iterations taken before giving up suspending a channel */
  33. #define D40_SUSPEND_MAX_IT 500
  34. /* Milliseconds */
  35. #define DMA40_AUTOSUSPEND_DELAY 100
  36. /* Hardware requirement on LCLA alignment */
  37. #define LCLA_ALIGNMENT 0x40000
  38. /* Max number of links per event group */
  39. #define D40_LCLA_LINK_PER_EVENT_GRP 128
  40. #define D40_LCLA_END D40_LCLA_LINK_PER_EVENT_GRP
  41. /* Max number of logical channels per physical channel */
  42. #define D40_MAX_LOG_CHAN_PER_PHY 32
  43. /* Attempts before giving up to trying to get pages that are aligned */
  44. #define MAX_LCLA_ALLOC_ATTEMPTS 256
  45. /* Bit markings for allocation map */
  46. #define D40_ALLOC_FREE BIT(31)
  47. #define D40_ALLOC_PHY BIT(30)
  48. #define D40_ALLOC_LOG_FREE 0
  49. #define D40_MEMCPY_MAX_CHANS 8
  50. /* Reserved event lines for memcpy only. */
  51. #define DB8500_DMA_MEMCPY_EV_0 51
  52. #define DB8500_DMA_MEMCPY_EV_1 56
  53. #define DB8500_DMA_MEMCPY_EV_2 57
  54. #define DB8500_DMA_MEMCPY_EV_3 58
  55. #define DB8500_DMA_MEMCPY_EV_4 59
  56. #define DB8500_DMA_MEMCPY_EV_5 60
  57. static int dma40_memcpy_channels[] = {
  58. DB8500_DMA_MEMCPY_EV_0,
  59. DB8500_DMA_MEMCPY_EV_1,
  60. DB8500_DMA_MEMCPY_EV_2,
  61. DB8500_DMA_MEMCPY_EV_3,
  62. DB8500_DMA_MEMCPY_EV_4,
  63. DB8500_DMA_MEMCPY_EV_5,
  64. };
  65. /* Default configuration for physcial memcpy */
  66. static struct stedma40_chan_cfg dma40_memcpy_conf_phy = {
  67. .mode = STEDMA40_MODE_PHYSICAL,
  68. .dir = DMA_MEM_TO_MEM,
  69. .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  70. .src_info.psize = STEDMA40_PSIZE_PHY_1,
  71. .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
  72. .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  73. .dst_info.psize = STEDMA40_PSIZE_PHY_1,
  74. .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
  75. };
  76. /* Default configuration for logical memcpy */
  77. static struct stedma40_chan_cfg dma40_memcpy_conf_log = {
  78. .mode = STEDMA40_MODE_LOGICAL,
  79. .dir = DMA_MEM_TO_MEM,
  80. .src_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  81. .src_info.psize = STEDMA40_PSIZE_LOG_1,
  82. .src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
  83. .dst_info.data_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  84. .dst_info.psize = STEDMA40_PSIZE_LOG_1,
  85. .dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL,
  86. };
  87. /**
  88. * enum 40_command - The different commands and/or statuses.
  89. *
  90. * @D40_DMA_STOP: DMA channel command STOP or status STOPPED,
  91. * @D40_DMA_RUN: The DMA channel is RUNNING of the command RUN.
  92. * @D40_DMA_SUSPEND_REQ: Request the DMA to SUSPEND as soon as possible.
  93. * @D40_DMA_SUSPENDED: The DMA channel is SUSPENDED.
  94. */
  95. enum d40_command {
  96. D40_DMA_STOP = 0,
  97. D40_DMA_RUN = 1,
  98. D40_DMA_SUSPEND_REQ = 2,
  99. D40_DMA_SUSPENDED = 3
  100. };
  101. /*
  102. * enum d40_events - The different Event Enables for the event lines.
  103. *
  104. * @D40_DEACTIVATE_EVENTLINE: De-activate Event line, stopping the logical chan.
  105. * @D40_ACTIVATE_EVENTLINE: Activate the Event line, to start a logical chan.
  106. * @D40_SUSPEND_REQ_EVENTLINE: Requesting for suspending a event line.
  107. * @D40_ROUND_EVENTLINE: Status check for event line.
  108. */
  109. enum d40_events {
  110. D40_DEACTIVATE_EVENTLINE = 0,
  111. D40_ACTIVATE_EVENTLINE = 1,
  112. D40_SUSPEND_REQ_EVENTLINE = 2,
  113. D40_ROUND_EVENTLINE = 3
  114. };
  115. /*
  116. * These are the registers that has to be saved and later restored
  117. * when the DMA hw is powered off.
  118. * TODO: Add save/restore of D40_DREG_GCC on dma40 v3 or later, if that works.
  119. */
  120. static u32 d40_backup_regs[] = {
  121. D40_DREG_LCPA,
  122. D40_DREG_LCLA,
  123. D40_DREG_PRMSE,
  124. D40_DREG_PRMSO,
  125. D40_DREG_PRMOE,
  126. D40_DREG_PRMOO,
  127. };
  128. #define BACKUP_REGS_SZ ARRAY_SIZE(d40_backup_regs)
  129. /*
  130. * since 9540 and 8540 has the same HW revision
  131. * use v4a for 9540 or ealier
  132. * use v4b for 8540 or later
  133. * HW revision:
  134. * DB8500ed has revision 0
  135. * DB8500v1 has revision 2
  136. * DB8500v2 has revision 3
  137. * AP9540v1 has revision 4
  138. * DB8540v1 has revision 4
  139. * TODO: Check if all these registers have to be saved/restored on dma40 v4a
  140. */
  141. static u32 d40_backup_regs_v4a[] = {
  142. D40_DREG_PSEG1,
  143. D40_DREG_PSEG2,
  144. D40_DREG_PSEG3,
  145. D40_DREG_PSEG4,
  146. D40_DREG_PCEG1,
  147. D40_DREG_PCEG2,
  148. D40_DREG_PCEG3,
  149. D40_DREG_PCEG4,
  150. D40_DREG_RSEG1,
  151. D40_DREG_RSEG2,
  152. D40_DREG_RSEG3,
  153. D40_DREG_RSEG4,
  154. D40_DREG_RCEG1,
  155. D40_DREG_RCEG2,
  156. D40_DREG_RCEG3,
  157. D40_DREG_RCEG4,
  158. };
  159. #define BACKUP_REGS_SZ_V4A ARRAY_SIZE(d40_backup_regs_v4a)
  160. static u32 d40_backup_regs_v4b[] = {
  161. D40_DREG_CPSEG1,
  162. D40_DREG_CPSEG2,
  163. D40_DREG_CPSEG3,
  164. D40_DREG_CPSEG4,
  165. D40_DREG_CPSEG5,
  166. D40_DREG_CPCEG1,
  167. D40_DREG_CPCEG2,
  168. D40_DREG_CPCEG3,
  169. D40_DREG_CPCEG4,
  170. D40_DREG_CPCEG5,
  171. D40_DREG_CRSEG1,
  172. D40_DREG_CRSEG2,
  173. D40_DREG_CRSEG3,
  174. D40_DREG_CRSEG4,
  175. D40_DREG_CRSEG5,
  176. D40_DREG_CRCEG1,
  177. D40_DREG_CRCEG2,
  178. D40_DREG_CRCEG3,
  179. D40_DREG_CRCEG4,
  180. D40_DREG_CRCEG5,
  181. };
  182. #define BACKUP_REGS_SZ_V4B ARRAY_SIZE(d40_backup_regs_v4b)
  183. static u32 d40_backup_regs_chan[] = {
  184. D40_CHAN_REG_SSCFG,
  185. D40_CHAN_REG_SSELT,
  186. D40_CHAN_REG_SSPTR,
  187. D40_CHAN_REG_SSLNK,
  188. D40_CHAN_REG_SDCFG,
  189. D40_CHAN_REG_SDELT,
  190. D40_CHAN_REG_SDPTR,
  191. D40_CHAN_REG_SDLNK,
  192. };
  193. #define BACKUP_REGS_SZ_MAX ((BACKUP_REGS_SZ_V4A > BACKUP_REGS_SZ_V4B) ? \
  194. BACKUP_REGS_SZ_V4A : BACKUP_REGS_SZ_V4B)
  195. /**
  196. * struct d40_interrupt_lookup - lookup table for interrupt handler
  197. *
  198. * @src: Interrupt mask register.
  199. * @clr: Interrupt clear register.
  200. * @is_error: true if this is an error interrupt.
  201. * @offset: start delta in the lookup_log_chans in d40_base. If equals to
  202. * D40_PHY_CHAN, the lookup_phy_chans shall be used instead.
  203. */
  204. struct d40_interrupt_lookup {
  205. u32 src;
  206. u32 clr;
  207. bool is_error;
  208. int offset;
  209. };
  210. static struct d40_interrupt_lookup il_v4a[] = {
  211. {D40_DREG_LCTIS0, D40_DREG_LCICR0, false, 0},
  212. {D40_DREG_LCTIS1, D40_DREG_LCICR1, false, 32},
  213. {D40_DREG_LCTIS2, D40_DREG_LCICR2, false, 64},
  214. {D40_DREG_LCTIS3, D40_DREG_LCICR3, false, 96},
  215. {D40_DREG_LCEIS0, D40_DREG_LCICR0, true, 0},
  216. {D40_DREG_LCEIS1, D40_DREG_LCICR1, true, 32},
  217. {D40_DREG_LCEIS2, D40_DREG_LCICR2, true, 64},
  218. {D40_DREG_LCEIS3, D40_DREG_LCICR3, true, 96},
  219. {D40_DREG_PCTIS, D40_DREG_PCICR, false, D40_PHY_CHAN},
  220. {D40_DREG_PCEIS, D40_DREG_PCICR, true, D40_PHY_CHAN},
  221. };
  222. static struct d40_interrupt_lookup il_v4b[] = {
  223. {D40_DREG_CLCTIS1, D40_DREG_CLCICR1, false, 0},
  224. {D40_DREG_CLCTIS2, D40_DREG_CLCICR2, false, 32},
  225. {D40_DREG_CLCTIS3, D40_DREG_CLCICR3, false, 64},
  226. {D40_DREG_CLCTIS4, D40_DREG_CLCICR4, false, 96},
  227. {D40_DREG_CLCTIS5, D40_DREG_CLCICR5, false, 128},
  228. {D40_DREG_CLCEIS1, D40_DREG_CLCICR1, true, 0},
  229. {D40_DREG_CLCEIS2, D40_DREG_CLCICR2, true, 32},
  230. {D40_DREG_CLCEIS3, D40_DREG_CLCICR3, true, 64},
  231. {D40_DREG_CLCEIS4, D40_DREG_CLCICR4, true, 96},
  232. {D40_DREG_CLCEIS5, D40_DREG_CLCICR5, true, 128},
  233. {D40_DREG_CPCTIS, D40_DREG_CPCICR, false, D40_PHY_CHAN},
  234. {D40_DREG_CPCEIS, D40_DREG_CPCICR, true, D40_PHY_CHAN},
  235. };
  236. /**
  237. * struct d40_reg_val - simple lookup struct
  238. *
  239. * @reg: The register.
  240. * @val: The value that belongs to the register in reg.
  241. */
  242. struct d40_reg_val {
  243. unsigned int reg;
  244. unsigned int val;
  245. };
  246. static __initdata struct d40_reg_val dma_init_reg_v4a[] = {
  247. /* Clock every part of the DMA block from start */
  248. { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
  249. /* Interrupts on all logical channels */
  250. { .reg = D40_DREG_LCMIS0, .val = 0xFFFFFFFF},
  251. { .reg = D40_DREG_LCMIS1, .val = 0xFFFFFFFF},
  252. { .reg = D40_DREG_LCMIS2, .val = 0xFFFFFFFF},
  253. { .reg = D40_DREG_LCMIS3, .val = 0xFFFFFFFF},
  254. { .reg = D40_DREG_LCICR0, .val = 0xFFFFFFFF},
  255. { .reg = D40_DREG_LCICR1, .val = 0xFFFFFFFF},
  256. { .reg = D40_DREG_LCICR2, .val = 0xFFFFFFFF},
  257. { .reg = D40_DREG_LCICR3, .val = 0xFFFFFFFF},
  258. { .reg = D40_DREG_LCTIS0, .val = 0xFFFFFFFF},
  259. { .reg = D40_DREG_LCTIS1, .val = 0xFFFFFFFF},
  260. { .reg = D40_DREG_LCTIS2, .val = 0xFFFFFFFF},
  261. { .reg = D40_DREG_LCTIS3, .val = 0xFFFFFFFF}
  262. };
  263. static __initdata struct d40_reg_val dma_init_reg_v4b[] = {
  264. /* Clock every part of the DMA block from start */
  265. { .reg = D40_DREG_GCC, .val = D40_DREG_GCC_ENABLE_ALL},
  266. /* Interrupts on all logical channels */
  267. { .reg = D40_DREG_CLCMIS1, .val = 0xFFFFFFFF},
  268. { .reg = D40_DREG_CLCMIS2, .val = 0xFFFFFFFF},
  269. { .reg = D40_DREG_CLCMIS3, .val = 0xFFFFFFFF},
  270. { .reg = D40_DREG_CLCMIS4, .val = 0xFFFFFFFF},
  271. { .reg = D40_DREG_CLCMIS5, .val = 0xFFFFFFFF},
  272. { .reg = D40_DREG_CLCICR1, .val = 0xFFFFFFFF},
  273. { .reg = D40_DREG_CLCICR2, .val = 0xFFFFFFFF},
  274. { .reg = D40_DREG_CLCICR3, .val = 0xFFFFFFFF},
  275. { .reg = D40_DREG_CLCICR4, .val = 0xFFFFFFFF},
  276. { .reg = D40_DREG_CLCICR5, .val = 0xFFFFFFFF},
  277. { .reg = D40_DREG_CLCTIS1, .val = 0xFFFFFFFF},
  278. { .reg = D40_DREG_CLCTIS2, .val = 0xFFFFFFFF},
  279. { .reg = D40_DREG_CLCTIS3, .val = 0xFFFFFFFF},
  280. { .reg = D40_DREG_CLCTIS4, .val = 0xFFFFFFFF},
  281. { .reg = D40_DREG_CLCTIS5, .val = 0xFFFFFFFF}
  282. };
  283. /**
  284. * struct d40_lli_pool - Structure for keeping LLIs in memory
  285. *
  286. * @base: Pointer to memory area when the pre_alloc_lli's are not large
  287. * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
  288. * pre_alloc_lli is used.
  289. * @dma_addr: DMA address, if mapped
  290. * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
  291. * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
  292. * one buffer to one buffer.
  293. */
  294. struct d40_lli_pool {
  295. void *base;
  296. int size;
  297. dma_addr_t dma_addr;
  298. /* Space for dst and src, plus an extra for padding */
  299. u8 pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
  300. };
  301. /**
  302. * struct d40_desc - A descriptor is one DMA job.
  303. *
  304. * @lli_phy: LLI settings for physical channel. Both src and dst=
  305. * points into the lli_pool, to base if lli_len > 1 or to pre_alloc_lli if
  306. * lli_len equals one.
  307. * @lli_log: Same as above but for logical channels.
  308. * @lli_pool: The pool with two entries pre-allocated.
  309. * @lli_len: Number of llis of current descriptor.
  310. * @lli_current: Number of transferred llis.
  311. * @lcla_alloc: Number of LCLA entries allocated.
  312. * @txd: DMA engine struct. Used for among other things for communication
  313. * during a transfer.
  314. * @node: List entry.
  315. * @is_in_client_list: true if the client owns this descriptor.
  316. * @cyclic: true if this is a cyclic job
  317. *
  318. * This descriptor is used for both logical and physical transfers.
  319. */
  320. struct d40_desc {
  321. /* LLI physical */
  322. struct d40_phy_lli_bidir lli_phy;
  323. /* LLI logical */
  324. struct d40_log_lli_bidir lli_log;
  325. struct d40_lli_pool lli_pool;
  326. int lli_len;
  327. int lli_current;
  328. int lcla_alloc;
  329. struct dma_async_tx_descriptor txd;
  330. struct list_head node;
  331. bool is_in_client_list;
  332. bool cyclic;
  333. };
  334. /**
  335. * struct d40_lcla_pool - LCLA pool settings and data.
  336. *
  337. * @base: The virtual address of LCLA. 18 bit aligned.
  338. * @base_unaligned: The orignal kmalloc pointer, if kmalloc is used.
  339. * This pointer is only there for clean-up on error.
  340. * @pages: The number of pages needed for all physical channels.
  341. * Only used later for clean-up on error
  342. * @lock: Lock to protect the content in this struct.
  343. * @alloc_map: big map over which LCLA entry is own by which job.
  344. */
  345. struct d40_lcla_pool {
  346. void *base;
  347. dma_addr_t dma_addr;
  348. void *base_unaligned;
  349. int pages;
  350. spinlock_t lock;
  351. struct d40_desc **alloc_map;
  352. };
  353. /**
  354. * struct d40_phy_res - struct for handling eventlines mapped to physical
  355. * channels.
  356. *
  357. * @lock: A lock protection this entity.
  358. * @reserved: True if used by secure world or otherwise.
  359. * @num: The physical channel number of this entity.
  360. * @allocated_src: Bit mapped to show which src event line's are mapped to
  361. * this physical channel. Can also be free or physically allocated.
  362. * @allocated_dst: Same as for src but is dst.
  363. * allocated_dst and allocated_src uses the D40_ALLOC* defines as well as
  364. * event line number.
  365. * @use_soft_lli: To mark if the linked lists of channel are managed by SW.
  366. */
  367. struct d40_phy_res {
  368. spinlock_t lock;
  369. bool reserved;
  370. int num;
  371. u32 allocated_src;
  372. u32 allocated_dst;
  373. bool use_soft_lli;
  374. };
  375. struct d40_base;
  376. /**
  377. * struct d40_chan - Struct that describes a channel.
  378. *
  379. * @lock: A spinlock to protect this struct.
  380. * @log_num: The logical number, if any of this channel.
  381. * @pending_tx: The number of pending transfers. Used between interrupt handler
  382. * and tasklet.
  383. * @busy: Set to true when transfer is ongoing on this channel.
  384. * @phy_chan: Pointer to physical channel which this instance runs on. If this
  385. * point is NULL, then the channel is not allocated.
  386. * @chan: DMA engine handle.
  387. * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
  388. * transfer and call client callback.
  389. * @client: Cliented owned descriptor list.
  390. * @pending_queue: Submitted jobs, to be issued by issue_pending()
  391. * @active: Active descriptor.
  392. * @done: Completed jobs
  393. * @queue: Queued jobs.
  394. * @prepare_queue: Prepared jobs.
  395. * @dma_cfg: The client configuration of this dma channel.
  396. * @configured: whether the dma_cfg configuration is valid
  397. * @base: Pointer to the device instance struct.
  398. * @src_def_cfg: Default cfg register setting for src.
  399. * @dst_def_cfg: Default cfg register setting for dst.
  400. * @log_def: Default logical channel settings.
  401. * @lcpa: Pointer to dst and src lcpa settings.
  402. * @runtime_addr: runtime configured address.
  403. * @runtime_direction: runtime configured direction.
  404. *
  405. * This struct can either "be" a logical or a physical channel.
  406. */
  407. struct d40_chan {
  408. spinlock_t lock;
  409. int log_num;
  410. int pending_tx;
  411. bool busy;
  412. struct d40_phy_res *phy_chan;
  413. struct dma_chan chan;
  414. struct tasklet_struct tasklet;
  415. struct list_head client;
  416. struct list_head pending_queue;
  417. struct list_head active;
  418. struct list_head done;
  419. struct list_head queue;
  420. struct list_head prepare_queue;
  421. struct stedma40_chan_cfg dma_cfg;
  422. bool configured;
  423. struct d40_base *base;
  424. /* Default register configurations */
  425. u32 src_def_cfg;
  426. u32 dst_def_cfg;
  427. struct d40_def_lcsp log_def;
  428. struct d40_log_lli_full *lcpa;
  429. /* Runtime reconfiguration */
  430. dma_addr_t runtime_addr;
  431. enum dma_transfer_direction runtime_direction;
  432. };
  433. /**
  434. * struct d40_gen_dmac - generic values to represent u8500/u8540 DMA
  435. * controller
  436. *
  437. * @backup: the pointer to the registers address array for backup
  438. * @backup_size: the size of the registers address array for backup
  439. * @realtime_en: the realtime enable register
  440. * @realtime_clear: the realtime clear register
  441. * @high_prio_en: the high priority enable register
  442. * @high_prio_clear: the high priority clear register
  443. * @interrupt_en: the interrupt enable register
  444. * @interrupt_clear: the interrupt clear register
  445. * @il: the pointer to struct d40_interrupt_lookup
  446. * @il_size: the size of d40_interrupt_lookup array
  447. * @init_reg: the pointer to the struct d40_reg_val
  448. * @init_reg_size: the size of d40_reg_val array
  449. */
  450. struct d40_gen_dmac {
  451. u32 *backup;
  452. u32 backup_size;
  453. u32 realtime_en;
  454. u32 realtime_clear;
  455. u32 high_prio_en;
  456. u32 high_prio_clear;
  457. u32 interrupt_en;
  458. u32 interrupt_clear;
  459. struct d40_interrupt_lookup *il;
  460. u32 il_size;
  461. struct d40_reg_val *init_reg;
  462. u32 init_reg_size;
  463. };
  464. /**
  465. * struct d40_base - The big global struct, one for each probe'd instance.
  466. *
  467. * @interrupt_lock: Lock used to make sure one interrupt is handle a time.
  468. * @execmd_lock: Lock for execute command usage since several channels share
  469. * the same physical register.
  470. * @dev: The device structure.
  471. * @virtbase: The virtual base address of the DMA's register.
  472. * @rev: silicon revision detected.
  473. * @clk: Pointer to the DMA clock structure.
  474. * @phy_start: Physical memory start of the DMA registers.
  475. * @phy_size: Size of the DMA register map.
  476. * @irq: The IRQ number.
  477. * @num_memcpy_chans: The number of channels used for memcpy (mem-to-mem
  478. * transfers).
  479. * @num_phy_chans: The number of physical channels. Read from HW. This
  480. * is the number of available channels for this driver, not counting "Secure
  481. * mode" allocated physical channels.
  482. * @num_log_chans: The number of logical channels. Calculated from
  483. * num_phy_chans.
  484. * @dma_both: dma_device channels that can do both memcpy and slave transfers.
  485. * @dma_slave: dma_device channels that can do only do slave transfers.
  486. * @dma_memcpy: dma_device channels that can do only do memcpy transfers.
  487. * @phy_chans: Room for all possible physical channels in system.
  488. * @log_chans: Room for all possible logical channels in system.
  489. * @lookup_log_chans: Used to map interrupt number to logical channel. Points
  490. * to log_chans entries.
  491. * @lookup_phy_chans: Used to map interrupt number to physical channel. Points
  492. * to phy_chans entries.
  493. * @plat_data: Pointer to provided platform_data which is the driver
  494. * configuration.
  495. * @lcpa_regulator: Pointer to hold the regulator for the esram bank for lcla.
  496. * @phy_res: Vector containing all physical channels.
  497. * @lcla_pool: lcla pool settings and data.
  498. * @lcpa_base: The virtual mapped address of LCPA.
  499. * @phy_lcpa: The physical address of the LCPA.
  500. * @lcpa_size: The size of the LCPA area.
  501. * @desc_slab: cache for descriptors.
  502. * @reg_val_backup: Here the values of some hardware registers are stored
  503. * before the DMA is powered off. They are restored when the power is back on.
  504. * @reg_val_backup_v4: Backup of registers that only exits on dma40 v3 and
  505. * later
  506. * @reg_val_backup_chan: Backup data for standard channel parameter registers.
  507. * @gcc_pwr_off_mask: Mask to maintain the channels that can be turned off.
  508. * @gen_dmac: the struct for generic registers values to represent u8500/8540
  509. * DMA controller
  510. */
  511. struct d40_base {
  512. spinlock_t interrupt_lock;
  513. spinlock_t execmd_lock;
  514. struct device *dev;
  515. void __iomem *virtbase;
  516. u8 rev:4;
  517. struct clk *clk;
  518. phys_addr_t phy_start;
  519. resource_size_t phy_size;
  520. int irq;
  521. int num_memcpy_chans;
  522. int num_phy_chans;
  523. int num_log_chans;
  524. struct device_dma_parameters dma_parms;
  525. struct dma_device dma_both;
  526. struct dma_device dma_slave;
  527. struct dma_device dma_memcpy;
  528. struct d40_chan *phy_chans;
  529. struct d40_chan *log_chans;
  530. struct d40_chan **lookup_log_chans;
  531. struct d40_chan **lookup_phy_chans;
  532. struct stedma40_platform_data *plat_data;
  533. struct regulator *lcpa_regulator;
  534. /* Physical half channels */
  535. struct d40_phy_res *phy_res;
  536. struct d40_lcla_pool lcla_pool;
  537. void *lcpa_base;
  538. dma_addr_t phy_lcpa;
  539. resource_size_t lcpa_size;
  540. struct kmem_cache *desc_slab;
  541. u32 reg_val_backup[BACKUP_REGS_SZ];
  542. u32 reg_val_backup_v4[BACKUP_REGS_SZ_MAX];
  543. u32 *reg_val_backup_chan;
  544. u16 gcc_pwr_off_mask;
  545. struct d40_gen_dmac gen_dmac;
  546. };
  547. static struct device *chan2dev(struct d40_chan *d40c)
  548. {
  549. return &d40c->chan.dev->device;
  550. }
  551. static bool chan_is_physical(struct d40_chan *chan)
  552. {
  553. return chan->log_num == D40_PHY_CHAN;
  554. }
  555. static bool chan_is_logical(struct d40_chan *chan)
  556. {
  557. return !chan_is_physical(chan);
  558. }
  559. static void __iomem *chan_base(struct d40_chan *chan)
  560. {
  561. return chan->base->virtbase + D40_DREG_PCBASE +
  562. chan->phy_chan->num * D40_DREG_PCDELTA;
  563. }
  564. #define d40_err(dev, format, arg...) \
  565. dev_err(dev, "[%s] " format, __func__, ## arg)
  566. #define chan_err(d40c, format, arg...) \
  567. d40_err(chan2dev(d40c), format, ## arg)
  568. static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
  569. int lli_len)
  570. {
  571. bool is_log = chan_is_logical(d40c);
  572. u32 align;
  573. void *base;
  574. if (is_log)
  575. align = sizeof(struct d40_log_lli);
  576. else
  577. align = sizeof(struct d40_phy_lli);
  578. if (lli_len == 1) {
  579. base = d40d->lli_pool.pre_alloc_lli;
  580. d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
  581. d40d->lli_pool.base = NULL;
  582. } else {
  583. d40d->lli_pool.size = lli_len * 2 * align;
  584. base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
  585. d40d->lli_pool.base = base;
  586. if (d40d->lli_pool.base == NULL)
  587. return -ENOMEM;
  588. }
  589. if (is_log) {
  590. d40d->lli_log.src = PTR_ALIGN(base, align);
  591. d40d->lli_log.dst = d40d->lli_log.src + lli_len;
  592. d40d->lli_pool.dma_addr = 0;
  593. } else {
  594. d40d->lli_phy.src = PTR_ALIGN(base, align);
  595. d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
  596. d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
  597. d40d->lli_phy.src,
  598. d40d->lli_pool.size,
  599. DMA_TO_DEVICE);
  600. if (dma_mapping_error(d40c->base->dev,
  601. d40d->lli_pool.dma_addr)) {
  602. kfree(d40d->lli_pool.base);
  603. d40d->lli_pool.base = NULL;
  604. d40d->lli_pool.dma_addr = 0;
  605. return -ENOMEM;
  606. }
  607. }
  608. return 0;
  609. }
  610. static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
  611. {
  612. if (d40d->lli_pool.dma_addr)
  613. dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
  614. d40d->lli_pool.size, DMA_TO_DEVICE);
  615. kfree(d40d->lli_pool.base);
  616. d40d->lli_pool.base = NULL;
  617. d40d->lli_pool.size = 0;
  618. d40d->lli_log.src = NULL;
  619. d40d->lli_log.dst = NULL;
  620. d40d->lli_phy.src = NULL;
  621. d40d->lli_phy.dst = NULL;
  622. }
  623. static int d40_lcla_alloc_one(struct d40_chan *d40c,
  624. struct d40_desc *d40d)
  625. {
  626. unsigned long flags;
  627. int i;
  628. int ret = -EINVAL;
  629. spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
  630. /*
  631. * Allocate both src and dst at the same time, therefore the half
  632. * start on 1 since 0 can't be used since zero is used as end marker.
  633. */
  634. for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
  635. int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
  636. if (!d40c->base->lcla_pool.alloc_map[idx]) {
  637. d40c->base->lcla_pool.alloc_map[idx] = d40d;
  638. d40d->lcla_alloc++;
  639. ret = i;
  640. break;
  641. }
  642. }
  643. spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
  644. return ret;
  645. }
  646. static int d40_lcla_free_all(struct d40_chan *d40c,
  647. struct d40_desc *d40d)
  648. {
  649. unsigned long flags;
  650. int i;
  651. int ret = -EINVAL;
  652. if (chan_is_physical(d40c))
  653. return 0;
  654. spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
  655. for (i = 1 ; i < D40_LCLA_LINK_PER_EVENT_GRP / 2; i++) {
  656. int idx = d40c->phy_chan->num * D40_LCLA_LINK_PER_EVENT_GRP + i;
  657. if (d40c->base->lcla_pool.alloc_map[idx] == d40d) {
  658. d40c->base->lcla_pool.alloc_map[idx] = NULL;
  659. d40d->lcla_alloc--;
  660. if (d40d->lcla_alloc == 0) {
  661. ret = 0;
  662. break;
  663. }
  664. }
  665. }
  666. spin_unlock_irqrestore(&d40c->base->lcla_pool.lock, flags);
  667. return ret;
  668. }
  669. static void d40_desc_remove(struct d40_desc *d40d)
  670. {
  671. list_del(&d40d->node);
  672. }
  673. static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
  674. {
  675. struct d40_desc *desc = NULL;
  676. if (!list_empty(&d40c->client)) {
  677. struct d40_desc *d;
  678. struct d40_desc *_d;
  679. list_for_each_entry_safe(d, _d, &d40c->client, node) {
  680. if (async_tx_test_ack(&d->txd)) {
  681. d40_desc_remove(d);
  682. desc = d;
  683. memset(desc, 0, sizeof(*desc));
  684. break;
  685. }
  686. }
  687. }
  688. if (!desc)
  689. desc = kmem_cache_zalloc(d40c->base->desc_slab, GFP_NOWAIT);
  690. if (desc)
  691. INIT_LIST_HEAD(&desc->node);
  692. return desc;
  693. }
  694. static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
  695. {
  696. d40_pool_lli_free(d40c, d40d);
  697. d40_lcla_free_all(d40c, d40d);
  698. kmem_cache_free(d40c->base->desc_slab, d40d);
  699. }
  700. static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
  701. {
  702. list_add_tail(&desc->node, &d40c->active);
  703. }
  704. static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
  705. {
  706. struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
  707. struct d40_phy_lli *lli_src = desc->lli_phy.src;
  708. void __iomem *base = chan_base(chan);
  709. writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
  710. writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
  711. writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
  712. writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
  713. writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
  714. writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
  715. writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
  716. writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
  717. }
  718. static void d40_desc_done(struct d40_chan *d40c, struct d40_desc *desc)
  719. {
  720. list_add_tail(&desc->node, &d40c->done);
  721. }
  722. static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
  723. {
  724. struct d40_lcla_pool *pool = &chan->base->lcla_pool;
  725. struct d40_log_lli_bidir *lli = &desc->lli_log;
  726. int lli_current = desc->lli_current;
  727. int lli_len = desc->lli_len;
  728. bool cyclic = desc->cyclic;
  729. int curr_lcla = -EINVAL;
  730. int first_lcla = 0;
  731. bool use_esram_lcla = chan->base->plat_data->use_esram_lcla;
  732. bool linkback;
  733. /*
  734. * We may have partially running cyclic transfers, in case we did't get
  735. * enough LCLA entries.
  736. */
  737. linkback = cyclic && lli_current == 0;
  738. /*
  739. * For linkback, we need one LCLA even with only one link, because we
  740. * can't link back to the one in LCPA space
  741. */
  742. if (linkback || (lli_len - lli_current > 1)) {
  743. /*
  744. * If the channel is expected to use only soft_lli don't
  745. * allocate a lcla. This is to avoid a HW issue that exists
  746. * in some controller during a peripheral to memory transfer
  747. * that uses linked lists.
  748. */
  749. if (!(chan->phy_chan->use_soft_lli &&
  750. chan->dma_cfg.dir == DMA_DEV_TO_MEM))
  751. curr_lcla = d40_lcla_alloc_one(chan, desc);
  752. first_lcla = curr_lcla;
  753. }
  754. /*
  755. * For linkback, we normally load the LCPA in the loop since we need to
  756. * link it to the second LCLA and not the first. However, if we
  757. * couldn't even get a first LCLA, then we have to run in LCPA and
  758. * reload manually.
  759. */
  760. if (!linkback || curr_lcla == -EINVAL) {
  761. unsigned int flags = 0;
  762. if (curr_lcla == -EINVAL)
  763. flags |= LLI_TERM_INT;
  764. d40_log_lli_lcpa_write(chan->lcpa,
  765. &lli->dst[lli_current],
  766. &lli->src[lli_current],
  767. curr_lcla,
  768. flags);
  769. lli_current++;
  770. }
  771. if (curr_lcla < 0)
  772. goto out;
  773. for (; lli_current < lli_len; lli_current++) {
  774. unsigned int lcla_offset = chan->phy_chan->num * 1024 +
  775. 8 * curr_lcla * 2;
  776. struct d40_log_lli *lcla = pool->base + lcla_offset;
  777. unsigned int flags = 0;
  778. int next_lcla;
  779. if (lli_current + 1 < lli_len)
  780. next_lcla = d40_lcla_alloc_one(chan, desc);
  781. else
  782. next_lcla = linkback ? first_lcla : -EINVAL;
  783. if (cyclic || next_lcla == -EINVAL)
  784. flags |= LLI_TERM_INT;
  785. if (linkback && curr_lcla == first_lcla) {
  786. /* First link goes in both LCPA and LCLA */
  787. d40_log_lli_lcpa_write(chan->lcpa,
  788. &lli->dst[lli_current],
  789. &lli->src[lli_current],
  790. next_lcla, flags);
  791. }
  792. /*
  793. * One unused LCLA in the cyclic case if the very first
  794. * next_lcla fails...
  795. */
  796. d40_log_lli_lcla_write(lcla,
  797. &lli->dst[lli_current],
  798. &lli->src[lli_current],
  799. next_lcla, flags);
  800. /*
  801. * Cache maintenance is not needed if lcla is
  802. * mapped in esram
  803. */
  804. if (!use_esram_lcla) {
  805. dma_sync_single_range_for_device(chan->base->dev,
  806. pool->dma_addr, lcla_offset,
  807. 2 * sizeof(struct d40_log_lli),
  808. DMA_TO_DEVICE);
  809. }
  810. curr_lcla = next_lcla;
  811. if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
  812. lli_current++;
  813. break;
  814. }
  815. }
  816. out:
  817. desc->lli_current = lli_current;
  818. }
  819. static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
  820. {
  821. if (chan_is_physical(d40c)) {
  822. d40_phy_lli_load(d40c, d40d);
  823. d40d->lli_current = d40d->lli_len;
  824. } else
  825. d40_log_lli_to_lcxa(d40c, d40d);
  826. }
  827. static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
  828. {
  829. struct d40_desc *d;
  830. if (list_empty(&d40c->active))
  831. return NULL;
  832. d = list_first_entry(&d40c->active,
  833. struct d40_desc,
  834. node);
  835. return d;
  836. }
  837. /* remove desc from current queue and add it to the pending_queue */
  838. static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
  839. {
  840. d40_desc_remove(desc);
  841. desc->is_in_client_list = false;
  842. list_add_tail(&desc->node, &d40c->pending_queue);
  843. }
  844. static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
  845. {
  846. struct d40_desc *d;
  847. if (list_empty(&d40c->pending_queue))
  848. return NULL;
  849. d = list_first_entry(&d40c->pending_queue,
  850. struct d40_desc,
  851. node);
  852. return d;
  853. }
  854. static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
  855. {
  856. struct d40_desc *d;
  857. if (list_empty(&d40c->queue))
  858. return NULL;
  859. d = list_first_entry(&d40c->queue,
  860. struct d40_desc,
  861. node);
  862. return d;
  863. }
  864. static struct d40_desc *d40_first_done(struct d40_chan *d40c)
  865. {
  866. if (list_empty(&d40c->done))
  867. return NULL;
  868. return list_first_entry(&d40c->done, struct d40_desc, node);
  869. }
  870. static int d40_psize_2_burst_size(bool is_log, int psize)
  871. {
  872. if (is_log) {
  873. if (psize == STEDMA40_PSIZE_LOG_1)
  874. return 1;
  875. } else {
  876. if (psize == STEDMA40_PSIZE_PHY_1)
  877. return 1;
  878. }
  879. return 2 << psize;
  880. }
  881. /*
  882. * The dma only supports transmitting packages up to
  883. * STEDMA40_MAX_SEG_SIZE * data_width, where data_width is stored in Bytes.
  884. *
  885. * Calculate the total number of dma elements required to send the entire sg list.
  886. */
  887. static int d40_size_2_dmalen(int size, u32 data_width1, u32 data_width2)
  888. {
  889. int dmalen;
  890. u32 max_w = max(data_width1, data_width2);
  891. u32 min_w = min(data_width1, data_width2);
  892. u32 seg_max = ALIGN(STEDMA40_MAX_SEG_SIZE * min_w, max_w);
  893. if (seg_max > STEDMA40_MAX_SEG_SIZE)
  894. seg_max -= max_w;
  895. if (!IS_ALIGNED(size, max_w))
  896. return -EINVAL;
  897. if (size <= seg_max)
  898. dmalen = 1;
  899. else {
  900. dmalen = size / seg_max;
  901. if (dmalen * seg_max < size)
  902. dmalen++;
  903. }
  904. return dmalen;
  905. }
  906. static int d40_sg_2_dmalen(struct scatterlist *sgl, int sg_len,
  907. u32 data_width1, u32 data_width2)
  908. {
  909. struct scatterlist *sg;
  910. int i;
  911. int len = 0;
  912. int ret;
  913. for_each_sg(sgl, sg, sg_len, i) {
  914. ret = d40_size_2_dmalen(sg_dma_len(sg),
  915. data_width1, data_width2);
  916. if (ret < 0)
  917. return ret;
  918. len += ret;
  919. }
  920. return len;
  921. }
  922. static int __d40_execute_command_phy(struct d40_chan *d40c,
  923. enum d40_command command)
  924. {
  925. u32 status;
  926. int i;
  927. void __iomem *active_reg;
  928. int ret = 0;
  929. unsigned long flags;
  930. u32 wmask;
  931. if (command == D40_DMA_STOP) {
  932. ret = __d40_execute_command_phy(d40c, D40_DMA_SUSPEND_REQ);
  933. if (ret)
  934. return ret;
  935. }
  936. spin_lock_irqsave(&d40c->base->execmd_lock, flags);
  937. if (d40c->phy_chan->num % 2 == 0)
  938. active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
  939. else
  940. active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
  941. if (command == D40_DMA_SUSPEND_REQ) {
  942. status = (readl(active_reg) &
  943. D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
  944. D40_CHAN_POS(d40c->phy_chan->num);
  945. if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
  946. goto done;
  947. }
  948. wmask = 0xffffffff & ~(D40_CHAN_POS_MASK(d40c->phy_chan->num));
  949. writel(wmask | (command << D40_CHAN_POS(d40c->phy_chan->num)),
  950. active_reg);
  951. if (command == D40_DMA_SUSPEND_REQ) {
  952. for (i = 0 ; i < D40_SUSPEND_MAX_IT; i++) {
  953. status = (readl(active_reg) &
  954. D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
  955. D40_CHAN_POS(d40c->phy_chan->num);
  956. cpu_relax();
  957. /*
  958. * Reduce the number of bus accesses while
  959. * waiting for the DMA to suspend.
  960. */
  961. udelay(3);
  962. if (status == D40_DMA_STOP ||
  963. status == D40_DMA_SUSPENDED)
  964. break;
  965. }
  966. if (i == D40_SUSPEND_MAX_IT) {
  967. chan_err(d40c,
  968. "unable to suspend the chl %d (log: %d) status %x\n",
  969. d40c->phy_chan->num, d40c->log_num,
  970. status);
  971. dump_stack();
  972. ret = -EBUSY;
  973. }
  974. }
  975. done:
  976. spin_unlock_irqrestore(&d40c->base->execmd_lock, flags);
  977. return ret;
  978. }
  979. static void d40_term_all(struct d40_chan *d40c)
  980. {
  981. struct d40_desc *d40d;
  982. struct d40_desc *_d;
  983. /* Release completed descriptors */
  984. while ((d40d = d40_first_done(d40c))) {
  985. d40_desc_remove(d40d);
  986. d40_desc_free(d40c, d40d);
  987. }
  988. /* Release active descriptors */
  989. while ((d40d = d40_first_active_get(d40c))) {
  990. d40_desc_remove(d40d);
  991. d40_desc_free(d40c, d40d);
  992. }
  993. /* Release queued descriptors waiting for transfer */
  994. while ((d40d = d40_first_queued(d40c))) {
  995. d40_desc_remove(d40d);
  996. d40_desc_free(d40c, d40d);
  997. }
  998. /* Release pending descriptors */
  999. while ((d40d = d40_first_pending(d40c))) {
  1000. d40_desc_remove(d40d);
  1001. d40_desc_free(d40c, d40d);
  1002. }
  1003. /* Release client owned descriptors */
  1004. if (!list_empty(&d40c->client))
  1005. list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
  1006. d40_desc_remove(d40d);
  1007. d40_desc_free(d40c, d40d);
  1008. }
  1009. /* Release descriptors in prepare queue */
  1010. if (!list_empty(&d40c->prepare_queue))
  1011. list_for_each_entry_safe(d40d, _d,
  1012. &d40c->prepare_queue, node) {
  1013. d40_desc_remove(d40d);
  1014. d40_desc_free(d40c, d40d);
  1015. }
  1016. d40c->pending_tx = 0;
  1017. }
  1018. static void __d40_config_set_event(struct d40_chan *d40c,
  1019. enum d40_events event_type, u32 event,
  1020. int reg)
  1021. {
  1022. void __iomem *addr = chan_base(d40c) + reg;
  1023. int tries;
  1024. u32 status;
  1025. switch (event_type) {
  1026. case D40_DEACTIVATE_EVENTLINE:
  1027. writel((D40_DEACTIVATE_EVENTLINE << D40_EVENTLINE_POS(event))
  1028. | ~D40_EVENTLINE_MASK(event), addr);
  1029. break;
  1030. case D40_SUSPEND_REQ_EVENTLINE:
  1031. status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
  1032. D40_EVENTLINE_POS(event);
  1033. if (status == D40_DEACTIVATE_EVENTLINE ||
  1034. status == D40_SUSPEND_REQ_EVENTLINE)
  1035. break;
  1036. writel((D40_SUSPEND_REQ_EVENTLINE << D40_EVENTLINE_POS(event))
  1037. | ~D40_EVENTLINE_MASK(event), addr);
  1038. for (tries = 0 ; tries < D40_SUSPEND_MAX_IT; tries++) {
  1039. status = (readl(addr) & D40_EVENTLINE_MASK(event)) >>
  1040. D40_EVENTLINE_POS(event);
  1041. cpu_relax();
  1042. /*
  1043. * Reduce the number of bus accesses while
  1044. * waiting for the DMA to suspend.
  1045. */
  1046. udelay(3);
  1047. if (status == D40_DEACTIVATE_EVENTLINE)
  1048. break;
  1049. }
  1050. if (tries == D40_SUSPEND_MAX_IT) {
  1051. chan_err(d40c,
  1052. "unable to stop the event_line chl %d (log: %d)"
  1053. "status %x\n", d40c->phy_chan->num,
  1054. d40c->log_num, status);
  1055. }
  1056. break;
  1057. case D40_ACTIVATE_EVENTLINE:
  1058. /*
  1059. * The hardware sometimes doesn't register the enable when src and dst
  1060. * event lines are active on the same logical channel. Retry to ensure
  1061. * it does. Usually only one retry is sufficient.
  1062. */
  1063. tries = 100;
  1064. while (--tries) {
  1065. writel((D40_ACTIVATE_EVENTLINE <<
  1066. D40_EVENTLINE_POS(event)) |
  1067. ~D40_EVENTLINE_MASK(event), addr);
  1068. if (readl(addr) & D40_EVENTLINE_MASK(event))
  1069. break;
  1070. }
  1071. if (tries != 99)
  1072. dev_dbg(chan2dev(d40c),
  1073. "[%s] workaround enable S%cLNK (%d tries)\n",
  1074. __func__, reg == D40_CHAN_REG_SSLNK ? 'S' : 'D',
  1075. 100 - tries);
  1076. WARN_ON(!tries);
  1077. break;
  1078. case D40_ROUND_EVENTLINE:
  1079. BUG();
  1080. break;
  1081. }
  1082. }
  1083. static void d40_config_set_event(struct d40_chan *d40c,
  1084. enum d40_events event_type)
  1085. {
  1086. u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
  1087. /* Enable event line connected to device (or memcpy) */
  1088. if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
  1089. (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
  1090. __d40_config_set_event(d40c, event_type, event,
  1091. D40_CHAN_REG_SSLNK);
  1092. if (d40c->dma_cfg.dir != DMA_DEV_TO_MEM)
  1093. __d40_config_set_event(d40c, event_type, event,
  1094. D40_CHAN_REG_SDLNK);
  1095. }
  1096. static u32 d40_chan_has_events(struct d40_chan *d40c)
  1097. {
  1098. void __iomem *chanbase = chan_base(d40c);
  1099. u32 val;
  1100. val = readl(chanbase + D40_CHAN_REG_SSLNK);
  1101. val |= readl(chanbase + D40_CHAN_REG_SDLNK);
  1102. return val;
  1103. }
  1104. static int
  1105. __d40_execute_command_log(struct d40_chan *d40c, enum d40_command command)
  1106. {
  1107. unsigned long flags;
  1108. int ret = 0;
  1109. u32 active_status;
  1110. void __iomem *active_reg;
  1111. if (d40c->phy_chan->num % 2 == 0)
  1112. active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
  1113. else
  1114. active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
  1115. spin_lock_irqsave(&d40c->phy_chan->lock, flags);
  1116. switch (command) {
  1117. case D40_DMA_STOP:
  1118. case D40_DMA_SUSPEND_REQ:
  1119. active_status = (readl(active_reg) &
  1120. D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
  1121. D40_CHAN_POS(d40c->phy_chan->num);
  1122. if (active_status == D40_DMA_RUN)
  1123. d40_config_set_event(d40c, D40_SUSPEND_REQ_EVENTLINE);
  1124. else
  1125. d40_config_set_event(d40c, D40_DEACTIVATE_EVENTLINE);
  1126. if (!d40_chan_has_events(d40c) && (command == D40_DMA_STOP))
  1127. ret = __d40_execute_command_phy(d40c, command);
  1128. break;
  1129. case D40_DMA_RUN:
  1130. d40_config_set_event(d40c, D40_ACTIVATE_EVENTLINE);
  1131. ret = __d40_execute_command_phy(d40c, command);
  1132. break;
  1133. case D40_DMA_SUSPENDED:
  1134. BUG();
  1135. break;
  1136. }
  1137. spin_unlock_irqrestore(&d40c->phy_chan->lock, flags);
  1138. return ret;
  1139. }
  1140. static int d40_channel_execute_command(struct d40_chan *d40c,
  1141. enum d40_command command)
  1142. {
  1143. if (chan_is_logical(d40c))
  1144. return __d40_execute_command_log(d40c, command);
  1145. else
  1146. return __d40_execute_command_phy(d40c, command);
  1147. }
  1148. static u32 d40_get_prmo(struct d40_chan *d40c)
  1149. {
  1150. static const unsigned int phy_map[] = {
  1151. [STEDMA40_PCHAN_BASIC_MODE]
  1152. = D40_DREG_PRMO_PCHAN_BASIC,
  1153. [STEDMA40_PCHAN_MODULO_MODE]
  1154. = D40_DREG_PRMO_PCHAN_MODULO,
  1155. [STEDMA40_PCHAN_DOUBLE_DST_MODE]
  1156. = D40_DREG_PRMO_PCHAN_DOUBLE_DST,
  1157. };
  1158. static const unsigned int log_map[] = {
  1159. [STEDMA40_LCHAN_SRC_PHY_DST_LOG]
  1160. = D40_DREG_PRMO_LCHAN_SRC_PHY_DST_LOG,
  1161. [STEDMA40_LCHAN_SRC_LOG_DST_PHY]
  1162. = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_PHY,
  1163. [STEDMA40_LCHAN_SRC_LOG_DST_LOG]
  1164. = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
  1165. };
  1166. if (chan_is_physical(d40c))
  1167. return phy_map[d40c->dma_cfg.mode_opt];
  1168. else
  1169. return log_map[d40c->dma_cfg.mode_opt];
  1170. }
  1171. static void d40_config_write(struct d40_chan *d40c)
  1172. {
  1173. u32 addr_base;
  1174. u32 var;
  1175. /* Odd addresses are even addresses + 4 */
  1176. addr_base = (d40c->phy_chan->num % 2) * 4;
  1177. /* Setup channel mode to logical or physical */
  1178. var = ((u32)(chan_is_logical(d40c)) + 1) <<
  1179. D40_CHAN_POS(d40c->phy_chan->num);
  1180. writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
  1181. /* Setup operational mode option register */
  1182. var = d40_get_prmo(d40c) << D40_CHAN_POS(d40c->phy_chan->num);
  1183. writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
  1184. if (chan_is_logical(d40c)) {
  1185. int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
  1186. & D40_SREG_ELEM_LOG_LIDX_MASK;
  1187. void __iomem *chanbase = chan_base(d40c);
  1188. /* Set default config for CFG reg */
  1189. writel(d40c->src_def_cfg, chanbase + D40_CHAN_REG_SSCFG);
  1190. writel(d40c->dst_def_cfg, chanbase + D40_CHAN_REG_SDCFG);
  1191. /* Set LIDX for lcla */
  1192. writel(lidx, chanbase + D40_CHAN_REG_SSELT);
  1193. writel(lidx, chanbase + D40_CHAN_REG_SDELT);
  1194. /* Clear LNK which will be used by d40_chan_has_events() */
  1195. writel(0, chanbase + D40_CHAN_REG_SSLNK);
  1196. writel(0, chanbase + D40_CHAN_REG_SDLNK);
  1197. }
  1198. }
  1199. static u32 d40_residue(struct d40_chan *d40c)
  1200. {
  1201. u32 num_elt;
  1202. if (chan_is_logical(d40c))
  1203. num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
  1204. >> D40_MEM_LCSP2_ECNT_POS;
  1205. else {
  1206. u32 val = readl(chan_base(d40c) + D40_CHAN_REG_SDELT);
  1207. num_elt = (val & D40_SREG_ELEM_PHY_ECNT_MASK)
  1208. >> D40_SREG_ELEM_PHY_ECNT_POS;
  1209. }
  1210. return num_elt * d40c->dma_cfg.dst_info.data_width;
  1211. }
  1212. static bool d40_tx_is_linked(struct d40_chan *d40c)
  1213. {
  1214. bool is_link;
  1215. if (chan_is_logical(d40c))
  1216. is_link = readl(&d40c->lcpa->lcsp3) & D40_MEM_LCSP3_DLOS_MASK;
  1217. else
  1218. is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
  1219. & D40_SREG_LNK_PHYS_LNK_MASK;
  1220. return is_link;
  1221. }
  1222. static int d40_pause(struct dma_chan *chan)
  1223. {
  1224. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  1225. int res = 0;
  1226. unsigned long flags;
  1227. if (d40c->phy_chan == NULL) {
  1228. chan_err(d40c, "Channel is not allocated!\n");
  1229. return -EINVAL;
  1230. }
  1231. if (!d40c->busy)
  1232. return 0;
  1233. spin_lock_irqsave(&d40c->lock, flags);
  1234. pm_runtime_get_sync(d40c->base->dev);
  1235. res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
  1236. pm_runtime_mark_last_busy(d40c->base->dev);
  1237. pm_runtime_put_autosuspend(d40c->base->dev);
  1238. spin_unlock_irqrestore(&d40c->lock, flags);
  1239. return res;
  1240. }
  1241. static int d40_resume(struct dma_chan *chan)
  1242. {
  1243. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  1244. int res = 0;
  1245. unsigned long flags;
  1246. if (d40c->phy_chan == NULL) {
  1247. chan_err(d40c, "Channel is not allocated!\n");
  1248. return -EINVAL;
  1249. }
  1250. if (!d40c->busy)
  1251. return 0;
  1252. spin_lock_irqsave(&d40c->lock, flags);
  1253. pm_runtime_get_sync(d40c->base->dev);
  1254. /* If bytes left to transfer or linked tx resume job */
  1255. if (d40_residue(d40c) || d40_tx_is_linked(d40c))
  1256. res = d40_channel_execute_command(d40c, D40_DMA_RUN);
  1257. pm_runtime_mark_last_busy(d40c->base->dev);
  1258. pm_runtime_put_autosuspend(d40c->base->dev);
  1259. spin_unlock_irqrestore(&d40c->lock, flags);
  1260. return res;
  1261. }
  1262. static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
  1263. {
  1264. struct d40_chan *d40c = container_of(tx->chan,
  1265. struct d40_chan,
  1266. chan);
  1267. struct d40_desc *d40d = container_of(tx, struct d40_desc, txd);
  1268. unsigned long flags;
  1269. dma_cookie_t cookie;
  1270. spin_lock_irqsave(&d40c->lock, flags);
  1271. cookie = dma_cookie_assign(tx);
  1272. d40_desc_queue(d40c, d40d);
  1273. spin_unlock_irqrestore(&d40c->lock, flags);
  1274. return cookie;
  1275. }
  1276. static int d40_start(struct d40_chan *d40c)
  1277. {
  1278. return d40_channel_execute_command(d40c, D40_DMA_RUN);
  1279. }
  1280. static struct d40_desc *d40_queue_start(struct d40_chan *d40c)
  1281. {
  1282. struct d40_desc *d40d;
  1283. int err;
  1284. /* Start queued jobs, if any */
  1285. d40d = d40_first_queued(d40c);
  1286. if (d40d != NULL) {
  1287. if (!d40c->busy) {
  1288. d40c->busy = true;
  1289. pm_runtime_get_sync(d40c->base->dev);
  1290. }
  1291. /* Remove from queue */
  1292. d40_desc_remove(d40d);
  1293. /* Add to active queue */
  1294. d40_desc_submit(d40c, d40d);
  1295. /* Initiate DMA job */
  1296. d40_desc_load(d40c, d40d);
  1297. /* Start dma job */
  1298. err = d40_start(d40c);
  1299. if (err)
  1300. return NULL;
  1301. }
  1302. return d40d;
  1303. }
  1304. /* called from interrupt context */
  1305. static void dma_tc_handle(struct d40_chan *d40c)
  1306. {
  1307. struct d40_desc *d40d;
  1308. /* Get first active entry from list */
  1309. d40d = d40_first_active_get(d40c);
  1310. if (d40d == NULL)
  1311. return;
  1312. if (d40d->cyclic) {
  1313. /*
  1314. * If this was a paritially loaded list, we need to reloaded
  1315. * it, and only when the list is completed. We need to check
  1316. * for done because the interrupt will hit for every link, and
  1317. * not just the last one.
  1318. */
  1319. if (d40d->lli_current < d40d->lli_len
  1320. && !d40_tx_is_linked(d40c)
  1321. && !d40_residue(d40c)) {
  1322. d40_lcla_free_all(d40c, d40d);
  1323. d40_desc_load(d40c, d40d);
  1324. (void) d40_start(d40c);
  1325. if (d40d->lli_current == d40d->lli_len)
  1326. d40d->lli_current = 0;
  1327. }
  1328. } else {
  1329. d40_lcla_free_all(d40c, d40d);
  1330. if (d40d->lli_current < d40d->lli_len) {
  1331. d40_desc_load(d40c, d40d);
  1332. /* Start dma job */
  1333. (void) d40_start(d40c);
  1334. return;
  1335. }
  1336. if (d40_queue_start(d40c) == NULL) {
  1337. d40c->busy = false;
  1338. pm_runtime_mark_last_busy(d40c->base->dev);
  1339. pm_runtime_put_autosuspend(d40c->base->dev);
  1340. }
  1341. d40_desc_remove(d40d);
  1342. d40_desc_done(d40c, d40d);
  1343. }
  1344. d40c->pending_tx++;
  1345. tasklet_schedule(&d40c->tasklet);
  1346. }
  1347. static void dma_tasklet(unsigned long data)
  1348. {
  1349. struct d40_chan *d40c = (struct d40_chan *) data;
  1350. struct d40_desc *d40d;
  1351. unsigned long flags;
  1352. bool callback_active;
  1353. dma_async_tx_callback callback;
  1354. void *callback_param;
  1355. spin_lock_irqsave(&d40c->lock, flags);
  1356. /* Get first entry from the done list */
  1357. d40d = d40_first_done(d40c);
  1358. if (d40d == NULL) {
  1359. /* Check if we have reached here for cyclic job */
  1360. d40d = d40_first_active_get(d40c);
  1361. if (d40d == NULL || !d40d->cyclic)
  1362. goto err;
  1363. }
  1364. if (!d40d->cyclic)
  1365. dma_cookie_complete(&d40d->txd);
  1366. /*
  1367. * If terminating a channel pending_tx is set to zero.
  1368. * This prevents any finished active jobs to return to the client.
  1369. */
  1370. if (d40c->pending_tx == 0) {
  1371. spin_unlock_irqrestore(&d40c->lock, flags);
  1372. return;
  1373. }
  1374. /* Callback to client */
  1375. callback_active = !!(d40d->txd.flags & DMA_PREP_INTERRUPT);
  1376. callback = d40d->txd.callback;
  1377. callback_param = d40d->txd.callback_param;
  1378. if (!d40d->cyclic) {
  1379. if (async_tx_test_ack(&d40d->txd)) {
  1380. d40_desc_remove(d40d);
  1381. d40_desc_free(d40c, d40d);
  1382. } else if (!d40d->is_in_client_list) {
  1383. d40_desc_remove(d40d);
  1384. d40_lcla_free_all(d40c, d40d);
  1385. list_add_tail(&d40d->node, &d40c->client);
  1386. d40d->is_in_client_list = true;
  1387. }
  1388. }
  1389. d40c->pending_tx--;
  1390. if (d40c->pending_tx)
  1391. tasklet_schedule(&d40c->tasklet);
  1392. spin_unlock_irqrestore(&d40c->lock, flags);
  1393. if (callback_active && callback)
  1394. callback(callback_param);
  1395. return;
  1396. err:
  1397. /* Rescue manouver if receiving double interrupts */
  1398. if (d40c->pending_tx > 0)
  1399. d40c->pending_tx--;
  1400. spin_unlock_irqrestore(&d40c->lock, flags);
  1401. }
  1402. static irqreturn_t d40_handle_interrupt(int irq, void *data)
  1403. {
  1404. int i;
  1405. u32 idx;
  1406. u32 row;
  1407. long chan = -1;
  1408. struct d40_chan *d40c;
  1409. unsigned long flags;
  1410. struct d40_base *base = data;
  1411. u32 regs[base->gen_dmac.il_size];
  1412. struct d40_interrupt_lookup *il = base->gen_dmac.il;
  1413. u32 il_size = base->gen_dmac.il_size;
  1414. spin_lock_irqsave(&base->interrupt_lock, flags);
  1415. /* Read interrupt status of both logical and physical channels */
  1416. for (i = 0; i < il_size; i++)
  1417. regs[i] = readl(base->virtbase + il[i].src);
  1418. for (;;) {
  1419. chan = find_next_bit((unsigned long *)regs,
  1420. BITS_PER_LONG * il_size, chan + 1);
  1421. /* No more set bits found? */
  1422. if (chan == BITS_PER_LONG * il_size)
  1423. break;
  1424. row = chan / BITS_PER_LONG;
  1425. idx = chan & (BITS_PER_LONG - 1);
  1426. if (il[row].offset == D40_PHY_CHAN)
  1427. d40c = base->lookup_phy_chans[idx];
  1428. else
  1429. d40c = base->lookup_log_chans[il[row].offset + idx];
  1430. if (!d40c) {
  1431. /*
  1432. * No error because this can happen if something else
  1433. * in the system is using the channel.
  1434. */
  1435. continue;
  1436. }
  1437. /* ACK interrupt */
  1438. writel(BIT(idx), base->virtbase + il[row].clr);
  1439. spin_lock(&d40c->lock);
  1440. if (!il[row].is_error)
  1441. dma_tc_handle(d40c);
  1442. else
  1443. d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
  1444. chan, il[row].offset, idx);
  1445. spin_unlock(&d40c->lock);
  1446. }
  1447. spin_unlock_irqrestore(&base->interrupt_lock, flags);
  1448. return IRQ_HANDLED;
  1449. }
  1450. static int d40_validate_conf(struct d40_chan *d40c,
  1451. struct stedma40_chan_cfg *conf)
  1452. {
  1453. int res = 0;
  1454. bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
  1455. if (!conf->dir) {
  1456. chan_err(d40c, "Invalid direction.\n");
  1457. res = -EINVAL;
  1458. }
  1459. if ((is_log && conf->dev_type > d40c->base->num_log_chans) ||
  1460. (!is_log && conf->dev_type > d40c->base->num_phy_chans) ||
  1461. (conf->dev_type < 0)) {
  1462. chan_err(d40c, "Invalid device type (%d)\n", conf->dev_type);
  1463. res = -EINVAL;
  1464. }
  1465. if (conf->dir == DMA_DEV_TO_DEV) {
  1466. /*
  1467. * DMAC HW supports it. Will be added to this driver,
  1468. * in case any dma client requires it.
  1469. */
  1470. chan_err(d40c, "periph to periph not supported\n");
  1471. res = -EINVAL;
  1472. }
  1473. if (d40_psize_2_burst_size(is_log, conf->src_info.psize) *
  1474. conf->src_info.data_width !=
  1475. d40_psize_2_burst_size(is_log, conf->dst_info.psize) *
  1476. conf->dst_info.data_width) {
  1477. /*
  1478. * The DMAC hardware only supports
  1479. * src (burst x width) == dst (burst x width)
  1480. */
  1481. chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
  1482. res = -EINVAL;
  1483. }
  1484. return res;
  1485. }
  1486. static bool d40_alloc_mask_set(struct d40_phy_res *phy,
  1487. bool is_src, int log_event_line, bool is_log,
  1488. bool *first_user)
  1489. {
  1490. unsigned long flags;
  1491. spin_lock_irqsave(&phy->lock, flags);
  1492. *first_user = ((phy->allocated_src | phy->allocated_dst)
  1493. == D40_ALLOC_FREE);
  1494. if (!is_log) {
  1495. /* Physical interrupts are masked per physical full channel */
  1496. if (phy->allocated_src == D40_ALLOC_FREE &&
  1497. phy->allocated_dst == D40_ALLOC_FREE) {
  1498. phy->allocated_dst = D40_ALLOC_PHY;
  1499. phy->allocated_src = D40_ALLOC_PHY;
  1500. goto found;
  1501. } else
  1502. goto not_found;
  1503. }
  1504. /* Logical channel */
  1505. if (is_src) {
  1506. if (phy->allocated_src == D40_ALLOC_PHY)
  1507. goto not_found;
  1508. if (phy->allocated_src == D40_ALLOC_FREE)
  1509. phy->allocated_src = D40_ALLOC_LOG_FREE;
  1510. if (!(phy->allocated_src & BIT(log_event_line))) {
  1511. phy->allocated_src |= BIT(log_event_line);
  1512. goto found;
  1513. } else
  1514. goto not_found;
  1515. } else {
  1516. if (phy->allocated_dst == D40_ALLOC_PHY)
  1517. goto not_found;
  1518. if (phy->allocated_dst == D40_ALLOC_FREE)
  1519. phy->allocated_dst = D40_ALLOC_LOG_FREE;
  1520. if (!(phy->allocated_dst & BIT(log_event_line))) {
  1521. phy->allocated_dst |= BIT(log_event_line);
  1522. goto found;
  1523. } else
  1524. goto not_found;
  1525. }
  1526. not_found:
  1527. spin_unlock_irqrestore(&phy->lock, flags);
  1528. return false;
  1529. found:
  1530. spin_unlock_irqrestore(&phy->lock, flags);
  1531. return true;
  1532. }
  1533. static bool d40_alloc_mask_free(struct d40_phy_res *phy, bool is_src,
  1534. int log_event_line)
  1535. {
  1536. unsigned long flags;
  1537. bool is_free = false;
  1538. spin_lock_irqsave(&phy->lock, flags);
  1539. if (!log_event_line) {
  1540. phy->allocated_dst = D40_ALLOC_FREE;
  1541. phy->allocated_src = D40_ALLOC_FREE;
  1542. is_free = true;
  1543. goto out;
  1544. }
  1545. /* Logical channel */
  1546. if (is_src) {
  1547. phy->allocated_src &= ~BIT(log_event_line);
  1548. if (phy->allocated_src == D40_ALLOC_LOG_FREE)
  1549. phy->allocated_src = D40_ALLOC_FREE;
  1550. } else {
  1551. phy->allocated_dst &= ~BIT(log_event_line);
  1552. if (phy->allocated_dst == D40_ALLOC_LOG_FREE)
  1553. phy->allocated_dst = D40_ALLOC_FREE;
  1554. }
  1555. is_free = ((phy->allocated_src | phy->allocated_dst) ==
  1556. D40_ALLOC_FREE);
  1557. out:
  1558. spin_unlock_irqrestore(&phy->lock, flags);
  1559. return is_free;
  1560. }
  1561. static int d40_allocate_channel(struct d40_chan *d40c, bool *first_phy_user)
  1562. {
  1563. int dev_type = d40c->dma_cfg.dev_type;
  1564. int event_group;
  1565. int event_line;
  1566. struct d40_phy_res *phys;
  1567. int i;
  1568. int j;
  1569. int log_num;
  1570. int num_phy_chans;
  1571. bool is_src;
  1572. bool is_log = d40c->dma_cfg.mode == STEDMA40_MODE_LOGICAL;
  1573. phys = d40c->base->phy_res;
  1574. num_phy_chans = d40c->base->num_phy_chans;
  1575. if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
  1576. log_num = 2 * dev_type;
  1577. is_src = true;
  1578. } else if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
  1579. d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
  1580. /* dst event lines are used for logical memcpy */
  1581. log_num = 2 * dev_type + 1;
  1582. is_src = false;
  1583. } else
  1584. return -EINVAL;
  1585. event_group = D40_TYPE_TO_GROUP(dev_type);
  1586. event_line = D40_TYPE_TO_EVENT(dev_type);
  1587. if (!is_log) {
  1588. if (d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
  1589. /* Find physical half channel */
  1590. if (d40c->dma_cfg.use_fixed_channel) {
  1591. i = d40c->dma_cfg.phy_channel;
  1592. if (d40_alloc_mask_set(&phys[i], is_src,
  1593. 0, is_log,
  1594. first_phy_user))
  1595. goto found_phy;
  1596. } else {
  1597. for (i = 0; i < num_phy_chans; i++) {
  1598. if (d40_alloc_mask_set(&phys[i], is_src,
  1599. 0, is_log,
  1600. first_phy_user))
  1601. goto found_phy;
  1602. }
  1603. }
  1604. } else
  1605. for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
  1606. int phy_num = j + event_group * 2;
  1607. for (i = phy_num; i < phy_num + 2; i++) {
  1608. if (d40_alloc_mask_set(&phys[i],
  1609. is_src,
  1610. 0,
  1611. is_log,
  1612. first_phy_user))
  1613. goto found_phy;
  1614. }
  1615. }
  1616. return -EINVAL;
  1617. found_phy:
  1618. d40c->phy_chan = &phys[i];
  1619. d40c->log_num = D40_PHY_CHAN;
  1620. goto out;
  1621. }
  1622. if (dev_type == -1)
  1623. return -EINVAL;
  1624. /* Find logical channel */
  1625. for (j = 0; j < d40c->base->num_phy_chans; j += 8) {
  1626. int phy_num = j + event_group * 2;
  1627. if (d40c->dma_cfg.use_fixed_channel) {
  1628. i = d40c->dma_cfg.phy_channel;
  1629. if ((i != phy_num) && (i != phy_num + 1)) {
  1630. dev_err(chan2dev(d40c),
  1631. "invalid fixed phy channel %d\n", i);
  1632. return -EINVAL;
  1633. }
  1634. if (d40_alloc_mask_set(&phys[i], is_src, event_line,
  1635. is_log, first_phy_user))
  1636. goto found_log;
  1637. dev_err(chan2dev(d40c),
  1638. "could not allocate fixed phy channel %d\n", i);
  1639. return -EINVAL;
  1640. }
  1641. /*
  1642. * Spread logical channels across all available physical rather
  1643. * than pack every logical channel at the first available phy
  1644. * channels.
  1645. */
  1646. if (is_src) {
  1647. for (i = phy_num; i < phy_num + 2; i++) {
  1648. if (d40_alloc_mask_set(&phys[i], is_src,
  1649. event_line, is_log,
  1650. first_phy_user))
  1651. goto found_log;
  1652. }
  1653. } else {
  1654. for (i = phy_num + 1; i >= phy_num; i--) {
  1655. if (d40_alloc_mask_set(&phys[i], is_src,
  1656. event_line, is_log,
  1657. first_phy_user))
  1658. goto found_log;
  1659. }
  1660. }
  1661. }
  1662. return -EINVAL;
  1663. found_log:
  1664. d40c->phy_chan = &phys[i];
  1665. d40c->log_num = log_num;
  1666. out:
  1667. if (is_log)
  1668. d40c->base->lookup_log_chans[d40c->log_num] = d40c;
  1669. else
  1670. d40c->base->lookup_phy_chans[d40c->phy_chan->num] = d40c;
  1671. return 0;
  1672. }
  1673. static int d40_config_memcpy(struct d40_chan *d40c)
  1674. {
  1675. dma_cap_mask_t cap = d40c->chan.device->cap_mask;
  1676. if (dma_has_cap(DMA_MEMCPY, cap) && !dma_has_cap(DMA_SLAVE, cap)) {
  1677. d40c->dma_cfg = dma40_memcpy_conf_log;
  1678. d40c->dma_cfg.dev_type = dma40_memcpy_channels[d40c->chan.chan_id];
  1679. d40_log_cfg(&d40c->dma_cfg,
  1680. &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
  1681. } else if (dma_has_cap(DMA_MEMCPY, cap) &&
  1682. dma_has_cap(DMA_SLAVE, cap)) {
  1683. d40c->dma_cfg = dma40_memcpy_conf_phy;
  1684. /* Generate interrrupt at end of transfer or relink. */
  1685. d40c->dst_def_cfg |= BIT(D40_SREG_CFG_TIM_POS);
  1686. /* Generate interrupt on error. */
  1687. d40c->src_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
  1688. d40c->dst_def_cfg |= BIT(D40_SREG_CFG_EIM_POS);
  1689. } else {
  1690. chan_err(d40c, "No memcpy\n");
  1691. return -EINVAL;
  1692. }
  1693. return 0;
  1694. }
  1695. static int d40_free_dma(struct d40_chan *d40c)
  1696. {
  1697. int res = 0;
  1698. u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
  1699. struct d40_phy_res *phy = d40c->phy_chan;
  1700. bool is_src;
  1701. /* Terminate all queued and active transfers */
  1702. d40_term_all(d40c);
  1703. if (phy == NULL) {
  1704. chan_err(d40c, "phy == null\n");
  1705. return -EINVAL;
  1706. }
  1707. if (phy->allocated_src == D40_ALLOC_FREE &&
  1708. phy->allocated_dst == D40_ALLOC_FREE) {
  1709. chan_err(d40c, "channel already free\n");
  1710. return -EINVAL;
  1711. }
  1712. if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
  1713. d40c->dma_cfg.dir == DMA_MEM_TO_MEM)
  1714. is_src = false;
  1715. else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
  1716. is_src = true;
  1717. else {
  1718. chan_err(d40c, "Unknown direction\n");
  1719. return -EINVAL;
  1720. }
  1721. pm_runtime_get_sync(d40c->base->dev);
  1722. res = d40_channel_execute_command(d40c, D40_DMA_STOP);
  1723. if (res) {
  1724. chan_err(d40c, "stop failed\n");
  1725. goto out;
  1726. }
  1727. d40_alloc_mask_free(phy, is_src, chan_is_logical(d40c) ? event : 0);
  1728. if (chan_is_logical(d40c))
  1729. d40c->base->lookup_log_chans[d40c->log_num] = NULL;
  1730. else
  1731. d40c->base->lookup_phy_chans[phy->num] = NULL;
  1732. if (d40c->busy) {
  1733. pm_runtime_mark_last_busy(d40c->base->dev);
  1734. pm_runtime_put_autosuspend(d40c->base->dev);
  1735. }
  1736. d40c->busy = false;
  1737. d40c->phy_chan = NULL;
  1738. d40c->configured = false;
  1739. out:
  1740. pm_runtime_mark_last_busy(d40c->base->dev);
  1741. pm_runtime_put_autosuspend(d40c->base->dev);
  1742. return res;
  1743. }
  1744. static bool d40_is_paused(struct d40_chan *d40c)
  1745. {
  1746. void __iomem *chanbase = chan_base(d40c);
  1747. bool is_paused = false;
  1748. unsigned long flags;
  1749. void __iomem *active_reg;
  1750. u32 status;
  1751. u32 event = D40_TYPE_TO_EVENT(d40c->dma_cfg.dev_type);
  1752. spin_lock_irqsave(&d40c->lock, flags);
  1753. if (chan_is_physical(d40c)) {
  1754. if (d40c->phy_chan->num % 2 == 0)
  1755. active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
  1756. else
  1757. active_reg = d40c->base->virtbase + D40_DREG_ACTIVO;
  1758. status = (readl(active_reg) &
  1759. D40_CHAN_POS_MASK(d40c->phy_chan->num)) >>
  1760. D40_CHAN_POS(d40c->phy_chan->num);
  1761. if (status == D40_DMA_SUSPENDED || status == D40_DMA_STOP)
  1762. is_paused = true;
  1763. goto _exit;
  1764. }
  1765. if (d40c->dma_cfg.dir == DMA_MEM_TO_DEV ||
  1766. d40c->dma_cfg.dir == DMA_MEM_TO_MEM) {
  1767. status = readl(chanbase + D40_CHAN_REG_SDLNK);
  1768. } else if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM) {
  1769. status = readl(chanbase + D40_CHAN_REG_SSLNK);
  1770. } else {
  1771. chan_err(d40c, "Unknown direction\n");
  1772. goto _exit;
  1773. }
  1774. status = (status & D40_EVENTLINE_MASK(event)) >>
  1775. D40_EVENTLINE_POS(event);
  1776. if (status != D40_DMA_RUN)
  1777. is_paused = true;
  1778. _exit:
  1779. spin_unlock_irqrestore(&d40c->lock, flags);
  1780. return is_paused;
  1781. }
  1782. static u32 stedma40_residue(struct dma_chan *chan)
  1783. {
  1784. struct d40_chan *d40c =
  1785. container_of(chan, struct d40_chan, chan);
  1786. u32 bytes_left;
  1787. unsigned long flags;
  1788. spin_lock_irqsave(&d40c->lock, flags);
  1789. bytes_left = d40_residue(d40c);
  1790. spin_unlock_irqrestore(&d40c->lock, flags);
  1791. return bytes_left;
  1792. }
  1793. static int
  1794. d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
  1795. struct scatterlist *sg_src, struct scatterlist *sg_dst,
  1796. unsigned int sg_len, dma_addr_t src_dev_addr,
  1797. dma_addr_t dst_dev_addr)
  1798. {
  1799. struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
  1800. struct stedma40_half_channel_info *src_info = &cfg->src_info;
  1801. struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
  1802. int ret;
  1803. ret = d40_log_sg_to_lli(sg_src, sg_len,
  1804. src_dev_addr,
  1805. desc->lli_log.src,
  1806. chan->log_def.lcsp1,
  1807. src_info->data_width,
  1808. dst_info->data_width);
  1809. ret = d40_log_sg_to_lli(sg_dst, sg_len,
  1810. dst_dev_addr,
  1811. desc->lli_log.dst,
  1812. chan->log_def.lcsp3,
  1813. dst_info->data_width,
  1814. src_info->data_width);
  1815. return ret < 0 ? ret : 0;
  1816. }
  1817. static int
  1818. d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
  1819. struct scatterlist *sg_src, struct scatterlist *sg_dst,
  1820. unsigned int sg_len, dma_addr_t src_dev_addr,
  1821. dma_addr_t dst_dev_addr)
  1822. {
  1823. struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
  1824. struct stedma40_half_channel_info *src_info = &cfg->src_info;
  1825. struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
  1826. unsigned long flags = 0;
  1827. int ret;
  1828. if (desc->cyclic)
  1829. flags |= LLI_CYCLIC | LLI_TERM_INT;
  1830. ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
  1831. desc->lli_phy.src,
  1832. virt_to_phys(desc->lli_phy.src),
  1833. chan->src_def_cfg,
  1834. src_info, dst_info, flags);
  1835. ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
  1836. desc->lli_phy.dst,
  1837. virt_to_phys(desc->lli_phy.dst),
  1838. chan->dst_def_cfg,
  1839. dst_info, src_info, flags);
  1840. dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
  1841. desc->lli_pool.size, DMA_TO_DEVICE);
  1842. return ret < 0 ? ret : 0;
  1843. }
  1844. static struct d40_desc *
  1845. d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
  1846. unsigned int sg_len, unsigned long dma_flags)
  1847. {
  1848. struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
  1849. struct d40_desc *desc;
  1850. int ret;
  1851. desc = d40_desc_get(chan);
  1852. if (!desc)
  1853. return NULL;
  1854. desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
  1855. cfg->dst_info.data_width);
  1856. if (desc->lli_len < 0) {
  1857. chan_err(chan, "Unaligned size\n");
  1858. goto err;
  1859. }
  1860. ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
  1861. if (ret < 0) {
  1862. chan_err(chan, "Could not allocate lli\n");
  1863. goto err;
  1864. }
  1865. desc->lli_current = 0;
  1866. desc->txd.flags = dma_flags;
  1867. desc->txd.tx_submit = d40_tx_submit;
  1868. dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
  1869. return desc;
  1870. err:
  1871. d40_desc_free(chan, desc);
  1872. return NULL;
  1873. }
  1874. static struct dma_async_tx_descriptor *
  1875. d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
  1876. struct scatterlist *sg_dst, unsigned int sg_len,
  1877. enum dma_transfer_direction direction, unsigned long dma_flags)
  1878. {
  1879. struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
  1880. dma_addr_t src_dev_addr = 0;
  1881. dma_addr_t dst_dev_addr = 0;
  1882. struct d40_desc *desc;
  1883. unsigned long flags;
  1884. int ret;
  1885. if (!chan->phy_chan) {
  1886. chan_err(chan, "Cannot prepare unallocated channel\n");
  1887. return NULL;
  1888. }
  1889. spin_lock_irqsave(&chan->lock, flags);
  1890. desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
  1891. if (desc == NULL)
  1892. goto err;
  1893. if (sg_next(&sg_src[sg_len - 1]) == sg_src)
  1894. desc->cyclic = true;
  1895. if (direction == DMA_DEV_TO_MEM)
  1896. src_dev_addr = chan->runtime_addr;
  1897. else if (direction == DMA_MEM_TO_DEV)
  1898. dst_dev_addr = chan->runtime_addr;
  1899. if (chan_is_logical(chan))
  1900. ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
  1901. sg_len, src_dev_addr, dst_dev_addr);
  1902. else
  1903. ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
  1904. sg_len, src_dev_addr, dst_dev_addr);
  1905. if (ret) {
  1906. chan_err(chan, "Failed to prepare %s sg job: %d\n",
  1907. chan_is_logical(chan) ? "log" : "phy", ret);
  1908. goto err;
  1909. }
  1910. /*
  1911. * add descriptor to the prepare queue in order to be able
  1912. * to free them later in terminate_all
  1913. */
  1914. list_add_tail(&desc->node, &chan->prepare_queue);
  1915. spin_unlock_irqrestore(&chan->lock, flags);
  1916. return &desc->txd;
  1917. err:
  1918. if (desc)
  1919. d40_desc_free(chan, desc);
  1920. spin_unlock_irqrestore(&chan->lock, flags);
  1921. return NULL;
  1922. }
  1923. bool stedma40_filter(struct dma_chan *chan, void *data)
  1924. {
  1925. struct stedma40_chan_cfg *info = data;
  1926. struct d40_chan *d40c =
  1927. container_of(chan, struct d40_chan, chan);
  1928. int err;
  1929. if (data) {
  1930. err = d40_validate_conf(d40c, info);
  1931. if (!err)
  1932. d40c->dma_cfg = *info;
  1933. } else
  1934. err = d40_config_memcpy(d40c);
  1935. if (!err)
  1936. d40c->configured = true;
  1937. return err == 0;
  1938. }
  1939. EXPORT_SYMBOL(stedma40_filter);
  1940. static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
  1941. {
  1942. bool realtime = d40c->dma_cfg.realtime;
  1943. bool highprio = d40c->dma_cfg.high_priority;
  1944. u32 rtreg;
  1945. u32 event = D40_TYPE_TO_EVENT(dev_type);
  1946. u32 group = D40_TYPE_TO_GROUP(dev_type);
  1947. u32 bit = BIT(event);
  1948. u32 prioreg;
  1949. struct d40_gen_dmac *dmac = &d40c->base->gen_dmac;
  1950. rtreg = realtime ? dmac->realtime_en : dmac->realtime_clear;
  1951. /*
  1952. * Due to a hardware bug, in some cases a logical channel triggered by
  1953. * a high priority destination event line can generate extra packet
  1954. * transactions.
  1955. *
  1956. * The workaround is to not set the high priority level for the
  1957. * destination event lines that trigger logical channels.
  1958. */
  1959. if (!src && chan_is_logical(d40c))
  1960. highprio = false;
  1961. prioreg = highprio ? dmac->high_prio_en : dmac->high_prio_clear;
  1962. /* Destination event lines are stored in the upper halfword */
  1963. if (!src)
  1964. bit <<= 16;
  1965. writel(bit, d40c->base->virtbase + prioreg + group * 4);
  1966. writel(bit, d40c->base->virtbase + rtreg + group * 4);
  1967. }
  1968. static void d40_set_prio_realtime(struct d40_chan *d40c)
  1969. {
  1970. if (d40c->base->rev < 3)
  1971. return;
  1972. if ((d40c->dma_cfg.dir == DMA_DEV_TO_MEM) ||
  1973. (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
  1974. __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, true);
  1975. if ((d40c->dma_cfg.dir == DMA_MEM_TO_DEV) ||
  1976. (d40c->dma_cfg.dir == DMA_DEV_TO_DEV))
  1977. __d40_set_prio_rt(d40c, d40c->dma_cfg.dev_type, false);
  1978. }
  1979. #define D40_DT_FLAGS_MODE(flags) ((flags >> 0) & 0x1)
  1980. #define D40_DT_FLAGS_DIR(flags) ((flags >> 1) & 0x1)
  1981. #define D40_DT_FLAGS_BIG_ENDIAN(flags) ((flags >> 2) & 0x1)
  1982. #define D40_DT_FLAGS_FIXED_CHAN(flags) ((flags >> 3) & 0x1)
  1983. #define D40_DT_FLAGS_HIGH_PRIO(flags) ((flags >> 4) & 0x1)
  1984. static struct dma_chan *d40_xlate(struct of_phandle_args *dma_spec,
  1985. struct of_dma *ofdma)
  1986. {
  1987. struct stedma40_chan_cfg cfg;
  1988. dma_cap_mask_t cap;
  1989. u32 flags;
  1990. memset(&cfg, 0, sizeof(struct stedma40_chan_cfg));
  1991. dma_cap_zero(cap);
  1992. dma_cap_set(DMA_SLAVE, cap);
  1993. cfg.dev_type = dma_spec->args[0];
  1994. flags = dma_spec->args[2];
  1995. switch (D40_DT_FLAGS_MODE(flags)) {
  1996. case 0: cfg.mode = STEDMA40_MODE_LOGICAL; break;
  1997. case 1: cfg.mode = STEDMA40_MODE_PHYSICAL; break;
  1998. }
  1999. switch (D40_DT_FLAGS_DIR(flags)) {
  2000. case 0:
  2001. cfg.dir = DMA_MEM_TO_DEV;
  2002. cfg.dst_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
  2003. break;
  2004. case 1:
  2005. cfg.dir = DMA_DEV_TO_MEM;
  2006. cfg.src_info.big_endian = D40_DT_FLAGS_BIG_ENDIAN(flags);
  2007. break;
  2008. }
  2009. if (D40_DT_FLAGS_FIXED_CHAN(flags)) {
  2010. cfg.phy_channel = dma_spec->args[1];
  2011. cfg.use_fixed_channel = true;
  2012. }
  2013. if (D40_DT_FLAGS_HIGH_PRIO(flags))
  2014. cfg.high_priority = true;
  2015. return dma_request_channel(cap, stedma40_filter, &cfg);
  2016. }
  2017. /* DMA ENGINE functions */
  2018. static int d40_alloc_chan_resources(struct dma_chan *chan)
  2019. {
  2020. int err;
  2021. unsigned long flags;
  2022. struct d40_chan *d40c =
  2023. container_of(chan, struct d40_chan, chan);
  2024. bool is_free_phy;
  2025. spin_lock_irqsave(&d40c->lock, flags);
  2026. dma_cookie_init(chan);
  2027. /* If no dma configuration is set use default configuration (memcpy) */
  2028. if (!d40c->configured) {
  2029. err = d40_config_memcpy(d40c);
  2030. if (err) {
  2031. chan_err(d40c, "Failed to configure memcpy channel\n");
  2032. goto fail;
  2033. }
  2034. }
  2035. err = d40_allocate_channel(d40c, &is_free_phy);
  2036. if (err) {
  2037. chan_err(d40c, "Failed to allocate channel\n");
  2038. d40c->configured = false;
  2039. goto fail;
  2040. }
  2041. pm_runtime_get_sync(d40c->base->dev);
  2042. d40_set_prio_realtime(d40c);
  2043. if (chan_is_logical(d40c)) {
  2044. if (d40c->dma_cfg.dir == DMA_DEV_TO_MEM)
  2045. d40c->lcpa = d40c->base->lcpa_base +
  2046. d40c->dma_cfg.dev_type * D40_LCPA_CHAN_SIZE;
  2047. else
  2048. d40c->lcpa = d40c->base->lcpa_base +
  2049. d40c->dma_cfg.dev_type *
  2050. D40_LCPA_CHAN_SIZE + D40_LCPA_CHAN_DST_DELTA;
  2051. /* Unmask the Global Interrupt Mask. */
  2052. d40c->src_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
  2053. d40c->dst_def_cfg |= BIT(D40_SREG_CFG_LOG_GIM_POS);
  2054. }
  2055. dev_dbg(chan2dev(d40c), "allocated %s channel (phy %d%s)\n",
  2056. chan_is_logical(d40c) ? "logical" : "physical",
  2057. d40c->phy_chan->num,
  2058. d40c->dma_cfg.use_fixed_channel ? ", fixed" : "");
  2059. /*
  2060. * Only write channel configuration to the DMA if the physical
  2061. * resource is free. In case of multiple logical channels
  2062. * on the same physical resource, only the first write is necessary.
  2063. */
  2064. if (is_free_phy)
  2065. d40_config_write(d40c);
  2066. fail:
  2067. pm_runtime_mark_last_busy(d40c->base->dev);
  2068. pm_runtime_put_autosuspend(d40c->base->dev);
  2069. spin_unlock_irqrestore(&d40c->lock, flags);
  2070. return err;
  2071. }
  2072. static void d40_free_chan_resources(struct dma_chan *chan)
  2073. {
  2074. struct d40_chan *d40c =
  2075. container_of(chan, struct d40_chan, chan);
  2076. int err;
  2077. unsigned long flags;
  2078. if (d40c->phy_chan == NULL) {
  2079. chan_err(d40c, "Cannot free unallocated channel\n");
  2080. return;
  2081. }
  2082. spin_lock_irqsave(&d40c->lock, flags);
  2083. err = d40_free_dma(d40c);
  2084. if (err)
  2085. chan_err(d40c, "Failed to free channel\n");
  2086. spin_unlock_irqrestore(&d40c->lock, flags);
  2087. }
  2088. static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
  2089. dma_addr_t dst,
  2090. dma_addr_t src,
  2091. size_t size,
  2092. unsigned long dma_flags)
  2093. {
  2094. struct scatterlist dst_sg;
  2095. struct scatterlist src_sg;
  2096. sg_init_table(&dst_sg, 1);
  2097. sg_init_table(&src_sg, 1);
  2098. sg_dma_address(&dst_sg) = dst;
  2099. sg_dma_address(&src_sg) = src;
  2100. sg_dma_len(&dst_sg) = size;
  2101. sg_dma_len(&src_sg) = size;
  2102. return d40_prep_sg(chan, &src_sg, &dst_sg, 1,
  2103. DMA_MEM_TO_MEM, dma_flags);
  2104. }
  2105. static struct dma_async_tx_descriptor *
  2106. d40_prep_memcpy_sg(struct dma_chan *chan,
  2107. struct scatterlist *dst_sg, unsigned int dst_nents,
  2108. struct scatterlist *src_sg, unsigned int src_nents,
  2109. unsigned long dma_flags)
  2110. {
  2111. if (dst_nents != src_nents)
  2112. return NULL;
  2113. return d40_prep_sg(chan, src_sg, dst_sg, src_nents,
  2114. DMA_MEM_TO_MEM, dma_flags);
  2115. }
  2116. static struct dma_async_tx_descriptor *
  2117. d40_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  2118. unsigned int sg_len, enum dma_transfer_direction direction,
  2119. unsigned long dma_flags, void *context)
  2120. {
  2121. if (!is_slave_direction(direction))
  2122. return NULL;
  2123. return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
  2124. }
  2125. static struct dma_async_tx_descriptor *
  2126. dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
  2127. size_t buf_len, size_t period_len,
  2128. enum dma_transfer_direction direction, unsigned long flags)
  2129. {
  2130. unsigned int periods = buf_len / period_len;
  2131. struct dma_async_tx_descriptor *txd;
  2132. struct scatterlist *sg;
  2133. int i;
  2134. sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
  2135. if (!sg)
  2136. return NULL;
  2137. for (i = 0; i < periods; i++) {
  2138. sg_dma_address(&sg[i]) = dma_addr;
  2139. sg_dma_len(&sg[i]) = period_len;
  2140. dma_addr += period_len;
  2141. }
  2142. sg[periods].offset = 0;
  2143. sg_dma_len(&sg[periods]) = 0;
  2144. sg[periods].page_link =
  2145. ((unsigned long)sg | 0x01) & ~0x02;
  2146. txd = d40_prep_sg(chan, sg, sg, periods, direction,
  2147. DMA_PREP_INTERRUPT);
  2148. kfree(sg);
  2149. return txd;
  2150. }
  2151. static enum dma_status d40_tx_status(struct dma_chan *chan,
  2152. dma_cookie_t cookie,
  2153. struct dma_tx_state *txstate)
  2154. {
  2155. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  2156. enum dma_status ret;
  2157. if (d40c->phy_chan == NULL) {
  2158. chan_err(d40c, "Cannot read status of unallocated channel\n");
  2159. return -EINVAL;
  2160. }
  2161. ret = dma_cookie_status(chan, cookie, txstate);
  2162. if (ret != DMA_COMPLETE)
  2163. dma_set_residue(txstate, stedma40_residue(chan));
  2164. if (d40_is_paused(d40c))
  2165. ret = DMA_PAUSED;
  2166. return ret;
  2167. }
  2168. static void d40_issue_pending(struct dma_chan *chan)
  2169. {
  2170. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  2171. unsigned long flags;
  2172. if (d40c->phy_chan == NULL) {
  2173. chan_err(d40c, "Channel is not allocated!\n");
  2174. return;
  2175. }
  2176. spin_lock_irqsave(&d40c->lock, flags);
  2177. list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
  2178. /* Busy means that queued jobs are already being processed */
  2179. if (!d40c->busy)
  2180. (void) d40_queue_start(d40c);
  2181. spin_unlock_irqrestore(&d40c->lock, flags);
  2182. }
  2183. static int d40_terminate_all(struct dma_chan *chan)
  2184. {
  2185. unsigned long flags;
  2186. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  2187. int ret;
  2188. if (d40c->phy_chan == NULL) {
  2189. chan_err(d40c, "Channel is not allocated!\n");
  2190. return -EINVAL;
  2191. }
  2192. spin_lock_irqsave(&d40c->lock, flags);
  2193. pm_runtime_get_sync(d40c->base->dev);
  2194. ret = d40_channel_execute_command(d40c, D40_DMA_STOP);
  2195. if (ret)
  2196. chan_err(d40c, "Failed to stop channel\n");
  2197. d40_term_all(d40c);
  2198. pm_runtime_mark_last_busy(d40c->base->dev);
  2199. pm_runtime_put_autosuspend(d40c->base->dev);
  2200. if (d40c->busy) {
  2201. pm_runtime_mark_last_busy(d40c->base->dev);
  2202. pm_runtime_put_autosuspend(d40c->base->dev);
  2203. }
  2204. d40c->busy = false;
  2205. spin_unlock_irqrestore(&d40c->lock, flags);
  2206. return 0;
  2207. }
  2208. static int
  2209. dma40_config_to_halfchannel(struct d40_chan *d40c,
  2210. struct stedma40_half_channel_info *info,
  2211. u32 maxburst)
  2212. {
  2213. int psize;
  2214. if (chan_is_logical(d40c)) {
  2215. if (maxburst >= 16)
  2216. psize = STEDMA40_PSIZE_LOG_16;
  2217. else if (maxburst >= 8)
  2218. psize = STEDMA40_PSIZE_LOG_8;
  2219. else if (maxburst >= 4)
  2220. psize = STEDMA40_PSIZE_LOG_4;
  2221. else
  2222. psize = STEDMA40_PSIZE_LOG_1;
  2223. } else {
  2224. if (maxburst >= 16)
  2225. psize = STEDMA40_PSIZE_PHY_16;
  2226. else if (maxburst >= 8)
  2227. psize = STEDMA40_PSIZE_PHY_8;
  2228. else if (maxburst >= 4)
  2229. psize = STEDMA40_PSIZE_PHY_4;
  2230. else
  2231. psize = STEDMA40_PSIZE_PHY_1;
  2232. }
  2233. info->psize = psize;
  2234. info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
  2235. return 0;
  2236. }
  2237. /* Runtime reconfiguration extension */
  2238. static int d40_set_runtime_config(struct dma_chan *chan,
  2239. struct dma_slave_config *config)
  2240. {
  2241. struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
  2242. struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
  2243. enum dma_slave_buswidth src_addr_width, dst_addr_width;
  2244. dma_addr_t config_addr;
  2245. u32 src_maxburst, dst_maxburst;
  2246. int ret;
  2247. if (d40c->phy_chan == NULL) {
  2248. chan_err(d40c, "Channel is not allocated!\n");
  2249. return -EINVAL;
  2250. }
  2251. src_addr_width = config->src_addr_width;
  2252. src_maxburst = config->src_maxburst;
  2253. dst_addr_width = config->dst_addr_width;
  2254. dst_maxburst = config->dst_maxburst;
  2255. if (config->direction == DMA_DEV_TO_MEM) {
  2256. config_addr = config->src_addr;
  2257. if (cfg->dir != DMA_DEV_TO_MEM)
  2258. dev_dbg(d40c->base->dev,
  2259. "channel was not configured for peripheral "
  2260. "to memory transfer (%d) overriding\n",
  2261. cfg->dir);
  2262. cfg->dir = DMA_DEV_TO_MEM;
  2263. /* Configure the memory side */
  2264. if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  2265. dst_addr_width = src_addr_width;
  2266. if (dst_maxburst == 0)
  2267. dst_maxburst = src_maxburst;
  2268. } else if (config->direction == DMA_MEM_TO_DEV) {
  2269. config_addr = config->dst_addr;
  2270. if (cfg->dir != DMA_MEM_TO_DEV)
  2271. dev_dbg(d40c->base->dev,
  2272. "channel was not configured for memory "
  2273. "to peripheral transfer (%d) overriding\n",
  2274. cfg->dir);
  2275. cfg->dir = DMA_MEM_TO_DEV;
  2276. /* Configure the memory side */
  2277. if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  2278. src_addr_width = dst_addr_width;
  2279. if (src_maxburst == 0)
  2280. src_maxburst = dst_maxburst;
  2281. } else {
  2282. dev_err(d40c->base->dev,
  2283. "unrecognized channel direction %d\n",
  2284. config->direction);
  2285. return -EINVAL;
  2286. }
  2287. if (config_addr <= 0) {
  2288. dev_err(d40c->base->dev, "no address supplied\n");
  2289. return -EINVAL;
  2290. }
  2291. if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
  2292. dev_err(d40c->base->dev,
  2293. "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
  2294. src_maxburst,
  2295. src_addr_width,
  2296. dst_maxburst,
  2297. dst_addr_width);
  2298. return -EINVAL;
  2299. }
  2300. if (src_maxburst > 16) {
  2301. src_maxburst = 16;
  2302. dst_maxburst = src_maxburst * src_addr_width / dst_addr_width;
  2303. } else if (dst_maxburst > 16) {
  2304. dst_maxburst = 16;
  2305. src_maxburst = dst_maxburst * dst_addr_width / src_addr_width;
  2306. }
  2307. /* Only valid widths are; 1, 2, 4 and 8. */
  2308. if (src_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
  2309. src_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
  2310. dst_addr_width <= DMA_SLAVE_BUSWIDTH_UNDEFINED ||
  2311. dst_addr_width > DMA_SLAVE_BUSWIDTH_8_BYTES ||
  2312. !is_power_of_2(src_addr_width) ||
  2313. !is_power_of_2(dst_addr_width))
  2314. return -EINVAL;
  2315. cfg->src_info.data_width = src_addr_width;
  2316. cfg->dst_info.data_width = dst_addr_width;
  2317. ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
  2318. src_maxburst);
  2319. if (ret)
  2320. return ret;
  2321. ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
  2322. dst_maxburst);
  2323. if (ret)
  2324. return ret;
  2325. /* Fill in register values */
  2326. if (chan_is_logical(d40c))
  2327. d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
  2328. else
  2329. d40_phy_cfg(cfg, &d40c->src_def_cfg, &d40c->dst_def_cfg);
  2330. /* These settings will take precedence later */
  2331. d40c->runtime_addr = config_addr;
  2332. d40c->runtime_direction = config->direction;
  2333. dev_dbg(d40c->base->dev,
  2334. "configured channel %s for %s, data width %d/%d, "
  2335. "maxburst %d/%d elements, LE, no flow control\n",
  2336. dma_chan_name(chan),
  2337. (config->direction == DMA_DEV_TO_MEM) ? "RX" : "TX",
  2338. src_addr_width, dst_addr_width,
  2339. src_maxburst, dst_maxburst);
  2340. return 0;
  2341. }
  2342. /* Initialization functions */
  2343. static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
  2344. struct d40_chan *chans, int offset,
  2345. int num_chans)
  2346. {
  2347. int i = 0;
  2348. struct d40_chan *d40c;
  2349. INIT_LIST_HEAD(&dma->channels);
  2350. for (i = offset; i < offset + num_chans; i++) {
  2351. d40c = &chans[i];
  2352. d40c->base = base;
  2353. d40c->chan.device = dma;
  2354. spin_lock_init(&d40c->lock);
  2355. d40c->log_num = D40_PHY_CHAN;
  2356. INIT_LIST_HEAD(&d40c->done);
  2357. INIT_LIST_HEAD(&d40c->active);
  2358. INIT_LIST_HEAD(&d40c->queue);
  2359. INIT_LIST_HEAD(&d40c->pending_queue);
  2360. INIT_LIST_HEAD(&d40c->client);
  2361. INIT_LIST_HEAD(&d40c->prepare_queue);
  2362. tasklet_init(&d40c->tasklet, dma_tasklet,
  2363. (unsigned long) d40c);
  2364. list_add_tail(&d40c->chan.device_node,
  2365. &dma->channels);
  2366. }
  2367. }
  2368. static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
  2369. {
  2370. if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
  2371. dev->device_prep_slave_sg = d40_prep_slave_sg;
  2372. if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
  2373. dev->device_prep_dma_memcpy = d40_prep_memcpy;
  2374. /*
  2375. * This controller can only access address at even
  2376. * 32bit boundaries, i.e. 2^2
  2377. */
  2378. dev->copy_align = DMAENGINE_ALIGN_4_BYTES;
  2379. }
  2380. if (dma_has_cap(DMA_SG, dev->cap_mask))
  2381. dev->device_prep_dma_sg = d40_prep_memcpy_sg;
  2382. if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
  2383. dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
  2384. dev->device_alloc_chan_resources = d40_alloc_chan_resources;
  2385. dev->device_free_chan_resources = d40_free_chan_resources;
  2386. dev->device_issue_pending = d40_issue_pending;
  2387. dev->device_tx_status = d40_tx_status;
  2388. dev->device_config = d40_set_runtime_config;
  2389. dev->device_pause = d40_pause;
  2390. dev->device_resume = d40_resume;
  2391. dev->device_terminate_all = d40_terminate_all;
  2392. dev->dev = base->dev;
  2393. }
  2394. static int __init d40_dmaengine_init(struct d40_base *base,
  2395. int num_reserved_chans)
  2396. {
  2397. int err ;
  2398. d40_chan_init(base, &base->dma_slave, base->log_chans,
  2399. 0, base->num_log_chans);
  2400. dma_cap_zero(base->dma_slave.cap_mask);
  2401. dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
  2402. dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
  2403. d40_ops_init(base, &base->dma_slave);
  2404. err = dma_async_device_register(&base->dma_slave);
  2405. if (err) {
  2406. d40_err(base->dev, "Failed to register slave channels\n");
  2407. goto failure1;
  2408. }
  2409. d40_chan_init(base, &base->dma_memcpy, base->log_chans,
  2410. base->num_log_chans, base->num_memcpy_chans);
  2411. dma_cap_zero(base->dma_memcpy.cap_mask);
  2412. dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
  2413. dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
  2414. d40_ops_init(base, &base->dma_memcpy);
  2415. err = dma_async_device_register(&base->dma_memcpy);
  2416. if (err) {
  2417. d40_err(base->dev,
  2418. "Failed to register memcpy only channels\n");
  2419. goto failure2;
  2420. }
  2421. d40_chan_init(base, &base->dma_both, base->phy_chans,
  2422. 0, num_reserved_chans);
  2423. dma_cap_zero(base->dma_both.cap_mask);
  2424. dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
  2425. dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
  2426. dma_cap_set(DMA_SG, base->dma_both.cap_mask);
  2427. dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
  2428. d40_ops_init(base, &base->dma_both);
  2429. err = dma_async_device_register(&base->dma_both);
  2430. if (err) {
  2431. d40_err(base->dev,
  2432. "Failed to register logical and physical capable channels\n");
  2433. goto failure3;
  2434. }
  2435. return 0;
  2436. failure3:
  2437. dma_async_device_unregister(&base->dma_memcpy);
  2438. failure2:
  2439. dma_async_device_unregister(&base->dma_slave);
  2440. failure1:
  2441. return err;
  2442. }
  2443. /* Suspend resume functionality */
  2444. #ifdef CONFIG_PM_SLEEP
  2445. static int dma40_suspend(struct device *dev)
  2446. {
  2447. struct platform_device *pdev = to_platform_device(dev);
  2448. struct d40_base *base = platform_get_drvdata(pdev);
  2449. int ret;
  2450. ret = pm_runtime_force_suspend(dev);
  2451. if (ret)
  2452. return ret;
  2453. if (base->lcpa_regulator)
  2454. ret = regulator_disable(base->lcpa_regulator);
  2455. return ret;
  2456. }
  2457. static int dma40_resume(struct device *dev)
  2458. {
  2459. struct platform_device *pdev = to_platform_device(dev);
  2460. struct d40_base *base = platform_get_drvdata(pdev);
  2461. int ret = 0;
  2462. if (base->lcpa_regulator) {
  2463. ret = regulator_enable(base->lcpa_regulator);
  2464. if (ret)
  2465. return ret;
  2466. }
  2467. return pm_runtime_force_resume(dev);
  2468. }
  2469. #endif
  2470. #ifdef CONFIG_PM
  2471. static void dma40_backup(void __iomem *baseaddr, u32 *backup,
  2472. u32 *regaddr, int num, bool save)
  2473. {
  2474. int i;
  2475. for (i = 0; i < num; i++) {
  2476. void __iomem *addr = baseaddr + regaddr[i];
  2477. if (save)
  2478. backup[i] = readl_relaxed(addr);
  2479. else
  2480. writel_relaxed(backup[i], addr);
  2481. }
  2482. }
  2483. static void d40_save_restore_registers(struct d40_base *base, bool save)
  2484. {
  2485. int i;
  2486. /* Save/Restore channel specific registers */
  2487. for (i = 0; i < base->num_phy_chans; i++) {
  2488. void __iomem *addr;
  2489. int idx;
  2490. if (base->phy_res[i].reserved)
  2491. continue;
  2492. addr = base->virtbase + D40_DREG_PCBASE + i * D40_DREG_PCDELTA;
  2493. idx = i * ARRAY_SIZE(d40_backup_regs_chan);
  2494. dma40_backup(addr, &base->reg_val_backup_chan[idx],
  2495. d40_backup_regs_chan,
  2496. ARRAY_SIZE(d40_backup_regs_chan),
  2497. save);
  2498. }
  2499. /* Save/Restore global registers */
  2500. dma40_backup(base->virtbase, base->reg_val_backup,
  2501. d40_backup_regs, ARRAY_SIZE(d40_backup_regs),
  2502. save);
  2503. /* Save/Restore registers only existing on dma40 v3 and later */
  2504. if (base->gen_dmac.backup)
  2505. dma40_backup(base->virtbase, base->reg_val_backup_v4,
  2506. base->gen_dmac.backup,
  2507. base->gen_dmac.backup_size,
  2508. save);
  2509. }
  2510. static int dma40_runtime_suspend(struct device *dev)
  2511. {
  2512. struct platform_device *pdev = to_platform_device(dev);
  2513. struct d40_base *base = platform_get_drvdata(pdev);
  2514. d40_save_restore_registers(base, true);
  2515. /* Don't disable/enable clocks for v1 due to HW bugs */
  2516. if (base->rev != 1)
  2517. writel_relaxed(base->gcc_pwr_off_mask,
  2518. base->virtbase + D40_DREG_GCC);
  2519. return 0;
  2520. }
  2521. static int dma40_runtime_resume(struct device *dev)
  2522. {
  2523. struct platform_device *pdev = to_platform_device(dev);
  2524. struct d40_base *base = platform_get_drvdata(pdev);
  2525. d40_save_restore_registers(base, false);
  2526. writel_relaxed(D40_DREG_GCC_ENABLE_ALL,
  2527. base->virtbase + D40_DREG_GCC);
  2528. return 0;
  2529. }
  2530. #endif
  2531. static const struct dev_pm_ops dma40_pm_ops = {
  2532. SET_LATE_SYSTEM_SLEEP_PM_OPS(dma40_suspend, dma40_resume)
  2533. SET_RUNTIME_PM_OPS(dma40_runtime_suspend,
  2534. dma40_runtime_resume,
  2535. NULL)
  2536. };
  2537. /* Initialization functions. */
  2538. static int __init d40_phy_res_init(struct d40_base *base)
  2539. {
  2540. int i;
  2541. int num_phy_chans_avail = 0;
  2542. u32 val[2];
  2543. int odd_even_bit = -2;
  2544. int gcc = D40_DREG_GCC_ENA;
  2545. val[0] = readl(base->virtbase + D40_DREG_PRSME);
  2546. val[1] = readl(base->virtbase + D40_DREG_PRSMO);
  2547. for (i = 0; i < base->num_phy_chans; i++) {
  2548. base->phy_res[i].num = i;
  2549. odd_even_bit += 2 * ((i % 2) == 0);
  2550. if (((val[i % 2] >> odd_even_bit) & 3) == 1) {
  2551. /* Mark security only channels as occupied */
  2552. base->phy_res[i].allocated_src = D40_ALLOC_PHY;
  2553. base->phy_res[i].allocated_dst = D40_ALLOC_PHY;
  2554. base->phy_res[i].reserved = true;
  2555. gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
  2556. D40_DREG_GCC_SRC);
  2557. gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(i),
  2558. D40_DREG_GCC_DST);
  2559. } else {
  2560. base->phy_res[i].allocated_src = D40_ALLOC_FREE;
  2561. base->phy_res[i].allocated_dst = D40_ALLOC_FREE;
  2562. base->phy_res[i].reserved = false;
  2563. num_phy_chans_avail++;
  2564. }
  2565. spin_lock_init(&base->phy_res[i].lock);
  2566. }
  2567. /* Mark disabled channels as occupied */
  2568. for (i = 0; base->plat_data->disabled_channels[i] != -1; i++) {
  2569. int chan = base->plat_data->disabled_channels[i];
  2570. base->phy_res[chan].allocated_src = D40_ALLOC_PHY;
  2571. base->phy_res[chan].allocated_dst = D40_ALLOC_PHY;
  2572. base->phy_res[chan].reserved = true;
  2573. gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
  2574. D40_DREG_GCC_SRC);
  2575. gcc |= D40_DREG_GCC_EVTGRP_ENA(D40_PHYS_TO_GROUP(chan),
  2576. D40_DREG_GCC_DST);
  2577. num_phy_chans_avail--;
  2578. }
  2579. /* Mark soft_lli channels */
  2580. for (i = 0; i < base->plat_data->num_of_soft_lli_chans; i++) {
  2581. int chan = base->plat_data->soft_lli_chans[i];
  2582. base->phy_res[chan].use_soft_lli = true;
  2583. }
  2584. dev_info(base->dev, "%d of %d physical DMA channels available\n",
  2585. num_phy_chans_avail, base->num_phy_chans);
  2586. /* Verify settings extended vs standard */
  2587. val[0] = readl(base->virtbase + D40_DREG_PRTYP);
  2588. for (i = 0; i < base->num_phy_chans; i++) {
  2589. if (base->phy_res[i].allocated_src == D40_ALLOC_FREE &&
  2590. (val[0] & 0x3) != 1)
  2591. dev_info(base->dev,
  2592. "[%s] INFO: channel %d is misconfigured (%d)\n",
  2593. __func__, i, val[0] & 0x3);
  2594. val[0] = val[0] >> 2;
  2595. }
  2596. /*
  2597. * To keep things simple, Enable all clocks initially.
  2598. * The clocks will get managed later post channel allocation.
  2599. * The clocks for the event lines on which reserved channels exists
  2600. * are not managed here.
  2601. */
  2602. writel(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
  2603. base->gcc_pwr_off_mask = gcc;
  2604. return num_phy_chans_avail;
  2605. }
  2606. static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
  2607. {
  2608. struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
  2609. struct clk *clk = NULL;
  2610. void __iomem *virtbase = NULL;
  2611. struct resource *res = NULL;
  2612. struct d40_base *base = NULL;
  2613. int num_log_chans = 0;
  2614. int num_phy_chans;
  2615. int num_memcpy_chans;
  2616. int clk_ret = -EINVAL;
  2617. int i;
  2618. u32 pid;
  2619. u32 cid;
  2620. u8 rev;
  2621. clk = clk_get(&pdev->dev, NULL);
  2622. if (IS_ERR(clk)) {
  2623. d40_err(&pdev->dev, "No matching clock found\n");
  2624. goto failure;
  2625. }
  2626. clk_ret = clk_prepare_enable(clk);
  2627. if (clk_ret) {
  2628. d40_err(&pdev->dev, "Failed to prepare/enable clock\n");
  2629. goto failure;
  2630. }
  2631. /* Get IO for DMAC base address */
  2632. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "base");
  2633. if (!res)
  2634. goto failure;
  2635. if (request_mem_region(res->start, resource_size(res),
  2636. D40_NAME " I/O base") == NULL)
  2637. goto failure;
  2638. virtbase = ioremap(res->start, resource_size(res));
  2639. if (!virtbase)
  2640. goto failure;
  2641. /* This is just a regular AMBA PrimeCell ID actually */
  2642. for (pid = 0, i = 0; i < 4; i++)
  2643. pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
  2644. & 255) << (i * 8);
  2645. for (cid = 0, i = 0; i < 4; i++)
  2646. cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
  2647. & 255) << (i * 8);
  2648. if (cid != AMBA_CID) {
  2649. d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
  2650. goto failure;
  2651. }
  2652. if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
  2653. d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
  2654. AMBA_MANF_BITS(pid),
  2655. AMBA_VENDOR_ST);
  2656. goto failure;
  2657. }
  2658. /*
  2659. * HW revision:
  2660. * DB8500ed has revision 0
  2661. * ? has revision 1
  2662. * DB8500v1 has revision 2
  2663. * DB8500v2 has revision 3
  2664. * AP9540v1 has revision 4
  2665. * DB8540v1 has revision 4
  2666. */
  2667. rev = AMBA_REV_BITS(pid);
  2668. if (rev < 2) {
  2669. d40_err(&pdev->dev, "hardware revision: %d is not supported", rev);
  2670. goto failure;
  2671. }
  2672. /* The number of physical channels on this HW */
  2673. if (plat_data->num_of_phy_chans)
  2674. num_phy_chans = plat_data->num_of_phy_chans;
  2675. else
  2676. num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
  2677. /* The number of channels used for memcpy */
  2678. if (plat_data->num_of_memcpy_chans)
  2679. num_memcpy_chans = plat_data->num_of_memcpy_chans;
  2680. else
  2681. num_memcpy_chans = ARRAY_SIZE(dma40_memcpy_channels);
  2682. num_log_chans = num_phy_chans * D40_MAX_LOG_CHAN_PER_PHY;
  2683. dev_info(&pdev->dev,
  2684. "hardware rev: %d @ %pa with %d physical and %d logical channels\n",
  2685. rev, &res->start, num_phy_chans, num_log_chans);
  2686. base = kzalloc(ALIGN(sizeof(struct d40_base), 4) +
  2687. (num_phy_chans + num_log_chans + num_memcpy_chans) *
  2688. sizeof(struct d40_chan), GFP_KERNEL);
  2689. if (base == NULL) {
  2690. d40_err(&pdev->dev, "Out of memory\n");
  2691. goto failure;
  2692. }
  2693. base->rev = rev;
  2694. base->clk = clk;
  2695. base->num_memcpy_chans = num_memcpy_chans;
  2696. base->num_phy_chans = num_phy_chans;
  2697. base->num_log_chans = num_log_chans;
  2698. base->phy_start = res->start;
  2699. base->phy_size = resource_size(res);
  2700. base->virtbase = virtbase;
  2701. base->plat_data = plat_data;
  2702. base->dev = &pdev->dev;
  2703. base->phy_chans = ((void *)base) + ALIGN(sizeof(struct d40_base), 4);
  2704. base->log_chans = &base->phy_chans[num_phy_chans];
  2705. if (base->plat_data->num_of_phy_chans == 14) {
  2706. base->gen_dmac.backup = d40_backup_regs_v4b;
  2707. base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4B;
  2708. base->gen_dmac.interrupt_en = D40_DREG_CPCMIS;
  2709. base->gen_dmac.interrupt_clear = D40_DREG_CPCICR;
  2710. base->gen_dmac.realtime_en = D40_DREG_CRSEG1;
  2711. base->gen_dmac.realtime_clear = D40_DREG_CRCEG1;
  2712. base->gen_dmac.high_prio_en = D40_DREG_CPSEG1;
  2713. base->gen_dmac.high_prio_clear = D40_DREG_CPCEG1;
  2714. base->gen_dmac.il = il_v4b;
  2715. base->gen_dmac.il_size = ARRAY_SIZE(il_v4b);
  2716. base->gen_dmac.init_reg = dma_init_reg_v4b;
  2717. base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4b);
  2718. } else {
  2719. if (base->rev >= 3) {
  2720. base->gen_dmac.backup = d40_backup_regs_v4a;
  2721. base->gen_dmac.backup_size = BACKUP_REGS_SZ_V4A;
  2722. }
  2723. base->gen_dmac.interrupt_en = D40_DREG_PCMIS;
  2724. base->gen_dmac.interrupt_clear = D40_DREG_PCICR;
  2725. base->gen_dmac.realtime_en = D40_DREG_RSEG1;
  2726. base->gen_dmac.realtime_clear = D40_DREG_RCEG1;
  2727. base->gen_dmac.high_prio_en = D40_DREG_PSEG1;
  2728. base->gen_dmac.high_prio_clear = D40_DREG_PCEG1;
  2729. base->gen_dmac.il = il_v4a;
  2730. base->gen_dmac.il_size = ARRAY_SIZE(il_v4a);
  2731. base->gen_dmac.init_reg = dma_init_reg_v4a;
  2732. base->gen_dmac.init_reg_size = ARRAY_SIZE(dma_init_reg_v4a);
  2733. }
  2734. base->phy_res = kzalloc(num_phy_chans * sizeof(struct d40_phy_res),
  2735. GFP_KERNEL);
  2736. if (!base->phy_res)
  2737. goto failure;
  2738. base->lookup_phy_chans = kzalloc(num_phy_chans *
  2739. sizeof(struct d40_chan *),
  2740. GFP_KERNEL);
  2741. if (!base->lookup_phy_chans)
  2742. goto failure;
  2743. base->lookup_log_chans = kzalloc(num_log_chans *
  2744. sizeof(struct d40_chan *),
  2745. GFP_KERNEL);
  2746. if (!base->lookup_log_chans)
  2747. goto failure;
  2748. base->reg_val_backup_chan = kmalloc(base->num_phy_chans *
  2749. sizeof(d40_backup_regs_chan),
  2750. GFP_KERNEL);
  2751. if (!base->reg_val_backup_chan)
  2752. goto failure;
  2753. base->lcla_pool.alloc_map =
  2754. kzalloc(num_phy_chans * sizeof(struct d40_desc *)
  2755. * D40_LCLA_LINK_PER_EVENT_GRP, GFP_KERNEL);
  2756. if (!base->lcla_pool.alloc_map)
  2757. goto failure;
  2758. base->desc_slab = kmem_cache_create(D40_NAME, sizeof(struct d40_desc),
  2759. 0, SLAB_HWCACHE_ALIGN,
  2760. NULL);
  2761. if (base->desc_slab == NULL)
  2762. goto failure;
  2763. return base;
  2764. failure:
  2765. if (!clk_ret)
  2766. clk_disable_unprepare(clk);
  2767. if (!IS_ERR(clk))
  2768. clk_put(clk);
  2769. if (virtbase)
  2770. iounmap(virtbase);
  2771. if (res)
  2772. release_mem_region(res->start,
  2773. resource_size(res));
  2774. if (virtbase)
  2775. iounmap(virtbase);
  2776. if (base) {
  2777. kfree(base->lcla_pool.alloc_map);
  2778. kfree(base->reg_val_backup_chan);
  2779. kfree(base->lookup_log_chans);
  2780. kfree(base->lookup_phy_chans);
  2781. kfree(base->phy_res);
  2782. kfree(base);
  2783. }
  2784. return NULL;
  2785. }
  2786. static void __init d40_hw_init(struct d40_base *base)
  2787. {
  2788. int i;
  2789. u32 prmseo[2] = {0, 0};
  2790. u32 activeo[2] = {0xFFFFFFFF, 0xFFFFFFFF};
  2791. u32 pcmis = 0;
  2792. u32 pcicr = 0;
  2793. struct d40_reg_val *dma_init_reg = base->gen_dmac.init_reg;
  2794. u32 reg_size = base->gen_dmac.init_reg_size;
  2795. for (i = 0; i < reg_size; i++)
  2796. writel(dma_init_reg[i].val,
  2797. base->virtbase + dma_init_reg[i].reg);
  2798. /* Configure all our dma channels to default settings */
  2799. for (i = 0; i < base->num_phy_chans; i++) {
  2800. activeo[i % 2] = activeo[i % 2] << 2;
  2801. if (base->phy_res[base->num_phy_chans - i - 1].allocated_src
  2802. == D40_ALLOC_PHY) {
  2803. activeo[i % 2] |= 3;
  2804. continue;
  2805. }
  2806. /* Enable interrupt # */
  2807. pcmis = (pcmis << 1) | 1;
  2808. /* Clear interrupt # */
  2809. pcicr = (pcicr << 1) | 1;
  2810. /* Set channel to physical mode */
  2811. prmseo[i % 2] = prmseo[i % 2] << 2;
  2812. prmseo[i % 2] |= 1;
  2813. }
  2814. writel(prmseo[1], base->virtbase + D40_DREG_PRMSE);
  2815. writel(prmseo[0], base->virtbase + D40_DREG_PRMSO);
  2816. writel(activeo[1], base->virtbase + D40_DREG_ACTIVE);
  2817. writel(activeo[0], base->virtbase + D40_DREG_ACTIVO);
  2818. /* Write which interrupt to enable */
  2819. writel(pcmis, base->virtbase + base->gen_dmac.interrupt_en);
  2820. /* Write which interrupt to clear */
  2821. writel(pcicr, base->virtbase + base->gen_dmac.interrupt_clear);
  2822. /* These are __initdata and cannot be accessed after init */
  2823. base->gen_dmac.init_reg = NULL;
  2824. base->gen_dmac.init_reg_size = 0;
  2825. }
  2826. static int __init d40_lcla_allocate(struct d40_base *base)
  2827. {
  2828. struct d40_lcla_pool *pool = &base->lcla_pool;
  2829. unsigned long *page_list;
  2830. int i, j;
  2831. int ret = 0;
  2832. /*
  2833. * This is somewhat ugly. We need 8192 bytes that are 18 bit aligned,
  2834. * To full fill this hardware requirement without wasting 256 kb
  2835. * we allocate pages until we get an aligned one.
  2836. */
  2837. page_list = kmalloc(sizeof(unsigned long) * MAX_LCLA_ALLOC_ATTEMPTS,
  2838. GFP_KERNEL);
  2839. if (!page_list) {
  2840. ret = -ENOMEM;
  2841. goto failure;
  2842. }
  2843. /* Calculating how many pages that are required */
  2844. base->lcla_pool.pages = SZ_1K * base->num_phy_chans / PAGE_SIZE;
  2845. for (i = 0; i < MAX_LCLA_ALLOC_ATTEMPTS; i++) {
  2846. page_list[i] = __get_free_pages(GFP_KERNEL,
  2847. base->lcla_pool.pages);
  2848. if (!page_list[i]) {
  2849. d40_err(base->dev, "Failed to allocate %d pages.\n",
  2850. base->lcla_pool.pages);
  2851. ret = -ENOMEM;
  2852. for (j = 0; j < i; j++)
  2853. free_pages(page_list[j], base->lcla_pool.pages);
  2854. goto failure;
  2855. }
  2856. if ((virt_to_phys((void *)page_list[i]) &
  2857. (LCLA_ALIGNMENT - 1)) == 0)
  2858. break;
  2859. }
  2860. for (j = 0; j < i; j++)
  2861. free_pages(page_list[j], base->lcla_pool.pages);
  2862. if (i < MAX_LCLA_ALLOC_ATTEMPTS) {
  2863. base->lcla_pool.base = (void *)page_list[i];
  2864. } else {
  2865. /*
  2866. * After many attempts and no succees with finding the correct
  2867. * alignment, try with allocating a big buffer.
  2868. */
  2869. dev_warn(base->dev,
  2870. "[%s] Failed to get %d pages @ 18 bit align.\n",
  2871. __func__, base->lcla_pool.pages);
  2872. base->lcla_pool.base_unaligned = kmalloc(SZ_1K *
  2873. base->num_phy_chans +
  2874. LCLA_ALIGNMENT,
  2875. GFP_KERNEL);
  2876. if (!base->lcla_pool.base_unaligned) {
  2877. ret = -ENOMEM;
  2878. goto failure;
  2879. }
  2880. base->lcla_pool.base = PTR_ALIGN(base->lcla_pool.base_unaligned,
  2881. LCLA_ALIGNMENT);
  2882. }
  2883. pool->dma_addr = dma_map_single(base->dev, pool->base,
  2884. SZ_1K * base->num_phy_chans,
  2885. DMA_TO_DEVICE);
  2886. if (dma_mapping_error(base->dev, pool->dma_addr)) {
  2887. pool->dma_addr = 0;
  2888. ret = -ENOMEM;
  2889. goto failure;
  2890. }
  2891. writel(virt_to_phys(base->lcla_pool.base),
  2892. base->virtbase + D40_DREG_LCLA);
  2893. failure:
  2894. kfree(page_list);
  2895. return ret;
  2896. }
  2897. static int __init d40_of_probe(struct platform_device *pdev,
  2898. struct device_node *np)
  2899. {
  2900. struct stedma40_platform_data *pdata;
  2901. int num_phy = 0, num_memcpy = 0, num_disabled = 0;
  2902. const __be32 *list;
  2903. pdata = devm_kzalloc(&pdev->dev,
  2904. sizeof(struct stedma40_platform_data),
  2905. GFP_KERNEL);
  2906. if (!pdata)
  2907. return -ENOMEM;
  2908. /* If absent this value will be obtained from h/w. */
  2909. of_property_read_u32(np, "dma-channels", &num_phy);
  2910. if (num_phy > 0)
  2911. pdata->num_of_phy_chans = num_phy;
  2912. list = of_get_property(np, "memcpy-channels", &num_memcpy);
  2913. num_memcpy /= sizeof(*list);
  2914. if (num_memcpy > D40_MEMCPY_MAX_CHANS || num_memcpy <= 0) {
  2915. d40_err(&pdev->dev,
  2916. "Invalid number of memcpy channels specified (%d)\n",
  2917. num_memcpy);
  2918. return -EINVAL;
  2919. }
  2920. pdata->num_of_memcpy_chans = num_memcpy;
  2921. of_property_read_u32_array(np, "memcpy-channels",
  2922. dma40_memcpy_channels,
  2923. num_memcpy);
  2924. list = of_get_property(np, "disabled-channels", &num_disabled);
  2925. num_disabled /= sizeof(*list);
  2926. if (num_disabled >= STEDMA40_MAX_PHYS || num_disabled < 0) {
  2927. d40_err(&pdev->dev,
  2928. "Invalid number of disabled channels specified (%d)\n",
  2929. num_disabled);
  2930. return -EINVAL;
  2931. }
  2932. of_property_read_u32_array(np, "disabled-channels",
  2933. pdata->disabled_channels,
  2934. num_disabled);
  2935. pdata->disabled_channels[num_disabled] = -1;
  2936. pdev->dev.platform_data = pdata;
  2937. return 0;
  2938. }
  2939. static int __init d40_probe(struct platform_device *pdev)
  2940. {
  2941. struct stedma40_platform_data *plat_data = dev_get_platdata(&pdev->dev);
  2942. struct device_node *np = pdev->dev.of_node;
  2943. int ret = -ENOENT;
  2944. struct d40_base *base = NULL;
  2945. struct resource *res = NULL;
  2946. int num_reserved_chans;
  2947. u32 val;
  2948. if (!plat_data) {
  2949. if (np) {
  2950. if (d40_of_probe(pdev, np)) {
  2951. ret = -ENOMEM;
  2952. goto failure;
  2953. }
  2954. } else {
  2955. d40_err(&pdev->dev, "No pdata or Device Tree provided\n");
  2956. goto failure;
  2957. }
  2958. }
  2959. base = d40_hw_detect_init(pdev);
  2960. if (!base)
  2961. goto failure;
  2962. num_reserved_chans = d40_phy_res_init(base);
  2963. platform_set_drvdata(pdev, base);
  2964. spin_lock_init(&base->interrupt_lock);
  2965. spin_lock_init(&base->execmd_lock);
  2966. /* Get IO for logical channel parameter address */
  2967. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
  2968. if (!res) {
  2969. ret = -ENOENT;
  2970. d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
  2971. goto failure;
  2972. }
  2973. base->lcpa_size = resource_size(res);
  2974. base->phy_lcpa = res->start;
  2975. if (request_mem_region(res->start, resource_size(res),
  2976. D40_NAME " I/O lcpa") == NULL) {
  2977. ret = -EBUSY;
  2978. d40_err(&pdev->dev, "Failed to request LCPA region %pR\n", res);
  2979. goto failure;
  2980. }
  2981. /* We make use of ESRAM memory for this. */
  2982. val = readl(base->virtbase + D40_DREG_LCPA);
  2983. if (res->start != val && val != 0) {
  2984. dev_warn(&pdev->dev,
  2985. "[%s] Mismatch LCPA dma 0x%x, def %pa\n",
  2986. __func__, val, &res->start);
  2987. } else
  2988. writel(res->start, base->virtbase + D40_DREG_LCPA);
  2989. base->lcpa_base = ioremap(res->start, resource_size(res));
  2990. if (!base->lcpa_base) {
  2991. ret = -ENOMEM;
  2992. d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
  2993. goto failure;
  2994. }
  2995. /* If lcla has to be located in ESRAM we don't need to allocate */
  2996. if (base->plat_data->use_esram_lcla) {
  2997. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2998. "lcla_esram");
  2999. if (!res) {
  3000. ret = -ENOENT;
  3001. d40_err(&pdev->dev,
  3002. "No \"lcla_esram\" memory resource\n");
  3003. goto failure;
  3004. }
  3005. base->lcla_pool.base = ioremap(res->start,
  3006. resource_size(res));
  3007. if (!base->lcla_pool.base) {
  3008. ret = -ENOMEM;
  3009. d40_err(&pdev->dev, "Failed to ioremap LCLA region\n");
  3010. goto failure;
  3011. }
  3012. writel(res->start, base->virtbase + D40_DREG_LCLA);
  3013. } else {
  3014. ret = d40_lcla_allocate(base);
  3015. if (ret) {
  3016. d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
  3017. goto failure;
  3018. }
  3019. }
  3020. spin_lock_init(&base->lcla_pool.lock);
  3021. base->irq = platform_get_irq(pdev, 0);
  3022. ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
  3023. if (ret) {
  3024. d40_err(&pdev->dev, "No IRQ defined\n");
  3025. goto failure;
  3026. }
  3027. if (base->plat_data->use_esram_lcla) {
  3028. base->lcpa_regulator = regulator_get(base->dev, "lcla_esram");
  3029. if (IS_ERR(base->lcpa_regulator)) {
  3030. d40_err(&pdev->dev, "Failed to get lcpa_regulator\n");
  3031. ret = PTR_ERR(base->lcpa_regulator);
  3032. base->lcpa_regulator = NULL;
  3033. goto failure;
  3034. }
  3035. ret = regulator_enable(base->lcpa_regulator);
  3036. if (ret) {
  3037. d40_err(&pdev->dev,
  3038. "Failed to enable lcpa_regulator\n");
  3039. regulator_put(base->lcpa_regulator);
  3040. base->lcpa_regulator = NULL;
  3041. goto failure;
  3042. }
  3043. }
  3044. writel_relaxed(D40_DREG_GCC_ENABLE_ALL, base->virtbase + D40_DREG_GCC);
  3045. pm_runtime_irq_safe(base->dev);
  3046. pm_runtime_set_autosuspend_delay(base->dev, DMA40_AUTOSUSPEND_DELAY);
  3047. pm_runtime_use_autosuspend(base->dev);
  3048. pm_runtime_mark_last_busy(base->dev);
  3049. pm_runtime_set_active(base->dev);
  3050. pm_runtime_enable(base->dev);
  3051. ret = d40_dmaengine_init(base, num_reserved_chans);
  3052. if (ret)
  3053. goto failure;
  3054. base->dev->dma_parms = &base->dma_parms;
  3055. ret = dma_set_max_seg_size(base->dev, STEDMA40_MAX_SEG_SIZE);
  3056. if (ret) {
  3057. d40_err(&pdev->dev, "Failed to set dma max seg size\n");
  3058. goto failure;
  3059. }
  3060. d40_hw_init(base);
  3061. if (np) {
  3062. ret = of_dma_controller_register(np, d40_xlate, NULL);
  3063. if (ret)
  3064. dev_err(&pdev->dev,
  3065. "could not register of_dma_controller\n");
  3066. }
  3067. dev_info(base->dev, "initialized\n");
  3068. return 0;
  3069. failure:
  3070. if (base) {
  3071. if (base->desc_slab)
  3072. kmem_cache_destroy(base->desc_slab);
  3073. if (base->virtbase)
  3074. iounmap(base->virtbase);
  3075. if (base->lcla_pool.base && base->plat_data->use_esram_lcla) {
  3076. iounmap(base->lcla_pool.base);
  3077. base->lcla_pool.base = NULL;
  3078. }
  3079. if (base->lcla_pool.dma_addr)
  3080. dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
  3081. SZ_1K * base->num_phy_chans,
  3082. DMA_TO_DEVICE);
  3083. if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
  3084. free_pages((unsigned long)base->lcla_pool.base,
  3085. base->lcla_pool.pages);
  3086. kfree(base->lcla_pool.base_unaligned);
  3087. if (base->phy_lcpa)
  3088. release_mem_region(base->phy_lcpa,
  3089. base->lcpa_size);
  3090. if (base->phy_start)
  3091. release_mem_region(base->phy_start,
  3092. base->phy_size);
  3093. if (base->clk) {
  3094. clk_disable_unprepare(base->clk);
  3095. clk_put(base->clk);
  3096. }
  3097. if (base->lcpa_regulator) {
  3098. regulator_disable(base->lcpa_regulator);
  3099. regulator_put(base->lcpa_regulator);
  3100. }
  3101. kfree(base->lcla_pool.alloc_map);
  3102. kfree(base->lookup_log_chans);
  3103. kfree(base->lookup_phy_chans);
  3104. kfree(base->phy_res);
  3105. kfree(base);
  3106. }
  3107. d40_err(&pdev->dev, "probe failed\n");
  3108. return ret;
  3109. }
  3110. static const struct of_device_id d40_match[] = {
  3111. { .compatible = "stericsson,dma40", },
  3112. {}
  3113. };
  3114. static struct platform_driver d40_driver = {
  3115. .driver = {
  3116. .name = D40_NAME,
  3117. .pm = &dma40_pm_ops,
  3118. .of_match_table = d40_match,
  3119. },
  3120. };
  3121. static int __init stedma40_init(void)
  3122. {
  3123. return platform_driver_probe(&d40_driver, d40_probe);
  3124. }
  3125. subsys_initcall(stedma40_init);