at_xdmac.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. /*
  2. * Driver for the Atmel Extensible DMA Controller (aka XDMAC on AT91 systems)
  3. *
  4. * Copyright (C) 2014 Atmel Corporation
  5. *
  6. * Author: Ludovic Desroches <ludovic.desroches@atmel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <asm/barrier.h>
  21. #include <dt-bindings/dma/at91.h>
  22. #include <linux/clk.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/dmapool.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/kernel.h>
  28. #include <linux/list.h>
  29. #include <linux/module.h>
  30. #include <linux/of_dma.h>
  31. #include <linux/of_platform.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/pm.h>
  34. #include "dmaengine.h"
  35. /* Global registers */
  36. #define AT_XDMAC_GTYPE 0x00 /* Global Type Register */
  37. #define AT_XDMAC_NB_CH(i) (((i) & 0x1F) + 1) /* Number of Channels Minus One */
  38. #define AT_XDMAC_FIFO_SZ(i) (((i) >> 5) & 0x7FF) /* Number of Bytes */
  39. #define AT_XDMAC_NB_REQ(i) ((((i) >> 16) & 0x3F) + 1) /* Number of Peripheral Requests Minus One */
  40. #define AT_XDMAC_GCFG 0x04 /* Global Configuration Register */
  41. #define AT_XDMAC_GWAC 0x08 /* Global Weighted Arbiter Configuration Register */
  42. #define AT_XDMAC_GIE 0x0C /* Global Interrupt Enable Register */
  43. #define AT_XDMAC_GID 0x10 /* Global Interrupt Disable Register */
  44. #define AT_XDMAC_GIM 0x14 /* Global Interrupt Mask Register */
  45. #define AT_XDMAC_GIS 0x18 /* Global Interrupt Status Register */
  46. #define AT_XDMAC_GE 0x1C /* Global Channel Enable Register */
  47. #define AT_XDMAC_GD 0x20 /* Global Channel Disable Register */
  48. #define AT_XDMAC_GS 0x24 /* Global Channel Status Register */
  49. #define AT_XDMAC_GRS 0x28 /* Global Channel Read Suspend Register */
  50. #define AT_XDMAC_GWS 0x2C /* Global Write Suspend Register */
  51. #define AT_XDMAC_GRWS 0x30 /* Global Channel Read Write Suspend Register */
  52. #define AT_XDMAC_GRWR 0x34 /* Global Channel Read Write Resume Register */
  53. #define AT_XDMAC_GSWR 0x38 /* Global Channel Software Request Register */
  54. #define AT_XDMAC_GSWS 0x3C /* Global channel Software Request Status Register */
  55. #define AT_XDMAC_GSWF 0x40 /* Global Channel Software Flush Request Register */
  56. #define AT_XDMAC_VERSION 0xFFC /* XDMAC Version Register */
  57. /* Channel relative registers offsets */
  58. #define AT_XDMAC_CIE 0x00 /* Channel Interrupt Enable Register */
  59. #define AT_XDMAC_CIE_BIE BIT(0) /* End of Block Interrupt Enable Bit */
  60. #define AT_XDMAC_CIE_LIE BIT(1) /* End of Linked List Interrupt Enable Bit */
  61. #define AT_XDMAC_CIE_DIE BIT(2) /* End of Disable Interrupt Enable Bit */
  62. #define AT_XDMAC_CIE_FIE BIT(3) /* End of Flush Interrupt Enable Bit */
  63. #define AT_XDMAC_CIE_RBEIE BIT(4) /* Read Bus Error Interrupt Enable Bit */
  64. #define AT_XDMAC_CIE_WBEIE BIT(5) /* Write Bus Error Interrupt Enable Bit */
  65. #define AT_XDMAC_CIE_ROIE BIT(6) /* Request Overflow Interrupt Enable Bit */
  66. #define AT_XDMAC_CID 0x04 /* Channel Interrupt Disable Register */
  67. #define AT_XDMAC_CID_BID BIT(0) /* End of Block Interrupt Disable Bit */
  68. #define AT_XDMAC_CID_LID BIT(1) /* End of Linked List Interrupt Disable Bit */
  69. #define AT_XDMAC_CID_DID BIT(2) /* End of Disable Interrupt Disable Bit */
  70. #define AT_XDMAC_CID_FID BIT(3) /* End of Flush Interrupt Disable Bit */
  71. #define AT_XDMAC_CID_RBEID BIT(4) /* Read Bus Error Interrupt Disable Bit */
  72. #define AT_XDMAC_CID_WBEID BIT(5) /* Write Bus Error Interrupt Disable Bit */
  73. #define AT_XDMAC_CID_ROID BIT(6) /* Request Overflow Interrupt Disable Bit */
  74. #define AT_XDMAC_CIM 0x08 /* Channel Interrupt Mask Register */
  75. #define AT_XDMAC_CIM_BIM BIT(0) /* End of Block Interrupt Mask Bit */
  76. #define AT_XDMAC_CIM_LIM BIT(1) /* End of Linked List Interrupt Mask Bit */
  77. #define AT_XDMAC_CIM_DIM BIT(2) /* End of Disable Interrupt Mask Bit */
  78. #define AT_XDMAC_CIM_FIM BIT(3) /* End of Flush Interrupt Mask Bit */
  79. #define AT_XDMAC_CIM_RBEIM BIT(4) /* Read Bus Error Interrupt Mask Bit */
  80. #define AT_XDMAC_CIM_WBEIM BIT(5) /* Write Bus Error Interrupt Mask Bit */
  81. #define AT_XDMAC_CIM_ROIM BIT(6) /* Request Overflow Interrupt Mask Bit */
  82. #define AT_XDMAC_CIS 0x0C /* Channel Interrupt Status Register */
  83. #define AT_XDMAC_CIS_BIS BIT(0) /* End of Block Interrupt Status Bit */
  84. #define AT_XDMAC_CIS_LIS BIT(1) /* End of Linked List Interrupt Status Bit */
  85. #define AT_XDMAC_CIS_DIS BIT(2) /* End of Disable Interrupt Status Bit */
  86. #define AT_XDMAC_CIS_FIS BIT(3) /* End of Flush Interrupt Status Bit */
  87. #define AT_XDMAC_CIS_RBEIS BIT(4) /* Read Bus Error Interrupt Status Bit */
  88. #define AT_XDMAC_CIS_WBEIS BIT(5) /* Write Bus Error Interrupt Status Bit */
  89. #define AT_XDMAC_CIS_ROIS BIT(6) /* Request Overflow Interrupt Status Bit */
  90. #define AT_XDMAC_CSA 0x10 /* Channel Source Address Register */
  91. #define AT_XDMAC_CDA 0x14 /* Channel Destination Address Register */
  92. #define AT_XDMAC_CNDA 0x18 /* Channel Next Descriptor Address Register */
  93. #define AT_XDMAC_CNDA_NDAIF(i) ((i) & 0x1) /* Channel x Next Descriptor Interface */
  94. #define AT_XDMAC_CNDA_NDA(i) ((i) & 0xfffffffc) /* Channel x Next Descriptor Address */
  95. #define AT_XDMAC_CNDC 0x1C /* Channel Next Descriptor Control Register */
  96. #define AT_XDMAC_CNDC_NDE (0x1 << 0) /* Channel x Next Descriptor Enable */
  97. #define AT_XDMAC_CNDC_NDSUP (0x1 << 1) /* Channel x Next Descriptor Source Update */
  98. #define AT_XDMAC_CNDC_NDDUP (0x1 << 2) /* Channel x Next Descriptor Destination Update */
  99. #define AT_XDMAC_CNDC_NDVIEW_NDV0 (0x0 << 3) /* Channel x Next Descriptor View 0 */
  100. #define AT_XDMAC_CNDC_NDVIEW_NDV1 (0x1 << 3) /* Channel x Next Descriptor View 1 */
  101. #define AT_XDMAC_CNDC_NDVIEW_NDV2 (0x2 << 3) /* Channel x Next Descriptor View 2 */
  102. #define AT_XDMAC_CNDC_NDVIEW_NDV3 (0x3 << 3) /* Channel x Next Descriptor View 3 */
  103. #define AT_XDMAC_CUBC 0x20 /* Channel Microblock Control Register */
  104. #define AT_XDMAC_CBC 0x24 /* Channel Block Control Register */
  105. #define AT_XDMAC_CC 0x28 /* Channel Configuration Register */
  106. #define AT_XDMAC_CC_TYPE (0x1 << 0) /* Channel Transfer Type */
  107. #define AT_XDMAC_CC_TYPE_MEM_TRAN (0x0 << 0) /* Memory to Memory Transfer */
  108. #define AT_XDMAC_CC_TYPE_PER_TRAN (0x1 << 0) /* Peripheral to Memory or Memory to Peripheral Transfer */
  109. #define AT_XDMAC_CC_MBSIZE_MASK (0x3 << 1)
  110. #define AT_XDMAC_CC_MBSIZE_SINGLE (0x0 << 1)
  111. #define AT_XDMAC_CC_MBSIZE_FOUR (0x1 << 1)
  112. #define AT_XDMAC_CC_MBSIZE_EIGHT (0x2 << 1)
  113. #define AT_XDMAC_CC_MBSIZE_SIXTEEN (0x3 << 1)
  114. #define AT_XDMAC_CC_DSYNC (0x1 << 4) /* Channel Synchronization */
  115. #define AT_XDMAC_CC_DSYNC_PER2MEM (0x0 << 4)
  116. #define AT_XDMAC_CC_DSYNC_MEM2PER (0x1 << 4)
  117. #define AT_XDMAC_CC_PROT (0x1 << 5) /* Channel Protection */
  118. #define AT_XDMAC_CC_PROT_SEC (0x0 << 5)
  119. #define AT_XDMAC_CC_PROT_UNSEC (0x1 << 5)
  120. #define AT_XDMAC_CC_SWREQ (0x1 << 6) /* Channel Software Request Trigger */
  121. #define AT_XDMAC_CC_SWREQ_HWR_CONNECTED (0x0 << 6)
  122. #define AT_XDMAC_CC_SWREQ_SWR_CONNECTED (0x1 << 6)
  123. #define AT_XDMAC_CC_MEMSET (0x1 << 7) /* Channel Fill Block of memory */
  124. #define AT_XDMAC_CC_MEMSET_NORMAL_MODE (0x0 << 7)
  125. #define AT_XDMAC_CC_MEMSET_HW_MODE (0x1 << 7)
  126. #define AT_XDMAC_CC_CSIZE(i) ((0x7 & (i)) << 8) /* Channel Chunk Size */
  127. #define AT_XDMAC_CC_DWIDTH_OFFSET 11
  128. #define AT_XDMAC_CC_DWIDTH_MASK (0x3 << AT_XDMAC_CC_DWIDTH_OFFSET)
  129. #define AT_XDMAC_CC_DWIDTH(i) ((0x3 & (i)) << AT_XDMAC_CC_DWIDTH_OFFSET) /* Channel Data Width */
  130. #define AT_XDMAC_CC_DWIDTH_BYTE 0x0
  131. #define AT_XDMAC_CC_DWIDTH_HALFWORD 0x1
  132. #define AT_XDMAC_CC_DWIDTH_WORD 0x2
  133. #define AT_XDMAC_CC_DWIDTH_DWORD 0x3
  134. #define AT_XDMAC_CC_SIF(i) ((0x1 & (i)) << 13) /* Channel Source Interface Identifier */
  135. #define AT_XDMAC_CC_DIF(i) ((0x1 & (i)) << 14) /* Channel Destination Interface Identifier */
  136. #define AT_XDMAC_CC_SAM_MASK (0x3 << 16) /* Channel Source Addressing Mode */
  137. #define AT_XDMAC_CC_SAM_FIXED_AM (0x0 << 16)
  138. #define AT_XDMAC_CC_SAM_INCREMENTED_AM (0x1 << 16)
  139. #define AT_XDMAC_CC_SAM_UBS_AM (0x2 << 16)
  140. #define AT_XDMAC_CC_SAM_UBS_DS_AM (0x3 << 16)
  141. #define AT_XDMAC_CC_DAM_MASK (0x3 << 18) /* Channel Source Addressing Mode */
  142. #define AT_XDMAC_CC_DAM_FIXED_AM (0x0 << 18)
  143. #define AT_XDMAC_CC_DAM_INCREMENTED_AM (0x1 << 18)
  144. #define AT_XDMAC_CC_DAM_UBS_AM (0x2 << 18)
  145. #define AT_XDMAC_CC_DAM_UBS_DS_AM (0x3 << 18)
  146. #define AT_XDMAC_CC_INITD (0x1 << 21) /* Channel Initialization Terminated (read only) */
  147. #define AT_XDMAC_CC_INITD_TERMINATED (0x0 << 21)
  148. #define AT_XDMAC_CC_INITD_IN_PROGRESS (0x1 << 21)
  149. #define AT_XDMAC_CC_RDIP (0x1 << 22) /* Read in Progress (read only) */
  150. #define AT_XDMAC_CC_RDIP_DONE (0x0 << 22)
  151. #define AT_XDMAC_CC_RDIP_IN_PROGRESS (0x1 << 22)
  152. #define AT_XDMAC_CC_WRIP (0x1 << 23) /* Write in Progress (read only) */
  153. #define AT_XDMAC_CC_WRIP_DONE (0x0 << 23)
  154. #define AT_XDMAC_CC_WRIP_IN_PROGRESS (0x1 << 23)
  155. #define AT_XDMAC_CC_PERID(i) (0x7f & (i) << 24) /* Channel Peripheral Identifier */
  156. #define AT_XDMAC_CDS_MSP 0x2C /* Channel Data Stride Memory Set Pattern */
  157. #define AT_XDMAC_CSUS 0x30 /* Channel Source Microblock Stride */
  158. #define AT_XDMAC_CDUS 0x34 /* Channel Destination Microblock Stride */
  159. #define AT_XDMAC_CHAN_REG_BASE 0x50 /* Channel registers base address */
  160. /* Microblock control members */
  161. #define AT_XDMAC_MBR_UBC_UBLEN_MAX 0xFFFFFFUL /* Maximum Microblock Length */
  162. #define AT_XDMAC_MBR_UBC_NDE (0x1 << 24) /* Next Descriptor Enable */
  163. #define AT_XDMAC_MBR_UBC_NSEN (0x1 << 25) /* Next Descriptor Source Update */
  164. #define AT_XDMAC_MBR_UBC_NDEN (0x1 << 26) /* Next Descriptor Destination Update */
  165. #define AT_XDMAC_MBR_UBC_NDV0 (0x0 << 27) /* Next Descriptor View 0 */
  166. #define AT_XDMAC_MBR_UBC_NDV1 (0x1 << 27) /* Next Descriptor View 1 */
  167. #define AT_XDMAC_MBR_UBC_NDV2 (0x2 << 27) /* Next Descriptor View 2 */
  168. #define AT_XDMAC_MBR_UBC_NDV3 (0x3 << 27) /* Next Descriptor View 3 */
  169. #define AT_XDMAC_MAX_CHAN 0x20
  170. #define AT_XDMAC_MAX_CSIZE 16 /* 16 data */
  171. #define AT_XDMAC_MAX_DWIDTH 8 /* 64 bits */
  172. #define AT_XDMAC_RESIDUE_MAX_RETRIES 5
  173. #define AT_XDMAC_DMA_BUSWIDTHS\
  174. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) |\
  175. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |\
  176. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |\
  177. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |\
  178. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  179. enum atc_status {
  180. AT_XDMAC_CHAN_IS_CYCLIC = 0,
  181. AT_XDMAC_CHAN_IS_PAUSED,
  182. };
  183. /* ----- Channels ----- */
  184. struct at_xdmac_chan {
  185. struct dma_chan chan;
  186. void __iomem *ch_regs;
  187. u32 mask; /* Channel Mask */
  188. u32 cfg; /* Channel Configuration Register */
  189. u8 perid; /* Peripheral ID */
  190. u8 perif; /* Peripheral Interface */
  191. u8 memif; /* Memory Interface */
  192. u32 save_cc;
  193. u32 save_cim;
  194. u32 save_cnda;
  195. u32 save_cndc;
  196. unsigned long status;
  197. struct tasklet_struct tasklet;
  198. struct dma_slave_config sconfig;
  199. spinlock_t lock;
  200. struct list_head xfers_list;
  201. struct list_head free_descs_list;
  202. };
  203. /* ----- Controller ----- */
  204. struct at_xdmac {
  205. struct dma_device dma;
  206. void __iomem *regs;
  207. int irq;
  208. struct clk *clk;
  209. u32 save_gim;
  210. u32 save_gs;
  211. struct dma_pool *at_xdmac_desc_pool;
  212. struct at_xdmac_chan chan[0];
  213. };
  214. /* ----- Descriptors ----- */
  215. /* Linked List Descriptor */
  216. struct at_xdmac_lld {
  217. dma_addr_t mbr_nda; /* Next Descriptor Member */
  218. u32 mbr_ubc; /* Microblock Control Member */
  219. dma_addr_t mbr_sa; /* Source Address Member */
  220. dma_addr_t mbr_da; /* Destination Address Member */
  221. u32 mbr_cfg; /* Configuration Register */
  222. u32 mbr_bc; /* Block Control Register */
  223. u32 mbr_ds; /* Data Stride Register */
  224. u32 mbr_sus; /* Source Microblock Stride Register */
  225. u32 mbr_dus; /* Destination Microblock Stride Register */
  226. };
  227. struct at_xdmac_desc {
  228. struct at_xdmac_lld lld;
  229. enum dma_transfer_direction direction;
  230. struct dma_async_tx_descriptor tx_dma_desc;
  231. struct list_head desc_node;
  232. /* Following members are only used by the first descriptor */
  233. bool active_xfer;
  234. unsigned int xfer_size;
  235. struct list_head descs_list;
  236. struct list_head xfer_node;
  237. };
  238. static inline void __iomem *at_xdmac_chan_reg_base(struct at_xdmac *atxdmac, unsigned int chan_nb)
  239. {
  240. return atxdmac->regs + (AT_XDMAC_CHAN_REG_BASE + chan_nb * 0x40);
  241. }
  242. #define at_xdmac_read(atxdmac, reg) readl_relaxed((atxdmac)->regs + (reg))
  243. #define at_xdmac_write(atxdmac, reg, value) \
  244. writel_relaxed((value), (atxdmac)->regs + (reg))
  245. #define at_xdmac_chan_read(atchan, reg) readl_relaxed((atchan)->ch_regs + (reg))
  246. #define at_xdmac_chan_write(atchan, reg, value) writel_relaxed((value), (atchan)->ch_regs + (reg))
  247. static inline struct at_xdmac_chan *to_at_xdmac_chan(struct dma_chan *dchan)
  248. {
  249. return container_of(dchan, struct at_xdmac_chan, chan);
  250. }
  251. static struct device *chan2dev(struct dma_chan *chan)
  252. {
  253. return &chan->dev->device;
  254. }
  255. static inline struct at_xdmac *to_at_xdmac(struct dma_device *ddev)
  256. {
  257. return container_of(ddev, struct at_xdmac, dma);
  258. }
  259. static inline struct at_xdmac_desc *txd_to_at_desc(struct dma_async_tx_descriptor *txd)
  260. {
  261. return container_of(txd, struct at_xdmac_desc, tx_dma_desc);
  262. }
  263. static inline int at_xdmac_chan_is_cyclic(struct at_xdmac_chan *atchan)
  264. {
  265. return test_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  266. }
  267. static inline int at_xdmac_chan_is_paused(struct at_xdmac_chan *atchan)
  268. {
  269. return test_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  270. }
  271. static inline int at_xdmac_csize(u32 maxburst)
  272. {
  273. int csize;
  274. csize = ffs(maxburst) - 1;
  275. if (csize > 4)
  276. csize = -EINVAL;
  277. return csize;
  278. };
  279. static inline u8 at_xdmac_get_dwidth(u32 cfg)
  280. {
  281. return (cfg & AT_XDMAC_CC_DWIDTH_MASK) >> AT_XDMAC_CC_DWIDTH_OFFSET;
  282. };
  283. static unsigned int init_nr_desc_per_channel = 64;
  284. module_param(init_nr_desc_per_channel, uint, 0644);
  285. MODULE_PARM_DESC(init_nr_desc_per_channel,
  286. "initial descriptors per channel (default: 64)");
  287. static bool at_xdmac_chan_is_enabled(struct at_xdmac_chan *atchan)
  288. {
  289. return at_xdmac_chan_read(atchan, AT_XDMAC_GS) & atchan->mask;
  290. }
  291. static void at_xdmac_off(struct at_xdmac *atxdmac)
  292. {
  293. at_xdmac_write(atxdmac, AT_XDMAC_GD, -1L);
  294. /* Wait that all chans are disabled. */
  295. while (at_xdmac_read(atxdmac, AT_XDMAC_GS))
  296. cpu_relax();
  297. at_xdmac_write(atxdmac, AT_XDMAC_GID, -1L);
  298. }
  299. /* Call with lock hold. */
  300. static void at_xdmac_start_xfer(struct at_xdmac_chan *atchan,
  301. struct at_xdmac_desc *first)
  302. {
  303. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  304. u32 reg;
  305. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, first);
  306. if (at_xdmac_chan_is_enabled(atchan))
  307. return;
  308. /* Set transfer as active to not try to start it again. */
  309. first->active_xfer = true;
  310. /* Tell xdmac where to get the first descriptor. */
  311. reg = AT_XDMAC_CNDA_NDA(first->tx_dma_desc.phys)
  312. | AT_XDMAC_CNDA_NDAIF(atchan->memif);
  313. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, reg);
  314. /*
  315. * When doing non cyclic transfer we need to use the next
  316. * descriptor view 2 since some fields of the configuration register
  317. * depend on transfer size and src/dest addresses.
  318. */
  319. if (at_xdmac_chan_is_cyclic(atchan))
  320. reg = AT_XDMAC_CNDC_NDVIEW_NDV1;
  321. else if (first->lld.mbr_ubc & AT_XDMAC_MBR_UBC_NDV3)
  322. reg = AT_XDMAC_CNDC_NDVIEW_NDV3;
  323. else
  324. reg = AT_XDMAC_CNDC_NDVIEW_NDV2;
  325. /*
  326. * Even if the register will be updated from the configuration in the
  327. * descriptor when using view 2 or higher, the PROT bit won't be set
  328. * properly. This bit can be modified only by using the channel
  329. * configuration register.
  330. */
  331. at_xdmac_chan_write(atchan, AT_XDMAC_CC, first->lld.mbr_cfg);
  332. reg |= AT_XDMAC_CNDC_NDDUP
  333. | AT_XDMAC_CNDC_NDSUP
  334. | AT_XDMAC_CNDC_NDE;
  335. at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, reg);
  336. dev_vdbg(chan2dev(&atchan->chan),
  337. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  338. __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  339. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  340. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  341. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  342. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  343. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  344. at_xdmac_chan_write(atchan, AT_XDMAC_CID, 0xffffffff);
  345. reg = AT_XDMAC_CIE_RBEIE | AT_XDMAC_CIE_WBEIE | AT_XDMAC_CIE_ROIE;
  346. /*
  347. * There is no end of list when doing cyclic dma, we need to get
  348. * an interrupt after each periods.
  349. */
  350. if (at_xdmac_chan_is_cyclic(atchan))
  351. at_xdmac_chan_write(atchan, AT_XDMAC_CIE,
  352. reg | AT_XDMAC_CIE_BIE);
  353. else
  354. at_xdmac_chan_write(atchan, AT_XDMAC_CIE,
  355. reg | AT_XDMAC_CIE_LIE);
  356. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atchan->mask);
  357. dev_vdbg(chan2dev(&atchan->chan),
  358. "%s: enable channel (0x%08x)\n", __func__, atchan->mask);
  359. wmb();
  360. at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
  361. dev_vdbg(chan2dev(&atchan->chan),
  362. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  363. __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  364. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  365. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  366. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  367. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  368. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  369. }
  370. static dma_cookie_t at_xdmac_tx_submit(struct dma_async_tx_descriptor *tx)
  371. {
  372. struct at_xdmac_desc *desc = txd_to_at_desc(tx);
  373. struct at_xdmac_chan *atchan = to_at_xdmac_chan(tx->chan);
  374. dma_cookie_t cookie;
  375. unsigned long irqflags;
  376. spin_lock_irqsave(&atchan->lock, irqflags);
  377. cookie = dma_cookie_assign(tx);
  378. dev_vdbg(chan2dev(tx->chan), "%s: atchan 0x%p, add desc 0x%p to xfers_list\n",
  379. __func__, atchan, desc);
  380. list_add_tail(&desc->xfer_node, &atchan->xfers_list);
  381. if (list_is_singular(&atchan->xfers_list))
  382. at_xdmac_start_xfer(atchan, desc);
  383. spin_unlock_irqrestore(&atchan->lock, irqflags);
  384. return cookie;
  385. }
  386. static struct at_xdmac_desc *at_xdmac_alloc_desc(struct dma_chan *chan,
  387. gfp_t gfp_flags)
  388. {
  389. struct at_xdmac_desc *desc;
  390. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  391. dma_addr_t phys;
  392. desc = dma_pool_alloc(atxdmac->at_xdmac_desc_pool, gfp_flags, &phys);
  393. if (desc) {
  394. memset(desc, 0, sizeof(*desc));
  395. INIT_LIST_HEAD(&desc->descs_list);
  396. dma_async_tx_descriptor_init(&desc->tx_dma_desc, chan);
  397. desc->tx_dma_desc.tx_submit = at_xdmac_tx_submit;
  398. desc->tx_dma_desc.phys = phys;
  399. }
  400. return desc;
  401. }
  402. void at_xdmac_init_used_desc(struct at_xdmac_desc *desc)
  403. {
  404. memset(&desc->lld, 0, sizeof(desc->lld));
  405. INIT_LIST_HEAD(&desc->descs_list);
  406. desc->direction = DMA_TRANS_NONE;
  407. desc->xfer_size = 0;
  408. desc->active_xfer = false;
  409. }
  410. /* Call must be protected by lock. */
  411. static struct at_xdmac_desc *at_xdmac_get_desc(struct at_xdmac_chan *atchan)
  412. {
  413. struct at_xdmac_desc *desc;
  414. if (list_empty(&atchan->free_descs_list)) {
  415. desc = at_xdmac_alloc_desc(&atchan->chan, GFP_NOWAIT);
  416. } else {
  417. desc = list_first_entry(&atchan->free_descs_list,
  418. struct at_xdmac_desc, desc_node);
  419. list_del(&desc->desc_node);
  420. at_xdmac_init_used_desc(desc);
  421. }
  422. return desc;
  423. }
  424. static void at_xdmac_queue_desc(struct dma_chan *chan,
  425. struct at_xdmac_desc *prev,
  426. struct at_xdmac_desc *desc)
  427. {
  428. if (!prev || !desc)
  429. return;
  430. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  431. prev->lld.mbr_ubc |= AT_XDMAC_MBR_UBC_NDE;
  432. dev_dbg(chan2dev(chan), "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  433. __func__, prev, &prev->lld.mbr_nda);
  434. }
  435. static inline void at_xdmac_increment_block_count(struct dma_chan *chan,
  436. struct at_xdmac_desc *desc)
  437. {
  438. if (!desc)
  439. return;
  440. desc->lld.mbr_bc++;
  441. dev_dbg(chan2dev(chan),
  442. "%s: incrementing the block count of the desc 0x%p\n",
  443. __func__, desc);
  444. }
  445. static struct dma_chan *at_xdmac_xlate(struct of_phandle_args *dma_spec,
  446. struct of_dma *of_dma)
  447. {
  448. struct at_xdmac *atxdmac = of_dma->of_dma_data;
  449. struct at_xdmac_chan *atchan;
  450. struct dma_chan *chan;
  451. struct device *dev = atxdmac->dma.dev;
  452. if (dma_spec->args_count != 1) {
  453. dev_err(dev, "dma phandler args: bad number of args\n");
  454. return NULL;
  455. }
  456. chan = dma_get_any_slave_channel(&atxdmac->dma);
  457. if (!chan) {
  458. dev_err(dev, "can't get a dma channel\n");
  459. return NULL;
  460. }
  461. atchan = to_at_xdmac_chan(chan);
  462. atchan->memif = AT91_XDMAC_DT_GET_MEM_IF(dma_spec->args[0]);
  463. atchan->perif = AT91_XDMAC_DT_GET_PER_IF(dma_spec->args[0]);
  464. atchan->perid = AT91_XDMAC_DT_GET_PERID(dma_spec->args[0]);
  465. dev_dbg(dev, "chan dt cfg: memif=%u perif=%u perid=%u\n",
  466. atchan->memif, atchan->perif, atchan->perid);
  467. return chan;
  468. }
  469. static int at_xdmac_compute_chan_conf(struct dma_chan *chan,
  470. enum dma_transfer_direction direction)
  471. {
  472. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  473. int csize, dwidth;
  474. if (direction == DMA_DEV_TO_MEM) {
  475. atchan->cfg =
  476. AT91_XDMAC_DT_PERID(atchan->perid)
  477. | AT_XDMAC_CC_DAM_INCREMENTED_AM
  478. | AT_XDMAC_CC_SAM_FIXED_AM
  479. | AT_XDMAC_CC_DIF(atchan->memif)
  480. | AT_XDMAC_CC_SIF(atchan->perif)
  481. | AT_XDMAC_CC_SWREQ_HWR_CONNECTED
  482. | AT_XDMAC_CC_DSYNC_PER2MEM
  483. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  484. | AT_XDMAC_CC_TYPE_PER_TRAN;
  485. csize = ffs(atchan->sconfig.src_maxburst) - 1;
  486. if (csize < 0) {
  487. dev_err(chan2dev(chan), "invalid src maxburst value\n");
  488. return -EINVAL;
  489. }
  490. atchan->cfg |= AT_XDMAC_CC_CSIZE(csize);
  491. dwidth = ffs(atchan->sconfig.src_addr_width) - 1;
  492. if (dwidth < 0) {
  493. dev_err(chan2dev(chan), "invalid src addr width value\n");
  494. return -EINVAL;
  495. }
  496. atchan->cfg |= AT_XDMAC_CC_DWIDTH(dwidth);
  497. } else if (direction == DMA_MEM_TO_DEV) {
  498. atchan->cfg =
  499. AT91_XDMAC_DT_PERID(atchan->perid)
  500. | AT_XDMAC_CC_DAM_FIXED_AM
  501. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  502. | AT_XDMAC_CC_DIF(atchan->perif)
  503. | AT_XDMAC_CC_SIF(atchan->memif)
  504. | AT_XDMAC_CC_SWREQ_HWR_CONNECTED
  505. | AT_XDMAC_CC_DSYNC_MEM2PER
  506. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  507. | AT_XDMAC_CC_TYPE_PER_TRAN;
  508. csize = ffs(atchan->sconfig.dst_maxburst) - 1;
  509. if (csize < 0) {
  510. dev_err(chan2dev(chan), "invalid src maxburst value\n");
  511. return -EINVAL;
  512. }
  513. atchan->cfg |= AT_XDMAC_CC_CSIZE(csize);
  514. dwidth = ffs(atchan->sconfig.dst_addr_width) - 1;
  515. if (dwidth < 0) {
  516. dev_err(chan2dev(chan), "invalid dst addr width value\n");
  517. return -EINVAL;
  518. }
  519. atchan->cfg |= AT_XDMAC_CC_DWIDTH(dwidth);
  520. }
  521. dev_dbg(chan2dev(chan), "%s: cfg=0x%08x\n", __func__, atchan->cfg);
  522. return 0;
  523. }
  524. /*
  525. * Only check that maxburst and addr width values are supported by the
  526. * the controller but not that the configuration is good to perform the
  527. * transfer since we don't know the direction at this stage.
  528. */
  529. static int at_xdmac_check_slave_config(struct dma_slave_config *sconfig)
  530. {
  531. if ((sconfig->src_maxburst > AT_XDMAC_MAX_CSIZE)
  532. || (sconfig->dst_maxburst > AT_XDMAC_MAX_CSIZE))
  533. return -EINVAL;
  534. if ((sconfig->src_addr_width > AT_XDMAC_MAX_DWIDTH)
  535. || (sconfig->dst_addr_width > AT_XDMAC_MAX_DWIDTH))
  536. return -EINVAL;
  537. return 0;
  538. }
  539. static int at_xdmac_set_slave_config(struct dma_chan *chan,
  540. struct dma_slave_config *sconfig)
  541. {
  542. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  543. if (at_xdmac_check_slave_config(sconfig)) {
  544. dev_err(chan2dev(chan), "invalid slave configuration\n");
  545. return -EINVAL;
  546. }
  547. memcpy(&atchan->sconfig, sconfig, sizeof(atchan->sconfig));
  548. return 0;
  549. }
  550. static struct dma_async_tx_descriptor *
  551. at_xdmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  552. unsigned int sg_len, enum dma_transfer_direction direction,
  553. unsigned long flags, void *context)
  554. {
  555. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  556. struct at_xdmac_desc *first = NULL, *prev = NULL;
  557. struct scatterlist *sg;
  558. int i;
  559. unsigned int xfer_size = 0;
  560. unsigned long irqflags;
  561. struct dma_async_tx_descriptor *ret = NULL;
  562. if (!sgl)
  563. return NULL;
  564. if (!is_slave_direction(direction)) {
  565. dev_err(chan2dev(chan), "invalid DMA direction\n");
  566. return NULL;
  567. }
  568. dev_dbg(chan2dev(chan), "%s: sg_len=%d, dir=%s, flags=0x%lx\n",
  569. __func__, sg_len,
  570. direction == DMA_MEM_TO_DEV ? "to device" : "from device",
  571. flags);
  572. /* Protect dma_sconfig field that can be modified by set_slave_conf. */
  573. spin_lock_irqsave(&atchan->lock, irqflags);
  574. if (at_xdmac_compute_chan_conf(chan, direction))
  575. goto spin_unlock;
  576. /* Prepare descriptors. */
  577. for_each_sg(sgl, sg, sg_len, i) {
  578. struct at_xdmac_desc *desc = NULL;
  579. u32 len, mem, dwidth, fixed_dwidth;
  580. len = sg_dma_len(sg);
  581. mem = sg_dma_address(sg);
  582. if (unlikely(!len)) {
  583. dev_err(chan2dev(chan), "sg data length is zero\n");
  584. goto spin_unlock;
  585. }
  586. dev_dbg(chan2dev(chan), "%s: * sg%d len=%u, mem=0x%08x\n",
  587. __func__, i, len, mem);
  588. desc = at_xdmac_get_desc(atchan);
  589. if (!desc) {
  590. dev_err(chan2dev(chan), "can't get descriptor\n");
  591. if (first)
  592. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  593. goto spin_unlock;
  594. }
  595. /* Linked list descriptor setup. */
  596. if (direction == DMA_DEV_TO_MEM) {
  597. desc->lld.mbr_sa = atchan->sconfig.src_addr;
  598. desc->lld.mbr_da = mem;
  599. } else {
  600. desc->lld.mbr_sa = mem;
  601. desc->lld.mbr_da = atchan->sconfig.dst_addr;
  602. }
  603. dwidth = at_xdmac_get_dwidth(atchan->cfg);
  604. fixed_dwidth = IS_ALIGNED(len, 1 << dwidth)
  605. ? dwidth
  606. : AT_XDMAC_CC_DWIDTH_BYTE;
  607. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2 /* next descriptor view */
  608. | AT_XDMAC_MBR_UBC_NDEN /* next descriptor dst parameter update */
  609. | AT_XDMAC_MBR_UBC_NSEN /* next descriptor src parameter update */
  610. | (len >> fixed_dwidth); /* microblock length */
  611. desc->lld.mbr_cfg = (atchan->cfg & ~AT_XDMAC_CC_DWIDTH_MASK) |
  612. AT_XDMAC_CC_DWIDTH(fixed_dwidth);
  613. dev_dbg(chan2dev(chan),
  614. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  615. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  616. /* Chain lld. */
  617. if (prev)
  618. at_xdmac_queue_desc(chan, prev, desc);
  619. prev = desc;
  620. if (!first)
  621. first = desc;
  622. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  623. __func__, desc, first);
  624. list_add_tail(&desc->desc_node, &first->descs_list);
  625. xfer_size += len;
  626. }
  627. first->tx_dma_desc.flags = flags;
  628. first->xfer_size = xfer_size;
  629. first->direction = direction;
  630. ret = &first->tx_dma_desc;
  631. spin_unlock:
  632. spin_unlock_irqrestore(&atchan->lock, irqflags);
  633. return ret;
  634. }
  635. static struct dma_async_tx_descriptor *
  636. at_xdmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
  637. size_t buf_len, size_t period_len,
  638. enum dma_transfer_direction direction,
  639. unsigned long flags)
  640. {
  641. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  642. struct at_xdmac_desc *first = NULL, *prev = NULL;
  643. unsigned int periods = buf_len / period_len;
  644. int i;
  645. unsigned long irqflags;
  646. dev_dbg(chan2dev(chan), "%s: buf_addr=%pad, buf_len=%zd, period_len=%zd, dir=%s, flags=0x%lx\n",
  647. __func__, &buf_addr, buf_len, period_len,
  648. direction == DMA_MEM_TO_DEV ? "mem2per" : "per2mem", flags);
  649. if (!is_slave_direction(direction)) {
  650. dev_err(chan2dev(chan), "invalid DMA direction\n");
  651. return NULL;
  652. }
  653. if (test_and_set_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status)) {
  654. dev_err(chan2dev(chan), "channel currently used\n");
  655. return NULL;
  656. }
  657. if (at_xdmac_compute_chan_conf(chan, direction))
  658. return NULL;
  659. for (i = 0; i < periods; i++) {
  660. struct at_xdmac_desc *desc = NULL;
  661. spin_lock_irqsave(&atchan->lock, irqflags);
  662. desc = at_xdmac_get_desc(atchan);
  663. if (!desc) {
  664. dev_err(chan2dev(chan), "can't get descriptor\n");
  665. if (first)
  666. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  667. spin_unlock_irqrestore(&atchan->lock, irqflags);
  668. return NULL;
  669. }
  670. spin_unlock_irqrestore(&atchan->lock, irqflags);
  671. dev_dbg(chan2dev(chan),
  672. "%s: desc=0x%p, tx_dma_desc.phys=%pad\n",
  673. __func__, desc, &desc->tx_dma_desc.phys);
  674. if (direction == DMA_DEV_TO_MEM) {
  675. desc->lld.mbr_sa = atchan->sconfig.src_addr;
  676. desc->lld.mbr_da = buf_addr + i * period_len;
  677. } else {
  678. desc->lld.mbr_sa = buf_addr + i * period_len;
  679. desc->lld.mbr_da = atchan->sconfig.dst_addr;
  680. }
  681. desc->lld.mbr_cfg = atchan->cfg;
  682. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV1
  683. | AT_XDMAC_MBR_UBC_NDEN
  684. | AT_XDMAC_MBR_UBC_NSEN
  685. | period_len >> at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  686. dev_dbg(chan2dev(chan),
  687. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  688. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  689. /* Chain lld. */
  690. if (prev)
  691. at_xdmac_queue_desc(chan, prev, desc);
  692. prev = desc;
  693. if (!first)
  694. first = desc;
  695. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  696. __func__, desc, first);
  697. list_add_tail(&desc->desc_node, &first->descs_list);
  698. }
  699. at_xdmac_queue_desc(chan, prev, first);
  700. first->tx_dma_desc.flags = flags;
  701. first->xfer_size = buf_len;
  702. first->direction = direction;
  703. return &first->tx_dma_desc;
  704. }
  705. static inline u32 at_xdmac_align_width(struct dma_chan *chan, dma_addr_t addr)
  706. {
  707. u32 width;
  708. /*
  709. * Check address alignment to select the greater data width we
  710. * can use.
  711. *
  712. * Some XDMAC implementations don't provide dword transfer, in
  713. * this case selecting dword has the same behavior as
  714. * selecting word transfers.
  715. */
  716. if (!(addr & 7)) {
  717. width = AT_XDMAC_CC_DWIDTH_DWORD;
  718. dev_dbg(chan2dev(chan), "%s: dwidth: double word\n", __func__);
  719. } else if (!(addr & 3)) {
  720. width = AT_XDMAC_CC_DWIDTH_WORD;
  721. dev_dbg(chan2dev(chan), "%s: dwidth: word\n", __func__);
  722. } else if (!(addr & 1)) {
  723. width = AT_XDMAC_CC_DWIDTH_HALFWORD;
  724. dev_dbg(chan2dev(chan), "%s: dwidth: half word\n", __func__);
  725. } else {
  726. width = AT_XDMAC_CC_DWIDTH_BYTE;
  727. dev_dbg(chan2dev(chan), "%s: dwidth: byte\n", __func__);
  728. }
  729. return width;
  730. }
  731. static struct at_xdmac_desc *
  732. at_xdmac_interleaved_queue_desc(struct dma_chan *chan,
  733. struct at_xdmac_chan *atchan,
  734. struct at_xdmac_desc *prev,
  735. dma_addr_t src, dma_addr_t dst,
  736. struct dma_interleaved_template *xt,
  737. struct data_chunk *chunk)
  738. {
  739. struct at_xdmac_desc *desc;
  740. u32 dwidth;
  741. unsigned long flags;
  742. size_t ublen;
  743. /*
  744. * WARNING: The channel configuration is set here since there is no
  745. * dmaengine_slave_config call in this case. Moreover we don't know the
  746. * direction, it involves we can't dynamically set the source and dest
  747. * interface so we have to use the same one. Only interface 0 allows EBI
  748. * access. Hopefully we can access DDR through both ports (at least on
  749. * SAMA5D4x), so we can use the same interface for source and dest,
  750. * that solves the fact we don't know the direction.
  751. * ERRATA: Even if useless for memory transfers, the PERID has to not
  752. * match the one of another channel. If not, it could lead to spurious
  753. * flag status.
  754. */
  755. u32 chan_cc = AT_XDMAC_CC_PERID(0x3f)
  756. | AT_XDMAC_CC_DIF(0)
  757. | AT_XDMAC_CC_SIF(0)
  758. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  759. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  760. dwidth = at_xdmac_align_width(chan, src | dst | chunk->size);
  761. if (chunk->size >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) {
  762. dev_dbg(chan2dev(chan),
  763. "%s: chunk too big (%d, max size %lu)...\n",
  764. __func__, chunk->size,
  765. AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth);
  766. return NULL;
  767. }
  768. if (prev)
  769. dev_dbg(chan2dev(chan),
  770. "Adding items at the end of desc 0x%p\n", prev);
  771. if (xt->src_inc) {
  772. if (xt->src_sgl)
  773. chan_cc |= AT_XDMAC_CC_SAM_UBS_AM;
  774. else
  775. chan_cc |= AT_XDMAC_CC_SAM_INCREMENTED_AM;
  776. }
  777. if (xt->dst_inc) {
  778. if (xt->dst_sgl)
  779. chan_cc |= AT_XDMAC_CC_DAM_UBS_AM;
  780. else
  781. chan_cc |= AT_XDMAC_CC_DAM_INCREMENTED_AM;
  782. }
  783. spin_lock_irqsave(&atchan->lock, flags);
  784. desc = at_xdmac_get_desc(atchan);
  785. spin_unlock_irqrestore(&atchan->lock, flags);
  786. if (!desc) {
  787. dev_err(chan2dev(chan), "can't get descriptor\n");
  788. return NULL;
  789. }
  790. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  791. ublen = chunk->size >> dwidth;
  792. desc->lld.mbr_sa = src;
  793. desc->lld.mbr_da = dst;
  794. desc->lld.mbr_sus = dmaengine_get_src_icg(xt, chunk);
  795. desc->lld.mbr_dus = dmaengine_get_dst_icg(xt, chunk);
  796. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3
  797. | AT_XDMAC_MBR_UBC_NDEN
  798. | AT_XDMAC_MBR_UBC_NSEN
  799. | ublen;
  800. desc->lld.mbr_cfg = chan_cc;
  801. dev_dbg(chan2dev(chan),
  802. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  803. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da,
  804. desc->lld.mbr_ubc, desc->lld.mbr_cfg);
  805. /* Chain lld. */
  806. if (prev)
  807. at_xdmac_queue_desc(chan, prev, desc);
  808. return desc;
  809. }
  810. static struct dma_async_tx_descriptor *
  811. at_xdmac_prep_interleaved(struct dma_chan *chan,
  812. struct dma_interleaved_template *xt,
  813. unsigned long flags)
  814. {
  815. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  816. struct at_xdmac_desc *prev = NULL, *first = NULL;
  817. dma_addr_t dst_addr, src_addr;
  818. size_t src_skip = 0, dst_skip = 0, len = 0;
  819. struct data_chunk *chunk;
  820. int i;
  821. if (!xt || !xt->numf || (xt->dir != DMA_MEM_TO_MEM))
  822. return NULL;
  823. /*
  824. * TODO: Handle the case where we have to repeat a chain of
  825. * descriptors...
  826. */
  827. if ((xt->numf > 1) && (xt->frame_size > 1))
  828. return NULL;
  829. dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, numf=%d, frame_size=%d, flags=0x%lx\n",
  830. __func__, &xt->src_start, &xt->dst_start, xt->numf,
  831. xt->frame_size, flags);
  832. src_addr = xt->src_start;
  833. dst_addr = xt->dst_start;
  834. if (xt->numf > 1) {
  835. first = at_xdmac_interleaved_queue_desc(chan, atchan,
  836. NULL,
  837. src_addr, dst_addr,
  838. xt, xt->sgl);
  839. /* Length of the block is (BLEN+1) microblocks. */
  840. for (i = 0; i < xt->numf - 1; i++)
  841. at_xdmac_increment_block_count(chan, first);
  842. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  843. __func__, first, first);
  844. list_add_tail(&first->desc_node, &first->descs_list);
  845. } else {
  846. for (i = 0; i < xt->frame_size; i++) {
  847. size_t src_icg = 0, dst_icg = 0;
  848. struct at_xdmac_desc *desc;
  849. chunk = xt->sgl + i;
  850. dst_icg = dmaengine_get_dst_icg(xt, chunk);
  851. src_icg = dmaengine_get_src_icg(xt, chunk);
  852. src_skip = chunk->size + src_icg;
  853. dst_skip = chunk->size + dst_icg;
  854. dev_dbg(chan2dev(chan),
  855. "%s: chunk size=%d, src icg=%d, dst icg=%d\n",
  856. __func__, chunk->size, src_icg, dst_icg);
  857. desc = at_xdmac_interleaved_queue_desc(chan, atchan,
  858. prev,
  859. src_addr, dst_addr,
  860. xt, chunk);
  861. if (!desc) {
  862. list_splice_init(&first->descs_list,
  863. &atchan->free_descs_list);
  864. return NULL;
  865. }
  866. if (!first)
  867. first = desc;
  868. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  869. __func__, desc, first);
  870. list_add_tail(&desc->desc_node, &first->descs_list);
  871. if (xt->src_sgl)
  872. src_addr += src_skip;
  873. if (xt->dst_sgl)
  874. dst_addr += dst_skip;
  875. len += chunk->size;
  876. prev = desc;
  877. }
  878. }
  879. first->tx_dma_desc.cookie = -EBUSY;
  880. first->tx_dma_desc.flags = flags;
  881. first->xfer_size = len;
  882. return &first->tx_dma_desc;
  883. }
  884. static struct dma_async_tx_descriptor *
  885. at_xdmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  886. size_t len, unsigned long flags)
  887. {
  888. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  889. struct at_xdmac_desc *first = NULL, *prev = NULL;
  890. size_t remaining_size = len, xfer_size = 0, ublen;
  891. dma_addr_t src_addr = src, dst_addr = dest;
  892. u32 dwidth;
  893. /*
  894. * WARNING: We don't know the direction, it involves we can't
  895. * dynamically set the source and dest interface so we have to use the
  896. * same one. Only interface 0 allows EBI access. Hopefully we can
  897. * access DDR through both ports (at least on SAMA5D4x), so we can use
  898. * the same interface for source and dest, that solves the fact we
  899. * don't know the direction.
  900. * ERRATA: Even if useless for memory transfers, the PERID has to not
  901. * match the one of another channel. If not, it could lead to spurious
  902. * flag status.
  903. */
  904. u32 chan_cc = AT_XDMAC_CC_PERID(0x3f)
  905. | AT_XDMAC_CC_DAM_INCREMENTED_AM
  906. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  907. | AT_XDMAC_CC_DIF(0)
  908. | AT_XDMAC_CC_SIF(0)
  909. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  910. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  911. unsigned long irqflags;
  912. dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, len=%zd, flags=0x%lx\n",
  913. __func__, &src, &dest, len, flags);
  914. if (unlikely(!len))
  915. return NULL;
  916. dwidth = at_xdmac_align_width(chan, src_addr | dst_addr);
  917. /* Prepare descriptors. */
  918. while (remaining_size) {
  919. struct at_xdmac_desc *desc = NULL;
  920. dev_dbg(chan2dev(chan), "%s: remaining_size=%zu\n", __func__, remaining_size);
  921. spin_lock_irqsave(&atchan->lock, irqflags);
  922. desc = at_xdmac_get_desc(atchan);
  923. spin_unlock_irqrestore(&atchan->lock, irqflags);
  924. if (!desc) {
  925. dev_err(chan2dev(chan), "can't get descriptor\n");
  926. if (first)
  927. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  928. return NULL;
  929. }
  930. /* Update src and dest addresses. */
  931. src_addr += xfer_size;
  932. dst_addr += xfer_size;
  933. if (remaining_size >= AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)
  934. xfer_size = AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth;
  935. else
  936. xfer_size = remaining_size;
  937. dev_dbg(chan2dev(chan), "%s: xfer_size=%zu\n", __func__, xfer_size);
  938. /* Check remaining length and change data width if needed. */
  939. dwidth = at_xdmac_align_width(chan,
  940. src_addr | dst_addr | xfer_size);
  941. chan_cc &= ~AT_XDMAC_CC_DWIDTH_MASK;
  942. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  943. ublen = xfer_size >> dwidth;
  944. remaining_size -= xfer_size;
  945. desc->lld.mbr_sa = src_addr;
  946. desc->lld.mbr_da = dst_addr;
  947. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2
  948. | AT_XDMAC_MBR_UBC_NDEN
  949. | AT_XDMAC_MBR_UBC_NSEN
  950. | ublen;
  951. desc->lld.mbr_cfg = chan_cc;
  952. dev_dbg(chan2dev(chan),
  953. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  954. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc, desc->lld.mbr_cfg);
  955. /* Chain lld. */
  956. if (prev)
  957. at_xdmac_queue_desc(chan, prev, desc);
  958. prev = desc;
  959. if (!first)
  960. first = desc;
  961. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  962. __func__, desc, first);
  963. list_add_tail(&desc->desc_node, &first->descs_list);
  964. }
  965. first->tx_dma_desc.flags = flags;
  966. first->xfer_size = len;
  967. return &first->tx_dma_desc;
  968. }
  969. static struct at_xdmac_desc *at_xdmac_memset_create_desc(struct dma_chan *chan,
  970. struct at_xdmac_chan *atchan,
  971. dma_addr_t dst_addr,
  972. size_t len,
  973. int value)
  974. {
  975. struct at_xdmac_desc *desc;
  976. unsigned long flags;
  977. size_t ublen;
  978. u32 dwidth;
  979. /*
  980. * WARNING: The channel configuration is set here since there is no
  981. * dmaengine_slave_config call in this case. Moreover we don't know the
  982. * direction, it involves we can't dynamically set the source and dest
  983. * interface so we have to use the same one. Only interface 0 allows EBI
  984. * access. Hopefully we can access DDR through both ports (at least on
  985. * SAMA5D4x), so we can use the same interface for source and dest,
  986. * that solves the fact we don't know the direction.
  987. * ERRATA: Even if useless for memory transfers, the PERID has to not
  988. * match the one of another channel. If not, it could lead to spurious
  989. * flag status.
  990. */
  991. u32 chan_cc = AT_XDMAC_CC_PERID(0x3f)
  992. | AT_XDMAC_CC_DAM_UBS_AM
  993. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  994. | AT_XDMAC_CC_DIF(0)
  995. | AT_XDMAC_CC_SIF(0)
  996. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  997. | AT_XDMAC_CC_MEMSET_HW_MODE
  998. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  999. dwidth = at_xdmac_align_width(chan, dst_addr);
  1000. if (len >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) {
  1001. dev_err(chan2dev(chan),
  1002. "%s: Transfer too large, aborting...\n",
  1003. __func__);
  1004. return NULL;
  1005. }
  1006. spin_lock_irqsave(&atchan->lock, flags);
  1007. desc = at_xdmac_get_desc(atchan);
  1008. spin_unlock_irqrestore(&atchan->lock, flags);
  1009. if (!desc) {
  1010. dev_err(chan2dev(chan), "can't get descriptor\n");
  1011. return NULL;
  1012. }
  1013. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  1014. ublen = len >> dwidth;
  1015. desc->lld.mbr_da = dst_addr;
  1016. desc->lld.mbr_ds = value;
  1017. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3
  1018. | AT_XDMAC_MBR_UBC_NDEN
  1019. | AT_XDMAC_MBR_UBC_NSEN
  1020. | ublen;
  1021. desc->lld.mbr_cfg = chan_cc;
  1022. dev_dbg(chan2dev(chan),
  1023. "%s: lld: mbr_da=%pad, mbr_ds=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  1024. __func__, &desc->lld.mbr_da, &desc->lld.mbr_ds, desc->lld.mbr_ubc,
  1025. desc->lld.mbr_cfg);
  1026. return desc;
  1027. }
  1028. struct dma_async_tx_descriptor *
  1029. at_xdmac_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
  1030. size_t len, unsigned long flags)
  1031. {
  1032. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1033. struct at_xdmac_desc *desc;
  1034. dev_dbg(chan2dev(chan), "%s: dest=%pad, len=%d, pattern=0x%x, flags=0x%lx\n",
  1035. __func__, &dest, len, value, flags);
  1036. if (unlikely(!len))
  1037. return NULL;
  1038. desc = at_xdmac_memset_create_desc(chan, atchan, dest, len, value);
  1039. list_add_tail(&desc->desc_node, &desc->descs_list);
  1040. desc->tx_dma_desc.cookie = -EBUSY;
  1041. desc->tx_dma_desc.flags = flags;
  1042. desc->xfer_size = len;
  1043. return &desc->tx_dma_desc;
  1044. }
  1045. static struct dma_async_tx_descriptor *
  1046. at_xdmac_prep_dma_memset_sg(struct dma_chan *chan, struct scatterlist *sgl,
  1047. unsigned int sg_len, int value,
  1048. unsigned long flags)
  1049. {
  1050. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1051. struct at_xdmac_desc *desc, *pdesc = NULL,
  1052. *ppdesc = NULL, *first = NULL;
  1053. struct scatterlist *sg, *psg = NULL, *ppsg = NULL;
  1054. size_t stride = 0, pstride = 0, len = 0;
  1055. int i;
  1056. if (!sgl)
  1057. return NULL;
  1058. dev_dbg(chan2dev(chan), "%s: sg_len=%d, value=0x%x, flags=0x%lx\n",
  1059. __func__, sg_len, value, flags);
  1060. /* Prepare descriptors. */
  1061. for_each_sg(sgl, sg, sg_len, i) {
  1062. dev_dbg(chan2dev(chan), "%s: dest=%pad, len=%d, pattern=0x%x, flags=0x%lx\n",
  1063. __func__, &sg_dma_address(sg), sg_dma_len(sg),
  1064. value, flags);
  1065. desc = at_xdmac_memset_create_desc(chan, atchan,
  1066. sg_dma_address(sg),
  1067. sg_dma_len(sg),
  1068. value);
  1069. if (!desc && first)
  1070. list_splice_init(&first->descs_list,
  1071. &atchan->free_descs_list);
  1072. if (!first)
  1073. first = desc;
  1074. /* Update our strides */
  1075. pstride = stride;
  1076. if (psg)
  1077. stride = sg_dma_address(sg) -
  1078. (sg_dma_address(psg) + sg_dma_len(psg));
  1079. /*
  1080. * The scatterlist API gives us only the address and
  1081. * length of each elements.
  1082. *
  1083. * Unfortunately, we don't have the stride, which we
  1084. * will need to compute.
  1085. *
  1086. * That make us end up in a situation like this one:
  1087. * len stride len stride len
  1088. * +-------+ +-------+ +-------+
  1089. * | N-2 | | N-1 | | N |
  1090. * +-------+ +-------+ +-------+
  1091. *
  1092. * We need all these three elements (N-2, N-1 and N)
  1093. * to actually take the decision on whether we need to
  1094. * queue N-1 or reuse N-2.
  1095. *
  1096. * We will only consider N if it is the last element.
  1097. */
  1098. if (ppdesc && pdesc) {
  1099. if ((stride == pstride) &&
  1100. (sg_dma_len(ppsg) == sg_dma_len(psg))) {
  1101. dev_dbg(chan2dev(chan),
  1102. "%s: desc 0x%p can be merged with desc 0x%p\n",
  1103. __func__, pdesc, ppdesc);
  1104. /*
  1105. * Increment the block count of the
  1106. * N-2 descriptor
  1107. */
  1108. at_xdmac_increment_block_count(chan, ppdesc);
  1109. ppdesc->lld.mbr_dus = stride;
  1110. /*
  1111. * Put back the N-1 descriptor in the
  1112. * free descriptor list
  1113. */
  1114. list_add_tail(&pdesc->desc_node,
  1115. &atchan->free_descs_list);
  1116. /*
  1117. * Make our N-1 descriptor pointer
  1118. * point to the N-2 since they were
  1119. * actually merged.
  1120. */
  1121. pdesc = ppdesc;
  1122. /*
  1123. * Rule out the case where we don't have
  1124. * pstride computed yet (our second sg
  1125. * element)
  1126. *
  1127. * We also want to catch the case where there
  1128. * would be a negative stride,
  1129. */
  1130. } else if (pstride ||
  1131. sg_dma_address(sg) < sg_dma_address(psg)) {
  1132. /*
  1133. * Queue the N-1 descriptor after the
  1134. * N-2
  1135. */
  1136. at_xdmac_queue_desc(chan, ppdesc, pdesc);
  1137. /*
  1138. * Add the N-1 descriptor to the list
  1139. * of the descriptors used for this
  1140. * transfer
  1141. */
  1142. list_add_tail(&desc->desc_node,
  1143. &first->descs_list);
  1144. dev_dbg(chan2dev(chan),
  1145. "%s: add desc 0x%p to descs_list 0x%p\n",
  1146. __func__, desc, first);
  1147. }
  1148. }
  1149. /*
  1150. * If we are the last element, just see if we have the
  1151. * same size than the previous element.
  1152. *
  1153. * If so, we can merge it with the previous descriptor
  1154. * since we don't care about the stride anymore.
  1155. */
  1156. if ((i == (sg_len - 1)) &&
  1157. sg_dma_len(psg) == sg_dma_len(sg)) {
  1158. dev_dbg(chan2dev(chan),
  1159. "%s: desc 0x%p can be merged with desc 0x%p\n",
  1160. __func__, desc, pdesc);
  1161. /*
  1162. * Increment the block count of the N-1
  1163. * descriptor
  1164. */
  1165. at_xdmac_increment_block_count(chan, pdesc);
  1166. pdesc->lld.mbr_dus = stride;
  1167. /*
  1168. * Put back the N descriptor in the free
  1169. * descriptor list
  1170. */
  1171. list_add_tail(&desc->desc_node,
  1172. &atchan->free_descs_list);
  1173. }
  1174. /* Update our descriptors */
  1175. ppdesc = pdesc;
  1176. pdesc = desc;
  1177. /* Update our scatter pointers */
  1178. ppsg = psg;
  1179. psg = sg;
  1180. len += sg_dma_len(sg);
  1181. }
  1182. first->tx_dma_desc.cookie = -EBUSY;
  1183. first->tx_dma_desc.flags = flags;
  1184. first->xfer_size = len;
  1185. return &first->tx_dma_desc;
  1186. }
  1187. static enum dma_status
  1188. at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  1189. struct dma_tx_state *txstate)
  1190. {
  1191. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1192. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1193. struct at_xdmac_desc *desc, *_desc;
  1194. struct list_head *descs_list;
  1195. enum dma_status ret;
  1196. int residue, retry;
  1197. u32 cur_nda, check_nda, cur_ubc, mask, value;
  1198. u8 dwidth = 0;
  1199. unsigned long flags;
  1200. ret = dma_cookie_status(chan, cookie, txstate);
  1201. if (ret == DMA_COMPLETE)
  1202. return ret;
  1203. if (!txstate)
  1204. return ret;
  1205. spin_lock_irqsave(&atchan->lock, flags);
  1206. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  1207. /*
  1208. * If the transfer has not been started yet, don't need to compute the
  1209. * residue, it's the transfer length.
  1210. */
  1211. if (!desc->active_xfer) {
  1212. dma_set_residue(txstate, desc->xfer_size);
  1213. goto spin_unlock;
  1214. }
  1215. residue = desc->xfer_size;
  1216. /*
  1217. * Flush FIFO: only relevant when the transfer is source peripheral
  1218. * synchronized.
  1219. */
  1220. mask = AT_XDMAC_CC_TYPE | AT_XDMAC_CC_DSYNC;
  1221. value = AT_XDMAC_CC_TYPE_PER_TRAN | AT_XDMAC_CC_DSYNC_PER2MEM;
  1222. if ((desc->lld.mbr_cfg & mask) == value) {
  1223. at_xdmac_write(atxdmac, AT_XDMAC_GSWF, atchan->mask);
  1224. while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
  1225. cpu_relax();
  1226. }
  1227. /*
  1228. * When processing the residue, we need to read two registers but we
  1229. * can't do it in an atomic way. AT_XDMAC_CNDA is used to find where
  1230. * we stand in the descriptor list and AT_XDMAC_CUBC is used
  1231. * to know how many data are remaining for the current descriptor.
  1232. * Since the dma channel is not paused to not loose data, between the
  1233. * AT_XDMAC_CNDA and AT_XDMAC_CUBC read, we may have change of
  1234. * descriptor.
  1235. * For that reason, after reading AT_XDMAC_CUBC, we check if we are
  1236. * still using the same descriptor by reading a second time
  1237. * AT_XDMAC_CNDA. If AT_XDMAC_CNDA has changed, it means we have to
  1238. * read again AT_XDMAC_CUBC.
  1239. * Memory barriers are used to ensure the read order of the registers.
  1240. * A max number of retries is set because unlikely it can never ends if
  1241. * we are transferring a lot of data with small buffers.
  1242. */
  1243. cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  1244. rmb();
  1245. cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC);
  1246. for (retry = 0; retry < AT_XDMAC_RESIDUE_MAX_RETRIES; retry++) {
  1247. rmb();
  1248. check_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  1249. if (likely(cur_nda == check_nda))
  1250. break;
  1251. cur_nda = check_nda;
  1252. rmb();
  1253. cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC);
  1254. }
  1255. if (unlikely(retry >= AT_XDMAC_RESIDUE_MAX_RETRIES)) {
  1256. ret = DMA_ERROR;
  1257. goto spin_unlock;
  1258. }
  1259. /*
  1260. * Remove size of all microblocks already transferred and the current
  1261. * one. Then add the remaining size to transfer of the current
  1262. * microblock.
  1263. */
  1264. descs_list = &desc->descs_list;
  1265. list_for_each_entry_safe(desc, _desc, descs_list, desc_node) {
  1266. dwidth = at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  1267. residue -= (desc->lld.mbr_ubc & 0xffffff) << dwidth;
  1268. if ((desc->lld.mbr_nda & 0xfffffffc) == cur_nda)
  1269. break;
  1270. }
  1271. residue += cur_ubc << dwidth;
  1272. dma_set_residue(txstate, residue);
  1273. dev_dbg(chan2dev(chan),
  1274. "%s: desc=0x%p, tx_dma_desc.phys=%pad, tx_status=%d, cookie=%d, residue=%d\n",
  1275. __func__, desc, &desc->tx_dma_desc.phys, ret, cookie, residue);
  1276. spin_unlock:
  1277. spin_unlock_irqrestore(&atchan->lock, flags);
  1278. return ret;
  1279. }
  1280. /* Call must be protected by lock. */
  1281. static void at_xdmac_remove_xfer(struct at_xdmac_chan *atchan,
  1282. struct at_xdmac_desc *desc)
  1283. {
  1284. dev_dbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1285. /*
  1286. * Remove the transfer from the transfer list then move the transfer
  1287. * descriptors into the free descriptors list.
  1288. */
  1289. list_del(&desc->xfer_node);
  1290. list_splice_init(&desc->descs_list, &atchan->free_descs_list);
  1291. }
  1292. static void at_xdmac_advance_work(struct at_xdmac_chan *atchan)
  1293. {
  1294. struct at_xdmac_desc *desc;
  1295. unsigned long flags;
  1296. spin_lock_irqsave(&atchan->lock, flags);
  1297. /*
  1298. * If channel is enabled, do nothing, advance_work will be triggered
  1299. * after the interruption.
  1300. */
  1301. if (!at_xdmac_chan_is_enabled(atchan) && !list_empty(&atchan->xfers_list)) {
  1302. desc = list_first_entry(&atchan->xfers_list,
  1303. struct at_xdmac_desc,
  1304. xfer_node);
  1305. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1306. if (!desc->active_xfer)
  1307. at_xdmac_start_xfer(atchan, desc);
  1308. }
  1309. spin_unlock_irqrestore(&atchan->lock, flags);
  1310. }
  1311. static void at_xdmac_handle_cyclic(struct at_xdmac_chan *atchan)
  1312. {
  1313. struct at_xdmac_desc *desc;
  1314. struct dma_async_tx_descriptor *txd;
  1315. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  1316. txd = &desc->tx_dma_desc;
  1317. if (txd->callback && (txd->flags & DMA_PREP_INTERRUPT))
  1318. txd->callback(txd->callback_param);
  1319. }
  1320. static void at_xdmac_tasklet(unsigned long data)
  1321. {
  1322. struct at_xdmac_chan *atchan = (struct at_xdmac_chan *)data;
  1323. struct at_xdmac_desc *desc;
  1324. u32 error_mask;
  1325. dev_dbg(chan2dev(&atchan->chan), "%s: status=0x%08lx\n",
  1326. __func__, atchan->status);
  1327. error_mask = AT_XDMAC_CIS_RBEIS
  1328. | AT_XDMAC_CIS_WBEIS
  1329. | AT_XDMAC_CIS_ROIS;
  1330. if (at_xdmac_chan_is_cyclic(atchan)) {
  1331. at_xdmac_handle_cyclic(atchan);
  1332. } else if ((atchan->status & AT_XDMAC_CIS_LIS)
  1333. || (atchan->status & error_mask)) {
  1334. struct dma_async_tx_descriptor *txd;
  1335. if (atchan->status & AT_XDMAC_CIS_RBEIS)
  1336. dev_err(chan2dev(&atchan->chan), "read bus error!!!");
  1337. if (atchan->status & AT_XDMAC_CIS_WBEIS)
  1338. dev_err(chan2dev(&atchan->chan), "write bus error!!!");
  1339. if (atchan->status & AT_XDMAC_CIS_ROIS)
  1340. dev_err(chan2dev(&atchan->chan), "request overflow error!!!");
  1341. spin_lock_bh(&atchan->lock);
  1342. desc = list_first_entry(&atchan->xfers_list,
  1343. struct at_xdmac_desc,
  1344. xfer_node);
  1345. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1346. BUG_ON(!desc->active_xfer);
  1347. txd = &desc->tx_dma_desc;
  1348. at_xdmac_remove_xfer(atchan, desc);
  1349. spin_unlock_bh(&atchan->lock);
  1350. if (!at_xdmac_chan_is_cyclic(atchan)) {
  1351. dma_cookie_complete(txd);
  1352. if (txd->callback && (txd->flags & DMA_PREP_INTERRUPT))
  1353. txd->callback(txd->callback_param);
  1354. }
  1355. dma_run_dependencies(txd);
  1356. at_xdmac_advance_work(atchan);
  1357. }
  1358. }
  1359. static irqreturn_t at_xdmac_interrupt(int irq, void *dev_id)
  1360. {
  1361. struct at_xdmac *atxdmac = (struct at_xdmac *)dev_id;
  1362. struct at_xdmac_chan *atchan;
  1363. u32 imr, status, pending;
  1364. u32 chan_imr, chan_status;
  1365. int i, ret = IRQ_NONE;
  1366. do {
  1367. imr = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1368. status = at_xdmac_read(atxdmac, AT_XDMAC_GIS);
  1369. pending = status & imr;
  1370. dev_vdbg(atxdmac->dma.dev,
  1371. "%s: status=0x%08x, imr=0x%08x, pending=0x%08x\n",
  1372. __func__, status, imr, pending);
  1373. if (!pending)
  1374. break;
  1375. /* We have to find which channel has generated the interrupt. */
  1376. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1377. if (!((1 << i) & pending))
  1378. continue;
  1379. atchan = &atxdmac->chan[i];
  1380. chan_imr = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1381. chan_status = at_xdmac_chan_read(atchan, AT_XDMAC_CIS);
  1382. atchan->status = chan_status & chan_imr;
  1383. dev_vdbg(atxdmac->dma.dev,
  1384. "%s: chan%d: imr=0x%x, status=0x%x\n",
  1385. __func__, i, chan_imr, chan_status);
  1386. dev_vdbg(chan2dev(&atchan->chan),
  1387. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  1388. __func__,
  1389. at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  1390. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  1391. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  1392. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  1393. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  1394. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  1395. if (atchan->status & (AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS))
  1396. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  1397. tasklet_schedule(&atchan->tasklet);
  1398. ret = IRQ_HANDLED;
  1399. }
  1400. } while (pending);
  1401. return ret;
  1402. }
  1403. static void at_xdmac_issue_pending(struct dma_chan *chan)
  1404. {
  1405. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1406. dev_dbg(chan2dev(&atchan->chan), "%s\n", __func__);
  1407. if (!at_xdmac_chan_is_cyclic(atchan))
  1408. at_xdmac_advance_work(atchan);
  1409. return;
  1410. }
  1411. static int at_xdmac_device_config(struct dma_chan *chan,
  1412. struct dma_slave_config *config)
  1413. {
  1414. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1415. int ret;
  1416. unsigned long flags;
  1417. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1418. spin_lock_irqsave(&atchan->lock, flags);
  1419. ret = at_xdmac_set_slave_config(chan, config);
  1420. spin_unlock_irqrestore(&atchan->lock, flags);
  1421. return ret;
  1422. }
  1423. static int at_xdmac_device_pause(struct dma_chan *chan)
  1424. {
  1425. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1426. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1427. unsigned long flags;
  1428. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1429. if (test_and_set_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status))
  1430. return 0;
  1431. spin_lock_irqsave(&atchan->lock, flags);
  1432. at_xdmac_write(atxdmac, AT_XDMAC_GRWS, atchan->mask);
  1433. while (at_xdmac_chan_read(atchan, AT_XDMAC_CC)
  1434. & (AT_XDMAC_CC_WRIP | AT_XDMAC_CC_RDIP))
  1435. cpu_relax();
  1436. spin_unlock_irqrestore(&atchan->lock, flags);
  1437. return 0;
  1438. }
  1439. static int at_xdmac_device_resume(struct dma_chan *chan)
  1440. {
  1441. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1442. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1443. unsigned long flags;
  1444. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1445. spin_lock_irqsave(&atchan->lock, flags);
  1446. if (!at_xdmac_chan_is_paused(atchan)) {
  1447. spin_unlock_irqrestore(&atchan->lock, flags);
  1448. return 0;
  1449. }
  1450. at_xdmac_write(atxdmac, AT_XDMAC_GRWR, atchan->mask);
  1451. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  1452. spin_unlock_irqrestore(&atchan->lock, flags);
  1453. return 0;
  1454. }
  1455. static int at_xdmac_device_terminate_all(struct dma_chan *chan)
  1456. {
  1457. struct at_xdmac_desc *desc, *_desc;
  1458. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1459. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1460. unsigned long flags;
  1461. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1462. spin_lock_irqsave(&atchan->lock, flags);
  1463. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  1464. while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
  1465. cpu_relax();
  1466. /* Cancel all pending transfers. */
  1467. list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node)
  1468. at_xdmac_remove_xfer(atchan, desc);
  1469. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  1470. clear_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  1471. spin_unlock_irqrestore(&atchan->lock, flags);
  1472. return 0;
  1473. }
  1474. static int at_xdmac_alloc_chan_resources(struct dma_chan *chan)
  1475. {
  1476. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1477. struct at_xdmac_desc *desc;
  1478. int i;
  1479. unsigned long flags;
  1480. spin_lock_irqsave(&atchan->lock, flags);
  1481. if (at_xdmac_chan_is_enabled(atchan)) {
  1482. dev_err(chan2dev(chan),
  1483. "can't allocate channel resources (channel enabled)\n");
  1484. i = -EIO;
  1485. goto spin_unlock;
  1486. }
  1487. if (!list_empty(&atchan->free_descs_list)) {
  1488. dev_err(chan2dev(chan),
  1489. "can't allocate channel resources (channel not free from a previous use)\n");
  1490. i = -EIO;
  1491. goto spin_unlock;
  1492. }
  1493. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1494. desc = at_xdmac_alloc_desc(chan, GFP_ATOMIC);
  1495. if (!desc) {
  1496. dev_warn(chan2dev(chan),
  1497. "only %d descriptors have been allocated\n", i);
  1498. break;
  1499. }
  1500. list_add_tail(&desc->desc_node, &atchan->free_descs_list);
  1501. }
  1502. dma_cookie_init(chan);
  1503. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  1504. spin_unlock:
  1505. spin_unlock_irqrestore(&atchan->lock, flags);
  1506. return i;
  1507. }
  1508. static void at_xdmac_free_chan_resources(struct dma_chan *chan)
  1509. {
  1510. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1511. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  1512. struct at_xdmac_desc *desc, *_desc;
  1513. list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) {
  1514. dev_dbg(chan2dev(chan), "%s: freeing descriptor %p\n", __func__, desc);
  1515. list_del(&desc->desc_node);
  1516. dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys);
  1517. }
  1518. return;
  1519. }
  1520. #ifdef CONFIG_PM
  1521. static int atmel_xdmac_prepare(struct device *dev)
  1522. {
  1523. struct platform_device *pdev = to_platform_device(dev);
  1524. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1525. struct dma_chan *chan, *_chan;
  1526. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1527. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1528. /* Wait for transfer completion, except in cyclic case. */
  1529. if (at_xdmac_chan_is_enabled(atchan) && !at_xdmac_chan_is_cyclic(atchan))
  1530. return -EAGAIN;
  1531. }
  1532. return 0;
  1533. }
  1534. #else
  1535. # define atmel_xdmac_prepare NULL
  1536. #endif
  1537. #ifdef CONFIG_PM_SLEEP
  1538. static int atmel_xdmac_suspend(struct device *dev)
  1539. {
  1540. struct platform_device *pdev = to_platform_device(dev);
  1541. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1542. struct dma_chan *chan, *_chan;
  1543. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1544. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1545. atchan->save_cc = at_xdmac_chan_read(atchan, AT_XDMAC_CC);
  1546. if (at_xdmac_chan_is_cyclic(atchan)) {
  1547. if (!at_xdmac_chan_is_paused(atchan))
  1548. at_xdmac_device_pause(chan);
  1549. atchan->save_cim = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1550. atchan->save_cnda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA);
  1551. atchan->save_cndc = at_xdmac_chan_read(atchan, AT_XDMAC_CNDC);
  1552. }
  1553. }
  1554. atxdmac->save_gim = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1555. at_xdmac_off(atxdmac);
  1556. clk_disable_unprepare(atxdmac->clk);
  1557. return 0;
  1558. }
  1559. static int atmel_xdmac_resume(struct device *dev)
  1560. {
  1561. struct platform_device *pdev = to_platform_device(dev);
  1562. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1563. struct at_xdmac_chan *atchan;
  1564. struct dma_chan *chan, *_chan;
  1565. int i;
  1566. clk_prepare_enable(atxdmac->clk);
  1567. /* Clear pending interrupts. */
  1568. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1569. atchan = &atxdmac->chan[i];
  1570. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1571. cpu_relax();
  1572. }
  1573. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atxdmac->save_gim);
  1574. at_xdmac_write(atxdmac, AT_XDMAC_GE, atxdmac->save_gs);
  1575. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1576. atchan = to_at_xdmac_chan(chan);
  1577. at_xdmac_chan_write(atchan, AT_XDMAC_CC, atchan->save_cc);
  1578. if (at_xdmac_chan_is_cyclic(atchan)) {
  1579. if (at_xdmac_chan_is_paused(atchan))
  1580. at_xdmac_device_resume(chan);
  1581. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, atchan->save_cnda);
  1582. at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, atchan->save_cndc);
  1583. at_xdmac_chan_write(atchan, AT_XDMAC_CIE, atchan->save_cim);
  1584. wmb();
  1585. at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
  1586. }
  1587. }
  1588. return 0;
  1589. }
  1590. #endif /* CONFIG_PM_SLEEP */
  1591. static int at_xdmac_probe(struct platform_device *pdev)
  1592. {
  1593. struct resource *res;
  1594. struct at_xdmac *atxdmac;
  1595. int irq, size, nr_channels, i, ret;
  1596. void __iomem *base;
  1597. u32 reg;
  1598. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1599. if (!res)
  1600. return -EINVAL;
  1601. irq = platform_get_irq(pdev, 0);
  1602. if (irq < 0)
  1603. return irq;
  1604. base = devm_ioremap_resource(&pdev->dev, res);
  1605. if (IS_ERR(base))
  1606. return PTR_ERR(base);
  1607. /*
  1608. * Read number of xdmac channels, read helper function can't be used
  1609. * since atxdmac is not yet allocated and we need to know the number
  1610. * of channels to do the allocation.
  1611. */
  1612. reg = readl_relaxed(base + AT_XDMAC_GTYPE);
  1613. nr_channels = AT_XDMAC_NB_CH(reg);
  1614. if (nr_channels > AT_XDMAC_MAX_CHAN) {
  1615. dev_err(&pdev->dev, "invalid number of channels (%u)\n",
  1616. nr_channels);
  1617. return -EINVAL;
  1618. }
  1619. size = sizeof(*atxdmac);
  1620. size += nr_channels * sizeof(struct at_xdmac_chan);
  1621. atxdmac = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1622. if (!atxdmac) {
  1623. dev_err(&pdev->dev, "can't allocate at_xdmac structure\n");
  1624. return -ENOMEM;
  1625. }
  1626. atxdmac->regs = base;
  1627. atxdmac->irq = irq;
  1628. atxdmac->clk = devm_clk_get(&pdev->dev, "dma_clk");
  1629. if (IS_ERR(atxdmac->clk)) {
  1630. dev_err(&pdev->dev, "can't get dma_clk\n");
  1631. return PTR_ERR(atxdmac->clk);
  1632. }
  1633. /* Do not use dev res to prevent races with tasklet */
  1634. ret = request_irq(atxdmac->irq, at_xdmac_interrupt, 0, "at_xdmac", atxdmac);
  1635. if (ret) {
  1636. dev_err(&pdev->dev, "can't request irq\n");
  1637. return ret;
  1638. }
  1639. ret = clk_prepare_enable(atxdmac->clk);
  1640. if (ret) {
  1641. dev_err(&pdev->dev, "can't prepare or enable clock\n");
  1642. goto err_free_irq;
  1643. }
  1644. atxdmac->at_xdmac_desc_pool =
  1645. dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  1646. sizeof(struct at_xdmac_desc), 4, 0);
  1647. if (!atxdmac->at_xdmac_desc_pool) {
  1648. dev_err(&pdev->dev, "no memory for descriptors dma pool\n");
  1649. ret = -ENOMEM;
  1650. goto err_clk_disable;
  1651. }
  1652. dma_cap_set(DMA_CYCLIC, atxdmac->dma.cap_mask);
  1653. dma_cap_set(DMA_INTERLEAVE, atxdmac->dma.cap_mask);
  1654. dma_cap_set(DMA_MEMCPY, atxdmac->dma.cap_mask);
  1655. dma_cap_set(DMA_MEMSET, atxdmac->dma.cap_mask);
  1656. dma_cap_set(DMA_MEMSET_SG, atxdmac->dma.cap_mask);
  1657. dma_cap_set(DMA_SLAVE, atxdmac->dma.cap_mask);
  1658. /*
  1659. * Without DMA_PRIVATE the driver is not able to allocate more than
  1660. * one channel, second allocation fails in private_candidate.
  1661. */
  1662. dma_cap_set(DMA_PRIVATE, atxdmac->dma.cap_mask);
  1663. atxdmac->dma.dev = &pdev->dev;
  1664. atxdmac->dma.device_alloc_chan_resources = at_xdmac_alloc_chan_resources;
  1665. atxdmac->dma.device_free_chan_resources = at_xdmac_free_chan_resources;
  1666. atxdmac->dma.device_tx_status = at_xdmac_tx_status;
  1667. atxdmac->dma.device_issue_pending = at_xdmac_issue_pending;
  1668. atxdmac->dma.device_prep_dma_cyclic = at_xdmac_prep_dma_cyclic;
  1669. atxdmac->dma.device_prep_interleaved_dma = at_xdmac_prep_interleaved;
  1670. atxdmac->dma.device_prep_dma_memcpy = at_xdmac_prep_dma_memcpy;
  1671. atxdmac->dma.device_prep_dma_memset = at_xdmac_prep_dma_memset;
  1672. atxdmac->dma.device_prep_dma_memset_sg = at_xdmac_prep_dma_memset_sg;
  1673. atxdmac->dma.device_prep_slave_sg = at_xdmac_prep_slave_sg;
  1674. atxdmac->dma.device_config = at_xdmac_device_config;
  1675. atxdmac->dma.device_pause = at_xdmac_device_pause;
  1676. atxdmac->dma.device_resume = at_xdmac_device_resume;
  1677. atxdmac->dma.device_terminate_all = at_xdmac_device_terminate_all;
  1678. atxdmac->dma.src_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1679. atxdmac->dma.dst_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1680. atxdmac->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1681. atxdmac->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1682. /* Disable all chans and interrupts. */
  1683. at_xdmac_off(atxdmac);
  1684. /* Init channels. */
  1685. INIT_LIST_HEAD(&atxdmac->dma.channels);
  1686. for (i = 0; i < nr_channels; i++) {
  1687. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1688. atchan->chan.device = &atxdmac->dma;
  1689. list_add_tail(&atchan->chan.device_node,
  1690. &atxdmac->dma.channels);
  1691. atchan->ch_regs = at_xdmac_chan_reg_base(atxdmac, i);
  1692. atchan->mask = 1 << i;
  1693. spin_lock_init(&atchan->lock);
  1694. INIT_LIST_HEAD(&atchan->xfers_list);
  1695. INIT_LIST_HEAD(&atchan->free_descs_list);
  1696. tasklet_init(&atchan->tasklet, at_xdmac_tasklet,
  1697. (unsigned long)atchan);
  1698. /* Clear pending interrupts. */
  1699. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1700. cpu_relax();
  1701. }
  1702. platform_set_drvdata(pdev, atxdmac);
  1703. ret = dma_async_device_register(&atxdmac->dma);
  1704. if (ret) {
  1705. dev_err(&pdev->dev, "fail to register DMA engine device\n");
  1706. goto err_clk_disable;
  1707. }
  1708. ret = of_dma_controller_register(pdev->dev.of_node,
  1709. at_xdmac_xlate, atxdmac);
  1710. if (ret) {
  1711. dev_err(&pdev->dev, "could not register of dma controller\n");
  1712. goto err_dma_unregister;
  1713. }
  1714. dev_info(&pdev->dev, "%d channels, mapped at 0x%p\n",
  1715. nr_channels, atxdmac->regs);
  1716. return 0;
  1717. err_dma_unregister:
  1718. dma_async_device_unregister(&atxdmac->dma);
  1719. err_clk_disable:
  1720. clk_disable_unprepare(atxdmac->clk);
  1721. err_free_irq:
  1722. free_irq(atxdmac->irq, atxdmac->dma.dev);
  1723. return ret;
  1724. }
  1725. static int at_xdmac_remove(struct platform_device *pdev)
  1726. {
  1727. struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
  1728. int i;
  1729. at_xdmac_off(atxdmac);
  1730. of_dma_controller_free(pdev->dev.of_node);
  1731. dma_async_device_unregister(&atxdmac->dma);
  1732. clk_disable_unprepare(atxdmac->clk);
  1733. free_irq(atxdmac->irq, atxdmac->dma.dev);
  1734. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1735. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1736. tasklet_kill(&atchan->tasklet);
  1737. at_xdmac_free_chan_resources(&atchan->chan);
  1738. }
  1739. return 0;
  1740. }
  1741. static const struct dev_pm_ops atmel_xdmac_dev_pm_ops = {
  1742. .prepare = atmel_xdmac_prepare,
  1743. SET_LATE_SYSTEM_SLEEP_PM_OPS(atmel_xdmac_suspend, atmel_xdmac_resume)
  1744. };
  1745. static const struct of_device_id atmel_xdmac_dt_ids[] = {
  1746. {
  1747. .compatible = "atmel,sama5d4-dma",
  1748. }, {
  1749. /* sentinel */
  1750. }
  1751. };
  1752. MODULE_DEVICE_TABLE(of, atmel_xdmac_dt_ids);
  1753. static struct platform_driver at_xdmac_driver = {
  1754. .probe = at_xdmac_probe,
  1755. .remove = at_xdmac_remove,
  1756. .driver = {
  1757. .name = "at_xdmac",
  1758. .of_match_table = of_match_ptr(atmel_xdmac_dt_ids),
  1759. .pm = &atmel_xdmac_dev_pm_ops,
  1760. }
  1761. };
  1762. static int __init at_xdmac_init(void)
  1763. {
  1764. return platform_driver_probe(&at_xdmac_driver, at_xdmac_probe);
  1765. }
  1766. subsys_initcall(at_xdmac_init);
  1767. MODULE_DESCRIPTION("Atmel Extended DMA Controller driver");
  1768. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  1769. MODULE_LICENSE("GPL");