at_xdmac.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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 & (h) << 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_DMA_BUSWIDTHS\
  171. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) |\
  172. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |\
  173. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |\
  174. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |\
  175. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  176. enum atc_status {
  177. AT_XDMAC_CHAN_IS_CYCLIC = 0,
  178. AT_XDMAC_CHAN_IS_PAUSED,
  179. };
  180. /* ----- Channels ----- */
  181. struct at_xdmac_chan {
  182. struct dma_chan chan;
  183. void __iomem *ch_regs;
  184. u32 mask; /* Channel Mask */
  185. u32 cfg[2]; /* Channel Configuration Register */
  186. #define AT_XDMAC_DEV_TO_MEM_CFG 0 /* Predifined dev to mem channel conf */
  187. #define AT_XDMAC_MEM_TO_DEV_CFG 1 /* Predifined mem to dev channel conf */
  188. u8 perid; /* Peripheral ID */
  189. u8 perif; /* Peripheral Interface */
  190. u8 memif; /* Memory Interface */
  191. u32 per_src_addr;
  192. u32 per_dst_addr;
  193. u32 save_cc;
  194. u32 save_cim;
  195. u32 save_cnda;
  196. u32 save_cndc;
  197. unsigned long status;
  198. struct tasklet_struct tasklet;
  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. };
  223. struct at_xdmac_desc {
  224. struct at_xdmac_lld lld;
  225. enum dma_transfer_direction direction;
  226. struct dma_async_tx_descriptor tx_dma_desc;
  227. struct list_head desc_node;
  228. /* Following members are only used by the first descriptor */
  229. bool active_xfer;
  230. unsigned int xfer_size;
  231. struct list_head descs_list;
  232. struct list_head xfer_node;
  233. };
  234. static inline void __iomem *at_xdmac_chan_reg_base(struct at_xdmac *atxdmac, unsigned int chan_nb)
  235. {
  236. return atxdmac->regs + (AT_XDMAC_CHAN_REG_BASE + chan_nb * 0x40);
  237. }
  238. #define at_xdmac_read(atxdmac, reg) readl_relaxed((atxdmac)->regs + (reg))
  239. #define at_xdmac_write(atxdmac, reg, value) \
  240. writel_relaxed((value), (atxdmac)->regs + (reg))
  241. #define at_xdmac_chan_read(atchan, reg) readl_relaxed((atchan)->ch_regs + (reg))
  242. #define at_xdmac_chan_write(atchan, reg, value) writel_relaxed((value), (atchan)->ch_regs + (reg))
  243. static inline struct at_xdmac_chan *to_at_xdmac_chan(struct dma_chan *dchan)
  244. {
  245. return container_of(dchan, struct at_xdmac_chan, chan);
  246. }
  247. static struct device *chan2dev(struct dma_chan *chan)
  248. {
  249. return &chan->dev->device;
  250. }
  251. static inline struct at_xdmac *to_at_xdmac(struct dma_device *ddev)
  252. {
  253. return container_of(ddev, struct at_xdmac, dma);
  254. }
  255. static inline struct at_xdmac_desc *txd_to_at_desc(struct dma_async_tx_descriptor *txd)
  256. {
  257. return container_of(txd, struct at_xdmac_desc, tx_dma_desc);
  258. }
  259. static inline int at_xdmac_chan_is_cyclic(struct at_xdmac_chan *atchan)
  260. {
  261. return test_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  262. }
  263. static inline int at_xdmac_chan_is_paused(struct at_xdmac_chan *atchan)
  264. {
  265. return test_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  266. }
  267. static inline int at_xdmac_csize(u32 maxburst)
  268. {
  269. int csize;
  270. csize = ffs(maxburst) - 1;
  271. if (csize > 4)
  272. csize = -EINVAL;
  273. return csize;
  274. };
  275. static inline u8 at_xdmac_get_dwidth(u32 cfg)
  276. {
  277. return (cfg & AT_XDMAC_CC_DWIDTH_MASK) >> AT_XDMAC_CC_DWIDTH_OFFSET;
  278. };
  279. static unsigned int init_nr_desc_per_channel = 64;
  280. module_param(init_nr_desc_per_channel, uint, 0644);
  281. MODULE_PARM_DESC(init_nr_desc_per_channel,
  282. "initial descriptors per channel (default: 64)");
  283. static bool at_xdmac_chan_is_enabled(struct at_xdmac_chan *atchan)
  284. {
  285. return at_xdmac_chan_read(atchan, AT_XDMAC_GS) & atchan->mask;
  286. }
  287. static void at_xdmac_off(struct at_xdmac *atxdmac)
  288. {
  289. at_xdmac_write(atxdmac, AT_XDMAC_GD, -1L);
  290. /* Wait that all chans are disabled. */
  291. while (at_xdmac_read(atxdmac, AT_XDMAC_GS))
  292. cpu_relax();
  293. at_xdmac_write(atxdmac, AT_XDMAC_GID, -1L);
  294. }
  295. /* Call with lock hold. */
  296. static void at_xdmac_start_xfer(struct at_xdmac_chan *atchan,
  297. struct at_xdmac_desc *first)
  298. {
  299. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  300. u32 reg;
  301. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, first);
  302. if (at_xdmac_chan_is_enabled(atchan))
  303. return;
  304. /* Set transfer as active to not try to start it again. */
  305. first->active_xfer = true;
  306. /* Tell xdmac where to get the first descriptor. */
  307. reg = AT_XDMAC_CNDA_NDA(first->tx_dma_desc.phys)
  308. | AT_XDMAC_CNDA_NDAIF(atchan->memif);
  309. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, reg);
  310. /*
  311. * When doing non cyclic transfer we need to use the next
  312. * descriptor view 2 since some fields of the configuration register
  313. * depend on transfer size and src/dest addresses.
  314. */
  315. if (at_xdmac_chan_is_cyclic(atchan)) {
  316. reg = AT_XDMAC_CNDC_NDVIEW_NDV1;
  317. at_xdmac_chan_write(atchan, AT_XDMAC_CC, first->lld.mbr_cfg);
  318. } else {
  319. /*
  320. * No need to write AT_XDMAC_CC reg, it will be done when the
  321. * descriptor is fecthed.
  322. */
  323. reg = AT_XDMAC_CNDC_NDVIEW_NDV2;
  324. }
  325. reg |= AT_XDMAC_CNDC_NDDUP
  326. | AT_XDMAC_CNDC_NDSUP
  327. | AT_XDMAC_CNDC_NDE;
  328. at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, reg);
  329. dev_vdbg(chan2dev(&atchan->chan),
  330. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  331. __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  332. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  333. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  334. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  335. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  336. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  337. at_xdmac_chan_write(atchan, AT_XDMAC_CID, 0xffffffff);
  338. reg = AT_XDMAC_CIE_RBEIE | AT_XDMAC_CIE_WBEIE | AT_XDMAC_CIE_ROIE;
  339. /*
  340. * There is no end of list when doing cyclic dma, we need to get
  341. * an interrupt after each periods.
  342. */
  343. if (at_xdmac_chan_is_cyclic(atchan))
  344. at_xdmac_chan_write(atchan, AT_XDMAC_CIE,
  345. reg | AT_XDMAC_CIE_BIE);
  346. else
  347. at_xdmac_chan_write(atchan, AT_XDMAC_CIE,
  348. reg | AT_XDMAC_CIE_LIE);
  349. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atchan->mask);
  350. dev_vdbg(chan2dev(&atchan->chan),
  351. "%s: enable channel (0x%08x)\n", __func__, atchan->mask);
  352. wmb();
  353. at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
  354. dev_vdbg(chan2dev(&atchan->chan),
  355. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  356. __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  357. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  358. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  359. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  360. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  361. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  362. }
  363. static dma_cookie_t at_xdmac_tx_submit(struct dma_async_tx_descriptor *tx)
  364. {
  365. struct at_xdmac_desc *desc = txd_to_at_desc(tx);
  366. struct at_xdmac_chan *atchan = to_at_xdmac_chan(tx->chan);
  367. dma_cookie_t cookie;
  368. spin_lock_bh(&atchan->lock);
  369. cookie = dma_cookie_assign(tx);
  370. dev_vdbg(chan2dev(tx->chan), "%s: atchan 0x%p, add desc 0x%p to xfers_list\n",
  371. __func__, atchan, desc);
  372. list_add_tail(&desc->xfer_node, &atchan->xfers_list);
  373. if (list_is_singular(&atchan->xfers_list))
  374. at_xdmac_start_xfer(atchan, desc);
  375. spin_unlock_bh(&atchan->lock);
  376. return cookie;
  377. }
  378. static struct at_xdmac_desc *at_xdmac_alloc_desc(struct dma_chan *chan,
  379. gfp_t gfp_flags)
  380. {
  381. struct at_xdmac_desc *desc;
  382. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  383. dma_addr_t phys;
  384. desc = dma_pool_alloc(atxdmac->at_xdmac_desc_pool, gfp_flags, &phys);
  385. if (desc) {
  386. memset(desc, 0, sizeof(*desc));
  387. INIT_LIST_HEAD(&desc->descs_list);
  388. dma_async_tx_descriptor_init(&desc->tx_dma_desc, chan);
  389. desc->tx_dma_desc.tx_submit = at_xdmac_tx_submit;
  390. desc->tx_dma_desc.phys = phys;
  391. }
  392. return desc;
  393. }
  394. /* Call must be protected by lock. */
  395. static struct at_xdmac_desc *at_xdmac_get_desc(struct at_xdmac_chan *atchan)
  396. {
  397. struct at_xdmac_desc *desc;
  398. if (list_empty(&atchan->free_descs_list)) {
  399. desc = at_xdmac_alloc_desc(&atchan->chan, GFP_NOWAIT);
  400. } else {
  401. desc = list_first_entry(&atchan->free_descs_list,
  402. struct at_xdmac_desc, desc_node);
  403. list_del(&desc->desc_node);
  404. desc->active_xfer = false;
  405. }
  406. return desc;
  407. }
  408. static struct dma_chan *at_xdmac_xlate(struct of_phandle_args *dma_spec,
  409. struct of_dma *of_dma)
  410. {
  411. struct at_xdmac *atxdmac = of_dma->of_dma_data;
  412. struct at_xdmac_chan *atchan;
  413. struct dma_chan *chan;
  414. struct device *dev = atxdmac->dma.dev;
  415. if (dma_spec->args_count != 1) {
  416. dev_err(dev, "dma phandler args: bad number of args\n");
  417. return NULL;
  418. }
  419. chan = dma_get_any_slave_channel(&atxdmac->dma);
  420. if (!chan) {
  421. dev_err(dev, "can't get a dma channel\n");
  422. return NULL;
  423. }
  424. atchan = to_at_xdmac_chan(chan);
  425. atchan->memif = AT91_XDMAC_DT_GET_MEM_IF(dma_spec->args[0]);
  426. atchan->perif = AT91_XDMAC_DT_GET_PER_IF(dma_spec->args[0]);
  427. atchan->perid = AT91_XDMAC_DT_GET_PERID(dma_spec->args[0]);
  428. dev_dbg(dev, "chan dt cfg: memif=%u perif=%u perid=%u\n",
  429. atchan->memif, atchan->perif, atchan->perid);
  430. return chan;
  431. }
  432. static int at_xdmac_set_slave_config(struct dma_chan *chan,
  433. struct dma_slave_config *sconfig)
  434. {
  435. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  436. u8 dwidth;
  437. int csize;
  438. atchan->cfg[AT_XDMAC_DEV_TO_MEM_CFG] =
  439. AT91_XDMAC_DT_PERID(atchan->perid)
  440. | AT_XDMAC_CC_DAM_INCREMENTED_AM
  441. | AT_XDMAC_CC_SAM_FIXED_AM
  442. | AT_XDMAC_CC_DIF(atchan->memif)
  443. | AT_XDMAC_CC_SIF(atchan->perif)
  444. | AT_XDMAC_CC_SWREQ_HWR_CONNECTED
  445. | AT_XDMAC_CC_DSYNC_PER2MEM
  446. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  447. | AT_XDMAC_CC_TYPE_PER_TRAN;
  448. csize = at_xdmac_csize(sconfig->src_maxburst);
  449. if (csize < 0) {
  450. dev_err(chan2dev(chan), "invalid src maxburst value\n");
  451. return -EINVAL;
  452. }
  453. atchan->cfg[AT_XDMAC_DEV_TO_MEM_CFG] |= AT_XDMAC_CC_CSIZE(csize);
  454. dwidth = ffs(sconfig->src_addr_width) - 1;
  455. atchan->cfg[AT_XDMAC_DEV_TO_MEM_CFG] |= AT_XDMAC_CC_DWIDTH(dwidth);
  456. atchan->cfg[AT_XDMAC_MEM_TO_DEV_CFG] =
  457. AT91_XDMAC_DT_PERID(atchan->perid)
  458. | AT_XDMAC_CC_DAM_FIXED_AM
  459. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  460. | AT_XDMAC_CC_DIF(atchan->perif)
  461. | AT_XDMAC_CC_SIF(atchan->memif)
  462. | AT_XDMAC_CC_SWREQ_HWR_CONNECTED
  463. | AT_XDMAC_CC_DSYNC_MEM2PER
  464. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  465. | AT_XDMAC_CC_TYPE_PER_TRAN;
  466. csize = at_xdmac_csize(sconfig->dst_maxburst);
  467. if (csize < 0) {
  468. dev_err(chan2dev(chan), "invalid src maxburst value\n");
  469. return -EINVAL;
  470. }
  471. atchan->cfg[AT_XDMAC_MEM_TO_DEV_CFG] |= AT_XDMAC_CC_CSIZE(csize);
  472. dwidth = ffs(sconfig->dst_addr_width) - 1;
  473. atchan->cfg[AT_XDMAC_MEM_TO_DEV_CFG] |= AT_XDMAC_CC_DWIDTH(dwidth);
  474. /* Src and dst addr are needed to configure the link list descriptor. */
  475. atchan->per_src_addr = sconfig->src_addr;
  476. atchan->per_dst_addr = sconfig->dst_addr;
  477. dev_dbg(chan2dev(chan),
  478. "%s: cfg[dev2mem]=0x%08x, cfg[mem2dev]=0x%08x, per_src_addr=0x%08x, per_dst_addr=0x%08x\n",
  479. __func__, atchan->cfg[AT_XDMAC_DEV_TO_MEM_CFG],
  480. atchan->cfg[AT_XDMAC_MEM_TO_DEV_CFG],
  481. atchan->per_src_addr, atchan->per_dst_addr);
  482. return 0;
  483. }
  484. static struct dma_async_tx_descriptor *
  485. at_xdmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  486. unsigned int sg_len, enum dma_transfer_direction direction,
  487. unsigned long flags, void *context)
  488. {
  489. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  490. struct at_xdmac_desc *first = NULL, *prev = NULL;
  491. struct scatterlist *sg;
  492. int i;
  493. unsigned int xfer_size = 0;
  494. if (!sgl)
  495. return NULL;
  496. if (!is_slave_direction(direction)) {
  497. dev_err(chan2dev(chan), "invalid DMA direction\n");
  498. return NULL;
  499. }
  500. dev_dbg(chan2dev(chan), "%s: sg_len=%d, dir=%s, flags=0x%lx\n",
  501. __func__, sg_len,
  502. direction == DMA_MEM_TO_DEV ? "to device" : "from device",
  503. flags);
  504. /* Protect dma_sconfig field that can be modified by set_slave_conf. */
  505. spin_lock_bh(&atchan->lock);
  506. /* Prepare descriptors. */
  507. for_each_sg(sgl, sg, sg_len, i) {
  508. struct at_xdmac_desc *desc = NULL;
  509. u32 len, mem, dwidth, fixed_dwidth;
  510. len = sg_dma_len(sg);
  511. mem = sg_dma_address(sg);
  512. if (unlikely(!len)) {
  513. dev_err(chan2dev(chan), "sg data length is zero\n");
  514. spin_unlock_bh(&atchan->lock);
  515. return NULL;
  516. }
  517. dev_dbg(chan2dev(chan), "%s: * sg%d len=%u, mem=0x%08x\n",
  518. __func__, i, len, mem);
  519. desc = at_xdmac_get_desc(atchan);
  520. if (!desc) {
  521. dev_err(chan2dev(chan), "can't get descriptor\n");
  522. if (first)
  523. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  524. spin_unlock_bh(&atchan->lock);
  525. return NULL;
  526. }
  527. /* Linked list descriptor setup. */
  528. if (direction == DMA_DEV_TO_MEM) {
  529. desc->lld.mbr_sa = atchan->per_src_addr;
  530. desc->lld.mbr_da = mem;
  531. desc->lld.mbr_cfg = atchan->cfg[AT_XDMAC_DEV_TO_MEM_CFG];
  532. } else {
  533. desc->lld.mbr_sa = mem;
  534. desc->lld.mbr_da = atchan->per_dst_addr;
  535. desc->lld.mbr_cfg = atchan->cfg[AT_XDMAC_MEM_TO_DEV_CFG];
  536. }
  537. dwidth = at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  538. fixed_dwidth = IS_ALIGNED(len, 1 << dwidth)
  539. ? at_xdmac_get_dwidth(desc->lld.mbr_cfg)
  540. : AT_XDMAC_CC_DWIDTH_BYTE;
  541. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2 /* next descriptor view */
  542. | AT_XDMAC_MBR_UBC_NDEN /* next descriptor dst parameter update */
  543. | AT_XDMAC_MBR_UBC_NSEN /* next descriptor src parameter update */
  544. | (i == sg_len - 1 ? 0 : AT_XDMAC_MBR_UBC_NDE) /* descriptor fetch */
  545. | (len >> fixed_dwidth); /* microblock length */
  546. dev_dbg(chan2dev(chan),
  547. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  548. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  549. /* Chain lld. */
  550. if (prev) {
  551. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  552. dev_dbg(chan2dev(chan),
  553. "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  554. __func__, prev, &prev->lld.mbr_nda);
  555. }
  556. prev = desc;
  557. if (!first)
  558. first = desc;
  559. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  560. __func__, desc, first);
  561. list_add_tail(&desc->desc_node, &first->descs_list);
  562. xfer_size += len;
  563. }
  564. spin_unlock_bh(&atchan->lock);
  565. first->tx_dma_desc.flags = flags;
  566. first->xfer_size = xfer_size;
  567. first->direction = direction;
  568. return &first->tx_dma_desc;
  569. }
  570. static struct dma_async_tx_descriptor *
  571. at_xdmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
  572. size_t buf_len, size_t period_len,
  573. enum dma_transfer_direction direction,
  574. unsigned long flags)
  575. {
  576. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  577. struct at_xdmac_desc *first = NULL, *prev = NULL;
  578. unsigned int periods = buf_len / period_len;
  579. int i;
  580. u32 cfg;
  581. dev_dbg(chan2dev(chan), "%s: buf_addr=%pad, buf_len=%zd, period_len=%zd, dir=%s, flags=0x%lx\n",
  582. __func__, &buf_addr, buf_len, period_len,
  583. direction == DMA_MEM_TO_DEV ? "mem2per" : "per2mem", flags);
  584. if (!is_slave_direction(direction)) {
  585. dev_err(chan2dev(chan), "invalid DMA direction\n");
  586. return NULL;
  587. }
  588. if (test_and_set_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status)) {
  589. dev_err(chan2dev(chan), "channel currently used\n");
  590. return NULL;
  591. }
  592. for (i = 0; i < periods; i++) {
  593. struct at_xdmac_desc *desc = NULL;
  594. spin_lock_bh(&atchan->lock);
  595. desc = at_xdmac_get_desc(atchan);
  596. if (!desc) {
  597. dev_err(chan2dev(chan), "can't get descriptor\n");
  598. if (first)
  599. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  600. spin_unlock_bh(&atchan->lock);
  601. return NULL;
  602. }
  603. spin_unlock_bh(&atchan->lock);
  604. dev_dbg(chan2dev(chan),
  605. "%s: desc=0x%p, tx_dma_desc.phys=%pad\n",
  606. __func__, desc, &desc->tx_dma_desc.phys);
  607. if (direction == DMA_DEV_TO_MEM) {
  608. desc->lld.mbr_sa = atchan->per_src_addr;
  609. desc->lld.mbr_da = buf_addr + i * period_len;
  610. cfg = atchan->cfg[AT_XDMAC_DEV_TO_MEM_CFG];
  611. } else {
  612. desc->lld.mbr_sa = buf_addr + i * period_len;
  613. desc->lld.mbr_da = atchan->per_dst_addr;
  614. cfg = atchan->cfg[AT_XDMAC_MEM_TO_DEV_CFG];
  615. }
  616. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV1
  617. | AT_XDMAC_MBR_UBC_NDEN
  618. | AT_XDMAC_MBR_UBC_NSEN
  619. | AT_XDMAC_MBR_UBC_NDE
  620. | period_len >> at_xdmac_get_dwidth(cfg);
  621. dev_dbg(chan2dev(chan),
  622. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  623. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  624. /* Chain lld. */
  625. if (prev) {
  626. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  627. dev_dbg(chan2dev(chan),
  628. "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  629. __func__, prev, &prev->lld.mbr_nda);
  630. }
  631. prev = desc;
  632. if (!first)
  633. first = desc;
  634. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  635. __func__, desc, first);
  636. list_add_tail(&desc->desc_node, &first->descs_list);
  637. }
  638. prev->lld.mbr_nda = first->tx_dma_desc.phys;
  639. dev_dbg(chan2dev(chan),
  640. "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  641. __func__, prev, &prev->lld.mbr_nda);
  642. first->tx_dma_desc.flags = flags;
  643. first->xfer_size = buf_len;
  644. first->direction = direction;
  645. return &first->tx_dma_desc;
  646. }
  647. static struct dma_async_tx_descriptor *
  648. at_xdmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  649. size_t len, unsigned long flags)
  650. {
  651. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  652. struct at_xdmac_desc *first = NULL, *prev = NULL;
  653. size_t remaining_size = len, xfer_size = 0, ublen;
  654. dma_addr_t src_addr = src, dst_addr = dest;
  655. u32 dwidth;
  656. /*
  657. * WARNING: We don't know the direction, it involves we can't
  658. * dynamically set the source and dest interface so we have to use the
  659. * same one. Only interface 0 allows EBI access. Hopefully we can
  660. * access DDR through both ports (at least on SAMA5D4x), so we can use
  661. * the same interface for source and dest, that solves the fact we
  662. * don't know the direction.
  663. */
  664. u32 chan_cc = AT_XDMAC_CC_DAM_INCREMENTED_AM
  665. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  666. | AT_XDMAC_CC_DIF(0)
  667. | AT_XDMAC_CC_SIF(0)
  668. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  669. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  670. dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, len=%zd, flags=0x%lx\n",
  671. __func__, &src, &dest, len, flags);
  672. if (unlikely(!len))
  673. return NULL;
  674. /*
  675. * Check address alignment to select the greater data width we can use.
  676. * Some XDMAC implementations don't provide dword transfer, in this
  677. * case selecting dword has the same behavior as selecting word transfers.
  678. */
  679. if (!((src_addr | dst_addr) & 7)) {
  680. dwidth = AT_XDMAC_CC_DWIDTH_DWORD;
  681. dev_dbg(chan2dev(chan), "%s: dwidth: double word\n", __func__);
  682. } else if (!((src_addr | dst_addr) & 3)) {
  683. dwidth = AT_XDMAC_CC_DWIDTH_WORD;
  684. dev_dbg(chan2dev(chan), "%s: dwidth: word\n", __func__);
  685. } else if (!((src_addr | dst_addr) & 1)) {
  686. dwidth = AT_XDMAC_CC_DWIDTH_HALFWORD;
  687. dev_dbg(chan2dev(chan), "%s: dwidth: half word\n", __func__);
  688. } else {
  689. dwidth = AT_XDMAC_CC_DWIDTH_BYTE;
  690. dev_dbg(chan2dev(chan), "%s: dwidth: byte\n", __func__);
  691. }
  692. /* Prepare descriptors. */
  693. while (remaining_size) {
  694. struct at_xdmac_desc *desc = NULL;
  695. dev_dbg(chan2dev(chan), "%s: remaining_size=%zu\n", __func__, remaining_size);
  696. spin_lock_bh(&atchan->lock);
  697. desc = at_xdmac_get_desc(atchan);
  698. spin_unlock_bh(&atchan->lock);
  699. if (!desc) {
  700. dev_err(chan2dev(chan), "can't get descriptor\n");
  701. if (first)
  702. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  703. return NULL;
  704. }
  705. /* Update src and dest addresses. */
  706. src_addr += xfer_size;
  707. dst_addr += xfer_size;
  708. if (remaining_size >= AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)
  709. xfer_size = AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth;
  710. else
  711. xfer_size = remaining_size;
  712. dev_dbg(chan2dev(chan), "%s: xfer_size=%zu\n", __func__, xfer_size);
  713. /* Check remaining length and change data width if needed. */
  714. if (!((src_addr | dst_addr | xfer_size) & 7)) {
  715. dwidth = AT_XDMAC_CC_DWIDTH_DWORD;
  716. dev_dbg(chan2dev(chan), "%s: dwidth: double word\n", __func__);
  717. } else if (!((src_addr | dst_addr | xfer_size) & 3)) {
  718. dwidth = AT_XDMAC_CC_DWIDTH_WORD;
  719. dev_dbg(chan2dev(chan), "%s: dwidth: word\n", __func__);
  720. } else if (!((src_addr | dst_addr | xfer_size) & 1)) {
  721. dwidth = AT_XDMAC_CC_DWIDTH_HALFWORD;
  722. dev_dbg(chan2dev(chan), "%s: dwidth: half word\n", __func__);
  723. } else if ((src_addr | dst_addr | xfer_size) & 1) {
  724. dwidth = AT_XDMAC_CC_DWIDTH_BYTE;
  725. dev_dbg(chan2dev(chan), "%s: dwidth: byte\n", __func__);
  726. }
  727. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  728. ublen = xfer_size >> dwidth;
  729. remaining_size -= xfer_size;
  730. desc->lld.mbr_sa = src_addr;
  731. desc->lld.mbr_da = dst_addr;
  732. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2
  733. | AT_XDMAC_MBR_UBC_NDEN
  734. | AT_XDMAC_MBR_UBC_NSEN
  735. | (remaining_size ? AT_XDMAC_MBR_UBC_NDE : 0)
  736. | ublen;
  737. desc->lld.mbr_cfg = chan_cc;
  738. dev_dbg(chan2dev(chan),
  739. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  740. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc, desc->lld.mbr_cfg);
  741. /* Chain lld. */
  742. if (prev) {
  743. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  744. dev_dbg(chan2dev(chan),
  745. "%s: chain lld: prev=0x%p, mbr_nda=0x%08x\n",
  746. __func__, prev, prev->lld.mbr_nda);
  747. }
  748. prev = desc;
  749. if (!first)
  750. first = desc;
  751. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  752. __func__, desc, first);
  753. list_add_tail(&desc->desc_node, &first->descs_list);
  754. }
  755. first->tx_dma_desc.flags = flags;
  756. first->xfer_size = len;
  757. return &first->tx_dma_desc;
  758. }
  759. static enum dma_status
  760. at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  761. struct dma_tx_state *txstate)
  762. {
  763. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  764. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  765. struct at_xdmac_desc *desc, *_desc;
  766. struct list_head *descs_list;
  767. enum dma_status ret;
  768. int residue;
  769. u32 cur_nda, mask, value;
  770. u8 dwidth = 0;
  771. ret = dma_cookie_status(chan, cookie, txstate);
  772. if (ret == DMA_COMPLETE)
  773. return ret;
  774. if (!txstate)
  775. return ret;
  776. spin_lock_bh(&atchan->lock);
  777. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  778. /*
  779. * If the transfer has not been started yet, don't need to compute the
  780. * residue, it's the transfer length.
  781. */
  782. if (!desc->active_xfer) {
  783. dma_set_residue(txstate, desc->xfer_size);
  784. spin_unlock_bh(&atchan->lock);
  785. return ret;
  786. }
  787. residue = desc->xfer_size;
  788. /*
  789. * Flush FIFO: only relevant when the transfer is source peripheral
  790. * synchronized.
  791. */
  792. mask = AT_XDMAC_CC_TYPE | AT_XDMAC_CC_DSYNC;
  793. value = AT_XDMAC_CC_TYPE_PER_TRAN | AT_XDMAC_CC_DSYNC_PER2MEM;
  794. if ((desc->lld.mbr_cfg & mask) == value) {
  795. at_xdmac_write(atxdmac, AT_XDMAC_GSWF, atchan->mask);
  796. while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
  797. cpu_relax();
  798. }
  799. cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  800. /*
  801. * Remove size of all microblocks already transferred and the current
  802. * one. Then add the remaining size to transfer of the current
  803. * microblock.
  804. */
  805. descs_list = &desc->descs_list;
  806. list_for_each_entry_safe(desc, _desc, descs_list, desc_node) {
  807. dwidth = at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  808. residue -= (desc->lld.mbr_ubc & 0xffffff) << dwidth;
  809. if ((desc->lld.mbr_nda & 0xfffffffc) == cur_nda)
  810. break;
  811. }
  812. residue += at_xdmac_chan_read(atchan, AT_XDMAC_CUBC) << dwidth;
  813. spin_unlock_bh(&atchan->lock);
  814. dma_set_residue(txstate, residue);
  815. dev_dbg(chan2dev(chan),
  816. "%s: desc=0x%p, tx_dma_desc.phys=%pad, tx_status=%d, cookie=%d, residue=%d\n",
  817. __func__, desc, &desc->tx_dma_desc.phys, ret, cookie, residue);
  818. return ret;
  819. }
  820. /* Call must be protected by lock. */
  821. static void at_xdmac_remove_xfer(struct at_xdmac_chan *atchan,
  822. struct at_xdmac_desc *desc)
  823. {
  824. dev_dbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  825. /*
  826. * Remove the transfer from the transfer list then move the transfer
  827. * descriptors into the free descriptors list.
  828. */
  829. list_del(&desc->xfer_node);
  830. list_splice_init(&desc->descs_list, &atchan->free_descs_list);
  831. }
  832. static void at_xdmac_advance_work(struct at_xdmac_chan *atchan)
  833. {
  834. struct at_xdmac_desc *desc;
  835. spin_lock_bh(&atchan->lock);
  836. /*
  837. * If channel is enabled, do nothing, advance_work will be triggered
  838. * after the interruption.
  839. */
  840. if (!at_xdmac_chan_is_enabled(atchan) && !list_empty(&atchan->xfers_list)) {
  841. desc = list_first_entry(&atchan->xfers_list,
  842. struct at_xdmac_desc,
  843. xfer_node);
  844. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  845. if (!desc->active_xfer)
  846. at_xdmac_start_xfer(atchan, desc);
  847. }
  848. spin_unlock_bh(&atchan->lock);
  849. }
  850. static void at_xdmac_handle_cyclic(struct at_xdmac_chan *atchan)
  851. {
  852. struct at_xdmac_desc *desc;
  853. struct dma_async_tx_descriptor *txd;
  854. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  855. txd = &desc->tx_dma_desc;
  856. if (txd->callback && (txd->flags & DMA_PREP_INTERRUPT))
  857. txd->callback(txd->callback_param);
  858. }
  859. static void at_xdmac_tasklet(unsigned long data)
  860. {
  861. struct at_xdmac_chan *atchan = (struct at_xdmac_chan *)data;
  862. struct at_xdmac_desc *desc;
  863. u32 error_mask;
  864. dev_dbg(chan2dev(&atchan->chan), "%s: status=0x%08lx\n",
  865. __func__, atchan->status);
  866. error_mask = AT_XDMAC_CIS_RBEIS
  867. | AT_XDMAC_CIS_WBEIS
  868. | AT_XDMAC_CIS_ROIS;
  869. if (at_xdmac_chan_is_cyclic(atchan)) {
  870. at_xdmac_handle_cyclic(atchan);
  871. } else if ((atchan->status & AT_XDMAC_CIS_LIS)
  872. || (atchan->status & error_mask)) {
  873. struct dma_async_tx_descriptor *txd;
  874. if (atchan->status & AT_XDMAC_CIS_RBEIS)
  875. dev_err(chan2dev(&atchan->chan), "read bus error!!!");
  876. if (atchan->status & AT_XDMAC_CIS_WBEIS)
  877. dev_err(chan2dev(&atchan->chan), "write bus error!!!");
  878. if (atchan->status & AT_XDMAC_CIS_ROIS)
  879. dev_err(chan2dev(&atchan->chan), "request overflow error!!!");
  880. spin_lock_bh(&atchan->lock);
  881. desc = list_first_entry(&atchan->xfers_list,
  882. struct at_xdmac_desc,
  883. xfer_node);
  884. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  885. BUG_ON(!desc->active_xfer);
  886. txd = &desc->tx_dma_desc;
  887. at_xdmac_remove_xfer(atchan, desc);
  888. spin_unlock_bh(&atchan->lock);
  889. if (!at_xdmac_chan_is_cyclic(atchan)) {
  890. dma_cookie_complete(txd);
  891. if (txd->callback && (txd->flags & DMA_PREP_INTERRUPT))
  892. txd->callback(txd->callback_param);
  893. }
  894. dma_run_dependencies(txd);
  895. at_xdmac_advance_work(atchan);
  896. }
  897. }
  898. static irqreturn_t at_xdmac_interrupt(int irq, void *dev_id)
  899. {
  900. struct at_xdmac *atxdmac = (struct at_xdmac *)dev_id;
  901. struct at_xdmac_chan *atchan;
  902. u32 imr, status, pending;
  903. u32 chan_imr, chan_status;
  904. int i, ret = IRQ_NONE;
  905. do {
  906. imr = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  907. status = at_xdmac_read(atxdmac, AT_XDMAC_GIS);
  908. pending = status & imr;
  909. dev_vdbg(atxdmac->dma.dev,
  910. "%s: status=0x%08x, imr=0x%08x, pending=0x%08x\n",
  911. __func__, status, imr, pending);
  912. if (!pending)
  913. break;
  914. /* We have to find which channel has generated the interrupt. */
  915. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  916. if (!((1 << i) & pending))
  917. continue;
  918. atchan = &atxdmac->chan[i];
  919. chan_imr = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  920. chan_status = at_xdmac_chan_read(atchan, AT_XDMAC_CIS);
  921. atchan->status = chan_status & chan_imr;
  922. dev_vdbg(atxdmac->dma.dev,
  923. "%s: chan%d: imr=0x%x, status=0x%x\n",
  924. __func__, i, chan_imr, chan_status);
  925. dev_vdbg(chan2dev(&atchan->chan),
  926. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  927. __func__,
  928. at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  929. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  930. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  931. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  932. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  933. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  934. if (atchan->status & (AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS))
  935. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  936. tasklet_schedule(&atchan->tasklet);
  937. ret = IRQ_HANDLED;
  938. }
  939. } while (pending);
  940. return ret;
  941. }
  942. static void at_xdmac_issue_pending(struct dma_chan *chan)
  943. {
  944. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  945. dev_dbg(chan2dev(&atchan->chan), "%s\n", __func__);
  946. if (!at_xdmac_chan_is_cyclic(atchan))
  947. at_xdmac_advance_work(atchan);
  948. return;
  949. }
  950. static int at_xdmac_device_config(struct dma_chan *chan,
  951. struct dma_slave_config *config)
  952. {
  953. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  954. int ret;
  955. dev_dbg(chan2dev(chan), "%s\n", __func__);
  956. spin_lock_bh(&atchan->lock);
  957. ret = at_xdmac_set_slave_config(chan, config);
  958. spin_unlock_bh(&atchan->lock);
  959. return ret;
  960. }
  961. static int at_xdmac_device_pause(struct dma_chan *chan)
  962. {
  963. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  964. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  965. dev_dbg(chan2dev(chan), "%s\n", __func__);
  966. if (test_and_set_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status))
  967. return 0;
  968. spin_lock_bh(&atchan->lock);
  969. at_xdmac_write(atxdmac, AT_XDMAC_GRWS, atchan->mask);
  970. while (at_xdmac_chan_read(atchan, AT_XDMAC_CC)
  971. & (AT_XDMAC_CC_WRIP | AT_XDMAC_CC_RDIP))
  972. cpu_relax();
  973. spin_unlock_bh(&atchan->lock);
  974. return 0;
  975. }
  976. static int at_xdmac_device_resume(struct dma_chan *chan)
  977. {
  978. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  979. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  980. dev_dbg(chan2dev(chan), "%s\n", __func__);
  981. spin_lock_bh(&atchan->lock);
  982. if (!at_xdmac_chan_is_paused(atchan))
  983. return 0;
  984. at_xdmac_write(atxdmac, AT_XDMAC_GRWR, atchan->mask);
  985. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  986. spin_unlock_bh(&atchan->lock);
  987. return 0;
  988. }
  989. static int at_xdmac_device_terminate_all(struct dma_chan *chan)
  990. {
  991. struct at_xdmac_desc *desc, *_desc;
  992. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  993. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  994. dev_dbg(chan2dev(chan), "%s\n", __func__);
  995. spin_lock_bh(&atchan->lock);
  996. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  997. while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
  998. cpu_relax();
  999. /* Cancel all pending transfers. */
  1000. list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node)
  1001. at_xdmac_remove_xfer(atchan, desc);
  1002. clear_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  1003. spin_unlock_bh(&atchan->lock);
  1004. return 0;
  1005. }
  1006. static int at_xdmac_alloc_chan_resources(struct dma_chan *chan)
  1007. {
  1008. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1009. struct at_xdmac_desc *desc;
  1010. int i;
  1011. spin_lock_bh(&atchan->lock);
  1012. if (at_xdmac_chan_is_enabled(atchan)) {
  1013. dev_err(chan2dev(chan),
  1014. "can't allocate channel resources (channel enabled)\n");
  1015. i = -EIO;
  1016. goto spin_unlock;
  1017. }
  1018. if (!list_empty(&atchan->free_descs_list)) {
  1019. dev_err(chan2dev(chan),
  1020. "can't allocate channel resources (channel not free from a previous use)\n");
  1021. i = -EIO;
  1022. goto spin_unlock;
  1023. }
  1024. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1025. desc = at_xdmac_alloc_desc(chan, GFP_ATOMIC);
  1026. if (!desc) {
  1027. dev_warn(chan2dev(chan),
  1028. "only %d descriptors have been allocated\n", i);
  1029. break;
  1030. }
  1031. list_add_tail(&desc->desc_node, &atchan->free_descs_list);
  1032. }
  1033. dma_cookie_init(chan);
  1034. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  1035. spin_unlock:
  1036. spin_unlock_bh(&atchan->lock);
  1037. return i;
  1038. }
  1039. static void at_xdmac_free_chan_resources(struct dma_chan *chan)
  1040. {
  1041. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1042. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  1043. struct at_xdmac_desc *desc, *_desc;
  1044. list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) {
  1045. dev_dbg(chan2dev(chan), "%s: freeing descriptor %p\n", __func__, desc);
  1046. list_del(&desc->desc_node);
  1047. dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys);
  1048. }
  1049. return;
  1050. }
  1051. #ifdef CONFIG_PM
  1052. static int atmel_xdmac_prepare(struct device *dev)
  1053. {
  1054. struct platform_device *pdev = to_platform_device(dev);
  1055. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1056. struct dma_chan *chan, *_chan;
  1057. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1058. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1059. /* Wait for transfer completion, except in cyclic case. */
  1060. if (at_xdmac_chan_is_enabled(atchan) && !at_xdmac_chan_is_cyclic(atchan))
  1061. return -EAGAIN;
  1062. }
  1063. return 0;
  1064. }
  1065. #else
  1066. # define atmel_xdmac_prepare NULL
  1067. #endif
  1068. #ifdef CONFIG_PM_SLEEP
  1069. static int atmel_xdmac_suspend(struct device *dev)
  1070. {
  1071. struct platform_device *pdev = to_platform_device(dev);
  1072. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1073. struct dma_chan *chan, *_chan;
  1074. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1075. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1076. atchan->save_cc = at_xdmac_chan_read(atchan, AT_XDMAC_CC);
  1077. if (at_xdmac_chan_is_cyclic(atchan)) {
  1078. if (!at_xdmac_chan_is_paused(atchan))
  1079. at_xdmac_device_pause(chan);
  1080. atchan->save_cim = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1081. atchan->save_cnda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA);
  1082. atchan->save_cndc = at_xdmac_chan_read(atchan, AT_XDMAC_CNDC);
  1083. }
  1084. }
  1085. atxdmac->save_gim = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1086. at_xdmac_off(atxdmac);
  1087. clk_disable_unprepare(atxdmac->clk);
  1088. return 0;
  1089. }
  1090. static int atmel_xdmac_resume(struct device *dev)
  1091. {
  1092. struct platform_device *pdev = to_platform_device(dev);
  1093. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1094. struct at_xdmac_chan *atchan;
  1095. struct dma_chan *chan, *_chan;
  1096. int i;
  1097. clk_prepare_enable(atxdmac->clk);
  1098. /* Clear pending interrupts. */
  1099. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1100. atchan = &atxdmac->chan[i];
  1101. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1102. cpu_relax();
  1103. }
  1104. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atxdmac->save_gim);
  1105. at_xdmac_write(atxdmac, AT_XDMAC_GE, atxdmac->save_gs);
  1106. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1107. atchan = to_at_xdmac_chan(chan);
  1108. at_xdmac_chan_write(atchan, AT_XDMAC_CC, atchan->save_cc);
  1109. if (at_xdmac_chan_is_cyclic(atchan)) {
  1110. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, atchan->save_cnda);
  1111. at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, atchan->save_cndc);
  1112. at_xdmac_chan_write(atchan, AT_XDMAC_CIE, atchan->save_cim);
  1113. wmb();
  1114. at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
  1115. }
  1116. }
  1117. return 0;
  1118. }
  1119. #endif /* CONFIG_PM_SLEEP */
  1120. static int at_xdmac_probe(struct platform_device *pdev)
  1121. {
  1122. struct resource *res;
  1123. struct at_xdmac *atxdmac;
  1124. int irq, size, nr_channels, i, ret;
  1125. void __iomem *base;
  1126. u32 reg;
  1127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1128. if (!res)
  1129. return -EINVAL;
  1130. irq = platform_get_irq(pdev, 0);
  1131. if (irq < 0)
  1132. return irq;
  1133. base = devm_ioremap_resource(&pdev->dev, res);
  1134. if (IS_ERR(base))
  1135. return PTR_ERR(base);
  1136. /*
  1137. * Read number of xdmac channels, read helper function can't be used
  1138. * since atxdmac is not yet allocated and we need to know the number
  1139. * of channels to do the allocation.
  1140. */
  1141. reg = readl_relaxed(base + AT_XDMAC_GTYPE);
  1142. nr_channels = AT_XDMAC_NB_CH(reg);
  1143. if (nr_channels > AT_XDMAC_MAX_CHAN) {
  1144. dev_err(&pdev->dev, "invalid number of channels (%u)\n",
  1145. nr_channels);
  1146. return -EINVAL;
  1147. }
  1148. size = sizeof(*atxdmac);
  1149. size += nr_channels * sizeof(struct at_xdmac_chan);
  1150. atxdmac = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1151. if (!atxdmac) {
  1152. dev_err(&pdev->dev, "can't allocate at_xdmac structure\n");
  1153. return -ENOMEM;
  1154. }
  1155. atxdmac->regs = base;
  1156. atxdmac->irq = irq;
  1157. atxdmac->clk = devm_clk_get(&pdev->dev, "dma_clk");
  1158. if (IS_ERR(atxdmac->clk)) {
  1159. dev_err(&pdev->dev, "can't get dma_clk\n");
  1160. return PTR_ERR(atxdmac->clk);
  1161. }
  1162. /* Do not use dev res to prevent races with tasklet */
  1163. ret = request_irq(atxdmac->irq, at_xdmac_interrupt, 0, "at_xdmac", atxdmac);
  1164. if (ret) {
  1165. dev_err(&pdev->dev, "can't request irq\n");
  1166. return ret;
  1167. }
  1168. ret = clk_prepare_enable(atxdmac->clk);
  1169. if (ret) {
  1170. dev_err(&pdev->dev, "can't prepare or enable clock\n");
  1171. goto err_free_irq;
  1172. }
  1173. atxdmac->at_xdmac_desc_pool =
  1174. dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  1175. sizeof(struct at_xdmac_desc), 4, 0);
  1176. if (!atxdmac->at_xdmac_desc_pool) {
  1177. dev_err(&pdev->dev, "no memory for descriptors dma pool\n");
  1178. ret = -ENOMEM;
  1179. goto err_clk_disable;
  1180. }
  1181. dma_cap_set(DMA_CYCLIC, atxdmac->dma.cap_mask);
  1182. dma_cap_set(DMA_MEMCPY, atxdmac->dma.cap_mask);
  1183. dma_cap_set(DMA_SLAVE, atxdmac->dma.cap_mask);
  1184. /*
  1185. * Without DMA_PRIVATE the driver is not able to allocate more than
  1186. * one channel, second allocation fails in private_candidate.
  1187. */
  1188. dma_cap_set(DMA_PRIVATE, atxdmac->dma.cap_mask);
  1189. atxdmac->dma.dev = &pdev->dev;
  1190. atxdmac->dma.device_alloc_chan_resources = at_xdmac_alloc_chan_resources;
  1191. atxdmac->dma.device_free_chan_resources = at_xdmac_free_chan_resources;
  1192. atxdmac->dma.device_tx_status = at_xdmac_tx_status;
  1193. atxdmac->dma.device_issue_pending = at_xdmac_issue_pending;
  1194. atxdmac->dma.device_prep_dma_cyclic = at_xdmac_prep_dma_cyclic;
  1195. atxdmac->dma.device_prep_dma_memcpy = at_xdmac_prep_dma_memcpy;
  1196. atxdmac->dma.device_prep_slave_sg = at_xdmac_prep_slave_sg;
  1197. atxdmac->dma.device_config = at_xdmac_device_config;
  1198. atxdmac->dma.device_pause = at_xdmac_device_pause;
  1199. atxdmac->dma.device_resume = at_xdmac_device_resume;
  1200. atxdmac->dma.device_terminate_all = at_xdmac_device_terminate_all;
  1201. atxdmac->dma.src_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1202. atxdmac->dma.dst_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1203. atxdmac->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1204. atxdmac->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1205. /* Disable all chans and interrupts. */
  1206. at_xdmac_off(atxdmac);
  1207. /* Init channels. */
  1208. INIT_LIST_HEAD(&atxdmac->dma.channels);
  1209. for (i = 0; i < nr_channels; i++) {
  1210. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1211. atchan->chan.device = &atxdmac->dma;
  1212. list_add_tail(&atchan->chan.device_node,
  1213. &atxdmac->dma.channels);
  1214. atchan->ch_regs = at_xdmac_chan_reg_base(atxdmac, i);
  1215. atchan->mask = 1 << i;
  1216. spin_lock_init(&atchan->lock);
  1217. INIT_LIST_HEAD(&atchan->xfers_list);
  1218. INIT_LIST_HEAD(&atchan->free_descs_list);
  1219. tasklet_init(&atchan->tasklet, at_xdmac_tasklet,
  1220. (unsigned long)atchan);
  1221. /* Clear pending interrupts. */
  1222. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1223. cpu_relax();
  1224. }
  1225. platform_set_drvdata(pdev, atxdmac);
  1226. ret = dma_async_device_register(&atxdmac->dma);
  1227. if (ret) {
  1228. dev_err(&pdev->dev, "fail to register DMA engine device\n");
  1229. goto err_clk_disable;
  1230. }
  1231. ret = of_dma_controller_register(pdev->dev.of_node,
  1232. at_xdmac_xlate, atxdmac);
  1233. if (ret) {
  1234. dev_err(&pdev->dev, "could not register of dma controller\n");
  1235. goto err_dma_unregister;
  1236. }
  1237. dev_info(&pdev->dev, "%d channels, mapped at 0x%p\n",
  1238. nr_channels, atxdmac->regs);
  1239. return 0;
  1240. err_dma_unregister:
  1241. dma_async_device_unregister(&atxdmac->dma);
  1242. err_clk_disable:
  1243. clk_disable_unprepare(atxdmac->clk);
  1244. err_free_irq:
  1245. free_irq(atxdmac->irq, atxdmac->dma.dev);
  1246. return ret;
  1247. }
  1248. static int at_xdmac_remove(struct platform_device *pdev)
  1249. {
  1250. struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
  1251. int i;
  1252. at_xdmac_off(atxdmac);
  1253. of_dma_controller_free(pdev->dev.of_node);
  1254. dma_async_device_unregister(&atxdmac->dma);
  1255. clk_disable_unprepare(atxdmac->clk);
  1256. synchronize_irq(atxdmac->irq);
  1257. free_irq(atxdmac->irq, atxdmac->dma.dev);
  1258. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1259. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1260. tasklet_kill(&atchan->tasklet);
  1261. at_xdmac_free_chan_resources(&atchan->chan);
  1262. }
  1263. return 0;
  1264. }
  1265. static const struct dev_pm_ops atmel_xdmac_dev_pm_ops = {
  1266. .prepare = atmel_xdmac_prepare,
  1267. SET_LATE_SYSTEM_SLEEP_PM_OPS(atmel_xdmac_suspend, atmel_xdmac_resume)
  1268. };
  1269. static const struct of_device_id atmel_xdmac_dt_ids[] = {
  1270. {
  1271. .compatible = "atmel,sama5d4-dma",
  1272. }, {
  1273. /* sentinel */
  1274. }
  1275. };
  1276. MODULE_DEVICE_TABLE(of, atmel_xdmac_dt_ids);
  1277. static struct platform_driver at_xdmac_driver = {
  1278. .probe = at_xdmac_probe,
  1279. .remove = at_xdmac_remove,
  1280. .driver = {
  1281. .name = "at_xdmac",
  1282. .of_match_table = of_match_ptr(atmel_xdmac_dt_ids),
  1283. .pm = &atmel_xdmac_dev_pm_ops,
  1284. }
  1285. };
  1286. static int __init at_xdmac_init(void)
  1287. {
  1288. return platform_driver_probe(&at_xdmac_driver, at_xdmac_probe);
  1289. }
  1290. subsys_initcall(at_xdmac_init);
  1291. MODULE_DESCRIPTION("Atmel Extended DMA Controller driver");
  1292. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  1293. MODULE_LICENSE("GPL");