ste_dma40.c 95 KB

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