at_xdmac.c 51 KB

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