xilinx_dma.c 73 KB

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