xilinx_dma.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DMA driver for Xilinx Video DMA Engine
  4. *
  5. * Copyright (C) 2010-2014 Xilinx, Inc. All rights reserved.
  6. *
  7. * Based on the Freescale DMA driver.
  8. *
  9. * Description:
  10. * The AXI Video Direct Memory Access (AXI VDMA) core is a soft Xilinx IP
  11. * core that provides high-bandwidth direct memory access between memory
  12. * and AXI4-Stream type video target peripherals. The core provides efficient
  13. * two dimensional DMA operations with independent asynchronous read (S2MM)
  14. * and write (MM2S) channel operation. It can be configured to have either
  15. * one channel or two channels. If configured as two channels, one is to
  16. * transmit to the video device (MM2S) and another is to receive from the
  17. * video device (S2MM). Initialization, status, interrupt and management
  18. * registers are accessed through an AXI4-Lite slave interface.
  19. *
  20. * The AXI Direct Memory Access (AXI DMA) core is a soft Xilinx IP core that
  21. * provides high-bandwidth one dimensional direct memory access between memory
  22. * and AXI4-Stream target peripherals. It supports one receive and one
  23. * transmit channel, both of them optional at synthesis time.
  24. *
  25. * The AXI CDMA, is a soft IP, which provides high-bandwidth Direct Memory
  26. * Access (DMA) between a memory-mapped source address and a memory-mapped
  27. * destination address.
  28. *
  29. * The AXI Multichannel Direct Memory Access (AXI MCDMA) core is a soft
  30. * Xilinx IP that provides high-bandwidth direct memory access between
  31. * memory and AXI4-Stream target peripherals. It provides scatter gather
  32. * (SG) interface with multiple channels independent configuration support.
  33. *
  34. */
  35. #include <linux/bitops.h>
  36. #include <linux/dmapool.h>
  37. #include <linux/dma/xilinx_dma.h>
  38. #include <linux/init.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/io.h>
  41. #include <linux/iopoll.h>
  42. #include <linux/module.h>
  43. #include <linux/of_address.h>
  44. #include <linux/of_dma.h>
  45. #include <linux/of_platform.h>
  46. #include <linux/of_irq.h>
  47. #include <linux/slab.h>
  48. #include <linux/clk.h>
  49. #include <linux/io-64-nonatomic-lo-hi.h>
  50. #include "../dmaengine.h"
  51. /* Register/Descriptor Offsets */
  52. #define XILINX_DMA_MM2S_CTRL_OFFSET 0x0000
  53. #define XILINX_DMA_S2MM_CTRL_OFFSET 0x0030
  54. #define XILINX_VDMA_MM2S_DESC_OFFSET 0x0050
  55. #define XILINX_VDMA_S2MM_DESC_OFFSET 0x00a0
  56. /* Control Registers */
  57. #define XILINX_DMA_REG_DMACR 0x0000
  58. #define XILINX_DMA_DMACR_DELAY_MAX 0xff
  59. #define XILINX_DMA_DMACR_DELAY_SHIFT 24
  60. #define XILINX_DMA_DMACR_FRAME_COUNT_MAX 0xff
  61. #define XILINX_DMA_DMACR_FRAME_COUNT_SHIFT 16
  62. #define XILINX_DMA_DMACR_ERR_IRQ BIT(14)
  63. #define XILINX_DMA_DMACR_DLY_CNT_IRQ BIT(13)
  64. #define XILINX_DMA_DMACR_FRM_CNT_IRQ BIT(12)
  65. #define XILINX_DMA_DMACR_MASTER_SHIFT 8
  66. #define XILINX_DMA_DMACR_FSYNCSRC_SHIFT 5
  67. #define XILINX_DMA_DMACR_FRAMECNT_EN BIT(4)
  68. #define XILINX_DMA_DMACR_GENLOCK_EN BIT(3)
  69. #define XILINX_DMA_DMACR_RESET BIT(2)
  70. #define XILINX_DMA_DMACR_CIRC_EN BIT(1)
  71. #define XILINX_DMA_DMACR_RUNSTOP BIT(0)
  72. #define XILINX_DMA_DMACR_FSYNCSRC_MASK GENMASK(6, 5)
  73. #define XILINX_DMA_DMACR_DELAY_MASK GENMASK(31, 24)
  74. #define XILINX_DMA_DMACR_FRAME_COUNT_MASK GENMASK(23, 16)
  75. #define XILINX_DMA_DMACR_MASTER_MASK GENMASK(11, 8)
  76. #define XILINX_DMA_REG_DMASR 0x0004
  77. #define XILINX_DMA_DMASR_EOL_LATE_ERR BIT(15)
  78. #define XILINX_DMA_DMASR_ERR_IRQ BIT(14)
  79. #define XILINX_DMA_DMASR_DLY_CNT_IRQ BIT(13)
  80. #define XILINX_DMA_DMASR_FRM_CNT_IRQ BIT(12)
  81. #define XILINX_DMA_DMASR_SOF_LATE_ERR BIT(11)
  82. #define XILINX_DMA_DMASR_SG_DEC_ERR BIT(10)
  83. #define XILINX_DMA_DMASR_SG_SLV_ERR BIT(9)
  84. #define XILINX_DMA_DMASR_EOF_EARLY_ERR BIT(8)
  85. #define XILINX_DMA_DMASR_SOF_EARLY_ERR BIT(7)
  86. #define XILINX_DMA_DMASR_DMA_DEC_ERR BIT(6)
  87. #define XILINX_DMA_DMASR_DMA_SLAVE_ERR BIT(5)
  88. #define XILINX_DMA_DMASR_DMA_INT_ERR BIT(4)
  89. #define XILINX_DMA_DMASR_SG_MASK BIT(3)
  90. #define XILINX_DMA_DMASR_IDLE BIT(1)
  91. #define XILINX_DMA_DMASR_HALTED BIT(0)
  92. #define XILINX_DMA_DMASR_DELAY_MASK GENMASK(31, 24)
  93. #define XILINX_DMA_DMASR_FRAME_COUNT_MASK GENMASK(23, 16)
  94. #define XILINX_DMA_REG_CURDESC 0x0008
  95. #define XILINX_DMA_REG_TAILDESC 0x0010
  96. #define XILINX_DMA_REG_REG_INDEX 0x0014
  97. #define XILINX_DMA_REG_FRMSTORE 0x0018
  98. #define XILINX_DMA_REG_THRESHOLD 0x001c
  99. #define XILINX_DMA_REG_FRMPTR_STS 0x0024
  100. #define XILINX_DMA_REG_PARK_PTR 0x0028
  101. #define XILINX_DMA_PARK_PTR_WR_REF_SHIFT 8
  102. #define XILINX_DMA_PARK_PTR_WR_REF_MASK GENMASK(12, 8)
  103. #define XILINX_DMA_PARK_PTR_RD_REF_SHIFT 0
  104. #define XILINX_DMA_PARK_PTR_RD_REF_MASK GENMASK(4, 0)
  105. #define XILINX_DMA_REG_VDMA_VERSION 0x002c
  106. /* Register Direct Mode Registers */
  107. #define XILINX_DMA_REG_VSIZE 0x0000
  108. #define XILINX_DMA_REG_HSIZE 0x0004
  109. #define XILINX_DMA_REG_FRMDLY_STRIDE 0x0008
  110. #define XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT 24
  111. #define XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT 0
  112. #define XILINX_VDMA_REG_START_ADDRESS(n) (0x000c + 4 * (n))
  113. #define XILINX_VDMA_REG_START_ADDRESS_64(n) (0x000c + 8 * (n))
  114. #define XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP 0x00ec
  115. #define XILINX_VDMA_ENABLE_VERTICAL_FLIP BIT(0)
  116. /* HW specific definitions */
  117. #define XILINX_MCDMA_MAX_CHANS_PER_DEVICE 0x20
  118. #define XILINX_DMA_MAX_CHANS_PER_DEVICE 0x2
  119. #define XILINX_CDMA_MAX_CHANS_PER_DEVICE 0x1
  120. #define XILINX_DMA_DMAXR_ALL_IRQ_MASK \
  121. (XILINX_DMA_DMASR_FRM_CNT_IRQ | \
  122. XILINX_DMA_DMASR_DLY_CNT_IRQ | \
  123. XILINX_DMA_DMASR_ERR_IRQ)
  124. #define XILINX_DMA_DMASR_ALL_ERR_MASK \
  125. (XILINX_DMA_DMASR_EOL_LATE_ERR | \
  126. XILINX_DMA_DMASR_SOF_LATE_ERR | \
  127. XILINX_DMA_DMASR_SG_DEC_ERR | \
  128. XILINX_DMA_DMASR_SG_SLV_ERR | \
  129. XILINX_DMA_DMASR_EOF_EARLY_ERR | \
  130. XILINX_DMA_DMASR_SOF_EARLY_ERR | \
  131. XILINX_DMA_DMASR_DMA_DEC_ERR | \
  132. XILINX_DMA_DMASR_DMA_SLAVE_ERR | \
  133. XILINX_DMA_DMASR_DMA_INT_ERR)
  134. /*
  135. * Recoverable errors are DMA Internal error, SOF Early, EOF Early
  136. * and SOF Late. They are only recoverable when C_FLUSH_ON_FSYNC
  137. * is enabled in the h/w system.
  138. */
  139. #define XILINX_DMA_DMASR_ERR_RECOVER_MASK \
  140. (XILINX_DMA_DMASR_SOF_LATE_ERR | \
  141. XILINX_DMA_DMASR_EOF_EARLY_ERR | \
  142. XILINX_DMA_DMASR_SOF_EARLY_ERR | \
  143. XILINX_DMA_DMASR_DMA_INT_ERR)
  144. /* Axi VDMA Flush on Fsync bits */
  145. #define XILINX_DMA_FLUSH_S2MM 3
  146. #define XILINX_DMA_FLUSH_MM2S 2
  147. #define XILINX_DMA_FLUSH_BOTH 1
  148. /* Delay loop counter to prevent hardware failure */
  149. #define XILINX_DMA_LOOP_COUNT 1000000
  150. /* AXI DMA Specific Registers/Offsets */
  151. #define XILINX_DMA_REG_SRCDSTADDR 0x18
  152. #define XILINX_DMA_REG_BTT 0x28
  153. /* AXI DMA Specific Masks/Bit fields */
  154. #define XILINX_DMA_MAX_TRANS_LEN_MIN 8
  155. #define XILINX_DMA_MAX_TRANS_LEN_MAX 23
  156. #define XILINX_DMA_V2_MAX_TRANS_LEN_MAX 26
  157. #define XILINX_DMA_CR_COALESCE_MAX GENMASK(23, 16)
  158. #define XILINX_DMA_CR_CYCLIC_BD_EN_MASK BIT(4)
  159. #define XILINX_DMA_CR_COALESCE_SHIFT 16
  160. #define XILINX_DMA_BD_SOP BIT(27)
  161. #define XILINX_DMA_BD_EOP BIT(26)
  162. #define XILINX_DMA_COALESCE_MAX 255
  163. #define XILINX_DMA_NUM_DESCS 255
  164. #define XILINX_DMA_NUM_APP_WORDS 5
  165. /* AXI CDMA Specific Registers/Offsets */
  166. #define XILINX_CDMA_REG_SRCADDR 0x18
  167. #define XILINX_CDMA_REG_DSTADDR 0x20
  168. /* AXI CDMA Specific Masks */
  169. #define XILINX_CDMA_CR_SGMODE BIT(3)
  170. #define xilinx_prep_dma_addr_t(addr) \
  171. ((dma_addr_t)((u64)addr##_##msb << 32 | (addr)))
  172. /* AXI MCDMA Specific Registers/Offsets */
  173. #define XILINX_MCDMA_MM2S_CTRL_OFFSET 0x0000
  174. #define XILINX_MCDMA_S2MM_CTRL_OFFSET 0x0500
  175. #define XILINX_MCDMA_CHEN_OFFSET 0x0008
  176. #define XILINX_MCDMA_CH_ERR_OFFSET 0x0010
  177. #define XILINX_MCDMA_RXINT_SER_OFFSET 0x0020
  178. #define XILINX_MCDMA_TXINT_SER_OFFSET 0x0028
  179. #define XILINX_MCDMA_CHAN_CR_OFFSET(x) (0x40 + (x) * 0x40)
  180. #define XILINX_MCDMA_CHAN_SR_OFFSET(x) (0x44 + (x) * 0x40)
  181. #define XILINX_MCDMA_CHAN_CDESC_OFFSET(x) (0x48 + (x) * 0x40)
  182. #define XILINX_MCDMA_CHAN_TDESC_OFFSET(x) (0x50 + (x) * 0x40)
  183. /* AXI MCDMA Specific Masks/Shifts */
  184. #define XILINX_MCDMA_COALESCE_SHIFT 16
  185. #define XILINX_MCDMA_COALESCE_MAX 24
  186. #define XILINX_MCDMA_IRQ_ALL_MASK GENMASK(7, 5)
  187. #define XILINX_MCDMA_COALESCE_MASK GENMASK(23, 16)
  188. #define XILINX_MCDMA_CR_RUNSTOP_MASK BIT(0)
  189. #define XILINX_MCDMA_IRQ_IOC_MASK BIT(5)
  190. #define XILINX_MCDMA_IRQ_DELAY_MASK BIT(6)
  191. #define XILINX_MCDMA_IRQ_ERR_MASK BIT(7)
  192. #define XILINX_MCDMA_BD_EOP BIT(30)
  193. #define XILINX_MCDMA_BD_SOP BIT(31)
  194. /**
  195. * struct xilinx_vdma_desc_hw - Hardware Descriptor
  196. * @next_desc: Next Descriptor Pointer @0x00
  197. * @pad1: Reserved @0x04
  198. * @buf_addr: Buffer address @0x08
  199. * @buf_addr_msb: MSB of Buffer address @0x0C
  200. * @vsize: Vertical Size @0x10
  201. * @hsize: Horizontal Size @0x14
  202. * @stride: Number of bytes between the first
  203. * pixels of each horizontal line @0x18
  204. */
  205. struct xilinx_vdma_desc_hw {
  206. u32 next_desc;
  207. u32 pad1;
  208. u32 buf_addr;
  209. u32 buf_addr_msb;
  210. u32 vsize;
  211. u32 hsize;
  212. u32 stride;
  213. } __aligned(64);
  214. /**
  215. * struct xilinx_axidma_desc_hw - Hardware Descriptor for AXI DMA
  216. * @next_desc: Next Descriptor Pointer @0x00
  217. * @next_desc_msb: MSB of Next Descriptor Pointer @0x04
  218. * @buf_addr: Buffer address @0x08
  219. * @buf_addr_msb: MSB of Buffer address @0x0C
  220. * @reserved1: Reserved @0x10
  221. * @reserved2: Reserved @0x14
  222. * @control: Control field @0x18
  223. * @status: Status field @0x1C
  224. * @app: APP Fields @0x20 - 0x30
  225. */
  226. struct xilinx_axidma_desc_hw {
  227. u32 next_desc;
  228. u32 next_desc_msb;
  229. u32 buf_addr;
  230. u32 buf_addr_msb;
  231. u32 reserved1;
  232. u32 reserved2;
  233. u32 control;
  234. u32 status;
  235. u32 app[XILINX_DMA_NUM_APP_WORDS];
  236. } __aligned(64);
  237. /**
  238. * struct xilinx_aximcdma_desc_hw - Hardware Descriptor for AXI MCDMA
  239. * @next_desc: Next Descriptor Pointer @0x00
  240. * @next_desc_msb: MSB of Next Descriptor Pointer @0x04
  241. * @buf_addr: Buffer address @0x08
  242. * @buf_addr_msb: MSB of Buffer address @0x0C
  243. * @rsvd: Reserved field @0x10
  244. * @control: Control Information field @0x14
  245. * @status: Status field @0x18
  246. * @sideband_status: Status of sideband signals @0x1C
  247. * @app: APP Fields @0x20 - 0x30
  248. */
  249. struct xilinx_aximcdma_desc_hw {
  250. u32 next_desc;
  251. u32 next_desc_msb;
  252. u32 buf_addr;
  253. u32 buf_addr_msb;
  254. u32 rsvd;
  255. u32 control;
  256. u32 status;
  257. u32 sideband_status;
  258. u32 app[XILINX_DMA_NUM_APP_WORDS];
  259. } __aligned(64);
  260. /**
  261. * struct xilinx_cdma_desc_hw - Hardware Descriptor
  262. * @next_desc: Next Descriptor Pointer @0x00
  263. * @next_desc_msb: Next Descriptor Pointer MSB @0x04
  264. * @src_addr: Source address @0x08
  265. * @src_addr_msb: Source address MSB @0x0C
  266. * @dest_addr: Destination address @0x10
  267. * @dest_addr_msb: Destination address MSB @0x14
  268. * @control: Control field @0x18
  269. * @status: Status field @0x1C
  270. */
  271. struct xilinx_cdma_desc_hw {
  272. u32 next_desc;
  273. u32 next_desc_msb;
  274. u32 src_addr;
  275. u32 src_addr_msb;
  276. u32 dest_addr;
  277. u32 dest_addr_msb;
  278. u32 control;
  279. u32 status;
  280. } __aligned(64);
  281. /**
  282. * struct xilinx_vdma_tx_segment - Descriptor segment
  283. * @hw: Hardware descriptor
  284. * @node: Node in the descriptor segments list
  285. * @phys: Physical address of segment
  286. */
  287. struct xilinx_vdma_tx_segment {
  288. struct xilinx_vdma_desc_hw hw;
  289. struct list_head node;
  290. dma_addr_t phys;
  291. } __aligned(64);
  292. /**
  293. * struct xilinx_axidma_tx_segment - Descriptor segment
  294. * @hw: Hardware descriptor
  295. * @node: Node in the descriptor segments list
  296. * @phys: Physical address of segment
  297. */
  298. struct xilinx_axidma_tx_segment {
  299. struct xilinx_axidma_desc_hw hw;
  300. struct list_head node;
  301. dma_addr_t phys;
  302. } __aligned(64);
  303. /**
  304. * struct xilinx_aximcdma_tx_segment - Descriptor segment
  305. * @hw: Hardware descriptor
  306. * @node: Node in the descriptor segments list
  307. * @phys: Physical address of segment
  308. */
  309. struct xilinx_aximcdma_tx_segment {
  310. struct xilinx_aximcdma_desc_hw hw;
  311. struct list_head node;
  312. dma_addr_t phys;
  313. } __aligned(64);
  314. /**
  315. * struct xilinx_cdma_tx_segment - Descriptor segment
  316. * @hw: Hardware descriptor
  317. * @node: Node in the descriptor segments list
  318. * @phys: Physical address of segment
  319. */
  320. struct xilinx_cdma_tx_segment {
  321. struct xilinx_cdma_desc_hw hw;
  322. struct list_head node;
  323. dma_addr_t phys;
  324. } __aligned(64);
  325. /**
  326. * struct xilinx_dma_tx_descriptor - Per Transaction structure
  327. * @async_tx: Async transaction descriptor
  328. * @segments: TX segments list
  329. * @node: Node in the channel descriptors list
  330. * @cyclic: Check for cyclic transfers.
  331. * @err: Whether the descriptor has an error.
  332. * @residue: Residue of the completed descriptor
  333. */
  334. struct xilinx_dma_tx_descriptor {
  335. struct dma_async_tx_descriptor async_tx;
  336. struct list_head segments;
  337. struct list_head node;
  338. bool cyclic;
  339. bool err;
  340. u32 residue;
  341. };
  342. /**
  343. * struct xilinx_dma_chan - Driver specific DMA channel structure
  344. * @xdev: Driver specific device structure
  345. * @ctrl_offset: Control registers offset
  346. * @desc_offset: TX descriptor registers offset
  347. * @lock: Descriptor operation lock
  348. * @pending_list: Descriptors waiting
  349. * @active_list: Descriptors ready to submit
  350. * @done_list: Complete descriptors
  351. * @free_seg_list: Free descriptors
  352. * @common: DMA common channel
  353. * @desc_pool: Descriptors pool
  354. * @dev: The dma device
  355. * @irq: Channel IRQ
  356. * @id: Channel ID
  357. * @direction: Transfer direction
  358. * @num_frms: Number of frames
  359. * @has_sg: Support scatter transfers
  360. * @cyclic: Check for cyclic transfers.
  361. * @genlock: Support genlock mode
  362. * @err: Channel has errors
  363. * @idle: Check for channel idle
  364. * @terminating: Check for channel being synchronized by user
  365. * @tasklet: Cleanup work after irq
  366. * @config: Device configuration info
  367. * @flush_on_fsync: Flush on Frame sync
  368. * @desc_pendingcount: Descriptor pending count
  369. * @ext_addr: Indicates 64 bit addressing is supported by dma channel
  370. * @desc_submitcount: Descriptor h/w submitted count
  371. * @seg_v: Statically allocated segments base
  372. * @seg_mv: Statically allocated segments base for MCDMA
  373. * @seg_p: Physical allocated segments base
  374. * @cyclic_seg_v: Statically allocated segment base for cyclic transfers
  375. * @cyclic_seg_p: Physical allocated segments base for cyclic dma
  376. * @start_transfer: Differentiate b/w DMA IP's transfer
  377. * @stop_transfer: Differentiate b/w DMA IP's quiesce
  378. * @tdest: TDEST value for mcdma
  379. * @has_vflip: S2MM vertical flip
  380. */
  381. struct xilinx_dma_chan {
  382. struct xilinx_dma_device *xdev;
  383. u32 ctrl_offset;
  384. u32 desc_offset;
  385. spinlock_t lock;
  386. struct list_head pending_list;
  387. struct list_head active_list;
  388. struct list_head done_list;
  389. struct list_head free_seg_list;
  390. struct dma_chan common;
  391. struct dma_pool *desc_pool;
  392. struct device *dev;
  393. int irq;
  394. int id;
  395. enum dma_transfer_direction direction;
  396. int num_frms;
  397. bool has_sg;
  398. bool cyclic;
  399. bool genlock;
  400. bool err;
  401. bool idle;
  402. bool terminating;
  403. struct tasklet_struct tasklet;
  404. struct xilinx_vdma_config config;
  405. bool flush_on_fsync;
  406. u32 desc_pendingcount;
  407. bool ext_addr;
  408. u32 desc_submitcount;
  409. struct xilinx_axidma_tx_segment *seg_v;
  410. struct xilinx_aximcdma_tx_segment *seg_mv;
  411. dma_addr_t seg_p;
  412. struct xilinx_axidma_tx_segment *cyclic_seg_v;
  413. dma_addr_t cyclic_seg_p;
  414. void (*start_transfer)(struct xilinx_dma_chan *chan);
  415. int (*stop_transfer)(struct xilinx_dma_chan *chan);
  416. u16 tdest;
  417. bool has_vflip;
  418. };
  419. /**
  420. * enum xdma_ip_type - DMA IP type.
  421. *
  422. * @XDMA_TYPE_AXIDMA: Axi dma ip.
  423. * @XDMA_TYPE_CDMA: Axi cdma ip.
  424. * @XDMA_TYPE_VDMA: Axi vdma ip.
  425. * @XDMA_TYPE_AXIMCDMA: Axi MCDMA ip.
  426. *
  427. */
  428. enum xdma_ip_type {
  429. XDMA_TYPE_AXIDMA = 0,
  430. XDMA_TYPE_CDMA,
  431. XDMA_TYPE_VDMA,
  432. XDMA_TYPE_AXIMCDMA
  433. };
  434. struct xilinx_dma_config {
  435. enum xdma_ip_type dmatype;
  436. int (*clk_init)(struct platform_device *pdev, struct clk **axi_clk,
  437. struct clk **tx_clk, struct clk **txs_clk,
  438. struct clk **rx_clk, struct clk **rxs_clk);
  439. irqreturn_t (*irq_handler)(int irq, void *data);
  440. const int max_channels;
  441. };
  442. /**
  443. * struct xilinx_dma_device - DMA device structure
  444. * @regs: I/O mapped base address
  445. * @dev: Device Structure
  446. * @common: DMA device structure
  447. * @chan: Driver specific DMA channel
  448. * @flush_on_fsync: Flush on frame sync
  449. * @ext_addr: Indicates 64 bit addressing is supported by dma device
  450. * @pdev: Platform device structure pointer
  451. * @dma_config: DMA config structure
  452. * @axi_clk: DMA Axi4-lite interace clock
  453. * @tx_clk: DMA mm2s clock
  454. * @txs_clk: DMA mm2s stream clock
  455. * @rx_clk: DMA s2mm clock
  456. * @rxs_clk: DMA s2mm stream clock
  457. * @s2mm_chan_id: DMA s2mm channel identifier
  458. * @mm2s_chan_id: DMA mm2s channel identifier
  459. * @max_buffer_len: Max buffer length
  460. */
  461. struct xilinx_dma_device {
  462. void __iomem *regs;
  463. struct device *dev;
  464. struct dma_device common;
  465. struct xilinx_dma_chan *chan[XILINX_MCDMA_MAX_CHANS_PER_DEVICE];
  466. u32 flush_on_fsync;
  467. bool ext_addr;
  468. struct platform_device *pdev;
  469. const struct xilinx_dma_config *dma_config;
  470. struct clk *axi_clk;
  471. struct clk *tx_clk;
  472. struct clk *txs_clk;
  473. struct clk *rx_clk;
  474. struct clk *rxs_clk;
  475. u32 s2mm_chan_id;
  476. u32 mm2s_chan_id;
  477. u32 max_buffer_len;
  478. };
  479. /* Macros */
  480. #define to_xilinx_chan(chan) \
  481. container_of(chan, struct xilinx_dma_chan, common)
  482. #define to_dma_tx_descriptor(tx) \
  483. container_of(tx, struct xilinx_dma_tx_descriptor, async_tx)
  484. #define xilinx_dma_poll_timeout(chan, reg, val, cond, delay_us, timeout_us) \
  485. readl_poll_timeout_atomic(chan->xdev->regs + chan->ctrl_offset + reg, \
  486. val, cond, delay_us, timeout_us)
  487. /* IO accessors */
  488. static inline u32 dma_read(struct xilinx_dma_chan *chan, u32 reg)
  489. {
  490. return ioread32(chan->xdev->regs + reg);
  491. }
  492. static inline void dma_write(struct xilinx_dma_chan *chan, u32 reg, u32 value)
  493. {
  494. iowrite32(value, chan->xdev->regs + reg);
  495. }
  496. static inline void vdma_desc_write(struct xilinx_dma_chan *chan, u32 reg,
  497. u32 value)
  498. {
  499. dma_write(chan, chan->desc_offset + reg, value);
  500. }
  501. static inline u32 dma_ctrl_read(struct xilinx_dma_chan *chan, u32 reg)
  502. {
  503. return dma_read(chan, chan->ctrl_offset + reg);
  504. }
  505. static inline void dma_ctrl_write(struct xilinx_dma_chan *chan, u32 reg,
  506. u32 value)
  507. {
  508. dma_write(chan, chan->ctrl_offset + reg, value);
  509. }
  510. static inline void dma_ctrl_clr(struct xilinx_dma_chan *chan, u32 reg,
  511. u32 clr)
  512. {
  513. dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) & ~clr);
  514. }
  515. static inline void dma_ctrl_set(struct xilinx_dma_chan *chan, u32 reg,
  516. u32 set)
  517. {
  518. dma_ctrl_write(chan, reg, dma_ctrl_read(chan, reg) | set);
  519. }
  520. /**
  521. * vdma_desc_write_64 - 64-bit descriptor write
  522. * @chan: Driver specific VDMA channel
  523. * @reg: Register to write
  524. * @value_lsb: lower address of the descriptor.
  525. * @value_msb: upper address of the descriptor.
  526. *
  527. * Since vdma driver is trying to write to a register offset which is not a
  528. * multiple of 64 bits(ex : 0x5c), we are writing as two separate 32 bits
  529. * instead of a single 64 bit register write.
  530. */
  531. static inline void vdma_desc_write_64(struct xilinx_dma_chan *chan, u32 reg,
  532. u32 value_lsb, u32 value_msb)
  533. {
  534. /* Write the lsb 32 bits*/
  535. writel(value_lsb, chan->xdev->regs + chan->desc_offset + reg);
  536. /* Write the msb 32 bits */
  537. writel(value_msb, chan->xdev->regs + chan->desc_offset + reg + 4);
  538. }
  539. static inline void dma_writeq(struct xilinx_dma_chan *chan, u32 reg, u64 value)
  540. {
  541. lo_hi_writeq(value, chan->xdev->regs + chan->ctrl_offset + reg);
  542. }
  543. static inline void xilinx_write(struct xilinx_dma_chan *chan, u32 reg,
  544. dma_addr_t addr)
  545. {
  546. if (chan->ext_addr)
  547. dma_writeq(chan, reg, addr);
  548. else
  549. dma_ctrl_write(chan, reg, addr);
  550. }
  551. static inline void xilinx_axidma_buf(struct xilinx_dma_chan *chan,
  552. struct xilinx_axidma_desc_hw *hw,
  553. dma_addr_t buf_addr, size_t sg_used,
  554. size_t period_len)
  555. {
  556. if (chan->ext_addr) {
  557. hw->buf_addr = lower_32_bits(buf_addr + sg_used + period_len);
  558. hw->buf_addr_msb = upper_32_bits(buf_addr + sg_used +
  559. period_len);
  560. } else {
  561. hw->buf_addr = buf_addr + sg_used + period_len;
  562. }
  563. }
  564. static inline void xilinx_aximcdma_buf(struct xilinx_dma_chan *chan,
  565. struct xilinx_aximcdma_desc_hw *hw,
  566. dma_addr_t buf_addr, size_t sg_used)
  567. {
  568. if (chan->ext_addr) {
  569. hw->buf_addr = lower_32_bits(buf_addr + sg_used);
  570. hw->buf_addr_msb = upper_32_bits(buf_addr + sg_used);
  571. } else {
  572. hw->buf_addr = buf_addr + sg_used;
  573. }
  574. }
  575. /* -----------------------------------------------------------------------------
  576. * Descriptors and segments alloc and free
  577. */
  578. /**
  579. * xilinx_vdma_alloc_tx_segment - Allocate transaction segment
  580. * @chan: Driver specific DMA channel
  581. *
  582. * Return: The allocated segment on success and NULL on failure.
  583. */
  584. static struct xilinx_vdma_tx_segment *
  585. xilinx_vdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
  586. {
  587. struct xilinx_vdma_tx_segment *segment;
  588. dma_addr_t phys;
  589. segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
  590. if (!segment)
  591. return NULL;
  592. segment->phys = phys;
  593. return segment;
  594. }
  595. /**
  596. * xilinx_cdma_alloc_tx_segment - Allocate transaction segment
  597. * @chan: Driver specific DMA channel
  598. *
  599. * Return: The allocated segment on success and NULL on failure.
  600. */
  601. static struct xilinx_cdma_tx_segment *
  602. xilinx_cdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
  603. {
  604. struct xilinx_cdma_tx_segment *segment;
  605. dma_addr_t phys;
  606. segment = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &phys);
  607. if (!segment)
  608. return NULL;
  609. segment->phys = phys;
  610. return segment;
  611. }
  612. /**
  613. * xilinx_axidma_alloc_tx_segment - Allocate transaction segment
  614. * @chan: Driver specific DMA channel
  615. *
  616. * Return: The allocated segment on success and NULL on failure.
  617. */
  618. static struct xilinx_axidma_tx_segment *
  619. xilinx_axidma_alloc_tx_segment(struct xilinx_dma_chan *chan)
  620. {
  621. struct xilinx_axidma_tx_segment *segment = NULL;
  622. unsigned long flags;
  623. spin_lock_irqsave(&chan->lock, flags);
  624. if (!list_empty(&chan->free_seg_list)) {
  625. segment = list_first_entry(&chan->free_seg_list,
  626. struct xilinx_axidma_tx_segment,
  627. node);
  628. list_del(&segment->node);
  629. }
  630. spin_unlock_irqrestore(&chan->lock, flags);
  631. if (!segment)
  632. dev_dbg(chan->dev, "Could not find free tx segment\n");
  633. return segment;
  634. }
  635. /**
  636. * xilinx_aximcdma_alloc_tx_segment - Allocate transaction segment
  637. * @chan: Driver specific DMA channel
  638. *
  639. * Return: The allocated segment on success and NULL on failure.
  640. */
  641. static struct xilinx_aximcdma_tx_segment *
  642. xilinx_aximcdma_alloc_tx_segment(struct xilinx_dma_chan *chan)
  643. {
  644. struct xilinx_aximcdma_tx_segment *segment = NULL;
  645. unsigned long flags;
  646. spin_lock_irqsave(&chan->lock, flags);
  647. if (!list_empty(&chan->free_seg_list)) {
  648. segment = list_first_entry(&chan->free_seg_list,
  649. struct xilinx_aximcdma_tx_segment,
  650. node);
  651. list_del(&segment->node);
  652. }
  653. spin_unlock_irqrestore(&chan->lock, flags);
  654. return segment;
  655. }
  656. static void xilinx_dma_clean_hw_desc(struct xilinx_axidma_desc_hw *hw)
  657. {
  658. u32 next_desc = hw->next_desc;
  659. u32 next_desc_msb = hw->next_desc_msb;
  660. memset(hw, 0, sizeof(struct xilinx_axidma_desc_hw));
  661. hw->next_desc = next_desc;
  662. hw->next_desc_msb = next_desc_msb;
  663. }
  664. static void xilinx_mcdma_clean_hw_desc(struct xilinx_aximcdma_desc_hw *hw)
  665. {
  666. u32 next_desc = hw->next_desc;
  667. u32 next_desc_msb = hw->next_desc_msb;
  668. memset(hw, 0, sizeof(struct xilinx_aximcdma_desc_hw));
  669. hw->next_desc = next_desc;
  670. hw->next_desc_msb = next_desc_msb;
  671. }
  672. /**
  673. * xilinx_dma_free_tx_segment - Free transaction segment
  674. * @chan: Driver specific DMA channel
  675. * @segment: DMA transaction segment
  676. */
  677. static void xilinx_dma_free_tx_segment(struct xilinx_dma_chan *chan,
  678. struct xilinx_axidma_tx_segment *segment)
  679. {
  680. xilinx_dma_clean_hw_desc(&segment->hw);
  681. list_add_tail(&segment->node, &chan->free_seg_list);
  682. }
  683. /**
  684. * xilinx_mcdma_free_tx_segment - Free transaction segment
  685. * @chan: Driver specific DMA channel
  686. * @segment: DMA transaction segment
  687. */
  688. static void xilinx_mcdma_free_tx_segment(struct xilinx_dma_chan *chan,
  689. struct xilinx_aximcdma_tx_segment *
  690. segment)
  691. {
  692. xilinx_mcdma_clean_hw_desc(&segment->hw);
  693. list_add_tail(&segment->node, &chan->free_seg_list);
  694. }
  695. /**
  696. * xilinx_cdma_free_tx_segment - Free transaction segment
  697. * @chan: Driver specific DMA channel
  698. * @segment: DMA transaction segment
  699. */
  700. static void xilinx_cdma_free_tx_segment(struct xilinx_dma_chan *chan,
  701. struct xilinx_cdma_tx_segment *segment)
  702. {
  703. dma_pool_free(chan->desc_pool, segment, segment->phys);
  704. }
  705. /**
  706. * xilinx_vdma_free_tx_segment - Free transaction segment
  707. * @chan: Driver specific DMA channel
  708. * @segment: DMA transaction segment
  709. */
  710. static void xilinx_vdma_free_tx_segment(struct xilinx_dma_chan *chan,
  711. struct xilinx_vdma_tx_segment *segment)
  712. {
  713. dma_pool_free(chan->desc_pool, segment, segment->phys);
  714. }
  715. /**
  716. * xilinx_dma_alloc_tx_descriptor - Allocate transaction descriptor
  717. * @chan: Driver specific DMA channel
  718. *
  719. * Return: The allocated descriptor on success and NULL on failure.
  720. */
  721. static struct xilinx_dma_tx_descriptor *
  722. xilinx_dma_alloc_tx_descriptor(struct xilinx_dma_chan *chan)
  723. {
  724. struct xilinx_dma_tx_descriptor *desc;
  725. desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
  726. if (!desc)
  727. return NULL;
  728. INIT_LIST_HEAD(&desc->segments);
  729. return desc;
  730. }
  731. /**
  732. * xilinx_dma_free_tx_descriptor - Free transaction descriptor
  733. * @chan: Driver specific DMA channel
  734. * @desc: DMA transaction descriptor
  735. */
  736. static void
  737. xilinx_dma_free_tx_descriptor(struct xilinx_dma_chan *chan,
  738. struct xilinx_dma_tx_descriptor *desc)
  739. {
  740. struct xilinx_vdma_tx_segment *segment, *next;
  741. struct xilinx_cdma_tx_segment *cdma_segment, *cdma_next;
  742. struct xilinx_axidma_tx_segment *axidma_segment, *axidma_next;
  743. struct xilinx_aximcdma_tx_segment *aximcdma_segment, *aximcdma_next;
  744. if (!desc)
  745. return;
  746. if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
  747. list_for_each_entry_safe(segment, next, &desc->segments, node) {
  748. list_del(&segment->node);
  749. xilinx_vdma_free_tx_segment(chan, segment);
  750. }
  751. } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
  752. list_for_each_entry_safe(cdma_segment, cdma_next,
  753. &desc->segments, node) {
  754. list_del(&cdma_segment->node);
  755. xilinx_cdma_free_tx_segment(chan, cdma_segment);
  756. }
  757. } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
  758. list_for_each_entry_safe(axidma_segment, axidma_next,
  759. &desc->segments, node) {
  760. list_del(&axidma_segment->node);
  761. xilinx_dma_free_tx_segment(chan, axidma_segment);
  762. }
  763. } else {
  764. list_for_each_entry_safe(aximcdma_segment, aximcdma_next,
  765. &desc->segments, node) {
  766. list_del(&aximcdma_segment->node);
  767. xilinx_mcdma_free_tx_segment(chan, aximcdma_segment);
  768. }
  769. }
  770. kfree(desc);
  771. }
  772. /* Required functions */
  773. /**
  774. * xilinx_dma_free_desc_list - Free descriptors list
  775. * @chan: Driver specific DMA channel
  776. * @list: List to parse and delete the descriptor
  777. */
  778. static void xilinx_dma_free_desc_list(struct xilinx_dma_chan *chan,
  779. struct list_head *list)
  780. {
  781. struct xilinx_dma_tx_descriptor *desc, *next;
  782. list_for_each_entry_safe(desc, next, list, node) {
  783. list_del(&desc->node);
  784. xilinx_dma_free_tx_descriptor(chan, desc);
  785. }
  786. }
  787. /**
  788. * xilinx_dma_free_descriptors - Free channel descriptors
  789. * @chan: Driver specific DMA channel
  790. */
  791. static void xilinx_dma_free_descriptors(struct xilinx_dma_chan *chan)
  792. {
  793. unsigned long flags;
  794. spin_lock_irqsave(&chan->lock, flags);
  795. xilinx_dma_free_desc_list(chan, &chan->pending_list);
  796. xilinx_dma_free_desc_list(chan, &chan->done_list);
  797. xilinx_dma_free_desc_list(chan, &chan->active_list);
  798. spin_unlock_irqrestore(&chan->lock, flags);
  799. }
  800. /**
  801. * xilinx_dma_free_chan_resources - Free channel resources
  802. * @dchan: DMA channel
  803. */
  804. static void xilinx_dma_free_chan_resources(struct dma_chan *dchan)
  805. {
  806. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  807. unsigned long flags;
  808. dev_dbg(chan->dev, "Free all channel resources.\n");
  809. xilinx_dma_free_descriptors(chan);
  810. if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
  811. spin_lock_irqsave(&chan->lock, flags);
  812. INIT_LIST_HEAD(&chan->free_seg_list);
  813. spin_unlock_irqrestore(&chan->lock, flags);
  814. /* Free memory that is allocated for BD */
  815. dma_free_coherent(chan->dev, sizeof(*chan->seg_v) *
  816. XILINX_DMA_NUM_DESCS, chan->seg_v,
  817. chan->seg_p);
  818. /* Free Memory that is allocated for cyclic DMA Mode */
  819. dma_free_coherent(chan->dev, sizeof(*chan->cyclic_seg_v),
  820. chan->cyclic_seg_v, chan->cyclic_seg_p);
  821. }
  822. if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
  823. spin_lock_irqsave(&chan->lock, flags);
  824. INIT_LIST_HEAD(&chan->free_seg_list);
  825. spin_unlock_irqrestore(&chan->lock, flags);
  826. /* Free memory that is allocated for BD */
  827. dma_free_coherent(chan->dev, sizeof(*chan->seg_mv) *
  828. XILINX_DMA_NUM_DESCS, chan->seg_mv,
  829. chan->seg_p);
  830. }
  831. if (chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIDMA &&
  832. chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIMCDMA) {
  833. dma_pool_destroy(chan->desc_pool);
  834. chan->desc_pool = NULL;
  835. }
  836. }
  837. /**
  838. * xilinx_dma_get_residue - Compute residue for a given descriptor
  839. * @chan: Driver specific dma channel
  840. * @desc: dma transaction descriptor
  841. *
  842. * Return: The number of residue bytes for the descriptor.
  843. */
  844. static u32 xilinx_dma_get_residue(struct xilinx_dma_chan *chan,
  845. struct xilinx_dma_tx_descriptor *desc)
  846. {
  847. struct xilinx_cdma_tx_segment *cdma_seg;
  848. struct xilinx_axidma_tx_segment *axidma_seg;
  849. struct xilinx_aximcdma_tx_segment *aximcdma_seg;
  850. struct xilinx_cdma_desc_hw *cdma_hw;
  851. struct xilinx_axidma_desc_hw *axidma_hw;
  852. struct xilinx_aximcdma_desc_hw *aximcdma_hw;
  853. struct list_head *entry;
  854. u32 residue = 0;
  855. list_for_each(entry, &desc->segments) {
  856. if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
  857. cdma_seg = list_entry(entry,
  858. struct xilinx_cdma_tx_segment,
  859. node);
  860. cdma_hw = &cdma_seg->hw;
  861. residue += (cdma_hw->control - cdma_hw->status) &
  862. chan->xdev->max_buffer_len;
  863. } else if (chan->xdev->dma_config->dmatype ==
  864. XDMA_TYPE_AXIDMA) {
  865. axidma_seg = list_entry(entry,
  866. struct xilinx_axidma_tx_segment,
  867. node);
  868. axidma_hw = &axidma_seg->hw;
  869. residue += (axidma_hw->control - axidma_hw->status) &
  870. chan->xdev->max_buffer_len;
  871. } else {
  872. aximcdma_seg =
  873. list_entry(entry,
  874. struct xilinx_aximcdma_tx_segment,
  875. node);
  876. aximcdma_hw = &aximcdma_seg->hw;
  877. residue +=
  878. (aximcdma_hw->control - aximcdma_hw->status) &
  879. chan->xdev->max_buffer_len;
  880. }
  881. }
  882. return residue;
  883. }
  884. /**
  885. * xilinx_dma_chan_handle_cyclic - Cyclic dma callback
  886. * @chan: Driver specific dma channel
  887. * @desc: dma transaction descriptor
  888. * @flags: flags for spin lock
  889. */
  890. static void xilinx_dma_chan_handle_cyclic(struct xilinx_dma_chan *chan,
  891. struct xilinx_dma_tx_descriptor *desc,
  892. unsigned long *flags)
  893. {
  894. struct dmaengine_desc_callback cb;
  895. dmaengine_desc_get_callback(&desc->async_tx, &cb);
  896. if (dmaengine_desc_callback_valid(&cb)) {
  897. spin_unlock_irqrestore(&chan->lock, *flags);
  898. dmaengine_desc_callback_invoke(&cb, NULL);
  899. spin_lock_irqsave(&chan->lock, *flags);
  900. }
  901. }
  902. /**
  903. * xilinx_dma_chan_desc_cleanup - Clean channel descriptors
  904. * @chan: Driver specific DMA channel
  905. */
  906. static void xilinx_dma_chan_desc_cleanup(struct xilinx_dma_chan *chan)
  907. {
  908. struct xilinx_dma_tx_descriptor *desc, *next;
  909. unsigned long flags;
  910. spin_lock_irqsave(&chan->lock, flags);
  911. list_for_each_entry_safe(desc, next, &chan->done_list, node) {
  912. struct dmaengine_result result;
  913. if (desc->cyclic) {
  914. xilinx_dma_chan_handle_cyclic(chan, desc, &flags);
  915. break;
  916. }
  917. /* Remove from the list of running transactions */
  918. list_del(&desc->node);
  919. if (unlikely(desc->err)) {
  920. if (chan->direction == DMA_DEV_TO_MEM)
  921. result.result = DMA_TRANS_READ_FAILED;
  922. else
  923. result.result = DMA_TRANS_WRITE_FAILED;
  924. } else {
  925. result.result = DMA_TRANS_NOERROR;
  926. }
  927. result.residue = desc->residue;
  928. /* Run the link descriptor callback function */
  929. spin_unlock_irqrestore(&chan->lock, flags);
  930. dmaengine_desc_get_callback_invoke(&desc->async_tx, &result);
  931. spin_lock_irqsave(&chan->lock, flags);
  932. /* Run any dependencies, then free the descriptor */
  933. dma_run_dependencies(&desc->async_tx);
  934. xilinx_dma_free_tx_descriptor(chan, desc);
  935. /*
  936. * While we ran a callback the user called a terminate function,
  937. * which takes care of cleaning up any remaining descriptors
  938. */
  939. if (chan->terminating)
  940. break;
  941. }
  942. spin_unlock_irqrestore(&chan->lock, flags);
  943. }
  944. /**
  945. * xilinx_dma_do_tasklet - Schedule completion tasklet
  946. * @t: Pointer to the Xilinx DMA channel structure
  947. */
  948. static void xilinx_dma_do_tasklet(struct tasklet_struct *t)
  949. {
  950. struct xilinx_dma_chan *chan = from_tasklet(chan, t, tasklet);
  951. xilinx_dma_chan_desc_cleanup(chan);
  952. }
  953. /**
  954. * xilinx_dma_alloc_chan_resources - Allocate channel resources
  955. * @dchan: DMA channel
  956. *
  957. * Return: '0' on success and failure value on error
  958. */
  959. static int xilinx_dma_alloc_chan_resources(struct dma_chan *dchan)
  960. {
  961. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  962. int i;
  963. /* Has this channel already been allocated? */
  964. if (chan->desc_pool)
  965. return 0;
  966. /*
  967. * We need the descriptor to be aligned to 64bytes
  968. * for meeting Xilinx VDMA specification requirement.
  969. */
  970. if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
  971. /* Allocate the buffer descriptors. */
  972. chan->seg_v = dma_alloc_coherent(chan->dev,
  973. sizeof(*chan->seg_v) * XILINX_DMA_NUM_DESCS,
  974. &chan->seg_p, GFP_KERNEL);
  975. if (!chan->seg_v) {
  976. dev_err(chan->dev,
  977. "unable to allocate channel %d descriptors\n",
  978. chan->id);
  979. return -ENOMEM;
  980. }
  981. /*
  982. * For cyclic DMA mode we need to program the tail Descriptor
  983. * register with a value which is not a part of the BD chain
  984. * so allocating a desc segment during channel allocation for
  985. * programming tail descriptor.
  986. */
  987. chan->cyclic_seg_v = dma_alloc_coherent(chan->dev,
  988. sizeof(*chan->cyclic_seg_v),
  989. &chan->cyclic_seg_p,
  990. GFP_KERNEL);
  991. if (!chan->cyclic_seg_v) {
  992. dev_err(chan->dev,
  993. "unable to allocate desc segment for cyclic DMA\n");
  994. dma_free_coherent(chan->dev, sizeof(*chan->seg_v) *
  995. XILINX_DMA_NUM_DESCS, chan->seg_v,
  996. chan->seg_p);
  997. return -ENOMEM;
  998. }
  999. chan->cyclic_seg_v->phys = chan->cyclic_seg_p;
  1000. for (i = 0; i < XILINX_DMA_NUM_DESCS; i++) {
  1001. chan->seg_v[i].hw.next_desc =
  1002. lower_32_bits(chan->seg_p + sizeof(*chan->seg_v) *
  1003. ((i + 1) % XILINX_DMA_NUM_DESCS));
  1004. chan->seg_v[i].hw.next_desc_msb =
  1005. upper_32_bits(chan->seg_p + sizeof(*chan->seg_v) *
  1006. ((i + 1) % XILINX_DMA_NUM_DESCS));
  1007. chan->seg_v[i].phys = chan->seg_p +
  1008. sizeof(*chan->seg_v) * i;
  1009. list_add_tail(&chan->seg_v[i].node,
  1010. &chan->free_seg_list);
  1011. }
  1012. } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
  1013. /* Allocate the buffer descriptors. */
  1014. chan->seg_mv = dma_alloc_coherent(chan->dev,
  1015. sizeof(*chan->seg_mv) *
  1016. XILINX_DMA_NUM_DESCS,
  1017. &chan->seg_p, GFP_KERNEL);
  1018. if (!chan->seg_mv) {
  1019. dev_err(chan->dev,
  1020. "unable to allocate channel %d descriptors\n",
  1021. chan->id);
  1022. return -ENOMEM;
  1023. }
  1024. for (i = 0; i < XILINX_DMA_NUM_DESCS; i++) {
  1025. chan->seg_mv[i].hw.next_desc =
  1026. lower_32_bits(chan->seg_p + sizeof(*chan->seg_mv) *
  1027. ((i + 1) % XILINX_DMA_NUM_DESCS));
  1028. chan->seg_mv[i].hw.next_desc_msb =
  1029. upper_32_bits(chan->seg_p + sizeof(*chan->seg_mv) *
  1030. ((i + 1) % XILINX_DMA_NUM_DESCS));
  1031. chan->seg_mv[i].phys = chan->seg_p +
  1032. sizeof(*chan->seg_mv) * i;
  1033. list_add_tail(&chan->seg_mv[i].node,
  1034. &chan->free_seg_list);
  1035. }
  1036. } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
  1037. chan->desc_pool = dma_pool_create("xilinx_cdma_desc_pool",
  1038. chan->dev,
  1039. sizeof(struct xilinx_cdma_tx_segment),
  1040. __alignof__(struct xilinx_cdma_tx_segment),
  1041. 0);
  1042. } else {
  1043. chan->desc_pool = dma_pool_create("xilinx_vdma_desc_pool",
  1044. chan->dev,
  1045. sizeof(struct xilinx_vdma_tx_segment),
  1046. __alignof__(struct xilinx_vdma_tx_segment),
  1047. 0);
  1048. }
  1049. if (!chan->desc_pool &&
  1050. ((chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIDMA) &&
  1051. chan->xdev->dma_config->dmatype != XDMA_TYPE_AXIMCDMA)) {
  1052. dev_err(chan->dev,
  1053. "unable to allocate channel %d descriptor pool\n",
  1054. chan->id);
  1055. return -ENOMEM;
  1056. }
  1057. dma_cookie_init(dchan);
  1058. if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
  1059. /* For AXI DMA resetting once channel will reset the
  1060. * other channel as well so enable the interrupts here.
  1061. */
  1062. dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
  1063. XILINX_DMA_DMAXR_ALL_IRQ_MASK);
  1064. }
  1065. if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
  1066. dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
  1067. XILINX_CDMA_CR_SGMODE);
  1068. return 0;
  1069. }
  1070. /**
  1071. * xilinx_dma_calc_copysize - Calculate the amount of data to copy
  1072. * @chan: Driver specific DMA channel
  1073. * @size: Total data that needs to be copied
  1074. * @done: Amount of data that has been already copied
  1075. *
  1076. * Return: Amount of data that has to be copied
  1077. */
  1078. static int xilinx_dma_calc_copysize(struct xilinx_dma_chan *chan,
  1079. int size, int done)
  1080. {
  1081. size_t copy;
  1082. copy = min_t(size_t, size - done,
  1083. chan->xdev->max_buffer_len);
  1084. if ((copy + done < size) &&
  1085. chan->xdev->common.copy_align) {
  1086. /*
  1087. * If this is not the last descriptor, make sure
  1088. * the next one will be properly aligned
  1089. */
  1090. copy = rounddown(copy,
  1091. (1 << chan->xdev->common.copy_align));
  1092. }
  1093. return copy;
  1094. }
  1095. /**
  1096. * xilinx_dma_tx_status - Get DMA transaction status
  1097. * @dchan: DMA channel
  1098. * @cookie: Transaction identifier
  1099. * @txstate: Transaction state
  1100. *
  1101. * Return: DMA transaction status
  1102. */
  1103. static enum dma_status xilinx_dma_tx_status(struct dma_chan *dchan,
  1104. dma_cookie_t cookie,
  1105. struct dma_tx_state *txstate)
  1106. {
  1107. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  1108. struct xilinx_dma_tx_descriptor *desc;
  1109. enum dma_status ret;
  1110. unsigned long flags;
  1111. u32 residue = 0;
  1112. ret = dma_cookie_status(dchan, cookie, txstate);
  1113. if (ret == DMA_COMPLETE || !txstate)
  1114. return ret;
  1115. spin_lock_irqsave(&chan->lock, flags);
  1116. if (!list_empty(&chan->active_list)) {
  1117. desc = list_last_entry(&chan->active_list,
  1118. struct xilinx_dma_tx_descriptor, node);
  1119. /*
  1120. * VDMA and simple mode do not support residue reporting, so the
  1121. * residue field will always be 0.
  1122. */
  1123. if (chan->has_sg && chan->xdev->dma_config->dmatype != XDMA_TYPE_VDMA)
  1124. residue = xilinx_dma_get_residue(chan, desc);
  1125. }
  1126. spin_unlock_irqrestore(&chan->lock, flags);
  1127. dma_set_residue(txstate, residue);
  1128. return ret;
  1129. }
  1130. /**
  1131. * xilinx_dma_stop_transfer - Halt DMA channel
  1132. * @chan: Driver specific DMA channel
  1133. *
  1134. * Return: '0' on success and failure value on error
  1135. */
  1136. static int xilinx_dma_stop_transfer(struct xilinx_dma_chan *chan)
  1137. {
  1138. u32 val;
  1139. dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
  1140. /* Wait for the hardware to halt */
  1141. return xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
  1142. val & XILINX_DMA_DMASR_HALTED, 0,
  1143. XILINX_DMA_LOOP_COUNT);
  1144. }
  1145. /**
  1146. * xilinx_cdma_stop_transfer - Wait for the current transfer to complete
  1147. * @chan: Driver specific DMA channel
  1148. *
  1149. * Return: '0' on success and failure value on error
  1150. */
  1151. static int xilinx_cdma_stop_transfer(struct xilinx_dma_chan *chan)
  1152. {
  1153. u32 val;
  1154. return xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
  1155. val & XILINX_DMA_DMASR_IDLE, 0,
  1156. XILINX_DMA_LOOP_COUNT);
  1157. }
  1158. /**
  1159. * xilinx_dma_start - Start DMA channel
  1160. * @chan: Driver specific DMA channel
  1161. */
  1162. static void xilinx_dma_start(struct xilinx_dma_chan *chan)
  1163. {
  1164. int err;
  1165. u32 val;
  1166. dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RUNSTOP);
  1167. /* Wait for the hardware to start */
  1168. err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMASR, val,
  1169. !(val & XILINX_DMA_DMASR_HALTED), 0,
  1170. XILINX_DMA_LOOP_COUNT);
  1171. if (err) {
  1172. dev_err(chan->dev, "Cannot start channel %p: %x\n",
  1173. chan, dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
  1174. chan->err = true;
  1175. }
  1176. }
  1177. /**
  1178. * xilinx_vdma_start_transfer - Starts VDMA transfer
  1179. * @chan: Driver specific channel struct pointer
  1180. */
  1181. static void xilinx_vdma_start_transfer(struct xilinx_dma_chan *chan)
  1182. {
  1183. struct xilinx_vdma_config *config = &chan->config;
  1184. struct xilinx_dma_tx_descriptor *desc;
  1185. u32 reg, j;
  1186. struct xilinx_vdma_tx_segment *segment, *last = NULL;
  1187. int i = 0;
  1188. /* This function was invoked with lock held */
  1189. if (chan->err)
  1190. return;
  1191. if (!chan->idle)
  1192. return;
  1193. if (list_empty(&chan->pending_list))
  1194. return;
  1195. desc = list_first_entry(&chan->pending_list,
  1196. struct xilinx_dma_tx_descriptor, node);
  1197. /* Configure the hardware using info in the config structure */
  1198. if (chan->has_vflip) {
  1199. reg = dma_read(chan, XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP);
  1200. reg &= ~XILINX_VDMA_ENABLE_VERTICAL_FLIP;
  1201. reg |= config->vflip_en;
  1202. dma_write(chan, XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP,
  1203. reg);
  1204. }
  1205. reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
  1206. if (config->frm_cnt_en)
  1207. reg |= XILINX_DMA_DMACR_FRAMECNT_EN;
  1208. else
  1209. reg &= ~XILINX_DMA_DMACR_FRAMECNT_EN;
  1210. /* If not parking, enable circular mode */
  1211. if (config->park)
  1212. reg &= ~XILINX_DMA_DMACR_CIRC_EN;
  1213. else
  1214. reg |= XILINX_DMA_DMACR_CIRC_EN;
  1215. dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
  1216. j = chan->desc_submitcount;
  1217. reg = dma_read(chan, XILINX_DMA_REG_PARK_PTR);
  1218. if (chan->direction == DMA_MEM_TO_DEV) {
  1219. reg &= ~XILINX_DMA_PARK_PTR_RD_REF_MASK;
  1220. reg |= j << XILINX_DMA_PARK_PTR_RD_REF_SHIFT;
  1221. } else {
  1222. reg &= ~XILINX_DMA_PARK_PTR_WR_REF_MASK;
  1223. reg |= j << XILINX_DMA_PARK_PTR_WR_REF_SHIFT;
  1224. }
  1225. dma_write(chan, XILINX_DMA_REG_PARK_PTR, reg);
  1226. /* Start the hardware */
  1227. xilinx_dma_start(chan);
  1228. if (chan->err)
  1229. return;
  1230. /* Start the transfer */
  1231. if (chan->desc_submitcount < chan->num_frms)
  1232. i = chan->desc_submitcount;
  1233. list_for_each_entry(segment, &desc->segments, node) {
  1234. if (chan->ext_addr)
  1235. vdma_desc_write_64(chan,
  1236. XILINX_VDMA_REG_START_ADDRESS_64(i++),
  1237. segment->hw.buf_addr,
  1238. segment->hw.buf_addr_msb);
  1239. else
  1240. vdma_desc_write(chan,
  1241. XILINX_VDMA_REG_START_ADDRESS(i++),
  1242. segment->hw.buf_addr);
  1243. last = segment;
  1244. }
  1245. if (!last)
  1246. return;
  1247. /* HW expects these parameters to be same for one transaction */
  1248. vdma_desc_write(chan, XILINX_DMA_REG_HSIZE, last->hw.hsize);
  1249. vdma_desc_write(chan, XILINX_DMA_REG_FRMDLY_STRIDE,
  1250. last->hw.stride);
  1251. vdma_desc_write(chan, XILINX_DMA_REG_VSIZE, last->hw.vsize);
  1252. chan->desc_submitcount++;
  1253. chan->desc_pendingcount--;
  1254. list_move_tail(&desc->node, &chan->active_list);
  1255. if (chan->desc_submitcount == chan->num_frms)
  1256. chan->desc_submitcount = 0;
  1257. chan->idle = false;
  1258. }
  1259. /**
  1260. * xilinx_cdma_start_transfer - Starts cdma transfer
  1261. * @chan: Driver specific channel struct pointer
  1262. */
  1263. static void xilinx_cdma_start_transfer(struct xilinx_dma_chan *chan)
  1264. {
  1265. struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
  1266. struct xilinx_cdma_tx_segment *tail_segment;
  1267. u32 ctrl_reg = dma_read(chan, XILINX_DMA_REG_DMACR);
  1268. if (chan->err)
  1269. return;
  1270. if (!chan->idle)
  1271. return;
  1272. if (list_empty(&chan->pending_list))
  1273. return;
  1274. head_desc = list_first_entry(&chan->pending_list,
  1275. struct xilinx_dma_tx_descriptor, node);
  1276. tail_desc = list_last_entry(&chan->pending_list,
  1277. struct xilinx_dma_tx_descriptor, node);
  1278. tail_segment = list_last_entry(&tail_desc->segments,
  1279. struct xilinx_cdma_tx_segment, node);
  1280. if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
  1281. ctrl_reg &= ~XILINX_DMA_CR_COALESCE_MAX;
  1282. ctrl_reg |= chan->desc_pendingcount <<
  1283. XILINX_DMA_CR_COALESCE_SHIFT;
  1284. dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, ctrl_reg);
  1285. }
  1286. if (chan->has_sg) {
  1287. dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
  1288. XILINX_CDMA_CR_SGMODE);
  1289. dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
  1290. XILINX_CDMA_CR_SGMODE);
  1291. xilinx_write(chan, XILINX_DMA_REG_CURDESC,
  1292. head_desc->async_tx.phys);
  1293. /* Update tail ptr register which will start the transfer */
  1294. xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
  1295. tail_segment->phys);
  1296. } else {
  1297. /* In simple mode */
  1298. struct xilinx_cdma_tx_segment *segment;
  1299. struct xilinx_cdma_desc_hw *hw;
  1300. segment = list_first_entry(&head_desc->segments,
  1301. struct xilinx_cdma_tx_segment,
  1302. node);
  1303. hw = &segment->hw;
  1304. xilinx_write(chan, XILINX_CDMA_REG_SRCADDR,
  1305. xilinx_prep_dma_addr_t(hw->src_addr));
  1306. xilinx_write(chan, XILINX_CDMA_REG_DSTADDR,
  1307. xilinx_prep_dma_addr_t(hw->dest_addr));
  1308. /* Start the transfer */
  1309. dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
  1310. hw->control & chan->xdev->max_buffer_len);
  1311. }
  1312. list_splice_tail_init(&chan->pending_list, &chan->active_list);
  1313. chan->desc_pendingcount = 0;
  1314. chan->idle = false;
  1315. }
  1316. /**
  1317. * xilinx_dma_start_transfer - Starts DMA transfer
  1318. * @chan: Driver specific channel struct pointer
  1319. */
  1320. static void xilinx_dma_start_transfer(struct xilinx_dma_chan *chan)
  1321. {
  1322. struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
  1323. struct xilinx_axidma_tx_segment *tail_segment;
  1324. u32 reg;
  1325. if (chan->err)
  1326. return;
  1327. if (list_empty(&chan->pending_list))
  1328. return;
  1329. if (!chan->idle)
  1330. return;
  1331. head_desc = list_first_entry(&chan->pending_list,
  1332. struct xilinx_dma_tx_descriptor, node);
  1333. tail_desc = list_last_entry(&chan->pending_list,
  1334. struct xilinx_dma_tx_descriptor, node);
  1335. tail_segment = list_last_entry(&tail_desc->segments,
  1336. struct xilinx_axidma_tx_segment, node);
  1337. reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
  1338. if (chan->desc_pendingcount <= XILINX_DMA_COALESCE_MAX) {
  1339. reg &= ~XILINX_DMA_CR_COALESCE_MAX;
  1340. reg |= chan->desc_pendingcount <<
  1341. XILINX_DMA_CR_COALESCE_SHIFT;
  1342. dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
  1343. }
  1344. if (chan->has_sg)
  1345. xilinx_write(chan, XILINX_DMA_REG_CURDESC,
  1346. head_desc->async_tx.phys);
  1347. xilinx_dma_start(chan);
  1348. if (chan->err)
  1349. return;
  1350. /* Start the transfer */
  1351. if (chan->has_sg) {
  1352. if (chan->cyclic)
  1353. xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
  1354. chan->cyclic_seg_v->phys);
  1355. else
  1356. xilinx_write(chan, XILINX_DMA_REG_TAILDESC,
  1357. tail_segment->phys);
  1358. } else {
  1359. struct xilinx_axidma_tx_segment *segment;
  1360. struct xilinx_axidma_desc_hw *hw;
  1361. segment = list_first_entry(&head_desc->segments,
  1362. struct xilinx_axidma_tx_segment,
  1363. node);
  1364. hw = &segment->hw;
  1365. xilinx_write(chan, XILINX_DMA_REG_SRCDSTADDR,
  1366. xilinx_prep_dma_addr_t(hw->buf_addr));
  1367. /* Start the transfer */
  1368. dma_ctrl_write(chan, XILINX_DMA_REG_BTT,
  1369. hw->control & chan->xdev->max_buffer_len);
  1370. }
  1371. list_splice_tail_init(&chan->pending_list, &chan->active_list);
  1372. chan->desc_pendingcount = 0;
  1373. chan->idle = false;
  1374. }
  1375. /**
  1376. * xilinx_mcdma_start_transfer - Starts MCDMA transfer
  1377. * @chan: Driver specific channel struct pointer
  1378. */
  1379. static void xilinx_mcdma_start_transfer(struct xilinx_dma_chan *chan)
  1380. {
  1381. struct xilinx_dma_tx_descriptor *head_desc, *tail_desc;
  1382. struct xilinx_aximcdma_tx_segment *tail_segment;
  1383. u32 reg;
  1384. /*
  1385. * lock has been held by calling functions, so we don't need it
  1386. * to take it here again.
  1387. */
  1388. if (chan->err)
  1389. return;
  1390. if (!chan->idle)
  1391. return;
  1392. if (list_empty(&chan->pending_list))
  1393. return;
  1394. head_desc = list_first_entry(&chan->pending_list,
  1395. struct xilinx_dma_tx_descriptor, node);
  1396. tail_desc = list_last_entry(&chan->pending_list,
  1397. struct xilinx_dma_tx_descriptor, node);
  1398. tail_segment = list_last_entry(&tail_desc->segments,
  1399. struct xilinx_aximcdma_tx_segment, node);
  1400. reg = dma_ctrl_read(chan, XILINX_MCDMA_CHAN_CR_OFFSET(chan->tdest));
  1401. if (chan->desc_pendingcount <= XILINX_MCDMA_COALESCE_MAX) {
  1402. reg &= ~XILINX_MCDMA_COALESCE_MASK;
  1403. reg |= chan->desc_pendingcount <<
  1404. XILINX_MCDMA_COALESCE_SHIFT;
  1405. }
  1406. reg |= XILINX_MCDMA_IRQ_ALL_MASK;
  1407. dma_ctrl_write(chan, XILINX_MCDMA_CHAN_CR_OFFSET(chan->tdest), reg);
  1408. /* Program current descriptor */
  1409. xilinx_write(chan, XILINX_MCDMA_CHAN_CDESC_OFFSET(chan->tdest),
  1410. head_desc->async_tx.phys);
  1411. /* Program channel enable register */
  1412. reg = dma_ctrl_read(chan, XILINX_MCDMA_CHEN_OFFSET);
  1413. reg |= BIT(chan->tdest);
  1414. dma_ctrl_write(chan, XILINX_MCDMA_CHEN_OFFSET, reg);
  1415. /* Start the fetch of BDs for the channel */
  1416. reg = dma_ctrl_read(chan, XILINX_MCDMA_CHAN_CR_OFFSET(chan->tdest));
  1417. reg |= XILINX_MCDMA_CR_RUNSTOP_MASK;
  1418. dma_ctrl_write(chan, XILINX_MCDMA_CHAN_CR_OFFSET(chan->tdest), reg);
  1419. xilinx_dma_start(chan);
  1420. if (chan->err)
  1421. return;
  1422. /* Start the transfer */
  1423. xilinx_write(chan, XILINX_MCDMA_CHAN_TDESC_OFFSET(chan->tdest),
  1424. tail_segment->phys);
  1425. list_splice_tail_init(&chan->pending_list, &chan->active_list);
  1426. chan->desc_pendingcount = 0;
  1427. chan->idle = false;
  1428. }
  1429. /**
  1430. * xilinx_dma_issue_pending - Issue pending transactions
  1431. * @dchan: DMA channel
  1432. */
  1433. static void xilinx_dma_issue_pending(struct dma_chan *dchan)
  1434. {
  1435. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  1436. unsigned long flags;
  1437. spin_lock_irqsave(&chan->lock, flags);
  1438. chan->start_transfer(chan);
  1439. spin_unlock_irqrestore(&chan->lock, flags);
  1440. }
  1441. /**
  1442. * xilinx_dma_device_config - Configure the DMA channel
  1443. * @dchan: DMA channel
  1444. * @config: channel configuration
  1445. */
  1446. static int xilinx_dma_device_config(struct dma_chan *dchan,
  1447. struct dma_slave_config *config)
  1448. {
  1449. return 0;
  1450. }
  1451. /**
  1452. * xilinx_dma_complete_descriptor - Mark the active descriptor as complete
  1453. * @chan : xilinx DMA channel
  1454. *
  1455. * CONTEXT: hardirq
  1456. */
  1457. static void xilinx_dma_complete_descriptor(struct xilinx_dma_chan *chan)
  1458. {
  1459. struct xilinx_dma_tx_descriptor *desc, *next;
  1460. /* This function was invoked with lock held */
  1461. if (list_empty(&chan->active_list))
  1462. return;
  1463. list_for_each_entry_safe(desc, next, &chan->active_list, node) {
  1464. if (chan->has_sg && chan->xdev->dma_config->dmatype !=
  1465. XDMA_TYPE_VDMA)
  1466. desc->residue = xilinx_dma_get_residue(chan, desc);
  1467. else
  1468. desc->residue = 0;
  1469. desc->err = chan->err;
  1470. list_del(&desc->node);
  1471. if (!desc->cyclic)
  1472. dma_cookie_complete(&desc->async_tx);
  1473. list_add_tail(&desc->node, &chan->done_list);
  1474. }
  1475. }
  1476. /**
  1477. * xilinx_dma_reset - Reset DMA channel
  1478. * @chan: Driver specific DMA channel
  1479. *
  1480. * Return: '0' on success and failure value on error
  1481. */
  1482. static int xilinx_dma_reset(struct xilinx_dma_chan *chan)
  1483. {
  1484. int err;
  1485. u32 tmp;
  1486. dma_ctrl_set(chan, XILINX_DMA_REG_DMACR, XILINX_DMA_DMACR_RESET);
  1487. /* Wait for the hardware to finish reset */
  1488. err = xilinx_dma_poll_timeout(chan, XILINX_DMA_REG_DMACR, tmp,
  1489. !(tmp & XILINX_DMA_DMACR_RESET), 0,
  1490. XILINX_DMA_LOOP_COUNT);
  1491. if (err) {
  1492. dev_err(chan->dev, "reset timeout, cr %x, sr %x\n",
  1493. dma_ctrl_read(chan, XILINX_DMA_REG_DMACR),
  1494. dma_ctrl_read(chan, XILINX_DMA_REG_DMASR));
  1495. return -ETIMEDOUT;
  1496. }
  1497. chan->err = false;
  1498. chan->idle = true;
  1499. chan->desc_pendingcount = 0;
  1500. chan->desc_submitcount = 0;
  1501. return err;
  1502. }
  1503. /**
  1504. * xilinx_dma_chan_reset - Reset DMA channel and enable interrupts
  1505. * @chan: Driver specific DMA channel
  1506. *
  1507. * Return: '0' on success and failure value on error
  1508. */
  1509. static int xilinx_dma_chan_reset(struct xilinx_dma_chan *chan)
  1510. {
  1511. int err;
  1512. /* Reset VDMA */
  1513. err = xilinx_dma_reset(chan);
  1514. if (err)
  1515. return err;
  1516. /* Enable interrupts */
  1517. dma_ctrl_set(chan, XILINX_DMA_REG_DMACR,
  1518. XILINX_DMA_DMAXR_ALL_IRQ_MASK);
  1519. return 0;
  1520. }
  1521. /**
  1522. * xilinx_mcdma_irq_handler - MCDMA Interrupt handler
  1523. * @irq: IRQ number
  1524. * @data: Pointer to the Xilinx MCDMA channel structure
  1525. *
  1526. * Return: IRQ_HANDLED/IRQ_NONE
  1527. */
  1528. static irqreturn_t xilinx_mcdma_irq_handler(int irq, void *data)
  1529. {
  1530. struct xilinx_dma_chan *chan = data;
  1531. u32 status, ser_offset, chan_sermask, chan_offset = 0, chan_id;
  1532. if (chan->direction == DMA_DEV_TO_MEM)
  1533. ser_offset = XILINX_MCDMA_RXINT_SER_OFFSET;
  1534. else
  1535. ser_offset = XILINX_MCDMA_TXINT_SER_OFFSET;
  1536. /* Read the channel id raising the interrupt*/
  1537. chan_sermask = dma_ctrl_read(chan, ser_offset);
  1538. chan_id = ffs(chan_sermask);
  1539. if (!chan_id)
  1540. return IRQ_NONE;
  1541. if (chan->direction == DMA_DEV_TO_MEM)
  1542. chan_offset = chan->xdev->dma_config->max_channels / 2;
  1543. chan_offset = chan_offset + (chan_id - 1);
  1544. chan = chan->xdev->chan[chan_offset];
  1545. /* Read the status and ack the interrupts. */
  1546. status = dma_ctrl_read(chan, XILINX_MCDMA_CHAN_SR_OFFSET(chan->tdest));
  1547. if (!(status & XILINX_MCDMA_IRQ_ALL_MASK))
  1548. return IRQ_NONE;
  1549. dma_ctrl_write(chan, XILINX_MCDMA_CHAN_SR_OFFSET(chan->tdest),
  1550. status & XILINX_MCDMA_IRQ_ALL_MASK);
  1551. if (status & XILINX_MCDMA_IRQ_ERR_MASK) {
  1552. dev_err(chan->dev, "Channel %p has errors %x cdr %x tdr %x\n",
  1553. chan,
  1554. dma_ctrl_read(chan, XILINX_MCDMA_CH_ERR_OFFSET),
  1555. dma_ctrl_read(chan, XILINX_MCDMA_CHAN_CDESC_OFFSET
  1556. (chan->tdest)),
  1557. dma_ctrl_read(chan, XILINX_MCDMA_CHAN_TDESC_OFFSET
  1558. (chan->tdest)));
  1559. chan->err = true;
  1560. }
  1561. if (status & XILINX_MCDMA_IRQ_DELAY_MASK) {
  1562. /*
  1563. * Device takes too long to do the transfer when user requires
  1564. * responsiveness.
  1565. */
  1566. dev_dbg(chan->dev, "Inter-packet latency too long\n");
  1567. }
  1568. if (status & XILINX_MCDMA_IRQ_IOC_MASK) {
  1569. spin_lock(&chan->lock);
  1570. xilinx_dma_complete_descriptor(chan);
  1571. chan->idle = true;
  1572. chan->start_transfer(chan);
  1573. spin_unlock(&chan->lock);
  1574. }
  1575. tasklet_schedule(&chan->tasklet);
  1576. return IRQ_HANDLED;
  1577. }
  1578. /**
  1579. * xilinx_dma_irq_handler - DMA Interrupt handler
  1580. * @irq: IRQ number
  1581. * @data: Pointer to the Xilinx DMA channel structure
  1582. *
  1583. * Return: IRQ_HANDLED/IRQ_NONE
  1584. */
  1585. static irqreturn_t xilinx_dma_irq_handler(int irq, void *data)
  1586. {
  1587. struct xilinx_dma_chan *chan = data;
  1588. u32 status;
  1589. /* Read the status and ack the interrupts. */
  1590. status = dma_ctrl_read(chan, XILINX_DMA_REG_DMASR);
  1591. if (!(status & XILINX_DMA_DMAXR_ALL_IRQ_MASK))
  1592. return IRQ_NONE;
  1593. dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
  1594. status & XILINX_DMA_DMAXR_ALL_IRQ_MASK);
  1595. if (status & XILINX_DMA_DMASR_ERR_IRQ) {
  1596. /*
  1597. * An error occurred. If C_FLUSH_ON_FSYNC is enabled and the
  1598. * error is recoverable, ignore it. Otherwise flag the error.
  1599. *
  1600. * Only recoverable errors can be cleared in the DMASR register,
  1601. * make sure not to write to other error bits to 1.
  1602. */
  1603. u32 errors = status & XILINX_DMA_DMASR_ALL_ERR_MASK;
  1604. dma_ctrl_write(chan, XILINX_DMA_REG_DMASR,
  1605. errors & XILINX_DMA_DMASR_ERR_RECOVER_MASK);
  1606. if (!chan->flush_on_fsync ||
  1607. (errors & ~XILINX_DMA_DMASR_ERR_RECOVER_MASK)) {
  1608. dev_err(chan->dev,
  1609. "Channel %p has errors %x, cdr %x tdr %x\n",
  1610. chan, errors,
  1611. dma_ctrl_read(chan, XILINX_DMA_REG_CURDESC),
  1612. dma_ctrl_read(chan, XILINX_DMA_REG_TAILDESC));
  1613. chan->err = true;
  1614. }
  1615. }
  1616. if (status & XILINX_DMA_DMASR_DLY_CNT_IRQ) {
  1617. /*
  1618. * Device takes too long to do the transfer when user requires
  1619. * responsiveness.
  1620. */
  1621. dev_dbg(chan->dev, "Inter-packet latency too long\n");
  1622. }
  1623. if (status & XILINX_DMA_DMASR_FRM_CNT_IRQ) {
  1624. spin_lock(&chan->lock);
  1625. xilinx_dma_complete_descriptor(chan);
  1626. chan->idle = true;
  1627. chan->start_transfer(chan);
  1628. spin_unlock(&chan->lock);
  1629. }
  1630. tasklet_schedule(&chan->tasklet);
  1631. return IRQ_HANDLED;
  1632. }
  1633. /**
  1634. * append_desc_queue - Queuing descriptor
  1635. * @chan: Driver specific dma channel
  1636. * @desc: dma transaction descriptor
  1637. */
  1638. static void append_desc_queue(struct xilinx_dma_chan *chan,
  1639. struct xilinx_dma_tx_descriptor *desc)
  1640. {
  1641. struct xilinx_vdma_tx_segment *tail_segment;
  1642. struct xilinx_dma_tx_descriptor *tail_desc;
  1643. struct xilinx_axidma_tx_segment *axidma_tail_segment;
  1644. struct xilinx_aximcdma_tx_segment *aximcdma_tail_segment;
  1645. struct xilinx_cdma_tx_segment *cdma_tail_segment;
  1646. if (list_empty(&chan->pending_list))
  1647. goto append;
  1648. /*
  1649. * Add the hardware descriptor to the chain of hardware descriptors
  1650. * that already exists in memory.
  1651. */
  1652. tail_desc = list_last_entry(&chan->pending_list,
  1653. struct xilinx_dma_tx_descriptor, node);
  1654. if (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
  1655. tail_segment = list_last_entry(&tail_desc->segments,
  1656. struct xilinx_vdma_tx_segment,
  1657. node);
  1658. tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
  1659. } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
  1660. cdma_tail_segment = list_last_entry(&tail_desc->segments,
  1661. struct xilinx_cdma_tx_segment,
  1662. node);
  1663. cdma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
  1664. } else if (chan->xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
  1665. axidma_tail_segment = list_last_entry(&tail_desc->segments,
  1666. struct xilinx_axidma_tx_segment,
  1667. node);
  1668. axidma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
  1669. } else {
  1670. aximcdma_tail_segment =
  1671. list_last_entry(&tail_desc->segments,
  1672. struct xilinx_aximcdma_tx_segment,
  1673. node);
  1674. aximcdma_tail_segment->hw.next_desc = (u32)desc->async_tx.phys;
  1675. }
  1676. /*
  1677. * Add the software descriptor and all children to the list
  1678. * of pending transactions
  1679. */
  1680. append:
  1681. list_add_tail(&desc->node, &chan->pending_list);
  1682. chan->desc_pendingcount++;
  1683. if (chan->has_sg && (chan->xdev->dma_config->dmatype == XDMA_TYPE_VDMA)
  1684. && unlikely(chan->desc_pendingcount > chan->num_frms)) {
  1685. dev_dbg(chan->dev, "desc pendingcount is too high\n");
  1686. chan->desc_pendingcount = chan->num_frms;
  1687. }
  1688. }
  1689. /**
  1690. * xilinx_dma_tx_submit - Submit DMA transaction
  1691. * @tx: Async transaction descriptor
  1692. *
  1693. * Return: cookie value on success and failure value on error
  1694. */
  1695. static dma_cookie_t xilinx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  1696. {
  1697. struct xilinx_dma_tx_descriptor *desc = to_dma_tx_descriptor(tx);
  1698. struct xilinx_dma_chan *chan = to_xilinx_chan(tx->chan);
  1699. dma_cookie_t cookie;
  1700. unsigned long flags;
  1701. int err;
  1702. if (chan->cyclic) {
  1703. xilinx_dma_free_tx_descriptor(chan, desc);
  1704. return -EBUSY;
  1705. }
  1706. if (chan->err) {
  1707. /*
  1708. * If reset fails, need to hard reset the system.
  1709. * Channel is no longer functional
  1710. */
  1711. err = xilinx_dma_chan_reset(chan);
  1712. if (err < 0)
  1713. return err;
  1714. }
  1715. spin_lock_irqsave(&chan->lock, flags);
  1716. cookie = dma_cookie_assign(tx);
  1717. /* Put this transaction onto the tail of the pending queue */
  1718. append_desc_queue(chan, desc);
  1719. if (desc->cyclic)
  1720. chan->cyclic = true;
  1721. chan->terminating = false;
  1722. spin_unlock_irqrestore(&chan->lock, flags);
  1723. return cookie;
  1724. }
  1725. /**
  1726. * xilinx_vdma_dma_prep_interleaved - prepare a descriptor for a
  1727. * DMA_SLAVE transaction
  1728. * @dchan: DMA channel
  1729. * @xt: Interleaved template pointer
  1730. * @flags: transfer ack flags
  1731. *
  1732. * Return: Async transaction descriptor on success and NULL on failure
  1733. */
  1734. static struct dma_async_tx_descriptor *
  1735. xilinx_vdma_dma_prep_interleaved(struct dma_chan *dchan,
  1736. struct dma_interleaved_template *xt,
  1737. unsigned long flags)
  1738. {
  1739. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  1740. struct xilinx_dma_tx_descriptor *desc;
  1741. struct xilinx_vdma_tx_segment *segment;
  1742. struct xilinx_vdma_desc_hw *hw;
  1743. if (!is_slave_direction(xt->dir))
  1744. return NULL;
  1745. if (!xt->numf || !xt->sgl[0].size)
  1746. return NULL;
  1747. if (xt->frame_size != 1)
  1748. return NULL;
  1749. /* Allocate a transaction descriptor. */
  1750. desc = xilinx_dma_alloc_tx_descriptor(chan);
  1751. if (!desc)
  1752. return NULL;
  1753. dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
  1754. desc->async_tx.tx_submit = xilinx_dma_tx_submit;
  1755. async_tx_ack(&desc->async_tx);
  1756. /* Allocate the link descriptor from DMA pool */
  1757. segment = xilinx_vdma_alloc_tx_segment(chan);
  1758. if (!segment)
  1759. goto error;
  1760. /* Fill in the hardware descriptor */
  1761. hw = &segment->hw;
  1762. hw->vsize = xt->numf;
  1763. hw->hsize = xt->sgl[0].size;
  1764. hw->stride = (xt->sgl[0].icg + xt->sgl[0].size) <<
  1765. XILINX_DMA_FRMDLY_STRIDE_STRIDE_SHIFT;
  1766. hw->stride |= chan->config.frm_dly <<
  1767. XILINX_DMA_FRMDLY_STRIDE_FRMDLY_SHIFT;
  1768. if (xt->dir != DMA_MEM_TO_DEV) {
  1769. if (chan->ext_addr) {
  1770. hw->buf_addr = lower_32_bits(xt->dst_start);
  1771. hw->buf_addr_msb = upper_32_bits(xt->dst_start);
  1772. } else {
  1773. hw->buf_addr = xt->dst_start;
  1774. }
  1775. } else {
  1776. if (chan->ext_addr) {
  1777. hw->buf_addr = lower_32_bits(xt->src_start);
  1778. hw->buf_addr_msb = upper_32_bits(xt->src_start);
  1779. } else {
  1780. hw->buf_addr = xt->src_start;
  1781. }
  1782. }
  1783. /* Insert the segment into the descriptor segments list. */
  1784. list_add_tail(&segment->node, &desc->segments);
  1785. /* Link the last hardware descriptor with the first. */
  1786. segment = list_first_entry(&desc->segments,
  1787. struct xilinx_vdma_tx_segment, node);
  1788. desc->async_tx.phys = segment->phys;
  1789. return &desc->async_tx;
  1790. error:
  1791. xilinx_dma_free_tx_descriptor(chan, desc);
  1792. return NULL;
  1793. }
  1794. /**
  1795. * xilinx_cdma_prep_memcpy - prepare descriptors for a memcpy transaction
  1796. * @dchan: DMA channel
  1797. * @dma_dst: destination address
  1798. * @dma_src: source address
  1799. * @len: transfer length
  1800. * @flags: transfer ack flags
  1801. *
  1802. * Return: Async transaction descriptor on success and NULL on failure
  1803. */
  1804. static struct dma_async_tx_descriptor *
  1805. xilinx_cdma_prep_memcpy(struct dma_chan *dchan, dma_addr_t dma_dst,
  1806. dma_addr_t dma_src, size_t len, unsigned long flags)
  1807. {
  1808. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  1809. struct xilinx_dma_tx_descriptor *desc;
  1810. struct xilinx_cdma_tx_segment *segment;
  1811. struct xilinx_cdma_desc_hw *hw;
  1812. if (!len || len > chan->xdev->max_buffer_len)
  1813. return NULL;
  1814. desc = xilinx_dma_alloc_tx_descriptor(chan);
  1815. if (!desc)
  1816. return NULL;
  1817. dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
  1818. desc->async_tx.tx_submit = xilinx_dma_tx_submit;
  1819. /* Allocate the link descriptor from DMA pool */
  1820. segment = xilinx_cdma_alloc_tx_segment(chan);
  1821. if (!segment)
  1822. goto error;
  1823. hw = &segment->hw;
  1824. hw->control = len;
  1825. hw->src_addr = dma_src;
  1826. hw->dest_addr = dma_dst;
  1827. if (chan->ext_addr) {
  1828. hw->src_addr_msb = upper_32_bits(dma_src);
  1829. hw->dest_addr_msb = upper_32_bits(dma_dst);
  1830. }
  1831. /* Insert the segment into the descriptor segments list. */
  1832. list_add_tail(&segment->node, &desc->segments);
  1833. desc->async_tx.phys = segment->phys;
  1834. hw->next_desc = segment->phys;
  1835. return &desc->async_tx;
  1836. error:
  1837. xilinx_dma_free_tx_descriptor(chan, desc);
  1838. return NULL;
  1839. }
  1840. /**
  1841. * xilinx_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  1842. * @dchan: DMA channel
  1843. * @sgl: scatterlist to transfer to/from
  1844. * @sg_len: number of entries in @scatterlist
  1845. * @direction: DMA direction
  1846. * @flags: transfer ack flags
  1847. * @context: APP words of the descriptor
  1848. *
  1849. * Return: Async transaction descriptor on success and NULL on failure
  1850. */
  1851. static struct dma_async_tx_descriptor *xilinx_dma_prep_slave_sg(
  1852. struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
  1853. enum dma_transfer_direction direction, unsigned long flags,
  1854. void *context)
  1855. {
  1856. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  1857. struct xilinx_dma_tx_descriptor *desc;
  1858. struct xilinx_axidma_tx_segment *segment = NULL;
  1859. u32 *app_w = (u32 *)context;
  1860. struct scatterlist *sg;
  1861. size_t copy;
  1862. size_t sg_used;
  1863. unsigned int i;
  1864. if (!is_slave_direction(direction))
  1865. return NULL;
  1866. /* Allocate a transaction descriptor. */
  1867. desc = xilinx_dma_alloc_tx_descriptor(chan);
  1868. if (!desc)
  1869. return NULL;
  1870. dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
  1871. desc->async_tx.tx_submit = xilinx_dma_tx_submit;
  1872. /* Build transactions using information in the scatter gather list */
  1873. for_each_sg(sgl, sg, sg_len, i) {
  1874. sg_used = 0;
  1875. /* Loop until the entire scatterlist entry is used */
  1876. while (sg_used < sg_dma_len(sg)) {
  1877. struct xilinx_axidma_desc_hw *hw;
  1878. /* Get a free segment */
  1879. segment = xilinx_axidma_alloc_tx_segment(chan);
  1880. if (!segment)
  1881. goto error;
  1882. /*
  1883. * Calculate the maximum number of bytes to transfer,
  1884. * making sure it is less than the hw limit
  1885. */
  1886. copy = xilinx_dma_calc_copysize(chan, sg_dma_len(sg),
  1887. sg_used);
  1888. hw = &segment->hw;
  1889. /* Fill in the descriptor */
  1890. xilinx_axidma_buf(chan, hw, sg_dma_address(sg),
  1891. sg_used, 0);
  1892. hw->control = copy;
  1893. if (chan->direction == DMA_MEM_TO_DEV) {
  1894. if (app_w)
  1895. memcpy(hw->app, app_w, sizeof(u32) *
  1896. XILINX_DMA_NUM_APP_WORDS);
  1897. }
  1898. sg_used += copy;
  1899. /*
  1900. * Insert the segment into the descriptor segments
  1901. * list.
  1902. */
  1903. list_add_tail(&segment->node, &desc->segments);
  1904. }
  1905. }
  1906. segment = list_first_entry(&desc->segments,
  1907. struct xilinx_axidma_tx_segment, node);
  1908. desc->async_tx.phys = segment->phys;
  1909. /* For the last DMA_MEM_TO_DEV transfer, set EOP */
  1910. if (chan->direction == DMA_MEM_TO_DEV) {
  1911. segment->hw.control |= XILINX_DMA_BD_SOP;
  1912. segment = list_last_entry(&desc->segments,
  1913. struct xilinx_axidma_tx_segment,
  1914. node);
  1915. segment->hw.control |= XILINX_DMA_BD_EOP;
  1916. }
  1917. return &desc->async_tx;
  1918. error:
  1919. xilinx_dma_free_tx_descriptor(chan, desc);
  1920. return NULL;
  1921. }
  1922. /**
  1923. * xilinx_dma_prep_dma_cyclic - prepare descriptors for a DMA_SLAVE transaction
  1924. * @dchan: DMA channel
  1925. * @buf_addr: Physical address of the buffer
  1926. * @buf_len: Total length of the cyclic buffers
  1927. * @period_len: length of individual cyclic buffer
  1928. * @direction: DMA direction
  1929. * @flags: transfer ack flags
  1930. *
  1931. * Return: Async transaction descriptor on success and NULL on failure
  1932. */
  1933. static struct dma_async_tx_descriptor *xilinx_dma_prep_dma_cyclic(
  1934. struct dma_chan *dchan, dma_addr_t buf_addr, size_t buf_len,
  1935. size_t period_len, enum dma_transfer_direction direction,
  1936. unsigned long flags)
  1937. {
  1938. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  1939. struct xilinx_dma_tx_descriptor *desc;
  1940. struct xilinx_axidma_tx_segment *segment, *head_segment, *prev = NULL;
  1941. size_t copy, sg_used;
  1942. unsigned int num_periods;
  1943. int i;
  1944. u32 reg;
  1945. if (!period_len)
  1946. return NULL;
  1947. num_periods = buf_len / period_len;
  1948. if (!num_periods)
  1949. return NULL;
  1950. if (!is_slave_direction(direction))
  1951. return NULL;
  1952. /* Allocate a transaction descriptor. */
  1953. desc = xilinx_dma_alloc_tx_descriptor(chan);
  1954. if (!desc)
  1955. return NULL;
  1956. chan->direction = direction;
  1957. dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
  1958. desc->async_tx.tx_submit = xilinx_dma_tx_submit;
  1959. for (i = 0; i < num_periods; ++i) {
  1960. sg_used = 0;
  1961. while (sg_used < period_len) {
  1962. struct xilinx_axidma_desc_hw *hw;
  1963. /* Get a free segment */
  1964. segment = xilinx_axidma_alloc_tx_segment(chan);
  1965. if (!segment)
  1966. goto error;
  1967. /*
  1968. * Calculate the maximum number of bytes to transfer,
  1969. * making sure it is less than the hw limit
  1970. */
  1971. copy = xilinx_dma_calc_copysize(chan, period_len,
  1972. sg_used);
  1973. hw = &segment->hw;
  1974. xilinx_axidma_buf(chan, hw, buf_addr, sg_used,
  1975. period_len * i);
  1976. hw->control = copy;
  1977. if (prev)
  1978. prev->hw.next_desc = segment->phys;
  1979. prev = segment;
  1980. sg_used += copy;
  1981. /*
  1982. * Insert the segment into the descriptor segments
  1983. * list.
  1984. */
  1985. list_add_tail(&segment->node, &desc->segments);
  1986. }
  1987. }
  1988. head_segment = list_first_entry(&desc->segments,
  1989. struct xilinx_axidma_tx_segment, node);
  1990. desc->async_tx.phys = head_segment->phys;
  1991. desc->cyclic = true;
  1992. reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
  1993. reg |= XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
  1994. dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
  1995. segment = list_last_entry(&desc->segments,
  1996. struct xilinx_axidma_tx_segment,
  1997. node);
  1998. segment->hw.next_desc = (u32) head_segment->phys;
  1999. /* For the last DMA_MEM_TO_DEV transfer, set EOP */
  2000. if (direction == DMA_MEM_TO_DEV) {
  2001. head_segment->hw.control |= XILINX_DMA_BD_SOP;
  2002. segment->hw.control |= XILINX_DMA_BD_EOP;
  2003. }
  2004. return &desc->async_tx;
  2005. error:
  2006. xilinx_dma_free_tx_descriptor(chan, desc);
  2007. return NULL;
  2008. }
  2009. /**
  2010. * xilinx_mcdma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  2011. * @dchan: DMA channel
  2012. * @sgl: scatterlist to transfer to/from
  2013. * @sg_len: number of entries in @scatterlist
  2014. * @direction: DMA direction
  2015. * @flags: transfer ack flags
  2016. * @context: APP words of the descriptor
  2017. *
  2018. * Return: Async transaction descriptor on success and NULL on failure
  2019. */
  2020. static struct dma_async_tx_descriptor *
  2021. xilinx_mcdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
  2022. unsigned int sg_len,
  2023. enum dma_transfer_direction direction,
  2024. unsigned long flags, void *context)
  2025. {
  2026. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  2027. struct xilinx_dma_tx_descriptor *desc;
  2028. struct xilinx_aximcdma_tx_segment *segment = NULL;
  2029. u32 *app_w = (u32 *)context;
  2030. struct scatterlist *sg;
  2031. size_t copy;
  2032. size_t sg_used;
  2033. unsigned int i;
  2034. if (!is_slave_direction(direction))
  2035. return NULL;
  2036. /* Allocate a transaction descriptor. */
  2037. desc = xilinx_dma_alloc_tx_descriptor(chan);
  2038. if (!desc)
  2039. return NULL;
  2040. dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
  2041. desc->async_tx.tx_submit = xilinx_dma_tx_submit;
  2042. /* Build transactions using information in the scatter gather list */
  2043. for_each_sg(sgl, sg, sg_len, i) {
  2044. sg_used = 0;
  2045. /* Loop until the entire scatterlist entry is used */
  2046. while (sg_used < sg_dma_len(sg)) {
  2047. struct xilinx_aximcdma_desc_hw *hw;
  2048. /* Get a free segment */
  2049. segment = xilinx_aximcdma_alloc_tx_segment(chan);
  2050. if (!segment)
  2051. goto error;
  2052. /*
  2053. * Calculate the maximum number of bytes to transfer,
  2054. * making sure it is less than the hw limit
  2055. */
  2056. copy = min_t(size_t, sg_dma_len(sg) - sg_used,
  2057. chan->xdev->max_buffer_len);
  2058. hw = &segment->hw;
  2059. /* Fill in the descriptor */
  2060. xilinx_aximcdma_buf(chan, hw, sg_dma_address(sg),
  2061. sg_used);
  2062. hw->control = copy;
  2063. if (chan->direction == DMA_MEM_TO_DEV && app_w) {
  2064. memcpy(hw->app, app_w, sizeof(u32) *
  2065. XILINX_DMA_NUM_APP_WORDS);
  2066. }
  2067. sg_used += copy;
  2068. /*
  2069. * Insert the segment into the descriptor segments
  2070. * list.
  2071. */
  2072. list_add_tail(&segment->node, &desc->segments);
  2073. }
  2074. }
  2075. segment = list_first_entry(&desc->segments,
  2076. struct xilinx_aximcdma_tx_segment, node);
  2077. desc->async_tx.phys = segment->phys;
  2078. /* For the last DMA_MEM_TO_DEV transfer, set EOP */
  2079. if (chan->direction == DMA_MEM_TO_DEV) {
  2080. segment->hw.control |= XILINX_MCDMA_BD_SOP;
  2081. segment = list_last_entry(&desc->segments,
  2082. struct xilinx_aximcdma_tx_segment,
  2083. node);
  2084. segment->hw.control |= XILINX_MCDMA_BD_EOP;
  2085. }
  2086. return &desc->async_tx;
  2087. error:
  2088. xilinx_dma_free_tx_descriptor(chan, desc);
  2089. return NULL;
  2090. }
  2091. /**
  2092. * xilinx_dma_terminate_all - Halt the channel and free descriptors
  2093. * @dchan: Driver specific DMA Channel pointer
  2094. *
  2095. * Return: '0' always.
  2096. */
  2097. static int xilinx_dma_terminate_all(struct dma_chan *dchan)
  2098. {
  2099. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  2100. u32 reg;
  2101. int err;
  2102. if (!chan->cyclic) {
  2103. err = chan->stop_transfer(chan);
  2104. if (err) {
  2105. dev_err(chan->dev, "Cannot stop channel %p: %x\n",
  2106. chan, dma_ctrl_read(chan,
  2107. XILINX_DMA_REG_DMASR));
  2108. chan->err = true;
  2109. }
  2110. }
  2111. xilinx_dma_chan_reset(chan);
  2112. /* Remove and free all of the descriptors in the lists */
  2113. chan->terminating = true;
  2114. xilinx_dma_free_descriptors(chan);
  2115. chan->idle = true;
  2116. if (chan->cyclic) {
  2117. reg = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
  2118. reg &= ~XILINX_DMA_CR_CYCLIC_BD_EN_MASK;
  2119. dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, reg);
  2120. chan->cyclic = false;
  2121. }
  2122. if ((chan->xdev->dma_config->dmatype == XDMA_TYPE_CDMA) && chan->has_sg)
  2123. dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
  2124. XILINX_CDMA_CR_SGMODE);
  2125. return 0;
  2126. }
  2127. static void xilinx_dma_synchronize(struct dma_chan *dchan)
  2128. {
  2129. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  2130. tasklet_kill(&chan->tasklet);
  2131. }
  2132. /**
  2133. * xilinx_vdma_channel_set_config - Configure VDMA channel
  2134. * Run-time configuration for Axi VDMA, supports:
  2135. * . halt the channel
  2136. * . configure interrupt coalescing and inter-packet delay threshold
  2137. * . start/stop parking
  2138. * . enable genlock
  2139. *
  2140. * @dchan: DMA channel
  2141. * @cfg: VDMA device configuration pointer
  2142. *
  2143. * Return: '0' on success and failure value on error
  2144. */
  2145. int xilinx_vdma_channel_set_config(struct dma_chan *dchan,
  2146. struct xilinx_vdma_config *cfg)
  2147. {
  2148. struct xilinx_dma_chan *chan = to_xilinx_chan(dchan);
  2149. u32 dmacr;
  2150. if (cfg->reset)
  2151. return xilinx_dma_chan_reset(chan);
  2152. dmacr = dma_ctrl_read(chan, XILINX_DMA_REG_DMACR);
  2153. chan->config.frm_dly = cfg->frm_dly;
  2154. chan->config.park = cfg->park;
  2155. /* genlock settings */
  2156. chan->config.gen_lock = cfg->gen_lock;
  2157. chan->config.master = cfg->master;
  2158. dmacr &= ~XILINX_DMA_DMACR_GENLOCK_EN;
  2159. if (cfg->gen_lock && chan->genlock) {
  2160. dmacr |= XILINX_DMA_DMACR_GENLOCK_EN;
  2161. dmacr &= ~XILINX_DMA_DMACR_MASTER_MASK;
  2162. dmacr |= cfg->master << XILINX_DMA_DMACR_MASTER_SHIFT;
  2163. }
  2164. chan->config.frm_cnt_en = cfg->frm_cnt_en;
  2165. chan->config.vflip_en = cfg->vflip_en;
  2166. if (cfg->park)
  2167. chan->config.park_frm = cfg->park_frm;
  2168. else
  2169. chan->config.park_frm = -1;
  2170. chan->config.coalesc = cfg->coalesc;
  2171. chan->config.delay = cfg->delay;
  2172. if (cfg->coalesc <= XILINX_DMA_DMACR_FRAME_COUNT_MAX) {
  2173. dmacr &= ~XILINX_DMA_DMACR_FRAME_COUNT_MASK;
  2174. dmacr |= cfg->coalesc << XILINX_DMA_DMACR_FRAME_COUNT_SHIFT;
  2175. chan->config.coalesc = cfg->coalesc;
  2176. }
  2177. if (cfg->delay <= XILINX_DMA_DMACR_DELAY_MAX) {
  2178. dmacr &= ~XILINX_DMA_DMACR_DELAY_MASK;
  2179. dmacr |= cfg->delay << XILINX_DMA_DMACR_DELAY_SHIFT;
  2180. chan->config.delay = cfg->delay;
  2181. }
  2182. /* FSync Source selection */
  2183. dmacr &= ~XILINX_DMA_DMACR_FSYNCSRC_MASK;
  2184. dmacr |= cfg->ext_fsync << XILINX_DMA_DMACR_FSYNCSRC_SHIFT;
  2185. dma_ctrl_write(chan, XILINX_DMA_REG_DMACR, dmacr);
  2186. return 0;
  2187. }
  2188. EXPORT_SYMBOL(xilinx_vdma_channel_set_config);
  2189. /* -----------------------------------------------------------------------------
  2190. * Probe and remove
  2191. */
  2192. /**
  2193. * xilinx_dma_chan_remove - Per Channel remove function
  2194. * @chan: Driver specific DMA channel
  2195. */
  2196. static void xilinx_dma_chan_remove(struct xilinx_dma_chan *chan)
  2197. {
  2198. /* Disable all interrupts */
  2199. dma_ctrl_clr(chan, XILINX_DMA_REG_DMACR,
  2200. XILINX_DMA_DMAXR_ALL_IRQ_MASK);
  2201. if (chan->irq > 0)
  2202. free_irq(chan->irq, chan);
  2203. tasklet_kill(&chan->tasklet);
  2204. list_del(&chan->common.device_node);
  2205. }
  2206. static int axidma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
  2207. struct clk **tx_clk, struct clk **rx_clk,
  2208. struct clk **sg_clk, struct clk **tmp_clk)
  2209. {
  2210. int err;
  2211. *tmp_clk = NULL;
  2212. *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
  2213. if (IS_ERR(*axi_clk))
  2214. return dev_err_probe(&pdev->dev, PTR_ERR(*axi_clk), "failed to get axi_aclk\n");
  2215. *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
  2216. if (IS_ERR(*tx_clk))
  2217. *tx_clk = NULL;
  2218. *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
  2219. if (IS_ERR(*rx_clk))
  2220. *rx_clk = NULL;
  2221. *sg_clk = devm_clk_get(&pdev->dev, "m_axi_sg_aclk");
  2222. if (IS_ERR(*sg_clk))
  2223. *sg_clk = NULL;
  2224. err = clk_prepare_enable(*axi_clk);
  2225. if (err) {
  2226. dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
  2227. return err;
  2228. }
  2229. err = clk_prepare_enable(*tx_clk);
  2230. if (err) {
  2231. dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
  2232. goto err_disable_axiclk;
  2233. }
  2234. err = clk_prepare_enable(*rx_clk);
  2235. if (err) {
  2236. dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
  2237. goto err_disable_txclk;
  2238. }
  2239. err = clk_prepare_enable(*sg_clk);
  2240. if (err) {
  2241. dev_err(&pdev->dev, "failed to enable sg_clk (%d)\n", err);
  2242. goto err_disable_rxclk;
  2243. }
  2244. return 0;
  2245. err_disable_rxclk:
  2246. clk_disable_unprepare(*rx_clk);
  2247. err_disable_txclk:
  2248. clk_disable_unprepare(*tx_clk);
  2249. err_disable_axiclk:
  2250. clk_disable_unprepare(*axi_clk);
  2251. return err;
  2252. }
  2253. static int axicdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
  2254. struct clk **dev_clk, struct clk **tmp_clk,
  2255. struct clk **tmp1_clk, struct clk **tmp2_clk)
  2256. {
  2257. int err;
  2258. *tmp_clk = NULL;
  2259. *tmp1_clk = NULL;
  2260. *tmp2_clk = NULL;
  2261. *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
  2262. if (IS_ERR(*axi_clk))
  2263. return dev_err_probe(&pdev->dev, PTR_ERR(*axi_clk), "failed to get axi_aclk\n");
  2264. *dev_clk = devm_clk_get(&pdev->dev, "m_axi_aclk");
  2265. if (IS_ERR(*dev_clk))
  2266. return dev_err_probe(&pdev->dev, PTR_ERR(*dev_clk), "failed to get dev_clk\n");
  2267. err = clk_prepare_enable(*axi_clk);
  2268. if (err) {
  2269. dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n", err);
  2270. return err;
  2271. }
  2272. err = clk_prepare_enable(*dev_clk);
  2273. if (err) {
  2274. dev_err(&pdev->dev, "failed to enable dev_clk (%d)\n", err);
  2275. goto err_disable_axiclk;
  2276. }
  2277. return 0;
  2278. err_disable_axiclk:
  2279. clk_disable_unprepare(*axi_clk);
  2280. return err;
  2281. }
  2282. static int axivdma_clk_init(struct platform_device *pdev, struct clk **axi_clk,
  2283. struct clk **tx_clk, struct clk **txs_clk,
  2284. struct clk **rx_clk, struct clk **rxs_clk)
  2285. {
  2286. int err;
  2287. *axi_clk = devm_clk_get(&pdev->dev, "s_axi_lite_aclk");
  2288. if (IS_ERR(*axi_clk))
  2289. return dev_err_probe(&pdev->dev, PTR_ERR(*axi_clk), "failed to get axi_aclk\n");
  2290. *tx_clk = devm_clk_get(&pdev->dev, "m_axi_mm2s_aclk");
  2291. if (IS_ERR(*tx_clk))
  2292. *tx_clk = NULL;
  2293. *txs_clk = devm_clk_get(&pdev->dev, "m_axis_mm2s_aclk");
  2294. if (IS_ERR(*txs_clk))
  2295. *txs_clk = NULL;
  2296. *rx_clk = devm_clk_get(&pdev->dev, "m_axi_s2mm_aclk");
  2297. if (IS_ERR(*rx_clk))
  2298. *rx_clk = NULL;
  2299. *rxs_clk = devm_clk_get(&pdev->dev, "s_axis_s2mm_aclk");
  2300. if (IS_ERR(*rxs_clk))
  2301. *rxs_clk = NULL;
  2302. err = clk_prepare_enable(*axi_clk);
  2303. if (err) {
  2304. dev_err(&pdev->dev, "failed to enable axi_clk (%d)\n",
  2305. err);
  2306. return err;
  2307. }
  2308. err = clk_prepare_enable(*tx_clk);
  2309. if (err) {
  2310. dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
  2311. goto err_disable_axiclk;
  2312. }
  2313. err = clk_prepare_enable(*txs_clk);
  2314. if (err) {
  2315. dev_err(&pdev->dev, "failed to enable txs_clk (%d)\n", err);
  2316. goto err_disable_txclk;
  2317. }
  2318. err = clk_prepare_enable(*rx_clk);
  2319. if (err) {
  2320. dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
  2321. goto err_disable_txsclk;
  2322. }
  2323. err = clk_prepare_enable(*rxs_clk);
  2324. if (err) {
  2325. dev_err(&pdev->dev, "failed to enable rxs_clk (%d)\n", err);
  2326. goto err_disable_rxclk;
  2327. }
  2328. return 0;
  2329. err_disable_rxclk:
  2330. clk_disable_unprepare(*rx_clk);
  2331. err_disable_txsclk:
  2332. clk_disable_unprepare(*txs_clk);
  2333. err_disable_txclk:
  2334. clk_disable_unprepare(*tx_clk);
  2335. err_disable_axiclk:
  2336. clk_disable_unprepare(*axi_clk);
  2337. return err;
  2338. }
  2339. static void xdma_disable_allclks(struct xilinx_dma_device *xdev)
  2340. {
  2341. clk_disable_unprepare(xdev->rxs_clk);
  2342. clk_disable_unprepare(xdev->rx_clk);
  2343. clk_disable_unprepare(xdev->txs_clk);
  2344. clk_disable_unprepare(xdev->tx_clk);
  2345. clk_disable_unprepare(xdev->axi_clk);
  2346. }
  2347. /**
  2348. * xilinx_dma_chan_probe - Per Channel Probing
  2349. * It get channel features from the device tree entry and
  2350. * initialize special channel handling routines
  2351. *
  2352. * @xdev: Driver specific device structure
  2353. * @node: Device node
  2354. *
  2355. * Return: '0' on success and failure value on error
  2356. */
  2357. static int xilinx_dma_chan_probe(struct xilinx_dma_device *xdev,
  2358. struct device_node *node)
  2359. {
  2360. struct xilinx_dma_chan *chan;
  2361. bool has_dre = false;
  2362. u32 value, width;
  2363. int err;
  2364. /* Allocate and initialize the channel structure */
  2365. chan = devm_kzalloc(xdev->dev, sizeof(*chan), GFP_KERNEL);
  2366. if (!chan)
  2367. return -ENOMEM;
  2368. chan->dev = xdev->dev;
  2369. chan->xdev = xdev;
  2370. chan->desc_pendingcount = 0x0;
  2371. chan->ext_addr = xdev->ext_addr;
  2372. /* This variable ensures that descriptors are not
  2373. * Submitted when dma engine is in progress. This variable is
  2374. * Added to avoid polling for a bit in the status register to
  2375. * Know dma state in the driver hot path.
  2376. */
  2377. chan->idle = true;
  2378. spin_lock_init(&chan->lock);
  2379. INIT_LIST_HEAD(&chan->pending_list);
  2380. INIT_LIST_HEAD(&chan->done_list);
  2381. INIT_LIST_HEAD(&chan->active_list);
  2382. INIT_LIST_HEAD(&chan->free_seg_list);
  2383. /* Retrieve the channel properties from the device tree */
  2384. has_dre = of_property_read_bool(node, "xlnx,include-dre");
  2385. chan->genlock = of_property_read_bool(node, "xlnx,genlock-mode");
  2386. err = of_property_read_u32(node, "xlnx,datawidth", &value);
  2387. if (err) {
  2388. dev_err(xdev->dev, "missing xlnx,datawidth property\n");
  2389. return err;
  2390. }
  2391. width = value >> 3; /* Convert bits to bytes */
  2392. /* If data width is greater than 8 bytes, DRE is not in hw */
  2393. if (width > 8)
  2394. has_dre = false;
  2395. if (!has_dre)
  2396. xdev->common.copy_align = (enum dmaengine_alignment)fls(width - 1);
  2397. if (of_device_is_compatible(node, "xlnx,axi-vdma-mm2s-channel") ||
  2398. of_device_is_compatible(node, "xlnx,axi-dma-mm2s-channel") ||
  2399. of_device_is_compatible(node, "xlnx,axi-cdma-channel")) {
  2400. chan->direction = DMA_MEM_TO_DEV;
  2401. chan->id = xdev->mm2s_chan_id++;
  2402. chan->tdest = chan->id;
  2403. chan->ctrl_offset = XILINX_DMA_MM2S_CTRL_OFFSET;
  2404. if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
  2405. chan->desc_offset = XILINX_VDMA_MM2S_DESC_OFFSET;
  2406. chan->config.park = 1;
  2407. if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
  2408. xdev->flush_on_fsync == XILINX_DMA_FLUSH_MM2S)
  2409. chan->flush_on_fsync = true;
  2410. }
  2411. } else if (of_device_is_compatible(node,
  2412. "xlnx,axi-vdma-s2mm-channel") ||
  2413. of_device_is_compatible(node,
  2414. "xlnx,axi-dma-s2mm-channel")) {
  2415. chan->direction = DMA_DEV_TO_MEM;
  2416. chan->id = xdev->s2mm_chan_id++;
  2417. chan->tdest = chan->id - xdev->dma_config->max_channels / 2;
  2418. chan->has_vflip = of_property_read_bool(node,
  2419. "xlnx,enable-vert-flip");
  2420. if (chan->has_vflip) {
  2421. chan->config.vflip_en = dma_read(chan,
  2422. XILINX_VDMA_REG_ENABLE_VERTICAL_FLIP) &
  2423. XILINX_VDMA_ENABLE_VERTICAL_FLIP;
  2424. }
  2425. if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA)
  2426. chan->ctrl_offset = XILINX_MCDMA_S2MM_CTRL_OFFSET;
  2427. else
  2428. chan->ctrl_offset = XILINX_DMA_S2MM_CTRL_OFFSET;
  2429. if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
  2430. chan->desc_offset = XILINX_VDMA_S2MM_DESC_OFFSET;
  2431. chan->config.park = 1;
  2432. if (xdev->flush_on_fsync == XILINX_DMA_FLUSH_BOTH ||
  2433. xdev->flush_on_fsync == XILINX_DMA_FLUSH_S2MM)
  2434. chan->flush_on_fsync = true;
  2435. }
  2436. } else {
  2437. dev_err(xdev->dev, "Invalid channel compatible node\n");
  2438. return -EINVAL;
  2439. }
  2440. /* Request the interrupt */
  2441. chan->irq = of_irq_get(node, chan->tdest);
  2442. if (chan->irq < 0)
  2443. return dev_err_probe(xdev->dev, chan->irq, "failed to get irq\n");
  2444. err = request_irq(chan->irq, xdev->dma_config->irq_handler,
  2445. IRQF_SHARED, "xilinx-dma-controller", chan);
  2446. if (err) {
  2447. dev_err(xdev->dev, "unable to request IRQ %d\n", chan->irq);
  2448. return err;
  2449. }
  2450. if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
  2451. chan->start_transfer = xilinx_dma_start_transfer;
  2452. chan->stop_transfer = xilinx_dma_stop_transfer;
  2453. } else if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
  2454. chan->start_transfer = xilinx_mcdma_start_transfer;
  2455. chan->stop_transfer = xilinx_dma_stop_transfer;
  2456. } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
  2457. chan->start_transfer = xilinx_cdma_start_transfer;
  2458. chan->stop_transfer = xilinx_cdma_stop_transfer;
  2459. } else {
  2460. chan->start_transfer = xilinx_vdma_start_transfer;
  2461. chan->stop_transfer = xilinx_dma_stop_transfer;
  2462. }
  2463. /* check if SG is enabled (only for AXIDMA, AXIMCDMA, and CDMA) */
  2464. if (xdev->dma_config->dmatype != XDMA_TYPE_VDMA) {
  2465. if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA ||
  2466. dma_ctrl_read(chan, XILINX_DMA_REG_DMASR) &
  2467. XILINX_DMA_DMASR_SG_MASK)
  2468. chan->has_sg = true;
  2469. dev_dbg(chan->dev, "ch %d: SG %s\n", chan->id,
  2470. chan->has_sg ? "enabled" : "disabled");
  2471. }
  2472. /* Initialize the tasklet */
  2473. tasklet_setup(&chan->tasklet, xilinx_dma_do_tasklet);
  2474. /*
  2475. * Initialize the DMA channel and add it to the DMA engine channels
  2476. * list.
  2477. */
  2478. chan->common.device = &xdev->common;
  2479. list_add_tail(&chan->common.device_node, &xdev->common.channels);
  2480. xdev->chan[chan->id] = chan;
  2481. /* Reset the channel */
  2482. err = xilinx_dma_chan_reset(chan);
  2483. if (err < 0) {
  2484. dev_err(xdev->dev, "Reset channel failed\n");
  2485. return err;
  2486. }
  2487. return 0;
  2488. }
  2489. /**
  2490. * xilinx_dma_child_probe - Per child node probe
  2491. * It get number of dma-channels per child node from
  2492. * device-tree and initializes all the channels.
  2493. *
  2494. * @xdev: Driver specific device structure
  2495. * @node: Device node
  2496. *
  2497. * Return: 0 always.
  2498. */
  2499. static int xilinx_dma_child_probe(struct xilinx_dma_device *xdev,
  2500. struct device_node *node)
  2501. {
  2502. int ret, i;
  2503. u32 nr_channels = 1;
  2504. ret = of_property_read_u32(node, "dma-channels", &nr_channels);
  2505. if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA && ret < 0)
  2506. dev_warn(xdev->dev, "missing dma-channels property\n");
  2507. for (i = 0; i < nr_channels; i++) {
  2508. ret = xilinx_dma_chan_probe(xdev, node);
  2509. if (ret)
  2510. return ret;
  2511. }
  2512. return 0;
  2513. }
  2514. /**
  2515. * of_dma_xilinx_xlate - Translation function
  2516. * @dma_spec: Pointer to DMA specifier as found in the device tree
  2517. * @ofdma: Pointer to DMA controller data
  2518. *
  2519. * Return: DMA channel pointer on success and NULL on error
  2520. */
  2521. static struct dma_chan *of_dma_xilinx_xlate(struct of_phandle_args *dma_spec,
  2522. struct of_dma *ofdma)
  2523. {
  2524. struct xilinx_dma_device *xdev = ofdma->of_dma_data;
  2525. int chan_id = dma_spec->args[0];
  2526. if (chan_id >= xdev->dma_config->max_channels || !xdev->chan[chan_id])
  2527. return NULL;
  2528. return dma_get_slave_channel(&xdev->chan[chan_id]->common);
  2529. }
  2530. static const struct xilinx_dma_config axidma_config = {
  2531. .dmatype = XDMA_TYPE_AXIDMA,
  2532. .clk_init = axidma_clk_init,
  2533. .irq_handler = xilinx_dma_irq_handler,
  2534. .max_channels = XILINX_DMA_MAX_CHANS_PER_DEVICE,
  2535. };
  2536. static const struct xilinx_dma_config aximcdma_config = {
  2537. .dmatype = XDMA_TYPE_AXIMCDMA,
  2538. .clk_init = axidma_clk_init,
  2539. .irq_handler = xilinx_mcdma_irq_handler,
  2540. .max_channels = XILINX_MCDMA_MAX_CHANS_PER_DEVICE,
  2541. };
  2542. static const struct xilinx_dma_config axicdma_config = {
  2543. .dmatype = XDMA_TYPE_CDMA,
  2544. .clk_init = axicdma_clk_init,
  2545. .irq_handler = xilinx_dma_irq_handler,
  2546. .max_channels = XILINX_CDMA_MAX_CHANS_PER_DEVICE,
  2547. };
  2548. static const struct xilinx_dma_config axivdma_config = {
  2549. .dmatype = XDMA_TYPE_VDMA,
  2550. .clk_init = axivdma_clk_init,
  2551. .irq_handler = xilinx_dma_irq_handler,
  2552. .max_channels = XILINX_DMA_MAX_CHANS_PER_DEVICE,
  2553. };
  2554. static const struct of_device_id xilinx_dma_of_ids[] = {
  2555. { .compatible = "xlnx,axi-dma-1.00.a", .data = &axidma_config },
  2556. { .compatible = "xlnx,axi-cdma-1.00.a", .data = &axicdma_config },
  2557. { .compatible = "xlnx,axi-vdma-1.00.a", .data = &axivdma_config },
  2558. { .compatible = "xlnx,axi-mcdma-1.00.a", .data = &aximcdma_config },
  2559. {}
  2560. };
  2561. MODULE_DEVICE_TABLE(of, xilinx_dma_of_ids);
  2562. /**
  2563. * xilinx_dma_probe - Driver probe function
  2564. * @pdev: Pointer to the platform_device structure
  2565. *
  2566. * Return: '0' on success and failure value on error
  2567. */
  2568. static int xilinx_dma_probe(struct platform_device *pdev)
  2569. {
  2570. int (*clk_init)(struct platform_device *, struct clk **, struct clk **,
  2571. struct clk **, struct clk **, struct clk **)
  2572. = axivdma_clk_init;
  2573. struct device_node *node = pdev->dev.of_node;
  2574. struct xilinx_dma_device *xdev;
  2575. struct device_node *child, *np = pdev->dev.of_node;
  2576. u32 num_frames, addr_width, len_width;
  2577. int i, err;
  2578. /* Allocate and initialize the DMA engine structure */
  2579. xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
  2580. if (!xdev)
  2581. return -ENOMEM;
  2582. xdev->dev = &pdev->dev;
  2583. if (np) {
  2584. const struct of_device_id *match;
  2585. match = of_match_node(xilinx_dma_of_ids, np);
  2586. if (match && match->data) {
  2587. xdev->dma_config = match->data;
  2588. clk_init = xdev->dma_config->clk_init;
  2589. }
  2590. }
  2591. err = clk_init(pdev, &xdev->axi_clk, &xdev->tx_clk, &xdev->txs_clk,
  2592. &xdev->rx_clk, &xdev->rxs_clk);
  2593. if (err)
  2594. return err;
  2595. /* Request and map I/O memory */
  2596. xdev->regs = devm_platform_ioremap_resource(pdev, 0);
  2597. if (IS_ERR(xdev->regs)) {
  2598. err = PTR_ERR(xdev->regs);
  2599. goto disable_clks;
  2600. }
  2601. /* Retrieve the DMA engine properties from the device tree */
  2602. xdev->max_buffer_len = GENMASK(XILINX_DMA_MAX_TRANS_LEN_MAX - 1, 0);
  2603. xdev->s2mm_chan_id = xdev->dma_config->max_channels / 2;
  2604. if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA ||
  2605. xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
  2606. if (!of_property_read_u32(node, "xlnx,sg-length-width",
  2607. &len_width)) {
  2608. if (len_width < XILINX_DMA_MAX_TRANS_LEN_MIN ||
  2609. len_width > XILINX_DMA_V2_MAX_TRANS_LEN_MAX) {
  2610. dev_warn(xdev->dev,
  2611. "invalid xlnx,sg-length-width property value. Using default width\n");
  2612. } else {
  2613. if (len_width > XILINX_DMA_MAX_TRANS_LEN_MAX)
  2614. dev_warn(xdev->dev, "Please ensure that IP supports buffer length > 23 bits\n");
  2615. xdev->max_buffer_len =
  2616. GENMASK(len_width - 1, 0);
  2617. }
  2618. }
  2619. }
  2620. if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
  2621. err = of_property_read_u32(node, "xlnx,num-fstores",
  2622. &num_frames);
  2623. if (err < 0) {
  2624. dev_err(xdev->dev,
  2625. "missing xlnx,num-fstores property\n");
  2626. goto disable_clks;
  2627. }
  2628. err = of_property_read_u32(node, "xlnx,flush-fsync",
  2629. &xdev->flush_on_fsync);
  2630. if (err < 0)
  2631. dev_warn(xdev->dev,
  2632. "missing xlnx,flush-fsync property\n");
  2633. }
  2634. err = of_property_read_u32(node, "xlnx,addrwidth", &addr_width);
  2635. if (err < 0)
  2636. dev_warn(xdev->dev, "missing xlnx,addrwidth property\n");
  2637. if (addr_width > 32)
  2638. xdev->ext_addr = true;
  2639. else
  2640. xdev->ext_addr = false;
  2641. /* Set the dma mask bits */
  2642. err = dma_set_mask_and_coherent(xdev->dev, DMA_BIT_MASK(addr_width));
  2643. if (err < 0) {
  2644. dev_err(xdev->dev, "DMA mask error %d\n", err);
  2645. goto disable_clks;
  2646. }
  2647. /* Initialize the DMA engine */
  2648. xdev->common.dev = &pdev->dev;
  2649. INIT_LIST_HEAD(&xdev->common.channels);
  2650. if (!(xdev->dma_config->dmatype == XDMA_TYPE_CDMA)) {
  2651. dma_cap_set(DMA_SLAVE, xdev->common.cap_mask);
  2652. dma_cap_set(DMA_PRIVATE, xdev->common.cap_mask);
  2653. }
  2654. xdev->common.device_alloc_chan_resources =
  2655. xilinx_dma_alloc_chan_resources;
  2656. xdev->common.device_free_chan_resources =
  2657. xilinx_dma_free_chan_resources;
  2658. xdev->common.device_terminate_all = xilinx_dma_terminate_all;
  2659. xdev->common.device_synchronize = xilinx_dma_synchronize;
  2660. xdev->common.device_tx_status = xilinx_dma_tx_status;
  2661. xdev->common.device_issue_pending = xilinx_dma_issue_pending;
  2662. xdev->common.device_config = xilinx_dma_device_config;
  2663. if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA) {
  2664. dma_cap_set(DMA_CYCLIC, xdev->common.cap_mask);
  2665. xdev->common.device_prep_slave_sg = xilinx_dma_prep_slave_sg;
  2666. xdev->common.device_prep_dma_cyclic =
  2667. xilinx_dma_prep_dma_cyclic;
  2668. /* Residue calculation is supported by only AXI DMA and CDMA */
  2669. xdev->common.residue_granularity =
  2670. DMA_RESIDUE_GRANULARITY_SEGMENT;
  2671. } else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA) {
  2672. dma_cap_set(DMA_MEMCPY, xdev->common.cap_mask);
  2673. xdev->common.device_prep_dma_memcpy = xilinx_cdma_prep_memcpy;
  2674. /* Residue calculation is supported by only AXI DMA and CDMA */
  2675. xdev->common.residue_granularity =
  2676. DMA_RESIDUE_GRANULARITY_SEGMENT;
  2677. } else if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA) {
  2678. xdev->common.device_prep_slave_sg = xilinx_mcdma_prep_slave_sg;
  2679. } else {
  2680. xdev->common.device_prep_interleaved_dma =
  2681. xilinx_vdma_dma_prep_interleaved;
  2682. }
  2683. platform_set_drvdata(pdev, xdev);
  2684. /* Initialize the channels */
  2685. for_each_child_of_node(node, child) {
  2686. err = xilinx_dma_child_probe(xdev, child);
  2687. if (err < 0) {
  2688. of_node_put(child);
  2689. goto error;
  2690. }
  2691. }
  2692. if (xdev->dma_config->dmatype == XDMA_TYPE_VDMA) {
  2693. for (i = 0; i < xdev->dma_config->max_channels; i++)
  2694. if (xdev->chan[i])
  2695. xdev->chan[i]->num_frms = num_frames;
  2696. }
  2697. /* Register the DMA engine with the core */
  2698. err = dma_async_device_register(&xdev->common);
  2699. if (err) {
  2700. dev_err(xdev->dev, "failed to register the dma device\n");
  2701. goto error;
  2702. }
  2703. err = of_dma_controller_register(node, of_dma_xilinx_xlate,
  2704. xdev);
  2705. if (err < 0) {
  2706. dev_err(&pdev->dev, "Unable to register DMA to DT\n");
  2707. dma_async_device_unregister(&xdev->common);
  2708. goto error;
  2709. }
  2710. if (xdev->dma_config->dmatype == XDMA_TYPE_AXIDMA)
  2711. dev_info(&pdev->dev, "Xilinx AXI DMA Engine Driver Probed!!\n");
  2712. else if (xdev->dma_config->dmatype == XDMA_TYPE_CDMA)
  2713. dev_info(&pdev->dev, "Xilinx AXI CDMA Engine Driver Probed!!\n");
  2714. else if (xdev->dma_config->dmatype == XDMA_TYPE_AXIMCDMA)
  2715. dev_info(&pdev->dev, "Xilinx AXI MCDMA Engine Driver Probed!!\n");
  2716. else
  2717. dev_info(&pdev->dev, "Xilinx AXI VDMA Engine Driver Probed!!\n");
  2718. return 0;
  2719. error:
  2720. for (i = 0; i < xdev->dma_config->max_channels; i++)
  2721. if (xdev->chan[i])
  2722. xilinx_dma_chan_remove(xdev->chan[i]);
  2723. disable_clks:
  2724. xdma_disable_allclks(xdev);
  2725. return err;
  2726. }
  2727. /**
  2728. * xilinx_dma_remove - Driver remove function
  2729. * @pdev: Pointer to the platform_device structure
  2730. *
  2731. * Return: Always '0'
  2732. */
  2733. static int xilinx_dma_remove(struct platform_device *pdev)
  2734. {
  2735. struct xilinx_dma_device *xdev = platform_get_drvdata(pdev);
  2736. int i;
  2737. of_dma_controller_free(pdev->dev.of_node);
  2738. dma_async_device_unregister(&xdev->common);
  2739. for (i = 0; i < xdev->dma_config->max_channels; i++)
  2740. if (xdev->chan[i])
  2741. xilinx_dma_chan_remove(xdev->chan[i]);
  2742. xdma_disable_allclks(xdev);
  2743. return 0;
  2744. }
  2745. static struct platform_driver xilinx_vdma_driver = {
  2746. .driver = {
  2747. .name = "xilinx-vdma",
  2748. .of_match_table = xilinx_dma_of_ids,
  2749. },
  2750. .probe = xilinx_dma_probe,
  2751. .remove = xilinx_dma_remove,
  2752. };
  2753. module_platform_driver(xilinx_vdma_driver);
  2754. MODULE_AUTHOR("Xilinx, Inc.");
  2755. MODULE_DESCRIPTION("Xilinx VDMA driver");
  2756. MODULE_LICENSE("GPL v2");