at_xdmac.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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/list.h>
  28. #include <linux/module.h>
  29. #include <linux/of_dma.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/pm.h>
  33. #include "dmaengine.h"
  34. /* Global registers */
  35. #define AT_XDMAC_GTYPE 0x00 /* Global Type Register */
  36. #define AT_XDMAC_NB_CH(i) (((i) & 0x1F) + 1) /* Number of Channels Minus One */
  37. #define AT_XDMAC_FIFO_SZ(i) (((i) >> 5) & 0x7FF) /* Number of Bytes */
  38. #define AT_XDMAC_NB_REQ(i) ((((i) >> 16) & 0x3F) + 1) /* Number of Peripheral Requests Minus One */
  39. #define AT_XDMAC_GCFG 0x04 /* Global Configuration Register */
  40. #define AT_XDMAC_GWAC 0x08 /* Global Weighted Arbiter Configuration Register */
  41. #define AT_XDMAC_GIE 0x0C /* Global Interrupt Enable Register */
  42. #define AT_XDMAC_GID 0x10 /* Global Interrupt Disable Register */
  43. #define AT_XDMAC_GIM 0x14 /* Global Interrupt Mask Register */
  44. #define AT_XDMAC_GIS 0x18 /* Global Interrupt Status Register */
  45. #define AT_XDMAC_GE 0x1C /* Global Channel Enable Register */
  46. #define AT_XDMAC_GD 0x20 /* Global Channel Disable Register */
  47. #define AT_XDMAC_GS 0x24 /* Global Channel Status Register */
  48. #define AT_XDMAC_GRS 0x28 /* Global Channel Read Suspend Register */
  49. #define AT_XDMAC_GWS 0x2C /* Global Write Suspend Register */
  50. #define AT_XDMAC_GRWS 0x30 /* Global Channel Read Write Suspend Register */
  51. #define AT_XDMAC_GRWR 0x34 /* Global Channel Read Write Resume Register */
  52. #define AT_XDMAC_GSWR 0x38 /* Global Channel Software Request Register */
  53. #define AT_XDMAC_GSWS 0x3C /* Global channel Software Request Status Register */
  54. #define AT_XDMAC_GSWF 0x40 /* Global Channel Software Flush Request Register */
  55. #define AT_XDMAC_VERSION 0xFFC /* XDMAC Version Register */
  56. /* Channel relative registers offsets */
  57. #define AT_XDMAC_CIE 0x00 /* Channel Interrupt Enable Register */
  58. #define AT_XDMAC_CIE_BIE BIT(0) /* End of Block Interrupt Enable Bit */
  59. #define AT_XDMAC_CIE_LIE BIT(1) /* End of Linked List Interrupt Enable Bit */
  60. #define AT_XDMAC_CIE_DIE BIT(2) /* End of Disable Interrupt Enable Bit */
  61. #define AT_XDMAC_CIE_FIE BIT(3) /* End of Flush Interrupt Enable Bit */
  62. #define AT_XDMAC_CIE_RBEIE BIT(4) /* Read Bus Error Interrupt Enable Bit */
  63. #define AT_XDMAC_CIE_WBEIE BIT(5) /* Write Bus Error Interrupt Enable Bit */
  64. #define AT_XDMAC_CIE_ROIE BIT(6) /* Request Overflow Interrupt Enable Bit */
  65. #define AT_XDMAC_CID 0x04 /* Channel Interrupt Disable Register */
  66. #define AT_XDMAC_CID_BID BIT(0) /* End of Block Interrupt Disable Bit */
  67. #define AT_XDMAC_CID_LID BIT(1) /* End of Linked List Interrupt Disable Bit */
  68. #define AT_XDMAC_CID_DID BIT(2) /* End of Disable Interrupt Disable Bit */
  69. #define AT_XDMAC_CID_FID BIT(3) /* End of Flush Interrupt Disable Bit */
  70. #define AT_XDMAC_CID_RBEID BIT(4) /* Read Bus Error Interrupt Disable Bit */
  71. #define AT_XDMAC_CID_WBEID BIT(5) /* Write Bus Error Interrupt Disable Bit */
  72. #define AT_XDMAC_CID_ROID BIT(6) /* Request Overflow Interrupt Disable Bit */
  73. #define AT_XDMAC_CIM 0x08 /* Channel Interrupt Mask Register */
  74. #define AT_XDMAC_CIM_BIM BIT(0) /* End of Block Interrupt Mask Bit */
  75. #define AT_XDMAC_CIM_LIM BIT(1) /* End of Linked List Interrupt Mask Bit */
  76. #define AT_XDMAC_CIM_DIM BIT(2) /* End of Disable Interrupt Mask Bit */
  77. #define AT_XDMAC_CIM_FIM BIT(3) /* End of Flush Interrupt Mask Bit */
  78. #define AT_XDMAC_CIM_RBEIM BIT(4) /* Read Bus Error Interrupt Mask Bit */
  79. #define AT_XDMAC_CIM_WBEIM BIT(5) /* Write Bus Error Interrupt Mask Bit */
  80. #define AT_XDMAC_CIM_ROIM BIT(6) /* Request Overflow Interrupt Mask Bit */
  81. #define AT_XDMAC_CIS 0x0C /* Channel Interrupt Status Register */
  82. #define AT_XDMAC_CIS_BIS BIT(0) /* End of Block Interrupt Status Bit */
  83. #define AT_XDMAC_CIS_LIS BIT(1) /* End of Linked List Interrupt Status Bit */
  84. #define AT_XDMAC_CIS_DIS BIT(2) /* End of Disable Interrupt Status Bit */
  85. #define AT_XDMAC_CIS_FIS BIT(3) /* End of Flush Interrupt Status Bit */
  86. #define AT_XDMAC_CIS_RBEIS BIT(4) /* Read Bus Error Interrupt Status Bit */
  87. #define AT_XDMAC_CIS_WBEIS BIT(5) /* Write Bus Error Interrupt Status Bit */
  88. #define AT_XDMAC_CIS_ROIS BIT(6) /* Request Overflow Interrupt Status Bit */
  89. #define AT_XDMAC_CSA 0x10 /* Channel Source Address Register */
  90. #define AT_XDMAC_CDA 0x14 /* Channel Destination Address Register */
  91. #define AT_XDMAC_CNDA 0x18 /* Channel Next Descriptor Address Register */
  92. #define AT_XDMAC_CNDA_NDAIF(i) ((i) & 0x1) /* Channel x Next Descriptor Interface */
  93. #define AT_XDMAC_CNDA_NDA(i) ((i) & 0xfffffffc) /* Channel x Next Descriptor Address */
  94. #define AT_XDMAC_CNDC 0x1C /* Channel Next Descriptor Control Register */
  95. #define AT_XDMAC_CNDC_NDE (0x1 << 0) /* Channel x Next Descriptor Enable */
  96. #define AT_XDMAC_CNDC_NDSUP (0x1 << 1) /* Channel x Next Descriptor Source Update */
  97. #define AT_XDMAC_CNDC_NDDUP (0x1 << 2) /* Channel x Next Descriptor Destination Update */
  98. #define AT_XDMAC_CNDC_NDVIEW_NDV0 (0x0 << 3) /* Channel x Next Descriptor View 0 */
  99. #define AT_XDMAC_CNDC_NDVIEW_NDV1 (0x1 << 3) /* Channel x Next Descriptor View 1 */
  100. #define AT_XDMAC_CNDC_NDVIEW_NDV2 (0x2 << 3) /* Channel x Next Descriptor View 2 */
  101. #define AT_XDMAC_CNDC_NDVIEW_NDV3 (0x3 << 3) /* Channel x Next Descriptor View 3 */
  102. #define AT_XDMAC_CUBC 0x20 /* Channel Microblock Control Register */
  103. #define AT_XDMAC_CBC 0x24 /* Channel Block Control Register */
  104. #define AT_XDMAC_CC 0x28 /* Channel Configuration Register */
  105. #define AT_XDMAC_CC_TYPE (0x1 << 0) /* Channel Transfer Type */
  106. #define AT_XDMAC_CC_TYPE_MEM_TRAN (0x0 << 0) /* Memory to Memory Transfer */
  107. #define AT_XDMAC_CC_TYPE_PER_TRAN (0x1 << 0) /* Peripheral to Memory or Memory to Peripheral Transfer */
  108. #define AT_XDMAC_CC_MBSIZE_MASK (0x3 << 1)
  109. #define AT_XDMAC_CC_MBSIZE_SINGLE (0x0 << 1)
  110. #define AT_XDMAC_CC_MBSIZE_FOUR (0x1 << 1)
  111. #define AT_XDMAC_CC_MBSIZE_EIGHT (0x2 << 1)
  112. #define AT_XDMAC_CC_MBSIZE_SIXTEEN (0x3 << 1)
  113. #define AT_XDMAC_CC_DSYNC (0x1 << 4) /* Channel Synchronization */
  114. #define AT_XDMAC_CC_DSYNC_PER2MEM (0x0 << 4)
  115. #define AT_XDMAC_CC_DSYNC_MEM2PER (0x1 << 4)
  116. #define AT_XDMAC_CC_PROT (0x1 << 5) /* Channel Protection */
  117. #define AT_XDMAC_CC_PROT_SEC (0x0 << 5)
  118. #define AT_XDMAC_CC_PROT_UNSEC (0x1 << 5)
  119. #define AT_XDMAC_CC_SWREQ (0x1 << 6) /* Channel Software Request Trigger */
  120. #define AT_XDMAC_CC_SWREQ_HWR_CONNECTED (0x0 << 6)
  121. #define AT_XDMAC_CC_SWREQ_SWR_CONNECTED (0x1 << 6)
  122. #define AT_XDMAC_CC_MEMSET (0x1 << 7) /* Channel Fill Block of memory */
  123. #define AT_XDMAC_CC_MEMSET_NORMAL_MODE (0x0 << 7)
  124. #define AT_XDMAC_CC_MEMSET_HW_MODE (0x1 << 7)
  125. #define AT_XDMAC_CC_CSIZE(i) ((0x7 & (i)) << 8) /* Channel Chunk Size */
  126. #define AT_XDMAC_CC_DWIDTH_OFFSET 11
  127. #define AT_XDMAC_CC_DWIDTH_MASK (0x3 << AT_XDMAC_CC_DWIDTH_OFFSET)
  128. #define AT_XDMAC_CC_DWIDTH(i) ((0x3 & (i)) << AT_XDMAC_CC_DWIDTH_OFFSET) /* Channel Data Width */
  129. #define AT_XDMAC_CC_DWIDTH_BYTE 0x0
  130. #define AT_XDMAC_CC_DWIDTH_HALFWORD 0x1
  131. #define AT_XDMAC_CC_DWIDTH_WORD 0x2
  132. #define AT_XDMAC_CC_DWIDTH_DWORD 0x3
  133. #define AT_XDMAC_CC_SIF(i) ((0x1 & (i)) << 13) /* Channel Source Interface Identifier */
  134. #define AT_XDMAC_CC_DIF(i) ((0x1 & (i)) << 14) /* Channel Destination Interface Identifier */
  135. #define AT_XDMAC_CC_SAM_MASK (0x3 << 16) /* Channel Source Addressing Mode */
  136. #define AT_XDMAC_CC_SAM_FIXED_AM (0x0 << 16)
  137. #define AT_XDMAC_CC_SAM_INCREMENTED_AM (0x1 << 16)
  138. #define AT_XDMAC_CC_SAM_UBS_AM (0x2 << 16)
  139. #define AT_XDMAC_CC_SAM_UBS_DS_AM (0x3 << 16)
  140. #define AT_XDMAC_CC_DAM_MASK (0x3 << 18) /* Channel Source Addressing Mode */
  141. #define AT_XDMAC_CC_DAM_FIXED_AM (0x0 << 18)
  142. #define AT_XDMAC_CC_DAM_INCREMENTED_AM (0x1 << 18)
  143. #define AT_XDMAC_CC_DAM_UBS_AM (0x2 << 18)
  144. #define AT_XDMAC_CC_DAM_UBS_DS_AM (0x3 << 18)
  145. #define AT_XDMAC_CC_INITD (0x1 << 21) /* Channel Initialization Terminated (read only) */
  146. #define AT_XDMAC_CC_INITD_TERMINATED (0x0 << 21)
  147. #define AT_XDMAC_CC_INITD_IN_PROGRESS (0x1 << 21)
  148. #define AT_XDMAC_CC_RDIP (0x1 << 22) /* Read in Progress (read only) */
  149. #define AT_XDMAC_CC_RDIP_DONE (0x0 << 22)
  150. #define AT_XDMAC_CC_RDIP_IN_PROGRESS (0x1 << 22)
  151. #define AT_XDMAC_CC_WRIP (0x1 << 23) /* Write in Progress (read only) */
  152. #define AT_XDMAC_CC_WRIP_DONE (0x0 << 23)
  153. #define AT_XDMAC_CC_WRIP_IN_PROGRESS (0x1 << 23)
  154. #define AT_XDMAC_CC_PERID(i) (0x7f & (h) << 24) /* Channel Peripheral Identifier */
  155. #define AT_XDMAC_CDS_MSP 0x2C /* Channel Data Stride Memory Set Pattern */
  156. #define AT_XDMAC_CSUS 0x30 /* Channel Source Microblock Stride */
  157. #define AT_XDMAC_CDUS 0x34 /* Channel Destination Microblock Stride */
  158. #define AT_XDMAC_CHAN_REG_BASE 0x50 /* Channel registers base address */
  159. /* Microblock control members */
  160. #define AT_XDMAC_MBR_UBC_UBLEN_MAX 0xFFFFFFUL /* Maximum Microblock Length */
  161. #define AT_XDMAC_MBR_UBC_NDE (0x1 << 24) /* Next Descriptor Enable */
  162. #define AT_XDMAC_MBR_UBC_NSEN (0x1 << 25) /* Next Descriptor Source Update */
  163. #define AT_XDMAC_MBR_UBC_NDEN (0x1 << 26) /* Next Descriptor Destination Update */
  164. #define AT_XDMAC_MBR_UBC_NDV0 (0x0 << 27) /* Next Descriptor View 0 */
  165. #define AT_XDMAC_MBR_UBC_NDV1 (0x1 << 27) /* Next Descriptor View 1 */
  166. #define AT_XDMAC_MBR_UBC_NDV2 (0x2 << 27) /* Next Descriptor View 2 */
  167. #define AT_XDMAC_MBR_UBC_NDV3 (0x3 << 27) /* Next Descriptor View 3 */
  168. #define AT_XDMAC_MAX_CHAN 0x20
  169. enum atc_status {
  170. AT_XDMAC_CHAN_IS_CYCLIC = 0,
  171. AT_XDMAC_CHAN_IS_PAUSED,
  172. };
  173. /* ----- Channels ----- */
  174. struct at_xdmac_chan {
  175. struct dma_chan chan;
  176. void __iomem *ch_regs;
  177. u32 mask; /* Channel Mask */
  178. u32 cfg[3]; /* Channel Configuration Register */
  179. #define AT_XDMAC_CUR_CFG 0 /* Current channel conf */
  180. #define AT_XDMAC_DEV_TO_MEM_CFG 1 /* Predifined dev to mem channel conf */
  181. #define AT_XDMAC_MEM_TO_DEV_CFG 2 /* Predifined mem to dev channel conf */
  182. u8 perid; /* Peripheral ID */
  183. u8 perif; /* Peripheral Interface */
  184. u8 memif; /* Memory Interface */
  185. u32 per_src_addr;
  186. u32 per_dst_addr;
  187. u32 save_cim;
  188. u32 save_cnda;
  189. u32 save_cndc;
  190. unsigned long status;
  191. struct tasklet_struct tasklet;
  192. spinlock_t lock;
  193. struct list_head xfers_list;
  194. struct list_head free_descs_list;
  195. };
  196. /* ----- Controller ----- */
  197. struct at_xdmac {
  198. struct dma_device dma;
  199. void __iomem *regs;
  200. int irq;
  201. struct clk *clk;
  202. u32 save_gim;
  203. u32 save_gs;
  204. struct dma_pool *at_xdmac_desc_pool;
  205. struct at_xdmac_chan chan[0];
  206. };
  207. /* ----- Descriptors ----- */
  208. /* Linked List Descriptor */
  209. struct at_xdmac_lld {
  210. dma_addr_t mbr_nda; /* Next Descriptor Member */
  211. u32 mbr_ubc; /* Microblock Control Member */
  212. dma_addr_t mbr_sa; /* Source Address Member */
  213. dma_addr_t mbr_da; /* Destination Address Member */
  214. u32 mbr_cfg; /* Configuration Register */
  215. };
  216. struct at_xdmac_desc {
  217. struct at_xdmac_lld lld;
  218. enum dma_transfer_direction direction;
  219. struct dma_async_tx_descriptor tx_dma_desc;
  220. struct list_head desc_node;
  221. /* Following members are only used by the first descriptor */
  222. bool active_xfer;
  223. unsigned int xfer_size;
  224. struct list_head descs_list;
  225. struct list_head xfer_node;
  226. };
  227. static inline void __iomem *at_xdmac_chan_reg_base(struct at_xdmac *atxdmac, unsigned int chan_nb)
  228. {
  229. return atxdmac->regs + (AT_XDMAC_CHAN_REG_BASE + chan_nb * 0x40);
  230. }
  231. #define at_xdmac_read(atxdmac, reg) readl_relaxed((atxdmac)->regs + (reg))
  232. #define at_xdmac_write(atxdmac, reg, value) \
  233. writel_relaxed((value), (atxdmac)->regs + (reg))
  234. #define at_xdmac_chan_read(atchan, reg) readl_relaxed((atchan)->ch_regs + (reg))
  235. #define at_xdmac_chan_write(atchan, reg, value) writel_relaxed((value), (atchan)->ch_regs + (reg))
  236. static inline struct at_xdmac_chan *to_at_xdmac_chan(struct dma_chan *dchan)
  237. {
  238. return container_of(dchan, struct at_xdmac_chan, chan);
  239. }
  240. static struct device *chan2dev(struct dma_chan *chan)
  241. {
  242. return &chan->dev->device;
  243. }
  244. static inline struct at_xdmac *to_at_xdmac(struct dma_device *ddev)
  245. {
  246. return container_of(ddev, struct at_xdmac, dma);
  247. }
  248. static inline struct at_xdmac_desc *txd_to_at_desc(struct dma_async_tx_descriptor *txd)
  249. {
  250. return container_of(txd, struct at_xdmac_desc, tx_dma_desc);
  251. }
  252. static inline int at_xdmac_chan_is_cyclic(struct at_xdmac_chan *atchan)
  253. {
  254. return test_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  255. }
  256. static inline int at_xdmac_chan_is_paused(struct at_xdmac_chan *atchan)
  257. {
  258. return test_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  259. }
  260. static inline int at_xdmac_csize(u32 maxburst)
  261. {
  262. int csize;
  263. csize = ffs(maxburst) - 1;
  264. if (csize > 4)
  265. csize = -EINVAL;
  266. return csize;
  267. };
  268. static inline u8 at_xdmac_get_dwidth(u32 cfg)
  269. {
  270. return (cfg & AT_XDMAC_CC_DWIDTH_MASK) >> AT_XDMAC_CC_DWIDTH_OFFSET;
  271. };
  272. static unsigned int init_nr_desc_per_channel = 64;
  273. module_param(init_nr_desc_per_channel, uint, 0644);
  274. MODULE_PARM_DESC(init_nr_desc_per_channel,
  275. "initial descriptors per channel (default: 64)");
  276. static bool at_xdmac_chan_is_enabled(struct at_xdmac_chan *atchan)
  277. {
  278. return at_xdmac_chan_read(atchan, AT_XDMAC_GS) & atchan->mask;
  279. }
  280. static void at_xdmac_off(struct at_xdmac *atxdmac)
  281. {
  282. at_xdmac_write(atxdmac, AT_XDMAC_GD, -1L);
  283. /* Wait that all chans are disabled. */
  284. while (at_xdmac_read(atxdmac, AT_XDMAC_GS))
  285. cpu_relax();
  286. at_xdmac_write(atxdmac, AT_XDMAC_GID, -1L);
  287. }
  288. /* Call with lock hold. */
  289. static void at_xdmac_start_xfer(struct at_xdmac_chan *atchan,
  290. struct at_xdmac_desc *first)
  291. {
  292. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  293. u32 reg;
  294. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, first);
  295. if (at_xdmac_chan_is_enabled(atchan))
  296. return;
  297. /* Set transfer as active to not try to start it again. */
  298. first->active_xfer = true;
  299. /* Tell xdmac where to get the first descriptor. */
  300. reg = AT_XDMAC_CNDA_NDA(first->tx_dma_desc.phys)
  301. | AT_XDMAC_CNDA_NDAIF(atchan->memif);
  302. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, reg);
  303. /*
  304. * When doing memory to memory transfer we need to use the next
  305. * descriptor view 2 since some fields of the configuration register
  306. * depend on transfer size and src/dest addresses.
  307. */
  308. if (is_slave_direction(first->direction)) {
  309. reg = AT_XDMAC_CNDC_NDVIEW_NDV1;
  310. if (first->direction == DMA_MEM_TO_DEV)
  311. atchan->cfg[AT_XDMAC_CUR_CFG] =
  312. atchan->cfg[AT_XDMAC_MEM_TO_DEV_CFG];
  313. else
  314. atchan->cfg[AT_XDMAC_CUR_CFG] =
  315. atchan->cfg[AT_XDMAC_DEV_TO_MEM_CFG];
  316. at_xdmac_chan_write(atchan, AT_XDMAC_CC,
  317. atchan->cfg[AT_XDMAC_CUR_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. u32 cfg;
  494. unsigned int xfer_size = 0;
  495. if (!sgl)
  496. return NULL;
  497. if (!is_slave_direction(direction)) {
  498. dev_err(chan2dev(chan), "invalid DMA direction\n");
  499. return NULL;
  500. }
  501. dev_dbg(chan2dev(chan), "%s: sg_len=%d, dir=%s, flags=0x%lx\n",
  502. __func__, sg_len,
  503. direction == DMA_MEM_TO_DEV ? "to device" : "from device",
  504. flags);
  505. /* Protect dma_sconfig field that can be modified by set_slave_conf. */
  506. spin_lock_bh(&atchan->lock);
  507. /* Prepare descriptors. */
  508. for_each_sg(sgl, sg, sg_len, i) {
  509. struct at_xdmac_desc *desc = NULL;
  510. u32 len, mem;
  511. len = sg_dma_len(sg);
  512. mem = sg_dma_address(sg);
  513. if (unlikely(!len)) {
  514. dev_err(chan2dev(chan), "sg data length is zero\n");
  515. spin_unlock_bh(&atchan->lock);
  516. return NULL;
  517. }
  518. dev_dbg(chan2dev(chan), "%s: * sg%d len=%u, mem=0x%08x\n",
  519. __func__, i, len, mem);
  520. desc = at_xdmac_get_desc(atchan);
  521. if (!desc) {
  522. dev_err(chan2dev(chan), "can't get descriptor\n");
  523. if (first)
  524. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  525. spin_unlock_bh(&atchan->lock);
  526. return NULL;
  527. }
  528. /* Linked list descriptor setup. */
  529. if (direction == DMA_DEV_TO_MEM) {
  530. desc->lld.mbr_sa = atchan->per_src_addr;
  531. desc->lld.mbr_da = mem;
  532. cfg = atchan->cfg[AT_XDMAC_DEV_TO_MEM_CFG];
  533. } else {
  534. desc->lld.mbr_sa = mem;
  535. desc->lld.mbr_da = atchan->per_dst_addr;
  536. cfg = atchan->cfg[AT_XDMAC_MEM_TO_DEV_CFG];
  537. }
  538. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV1 /* next descriptor view */
  539. | AT_XDMAC_MBR_UBC_NDEN /* next descriptor dst parameter update */
  540. | AT_XDMAC_MBR_UBC_NSEN /* next descriptor src parameter update */
  541. | (i == sg_len - 1 ? 0 : AT_XDMAC_MBR_UBC_NDE) /* descriptor fetch */
  542. | len / (1 << at_xdmac_get_dwidth(cfg)); /* microblock length */
  543. dev_dbg(chan2dev(chan),
  544. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  545. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  546. /* Chain lld. */
  547. if (prev) {
  548. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  549. dev_dbg(chan2dev(chan),
  550. "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  551. __func__, prev, &prev->lld.mbr_nda);
  552. }
  553. prev = desc;
  554. if (!first)
  555. first = desc;
  556. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  557. __func__, desc, first);
  558. list_add_tail(&desc->desc_node, &first->descs_list);
  559. xfer_size += len;
  560. }
  561. spin_unlock_bh(&atchan->lock);
  562. first->tx_dma_desc.flags = flags;
  563. first->xfer_size = xfer_size;
  564. first->direction = direction;
  565. return &first->tx_dma_desc;
  566. }
  567. static struct dma_async_tx_descriptor *
  568. at_xdmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
  569. size_t buf_len, size_t period_len,
  570. enum dma_transfer_direction direction,
  571. unsigned long flags)
  572. {
  573. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  574. struct at_xdmac_desc *first = NULL, *prev = NULL;
  575. unsigned int periods = buf_len / period_len;
  576. int i;
  577. u32 cfg;
  578. dev_dbg(chan2dev(chan), "%s: buf_addr=%pad, buf_len=%zd, period_len=%zd, dir=%s, flags=0x%lx\n",
  579. __func__, &buf_addr, buf_len, period_len,
  580. direction == DMA_MEM_TO_DEV ? "mem2per" : "per2mem", flags);
  581. if (!is_slave_direction(direction)) {
  582. dev_err(chan2dev(chan), "invalid DMA direction\n");
  583. return NULL;
  584. }
  585. if (test_and_set_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status)) {
  586. dev_err(chan2dev(chan), "channel currently used\n");
  587. return NULL;
  588. }
  589. for (i = 0; i < periods; i++) {
  590. struct at_xdmac_desc *desc = NULL;
  591. spin_lock_bh(&atchan->lock);
  592. desc = at_xdmac_get_desc(atchan);
  593. if (!desc) {
  594. dev_err(chan2dev(chan), "can't get descriptor\n");
  595. if (first)
  596. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  597. spin_unlock_bh(&atchan->lock);
  598. return NULL;
  599. }
  600. spin_unlock_bh(&atchan->lock);
  601. dev_dbg(chan2dev(chan),
  602. "%s: desc=0x%p, tx_dma_desc.phys=%pad\n",
  603. __func__, desc, &desc->tx_dma_desc.phys);
  604. if (direction == DMA_DEV_TO_MEM) {
  605. desc->lld.mbr_sa = atchan->per_src_addr;
  606. desc->lld.mbr_da = buf_addr + i * period_len;
  607. cfg = atchan->cfg[AT_XDMAC_DEV_TO_MEM_CFG];
  608. } else {
  609. desc->lld.mbr_sa = buf_addr + i * period_len;
  610. desc->lld.mbr_da = atchan->per_dst_addr;
  611. cfg = atchan->cfg[AT_XDMAC_MEM_TO_DEV_CFG];
  612. }
  613. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV1
  614. | AT_XDMAC_MBR_UBC_NDEN
  615. | AT_XDMAC_MBR_UBC_NSEN
  616. | AT_XDMAC_MBR_UBC_NDE
  617. | period_len >> at_xdmac_get_dwidth(cfg);
  618. dev_dbg(chan2dev(chan),
  619. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  620. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  621. /* Chain lld. */
  622. if (prev) {
  623. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  624. dev_dbg(chan2dev(chan),
  625. "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  626. __func__, prev, &prev->lld.mbr_nda);
  627. }
  628. prev = desc;
  629. if (!first)
  630. first = desc;
  631. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  632. __func__, desc, first);
  633. list_add_tail(&desc->desc_node, &first->descs_list);
  634. }
  635. prev->lld.mbr_nda = first->tx_dma_desc.phys;
  636. dev_dbg(chan2dev(chan),
  637. "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  638. __func__, prev, &prev->lld.mbr_nda);
  639. first->tx_dma_desc.flags = flags;
  640. first->xfer_size = buf_len;
  641. first->direction = direction;
  642. return &first->tx_dma_desc;
  643. }
  644. static struct dma_async_tx_descriptor *
  645. at_xdmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  646. size_t len, unsigned long flags)
  647. {
  648. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  649. struct at_xdmac_desc *first = NULL, *prev = NULL;
  650. size_t remaining_size = len, xfer_size = 0, ublen;
  651. dma_addr_t src_addr = src, dst_addr = dest;
  652. u32 dwidth;
  653. /*
  654. * WARNING: We don't know the direction, it involves we can't
  655. * dynamically set the source and dest interface so we have to use the
  656. * same one. Only interface 0 allows EBI access. Hopefully we can
  657. * access DDR through both ports (at least on SAMA5D4x), so we can use
  658. * the same interface for source and dest, that solves the fact we
  659. * don't know the direction.
  660. */
  661. u32 chan_cc = AT_XDMAC_CC_DAM_INCREMENTED_AM
  662. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  663. | AT_XDMAC_CC_DIF(0)
  664. | AT_XDMAC_CC_SIF(0)
  665. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  666. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  667. dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, len=%zd, flags=0x%lx\n",
  668. __func__, &src, &dest, len, flags);
  669. if (unlikely(!len))
  670. return NULL;
  671. /*
  672. * Check address alignment to select the greater data width we can use.
  673. * Some XDMAC implementations don't provide dword transfer, in this
  674. * case selecting dword has the same behavior as selecting word transfers.
  675. */
  676. if (!((src_addr | dst_addr) & 7)) {
  677. dwidth = AT_XDMAC_CC_DWIDTH_DWORD;
  678. dev_dbg(chan2dev(chan), "%s: dwidth: double word\n", __func__);
  679. } else if (!((src_addr | dst_addr) & 3)) {
  680. dwidth = AT_XDMAC_CC_DWIDTH_WORD;
  681. dev_dbg(chan2dev(chan), "%s: dwidth: word\n", __func__);
  682. } else if (!((src_addr | dst_addr) & 1)) {
  683. dwidth = AT_XDMAC_CC_DWIDTH_HALFWORD;
  684. dev_dbg(chan2dev(chan), "%s: dwidth: half word\n", __func__);
  685. } else {
  686. dwidth = AT_XDMAC_CC_DWIDTH_BYTE;
  687. dev_dbg(chan2dev(chan), "%s: dwidth: byte\n", __func__);
  688. }
  689. /* Prepare descriptors. */
  690. while (remaining_size) {
  691. struct at_xdmac_desc *desc = NULL;
  692. dev_dbg(chan2dev(chan), "%s: remaining_size=%zu\n", __func__, remaining_size);
  693. spin_lock_bh(&atchan->lock);
  694. desc = at_xdmac_get_desc(atchan);
  695. spin_unlock_bh(&atchan->lock);
  696. if (!desc) {
  697. dev_err(chan2dev(chan), "can't get descriptor\n");
  698. if (first)
  699. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  700. return NULL;
  701. }
  702. /* Update src and dest addresses. */
  703. src_addr += xfer_size;
  704. dst_addr += xfer_size;
  705. if (remaining_size >= AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)
  706. xfer_size = AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth;
  707. else
  708. xfer_size = remaining_size;
  709. dev_dbg(chan2dev(chan), "%s: xfer_size=%zu\n", __func__, xfer_size);
  710. /* Check remaining length and change data width if needed. */
  711. if (!((src_addr | dst_addr | xfer_size) & 7)) {
  712. dwidth = AT_XDMAC_CC_DWIDTH_DWORD;
  713. dev_dbg(chan2dev(chan), "%s: dwidth: double word\n", __func__);
  714. } else if (!((src_addr | dst_addr | xfer_size) & 3)) {
  715. dwidth = AT_XDMAC_CC_DWIDTH_WORD;
  716. dev_dbg(chan2dev(chan), "%s: dwidth: word\n", __func__);
  717. } else if (!((src_addr | dst_addr | xfer_size) & 1)) {
  718. dwidth = AT_XDMAC_CC_DWIDTH_HALFWORD;
  719. dev_dbg(chan2dev(chan), "%s: dwidth: half word\n", __func__);
  720. } else if ((src_addr | dst_addr | xfer_size) & 1) {
  721. dwidth = AT_XDMAC_CC_DWIDTH_BYTE;
  722. dev_dbg(chan2dev(chan), "%s: dwidth: byte\n", __func__);
  723. }
  724. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  725. ublen = xfer_size >> dwidth;
  726. remaining_size -= xfer_size;
  727. desc->lld.mbr_sa = src_addr;
  728. desc->lld.mbr_da = dst_addr;
  729. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2
  730. | AT_XDMAC_MBR_UBC_NDEN
  731. | AT_XDMAC_MBR_UBC_NSEN
  732. | (remaining_size ? AT_XDMAC_MBR_UBC_NDE : 0)
  733. | ublen;
  734. desc->lld.mbr_cfg = chan_cc;
  735. dev_dbg(chan2dev(chan),
  736. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  737. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc, desc->lld.mbr_cfg);
  738. /* Chain lld. */
  739. if (prev) {
  740. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  741. dev_dbg(chan2dev(chan),
  742. "%s: chain lld: prev=0x%p, mbr_nda=0x%08x\n",
  743. __func__, prev, prev->lld.mbr_nda);
  744. }
  745. prev = desc;
  746. if (!first)
  747. first = desc;
  748. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  749. __func__, desc, first);
  750. list_add_tail(&desc->desc_node, &first->descs_list);
  751. }
  752. first->tx_dma_desc.flags = flags;
  753. first->xfer_size = len;
  754. return &first->tx_dma_desc;
  755. }
  756. static enum dma_status
  757. at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  758. struct dma_tx_state *txstate)
  759. {
  760. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  761. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  762. struct at_xdmac_desc *desc, *_desc;
  763. struct list_head *descs_list;
  764. enum dma_status ret;
  765. int residue;
  766. u32 cur_nda, mask, value;
  767. u8 dwidth = at_xdmac_get_dwidth(atchan->cfg[AT_XDMAC_CUR_CFG]);
  768. ret = dma_cookie_status(chan, cookie, txstate);
  769. if (ret == DMA_COMPLETE)
  770. return ret;
  771. if (!txstate)
  772. return ret;
  773. spin_lock_bh(&atchan->lock);
  774. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  775. /*
  776. * If the transfer has not been started yet, don't need to compute the
  777. * residue, it's the transfer length.
  778. */
  779. if (!desc->active_xfer) {
  780. dma_set_residue(txstate, desc->xfer_size);
  781. spin_unlock_bh(&atchan->lock);
  782. return ret;
  783. }
  784. residue = desc->xfer_size;
  785. /*
  786. * Flush FIFO: only relevant when the transfer is source peripheral
  787. * synchronized.
  788. */
  789. mask = AT_XDMAC_CC_TYPE | AT_XDMAC_CC_DSYNC;
  790. value = AT_XDMAC_CC_TYPE_PER_TRAN | AT_XDMAC_CC_DSYNC_PER2MEM;
  791. if ((atchan->cfg[AT_XDMAC_CUR_CFG] & mask) == value) {
  792. at_xdmac_write(atxdmac, AT_XDMAC_GSWF, atchan->mask);
  793. while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
  794. cpu_relax();
  795. }
  796. cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  797. /*
  798. * Remove size of all microblocks already transferred and the current
  799. * one. Then add the remaining size to transfer of the current
  800. * microblock.
  801. */
  802. descs_list = &desc->descs_list;
  803. list_for_each_entry_safe(desc, _desc, descs_list, desc_node) {
  804. residue -= (desc->lld.mbr_ubc & 0xffffff) << dwidth;
  805. if ((desc->lld.mbr_nda & 0xfffffffc) == cur_nda)
  806. break;
  807. }
  808. residue += at_xdmac_chan_read(atchan, AT_XDMAC_CUBC) << dwidth;
  809. spin_unlock_bh(&atchan->lock);
  810. dma_set_residue(txstate, residue);
  811. dev_dbg(chan2dev(chan),
  812. "%s: desc=0x%p, tx_dma_desc.phys=%pad, tx_status=%d, cookie=%d, residue=%d\n",
  813. __func__, desc, &desc->tx_dma_desc.phys, ret, cookie, residue);
  814. return ret;
  815. }
  816. /* Call must be protected by lock. */
  817. static void at_xdmac_remove_xfer(struct at_xdmac_chan *atchan,
  818. struct at_xdmac_desc *desc)
  819. {
  820. dev_dbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  821. /*
  822. * Remove the transfer from the transfer list then move the transfer
  823. * descriptors into the free descriptors list.
  824. */
  825. list_del(&desc->xfer_node);
  826. list_splice_init(&desc->descs_list, &atchan->free_descs_list);
  827. }
  828. static void at_xdmac_advance_work(struct at_xdmac_chan *atchan)
  829. {
  830. struct at_xdmac_desc *desc;
  831. spin_lock_bh(&atchan->lock);
  832. /*
  833. * If channel is enabled, do nothing, advance_work will be triggered
  834. * after the interruption.
  835. */
  836. if (!at_xdmac_chan_is_enabled(atchan) && !list_empty(&atchan->xfers_list)) {
  837. desc = list_first_entry(&atchan->xfers_list,
  838. struct at_xdmac_desc,
  839. xfer_node);
  840. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  841. if (!desc->active_xfer)
  842. at_xdmac_start_xfer(atchan, desc);
  843. }
  844. spin_unlock_bh(&atchan->lock);
  845. }
  846. static void at_xdmac_handle_cyclic(struct at_xdmac_chan *atchan)
  847. {
  848. struct at_xdmac_desc *desc;
  849. struct dma_async_tx_descriptor *txd;
  850. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  851. txd = &desc->tx_dma_desc;
  852. if (txd->callback && (txd->flags & DMA_PREP_INTERRUPT))
  853. txd->callback(txd->callback_param);
  854. }
  855. static void at_xdmac_tasklet(unsigned long data)
  856. {
  857. struct at_xdmac_chan *atchan = (struct at_xdmac_chan *)data;
  858. struct at_xdmac_desc *desc;
  859. u32 error_mask;
  860. dev_dbg(chan2dev(&atchan->chan), "%s: status=0x%08lx\n",
  861. __func__, atchan->status);
  862. error_mask = AT_XDMAC_CIS_RBEIS
  863. | AT_XDMAC_CIS_WBEIS
  864. | AT_XDMAC_CIS_ROIS;
  865. if (at_xdmac_chan_is_cyclic(atchan)) {
  866. at_xdmac_handle_cyclic(atchan);
  867. } else if ((atchan->status & AT_XDMAC_CIS_LIS)
  868. || (atchan->status & error_mask)) {
  869. struct dma_async_tx_descriptor *txd;
  870. if (atchan->status & AT_XDMAC_CIS_RBEIS)
  871. dev_err(chan2dev(&atchan->chan), "read bus error!!!");
  872. if (atchan->status & AT_XDMAC_CIS_WBEIS)
  873. dev_err(chan2dev(&atchan->chan), "write bus error!!!");
  874. if (atchan->status & AT_XDMAC_CIS_ROIS)
  875. dev_err(chan2dev(&atchan->chan), "request overflow error!!!");
  876. spin_lock_bh(&atchan->lock);
  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. BUG_ON(!desc->active_xfer);
  882. txd = &desc->tx_dma_desc;
  883. at_xdmac_remove_xfer(atchan, desc);
  884. spin_unlock_bh(&atchan->lock);
  885. if (!at_xdmac_chan_is_cyclic(atchan)) {
  886. dma_cookie_complete(txd);
  887. if (txd->callback && (txd->flags & DMA_PREP_INTERRUPT))
  888. txd->callback(txd->callback_param);
  889. }
  890. dma_run_dependencies(txd);
  891. at_xdmac_advance_work(atchan);
  892. }
  893. }
  894. static irqreturn_t at_xdmac_interrupt(int irq, void *dev_id)
  895. {
  896. struct at_xdmac *atxdmac = (struct at_xdmac *)dev_id;
  897. struct at_xdmac_chan *atchan;
  898. u32 imr, status, pending;
  899. u32 chan_imr, chan_status;
  900. int i, ret = IRQ_NONE;
  901. do {
  902. imr = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  903. status = at_xdmac_read(atxdmac, AT_XDMAC_GIS);
  904. pending = status & imr;
  905. dev_vdbg(atxdmac->dma.dev,
  906. "%s: status=0x%08x, imr=0x%08x, pending=0x%08x\n",
  907. __func__, status, imr, pending);
  908. if (!pending)
  909. break;
  910. /* We have to find which channel has generated the interrupt. */
  911. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  912. if (!((1 << i) & pending))
  913. continue;
  914. atchan = &atxdmac->chan[i];
  915. chan_imr = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  916. chan_status = at_xdmac_chan_read(atchan, AT_XDMAC_CIS);
  917. atchan->status = chan_status & chan_imr;
  918. dev_vdbg(atxdmac->dma.dev,
  919. "%s: chan%d: imr=0x%x, status=0x%x\n",
  920. __func__, i, chan_imr, chan_status);
  921. dev_vdbg(chan2dev(&atchan->chan),
  922. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  923. __func__,
  924. at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  925. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  926. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  927. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  928. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  929. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  930. if (atchan->status & (AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS))
  931. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  932. tasklet_schedule(&atchan->tasklet);
  933. ret = IRQ_HANDLED;
  934. }
  935. } while (pending);
  936. return ret;
  937. }
  938. static void at_xdmac_issue_pending(struct dma_chan *chan)
  939. {
  940. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  941. dev_dbg(chan2dev(&atchan->chan), "%s\n", __func__);
  942. if (!at_xdmac_chan_is_cyclic(atchan))
  943. at_xdmac_advance_work(atchan);
  944. return;
  945. }
  946. static int at_xdmac_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  947. unsigned long arg)
  948. {
  949. struct at_xdmac_desc *desc, *_desc;
  950. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  951. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  952. int ret = 0;
  953. dev_dbg(chan2dev(chan), "%s: cmd=%d\n", __func__, cmd);
  954. spin_lock_bh(&atchan->lock);
  955. switch (cmd) {
  956. case DMA_PAUSE:
  957. at_xdmac_write(atxdmac, AT_XDMAC_GRWS, atchan->mask);
  958. set_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  959. break;
  960. case DMA_RESUME:
  961. if (!at_xdmac_chan_is_paused(atchan))
  962. break;
  963. at_xdmac_write(atxdmac, AT_XDMAC_GRWR, atchan->mask);
  964. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  965. break;
  966. case DMA_TERMINATE_ALL:
  967. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  968. while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
  969. cpu_relax();
  970. /* Cancel all pending transfers. */
  971. list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node)
  972. at_xdmac_remove_xfer(atchan, desc);
  973. clear_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  974. break;
  975. case DMA_SLAVE_CONFIG:
  976. ret = at_xdmac_set_slave_config(chan,
  977. (struct dma_slave_config *)arg);
  978. break;
  979. default:
  980. dev_err(chan2dev(chan),
  981. "unmanaged or unknown dma control cmd: %d\n", cmd);
  982. ret = -ENXIO;
  983. }
  984. spin_unlock_bh(&atchan->lock);
  985. return ret;
  986. }
  987. static int at_xdmac_alloc_chan_resources(struct dma_chan *chan)
  988. {
  989. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  990. struct at_xdmac_desc *desc;
  991. int i;
  992. spin_lock_bh(&atchan->lock);
  993. if (at_xdmac_chan_is_enabled(atchan)) {
  994. dev_err(chan2dev(chan),
  995. "can't allocate channel resources (channel enabled)\n");
  996. i = -EIO;
  997. goto spin_unlock;
  998. }
  999. if (!list_empty(&atchan->free_descs_list)) {
  1000. dev_err(chan2dev(chan),
  1001. "can't allocate channel resources (channel not free from a previous use)\n");
  1002. i = -EIO;
  1003. goto spin_unlock;
  1004. }
  1005. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1006. desc = at_xdmac_alloc_desc(chan, GFP_ATOMIC);
  1007. if (!desc) {
  1008. dev_warn(chan2dev(chan),
  1009. "only %d descriptors have been allocated\n", i);
  1010. break;
  1011. }
  1012. list_add_tail(&desc->desc_node, &atchan->free_descs_list);
  1013. }
  1014. dma_cookie_init(chan);
  1015. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  1016. spin_unlock:
  1017. spin_unlock_bh(&atchan->lock);
  1018. return i;
  1019. }
  1020. static void at_xdmac_free_chan_resources(struct dma_chan *chan)
  1021. {
  1022. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1023. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  1024. struct at_xdmac_desc *desc, *_desc;
  1025. list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) {
  1026. dev_dbg(chan2dev(chan), "%s: freeing descriptor %p\n", __func__, desc);
  1027. list_del(&desc->desc_node);
  1028. dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys);
  1029. }
  1030. return;
  1031. }
  1032. #define AT_XDMAC_DMA_BUSWIDTHS\
  1033. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) |\
  1034. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |\
  1035. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |\
  1036. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |\
  1037. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  1038. static int at_xdmac_device_slave_caps(struct dma_chan *dchan,
  1039. struct dma_slave_caps *caps)
  1040. {
  1041. caps->src_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1042. caps->dstn_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1043. caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1044. caps->cmd_pause = true;
  1045. caps->cmd_terminate = true;
  1046. caps->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1047. return 0;
  1048. }
  1049. #ifdef CONFIG_PM
  1050. static int atmel_xdmac_prepare(struct device *dev)
  1051. {
  1052. struct platform_device *pdev = to_platform_device(dev);
  1053. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1054. struct dma_chan *chan, *_chan;
  1055. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1056. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1057. /* Wait for transfer completion, except in cyclic case. */
  1058. if (at_xdmac_chan_is_enabled(atchan) && !at_xdmac_chan_is_cyclic(atchan))
  1059. return -EAGAIN;
  1060. }
  1061. return 0;
  1062. }
  1063. #else
  1064. # define atmel_xdmac_prepare NULL
  1065. #endif
  1066. #ifdef CONFIG_PM_SLEEP
  1067. static int atmel_xdmac_suspend(struct device *dev)
  1068. {
  1069. struct platform_device *pdev = to_platform_device(dev);
  1070. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1071. struct dma_chan *chan, *_chan;
  1072. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1073. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1074. if (at_xdmac_chan_is_cyclic(atchan)) {
  1075. if (!at_xdmac_chan_is_paused(atchan))
  1076. at_xdmac_control(chan, DMA_PAUSE, 0);
  1077. atchan->save_cim = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1078. atchan->save_cnda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA);
  1079. atchan->save_cndc = at_xdmac_chan_read(atchan, AT_XDMAC_CNDC);
  1080. }
  1081. }
  1082. atxdmac->save_gim = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1083. at_xdmac_off(atxdmac);
  1084. clk_disable_unprepare(atxdmac->clk);
  1085. return 0;
  1086. }
  1087. static int atmel_xdmac_resume(struct device *dev)
  1088. {
  1089. struct platform_device *pdev = to_platform_device(dev);
  1090. struct at_xdmac *atxdmac = platform_get_drvdata(pdev);
  1091. struct at_xdmac_chan *atchan;
  1092. struct dma_chan *chan, *_chan;
  1093. int i;
  1094. u32 cfg;
  1095. clk_prepare_enable(atxdmac->clk);
  1096. /* Clear pending interrupts. */
  1097. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1098. atchan = &atxdmac->chan[i];
  1099. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1100. cpu_relax();
  1101. }
  1102. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atxdmac->save_gim);
  1103. at_xdmac_write(atxdmac, AT_XDMAC_GE, atxdmac->save_gs);
  1104. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1105. atchan = to_at_xdmac_chan(chan);
  1106. cfg = atchan->cfg[AT_XDMAC_CUR_CFG];
  1107. at_xdmac_chan_write(atchan, AT_XDMAC_CC, cfg);
  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_control = at_xdmac_control;
  1197. atxdmac->dma.device_slave_caps = at_xdmac_device_slave_caps;
  1198. /* Disable all chans and interrupts. */
  1199. at_xdmac_off(atxdmac);
  1200. /* Init channels. */
  1201. INIT_LIST_HEAD(&atxdmac->dma.channels);
  1202. for (i = 0; i < nr_channels; i++) {
  1203. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1204. atchan->chan.device = &atxdmac->dma;
  1205. list_add_tail(&atchan->chan.device_node,
  1206. &atxdmac->dma.channels);
  1207. atchan->ch_regs = at_xdmac_chan_reg_base(atxdmac, i);
  1208. atchan->mask = 1 << i;
  1209. spin_lock_init(&atchan->lock);
  1210. INIT_LIST_HEAD(&atchan->xfers_list);
  1211. INIT_LIST_HEAD(&atchan->free_descs_list);
  1212. tasklet_init(&atchan->tasklet, at_xdmac_tasklet,
  1213. (unsigned long)atchan);
  1214. /* Clear pending interrupts. */
  1215. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1216. cpu_relax();
  1217. }
  1218. platform_set_drvdata(pdev, atxdmac);
  1219. ret = dma_async_device_register(&atxdmac->dma);
  1220. if (ret) {
  1221. dev_err(&pdev->dev, "fail to register DMA engine device\n");
  1222. goto err_clk_disable;
  1223. }
  1224. ret = of_dma_controller_register(pdev->dev.of_node,
  1225. at_xdmac_xlate, atxdmac);
  1226. if (ret) {
  1227. dev_err(&pdev->dev, "could not register of dma controller\n");
  1228. goto err_dma_unregister;
  1229. }
  1230. dev_info(&pdev->dev, "%d channels, mapped at 0x%p\n",
  1231. nr_channels, atxdmac->regs);
  1232. return 0;
  1233. err_dma_unregister:
  1234. dma_async_device_unregister(&atxdmac->dma);
  1235. err_clk_disable:
  1236. clk_disable_unprepare(atxdmac->clk);
  1237. err_free_irq:
  1238. free_irq(atxdmac->irq, atxdmac->dma.dev);
  1239. return ret;
  1240. }
  1241. static int at_xdmac_remove(struct platform_device *pdev)
  1242. {
  1243. struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
  1244. int i;
  1245. at_xdmac_off(atxdmac);
  1246. of_dma_controller_free(pdev->dev.of_node);
  1247. dma_async_device_unregister(&atxdmac->dma);
  1248. clk_disable_unprepare(atxdmac->clk);
  1249. synchronize_irq(atxdmac->irq);
  1250. free_irq(atxdmac->irq, atxdmac->dma.dev);
  1251. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1252. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1253. tasklet_kill(&atchan->tasklet);
  1254. at_xdmac_free_chan_resources(&atchan->chan);
  1255. }
  1256. return 0;
  1257. }
  1258. static const struct dev_pm_ops atmel_xdmac_dev_pm_ops = {
  1259. .prepare = atmel_xdmac_prepare,
  1260. SET_LATE_SYSTEM_SLEEP_PM_OPS(atmel_xdmac_suspend, atmel_xdmac_resume)
  1261. };
  1262. static const struct of_device_id atmel_xdmac_dt_ids[] = {
  1263. {
  1264. .compatible = "atmel,sama5d4-dma",
  1265. }, {
  1266. /* sentinel */
  1267. }
  1268. };
  1269. MODULE_DEVICE_TABLE(of, atmel_xdmac_dt_ids);
  1270. static struct platform_driver at_xdmac_driver = {
  1271. .probe = at_xdmac_probe,
  1272. .remove = at_xdmac_remove,
  1273. .driver = {
  1274. .name = "at_xdmac",
  1275. .owner = THIS_MODULE,
  1276. .of_match_table = of_match_ptr(atmel_xdmac_dt_ids),
  1277. .pm = &atmel_xdmac_dev_pm_ops,
  1278. }
  1279. };
  1280. static int __init at_xdmac_init(void)
  1281. {
  1282. return platform_driver_probe(&at_xdmac_driver, at_xdmac_probe);
  1283. }
  1284. subsys_initcall(at_xdmac_init);
  1285. MODULE_DESCRIPTION("Atmel Extended DMA Controller driver");
  1286. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  1287. MODULE_LICENSE("GPL");