at_xdmac.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  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. u32 irq_status;
  197. unsigned long status;
  198. struct tasklet_struct tasklet;
  199. struct dma_slave_config sconfig;
  200. spinlock_t lock;
  201. struct list_head xfers_list;
  202. struct list_head free_descs_list;
  203. };
  204. /* ----- Controller ----- */
  205. struct at_xdmac {
  206. struct dma_device dma;
  207. void __iomem *regs;
  208. int irq;
  209. struct clk *clk;
  210. u32 save_gim;
  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. /* 64-bit alignment needed to update CNDA and CUBC registers in an atomic way. */
  228. struct at_xdmac_desc {
  229. struct at_xdmac_lld lld;
  230. enum dma_transfer_direction direction;
  231. struct dma_async_tx_descriptor tx_dma_desc;
  232. struct list_head desc_node;
  233. /* Following members are only used by the first descriptor */
  234. bool active_xfer;
  235. unsigned int xfer_size;
  236. struct list_head descs_list;
  237. struct list_head xfer_node;
  238. } __aligned(sizeof(u64));
  239. static inline void __iomem *at_xdmac_chan_reg_base(struct at_xdmac *atxdmac, unsigned int chan_nb)
  240. {
  241. return atxdmac->regs + (AT_XDMAC_CHAN_REG_BASE + chan_nb * 0x40);
  242. }
  243. #define at_xdmac_read(atxdmac, reg) readl_relaxed((atxdmac)->regs + (reg))
  244. #define at_xdmac_write(atxdmac, reg, value) \
  245. writel_relaxed((value), (atxdmac)->regs + (reg))
  246. #define at_xdmac_chan_read(atchan, reg) readl_relaxed((atchan)->ch_regs + (reg))
  247. #define at_xdmac_chan_write(atchan, reg, value) writel_relaxed((value), (atchan)->ch_regs + (reg))
  248. static inline struct at_xdmac_chan *to_at_xdmac_chan(struct dma_chan *dchan)
  249. {
  250. return container_of(dchan, struct at_xdmac_chan, chan);
  251. }
  252. static struct device *chan2dev(struct dma_chan *chan)
  253. {
  254. return &chan->dev->device;
  255. }
  256. static inline struct at_xdmac *to_at_xdmac(struct dma_device *ddev)
  257. {
  258. return container_of(ddev, struct at_xdmac, dma);
  259. }
  260. static inline struct at_xdmac_desc *txd_to_at_desc(struct dma_async_tx_descriptor *txd)
  261. {
  262. return container_of(txd, struct at_xdmac_desc, tx_dma_desc);
  263. }
  264. static inline int at_xdmac_chan_is_cyclic(struct at_xdmac_chan *atchan)
  265. {
  266. return test_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  267. }
  268. static inline int at_xdmac_chan_is_paused(struct at_xdmac_chan *atchan)
  269. {
  270. return test_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  271. }
  272. static inline int at_xdmac_csize(u32 maxburst)
  273. {
  274. int csize;
  275. csize = ffs(maxburst) - 1;
  276. if (csize > 4)
  277. csize = -EINVAL;
  278. return csize;
  279. };
  280. static inline u8 at_xdmac_get_dwidth(u32 cfg)
  281. {
  282. return (cfg & AT_XDMAC_CC_DWIDTH_MASK) >> AT_XDMAC_CC_DWIDTH_OFFSET;
  283. };
  284. static unsigned int init_nr_desc_per_channel = 64;
  285. module_param(init_nr_desc_per_channel, uint, 0644);
  286. MODULE_PARM_DESC(init_nr_desc_per_channel,
  287. "initial descriptors per channel (default: 64)");
  288. static bool at_xdmac_chan_is_enabled(struct at_xdmac_chan *atchan)
  289. {
  290. return at_xdmac_chan_read(atchan, AT_XDMAC_GS) & atchan->mask;
  291. }
  292. static void at_xdmac_off(struct at_xdmac *atxdmac)
  293. {
  294. at_xdmac_write(atxdmac, AT_XDMAC_GD, -1L);
  295. /* Wait that all chans are disabled. */
  296. while (at_xdmac_read(atxdmac, AT_XDMAC_GS))
  297. cpu_relax();
  298. at_xdmac_write(atxdmac, AT_XDMAC_GID, -1L);
  299. }
  300. /* Call with lock hold. */
  301. static void at_xdmac_start_xfer(struct at_xdmac_chan *atchan,
  302. struct at_xdmac_desc *first)
  303. {
  304. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  305. u32 reg;
  306. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, first);
  307. if (at_xdmac_chan_is_enabled(atchan))
  308. return;
  309. /* Set transfer as active to not try to start it again. */
  310. first->active_xfer = true;
  311. /* Tell xdmac where to get the first descriptor. */
  312. reg = AT_XDMAC_CNDA_NDA(first->tx_dma_desc.phys)
  313. | AT_XDMAC_CNDA_NDAIF(atchan->memif);
  314. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, reg);
  315. /*
  316. * When doing non cyclic transfer we need to use the next
  317. * descriptor view 2 since some fields of the configuration register
  318. * depend on transfer size and src/dest addresses.
  319. */
  320. if (at_xdmac_chan_is_cyclic(atchan))
  321. reg = AT_XDMAC_CNDC_NDVIEW_NDV1;
  322. else if (first->lld.mbr_ubc & AT_XDMAC_MBR_UBC_NDV3)
  323. reg = AT_XDMAC_CNDC_NDVIEW_NDV3;
  324. else
  325. reg = AT_XDMAC_CNDC_NDVIEW_NDV2;
  326. /*
  327. * Even if the register will be updated from the configuration in the
  328. * descriptor when using view 2 or higher, the PROT bit won't be set
  329. * properly. This bit can be modified only by using the channel
  330. * configuration register.
  331. */
  332. at_xdmac_chan_write(atchan, AT_XDMAC_CC, first->lld.mbr_cfg);
  333. reg |= AT_XDMAC_CNDC_NDDUP
  334. | AT_XDMAC_CNDC_NDSUP
  335. | AT_XDMAC_CNDC_NDE;
  336. at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, reg);
  337. dev_vdbg(chan2dev(&atchan->chan),
  338. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  339. __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  340. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  341. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  342. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  343. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  344. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  345. at_xdmac_chan_write(atchan, AT_XDMAC_CID, 0xffffffff);
  346. reg = AT_XDMAC_CIE_RBEIE | AT_XDMAC_CIE_WBEIE | AT_XDMAC_CIE_ROIE;
  347. /*
  348. * There is no end of list when doing cyclic dma, we need to get
  349. * an interrupt after each periods.
  350. */
  351. if (at_xdmac_chan_is_cyclic(atchan))
  352. at_xdmac_chan_write(atchan, AT_XDMAC_CIE,
  353. reg | AT_XDMAC_CIE_BIE);
  354. else
  355. at_xdmac_chan_write(atchan, AT_XDMAC_CIE,
  356. reg | AT_XDMAC_CIE_LIE);
  357. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atchan->mask);
  358. dev_vdbg(chan2dev(&atchan->chan),
  359. "%s: enable channel (0x%08x)\n", __func__, atchan->mask);
  360. wmb();
  361. at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
  362. dev_vdbg(chan2dev(&atchan->chan),
  363. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  364. __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  365. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  366. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  367. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  368. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  369. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  370. }
  371. static dma_cookie_t at_xdmac_tx_submit(struct dma_async_tx_descriptor *tx)
  372. {
  373. struct at_xdmac_desc *desc = txd_to_at_desc(tx);
  374. struct at_xdmac_chan *atchan = to_at_xdmac_chan(tx->chan);
  375. dma_cookie_t cookie;
  376. unsigned long irqflags;
  377. spin_lock_irqsave(&atchan->lock, irqflags);
  378. cookie = dma_cookie_assign(tx);
  379. dev_vdbg(chan2dev(tx->chan), "%s: atchan 0x%p, add desc 0x%p to xfers_list\n",
  380. __func__, atchan, desc);
  381. list_add_tail(&desc->xfer_node, &atchan->xfers_list);
  382. if (list_is_singular(&atchan->xfers_list))
  383. at_xdmac_start_xfer(atchan, desc);
  384. spin_unlock_irqrestore(&atchan->lock, irqflags);
  385. return cookie;
  386. }
  387. static struct at_xdmac_desc *at_xdmac_alloc_desc(struct dma_chan *chan,
  388. gfp_t gfp_flags)
  389. {
  390. struct at_xdmac_desc *desc;
  391. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  392. dma_addr_t phys;
  393. desc = dma_pool_zalloc(atxdmac->at_xdmac_desc_pool, gfp_flags, &phys);
  394. if (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. static 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 (%zu, 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=%zu, frame_size=%zu, 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=%zu, src icg=%zu, dst icg=%zu\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=0x%08x, 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. static 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=%zu, 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. bool initd;
  1201. ret = dma_cookie_status(chan, cookie, txstate);
  1202. if (ret == DMA_COMPLETE)
  1203. return ret;
  1204. if (!txstate)
  1205. return ret;
  1206. spin_lock_irqsave(&atchan->lock, flags);
  1207. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  1208. /*
  1209. * If the transfer has not been started yet, don't need to compute the
  1210. * residue, it's the transfer length.
  1211. */
  1212. if (!desc->active_xfer) {
  1213. dma_set_residue(txstate, desc->xfer_size);
  1214. goto spin_unlock;
  1215. }
  1216. residue = desc->xfer_size;
  1217. /*
  1218. * Flush FIFO: only relevant when the transfer is source peripheral
  1219. * synchronized. Flush is needed before reading CUBC because data in
  1220. * the FIFO are not reported by CUBC. Reporting a residue of the
  1221. * transfer length while we have data in FIFO can cause issue.
  1222. * Usecase: atmel USART has a timeout which means I have received
  1223. * characters but there is no more character received for a while. On
  1224. * timeout, it requests the residue. If the data are in the DMA FIFO,
  1225. * we will return a residue of the transfer length. It means no data
  1226. * received. If an application is waiting for these data, it will hang
  1227. * since we won't have another USART timeout without receiving new
  1228. * data.
  1229. */
  1230. mask = AT_XDMAC_CC_TYPE | AT_XDMAC_CC_DSYNC;
  1231. value = AT_XDMAC_CC_TYPE_PER_TRAN | AT_XDMAC_CC_DSYNC_PER2MEM;
  1232. if ((desc->lld.mbr_cfg & mask) == value) {
  1233. at_xdmac_write(atxdmac, AT_XDMAC_GSWF, atchan->mask);
  1234. while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
  1235. cpu_relax();
  1236. }
  1237. /*
  1238. * The easiest way to compute the residue should be to pause the DMA
  1239. * but doing this can lead to miss some data as some devices don't
  1240. * have FIFO.
  1241. * We need to read several registers because:
  1242. * - DMA is running therefore a descriptor change is possible while
  1243. * reading these registers
  1244. * - When the block transfer is done, the value of the CUBC register
  1245. * is set to its initial value until the fetch of the next descriptor.
  1246. * This value will corrupt the residue calculation so we have to skip
  1247. * it.
  1248. *
  1249. * INITD -------- ------------
  1250. * |____________________|
  1251. * _______________________ _______________
  1252. * NDA @desc2 \/ @desc3
  1253. * _______________________/\_______________
  1254. * __________ ___________ _______________
  1255. * CUBC 0 \/ MAX desc1 \/ MAX desc2
  1256. * __________/\___________/\_______________
  1257. *
  1258. * Since descriptors are aligned on 64 bits, we can assume that
  1259. * the update of NDA and CUBC is atomic.
  1260. * Memory barriers are used to ensure the read order of the registers.
  1261. * A max number of retries is set because unlikely it could never ends.
  1262. */
  1263. for (retry = 0; retry < AT_XDMAC_RESIDUE_MAX_RETRIES; retry++) {
  1264. check_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  1265. rmb();
  1266. cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC);
  1267. rmb();
  1268. initd = !!(at_xdmac_chan_read(atchan, AT_XDMAC_CC) & AT_XDMAC_CC_INITD);
  1269. rmb();
  1270. cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  1271. rmb();
  1272. if ((check_nda == cur_nda) && initd)
  1273. break;
  1274. }
  1275. if (unlikely(retry >= AT_XDMAC_RESIDUE_MAX_RETRIES)) {
  1276. ret = DMA_ERROR;
  1277. goto spin_unlock;
  1278. }
  1279. /*
  1280. * Flush FIFO: only relevant when the transfer is source peripheral
  1281. * synchronized. Another flush is needed here because CUBC is updated
  1282. * when the controller sends the data write command. It can lead to
  1283. * report data that are not written in the memory or the device. The
  1284. * FIFO flush ensures that data are really written.
  1285. */
  1286. if ((desc->lld.mbr_cfg & mask) == value) {
  1287. at_xdmac_write(atxdmac, AT_XDMAC_GSWF, atchan->mask);
  1288. while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
  1289. cpu_relax();
  1290. }
  1291. /*
  1292. * Remove size of all microblocks already transferred and the current
  1293. * one. Then add the remaining size to transfer of the current
  1294. * microblock.
  1295. */
  1296. descs_list = &desc->descs_list;
  1297. list_for_each_entry_safe(desc, _desc, descs_list, desc_node) {
  1298. dwidth = at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  1299. residue -= (desc->lld.mbr_ubc & 0xffffff) << dwidth;
  1300. if ((desc->lld.mbr_nda & 0xfffffffc) == cur_nda)
  1301. break;
  1302. }
  1303. residue += cur_ubc << dwidth;
  1304. dma_set_residue(txstate, residue);
  1305. dev_dbg(chan2dev(chan),
  1306. "%s: desc=0x%p, tx_dma_desc.phys=%pad, tx_status=%d, cookie=%d, residue=%d\n",
  1307. __func__, desc, &desc->tx_dma_desc.phys, ret, cookie, residue);
  1308. spin_unlock:
  1309. spin_unlock_irqrestore(&atchan->lock, flags);
  1310. return ret;
  1311. }
  1312. /* Call must be protected by lock. */
  1313. static void at_xdmac_remove_xfer(struct at_xdmac_chan *atchan,
  1314. struct at_xdmac_desc *desc)
  1315. {
  1316. dev_dbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1317. /*
  1318. * Remove the transfer from the transfer list then move the transfer
  1319. * descriptors into the free descriptors list.
  1320. */
  1321. list_del(&desc->xfer_node);
  1322. list_splice_init(&desc->descs_list, &atchan->free_descs_list);
  1323. }
  1324. static void at_xdmac_advance_work(struct at_xdmac_chan *atchan)
  1325. {
  1326. struct at_xdmac_desc *desc;
  1327. unsigned long flags;
  1328. spin_lock_irqsave(&atchan->lock, flags);
  1329. /*
  1330. * If channel is enabled, do nothing, advance_work will be triggered
  1331. * after the interruption.
  1332. */
  1333. if (!at_xdmac_chan_is_enabled(atchan) && !list_empty(&atchan->xfers_list)) {
  1334. desc = list_first_entry(&atchan->xfers_list,
  1335. struct at_xdmac_desc,
  1336. xfer_node);
  1337. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1338. if (!desc->active_xfer)
  1339. at_xdmac_start_xfer(atchan, desc);
  1340. }
  1341. spin_unlock_irqrestore(&atchan->lock, flags);
  1342. }
  1343. static void at_xdmac_handle_cyclic(struct at_xdmac_chan *atchan)
  1344. {
  1345. struct at_xdmac_desc *desc;
  1346. struct dma_async_tx_descriptor *txd;
  1347. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  1348. txd = &desc->tx_dma_desc;
  1349. if (txd->flags & DMA_PREP_INTERRUPT)
  1350. dmaengine_desc_get_callback_invoke(txd, NULL);
  1351. }
  1352. static void at_xdmac_tasklet(unsigned long data)
  1353. {
  1354. struct at_xdmac_chan *atchan = (struct at_xdmac_chan *)data;
  1355. struct at_xdmac_desc *desc;
  1356. u32 error_mask;
  1357. dev_dbg(chan2dev(&atchan->chan), "%s: status=0x%08x\n",
  1358. __func__, atchan->irq_status);
  1359. error_mask = AT_XDMAC_CIS_RBEIS
  1360. | AT_XDMAC_CIS_WBEIS
  1361. | AT_XDMAC_CIS_ROIS;
  1362. if (at_xdmac_chan_is_cyclic(atchan)) {
  1363. at_xdmac_handle_cyclic(atchan);
  1364. } else if ((atchan->irq_status & AT_XDMAC_CIS_LIS)
  1365. || (atchan->irq_status & error_mask)) {
  1366. struct dma_async_tx_descriptor *txd;
  1367. if (atchan->irq_status & AT_XDMAC_CIS_RBEIS)
  1368. dev_err(chan2dev(&atchan->chan), "read bus error!!!");
  1369. if (atchan->irq_status & AT_XDMAC_CIS_WBEIS)
  1370. dev_err(chan2dev(&atchan->chan), "write bus error!!!");
  1371. if (atchan->irq_status & AT_XDMAC_CIS_ROIS)
  1372. dev_err(chan2dev(&atchan->chan), "request overflow error!!!");
  1373. spin_lock_bh(&atchan->lock);
  1374. desc = list_first_entry(&atchan->xfers_list,
  1375. struct at_xdmac_desc,
  1376. xfer_node);
  1377. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1378. if (!desc->active_xfer) {
  1379. dev_err(chan2dev(&atchan->chan), "Xfer not active: exiting");
  1380. spin_unlock_bh(&atchan->lock);
  1381. return;
  1382. }
  1383. txd = &desc->tx_dma_desc;
  1384. at_xdmac_remove_xfer(atchan, desc);
  1385. spin_unlock_bh(&atchan->lock);
  1386. if (!at_xdmac_chan_is_cyclic(atchan)) {
  1387. dma_cookie_complete(txd);
  1388. if (txd->flags & DMA_PREP_INTERRUPT)
  1389. dmaengine_desc_get_callback_invoke(txd, NULL);
  1390. }
  1391. dma_run_dependencies(txd);
  1392. at_xdmac_advance_work(atchan);
  1393. }
  1394. }
  1395. static irqreturn_t at_xdmac_interrupt(int irq, void *dev_id)
  1396. {
  1397. struct at_xdmac *atxdmac = (struct at_xdmac *)dev_id;
  1398. struct at_xdmac_chan *atchan;
  1399. u32 imr, status, pending;
  1400. u32 chan_imr, chan_status;
  1401. int i, ret = IRQ_NONE;
  1402. do {
  1403. imr = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1404. status = at_xdmac_read(atxdmac, AT_XDMAC_GIS);
  1405. pending = status & imr;
  1406. dev_vdbg(atxdmac->dma.dev,
  1407. "%s: status=0x%08x, imr=0x%08x, pending=0x%08x\n",
  1408. __func__, status, imr, pending);
  1409. if (!pending)
  1410. break;
  1411. /* We have to find which channel has generated the interrupt. */
  1412. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1413. if (!((1 << i) & pending))
  1414. continue;
  1415. atchan = &atxdmac->chan[i];
  1416. chan_imr = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1417. chan_status = at_xdmac_chan_read(atchan, AT_XDMAC_CIS);
  1418. atchan->irq_status = chan_status & chan_imr;
  1419. dev_vdbg(atxdmac->dma.dev,
  1420. "%s: chan%d: imr=0x%x, status=0x%x\n",
  1421. __func__, i, chan_imr, chan_status);
  1422. dev_vdbg(chan2dev(&atchan->chan),
  1423. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  1424. __func__,
  1425. at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  1426. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  1427. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  1428. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  1429. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  1430. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  1431. if (atchan->irq_status & (AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS))
  1432. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  1433. tasklet_schedule(&atchan->tasklet);
  1434. ret = IRQ_HANDLED;
  1435. }
  1436. } while (pending);
  1437. return ret;
  1438. }
  1439. static void at_xdmac_issue_pending(struct dma_chan *chan)
  1440. {
  1441. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1442. dev_dbg(chan2dev(&atchan->chan), "%s\n", __func__);
  1443. if (!at_xdmac_chan_is_cyclic(atchan))
  1444. at_xdmac_advance_work(atchan);
  1445. return;
  1446. }
  1447. static int at_xdmac_device_config(struct dma_chan *chan,
  1448. struct dma_slave_config *config)
  1449. {
  1450. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1451. int ret;
  1452. unsigned long flags;
  1453. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1454. spin_lock_irqsave(&atchan->lock, flags);
  1455. ret = at_xdmac_set_slave_config(chan, config);
  1456. spin_unlock_irqrestore(&atchan->lock, flags);
  1457. return ret;
  1458. }
  1459. static int at_xdmac_device_pause(struct dma_chan *chan)
  1460. {
  1461. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1462. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1463. unsigned long flags;
  1464. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1465. if (test_and_set_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status))
  1466. return 0;
  1467. spin_lock_irqsave(&atchan->lock, flags);
  1468. at_xdmac_write(atxdmac, AT_XDMAC_GRWS, atchan->mask);
  1469. while (at_xdmac_chan_read(atchan, AT_XDMAC_CC)
  1470. & (AT_XDMAC_CC_WRIP | AT_XDMAC_CC_RDIP))
  1471. cpu_relax();
  1472. spin_unlock_irqrestore(&atchan->lock, flags);
  1473. return 0;
  1474. }
  1475. static int at_xdmac_device_resume(struct dma_chan *chan)
  1476. {
  1477. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1478. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1479. unsigned long flags;
  1480. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1481. spin_lock_irqsave(&atchan->lock, flags);
  1482. if (!at_xdmac_chan_is_paused(atchan)) {
  1483. spin_unlock_irqrestore(&atchan->lock, flags);
  1484. return 0;
  1485. }
  1486. at_xdmac_write(atxdmac, AT_XDMAC_GRWR, atchan->mask);
  1487. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  1488. spin_unlock_irqrestore(&atchan->lock, flags);
  1489. return 0;
  1490. }
  1491. static int at_xdmac_device_terminate_all(struct dma_chan *chan)
  1492. {
  1493. struct at_xdmac_desc *desc, *_desc;
  1494. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1495. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1496. unsigned long flags;
  1497. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1498. spin_lock_irqsave(&atchan->lock, flags);
  1499. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  1500. while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
  1501. cpu_relax();
  1502. /* Cancel all pending transfers. */
  1503. list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node)
  1504. at_xdmac_remove_xfer(atchan, desc);
  1505. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  1506. clear_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  1507. spin_unlock_irqrestore(&atchan->lock, flags);
  1508. return 0;
  1509. }
  1510. static int at_xdmac_alloc_chan_resources(struct dma_chan *chan)
  1511. {
  1512. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1513. struct at_xdmac_desc *desc;
  1514. int i;
  1515. unsigned long flags;
  1516. spin_lock_irqsave(&atchan->lock, flags);
  1517. if (at_xdmac_chan_is_enabled(atchan)) {
  1518. dev_err(chan2dev(chan),
  1519. "can't allocate channel resources (channel enabled)\n");
  1520. i = -EIO;
  1521. goto spin_unlock;
  1522. }
  1523. if (!list_empty(&atchan->free_descs_list)) {
  1524. dev_err(chan2dev(chan),
  1525. "can't allocate channel resources (channel not free from a previous use)\n");
  1526. i = -EIO;
  1527. goto spin_unlock;
  1528. }
  1529. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1530. desc = at_xdmac_alloc_desc(chan, GFP_ATOMIC);
  1531. if (!desc) {
  1532. dev_warn(chan2dev(chan),
  1533. "only %d descriptors have been allocated\n", i);
  1534. break;
  1535. }
  1536. list_add_tail(&desc->desc_node, &atchan->free_descs_list);
  1537. }
  1538. dma_cookie_init(chan);
  1539. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  1540. spin_unlock:
  1541. spin_unlock_irqrestore(&atchan->lock, flags);
  1542. return i;
  1543. }
  1544. static void at_xdmac_free_chan_resources(struct dma_chan *chan)
  1545. {
  1546. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1547. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  1548. struct at_xdmac_desc *desc, *_desc;
  1549. list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) {
  1550. dev_dbg(chan2dev(chan), "%s: freeing descriptor %p\n", __func__, desc);
  1551. list_del(&desc->desc_node);
  1552. dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys);
  1553. }
  1554. return;
  1555. }
  1556. #ifdef CONFIG_PM
  1557. static int atmel_xdmac_prepare(struct device *dev)
  1558. {
  1559. struct platform_device *pdev = to_platform_device(dev);
  1560. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1561. struct dma_chan *chan, *_chan;
  1562. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1563. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1564. /* Wait for transfer completion, except in cyclic case. */
  1565. if (at_xdmac_chan_is_enabled(atchan) && !at_xdmac_chan_is_cyclic(atchan))
  1566. return -EAGAIN;
  1567. }
  1568. return 0;
  1569. }
  1570. #else
  1571. # define atmel_xdmac_prepare NULL
  1572. #endif
  1573. #ifdef CONFIG_PM_SLEEP
  1574. static int atmel_xdmac_suspend(struct device *dev)
  1575. {
  1576. struct platform_device *pdev = to_platform_device(dev);
  1577. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1578. struct dma_chan *chan, *_chan;
  1579. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1580. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1581. atchan->save_cc = at_xdmac_chan_read(atchan, AT_XDMAC_CC);
  1582. if (at_xdmac_chan_is_cyclic(atchan)) {
  1583. if (!at_xdmac_chan_is_paused(atchan))
  1584. at_xdmac_device_pause(chan);
  1585. atchan->save_cim = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1586. atchan->save_cnda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA);
  1587. atchan->save_cndc = at_xdmac_chan_read(atchan, AT_XDMAC_CNDC);
  1588. }
  1589. }
  1590. atxdmac->save_gim = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1591. at_xdmac_off(atxdmac);
  1592. clk_disable_unprepare(atxdmac->clk);
  1593. return 0;
  1594. }
  1595. static int atmel_xdmac_resume(struct device *dev)
  1596. {
  1597. struct platform_device *pdev = to_platform_device(dev);
  1598. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1599. struct at_xdmac_chan *atchan;
  1600. struct dma_chan *chan, *_chan;
  1601. int i;
  1602. int ret;
  1603. ret = clk_prepare_enable(atxdmac->clk);
  1604. if (ret)
  1605. return ret;
  1606. /* Clear pending interrupts. */
  1607. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1608. atchan = &atxdmac->chan[i];
  1609. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1610. cpu_relax();
  1611. }
  1612. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atxdmac->save_gim);
  1613. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1614. atchan = to_at_xdmac_chan(chan);
  1615. at_xdmac_chan_write(atchan, AT_XDMAC_CC, atchan->save_cc);
  1616. if (at_xdmac_chan_is_cyclic(atchan)) {
  1617. if (at_xdmac_chan_is_paused(atchan))
  1618. at_xdmac_device_resume(chan);
  1619. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, atchan->save_cnda);
  1620. at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, atchan->save_cndc);
  1621. at_xdmac_chan_write(atchan, AT_XDMAC_CIE, atchan->save_cim);
  1622. wmb();
  1623. at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
  1624. }
  1625. }
  1626. return 0;
  1627. }
  1628. #endif /* CONFIG_PM_SLEEP */
  1629. static int at_xdmac_probe(struct platform_device *pdev)
  1630. {
  1631. struct resource *res;
  1632. struct at_xdmac *atxdmac;
  1633. int irq, size, nr_channels, i, ret;
  1634. void __iomem *base;
  1635. u32 reg;
  1636. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1637. if (!res)
  1638. return -EINVAL;
  1639. irq = platform_get_irq(pdev, 0);
  1640. if (irq < 0)
  1641. return irq;
  1642. base = devm_ioremap_resource(&pdev->dev, res);
  1643. if (IS_ERR(base))
  1644. return PTR_ERR(base);
  1645. /*
  1646. * Read number of xdmac channels, read helper function can't be used
  1647. * since atxdmac is not yet allocated and we need to know the number
  1648. * of channels to do the allocation.
  1649. */
  1650. reg = readl_relaxed(base + AT_XDMAC_GTYPE);
  1651. nr_channels = AT_XDMAC_NB_CH(reg);
  1652. if (nr_channels > AT_XDMAC_MAX_CHAN) {
  1653. dev_err(&pdev->dev, "invalid number of channels (%u)\n",
  1654. nr_channels);
  1655. return -EINVAL;
  1656. }
  1657. size = sizeof(*atxdmac);
  1658. size += nr_channels * sizeof(struct at_xdmac_chan);
  1659. atxdmac = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1660. if (!atxdmac) {
  1661. dev_err(&pdev->dev, "can't allocate at_xdmac structure\n");
  1662. return -ENOMEM;
  1663. }
  1664. atxdmac->regs = base;
  1665. atxdmac->irq = irq;
  1666. atxdmac->clk = devm_clk_get(&pdev->dev, "dma_clk");
  1667. if (IS_ERR(atxdmac->clk)) {
  1668. dev_err(&pdev->dev, "can't get dma_clk\n");
  1669. return PTR_ERR(atxdmac->clk);
  1670. }
  1671. /* Do not use dev res to prevent races with tasklet */
  1672. ret = request_irq(atxdmac->irq, at_xdmac_interrupt, 0, "at_xdmac", atxdmac);
  1673. if (ret) {
  1674. dev_err(&pdev->dev, "can't request irq\n");
  1675. return ret;
  1676. }
  1677. ret = clk_prepare_enable(atxdmac->clk);
  1678. if (ret) {
  1679. dev_err(&pdev->dev, "can't prepare or enable clock\n");
  1680. goto err_free_irq;
  1681. }
  1682. atxdmac->at_xdmac_desc_pool =
  1683. dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  1684. sizeof(struct at_xdmac_desc), 4, 0);
  1685. if (!atxdmac->at_xdmac_desc_pool) {
  1686. dev_err(&pdev->dev, "no memory for descriptors dma pool\n");
  1687. ret = -ENOMEM;
  1688. goto err_clk_disable;
  1689. }
  1690. dma_cap_set(DMA_CYCLIC, atxdmac->dma.cap_mask);
  1691. dma_cap_set(DMA_INTERLEAVE, atxdmac->dma.cap_mask);
  1692. dma_cap_set(DMA_MEMCPY, atxdmac->dma.cap_mask);
  1693. dma_cap_set(DMA_MEMSET, atxdmac->dma.cap_mask);
  1694. dma_cap_set(DMA_MEMSET_SG, atxdmac->dma.cap_mask);
  1695. dma_cap_set(DMA_SLAVE, atxdmac->dma.cap_mask);
  1696. /*
  1697. * Without DMA_PRIVATE the driver is not able to allocate more than
  1698. * one channel, second allocation fails in private_candidate.
  1699. */
  1700. dma_cap_set(DMA_PRIVATE, atxdmac->dma.cap_mask);
  1701. atxdmac->dma.dev = &pdev->dev;
  1702. atxdmac->dma.device_alloc_chan_resources = at_xdmac_alloc_chan_resources;
  1703. atxdmac->dma.device_free_chan_resources = at_xdmac_free_chan_resources;
  1704. atxdmac->dma.device_tx_status = at_xdmac_tx_status;
  1705. atxdmac->dma.device_issue_pending = at_xdmac_issue_pending;
  1706. atxdmac->dma.device_prep_dma_cyclic = at_xdmac_prep_dma_cyclic;
  1707. atxdmac->dma.device_prep_interleaved_dma = at_xdmac_prep_interleaved;
  1708. atxdmac->dma.device_prep_dma_memcpy = at_xdmac_prep_dma_memcpy;
  1709. atxdmac->dma.device_prep_dma_memset = at_xdmac_prep_dma_memset;
  1710. atxdmac->dma.device_prep_dma_memset_sg = at_xdmac_prep_dma_memset_sg;
  1711. atxdmac->dma.device_prep_slave_sg = at_xdmac_prep_slave_sg;
  1712. atxdmac->dma.device_config = at_xdmac_device_config;
  1713. atxdmac->dma.device_pause = at_xdmac_device_pause;
  1714. atxdmac->dma.device_resume = at_xdmac_device_resume;
  1715. atxdmac->dma.device_terminate_all = at_xdmac_device_terminate_all;
  1716. atxdmac->dma.src_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1717. atxdmac->dma.dst_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1718. atxdmac->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1719. atxdmac->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1720. /* Disable all chans and interrupts. */
  1721. at_xdmac_off(atxdmac);
  1722. /* Init channels. */
  1723. INIT_LIST_HEAD(&atxdmac->dma.channels);
  1724. for (i = 0; i < nr_channels; i++) {
  1725. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1726. atchan->chan.device = &atxdmac->dma;
  1727. list_add_tail(&atchan->chan.device_node,
  1728. &atxdmac->dma.channels);
  1729. atchan->ch_regs = at_xdmac_chan_reg_base(atxdmac, i);
  1730. atchan->mask = 1 << i;
  1731. spin_lock_init(&atchan->lock);
  1732. INIT_LIST_HEAD(&atchan->xfers_list);
  1733. INIT_LIST_HEAD(&atchan->free_descs_list);
  1734. tasklet_init(&atchan->tasklet, at_xdmac_tasklet,
  1735. (unsigned long)atchan);
  1736. /* Clear pending interrupts. */
  1737. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1738. cpu_relax();
  1739. }
  1740. platform_set_drvdata(pdev, atxdmac);
  1741. ret = dma_async_device_register(&atxdmac->dma);
  1742. if (ret) {
  1743. dev_err(&pdev->dev, "fail to register DMA engine device\n");
  1744. goto err_clk_disable;
  1745. }
  1746. ret = of_dma_controller_register(pdev->dev.of_node,
  1747. at_xdmac_xlate, atxdmac);
  1748. if (ret) {
  1749. dev_err(&pdev->dev, "could not register of dma controller\n");
  1750. goto err_dma_unregister;
  1751. }
  1752. dev_info(&pdev->dev, "%d channels, mapped at 0x%p\n",
  1753. nr_channels, atxdmac->regs);
  1754. return 0;
  1755. err_dma_unregister:
  1756. dma_async_device_unregister(&atxdmac->dma);
  1757. err_clk_disable:
  1758. clk_disable_unprepare(atxdmac->clk);
  1759. err_free_irq:
  1760. free_irq(atxdmac->irq, atxdmac);
  1761. return ret;
  1762. }
  1763. static int at_xdmac_remove(struct platform_device *pdev)
  1764. {
  1765. struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
  1766. int i;
  1767. at_xdmac_off(atxdmac);
  1768. of_dma_controller_free(pdev->dev.of_node);
  1769. dma_async_device_unregister(&atxdmac->dma);
  1770. clk_disable_unprepare(atxdmac->clk);
  1771. free_irq(atxdmac->irq, atxdmac);
  1772. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1773. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1774. tasklet_kill(&atchan->tasklet);
  1775. at_xdmac_free_chan_resources(&atchan->chan);
  1776. }
  1777. return 0;
  1778. }
  1779. static const struct dev_pm_ops atmel_xdmac_dev_pm_ops = {
  1780. .prepare = atmel_xdmac_prepare,
  1781. SET_LATE_SYSTEM_SLEEP_PM_OPS(atmel_xdmac_suspend, atmel_xdmac_resume)
  1782. };
  1783. static const struct of_device_id atmel_xdmac_dt_ids[] = {
  1784. {
  1785. .compatible = "atmel,sama5d4-dma",
  1786. }, {
  1787. /* sentinel */
  1788. }
  1789. };
  1790. MODULE_DEVICE_TABLE(of, atmel_xdmac_dt_ids);
  1791. static struct platform_driver at_xdmac_driver = {
  1792. .probe = at_xdmac_probe,
  1793. .remove = at_xdmac_remove,
  1794. .driver = {
  1795. .name = "at_xdmac",
  1796. .of_match_table = of_match_ptr(atmel_xdmac_dt_ids),
  1797. .pm = &atmel_xdmac_dev_pm_ops,
  1798. }
  1799. };
  1800. static int __init at_xdmac_init(void)
  1801. {
  1802. return platform_driver_probe(&at_xdmac_driver, at_xdmac_probe);
  1803. }
  1804. subsys_initcall(at_xdmac_init);
  1805. MODULE_DESCRIPTION("Atmel Extended DMA Controller driver");
  1806. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  1807. MODULE_LICENSE("GPL");