at_xdmac.c 66 KB

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