at_xdmac.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  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. struct dma_pool *at_xdmac_desc_pool;
  211. struct at_xdmac_chan chan[0];
  212. };
  213. /* ----- Descriptors ----- */
  214. /* Linked List Descriptor */
  215. struct at_xdmac_lld {
  216. dma_addr_t mbr_nda; /* Next Descriptor Member */
  217. u32 mbr_ubc; /* Microblock Control Member */
  218. dma_addr_t mbr_sa; /* Source Address Member */
  219. dma_addr_t mbr_da; /* Destination Address Member */
  220. u32 mbr_cfg; /* Configuration Register */
  221. u32 mbr_bc; /* Block Control Register */
  222. u32 mbr_ds; /* Data Stride Register */
  223. u32 mbr_sus; /* Source Microblock Stride Register */
  224. u32 mbr_dus; /* Destination Microblock Stride Register */
  225. };
  226. /* 64-bit alignment needed to update CNDA and CUBC registers in an atomic way. */
  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. } __aligned(sizeof(u64));
  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_zalloc(atxdmac->at_xdmac_desc_pool, gfp_flags, &phys);
  393. if (desc) {
  394. INIT_LIST_HEAD(&desc->descs_list);
  395. dma_async_tx_descriptor_init(&desc->tx_dma_desc, chan);
  396. desc->tx_dma_desc.tx_submit = at_xdmac_tx_submit;
  397. desc->tx_dma_desc.phys = phys;
  398. }
  399. return desc;
  400. }
  401. static void at_xdmac_init_used_desc(struct at_xdmac_desc *desc)
  402. {
  403. memset(&desc->lld, 0, sizeof(desc->lld));
  404. INIT_LIST_HEAD(&desc->descs_list);
  405. desc->direction = DMA_TRANS_NONE;
  406. desc->xfer_size = 0;
  407. desc->active_xfer = false;
  408. }
  409. /* Call must be protected by lock. */
  410. static struct at_xdmac_desc *at_xdmac_get_desc(struct at_xdmac_chan *atchan)
  411. {
  412. struct at_xdmac_desc *desc;
  413. if (list_empty(&atchan->free_descs_list)) {
  414. desc = at_xdmac_alloc_desc(&atchan->chan, GFP_NOWAIT);
  415. } else {
  416. desc = list_first_entry(&atchan->free_descs_list,
  417. struct at_xdmac_desc, desc_node);
  418. list_del(&desc->desc_node);
  419. at_xdmac_init_used_desc(desc);
  420. }
  421. return desc;
  422. }
  423. static void at_xdmac_queue_desc(struct dma_chan *chan,
  424. struct at_xdmac_desc *prev,
  425. struct at_xdmac_desc *desc)
  426. {
  427. if (!prev || !desc)
  428. return;
  429. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  430. prev->lld.mbr_ubc |= AT_XDMAC_MBR_UBC_NDE;
  431. dev_dbg(chan2dev(chan), "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  432. __func__, prev, &prev->lld.mbr_nda);
  433. }
  434. static inline void at_xdmac_increment_block_count(struct dma_chan *chan,
  435. struct at_xdmac_desc *desc)
  436. {
  437. if (!desc)
  438. return;
  439. desc->lld.mbr_bc++;
  440. dev_dbg(chan2dev(chan),
  441. "%s: incrementing the block count of the desc 0x%p\n",
  442. __func__, desc);
  443. }
  444. static struct dma_chan *at_xdmac_xlate(struct of_phandle_args *dma_spec,
  445. struct of_dma *of_dma)
  446. {
  447. struct at_xdmac *atxdmac = of_dma->of_dma_data;
  448. struct at_xdmac_chan *atchan;
  449. struct dma_chan *chan;
  450. struct device *dev = atxdmac->dma.dev;
  451. if (dma_spec->args_count != 1) {
  452. dev_err(dev, "dma phandler args: bad number of args\n");
  453. return NULL;
  454. }
  455. chan = dma_get_any_slave_channel(&atxdmac->dma);
  456. if (!chan) {
  457. dev_err(dev, "can't get a dma channel\n");
  458. return NULL;
  459. }
  460. atchan = to_at_xdmac_chan(chan);
  461. atchan->memif = AT91_XDMAC_DT_GET_MEM_IF(dma_spec->args[0]);
  462. atchan->perif = AT91_XDMAC_DT_GET_PER_IF(dma_spec->args[0]);
  463. atchan->perid = AT91_XDMAC_DT_GET_PERID(dma_spec->args[0]);
  464. dev_dbg(dev, "chan dt cfg: memif=%u perif=%u perid=%u\n",
  465. atchan->memif, atchan->perif, atchan->perid);
  466. return chan;
  467. }
  468. static int at_xdmac_compute_chan_conf(struct dma_chan *chan,
  469. enum dma_transfer_direction direction)
  470. {
  471. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  472. int csize, dwidth;
  473. if (direction == DMA_DEV_TO_MEM) {
  474. atchan->cfg =
  475. AT91_XDMAC_DT_PERID(atchan->perid)
  476. | AT_XDMAC_CC_DAM_INCREMENTED_AM
  477. | AT_XDMAC_CC_SAM_FIXED_AM
  478. | AT_XDMAC_CC_DIF(atchan->memif)
  479. | AT_XDMAC_CC_SIF(atchan->perif)
  480. | AT_XDMAC_CC_SWREQ_HWR_CONNECTED
  481. | AT_XDMAC_CC_DSYNC_PER2MEM
  482. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  483. | AT_XDMAC_CC_TYPE_PER_TRAN;
  484. csize = ffs(atchan->sconfig.src_maxburst) - 1;
  485. if (csize < 0) {
  486. dev_err(chan2dev(chan), "invalid src maxburst value\n");
  487. return -EINVAL;
  488. }
  489. atchan->cfg |= AT_XDMAC_CC_CSIZE(csize);
  490. dwidth = ffs(atchan->sconfig.src_addr_width) - 1;
  491. if (dwidth < 0) {
  492. dev_err(chan2dev(chan), "invalid src addr width value\n");
  493. return -EINVAL;
  494. }
  495. atchan->cfg |= AT_XDMAC_CC_DWIDTH(dwidth);
  496. } else if (direction == DMA_MEM_TO_DEV) {
  497. atchan->cfg =
  498. AT91_XDMAC_DT_PERID(atchan->perid)
  499. | AT_XDMAC_CC_DAM_FIXED_AM
  500. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  501. | AT_XDMAC_CC_DIF(atchan->perif)
  502. | AT_XDMAC_CC_SIF(atchan->memif)
  503. | AT_XDMAC_CC_SWREQ_HWR_CONNECTED
  504. | AT_XDMAC_CC_DSYNC_MEM2PER
  505. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  506. | AT_XDMAC_CC_TYPE_PER_TRAN;
  507. csize = ffs(atchan->sconfig.dst_maxburst) - 1;
  508. if (csize < 0) {
  509. dev_err(chan2dev(chan), "invalid src maxburst value\n");
  510. return -EINVAL;
  511. }
  512. atchan->cfg |= AT_XDMAC_CC_CSIZE(csize);
  513. dwidth = ffs(atchan->sconfig.dst_addr_width) - 1;
  514. if (dwidth < 0) {
  515. dev_err(chan2dev(chan), "invalid dst addr width value\n");
  516. return -EINVAL;
  517. }
  518. atchan->cfg |= AT_XDMAC_CC_DWIDTH(dwidth);
  519. }
  520. dev_dbg(chan2dev(chan), "%s: cfg=0x%08x\n", __func__, atchan->cfg);
  521. return 0;
  522. }
  523. /*
  524. * Only check that maxburst and addr width values are supported by the
  525. * the controller but not that the configuration is good to perform the
  526. * transfer since we don't know the direction at this stage.
  527. */
  528. static int at_xdmac_check_slave_config(struct dma_slave_config *sconfig)
  529. {
  530. if ((sconfig->src_maxburst > AT_XDMAC_MAX_CSIZE)
  531. || (sconfig->dst_maxburst > AT_XDMAC_MAX_CSIZE))
  532. return -EINVAL;
  533. if ((sconfig->src_addr_width > AT_XDMAC_MAX_DWIDTH)
  534. || (sconfig->dst_addr_width > AT_XDMAC_MAX_DWIDTH))
  535. return -EINVAL;
  536. return 0;
  537. }
  538. static int at_xdmac_set_slave_config(struct dma_chan *chan,
  539. struct dma_slave_config *sconfig)
  540. {
  541. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  542. if (at_xdmac_check_slave_config(sconfig)) {
  543. dev_err(chan2dev(chan), "invalid slave configuration\n");
  544. return -EINVAL;
  545. }
  546. memcpy(&atchan->sconfig, sconfig, sizeof(atchan->sconfig));
  547. return 0;
  548. }
  549. static struct dma_async_tx_descriptor *
  550. at_xdmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  551. unsigned int sg_len, enum dma_transfer_direction direction,
  552. unsigned long flags, void *context)
  553. {
  554. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  555. struct at_xdmac_desc *first = NULL, *prev = NULL;
  556. struct scatterlist *sg;
  557. int i;
  558. unsigned int xfer_size = 0;
  559. unsigned long irqflags;
  560. struct dma_async_tx_descriptor *ret = NULL;
  561. if (!sgl)
  562. return NULL;
  563. if (!is_slave_direction(direction)) {
  564. dev_err(chan2dev(chan), "invalid DMA direction\n");
  565. return NULL;
  566. }
  567. dev_dbg(chan2dev(chan), "%s: sg_len=%d, dir=%s, flags=0x%lx\n",
  568. __func__, sg_len,
  569. direction == DMA_MEM_TO_DEV ? "to device" : "from device",
  570. flags);
  571. /* Protect dma_sconfig field that can be modified by set_slave_conf. */
  572. spin_lock_irqsave(&atchan->lock, irqflags);
  573. if (at_xdmac_compute_chan_conf(chan, direction))
  574. goto spin_unlock;
  575. /* Prepare descriptors. */
  576. for_each_sg(sgl, sg, sg_len, i) {
  577. struct at_xdmac_desc *desc = NULL;
  578. u32 len, mem, dwidth, fixed_dwidth;
  579. len = sg_dma_len(sg);
  580. mem = sg_dma_address(sg);
  581. if (unlikely(!len)) {
  582. dev_err(chan2dev(chan), "sg data length is zero\n");
  583. goto spin_unlock;
  584. }
  585. dev_dbg(chan2dev(chan), "%s: * sg%d len=%u, mem=0x%08x\n",
  586. __func__, i, len, mem);
  587. desc = at_xdmac_get_desc(atchan);
  588. if (!desc) {
  589. dev_err(chan2dev(chan), "can't get descriptor\n");
  590. if (first)
  591. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  592. goto spin_unlock;
  593. }
  594. /* Linked list descriptor setup. */
  595. if (direction == DMA_DEV_TO_MEM) {
  596. desc->lld.mbr_sa = atchan->sconfig.src_addr;
  597. desc->lld.mbr_da = mem;
  598. } else {
  599. desc->lld.mbr_sa = mem;
  600. desc->lld.mbr_da = atchan->sconfig.dst_addr;
  601. }
  602. dwidth = at_xdmac_get_dwidth(atchan->cfg);
  603. fixed_dwidth = IS_ALIGNED(len, 1 << dwidth)
  604. ? dwidth
  605. : AT_XDMAC_CC_DWIDTH_BYTE;
  606. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2 /* next descriptor view */
  607. | AT_XDMAC_MBR_UBC_NDEN /* next descriptor dst parameter update */
  608. | AT_XDMAC_MBR_UBC_NSEN /* next descriptor src parameter update */
  609. | (len >> fixed_dwidth); /* microblock length */
  610. desc->lld.mbr_cfg = (atchan->cfg & ~AT_XDMAC_CC_DWIDTH_MASK) |
  611. AT_XDMAC_CC_DWIDTH(fixed_dwidth);
  612. dev_dbg(chan2dev(chan),
  613. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  614. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  615. /* Chain lld. */
  616. if (prev)
  617. at_xdmac_queue_desc(chan, prev, desc);
  618. prev = desc;
  619. if (!first)
  620. first = desc;
  621. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  622. __func__, desc, first);
  623. list_add_tail(&desc->desc_node, &first->descs_list);
  624. xfer_size += len;
  625. }
  626. first->tx_dma_desc.flags = flags;
  627. first->xfer_size = xfer_size;
  628. first->direction = direction;
  629. ret = &first->tx_dma_desc;
  630. spin_unlock:
  631. spin_unlock_irqrestore(&atchan->lock, irqflags);
  632. return ret;
  633. }
  634. static struct dma_async_tx_descriptor *
  635. at_xdmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
  636. size_t buf_len, size_t period_len,
  637. enum dma_transfer_direction direction,
  638. unsigned long flags)
  639. {
  640. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  641. struct at_xdmac_desc *first = NULL, *prev = NULL;
  642. unsigned int periods = buf_len / period_len;
  643. int i;
  644. unsigned long irqflags;
  645. dev_dbg(chan2dev(chan), "%s: buf_addr=%pad, buf_len=%zd, period_len=%zd, dir=%s, flags=0x%lx\n",
  646. __func__, &buf_addr, buf_len, period_len,
  647. direction == DMA_MEM_TO_DEV ? "mem2per" : "per2mem", flags);
  648. if (!is_slave_direction(direction)) {
  649. dev_err(chan2dev(chan), "invalid DMA direction\n");
  650. return NULL;
  651. }
  652. if (test_and_set_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status)) {
  653. dev_err(chan2dev(chan), "channel currently used\n");
  654. return NULL;
  655. }
  656. if (at_xdmac_compute_chan_conf(chan, direction))
  657. return NULL;
  658. for (i = 0; i < periods; i++) {
  659. struct at_xdmac_desc *desc = NULL;
  660. spin_lock_irqsave(&atchan->lock, irqflags);
  661. desc = at_xdmac_get_desc(atchan);
  662. if (!desc) {
  663. dev_err(chan2dev(chan), "can't get descriptor\n");
  664. if (first)
  665. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  666. spin_unlock_irqrestore(&atchan->lock, irqflags);
  667. return NULL;
  668. }
  669. spin_unlock_irqrestore(&atchan->lock, irqflags);
  670. dev_dbg(chan2dev(chan),
  671. "%s: desc=0x%p, tx_dma_desc.phys=%pad\n",
  672. __func__, desc, &desc->tx_dma_desc.phys);
  673. if (direction == DMA_DEV_TO_MEM) {
  674. desc->lld.mbr_sa = atchan->sconfig.src_addr;
  675. desc->lld.mbr_da = buf_addr + i * period_len;
  676. } else {
  677. desc->lld.mbr_sa = buf_addr + i * period_len;
  678. desc->lld.mbr_da = atchan->sconfig.dst_addr;
  679. }
  680. desc->lld.mbr_cfg = atchan->cfg;
  681. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV1
  682. | AT_XDMAC_MBR_UBC_NDEN
  683. | AT_XDMAC_MBR_UBC_NSEN
  684. | period_len >> at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  685. dev_dbg(chan2dev(chan),
  686. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  687. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  688. /* Chain lld. */
  689. if (prev)
  690. at_xdmac_queue_desc(chan, prev, desc);
  691. prev = desc;
  692. if (!first)
  693. first = desc;
  694. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  695. __func__, desc, first);
  696. list_add_tail(&desc->desc_node, &first->descs_list);
  697. }
  698. at_xdmac_queue_desc(chan, prev, first);
  699. first->tx_dma_desc.flags = flags;
  700. first->xfer_size = buf_len;
  701. first->direction = direction;
  702. return &first->tx_dma_desc;
  703. }
  704. static inline u32 at_xdmac_align_width(struct dma_chan *chan, dma_addr_t addr)
  705. {
  706. u32 width;
  707. /*
  708. * Check address alignment to select the greater data width we
  709. * can use.
  710. *
  711. * Some XDMAC implementations don't provide dword transfer, in
  712. * this case selecting dword has the same behavior as
  713. * selecting word transfers.
  714. */
  715. if (!(addr & 7)) {
  716. width = AT_XDMAC_CC_DWIDTH_DWORD;
  717. dev_dbg(chan2dev(chan), "%s: dwidth: double word\n", __func__);
  718. } else if (!(addr & 3)) {
  719. width = AT_XDMAC_CC_DWIDTH_WORD;
  720. dev_dbg(chan2dev(chan), "%s: dwidth: word\n", __func__);
  721. } else if (!(addr & 1)) {
  722. width = AT_XDMAC_CC_DWIDTH_HALFWORD;
  723. dev_dbg(chan2dev(chan), "%s: dwidth: half word\n", __func__);
  724. } else {
  725. width = AT_XDMAC_CC_DWIDTH_BYTE;
  726. dev_dbg(chan2dev(chan), "%s: dwidth: byte\n", __func__);
  727. }
  728. return width;
  729. }
  730. static struct at_xdmac_desc *
  731. at_xdmac_interleaved_queue_desc(struct dma_chan *chan,
  732. struct at_xdmac_chan *atchan,
  733. struct at_xdmac_desc *prev,
  734. dma_addr_t src, dma_addr_t dst,
  735. struct dma_interleaved_template *xt,
  736. struct data_chunk *chunk)
  737. {
  738. struct at_xdmac_desc *desc;
  739. u32 dwidth;
  740. unsigned long flags;
  741. size_t ublen;
  742. /*
  743. * WARNING: The channel configuration is set here since there is no
  744. * dmaengine_slave_config call in this case. Moreover we don't know the
  745. * direction, it involves we can't dynamically set the source and dest
  746. * interface so we have to use the same one. Only interface 0 allows EBI
  747. * access. Hopefully we can access DDR through both ports (at least on
  748. * SAMA5D4x), so we can use the same interface for source and dest,
  749. * that solves the fact we don't know the direction.
  750. * ERRATA: Even if useless for memory transfers, the PERID has to not
  751. * match the one of another channel. If not, it could lead to spurious
  752. * flag status.
  753. */
  754. u32 chan_cc = AT_XDMAC_CC_PERID(0x3f)
  755. | AT_XDMAC_CC_DIF(0)
  756. | AT_XDMAC_CC_SIF(0)
  757. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  758. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  759. dwidth = at_xdmac_align_width(chan, src | dst | chunk->size);
  760. if (chunk->size >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) {
  761. dev_dbg(chan2dev(chan),
  762. "%s: chunk too big (%d, max size %lu)...\n",
  763. __func__, chunk->size,
  764. AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth);
  765. return NULL;
  766. }
  767. if (prev)
  768. dev_dbg(chan2dev(chan),
  769. "Adding items at the end of desc 0x%p\n", prev);
  770. if (xt->src_inc) {
  771. if (xt->src_sgl)
  772. chan_cc |= AT_XDMAC_CC_SAM_UBS_AM;
  773. else
  774. chan_cc |= AT_XDMAC_CC_SAM_INCREMENTED_AM;
  775. }
  776. if (xt->dst_inc) {
  777. if (xt->dst_sgl)
  778. chan_cc |= AT_XDMAC_CC_DAM_UBS_AM;
  779. else
  780. chan_cc |= AT_XDMAC_CC_DAM_INCREMENTED_AM;
  781. }
  782. spin_lock_irqsave(&atchan->lock, flags);
  783. desc = at_xdmac_get_desc(atchan);
  784. spin_unlock_irqrestore(&atchan->lock, flags);
  785. if (!desc) {
  786. dev_err(chan2dev(chan), "can't get descriptor\n");
  787. return NULL;
  788. }
  789. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  790. ublen = chunk->size >> dwidth;
  791. desc->lld.mbr_sa = src;
  792. desc->lld.mbr_da = dst;
  793. desc->lld.mbr_sus = dmaengine_get_src_icg(xt, chunk);
  794. desc->lld.mbr_dus = dmaengine_get_dst_icg(xt, chunk);
  795. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3
  796. | AT_XDMAC_MBR_UBC_NDEN
  797. | AT_XDMAC_MBR_UBC_NSEN
  798. | ublen;
  799. desc->lld.mbr_cfg = chan_cc;
  800. dev_dbg(chan2dev(chan),
  801. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  802. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da,
  803. desc->lld.mbr_ubc, desc->lld.mbr_cfg);
  804. /* Chain lld. */
  805. if (prev)
  806. at_xdmac_queue_desc(chan, prev, desc);
  807. return desc;
  808. }
  809. static struct dma_async_tx_descriptor *
  810. at_xdmac_prep_interleaved(struct dma_chan *chan,
  811. struct dma_interleaved_template *xt,
  812. unsigned long flags)
  813. {
  814. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  815. struct at_xdmac_desc *prev = NULL, *first = NULL;
  816. dma_addr_t dst_addr, src_addr;
  817. size_t src_skip = 0, dst_skip = 0, len = 0;
  818. struct data_chunk *chunk;
  819. int i;
  820. if (!xt || !xt->numf || (xt->dir != DMA_MEM_TO_MEM))
  821. return NULL;
  822. /*
  823. * TODO: Handle the case where we have to repeat a chain of
  824. * descriptors...
  825. */
  826. if ((xt->numf > 1) && (xt->frame_size > 1))
  827. return NULL;
  828. dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, numf=%d, frame_size=%d, flags=0x%lx\n",
  829. __func__, &xt->src_start, &xt->dst_start, xt->numf,
  830. xt->frame_size, flags);
  831. src_addr = xt->src_start;
  832. dst_addr = xt->dst_start;
  833. if (xt->numf > 1) {
  834. first = at_xdmac_interleaved_queue_desc(chan, atchan,
  835. NULL,
  836. src_addr, dst_addr,
  837. xt, xt->sgl);
  838. /* Length of the block is (BLEN+1) microblocks. */
  839. for (i = 0; i < xt->numf - 1; i++)
  840. at_xdmac_increment_block_count(chan, first);
  841. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  842. __func__, first, first);
  843. list_add_tail(&first->desc_node, &first->descs_list);
  844. } else {
  845. for (i = 0; i < xt->frame_size; i++) {
  846. size_t src_icg = 0, dst_icg = 0;
  847. struct at_xdmac_desc *desc;
  848. chunk = xt->sgl + i;
  849. dst_icg = dmaengine_get_dst_icg(xt, chunk);
  850. src_icg = dmaengine_get_src_icg(xt, chunk);
  851. src_skip = chunk->size + src_icg;
  852. dst_skip = chunk->size + dst_icg;
  853. dev_dbg(chan2dev(chan),
  854. "%s: chunk size=%d, src icg=%d, dst icg=%d\n",
  855. __func__, chunk->size, src_icg, dst_icg);
  856. desc = at_xdmac_interleaved_queue_desc(chan, atchan,
  857. prev,
  858. src_addr, dst_addr,
  859. xt, chunk);
  860. if (!desc) {
  861. list_splice_init(&first->descs_list,
  862. &atchan->free_descs_list);
  863. return NULL;
  864. }
  865. if (!first)
  866. first = desc;
  867. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  868. __func__, desc, first);
  869. list_add_tail(&desc->desc_node, &first->descs_list);
  870. if (xt->src_sgl)
  871. src_addr += src_skip;
  872. if (xt->dst_sgl)
  873. dst_addr += dst_skip;
  874. len += chunk->size;
  875. prev = desc;
  876. }
  877. }
  878. first->tx_dma_desc.cookie = -EBUSY;
  879. first->tx_dma_desc.flags = flags;
  880. first->xfer_size = len;
  881. return &first->tx_dma_desc;
  882. }
  883. static struct dma_async_tx_descriptor *
  884. at_xdmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  885. size_t len, unsigned long flags)
  886. {
  887. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  888. struct at_xdmac_desc *first = NULL, *prev = NULL;
  889. size_t remaining_size = len, xfer_size = 0, ublen;
  890. dma_addr_t src_addr = src, dst_addr = dest;
  891. u32 dwidth;
  892. /*
  893. * WARNING: We don't know the direction, it involves we can't
  894. * dynamically set the source and dest interface so we have to use the
  895. * same one. Only interface 0 allows EBI access. Hopefully we can
  896. * access DDR through both ports (at least on SAMA5D4x), so we can use
  897. * the same interface for source and dest, that solves the fact we
  898. * don't know the direction.
  899. * ERRATA: Even if useless for memory transfers, the PERID has to not
  900. * match the one of another channel. If not, it could lead to spurious
  901. * flag status.
  902. */
  903. u32 chan_cc = AT_XDMAC_CC_PERID(0x3f)
  904. | AT_XDMAC_CC_DAM_INCREMENTED_AM
  905. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  906. | AT_XDMAC_CC_DIF(0)
  907. | AT_XDMAC_CC_SIF(0)
  908. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  909. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  910. unsigned long irqflags;
  911. dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, len=%zd, flags=0x%lx\n",
  912. __func__, &src, &dest, len, flags);
  913. if (unlikely(!len))
  914. return NULL;
  915. dwidth = at_xdmac_align_width(chan, src_addr | dst_addr);
  916. /* Prepare descriptors. */
  917. while (remaining_size) {
  918. struct at_xdmac_desc *desc = NULL;
  919. dev_dbg(chan2dev(chan), "%s: remaining_size=%zu\n", __func__, remaining_size);
  920. spin_lock_irqsave(&atchan->lock, irqflags);
  921. desc = at_xdmac_get_desc(atchan);
  922. spin_unlock_irqrestore(&atchan->lock, irqflags);
  923. if (!desc) {
  924. dev_err(chan2dev(chan), "can't get descriptor\n");
  925. if (first)
  926. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  927. return NULL;
  928. }
  929. /* Update src and dest addresses. */
  930. src_addr += xfer_size;
  931. dst_addr += xfer_size;
  932. if (remaining_size >= AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)
  933. xfer_size = AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth;
  934. else
  935. xfer_size = remaining_size;
  936. dev_dbg(chan2dev(chan), "%s: xfer_size=%zu\n", __func__, xfer_size);
  937. /* Check remaining length and change data width if needed. */
  938. dwidth = at_xdmac_align_width(chan,
  939. src_addr | dst_addr | xfer_size);
  940. chan_cc &= ~AT_XDMAC_CC_DWIDTH_MASK;
  941. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  942. ublen = xfer_size >> dwidth;
  943. remaining_size -= xfer_size;
  944. desc->lld.mbr_sa = src_addr;
  945. desc->lld.mbr_da = dst_addr;
  946. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2
  947. | AT_XDMAC_MBR_UBC_NDEN
  948. | AT_XDMAC_MBR_UBC_NSEN
  949. | ublen;
  950. desc->lld.mbr_cfg = chan_cc;
  951. dev_dbg(chan2dev(chan),
  952. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  953. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc, desc->lld.mbr_cfg);
  954. /* Chain lld. */
  955. if (prev)
  956. at_xdmac_queue_desc(chan, prev, desc);
  957. prev = desc;
  958. if (!first)
  959. first = desc;
  960. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  961. __func__, desc, first);
  962. list_add_tail(&desc->desc_node, &first->descs_list);
  963. }
  964. first->tx_dma_desc.flags = flags;
  965. first->xfer_size = len;
  966. return &first->tx_dma_desc;
  967. }
  968. static struct at_xdmac_desc *at_xdmac_memset_create_desc(struct dma_chan *chan,
  969. struct at_xdmac_chan *atchan,
  970. dma_addr_t dst_addr,
  971. size_t len,
  972. int value)
  973. {
  974. struct at_xdmac_desc *desc;
  975. unsigned long flags;
  976. size_t ublen;
  977. u32 dwidth;
  978. /*
  979. * WARNING: The channel configuration is set here since there is no
  980. * dmaengine_slave_config call in this case. Moreover we don't know the
  981. * direction, it involves we can't dynamically set the source and dest
  982. * interface so we have to use the same one. Only interface 0 allows EBI
  983. * access. Hopefully we can access DDR through both ports (at least on
  984. * SAMA5D4x), so we can use the same interface for source and dest,
  985. * that solves the fact we don't know the direction.
  986. * ERRATA: Even if useless for memory transfers, the PERID has to not
  987. * match the one of another channel. If not, it could lead to spurious
  988. * flag status.
  989. */
  990. u32 chan_cc = AT_XDMAC_CC_PERID(0x3f)
  991. | AT_XDMAC_CC_DAM_UBS_AM
  992. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  993. | AT_XDMAC_CC_DIF(0)
  994. | AT_XDMAC_CC_SIF(0)
  995. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  996. | AT_XDMAC_CC_MEMSET_HW_MODE
  997. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  998. dwidth = at_xdmac_align_width(chan, dst_addr);
  999. if (len >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) {
  1000. dev_err(chan2dev(chan),
  1001. "%s: Transfer too large, aborting...\n",
  1002. __func__);
  1003. return NULL;
  1004. }
  1005. spin_lock_irqsave(&atchan->lock, flags);
  1006. desc = at_xdmac_get_desc(atchan);
  1007. spin_unlock_irqrestore(&atchan->lock, flags);
  1008. if (!desc) {
  1009. dev_err(chan2dev(chan), "can't get descriptor\n");
  1010. return NULL;
  1011. }
  1012. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  1013. ublen = len >> dwidth;
  1014. desc->lld.mbr_da = dst_addr;
  1015. desc->lld.mbr_ds = value;
  1016. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3
  1017. | AT_XDMAC_MBR_UBC_NDEN
  1018. | AT_XDMAC_MBR_UBC_NSEN
  1019. | ublen;
  1020. desc->lld.mbr_cfg = chan_cc;
  1021. dev_dbg(chan2dev(chan),
  1022. "%s: lld: mbr_da=%pad, mbr_ds=0x%08x, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  1023. __func__, &desc->lld.mbr_da, desc->lld.mbr_ds, desc->lld.mbr_ubc,
  1024. desc->lld.mbr_cfg);
  1025. return desc;
  1026. }
  1027. static struct dma_async_tx_descriptor *
  1028. at_xdmac_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
  1029. size_t len, unsigned long flags)
  1030. {
  1031. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1032. struct at_xdmac_desc *desc;
  1033. dev_dbg(chan2dev(chan), "%s: dest=%pad, len=%d, pattern=0x%x, flags=0x%lx\n",
  1034. __func__, &dest, len, value, flags);
  1035. if (unlikely(!len))
  1036. return NULL;
  1037. desc = at_xdmac_memset_create_desc(chan, atchan, dest, len, value);
  1038. list_add_tail(&desc->desc_node, &desc->descs_list);
  1039. desc->tx_dma_desc.cookie = -EBUSY;
  1040. desc->tx_dma_desc.flags = flags;
  1041. desc->xfer_size = len;
  1042. return &desc->tx_dma_desc;
  1043. }
  1044. static struct dma_async_tx_descriptor *
  1045. at_xdmac_prep_dma_memset_sg(struct dma_chan *chan, struct scatterlist *sgl,
  1046. unsigned int sg_len, int value,
  1047. unsigned long flags)
  1048. {
  1049. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1050. struct at_xdmac_desc *desc, *pdesc = NULL,
  1051. *ppdesc = NULL, *first = NULL;
  1052. struct scatterlist *sg, *psg = NULL, *ppsg = NULL;
  1053. size_t stride = 0, pstride = 0, len = 0;
  1054. int i;
  1055. if (!sgl)
  1056. return NULL;
  1057. dev_dbg(chan2dev(chan), "%s: sg_len=%d, value=0x%x, flags=0x%lx\n",
  1058. __func__, sg_len, value, flags);
  1059. /* Prepare descriptors. */
  1060. for_each_sg(sgl, sg, sg_len, i) {
  1061. dev_dbg(chan2dev(chan), "%s: dest=%pad, len=%d, pattern=0x%x, flags=0x%lx\n",
  1062. __func__, &sg_dma_address(sg), sg_dma_len(sg),
  1063. value, flags);
  1064. desc = at_xdmac_memset_create_desc(chan, atchan,
  1065. sg_dma_address(sg),
  1066. sg_dma_len(sg),
  1067. value);
  1068. if (!desc && first)
  1069. list_splice_init(&first->descs_list,
  1070. &atchan->free_descs_list);
  1071. if (!first)
  1072. first = desc;
  1073. /* Update our strides */
  1074. pstride = stride;
  1075. if (psg)
  1076. stride = sg_dma_address(sg) -
  1077. (sg_dma_address(psg) + sg_dma_len(psg));
  1078. /*
  1079. * The scatterlist API gives us only the address and
  1080. * length of each elements.
  1081. *
  1082. * Unfortunately, we don't have the stride, which we
  1083. * will need to compute.
  1084. *
  1085. * That make us end up in a situation like this one:
  1086. * len stride len stride len
  1087. * +-------+ +-------+ +-------+
  1088. * | N-2 | | N-1 | | N |
  1089. * +-------+ +-------+ +-------+
  1090. *
  1091. * We need all these three elements (N-2, N-1 and N)
  1092. * to actually take the decision on whether we need to
  1093. * queue N-1 or reuse N-2.
  1094. *
  1095. * We will only consider N if it is the last element.
  1096. */
  1097. if (ppdesc && pdesc) {
  1098. if ((stride == pstride) &&
  1099. (sg_dma_len(ppsg) == sg_dma_len(psg))) {
  1100. dev_dbg(chan2dev(chan),
  1101. "%s: desc 0x%p can be merged with desc 0x%p\n",
  1102. __func__, pdesc, ppdesc);
  1103. /*
  1104. * Increment the block count of the
  1105. * N-2 descriptor
  1106. */
  1107. at_xdmac_increment_block_count(chan, ppdesc);
  1108. ppdesc->lld.mbr_dus = stride;
  1109. /*
  1110. * Put back the N-1 descriptor in the
  1111. * free descriptor list
  1112. */
  1113. list_add_tail(&pdesc->desc_node,
  1114. &atchan->free_descs_list);
  1115. /*
  1116. * Make our N-1 descriptor pointer
  1117. * point to the N-2 since they were
  1118. * actually merged.
  1119. */
  1120. pdesc = ppdesc;
  1121. /*
  1122. * Rule out the case where we don't have
  1123. * pstride computed yet (our second sg
  1124. * element)
  1125. *
  1126. * We also want to catch the case where there
  1127. * would be a negative stride,
  1128. */
  1129. } else if (pstride ||
  1130. sg_dma_address(sg) < sg_dma_address(psg)) {
  1131. /*
  1132. * Queue the N-1 descriptor after the
  1133. * N-2
  1134. */
  1135. at_xdmac_queue_desc(chan, ppdesc, pdesc);
  1136. /*
  1137. * Add the N-1 descriptor to the list
  1138. * of the descriptors used for this
  1139. * transfer
  1140. */
  1141. list_add_tail(&desc->desc_node,
  1142. &first->descs_list);
  1143. dev_dbg(chan2dev(chan),
  1144. "%s: add desc 0x%p to descs_list 0x%p\n",
  1145. __func__, desc, first);
  1146. }
  1147. }
  1148. /*
  1149. * If we are the last element, just see if we have the
  1150. * same size than the previous element.
  1151. *
  1152. * If so, we can merge it with the previous descriptor
  1153. * since we don't care about the stride anymore.
  1154. */
  1155. if ((i == (sg_len - 1)) &&
  1156. sg_dma_len(psg) == sg_dma_len(sg)) {
  1157. dev_dbg(chan2dev(chan),
  1158. "%s: desc 0x%p can be merged with desc 0x%p\n",
  1159. __func__, desc, pdesc);
  1160. /*
  1161. * Increment the block count of the N-1
  1162. * descriptor
  1163. */
  1164. at_xdmac_increment_block_count(chan, pdesc);
  1165. pdesc->lld.mbr_dus = stride;
  1166. /*
  1167. * Put back the N descriptor in the free
  1168. * descriptor list
  1169. */
  1170. list_add_tail(&desc->desc_node,
  1171. &atchan->free_descs_list);
  1172. }
  1173. /* Update our descriptors */
  1174. ppdesc = pdesc;
  1175. pdesc = desc;
  1176. /* Update our scatter pointers */
  1177. ppsg = psg;
  1178. psg = sg;
  1179. len += sg_dma_len(sg);
  1180. }
  1181. first->tx_dma_desc.cookie = -EBUSY;
  1182. first->tx_dma_desc.flags = flags;
  1183. first->xfer_size = len;
  1184. return &first->tx_dma_desc;
  1185. }
  1186. static enum dma_status
  1187. at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  1188. struct dma_tx_state *txstate)
  1189. {
  1190. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1191. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1192. struct at_xdmac_desc *desc, *_desc;
  1193. struct list_head *descs_list;
  1194. enum dma_status ret;
  1195. int residue, retry;
  1196. u32 cur_nda, check_nda, cur_ubc, mask, value;
  1197. u8 dwidth = 0;
  1198. unsigned long flags;
  1199. bool initd;
  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. Flush is needed before reading CUBC because data in
  1219. * the FIFO are not reported by CUBC. Reporting a residue of the
  1220. * transfer length while we have data in FIFO can cause issue.
  1221. * Usecase: atmel USART has a timeout which means I have received
  1222. * characters but there is no more character received for a while. On
  1223. * timeout, it requests the residue. If the data are in the DMA FIFO,
  1224. * we will return a residue of the transfer length. It means no data
  1225. * received. If an application is waiting for these data, it will hang
  1226. * since we won't have another USART timeout without receiving new
  1227. * data.
  1228. */
  1229. mask = AT_XDMAC_CC_TYPE | AT_XDMAC_CC_DSYNC;
  1230. value = AT_XDMAC_CC_TYPE_PER_TRAN | AT_XDMAC_CC_DSYNC_PER2MEM;
  1231. if ((desc->lld.mbr_cfg & mask) == value) {
  1232. at_xdmac_write(atxdmac, AT_XDMAC_GSWF, atchan->mask);
  1233. while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
  1234. cpu_relax();
  1235. }
  1236. /*
  1237. * The easiest way to compute the residue should be to pause the DMA
  1238. * but doing this can lead to miss some data as some devices don't
  1239. * have FIFO.
  1240. * We need to read several registers because:
  1241. * - DMA is running therefore a descriptor change is possible while
  1242. * reading these registers
  1243. * - When the block transfer is done, the value of the CUBC register
  1244. * is set to its initial value until the fetch of the next descriptor.
  1245. * This value will corrupt the residue calculation so we have to skip
  1246. * it.
  1247. *
  1248. * INITD -------- ------------
  1249. * |____________________|
  1250. * _______________________ _______________
  1251. * NDA @desc2 \/ @desc3
  1252. * _______________________/\_______________
  1253. * __________ ___________ _______________
  1254. * CUBC 0 \/ MAX desc1 \/ MAX desc2
  1255. * __________/\___________/\_______________
  1256. *
  1257. * Since descriptors are aligned on 64 bits, we can assume that
  1258. * the update of NDA and CUBC is atomic.
  1259. * Memory barriers are used to ensure the read order of the registers.
  1260. * A max number of retries is set because unlikely it could never ends.
  1261. */
  1262. for (retry = 0; retry < AT_XDMAC_RESIDUE_MAX_RETRIES; retry++) {
  1263. check_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  1264. rmb();
  1265. initd = !!(at_xdmac_chan_read(atchan, AT_XDMAC_CC) & AT_XDMAC_CC_INITD);
  1266. rmb();
  1267. cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC);
  1268. rmb();
  1269. cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  1270. rmb();
  1271. if ((check_nda == cur_nda) && initd)
  1272. break;
  1273. }
  1274. if (unlikely(retry >= AT_XDMAC_RESIDUE_MAX_RETRIES)) {
  1275. ret = DMA_ERROR;
  1276. goto spin_unlock;
  1277. }
  1278. /*
  1279. * Flush FIFO: only relevant when the transfer is source peripheral
  1280. * synchronized. Another flush is needed here because CUBC is updated
  1281. * when the controller sends the data write command. It can lead to
  1282. * report data that are not written in the memory or the device. The
  1283. * FIFO flush ensures that data are really written.
  1284. */
  1285. if ((desc->lld.mbr_cfg & mask) == value) {
  1286. at_xdmac_write(atxdmac, AT_XDMAC_GSWF, atchan->mask);
  1287. while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
  1288. cpu_relax();
  1289. }
  1290. /*
  1291. * Remove size of all microblocks already transferred and the current
  1292. * one. Then add the remaining size to transfer of the current
  1293. * microblock.
  1294. */
  1295. descs_list = &desc->descs_list;
  1296. list_for_each_entry_safe(desc, _desc, descs_list, desc_node) {
  1297. dwidth = at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  1298. residue -= (desc->lld.mbr_ubc & 0xffffff) << dwidth;
  1299. if ((desc->lld.mbr_nda & 0xfffffffc) == cur_nda)
  1300. break;
  1301. }
  1302. residue += cur_ubc << dwidth;
  1303. dma_set_residue(txstate, residue);
  1304. dev_dbg(chan2dev(chan),
  1305. "%s: desc=0x%p, tx_dma_desc.phys=%pad, tx_status=%d, cookie=%d, residue=%d\n",
  1306. __func__, desc, &desc->tx_dma_desc.phys, ret, cookie, residue);
  1307. spin_unlock:
  1308. spin_unlock_irqrestore(&atchan->lock, flags);
  1309. return ret;
  1310. }
  1311. /* Call must be protected by lock. */
  1312. static void at_xdmac_remove_xfer(struct at_xdmac_chan *atchan,
  1313. struct at_xdmac_desc *desc)
  1314. {
  1315. dev_dbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1316. /*
  1317. * Remove the transfer from the transfer list then move the transfer
  1318. * descriptors into the free descriptors list.
  1319. */
  1320. list_del(&desc->xfer_node);
  1321. list_splice_init(&desc->descs_list, &atchan->free_descs_list);
  1322. }
  1323. static void at_xdmac_advance_work(struct at_xdmac_chan *atchan)
  1324. {
  1325. struct at_xdmac_desc *desc;
  1326. unsigned long flags;
  1327. spin_lock_irqsave(&atchan->lock, flags);
  1328. /*
  1329. * If channel is enabled, do nothing, advance_work will be triggered
  1330. * after the interruption.
  1331. */
  1332. if (!at_xdmac_chan_is_enabled(atchan) && !list_empty(&atchan->xfers_list)) {
  1333. desc = list_first_entry(&atchan->xfers_list,
  1334. struct at_xdmac_desc,
  1335. xfer_node);
  1336. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1337. if (!desc->active_xfer)
  1338. at_xdmac_start_xfer(atchan, desc);
  1339. }
  1340. spin_unlock_irqrestore(&atchan->lock, flags);
  1341. }
  1342. static void at_xdmac_handle_cyclic(struct at_xdmac_chan *atchan)
  1343. {
  1344. struct at_xdmac_desc *desc;
  1345. struct dma_async_tx_descriptor *txd;
  1346. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  1347. txd = &desc->tx_dma_desc;
  1348. if (txd->flags & DMA_PREP_INTERRUPT)
  1349. dmaengine_desc_get_callback_invoke(txd, NULL);
  1350. }
  1351. static void at_xdmac_tasklet(unsigned long data)
  1352. {
  1353. struct at_xdmac_chan *atchan = (struct at_xdmac_chan *)data;
  1354. struct at_xdmac_desc *desc;
  1355. u32 error_mask;
  1356. dev_dbg(chan2dev(&atchan->chan), "%s: status=0x%08lx\n",
  1357. __func__, atchan->status);
  1358. error_mask = AT_XDMAC_CIS_RBEIS
  1359. | AT_XDMAC_CIS_WBEIS
  1360. | AT_XDMAC_CIS_ROIS;
  1361. if (at_xdmac_chan_is_cyclic(atchan)) {
  1362. at_xdmac_handle_cyclic(atchan);
  1363. } else if ((atchan->status & AT_XDMAC_CIS_LIS)
  1364. || (atchan->status & error_mask)) {
  1365. struct dma_async_tx_descriptor *txd;
  1366. if (atchan->status & AT_XDMAC_CIS_RBEIS)
  1367. dev_err(chan2dev(&atchan->chan), "read bus error!!!");
  1368. if (atchan->status & AT_XDMAC_CIS_WBEIS)
  1369. dev_err(chan2dev(&atchan->chan), "write bus error!!!");
  1370. if (atchan->status & AT_XDMAC_CIS_ROIS)
  1371. dev_err(chan2dev(&atchan->chan), "request overflow error!!!");
  1372. spin_lock_bh(&atchan->lock);
  1373. desc = list_first_entry(&atchan->xfers_list,
  1374. struct at_xdmac_desc,
  1375. xfer_node);
  1376. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1377. BUG_ON(!desc->active_xfer);
  1378. txd = &desc->tx_dma_desc;
  1379. at_xdmac_remove_xfer(atchan, desc);
  1380. spin_unlock_bh(&atchan->lock);
  1381. if (!at_xdmac_chan_is_cyclic(atchan)) {
  1382. dma_cookie_complete(txd);
  1383. if (txd->flags & DMA_PREP_INTERRUPT)
  1384. dmaengine_desc_get_callback_invoke(txd, NULL);
  1385. }
  1386. dma_run_dependencies(txd);
  1387. at_xdmac_advance_work(atchan);
  1388. }
  1389. }
  1390. static irqreturn_t at_xdmac_interrupt(int irq, void *dev_id)
  1391. {
  1392. struct at_xdmac *atxdmac = (struct at_xdmac *)dev_id;
  1393. struct at_xdmac_chan *atchan;
  1394. u32 imr, status, pending;
  1395. u32 chan_imr, chan_status;
  1396. int i, ret = IRQ_NONE;
  1397. do {
  1398. imr = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1399. status = at_xdmac_read(atxdmac, AT_XDMAC_GIS);
  1400. pending = status & imr;
  1401. dev_vdbg(atxdmac->dma.dev,
  1402. "%s: status=0x%08x, imr=0x%08x, pending=0x%08x\n",
  1403. __func__, status, imr, pending);
  1404. if (!pending)
  1405. break;
  1406. /* We have to find which channel has generated the interrupt. */
  1407. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1408. if (!((1 << i) & pending))
  1409. continue;
  1410. atchan = &atxdmac->chan[i];
  1411. chan_imr = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1412. chan_status = at_xdmac_chan_read(atchan, AT_XDMAC_CIS);
  1413. atchan->status = chan_status & chan_imr;
  1414. dev_vdbg(atxdmac->dma.dev,
  1415. "%s: chan%d: imr=0x%x, status=0x%x\n",
  1416. __func__, i, chan_imr, chan_status);
  1417. dev_vdbg(chan2dev(&atchan->chan),
  1418. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  1419. __func__,
  1420. at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  1421. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  1422. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  1423. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  1424. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  1425. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  1426. if (atchan->status & (AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS))
  1427. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  1428. tasklet_schedule(&atchan->tasklet);
  1429. ret = IRQ_HANDLED;
  1430. }
  1431. } while (pending);
  1432. return ret;
  1433. }
  1434. static void at_xdmac_issue_pending(struct dma_chan *chan)
  1435. {
  1436. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1437. dev_dbg(chan2dev(&atchan->chan), "%s\n", __func__);
  1438. if (!at_xdmac_chan_is_cyclic(atchan))
  1439. at_xdmac_advance_work(atchan);
  1440. return;
  1441. }
  1442. static int at_xdmac_device_config(struct dma_chan *chan,
  1443. struct dma_slave_config *config)
  1444. {
  1445. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1446. int ret;
  1447. unsigned long flags;
  1448. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1449. spin_lock_irqsave(&atchan->lock, flags);
  1450. ret = at_xdmac_set_slave_config(chan, config);
  1451. spin_unlock_irqrestore(&atchan->lock, flags);
  1452. return ret;
  1453. }
  1454. static int at_xdmac_device_pause(struct dma_chan *chan)
  1455. {
  1456. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1457. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1458. unsigned long flags;
  1459. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1460. if (test_and_set_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status))
  1461. return 0;
  1462. spin_lock_irqsave(&atchan->lock, flags);
  1463. at_xdmac_write(atxdmac, AT_XDMAC_GRWS, atchan->mask);
  1464. while (at_xdmac_chan_read(atchan, AT_XDMAC_CC)
  1465. & (AT_XDMAC_CC_WRIP | AT_XDMAC_CC_RDIP))
  1466. cpu_relax();
  1467. spin_unlock_irqrestore(&atchan->lock, flags);
  1468. return 0;
  1469. }
  1470. static int at_xdmac_device_resume(struct dma_chan *chan)
  1471. {
  1472. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1473. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1474. unsigned long flags;
  1475. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1476. spin_lock_irqsave(&atchan->lock, flags);
  1477. if (!at_xdmac_chan_is_paused(atchan)) {
  1478. spin_unlock_irqrestore(&atchan->lock, flags);
  1479. return 0;
  1480. }
  1481. at_xdmac_write(atxdmac, AT_XDMAC_GRWR, atchan->mask);
  1482. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  1483. spin_unlock_irqrestore(&atchan->lock, flags);
  1484. return 0;
  1485. }
  1486. static int at_xdmac_device_terminate_all(struct dma_chan *chan)
  1487. {
  1488. struct at_xdmac_desc *desc, *_desc;
  1489. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1490. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1491. unsigned long flags;
  1492. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1493. spin_lock_irqsave(&atchan->lock, flags);
  1494. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  1495. while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
  1496. cpu_relax();
  1497. /* Cancel all pending transfers. */
  1498. list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node)
  1499. at_xdmac_remove_xfer(atchan, desc);
  1500. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  1501. clear_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  1502. spin_unlock_irqrestore(&atchan->lock, flags);
  1503. return 0;
  1504. }
  1505. static int at_xdmac_alloc_chan_resources(struct dma_chan *chan)
  1506. {
  1507. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1508. struct at_xdmac_desc *desc;
  1509. int i;
  1510. unsigned long flags;
  1511. spin_lock_irqsave(&atchan->lock, flags);
  1512. if (at_xdmac_chan_is_enabled(atchan)) {
  1513. dev_err(chan2dev(chan),
  1514. "can't allocate channel resources (channel enabled)\n");
  1515. i = -EIO;
  1516. goto spin_unlock;
  1517. }
  1518. if (!list_empty(&atchan->free_descs_list)) {
  1519. dev_err(chan2dev(chan),
  1520. "can't allocate channel resources (channel not free from a previous use)\n");
  1521. i = -EIO;
  1522. goto spin_unlock;
  1523. }
  1524. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1525. desc = at_xdmac_alloc_desc(chan, GFP_ATOMIC);
  1526. if (!desc) {
  1527. dev_warn(chan2dev(chan),
  1528. "only %d descriptors have been allocated\n", i);
  1529. break;
  1530. }
  1531. list_add_tail(&desc->desc_node, &atchan->free_descs_list);
  1532. }
  1533. dma_cookie_init(chan);
  1534. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  1535. spin_unlock:
  1536. spin_unlock_irqrestore(&atchan->lock, flags);
  1537. return i;
  1538. }
  1539. static void at_xdmac_free_chan_resources(struct dma_chan *chan)
  1540. {
  1541. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1542. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  1543. struct at_xdmac_desc *desc, *_desc;
  1544. list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) {
  1545. dev_dbg(chan2dev(chan), "%s: freeing descriptor %p\n", __func__, desc);
  1546. list_del(&desc->desc_node);
  1547. dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys);
  1548. }
  1549. return;
  1550. }
  1551. #ifdef CONFIG_PM
  1552. static int atmel_xdmac_prepare(struct device *dev)
  1553. {
  1554. struct platform_device *pdev = to_platform_device(dev);
  1555. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1556. struct dma_chan *chan, *_chan;
  1557. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1558. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1559. /* Wait for transfer completion, except in cyclic case. */
  1560. if (at_xdmac_chan_is_enabled(atchan) && !at_xdmac_chan_is_cyclic(atchan))
  1561. return -EAGAIN;
  1562. }
  1563. return 0;
  1564. }
  1565. #else
  1566. # define atmel_xdmac_prepare NULL
  1567. #endif
  1568. #ifdef CONFIG_PM_SLEEP
  1569. static int atmel_xdmac_suspend(struct device *dev)
  1570. {
  1571. struct platform_device *pdev = to_platform_device(dev);
  1572. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1573. struct dma_chan *chan, *_chan;
  1574. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1575. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1576. atchan->save_cc = at_xdmac_chan_read(atchan, AT_XDMAC_CC);
  1577. if (at_xdmac_chan_is_cyclic(atchan)) {
  1578. if (!at_xdmac_chan_is_paused(atchan))
  1579. at_xdmac_device_pause(chan);
  1580. atchan->save_cim = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1581. atchan->save_cnda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA);
  1582. atchan->save_cndc = at_xdmac_chan_read(atchan, AT_XDMAC_CNDC);
  1583. }
  1584. }
  1585. atxdmac->save_gim = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1586. at_xdmac_off(atxdmac);
  1587. clk_disable_unprepare(atxdmac->clk);
  1588. return 0;
  1589. }
  1590. static int atmel_xdmac_resume(struct device *dev)
  1591. {
  1592. struct platform_device *pdev = to_platform_device(dev);
  1593. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1594. struct at_xdmac_chan *atchan;
  1595. struct dma_chan *chan, *_chan;
  1596. int i;
  1597. clk_prepare_enable(atxdmac->clk);
  1598. /* Clear pending interrupts. */
  1599. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1600. atchan = &atxdmac->chan[i];
  1601. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1602. cpu_relax();
  1603. }
  1604. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atxdmac->save_gim);
  1605. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1606. atchan = to_at_xdmac_chan(chan);
  1607. at_xdmac_chan_write(atchan, AT_XDMAC_CC, atchan->save_cc);
  1608. if (at_xdmac_chan_is_cyclic(atchan)) {
  1609. if (at_xdmac_chan_is_paused(atchan))
  1610. at_xdmac_device_resume(chan);
  1611. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, atchan->save_cnda);
  1612. at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, atchan->save_cndc);
  1613. at_xdmac_chan_write(atchan, AT_XDMAC_CIE, atchan->save_cim);
  1614. wmb();
  1615. at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
  1616. }
  1617. }
  1618. return 0;
  1619. }
  1620. #endif /* CONFIG_PM_SLEEP */
  1621. static int at_xdmac_probe(struct platform_device *pdev)
  1622. {
  1623. struct resource *res;
  1624. struct at_xdmac *atxdmac;
  1625. int irq, size, nr_channels, i, ret;
  1626. void __iomem *base;
  1627. u32 reg;
  1628. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1629. if (!res)
  1630. return -EINVAL;
  1631. irq = platform_get_irq(pdev, 0);
  1632. if (irq < 0)
  1633. return irq;
  1634. base = devm_ioremap_resource(&pdev->dev, res);
  1635. if (IS_ERR(base))
  1636. return PTR_ERR(base);
  1637. /*
  1638. * Read number of xdmac channels, read helper function can't be used
  1639. * since atxdmac is not yet allocated and we need to know the number
  1640. * of channels to do the allocation.
  1641. */
  1642. reg = readl_relaxed(base + AT_XDMAC_GTYPE);
  1643. nr_channels = AT_XDMAC_NB_CH(reg);
  1644. if (nr_channels > AT_XDMAC_MAX_CHAN) {
  1645. dev_err(&pdev->dev, "invalid number of channels (%u)\n",
  1646. nr_channels);
  1647. return -EINVAL;
  1648. }
  1649. size = sizeof(*atxdmac);
  1650. size += nr_channels * sizeof(struct at_xdmac_chan);
  1651. atxdmac = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1652. if (!atxdmac) {
  1653. dev_err(&pdev->dev, "can't allocate at_xdmac structure\n");
  1654. return -ENOMEM;
  1655. }
  1656. atxdmac->regs = base;
  1657. atxdmac->irq = irq;
  1658. atxdmac->clk = devm_clk_get(&pdev->dev, "dma_clk");
  1659. if (IS_ERR(atxdmac->clk)) {
  1660. dev_err(&pdev->dev, "can't get dma_clk\n");
  1661. return PTR_ERR(atxdmac->clk);
  1662. }
  1663. /* Do not use dev res to prevent races with tasklet */
  1664. ret = request_irq(atxdmac->irq, at_xdmac_interrupt, 0, "at_xdmac", atxdmac);
  1665. if (ret) {
  1666. dev_err(&pdev->dev, "can't request irq\n");
  1667. return ret;
  1668. }
  1669. ret = clk_prepare_enable(atxdmac->clk);
  1670. if (ret) {
  1671. dev_err(&pdev->dev, "can't prepare or enable clock\n");
  1672. goto err_free_irq;
  1673. }
  1674. atxdmac->at_xdmac_desc_pool =
  1675. dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  1676. sizeof(struct at_xdmac_desc), 4, 0);
  1677. if (!atxdmac->at_xdmac_desc_pool) {
  1678. dev_err(&pdev->dev, "no memory for descriptors dma pool\n");
  1679. ret = -ENOMEM;
  1680. goto err_clk_disable;
  1681. }
  1682. dma_cap_set(DMA_CYCLIC, atxdmac->dma.cap_mask);
  1683. dma_cap_set(DMA_INTERLEAVE, atxdmac->dma.cap_mask);
  1684. dma_cap_set(DMA_MEMCPY, atxdmac->dma.cap_mask);
  1685. dma_cap_set(DMA_MEMSET, atxdmac->dma.cap_mask);
  1686. dma_cap_set(DMA_MEMSET_SG, atxdmac->dma.cap_mask);
  1687. dma_cap_set(DMA_SLAVE, atxdmac->dma.cap_mask);
  1688. /*
  1689. * Without DMA_PRIVATE the driver is not able to allocate more than
  1690. * one channel, second allocation fails in private_candidate.
  1691. */
  1692. dma_cap_set(DMA_PRIVATE, atxdmac->dma.cap_mask);
  1693. atxdmac->dma.dev = &pdev->dev;
  1694. atxdmac->dma.device_alloc_chan_resources = at_xdmac_alloc_chan_resources;
  1695. atxdmac->dma.device_free_chan_resources = at_xdmac_free_chan_resources;
  1696. atxdmac->dma.device_tx_status = at_xdmac_tx_status;
  1697. atxdmac->dma.device_issue_pending = at_xdmac_issue_pending;
  1698. atxdmac->dma.device_prep_dma_cyclic = at_xdmac_prep_dma_cyclic;
  1699. atxdmac->dma.device_prep_interleaved_dma = at_xdmac_prep_interleaved;
  1700. atxdmac->dma.device_prep_dma_memcpy = at_xdmac_prep_dma_memcpy;
  1701. atxdmac->dma.device_prep_dma_memset = at_xdmac_prep_dma_memset;
  1702. atxdmac->dma.device_prep_dma_memset_sg = at_xdmac_prep_dma_memset_sg;
  1703. atxdmac->dma.device_prep_slave_sg = at_xdmac_prep_slave_sg;
  1704. atxdmac->dma.device_config = at_xdmac_device_config;
  1705. atxdmac->dma.device_pause = at_xdmac_device_pause;
  1706. atxdmac->dma.device_resume = at_xdmac_device_resume;
  1707. atxdmac->dma.device_terminate_all = at_xdmac_device_terminate_all;
  1708. atxdmac->dma.src_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1709. atxdmac->dma.dst_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1710. atxdmac->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1711. atxdmac->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1712. /* Disable all chans and interrupts. */
  1713. at_xdmac_off(atxdmac);
  1714. /* Init channels. */
  1715. INIT_LIST_HEAD(&atxdmac->dma.channels);
  1716. for (i = 0; i < nr_channels; i++) {
  1717. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1718. atchan->chan.device = &atxdmac->dma;
  1719. list_add_tail(&atchan->chan.device_node,
  1720. &atxdmac->dma.channels);
  1721. atchan->ch_regs = at_xdmac_chan_reg_base(atxdmac, i);
  1722. atchan->mask = 1 << i;
  1723. spin_lock_init(&atchan->lock);
  1724. INIT_LIST_HEAD(&atchan->xfers_list);
  1725. INIT_LIST_HEAD(&atchan->free_descs_list);
  1726. tasklet_init(&atchan->tasklet, at_xdmac_tasklet,
  1727. (unsigned long)atchan);
  1728. /* Clear pending interrupts. */
  1729. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1730. cpu_relax();
  1731. }
  1732. platform_set_drvdata(pdev, atxdmac);
  1733. ret = dma_async_device_register(&atxdmac->dma);
  1734. if (ret) {
  1735. dev_err(&pdev->dev, "fail to register DMA engine device\n");
  1736. goto err_clk_disable;
  1737. }
  1738. ret = of_dma_controller_register(pdev->dev.of_node,
  1739. at_xdmac_xlate, atxdmac);
  1740. if (ret) {
  1741. dev_err(&pdev->dev, "could not register of dma controller\n");
  1742. goto err_dma_unregister;
  1743. }
  1744. dev_info(&pdev->dev, "%d channels, mapped at 0x%p\n",
  1745. nr_channels, atxdmac->regs);
  1746. return 0;
  1747. err_dma_unregister:
  1748. dma_async_device_unregister(&atxdmac->dma);
  1749. err_clk_disable:
  1750. clk_disable_unprepare(atxdmac->clk);
  1751. err_free_irq:
  1752. free_irq(atxdmac->irq, atxdmac);
  1753. return ret;
  1754. }
  1755. static int at_xdmac_remove(struct platform_device *pdev)
  1756. {
  1757. struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
  1758. int i;
  1759. at_xdmac_off(atxdmac);
  1760. of_dma_controller_free(pdev->dev.of_node);
  1761. dma_async_device_unregister(&atxdmac->dma);
  1762. clk_disable_unprepare(atxdmac->clk);
  1763. free_irq(atxdmac->irq, atxdmac);
  1764. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1765. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1766. tasklet_kill(&atchan->tasklet);
  1767. at_xdmac_free_chan_resources(&atchan->chan);
  1768. }
  1769. return 0;
  1770. }
  1771. static const struct dev_pm_ops atmel_xdmac_dev_pm_ops = {
  1772. .prepare = atmel_xdmac_prepare,
  1773. SET_LATE_SYSTEM_SLEEP_PM_OPS(atmel_xdmac_suspend, atmel_xdmac_resume)
  1774. };
  1775. static const struct of_device_id atmel_xdmac_dt_ids[] = {
  1776. {
  1777. .compatible = "atmel,sama5d4-dma",
  1778. }, {
  1779. /* sentinel */
  1780. }
  1781. };
  1782. MODULE_DEVICE_TABLE(of, atmel_xdmac_dt_ids);
  1783. static struct platform_driver at_xdmac_driver = {
  1784. .probe = at_xdmac_probe,
  1785. .remove = at_xdmac_remove,
  1786. .driver = {
  1787. .name = "at_xdmac",
  1788. .of_match_table = of_match_ptr(atmel_xdmac_dt_ids),
  1789. .pm = &atmel_xdmac_dev_pm_ops,
  1790. }
  1791. };
  1792. static int __init at_xdmac_init(void)
  1793. {
  1794. return platform_driver_probe(&at_xdmac_driver, at_xdmac_probe);
  1795. }
  1796. subsys_initcall(at_xdmac_init);
  1797. MODULE_DESCRIPTION("Atmel Extended DMA Controller driver");
  1798. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  1799. MODULE_LICENSE("GPL");