xilinx_dma.c 73 KB

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