xilinx_dma.c 72 KB

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