at_xdmac.c 58 KB

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