xilinx_dma.c 74 KB

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