at_xdmac.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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. dev_dbg(chan2dev(chan), "%s: buf_addr=%pad, buf_len=%zd, period_len=%zd, dir=%s, flags=0x%lx\n",
  581. __func__, &buf_addr, buf_len, period_len,
  582. direction == DMA_MEM_TO_DEV ? "mem2per" : "per2mem", flags);
  583. if (!is_slave_direction(direction)) {
  584. dev_err(chan2dev(chan), "invalid DMA direction\n");
  585. return NULL;
  586. }
  587. if (test_and_set_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status)) {
  588. dev_err(chan2dev(chan), "channel currently used\n");
  589. return NULL;
  590. }
  591. for (i = 0; i < periods; i++) {
  592. struct at_xdmac_desc *desc = NULL;
  593. spin_lock_bh(&atchan->lock);
  594. desc = at_xdmac_get_desc(atchan);
  595. if (!desc) {
  596. dev_err(chan2dev(chan), "can't get descriptor\n");
  597. if (first)
  598. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  599. spin_unlock_bh(&atchan->lock);
  600. return NULL;
  601. }
  602. spin_unlock_bh(&atchan->lock);
  603. dev_dbg(chan2dev(chan),
  604. "%s: desc=0x%p, tx_dma_desc.phys=%pad\n",
  605. __func__, desc, &desc->tx_dma_desc.phys);
  606. if (direction == DMA_DEV_TO_MEM) {
  607. desc->lld.mbr_sa = atchan->per_src_addr;
  608. desc->lld.mbr_da = buf_addr + i * period_len;
  609. desc->lld.mbr_cfg = atchan->cfg[AT_XDMAC_DEV_TO_MEM_CFG];
  610. } else {
  611. desc->lld.mbr_sa = buf_addr + i * period_len;
  612. desc->lld.mbr_da = atchan->per_dst_addr;
  613. desc->lld.mbr_cfg = atchan->cfg[AT_XDMAC_MEM_TO_DEV_CFG];
  614. }
  615. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV1
  616. | AT_XDMAC_MBR_UBC_NDEN
  617. | AT_XDMAC_MBR_UBC_NSEN
  618. | AT_XDMAC_MBR_UBC_NDE
  619. | period_len >> at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  620. dev_dbg(chan2dev(chan),
  621. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  622. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  623. /* Chain lld. */
  624. if (prev) {
  625. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  626. dev_dbg(chan2dev(chan),
  627. "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  628. __func__, prev, &prev->lld.mbr_nda);
  629. }
  630. prev = desc;
  631. if (!first)
  632. first = desc;
  633. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  634. __func__, desc, first);
  635. list_add_tail(&desc->desc_node, &first->descs_list);
  636. }
  637. prev->lld.mbr_nda = first->tx_dma_desc.phys;
  638. dev_dbg(chan2dev(chan),
  639. "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  640. __func__, prev, &prev->lld.mbr_nda);
  641. first->tx_dma_desc.flags = flags;
  642. first->xfer_size = buf_len;
  643. first->direction = direction;
  644. return &first->tx_dma_desc;
  645. }
  646. static struct dma_async_tx_descriptor *
  647. at_xdmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  648. size_t len, unsigned long flags)
  649. {
  650. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  651. struct at_xdmac_desc *first = NULL, *prev = NULL;
  652. size_t remaining_size = len, xfer_size = 0, ublen;
  653. dma_addr_t src_addr = src, dst_addr = dest;
  654. u32 dwidth;
  655. /*
  656. * WARNING: We don't know the direction, it involves we can't
  657. * dynamically set the source and dest interface so we have to use the
  658. * same one. Only interface 0 allows EBI access. Hopefully we can
  659. * access DDR through both ports (at least on SAMA5D4x), so we can use
  660. * the same interface for source and dest, that solves the fact we
  661. * don't know the direction.
  662. */
  663. u32 chan_cc = AT_XDMAC_CC_DAM_INCREMENTED_AM
  664. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  665. | AT_XDMAC_CC_DIF(0)
  666. | AT_XDMAC_CC_SIF(0)
  667. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  668. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  669. dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, len=%zd, flags=0x%lx\n",
  670. __func__, &src, &dest, len, flags);
  671. if (unlikely(!len))
  672. return NULL;
  673. /*
  674. * Check address alignment to select the greater data width we can use.
  675. * Some XDMAC implementations don't provide dword transfer, in this
  676. * case selecting dword has the same behavior as selecting word transfers.
  677. */
  678. if (!((src_addr | dst_addr) & 7)) {
  679. dwidth = AT_XDMAC_CC_DWIDTH_DWORD;
  680. dev_dbg(chan2dev(chan), "%s: dwidth: double word\n", __func__);
  681. } else if (!((src_addr | dst_addr) & 3)) {
  682. dwidth = AT_XDMAC_CC_DWIDTH_WORD;
  683. dev_dbg(chan2dev(chan), "%s: dwidth: word\n", __func__);
  684. } else if (!((src_addr | dst_addr) & 1)) {
  685. dwidth = AT_XDMAC_CC_DWIDTH_HALFWORD;
  686. dev_dbg(chan2dev(chan), "%s: dwidth: half word\n", __func__);
  687. } else {
  688. dwidth = AT_XDMAC_CC_DWIDTH_BYTE;
  689. dev_dbg(chan2dev(chan), "%s: dwidth: byte\n", __func__);
  690. }
  691. /* Prepare descriptors. */
  692. while (remaining_size) {
  693. struct at_xdmac_desc *desc = NULL;
  694. dev_dbg(chan2dev(chan), "%s: remaining_size=%zu\n", __func__, remaining_size);
  695. spin_lock_bh(&atchan->lock);
  696. desc = at_xdmac_get_desc(atchan);
  697. spin_unlock_bh(&atchan->lock);
  698. if (!desc) {
  699. dev_err(chan2dev(chan), "can't get descriptor\n");
  700. if (first)
  701. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  702. return NULL;
  703. }
  704. /* Update src and dest addresses. */
  705. src_addr += xfer_size;
  706. dst_addr += xfer_size;
  707. if (remaining_size >= AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)
  708. xfer_size = AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth;
  709. else
  710. xfer_size = remaining_size;
  711. dev_dbg(chan2dev(chan), "%s: xfer_size=%zu\n", __func__, xfer_size);
  712. /* Check remaining length and change data width if needed. */
  713. if (!((src_addr | dst_addr | xfer_size) & 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 | xfer_size) & 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 | xfer_size) & 1)) {
  720. dwidth = AT_XDMAC_CC_DWIDTH_HALFWORD;
  721. dev_dbg(chan2dev(chan), "%s: dwidth: half word\n", __func__);
  722. } else if ((src_addr | dst_addr | xfer_size) & 1) {
  723. dwidth = AT_XDMAC_CC_DWIDTH_BYTE;
  724. dev_dbg(chan2dev(chan), "%s: dwidth: byte\n", __func__);
  725. }
  726. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  727. ublen = xfer_size >> dwidth;
  728. remaining_size -= xfer_size;
  729. desc->lld.mbr_sa = src_addr;
  730. desc->lld.mbr_da = dst_addr;
  731. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2
  732. | AT_XDMAC_MBR_UBC_NDEN
  733. | AT_XDMAC_MBR_UBC_NSEN
  734. | (remaining_size ? AT_XDMAC_MBR_UBC_NDE : 0)
  735. | ublen;
  736. desc->lld.mbr_cfg = chan_cc;
  737. dev_dbg(chan2dev(chan),
  738. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  739. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc, desc->lld.mbr_cfg);
  740. /* Chain lld. */
  741. if (prev) {
  742. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  743. dev_dbg(chan2dev(chan),
  744. "%s: chain lld: prev=0x%p, mbr_nda=0x%08x\n",
  745. __func__, prev, prev->lld.mbr_nda);
  746. }
  747. prev = desc;
  748. if (!first)
  749. first = desc;
  750. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  751. __func__, desc, first);
  752. list_add_tail(&desc->desc_node, &first->descs_list);
  753. }
  754. first->tx_dma_desc.flags = flags;
  755. first->xfer_size = len;
  756. return &first->tx_dma_desc;
  757. }
  758. static enum dma_status
  759. at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  760. struct dma_tx_state *txstate)
  761. {
  762. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  763. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  764. struct at_xdmac_desc *desc, *_desc;
  765. struct list_head *descs_list;
  766. enum dma_status ret;
  767. int residue;
  768. u32 cur_nda, mask, value;
  769. u8 dwidth = 0;
  770. ret = dma_cookie_status(chan, cookie, txstate);
  771. if (ret == DMA_COMPLETE)
  772. return ret;
  773. if (!txstate)
  774. return ret;
  775. spin_lock_bh(&atchan->lock);
  776. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  777. /*
  778. * If the transfer has not been started yet, don't need to compute the
  779. * residue, it's the transfer length.
  780. */
  781. if (!desc->active_xfer) {
  782. dma_set_residue(txstate, desc->xfer_size);
  783. spin_unlock_bh(&atchan->lock);
  784. return ret;
  785. }
  786. residue = desc->xfer_size;
  787. /*
  788. * Flush FIFO: only relevant when the transfer is source peripheral
  789. * synchronized.
  790. */
  791. mask = AT_XDMAC_CC_TYPE | AT_XDMAC_CC_DSYNC;
  792. value = AT_XDMAC_CC_TYPE_PER_TRAN | AT_XDMAC_CC_DSYNC_PER2MEM;
  793. if ((desc->lld.mbr_cfg & mask) == value) {
  794. at_xdmac_write(atxdmac, AT_XDMAC_GSWF, atchan->mask);
  795. while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
  796. cpu_relax();
  797. }
  798. cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  799. /*
  800. * Remove size of all microblocks already transferred and the current
  801. * one. Then add the remaining size to transfer of the current
  802. * microblock.
  803. */
  804. descs_list = &desc->descs_list;
  805. list_for_each_entry_safe(desc, _desc, descs_list, desc_node) {
  806. dwidth = at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  807. residue -= (desc->lld.mbr_ubc & 0xffffff) << dwidth;
  808. if ((desc->lld.mbr_nda & 0xfffffffc) == cur_nda)
  809. break;
  810. }
  811. residue += at_xdmac_chan_read(atchan, AT_XDMAC_CUBC) << dwidth;
  812. spin_unlock_bh(&atchan->lock);
  813. dma_set_residue(txstate, residue);
  814. dev_dbg(chan2dev(chan),
  815. "%s: desc=0x%p, tx_dma_desc.phys=%pad, tx_status=%d, cookie=%d, residue=%d\n",
  816. __func__, desc, &desc->tx_dma_desc.phys, ret, cookie, residue);
  817. return ret;
  818. }
  819. /* Call must be protected by lock. */
  820. static void at_xdmac_remove_xfer(struct at_xdmac_chan *atchan,
  821. struct at_xdmac_desc *desc)
  822. {
  823. dev_dbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  824. /*
  825. * Remove the transfer from the transfer list then move the transfer
  826. * descriptors into the free descriptors list.
  827. */
  828. list_del(&desc->xfer_node);
  829. list_splice_init(&desc->descs_list, &atchan->free_descs_list);
  830. }
  831. static void at_xdmac_advance_work(struct at_xdmac_chan *atchan)
  832. {
  833. struct at_xdmac_desc *desc;
  834. spin_lock_bh(&atchan->lock);
  835. /*
  836. * If channel is enabled, do nothing, advance_work will be triggered
  837. * after the interruption.
  838. */
  839. if (!at_xdmac_chan_is_enabled(atchan) && !list_empty(&atchan->xfers_list)) {
  840. desc = list_first_entry(&atchan->xfers_list,
  841. struct at_xdmac_desc,
  842. xfer_node);
  843. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  844. if (!desc->active_xfer)
  845. at_xdmac_start_xfer(atchan, desc);
  846. }
  847. spin_unlock_bh(&atchan->lock);
  848. }
  849. static void at_xdmac_handle_cyclic(struct at_xdmac_chan *atchan)
  850. {
  851. struct at_xdmac_desc *desc;
  852. struct dma_async_tx_descriptor *txd;
  853. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  854. txd = &desc->tx_dma_desc;
  855. if (txd->callback && (txd->flags & DMA_PREP_INTERRUPT))
  856. txd->callback(txd->callback_param);
  857. }
  858. static void at_xdmac_tasklet(unsigned long data)
  859. {
  860. struct at_xdmac_chan *atchan = (struct at_xdmac_chan *)data;
  861. struct at_xdmac_desc *desc;
  862. u32 error_mask;
  863. dev_dbg(chan2dev(&atchan->chan), "%s: status=0x%08lx\n",
  864. __func__, atchan->status);
  865. error_mask = AT_XDMAC_CIS_RBEIS
  866. | AT_XDMAC_CIS_WBEIS
  867. | AT_XDMAC_CIS_ROIS;
  868. if (at_xdmac_chan_is_cyclic(atchan)) {
  869. at_xdmac_handle_cyclic(atchan);
  870. } else if ((atchan->status & AT_XDMAC_CIS_LIS)
  871. || (atchan->status & error_mask)) {
  872. struct dma_async_tx_descriptor *txd;
  873. if (atchan->status & AT_XDMAC_CIS_RBEIS)
  874. dev_err(chan2dev(&atchan->chan), "read bus error!!!");
  875. if (atchan->status & AT_XDMAC_CIS_WBEIS)
  876. dev_err(chan2dev(&atchan->chan), "write bus error!!!");
  877. if (atchan->status & AT_XDMAC_CIS_ROIS)
  878. dev_err(chan2dev(&atchan->chan), "request overflow error!!!");
  879. spin_lock_bh(&atchan->lock);
  880. desc = list_first_entry(&atchan->xfers_list,
  881. struct at_xdmac_desc,
  882. xfer_node);
  883. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  884. BUG_ON(!desc->active_xfer);
  885. txd = &desc->tx_dma_desc;
  886. at_xdmac_remove_xfer(atchan, desc);
  887. spin_unlock_bh(&atchan->lock);
  888. if (!at_xdmac_chan_is_cyclic(atchan)) {
  889. dma_cookie_complete(txd);
  890. if (txd->callback && (txd->flags & DMA_PREP_INTERRUPT))
  891. txd->callback(txd->callback_param);
  892. }
  893. dma_run_dependencies(txd);
  894. at_xdmac_advance_work(atchan);
  895. }
  896. }
  897. static irqreturn_t at_xdmac_interrupt(int irq, void *dev_id)
  898. {
  899. struct at_xdmac *atxdmac = (struct at_xdmac *)dev_id;
  900. struct at_xdmac_chan *atchan;
  901. u32 imr, status, pending;
  902. u32 chan_imr, chan_status;
  903. int i, ret = IRQ_NONE;
  904. do {
  905. imr = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  906. status = at_xdmac_read(atxdmac, AT_XDMAC_GIS);
  907. pending = status & imr;
  908. dev_vdbg(atxdmac->dma.dev,
  909. "%s: status=0x%08x, imr=0x%08x, pending=0x%08x\n",
  910. __func__, status, imr, pending);
  911. if (!pending)
  912. break;
  913. /* We have to find which channel has generated the interrupt. */
  914. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  915. if (!((1 << i) & pending))
  916. continue;
  917. atchan = &atxdmac->chan[i];
  918. chan_imr = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  919. chan_status = at_xdmac_chan_read(atchan, AT_XDMAC_CIS);
  920. atchan->status = chan_status & chan_imr;
  921. dev_vdbg(atxdmac->dma.dev,
  922. "%s: chan%d: imr=0x%x, status=0x%x\n",
  923. __func__, i, chan_imr, chan_status);
  924. dev_vdbg(chan2dev(&atchan->chan),
  925. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  926. __func__,
  927. at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  928. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  929. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  930. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  931. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  932. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  933. if (atchan->status & (AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS))
  934. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  935. tasklet_schedule(&atchan->tasklet);
  936. ret = IRQ_HANDLED;
  937. }
  938. } while (pending);
  939. return ret;
  940. }
  941. static void at_xdmac_issue_pending(struct dma_chan *chan)
  942. {
  943. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  944. dev_dbg(chan2dev(&atchan->chan), "%s\n", __func__);
  945. if (!at_xdmac_chan_is_cyclic(atchan))
  946. at_xdmac_advance_work(atchan);
  947. return;
  948. }
  949. static int at_xdmac_device_config(struct dma_chan *chan,
  950. struct dma_slave_config *config)
  951. {
  952. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  953. int ret;
  954. dev_dbg(chan2dev(chan), "%s\n", __func__);
  955. spin_lock_bh(&atchan->lock);
  956. ret = at_xdmac_set_slave_config(chan, config);
  957. spin_unlock_bh(&atchan->lock);
  958. return ret;
  959. }
  960. static int at_xdmac_device_pause(struct dma_chan *chan)
  961. {
  962. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  963. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  964. dev_dbg(chan2dev(chan), "%s\n", __func__);
  965. if (test_and_set_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status))
  966. return 0;
  967. spin_lock_bh(&atchan->lock);
  968. at_xdmac_write(atxdmac, AT_XDMAC_GRWS, atchan->mask);
  969. while (at_xdmac_chan_read(atchan, AT_XDMAC_CC)
  970. & (AT_XDMAC_CC_WRIP | AT_XDMAC_CC_RDIP))
  971. cpu_relax();
  972. spin_unlock_bh(&atchan->lock);
  973. return 0;
  974. }
  975. static int at_xdmac_device_resume(struct dma_chan *chan)
  976. {
  977. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  978. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  979. dev_dbg(chan2dev(chan), "%s\n", __func__);
  980. spin_lock_bh(&atchan->lock);
  981. if (!at_xdmac_chan_is_paused(atchan))
  982. return 0;
  983. at_xdmac_write(atxdmac, AT_XDMAC_GRWR, atchan->mask);
  984. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  985. spin_unlock_bh(&atchan->lock);
  986. return 0;
  987. }
  988. static int at_xdmac_device_terminate_all(struct dma_chan *chan)
  989. {
  990. struct at_xdmac_desc *desc, *_desc;
  991. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  992. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  993. dev_dbg(chan2dev(chan), "%s\n", __func__);
  994. spin_lock_bh(&atchan->lock);
  995. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  996. while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
  997. cpu_relax();
  998. /* Cancel all pending transfers. */
  999. list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node)
  1000. at_xdmac_remove_xfer(atchan, desc);
  1001. clear_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  1002. spin_unlock_bh(&atchan->lock);
  1003. return 0;
  1004. }
  1005. static int at_xdmac_alloc_chan_resources(struct dma_chan *chan)
  1006. {
  1007. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1008. struct at_xdmac_desc *desc;
  1009. int i;
  1010. spin_lock_bh(&atchan->lock);
  1011. if (at_xdmac_chan_is_enabled(atchan)) {
  1012. dev_err(chan2dev(chan),
  1013. "can't allocate channel resources (channel enabled)\n");
  1014. i = -EIO;
  1015. goto spin_unlock;
  1016. }
  1017. if (!list_empty(&atchan->free_descs_list)) {
  1018. dev_err(chan2dev(chan),
  1019. "can't allocate channel resources (channel not free from a previous use)\n");
  1020. i = -EIO;
  1021. goto spin_unlock;
  1022. }
  1023. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1024. desc = at_xdmac_alloc_desc(chan, GFP_ATOMIC);
  1025. if (!desc) {
  1026. dev_warn(chan2dev(chan),
  1027. "only %d descriptors have been allocated\n", i);
  1028. break;
  1029. }
  1030. list_add_tail(&desc->desc_node, &atchan->free_descs_list);
  1031. }
  1032. dma_cookie_init(chan);
  1033. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  1034. spin_unlock:
  1035. spin_unlock_bh(&atchan->lock);
  1036. return i;
  1037. }
  1038. static void at_xdmac_free_chan_resources(struct dma_chan *chan)
  1039. {
  1040. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1041. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  1042. struct at_xdmac_desc *desc, *_desc;
  1043. list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) {
  1044. dev_dbg(chan2dev(chan), "%s: freeing descriptor %p\n", __func__, desc);
  1045. list_del(&desc->desc_node);
  1046. dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys);
  1047. }
  1048. return;
  1049. }
  1050. #ifdef CONFIG_PM
  1051. static int atmel_xdmac_prepare(struct device *dev)
  1052. {
  1053. struct platform_device *pdev = to_platform_device(dev);
  1054. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1055. struct dma_chan *chan, *_chan;
  1056. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1057. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1058. /* Wait for transfer completion, except in cyclic case. */
  1059. if (at_xdmac_chan_is_enabled(atchan) && !at_xdmac_chan_is_cyclic(atchan))
  1060. return -EAGAIN;
  1061. }
  1062. return 0;
  1063. }
  1064. #else
  1065. # define atmel_xdmac_prepare NULL
  1066. #endif
  1067. #ifdef CONFIG_PM_SLEEP
  1068. static int atmel_xdmac_suspend(struct device *dev)
  1069. {
  1070. struct platform_device *pdev = to_platform_device(dev);
  1071. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1072. struct dma_chan *chan, *_chan;
  1073. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1074. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1075. atchan->save_cc = at_xdmac_chan_read(atchan, AT_XDMAC_CC);
  1076. if (at_xdmac_chan_is_cyclic(atchan)) {
  1077. if (!at_xdmac_chan_is_paused(atchan))
  1078. at_xdmac_device_pause(chan);
  1079. atchan->save_cim = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1080. atchan->save_cnda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA);
  1081. atchan->save_cndc = at_xdmac_chan_read(atchan, AT_XDMAC_CNDC);
  1082. }
  1083. }
  1084. atxdmac->save_gim = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1085. at_xdmac_off(atxdmac);
  1086. clk_disable_unprepare(atxdmac->clk);
  1087. return 0;
  1088. }
  1089. static int atmel_xdmac_resume(struct device *dev)
  1090. {
  1091. struct platform_device *pdev = to_platform_device(dev);
  1092. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1093. struct at_xdmac_chan *atchan;
  1094. struct dma_chan *chan, *_chan;
  1095. int i;
  1096. clk_prepare_enable(atxdmac->clk);
  1097. /* Clear pending interrupts. */
  1098. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1099. atchan = &atxdmac->chan[i];
  1100. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1101. cpu_relax();
  1102. }
  1103. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atxdmac->save_gim);
  1104. at_xdmac_write(atxdmac, AT_XDMAC_GE, atxdmac->save_gs);
  1105. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1106. atchan = to_at_xdmac_chan(chan);
  1107. at_xdmac_chan_write(atchan, AT_XDMAC_CC, atchan->save_cc);
  1108. if (at_xdmac_chan_is_cyclic(atchan)) {
  1109. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, atchan->save_cnda);
  1110. at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, atchan->save_cndc);
  1111. at_xdmac_chan_write(atchan, AT_XDMAC_CIE, atchan->save_cim);
  1112. wmb();
  1113. at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
  1114. }
  1115. }
  1116. return 0;
  1117. }
  1118. #endif /* CONFIG_PM_SLEEP */
  1119. static int at_xdmac_probe(struct platform_device *pdev)
  1120. {
  1121. struct resource *res;
  1122. struct at_xdmac *atxdmac;
  1123. int irq, size, nr_channels, i, ret;
  1124. void __iomem *base;
  1125. u32 reg;
  1126. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1127. if (!res)
  1128. return -EINVAL;
  1129. irq = platform_get_irq(pdev, 0);
  1130. if (irq < 0)
  1131. return irq;
  1132. base = devm_ioremap_resource(&pdev->dev, res);
  1133. if (IS_ERR(base))
  1134. return PTR_ERR(base);
  1135. /*
  1136. * Read number of xdmac channels, read helper function can't be used
  1137. * since atxdmac is not yet allocated and we need to know the number
  1138. * of channels to do the allocation.
  1139. */
  1140. reg = readl_relaxed(base + AT_XDMAC_GTYPE);
  1141. nr_channels = AT_XDMAC_NB_CH(reg);
  1142. if (nr_channels > AT_XDMAC_MAX_CHAN) {
  1143. dev_err(&pdev->dev, "invalid number of channels (%u)\n",
  1144. nr_channels);
  1145. return -EINVAL;
  1146. }
  1147. size = sizeof(*atxdmac);
  1148. size += nr_channels * sizeof(struct at_xdmac_chan);
  1149. atxdmac = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1150. if (!atxdmac) {
  1151. dev_err(&pdev->dev, "can't allocate at_xdmac structure\n");
  1152. return -ENOMEM;
  1153. }
  1154. atxdmac->regs = base;
  1155. atxdmac->irq = irq;
  1156. atxdmac->clk = devm_clk_get(&pdev->dev, "dma_clk");
  1157. if (IS_ERR(atxdmac->clk)) {
  1158. dev_err(&pdev->dev, "can't get dma_clk\n");
  1159. return PTR_ERR(atxdmac->clk);
  1160. }
  1161. /* Do not use dev res to prevent races with tasklet */
  1162. ret = request_irq(atxdmac->irq, at_xdmac_interrupt, 0, "at_xdmac", atxdmac);
  1163. if (ret) {
  1164. dev_err(&pdev->dev, "can't request irq\n");
  1165. return ret;
  1166. }
  1167. ret = clk_prepare_enable(atxdmac->clk);
  1168. if (ret) {
  1169. dev_err(&pdev->dev, "can't prepare or enable clock\n");
  1170. goto err_free_irq;
  1171. }
  1172. atxdmac->at_xdmac_desc_pool =
  1173. dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  1174. sizeof(struct at_xdmac_desc), 4, 0);
  1175. if (!atxdmac->at_xdmac_desc_pool) {
  1176. dev_err(&pdev->dev, "no memory for descriptors dma pool\n");
  1177. ret = -ENOMEM;
  1178. goto err_clk_disable;
  1179. }
  1180. dma_cap_set(DMA_CYCLIC, atxdmac->dma.cap_mask);
  1181. dma_cap_set(DMA_MEMCPY, atxdmac->dma.cap_mask);
  1182. dma_cap_set(DMA_SLAVE, atxdmac->dma.cap_mask);
  1183. /*
  1184. * Without DMA_PRIVATE the driver is not able to allocate more than
  1185. * one channel, second allocation fails in private_candidate.
  1186. */
  1187. dma_cap_set(DMA_PRIVATE, atxdmac->dma.cap_mask);
  1188. atxdmac->dma.dev = &pdev->dev;
  1189. atxdmac->dma.device_alloc_chan_resources = at_xdmac_alloc_chan_resources;
  1190. atxdmac->dma.device_free_chan_resources = at_xdmac_free_chan_resources;
  1191. atxdmac->dma.device_tx_status = at_xdmac_tx_status;
  1192. atxdmac->dma.device_issue_pending = at_xdmac_issue_pending;
  1193. atxdmac->dma.device_prep_dma_cyclic = at_xdmac_prep_dma_cyclic;
  1194. atxdmac->dma.device_prep_dma_memcpy = at_xdmac_prep_dma_memcpy;
  1195. atxdmac->dma.device_prep_slave_sg = at_xdmac_prep_slave_sg;
  1196. atxdmac->dma.device_config = at_xdmac_device_config;
  1197. atxdmac->dma.device_pause = at_xdmac_device_pause;
  1198. atxdmac->dma.device_resume = at_xdmac_device_resume;
  1199. atxdmac->dma.device_terminate_all = at_xdmac_device_terminate_all;
  1200. atxdmac->dma.src_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1201. atxdmac->dma.dst_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1202. atxdmac->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1203. atxdmac->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1204. /* Disable all chans and interrupts. */
  1205. at_xdmac_off(atxdmac);
  1206. /* Init channels. */
  1207. INIT_LIST_HEAD(&atxdmac->dma.channels);
  1208. for (i = 0; i < nr_channels; i++) {
  1209. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1210. atchan->chan.device = &atxdmac->dma;
  1211. list_add_tail(&atchan->chan.device_node,
  1212. &atxdmac->dma.channels);
  1213. atchan->ch_regs = at_xdmac_chan_reg_base(atxdmac, i);
  1214. atchan->mask = 1 << i;
  1215. spin_lock_init(&atchan->lock);
  1216. INIT_LIST_HEAD(&atchan->xfers_list);
  1217. INIT_LIST_HEAD(&atchan->free_descs_list);
  1218. tasklet_init(&atchan->tasklet, at_xdmac_tasklet,
  1219. (unsigned long)atchan);
  1220. /* Clear pending interrupts. */
  1221. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1222. cpu_relax();
  1223. }
  1224. platform_set_drvdata(pdev, atxdmac);
  1225. ret = dma_async_device_register(&atxdmac->dma);
  1226. if (ret) {
  1227. dev_err(&pdev->dev, "fail to register DMA engine device\n");
  1228. goto err_clk_disable;
  1229. }
  1230. ret = of_dma_controller_register(pdev->dev.of_node,
  1231. at_xdmac_xlate, atxdmac);
  1232. if (ret) {
  1233. dev_err(&pdev->dev, "could not register of dma controller\n");
  1234. goto err_dma_unregister;
  1235. }
  1236. dev_info(&pdev->dev, "%d channels, mapped at 0x%p\n",
  1237. nr_channels, atxdmac->regs);
  1238. return 0;
  1239. err_dma_unregister:
  1240. dma_async_device_unregister(&atxdmac->dma);
  1241. err_clk_disable:
  1242. clk_disable_unprepare(atxdmac->clk);
  1243. err_free_irq:
  1244. free_irq(atxdmac->irq, atxdmac->dma.dev);
  1245. return ret;
  1246. }
  1247. static int at_xdmac_remove(struct platform_device *pdev)
  1248. {
  1249. struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
  1250. int i;
  1251. at_xdmac_off(atxdmac);
  1252. of_dma_controller_free(pdev->dev.of_node);
  1253. dma_async_device_unregister(&atxdmac->dma);
  1254. clk_disable_unprepare(atxdmac->clk);
  1255. synchronize_irq(atxdmac->irq);
  1256. free_irq(atxdmac->irq, atxdmac->dma.dev);
  1257. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1258. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1259. tasklet_kill(&atchan->tasklet);
  1260. at_xdmac_free_chan_resources(&atchan->chan);
  1261. }
  1262. return 0;
  1263. }
  1264. static const struct dev_pm_ops atmel_xdmac_dev_pm_ops = {
  1265. .prepare = atmel_xdmac_prepare,
  1266. SET_LATE_SYSTEM_SLEEP_PM_OPS(atmel_xdmac_suspend, atmel_xdmac_resume)
  1267. };
  1268. static const struct of_device_id atmel_xdmac_dt_ids[] = {
  1269. {
  1270. .compatible = "atmel,sama5d4-dma",
  1271. }, {
  1272. /* sentinel */
  1273. }
  1274. };
  1275. MODULE_DEVICE_TABLE(of, atmel_xdmac_dt_ids);
  1276. static struct platform_driver at_xdmac_driver = {
  1277. .probe = at_xdmac_probe,
  1278. .remove = at_xdmac_remove,
  1279. .driver = {
  1280. .name = "at_xdmac",
  1281. .of_match_table = of_match_ptr(atmel_xdmac_dt_ids),
  1282. .pm = &atmel_xdmac_dev_pm_ops,
  1283. }
  1284. };
  1285. static int __init at_xdmac_init(void)
  1286. {
  1287. return platform_driver_probe(&at_xdmac_driver, at_xdmac_probe);
  1288. }
  1289. subsys_initcall(at_xdmac_init);
  1290. MODULE_DESCRIPTION("Atmel Extended DMA Controller driver");
  1291. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  1292. MODULE_LICENSE("GPL");