ste_dma40.c 96 KB

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