xilinx_dma.c 72 KB

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