xilinx_dma.c 75 KB

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