at_xdmac.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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 & (i) << 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_RESIDUE_MAX_RETRIES 5
  173. #define AT_XDMAC_DMA_BUSWIDTHS\
  174. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) |\
  175. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |\
  176. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |\
  177. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |\
  178. BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
  179. enum atc_status {
  180. AT_XDMAC_CHAN_IS_CYCLIC = 0,
  181. AT_XDMAC_CHAN_IS_PAUSED,
  182. };
  183. /* ----- Channels ----- */
  184. struct at_xdmac_chan {
  185. struct dma_chan chan;
  186. void __iomem *ch_regs;
  187. u32 mask; /* Channel Mask */
  188. u32 cfg; /* Channel Configuration Register */
  189. u8 perid; /* Peripheral ID */
  190. u8 perif; /* Peripheral Interface */
  191. u8 memif; /* Memory Interface */
  192. u32 save_cc;
  193. u32 save_cim;
  194. u32 save_cnda;
  195. u32 save_cndc;
  196. u32 irq_status;
  197. unsigned long status;
  198. struct tasklet_struct tasklet;
  199. struct dma_slave_config sconfig;
  200. spinlock_t lock;
  201. struct list_head xfers_list;
  202. struct list_head free_descs_list;
  203. };
  204. /* ----- Controller ----- */
  205. struct at_xdmac {
  206. struct dma_device dma;
  207. void __iomem *regs;
  208. int irq;
  209. struct clk *clk;
  210. u32 save_gim;
  211. struct dma_pool *at_xdmac_desc_pool;
  212. struct at_xdmac_chan chan[0];
  213. };
  214. /* ----- Descriptors ----- */
  215. /* Linked List Descriptor */
  216. struct at_xdmac_lld {
  217. dma_addr_t mbr_nda; /* Next Descriptor Member */
  218. u32 mbr_ubc; /* Microblock Control Member */
  219. dma_addr_t mbr_sa; /* Source Address Member */
  220. dma_addr_t mbr_da; /* Destination Address Member */
  221. u32 mbr_cfg; /* Configuration Register */
  222. u32 mbr_bc; /* Block Control Register */
  223. u32 mbr_ds; /* Data Stride Register */
  224. u32 mbr_sus; /* Source Microblock Stride Register */
  225. u32 mbr_dus; /* Destination Microblock Stride Register */
  226. };
  227. /* 64-bit alignment needed to update CNDA and CUBC registers in an atomic way. */
  228. struct at_xdmac_desc {
  229. struct at_xdmac_lld lld;
  230. enum dma_transfer_direction direction;
  231. struct dma_async_tx_descriptor tx_dma_desc;
  232. struct list_head desc_node;
  233. /* Following members are only used by the first descriptor */
  234. bool active_xfer;
  235. unsigned int xfer_size;
  236. struct list_head descs_list;
  237. struct list_head xfer_node;
  238. } __aligned(sizeof(u64));
  239. static inline void __iomem *at_xdmac_chan_reg_base(struct at_xdmac *atxdmac, unsigned int chan_nb)
  240. {
  241. return atxdmac->regs + (AT_XDMAC_CHAN_REG_BASE + chan_nb * 0x40);
  242. }
  243. #define at_xdmac_read(atxdmac, reg) readl_relaxed((atxdmac)->regs + (reg))
  244. #define at_xdmac_write(atxdmac, reg, value) \
  245. writel_relaxed((value), (atxdmac)->regs + (reg))
  246. #define at_xdmac_chan_read(atchan, reg) readl_relaxed((atchan)->ch_regs + (reg))
  247. #define at_xdmac_chan_write(atchan, reg, value) writel_relaxed((value), (atchan)->ch_regs + (reg))
  248. static inline struct at_xdmac_chan *to_at_xdmac_chan(struct dma_chan *dchan)
  249. {
  250. return container_of(dchan, struct at_xdmac_chan, chan);
  251. }
  252. static struct device *chan2dev(struct dma_chan *chan)
  253. {
  254. return &chan->dev->device;
  255. }
  256. static inline struct at_xdmac *to_at_xdmac(struct dma_device *ddev)
  257. {
  258. return container_of(ddev, struct at_xdmac, dma);
  259. }
  260. static inline struct at_xdmac_desc *txd_to_at_desc(struct dma_async_tx_descriptor *txd)
  261. {
  262. return container_of(txd, struct at_xdmac_desc, tx_dma_desc);
  263. }
  264. static inline int at_xdmac_chan_is_cyclic(struct at_xdmac_chan *atchan)
  265. {
  266. return test_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  267. }
  268. static inline int at_xdmac_chan_is_paused(struct at_xdmac_chan *atchan)
  269. {
  270. return test_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  271. }
  272. static inline int at_xdmac_csize(u32 maxburst)
  273. {
  274. int csize;
  275. csize = ffs(maxburst) - 1;
  276. if (csize > 4)
  277. csize = -EINVAL;
  278. return csize;
  279. };
  280. static inline u8 at_xdmac_get_dwidth(u32 cfg)
  281. {
  282. return (cfg & AT_XDMAC_CC_DWIDTH_MASK) >> AT_XDMAC_CC_DWIDTH_OFFSET;
  283. };
  284. static unsigned int init_nr_desc_per_channel = 64;
  285. module_param(init_nr_desc_per_channel, uint, 0644);
  286. MODULE_PARM_DESC(init_nr_desc_per_channel,
  287. "initial descriptors per channel (default: 64)");
  288. static bool at_xdmac_chan_is_enabled(struct at_xdmac_chan *atchan)
  289. {
  290. return at_xdmac_chan_read(atchan, AT_XDMAC_GS) & atchan->mask;
  291. }
  292. static void at_xdmac_off(struct at_xdmac *atxdmac)
  293. {
  294. at_xdmac_write(atxdmac, AT_XDMAC_GD, -1L);
  295. /* Wait that all chans are disabled. */
  296. while (at_xdmac_read(atxdmac, AT_XDMAC_GS))
  297. cpu_relax();
  298. at_xdmac_write(atxdmac, AT_XDMAC_GID, -1L);
  299. }
  300. /* Call with lock hold. */
  301. static void at_xdmac_start_xfer(struct at_xdmac_chan *atchan,
  302. struct at_xdmac_desc *first)
  303. {
  304. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  305. u32 reg;
  306. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, first);
  307. if (at_xdmac_chan_is_enabled(atchan))
  308. return;
  309. /* Set transfer as active to not try to start it again. */
  310. first->active_xfer = true;
  311. /* Tell xdmac where to get the first descriptor. */
  312. reg = AT_XDMAC_CNDA_NDA(first->tx_dma_desc.phys)
  313. | AT_XDMAC_CNDA_NDAIF(atchan->memif);
  314. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, reg);
  315. /*
  316. * When doing non cyclic transfer we need to use the next
  317. * descriptor view 2 since some fields of the configuration register
  318. * depend on transfer size and src/dest addresses.
  319. */
  320. if (at_xdmac_chan_is_cyclic(atchan))
  321. reg = AT_XDMAC_CNDC_NDVIEW_NDV1;
  322. else if (first->lld.mbr_ubc & AT_XDMAC_MBR_UBC_NDV3)
  323. reg = AT_XDMAC_CNDC_NDVIEW_NDV3;
  324. else
  325. reg = AT_XDMAC_CNDC_NDVIEW_NDV2;
  326. /*
  327. * Even if the register will be updated from the configuration in the
  328. * descriptor when using view 2 or higher, the PROT bit won't be set
  329. * properly. This bit can be modified only by using the channel
  330. * configuration register.
  331. */
  332. at_xdmac_chan_write(atchan, AT_XDMAC_CC, first->lld.mbr_cfg);
  333. reg |= AT_XDMAC_CNDC_NDDUP
  334. | AT_XDMAC_CNDC_NDSUP
  335. | AT_XDMAC_CNDC_NDE;
  336. at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, reg);
  337. dev_vdbg(chan2dev(&atchan->chan),
  338. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  339. __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  340. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  341. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  342. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  343. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  344. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  345. at_xdmac_chan_write(atchan, AT_XDMAC_CID, 0xffffffff);
  346. reg = AT_XDMAC_CIE_RBEIE | AT_XDMAC_CIE_WBEIE | AT_XDMAC_CIE_ROIE;
  347. /*
  348. * There is no end of list when doing cyclic dma, we need to get
  349. * an interrupt after each periods.
  350. */
  351. if (at_xdmac_chan_is_cyclic(atchan))
  352. at_xdmac_chan_write(atchan, AT_XDMAC_CIE,
  353. reg | AT_XDMAC_CIE_BIE);
  354. else
  355. at_xdmac_chan_write(atchan, AT_XDMAC_CIE,
  356. reg | AT_XDMAC_CIE_LIE);
  357. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atchan->mask);
  358. dev_vdbg(chan2dev(&atchan->chan),
  359. "%s: enable channel (0x%08x)\n", __func__, atchan->mask);
  360. wmb();
  361. at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
  362. dev_vdbg(chan2dev(&atchan->chan),
  363. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  364. __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  365. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  366. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  367. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  368. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  369. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  370. }
  371. static dma_cookie_t at_xdmac_tx_submit(struct dma_async_tx_descriptor *tx)
  372. {
  373. struct at_xdmac_desc *desc = txd_to_at_desc(tx);
  374. struct at_xdmac_chan *atchan = to_at_xdmac_chan(tx->chan);
  375. dma_cookie_t cookie;
  376. unsigned long irqflags;
  377. spin_lock_irqsave(&atchan->lock, irqflags);
  378. cookie = dma_cookie_assign(tx);
  379. dev_vdbg(chan2dev(tx->chan), "%s: atchan 0x%p, add desc 0x%p to xfers_list\n",
  380. __func__, atchan, desc);
  381. list_add_tail(&desc->xfer_node, &atchan->xfers_list);
  382. if (list_is_singular(&atchan->xfers_list))
  383. at_xdmac_start_xfer(atchan, desc);
  384. spin_unlock_irqrestore(&atchan->lock, irqflags);
  385. return cookie;
  386. }
  387. static struct at_xdmac_desc *at_xdmac_alloc_desc(struct dma_chan *chan,
  388. gfp_t gfp_flags)
  389. {
  390. struct at_xdmac_desc *desc;
  391. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  392. dma_addr_t phys;
  393. desc = dma_pool_zalloc(atxdmac->at_xdmac_desc_pool, gfp_flags, &phys);
  394. if (desc) {
  395. INIT_LIST_HEAD(&desc->descs_list);
  396. dma_async_tx_descriptor_init(&desc->tx_dma_desc, chan);
  397. desc->tx_dma_desc.tx_submit = at_xdmac_tx_submit;
  398. desc->tx_dma_desc.phys = phys;
  399. }
  400. return desc;
  401. }
  402. static void at_xdmac_init_used_desc(struct at_xdmac_desc *desc)
  403. {
  404. memset(&desc->lld, 0, sizeof(desc->lld));
  405. INIT_LIST_HEAD(&desc->descs_list);
  406. desc->direction = DMA_TRANS_NONE;
  407. desc->xfer_size = 0;
  408. desc->active_xfer = false;
  409. }
  410. /* Call must be protected by lock. */
  411. static struct at_xdmac_desc *at_xdmac_get_desc(struct at_xdmac_chan *atchan)
  412. {
  413. struct at_xdmac_desc *desc;
  414. if (list_empty(&atchan->free_descs_list)) {
  415. desc = at_xdmac_alloc_desc(&atchan->chan, GFP_NOWAIT);
  416. } else {
  417. desc = list_first_entry(&atchan->free_descs_list,
  418. struct at_xdmac_desc, desc_node);
  419. list_del(&desc->desc_node);
  420. at_xdmac_init_used_desc(desc);
  421. }
  422. return desc;
  423. }
  424. static void at_xdmac_queue_desc(struct dma_chan *chan,
  425. struct at_xdmac_desc *prev,
  426. struct at_xdmac_desc *desc)
  427. {
  428. if (!prev || !desc)
  429. return;
  430. prev->lld.mbr_nda = desc->tx_dma_desc.phys;
  431. prev->lld.mbr_ubc |= AT_XDMAC_MBR_UBC_NDE;
  432. dev_dbg(chan2dev(chan), "%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
  433. __func__, prev, &prev->lld.mbr_nda);
  434. }
  435. static inline void at_xdmac_increment_block_count(struct dma_chan *chan,
  436. struct at_xdmac_desc *desc)
  437. {
  438. if (!desc)
  439. return;
  440. desc->lld.mbr_bc++;
  441. dev_dbg(chan2dev(chan),
  442. "%s: incrementing the block count of the desc 0x%p\n",
  443. __func__, desc);
  444. }
  445. static struct dma_chan *at_xdmac_xlate(struct of_phandle_args *dma_spec,
  446. struct of_dma *of_dma)
  447. {
  448. struct at_xdmac *atxdmac = of_dma->of_dma_data;
  449. struct at_xdmac_chan *atchan;
  450. struct dma_chan *chan;
  451. struct device *dev = atxdmac->dma.dev;
  452. if (dma_spec->args_count != 1) {
  453. dev_err(dev, "dma phandler args: bad number of args\n");
  454. return NULL;
  455. }
  456. chan = dma_get_any_slave_channel(&atxdmac->dma);
  457. if (!chan) {
  458. dev_err(dev, "can't get a dma channel\n");
  459. return NULL;
  460. }
  461. atchan = to_at_xdmac_chan(chan);
  462. atchan->memif = AT91_XDMAC_DT_GET_MEM_IF(dma_spec->args[0]);
  463. atchan->perif = AT91_XDMAC_DT_GET_PER_IF(dma_spec->args[0]);
  464. atchan->perid = AT91_XDMAC_DT_GET_PERID(dma_spec->args[0]);
  465. dev_dbg(dev, "chan dt cfg: memif=%u perif=%u perid=%u\n",
  466. atchan->memif, atchan->perif, atchan->perid);
  467. return chan;
  468. }
  469. static int at_xdmac_compute_chan_conf(struct dma_chan *chan,
  470. enum dma_transfer_direction direction)
  471. {
  472. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  473. int csize, dwidth;
  474. if (direction == DMA_DEV_TO_MEM) {
  475. atchan->cfg =
  476. AT91_XDMAC_DT_PERID(atchan->perid)
  477. | AT_XDMAC_CC_DAM_INCREMENTED_AM
  478. | AT_XDMAC_CC_SAM_FIXED_AM
  479. | AT_XDMAC_CC_DIF(atchan->memif)
  480. | AT_XDMAC_CC_SIF(atchan->perif)
  481. | AT_XDMAC_CC_SWREQ_HWR_CONNECTED
  482. | AT_XDMAC_CC_DSYNC_PER2MEM
  483. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  484. | AT_XDMAC_CC_TYPE_PER_TRAN;
  485. csize = ffs(atchan->sconfig.src_maxburst) - 1;
  486. if (csize < 0) {
  487. dev_err(chan2dev(chan), "invalid src maxburst value\n");
  488. return -EINVAL;
  489. }
  490. atchan->cfg |= AT_XDMAC_CC_CSIZE(csize);
  491. dwidth = ffs(atchan->sconfig.src_addr_width) - 1;
  492. if (dwidth < 0) {
  493. dev_err(chan2dev(chan), "invalid src addr width value\n");
  494. return -EINVAL;
  495. }
  496. atchan->cfg |= AT_XDMAC_CC_DWIDTH(dwidth);
  497. } else if (direction == DMA_MEM_TO_DEV) {
  498. atchan->cfg =
  499. AT91_XDMAC_DT_PERID(atchan->perid)
  500. | AT_XDMAC_CC_DAM_FIXED_AM
  501. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  502. | AT_XDMAC_CC_DIF(atchan->perif)
  503. | AT_XDMAC_CC_SIF(atchan->memif)
  504. | AT_XDMAC_CC_SWREQ_HWR_CONNECTED
  505. | AT_XDMAC_CC_DSYNC_MEM2PER
  506. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  507. | AT_XDMAC_CC_TYPE_PER_TRAN;
  508. csize = ffs(atchan->sconfig.dst_maxburst) - 1;
  509. if (csize < 0) {
  510. dev_err(chan2dev(chan), "invalid src maxburst value\n");
  511. return -EINVAL;
  512. }
  513. atchan->cfg |= AT_XDMAC_CC_CSIZE(csize);
  514. dwidth = ffs(atchan->sconfig.dst_addr_width) - 1;
  515. if (dwidth < 0) {
  516. dev_err(chan2dev(chan), "invalid dst addr width value\n");
  517. return -EINVAL;
  518. }
  519. atchan->cfg |= AT_XDMAC_CC_DWIDTH(dwidth);
  520. }
  521. dev_dbg(chan2dev(chan), "%s: cfg=0x%08x\n", __func__, atchan->cfg);
  522. return 0;
  523. }
  524. /*
  525. * Only check that maxburst and addr width values are supported by the
  526. * the controller but not that the configuration is good to perform the
  527. * transfer since we don't know the direction at this stage.
  528. */
  529. static int at_xdmac_check_slave_config(struct dma_slave_config *sconfig)
  530. {
  531. if ((sconfig->src_maxburst > AT_XDMAC_MAX_CSIZE)
  532. || (sconfig->dst_maxburst > AT_XDMAC_MAX_CSIZE))
  533. return -EINVAL;
  534. if ((sconfig->src_addr_width > AT_XDMAC_MAX_DWIDTH)
  535. || (sconfig->dst_addr_width > AT_XDMAC_MAX_DWIDTH))
  536. return -EINVAL;
  537. return 0;
  538. }
  539. static int at_xdmac_set_slave_config(struct dma_chan *chan,
  540. struct dma_slave_config *sconfig)
  541. {
  542. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  543. if (at_xdmac_check_slave_config(sconfig)) {
  544. dev_err(chan2dev(chan), "invalid slave configuration\n");
  545. return -EINVAL;
  546. }
  547. memcpy(&atchan->sconfig, sconfig, sizeof(atchan->sconfig));
  548. return 0;
  549. }
  550. static struct dma_async_tx_descriptor *
  551. at_xdmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  552. unsigned int sg_len, enum dma_transfer_direction direction,
  553. unsigned long flags, void *context)
  554. {
  555. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  556. struct at_xdmac_desc *first = NULL, *prev = NULL;
  557. struct scatterlist *sg;
  558. int i;
  559. unsigned int xfer_size = 0;
  560. unsigned long irqflags;
  561. struct dma_async_tx_descriptor *ret = NULL;
  562. if (!sgl)
  563. return NULL;
  564. if (!is_slave_direction(direction)) {
  565. dev_err(chan2dev(chan), "invalid DMA direction\n");
  566. return NULL;
  567. }
  568. dev_dbg(chan2dev(chan), "%s: sg_len=%d, dir=%s, flags=0x%lx\n",
  569. __func__, sg_len,
  570. direction == DMA_MEM_TO_DEV ? "to device" : "from device",
  571. flags);
  572. /* Protect dma_sconfig field that can be modified by set_slave_conf. */
  573. spin_lock_irqsave(&atchan->lock, irqflags);
  574. if (at_xdmac_compute_chan_conf(chan, direction))
  575. goto spin_unlock;
  576. /* Prepare descriptors. */
  577. for_each_sg(sgl, sg, sg_len, i) {
  578. struct at_xdmac_desc *desc = NULL;
  579. u32 len, mem, dwidth, fixed_dwidth;
  580. len = sg_dma_len(sg);
  581. mem = sg_dma_address(sg);
  582. if (unlikely(!len)) {
  583. dev_err(chan2dev(chan), "sg data length is zero\n");
  584. goto spin_unlock;
  585. }
  586. dev_dbg(chan2dev(chan), "%s: * sg%d len=%u, mem=0x%08x\n",
  587. __func__, i, len, mem);
  588. desc = at_xdmac_get_desc(atchan);
  589. if (!desc) {
  590. dev_err(chan2dev(chan), "can't get descriptor\n");
  591. if (first)
  592. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  593. goto spin_unlock;
  594. }
  595. /* Linked list descriptor setup. */
  596. if (direction == DMA_DEV_TO_MEM) {
  597. desc->lld.mbr_sa = atchan->sconfig.src_addr;
  598. desc->lld.mbr_da = mem;
  599. } else {
  600. desc->lld.mbr_sa = mem;
  601. desc->lld.mbr_da = atchan->sconfig.dst_addr;
  602. }
  603. dwidth = at_xdmac_get_dwidth(atchan->cfg);
  604. fixed_dwidth = IS_ALIGNED(len, 1 << dwidth)
  605. ? dwidth
  606. : AT_XDMAC_CC_DWIDTH_BYTE;
  607. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2 /* next descriptor view */
  608. | AT_XDMAC_MBR_UBC_NDEN /* next descriptor dst parameter update */
  609. | AT_XDMAC_MBR_UBC_NSEN /* next descriptor src parameter update */
  610. | (len >> fixed_dwidth); /* microblock length */
  611. desc->lld.mbr_cfg = (atchan->cfg & ~AT_XDMAC_CC_DWIDTH_MASK) |
  612. AT_XDMAC_CC_DWIDTH(fixed_dwidth);
  613. dev_dbg(chan2dev(chan),
  614. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  615. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  616. /* Chain lld. */
  617. if (prev)
  618. at_xdmac_queue_desc(chan, prev, desc);
  619. prev = desc;
  620. if (!first)
  621. first = desc;
  622. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  623. __func__, desc, first);
  624. list_add_tail(&desc->desc_node, &first->descs_list);
  625. xfer_size += len;
  626. }
  627. first->tx_dma_desc.flags = flags;
  628. first->xfer_size = xfer_size;
  629. first->direction = direction;
  630. ret = &first->tx_dma_desc;
  631. spin_unlock:
  632. spin_unlock_irqrestore(&atchan->lock, irqflags);
  633. return ret;
  634. }
  635. static struct dma_async_tx_descriptor *
  636. at_xdmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
  637. size_t buf_len, size_t period_len,
  638. enum dma_transfer_direction direction,
  639. unsigned long flags)
  640. {
  641. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  642. struct at_xdmac_desc *first = NULL, *prev = NULL;
  643. unsigned int periods = buf_len / period_len;
  644. int i;
  645. unsigned long irqflags;
  646. dev_dbg(chan2dev(chan), "%s: buf_addr=%pad, buf_len=%zd, period_len=%zd, dir=%s, flags=0x%lx\n",
  647. __func__, &buf_addr, buf_len, period_len,
  648. direction == DMA_MEM_TO_DEV ? "mem2per" : "per2mem", flags);
  649. if (!is_slave_direction(direction)) {
  650. dev_err(chan2dev(chan), "invalid DMA direction\n");
  651. return NULL;
  652. }
  653. if (test_and_set_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status)) {
  654. dev_err(chan2dev(chan), "channel currently used\n");
  655. return NULL;
  656. }
  657. if (at_xdmac_compute_chan_conf(chan, direction))
  658. return NULL;
  659. for (i = 0; i < periods; i++) {
  660. struct at_xdmac_desc *desc = NULL;
  661. spin_lock_irqsave(&atchan->lock, irqflags);
  662. desc = at_xdmac_get_desc(atchan);
  663. if (!desc) {
  664. dev_err(chan2dev(chan), "can't get descriptor\n");
  665. if (first)
  666. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  667. spin_unlock_irqrestore(&atchan->lock, irqflags);
  668. return NULL;
  669. }
  670. spin_unlock_irqrestore(&atchan->lock, irqflags);
  671. dev_dbg(chan2dev(chan),
  672. "%s: desc=0x%p, tx_dma_desc.phys=%pad\n",
  673. __func__, desc, &desc->tx_dma_desc.phys);
  674. if (direction == DMA_DEV_TO_MEM) {
  675. desc->lld.mbr_sa = atchan->sconfig.src_addr;
  676. desc->lld.mbr_da = buf_addr + i * period_len;
  677. } else {
  678. desc->lld.mbr_sa = buf_addr + i * period_len;
  679. desc->lld.mbr_da = atchan->sconfig.dst_addr;
  680. }
  681. desc->lld.mbr_cfg = atchan->cfg;
  682. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV1
  683. | AT_XDMAC_MBR_UBC_NDEN
  684. | AT_XDMAC_MBR_UBC_NSEN
  685. | period_len >> at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  686. dev_dbg(chan2dev(chan),
  687. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
  688. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
  689. /* Chain lld. */
  690. if (prev)
  691. at_xdmac_queue_desc(chan, prev, desc);
  692. prev = desc;
  693. if (!first)
  694. first = desc;
  695. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  696. __func__, desc, first);
  697. list_add_tail(&desc->desc_node, &first->descs_list);
  698. }
  699. at_xdmac_queue_desc(chan, prev, first);
  700. first->tx_dma_desc.flags = flags;
  701. first->xfer_size = buf_len;
  702. first->direction = direction;
  703. return &first->tx_dma_desc;
  704. }
  705. static inline u32 at_xdmac_align_width(struct dma_chan *chan, dma_addr_t addr)
  706. {
  707. u32 width;
  708. /*
  709. * Check address alignment to select the greater data width we
  710. * can use.
  711. *
  712. * Some XDMAC implementations don't provide dword transfer, in
  713. * this case selecting dword has the same behavior as
  714. * selecting word transfers.
  715. */
  716. if (!(addr & 7)) {
  717. width = AT_XDMAC_CC_DWIDTH_DWORD;
  718. dev_dbg(chan2dev(chan), "%s: dwidth: double word\n", __func__);
  719. } else if (!(addr & 3)) {
  720. width = AT_XDMAC_CC_DWIDTH_WORD;
  721. dev_dbg(chan2dev(chan), "%s: dwidth: word\n", __func__);
  722. } else if (!(addr & 1)) {
  723. width = AT_XDMAC_CC_DWIDTH_HALFWORD;
  724. dev_dbg(chan2dev(chan), "%s: dwidth: half word\n", __func__);
  725. } else {
  726. width = AT_XDMAC_CC_DWIDTH_BYTE;
  727. dev_dbg(chan2dev(chan), "%s: dwidth: byte\n", __func__);
  728. }
  729. return width;
  730. }
  731. static struct at_xdmac_desc *
  732. at_xdmac_interleaved_queue_desc(struct dma_chan *chan,
  733. struct at_xdmac_chan *atchan,
  734. struct at_xdmac_desc *prev,
  735. dma_addr_t src, dma_addr_t dst,
  736. struct dma_interleaved_template *xt,
  737. struct data_chunk *chunk)
  738. {
  739. struct at_xdmac_desc *desc;
  740. u32 dwidth;
  741. unsigned long flags;
  742. size_t ublen;
  743. /*
  744. * WARNING: The channel configuration is set here since there is no
  745. * dmaengine_slave_config call in this case. Moreover we don't know the
  746. * direction, it involves we can't dynamically set the source and dest
  747. * interface so we have to use the same one. Only interface 0 allows EBI
  748. * access. Hopefully we can access DDR through both ports (at least on
  749. * SAMA5D4x), so we can use the same interface for source and dest,
  750. * that solves the fact we don't know the direction.
  751. * ERRATA: Even if useless for memory transfers, the PERID has to not
  752. * match the one of another channel. If not, it could lead to spurious
  753. * flag status.
  754. */
  755. u32 chan_cc = AT_XDMAC_CC_PERID(0x3f)
  756. | AT_XDMAC_CC_DIF(0)
  757. | AT_XDMAC_CC_SIF(0)
  758. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  759. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  760. dwidth = at_xdmac_align_width(chan, src | dst | chunk->size);
  761. if (chunk->size >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) {
  762. dev_dbg(chan2dev(chan),
  763. "%s: chunk too big (%zu, max size %lu)...\n",
  764. __func__, chunk->size,
  765. AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth);
  766. return NULL;
  767. }
  768. if (prev)
  769. dev_dbg(chan2dev(chan),
  770. "Adding items at the end of desc 0x%p\n", prev);
  771. if (xt->src_inc) {
  772. if (xt->src_sgl)
  773. chan_cc |= AT_XDMAC_CC_SAM_UBS_AM;
  774. else
  775. chan_cc |= AT_XDMAC_CC_SAM_INCREMENTED_AM;
  776. }
  777. if (xt->dst_inc) {
  778. if (xt->dst_sgl)
  779. chan_cc |= AT_XDMAC_CC_DAM_UBS_AM;
  780. else
  781. chan_cc |= AT_XDMAC_CC_DAM_INCREMENTED_AM;
  782. }
  783. spin_lock_irqsave(&atchan->lock, flags);
  784. desc = at_xdmac_get_desc(atchan);
  785. spin_unlock_irqrestore(&atchan->lock, flags);
  786. if (!desc) {
  787. dev_err(chan2dev(chan), "can't get descriptor\n");
  788. return NULL;
  789. }
  790. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  791. ublen = chunk->size >> dwidth;
  792. desc->lld.mbr_sa = src;
  793. desc->lld.mbr_da = dst;
  794. desc->lld.mbr_sus = dmaengine_get_src_icg(xt, chunk);
  795. desc->lld.mbr_dus = dmaengine_get_dst_icg(xt, chunk);
  796. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3
  797. | AT_XDMAC_MBR_UBC_NDEN
  798. | AT_XDMAC_MBR_UBC_NSEN
  799. | ublen;
  800. desc->lld.mbr_cfg = chan_cc;
  801. dev_dbg(chan2dev(chan),
  802. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  803. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da,
  804. desc->lld.mbr_ubc, desc->lld.mbr_cfg);
  805. /* Chain lld. */
  806. if (prev)
  807. at_xdmac_queue_desc(chan, prev, desc);
  808. return desc;
  809. }
  810. static struct dma_async_tx_descriptor *
  811. at_xdmac_prep_interleaved(struct dma_chan *chan,
  812. struct dma_interleaved_template *xt,
  813. unsigned long flags)
  814. {
  815. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  816. struct at_xdmac_desc *prev = NULL, *first = NULL;
  817. dma_addr_t dst_addr, src_addr;
  818. size_t src_skip = 0, dst_skip = 0, len = 0;
  819. struct data_chunk *chunk;
  820. int i;
  821. if (!xt || !xt->numf || (xt->dir != DMA_MEM_TO_MEM))
  822. return NULL;
  823. /*
  824. * TODO: Handle the case where we have to repeat a chain of
  825. * descriptors...
  826. */
  827. if ((xt->numf > 1) && (xt->frame_size > 1))
  828. return NULL;
  829. dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, numf=%zu, frame_size=%zu, flags=0x%lx\n",
  830. __func__, &xt->src_start, &xt->dst_start, xt->numf,
  831. xt->frame_size, flags);
  832. src_addr = xt->src_start;
  833. dst_addr = xt->dst_start;
  834. if (xt->numf > 1) {
  835. first = at_xdmac_interleaved_queue_desc(chan, atchan,
  836. NULL,
  837. src_addr, dst_addr,
  838. xt, xt->sgl);
  839. /* Length of the block is (BLEN+1) microblocks. */
  840. for (i = 0; i < xt->numf - 1; i++)
  841. at_xdmac_increment_block_count(chan, first);
  842. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  843. __func__, first, first);
  844. list_add_tail(&first->desc_node, &first->descs_list);
  845. } else {
  846. for (i = 0; i < xt->frame_size; i++) {
  847. size_t src_icg = 0, dst_icg = 0;
  848. struct at_xdmac_desc *desc;
  849. chunk = xt->sgl + i;
  850. dst_icg = dmaengine_get_dst_icg(xt, chunk);
  851. src_icg = dmaengine_get_src_icg(xt, chunk);
  852. src_skip = chunk->size + src_icg;
  853. dst_skip = chunk->size + dst_icg;
  854. dev_dbg(chan2dev(chan),
  855. "%s: chunk size=%zu, src icg=%zu, dst icg=%zu\n",
  856. __func__, chunk->size, src_icg, dst_icg);
  857. desc = at_xdmac_interleaved_queue_desc(chan, atchan,
  858. prev,
  859. src_addr, dst_addr,
  860. xt, chunk);
  861. if (!desc) {
  862. list_splice_init(&first->descs_list,
  863. &atchan->free_descs_list);
  864. return NULL;
  865. }
  866. if (!first)
  867. first = desc;
  868. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  869. __func__, desc, first);
  870. list_add_tail(&desc->desc_node, &first->descs_list);
  871. if (xt->src_sgl)
  872. src_addr += src_skip;
  873. if (xt->dst_sgl)
  874. dst_addr += dst_skip;
  875. len += chunk->size;
  876. prev = desc;
  877. }
  878. }
  879. first->tx_dma_desc.cookie = -EBUSY;
  880. first->tx_dma_desc.flags = flags;
  881. first->xfer_size = len;
  882. return &first->tx_dma_desc;
  883. }
  884. static struct dma_async_tx_descriptor *
  885. at_xdmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  886. size_t len, unsigned long flags)
  887. {
  888. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  889. struct at_xdmac_desc *first = NULL, *prev = NULL;
  890. size_t remaining_size = len, xfer_size = 0, ublen;
  891. dma_addr_t src_addr = src, dst_addr = dest;
  892. u32 dwidth;
  893. /*
  894. * WARNING: We don't know the direction, it involves we can't
  895. * dynamically set the source and dest interface so we have to use the
  896. * same one. Only interface 0 allows EBI access. Hopefully we can
  897. * access DDR through both ports (at least on SAMA5D4x), so we can use
  898. * the same interface for source and dest, that solves the fact we
  899. * don't know the direction.
  900. * ERRATA: Even if useless for memory transfers, the PERID has to not
  901. * match the one of another channel. If not, it could lead to spurious
  902. * flag status.
  903. */
  904. u32 chan_cc = AT_XDMAC_CC_PERID(0x3f)
  905. | AT_XDMAC_CC_DAM_INCREMENTED_AM
  906. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  907. | AT_XDMAC_CC_DIF(0)
  908. | AT_XDMAC_CC_SIF(0)
  909. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  910. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  911. unsigned long irqflags;
  912. dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, len=%zd, flags=0x%lx\n",
  913. __func__, &src, &dest, len, flags);
  914. if (unlikely(!len))
  915. return NULL;
  916. dwidth = at_xdmac_align_width(chan, src_addr | dst_addr);
  917. /* Prepare descriptors. */
  918. while (remaining_size) {
  919. struct at_xdmac_desc *desc = NULL;
  920. dev_dbg(chan2dev(chan), "%s: remaining_size=%zu\n", __func__, remaining_size);
  921. spin_lock_irqsave(&atchan->lock, irqflags);
  922. desc = at_xdmac_get_desc(atchan);
  923. spin_unlock_irqrestore(&atchan->lock, irqflags);
  924. if (!desc) {
  925. dev_err(chan2dev(chan), "can't get descriptor\n");
  926. if (first)
  927. list_splice_init(&first->descs_list, &atchan->free_descs_list);
  928. return NULL;
  929. }
  930. /* Update src and dest addresses. */
  931. src_addr += xfer_size;
  932. dst_addr += xfer_size;
  933. if (remaining_size >= AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)
  934. xfer_size = AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth;
  935. else
  936. xfer_size = remaining_size;
  937. dev_dbg(chan2dev(chan), "%s: xfer_size=%zu\n", __func__, xfer_size);
  938. /* Check remaining length and change data width if needed. */
  939. dwidth = at_xdmac_align_width(chan,
  940. src_addr | dst_addr | xfer_size);
  941. chan_cc &= ~AT_XDMAC_CC_DWIDTH_MASK;
  942. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  943. ublen = xfer_size >> dwidth;
  944. remaining_size -= xfer_size;
  945. desc->lld.mbr_sa = src_addr;
  946. desc->lld.mbr_da = dst_addr;
  947. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2
  948. | AT_XDMAC_MBR_UBC_NDEN
  949. | AT_XDMAC_MBR_UBC_NSEN
  950. | ublen;
  951. desc->lld.mbr_cfg = chan_cc;
  952. dev_dbg(chan2dev(chan),
  953. "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  954. __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc, desc->lld.mbr_cfg);
  955. /* Chain lld. */
  956. if (prev)
  957. at_xdmac_queue_desc(chan, prev, desc);
  958. prev = desc;
  959. if (!first)
  960. first = desc;
  961. dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
  962. __func__, desc, first);
  963. list_add_tail(&desc->desc_node, &first->descs_list);
  964. }
  965. first->tx_dma_desc.flags = flags;
  966. first->xfer_size = len;
  967. return &first->tx_dma_desc;
  968. }
  969. static struct at_xdmac_desc *at_xdmac_memset_create_desc(struct dma_chan *chan,
  970. struct at_xdmac_chan *atchan,
  971. dma_addr_t dst_addr,
  972. size_t len,
  973. int value)
  974. {
  975. struct at_xdmac_desc *desc;
  976. unsigned long flags;
  977. size_t ublen;
  978. u32 dwidth;
  979. /*
  980. * WARNING: The channel configuration is set here since there is no
  981. * dmaengine_slave_config call in this case. Moreover we don't know the
  982. * direction, it involves we can't dynamically set the source and dest
  983. * interface so we have to use the same one. Only interface 0 allows EBI
  984. * access. Hopefully we can access DDR through both ports (at least on
  985. * SAMA5D4x), so we can use the same interface for source and dest,
  986. * that solves the fact we don't know the direction.
  987. * ERRATA: Even if useless for memory transfers, the PERID has to not
  988. * match the one of another channel. If not, it could lead to spurious
  989. * flag status.
  990. */
  991. u32 chan_cc = AT_XDMAC_CC_PERID(0x3f)
  992. | AT_XDMAC_CC_DAM_UBS_AM
  993. | AT_XDMAC_CC_SAM_INCREMENTED_AM
  994. | AT_XDMAC_CC_DIF(0)
  995. | AT_XDMAC_CC_SIF(0)
  996. | AT_XDMAC_CC_MBSIZE_SIXTEEN
  997. | AT_XDMAC_CC_MEMSET_HW_MODE
  998. | AT_XDMAC_CC_TYPE_MEM_TRAN;
  999. dwidth = at_xdmac_align_width(chan, dst_addr);
  1000. if (len >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) {
  1001. dev_err(chan2dev(chan),
  1002. "%s: Transfer too large, aborting...\n",
  1003. __func__);
  1004. return NULL;
  1005. }
  1006. spin_lock_irqsave(&atchan->lock, flags);
  1007. desc = at_xdmac_get_desc(atchan);
  1008. spin_unlock_irqrestore(&atchan->lock, flags);
  1009. if (!desc) {
  1010. dev_err(chan2dev(chan), "can't get descriptor\n");
  1011. return NULL;
  1012. }
  1013. chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
  1014. ublen = len >> dwidth;
  1015. desc->lld.mbr_da = dst_addr;
  1016. desc->lld.mbr_ds = value;
  1017. desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3
  1018. | AT_XDMAC_MBR_UBC_NDEN
  1019. | AT_XDMAC_MBR_UBC_NSEN
  1020. | ublen;
  1021. desc->lld.mbr_cfg = chan_cc;
  1022. dev_dbg(chan2dev(chan),
  1023. "%s: lld: mbr_da=%pad, mbr_ds=0x%08x, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
  1024. __func__, &desc->lld.mbr_da, desc->lld.mbr_ds, desc->lld.mbr_ubc,
  1025. desc->lld.mbr_cfg);
  1026. return desc;
  1027. }
  1028. static struct dma_async_tx_descriptor *
  1029. at_xdmac_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
  1030. size_t len, unsigned long flags)
  1031. {
  1032. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1033. struct at_xdmac_desc *desc;
  1034. dev_dbg(chan2dev(chan), "%s: dest=%pad, len=%zu, pattern=0x%x, flags=0x%lx\n",
  1035. __func__, &dest, len, value, flags);
  1036. if (unlikely(!len))
  1037. return NULL;
  1038. desc = at_xdmac_memset_create_desc(chan, atchan, dest, len, value);
  1039. list_add_tail(&desc->desc_node, &desc->descs_list);
  1040. desc->tx_dma_desc.cookie = -EBUSY;
  1041. desc->tx_dma_desc.flags = flags;
  1042. desc->xfer_size = len;
  1043. return &desc->tx_dma_desc;
  1044. }
  1045. static struct dma_async_tx_descriptor *
  1046. at_xdmac_prep_dma_memset_sg(struct dma_chan *chan, struct scatterlist *sgl,
  1047. unsigned int sg_len, int value,
  1048. unsigned long flags)
  1049. {
  1050. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1051. struct at_xdmac_desc *desc, *pdesc = NULL,
  1052. *ppdesc = NULL, *first = NULL;
  1053. struct scatterlist *sg, *psg = NULL, *ppsg = NULL;
  1054. size_t stride = 0, pstride = 0, len = 0;
  1055. int i;
  1056. if (!sgl)
  1057. return NULL;
  1058. dev_dbg(chan2dev(chan), "%s: sg_len=%d, value=0x%x, flags=0x%lx\n",
  1059. __func__, sg_len, value, flags);
  1060. /* Prepare descriptors. */
  1061. for_each_sg(sgl, sg, sg_len, i) {
  1062. dev_dbg(chan2dev(chan), "%s: dest=%pad, len=%d, pattern=0x%x, flags=0x%lx\n",
  1063. __func__, &sg_dma_address(sg), sg_dma_len(sg),
  1064. value, flags);
  1065. desc = at_xdmac_memset_create_desc(chan, atchan,
  1066. sg_dma_address(sg),
  1067. sg_dma_len(sg),
  1068. value);
  1069. if (!desc && first)
  1070. list_splice_init(&first->descs_list,
  1071. &atchan->free_descs_list);
  1072. if (!first)
  1073. first = desc;
  1074. /* Update our strides */
  1075. pstride = stride;
  1076. if (psg)
  1077. stride = sg_dma_address(sg) -
  1078. (sg_dma_address(psg) + sg_dma_len(psg));
  1079. /*
  1080. * The scatterlist API gives us only the address and
  1081. * length of each elements.
  1082. *
  1083. * Unfortunately, we don't have the stride, which we
  1084. * will need to compute.
  1085. *
  1086. * That make us end up in a situation like this one:
  1087. * len stride len stride len
  1088. * +-------+ +-------+ +-------+
  1089. * | N-2 | | N-1 | | N |
  1090. * +-------+ +-------+ +-------+
  1091. *
  1092. * We need all these three elements (N-2, N-1 and N)
  1093. * to actually take the decision on whether we need to
  1094. * queue N-1 or reuse N-2.
  1095. *
  1096. * We will only consider N if it is the last element.
  1097. */
  1098. if (ppdesc && pdesc) {
  1099. if ((stride == pstride) &&
  1100. (sg_dma_len(ppsg) == sg_dma_len(psg))) {
  1101. dev_dbg(chan2dev(chan),
  1102. "%s: desc 0x%p can be merged with desc 0x%p\n",
  1103. __func__, pdesc, ppdesc);
  1104. /*
  1105. * Increment the block count of the
  1106. * N-2 descriptor
  1107. */
  1108. at_xdmac_increment_block_count(chan, ppdesc);
  1109. ppdesc->lld.mbr_dus = stride;
  1110. /*
  1111. * Put back the N-1 descriptor in the
  1112. * free descriptor list
  1113. */
  1114. list_add_tail(&pdesc->desc_node,
  1115. &atchan->free_descs_list);
  1116. /*
  1117. * Make our N-1 descriptor pointer
  1118. * point to the N-2 since they were
  1119. * actually merged.
  1120. */
  1121. pdesc = ppdesc;
  1122. /*
  1123. * Rule out the case where we don't have
  1124. * pstride computed yet (our second sg
  1125. * element)
  1126. *
  1127. * We also want to catch the case where there
  1128. * would be a negative stride,
  1129. */
  1130. } else if (pstride ||
  1131. sg_dma_address(sg) < sg_dma_address(psg)) {
  1132. /*
  1133. * Queue the N-1 descriptor after the
  1134. * N-2
  1135. */
  1136. at_xdmac_queue_desc(chan, ppdesc, pdesc);
  1137. /*
  1138. * Add the N-1 descriptor to the list
  1139. * of the descriptors used for this
  1140. * transfer
  1141. */
  1142. list_add_tail(&desc->desc_node,
  1143. &first->descs_list);
  1144. dev_dbg(chan2dev(chan),
  1145. "%s: add desc 0x%p to descs_list 0x%p\n",
  1146. __func__, desc, first);
  1147. }
  1148. }
  1149. /*
  1150. * If we are the last element, just see if we have the
  1151. * same size than the previous element.
  1152. *
  1153. * If so, we can merge it with the previous descriptor
  1154. * since we don't care about the stride anymore.
  1155. */
  1156. if ((i == (sg_len - 1)) &&
  1157. sg_dma_len(psg) == sg_dma_len(sg)) {
  1158. dev_dbg(chan2dev(chan),
  1159. "%s: desc 0x%p can be merged with desc 0x%p\n",
  1160. __func__, desc, pdesc);
  1161. /*
  1162. * Increment the block count of the N-1
  1163. * descriptor
  1164. */
  1165. at_xdmac_increment_block_count(chan, pdesc);
  1166. pdesc->lld.mbr_dus = stride;
  1167. /*
  1168. * Put back the N descriptor in the free
  1169. * descriptor list
  1170. */
  1171. list_add_tail(&desc->desc_node,
  1172. &atchan->free_descs_list);
  1173. }
  1174. /* Update our descriptors */
  1175. ppdesc = pdesc;
  1176. pdesc = desc;
  1177. /* Update our scatter pointers */
  1178. ppsg = psg;
  1179. psg = sg;
  1180. len += sg_dma_len(sg);
  1181. }
  1182. first->tx_dma_desc.cookie = -EBUSY;
  1183. first->tx_dma_desc.flags = flags;
  1184. first->xfer_size = len;
  1185. return &first->tx_dma_desc;
  1186. }
  1187. static enum dma_status
  1188. at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  1189. struct dma_tx_state *txstate)
  1190. {
  1191. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1192. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1193. struct at_xdmac_desc *desc, *_desc;
  1194. struct list_head *descs_list;
  1195. enum dma_status ret;
  1196. int residue, retry;
  1197. u32 cur_nda, check_nda, cur_ubc, mask, value;
  1198. u8 dwidth = 0;
  1199. unsigned long flags;
  1200. bool initd;
  1201. ret = dma_cookie_status(chan, cookie, txstate);
  1202. if (ret == DMA_COMPLETE)
  1203. return ret;
  1204. if (!txstate)
  1205. return ret;
  1206. spin_lock_irqsave(&atchan->lock, flags);
  1207. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  1208. /*
  1209. * If the transfer has not been started yet, don't need to compute the
  1210. * residue, it's the transfer length.
  1211. */
  1212. if (!desc->active_xfer) {
  1213. dma_set_residue(txstate, desc->xfer_size);
  1214. goto spin_unlock;
  1215. }
  1216. residue = desc->xfer_size;
  1217. /*
  1218. * Flush FIFO: only relevant when the transfer is source peripheral
  1219. * synchronized. Flush is needed before reading CUBC because data in
  1220. * the FIFO are not reported by CUBC. Reporting a residue of the
  1221. * transfer length while we have data in FIFO can cause issue.
  1222. * Usecase: atmel USART has a timeout which means I have received
  1223. * characters but there is no more character received for a while. On
  1224. * timeout, it requests the residue. If the data are in the DMA FIFO,
  1225. * we will return a residue of the transfer length. It means no data
  1226. * received. If an application is waiting for these data, it will hang
  1227. * since we won't have another USART timeout without receiving new
  1228. * data.
  1229. */
  1230. mask = AT_XDMAC_CC_TYPE | AT_XDMAC_CC_DSYNC;
  1231. value = AT_XDMAC_CC_TYPE_PER_TRAN | AT_XDMAC_CC_DSYNC_PER2MEM;
  1232. if ((desc->lld.mbr_cfg & mask) == value) {
  1233. at_xdmac_write(atxdmac, AT_XDMAC_GSWF, atchan->mask);
  1234. while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
  1235. cpu_relax();
  1236. }
  1237. /*
  1238. * The easiest way to compute the residue should be to pause the DMA
  1239. * but doing this can lead to miss some data as some devices don't
  1240. * have FIFO.
  1241. * We need to read several registers because:
  1242. * - DMA is running therefore a descriptor change is possible while
  1243. * reading these registers
  1244. * - When the block transfer is done, the value of the CUBC register
  1245. * is set to its initial value until the fetch of the next descriptor.
  1246. * This value will corrupt the residue calculation so we have to skip
  1247. * it.
  1248. *
  1249. * INITD -------- ------------
  1250. * |____________________|
  1251. * _______________________ _______________
  1252. * NDA @desc2 \/ @desc3
  1253. * _______________________/\_______________
  1254. * __________ ___________ _______________
  1255. * CUBC 0 \/ MAX desc1 \/ MAX desc2
  1256. * __________/\___________/\_______________
  1257. *
  1258. * Since descriptors are aligned on 64 bits, we can assume that
  1259. * the update of NDA and CUBC is atomic.
  1260. * Memory barriers are used to ensure the read order of the registers.
  1261. * A max number of retries is set because unlikely it could never ends.
  1262. */
  1263. for (retry = 0; retry < AT_XDMAC_RESIDUE_MAX_RETRIES; retry++) {
  1264. check_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  1265. rmb();
  1266. cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC);
  1267. rmb();
  1268. initd = !!(at_xdmac_chan_read(atchan, AT_XDMAC_CC) & AT_XDMAC_CC_INITD);
  1269. rmb();
  1270. cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
  1271. rmb();
  1272. if ((check_nda == cur_nda) && initd)
  1273. break;
  1274. }
  1275. if (unlikely(retry >= AT_XDMAC_RESIDUE_MAX_RETRIES)) {
  1276. ret = DMA_ERROR;
  1277. goto spin_unlock;
  1278. }
  1279. /*
  1280. * Flush FIFO: only relevant when the transfer is source peripheral
  1281. * synchronized. Another flush is needed here because CUBC is updated
  1282. * when the controller sends the data write command. It can lead to
  1283. * report data that are not written in the memory or the device. The
  1284. * FIFO flush ensures that data are really written.
  1285. */
  1286. if ((desc->lld.mbr_cfg & mask) == value) {
  1287. at_xdmac_write(atxdmac, AT_XDMAC_GSWF, atchan->mask);
  1288. while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
  1289. cpu_relax();
  1290. }
  1291. /*
  1292. * Remove size of all microblocks already transferred and the current
  1293. * one. Then add the remaining size to transfer of the current
  1294. * microblock.
  1295. */
  1296. descs_list = &desc->descs_list;
  1297. list_for_each_entry_safe(desc, _desc, descs_list, desc_node) {
  1298. dwidth = at_xdmac_get_dwidth(desc->lld.mbr_cfg);
  1299. residue -= (desc->lld.mbr_ubc & 0xffffff) << dwidth;
  1300. if ((desc->lld.mbr_nda & 0xfffffffc) == cur_nda)
  1301. break;
  1302. }
  1303. residue += cur_ubc << dwidth;
  1304. dma_set_residue(txstate, residue);
  1305. dev_dbg(chan2dev(chan),
  1306. "%s: desc=0x%p, tx_dma_desc.phys=%pad, tx_status=%d, cookie=%d, residue=%d\n",
  1307. __func__, desc, &desc->tx_dma_desc.phys, ret, cookie, residue);
  1308. spin_unlock:
  1309. spin_unlock_irqrestore(&atchan->lock, flags);
  1310. return ret;
  1311. }
  1312. /* Call must be protected by lock. */
  1313. static void at_xdmac_remove_xfer(struct at_xdmac_chan *atchan,
  1314. struct at_xdmac_desc *desc)
  1315. {
  1316. dev_dbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1317. /*
  1318. * Remove the transfer from the transfer list then move the transfer
  1319. * descriptors into the free descriptors list.
  1320. */
  1321. list_del(&desc->xfer_node);
  1322. list_splice_init(&desc->descs_list, &atchan->free_descs_list);
  1323. }
  1324. static void at_xdmac_advance_work(struct at_xdmac_chan *atchan)
  1325. {
  1326. struct at_xdmac_desc *desc;
  1327. unsigned long flags;
  1328. spin_lock_irqsave(&atchan->lock, flags);
  1329. /*
  1330. * If channel is enabled, do nothing, advance_work will be triggered
  1331. * after the interruption.
  1332. */
  1333. if (!at_xdmac_chan_is_enabled(atchan) && !list_empty(&atchan->xfers_list)) {
  1334. desc = list_first_entry(&atchan->xfers_list,
  1335. struct at_xdmac_desc,
  1336. xfer_node);
  1337. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1338. if (!desc->active_xfer)
  1339. at_xdmac_start_xfer(atchan, desc);
  1340. }
  1341. spin_unlock_irqrestore(&atchan->lock, flags);
  1342. }
  1343. static void at_xdmac_handle_cyclic(struct at_xdmac_chan *atchan)
  1344. {
  1345. struct at_xdmac_desc *desc;
  1346. struct dma_async_tx_descriptor *txd;
  1347. desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
  1348. txd = &desc->tx_dma_desc;
  1349. if (txd->flags & DMA_PREP_INTERRUPT)
  1350. dmaengine_desc_get_callback_invoke(txd, NULL);
  1351. }
  1352. static void at_xdmac_tasklet(unsigned long data)
  1353. {
  1354. struct at_xdmac_chan *atchan = (struct at_xdmac_chan *)data;
  1355. struct at_xdmac_desc *desc;
  1356. u32 error_mask;
  1357. dev_dbg(chan2dev(&atchan->chan), "%s: status=0x%08x\n",
  1358. __func__, atchan->irq_status);
  1359. error_mask = AT_XDMAC_CIS_RBEIS
  1360. | AT_XDMAC_CIS_WBEIS
  1361. | AT_XDMAC_CIS_ROIS;
  1362. if (at_xdmac_chan_is_cyclic(atchan)) {
  1363. at_xdmac_handle_cyclic(atchan);
  1364. } else if ((atchan->irq_status & AT_XDMAC_CIS_LIS)
  1365. || (atchan->irq_status & error_mask)) {
  1366. struct dma_async_tx_descriptor *txd;
  1367. if (atchan->irq_status & AT_XDMAC_CIS_RBEIS)
  1368. dev_err(chan2dev(&atchan->chan), "read bus error!!!");
  1369. if (atchan->irq_status & AT_XDMAC_CIS_WBEIS)
  1370. dev_err(chan2dev(&atchan->chan), "write bus error!!!");
  1371. if (atchan->irq_status & AT_XDMAC_CIS_ROIS)
  1372. dev_err(chan2dev(&atchan->chan), "request overflow error!!!");
  1373. spin_lock_bh(&atchan->lock);
  1374. desc = list_first_entry(&atchan->xfers_list,
  1375. struct at_xdmac_desc,
  1376. xfer_node);
  1377. dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
  1378. if (!desc->active_xfer) {
  1379. dev_err(chan2dev(&atchan->chan), "Xfer not active: exiting");
  1380. spin_unlock(&atchan->lock);
  1381. return;
  1382. }
  1383. txd = &desc->tx_dma_desc;
  1384. at_xdmac_remove_xfer(atchan, desc);
  1385. spin_unlock_bh(&atchan->lock);
  1386. if (!at_xdmac_chan_is_cyclic(atchan)) {
  1387. dma_cookie_complete(txd);
  1388. if (txd->flags & DMA_PREP_INTERRUPT)
  1389. dmaengine_desc_get_callback_invoke(txd, NULL);
  1390. }
  1391. dma_run_dependencies(txd);
  1392. at_xdmac_advance_work(atchan);
  1393. }
  1394. }
  1395. static irqreturn_t at_xdmac_interrupt(int irq, void *dev_id)
  1396. {
  1397. struct at_xdmac *atxdmac = (struct at_xdmac *)dev_id;
  1398. struct at_xdmac_chan *atchan;
  1399. u32 imr, status, pending;
  1400. u32 chan_imr, chan_status;
  1401. int i, ret = IRQ_NONE;
  1402. do {
  1403. imr = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1404. status = at_xdmac_read(atxdmac, AT_XDMAC_GIS);
  1405. pending = status & imr;
  1406. dev_vdbg(atxdmac->dma.dev,
  1407. "%s: status=0x%08x, imr=0x%08x, pending=0x%08x\n",
  1408. __func__, status, imr, pending);
  1409. if (!pending)
  1410. break;
  1411. /* We have to find which channel has generated the interrupt. */
  1412. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1413. if (!((1 << i) & pending))
  1414. continue;
  1415. atchan = &atxdmac->chan[i];
  1416. chan_imr = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1417. chan_status = at_xdmac_chan_read(atchan, AT_XDMAC_CIS);
  1418. atchan->irq_status = chan_status & chan_imr;
  1419. dev_vdbg(atxdmac->dma.dev,
  1420. "%s: chan%d: imr=0x%x, status=0x%x\n",
  1421. __func__, i, chan_imr, chan_status);
  1422. dev_vdbg(chan2dev(&atchan->chan),
  1423. "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
  1424. __func__,
  1425. at_xdmac_chan_read(atchan, AT_XDMAC_CC),
  1426. at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
  1427. at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
  1428. at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
  1429. at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
  1430. at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
  1431. if (atchan->irq_status & (AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS))
  1432. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  1433. tasklet_schedule(&atchan->tasklet);
  1434. ret = IRQ_HANDLED;
  1435. }
  1436. } while (pending);
  1437. return ret;
  1438. }
  1439. static void at_xdmac_issue_pending(struct dma_chan *chan)
  1440. {
  1441. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1442. dev_dbg(chan2dev(&atchan->chan), "%s\n", __func__);
  1443. if (!at_xdmac_chan_is_cyclic(atchan))
  1444. at_xdmac_advance_work(atchan);
  1445. return;
  1446. }
  1447. static int at_xdmac_device_config(struct dma_chan *chan,
  1448. struct dma_slave_config *config)
  1449. {
  1450. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1451. int ret;
  1452. unsigned long flags;
  1453. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1454. spin_lock_irqsave(&atchan->lock, flags);
  1455. ret = at_xdmac_set_slave_config(chan, config);
  1456. spin_unlock_irqrestore(&atchan->lock, flags);
  1457. return ret;
  1458. }
  1459. static int at_xdmac_device_pause(struct dma_chan *chan)
  1460. {
  1461. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1462. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1463. unsigned long flags;
  1464. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1465. if (test_and_set_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status))
  1466. return 0;
  1467. spin_lock_irqsave(&atchan->lock, flags);
  1468. at_xdmac_write(atxdmac, AT_XDMAC_GRWS, atchan->mask);
  1469. while (at_xdmac_chan_read(atchan, AT_XDMAC_CC)
  1470. & (AT_XDMAC_CC_WRIP | AT_XDMAC_CC_RDIP))
  1471. cpu_relax();
  1472. spin_unlock_irqrestore(&atchan->lock, flags);
  1473. return 0;
  1474. }
  1475. static int at_xdmac_device_resume(struct dma_chan *chan)
  1476. {
  1477. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1478. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1479. unsigned long flags;
  1480. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1481. spin_lock_irqsave(&atchan->lock, flags);
  1482. if (!at_xdmac_chan_is_paused(atchan)) {
  1483. spin_unlock_irqrestore(&atchan->lock, flags);
  1484. return 0;
  1485. }
  1486. at_xdmac_write(atxdmac, AT_XDMAC_GRWR, atchan->mask);
  1487. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  1488. spin_unlock_irqrestore(&atchan->lock, flags);
  1489. return 0;
  1490. }
  1491. static int at_xdmac_device_terminate_all(struct dma_chan *chan)
  1492. {
  1493. struct at_xdmac_desc *desc, *_desc;
  1494. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1495. struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
  1496. unsigned long flags;
  1497. dev_dbg(chan2dev(chan), "%s\n", __func__);
  1498. spin_lock_irqsave(&atchan->lock, flags);
  1499. at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
  1500. while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
  1501. cpu_relax();
  1502. /* Cancel all pending transfers. */
  1503. list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node)
  1504. at_xdmac_remove_xfer(atchan, desc);
  1505. clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
  1506. clear_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
  1507. spin_unlock_irqrestore(&atchan->lock, flags);
  1508. return 0;
  1509. }
  1510. static int at_xdmac_alloc_chan_resources(struct dma_chan *chan)
  1511. {
  1512. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1513. struct at_xdmac_desc *desc;
  1514. int i;
  1515. unsigned long flags;
  1516. spin_lock_irqsave(&atchan->lock, flags);
  1517. if (at_xdmac_chan_is_enabled(atchan)) {
  1518. dev_err(chan2dev(chan),
  1519. "can't allocate channel resources (channel enabled)\n");
  1520. i = -EIO;
  1521. goto spin_unlock;
  1522. }
  1523. if (!list_empty(&atchan->free_descs_list)) {
  1524. dev_err(chan2dev(chan),
  1525. "can't allocate channel resources (channel not free from a previous use)\n");
  1526. i = -EIO;
  1527. goto spin_unlock;
  1528. }
  1529. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1530. desc = at_xdmac_alloc_desc(chan, GFP_ATOMIC);
  1531. if (!desc) {
  1532. dev_warn(chan2dev(chan),
  1533. "only %d descriptors have been allocated\n", i);
  1534. break;
  1535. }
  1536. list_add_tail(&desc->desc_node, &atchan->free_descs_list);
  1537. }
  1538. dma_cookie_init(chan);
  1539. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  1540. spin_unlock:
  1541. spin_unlock_irqrestore(&atchan->lock, flags);
  1542. return i;
  1543. }
  1544. static void at_xdmac_free_chan_resources(struct dma_chan *chan)
  1545. {
  1546. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1547. struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
  1548. struct at_xdmac_desc *desc, *_desc;
  1549. list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) {
  1550. dev_dbg(chan2dev(chan), "%s: freeing descriptor %p\n", __func__, desc);
  1551. list_del(&desc->desc_node);
  1552. dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys);
  1553. }
  1554. return;
  1555. }
  1556. #ifdef CONFIG_PM
  1557. static int atmel_xdmac_prepare(struct device *dev)
  1558. {
  1559. struct at_xdmac *atxdmac = dev_get_drvdata(dev);
  1560. struct dma_chan *chan, *_chan;
  1561. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1562. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1563. /* Wait for transfer completion, except in cyclic case. */
  1564. if (at_xdmac_chan_is_enabled(atchan) && !at_xdmac_chan_is_cyclic(atchan))
  1565. return -EAGAIN;
  1566. }
  1567. return 0;
  1568. }
  1569. #else
  1570. # define atmel_xdmac_prepare NULL
  1571. #endif
  1572. #ifdef CONFIG_PM_SLEEP
  1573. static int atmel_xdmac_suspend(struct device *dev)
  1574. {
  1575. struct at_xdmac *atxdmac = dev_get_drvdata(dev);
  1576. struct dma_chan *chan, *_chan;
  1577. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1578. struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
  1579. atchan->save_cc = at_xdmac_chan_read(atchan, AT_XDMAC_CC);
  1580. if (at_xdmac_chan_is_cyclic(atchan)) {
  1581. if (!at_xdmac_chan_is_paused(atchan))
  1582. at_xdmac_device_pause(chan);
  1583. atchan->save_cim = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
  1584. atchan->save_cnda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA);
  1585. atchan->save_cndc = at_xdmac_chan_read(atchan, AT_XDMAC_CNDC);
  1586. }
  1587. }
  1588. atxdmac->save_gim = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
  1589. at_xdmac_off(atxdmac);
  1590. clk_disable_unprepare(atxdmac->clk);
  1591. return 0;
  1592. }
  1593. static int atmel_xdmac_resume(struct device *dev)
  1594. {
  1595. struct at_xdmac *atxdmac = dev_get_drvdata(dev);
  1596. struct at_xdmac_chan *atchan;
  1597. struct dma_chan *chan, *_chan;
  1598. int i;
  1599. int ret;
  1600. ret = clk_prepare_enable(atxdmac->clk);
  1601. if (ret)
  1602. return ret;
  1603. /* Clear pending interrupts. */
  1604. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1605. atchan = &atxdmac->chan[i];
  1606. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1607. cpu_relax();
  1608. }
  1609. at_xdmac_write(atxdmac, AT_XDMAC_GIE, atxdmac->save_gim);
  1610. list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
  1611. atchan = to_at_xdmac_chan(chan);
  1612. at_xdmac_chan_write(atchan, AT_XDMAC_CC, atchan->save_cc);
  1613. if (at_xdmac_chan_is_cyclic(atchan)) {
  1614. if (at_xdmac_chan_is_paused(atchan))
  1615. at_xdmac_device_resume(chan);
  1616. at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, atchan->save_cnda);
  1617. at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, atchan->save_cndc);
  1618. at_xdmac_chan_write(atchan, AT_XDMAC_CIE, atchan->save_cim);
  1619. wmb();
  1620. at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
  1621. }
  1622. }
  1623. return 0;
  1624. }
  1625. #endif /* CONFIG_PM_SLEEP */
  1626. static int at_xdmac_probe(struct platform_device *pdev)
  1627. {
  1628. struct resource *res;
  1629. struct at_xdmac *atxdmac;
  1630. int irq, size, nr_channels, i, ret;
  1631. void __iomem *base;
  1632. u32 reg;
  1633. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1634. if (!res)
  1635. return -EINVAL;
  1636. irq = platform_get_irq(pdev, 0);
  1637. if (irq < 0)
  1638. return irq;
  1639. base = devm_ioremap_resource(&pdev->dev, res);
  1640. if (IS_ERR(base))
  1641. return PTR_ERR(base);
  1642. /*
  1643. * Read number of xdmac channels, read helper function can't be used
  1644. * since atxdmac is not yet allocated and we need to know the number
  1645. * of channels to do the allocation.
  1646. */
  1647. reg = readl_relaxed(base + AT_XDMAC_GTYPE);
  1648. nr_channels = AT_XDMAC_NB_CH(reg);
  1649. if (nr_channels > AT_XDMAC_MAX_CHAN) {
  1650. dev_err(&pdev->dev, "invalid number of channels (%u)\n",
  1651. nr_channels);
  1652. return -EINVAL;
  1653. }
  1654. size = sizeof(*atxdmac);
  1655. size += nr_channels * sizeof(struct at_xdmac_chan);
  1656. atxdmac = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1657. if (!atxdmac) {
  1658. dev_err(&pdev->dev, "can't allocate at_xdmac structure\n");
  1659. return -ENOMEM;
  1660. }
  1661. atxdmac->regs = base;
  1662. atxdmac->irq = irq;
  1663. atxdmac->clk = devm_clk_get(&pdev->dev, "dma_clk");
  1664. if (IS_ERR(atxdmac->clk)) {
  1665. dev_err(&pdev->dev, "can't get dma_clk\n");
  1666. return PTR_ERR(atxdmac->clk);
  1667. }
  1668. /* Do not use dev res to prevent races with tasklet */
  1669. ret = request_irq(atxdmac->irq, at_xdmac_interrupt, 0, "at_xdmac", atxdmac);
  1670. if (ret) {
  1671. dev_err(&pdev->dev, "can't request irq\n");
  1672. return ret;
  1673. }
  1674. ret = clk_prepare_enable(atxdmac->clk);
  1675. if (ret) {
  1676. dev_err(&pdev->dev, "can't prepare or enable clock\n");
  1677. goto err_free_irq;
  1678. }
  1679. atxdmac->at_xdmac_desc_pool =
  1680. dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  1681. sizeof(struct at_xdmac_desc), 4, 0);
  1682. if (!atxdmac->at_xdmac_desc_pool) {
  1683. dev_err(&pdev->dev, "no memory for descriptors dma pool\n");
  1684. ret = -ENOMEM;
  1685. goto err_clk_disable;
  1686. }
  1687. dma_cap_set(DMA_CYCLIC, atxdmac->dma.cap_mask);
  1688. dma_cap_set(DMA_INTERLEAVE, atxdmac->dma.cap_mask);
  1689. dma_cap_set(DMA_MEMCPY, atxdmac->dma.cap_mask);
  1690. dma_cap_set(DMA_MEMSET, atxdmac->dma.cap_mask);
  1691. dma_cap_set(DMA_MEMSET_SG, atxdmac->dma.cap_mask);
  1692. dma_cap_set(DMA_SLAVE, atxdmac->dma.cap_mask);
  1693. /*
  1694. * Without DMA_PRIVATE the driver is not able to allocate more than
  1695. * one channel, second allocation fails in private_candidate.
  1696. */
  1697. dma_cap_set(DMA_PRIVATE, atxdmac->dma.cap_mask);
  1698. atxdmac->dma.dev = &pdev->dev;
  1699. atxdmac->dma.device_alloc_chan_resources = at_xdmac_alloc_chan_resources;
  1700. atxdmac->dma.device_free_chan_resources = at_xdmac_free_chan_resources;
  1701. atxdmac->dma.device_tx_status = at_xdmac_tx_status;
  1702. atxdmac->dma.device_issue_pending = at_xdmac_issue_pending;
  1703. atxdmac->dma.device_prep_dma_cyclic = at_xdmac_prep_dma_cyclic;
  1704. atxdmac->dma.device_prep_interleaved_dma = at_xdmac_prep_interleaved;
  1705. atxdmac->dma.device_prep_dma_memcpy = at_xdmac_prep_dma_memcpy;
  1706. atxdmac->dma.device_prep_dma_memset = at_xdmac_prep_dma_memset;
  1707. atxdmac->dma.device_prep_dma_memset_sg = at_xdmac_prep_dma_memset_sg;
  1708. atxdmac->dma.device_prep_slave_sg = at_xdmac_prep_slave_sg;
  1709. atxdmac->dma.device_config = at_xdmac_device_config;
  1710. atxdmac->dma.device_pause = at_xdmac_device_pause;
  1711. atxdmac->dma.device_resume = at_xdmac_device_resume;
  1712. atxdmac->dma.device_terminate_all = at_xdmac_device_terminate_all;
  1713. atxdmac->dma.src_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1714. atxdmac->dma.dst_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
  1715. atxdmac->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1716. atxdmac->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1717. /* Disable all chans and interrupts. */
  1718. at_xdmac_off(atxdmac);
  1719. /* Init channels. */
  1720. INIT_LIST_HEAD(&atxdmac->dma.channels);
  1721. for (i = 0; i < nr_channels; i++) {
  1722. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1723. atchan->chan.device = &atxdmac->dma;
  1724. list_add_tail(&atchan->chan.device_node,
  1725. &atxdmac->dma.channels);
  1726. atchan->ch_regs = at_xdmac_chan_reg_base(atxdmac, i);
  1727. atchan->mask = 1 << i;
  1728. spin_lock_init(&atchan->lock);
  1729. INIT_LIST_HEAD(&atchan->xfers_list);
  1730. INIT_LIST_HEAD(&atchan->free_descs_list);
  1731. tasklet_init(&atchan->tasklet, at_xdmac_tasklet,
  1732. (unsigned long)atchan);
  1733. /* Clear pending interrupts. */
  1734. while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
  1735. cpu_relax();
  1736. }
  1737. platform_set_drvdata(pdev, atxdmac);
  1738. ret = dma_async_device_register(&atxdmac->dma);
  1739. if (ret) {
  1740. dev_err(&pdev->dev, "fail to register DMA engine device\n");
  1741. goto err_clk_disable;
  1742. }
  1743. ret = of_dma_controller_register(pdev->dev.of_node,
  1744. at_xdmac_xlate, atxdmac);
  1745. if (ret) {
  1746. dev_err(&pdev->dev, "could not register of dma controller\n");
  1747. goto err_dma_unregister;
  1748. }
  1749. dev_info(&pdev->dev, "%d channels, mapped at 0x%p\n",
  1750. nr_channels, atxdmac->regs);
  1751. return 0;
  1752. err_dma_unregister:
  1753. dma_async_device_unregister(&atxdmac->dma);
  1754. err_clk_disable:
  1755. clk_disable_unprepare(atxdmac->clk);
  1756. err_free_irq:
  1757. free_irq(atxdmac->irq, atxdmac);
  1758. return ret;
  1759. }
  1760. static int at_xdmac_remove(struct platform_device *pdev)
  1761. {
  1762. struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
  1763. int i;
  1764. at_xdmac_off(atxdmac);
  1765. of_dma_controller_free(pdev->dev.of_node);
  1766. dma_async_device_unregister(&atxdmac->dma);
  1767. clk_disable_unprepare(atxdmac->clk);
  1768. free_irq(atxdmac->irq, atxdmac);
  1769. for (i = 0; i < atxdmac->dma.chancnt; i++) {
  1770. struct at_xdmac_chan *atchan = &atxdmac->chan[i];
  1771. tasklet_kill(&atchan->tasklet);
  1772. at_xdmac_free_chan_resources(&atchan->chan);
  1773. }
  1774. return 0;
  1775. }
  1776. static const struct dev_pm_ops atmel_xdmac_dev_pm_ops = {
  1777. .prepare = atmel_xdmac_prepare,
  1778. SET_LATE_SYSTEM_SLEEP_PM_OPS(atmel_xdmac_suspend, atmel_xdmac_resume)
  1779. };
  1780. static const struct of_device_id atmel_xdmac_dt_ids[] = {
  1781. {
  1782. .compatible = "atmel,sama5d4-dma",
  1783. }, {
  1784. /* sentinel */
  1785. }
  1786. };
  1787. MODULE_DEVICE_TABLE(of, atmel_xdmac_dt_ids);
  1788. static struct platform_driver at_xdmac_driver = {
  1789. .probe = at_xdmac_probe,
  1790. .remove = at_xdmac_remove,
  1791. .driver = {
  1792. .name = "at_xdmac",
  1793. .of_match_table = of_match_ptr(atmel_xdmac_dt_ids),
  1794. .pm = &atmel_xdmac_dev_pm_ops,
  1795. }
  1796. };
  1797. static int __init at_xdmac_init(void)
  1798. {
  1799. return platform_driver_probe(&at_xdmac_driver, at_xdmac_probe);
  1800. }
  1801. subsys_initcall(at_xdmac_init);
  1802. MODULE_DESCRIPTION("Atmel Extended DMA Controller driver");
  1803. MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
  1804. MODULE_LICENSE("GPL");