ste_dma40.c 96 KB

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